OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [net/] [wan/] [sbni.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.