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