GNU Linux-libre 4.9.283-gnu1
[releases.git] / drivers / net / wireless / intersil / 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, see <http://www.gnu.org/licenses/>.
17  *
18  */
19
20 #include <linux/hardirq.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23
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>
31
32 #include <asm/io.h>
33
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"
41
42 #define ISL3877_IMAGE_FILE      "/*(DEBLOBBED)*/"
43 #define ISL3886_IMAGE_FILE      "/*(DEBLOBBED)*/"
44 #define ISL3890_IMAGE_FILE      "/*(DEBLOBBED)*/"
45 /*(DEBLOBBED)*/
46
47 static int prism54_bring_down(islpci_private *);
48 static int islpci_alloc_memory(islpci_private *);
49
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 */
57 static const unsigned char      dummy_mac[6] = { 0x00, 0x30, 0xB4, 0x00, 0x00, 0x00 };
58
59 static int
60 isl_upload_firmware(islpci_private *priv)
61 {
62         u32 reg, rc;
63         void __iomem *device_base = priv->device_base;
64
65         /* clear the RAMBoot and the Reset bit */
66         reg = readl(device_base + ISL38XX_CTRL_STAT_REG);
67         reg &= ~ISL38XX_CTRL_STAT_RESET;
68         reg &= ~ISL38XX_CTRL_STAT_RAMBOOT;
69         writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
70         wmb();
71         udelay(ISL38XX_WRITEIO_DELAY);
72
73         /* set the Reset bit without reading the register ! */
74         reg |= ISL38XX_CTRL_STAT_RESET;
75         writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
76         wmb();
77         udelay(ISL38XX_WRITEIO_DELAY);
78
79         /* clear the Reset bit */
80         reg &= ~ISL38XX_CTRL_STAT_RESET;
81         writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
82         wmb();
83
84         /* wait a while for the device to reboot */
85         mdelay(50);
86
87         {
88                 const struct firmware *fw_entry = NULL;
89                 long fw_len;
90                 const u32 *fw_ptr;
91
92                 rc = reject_firmware(&fw_entry, priv->firmware, PRISM_FW_PDEV);
93                 if (rc) {
94                         printk(KERN_ERR
95                                "%s: reject_firmware() failed for '%s'\n",
96                                "prism54", priv->firmware);
97                         return rc;
98                 }
99                 /* prepare the Direct Memory Base register */
100                 reg = ISL38XX_DEV_FIRMWARE_ADDRES;
101
102                 fw_ptr = (u32 *) fw_entry->data;
103                 fw_len = fw_entry->size;
104
105                 if (fw_len % 4) {
106                         printk(KERN_ERR
107                                "%s: firmware '%s' size is not multiple of 32bit, aborting!\n",
108                                "prism54", priv->firmware);
109                         release_firmware(fw_entry);
110                         return -EILSEQ; /* Illegal byte sequence  */;
111                 }
112
113                 while (fw_len > 0) {
114                         long _fw_len =
115                             (fw_len >
116                              ISL38XX_MEMORY_WINDOW_SIZE) ?
117                             ISL38XX_MEMORY_WINDOW_SIZE : fw_len;
118                         u32 __iomem *dev_fw_ptr = device_base + ISL38XX_DIRECT_MEM_WIN;
119
120                         /* set the card's base address for writing the data */
121                         isl38xx_w32_flush(device_base, reg,
122                                           ISL38XX_DIR_MEM_BASE_REG);
123                         wmb();  /* be paranoid */
124
125                         /* increment the write address for next iteration */
126                         reg += _fw_len;
127                         fw_len -= _fw_len;
128
129                         /* write the data to the Direct Memory Window 32bit-wise */
130                         /* memcpy_toio() doesn't guarantee 32bit writes :-| */
131                         while (_fw_len > 0) {
132                                 /* use non-swapping writel() */
133                                 __raw_writel(*fw_ptr, dev_fw_ptr);
134                                 fw_ptr++, dev_fw_ptr++;
135                                 _fw_len -= 4;
136                         }
137
138                         /* flush PCI posting */
139                         (void) readl(device_base + ISL38XX_PCI_POSTING_FLUSH);
140                         wmb();  /* be paranoid again */
141
142                         BUG_ON(_fw_len != 0);
143                 }
144
145                 BUG_ON(fw_len != 0);
146
147                 /* Firmware version is at offset 40 (also for "newmac") */
148                 printk(KERN_DEBUG "%s: firmware version: %.8s\n",
149                        priv->ndev->name, fw_entry->data + 40);
150
151                 release_firmware(fw_entry);
152         }
153
154         /* now reset the device
155          * clear the Reset & ClkRun bit, set the RAMBoot bit */
156         reg = readl(device_base + ISL38XX_CTRL_STAT_REG);
157         reg &= ~ISL38XX_CTRL_STAT_CLKRUN;
158         reg &= ~ISL38XX_CTRL_STAT_RESET;
159         reg |= ISL38XX_CTRL_STAT_RAMBOOT;
160         isl38xx_w32_flush(device_base, reg, ISL38XX_CTRL_STAT_REG);
161         wmb();
162         udelay(ISL38XX_WRITEIO_DELAY);
163
164         /* set the reset bit latches the host override and RAMBoot bits
165          * into the device for operation when the reset bit is reset */
166         reg |= ISL38XX_CTRL_STAT_RESET;
167         writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
168         /* don't do flush PCI posting here! */
169         wmb();
170         udelay(ISL38XX_WRITEIO_DELAY);
171
172         /* clear the reset bit should start the whole circus */
173         reg &= ~ISL38XX_CTRL_STAT_RESET;
174         writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
175         /* don't do flush PCI posting here! */
176         wmb();
177         udelay(ISL38XX_WRITEIO_DELAY);
178
179         return 0;
180 }
181
182 /******************************************************************************
183     Device Interrupt Handler
184 ******************************************************************************/
185
186 irqreturn_t
187 islpci_interrupt(int irq, void *config)
188 {
189         u32 reg;
190         islpci_private *priv = config;
191         struct net_device *ndev = priv->ndev;
192         void __iomem *device = priv->device_base;
193         int powerstate = ISL38XX_PSM_POWERSAVE_STATE;
194
195         /* lock the interrupt handler */
196         spin_lock(&priv->slock);
197
198         /* received an interrupt request on a shared IRQ line
199          * first check whether the device is in sleep mode */
200         reg = readl(device + ISL38XX_CTRL_STAT_REG);
201         if (reg & ISL38XX_CTRL_STAT_SLEEPMODE)
202                 /* device is in sleep mode, IRQ was generated by someone else */
203         {
204 #if VERBOSE > SHOW_ERROR_MESSAGES
205                 DEBUG(SHOW_TRACING, "Assuming someone else called the IRQ\n");
206 #endif
207                 spin_unlock(&priv->slock);
208                 return IRQ_NONE;
209         }
210
211
212         /* check whether there is any source of interrupt on the device */
213         reg = readl(device + ISL38XX_INT_IDENT_REG);
214
215         /* also check the contents of the Interrupt Enable Register, because this
216          * will filter out interrupt sources from other devices on the same irq ! */
217         reg &= readl(device + ISL38XX_INT_EN_REG);
218         reg &= ISL38XX_INT_SOURCES;
219
220         if (reg != 0) {
221                 if (islpci_get_state(priv) != PRV_STATE_SLEEP)
222                         powerstate = ISL38XX_PSM_ACTIVE_STATE;
223
224                 /* reset the request bits in the Identification register */
225                 isl38xx_w32_flush(device, reg, ISL38XX_INT_ACK_REG);
226
227 #if VERBOSE > SHOW_ERROR_MESSAGES
228                 DEBUG(SHOW_FUNCTION_CALLS,
229                       "IRQ: Identification register 0x%p 0x%x\n", device, reg);
230 #endif
231
232                 /* check for each bit in the register separately */
233                 if (reg & ISL38XX_INT_IDENT_UPDATE) {
234 #if VERBOSE > SHOW_ERROR_MESSAGES
235                         /* Queue has been updated */
236                         DEBUG(SHOW_TRACING, "IRQ: Update flag\n");
237
238                         DEBUG(SHOW_QUEUE_INDEXES,
239                               "CB drv Qs: [%i][%i][%i][%i][%i][%i]\n",
240                               le32_to_cpu(priv->control_block->
241                                           driver_curr_frag[0]),
242                               le32_to_cpu(priv->control_block->
243                                           driver_curr_frag[1]),
244                               le32_to_cpu(priv->control_block->
245                                           driver_curr_frag[2]),
246                               le32_to_cpu(priv->control_block->
247                                           driver_curr_frag[3]),
248                               le32_to_cpu(priv->control_block->
249                                           driver_curr_frag[4]),
250                               le32_to_cpu(priv->control_block->
251                                           driver_curr_frag[5])
252                             );
253
254                         DEBUG(SHOW_QUEUE_INDEXES,
255                               "CB dev Qs: [%i][%i][%i][%i][%i][%i]\n",
256                               le32_to_cpu(priv->control_block->
257                                           device_curr_frag[0]),
258                               le32_to_cpu(priv->control_block->
259                                           device_curr_frag[1]),
260                               le32_to_cpu(priv->control_block->
261                                           device_curr_frag[2]),
262                               le32_to_cpu(priv->control_block->
263                                           device_curr_frag[3]),
264                               le32_to_cpu(priv->control_block->
265                                           device_curr_frag[4]),
266                               le32_to_cpu(priv->control_block->
267                                           device_curr_frag[5])
268                             );
269 #endif
270
271                         /* cleanup the data low transmit queue */
272                         islpci_eth_cleanup_transmit(priv, priv->control_block);
273
274                         /* device is in active state, update the
275                          * powerstate flag if necessary */
276                         powerstate = ISL38XX_PSM_ACTIVE_STATE;
277
278                         /* check all three queues in priority order
279                          * call the PIMFOR receive function until the
280                          * queue is empty */
281                         if (isl38xx_in_queue(priv->control_block,
282                                                 ISL38XX_CB_RX_MGMTQ) != 0) {
283 #if VERBOSE > SHOW_ERROR_MESSAGES
284                                 DEBUG(SHOW_TRACING,
285                                       "Received frame in Management Queue\n");
286 #endif
287                                 islpci_mgt_receive(ndev);
288
289                                 islpci_mgt_cleanup_transmit(ndev);
290
291                                 /* Refill slots in receive queue */
292                                 islpci_mgmt_rx_fill(ndev);
293
294                                 /* no need to trigger the device, next
295                                    islpci_mgt_transaction does it */
296                         }
297
298                         while (isl38xx_in_queue(priv->control_block,
299                                                 ISL38XX_CB_RX_DATA_LQ) != 0) {
300 #if VERBOSE > SHOW_ERROR_MESSAGES
301                                 DEBUG(SHOW_TRACING,
302                                       "Received frame in Data Low Queue\n");
303 #endif
304                                 islpci_eth_receive(priv);
305                         }
306
307                         /* check whether the data transmit queues were full */
308                         if (priv->data_low_tx_full) {
309                                 /* check whether the transmit is not full anymore */
310                                 if (ISL38XX_CB_TX_QSIZE -
311                                     isl38xx_in_queue(priv->control_block,
312                                                      ISL38XX_CB_TX_DATA_LQ) >=
313                                     ISL38XX_MIN_QTHRESHOLD) {
314                                         /* nope, the driver is ready for more network frames */
315                                         netif_wake_queue(priv->ndev);
316
317                                         /* reset the full flag */
318                                         priv->data_low_tx_full = 0;
319                                 }
320                         }
321                 }
322
323                 if (reg & ISL38XX_INT_IDENT_INIT) {
324                         /* Device has been initialized */
325 #if VERBOSE > SHOW_ERROR_MESSAGES
326                         DEBUG(SHOW_TRACING,
327                               "IRQ: Init flag, device initialized\n");
328 #endif
329                         wake_up(&priv->reset_done);
330                 }
331
332                 if (reg & ISL38XX_INT_IDENT_SLEEP) {
333                         /* Device intends to move to powersave state */
334 #if VERBOSE > SHOW_ERROR_MESSAGES
335                         DEBUG(SHOW_TRACING, "IRQ: Sleep flag\n");
336 #endif
337                         isl38xx_handle_sleep_request(priv->control_block,
338                                                      &powerstate,
339                                                      priv->device_base);
340                 }
341
342                 if (reg & ISL38XX_INT_IDENT_WAKEUP) {
343                         /* Device has been woken up to active state */
344 #if VERBOSE > SHOW_ERROR_MESSAGES
345                         DEBUG(SHOW_TRACING, "IRQ: Wakeup flag\n");
346 #endif
347
348                         isl38xx_handle_wakeup(priv->control_block,
349                                               &powerstate, priv->device_base);
350                 }
351         } else {
352 #if VERBOSE > SHOW_ERROR_MESSAGES
353                 DEBUG(SHOW_TRACING, "Assuming someone else called the IRQ\n");
354 #endif
355                 spin_unlock(&priv->slock);
356                 return IRQ_NONE;
357         }
358
359         /* sleep -> ready */
360         if (islpci_get_state(priv) == PRV_STATE_SLEEP
361             && powerstate == ISL38XX_PSM_ACTIVE_STATE)
362                 islpci_set_state(priv, PRV_STATE_READY);
363
364         /* !sleep -> sleep */
365         if (islpci_get_state(priv) != PRV_STATE_SLEEP
366             && powerstate == ISL38XX_PSM_POWERSAVE_STATE)
367                 islpci_set_state(priv, PRV_STATE_SLEEP);
368
369         /* unlock the interrupt handler */
370         spin_unlock(&priv->slock);
371
372         return IRQ_HANDLED;
373 }
374
375 /******************************************************************************
376     Network Interface Control & Statistical functions
377 ******************************************************************************/
378 static int
379 islpci_open(struct net_device *ndev)
380 {
381         u32 rc;
382         islpci_private *priv = netdev_priv(ndev);
383
384         /* reset data structures, upload firmware and reset device */
385         rc = islpci_reset(priv,1);
386         if (rc) {
387                 prism54_bring_down(priv);
388                 return rc; /* Returns informative message */
389         }
390
391         netif_start_queue(ndev);
392
393         /* 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);
402
403         return 0;
404 }
405
406 static int
407 islpci_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
418 static int
419 prism54_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 */
427         isl38xx_disable_interrupts(priv->device_base);
428
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 */
452         schedule_timeout_uninterruptible(msecs_to_jiffies(50));
453
454         return 0;
455 }
456
457 static int
458 islpci_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
485 static int
486 islpci_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);
494
495         /* 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. */
502
503                 remaining = schedule_timeout_uninterruptible(HZ);
504
505                 if(remaining > 0) {
506                         result = 0;
507                         break;
508                 }
509
510                 /* If we're here it's because our IRQ hasn't yet gone through.
511                  * 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 --
528          * NOTE: this is not *yet* true since we've only allowed the
529          * 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
548 int
549 islpci_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
619 /******************************************************************************
620     Network device configuration functions
621 ******************************************************************************/
622 static int
623 islpci_alloc_memory(islpci_private *priv)
624 {
625         int counter;
626
627 #if VERBOSE > SHOW_ERROR_MESSAGES
628         printk(KERN_DEBUG "islpci_alloc_memory\n");
629 #endif
630
631         /* remap the PCI device base address to accessible */
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 (pci_dma_mapping_error(priv->pdev,
709                                           priv->pci_map_rx_address[counter])) {
710                         priv->pci_map_rx_address[counter] = 0;
711                         /* error mapping the buffer to device
712                            accessible memory address */
713                         printk(KERN_ERR "failed to map skb DMA'able\n");
714                         goto out_free;
715                 }
716         }
717
718         prism54_acl_init(&priv->acl);
719         prism54_wpa_bss_ie_init(priv);
720         if (mgt_init(priv))
721                 goto out_free;
722
723         return 0;
724  out_free:
725         islpci_free_memory(priv);
726         return -1;
727 }
728
729 int
730 islpci_free_memory(islpci_private *priv)
731 {
732         int counter;
733
734         if (priv->device_base)
735                 iounmap(priv->device_base);
736         priv->device_base = NULL;
737
738         /* free consistent DMA area... */
739         if (priv->driver_mem_address)
740                 pci_free_consistent(priv->pdev, HOST_MEM_BLOCK,
741                                     priv->driver_mem_address,
742                                     priv->device_host_address);
743
744         /* clear some dangling pointers */
745         priv->driver_mem_address = NULL;
746         priv->device_host_address = 0;
747         priv->device_psm_buffer = 0;
748         priv->control_block = NULL;
749
750         /* clean up mgmt rx buffers */
751         for (counter = 0; counter < ISL38XX_CB_MGMT_QSIZE; counter++) {
752                 struct islpci_membuf *buf = &priv->mgmt_rx[counter];
753                 if (buf->pci_addr)
754                         pci_unmap_single(priv->pdev, buf->pci_addr,
755                                          buf->size, PCI_DMA_FROMDEVICE);
756                 buf->pci_addr = 0;
757                 kfree(buf->mem);
758                 buf->size = 0;
759                 buf->mem = NULL;
760         }
761
762         /* clean up data rx buffers */
763         for (counter = 0; counter < ISL38XX_CB_RX_QSIZE; counter++) {
764                 if (priv->pci_map_rx_address[counter])
765                         pci_unmap_single(priv->pdev,
766                                          priv->pci_map_rx_address[counter],
767                                          MAX_FRAGMENT_SIZE_RX + 2,
768                                          PCI_DMA_FROMDEVICE);
769                 priv->pci_map_rx_address[counter] = 0;
770
771                 if (priv->data_low_rx[counter])
772                         dev_kfree_skb(priv->data_low_rx[counter]);
773                 priv->data_low_rx[counter] = NULL;
774         }
775
776         /* Free the access control list and the WPA list */
777         prism54_acl_clean(&priv->acl);
778         prism54_wpa_bss_ie_clean(priv);
779         mgt_clean(priv);
780
781         return 0;
782 }
783
784 #if 0
785 static void
786 islpci_set_multicast_list(struct net_device *dev)
787 {
788         /* put device into promisc mode and let network layer handle it */
789 }
790 #endif
791
792 static void islpci_ethtool_get_drvinfo(struct net_device *dev,
793                                        struct ethtool_drvinfo *info)
794 {
795         strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
796         strlcpy(info->version, DRV_VERSION, sizeof(info->version));
797 }
798
799 static const struct ethtool_ops islpci_ethtool_ops = {
800         .get_drvinfo = islpci_ethtool_get_drvinfo,
801 };
802
803 static const struct net_device_ops islpci_netdev_ops = {
804         .ndo_open               = islpci_open,
805         .ndo_stop               = islpci_close,
806         .ndo_start_xmit         = islpci_eth_transmit,
807         .ndo_tx_timeout         = islpci_eth_tx_timeout,
808         .ndo_set_mac_address    = prism54_set_mac_address,
809         .ndo_change_mtu         = eth_change_mtu,
810         .ndo_validate_addr      = eth_validate_addr,
811 };
812
813 static struct device_type wlan_type = {
814         .name   = "wlan",
815 };
816
817 struct net_device *
818 islpci_setup(struct pci_dev *pdev)
819 {
820         islpci_private *priv;
821         struct net_device *ndev = alloc_etherdev(sizeof (islpci_private));
822
823         if (!ndev)
824                 return ndev;
825
826         pci_set_drvdata(pdev, ndev);
827         SET_NETDEV_DEV(ndev, &pdev->dev);
828         SET_NETDEV_DEVTYPE(ndev, &wlan_type);
829
830         /* setup the structure members */
831         ndev->base_addr = pci_resource_start(pdev, 0);
832         ndev->irq = pdev->irq;
833
834         /* initialize the function pointers */
835         ndev->netdev_ops = &islpci_netdev_ops;
836         ndev->wireless_handlers = &prism54_handler_def;
837         ndev->ethtool_ops = &islpci_ethtool_ops;
838
839         /* ndev->set_multicast_list = &islpci_set_multicast_list; */
840         ndev->addr_len = ETH_ALEN;
841         /* Get a non-zero dummy MAC address for nameif. Jean II */
842         memcpy(ndev->dev_addr, dummy_mac, ETH_ALEN);
843
844         ndev->watchdog_timeo = ISLPCI_TX_TIMEOUT;
845
846         /* allocate a private device structure to the network device  */
847         priv = netdev_priv(ndev);
848         priv->ndev = ndev;
849         priv->pdev = pdev;
850         priv->monitor_type = ARPHRD_IEEE80211;
851         priv->ndev->type = (priv->iw_mode == IW_MODE_MONITOR) ?
852                 priv->monitor_type : ARPHRD_ETHER;
853
854         /* Add pointers to enable iwspy support. */
855         priv->wireless_data.spy_data = &priv->spy_data;
856         ndev->wireless_data = &priv->wireless_data;
857
858         /* save the start and end address of the PCI memory area */
859         ndev->mem_start = (unsigned long) priv->device_base;
860         ndev->mem_end = ndev->mem_start + ISL38XX_PCI_MEM_SIZE;
861
862 #if VERBOSE > SHOW_ERROR_MESSAGES
863         DEBUG(SHOW_TRACING, "PCI Memory remapped to 0x%p\n", priv->device_base);
864 #endif
865
866         init_waitqueue_head(&priv->reset_done);
867
868         /* init the queue read locks, process wait counter */
869         mutex_init(&priv->mgmt_lock);
870         priv->mgmt_received = NULL;
871         init_waitqueue_head(&priv->mgmt_wqueue);
872         mutex_init(&priv->stats_lock);
873         spin_lock_init(&priv->slock);
874
875         /* init state machine with off#1 state */
876         priv->state = PRV_STATE_OFF;
877         priv->state_off = 1;
878
879         /* initialize workqueue's */
880         INIT_WORK(&priv->stats_work, prism54_update_stats);
881         priv->stats_timestamp = 0;
882
883         INIT_WORK(&priv->reset_task, islpci_do_reset_and_wake);
884         priv->reset_task_pending = 0;
885
886         /* allocate various memory areas */
887         if (islpci_alloc_memory(priv))
888                 goto do_free_netdev;
889
890         /* select the firmware file depending on the device id */
891         switch (pdev->device) {
892         case 0x3877:
893                 strcpy(priv->firmware, ISL3877_IMAGE_FILE);
894                 break;
895
896         case 0x3886:
897                 strcpy(priv->firmware, ISL3886_IMAGE_FILE);
898                 break;
899
900         default:
901                 strcpy(priv->firmware, ISL3890_IMAGE_FILE);
902                 break;
903         }
904
905         if (register_netdev(ndev)) {
906                 DEBUG(SHOW_ERROR_MESSAGES,
907                       "ERROR: register_netdev() failed\n");
908                 goto do_islpci_free_memory;
909         }
910
911         return ndev;
912
913       do_islpci_free_memory:
914         islpci_free_memory(priv);
915       do_free_netdev:
916         free_netdev(ndev);
917         priv = NULL;
918         return NULL;
919 }
920
921 islpci_state_t
922 islpci_set_state(islpci_private *priv, islpci_state_t new_state)
923 {
924         islpci_state_t old_state;
925
926         /* lock */
927         old_state = priv->state;
928
929         /* this means either a race condition or some serious error in
930          * the driver code */
931         switch (new_state) {
932         case PRV_STATE_OFF:
933                 priv->state_off++;
934         default:
935                 priv->state = new_state;
936                 break;
937
938         case PRV_STATE_PREBOOT:
939                 /* there are actually many off-states, enumerated by
940                  * state_off */
941                 if (old_state == PRV_STATE_OFF)
942                         priv->state_off--;
943
944                 /* only if hw_unavailable is zero now it means we either
945                  * were in off#1 state, or came here from
946                  * somewhere else */
947                 if (!priv->state_off)
948                         priv->state = new_state;
949                 break;
950         }
951 #if 0
952         printk(KERN_DEBUG "%s: state transition %d -> %d (off#%d)\n",
953                priv->ndev->name, old_state, new_state, priv->state_off);
954 #endif
955
956         /* invariants */
957         BUG_ON(priv->state_off < 0);
958         BUG_ON(priv->state_off && (priv->state != PRV_STATE_OFF));
959         BUG_ON(!priv->state_off && (priv->state == PRV_STATE_OFF));
960
961         /* unlock */
962         return old_state;
963 }