[PATCH] Represent laptop_mode as jiffies internally
[linux-2.6/verdex.git] / drivers / net / wan / sdlamain.c
blob7a8b22a7ea31e9f58660b4cad62b17d1ff6b1c09
1 /****************************************************************************
2 * sdlamain.c WANPIPE(tm) Multiprotocol WAN Link Driver. Main module.
4 * Author: Nenad Corbic <ncorbic@sangoma.com>
5 * Gideon Hack
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
17 * device allocation.
18 * Nov 7, 2000 Nenad Corbic Fixed the Multi-Port PPP for kernels
19 * 2.2.16 and above.
20 * Aug 2, 2000 Nenad Corbic Block the Multi-Port PPP from running on
21 * kernels 2.2.16 or greater. The SyncPPP
22 * has changed.
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
43 * busy
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>
62 #include <linux/in.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>
71 #include <linux/ip.h>
72 #include <net/route.h>
74 #define KMEM_SAFETYZONE 8
77 #ifndef CONFIG_WANPIPE_FR
78 #define wpf_init(a,b) (-EPROTONOSUPPORT)
79 #endif
81 #ifndef CONFIG_WANPIPE_CHDLC
82 #define wpc_init(a,b) (-EPROTONOSUPPORT)
83 #endif
85 #ifndef CONFIG_WANPIPE_X25
86 #define wpx_init(a,b) (-EPROTONOSUPPORT)
87 #endif
89 #ifndef CONFIG_WANPIPE_PPP
90 #define wpp_init(a,b) (-EPROTONOSUPPORT)
91 #endif
93 #ifndef CONFIG_WANPIPE_MULTPPP
94 #define wsppp_init(a,b) (-EPROTONOSUPPORT)
95 #endif
98 /***********FOR DEBUGGING PURPOSES*********************************************
99 static void * dbg_kmalloc(unsigned int size, int prio, int line) {
100 int i = 0;
101 void * v = kmalloc(size+sizeof(unsigned int)+2*KMEM_SAFETYZONE*8,prio);
102 char * c1 = v;
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';
109 c1 += 8;
111 c1 += size;
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';
115 c1 += 8;
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);
119 return 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;
125 int i = 0;
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] );
133 c1 += 8;
135 c1 += size;
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] );
144 c1 += 8;
146 printk(KERN_INFO "line %d kfree(%p)\n",line,v);
147 v = ((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8);
148 kfree(v);
151 #define kmalloc(x,y) dbg_kmalloc(x,y,__LINE__)
152 #define kfree(x) dbg_kfree(x,__LINE__)
153 ******************************************************************************/
157 /****** Defines & Macros ****************************************************/
159 #ifdef _DEBUG_
160 #define STATIC
161 #else
162 #define STATIC static
163 #endif
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
171 #endif
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);
186 /* IOCTL handlers */
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!!!
202 /* private data */
203 static char drvname[] = "wanpipe";
204 static char fullname[] = "WANPIPE(tm) Multiprotocol Driver";
205 static char copyright[] = "(c) 1995-2000 Sangoma Technologies Inc.";
206 static int ncards;
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.
232 * Return: 0 Ok
233 * < 0 error.
234 * Context: process
237 static int __init wanpipe_init(void)
239 int cnt, err = 0;
241 printk(KERN_INFO "%s v%u.%u %s\n",
242 fullname, DRV_VERSION, DRV_RELEASE, copyright);
244 wanpipe_wq = create_workqueue("wanpipe_wq");
245 if (!wanpipe_wq)
246 return -ENOMEM;
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();
251 if (ncards){
252 printk(KERN_INFO "wanpipe: Allocating maximum %i devices: wanpipe%i - wanpipe%i.\n",ncards,1,ncards);
253 }else{
254 printk(KERN_INFO "wanpipe: No S514/S508 cards found, unloading modules!\n");
255 destroy_workqueue(wanpipe_wq);
256 return -ENODEV;
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);
263 return -ENOMEM;
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;
273 card->next = NULL;
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);
283 if (err) {
284 printk(KERN_INFO
285 "%s: %s registration failed with error %d!\n",
286 drvname, card->devname, err);
287 break;
290 if (cnt){
291 ncards = cnt; /* adjust actual number of cards */
292 }else {
293 kfree(card_array);
294 destroy_workqueue(wanpipe_wq);
295 printk(KERN_INFO "IN Init Module: NO Cards registered\n");
296 err = -ENODEV;
299 return err;
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)
309 int i;
311 if (!ncards)
312 return;
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);
319 kfree(card_array);
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
343 * any).
346 static int setup(struct wan_device* wandev, wandev_conf_t* conf)
348 sdla_t* card;
349 int err = 0;
350 int irq=0;
352 /* Sanity checks */
353 if ((wandev == NULL) || (wandev->private == NULL) || (conf == NULL)){
354 printk(KERN_INFO
355 "%s: Failed Sdlamain Setup wandev %u, card %u, conf %u !\n",
356 wandev->name,
357 (unsigned int)wandev,(unsigned int)wandev->private,
358 (unsigned int)conf);
359 return -EFAULT;
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",
367 wandev->name);
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
375 * del_if get called.
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)) {
382 printk(KERN_INFO
383 "%s: firmware not found in configuration data!\n",
384 wandev->name);
385 return -EINVAL;
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){
392 return err;
394 }else {
395 if ((err=check_s514_conflicts(card,conf,&irq)) != 0){
396 return err;
400 /* If the current card has already been configured
401 * or it's a piggyback card, do not try to allocate
402 * resources.
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' */
411 /* parameter */
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]);
421 }else{
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);
426 }else{
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 */
434 if(conf->maddr){
435 card->hw.dpmbase = phys_to_virt(conf->maddr);
436 }else{
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);
447 if (err){
448 printk(KERN_INFO "%s: Hardware setup Failed %i\n",
449 card->devname,err);
450 return err;
453 if(card->hw.type != SDLA_S514)
454 irq = (conf->irq == 2) ? 9 : conf->irq; /* IRQ2 -> IRQ9 */
455 else
456 irq = card->hw.irq;
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);
466 return -EINVAL;
469 }else{
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 */
487 wandev->irq = irq;
488 wandev->dma = 0;
489 if(card->hw.type != SDLA_S514){
490 wandev->ioport = card->hw.port;
491 }else{
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) {
506 case SFID_X25_502:
507 case SFID_X25_508:
508 printk(KERN_INFO "%s: Starting X.25 Protocol Init.\n",
509 card->devname);
510 err = wpx_init(card, conf);
511 break;
512 case SFID_FR502:
513 case SFID_FR508:
514 printk(KERN_INFO "%s: Starting Frame Relay Protocol Init.\n",
515 card->devname);
516 err = wpf_init(card, conf);
517 break;
518 case SFID_PPP502:
519 case SFID_PPP508:
520 printk(KERN_INFO "%s: Starting PPP Protocol Init.\n",
521 card->devname);
522 err = wpp_init(card, conf);
523 break;
525 case SFID_CHDLC508:
526 case SFID_CHDLC514:
527 if (conf->ft1){
528 printk(KERN_INFO "%s: Starting FT1 CSU/DSU Config Driver.\n",
529 card->devname);
530 err = wpft1_init(card, conf);
531 break;
533 }else if (conf->config_id == WANCONFIG_MPPP){
534 printk(KERN_INFO "%s: Starting Multi-Port PPP Protocol Init.\n",
535 card->devname);
536 err = wsppp_init(card,conf);
537 break;
539 }else{
540 printk(KERN_INFO "%s: Starting CHDLC Protocol Init.\n",
541 card->devname);
542 err = wpc_init(card, conf);
543 break;
545 default:
546 printk(KERN_INFO "%s: Error, Firmware is not supported %X %X!\n",
547 wandev->name,card->hw.fwid,SFID_CHDLC508);
548 err = -EPROTONOSUPPORT;
551 if (err != 0){
552 if (err == -EPROTONOSUPPORT){
553 printk(KERN_INFO
554 "%s: Error, Protocol selected has not been compiled!\n",
555 card->devname);
556 printk(KERN_INFO
557 "%s: Re-configure the kernel and re-build the modules!\n",
558 card->devname);
561 release_hw(card);
562 wandev->state = WAN_UNCONFIGURED;
563 return err;
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,
570 wandev->name)) {
571 printk(KERN_WARNING "port 0x%04x busy\n", card->hw.port);
572 release_hw(card);
573 wandev->state = WAN_UNCONFIGURED;
574 return -EBUSY;
577 /* Only use the polling routine for the X25 protocol */
579 card->wandev.critical=0;
580 return 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
588 * card.
591 static int check_s508_conflicts (sdla_t* card,wandev_conf_t* conf, int *irq)
593 unsigned long smp_flags;
594 int i;
596 if (conf->ioport <= 0) {
597 printk(KERN_INFO
598 "%s: can't configure without I/O port address!\n",
599 card->wandev.name);
600 return -EINVAL;
603 if (conf->irq <= 0) {
604 printk(KERN_INFO "%s: can't configure without IRQ!\n",
605 card->wandev.name);
606 return -EINVAL;
609 if (test_bit(0,&card->configured))
610 return 0;
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)
623 continue;
626 /* Find a card that is already configured with the
627 * same IO Port */
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
656 * exists */
657 spin_lock_init(&card->wandev.lock);
659 unlock_adapter_irq(&nxt_card->wandev.lock, &smp_flags);
660 break;
661 }else{
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);
668 return -EEXIST;
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);
682 if (!rr) {
683 printk(KERN_INFO
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);
687 return -EINVAL;
691 return 0;
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
699 * card.
703 static int check_s514_conflicts(sdla_t* card,wandev_conf_t* conf, int *irq)
705 unsigned long smp_flags;
706 int i;
708 if (test_bit(0,&card->configured))
709 return 0;
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];
720 if(nxt_card == card)
721 continue;
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
747 * exists */
748 spin_lock_init(&card->wandev.lock);
750 unlock_adapter_irq(&nxt_card->wandev.lock,&smp_flags);
752 }else{
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",
757 card->devname,
758 conf->S514_CPU_no[0],conf->PCI_slot_no,
759 card->devname,card->devname);
760 return -EEXIST;
765 return 0;
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)
780 sdla_t *card;
781 int err=0;
783 /* sanity checks */
784 if ((wandev == NULL) || (wandev->private == NULL)){
785 return -EFAULT;
788 if (wandev->state == WAN_UNCONFIGURED){
789 return 0;
792 card = wandev->private;
794 if (card->tty_opt){
795 if (card->tty_open){
796 printk(KERN_INFO
797 "%s: Shutdown Failed: TTY is still open\n",
798 card->devname);
799 return -EBUSY;
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 */
816 release_hw(card);
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));
825 if (card->next){
826 memset(&card->next->hw, 0, sizeof(sdlahw_t));
831 clear_bit(PERI_CRIT,(void*)&wandev->critical);
832 return err;
835 static void release_hw (sdla_t *card)
837 sdla_t *nxt_card;
840 /* Check if next device exists */
841 if (card->next){
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;
855 }else{
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;
863 }else{
864 printk(KERN_INFO "%s: Device still running %i\n",
865 nxt_card->devname,nxt_card->wandev.state);
867 card->configured = 1;
869 }else{
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;
875 return;
879 /*============================================================================
880 * Driver I/O control.
881 * o verify arguments
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)
889 sdla_t* card;
890 int err;
892 /* sanity checks */
893 if ((wandev == NULL) || (wandev->private == NULL))
894 return -EFAULT;
895 if (wandev->state == WAN_UNCONFIGURED)
896 return -ENODEV;
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)) {
905 return -EAGAIN;
908 switch (cmd) {
909 case WANPIPE_DUMP:
910 err = ioctl_dump(wandev->private, (void*)arg);
911 break;
913 case WANPIPE_EXEC:
914 err = ioctl_exec(wandev->private, (void*)arg, cmd);
915 break;
916 default:
917 err = -EINVAL;
920 return err;
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)
938 sdla_dump_t dump;
939 unsigned winsize;
940 unsigned long oldvec; /* DPM window vector */
941 unsigned long smp_flags;
942 int err = 0;
944 if(copy_from_user((void*)&dump, (void*)u_dump, sizeof(sdla_dump_t)))
945 return -EFAULT;
947 if ((dump.magic != WANPIPE_MAGIC) ||
948 (dump.offset + dump.length > card->hw.memory))
949 return -EINVAL;
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) {
959 /* current offset */
960 unsigned pos = dump.offset % winsize;
961 /* current vector */
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) {
967 err = -EIO;
968 break;
971 if(copy_to_user((void *)dump.ptr,
972 (u8 *)card->hw.dpmbase + pos, len)){
974 unlock_adapter_irq(&card->wandev.lock, &smp_flags);
975 return -EFAULT;
978 dump.length -= len;
979 dump.offset += len;
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);
986 }else {
988 if(copy_to_user((void *)dump.ptr,
989 (u8 *)card->hw.dpmbase + dump.offset, dump.length)){
990 return -EFAULT;
994 return err;
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)
1005 sdla_exec_t exec;
1006 int err=0;
1008 if (card->exec == NULL && cmd == WANPIPE_EXEC){
1009 return -ENODEV;
1012 if(copy_from_user((void*)&exec, (void*)u_exec, sizeof(sdla_exec_t)))
1013 return -EFAULT;
1015 if ((exec.magic != WANPIPE_MAGIC) || (exec.cmd == NULL))
1016 return -EINVAL;
1018 switch (cmd) {
1019 case WANPIPE_EXEC:
1020 err = card->exec(card, exec.cmd, exec.data);
1021 break;
1023 return err;
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 */
1038 u32 int_status;
1039 unsigned char CPU_no = card->hw.S514_cpu_no[0];
1040 unsigned char card_found_for_IRQ;
1041 u8 IRQ_count = 0;
1043 for(;;) {
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)))
1050 return IRQ_HANDLED;
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 */
1065 switch (CPU_no) {
1066 case S514_CPU_A:
1067 if((unsigned char)int_status &
1068 IRQ_CPU_A)
1069 card_found_for_IRQ = 1;
1070 break;
1072 case S514_CPU_B:
1073 if((unsigned char)int_status &
1074 IRQ_CPU_B)
1075 card_found_for_IRQ = 1;
1076 break;
1079 /* exit if the interrupt is for another CPU on the */
1080 /* same IRQ */
1081 if(!card_found_for_IRQ)
1082 return IRQ_HANDLED;
1084 if (!card ||
1085 (card->wandev.state == WAN_UNCONFIGURED && !card->configured)){
1086 printk(KERN_INFO
1087 "Received IRQ %d for CPU #%c\n",
1088 irq, CPU_no);
1089 printk(KERN_INFO
1090 "IRQ for unconfigured adapter\n");
1091 S514_intack(&card->hw, int_status);
1092 return IRQ_HANDLED;
1095 if (card->in_isr) {
1096 printk(KERN_INFO
1097 "%s: interrupt re-entrancy on IRQ %d\n",
1098 card->devname, card->wandev.irq);
1099 S514_intack(&card->hw, int_status);
1100 return IRQ_HANDLED;
1103 spin_lock(&card->wandev.lock);
1104 if (card->next){
1105 spin_lock(&card->next->wandev.lock);
1108 S514_intack(&card->hw, int_status);
1109 if (card->isr)
1110 card->isr(card);
1112 if (card->next){
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)
1120 return IRQ_HANDLED;
1124 else { /* handle interrupt on S508 adapter */
1126 if (!card || ((card->wandev.state == WAN_UNCONFIGURED) && !card->configured))
1127 return IRQ_HANDLED;
1129 if (card->in_isr) {
1130 printk(KERN_INFO
1131 "%s: interrupt re-entrancy on IRQ %d!\n",
1132 card->devname, card->wandev.irq);
1133 return IRQ_HANDLED;
1136 spin_lock(&card->wandev.lock);
1137 if (card->next){
1138 spin_lock(&card->next->wandev.lock);
1141 sdla_intack(&card->hw);
1142 if (card->isr)
1143 card->isr(card);
1145 if (card->next){
1146 spin_unlock(&card->next->wandev.lock);
1148 spin_unlock(&card->wandev.lock);
1151 return IRQ_HANDLED;
1152 #undef card
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)
1163 ++card->open_cnt;
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)
1174 --card->open_cnt;
1177 /*============================================================================
1178 * Set WAN device state.
1180 void wanpipe_set_state (sdla_t* card, int state)
1182 if (card->wandev.state != state) {
1183 switch (state) {
1184 case WAN_CONNECTED:
1185 printk (KERN_INFO "%s: link connected!\n",
1186 card->devname);
1187 break;
1189 case WAN_CONNECTING:
1190 printk (KERN_INFO "%s: link connecting...\n",
1191 card->devname);
1192 break;
1194 case WAN_DISCONNECTED:
1195 printk (KERN_INFO "%s: link disconnected!\n",
1196 card->devname);
1197 break;
1199 card->wandev.state = state;
1201 card->state_tick = jiffies;
1204 sdla_t * wanpipe_find_card (char *name)
1206 int cnt;
1207 for (cnt = 0; cnt < ncards; ++ cnt) {
1208 sdla_t* card = &card_array[cnt];
1209 if (!strcmp(card->devname,name))
1210 return card;
1212 return NULL;
1215 sdla_t * wanpipe_find_card_num (int num)
1217 if (num < 1 || num > ncards)
1218 return NULL;
1219 num--;
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))
1242 return;
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();
1254 int err;
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);
1262 set_fs(fs);
1264 return err;
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;
1274 rcu_read_lock();
1275 if ((in_dev = __in_dev_get_rcu(dev)) == NULL){
1276 goto out;
1279 if ((ifaddr = in_dev->ifa_list)== NULL ){
1280 goto out;
1283 switch (option){
1285 case WAN_LOCAL_IP:
1286 addr = ifaddr->ifa_local;
1287 break;
1289 case WAN_POINTOPOINT_IP:
1290 addr = ifaddr->ifa_address;
1291 break;
1293 case WAN_NETMASK_IP:
1294 addr = ifaddr->ifa_mask;
1295 break;
1297 case WAN_BROADCAST_IP:
1298 addr = ifaddr->ifa_broadcast;
1299 break;
1300 default:
1301 break;
1304 out:
1305 rcu_read_unlock();
1306 return addr;
1309 void add_gateway(sdla_t *card, struct net_device *dev)
1311 mm_segment_t oldfs;
1312 struct rtentry route;
1313 int res;
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;
1328 route.rt_flags = 0;
1329 route.rt_dev = dev->name;
1331 oldfs = get_fs();
1332 set_fs(get_ds());
1333 res = ip_rt_ioctl(SIOCADDRT,&route);
1334 set_fs(oldfs);
1336 if (res == 0){
1337 printk(KERN_INFO "%s: Gateway added for %s\n",
1338 card->devname,dev->name);
1341 return;
1344 MODULE_LICENSE("GPL");
1346 /****** End *********************************************************/