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