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