1 /*****************************************************************************
2 * sdla_chdlc.c	WANPIPE(tm) Multiprotocol WAN Link Driver. Cisco HDLC module.
3 *
4 * Authors: 	Nenad Corbic <ncorbic@sangoma.com>
5 *		Gideon Hack
6 *
7 * Copyright:	(c) 1995-2001 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 * Feb 28, 2001  Nenad Corbic	Updated if_tx_timeout() routine for
15 * 				2.4.X kernels.
16 * Jan 25, 2001  Nenad Corbic	Added a TTY Sync serial driver over the
17 * 				HDLC streaming protocol
18 * 				Added a TTY Async serial driver over the
19 * 				Async protocol.
20 * Dec 15, 2000  Nenad Corbic    Updated for 2.4.X Kernel support
21 * Nov 13, 2000  Nenad Corbic    Added true interface type encoding option.
22 * 				Tcpdump doesn't support CHDLC inteface
23 * 				types, to fix this "true type" option will set
24 * 				the interface type to RAW IP mode.
25 * Nov 07, 2000  Nenad Corbic	Added security features for UDP debugging:
26 *                               Deny all and specify allowed requests.
27 * Jun 20, 2000  Nenad Corbic	Fixed the API IP ERROR bug. Caused by the
28 *                               latest update.
29 * May 09, 2000	Nenad Corbic	Option to bring down an interface
30 *                               upon disconnect.
31 * Mar 23, 2000  Nenad Corbic	Improved task queue, bh handling.
32 * Mar 16, 2000	Nenad Corbic	Fixed the SLARP Dynamic IP addressing.
33 * Mar 06, 2000  Nenad Corbic	Bug Fix: corrupted mbox recovery.
34 * Feb 10, 2000  Gideon Hack     Added ASYNC support.
35 * Feb 09, 2000  Nenad Corbic    Fixed two shutdown bugs in update() and
36 *                               if_stats() functions.
37 * Jan 24, 2000  Nenad Corbic    Fixed a startup wanpipe state racing,
38 *                               condition between if_open and isr.
39 * Jan 10, 2000  Nenad Corbic    Added new socket API support.
40 * Dev 15, 1999  Nenad Corbic    Fixed up header files for 2.0.X kernels
41 * Nov 20, 1999  Nenad Corbic 	Fixed zero length API bug.
42 * Sep 30, 1999  Nenad Corbic    Fixed dynamic IP and route setup.
43 * Sep 23, 1999  Nenad Corbic    Added SMP support, fixed tracing
44 * Sep 13, 1999  Nenad Corbic	Split up Port 0 and 1 into separate devices.
45 * Jun 02, 1999  Gideon Hack     Added support for the S514 adapter.
46 * Oct 30, 1998	Jaspreet Singh	Added Support for CHDLC API (HDLC STREAMING).
47 * Oct 28, 1998	Jaspreet Singh	Added Support for Dual Port CHDLC.
48 * Aug 07, 1998	David Fong	Initial version.
49 *****************************************************************************/
50 
51 #include <linux/module.h>
52 #include <linux/version.h>
53 #include <linux/kernel.h>	/* printk(), and other useful stuff */
54 #include <linux/stddef.h>	/* offsetof(), etc. */
55 #include <linux/errno.h>	/* return codes */
56 #include <linux/string.h>	/* inline memset(), etc. */
57 #include <linux/slab.h>	/* kmalloc(), kfree() */
58 #include <linux/wanrouter.h>	/* WAN router definitions */
59 #include <linux/wanpipe.h>	/* WANPIPE common user API definitions */
60 #include <linux/if_arp.h>	/* ARPHRD_* defines */
61 
62 
63 #if defined(LINUX_2_1) || defined(LINUX_2_4)
64  #include <asm/uaccess.h>
65  #include <linux/inetdevice.h>
66  #include <linux/netdevice.h>
67 #else
68  #include <asm/segment.h>
69  #include <net/route.h>          /* Adding new route entries : 2.0.X kernels */
70 #endif
71 
72 #include <linux/in.h>		/* sockaddr_in */
73 #include <linux/inet.h>
74 #include <linux/if.h>
75 #include <asm/byteorder.h>	/* htons(), etc. */
76 #include <linux/sdlapci.h>
77 #include <asm/io.h>
78 
79 #include <linux/sdla_chdlc.h>		/* CHDLC firmware API definitions */
80 #include <linux/sdla_asy.h>           	/* CHDLC (async) API definitions */
81 
82 #include <linux/if_wanpipe_common.h>    /* Socket Driver common area */
83 #include <linux/if_wanpipe.h>
84 
85 /* TTY Includes */
86 #include <linux/tty.h>
87 #include <linux/tty_flip.h>
88 #include <linux/serial.h>
89 
90 
91 /****** Defines & Macros ****************************************************/
92 
93 /* reasons for enabling the timer interrupt on the adapter */
94 #define TMR_INT_ENABLED_UDP   		0x01
95 #define TMR_INT_ENABLED_UPDATE		0x02
96 #define TMR_INT_ENABLED_CONFIG		0x10
97 
98 #define MAX_IP_ERRORS	10
99 
100 #define TTY_CHDLC_MAX_MTU	2000
101 #define	CHDLC_DFLT_DATA_LEN	1500		/* default MTU */
102 #define CHDLC_HDR_LEN		1
103 
104 #define CHDLC_API 0x01
105 
106 #define PORT(x)   (x == 0 ? "PRIMARY" : "SECONDARY" )
107 #define MAX_BH_BUFF	10
108 
109 //#define PRINT_DEBUG
110 #ifdef PRINT_DEBUG
111 #define dbg_printk(format, a...) printk(format, ## a)
112 #else
113 #define dbg_printk(format, a...)
114 #endif
115 
116 /******Data Structures*****************************************************/
117 
118 /* This structure is placed in the private data area of the device structure.
119  * The card structure used to occupy the private area but now the following
120  * structure will incorporate the card structure along with CHDLC specific data
121  */
122 
123 typedef struct chdlc_private_area
124 {
125 	wanpipe_common_t common;
126 	sdla_t		*card;
127 	int 		TracingEnabled;		/* For enabling Tracing */
128 	unsigned long 	curr_trace_addr;	/* Used for Tracing */
129 	unsigned long 	start_trace_addr;
130 	unsigned long 	end_trace_addr;
131 	unsigned long 	base_addr_trace_buffer;
132 	unsigned long 	end_addr_trace_buffer;
133 	unsigned short 	number_trace_elements;
134 	unsigned  	available_buffer_space;
135 	unsigned long 	router_start_time;
136 	unsigned char 	route_status;
137 	unsigned char 	route_removed;
138 	unsigned long 	tick_counter;		/* For 5s timeout counter */
139 	unsigned long 	router_up_time;
140         u32             IP_address;		/* IP addressing */
141         u32             IP_netmask;
142 	u32		ip_local;
143 	u32		ip_remote;
144 	u32 		ip_local_tmp;
145 	u32		ip_remote_tmp;
146 	u8		ip_error;
147 	u8		config_chdlc;
148 	u8 		config_chdlc_timeout;
149 	unsigned char  mc;			/* Mulitcast support on/off */
150 	unsigned short udp_pkt_lgth;		/* udp packet processing */
151 	char udp_pkt_src;
152 	char udp_pkt_data[MAX_LGTH_UDP_MGNT_PKT];
153 	unsigned short timer_int_enabled;
154 	char update_comms_stats;		/* updating comms stats */
155 
156 #if defined(LINUX_2_1) || defined(LINUX_2_4)
157 	bh_data_t *bh_head;	  	  /* Circular buffer for chdlc_bh */
158 	unsigned long  tq_working;
159 	volatile int  bh_write;
160 	volatile int  bh_read;
161 	atomic_t  bh_buff_used;
162 #endif
163 
164 	unsigned char interface_down;
165 
166 	/* Polling task queue. Each interface
167          * has its own task queue, which is used
168          * to defer events from the interrupt */
169 	struct tq_struct poll_task;
170 	struct timer_list poll_delay_timer;
171 
172 	u8 gateway;
173 	u8 true_if_encoding;
174 	//FIXME: add driver stats as per frame relay!
175 
176 } chdlc_private_area_t;
177 
178 /* Route Status options */
179 #define NO_ROUTE	0x00
180 #define ADD_ROUTE	0x01
181 #define ROUTE_ADDED	0x02
182 #define REMOVE_ROUTE	0x03
183 
184 
185 /* variable for keeping track of enabling/disabling FT1 monitor status */
186 static int rCount = 0;
187 
188 /* variable for tracking how many interfaces to open for WANPIPE on the
189    two ports */
190 
191 extern void disable_irq(unsigned int);
192 extern void enable_irq(unsigned int);
193 
194 /****** Function Prototypes *************************************************/
195 /* WAN link driver entry points. These are called by the WAN router module. */
196 static int update (wan_device_t* wandev);
197 static int new_if (wan_device_t* wandev, netdevice_t* dev,
198 	wanif_conf_t* conf);
199 
200 /* Network device interface */
201 static int if_init   (netdevice_t* dev);
202 static int if_open   (netdevice_t* dev);
203 static int if_close  (netdevice_t* dev);
204 static int if_header (struct sk_buff* skb, netdevice_t* dev,
205 	unsigned short type, void* daddr, void* saddr, unsigned len);
206 
207 #if defined(LINUX_2_1) || defined(LINUX_2_4)
208   static int if_rebuild_hdr (struct sk_buff *skb);
209   static struct net_device_stats* if_stats (netdevice_t* dev);
210 
211 #else
212   static int if_rebuild_hdr (void* hdr, netdevice_t* dev, unsigned long raddr,
213         struct sk_buff* skb);
214   static struct enet_statistics* if_stats (netdevice_t* dev);
215 #endif
216 
217 static int if_send (struct sk_buff* skb, netdevice_t* dev);
218 
219 /* CHDLC Firmware interface functions */
220 static int chdlc_configure 	(sdla_t* card, void* data);
221 static int chdlc_comm_enable 	(sdla_t* card);
222 static int chdlc_read_version 	(sdla_t* card, char* str);
223 static int chdlc_set_intr_mode 	(sdla_t* card, unsigned mode);
224 static int chdlc_send (sdla_t* card, void* data, unsigned len);
225 static int chdlc_read_comm_err_stats (sdla_t* card);
226 static int chdlc_read_op_stats (sdla_t* card);
227 static int chdlc_error (sdla_t *card, int err, CHDLC_MAILBOX_STRUCT *mb);
228 
229 
230 static int chdlc_disable_comm_shutdown (sdla_t *card);
231 #ifdef LINUX_2_4
232   static void if_tx_timeout (netdevice_t *dev);
233 #endif
234 
235 /* Miscellaneous CHDLC Functions */
236 static int set_chdlc_config (sdla_t* card);
237 static void init_chdlc_tx_rx_buff( sdla_t* card);
238 static int process_chdlc_exception(sdla_t *card);
239 static int process_global_exception(sdla_t *card);
240 static int update_comms_stats(sdla_t* card,
241         chdlc_private_area_t* chdlc_priv_area);
242 static int configure_ip (sdla_t* card);
243 static int unconfigure_ip (sdla_t* card);
244 static void process_route(sdla_t *card);
245 static void port_set_state (sdla_t *card, int);
246 static int config_chdlc (sdla_t *card);
247 static void disable_comm (sdla_t *card);
248 
249 static void trigger_chdlc_poll (netdevice_t *);
250 static void chdlc_poll (netdevice_t *);
251 static void chdlc_poll_delay (unsigned long dev_ptr);
252 
253 
254 /* Miscellaneous asynchronous interface Functions */
255 static int set_asy_config (sdla_t* card);
256 static int asy_comm_enable (sdla_t* card);
257 
258 /* Interrupt handlers */
259 static void wpc_isr (sdla_t* card);
260 static void rx_intr (sdla_t* card);
261 static void timer_intr(sdla_t *);
262 
263 #if defined(LINUX_2_1) || defined(LINUX_2_4)
264   /* Bottom half handlers */
265   static void chdlc_bh (netdevice_t *);
266   static int chdlc_bh_cleanup (netdevice_t *);
267   static int bh_enqueue (netdevice_t *, struct sk_buff *);
268 #endif
269 
270 /* Miscellaneous functions */
271 static int chk_bcast_mcast_addr(sdla_t* card, netdevice_t* dev,
272 				struct sk_buff *skb);
273 static int reply_udp( unsigned char *data, unsigned int mbox_len );
274 static int intr_test( sdla_t* card);
275 static int udp_pkt_type( struct sk_buff *skb , sdla_t* card);
276 static int store_udp_mgmt_pkt(char udp_pkt_src, sdla_t* card,
277                                 struct sk_buff *skb, netdevice_t* dev,
278                                 chdlc_private_area_t* chdlc_priv_area);
279 static int process_udp_mgmt_pkt(sdla_t* card, netdevice_t* dev,
280 				chdlc_private_area_t* chdlc_priv_area);
281 static unsigned short calc_checksum (char *, int);
282 static void s508_lock (sdla_t *card, unsigned long *smp_flags);
283 static void s508_unlock (sdla_t *card, unsigned long *smp_flags);
284 
285 
286 static int  Intr_test_counter;
287 
288 /* TTY Global Definitions */
289 
290 #if defined(LINUX_2_4) || defined(LINUX_2_1)
291 
292 #define NR_PORTS 4
293 #define WAN_TTY_MAJOR 226
294 #define WAN_TTY_MINOR 0
295 
296 #define WAN_CARD(port) (tty_card_map[port])
297 #define MIN_PORT 0
298 #define MAX_PORT NR_PORTS-1
299 
300 #define CRC_LENGTH 2
301 
302 static int wanpipe_tty_init(sdla_t *card);
303 static void wanpipe_tty_receive(sdla_t *, unsigned, unsigned int);
304 static void wanpipe_tty_trigger_poll(sdla_t *card);
305 
306 static struct tty_driver serial_driver, callout_driver;
307 static int serial_refcount=1;
308 static int tty_init_cnt=0;
309 
310 static struct serial_state rs_table[NR_PORTS];
311 static struct tty_struct *serial_table[NR_PORTS];
312 static struct termios *serial_termios[NR_PORTS];
313 static struct termios *serial_termios_locked[NR_PORTS];
314 
315 static char tty_driver_mode=WANOPT_TTY_SYNC;
316 
317 static char *opt_decode[] = {"NONE","CRTSCTS","XONXOFF-RX",
318 	  	             "CRTSCTS XONXOFF-RX","XONXOFF-TX",
319 		             "CRTSCTS XONXOFF-TX","CRTSCTS XONXOFF"};
320 static char *p_decode[] = {"NONE","ODD","EVEN"};
321 
322 static void* tty_card_map[NR_PORTS] = {NULL,NULL,NULL,NULL};
323 
324 #endif
325 
326 
327 /****** Public Functions ****************************************************/
328 
329 /*============================================================================
330  * Cisco HDLC protocol initialization routine.
331  *
332  * This routine is called by the main WANPIPE module during setup.  At this
333  * point adapter is completely initialized and firmware is running.
334  *  o read firmware version (to make sure it's alive)
335  *  o configure adapter
336  *  o initialize protocol-specific fields of the adapter data space.
337  *
338  * Return:	0	o.k.
339  *		< 0	failure.
340  */
wpc_init(sdla_t * card,wandev_conf_t * conf)341 int wpc_init (sdla_t* card, wandev_conf_t* conf)
342 {
343 	unsigned char port_num;
344 	int err;
345 	unsigned long max_permitted_baud = 0;
346 	SHARED_MEMORY_INFO_STRUCT *flags;
347 
348 	union
349 		{
350 		char str[80];
351 		} u;
352 	volatile CHDLC_MAILBOX_STRUCT* mb;
353 	CHDLC_MAILBOX_STRUCT* mb1;
354 	unsigned long timeout;
355 
356 	/* Verify configuration ID */
357 	if (conf->config_id != WANCONFIG_CHDLC) {
358 		printk(KERN_INFO "%s: invalid configuration ID %u!\n",
359 				  card->devname, conf->config_id);
360 		return -EINVAL;
361 	}
362 
363 	/* Find out which Port to use */
364 	if ((conf->comm_port == WANOPT_PRI) || (conf->comm_port == WANOPT_SEC)){
365 		if (card->next){
366 
367 			if (conf->comm_port != card->next->u.c.comm_port){
368 				card->u.c.comm_port = conf->comm_port;
369 			}else{
370 				printk(KERN_INFO "%s: ERROR - %s port used!\n",
371         		        	card->wandev.name, PORT(conf->comm_port));
372 				return -EINVAL;
373 			}
374 		}else{
375 			card->u.c.comm_port = conf->comm_port;
376 		}
377 	}else{
378 		printk(KERN_INFO "%s: ERROR - Invalid Port Selected!\n",
379                 			card->wandev.name);
380 		return -EINVAL;
381 	}
382 
383 
384 	/* Initialize protocol-specific fields */
385 	if(card->hw.type != SDLA_S514){
386 
387 		if (card->u.c.comm_port == WANOPT_PRI){
388 			card->mbox  = (void *) card->hw.dpmbase;
389 		}else{
390 			card->mbox  = (void *) card->hw.dpmbase +
391 				SEC_BASE_ADDR_MB_STRUCT - PRI_BASE_ADDR_MB_STRUCT;
392 		}
393 	}else{
394 		/* for a S514 adapter, set a pointer to the actual mailbox in the */
395 		/* allocated virtual memory area */
396 		if (card->u.c.comm_port == WANOPT_PRI){
397 			card->mbox = (void *) card->hw.dpmbase + PRI_BASE_ADDR_MB_STRUCT;
398 		}else{
399 			card->mbox = (void *) card->hw.dpmbase + SEC_BASE_ADDR_MB_STRUCT;
400 		}
401 	}
402 
403 	mb = mb1 = card->mbox;
404 
405 	if (!card->configured){
406 
407 		/* The board will place an 'I' in the return code to indicate that it is
408 	   	ready to accept commands.  We expect this to be completed in less
409            	than 1 second. */
410 
411 		timeout = jiffies;
412 		while (mb->return_code != 'I')	/* Wait 1s for board to initialize */
413 			if ((jiffies - timeout) > 1*HZ) break;
414 
415 		if (mb->return_code != 'I') {
416 			printk(KERN_INFO
417 				"%s: Initialization not completed by adapter\n",
418 				card->devname);
419 			printk(KERN_INFO "Please contact Sangoma representative.\n");
420 			return -EIO;
421 		}
422 	}
423 
424 	/* Read firmware version.  Note that when adapter initializes, it
425 	 * clears the mailbox, so it may appear that the first command was
426 	 * executed successfully when in fact it was merely erased. To work
427 	 * around this, we execute the first command twice.
428 	 */
429 
430 	if (chdlc_read_version(card, u.str))
431 		return -EIO;
432 
433 	printk(KERN_INFO "%s: Running Cisco HDLC firmware v%s\n",
434 		card->devname, u.str);
435 
436 	card->isr			= &wpc_isr;
437 	card->poll			= NULL;
438 	card->exec			= NULL;
439 	card->wandev.update		= &update;
440  	card->wandev.new_if		= &new_if;
441 	card->wandev.del_if		= NULL;
442 	card->wandev.udp_port   	= conf->udp_port;
443 	card->disable_comm		= &disable_comm;
444 	card->wandev.new_if_cnt = 0;
445 
446 	/* reset the number of times the 'update()' proc has been called */
447 	card->u.c.update_call_count = 0;
448 
449 	card->wandev.ttl = conf->ttl;
450 	card->wandev.interface = conf->interface;
451 
452 	if ((card->u.c.comm_port == WANOPT_SEC && conf->interface == WANOPT_V35)&&
453 	    card->hw.type != SDLA_S514){
454 		printk(KERN_INFO "%s: ERROR - V35 Interface not supported on S508 %s port \n",
455 			card->devname, PORT(card->u.c.comm_port));
456 		return -EIO;
457 	}
458 
459 	card->wandev.clocking = conf->clocking;
460 
461 	port_num = card->u.c.comm_port;
462 
463 	/* in API mode, we can configure for "receive only" buffering */
464 	if(card->hw.type == SDLA_S514) {
465 		card->u.c.receive_only = conf->receive_only;
466 		if(conf->receive_only) {
467 			printk(KERN_INFO
468 				"%s: Configured for 'receive only' mode\n",
469                                 card->devname);
470 		}
471 	}
472 
473 	/* Setup Port Bps */
474 
475 	if(card->wandev.clocking) {
476 		if((port_num == WANOPT_PRI) || card->u.c.receive_only) {
477 			/* For Primary Port 0 */
478                		max_permitted_baud =
479 				(card->hw.type == SDLA_S514) ?
480 				PRI_MAX_BAUD_RATE_S514 :
481 				PRI_MAX_BAUD_RATE_S508;
482 
483 		}else if(port_num == WANOPT_SEC) {
484 			/* For Secondary Port 1 */
485                         max_permitted_baud =
486                                (card->hw.type == SDLA_S514) ?
487                                 SEC_MAX_BAUD_RATE_S514 :
488                                 SEC_MAX_BAUD_RATE_S508;
489                         }
490 
491 			if(conf->bps > max_permitted_baud) {
492 				conf->bps = max_permitted_baud;
493 				printk(KERN_INFO "%s: Baud too high!\n",
494 					card->wandev.name);
495  				printk(KERN_INFO "%s: Baud rate set to %lu bps\n",
496 					card->wandev.name, max_permitted_baud);
497 			}
498 			card->wandev.bps = conf->bps;
499 	}else{
500         	card->wandev.bps = 0;
501   	}
502 
503 	/* Setup the Port MTU */
504 	if((port_num == WANOPT_PRI) || card->u.c.receive_only) {
505 
506 		/* For Primary Port 0 */
507 		card->wandev.mtu =
508 			(conf->mtu >= MIN_LGTH_CHDLC_DATA_CFG) ?
509 			min_t(unsigned int, conf->mtu, PRI_MAX_NO_DATA_BYTES_IN_FRAME) :
510 			CHDLC_DFLT_DATA_LEN;
511 	} else if(port_num == WANOPT_SEC) {
512 		/* For Secondary Port 1 */
513 		card->wandev.mtu =
514 			(conf->mtu >= MIN_LGTH_CHDLC_DATA_CFG) ?
515 			min_t(unsigned int, conf->mtu, SEC_MAX_NO_DATA_BYTES_IN_FRAME) :
516 			CHDLC_DFLT_DATA_LEN;
517 	}
518 
519 	/* Set up the interrupt status area */
520 	/* Read the CHDLC Configuration and obtain:
521 	 *	Ptr to shared memory infor struct
522          * Use this pointer to calculate the value of card->u.c.flags !
523  	 */
524 	mb1->buffer_length = 0;
525 	mb1->command = READ_CHDLC_CONFIGURATION;
526 	err = sdla_exec(mb1) ? mb1->return_code : CMD_TIMEOUT;
527 	if(err != COMMAND_OK) {
528                 if(card->hw.type != SDLA_S514)
529                 	enable_irq(card->hw.irq);
530 
531 		chdlc_error(card, err, mb1);
532 		return -EIO;
533 	}
534 
535 	if(card->hw.type == SDLA_S514){
536                	card->u.c.flags = (void *)(card->hw.dpmbase +
537                		(((CHDLC_CONFIGURATION_STRUCT *)mb1->data)->
538 			ptr_shared_mem_info_struct));
539         }else{
540                 card->u.c.flags = (void *)(card->hw.dpmbase +
541                         (((CHDLC_CONFIGURATION_STRUCT *)mb1->data)->
542 			ptr_shared_mem_info_struct % SDLA_WINDOWSIZE));
543 	}
544 
545 	flags = card->u.c.flags;
546 
547 	/* This is for the ports link state */
548 	card->wandev.state = WAN_DUALPORT;
549 	card->u.c.state = WAN_DISCONNECTED;
550 
551 
552 	if (!card->wandev.piggyback){
553 		int err;
554 
555 		/* Perform interrupt testing */
556 		err = intr_test(card);
557 
558 		if(err || (Intr_test_counter < MAX_INTR_TEST_COUNTER)) {
559 			printk(KERN_INFO "%s: Interrupt test failed (%i)\n",
560 					card->devname, Intr_test_counter);
561 			printk(KERN_INFO "%s: Please choose another interrupt\n",
562 					card->devname);
563 			return -EIO;
564 		}
565 
566 		printk(KERN_INFO "%s: Interrupt test passed (%i)\n",
567 				card->devname, Intr_test_counter);
568 		card->configured = 1;
569 	}
570 
571 	if ((card->tty_opt=conf->tty) == WANOPT_YES){
572 #if defined(LINUX_2_4) || defined(LINUX_2_1)
573 		int err;
574 		card->tty_minor = conf->tty_minor;
575 
576 		/* On ASYNC connections internal clocking
577 		 * is mandatory */
578 		if ((card->u.c.async_mode = conf->tty_mode)){
579 			card->wandev.clocking = 1;
580 		}
581 		err=wanpipe_tty_init(card);
582 		if (err){
583 			return err;
584 		}
585 #else
586 		printk(KERN_INFO "%s: Error: TTY driver is not supported on 2.0.X kernels!\n",
587 				card->devname);
588 		return -EINVAL;
589 #endif
590 	}else{
591 
592 
593 		if (chdlc_set_intr_mode(card, APP_INT_ON_TIMER)){
594 			printk (KERN_INFO "%s: Failed to set interrupt triggers!\n",
595 				card->devname);
596 			return -EIO;
597         	}
598 
599 		/* Mask the Timer interrupt */
600 		flags->interrupt_info_struct.interrupt_permission &=
601 			~APP_INT_ON_TIMER;
602 	}
603 
604 	/* If we are using CHDLC in backup mode, this flag will
605 	 * indicate not to look for IP addresses in config_chdlc()*/
606 	card->u.c.backup = conf->backup;
607 
608 	printk(KERN_INFO "\n");
609 
610 	return 0;
611 }
612 
613 /******* WAN Device Driver Entry Points *************************************/
614 
615 /*============================================================================
616  * Update device status & statistics
617  * This procedure is called when updating the PROC file system and returns
618  * various communications statistics. These statistics are accumulated from 3
619  * different locations:
620  * 	1) The 'if_stats' recorded for the device.
621  * 	2) Communication error statistics on the adapter.
622  *      3) CHDLC operational statistics on the adapter.
623  * The board level statistics are read during a timer interrupt. Note that we
624  * read the error and operational statistics during consecitive timer ticks so
625  * as to minimize the time that we are inside the interrupt handler.
626  *
627  */
update(wan_device_t * wandev)628 static int update (wan_device_t* wandev)
629 {
630 	sdla_t* card = wandev->private;
631  	netdevice_t* dev;
632         volatile chdlc_private_area_t* chdlc_priv_area;
633         SHARED_MEMORY_INFO_STRUCT *flags;
634 	unsigned long timeout;
635 
636 	/* sanity checks */
637 	if((wandev == NULL) || (wandev->private == NULL))
638 		return -EFAULT;
639 
640 	if(wandev->state == WAN_UNCONFIGURED)
641 		return -ENODEV;
642 
643 	/* more sanity checks */
644         if(!card->u.c.flags)
645                 return -ENODEV;
646 
647 	if(test_bit(PERI_CRIT, (void*)&card->wandev.critical))
648                 return -EAGAIN;
649 
650 	if((dev=card->wandev.dev) == NULL)
651 		return -ENODEV;
652 
653 	if((chdlc_priv_area=dev->priv) == NULL)
654 		return -ENODEV;
655 
656       	flags = card->u.c.flags;
657        	if(chdlc_priv_area->update_comms_stats){
658 		return -EAGAIN;
659 	}
660 
661 	/* we will need 2 timer interrupts to complete the */
662 	/* reading of the statistics */
663 	chdlc_priv_area->update_comms_stats = 2;
664        	flags->interrupt_info_struct.interrupt_permission |= APP_INT_ON_TIMER;
665 	chdlc_priv_area->timer_int_enabled = TMR_INT_ENABLED_UPDATE;
666 
667 	/* wait a maximum of 1 second for the statistics to be updated */
668         timeout = jiffies;
669         for(;;) {
670 		if(chdlc_priv_area->update_comms_stats == 0)
671 			break;
672                 if ((jiffies - timeout) > (1 * HZ)){
673     			chdlc_priv_area->update_comms_stats = 0;
674  			chdlc_priv_area->timer_int_enabled &=
675 				~TMR_INT_ENABLED_UPDATE;
676  			return -EAGAIN;
677 		}
678         }
679 
680 	return 0;
681 }
682 
683 
684 /*============================================================================
685  * Create new logical channel.
686  * This routine is called by the router when ROUTER_IFNEW IOCTL is being
687  * handled.
688  * o parse media- and hardware-specific configuration
689  * o make sure that a new channel can be created
690  * o allocate resources, if necessary
691  * o prepare network device structure for registaration.
692  *
693  * Return:	0	o.k.
694  *		< 0	failure (channel will not be created)
695  */
new_if(wan_device_t * wandev,netdevice_t * dev,wanif_conf_t * conf)696 static int new_if (wan_device_t* wandev, netdevice_t* dev, wanif_conf_t* conf)
697 {
698 	sdla_t* card = wandev->private;
699 	chdlc_private_area_t* chdlc_priv_area;
700 
701 
702 	printk(KERN_INFO "%s: Configuring Interface: %s\n",
703 			card->devname, conf->name);
704 
705 	if ((conf->name[0] == '\0') || (strlen(conf->name) > WAN_IFNAME_SZ)) {
706 		printk(KERN_INFO "%s: Invalid interface name!\n",
707 			card->devname);
708 		return -EINVAL;
709 	}
710 
711 	/* allocate and initialize private data */
712 	chdlc_priv_area = kmalloc(sizeof(chdlc_private_area_t), GFP_KERNEL);
713 
714 	if(chdlc_priv_area == NULL)
715 		return -ENOMEM;
716 
717 	memset(chdlc_priv_area, 0, sizeof(chdlc_private_area_t));
718 
719 	chdlc_priv_area->card = card;
720 	chdlc_priv_area->common.sk = NULL;
721 	chdlc_priv_area->common.func = NULL;
722 
723 	/* initialize data */
724 	strcpy(card->u.c.if_name, conf->name);
725 
726 	if(card->wandev.new_if_cnt > 0) {
727                 kfree(chdlc_priv_area);
728 		return -EEXIST;
729 	}
730 
731 	card->wandev.new_if_cnt++;
732 
733 	chdlc_priv_area->TracingEnabled = 0;
734 	chdlc_priv_area->route_status = NO_ROUTE;
735 	chdlc_priv_area->route_removed = 0;
736 
737 	card->u.c.async_mode = conf->async_mode;
738 
739 	/* setup for asynchronous mode */
740 	if(conf->async_mode) {
741 		printk(KERN_INFO "%s: Configuring for asynchronous mode\n",
742 			wandev->name);
743 
744 		if(card->u.c.comm_port == WANOPT_PRI) {
745 			printk(KERN_INFO
746 				"%s:Asynchronous mode on secondary port only\n",
747 					wandev->name);
748 			kfree(chdlc_priv_area);
749 			return -EINVAL;
750 		}
751 
752 	       	if(strcmp(conf->usedby, "WANPIPE") == 0) {
753 			printk(KERN_INFO
754                                 "%s: Running in WANIPE Async Mode\n",                                        			wandev->name);
755 			card->u.c.usedby = WANPIPE;
756 		}else{
757 			card->u.c.usedby = API;
758 		}
759 
760 		if(!card->wandev.clocking) {
761 			printk(KERN_INFO
762 				"%s: Asynch. clocking must be 'Internal'\n",
763 				wandev->name);
764 			kfree(chdlc_priv_area);
765 			return -EINVAL;
766 		}
767 
768 		if((card->wandev.bps < MIN_ASY_BAUD_RATE) ||
769 			(card->wandev.bps > MAX_ASY_BAUD_RATE)) {
770 			printk(KERN_INFO "%s: Selected baud rate is invalid.\n",
771 				wandev->name);
772 			printk(KERN_INFO "Must be between %u and %u bps.\n",
773 				MIN_ASY_BAUD_RATE, MAX_ASY_BAUD_RATE);
774 			kfree(chdlc_priv_area);
775 			return -EINVAL;
776 		}
777 
778 		card->u.c.api_options = 0;
779                 if (conf->asy_data_trans == WANOPT_YES) {
780                         card->u.c.api_options |= ASY_RX_DATA_TRANSPARENT;
781                 }
782 
783 		card->u.c.protocol_options = 0;
784 		if (conf->rts_hs_for_receive == WANOPT_YES) {
785 			card->u.c.protocol_options |= ASY_RTS_HS_FOR_RX;
786 	        }
787                 if (conf->xon_xoff_hs_for_receive == WANOPT_YES) {
788                         card->u.c.protocol_options |= ASY_XON_XOFF_HS_FOR_RX;
789                 }
790                 if (conf->xon_xoff_hs_for_transmit == WANOPT_YES) {
791                         card->u.c.protocol_options |= ASY_XON_XOFF_HS_FOR_TX;
792                 }
793                 if (conf->dcd_hs_for_transmit == WANOPT_YES) {
794                         card->u.c.protocol_options |= ASY_DCD_HS_FOR_TX;
795                 }
796                 if (conf->cts_hs_for_transmit == WANOPT_YES) {
797                         card->u.c.protocol_options |= ASY_CTS_HS_FOR_TX;
798                 }
799 
800 		card->u.c.tx_bits_per_char = conf->tx_bits_per_char;
801                 card->u.c.rx_bits_per_char = conf->rx_bits_per_char;
802                 card->u.c.stop_bits = conf->stop_bits;
803 		card->u.c.parity = conf->parity;
804 		card->u.c.break_timer = conf->break_timer;
805 		card->u.c.inter_char_timer = conf->inter_char_timer;
806 		card->u.c.rx_complete_length = conf->rx_complete_length;
807 		card->u.c.xon_char = conf->xon_char;
808 
809 	} else {	/* setup for synchronous mode */
810 
811 		card->u.c.protocol_options = 0;
812 		if (conf->ignore_dcd == WANOPT_YES){
813 			card->u.c.protocol_options |= IGNORE_DCD_FOR_LINK_STAT;
814 		}
815 		if (conf->ignore_cts == WANOPT_YES){
816 			card->u.c.protocol_options |= IGNORE_CTS_FOR_LINK_STAT;
817 		}
818 
819 		if (conf->ignore_keepalive == WANOPT_YES) {
820 			card->u.c.protocol_options |=
821 				IGNORE_KPALV_FOR_LINK_STAT;
822 			card->u.c.kpalv_tx  = MIN_Tx_KPALV_TIMER;
823 			card->u.c.kpalv_rx  = MIN_Rx_KPALV_TIMER;
824 			card->u.c.kpalv_err = MIN_KPALV_ERR_TOL;
825 
826 		} else {   /* Do not ignore keepalives */
827 			card->u.c.kpalv_tx =
828 				((conf->keepalive_tx_tmr - MIN_Tx_KPALV_TIMER)
829 				>= 0) ?
830 	   			min_t(unsigned int, conf->keepalive_tx_tmr,MAX_Tx_KPALV_TIMER) :
831 				DEFAULT_Tx_KPALV_TIMER;
832 
833 			card->u.c.kpalv_rx =
834 		   		((conf->keepalive_rx_tmr - MIN_Rx_KPALV_TIMER)
835 				>= 0) ?
836 	   			min_t(unsigned int, conf->keepalive_rx_tmr,MAX_Rx_KPALV_TIMER) :
837 				DEFAULT_Rx_KPALV_TIMER;
838 
839 			card->u.c.kpalv_err =
840 		   		((conf->keepalive_err_margin-MIN_KPALV_ERR_TOL)
841 				>= 0) ?
842 	   			min_t(unsigned int, conf->keepalive_err_margin,
843 				MAX_KPALV_ERR_TOL) :
844 	   			DEFAULT_KPALV_ERR_TOL;
845 		}
846 
847 		/* Setup slarp timer to control delay between slarps */
848 		card->u.c.slarp_timer =
849 			((conf->slarp_timer - MIN_SLARP_REQ_TIMER) >= 0) ?
850 			min_t(unsigned int, conf->slarp_timer, MAX_SLARP_REQ_TIMER) :
851 			DEFAULT_SLARP_REQ_TIMER;
852 
853 #ifdef LINUX_2_0
854 		if (card->u.c.slarp_timer){
855 			printk(KERN_INFO
856 				"%s: Error: Dynamic IP support not available for 2.0.X kernels\n",
857 					card->devname);
858 			printk(KERN_INFO "%s:        Defaulting to Static IP addressing\n",
859 						card->devname);
860 		}
861 		card->u.c.slarp_timer=0;
862 #endif
863 
864 
865 		if (conf->hdlc_streaming == WANOPT_YES) {
866 			printk(KERN_INFO "%s: Enabling HDLC STREAMING Mode\n",
867 				wandev->name);
868 			card->u.c.protocol_options = HDLC_STREAMING_MODE;
869 		}
870 
871 		if ((chdlc_priv_area->true_if_encoding = conf->true_if_encoding) == WANOPT_YES){
872 			printk(KERN_INFO
873 				"%s: Enabling, true interface type encoding.\n",
874 				card->devname);
875 		}
876 
877         	/* Setup wanpipe as a router (WANPIPE) or as an API */
878 		if( strcmp(conf->usedby, "WANPIPE") == 0) {
879 
880 			printk(KERN_INFO "%s: Running in WANPIPE mode!\n",
881 				wandev->name);
882 			card->u.c.usedby = WANPIPE;
883 
884 			/* Option to bring down the interface when
885         		 * the link goes down */
886 			if (conf->if_down){
887 				set_bit(DYN_OPT_ON,&chdlc_priv_area->interface_down);
888 				printk(KERN_INFO
889 				 "%s: Dynamic interface configuration enabled\n",
890 				   card->devname);
891 			}
892 
893 		} else if( strcmp(conf->usedby, "API") == 0) {
894 #if defined(LINUX_2_1) || defined(LINUX_2_4)
895 			card->u.c.usedby = API;
896 			printk(KERN_INFO "%s: Running in API mode !\n",
897 				wandev->name);
898 #else
899 			printk(KERN_INFO "%s: API Mode is not supported for kernels lower than 2.2.X!\n",
900 				wandev->name);
901 			printk(KERN_INFO "%s: Please upgrade to a 2.2.X kernel fro the API support\n",
902 				wandev->name);
903 	                kfree(chdlc_priv_area);
904 			return -EINVAL;
905 #endif
906 		}
907 	}
908 
909 #if defined(LINUX_2_1) || defined(LINUX_2_4)
910 	/* Tells us that if this interface is a
911          * gateway or not */
912 	if ((chdlc_priv_area->gateway = conf->gateway) == WANOPT_YES){
913 		printk(KERN_INFO "%s: Interface %s is set as a gateway.\n",
914 			card->devname,card->u.c.if_name);
915 	}
916 #endif
917 
918 	/* Get Multicast Information */
919 	chdlc_priv_area->mc = conf->mc;
920 
921 	/* prepare network device data space for registration */
922 #ifdef LINUX_2_4
923 	strcpy(dev->name,card->u.c.if_name);
924 #else
925 	dev->name = (char *)kmalloc(strlen(card->u.c.if_name) + 2, GFP_KERNEL);
926 	sprintf(dev->name, "%s", card->u.c.if_name);
927 #endif
928 
929 	dev->init = &if_init;
930 	dev->priv = chdlc_priv_area;
931 
932 	/* Initialize the polling task routine */
933 #ifndef LINUX_2_4
934 	chdlc_priv_area->poll_task.next = NULL;
935 #endif
936 	chdlc_priv_area->poll_task.sync=0;
937 	chdlc_priv_area->poll_task.routine = (void*)(void*)chdlc_poll;
938 	chdlc_priv_area->poll_task.data = dev;
939 
940 	/* Initialize the polling delay timer */
941 	init_timer(&chdlc_priv_area->poll_delay_timer);
942 	chdlc_priv_area->poll_delay_timer.data = (unsigned long)dev;
943 	chdlc_priv_area->poll_delay_timer.function = chdlc_poll_delay;
944 
945 	printk(KERN_INFO "\n");
946 
947 	return 0;
948 }
949 
950 
951 /****** Network Device Interface ********************************************/
952 
953 /*============================================================================
954  * Initialize Linux network interface.
955  *
956  * This routine is called only once for each interface, during Linux network
957  * interface registration.  Returning anything but zero will fail interface
958  * registration.
959  */
if_init(netdevice_t * dev)960 static int if_init (netdevice_t* dev)
961 	{
962 	chdlc_private_area_t* chdlc_priv_area = dev->priv;
963 	sdla_t* card = chdlc_priv_area->card;
964 	wan_device_t* wandev = &card->wandev;
965 #ifdef LINUX_2_0
966 	int i;
967 #endif
968 
969 	/* Initialize device driver entry points */
970 	dev->open		= &if_open;
971 	dev->stop		= &if_close;
972 	dev->hard_header	= &if_header;
973 	dev->rebuild_header	= &if_rebuild_hdr;
974 	dev->hard_start_xmit	= &if_send;
975 	dev->get_stats		= &if_stats;
976 #ifdef LINUX_2_4
977 	dev->tx_timeout		= &if_tx_timeout;
978 	dev->watchdog_timeo	= TX_TIMEOUT;
979 #endif
980 
981 
982 	/* Initialize media-specific parameters */
983 	dev->flags		|= IFF_POINTOPOINT;
984 	dev->flags		|= IFF_NOARP;
985 
986 	/* Enable Mulitcasting if user selected */
987 	if (chdlc_priv_area->mc == WANOPT_YES){
988 		dev->flags 	|= IFF_MULTICAST;
989 	}
990 
991 #ifdef LINUX_2_0
992 	dev->family		= AF_INET;
993 #endif
994 
995 	if (chdlc_priv_area->true_if_encoding){
996 #if defined(LINUX_2_1) || defined(LINUX_2_4)
997 		dev->type	= ARPHRD_HDLC; /* This breaks the tcpdump */
998 #else
999 		dev->type	= ARPHRD_PPP;
1000 #endif
1001 	}else{
1002 		dev->type	= ARPHRD_PPP;
1003 	}
1004 
1005 	dev->mtu		= card->wandev.mtu;
1006 	/* for API usage, add the API header size to the requested MTU size */
1007 	if(card->u.c.usedby == API) {
1008 		dev->mtu += sizeof(api_tx_hdr_t);
1009 	}
1010 
1011 	dev->hard_header_len	= CHDLC_HDR_LEN;
1012 
1013 	/* Initialize hardware parameters */
1014 	dev->irq	= wandev->irq;
1015 	dev->dma	= wandev->dma;
1016 	dev->base_addr	= wandev->ioport;
1017 	dev->mem_start	= wandev->maddr;
1018 	dev->mem_end	= wandev->maddr + wandev->msize - 1;
1019 
1020 	/* Set transmit buffer queue length
1021 	 * If too low packets will not be retransmitted
1022          * by stack.
1023 	 */
1024         dev->tx_queue_len = 100;
1025 
1026 	/* Initialize socket buffers */
1027 #if !defined(LINUX_2_1) && !defined(LINUX_2_4)
1028         for (i = 0; i < DEV_NUMBUFFS; ++i)
1029                 skb_queue_head_init(&dev->buffs[i]);
1030 #endif
1031 	return 0;
1032 }
1033 
1034 /*============================================================================
1035  * Open network interface.
1036  * o enable communications and interrupts.
1037  * o prevent module from unloading by incrementing use count
1038  *
1039  * Return 0 if O.k. or errno.
1040  */
if_open(netdevice_t * dev)1041 static int if_open (netdevice_t* dev)
1042 {
1043 	chdlc_private_area_t* chdlc_priv_area = dev->priv;
1044 	sdla_t* card = chdlc_priv_area->card;
1045 	struct timeval tv;
1046 	int err = 0;
1047 
1048 	/* Only one open per interface is allowed */
1049 
1050 	if (is_dev_running(dev))
1051 		return -EBUSY;
1052 
1053 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1054 	/* Initialize the task queue */
1055 	chdlc_priv_area->tq_working=0;
1056 
1057 #ifndef LINUX_2_4
1058 	chdlc_priv_area->common.wanpipe_task.next = NULL;
1059 #endif
1060 	chdlc_priv_area->common.wanpipe_task.sync = 0;
1061 	chdlc_priv_area->common.wanpipe_task.routine = (void *)(void *)chdlc_bh;
1062 	chdlc_priv_area->common.wanpipe_task.data = dev;
1063 
1064 	/* Allocate and initialize BH circular buffer */
1065 	/* Add 1 to MAX_BH_BUFF so we don't have test with (MAX_BH_BUFF-1) */
1066 	chdlc_priv_area->bh_head = kmalloc((sizeof(bh_data_t)*(MAX_BH_BUFF+1)),GFP_ATOMIC);
1067 	memset(chdlc_priv_area->bh_head,0,(sizeof(bh_data_t)*(MAX_BH_BUFF+1)));
1068 	atomic_set(&chdlc_priv_area->bh_buff_used, 0);
1069 #endif
1070 
1071 	do_gettimeofday(&tv);
1072 	chdlc_priv_area->router_start_time = tv.tv_sec;
1073 
1074 #ifdef LINUX_2_4
1075 	netif_start_queue(dev);
1076 #else
1077 	dev->interrupt = 0;
1078 	dev->tbusy = 0;
1079 	dev->start = 1;
1080 #endif
1081 
1082 	wanpipe_open(card);
1083 
1084 	/* TTY is configured during wanpipe_set_termios
1085 	 * call, not here */
1086 	if (card->tty_opt)
1087 		return err;
1088 
1089 	set_bit(0,&chdlc_priv_area->config_chdlc);
1090 	chdlc_priv_area->config_chdlc_timeout=jiffies;
1091 
1092 	/* Start the CHDLC configuration after 1sec delay.
1093 	 * This will give the interface initilization time
1094 	 * to finish its configuration */
1095 	mod_timer(&chdlc_priv_area->poll_delay_timer, jiffies + HZ);
1096 	return err;
1097 }
1098 
1099 /*============================================================================
1100  * Close network interface.
1101  * o if this is the last close, then disable communications and interrupts.
1102  * o reset flags.
1103  */
if_close(netdevice_t * dev)1104 static int if_close (netdevice_t* dev)
1105 {
1106 	chdlc_private_area_t* chdlc_priv_area = dev->priv;
1107 	sdla_t* card = chdlc_priv_area->card;
1108 
1109 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1110 
1111 	if (chdlc_priv_area->bh_head){
1112 		int i;
1113 		struct sk_buff *skb;
1114 
1115 		for (i=0; i<(MAX_BH_BUFF+1); i++){
1116 			skb = ((bh_data_t *)&chdlc_priv_area->bh_head[i])->skb;
1117 			if (skb != NULL){
1118                 		wan_dev_kfree_skb(skb, FREE_READ);
1119 			}
1120 		}
1121 		kfree(chdlc_priv_area->bh_head);
1122 		chdlc_priv_area->bh_head=NULL;
1123 	}
1124 #endif
1125 
1126 	stop_net_queue(dev);
1127 #ifndef LINUX_2_4
1128 	dev->start=0;
1129 #endif
1130 	wanpipe_close(card);
1131 	del_timer(&chdlc_priv_area->poll_delay_timer);
1132 	return 0;
1133 }
1134 
disable_comm(sdla_t * card)1135 static void disable_comm (sdla_t *card)
1136 {
1137 	SHARED_MEMORY_INFO_STRUCT *flags = card->u.c.flags;
1138 
1139 	if (card->u.c.comm_enabled){
1140 		chdlc_disable_comm_shutdown (card);
1141 	}else{
1142 		flags->interrupt_info_struct.interrupt_permission = 0;
1143 	}
1144 
1145 #if defined(LINUX_2_4) || defined(LINUX_2_1)
1146 	if (!tty_init_cnt)
1147 		return;
1148 
1149 	if (card->tty_opt){
1150 		struct serial_state * state;
1151 		if (!(--tty_init_cnt)){
1152 			int e1,e2;
1153 			*serial_driver.refcount=0;
1154 
1155 			if ((e1 = tty_unregister_driver(&serial_driver)))
1156 				printk("SERIAL: failed to unregister serial driver (%d)\n",
1157 				       e1);
1158 			if ((e2 = tty_unregister_driver(&callout_driver)))
1159 				printk("SERIAL: failed to unregister callout driver (%d)\n",
1160 				       e2);
1161 			printk(KERN_INFO "%s: Unregistering TTY Driver, Major %i\n",
1162 					card->devname,WAN_TTY_MAJOR);
1163 		}
1164 		card->tty=NULL;
1165 		tty_card_map[card->tty_minor]=NULL;
1166 		state = &rs_table[card->tty_minor];
1167 		memset(state,0,sizeof(state));
1168 	}
1169 #endif
1170 	return;
1171 }
1172 
1173 
1174 /*============================================================================
1175  * Build media header.
1176  *
1177  * The trick here is to put packet type (Ethertype) into 'protocol' field of
1178  * the socket buffer, so that we don't forget it.  If packet type is not
1179  * supported, set skb->protocol to 0 and discard packet later.
1180  *
1181  * Return:	media header length.
1182  */
if_header(struct sk_buff * skb,netdevice_t * dev,unsigned short type,void * daddr,void * saddr,unsigned len)1183 static int if_header (struct sk_buff* skb, netdevice_t* dev,
1184 	unsigned short type, void* daddr, void* saddr, unsigned len)
1185 {
1186 	skb->protocol = htons(type);
1187 
1188 	return CHDLC_HDR_LEN;
1189 }
1190 
1191 
1192 #ifdef LINUX_2_4
1193 /*============================================================================
1194  * Handle transmit timeout event from netif watchdog
1195  */
if_tx_timeout(netdevice_t * dev)1196 static void if_tx_timeout (netdevice_t *dev)
1197 {
1198     	chdlc_private_area_t* chan = dev->priv;
1199 	sdla_t *card = chan->card;
1200 
1201 	/* If our device stays busy for at least 5 seconds then we will
1202 	 * kick start the device by making dev->tbusy = 0.  We expect
1203 	 * that our device never stays busy more than 5 seconds. So this
1204 	 * is only used as a last resort.
1205 	 */
1206 
1207 	++card->wandev.stats.collisions;
1208 
1209 	printk (KERN_INFO "%s: Transmit timed out on %s\n", card->devname,dev->name);
1210 	netif_wake_queue (dev);
1211 }
1212 #endif
1213 
1214 
1215 
1216 /*============================================================================
1217  * Re-build media header.
1218  *
1219  * Return:	1	physical address resolved.
1220  *		0	physical address not resolved
1221  */
1222 #if defined(LINUX_2_1) || defined(LINUX_2_4)
if_rebuild_hdr(struct sk_buff * skb)1223 static int if_rebuild_hdr (struct sk_buff *skb)
1224 {
1225 	return 1;
1226 }
1227 #else
if_rebuild_hdr(void * hdr,netdevice_t * dev,unsigned long raddr,struct sk_buff * skb)1228 static int if_rebuild_hdr (void* hdr, netdevice_t* dev, unsigned long raddr,
1229                            struct sk_buff* skb)
1230 {
1231         return 1;
1232 }
1233 #endif
1234 
1235 /*============================================================================
1236  * Send a packet on a network interface.
1237  * o set tbusy flag (marks start of the transmission) to block a timer-based
1238  *   transmit from overlapping.
1239  * o check link state. If link is not up, then drop the packet.
1240  * o execute adapter send command.
1241  * o free socket buffer
1242  *
1243  * Return:	0	complete (socket buffer must be freed)
1244  *		non-0	packet may be re-transmitted (tbusy must be set)
1245  *
1246  * Notes:
1247  * 1. This routine is called either by the protocol stack or by the "net
1248  *    bottom half" (with interrupts enabled).
1249  * 2. Setting tbusy flag will inhibit further transmit requests from the
1250  *    protocol stack and can be used for flow control with protocol layer.
1251  */
if_send(struct sk_buff * skb,netdevice_t * dev)1252 static int if_send (struct sk_buff* skb, netdevice_t* dev)
1253 {
1254 	chdlc_private_area_t *chdlc_priv_area = dev->priv;
1255 	sdla_t *card = chdlc_priv_area->card;
1256 	SHARED_MEMORY_INFO_STRUCT *flags = card->u.c.flags;
1257 	INTERRUPT_INFORMATION_STRUCT *chdlc_int = &flags->interrupt_info_struct;
1258 	int udp_type = 0;
1259 	unsigned long smp_flags;
1260 	int err=0;
1261 
1262 #ifdef LINUX_2_4
1263 	netif_stop_queue(dev);
1264 #endif
1265 
1266 	if (skb == NULL){
1267 		/* If we get here, some higher layer thinks we've missed an
1268 		 * tx-done interrupt.
1269 		 */
1270 		printk(KERN_INFO "%s: interface %s got kicked!\n",
1271 			card->devname, dev->name);
1272 
1273 		wake_net_dev(dev);
1274 		return 0;
1275 	}
1276 
1277 #ifndef LINUX_2_4
1278 	if (dev->tbusy){
1279 
1280 		/* If our device stays busy for at least 5 seconds then we will
1281 		 * kick start the device by making dev->tbusy = 0.  We expect
1282 		 * that our device never stays busy more than 5 seconds. So this
1283 		 * is only used as a last resort.
1284 		 */
1285                 ++card->wandev.stats.collisions;
1286 		if((jiffies - chdlc_priv_area->tick_counter) < (5 * HZ)) {
1287 			return 1;
1288 		}
1289 
1290 		printk (KERN_INFO "%s: Transmit timeout !\n",
1291 			card->devname);
1292 
1293 		/* unbusy the interface */
1294 		clear_bit(0,&dev->tbusy);
1295 	}
1296 #endif
1297 
1298    	if (ntohs(skb->protocol) != htons(PVC_PROT)){
1299 
1300 		/* check the udp packet type */
1301 
1302 		udp_type = udp_pkt_type(skb, card);
1303 
1304 		if (udp_type == UDP_CPIPE_TYPE){
1305                         if(store_udp_mgmt_pkt(UDP_PKT_FRM_STACK, card, skb, dev,
1306                                 chdlc_priv_area)){
1307 	                	chdlc_int->interrupt_permission |=
1308 					APP_INT_ON_TIMER;
1309 			}
1310 			start_net_queue(dev);
1311 			return 0;
1312 		}
1313 
1314 		/* check to see if the source IP address is a broadcast or */
1315 		/* multicast IP address */
1316                 if(chk_bcast_mcast_addr(card, dev, skb)){
1317 			++card->wandev.stats.tx_dropped;
1318 			wan_dev_kfree_skb(skb,FREE_WRITE);
1319 			start_net_queue(dev);
1320 			return 0;
1321 		}
1322         }
1323 
1324 	/* Lock the 508 Card: SMP is supported */
1325       	if(card->hw.type != SDLA_S514){
1326 		s508_lock(card,&smp_flags);
1327 	}
1328 
1329     	if(test_and_set_bit(SEND_CRIT, (void*)&card->wandev.critical)) {
1330 
1331 		printk(KERN_INFO "%s: Critical in if_send: %lx\n",
1332 					card->wandev.name,card->wandev.critical);
1333                 ++card->wandev.stats.tx_dropped;
1334 		start_net_queue(dev);
1335 		goto if_send_exit_crit;
1336 	}
1337 
1338 	if(card->u.c.state != WAN_CONNECTED){
1339        		++card->wandev.stats.tx_dropped;
1340 		start_net_queue(dev);
1341 
1342 	}else if(!skb->protocol){
1343         	++card->wandev.stats.tx_errors;
1344 		start_net_queue(dev);
1345 
1346 	}else {
1347 		void* data = skb->data;
1348 		unsigned len = skb->len;
1349 		unsigned char attr;
1350 
1351 		/* If it's an API packet pull off the API
1352 		 * header. Also check that the packet size
1353 		 * is larger than the API header
1354 	         */
1355 		if (card->u.c.usedby == API){
1356 			api_tx_hdr_t* api_tx_hdr;
1357 
1358 			/* discard the frame if we are configured for */
1359 			/* 'receive only' mode or if there is no data */
1360 			if (card->u.c.receive_only ||
1361 				(len <= sizeof(api_tx_hdr_t))) {
1362 
1363 				++card->wandev.stats.tx_dropped;
1364 				start_net_queue(dev);
1365 				goto if_send_exit_crit;
1366 			}
1367 
1368 			api_tx_hdr = (api_tx_hdr_t *)data;
1369 			attr = api_tx_hdr->attr;
1370 			data += sizeof(api_tx_hdr_t);
1371 			len -= sizeof(api_tx_hdr_t);
1372 		}
1373 
1374 		if(chdlc_send(card, data, len)) {
1375 			stop_net_queue(dev);
1376 		}else{
1377 			++card->wandev.stats.tx_packets;
1378 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1379                         card->wandev.stats.tx_bytes += len;
1380 #endif
1381 
1382 			start_net_queue(dev);
1383 
1384 #ifdef LINUX_2_4
1385 		 	dev->trans_start = jiffies;
1386 #endif
1387 		}
1388 	}
1389 
1390 if_send_exit_crit:
1391 
1392 	if (!(err=is_queue_stopped(dev))) {
1393 		wan_dev_kfree_skb(skb, FREE_WRITE);
1394 	}else{
1395 		chdlc_priv_area->tick_counter = jiffies;
1396 		chdlc_int->interrupt_permission |= APP_INT_ON_TX_FRAME;
1397 	}
1398 
1399 	clear_bit(SEND_CRIT, (void*)&card->wandev.critical);
1400 	if(card->hw.type != SDLA_S514){
1401 		s508_unlock(card,&smp_flags);
1402 	}
1403 
1404 	return err;
1405 }
1406 
1407 
1408 /*============================================================================
1409  * Check to see if the packet to be transmitted contains a broadcast or
1410  * multicast source IP address.
1411  */
1412 
chk_bcast_mcast_addr(sdla_t * card,netdevice_t * dev,struct sk_buff * skb)1413 static int chk_bcast_mcast_addr(sdla_t *card, netdevice_t* dev,
1414 				struct sk_buff *skb)
1415 {
1416 	u32 src_ip_addr;
1417         u32 broadcast_ip_addr = 0;
1418 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1419         struct in_device *in_dev;
1420 #endif
1421         /* read the IP source address from the outgoing packet */
1422         src_ip_addr = *(u32 *)(skb->data + 12);
1423 
1424 	/* read the IP broadcast address for the device */
1425 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1426         in_dev = dev->ip_ptr;
1427         if(in_dev != NULL) {
1428                 struct in_ifaddr *ifa= in_dev->ifa_list;
1429                 if(ifa != NULL)
1430                         broadcast_ip_addr = ifa->ifa_broadcast;
1431                 else
1432                         return 0;
1433         }
1434 #else
1435         broadcast_ip_addr = dev->pa_brdaddr;
1436 #endif
1437 
1438         /* check if the IP Source Address is a Broadcast address */
1439         if((dev->flags & IFF_BROADCAST) && (src_ip_addr == broadcast_ip_addr)) {
1440                 printk(KERN_INFO "%s: Broadcast Source Address silently discarded\n",
1441 				card->devname);
1442                 return 1;
1443         }
1444 
1445         /* check if the IP Source Address is a Multicast address */
1446         if((ntohl(src_ip_addr) >= 0xE0000001) &&
1447 		(ntohl(src_ip_addr) <= 0xFFFFFFFE)) {
1448                 printk(KERN_INFO "%s: Multicast Source Address silently discarded\n",
1449 				card->devname);
1450                 return 1;
1451         }
1452 
1453         return 0;
1454 }
1455 
1456 
1457 /*============================================================================
1458  * Reply to UDP Management system.
1459  * Return length of reply.
1460  */
reply_udp(unsigned char * data,unsigned int mbox_len)1461 static int reply_udp( unsigned char *data, unsigned int mbox_len )
1462 {
1463 
1464 	unsigned short len, udp_length, temp, ip_length;
1465 	unsigned long ip_temp;
1466 	int even_bound = 0;
1467   	chdlc_udp_pkt_t *c_udp_pkt = (chdlc_udp_pkt_t *)data;
1468 
1469 	/* Set length of packet */
1470 	len = sizeof(ip_pkt_t)+
1471 	      sizeof(udp_pkt_t)+
1472 	      sizeof(wp_mgmt_t)+
1473 	      sizeof(cblock_t)+
1474 	      sizeof(trace_info_t)+
1475 	      mbox_len;
1476 
1477 	/* fill in UDP reply */
1478 	c_udp_pkt->wp_mgmt.request_reply = UDPMGMT_REPLY;
1479 
1480 	/* fill in UDP length */
1481 	udp_length = sizeof(udp_pkt_t)+
1482 		     sizeof(wp_mgmt_t)+
1483 		     sizeof(cblock_t)+
1484 	             sizeof(trace_info_t)+
1485 		     mbox_len;
1486 
1487  	/* put it on an even boundary */
1488 	if ( udp_length & 0x0001 ) {
1489 		udp_length += 1;
1490 		len += 1;
1491 		even_bound = 1;
1492 	}
1493 
1494 	temp = (udp_length<<8)|(udp_length>>8);
1495 	c_udp_pkt->udp_pkt.udp_length = temp;
1496 
1497 	/* swap UDP ports */
1498 	temp = c_udp_pkt->udp_pkt.udp_src_port;
1499 	c_udp_pkt->udp_pkt.udp_src_port =
1500 			c_udp_pkt->udp_pkt.udp_dst_port;
1501 	c_udp_pkt->udp_pkt.udp_dst_port = temp;
1502 
1503 	/* add UDP pseudo header */
1504 	temp = 0x1100;
1505 	*((unsigned short *)(c_udp_pkt->data+mbox_len+even_bound)) = temp;
1506 	temp = (udp_length<<8)|(udp_length>>8);
1507 	*((unsigned short *)(c_udp_pkt->data+mbox_len+even_bound+2)) = temp;
1508 
1509 
1510 	/* calculate UDP checksum */
1511 	c_udp_pkt->udp_pkt.udp_checksum = 0;
1512 	c_udp_pkt->udp_pkt.udp_checksum = calc_checksum(&data[UDP_OFFSET],udp_length+UDP_OFFSET);
1513 
1514 	/* fill in IP length */
1515 	ip_length = len;
1516 	temp = (ip_length<<8)|(ip_length>>8);
1517 	c_udp_pkt->ip_pkt.total_length = temp;
1518 
1519 	/* swap IP addresses */
1520 	ip_temp = c_udp_pkt->ip_pkt.ip_src_address;
1521 	c_udp_pkt->ip_pkt.ip_src_address = c_udp_pkt->ip_pkt.ip_dst_address;
1522 	c_udp_pkt->ip_pkt.ip_dst_address = ip_temp;
1523 
1524 	/* fill in IP checksum */
1525 	c_udp_pkt->ip_pkt.hdr_checksum = 0;
1526 	c_udp_pkt->ip_pkt.hdr_checksum = calc_checksum(data,sizeof(ip_pkt_t));
1527 
1528 	return len;
1529 
1530 } /* reply_udp */
1531 
calc_checksum(char * data,int len)1532 unsigned short calc_checksum (char *data, int len)
1533 {
1534 	unsigned short temp;
1535 	unsigned long sum=0;
1536 	int i;
1537 
1538 	for( i = 0; i <len; i+=2 ) {
1539 		memcpy(&temp,&data[i],2);
1540 		sum += (unsigned long)temp;
1541 	}
1542 
1543 	while (sum >> 16 ) {
1544 		sum = (sum & 0xffffUL) + (sum >> 16);
1545 	}
1546 
1547 	temp = (unsigned short)sum;
1548 	temp = ~temp;
1549 
1550 	if( temp == 0 )
1551 		temp = 0xffff;
1552 
1553 	return temp;
1554 }
1555 
1556 
1557 /*============================================================================
1558  * Get ethernet-style interface statistics.
1559  * Return a pointer to struct enet_statistics.
1560  */
1561 #if defined(LINUX_2_1) || defined(LINUX_2_4)
if_stats(netdevice_t * dev)1562 static struct net_device_stats* if_stats (netdevice_t* dev)
1563 {
1564 	sdla_t *my_card;
1565 	chdlc_private_area_t* chdlc_priv_area;
1566 
1567 	if ((chdlc_priv_area=dev->priv) == NULL)
1568 		return NULL;
1569 
1570 	my_card = chdlc_priv_area->card;
1571 	return &my_card->wandev.stats;
1572 }
1573 #else
if_stats(netdevice_t * dev)1574 static struct enet_statistics* if_stats (netdevice_t* dev)
1575 {
1576         sdla_t *my_card;
1577         chdlc_private_area_t* chdlc_priv_area = dev->priv;
1578 
1579 	if ((chdlc_priv_area=dev->priv) == NULL)
1580 		return NULL;
1581 
1582         my_card = chdlc_priv_area->card;
1583         return &my_card->wandev.stats;
1584 }
1585 #endif
1586 
1587 /****** Cisco HDLC Firmware Interface Functions *******************************/
1588 
1589 /*============================================================================
1590  * Read firmware code version.
1591  *	Put code version as ASCII string in str.
1592  */
chdlc_read_version(sdla_t * card,char * str)1593 static int chdlc_read_version (sdla_t* card, char* str)
1594 {
1595 	CHDLC_MAILBOX_STRUCT* mb = card->mbox;
1596 	int len;
1597 	char err;
1598 	mb->buffer_length = 0;
1599 	mb->command = READ_CHDLC_CODE_VERSION;
1600 	err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
1601 
1602 	if(err != COMMAND_OK) {
1603 		chdlc_error(card,err,mb);
1604 	}
1605 	else if (str) {  /* is not null */
1606 		len = mb->buffer_length;
1607 		memcpy(str, mb->data, len);
1608 		str[len] = '\0';
1609 	}
1610 	return (err);
1611 }
1612 
1613 /*-----------------------------------------------------------------------------
1614  *  Configure CHDLC firmware.
1615  */
chdlc_configure(sdla_t * card,void * data)1616 static int chdlc_configure (sdla_t* card, void* data)
1617 {
1618 	int err;
1619 	CHDLC_MAILBOX_STRUCT *mailbox = card->mbox;
1620 	int data_length = sizeof(CHDLC_CONFIGURATION_STRUCT);
1621 
1622 	mailbox->buffer_length = data_length;
1623 	memcpy(mailbox->data, data, data_length);
1624 	mailbox->command = SET_CHDLC_CONFIGURATION;
1625 	err = sdla_exec(mailbox) ? mailbox->return_code : CMD_TIMEOUT;
1626 
1627 	if (err != COMMAND_OK) chdlc_error (card, err, mailbox);
1628 
1629 	return err;
1630 }
1631 
1632 
1633 /*============================================================================
1634  * Set interrupt mode -- HDLC Version.
1635  */
1636 
chdlc_set_intr_mode(sdla_t * card,unsigned mode)1637 static int chdlc_set_intr_mode (sdla_t* card, unsigned mode)
1638 {
1639 	CHDLC_MAILBOX_STRUCT* mb = card->mbox;
1640 	CHDLC_INT_TRIGGERS_STRUCT* int_data =
1641 		 (CHDLC_INT_TRIGGERS_STRUCT *)mb->data;
1642 	int err;
1643 
1644 	int_data->CHDLC_interrupt_triggers 	= mode;
1645 	int_data->IRQ				= card->hw.irq;
1646 	int_data->interrupt_timer               = 1;
1647 
1648 	mb->buffer_length = sizeof(CHDLC_INT_TRIGGERS_STRUCT);
1649 	mb->command = SET_CHDLC_INTERRUPT_TRIGGERS;
1650 	err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
1651 	if (err != COMMAND_OK)
1652 		chdlc_error (card, err, mb);
1653 	return err;
1654 }
1655 
1656 
1657 /*===========================================================
1658  * chdlc_disable_comm_shutdown
1659  *
1660  * Shutdown() disables the communications. We must
1661  * have a sparate functions, because we must not
1662  * call chdlc_error() hander since the private
1663  * area has already been replaced */
1664 
chdlc_disable_comm_shutdown(sdla_t * card)1665 static int chdlc_disable_comm_shutdown (sdla_t *card)
1666 {
1667 	CHDLC_MAILBOX_STRUCT* mb = card->mbox;
1668 	CHDLC_INT_TRIGGERS_STRUCT* int_data =
1669 		 (CHDLC_INT_TRIGGERS_STRUCT *)mb->data;
1670 	int err;
1671 
1672 	/* Disable Interrutps */
1673 	int_data->CHDLC_interrupt_triggers 	= 0;
1674 	int_data->IRQ				= card->hw.irq;
1675 	int_data->interrupt_timer               = 1;
1676 
1677 	mb->buffer_length = sizeof(CHDLC_INT_TRIGGERS_STRUCT);
1678 	mb->command = SET_CHDLC_INTERRUPT_TRIGGERS;
1679 	err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
1680 
1681 	/* Disable Communications */
1682 
1683 	if (card->u.c.async_mode) {
1684 		mb->command = DISABLE_ASY_COMMUNICATIONS;
1685 	}else{
1686 		mb->command = DISABLE_CHDLC_COMMUNICATIONS;
1687 	}
1688 
1689 	mb->buffer_length = 0;
1690 	err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
1691 
1692 	card->u.c.comm_enabled = 0;
1693 
1694 	return 0;
1695 }
1696 
1697 /*============================================================================
1698  * Enable communications.
1699  */
1700 
chdlc_comm_enable(sdla_t * card)1701 static int chdlc_comm_enable (sdla_t* card)
1702 {
1703 	int err;
1704 	CHDLC_MAILBOX_STRUCT* mb = card->mbox;
1705 
1706 	mb->buffer_length = 0;
1707 	mb->command = ENABLE_CHDLC_COMMUNICATIONS;
1708 	err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
1709 	if (err != COMMAND_OK)
1710 		chdlc_error(card, err, mb);
1711 	else
1712 		card->u.c.comm_enabled = 1;
1713 
1714 	return err;
1715 }
1716 
1717 /*============================================================================
1718  * Read communication error statistics.
1719  */
chdlc_read_comm_err_stats(sdla_t * card)1720 static int chdlc_read_comm_err_stats (sdla_t* card)
1721 {
1722         int err;
1723         CHDLC_MAILBOX_STRUCT* mb = card->mbox;
1724 
1725         mb->buffer_length = 0;
1726         mb->command = READ_COMMS_ERROR_STATS;
1727         err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
1728         if (err != COMMAND_OK)
1729                 chdlc_error(card,err,mb);
1730         return err;
1731 }
1732 
1733 
1734 /*============================================================================
1735  * Read CHDLC operational statistics.
1736  */
chdlc_read_op_stats(sdla_t * card)1737 static int chdlc_read_op_stats (sdla_t* card)
1738 {
1739         int err;
1740         CHDLC_MAILBOX_STRUCT* mb = card->mbox;
1741 
1742         mb->buffer_length = 0;
1743         mb->command = READ_CHDLC_OPERATIONAL_STATS;
1744         err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
1745         if (err != COMMAND_OK)
1746                 chdlc_error(card,err,mb);
1747         return err;
1748 }
1749 
1750 
1751 /*============================================================================
1752  * Update communications error and general packet statistics.
1753  */
update_comms_stats(sdla_t * card,chdlc_private_area_t * chdlc_priv_area)1754 static int update_comms_stats(sdla_t* card,
1755 	chdlc_private_area_t* chdlc_priv_area)
1756 {
1757         CHDLC_MAILBOX_STRUCT* mb = card->mbox;
1758   	COMMS_ERROR_STATS_STRUCT* err_stats;
1759         CHDLC_OPERATIONAL_STATS_STRUCT *op_stats;
1760 
1761 	/* on the first timer interrupt, read the comms error statistics */
1762 	if(chdlc_priv_area->update_comms_stats == 2) {
1763 		if(chdlc_read_comm_err_stats(card))
1764 			return 1;
1765 		err_stats = (COMMS_ERROR_STATS_STRUCT *)mb->data;
1766 		card->wandev.stats.rx_over_errors =
1767 				err_stats->Rx_overrun_err_count;
1768 		card->wandev.stats.rx_crc_errors =
1769 				err_stats->CRC_err_count;
1770 		card->wandev.stats.rx_frame_errors =
1771 				err_stats->Rx_abort_count;
1772 		card->wandev.stats.rx_fifo_errors =
1773 				err_stats->Rx_dis_pri_bfrs_full_count;
1774 		card->wandev.stats.rx_missed_errors =
1775 				card->wandev.stats.rx_fifo_errors;
1776 		card->wandev.stats.tx_aborted_errors =
1777 				err_stats->sec_Tx_abort_count;
1778 	}
1779 
1780         /* on the second timer interrupt, read the operational statistics */
1781 	else {
1782         	if(chdlc_read_op_stats(card))
1783                 	return 1;
1784 		op_stats = (CHDLC_OPERATIONAL_STATS_STRUCT *)mb->data;
1785 		card->wandev.stats.rx_length_errors =
1786 			(op_stats->Rx_Data_discard_short_count +
1787 			op_stats->Rx_Data_discard_long_count);
1788 	}
1789 
1790 	return 0;
1791 }
1792 
1793 /*============================================================================
1794  * Send packet.
1795  *	Return:	0 - o.k.
1796  *		1 - no transmit buffers available
1797  */
chdlc_send(sdla_t * card,void * data,unsigned len)1798 static int chdlc_send (sdla_t* card, void* data, unsigned len)
1799 {
1800 	CHDLC_DATA_TX_STATUS_EL_STRUCT *txbuf = card->u.c.txbuf;
1801 
1802 	if (txbuf->opp_flag)
1803 		return 1;
1804 
1805 	sdla_poke(&card->hw, txbuf->ptr_data_bfr, data, len);
1806 
1807 	txbuf->frame_length = len;
1808 	txbuf->opp_flag = 1;		/* start transmission */
1809 
1810 	/* Update transmit buffer control fields */
1811 	card->u.c.txbuf = ++txbuf;
1812 
1813 	if ((void*)txbuf > card->u.c.txbuf_last)
1814 		card->u.c.txbuf = card->u.c.txbuf_base;
1815 
1816 	return 0;
1817 }
1818 
1819 /****** Firmware Error Handler **********************************************/
1820 
1821 /*============================================================================
1822  * Firmware error handler.
1823  *	This routine is called whenever firmware command returns non-zero
1824  *	return code.
1825  *
1826  * Return zero if previous command has to be cancelled.
1827  */
chdlc_error(sdla_t * card,int err,CHDLC_MAILBOX_STRUCT * mb)1828 static int chdlc_error (sdla_t *card, int err, CHDLC_MAILBOX_STRUCT *mb)
1829 {
1830 	unsigned cmd = mb->command;
1831 
1832 	switch (err) {
1833 
1834 	case CMD_TIMEOUT:
1835 		printk(KERN_INFO "%s: command 0x%02X timed out!\n",
1836 			card->devname, cmd);
1837 		break;
1838 
1839 	case S514_BOTH_PORTS_SAME_CLK_MODE:
1840 		if(cmd == SET_CHDLC_CONFIGURATION) {
1841 			printk(KERN_INFO
1842 			 "%s: Configure both ports for the same clock source\n",
1843 				card->devname);
1844 			break;
1845 		}
1846 
1847 	default:
1848 		printk(KERN_INFO "%s: command 0x%02X returned 0x%02X!\n",
1849 			card->devname, cmd, err);
1850 	}
1851 
1852 	return 0;
1853 }
1854 
1855 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1856 /********** Bottom Half Handlers ********************************************/
1857 
1858 /* NOTE: There is no API, BH support for Kernels lower than 2.2.X.
1859  *       DO NOT INSERT ANY CODE HERE, NOTICE THE
1860  *       PREPROCESSOR STATEMENT ABOVE, UNLESS YOU KNOW WHAT YOU ARE
1861  *       DOING */
1862 
chdlc_bh(netdevice_t * dev)1863 static void chdlc_bh (netdevice_t * dev)
1864 {
1865 	chdlc_private_area_t* chan = dev->priv;
1866 	sdla_t *card = chan->card;
1867 	struct sk_buff *skb;
1868 
1869 	if (atomic_read(&chan->bh_buff_used) == 0){
1870 		clear_bit(0, &chan->tq_working);
1871 		return;
1872 	}
1873 
1874 	while (atomic_read(&chan->bh_buff_used)){
1875 
1876 		skb  = ((bh_data_t *)&chan->bh_head[chan->bh_read])->skb;
1877 
1878 		if (skb != NULL){
1879 
1880 			if (chan->common.sk == NULL || chan->common.func == NULL){
1881 				++card->wandev.stats.rx_dropped;
1882 				wan_dev_kfree_skb(skb, FREE_READ);
1883 				chdlc_bh_cleanup(dev);
1884 				continue;
1885 			}
1886 
1887 			if (chan->common.func(skb,dev,chan->common.sk) != 0){
1888 				/* Sock full cannot send, queue us for another
1889                                  * try */
1890 				atomic_set(&chan->common.receive_block,1);
1891 				return;
1892 			}else{
1893 				chdlc_bh_cleanup(dev);
1894 			}
1895 		}else{
1896 			chdlc_bh_cleanup(dev);
1897 		}
1898 	}
1899 	clear_bit(0, &chan->tq_working);
1900 
1901 	return;
1902 }
1903 
chdlc_bh_cleanup(netdevice_t * dev)1904 static int chdlc_bh_cleanup (netdevice_t *dev)
1905 {
1906 	chdlc_private_area_t* chan = dev->priv;
1907 
1908 	((bh_data_t *)&chan->bh_head[chan->bh_read])->skb = NULL;
1909 
1910 	if (chan->bh_read == MAX_BH_BUFF){
1911 		chan->bh_read=0;
1912 	}else{
1913 		++chan->bh_read;
1914 	}
1915 
1916 	atomic_dec(&chan->bh_buff_used);
1917 	return 0;
1918 }
1919 
1920 
1921 
bh_enqueue(netdevice_t * dev,struct sk_buff * skb)1922 static int bh_enqueue (netdevice_t *dev, struct sk_buff *skb)
1923 {
1924 	/* Check for full */
1925 	chdlc_private_area_t* chan = dev->priv;
1926 	sdla_t *card = chan->card;
1927 
1928 	if (atomic_read(&chan->bh_buff_used) == (MAX_BH_BUFF+1)){
1929 		++card->wandev.stats.rx_dropped;
1930 		wan_dev_kfree_skb(skb, FREE_READ);
1931 		return 1;
1932 	}
1933 
1934 	((bh_data_t *)&chan->bh_head[chan->bh_write])->skb = skb;
1935 
1936 	if (chan->bh_write == MAX_BH_BUFF){
1937 		chan->bh_write=0;
1938 	}else{
1939 		++chan->bh_write;
1940 	}
1941 
1942 	atomic_inc(&chan->bh_buff_used);
1943 
1944 	return 0;
1945 }
1946 
1947 /* END OF API BH Support */
1948 
1949 #endif
1950 
1951 /****** Interrupt Handlers **************************************************/
1952 
1953 /*============================================================================
1954  * Cisco HDLC interrupt service routine.
1955  */
wpc_isr(sdla_t * card)1956 static void wpc_isr (sdla_t* card)
1957 {
1958 	netdevice_t* dev;
1959 	SHARED_MEMORY_INFO_STRUCT* flags = NULL;
1960 	int i;
1961 	sdla_t *my_card;
1962 
1963 
1964 	/* Check for which port the interrupt has been generated
1965 	 * Since Secondary Port is piggybacking on the Primary
1966          * the check must be done here.
1967 	 */
1968 
1969 	flags = card->u.c.flags;
1970 	if (!flags->interrupt_info_struct.interrupt_type){
1971 		/* Check for a second port (piggybacking) */
1972 		if ((my_card = card->next)){
1973 			flags = my_card->u.c.flags;
1974 			if (flags->interrupt_info_struct.interrupt_type){
1975 				card = my_card;
1976 				card->isr(card);
1977 				return;
1978 			}
1979 		}
1980 	}
1981 
1982 	flags = card->u.c.flags;
1983 	card->in_isr = 1;
1984 	dev = card->wandev.dev;
1985 
1986 	/* If we get an interrupt with no network device, stop the interrupts
1987 	 * and issue an error */
1988 	if (!card->tty_opt && !dev &&
1989 	    flags->interrupt_info_struct.interrupt_type !=
1990 	    	COMMAND_COMPLETE_APP_INT_PEND){
1991 
1992 		goto isr_done;
1993 	}
1994 
1995 	/* if critical due to peripheral operations
1996 	 * ie. update() or getstats() then reset the interrupt and
1997 	 * wait for the board to retrigger.
1998 	 */
1999 	if(test_bit(PERI_CRIT, (void*)&card->wandev.critical)) {
2000 		printk(KERN_INFO "ISR CRIT TO PERI\n");
2001 		goto isr_done;
2002 	}
2003 
2004 	/* On a 508 Card, if critical due to if_send
2005          * Major Error !!! */
2006 	if(card->hw.type != SDLA_S514) {
2007 		if(test_bit(SEND_CRIT, (void*)&card->wandev.critical)) {
2008 			printk(KERN_INFO "%s: Critical while in ISR: %lx\n",
2009 				card->devname, card->wandev.critical);
2010 			card->in_isr = 0;
2011 			flags->interrupt_info_struct.interrupt_type = 0;
2012 			return;
2013 		}
2014 	}
2015 
2016 	switch(flags->interrupt_info_struct.interrupt_type) {
2017 
2018 	case RX_APP_INT_PEND:	/* 0x01: receive interrupt */
2019 		rx_intr(card);
2020 		break;
2021 
2022 	case TX_APP_INT_PEND:	/* 0x02: transmit interrupt */
2023 		flags->interrupt_info_struct.interrupt_permission &=
2024 			 ~APP_INT_ON_TX_FRAME;
2025 
2026 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2027 
2028 		if (card->tty_opt){
2029 			wanpipe_tty_trigger_poll(card);
2030 			break;
2031 		}
2032 
2033 		if (dev && is_queue_stopped(dev)){
2034 			if (card->u.c.usedby == API){
2035 				start_net_queue(dev);
2036 				wakeup_sk_bh(dev);
2037 			}else{
2038 				wake_net_dev(dev);
2039 			}
2040 		}
2041 #else
2042 		wake_net_dev(dev);
2043 #endif
2044 		break;
2045 
2046 	case COMMAND_COMPLETE_APP_INT_PEND:/* 0x04: cmd cplt */
2047 		++ Intr_test_counter;
2048 		break;
2049 
2050 	case CHDLC_EXCEP_COND_APP_INT_PEND:	/* 0x20 */
2051 		process_chdlc_exception(card);
2052 		break;
2053 
2054 	case GLOBAL_EXCEP_COND_APP_INT_PEND:
2055 		process_global_exception(card);
2056 		break;
2057 
2058 	case TIMER_APP_INT_PEND:
2059 		timer_intr(card);
2060 		break;
2061 
2062 	default:
2063 		printk(KERN_INFO "%s: spurious interrupt 0x%02X!\n",
2064 			card->devname,
2065 			flags->interrupt_info_struct.interrupt_type);
2066 		printk(KERN_INFO "Code name: ");
2067 		for(i = 0; i < 4; i ++)
2068 			printk(KERN_INFO "%c",
2069 				flags->global_info_struct.codename[i]);
2070 		printk(KERN_INFO "\nCode version: ");
2071 	 	for(i = 0; i < 4; i ++)
2072 			printk(KERN_INFO "%c",
2073 				flags->global_info_struct.codeversion[i]);
2074 		printk(KERN_INFO "\n");
2075 		break;
2076 	}
2077 
2078 isr_done:
2079 
2080 	card->in_isr = 0;
2081 	flags->interrupt_info_struct.interrupt_type = 0;
2082 	return;
2083 }
2084 
2085 /*============================================================================
2086  * Receive interrupt handler.
2087  */
rx_intr(sdla_t * card)2088 static void rx_intr (sdla_t* card)
2089 {
2090 	netdevice_t *dev;
2091 	chdlc_private_area_t *chdlc_priv_area;
2092 	SHARED_MEMORY_INFO_STRUCT *flags = card->u.c.flags;
2093 	CHDLC_DATA_RX_STATUS_EL_STRUCT *rxbuf = card->u.c.rxmb;
2094 	struct sk_buff *skb;
2095 	unsigned len;
2096 	unsigned addr = rxbuf->ptr_data_bfr;
2097 	void *buf;
2098 	int i,udp_type;
2099 
2100 	if (rxbuf->opp_flag != 0x01) {
2101 		printk(KERN_INFO
2102 			"%s: corrupted Rx buffer @ 0x%X, flag = 0x%02X!\n",
2103 			card->devname, (unsigned)rxbuf, rxbuf->opp_flag);
2104                 printk(KERN_INFO "Code name: ");
2105                 for(i = 0; i < 4; i ++)
2106                         printk(KERN_INFO "%c",
2107                                 flags->global_info_struct.codename[i]);
2108                 printk(KERN_INFO "\nCode version: ");
2109                 for(i = 0; i < 4; i ++)
2110                         printk(KERN_INFO "%c",
2111                                 flags->global_info_struct.codeversion[i]);
2112                 printk(KERN_INFO "\n");
2113 
2114 
2115 		/* Bug Fix: Mar 6 2000
2116                  * If we get a corrupted mailbox, it measn that driver
2117                  * is out of sync with the firmware. There is no recovery.
2118                  * If we don't turn off all interrupts for this card
2119                  * the machine will crash.
2120                  */
2121 		printk(KERN_INFO "%s: Critical router failure ...!!!\n", card->devname);
2122 		printk(KERN_INFO "Please contact Sangoma Technologies !\n");
2123 		chdlc_set_intr_mode(card,0);
2124 		return;
2125 	}
2126 
2127 	len  = rxbuf->frame_length;
2128 
2129 #if defined(LINUX_2_4) || defined(LINUX_2_1)
2130 	if (card->tty_opt){
2131 
2132 		if (rxbuf->error_flag){
2133 			goto rx_exit;
2134 		}
2135 
2136 		if (len <= CRC_LENGTH){
2137 			goto rx_exit;
2138 		}
2139 
2140 		if (!card->u.c.async_mode){
2141 			len -= CRC_LENGTH;
2142 		}
2143 
2144 		wanpipe_tty_receive(card,addr,len);
2145 		goto rx_exit;
2146 	}
2147 #endif
2148 
2149 	dev = card->wandev.dev;
2150 
2151 	if (!dev){
2152 		goto rx_exit;
2153 	}
2154 
2155 	if (!is_dev_running(dev))
2156 		goto rx_exit;
2157 
2158 	chdlc_priv_area = dev->priv;
2159 
2160 
2161 	/* Allocate socket buffer */
2162 	skb = dev_alloc_skb(len);
2163 
2164 	if (skb == NULL) {
2165 		printk(KERN_INFO "%s: no socket buffers available!\n",
2166 					card->devname);
2167 		++card->wandev.stats.rx_dropped;
2168 		goto rx_exit;
2169 	}
2170 
2171 	/* Copy data to the socket buffer */
2172 	if((addr + len) > card->u.c.rx_top + 1) {
2173 		unsigned tmp = card->u.c.rx_top - addr + 1;
2174 		buf = skb_put(skb, tmp);
2175 		sdla_peek(&card->hw, addr, buf, tmp);
2176 		addr = card->u.c.rx_base;
2177 		len -= tmp;
2178 	}
2179 
2180 	buf = skb_put(skb, len);
2181 	sdla_peek(&card->hw, addr, buf, len);
2182 
2183 	skb->protocol = htons(ETH_P_IP);
2184 
2185 	card->wandev.stats.rx_packets ++;
2186 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2187 	card->wandev.stats.rx_bytes += skb->len;
2188 #endif
2189 	udp_type = udp_pkt_type( skb, card );
2190 
2191 	if(udp_type == UDP_CPIPE_TYPE) {
2192 		if(store_udp_mgmt_pkt(UDP_PKT_FRM_NETWORK,
2193    				      card, skb, dev, chdlc_priv_area)) {
2194      		        flags->interrupt_info_struct.
2195 						interrupt_permission |=
2196 							APP_INT_ON_TIMER;
2197 		}
2198 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2199 	} else if(card->u.c.usedby == API) {
2200 
2201 		api_rx_hdr_t* api_rx_hdr;
2202        		skb_push(skb, sizeof(api_rx_hdr_t));
2203                 api_rx_hdr = (api_rx_hdr_t*)&skb->data[0x00];
2204 		api_rx_hdr->error_flag = rxbuf->error_flag;
2205      		api_rx_hdr->time_stamp = rxbuf->time_stamp;
2206 
2207                 skb->protocol = htons(PVC_PROT);
2208      		skb->mac.raw  = skb->data;
2209 		skb->dev      = dev;
2210                	skb->pkt_type = WAN_PACKET_DATA;
2211 
2212 		bh_enqueue(dev, skb);
2213 
2214 		if (!test_and_set_bit(0,&chdlc_priv_area->tq_working)){
2215 			wanpipe_queue_tq(&chdlc_priv_area->common.wanpipe_task);
2216 		        wanpipe_mark_bh();
2217 		}
2218 #endif
2219 	}else{
2220 		/* FIXME: we should check to see if the received packet is a
2221                           multicast packet so that we can increment the multicast
2222                           statistic
2223                           ++ chdlc_priv_area->if_stats.multicast;
2224 		*/
2225                	/* Pass it up the protocol stack */
2226 
2227                 skb->dev = dev;
2228                 skb->mac.raw  = skb->data;
2229                 netif_rx(skb);
2230 	}
2231 
2232 rx_exit:
2233 	/* Release buffer element and calculate a pointer to the next one */
2234 	rxbuf->opp_flag = 0x00;
2235 	card->u.c.rxmb = ++ rxbuf;
2236 	if((void*)rxbuf > card->u.c.rxbuf_last){
2237 		card->u.c.rxmb = card->u.c.rxbuf_base;
2238 	}
2239 }
2240 
2241 /*============================================================================
2242  * Timer interrupt handler.
2243  * The timer interrupt is used for two purposes:
2244  *    1) Processing udp calls from 'cpipemon'.
2245  *    2) Reading board-level statistics for updating the proc file system.
2246  */
timer_intr(sdla_t * card)2247 void timer_intr(sdla_t *card)
2248 {
2249         netdevice_t* dev;
2250         chdlc_private_area_t* chdlc_priv_area = NULL;
2251         SHARED_MEMORY_INFO_STRUCT* flags = NULL;
2252 
2253         if ((dev = card->wandev.dev)==NULL){
2254 		flags = card->u.c.flags;
2255                 flags->interrupt_info_struct.interrupt_permission &=
2256                         ~APP_INT_ON_TIMER;
2257 		return;
2258 	}
2259 
2260         chdlc_priv_area = dev->priv;
2261 
2262 	if (chdlc_priv_area->timer_int_enabled & TMR_INT_ENABLED_CONFIG) {
2263 		if (!config_chdlc(card)){
2264 			chdlc_priv_area->timer_int_enabled &= ~TMR_INT_ENABLED_CONFIG;
2265 		}
2266 	}
2267 
2268 	/* process a udp call if pending */
2269        	if(chdlc_priv_area->timer_int_enabled & TMR_INT_ENABLED_UDP) {
2270                	process_udp_mgmt_pkt(card, dev,
2271                        chdlc_priv_area);
2272 		chdlc_priv_area->timer_int_enabled &= ~TMR_INT_ENABLED_UDP;
2273         }
2274 
2275 	/* read the communications statistics if required */
2276 	if(chdlc_priv_area->timer_int_enabled & TMR_INT_ENABLED_UPDATE) {
2277 		update_comms_stats(card, chdlc_priv_area);
2278                 if(!(-- chdlc_priv_area->update_comms_stats)) {
2279 			chdlc_priv_area->timer_int_enabled &=
2280 				~TMR_INT_ENABLED_UPDATE;
2281 		}
2282         }
2283 
2284 	/* only disable the timer interrupt if there are no udp or statistic */
2285 	/* updates pending */
2286         if(!chdlc_priv_area->timer_int_enabled) {
2287                 flags = card->u.c.flags;
2288                 flags->interrupt_info_struct.interrupt_permission &=
2289                         ~APP_INT_ON_TIMER;
2290         }
2291 }
2292 
2293 /*------------------------------------------------------------------------------
2294   Miscellaneous Functions
2295 	- set_chdlc_config() used to set configuration options on the board
2296 ------------------------------------------------------------------------------*/
2297 
set_chdlc_config(sdla_t * card)2298 static int set_chdlc_config(sdla_t* card)
2299 {
2300 	CHDLC_CONFIGURATION_STRUCT cfg;
2301 
2302 	memset(&cfg, 0, sizeof(CHDLC_CONFIGURATION_STRUCT));
2303 
2304 	if(card->wandev.clocking){
2305 		cfg.baud_rate = card->wandev.bps;
2306 	}
2307 
2308 	cfg.line_config_options = (card->wandev.interface == WANOPT_RS232) ?
2309 		INTERFACE_LEVEL_RS232 : INTERFACE_LEVEL_V35;
2310 
2311 	cfg.modem_config_options	= 0;
2312 	cfg.modem_status_timer		= 100;
2313 
2314 	cfg.CHDLC_protocol_options	= card->u.c.protocol_options;
2315 
2316 	if (card->tty_opt){
2317 		cfg.CHDLC_API_options	= DISCARD_RX_ERROR_FRAMES;
2318 	}
2319 
2320 	cfg.percent_data_buffer_for_Tx  = (card->u.c.receive_only) ? 0 : 50;
2321 	cfg.CHDLC_statistics_options	= (CHDLC_TX_DATA_BYTE_COUNT_STAT |
2322 		CHDLC_RX_DATA_BYTE_COUNT_STAT);
2323 
2324 	if (card->tty_opt){
2325 		card->wandev.mtu = TTY_CHDLC_MAX_MTU;
2326 	}
2327 	cfg.max_CHDLC_data_field_length	= card->wandev.mtu;
2328 	cfg.transmit_keepalive_timer	= card->u.c.kpalv_tx;
2329 	cfg.receive_keepalive_timer	= card->u.c.kpalv_rx;
2330 	cfg.keepalive_error_tolerance	= card->u.c.kpalv_err;
2331 	cfg.SLARP_request_timer		= card->u.c.slarp_timer;
2332 
2333 	if (cfg.SLARP_request_timer) {
2334 		cfg.IP_address		= 0;
2335 		cfg.IP_netmask		= 0;
2336 
2337 	}else if (card->wandev.dev){
2338 		netdevice_t * dev = card->wandev.dev;
2339 		chdlc_private_area_t *chdlc_priv_area = dev->priv;
2340 
2341 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2342                 struct in_device *in_dev = dev->ip_ptr;
2343 
2344 		if(in_dev != NULL) {
2345 			struct in_ifaddr *ifa = in_dev->ifa_list;
2346 
2347 			if (ifa != NULL ) {
2348 				cfg.IP_address	= ntohl(ifa->ifa_local);
2349 				cfg.IP_netmask	= ntohl(ifa->ifa_mask);
2350 				chdlc_priv_area->IP_address = ntohl(ifa->ifa_local);
2351 				chdlc_priv_area->IP_netmask = ntohl(ifa->ifa_mask);
2352 			}
2353 		}
2354 #else
2355                 cfg.IP_address          = ntohl(dev->pa_addr);
2356                 cfg.IP_netmask          = ntohl(dev->pa_mask);
2357 		chdlc_priv_area->IP_address = ntohl(dev->pa_addr);
2358 		chdlc_priv_area->IP_netmask = ntohl(dev->pa_mask);
2359 #endif
2360 
2361 		/* FIXME: We must re-think this message in next release
2362 		if((cfg.IP_address & 0x000000FF) > 2) {
2363 			printk(KERN_WARNING "\n");
2364 	                printk(KERN_WARNING "  WARNING:%s configured with an\n",
2365 				card->devname);
2366 			printk(KERN_WARNING "  invalid local IP address.\n");
2367                         printk(KERN_WARNING "  Slarp pragmatics will fail.\n");
2368                         printk(KERN_WARNING "  IP address should be of the\n");
2369 			printk(KERN_WARNING "  format A.B.C.1 or A.B.C.2.\n");
2370 		}
2371 		*/
2372 	}
2373 
2374 	return chdlc_configure(card, &cfg);
2375 }
2376 
2377 
2378 /*-----------------------------------------------------------------------------
2379    set_asy_config() used to set asynchronous configuration options on the board
2380 ------------------------------------------------------------------------------*/
2381 
set_asy_config(sdla_t * card)2382 static int set_asy_config(sdla_t* card)
2383 {
2384 
2385         ASY_CONFIGURATION_STRUCT cfg;
2386  	CHDLC_MAILBOX_STRUCT *mailbox = card->mbox;
2387 	int err;
2388 
2389 	memset(&cfg, 0, sizeof(ASY_CONFIGURATION_STRUCT));
2390 
2391 	if(card->wandev.clocking)
2392 		cfg.baud_rate = card->wandev.bps;
2393 
2394 	cfg.line_config_options = (card->wandev.interface == WANOPT_RS232) ?
2395 		INTERFACE_LEVEL_RS232 : INTERFACE_LEVEL_V35;
2396 
2397 	cfg.modem_config_options	= 0;
2398 	cfg.asy_API_options 		= card->u.c.api_options;
2399 	cfg.asy_protocol_options	= card->u.c.protocol_options;
2400 	cfg.Tx_bits_per_char		= card->u.c.tx_bits_per_char;
2401 	cfg.Rx_bits_per_char		= card->u.c.rx_bits_per_char;
2402 	cfg.stop_bits			= card->u.c.stop_bits;
2403 	cfg.parity			= card->u.c.parity;
2404 	cfg.break_timer			= card->u.c.break_timer;
2405 	cfg.asy_Rx_inter_char_timer	= card->u.c.inter_char_timer;
2406 	cfg.asy_Rx_complete_length	= card->u.c.rx_complete_length;
2407 	cfg.XON_char			= card->u.c.xon_char;
2408 	cfg.XOFF_char			= card->u.c.xoff_char;
2409 	cfg.asy_statistics_options	= (CHDLC_TX_DATA_BYTE_COUNT_STAT |
2410 		CHDLC_RX_DATA_BYTE_COUNT_STAT);
2411 
2412 	mailbox->buffer_length = sizeof(ASY_CONFIGURATION_STRUCT);
2413 	memcpy(mailbox->data, &cfg, mailbox->buffer_length);
2414 	mailbox->command = SET_ASY_CONFIGURATION;
2415 	err = sdla_exec(mailbox) ? mailbox->return_code : CMD_TIMEOUT;
2416 	if (err != COMMAND_OK)
2417 		chdlc_error (card, err, mailbox);
2418 	return err;
2419 }
2420 
2421 /*============================================================================
2422  * Enable asynchronous communications.
2423  */
2424 
asy_comm_enable(sdla_t * card)2425 static int asy_comm_enable (sdla_t* card)
2426 {
2427 
2428 	int err;
2429 	CHDLC_MAILBOX_STRUCT* mb = card->mbox;
2430 
2431 	mb->buffer_length = 0;
2432 	mb->command = ENABLE_ASY_COMMUNICATIONS;
2433 	err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
2434 	if (err != COMMAND_OK && card->wandev.dev)
2435 		chdlc_error(card, err, mb);
2436 
2437 	if (!err)
2438 		card->u.c.comm_enabled = 1;
2439 
2440 	return err;
2441 }
2442 
2443 /*============================================================================
2444  * Process global exception condition
2445  */
process_global_exception(sdla_t * card)2446 static int process_global_exception(sdla_t *card)
2447 {
2448 	CHDLC_MAILBOX_STRUCT* mbox = card->mbox;
2449 	int err;
2450 
2451 	mbox->buffer_length = 0;
2452 	mbox->command = READ_GLOBAL_EXCEPTION_CONDITION;
2453 	err = sdla_exec(mbox) ? mbox->return_code : CMD_TIMEOUT;
2454 
2455 	if(err != CMD_TIMEOUT ){
2456 
2457 		switch(mbox->return_code) {
2458 
2459 	      	case EXCEP_MODEM_STATUS_CHANGE:
2460 
2461 			printk(KERN_INFO "%s: Modem status change\n",
2462 				card->devname);
2463 
2464 			switch(mbox->data[0] & (DCD_HIGH | CTS_HIGH)) {
2465 				case (DCD_HIGH):
2466 					printk(KERN_INFO "%s: DCD high, CTS low\n",card->devname);
2467 					break;
2468 				case (CTS_HIGH):
2469                                         printk(KERN_INFO "%s: DCD low, CTS high\n",card->devname);
2470 					break;
2471                                 case ((DCD_HIGH | CTS_HIGH)):
2472                                         printk(KERN_INFO "%s: DCD high, CTS high\n",card->devname);
2473                                         break;
2474 				default:
2475                                         printk(KERN_INFO "%s: DCD low, CTS low\n",card->devname);
2476                                         break;
2477 			}
2478 			break;
2479 
2480                 case EXCEP_TRC_DISABLED:
2481                         printk(KERN_INFO "%s: Line trace disabled\n",
2482 				card->devname);
2483                         break;
2484 
2485 		case EXCEP_IRQ_TIMEOUT:
2486 			printk(KERN_INFO "%s: IRQ timeout occurred\n",
2487 				card->devname);
2488 			break;
2489 
2490 		case 0x17:
2491 			if (card->tty_opt){
2492 				if (card->tty && card->tty_open){
2493 					printk(KERN_INFO
2494 						"%s: Modem Hangup Exception: Hanging Up!\n",
2495 						card->devname);
2496 					tty_hangup(card->tty);
2497 				}
2498 				break;
2499 			}
2500 
2501 			/* If TTY is not used just drop throught */
2502 
2503                 default:
2504                         printk(KERN_INFO "%s: Global exception %x\n",
2505 				card->devname, mbox->return_code);
2506                         break;
2507                 }
2508 	}
2509 	return 0;
2510 }
2511 
2512 
2513 /*============================================================================
2514  * Process chdlc exception condition
2515  */
process_chdlc_exception(sdla_t * card)2516 static int process_chdlc_exception(sdla_t *card)
2517 {
2518 	CHDLC_MAILBOX_STRUCT* mb = card->mbox;
2519 	int err;
2520 
2521 	mb->buffer_length = 0;
2522 	mb->command = READ_CHDLC_EXCEPTION_CONDITION;
2523 	err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
2524 	if(err != CMD_TIMEOUT) {
2525 
2526 		switch (err) {
2527 
2528 		case EXCEP_LINK_ACTIVE:
2529 			port_set_state(card, WAN_CONNECTED);
2530 			trigger_chdlc_poll(card->wandev.dev);
2531 			break;
2532 
2533 		case EXCEP_LINK_INACTIVE_MODEM:
2534 			port_set_state(card, WAN_DISCONNECTED);
2535 			unconfigure_ip(card);
2536 			trigger_chdlc_poll(card->wandev.dev);
2537 			break;
2538 
2539 		case EXCEP_LINK_INACTIVE_KPALV:
2540 			port_set_state(card, WAN_DISCONNECTED);
2541 			printk(KERN_INFO "%s: Keepalive timer expired.\n",
2542 				 		card->devname);
2543 			unconfigure_ip(card);
2544 			trigger_chdlc_poll(card->wandev.dev);
2545 			break;
2546 
2547 		case EXCEP_IP_ADDRESS_DISCOVERED:
2548 			if (configure_ip(card))
2549 				return -1;
2550 			break;
2551 
2552 		case EXCEP_LOOPBACK_CONDITION:
2553 			printk(KERN_INFO "%s: Loopback Condition Detected.\n",
2554 						card->devname);
2555 			break;
2556 
2557 		case NO_CHDLC_EXCEP_COND_TO_REPORT:
2558 			printk(KERN_INFO "%s: No exceptions reported.\n",
2559 						card->devname);
2560 			break;
2561 		}
2562 
2563 	}
2564 	return 0;
2565 }
2566 
2567 
2568 /*============================================================================
2569  * Configure IP from SLARP negotiation
2570  * This adds dynamic routes when SLARP has provided valid addresses
2571  */
2572 
configure_ip(sdla_t * card)2573 static int configure_ip (sdla_t* card)
2574 {
2575 	netdevice_t *dev = card->wandev.dev;
2576         chdlc_private_area_t *chdlc_priv_area;
2577         char err;
2578 
2579 	if (!dev)
2580 		return 0;
2581 
2582 	chdlc_priv_area = dev->priv;
2583 
2584 
2585         /* set to discover */
2586         if(card->u.c.slarp_timer != 0x00) {
2587 		CHDLC_MAILBOX_STRUCT* mb = card->mbox;
2588 		CHDLC_CONFIGURATION_STRUCT *cfg;
2589 
2590      		mb->buffer_length = 0;
2591 		mb->command = READ_CHDLC_CONFIGURATION;
2592 		err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
2593 
2594 		if(err != COMMAND_OK) {
2595 			chdlc_error(card,err,mb);
2596 			return -1;
2597 		}
2598 
2599 		cfg = (CHDLC_CONFIGURATION_STRUCT *)mb->data;
2600                 chdlc_priv_area->IP_address = cfg->IP_address;
2601                 chdlc_priv_area->IP_netmask = cfg->IP_netmask;
2602 
2603 		/* Set flag to add route */
2604 		chdlc_priv_area->route_status = ADD_ROUTE;
2605 
2606 		/* The idea here is to add the route in the poll routine.
2607 	   	This way, we aren't in interrupt context when adding routes */
2608 		trigger_chdlc_poll(dev);
2609         }
2610 
2611 	return 0;
2612 }
2613 
2614 
2615 /*============================================================================
2616  * Un-Configure IP negotiated by SLARP
2617  * This removes dynamic routes when the link becomes inactive.
2618  */
2619 
unconfigure_ip(sdla_t * card)2620 static int unconfigure_ip (sdla_t* card)
2621 {
2622 	netdevice_t *dev = card->wandev.dev;
2623 	chdlc_private_area_t *chdlc_priv_area;
2624 
2625 	if (!dev)
2626 		return 0;
2627 
2628 	chdlc_priv_area= dev->priv;
2629 
2630 	if (chdlc_priv_area->route_status == ROUTE_ADDED) {
2631 
2632 		/* Note: If this function is called, the
2633                  * port state has been DISCONNECTED.  This state
2634                  * change will trigger a poll_disconnected
2635                  * function, that will check for this condition.
2636 		 */
2637 		chdlc_priv_area->route_status = REMOVE_ROUTE;
2638 
2639 	}
2640 	return 0;
2641 }
2642 
2643 /*============================================================================
2644  * Routine to add/remove routes
2645  * Called like a polling routine when Routes are flagged to be added/removed.
2646  */
2647 
process_route(sdla_t * card)2648 static void process_route (sdla_t *card)
2649 {
2650         netdevice_t *dev = card->wandev.dev;
2651         unsigned char port_num;
2652         chdlc_private_area_t *chdlc_priv_area = NULL;
2653 	u32 local_IP_addr = 0;
2654 	u32 remote_IP_addr = 0;
2655 	u32 IP_netmask, IP_addr;
2656         int err = 0;
2657 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2658 	struct in_device *in_dev;
2659 	mm_segment_t fs;
2660 	struct ifreq if_info;
2661         struct sockaddr_in *if_data1, *if_data2;
2662 #else
2663 	unsigned long fs = 0;
2664         struct rtentry route;
2665 #endif
2666 
2667         chdlc_priv_area = dev->priv;
2668         port_num = card->u.c.comm_port;
2669 
2670 	/* Bug Fix Mar 16 2000
2671 	 * AND the IP address to the Mask before checking
2672          * the last two bits. */
2673 
2674 	if((chdlc_priv_area->route_status == ADD_ROUTE) &&
2675 		((chdlc_priv_area->IP_address & ~chdlc_priv_area->IP_netmask) > 2)) {
2676 
2677 		printk(KERN_INFO "%s: Dynamic route failure.\n",card->devname);
2678 
2679                 if(card->u.c.slarp_timer) {
2680 			u32 addr_net = htonl(chdlc_priv_area->IP_address);
2681 
2682 			printk(KERN_INFO "%s: Bad IP address %u.%u.%u.%u received\n",
2683 				card->devname,
2684 			       NIPQUAD(addr_net));
2685                         printk(KERN_INFO "%s: from remote station.\n",
2686 				card->devname);
2687 
2688                 }else{
2689 			u32 addr_net = htonl(chdlc_priv_area->IP_address);
2690 
2691                         printk(KERN_INFO "%s: Bad IP address %u.%u.%u.%u issued\n",
2692 			       card->devname,
2693 			       NIPQUAD(addr_net));
2694                         printk(KERN_INFO "%s: to remote station. Local\n",
2695 				card->devname);
2696 			printk(KERN_INFO "%s: IP address must be A.B.C.1\n",
2697 				card->devname);
2698 			printk(KERN_INFO "%s: or A.B.C.2.\n",card->devname);
2699 		}
2700 
2701 		/* remove the route due to the IP address error condition */
2702 		chdlc_priv_area->route_status = REMOVE_ROUTE;
2703 		err = 1;
2704    	}
2705 
2706 	/* If we are removing a route with bad IP addressing, then use the */
2707 	/* locally configured IP addresses */
2708         if((chdlc_priv_area->route_status == REMOVE_ROUTE) && err) {
2709 
2710  	        /* do not remove a bad route that has already been removed */
2711         	if(chdlc_priv_area->route_removed) {
2712 	                return;
2713         	}
2714 
2715 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2716                 in_dev = dev->ip_ptr;
2717 
2718                 if(in_dev != NULL) {
2719                         struct in_ifaddr *ifa = in_dev->ifa_list;
2720                         if (ifa != NULL ) {
2721                                 local_IP_addr = ifa->ifa_local;
2722                                 IP_netmask  = ifa->ifa_mask;
2723                         }
2724                 }
2725 #else
2726                 local_IP_addr = dev->pa_addr;
2727                 remote_IP_addr = dev->pa_dstaddr;
2728                 IP_netmask = dev->pa_mask;
2729 #endif
2730 	}else{
2731        		/* According to Cisco HDLC, if the point-to-point address is
2732 		   A.B.C.1, then we are the opposite (A.B.C.2), and vice-versa.
2733 		*/
2734 		IP_netmask = ntohl(chdlc_priv_area->IP_netmask);
2735 	        remote_IP_addr = ntohl(chdlc_priv_area->IP_address);
2736 
2737 
2738 		/* If Netmask is 255.255.255.255 the local address
2739                  * calculation will fail. Default it back to 255.255.255.0 */
2740 		if (IP_netmask == 0xffffffff)
2741 			IP_netmask &= 0x00ffffff;
2742 
2743 		/* Bug Fix Mar 16 2000
2744 		 * AND the Remote IP address with IP netmask, instead
2745                  * of static netmask of 255.255.255.0 */
2746         	local_IP_addr = (remote_IP_addr & IP_netmask) +
2747                 	(~remote_IP_addr & ntohl(0x0003));
2748 
2749 	        if(!card->u.c.slarp_timer) {
2750 			IP_addr = local_IP_addr;
2751 			local_IP_addr = remote_IP_addr;
2752 			remote_IP_addr = IP_addr;
2753        		}
2754 	}
2755 
2756         fs = get_fs();                  /* Save file system  */
2757         set_fs(get_ds());               /* Get user space block */
2758 
2759 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2760         /* Setup a structure for adding/removing routes */
2761         memset(&if_info, 0, sizeof(if_info));
2762         strcpy(if_info.ifr_name, dev->name);
2763 
2764 #else
2765 	/* Setup a structure for adding/removing routes */
2766 	dev->pa_mask = IP_netmask;
2767 	dev->pa_dstaddr = remote_IP_addr;
2768 	dev->pa_addr = local_IP_addr;
2769 
2770 	memset(&route, 0, sizeof(route));
2771 	route.rt_dev = dev->name;
2772 	route.rt_flags = 0;
2773 	((struct sockaddr_in *)&(route.rt_dst))->sin_addr.s_addr =
2774 			dev->pa_dstaddr;
2775 	((struct sockaddr_in *)&(route.rt_dst))->sin_family = AF_INET;
2776 	((struct sockaddr_in *)&(route.rt_genmask))->sin_addr.s_addr =
2777 			0xFFFFFFFF;
2778         ((struct sockaddr_in *)&(route.rt_genmask))->sin_family =
2779 			AF_INET;
2780 #endif
2781 
2782 	switch (chdlc_priv_area->route_status) {
2783 
2784 	case ADD_ROUTE:
2785 
2786 		if(!card->u.c.slarp_timer) {
2787 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2788 			if_data2 = (struct sockaddr_in *)&if_info.ifr_dstaddr;
2789 			if_data2->sin_addr.s_addr = remote_IP_addr;
2790 			if_data2->sin_family = AF_INET;
2791 			err = devinet_ioctl(SIOCSIFDSTADDR, &if_info);
2792 #else
2793                         err = ip_rt_new(&route);
2794 #endif
2795 		} else {
2796 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2797 			if_data1 = (struct sockaddr_in *)&if_info.ifr_addr;
2798 			if_data1->sin_addr.s_addr = local_IP_addr;
2799 			if_data1->sin_family = AF_INET;
2800 			if(!(err = devinet_ioctl(SIOCSIFADDR, &if_info))){
2801 				if_data2 = (struct sockaddr_in *)&if_info.ifr_dstaddr;
2802 				if_data2->sin_addr.s_addr = remote_IP_addr;
2803 				if_data2->sin_family = AF_INET;
2804 				err = devinet_ioctl(SIOCSIFDSTADDR, &if_info);
2805 			}
2806 #else
2807                		err = ip_rt_new(&route);
2808 #endif
2809 		}
2810 
2811                if(err) {
2812 			printk(KERN_INFO "%s: Add route %u.%u.%u.%u failed (%d)\n",
2813 				card->devname, NIPQUAD(remote_IP_addr), err);
2814 		} else {
2815 			((chdlc_private_area_t *)dev->priv)->route_status = ROUTE_ADDED;
2816 			printk(KERN_INFO "%s: Dynamic route added.\n",
2817 				card->devname);
2818 			printk(KERN_INFO "%s:    Local IP addr : %u.%u.%u.%u\n",
2819 				card->devname, NIPQUAD(local_IP_addr));
2820 			printk(KERN_INFO "%s:    Remote IP addr: %u.%u.%u.%u\n",
2821 				card->devname, NIPQUAD(remote_IP_addr));
2822 			chdlc_priv_area->route_removed = 0;
2823 		}
2824 		break;
2825 
2826 
2827 	case REMOVE_ROUTE:
2828 
2829 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2830 		/* Change the local ip address of the interface to 0.
2831 		 * This will also delete the destination route.
2832 		 */
2833 		if(!card->u.c.slarp_timer) {
2834 			if_data2 = (struct sockaddr_in *)&if_info.ifr_dstaddr;
2835 			if_data2->sin_addr.s_addr = 0;
2836 			if_data2->sin_family = AF_INET;
2837 			err = devinet_ioctl(SIOCSIFDSTADDR, &if_info);
2838 		} else {
2839 			if_data1 = (struct sockaddr_in *)&if_info.ifr_addr;
2840 			if_data1->sin_addr.s_addr = 0;
2841 			if_data1->sin_family = AF_INET;
2842 			err = devinet_ioctl(SIOCSIFADDR,&if_info);
2843 
2844 		}
2845 #else
2846 		/* set the point-to-point IP address to 0.0.0.0 */
2847 		dev->pa_dstaddr = 0;
2848 		err = ip_rt_kill(&route);
2849 #endif
2850 		if(err) {
2851 			printk(KERN_INFO
2852 				"%s: Remove route %u.%u.%u.%u failed, (err %d)\n",
2853 					card->devname, NIPQUAD(remote_IP_addr),
2854 					err);
2855 		} else {
2856 			((chdlc_private_area_t *)dev->priv)->route_status =
2857 				NO_ROUTE;
2858                         printk(KERN_INFO "%s: Dynamic route removed: %u.%u.%u.%u\n",
2859                                         card->devname, NIPQUAD(local_IP_addr));
2860 			chdlc_priv_area->route_removed = 1;
2861 		}
2862 		break;
2863 	}
2864 
2865         set_fs(fs);                     /* Restore file system */
2866 
2867 }
2868 
2869 
2870 /*=============================================================================
2871  * Store a UDP management packet for later processing.
2872  */
2873 
store_udp_mgmt_pkt(char udp_pkt_src,sdla_t * card,struct sk_buff * skb,netdevice_t * dev,chdlc_private_area_t * chdlc_priv_area)2874 static int store_udp_mgmt_pkt(char udp_pkt_src, sdla_t* card,
2875                                 struct sk_buff *skb, netdevice_t* dev,
2876                                 chdlc_private_area_t* chdlc_priv_area )
2877 {
2878 	int udp_pkt_stored = 0;
2879 
2880 	if(!chdlc_priv_area->udp_pkt_lgth &&
2881 	  (skb->len <= MAX_LGTH_UDP_MGNT_PKT)) {
2882         	chdlc_priv_area->udp_pkt_lgth = skb->len;
2883 		chdlc_priv_area->udp_pkt_src = udp_pkt_src;
2884        		memcpy(chdlc_priv_area->udp_pkt_data, skb->data, skb->len);
2885 		chdlc_priv_area->timer_int_enabled = TMR_INT_ENABLED_UDP;
2886 		udp_pkt_stored = 1;
2887 	}
2888 
2889 	if(udp_pkt_src == UDP_PKT_FRM_STACK){
2890 		wan_dev_kfree_skb(skb, FREE_WRITE);
2891 	}else{
2892                 wan_dev_kfree_skb(skb, FREE_READ);
2893 	}
2894 
2895 	return(udp_pkt_stored);
2896 }
2897 
2898 
2899 /*=============================================================================
2900  * Process UDP management packet.
2901  */
2902 
process_udp_mgmt_pkt(sdla_t * card,netdevice_t * dev,chdlc_private_area_t * chdlc_priv_area)2903 static int process_udp_mgmt_pkt(sdla_t* card, netdevice_t* dev,
2904 				chdlc_private_area_t* chdlc_priv_area )
2905 {
2906 	unsigned char *buf;
2907 	unsigned int frames, len;
2908 	struct sk_buff *new_skb;
2909 	unsigned short buffer_length, real_len;
2910 	unsigned long data_ptr;
2911 	unsigned data_length;
2912 	int udp_mgmt_req_valid = 1;
2913 	CHDLC_MAILBOX_STRUCT *mb = card->mbox;
2914 	SHARED_MEMORY_INFO_STRUCT *flags = card->u.c.flags;
2915 	chdlc_udp_pkt_t *chdlc_udp_pkt;
2916 	struct timeval tv;
2917 	int err;
2918 	char ut_char;
2919 
2920 	chdlc_udp_pkt = (chdlc_udp_pkt_t *) chdlc_priv_area->udp_pkt_data;
2921 
2922 	if(chdlc_priv_area->udp_pkt_src == UDP_PKT_FRM_NETWORK){
2923 
2924 		/* Only these commands are support for remote debugging.
2925 		 * All others are not */
2926 		switch(chdlc_udp_pkt->cblock.command) {
2927 
2928 			case READ_GLOBAL_STATISTICS:
2929 			case READ_MODEM_STATUS:
2930 			case READ_CHDLC_LINK_STATUS:
2931 			case CPIPE_ROUTER_UP_TIME:
2932 			case READ_COMMS_ERROR_STATS:
2933 			case READ_CHDLC_OPERATIONAL_STATS:
2934 
2935 			/* These two commands are executed for
2936 			 * each request */
2937 			case READ_CHDLC_CONFIGURATION:
2938 			case READ_CHDLC_CODE_VERSION:
2939 				udp_mgmt_req_valid = 1;
2940 				break;
2941 			default:
2942 				udp_mgmt_req_valid = 0;
2943 				break;
2944 		}
2945 	}
2946 
2947   	if(!udp_mgmt_req_valid) {
2948 
2949 		/* set length to 0 */
2950 		chdlc_udp_pkt->cblock.buffer_length = 0;
2951 
2952     		/* set return code */
2953 		chdlc_udp_pkt->cblock.return_code = 0xCD;
2954 
2955 		if (net_ratelimit()){
2956 			printk(KERN_INFO
2957 			"%s: Warning, Illegal UDP command attempted from network: %x\n",
2958 			card->devname,chdlc_udp_pkt->cblock.command);
2959 		}
2960 
2961    	} else {
2962 	   	unsigned long trace_status_cfg_addr = 0;
2963 		TRACE_STATUS_EL_CFG_STRUCT trace_cfg_struct;
2964 		TRACE_STATUS_ELEMENT_STRUCT trace_element_struct;
2965 
2966 		switch(chdlc_udp_pkt->cblock.command) {
2967 
2968 		case CPIPE_ENABLE_TRACING:
2969 		     if (!chdlc_priv_area->TracingEnabled) {
2970 
2971 			/* OPERATE_DATALINE_MONITOR */
2972 
2973 			mb->buffer_length = sizeof(LINE_TRACE_CONFIG_STRUCT);
2974 			mb->command = SET_TRACE_CONFIGURATION;
2975 
2976     			((LINE_TRACE_CONFIG_STRUCT *)mb->data)->
2977 				trace_config = TRACE_ACTIVE;
2978 			/* Trace delay mode is not used because it slows
2979 			   down transfer and results in a standoff situation
2980 			   when there is a lot of data */
2981 
2982 			/* Configure the Trace based on user inputs */
2983 			((LINE_TRACE_CONFIG_STRUCT *)mb->data)->trace_config |=
2984 					chdlc_udp_pkt->data[0];
2985 
2986 			((LINE_TRACE_CONFIG_STRUCT *)mb->data)->
2987 			   trace_deactivation_timer = 4000;
2988 
2989 
2990 			err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
2991 			if (err != COMMAND_OK) {
2992 				chdlc_error(card,err,mb);
2993 				card->TracingEnabled = 0;
2994 				chdlc_udp_pkt->cblock.return_code = err;
2995 				mb->buffer_length = 0;
2996 				break;
2997 	    		}
2998 
2999 			/* Get the base address of the trace element list */
3000 			mb->buffer_length = 0;
3001 			mb->command = READ_TRACE_CONFIGURATION;
3002 			err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
3003 
3004 			if (err != COMMAND_OK) {
3005 				chdlc_error(card,err,mb);
3006 				chdlc_priv_area->TracingEnabled = 0;
3007 				chdlc_udp_pkt->cblock.return_code = err;
3008 				mb->buffer_length = 0;
3009 				break;
3010 	    		}
3011 
3012 	   		trace_status_cfg_addr =((LINE_TRACE_CONFIG_STRUCT *)
3013 				mb->data) -> ptr_trace_stat_el_cfg_struct;
3014 
3015 			sdla_peek(&card->hw, trace_status_cfg_addr,
3016 				 &trace_cfg_struct, sizeof(trace_cfg_struct));
3017 
3018 			chdlc_priv_area->start_trace_addr = trace_cfg_struct.
3019 				base_addr_trace_status_elements;
3020 
3021 			chdlc_priv_area->number_trace_elements =
3022 					trace_cfg_struct.number_trace_status_elements;
3023 
3024 			chdlc_priv_area->end_trace_addr = (unsigned long)
3025 					((TRACE_STATUS_ELEMENT_STRUCT *)
3026 					 chdlc_priv_area->start_trace_addr +
3027 					 (chdlc_priv_area->number_trace_elements - 1));
3028 
3029 			chdlc_priv_area->base_addr_trace_buffer =
3030 					trace_cfg_struct.base_addr_trace_buffer;
3031 
3032 			chdlc_priv_area->end_addr_trace_buffer =
3033 					trace_cfg_struct.end_addr_trace_buffer;
3034 
3035 		    	chdlc_priv_area->curr_trace_addr =
3036 					trace_cfg_struct.next_trace_element_to_use;
3037 
3038 	    		chdlc_priv_area->available_buffer_space = 2000 -
3039 								  sizeof(ip_pkt_t) -
3040 								  sizeof(udp_pkt_t) -
3041 							      	  sizeof(wp_mgmt_t) -
3042 								  sizeof(cblock_t) -
3043 							          sizeof(trace_info_t);
3044 	       	     }
3045 		     chdlc_udp_pkt->cblock.return_code = COMMAND_OK;
3046 		     mb->buffer_length = 0;
3047 	       	     chdlc_priv_area->TracingEnabled = 1;
3048 	       	     break;
3049 
3050 
3051 		case CPIPE_DISABLE_TRACING:
3052 		     if (chdlc_priv_area->TracingEnabled) {
3053 
3054 			/* OPERATE_DATALINE_MONITOR */
3055 			mb->buffer_length = sizeof(LINE_TRACE_CONFIG_STRUCT);
3056 			mb->command = SET_TRACE_CONFIGURATION;
3057     			((LINE_TRACE_CONFIG_STRUCT *)mb->data)->
3058 				trace_config = TRACE_INACTIVE;
3059 			err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
3060 		     }
3061 
3062 		     chdlc_priv_area->TracingEnabled = 0;
3063 		     chdlc_udp_pkt->cblock.return_code = COMMAND_OK;
3064 		     mb->buffer_length = 0;
3065 		     break;
3066 
3067 
3068 		case CPIPE_GET_TRACE_INFO:
3069 
3070 		     if (!chdlc_priv_area->TracingEnabled) {
3071 			chdlc_udp_pkt->cblock.return_code = 1;
3072 			mb->buffer_length = 0;
3073 			break;
3074 		     }
3075 
3076   		     chdlc_udp_pkt->trace_info.ismoredata = 0x00;
3077 		     buffer_length = 0;	/* offset of packet already occupied */
3078 
3079 		     for (frames=0; frames < chdlc_priv_area->number_trace_elements; frames++){
3080 
3081 			trace_pkt_t *trace_pkt = (trace_pkt_t *)
3082 				&chdlc_udp_pkt->data[buffer_length];
3083 
3084 			sdla_peek(&card->hw, chdlc_priv_area->curr_trace_addr,
3085 			   	  (unsigned char *)&trace_element_struct,
3086 			   	  sizeof(TRACE_STATUS_ELEMENT_STRUCT));
3087 
3088      			if (trace_element_struct.opp_flag == 0x00) {
3089 			 	break;
3090 			}
3091 
3092 			/* get pointer to real data */
3093 			data_ptr = trace_element_struct.ptr_data_bfr;
3094 
3095 			/* See if there is actual data on the trace buffer */
3096 			if (data_ptr){
3097 				data_length = trace_element_struct.trace_length;
3098 			}else{
3099 				data_length = 0;
3100 				chdlc_udp_pkt->trace_info.ismoredata = 0x01;
3101 			}
3102 
3103    			if( (chdlc_priv_area->available_buffer_space - buffer_length)
3104 				< ( sizeof(trace_pkt_t) + data_length) ) {
3105 
3106                             /* indicate there are more frames on board & exit */
3107 				chdlc_udp_pkt->trace_info.ismoredata = 0x01;
3108                                	break;
3109                          }
3110 
3111 			trace_pkt->status = trace_element_struct.trace_type;
3112 
3113 			trace_pkt->time_stamp =
3114 				trace_element_struct.trace_time_stamp;
3115 
3116 			trace_pkt->real_length =
3117 				trace_element_struct.trace_length;
3118 
3119 			/* see if we can fit the frame into the user buffer */
3120 			real_len = trace_pkt->real_length;
3121 
3122 			if (data_ptr == 0) {
3123 			     	trace_pkt->data_avail = 0x00;
3124 			} else {
3125 				unsigned tmp = 0;
3126 
3127 				/* get the data from circular buffer
3128 				    must check for end of buffer */
3129 			        trace_pkt->data_avail = 0x01;
3130 
3131 				if ((data_ptr + real_len) >
3132 					     chdlc_priv_area->end_addr_trace_buffer + 1){
3133 
3134 				    	tmp = chdlc_priv_area->end_addr_trace_buffer - data_ptr + 1;
3135 				    	sdla_peek(&card->hw, data_ptr,
3136 					       	  trace_pkt->data,tmp);
3137 				    	data_ptr = chdlc_priv_area->base_addr_trace_buffer;
3138 				}
3139 
3140 		        	sdla_peek(&card->hw, data_ptr,
3141 					  &trace_pkt->data[tmp], real_len - tmp);
3142 			}
3143 
3144 			/* zero the opp flag to show we got the frame */
3145 			ut_char = 0x00;
3146 			sdla_poke(&card->hw, chdlc_priv_area->curr_trace_addr, &ut_char, 1);
3147 
3148        			/* now move onto the next frame */
3149        			chdlc_priv_area->curr_trace_addr += sizeof(TRACE_STATUS_ELEMENT_STRUCT);
3150 
3151        			/* check if we went over the last address */
3152 			if ( chdlc_priv_area->curr_trace_addr > chdlc_priv_area->end_trace_addr ) {
3153 				chdlc_priv_area->curr_trace_addr = chdlc_priv_area->start_trace_addr;
3154        			}
3155 
3156             		if(trace_pkt->data_avail == 0x01) {
3157 				buffer_length += real_len - 1;
3158 			}
3159 
3160 	       	    	/* for the header */
3161 	            	buffer_length += sizeof(trace_pkt_t);
3162 
3163 		     }  /* For Loop */
3164 
3165 		     if (frames == chdlc_priv_area->number_trace_elements){
3166 			chdlc_udp_pkt->trace_info.ismoredata = 0x01;
3167 	             }
3168  		     chdlc_udp_pkt->trace_info.num_frames = frames;
3169 
3170     		     mb->buffer_length = buffer_length;
3171 		     chdlc_udp_pkt->cblock.buffer_length = buffer_length;
3172 
3173 		     chdlc_udp_pkt->cblock.return_code = COMMAND_OK;
3174 
3175 		     break;
3176 
3177 
3178 		case CPIPE_FT1_READ_STATUS:
3179 			((unsigned char *)chdlc_udp_pkt->data )[0] =
3180 				flags->FT1_info_struct.parallel_port_A_input;
3181 
3182 			((unsigned char *)chdlc_udp_pkt->data )[1] =
3183 				flags->FT1_info_struct.parallel_port_B_input;
3184 
3185 			chdlc_udp_pkt->cblock.return_code = COMMAND_OK;
3186 			chdlc_udp_pkt->cblock.buffer_length = 2;
3187 			mb->buffer_length = 2;
3188 			break;
3189 
3190 		case CPIPE_ROUTER_UP_TIME:
3191 			do_gettimeofday( &tv );
3192 			chdlc_priv_area->router_up_time = tv.tv_sec -
3193 					chdlc_priv_area->router_start_time;
3194 			*(unsigned long *)&chdlc_udp_pkt->data =
3195 					chdlc_priv_area->router_up_time;
3196 			mb->buffer_length = sizeof(unsigned long);
3197 			chdlc_udp_pkt->cblock.buffer_length = sizeof(unsigned long);
3198 			chdlc_udp_pkt->cblock.return_code = COMMAND_OK;
3199 			break;
3200 
3201    		case FT1_MONITOR_STATUS_CTRL:
3202 			/* Enable FT1 MONITOR STATUS */
3203 	        	if ((chdlc_udp_pkt->data[0] & ENABLE_READ_FT1_STATUS) ||
3204 				(chdlc_udp_pkt->data[0] & ENABLE_READ_FT1_OP_STATS)) {
3205 
3206 			     	if( rCount++ != 0 ) {
3207 					chdlc_udp_pkt->cblock.
3208 					return_code = COMMAND_OK;
3209 					mb->buffer_length = 1;
3210 		  			break;
3211 		    	     	}
3212 	      		}
3213 
3214 	      		/* Disable FT1 MONITOR STATUS */
3215 	      		if( chdlc_udp_pkt->data[0] == 0) {
3216 
3217 	      	   	     	if( --rCount != 0) {
3218 		  			chdlc_udp_pkt->cblock.
3219 					return_code = COMMAND_OK;
3220 					mb->buffer_length = 1;
3221 		  			break;
3222 	   	    	     	}
3223 	      		}
3224 			goto dflt_1;
3225 
3226 		default:
3227 dflt_1:
3228 			/* it's a board command */
3229 			mb->command = chdlc_udp_pkt->cblock.command;
3230 			mb->buffer_length = chdlc_udp_pkt->cblock.buffer_length;
3231 			if (mb->buffer_length) {
3232 				memcpy(&mb->data, (unsigned char *) chdlc_udp_pkt->
3233 							data, mb->buffer_length);
3234 	      		}
3235 			/* run the command on the board */
3236 			err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
3237 			if (err != COMMAND_OK) {
3238 				break;
3239 			}
3240 
3241 			/* copy the result back to our buffer */
3242 	         	memcpy(&chdlc_udp_pkt->cblock, mb, sizeof(cblock_t));
3243 
3244 			if (mb->buffer_length) {
3245 	         		memcpy(&chdlc_udp_pkt->data, &mb->data,
3246 								mb->buffer_length);
3247 	      		}
3248 
3249 		} /* end of switch */
3250      	} /* end of else */
3251 
3252      	/* Fill UDP TTL */
3253 	chdlc_udp_pkt->ip_pkt.ttl = card->wandev.ttl;
3254 
3255      	len = reply_udp(chdlc_priv_area->udp_pkt_data, mb->buffer_length);
3256 
3257 
3258      	if(chdlc_priv_area->udp_pkt_src == UDP_PKT_FRM_NETWORK){
3259 
3260 		/* Must check if we interrupted if_send() routine. The
3261 		 * tx buffers might be used. If so drop the packet */
3262 	   	if (!test_bit(SEND_CRIT,&card->wandev.critical)) {
3263 
3264 			if(!chdlc_send(card, chdlc_priv_area->udp_pkt_data, len)) {
3265 				++ card->wandev.stats.tx_packets;
3266 #if defined(LINUX_2_1) || defined(LINUX_2_4)
3267 				card->wandev.stats.tx_bytes += len;
3268 #endif
3269 			}
3270 		}
3271 	} else {
3272 
3273 		/* Pass it up the stack
3274     		   Allocate socket buffer */
3275 		if ((new_skb = dev_alloc_skb(len)) != NULL) {
3276 			/* copy data into new_skb */
3277 
3278  	    		buf = skb_put(new_skb, len);
3279   	    		memcpy(buf, chdlc_priv_area->udp_pkt_data, len);
3280 
3281             		/* Decapsulate pkt and pass it up the protocol stack */
3282 	    		new_skb->protocol = htons(ETH_P_IP);
3283             		new_skb->dev = dev;
3284 	    		new_skb->mac.raw  = new_skb->data;
3285 
3286 			netif_rx(new_skb);
3287 		} else {
3288 
3289 			printk(KERN_INFO "%s: no socket buffers available!\n",
3290 					card->devname);
3291   		}
3292     	}
3293 
3294 	chdlc_priv_area->udp_pkt_lgth = 0;
3295 
3296 	return 0;
3297 }
3298 
3299 /*============================================================================
3300  * Initialize Receive and Transmit Buffers.
3301  */
3302 
init_chdlc_tx_rx_buff(sdla_t * card)3303 static void init_chdlc_tx_rx_buff( sdla_t* card)
3304 {
3305 	CHDLC_MAILBOX_STRUCT* mb = card->mbox;
3306 	CHDLC_TX_STATUS_EL_CFG_STRUCT *tx_config;
3307 	CHDLC_RX_STATUS_EL_CFG_STRUCT *rx_config;
3308 	char err;
3309 
3310 	mb->buffer_length = 0;
3311 	mb->command = READ_CHDLC_CONFIGURATION;
3312 	err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
3313 
3314 	if(err != COMMAND_OK) {
3315 		if (card->wandev.dev){
3316 			chdlc_error(card,err,mb);
3317 		}
3318 		return;
3319 	}
3320 
3321 	if(card->hw.type == SDLA_S514) {
3322 		tx_config = (CHDLC_TX_STATUS_EL_CFG_STRUCT *)(card->hw.dpmbase +
3323                 (((CHDLC_CONFIGURATION_STRUCT *)mb->data)->
3324                             ptr_CHDLC_Tx_stat_el_cfg_struct));
3325         	rx_config = (CHDLC_RX_STATUS_EL_CFG_STRUCT *)(card->hw.dpmbase +
3326                 (((CHDLC_CONFIGURATION_STRUCT *)mb->data)->
3327                             ptr_CHDLC_Rx_stat_el_cfg_struct));
3328 
3329        		/* Setup Head and Tails for buffers */
3330         	card->u.c.txbuf_base = (void *)(card->hw.dpmbase +
3331                 tx_config->base_addr_Tx_status_elements);
3332         	card->u.c.txbuf_last =
3333 		(CHDLC_DATA_TX_STATUS_EL_STRUCT *)
3334                 card->u.c.txbuf_base +
3335 		(tx_config->number_Tx_status_elements - 1);
3336 
3337         	card->u.c.rxbuf_base = (void *)(card->hw.dpmbase +
3338                 rx_config->base_addr_Rx_status_elements);
3339         	card->u.c.rxbuf_last =
3340 		(CHDLC_DATA_RX_STATUS_EL_STRUCT *)
3341                 card->u.c.rxbuf_base +
3342 		(rx_config->number_Rx_status_elements - 1);
3343 
3344  		/* Set up next pointer to be used */
3345         	card->u.c.txbuf = (void *)(card->hw.dpmbase +
3346                 tx_config->next_Tx_status_element_to_use);
3347         	card->u.c.rxmb = (void *)(card->hw.dpmbase +
3348                 rx_config->next_Rx_status_element_to_use);
3349 	}
3350         else {
3351                 tx_config = (CHDLC_TX_STATUS_EL_CFG_STRUCT *)(card->hw.dpmbase +
3352 			(((CHDLC_CONFIGURATION_STRUCT *)mb->data)->
3353 			ptr_CHDLC_Tx_stat_el_cfg_struct % SDLA_WINDOWSIZE));
3354 
3355                 rx_config = (CHDLC_RX_STATUS_EL_CFG_STRUCT *)(card->hw.dpmbase +
3356 			(((CHDLC_CONFIGURATION_STRUCT *)mb->data)->
3357 			ptr_CHDLC_Rx_stat_el_cfg_struct % SDLA_WINDOWSIZE));
3358 
3359                 /* Setup Head and Tails for buffers */
3360                 card->u.c.txbuf_base = (void *)(card->hw.dpmbase +
3361 		(tx_config->base_addr_Tx_status_elements % SDLA_WINDOWSIZE));
3362                 card->u.c.txbuf_last =
3363 		(CHDLC_DATA_TX_STATUS_EL_STRUCT *)card->u.c.txbuf_base
3364 		+ (tx_config->number_Tx_status_elements - 1);
3365                 card->u.c.rxbuf_base = (void *)(card->hw.dpmbase +
3366 		(rx_config->base_addr_Rx_status_elements % SDLA_WINDOWSIZE));
3367                 card->u.c.rxbuf_last =
3368 		(CHDLC_DATA_RX_STATUS_EL_STRUCT *)card->u.c.rxbuf_base
3369 		+ (rx_config->number_Rx_status_elements - 1);
3370 
3371                  /* Set up next pointer to be used */
3372                 card->u.c.txbuf = (void *)(card->hw.dpmbase +
3373 		(tx_config->next_Tx_status_element_to_use % SDLA_WINDOWSIZE));
3374                 card->u.c.rxmb = (void *)(card->hw.dpmbase +
3375 		(rx_config->next_Rx_status_element_to_use % SDLA_WINDOWSIZE));
3376         }
3377 
3378         /* Setup Actual Buffer Start and end addresses */
3379         card->u.c.rx_base = rx_config->base_addr_Rx_buffer;
3380         card->u.c.rx_top  = rx_config->end_addr_Rx_buffer;
3381 
3382 }
3383 
3384 /*=============================================================================
3385  * Perform Interrupt Test by running READ_CHDLC_CODE_VERSION command MAX_INTR
3386  * _TEST_COUNTER times.
3387  */
intr_test(sdla_t * card)3388 static int intr_test( sdla_t* card)
3389 {
3390 	CHDLC_MAILBOX_STRUCT* mb = card->mbox;
3391 	int err,i;
3392 
3393 	Intr_test_counter = 0;
3394 
3395 	err = chdlc_set_intr_mode(card, APP_INT_ON_COMMAND_COMPLETE);
3396 
3397 	if (err == CMD_OK) {
3398 		for (i = 0; i < MAX_INTR_TEST_COUNTER; i ++) {
3399 			mb->buffer_length  = 0;
3400 			mb->command = READ_CHDLC_CODE_VERSION;
3401 			err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
3402 			if (err != CMD_OK)
3403 				chdlc_error(card, err, mb);
3404 		}
3405 	}
3406 	else {
3407 		return err;
3408 	}
3409 
3410 	err = chdlc_set_intr_mode(card, 0);
3411 
3412 	if (err != CMD_OK)
3413 		return err;
3414 
3415 	return 0;
3416 }
3417 
3418 /*==============================================================================
3419  * Determine what type of UDP call it is. CPIPEAB ?
3420  */
udp_pkt_type(struct sk_buff * skb,sdla_t * card)3421 static int udp_pkt_type(struct sk_buff *skb, sdla_t* card)
3422 {
3423 	 chdlc_udp_pkt_t *chdlc_udp_pkt = (chdlc_udp_pkt_t *)skb->data;
3424 
3425 #ifdef _WAN_UDP_DEBUG
3426 		printk(KERN_INFO "SIG %s = %s\n\
3427 				  UPP %x = %x\n\
3428 				  PRT %x = %x\n\
3429 				  REQ %i = %i\n\
3430 				  36 th = %x 37th = %x\n",
3431 				  chdlc_udp_pkt->wp_mgmt.signature,
3432 				  UDPMGMT_SIGNATURE,
3433 				  chdlc_udp_pkt->udp_pkt.udp_dst_port,
3434 				  ntohs(card->wandev.udp_port),
3435 				  chdlc_udp_pkt->ip_pkt.protocol,
3436 				  UDPMGMT_UDP_PROTOCOL,
3437 				  chdlc_udp_pkt->wp_mgmt.request_reply,
3438 				  UDPMGMT_REQUEST,
3439 				  skb->data[36], skb->data[37]);
3440 #endif
3441 
3442 	if (!strncmp(chdlc_udp_pkt->wp_mgmt.signature,UDPMGMT_SIGNATURE,8) &&
3443 	   (chdlc_udp_pkt->udp_pkt.udp_dst_port == ntohs(card->wandev.udp_port)) &&
3444 	   (chdlc_udp_pkt->ip_pkt.protocol == UDPMGMT_UDP_PROTOCOL) &&
3445 	   (chdlc_udp_pkt->wp_mgmt.request_reply == UDPMGMT_REQUEST)) {
3446 
3447 		return UDP_CPIPE_TYPE;
3448 
3449 	}else{
3450 		return UDP_INVALID_TYPE;
3451 	}
3452 }
3453 
3454 /*============================================================================
3455  * Set PORT state.
3456  */
port_set_state(sdla_t * card,int state)3457 static void port_set_state (sdla_t *card, int state)
3458 {
3459         if (card->u.c.state != state)
3460         {
3461                 switch (state)
3462                 {
3463                 case WAN_CONNECTED:
3464                         printk (KERN_INFO "%s: Link connected!\n",
3465                                 card->devname);
3466                       	break;
3467 
3468                 case WAN_CONNECTING:
3469                         printk (KERN_INFO "%s: Link connecting...\n",
3470                                 card->devname);
3471                         break;
3472 
3473                 case WAN_DISCONNECTED:
3474                         printk (KERN_INFO "%s: Link disconnected!\n",
3475                                 card->devname);
3476                         break;
3477                 }
3478 
3479                 card->wandev.state = card->u.c.state = state;
3480 		if (card->wandev.dev){
3481 			netdevice_t *dev = card->wandev.dev;
3482 			chdlc_private_area_t *chdlc_priv_area = dev->priv;
3483 			chdlc_priv_area->common.state = state;
3484 		}
3485         }
3486 }
3487 
3488 /*===========================================================================
3489  * config_chdlc
3490  *
3491  *	Configure the chdlc protocol and enable communications.
3492  *
3493  *   	The if_open() function binds this function to the poll routine.
3494  *      Therefore, this function will run every time the chdlc interface
3495  *      is brought up. We cannot run this function from the if_open
3496  *      because if_open does not have access to the remote IP address.
3497  *
3498  *	If the communications are not enabled, proceed to configure
3499  *      the card and enable communications.
3500  *
3501  *      If the communications are enabled, it means that the interface
3502  *      was shutdown by ether the user or driver. In this case, we
3503  *      have to check that the IP addresses have not changed.  If
3504  *      the IP addresses have changed, we have to reconfigure the firmware
3505  *      and update the changed IP addresses.  Otherwise, just exit.
3506  *
3507  */
3508 
config_chdlc(sdla_t * card)3509 static int config_chdlc (sdla_t *card)
3510 {
3511 	netdevice_t *dev = card->wandev.dev;
3512 	chdlc_private_area_t *chdlc_priv_area = dev->priv;
3513 	SHARED_MEMORY_INFO_STRUCT *flags = card->u.c.flags;
3514 
3515 	if (card->u.c.comm_enabled){
3516 
3517 		/* Jun 20. 2000: NC
3518 		 * IP addresses are not used in the API mode */
3519 
3520 		if ((chdlc_priv_area->ip_local_tmp != chdlc_priv_area->ip_local ||
3521 		     chdlc_priv_area->ip_remote_tmp != chdlc_priv_area->ip_remote) &&
3522 		     card->u.c.usedby == WANPIPE) {
3523 
3524 			/* The IP addersses have changed, we must
3525                          * stop the communications and reconfigure
3526                          * the card. Reason: the firmware must know
3527                          * the local and remote IP addresses. */
3528 			disable_comm(card);
3529 			port_set_state(card, WAN_DISCONNECTED);
3530 			printk(KERN_INFO
3531 				"%s: IP addresses changed!\n",
3532 					card->devname);
3533 			printk(KERN_INFO
3534 				"%s: Restarting communications ...\n",
3535 					card->devname);
3536 		}else{
3537 			/* IP addresses are the same and the link is up,
3538                          * we dont have to do anything here. Therefore, exit */
3539 			return 0;
3540 		}
3541 	}
3542 
3543 	chdlc_priv_area->ip_local = chdlc_priv_area->ip_local_tmp;
3544 	chdlc_priv_area->ip_remote = chdlc_priv_area->ip_remote_tmp;
3545 
3546 
3547 	/* Setup the Board for asynchronous mode */
3548 	if (card->u.c.async_mode){
3549 
3550 		if (set_asy_config(card)) {
3551 			printk (KERN_INFO "%s: Failed CHDLC Async configuration!\n",
3552 				card->devname);
3553 			return 0;
3554 		}
3555 	}else{
3556 		/* Setup the Board for CHDLC */
3557 		if (set_chdlc_config(card)) {
3558 			printk (KERN_INFO "%s: Failed CHDLC configuration!\n",
3559 				card->devname);
3560 			return 0;
3561 		}
3562 	}
3563 
3564 	/* Set interrupt mode and mask */
3565         if (chdlc_set_intr_mode(card, APP_INT_ON_RX_FRAME |
3566                 		APP_INT_ON_GLOBAL_EXCEP_COND |
3567                 		APP_INT_ON_TX_FRAME |
3568                 		APP_INT_ON_CHDLC_EXCEP_COND | APP_INT_ON_TIMER)){
3569 		printk (KERN_INFO "%s: Failed to set interrupt triggers!\n",
3570 				card->devname);
3571 		return 0;
3572         }
3573 
3574 
3575 	/* Mask the Transmit and Timer interrupt */
3576 	flags->interrupt_info_struct.interrupt_permission &=
3577 		~(APP_INT_ON_TX_FRAME | APP_INT_ON_TIMER);
3578 
3579 	/* In TTY mode, receive interrupt will be enabled during
3580 	 * wanpipe_tty_open() operation */
3581 	if (card->tty_opt){
3582 		flags->interrupt_info_struct.interrupt_permission &= ~APP_INT_ON_RX_FRAME;
3583 	}
3584 
3585 	/* Enable communications */
3586  	if (card->u.c.async_mode){
3587 		if (asy_comm_enable(card) != 0) {
3588 			printk(KERN_INFO "%s: Failed to enable async commnunication!\n",
3589 					card->devname);
3590 			flags->interrupt_info_struct.interrupt_permission = 0;
3591 			card->u.c.comm_enabled=0;
3592 			chdlc_set_intr_mode(card,0);
3593 			return 0;
3594 		}
3595         }else{
3596 		if (chdlc_comm_enable(card) != 0) {
3597 			printk(KERN_INFO "%s: Failed to enable chdlc communications!\n",
3598 					card->devname);
3599 			flags->interrupt_info_struct.interrupt_permission = 0;
3600 			card->u.c.comm_enabled=0;
3601 			chdlc_set_intr_mode(card,0);
3602 			return 0;
3603 		}
3604 	}
3605 
3606 	/* Initialize Rx/Tx buffer control fields */
3607 	init_chdlc_tx_rx_buff(card);
3608 	port_set_state(card, WAN_CONNECTING);
3609 	return 0;
3610 }
3611 
3612 
3613 /*============================================================
3614  * chdlc_poll
3615  *
3616  * Rationale:
3617  * 	We cannot manipulate the routing tables, or
3618  *      ip addresses withing the interrupt. Therefore
3619  *      we must perform such actons outside an interrupt
3620  *      at a later time.
3621  *
3622  * Description:
3623  *	CHDLC polling routine, responsible for
3624  *     	shutting down interfaces upon disconnect
3625  *     	and adding/removing routes.
3626  *
3627  * Usage:
3628  * 	This function is executed for each CHDLC
3629  * 	interface through a tq_schedule bottom half.
3630  *
3631  *      trigger_chdlc_poll() function is used to kick
3632  *      the chldc_poll routine.
3633  */
3634 
chdlc_poll(netdevice_t * dev)3635 static void chdlc_poll (netdevice_t *dev)
3636 {
3637 	chdlc_private_area_t *chdlc_priv_area;
3638 	sdla_t *card;
3639 	u8 check_gateway=0;
3640 	SHARED_MEMORY_INFO_STRUCT* flags;
3641 
3642 
3643 	if (!dev || (chdlc_priv_area=dev->priv) == NULL)
3644 		return;
3645 
3646 	card = chdlc_priv_area->card;
3647 	flags = card->u.c.flags;
3648 
3649 	/* (Re)Configuraiton is in progress, stop what you are
3650 	 * doing and get out */
3651 	if (test_bit(PERI_CRIT,&card->wandev.critical)){
3652 		clear_bit(POLL_CRIT,&card->wandev.critical);
3653 		return;
3654 	}
3655 
3656 	/* if_open() function has triggered the polling routine
3657 	 * to determine the configured IP addresses.  Once the
3658 	 * addresses are found, trigger the chdlc configuration */
3659 	if (test_bit(0,&chdlc_priv_area->config_chdlc)){
3660 
3661 		chdlc_priv_area->ip_local_tmp  = get_ip_address(dev,WAN_LOCAL_IP);
3662 		chdlc_priv_area->ip_remote_tmp = get_ip_address(dev,WAN_POINTOPOINT_IP);
3663 
3664 	       /* Jun 20. 2000 Bug Fix
3665 	 	* Only perform this check in WANPIPE mode, since
3666 	 	* IP addresses are not used in the API mode. */
3667 
3668 		if (chdlc_priv_area->ip_local_tmp == chdlc_priv_area->ip_remote_tmp &&
3669 		    card->u.c.slarp_timer == 0x00 &&
3670 		    !card->u.c.backup &&
3671 		    card->u.c.usedby == WANPIPE){
3672 
3673 			if (++chdlc_priv_area->ip_error > MAX_IP_ERRORS){
3674 				printk(KERN_INFO "\n%s: --- WARNING ---\n",
3675 						card->devname);
3676 				printk(KERN_INFO
3677 				"%s: The local IP address is the same as the\n",
3678 						card->devname);
3679 				printk(KERN_INFO
3680 				"%s: Point-to-Point IP address.\n",
3681 						card->devname);
3682 				printk(KERN_INFO "%s: --- WARNING ---\n\n",
3683 						card->devname);
3684 			}else{
3685 				clear_bit(POLL_CRIT,&card->wandev.critical);
3686 				chdlc_priv_area->poll_delay_timer.expires = jiffies+HZ;
3687 				add_timer(&chdlc_priv_area->poll_delay_timer);
3688 				return;
3689 			}
3690 		}
3691 
3692 		clear_bit(0,&chdlc_priv_area->config_chdlc);
3693 		clear_bit(POLL_CRIT,&card->wandev.critical);
3694 
3695 		chdlc_priv_area->timer_int_enabled |= TMR_INT_ENABLED_CONFIG;
3696 		flags->interrupt_info_struct.interrupt_permission |= APP_INT_ON_TIMER;
3697 		return;
3698 	}
3699 	/* Dynamic interface implementation, as well as dynamic
3700 	 * routing.  */
3701 
3702 	switch (card->u.c.state){
3703 
3704 	case WAN_DISCONNECTED:
3705 
3706 		/* If the dynamic interface configuration is on, and interface
3707 		 * is up, then bring down the netowrk interface */
3708 
3709 		if (test_bit(DYN_OPT_ON,&chdlc_priv_area->interface_down) &&
3710 		    !test_bit(DEV_DOWN,  &chdlc_priv_area->interface_down) &&
3711 		    card->wandev.dev->flags & IFF_UP){
3712 
3713 			printk(KERN_INFO "%s: Interface %s down.\n",
3714 				card->devname,card->wandev.dev->name);
3715 			change_dev_flags(card->wandev.dev,(card->wandev.dev->flags&~IFF_UP));
3716 			set_bit(DEV_DOWN,&chdlc_priv_area->interface_down);
3717 			chdlc_priv_area->route_status = NO_ROUTE;
3718 
3719 		}else{
3720 			/* We need to check if the local IP address is
3721                	  	 * zero. If it is, we shouldn't try to remove it.
3722                  	 */
3723 
3724 			if (card->wandev.dev->flags & IFF_UP &&
3725 		    	    get_ip_address(card->wandev.dev,WAN_LOCAL_IP) &&
3726 		    	    chdlc_priv_area->route_status != NO_ROUTE &&
3727 			    card->u.c.slarp_timer){
3728 
3729 				process_route(card);
3730 			}
3731 		}
3732 		break;
3733 
3734 	case WAN_CONNECTED:
3735 
3736 		/* In SMP machine this code can execute before the interface
3737 		 * comes up.  In this case, we must make sure that we do not
3738 		 * try to bring up the interface before dev_open() is finished */
3739 
3740 
3741 		/* DEV_DOWN will be set only when we bring down the interface
3742 		 * for the very first time. This way we know that it was us
3743 		 * that brought the interface down */
3744 
3745 		if (test_bit(DYN_OPT_ON,&chdlc_priv_area->interface_down) &&
3746 		    test_bit(DEV_DOWN,  &chdlc_priv_area->interface_down) &&
3747 		    !(card->wandev.dev->flags & IFF_UP)){
3748 
3749 			printk(KERN_INFO "%s: Interface %s up.\n",
3750 				card->devname,card->wandev.dev->name);
3751 			change_dev_flags(card->wandev.dev,(card->wandev.dev->flags|IFF_UP));
3752 			clear_bit(DEV_DOWN,&chdlc_priv_area->interface_down);
3753 			check_gateway=1;
3754 		}
3755 
3756 		if (chdlc_priv_area->route_status == ADD_ROUTE &&
3757 		    card->u.c.slarp_timer){
3758 
3759 			process_route(card);
3760 			check_gateway=1;
3761 		}
3762 
3763 		if (chdlc_priv_area->gateway && check_gateway)
3764 			add_gateway(card,dev);
3765 
3766 		break;
3767 	}
3768 
3769 	clear_bit(POLL_CRIT,&card->wandev.critical);
3770 }
3771 
3772 /*============================================================
3773  * trigger_chdlc_poll
3774  *
3775  * Description:
3776  * 	Add a chdlc_poll() task into a tq_scheduler bh handler
3777  *      for a specific dlci/interface.  This will kick
3778  *      the fr_poll() routine at a later time.
3779  *
3780  * Usage:
3781  * 	Interrupts use this to defer a taks to
3782  *      a polling routine.
3783  *
3784  */
trigger_chdlc_poll(netdevice_t * dev)3785 static void trigger_chdlc_poll (netdevice_t *dev)
3786 {
3787 	chdlc_private_area_t *chdlc_priv_area;
3788 	sdla_t *card;
3789 
3790 	if (!dev)
3791 		return;
3792 
3793 	if ((chdlc_priv_area = dev->priv)==NULL)
3794 		return;
3795 
3796 	card = chdlc_priv_area->card;
3797 
3798 	if (test_and_set_bit(POLL_CRIT,&card->wandev.critical)){
3799 		return;
3800 	}
3801 	if (test_bit(PERI_CRIT,&card->wandev.critical)){
3802 		return;
3803 	}
3804 #ifdef LINUX_2_4
3805 	schedule_task(&chdlc_priv_area->poll_task);
3806 #else
3807 	queue_task(&chdlc_priv_area->poll_task, &tq_scheduler);
3808 #endif
3809 	return;
3810 }
3811 
3812 
chdlc_poll_delay(unsigned long dev_ptr)3813 static void chdlc_poll_delay (unsigned long dev_ptr)
3814 {
3815 	netdevice_t *dev = (netdevice_t *)dev_ptr;
3816 	trigger_chdlc_poll(dev);
3817 }
3818 
3819 
s508_lock(sdla_t * card,unsigned long * smp_flags)3820 void s508_lock (sdla_t *card, unsigned long *smp_flags)
3821 {
3822 #if defined(CONFIG_SMP) || defined(LINUX_2_4)
3823 	spin_lock_irqsave(&card->wandev.lock, *smp_flags);
3824         if (card->next){
3825         	spin_lock(&card->next->wandev.lock);
3826 	}
3827 #else
3828         disable_irq(card->hw.irq);
3829 #endif
3830 }
3831 
s508_unlock(sdla_t * card,unsigned long * smp_flags)3832 void s508_unlock (sdla_t *card, unsigned long *smp_flags)
3833 {
3834 #if defined(CONFIG_SMP) || defined(LINUX_2_4)
3835         if (card->next){
3836         	spin_unlock(&card->next->wandev.lock);
3837         }
3838         spin_unlock_irqrestore(&card->wandev.lock, *smp_flags);
3839 #else
3840         enable_irq(card->hw.irq);
3841 #endif
3842 }
3843 
3844 //*********** TTY SECTION ****************
3845 #if defined(LINUX_2_4) || defined(LINUX_2_1)
3846 
wanpipe_tty_trigger_tx_irq(sdla_t * card)3847 static void wanpipe_tty_trigger_tx_irq(sdla_t *card)
3848 {
3849 	SHARED_MEMORY_INFO_STRUCT *flags = card->u.c.flags;
3850 	INTERRUPT_INFORMATION_STRUCT *chdlc_int = &flags->interrupt_info_struct;
3851 	chdlc_int->interrupt_permission |= APP_INT_ON_TX_FRAME;
3852 }
3853 
wanpipe_tty_trigger_poll(sdla_t * card)3854 static void wanpipe_tty_trigger_poll(sdla_t *card)
3855 {
3856 #ifdef LINUX_2_4
3857 	schedule_task(&card->tty_task_queue);
3858 #else
3859 	queue_task(&card->tty_task_queue, &tq_scheduler);
3860 #endif
3861 }
3862 
tty_poll_task(void * data)3863 static void tty_poll_task (void* data)
3864 {
3865 	sdla_t *card = (sdla_t*)data;
3866 	struct tty_struct *tty;
3867 
3868 	if ((tty=card->tty)==NULL)
3869 		return;
3870 
3871 	tty_wakeup(tty);
3872 #if defined(SERIAL_HAVE_POLL_WAIT) || \
3873          (defined LINUX_2_1 && LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,15))
3874 	wake_up_interruptible(&tty->poll_wait);
3875 #endif
3876 	return;
3877 }
3878 
wanpipe_tty_close(struct tty_struct * tty,struct file * filp)3879 static void wanpipe_tty_close(struct tty_struct *tty, struct file * filp)
3880 {
3881 	sdla_t *card;
3882 	unsigned long smp_flags;
3883 
3884 	if (!tty || !tty->driver_data){
3885 		return;
3886 	}
3887 
3888 	card = (sdla_t*)tty->driver_data;
3889 
3890 	if (!card)
3891 		return;
3892 
3893 	printk(KERN_INFO "%s: Closing TTY Driver!\n",
3894 			card->devname);
3895 
3896 	/* Sanity Check */
3897 	if (!card->tty_open)
3898 		return;
3899 
3900 	wanpipe_close(card);
3901 	if (--card->tty_open == 0){
3902 
3903 		lock_adapter_irq(&card->wandev.lock,&smp_flags);
3904 		card->tty=NULL;
3905 		chdlc_disable_comm_shutdown(card);
3906 		unlock_adapter_irq(&card->wandev.lock,&smp_flags);
3907 
3908 		if (card->tty_buf){
3909 			kfree(card->tty_buf);
3910 			card->tty_buf=NULL;
3911 		}
3912 
3913 		if (card->tty_rx){
3914 			kfree(card->tty_rx);
3915 			card->tty_rx=NULL;
3916 		}
3917 	}
3918 	return;
3919 }
wanpipe_tty_open(struct tty_struct * tty,struct file * filp)3920 static int wanpipe_tty_open(struct tty_struct *tty, struct file * filp)
3921 {
3922 	unsigned long smp_flags;
3923 	sdla_t *card;
3924 
3925 	if (!tty){
3926 		return -ENODEV;
3927 	}
3928 
3929 	if (!tty->driver_data){
3930 		int port;
3931 		port = MINOR(tty->device) - tty->driver.minor_start;
3932 		if ((port < 0) || (port >= NR_PORTS))
3933 			return -ENODEV;
3934 
3935 		tty->driver_data = WAN_CARD(port);
3936 		if (!tty->driver_data)
3937 			return -ENODEV;
3938 	}
3939 
3940 	card = (sdla_t*)tty->driver_data;
3941 
3942 	if (!card){
3943 		lock_adapter_irq(&card->wandev.lock,&smp_flags);
3944 		card->tty=NULL;
3945 		unlock_adapter_irq(&card->wandev.lock,&smp_flags);
3946 		return -ENODEV;
3947 	}
3948 
3949 	printk(KERN_INFO "%s: Opening TTY Driver!\n",
3950 			card->devname);
3951 
3952 	if (card->tty_open == 0){
3953 		lock_adapter_irq(&card->wandev.lock,&smp_flags);
3954 		card->tty=tty;
3955 		unlock_adapter_irq(&card->wandev.lock,&smp_flags);
3956 
3957 		if (!card->tty_buf){
3958 			card->tty_buf = kmalloc(TTY_CHDLC_MAX_MTU, GFP_KERNEL);
3959 			if (!card->tty_buf){
3960 				card->tty_buf=NULL;
3961 				card->tty=NULL;
3962 				return -ENOMEM;
3963 			}
3964 		}
3965 
3966 		if (!card->tty_rx){
3967 			card->tty_rx = kmalloc(TTY_CHDLC_MAX_MTU, GFP_KERNEL);
3968 			if (!card->tty_rx){
3969 				/* Free the buffer above */
3970 				kfree(card->tty_buf);
3971 				card->tty_buf=NULL;
3972 				card->tty=NULL;
3973 				return -ENOMEM;
3974 			}
3975 		}
3976 	}
3977 
3978 	++card->tty_open;
3979 	wanpipe_open(card);
3980 	return 0;
3981 }
3982 
wanpipe_tty_write(struct tty_struct * tty,int from_user,const unsigned char * buf,int count)3983 static int wanpipe_tty_write(struct tty_struct * tty, int from_user,
3984 		    const unsigned char *buf, int count)
3985 {
3986 	unsigned long smp_flags=0;
3987 	sdla_t *card=NULL;
3988 
3989 	if (!tty){
3990 		dbg_printk(KERN_INFO "NO TTY in Write\n");
3991 		return -ENODEV;
3992 	}
3993 
3994 	card = (sdla_t *)tty->driver_data;
3995 
3996 	if (!card){
3997 		dbg_printk(KERN_INFO "No Card in TTY Write\n");
3998 		return -ENODEV;
3999 	}
4000 
4001 	if (count > card->wandev.mtu){
4002 		dbg_printk(KERN_INFO "Frame too big in Write %i Max: %i\n",
4003 				count,card->wandev.mtu);
4004 		return -EINVAL;
4005 	}
4006 
4007 	if (card->wandev.state != WAN_CONNECTED){
4008 		dbg_printk(KERN_INFO "Card not connected in TTY Write\n");
4009 		return -EINVAL;
4010 	}
4011 
4012 	/* Lock the 508 Card: SMP is supported */
4013       	if(card->hw.type != SDLA_S514){
4014 		s508_lock(card,&smp_flags);
4015 	}
4016 
4017 	if (test_and_set_bit(SEND_CRIT,(void*)&card->wandev.critical)){
4018 		printk(KERN_INFO "%s: Critical in TTY Write\n",
4019 				card->devname);
4020 
4021 		/* Lock the 508 Card: SMP is supported */
4022 		if(card->hw.type != SDLA_S514)
4023 			s508_unlock(card,&smp_flags);
4024 
4025 		return -EINVAL;
4026 	}
4027 
4028 	if (from_user) {
4029 
4030 		unsigned char *tmp_buf;
4031 
4032 		if ((tmp_buf=card->tty_buf)==NULL){
4033 			dbg_printk(KERN_INFO "No TTY BUF in Write\n");
4034 
4035 			clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
4036 
4037 			if(card->hw.type != SDLA_S514)
4038 				s508_unlock(card,&smp_flags);
4039 
4040 			return -ENOMEM;
4041 		}
4042 
4043 		if (copy_from_user(tmp_buf,buf,count)){
4044 			dbg_printk(KERN_INFO "%s: Failed to copy from user!\n",
4045 					card->devname);
4046 
4047 			clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
4048 
4049 			if(card->hw.type != SDLA_S514)
4050 				s508_unlock(card,&smp_flags);
4051 
4052 			return -EINVAL;
4053 		}
4054 
4055 		if (chdlc_send(card,(void*)tmp_buf,count)){
4056 			dbg_printk(KERN_INFO "%s: Failed to send, retry later: user!\n",
4057 					card->devname);
4058 
4059 			clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
4060 
4061 			wanpipe_tty_trigger_tx_irq(card);
4062 
4063 			if(card->hw.type != SDLA_S514)
4064 				s508_unlock(card,&smp_flags);
4065 			return 0;
4066 		}
4067 
4068 	}else{
4069 	 	if (chdlc_send(card,(void*)buf,count)){
4070 			dbg_printk(KERN_INFO "%s: Failed to send, retry later: kernel!\n",
4071 					card->devname);
4072 			clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
4073 
4074 			wanpipe_tty_trigger_tx_irq(card);
4075 
4076 			if(card->hw.type != SDLA_S514)
4077 				s508_unlock(card,&smp_flags);
4078 			return 0;
4079 		}
4080 	}
4081 	dbg_printk(KERN_INFO "%s: Packet sent OK: %i\n",card->devname,count);
4082 	clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
4083 
4084 	if(card->hw.type != SDLA_S514)
4085 		s508_unlock(card,&smp_flags);
4086 
4087 	return count;
4088 }
4089 
wanpipe_tty_receive(sdla_t * card,unsigned addr,unsigned int len)4090 static void wanpipe_tty_receive(sdla_t *card, unsigned addr, unsigned int len)
4091 {
4092 	unsigned offset=0;
4093 	unsigned olen=len;
4094 	char fp=0;
4095 	struct tty_struct *tty;
4096 	int i;
4097 	struct tty_ldisc *ld;
4098 
4099 	if (!card->tty_open){
4100 		dbg_printk(KERN_INFO "%s: TTY not open during receive\n",
4101 				card->devname);
4102 		return;
4103 	}
4104 
4105 	if ((tty=card->tty) == NULL){
4106 		dbg_printk(KERN_INFO "%s: No TTY on receive\n",
4107 				card->devname);
4108 		return;
4109 	}
4110 
4111 	if (!tty->driver_data){
4112 		dbg_printk(KERN_INFO "%s: No Driver Data, or Flip on receive\n",
4113 				card->devname);
4114 		return;
4115 	}
4116 
4117 
4118 	if (card->u.c.async_mode){
4119 		if ((tty->flip.count+len) >= TTY_FLIPBUF_SIZE){
4120 			if (net_ratelimit()){
4121 				printk(KERN_INFO
4122 					"%s: Received packet size too big: %i bytes, Max: %i!\n",
4123 					card->devname,len,TTY_FLIPBUF_SIZE);
4124 			}
4125 			return;
4126 		}
4127 
4128 
4129 		if((addr + len) > card->u.c.rx_top + 1) {
4130 			offset = card->u.c.rx_top - addr + 1;
4131 
4132 			sdla_peek(&card->hw, addr, tty->flip.char_buf_ptr, offset);
4133 
4134 			addr = card->u.c.rx_base;
4135 			len -= offset;
4136 
4137 			tty->flip.char_buf_ptr+=offset;
4138 			tty->flip.count+=offset;
4139 			for (i=0;i<offset;i++){
4140 				*tty->flip.flag_buf_ptr = 0;
4141 				tty->flip.flag_buf_ptr++;
4142 			}
4143 		}
4144 
4145 		sdla_peek(&card->hw, addr, tty->flip.char_buf_ptr, len);
4146 
4147 		tty->flip.char_buf_ptr+=len;
4148 		card->tty->flip.count+=len;
4149 		for (i=0;i<len;i++){
4150 			*tty->flip.flag_buf_ptr = 0;
4151 			tty->flip.flag_buf_ptr++;
4152 		}
4153 
4154 		tty->low_latency=1;
4155 		tty_flip_buffer_push(tty);
4156 	}else{
4157 		if (!card->tty_rx){
4158 			if (net_ratelimit()){
4159 				printk(KERN_INFO
4160 				"%s: Receive sync buffer not available!\n",
4161 				 card->devname);
4162 			}
4163 			return;
4164 		}
4165 
4166 		if (len > TTY_CHDLC_MAX_MTU){
4167 			if (net_ratelimit()){
4168 				printk(KERN_INFO
4169 				"%s: Received packet size too big: %i bytes, Max: %i!\n",
4170 					card->devname,len,TTY_FLIPBUF_SIZE);
4171 			}
4172 			return;
4173 		}
4174 
4175 
4176 		if((addr + len) > card->u.c.rx_top + 1) {
4177 			offset = card->u.c.rx_top - addr + 1;
4178 
4179 			sdla_peek(&card->hw, addr, card->tty_rx, offset);
4180 
4181 			addr = card->u.c.rx_base;
4182 			len -= offset;
4183 		}
4184 		sdla_peek(&card->hw, addr, card->tty_rx+offset, len);
4185 		ld = tty_ldisc_ref(tty);
4186 		if (ld) {
4187 			if (ld->receive_buf)
4188 				ld->receive_buf(tty,card->tty_rx,&fp,olen);
4189 			tty_ldisc_deref(ld);
4190 		}else{
4191 			if (net_ratelimit()){
4192 				printk(KERN_INFO
4193 					"%s: NO TTY Sync line discipline!\n",
4194 					card->devname);
4195 			}
4196 		}
4197 	}
4198 
4199 	dbg_printk(KERN_INFO "%s: Received Data %i\n",card->devname,olen);
4200 	return;
4201 }
4202 
4203 #if 0
4204 static int wanpipe_tty_ioctl(struct tty_struct *tty, struct file * file,
4205 		    unsigned int cmd, unsigned long arg)
4206 {
4207 	return -ENOIOCTLCMD;
4208 }
4209 #endif
4210 
wanpipe_tty_stop(struct tty_struct * tty)4211 static void wanpipe_tty_stop(struct tty_struct *tty)
4212 {
4213 	return;
4214 }
4215 
wanpipe_tty_start(struct tty_struct * tty)4216 static void wanpipe_tty_start(struct tty_struct *tty)
4217 {
4218 	return;
4219 }
4220 
config_tty(sdla_t * card)4221 static int config_tty (sdla_t *card)
4222 {
4223 	SHARED_MEMORY_INFO_STRUCT *flags = card->u.c.flags;
4224 
4225 	/* Setup the Board for asynchronous mode */
4226 	if (card->u.c.async_mode){
4227 
4228 		if (set_asy_config(card)) {
4229 			printk (KERN_INFO "%s: Failed CHDLC Async configuration!\n",
4230 				card->devname);
4231 			return -EINVAL;
4232 		}
4233 	}else{
4234 		/* Setup the Board for CHDLC */
4235 		if (set_chdlc_config(card)) {
4236 			printk (KERN_INFO "%s: Failed CHDLC configuration!\n",
4237 				card->devname);
4238 			return -EINVAL;
4239 		}
4240 	}
4241 
4242 	/* Set interrupt mode and mask */
4243         if (chdlc_set_intr_mode(card, APP_INT_ON_RX_FRAME |
4244                 		APP_INT_ON_GLOBAL_EXCEP_COND |
4245                 		APP_INT_ON_TX_FRAME |
4246                 		APP_INT_ON_CHDLC_EXCEP_COND | APP_INT_ON_TIMER)){
4247 		printk (KERN_INFO "%s: Failed to set interrupt triggers!\n",
4248 				card->devname);
4249 		return -EINVAL;
4250         }
4251 
4252 
4253 	/* Mask the Transmit and Timer interrupt */
4254 	flags->interrupt_info_struct.interrupt_permission &=
4255 		~(APP_INT_ON_TX_FRAME | APP_INT_ON_TIMER);
4256 
4257 
4258 	/* Enable communications */
4259  	if (card->u.c.async_mode){
4260 		if (asy_comm_enable(card) != 0) {
4261 			printk(KERN_INFO "%s: Failed to enable async commnunication!\n",
4262 					card->devname);
4263 			flags->interrupt_info_struct.interrupt_permission = 0;
4264 			card->u.c.comm_enabled=0;
4265 			chdlc_set_intr_mode(card,0);
4266 			return -EINVAL;
4267 		}
4268         }else{
4269 		if (chdlc_comm_enable(card) != 0) {
4270 			printk(KERN_INFO "%s: Failed to enable chdlc communications!\n",
4271 					card->devname);
4272 			flags->interrupt_info_struct.interrupt_permission = 0;
4273 			card->u.c.comm_enabled=0;
4274 			chdlc_set_intr_mode(card,0);
4275 			return -EINVAL;
4276 		}
4277 	}
4278 
4279 	/* Initialize Rx/Tx buffer control fields */
4280 	init_chdlc_tx_rx_buff(card);
4281 	port_set_state(card, WAN_CONNECTING);
4282 	return 0;
4283 }
4284 
4285 
change_speed(sdla_t * card,struct tty_struct * tty,struct termios * old_termios)4286 static int change_speed(sdla_t *card, struct tty_struct *tty,
4287 			 struct termios *old_termios)
4288 {
4289 	int	baud, ret=0;
4290 	unsigned cflag;
4291 	int	dbits,sbits,parity,handshaking;
4292 
4293 	cflag = tty->termios->c_cflag;
4294 
4295 	/* There is always one stop bit */
4296 	sbits=WANOPT_ONE;
4297 
4298 	/* Parity is defaulted to NONE */
4299 	parity = WANOPT_NONE;
4300 
4301 	handshaking=0;
4302 
4303 	/* byte size and parity */
4304 	switch (cflag & CSIZE) {
4305 	      case CS5: dbits = 5; break;
4306 	      case CS6: dbits = 6; break;
4307 	      case CS7: dbits = 7; break;
4308 	      case CS8: dbits = 8; break;
4309 	      /* Never happens, but GCC is too dumb to figure it out */
4310 	      default:  dbits = 8; break;
4311 	}
4312 
4313 	/* One more stop bit should be supported, thus increment
4314 	 * the number of stop bits Max=2 */
4315 	if (cflag & CSTOPB) {
4316 		sbits = WANOPT_TWO;
4317 	}
4318 	if (cflag & PARENB) {
4319 		parity = WANOPT_EVEN;
4320 	}
4321 	if (cflag & PARODD){
4322 		parity = WANOPT_ODD;
4323 	}
4324 
4325 	/* Determine divisor based on baud rate */
4326 	baud = tty_get_baud_rate(tty);
4327 
4328 	if (!baud)
4329 		baud = 9600;	/* B0 transition handled in rs_set_termios */
4330 
4331 	if (cflag & CRTSCTS) {
4332 		handshaking|=ASY_RTS_HS_FOR_RX;
4333 	}
4334 
4335 	if (I_IGNPAR(tty))
4336 		parity = WANOPT_NONE;
4337 
4338 	if (I_IXOFF(tty)){
4339 		handshaking|=ASY_XON_XOFF_HS_FOR_RX;
4340 		handshaking|=ASY_XON_XOFF_HS_FOR_TX;
4341 	}
4342 
4343 	if (I_IXON(tty)){
4344 		handshaking|=ASY_XON_XOFF_HS_FOR_RX;
4345 		handshaking|=ASY_XON_XOFF_HS_FOR_TX;
4346 	}
4347 
4348 	if (card->u.c.async_mode){
4349 		if (card->wandev.bps != baud)
4350 			ret=1;
4351 		card->wandev.bps = baud;
4352 	}
4353 
4354 	if (card->u.c.async_mode){
4355 		if (card->u.c.protocol_options != handshaking)
4356 			ret=1;
4357 		card->u.c.protocol_options = handshaking;
4358 
4359 		if (card->u.c.tx_bits_per_char != dbits)
4360 			ret=1;
4361 		card->u.c.tx_bits_per_char = dbits;
4362 
4363 		if (card->u.c.rx_bits_per_char != dbits)
4364 			ret=1;
4365 		card->u.c.rx_bits_per_char = dbits;
4366 
4367 		if (card->u.c.stop_bits != sbits)
4368 			ret=1;
4369 		card->u.c.stop_bits = sbits;
4370 
4371 		if (card->u.c.parity != parity)
4372 			ret=1;
4373 		card->u.c.parity = parity;
4374 
4375 		card->u.c.break_timer = 50;
4376 		card->u.c.inter_char_timer = 10;
4377 		card->u.c.rx_complete_length = 100;
4378 		card->u.c.xon_char = 0xFE;
4379 	}else{
4380 		card->u.c.protocol_options = HDLC_STREAMING_MODE;
4381 	}
4382 
4383 	return ret;
4384 }
4385 
4386 
wanpipe_tty_set_termios(struct tty_struct * tty,struct termios * old_termios)4387 static void wanpipe_tty_set_termios(struct tty_struct *tty, struct termios *old_termios)
4388 {
4389 	sdla_t *card;
4390 	int err=1;
4391 
4392 	if (!tty){
4393 		return;
4394 	}
4395 
4396 	card = (sdla_t *)tty->driver_data;
4397 
4398 	if (!card)
4399 		return;
4400 
4401 	if (change_speed(card, tty, old_termios) || !card->u.c.comm_enabled){
4402 		unsigned long smp_flags;
4403 
4404 		if (card->u.c.comm_enabled){
4405 			lock_adapter_irq(&card->wandev.lock,&smp_flags);
4406 			chdlc_disable_comm_shutdown(card);
4407 			unlock_adapter_irq(&card->wandev.lock,&smp_flags);
4408 		}
4409 		lock_adapter_irq(&card->wandev.lock,&smp_flags);
4410 		err = config_tty(card);
4411 		unlock_adapter_irq(&card->wandev.lock,&smp_flags);
4412 		if (card->u.c.async_mode){
4413 			printk(KERN_INFO "%s: TTY Async Configuration:\n"
4414 				 "   Baud        =%i\n"
4415 				 "   Handshaking =%s\n"
4416 				 "   Tx Dbits    =%i\n"
4417 				 "   Rx Dbits    =%i\n"
4418 				 "   Parity      =%s\n"
4419 				 "   Stop Bits   =%i\n",
4420 				 card->devname,
4421 				 card->wandev.bps,
4422 				 opt_decode[card->u.c.protocol_options],
4423 				 card->u.c.tx_bits_per_char,
4424 				 card->u.c.rx_bits_per_char,
4425 				 p_decode[card->u.c.parity] ,
4426 				 card->u.c.stop_bits);
4427 		}else{
4428 			printk(KERN_INFO "%s: TTY Sync Configuration:\n"
4429 				 "   Baud        =%i\n"
4430 				 "   Protocol    =HDLC_STREAMING\n",
4431 				 card->devname,card->wandev.bps);
4432 		}
4433 		if (!err){
4434 			port_set_state(card,WAN_CONNECTED);
4435 		}else{
4436 			port_set_state(card,WAN_DISCONNECTED);
4437 		}
4438 	}
4439 	return;
4440 }
4441 
wanpipe_tty_put_char(struct tty_struct * tty,unsigned char ch)4442 static void wanpipe_tty_put_char(struct tty_struct *tty, unsigned char ch)
4443 {
4444 	sdla_t *card;
4445 	unsigned long smp_flags=0;
4446 
4447 	if (!tty){
4448 		return;
4449 	}
4450 
4451 	card = (sdla_t *)tty->driver_data;
4452 
4453 	if (!card)
4454 		return;
4455 
4456 	if (card->wandev.state != WAN_CONNECTED)
4457 		return;
4458 
4459 	if(card->hw.type != SDLA_S514)
4460 		s508_lock(card,&smp_flags);
4461 
4462 	if (test_and_set_bit(SEND_CRIT,(void*)&card->wandev.critical)){
4463 
4464 		wanpipe_tty_trigger_tx_irq(card);
4465 
4466 		if(card->hw.type != SDLA_S514)
4467 			s508_unlock(card,&smp_flags);
4468 		return;
4469 	}
4470 
4471 	if (chdlc_send(card,(void*)&ch,1)){
4472 		wanpipe_tty_trigger_tx_irq(card);
4473 		dbg_printk("%s: Failed to TX char!\n",card->devname);
4474 	}
4475 
4476 	dbg_printk("%s: Char TX OK\n",card->devname);
4477 
4478 	clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
4479 
4480 	if(card->hw.type != SDLA_S514)
4481 		s508_unlock(card,&smp_flags);
4482 
4483 	return;
4484 }
4485 
wanpipe_tty_flush_chars(struct tty_struct * tty)4486 static void wanpipe_tty_flush_chars(struct tty_struct *tty)
4487 {
4488 	return;
4489 }
4490 
wanpipe_tty_flush_buffer(struct tty_struct * tty)4491 static void wanpipe_tty_flush_buffer(struct tty_struct *tty)
4492 {
4493 	if (!tty)
4494 		return;
4495 
4496 #if defined(SERIAL_HAVE_POLL_WAIT) || \
4497          (defined LINUX_2_1 && LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,15))
4498 	wake_up_interruptible(&tty->poll_wait);
4499 #endif
4500 	tty_wakeup(tty);
4501 
4502 	return;
4503 }
4504 
4505 /*
4506  * This function is used to send a high-priority XON/XOFF character to
4507  * the device
4508  */
wanpipe_tty_send_xchar(struct tty_struct * tty,char ch)4509 static void wanpipe_tty_send_xchar(struct tty_struct *tty, char ch)
4510 {
4511 	return;
4512 }
4513 
4514 
wanpipe_tty_chars_in_buffer(struct tty_struct * tty)4515 static int wanpipe_tty_chars_in_buffer(struct tty_struct *tty)
4516 {
4517 	return 0;
4518 }
4519 
4520 
wanpipe_tty_write_room(struct tty_struct * tty)4521 static int wanpipe_tty_write_room(struct tty_struct *tty)
4522 {
4523 	sdla_t *card;
4524 
4525 	printk(KERN_INFO "TTY Write Room\n");
4526 
4527 	if (!tty){
4528 		return 0;
4529 	}
4530 
4531 	card = (sdla_t *)tty->driver_data;
4532 	if (!card)
4533 		return 0;
4534 
4535 	if (card->wandev.state != WAN_CONNECTED)
4536 		return 0;
4537 
4538 	return SEC_MAX_NO_DATA_BYTES_IN_FRAME;
4539 }
4540 
4541 
set_modem_status(sdla_t * card,unsigned char data)4542 static int set_modem_status(sdla_t *card, unsigned char data)
4543 {
4544 	CHDLC_MAILBOX_STRUCT *mb = card->mbox;
4545 	int err;
4546 
4547 	mb->buffer_length=1;
4548 	mb->command=SET_MODEM_STATUS;
4549 	mb->data[0]=data;
4550 	err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
4551 	if (err != COMMAND_OK)
4552 		chdlc_error (card, err, mb);
4553 
4554 	return err;
4555 }
4556 
wanpipe_tty_hangup(struct tty_struct * tty)4557 static void wanpipe_tty_hangup(struct tty_struct *tty)
4558 {
4559 	sdla_t *card;
4560 	unsigned long smp_flags;
4561 
4562 	printk(KERN_INFO "TTY Hangup!\n");
4563 
4564 	if (!tty){
4565 		return;
4566 	}
4567 
4568 	card = (sdla_t *)tty->driver_data;
4569 	if (!card)
4570 		return;
4571 
4572 	lock_adapter_irq(&card->wandev.lock,&smp_flags);
4573 	set_modem_status(card,0);
4574 	unlock_adapter_irq(&card->wandev.lock,&smp_flags);
4575 	return;
4576 }
4577 
wanpipe_tty_break(struct tty_struct * tty,int break_state)4578 static void wanpipe_tty_break(struct tty_struct *tty, int break_state)
4579 {
4580 	return;
4581 }
4582 
wanpipe_tty_wait_until_sent(struct tty_struct * tty,int timeout)4583 static void wanpipe_tty_wait_until_sent(struct tty_struct *tty, int timeout)
4584 {
4585 	return;
4586 }
4587 
wanpipe_tty_throttle(struct tty_struct * tty)4588 static void wanpipe_tty_throttle(struct tty_struct * tty)
4589 {
4590 	return;
4591 }
4592 
wanpipe_tty_unthrottle(struct tty_struct * tty)4593 static void wanpipe_tty_unthrottle(struct tty_struct * tty)
4594 {
4595 	return;
4596 }
4597 
wanpipe_tty_read_proc(char * page,char ** start,off_t off,int count,int * eof,void * data)4598 int wanpipe_tty_read_proc(char *page, char **start, off_t off, int count,
4599 		 int *eof, void *data)
4600 {
4601 	return 0;
4602 }
4603 
4604 /*
4605  * The serial driver boot-time initialization code!
4606  */
wanpipe_tty_init(sdla_t * card)4607 int wanpipe_tty_init(sdla_t *card)
4608 {
4609 	struct serial_state * state;
4610 
4611 	/* Initialize the tty_driver structure */
4612 
4613 	if (card->tty_minor < 0 || card->tty_minor > NR_PORTS){
4614 		printk(KERN_INFO "%s: Illegal Minor TTY number (0-4): %i\n",
4615 				card->devname,card->tty_minor);
4616 		return -EINVAL;
4617 	}
4618 
4619 	if (WAN_CARD(card->tty_minor)){
4620 		printk(KERN_INFO "%s: TTY Minor %i, already in use\n",
4621 				card->devname,card->tty_minor);
4622 		return -EBUSY;
4623 	}
4624 
4625 	if (tty_init_cnt==0){
4626 
4627 		printk(KERN_INFO "%s: TTY %s Driver Init: Major %i, Minor Range %i-%i\n",
4628 				card->devname,
4629 				card->u.c.async_mode ? "ASYNC" : "SYNC",
4630 				WAN_TTY_MAJOR,MIN_PORT,MAX_PORT);
4631 
4632 		tty_driver_mode = card->u.c.async_mode;
4633 
4634 		memset(&serial_driver, 0, sizeof(struct tty_driver));
4635 		serial_driver.magic = TTY_DRIVER_MAGIC;
4636 		serial_driver.driver_name = "wanpipe_tty";
4637 		serial_driver.name = "ttyW";
4638 		serial_driver.major = WAN_TTY_MAJOR;
4639 		serial_driver.minor_start = WAN_TTY_MINOR;
4640 		serial_driver.num = NR_PORTS;
4641 		serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
4642 		serial_driver.subtype = SERIAL_TYPE_NORMAL;
4643 
4644 		serial_driver.init_termios = tty_std_termios;
4645 		serial_driver.init_termios.c_cflag =
4646 			B9600 | CS8 | CREAD | HUPCL | CLOCAL;
4647 		serial_driver.flags = TTY_DRIVER_REAL_RAW;
4648 
4649 		serial_driver.refcount = &serial_refcount;
4650 		serial_driver.table = serial_table;
4651 		serial_driver.termios = serial_termios;
4652 		serial_driver.termios_locked = serial_termios_locked;
4653 
4654 		serial_driver.open = wanpipe_tty_open;
4655 		serial_driver.close = wanpipe_tty_close;
4656 		serial_driver.write = wanpipe_tty_write;
4657 
4658 		serial_driver.put_char = wanpipe_tty_put_char;
4659 		serial_driver.flush_chars = wanpipe_tty_flush_chars;
4660 		serial_driver.write_room = wanpipe_tty_write_room;
4661 		serial_driver.chars_in_buffer = wanpipe_tty_chars_in_buffer;
4662 		serial_driver.flush_buffer = wanpipe_tty_flush_buffer;
4663 		//serial_driver.ioctl = wanpipe_tty_ioctl;
4664 		serial_driver.throttle = wanpipe_tty_throttle;
4665 		serial_driver.unthrottle = wanpipe_tty_unthrottle;
4666 		serial_driver.send_xchar = wanpipe_tty_send_xchar;
4667 		serial_driver.set_termios = wanpipe_tty_set_termios;
4668 		serial_driver.stop = wanpipe_tty_stop;
4669 		serial_driver.start = wanpipe_tty_start;
4670 		serial_driver.hangup = wanpipe_tty_hangup;
4671 		serial_driver.break_ctl = wanpipe_tty_break;
4672 		serial_driver.wait_until_sent = wanpipe_tty_wait_until_sent;
4673 		serial_driver.read_proc = wanpipe_tty_read_proc;
4674 
4675 		/*
4676 		 * The callout device is just like normal device except for
4677 		 * major number and the subtype code.
4678 		 */
4679 		callout_driver = serial_driver;
4680 		callout_driver.name = "cuw";
4681 		callout_driver.major = TTYAUX_MAJOR;
4682 		callout_driver.subtype = SERIAL_TYPE_CALLOUT;
4683 		callout_driver.read_proc = 0;
4684 		callout_driver.proc_entry = 0;
4685 
4686 		if (tty_register_driver(&serial_driver)){
4687 			printk(KERN_INFO "%s: Failed to register serial driver!\n",
4688 					card->devname);
4689 		}
4690 
4691 		if (tty_register_driver(&callout_driver)){
4692 			printk(KERN_INFO "%s: Failed to register callout driver!\n",
4693 					card->devname);
4694 		}
4695 
4696 	}
4697 
4698 
4699 	/* The subsequent ports must comply to the initial configuration */
4700 	if (tty_driver_mode != card->u.c.async_mode){
4701 		printk(KERN_INFO "%s: Error: TTY Driver operation mode mismatch!\n",
4702 				card->devname);
4703 		printk(KERN_INFO "%s: The TTY driver is configured for %s!\n",
4704 				card->devname, tty_driver_mode ? "ASYNC" : "SYNC");
4705 		return -EINVAL;
4706 	}
4707 
4708 	tty_init_cnt++;
4709 
4710 	printk(KERN_INFO "%s: Initializing TTY %s Driver Minor %i\n",
4711 			card->devname,
4712 			tty_driver_mode ? "ASYNC" : "SYNC",
4713 			card->tty_minor);
4714 
4715 	tty_card_map[card->tty_minor] = card;
4716 	state = &rs_table[card->tty_minor];
4717 
4718 	state->magic = SSTATE_MAGIC;
4719 	state->line = 0;
4720 	state->type = PORT_UNKNOWN;
4721 	state->custom_divisor = 0;
4722 	state->close_delay = 5*HZ/10;
4723 	state->closing_wait = 30*HZ;
4724 	state->callout_termios = callout_driver.init_termios;
4725 	state->normal_termios = serial_driver.init_termios;
4726 	state->icount.cts = state->icount.dsr =
4727 		state->icount.rng = state->icount.dcd = 0;
4728 	state->icount.rx = state->icount.tx = 0;
4729 	state->icount.frame = state->icount.parity = 0;
4730 	state->icount.overrun = state->icount.brk = 0;
4731 	state->irq = card->wandev.irq;
4732 
4733 	card->tty_task_queue.routine = tty_poll_task;
4734 	card->tty_task_queue.data = (void*)card;
4735 	return 0;
4736 }
4737 
4738 #endif
4739 
4740 
4741 MODULE_LICENSE("GPL");
4742 
4743 /****** End ****************************************************************/
4744