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