blob: 3e6a71ce5b5433a736a929b92af1e7517fc550e3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
22
23#include <linux/netdevice.h>
Kai Engertff86a542006-12-12 21:09:41 +010024#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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"
Ben Hutchingsa830e652009-11-07 22:01:55 +000043MODULE_FIRMWARE(ISL3877_IMAGE_FILE);
44MODULE_FIRMWARE(ISL3886_IMAGE_FILE);
45MODULE_FIRMWARE(ISL3890_IMAGE_FILE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47static int prism54_bring_down(islpci_private *);
48static int islpci_alloc_memory(islpci_private *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* Temporary dummy MAC address to use until firmware is loaded.
51 * The idea there is that some tools (such as nameif) may query
52 * the MAC address before the netdev is 'open'. By using a valid
53 * OUI prefix, they can process the netdev properly.
54 * Of course, this is not the final/real MAC address. It doesn't
55 * matter, as you are suppose to be able to change it anytime via
56 * ndev->set_mac_address. Jean II */
57static const unsigned char dummy_mac[6] = { 0x00, 0x30, 0xB4, 0x00, 0x00, 0x00 };
58
59static int
60isl_upload_firmware(islpci_private *priv)
61{
62 u32 reg, rc;
63 void __iomem *device_base = priv->device_base;
64
65 /* clear the RAMBoot and the Reset bit */
66 reg = readl(device_base + ISL38XX_CTRL_STAT_REG);
67 reg &= ~ISL38XX_CTRL_STAT_RESET;
68 reg &= ~ISL38XX_CTRL_STAT_RAMBOOT;
69 writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
70 wmb();
71 udelay(ISL38XX_WRITEIO_DELAY);
72
73 /* set the Reset bit without reading the register ! */
74 reg |= ISL38XX_CTRL_STAT_RESET;
75 writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
76 wmb();
77 udelay(ISL38XX_WRITEIO_DELAY);
78
79 /* clear the Reset bit */
80 reg &= ~ISL38XX_CTRL_STAT_RESET;
81 writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
82 wmb();
83
84 /* wait a while for the device to reboot */
85 mdelay(50);
86
87 {
88 const struct firmware *fw_entry = NULL;
89 long fw_len;
90 const u32 *fw_ptr;
91
92 rc = request_firmware(&fw_entry, priv->firmware, PRISM_FW_PDEV);
93 if (rc) {
94 printk(KERN_ERR
95 "%s: request_firmware() failed for '%s'\n",
96 "prism54", priv->firmware);
97 return rc;
98 }
99 /* prepare the Direct Memory Base register */
100 reg = ISL38XX_DEV_FIRMWARE_ADDRES;
101
102 fw_ptr = (u32 *) fw_entry->data;
103 fw_len = fw_entry->size;
104
105 if (fw_len % 4) {
106 printk(KERN_ERR
107 "%s: firmware '%s' size is not multiple of 32bit, aborting!\n",
108 "prism54", priv->firmware);
109 release_firmware(fw_entry);
110 return -EILSEQ; /* Illegal byte sequence */;
111 }
112
113 while (fw_len > 0) {
114 long _fw_len =
115 (fw_len >
116 ISL38XX_MEMORY_WINDOW_SIZE) ?
117 ISL38XX_MEMORY_WINDOW_SIZE : fw_len;
118 u32 __iomem *dev_fw_ptr = device_base + ISL38XX_DIRECT_MEM_WIN;
119
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200120 /* set the card's base address for writing the data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 isl38xx_w32_flush(device_base, reg,
122 ISL38XX_DIR_MEM_BASE_REG);
123 wmb(); /* be paranoid */
124
125 /* increment the write address for next iteration */
126 reg += _fw_len;
127 fw_len -= _fw_len;
128
129 /* write the data to the Direct Memory Window 32bit-wise */
130 /* memcpy_toio() doesn't guarantee 32bit writes :-| */
131 while (_fw_len > 0) {
132 /* use non-swapping writel() */
133 __raw_writel(*fw_ptr, dev_fw_ptr);
134 fw_ptr++, dev_fw_ptr++;
135 _fw_len -= 4;
136 }
137
138 /* flush PCI posting */
139 (void) readl(device_base + ISL38XX_PCI_POSTING_FLUSH);
140 wmb(); /* be paranoid again */
141
142 BUG_ON(_fw_len != 0);
143 }
144
145 BUG_ON(fw_len != 0);
146
147 /* Firmware version is at offset 40 (also for "newmac") */
148 printk(KERN_DEBUG "%s: firmware version: %.8s\n",
149 priv->ndev->name, fw_entry->data + 40);
150
151 release_firmware(fw_entry);
152 }
153
154 /* now reset the device
155 * clear the Reset & ClkRun bit, set the RAMBoot bit */
156 reg = readl(device_base + ISL38XX_CTRL_STAT_REG);
157 reg &= ~ISL38XX_CTRL_STAT_CLKRUN;
158 reg &= ~ISL38XX_CTRL_STAT_RESET;
159 reg |= ISL38XX_CTRL_STAT_RAMBOOT;
160 isl38xx_w32_flush(device_base, reg, ISL38XX_CTRL_STAT_REG);
161 wmb();
162 udelay(ISL38XX_WRITEIO_DELAY);
163
164 /* set the reset bit latches the host override and RAMBoot bits
165 * into the device for operation when the reset bit is reset */
166 reg |= ISL38XX_CTRL_STAT_RESET;
167 writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
168 /* don't do flush PCI posting here! */
169 wmb();
170 udelay(ISL38XX_WRITEIO_DELAY);
171
172 /* clear the reset bit should start the whole circus */
173 reg &= ~ISL38XX_CTRL_STAT_RESET;
174 writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
175 /* don't do flush PCI posting here! */
176 wmb();
177 udelay(ISL38XX_WRITEIO_DELAY);
178
179 return 0;
180}
181
182/******************************************************************************
183 Device Interrupt Handler
184******************************************************************************/
185
186irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100187islpci_interrupt(int irq, void *config)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 u32 reg;
190 islpci_private *priv = config;
191 struct net_device *ndev = priv->ndev;
192 void __iomem *device = priv->device_base;
193 int powerstate = ISL38XX_PSM_POWERSAVE_STATE;
194
195 /* lock the interrupt handler */
196 spin_lock(&priv->slock);
197
198 /* received an interrupt request on a shared IRQ line
199 * first check whether the device is in sleep mode */
200 reg = readl(device + ISL38XX_CTRL_STAT_REG);
201 if (reg & ISL38XX_CTRL_STAT_SLEEPMODE)
202 /* device is in sleep mode, IRQ was generated by someone else */
203 {
204#if VERBOSE > SHOW_ERROR_MESSAGES
205 DEBUG(SHOW_TRACING, "Assuming someone else called the IRQ\n");
206#endif
207 spin_unlock(&priv->slock);
208 return IRQ_NONE;
209 }
210
211
212 /* check whether there is any source of interrupt on the device */
213 reg = readl(device + ISL38XX_INT_IDENT_REG);
214
215 /* also check the contents of the Interrupt Enable Register, because this
216 * will filter out interrupt sources from other devices on the same irq ! */
217 reg &= readl(device + ISL38XX_INT_EN_REG);
218 reg &= ISL38XX_INT_SOURCES;
219
220 if (reg != 0) {
221 if (islpci_get_state(priv) != PRV_STATE_SLEEP)
222 powerstate = ISL38XX_PSM_ACTIVE_STATE;
223
224 /* reset the request bits in the Identification register */
225 isl38xx_w32_flush(device, reg, ISL38XX_INT_ACK_REG);
226
227#if VERBOSE > SHOW_ERROR_MESSAGES
228 DEBUG(SHOW_FUNCTION_CALLS,
229 "IRQ: Identification register 0x%p 0x%x \n", device, reg);
230#endif
231
232 /* check for each bit in the register separately */
233 if (reg & ISL38XX_INT_IDENT_UPDATE) {
234#if VERBOSE > SHOW_ERROR_MESSAGES
235 /* Queue has been updated */
236 DEBUG(SHOW_TRACING, "IRQ: Update flag \n");
237
238 DEBUG(SHOW_QUEUE_INDEXES,
239 "CB drv Qs: [%i][%i][%i][%i][%i][%i]\n",
240 le32_to_cpu(priv->control_block->
241 driver_curr_frag[0]),
242 le32_to_cpu(priv->control_block->
243 driver_curr_frag[1]),
244 le32_to_cpu(priv->control_block->
245 driver_curr_frag[2]),
246 le32_to_cpu(priv->control_block->
247 driver_curr_frag[3]),
248 le32_to_cpu(priv->control_block->
249 driver_curr_frag[4]),
250 le32_to_cpu(priv->control_block->
251 driver_curr_frag[5])
252 );
253
254 DEBUG(SHOW_QUEUE_INDEXES,
255 "CB dev Qs: [%i][%i][%i][%i][%i][%i]\n",
256 le32_to_cpu(priv->control_block->
257 device_curr_frag[0]),
258 le32_to_cpu(priv->control_block->
259 device_curr_frag[1]),
260 le32_to_cpu(priv->control_block->
261 device_curr_frag[2]),
262 le32_to_cpu(priv->control_block->
263 device_curr_frag[3]),
264 le32_to_cpu(priv->control_block->
265 device_curr_frag[4]),
266 le32_to_cpu(priv->control_block->
267 device_curr_frag[5])
268 );
269#endif
270
271 /* cleanup the data low transmit queue */
272 islpci_eth_cleanup_transmit(priv, priv->control_block);
273
274 /* device is in active state, update the
275 * powerstate flag if necessary */
276 powerstate = ISL38XX_PSM_ACTIVE_STATE;
277
278 /* check all three queues in priority order
279 * call the PIMFOR receive function until the
280 * queue is empty */
281 if (isl38xx_in_queue(priv->control_block,
282 ISL38XX_CB_RX_MGMTQ) != 0) {
283#if VERBOSE > SHOW_ERROR_MESSAGES
284 DEBUG(SHOW_TRACING,
285 "Received frame in Management Queue\n");
286#endif
287 islpci_mgt_receive(ndev);
288
289 islpci_mgt_cleanup_transmit(ndev);
290
291 /* Refill slots in receive queue */
292 islpci_mgmt_rx_fill(ndev);
293
294 /* no need to trigger the device, next
295 islpci_mgt_transaction does it */
296 }
297
298 while (isl38xx_in_queue(priv->control_block,
299 ISL38XX_CB_RX_DATA_LQ) != 0) {
300#if VERBOSE > SHOW_ERROR_MESSAGES
301 DEBUG(SHOW_TRACING,
302 "Received frame in Data Low Queue \n");
303#endif
304 islpci_eth_receive(priv);
305 }
306
307 /* check whether the data transmit queues were full */
308 if (priv->data_low_tx_full) {
309 /* check whether the transmit is not full anymore */
310 if (ISL38XX_CB_TX_QSIZE -
311 isl38xx_in_queue(priv->control_block,
312 ISL38XX_CB_TX_DATA_LQ) >=
313 ISL38XX_MIN_QTHRESHOLD) {
314 /* nope, the driver is ready for more network frames */
315 netif_wake_queue(priv->ndev);
316
317 /* reset the full flag */
318 priv->data_low_tx_full = 0;
319 }
320 }
321 }
322
323 if (reg & ISL38XX_INT_IDENT_INIT) {
324 /* Device has been initialized */
325#if VERBOSE > SHOW_ERROR_MESSAGES
326 DEBUG(SHOW_TRACING,
327 "IRQ: Init flag, device initialized \n");
328#endif
329 wake_up(&priv->reset_done);
330 }
331
332 if (reg & ISL38XX_INT_IDENT_SLEEP) {
333 /* Device intends to move to powersave state */
334#if VERBOSE > SHOW_ERROR_MESSAGES
335 DEBUG(SHOW_TRACING, "IRQ: Sleep flag \n");
336#endif
337 isl38xx_handle_sleep_request(priv->control_block,
338 &powerstate,
339 priv->device_base);
340 }
341
342 if (reg & ISL38XX_INT_IDENT_WAKEUP) {
343 /* Device has been woken up to active state */
344#if VERBOSE > SHOW_ERROR_MESSAGES
345 DEBUG(SHOW_TRACING, "IRQ: Wakeup flag \n");
346#endif
347
348 isl38xx_handle_wakeup(priv->control_block,
349 &powerstate, priv->device_base);
350 }
351 } else {
352#if VERBOSE > SHOW_ERROR_MESSAGES
353 DEBUG(SHOW_TRACING, "Assuming someone else called the IRQ\n");
354#endif
355 spin_unlock(&priv->slock);
356 return IRQ_NONE;
357 }
358
359 /* sleep -> ready */
360 if (islpci_get_state(priv) == PRV_STATE_SLEEP
361 && powerstate == ISL38XX_PSM_ACTIVE_STATE)
362 islpci_set_state(priv, PRV_STATE_READY);
363
364 /* !sleep -> sleep */
365 if (islpci_get_state(priv) != PRV_STATE_SLEEP
366 && powerstate == ISL38XX_PSM_POWERSAVE_STATE)
367 islpci_set_state(priv, PRV_STATE_SLEEP);
368
369 /* unlock the interrupt handler */
370 spin_unlock(&priv->slock);
371
372 return IRQ_HANDLED;
373}
374
375/******************************************************************************
376 Network Interface Control & Statistical functions
377******************************************************************************/
378static int
379islpci_open(struct net_device *ndev)
380{
381 u32 rc;
382 islpci_private *priv = netdev_priv(ndev);
383
384 /* reset data structures, upload firmware and reset device */
385 rc = islpci_reset(priv,1);
386 if (rc) {
387 prism54_bring_down(priv);
388 return rc; /* Returns informative message */
389 }
390
391 netif_start_queue(ndev);
Luis R. Rodriguez7b463ce2008-04-01 15:17:36 -0400392
Luis R. Rodriguez85b442e2008-04-22 14:03:32 -0400393 /* Turn off carrier if in STA or Ad-hoc mode. It will be turned on
394 * once the firmware receives a trap of being associated
395 * (GEN_OID_LINKSTATE). In other modes (AP or WDS or monitor) we
396 * should just leave the carrier on as its expected the firmware
397 * won't send us a trigger. */
398 if (priv->iw_mode == IW_MODE_INFRA || priv->iw_mode == IW_MODE_ADHOC)
399 netif_carrier_off(ndev);
400 else
401 netif_carrier_on(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 return 0;
404}
405
406static int
407islpci_close(struct net_device *ndev)
408{
409 islpci_private *priv = netdev_priv(ndev);
410
411 printk(KERN_DEBUG "%s: islpci_close ()\n", ndev->name);
412
413 netif_stop_queue(ndev);
414
415 return prism54_bring_down(priv);
416}
417
418static int
419prism54_bring_down(islpci_private *priv)
420{
421 void __iomem *device_base = priv->device_base;
422 u32 reg;
423 /* we are going to shutdown the device */
424 islpci_set_state(priv, PRV_STATE_PREBOOT);
425
426 /* disable all device interrupts in case they weren't */
Dmitry Torokhov93b2dd12006-10-08 00:38:15 -0400427 isl38xx_disable_interrupts(priv->device_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /* For safety reasons, we may want to ensure that no DMA transfer is
430 * currently in progress by emptying the TX and RX queues. */
431
432 /* wait until interrupts have finished executing on other CPUs */
433 synchronize_irq(priv->pdev->irq);
434
435 reg = readl(device_base + ISL38XX_CTRL_STAT_REG);
436 reg &= ~(ISL38XX_CTRL_STAT_RESET | ISL38XX_CTRL_STAT_RAMBOOT);
437 writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
438 wmb();
439 udelay(ISL38XX_WRITEIO_DELAY);
440
441 reg |= ISL38XX_CTRL_STAT_RESET;
442 writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
443 wmb();
444 udelay(ISL38XX_WRITEIO_DELAY);
445
446 /* clear the Reset bit */
447 reg &= ~ISL38XX_CTRL_STAT_RESET;
448 writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
449 wmb();
450
451 /* wait a while for the device to reset */
Nishanth Aravamudan3173c892005-09-11 02:09:55 -0700452 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 return 0;
455}
456
457static int
458islpci_upload_fw(islpci_private *priv)
459{
460 islpci_state_t old_state;
461 u32 rc;
462
463 old_state = islpci_set_state(priv, PRV_STATE_BOOT);
464
465 printk(KERN_DEBUG "%s: uploading firmware...\n", priv->ndev->name);
466
467 rc = isl_upload_firmware(priv);
468 if (rc) {
469 /* error uploading the firmware */
470 printk(KERN_ERR "%s: could not upload firmware ('%s')\n",
471 priv->ndev->name, priv->firmware);
472
473 islpci_set_state(priv, old_state);
474 return rc;
475 }
476
477 printk(KERN_DEBUG "%s: firmware upload complete\n",
478 priv->ndev->name);
479
480 islpci_set_state(priv, PRV_STATE_POSTBOOT);
481
482 return 0;
483}
484
485static int
486islpci_reset_if(islpci_private *priv)
487{
488 long remaining;
489 int result = -ETIME;
490 int count;
491
492 DEFINE_WAIT(wait);
493 prepare_to_wait(&priv->reset_done, &wait, TASK_UNINTERRUPTIBLE);
Dmitry Torokhov93b2dd12006-10-08 00:38:15 -0400494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 /* now the last step is to reset the interface */
496 isl38xx_interface_reset(priv->device_base, priv->device_host_address);
497 islpci_set_state(priv, PRV_STATE_PREINIT);
498
499 for(count = 0; count < 2 && result; count++) {
500 /* The software reset acknowledge needs about 220 msec here.
501 * Be conservative and wait for up to one second. */
Dmitry Torokhov93b2dd12006-10-08 00:38:15 -0400502
Nishanth Aravamudan3173c892005-09-11 02:09:55 -0700503 remaining = schedule_timeout_uninterruptible(HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 if(remaining > 0) {
506 result = 0;
507 break;
508 }
509
Dmitry Torokhov93b2dd12006-10-08 00:38:15 -0400510 /* If we're here it's because our IRQ hasn't yet gone through.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * Retry a bit more...
512 */
513 printk(KERN_ERR "%s: no 'reset complete' IRQ seen - retrying\n",
514 priv->ndev->name);
515 }
516
517 finish_wait(&priv->reset_done, &wait);
518
519 if (result) {
520 printk(KERN_ERR "%s: interface reset failure\n", priv->ndev->name);
521 return result;
522 }
523
524 islpci_set_state(priv, PRV_STATE_INIT);
525
526 /* Now that the device is 100% up, let's allow
527 * for the other interrupts --
Dmitry Torokhov93b2dd12006-10-08 00:38:15 -0400528 * NOTE: this is not *yet* true since we've only allowed the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 * INIT interrupt on the IRQ line. We can perhaps poll
530 * the IRQ line until we know for sure the reset went through */
531 isl38xx_enable_common_interrupts(priv->device_base);
532
533 down_write(&priv->mib_sem);
534 result = mgt_commit(priv);
535 if (result) {
536 printk(KERN_ERR "%s: interface reset failure\n", priv->ndev->name);
537 up_write(&priv->mib_sem);
538 return result;
539 }
540 up_write(&priv->mib_sem);
541
542 islpci_set_state(priv, PRV_STATE_READY);
543
544 printk(KERN_DEBUG "%s: interface reset complete\n", priv->ndev->name);
545 return 0;
546}
547
548int
549islpci_reset(islpci_private *priv, int reload_firmware)
550{
551 isl38xx_control_block *cb = /* volatile not needed */
552 (isl38xx_control_block *) priv->control_block;
553 unsigned counter;
554 int rc;
555
556 if (reload_firmware)
557 islpci_set_state(priv, PRV_STATE_PREBOOT);
558 else
559 islpci_set_state(priv, PRV_STATE_POSTBOOT);
560
561 printk(KERN_DEBUG "%s: resetting device...\n", priv->ndev->name);
562
563 /* disable all device interrupts in case they weren't */
564 isl38xx_disable_interrupts(priv->device_base);
565
566 /* flush all management queues */
567 priv->index_mgmt_tx = 0;
568 priv->index_mgmt_rx = 0;
569
570 /* clear the indexes in the frame pointer */
571 for (counter = 0; counter < ISL38XX_CB_QCOUNT; counter++) {
572 cb->driver_curr_frag[counter] = cpu_to_le32(0);
573 cb->device_curr_frag[counter] = cpu_to_le32(0);
574 }
575
576 /* reset the mgmt receive queue */
577 for (counter = 0; counter < ISL38XX_CB_MGMT_QSIZE; counter++) {
578 isl38xx_fragment *frag = &cb->rx_data_mgmt[counter];
579 frag->size = cpu_to_le16(MGMT_FRAME_SIZE);
580 frag->flags = 0;
581 frag->address = cpu_to_le32(priv->mgmt_rx[counter].pci_addr);
582 }
583
584 for (counter = 0; counter < ISL38XX_CB_RX_QSIZE; counter++) {
585 cb->rx_data_low[counter].address =
586 cpu_to_le32((u32) priv->pci_map_rx_address[counter]);
587 }
588
589 /* since the receive queues are filled with empty fragments, now we can
590 * set the corresponding indexes in the Control Block */
591 priv->control_block->driver_curr_frag[ISL38XX_CB_RX_DATA_LQ] =
592 cpu_to_le32(ISL38XX_CB_RX_QSIZE);
593 priv->control_block->driver_curr_frag[ISL38XX_CB_RX_MGMTQ] =
594 cpu_to_le32(ISL38XX_CB_MGMT_QSIZE);
595
596 /* reset the remaining real index registers and full flags */
597 priv->free_data_rx = 0;
598 priv->free_data_tx = 0;
599 priv->data_low_tx_full = 0;
600
601 if (reload_firmware) { /* Should we load the firmware ? */
602 /* now that the data structures are cleaned up, upload
603 * firmware and reset interface */
604 rc = islpci_upload_fw(priv);
605 if (rc) {
606 printk(KERN_ERR "%s: islpci_reset: failure\n",
607 priv->ndev->name);
608 return rc;
609 }
610 }
611
612 /* finally reset interface */
613 rc = islpci_reset_if(priv);
614 if (rc)
615 printk(KERN_ERR "prism54: Your card/socket may be faulty, or IRQ line too busy :(\n");
616 return rc;
617}
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/******************************************************************************
620 Network device configuration functions
621******************************************************************************/
622static int
623islpci_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);
Dan Williamseab411f2006-07-17 21:21:47 -0400717 prism54_wpa_bss_ie_init(priv);
Dmitry Torokhov93b2dd12006-10-08 00:38:15 -0400718 if (mgt_init(priv))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 goto out_free;
720
721 return 0;
722 out_free:
723 islpci_free_memory(priv);
724 return -1;
725}
726
727int
728islpci_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;
Jesper Juhlb4558ea2005-10-28 16:53:13 -0400755 kfree(buf->mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 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);
Dan Williamseab411f2006-07-17 21:21:47 -0400776 prism54_wpa_bss_ie_clean(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 mgt_clean(priv);
778
779 return 0;
780}
781
782#if 0
783static void
784islpci_set_multicast_list(struct net_device *dev)
785{
786 /* put device into promisc mode and let network layer handle it */
787}
788#endif
789
Kai Engertff86a542006-12-12 21:09:41 +0100790static void islpci_ethtool_get_drvinfo(struct net_device *dev,
791 struct ethtool_drvinfo *info)
792{
793 strcpy(info->driver, DRV_NAME);
794 strcpy(info->version, DRV_VERSION);
795}
796
Stephen Hemminger6685254f2009-03-20 19:36:34 +0000797static const struct ethtool_ops islpci_ethtool_ops = {
Kai Engertff86a542006-12-12 21:09:41 +0100798 .get_drvinfo = islpci_ethtool_get_drvinfo,
799};
800
Stephen Hemminger6685254f2009-03-20 19:36:34 +0000801static const struct net_device_ops islpci_netdev_ops = {
802 .ndo_open = islpci_open,
803 .ndo_stop = islpci_close,
Stephen Hemminger6685254f2009-03-20 19:36:34 +0000804 .ndo_do_ioctl = prism54_ioctl,
805 .ndo_start_xmit = islpci_eth_transmit,
806 .ndo_tx_timeout = islpci_eth_tx_timeout,
807 .ndo_set_mac_address = prism54_set_mac_address,
808 .ndo_change_mtu = eth_change_mtu,
Stephen Hemminger6685254f2009-03-20 19:36:34 +0000809 .ndo_validate_addr = eth_validate_addr,
810};
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812struct net_device *
813islpci_setup(struct pci_dev *pdev)
814{
815 islpci_private *priv;
816 struct net_device *ndev = alloc_etherdev(sizeof (islpci_private));
817
818 if (!ndev)
819 return ndev;
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 pci_set_drvdata(pdev, ndev);
822#if defined(SET_NETDEV_DEV)
823 SET_NETDEV_DEV(ndev, &pdev->dev);
824#endif
825
826 /* setup the structure members */
827 ndev->base_addr = pci_resource_start(pdev, 0);
828 ndev->irq = pdev->irq;
829
830 /* initialize the function pointers */
Stephen Hemminger6685254f2009-03-20 19:36:34 +0000831 ndev->netdev_ops = &islpci_netdev_ops;
832 ndev->wireless_handlers = &prism54_handler_def;
Kai Engertff86a542006-12-12 21:09:41 +0100833 ndev->ethtool_ops = &islpci_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /* ndev->set_multicast_list = &islpci_set_multicast_list; */
836 ndev->addr_len = ETH_ALEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /* Get a non-zero dummy MAC address for nameif. Jean II */
838 memcpy(ndev->dev_addr, dummy_mac, 6);
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 ndev->watchdog_timeo = ISLPCI_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 /* allocate a private device structure to the network device */
843 priv = netdev_priv(ndev);
844 priv->ndev = ndev;
845 priv->pdev = pdev;
846 priv->monitor_type = ARPHRD_IEEE80211;
847 priv->ndev->type = (priv->iw_mode == IW_MODE_MONITOR) ?
848 priv->monitor_type : ARPHRD_ETHER;
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /* Add pointers to enable iwspy support. */
851 priv->wireless_data.spy_data = &priv->spy_data;
852 ndev->wireless_data = &priv->wireless_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 /* save the start and end address of the PCI memory area */
855 ndev->mem_start = (unsigned long) priv->device_base;
856 ndev->mem_end = ndev->mem_start + ISL38XX_PCI_MEM_SIZE;
857
858#if VERBOSE > SHOW_ERROR_MESSAGES
859 DEBUG(SHOW_TRACING, "PCI Memory remapped to 0x%p\n", priv->device_base);
860#endif
861
862 init_waitqueue_head(&priv->reset_done);
863
864 /* init the queue read locks, process wait counter */
Matthias Kaehlcke1b34fd32007-11-05 09:41:01 +0100865 mutex_init(&priv->mgmt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 priv->mgmt_received = NULL;
867 init_waitqueue_head(&priv->mgmt_wqueue);
Matthias Kaehlckef8139212008-02-15 20:56:59 +0100868 mutex_init(&priv->stats_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 spin_lock_init(&priv->slock);
870
871 /* init state machine with off#1 state */
872 priv->state = PRV_STATE_OFF;
873 priv->state_off = 1;
874
875 /* initialize workqueue's */
David Howellsc4028952006-11-22 14:57:56 +0000876 INIT_WORK(&priv->stats_work, prism54_update_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 priv->stats_timestamp = 0;
878
David Howellsc4028952006-11-22 14:57:56 +0000879 INIT_WORK(&priv->reset_task, islpci_do_reset_and_wake);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 priv->reset_task_pending = 0;
881
882 /* allocate various memory areas */
883 if (islpci_alloc_memory(priv))
884 goto do_free_netdev;
885
886 /* select the firmware file depending on the device id */
887 switch (pdev->device) {
888 case 0x3877:
889 strcpy(priv->firmware, ISL3877_IMAGE_FILE);
890 break;
891
892 case 0x3886:
893 strcpy(priv->firmware, ISL3886_IMAGE_FILE);
894 break;
895
896 default:
897 strcpy(priv->firmware, ISL3890_IMAGE_FILE);
898 break;
899 }
900
901 if (register_netdev(ndev)) {
902 DEBUG(SHOW_ERROR_MESSAGES,
903 "ERROR: register_netdev() failed \n");
904 goto do_islpci_free_memory;
905 }
906
907 return ndev;
908
909 do_islpci_free_memory:
910 islpci_free_memory(priv);
911 do_free_netdev:
912 pci_set_drvdata(pdev, NULL);
913 free_netdev(ndev);
914 priv = NULL;
915 return NULL;
916}
917
918islpci_state_t
919islpci_set_state(islpci_private *priv, islpci_state_t new_state)
920{
921 islpci_state_t old_state;
922
923 /* lock */
924 old_state = priv->state;
925
926 /* this means either a race condition or some serious error in
927 * the driver code */
928 switch (new_state) {
929 case PRV_STATE_OFF:
930 priv->state_off++;
931 default:
932 priv->state = new_state;
933 break;
934
935 case PRV_STATE_PREBOOT:
936 /* there are actually many off-states, enumerated by
937 * state_off */
938 if (old_state == PRV_STATE_OFF)
939 priv->state_off--;
940
941 /* only if hw_unavailable is zero now it means we either
942 * were in off#1 state, or came here from
943 * somewhere else */
944 if (!priv->state_off)
945 priv->state = new_state;
946 break;
947 };
948#if 0
949 printk(KERN_DEBUG "%s: state transition %d -> %d (off#%d)\n",
950 priv->ndev->name, old_state, new_state, priv->state_off);
951#endif
952
953 /* invariants */
954 BUG_ON(priv->state_off < 0);
955 BUG_ON(priv->state_off && (priv->state != PRV_STATE_OFF));
956 BUG_ON(!priv->state_off && (priv->state == PRV_STATE_OFF));
957
958 /* unlock */
959 return old_state;
960}