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