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