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