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