fix console change race exposed by CFS
[pandora-kernel.git] / drivers / net / wan / sbni.c
1 /* sbni.c:  Granch SBNI12 leased line adapters driver for linux
2  *
3  *      Written 2001 by Denis I.Timofeev (timofeev@granch.ru)
4  *
5  *      Previous versions were written by Yaroslav Polyakov,
6  *      Alexey Zverev and Max Khon.
7  *
8  *      Driver supports SBNI12-02,-04,-05,-10,-11 cards, single and
9  *      double-channel, PCI and ISA modifications.
10  *      More info and useful utilities to work with SBNI12 cards you can find
11  *      at http://www.granch.com (English) or http://www.granch.ru (Russian)
12  *
13  *      This software may be used and distributed according to the terms
14  *      of the GNU General Public License.
15  *
16  *
17  *  5.0.1       Jun 22 2001
18  *        - Fixed bug in probe
19  *  5.0.0       Jun 06 2001
20  *        - Driver was completely redesigned by Denis I.Timofeev,
21  *        - now PCI/Dual, ISA/Dual (with single interrupt line) models are
22  *        - supported
23  *  3.3.0       Thu Feb 24 21:30:28 NOVT 2000 
24  *        - PCI cards support
25  *  3.2.0       Mon Dec 13 22:26:53 NOVT 1999
26  *        - Completely rebuilt all the packet storage system
27  *        -    to work in Ethernet-like style.
28  *  3.1.1       just fixed some bugs (5 aug 1999)
29  *  3.1.0       added balancing feature (26 apr 1999)
30  *  3.0.1       just fixed some bugs (14 apr 1999).
31  *  3.0.0       Initial Revision, Yaroslav Polyakov (24 Feb 1999)
32  *        - added pre-calculation for CRC, fixed bug with "len-2" frames, 
33  *        - removed outbound fragmentation (MTU=1000), written CRC-calculation 
34  *        - on asm, added work with hard_headers and now we have our own cache 
35  *        - for them, optionally supported word-interchange on some chipsets,
36  * 
37  *      Known problem: this driver wasn't tested on multiprocessor machine.
38  */
39
40 #include <linux/module.h>
41 #include <linux/kernel.h>
42 #include <linux/ptrace.h>
43 #include <linux/fcntl.h>
44 #include <linux/ioport.h>
45 #include <linux/interrupt.h>
46 #include <linux/slab.h>
47 #include <linux/string.h>
48 #include <linux/errno.h>
49 #include <linux/netdevice.h>
50 #include <linux/etherdevice.h>
51 #include <linux/pci.h>
52 #include <linux/skbuff.h>
53 #include <linux/timer.h>
54 #include <linux/init.h>
55 #include <linux/delay.h>
56
57 #include <net/arp.h>
58
59 #include <asm/io.h>
60 #include <asm/types.h>
61 #include <asm/byteorder.h>
62 #include <asm/irq.h>
63 #include <asm/uaccess.h>
64
65 #include "sbni.h"
66
67 /* device private data */
68
69 struct net_local {
70         struct net_device_stats stats;
71         struct timer_list       watchdog;
72
73         spinlock_t      lock;
74         struct sk_buff  *rx_buf_p;              /* receive buffer ptr */
75         struct sk_buff  *tx_buf_p;              /* transmit buffer ptr */
76         
77         unsigned int    framelen;               /* current frame length */
78         unsigned int    maxframe;               /* maximum valid frame length */
79         unsigned int    state;
80         unsigned int    inppos, outpos;         /* positions in rx/tx buffers */
81
82         /* transmitting frame number - from frames qty to 1 */
83         unsigned int    tx_frameno;
84
85         /* expected number of next receiving frame */
86         unsigned int    wait_frameno;
87
88         /* count of failed attempts to frame send - 32 attempts do before
89            error - while receiver tunes on opposite side of wire */
90         unsigned int    trans_errors;
91
92         /* idle time; send pong when limit exceeded */
93         unsigned int    timer_ticks;
94
95         /* fields used for receive level autoselection */
96         int     delta_rxl;
97         unsigned int    cur_rxl_index, timeout_rxl;
98         unsigned long   cur_rxl_rcvd, prev_rxl_rcvd;
99
100         struct sbni_csr1        csr1;           /* current value of CSR1 */
101         struct sbni_in_stats    in_stats;       /* internal statistics */ 
102
103         struct net_device               *second;        /* for ISA/dual cards */
104
105 #ifdef CONFIG_SBNI_MULTILINE
106         struct net_device               *master;
107         struct net_device               *link;
108 #endif
109 };
110
111
112 static int  sbni_card_probe( unsigned long );
113 static int  sbni_pci_probe( struct net_device  * );
114 static struct net_device  *sbni_probe1(struct net_device *, unsigned long, int);
115 static int  sbni_open( struct net_device * );
116 static int  sbni_close( struct net_device * );
117 static int  sbni_start_xmit( struct sk_buff *, struct net_device * );
118 static int  sbni_ioctl( struct net_device *, struct ifreq *, int );
119 static struct net_device_stats  *sbni_get_stats( struct net_device * );
120 static void  set_multicast_list( struct net_device * );
121
122 static irqreturn_t sbni_interrupt( int, void * );
123 static void  handle_channel( struct net_device * );
124 static int   recv_frame( struct net_device * );
125 static void  send_frame( struct net_device * );
126 static int   upload_data( struct net_device *,
127                           unsigned, unsigned, unsigned, u32 );
128 static void  download_data( struct net_device *, u32 * );
129 static void  sbni_watchdog( unsigned long );
130 static void  interpret_ack( struct net_device *, unsigned );
131 static int   append_frame_to_pkt( struct net_device *, unsigned, u32 );
132 static void  indicate_pkt( struct net_device * );
133 static void  card_start( struct net_device * );
134 static void  prepare_to_send( struct sk_buff *, struct net_device * );
135 static void  drop_xmit_queue( struct net_device * );
136 static void  send_frame_header( struct net_device *, u32 * );
137 static int   skip_tail( unsigned int, unsigned int, u32 );
138 static int   check_fhdr( u32, u32 *, u32 *, u32 *, u32 *, u32 * );
139 static void  change_level( struct net_device * );
140 static void  timeout_change_level( struct net_device * );
141 static u32   calc_crc32( u32, u8 *, u32 );
142 static struct sk_buff *  get_rx_buf( struct net_device * );
143 static int  sbni_init( struct net_device * );
144
145 #ifdef CONFIG_SBNI_MULTILINE
146 static int  enslave( struct net_device *, struct net_device * );
147 static int  emancipate( struct net_device * );
148 #endif
149
150 #ifdef __i386__
151 #define ASM_CRC 1
152 #endif
153
154 static const char  version[] =
155         "Granch SBNI12 driver ver 5.0.1  Jun 22 2001  Denis I.Timofeev.\n";
156
157 static int  skip_pci_probe      __initdata = 0;
158 static int  scandone    __initdata = 0;
159 static int  num         __initdata = 0;
160
161 static unsigned char  rxl_tab[];
162 static u32  crc32tab[];
163
164 /* A list of all installed devices, for removing the driver module. */
165 static struct net_device  *sbni_cards[ SBNI_MAX_NUM_CARDS ];
166
167 /* Lists of device's parameters */
168 static u32      io[   SBNI_MAX_NUM_CARDS ] __initdata =
169         { [0 ... SBNI_MAX_NUM_CARDS-1] = -1 };
170 static u32      irq[  SBNI_MAX_NUM_CARDS ] __initdata;
171 static u32      baud[ SBNI_MAX_NUM_CARDS ] __initdata;
172 static u32      rxl[  SBNI_MAX_NUM_CARDS ] __initdata =
173         { [0 ... SBNI_MAX_NUM_CARDS-1] = -1 };
174 static u32      mac[  SBNI_MAX_NUM_CARDS ] __initdata;
175
176 #ifndef MODULE
177 typedef u32  iarr[];
178 static iarr __initdata *dest[5] = { &io, &irq, &baud, &rxl, &mac };
179 #endif
180
181 /* A zero-terminated list of I/O addresses to be probed on ISA bus */
182 static unsigned int  netcard_portlist[ ] __initdata = { 
183         0x210, 0x214, 0x220, 0x224, 0x230, 0x234, 0x240, 0x244, 0x250, 0x254,
184         0x260, 0x264, 0x270, 0x274, 0x280, 0x284, 0x290, 0x294, 0x2a0, 0x2a4,
185         0x2b0, 0x2b4, 0x2c0, 0x2c4, 0x2d0, 0x2d4, 0x2e0, 0x2e4, 0x2f0, 0x2f4,
186         0 };
187
188
189 /*
190  * Look for SBNI card which addr stored in dev->base_addr, if nonzero.
191  * Otherwise, look through PCI bus. If none PCI-card was found, scan ISA.
192  */
193
194 static inline int __init
195 sbni_isa_probe( struct net_device  *dev )
196 {
197         if( dev->base_addr > 0x1ff
198             &&  request_region( dev->base_addr, SBNI_IO_EXTENT, dev->name )
199             &&  sbni_probe1( dev, dev->base_addr, dev->irq ) )
200
201                 return  0;
202         else {
203                 printk( KERN_ERR "sbni: base address 0x%lx is busy, or adapter "
204                         "is malfunctional!\n", dev->base_addr );
205                 return  -ENODEV;
206         }
207 }
208
209 static void __init sbni_devsetup(struct net_device *dev)
210 {
211         ether_setup( dev );
212         dev->open               = &sbni_open;
213         dev->stop               = &sbni_close;
214         dev->hard_start_xmit    = &sbni_start_xmit;
215         dev->get_stats          = &sbni_get_stats;
216         dev->set_multicast_list = &set_multicast_list;
217         dev->do_ioctl           = &sbni_ioctl;
218
219         SET_MODULE_OWNER( dev );
220 }
221
222 int __init sbni_probe(int unit)
223 {
224         struct net_device *dev;
225         static unsigned  version_printed __initdata = 0;
226         int err;
227
228         dev = alloc_netdev(sizeof(struct net_local), "sbni", sbni_devsetup);
229         if (!dev)
230                 return -ENOMEM;
231
232         sprintf(dev->name, "sbni%d", unit);
233         netdev_boot_setup_check(dev);
234
235         err = sbni_init(dev);
236         if (err) {
237                 free_netdev(dev);
238                 return err;
239         }
240
241         err = register_netdev(dev);
242         if (err) {
243                 release_region( dev->base_addr, SBNI_IO_EXTENT );
244                 free_netdev(dev);
245                 return err;
246         }
247         if( version_printed++ == 0 )
248                 printk( KERN_INFO "%s", version );
249         return 0;
250 }
251
252 static int __init sbni_init(struct net_device *dev)
253 {
254         int  i;
255         if( dev->base_addr )
256                 return  sbni_isa_probe( dev );
257         /* otherwise we have to perform search our adapter */
258
259         if( io[ num ] != -1 )
260                 dev->base_addr  = io[ num ],
261                 dev->irq        = irq[ num ];
262         else if( scandone  ||  io[ 0 ] != -1 )
263                 return  -ENODEV;
264
265         /* if io[ num ] contains non-zero address, then that is on ISA bus */
266         if( dev->base_addr )
267                 return  sbni_isa_probe( dev );
268
269         /* ...otherwise - scan PCI first */
270         if( !skip_pci_probe  &&  !sbni_pci_probe( dev ) )
271                 return  0;
272
273         if( io[ num ] == -1 ) {
274                 /* Auto-scan will be stopped when first ISA card were found */
275                 scandone = 1;
276                 if( num > 0 )
277                         return  -ENODEV;
278         }
279
280         for( i = 0;  netcard_portlist[ i ];  ++i ) {
281                 int  ioaddr = netcard_portlist[ i ];
282                 if( request_region( ioaddr, SBNI_IO_EXTENT, dev->name )
283                     &&  sbni_probe1( dev, ioaddr, 0 ))
284                         return 0;
285         }
286
287         return  -ENODEV;
288 }
289
290
291 int __init
292 sbni_pci_probe( struct net_device  *dev )
293 {
294         struct pci_dev  *pdev = NULL;
295
296         while( (pdev = pci_get_class( PCI_CLASS_NETWORK_OTHER << 8, pdev ))
297                != NULL ) {
298                 int  pci_irq_line;
299                 unsigned long  pci_ioaddr;
300                 u16  subsys;
301
302                 if( pdev->vendor != SBNI_PCI_VENDOR
303                     &&  pdev->device != SBNI_PCI_DEVICE )
304                                 continue;
305
306                 pci_ioaddr = pci_resource_start( pdev, 0 );
307                 pci_irq_line = pdev->irq;
308
309                 /* Avoid already found cards from previous calls */
310                 if( !request_region( pci_ioaddr, SBNI_IO_EXTENT, dev->name ) ) {
311                         pci_read_config_word( pdev, PCI_SUBSYSTEM_ID, &subsys );
312
313                         if (subsys != 2)
314                                 continue;
315
316                         /* Dual adapter is present */
317                         if (!request_region(pci_ioaddr += 4, SBNI_IO_EXTENT,
318                                                         dev->name ) )
319                                 continue;
320                 }
321
322                 if( pci_irq_line <= 0  ||  pci_irq_line >= NR_IRQS )
323                         printk( KERN_WARNING "  WARNING: The PCI BIOS assigned "
324                                 "this PCI card to IRQ %d, which is unlikely "
325                                 "to work!.\n"
326                                 KERN_WARNING " You should use the PCI BIOS "
327                                 "setup to assign a valid IRQ line.\n",
328                                 pci_irq_line );
329
330                 /* avoiding re-enable dual adapters */
331                 if( (pci_ioaddr & 7) == 0  &&  pci_enable_device( pdev ) ) {
332                         release_region( pci_ioaddr, SBNI_IO_EXTENT );
333                         pci_dev_put( pdev );
334                         return  -EIO;
335                 }
336                 if( sbni_probe1( dev, pci_ioaddr, pci_irq_line ) ) {
337                         SET_NETDEV_DEV(dev, &pdev->dev);
338                         /* not the best thing to do, but this is all messed up 
339                            for hotplug systems anyway... */
340                         pci_dev_put( pdev );
341                         return  0;
342                 }
343         }
344         return  -ENODEV;
345 }
346
347
348 static struct net_device * __init
349 sbni_probe1( struct net_device  *dev,  unsigned long  ioaddr,  int  irq )
350 {
351         struct net_local  *nl;
352
353         if( sbni_card_probe( ioaddr ) ) {
354                 release_region( ioaddr, SBNI_IO_EXTENT );
355                 return NULL;
356         }
357
358         outb( 0, ioaddr + CSR0 );
359
360         if( irq < 2 ) {
361                 unsigned long irq_mask;
362
363                 irq_mask = probe_irq_on();
364                 outb( EN_INT | TR_REQ, ioaddr + CSR0 );
365                 outb( PR_RES, ioaddr + CSR1 );
366                 mdelay(50);
367                 irq = probe_irq_off(irq_mask);
368                 outb( 0, ioaddr + CSR0 );
369
370                 if( !irq ) {
371                         printk( KERN_ERR "%s: can't detect device irq!\n",
372                                 dev->name );
373                         release_region( ioaddr, SBNI_IO_EXTENT );
374                         return NULL;
375                 }
376         } else if( irq == 2 )
377                 irq = 9;
378
379         dev->irq = irq;
380         dev->base_addr = ioaddr;
381
382         /* Allocate dev->priv and fill in sbni-specific dev fields. */
383         nl = dev->priv;
384         if( !nl ) {
385                 printk( KERN_ERR "%s: unable to get memory!\n", dev->name );
386                 release_region( ioaddr, SBNI_IO_EXTENT );
387                 return NULL;
388         }
389
390         dev->priv = nl;
391         memset( nl, 0, sizeof(struct net_local) );
392         spin_lock_init( &nl->lock );
393
394         /* store MAC address (generate if that isn't known) */
395         *(u16 *)dev->dev_addr = htons( 0x00ff );
396         *(u32 *)(dev->dev_addr + 2) = htonl( 0x01000000 |
397                 ( (mac[num]  ?  mac[num]  :  (u32)((long)dev->priv)) & 0x00ffffff) );
398
399         /* store link settings (speed, receive level ) */
400         nl->maxframe  = DEFAULT_FRAME_LEN;
401         nl->csr1.rate = baud[ num ];
402
403         if( (nl->cur_rxl_index = rxl[ num ]) == -1 )
404                 /* autotune rxl */
405                 nl->cur_rxl_index = DEF_RXL,
406                 nl->delta_rxl = DEF_RXL_DELTA;
407         else
408                 nl->delta_rxl = 0;
409         nl->csr1.rxl  = rxl_tab[ nl->cur_rxl_index ];
410         if( inb( ioaddr + CSR0 ) & 0x01 )
411                 nl->state |= FL_SLOW_MODE;
412
413         printk( KERN_NOTICE "%s: ioaddr %#lx, irq %d, "
414                 "MAC: 00:ff:01:%02x:%02x:%02x\n", 
415                 dev->name, dev->base_addr, dev->irq,
416                 ((u8 *) dev->dev_addr) [3],
417                 ((u8 *) dev->dev_addr) [4],
418                 ((u8 *) dev->dev_addr) [5] );
419
420         printk( KERN_NOTICE "%s: speed %d, receive level ", dev->name,
421                 ( (nl->state & FL_SLOW_MODE)  ?  500000 : 2000000)
422                 / (1 << nl->csr1.rate) );
423
424         if( nl->delta_rxl == 0 )
425                 printk( "0x%x (fixed)\n", nl->cur_rxl_index ); 
426         else
427                 printk( "(auto)\n");
428
429 #ifdef CONFIG_SBNI_MULTILINE
430         nl->master = dev;
431         nl->link   = NULL;
432 #endif
433    
434         sbni_cards[ num++ ] = dev;
435         return  dev;
436 }
437
438 /* -------------------------------------------------------------------------- */
439
440 #ifdef CONFIG_SBNI_MULTILINE
441
442 static int
443 sbni_start_xmit( struct sk_buff  *skb,  struct net_device  *dev )
444 {
445         struct net_device  *p;
446
447         netif_stop_queue( dev );
448
449         /* Looking for idle device in the list */
450         for( p = dev;  p; ) {
451                 struct net_local  *nl = (struct net_local *) p->priv;
452                 spin_lock( &nl->lock );
453                 if( nl->tx_buf_p  ||  (nl->state & FL_LINE_DOWN) ) {
454                         p = nl->link;
455                         spin_unlock( &nl->lock );
456                 } else {
457                         /* Idle dev is found */
458                         prepare_to_send( skb, p );
459                         spin_unlock( &nl->lock );
460                         netif_start_queue( dev );
461                         return  0;
462                 }
463         }
464
465         return  1;
466 }
467
468 #else   /* CONFIG_SBNI_MULTILINE */
469
470 static int
471 sbni_start_xmit( struct sk_buff  *skb,  struct net_device  *dev )
472 {
473         struct net_local  *nl  = (struct net_local *) dev->priv;
474
475         netif_stop_queue( dev );
476         spin_lock( &nl->lock );
477
478         prepare_to_send( skb, dev );
479
480         spin_unlock( &nl->lock );
481         return  0;
482 }
483
484 #endif  /* CONFIG_SBNI_MULTILINE */
485
486 /* -------------------------------------------------------------------------- */
487
488 /* interrupt handler */
489
490 /*
491  *      SBNI12D-10, -11/ISA boards within "common interrupt" mode could not
492  * be looked as two independent single-channel devices. Every channel seems
493  * as Ethernet interface but interrupt handler must be common. Really, first
494  * channel ("master") driver only registers the handler. In its struct net_local
495  * it has got pointer to "slave" channel's struct net_local and handles that's
496  * interrupts too.
497  *      dev of successfully attached ISA SBNI boards is linked to list.
498  * While next board driver is initialized, it scans this list. If one
499  * has found dev with same irq and ioaddr different by 4 then it assumes
500  * this board to be "master".
501  */ 
502
503 static irqreturn_t
504 sbni_interrupt( int  irq,  void  *dev_id )
505 {
506         struct net_device         *dev = (struct net_device *) dev_id;
507         struct net_local  *nl  = (struct net_local *) dev->priv;
508         int     repeat;
509
510         spin_lock( &nl->lock );
511         if( nl->second )
512                 spin_lock( &((struct net_local *) nl->second->priv)->lock );
513
514         do {
515                 repeat = 0;
516                 if( inb( dev->base_addr + CSR0 ) & (RC_RDY | TR_RDY) )
517                         handle_channel( dev ),
518                         repeat = 1;
519                 if( nl->second  &&      /* second channel present */
520                     (inb( nl->second->base_addr+CSR0 ) & (RC_RDY | TR_RDY)) )
521                         handle_channel( nl->second ),
522                         repeat = 1;
523         } while( repeat );
524
525         if( nl->second )
526                 spin_unlock( &((struct net_local *)nl->second->priv)->lock );
527         spin_unlock( &nl->lock );
528         return IRQ_HANDLED;
529 }
530
531
532 static void
533 handle_channel( struct net_device  *dev )
534 {
535         struct net_local        *nl    = (struct net_local *) dev->priv;
536         unsigned long           ioaddr = dev->base_addr;
537
538         int  req_ans;
539         unsigned char  csr0;
540
541 #ifdef CONFIG_SBNI_MULTILINE
542         /* Lock the master device because we going to change its local data */
543         if( nl->state & FL_SLAVE )
544                 spin_lock( &((struct net_local *) nl->master->priv)->lock );
545 #endif
546
547         outb( (inb( ioaddr + CSR0 ) & ~EN_INT) | TR_REQ, ioaddr + CSR0 );
548
549         nl->timer_ticks = CHANGE_LEVEL_START_TICKS;
550         for(;;) {
551                 csr0 = inb( ioaddr + CSR0 );
552                 if( ( csr0 & (RC_RDY | TR_RDY) ) == 0 )
553                         break;
554
555                 req_ans = !(nl->state & FL_PREV_OK);
556
557                 if( csr0 & RC_RDY )
558                         req_ans = recv_frame( dev );
559
560                 /*
561                  * TR_RDY always equals 1 here because we have owned the marker,
562                  * and we set TR_REQ when disabled interrupts
563                  */
564                 csr0 = inb( ioaddr + CSR0 );
565                 if( !(csr0 & TR_RDY)  ||  (csr0 & RC_RDY) )
566                         printk( KERN_ERR "%s: internal error!\n", dev->name );
567
568                 /* if state & FL_NEED_RESEND != 0 then tx_frameno != 0 */
569                 if( req_ans  ||  nl->tx_frameno != 0 )
570                         send_frame( dev );
571                 else
572                         /* send marker without any data */
573                         outb( inb( ioaddr + CSR0 ) & ~TR_REQ, ioaddr + CSR0 );
574         }
575
576         outb( inb( ioaddr + CSR0 ) | EN_INT, ioaddr + CSR0 );
577
578 #ifdef CONFIG_SBNI_MULTILINE
579         if( nl->state & FL_SLAVE )
580                 spin_unlock( &((struct net_local *) nl->master->priv)->lock );
581 #endif
582 }
583
584
585 /*
586  * Routine returns 1 if it need to acknoweledge received frame.
587  * Empty frame received without errors won't be acknoweledged.
588  */
589
590 static int
591 recv_frame( struct net_device  *dev )
592 {
593         struct net_local  *nl   = (struct net_local *) dev->priv;
594         unsigned long  ioaddr   = dev->base_addr;
595
596         u32  crc = CRC32_INITIAL;
597
598         unsigned  framelen = 0, frameno, ack;
599         unsigned  is_first, frame_ok = 0;
600
601         if( check_fhdr( ioaddr, &framelen, &frameno, &ack, &is_first, &crc ) ) {
602                 frame_ok = framelen > 4
603                         ?  upload_data( dev, framelen, frameno, is_first, crc )
604                         :  skip_tail( ioaddr, framelen, crc );
605                 if( frame_ok )
606                         interpret_ack( dev, ack );
607         }
608
609         outb( inb( ioaddr + CSR0 ) ^ CT_ZER, ioaddr + CSR0 );
610         if( frame_ok ) {
611                 nl->state |= FL_PREV_OK;
612                 if( framelen > 4 )
613                         nl->in_stats.all_rx_number++;
614         } else
615                 nl->state &= ~FL_PREV_OK,
616                 change_level( dev ),
617                 nl->in_stats.all_rx_number++,
618                 nl->in_stats.bad_rx_number++;
619
620         return  !frame_ok  ||  framelen > 4;
621 }
622
623
624 static void
625 send_frame( struct net_device  *dev )
626 {
627         struct net_local  *nl    = (struct net_local *) dev->priv;
628
629         u32  crc = CRC32_INITIAL;
630
631         if( nl->state & FL_NEED_RESEND ) {
632
633                 /* if frame was sended but not ACK'ed - resend it */
634                 if( nl->trans_errors ) {
635                         --nl->trans_errors;
636                         if( nl->framelen != 0 )
637                                 nl->in_stats.resend_tx_number++;
638                 } else {
639                         /* cannot xmit with many attempts */
640 #ifdef CONFIG_SBNI_MULTILINE
641                         if( (nl->state & FL_SLAVE)  ||  nl->link )
642 #endif
643                         nl->state |= FL_LINE_DOWN;
644                         drop_xmit_queue( dev );
645                         goto  do_send;
646                 }
647         } else
648                 nl->trans_errors = TR_ERROR_COUNT;
649
650         send_frame_header( dev, &crc );
651         nl->state |= FL_NEED_RESEND;
652         /*
653          * FL_NEED_RESEND will be cleared after ACK, but if empty
654          * frame sended then in prepare_to_send next frame
655          */
656
657
658         if( nl->framelen ) {
659                 download_data( dev, &crc );
660                 nl->in_stats.all_tx_number++;
661                 nl->state |= FL_WAIT_ACK;
662         }
663
664         outsb( dev->base_addr + DAT, (u8 *)&crc, sizeof crc );
665
666 do_send:
667         outb( inb( dev->base_addr + CSR0 ) & ~TR_REQ, dev->base_addr + CSR0 );
668
669         if( nl->tx_frameno )
670                 /* next frame exists - we request card to send it */
671                 outb( inb( dev->base_addr + CSR0 ) | TR_REQ,
672                       dev->base_addr + CSR0 );
673 }
674
675
676 /*
677  * Write the frame data into adapter's buffer memory, and calculate CRC.
678  * Do padding if necessary.
679  */
680
681 static void
682 download_data( struct net_device  *dev,  u32  *crc_p )
683 {
684         struct net_local  *nl    = (struct net_local *) dev->priv;
685         struct sk_buff    *skb   = nl->tx_buf_p;
686
687         unsigned  len = min_t(unsigned int, skb->len - nl->outpos, nl->framelen);
688
689         outsb( dev->base_addr + DAT, skb->data + nl->outpos, len );
690         *crc_p = calc_crc32( *crc_p, skb->data + nl->outpos, len );
691
692         /* if packet too short we should write some more bytes to pad */
693         for( len = nl->framelen - len;  len--; )
694                 outb( 0, dev->base_addr + DAT ),
695                 *crc_p = CRC32( 0, *crc_p );
696 }
697
698
699 static int
700 upload_data( struct net_device  *dev,  unsigned  framelen,  unsigned  frameno,
701              unsigned  is_first,  u32  crc )
702 {
703         struct net_local  *nl = (struct net_local *) dev->priv;
704
705         int  frame_ok;
706
707         if( is_first )
708                 nl->wait_frameno = frameno,
709                 nl->inppos = 0;
710
711         if( nl->wait_frameno == frameno ) {
712
713                 if( nl->inppos + framelen  <=  ETHER_MAX_LEN )
714                         frame_ok = append_frame_to_pkt( dev, framelen, crc );
715
716                 /*
717                  * if CRC is right but framelen incorrect then transmitter
718                  * error was occurred... drop entire packet
719                  */
720                 else if( (frame_ok = skip_tail( dev->base_addr, framelen, crc ))
721                          != 0 )
722                         nl->wait_frameno = 0,
723                         nl->inppos = 0,
724 #ifdef CONFIG_SBNI_MULTILINE
725                         ((struct net_local *) nl->master->priv)
726                                 ->stats.rx_errors++,
727                         ((struct net_local *) nl->master->priv)
728                                 ->stats.rx_missed_errors++;
729 #else
730                         nl->stats.rx_errors++,
731                         nl->stats.rx_missed_errors++;
732 #endif
733                         /* now skip all frames until is_first != 0 */
734         } else
735                 frame_ok = skip_tail( dev->base_addr, framelen, crc );
736
737         if( is_first  &&  !frame_ok )
738                 /*
739                  * Frame has been broken, but we had already stored
740                  * is_first... Drop entire packet.
741                  */
742                 nl->wait_frameno = 0,
743 #ifdef CONFIG_SBNI_MULTILINE
744                 ((struct net_local *) nl->master->priv)->stats.rx_errors++,
745                 ((struct net_local *) nl->master->priv)->stats.rx_crc_errors++;
746 #else
747                 nl->stats.rx_errors++,
748                 nl->stats.rx_crc_errors++;
749 #endif
750
751         return  frame_ok;
752 }
753
754
755 static __inline void
756 send_complete( struct net_local  *nl )
757 {
758 #ifdef CONFIG_SBNI_MULTILINE
759         ((struct net_local *) nl->master->priv)->stats.tx_packets++;
760         ((struct net_local *) nl->master->priv)->stats.tx_bytes
761                 += nl->tx_buf_p->len;
762 #else
763         nl->stats.tx_packets++;
764         nl->stats.tx_bytes += nl->tx_buf_p->len;
765 #endif
766         dev_kfree_skb_irq( nl->tx_buf_p );
767
768         nl->tx_buf_p = NULL;
769
770         nl->outpos = 0;
771         nl->state &= ~(FL_WAIT_ACK | FL_NEED_RESEND);
772         nl->framelen   = 0;
773 }
774
775
776 static void
777 interpret_ack( struct net_device  *dev,  unsigned  ack )
778 {
779         struct net_local  *nl = (struct net_local *) dev->priv;
780
781         if( ack == FRAME_SENT_OK ) {
782                 nl->state &= ~FL_NEED_RESEND;
783
784                 if( nl->state & FL_WAIT_ACK ) {
785                         nl->outpos += nl->framelen;
786
787                         if( --nl->tx_frameno )
788                                 nl->framelen = min_t(unsigned int,
789                                                    nl->maxframe,
790                                                    nl->tx_buf_p->len - nl->outpos);
791                         else
792                                 send_complete( nl ),
793 #ifdef CONFIG_SBNI_MULTILINE
794                                 netif_wake_queue( nl->master );
795 #else
796                                 netif_wake_queue( dev );
797 #endif
798                 }
799         }
800
801         nl->state &= ~FL_WAIT_ACK;
802 }
803
804
805 /*
806  * Glue received frame with previous fragments of packet.
807  * Indicate packet when last frame would be accepted.
808  */
809
810 static int
811 append_frame_to_pkt( struct net_device  *dev,  unsigned  framelen,  u32  crc )
812 {
813         struct net_local  *nl = (struct net_local *) dev->priv;
814
815         u8  *p;
816
817         if( nl->inppos + framelen  >  ETHER_MAX_LEN )
818                 return  0;
819
820         if( !nl->rx_buf_p  &&  !(nl->rx_buf_p = get_rx_buf( dev )) )
821                 return  0;
822
823         p = nl->rx_buf_p->data + nl->inppos;
824         insb( dev->base_addr + DAT, p, framelen );
825         if( calc_crc32( crc, p, framelen ) != CRC32_REMAINDER )
826                 return  0;
827
828         nl->inppos += framelen - 4;
829         if( --nl->wait_frameno == 0 )           /* last frame received */
830                 indicate_pkt( dev );
831
832         return  1;
833 }
834
835
836 /*
837  * Prepare to start output on adapter.
838  * Transmitter will be actually activated when marker is accepted.
839  */
840
841 static void
842 prepare_to_send( struct sk_buff  *skb,  struct net_device  *dev )
843 {
844         struct net_local  *nl = (struct net_local *) dev->priv;
845
846         unsigned int  len;
847
848         /* nl->tx_buf_p == NULL here! */
849         if( nl->tx_buf_p )
850                 printk( KERN_ERR "%s: memory leak!\n", dev->name );
851
852         nl->outpos = 0;
853         nl->state &= ~(FL_WAIT_ACK | FL_NEED_RESEND);
854
855         len = skb->len;
856         if( len < SBNI_MIN_LEN )
857                 len = SBNI_MIN_LEN;
858
859         nl->tx_buf_p    = skb;
860         nl->tx_frameno  = (len + nl->maxframe - 1) / nl->maxframe;
861         nl->framelen    = len < nl->maxframe  ?  len  :  nl->maxframe;
862
863         outb( inb( dev->base_addr + CSR0 ) | TR_REQ,  dev->base_addr + CSR0 );
864 #ifdef CONFIG_SBNI_MULTILINE
865         nl->master->trans_start = jiffies;
866 #else
867         dev->trans_start = jiffies;
868 #endif
869 }
870
871
872 static void
873 drop_xmit_queue( struct net_device  *dev )
874 {
875         struct net_local  *nl = (struct net_local *) dev->priv;
876
877         if( nl->tx_buf_p )
878                 dev_kfree_skb_any( nl->tx_buf_p ),
879                 nl->tx_buf_p = NULL,
880 #ifdef CONFIG_SBNI_MULTILINE
881                 ((struct net_local *) nl->master->priv)
882                         ->stats.tx_errors++,
883                 ((struct net_local *) nl->master->priv)
884                         ->stats.tx_carrier_errors++;
885 #else
886                 nl->stats.tx_errors++,
887                 nl->stats.tx_carrier_errors++;
888 #endif
889
890         nl->tx_frameno  = 0;
891         nl->framelen    = 0;
892         nl->outpos      = 0;
893         nl->state &= ~(FL_WAIT_ACK | FL_NEED_RESEND);
894 #ifdef CONFIG_SBNI_MULTILINE
895         netif_start_queue( nl->master );
896         nl->master->trans_start = jiffies;
897 #else
898         netif_start_queue( dev );
899         dev->trans_start = jiffies;
900 #endif
901 }
902
903
904 static void
905 send_frame_header( struct net_device  *dev,  u32  *crc_p )
906 {
907         struct net_local  *nl  = (struct net_local *) dev->priv;
908
909         u32  crc = *crc_p;
910         u32  len_field = nl->framelen + 6;      /* CRC + frameno + reserved */
911         u8   value;
912
913         if( nl->state & FL_NEED_RESEND )
914                 len_field |= FRAME_RETRY;       /* non-first attempt... */
915
916         if( nl->outpos == 0 )
917                 len_field |= FRAME_FIRST;
918
919         len_field |= (nl->state & FL_PREV_OK) ? FRAME_SENT_OK : FRAME_SENT_BAD;
920         outb( SBNI_SIG, dev->base_addr + DAT );
921
922         value = (u8) len_field;
923         outb( value, dev->base_addr + DAT );
924         crc = CRC32( value, crc );
925         value = (u8) (len_field >> 8);
926         outb( value, dev->base_addr + DAT );
927         crc = CRC32( value, crc );
928
929         outb( nl->tx_frameno, dev->base_addr + DAT );
930         crc = CRC32( nl->tx_frameno, crc );
931         outb( 0, dev->base_addr + DAT );
932         crc = CRC32( 0, crc );
933         *crc_p = crc;
934 }
935
936
937 /*
938  * if frame tail not needed (incorrect number or received twice),
939  * it won't store, but CRC will be calculated
940  */
941
942 static int
943 skip_tail( unsigned int  ioaddr,  unsigned int  tail_len,  u32 crc )
944 {
945         while( tail_len-- )
946                 crc = CRC32( inb( ioaddr + DAT ), crc );
947
948         return  crc == CRC32_REMAINDER;
949 }
950
951
952 /*
953  * Preliminary checks if frame header is correct, calculates its CRC
954  * and split it to simple fields
955  */
956
957 static int
958 check_fhdr( u32  ioaddr,  u32  *framelen,  u32  *frameno,  u32  *ack,
959             u32  *is_first,  u32  *crc_p )
960 {
961         u32  crc = *crc_p;
962         u8   value;
963
964         if( inb( ioaddr + DAT ) != SBNI_SIG )
965                 return  0;
966
967         value = inb( ioaddr + DAT );
968         *framelen = (u32)value;
969         crc = CRC32( value, crc );
970         value = inb( ioaddr + DAT );
971         *framelen |= ((u32)value) << 8;
972         crc = CRC32( value, crc );
973
974         *ack = *framelen & FRAME_ACK_MASK;
975         *is_first = (*framelen & FRAME_FIRST) != 0;
976
977         if( (*framelen &= FRAME_LEN_MASK) < 6
978             ||  *framelen > SBNI_MAX_FRAME - 3 )
979                 return  0;
980
981         value = inb( ioaddr + DAT );
982         *frameno = (u32)value;
983         crc = CRC32( value, crc );
984
985         crc = CRC32( inb( ioaddr + DAT ), crc );        /* reserved byte */
986         *framelen -= 2;
987
988         *crc_p = crc;
989         return  1;
990 }
991
992
993 static struct sk_buff *
994 get_rx_buf( struct net_device  *dev )
995 {
996         /* +2 is to compensate for the alignment fixup below */
997         struct sk_buff  *skb = dev_alloc_skb( ETHER_MAX_LEN + 2 );
998         if( !skb )
999                 return  NULL;
1000
1001         skb_reserve( skb, 2 );          /* Align IP on longword boundaries */
1002         return  skb;
1003 }
1004
1005
1006 static void
1007 indicate_pkt( struct net_device  *dev )
1008 {
1009         struct net_local  *nl  = (struct net_local *) dev->priv;
1010         struct sk_buff    *skb = nl->rx_buf_p;
1011
1012         skb_put( skb, nl->inppos );
1013
1014 #ifdef CONFIG_SBNI_MULTILINE
1015         skb->protocol = eth_type_trans( skb, nl->master );
1016         netif_rx( skb );
1017         dev->last_rx = jiffies;
1018         ++((struct net_local *) nl->master->priv)->stats.rx_packets;
1019         ((struct net_local *) nl->master->priv)->stats.rx_bytes += nl->inppos;
1020 #else
1021         skb->protocol = eth_type_trans( skb, dev );
1022         netif_rx( skb );
1023         dev->last_rx = jiffies;
1024         ++nl->stats.rx_packets;
1025         nl->stats.rx_bytes += nl->inppos;
1026 #endif
1027         nl->rx_buf_p = NULL;    /* protocol driver will clear this sk_buff */
1028 }
1029
1030
1031 /* -------------------------------------------------------------------------- */
1032
1033 /*
1034  * Routine checks periodically wire activity and regenerates marker if
1035  * connect was inactive for a long time.
1036  */
1037
1038 static void
1039 sbni_watchdog( unsigned long  arg )
1040 {
1041         struct net_device  *dev = (struct net_device *) arg;
1042         struct net_local   *nl  = (struct net_local *) dev->priv;
1043         struct timer_list  *w   = &nl->watchdog; 
1044         unsigned long      flags;
1045         unsigned char      csr0;
1046
1047         spin_lock_irqsave( &nl->lock, flags );
1048
1049         csr0 = inb( dev->base_addr + CSR0 );
1050         if( csr0 & RC_CHK ) {
1051
1052                 if( nl->timer_ticks ) {
1053                         if( csr0 & (RC_RDY | BU_EMP) )
1054                                 /* receiving not active */
1055                                 nl->timer_ticks--;
1056                 } else {
1057                         nl->in_stats.timeout_number++;
1058                         if( nl->delta_rxl )
1059                                 timeout_change_level( dev );
1060
1061                         outb( *(u_char *)&nl->csr1 | PR_RES,
1062                               dev->base_addr + CSR1 );
1063                         csr0 = inb( dev->base_addr + CSR0 );
1064                 }
1065         } else
1066                 nl->state &= ~FL_LINE_DOWN;
1067
1068         outb( csr0 | RC_CHK, dev->base_addr + CSR0 ); 
1069
1070         init_timer( w );
1071         w->expires      = jiffies + SBNI_TIMEOUT;
1072         w->data         = arg;
1073         w->function     = sbni_watchdog;
1074         add_timer( w );
1075
1076         spin_unlock_irqrestore( &nl->lock, flags );
1077 }
1078
1079
1080 static unsigned char  rxl_tab[] = {
1081         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08,
1082         0x0a, 0x0c, 0x0f, 0x16, 0x18, 0x1a, 0x1c, 0x1f
1083 };
1084
1085 #define SIZE_OF_TIMEOUT_RXL_TAB 4
1086 static unsigned char  timeout_rxl_tab[] = {
1087         0x03, 0x05, 0x08, 0x0b
1088 };
1089
1090 /* -------------------------------------------------------------------------- */
1091
1092 static void
1093 card_start( struct net_device  *dev )
1094 {
1095         struct net_local  *nl = (struct net_local *) dev->priv;
1096
1097         nl->timer_ticks = CHANGE_LEVEL_START_TICKS;
1098         nl->state &= ~(FL_WAIT_ACK | FL_NEED_RESEND);
1099         nl->state |= FL_PREV_OK;
1100
1101         nl->inppos = nl->outpos = 0;
1102         nl->wait_frameno = 0;
1103         nl->tx_frameno   = 0;
1104         nl->framelen     = 0;
1105
1106         outb( *(u_char *)&nl->csr1 | PR_RES, dev->base_addr + CSR1 );
1107         outb( EN_INT, dev->base_addr + CSR0 );
1108 }
1109
1110 /* -------------------------------------------------------------------------- */
1111
1112 /* Receive level auto-selection */
1113
1114 static void
1115 change_level( struct net_device  *dev )
1116 {
1117         struct net_local  *nl = (struct net_local *) dev->priv;
1118
1119         if( nl->delta_rxl == 0 )        /* do not auto-negotiate RxL */
1120                 return;
1121
1122         if( nl->cur_rxl_index == 0 )
1123                 nl->delta_rxl = 1;
1124         else if( nl->cur_rxl_index == 15 )
1125                 nl->delta_rxl = -1;
1126         else if( nl->cur_rxl_rcvd < nl->prev_rxl_rcvd )
1127                 nl->delta_rxl = -nl->delta_rxl;
1128
1129         nl->csr1.rxl = rxl_tab[ nl->cur_rxl_index += nl->delta_rxl ];
1130         inb( dev->base_addr + CSR0 );   /* needs for PCI cards */
1131         outb( *(u8 *)&nl->csr1, dev->base_addr + CSR1 );
1132
1133         nl->prev_rxl_rcvd = nl->cur_rxl_rcvd;
1134         nl->cur_rxl_rcvd  = 0;
1135 }
1136
1137
1138 static void
1139 timeout_change_level( struct net_device  *dev )
1140 {
1141         struct net_local  *nl = (struct net_local *) dev->priv;
1142
1143         nl->cur_rxl_index = timeout_rxl_tab[ nl->timeout_rxl ];
1144         if( ++nl->timeout_rxl >= 4 )
1145                 nl->timeout_rxl = 0;
1146
1147         nl->csr1.rxl = rxl_tab[ nl->cur_rxl_index ];
1148         inb( dev->base_addr + CSR0 );
1149         outb( *(unsigned char *)&nl->csr1, dev->base_addr + CSR1 );
1150
1151         nl->prev_rxl_rcvd = nl->cur_rxl_rcvd;
1152         nl->cur_rxl_rcvd  = 0;
1153 }
1154
1155 /* -------------------------------------------------------------------------- */
1156
1157 /*
1158  *      Open/initialize the board. 
1159  */
1160
1161 static int
1162 sbni_open( struct net_device  *dev )
1163 {
1164         struct net_local        *nl = (struct net_local *) dev->priv;
1165         struct timer_list       *w  = &nl->watchdog;
1166
1167         /*
1168          * For double ISA adapters within "common irq" mode, we have to
1169          * determine whether primary or secondary channel is initialized,
1170          * and set the irq handler only in first case.
1171          */
1172         if( dev->base_addr < 0x400 ) {          /* ISA only */
1173                 struct net_device  **p = sbni_cards;
1174                 for( ;  *p  &&  p < sbni_cards + SBNI_MAX_NUM_CARDS;  ++p )
1175                         if( (*p)->irq == dev->irq
1176                             &&  ((*p)->base_addr == dev->base_addr + 4
1177                                  ||  (*p)->base_addr == dev->base_addr - 4)
1178                             &&  (*p)->flags & IFF_UP ) {
1179
1180                                 ((struct net_local *) ((*p)->priv))
1181                                         ->second = dev;
1182                                 printk( KERN_NOTICE "%s: using shared irq "
1183                                         "with %s\n", dev->name, (*p)->name );
1184                                 nl->state |= FL_SECONDARY;
1185                                 goto  handler_attached;
1186                         }
1187         }
1188
1189         if( request_irq(dev->irq, sbni_interrupt, IRQF_SHARED, dev->name, dev) ) {
1190                 printk( KERN_ERR "%s: unable to get IRQ %d.\n",
1191                         dev->name, dev->irq );
1192                 return  -EAGAIN;
1193         }
1194
1195 handler_attached:
1196
1197         spin_lock( &nl->lock );
1198         memset( &nl->stats, 0, sizeof(struct net_device_stats) );
1199         memset( &nl->in_stats, 0, sizeof(struct sbni_in_stats) );
1200
1201         card_start( dev );
1202
1203         netif_start_queue( dev );
1204
1205         /* set timer watchdog */
1206         init_timer( w );
1207         w->expires      = jiffies + SBNI_TIMEOUT;
1208         w->data         = (unsigned long) dev;
1209         w->function     = sbni_watchdog;
1210         add_timer( w );
1211    
1212         spin_unlock( &nl->lock );
1213         return 0;
1214 }
1215
1216
1217 static int
1218 sbni_close( struct net_device  *dev )
1219 {
1220         struct net_local  *nl = (struct net_local *) dev->priv;
1221
1222         if( nl->second  &&  nl->second->flags & IFF_UP ) {
1223                 printk( KERN_NOTICE "Secondary channel (%s) is active!\n",
1224                         nl->second->name );
1225                 return  -EBUSY;
1226         }
1227
1228 #ifdef CONFIG_SBNI_MULTILINE
1229         if( nl->state & FL_SLAVE )
1230                 emancipate( dev );
1231         else
1232                 while( nl->link )       /* it's master device! */
1233                         emancipate( nl->link );
1234 #endif
1235
1236         spin_lock( &nl->lock );
1237
1238         nl->second = NULL;
1239         drop_xmit_queue( dev ); 
1240         netif_stop_queue( dev );
1241    
1242         del_timer( &nl->watchdog );
1243
1244         outb( 0, dev->base_addr + CSR0 );
1245
1246         if( !(nl->state & FL_SECONDARY) )
1247                 free_irq( dev->irq, dev );
1248         nl->state &= FL_SECONDARY;
1249
1250         spin_unlock( &nl->lock );
1251         return 0;
1252 }
1253
1254
1255 /*
1256         Valid combinations in CSR0 (for probing):
1257
1258         VALID_DECODER   0000,0011,1011,1010
1259
1260                                         ; 0   ; -
1261                                 TR_REQ  ; 1   ; +
1262                         TR_RDY          ; 2   ; -
1263                         TR_RDY  TR_REQ  ; 3   ; +
1264                 BU_EMP                  ; 4   ; +
1265                 BU_EMP          TR_REQ  ; 5   ; +
1266                 BU_EMP  TR_RDY          ; 6   ; -
1267                 BU_EMP  TR_RDY  TR_REQ  ; 7   ; +
1268         RC_RDY                          ; 8   ; +
1269         RC_RDY                  TR_REQ  ; 9   ; +
1270         RC_RDY          TR_RDY          ; 10  ; -
1271         RC_RDY          TR_RDY  TR_REQ  ; 11  ; -
1272         RC_RDY  BU_EMP                  ; 12  ; -
1273         RC_RDY  BU_EMP          TR_REQ  ; 13  ; -
1274         RC_RDY  BU_EMP  TR_RDY          ; 14  ; -
1275         RC_RDY  BU_EMP  TR_RDY  TR_REQ  ; 15  ; -
1276 */
1277
1278 #define VALID_DECODER (2 + 8 + 0x10 + 0x20 + 0x80 + 0x100 + 0x200)
1279
1280
1281 static int
1282 sbni_card_probe( unsigned long  ioaddr )
1283 {
1284         unsigned char  csr0;
1285
1286         csr0 = inb( ioaddr + CSR0 );
1287         if( csr0 != 0xff  &&  csr0 != 0x00 ) {
1288                 csr0 &= ~EN_INT;
1289                 if( csr0 & BU_EMP )
1290                         csr0 |= EN_INT;
1291       
1292                 if( VALID_DECODER & (1 << (csr0 >> 4)) )
1293                         return  0;
1294         }
1295    
1296         return  -ENODEV;
1297 }
1298
1299 /* -------------------------------------------------------------------------- */
1300
1301 static int
1302 sbni_ioctl( struct net_device  *dev,  struct ifreq  *ifr,  int  cmd )
1303 {
1304         struct net_local  *nl = (struct net_local *) dev->priv; 
1305         struct sbni_flags  flags;
1306         int  error = 0;
1307
1308 #ifdef CONFIG_SBNI_MULTILINE
1309         struct net_device  *slave_dev;
1310         char  slave_name[ 8 ];
1311 #endif
1312   
1313         switch( cmd ) {
1314         case  SIOCDEVGETINSTATS :
1315                 if (copy_to_user( ifr->ifr_data, &nl->in_stats,
1316                                         sizeof(struct sbni_in_stats) ))
1317                         error = -EFAULT;
1318                 break;
1319
1320         case  SIOCDEVRESINSTATS :
1321                 if( current->euid != 0 )        /* root only */
1322                         return  -EPERM;
1323                 memset( &nl->in_stats, 0, sizeof(struct sbni_in_stats) );
1324                 break;
1325
1326         case  SIOCDEVGHWSTATE :
1327                 flags.mac_addr  = *(u32 *)(dev->dev_addr + 3);
1328                 flags.rate      = nl->csr1.rate;
1329                 flags.slow_mode = (nl->state & FL_SLOW_MODE) != 0;
1330                 flags.rxl       = nl->cur_rxl_index;
1331                 flags.fixed_rxl = nl->delta_rxl == 0;
1332
1333                 if (copy_to_user( ifr->ifr_data, &flags, sizeof flags ))
1334                         error = -EFAULT;
1335                 break;
1336
1337         case  SIOCDEVSHWSTATE :
1338                 if( current->euid != 0 )        /* root only */
1339                         return  -EPERM;
1340
1341                 spin_lock( &nl->lock );
1342                 flags = *(struct sbni_flags*) &ifr->ifr_ifru;
1343                 if( flags.fixed_rxl )
1344                         nl->delta_rxl = 0,
1345                         nl->cur_rxl_index = flags.rxl;
1346                 else
1347                         nl->delta_rxl = DEF_RXL_DELTA,
1348                         nl->cur_rxl_index = DEF_RXL;
1349
1350                 nl->csr1.rxl = rxl_tab[ nl->cur_rxl_index ];
1351                 nl->csr1.rate = flags.rate;
1352                 outb( *(u8 *)&nl->csr1 | PR_RES, dev->base_addr + CSR1 );
1353                 spin_unlock( &nl->lock );
1354                 break;
1355
1356 #ifdef CONFIG_SBNI_MULTILINE
1357
1358         case  SIOCDEVENSLAVE :
1359                 if( current->euid != 0 )        /* root only */
1360                         return  -EPERM;
1361
1362                 if (copy_from_user( slave_name, ifr->ifr_data, sizeof slave_name ))
1363                         return -EFAULT;
1364                 slave_dev = dev_get_by_name( slave_name );
1365                 if( !slave_dev  ||  !(slave_dev->flags & IFF_UP) ) {
1366                         printk( KERN_ERR "%s: trying to enslave non-active "
1367                                 "device %s\n", dev->name, slave_name );
1368                         return  -EPERM;
1369                 }
1370
1371                 return  enslave( dev, slave_dev );
1372
1373         case  SIOCDEVEMANSIPATE :
1374                 if( current->euid != 0 )        /* root only */
1375                         return  -EPERM;
1376
1377                 return  emancipate( dev );
1378
1379 #endif  /* CONFIG_SBNI_MULTILINE */
1380
1381         default :
1382                 return  -EOPNOTSUPP;
1383         }
1384
1385         return  error;
1386 }
1387
1388
1389 #ifdef CONFIG_SBNI_MULTILINE
1390
1391 static int
1392 enslave( struct net_device  *dev,  struct net_device  *slave_dev )
1393 {
1394         struct net_local  *nl  = (struct net_local *) dev->priv;
1395         struct net_local  *snl = (struct net_local *) slave_dev->priv;
1396
1397         if( nl->state & FL_SLAVE )      /* This isn't master or free device */
1398                 return  -EBUSY;
1399
1400         if( snl->state & FL_SLAVE )     /* That was already enslaved */
1401                 return  -EBUSY;
1402
1403         spin_lock( &nl->lock );
1404         spin_lock( &snl->lock );
1405
1406         /* append to list */
1407         snl->link = nl->link;
1408         nl->link  = slave_dev;
1409         snl->master = dev;
1410         snl->state |= FL_SLAVE;
1411
1412         /* Summary statistics of MultiLine operation will be stored
1413            in master's counters */
1414         memset( &snl->stats, 0, sizeof(struct net_device_stats) );
1415         netif_stop_queue( slave_dev );
1416         netif_wake_queue( dev );        /* Now we are able to transmit */
1417
1418         spin_unlock( &snl->lock );
1419         spin_unlock( &nl->lock );
1420         printk( KERN_NOTICE "%s: slave device (%s) attached.\n",
1421                 dev->name, slave_dev->name );
1422         return  0;
1423 }
1424
1425
1426 static int
1427 emancipate( struct net_device  *dev )
1428 {
1429         struct net_local   *snl = (struct net_local *) dev->priv;
1430         struct net_device  *p   = snl->master;
1431         struct net_local   *nl  = (struct net_local *) p->priv;
1432
1433         if( !(snl->state & FL_SLAVE) )
1434                 return  -EINVAL;
1435
1436         spin_lock( &nl->lock );
1437         spin_lock( &snl->lock );
1438         drop_xmit_queue( dev );
1439
1440         /* exclude from list */
1441         for(;;) {       /* must be in list */
1442                 struct net_local  *t = (struct net_local *) p->priv;
1443                 if( t->link == dev ) {
1444                         t->link = snl->link;
1445                         break;
1446                 }
1447                 p = t->link;
1448         }
1449
1450         snl->link = NULL;
1451         snl->master = dev;
1452         snl->state &= ~FL_SLAVE;
1453
1454         netif_start_queue( dev );
1455
1456         spin_unlock( &snl->lock );
1457         spin_unlock( &nl->lock );
1458
1459         dev_put( dev );
1460         return  0;
1461 }
1462
1463 #endif
1464
1465
1466 static struct net_device_stats *
1467 sbni_get_stats( struct net_device  *dev )
1468 {
1469         return  &((struct net_local *) dev->priv)->stats;
1470 }
1471
1472
1473 static void
1474 set_multicast_list( struct net_device  *dev )
1475 {
1476         return;         /* sbni always operate in promiscuos mode */
1477 }
1478
1479
1480 #ifdef MODULE
1481 module_param_array(io, int, NULL, 0);
1482 module_param_array(irq, int, NULL, 0);
1483 module_param_array(baud, int, NULL, 0);
1484 module_param_array(rxl, int, NULL, 0);
1485 module_param_array(mac, int, NULL, 0);
1486 module_param(skip_pci_probe, bool, 0);
1487
1488 MODULE_LICENSE("GPL");
1489
1490
1491 int __init init_module( void )
1492 {
1493         struct net_device  *dev;
1494         int err;
1495
1496         while( num < SBNI_MAX_NUM_CARDS ) {
1497                 dev = alloc_netdev(sizeof(struct net_local), 
1498                                    "sbni%d", sbni_devsetup);
1499                 if( !dev)
1500                         break;
1501
1502                 sprintf( dev->name, "sbni%d", num );
1503
1504                 err = sbni_init(dev);
1505                 if (err) {
1506                         free_netdev(dev);
1507                         break;
1508                 }
1509
1510                 if( register_netdev( dev ) ) {
1511                         release_region( dev->base_addr, SBNI_IO_EXTENT );
1512                         free_netdev( dev );
1513                         break;
1514                 }
1515         }
1516
1517         return  *sbni_cards  ?  0  :  -ENODEV;
1518 }
1519
1520 void
1521 cleanup_module( void )
1522 {
1523         struct net_device  *dev;
1524         int  num;
1525
1526         for( num = 0;  num < SBNI_MAX_NUM_CARDS;  ++num )
1527                 if( (dev = sbni_cards[ num ]) != NULL ) {
1528                         unregister_netdev( dev );
1529                         release_region( dev->base_addr, SBNI_IO_EXTENT );
1530                         free_netdev( dev );
1531                 }
1532 }
1533
1534 #else   /* MODULE */
1535
1536 static int __init
1537 sbni_setup( char  *p )
1538 {
1539         int  n, parm;
1540
1541         if( *p++ != '(' )
1542                 goto  bad_param;
1543
1544         for( n = 0, parm = 0;  *p  &&  n < 8; ) {
1545                 (*dest[ parm ])[ n ] = simple_strtol( p, &p, 0 );
1546                 if( !*p  ||  *p == ')' )
1547                         return 1;
1548                 if( *p == ';' )
1549                         ++p, ++n, parm = 0;
1550                 else if( *p++ != ',' )
1551                         break;
1552                 else
1553                         if( ++parm >= 5 )
1554                                 break;
1555         }
1556 bad_param:
1557         printk( KERN_ERR "Error in sbni kernel parameter!\n" );
1558         return 0;
1559 }
1560
1561 __setup( "sbni=", sbni_setup );
1562
1563 #endif  /* MODULE */
1564
1565 /* -------------------------------------------------------------------------- */
1566
1567 #ifdef ASM_CRC
1568
1569 static u32
1570 calc_crc32( u32  crc,  u8  *p,  u32  len )
1571 {
1572         register u32  _crc;
1573         _crc = crc;
1574         
1575         __asm__ __volatile__ (
1576                 "xorl   %%ebx, %%ebx\n"
1577                 "movl   %2, %%esi\n" 
1578                 "movl   %3, %%ecx\n" 
1579                 "movl   $crc32tab, %%edi\n"
1580                 "shrl   $2, %%ecx\n"
1581                 "jz     1f\n"
1582
1583                 ".align 4\n"
1584         "0:\n"
1585                 "movb   %%al, %%bl\n"
1586                 "movl   (%%esi), %%edx\n"
1587                 "shrl   $8, %%eax\n"
1588                 "xorb   %%dl, %%bl\n"
1589                 "shrl   $8, %%edx\n"
1590                 "xorl   (%%edi,%%ebx,4), %%eax\n"
1591
1592                 "movb   %%al, %%bl\n"
1593                 "shrl   $8, %%eax\n"
1594                 "xorb   %%dl, %%bl\n"
1595                 "shrl   $8, %%edx\n"
1596                 "xorl   (%%edi,%%ebx,4), %%eax\n"
1597
1598                 "movb   %%al, %%bl\n"
1599                 "shrl   $8, %%eax\n"
1600                 "xorb   %%dl, %%bl\n"
1601                 "movb   %%dh, %%dl\n" 
1602                 "xorl   (%%edi,%%ebx,4), %%eax\n"
1603
1604                 "movb   %%al, %%bl\n"
1605                 "shrl   $8, %%eax\n"
1606                 "xorb   %%dl, %%bl\n"
1607                 "addl   $4, %%esi\n"
1608                 "xorl   (%%edi,%%ebx,4), %%eax\n"
1609
1610                 "decl   %%ecx\n"
1611                 "jnz    0b\n"
1612
1613         "1:\n"
1614                 "movl   %3, %%ecx\n"
1615                 "andl   $3, %%ecx\n"
1616                 "jz     2f\n"
1617
1618                 "movb   %%al, %%bl\n"
1619                 "shrl   $8, %%eax\n"
1620                 "xorb   (%%esi), %%bl\n"
1621                 "xorl   (%%edi,%%ebx,4), %%eax\n"
1622
1623                 "decl   %%ecx\n"
1624                 "jz     2f\n"
1625
1626                 "movb   %%al, %%bl\n"
1627                 "shrl   $8, %%eax\n"
1628                 "xorb   1(%%esi), %%bl\n"
1629                 "xorl   (%%edi,%%ebx,4), %%eax\n"
1630
1631                 "decl   %%ecx\n"
1632                 "jz     2f\n"
1633
1634                 "movb   %%al, %%bl\n"
1635                 "shrl   $8, %%eax\n"
1636                 "xorb   2(%%esi), %%bl\n"
1637                 "xorl   (%%edi,%%ebx,4), %%eax\n"
1638         "2:\n"
1639                 : "=a" (_crc)
1640                 : "0" (_crc), "g" (p), "g" (len)
1641                 : "bx", "cx", "dx", "si", "di"
1642         );
1643
1644         return  _crc;
1645 }
1646
1647 #else   /* ASM_CRC */
1648
1649 static u32
1650 calc_crc32( u32  crc,  u8  *p,  u32  len )
1651 {
1652         while( len-- )
1653                 crc = CRC32( *p++, crc );
1654
1655         return  crc;
1656 }
1657
1658 #endif  /* ASM_CRC */
1659
1660
1661 static u32  crc32tab[] __attribute__ ((aligned(8))) = {
1662         0xD202EF8D,  0xA505DF1B,  0x3C0C8EA1,  0x4B0BBE37,
1663         0xD56F2B94,  0xA2681B02,  0x3B614AB8,  0x4C667A2E,
1664         0xDCD967BF,  0xABDE5729,  0x32D70693,  0x45D03605,
1665         0xDBB4A3A6,  0xACB39330,  0x35BAC28A,  0x42BDF21C,
1666         0xCFB5FFE9,  0xB8B2CF7F,  0x21BB9EC5,  0x56BCAE53,
1667         0xC8D83BF0,  0xBFDF0B66,  0x26D65ADC,  0x51D16A4A,
1668         0xC16E77DB,  0xB669474D,  0x2F6016F7,  0x58672661,
1669         0xC603B3C2,  0xB1048354,  0x280DD2EE,  0x5F0AE278,
1670         0xE96CCF45,  0x9E6BFFD3,  0x0762AE69,  0x70659EFF,
1671         0xEE010B5C,  0x99063BCA,  0x000F6A70,  0x77085AE6,
1672         0xE7B74777,  0x90B077E1,  0x09B9265B,  0x7EBE16CD,
1673         0xE0DA836E,  0x97DDB3F8,  0x0ED4E242,  0x79D3D2D4,
1674         0xF4DBDF21,  0x83DCEFB7,  0x1AD5BE0D,  0x6DD28E9B,
1675         0xF3B61B38,  0x84B12BAE,  0x1DB87A14,  0x6ABF4A82,
1676         0xFA005713,  0x8D076785,  0x140E363F,  0x630906A9,
1677         0xFD6D930A,  0x8A6AA39C,  0x1363F226,  0x6464C2B0,
1678         0xA4DEAE1D,  0xD3D99E8B,  0x4AD0CF31,  0x3DD7FFA7,
1679         0xA3B36A04,  0xD4B45A92,  0x4DBD0B28,  0x3ABA3BBE,
1680         0xAA05262F,  0xDD0216B9,  0x440B4703,  0x330C7795,
1681         0xAD68E236,  0xDA6FD2A0,  0x4366831A,  0x3461B38C,
1682         0xB969BE79,  0xCE6E8EEF,  0x5767DF55,  0x2060EFC3,
1683         0xBE047A60,  0xC9034AF6,  0x500A1B4C,  0x270D2BDA,
1684         0xB7B2364B,  0xC0B506DD,  0x59BC5767,  0x2EBB67F1,
1685         0xB0DFF252,  0xC7D8C2C4,  0x5ED1937E,  0x29D6A3E8,
1686         0x9FB08ED5,  0xE8B7BE43,  0x71BEEFF9,  0x06B9DF6F,
1687         0x98DD4ACC,  0xEFDA7A5A,  0x76D32BE0,  0x01D41B76,
1688         0x916B06E7,  0xE66C3671,  0x7F6567CB,  0x0862575D,
1689         0x9606C2FE,  0xE101F268,  0x7808A3D2,  0x0F0F9344,
1690         0x82079EB1,  0xF500AE27,  0x6C09FF9D,  0x1B0ECF0B,
1691         0x856A5AA8,  0xF26D6A3E,  0x6B643B84,  0x1C630B12,
1692         0x8CDC1683,  0xFBDB2615,  0x62D277AF,  0x15D54739,
1693         0x8BB1D29A,  0xFCB6E20C,  0x65BFB3B6,  0x12B88320,
1694         0x3FBA6CAD,  0x48BD5C3B,  0xD1B40D81,  0xA6B33D17,
1695         0x38D7A8B4,  0x4FD09822,  0xD6D9C998,  0xA1DEF90E,
1696         0x3161E49F,  0x4666D409,  0xDF6F85B3,  0xA868B525,
1697         0x360C2086,  0x410B1010,  0xD80241AA,  0xAF05713C,
1698         0x220D7CC9,  0x550A4C5F,  0xCC031DE5,  0xBB042D73,
1699         0x2560B8D0,  0x52678846,  0xCB6ED9FC,  0xBC69E96A,
1700         0x2CD6F4FB,  0x5BD1C46D,  0xC2D895D7,  0xB5DFA541,
1701         0x2BBB30E2,  0x5CBC0074,  0xC5B551CE,  0xB2B26158,
1702         0x04D44C65,  0x73D37CF3,  0xEADA2D49,  0x9DDD1DDF,
1703         0x03B9887C,  0x74BEB8EA,  0xEDB7E950,  0x9AB0D9C6,
1704         0x0A0FC457,  0x7D08F4C1,  0xE401A57B,  0x930695ED,
1705         0x0D62004E,  0x7A6530D8,  0xE36C6162,  0x946B51F4,
1706         0x19635C01,  0x6E646C97,  0xF76D3D2D,  0x806A0DBB,
1707         0x1E0E9818,  0x6909A88E,  0xF000F934,  0x8707C9A2,
1708         0x17B8D433,  0x60BFE4A5,  0xF9B6B51F,  0x8EB18589,
1709         0x10D5102A,  0x67D220BC,  0xFEDB7106,  0x89DC4190,
1710         0x49662D3D,  0x3E611DAB,  0xA7684C11,  0xD06F7C87,
1711         0x4E0BE924,  0x390CD9B2,  0xA0058808,  0xD702B89E,
1712         0x47BDA50F,  0x30BA9599,  0xA9B3C423,  0xDEB4F4B5,
1713         0x40D06116,  0x37D75180,  0xAEDE003A,  0xD9D930AC,
1714         0x54D13D59,  0x23D60DCF,  0xBADF5C75,  0xCDD86CE3,
1715         0x53BCF940,  0x24BBC9D6,  0xBDB2986C,  0xCAB5A8FA,
1716         0x5A0AB56B,  0x2D0D85FD,  0xB404D447,  0xC303E4D1,
1717         0x5D677172,  0x2A6041E4,  0xB369105E,  0xC46E20C8,
1718         0x72080DF5,  0x050F3D63,  0x9C066CD9,  0xEB015C4F,
1719         0x7565C9EC,  0x0262F97A,  0x9B6BA8C0,  0xEC6C9856,
1720         0x7CD385C7,  0x0BD4B551,  0x92DDE4EB,  0xE5DAD47D,
1721         0x7BBE41DE,  0x0CB97148,  0x95B020F2,  0xE2B71064,
1722         0x6FBF1D91,  0x18B82D07,  0x81B17CBD,  0xF6B64C2B,
1723         0x68D2D988,  0x1FD5E91E,  0x86DCB8A4,  0xF1DB8832,
1724         0x616495A3,  0x1663A535,  0x8F6AF48F,  0xF86DC419,
1725         0x660951BA,  0x110E612C,  0x88073096,  0xFF000000
1726 };
1727