1 /****************************************************************************
2 * sdlamain.c	WANPIPE(tm) Multiprotocol WAN Link Driver.  Main module.
3 *
4 * Author:	Nenad Corbic	<ncorbic@sangoma.com>
5 *		Gideon Hack
6 *
7 * Copyright:	(c) 1995-2000 Sangoma Technologies Inc.
8 *
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 intializes 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 *****************************************************************************/
48 
49 #include <linux/version.h>
50 #include <linux/config.h>	/* OS configuration options */
51 #include <linux/stddef.h>	/* offsetof(), etc. */
52 #include <linux/errno.h>	/* return codes */
53 #include <linux/string.h>	/* inline memset(), etc. */
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 
61 #include <linux/in.h>
62 #include <asm/io.h>		/* phys_to_virt() */
63 #include <linux/pci.h>
64 #include <linux/sdlapci.h>
65 #include <linux/if_wanpipe_common.h>
66 
67 #if defined(LINUX_2_4)
68 
69  #include <asm/uaccess.h>	/* kernel <-> user copy */
70  #include <linux/inetdevice.h>
71  #define netdevice_t struct net_device
72 
73 #elif defined(LINUX_2_1)
74 
75  #include <asm/uaccess.h>	/* kernel <-> user copy */
76  #include <linux/inetdevice.h>
77  #define netdevice_t struct device
78 
79 #else
80 
81  #include <asm/segment.h>
82  #define devinet_ioctl(x,y) dev_ioctl(x,y)
83  #define netdevice_t struct device
84  #define test_and_set_bit set_bit
85  typedef unsigned long mm_segment_t;
86 #endif
87 
88 #include <linux/ip.h>
89 #include <net/route.h>
90 
91 #define KMEM_SAFETYZONE 8
92 
93 
94 #ifndef CONFIG_WANPIPE_FR
95   #define wpf_init(a,b) (-EPROTONOSUPPORT)
96 #endif
97 
98 #ifndef CONFIG_WANPIPE_CHDLC
99  #define wpc_init(a,b) (-EPROTONOSUPPORT)
100 #endif
101 
102 #ifndef CONFIG_WANPIPE_X25
103  #define wpx_init(a,b) (-EPROTONOSUPPORT)
104 #endif
105 
106 #ifndef CONFIG_WANPIPE_PPP
107  #define wpp_init(a,b) (-EPROTONOSUPPORT)
108 #endif
109 
110 #ifndef CONFIG_WANPIPE_MULTPPP
111  #define wsppp_init(a,b) (-EPROTONOSUPPORT)
112 #endif
113 
114 
115 /***********FOR DEBUGGING PURPOSES*********************************************
116 static void * dbg_kmalloc(unsigned int size, int prio, int line) {
117 	int i = 0;
118 	void * v = kmalloc(size+sizeof(unsigned int)+2*KMEM_SAFETYZONE*8,prio);
119 	char * c1 = v;
120 	c1 += sizeof(unsigned int);
121 	*((unsigned int *)v) = size;
122 
123 	for (i = 0; i < KMEM_SAFETYZONE; i++) {
124 		c1[0] = 'D'; c1[1] = 'E'; c1[2] = 'A'; c1[3] = 'D';
125 		c1[4] = 'B'; c1[5] = 'E'; c1[6] = 'E'; c1[7] = 'F';
126 		c1 += 8;
127 	}
128 	c1 += size;
129 	for (i = 0; i < KMEM_SAFETYZONE; i++) {
130 		c1[0] = 'M'; c1[1] = 'U'; c1[2] = 'N'; c1[3] = 'G';
131 		c1[4] = 'W'; c1[5] = 'A'; c1[6] = 'L'; c1[7] = 'L';
132 		c1 += 8;
133 	}
134 	v = ((char *)v) + sizeof(unsigned int) + KMEM_SAFETYZONE*8;
135 	printk(KERN_INFO "line %d  kmalloc(%d,%d) = %p\n",line,size,prio,v);
136 	return v;
137 }
138 static void dbg_kfree(void * v, int line) {
139 	unsigned int * sp = (unsigned int *)(((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8));
140 	unsigned int size = *sp;
141 	char * c1 = ((char *)v) - KMEM_SAFETYZONE*8;
142 	int i = 0;
143 	for (i = 0; i < KMEM_SAFETYZONE; i++) {
144 		if (   c1[0] != 'D' || c1[1] != 'E' || c1[2] != 'A' || c1[3] != 'D'
145 		    || c1[4] != 'B' || c1[5] != 'E' || c1[6] != 'E' || c1[7] != 'F') {
146 			printk(KERN_INFO "kmalloced block at %p has been corrupted (underrun)!\n",v);
147 			printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2x\n", i*8,
148 			                c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
149 		}
150 		c1 += 8;
151 	}
152 	c1 += size;
153 	for (i = 0; i < KMEM_SAFETYZONE; i++) {
154 		if (   c1[0] != 'M' || c1[1] != 'U' || c1[2] != 'N' || c1[3] != 'G'
155 		    || c1[4] != 'W' || c1[5] != 'A' || c1[6] != 'L' || c1[7] != 'L'
156 		   ) {
157 			printk(KERN_INFO "kmalloced block at %p has been corrupted (overrun):\n",v);
158 			printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2x\n", i*8,
159 			                c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
160 		}
161 		c1 += 8;
162 	}
163 	printk(KERN_INFO "line %d  kfree(%p)\n",line,v);
164 	v = ((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8);
165 	kfree(v);
166 }
167 
168 #define kmalloc(x,y) dbg_kmalloc(x,y,__LINE__)
169 #define kfree(x) dbg_kfree(x,__LINE__)
170 ******************************************************************************/
171 
172 
173 
174 /****** Defines & Macros ****************************************************/
175 
176 #ifdef	_DEBUG_
177 #define	STATIC
178 #else
179 #define	STATIC		static
180 #endif
181 
182 #define	DRV_VERSION	5		/* version number */
183 #define	DRV_RELEASE	0		/* release (minor version) number */
184 #define	MAX_CARDS	16		/* max number of adapters */
185 
186 #ifndef	CONFIG_WANPIPE_CARDS		/* configurable option */
187 #define	CONFIG_WANPIPE_CARDS 1
188 #endif
189 
190 #define	CMD_OK		0		/* normal firmware return code */
191 #define	CMD_TIMEOUT	0xFF		/* firmware command timed out */
192 #define	MAX_CMD_RETRY	10		/* max number of firmware retries */
193 /****** Function Prototypes *************************************************/
194 
195 extern void disable_irq(unsigned int);
196 extern void enable_irq(unsigned int);
197 
198 /* Module entry points */
199 int init_module (void);
200 void cleanup_module (void);
201 
202 /* WAN link driver entry points */
203 static int setup    (wan_device_t* wandev, wandev_conf_t* conf);
204 static int shutdown (wan_device_t* wandev);
205 static int ioctl    (wan_device_t* wandev, unsigned cmd, unsigned long arg);
206 
207 /* IOCTL handlers */
208 static int ioctl_dump	(sdla_t* card, sdla_dump_t* u_dump);
209 static int ioctl_exec	(sdla_t* card, sdla_exec_t* u_exec, int);
210 
211 /* Miscellaneous functions */
212 STATIC void sdla_isr	(int irq, void* dev_id, struct pt_regs *regs);
213 static void release_hw  (sdla_t *card);
214 static void run_wanpipe_tq (unsigned long);
215 
216 static int check_s508_conflicts (sdla_t* card,wandev_conf_t* conf, int*);
217 static int check_s514_conflicts (sdla_t* card,wandev_conf_t* conf, int*);
218 
219 
220 /****** Global Data **********************************************************
221  * Note: All data must be explicitly initialized!!!
222  */
223 
224 /* private data */
225 static char drvname[]	= "wanpipe";
226 static char fullname[]	= "WANPIPE(tm) Multiprotocol Driver";
227 static char copyright[]	= "(c) 1995-2000 Sangoma Technologies Inc.";
228 static int ncards = 0;
229 static sdla_t* card_array = NULL;	/* adapter data space */
230 
231 /* Wanpipe's own task queue, used for all API's.
232  * All protocol specific tasks will be instered
233  * into "wanpipe_tq_custom" task_queue.
234 
235  * On each rx_interrupt, the whole task queue
236  * (wanpipe_tq_custom) will be queued into
237  * IMMEDIATE_BH via wanpipe_mark_bh() call.
238 
239  * The IMMEDIATE_BH will execute run_wanpipe_tq()
240  * function, which will execute all pending,
241  * tasks in wanpipe_tq_custom queue */
242 
243 #ifdef LINUX_2_4
244 DECLARE_TASK_QUEUE(wanpipe_tq_custom);
245 static struct tq_struct wanpipe_tq_task =
246 {
247 	routine: (void (*)(void *)) run_wanpipe_tq,
248 	data: &wanpipe_tq_custom
249 };
250 #else
251 static struct tq_struct *wanpipe_tq_custom = NULL;
252 static struct tq_struct wanpipe_tq_task =
253 {
254 	NULL,
255 	0,
256 	(void *)(void *) run_wanpipe_tq,
257 	&wanpipe_tq_custom
258 };
259 #endif
260 
261 static int wanpipe_bh_critical=0;
262 
263 /******* Kernel Loadable Module Entry Points ********************************/
264 
265 /*============================================================================
266  * Module 'insert' entry point.
267  * o print announcement
268  * o allocate adapter data space
269  * o initialize static data
270  * o register all cards with WAN router
271  * o calibrate SDLA shared memory access delay.
272  *
273  * Return:	0	Ok
274  *		< 0	error.
275  * Context:	process
276  */
277 
278 #ifdef MODULE
init_module(void)279 int init_module (void)
280 #else
281 int wanpipe_init(void)
282 #endif
283 {
284 	int cnt, err = 0;
285 
286 	printk(KERN_INFO "%s v%u.%u %s\n",
287 		fullname, DRV_VERSION, DRV_RELEASE, copyright);
288 
289 	/* Probe for wanpipe cards and return the number found */
290 	printk(KERN_INFO "wanpipe: Probing for WANPIPE hardware.\n");
291 	ncards = wanpipe_hw_probe();
292 	if (ncards){
293 		printk(KERN_INFO "wanpipe: Allocating maximum %i devices: wanpipe%i - wanpipe%i.\n",ncards,1,ncards);
294 	}else{
295 		printk(KERN_INFO "wanpipe: No S514/S508 cards found, unloading modules!\n");
296 		return -ENODEV;
297 	}
298 
299 	/* Verify number of cards and allocate adapter data space */
300 	card_array = kmalloc(sizeof(sdla_t) * ncards, GFP_KERNEL);
301 	if (card_array == NULL)
302 		return -ENOMEM;
303 
304 	memset(card_array, 0, sizeof(sdla_t) * ncards);
305 
306 	/* Register adapters with WAN router */
307 	for (cnt = 0; cnt < ncards; ++ cnt) {
308 		sdla_t* card = &card_array[cnt];
309 		wan_device_t* wandev = &card->wandev;
310 
311 		card->next = NULL;
312 		sprintf(card->devname, "%s%d", drvname, cnt + 1);
313 		wandev->magic    = ROUTER_MAGIC;
314 		wandev->name     = card->devname;
315 		wandev->private  = card;
316 		wandev->enable_tx_int = 0;
317 		wandev->setup    = &setup;
318 		wandev->shutdown = &shutdown;
319 		wandev->ioctl    = &ioctl;
320 		err = register_wan_device(wandev);
321 		if (err) {
322 			printk(KERN_INFO
323 				"%s: %s registration failed with error %d!\n",
324 				drvname, card->devname, err);
325 			break;
326 		}
327 	}
328 	if (cnt){
329 		ncards = cnt;	/* adjust actual number of cards */
330 	}else {
331 		kfree(card_array);
332 		printk(KERN_INFO "IN Init Module: NO Cards registered\n");
333 		err = -ENODEV;
334 	}
335 
336 	return err;
337 }
338 
339 #ifdef MODULE
340 /*============================================================================
341  * Module 'remove' entry point.
342  * o unregister all adapters from the WAN router
343  * o release all remaining system resources
344  */
cleanup_module(void)345 void cleanup_module (void)
346 {
347 	int i;
348 
349 	if (!ncards)
350 		return;
351 
352 	for (i = 0; i < ncards; ++i) {
353 		sdla_t* card = &card_array[i];
354 		unregister_wan_device(card->devname);
355 	}
356 	kfree(card_array);
357 
358 	printk(KERN_INFO "\nwanpipe: WANPIPE Modules Unloaded.\n");
359 }
360 
361 #endif
362 
363 /******* WAN Device Driver Entry Points *************************************/
364 
365 /*============================================================================
366  * Setup/configure WAN link driver.
367  * o check adapter state
368  * o make sure firmware is present in configuration
369  * o make sure I/O port and IRQ are specified
370  * o make sure I/O region is available
371  * o allocate interrupt vector
372  * o setup SDLA hardware
373  * o call appropriate routine to perform protocol-specific initialization
374  * o mark I/O region as used
375  * o if this is the first active card, then schedule background task
376  *
377  * This function is called when router handles ROUTER_SETUP IOCTL. The
378  * configuration structure is in kernel memory (including extended data, if
379  * any).
380  */
381 
setup(wan_device_t * wandev,wandev_conf_t * conf)382 static int setup (wan_device_t* wandev, wandev_conf_t* conf)
383 {
384 	sdla_t* card;
385 	int err = 0;
386 	int irq=0;
387 
388 	/* Sanity checks */
389 	if ((wandev == NULL) || (wandev->private == NULL) || (conf == NULL)){
390 		printk(KERN_INFO
391 		      "%s: Failed Sdlamain Setup wandev %u, card %u, conf %u !\n",
392 		      wandev->name,
393 		      (unsigned int)wandev,(unsigned int)wandev->private,
394 		      (unsigned int)conf);
395 		return -EFAULT;
396 	}
397 
398 	printk(KERN_INFO "%s: Starting WAN Setup\n", wandev->name);
399 
400 	card = wandev->private;
401 	if (wandev->state != WAN_UNCONFIGURED){
402 		printk(KERN_INFO "%s: failed sdlamain setup, busy!\n",
403 			wandev->name);
404 		return -EBUSY;		/* already configured */
405 	}
406 
407 	printk(KERN_INFO "\nProcessing WAN device %s...\n", wandev->name);
408 
409 	/* Initialize the counters for each wandev
410 	 * Used for counting number of times new_if and
411          * del_if get called.
412 	 */
413 	wandev->del_if_cnt = 0;
414 	wandev->new_if_cnt = 0;
415 	wandev->config_id  = conf->config_id;
416 
417 	if (!conf->data_size || (conf->data == NULL)) {
418 		printk(KERN_INFO
419 			"%s: firmware not found in configuration data!\n",
420 			wandev->name);
421 		return -EINVAL;
422 	}
423 
424 	/* Check for resource conflicts and setup the
425 	 * card for piggibacking if necessary */
426 	if(!conf->S514_CPU_no[0]) {
427 		if ((err=check_s508_conflicts(card,conf,&irq)) != 0){
428 			return err;
429 		}
430 	}else {
431 		if ((err=check_s514_conflicts(card,conf,&irq)) != 0){
432 			return err;
433 		}
434 	}
435 
436 	/* If the current card has already been configured
437          * or its a piggyback card, do not try to allocate
438          * resources.
439 	 */
440 	if (!card->wandev.piggyback && !card->configured){
441 
442 		/* Configure hardware, load firmware, etc. */
443 		memset(&card->hw, 0, sizeof(sdlahw_t));
444 
445 		/* for an S514 adapter, pass the CPU number and the slot number read */
446 		/* from 'router.conf' to the 'sdla_setup()' function via the 'port' */
447 		/* parameter */
448 		if (conf->S514_CPU_no[0]){
449 
450 			card->hw.S514_cpu_no[0] = conf->S514_CPU_no[0];
451 			card->hw.S514_slot_no = conf->PCI_slot_no;
452 			card->hw.auto_pci_cfg = conf->auto_pci_cfg;
453 
454 			if (card->hw.auto_pci_cfg == WANOPT_YES){
455 				printk(KERN_INFO "%s: Setting CPU to %c and Slot to Auto\n",
456 				card->devname, card->hw.S514_cpu_no[0]);
457 			}else{
458 				printk(KERN_INFO "%s: Setting CPU to %c and Slot to %i\n",
459 				card->devname, card->hw.S514_cpu_no[0], card->hw.S514_slot_no);
460 			}
461 
462 		}else{
463 			/* 508 Card io port and irq initialization */
464 			card->hw.port = conf->ioport;
465 			card->hw.irq = (conf->irq == 9) ? 2 : conf->irq;
466 		}
467 
468 
469 		/* Compute the virtual address of the card in kernel space */
470 		if(conf->maddr){
471 			card->hw.dpmbase = phys_to_virt(conf->maddr);
472 		}else{
473 			card->hw.dpmbase = (void *)conf->maddr;
474 		}
475 
476 		card->hw.dpmsize = SDLA_WINDOWSIZE;
477 
478 		/* set the adapter type if using an S514 adapter */
479 		card->hw.type = (conf->S514_CPU_no[0]) ? SDLA_S514 : conf->hw_opt[0];
480 		card->hw.pclk = conf->hw_opt[1];
481 
482 		err = sdla_setup(&card->hw, conf->data, conf->data_size);
483 		if (err){
484 			printk(KERN_INFO "%s: Hardware setup Failed %i\n",
485 					card->devname,err);
486 			return err;
487 		}
488 
489 	        if(card->hw.type != SDLA_S514)
490 			irq = (conf->irq == 2) ? 9 : conf->irq; /* IRQ2 -> IRQ9 */
491 		else
492 			irq = card->hw.irq;
493 
494 		/* request an interrupt vector - note that interrupts may be shared */
495 		/* when using the S514 PCI adapter */
496 
497        		if(request_irq(irq, sdla_isr,
498 		      (card->hw.type == SDLA_S514) ? SA_SHIRQ : 0,
499 		       wandev->name, card)){
500 
501 			printk(KERN_INFO "%s: Can't reserve IRQ %d!\n", wandev->name, irq);
502 			return -EINVAL;
503 		}
504 
505 	}else{
506 		printk(KERN_INFO "%s: Card Configured %i or Piggybacking %i!\n",
507 			wandev->name,card->configured,card->wandev.piggyback);
508 	}
509 
510 
511 	if (!card->configured){
512 
513 		/* Initialize the Spin lock */
514 #if defined(CONFIG_SMP) || defined(LINUX_2_4)
515 		printk(KERN_INFO "%s: Initializing for SMP\n",wandev->name);
516 #endif
517 
518 		/* Piggyback spin lock has already been initialized,
519 		 * in check_s514/s508_conflicts() */
520 		if (!card->wandev.piggyback){
521 			spin_lock_init(&card->wandev.lock);
522 		}
523 
524 		/* Intialize WAN device data space */
525 		wandev->irq       = irq;
526 		wandev->dma       = 0;
527 		if(card->hw.type != SDLA_S514){
528 			wandev->ioport = card->hw.port;
529 		}else{
530 			wandev->S514_cpu_no[0] = card->hw.S514_cpu_no[0];
531 			wandev->S514_slot_no = card->hw.S514_slot_no;
532 		}
533 		wandev->maddr     = (unsigned long)card->hw.dpmbase;
534 		wandev->msize     = card->hw.dpmsize;
535 		wandev->hw_opt[0] = card->hw.type;
536 		wandev->hw_opt[1] = card->hw.pclk;
537 		wandev->hw_opt[2] = card->hw.memory;
538 		wandev->hw_opt[3] = card->hw.fwid;
539 	}
540 
541 	/* Protocol-specific initialization */
542 	switch (card->hw.fwid) {
543 
544 	case SFID_X25_502:
545 	case SFID_X25_508:
546 		printk(KERN_INFO "%s: Starting X.25 Protocol Init.\n",
547 				card->devname);
548 		err = wpx_init(card, conf);
549 		break;
550 	case SFID_FR502:
551 	case SFID_FR508:
552 		printk(KERN_INFO "%s: Starting Frame Relay Protocol Init.\n",
553 				card->devname);
554 		err = wpf_init(card, conf);
555 		break;
556 	case SFID_PPP502:
557 	case SFID_PPP508:
558 		printk(KERN_INFO "%s: Starting PPP Protocol Init.\n",
559 				card->devname);
560 		err = wpp_init(card, conf);
561 		break;
562 
563 	case SFID_CHDLC508:
564 	case SFID_CHDLC514:
565 		if (conf->ft1){
566 			printk(KERN_INFO "%s: Starting FT1 CSU/DSU Config Driver.\n",
567 				card->devname);
568 			err = wpft1_init(card, conf);
569 			break;
570 
571 		}else if (conf->config_id == WANCONFIG_MPPP){
572 			printk(KERN_INFO "%s: Starting Multi-Port PPP Protocol Init.\n",
573 					card->devname);
574 			err = wsppp_init(card,conf);
575 			break;
576 
577 		}else{
578 			printk(KERN_INFO "%s: Starting CHDLC Protocol Init.\n",
579 					card->devname);
580 			err = wpc_init(card, conf);
581 			break;
582 		}
583 	default:
584 		printk(KERN_INFO "%s: Error, Firmware is not supported %X %X!\n",
585 			wandev->name,card->hw.fwid,SFID_CHDLC508);
586 		err = -EPROTONOSUPPORT;
587 	}
588 
589 	if (err != 0){
590 		if (err == -EPROTONOSUPPORT){
591 			printk(KERN_INFO
592 				"%s: Error, Protocol selected has not been compiled!\n",
593 					card->devname);
594 			printk(KERN_INFO
595 				"%s:        Re-configure the kernel and re-build the modules!\n",
596 					card->devname);
597 		}
598 
599 		release_hw(card);
600 		wandev->state = WAN_UNCONFIGURED;
601 		return err;
602 	}
603 
604 
605   	/* Reserve I/O region and schedule background task */
606         if(card->hw.type != SDLA_S514 && !card->wandev.piggyback)
607 		if (!request_region(card->hw.port, card->hw.io_range,
608 				wandev->name)) {
609 			printk(KERN_WARNING "port 0x%04x busy\n", card->hw.port);
610 			release_hw(card);
611 			wandev->state = WAN_UNCONFIGURED;
612 			return -EBUSY;
613 	  }
614 
615 	/* Only use the polling routine for the X25 protocol */
616 
617 	card->wandev.critical=0;
618 	return 0;
619 }
620 
621 /*==================================================================
622  * configure_s508_card
623  *
624  * For a S508 adapter, check for a possible configuration error in that
625  * we are loading an adapter in the same IO port as a previously loaded S508
626  * card.
627  */
628 
check_s508_conflicts(sdla_t * card,wandev_conf_t * conf,int * irq)629 static int check_s508_conflicts (sdla_t* card,wandev_conf_t* conf, int *irq)
630 {
631 	unsigned long smp_flags;
632 	int i;
633 
634 	if (conf->ioport <= 0) {
635 		printk(KERN_INFO
636 		"%s: can't configure without I/O port address!\n",
637 		card->wandev.name);
638 		return -EINVAL;
639 	}
640 
641 	if (conf->irq <= 0) {
642 		printk(KERN_INFO "%s: can't configure without IRQ!\n",
643 		card->wandev.name);
644 		return -EINVAL;
645 	}
646 
647 	if (test_bit(0,&card->configured))
648 		return 0;
649 
650 
651 	/* Check for already loaded card with the same IO port and IRQ
652 	 * If found, copy its hardware configuration and use its
653 	 * resources (i.e. piggybacking)
654 	 */
655 
656 	for (i = 0; i < ncards; i++) {
657 		sdla_t *nxt_card = &card_array[i];
658 
659 		/* Skip the current card ptr */
660 		if (nxt_card == card)
661 			continue;
662 
663 
664 		/* Find a card that is already configured with the
665 		 * same IO Port */
666 		if ((nxt_card->hw.type == SDLA_S508) &&
667 		    (nxt_card->hw.port == conf->ioport) &&
668 		    (nxt_card->next == NULL)){
669 
670 			/* We found a card the card that has same configuration
671 			 * as us. This means, that we must setup this card in
672 			 * piggibacking mode. However, only CHDLC and MPPP protocol
673 			 * support this setup */
674 
675 			if ((conf->config_id == WANCONFIG_CHDLC ||
676 			     conf->config_id == WANCONFIG_MPPP) &&
677 			    (nxt_card->wandev.config_id == WANCONFIG_CHDLC ||
678 			     nxt_card->wandev.config_id == WANCONFIG_MPPP)){
679 
680 				*irq = nxt_card->hw.irq;
681 				memcpy(&card->hw, &nxt_card->hw, sizeof(sdlahw_t));
682 
683 				/* The master could already be running, we must
684 				 * set this as a critical area */
685 				lock_adapter_irq(&nxt_card->wandev.lock, &smp_flags);
686 
687 				nxt_card->next = card;
688 				card->next = nxt_card;
689 
690 				card->wandev.piggyback = WANOPT_YES;
691 
692 				/* We must initialise the piggiback spin lock here
693 				 * since isr will try to lock card->next if it
694 				 * exists */
695 				spin_lock_init(&card->wandev.lock);
696 
697 				unlock_adapter_irq(&nxt_card->wandev.lock, &smp_flags);
698 				break;
699 			}else{
700 				/* Trying to run piggibacking with a wrong protocol */
701 				printk(KERN_INFO "%s: ERROR: Resource busy, ioport: 0x%x\n"
702 						 "%s:        This protocol doesn't support\n"
703 						 "%s:        multi-port operation!\n",
704 						 card->devname,nxt_card->hw.port,
705 						 card->devname,card->devname);
706 				return -EEXIST;
707 			}
708 		}
709 	}
710 
711 
712 	/* Make sure I/O port region is available only if we are the
713 	 * master device.  If we are running in piggibacking mode,
714 	 * we will use the resources of the master card */
715 	if (check_region(conf->ioport, SDLA_MAXIORANGE) &&
716 	    !card->wandev.piggyback) {
717 		printk(KERN_INFO
718 			"%s: I/O region 0x%X - 0x%X is in use!\n",
719 			card->wandev.name, conf->ioport,
720 			conf->ioport + SDLA_MAXIORANGE);
721 		return -EINVAL;
722 	}
723 
724 	return 0;
725 }
726 
727 /*==================================================================
728  * configure_s514_card
729  *
730  * For a S514 adapter, check for a possible configuration error in that
731  * we are loading an adapter in the same slot as a previously loaded S514
732  * card.
733  */
734 
735 
check_s514_conflicts(sdla_t * card,wandev_conf_t * conf,int * irq)736 static int check_s514_conflicts(sdla_t* card,wandev_conf_t* conf, int *irq)
737 {
738 	unsigned long smp_flags;
739 	int i;
740 
741 	if (test_bit(0,&card->configured))
742 		return 0;
743 
744 
745 	/* Check for already loaded card with the same IO port and IRQ
746 	 * If found, copy its hardware configuration and use its
747 	 * resources (i.e. piggybacking)
748 	 */
749 
750 	for (i = 0; i < ncards; i ++) {
751 
752 		sdla_t* nxt_card = &card_array[i];
753 		if(nxt_card == card)
754 			continue;
755 
756 		if((nxt_card->hw.type == SDLA_S514) &&
757 		   (nxt_card->hw.S514_slot_no == conf->PCI_slot_no) &&
758 		   (nxt_card->hw.S514_cpu_no[0] == conf->S514_CPU_no[0])&&
759 		   (nxt_card->next == NULL)){
760 
761 
762 			if ((conf->config_id == WANCONFIG_CHDLC ||
763 			     conf->config_id == WANCONFIG_MPPP) &&
764 			    (nxt_card->wandev.config_id == WANCONFIG_CHDLC ||
765 			     nxt_card->wandev.config_id == WANCONFIG_MPPP)){
766 
767 				*irq = nxt_card->hw.irq;
768 				memcpy(&card->hw, &nxt_card->hw, sizeof(sdlahw_t));
769 
770 				/* The master could already be running, we must
771 				 * set this as a critical area */
772 				lock_adapter_irq(&nxt_card->wandev.lock,&smp_flags);
773 				nxt_card->next = card;
774 				card->next = nxt_card;
775 
776 				card->wandev.piggyback = WANOPT_YES;
777 
778 				/* We must initialise the piggiback spin lock here
779 				 * since isr will try to lock card->next if it
780 				 * exists */
781 				spin_lock_init(&card->wandev.lock);
782 
783 				unlock_adapter_irq(&nxt_card->wandev.lock,&smp_flags);
784 
785 			}else{
786 				/* Trying to run piggibacking with a wrong protocol */
787 				printk(KERN_INFO "%s: ERROR: Resource busy: CPU %c PCISLOT %i\n"
788 						 "%s:        This protocol doesn't support\n"
789 						 "%s:        multi-port operation!\n",
790 						 card->devname,
791 						 conf->S514_CPU_no[0],conf->PCI_slot_no,
792 						 card->devname,card->devname);
793 				return -EEXIST;
794 			}
795 		}
796 	}
797 
798 	return 0;
799 }
800 
801 
802 
803 /*============================================================================
804  * Shut down WAN link driver.
805  * o shut down adapter hardware
806  * o release system resources.
807  *
808  * This function is called by the router when device is being unregistered or
809  * when it handles ROUTER_DOWN IOCTL.
810  */
shutdown(wan_device_t * wandev)811 static int shutdown (wan_device_t* wandev)
812 {
813 	sdla_t *card;
814 	int err=0;
815 
816 	/* sanity checks */
817 	if ((wandev == NULL) || (wandev->private == NULL)){
818 		return -EFAULT;
819 	}
820 
821 	if (wandev->state == WAN_UNCONFIGURED){
822 		return 0;
823 	}
824 
825 	card = wandev->private;
826 
827 	if (card->tty_opt){
828 		if (card->tty_open){
829 			printk(KERN_INFO
830 				"%s: Shutdown Failed: TTY is still open\n",
831 				  card->devname);
832 			return -EBUSY;
833 		}
834 	}
835 
836 	wandev->state = WAN_UNCONFIGURED;
837 
838 	set_bit(PERI_CRIT,(void*)&wandev->critical);
839 
840 	/* In case of piggibacking, make sure that
841          * we never try to shutdown both devices at the same
842          * time, because they depend on one another */
843 
844 	if (card->disable_comm){
845 		card->disable_comm(card);
846 	}
847 
848 	/* Release Resources */
849 	release_hw(card);
850 
851         /* only free the allocated I/O range if not an S514 adapter */
852 	if (wandev->hw_opt[0] != SDLA_S514 && !card->configured){
853               	release_region(card->hw.port, card->hw.io_range);
854 	}
855 
856 	if (!card->configured){
857 		memset(&card->hw, 0, sizeof(sdlahw_t));
858 	      	if (card->next){
859 			memset(&card->next->hw, 0, sizeof(sdlahw_t));
860 		}
861 	}
862 
863 
864 	clear_bit(PERI_CRIT,(void*)&wandev->critical);
865 	return err;
866 }
867 
release_hw(sdla_t * card)868 static void release_hw (sdla_t *card)
869 {
870 	sdla_t *nxt_card;
871 
872 
873 	/* Check if next device exists */
874 	if (card->next){
875 		nxt_card = card->next;
876 		/* If next device is down then release resources */
877 		if (nxt_card->wandev.state == WAN_UNCONFIGURED){
878 			if (card->wandev.piggyback){
879 				/* If this device is piggyback then use
880                                  * information of the master device
881 				 */
882 				printk(KERN_INFO "%s: Piggyback shutting down\n",card->devname);
883 				sdla_down(&card->next->hw);
884        				free_irq(card->wandev.irq, card->next);
885 				card->configured = 0;
886 				card->next->configured = 0;
887 				card->wandev.piggyback = 0;
888 			}else{
889 				/* Master device shutting down */
890 				printk(KERN_INFO "%s: Master shutting down\n",card->devname);
891 				sdla_down(&card->hw);
892 				free_irq(card->wandev.irq, card);
893 				card->configured = 0;
894 				card->next->configured = 0;
895 			}
896 		}else{
897 			printk(KERN_INFO "%s: Device still running %i\n",
898 				nxt_card->devname,nxt_card->wandev.state);
899 
900 			card->configured = 1;
901 		}
902 	}else{
903 		printk(KERN_INFO "%s: Master shutting down\n",card->devname);
904 		sdla_down(&card->hw);
905        		free_irq(card->wandev.irq, card);
906 		card->configured = 0;
907 	}
908 	return;
909 }
910 
911 
912 /*============================================================================
913  * Driver I/O control.
914  * o verify arguments
915  * o perform requested action
916  *
917  * This function is called when router handles one of the reserved user
918  * IOCTLs.  Note that 'arg' stil points to user address space.
919  */
ioctl(wan_device_t * wandev,unsigned cmd,unsigned long arg)920 static int ioctl (wan_device_t* wandev, unsigned cmd, unsigned long arg)
921 {
922 	sdla_t* card;
923 	int err;
924 
925 	/* sanity checks */
926 	if ((wandev == NULL) || (wandev->private == NULL))
927 		return -EFAULT;
928 	if (wandev->state == WAN_UNCONFIGURED)
929 		return -ENODEV;
930 
931 	card = wandev->private;
932 
933 	if(card->hw.type != SDLA_S514){
934 		disable_irq(card->hw.irq);
935 	}
936 
937 	if (test_bit(SEND_CRIT, (void*)&wandev->critical)) {
938 		return -EAGAIN;
939 	}
940 
941 	switch (cmd) {
942 	case WANPIPE_DUMP:
943 		err = ioctl_dump(wandev->private, (void*)arg);
944 		break;
945 
946 	case WANPIPE_EXEC:
947 		err = ioctl_exec(wandev->private, (void*)arg, cmd);
948 		break;
949 	default:
950 		err = -EINVAL;
951 	}
952 
953 	return err;
954 }
955 
956 /****** Driver IOCTL Handlers ***********************************************/
957 
958 /*============================================================================
959  * Dump adapter memory to user buffer.
960  * o verify request structure
961  * o copy request structure to kernel data space
962  * o verify length/offset
963  * o verify user buffer
964  * o copy adapter memory image to user buffer
965  *
966  * Note: when dumping memory, this routine switches curent dual-port memory
967  *	 vector, so care must be taken to avoid racing conditions.
968  */
ioctl_dump(sdla_t * card,sdla_dump_t * u_dump)969 static int ioctl_dump (sdla_t* card, sdla_dump_t* u_dump)
970 {
971 	sdla_dump_t dump;
972 	unsigned winsize;
973 	unsigned long oldvec;	/* DPM window vector */
974 	unsigned long smp_flags;
975 	int err = 0;
976 
977       #if defined(LINUX_2_1) || defined(LINUX_2_4)
978 	if(copy_from_user((void*)&dump, (void*)u_dump, sizeof(sdla_dump_t)))
979 		return -EFAULT;
980       #else
981         if ((u_dump == NULL) ||
982             verify_area(VERIFY_READ, u_dump, sizeof(sdla_dump_t)))
983                 return -EFAULT;
984         memcpy_fromfs((void*)&dump, (void*)u_dump, sizeof(sdla_dump_t));
985       #endif
986 
987 	if ((dump.magic != WANPIPE_MAGIC) ||
988 	    (dump.offset + dump.length > card->hw.memory))
989 		return -EINVAL;
990 
991       #ifdef LINUX_2_0
992         if ((dump.ptr == NULL) ||
993             verify_area(VERIFY_WRITE, dump.ptr, dump.length))
994                 return -EFAULT;
995       #endif
996 
997 	winsize = card->hw.dpmsize;
998 
999 	if(card->hw.type != SDLA_S514) {
1000 
1001 		lock_adapter_irq(&card->wandev.lock, &smp_flags);
1002 
1003                 oldvec = card->hw.vector;
1004                 while (dump.length) {
1005 			/* current offset */
1006                         unsigned pos = dump.offset % winsize;
1007 			/* current vector */
1008                         unsigned long vec = dump.offset - pos;
1009                         unsigned len = (dump.length > (winsize - pos)) ?
1010                         	(winsize - pos) : dump.length;
1011 			/* relocate window */
1012                         if (sdla_mapmem(&card->hw, vec) != 0) {
1013                                 err = -EIO;
1014                                 break;
1015                         }
1016 
1017                       #if defined(LINUX_2_1) || defined(LINUX_2_4)
1018                         if(copy_to_user((void *)dump.ptr,
1019                                 (u8 *)card->hw.dpmbase + pos, len)){
1020 
1021 				unlock_adapter_irq(&card->wandev.lock, &smp_flags);
1022 				return -EFAULT;
1023 			}
1024                       #else
1025 			memcpy_tofs((void*)(dump.ptr),
1026                         	(void*)(card->hw.dpmbase + pos), len);
1027                       #endif
1028                         dump.length     -= len;
1029                         dump.offset     += len;
1030                         dump.ptr         = (char*)dump.ptr + len;
1031                 }
1032 
1033                 sdla_mapmem(&card->hw, oldvec);/* restore DPM window position */
1034 		unlock_adapter_irq(&card->wandev.lock, &smp_flags);
1035 
1036 	}else {
1037 
1038 	     #if defined(LINUX_2_1) || defined(LINUX_2_4)
1039                if(copy_to_user((void *)dump.ptr,
1040 			       (u8 *)card->hw.dpmbase + dump.offset, dump.length)){
1041 			return -EFAULT;
1042 		}
1043              #else
1044                 memcpy_tofs((void*)(dump.ptr),
1045                 (void*)(card->hw.dpmbase + dump.offset), dump.length);
1046              #endif
1047 	}
1048 
1049 	return err;
1050 }
1051 
1052 /*============================================================================
1053  * Execute adapter firmware command.
1054  * o verify request structure
1055  * o copy request structure to kernel data space
1056  * o call protocol-specific 'exec' function
1057  */
ioctl_exec(sdla_t * card,sdla_exec_t * u_exec,int cmd)1058 static int ioctl_exec (sdla_t* card, sdla_exec_t* u_exec, int cmd)
1059 {
1060 	sdla_exec_t exec;
1061 	int err=0;
1062 
1063 	if (card->exec == NULL && cmd == WANPIPE_EXEC){
1064 		return -ENODEV;
1065 	}
1066 
1067       #if defined(LINUX_2_1) || defined(LINUX_2_4)
1068 	if(copy_from_user((void*)&exec, (void*)u_exec, sizeof(sdla_exec_t)))
1069 		return -EFAULT;
1070       #else
1071         if ((u_exec == NULL) ||
1072             verify_area(VERIFY_READ, u_exec, sizeof(sdla_exec_t)))
1073                 return -EFAULT;
1074         memcpy_fromfs((void*)&exec, (void*)u_exec, sizeof(sdla_exec_t));
1075       #endif
1076 
1077 	if ((exec.magic != WANPIPE_MAGIC) || (exec.cmd == NULL))
1078 		return -EINVAL;
1079 
1080 	switch (cmd) {
1081 		case WANPIPE_EXEC:
1082 			err = card->exec(card, exec.cmd, exec.data);
1083 			break;
1084 	}
1085 	return err;
1086 }
1087 
1088 /******* Miscellaneous ******************************************************/
1089 
1090 /*============================================================================
1091  * SDLA Interrupt Service Routine.
1092  * o acknowledge SDLA hardware interrupt.
1093  * o call protocol-specific interrupt service routine, if any.
1094  */
sdla_isr(int irq,void * dev_id,struct pt_regs * regs)1095 STATIC void sdla_isr (int irq, void* dev_id, struct pt_regs *regs)
1096 {
1097 #define	card	((sdla_t*)dev_id)
1098 
1099 	if(card->hw.type == SDLA_S514) {	/* handle interrrupt on S514 */
1100                 u32 int_status;
1101                 unsigned char CPU_no = card->hw.S514_cpu_no[0];
1102                 unsigned char card_found_for_IRQ;
1103 		u8 IRQ_count = 0;
1104 
1105 		for(;;) {
1106 
1107 			read_S514_int_stat(&card->hw, &int_status);
1108 
1109 			/* check if the interrupt is for this device */
1110  			if(!((unsigned char)int_status &
1111 				(IRQ_CPU_A | IRQ_CPU_B)))
1112                 	        return;
1113 
1114 			/* if the IRQ is for both CPUs on the same adapter, */
1115 			/* then alter the interrupt status so as to handle */
1116 			/* one CPU at a time */
1117 			if(((unsigned char)int_status & (IRQ_CPU_A | IRQ_CPU_B))
1118 				== (IRQ_CPU_A | IRQ_CPU_B)) {
1119 				int_status &= (CPU_no == S514_CPU_A) ?
1120 					~IRQ_CPU_B : ~IRQ_CPU_A;
1121 			}
1122 
1123 			card_found_for_IRQ = 0;
1124 
1125 	             	/* check to see that the CPU number for this device */
1126 			/* corresponds to the interrupt status read */
1127                 	switch (CPU_no) {
1128                         	case S514_CPU_A:
1129                                 	if((unsigned char)int_status &
1130 						IRQ_CPU_A)
1131                                         card_found_for_IRQ = 1;
1132                                 break;
1133 
1134 	                        case S514_CPU_B:
1135         	                        if((unsigned char)int_status &
1136 						IRQ_CPU_B)
1137                                         card_found_for_IRQ = 1;
1138                                 break;
1139                 	}
1140 
1141 			/* exit if the interrupt is for another CPU on the */
1142 			/* same IRQ */
1143 			if(!card_found_for_IRQ)
1144 				return;
1145 
1146        	 		if (!card ||
1147 			   (card->wandev.state == WAN_UNCONFIGURED && !card->configured)){
1148 					printk(KERN_INFO
1149 						"Received IRQ %d for CPU #%c\n",
1150 						irq, CPU_no);
1151 					printk(KERN_INFO
1152 						"IRQ for unconfigured adapter\n");
1153 					S514_intack(&card->hw, int_status);
1154 					return;
1155        			}
1156 
1157 	        	if (card->in_isr) {
1158         	       		printk(KERN_INFO
1159 					"%s: interrupt re-entrancy on IRQ %d\n",
1160                        			card->devname, card->wandev.irq);
1161 				S514_intack(&card->hw, int_status);
1162  				return;
1163        			}
1164 
1165 			spin_lock(&card->wandev.lock);
1166 			if (card->next){
1167 				spin_lock(&card->next->wandev.lock);
1168 			}
1169 
1170 	               	S514_intack(&card->hw, int_status);
1171        			if (card->isr)
1172 				card->isr(card);
1173 
1174 			if (card->next){
1175 				spin_unlock(&card->next->wandev.lock);
1176 			}
1177 			spin_unlock(&card->wandev.lock);
1178 
1179 			/* handle a maximum of two interrupts (one for each */
1180 			/* CPU on the adapter) before returning */
1181 			if((++ IRQ_count) == 2)
1182 				return;
1183 		}
1184 	}
1185 
1186 	else {			/* handle interrupt on S508 adapter */
1187 
1188 		if (!card || ((card->wandev.state == WAN_UNCONFIGURED) && !card->configured))
1189 			return;
1190 
1191 		if (card->in_isr) {
1192 			printk(KERN_INFO
1193 				"%s: interrupt re-entrancy on IRQ %d!\n",
1194 				card->devname, card->wandev.irq);
1195 			return;
1196 		}
1197 
1198 		spin_lock(&card->wandev.lock);
1199 		if (card->next){
1200 			spin_lock(&card->next->wandev.lock);
1201 		}
1202 
1203 		sdla_intack(&card->hw);
1204 		if (card->isr)
1205 			card->isr(card);
1206 
1207 		if (card->next){
1208 			spin_unlock(&card->next->wandev.lock);
1209 		}
1210 		spin_unlock(&card->wandev.lock);
1211 
1212 	}
1213 
1214 #undef	card
1215 }
1216 
1217 /*============================================================================
1218  * This routine is called by the protocol-specific modules when network
1219  * interface is being open.  The only reason we need this, is because we
1220  * have to call MOD_INC_USE_COUNT, but cannot include 'module.h' where it's
1221  * defined more than once into the same kernel module.
1222  */
wanpipe_open(sdla_t * card)1223 void wanpipe_open (sdla_t* card)
1224 {
1225 	++card->open_cnt;
1226 	MOD_INC_USE_COUNT;
1227 }
1228 
1229 /*============================================================================
1230  * This routine is called by the protocol-specific modules when network
1231  * interface is being closed.  The only reason we need this, is because we
1232  * have to call MOD_DEC_USE_COUNT, but cannot include 'module.h' where it's
1233  * defined more than once into the same kernel module.
1234  */
wanpipe_close(sdla_t * card)1235 void wanpipe_close (sdla_t* card)
1236 {
1237 	--card->open_cnt;
1238 	MOD_DEC_USE_COUNT;
1239 }
1240 
1241 /*============================================================================
1242  * Set WAN device state.
1243  */
wanpipe_set_state(sdla_t * card,int state)1244 void wanpipe_set_state (sdla_t* card, int state)
1245 {
1246 	if (card->wandev.state != state) {
1247 		switch (state) {
1248 		case WAN_CONNECTED:
1249 			printk (KERN_INFO "%s: link connected!\n",
1250 				card->devname);
1251 			break;
1252 
1253 		case WAN_CONNECTING:
1254 			printk (KERN_INFO "%s: link connecting...\n",
1255 				card->devname);
1256 			break;
1257 
1258 		case WAN_DISCONNECTED:
1259 			printk (KERN_INFO "%s: link disconnected!\n",
1260 				card->devname);
1261 			break;
1262 		}
1263 		card->wandev.state = state;
1264 	}
1265 	card->state_tick = jiffies;
1266 }
1267 
wanpipe_find_card(char * name)1268 sdla_t * wanpipe_find_card (char *name)
1269 {
1270 	int cnt;
1271 	for (cnt = 0; cnt < ncards; ++ cnt) {
1272 		sdla_t* card = &card_array[cnt];
1273 		if (!strcmp(card->devname,name))
1274 			return card;
1275 	}
1276 	return NULL;
1277 }
wanpipe_find_card_num(int num)1278 sdla_t * wanpipe_find_card_num (int num)
1279 {
1280 	if (num < 1 || num > ncards)
1281 		return NULL;
1282 	num--;
1283 	return &card_array[num];
1284 }
1285 
1286 
run_wanpipe_tq(unsigned long data)1287 static void run_wanpipe_tq (unsigned long data)
1288 {
1289 	task_queue *tq_queue = (task_queue *)data;
1290 	if (test_and_set_bit(2,(void*)&wanpipe_bh_critical))
1291 		printk(KERN_INFO "CRITICAL IN RUNNING TASK QUEUE\n");
1292 	run_task_queue (tq_queue);
1293 	clear_bit(2,(void*)&wanpipe_bh_critical);
1294 
1295 }
1296 
wanpipe_queue_tq(struct tq_struct * bh_pointer)1297 void wanpipe_queue_tq (struct tq_struct *bh_pointer)
1298 {
1299 	if (test_and_set_bit(1,(void*)&wanpipe_bh_critical))
1300 		printk(KERN_INFO "CRITICAL IN QUEUING TASK\n");
1301 
1302 	queue_task(bh_pointer,&wanpipe_tq_custom);
1303 	clear_bit(1,(void*)&wanpipe_bh_critical);
1304 }
1305 
wanpipe_mark_bh(void)1306 void wanpipe_mark_bh (void)
1307 {
1308 	if (!test_and_set_bit(0,(void*)&wanpipe_bh_critical)){
1309 		queue_task(&wanpipe_tq_task,&tq_immediate);
1310 		mark_bh(IMMEDIATE_BH);
1311 		clear_bit(0,(void*)&wanpipe_bh_critical);
1312 	}
1313 }
1314 
wakeup_sk_bh(netdevice_t * dev)1315 void wakeup_sk_bh (netdevice_t *dev)
1316 {
1317 	wanpipe_common_t *chan = dev->priv;
1318 
1319 	if (test_bit(0,&chan->common_critical))
1320 		return;
1321 
1322 	if (chan->sk && chan->tx_timer){
1323 		chan->tx_timer->expires=jiffies+1;
1324 		add_timer(chan->tx_timer);
1325 	}
1326 }
1327 
change_dev_flags(netdevice_t * dev,unsigned flags)1328 int change_dev_flags (netdevice_t *dev, unsigned flags)
1329 {
1330 	struct ifreq if_info;
1331 	mm_segment_t fs = get_fs();
1332 	int err;
1333 
1334 	memset(&if_info, 0, sizeof(if_info));
1335 	strcpy(if_info.ifr_name, dev->name);
1336 	if_info.ifr_flags = flags;
1337 
1338 	set_fs(get_ds());     /* get user space block */
1339 	err = devinet_ioctl(SIOCSIFFLAGS, &if_info);
1340 	set_fs(fs);
1341 
1342 	return err;
1343 }
1344 
get_ip_address(netdevice_t * dev,int option)1345 unsigned long get_ip_address (netdevice_t *dev, int option)
1346 {
1347 
1348       #ifdef LINUX_2_4
1349 	struct in_ifaddr *ifaddr;
1350 	struct in_device *in_dev;
1351 
1352 	if ((in_dev = __in_dev_get(dev)) == NULL){
1353 		return 0;
1354 	}
1355       #elif defined(LINUX_2_1)
1356 	struct in_ifaddr *ifaddr;
1357 	struct in_device *in_dev;
1358 
1359 	if ((in_dev = dev->ip_ptr) == NULL){
1360 		return 0;
1361 	}
1362       #endif
1363 
1364       #if defined(LINUX_2_1) || defined(LINUX_2_4)
1365 	if ((ifaddr = in_dev->ifa_list)== NULL ){
1366 		return 0;
1367 	}
1368       #endif
1369 
1370 	switch (option){
1371 
1372 	case WAN_LOCAL_IP:
1373 	      #ifdef LINUX_2_0
1374 		return dev->pa_addr;
1375 	      #else
1376 		return ifaddr->ifa_local;
1377 	      #endif
1378 		break;
1379 
1380 	case WAN_POINTOPOINT_IP:
1381 	      #ifdef LINUX_2_0
1382 		return dev->pa_dstaddr;
1383 	      #else
1384 		return ifaddr->ifa_address;
1385 	      #endif
1386 		break;
1387 
1388 	case WAN_NETMASK_IP:
1389 	      #ifdef LINUX_2_0
1390 		return dev->pa_mask;
1391 	      #else
1392 		return ifaddr->ifa_mask;
1393 	      #endif
1394 		break;
1395 
1396 	case WAN_BROADCAST_IP:
1397 	      #ifdef LINUX_2_0
1398 		return dev->pa_brdaddr;
1399 	      #else
1400 		return ifaddr->ifa_broadcast;
1401 	      #endif
1402 		break;
1403 	default:
1404 		return 0;
1405 	}
1406 
1407 	return 0;
1408 }
1409 
add_gateway(sdla_t * card,netdevice_t * dev)1410 void add_gateway(sdla_t *card, netdevice_t *dev)
1411 {
1412 	mm_segment_t oldfs;
1413 	struct rtentry route;
1414 	int res;
1415 
1416 	memset((char*)&route,0,sizeof(struct rtentry));
1417 
1418 	((struct sockaddr_in *)
1419 		&(route.rt_dst))->sin_addr.s_addr = 0;
1420 	((struct sockaddr_in *)
1421 		&(route.rt_dst))->sin_family = AF_INET;
1422 
1423 	((struct sockaddr_in *)
1424 		&(route.rt_genmask))->sin_addr.s_addr = 0;
1425 	((struct sockaddr_in *)
1426 		&(route.rt_genmask)) ->sin_family = AF_INET;
1427 
1428 
1429 	route.rt_flags = 0;
1430 	route.rt_dev = dev->name;
1431 
1432 	oldfs = get_fs();
1433 	set_fs(get_ds());
1434       #if defined(LINUX_2_1) || defined(LINUX_2_4)
1435 	res = ip_rt_ioctl(SIOCADDRT,&route);
1436       #else
1437 	res = ip_rt_new(&route);
1438       #endif
1439 	set_fs(oldfs);
1440 
1441 	if (res == 0){
1442 		printk(KERN_INFO "%s: Gateway added for %s\n",
1443 			card->devname,dev->name);
1444 	}
1445 
1446 	return;
1447 }
1448 
1449 MODULE_LICENSE("GPL");
1450 
1451 /****** End *********************************************************/
1452