Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux-2.6] / drivers / net / tokenring / olympic.c
1 /*
2  *   olympic.c (c) 1999 Peter De Schrijver All Rights Reserved
3  *                 1999/2000 Mike Phillips (mikep@linuxtr.net)
4  *
5  *  Linux driver for IBM PCI tokenring cards based on the Pit/Pit-Phy/Olympic
6  *  chipset. 
7  *
8  *  Base Driver Skeleton:
9  *      Written 1993-94 by Donald Becker.
10  *
11  *      Copyright 1993 United States Government as represented by the
12  *      Director, National Security Agency.
13  *
14  *  Thanks to Erik De Cock, Adrian Bridgett and Frank Fiene for their 
15  *  assistance and perserverance with the testing of this driver.
16  *
17  *  This software may be used and distributed according to the terms
18  *  of the GNU General Public License, incorporated herein by reference.
19  * 
20  *  4/27/99 - Alpha Release 0.1.0
21  *            First release to the public
22  *
23  *  6/8/99  - Official Release 0.2.0   
24  *            Merged into the kernel code 
25  *  8/18/99 - Updated driver for 2.3.13 kernel to use new pci
26  *            resource. Driver also reports the card name returned by
27  *            the pci resource.
28  *  1/11/00 - Added spinlocks for smp
29  *  2/23/00 - Updated to dev_kfree_irq 
30  *  3/10/00 - Fixed FDX enable which triggered other bugs also 
31  *            squashed.
32  *  5/20/00 - Changes to handle Olympic on LinuxPPC. Endian changes.
33  *            The odd thing about the changes is that the fix for
34  *            endian issues with the big-endian data in the arb, asb...
35  *            was to always swab() the bytes, no matter what CPU.
36  *            That's because the read[wl]() functions always swap the
37  *            bytes on the way in on PPC.
38  *            Fixing the hardware descriptors was another matter,
39  *            because they weren't going through read[wl](), there all
40  *            the results had to be in memory in le32 values. kdaaker
41  *
42  * 12/23/00 - Added minimal Cardbus support (Thanks Donald).
43  *
44  * 03/09/01 - Add new pci api, dev_base_lock, general clean up. 
45  *
46  * 03/27/01 - Add new dma pci (Thanks to Kyle Lucke) and alloc_trdev
47  *            Change proc_fs behaviour, now one entry per adapter.
48  *
49  * 04/09/01 - Couple of bug fixes to the dma unmaps and ejecting the
50  *            adapter when live does not take the system down with it.
51  * 
52  * 06/02/01 - Clean up, copy skb for small packets
53  * 
54  * 06/22/01 - Add EISR error handling routines 
55  *
56  * 07/19/01 - Improve bad LAA reporting, strip out freemem
57  *            into a separate function, its called from 3 
58  *            different places now. 
59  * 02/09/02 - Replaced sleep_on. 
60  * 03/01/02 - Replace access to several registers from 32 bit to 
61  *            16 bit. Fixes alignment errors on PPC 64 bit machines.
62  *            Thanks to Al Trautman for this one.
63  * 03/10/02 - Fix BUG in arb_cmd. Bug was there all along but was
64  *            silently ignored until the error checking code 
65  *            went into version 1.0.0 
66  * 06/04/02 - Add correct start up sequence for the cardbus adapters.
67  *            Required for strict compliance with pci power mgmt specs.
68  *  To Do:
69  *
70  *           Wake on lan        
71  * 
72  *  If Problems do Occur
73  *  Most problems can be rectified by either closing and opening the interface
74  *  (ifconfig down and up) or rmmod and insmod'ing the driver (a bit difficult
75  *  if compiled into the kernel).
76  */
77
78 /* Change OLYMPIC_DEBUG to 1 to get verbose, and I mean really verbose, messages */
79
80 #define OLYMPIC_DEBUG 0
81
82
83 #include <linux/module.h>
84 #include <linux/kernel.h>
85 #include <linux/errno.h>
86 #include <linux/timer.h>
87 #include <linux/in.h>
88 #include <linux/ioport.h>
89 #include <linux/string.h>
90 #include <linux/proc_fs.h>
91 #include <linux/ptrace.h>
92 #include <linux/skbuff.h>
93 #include <linux/interrupt.h>
94 #include <linux/delay.h>
95 #include <linux/netdevice.h>
96 #include <linux/trdevice.h>
97 #include <linux/stddef.h>
98 #include <linux/init.h>
99 #include <linux/pci.h>
100 #include <linux/spinlock.h>
101 #include <linux/bitops.h>
102 #include <linux/jiffies.h>
103
104 #include <net/checksum.h>
105
106 #include <asm/io.h>
107 #include <asm/system.h>
108
109 #include "olympic.h"
110
111 /* I've got to put some intelligence into the version number so that Peter and I know
112  * which version of the code somebody has got. 
113  * Version Number = a.b.c.d  where a.b.c is the level of code and d is the latest author.
114  * So 0.0.1.pds = Peter, 0.0.1.mlp = Mike
115  * 
116  * Official releases will only have an a.b.c version number format. 
117  */
118
119 static char version[] __devinitdata = 
120 "Olympic.c v1.0.5 6/04/02 - Peter De Schrijver & Mike Phillips" ; 
121
122 static char *open_maj_error[]  = {"No error", "Lobe Media Test", "Physical Insertion",
123                                    "Address Verification", "Neighbor Notification (Ring Poll)",
124                                    "Request Parameters","FDX Registration Request",
125                                    "FDX Duplicate Address Check", "Station registration Query Wait",
126                                    "Unknown stage"};
127
128 static char *open_min_error[] = {"No error", "Function Failure", "Signal Lost", "Wire Fault",
129                                    "Ring Speed Mismatch", "Timeout","Ring Failure","Ring Beaconing",
130                                    "Duplicate Node Address","Request Parameters","Remove Received",
131                                    "Reserved", "Reserved", "No Monitor Detected for RPL", 
132                                    "Monitor Contention failer for RPL", "FDX Protocol Error"};
133
134 /* Module paramters */
135
136 MODULE_AUTHOR("Mike Phillips <mikep@linuxtr.net>") ; 
137 MODULE_DESCRIPTION("Olympic PCI/Cardbus Chipset Driver") ; 
138
139 /* Ring Speed 0,4,16,100 
140  * 0 = Autosense         
141  * 4,16 = Selected speed only, no autosense
142  * This allows the card to be the first on the ring
143  * and become the active monitor.
144  * 100 = Nothing at present, 100mbps is autodetected
145  * if FDX is turned on. May be implemented in the future to 
146  * fail if 100mpbs is not detected.
147  *
148  * WARNING: Some hubs will allow you to insert
149  * at the wrong speed
150  */
151
152 static int ringspeed[OLYMPIC_MAX_ADAPTERS] = {0,} ;
153 module_param_array(ringspeed, int, NULL, 0);
154
155 /* Packet buffer size */
156
157 static int pkt_buf_sz[OLYMPIC_MAX_ADAPTERS] = {0,} ;
158 module_param_array(pkt_buf_sz, int, NULL, 0) ;
159
160 /* Message Level */
161
162 static int message_level[OLYMPIC_MAX_ADAPTERS] = {0,} ; 
163 module_param_array(message_level, int, NULL, 0) ;
164
165 /* Change network_monitor to receive mac frames through the arb channel.
166  * Will also create a /proc/net/olympic_tr%d entry, where %d is the tr
167  * device, i.e. tr0, tr1 etc. 
168  * Intended to be used to create a ring-error reporting network module 
169  * i.e. it will give you the source address of beaconers on the ring 
170  */
171 static int network_monitor[OLYMPIC_MAX_ADAPTERS] = {0,};
172 module_param_array(network_monitor, int, NULL, 0);
173
174 static struct pci_device_id olympic_pci_tbl[] = {
175         {PCI_VENDOR_ID_IBM,PCI_DEVICE_ID_IBM_TR_WAKE,PCI_ANY_ID,PCI_ANY_ID,},
176         { }     /* Terminating Entry */
177 };
178 MODULE_DEVICE_TABLE(pci,olympic_pci_tbl) ; 
179
180
181 static int olympic_probe(struct pci_dev *pdev, const struct pci_device_id *ent); 
182 static int olympic_init(struct net_device *dev);
183 static int olympic_open(struct net_device *dev);
184 static int olympic_xmit(struct sk_buff *skb, struct net_device *dev);
185 static int olympic_close(struct net_device *dev);
186 static void olympic_set_rx_mode(struct net_device *dev);
187 static void olympic_freemem(struct net_device *dev) ;  
188 static irqreturn_t olympic_interrupt(int irq, void *dev_id);
189 static struct net_device_stats * olympic_get_stats(struct net_device *dev);
190 static int olympic_set_mac_address(struct net_device *dev, void *addr) ; 
191 static void olympic_arb_cmd(struct net_device *dev);
192 static int olympic_change_mtu(struct net_device *dev, int mtu);
193 static void olympic_srb_bh(struct net_device *dev) ; 
194 static void olympic_asb_bh(struct net_device *dev) ; 
195 static int olympic_proc_info(char *buffer, char **start, off_t offset, int length, int *eof, void *data) ; 
196
197 static int __devinit olympic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
198 {
199         struct net_device *dev ; 
200         struct olympic_private *olympic_priv;
201         static int card_no = -1 ;
202         int i ; 
203
204         card_no++ ; 
205
206         if ((i = pci_enable_device(pdev))) {
207                 return i ; 
208         }
209
210         pci_set_master(pdev);
211
212         if ((i = pci_request_regions(pdev,"olympic"))) { 
213                 goto op_disable_dev;
214         }
215  
216         dev = alloc_trdev(sizeof(struct olympic_private)) ; 
217         if (!dev) {
218                 i = -ENOMEM; 
219                 goto op_release_dev;
220         }
221
222         olympic_priv = dev->priv ;
223         
224         spin_lock_init(&olympic_priv->olympic_lock) ; 
225
226         init_waitqueue_head(&olympic_priv->srb_wait);
227         init_waitqueue_head(&olympic_priv->trb_wait);
228 #if OLYMPIC_DEBUG  
229         printk(KERN_INFO "pci_device: %p, dev:%p, dev->priv: %p\n", pdev, dev, dev->priv);
230 #endif
231         dev->irq=pdev->irq;
232         dev->base_addr=pci_resource_start(pdev, 0);
233         olympic_priv->olympic_card_name = pci_name(pdev);
234         olympic_priv->pdev = pdev; 
235         olympic_priv->olympic_mmio = ioremap(pci_resource_start(pdev,1),256);
236         olympic_priv->olympic_lap = ioremap(pci_resource_start(pdev,2),2048);
237         if (!olympic_priv->olympic_mmio || !olympic_priv->olympic_lap) {
238                 goto op_free_iomap;
239         }
240                                 
241         if ((pkt_buf_sz[card_no] < 100) || (pkt_buf_sz[card_no] > 18000) )
242                 olympic_priv->pkt_buf_sz = PKT_BUF_SZ ; 
243         else
244                 olympic_priv->pkt_buf_sz = pkt_buf_sz[card_no] ; 
245
246         dev->mtu = olympic_priv->pkt_buf_sz - TR_HLEN ; 
247         olympic_priv->olympic_ring_speed = ringspeed[card_no] ; 
248         olympic_priv->olympic_message_level = message_level[card_no] ; 
249         olympic_priv->olympic_network_monitor = network_monitor[card_no];
250         
251         if ((i = olympic_init(dev))) {
252                 goto op_free_iomap;
253         }                               
254
255         dev->open=&olympic_open;
256         dev->hard_start_xmit=&olympic_xmit;
257         dev->change_mtu=&olympic_change_mtu;
258         dev->stop=&olympic_close;
259         dev->do_ioctl=NULL;
260         dev->set_multicast_list=&olympic_set_rx_mode;
261         dev->get_stats=&olympic_get_stats ;
262         dev->set_mac_address=&olympic_set_mac_address ;  
263         SET_MODULE_OWNER(dev) ; 
264         SET_NETDEV_DEV(dev, &pdev->dev);
265
266         pci_set_drvdata(pdev,dev) ; 
267         register_netdev(dev) ; 
268         printk("Olympic: %s registered as: %s\n",olympic_priv->olympic_card_name,dev->name);
269         if (olympic_priv->olympic_network_monitor) { /* Must go after register_netdev as we need the device name */ 
270                 char proc_name[20] ; 
271                 strcpy(proc_name,"net/olympic_") ; 
272                 strcat(proc_name,dev->name) ; 
273                 create_proc_read_entry(proc_name,0,NULL,olympic_proc_info,(void *)dev) ; 
274                 printk("Olympic: Network Monitor information: /proc/%s\n",proc_name); 
275         }
276         return  0 ;
277
278 op_free_iomap:
279         if (olympic_priv->olympic_mmio)
280                 iounmap(olympic_priv->olympic_mmio); 
281         if (olympic_priv->olympic_lap)
282                 iounmap(olympic_priv->olympic_lap);
283
284         free_netdev(dev);
285 op_release_dev:
286         pci_release_regions(pdev); 
287
288 op_disable_dev:
289         pci_disable_device(pdev);
290         return i;
291 }
292
293 static int __devinit olympic_init(struct net_device *dev)
294 {
295         struct olympic_private *olympic_priv;
296         u8 __iomem *olympic_mmio, *init_srb,*adapter_addr;
297         unsigned long t; 
298         unsigned int uaa_addr;
299
300         olympic_priv=(struct olympic_private *)dev->priv;
301         olympic_mmio=olympic_priv->olympic_mmio;
302
303         printk("%s \n", version);
304         printk("%s. I/O at %hx, MMIO at %p, LAP at %p, using irq %d\n", olympic_priv->olympic_card_name, (unsigned int) dev->base_addr,olympic_priv->olympic_mmio, olympic_priv->olympic_lap, dev->irq);
305
306         writel(readl(olympic_mmio+BCTL) | BCTL_SOFTRESET,olympic_mmio+BCTL);
307         t=jiffies;
308         while((readl(olympic_mmio+BCTL)) & BCTL_SOFTRESET) {
309                 schedule();             
310                 if(time_after(jiffies, t + 40*HZ)) {
311                         printk(KERN_ERR "IBM PCI tokenring card not responding.\n");
312                         return -ENODEV;
313                 }
314         }
315
316
317         /* Needed for cardbus */
318         if(!(readl(olympic_mmio+BCTL) & BCTL_MODE_INDICATOR)) {
319                 writel(readl(olympic_priv->olympic_mmio+FERMASK)|FERMASK_INT_BIT, olympic_mmio+FERMASK);
320         }
321         
322 #if OLYMPIC_DEBUG
323         printk("BCTL: %x\n",readl(olympic_mmio+BCTL));
324         printk("GPR: %x\n",readw(olympic_mmio+GPR));
325         printk("SISRMASK: %x\n",readl(olympic_mmio+SISR_MASK));
326 #endif
327         /* Aaaahhh, You have got to be real careful setting GPR, the card
328            holds the previous values from flash memory, including autosense 
329            and ring speed */
330
331         writel(readl(olympic_mmio+BCTL)|BCTL_MIMREB,olympic_mmio+BCTL);
332         
333         if (olympic_priv->olympic_ring_speed  == 0) { /* Autosense */
334                 writew(readw(olympic_mmio+GPR)|GPR_AUTOSENSE,olympic_mmio+GPR);
335                 if (olympic_priv->olympic_message_level) 
336                         printk(KERN_INFO "%s: Ringspeed autosense mode on\n",olympic_priv->olympic_card_name);
337         } else if (olympic_priv->olympic_ring_speed == 16) {
338                 if (olympic_priv->olympic_message_level) 
339                         printk(KERN_INFO "%s: Trying to open at 16 Mbps as requested\n", olympic_priv->olympic_card_name);
340                 writew(GPR_16MBPS, olympic_mmio+GPR);
341         } else if (olympic_priv->olympic_ring_speed == 4) {
342                 if (olympic_priv->olympic_message_level) 
343                         printk(KERN_INFO "%s: Trying to open at 4 Mbps as requested\n", olympic_priv->olympic_card_name) ; 
344                 writew(0, olympic_mmio+GPR);
345         } 
346         
347         writew(readw(olympic_mmio+GPR)|GPR_NEPTUNE_BF,olympic_mmio+GPR);
348
349 #if OLYMPIC_DEBUG
350         printk("GPR = %x\n",readw(olympic_mmio + GPR) ) ; 
351 #endif
352         /* Solo has been paused to meet the Cardbus power
353          * specs if the adapter is cardbus. Check to 
354          * see its been paused and then restart solo. The
355          * adapter should set the pause bit within 1 second.
356          */
357
358         if(!(readl(olympic_mmio+BCTL) & BCTL_MODE_INDICATOR)) { 
359                 t=jiffies;
360                 while (!readl(olympic_mmio+CLKCTL) & CLKCTL_PAUSE) { 
361                         schedule() ; 
362                         if(time_after(jiffies, t + 2*HZ)) {
363                                 printk(KERN_ERR "IBM Cardbus tokenring adapter not responsing.\n") ; 
364                                 return -ENODEV;
365                         }
366                 }
367                 writel(readl(olympic_mmio+CLKCTL) & ~CLKCTL_PAUSE, olympic_mmio+CLKCTL) ; 
368         }
369         
370         /* start solo init */
371         writel((1<<15),olympic_mmio+SISR_MASK_SUM);
372
373         t=jiffies;
374         while(!((readl(olympic_mmio+SISR_RR)) & SISR_SRB_REPLY)) {
375                 schedule();             
376                 if(time_after(jiffies, t + 15*HZ)) {
377                         printk(KERN_ERR "IBM PCI tokenring card not responding.\n");
378                         return -ENODEV;
379                 }
380         }
381         
382         writel(readw(olympic_mmio+LAPWWO),olympic_mmio+LAPA);
383
384 #if OLYMPIC_DEBUG
385         printk("LAPWWO: %x, LAPA: %x\n",readl(olympic_mmio+LAPWWO), readl(olympic_mmio+LAPA));
386 #endif
387
388         init_srb=olympic_priv->olympic_lap + ((readw(olympic_mmio+LAPWWO)) & (~0xf800));
389
390 #if OLYMPIC_DEBUG               
391 {
392         int i;
393         printk("init_srb(%p): ",init_srb);
394         for(i=0;i<20;i++)
395                 printk("%x ",readb(init_srb+i));
396         printk("\n");
397 }
398 #endif  
399         if(readw(init_srb+6)) {
400                 printk(KERN_INFO "tokenring card initialization failed. errorcode : %x\n",readw(init_srb+6));
401                 return -ENODEV;
402         }
403
404         if (olympic_priv->olympic_message_level) {
405                 if ( readb(init_srb +2) & 0x40) { 
406                         printk(KERN_INFO "Olympic: Adapter is FDX capable.\n") ;
407                 } else { 
408                         printk(KERN_INFO "Olympic: Adapter cannot do FDX.\n");
409                 }
410         }
411   
412         uaa_addr=swab16(readw(init_srb+8));
413
414 #if OLYMPIC_DEBUG
415         printk("UAA resides at %x\n",uaa_addr);
416 #endif
417
418         writel(uaa_addr,olympic_mmio+LAPA);
419         adapter_addr=olympic_priv->olympic_lap + (uaa_addr & (~0xf800));
420
421 #if OLYMPIC_DEBUG
422         printk("adapter address: %02x:%02x:%02x:%02x:%02x:%02x\n",
423                         readb(adapter_addr), readb(adapter_addr+1),readb(adapter_addr+2),
424                         readb(adapter_addr+3),readb(adapter_addr+4),readb(adapter_addr+5));
425 #endif
426
427         memcpy_fromio(&dev->dev_addr[0], adapter_addr,6);
428
429         olympic_priv->olympic_addr_table_addr = swab16(readw(init_srb + 12)); 
430         olympic_priv->olympic_parms_addr = swab16(readw(init_srb + 14)); 
431
432         return 0;
433
434 }
435
436 static int olympic_open(struct net_device *dev) 
437 {
438         struct olympic_private *olympic_priv=(struct olympic_private *)dev->priv;
439         u8 __iomem *olympic_mmio=olympic_priv->olympic_mmio,*init_srb;
440         unsigned long flags, t;
441         int i, open_finished = 1 ;
442         u8 resp, err;
443
444         DECLARE_WAITQUEUE(wait,current) ; 
445
446         olympic_init(dev);
447
448         if(request_irq(dev->irq, &olympic_interrupt, IRQF_SHARED , "olympic", dev)) {
449                 return -EAGAIN;
450         }
451
452 #if OLYMPIC_DEBUG
453         printk("BMCTL: %x\n",readl(olympic_mmio+BMCTL_SUM));
454         printk("pending ints: %x\n",readl(olympic_mmio+SISR_RR));
455 #endif
456
457         writel(SISR_MI,olympic_mmio+SISR_MASK_SUM);
458
459         writel(SISR_MI | SISR_SRB_REPLY, olympic_mmio+SISR_MASK); /* more ints later, doesn't stop arb cmd interrupt */
460
461         writel(LISR_LIE,olympic_mmio+LISR); /* more ints later */
462
463         /* adapter is closed, so SRB is pointed to by LAPWWO */
464
465         writel(readw(olympic_mmio+LAPWWO),olympic_mmio+LAPA);
466         init_srb=olympic_priv->olympic_lap + ((readw(olympic_mmio+LAPWWO)) & (~0xf800));
467         
468 #if OLYMPIC_DEBUG
469         printk("LAPWWO: %x, LAPA: %x\n",readw(olympic_mmio+LAPWWO), readl(olympic_mmio+LAPA));
470         printk("SISR Mask = %04x\n", readl(olympic_mmio+SISR_MASK));
471         printk("Before the open command \n");
472 #endif  
473         do {
474                 memset_io(init_srb,0,SRB_COMMAND_SIZE);
475
476                 writeb(SRB_OPEN_ADAPTER,init_srb) ;     /* open */
477                 writeb(OLYMPIC_CLEAR_RET_CODE,init_srb+2);
478
479                 /* If Network Monitor, instruct card to copy MAC frames through the ARB */
480                 if (olympic_priv->olympic_network_monitor) 
481                         writew(swab16(OPEN_ADAPTER_ENABLE_FDX | OPEN_ADAPTER_PASS_ADC_MAC | OPEN_ADAPTER_PASS_ATT_MAC | OPEN_ADAPTER_PASS_BEACON), init_srb+8);
482                 else
483                         writew(swab16(OPEN_ADAPTER_ENABLE_FDX), init_srb+8);
484         
485                 /* Test OR of first 3 bytes as its totally possible for 
486                  * someone to set the first 2 bytes to be zero, although this 
487                  * is an error, the first byte must have bit 6 set to 1  */
488
489                 if (olympic_priv->olympic_laa[0] | olympic_priv->olympic_laa[1] | olympic_priv->olympic_laa[2]) {
490                         writeb(olympic_priv->olympic_laa[0],init_srb+12);
491                         writeb(olympic_priv->olympic_laa[1],init_srb+13);
492                         writeb(olympic_priv->olympic_laa[2],init_srb+14);
493                         writeb(olympic_priv->olympic_laa[3],init_srb+15);
494                         writeb(olympic_priv->olympic_laa[4],init_srb+16);
495                         writeb(olympic_priv->olympic_laa[5],init_srb+17);
496                         memcpy(dev->dev_addr,olympic_priv->olympic_laa,dev->addr_len) ;  
497                 }       
498                 writeb(1,init_srb+30);
499
500                 spin_lock_irqsave(&olympic_priv->olympic_lock,flags);   
501                 olympic_priv->srb_queued=1;
502
503                 writel(LISR_SRB_CMD,olympic_mmio+LISR_SUM);
504                 spin_unlock_irqrestore(&olympic_priv->olympic_lock,flags);
505
506                 t = jiffies ; 
507         
508                 add_wait_queue(&olympic_priv->srb_wait,&wait) ;
509                 set_current_state(TASK_INTERRUPTIBLE) ; 
510  
511                 while(olympic_priv->srb_queued) {        
512                         schedule() ; 
513                         if(signal_pending(current))     {            
514                                 printk(KERN_WARNING "%s: Signal received in open.\n",
515                                         dev->name);
516                                 printk(KERN_WARNING "SISR=%x LISR=%x\n",
517                                         readl(olympic_mmio+SISR),
518                                         readl(olympic_mmio+LISR));
519                                 olympic_priv->srb_queued=0;
520                                 break;
521                         }
522                         if (time_after(jiffies, t + 10*HZ)) {
523                                 printk(KERN_WARNING "%s: SRB timed out. \n",dev->name) ; 
524                                 olympic_priv->srb_queued=0;
525                                 break ; 
526                         } 
527                         set_current_state(TASK_INTERRUPTIBLE) ; 
528                 }
529                 remove_wait_queue(&olympic_priv->srb_wait,&wait) ; 
530                 set_current_state(TASK_RUNNING) ; 
531                 olympic_priv->srb_queued = 0 ; 
532 #if OLYMPIC_DEBUG
533                 printk("init_srb(%p): ",init_srb);
534                 for(i=0;i<20;i++)
535                         printk("%02x ",readb(init_srb+i));
536                 printk("\n");
537 #endif
538                 
539                 /* If we get the same return response as we set, the interrupt wasn't raised and the open
540                  * timed out.
541                  */
542
543                 switch (resp = readb(init_srb+2)) {
544                 case OLYMPIC_CLEAR_RET_CODE:
545                         printk(KERN_WARNING "%s: Adapter Open time out or error.\n", dev->name) ; 
546                         goto out;
547                 case 0:
548                         open_finished = 1;
549                         break;
550                 case 0x07:
551                         if (!olympic_priv->olympic_ring_speed && open_finished) { /* Autosense , first time around */
552                                 printk(KERN_WARNING "%s: Retrying at different ring speed \n", dev->name); 
553                                 open_finished = 0 ;  
554                                 continue;
555                         }
556
557                         err = readb(init_srb+7);
558
559                         if (!olympic_priv->olympic_ring_speed && ((err & 0x0f) == 0x0d)) { 
560                                 printk(KERN_WARNING "%s: Tried to autosense ring speed with no monitors present\n",dev->name);
561                                 printk(KERN_WARNING "%s: Please try again with a specified ring speed \n",dev->name);
562                         } else {
563                                 printk(KERN_WARNING "%s: %s - %s\n", dev->name,
564                                         open_maj_error[(err & 0xf0) >> 4],
565                                         open_min_error[(err & 0x0f)]);
566                         }
567                         goto out;
568
569                 case 0x32:
570                         printk(KERN_WARNING "%s: Invalid LAA: %02x:%02x:%02x:%02x:%02x:%02x\n",
571                                 dev->name, 
572                                 olympic_priv->olympic_laa[0],
573                                 olympic_priv->olympic_laa[1],
574                                 olympic_priv->olympic_laa[2],
575                                 olympic_priv->olympic_laa[3],
576                                 olympic_priv->olympic_laa[4],
577                                 olympic_priv->olympic_laa[5]) ; 
578                         goto out;
579
580                 default:
581                         printk(KERN_WARNING "%s: Bad OPEN response: %x\n", dev->name, resp);
582                         goto out;
583
584                 }
585         } while (!(open_finished)) ; /* Will only loop if ring speed mismatch re-open attempted && autosense is on */   
586
587         if (readb(init_srb+18) & (1<<3)) 
588                 if (olympic_priv->olympic_message_level) 
589                         printk(KERN_INFO "%s: Opened in FDX Mode\n",dev->name);
590
591         if (readb(init_srb+18) & (1<<1))
592                 olympic_priv->olympic_ring_speed = 100 ; 
593         else if (readb(init_srb+18) & 1)
594                 olympic_priv->olympic_ring_speed = 16 ; 
595         else
596                 olympic_priv->olympic_ring_speed = 4 ; 
597
598         if (olympic_priv->olympic_message_level) 
599                 printk(KERN_INFO "%s: Opened in %d Mbps mode\n",dev->name, olympic_priv->olympic_ring_speed);
600
601         olympic_priv->asb = swab16(readw(init_srb+8));
602         olympic_priv->srb = swab16(readw(init_srb+10));
603         olympic_priv->arb = swab16(readw(init_srb+12));
604         olympic_priv->trb = swab16(readw(init_srb+16));
605
606         olympic_priv->olympic_receive_options = 0x01 ; 
607         olympic_priv->olympic_copy_all_options = 0 ; 
608         
609         /* setup rx ring */
610         
611         writel((3<<16),olympic_mmio+BMCTL_RWM); /* Ensure end of frame generated interrupts */ 
612
613         writel(BMCTL_RX_DIS|3,olympic_mmio+BMCTL_RWM); /* Yes, this the enables RX channel */
614
615         for(i=0;i<OLYMPIC_RX_RING_SIZE;i++) {
616
617                 struct sk_buff *skb;
618                 
619                 skb=dev_alloc_skb(olympic_priv->pkt_buf_sz);
620                 if(skb == NULL)
621                         break;
622
623                 skb->dev = dev;
624
625                 olympic_priv->olympic_rx_ring[i].buffer = cpu_to_le32(pci_map_single(olympic_priv->pdev, 
626                                                           skb->data,olympic_priv->pkt_buf_sz, PCI_DMA_FROMDEVICE)) ; 
627                 olympic_priv->olympic_rx_ring[i].res_length = cpu_to_le32(olympic_priv->pkt_buf_sz); 
628                 olympic_priv->rx_ring_skb[i]=skb;
629         }
630
631         if (i==0) {
632                 printk(KERN_WARNING "%s: Not enough memory to allocate rx buffers. Adapter disabled\n",dev->name);
633                 goto out;
634         }
635
636         olympic_priv->rx_ring_dma_addr = pci_map_single(olympic_priv->pdev,olympic_priv->olympic_rx_ring, 
637                                          sizeof(struct olympic_rx_desc) * OLYMPIC_RX_RING_SIZE, PCI_DMA_TODEVICE);
638         writel(olympic_priv->rx_ring_dma_addr, olympic_mmio+RXDESCQ);
639         writel(olympic_priv->rx_ring_dma_addr, olympic_mmio+RXCDA);
640         writew(i, olympic_mmio+RXDESCQCNT);
641                 
642         olympic_priv->rx_status_ring_dma_addr = pci_map_single(olympic_priv->pdev, olympic_priv->olympic_rx_status_ring, 
643                                                 sizeof(struct olympic_rx_status) * OLYMPIC_RX_RING_SIZE, PCI_DMA_FROMDEVICE);
644         writel(olympic_priv->rx_status_ring_dma_addr, olympic_mmio+RXSTATQ);
645         writel(olympic_priv->rx_status_ring_dma_addr, olympic_mmio+RXCSA);
646         
647         olympic_priv->rx_ring_last_received = OLYMPIC_RX_RING_SIZE - 1; /* last processed rx status */
648         olympic_priv->rx_status_last_received = OLYMPIC_RX_RING_SIZE - 1;  
649
650         writew(i, olympic_mmio+RXSTATQCNT);
651
652 #if OLYMPIC_DEBUG 
653         printk("# of rx buffers: %d, RXENQ: %x\n",i, readw(olympic_mmio+RXENQ));
654         printk("RXCSA: %x, rx_status_ring[0]: %p\n",readl(olympic_mmio+RXCSA),&olympic_priv->olympic_rx_status_ring[0]);
655         printk(" stat_ring[1]: %p, stat_ring[2]: %p, stat_ring[3]: %p\n", &(olympic_priv->olympic_rx_status_ring[1]), &(olympic_priv->olympic_rx_status_ring[2]), &(olympic_priv->olympic_rx_status_ring[3]) );
656         printk(" stat_ring[4]: %p, stat_ring[5]: %p, stat_ring[6]: %p\n", &(olympic_priv->olympic_rx_status_ring[4]), &(olympic_priv->olympic_rx_status_ring[5]), &(olympic_priv->olympic_rx_status_ring[6]) );
657         printk(" stat_ring[7]: %p\n", &(olympic_priv->olympic_rx_status_ring[7])  );
658
659         printk("RXCDA: %x, rx_ring[0]: %p\n",readl(olympic_mmio+RXCDA),&olympic_priv->olympic_rx_ring[0]);
660         printk("Rx_ring_dma_addr = %08x, rx_status_dma_addr = %08x\n",
661                 olympic_priv->rx_ring_dma_addr,olympic_priv->rx_status_ring_dma_addr) ; 
662 #endif
663
664         writew((((readw(olympic_mmio+RXENQ)) & 0x8000) ^ 0x8000) | i,olympic_mmio+RXENQ);
665
666 #if OLYMPIC_DEBUG 
667         printk("# of rx buffers: %d, RXENQ: %x\n",i, readw(olympic_mmio+RXENQ));
668         printk("RXCSA: %x, rx_ring[0]: %p\n",readl(olympic_mmio+RXCSA),&olympic_priv->olympic_rx_status_ring[0]);
669         printk("RXCDA: %x, rx_ring[0]: %p\n",readl(olympic_mmio+RXCDA),&olympic_priv->olympic_rx_ring[0]);
670 #endif 
671
672         writel(SISR_RX_STATUS | SISR_RX_NOBUF,olympic_mmio+SISR_MASK_SUM);
673
674         /* setup tx ring */
675
676         writel(BMCTL_TX1_DIS,olympic_mmio+BMCTL_RWM); /* Yes, this enables TX channel 1 */
677         for(i=0;i<OLYMPIC_TX_RING_SIZE;i++) 
678                 olympic_priv->olympic_tx_ring[i].buffer=0xdeadbeef;
679
680         olympic_priv->free_tx_ring_entries=OLYMPIC_TX_RING_SIZE;
681         olympic_priv->tx_ring_dma_addr = pci_map_single(olympic_priv->pdev,olympic_priv->olympic_tx_ring,
682                                          sizeof(struct olympic_tx_desc) * OLYMPIC_TX_RING_SIZE,PCI_DMA_TODEVICE) ; 
683         writel(olympic_priv->tx_ring_dma_addr, olympic_mmio+TXDESCQ_1);
684         writel(olympic_priv->tx_ring_dma_addr, olympic_mmio+TXCDA_1);
685         writew(OLYMPIC_TX_RING_SIZE, olympic_mmio+TXDESCQCNT_1);
686         
687         olympic_priv->tx_status_ring_dma_addr = pci_map_single(olympic_priv->pdev, olympic_priv->olympic_tx_status_ring,
688                                                 sizeof(struct olympic_tx_status) * OLYMPIC_TX_RING_SIZE, PCI_DMA_FROMDEVICE);
689         writel(olympic_priv->tx_status_ring_dma_addr,olympic_mmio+TXSTATQ_1);
690         writel(olympic_priv->tx_status_ring_dma_addr,olympic_mmio+TXCSA_1);
691         writew(OLYMPIC_TX_RING_SIZE,olympic_mmio+TXSTATQCNT_1);
692                 
693         olympic_priv->tx_ring_free=0; /* next entry in tx ring to use */
694         olympic_priv->tx_ring_last_status=OLYMPIC_TX_RING_SIZE-1; /* last processed tx status */
695
696         writel(0xffffffff, olympic_mmio+EISR_RWM) ; /* clean the eisr */
697         writel(0,olympic_mmio+EISR) ; 
698         writel(EISR_MASK_OPTIONS,olympic_mmio+EISR_MASK) ; /* enables most of the TX error interrupts */
699         writel(SISR_TX1_EOF | SISR_ADAPTER_CHECK | SISR_ARB_CMD | SISR_TRB_REPLY | SISR_ASB_FREE | SISR_ERR,olympic_mmio+SISR_MASK_SUM);
700
701 #if OLYMPIC_DEBUG 
702         printk("BMCTL: %x\n",readl(olympic_mmio+BMCTL_SUM));
703         printk("SISR MASK: %x\n",readl(olympic_mmio+SISR_MASK));
704 #endif
705
706         if (olympic_priv->olympic_network_monitor) { 
707                 u8 __iomem *oat ; 
708                 u8 __iomem *opt ; 
709                 oat = (olympic_priv->olympic_lap + olympic_priv->olympic_addr_table_addr) ; 
710                 opt = (olympic_priv->olympic_lap + olympic_priv->olympic_parms_addr) ; 
711
712                 printk("%s: Node Address: %02x:%02x:%02x:%02x:%02x:%02x\n",dev->name, 
713                         readb(oat+offsetof(struct olympic_adapter_addr_table,node_addr)), 
714                         readb(oat+offsetof(struct olympic_adapter_addr_table,node_addr)+1),
715                         readb(oat+offsetof(struct olympic_adapter_addr_table,node_addr)+2),
716                         readb(oat+offsetof(struct olympic_adapter_addr_table,node_addr)+3),
717                         readb(oat+offsetof(struct olympic_adapter_addr_table,node_addr)+4),
718                         readb(oat+offsetof(struct olympic_adapter_addr_table,node_addr)+5));
719                 printk("%s: Functional Address: %02x:%02x:%02x:%02x\n",dev->name, 
720                         readb(oat+offsetof(struct olympic_adapter_addr_table,func_addr)), 
721                         readb(oat+offsetof(struct olympic_adapter_addr_table,func_addr)+1),
722                         readb(oat+offsetof(struct olympic_adapter_addr_table,func_addr)+2),
723                         readb(oat+offsetof(struct olympic_adapter_addr_table,func_addr)+3));
724                 printk("%s: NAUN Address: %02x:%02x:%02x:%02x:%02x:%02x\n",dev->name, 
725                         readb(opt+offsetof(struct olympic_parameters_table, up_node_addr)),
726                         readb(opt+offsetof(struct olympic_parameters_table, up_node_addr)+1),
727                         readb(opt+offsetof(struct olympic_parameters_table, up_node_addr)+2),
728                         readb(opt+offsetof(struct olympic_parameters_table, up_node_addr)+3),
729                         readb(opt+offsetof(struct olympic_parameters_table, up_node_addr)+4),
730                         readb(opt+offsetof(struct olympic_parameters_table, up_node_addr)+5));
731         }
732         
733         netif_start_queue(dev);
734         return 0;
735
736 out:
737         free_irq(dev->irq, dev);
738         return -EIO;
739 }       
740
741 /*
742  *      When we enter the rx routine we do not know how many frames have been 
743  *      queued on the rx channel.  Therefore we start at the next rx status
744  *      position and travel around the receive ring until we have completed
745  *      all the frames.
746  *
747  *      This means that we may process the frame before we receive the end
748  *      of frame interrupt. This is why we always test the status instead
749  *      of blindly processing the next frame.
750  *
751  *      We also remove the last 4 bytes from the packet as well, these are
752  *      just token ring trailer info and upset protocols that don't check 
753  *      their own length, i.e. SNA. 
754  *      
755  */
756 static void olympic_rx(struct net_device *dev)
757 {
758         struct olympic_private *olympic_priv=(struct olympic_private *)dev->priv;
759         u8 __iomem *olympic_mmio=olympic_priv->olympic_mmio;
760         struct olympic_rx_status *rx_status;
761         struct olympic_rx_desc *rx_desc ; 
762         int rx_ring_last_received,length, buffer_cnt, cpy_length, frag_len;
763         struct sk_buff *skb, *skb2;
764         int i;
765
766         rx_status=&(olympic_priv->olympic_rx_status_ring[(olympic_priv->rx_status_last_received + 1) & (OLYMPIC_RX_RING_SIZE - 1)]) ; 
767  
768         while (rx_status->status_buffercnt) { 
769                 u32 l_status_buffercnt;
770
771                 olympic_priv->rx_status_last_received++ ;
772                 olympic_priv->rx_status_last_received &= (OLYMPIC_RX_RING_SIZE -1);
773 #if OLYMPIC_DEBUG
774                 printk("rx status: %x rx len: %x \n", le32_to_cpu(rx_status->status_buffercnt), le32_to_cpu(rx_status->fragmentcnt_framelen));
775 #endif
776                 length = le32_to_cpu(rx_status->fragmentcnt_framelen) & 0xffff;
777                 buffer_cnt = le32_to_cpu(rx_status->status_buffercnt) & 0xffff; 
778                 i = buffer_cnt ; /* Need buffer_cnt later for rxenq update */ 
779                 frag_len = le32_to_cpu(rx_status->fragmentcnt_framelen) >> 16; 
780
781 #if OLYMPIC_DEBUG 
782                 printk("length: %x, frag_len: %x, buffer_cnt: %x\n", length, frag_len, buffer_cnt);
783 #endif
784                 l_status_buffercnt = le32_to_cpu(rx_status->status_buffercnt);
785                 if(l_status_buffercnt & 0xC0000000) {
786                         if (l_status_buffercnt & 0x3B000000) {
787                                 if (olympic_priv->olympic_message_level) {
788                                         if (l_status_buffercnt & (1<<29))  /* Rx Frame Truncated */
789                                                 printk(KERN_WARNING "%s: Rx Frame Truncated \n",dev->name);
790                                         if (l_status_buffercnt & (1<<28)) /*Rx receive overrun */
791                                                 printk(KERN_WARNING "%s: Rx Frame Receive overrun \n",dev->name);
792                                         if (l_status_buffercnt & (1<<27)) /* No receive buffers */
793                                                 printk(KERN_WARNING "%s: No receive buffers \n",dev->name);
794                                         if (l_status_buffercnt & (1<<25)) /* Receive frame error detect */
795                                                 printk(KERN_WARNING "%s: Receive frame error detect \n",dev->name);
796                                         if (l_status_buffercnt & (1<<24)) /* Received Error Detect */
797                                                 printk(KERN_WARNING "%s: Received Error Detect \n",dev->name);
798                                 } 
799                                 olympic_priv->rx_ring_last_received += i ; 
800                                 olympic_priv->rx_ring_last_received &= (OLYMPIC_RX_RING_SIZE -1) ; 
801                                 olympic_priv->olympic_stats.rx_errors++;         
802                         } else {        
803                         
804                                 if (buffer_cnt == 1) {
805                                         skb = dev_alloc_skb(max_t(int, olympic_priv->pkt_buf_sz,length)) ; 
806                                 } else {
807                                         skb = dev_alloc_skb(length) ; 
808                                 }
809
810                                 if (skb == NULL) {
811                                         printk(KERN_WARNING "%s: Not enough memory to copy packet to upper layers. \n",dev->name) ;
812                                         olympic_priv->olympic_stats.rx_dropped++ ; 
813                                         /* Update counters even though we don't transfer the frame */
814                                         olympic_priv->rx_ring_last_received += i ; 
815                                         olympic_priv->rx_ring_last_received &= (OLYMPIC_RX_RING_SIZE -1) ;  
816                                 } else  {
817                                         /* Optimise based upon number of buffers used. 
818                                            If only one buffer is used we can simply swap the buffers around.
819                                            If more than one then we must use the new buffer and copy the information
820                                            first. Ideally all frames would be in a single buffer, this can be tuned by
821                                            altering the buffer size. If the length of the packet is less than
822                                            1500 bytes we're going to copy it over anyway to stop packets getting
823                                            dropped from sockets with buffers smaller than our pkt_buf_sz. */
824                                 
825                                         if (buffer_cnt==1) {
826                                                 olympic_priv->rx_ring_last_received++ ; 
827                                                 olympic_priv->rx_ring_last_received &= (OLYMPIC_RX_RING_SIZE -1);
828                                                 rx_ring_last_received = olympic_priv->rx_ring_last_received ;
829                                                 if (length > 1500) { 
830                                                         skb2=olympic_priv->rx_ring_skb[rx_ring_last_received] ; 
831                                                         /* unmap buffer */
832                                                         pci_unmap_single(olympic_priv->pdev,
833                                                                 le32_to_cpu(olympic_priv->olympic_rx_ring[rx_ring_last_received].buffer), 
834                                                                 olympic_priv->pkt_buf_sz,PCI_DMA_FROMDEVICE) ; 
835                                                         skb_put(skb2,length-4);
836                                                         skb2->protocol = tr_type_trans(skb2,dev);
837                                                         olympic_priv->olympic_rx_ring[rx_ring_last_received].buffer = 
838                                                                 cpu_to_le32(pci_map_single(olympic_priv->pdev, skb->data, 
839                                                                 olympic_priv->pkt_buf_sz, PCI_DMA_FROMDEVICE));
840                                                         olympic_priv->olympic_rx_ring[rx_ring_last_received].res_length = 
841                                                                 cpu_to_le32(olympic_priv->pkt_buf_sz); 
842                                                         olympic_priv->rx_ring_skb[rx_ring_last_received] = skb ; 
843                                                         netif_rx(skb2) ; 
844                                                 } else { 
845                                                         pci_dma_sync_single_for_cpu(olympic_priv->pdev,
846                                                                 le32_to_cpu(olympic_priv->olympic_rx_ring[rx_ring_last_received].buffer),
847                                                                 olympic_priv->pkt_buf_sz,PCI_DMA_FROMDEVICE) ; 
848                                                         skb_copy_from_linear_data(olympic_priv->rx_ring_skb[rx_ring_last_received],
849                                                                       skb_put(skb,length - 4),
850                                                                       length - 4);
851                                                         pci_dma_sync_single_for_device(olympic_priv->pdev,
852                                                                 le32_to_cpu(olympic_priv->olympic_rx_ring[rx_ring_last_received].buffer),
853                                                                 olympic_priv->pkt_buf_sz,PCI_DMA_FROMDEVICE) ;
854                                                         skb->protocol = tr_type_trans(skb,dev) ; 
855                                                         netif_rx(skb) ; 
856                                                 } 
857                                         } else {
858                                                 do { /* Walk the buffers */ 
859                                                         olympic_priv->rx_ring_last_received++ ; 
860                                                         olympic_priv->rx_ring_last_received &= (OLYMPIC_RX_RING_SIZE -1);
861                                                         rx_ring_last_received = olympic_priv->rx_ring_last_received ; 
862                                                         pci_dma_sync_single_for_cpu(olympic_priv->pdev,
863                                                                 le32_to_cpu(olympic_priv->olympic_rx_ring[rx_ring_last_received].buffer),
864                                                                 olympic_priv->pkt_buf_sz,PCI_DMA_FROMDEVICE) ; 
865                                                         rx_desc = &(olympic_priv->olympic_rx_ring[rx_ring_last_received]);
866                                                         cpy_length = (i == 1 ? frag_len : le32_to_cpu(rx_desc->res_length)); 
867                                                         skb_copy_from_linear_data(olympic_priv->rx_ring_skb[rx_ring_last_received],
868                                                                       skb_put(skb, cpy_length),
869                                                                       cpy_length);
870                                                         pci_dma_sync_single_for_device(olympic_priv->pdev,
871                                                                 le32_to_cpu(olympic_priv->olympic_rx_ring[rx_ring_last_received].buffer),
872                                                                 olympic_priv->pkt_buf_sz,PCI_DMA_FROMDEVICE) ;
873                                                 } while (--i) ; 
874                                                 skb_trim(skb,skb->len-4) ; 
875                                                 skb->protocol = tr_type_trans(skb,dev);
876                                                 netif_rx(skb) ; 
877                                         } 
878                                         dev->last_rx = jiffies ; 
879                                         olympic_priv->olympic_stats.rx_packets++ ; 
880                                         olympic_priv->olympic_stats.rx_bytes += length ; 
881                                 } /* if skb == null */
882                         } /* If status & 0x3b */
883
884                 } else { /*if buffercnt & 0xC */
885                         olympic_priv->rx_ring_last_received += i ; 
886                         olympic_priv->rx_ring_last_received &= (OLYMPIC_RX_RING_SIZE - 1) ; 
887                 } 
888
889                 rx_status->fragmentcnt_framelen = 0 ; 
890                 rx_status->status_buffercnt = 0 ; 
891                 rx_status = &(olympic_priv->olympic_rx_status_ring[(olympic_priv->rx_status_last_received+1) & (OLYMPIC_RX_RING_SIZE -1) ]);
892
893                 writew((((readw(olympic_mmio+RXENQ)) & 0x8000) ^ 0x8000) |  buffer_cnt , olympic_mmio+RXENQ); 
894         } /* while */
895
896 }
897
898 static void olympic_freemem(struct net_device *dev) 
899
900         struct olympic_private *olympic_priv=(struct olympic_private *)dev->priv;
901         int i;
902                         
903         for(i=0;i<OLYMPIC_RX_RING_SIZE;i++) {
904                 if (olympic_priv->rx_ring_skb[olympic_priv->rx_status_last_received] != NULL) {
905                         dev_kfree_skb_irq(olympic_priv->rx_ring_skb[olympic_priv->rx_status_last_received]);
906                         olympic_priv->rx_ring_skb[olympic_priv->rx_status_last_received] = NULL;
907                 }
908                 if (olympic_priv->olympic_rx_ring[olympic_priv->rx_status_last_received].buffer != 0xdeadbeef) {
909                         pci_unmap_single(olympic_priv->pdev, 
910                         le32_to_cpu(olympic_priv->olympic_rx_ring[olympic_priv->rx_status_last_received].buffer),
911                         olympic_priv->pkt_buf_sz, PCI_DMA_FROMDEVICE);
912                 }
913                 olympic_priv->rx_status_last_received++;
914                 olympic_priv->rx_status_last_received&=OLYMPIC_RX_RING_SIZE-1;
915         }
916         /* unmap rings */
917         pci_unmap_single(olympic_priv->pdev, olympic_priv->rx_status_ring_dma_addr, 
918                 sizeof(struct olympic_rx_status) * OLYMPIC_RX_RING_SIZE, PCI_DMA_FROMDEVICE);
919         pci_unmap_single(olympic_priv->pdev, olympic_priv->rx_ring_dma_addr,
920                 sizeof(struct olympic_rx_desc) * OLYMPIC_RX_RING_SIZE, PCI_DMA_TODEVICE);
921
922         pci_unmap_single(olympic_priv->pdev, olympic_priv->tx_status_ring_dma_addr, 
923                 sizeof(struct olympic_tx_status) * OLYMPIC_TX_RING_SIZE, PCI_DMA_FROMDEVICE);
924         pci_unmap_single(olympic_priv->pdev, olympic_priv->tx_ring_dma_addr, 
925                 sizeof(struct olympic_tx_desc) * OLYMPIC_TX_RING_SIZE, PCI_DMA_TODEVICE);
926
927         return ; 
928 }
929  
930 static irqreturn_t olympic_interrupt(int irq, void *dev_id) 
931 {
932         struct net_device *dev= (struct net_device *)dev_id;
933         struct olympic_private *olympic_priv=(struct olympic_private *)dev->priv;
934         u8 __iomem *olympic_mmio=olympic_priv->olympic_mmio;
935         u32 sisr;
936         u8 __iomem *adapter_check_area ; 
937         
938         /* 
939          *  Read sisr but don't reset it yet. 
940          *  The indication bit may have been set but the interrupt latch
941          *  bit may not be set, so we'd lose the interrupt later. 
942          */ 
943         sisr=readl(olympic_mmio+SISR) ; 
944         if (!(sisr & SISR_MI)) /* Interrupt isn't for us */ 
945                 return IRQ_NONE;
946         sisr=readl(olympic_mmio+SISR_RR) ;  /* Read & Reset sisr */ 
947
948         spin_lock(&olympic_priv->olympic_lock);
949
950         /* Hotswap gives us this on removal */
951         if (sisr == 0xffffffff) { 
952                 printk(KERN_WARNING "%s: Hotswap adapter removal.\n",dev->name) ; 
953                 spin_unlock(&olympic_priv->olympic_lock) ; 
954                 return IRQ_NONE;
955         } 
956                 
957         if (sisr & (SISR_SRB_REPLY | SISR_TX1_EOF | SISR_RX_STATUS | SISR_ADAPTER_CHECK |  
958                         SISR_ASB_FREE | SISR_ARB_CMD | SISR_TRB_REPLY | SISR_RX_NOBUF | SISR_ERR)) {  
959         
960                 /* If we ever get this the adapter is seriously dead. Only a reset is going to 
961                  * bring it back to life. We're talking pci bus errors and such like :( */ 
962                 if((sisr & SISR_ERR) && (readl(olympic_mmio+EISR) & EISR_MASK_OPTIONS)) {
963                         printk(KERN_ERR "Olympic: EISR Error, EISR=%08x\n",readl(olympic_mmio+EISR)) ; 
964                         printk(KERN_ERR "The adapter must be reset to clear this condition.\n") ; 
965                         printk(KERN_ERR "Please report this error to the driver maintainer and/\n") ; 
966                         printk(KERN_ERR "or the linux-tr mailing list.\n") ; 
967                         wake_up_interruptible(&olympic_priv->srb_wait);
968                         spin_unlock(&olympic_priv->olympic_lock) ; 
969                         return IRQ_HANDLED;
970                 } /* SISR_ERR */
971
972                 if(sisr & SISR_SRB_REPLY) {
973                         if(olympic_priv->srb_queued==1) {
974                                 wake_up_interruptible(&olympic_priv->srb_wait);
975                         } else if (olympic_priv->srb_queued==2) { 
976                                 olympic_srb_bh(dev) ; 
977                         }
978                         olympic_priv->srb_queued=0;
979                 } /* SISR_SRB_REPLY */
980
981                 /* We shouldn't ever miss the Tx interrupt, but the you never know, hence the loop to ensure
982                    we get all tx completions. */
983                 if (sisr & SISR_TX1_EOF) {
984                         while(olympic_priv->olympic_tx_status_ring[(olympic_priv->tx_ring_last_status + 1) & (OLYMPIC_TX_RING_SIZE-1)].status) { 
985                                 olympic_priv->tx_ring_last_status++;
986                                 olympic_priv->tx_ring_last_status &= (OLYMPIC_TX_RING_SIZE-1);
987                                 olympic_priv->free_tx_ring_entries++;
988                                 olympic_priv->olympic_stats.tx_bytes += olympic_priv->tx_ring_skb[olympic_priv->tx_ring_last_status]->len;
989                                 olympic_priv->olympic_stats.tx_packets++ ; 
990                                 pci_unmap_single(olympic_priv->pdev, 
991                                         le32_to_cpu(olympic_priv->olympic_tx_ring[olympic_priv->tx_ring_last_status].buffer), 
992                                         olympic_priv->tx_ring_skb[olympic_priv->tx_ring_last_status]->len,PCI_DMA_TODEVICE);
993                                 dev_kfree_skb_irq(olympic_priv->tx_ring_skb[olympic_priv->tx_ring_last_status]);
994                                 olympic_priv->olympic_tx_ring[olympic_priv->tx_ring_last_status].buffer=0xdeadbeef;
995                                 olympic_priv->olympic_tx_status_ring[olympic_priv->tx_ring_last_status].status=0;
996                         }
997                         netif_wake_queue(dev);
998                 } /* SISR_TX1_EOF */
999         
1000                 if (sisr & SISR_RX_STATUS) {
1001                         olympic_rx(dev);
1002                 } /* SISR_RX_STATUS */
1003         
1004                 if (sisr & SISR_ADAPTER_CHECK) {
1005                         netif_stop_queue(dev);
1006                         printk(KERN_WARNING "%s: Adapter Check Interrupt Raised, 8 bytes of information follow:\n", dev->name);
1007                         writel(readl(olympic_mmio+LAPWWC),olympic_mmio+LAPA);
1008                         adapter_check_area = olympic_priv->olympic_lap + ((readl(olympic_mmio+LAPWWC)) & (~0xf800)) ;
1009                         printk(KERN_WARNING "%s: Bytes %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",dev->name, readb(adapter_check_area+0), readb(adapter_check_area+1), readb(adapter_check_area+2), readb(adapter_check_area+3), readb(adapter_check_area+4), readb(adapter_check_area+5), readb(adapter_check_area+6), readb(adapter_check_area+7)) ; 
1010                         spin_unlock(&olympic_priv->olympic_lock) ; 
1011                         return IRQ_HANDLED; 
1012                 } /* SISR_ADAPTER_CHECK */
1013         
1014                 if (sisr & SISR_ASB_FREE) {
1015                         /* Wake up anything that is waiting for the asb response */  
1016                         if (olympic_priv->asb_queued) {
1017                                 olympic_asb_bh(dev) ; 
1018                         }
1019                 } /* SISR_ASB_FREE */
1020         
1021                 if (sisr & SISR_ARB_CMD) {
1022                         olympic_arb_cmd(dev) ; 
1023                 } /* SISR_ARB_CMD */
1024         
1025                 if (sisr & SISR_TRB_REPLY) {
1026                         /* Wake up anything that is waiting for the trb response */
1027                         if (olympic_priv->trb_queued) {
1028                                 wake_up_interruptible(&olympic_priv->trb_wait);
1029                         }
1030                         olympic_priv->trb_queued = 0 ; 
1031                 } /* SISR_TRB_REPLY */  
1032         
1033                 if (sisr & SISR_RX_NOBUF) {
1034                         /* According to the documentation, we don't have to do anything, but trapping it keeps it out of
1035                                    /var/log/messages.  */
1036                 } /* SISR_RX_NOBUF */
1037         } else { 
1038                 printk(KERN_WARNING "%s: Unexpected interrupt: %x\n",dev->name, sisr);
1039                 printk(KERN_WARNING "%s: SISR_MASK: %x\n",dev->name, readl(olympic_mmio+SISR_MASK)) ;
1040         } /* One if the interrupts we want */
1041         writel(SISR_MI,olympic_mmio+SISR_MASK_SUM);
1042         
1043         spin_unlock(&olympic_priv->olympic_lock) ; 
1044         return IRQ_HANDLED;
1045 }       
1046
1047 static int olympic_xmit(struct sk_buff *skb, struct net_device *dev) 
1048 {
1049         struct olympic_private *olympic_priv=(struct olympic_private *)dev->priv;
1050         u8 __iomem *olympic_mmio=olympic_priv->olympic_mmio;
1051         unsigned long flags ; 
1052
1053         spin_lock_irqsave(&olympic_priv->olympic_lock, flags);
1054
1055         netif_stop_queue(dev);
1056         
1057         if(olympic_priv->free_tx_ring_entries) {
1058                 olympic_priv->olympic_tx_ring[olympic_priv->tx_ring_free].buffer = 
1059                         cpu_to_le32(pci_map_single(olympic_priv->pdev, skb->data, skb->len,PCI_DMA_TODEVICE));
1060                 olympic_priv->olympic_tx_ring[olympic_priv->tx_ring_free].status_length = cpu_to_le32(skb->len | (0x80000000));
1061                 olympic_priv->tx_ring_skb[olympic_priv->tx_ring_free]=skb;
1062                 olympic_priv->free_tx_ring_entries--;
1063
1064                 olympic_priv->tx_ring_free++;
1065                 olympic_priv->tx_ring_free &= (OLYMPIC_TX_RING_SIZE-1);
1066                 writew((((readw(olympic_mmio+TXENQ_1)) & 0x8000) ^ 0x8000) | 1,olympic_mmio+TXENQ_1);
1067                 netif_wake_queue(dev);
1068                 spin_unlock_irqrestore(&olympic_priv->olympic_lock,flags);
1069                 return 0;
1070         } else {
1071                 spin_unlock_irqrestore(&olympic_priv->olympic_lock,flags);
1072                 return 1;
1073         } 
1074
1075 }
1076         
1077
1078 static int olympic_close(struct net_device *dev) 
1079 {
1080         struct olympic_private *olympic_priv=(struct olympic_private *)dev->priv;
1081         u8 __iomem *olympic_mmio=olympic_priv->olympic_mmio,*srb;
1082         unsigned long t,flags;
1083
1084         DECLARE_WAITQUEUE(wait,current) ; 
1085
1086         netif_stop_queue(dev);
1087         
1088         writel(olympic_priv->srb,olympic_mmio+LAPA);
1089         srb=olympic_priv->olympic_lap + (olympic_priv->srb & (~0xf800));
1090         
1091         writeb(SRB_CLOSE_ADAPTER,srb+0);
1092         writeb(0,srb+1);
1093         writeb(OLYMPIC_CLEAR_RET_CODE,srb+2);
1094
1095         add_wait_queue(&olympic_priv->srb_wait,&wait) ;
1096         set_current_state(TASK_INTERRUPTIBLE) ; 
1097
1098         spin_lock_irqsave(&olympic_priv->olympic_lock,flags);
1099         olympic_priv->srb_queued=1;
1100
1101         writel(LISR_SRB_CMD,olympic_mmio+LISR_SUM);
1102         spin_unlock_irqrestore(&olympic_priv->olympic_lock,flags);
1103
1104         while(olympic_priv->srb_queued) {
1105
1106                 t = schedule_timeout_interruptible(60*HZ);
1107
1108                 if(signal_pending(current))     {            
1109                         printk(KERN_WARNING "%s: SRB timed out.\n",dev->name);
1110                         printk(KERN_WARNING "SISR=%x MISR=%x\n",readl(olympic_mmio+SISR),readl(olympic_mmio+LISR));
1111                         olympic_priv->srb_queued=0;
1112                         break;
1113                 }
1114
1115                 if (t == 0) { 
1116                         printk(KERN_WARNING "%s: SRB timed out. May not be fatal. \n",dev->name) ; 
1117                 } 
1118                 olympic_priv->srb_queued=0;
1119         }
1120         remove_wait_queue(&olympic_priv->srb_wait,&wait) ; 
1121
1122         olympic_priv->rx_status_last_received++;
1123         olympic_priv->rx_status_last_received&=OLYMPIC_RX_RING_SIZE-1;
1124
1125         olympic_freemem(dev) ;  
1126
1127         /* reset tx/rx fifo's and busmaster logic */
1128
1129         writel(readl(olympic_mmio+BCTL)|(3<<13),olympic_mmio+BCTL);
1130         udelay(1);
1131         writel(readl(olympic_mmio+BCTL)&~(3<<13),olympic_mmio+BCTL);
1132
1133 #if OLYMPIC_DEBUG
1134         {
1135         int i ; 
1136         printk("srb(%p): ",srb);
1137         for(i=0;i<4;i++)
1138                 printk("%x ",readb(srb+i));
1139         printk("\n");
1140         }
1141 #endif
1142         free_irq(dev->irq,dev);
1143
1144         return 0;
1145         
1146 }
1147
1148 static void olympic_set_rx_mode(struct net_device *dev) 
1149 {
1150         struct olympic_private *olympic_priv = (struct olympic_private *) dev->priv ; 
1151         u8 __iomem *olympic_mmio = olympic_priv->olympic_mmio ; 
1152         u8 options = 0; 
1153         u8 __iomem *srb;
1154         struct dev_mc_list *dmi ; 
1155         unsigned char dev_mc_address[4] ; 
1156         int i ; 
1157
1158         writel(olympic_priv->srb,olympic_mmio+LAPA);
1159         srb=olympic_priv->olympic_lap + (olympic_priv->srb & (~0xf800));
1160         options = olympic_priv->olympic_copy_all_options; 
1161
1162         if (dev->flags&IFF_PROMISC)  
1163                 options |= 0x61 ;
1164         else
1165                 options &= ~0x61 ; 
1166
1167         /* Only issue the srb if there is a change in options */
1168
1169         if ((options ^ olympic_priv->olympic_copy_all_options)) { 
1170         
1171                 /* Now to issue the srb command to alter the copy.all.options */
1172         
1173                 writeb(SRB_MODIFY_RECEIVE_OPTIONS,srb);
1174                 writeb(0,srb+1);
1175                 writeb(OLYMPIC_CLEAR_RET_CODE,srb+2);
1176                 writeb(0,srb+3);
1177                 writeb(olympic_priv->olympic_receive_options,srb+4);
1178                 writeb(options,srb+5);
1179
1180                 olympic_priv->srb_queued=2; /* Can't sleep, use srb_bh */
1181
1182                 writel(LISR_SRB_CMD,olympic_mmio+LISR_SUM);
1183
1184                 olympic_priv->olympic_copy_all_options = options ;
1185                 
1186                 return ;  
1187         } 
1188
1189         /* Set the functional addresses we need for multicast */
1190
1191         dev_mc_address[0] = dev_mc_address[1] = dev_mc_address[2] = dev_mc_address[3] = 0 ; 
1192
1193         for (i=0,dmi=dev->mc_list;i < dev->mc_count; i++,dmi = dmi->next) { 
1194                 dev_mc_address[0] |= dmi->dmi_addr[2] ; 
1195                 dev_mc_address[1] |= dmi->dmi_addr[3] ; 
1196                 dev_mc_address[2] |= dmi->dmi_addr[4] ; 
1197                 dev_mc_address[3] |= dmi->dmi_addr[5] ; 
1198         }
1199
1200         writeb(SRB_SET_FUNC_ADDRESS,srb+0);
1201         writeb(0,srb+1);
1202         writeb(OLYMPIC_CLEAR_RET_CODE,srb+2);
1203         writeb(0,srb+3);
1204         writeb(0,srb+4);
1205         writeb(0,srb+5);
1206         writeb(dev_mc_address[0],srb+6);
1207         writeb(dev_mc_address[1],srb+7);
1208         writeb(dev_mc_address[2],srb+8);
1209         writeb(dev_mc_address[3],srb+9);
1210
1211         olympic_priv->srb_queued = 2 ;
1212         writel(LISR_SRB_CMD,olympic_mmio+LISR_SUM);
1213
1214 }
1215
1216 static void olympic_srb_bh(struct net_device *dev) 
1217
1218         struct olympic_private *olympic_priv = (struct olympic_private *) dev->priv ; 
1219         u8 __iomem *olympic_mmio = olympic_priv->olympic_mmio ; 
1220         u8 __iomem *srb;
1221
1222         writel(olympic_priv->srb,olympic_mmio+LAPA);
1223         srb=olympic_priv->olympic_lap + (olympic_priv->srb & (~0xf800));
1224
1225         switch (readb(srb)) { 
1226
1227                 /* SRB_MODIFY_RECEIVE_OPTIONS i.e. set_multicast_list options (promiscuous) 
1228                  * At some point we should do something if we get an error, such as
1229                  * resetting the IFF_PROMISC flag in dev
1230                  */
1231
1232                 case SRB_MODIFY_RECEIVE_OPTIONS:
1233                         switch (readb(srb+2)) { 
1234                                 case 0x01:
1235                                         printk(KERN_WARNING "%s: Unrecognized srb command\n",dev->name) ; 
1236                                         break ; 
1237                                 case 0x04:
1238                                         printk(KERN_WARNING "%s: Adapter must be open for this operation, doh!!\n",dev->name);
1239                                         break ; 
1240                                 default:
1241                                         if (olympic_priv->olympic_message_level) 
1242                                                 printk(KERN_WARNING "%s: Receive Options Modified to %x,%x\n",dev->name,olympic_priv->olympic_copy_all_options, olympic_priv->olympic_receive_options) ; 
1243                                         break ;         
1244                         } /* switch srb[2] */ 
1245                         break ;
1246                 
1247                 /* SRB_SET_GROUP_ADDRESS - Multicast group setting 
1248                  */
1249
1250                 case SRB_SET_GROUP_ADDRESS:
1251                         switch (readb(srb+2)) { 
1252                                 case 0x00:
1253                                         break ; 
1254                                 case 0x01:
1255                                         printk(KERN_WARNING "%s: Unrecognized srb command \n",dev->name) ; 
1256                                         break ;
1257                                 case 0x04:
1258                                         printk(KERN_WARNING "%s: Adapter must be open for this operation, doh!!\n",dev->name); 
1259                                         break ;
1260                                 case 0x3c:
1261                                         printk(KERN_WARNING "%s: Group/Functional address indicator bits not set correctly\n",dev->name) ; 
1262                                         break ;
1263                                 case 0x3e: /* If we ever implement individual multicast addresses, will need to deal with this */
1264                                         printk(KERN_WARNING "%s: Group address registers full\n",dev->name) ; 
1265                                         break ;  
1266                                 case 0x55:
1267                                         printk(KERN_INFO "%s: Group Address already set.\n",dev->name) ; 
1268                                         break ;
1269                                 default:
1270                                         break ; 
1271                         } /* switch srb[2] */ 
1272                         break ; 
1273
1274                 /* SRB_RESET_GROUP_ADDRESS - Remove a multicast address from group list
1275                  */
1276
1277                 case SRB_RESET_GROUP_ADDRESS:
1278                         switch (readb(srb+2)) { 
1279                                 case 0x00:
1280                                         break ; 
1281                                 case 0x01:
1282                                         printk(KERN_WARNING "%s: Unrecognized srb command \n",dev->name) ; 
1283                                         break ; 
1284                                 case 0x04:
1285                                         printk(KERN_WARNING "%s: Adapter must be open for this operation, doh!!\n",dev->name) ; 
1286                                         break ; 
1287                                 case 0x39: /* Must deal with this if individual multicast addresses used */
1288                                         printk(KERN_INFO "%s: Group address not found \n",dev->name); 
1289                                         break ;
1290                                 default:
1291                                         break ; 
1292                         } /* switch srb[2] */
1293                         break ; 
1294
1295                 
1296                 /* SRB_SET_FUNC_ADDRESS - Called by the set_rx_mode 
1297                  */
1298
1299                 case SRB_SET_FUNC_ADDRESS:
1300                         switch (readb(srb+2)) { 
1301                                 case 0x00:
1302                                         if (olympic_priv->olympic_message_level)
1303                                                 printk(KERN_INFO "%s: Functional Address Mask Set \n",dev->name) ; 
1304                                         break ;
1305                                 case 0x01:
1306                                         printk(KERN_WARNING "%s: Unrecognized srb command \n",dev->name) ; 
1307                                         break ; 
1308                                 case 0x04:
1309                                         printk(KERN_WARNING "%s: Adapter must be open for this operation, doh!!\n",dev->name) ; 
1310                                         break ; 
1311                                 default:
1312                                         break ; 
1313                         } /* switch srb[2] */
1314                         break ; 
1315         
1316                 /* SRB_READ_LOG - Read and reset the adapter error counters
1317                  */
1318
1319                 case SRB_READ_LOG:
1320                         switch (readb(srb+2)) { 
1321                                 case 0x00: 
1322                                         if (olympic_priv->olympic_message_level) 
1323                                                 printk(KERN_INFO "%s: Read Log issued\n",dev->name) ; 
1324                                         break ; 
1325                                 case 0x01:
1326                                         printk(KERN_WARNING "%s: Unrecognized srb command \n",dev->name) ; 
1327                                         break ; 
1328                                 case 0x04:
1329                                         printk(KERN_WARNING "%s: Adapter must be open for this operation, doh!!\n",dev->name) ; 
1330                                         break ; 
1331                         
1332                         } /* switch srb[2] */
1333                         break ; 
1334                 
1335                 /* SRB_READ_SR_COUNTERS - Read and reset the source routing bridge related counters */
1336
1337                 case SRB_READ_SR_COUNTERS:
1338                         switch (readb(srb+2)) { 
1339                                 case 0x00: 
1340                                         if (olympic_priv->olympic_message_level) 
1341                                                 printk(KERN_INFO "%s: Read Source Routing Counters issued\n",dev->name) ; 
1342                                         break ; 
1343                                 case 0x01:
1344                                         printk(KERN_WARNING "%s: Unrecognized srb command \n",dev->name) ; 
1345                                         break ; 
1346                                 case 0x04:
1347                                         printk(KERN_WARNING "%s: Adapter must be open for this operation, doh!!\n",dev->name) ; 
1348                                         break ; 
1349                                 default:
1350                                         break ; 
1351                         } /* switch srb[2] */
1352                         break ;
1353  
1354                 default:
1355                         printk(KERN_WARNING "%s: Unrecognized srb bh return value.\n",dev->name);
1356                         break ; 
1357         } /* switch srb[0] */
1358
1359
1360
1361 static struct net_device_stats * olympic_get_stats(struct net_device *dev)
1362 {
1363         struct olympic_private *olympic_priv ;
1364         olympic_priv=(struct olympic_private *) dev->priv;
1365         return (struct net_device_stats *) &olympic_priv->olympic_stats; 
1366 }
1367
1368 static int olympic_set_mac_address (struct net_device *dev, void *addr) 
1369 {
1370         struct sockaddr *saddr = addr ; 
1371         struct olympic_private *olympic_priv = (struct olympic_private *)dev->priv ; 
1372
1373         if (netif_running(dev)) { 
1374                 printk(KERN_WARNING "%s: Cannot set mac/laa address while card is open\n", dev->name) ; 
1375                 return -EIO ; 
1376         }
1377
1378         memcpy(olympic_priv->olympic_laa, saddr->sa_data,dev->addr_len) ; 
1379         
1380         if (olympic_priv->olympic_message_level) { 
1381                 printk(KERN_INFO "%s: MAC/LAA Set to  = %x.%x.%x.%x.%x.%x\n",dev->name, olympic_priv->olympic_laa[0],
1382                 olympic_priv->olympic_laa[1], olympic_priv->olympic_laa[2],
1383                 olympic_priv->olympic_laa[3], olympic_priv->olympic_laa[4],
1384                 olympic_priv->olympic_laa[5]);
1385         } 
1386
1387         return 0 ; 
1388 }
1389
1390 static void olympic_arb_cmd(struct net_device *dev)
1391 {
1392         struct olympic_private *olympic_priv = (struct olympic_private *) dev->priv;
1393         u8 __iomem *olympic_mmio=olympic_priv->olympic_mmio;
1394         u8 __iomem *arb_block, *asb_block, *srb  ; 
1395         u8 header_len ; 
1396         u16 frame_len, buffer_len ;
1397         struct sk_buff *mac_frame ;  
1398         u8 __iomem *buf_ptr ;
1399         u8 __iomem *frame_data ;  
1400         u16 buff_off ; 
1401         u16 lan_status = 0, lan_status_diff  ; /* Initialize to stop compiler warning */
1402         u8 fdx_prot_error ; 
1403         u16 next_ptr;
1404
1405         arb_block = (olympic_priv->olympic_lap + olympic_priv->arb) ; 
1406         asb_block = (olympic_priv->olympic_lap + olympic_priv->asb) ; 
1407         srb = (olympic_priv->olympic_lap + olympic_priv->srb) ; 
1408         
1409         if (readb(arb_block+0) == ARB_RECEIVE_DATA) { /* Receive.data, MAC frames */
1410
1411                 header_len = readb(arb_block+8) ; /* 802.5 Token-Ring Header Length */  
1412                 frame_len = swab16(readw(arb_block + 10)) ; 
1413
1414                 buff_off = swab16(readw(arb_block + 6)) ;
1415                 
1416                 buf_ptr = olympic_priv->olympic_lap + buff_off ; 
1417
1418 #if OLYMPIC_DEBUG
1419 {
1420                 int i;
1421                 frame_data = buf_ptr+offsetof(struct mac_receive_buffer,frame_data) ; 
1422
1423                 for (i=0 ;  i < 14 ; i++) { 
1424                         printk("Loc %d = %02x\n",i,readb(frame_data + i)); 
1425                 }
1426
1427                 printk("next %04x, fs %02x, len %04x \n",readw(buf_ptr+offsetof(struct mac_receive_buffer,next)), readb(buf_ptr+offsetof(struct mac_receive_buffer,frame_status)), readw(buf_ptr+offsetof(struct mac_receive_buffer,buffer_length))); 
1428 }
1429 #endif 
1430                 mac_frame = dev_alloc_skb(frame_len) ; 
1431                 if (!mac_frame) {
1432                         printk(KERN_WARNING "%s: Memory squeeze, dropping frame.\n", dev->name);
1433                         goto drop_frame;
1434                 }
1435
1436                 /* Walk the buffer chain, creating the frame */
1437
1438                 do {
1439                         frame_data = buf_ptr+offsetof(struct mac_receive_buffer,frame_data) ; 
1440                         buffer_len = swab16(readw(buf_ptr+offsetof(struct mac_receive_buffer,buffer_length))); 
1441                         memcpy_fromio(skb_put(mac_frame, buffer_len), frame_data , buffer_len ) ;
1442                         next_ptr=readw(buf_ptr+offsetof(struct mac_receive_buffer,next)); 
1443                 } while (next_ptr && (buf_ptr=olympic_priv->olympic_lap + ntohs(next_ptr)));
1444
1445                 mac_frame->protocol = tr_type_trans(mac_frame, dev);
1446
1447                 if (olympic_priv->olympic_network_monitor) { 
1448                         struct trh_hdr *mac_hdr ; 
1449                         printk(KERN_WARNING "%s: Received MAC Frame, details: \n",dev->name) ;
1450                         mac_hdr = tr_hdr(mac_frame);
1451                         printk(KERN_WARNING "%s: MAC Frame Dest. Addr: %02x:%02x:%02x:%02x:%02x:%02x \n", dev->name , mac_hdr->daddr[0], mac_hdr->daddr[1], mac_hdr->daddr[2], mac_hdr->daddr[3], mac_hdr->daddr[4], mac_hdr->daddr[5]) ; 
1452                         printk(KERN_WARNING "%s: MAC Frame Srce. Addr: %02x:%02x:%02x:%02x:%02x:%02x \n", dev->name , mac_hdr->saddr[0], mac_hdr->saddr[1], mac_hdr->saddr[2], mac_hdr->saddr[3], mac_hdr->saddr[4], mac_hdr->saddr[5]) ; 
1453                 }
1454                 netif_rx(mac_frame);
1455                 dev->last_rx = jiffies;
1456
1457 drop_frame:
1458                 /* Now tell the card we have dealt with the received frame */
1459
1460                 /* Set LISR Bit 1 */
1461                 writel(LISR_ARB_FREE,olympic_priv->olympic_mmio + LISR_SUM);
1462
1463                 /* Is the ASB free ? */         
1464                 
1465                 if (readb(asb_block + 2) != 0xff) { 
1466                         olympic_priv->asb_queued = 1 ; 
1467                         writel(LISR_ASB_FREE_REQ,olympic_priv->olympic_mmio+LISR_SUM); 
1468                         return ;        
1469                         /* Drop out and wait for the bottom half to be run */
1470                 }
1471                 
1472                 writeb(ASB_RECEIVE_DATA,asb_block); /* Receive data */
1473                 writeb(OLYMPIC_CLEAR_RET_CODE,asb_block+2); /* Necessary ?? */
1474                 writeb(readb(arb_block+6),asb_block+6); /* Must send the address back to the adapter */
1475                 writeb(readb(arb_block+7),asb_block+7); /* To let it know we have dealt with the data */                
1476
1477                 writel(LISR_ASB_REPLY | LISR_ASB_FREE_REQ,olympic_priv->olympic_mmio+LISR_SUM);
1478                 
1479                 olympic_priv->asb_queued = 2 ; 
1480         
1481                 return ;        
1482                 
1483         } else if (readb(arb_block) == ARB_LAN_CHANGE_STATUS) { /* Lan.change.status */
1484                 lan_status = swab16(readw(arb_block+6));
1485                 fdx_prot_error = readb(arb_block+8) ; 
1486                 
1487                 /* Issue ARB Free */
1488                 writel(LISR_ARB_FREE,olympic_priv->olympic_mmio+LISR_SUM);
1489
1490                 lan_status_diff = olympic_priv->olympic_lan_status ^ lan_status ; 
1491
1492                 if (lan_status_diff & (LSC_LWF | LSC_ARW | LSC_FPE | LSC_RR) ) { 
1493                         if (lan_status_diff & LSC_LWF) 
1494                                         printk(KERN_WARNING "%s: Short circuit detected on the lobe\n",dev->name);
1495                         if (lan_status_diff & LSC_ARW) 
1496                                         printk(KERN_WARNING "%s: Auto removal error\n",dev->name);
1497                         if (lan_status_diff & LSC_FPE)
1498                                         printk(KERN_WARNING "%s: FDX Protocol Error\n",dev->name);
1499                         if (lan_status_diff & LSC_RR) 
1500                                         printk(KERN_WARNING "%s: Force remove MAC frame received\n",dev->name);
1501                 
1502                         /* Adapter has been closed by the hardware */
1503                 
1504                         /* reset tx/rx fifo's and busmaster logic */
1505
1506                         writel(readl(olympic_mmio+BCTL)|(3<<13),olympic_mmio+BCTL);
1507                         udelay(1);
1508                         writel(readl(olympic_mmio+BCTL)&~(3<<13),olympic_mmio+BCTL);
1509                         netif_stop_queue(dev);
1510                         olympic_priv->srb = readw(olympic_priv->olympic_lap + LAPWWO) ; 
1511                         printk(KERN_WARNING "%s: Adapter has been closed \n", dev->name) ; 
1512                 } /* If serious error */
1513                 
1514                 if (olympic_priv->olympic_message_level) { 
1515                         if (lan_status_diff & LSC_SIG_LOSS) 
1516                                         printk(KERN_WARNING "%s: No receive signal detected \n", dev->name) ; 
1517                         if (lan_status_diff & LSC_HARD_ERR)
1518                                         printk(KERN_INFO "%s: Beaconing \n",dev->name);
1519                         if (lan_status_diff & LSC_SOFT_ERR)
1520                                         printk(KERN_WARNING "%s: Adapter transmitted Soft Error Report Mac Frame \n",dev->name);
1521                         if (lan_status_diff & LSC_TRAN_BCN) 
1522                                         printk(KERN_INFO "%s: We are tranmitting the beacon, aaah\n",dev->name);
1523                         if (lan_status_diff & LSC_SS) 
1524                                         printk(KERN_INFO "%s: Single Station on the ring \n", dev->name);
1525                         if (lan_status_diff & LSC_RING_REC)
1526                                         printk(KERN_INFO "%s: Ring recovery ongoing\n",dev->name);
1527                         if (lan_status_diff & LSC_FDX_MODE)
1528                                         printk(KERN_INFO "%s: Operating in FDX mode\n",dev->name);
1529                 }       
1530                 
1531                 if (lan_status_diff & LSC_CO) { 
1532                                         
1533                                 if (olympic_priv->olympic_message_level) 
1534                                         printk(KERN_INFO "%s: Counter Overflow \n", dev->name);
1535                                         
1536                                 /* Issue READ.LOG command */
1537
1538                                 writeb(SRB_READ_LOG, srb);
1539                                 writeb(0,srb+1);
1540                                 writeb(OLYMPIC_CLEAR_RET_CODE,srb+2);
1541                                 writeb(0,srb+3);
1542                                 writeb(0,srb+4);
1543                                 writeb(0,srb+5);
1544                                         
1545                                 olympic_priv->srb_queued=2; /* Can't sleep, use srb_bh */
1546
1547                                 writel(LISR_SRB_CMD,olympic_mmio+LISR_SUM);
1548                                         
1549                 }
1550
1551                 if (lan_status_diff & LSC_SR_CO) { 
1552
1553                                 if (olympic_priv->olympic_message_level)
1554                                         printk(KERN_INFO "%s: Source routing counters overflow\n", dev->name);
1555
1556                                 /* Issue a READ.SR.COUNTERS */
1557                                 
1558                                 writeb(SRB_READ_SR_COUNTERS,srb);
1559                                 writeb(0,srb+1);
1560                                 writeb(OLYMPIC_CLEAR_RET_CODE,srb+2);
1561                                 writeb(0,srb+3);
1562                                 
1563                                 olympic_priv->srb_queued=2; /* Can't sleep, use srb_bh */
1564
1565                                 writel(LISR_SRB_CMD,olympic_mmio+LISR_SUM);
1566
1567                 }
1568
1569                 olympic_priv->olympic_lan_status = lan_status ; 
1570         
1571         }  /* Lan.change.status */
1572         else
1573                 printk(KERN_WARNING "%s: Unknown arb command \n", dev->name);
1574 }
1575
1576 static void olympic_asb_bh(struct net_device *dev) 
1577 {
1578         struct olympic_private *olympic_priv = (struct olympic_private *) dev->priv ; 
1579         u8 __iomem *arb_block, *asb_block ; 
1580
1581         arb_block = (olympic_priv->olympic_lap + olympic_priv->arb) ; 
1582         asb_block = (olympic_priv->olympic_lap + olympic_priv->asb) ; 
1583
1584         if (olympic_priv->asb_queued == 1) {   /* Dropped through the first time */
1585
1586                 writeb(ASB_RECEIVE_DATA,asb_block); /* Receive data */
1587                 writeb(OLYMPIC_CLEAR_RET_CODE,asb_block+2); /* Necessary ?? */
1588                 writeb(readb(arb_block+6),asb_block+6); /* Must send the address back to the adapter */
1589                 writeb(readb(arb_block+7),asb_block+7); /* To let it know we have dealt with the data */                
1590
1591                 writel(LISR_ASB_REPLY | LISR_ASB_FREE_REQ,olympic_priv->olympic_mmio+LISR_SUM);
1592                 olympic_priv->asb_queued = 2 ; 
1593
1594                 return ; 
1595         }
1596
1597         if (olympic_priv->asb_queued == 2) { 
1598                 switch (readb(asb_block+2)) {
1599                         case 0x01:
1600                                 printk(KERN_WARNING "%s: Unrecognized command code \n", dev->name);
1601                                 break ;
1602                         case 0x26:
1603                                 printk(KERN_WARNING "%s: Unrecognized buffer address \n", dev->name);
1604                                 break ;
1605                         case 0xFF:
1606                                 /* Valid response, everything should be ok again */
1607                                 break ;
1608                         default:
1609                                 printk(KERN_WARNING "%s: Invalid return code in asb\n",dev->name);
1610                                 break ;
1611                 }
1612         }
1613         olympic_priv->asb_queued = 0 ; 
1614 }
1615  
1616 static int olympic_change_mtu(struct net_device *dev, int mtu) 
1617 {
1618         struct olympic_private *olympic_priv = (struct olympic_private *) dev->priv;
1619         u16 max_mtu ; 
1620
1621         if (olympic_priv->olympic_ring_speed == 4)
1622                 max_mtu = 4500 ; 
1623         else
1624                 max_mtu = 18000 ; 
1625         
1626         if (mtu > max_mtu)
1627                 return -EINVAL ; 
1628         if (mtu < 100) 
1629                 return -EINVAL ; 
1630
1631         dev->mtu = mtu ; 
1632         olympic_priv->pkt_buf_sz = mtu + TR_HLEN ; 
1633
1634         return 0 ; 
1635 }
1636
1637 static int olympic_proc_info(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
1638 {
1639         struct net_device *dev = (struct net_device *)data ; 
1640         struct olympic_private *olympic_priv=(struct olympic_private *)dev->priv;
1641         u8 __iomem *oat = (olympic_priv->olympic_lap + olympic_priv->olympic_addr_table_addr) ; 
1642         u8 __iomem *opt = (olympic_priv->olympic_lap + olympic_priv->olympic_parms_addr) ; 
1643         int size = 0 ; 
1644         int len=0;
1645         off_t begin=0;
1646         off_t pos=0;
1647         
1648         size = sprintf(buffer, 
1649                 "IBM Pit/Pit-Phy/Olympic Chipset Token Ring Adapter %s\n",dev->name);
1650         size += sprintf(buffer+size, "\n%6s: Adapter Address   : Node Address      : Functional Addr\n",
1651            dev->name); 
1652
1653         size += sprintf(buffer+size, "%6s: %02x:%02x:%02x:%02x:%02x:%02x : %02x:%02x:%02x:%02x:%02x:%02x : %02x:%02x:%02x:%02x\n",
1654            dev->name,
1655            dev->dev_addr[0],
1656            dev->dev_addr[1],
1657            dev->dev_addr[2],
1658            dev->dev_addr[3],
1659            dev->dev_addr[4],
1660            dev->dev_addr[5],
1661            readb(oat+offsetof(struct olympic_adapter_addr_table,node_addr)), 
1662            readb(oat+offsetof(struct olympic_adapter_addr_table,node_addr)+1),
1663            readb(oat+offsetof(struct olympic_adapter_addr_table,node_addr)+2),
1664            readb(oat+offsetof(struct olympic_adapter_addr_table,node_addr)+3),
1665            readb(oat+offsetof(struct olympic_adapter_addr_table,node_addr)+4),
1666            readb(oat+offsetof(struct olympic_adapter_addr_table,node_addr)+5),
1667            readb(oat+offsetof(struct olympic_adapter_addr_table,func_addr)), 
1668            readb(oat+offsetof(struct olympic_adapter_addr_table,func_addr)+1),
1669            readb(oat+offsetof(struct olympic_adapter_addr_table,func_addr)+2),
1670            readb(oat+offsetof(struct olympic_adapter_addr_table,func_addr)+3));
1671          
1672         size += sprintf(buffer+size, "\n%6s: Token Ring Parameters Table:\n", dev->name);
1673
1674         size += sprintf(buffer+size, "%6s: Physical Addr : Up Node Address   : Poll Address      : AccPri : Auth Src : Att Code :\n",
1675           dev->name) ; 
1676            
1677         size += sprintf(buffer+size, "%6s: %02x:%02x:%02x:%02x   : %02x:%02x:%02x:%02x:%02x:%02x : %02x:%02x:%02x:%02x:%02x:%02x : %04x   : %04x     :  %04x    :\n",
1678           dev->name,
1679           readb(opt+offsetof(struct olympic_parameters_table, phys_addr)),
1680           readb(opt+offsetof(struct olympic_parameters_table, phys_addr)+1),
1681           readb(opt+offsetof(struct olympic_parameters_table, phys_addr)+2),
1682           readb(opt+offsetof(struct olympic_parameters_table, phys_addr)+3),
1683           readb(opt+offsetof(struct olympic_parameters_table, up_node_addr)),
1684           readb(opt+offsetof(struct olympic_parameters_table, up_node_addr)+1),
1685           readb(opt+offsetof(struct olympic_parameters_table, up_node_addr)+2),
1686           readb(opt+offsetof(struct olympic_parameters_table, up_node_addr)+3),
1687           readb(opt+offsetof(struct olympic_parameters_table, up_node_addr)+4),
1688           readb(opt+offsetof(struct olympic_parameters_table, up_node_addr)+5),
1689           readb(opt+offsetof(struct olympic_parameters_table, poll_addr)),
1690           readb(opt+offsetof(struct olympic_parameters_table, poll_addr)+1),
1691           readb(opt+offsetof(struct olympic_parameters_table, poll_addr)+2),
1692           readb(opt+offsetof(struct olympic_parameters_table, poll_addr)+3),
1693           readb(opt+offsetof(struct olympic_parameters_table, poll_addr)+4),
1694           readb(opt+offsetof(struct olympic_parameters_table, poll_addr)+5),
1695           swab16(readw(opt+offsetof(struct olympic_parameters_table, acc_priority))),
1696           swab16(readw(opt+offsetof(struct olympic_parameters_table, auth_source_class))),
1697           swab16(readw(opt+offsetof(struct olympic_parameters_table, att_code))));
1698
1699         size += sprintf(buffer+size, "%6s: Source Address    : Bcn T : Maj. V : Lan St : Lcl Rg : Mon Err : Frame Correl : \n",
1700           dev->name) ; 
1701         
1702         size += sprintf(buffer+size, "%6s: %02x:%02x:%02x:%02x:%02x:%02x : %04x  : %04x   : %04x   : %04x   : %04x    :     %04x     : \n",
1703           dev->name,
1704           readb(opt+offsetof(struct olympic_parameters_table, source_addr)),
1705           readb(opt+offsetof(struct olympic_parameters_table, source_addr)+1),
1706           readb(opt+offsetof(struct olympic_parameters_table, source_addr)+2),
1707           readb(opt+offsetof(struct olympic_parameters_table, source_addr)+3),
1708           readb(opt+offsetof(struct olympic_parameters_table, source_addr)+4),
1709           readb(opt+offsetof(struct olympic_parameters_table, source_addr)+5),
1710           swab16(readw(opt+offsetof(struct olympic_parameters_table, beacon_type))),
1711           swab16(readw(opt+offsetof(struct olympic_parameters_table, major_vector))),
1712           swab16(readw(opt+offsetof(struct olympic_parameters_table, lan_status))),
1713           swab16(readw(opt+offsetof(struct olympic_parameters_table, local_ring))),
1714           swab16(readw(opt+offsetof(struct olympic_parameters_table, mon_error))),
1715           swab16(readw(opt+offsetof(struct olympic_parameters_table, frame_correl))));
1716
1717         size += sprintf(buffer+size, "%6s: Beacon Details :  Tx  :  Rx  : NAUN Node Address : NAUN Node Phys : \n",
1718           dev->name) ; 
1719
1720         size += sprintf(buffer+size, "%6s:                :  %02x  :  %02x  : %02x:%02x:%02x:%02x:%02x:%02x : %02x:%02x:%02x:%02x    : \n",
1721           dev->name,
1722           swab16(readw(opt+offsetof(struct olympic_parameters_table, beacon_transmit))),
1723           swab16(readw(opt+offsetof(struct olympic_parameters_table, beacon_receive))),
1724           readb(opt+offsetof(struct olympic_parameters_table, beacon_naun)),
1725           readb(opt+offsetof(struct olympic_parameters_table, beacon_naun)+1),
1726           readb(opt+offsetof(struct olympic_parameters_table, beacon_naun)+2),
1727           readb(opt+offsetof(struct olympic_parameters_table, beacon_naun)+3),
1728           readb(opt+offsetof(struct olympic_parameters_table, beacon_naun)+4),
1729           readb(opt+offsetof(struct olympic_parameters_table, beacon_naun)+5),
1730           readb(opt+offsetof(struct olympic_parameters_table, beacon_phys)),
1731           readb(opt+offsetof(struct olympic_parameters_table, beacon_phys)+1),
1732           readb(opt+offsetof(struct olympic_parameters_table, beacon_phys)+2),
1733           readb(opt+offsetof(struct olympic_parameters_table, beacon_phys)+3));
1734
1735         len=size;
1736         pos=begin+size;
1737         if (pos<offset) {
1738                 len=0;
1739                 begin=pos;
1740         }
1741         *start=buffer+(offset-begin);   /* Start of wanted data */
1742         len-=(offset-begin);            /* Start slop */
1743         if(len>length)
1744                 len=length;             /* Ending slop */
1745         return len;
1746 }
1747
1748 static void __devexit olympic_remove_one(struct pci_dev *pdev) 
1749 {
1750         struct net_device *dev = pci_get_drvdata(pdev) ; 
1751         struct olympic_private *olympic_priv=(struct olympic_private *)dev->priv;
1752
1753         if (olympic_priv->olympic_network_monitor) { 
1754                 char proc_name[20] ; 
1755                 strcpy(proc_name,"net/olympic_") ; 
1756                 strcat(proc_name,dev->name) ;
1757                 remove_proc_entry(proc_name,NULL); 
1758         }
1759         unregister_netdev(dev) ; 
1760         iounmap(olympic_priv->olympic_mmio) ; 
1761         iounmap(olympic_priv->olympic_lap) ; 
1762         pci_release_regions(pdev) ;
1763         pci_set_drvdata(pdev,NULL) ;    
1764         free_netdev(dev) ; 
1765 }
1766
1767 static struct pci_driver olympic_driver = { 
1768         .name           = "olympic",
1769         .id_table       = olympic_pci_tbl,
1770         .probe          = olympic_probe,
1771         .remove         = __devexit_p(olympic_remove_one),
1772 };
1773
1774 static int __init olympic_pci_init(void) 
1775 {
1776         return pci_register_driver(&olympic_driver) ;
1777 }
1778
1779 static void __exit olympic_pci_cleanup(void)
1780 {
1781         pci_unregister_driver(&olympic_driver) ; 
1782 }       
1783
1784
1785 module_init(olympic_pci_init) ; 
1786 module_exit(olympic_pci_cleanup) ; 
1787
1788 MODULE_LICENSE("GPL");