1 /****************************************************************************
2 * sdlamain.c WANPIPE(tm) Multiprotocol WAN Link Driver. Main module.
4 * Author: Nenad Corbic <ncorbic@sangoma.com>
7 * Copyright: (c) 1995-2000 Sangoma Technologies Inc.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 * ============================================================================
14 * Dec 22, 2000 Nenad Corbic Updated for 2.4.X kernels.
15 * Removed the polling routine.
16 * Nov 13, 2000 Nenad Corbic Added hw probing on module load and dynamic
18 * Nov 7, 2000 Nenad Corbic Fixed the Multi-Port PPP for kernels
20 * Aug 2, 2000 Nenad Corbic Block the Multi-Port PPP from running on
21 * kernels 2.2.16 or greater. The SyncPPP
23 * Jul 25, 2000 Nenad Corbic Updated the Piggiback support for MultPPPP.
24 * Jul 13, 2000 Nenad Corbic Added Multi-PPP support.
25 * Feb 02, 2000 Nenad Corbic Fixed up piggyback probing and selection.
26 * Sep 23, 1999 Nenad Corbic Added support for SMP
27 * Sep 13, 1999 Nenad Corbic Each port is treated as a separate device.
28 * Jun 02, 1999 Gideon Hack Added support for the S514 adapter.
29 * Updates for Linux 2.2.X kernels.
30 * Sep 17, 1998 Jaspreet Singh Updated for 2.1.121+ kernel
31 * Nov 28, 1997 Jaspreet Singh Changed DRV_RELEASE to 1
32 * Nov 10, 1997 Jaspreet Singh Changed sti() to restore_flags();
33 * Nov 06, 1997 Jaspreet Singh Changed DRV_VERSION to 4 and DRV_RELEASE to 0
34 * Oct 20, 1997 Jaspreet Singh Modified sdla_isr routine so that card->in_isr
35 * assignments are taken out and placed in the
36 * sdla_ppp.c, sdla_fr.c and sdla_x25.c isr
37 * routines. Took out 'wandev->tx_int_enabled' and
38 * replaced it with 'wandev->enable_tx_int'.
39 * May 29, 1997 Jaspreet Singh Flow Control Problem
40 * added "wandev->tx_int_enabled=1" line in the
41 * init module. This line initializes the flag for
42 * preventing Interrupt disabled with device set to
44 * Jan 15, 1997 Gene Kozin Version 3.1.0
45 * o added UDP management stuff
46 * Jan 02, 1997 Gene Kozin Initial version.
47 *****************************************************************************/
49 #include <linux/config.h> /* OS configuration options */
50 #include <linux/stddef.h> /* offsetof(), etc. */
51 #include <linux/errno.h> /* return codes */
52 #include <linux/string.h> /* inline memset(), etc. */
53 #include <linux/init.h>
54 #include <linux/slab.h> /* kmalloc(), kfree() */
55 #include <linux/kernel.h> /* printk(), and other useful stuff */
56 #include <linux/module.h> /* support for loadable modules */
57 #include <linux/ioport.h> /* request_region(), release_region() */
58 #include <linux/wanrouter.h> /* WAN router definitions */
59 #include <linux/wanpipe.h> /* WANPIPE common user API definitions */
60 #include <linux/rcupdate.h>
63 #include <asm/io.h> /* phys_to_virt() */
64 #include <linux/pci.h>
65 #include <linux/sdlapci.h>
66 #include <linux/if_wanpipe_common.h>
68 #include <asm/uaccess.h> /* kernel <-> user copy */
69 #include <linux/inetdevice.h>
72 #include <net/route.h>
74 #define KMEM_SAFETYZONE 8
77 #ifndef CONFIG_WANPIPE_FR
78 #define wpf_init(a,b) (-EPROTONOSUPPORT)
81 #ifndef CONFIG_WANPIPE_CHDLC
82 #define wpc_init(a,b) (-EPROTONOSUPPORT)
85 #ifndef CONFIG_WANPIPE_X25
86 #define wpx_init(a,b) (-EPROTONOSUPPORT)
89 #ifndef CONFIG_WANPIPE_PPP
90 #define wpp_init(a,b) (-EPROTONOSUPPORT)
93 #ifndef CONFIG_WANPIPE_MULTPPP
94 #define wsppp_init(a,b) (-EPROTONOSUPPORT)
98 /***********FOR DEBUGGING PURPOSES*********************************************
99 static void * dbg_kmalloc(unsigned int size, int prio, int line) {
101 void * v = kmalloc(size+sizeof(unsigned int)+2*KMEM_SAFETYZONE*8,prio);
103 c1 += sizeof(unsigned int);
104 *((unsigned int *)v) = size;
106 for (i = 0; i < KMEM_SAFETYZONE; i++) {
107 c1[0] = 'D'; c1[1] = 'E'; c1[2] = 'A'; c1[3] = 'D';
108 c1[4] = 'B'; c1[5] = 'E'; c1[6] = 'E'; c1[7] = 'F';
112 for (i = 0; i < KMEM_SAFETYZONE; i++) {
113 c1[0] = 'M'; c1[1] = 'U'; c1[2] = 'N'; c1[3] = 'G';
114 c1[4] = 'W'; c1[5] = 'A'; c1[6] = 'L'; c1[7] = 'L';
117 v = ((char *)v) + sizeof(unsigned int) + KMEM_SAFETYZONE*8;
118 printk(KERN_INFO "line %d kmalloc(%d,%d) = %p\n",line,size,prio,v);
121 static void dbg_kfree(void * v, int line) {
122 unsigned int * sp = (unsigned int *)(((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8));
123 unsigned int size = *sp;
124 char * c1 = ((char *)v) - KMEM_SAFETYZONE*8;
126 for (i = 0; i < KMEM_SAFETYZONE; i++) {
127 if ( c1[0] != 'D' || c1[1] != 'E' || c1[2] != 'A' || c1[3] != 'D'
128 || c1[4] != 'B' || c1[5] != 'E' || c1[6] != 'E' || c1[7] != 'F') {
129 printk(KERN_INFO "kmalloced block at %p has been corrupted (underrun)!\n",v);
130 printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2x\n", i*8,
131 c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
136 for (i = 0; i < KMEM_SAFETYZONE; i++) {
137 if ( c1[0] != 'M' || c1[1] != 'U' || c1[2] != 'N' || c1[3] != 'G'
138 || c1[4] != 'W' || c1[5] != 'A' || c1[6] != 'L' || c1[7] != 'L'
140 printk(KERN_INFO "kmalloced block at %p has been corrupted (overrun):\n",v);
141 printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2x\n", i*8,
142 c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
146 printk(KERN_INFO "line %d kfree(%p)\n",line,v);
147 v = ((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8);
151 #define kmalloc(x,y) dbg_kmalloc(x,y,__LINE__)
152 #define kfree(x) dbg_kfree(x,__LINE__)
153 ******************************************************************************/
157 /****** Defines & Macros ****************************************************/
162 #define STATIC static
165 #define DRV_VERSION 5 /* version number */
166 #define DRV_RELEASE 0 /* release (minor version) number */
167 #define MAX_CARDS 16 /* max number of adapters */
169 #ifndef CONFIG_WANPIPE_CARDS /* configurable option */
170 #define CONFIG_WANPIPE_CARDS 1
173 #define CMD_OK 0 /* normal firmware return code */
174 #define CMD_TIMEOUT 0xFF /* firmware command timed out */
175 #define MAX_CMD_RETRY 10 /* max number of firmware retries */
176 /****** Function Prototypes *************************************************/
178 extern void disable_irq(unsigned int);
179 extern void enable_irq(unsigned int);
181 /* WAN link driver entry points */
182 static int setup(struct wan_device* wandev, wandev_conf_t* conf);
183 static int shutdown(struct wan_device* wandev);
184 static int ioctl(struct wan_device* wandev, unsigned cmd, unsigned long arg);
187 static int ioctl_dump (sdla_t* card, sdla_dump_t* u_dump);
188 static int ioctl_exec (sdla_t* card, sdla_exec_t* u_exec, int);
190 /* Miscellaneous functions */
191 STATIC irqreturn_t sdla_isr (int irq, void* dev_id, struct pt_regs *regs);
192 static void release_hw (sdla_t *card);
194 static int check_s508_conflicts (sdla_t* card,wandev_conf_t* conf, int*);
195 static int check_s514_conflicts (sdla_t* card,wandev_conf_t* conf, int*);
198 /****** Global Data **********************************************************
199 * Note: All data must be explicitly initialized!!!
203 static char drvname[] = "wanpipe";
204 static char fullname[] = "WANPIPE(tm) Multiprotocol Driver";
205 static char copyright[] = "(c) 1995-2000 Sangoma Technologies Inc.";
207 static sdla_t* card_array; /* adapter data space */
209 /* Wanpipe's own workqueue, used for all API's.
210 * All protocol specific tasks will be inserted
211 * into the "wanpipe_wq" workqueue.
213 * The kernel workqueue mechanism will execute
214 * all pending tasks in the "wanpipe_wq" workqueue.
217 struct workqueue_struct *wanpipe_wq;
218 DECLARE_WORK(wanpipe_work, NULL, NULL);
220 static int wanpipe_bh_critical;
222 /******* Kernel Loadable Module Entry Points ********************************/
224 /*============================================================================
225 * Module 'insert' entry point.
226 * o print announcement
227 * o allocate adapter data space
228 * o initialize static data
229 * o register all cards with WAN router
230 * o calibrate SDLA shared memory access delay.
237 static int __init wanpipe_init(void)
241 printk(KERN_INFO "%s v%u.%u %s\n",
242 fullname, DRV_VERSION, DRV_RELEASE, copyright);
244 wanpipe_wq = create_workqueue("wanpipe_wq");
248 /* Probe for wanpipe cards and return the number found */
249 printk(KERN_INFO "wanpipe: Probing for WANPIPE hardware.\n");
250 ncards = wanpipe_hw_probe();
252 printk(KERN_INFO "wanpipe: Allocating maximum %i devices: wanpipe%i - wanpipe%i.\n",ncards,1,ncards);
254 printk(KERN_INFO "wanpipe: No S514/S508 cards found, unloading modules!\n");
255 destroy_workqueue(wanpipe_wq);
259 /* Verify number of cards and allocate adapter data space */
260 card_array = kmalloc(sizeof(sdla_t) * ncards, GFP_KERNEL);
261 if (card_array == NULL) {
262 destroy_workqueue(wanpipe_wq);
266 memset(card_array, 0, sizeof(sdla_t) * ncards);
268 /* Register adapters with WAN router */
269 for (cnt = 0; cnt < ncards; ++ cnt) {
270 sdla_t* card = &card_array[cnt];
271 struct wan_device* wandev = &card->wandev;
274 sprintf(card->devname, "%s%d", drvname, cnt + 1);
275 wandev->magic = ROUTER_MAGIC;
276 wandev->name = card->devname;
277 wandev->private = card;
278 wandev->enable_tx_int = 0;
279 wandev->setup = &setup;
280 wandev->shutdown = &shutdown;
281 wandev->ioctl = &ioctl;
282 err = register_wan_device(wandev);
285 "%s: %s registration failed with error %d!\n",
286 drvname, card->devname, err);
291 ncards = cnt; /* adjust actual number of cards */
294 destroy_workqueue(wanpipe_wq);
295 printk(KERN_INFO "IN Init Module: NO Cards registered\n");
302 /*============================================================================
303 * Module 'remove' entry point.
304 * o unregister all adapters from the WAN router
305 * o release all remaining system resources
307 static void __exit wanpipe_cleanup(void)
314 for (i = 0; i < ncards; ++i) {
315 sdla_t* card = &card_array[i];
316 unregister_wan_device(card->devname);
318 destroy_workqueue(wanpipe_wq);
321 printk(KERN_INFO "\nwanpipe: WANPIPE Modules Unloaded.\n");
324 module_init(wanpipe_init);
325 module_exit(wanpipe_cleanup);
327 /******* WAN Device Driver Entry Points *************************************/
329 /*============================================================================
330 * Setup/configure WAN link driver.
331 * o check adapter state
332 * o make sure firmware is present in configuration
333 * o make sure I/O port and IRQ are specified
334 * o make sure I/O region is available
335 * o allocate interrupt vector
336 * o setup SDLA hardware
337 * o call appropriate routine to perform protocol-specific initialization
338 * o mark I/O region as used
339 * o if this is the first active card, then schedule background task
341 * This function is called when router handles ROUTER_SETUP IOCTL. The
342 * configuration structure is in kernel memory (including extended data, if
346 static int setup(struct wan_device* wandev, wandev_conf_t* conf)
353 if ((wandev == NULL) || (wandev->private == NULL) || (conf == NULL)){
355 "%s: Failed Sdlamain Setup wandev %u, card %u, conf %u !\n",
357 (unsigned int)wandev,(unsigned int)wandev->private,
362 printk(KERN_INFO "%s: Starting WAN Setup\n", wandev->name);
364 card = wandev->private;
365 if (wandev->state != WAN_UNCONFIGURED){
366 printk(KERN_INFO "%s: failed sdlamain setup, busy!\n",
368 return -EBUSY; /* already configured */
371 printk(KERN_INFO "\nProcessing WAN device %s...\n", wandev->name);
373 /* Initialize the counters for each wandev
374 * Used for counting number of times new_if and
377 wandev->del_if_cnt = 0;
378 wandev->new_if_cnt = 0;
379 wandev->config_id = conf->config_id;
381 if (!conf->data_size || (conf->data == NULL)) {
383 "%s: firmware not found in configuration data!\n",
388 /* Check for resource conflicts and setup the
389 * card for piggibacking if necessary */
390 if(!conf->S514_CPU_no[0]) {
391 if ((err=check_s508_conflicts(card,conf,&irq)) != 0){
395 if ((err=check_s514_conflicts(card,conf,&irq)) != 0){
400 /* If the current card has already been configured
401 * or it's a piggyback card, do not try to allocate
404 if (!card->wandev.piggyback && !card->configured){
406 /* Configure hardware, load firmware, etc. */
407 memset(&card->hw, 0, sizeof(sdlahw_t));
409 /* for an S514 adapter, pass the CPU number and the slot number read */
410 /* from 'router.conf' to the 'sdla_setup()' function via the 'port' */
412 if (conf->S514_CPU_no[0]){
414 card->hw.S514_cpu_no[0] = conf->S514_CPU_no[0];
415 card->hw.S514_slot_no = conf->PCI_slot_no;
416 card->hw.auto_pci_cfg = conf->auto_pci_cfg;
418 if (card->hw.auto_pci_cfg == WANOPT_YES){
419 printk(KERN_INFO "%s: Setting CPU to %c and Slot to Auto\n",
420 card->devname, card->hw.S514_cpu_no[0]);
422 printk(KERN_INFO "%s: Setting CPU to %c and Slot to %i\n",
423 card->devname, card->hw.S514_cpu_no[0], card->hw.S514_slot_no);
427 /* 508 Card io port and irq initialization */
428 card->hw.port = conf->ioport;
429 card->hw.irq = (conf->irq == 9) ? 2 : conf->irq;
433 /* Compute the virtual address of the card in kernel space */
435 card->hw.dpmbase = phys_to_virt(conf->maddr);
437 card->hw.dpmbase = (void *)conf->maddr;
440 card->hw.dpmsize = SDLA_WINDOWSIZE;
442 /* set the adapter type if using an S514 adapter */
443 card->hw.type = (conf->S514_CPU_no[0]) ? SDLA_S514 : conf->hw_opt[0];
444 card->hw.pclk = conf->hw_opt[1];
446 err = sdla_setup(&card->hw, conf->data, conf->data_size);
448 printk(KERN_INFO "%s: Hardware setup Failed %i\n",
453 if(card->hw.type != SDLA_S514)
454 irq = (conf->irq == 2) ? 9 : conf->irq; /* IRQ2 -> IRQ9 */
458 /* request an interrupt vector - note that interrupts may be shared */
459 /* when using the S514 PCI adapter */
461 if(request_irq(irq, sdla_isr,
462 (card->hw.type == SDLA_S514) ? SA_SHIRQ : 0,
463 wandev->name, card)){
465 printk(KERN_INFO "%s: Can't reserve IRQ %d!\n", wandev->name, irq);
470 printk(KERN_INFO "%s: Card Configured %lu or Piggybacking %i!\n",
471 wandev->name,card->configured,card->wandev.piggyback);
475 if (!card->configured){
477 /* Initialize the Spin lock */
478 printk(KERN_INFO "%s: Initializing for SMP\n",wandev->name);
480 /* Piggyback spin lock has already been initialized,
481 * in check_s514/s508_conflicts() */
482 if (!card->wandev.piggyback){
483 spin_lock_init(&card->wandev.lock);
486 /* Intialize WAN device data space */
489 if(card->hw.type != SDLA_S514){
490 wandev->ioport = card->hw.port;
492 wandev->S514_cpu_no[0] = card->hw.S514_cpu_no[0];
493 wandev->S514_slot_no = card->hw.S514_slot_no;
495 wandev->maddr = (unsigned long)card->hw.dpmbase;
496 wandev->msize = card->hw.dpmsize;
497 wandev->hw_opt[0] = card->hw.type;
498 wandev->hw_opt[1] = card->hw.pclk;
499 wandev->hw_opt[2] = card->hw.memory;
500 wandev->hw_opt[3] = card->hw.fwid;
503 /* Protocol-specific initialization */
504 switch (card->hw.fwid) {
508 printk(KERN_INFO "%s: Starting X.25 Protocol Init.\n",
510 err = wpx_init(card, conf);
514 printk(KERN_INFO "%s: Starting Frame Relay Protocol Init.\n",
516 err = wpf_init(card, conf);
520 printk(KERN_INFO "%s: Starting PPP Protocol Init.\n",
522 err = wpp_init(card, conf);
528 printk(KERN_INFO "%s: Starting FT1 CSU/DSU Config Driver.\n",
530 err = wpft1_init(card, conf);
533 }else if (conf->config_id == WANCONFIG_MPPP){
534 printk(KERN_INFO "%s: Starting Multi-Port PPP Protocol Init.\n",
536 err = wsppp_init(card,conf);
540 printk(KERN_INFO "%s: Starting CHDLC Protocol Init.\n",
542 err = wpc_init(card, conf);
546 printk(KERN_INFO "%s: Error, Firmware is not supported %X %X!\n",
547 wandev->name,card->hw.fwid,SFID_CHDLC508);
548 err = -EPROTONOSUPPORT;
552 if (err == -EPROTONOSUPPORT){
554 "%s: Error, Protocol selected has not been compiled!\n",
557 "%s: Re-configure the kernel and re-build the modules!\n",
562 wandev->state = WAN_UNCONFIGURED;
567 /* Reserve I/O region and schedule background task */
568 if(card->hw.type != SDLA_S514 && !card->wandev.piggyback)
569 if (!request_region(card->hw.port, card->hw.io_range,
571 printk(KERN_WARNING "port 0x%04x busy\n", card->hw.port);
573 wandev->state = WAN_UNCONFIGURED;
577 /* Only use the polling routine for the X25 protocol */
579 card->wandev.critical=0;
583 /*==================================================================
584 * configure_s508_card
586 * For a S508 adapter, check for a possible configuration error in that
587 * we are loading an adapter in the same IO port as a previously loaded S508
591 static int check_s508_conflicts (sdla_t* card,wandev_conf_t* conf, int *irq)
593 unsigned long smp_flags;
596 if (conf->ioport <= 0) {
598 "%s: can't configure without I/O port address!\n",
603 if (conf->irq <= 0) {
604 printk(KERN_INFO "%s: can't configure without IRQ!\n",
609 if (test_bit(0,&card->configured))
613 /* Check for already loaded card with the same IO port and IRQ
614 * If found, copy its hardware configuration and use its
615 * resources (i.e. piggybacking)
618 for (i = 0; i < ncards; i++) {
619 sdla_t *nxt_card = &card_array[i];
621 /* Skip the current card ptr */
622 if (nxt_card == card)
626 /* Find a card that is already configured with the
628 if ((nxt_card->hw.type == SDLA_S508) &&
629 (nxt_card->hw.port == conf->ioport) &&
630 (nxt_card->next == NULL)){
632 /* We found a card the card that has same configuration
633 * as us. This means, that we must setup this card in
634 * piggibacking mode. However, only CHDLC and MPPP protocol
635 * support this setup */
637 if ((conf->config_id == WANCONFIG_CHDLC ||
638 conf->config_id == WANCONFIG_MPPP) &&
639 (nxt_card->wandev.config_id == WANCONFIG_CHDLC ||
640 nxt_card->wandev.config_id == WANCONFIG_MPPP)){
642 *irq = nxt_card->hw.irq;
643 memcpy(&card->hw, &nxt_card->hw, sizeof(sdlahw_t));
645 /* The master could already be running, we must
646 * set this as a critical area */
647 lock_adapter_irq(&nxt_card->wandev.lock, &smp_flags);
649 nxt_card->next = card;
650 card->next = nxt_card;
652 card->wandev.piggyback = WANOPT_YES;
654 /* We must initialise the piggiback spin lock here
655 * since isr will try to lock card->next if it
657 spin_lock_init(&card->wandev.lock);
659 unlock_adapter_irq(&nxt_card->wandev.lock, &smp_flags);
662 /* Trying to run piggibacking with a wrong protocol */
663 printk(KERN_INFO "%s: ERROR: Resource busy, ioport: 0x%x\n"
664 "%s: This protocol doesn't support\n"
665 "%s: multi-port operation!\n",
666 card->devname,nxt_card->hw.port,
667 card->devname,card->devname);
674 /* Make sure I/O port region is available only if we are the
675 * master device. If we are running in piggybacking mode,
676 * we will use the resources of the master card. */
677 if (!card->wandev.piggyback) {
678 struct resource *rr =
679 request_region(conf->ioport, SDLA_MAXIORANGE, "sdlamain");
680 release_region(conf->ioport, SDLA_MAXIORANGE);
684 "%s: I/O region 0x%X - 0x%X is in use!\n",
685 card->wandev.name, conf->ioport,
686 conf->ioport + SDLA_MAXIORANGE - 1);
694 /*==================================================================
695 * configure_s514_card
697 * For a S514 adapter, check for a possible configuration error in that
698 * we are loading an adapter in the same slot as a previously loaded S514
703 static int check_s514_conflicts(sdla_t* card,wandev_conf_t* conf, int *irq)
705 unsigned long smp_flags;
708 if (test_bit(0,&card->configured))
712 /* Check for already loaded card with the same IO port and IRQ
713 * If found, copy its hardware configuration and use its
714 * resources (i.e. piggybacking)
717 for (i = 0; i < ncards; i ++) {
719 sdla_t* nxt_card = &card_array[i];
723 if((nxt_card->hw.type == SDLA_S514) &&
724 (nxt_card->hw.S514_slot_no == conf->PCI_slot_no) &&
725 (nxt_card->hw.S514_cpu_no[0] == conf->S514_CPU_no[0])&&
726 (nxt_card->next == NULL)){
729 if ((conf->config_id == WANCONFIG_CHDLC ||
730 conf->config_id == WANCONFIG_MPPP) &&
731 (nxt_card->wandev.config_id == WANCONFIG_CHDLC ||
732 nxt_card->wandev.config_id == WANCONFIG_MPPP)){
734 *irq = nxt_card->hw.irq;
735 memcpy(&card->hw, &nxt_card->hw, sizeof(sdlahw_t));
737 /* The master could already be running, we must
738 * set this as a critical area */
739 lock_adapter_irq(&nxt_card->wandev.lock,&smp_flags);
740 nxt_card->next = card;
741 card->next = nxt_card;
743 card->wandev.piggyback = WANOPT_YES;
745 /* We must initialise the piggiback spin lock here
746 * since isr will try to lock card->next if it
748 spin_lock_init(&card->wandev.lock);
750 unlock_adapter_irq(&nxt_card->wandev.lock,&smp_flags);
753 /* Trying to run piggibacking with a wrong protocol */
754 printk(KERN_INFO "%s: ERROR: Resource busy: CPU %c PCISLOT %i\n"
755 "%s: This protocol doesn't support\n"
756 "%s: multi-port operation!\n",
758 conf->S514_CPU_no[0],conf->PCI_slot_no,
759 card->devname,card->devname);
770 /*============================================================================
771 * Shut down WAN link driver.
772 * o shut down adapter hardware
773 * o release system resources.
775 * This function is called by the router when device is being unregistered or
776 * when it handles ROUTER_DOWN IOCTL.
778 static int shutdown(struct wan_device* wandev)
784 if ((wandev == NULL) || (wandev->private == NULL)){
788 if (wandev->state == WAN_UNCONFIGURED){
792 card = wandev->private;
797 "%s: Shutdown Failed: TTY is still open\n",
803 wandev->state = WAN_UNCONFIGURED;
805 set_bit(PERI_CRIT,(void*)&wandev->critical);
807 /* In case of piggibacking, make sure that
808 * we never try to shutdown both devices at the same
809 * time, because they depend on one another */
811 if (card->disable_comm){
812 card->disable_comm(card);
815 /* Release Resources */
818 /* only free the allocated I/O range if not an S514 adapter */
819 if (wandev->hw_opt[0] != SDLA_S514 && !card->configured){
820 release_region(card->hw.port, card->hw.io_range);
823 if (!card->configured){
824 memset(&card->hw, 0, sizeof(sdlahw_t));
826 memset(&card->next->hw, 0, sizeof(sdlahw_t));
831 clear_bit(PERI_CRIT,(void*)&wandev->critical);
835 static void release_hw (sdla_t *card)
840 /* Check if next device exists */
842 nxt_card = card->next;
843 /* If next device is down then release resources */
844 if (nxt_card->wandev.state == WAN_UNCONFIGURED){
845 if (card->wandev.piggyback){
846 /* If this device is piggyback then use
847 * information of the master device
849 printk(KERN_INFO "%s: Piggyback shutting down\n",card->devname);
850 sdla_down(&card->next->hw);
851 free_irq(card->wandev.irq, card->next);
852 card->configured = 0;
853 card->next->configured = 0;
854 card->wandev.piggyback = 0;
856 /* Master device shutting down */
857 printk(KERN_INFO "%s: Master shutting down\n",card->devname);
858 sdla_down(&card->hw);
859 free_irq(card->wandev.irq, card);
860 card->configured = 0;
861 card->next->configured = 0;
864 printk(KERN_INFO "%s: Device still running %i\n",
865 nxt_card->devname,nxt_card->wandev.state);
867 card->configured = 1;
870 printk(KERN_INFO "%s: Master shutting down\n",card->devname);
871 sdla_down(&card->hw);
872 free_irq(card->wandev.irq, card);
873 card->configured = 0;
879 /*============================================================================
880 * Driver I/O control.
882 * o perform requested action
884 * This function is called when router handles one of the reserved user
885 * IOCTLs. Note that 'arg' stil points to user address space.
887 static int ioctl(struct wan_device* wandev, unsigned cmd, unsigned long arg)
893 if ((wandev == NULL) || (wandev->private == NULL))
895 if (wandev->state == WAN_UNCONFIGURED)
898 card = wandev->private;
900 if(card->hw.type != SDLA_S514){
901 disable_irq(card->hw.irq);
904 if (test_bit(SEND_CRIT, (void*)&wandev->critical)) {
910 err = ioctl_dump(wandev->private, (void*)arg);
914 err = ioctl_exec(wandev->private, (void*)arg, cmd);
923 /****** Driver IOCTL Handlers ***********************************************/
925 /*============================================================================
926 * Dump adapter memory to user buffer.
927 * o verify request structure
928 * o copy request structure to kernel data space
929 * o verify length/offset
930 * o verify user buffer
931 * o copy adapter memory image to user buffer
933 * Note: when dumping memory, this routine switches curent dual-port memory
934 * vector, so care must be taken to avoid racing conditions.
936 static int ioctl_dump (sdla_t* card, sdla_dump_t* u_dump)
940 unsigned long oldvec; /* DPM window vector */
941 unsigned long smp_flags;
944 if(copy_from_user((void*)&dump, (void*)u_dump, sizeof(sdla_dump_t)))
947 if ((dump.magic != WANPIPE_MAGIC) ||
948 (dump.offset + dump.length > card->hw.memory))
951 winsize = card->hw.dpmsize;
953 if(card->hw.type != SDLA_S514) {
955 lock_adapter_irq(&card->wandev.lock, &smp_flags);
957 oldvec = card->hw.vector;
958 while (dump.length) {
960 unsigned pos = dump.offset % winsize;
962 unsigned long vec = dump.offset - pos;
963 unsigned len = (dump.length > (winsize - pos)) ?
964 (winsize - pos) : dump.length;
965 /* relocate window */
966 if (sdla_mapmem(&card->hw, vec) != 0) {
971 if(copy_to_user((void *)dump.ptr,
972 (u8 *)card->hw.dpmbase + pos, len)){
974 unlock_adapter_irq(&card->wandev.lock, &smp_flags);
980 dump.ptr = (char*)dump.ptr + len;
983 sdla_mapmem(&card->hw, oldvec);/* restore DPM window position */
984 unlock_adapter_irq(&card->wandev.lock, &smp_flags);
988 if(copy_to_user((void *)dump.ptr,
989 (u8 *)card->hw.dpmbase + dump.offset, dump.length)){
997 /*============================================================================
998 * Execute adapter firmware command.
999 * o verify request structure
1000 * o copy request structure to kernel data space
1001 * o call protocol-specific 'exec' function
1003 static int ioctl_exec (sdla_t* card, sdla_exec_t* u_exec, int cmd)
1008 if (card->exec == NULL && cmd == WANPIPE_EXEC){
1012 if(copy_from_user((void*)&exec, (void*)u_exec, sizeof(sdla_exec_t)))
1015 if ((exec.magic != WANPIPE_MAGIC) || (exec.cmd == NULL))
1020 err = card->exec(card, exec.cmd, exec.data);
1026 /******* Miscellaneous ******************************************************/
1028 /*============================================================================
1029 * SDLA Interrupt Service Routine.
1030 * o acknowledge SDLA hardware interrupt.
1031 * o call protocol-specific interrupt service routine, if any.
1033 STATIC irqreturn_t sdla_isr (int irq, void* dev_id, struct pt_regs *regs)
1035 #define card ((sdla_t*)dev_id)
1037 if(card->hw.type == SDLA_S514) { /* handle interrrupt on S514 */
1039 unsigned char CPU_no = card->hw.S514_cpu_no[0];
1040 unsigned char card_found_for_IRQ;
1045 read_S514_int_stat(&card->hw, &int_status);
1047 /* check if the interrupt is for this device */
1048 if(!((unsigned char)int_status &
1049 (IRQ_CPU_A | IRQ_CPU_B)))
1052 /* if the IRQ is for both CPUs on the same adapter, */
1053 /* then alter the interrupt status so as to handle */
1054 /* one CPU at a time */
1055 if(((unsigned char)int_status & (IRQ_CPU_A | IRQ_CPU_B))
1056 == (IRQ_CPU_A | IRQ_CPU_B)) {
1057 int_status &= (CPU_no == S514_CPU_A) ?
1058 ~IRQ_CPU_B : ~IRQ_CPU_A;
1061 card_found_for_IRQ = 0;
1063 /* check to see that the CPU number for this device */
1064 /* corresponds to the interrupt status read */
1067 if((unsigned char)int_status &
1069 card_found_for_IRQ = 1;
1073 if((unsigned char)int_status &
1075 card_found_for_IRQ = 1;
1079 /* exit if the interrupt is for another CPU on the */
1081 if(!card_found_for_IRQ)
1085 (card->wandev.state == WAN_UNCONFIGURED && !card->configured)){
1087 "Received IRQ %d for CPU #%c\n",
1090 "IRQ for unconfigured adapter\n");
1091 S514_intack(&card->hw, int_status);
1097 "%s: interrupt re-entrancy on IRQ %d\n",
1098 card->devname, card->wandev.irq);
1099 S514_intack(&card->hw, int_status);
1103 spin_lock(&card->wandev.lock);
1105 spin_lock(&card->next->wandev.lock);
1108 S514_intack(&card->hw, int_status);
1113 spin_unlock(&card->next->wandev.lock);
1115 spin_unlock(&card->wandev.lock);
1117 /* handle a maximum of two interrupts (one for each */
1118 /* CPU on the adapter) before returning */
1119 if((++ IRQ_count) == 2)
1124 else { /* handle interrupt on S508 adapter */
1126 if (!card || ((card->wandev.state == WAN_UNCONFIGURED) && !card->configured))
1131 "%s: interrupt re-entrancy on IRQ %d!\n",
1132 card->devname, card->wandev.irq);
1136 spin_lock(&card->wandev.lock);
1138 spin_lock(&card->next->wandev.lock);
1141 sdla_intack(&card->hw);
1146 spin_unlock(&card->next->wandev.lock);
1148 spin_unlock(&card->wandev.lock);
1155 /*============================================================================
1156 * This routine is called by the protocol-specific modules when network
1157 * interface is being open. The only reason we need this, is because we
1158 * have to call MOD_INC_USE_COUNT, but cannot include 'module.h' where it's
1159 * defined more than once into the same kernel module.
1161 void wanpipe_open (sdla_t* card)
1166 /*============================================================================
1167 * This routine is called by the protocol-specific modules when network
1168 * interface is being closed. The only reason we need this, is because we
1169 * have to call MOD_DEC_USE_COUNT, but cannot include 'module.h' where it's
1170 * defined more than once into the same kernel module.
1172 void wanpipe_close (sdla_t* card)
1177 /*============================================================================
1178 * Set WAN device state.
1180 void wanpipe_set_state (sdla_t* card, int state)
1182 if (card->wandev.state != state) {
1185 printk (KERN_INFO "%s: link connected!\n",
1189 case WAN_CONNECTING:
1190 printk (KERN_INFO "%s: link connecting...\n",
1194 case WAN_DISCONNECTED:
1195 printk (KERN_INFO "%s: link disconnected!\n",
1199 card->wandev.state = state;
1201 card->state_tick = jiffies;
1204 sdla_t * wanpipe_find_card (char *name)
1207 for (cnt = 0; cnt < ncards; ++ cnt) {
1208 sdla_t* card = &card_array[cnt];
1209 if (!strcmp(card->devname,name))
1215 sdla_t * wanpipe_find_card_num (int num)
1217 if (num < 1 || num > ncards)
1220 return &card_array[num];
1224 * @work_pointer: work_struct to be done;
1225 * should already have PREPARE_WORK() or
1226 * INIT_WORK() done on it by caller;
1228 void wanpipe_queue_work (struct work_struct *work_pointer)
1230 if (test_and_set_bit(1, (void*)&wanpipe_bh_critical))
1231 printk(KERN_INFO "CRITICAL IN QUEUING WORK\n");
1233 queue_work(wanpipe_wq, work_pointer);
1234 clear_bit(1,(void*)&wanpipe_bh_critical);
1237 void wakeup_sk_bh(struct net_device *dev)
1239 wanpipe_common_t *chan = dev->priv;
1241 if (test_bit(0,&chan->common_critical))
1244 if (chan->sk && chan->tx_timer){
1245 chan->tx_timer->expires=jiffies+1;
1246 add_timer(chan->tx_timer);
1250 int change_dev_flags(struct net_device *dev, unsigned flags)
1252 struct ifreq if_info;
1253 mm_segment_t fs = get_fs();
1256 memset(&if_info, 0, sizeof(if_info));
1257 strcpy(if_info.ifr_name, dev->name);
1258 if_info.ifr_flags = flags;
1260 set_fs(get_ds()); /* get user space block */
1261 err = devinet_ioctl(SIOCSIFFLAGS, &if_info);
1267 unsigned long get_ip_address(struct net_device *dev, int option)
1270 struct in_ifaddr *ifaddr;
1271 struct in_device *in_dev;
1272 unsigned long addr = 0;
1275 if ((in_dev = __in_dev_get_rcu(dev)) == NULL){
1279 if ((ifaddr = in_dev->ifa_list)== NULL ){
1286 addr = ifaddr->ifa_local;
1289 case WAN_POINTOPOINT_IP:
1290 addr = ifaddr->ifa_address;
1293 case WAN_NETMASK_IP:
1294 addr = ifaddr->ifa_mask;
1297 case WAN_BROADCAST_IP:
1298 addr = ifaddr->ifa_broadcast;
1309 void add_gateway(sdla_t *card, struct net_device *dev)
1312 struct rtentry route;
1315 memset((char*)&route,0,sizeof(struct rtentry));
1317 ((struct sockaddr_in *)
1318 &(route.rt_dst))->sin_addr.s_addr = 0;
1319 ((struct sockaddr_in *)
1320 &(route.rt_dst))->sin_family = AF_INET;
1322 ((struct sockaddr_in *)
1323 &(route.rt_genmask))->sin_addr.s_addr = 0;
1324 ((struct sockaddr_in *)
1325 &(route.rt_genmask)) ->sin_family = AF_INET;
1329 route.rt_dev = dev->name;
1333 res = ip_rt_ioctl(SIOCADDRT,&route);
1337 printk(KERN_INFO "%s: Gateway added for %s\n",
1338 card->devname,dev->name);
1344 MODULE_LICENSE("GPL");
1346 /****** End *********************************************************/