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