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