1/*
2 *   lanstreamer.c -- driver for the IBM Auto LANStreamer PCI Adapter
3 *
4 *  Written By: Mike Sullivan, IBM Corporation
5 *
6 *  Copyright (C) 1999 IBM Corporation
7 *
8 *  Linux driver for IBM PCI tokenring cards based on the LanStreamer MPC
9 *  chipset.
10 *
11 *  This driver is based on the olympic driver for IBM PCI TokenRing cards (Pit/Pit-Phy/Olympic
12 *  chipsets) written  by:
13 *      1999 Peter De Schrijver All Rights Reserved
14 *	1999 Mike Phillips (phillim@amtrak.com)
15 *
16 *  Base Driver Skeleton:
17 *      Written 1993-94 by Donald Becker.
18 *
19 *      Copyright 1993 United States Government as represented by the
20 *      Director, National Security Agency.
21 *
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30 * GNU General Public License for more details.
31 *
32 * NO WARRANTY
33 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
34 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
35 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
36 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
37 * solely responsible for determining the appropriateness of using and
38 * distributing the Program and assumes all risks associated with its
39 * exercise of rights under this Agreement, including but not limited to
40 * the risks and costs of program errors, damage to or loss of data,
41 * programs or equipment, and unavailability or interruption of operations.
42 *
43 * DISCLAIMER OF LIABILITY
44 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
45 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
47 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
48 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
49 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
50 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
51 *
52 * You should have received a copy of the GNU General Public License
53 * along with this program; if not, write to the Free Software
54 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
55 *
56 *
57 *  12/10/99 - Alpha Release 0.1.0
58 *            First release to the public
59 *  03/03/00 - Merged to kernel, indented -kr -i8 -bri0, fixed some missing
60 *		malloc free checks, reviewed code. <alan@redhat.com>
61 *  03/13/00 - Added spinlocks for smp
62 *  03/08/01 - Added support for module_init() and module_exit()
63 *  08/15/01 - Added ioctl() functionality for debugging, changed netif_*_queue
64 *             calls and other incorrectness - Kent Yoder <yoder1@us.ibm.com>
65 *  11/05/01 - Restructured the interrupt function, added delays, reduced the
66 *             the number of TX descriptors to 1, which together can prevent
67 *             the card from locking up the box - <yoder1@us.ibm.com>
68 *  09/27/02 - New PCI interface + bug fix. - <yoder1@us.ibm.com>
69 *
70 *  To Do:
71 *
72 *
73 *  If Problems do Occur
74 *  Most problems can be rectified by either closing and opening the interface
75 *  (ifconfig down and up) or rmmod and insmod'ing the driver (a bit difficult
76 *  if compiled into the kernel).
77 */
78
79/* Change STREAMER_DEBUG to 1 to get verbose, and I mean really verbose, messages */
80
81#define STREAMER_DEBUG 0
82#define STREAMER_DEBUG_PACKETS 0
83
84/* Change STREAMER_NETWORK_MONITOR to receive mac frames through the arb channel.
85 * Will also create a /proc/net/streamer_tr entry if proc_fs is compiled into the
86 * kernel.
87 * Intended to be used to create a ring-error reporting network module
88 * i.e. it will give you the source address of beaconers on the ring
89 */
90
91#define STREAMER_NETWORK_MONITOR 0
92
93/* #define CONFIG_PROC_FS */
94
95/*
96 *  Allow or disallow ioctl's for debugging
97 */
98
99#define STREAMER_IOCTL 0
100
101#include <linux/config.h>
102#include <linux/module.h>
103
104#include <linux/kernel.h>
105#include <linux/sched.h>
106#include <linux/errno.h>
107#include <linux/timer.h>
108#include <linux/in.h>
109#include <linux/ioport.h>
110#include <linux/string.h>
111#include <linux/proc_fs.h>
112#include <linux/ptrace.h>
113#include <linux/skbuff.h>
114#include <linux/interrupt.h>
115#include <linux/delay.h>
116#include <linux/netdevice.h>
117#include <linux/trdevice.h>
118#include <linux/stddef.h>
119#include <linux/init.h>
120#include <linux/pci.h>
121#include <linux/spinlock.h>
122#include <linux/version.h>
123#include <net/checksum.h>
124
125#include <asm/io.h>
126#include <asm/system.h>
127#include <asm/bitops.h>
128
129#include "lanstreamer.h"
130
131/* I've got to put some intelligence into the version number so that Peter and I know
132 * which version of the code somebody has got.
133 * Version Number = a.b.c.d  where a.b.c is the level of code and d is the latest author.
134 * So 0.0.1.pds = Peter, 0.0.1.mlp = Mike
135 *
136 * Official releases will only have an a.b.c version number format.
137 */
138
139static char version[] = "LanStreamer.c v0.4.0 03/08/01 - Mike Sullivan\n"
140                        "              v0.5.2 09/30/02 - Kent Yoder";
141
142static struct pci_device_id streamer_pci_tbl[] __initdata = {
143	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_TR, PCI_ANY_ID, PCI_ANY_ID,},
144	{}	/* terminating entry */
145};
146MODULE_DEVICE_TABLE(pci,streamer_pci_tbl);
147
148
149static char *open_maj_error[] = {
150	"No error", "Lobe Media Test", "Physical Insertion",
151	"Address Verification", "Neighbor Notification (Ring Poll)",
152	"Request Parameters", "FDX Registration Request",
153	"FDX Lobe Media Test", "FDX Duplicate Address Check",
154	"Unknown stage"
155};
156
157static char *open_min_error[] = {
158	"No error", "Function Failure", "Signal Lost", "Wire Fault",
159	"Ring Speed Mismatch", "Timeout", "Ring Failure", "Ring Beaconing",
160	"Duplicate Node Address", "Request Parameters", "Remove Received",
161	"Reserved", "Reserved", "No Monitor Detected for RPL",
162	"Monitor Contention failer for RPL", "FDX Protocol Error"
163};
164
165/* Module paramters */
166
167/* Ring Speed 0,4,16
168 * 0 = Autosense
169 * 4,16 = Selected speed only, no autosense
170 * This allows the card to be the first on the ring
171 * and become the active monitor.
172 *
173 * WARNING: Some hubs will allow you to insert
174 * at the wrong speed
175 */
176
177static int ringspeed[STREAMER_MAX_ADAPTERS] = { 0, };
178
179MODULE_PARM(ringspeed, "1-" __MODULE_STRING(STREAMER_MAX_ADAPTERS) "i");
180
181/* Packet buffer size */
182
183static int pkt_buf_sz[STREAMER_MAX_ADAPTERS] = { 0, };
184
185MODULE_PARM(pkt_buf_sz, "1-" __MODULE_STRING(STREAMER_MAX_ADAPTERS) "i");
186
187/* Message Level */
188
189static int message_level[STREAMER_MAX_ADAPTERS] = { 1, };
190
191MODULE_PARM(message_level,
192	    "1-" __MODULE_STRING(STREAMER_MAX_ADAPTERS) "i");
193
194#if STREAMER_IOCTL
195static int streamer_ioctl(struct net_device *, struct ifreq *, int);
196#endif
197
198static int streamer_reset(struct net_device *dev);
199static int streamer_open(struct net_device *dev);
200static int streamer_xmit(struct sk_buff *skb, struct net_device *dev);
201static int streamer_close(struct net_device *dev);
202static void streamer_set_rx_mode(struct net_device *dev);
203static void streamer_interrupt(int irq, void *dev_id,
204			       struct pt_regs *regs);
205static struct net_device_stats *streamer_get_stats(struct net_device *dev);
206static int streamer_set_mac_address(struct net_device *dev, void *addr);
207static void streamer_arb_cmd(struct net_device *dev);
208static int streamer_change_mtu(struct net_device *dev, int mtu);
209static void streamer_srb_bh(struct net_device *dev);
210static void streamer_asb_bh(struct net_device *dev);
211#if STREAMER_NETWORK_MONITOR
212#ifdef CONFIG_PROC_FS
213static int streamer_proc_info(char *buffer, char **start, off_t offset,
214			      int length, int *eof, void *data);
215static int sprintf_info(char *buffer, struct net_device *dev);
216struct streamer_private *dev_streamer=NULL;
217#endif
218#endif
219
220static int __devinit streamer_init_one(struct pci_dev *pdev,
221				       const struct pci_device_id *ent)
222{
223  struct net_device *dev=NULL;
224	struct streamer_private *streamer_priv;
225  __u32 pio_start, pio_end, pio_flags, pio_len;
226  __u32 mmio_start, mmio_end, mmio_flags, mmio_len;
227  int rc=0;
228  static int card_no=-1;
229  u16 pcr;
230  u8 cls = 0;
231
232#if STREAMER_DEBUG
233  printk("lanstreamer::streamer_init_one, entry pdev %p\n",pdev);
234#endif
235
236				card_no++;
237  dev=init_trdev(dev, sizeof(*streamer_priv));
238  if(dev==NULL) {
239				printk(KERN_ERR "lanstreamer: out of memory.\n");
240    return -ENOMEM;
241			}
242			SET_MODULE_OWNER(dev);
243  streamer_priv=dev->priv;
244
245#if STREAMER_NETWORK_MONITOR
246#ifdef CONFIG_PROC_FS
247  if (!dev_streamer) {
248    create_proc_read_entry("net/streamer_tr",0,0,streamer_proc_info,NULL);
249  }
250  streamer_priv->next=dev_streamer;
251  dev_streamer=streamer_priv;
252#endif
253#endif
254
255  if(pci_set_dma_mask(pdev, 0xFFFFFFFF)) {
256    printk(KERN_ERR "%s: No suitable PCI mapping available.\n", dev->name);
257    rc = -ENODEV;
258    goto err_out;
259  }
260
261  if (pci_enable_device(pdev)) {
262    printk(KERN_ERR "lanstreamer: unable to enable pci device\n");
263    rc=-EIO;
264    goto err_out;
265  }
266
267  pci_set_master(pdev);
268
269  pio_start = pci_resource_start(pdev, 0);
270  pio_end = pci_resource_end(pdev, 0);
271  pio_flags = pci_resource_flags(pdev, 0);
272  pio_len = pci_resource_len(pdev, 0);
273
274  mmio_start = pci_resource_start(pdev, 1);
275  mmio_end = pci_resource_end(pdev, 1);
276  mmio_flags = pci_resource_flags(pdev, 1);
277  mmio_len = pci_resource_len(pdev, 1);
278
279#if STREAMER_DEBUG
280  printk("lanstreamer: pio_start %x pio_end %x pio_len %x pio_flags %x\n",
281	 pio_start, pio_end, pio_len, pio_flags);
282  printk("lanstreamer: mmio_start %x mmio_end %x mmio_len %x mmio_flags %x\n",
283	 mmio_start, mmio_end, mmio_flags, mmio_len);
284#endif
285
286  if (!request_region(pio_start, pio_len, "lanstreamer")) {
287    printk(KERN_ERR "lanstreamer: unable to get pci io addr %x\n",pio_start);
288    rc= -EBUSY;
289    goto err_out;
290  }
291
292  if (!request_mem_region(mmio_start, mmio_len, "lanstreamer")) {
293    printk(KERN_ERR "lanstreamer: unable to get pci mmio addr %x\n",mmio_start);
294    rc= -EBUSY;
295    goto err_out_free_pio;
296  }
297
298  streamer_priv->streamer_mmio=ioremap(mmio_start, mmio_len);
299  if (streamer_priv->streamer_mmio == NULL) {
300    printk(KERN_ERR "lanstreamer: unable to remap MMIO %x\n",mmio_start);
301    rc= -EIO;
302    goto err_out_free_mmio;
303			}
304
305  init_waitqueue_head(&streamer_priv->srb_wait);
306  init_waitqueue_head(&streamer_priv->trb_wait);
307
308			dev->open = &streamer_open;
309			dev->hard_start_xmit = &streamer_xmit;
310			dev->change_mtu = &streamer_change_mtu;
311			dev->stop = &streamer_close;
312#if STREAMER_IOCTL
313			dev->do_ioctl = &streamer_ioctl;
314#else
315			dev->do_ioctl = NULL;
316#endif
317			dev->set_multicast_list = &streamer_set_rx_mode;
318			dev->get_stats = &streamer_get_stats;
319			dev->set_mac_address = &streamer_set_mac_address;
320  dev->irq = pdev->irq;
321  dev->base_addr=pio_start;
322
323  streamer_priv->streamer_card_name = (char *)pdev->resource[0].name;
324  streamer_priv->pci_dev=pdev;
325
326  if ((pkt_buf_sz[card_no] < 100) || (pkt_buf_sz[card_no] > 18000))
327    streamer_priv->pkt_buf_sz = PKT_BUF_SZ;
328  else
329    streamer_priv->pkt_buf_sz = pkt_buf_sz[card_no];
330
331  streamer_priv->streamer_ring_speed = ringspeed[card_no];
332  streamer_priv->streamer_message_level = message_level[card_no];
333
334  pci_set_drvdata(pdev, dev);
335
336  spin_lock_init(&streamer_priv->streamer_lock);
337
338  pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &cls);
339  cls <<= 2;
340  if (cls != SMP_CACHE_BYTES) {
341         printk(KERN_INFO "  PCI cache line size set incorrectly "
342                "(%i bytes) by BIOS/FW, ", cls);
343         if (cls > SMP_CACHE_BYTES)
344                printk("expecting %i\n", SMP_CACHE_BYTES);
345         else {
346                printk("correcting to %i\n", SMP_CACHE_BYTES);
347                pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
348                                      SMP_CACHE_BYTES >> 2);
349         }
350  }
351
352  pci_read_config_word (pdev, PCI_COMMAND, &pcr);
353
354  pcr |= (PCI_COMMAND_INVALIDATE | PCI_COMMAND_SERR);
355
356  pci_write_config_word (pdev, PCI_COMMAND, pcr);
357  pci_read_config_word (pdev, PCI_COMMAND, &pcr);
358
359  printk("%s \n", version);
360  printk("%s: %s. I/O at %hx, MMIO at %p, using irq %d\n",dev->name,
361	 streamer_priv->streamer_card_name,
362	 (unsigned int) dev->base_addr,
363	 streamer_priv->streamer_mmio,
364	 dev->irq);
365
366  if (!streamer_reset(dev)) {
367    return 0;
368  }
369
370  iounmap(streamer_priv->streamer_mmio);
371err_out_free_mmio:
372  release_mem_region(mmio_start, mmio_len);
373err_out_free_pio:
374  release_region(pio_start, pio_len);
375err_out:
376  unregister_trdev(dev);
377  kfree(dev);
378#if STREAMER_DEBUG
379  printk("lanstreamer: Exit error %x\n",rc);
380#endif
381  return rc;
382}
383
384static void __devexit streamer_remove_one(struct pci_dev *pdev) {
385  struct net_device *dev=pci_get_drvdata(pdev);
386  struct streamer_private *streamer_priv;
387
388#if STREAMER_DEBUG
389  printk("lanstreamer::streamer_remove_one entry pdev %p\n",pdev);
390#endif
391
392  if (dev == NULL) {
393    printk(KERN_ERR "lanstreamer::streamer_remove_one, ERROR dev is NULL\n");
394    return;
395		}
396
397  streamer_priv=dev->priv;
398  if (streamer_priv == NULL) {
399    printk(KERN_ERR "lanstreamer::streamer_remove_one, ERROR dev->priv is NULL\n");
400    return;
401	}
402
403#if STREAMER_NETWORK_MONITOR
404#ifdef CONFIG_PROC_FS
405  {
406    struct streamer_private *slast;
407    struct streamer_private *scurrent;
408    if (streamer_priv == dev_streamer) {
409      dev_streamer=dev_streamer->next;
410    } else {
411      for(slast=scurrent=dev_streamer; dev_streamer; slast=scurrent, scurrent=scurrent->next) {
412	if (scurrent == streamer_priv) {
413	  slast->next=scurrent->next;
414	  break;
415	}
416      }
417    }
418    if (!dev_streamer) {
419      remove_proc_entry("net/streamer_tr", NULL);
420    }
421  }
422#endif
423#endif
424
425  unregister_trdev(dev);
426  release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev,0));
427  release_mem_region(pci_resource_start(pdev, 1), pci_resource_len(pdev,1));
428  kfree(dev);
429  pci_set_drvdata(pdev, NULL);
430}
431
432
433static int streamer_reset(struct net_device *dev)
434{
435	struct streamer_private *streamer_priv;
436	__u8 *streamer_mmio;
437	unsigned long t;
438	unsigned int uaa_addr;
439	struct sk_buff *skb = 0;
440	__u16 misr;
441
442	streamer_priv = (struct streamer_private *) dev->priv;
443	streamer_mmio = streamer_priv->streamer_mmio;
444
445	writew(readw(streamer_mmio + BCTL) | BCTL_SOFTRESET, streamer_mmio + BCTL);
446	t = jiffies;
447	/* Hold soft reset bit for a while */
448	current->state = TASK_UNINTERRUPTIBLE;
449	schedule_timeout(HZ);
450
451	writew(readw(streamer_mmio + BCTL) & ~BCTL_SOFTRESET,
452	       streamer_mmio + BCTL);
453
454#if STREAMER_DEBUG
455	printk("BCTL: %x\n", readw(streamer_mmio + BCTL));
456	printk("GPR: %x\n", readw(streamer_mmio + GPR));
457	printk("SISRMASK: %x\n", readw(streamer_mmio + SISR_MASK));
458#endif
459	writew(readw(streamer_mmio + BCTL) | (BCTL_RX_FIFO_8 | BCTL_TX_FIFO_8), streamer_mmio + BCTL );
460
461	if (streamer_priv->streamer_ring_speed == 0) {	/* Autosense */
462		writew(readw(streamer_mmio + GPR) | GPR_AUTOSENSE,
463		       streamer_mmio + GPR);
464		if (streamer_priv->streamer_message_level)
465			printk(KERN_INFO "%s: Ringspeed autosense mode on\n",
466			       dev->name);
467	} else if (streamer_priv->streamer_ring_speed == 16) {
468		if (streamer_priv->streamer_message_level)
469			printk(KERN_INFO "%s: Trying to open at 16 Mbps as requested\n",
470			       dev->name);
471		writew(GPR_16MBPS, streamer_mmio + GPR);
472	} else if (streamer_priv->streamer_ring_speed == 4) {
473		if (streamer_priv->streamer_message_level)
474			printk(KERN_INFO "%s: Trying to open at 4 Mbps as requested\n",
475			       dev->name);
476		writew(0, streamer_mmio + GPR);
477	}
478
479	skb = dev_alloc_skb(streamer_priv->pkt_buf_sz);
480	if (!skb) {
481		printk(KERN_INFO "%s: skb allocation for diagnostics failed...proceeding\n",
482		       dev->name);
483	} else {
484	        struct streamer_rx_desc *rx_ring;
485                u8 *data;
486
487		rx_ring=(struct streamer_rx_desc *)skb->data;
488		data=((u8 *)skb->data)+sizeof(struct streamer_rx_desc);
489		rx_ring->forward=0;
490		rx_ring->status=0;
491		rx_ring->buffer=cpu_to_le32(pci_map_single(streamer_priv->pci_dev, data,
492							512, PCI_DMA_FROMDEVICE));
493		rx_ring->framelen_buflen=512;
494		writel(cpu_to_le32(pci_map_single(streamer_priv->pci_dev, rx_ring, 512, PCI_DMA_FROMDEVICE)),
495			streamer_mmio+RXBDA);
496	}
497
498#if STREAMER_DEBUG
499	printk("GPR = %x\n", readw(streamer_mmio + GPR));
500#endif
501	/* start solo init */
502	writew(SISR_MI, streamer_mmio + SISR_MASK_SUM);
503
504	while (!((readw(streamer_mmio + SISR)) & SISR_SRB_REPLY)) {
505		current->state = TASK_INTERRUPTIBLE;
506		schedule_timeout(HZ/10);
507		if (jiffies - t > 40 * HZ) {
508			printk(KERN_ERR
509			       "IBM PCI tokenring card not responding\n");
510			release_region(dev->base_addr, STREAMER_IO_SPACE);
511			if (skb)
512				dev_kfree_skb(skb);
513			return -1;
514		}
515	}
516	writew(~SISR_SRB_REPLY, streamer_mmio + SISR_RUM);
517	misr = readw(streamer_mmio + MISR_RUM);
518	writew(~misr, streamer_mmio + MISR_RUM);
519
520	if (skb)
521		dev_kfree_skb(skb);	/* release skb used for diagnostics */
522
523#if STREAMER_DEBUG
524	printk("LAPWWO: %x, LAPA: %x LAPE:  %x\n",
525	       readw(streamer_mmio + LAPWWO), readw(streamer_mmio + LAPA),
526	       readw(streamer_mmio + LAPE));
527#endif
528
529#if STREAMER_DEBUG
530	{
531		int i;
532		writew(readw(streamer_mmio + LAPWWO),
533		       streamer_mmio + LAPA);
534		printk("initialization response srb dump: ");
535		for (i = 0; i < 10; i++)
536			printk("%x:",
537			       ntohs(readw(streamer_mmio + LAPDINC)));
538		printk("\n");
539	}
540#endif
541
542	writew(readw(streamer_mmio + LAPWWO) + 6, streamer_mmio + LAPA);
543	if (readw(streamer_mmio + LAPD)) {
544		printk(KERN_INFO "tokenring card intialization failed. errorcode : %x\n",
545		       ntohs(readw(streamer_mmio + LAPD)));
546		release_region(dev->base_addr, STREAMER_IO_SPACE);
547		return -1;
548	}
549
550	writew(readw(streamer_mmio + LAPWWO) + 8, streamer_mmio + LAPA);
551	uaa_addr = ntohs(readw(streamer_mmio + LAPDINC));
552	readw(streamer_mmio + LAPDINC);	/* skip over Level.Addr field */
553	streamer_priv->streamer_addr_table_addr = ntohs(readw(streamer_mmio + LAPDINC));
554	streamer_priv->streamer_parms_addr = ntohs(readw(streamer_mmio + LAPDINC));
555
556#if STREAMER_DEBUG
557	printk("UAA resides at %x\n", uaa_addr);
558#endif
559
560	/* setup uaa area for access with LAPD */
561	{
562		int i;
563		__u16 addr;
564		writew(uaa_addr, streamer_mmio + LAPA);
565		for (i = 0; i < 6; i += 2) {
566		        addr=ntohs(readw(streamer_mmio+LAPDINC));
567			dev->dev_addr[i]= (addr >> 8) & 0xff;
568			dev->dev_addr[i+1]= addr & 0xff;
569		}
570#if STREAMER_DEBUG
571		printk("Adapter address: ");
572		for (i = 0; i < 6; i++) {
573			printk("%02x:", dev->dev_addr[i]);
574		}
575		printk("\n");
576#endif
577	}
578	return 0;
579}
580
581static int streamer_open(struct net_device *dev)
582{
583	struct streamer_private *streamer_priv = (struct streamer_private *) dev->priv;
584	__u8 *streamer_mmio = streamer_priv->streamer_mmio;
585	unsigned long flags;
586	char open_error[255];
587	int i, open_finished = 1;
588	__u16 srb_word;
589	__u16 srb_open;
590	int rc;
591
592	if (readw(streamer_mmio+BMCTL_SUM) & BMCTL_RX_ENABLED) {
593	        rc=streamer_reset(dev);
594	}
595
596	if (request_irq(dev->irq, &streamer_interrupt, SA_SHIRQ, "lanstreamer", dev)) {
597		return -EAGAIN;
598	}
599#if STREAMER_DEBUG
600	printk("BMCTL: %x\n", readw(streamer_mmio + BMCTL_SUM));
601	printk("pending ints: %x\n", readw(streamer_mmio + SISR));
602#endif
603
604	writew(SISR_MI | SISR_SRB_REPLY, streamer_mmio + SISR_MASK);	/* more ints later, doesn't stop arb cmd interrupt */
605	writew(LISR_LIE, streamer_mmio + LISR);	/* more ints later */
606
607	/* adapter is closed, so SRB is pointed to by LAPWWO */
608	writew(readw(streamer_mmio + LAPWWO), streamer_mmio + LAPA);
609
610#if STREAMER_DEBUG
611	printk("LAPWWO: %x, LAPA: %x\n", readw(streamer_mmio + LAPWWO),
612	       readw(streamer_mmio + LAPA));
613	printk("LAPE: %x\n", readw(streamer_mmio + LAPE));
614	printk("SISR Mask = %04x\n", readw(streamer_mmio + SISR_MASK));
615#endif
616	do {
617		int i;
618
619		for (i = 0; i < SRB_COMMAND_SIZE; i += 2) {
620			writew(0, streamer_mmio + LAPDINC);
621		}
622
623		writew(readw(streamer_mmio+LAPWWO),streamer_mmio+LAPA);
624		writew(htons(SRB_OPEN_ADAPTER<<8),streamer_mmio+LAPDINC) ; 	/* open */
625		writew(htons(STREAMER_CLEAR_RET_CODE<<8),streamer_mmio+LAPDINC);
626		writew(STREAMER_CLEAR_RET_CODE, streamer_mmio + LAPDINC);
627
628		writew(readw(streamer_mmio + LAPWWO) + 8, streamer_mmio + LAPA);
629#if STREAMER_NETWORK_MONITOR
630		/* If Network Monitor, instruct card to copy MAC frames through the ARB */
631		writew(htons(OPEN_ADAPTER_ENABLE_FDX | OPEN_ADAPTER_PASS_ADC_MAC | OPEN_ADAPTER_PASS_ATT_MAC | OPEN_ADAPTER_PASS_BEACON), streamer_mmio + LAPDINC);	/* offset 8 word contains open options */
632#else
633		writew(htons(OPEN_ADAPTER_ENABLE_FDX), streamer_mmio + LAPDINC);	/* Offset 8 word contains Open.Options */
634#endif
635
636		if (streamer_priv->streamer_laa[0]) {
637			writew(readw(streamer_mmio + LAPWWO) + 12, streamer_mmio + LAPA);
638			writew(htons((streamer_priv->streamer_laa[0] << 8) |
639				     streamer_priv->streamer_laa[1]),streamer_mmio+LAPDINC);
640			writew(htons((streamer_priv->streamer_laa[2] << 8) |
641				     streamer_priv->streamer_laa[3]),streamer_mmio+LAPDINC);
642			writew(htons((streamer_priv->streamer_laa[4] << 8) |
643				     streamer_priv->streamer_laa[5]),streamer_mmio+LAPDINC);
644			memcpy(dev->dev_addr, streamer_priv->streamer_laa, dev->addr_len);
645		}
646
647		/* save off srb open offset */
648		srb_open = readw(streamer_mmio + LAPWWO);
649#if STREAMER_DEBUG
650		writew(readw(streamer_mmio + LAPWWO),
651		       streamer_mmio + LAPA);
652		printk("srb open request: \n");
653		for (i = 0; i < 16; i++) {
654			printk("%x:", ntohs(readw(streamer_mmio + LAPDINC)));
655		}
656		printk("\n");
657#endif
658		spin_lock_irqsave(&streamer_priv->streamer_lock, flags);
659		streamer_priv->srb_queued = 1;
660
661		/* signal solo that SRB command has been issued */
662		writew(LISR_SRB_CMD, streamer_mmio + LISR_SUM);
663		spin_unlock_irqrestore(&streamer_priv->streamer_lock, flags);
664
665		while (streamer_priv->srb_queued) {
666			interruptible_sleep_on_timeout(&streamer_priv->srb_wait, 5 * HZ);
667			if (signal_pending(current)) {
668				printk(KERN_WARNING "%s: SRB timed out.\n", dev->name);
669				printk(KERN_WARNING "SISR=%x MISR=%x, LISR=%x\n",
670				       readw(streamer_mmio + SISR),
671				       readw(streamer_mmio + MISR_RUM),
672				       readw(streamer_mmio + LISR));
673				streamer_priv->srb_queued = 0;
674				break;
675			}
676		}
677
678#if STREAMER_DEBUG
679		printk("SISR_MASK: %x\n", readw(streamer_mmio + SISR_MASK));
680		printk("srb open response:\n");
681		writew(srb_open, streamer_mmio + LAPA);
682		for (i = 0; i < 10; i++) {
683			printk("%x:",
684			       ntohs(readw(streamer_mmio + LAPDINC)));
685		}
686#endif
687
688		/* If we get the same return response as we set, the interrupt wasn't raised and the open
689		 * timed out.
690		 */
691		writew(srb_open + 2, streamer_mmio + LAPA);
692		srb_word = ntohs(readw(streamer_mmio + LAPD)) >> 8;
693		if (srb_word == STREAMER_CLEAR_RET_CODE) {
694			printk(KERN_WARNING "%s: Adapter Open time out or error.\n",
695			       dev->name);
696			return -EIO;
697		}
698
699		if (srb_word != 0) {
700			if (srb_word == 0x07) {
701				if (!streamer_priv->streamer_ring_speed && open_finished) {	/* Autosense , first time around */
702					printk(KERN_WARNING "%s: Retrying at different ring speed \n",
703					       dev->name);
704					open_finished = 0;
705				} else {
706					__u16 error_code;
707
708					writew(srb_open + 6, streamer_mmio + LAPA);
709					error_code = ntohs(readw(streamer_mmio + LAPD));
710					strcpy(open_error, open_maj_error[(error_code & 0xf0) >> 4]);
711					strcat(open_error, " - ");
712					strcat(open_error, open_min_error[(error_code & 0x0f)]);
713
714					if (!streamer_priv->streamer_ring_speed
715					    && ((error_code & 0x0f) == 0x0d))
716					{
717						printk(KERN_WARNING "%s: Tried to autosense ring speed with no monitors present\n", dev->name);
718						printk(KERN_WARNING "%s: Please try again with a specified ring speed \n", dev->name);
719						free_irq(dev->irq, dev);
720						return -EIO;
721					}
722
723					printk(KERN_WARNING "%s: %s\n",
724					       dev->name, open_error);
725					free_irq(dev->irq, dev);
726					return -EIO;
727
728				}	/* if autosense && open_finished */
729			} else {
730				printk(KERN_WARNING "%s: Bad OPEN response: %x\n",
731				       dev->name, srb_word);
732				free_irq(dev->irq, dev);
733				return -EIO;
734			}
735		} else
736			open_finished = 1;
737	} while (!(open_finished));	/* Will only loop if ring speed mismatch re-open attempted && autosense is on */
738
739	writew(srb_open + 18, streamer_mmio + LAPA);
740	srb_word=ntohs(readw(streamer_mmio+LAPD)) >> 8;
741	if (srb_word & (1 << 3))
742		if (streamer_priv->streamer_message_level)
743			printk(KERN_INFO "%s: Opened in FDX Mode\n", dev->name);
744
745	if (srb_word & 1)
746		streamer_priv->streamer_ring_speed = 16;
747	else
748		streamer_priv->streamer_ring_speed = 4;
749
750	if (streamer_priv->streamer_message_level)
751		printk(KERN_INFO "%s: Opened in %d Mbps mode\n",
752			dev->name,
753			streamer_priv->streamer_ring_speed);
754
755	writew(srb_open + 8, streamer_mmio + LAPA);
756	streamer_priv->asb = ntohs(readw(streamer_mmio + LAPDINC));
757	streamer_priv->srb = ntohs(readw(streamer_mmio + LAPDINC));
758	streamer_priv->arb = ntohs(readw(streamer_mmio + LAPDINC));
759	readw(streamer_mmio + LAPDINC);	/* offset 14 word is rsvd */
760	streamer_priv->trb = ntohs(readw(streamer_mmio + LAPDINC));
761
762	streamer_priv->streamer_receive_options = 0x00;
763	streamer_priv->streamer_copy_all_options = 0;
764
765	/* setup rx ring */
766	/* enable rx channel */
767	writew(~BMCTL_RX_DIS, streamer_mmio + BMCTL_RUM);
768
769	/* setup rx descriptors */
770	streamer_priv->streamer_rx_ring=
771	    kmalloc( sizeof(struct streamer_rx_desc)*
772		     STREAMER_RX_RING_SIZE,GFP_KERNEL);
773	if (!streamer_priv->streamer_rx_ring) {
774	    printk(KERN_WARNING "%s ALLOC of streamer rx ring FAILED!!\n",dev->name);
775	    return -EIO;
776	}
777
778	for (i = 0; i < STREAMER_RX_RING_SIZE; i++) {
779		struct sk_buff *skb;
780
781		skb = dev_alloc_skb(streamer_priv->pkt_buf_sz);
782		if (skb == NULL)
783			break;
784
785		skb->dev = dev;
786
787		streamer_priv->streamer_rx_ring[i].forward =
788			cpu_to_le32(pci_map_single(streamer_priv->pci_dev, &streamer_priv->streamer_rx_ring[i + 1],
789					sizeof(struct streamer_rx_desc), PCI_DMA_FROMDEVICE));
790		streamer_priv->streamer_rx_ring[i].status = 0;
791		streamer_priv->streamer_rx_ring[i].buffer =
792			cpu_to_le32(pci_map_single(streamer_priv->pci_dev, skb->data,
793					      streamer_priv->pkt_buf_sz, PCI_DMA_FROMDEVICE));
794		streamer_priv->streamer_rx_ring[i].framelen_buflen = streamer_priv->pkt_buf_sz;
795		streamer_priv->rx_ring_skb[i] = skb;
796	}
797	streamer_priv->streamer_rx_ring[STREAMER_RX_RING_SIZE - 1].forward =
798				cpu_to_le32(pci_map_single(streamer_priv->pci_dev, &streamer_priv->streamer_rx_ring[0],
799						sizeof(struct streamer_rx_desc), PCI_DMA_FROMDEVICE));
800
801	if (i == 0) {
802		printk(KERN_WARNING "%s: Not enough memory to allocate rx buffers. Adapter disabled\n", dev->name);
803		free_irq(dev->irq, dev);
804		return -EIO;
805	}
806
807	streamer_priv->rx_ring_last_received = STREAMER_RX_RING_SIZE - 1;	/* last processed rx status */
808
809	writel(cpu_to_le32(pci_map_single(streamer_priv->pci_dev, &streamer_priv->streamer_rx_ring[0],
810				sizeof(struct streamer_rx_desc), PCI_DMA_TODEVICE)),
811		streamer_mmio + RXBDA);
812	writel(cpu_to_le32(pci_map_single(streamer_priv->pci_dev, &streamer_priv->streamer_rx_ring[STREAMER_RX_RING_SIZE - 1],
813				sizeof(struct streamer_rx_desc), PCI_DMA_TODEVICE)),
814		streamer_mmio + RXLBDA);
815
816	/* set bus master interrupt event mask */
817	writew(MISR_RX_NOBUF | MISR_RX_EOF, streamer_mmio + MISR_MASK);
818
819
820	/* setup tx ring */
821	streamer_priv->streamer_tx_ring=kmalloc(sizeof(struct streamer_tx_desc)*
822						STREAMER_TX_RING_SIZE,GFP_KERNEL);
823	if (!streamer_priv->streamer_tx_ring) {
824	    printk(KERN_WARNING "%s ALLOC of streamer_tx_ring FAILED\n",dev->name);
825	    return -EIO;
826	}
827
828	writew(~BMCTL_TX2_DIS, streamer_mmio + BMCTL_RUM);	/* Enables TX channel 2 */
829	for (i = 0; i < STREAMER_TX_RING_SIZE; i++) {
830		streamer_priv->streamer_tx_ring[i].forward = cpu_to_le32(pci_map_single(streamer_priv->pci_dev,
831										&streamer_priv->streamer_tx_ring[i + 1],
832										sizeof(struct streamer_tx_desc),
833										PCI_DMA_TODEVICE));
834		streamer_priv->streamer_tx_ring[i].status = 0;
835		streamer_priv->streamer_tx_ring[i].bufcnt_framelen = 0;
836		streamer_priv->streamer_tx_ring[i].buffer = 0;
837		streamer_priv->streamer_tx_ring[i].buflen = 0;
838		streamer_priv->streamer_tx_ring[i].rsvd1 = 0;
839		streamer_priv->streamer_tx_ring[i].rsvd2 = 0;
840		streamer_priv->streamer_tx_ring[i].rsvd3 = 0;
841	}
842	streamer_priv->streamer_tx_ring[STREAMER_TX_RING_SIZE - 1].forward =
843					cpu_to_le32(pci_map_single(streamer_priv->pci_dev, &streamer_priv->streamer_tx_ring[0],
844							sizeof(struct streamer_tx_desc), PCI_DMA_TODEVICE));
845
846	streamer_priv->free_tx_ring_entries = STREAMER_TX_RING_SIZE;
847	streamer_priv->tx_ring_free = 0;	/* next entry in tx ring to use */
848	streamer_priv->tx_ring_last_status = STREAMER_TX_RING_SIZE - 1;
849
850	/* set Busmaster interrupt event mask (handle receives on interrupt only */
851	writew(MISR_TX2_EOF | MISR_RX_NOBUF | MISR_RX_EOF, streamer_mmio + MISR_MASK);
852	/* set system event interrupt mask */
853	writew(SISR_ADAPTER_CHECK | SISR_ARB_CMD | SISR_TRB_REPLY | SISR_ASB_FREE, streamer_mmio + SISR_MASK_SUM);
854
855#if STREAMER_DEBUG
856	printk("BMCTL: %x\n", readw(streamer_mmio + BMCTL_SUM));
857	printk("SISR MASK: %x\n", readw(streamer_mmio + SISR_MASK));
858#endif
859
860#if STREAMER_NETWORK_MONITOR
861
862	writew(streamer_priv->streamer_addr_table_addr, streamer_mmio + LAPA);
863	printk("%s: Node Address: %04x:%04x:%04x\n", dev->name,
864		ntohs(readw(streamer_mmio + LAPDINC)),
865		ntohs(readw(streamer_mmio + LAPDINC)),
866		ntohs(readw(streamer_mmio + LAPDINC)));
867	readw(streamer_mmio + LAPDINC);
868	readw(streamer_mmio + LAPDINC);
869	printk("%s: Functional Address: %04x:%04x\n", dev->name,
870		ntohs(readw(streamer_mmio + LAPDINC)),
871		ntohs(readw(streamer_mmio + LAPDINC)));
872
873	writew(streamer_priv->streamer_parms_addr + 4,
874		streamer_mmio + LAPA);
875	printk("%s: NAUN Address: %04x:%04x:%04x\n", dev->name,
876		ntohs(readw(streamer_mmio + LAPDINC)),
877		ntohs(readw(streamer_mmio + LAPDINC)),
878		ntohs(readw(streamer_mmio + LAPDINC)));
879#endif
880
881	netif_start_queue(dev);
882	return 0;
883}
884
885/*
886 *	When we enter the rx routine we do not know how many frames have been
887 *	queued on the rx channel.  Therefore we start at the next rx status
888 *	position and travel around the receive ring until we have completed
889 *	all the frames.
890 *
891 *	This means that we may process the frame before we receive the end
892 *	of frame interrupt. This is why we always test the status instead
893 *	of blindly processing the next frame.
894 *
895 */
896static void streamer_rx(struct net_device *dev)
897{
898	struct streamer_private *streamer_priv =
899	    (struct streamer_private *) dev->priv;
900	__u8 *streamer_mmio = streamer_priv->streamer_mmio;
901	struct streamer_rx_desc *rx_desc;
902	int rx_ring_last_received, length, frame_length, buffer_cnt = 0;
903	struct sk_buff *skb, *skb2;
904
905	/* setup the next rx descriptor to be received */
906	rx_desc = &streamer_priv->streamer_rx_ring[(streamer_priv->rx_ring_last_received + 1) & (STREAMER_RX_RING_SIZE - 1)];
907	rx_ring_last_received = streamer_priv->rx_ring_last_received;
908
909	while (rx_desc->status & 0x01000000) {	/* While processed descriptors are available */
910		if (rx_ring_last_received != streamer_priv->rx_ring_last_received)
911		{
912			printk(KERN_WARNING "RX Error 1 rx_ring_last_received not the same %x %x\n",
913				rx_ring_last_received, streamer_priv->rx_ring_last_received);
914		}
915		streamer_priv->rx_ring_last_received = (streamer_priv->rx_ring_last_received + 1) & (STREAMER_RX_RING_SIZE - 1);
916		rx_ring_last_received = streamer_priv->rx_ring_last_received;
917
918		length = rx_desc->framelen_buflen & 0xffff;	/* buffer length */
919		frame_length = (rx_desc->framelen_buflen >> 16) & 0xffff;
920
921		if (rx_desc->status & 0x7E830000) {	/* errors */
922			if (streamer_priv->streamer_message_level) {
923				printk(KERN_WARNING "%s: Rx Error %x \n",
924				       dev->name, rx_desc->status);
925			}
926		} else {	/* received without errors */
927			if (rx_desc->status & 0x80000000) {	/* frame complete */
928				buffer_cnt = 1;
929				skb = dev_alloc_skb(streamer_priv->pkt_buf_sz);
930			} else {
931				skb = dev_alloc_skb(frame_length);
932			}
933
934			if (skb == NULL)
935			{
936				printk(KERN_WARNING "%s: Not enough memory to copy packet to upper layers. \n",	dev->name);
937				streamer_priv->streamer_stats.rx_dropped++;
938			} else {	/* we allocated an skb OK */
939				skb->dev = dev;
940
941				if (buffer_cnt == 1) {
942					/* release the DMA mapping */
943					pci_unmap_single(streamer_priv->pci_dev,
944						le32_to_cpu(streamer_priv->streamer_rx_ring[rx_ring_last_received].buffer),
945						streamer_priv->pkt_buf_sz,
946						PCI_DMA_FROMDEVICE);
947					skb2 = streamer_priv->rx_ring_skb[rx_ring_last_received];
948#if STREAMER_DEBUG_PACKETS
949					{
950						int i;
951						printk("streamer_rx packet print: skb->data2 %p  skb->head %p\n", skb2->data, skb2->head);
952						for (i = 0; i < frame_length; i++)
953						{
954							printk("%x:", skb2->data[i]);
955							if (((i + 1) % 16) == 0)
956								printk("\n");
957						}
958						printk("\n");
959					}
960#endif
961					skb_put(skb2, length);
962					skb2->protocol = tr_type_trans(skb2, dev);
963					/* recycle this descriptor */
964					streamer_priv->streamer_rx_ring[rx_ring_last_received].status = 0;
965					streamer_priv->streamer_rx_ring[rx_ring_last_received].framelen_buflen = streamer_priv->pkt_buf_sz;
966					streamer_priv->streamer_rx_ring[rx_ring_last_received].buffer =
967						cpu_to_le32(pci_map_single(streamer_priv->pci_dev, skb->data, streamer_priv->pkt_buf_sz,
968								PCI_DMA_FROMDEVICE));
969					streamer_priv->rx_ring_skb[rx_ring_last_received] = skb;
970					/* place recycled descriptor back on the adapter */
971					writel(cpu_to_le32(pci_map_single(streamer_priv->pci_dev,
972									&streamer_priv->streamer_rx_ring[rx_ring_last_received],
973									sizeof(struct streamer_rx_desc), PCI_DMA_FROMDEVICE)),
974						streamer_mmio + RXLBDA);
975					/* pass the received skb up to the protocol */
976					netif_rx(skb2);
977				} else {
978					do {	/* Walk the buffers */
979						pci_unmap_single(streamer_priv->pci_dev, le32_to_cpu(rx_desc->buffer), length, PCI_DMA_FROMDEVICE),
980						memcpy(skb_put(skb, length), (void *)rx_desc->buffer, length);	/* copy this fragment */
981						streamer_priv->streamer_rx_ring[rx_ring_last_received].status = 0;
982						streamer_priv->streamer_rx_ring[rx_ring_last_received].framelen_buflen = streamer_priv->pkt_buf_sz;
983
984						/* give descriptor back to the adapter */
985						writel(cpu_to_le32(pci_map_single(streamer_priv->pci_dev,
986									&streamer_priv->streamer_rx_ring[rx_ring_last_received],
987									length, PCI_DMA_FROMDEVICE)),
988							streamer_mmio + RXLBDA);
989
990						if (rx_desc->status & 0x80000000)
991							break;	/* this descriptor completes the frame */
992
993						/* else get the next pending descriptor */
994						if (rx_ring_last_received!= streamer_priv->rx_ring_last_received)
995						{
996							printk("RX Error rx_ring_last_received not the same %x %x\n",
997								rx_ring_last_received,
998								streamer_priv->rx_ring_last_received);
999						}
1000						rx_desc = &streamer_priv->streamer_rx_ring[(streamer_priv->rx_ring_last_received+1) & (STREAMER_RX_RING_SIZE-1)];
1001
1002						length = rx_desc->framelen_buflen & 0xffff;	/* buffer length */
1003						streamer_priv->rx_ring_last_received =	(streamer_priv->rx_ring_last_received+1) & (STREAMER_RX_RING_SIZE - 1);
1004						rx_ring_last_received = streamer_priv->rx_ring_last_received;
1005					} while (1);
1006
1007					skb->protocol = tr_type_trans(skb, dev);
1008					/* send up to the protocol */
1009					netif_rx(skb);
1010				}
1011				dev->last_rx = jiffies;
1012				streamer_priv->streamer_stats.rx_packets++;
1013				streamer_priv->streamer_stats.rx_bytes += length;
1014			}	/* if skb == null */
1015		}		/* end received without errors */
1016
1017		/* try the next one */
1018		rx_desc = &streamer_priv->streamer_rx_ring[(rx_ring_last_received + 1) & (STREAMER_RX_RING_SIZE - 1)];
1019	}			/* end for all completed rx descriptors */
1020}
1021
1022static void streamer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1023{
1024	struct net_device *dev = (struct net_device *) dev_id;
1025	struct streamer_private *streamer_priv =
1026	    (struct streamer_private *) dev->priv;
1027	__u8 *streamer_mmio = streamer_priv->streamer_mmio;
1028	__u16 sisr;
1029	__u16 misr;
1030	u8 max_intr = MAX_INTR;
1031
1032	spin_lock(&streamer_priv->streamer_lock);
1033	sisr = readw(streamer_mmio + SISR);
1034
1035	while((sisr & (SISR_MI | SISR_SRB_REPLY | SISR_ADAPTER_CHECK | SISR_ASB_FREE |
1036		       SISR_ARB_CMD | SISR_TRB_REPLY | SISR_PAR_ERR | SISR_SERR_ERR))
1037               && (max_intr > 0)) {
1038
1039		if(sisr & SISR_PAR_ERR) {
1040			writew(~SISR_PAR_ERR, streamer_mmio + SISR_RUM);
1041			(void)readw(streamer_mmio + SISR_RUM);
1042		}
1043
1044		else if(sisr & SISR_SERR_ERR) {
1045			writew(~SISR_SERR_ERR, streamer_mmio + SISR_RUM);
1046			(void)readw(streamer_mmio + SISR_RUM);
1047		}
1048
1049		else if(sisr & SISR_MI) {
1050			misr = readw(streamer_mmio + MISR_RUM);
1051
1052		if (misr & MISR_TX2_EOF) {
1053				while(streamer_priv->streamer_tx_ring[(streamer_priv->tx_ring_last_status + 1) & (STREAMER_TX_RING_SIZE - 1)].status) {
1054				streamer_priv->tx_ring_last_status = (streamer_priv->tx_ring_last_status + 1) & (STREAMER_TX_RING_SIZE - 1);
1055				streamer_priv->free_tx_ring_entries++;
1056				streamer_priv->streamer_stats.tx_bytes += streamer_priv->tx_ring_skb[streamer_priv->tx_ring_last_status]->len;
1057				streamer_priv->streamer_stats.tx_packets++;
1058				dev_kfree_skb_irq(streamer_priv->tx_ring_skb[streamer_priv->tx_ring_last_status]);
1059				streamer_priv->streamer_tx_ring[streamer_priv->tx_ring_last_status].buffer = 0xdeadbeef;
1060				streamer_priv->streamer_tx_ring[streamer_priv->tx_ring_last_status].status = 0;
1061				streamer_priv->streamer_tx_ring[streamer_priv->tx_ring_last_status].bufcnt_framelen = 0;
1062				streamer_priv->streamer_tx_ring[streamer_priv->tx_ring_last_status].buflen = 0;
1063				streamer_priv->streamer_tx_ring[streamer_priv->tx_ring_last_status].rsvd1 = 0;
1064				streamer_priv->streamer_tx_ring[streamer_priv->tx_ring_last_status].rsvd2 = 0;
1065				streamer_priv->streamer_tx_ring[streamer_priv->tx_ring_last_status].rsvd3 = 0;
1066			}
1067			netif_wake_queue(dev);
1068		}
1069
1070		if (misr & MISR_RX_EOF) {
1071			streamer_rx(dev);
1072		}
1073		/* MISR_RX_EOF */
1074
1075			if (misr & MISR_RX_NOBUF) {
1076				/* According to the documentation, we don't have to do anything,
1077                                 * but trapping it keeps it out of /var/log/messages.
1078                                 */
1079			}		/* SISR_RX_NOBUF */
1080
1081			writew(~misr, streamer_mmio + MISR_RUM);
1082			(void)readw(streamer_mmio + MISR_RUM);
1083		}
1084
1085		else if (sisr & SISR_SRB_REPLY) {
1086			if (streamer_priv->srb_queued == 1) {
1087				wake_up_interruptible(&streamer_priv->srb_wait);
1088			} else if (streamer_priv->srb_queued == 2) {
1089				streamer_srb_bh(dev);
1090			}
1091			streamer_priv->srb_queued = 0;
1092
1093			writew(~SISR_SRB_REPLY, streamer_mmio + SISR_RUM);
1094			(void)readw(streamer_mmio + SISR_RUM);
1095		}
1096
1097		else if (sisr & SISR_ADAPTER_CHECK) {
1098			printk(KERN_WARNING "%s: Adapter Check Interrupt Raised, 8 bytes of information follow:\n", dev->name);
1099			writel(readl(streamer_mmio + LAPWWO), streamer_mmio + LAPA);
1100			printk(KERN_WARNING "%s: Words %x:%x:%x:%x:\n",
1101			       dev->name, readw(streamer_mmio + LAPDINC),
1102			       ntohs(readw(streamer_mmio + LAPDINC)),
1103			       ntohs(readw(streamer_mmio + LAPDINC)),
1104			       ntohs(readw(streamer_mmio + LAPDINC)));
1105			free_irq(dev->irq, dev);
1106		}
1107
1108		/* SISR_ADAPTER_CHECK */
1109		else if (sisr & SISR_ASB_FREE) {
1110			/* Wake up anything that is waiting for the asb response */
1111			if (streamer_priv->asb_queued) {
1112				streamer_asb_bh(dev);
1113			}
1114			writew(~SISR_ASB_FREE, streamer_mmio + SISR_RUM);
1115			(void)readw(streamer_mmio + SISR_RUM);
1116		}
1117		/* SISR_ASB_FREE */
1118		else if (sisr & SISR_ARB_CMD) {
1119			streamer_arb_cmd(dev);
1120			writew(~SISR_ARB_CMD, streamer_mmio + SISR_RUM);
1121			(void)readw(streamer_mmio + SISR_RUM);
1122		}
1123		/* SISR_ARB_CMD */
1124		else if (sisr & SISR_TRB_REPLY) {
1125			/* Wake up anything that is waiting for the trb response */
1126			if (streamer_priv->trb_queued) {
1127				wake_up_interruptible(&streamer_priv->
1128						      trb_wait);
1129			}
1130			streamer_priv->trb_queued = 0;
1131			writew(~SISR_TRB_REPLY, streamer_mmio + SISR_RUM);
1132			(void)readw(streamer_mmio + SISR_RUM);
1133		}
1134		/* SISR_TRB_REPLY */
1135
1136		sisr = readw(streamer_mmio + SISR);
1137		max_intr--;
1138	} /* while() */
1139
1140	spin_unlock(&streamer_priv->streamer_lock) ;
1141}
1142
1143static int streamer_xmit(struct sk_buff *skb, struct net_device *dev)
1144{
1145	struct streamer_private *streamer_priv =
1146	    (struct streamer_private *) dev->priv;
1147	__u8 *streamer_mmio = streamer_priv->streamer_mmio;
1148	unsigned long flags ;
1149
1150	spin_lock_irqsave(&streamer_priv->streamer_lock, flags);
1151
1152	if (streamer_priv->free_tx_ring_entries) {
1153		streamer_priv->streamer_tx_ring[streamer_priv->tx_ring_free].status = 0;
1154		streamer_priv->streamer_tx_ring[streamer_priv->tx_ring_free].bufcnt_framelen = 0x00020000 | skb->len;
1155		streamer_priv->streamer_tx_ring[streamer_priv->tx_ring_free].buffer =
1156			cpu_to_le32(pci_map_single(streamer_priv->pci_dev, skb->data, skb->len, PCI_DMA_TODEVICE));
1157		streamer_priv->streamer_tx_ring[streamer_priv->tx_ring_free].rsvd1 = skb->len;
1158		streamer_priv->streamer_tx_ring[streamer_priv->tx_ring_free].rsvd2 = 0;
1159		streamer_priv->streamer_tx_ring[streamer_priv->tx_ring_free].rsvd3 = 0;
1160		streamer_priv->streamer_tx_ring[streamer_priv->tx_ring_free].buflen = skb->len;
1161
1162		streamer_priv->tx_ring_skb[streamer_priv->tx_ring_free] = skb;
1163		streamer_priv->free_tx_ring_entries--;
1164#if STREAMER_DEBUG_PACKETS
1165		{
1166			int i;
1167			printk("streamer_xmit packet print:\n");
1168			for (i = 0; i < skb->len; i++) {
1169				printk("%x:", skb->data[i]);
1170				if (((i + 1) % 16) == 0)
1171					printk("\n");
1172			}
1173			printk("\n");
1174		}
1175#endif
1176
1177		writel(cpu_to_le32(pci_map_single(streamer_priv->pci_dev,
1178					&streamer_priv->streamer_tx_ring[streamer_priv->tx_ring_free],
1179					sizeof(struct streamer_tx_desc), PCI_DMA_TODEVICE)),
1180			streamer_mmio + TX2LFDA);
1181		(void)readl(streamer_mmio + TX2LFDA);
1182
1183		streamer_priv->tx_ring_free = (streamer_priv->tx_ring_free + 1) & (STREAMER_TX_RING_SIZE - 1);
1184		spin_unlock_irqrestore(&streamer_priv->streamer_lock,flags);
1185		return 0;
1186	} else {
1187	        netif_stop_queue(dev);
1188	        spin_unlock_irqrestore(&streamer_priv->streamer_lock,flags);
1189		return 1;
1190	}
1191}
1192
1193
1194static int streamer_close(struct net_device *dev)
1195{
1196	struct streamer_private *streamer_priv =
1197	    (struct streamer_private *) dev->priv;
1198	__u8 *streamer_mmio = streamer_priv->streamer_mmio;
1199	unsigned long flags;
1200	int i;
1201
1202	netif_stop_queue(dev);
1203	writew(streamer_priv->srb, streamer_mmio + LAPA);
1204	writew(htons(SRB_CLOSE_ADAPTER << 8),streamer_mmio+LAPDINC);
1205	writew(htons(STREAMER_CLEAR_RET_CODE << 8), streamer_mmio+LAPDINC);
1206
1207	spin_lock_irqsave(&streamer_priv->streamer_lock, flags);
1208
1209	streamer_priv->srb_queued = 1;
1210	writew(LISR_SRB_CMD, streamer_mmio + LISR_SUM);
1211
1212	spin_unlock_irqrestore(&streamer_priv->streamer_lock, flags);
1213
1214	while (streamer_priv->srb_queued)
1215	{
1216		interruptible_sleep_on_timeout(&streamer_priv->srb_wait,
1217					       jiffies + 60 * HZ);
1218		if (signal_pending(current))
1219		{
1220			printk(KERN_WARNING "%s: SRB timed out.\n", dev->name);
1221			printk(KERN_WARNING "SISR=%x MISR=%x LISR=%x\n",
1222			       readw(streamer_mmio + SISR),
1223			       readw(streamer_mmio + MISR_RUM),
1224			       readw(streamer_mmio + LISR));
1225			streamer_priv->srb_queued = 0;
1226			break;
1227		}
1228	}
1229
1230	streamer_priv->rx_ring_last_received = (streamer_priv->rx_ring_last_received + 1) & (STREAMER_RX_RING_SIZE - 1);
1231
1232	for (i = 0; i < STREAMER_RX_RING_SIZE; i++) {
1233	        if (streamer_priv->rx_ring_skb[streamer_priv->rx_ring_last_received]) {
1234		        dev_kfree_skb(streamer_priv->rx_ring_skb[streamer_priv->rx_ring_last_received]);
1235		}
1236		streamer_priv->rx_ring_last_received = (streamer_priv->rx_ring_last_received + 1) & (STREAMER_RX_RING_SIZE - 1);
1237	}
1238
1239	/* reset tx/rx fifo's and busmaster logic */
1240
1241	/* TBD. Add graceful way to reset the LLC channel without doing a soft reset.
1242	   writel(readl(streamer_mmio+BCTL)|(3<<13),streamer_mmio+BCTL);
1243	   udelay(1);
1244	   writel(readl(streamer_mmio+BCTL)&~(3<<13),streamer_mmio+BCTL);
1245	 */
1246
1247#if STREAMER_DEBUG
1248	writew(streamer_priv->srb, streamer_mmio + LAPA);
1249	printk("srb): ");
1250	for (i = 0; i < 2; i++) {
1251		printk("%x ", ntohs(readw(streamer_mmio + LAPDINC)));
1252	}
1253	printk("\n");
1254#endif
1255	free_irq(dev->irq, dev);
1256	return 0;
1257}
1258
1259static void streamer_set_rx_mode(struct net_device *dev)
1260{
1261	struct streamer_private *streamer_priv =
1262	    (struct streamer_private *) dev->priv;
1263	__u8 *streamer_mmio = streamer_priv->streamer_mmio;
1264	__u8 options = 0;
1265	struct dev_mc_list *dmi;
1266	unsigned char dev_mc_address[5];
1267	int i;
1268
1269	writel(streamer_priv->srb, streamer_mmio + LAPA);
1270	options = streamer_priv->streamer_copy_all_options;
1271
1272	if (dev->flags & IFF_PROMISC)
1273		options |= (3 << 5);	/* All LLC and MAC frames, all through the main rx channel */
1274	else
1275		options &= ~(3 << 5);
1276
1277	/* Only issue the srb if there is a change in options */
1278
1279	if ((options ^ streamer_priv->streamer_copy_all_options))
1280	{
1281		/* Now to issue the srb command to alter the copy.all.options */
1282		writew(htons(SRB_MODIFY_RECEIVE_OPTIONS << 8), streamer_mmio+LAPDINC);
1283		writew(htons(STREAMER_CLEAR_RET_CODE << 8), streamer_mmio+LAPDINC);
1284		writew(htons((streamer_priv->streamer_receive_options << 8) | options),streamer_mmio+LAPDINC);
1285		writew(htons(0x4a41),streamer_mmio+LAPDINC);
1286		writew(htons(0x4d45),streamer_mmio+LAPDINC);
1287		writew(htons(0x5320),streamer_mmio+LAPDINC);
1288		writew(0x2020, streamer_mmio + LAPDINC);
1289
1290		streamer_priv->srb_queued = 2;	/* Can't sleep, use srb_bh */
1291
1292		writel(LISR_SRB_CMD, streamer_mmio + LISR_SUM);
1293
1294		streamer_priv->streamer_copy_all_options = options;
1295		return;
1296	}
1297
1298	/* Set the functional addresses we need for multicast */
1299	writel(streamer_priv->srb,streamer_mmio+LAPA);
1300	dev_mc_address[0] = dev_mc_address[1] = dev_mc_address[2] = dev_mc_address[3] = 0 ;
1301
1302	for (i=0,dmi=dev->mc_list;i < dev->mc_count; i++,dmi = dmi->next)
1303	{
1304   	        dev_mc_address[0] |= dmi->dmi_addr[2] ;
1305		dev_mc_address[1] |= dmi->dmi_addr[3] ;
1306		dev_mc_address[2] |= dmi->dmi_addr[4] ;
1307		dev_mc_address[3] |= dmi->dmi_addr[5] ;
1308	}
1309
1310	writew(htons(SRB_SET_FUNC_ADDRESS << 8),streamer_mmio+LAPDINC);
1311	writew(htons(STREAMER_CLEAR_RET_CODE << 8), streamer_mmio+LAPDINC);
1312	writew(0,streamer_mmio+LAPDINC);
1313	writew(htons( (dev_mc_address[0] << 8) | dev_mc_address[1]),streamer_mmio+LAPDINC);
1314	writew(htons( (dev_mc_address[2] << 8) | dev_mc_address[3]),streamer_mmio+LAPDINC);
1315	streamer_priv->srb_queued = 2 ;
1316	writel(LISR_SRB_CMD,streamer_mmio+LISR_SUM);
1317}
1318
1319static void streamer_srb_bh(struct net_device *dev)
1320{
1321	struct streamer_private *streamer_priv = (struct streamer_private *) dev->priv;
1322	__u8 *streamer_mmio = streamer_priv->streamer_mmio;
1323	__u16 srb_word;
1324
1325	writew(streamer_priv->srb, streamer_mmio + LAPA);
1326	srb_word=ntohs(readw(streamer_mmio+LAPDINC)) >> 8;
1327
1328	switch (srb_word) {
1329
1330		/* SRB_MODIFY_RECEIVE_OPTIONS i.e. set_multicast_list options (promiscuous)
1331		 * At some point we should do something if we get an error, such as
1332		 * resetting the IFF_PROMISC flag in dev
1333		 */
1334
1335	case SRB_MODIFY_RECEIVE_OPTIONS:
1336	        srb_word=ntohs(readw(streamer_mmio+LAPDINC)) >> 8;
1337
1338		switch (srb_word) {
1339		case 0x01:
1340			printk(KERN_WARNING "%s: Unrecognized srb command\n", dev->name);
1341			break;
1342		case 0x04:
1343			printk(KERN_WARNING "%s: Adapter must be open for this operation, doh!!\n", dev->name);
1344			break;
1345		default:
1346			if (streamer_priv->streamer_message_level)
1347				printk(KERN_WARNING "%s: Receive Options Modified to %x,%x\n",
1348				       dev->name,
1349				       streamer_priv->streamer_copy_all_options,
1350				       streamer_priv->streamer_receive_options);
1351			break;
1352		}		/* switch srb[2] */
1353		break;
1354
1355
1356		/* SRB_SET_GROUP_ADDRESS - Multicast group setting
1357		 */
1358	case SRB_SET_GROUP_ADDRESS:
1359	        srb_word=ntohs(readw(streamer_mmio+LAPDINC)) >> 8;
1360		switch (srb_word) {
1361		case 0x00:
1362		        break;
1363		case 0x01:
1364			printk(KERN_WARNING "%s: Unrecognized srb command \n",dev->name);
1365			break;
1366		case 0x04:
1367			printk(KERN_WARNING "%s: Adapter must be open for this operation, doh!!\n", dev->name);
1368			break;
1369		case 0x3c:
1370			printk(KERN_WARNING "%s: Group/Functional address indicator bits not set correctly\n", dev->name);
1371			break;
1372		case 0x3e:	/* If we ever implement individual multicast addresses, will need to deal with this */
1373			printk(KERN_WARNING "%s: Group address registers full\n", dev->name);
1374			break;
1375		case 0x55:
1376			printk(KERN_INFO "%s: Group Address already set.\n", dev->name);
1377			break;
1378		default:
1379			break;
1380		}		/* switch srb[2] */
1381		break;
1382
1383
1384		/* SRB_RESET_GROUP_ADDRESS - Remove a multicast address from group list
1385		 */
1386	case SRB_RESET_GROUP_ADDRESS:
1387	        srb_word=ntohs(readw(streamer_mmio+LAPDINC)) >> 8;
1388		switch (srb_word) {
1389		case 0x00:
1390		        break;
1391		case 0x01:
1392			printk(KERN_WARNING "%s: Unrecognized srb command \n", dev->name);
1393			break;
1394		case 0x04:
1395			printk(KERN_WARNING "%s: Adapter must be open for this operation, doh!!\n", dev->name);
1396			break;
1397		case 0x39:	/* Must deal with this if individual multicast addresses used */
1398			printk(KERN_INFO "%s: Group address not found \n", dev->name);
1399			break;
1400		default:
1401			break;
1402		}		/* switch srb[2] */
1403		break;
1404
1405
1406		/* SRB_SET_FUNC_ADDRESS - Called by the set_rx_mode
1407		 */
1408
1409	case SRB_SET_FUNC_ADDRESS:
1410	        srb_word=ntohs(readw(streamer_mmio+LAPDINC)) >> 8;
1411		switch (srb_word) {
1412		case 0x00:
1413			if (streamer_priv->streamer_message_level)
1414				printk(KERN_INFO "%s: Functional Address Mask Set \n", dev->name);
1415			break;
1416		case 0x01:
1417			printk(KERN_WARNING "%s: Unrecognized srb command \n", dev->name);
1418			break;
1419		case 0x04:
1420			printk(KERN_WARNING "%s: Adapter must be open for this operation, doh!!\n", dev->name);
1421			break;
1422		default:
1423			break;
1424		}		/* switch srb[2] */
1425		break;
1426
1427		/* SRB_READ_LOG - Read and reset the adapter error counters
1428		 */
1429
1430	case SRB_READ_LOG:
1431	        srb_word=ntohs(readw(streamer_mmio+LAPDINC)) >> 8;
1432		switch (srb_word) {
1433		case 0x00:
1434			{
1435				int i;
1436				if (streamer_priv->streamer_message_level)
1437					printk(KERN_INFO "%s: Read Log command complete\n", dev->name);
1438				printk("Read Log statistics: ");
1439				writew(streamer_priv->srb + 6,
1440				       streamer_mmio + LAPA);
1441				for (i = 0; i < 5; i++) {
1442					printk("%x:", ntohs(readw(streamer_mmio + LAPDINC)));
1443				}
1444				printk("\n");
1445			}
1446			break;
1447		case 0x01:
1448			printk(KERN_WARNING "%s: Unrecognized srb command \n", dev->name);
1449			break;
1450		case 0x04:
1451			printk(KERN_WARNING "%s: Adapter must be open for this operation, doh!!\n", dev->name);
1452			break;
1453
1454		}		/* switch srb[2] */
1455		break;
1456
1457		/* SRB_READ_SR_COUNTERS - Read and reset the source routing bridge related counters */
1458
1459	case SRB_READ_SR_COUNTERS:
1460	        srb_word=ntohs(readw(streamer_mmio+LAPDINC)) >> 8;
1461		switch (srb_word) {
1462		case 0x00:
1463			if (streamer_priv->streamer_message_level)
1464				printk(KERN_INFO "%s: Read Source Routing Counters issued\n", dev->name);
1465			break;
1466		case 0x01:
1467			printk(KERN_WARNING "%s: Unrecognized srb command \n", dev->name);
1468			break;
1469		case 0x04:
1470			printk(KERN_WARNING "%s: Adapter must be open for this operation, doh!!\n", dev->name);
1471			break;
1472		default:
1473			break;
1474		}		/* switch srb[2] */
1475		break;
1476
1477	default:
1478		printk(KERN_WARNING "%s: Unrecognized srb bh return value.\n", dev->name);
1479		break;
1480	}			/* switch srb[0] */
1481}
1482
1483static struct net_device_stats *streamer_get_stats(struct net_device *dev)
1484{
1485	struct streamer_private *streamer_priv;
1486	streamer_priv = (struct streamer_private *) dev->priv;
1487	return (struct net_device_stats *) &streamer_priv->streamer_stats;
1488}
1489
1490static int streamer_set_mac_address(struct net_device *dev, void *addr)
1491{
1492	struct sockaddr *saddr = addr;
1493	struct streamer_private *streamer_priv = (struct streamer_private *) dev->priv;
1494
1495	if (netif_running(dev))
1496	{
1497		printk(KERN_WARNING "%s: Cannot set mac/laa address while card is open\n", dev->name);
1498		return -EIO;
1499	}
1500
1501	memcpy(streamer_priv->streamer_laa, saddr->sa_data, dev->addr_len);
1502
1503	if (streamer_priv->streamer_message_level) {
1504		printk(KERN_INFO "%s: MAC/LAA Set to  = %x.%x.%x.%x.%x.%x\n",
1505		       dev->name, streamer_priv->streamer_laa[0],
1506		       streamer_priv->streamer_laa[1],
1507		       streamer_priv->streamer_laa[2],
1508		       streamer_priv->streamer_laa[3],
1509		       streamer_priv->streamer_laa[4],
1510		       streamer_priv->streamer_laa[5]);
1511	}
1512	return 0;
1513}
1514
1515static void streamer_arb_cmd(struct net_device *dev)
1516{
1517	struct streamer_private *streamer_priv =
1518	    (struct streamer_private *) dev->priv;
1519	__u8 *streamer_mmio = streamer_priv->streamer_mmio;
1520	__u8 header_len;
1521	__u16 frame_len, buffer_len;
1522	struct sk_buff *mac_frame;
1523	__u8 frame_data[256];
1524	__u16 buff_off;
1525	__u16 lan_status = 0, lan_status_diff;	/* Initialize to stop compiler warning */
1526	__u8 fdx_prot_error;
1527	__u16 next_ptr;
1528	__u16 arb_word;
1529
1530#if STREAMER_NETWORK_MONITOR
1531	struct trh_hdr *mac_hdr;
1532#endif
1533
1534	writew(streamer_priv->arb, streamer_mmio + LAPA);
1535	arb_word=ntohs(readw(streamer_mmio+LAPD)) >> 8;
1536
1537	if (arb_word == ARB_RECEIVE_DATA) {	/* Receive.data, MAC frames */
1538		writew(streamer_priv->arb + 6, streamer_mmio + LAPA);
1539		streamer_priv->mac_rx_buffer = buff_off = ntohs(readw(streamer_mmio + LAPDINC));
1540		header_len=ntohs(readw(streamer_mmio+LAPDINC)) >> 8; /* 802.5 Token-Ring Header Length */
1541		frame_len = ntohs(readw(streamer_mmio + LAPDINC));
1542
1543#if STREAMER_DEBUG
1544		{
1545			int i;
1546			__u16 next;
1547			__u8 status;
1548			__u16 len;
1549
1550			writew(ntohs(buff_off), streamer_mmio + LAPA);	/*setup window to frame data */
1551			next = htons(readw(streamer_mmio + LAPDINC));
1552			status =
1553			    ntohs(readw(streamer_mmio + LAPDINC)) & 0xff;
1554			len = ntohs(readw(streamer_mmio + LAPDINC));
1555
1556			/* print out 1st 14 bytes of frame data */
1557			for (i = 0; i < 7; i++) {
1558				printk("Loc %d = %04x\n", i,
1559				       ntohs(readw
1560					     (streamer_mmio + LAPDINC)));
1561			}
1562
1563			printk("next %04x, fs %02x, len %04x \n", next,
1564			       status, len);
1565		}
1566#endif
1567		if (!(mac_frame = dev_alloc_skb(frame_len))) {
1568			printk(KERN_WARNING "%s: Memory squeeze, dropping frame.\n",
1569			       dev->name);
1570			goto drop_frame;
1571		}
1572		/* Walk the buffer chain, creating the frame */
1573
1574		do {
1575			int i;
1576			__u16 rx_word;
1577
1578			writew(htons(buff_off), streamer_mmio + LAPA);	/* setup window to frame data */
1579			next_ptr = ntohs(readw(streamer_mmio + LAPDINC));
1580			readw(streamer_mmio + LAPDINC);	/* read thru status word */
1581			buffer_len = ntohs(readw(streamer_mmio + LAPDINC));
1582
1583			if (buffer_len > 256)
1584				break;
1585
1586			i = 0;
1587			while (i < buffer_len) {
1588				rx_word=ntohs(readw(streamer_mmio+LAPDINC));
1589				frame_data[i]=rx_word >> 8;
1590				frame_data[i+1]=rx_word & 0xff;
1591				i += 2;
1592			}
1593
1594			memcpy_fromio(skb_put(mac_frame, buffer_len),
1595				      frame_data, buffer_len);
1596		} while (next_ptr && (buff_off = next_ptr));
1597
1598#if STREAMER_NETWORK_MONITOR
1599		printk(KERN_WARNING "%s: Received MAC Frame, details: \n",
1600		       dev->name);
1601		mac_hdr = (struct trh_hdr *) mac_frame->data;
1602		printk(KERN_WARNING
1603		       "%s: MAC Frame Dest. Addr: %02x:%02x:%02x:%02x:%02x:%02x \n",
1604		       dev->name, mac_hdr->daddr[0], mac_hdr->daddr[1],
1605		       mac_hdr->daddr[2], mac_hdr->daddr[3],
1606		       mac_hdr->daddr[4], mac_hdr->daddr[5]);
1607		printk(KERN_WARNING
1608		       "%s: MAC Frame Srce. Addr: %02x:%02x:%02x:%02x:%02x:%02x \n",
1609		       dev->name, mac_hdr->saddr[0], mac_hdr->saddr[1],
1610		       mac_hdr->saddr[2], mac_hdr->saddr[3],
1611		       mac_hdr->saddr[4], mac_hdr->saddr[5]);
1612#endif
1613		mac_frame->dev = dev;
1614		mac_frame->protocol = tr_type_trans(mac_frame, dev);
1615		netif_rx(mac_frame);
1616
1617		/* Now tell the card we have dealt with the received frame */
1618drop_frame:
1619		/* Set LISR Bit 1 */
1620		writel(LISR_ARB_FREE, streamer_priv->streamer_mmio + LISR_SUM);
1621
1622		/* Is the ASB free ? */
1623
1624		if (!(readl(streamer_priv->streamer_mmio + SISR) & SISR_ASB_FREE))
1625		{
1626			streamer_priv->asb_queued = 1;
1627			writel(LISR_ASB_FREE_REQ, streamer_priv->streamer_mmio + LISR_SUM);
1628			return;
1629			/* Drop out and wait for the bottom half to be run */
1630		}
1631
1632
1633		writew(streamer_priv->asb, streamer_mmio + LAPA);
1634		writew(htons(ASB_RECEIVE_DATA << 8), streamer_mmio+LAPDINC);
1635		writew(htons(STREAMER_CLEAR_RET_CODE << 8), streamer_mmio+LAPDINC);
1636		writew(0, streamer_mmio + LAPDINC);
1637		writew(htons(streamer_priv->mac_rx_buffer), streamer_mmio + LAPD);
1638
1639		writel(LISR_ASB_REPLY | LISR_ASB_FREE_REQ, streamer_priv->streamer_mmio + LISR_SUM);
1640
1641		streamer_priv->asb_queued = 2;
1642		return;
1643
1644	} else if (arb_word == ARB_LAN_CHANGE_STATUS) {	/* Lan.change.status */
1645		writew(streamer_priv->arb + 6, streamer_mmio + LAPA);
1646		lan_status = ntohs(readw(streamer_mmio + LAPDINC));
1647		fdx_prot_error = ntohs(readw(streamer_mmio+LAPD)) >> 8;
1648
1649		/* Issue ARB Free */
1650		writew(LISR_ARB_FREE, streamer_priv->streamer_mmio + LISR_SUM);
1651
1652		lan_status_diff = (streamer_priv->streamer_lan_status ^ lan_status) &
1653		    lan_status;
1654
1655		if (lan_status_diff & (LSC_LWF | LSC_ARW | LSC_FPE | LSC_RR))
1656		{
1657			if (lan_status_diff & LSC_LWF)
1658				printk(KERN_WARNING "%s: Short circuit detected on the lobe\n", dev->name);
1659			if (lan_status_diff & LSC_ARW)
1660				printk(KERN_WARNING "%s: Auto removal error\n", dev->name);
1661			if (lan_status_diff & LSC_FPE)
1662				printk(KERN_WARNING "%s: FDX Protocol Error\n", dev->name);
1663			if (lan_status_diff & LSC_RR)
1664				printk(KERN_WARNING "%s: Force remove MAC frame received\n", dev->name);
1665
1666			/* Adapter has been closed by the hardware */
1667
1668			/* reset tx/rx fifo's and busmaster logic */
1669
1670			/* @TBD. no llc reset on autostreamer writel(readl(streamer_mmio+BCTL)|(3<<13),streamer_mmio+BCTL);
1671			   udelay(1);
1672			   writel(readl(streamer_mmio+BCTL)&~(3<<13),streamer_mmio+BCTL); */
1673			netif_stop_queue(dev);
1674			free_irq(dev->irq, dev);
1675
1676			printk(KERN_WARNING "%s: Adapter has been closed \n", dev->name);
1677
1678		}
1679		/* If serious error */
1680		if (streamer_priv->streamer_message_level) {
1681			if (lan_status_diff & LSC_SIG_LOSS)
1682				printk(KERN_WARNING "%s: No receive signal detected \n", dev->name);
1683			if (lan_status_diff & LSC_HARD_ERR)
1684				printk(KERN_INFO "%s: Beaconing \n", dev->name);
1685			if (lan_status_diff & LSC_SOFT_ERR)
1686				printk(KERN_WARNING "%s: Adapter transmitted Soft Error Report Mac Frame \n", dev->name);
1687			if (lan_status_diff & LSC_TRAN_BCN)
1688				printk(KERN_INFO "%s: We are tranmitting the beacon, aaah\n", dev->name);
1689			if (lan_status_diff & LSC_SS)
1690				printk(KERN_INFO "%s: Single Station on the ring \n", dev->name);
1691			if (lan_status_diff & LSC_RING_REC)
1692				printk(KERN_INFO "%s: Ring recovery ongoing\n", dev->name);
1693			if (lan_status_diff & LSC_FDX_MODE)
1694				printk(KERN_INFO "%s: Operating in FDX mode\n", dev->name);
1695		}
1696
1697		if (lan_status_diff & LSC_CO) {
1698			if (streamer_priv->streamer_message_level)
1699				printk(KERN_INFO "%s: Counter Overflow \n", dev->name);
1700
1701			/* Issue READ.LOG command */
1702
1703			writew(streamer_priv->srb, streamer_mmio + LAPA);
1704			writew(htons(SRB_READ_LOG << 8),streamer_mmio+LAPDINC);
1705			writew(htons(STREAMER_CLEAR_RET_CODE << 8), streamer_mmio+LAPDINC);
1706			writew(0, streamer_mmio + LAPDINC);
1707			streamer_priv->srb_queued = 2;	/* Can't sleep, use srb_bh */
1708
1709			writew(LISR_SRB_CMD, streamer_mmio + LISR_SUM);
1710		}
1711
1712		if (lan_status_diff & LSC_SR_CO) {
1713			if (streamer_priv->streamer_message_level)
1714				printk(KERN_INFO "%s: Source routing counters overflow\n", dev->name);
1715
1716			/* Issue a READ.SR.COUNTERS */
1717			writew(streamer_priv->srb, streamer_mmio + LAPA);
1718			writew(htons(SRB_READ_SR_COUNTERS << 8),
1719			       streamer_mmio+LAPDINC);
1720			writew(htons(STREAMER_CLEAR_RET_CODE << 8),
1721			       streamer_mmio+LAPDINC);
1722			streamer_priv->srb_queued = 2;	/* Can't sleep, use srb_bh */
1723			writew(LISR_SRB_CMD, streamer_mmio + LISR_SUM);
1724
1725		}
1726		streamer_priv->streamer_lan_status = lan_status;
1727	} /* Lan.change.status */
1728	else
1729		printk(KERN_WARNING "%s: Unknown arb command \n", dev->name);
1730}
1731
1732static void streamer_asb_bh(struct net_device *dev)
1733{
1734	struct streamer_private *streamer_priv =
1735	    (struct streamer_private *) dev->priv;
1736	__u8 *streamer_mmio = streamer_priv->streamer_mmio;
1737
1738	if (streamer_priv->asb_queued == 1)
1739	{
1740		/* Dropped through the first time */
1741
1742		writew(streamer_priv->asb, streamer_mmio + LAPA);
1743		writew(htons(ASB_RECEIVE_DATA << 8),streamer_mmio+LAPDINC);
1744		writew(htons(STREAMER_CLEAR_RET_CODE << 8), streamer_mmio+LAPDINC);
1745		writew(0, streamer_mmio + LAPDINC);
1746		writew(htons(streamer_priv->mac_rx_buffer), streamer_mmio + LAPD);
1747
1748		writel(LISR_ASB_REPLY | LISR_ASB_FREE_REQ, streamer_priv->streamer_mmio + LISR_SUM);
1749		streamer_priv->asb_queued = 2;
1750
1751		return;
1752	}
1753
1754	if (streamer_priv->asb_queued == 2) {
1755		__u8 rc;
1756		writew(streamer_priv->asb + 2, streamer_mmio + LAPA);
1757		rc=ntohs(readw(streamer_mmio+LAPD)) >> 8;
1758		switch (rc) {
1759		case 0x01:
1760			printk(KERN_WARNING "%s: Unrecognized command code \n", dev->name);
1761			break;
1762		case 0x26:
1763			printk(KERN_WARNING "%s: Unrecognized buffer address \n", dev->name);
1764			break;
1765		case 0xFF:
1766			/* Valid response, everything should be ok again */
1767			break;
1768		default:
1769			printk(KERN_WARNING "%s: Invalid return code in asb\n", dev->name);
1770			break;
1771		}
1772	}
1773	streamer_priv->asb_queued = 0;
1774}
1775
1776static int streamer_change_mtu(struct net_device *dev, int mtu)
1777{
1778	struct streamer_private *streamer_priv =
1779	    (struct streamer_private *) dev->priv;
1780	__u16 max_mtu;
1781
1782	if (streamer_priv->streamer_ring_speed == 4)
1783		max_mtu = 4500;
1784	else
1785		max_mtu = 18000;
1786
1787	if (mtu > max_mtu)
1788		return -EINVAL;
1789	if (mtu < 100)
1790		return -EINVAL;
1791
1792	dev->mtu = mtu;
1793	streamer_priv->pkt_buf_sz = mtu + TR_HLEN;
1794
1795	return 0;
1796}
1797
1798#if STREAMER_NETWORK_MONITOR
1799#ifdef CONFIG_PROC_FS
1800static int streamer_proc_info(char *buffer, char **start, off_t offset,
1801			      int length, int *eof, void *data)
1802{
1803  struct streamer_private *sdev=NULL;
1804	struct pci_dev *pci_device = NULL;
1805	int len = 0;
1806	off_t begin = 0;
1807	off_t pos = 0;
1808	int size;
1809
1810  struct net_device *dev;
1811
1812	size = sprintf(buffer, "IBM LanStreamer/MPC Chipset Token Ring Adapters\n");
1813
1814	pos += size;
1815	len += size;
1816
1817  for(sdev=dev_streamer; sdev; sdev=sdev->next) {
1818    pci_device=sdev->pci_dev;
1819    dev=pci_get_drvdata(pci_device);
1820
1821				size = sprintf_info(buffer + len, dev);
1822				len += size;
1823				pos = begin + len;
1824
1825				if (pos < offset) {
1826					len = 0;
1827					begin = pos;
1828				}
1829				if (pos > offset + length)
1830					break;
1831		}		/* for */
1832
1833	*start = buffer + (offset - begin);	/* Start of wanted data */
1834	len -= (offset - begin);	/* Start slop */
1835	if (len > length)
1836		len = length;	/* Ending slop */
1837	return len;
1838}
1839
1840static int sprintf_info(char *buffer, struct net_device *dev)
1841{
1842	struct streamer_private *streamer_priv =
1843	    (struct streamer_private *) dev->priv;
1844	__u8 *streamer_mmio = streamer_priv->streamer_mmio;
1845	struct streamer_adapter_addr_table sat;
1846	struct streamer_parameters_table spt;
1847	int size = 0;
1848	int i;
1849
1850	writew(streamer_priv->streamer_addr_table_addr, streamer_mmio + LAPA);
1851	for (i = 0; i < 14; i += 2) {
1852		__u16 io_word;
1853		__u8 *datap = (__u8 *) & sat;
1854		io_word=ntohs(readw(streamer_mmio+LAPDINC));
1855		datap[size]=io_word >> 8;
1856		datap[size+1]=io_word & 0xff;
1857	}
1858	writew(streamer_priv->streamer_parms_addr, streamer_mmio + LAPA);
1859	for (i = 0; i < 68; i += 2) {
1860		__u16 io_word;
1861		__u8 *datap = (__u8 *) & spt;
1862		io_word=ntohs(readw(streamer_mmio+LAPDINC));
1863		datap[size]=io_word >> 8;
1864		datap[size+1]=io_word & 0xff;
1865	}
1866
1867
1868	size = sprintf(buffer, "\n%6s: Adapter Address   : Node Address      : Functional Addr\n", dev->name);
1869
1870	size += sprintf(buffer + size,
1871		    "%6s: %02x:%02x:%02x:%02x:%02x:%02x : %02x:%02x:%02x:%02x:%02x:%02x : %02x:%02x:%02x:%02x\n",
1872		    dev->name, dev->dev_addr[0], dev->dev_addr[1],
1873		    dev->dev_addr[2], dev->dev_addr[3], dev->dev_addr[4],
1874		    dev->dev_addr[5], sat.node_addr[0], sat.node_addr[1],
1875		    sat.node_addr[2], sat.node_addr[3], sat.node_addr[4],
1876		    sat.node_addr[5], sat.func_addr[0], sat.func_addr[1],
1877		    sat.func_addr[2], sat.func_addr[3]);
1878
1879	size += sprintf(buffer + size, "\n%6s: Token Ring Parameters Table:\n", dev->name);
1880
1881	size += sprintf(buffer + size, "%6s: Physical Addr : Up Node Address   : Poll Address      : AccPri : Auth Src : Att Code :\n", dev->name);
1882
1883	size += sprintf(buffer + size,
1884		    "%6s: %02x:%02x:%02x:%02x   : %02x:%02x:%02x:%02x:%02x:%02x : %02x:%02x:%02x:%02x:%02x:%02x : %04x   : %04x     :  %04x    :\n",
1885		    dev->name, spt.phys_addr[0], spt.phys_addr[1],
1886		    spt.phys_addr[2], spt.phys_addr[3],
1887		    spt.up_node_addr[0], spt.up_node_addr[1],
1888		    spt.up_node_addr[2], spt.up_node_addr[3],
1889		    spt.up_node_addr[4], spt.up_node_addr[4],
1890		    spt.poll_addr[0], spt.poll_addr[1], spt.poll_addr[2],
1891		    spt.poll_addr[3], spt.poll_addr[4], spt.poll_addr[5],
1892		    ntohs(spt.acc_priority), ntohs(spt.auth_source_class),
1893		    ntohs(spt.att_code));
1894
1895	size += sprintf(buffer + size, "%6s: Source Address    : Bcn T : Maj. V : Lan St : Lcl Rg : Mon Err : Frame Correl : \n", dev->name);
1896
1897	size += sprintf(buffer + size,
1898		    "%6s: %02x:%02x:%02x:%02x:%02x:%02x : %04x  : %04x   : %04x   : %04x   : %04x    :     %04x     : \n",
1899		    dev->name, spt.source_addr[0], spt.source_addr[1],
1900		    spt.source_addr[2], spt.source_addr[3],
1901		    spt.source_addr[4], spt.source_addr[5],
1902		    ntohs(spt.beacon_type), ntohs(spt.major_vector),
1903		    ntohs(spt.lan_status), ntohs(spt.local_ring),
1904		    ntohs(spt.mon_error), ntohs(spt.frame_correl));
1905
1906	size += sprintf(buffer + size, "%6s: Beacon Details :  Tx  :  Rx  : NAUN Node Address : NAUN Node Phys : \n",
1907		    dev->name);
1908
1909	size += sprintf(buffer + size,
1910		    "%6s:                :  %02x  :  %02x  : %02x:%02x:%02x:%02x:%02x:%02x : %02x:%02x:%02x:%02x    : \n",
1911		    dev->name, ntohs(spt.beacon_transmit),
1912		    ntohs(spt.beacon_receive), spt.beacon_naun[0],
1913		    spt.beacon_naun[1], spt.beacon_naun[2],
1914		    spt.beacon_naun[3], spt.beacon_naun[4],
1915		    spt.beacon_naun[5], spt.beacon_phys[0],
1916		    spt.beacon_phys[1], spt.beacon_phys[2],
1917		    spt.beacon_phys[3]);
1918	return size;
1919}
1920#endif
1921#endif
1922
1923#if STREAMER_IOCTL && (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1924static int streamer_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1925{
1926        int i;
1927	struct streamer_private *streamer_priv = (struct streamer_private *) dev->priv;
1928	u8 *streamer_mmio = streamer_priv->streamer_mmio;
1929
1930	switch(cmd) {
1931	case IOCTL_SISR_MASK:
1932		writew(SISR_MI, streamer_mmio + SISR_MASK_SUM);
1933		break;
1934	case IOCTL_SPIN_LOCK_TEST:
1935	        printk(KERN_INFO "spin_lock() called.\n");
1936		spin_lock(&streamer_priv->streamer_lock);
1937		spin_unlock(&streamer_priv->streamer_lock);
1938		printk(KERN_INFO "spin_unlock() finished.\n");
1939		break;
1940	case IOCTL_PRINT_BDAS:
1941	        printk(KERN_INFO "bdas: RXBDA: %x RXLBDA: %x TX2FDA: %x TX2LFDA: %x\n",
1942		       readw(streamer_mmio + RXBDA),
1943		       readw(streamer_mmio + RXLBDA),
1944		       readw(streamer_mmio + TX2FDA),
1945		       readw(streamer_mmio + TX2LFDA));
1946		break;
1947	case IOCTL_PRINT_REGISTERS:
1948	        printk(KERN_INFO "registers:\n");
1949		printk(KERN_INFO "SISR: %04x MISR: %04x LISR: %04x BCTL: %04x BMCTL: %04x\nmask  %04x mask  %04x\n",
1950		       readw(streamer_mmio + SISR),
1951		       readw(streamer_mmio + MISR_RUM),
1952		       readw(streamer_mmio + LISR),
1953		       readw(streamer_mmio + BCTL),
1954		       readw(streamer_mmio + BMCTL_SUM),
1955		       readw(streamer_mmio + SISR_MASK),
1956		       readw(streamer_mmio + MISR_MASK));
1957		break;
1958	case IOCTL_PRINT_RX_BUFS:
1959	        printk(KERN_INFO "Print rx bufs:\n");
1960		for(i=0; i<STREAMER_RX_RING_SIZE; i++)
1961		        printk(KERN_INFO "rx_ring %d status: 0x%x\n", i,
1962			       streamer_priv->streamer_rx_ring[i].status);
1963		break;
1964	case IOCTL_PRINT_TX_BUFS:
1965	        printk(KERN_INFO "Print tx bufs:\n");
1966		for(i=0; i<STREAMER_TX_RING_SIZE; i++)
1967		        printk(KERN_INFO "tx_ring %d status: 0x%x\n", i,
1968			       streamer_priv->streamer_tx_ring[i].status);
1969		break;
1970	case IOCTL_RX_CMD:
1971	        streamer_rx(dev);
1972		printk(KERN_INFO "Sent rx command.\n");
1973		break;
1974	default:
1975	        printk(KERN_INFO "Bad ioctl!\n");
1976	}
1977	return 0;
1978}
1979#endif
1980
1981static struct pci_driver streamer_pci_driver = {
1982  name:       "lanstreamer",
1983  id_table:   streamer_pci_tbl,
1984  probe:      streamer_init_one,
1985  remove:     __devexit_p(streamer_remove_one),
1986};
1987
1988static int __init streamer_init_module(void) {
1989  return pci_module_init(&streamer_pci_driver);
1990}
1991
1992static void __exit streamer_cleanup_module(void) {
1993  pci_unregister_driver(&streamer_pci_driver);
1994}
1995
1996module_init(streamer_init_module);
1997module_exit(streamer_cleanup_module);
1998MODULE_LICENSE("GPL");
1999