xref: /openbmc/linux/drivers/net/hippi/rrunner.c (revision e23feb16)
1 /*
2  * rrunner.c: Linux driver for the Essential RoadRunner HIPPI board.
3  *
4  * Copyright (C) 1998-2002 by Jes Sorensen, <jes@wildopensource.com>.
5  *
6  * Thanks to Essential Communication for providing us with hardware
7  * and very comprehensive documentation without which I would not have
8  * been able to write this driver. A special thank you to John Gibbon
9  * for sorting out the legal issues, with the NDA, allowing the code to
10  * be released under the GPL.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * Thanks to Jayaram Bhat from ODS/Essential for fixing some of the
18  * stupid bugs in my code.
19  *
20  * Softnet support and various other patches from Val Henson of
21  * ODS/Essential.
22  *
23  * PCI DMA mapping code partly based on work by Francois Romieu.
24  */
25 
26 
27 #define DEBUG 1
28 #define RX_DMA_SKBUFF 1
29 #define PKT_COPY_THRESHOLD 512
30 
31 #include <linux/module.h>
32 #include <linux/types.h>
33 #include <linux/errno.h>
34 #include <linux/ioport.h>
35 #include <linux/pci.h>
36 #include <linux/kernel.h>
37 #include <linux/netdevice.h>
38 #include <linux/hippidevice.h>
39 #include <linux/skbuff.h>
40 #include <linux/init.h>
41 #include <linux/delay.h>
42 #include <linux/mm.h>
43 #include <linux/slab.h>
44 #include <net/sock.h>
45 
46 #include <asm/cache.h>
47 #include <asm/byteorder.h>
48 #include <asm/io.h>
49 #include <asm/irq.h>
50 #include <asm/uaccess.h>
51 
52 #define rr_if_busy(dev)     netif_queue_stopped(dev)
53 #define rr_if_running(dev)  netif_running(dev)
54 
55 #include "rrunner.h"
56 
57 #define RUN_AT(x) (jiffies + (x))
58 
59 
60 MODULE_AUTHOR("Jes Sorensen <jes@wildopensource.com>");
61 MODULE_DESCRIPTION("Essential RoadRunner HIPPI driver");
62 MODULE_LICENSE("GPL");
63 
64 static char version[] = "rrunner.c: v0.50 11/11/2002  Jes Sorensen (jes@wildopensource.com)\n";
65 
66 
67 static const struct net_device_ops rr_netdev_ops = {
68 	.ndo_open 		= rr_open,
69 	.ndo_stop		= rr_close,
70 	.ndo_do_ioctl		= rr_ioctl,
71 	.ndo_start_xmit		= rr_start_xmit,
72 	.ndo_change_mtu		= hippi_change_mtu,
73 	.ndo_set_mac_address	= hippi_mac_addr,
74 };
75 
76 /*
77  * Implementation notes:
78  *
79  * The DMA engine only allows for DMA within physical 64KB chunks of
80  * memory. The current approach of the driver (and stack) is to use
81  * linear blocks of memory for the skbuffs. However, as the data block
82  * is always the first part of the skb and skbs are 2^n aligned so we
83  * are guarantted to get the whole block within one 64KB align 64KB
84  * chunk.
85  *
86  * On the long term, relying on being able to allocate 64KB linear
87  * chunks of memory is not feasible and the skb handling code and the
88  * stack will need to know about I/O vectors or something similar.
89  */
90 
91 static int rr_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
92 {
93 	struct net_device *dev;
94 	static int version_disp;
95 	u8 pci_latency;
96 	struct rr_private *rrpriv;
97 	void *tmpptr;
98 	dma_addr_t ring_dma;
99 	int ret = -ENOMEM;
100 
101 	dev = alloc_hippi_dev(sizeof(struct rr_private));
102 	if (!dev)
103 		goto out3;
104 
105 	ret = pci_enable_device(pdev);
106 	if (ret) {
107 		ret = -ENODEV;
108 		goto out2;
109 	}
110 
111 	rrpriv = netdev_priv(dev);
112 
113 	SET_NETDEV_DEV(dev, &pdev->dev);
114 
115 	ret = pci_request_regions(pdev, "rrunner");
116 	if (ret < 0)
117 		goto out;
118 
119 	pci_set_drvdata(pdev, dev);
120 
121 	rrpriv->pci_dev = pdev;
122 
123 	spin_lock_init(&rrpriv->lock);
124 
125 	dev->netdev_ops = &rr_netdev_ops;
126 
127 	/* display version info if adapter is found */
128 	if (!version_disp) {
129 		/* set display flag to TRUE so that */
130 		/* we only display this string ONCE */
131 		version_disp = 1;
132 		printk(version);
133 	}
134 
135 	pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
136 	if (pci_latency <= 0x58){
137 		pci_latency = 0x58;
138 		pci_write_config_byte(pdev, PCI_LATENCY_TIMER, pci_latency);
139 	}
140 
141 	pci_set_master(pdev);
142 
143 	printk(KERN_INFO "%s: Essential RoadRunner serial HIPPI "
144 	       "at 0x%llx, irq %i, PCI latency %i\n", dev->name,
145 	       (unsigned long long)pci_resource_start(pdev, 0),
146 	       pdev->irq, pci_latency);
147 
148 	/*
149 	 * Remap the MMIO regs into kernel space.
150 	 */
151 	rrpriv->regs = pci_iomap(pdev, 0, 0x1000);
152 	if (!rrpriv->regs) {
153 		printk(KERN_ERR "%s:  Unable to map I/O register, "
154 			"RoadRunner will be disabled.\n", dev->name);
155 		ret = -EIO;
156 		goto out;
157 	}
158 
159 	tmpptr = pci_alloc_consistent(pdev, TX_TOTAL_SIZE, &ring_dma);
160 	rrpriv->tx_ring = tmpptr;
161 	rrpriv->tx_ring_dma = ring_dma;
162 
163 	if (!tmpptr) {
164 		ret = -ENOMEM;
165 		goto out;
166 	}
167 
168 	tmpptr = pci_alloc_consistent(pdev, RX_TOTAL_SIZE, &ring_dma);
169 	rrpriv->rx_ring = tmpptr;
170 	rrpriv->rx_ring_dma = ring_dma;
171 
172 	if (!tmpptr) {
173 		ret = -ENOMEM;
174 		goto out;
175 	}
176 
177 	tmpptr = pci_alloc_consistent(pdev, EVT_RING_SIZE, &ring_dma);
178 	rrpriv->evt_ring = tmpptr;
179 	rrpriv->evt_ring_dma = ring_dma;
180 
181 	if (!tmpptr) {
182 		ret = -ENOMEM;
183 		goto out;
184 	}
185 
186 	/*
187 	 * Don't access any register before this point!
188 	 */
189 #ifdef __BIG_ENDIAN
190 	writel(readl(&rrpriv->regs->HostCtrl) | NO_SWAP,
191 		&rrpriv->regs->HostCtrl);
192 #endif
193 	/*
194 	 * Need to add a case for little-endian 64-bit hosts here.
195 	 */
196 
197 	rr_init(dev);
198 
199 	ret = register_netdev(dev);
200 	if (ret)
201 		goto out;
202 	return 0;
203 
204  out:
205 	if (rrpriv->evt_ring)
206 		pci_free_consistent(pdev, EVT_RING_SIZE, rrpriv->evt_ring,
207 				    rrpriv->evt_ring_dma);
208 	if (rrpriv->rx_ring)
209 		pci_free_consistent(pdev, RX_TOTAL_SIZE, rrpriv->rx_ring,
210 				    rrpriv->rx_ring_dma);
211 	if (rrpriv->tx_ring)
212 		pci_free_consistent(pdev, TX_TOTAL_SIZE, rrpriv->tx_ring,
213 				    rrpriv->tx_ring_dma);
214 	if (rrpriv->regs)
215 		pci_iounmap(pdev, rrpriv->regs);
216 	if (pdev) {
217 		pci_release_regions(pdev);
218 		pci_set_drvdata(pdev, NULL);
219 	}
220  out2:
221 	free_netdev(dev);
222  out3:
223 	return ret;
224 }
225 
226 static void rr_remove_one(struct pci_dev *pdev)
227 {
228 	struct net_device *dev = pci_get_drvdata(pdev);
229 	struct rr_private *rr = netdev_priv(dev);
230 
231 	if (!(readl(&rr->regs->HostCtrl) & NIC_HALTED)) {
232 		printk(KERN_ERR "%s: trying to unload running NIC\n",
233 		       dev->name);
234 		writel(HALT_NIC, &rr->regs->HostCtrl);
235 	}
236 
237 	unregister_netdev(dev);
238 	pci_free_consistent(pdev, EVT_RING_SIZE, rr->evt_ring,
239 			    rr->evt_ring_dma);
240 	pci_free_consistent(pdev, RX_TOTAL_SIZE, rr->rx_ring,
241 			    rr->rx_ring_dma);
242 	pci_free_consistent(pdev, TX_TOTAL_SIZE, rr->tx_ring,
243 			    rr->tx_ring_dma);
244 	pci_iounmap(pdev, rr->regs);
245 	pci_release_regions(pdev);
246 	pci_disable_device(pdev);
247 	pci_set_drvdata(pdev, NULL);
248 	free_netdev(dev);
249 }
250 
251 
252 /*
253  * Commands are considered to be slow, thus there is no reason to
254  * inline this.
255  */
256 static void rr_issue_cmd(struct rr_private *rrpriv, struct cmd *cmd)
257 {
258 	struct rr_regs __iomem *regs;
259 	u32 idx;
260 
261 	regs = rrpriv->regs;
262 	/*
263 	 * This is temporary - it will go away in the final version.
264 	 * We probably also want to make this function inline.
265 	 */
266 	if (readl(&regs->HostCtrl) & NIC_HALTED){
267 		printk("issuing command for halted NIC, code 0x%x, "
268 		       "HostCtrl %08x\n", cmd->code, readl(&regs->HostCtrl));
269 		if (readl(&regs->Mode) & FATAL_ERR)
270 			printk("error codes Fail1 %02x, Fail2 %02x\n",
271 			       readl(&regs->Fail1), readl(&regs->Fail2));
272 	}
273 
274 	idx = rrpriv->info->cmd_ctrl.pi;
275 
276 	writel(*(u32*)(cmd), &regs->CmdRing[idx]);
277 	wmb();
278 
279 	idx = (idx - 1) % CMD_RING_ENTRIES;
280 	rrpriv->info->cmd_ctrl.pi = idx;
281 	wmb();
282 
283 	if (readl(&regs->Mode) & FATAL_ERR)
284 		printk("error code %02x\n", readl(&regs->Fail1));
285 }
286 
287 
288 /*
289  * Reset the board in a sensible manner. The NIC is already halted
290  * when we get here and a spin-lock is held.
291  */
292 static int rr_reset(struct net_device *dev)
293 {
294 	struct rr_private *rrpriv;
295 	struct rr_regs __iomem *regs;
296 	u32 start_pc;
297 	int i;
298 
299 	rrpriv = netdev_priv(dev);
300 	regs = rrpriv->regs;
301 
302 	rr_load_firmware(dev);
303 
304 	writel(0x01000000, &regs->TX_state);
305 	writel(0xff800000, &regs->RX_state);
306 	writel(0, &regs->AssistState);
307 	writel(CLEAR_INTA, &regs->LocalCtrl);
308 	writel(0x01, &regs->BrkPt);
309 	writel(0, &regs->Timer);
310 	writel(0, &regs->TimerRef);
311 	writel(RESET_DMA, &regs->DmaReadState);
312 	writel(RESET_DMA, &regs->DmaWriteState);
313 	writel(0, &regs->DmaWriteHostHi);
314 	writel(0, &regs->DmaWriteHostLo);
315 	writel(0, &regs->DmaReadHostHi);
316 	writel(0, &regs->DmaReadHostLo);
317 	writel(0, &regs->DmaReadLen);
318 	writel(0, &regs->DmaWriteLen);
319 	writel(0, &regs->DmaWriteLcl);
320 	writel(0, &regs->DmaWriteIPchecksum);
321 	writel(0, &regs->DmaReadLcl);
322 	writel(0, &regs->DmaReadIPchecksum);
323 	writel(0, &regs->PciState);
324 #if (BITS_PER_LONG == 64) && defined __LITTLE_ENDIAN
325 	writel(SWAP_DATA | PTR64BIT | PTR_WD_SWAP, &regs->Mode);
326 #elif (BITS_PER_LONG == 64)
327 	writel(SWAP_DATA | PTR64BIT | PTR_WD_NOSWAP, &regs->Mode);
328 #else
329 	writel(SWAP_DATA | PTR32BIT | PTR_WD_NOSWAP, &regs->Mode);
330 #endif
331 
332 #if 0
333 	/*
334 	 * Don't worry, this is just black magic.
335 	 */
336 	writel(0xdf000, &regs->RxBase);
337 	writel(0xdf000, &regs->RxPrd);
338 	writel(0xdf000, &regs->RxCon);
339 	writel(0xce000, &regs->TxBase);
340 	writel(0xce000, &regs->TxPrd);
341 	writel(0xce000, &regs->TxCon);
342 	writel(0, &regs->RxIndPro);
343 	writel(0, &regs->RxIndCon);
344 	writel(0, &regs->RxIndRef);
345 	writel(0, &regs->TxIndPro);
346 	writel(0, &regs->TxIndCon);
347 	writel(0, &regs->TxIndRef);
348 	writel(0xcc000, &regs->pad10[0]);
349 	writel(0, &regs->DrCmndPro);
350 	writel(0, &regs->DrCmndCon);
351 	writel(0, &regs->DwCmndPro);
352 	writel(0, &regs->DwCmndCon);
353 	writel(0, &regs->DwCmndRef);
354 	writel(0, &regs->DrDataPro);
355 	writel(0, &regs->DrDataCon);
356 	writel(0, &regs->DrDataRef);
357 	writel(0, &regs->DwDataPro);
358 	writel(0, &regs->DwDataCon);
359 	writel(0, &regs->DwDataRef);
360 #endif
361 
362 	writel(0xffffffff, &regs->MbEvent);
363 	writel(0, &regs->Event);
364 
365 	writel(0, &regs->TxPi);
366 	writel(0, &regs->IpRxPi);
367 
368 	writel(0, &regs->EvtCon);
369 	writel(0, &regs->EvtPrd);
370 
371 	rrpriv->info->evt_ctrl.pi = 0;
372 
373 	for (i = 0; i < CMD_RING_ENTRIES; i++)
374 		writel(0, &regs->CmdRing[i]);
375 
376 /*
377  * Why 32 ? is this not cache line size dependent?
378  */
379 	writel(RBURST_64|WBURST_64, &regs->PciState);
380 	wmb();
381 
382 	start_pc = rr_read_eeprom_word(rrpriv,
383 			offsetof(struct eeprom, rncd_info.FwStart));
384 
385 #if (DEBUG > 1)
386 	printk("%s: Executing firmware at address 0x%06x\n",
387 	       dev->name, start_pc);
388 #endif
389 
390 	writel(start_pc + 0x800, &regs->Pc);
391 	wmb();
392 	udelay(5);
393 
394 	writel(start_pc, &regs->Pc);
395 	wmb();
396 
397 	return 0;
398 }
399 
400 
401 /*
402  * Read a string from the EEPROM.
403  */
404 static unsigned int rr_read_eeprom(struct rr_private *rrpriv,
405 				unsigned long offset,
406 				unsigned char *buf,
407 				unsigned long length)
408 {
409 	struct rr_regs __iomem *regs = rrpriv->regs;
410 	u32 misc, io, host, i;
411 
412 	io = readl(&regs->ExtIo);
413 	writel(0, &regs->ExtIo);
414 	misc = readl(&regs->LocalCtrl);
415 	writel(0, &regs->LocalCtrl);
416 	host = readl(&regs->HostCtrl);
417 	writel(host | HALT_NIC, &regs->HostCtrl);
418 	mb();
419 
420 	for (i = 0; i < length; i++){
421 		writel((EEPROM_BASE + ((offset+i) << 3)), &regs->WinBase);
422 		mb();
423 		buf[i] = (readl(&regs->WinData) >> 24) & 0xff;
424 		mb();
425 	}
426 
427 	writel(host, &regs->HostCtrl);
428 	writel(misc, &regs->LocalCtrl);
429 	writel(io, &regs->ExtIo);
430 	mb();
431 	return i;
432 }
433 
434 
435 /*
436  * Shortcut to read one word (4 bytes) out of the EEPROM and convert
437  * it to our CPU byte-order.
438  */
439 static u32 rr_read_eeprom_word(struct rr_private *rrpriv,
440 			    size_t offset)
441 {
442 	__be32 word;
443 
444 	if ((rr_read_eeprom(rrpriv, offset,
445 			    (unsigned char *)&word, 4) == 4))
446 		return be32_to_cpu(word);
447 	return 0;
448 }
449 
450 
451 /*
452  * Write a string to the EEPROM.
453  *
454  * This is only called when the firmware is not running.
455  */
456 static unsigned int write_eeprom(struct rr_private *rrpriv,
457 				 unsigned long offset,
458 				 unsigned char *buf,
459 				 unsigned long length)
460 {
461 	struct rr_regs __iomem *regs = rrpriv->regs;
462 	u32 misc, io, data, i, j, ready, error = 0;
463 
464 	io = readl(&regs->ExtIo);
465 	writel(0, &regs->ExtIo);
466 	misc = readl(&regs->LocalCtrl);
467 	writel(ENABLE_EEPROM_WRITE, &regs->LocalCtrl);
468 	mb();
469 
470 	for (i = 0; i < length; i++){
471 		writel((EEPROM_BASE + ((offset+i) << 3)), &regs->WinBase);
472 		mb();
473 		data = buf[i] << 24;
474 		/*
475 		 * Only try to write the data if it is not the same
476 		 * value already.
477 		 */
478 		if ((readl(&regs->WinData) & 0xff000000) != data){
479 			writel(data, &regs->WinData);
480 			ready = 0;
481 			j = 0;
482 			mb();
483 			while(!ready){
484 				udelay(20);
485 				if ((readl(&regs->WinData) & 0xff000000) ==
486 				    data)
487 					ready = 1;
488 				mb();
489 				if (j++ > 5000){
490 					printk("data mismatch: %08x, "
491 					       "WinData %08x\n", data,
492 					       readl(&regs->WinData));
493 					ready = 1;
494 					error = 1;
495 				}
496 			}
497 		}
498 	}
499 
500 	writel(misc, &regs->LocalCtrl);
501 	writel(io, &regs->ExtIo);
502 	mb();
503 
504 	return error;
505 }
506 
507 
508 static int rr_init(struct net_device *dev)
509 {
510 	struct rr_private *rrpriv;
511 	struct rr_regs __iomem *regs;
512 	u32 sram_size, rev;
513 
514 	rrpriv = netdev_priv(dev);
515 	regs = rrpriv->regs;
516 
517 	rev = readl(&regs->FwRev);
518 	rrpriv->fw_rev = rev;
519 	if (rev > 0x00020024)
520 		printk("  Firmware revision: %i.%i.%i\n", (rev >> 16),
521 		       ((rev >> 8) & 0xff), (rev & 0xff));
522 	else if (rev >= 0x00020000) {
523 		printk("  Firmware revision: %i.%i.%i (2.0.37 or "
524 		       "later is recommended)\n", (rev >> 16),
525 		       ((rev >> 8) & 0xff), (rev & 0xff));
526 	}else{
527 		printk("  Firmware revision too old: %i.%i.%i, please "
528 		       "upgrade to 2.0.37 or later.\n",
529 		       (rev >> 16), ((rev >> 8) & 0xff), (rev & 0xff));
530 	}
531 
532 #if (DEBUG > 2)
533 	printk("  Maximum receive rings %i\n", readl(&regs->MaxRxRng));
534 #endif
535 
536 	/*
537 	 * Read the hardware address from the eeprom.  The HW address
538 	 * is not really necessary for HIPPI but awfully convenient.
539 	 * The pointer arithmetic to put it in dev_addr is ugly, but
540 	 * Donald Becker does it this way for the GigE version of this
541 	 * card and it's shorter and more portable than any
542 	 * other method I've seen.  -VAL
543 	 */
544 
545 	*(__be16 *)(dev->dev_addr) =
546 	  htons(rr_read_eeprom_word(rrpriv, offsetof(struct eeprom, manf.BoardULA)));
547 	*(__be32 *)(dev->dev_addr+2) =
548 	  htonl(rr_read_eeprom_word(rrpriv, offsetof(struct eeprom, manf.BoardULA[4])));
549 
550 	printk("  MAC: %pM\n", dev->dev_addr);
551 
552 	sram_size = rr_read_eeprom_word(rrpriv, 8);
553 	printk("  SRAM size 0x%06x\n", sram_size);
554 
555 	return 0;
556 }
557 
558 
559 static int rr_init1(struct net_device *dev)
560 {
561 	struct rr_private *rrpriv;
562 	struct rr_regs __iomem *regs;
563 	unsigned long myjif, flags;
564 	struct cmd cmd;
565 	u32 hostctrl;
566 	int ecode = 0;
567 	short i;
568 
569 	rrpriv = netdev_priv(dev);
570 	regs = rrpriv->regs;
571 
572 	spin_lock_irqsave(&rrpriv->lock, flags);
573 
574 	hostctrl = readl(&regs->HostCtrl);
575 	writel(hostctrl | HALT_NIC | RR_CLEAR_INT, &regs->HostCtrl);
576 	wmb();
577 
578 	if (hostctrl & PARITY_ERR){
579 		printk("%s: Parity error halting NIC - this is serious!\n",
580 		       dev->name);
581 		spin_unlock_irqrestore(&rrpriv->lock, flags);
582 		ecode = -EFAULT;
583 		goto error;
584 	}
585 
586 	set_rxaddr(regs, rrpriv->rx_ctrl_dma);
587 	set_infoaddr(regs, rrpriv->info_dma);
588 
589 	rrpriv->info->evt_ctrl.entry_size = sizeof(struct event);
590 	rrpriv->info->evt_ctrl.entries = EVT_RING_ENTRIES;
591 	rrpriv->info->evt_ctrl.mode = 0;
592 	rrpriv->info->evt_ctrl.pi = 0;
593 	set_rraddr(&rrpriv->info->evt_ctrl.rngptr, rrpriv->evt_ring_dma);
594 
595 	rrpriv->info->cmd_ctrl.entry_size = sizeof(struct cmd);
596 	rrpriv->info->cmd_ctrl.entries = CMD_RING_ENTRIES;
597 	rrpriv->info->cmd_ctrl.mode = 0;
598 	rrpriv->info->cmd_ctrl.pi = 15;
599 
600 	for (i = 0; i < CMD_RING_ENTRIES; i++) {
601 		writel(0, &regs->CmdRing[i]);
602 	}
603 
604 	for (i = 0; i < TX_RING_ENTRIES; i++) {
605 		rrpriv->tx_ring[i].size = 0;
606 		set_rraddr(&rrpriv->tx_ring[i].addr, 0);
607 		rrpriv->tx_skbuff[i] = NULL;
608 	}
609 	rrpriv->info->tx_ctrl.entry_size = sizeof(struct tx_desc);
610 	rrpriv->info->tx_ctrl.entries = TX_RING_ENTRIES;
611 	rrpriv->info->tx_ctrl.mode = 0;
612 	rrpriv->info->tx_ctrl.pi = 0;
613 	set_rraddr(&rrpriv->info->tx_ctrl.rngptr, rrpriv->tx_ring_dma);
614 
615 	/*
616 	 * Set dirty_tx before we start receiving interrupts, otherwise
617 	 * the interrupt handler might think it is supposed to process
618 	 * tx ints before we are up and running, which may cause a null
619 	 * pointer access in the int handler.
620 	 */
621 	rrpriv->tx_full = 0;
622 	rrpriv->cur_rx = 0;
623 	rrpriv->dirty_rx = rrpriv->dirty_tx = 0;
624 
625 	rr_reset(dev);
626 
627 	/* Tuning values */
628 	writel(0x5000, &regs->ConRetry);
629 	writel(0x100, &regs->ConRetryTmr);
630 	writel(0x500000, &regs->ConTmout);
631  	writel(0x60, &regs->IntrTmr);
632 	writel(0x500000, &regs->TxDataMvTimeout);
633 	writel(0x200000, &regs->RxDataMvTimeout);
634  	writel(0x80, &regs->WriteDmaThresh);
635  	writel(0x80, &regs->ReadDmaThresh);
636 
637 	rrpriv->fw_running = 0;
638 	wmb();
639 
640 	hostctrl &= ~(HALT_NIC | INVALID_INST_B | PARITY_ERR);
641 	writel(hostctrl, &regs->HostCtrl);
642 	wmb();
643 
644 	spin_unlock_irqrestore(&rrpriv->lock, flags);
645 
646 	for (i = 0; i < RX_RING_ENTRIES; i++) {
647 		struct sk_buff *skb;
648 		dma_addr_t addr;
649 
650 		rrpriv->rx_ring[i].mode = 0;
651 		skb = alloc_skb(dev->mtu + HIPPI_HLEN, GFP_ATOMIC);
652 		if (!skb) {
653 			printk(KERN_WARNING "%s: Unable to allocate memory "
654 			       "for receive ring - halting NIC\n", dev->name);
655 			ecode = -ENOMEM;
656 			goto error;
657 		}
658 		rrpriv->rx_skbuff[i] = skb;
659 	        addr = pci_map_single(rrpriv->pci_dev, skb->data,
660 			dev->mtu + HIPPI_HLEN, PCI_DMA_FROMDEVICE);
661 		/*
662 		 * Sanity test to see if we conflict with the DMA
663 		 * limitations of the Roadrunner.
664 		 */
665 		if ((((unsigned long)skb->data) & 0xfff) > ~65320)
666 			printk("skb alloc error\n");
667 
668 		set_rraddr(&rrpriv->rx_ring[i].addr, addr);
669 		rrpriv->rx_ring[i].size = dev->mtu + HIPPI_HLEN;
670 	}
671 
672 	rrpriv->rx_ctrl[4].entry_size = sizeof(struct rx_desc);
673 	rrpriv->rx_ctrl[4].entries = RX_RING_ENTRIES;
674 	rrpriv->rx_ctrl[4].mode = 8;
675 	rrpriv->rx_ctrl[4].pi = 0;
676 	wmb();
677 	set_rraddr(&rrpriv->rx_ctrl[4].rngptr, rrpriv->rx_ring_dma);
678 
679 	udelay(1000);
680 
681 	/*
682 	 * Now start the FirmWare.
683 	 */
684 	cmd.code = C_START_FW;
685 	cmd.ring = 0;
686 	cmd.index = 0;
687 
688 	rr_issue_cmd(rrpriv, &cmd);
689 
690 	/*
691 	 * Give the FirmWare time to chew on the `get running' command.
692 	 */
693 	myjif = jiffies + 5 * HZ;
694 	while (time_before(jiffies, myjif) && !rrpriv->fw_running)
695 		cpu_relax();
696 
697 	netif_start_queue(dev);
698 
699 	return ecode;
700 
701  error:
702 	/*
703 	 * We might have gotten here because we are out of memory,
704 	 * make sure we release everything we allocated before failing
705 	 */
706 	for (i = 0; i < RX_RING_ENTRIES; i++) {
707 		struct sk_buff *skb = rrpriv->rx_skbuff[i];
708 
709 		if (skb) {
710 	        	pci_unmap_single(rrpriv->pci_dev,
711 					 rrpriv->rx_ring[i].addr.addrlo,
712 					 dev->mtu + HIPPI_HLEN,
713 					 PCI_DMA_FROMDEVICE);
714 			rrpriv->rx_ring[i].size = 0;
715 			set_rraddr(&rrpriv->rx_ring[i].addr, 0);
716 			dev_kfree_skb(skb);
717 			rrpriv->rx_skbuff[i] = NULL;
718 		}
719 	}
720 	return ecode;
721 }
722 
723 
724 /*
725  * All events are considered to be slow (RX/TX ints do not generate
726  * events) and are handled here, outside the main interrupt handler,
727  * to reduce the size of the handler.
728  */
729 static u32 rr_handle_event(struct net_device *dev, u32 prodidx, u32 eidx)
730 {
731 	struct rr_private *rrpriv;
732 	struct rr_regs __iomem *regs;
733 	u32 tmp;
734 
735 	rrpriv = netdev_priv(dev);
736 	regs = rrpriv->regs;
737 
738 	while (prodidx != eidx){
739 		switch (rrpriv->evt_ring[eidx].code){
740 		case E_NIC_UP:
741 			tmp = readl(&regs->FwRev);
742 			printk(KERN_INFO "%s: Firmware revision %i.%i.%i "
743 			       "up and running\n", dev->name,
744 			       (tmp >> 16), ((tmp >> 8) & 0xff), (tmp & 0xff));
745 			rrpriv->fw_running = 1;
746 			writel(RX_RING_ENTRIES - 1, &regs->IpRxPi);
747 			wmb();
748 			break;
749 		case E_LINK_ON:
750 			printk(KERN_INFO "%s: Optical link ON\n", dev->name);
751 			break;
752 		case E_LINK_OFF:
753 			printk(KERN_INFO "%s: Optical link OFF\n", dev->name);
754 			break;
755 		case E_RX_IDLE:
756 			printk(KERN_WARNING "%s: RX data not moving\n",
757 			       dev->name);
758 			goto drop;
759 		case E_WATCHDOG:
760 			printk(KERN_INFO "%s: The watchdog is here to see "
761 			       "us\n", dev->name);
762 			break;
763 		case E_INTERN_ERR:
764 			printk(KERN_ERR "%s: HIPPI Internal NIC error\n",
765 			       dev->name);
766 			writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
767 			       &regs->HostCtrl);
768 			wmb();
769 			break;
770 		case E_HOST_ERR:
771 			printk(KERN_ERR "%s: Host software error\n",
772 			       dev->name);
773 			writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
774 			       &regs->HostCtrl);
775 			wmb();
776 			break;
777 		/*
778 		 * TX events.
779 		 */
780 		case E_CON_REJ:
781 			printk(KERN_WARNING "%s: Connection rejected\n",
782 			       dev->name);
783 			dev->stats.tx_aborted_errors++;
784 			break;
785 		case E_CON_TMOUT:
786 			printk(KERN_WARNING "%s: Connection timeout\n",
787 			       dev->name);
788 			break;
789 		case E_DISC_ERR:
790 			printk(KERN_WARNING "%s: HIPPI disconnect error\n",
791 			       dev->name);
792 			dev->stats.tx_aborted_errors++;
793 			break;
794 		case E_INT_PRTY:
795 			printk(KERN_ERR "%s: HIPPI Internal Parity error\n",
796 			       dev->name);
797 			writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
798 			       &regs->HostCtrl);
799 			wmb();
800 			break;
801 		case E_TX_IDLE:
802 			printk(KERN_WARNING "%s: Transmitter idle\n",
803 			       dev->name);
804 			break;
805 		case E_TX_LINK_DROP:
806 			printk(KERN_WARNING "%s: Link lost during transmit\n",
807 			       dev->name);
808 			dev->stats.tx_aborted_errors++;
809 			writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
810 			       &regs->HostCtrl);
811 			wmb();
812 			break;
813 		case E_TX_INV_RNG:
814 			printk(KERN_ERR "%s: Invalid send ring block\n",
815 			       dev->name);
816 			writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
817 			       &regs->HostCtrl);
818 			wmb();
819 			break;
820 		case E_TX_INV_BUF:
821 			printk(KERN_ERR "%s: Invalid send buffer address\n",
822 			       dev->name);
823 			writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
824 			       &regs->HostCtrl);
825 			wmb();
826 			break;
827 		case E_TX_INV_DSC:
828 			printk(KERN_ERR "%s: Invalid descriptor address\n",
829 			       dev->name);
830 			writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
831 			       &regs->HostCtrl);
832 			wmb();
833 			break;
834 		/*
835 		 * RX events.
836 		 */
837 		case E_RX_RNG_OUT:
838 			printk(KERN_INFO "%s: Receive ring full\n", dev->name);
839 			break;
840 
841 		case E_RX_PAR_ERR:
842 			printk(KERN_WARNING "%s: Receive parity error\n",
843 			       dev->name);
844 			goto drop;
845 		case E_RX_LLRC_ERR:
846 			printk(KERN_WARNING "%s: Receive LLRC error\n",
847 			       dev->name);
848 			goto drop;
849 		case E_PKT_LN_ERR:
850 			printk(KERN_WARNING "%s: Receive packet length "
851 			       "error\n", dev->name);
852 			goto drop;
853 		case E_DTA_CKSM_ERR:
854 			printk(KERN_WARNING "%s: Data checksum error\n",
855 			       dev->name);
856 			goto drop;
857 		case E_SHT_BST:
858 			printk(KERN_WARNING "%s: Unexpected short burst "
859 			       "error\n", dev->name);
860 			goto drop;
861 		case E_STATE_ERR:
862 			printk(KERN_WARNING "%s: Recv. state transition"
863 			       " error\n", dev->name);
864 			goto drop;
865 		case E_UNEXP_DATA:
866 			printk(KERN_WARNING "%s: Unexpected data error\n",
867 			       dev->name);
868 			goto drop;
869 		case E_LST_LNK_ERR:
870 			printk(KERN_WARNING "%s: Link lost error\n",
871 			       dev->name);
872 			goto drop;
873 		case E_FRM_ERR:
874 			printk(KERN_WARNING "%s: Framming Error\n",
875 			       dev->name);
876 			goto drop;
877 		case E_FLG_SYN_ERR:
878 			printk(KERN_WARNING "%s: Flag sync. lost during "
879 			       "packet\n", dev->name);
880 			goto drop;
881 		case E_RX_INV_BUF:
882 			printk(KERN_ERR "%s: Invalid receive buffer "
883 			       "address\n", dev->name);
884 			writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
885 			       &regs->HostCtrl);
886 			wmb();
887 			break;
888 		case E_RX_INV_DSC:
889 			printk(KERN_ERR "%s: Invalid receive descriptor "
890 			       "address\n", dev->name);
891 			writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
892 			       &regs->HostCtrl);
893 			wmb();
894 			break;
895 		case E_RNG_BLK:
896 			printk(KERN_ERR "%s: Invalid ring block\n",
897 			       dev->name);
898 			writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
899 			       &regs->HostCtrl);
900 			wmb();
901 			break;
902 		drop:
903 			/* Label packet to be dropped.
904 			 * Actual dropping occurs in rx
905 			 * handling.
906 			 *
907 			 * The index of packet we get to drop is
908 			 * the index of the packet following
909 			 * the bad packet. -kbf
910 			 */
911 			{
912 				u16 index = rrpriv->evt_ring[eidx].index;
913 				index = (index + (RX_RING_ENTRIES - 1)) %
914 					RX_RING_ENTRIES;
915 				rrpriv->rx_ring[index].mode |=
916 					(PACKET_BAD | PACKET_END);
917 			}
918 			break;
919 		default:
920 			printk(KERN_WARNING "%s: Unhandled event 0x%02x\n",
921 			       dev->name, rrpriv->evt_ring[eidx].code);
922 		}
923 		eidx = (eidx + 1) % EVT_RING_ENTRIES;
924 	}
925 
926 	rrpriv->info->evt_ctrl.pi = eidx;
927 	wmb();
928 	return eidx;
929 }
930 
931 
932 static void rx_int(struct net_device *dev, u32 rxlimit, u32 index)
933 {
934 	struct rr_private *rrpriv = netdev_priv(dev);
935 	struct rr_regs __iomem *regs = rrpriv->regs;
936 
937 	do {
938 		struct rx_desc *desc;
939 		u32 pkt_len;
940 
941 		desc = &(rrpriv->rx_ring[index]);
942 		pkt_len = desc->size;
943 #if (DEBUG > 2)
944 		printk("index %i, rxlimit %i\n", index, rxlimit);
945 		printk("len %x, mode %x\n", pkt_len, desc->mode);
946 #endif
947 		if ( (rrpriv->rx_ring[index].mode & PACKET_BAD) == PACKET_BAD){
948 			dev->stats.rx_dropped++;
949 			goto defer;
950 		}
951 
952 		if (pkt_len > 0){
953 			struct sk_buff *skb, *rx_skb;
954 
955 			rx_skb = rrpriv->rx_skbuff[index];
956 
957 			if (pkt_len < PKT_COPY_THRESHOLD) {
958 				skb = alloc_skb(pkt_len, GFP_ATOMIC);
959 				if (skb == NULL){
960 					printk(KERN_WARNING "%s: Unable to allocate skb (%i bytes), deferring packet\n", dev->name, pkt_len);
961 					dev->stats.rx_dropped++;
962 					goto defer;
963 				} else {
964 					pci_dma_sync_single_for_cpu(rrpriv->pci_dev,
965 								    desc->addr.addrlo,
966 								    pkt_len,
967 								    PCI_DMA_FROMDEVICE);
968 
969 					memcpy(skb_put(skb, pkt_len),
970 					       rx_skb->data, pkt_len);
971 
972 					pci_dma_sync_single_for_device(rrpriv->pci_dev,
973 								       desc->addr.addrlo,
974 								       pkt_len,
975 								       PCI_DMA_FROMDEVICE);
976 				}
977 			}else{
978 				struct sk_buff *newskb;
979 
980 				newskb = alloc_skb(dev->mtu + HIPPI_HLEN,
981 					GFP_ATOMIC);
982 				if (newskb){
983 					dma_addr_t addr;
984 
985 	        			pci_unmap_single(rrpriv->pci_dev,
986 						desc->addr.addrlo, dev->mtu +
987 						HIPPI_HLEN, PCI_DMA_FROMDEVICE);
988 					skb = rx_skb;
989 					skb_put(skb, pkt_len);
990 					rrpriv->rx_skbuff[index] = newskb;
991 	        			addr = pci_map_single(rrpriv->pci_dev,
992 						newskb->data,
993 						dev->mtu + HIPPI_HLEN,
994 						PCI_DMA_FROMDEVICE);
995 					set_rraddr(&desc->addr, addr);
996 				} else {
997 					printk("%s: Out of memory, deferring "
998 					       "packet\n", dev->name);
999 					dev->stats.rx_dropped++;
1000 					goto defer;
1001 				}
1002 			}
1003 			skb->protocol = hippi_type_trans(skb, dev);
1004 
1005 			netif_rx(skb);		/* send it up */
1006 
1007 			dev->stats.rx_packets++;
1008 			dev->stats.rx_bytes += pkt_len;
1009 		}
1010 	defer:
1011 		desc->mode = 0;
1012 		desc->size = dev->mtu + HIPPI_HLEN;
1013 
1014 		if ((index & 7) == 7)
1015 			writel(index, &regs->IpRxPi);
1016 
1017 		index = (index + 1) % RX_RING_ENTRIES;
1018 	} while(index != rxlimit);
1019 
1020 	rrpriv->cur_rx = index;
1021 	wmb();
1022 }
1023 
1024 
1025 static irqreturn_t rr_interrupt(int irq, void *dev_id)
1026 {
1027 	struct rr_private *rrpriv;
1028 	struct rr_regs __iomem *regs;
1029 	struct net_device *dev = (struct net_device *)dev_id;
1030 	u32 prodidx, rxindex, eidx, txcsmr, rxlimit, txcon;
1031 
1032 	rrpriv = netdev_priv(dev);
1033 	regs = rrpriv->regs;
1034 
1035 	if (!(readl(&regs->HostCtrl) & RR_INT))
1036 		return IRQ_NONE;
1037 
1038 	spin_lock(&rrpriv->lock);
1039 
1040 	prodidx = readl(&regs->EvtPrd);
1041 	txcsmr = (prodidx >> 8) & 0xff;
1042 	rxlimit = (prodidx >> 16) & 0xff;
1043 	prodidx &= 0xff;
1044 
1045 #if (DEBUG > 2)
1046 	printk("%s: interrupt, prodidx = %i, eidx = %i\n", dev->name,
1047 	       prodidx, rrpriv->info->evt_ctrl.pi);
1048 #endif
1049 	/*
1050 	 * Order here is important.  We must handle events
1051 	 * before doing anything else in order to catch
1052 	 * such things as LLRC errors, etc -kbf
1053 	 */
1054 
1055 	eidx = rrpriv->info->evt_ctrl.pi;
1056 	if (prodidx != eidx)
1057 		eidx = rr_handle_event(dev, prodidx, eidx);
1058 
1059 	rxindex = rrpriv->cur_rx;
1060 	if (rxindex != rxlimit)
1061 		rx_int(dev, rxlimit, rxindex);
1062 
1063 	txcon = rrpriv->dirty_tx;
1064 	if (txcsmr != txcon) {
1065 		do {
1066 			/* Due to occational firmware TX producer/consumer out
1067 			 * of sync. error need to check entry in ring -kbf
1068 			 */
1069 			if(rrpriv->tx_skbuff[txcon]){
1070 				struct tx_desc *desc;
1071 				struct sk_buff *skb;
1072 
1073 				desc = &(rrpriv->tx_ring[txcon]);
1074 				skb = rrpriv->tx_skbuff[txcon];
1075 
1076 				dev->stats.tx_packets++;
1077 				dev->stats.tx_bytes += skb->len;
1078 
1079 				pci_unmap_single(rrpriv->pci_dev,
1080 						 desc->addr.addrlo, skb->len,
1081 						 PCI_DMA_TODEVICE);
1082 				dev_kfree_skb_irq(skb);
1083 
1084 				rrpriv->tx_skbuff[txcon] = NULL;
1085 				desc->size = 0;
1086 				set_rraddr(&rrpriv->tx_ring[txcon].addr, 0);
1087 				desc->mode = 0;
1088 			}
1089 			txcon = (txcon + 1) % TX_RING_ENTRIES;
1090 		} while (txcsmr != txcon);
1091 		wmb();
1092 
1093 		rrpriv->dirty_tx = txcon;
1094 		if (rrpriv->tx_full && rr_if_busy(dev) &&
1095 		    (((rrpriv->info->tx_ctrl.pi + 1) % TX_RING_ENTRIES)
1096 		     != rrpriv->dirty_tx)){
1097 			rrpriv->tx_full = 0;
1098 			netif_wake_queue(dev);
1099 		}
1100 	}
1101 
1102 	eidx |= ((txcsmr << 8) | (rxlimit << 16));
1103 	writel(eidx, &regs->EvtCon);
1104 	wmb();
1105 
1106 	spin_unlock(&rrpriv->lock);
1107 	return IRQ_HANDLED;
1108 }
1109 
1110 static inline void rr_raz_tx(struct rr_private *rrpriv,
1111 			     struct net_device *dev)
1112 {
1113 	int i;
1114 
1115 	for (i = 0; i < TX_RING_ENTRIES; i++) {
1116 		struct sk_buff *skb = rrpriv->tx_skbuff[i];
1117 
1118 		if (skb) {
1119 			struct tx_desc *desc = &(rrpriv->tx_ring[i]);
1120 
1121 	        	pci_unmap_single(rrpriv->pci_dev, desc->addr.addrlo,
1122 				skb->len, PCI_DMA_TODEVICE);
1123 			desc->size = 0;
1124 			set_rraddr(&desc->addr, 0);
1125 			dev_kfree_skb(skb);
1126 			rrpriv->tx_skbuff[i] = NULL;
1127 		}
1128 	}
1129 }
1130 
1131 
1132 static inline void rr_raz_rx(struct rr_private *rrpriv,
1133 			     struct net_device *dev)
1134 {
1135 	int i;
1136 
1137 	for (i = 0; i < RX_RING_ENTRIES; i++) {
1138 		struct sk_buff *skb = rrpriv->rx_skbuff[i];
1139 
1140 		if (skb) {
1141 			struct rx_desc *desc = &(rrpriv->rx_ring[i]);
1142 
1143 	        	pci_unmap_single(rrpriv->pci_dev, desc->addr.addrlo,
1144 				dev->mtu + HIPPI_HLEN, PCI_DMA_FROMDEVICE);
1145 			desc->size = 0;
1146 			set_rraddr(&desc->addr, 0);
1147 			dev_kfree_skb(skb);
1148 			rrpriv->rx_skbuff[i] = NULL;
1149 		}
1150 	}
1151 }
1152 
1153 static void rr_timer(unsigned long data)
1154 {
1155 	struct net_device *dev = (struct net_device *)data;
1156 	struct rr_private *rrpriv = netdev_priv(dev);
1157 	struct rr_regs __iomem *regs = rrpriv->regs;
1158 	unsigned long flags;
1159 
1160 	if (readl(&regs->HostCtrl) & NIC_HALTED){
1161 		printk("%s: Restarting nic\n", dev->name);
1162 		memset(rrpriv->rx_ctrl, 0, 256 * sizeof(struct ring_ctrl));
1163 		memset(rrpriv->info, 0, sizeof(struct rr_info));
1164 		wmb();
1165 
1166 		rr_raz_tx(rrpriv, dev);
1167 		rr_raz_rx(rrpriv, dev);
1168 
1169 		if (rr_init1(dev)) {
1170 			spin_lock_irqsave(&rrpriv->lock, flags);
1171 			writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
1172 			       &regs->HostCtrl);
1173 			spin_unlock_irqrestore(&rrpriv->lock, flags);
1174 		}
1175 	}
1176 	rrpriv->timer.expires = RUN_AT(5*HZ);
1177 	add_timer(&rrpriv->timer);
1178 }
1179 
1180 
1181 static int rr_open(struct net_device *dev)
1182 {
1183 	struct rr_private *rrpriv = netdev_priv(dev);
1184 	struct pci_dev *pdev = rrpriv->pci_dev;
1185 	struct rr_regs __iomem *regs;
1186 	int ecode = 0;
1187 	unsigned long flags;
1188 	dma_addr_t dma_addr;
1189 
1190 	regs = rrpriv->regs;
1191 
1192 	if (rrpriv->fw_rev < 0x00020000) {
1193 		printk(KERN_WARNING "%s: trying to configure device with "
1194 		       "obsolete firmware\n", dev->name);
1195 		ecode = -EBUSY;
1196 		goto error;
1197 	}
1198 
1199 	rrpriv->rx_ctrl = pci_alloc_consistent(pdev,
1200 					       256 * sizeof(struct ring_ctrl),
1201 					       &dma_addr);
1202 	if (!rrpriv->rx_ctrl) {
1203 		ecode = -ENOMEM;
1204 		goto error;
1205 	}
1206 	rrpriv->rx_ctrl_dma = dma_addr;
1207 	memset(rrpriv->rx_ctrl, 0, 256*sizeof(struct ring_ctrl));
1208 
1209 	rrpriv->info = pci_alloc_consistent(pdev, sizeof(struct rr_info),
1210 					    &dma_addr);
1211 	if (!rrpriv->info) {
1212 		ecode = -ENOMEM;
1213 		goto error;
1214 	}
1215 	rrpriv->info_dma = dma_addr;
1216 	memset(rrpriv->info, 0, sizeof(struct rr_info));
1217 	wmb();
1218 
1219 	spin_lock_irqsave(&rrpriv->lock, flags);
1220 	writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT, &regs->HostCtrl);
1221 	readl(&regs->HostCtrl);
1222 	spin_unlock_irqrestore(&rrpriv->lock, flags);
1223 
1224 	if (request_irq(pdev->irq, rr_interrupt, IRQF_SHARED, dev->name, dev)) {
1225 		printk(KERN_WARNING "%s: Requested IRQ %d is busy\n",
1226 		       dev->name, pdev->irq);
1227 		ecode = -EAGAIN;
1228 		goto error;
1229 	}
1230 
1231 	if ((ecode = rr_init1(dev)))
1232 		goto error;
1233 
1234 	/* Set the timer to switch to check for link beat and perhaps switch
1235 	   to an alternate media type. */
1236 	init_timer(&rrpriv->timer);
1237 	rrpriv->timer.expires = RUN_AT(5*HZ);           /* 5 sec. watchdog */
1238 	rrpriv->timer.data = (unsigned long)dev;
1239 	rrpriv->timer.function = rr_timer;               /* timer handler */
1240 	add_timer(&rrpriv->timer);
1241 
1242 	netif_start_queue(dev);
1243 
1244 	return ecode;
1245 
1246  error:
1247 	spin_lock_irqsave(&rrpriv->lock, flags);
1248 	writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT, &regs->HostCtrl);
1249 	spin_unlock_irqrestore(&rrpriv->lock, flags);
1250 
1251 	if (rrpriv->info) {
1252 		pci_free_consistent(pdev, sizeof(struct rr_info), rrpriv->info,
1253 				    rrpriv->info_dma);
1254 		rrpriv->info = NULL;
1255 	}
1256 	if (rrpriv->rx_ctrl) {
1257 		pci_free_consistent(pdev, sizeof(struct ring_ctrl),
1258 				    rrpriv->rx_ctrl, rrpriv->rx_ctrl_dma);
1259 		rrpriv->rx_ctrl = NULL;
1260 	}
1261 
1262 	netif_stop_queue(dev);
1263 
1264 	return ecode;
1265 }
1266 
1267 
1268 static void rr_dump(struct net_device *dev)
1269 {
1270 	struct rr_private *rrpriv;
1271 	struct rr_regs __iomem *regs;
1272 	u32 index, cons;
1273 	short i;
1274 	int len;
1275 
1276 	rrpriv = netdev_priv(dev);
1277 	regs = rrpriv->regs;
1278 
1279 	printk("%s: dumping NIC TX rings\n", dev->name);
1280 
1281 	printk("RxPrd %08x, TxPrd %02x, EvtPrd %08x, TxPi %02x, TxCtrlPi %02x\n",
1282 	       readl(&regs->RxPrd), readl(&regs->TxPrd),
1283 	       readl(&regs->EvtPrd), readl(&regs->TxPi),
1284 	       rrpriv->info->tx_ctrl.pi);
1285 
1286 	printk("Error code 0x%x\n", readl(&regs->Fail1));
1287 
1288 	index = (((readl(&regs->EvtPrd) >> 8) & 0xff) - 1) % TX_RING_ENTRIES;
1289 	cons = rrpriv->dirty_tx;
1290 	printk("TX ring index %i, TX consumer %i\n",
1291 	       index, cons);
1292 
1293 	if (rrpriv->tx_skbuff[index]){
1294 		len = min_t(int, 0x80, rrpriv->tx_skbuff[index]->len);
1295 		printk("skbuff for index %i is valid - dumping data (0x%x bytes - DMA len 0x%x)\n", index, len, rrpriv->tx_ring[index].size);
1296 		for (i = 0; i < len; i++){
1297 			if (!(i & 7))
1298 				printk("\n");
1299 			printk("%02x ", (unsigned char) rrpriv->tx_skbuff[index]->data[i]);
1300 		}
1301 		printk("\n");
1302 	}
1303 
1304 	if (rrpriv->tx_skbuff[cons]){
1305 		len = min_t(int, 0x80, rrpriv->tx_skbuff[cons]->len);
1306 		printk("skbuff for cons %i is valid - dumping data (0x%x bytes - skbuff len 0x%x)\n", cons, len, rrpriv->tx_skbuff[cons]->len);
1307 		printk("mode 0x%x, size 0x%x,\n phys %08Lx, skbuff-addr %08lx, truesize 0x%x\n",
1308 		       rrpriv->tx_ring[cons].mode,
1309 		       rrpriv->tx_ring[cons].size,
1310 		       (unsigned long long) rrpriv->tx_ring[cons].addr.addrlo,
1311 		       (unsigned long)rrpriv->tx_skbuff[cons]->data,
1312 		       (unsigned int)rrpriv->tx_skbuff[cons]->truesize);
1313 		for (i = 0; i < len; i++){
1314 			if (!(i & 7))
1315 				printk("\n");
1316 			printk("%02x ", (unsigned char)rrpriv->tx_ring[cons].size);
1317 		}
1318 		printk("\n");
1319 	}
1320 
1321 	printk("dumping TX ring info:\n");
1322 	for (i = 0; i < TX_RING_ENTRIES; i++)
1323 		printk("mode 0x%x, size 0x%x, phys-addr %08Lx\n",
1324 		       rrpriv->tx_ring[i].mode,
1325 		       rrpriv->tx_ring[i].size,
1326 		       (unsigned long long) rrpriv->tx_ring[i].addr.addrlo);
1327 
1328 }
1329 
1330 
1331 static int rr_close(struct net_device *dev)
1332 {
1333 	struct rr_private *rrpriv = netdev_priv(dev);
1334 	struct rr_regs __iomem *regs = rrpriv->regs;
1335 	struct pci_dev *pdev = rrpriv->pci_dev;
1336 	unsigned long flags;
1337 	u32 tmp;
1338 	short i;
1339 
1340 	netif_stop_queue(dev);
1341 
1342 
1343 	/*
1344 	 * Lock to make sure we are not cleaning up while another CPU
1345 	 * is handling interrupts.
1346 	 */
1347 	spin_lock_irqsave(&rrpriv->lock, flags);
1348 
1349 	tmp = readl(&regs->HostCtrl);
1350 	if (tmp & NIC_HALTED){
1351 		printk("%s: NIC already halted\n", dev->name);
1352 		rr_dump(dev);
1353 	}else{
1354 		tmp |= HALT_NIC | RR_CLEAR_INT;
1355 		writel(tmp, &regs->HostCtrl);
1356 		readl(&regs->HostCtrl);
1357 	}
1358 
1359 	rrpriv->fw_running = 0;
1360 
1361 	del_timer_sync(&rrpriv->timer);
1362 
1363 	writel(0, &regs->TxPi);
1364 	writel(0, &regs->IpRxPi);
1365 
1366 	writel(0, &regs->EvtCon);
1367 	writel(0, &regs->EvtPrd);
1368 
1369 	for (i = 0; i < CMD_RING_ENTRIES; i++)
1370 		writel(0, &regs->CmdRing[i]);
1371 
1372 	rrpriv->info->tx_ctrl.entries = 0;
1373 	rrpriv->info->cmd_ctrl.pi = 0;
1374 	rrpriv->info->evt_ctrl.pi = 0;
1375 	rrpriv->rx_ctrl[4].entries = 0;
1376 
1377 	rr_raz_tx(rrpriv, dev);
1378 	rr_raz_rx(rrpriv, dev);
1379 
1380 	pci_free_consistent(pdev, 256 * sizeof(struct ring_ctrl),
1381 			    rrpriv->rx_ctrl, rrpriv->rx_ctrl_dma);
1382 	rrpriv->rx_ctrl = NULL;
1383 
1384 	pci_free_consistent(pdev, sizeof(struct rr_info), rrpriv->info,
1385 			    rrpriv->info_dma);
1386 	rrpriv->info = NULL;
1387 
1388 	free_irq(pdev->irq, dev);
1389 	spin_unlock_irqrestore(&rrpriv->lock, flags);
1390 
1391 	return 0;
1392 }
1393 
1394 
1395 static netdev_tx_t rr_start_xmit(struct sk_buff *skb,
1396 				 struct net_device *dev)
1397 {
1398 	struct rr_private *rrpriv = netdev_priv(dev);
1399 	struct rr_regs __iomem *regs = rrpriv->regs;
1400 	struct hippi_cb *hcb = (struct hippi_cb *) skb->cb;
1401 	struct ring_ctrl *txctrl;
1402 	unsigned long flags;
1403 	u32 index, len = skb->len;
1404 	u32 *ifield;
1405 	struct sk_buff *new_skb;
1406 
1407 	if (readl(&regs->Mode) & FATAL_ERR)
1408 		printk("error codes Fail1 %02x, Fail2 %02x\n",
1409 		       readl(&regs->Fail1), readl(&regs->Fail2));
1410 
1411 	/*
1412 	 * We probably need to deal with tbusy here to prevent overruns.
1413 	 */
1414 
1415 	if (skb_headroom(skb) < 8){
1416 		printk("incoming skb too small - reallocating\n");
1417 		if (!(new_skb = dev_alloc_skb(len + 8))) {
1418 			dev_kfree_skb(skb);
1419 			netif_wake_queue(dev);
1420 			return NETDEV_TX_OK;
1421 		}
1422 		skb_reserve(new_skb, 8);
1423 		skb_put(new_skb, len);
1424 		skb_copy_from_linear_data(skb, new_skb->data, len);
1425 		dev_kfree_skb(skb);
1426 		skb = new_skb;
1427 	}
1428 
1429 	ifield = (u32 *)skb_push(skb, 8);
1430 
1431 	ifield[0] = 0;
1432 	ifield[1] = hcb->ifield;
1433 
1434 	/*
1435 	 * We don't need the lock before we are actually going to start
1436 	 * fiddling with the control blocks.
1437 	 */
1438 	spin_lock_irqsave(&rrpriv->lock, flags);
1439 
1440 	txctrl = &rrpriv->info->tx_ctrl;
1441 
1442 	index = txctrl->pi;
1443 
1444 	rrpriv->tx_skbuff[index] = skb;
1445 	set_rraddr(&rrpriv->tx_ring[index].addr, pci_map_single(
1446 		rrpriv->pci_dev, skb->data, len + 8, PCI_DMA_TODEVICE));
1447 	rrpriv->tx_ring[index].size = len + 8; /* include IFIELD */
1448 	rrpriv->tx_ring[index].mode = PACKET_START | PACKET_END;
1449 	txctrl->pi = (index + 1) % TX_RING_ENTRIES;
1450 	wmb();
1451 	writel(txctrl->pi, &regs->TxPi);
1452 
1453 	if (txctrl->pi == rrpriv->dirty_tx){
1454 		rrpriv->tx_full = 1;
1455 		netif_stop_queue(dev);
1456 	}
1457 
1458 	spin_unlock_irqrestore(&rrpriv->lock, flags);
1459 
1460 	return NETDEV_TX_OK;
1461 }
1462 
1463 
1464 /*
1465  * Read the firmware out of the EEPROM and put it into the SRAM
1466  * (or from user space - later)
1467  *
1468  * This operation requires the NIC to be halted and is performed with
1469  * interrupts disabled and with the spinlock hold.
1470  */
1471 static int rr_load_firmware(struct net_device *dev)
1472 {
1473 	struct rr_private *rrpriv;
1474 	struct rr_regs __iomem *regs;
1475 	size_t eptr, segptr;
1476 	int i, j;
1477 	u32 localctrl, sptr, len, tmp;
1478 	u32 p2len, p2size, nr_seg, revision, io, sram_size;
1479 
1480 	rrpriv = netdev_priv(dev);
1481 	regs = rrpriv->regs;
1482 
1483 	if (dev->flags & IFF_UP)
1484 		return -EBUSY;
1485 
1486 	if (!(readl(&regs->HostCtrl) & NIC_HALTED)){
1487 		printk("%s: Trying to load firmware to a running NIC.\n",
1488 		       dev->name);
1489 		return -EBUSY;
1490 	}
1491 
1492 	localctrl = readl(&regs->LocalCtrl);
1493 	writel(0, &regs->LocalCtrl);
1494 
1495 	writel(0, &regs->EvtPrd);
1496 	writel(0, &regs->RxPrd);
1497 	writel(0, &regs->TxPrd);
1498 
1499 	/*
1500 	 * First wipe the entire SRAM, otherwise we might run into all
1501 	 * kinds of trouble ... sigh, this took almost all afternoon
1502 	 * to track down ;-(
1503 	 */
1504 	io = readl(&regs->ExtIo);
1505 	writel(0, &regs->ExtIo);
1506 	sram_size = rr_read_eeprom_word(rrpriv, 8);
1507 
1508 	for (i = 200; i < sram_size / 4; i++){
1509 		writel(i * 4, &regs->WinBase);
1510 		mb();
1511 		writel(0, &regs->WinData);
1512 		mb();
1513 	}
1514 	writel(io, &regs->ExtIo);
1515 	mb();
1516 
1517 	eptr = rr_read_eeprom_word(rrpriv,
1518 		       offsetof(struct eeprom, rncd_info.AddrRunCodeSegs));
1519 	eptr = ((eptr & 0x1fffff) >> 3);
1520 
1521 	p2len = rr_read_eeprom_word(rrpriv, 0x83*4);
1522 	p2len = (p2len << 2);
1523 	p2size = rr_read_eeprom_word(rrpriv, 0x84*4);
1524 	p2size = ((p2size & 0x1fffff) >> 3);
1525 
1526 	if ((eptr < p2size) || (eptr > (p2size + p2len))){
1527 		printk("%s: eptr is invalid\n", dev->name);
1528 		goto out;
1529 	}
1530 
1531 	revision = rr_read_eeprom_word(rrpriv,
1532 			offsetof(struct eeprom, manf.HeaderFmt));
1533 
1534 	if (revision != 1){
1535 		printk("%s: invalid firmware format (%i)\n",
1536 		       dev->name, revision);
1537 		goto out;
1538 	}
1539 
1540 	nr_seg = rr_read_eeprom_word(rrpriv, eptr);
1541 	eptr +=4;
1542 #if (DEBUG > 1)
1543 	printk("%s: nr_seg %i\n", dev->name, nr_seg);
1544 #endif
1545 
1546 	for (i = 0; i < nr_seg; i++){
1547 		sptr = rr_read_eeprom_word(rrpriv, eptr);
1548 		eptr += 4;
1549 		len = rr_read_eeprom_word(rrpriv, eptr);
1550 		eptr += 4;
1551 		segptr = rr_read_eeprom_word(rrpriv, eptr);
1552 		segptr = ((segptr & 0x1fffff) >> 3);
1553 		eptr += 4;
1554 #if (DEBUG > 1)
1555 		printk("%s: segment %i, sram address %06x, length %04x, segptr %06x\n",
1556 		       dev->name, i, sptr, len, segptr);
1557 #endif
1558 		for (j = 0; j < len; j++){
1559 			tmp = rr_read_eeprom_word(rrpriv, segptr);
1560 			writel(sptr, &regs->WinBase);
1561 			mb();
1562 			writel(tmp, &regs->WinData);
1563 			mb();
1564 			segptr += 4;
1565 			sptr += 4;
1566 		}
1567 	}
1568 
1569 out:
1570 	writel(localctrl, &regs->LocalCtrl);
1571 	mb();
1572 	return 0;
1573 }
1574 
1575 
1576 static int rr_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1577 {
1578 	struct rr_private *rrpriv;
1579 	unsigned char *image, *oldimage;
1580 	unsigned long flags;
1581 	unsigned int i;
1582 	int error = -EOPNOTSUPP;
1583 
1584 	rrpriv = netdev_priv(dev);
1585 
1586 	switch(cmd){
1587 	case SIOCRRGFW:
1588 		if (!capable(CAP_SYS_RAWIO)){
1589 			return -EPERM;
1590 		}
1591 
1592 		image = kmalloc(EEPROM_WORDS * sizeof(u32), GFP_KERNEL);
1593 		if (!image)
1594 			return -ENOMEM;
1595 
1596 		if (rrpriv->fw_running){
1597 			printk("%s: Firmware already running\n", dev->name);
1598 			error = -EPERM;
1599 			goto gf_out;
1600 		}
1601 
1602 		spin_lock_irqsave(&rrpriv->lock, flags);
1603 		i = rr_read_eeprom(rrpriv, 0, image, EEPROM_BYTES);
1604 		spin_unlock_irqrestore(&rrpriv->lock, flags);
1605 		if (i != EEPROM_BYTES){
1606 			printk(KERN_ERR "%s: Error reading EEPROM\n",
1607 			       dev->name);
1608 			error = -EFAULT;
1609 			goto gf_out;
1610 		}
1611 		error = copy_to_user(rq->ifr_data, image, EEPROM_BYTES);
1612 		if (error)
1613 			error = -EFAULT;
1614 	gf_out:
1615 		kfree(image);
1616 		return error;
1617 
1618 	case SIOCRRPFW:
1619 		if (!capable(CAP_SYS_RAWIO)){
1620 			return -EPERM;
1621 		}
1622 
1623 		image = kmalloc(EEPROM_WORDS * sizeof(u32), GFP_KERNEL);
1624 		oldimage = kmalloc(EEPROM_WORDS * sizeof(u32), GFP_KERNEL);
1625 		if (!image || !oldimage) {
1626 			error = -ENOMEM;
1627 			goto wf_out;
1628 		}
1629 
1630 		error = copy_from_user(image, rq->ifr_data, EEPROM_BYTES);
1631 		if (error) {
1632 			error = -EFAULT;
1633 			goto wf_out;
1634 		}
1635 
1636 		if (rrpriv->fw_running){
1637 			printk("%s: Firmware already running\n", dev->name);
1638 			error = -EPERM;
1639 			goto wf_out;
1640 		}
1641 
1642 		printk("%s: Updating EEPROM firmware\n", dev->name);
1643 
1644 		spin_lock_irqsave(&rrpriv->lock, flags);
1645 		error = write_eeprom(rrpriv, 0, image, EEPROM_BYTES);
1646 		if (error)
1647 			printk(KERN_ERR "%s: Error writing EEPROM\n",
1648 			       dev->name);
1649 
1650 		i = rr_read_eeprom(rrpriv, 0, oldimage, EEPROM_BYTES);
1651 		spin_unlock_irqrestore(&rrpriv->lock, flags);
1652 
1653 		if (i != EEPROM_BYTES)
1654 			printk(KERN_ERR "%s: Error reading back EEPROM "
1655 			       "image\n", dev->name);
1656 
1657 		error = memcmp(image, oldimage, EEPROM_BYTES);
1658 		if (error){
1659 			printk(KERN_ERR "%s: Error verifying EEPROM image\n",
1660 			       dev->name);
1661 			error = -EFAULT;
1662 		}
1663 	wf_out:
1664 		kfree(oldimage);
1665 		kfree(image);
1666 		return error;
1667 
1668 	case SIOCRRID:
1669 		return put_user(0x52523032, (int __user *)rq->ifr_data);
1670 	default:
1671 		return error;
1672 	}
1673 }
1674 
1675 static DEFINE_PCI_DEVICE_TABLE(rr_pci_tbl) = {
1676 	{ PCI_VENDOR_ID_ESSENTIAL, PCI_DEVICE_ID_ESSENTIAL_ROADRUNNER,
1677 		PCI_ANY_ID, PCI_ANY_ID, },
1678 	{ 0,}
1679 };
1680 MODULE_DEVICE_TABLE(pci, rr_pci_tbl);
1681 
1682 static struct pci_driver rr_driver = {
1683 	.name		= "rrunner",
1684 	.id_table	= rr_pci_tbl,
1685 	.probe		= rr_init_one,
1686 	.remove		= rr_remove_one,
1687 };
1688 
1689 module_pci_driver(rr_driver);
1690