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