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