i2c: gpio: fault-injector: refactor incomplete transfer
[linux/fpc-iii.git] / drivers / net / wireless / intersil / prism54 / islpci_dev.c
blob325176d4d796a6041488b0e2cd7fdffe3b2c4a08
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>
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
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <linux/hardirq.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
24 #include <linux/netdevice.h>
25 #include <linux/ethtool.h>
26 #include <linux/pci.h>
27 #include <linux/sched.h>
28 #include <linux/etherdevice.h>
29 #include <linux/delay.h>
30 #include <linux/if_arp.h>
32 #include <asm/io.h>
34 #include "prismcompat.h"
35 #include "isl_38xx.h"
36 #include "isl_ioctl.h"
37 #include "islpci_dev.h"
38 #include "islpci_mgt.h"
39 #include "islpci_eth.h"
40 #include "oid_mgt.h"
42 #define ISL3877_IMAGE_FILE "isl3877"
43 #define ISL3886_IMAGE_FILE "isl3886"
44 #define ISL3890_IMAGE_FILE "isl3890"
45 MODULE_FIRMWARE(ISL3877_IMAGE_FILE);
46 MODULE_FIRMWARE(ISL3886_IMAGE_FILE);
47 MODULE_FIRMWARE(ISL3890_IMAGE_FILE);
49 static int prism54_bring_down(islpci_private *);
50 static int islpci_alloc_memory(islpci_private *);
52 /* Temporary dummy MAC address to use until firmware is loaded.
53 * The idea there is that some tools (such as nameif) may query
54 * the MAC address before the netdev is 'open'. By using a valid
55 * OUI prefix, they can process the netdev properly.
56 * Of course, this is not the final/real MAC address. It doesn't
57 * matter, as you are suppose to be able to change it anytime via
58 * ndev->set_mac_address. Jean II */
59 static const unsigned char dummy_mac[6] = { 0x00, 0x30, 0xB4, 0x00, 0x00, 0x00 };
61 static int
62 isl_upload_firmware(islpci_private *priv)
64 u32 reg, rc;
65 void __iomem *device_base = priv->device_base;
67 /* clear the RAMBoot and the Reset bit */
68 reg = readl(device_base + ISL38XX_CTRL_STAT_REG);
69 reg &= ~ISL38XX_CTRL_STAT_RESET;
70 reg &= ~ISL38XX_CTRL_STAT_RAMBOOT;
71 writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
72 wmb();
73 udelay(ISL38XX_WRITEIO_DELAY);
75 /* set the Reset bit without reading the register ! */
76 reg |= ISL38XX_CTRL_STAT_RESET;
77 writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
78 wmb();
79 udelay(ISL38XX_WRITEIO_DELAY);
81 /* clear the Reset bit */
82 reg &= ~ISL38XX_CTRL_STAT_RESET;
83 writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
84 wmb();
86 /* wait a while for the device to reboot */
87 mdelay(50);
90 const struct firmware *fw_entry = NULL;
91 long fw_len;
92 const u32 *fw_ptr;
94 rc = request_firmware(&fw_entry, priv->firmware, PRISM_FW_PDEV);
95 if (rc) {
96 printk(KERN_ERR
97 "%s: request_firmware() failed for '%s'\n",
98 "prism54", priv->firmware);
99 return rc;
101 /* prepare the Direct Memory Base register */
102 reg = ISL38XX_DEV_FIRMWARE_ADDRES;
104 fw_ptr = (u32 *) fw_entry->data;
105 fw_len = fw_entry->size;
107 if (fw_len % 4) {
108 printk(KERN_ERR
109 "%s: firmware '%s' size is not multiple of 32bit, aborting!\n",
110 "prism54", priv->firmware);
111 release_firmware(fw_entry);
112 return -EILSEQ; /* Illegal byte sequence */;
115 while (fw_len > 0) {
116 long _fw_len =
117 (fw_len >
118 ISL38XX_MEMORY_WINDOW_SIZE) ?
119 ISL38XX_MEMORY_WINDOW_SIZE : fw_len;
120 u32 __iomem *dev_fw_ptr = device_base + ISL38XX_DIRECT_MEM_WIN;
122 /* set the card's base address for writing the data */
123 isl38xx_w32_flush(device_base, reg,
124 ISL38XX_DIR_MEM_BASE_REG);
125 wmb(); /* be paranoid */
127 /* increment the write address for next iteration */
128 reg += _fw_len;
129 fw_len -= _fw_len;
131 /* write the data to the Direct Memory Window 32bit-wise */
132 /* memcpy_toio() doesn't guarantee 32bit writes :-| */
133 while (_fw_len > 0) {
134 /* use non-swapping writel() */
135 __raw_writel(*fw_ptr, dev_fw_ptr);
136 fw_ptr++, dev_fw_ptr++;
137 _fw_len -= 4;
140 /* flush PCI posting */
141 (void) readl(device_base + ISL38XX_PCI_POSTING_FLUSH);
142 wmb(); /* be paranoid again */
144 BUG_ON(_fw_len != 0);
147 BUG_ON(fw_len != 0);
149 /* Firmware version is at offset 40 (also for "newmac") */
150 printk(KERN_DEBUG "%s: firmware version: %.8s\n",
151 priv->ndev->name, fw_entry->data + 40);
153 release_firmware(fw_entry);
156 /* now reset the device
157 * clear the Reset & ClkRun bit, set the RAMBoot bit */
158 reg = readl(device_base + ISL38XX_CTRL_STAT_REG);
159 reg &= ~ISL38XX_CTRL_STAT_CLKRUN;
160 reg &= ~ISL38XX_CTRL_STAT_RESET;
161 reg |= ISL38XX_CTRL_STAT_RAMBOOT;
162 isl38xx_w32_flush(device_base, reg, ISL38XX_CTRL_STAT_REG);
163 wmb();
164 udelay(ISL38XX_WRITEIO_DELAY);
166 /* set the reset bit latches the host override and RAMBoot bits
167 * into the device for operation when the reset bit is reset */
168 reg |= ISL38XX_CTRL_STAT_RESET;
169 writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
170 /* don't do flush PCI posting here! */
171 wmb();
172 udelay(ISL38XX_WRITEIO_DELAY);
174 /* clear the reset bit should start the whole circus */
175 reg &= ~ISL38XX_CTRL_STAT_RESET;
176 writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
177 /* don't do flush PCI posting here! */
178 wmb();
179 udelay(ISL38XX_WRITEIO_DELAY);
181 return 0;
184 /******************************************************************************
185 Device Interrupt Handler
186 ******************************************************************************/
188 irqreturn_t
189 islpci_interrupt(int irq, void *config)
191 u32 reg;
192 islpci_private *priv = config;
193 struct net_device *ndev = priv->ndev;
194 void __iomem *device = priv->device_base;
195 int powerstate = ISL38XX_PSM_POWERSAVE_STATE;
197 /* lock the interrupt handler */
198 spin_lock(&priv->slock);
200 /* received an interrupt request on a shared IRQ line
201 * first check whether the device is in sleep mode */
202 reg = readl(device + ISL38XX_CTRL_STAT_REG);
203 if (reg & ISL38XX_CTRL_STAT_SLEEPMODE)
204 /* device is in sleep mode, IRQ was generated by someone else */
206 #if VERBOSE > SHOW_ERROR_MESSAGES
207 DEBUG(SHOW_TRACING, "Assuming someone else called the IRQ\n");
208 #endif
209 spin_unlock(&priv->slock);
210 return IRQ_NONE;
214 /* check whether there is any source of interrupt on the device */
215 reg = readl(device + ISL38XX_INT_IDENT_REG);
217 /* also check the contents of the Interrupt Enable Register, because this
218 * will filter out interrupt sources from other devices on the same irq ! */
219 reg &= readl(device + ISL38XX_INT_EN_REG);
220 reg &= ISL38XX_INT_SOURCES;
222 if (reg != 0) {
223 if (islpci_get_state(priv) != PRV_STATE_SLEEP)
224 powerstate = ISL38XX_PSM_ACTIVE_STATE;
226 /* reset the request bits in the Identification register */
227 isl38xx_w32_flush(device, reg, ISL38XX_INT_ACK_REG);
229 #if VERBOSE > SHOW_ERROR_MESSAGES
230 DEBUG(SHOW_FUNCTION_CALLS,
231 "IRQ: Identification register 0x%p 0x%x\n", device, reg);
232 #endif
234 /* check for each bit in the register separately */
235 if (reg & ISL38XX_INT_IDENT_UPDATE) {
236 #if VERBOSE > SHOW_ERROR_MESSAGES
237 /* Queue has been updated */
238 DEBUG(SHOW_TRACING, "IRQ: Update flag\n");
240 DEBUG(SHOW_QUEUE_INDEXES,
241 "CB drv Qs: [%i][%i][%i][%i][%i][%i]\n",
242 le32_to_cpu(priv->control_block->
243 driver_curr_frag[0]),
244 le32_to_cpu(priv->control_block->
245 driver_curr_frag[1]),
246 le32_to_cpu(priv->control_block->
247 driver_curr_frag[2]),
248 le32_to_cpu(priv->control_block->
249 driver_curr_frag[3]),
250 le32_to_cpu(priv->control_block->
251 driver_curr_frag[4]),
252 le32_to_cpu(priv->control_block->
253 driver_curr_frag[5])
256 DEBUG(SHOW_QUEUE_INDEXES,
257 "CB dev Qs: [%i][%i][%i][%i][%i][%i]\n",
258 le32_to_cpu(priv->control_block->
259 device_curr_frag[0]),
260 le32_to_cpu(priv->control_block->
261 device_curr_frag[1]),
262 le32_to_cpu(priv->control_block->
263 device_curr_frag[2]),
264 le32_to_cpu(priv->control_block->
265 device_curr_frag[3]),
266 le32_to_cpu(priv->control_block->
267 device_curr_frag[4]),
268 le32_to_cpu(priv->control_block->
269 device_curr_frag[5])
271 #endif
273 /* cleanup the data low transmit queue */
274 islpci_eth_cleanup_transmit(priv, priv->control_block);
276 /* device is in active state, update the
277 * powerstate flag if necessary */
278 powerstate = ISL38XX_PSM_ACTIVE_STATE;
280 /* check all three queues in priority order
281 * call the PIMFOR receive function until the
282 * queue is empty */
283 if (isl38xx_in_queue(priv->control_block,
284 ISL38XX_CB_RX_MGMTQ) != 0) {
285 #if VERBOSE > SHOW_ERROR_MESSAGES
286 DEBUG(SHOW_TRACING,
287 "Received frame in Management Queue\n");
288 #endif
289 islpci_mgt_receive(ndev);
291 islpci_mgt_cleanup_transmit(ndev);
293 /* Refill slots in receive queue */
294 islpci_mgmt_rx_fill(ndev);
296 /* no need to trigger the device, next
297 islpci_mgt_transaction does it */
300 while (isl38xx_in_queue(priv->control_block,
301 ISL38XX_CB_RX_DATA_LQ) != 0) {
302 #if VERBOSE > SHOW_ERROR_MESSAGES
303 DEBUG(SHOW_TRACING,
304 "Received frame in Data Low Queue\n");
305 #endif
306 islpci_eth_receive(priv);
309 /* check whether the data transmit queues were full */
310 if (priv->data_low_tx_full) {
311 /* check whether the transmit is not full anymore */
312 if (ISL38XX_CB_TX_QSIZE -
313 isl38xx_in_queue(priv->control_block,
314 ISL38XX_CB_TX_DATA_LQ) >=
315 ISL38XX_MIN_QTHRESHOLD) {
316 /* nope, the driver is ready for more network frames */
317 netif_wake_queue(priv->ndev);
319 /* reset the full flag */
320 priv->data_low_tx_full = 0;
325 if (reg & ISL38XX_INT_IDENT_INIT) {
326 /* Device has been initialized */
327 #if VERBOSE > SHOW_ERROR_MESSAGES
328 DEBUG(SHOW_TRACING,
329 "IRQ: Init flag, device initialized\n");
330 #endif
331 wake_up(&priv->reset_done);
334 if (reg & ISL38XX_INT_IDENT_SLEEP) {
335 /* Device intends to move to powersave state */
336 #if VERBOSE > SHOW_ERROR_MESSAGES
337 DEBUG(SHOW_TRACING, "IRQ: Sleep flag\n");
338 #endif
339 isl38xx_handle_sleep_request(priv->control_block,
340 &powerstate,
341 priv->device_base);
344 if (reg & ISL38XX_INT_IDENT_WAKEUP) {
345 /* Device has been woken up to active state */
346 #if VERBOSE > SHOW_ERROR_MESSAGES
347 DEBUG(SHOW_TRACING, "IRQ: Wakeup flag\n");
348 #endif
350 isl38xx_handle_wakeup(priv->control_block,
351 &powerstate, priv->device_base);
353 } else {
354 #if VERBOSE > SHOW_ERROR_MESSAGES
355 DEBUG(SHOW_TRACING, "Assuming someone else called the IRQ\n");
356 #endif
357 spin_unlock(&priv->slock);
358 return IRQ_NONE;
361 /* sleep -> ready */
362 if (islpci_get_state(priv) == PRV_STATE_SLEEP
363 && powerstate == ISL38XX_PSM_ACTIVE_STATE)
364 islpci_set_state(priv, PRV_STATE_READY);
366 /* !sleep -> sleep */
367 if (islpci_get_state(priv) != PRV_STATE_SLEEP
368 && powerstate == ISL38XX_PSM_POWERSAVE_STATE)
369 islpci_set_state(priv, PRV_STATE_SLEEP);
371 /* unlock the interrupt handler */
372 spin_unlock(&priv->slock);
374 return IRQ_HANDLED;
377 /******************************************************************************
378 Network Interface Control & Statistical functions
379 ******************************************************************************/
380 static int
381 islpci_open(struct net_device *ndev)
383 u32 rc;
384 islpci_private *priv = netdev_priv(ndev);
386 /* reset data structures, upload firmware and reset device */
387 rc = islpci_reset(priv,1);
388 if (rc) {
389 prism54_bring_down(priv);
390 return rc; /* Returns informative message */
393 netif_start_queue(ndev);
395 /* Turn off carrier if in STA or Ad-hoc mode. It will be turned on
396 * once the firmware receives a trap of being associated
397 * (GEN_OID_LINKSTATE). In other modes (AP or WDS or monitor) we
398 * should just leave the carrier on as its expected the firmware
399 * won't send us a trigger. */
400 if (priv->iw_mode == IW_MODE_INFRA || priv->iw_mode == IW_MODE_ADHOC)
401 netif_carrier_off(ndev);
402 else
403 netif_carrier_on(ndev);
405 return 0;
408 static int
409 islpci_close(struct net_device *ndev)
411 islpci_private *priv = netdev_priv(ndev);
413 printk(KERN_DEBUG "%s: islpci_close ()\n", ndev->name);
415 netif_stop_queue(ndev);
417 return prism54_bring_down(priv);
420 static int
421 prism54_bring_down(islpci_private *priv)
423 void __iomem *device_base = priv->device_base;
424 u32 reg;
425 /* we are going to shutdown the device */
426 islpci_set_state(priv, PRV_STATE_PREBOOT);
428 /* disable all device interrupts in case they weren't */
429 isl38xx_disable_interrupts(priv->device_base);
431 /* For safety reasons, we may want to ensure that no DMA transfer is
432 * currently in progress by emptying the TX and RX queues. */
434 /* wait until interrupts have finished executing on other CPUs */
435 synchronize_irq(priv->pdev->irq);
437 reg = readl(device_base + ISL38XX_CTRL_STAT_REG);
438 reg &= ~(ISL38XX_CTRL_STAT_RESET | ISL38XX_CTRL_STAT_RAMBOOT);
439 writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
440 wmb();
441 udelay(ISL38XX_WRITEIO_DELAY);
443 reg |= ISL38XX_CTRL_STAT_RESET;
444 writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
445 wmb();
446 udelay(ISL38XX_WRITEIO_DELAY);
448 /* clear the Reset bit */
449 reg &= ~ISL38XX_CTRL_STAT_RESET;
450 writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
451 wmb();
453 /* wait a while for the device to reset */
454 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
456 return 0;
459 static int
460 islpci_upload_fw(islpci_private *priv)
462 islpci_state_t old_state;
463 u32 rc;
465 old_state = islpci_set_state(priv, PRV_STATE_BOOT);
467 printk(KERN_DEBUG "%s: uploading firmware...\n", priv->ndev->name);
469 rc = isl_upload_firmware(priv);
470 if (rc) {
471 /* error uploading the firmware */
472 printk(KERN_ERR "%s: could not upload firmware ('%s')\n",
473 priv->ndev->name, priv->firmware);
475 islpci_set_state(priv, old_state);
476 return rc;
479 printk(KERN_DEBUG "%s: firmware upload complete\n",
480 priv->ndev->name);
482 islpci_set_state(priv, PRV_STATE_POSTBOOT);
484 return 0;
487 static int
488 islpci_reset_if(islpci_private *priv)
490 long remaining;
491 int result = -ETIME;
492 int count;
494 DEFINE_WAIT(wait);
495 prepare_to_wait(&priv->reset_done, &wait, TASK_UNINTERRUPTIBLE);
497 /* now the last step is to reset the interface */
498 isl38xx_interface_reset(priv->device_base, priv->device_host_address);
499 islpci_set_state(priv, PRV_STATE_PREINIT);
501 for(count = 0; count < 2 && result; count++) {
502 /* The software reset acknowledge needs about 220 msec here.
503 * Be conservative and wait for up to one second. */
505 remaining = schedule_timeout_uninterruptible(HZ);
507 if(remaining > 0) {
508 result = 0;
509 break;
512 /* If we're here it's because our IRQ hasn't yet gone through.
513 * Retry a bit more...
515 printk(KERN_ERR "%s: no 'reset complete' IRQ seen - retrying\n",
516 priv->ndev->name);
519 finish_wait(&priv->reset_done, &wait);
521 if (result) {
522 printk(KERN_ERR "%s: interface reset failure\n", priv->ndev->name);
523 return result;
526 islpci_set_state(priv, PRV_STATE_INIT);
528 /* Now that the device is 100% up, let's allow
529 * for the other interrupts --
530 * NOTE: this is not *yet* true since we've only allowed the
531 * INIT interrupt on the IRQ line. We can perhaps poll
532 * the IRQ line until we know for sure the reset went through */
533 isl38xx_enable_common_interrupts(priv->device_base);
535 down_write(&priv->mib_sem);
536 result = mgt_commit(priv);
537 if (result) {
538 printk(KERN_ERR "%s: interface reset failure\n", priv->ndev->name);
539 up_write(&priv->mib_sem);
540 return result;
542 up_write(&priv->mib_sem);
544 islpci_set_state(priv, PRV_STATE_READY);
546 printk(KERN_DEBUG "%s: interface reset complete\n", priv->ndev->name);
547 return 0;
551 islpci_reset(islpci_private *priv, int reload_firmware)
553 isl38xx_control_block *cb = /* volatile not needed */
554 (isl38xx_control_block *) priv->control_block;
555 unsigned counter;
556 int rc;
558 if (reload_firmware)
559 islpci_set_state(priv, PRV_STATE_PREBOOT);
560 else
561 islpci_set_state(priv, PRV_STATE_POSTBOOT);
563 printk(KERN_DEBUG "%s: resetting device...\n", priv->ndev->name);
565 /* disable all device interrupts in case they weren't */
566 isl38xx_disable_interrupts(priv->device_base);
568 /* flush all management queues */
569 priv->index_mgmt_tx = 0;
570 priv->index_mgmt_rx = 0;
572 /* clear the indexes in the frame pointer */
573 for (counter = 0; counter < ISL38XX_CB_QCOUNT; counter++) {
574 cb->driver_curr_frag[counter] = cpu_to_le32(0);
575 cb->device_curr_frag[counter] = cpu_to_le32(0);
578 /* reset the mgmt receive queue */
579 for (counter = 0; counter < ISL38XX_CB_MGMT_QSIZE; counter++) {
580 isl38xx_fragment *frag = &cb->rx_data_mgmt[counter];
581 frag->size = cpu_to_le16(MGMT_FRAME_SIZE);
582 frag->flags = 0;
583 frag->address = cpu_to_le32(priv->mgmt_rx[counter].pci_addr);
586 for (counter = 0; counter < ISL38XX_CB_RX_QSIZE; counter++) {
587 cb->rx_data_low[counter].address =
588 cpu_to_le32((u32) priv->pci_map_rx_address[counter]);
591 /* since the receive queues are filled with empty fragments, now we can
592 * set the corresponding indexes in the Control Block */
593 priv->control_block->driver_curr_frag[ISL38XX_CB_RX_DATA_LQ] =
594 cpu_to_le32(ISL38XX_CB_RX_QSIZE);
595 priv->control_block->driver_curr_frag[ISL38XX_CB_RX_MGMTQ] =
596 cpu_to_le32(ISL38XX_CB_MGMT_QSIZE);
598 /* reset the remaining real index registers and full flags */
599 priv->free_data_rx = 0;
600 priv->free_data_tx = 0;
601 priv->data_low_tx_full = 0;
603 if (reload_firmware) { /* Should we load the firmware ? */
604 /* now that the data structures are cleaned up, upload
605 * firmware and reset interface */
606 rc = islpci_upload_fw(priv);
607 if (rc) {
608 printk(KERN_ERR "%s: islpci_reset: failure\n",
609 priv->ndev->name);
610 return rc;
614 /* finally reset interface */
615 rc = islpci_reset_if(priv);
616 if (rc)
617 printk(KERN_ERR "prism54: Your card/socket may be faulty, or IRQ line too busy :(\n");
618 return rc;
621 /******************************************************************************
622 Network device configuration functions
623 ******************************************************************************/
624 static int
625 islpci_alloc_memory(islpci_private *priv)
627 int counter;
629 #if VERBOSE > SHOW_ERROR_MESSAGES
630 printk(KERN_DEBUG "islpci_alloc_memory\n");
631 #endif
633 /* remap the PCI device base address to accessible */
634 if (!(priv->device_base =
635 ioremap(pci_resource_start(priv->pdev, 0),
636 ISL38XX_PCI_MEM_SIZE))) {
637 /* error in remapping the PCI device memory address range */
638 printk(KERN_ERR "PCI memory remapping failed\n");
639 return -1;
642 /* memory layout for consistent DMA region:
644 * Area 1: Control Block for the device interface
645 * Area 2: Power Save Mode Buffer for temporary frame storage. Be aware that
646 * the number of supported stations in the AP determines the minimal
647 * size of the buffer !
650 /* perform the allocation */
651 priv->driver_mem_address = pci_alloc_consistent(priv->pdev,
652 HOST_MEM_BLOCK,
653 &priv->
654 device_host_address);
656 if (!priv->driver_mem_address) {
657 /* error allocating the block of PCI memory */
658 printk(KERN_ERR "%s: could not allocate DMA memory, aborting!",
659 "prism54");
660 return -1;
663 /* assign the Control Block to the first address of the allocated area */
664 priv->control_block =
665 (isl38xx_control_block *) priv->driver_mem_address;
667 /* set the Power Save Buffer pointer directly behind the CB */
668 priv->device_psm_buffer =
669 priv->device_host_address + CONTROL_BLOCK_SIZE;
671 /* make sure all buffer pointers are initialized */
672 for (counter = 0; counter < ISL38XX_CB_QCOUNT; counter++) {
673 priv->control_block->driver_curr_frag[counter] = cpu_to_le32(0);
674 priv->control_block->device_curr_frag[counter] = cpu_to_le32(0);
677 priv->index_mgmt_rx = 0;
678 memset(priv->mgmt_rx, 0, sizeof(priv->mgmt_rx));
679 memset(priv->mgmt_tx, 0, sizeof(priv->mgmt_tx));
681 /* allocate rx queue for management frames */
682 if (islpci_mgmt_rx_fill(priv->ndev) < 0)
683 goto out_free;
685 /* now get the data rx skb's */
686 memset(priv->data_low_rx, 0, sizeof (priv->data_low_rx));
687 memset(priv->pci_map_rx_address, 0, sizeof (priv->pci_map_rx_address));
689 for (counter = 0; counter < ISL38XX_CB_RX_QSIZE; counter++) {
690 struct sk_buff *skb;
692 /* allocate an sk_buff for received data frames storage
693 * each frame on receive size consists of 1 fragment
694 * include any required allignment operations */
695 if (!(skb = dev_alloc_skb(MAX_FRAGMENT_SIZE_RX + 2))) {
696 /* error allocating an sk_buff structure elements */
697 printk(KERN_ERR "Error allocating skb.\n");
698 skb = NULL;
699 goto out_free;
701 skb_reserve(skb, (4 - (long) skb->data) & 0x03);
702 /* add the new allocated sk_buff to the buffer array */
703 priv->data_low_rx[counter] = skb;
705 /* map the allocated skb data area to pci */
706 priv->pci_map_rx_address[counter] =
707 pci_map_single(priv->pdev, (void *) skb->data,
708 MAX_FRAGMENT_SIZE_RX + 2,
709 PCI_DMA_FROMDEVICE);
710 if (pci_dma_mapping_error(priv->pdev,
711 priv->pci_map_rx_address[counter])) {
712 priv->pci_map_rx_address[counter] = 0;
713 /* error mapping the buffer to device
714 accessible memory address */
715 printk(KERN_ERR "failed to map skb DMA'able\n");
716 goto out_free;
720 prism54_acl_init(&priv->acl);
721 prism54_wpa_bss_ie_init(priv);
722 if (mgt_init(priv))
723 goto out_free;
725 return 0;
726 out_free:
727 islpci_free_memory(priv);
728 return -1;
732 islpci_free_memory(islpci_private *priv)
734 int counter;
736 if (priv->device_base)
737 iounmap(priv->device_base);
738 priv->device_base = NULL;
740 /* free consistent DMA area... */
741 if (priv->driver_mem_address)
742 pci_free_consistent(priv->pdev, HOST_MEM_BLOCK,
743 priv->driver_mem_address,
744 priv->device_host_address);
746 /* clear some dangling pointers */
747 priv->driver_mem_address = NULL;
748 priv->device_host_address = 0;
749 priv->device_psm_buffer = 0;
750 priv->control_block = NULL;
752 /* clean up mgmt rx buffers */
753 for (counter = 0; counter < ISL38XX_CB_MGMT_QSIZE; counter++) {
754 struct islpci_membuf *buf = &priv->mgmt_rx[counter];
755 if (buf->pci_addr)
756 pci_unmap_single(priv->pdev, buf->pci_addr,
757 buf->size, PCI_DMA_FROMDEVICE);
758 buf->pci_addr = 0;
759 kfree(buf->mem);
760 buf->size = 0;
761 buf->mem = NULL;
764 /* clean up data rx buffers */
765 for (counter = 0; counter < ISL38XX_CB_RX_QSIZE; counter++) {
766 if (priv->pci_map_rx_address[counter])
767 pci_unmap_single(priv->pdev,
768 priv->pci_map_rx_address[counter],
769 MAX_FRAGMENT_SIZE_RX + 2,
770 PCI_DMA_FROMDEVICE);
771 priv->pci_map_rx_address[counter] = 0;
773 if (priv->data_low_rx[counter])
774 dev_kfree_skb(priv->data_low_rx[counter]);
775 priv->data_low_rx[counter] = NULL;
778 /* Free the access control list and the WPA list */
779 prism54_acl_clean(&priv->acl);
780 prism54_wpa_bss_ie_clean(priv);
781 mgt_clean(priv);
783 return 0;
786 #if 0
787 static void
788 islpci_set_multicast_list(struct net_device *dev)
790 /* put device into promisc mode and let network layer handle it */
792 #endif
794 static void islpci_ethtool_get_drvinfo(struct net_device *dev,
795 struct ethtool_drvinfo *info)
797 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
798 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
801 static const struct ethtool_ops islpci_ethtool_ops = {
802 .get_drvinfo = islpci_ethtool_get_drvinfo,
805 static const struct net_device_ops islpci_netdev_ops = {
806 .ndo_open = islpci_open,
807 .ndo_stop = islpci_close,
808 .ndo_start_xmit = islpci_eth_transmit,
809 .ndo_tx_timeout = islpci_eth_tx_timeout,
810 .ndo_set_mac_address = prism54_set_mac_address,
811 .ndo_validate_addr = eth_validate_addr,
814 static struct device_type wlan_type = {
815 .name = "wlan",
818 struct net_device *
819 islpci_setup(struct pci_dev *pdev)
821 islpci_private *priv;
822 struct net_device *ndev = alloc_etherdev(sizeof (islpci_private));
824 if (!ndev)
825 return ndev;
827 pci_set_drvdata(pdev, ndev);
828 SET_NETDEV_DEV(ndev, &pdev->dev);
829 SET_NETDEV_DEVTYPE(ndev, &wlan_type);
831 /* setup the structure members */
832 ndev->base_addr = pci_resource_start(pdev, 0);
833 ndev->irq = pdev->irq;
835 /* initialize the function pointers */
836 ndev->netdev_ops = &islpci_netdev_ops;
837 ndev->wireless_handlers = &prism54_handler_def;
838 ndev->ethtool_ops = &islpci_ethtool_ops;
840 /* ndev->set_multicast_list = &islpci_set_multicast_list; */
841 ndev->addr_len = ETH_ALEN;
842 /* Get a non-zero dummy MAC address for nameif. Jean II */
843 memcpy(ndev->dev_addr, dummy_mac, ETH_ALEN);
845 ndev->watchdog_timeo = ISLPCI_TX_TIMEOUT;
847 /* allocate a private device structure to the network device */
848 priv = netdev_priv(ndev);
849 priv->ndev = ndev;
850 priv->pdev = pdev;
851 priv->monitor_type = ARPHRD_IEEE80211;
852 priv->ndev->type = (priv->iw_mode == IW_MODE_MONITOR) ?
853 priv->monitor_type : ARPHRD_ETHER;
855 /* Add pointers to enable iwspy support. */
856 priv->wireless_data.spy_data = &priv->spy_data;
857 ndev->wireless_data = &priv->wireless_data;
859 /* save the start and end address of the PCI memory area */
860 ndev->mem_start = (unsigned long) priv->device_base;
861 ndev->mem_end = ndev->mem_start + ISL38XX_PCI_MEM_SIZE;
863 #if VERBOSE > SHOW_ERROR_MESSAGES
864 DEBUG(SHOW_TRACING, "PCI Memory remapped to 0x%p\n", priv->device_base);
865 #endif
867 init_waitqueue_head(&priv->reset_done);
869 /* init the queue read locks, process wait counter */
870 mutex_init(&priv->mgmt_lock);
871 priv->mgmt_received = NULL;
872 init_waitqueue_head(&priv->mgmt_wqueue);
873 mutex_init(&priv->stats_lock);
874 spin_lock_init(&priv->slock);
876 /* init state machine with off#1 state */
877 priv->state = PRV_STATE_OFF;
878 priv->state_off = 1;
880 /* initialize workqueue's */
881 INIT_WORK(&priv->stats_work, prism54_update_stats);
882 priv->stats_timestamp = 0;
884 INIT_WORK(&priv->reset_task, islpci_do_reset_and_wake);
885 priv->reset_task_pending = 0;
887 /* allocate various memory areas */
888 if (islpci_alloc_memory(priv))
889 goto do_free_netdev;
891 /* select the firmware file depending on the device id */
892 switch (pdev->device) {
893 case 0x3877:
894 strcpy(priv->firmware, ISL3877_IMAGE_FILE);
895 break;
897 case 0x3886:
898 strcpy(priv->firmware, ISL3886_IMAGE_FILE);
899 break;
901 default:
902 strcpy(priv->firmware, ISL3890_IMAGE_FILE);
903 break;
906 if (register_netdev(ndev)) {
907 DEBUG(SHOW_ERROR_MESSAGES,
908 "ERROR: register_netdev() failed\n");
909 goto do_islpci_free_memory;
912 return ndev;
914 do_islpci_free_memory:
915 islpci_free_memory(priv);
916 do_free_netdev:
917 free_netdev(ndev);
918 priv = NULL;
919 return NULL;
922 islpci_state_t
923 islpci_set_state(islpci_private *priv, islpci_state_t new_state)
925 islpci_state_t old_state;
927 /* lock */
928 old_state = priv->state;
930 /* this means either a race condition or some serious error in
931 * the driver code */
932 switch (new_state) {
933 case PRV_STATE_OFF:
934 priv->state_off++;
935 default:
936 priv->state = new_state;
937 break;
939 case PRV_STATE_PREBOOT:
940 /* there are actually many off-states, enumerated by
941 * state_off */
942 if (old_state == PRV_STATE_OFF)
943 priv->state_off--;
945 /* only if hw_unavailable is zero now it means we either
946 * were in off#1 state, or came here from
947 * somewhere else */
948 if (!priv->state_off)
949 priv->state = new_state;
950 break;
952 #if 0
953 printk(KERN_DEBUG "%s: state transition %d -> %d (off#%d)\n",
954 priv->ndev->name, old_state, new_state, priv->state_off);
955 #endif
957 /* invariants */
958 BUG_ON(priv->state_off < 0);
959 BUG_ON(priv->state_off && (priv->state != PRV_STATE_OFF));
960 BUG_ON(!priv->state_off && (priv->state == PRV_STATE_OFF));
962 /* unlock */
963 return old_state;