xref: /openbmc/linux/drivers/net/hippi/rrunner.c (revision 6a74f103)
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 	spin_unlock_irqrestore(&rrpriv->lock, flags);
1359 	del_timer_sync(&rrpriv->timer);
1360 	spin_lock_irqsave(&rrpriv->lock, flags);
1361 
1362 	writel(0, &regs->TxPi);
1363 	writel(0, &regs->IpRxPi);
1364 
1365 	writel(0, &regs->EvtCon);
1366 	writel(0, &regs->EvtPrd);
1367 
1368 	for (i = 0; i < CMD_RING_ENTRIES; i++)
1369 		writel(0, &regs->CmdRing[i]);
1370 
1371 	rrpriv->info->tx_ctrl.entries = 0;
1372 	rrpriv->info->cmd_ctrl.pi = 0;
1373 	rrpriv->info->evt_ctrl.pi = 0;
1374 	rrpriv->rx_ctrl[4].entries = 0;
1375 
1376 	rr_raz_tx(rrpriv, dev);
1377 	rr_raz_rx(rrpriv, dev);
1378 
1379 	dma_free_coherent(&pdev->dev, 256 * sizeof(struct ring_ctrl),
1380 			  rrpriv->rx_ctrl, rrpriv->rx_ctrl_dma);
1381 	rrpriv->rx_ctrl = NULL;
1382 
1383 	dma_free_coherent(&pdev->dev, sizeof(struct rr_info), rrpriv->info,
1384 			  rrpriv->info_dma);
1385 	rrpriv->info = NULL;
1386 
1387 	spin_unlock_irqrestore(&rrpriv->lock, flags);
1388 	free_irq(pdev->irq, dev);
1389 
1390 	return 0;
1391 }
1392 
1393 
1394 static netdev_tx_t rr_start_xmit(struct sk_buff *skb,
1395 				 struct net_device *dev)
1396 {
1397 	struct rr_private *rrpriv = netdev_priv(dev);
1398 	struct rr_regs __iomem *regs = rrpriv->regs;
1399 	struct hippi_cb *hcb = (struct hippi_cb *) skb->cb;
1400 	struct ring_ctrl *txctrl;
1401 	unsigned long flags;
1402 	u32 index, len = skb->len;
1403 	u32 *ifield;
1404 	struct sk_buff *new_skb;
1405 
1406 	if (readl(&regs->Mode) & FATAL_ERR)
1407 		printk("error codes Fail1 %02x, Fail2 %02x\n",
1408 		       readl(&regs->Fail1), readl(&regs->Fail2));
1409 
1410 	/*
1411 	 * We probably need to deal with tbusy here to prevent overruns.
1412 	 */
1413 
1414 	if (skb_headroom(skb) < 8){
1415 		printk("incoming skb too small - reallocating\n");
1416 		if (!(new_skb = dev_alloc_skb(len + 8))) {
1417 			dev_kfree_skb(skb);
1418 			netif_wake_queue(dev);
1419 			return NETDEV_TX_OK;
1420 		}
1421 		skb_reserve(new_skb, 8);
1422 		skb_put(new_skb, len);
1423 		skb_copy_from_linear_data(skb, new_skb->data, len);
1424 		dev_kfree_skb(skb);
1425 		skb = new_skb;
1426 	}
1427 
1428 	ifield = skb_push(skb, 8);
1429 
1430 	ifield[0] = 0;
1431 	ifield[1] = hcb->ifield;
1432 
1433 	/*
1434 	 * We don't need the lock before we are actually going to start
1435 	 * fiddling with the control blocks.
1436 	 */
1437 	spin_lock_irqsave(&rrpriv->lock, flags);
1438 
1439 	txctrl = &rrpriv->info->tx_ctrl;
1440 
1441 	index = txctrl->pi;
1442 
1443 	rrpriv->tx_skbuff[index] = skb;
1444 	set_rraddr(&rrpriv->tx_ring[index].addr,
1445 		   dma_map_single(&rrpriv->pci_dev->dev, skb->data, len + 8, DMA_TO_DEVICE));
1446 	rrpriv->tx_ring[index].size = len + 8; /* include IFIELD */
1447 	rrpriv->tx_ring[index].mode = PACKET_START | PACKET_END;
1448 	txctrl->pi = (index + 1) % TX_RING_ENTRIES;
1449 	wmb();
1450 	writel(txctrl->pi, &regs->TxPi);
1451 
1452 	if (txctrl->pi == rrpriv->dirty_tx){
1453 		rrpriv->tx_full = 1;
1454 		netif_stop_queue(dev);
1455 	}
1456 
1457 	spin_unlock_irqrestore(&rrpriv->lock, flags);
1458 
1459 	return NETDEV_TX_OK;
1460 }
1461 
1462 
1463 /*
1464  * Read the firmware out of the EEPROM and put it into the SRAM
1465  * (or from user space - later)
1466  *
1467  * This operation requires the NIC to be halted and is performed with
1468  * interrupts disabled and with the spinlock hold.
1469  */
1470 static int rr_load_firmware(struct net_device *dev)
1471 {
1472 	struct rr_private *rrpriv;
1473 	struct rr_regs __iomem *regs;
1474 	size_t eptr, segptr;
1475 	int i, j;
1476 	u32 localctrl, sptr, len, tmp;
1477 	u32 p2len, p2size, nr_seg, revision, io, sram_size;
1478 
1479 	rrpriv = netdev_priv(dev);
1480 	regs = rrpriv->regs;
1481 
1482 	if (dev->flags & IFF_UP)
1483 		return -EBUSY;
1484 
1485 	if (!(readl(&regs->HostCtrl) & NIC_HALTED)){
1486 		printk("%s: Trying to load firmware to a running NIC.\n",
1487 		       dev->name);
1488 		return -EBUSY;
1489 	}
1490 
1491 	localctrl = readl(&regs->LocalCtrl);
1492 	writel(0, &regs->LocalCtrl);
1493 
1494 	writel(0, &regs->EvtPrd);
1495 	writel(0, &regs->RxPrd);
1496 	writel(0, &regs->TxPrd);
1497 
1498 	/*
1499 	 * First wipe the entire SRAM, otherwise we might run into all
1500 	 * kinds of trouble ... sigh, this took almost all afternoon
1501 	 * to track down ;-(
1502 	 */
1503 	io = readl(&regs->ExtIo);
1504 	writel(0, &regs->ExtIo);
1505 	sram_size = rr_read_eeprom_word(rrpriv, 8);
1506 
1507 	for (i = 200; i < sram_size / 4; i++){
1508 		writel(i * 4, &regs->WinBase);
1509 		mb();
1510 		writel(0, &regs->WinData);
1511 		mb();
1512 	}
1513 	writel(io, &regs->ExtIo);
1514 	mb();
1515 
1516 	eptr = rr_read_eeprom_word(rrpriv,
1517 		       offsetof(struct eeprom, rncd_info.AddrRunCodeSegs));
1518 	eptr = ((eptr & 0x1fffff) >> 3);
1519 
1520 	p2len = rr_read_eeprom_word(rrpriv, 0x83*4);
1521 	p2len = (p2len << 2);
1522 	p2size = rr_read_eeprom_word(rrpriv, 0x84*4);
1523 	p2size = ((p2size & 0x1fffff) >> 3);
1524 
1525 	if ((eptr < p2size) || (eptr > (p2size + p2len))){
1526 		printk("%s: eptr is invalid\n", dev->name);
1527 		goto out;
1528 	}
1529 
1530 	revision = rr_read_eeprom_word(rrpriv,
1531 			offsetof(struct eeprom, manf.HeaderFmt));
1532 
1533 	if (revision != 1){
1534 		printk("%s: invalid firmware format (%i)\n",
1535 		       dev->name, revision);
1536 		goto out;
1537 	}
1538 
1539 	nr_seg = rr_read_eeprom_word(rrpriv, eptr);
1540 	eptr +=4;
1541 #if (DEBUG > 1)
1542 	printk("%s: nr_seg %i\n", dev->name, nr_seg);
1543 #endif
1544 
1545 	for (i = 0; i < nr_seg; i++){
1546 		sptr = rr_read_eeprom_word(rrpriv, eptr);
1547 		eptr += 4;
1548 		len = rr_read_eeprom_word(rrpriv, eptr);
1549 		eptr += 4;
1550 		segptr = rr_read_eeprom_word(rrpriv, eptr);
1551 		segptr = ((segptr & 0x1fffff) >> 3);
1552 		eptr += 4;
1553 #if (DEBUG > 1)
1554 		printk("%s: segment %i, sram address %06x, length %04x, segptr %06x\n",
1555 		       dev->name, i, sptr, len, segptr);
1556 #endif
1557 		for (j = 0; j < len; j++){
1558 			tmp = rr_read_eeprom_word(rrpriv, segptr);
1559 			writel(sptr, &regs->WinBase);
1560 			mb();
1561 			writel(tmp, &regs->WinData);
1562 			mb();
1563 			segptr += 4;
1564 			sptr += 4;
1565 		}
1566 	}
1567 
1568 out:
1569 	writel(localctrl, &regs->LocalCtrl);
1570 	mb();
1571 	return 0;
1572 }
1573 
1574 
1575 static int rr_siocdevprivate(struct net_device *dev, struct ifreq *rq,
1576 			     void __user *data, 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_array(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(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 = memdup_user(data, EEPROM_BYTES);
1624 		if (IS_ERR(image))
1625 			return PTR_ERR(image);
1626 
1627 		oldimage = kmalloc(EEPROM_BYTES, GFP_KERNEL);
1628 		if (!oldimage) {
1629 			kfree(image);
1630 			return -ENOMEM;
1631 		}
1632 
1633 		if (rrpriv->fw_running){
1634 			printk("%s: Firmware already running\n", dev->name);
1635 			error = -EPERM;
1636 			goto wf_out;
1637 		}
1638 
1639 		printk("%s: Updating EEPROM firmware\n", dev->name);
1640 
1641 		spin_lock_irqsave(&rrpriv->lock, flags);
1642 		error = write_eeprom(rrpriv, 0, image, EEPROM_BYTES);
1643 		if (error)
1644 			printk(KERN_ERR "%s: Error writing EEPROM\n",
1645 			       dev->name);
1646 
1647 		i = rr_read_eeprom(rrpriv, 0, oldimage, EEPROM_BYTES);
1648 		spin_unlock_irqrestore(&rrpriv->lock, flags);
1649 
1650 		if (i != EEPROM_BYTES)
1651 			printk(KERN_ERR "%s: Error reading back EEPROM "
1652 			       "image\n", dev->name);
1653 
1654 		error = memcmp(image, oldimage, EEPROM_BYTES);
1655 		if (error){
1656 			printk(KERN_ERR "%s: Error verifying EEPROM image\n",
1657 			       dev->name);
1658 			error = -EFAULT;
1659 		}
1660 	wf_out:
1661 		kfree(oldimage);
1662 		kfree(image);
1663 		return error;
1664 
1665 	case SIOCRRID:
1666 		return put_user(0x52523032, (int __user *)data);
1667 	default:
1668 		return error;
1669 	}
1670 }
1671 
1672 static const struct pci_device_id rr_pci_tbl[] = {
1673 	{ PCI_VENDOR_ID_ESSENTIAL, PCI_DEVICE_ID_ESSENTIAL_ROADRUNNER,
1674 		PCI_ANY_ID, PCI_ANY_ID, },
1675 	{ 0,}
1676 };
1677 MODULE_DEVICE_TABLE(pci, rr_pci_tbl);
1678 
1679 static struct pci_driver rr_driver = {
1680 	.name		= "rrunner",
1681 	.id_table	= rr_pci_tbl,
1682 	.probe		= rr_init_one,
1683 	.remove		= rr_remove_one,
1684 };
1685 
1686 module_pci_driver(rr_driver);
1687