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