1 /*****************************************************************************
2 * sdla_x25.c	WANPIPE(tm) Multiprotocol WAN Link Driver.  X.25 module.
3 *
4 * Author:	Nenad Corbic	<ncorbic@sangoma.com>
5 *
6 * Copyright:	(c) 1995-2001 Sangoma Technologies Inc.
7 *
8 *		This program is free software; you can redistribute it and/or
9 *		modify it under the terms of the GNU General Public License
10 *		as published by the Free Software Foundation; either version
11 *		2 of the License, or (at your option) any later version.
12 * ============================================================================
13 * Apr 03, 2001  Nenad Corbic	 o Fixed the rx_skb=NULL bug in x25 in rx_intr().
14 * Dec 26, 2000  Nenad Corbic	 o Added a new polling routine, that uses
15 *                                  a kernel timer (more efficient).
16 * Dec 25, 2000  Nenad Corbic	 o Updated for 2.4.X kernel
17 * Jul 26, 2000  Nenad Corbic	 o Increased the local packet buffering
18 * 				   for API to 4096+header_size.
19 * Jul 17, 2000  Nenad Corbic	 o Fixed the x25 startup bug. Enable
20 * 				   communications only after all interfaces
21 * 				   come up.  HIGH SVC/PVC is used to calculate
22 * 				   the number of channels.
23 *                                  Enable protocol only after all interfaces
24 *                                  are enabled.
25 * Jul 10, 2000	Nenad Corbic	 o Fixed the M_BIT bug.
26 * Apr 25, 2000  Nenad Corbic	 o Pass Modem messages to the API.
27 *                                  Disable idle timeout in X25 API.
28 * Apr 14, 2000  Nenad Corbic	 o Fixed: Large LCN number support.
29 *                                  Maximum LCN number is 4095.
30 *                                  Maximum number of X25 channels is 255.
31 * Apr 06, 2000  Nenad Corbic	 o Added SMP Support.
32 * Mar 29, 2000  Nenad Corbic	 o Added support for S514 PCI Card
33 * Mar 23, 2000  Nenad Corbic	 o Improved task queue, BH handling.
34 * Mar 14, 2000  Nenad Corbic  	 o Updated Protocol Violation handling
35 *                                  routines.  Bug Fix.
36 * Mar 10, 2000  Nenad Corbic	 o Bug Fix: corrupted mbox recovery.
37 * Mar 09, 2000  Nenad Corbic     o Fixed the auto HDLC bug.
38 * Mar 08, 2000	Nenad Corbic     o Fixed LAPB HDLC startup problems.
39 *                                  Application must bring the link up
40 *                                  before tx/rx, and bring the
41 *                                  link down on close().
42 * Mar 06, 2000	Nenad Corbic	 o Added an option for logging call setup
43 *                                  information.
44 * Feb 29, 2000  Nenad Corbic 	 o Added support for LAPB HDLC API
45 * Feb 25, 2000  Nenad Corbic     o Fixed the modem failure handling.
46 *                                  No Modem OOB message will be passed
47 *                                  to the user.
48 * Feb 21, 2000  Nenad Corbic 	 o Added Xpipemon Debug Support
49 * Dec 30, 1999 	Nenad Corbic	 o Socket based X25API
50 * Sep 17, 1998	Jaspreet Singh	 o Updates for 2.2.X  kernel
51 * Mar 15, 1998	Alan Cox	 o 2.1.x porting
52 * Dec 19, 1997	Jaspreet Singh	 o Added multi-channel IPX support
53 * Nov 27, 1997	Jaspreet Singh	 o Added protection against enabling of irqs
54 *				   when they are disabled.
55 * Nov 17, 1997  Farhan Thawar    o Added IPX support
56 *				 o Changed if_send() to now buffer packets when
57 *				   the board is busy
58 *				 o Removed queueing of packets via the polling
59 *				   routing
60 *				 o Changed if_send() critical flags to properly
61 *				   handle race conditions
62 * Nov 06, 1997  Farhan Thawar    o Added support for SVC timeouts
63 *				 o Changed PVC encapsulation to ETH_P_IP
64 * Jul 21, 1997  Jaspreet Singh	 o Fixed freeing up of buffers using kfree()
65 *				   when packets are received.
66 * Mar 11, 1997  Farhan Thawar   Version 3.1.1
67 *                                o added support for V35
68 *                                o changed if_send() to return 0 if
69 *                                  wandev.critical() is true
70 *                                o free socket buffer in if_send() if
71 *                                  returning 0
72 *                                o added support for single '@' address to
73 *                                  accept all incoming calls
74 *                                o fixed bug in set_chan_state() to disconnect
75 * Jan 15, 1997	Gene Kozin	Version 3.1.0
76 *				 o implemented exec() entry point
77 * Jan 07, 1997	Gene Kozin	Initial version.
78 *****************************************************************************/
79 
80 /*======================================================
81  * 	Includes
82  *=====================================================*/
83 
84 #include <linux/module.h>
85 #include <linux/version.h>
86 #include <linux/kernel.h>	/* printk(), and other useful stuff */
87 #include <linux/stddef.h>	/* offsetof(), etc. */
88 #include <linux/errno.h>	/* return codes */
89 #include <linux/string.h>	/* inline memset(), etc. */
90 #include <linux/ctype.h>
91 #include <linux/slab.h>	/* kmalloc(), kfree() */
92 #include <linux/wanrouter.h>	/* WAN router definitions */
93 #include <linux/wanpipe.h>	/* WANPIPE common user API definitions */
94 #include <asm/byteorder.h>	/* htons(), etc. */
95 #include <asm/atomic.h>
96 #include <linux/delay.h>	/* Experimental delay */
97 
98 #if defined(LINUX_2_1) || defined(LINUX_2_4)
99  #include <asm/uaccess.h>
100 #else
101  #include <asm/segment.h>
102  #include <net/route.h>
103 #endif
104 
105 #include <linux/if.h>
106 #include <linux/if_arp.h>
107 #include <linux/sdla_x25.h>	/* X.25 firmware API definitions */
108 #include <linux/if_wanpipe_common.h>
109 #include <linux/if_wanpipe.h>
110 
111 
112 /*======================================================
113  * 	Defines & Macros
114  *=====================================================*/
115 
116 
117 #define	CMD_OK		0		/* normal firmware return code */
118 #define	CMD_TIMEOUT	0xFF		/* firmware command timed out */
119 #define	MAX_CMD_RETRY	10		/* max number of firmware retries */
120 
121 #define	X25_CHAN_MTU	4096		/* unfragmented logical channel MTU */
122 #define	X25_HRDHDR_SZ	7		/* max encapsulation header size */
123 #define	X25_CONCT_TMOUT	(90*HZ)		/* link connection timeout */
124 #define	X25_RECON_TMOUT	(10*HZ)		/* link connection timeout */
125 #define	CONNECT_TIMEOUT	(90*HZ)		/* link connection timeout */
126 #define	HOLD_DOWN_TIME	(30*HZ)		/* link hold down time */
127 #define MAX_BH_BUFF	10
128 #define M_BIT		0x01
129 
130 //#define PRINT_DEBUG 1
131 #ifdef PRINT_DEBUG
132 #define DBG_PRINTK(format, a...) printk(format, ## a)
133 #else
134 #define DBG_PRINTK(format, a...)
135 #endif
136 
137 #define TMR_INT_ENABLED_POLL_ACTIVE      0x01
138 #define TMR_INT_ENABLED_POLL_CONNECT_ON  0x02
139 #define TMR_INT_ENABLED_POLL_CONNECT_OFF 0x04
140 #define TMR_INT_ENABLED_POLL_DISCONNECT  0x08
141 #define TMR_INT_ENABLED_CMD_EXEC	 0x10
142 #define TMR_INT_ENABLED_UPDATE		 0x20
143 #define TMR_INT_ENABLED_UDP_PKT		 0x40
144 
145 #define MAX_X25_ADDR_SIZE	16
146 #define MAX_X25_DATA_SIZE 	129
147 #define MAX_X25_FACL_SIZE	110
148 
149 #define TRY_CMD_AGAIN	2
150 #define DELAY_RESULT    1
151 #define RETURN_RESULT   0
152 
153 #define DCD(x) (x & 0x03 ? "HIGH" : "LOW")
154 #define CTS(x) (x & 0x05 ? "HIGH" : "LOW")
155 
156 
157 /* Driver will not write log messages about
158  * modem status if defined.*/
159 #define MODEM_NOT_LOG 1
160 
161 /*====================================================
162  * 	For IPXWAN
163  *===================================================*/
164 
165 #define CVHexToAscii(b) (((unsigned char)(b) > (unsigned char)9) ? ((unsigned char)'A' + ((unsigned char)(b) - (unsigned char)10)) : ((unsigned char)'0' + (unsigned char)(b)))
166 
167 
168 /*====================================================
169  *           MEMORY DEBUGGING FUNCTION
170  *====================================================
171 
172 #define KMEM_SAFETYZONE 8
173 
174 static void * dbg_kmalloc(unsigned int size, int prio, int line) {
175 	int i = 0;
176 	void * v = kmalloc(size+sizeof(unsigned int)+2*KMEM_SAFETYZONE*8,prio);
177 	char * c1 = v;
178 	c1 += sizeof(unsigned int);
179 	*((unsigned int *)v) = size;
180 
181 	for (i = 0; i < KMEM_SAFETYZONE; i++) {
182 		c1[0] = 'D'; c1[1] = 'E'; c1[2] = 'A'; c1[3] = 'D';
183 		c1[4] = 'B'; c1[5] = 'E'; c1[6] = 'E'; c1[7] = 'F';
184 		c1 += 8;
185 	}
186 	c1 += size;
187 	for (i = 0; i < KMEM_SAFETYZONE; i++) {
188 		c1[0] = 'M'; c1[1] = 'U'; c1[2] = 'N'; c1[3] = 'G';
189 		c1[4] = 'W'; c1[5] = 'A'; c1[6] = 'L'; c1[7] = 'L';
190 		c1 += 8;
191 	}
192 	v = ((char *)v) + sizeof(unsigned int) + KMEM_SAFETYZONE*8;
193 	printk(KERN_INFO "line %d  kmalloc(%d,%d) = %p\n",line,size,prio,v);
194 	return v;
195 }
196 static void dbg_kfree(void * v, int line) {
197 	unsigned int * sp = (unsigned int *)(((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8));
198 	unsigned int size = *sp;
199 	char * c1 = ((char *)v) - KMEM_SAFETYZONE*8;
200 	int i = 0;
201 	for (i = 0; i < KMEM_SAFETYZONE; i++) {
202 		if (   c1[0] != 'D' || c1[1] != 'E' || c1[2] != 'A' || c1[3] != 'D'
203 		    || c1[4] != 'B' || c1[5] != 'E' || c1[6] != 'E' || c1[7] != 'F') {
204 			printk(KERN_INFO "kmalloced block at %p has been corrupted (underrun)!\n",v);
205 			printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2x\n", i*8,
206 			                c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
207 		}
208 		c1 += 8;
209 	}
210 	c1 += size;
211 	for (i = 0; i < KMEM_SAFETYZONE; i++) {
212 		if (   c1[0] != 'M' || c1[1] != 'U' || c1[2] != 'N' || c1[3] != 'G'
213 		    || c1[4] != 'W' || c1[5] != 'A' || c1[6] != 'L' || c1[7] != 'L'
214 		   ) {
215 			printk(KERN_INFO "kmalloced block at %p has been corrupted (overrun):\n",v);
216 			printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2x\n", i*8,
217 			                c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
218 		}
219 		c1 += 8;
220 	}
221 	printk(KERN_INFO "line %d  kfree(%p)\n",line,v);
222 	v = ((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8);
223 	kfree(v);
224 }
225 
226 #define kmalloc(x,y) dbg_kmalloc(x,y,__LINE__)
227 #define kfree(x) dbg_kfree(x,__LINE__)
228 
229 ==============================================================*/
230 
231 
232 
233 /*===============================================
234  * 	Data Structures
235  *===============================================*/
236 
237 
238 /*========================================================
239  * Name: 	x25_channel
240  *
241  * Purpose:	To hold private informaton for each
242  *              logical channel.
243  *
244  * Rationale:  	Per-channel debugging is possible if each
245  *              channel has its own private area.
246  *
247  * Assumptions:
248  *
249  * Description:	This is an extention of the 'netdevice_t'
250  *              we create for each network interface to keep
251  *              the rest of X.25 channel-specific data.
252  *
253  * Construct:	Typedef
254  */
255 typedef struct x25_channel
256 {
257 	wanpipe_common_t common;	/* common area for x25api and socket */
258 	char name[WAN_IFNAME_SZ+1];	/* interface name, ASCIIZ */
259 	char addr[WAN_ADDRESS_SZ+1];	/* media address, ASCIIZ */
260 	unsigned tx_pkt_size;
261 	unsigned short protocol;	/* ethertype, 0 - multiplexed */
262 	char drop_sequence;		/* mark sequence for dropping */
263 	unsigned long state_tick;	/* time of the last state change */
264 	unsigned idle_timeout;		/* sec, before disconnecting */
265 	unsigned long i_timeout_sofar;  /* # of sec's we've been idle */
266 	unsigned hold_timeout;		/* sec, before re-connecting */
267 	unsigned long tick_counter;	/* counter for transmit time out */
268 	char devtint;			/* Weather we should dev_tint() */
269 	struct sk_buff* rx_skb;		/* receive socket buffer */
270 	struct sk_buff* tx_skb;		/* transmit socket buffer */
271 
272 	bh_data_t *bh_head;	  	  /* Circular buffer for x25api_bh */
273 	unsigned long  tq_working;
274 	volatile int  bh_write;
275 	volatile int  bh_read;
276 	atomic_t  bh_buff_used;
277 
278 	sdla_t* card;			/* -> owner */
279 	netdevice_t *dev;		/* -> bound devce */
280 
281 	int ch_idx;
282 	unsigned char enable_IPX;
283 	unsigned long network_number;
284 #if defined(LINUX_2_1) || defined(LINUX_2_4)
285 	struct net_device_stats ifstats;	/* interface statistics */
286 #else
287 	struct enet_statistics ifstats;
288 #endif
289 	unsigned short transmit_length;
290 	unsigned short tx_offset;
291 	char transmit_buffer[X25_CHAN_MTU+sizeof(x25api_hdr_t)];
292 
293 	if_send_stat_t   if_send_stat;
294         rx_intr_stat_t   rx_intr_stat;
295         pipe_mgmt_stat_t pipe_mgmt_stat;
296 
297 	unsigned long router_start_time; /* Router start time in seconds */
298 	unsigned long router_up_time;
299 
300 } x25_channel_t;
301 
302 /* FIXME Take this out */
303 
304 #ifdef NEX_OLD_CALL_INFO
305 typedef struct x25_call_info
306 {
307 	char dest[17];			/* ASCIIZ destination address */
308 	char src[17];			/* ASCIIZ source address */
309 	char nuser;			/* number of user data bytes */
310 	unsigned char user[127];	/* user data */
311 	char nfacil;			/* number of facilities */
312 	struct
313 	{
314 		unsigned char code;
315 		unsigned char parm;
316 	} facil[64];			        /* facilities */
317 } x25_call_info_t;
318 #else
319 typedef struct x25_call_info
320 {
321 	char dest[MAX_X25_ADDR_SIZE];	/* ASCIIZ destination address */
322 	char src[MAX_X25_ADDR_SIZE];	/* ASCIIZ source address */
323 	unsigned char nuser;
324 	unsigned char user[MAX_X25_DATA_SIZE];/* user data */
325 	unsigned char nfacil;
326 	unsigned char facil[MAX_X25_FACL_SIZE];
327 	unsigned short lcn             		PACKED;
328 } x25_call_info_t;
329 #endif
330 
331 
332 
333 /*===============================================
334  *	Private Function Prototypes
335  *==============================================*/
336 
337 
338 /*=================================================
339  * WAN link driver entry points. These are
340  * called by the WAN router module.
341  */
342 static int update (wan_device_t* wandev);
343 static int new_if (wan_device_t* wandev, netdevice_t* dev,
344 	wanif_conf_t* conf);
345 static int del_if (wan_device_t* wandev, netdevice_t* dev);
346 static void disable_comm (sdla_t* card);
347 static void disable_comm_shutdown(sdla_t *card);
348 
349 
350 
351 /*=================================================
352  *	WANPIPE-specific entry points
353  */
354 static int wpx_exec (struct sdla* card, void* u_cmd, void* u_data);
355 static void x25api_bh (netdevice_t *);
356 static int x25api_bh_cleanup (netdevice_t *);
357 static int bh_enqueue (netdevice_t *, struct sk_buff *);
358 
359 
360 /*=================================================
361  * 	Network device interface
362  */
363 static int if_init   (netdevice_t* dev);
364 static int if_open   (netdevice_t* dev);
365 static int if_close  (netdevice_t* dev);
366 static int if_header (struct sk_buff* skb, netdevice_t* dev,
367 	unsigned short type, void* daddr, void* saddr, unsigned len);
368 static int if_rebuild_hdr (struct sk_buff* skb);
369 static int if_send (struct sk_buff* skb, netdevice_t* dev);
370 static struct net_device_stats *if_stats (netdevice_t* dev);
371 
372 #ifdef LINUX_2_4
373 static void if_tx_timeout (netdevice_t *dev);
374 #endif
375 
376 /*=================================================
377  * 	Interrupt handlers
378  */
379 static void wpx_isr	(sdla_t *);
380 static void rx_intr	(sdla_t *);
381 static void tx_intr	(sdla_t *);
382 static void status_intr	(sdla_t *);
383 static void event_intr	(sdla_t *);
384 static void spur_intr	(sdla_t *);
385 static void timer_intr  (sdla_t *);
386 
387 static int tx_intr_send(sdla_t *, netdevice_t *);
388 static netdevice_t * move_dev_to_next (sdla_t *, netdevice_t *);
389 
390 /*=================================================
391  *	Background polling routines
392  */
393 static void wpx_poll (sdla_t* card);
394 static void poll_disconnected (sdla_t* card);
395 static void poll_connecting (sdla_t* card);
396 static void poll_active (sdla_t* card);
397 static void trigger_x25_poll(sdla_t *card);
398 static void x25_timer_routine(unsigned long data);
399 
400 
401 
402 /*=================================================
403  *	X.25 firmware interface functions
404  */
405 static int x25_get_version (sdla_t* card, char* str);
406 static int x25_configure (sdla_t* card, TX25Config* conf);
407 static int hdlc_configure (sdla_t* card, TX25Config* conf);
408 static int set_hdlc_level (sdla_t* card);
409 static int x25_get_err_stats (sdla_t* card);
410 static int x25_get_stats (sdla_t* card);
411 static int x25_set_intr_mode (sdla_t* card, int mode);
412 static int x25_close_hdlc (sdla_t* card);
413 static int x25_open_hdlc (sdla_t* card);
414 static int x25_setup_hdlc (sdla_t* card);
415 static int x25_set_dtr (sdla_t* card, int dtr);
416 static int x25_get_chan_conf (sdla_t* card, x25_channel_t* chan);
417 static int x25_place_call (sdla_t* card, x25_channel_t* chan);
418 static int x25_accept_call (sdla_t* card, int lcn, int qdm);
419 static int x25_clear_call (sdla_t* card, int lcn, int cause, int diagn);
420 static int x25_send (sdla_t* card, int lcn, int qdm, int len, void* buf);
421 static int x25_fetch_events (sdla_t* card);
422 static int x25_error (sdla_t* card, int err, int cmd, int lcn);
423 
424 /*=================================================
425  *	X.25 asynchronous event handlers
426  */
427 static int incoming_call (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
428 static int call_accepted (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
429 static int call_cleared (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
430 static int timeout_event (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
431 static int restart_event (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
432 
433 
434 /*=================================================
435  *	Miscellaneous functions
436  */
437 static int connect (sdla_t* card);
438 static int disconnect (sdla_t* card);
439 static netdevice_t* get_dev_by_lcn(wan_device_t* wandev, unsigned lcn);
440 static int chan_connect (netdevice_t* dev);
441 static int chan_disc (netdevice_t* dev);
442 static void set_chan_state (netdevice_t* dev, int state);
443 static int chan_send (netdevice_t* , void* , unsigned, unsigned char);
444 static unsigned char bps_to_speed_code (unsigned long bps);
445 static unsigned int dec_to_uint (unsigned char* str, int len);
446 static unsigned int hex_to_uint (unsigned char*, int);
447 static void parse_call_info (unsigned char*, x25_call_info_t*);
448 static netdevice_t * find_channel(sdla_t *, unsigned);
449 static void bind_lcn_to_dev (sdla_t *, netdevice_t *,unsigned);
450 static void setup_for_delayed_transmit (netdevice_t*, void*, unsigned);
451 
452 
453 /*=================================================
454  *      X25 API Functions
455  */
456 static int wanpipe_pull_data_in_skb (sdla_t *, netdevice_t *, struct sk_buff **);
457 static void timer_intr_exec(sdla_t *, unsigned char);
458 static int execute_delayed_cmd (sdla_t*, netdevice_t *, mbox_cmd_t *,char);
459 static int api_incoming_call (sdla_t*, TX25Mbox *, int);
460 static int alloc_and_init_skb_buf (sdla_t *,struct sk_buff **, int);
461 static void send_delayed_cmd_result(sdla_t *, netdevice_t *dev, TX25Mbox*);
462 static int clear_confirm_event (sdla_t *, TX25Mbox*);
463 static void send_oob_msg (sdla_t *, netdevice_t *, TX25Mbox *);
464 static int timer_intr_cmd_exec(sdla_t *card);
465 static void api_oob_event (sdla_t *card,TX25Mbox *mbox);
466 static int check_bad_command (sdla_t *, netdevice_t *);
467 static int channel_disconnect (sdla_t*, netdevice_t *);
468 static void hdlc_link_down (sdla_t*);
469 
470 /*=================================================
471  *     XPIPEMON Functions
472  */
473 static int process_udp_mgmt_pkt(sdla_t *);
474 static int udp_pkt_type( struct sk_buff *, sdla_t*);
475 static int reply_udp( unsigned char *, unsigned int);
476 static void init_x25_channel_struct( x25_channel_t *);
477 static void init_global_statistics( sdla_t *);
478 static int store_udp_mgmt_pkt(int, char, sdla_t*, netdevice_t *, struct sk_buff *, int);
479 static unsigned short calc_checksum (char *, int);
480 
481 
482 
483 /*=================================================
484  *	IPX functions
485  */
486 static void switch_net_numbers(unsigned char *, unsigned long, unsigned char);
487 static int handle_IPXWAN(unsigned char *, char *, unsigned char ,
488 			 unsigned long , unsigned short );
489 
490 extern void disable_irq(unsigned int);
491 extern void enable_irq(unsigned int);
492 
493 static void S508_S514_lock(sdla_t *, unsigned long *);
494 static void S508_S514_unlock(sdla_t *, unsigned long *);
495 
496 
497 /*=================================================
498  * 	Global Variables
499  *=================================================*/
500 
501 
502 
503 /*=================================================
504  *	Public Functions
505  *=================================================*/
506 
507 
508 
509 
510 /*===================================================================
511  * wpx_init:	X.25 Protocol Initialization routine.
512  *
513  * Purpose:	To initialize the protocol/firmware.
514  *
515  * Rationale:	This function is called by setup() function, in
516  *              sdlamain.c, to dynamically setup the x25 protocol.
517  *		This is the first protocol specific function, which
518  *              executes once on startup.
519  *
520  * Description:	This procedure initializes the x25 firmware and
521  *    		sets up the mailbox, transmit and receive buffer
522  *              pointers. It also initializes all debugging structures
523  *              and sets up the X25 environment.
524  *
525  *		Sets up hardware options defined by user in [wanpipe#]
526  *		section of wanpipe#.conf configuration file.
527  *
528  * 		At this point adapter is completely initialized
529  *      	and X.25 firmware is running.
530  *  		o read firmware version (to make sure it's alive)
531  *  		o configure adapter
532  *  		o initialize protocol-specific fields of the
533  *                adapter data space.
534  *
535  * Called by:	setup() function in sdlamain.c
536  *
537  * Assumptions:	None
538  *
539  * Warnings:	None
540  *
541  * Return: 	0	o.k.
542  *	 	< 0	failure.
543  */
544 
wpx_init(sdla_t * card,wandev_conf_t * conf)545 int wpx_init (sdla_t* card, wandev_conf_t* conf)
546 {
547 	union{
548 		char str[80];
549 		TX25Config cfg;
550 	} u;
551 
552 	/* Verify configuration ID */
553 	if (conf->config_id != WANCONFIG_X25){
554 		printk(KERN_INFO "%s: invalid configuration ID %u!\n",
555 			card->devname, conf->config_id)
556 		;
557 		return -EINVAL;
558 	}
559 
560 	/* Initialize protocol-specific fields */
561 	card->mbox  = (void*)(card->hw.dpmbase + X25_MBOX_OFFS);
562 	card->rxmb  = (void*)(card->hw.dpmbase + X25_RXMBOX_OFFS);
563 	card->flags = (void*)(card->hw.dpmbase + X25_STATUS_OFFS);
564 
565 	/* Initialize for S514 Card */
566 	if(card->hw.type == SDLA_S514) {
567 		card->mbox += X25_MB_VECTOR;
568 		card->flags += X25_MB_VECTOR;
569 		card->rxmb += X25_MB_VECTOR;
570 	}
571 
572 
573 	/* Read firmware version.  Note that when adapter initializes, it
574 	 * clears the mailbox, so it may appear that the first command was
575 	 * executed successfully when in fact it was merely erased. To work
576 	 * around this, we execute the first command twice.
577 	 */
578 	if (x25_get_version(card, NULL) || x25_get_version(card, u.str))
579 		return -EIO;
580 
581 
582 	/* X25 firmware can run ether in X25 or LAPB HDLC mode.
583          * Check the user defined option and configure accordingly */
584 	if (conf->u.x25.LAPB_hdlc_only == WANOPT_YES){
585 		if (set_hdlc_level(card) != CMD_OK){
586 			return -EIO;
587 		}else{
588 			printk(KERN_INFO "%s: running LAP_B HDLC firmware v%s\n",
589 				card->devname, u.str);
590 		}
591 		card->u.x.LAPB_hdlc = 1;
592 	}else{
593 		printk(KERN_INFO "%s: running X.25 firmware v%s\n",
594 				card->devname, u.str);
595 		card->u.x.LAPB_hdlc = 0;
596 	}
597 
598 	/* Configure adapter. Here we set resonable defaults, then parse
599 	 * device configuration structure and set configuration options.
600 	 * Most configuration options are verified and corrected (if
601 	 * necessary) since we can't rely on the adapter to do so.
602 	 */
603 	memset(&u.cfg, 0, sizeof(u.cfg));
604 	u.cfg.t1		= 3;
605 	u.cfg.n2		= 10;
606 	u.cfg.autoHdlc		= 1;		/* automatic HDLC connection */
607 	u.cfg.hdlcWindow	= 7;
608 	u.cfg.pktWindow		= 2;
609 	u.cfg.station		= 1;		/* DTE */
610 	u.cfg.options		= 0x0090;	/* disable D-bit pragmatics */
611 	u.cfg.ccittCompat	= 1988;
612 	u.cfg.t10t20		= 30;
613 	u.cfg.t11t21		= 30;
614 	u.cfg.t12t22		= 30;
615 	u.cfg.t13t23		= 30;
616 	u.cfg.t16t26		= 30;
617 	u.cfg.t28		= 30;
618 	u.cfg.r10r20		= 5;
619 	u.cfg.r12r22		= 5;
620 	u.cfg.r13r23		= 5;
621 	u.cfg.responseOpt	= 1;		/* RR's after every packet */
622 
623 	if (card->u.x.LAPB_hdlc){
624 		u.cfg.hdlcMTU = 1027;
625 	}
626 
627 	if (conf->u.x25.x25_conf_opt){
628 		u.cfg.options = conf->u.x25.x25_conf_opt;
629 	}
630 
631 	if (conf->clocking != WANOPT_EXTERNAL)
632 		u.cfg.baudRate = bps_to_speed_code(conf->bps);
633 
634 	if (conf->station != WANOPT_DTE){
635 		u.cfg.station = 0;		/* DCE mode */
636 	}
637 
638         if (conf->interface != WANOPT_RS232 ){
639 	        u.cfg.hdlcOptions |= 0x80;      /* V35 mode */
640 	}
641 
642 	/* adjust MTU */
643 	if (!conf->mtu || (conf->mtu >= 1024))
644 		card->wandev.mtu = 1024;
645 	else if (conf->mtu >= 512)
646 		card->wandev.mtu = 512;
647 	else if (conf->mtu >= 256)
648 		card->wandev.mtu = 256;
649 	else if (conf->mtu >= 128)
650 		card->wandev.mtu = 128;
651 	else
652 		card->wandev.mtu = 64;
653 
654 	u.cfg.defPktSize = u.cfg.pktMTU = card->wandev.mtu;
655 
656 	if (conf->u.x25.hi_pvc){
657 		card->u.x.hi_pvc = min_t(unsigned int, conf->u.x25.hi_pvc, MAX_LCN_NUM);
658 		card->u.x.lo_pvc = min_t(unsigned int, conf->u.x25.lo_pvc, card->u.x.hi_pvc);
659 	}
660 
661 	if (conf->u.x25.hi_svc){
662 		card->u.x.hi_svc = min_t(unsigned int, conf->u.x25.hi_svc, MAX_LCN_NUM);
663 		card->u.x.lo_svc = min_t(unsigned int, conf->u.x25.lo_svc, card->u.x.hi_svc);
664 	}
665 
666 	/* Figure out the total number of channels to configure */
667 	card->u.x.num_of_ch = 0;
668 	if (card->u.x.hi_svc != 0){
669 		card->u.x.num_of_ch = (card->u.x.hi_svc - card->u.x.lo_svc) + 1;
670 	}
671 	if (card->u.x.hi_pvc != 0){
672 		card->u.x.num_of_ch += (card->u.x.hi_pvc - card->u.x.lo_pvc) + 1;
673 	}
674 
675 	if (card->u.x.num_of_ch == 0){
676 		printk(KERN_INFO "%s: ERROR, Minimum number of PVC/SVC channels is 1 !\n"
677 				 "%s: Please set the Lowest/Highest PVC/SVC values !\n",
678 				 card->devname,card->devname);
679 		return -ECHRNG;
680 	}
681 
682 	u.cfg.loPVC = card->u.x.lo_pvc;
683 	u.cfg.hiPVC = card->u.x.hi_pvc;
684 	u.cfg.loTwoWaySVC = card->u.x.lo_svc;
685 	u.cfg.hiTwoWaySVC = card->u.x.hi_svc;
686 
687 	if (conf->u.x25.hdlc_window)
688 		u.cfg.hdlcWindow = min_t(unsigned int, conf->u.x25.hdlc_window, 7);
689 	if (conf->u.x25.pkt_window)
690 		u.cfg.pktWindow = min_t(unsigned int, conf->u.x25.pkt_window, 7);
691 
692 	if (conf->u.x25.t1)
693 		u.cfg.t1 = min_t(unsigned int, conf->u.x25.t1, 30);
694 	if (conf->u.x25.t2)
695 		u.cfg.t2 = min_t(unsigned int, conf->u.x25.t2, 29);
696 	if (conf->u.x25.t4)
697 		u.cfg.t4 = min_t(unsigned int, conf->u.x25.t4, 240);
698 	if (conf->u.x25.n2)
699 		u.cfg.n2 = min_t(unsigned int, conf->u.x25.n2, 30);
700 
701 	if (conf->u.x25.t10_t20)
702 		u.cfg.t10t20 = min_t(unsigned int, conf->u.x25.t10_t20,255);
703 	if (conf->u.x25.t11_t21)
704 		u.cfg.t11t21 = min_t(unsigned int, conf->u.x25.t11_t21,255);
705 	if (conf->u.x25.t12_t22)
706 		u.cfg.t12t22 = min_t(unsigned int, conf->u.x25.t12_t22,255);
707 	if (conf->u.x25.t13_t23)
708 		u.cfg.t13t23 = min_t(unsigned int, conf->u.x25.t13_t23,255);
709 	if (conf->u.x25.t16_t26)
710 		u.cfg.t16t26 = min_t(unsigned int, conf->u.x25.t16_t26, 255);
711 	if (conf->u.x25.t28)
712 		u.cfg.t28 = min_t(unsigned int, conf->u.x25.t28, 255);
713 
714 	if (conf->u.x25.r10_r20)
715 		u.cfg.r10r20 = min_t(unsigned int, conf->u.x25.r10_r20,250);
716 	if (conf->u.x25.r12_r22)
717 		u.cfg.r12r22 = min_t(unsigned int, conf->u.x25.r12_r22,250);
718 	if (conf->u.x25.r13_r23)
719 		u.cfg.r13r23 = min_t(unsigned int, conf->u.x25.r13_r23,250);
720 
721 
722 	if (conf->u.x25.ccitt_compat)
723 		u.cfg.ccittCompat = conf->u.x25.ccitt_compat;
724 
725 	/* initialize adapter */
726 	if (card->u.x.LAPB_hdlc){
727 		if (hdlc_configure(card, &u.cfg) != CMD_OK)
728 			return -EIO;
729 	}else{
730 		if (x25_configure(card, &u.cfg) != CMD_OK)
731 			return -EIO;
732 	}
733 
734 	if ((x25_close_hdlc(card) != CMD_OK) ||		/* close HDLC link */
735 	    (x25_set_dtr(card, 0) != CMD_OK))		/* drop DTR */
736 		return -EIO;
737 
738 	/* Initialize protocol-specific fields of adapter data space */
739 	card->wandev.bps	= conf->bps;
740 	card->wandev.interface	= conf->interface;
741 	card->wandev.clocking	= conf->clocking;
742 	card->wandev.station	= conf->station;
743 	card->isr		= &wpx_isr;
744 	card->poll		= NULL; //&wpx_poll;
745 	card->disable_comm	= &disable_comm;
746 	card->exec		= &wpx_exec;
747 	card->wandev.update	= &update;
748 	card->wandev.new_if	= &new_if;
749 	card->wandev.del_if	= &del_if;
750 
751 	/* WARNING: This function cannot exit with an error
752 	 *          after the change of state */
753 	card->wandev.state	= WAN_DISCONNECTED;
754 
755 	card->wandev.enable_tx_int = 0;
756 	card->irq_dis_if_send_count = 0;
757         card->irq_dis_poll_count = 0;
758 	card->u.x.tx_dev = NULL;
759 	card->u.x.no_dev = 0;
760 
761 
762 	/* Configure for S514 PCI Card */
763 	if (card->hw.type == SDLA_S514) {
764 		card->u.x.hdlc_buf_status =
765 			(volatile unsigned char *)
766 				(card->hw.dpmbase + X25_MB_VECTOR+ X25_MISC_HDLC_BITS);
767 	}else{
768 		card->u.x.hdlc_buf_status =
769 			(volatile unsigned char *)(card->hw.dpmbase + X25_MISC_HDLC_BITS);
770 	}
771 
772 	card->u.x.poll_device=NULL;
773 	card->wandev.udp_port = conf->udp_port;
774 
775 	/* Enable or disable call setup logging */
776 	if (conf->u.x25.logging == WANOPT_YES){
777 		printk(KERN_INFO "%s: Enabling Call Logging.\n",
778 			card->devname);
779 		card->u.x.logging = 1;
780 	}else{
781 		card->u.x.logging = 0;
782 	}
783 
784 	/* Enable or disable modem status reporting */
785 	if (conf->u.x25.oob_on_modem == WANOPT_YES){
786 		printk(KERN_INFO "%s: Enabling OOB on Modem change.\n",
787 			card->devname);
788 		card->u.x.oob_on_modem = 1;
789 	}else{
790 		card->u.x.oob_on_modem = 0;
791 	}
792 
793 	init_global_statistics(card);
794 
795 #ifndef LINUX_2_4
796 	card->u.x.x25_poll_task.next = NULL;
797 #endif
798 	card->u.x.x25_poll_task.sync=0;
799 	card->u.x.x25_poll_task.routine = (void*)(void*)wpx_poll;
800 	card->u.x.x25_poll_task.data = card;
801 
802 	init_timer(&card->u.x.x25_timer);
803 	card->u.x.x25_timer.data = (unsigned long)card;
804 	card->u.x.x25_timer.function = x25_timer_routine;
805 
806 	return 0;
807 }
808 
809 /*=========================================================
810  *	WAN Device Driver Entry Points
811  *========================================================*/
812 
813 /*============================================================
814  * Name:	update(),  Update device status & statistics.
815  *
816  * Purpose:	To provide debugging and statitical
817  *              information to the /proc file system.
818  *              /proc/net/wanrouter/wanpipe#
819  *
820  * Rationale:	The /proc file system is used to collect
821  *              information about the kernel and drivers.
822  *              Using the /proc file system the user
823  *              can see exactly what the sangoma drivers are
824  *              doing. And in what state they are in.
825  *
826  * Description: Collect all driver statistical information
827  *              and pass it to the top laywer.
828  *
829  *		Since we have to execute a debugging command,
830  *              to obtain firmware statitics, we trigger a
831  *              UPDATE function within the timer interrtup.
832  *              We wait until the timer update is complete.
833  *              Once complete return the appropriate return
834  *              code to indicate that the update was successful.
835  *
836  * Called by:	device_stat() in wanmain.c
837  *
838  * Assumptions:
839  *
840  * Warnings:	This function will degrade the performance
841  *              of the router, since it uses the mailbox.
842  *
843  * Return: 	0 	OK
844  * 		<0	Failed (or busy).
845  */
846 
update(wan_device_t * wandev)847 static int update (wan_device_t* wandev)
848 {
849 	volatile sdla_t* card;
850 	TX25Status* status;
851 	unsigned long timeout;
852 
853 	/* sanity checks */
854 	if ((wandev == NULL) || (wandev->private == NULL))
855 		return -EFAULT;
856 
857 	if (wandev->state == WAN_UNCONFIGURED)
858 		return -ENODEV;
859 
860 	if (test_bit(SEND_CRIT, (void*)&wandev->critical))
861 		return -EAGAIN;
862 
863 	if (!wandev->dev)
864 		return -ENODEV;
865 
866 	card = wandev->private;
867 	status = card->flags;
868 
869 	card->u.x.timer_int_enabled |= TMR_INT_ENABLED_UPDATE;
870 	status->imask |= INTR_ON_TIMER;
871 	timeout = jiffies;
872 
873 	for (;;){
874 		if (!(card->u.x.timer_int_enabled & TMR_INT_ENABLED_UPDATE)){
875 			break;
876 		}
877 		if ((jiffies-timeout) > 1*HZ){
878 			card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_UPDATE;
879 			return -EAGAIN;
880 		}
881 	}
882 	return 0;
883 }
884 
885 
886 /*===================================================================
887  * Name:	new_if
888  *
889  * Purpose:	To allocate and initialize resources for a
890  *              new logical channel.
891  *
892  * Rationale:	A new channel can be added dynamically via
893  *              ioctl call.
894  *
895  * Description:	Allocate a private channel structure, x25_channel_t.
896  *		Parse the user interface options from wanpipe#.conf
897  *		configuration file.
898  *		Bind the private are into the network device private
899  *              area pointer (dev->priv).
900  *		Prepare the network device structure for registration.
901  *
902  * Called by:	ROUTER_IFNEW Ioctl call, from wanrouter_ioctl()
903  *              (wanmain.c)
904  *
905  * Assumptions: None
906  *
907  * Warnings:	None
908  *
909  * Return: 	0 	Ok
910  *		<0 	Failed (channel will not be created)
911  */
new_if(wan_device_t * wandev,netdevice_t * dev,wanif_conf_t * conf)912 static int new_if (wan_device_t* wandev, netdevice_t* dev, wanif_conf_t* conf)
913 {
914 	sdla_t* card = wandev->private;
915 	x25_channel_t* chan;
916 	int err = 0;
917 
918 	if ((conf->name[0] == '\0') || (strlen(conf->name) > WAN_IFNAME_SZ)){
919 		printk(KERN_INFO "%s: invalid interface name!\n",
920 			card->devname);
921 		return -EINVAL;
922 	}
923 
924 	if(card->wandev.new_if_cnt++ > 0 && card->u.x.LAPB_hdlc) {
925 		printk(KERN_INFO "%s: Error: Running LAPB HDLC Mode !\n",
926 						card->devname);
927 		printk(KERN_INFO
928 			"%s: Maximum number of network interfaces must be one !\n",
929 						card->devname);
930 		return -EEXIST;
931 	}
932 
933 	/* allocate and initialize private data */
934 	chan = kmalloc(sizeof(x25_channel_t), GFP_ATOMIC);
935 	if (chan == NULL){
936 		return -ENOMEM;
937 	}
938 
939 	memset(chan, 0, sizeof(x25_channel_t));
940 
941 	/* Bug Fix: Seg Err on PVC startup
942 	 * It must be here since bind_lcn_to_dev expects
943 	 * it bellow */
944 	dev->priv = chan;
945 
946 	strcpy(chan->name, conf->name);
947 	chan->card = card;
948 	chan->dev = dev;
949 	chan->common.sk = NULL;
950 	chan->common.func = NULL;
951 	chan->common.rw_bind = 0;
952 	chan->tx_skb = chan->rx_skb = NULL;
953 
954 	/* verify media address */
955 	if (conf->addr[0] == '@'){		/* SVC */
956 		chan->common.svc = 1;
957 		strncpy(chan->addr, &conf->addr[1], WAN_ADDRESS_SZ);
958 
959 		/* Set channel timeouts (default if not specified) */
960 		chan->idle_timeout = (conf->idle_timeout) ?
961 					conf->idle_timeout : 90;
962 		chan->hold_timeout = (conf->hold_timeout) ?
963 					conf->hold_timeout : 10;
964 
965 	}else if (is_digit(conf->addr[0])){	/* PVC */
966 		int lcn = dec_to_uint(conf->addr, 0);
967 
968 		if ((lcn >= card->u.x.lo_pvc) && (lcn <= card->u.x.hi_pvc)){
969 			bind_lcn_to_dev (card, dev, lcn);
970 		}else{
971 			printk(KERN_ERR
972 				"%s: PVC %u is out of range on interface %s!\n",
973 				wandev->name, lcn, chan->name);
974 			err = -EINVAL;
975 		}
976 	}else{
977 		printk(KERN_ERR
978 			"%s: invalid media address on interface %s!\n",
979 			wandev->name, chan->name);
980 		err = -EINVAL;
981 	}
982 
983 	if(strcmp(conf->usedby, "WANPIPE") == 0){
984                 printk(KERN_INFO "%s: Running in WANPIPE mode %s\n",
985 			wandev->name, chan->name);
986                 chan->common.usedby = WANPIPE;
987 		chan->protocol = htons(ETH_P_IP);
988 
989         }else if(strcmp(conf->usedby, "API") == 0){
990 		chan->common.usedby = API;
991                 printk(KERN_INFO "%s: Running in API mode %s\n",
992 			wandev->name, chan->name);
993 		chan->protocol = htons(X25_PROT);
994 	}
995 
996 
997 	if (err){
998 		kfree(chan);
999 		dev->priv = NULL;
1000 		return err;
1001 	}
1002 
1003 	chan->enable_IPX = conf->enable_IPX;
1004 
1005 	if (chan->enable_IPX)
1006 		chan->protocol = htons(ETH_P_IPX);
1007 
1008 	if (conf->network_number)
1009 		chan->network_number = conf->network_number;
1010 	else
1011 		chan->network_number = 0xDEADBEEF;
1012 
1013 	/* prepare network device data space for registration */
1014 #ifdef LINUX_2_4
1015 	strcpy(dev->name,chan->name);
1016 #else
1017 	dev->name = (char *)kmalloc(strlen(chan->name) + 2, GFP_KERNEL);
1018 	if(dev->name == NULL)
1019 	{
1020 		kfree(chan);
1021 		dev->priv = NULL;
1022 		return -ENOMEM;
1023 	}
1024 	sprintf(dev->name, "%s", chan->name);
1025 #endif
1026 	dev->init = &if_init;
1027 
1028 	init_x25_channel_struct(chan);
1029 
1030 	return 0;
1031 }
1032 
1033 /*===================================================================
1034  * Name:	del_if(),  Remove a logical channel.
1035  *
1036  * Purpose:	To dynamically remove a logical channel.
1037  *
1038  * Rationale:	Each logical channel should be dynamically
1039  *              removable. This functin is called by an
1040  *              IOCTL_IFDEL ioctl call or shutdown().
1041  *
1042  * Description: Do nothing.
1043  *
1044  * Called by:	IOCTL_IFDEL : wanrouter_ioctl() from wanmain.c
1045  *              shutdown() from sdlamain.c
1046  *
1047  * Assumptions:
1048  *
1049  * Warnings:
1050  *
1051  * Return: 	0 Ok. Void function.
1052  */
1053 
1054 //FIXME Del IF Should be taken out now.
1055 
del_if(wan_device_t * wandev,netdevice_t * dev)1056 static int del_if (wan_device_t* wandev, netdevice_t* dev)
1057 {
1058 	return 0;
1059 }
1060 
1061 
1062 /*============================================================
1063  * Name:	wpx_exec
1064  *
1065  * Description:	Execute adapter interface command.
1066  * 		This option is currently dissabled.
1067  *===========================================================*/
1068 
wpx_exec(struct sdla * card,void * u_cmd,void * u_data)1069 static int wpx_exec (struct sdla* card, void* u_cmd, void* u_data)
1070 {
1071         return 0;
1072 }
1073 
1074 /*============================================================
1075  * Name:	disable_comm
1076  *
1077  * Description:	Disable communications during shutdown.
1078  *              Dont check return code because there is
1079  *              nothing we can do about it.
1080  *
1081  * Warning:	Dev and private areas are gone at this point.
1082  *===========================================================*/
1083 
disable_comm(sdla_t * card)1084 static void disable_comm(sdla_t* card)
1085 {
1086 	disable_comm_shutdown(card);
1087 	del_timer(&card->u.x.x25_timer);
1088 	return;
1089 }
1090 
1091 
1092 /*============================================================
1093  *	Network Device Interface
1094  *===========================================================*/
1095 
1096 /*===================================================================
1097  * Name:	if_init(),   Netowrk Interface Initialization
1098  *
1099  * Purpose:	To initialize a network interface device structure.
1100  *
1101  * Rationale:	During network interface startup, the if_init
1102  *              is called by the kernel to initialize the
1103  *              netowrk device structure.  Thus a driver
1104  *              can customze a network device.
1105  *
1106  * Description:	Initialize the netowrk device call back
1107  *              routines.  This is where we tell the kernel
1108  *              which function to use when it wants to send
1109  *              via our interface.
1110  *		Furthermore, we initialize the device flags,
1111  *              MTU and physical address of the board.
1112  *
1113  * Called by:	Kernel (/usr/src/linux/net/core/dev.c)
1114  * 		(dev->init())
1115  *
1116  * Assumptions: None
1117  *
1118  * Warnings:	None
1119  *
1120  * Return: 	0 	Ok : Void function.
1121  */
if_init(netdevice_t * dev)1122 static int if_init (netdevice_t* dev)
1123 {
1124 	x25_channel_t* chan = dev->priv;
1125 	sdla_t* card = chan->card;
1126 	wan_device_t* wandev = &card->wandev;
1127 #ifdef LINUX_2_0
1128 	int i;
1129 #endif
1130 
1131 	/* Initialize device driver entry points */
1132 	dev->open		= &if_open;
1133 	dev->stop		= &if_close;
1134 	dev->hard_header	= &if_header;
1135 	dev->rebuild_header	= &if_rebuild_hdr;
1136 	dev->hard_start_xmit	= &if_send;
1137 	dev->get_stats		= &if_stats;
1138 
1139 #ifdef LINUX_2_4
1140 	dev->tx_timeout		= &if_tx_timeout;
1141 	dev->watchdog_timeo	= TX_TIMEOUT;
1142 #endif
1143 
1144 	/* Initialize media-specific parameters */
1145 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1146 	dev->type		= ARPHRD_PPP;		/* ARP h/w type */
1147 #else
1148         dev->family             = AF_INET;      /* address family */
1149         dev->type               = ARPHRD_PPP;   /* no x25 type */
1150 #endif
1151 	dev->flags		|= IFF_POINTOPOINT;
1152 	dev->flags		|= IFF_NOARP;
1153 
1154 	if (chan->common.usedby == API){
1155 		dev->mtu	= X25_CHAN_MTU+sizeof(x25api_hdr_t);
1156 	}else{
1157 		dev->mtu	= card->wandev.mtu;
1158 	}
1159 
1160 	dev->hard_header_len	= X25_HRDHDR_SZ; /* media header length */
1161 	dev->addr_len		= 2;		/* hardware address length */
1162 
1163 	if (!chan->common.svc){
1164 		*(unsigned short*)dev->dev_addr = htons(chan->common.lcn);
1165 	}
1166 
1167 	/* Initialize hardware parameters (just for reference) */
1168 	dev->irq	= wandev->irq;
1169 	dev->dma	= wandev->dma;
1170 	dev->base_addr	= wandev->ioport;
1171 	dev->mem_start	= (unsigned long)wandev->maddr;
1172 	dev->mem_end	= wandev->maddr + wandev->msize - 1;
1173 
1174         /* Set transmit buffer queue length */
1175         dev->tx_queue_len = 100;
1176 
1177 	/* Initialize socket buffers */
1178 #if !defined(LINUX_2_1) && !defined(LINUX_2_4)
1179         for (i = 0; i < DEV_NUMBUFFS; ++i)
1180                 skb_queue_head_init(&dev->buffs[i]);
1181 #endif
1182 	/* FIXME Why are we doing this */
1183 	set_chan_state(dev, WAN_DISCONNECTED);
1184 	return 0;
1185 }
1186 
1187 
1188 /*===================================================================
1189  * Name:	if_open(),   Open/Bring up the Netowrk Interface
1190  *
1191  * Purpose:	To bring up a network interface.
1192  *
1193  * Rationale:
1194  *
1195  * Description:	Open network interface.
1196  * 		o prevent module from unloading by incrementing use count
1197  * 		o if link is disconnected then initiate connection
1198  *
1199  * Called by:	Kernel (/usr/src/linux/net/core/dev.c)
1200  * 		(dev->open())
1201  *
1202  * Assumptions: None
1203  *
1204  * Warnings:	None
1205  *
1206  * Return: 	0 	Ok
1207  * 		<0 	Failur: Interface will not come up.
1208  */
1209 
if_open(netdevice_t * dev)1210 static int if_open (netdevice_t* dev)
1211 {
1212 	x25_channel_t* chan = dev->priv;
1213 	sdla_t* card = chan->card;
1214 	struct timeval tv;
1215 	unsigned long smp_flags;
1216 
1217 	if (is_dev_running(dev))
1218 		return -EBUSY;
1219 
1220 	chan->tq_working = 0;
1221 
1222 	/* Initialize the task queue */
1223 #ifndef LINUX_2_4
1224 	chan->common.wanpipe_task.next = NULL;
1225 #endif
1226 	chan->common.wanpipe_task.sync = 0;
1227 	chan->common.wanpipe_task.routine = (void *)(void *)x25api_bh;
1228 	chan->common.wanpipe_task.data = dev;
1229 
1230 	/* Allocate and initialize BH circular buffer */
1231 	/* Add 1 to MAX_BH_BUFF so we don't have test with (MAX_BH_BUFF-1) */
1232 	chan->bh_head = kmalloc((sizeof(bh_data_t)*(MAX_BH_BUFF+1)),GFP_ATOMIC);
1233 
1234 	if (chan->bh_head == NULL){
1235 		printk(KERN_INFO "%s: ERROR, failed to allocate memory ! BH_BUFFERS !\n",
1236 				card->devname);
1237 
1238 		return -ENOBUFS;
1239 	}
1240 	memset(chan->bh_head,0,(sizeof(bh_data_t)*(MAX_BH_BUFF+1)));
1241 	atomic_set(&chan->bh_buff_used, 0);
1242 
1243 	/* Increment the number of interfaces */
1244 	++card->u.x.no_dev;
1245 
1246 	wanpipe_open(card);
1247 
1248 	/* LAPB protocol only uses one interface, thus
1249 	 * start the protocol after it comes up. */
1250 	if (card->u.x.LAPB_hdlc){
1251 		if (card->open_cnt == 1){
1252 			TX25Status* status = card->flags;
1253 			S508_S514_lock(card, &smp_flags);
1254 			x25_set_intr_mode(card, INTR_ON_TIMER);
1255 			status->imask &= ~INTR_ON_TIMER;
1256 			S508_S514_unlock(card, &smp_flags);
1257 		}
1258 	}else{
1259 		/* X25 can have multiple interfaces thus, start the
1260 		 * protocol once all interfaces are up */
1261 
1262 		//FIXME: There is a bug here. If interface is
1263 		//brought down and up, it will try to enable comm.
1264 		if (card->open_cnt == card->u.x.num_of_ch){
1265 
1266 			S508_S514_lock(card, &smp_flags);
1267 			connect(card);
1268 			S508_S514_unlock(card, &smp_flags);
1269 
1270 			mod_timer(&card->u.x.x25_timer, jiffies + HZ);
1271 		}
1272 	}
1273 	/* Device is not up until the we are in connected state */
1274 	do_gettimeofday( &tv );
1275 	chan->router_start_time = tv.tv_sec;
1276 
1277 #ifdef LINUX_2_4
1278 	netif_start_queue(dev);
1279 #else
1280 	dev->interrupt = 0;
1281 	dev->tbusy = 0;
1282 	dev->start = 1;
1283 #endif
1284 	return 0;
1285 }
1286 
1287 /*===================================================================
1288  * Name:	if_close(),   Close/Bring down the Netowrk Interface
1289  *
1290  * Purpose:	To bring down a network interface.
1291  *
1292  * Rationale:
1293  *
1294  * Description:	Close network interface.
1295  * 		o decrement use module use count
1296  *
1297  * Called by:	Kernel (/usr/src/linux/net/core/dev.c)
1298  * 		(dev->close())
1299  *		ifconfig <name> down: will trigger the kernel
1300  *              which will call this function.
1301  *
1302  * Assumptions: None
1303  *
1304  * Warnings:	None
1305  *
1306  * Return: 	0 	Ok
1307  * 		<0 	Failure: Interface will not exit properly.
1308  */
if_close(netdevice_t * dev)1309 static int if_close (netdevice_t* dev)
1310 {
1311 	x25_channel_t* chan = dev->priv;
1312 	sdla_t* card = chan->card;
1313 	unsigned long smp_flags;
1314 
1315 	stop_net_queue(dev);
1316 #ifndef LINUX_2_4
1317 	dev->start=0;
1318 #endif
1319 
1320 	if ((chan->common.state == WAN_CONNECTED) ||
1321 	    (chan->common.state == WAN_CONNECTING)){
1322 		S508_S514_lock(card, &smp_flags);
1323 		chan_disc(dev);
1324 		S508_S514_unlock(card, &smp_flags);
1325 	}
1326 
1327 	wanpipe_close(card);
1328 
1329 	S508_S514_lock(card, &smp_flags);
1330 	if (chan->bh_head){
1331 		int i;
1332 		struct sk_buff *skb;
1333 
1334 		for (i=0; i<(MAX_BH_BUFF+1); i++){
1335 			skb = ((bh_data_t *)&chan->bh_head[i])->skb;
1336 			if (skb != NULL){
1337                 		wan_dev_kfree_skb(skb, FREE_READ);
1338 			}
1339 		}
1340 		kfree(chan->bh_head);
1341 		chan->bh_head=NULL;
1342 	}
1343 	S508_S514_unlock(card, &smp_flags);
1344 
1345 	/* If this is the last close, disconnect physical link */
1346 	if (!card->open_cnt){
1347 		S508_S514_lock(card, &smp_flags);
1348 		disconnect(card);
1349 		x25_set_intr_mode(card, 0);
1350 		S508_S514_unlock(card, &smp_flags);
1351 	}
1352 
1353 	/* Decrement the number of interfaces */
1354 	--card->u.x.no_dev;
1355 	return 0;
1356 }
1357 
1358 /*======================================================================
1359  * 	Build media header.
1360  * 	o encapsulate packet according to encapsulation type.
1361  *
1362  * 	The trick here is to put packet type (Ethertype) into 'protocol'
1363  *      field of the socket buffer, so that we don't forget it.
1364  *      If encapsulation fails, set skb->protocol to 0 and discard
1365  *      packet later.
1366  *
1367  * 	Return:		media header length.
1368  *======================================================================*/
1369 
if_header(struct sk_buff * skb,netdevice_t * dev,unsigned short type,void * daddr,void * saddr,unsigned len)1370 static int if_header (struct sk_buff* skb, netdevice_t* dev,
1371 	unsigned short type, void* daddr, void* saddr, unsigned len)
1372 {
1373 	x25_channel_t* chan = dev->priv;
1374 	int hdr_len = dev->hard_header_len;
1375 
1376 	skb->protocol = htons(type);
1377 	if (!chan->protocol){
1378 		hdr_len = wanrouter_encapsulate(skb, dev, type);
1379 		if (hdr_len < 0){
1380 			hdr_len = 0;
1381 			skb->protocol = htons(0);
1382 		}
1383 	}
1384 	return hdr_len;
1385 }
1386 
1387 /*===============================================================
1388  * 	Re-build media header.
1389  *
1390  * 	Return:		1	physical address resolved.
1391  *			0	physical address not resolved
1392  *==============================================================*/
1393 
if_rebuild_hdr(struct sk_buff * skb)1394 static int if_rebuild_hdr (struct sk_buff* skb)
1395 {
1396 	netdevice_t *dev = skb->dev;
1397 	x25_channel_t* chan = dev->priv;
1398 	sdla_t* card = chan->card;
1399 
1400 	printk(KERN_INFO "%s: rebuild_header() called for interface %s!\n",
1401 		card->devname, dev->name);
1402 	return 1;
1403 }
1404 
1405 
1406 #ifdef LINUX_2_4
1407 /*============================================================================
1408  * Handle transmit timeout event from netif watchdog
1409  */
if_tx_timeout(netdevice_t * dev)1410 static void if_tx_timeout (netdevice_t *dev)
1411 {
1412     	x25_channel_t* chan = dev->priv;
1413 	sdla_t *card = chan->card;
1414 
1415 	/* If our device stays busy for at least 5 seconds then we will
1416 	 * kick start the device by making dev->tbusy = 0.  We expect
1417 	 * that our device never stays busy more than 5 seconds. So this
1418 	 * is only used as a last resort.
1419 	 */
1420 
1421 	++chan->if_send_stat.if_send_tbusy_timeout;
1422 	printk (KERN_INFO "%s: Transmit timed out on %s\n",
1423 			card->devname, dev->name);
1424 	netif_wake_queue (dev);
1425 }
1426 #endif
1427 
1428 
1429 /*=========================================================================
1430  * 	Send a packet on a network interface.
1431  * 	o set tbusy flag (marks start of the transmission).
1432  * 	o check link state. If link is not up, then drop the packet.
1433  * 	o check channel status. If it's down then initiate a call.
1434  * 	o pass a packet to corresponding WAN device.
1435  * 	o free socket buffer
1436  *
1437  * 	Return:	0	complete (socket buffer must be freed)
1438  *		non-0	packet may be re-transmitted (tbusy must be set)
1439  *
1440  * 	Notes:
1441  * 	1. This routine is called either by the protocol stack or by the "net
1442  *    	bottom half" (with interrupts enabled).
1443  * 	2. Setting tbusy flag will inhibit further transmit requests from the
1444  *    	protocol stack and can be used for flow control with protocol layer.
1445  *
1446  *========================================================================*/
1447 
if_send(struct sk_buff * skb,netdevice_t * dev)1448 static int if_send (struct sk_buff* skb, netdevice_t* dev)
1449 {
1450 	x25_channel_t* chan = dev->priv;
1451 	sdla_t* card = chan->card;
1452 	TX25Status* status = card->flags;
1453 	int udp_type;
1454 	unsigned long smp_flags=0;
1455 
1456 	++chan->if_send_stat.if_send_entry;
1457 
1458 #ifdef LINUX_2_4
1459 	netif_stop_queue(dev);
1460 #endif
1461 
1462 	/* No need to check frame length, since socket code
1463          * will perform the check for us */
1464 
1465 #ifndef LINUX_2_4
1466 	if (dev->tbusy){
1467 		netdevice_t *dev2;
1468 
1469 		++chan->if_send_stat.if_send_tbusy;
1470 		if ((jiffies - chan->tick_counter) < (5*HZ)){
1471 			return 1;
1472 		}
1473 		printk(KERN_INFO "%s: Transmit time out %s!\n",
1474 			card->devname, dev->name);
1475 
1476 		for( dev2 = card->wandev.dev; dev2;
1477 		     dev2 = *((netdevice_t**)dev2->priv)){
1478 
1479 	        	dev2->tbusy = 0;
1480 		}
1481 		++chan->if_send_stat.if_send_tbusy_timeout;
1482 	}
1483 #endif
1484 
1485 	chan->tick_counter = jiffies;
1486 
1487 	/* Critical region starts here */
1488 	S508_S514_lock(card, &smp_flags);
1489 
1490 	if (test_and_set_bit(SEND_CRIT, (void*)&card->wandev.critical)){
1491 		printk(KERN_INFO "Hit critical in if_send()! %lx\n",card->wandev.critical);
1492 		goto if_send_crit_exit;
1493 	}
1494 
1495 	udp_type = udp_pkt_type(skb, card);
1496 
1497         if(udp_type != UDP_INVALID_TYPE) {
1498 
1499                 if(store_udp_mgmt_pkt(udp_type, UDP_PKT_FRM_STACK, card, dev, skb,
1500                         chan->common.lcn)) {
1501 
1502                         status->imask |= INTR_ON_TIMER;
1503                         if (udp_type == UDP_XPIPE_TYPE){
1504                                 chan->if_send_stat.if_send_PIPE_request++;
1505 			}
1506                	}
1507 		start_net_queue(dev);
1508 		clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
1509 		S508_S514_unlock(card, &smp_flags);
1510 		return 0;
1511 	}
1512 
1513 	if (chan->transmit_length){
1514 		//FIXME: This check doesn't make sense any more
1515 		if (chan->common.state != WAN_CONNECTED){
1516 			chan->transmit_length=0;
1517 			atomic_set(&chan->common.driver_busy,0);
1518 		}else{
1519 			stop_net_queue(dev);
1520 			++card->u.x.tx_interrupts_pending;
1521 		        status->imask |= INTR_ON_TX_FRAME;
1522 			clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
1523 			S508_S514_unlock(card, &smp_flags);
1524 			return 1;
1525 		}
1526 	}
1527 
1528 	if (card->wandev.state != WAN_CONNECTED){
1529 		++chan->ifstats.tx_dropped;
1530 		++card->wandev.stats.tx_dropped;
1531 		++chan->if_send_stat.if_send_wan_disconnected;
1532 
1533 	}else if ( chan->protocol && (chan->protocol != skb->protocol)){
1534 		printk(KERN_INFO
1535 			"%s: unsupported Ethertype 0x%04X on interface %s!\n",
1536 			chan->name, htons(skb->protocol), dev->name);
1537 
1538 		printk(KERN_INFO "PROTO %Xn", htons(chan->protocol));
1539 		++chan->ifstats.tx_errors;
1540 		++chan->ifstats.tx_dropped;
1541 		++card->wandev.stats.tx_dropped;
1542 		++chan->if_send_stat.if_send_protocol_error;
1543 
1544 	}else switch (chan->common.state){
1545 
1546 		case WAN_DISCONNECTED:
1547 			/* Try to establish connection. If succeded, then start
1548 			 * transmission, else drop a packet.
1549 			 */
1550 			if (chan->common.usedby == API){
1551 				++chan->ifstats.tx_dropped;
1552 				++card->wandev.stats.tx_dropped;
1553 				break;
1554 			}else{
1555 				if (chan_connect(dev) != 0){
1556 					++chan->ifstats.tx_dropped;
1557 					++card->wandev.stats.tx_dropped;
1558 					break;
1559 				}
1560 			}
1561 			/* fall through */
1562 
1563 		case WAN_CONNECTED:
1564 			if( skb->protocol == htons(ETH_P_IPX)) {
1565 				if(chan->enable_IPX) {
1566 					switch_net_numbers( skb->data,
1567 						chan->network_number, 0);
1568 				} else {
1569 					++card->wandev.stats.tx_dropped;
1570 					++chan->ifstats.tx_dropped;
1571 					++chan->if_send_stat.if_send_protocol_error;
1572 					goto if_send_crit_exit;
1573 				}
1574 			}
1575 			/* We never drop here, if cannot send than, copy
1576 	                 * a packet into a transmit buffer
1577                          */
1578 			chan_send(dev, skb->data, skb->len, 0);
1579 			break;
1580 
1581 		default:
1582 			++chan->ifstats.tx_dropped;
1583 			++card->wandev.stats.tx_dropped;
1584 			break;
1585 	}
1586 
1587 
1588 if_send_crit_exit:
1589 
1590        	wan_dev_kfree_skb(skb, FREE_WRITE);
1591 
1592 	start_net_queue(dev);
1593 	clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
1594 	S508_S514_unlock(card, &smp_flags);
1595 	return 0;
1596 }
1597 
1598 /*============================================================================
1599  * Setup so that a frame can be transmitted on the occurence of a transmit
1600  * interrupt.
1601  *===========================================================================*/
1602 
setup_for_delayed_transmit(netdevice_t * dev,void * buf,unsigned len)1603 static void setup_for_delayed_transmit (netdevice_t* dev, void* buf,
1604 	unsigned len)
1605 {
1606         x25_channel_t* chan = dev->priv;
1607         sdla_t* card = chan->card;
1608 	TX25Status* status = card->flags;
1609 
1610 	++chan->if_send_stat.if_send_adptr_bfrs_full;
1611 
1612         if(chan->transmit_length) {
1613                 printk(KERN_INFO "%s: Error, transmit length set in delayed transmit!\n",
1614 				card->devname);
1615                 return;
1616         }
1617 
1618 	if (chan->common.usedby == API){
1619 		if (len > X25_CHAN_MTU+sizeof(x25api_hdr_t)) {
1620 			++chan->ifstats.tx_dropped;
1621 			++card->wandev.stats.tx_dropped;
1622 			printk(KERN_INFO "%s: Length is too big for delayed transmit\n",
1623 				card->devname);
1624 			return;
1625 		}
1626 	}else{
1627 		if (len > X25_MAX_DATA) {
1628 			++chan->ifstats.tx_dropped;
1629 			++card->wandev.stats.tx_dropped;
1630 			printk(KERN_INFO "%s: Length is too big for delayed transmit\n",
1631 				card->devname);
1632 			return;
1633 		}
1634 	}
1635 
1636         chan->transmit_length = len;
1637 	atomic_set(&chan->common.driver_busy,1);
1638         memcpy(chan->transmit_buffer, buf, len);
1639 
1640 	++chan->if_send_stat.if_send_tx_int_enabled;
1641 
1642 	/* Enable Transmit Interrupt */
1643 	++card->u.x.tx_interrupts_pending;
1644         status->imask |= INTR_ON_TX_FRAME;
1645 }
1646 
1647 
1648 /*===============================================================
1649  * net_device_stats
1650  *
1651  * 	Get ethernet-style interface statistics.
1652  * 	Return a pointer to struct enet_statistics.
1653  *
1654  *==============================================================*/
if_stats(netdevice_t * dev)1655 static struct net_device_stats *if_stats (netdevice_t* dev)
1656 {
1657 	x25_channel_t *chan = dev->priv;
1658 
1659 	if(chan == NULL)
1660 		return NULL;
1661 
1662 	return &chan->ifstats;
1663 }
1664 
1665 
1666 /*
1667  *	Interrupt Handlers
1668  */
1669 
1670 /*
1671  * X.25 Interrupt Service Routine.
1672  */
1673 
wpx_isr(sdla_t * card)1674 static void wpx_isr (sdla_t* card)
1675 {
1676 	TX25Status* status = card->flags;
1677 
1678 	card->in_isr = 1;
1679 	++card->statistics.isr_entry;
1680 
1681 	if (test_bit(PERI_CRIT,(void*)&card->wandev.critical)){
1682 		card->in_isr=0;
1683 		status->iflags = 0;
1684 		return;
1685 	}
1686 
1687 	if (test_bit(SEND_CRIT, (void*)&card->wandev.critical)){
1688 
1689  		printk(KERN_INFO "%s: wpx_isr: wandev.critical set to 0x%02lx, int type = 0x%02x\n",
1690 			card->devname, card->wandev.critical, status->iflags);
1691 		card->in_isr = 0;
1692 		status->iflags = 0;
1693 		return;
1694 	}
1695 
1696 	/* For all interrupts set the critical flag to CRITICAL_RX_INTR.
1697          * If the if_send routine is called with this flag set it will set
1698          * the enable transmit flag to 1. (for a delayed interrupt)
1699          */
1700 	switch (status->iflags){
1701 
1702 		case RX_INTR_PENDING:		/* receive interrupt */
1703 			rx_intr(card);
1704 			break;
1705 
1706 		case TX_INTR_PENDING:		/* transmit interrupt */
1707 			tx_intr(card);
1708 			break;
1709 
1710 		case MODEM_INTR_PENDING:	/* modem status interrupt */
1711 			status_intr(card);
1712 			break;
1713 
1714 		case X25_ASY_TRANS_INTR_PENDING:	/* network event interrupt */
1715 			event_intr(card);
1716 			break;
1717 
1718 		case TIMER_INTR_PENDING:
1719 			timer_intr(card);
1720 			break;
1721 
1722 		default:		/* unwanted interrupt */
1723 			spur_intr(card);
1724 	}
1725 
1726 	card->in_isr = 0;
1727 	status->iflags = 0;	/* clear interrupt condition */
1728 }
1729 
1730 /*
1731  * 	Receive interrupt handler.
1732  * 	This routine handles fragmented IP packets using M-bit according to the
1733  * 	RFC1356.
1734  * 	o map ligical channel number to network interface.
1735  * 	o allocate socket buffer or append received packet to the existing one.
1736  * 	o if M-bit is reset (i.e. it's the last packet in a sequence) then
1737  *   	decapsulate packet and pass socket buffer to the protocol stack.
1738  *
1739  * 	Notes:
1740  * 	1. When allocating a socket buffer, if M-bit is set then more data is
1741  *    	coming and we have to allocate buffer for the maximum IP packet size
1742  *    	expected on this channel.
1743  * 	2. If something goes wrong and X.25 packet has to be dropped (e.g. no
1744  *    	socket buffers available) the whole packet sequence must be discarded.
1745  */
1746 
rx_intr(sdla_t * card)1747 static void rx_intr (sdla_t* card)
1748 {
1749 	TX25Mbox* rxmb = card->rxmb;
1750 	unsigned lcn = rxmb->cmd.lcn;
1751 	netdevice_t* dev = find_channel(card,lcn);
1752 	x25_channel_t* chan;
1753 	struct sk_buff* skb=NULL;
1754 
1755 	if (dev == NULL){
1756 		/* Invalid channel, discard packet */
1757 		printk(KERN_INFO "%s: receiving on orphaned LCN %d!\n",
1758 			card->devname, lcn);
1759 		return;
1760 	}
1761 
1762 	chan = dev->priv;
1763 	chan->i_timeout_sofar = jiffies;
1764 
1765 
1766 	/* Copy the data from the board, into an
1767          * skb buffer
1768 	 */
1769 	if (wanpipe_pull_data_in_skb(card,dev,&skb)){
1770 		++chan->ifstats.rx_dropped;
1771 		++card->wandev.stats.rx_dropped;
1772 		++chan->rx_intr_stat.rx_intr_no_socket;
1773 		++chan->rx_intr_stat.rx_intr_bfr_not_passed_to_stack;
1774 		return;
1775 	}
1776 
1777 	dev->last_rx = jiffies;		/* timestamp */
1778 
1779 
1780 	/* ------------ API ----------------*/
1781 
1782 	if (chan->common.usedby == API){
1783 
1784 		if (bh_enqueue(dev, skb)){
1785 			++chan->ifstats.rx_dropped;
1786 			++card->wandev.stats.rx_dropped;
1787 			++chan->rx_intr_stat.rx_intr_bfr_not_passed_to_stack;
1788 			wan_dev_kfree_skb(skb, FREE_READ);
1789 			return;
1790 		}
1791 
1792 		++chan->ifstats.rx_packets;
1793 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1794 		chan->ifstats.rx_bytes += skb->len;
1795 #endif
1796 
1797 
1798 		chan->rx_skb = NULL;
1799 		if (!test_and_set_bit(0, &chan->tq_working)){
1800 			wanpipe_queue_tq(&chan->common.wanpipe_task);
1801 			wanpipe_mark_bh();
1802 		}
1803 		return;
1804 	}
1805 
1806 
1807 	/* ------------- WANPIPE -------------------*/
1808 
1809 	/* set rx_skb to NULL so we won't access it later when kernel already owns it */
1810 	chan->rx_skb=NULL;
1811 
1812 	/* Decapsulate packet, if necessary */
1813 	if (!skb->protocol && !wanrouter_type_trans(skb, dev)){
1814 		/* can't decapsulate packet */
1815                 wan_dev_kfree_skb(skb, FREE_READ);
1816 		++chan->ifstats.rx_errors;
1817 		++chan->ifstats.rx_dropped;
1818 		++card->wandev.stats.rx_dropped;
1819 		++chan->rx_intr_stat.rx_intr_bfr_not_passed_to_stack;
1820 
1821 	}else{
1822 		if( handle_IPXWAN(skb->data, chan->name,
1823 				  chan->enable_IPX, chan->network_number,
1824 				  skb->protocol)){
1825 
1826 			if( chan->enable_IPX ){
1827 				if(chan_send(dev, skb->data, skb->len,0)){
1828 					chan->tx_skb = skb;
1829 				}else{
1830                                         wan_dev_kfree_skb(skb, FREE_WRITE);
1831 					++chan->rx_intr_stat.rx_intr_bfr_not_passed_to_stack;
1832 				}
1833 			}else{
1834 				/* increment IPX packet dropped statistic */
1835 				++chan->ifstats.rx_dropped;
1836 				++chan->rx_intr_stat.rx_intr_bfr_not_passed_to_stack;
1837 			}
1838 		}else{
1839 			skb->mac.raw = skb->data;
1840 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1841 			chan->ifstats.rx_bytes += skb->len;
1842 #endif
1843 			++chan->ifstats.rx_packets;
1844 			++chan->rx_intr_stat.rx_intr_bfr_passed_to_stack;
1845 			netif_rx(skb);
1846 		}
1847 	}
1848 
1849 	return;
1850 }
1851 
1852 
wanpipe_pull_data_in_skb(sdla_t * card,netdevice_t * dev,struct sk_buff ** skb)1853 static int wanpipe_pull_data_in_skb (sdla_t *card, netdevice_t *dev, struct sk_buff **skb)
1854 {
1855 	void *bufptr;
1856 	TX25Mbox* rxmb = card->rxmb;
1857 	unsigned len = rxmb->cmd.length;	/* packet length */
1858 	unsigned qdm = rxmb->cmd.qdm;		/* Q,D and M bits */
1859 	x25_channel_t *chan = dev->priv;
1860 	struct sk_buff *new_skb = *skb;
1861 
1862 	if (chan->common.usedby == WANPIPE){
1863 		if (chan->drop_sequence){
1864 			if (!(qdm & 0x01)){
1865 				chan->drop_sequence = 0;
1866 			}
1867 			return 1;
1868 		}
1869 		new_skb = chan->rx_skb;
1870 	}else{
1871 		/* Add on the API header to the received
1872                  * data
1873 		 */
1874 		len += sizeof(x25api_hdr_t);
1875 	}
1876 
1877 	if (new_skb == NULL){
1878 		int bufsize;
1879 
1880 		if (chan->common.usedby == WANPIPE){
1881 			bufsize = (qdm & 0x01) ? dev->mtu : len;
1882 		}else{
1883 			bufsize = len;
1884 		}
1885 
1886 		/* Allocate new socket buffer */
1887 		new_skb = dev_alloc_skb(bufsize + dev->hard_header_len);
1888 		if (new_skb == NULL){
1889 			printk(KERN_INFO "%s: no socket buffers available!\n",
1890 				card->devname);
1891 			chan->drop_sequence = 1;	/* set flag */
1892 			++chan->ifstats.rx_dropped;
1893 			return 1;
1894 		}
1895 	}
1896 
1897 	if (skb_tailroom(new_skb) < len){
1898 		/* No room for the packet. Call off the whole thing! */
1899                 wan_dev_kfree_skb(new_skb, FREE_READ);
1900 		if (chan->common.usedby == WANPIPE){
1901 			chan->rx_skb = NULL;
1902 			if (qdm & 0x01){
1903 				chan->drop_sequence = 1;
1904 			}
1905 		}
1906 
1907 		printk(KERN_INFO "%s: unexpectedly long packet sequence "
1908 			"on interface %s!\n", card->devname, dev->name);
1909 		++chan->ifstats.rx_length_errors;
1910 		return 1;
1911 	}
1912 
1913 	bufptr = skb_put(new_skb,len);
1914 
1915 
1916 	if (chan->common.usedby == API){
1917 		/* Fill in the x25api header
1918 		 */
1919 		x25api_t * api_data = (x25api_t*)bufptr;
1920 		api_data->hdr.qdm = rxmb->cmd.qdm;
1921 		api_data->hdr.cause = rxmb->cmd.cause;
1922 		api_data->hdr.diagn = rxmb->cmd.diagn;
1923 		api_data->hdr.length = rxmb->cmd.length;
1924 		memcpy(api_data->data, rxmb->data, rxmb->cmd.length);
1925 	}else{
1926 		memcpy(bufptr, rxmb->data, len);
1927 	}
1928 
1929 	new_skb->dev = dev;
1930 
1931 	if (chan->common.usedby == API){
1932 		new_skb->mac.raw = new_skb->data;
1933 		new_skb->protocol = htons(X25_PROT);
1934 		new_skb->pkt_type = WAN_PACKET_DATA;
1935 	}else{
1936 		new_skb->protocol = chan->protocol;
1937 		chan->rx_skb = new_skb;
1938 	}
1939 
1940 	/* If qdm bit is set, more data is coming
1941          * thus, exit and wait for more data before
1942          * sending the packet up. (Used by router only)
1943 	 */
1944 	if ((qdm & 0x01) && (chan->common.usedby == WANPIPE))
1945 		return 1;
1946 
1947 	*skb = new_skb;
1948 
1949 	return 0;
1950 }
1951 
1952 /*===============================================================
1953  * tx_intr
1954  *
1955  * 	Transmit interrupt handler.
1956  *	For each dev, check that there is something to send.
1957  *	If data available, transmit.
1958  *
1959  *===============================================================*/
1960 
tx_intr(sdla_t * card)1961 static void tx_intr (sdla_t* card)
1962 {
1963 	netdevice_t *dev;
1964 	TX25Status* status = card->flags;
1965 	unsigned char more_to_tx=0;
1966 	x25_channel_t *chan=NULL;
1967 	int i=0;
1968 
1969 	if (card->u.x.tx_dev == NULL){
1970 		card->u.x.tx_dev = card->wandev.dev;
1971 	}
1972 
1973 	dev = card->u.x.tx_dev;
1974 
1975 	for (;;){
1976 
1977 		chan = dev->priv;
1978 		if (chan->transmit_length){
1979 			/* Device was set to transmit, check if the TX
1980                          * buffers are available
1981 			 */
1982 			if (chan->common.state != WAN_CONNECTED){
1983 				chan->transmit_length = 0;
1984 				atomic_set(&chan->common.driver_busy,0);
1985 				chan->tx_offset=0;
1986 				if (is_queue_stopped(dev)){
1987 					if (chan->common.usedby == API){
1988 						start_net_queue(dev);
1989 						wakeup_sk_bh(dev);
1990 					}else{
1991 						wake_net_dev(dev);
1992 					}
1993 				}
1994 				dev = move_dev_to_next(card,dev);
1995 				break;
1996 			}
1997 
1998 			if ((status->cflags[chan->ch_idx] & 0x40 || card->u.x.LAPB_hdlc) &&
1999 			     (*card->u.x.hdlc_buf_status & 0x40) ){
2000 				/* Tx buffer available, we can send */
2001 
2002 				if (tx_intr_send(card, dev)){
2003 					more_to_tx=1;
2004 				}
2005 
2006 				/* If more than one interface present, move the
2007                                  * device pointer to the next interface, so on the
2008                                  * next TX interrupt we will try sending from it.
2009                                  */
2010 				dev = move_dev_to_next(card,dev);
2011 				break;
2012 			}else{
2013 				/* Tx buffers not available, but device set
2014                                  * the TX interrupt.  Set more_to_tx and try
2015                                  * to transmit for other devices.
2016 				 */
2017 				more_to_tx=1;
2018 				dev = move_dev_to_next(card,dev);
2019 			}
2020 
2021 		}else{
2022 			/* This device was not set to transmit,
2023                          * go to next
2024 			 */
2025 			dev = move_dev_to_next(card,dev);
2026 		}
2027 
2028 		if (++i == card->u.x.no_dev){
2029 			if (!more_to_tx){
2030 				DBG_PRINTK(KERN_INFO "%s: Nothing to Send in TX INTR\n",
2031 					card->devname);
2032 			}
2033 			break;
2034 		}
2035 
2036 	} //End of FOR
2037 
2038 	card->u.x.tx_dev = dev;
2039 
2040 	if (!more_to_tx){
2041 		/* if any other interfaces have transmit interrupts pending, */
2042 		/* do not disable the global transmit interrupt */
2043 		if (!(--card->u.x.tx_interrupts_pending)){
2044 			status->imask &= ~INTR_ON_TX_FRAME;
2045 		}
2046 	}
2047 	return;
2048 }
2049 
2050 /*===============================================================
2051  * move_dev_to_next
2052  *
2053  *
2054  *===============================================================*/
2055 
2056 
move_dev_to_next(sdla_t * card,netdevice_t * dev)2057 netdevice_t * move_dev_to_next (sdla_t *card, netdevice_t *dev)
2058 {
2059 	if (card->u.x.no_dev != 1){
2060 		if (*((netdevice_t**)dev->priv) == NULL){
2061 			return card->wandev.dev;
2062 		}else{
2063 			return *((netdevice_t**)dev->priv);
2064 		}
2065 	}
2066 	return dev;
2067 }
2068 
2069 /*===============================================================
2070  *  tx_intr_send
2071  *
2072  *
2073  *===============================================================*/
2074 
tx_intr_send(sdla_t * card,netdevice_t * dev)2075 static int tx_intr_send(sdla_t *card, netdevice_t *dev)
2076 {
2077 	x25_channel_t* chan = dev->priv;
2078 
2079 	if (chan_send (dev,chan->transmit_buffer,chan->transmit_length,1)){
2080 
2081                 /* Packet was split up due to its size, do not disable
2082                  * tx_intr
2083                  */
2084 		return 1;
2085 	}
2086 
2087 	chan->transmit_length=0;
2088 	atomic_set(&chan->common.driver_busy,0);
2089 	chan->tx_offset=0;
2090 
2091 	/* If we are in API mode, wakeup the
2092          * sock BH handler, not the NET_BH */
2093 	if (is_queue_stopped(dev)){
2094 		if (chan->common.usedby == API){
2095 			start_net_queue(dev);
2096 			wakeup_sk_bh(dev);
2097 		}else{
2098 			wake_net_dev(dev);
2099 		}
2100 	}
2101 	return 0;
2102 }
2103 
2104 
2105 /*===============================================================
2106  * timer_intr
2107  *
2108  * 	Timer interrupt handler.
2109  *	Check who called the timer interrupt and perform
2110  *      action accordingly.
2111  *
2112  *===============================================================*/
2113 
timer_intr(sdla_t * card)2114 static void timer_intr (sdla_t *card)
2115 {
2116 	TX25Status* status = card->flags;
2117 
2118 	if (card->u.x.timer_int_enabled & TMR_INT_ENABLED_CMD_EXEC){
2119 
2120 		if (timer_intr_cmd_exec(card) == 0){
2121 			card->u.x.timer_int_enabled &=
2122 				~TMR_INT_ENABLED_CMD_EXEC;
2123 		}
2124 
2125 	}else  if(card->u.x.timer_int_enabled & TMR_INT_ENABLED_UDP_PKT) {
2126 
2127 		if ((*card->u.x.hdlc_buf_status & 0x40) &&
2128 		    card->u.x.udp_type == UDP_XPIPE_TYPE){
2129 
2130                     	if(process_udp_mgmt_pkt(card)) {
2131 		                card->u.x.timer_int_enabled &=
2132 					~TMR_INT_ENABLED_UDP_PKT;
2133 			}
2134 		}
2135 
2136 	}else if (card->u.x.timer_int_enabled & TMR_INT_ENABLED_POLL_ACTIVE) {
2137 
2138 		netdevice_t *dev = card->u.x.poll_device;
2139 		x25_channel_t *chan = NULL;
2140 
2141 		if (!dev){
2142 			card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_POLL_ACTIVE;
2143 			return;
2144 		}
2145 		chan = dev->priv;
2146 
2147 		printk(KERN_INFO
2148 			"%s: Closing down Idle link %s on LCN %d\n",
2149 					card->devname,chan->name,chan->common.lcn);
2150 		chan->i_timeout_sofar = jiffies;
2151 		chan_disc(dev);
2152          	card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_POLL_ACTIVE;
2153 		card->u.x.poll_device=NULL;
2154 
2155 	}else if (card->u.x.timer_int_enabled & TMR_INT_ENABLED_POLL_CONNECT_ON) {
2156 
2157 		wanpipe_set_state(card, WAN_CONNECTED);
2158 		if (card->u.x.LAPB_hdlc){
2159 			netdevice_t *dev = card->wandev.dev;
2160 			set_chan_state(dev,WAN_CONNECTED);
2161 			send_delayed_cmd_result(card,dev,card->mbox);
2162 		}
2163 
2164 		/* 0x8F enable all interrupts */
2165 		x25_set_intr_mode(card, INTR_ON_RX_FRAME|
2166 					INTR_ON_TX_FRAME|
2167 					INTR_ON_MODEM_STATUS_CHANGE|
2168 					//INTR_ON_COMMAND_COMPLETE|
2169 					X25_ASY_TRANS_INTR_PENDING |
2170 					INTR_ON_TIMER |
2171 					DIRECT_RX_INTR_USAGE
2172 				);
2173 
2174 		status->imask &= ~INTR_ON_TX_FRAME;	/* mask Tx interrupts */
2175 		card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_POLL_CONNECT_ON;
2176 
2177 	}else if (card->u.x.timer_int_enabled & TMR_INT_ENABLED_POLL_CONNECT_OFF) {
2178 
2179 		//printk(KERN_INFO "Poll connect, Turning OFF\n");
2180 		disconnect(card);
2181 		card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_POLL_CONNECT_OFF;
2182 
2183 	}else if (card->u.x.timer_int_enabled & TMR_INT_ENABLED_POLL_DISCONNECT) {
2184 
2185 		//printk(KERN_INFO "POll disconnect, trying to connect\n");
2186 		connect(card);
2187 		card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_POLL_DISCONNECT;
2188 
2189 	}else if (card->u.x.timer_int_enabled & TMR_INT_ENABLED_UPDATE){
2190 
2191 		if (*card->u.x.hdlc_buf_status & 0x40){
2192 			x25_get_err_stats(card);
2193 			x25_get_stats(card);
2194 			card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_UPDATE;
2195 		}
2196 	}
2197 
2198 	if(!card->u.x.timer_int_enabled){
2199 		//printk(KERN_INFO "Turning Timer Off \n");
2200                 status->imask &= ~INTR_ON_TIMER;
2201 	}
2202 }
2203 
2204 /*====================================================================
2205  * 	Modem status interrupt handler.
2206  *===================================================================*/
status_intr(sdla_t * card)2207 static void status_intr (sdla_t* card)
2208 {
2209 
2210 	/* Added to avoid Modem status message flooding */
2211 	static TX25ModemStatus last_stat;
2212 
2213 	TX25Mbox* mbox = card->mbox;
2214 	TX25ModemStatus *modem_status;
2215 	netdevice_t *dev;
2216 	x25_channel_t *chan;
2217 	int err;
2218 
2219 	memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2220 	mbox->cmd.command = X25_READ_MODEM_STATUS;
2221 	err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2222 	if (err){
2223 		x25_error(card, err, X25_READ_MODEM_STATUS, 0);
2224 	}else{
2225 
2226 		modem_status = (TX25ModemStatus*)mbox->data;
2227 
2228            	/* Check if the last status was the same
2229            	 * if it was, do NOT print message again */
2230 
2231 		if (last_stat.status != modem_status->status){
2232 
2233 	     		printk(KERN_INFO "%s: Modem Status Change: DCD=%s, CTS=%s\n",
2234 				card->devname,DCD(modem_status->status),CTS(modem_status->status));
2235 
2236 			last_stat.status = modem_status->status;
2237 
2238 			if (card->u.x.oob_on_modem){
2239 
2240 				mbox->cmd.pktType = mbox->cmd.command;
2241 				mbox->cmd.result = 0x08;
2242 
2243 				/* Send a OOB to all connected sockets */
2244 				for (dev = card->wandev.dev; dev; dev = *((netdevice_t**)dev->priv)){
2245 					chan=dev->priv;
2246 					if (chan->common.usedby == API){
2247 						send_oob_msg(card,dev,mbox);
2248 					}
2249 				}
2250 
2251 				/* The modem OOB message will probably kill the
2252 				 * the link. If we don't clear the flag here,
2253 				 * a deadlock could occur */
2254 				if (atomic_read(&card->u.x.command_busy)){
2255 					atomic_set(&card->u.x.command_busy,0);
2256 				}
2257 			}
2258 		}
2259 	}
2260 
2261 	memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2262 	mbox->cmd.command = X25_HDLC_LINK_STATUS;
2263 	err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2264 	if (err){
2265 		x25_error(card, err, X25_HDLC_LINK_STATUS, 0);
2266 	}
2267 
2268 }
2269 
2270 /*====================================================================
2271  * 	Network event interrupt handler.
2272  *===================================================================*/
event_intr(sdla_t * card)2273 static void event_intr (sdla_t* card)
2274 {
2275 	x25_fetch_events(card);
2276 }
2277 
2278 /*====================================================================
2279  * 	Spurious interrupt handler.
2280  * 	o print a warning
2281  * 	o
2282  *====================================================================*/
2283 
spur_intr(sdla_t * card)2284 static void spur_intr (sdla_t* card)
2285 {
2286 	printk(KERN_INFO "%s: spurious interrupt!\n", card->devname);
2287 }
2288 
2289 
2290 /*
2291  *	Background Polling Routines
2292  */
2293 
2294 /*====================================================================
2295  * 	Main polling routine.
2296  * 	This routine is repeatedly called by the WANPIPE 'thead' to allow for
2297  * 	time-dependent housekeeping work.
2298  *
2299  * 	Notes:
2300  * 	1. This routine may be called on interrupt context with all interrupts
2301  *    	enabled. Beware!
2302  *====================================================================*/
2303 
wpx_poll(sdla_t * card)2304 static void wpx_poll (sdla_t *card)
2305 {
2306 	if (!card->wandev.dev){
2307 		goto wpx_poll_exit;
2308 	}
2309 
2310 	if (card->open_cnt != card->u.x.num_of_ch){
2311 		goto wpx_poll_exit;
2312 	}
2313 
2314 	if (test_bit(PERI_CRIT,&card->wandev.critical)){
2315 		goto wpx_poll_exit;
2316 	}
2317 
2318 	if (test_bit(SEND_CRIT,&card->wandev.critical)){
2319 		goto wpx_poll_exit;
2320 	}
2321 
2322 	switch(card->wandev.state){
2323 		case WAN_CONNECTED:
2324 			poll_active(card);
2325 			break;
2326 
2327 		case WAN_CONNECTING:
2328 			poll_connecting(card);
2329 			break;
2330 
2331 		case WAN_DISCONNECTED:
2332 			poll_disconnected(card);
2333 			break;
2334 	}
2335 
2336 wpx_poll_exit:
2337 	clear_bit(POLL_CRIT,&card->wandev.critical);
2338 	return;
2339 }
2340 
trigger_x25_poll(sdla_t * card)2341 static void trigger_x25_poll(sdla_t *card)
2342 {
2343 #ifdef LINUX_2_4
2344 	schedule_task(&card->u.x.x25_poll_task);
2345 #else
2346 	queue_task(&card->u.x.x25_poll_task, &tq_scheduler);
2347 #endif
2348 }
2349 
2350 /*====================================================================
2351  * 	Handle physical link establishment phase.
2352  * 	o if connection timed out, disconnect the link.
2353  *===================================================================*/
2354 
poll_connecting(sdla_t * card)2355 static void poll_connecting (sdla_t* card)
2356 {
2357 	volatile TX25Status* status = card->flags;
2358 
2359 	if (status->gflags & X25_HDLC_ABM){
2360 
2361 		timer_intr_exec (card, TMR_INT_ENABLED_POLL_CONNECT_ON);
2362 
2363 	}else if ((jiffies - card->state_tick) > CONNECT_TIMEOUT){
2364 
2365 		timer_intr_exec (card, TMR_INT_ENABLED_POLL_CONNECT_OFF);
2366 
2367 	}
2368 }
2369 
2370 /*====================================================================
2371  * 	Handle physical link disconnected phase.
2372  * 	o if hold-down timeout has expired and there are open interfaces,
2373  *	connect link.
2374  *===================================================================*/
2375 
poll_disconnected(sdla_t * card)2376 static void poll_disconnected (sdla_t* card)
2377 {
2378 	netdevice_t *dev;
2379 	x25_channel_t *chan;
2380 	TX25Status* status = card->flags;
2381 
2382 	if (!card->u.x.LAPB_hdlc && card->open_cnt &&
2383 	    ((jiffies - card->state_tick) > HOLD_DOWN_TIME)){
2384 		timer_intr_exec(card, TMR_INT_ENABLED_POLL_DISCONNECT);
2385 	}
2386 
2387 
2388 	if ((dev=card->wandev.dev) == NULL)
2389 		return;
2390 
2391 	if ((chan=dev->priv) == NULL)
2392 		return;
2393 
2394 	if (chan->common.usedby == API &&
2395 	    atomic_read(&chan->common.command) &&
2396 	    card->u.x.LAPB_hdlc){
2397 
2398 		if (!(card->u.x.timer_int_enabled & TMR_INT_ENABLED_CMD_EXEC))
2399 			card->u.x.timer_int_enabled |= TMR_INT_ENABLED_CMD_EXEC;
2400 
2401 		if (!(status->imask & INTR_ON_TIMER))
2402 			status->imask |= INTR_ON_TIMER;
2403 	}
2404 
2405 }
2406 
2407 /*====================================================================
2408  * 	Handle active link phase.
2409  * 	o fetch X.25 asynchronous events.
2410  * 	o kick off transmission on all interfaces.
2411  *===================================================================*/
2412 
poll_active(sdla_t * card)2413 static void poll_active (sdla_t* card)
2414 {
2415 	netdevice_t* dev;
2416 	TX25Status* status = card->flags;
2417 
2418 	for (dev = card->wandev.dev; dev; dev = *((netdevice_t**)dev->priv)){
2419 		x25_channel_t* chan = dev->priv;
2420 
2421 		/* If SVC has been idle long enough, close virtual circuit */
2422 		if ( chan->common.svc &&
2423 		     chan->common.state == WAN_CONNECTED &&
2424 		     chan->common.usedby == WANPIPE ){
2425 
2426 			if( (jiffies - chan->i_timeout_sofar) / HZ > chan->idle_timeout ){
2427 				/* Close svc */
2428 				card->u.x.poll_device=dev;
2429 				timer_intr_exec	(card, TMR_INT_ENABLED_POLL_ACTIVE);
2430 			}
2431 		}
2432 
2433 #ifdef PRINT_DEBUG
2434 		chan->ifstats.tx_compressed = atomic_read(&chan->common.command);
2435 		chan->ifstats.tx_errors = chan->common.state;
2436 		chan->ifstats.rx_fifo_errors = atomic_read(&card->u.x.command_busy);
2437 		++chan->ifstats.tx_bytes;
2438 
2439 		chan->ifstats.rx_fifo_errors=atomic_read(&chan->common.disconnect);
2440 		chan->ifstats.multicast=atomic_read(&chan->bh_buff_used);
2441 		chan->ifstats.rx_length_errors=*card->u.x.hdlc_buf_status;
2442 #endif
2443 
2444 		if (chan->common.usedby == API &&
2445 		    atomic_read(&chan->common.command) &&
2446 	            !card->u.x.LAPB_hdlc){
2447 
2448 			if (!(card->u.x.timer_int_enabled & TMR_INT_ENABLED_CMD_EXEC))
2449 				card->u.x.timer_int_enabled |= TMR_INT_ENABLED_CMD_EXEC;
2450 
2451 			if (!(status->imask & INTR_ON_TIMER))
2452 				status->imask |= INTR_ON_TIMER;
2453 		}
2454 
2455 		if ((chan->common.usedby == API) &&
2456 		     atomic_read(&chan->common.disconnect)){
2457 
2458 			if (chan->common.state == WAN_DISCONNECTED){
2459 				atomic_set(&chan->common.disconnect,0);
2460 				return;
2461 			}
2462 
2463 			atomic_set(&chan->common.command,X25_CLEAR_CALL);
2464 			if (!(card->u.x.timer_int_enabled & TMR_INT_ENABLED_CMD_EXEC))
2465 				card->u.x.timer_int_enabled |= TMR_INT_ENABLED_CMD_EXEC;
2466 
2467 			if (!(status->imask & INTR_ON_TIMER))
2468 				status->imask |= INTR_ON_TIMER;
2469 		}
2470 	}
2471 }
2472 
timer_intr_exec(sdla_t * card,unsigned char TYPE)2473 static void timer_intr_exec(sdla_t *card, unsigned char TYPE)
2474 {
2475 	TX25Status* status = card->flags;
2476 	card->u.x.timer_int_enabled |= TYPE;
2477 	if (!(status->imask & INTR_ON_TIMER))
2478 		status->imask |= INTR_ON_TIMER;
2479 }
2480 
2481 
2482 /*====================================================================
2483  * SDLA Firmware-Specific Functions
2484  *
2485  *  Almost all X.25 commands can unexpetedly fail due to so called 'X.25
2486  *  asynchronous events' such as restart, interrupt, incoming call request,
2487  *  call clear request, etc.  They can't be ignored and have to be delt with
2488  *  immediately.  To tackle with this problem we execute each interface
2489  *  command in a loop until good return code is received or maximum number
2490  *  of retries is reached.  Each interface command returns non-zero return
2491  *  code, an asynchronous event/error handler x25_error() is called.
2492  *====================================================================*/
2493 
2494 /*====================================================================
2495  * 	Read X.25 firmware version.
2496  *		Put code version as ASCII string in str.
2497  *===================================================================*/
2498 
x25_get_version(sdla_t * card,char * str)2499 static int x25_get_version (sdla_t* card, char* str)
2500 {
2501 	TX25Mbox* mbox = card->mbox;
2502   	int retry = MAX_CMD_RETRY;
2503 	int err;
2504 
2505 	do
2506 	{
2507 		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2508 		mbox->cmd.command = X25_READ_CODE_VERSION;
2509 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2510 	} while (err && retry-- &&
2511 		 x25_error(card, err, X25_READ_CODE_VERSION, 0));
2512 
2513 	if (!err && str)
2514 	{
2515 		int len = mbox->cmd.length;
2516 
2517 		memcpy(str, mbox->data, len);
2518 		str[len] = '\0';
2519 	}
2520 	return err;
2521 }
2522 
2523 /*====================================================================
2524  * 	Configure adapter.
2525  *===================================================================*/
2526 
x25_configure(sdla_t * card,TX25Config * conf)2527 static int x25_configure (sdla_t* card, TX25Config* conf)
2528 {
2529 	TX25Mbox* mbox = card->mbox;
2530   	int retry = MAX_CMD_RETRY;
2531 	int err;
2532 
2533 	do{
2534 		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2535 		memcpy(mbox->data, (void*)conf, sizeof(TX25Config));
2536 		mbox->cmd.length  = sizeof(TX25Config);
2537 		mbox->cmd.command = X25_SET_CONFIGURATION;
2538 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2539 	} while (err && retry-- && x25_error(card, err, X25_SET_CONFIGURATION, 0));
2540 	return err;
2541 }
2542 
2543 /*====================================================================
2544  * 	Configure adapter for HDLC only.
2545  *===================================================================*/
2546 
hdlc_configure(sdla_t * card,TX25Config * conf)2547 static int hdlc_configure (sdla_t* card, TX25Config* conf)
2548 {
2549 	TX25Mbox* mbox = card->mbox;
2550   	int retry = MAX_CMD_RETRY;
2551 	int err;
2552 
2553 	do{
2554 		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2555 		memcpy(mbox->data, (void*)conf, sizeof(TX25Config));
2556 		mbox->cmd.length  = sizeof(TX25Config);
2557 		mbox->cmd.command = X25_HDLC_SET_CONFIG;
2558 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2559 	} while (err && retry-- && x25_error(card, err, X25_SET_CONFIGURATION, 0));
2560 
2561 	return err;
2562 }
2563 
set_hdlc_level(sdla_t * card)2564 static int set_hdlc_level (sdla_t* card)
2565 {
2566 
2567 	TX25Mbox* mbox = card->mbox;
2568   	int retry = MAX_CMD_RETRY;
2569 	int err;
2570 
2571 	do{
2572 		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2573 		mbox->cmd.command = SET_PROTOCOL_LEVEL;
2574 		mbox->cmd.length = 1;
2575 		mbox->data[0] = HDLC_LEVEL; //| DO_HDLC_LEVEL_ERROR_CHECKING;
2576 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2577 	} while (err && retry-- && x25_error(card, err, SET_PROTOCOL_LEVEL, 0));
2578 
2579 	return err;
2580 }
2581 
2582 
2583 
2584 /*====================================================================
2585  * Get communications error statistics.
2586  *====================================================================*/
2587 
x25_get_err_stats(sdla_t * card)2588 static int x25_get_err_stats (sdla_t* card)
2589 {
2590 	TX25Mbox* mbox = card->mbox;
2591   	int retry = MAX_CMD_RETRY;
2592 	int err;
2593 
2594 	do
2595 	{
2596 		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2597 		mbox->cmd.command = X25_HDLC_READ_COMM_ERR;
2598 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2599 	} while (err && retry-- && x25_error(card, err, X25_HDLC_READ_COMM_ERR, 0));
2600 
2601 	if (!err)
2602 	{
2603 		THdlcCommErr* stats = (void*)mbox->data;
2604 
2605 		card->wandev.stats.rx_over_errors    = stats->rxOverrun;
2606 		card->wandev.stats.rx_crc_errors     = stats->rxBadCrc;
2607 		card->wandev.stats.rx_missed_errors  = stats->rxAborted;
2608 		card->wandev.stats.tx_aborted_errors = stats->txAborted;
2609 	}
2610 	return err;
2611 }
2612 
2613 /*====================================================================
2614  * 	Get protocol statistics.
2615  *===================================================================*/
2616 
x25_get_stats(sdla_t * card)2617 static int x25_get_stats (sdla_t* card)
2618 {
2619 	TX25Mbox* mbox = card->mbox;
2620   	int retry = MAX_CMD_RETRY;
2621 	int err;
2622 
2623 	do
2624 	{
2625 		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2626 		mbox->cmd.command = X25_READ_STATISTICS;
2627 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2628 	} while (err && retry-- && x25_error(card, err, X25_READ_STATISTICS, 0)) ;
2629 
2630 	if (!err)
2631 	{
2632 		TX25Stats* stats = (void*)mbox->data;
2633 
2634 		card->wandev.stats.rx_packets = stats->rxData;
2635 		card->wandev.stats.tx_packets = stats->txData;
2636 	}
2637 	return err;
2638 }
2639 
2640 /*====================================================================
2641  * 	Close HDLC link.
2642  *===================================================================*/
2643 
x25_close_hdlc(sdla_t * card)2644 static int x25_close_hdlc (sdla_t* card)
2645 {
2646 	TX25Mbox* mbox = card->mbox;
2647   	int retry = MAX_CMD_RETRY;
2648 	int err;
2649 
2650 	do
2651 	{
2652 		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2653 		mbox->cmd.command = X25_HDLC_LINK_CLOSE;
2654 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2655 	} while (err && retry-- && x25_error(card, err, X25_HDLC_LINK_CLOSE, 0));
2656 
2657 	return err;
2658 }
2659 
2660 
2661 /*====================================================================
2662  * 	Open HDLC link.
2663  *===================================================================*/
2664 
x25_open_hdlc(sdla_t * card)2665 static int x25_open_hdlc (sdla_t* card)
2666 {
2667 	TX25Mbox* mbox = card->mbox;
2668   	int retry = MAX_CMD_RETRY;
2669 	int err;
2670 
2671 	do
2672 	{
2673 		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2674 		mbox->cmd.command = X25_HDLC_LINK_OPEN;
2675 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2676 	} while (err && retry-- && x25_error(card, err, X25_HDLC_LINK_OPEN, 0));
2677 
2678 	return err;
2679 }
2680 
2681 /*=====================================================================
2682  * Setup HDLC link.
2683  *====================================================================*/
x25_setup_hdlc(sdla_t * card)2684 static int x25_setup_hdlc (sdla_t* card)
2685 {
2686 	TX25Mbox* mbox = card->mbox;
2687   	int retry = MAX_CMD_RETRY;
2688 	int err;
2689 
2690 	do
2691 	{
2692 		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2693 		mbox->cmd.command = X25_HDLC_LINK_SETUP;
2694 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2695 	} while (err && retry-- && x25_error(card, err, X25_HDLC_LINK_SETUP, 0));
2696 
2697 	return err;
2698 }
2699 
2700 /*====================================================================
2701  * Set (raise/drop) DTR.
2702  *===================================================================*/
2703 
x25_set_dtr(sdla_t * card,int dtr)2704 static int x25_set_dtr (sdla_t* card, int dtr)
2705 {
2706 	TX25Mbox* mbox = card->mbox;
2707   	int retry = MAX_CMD_RETRY;
2708 	int err;
2709 
2710 	do
2711 	{
2712 		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2713 		mbox->data[0] = 0;
2714 		mbox->data[2] = 0;
2715 		mbox->data[1] = dtr ? 0x02 : 0x01;
2716 		mbox->cmd.length  = 3;
2717 		mbox->cmd.command = X25_SET_GLOBAL_VARS;
2718 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2719 	} while (err && retry-- && x25_error(card, err, X25_SET_GLOBAL_VARS, 0));
2720 
2721 	return err;
2722 }
2723 
2724 /*====================================================================
2725  * 	Set interrupt mode.
2726  *===================================================================*/
2727 
x25_set_intr_mode(sdla_t * card,int mode)2728 static int x25_set_intr_mode (sdla_t* card, int mode)
2729 {
2730 	TX25Mbox* mbox = card->mbox;
2731   	int retry = MAX_CMD_RETRY;
2732 	int err;
2733 
2734 	do
2735 	{
2736 		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2737 		mbox->data[0] = mode;
2738 		if (card->hw.fwid == SFID_X25_508){
2739 			mbox->data[1] = card->hw.irq;
2740 			mbox->data[2] = 2;
2741 			mbox->cmd.length = 3;
2742 		}else {
2743 		 	mbox->cmd.length  = 1;
2744 		}
2745 		mbox->cmd.command = X25_SET_INTERRUPT_MODE;
2746 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2747 	} while (err && retry-- && x25_error(card, err, X25_SET_INTERRUPT_MODE, 0));
2748 
2749 	return err;
2750 }
2751 
2752 /*====================================================================
2753  * 	Read X.25 channel configuration.
2754  *===================================================================*/
2755 
x25_get_chan_conf(sdla_t * card,x25_channel_t * chan)2756 static int x25_get_chan_conf (sdla_t* card, x25_channel_t* chan)
2757 {
2758 	TX25Mbox* mbox = card->mbox;
2759   	int retry = MAX_CMD_RETRY;
2760 	int lcn = chan->common.lcn;
2761 	int err;
2762 
2763 	do{
2764 		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2765 		mbox->cmd.lcn     = lcn;
2766 		mbox->cmd.command = X25_READ_CHANNEL_CONFIG;
2767 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2768 	} while (err && retry-- && x25_error(card, err, X25_READ_CHANNEL_CONFIG, lcn));
2769 
2770 	if (!err)
2771 	{
2772 		TX25Status* status = card->flags;
2773 
2774 		/* calculate an offset into the array of status bytes */
2775 		if (card->u.x.hi_svc <= X25_MAX_CHAN){
2776 
2777 			chan->ch_idx = lcn - 1;
2778 
2779 		}else{
2780 			int offset;
2781 
2782 			/* FIX: Apr 14 2000 : Nenad Corbic
2783 			 * The data field was being compared to 0x1F using
2784                          * '&&' instead of '&'.
2785 			 * This caused X25API to fail for LCNs greater than 255.
2786 			 */
2787 			switch (mbox->data[0] & 0x1F)
2788 			{
2789 				case 0x01:
2790 					offset = status->pvc_map; break;
2791 				case 0x03:
2792 					offset = status->icc_map; break;
2793 				case 0x07:
2794 					offset = status->twc_map; break;
2795 				case 0x0B:
2796 					offset = status->ogc_map; break;
2797 				default:
2798 					offset = 0;
2799 			}
2800 			chan->ch_idx = lcn - 1 - offset;
2801 		}
2802 
2803 		/* get actual transmit packet size on this channel */
2804 		switch(mbox->data[1] & 0x38)
2805 		{
2806 			case 0x00:
2807 				chan->tx_pkt_size = 16;
2808 				break;
2809 			case 0x08:
2810 				chan->tx_pkt_size = 32;
2811 				break;
2812 			case 0x10:
2813 				chan->tx_pkt_size = 64;
2814 				break;
2815 			case 0x18:
2816 				chan->tx_pkt_size = 128;
2817 				break;
2818 			case 0x20:
2819 				chan->tx_pkt_size = 256;
2820 				break;
2821 			case 0x28:
2822 				chan->tx_pkt_size = 512;
2823 				break;
2824 			case 0x30:
2825 				chan->tx_pkt_size = 1024;
2826 				break;
2827 		}
2828 		if (card->u.x.logging)
2829 			printk(KERN_INFO "%s: X.25 packet size on LCN %d is %d.\n",
2830 				card->devname, lcn, chan->tx_pkt_size);
2831 	}
2832 	return err;
2833 }
2834 
2835 /*====================================================================
2836  * 	Place X.25 call.
2837  *====================================================================*/
2838 
x25_place_call(sdla_t * card,x25_channel_t * chan)2839 static int x25_place_call (sdla_t* card, x25_channel_t* chan)
2840 {
2841 	TX25Mbox* mbox = card->mbox;
2842   	int retry = MAX_CMD_RETRY;
2843 	int err;
2844 	char str[64];
2845 
2846 
2847 	if (chan->protocol == htons(ETH_P_IP)){
2848 		sprintf(str, "-d%s -uCC", chan->addr);
2849 
2850 	}else if (chan->protocol == htons(ETH_P_IPX)){
2851 		sprintf(str, "-d%s -u800000008137", chan->addr);
2852 
2853 	}
2854 
2855 	do
2856 	{
2857 		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2858 		strcpy(mbox->data, str);
2859 		mbox->cmd.length  = strlen(str);
2860 		mbox->cmd.command = X25_PLACE_CALL;
2861 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2862 	} while (err && retry-- && x25_error(card, err, X25_PLACE_CALL, 0));
2863 
2864 	if (!err){
2865 		bind_lcn_to_dev (card, chan->dev, mbox->cmd.lcn);
2866 	}
2867 	return err;
2868 }
2869 
2870 /*====================================================================
2871  * 	Accept X.25 call.
2872  *====================================================================*/
2873 
x25_accept_call(sdla_t * card,int lcn,int qdm)2874 static int x25_accept_call (sdla_t* card, int lcn, int qdm)
2875 {
2876 	TX25Mbox* mbox = card->mbox;
2877   	int retry = MAX_CMD_RETRY;
2878 	int err;
2879 
2880 	do
2881 	{
2882 		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2883 		mbox->cmd.lcn     = lcn;
2884 		mbox->cmd.qdm     = qdm;
2885 		mbox->cmd.command = X25_ACCEPT_CALL;
2886 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2887 	} while (err && retry-- && x25_error(card, err, X25_ACCEPT_CALL, lcn));
2888 
2889 	return err;
2890 }
2891 
2892 /*====================================================================
2893  * 	Clear X.25 call.
2894  *====================================================================*/
2895 
x25_clear_call(sdla_t * card,int lcn,int cause,int diagn)2896 static int x25_clear_call (sdla_t* card, int lcn, int cause, int diagn)
2897 {
2898 	TX25Mbox* mbox = card->mbox;
2899   	int retry = MAX_CMD_RETRY;
2900 	int err;
2901 
2902 	do
2903 	{
2904 		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2905 		mbox->cmd.lcn     = lcn;
2906 		mbox->cmd.cause   = cause;
2907 		mbox->cmd.diagn   = diagn;
2908 		mbox->cmd.command = X25_CLEAR_CALL;
2909 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2910 	} while (err && retry-- && x25_error(card, err, X25_CLEAR_CALL, lcn));
2911 
2912 	return err;
2913 }
2914 
2915 /*====================================================================
2916  * 	Send X.25 data packet.
2917  *====================================================================*/
2918 
x25_send(sdla_t * card,int lcn,int qdm,int len,void * buf)2919 static int x25_send (sdla_t* card, int lcn, int qdm, int len, void* buf)
2920 {
2921 	TX25Mbox* mbox = card->mbox;
2922   	int retry = MAX_CMD_RETRY;
2923 	int err;
2924 	unsigned char cmd;
2925 
2926 	if (card->u.x.LAPB_hdlc)
2927 		cmd = X25_HDLC_WRITE;
2928 	else
2929 		cmd = X25_WRITE;
2930 
2931 	do
2932 	{
2933 		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2934 		memcpy(mbox->data, buf, len);
2935 		mbox->cmd.length  = len;
2936 		mbox->cmd.lcn     = lcn;
2937 
2938 		if (card->u.x.LAPB_hdlc){
2939 			mbox->cmd.pf = qdm;
2940 		}else{
2941 			mbox->cmd.qdm = qdm;
2942 		}
2943 
2944 		mbox->cmd.command = cmd;
2945 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2946 	} while (err && retry-- && x25_error(card, err, cmd , lcn));
2947 
2948 
2949 	/* If buffers are busy the return code for LAPB HDLC is
2950          * 1. The above functions are looking for return code
2951          * of X25RES_NOT_READY if busy. */
2952 
2953 	if (card->u.x.LAPB_hdlc && err == 1){
2954 		err = X25RES_NOT_READY;
2955 	}
2956 
2957 	return err;
2958 }
2959 
2960 /*====================================================================
2961  * 	Fetch X.25 asynchronous events.
2962  *===================================================================*/
2963 
x25_fetch_events(sdla_t * card)2964 static int x25_fetch_events (sdla_t* card)
2965 {
2966 	TX25Status* status = card->flags;
2967 	TX25Mbox* mbox = card->mbox;
2968 	int err = 0;
2969 
2970 	if (status->gflags & 0x20)
2971 	{
2972 		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
2973 		mbox->cmd.command = X25_IS_DATA_AVAILABLE;
2974 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2975  		if (err) x25_error(card, err, X25_IS_DATA_AVAILABLE, 0);
2976 	}
2977 	return err;
2978 }
2979 
2980 /*====================================================================
2981  * 	X.25 asynchronous event/error handler.
2982  *		This routine is called each time interface command returns
2983  *		non-zero return code to handle X.25 asynchronous events and
2984  *		common errors. Return non-zero to repeat command or zero to
2985  *		cancel it.
2986  *
2987  * 	Notes:
2988  * 	1. This function may be called recursively, as handling some of the
2989  *    	asynchronous events (e.g. call request) requires execution of the
2990  *    	interface command(s) that, in turn, may also return asynchronous
2991  *    	events.  To avoid re-entrancy problems we copy mailbox to dynamically
2992  *    	allocated memory before processing events.
2993  *====================================================================*/
2994 
x25_error(sdla_t * card,int err,int cmd,int lcn)2995 static int x25_error (sdla_t* card, int err, int cmd, int lcn)
2996 {
2997 	int retry = 1;
2998 	unsigned dlen = ((TX25Mbox*)card->mbox)->cmd.length;
2999 	TX25Mbox* mb;
3000 
3001 	mb = kmalloc(sizeof(TX25Mbox) + dlen, GFP_ATOMIC);
3002 	if (mb == NULL)
3003 	{
3004 		printk(KERN_ERR "%s: x25_error() out of memory!\n",
3005 			card->devname);
3006 		return 0;
3007 	}
3008 	memcpy(mb, card->mbox, sizeof(TX25Mbox) + dlen);
3009 	switch (err){
3010 
3011 	case X25RES_ASYNC_PACKET:	/* X.25 asynchronous packet was received */
3012 
3013 		mb->data[dlen] = '\0';
3014 
3015 		switch (mb->cmd.pktType & 0x7F){
3016 
3017 		case ASE_CALL_RQST:		/* incoming call */
3018 			retry = incoming_call(card, cmd, lcn, mb);
3019 			break;
3020 
3021 		case ASE_CALL_ACCEPTED:		/* connected */
3022 			retry = call_accepted(card, cmd, lcn, mb);
3023 			break;
3024 
3025 		case ASE_CLEAR_RQST:		/* call clear request */
3026 			retry = call_cleared(card, cmd, lcn, mb);
3027 			break;
3028 
3029 		case ASE_RESET_RQST:		/* reset request */
3030 			printk(KERN_INFO "%s: X.25 reset request on LCN %d! "
3031 				"Cause:0x%02X Diagn:0x%02X\n",
3032 				card->devname, mb->cmd.lcn, mb->cmd.cause,
3033 				mb->cmd.diagn);
3034 			api_oob_event (card,mb);
3035 			break;
3036 
3037 		case ASE_RESTART_RQST:		/* restart request */
3038 			retry = restart_event(card, cmd, lcn, mb);
3039 			break;
3040 
3041 		case ASE_CLEAR_CONFRM:
3042 			if (clear_confirm_event (card,mb))
3043 				break;
3044 
3045 			/* I use the goto statement here so if
3046 	                 * somebody inserts code between the
3047         	         * case and default, we will not have
3048                 	 * ghost problems */
3049 
3050 			goto dflt_1;
3051 
3052 		default:
3053 dflt_1:
3054 			printk(KERN_INFO "%s: X.25 event 0x%02X on LCN %d! "
3055 				"Cause:0x%02X Diagn:0x%02X\n",
3056 				card->devname, mb->cmd.pktType,
3057 				mb->cmd.lcn, mb->cmd.cause, mb->cmd.diagn);
3058 		}
3059 		break;
3060 
3061 	case X25RES_PROTO_VIOLATION:	/* X.25 protocol violation indication */
3062 
3063 		/* Bug Fix: Mar 14 2000
3064                  * The Protocol violation error conditions were
3065                  * not handeled previously */
3066 
3067 		switch (mb->cmd.pktType & 0x7F){
3068 
3069 		case PVE_CLEAR_RQST:	/* Clear request */
3070 			retry = call_cleared(card, cmd, lcn, mb);
3071 			break;
3072 
3073 		case PVE_RESET_RQST:	/* Reset request */
3074 			printk(KERN_INFO "%s: X.25 reset request on LCN %d! "
3075 				"Cause:0x%02X Diagn:0x%02X\n",
3076 				card->devname, mb->cmd.lcn, mb->cmd.cause,
3077 				mb->cmd.diagn);
3078 			api_oob_event (card,mb);
3079 			break;
3080 
3081 		case PVE_RESTART_RQST:	/* Restart request */
3082 			retry = restart_event(card, cmd, lcn, mb);
3083 			break;
3084 
3085 		default :
3086 			printk(KERN_INFO
3087 				"%s: X.25 protocol violation on LCN %d! "
3088 				"Packet:0x%02X Cause:0x%02X Diagn:0x%02X\n",
3089 				card->devname, mb->cmd.lcn,
3090 				mb->cmd.pktType & 0x7F, mb->cmd.cause, mb->cmd.diagn);
3091 			api_oob_event(card,mb);
3092 		}
3093 		break;
3094 
3095 	case 0x42:	/* X.25 timeout */
3096 		retry = timeout_event(card, cmd, lcn, mb);
3097 		break;
3098 
3099 	case 0x43:	/* X.25 retry limit exceeded */
3100 		printk(KERN_INFO
3101 			"%s: exceeded X.25 retry limit on LCN %d! "
3102 			"Packet:0x%02X Diagn:0x%02X\n", card->devname,
3103 			mb->cmd.lcn, mb->cmd.pktType, mb->cmd.diagn)
3104 		;
3105 		break;
3106 
3107 	case 0x08:	/* modem failure */
3108 #ifndef MODEM_NOT_LOG
3109 		printk(KERN_INFO "%s: modem failure!\n", card->devname);
3110 #endif /* MODEM_NOT_LOG */
3111 		api_oob_event(card,mb);
3112 		break;
3113 
3114 	case 0x09:	/* N2 retry limit */
3115 		printk(KERN_INFO "%s: exceeded HDLC retry limit!\n",
3116 			card->devname);
3117 		api_oob_event(card,mb);
3118 		break;
3119 
3120 	case 0x06:	/* unnumbered frame was received while in ABM */
3121 		printk(KERN_INFO "%s: received Unnumbered frame 0x%02X!\n",
3122 			card->devname, mb->data[0]);
3123 		api_oob_event(card,mb);
3124 		break;
3125 
3126 	case CMD_TIMEOUT:
3127 		printk(KERN_ERR "%s: command 0x%02X timed out!\n",
3128 			card->devname, cmd)
3129 		;
3130 		retry = 0;	/* abort command */
3131 		break;
3132 
3133 	case X25RES_NOT_READY:
3134 		retry = 1;
3135 		break;
3136 
3137 	case 0x01:
3138 		if (card->u.x.LAPB_hdlc)
3139 			break;
3140 
3141 		if (mb->cmd.command == 0x16)
3142 			break;
3143 		/* I use the goto statement here so if
3144                  * somebody inserts code between the
3145                  * case and default, we will not have
3146                  * ghost problems */
3147 		goto dflt_2;
3148 
3149 	default:
3150 dflt_2:
3151 		printk(KERN_INFO "%s: command 0x%02X returned 0x%02X! Lcn %i\n",
3152 			card->devname, cmd, err, mb->cmd.lcn)
3153 		;
3154 		retry = 0;	/* abort command */
3155 	}
3156 	kfree(mb);
3157 	return retry;
3158 }
3159 
3160 /*====================================================================
3161  *	X.25 Asynchronous Event Handlers
3162  * 	These functions are called by the x25_error() and should return 0, if
3163  * 	the command resulting in the asynchronous event must be aborted.
3164  *====================================================================*/
3165 
3166 
3167 
3168 /*====================================================================
3169  *Handle X.25 incoming call request.
3170  *	RFC 1356 establishes the following rules:
3171  *	1. The first octet in the Call User Data (CUD) field of the call
3172  *     	   request packet contains NLPID identifying protocol encapsulation
3173  * 	2. Calls MUST NOT be accepted unless router supports requested
3174  *   	   protocol encapsulation.
3175  *	3. A diagnostic code 249 defined by ISO/IEC 8208 may be used
3176  *	   when clearing a call because protocol encapsulation is not
3177  *	   supported.
3178  *	4. If an incoming call is received while a call request is
3179  *	   pending (i.e. call collision has occured), the incoming call
3180  *	   shall be rejected and call request shall be retried.
3181  *====================================================================*/
3182 
incoming_call(sdla_t * card,int cmd,int lcn,TX25Mbox * mb)3183 static int incoming_call (sdla_t* card, int cmd, int lcn, TX25Mbox* mb)
3184 {
3185 	wan_device_t* wandev = &card->wandev;
3186 	int new_lcn = mb->cmd.lcn;
3187 	netdevice_t* dev = get_dev_by_lcn(wandev, new_lcn);
3188 	x25_channel_t* chan = NULL;
3189 	int accept = 0;		/* set to '1' if o.k. to accept call */
3190 	unsigned int user_data;
3191 	x25_call_info_t* info;
3192 
3193 	/* Make sure there is no call collision */
3194 	if (dev != NULL)
3195 	{
3196 		printk(KERN_INFO
3197 			"%s: X.25 incoming call collision on LCN %d!\n",
3198 			card->devname, new_lcn);
3199 
3200 		x25_clear_call(card, new_lcn, 0, 0);
3201 		return 1;
3202 	}
3203 
3204 	/* Make sure D bit is not set in call request */
3205 //FIXME: THIS IS NOT TURE !!!! TAKE IT OUT
3206 //	if (mb->cmd.qdm & 0x02)
3207 //	{
3208 //		printk(KERN_INFO
3209 //			"%s: X.25 incoming call on LCN %d with D-bit set!\n",
3210 //			card->devname, new_lcn);
3211 //
3212 //		x25_clear_call(card, new_lcn, 0, 0);
3213 //		return 1;
3214 //	}
3215 
3216 	/* Parse call request data */
3217 	info = kmalloc(sizeof(x25_call_info_t), GFP_ATOMIC);
3218 	if (info == NULL)
3219 	{
3220 		printk(KERN_ERR
3221 			"%s: not enough memory to parse X.25 incoming call "
3222 			"on LCN %d!\n", card->devname, new_lcn);
3223 		x25_clear_call(card, new_lcn, 0, 0);
3224 		return 1;
3225 	}
3226 
3227 	parse_call_info(mb->data, info);
3228 
3229 	if (card->u.x.logging)
3230 		printk(KERN_INFO "\n%s: X.25 incoming call on LCN %d!\n",
3231 			card->devname, new_lcn);
3232 
3233 	/* Conver the first two ASCII characters into an
3234          * interger. Used to check the incoming protocol
3235          */
3236 	user_data = hex_to_uint(info->user,2);
3237 
3238 	/* Find available channel */
3239 	for (dev = wandev->dev; dev; dev = *((netdevice_t**)dev->priv)){
3240 		chan = dev->priv;
3241 
3242 		if (chan->common.usedby == API)
3243 			continue;
3244 
3245 		if (!chan->common.svc || (chan->common.state != WAN_DISCONNECTED))
3246 			continue;
3247 
3248 		if (user_data == NLPID_IP && chan->protocol != htons(ETH_P_IP)){
3249 			printk(KERN_INFO "IP packet but configured for IPX : %x, %x\n",
3250 				       htons(chan->protocol), info->user[0]);
3251 			continue;
3252 		}
3253 
3254 		if (user_data == NLPID_SNAP && chan->protocol != htons(ETH_P_IPX)){
3255 			printk(KERN_INFO "IPX packet but configured for IP: %x\n",
3256 				       htons(chan->protocol));
3257 			continue;
3258 		}
3259 		if (strcmp(info->src, chan->addr) == 0)
3260 			break;
3261 
3262 	        /* If just an '@' is specified, accept all incoming calls */
3263 	        if (strcmp(chan->addr, "") == 0)
3264 	                break;
3265 	}
3266 
3267 	if (dev == NULL){
3268 
3269 		/* If the call is not for any WANPIPE interfaces
3270                  * check to see if there is an API listening queue
3271                  * waiting for data. If there is send the packet
3272                  * up the stack.
3273                  */
3274 		if (card->sk != NULL && card->func != NULL){
3275 			if (api_incoming_call(card,mb,new_lcn)){
3276 				x25_clear_call(card, new_lcn, 0, 0);
3277 			}
3278 			accept = 0;
3279 		}else{
3280 			printk(KERN_INFO "%s: no channels available!\n",
3281 				card->devname);
3282 
3283 			x25_clear_call(card, new_lcn, 0, 0);
3284 		}
3285 
3286 	}else if (info->nuser == 0){
3287 
3288 		printk(KERN_INFO
3289 			"%s: no user data in incoming call on LCN %d!\n",
3290 			card->devname, new_lcn)
3291 		;
3292 		x25_clear_call(card, new_lcn, 0, 0);
3293 
3294 	}else switch (info->user[0]){
3295 
3296 		case 0:		/* multiplexed */
3297 			chan->protocol = htons(0);
3298 			accept = 1;
3299 			break;
3300 
3301 		case NLPID_IP:	/* IP datagrams */
3302 			accept = 1;
3303 			break;
3304 
3305 		case NLPID_SNAP: /* IPX datagrams */
3306 			accept = 1;
3307 			break;
3308 
3309 		default:
3310 			printk(KERN_INFO
3311 				"%s: unsupported NLPID 0x%02X in incoming call "
3312 				"on LCN %d!\n", card->devname, info->user[0], new_lcn);
3313 			x25_clear_call(card, new_lcn, 0, 249);
3314 	}
3315 
3316 	if (accept && (x25_accept_call(card, new_lcn, 0) == CMD_OK)){
3317 
3318 		bind_lcn_to_dev (card, chan->dev, new_lcn);
3319 
3320 		if (x25_get_chan_conf(card, chan) == CMD_OK)
3321 			set_chan_state(dev, WAN_CONNECTED);
3322 		else
3323 			x25_clear_call(card, new_lcn, 0, 0);
3324 	}
3325 	kfree(info);
3326 	return 1;
3327 }
3328 
3329 /*====================================================================
3330  * 	Handle accepted call.
3331  *====================================================================*/
3332 
call_accepted(sdla_t * card,int cmd,int lcn,TX25Mbox * mb)3333 static int call_accepted (sdla_t* card, int cmd, int lcn, TX25Mbox* mb)
3334 {
3335 	unsigned new_lcn = mb->cmd.lcn;
3336 	netdevice_t* dev = find_channel(card, new_lcn);
3337 	x25_channel_t* chan;
3338 
3339 	if (dev == NULL){
3340 		printk(KERN_INFO
3341 			"%s: clearing orphaned connection on LCN %d!\n",
3342 			card->devname, new_lcn);
3343 		x25_clear_call(card, new_lcn, 0, 0);
3344 		return 1;
3345 	}
3346 
3347 	if (card->u.x.logging)
3348 		printk(KERN_INFO "%s: X.25 call accepted on Dev %s and LCN %d!\n",
3349 			card->devname, dev->name, new_lcn);
3350 
3351 	/* Get channel configuration and notify router */
3352 	chan = dev->priv;
3353 	if (x25_get_chan_conf(card, chan) != CMD_OK)
3354 	{
3355 		x25_clear_call(card, new_lcn, 0, 0);
3356 		return 1;
3357 	}
3358 
3359 	set_chan_state(dev, WAN_CONNECTED);
3360 
3361 	if (chan->common.usedby == API){
3362 		send_delayed_cmd_result(card,dev,mb);
3363 		bind_lcn_to_dev (card, dev, new_lcn);
3364 	}
3365 
3366 	return 1;
3367 }
3368 
3369 /*====================================================================
3370  * 	Handle cleared call.
3371  *====================================================================*/
3372 
call_cleared(sdla_t * card,int cmd,int lcn,TX25Mbox * mb)3373 static int call_cleared (sdla_t* card, int cmd, int lcn, TX25Mbox* mb)
3374 {
3375 	unsigned new_lcn = mb->cmd.lcn;
3376 	netdevice_t* dev = find_channel(card, new_lcn);
3377 	x25_channel_t *chan;
3378 	unsigned char old_state;
3379 
3380 	if (card->u.x.logging){
3381 		printk(KERN_INFO "%s: X.25 clear request on LCN %d! Cause:0x%02X "
3382 		"Diagn:0x%02X\n",
3383 		card->devname, new_lcn, mb->cmd.cause, mb->cmd.diagn);
3384 	}
3385 
3386 	if (dev == NULL){
3387 		printk(KERN_INFO "%s: X.25 clear request : No device for clear\n",
3388 				card->devname);
3389 		return 1;
3390 	}
3391 
3392 	chan=dev->priv;
3393 
3394 	old_state = chan->common.state;
3395 
3396 	set_chan_state(dev, WAN_DISCONNECTED);
3397 
3398 	if (chan->common.usedby == API){
3399 
3400 		switch (old_state){
3401 
3402 		case WAN_CONNECTING:
3403 			send_delayed_cmd_result(card,dev,mb);
3404 			break;
3405 		case WAN_CONNECTED:
3406 			send_oob_msg(card,dev,mb);
3407 			break;
3408 		}
3409 	}
3410 
3411 	return ((cmd == X25_WRITE) && (lcn == new_lcn)) ? 0 : 1;
3412 }
3413 
3414 /*====================================================================
3415  * 	Handle X.25 restart event.
3416  *====================================================================*/
3417 
restart_event(sdla_t * card,int cmd,int lcn,TX25Mbox * mb)3418 static int restart_event (sdla_t* card, int cmd, int lcn, TX25Mbox* mb)
3419 {
3420 	wan_device_t* wandev = &card->wandev;
3421 	netdevice_t* dev;
3422 	x25_channel_t *chan;
3423 	unsigned char old_state;
3424 
3425 	printk(KERN_INFO
3426 		"%s: X.25 restart request! Cause:0x%02X Diagn:0x%02X\n",
3427 		card->devname, mb->cmd.cause, mb->cmd.diagn);
3428 
3429 	/* down all logical channels */
3430 	for (dev = wandev->dev; dev; dev = *((netdevice_t**)dev->priv)){
3431 		chan=dev->priv;
3432 		old_state = chan->common.state;
3433 
3434 		set_chan_state(dev, WAN_DISCONNECTED);
3435 
3436 		if (chan->common.usedby == API){
3437 			switch (old_state){
3438 
3439 			case WAN_CONNECTING:
3440 				send_delayed_cmd_result(card,dev,mb);
3441 				break;
3442 			case WAN_CONNECTED:
3443 				send_oob_msg(card,dev,mb);
3444 				break;
3445 			}
3446 		}
3447 	}
3448 	return (cmd == X25_WRITE) ? 0 : 1;
3449 }
3450 
3451 /*====================================================================
3452  * Handle timeout event.
3453  *====================================================================*/
3454 
timeout_event(sdla_t * card,int cmd,int lcn,TX25Mbox * mb)3455 static int timeout_event (sdla_t* card, int cmd, int lcn, TX25Mbox* mb)
3456 {
3457 	unsigned new_lcn = mb->cmd.lcn;
3458 
3459 	if (mb->cmd.pktType == 0x05)	/* call request time out */
3460 	{
3461 		netdevice_t* dev = find_channel(card,new_lcn);
3462 
3463 		printk(KERN_INFO "%s: X.25 call timed timeout on LCN %d!\n",
3464 			card->devname, new_lcn);
3465 
3466 		if (dev){
3467 			x25_channel_t *chan = dev->priv;
3468 			set_chan_state(dev, WAN_DISCONNECTED);
3469 
3470 			if (chan->common.usedby == API){
3471 				send_delayed_cmd_result(card,dev,card->mbox);
3472 			}
3473 		}
3474 	}else{
3475 		printk(KERN_INFO "%s: X.25 packet 0x%02X timeout on LCN %d!\n",
3476 		card->devname, mb->cmd.pktType, new_lcn);
3477 	}
3478 	return 1;
3479 }
3480 
3481 /*
3482  *	Miscellaneous
3483  */
3484 
3485 /*====================================================================
3486  * 	Establish physical connection.
3487  * 	o open HDLC and raise DTR
3488  *
3489  * 	Return:		0	connection established
3490  *			1	connection is in progress
3491  *			<0	error
3492  *===================================================================*/
3493 
connect(sdla_t * card)3494 static int connect (sdla_t* card)
3495 {
3496 	TX25Status* status = card->flags;
3497 
3498 	if (x25_open_hdlc(card) || x25_setup_hdlc(card))
3499 		return -EIO;
3500 
3501 	wanpipe_set_state(card, WAN_CONNECTING);
3502 
3503 	x25_set_intr_mode(card, INTR_ON_TIMER);
3504 	status->imask &= ~INTR_ON_TIMER;
3505 
3506 	return 1;
3507 }
3508 
3509 /*
3510  * 	Tear down physical connection.
3511  * 	o close HDLC link
3512  * 	o drop DTR
3513  *
3514  * 	Return:		0
3515  *			<0	error
3516  */
3517 
disconnect(sdla_t * card)3518 static int disconnect (sdla_t* card)
3519 {
3520 	wanpipe_set_state(card, WAN_DISCONNECTED);
3521 	x25_set_intr_mode(card, INTR_ON_TIMER);	/* disable all interrupt except timer */
3522 	x25_close_hdlc(card);			/* close HDLC link */
3523 	x25_set_dtr(card, 0);			/* drop DTR */
3524 	return 0;
3525 }
3526 
3527 /*
3528  * Find network device by its channel number.
3529  */
3530 
get_dev_by_lcn(wan_device_t * wandev,unsigned lcn)3531 static netdevice_t* get_dev_by_lcn (wan_device_t* wandev, unsigned lcn)
3532 {
3533 	netdevice_t* dev;
3534 
3535 	for (dev = wandev->dev; dev; dev = *((netdevice_t**)dev->priv))
3536 		if (((x25_channel_t*)dev->priv)->common.lcn == lcn)
3537 			break;
3538 	return dev;
3539 }
3540 
3541 /*
3542  * 	Initiate connection on the logical channel.
3543  * 	o for PVC we just get channel configuration
3544  * 	o for SVCs place an X.25 call
3545  *
3546  * 	Return:		0	connected
3547  *			>0	connection in progress
3548  *			<0	failure
3549  */
3550 
chan_connect(netdevice_t * dev)3551 static int chan_connect (netdevice_t* dev)
3552 {
3553 	x25_channel_t* chan = dev->priv;
3554 	sdla_t* card = chan->card;
3555 
3556 	if (chan->common.svc && chan->common.usedby == WANPIPE){
3557 		if (!chan->addr[0]){
3558 			printk(KERN_INFO "%s: No Destination Address\n",
3559 					card->devname);
3560 			return -EINVAL;	/* no destination address */
3561 		}
3562 		printk(KERN_INFO "%s: placing X.25 call to %s ...\n",
3563 			card->devname, chan->addr);
3564 
3565 		if (x25_place_call(card, chan) != CMD_OK)
3566 			return -EIO;
3567 
3568 		set_chan_state(dev, WAN_CONNECTING);
3569 		return 1;
3570 	}else{
3571 		if (x25_get_chan_conf(card, chan) != CMD_OK)
3572 			return -EIO;
3573 
3574 		set_chan_state(dev, WAN_CONNECTED);
3575 	}
3576 	return 0;
3577 }
3578 
3579 /*
3580  * 	Disconnect logical channel.
3581  * 	o if SVC then clear X.25 call
3582  */
3583 
chan_disc(netdevice_t * dev)3584 static int chan_disc (netdevice_t* dev)
3585 {
3586 	x25_channel_t* chan = dev->priv;
3587 
3588 	if (chan->common.svc){
3589 		x25_clear_call(chan->card, chan->common.lcn, 0, 0);
3590 
3591 		/* For API we disconnect on clear
3592                  * confirmation.
3593                  */
3594 		if (chan->common.usedby == API)
3595 			return 0;
3596 	}
3597 
3598 	set_chan_state(dev, WAN_DISCONNECTED);
3599 
3600 	return 0;
3601 }
3602 
3603 /*
3604  * 	Set logical channel state.
3605  */
3606 
set_chan_state(netdevice_t * dev,int state)3607 static void set_chan_state (netdevice_t* dev, int state)
3608 {
3609 	x25_channel_t* chan = dev->priv;
3610 	sdla_t* card = chan->card;
3611 	unsigned long flags;
3612 
3613 	save_flags(flags);
3614 	cli();
3615 	if (chan->common.state != state)
3616 	{
3617 		switch (state)
3618 		{
3619 			case WAN_CONNECTED:
3620 				if (card->u.x.logging){
3621 					printk (KERN_INFO
3622 						"%s: interface %s connected, lcn %i !\n",
3623 						card->devname, dev->name,chan->common.lcn);
3624 				}
3625 				*(unsigned short*)dev->dev_addr = htons(chan->common.lcn);
3626 				chan->i_timeout_sofar = jiffies;
3627 
3628 				/* LAPB is PVC Based */
3629 				if (card->u.x.LAPB_hdlc)
3630 					chan->common.svc=0;
3631 				break;
3632 
3633 			case WAN_CONNECTING:
3634 				if (card->u.x.logging){
3635 					printk (KERN_INFO
3636 						"%s: interface %s connecting, lcn %i ...\n",
3637 						card->devname, dev->name, chan->common.lcn);
3638 				}
3639 				break;
3640 
3641 			case WAN_DISCONNECTED:
3642 				if (card->u.x.logging){
3643 					printk (KERN_INFO
3644 						"%s: interface %s disconnected, lcn %i !\n",
3645 						card->devname, dev->name,chan->common.lcn);
3646 				}
3647 				atomic_set(&chan->common.disconnect,0);
3648 
3649 				if (chan->common.svc) {
3650 					*(unsigned short*)dev->dev_addr = 0;
3651 					card->u.x.svc_to_dev_map[(chan->common.lcn%X25_MAX_CHAN)]=NULL;
3652 		                	chan->common.lcn = 0;
3653 				}
3654 
3655 				if (chan->transmit_length){
3656 					chan->transmit_length=0;
3657 					atomic_set(&chan->common.driver_busy,0);
3658 					chan->tx_offset=0;
3659 					if (is_queue_stopped(dev)){
3660 						wake_net_dev(dev);
3661 					}
3662 				}
3663 				atomic_set(&chan->common.command,0);
3664 				break;
3665 
3666 			case WAN_DISCONNECTING:
3667 				if (card->u.x.logging){
3668 					printk (KERN_INFO
3669 					"\n%s: interface %s disconnecting, lcn %i ...\n",
3670 					card->devname, dev->name,chan->common.lcn);
3671 				}
3672 				atomic_set(&chan->common.disconnect,0);
3673 				break;
3674 		}
3675 		chan->common.state = state;
3676 	}
3677 	chan->state_tick = jiffies;
3678 	restore_flags(flags);
3679 }
3680 
3681 /*
3682  * 	Send packet on a logical channel.
3683  *		When this function is called, tx_skb field of the channel data
3684  *		space points to the transmit socket buffer.  When transmission
3685  *		is complete, release socket buffer and reset 'tbusy' flag.
3686  *
3687  * 	Return:		0	- transmission complete
3688  *			1	- busy
3689  *
3690  * 	Notes:
3691  * 	1. If packet length is greater than MTU for this channel, we'll fragment
3692  *    	the packet into 'complete sequence' using M-bit.
3693  * 	2. When transmission is complete, an event notification should be issued
3694  *    	to the router.
3695  */
3696 
chan_send(netdevice_t * dev,void * buff,unsigned data_len,unsigned char tx_intr)3697 static int chan_send (netdevice_t* dev, void* buff, unsigned data_len, unsigned char tx_intr)
3698 {
3699 	x25_channel_t* chan = dev->priv;
3700 	sdla_t* card = chan->card;
3701 	TX25Status* status = card->flags;
3702 	unsigned len=0, qdm=0, res=0, orig_len = 0;
3703 	void *data;
3704 
3705 	/* Check to see if channel is ready */
3706 	if ((!(status->cflags[chan->ch_idx] & 0x40) && !card->u.x.LAPB_hdlc)  ||
3707              !(*card->u.x.hdlc_buf_status & 0x40)){
3708 
3709 		if (!tx_intr){
3710 			setup_for_delayed_transmit (dev, buff, data_len);
3711 			return 0;
3712 		}else{
3713 			/* By returning 0 to tx_intr the packet will be dropped */
3714 			++card->wandev.stats.tx_dropped;
3715 			++chan->ifstats.tx_dropped;
3716 			printk(KERN_INFO "%s: ERROR, Tx intr could not send, dropping %s:\n",
3717 				card->devname,dev->name);
3718 			++chan->if_send_stat.if_send_bfr_not_passed_to_adptr;
3719 			return 0;
3720 		}
3721 	}
3722 
3723 	if (chan->common.usedby == API){
3724 		/* Remove the API Header */
3725 		x25api_hdr_t *api_data = (x25api_hdr_t *)buff;
3726 
3727 		/* Set the qdm bits from the packet header
3728                  * User has the option to set the qdm bits
3729                  */
3730 		qdm = api_data->qdm;
3731 
3732 		orig_len = len = data_len - sizeof(x25api_hdr_t);
3733 		data = (unsigned char*)buff + sizeof(x25api_hdr_t);
3734 	}else{
3735 		data = buff;
3736 		orig_len = len = data_len;
3737 	}
3738 
3739 	if (tx_intr){
3740 		/* We are in tx_intr, minus the tx_offset from
3741                  * the total length. The tx_offset part of the
3742 		 * data has already been sent. Also, move the
3743 		 * data pointer to proper offset location.
3744                  */
3745 		len -= chan->tx_offset;
3746 		data = (unsigned char*)data + chan->tx_offset;
3747 	}
3748 
3749 	/* Check if the packet length is greater than MTU
3750          * If YES: Cut the len to MTU and set the M bit
3751          */
3752 	if (len > chan->tx_pkt_size && !card->u.x.LAPB_hdlc){
3753 		len = chan->tx_pkt_size;
3754 		qdm |= M_BIT;
3755 	}
3756 
3757 
3758 	/* Pass only first three bits of the qdm byte to the send
3759          * routine. In case user sets any other bit which might
3760          * cause errors.
3761          */
3762 
3763 	switch(x25_send(card, chan->common.lcn, (qdm&0x07), len, data)){
3764 		case 0x00:	/* success */
3765 			chan->i_timeout_sofar = jiffies;
3766 
3767 #ifdef LINUX_2_4
3768 			dev->trans_start=jiffies;
3769 #endif
3770 
3771 			if ((qdm & M_BIT) && !card->u.x.LAPB_hdlc){
3772 				if (!tx_intr){
3773 					/* The M bit was set, which means that part of the
3774                                          * packet has been sent. Copy the packet into a buffer
3775 				         * and set the offset to len, so on next tx_inter
3776 					 * the packet will be sent using the below offset.
3777 					 */
3778 					chan->tx_offset += len;
3779 
3780 					++chan->ifstats.tx_packets;
3781 #if defined(LINUX_2_1) || defined(LINUX_2_4)
3782 					chan->ifstats.tx_bytes += len;
3783 #endif
3784 
3785 					if (chan->tx_offset < orig_len){
3786 						setup_for_delayed_transmit (dev, buff, data_len);
3787 					}
3788 					res=0;
3789 				}else{
3790 					/* We are already in tx_inter, thus data is already
3791                                          * in the buffer. Update the offset and wait for
3792                                          * next tx_intr. We add on to the offset, since data can
3793                                          * be X number of times larger than max data size.
3794 					 */
3795 					++chan->ifstats.tx_packets;
3796 #if defined(LINUX_2_1) || defined(LINUX_2_4)
3797 					chan->ifstats.tx_bytes += len;
3798 #endif
3799 
3800 					++chan->if_send_stat.if_send_bfr_passed_to_adptr;
3801 					chan->tx_offset += len;
3802 
3803 					/* The user can set the qdm bit as well.
3804                                          * If the entire packet was sent and qdm is still
3805                                          * set, than it's the user who has set the M bit. In that,
3806                                          * case indicate that the packet was send by returning
3807 					 * 0 and wait for a new packet. Otherwise, wait for next
3808                                          * tx interrupt to send the rest of the packet */
3809 
3810 					if (chan->tx_offset < orig_len){
3811 						res=1;
3812 					}else{
3813 						res=0;
3814 					}
3815 				}
3816 			}else{
3817 				++chan->ifstats.tx_packets;
3818 #if defined(LINUX_2_1) || defined(LINUX_2_4)
3819 				chan->ifstats.tx_bytes += len;
3820 #endif
3821 				++chan->if_send_stat.if_send_bfr_passed_to_adptr;
3822 				res=0;
3823 			}
3824 			break;
3825 
3826 		case 0x33:	/* Tx busy */
3827 			if (tx_intr){
3828 				printk(KERN_INFO "%s: Tx_intr: Big Error dropping packet %s\n",
3829 						card->devname,dev->name);
3830 				++chan->ifstats.tx_dropped;
3831 				++card->wandev.stats.tx_dropped;
3832 				++chan->if_send_stat.if_send_bfr_not_passed_to_adptr;
3833 				res=0;
3834 			}else{
3835 				DBG_PRINTK(KERN_INFO
3836 					"%s: Send: Big Error should have tx: storring %s\n",
3837 						card->devname,dev->name);
3838 				setup_for_delayed_transmit (dev, buff, data_len);
3839 				res=1;
3840 			}
3841 			break;
3842 
3843 		default:	/* failure */
3844 			++chan->ifstats.tx_errors;
3845 			if (tx_intr){
3846 				printk(KERN_INFO "%s: Tx_intr: Failure to send, dropping %s\n",
3847 					card->devname,dev->name);
3848 				++chan->ifstats.tx_dropped;
3849 				++card->wandev.stats.tx_dropped;
3850 				++chan->if_send_stat.if_send_bfr_not_passed_to_adptr;
3851 				res=0;
3852 			}else{
3853 				DBG_PRINTK(KERN_INFO "%s: Send: Failure to send !!!, storing %s\n",
3854 					card->devname,dev->name);
3855 				setup_for_delayed_transmit (dev, buff, data_len);
3856 				res=1;
3857 			}
3858 			break;
3859 	}
3860 	return res;
3861 }
3862 
3863 
3864 /*
3865  * 	Parse X.25 call request data and fill x25_call_info_t structure.
3866  */
3867 
parse_call_info(unsigned char * str,x25_call_info_t * info)3868 static void parse_call_info (unsigned char* str, x25_call_info_t* info)
3869 {
3870 	memset(info, 0, sizeof(x25_call_info_t));
3871 	for (; *str; ++str)
3872 	{
3873 		int i;
3874 		unsigned char ch;
3875 
3876 		if (*str == '-') switch (str[1]) {
3877 
3878 			/* Take minus 2 off the maximum size so that
3879                          * last byte is 0. This way we can use string
3880                          * manipulaton functions on call information.
3881                          */
3882 
3883 			case 'd':	/* destination address */
3884 				for (i = 0; i < (MAX_X25_ADDR_SIZE-2); ++i){
3885 					ch = str[2+i];
3886 					if (isspace(ch)) break;
3887 					info->dest[i] = ch;
3888 				}
3889 				break;
3890 
3891 			case 's':	/* source address */
3892 				for (i = 0; i < (MAX_X25_ADDR_SIZE-2); ++i){
3893 					ch = str[2+i];
3894 					if (isspace(ch)) break;
3895 					info->src[i] = ch;
3896 				}
3897 				break;
3898 
3899 			case 'u':	/* user data */
3900 				for (i = 0; i < (MAX_X25_DATA_SIZE-2); ++i){
3901 					ch = str[2+i];
3902 					if (isspace(ch)) break;
3903 					info->user[i] = ch;
3904 				}
3905 				info->nuser = i;
3906 				break;
3907 
3908 			case 'f':	/* facilities */
3909 				for (i = 0; i < (MAX_X25_FACL_SIZE-2); ++i){
3910 					ch = str[2+i];
3911 					if (isspace(ch)) break;
3912 					info->facil[i] = ch;
3913 				}
3914 				info->nfacil = i;
3915 				break;
3916 		}
3917 	}
3918 }
3919 
3920 /*
3921  * 	Convert line speed in bps to a number used by S502 code.
3922  */
3923 
bps_to_speed_code(unsigned long bps)3924 static unsigned char bps_to_speed_code (unsigned long bps)
3925 {
3926 	unsigned char	number;
3927 
3928 	if (bps <= 1200)        number = 0x01;
3929 	else if (bps <= 2400)   number = 0x02;
3930 	else if (bps <= 4800)   number = 0x03;
3931 	else if (bps <= 9600)   number = 0x04;
3932 	else if (bps <= 19200)  number = 0x05;
3933 	else if (bps <= 38400)  number = 0x06;
3934 	else if (bps <= 45000)  number = 0x07;
3935 	else if (bps <= 56000)  number = 0x08;
3936 	else if (bps <= 64000)  number = 0x09;
3937 	else if (bps <= 74000)  number = 0x0A;
3938 	else if (bps <= 112000) number = 0x0B;
3939 	else if (bps <= 128000) number = 0x0C;
3940 	else number = 0x0D;
3941 
3942 	return number;
3943 }
3944 
3945 /*
3946  * 	Convert decimal string to unsigned integer.
3947  * 	If len != 0 then only 'len' characters of the string are converted.
3948  */
3949 
dec_to_uint(unsigned char * str,int len)3950 static unsigned int dec_to_uint (unsigned char* str, int len)
3951 {
3952 	unsigned val;
3953 
3954 	if (!len)
3955 		len = strlen(str);
3956 
3957 	for (val = 0; len && is_digit(*str); ++str, --len)
3958 		val = (val * 10) + (*str - (unsigned)'0');
3959 
3960 	return val;
3961 }
3962 
3963 /*
3964  * 	Convert hex string to unsigned integer.
3965  * 	If len != 0 then only 'len' characters of the string are conferted.
3966  */
3967 
hex_to_uint(unsigned char * str,int len)3968 static unsigned int hex_to_uint (unsigned char* str, int len)
3969 {
3970 	unsigned val, ch;
3971 
3972 	if (!len)
3973 		len = strlen(str);
3974 
3975 	for (val = 0; len; ++str, --len)
3976 	{
3977 		ch = *str;
3978 		if (is_digit(ch))
3979 			val = (val << 4) + (ch - (unsigned)'0');
3980 		else if (is_hex_digit(ch))
3981 			val = (val << 4) + ((ch & 0xDF) - (unsigned)'A' + 10);
3982 		else break;
3983 	}
3984 	return val;
3985 }
3986 
3987 
handle_IPXWAN(unsigned char * sendpacket,char * devname,unsigned char enable_IPX,unsigned long network_number,unsigned short proto)3988 static int handle_IPXWAN(unsigned char *sendpacket, char *devname, unsigned char enable_IPX, unsigned long network_number, unsigned short proto)
3989 {
3990 	int i;
3991 
3992 	if( proto == ETH_P_IPX) {
3993 		/* It's an IPX packet */
3994 		if(!enable_IPX) {
3995 			/* Return 1 so we don't pass it up the stack. */
3996 			return 1;
3997 		}
3998 	} else {
3999 		/* It's not IPX so pass it up the stack.*/
4000 		return 0;
4001 	}
4002 
4003 	if( sendpacket[16] == 0x90 &&
4004 	    sendpacket[17] == 0x04)
4005 	{
4006 		/* It's IPXWAN  */
4007 
4008 		if( sendpacket[2] == 0x02 &&
4009 		    sendpacket[34] == 0x00)
4010 		{
4011 			/* It's a timer request packet */
4012 			printk(KERN_INFO "%s: Received IPXWAN Timer Request packet\n",devname);
4013 
4014 			/* Go through the routing options and answer no to every
4015 			 * option except Unnumbered RIP/SAP
4016 			 */
4017 			for(i = 41; sendpacket[i] == 0x00; i += 5)
4018 			{
4019 				/* 0x02 is the option for Unnumbered RIP/SAP */
4020 				if( sendpacket[i + 4] != 0x02)
4021 				{
4022 					sendpacket[i + 1] = 0;
4023 				}
4024 			}
4025 
4026 			/* Skip over the extended Node ID option */
4027 			if( sendpacket[i] == 0x04 )
4028 			{
4029 				i += 8;
4030 			}
4031 
4032 			/* We also want to turn off all header compression opt. 			 */
4033 			for(; sendpacket[i] == 0x80 ;)
4034 			{
4035 				sendpacket[i + 1] = 0;
4036 				i += (sendpacket[i + 2] << 8) + (sendpacket[i + 3]) + 4;
4037 			}
4038 
4039 			/* Set the packet type to timer response */
4040 			sendpacket[34] = 0x01;
4041 
4042 			printk(KERN_INFO "%s: Sending IPXWAN Timer Response\n",devname);
4043 		}
4044 		else if( sendpacket[34] == 0x02 )
4045 		{
4046 			/* This is an information request packet */
4047 			printk(KERN_INFO "%s: Received IPXWAN Information Request packet\n",devname);
4048 
4049 			/* Set the packet type to information response */
4050 			sendpacket[34] = 0x03;
4051 
4052 			/* Set the router name */
4053 			sendpacket[51] = 'X';
4054 			sendpacket[52] = 'T';
4055 			sendpacket[53] = 'P';
4056 			sendpacket[54] = 'I';
4057 			sendpacket[55] = 'P';
4058 			sendpacket[56] = 'E';
4059 			sendpacket[57] = '-';
4060 			sendpacket[58] = CVHexToAscii(network_number >> 28);
4061 			sendpacket[59] = CVHexToAscii((network_number & 0x0F000000)>> 24);
4062 			sendpacket[60] = CVHexToAscii((network_number & 0x00F00000)>> 20);
4063 			sendpacket[61] = CVHexToAscii((network_number & 0x000F0000)>> 16);
4064 			sendpacket[62] = CVHexToAscii((network_number & 0x0000F000)>> 12);
4065 			sendpacket[63] = CVHexToAscii((network_number & 0x00000F00)>> 8);
4066 			sendpacket[64] = CVHexToAscii((network_number & 0x000000F0)>> 4);
4067 			sendpacket[65] = CVHexToAscii(network_number & 0x0000000F);
4068 			for(i = 66; i < 99; i+= 1)
4069 			{
4070 				sendpacket[i] = 0;
4071 			}
4072 
4073 			printk(KERN_INFO "%s: Sending IPXWAN Information Response packet\n",devname);
4074 		}
4075 		else
4076 		{
4077 			printk(KERN_INFO "%s: Unknown IPXWAN packet!\n",devname);
4078 			return 0;
4079 		}
4080 
4081 		/* Set the WNodeID to our network address */
4082 		sendpacket[35] = (unsigned char)(network_number >> 24);
4083 		sendpacket[36] = (unsigned char)((network_number & 0x00FF0000) >> 16);
4084 		sendpacket[37] = (unsigned char)((network_number & 0x0000FF00) >> 8);
4085 		sendpacket[38] = (unsigned char)(network_number & 0x000000FF);
4086 
4087 		return 1;
4088 	} else {
4089 		/*If we get here its an IPX-data packet, so it'll get passed up the stack.
4090 		 */
4091 		/* switch the network numbers */
4092 		switch_net_numbers(sendpacket, network_number, 1);
4093 		return 0;
4094 	}
4095 }
4096 
4097 /*
4098  *  	If incoming is 0 (outgoing)- if the net numbers is ours make it 0
4099  *  	if incoming is 1 - if the net number is 0 make it ours
4100  */
4101 
switch_net_numbers(unsigned char * sendpacket,unsigned long network_number,unsigned char incoming)4102 static void switch_net_numbers(unsigned char *sendpacket, unsigned long network_number, unsigned char incoming)
4103 {
4104 	unsigned long pnetwork_number;
4105 
4106 	pnetwork_number = (unsigned long)((sendpacket[6] << 24) +
4107 			  (sendpacket[7] << 16) + (sendpacket[8] << 8) +
4108 			  sendpacket[9]);
4109 
4110 
4111 	if (!incoming) {
4112 		/*If the destination network number is ours, make it 0 */
4113 		if( pnetwork_number == network_number) {
4114 			sendpacket[6] = sendpacket[7] = sendpacket[8] =
4115 					 sendpacket[9] = 0x00;
4116 		}
4117 	} else {
4118 		/* If the incoming network is 0, make it ours */
4119 		if( pnetwork_number == 0) {
4120 			sendpacket[6] = (unsigned char)(network_number >> 24);
4121 			sendpacket[7] = (unsigned char)((network_number &
4122 					 0x00FF0000) >> 16);
4123 			sendpacket[8] = (unsigned char)((network_number &
4124 					 0x0000FF00) >> 8);
4125 			sendpacket[9] = (unsigned char)(network_number &
4126 					 0x000000FF);
4127 		}
4128 	}
4129 
4130 
4131 	pnetwork_number = (unsigned long)((sendpacket[18] << 24) +
4132 			  (sendpacket[19] << 16) + (sendpacket[20] << 8) +
4133 			  sendpacket[21]);
4134 
4135 
4136 	if( !incoming ) {
4137 		/* If the source network is ours, make it 0 */
4138 		if( pnetwork_number == network_number) {
4139 			sendpacket[18] = sendpacket[19] = sendpacket[20] =
4140 				 sendpacket[21] = 0x00;
4141 		}
4142 	} else {
4143 		/* If the source network is 0, make it ours */
4144 		if( pnetwork_number == 0 ) {
4145 			sendpacket[18] = (unsigned char)(network_number >> 24);
4146 			sendpacket[19] = (unsigned char)((network_number &
4147 					 0x00FF0000) >> 16);
4148 			sendpacket[20] = (unsigned char)((network_number &
4149 					 0x0000FF00) >> 8);
4150 			sendpacket[21] = (unsigned char)(network_number &
4151 					 0x000000FF);
4152 		}
4153 	}
4154 } /* switch_net_numbers */
4155 
4156 
4157 
4158 
4159 /********************* X25API SPECIFIC FUNCTIONS ****************/
4160 
4161 
4162 /*===============================================================
4163  *  find_channel
4164  *
4165  *	Manages the lcn to device map. It increases performance
4166  *      because it eliminates the need to search through the link
4167  *      list for a device which is bounded to a specific lcn.
4168  *
4169  *===============================================================*/
4170 
4171 
find_channel(sdla_t * card,unsigned lcn)4172 netdevice_t * find_channel(sdla_t *card, unsigned lcn)
4173 {
4174 	if (card->u.x.LAPB_hdlc){
4175 
4176 		return card->wandev.dev;
4177 
4178 	}else{
4179 		/* We don't know whether the incoming lcn
4180                  * is a PVC or an SVC channel. But we do know that
4181                  * the lcn cannot be for both the PVC and the SVC
4182                  * channel.
4183 
4184 		 * If the lcn number is greater or equal to 255,
4185                  * take the modulo 255 of that number. We only have
4186                  * 255 locations, thus higher numbers must be mapped
4187                  * to a number between 0 and 245.
4188 
4189 		 * We must separate pvc's and svc's since two don't
4190                  * have to be contiguous.  Meaning pvc's can start
4191                  * from 1 to 10 and svc's can start from 256 to 266.
4192                  * But 256%255 is 1, i.e. CONFLICT.
4193 		 */
4194 
4195 
4196 		/* Highest LCN number must be less or equal to 4096 */
4197 		if ((lcn <= MAX_LCN_NUM) && (lcn > 0)){
4198 
4199 			if (lcn < X25_MAX_CHAN){
4200 				if (card->u.x.svc_to_dev_map[lcn])
4201 					return card->u.x.svc_to_dev_map[lcn];
4202 
4203 				if (card->u.x.pvc_to_dev_map[lcn])
4204 					return card->u.x.pvc_to_dev_map[lcn];
4205 
4206 			}else{
4207 				int new_lcn = lcn%X25_MAX_CHAN;
4208 				if (card->u.x.svc_to_dev_map[new_lcn])
4209 					return card->u.x.svc_to_dev_map[new_lcn];
4210 
4211 				if (card->u.x.pvc_to_dev_map[new_lcn])
4212 					return card->u.x.pvc_to_dev_map[new_lcn];
4213 			}
4214 		}
4215 		return NULL;
4216 	}
4217 }
4218 
bind_lcn_to_dev(sdla_t * card,netdevice_t * dev,unsigned lcn)4219 void bind_lcn_to_dev (sdla_t *card, netdevice_t *dev,unsigned lcn)
4220 {
4221 	x25_channel_t *chan = dev->priv;
4222 
4223 	/* Modulo the lcn number by X25_MAX_CHAN (255)
4224 	 * because the lcn number can be greater than 255
4225          *
4226 	 * We need to split svc and pvc since they don't have
4227          * to be contigous.
4228 	 */
4229 
4230 	if (chan->common.svc){
4231 		card->u.x.svc_to_dev_map[(lcn % X25_MAX_CHAN)] = dev;
4232 	}else{
4233 		card->u.x.pvc_to_dev_map[(lcn % X25_MAX_CHAN)] = dev;
4234 	}
4235 	chan->common.lcn = lcn;
4236 }
4237 
4238 
4239 
4240 /*===============================================================
4241  * x25api_bh
4242  *
4243  *
4244  *==============================================================*/
4245 
x25api_bh(netdevice_t * dev)4246 static void x25api_bh (netdevice_t * dev)
4247 {
4248 	x25_channel_t* chan = dev->priv;
4249 	sdla_t* card = chan->card;
4250 	struct sk_buff *skb;
4251 
4252 	if (atomic_read(&chan->bh_buff_used) == 0){
4253 		printk(KERN_INFO "%s: BH Buffer Empty in BH\n",
4254 				card->devname);
4255 		clear_bit(0, &chan->tq_working);
4256 		return;
4257 	}
4258 
4259 	while (atomic_read(&chan->bh_buff_used)){
4260 
4261 		/* If the sock is in the process of unlinking the
4262 		 * driver from the socket, we must get out.
4263 		 * This never happends but is a sanity check. */
4264 		if (test_bit(0,&chan->common.common_critical)){
4265 			clear_bit(0, &chan->tq_working);
4266 			return;
4267 		}
4268 
4269 		/* If LAPB HDLC, do not drop packets if socket is
4270                  * not connected.  Let the buffer fill up and
4271                  * turn off rx interrupt */
4272 		if (card->u.x.LAPB_hdlc){
4273 			if (chan->common.sk == NULL || chan->common.func == NULL){
4274 				clear_bit(0, &chan->tq_working);
4275 				return;
4276 			}
4277 		}
4278 
4279 		skb  = ((bh_data_t *)&chan->bh_head[chan->bh_read])->skb;
4280 
4281 		if (skb == NULL){
4282 			printk(KERN_INFO "%s: BH Skb empty for read %i\n",
4283 					card->devname,chan->bh_read);
4284 		}else{
4285 
4286 			if (chan->common.sk == NULL || chan->common.func == NULL){
4287 				printk(KERN_INFO "%s: BH: Socket disconnected, dropping\n",
4288 						card->devname);
4289 				wan_dev_kfree_skb(skb, FREE_READ);
4290 				x25api_bh_cleanup(dev);
4291 				++chan->ifstats.rx_dropped;
4292 				++chan->rx_intr_stat.rx_intr_bfr_not_passed_to_stack;
4293 				continue;
4294 			}
4295 
4296 
4297 			if (chan->common.func(skb,dev,chan->common.sk) != 0){
4298 				/* Sock full cannot send, queue us for another
4299                                  * try
4300 				 */
4301 				printk(KERN_INFO "%s: BH: !!! Packet failed to send !!!!! \n",
4302 						card->devname);
4303 				atomic_set(&chan->common.receive_block,1);
4304 				return;
4305 			}else{
4306 				x25api_bh_cleanup(dev);
4307 				++chan->rx_intr_stat.rx_intr_bfr_passed_to_stack;
4308 			}
4309 		}
4310 	}
4311 	clear_bit(0, &chan->tq_working);
4312 
4313 	return;
4314 }
4315 
4316 /*===============================================================
4317  * x25api_bh_cleanup
4318  *
4319  *
4320  *==============================================================*/
4321 
x25api_bh_cleanup(netdevice_t * dev)4322 static int x25api_bh_cleanup (netdevice_t *dev)
4323 {
4324 	x25_channel_t* chan = dev->priv;
4325 	sdla_t *card = chan->card;
4326 	TX25Status* status = card->flags;
4327 
4328 
4329 	((bh_data_t *)&chan->bh_head[chan->bh_read])->skb = NULL;
4330 
4331 	if (chan->bh_read == MAX_BH_BUFF){
4332 		chan->bh_read=0;
4333 	}else{
4334 		++chan->bh_read;
4335 	}
4336 
4337 	/* If the Receive interrupt was off, it means
4338          * that we filled up our circular buffer. Check
4339          * that we have space in the buffer. If so
4340          * turn the RX interrupt back on.
4341 	 */
4342 	if (!(status->imask & INTR_ON_RX_FRAME)){
4343 		if (atomic_read(&chan->bh_buff_used) < (MAX_BH_BUFF+1)){
4344 			printk(KERN_INFO "%s: BH: Turning on the interrupt\n",
4345 					card->devname);
4346 			status->imask |= INTR_ON_RX_FRAME;
4347 		}
4348 	}
4349 
4350 	atomic_dec(&chan->bh_buff_used);
4351 	return 0;
4352 }
4353 
4354 
4355 /*===============================================================
4356  * bh_enqueue
4357  *
4358  *
4359  *==============================================================*/
4360 
bh_enqueue(netdevice_t * dev,struct sk_buff * skb)4361 static int bh_enqueue (netdevice_t *dev, struct sk_buff *skb)
4362 {
4363 	x25_channel_t* chan = dev->priv;
4364 	sdla_t *card = chan->card;
4365 	TX25Status* status = card->flags;
4366 
4367 	if (atomic_read(&chan->bh_buff_used) == (MAX_BH_BUFF+1)){
4368 		printk(KERN_INFO "%s: Bottom half buffer FULL\n",
4369 				card->devname);
4370 		return 1;
4371 	}
4372 
4373 	((bh_data_t *)&chan->bh_head[chan->bh_write])->skb = skb;
4374 
4375 	if (chan->bh_write == MAX_BH_BUFF){
4376 		chan->bh_write=0;
4377 	}else{
4378 		++chan->bh_write;
4379 	}
4380 
4381 	atomic_inc(&chan->bh_buff_used);
4382 
4383 	if (atomic_read(&chan->bh_buff_used) == (MAX_BH_BUFF+1)){
4384 		printk(KERN_INFO "%s: Buffer is now full, Turning off RX Intr\n",
4385 				card->devname);
4386 		status->imask &= ~INTR_ON_RX_FRAME;
4387 	}
4388 
4389 	return 0;
4390 }
4391 
4392 
4393 /*===============================================================
4394  * timer_intr_cmd_exec
4395  *
4396  *	Called by timer interrupt to execute a command
4397  *===============================================================*/
4398 
timer_intr_cmd_exec(sdla_t * card)4399 static int timer_intr_cmd_exec (sdla_t* card)
4400 {
4401 	netdevice_t *dev;
4402 	unsigned char more_to_exec=0;
4403 	volatile x25_channel_t *chan=NULL;
4404 	int i=0,bad_cmd=0,err=0;
4405 
4406 	if (card->u.x.cmd_dev == NULL){
4407 		card->u.x.cmd_dev = card->wandev.dev;
4408 	}
4409 
4410 	dev = card->u.x.cmd_dev;
4411 
4412 	for (;;){
4413 
4414 		chan = dev->priv;
4415 
4416 		if (atomic_read(&chan->common.command)){
4417 
4418 			bad_cmd = check_bad_command(card,dev);
4419 
4420 			if ((!chan->common.mbox || atomic_read(&chan->common.disconnect)) &&
4421 			     !bad_cmd){
4422 
4423 				/* Socket has died or exited, We must bring the
4424                                  * channel down before anybody else tries to
4425                                  * use it */
4426 				err = channel_disconnect(card,dev);
4427 			}else{
4428 			        err = execute_delayed_cmd(card, dev,
4429 							 (mbox_cmd_t*)chan->common.mbox,
4430 							  bad_cmd);
4431 			}
4432 
4433 			switch (err){
4434 
4435 			case RETURN_RESULT:
4436 
4437 				/* Return the result to the socket without
4438                                  * delay. NO_WAIT Command */
4439 				atomic_set(&chan->common.command,0);
4440 				if (atomic_read(&card->u.x.command_busy))
4441 					atomic_set(&card->u.x.command_busy,0);
4442 
4443 				send_delayed_cmd_result(card,dev,card->mbox);
4444 
4445 				more_to_exec=0;
4446 				break;
4447 			case DELAY_RESULT:
4448 
4449 				/* Wait for the remote to respond, before
4450                                  * sending the result up to the socket.
4451                                  * WAIT command */
4452 				if (atomic_read(&card->u.x.command_busy))
4453 					atomic_set(&card->u.x.command_busy,0);
4454 
4455 				atomic_set(&chan->common.command,0);
4456 				more_to_exec=0;
4457 				break;
4458 			default:
4459 
4460 				/* If command could not be executed for
4461                                  * some reason (i.e return code 0x33 busy)
4462                                  * set the more_to_exec bit which will
4463                                  * indicate that this command must be exectued
4464                                  * again during next timer interrupt
4465 				 */
4466 				more_to_exec=1;
4467 				if (atomic_read(&card->u.x.command_busy) == 0)
4468 					atomic_set(&card->u.x.command_busy,1);
4469 				break;
4470 			}
4471 
4472 			bad_cmd=0;
4473 
4474 			/* If flags is set, there are no hdlc buffers,
4475                          * thus, wait for the next pass and try the
4476                          * same command again. Otherwise, start searching
4477                          * from next device on the next pass.
4478 			 */
4479 			if (!more_to_exec){
4480 				dev = move_dev_to_next(card,dev);
4481 			}
4482 			break;
4483 		}else{
4484 			/* This device has nothing to execute,
4485                          * go to next.
4486 			 */
4487 			if (atomic_read(&card->u.x.command_busy))
4488 					atomic_set(&card->u.x.command_busy,0);
4489 			dev = move_dev_to_next(card,dev);
4490 		}
4491 
4492 		if (++i == card->u.x.no_dev){
4493 			if (!more_to_exec){
4494 				DBG_PRINTK(KERN_INFO "%s: Nothing to execute in Timer\n",
4495 					card->devname);
4496 				if (atomic_read(&card->u.x.command_busy)){
4497 					atomic_set(&card->u.x.command_busy,0);
4498 				}
4499 			}
4500 			break;
4501 		}
4502 
4503 	} //End of FOR
4504 
4505 	card->u.x.cmd_dev = dev;
4506 
4507 	if (more_to_exec){
4508 		/* If more commands are pending, do not turn off timer
4509                  * interrupt */
4510 		return 1;
4511 	}else{
4512 		/* No more commands, turn off timer interrupt */
4513 		return 0;
4514 	}
4515 }
4516 
4517 /*===============================================================
4518  * execute_delayed_cmd
4519  *
4520  *	Execute an API command which was passed down from the
4521  *      sock.  Sock is very limited in which commands it can
4522  *      execute.  Wait and No Wait commands are supported.
4523  *      Place Call, Clear Call and Reset wait commands, where
4524  *      Accept Call is a no_wait command.
4525  *
4526  *===============================================================*/
4527 
execute_delayed_cmd(sdla_t * card,netdevice_t * dev,mbox_cmd_t * usr_cmd,char bad_cmd)4528 static int execute_delayed_cmd (sdla_t* card, netdevice_t *dev, mbox_cmd_t *usr_cmd,char bad_cmd)
4529 {
4530 	TX25Mbox* mbox = card->mbox;
4531 	int err;
4532 	x25_channel_t *chan = dev->priv;
4533 	int delay=RETURN_RESULT;
4534 
4535 	if (!(*card->u.x.hdlc_buf_status & 0x40) && !bad_cmd){
4536 		return TRY_CMD_AGAIN;
4537 	}
4538 
4539 	/* This way a command is guaranteed to be executed for
4540          * a specific lcn, the network interface is bound to. */
4541 	usr_cmd->cmd.lcn = chan->common.lcn;
4542 
4543 
4544 	/* If channel is pvc, instead of place call
4545          * run x25_channel configuration. If running LAPB HDLC
4546          * enable communications.
4547          */
4548 	if ((!chan->common.svc) && (usr_cmd->cmd.command == X25_PLACE_CALL)){
4549 
4550 		if (card->u.x.LAPB_hdlc){
4551 			DBG_PRINTK(KERN_INFO "LAPB: Connecting\n");
4552 			connect(card);
4553 			set_chan_state(dev,WAN_CONNECTING);
4554 			return DELAY_RESULT;
4555 		}else{
4556 			DBG_PRINTK(KERN_INFO "%s: PVC is CONNECTING\n",card->devname);
4557 			if (x25_get_chan_conf(card, chan) == CMD_OK){
4558 				set_chan_state(dev, WAN_CONNECTED);
4559 			}else{
4560 				set_chan_state(dev, WAN_DISCONNECTED);
4561 			}
4562 			return RETURN_RESULT;
4563 		}
4564 	}
4565 
4566 	/* Copy the socket mbox command onto the board */
4567 
4568 	memcpy(&mbox->cmd, &usr_cmd->cmd, sizeof(TX25Cmd));
4569 	if (usr_cmd->cmd.length){
4570 		memcpy(mbox->data, usr_cmd->data, usr_cmd->cmd.length);
4571 	}
4572 
4573 	/* Check if command is bad. We need to copy the cmd into
4574          * the buffer regardless since we return the, mbox to
4575          * the user */
4576 	if (bad_cmd){
4577 		mbox->cmd.result=0x01;
4578 		return RETURN_RESULT;
4579 	}
4580 
4581 	err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
4582 
4583 	if (err != CMD_OK && err != X25RES_NOT_READY)
4584 		x25_error(card, err, usr_cmd->cmd.command, usr_cmd->cmd.lcn);
4585 
4586 	if (mbox->cmd.result == X25RES_NOT_READY){
4587 		return TRY_CMD_AGAIN;
4588 	}
4589 
4590 	switch (mbox->cmd.command){
4591 
4592 	case X25_PLACE_CALL:
4593 
4594 		switch (mbox->cmd.result){
4595 
4596 		case CMD_OK:
4597 
4598 			/* Check if Place call is a wait command or a
4599                	  	 * no wait command */
4600 			if (atomic_read(&chan->common.command) & 0x80)
4601  				delay=RETURN_RESULT;
4602 			else
4603 				delay=DELAY_RESULT;
4604 
4605 
4606 			DBG_PRINTK(KERN_INFO "\n%s: PLACE CALL Binding dev %s to lcn %i\n",
4607 					card->devname,dev->name, mbox->cmd.lcn);
4608 
4609 			bind_lcn_to_dev (card, dev, mbox->cmd.lcn);
4610 			set_chan_state(dev, WAN_CONNECTING);
4611 			break;
4612 
4613 
4614 		default:
4615 			delay=RETURN_RESULT;
4616 			set_chan_state(dev, WAN_DISCONNECTED);
4617 			break;
4618 		}
4619 		break;
4620 
4621 	case X25_ACCEPT_CALL:
4622 
4623 		switch (mbox->cmd.result){
4624 
4625 		case CMD_OK:
4626 
4627 			DBG_PRINTK(KERN_INFO "\n%s: ACCEPT Binding dev %s to lcn %i\n",
4628 				card->devname,dev->name,mbox->cmd.lcn);
4629 
4630 			bind_lcn_to_dev (card, dev, mbox->cmd.lcn);
4631 
4632 			if (x25_get_chan_conf(card, chan) == CMD_OK){
4633 
4634 				set_chan_state(dev, WAN_CONNECTED);
4635 				delay=RETURN_RESULT;
4636 
4637 			}else{
4638 				if (x25_clear_call(card, usr_cmd->cmd.lcn, 0, 0) == CMD_OK){
4639 					/* if clear is successful, wait for clear confirm
4640 					 */
4641 					delay=DELAY_RESULT;
4642 				}else{
4643 					/* Do not change the state here. If we fail
4644 					 * the accept the return code is send up
4645 					 *the stack, which will ether retry
4646                                	  	 * or clear the call
4647 					 */
4648 					DBG_PRINTK(KERN_INFO
4649 						"%s: ACCEPT: STATE MAY BE CURRUPTED 2 !!!!!\n",
4650 						card->devname);
4651 					delay=RETURN_RESULT;
4652 				}
4653 			}
4654 			break;
4655 
4656 
4657 		case X25RES_ASYNC_PACKET:
4658 			delay=TRY_CMD_AGAIN;
4659 			break;
4660 
4661 		default:
4662 			DBG_PRINTK(KERN_INFO "%s: ACCEPT FAILED\n",card->devname);
4663 			if (x25_clear_call(card, usr_cmd->cmd.lcn, 0, 0) == CMD_OK){
4664 				delay=DELAY_RESULT;
4665 			}else{
4666 				/* Do not change the state here. If we fail the accept. The
4667                                  * return code is send up the stack, which will ether retry
4668                                  * or clear the call */
4669 				DBG_PRINTK(KERN_INFO
4670 					"%s: ACCEPT: STATE MAY BE CORRUPTED 1 !!!!!\n",
4671 						card->devname);
4672 				delay=RETURN_RESULT;
4673 			}
4674 		}
4675 		break;
4676 
4677 	case X25_CLEAR_CALL:
4678 
4679 		switch (mbox->cmd.result){
4680 
4681 		case CMD_OK:
4682 			DBG_PRINTK(KERN_INFO
4683 					"CALL CLEAR OK: Dev %s Mbox Lcn %i  Chan Lcn %i\n",
4684 					dev->name,mbox->cmd.lcn,chan->common.lcn);
4685 			set_chan_state(dev, WAN_DISCONNECTING);
4686 			delay = DELAY_RESULT;
4687 			break;
4688 
4689 		case X25RES_CHANNEL_IN_USE:
4690 		case X25RES_ASYNC_PACKET:
4691 			delay = TRY_CMD_AGAIN;
4692 			break;
4693 
4694 		case X25RES_LINK_NOT_IN_ABM:
4695 		case X25RES_INVAL_LCN:
4696 		case X25RES_INVAL_STATE:
4697 			set_chan_state(dev, WAN_DISCONNECTED);
4698 			delay = RETURN_RESULT;
4699 			break;
4700 
4701 		default:
4702 			/* If command did not execute because of user
4703                          * fault, do not change the state. This will
4704                          * signal the socket that clear command failed.
4705                          * User can retry or close the socket.
4706                          * When socket gets killed, it will set the
4707                          * chan->disconnect which will signal
4708                          * driver to clear the call */
4709 			printk(KERN_INFO "%s: Clear Command Failed, Rc %x\n",
4710 				card->devname,mbox->cmd.command);
4711 			delay = RETURN_RESULT;
4712 		}
4713 		break;
4714 	}
4715 
4716 	return delay;
4717 }
4718 
4719 /*===============================================================
4720  * api_incoming_call
4721  *
4722  *	Pass an incoming call request up the listening
4723  *      sock.  If the API sock is not listening reject the
4724  *      call.
4725  *
4726  *===============================================================*/
4727 
api_incoming_call(sdla_t * card,TX25Mbox * mbox,int lcn)4728 static int api_incoming_call (sdla_t* card, TX25Mbox *mbox, int lcn)
4729 {
4730 	struct sk_buff *skb;
4731 	int len = sizeof(TX25Cmd)+mbox->cmd.length;
4732 
4733 	if (alloc_and_init_skb_buf(card, &skb, len)){
4734 		printk(KERN_INFO "%s: API incoming call, no memory\n",card->devname);
4735 		return 1;
4736 	}
4737 
4738 	memcpy(skb_put(skb,len),&mbox->cmd,len);
4739 
4740 	skb->mac.raw = skb->data;
4741 	skb->protocol = htons(X25_PROT);
4742 	skb->pkt_type = WAN_PACKET_ASYNC;
4743 
4744 	if (card->func(skb,card->sk) < 0){
4745 		printk(KERN_INFO "%s: MAJOR ERROR: Failed to send up place call \n",card->devname);
4746                 wan_dev_kfree_skb(skb, FREE_READ);
4747 		return 1;
4748 	}
4749 
4750 	return 0;
4751 }
4752 
4753 /*===============================================================
4754  * send_delayed_cmd_result
4755  *
4756  *	Wait commands like PLEACE CALL or CLEAR CALL must wait
4757  *      until the result arrives. This function passes
4758  *      the result to a waiting sock.
4759  *
4760  *===============================================================*/
send_delayed_cmd_result(sdla_t * card,netdevice_t * dev,TX25Mbox * mbox)4761 static void send_delayed_cmd_result(sdla_t *card, netdevice_t *dev, TX25Mbox* mbox)
4762 {
4763 	x25_channel_t *chan = dev->priv;
4764 	mbox_cmd_t *usr_cmd = (mbox_cmd_t *)chan->common.mbox;
4765 	struct sk_buff *skb;
4766 	int len=sizeof(unsigned char);
4767 
4768 	atomic_set(&chan->common.command,0);
4769 
4770 	/* If the sock is in the process of unlinking the
4771 	 * driver from the socket, we must get out.
4772 	 * This never happends but is a sanity check. */
4773 	if (test_bit(0,&chan->common.common_critical)){
4774 		return;
4775 	}
4776 
4777 	if (!usr_cmd || !chan->common.sk || !chan->common.func){
4778 		DBG_PRINTK(KERN_INFO "Delay result: Sock not bounded sk: %u, func: %u, mbox: %u\n",
4779 			(unsigned int)chan->common.sk,
4780 			(unsigned int)chan->common.func,
4781 			(unsigned int)usr_cmd);
4782 		return;
4783 	}
4784 
4785 	memcpy(&usr_cmd->cmd, &mbox->cmd, sizeof(TX25Cmd));
4786 	if (mbox->cmd.length > 0){
4787 		memcpy(usr_cmd->data, mbox->data, mbox->cmd.length);
4788 	}
4789 
4790 	if (alloc_and_init_skb_buf(card,&skb,len)){
4791 		printk(KERN_INFO "Delay result: No sock buffers\n");
4792 		return;
4793 	}
4794 
4795 	memcpy(skb_put(skb,len),&mbox->cmd.command,len);
4796 
4797 	skb->mac.raw = skb->data;
4798 	skb->pkt_type = WAN_PACKET_CMD;
4799 
4800 	chan->common.func(skb,dev,chan->common.sk);
4801 }
4802 
4803 /*===============================================================
4804  * clear_confirm_event
4805  *
4806  * 	Pass the clear confirmation event up the sock. The
4807  *      API will disconnect only after the clear confirmation
4808  *      has been received.
4809  *
4810  *      Depending on the state, clear confirmation could
4811  *      be an OOB event, or a result of an API command.
4812  *===============================================================*/
4813 
clear_confirm_event(sdla_t * card,TX25Mbox * mb)4814 static int clear_confirm_event (sdla_t *card, TX25Mbox* mb)
4815 {
4816 	netdevice_t *dev;
4817 	x25_channel_t *chan;
4818 	unsigned char old_state;
4819 
4820 	dev = find_channel(card,mb->cmd.lcn);
4821 	if (!dev){
4822 		DBG_PRINTK(KERN_INFO "%s: *** GOT CLEAR BUT NO DEV %i\n",
4823 				card->devname,mb->cmd.lcn);
4824 		return 0;
4825 	}
4826 
4827 	chan=dev->priv;
4828 	DBG_PRINTK(KERN_INFO "%s: GOT CLEAR CONFIRM %s:  Mbox lcn %i  Chan lcn %i\n",
4829 			card->devname, dev->name, mb->cmd.lcn, chan->common.lcn);
4830 
4831 	/* If not API fall through to default.
4832 	 * If API, send the result to a waiting
4833          * socket.
4834 	 */
4835 
4836 	old_state = chan->common.state;
4837 	set_chan_state(dev, WAN_DISCONNECTED);
4838 
4839 	if (chan->common.usedby == API){
4840 		switch (old_state) {
4841 
4842 		case WAN_DISCONNECTING:
4843 		case WAN_CONNECTING:
4844 			send_delayed_cmd_result(card,dev,mb);
4845 			break;
4846 		case WAN_CONNECTED:
4847 			send_oob_msg(card,dev,mb);
4848 			break;
4849 		}
4850 		return 1;
4851 	}
4852 
4853 	return 0;
4854 }
4855 
4856 /*===============================================================
4857  * send_oob_msg
4858  *
4859  *    Construct an NEM Message and pass it up the connected
4860  *    sock. If the sock is not bounded discard the NEM.
4861  *
4862  *===============================================================*/
4863 
send_oob_msg(sdla_t * card,netdevice_t * dev,TX25Mbox * mbox)4864 static void send_oob_msg (sdla_t *card, netdevice_t *dev, TX25Mbox *mbox)
4865 {
4866 	x25_channel_t *chan = dev->priv;
4867 	mbox_cmd_t *usr_cmd = (mbox_cmd_t *)chan->common.mbox;
4868 	struct sk_buff *skb;
4869 	int len=sizeof(x25api_hdr_t)+mbox->cmd.length;
4870 	x25api_t *api_hdr;
4871 
4872 	/* If the sock is in the process of unlinking the
4873 	 * driver from the socket, we must get out.
4874 	 * This never happends but is a sanity check. */
4875 	if (test_bit(0,&chan->common.common_critical)){
4876 		return;
4877 	}
4878 
4879 	if (!usr_cmd || !chan->common.sk || !chan->common.func){
4880 		DBG_PRINTK(KERN_INFO "OOB MSG: Sock not bounded\n");
4881 		return;
4882 	}
4883 
4884 	memcpy(&usr_cmd->cmd, &mbox->cmd, sizeof(TX25Cmd));
4885 	if (mbox->cmd.length > 0){
4886 		memcpy(usr_cmd->data, mbox->data, mbox->cmd.length);
4887 	}
4888 
4889 	if (alloc_and_init_skb_buf(card,&skb,len)){
4890 		printk(KERN_INFO "%s: OOB MSG: No sock buffers\n",card->devname);
4891 		return;
4892 	}
4893 
4894 	api_hdr = (x25api_t*)skb_put(skb,len);
4895 	api_hdr->hdr.pktType = mbox->cmd.pktType & 0x7F;
4896 	api_hdr->hdr.qdm     = mbox->cmd.qdm;
4897 	api_hdr->hdr.cause   = mbox->cmd.cause;
4898 	api_hdr->hdr.diagn   = mbox->cmd.diagn;
4899 	api_hdr->hdr.length  = mbox->cmd.length;
4900 	api_hdr->hdr.result  = mbox->cmd.result;
4901 	api_hdr->hdr.lcn     = mbox->cmd.lcn;
4902 
4903 	if (mbox->cmd.length > 0){
4904 		memcpy(api_hdr->data,mbox->data,mbox->cmd.length);
4905 	}
4906 
4907 	skb->mac.raw = skb->data;
4908 	skb->pkt_type = WAN_PACKET_ERR;
4909 
4910 	if (chan->common.func(skb,dev,chan->common.sk) < 0){
4911 		if (bh_enqueue(dev,skb)){
4912 			printk(KERN_INFO "%s: Dropping OOB MSG\n",card->devname);
4913                 	wan_dev_kfree_skb(skb, FREE_READ);
4914 		}
4915 	}
4916 
4917 	DBG_PRINTK(KERN_INFO "%s: OOB MSG OK, %s, lcn %i\n",
4918 			card->devname, dev->name, mbox->cmd.lcn);
4919 }
4920 
4921 /*===============================================================
4922  *  alloc_and_init_skb_buf
4923  *
4924  *	Allocate and initialize an skb buffer.
4925  *
4926  *===============================================================*/
4927 
alloc_and_init_skb_buf(sdla_t * card,struct sk_buff ** skb,int len)4928 static int alloc_and_init_skb_buf (sdla_t *card, struct sk_buff **skb, int len)
4929 {
4930 	struct sk_buff *new_skb = *skb;
4931 
4932 	new_skb = dev_alloc_skb(len + X25_HRDHDR_SZ);
4933 	if (new_skb == NULL){
4934 		printk(KERN_INFO "%s: no socket buffers available!\n",
4935 			card->devname);
4936 		return 1;
4937 	}
4938 
4939 	if (skb_tailroom(new_skb) < len){
4940 		/* No room for the packet. Call off the whole thing! */
4941                 wan_dev_kfree_skb(new_skb, FREE_READ);
4942 		printk(KERN_INFO "%s: Listen: unexpectedly long packet sequence\n"
4943 			,card->devname);
4944 		*skb = NULL;
4945 		return 1;
4946 	}
4947 
4948 	*skb = new_skb;
4949 	return 0;
4950 
4951 }
4952 
4953 /*===============================================================
4954  *  api_oob_event
4955  *
4956  *	Send an OOB event up to the sock
4957  *
4958  *===============================================================*/
4959 
api_oob_event(sdla_t * card,TX25Mbox * mbox)4960 static void api_oob_event (sdla_t *card,TX25Mbox *mbox)
4961 {
4962 	netdevice_t *dev = find_channel(card,mbox->cmd.lcn);
4963 	x25_channel_t *chan;
4964 
4965 	if (!dev)
4966 		return;
4967 
4968 	chan=dev->priv;
4969 
4970 	if (chan->common.usedby == API)
4971 		send_oob_msg(card,dev,mbox);
4972 
4973 }
4974 
4975 
4976 
4977 
channel_disconnect(sdla_t * card,netdevice_t * dev)4978 static int channel_disconnect (sdla_t* card, netdevice_t *dev)
4979 {
4980 
4981 	int err;
4982 	x25_channel_t *chan = dev->priv;
4983 
4984 	DBG_PRINTK(KERN_INFO "%s: TIMER: %s, Device down disconnecting\n",
4985 				card->devname,dev->name);
4986 
4987 	if (chan->common.svc){
4988 		err = x25_clear_call(card,chan->common.lcn,0,0);
4989 	}else{
4990 		/* If channel is PVC or LAPB HDLC, there is no call
4991                  * to be cleared, thus drop down to the default
4992                  * area
4993 	         */
4994 		err = 1;
4995 	}
4996 
4997 	switch (err){
4998 
4999 		case X25RES_CHANNEL_IN_USE:
5000 		case X25RES_NOT_READY:
5001 			err = TRY_CMD_AGAIN;
5002 			break;
5003 		case CMD_OK:
5004 			DBG_PRINTK(KERN_INFO "CALL CLEAR OK: Dev %s Chan Lcn %i\n",
5005 						dev->name,chan->common.lcn);
5006 
5007 			set_chan_state(dev,WAN_DISCONNECTING);
5008 			atomic_set(&chan->common.command,0);
5009 			err = DELAY_RESULT;
5010 			break;
5011 		default:
5012 			/* If LAPB HDLC protocol, bring the whole link down
5013                          * once the application terminates
5014 			 */
5015 
5016 			set_chan_state(dev,WAN_DISCONNECTED);
5017 
5018 			if (card->u.x.LAPB_hdlc){
5019 				DBG_PRINTK(KERN_INFO "LAPB: Disconnecting Link\n");
5020 				hdlc_link_down (card);
5021 			}
5022 			atomic_set(&chan->common.command,0);
5023 			err = RETURN_RESULT;
5024 			break;
5025 	}
5026 
5027 	return err;
5028 }
5029 
hdlc_link_down(sdla_t * card)5030 static void hdlc_link_down (sdla_t *card)
5031 {
5032 	TX25Mbox* mbox = card->mbox;
5033 	int retry = 5;
5034 	int err=0;
5035 
5036 	do {
5037 		memset(mbox,0,sizeof(TX25Mbox));
5038 		mbox->cmd.command = X25_HDLC_LINK_DISC;
5039 		mbox->cmd.length = 1;
5040 		mbox->data[0]=0;
5041 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
5042 
5043 	} while (err && retry-- && x25_error(card, err, X25_HDLC_LINK_DISC, 0));
5044 
5045 	if (err)
5046 		printk(KERN_INFO "%s: Hdlc Link Down Failed %x\n",card->devname,err);
5047 
5048 	disconnect (card);
5049 
5050 }
5051 
check_bad_command(sdla_t * card,netdevice_t * dev)5052 static int check_bad_command (sdla_t* card, netdevice_t *dev)
5053 {
5054 	x25_channel_t *chan = dev->priv;
5055 	int bad_cmd = 0;
5056 
5057 	switch (atomic_read(&chan->common.command)&0x7F){
5058 
5059 		case X25_PLACE_CALL:
5060 			if (chan->common.state != WAN_DISCONNECTED)
5061 				bad_cmd=1;
5062 			break;
5063 		case X25_CLEAR_CALL:
5064 			if (chan->common.state == WAN_DISCONNECTED)
5065 				bad_cmd=1;
5066 			break;
5067 		case X25_ACCEPT_CALL:
5068 			if (chan->common.state != WAN_CONNECTING)
5069 				bad_cmd=1;
5070 			break;
5071 		case X25_RESET:
5072 			if (chan->common.state != WAN_CONNECTED)
5073 				bad_cmd=1;
5074 			break;
5075 		default:
5076 			bad_cmd=1;
5077 			break;
5078 	}
5079 
5080 	if (bad_cmd){
5081 		printk(KERN_INFO "%s: Invalid State, BAD Command %x, dev %s, lcn %i, st %i\n",
5082 			card->devname,atomic_read(&chan->common.command),dev->name,
5083 			chan->common.lcn, chan->common.state);
5084 	}
5085 
5086 	return bad_cmd;
5087 }
5088 
5089 
5090 
5091 /*************************** XPIPEMON FUNCTIONS **************************/
5092 
5093 /*==============================================================================
5094  * Process UDP call of type XPIPE
5095  */
5096 
process_udp_mgmt_pkt(sdla_t * card)5097 static int process_udp_mgmt_pkt(sdla_t *card)
5098 {
5099 	int            c_retry = MAX_CMD_RETRY;
5100 	unsigned int   len;
5101 	struct sk_buff *new_skb;
5102 	TX25Mbox       *mbox = card->mbox;
5103 	int            err;
5104 	int            udp_mgmt_req_valid = 1;
5105 	netdevice_t  *dev;
5106         x25_channel_t  *chan;
5107 	unsigned short lcn;
5108 	struct timeval tv;
5109 
5110 
5111 	x25_udp_pkt_t *x25_udp_pkt;
5112 	x25_udp_pkt = (x25_udp_pkt_t *)card->u.x.udp_pkt_data;
5113 
5114 	dev = card->u.x.udp_dev;
5115 	chan = dev->priv;
5116 	lcn = chan->common.lcn;
5117 
5118 	switch(x25_udp_pkt->cblock.command) {
5119 
5120 		/* XPIPE_ENABLE_TRACE */
5121 		case XPIPE_ENABLE_TRACING:
5122 
5123 		/* XPIPE_GET_TRACE_INFO */
5124 		case XPIPE_GET_TRACE_INFO:
5125 
5126 		/* SET FT1 MODE */
5127 		case XPIPE_SET_FT1_MODE:
5128 
5129 			if(card->u.x.udp_pkt_src == UDP_PKT_FRM_NETWORK) {
5130                     		++chan->pipe_mgmt_stat.UDP_PIPE_mgmt_direction_err;
5131 				udp_mgmt_req_valid = 0;
5132 				break;
5133 			}
5134 
5135 		/* XPIPE_FT1_READ_STATUS */
5136 		case XPIPE_FT1_READ_STATUS:
5137 
5138 		/* FT1 MONITOR STATUS */
5139 		case XPIPE_FT1_STATUS_CTRL:
5140 			if(card->hw.fwid !=  SFID_X25_508) {
5141 				++chan->pipe_mgmt_stat.UDP_PIPE_mgmt_adptr_type_err;
5142 				udp_mgmt_req_valid = 0;
5143 				break;
5144 			}
5145 		default:
5146 			break;
5147        	}
5148 
5149 	if(!udp_mgmt_req_valid) {
5150            	/* set length to 0 */
5151 		x25_udp_pkt->cblock.length = 0;
5152 		/* set return code */
5153 		x25_udp_pkt->cblock.result = (card->hw.fwid != SFID_X25_508) ? 0x1F : 0xCD;
5154 
5155 	} else {
5156 
5157 		switch (x25_udp_pkt->cblock.command) {
5158 
5159 
5160 		case XPIPE_FLUSH_DRIVER_STATS:
5161 			init_x25_channel_struct(chan);
5162 			init_global_statistics(card);
5163 			mbox->cmd.length = 0;
5164 			break;
5165 
5166 
5167 		case XPIPE_DRIVER_STAT_IFSEND:
5168 			memcpy(x25_udp_pkt->data, &chan->if_send_stat, sizeof(if_send_stat_t));
5169 			mbox->cmd.length = sizeof(if_send_stat_t);
5170 			x25_udp_pkt->cblock.length =  mbox->cmd.length;
5171 			break;
5172 
5173 		case XPIPE_DRIVER_STAT_INTR:
5174 			memcpy(&x25_udp_pkt->data[0], &card->statistics, sizeof(global_stats_t));
5175                         memcpy(&x25_udp_pkt->data[sizeof(global_stats_t)],
5176                                 &chan->rx_intr_stat, sizeof(rx_intr_stat_t));
5177 
5178 			mbox->cmd.length = sizeof(global_stats_t) +
5179 					sizeof(rx_intr_stat_t);
5180 			x25_udp_pkt->cblock.length =  mbox->cmd.length;
5181 			break;
5182 
5183 		case XPIPE_DRIVER_STAT_GEN:
5184                         memcpy(x25_udp_pkt->data,
5185                                 &chan->pipe_mgmt_stat.UDP_PIPE_mgmt_kmalloc_err,
5186                                 sizeof(pipe_mgmt_stat_t));
5187 
5188                         memcpy(&x25_udp_pkt->data[sizeof(pipe_mgmt_stat_t)],
5189                                &card->statistics, sizeof(global_stats_t));
5190 
5191                         x25_udp_pkt->cblock.result = 0;
5192                         x25_udp_pkt->cblock.length = sizeof(global_stats_t)+
5193                                                      sizeof(rx_intr_stat_t);
5194                         mbox->cmd.length = x25_udp_pkt->cblock.length;
5195                         break;
5196 
5197 		case XPIPE_ROUTER_UP_TIME:
5198 			do_gettimeofday(&tv);
5199 			chan->router_up_time = tv.tv_sec - chan->router_start_time;
5200     	                *(unsigned long *)&x25_udp_pkt->data = chan->router_up_time;
5201 			x25_udp_pkt->cblock.length = mbox->cmd.length = 4;
5202 			x25_udp_pkt->cblock.result = 0;
5203 			break;
5204 
5205 		default :
5206 
5207 			do {
5208 				memcpy(&mbox->cmd, &x25_udp_pkt->cblock.command, sizeof(TX25Cmd));
5209 				if(mbox->cmd.length){
5210 					memcpy(&mbox->data,
5211 					       (char *)x25_udp_pkt->data,
5212 					       mbox->cmd.length);
5213 				}
5214 
5215 				err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
5216 			} while (err && c_retry-- && x25_error(card, err, mbox->cmd.command, 0));
5217 
5218 
5219 			if ( err == CMD_OK ||
5220 			    (err == 1 &&
5221 			     (mbox->cmd.command == 0x06 ||
5222 			      mbox->cmd.command == 0x16)  ) ){
5223 
5224 				++chan->pipe_mgmt_stat.UDP_PIPE_mgmt_adptr_cmnd_OK;
5225 			} else {
5226 				++chan->pipe_mgmt_stat.UDP_PIPE_mgmt_adptr_cmnd_timeout;
5227 			}
5228 
5229 			  /* copy the result back to our buffer */
5230 			memcpy(&x25_udp_pkt->cblock.command, &mbox->cmd, sizeof(TX25Cmd));
5231 
5232       	         	if(mbox->cmd.length) {
5233         	               memcpy(&x25_udp_pkt->data, &mbox->data, mbox->cmd.length);
5234 			}
5235 			break;
5236 
5237 		} //switch
5238 
5239         }
5240 
5241         /* Fill UDP TTL */
5242 
5243 	x25_udp_pkt->ip_pkt.ttl = card->wandev.ttl;
5244         len = reply_udp(card->u.x.udp_pkt_data, mbox->cmd.length);
5245 
5246 
5247         if(card->u.x.udp_pkt_src == UDP_PKT_FRM_NETWORK) {
5248 
5249 		err = x25_send(card, lcn, 0, len, card->u.x.udp_pkt_data);
5250 		if (!err)
5251 			++chan->pipe_mgmt_stat.UDP_PIPE_mgmt_adptr_send_passed;
5252 		else
5253 			++chan->pipe_mgmt_stat.UDP_PIPE_mgmt_adptr_send_failed;
5254 
5255 	} else {
5256 
5257 		/* Allocate socket buffer */
5258 		if((new_skb = dev_alloc_skb(len)) != NULL) {
5259 			void *buf;
5260 
5261 			/* copy data into new_skb */
5262 			buf = skb_put(new_skb, len);
5263 			memcpy(buf, card->u.x.udp_pkt_data, len);
5264 
5265 			/* Decapsulate packet and pass it up the protocol
5266 			   stack */
5267 			new_skb->dev = dev;
5268 
5269 			if (chan->common.usedby == API)
5270                         	new_skb->protocol = htons(X25_PROT);
5271 			else
5272 				new_skb->protocol = htons(ETH_P_IP);
5273 
5274                         new_skb->mac.raw = new_skb->data;
5275 
5276 			netif_rx(new_skb);
5277 			++chan->pipe_mgmt_stat.UDP_PIPE_mgmt_passed_to_stack;
5278 
5279 		} else {
5280 			++chan->pipe_mgmt_stat.UDP_PIPE_mgmt_no_socket;
5281 			printk(KERN_INFO
5282 			"%s: UDP mgmt cmnd, no socket buffers available!\n",
5283 			card->devname);
5284             	}
5285         }
5286 
5287 	card->u.x.udp_pkt_lgth = 0;
5288 
5289 	return 1;
5290 }
5291 
5292 
5293 /*==============================================================================
5294  * Determine what type of UDP call it is. DRVSTATS or XPIPE8ND ?
5295  */
udp_pkt_type(struct sk_buff * skb,sdla_t * card)5296 static int udp_pkt_type( struct sk_buff *skb, sdla_t* card )
5297 {
5298 	x25_udp_pkt_t *x25_udp_pkt = (x25_udp_pkt_t *)skb->data;
5299 
5300         if((x25_udp_pkt->ip_pkt.protocol == UDPMGMT_UDP_PROTOCOL) &&
5301 		(x25_udp_pkt->ip_pkt.ver_inet_hdr_length == 0x45) &&
5302 		(x25_udp_pkt->udp_pkt.udp_dst_port == ntohs(card->wandev.udp_port)) &&
5303 		(x25_udp_pkt->wp_mgmt.request_reply == UDPMGMT_REQUEST)) {
5304 
5305                         if(!strncmp(x25_udp_pkt->wp_mgmt.signature,
5306                                 UDPMGMT_XPIPE_SIGNATURE, 8)){
5307                                 return UDP_XPIPE_TYPE;
5308 			}else{
5309 				printk(KERN_INFO "%s: UDP Packet, Failed Signature !\n",
5310 					card->devname);
5311 			}
5312 	}
5313 
5314         return UDP_INVALID_TYPE;
5315 }
5316 
5317 
5318 /*============================================================================
5319  * Reply to UDP Management system.
5320  * Return nothing.
5321  */
reply_udp(unsigned char * data,unsigned int mbox_len)5322 static int reply_udp( unsigned char *data, unsigned int mbox_len )
5323 {
5324 	unsigned short len, udp_length, temp, ip_length;
5325 	unsigned long ip_temp;
5326 	int even_bound = 0;
5327 
5328 
5329 	x25_udp_pkt_t *x25_udp_pkt = (x25_udp_pkt_t *)data;
5330 
5331 	/* Set length of packet */
5332 	len = sizeof(ip_pkt_t)+
5333 	      sizeof(udp_pkt_t)+
5334 	      sizeof(wp_mgmt_t)+
5335 	      sizeof(cblock_t)+
5336 	      mbox_len;
5337 
5338 
5339 	/* fill in UDP reply */
5340 	x25_udp_pkt->wp_mgmt.request_reply = UDPMGMT_REPLY;
5341 
5342 	/* fill in UDP length */
5343 	udp_length = sizeof(udp_pkt_t)+
5344 		     sizeof(wp_mgmt_t)+
5345 		     sizeof(cblock_t)+
5346 		     mbox_len;
5347 
5348 
5349 	/* put it on an even boundary */
5350 	if ( udp_length & 0x0001 ) {
5351 		udp_length += 1;
5352 		len += 1;
5353 		even_bound = 1;
5354 	}
5355 
5356 	temp = (udp_length<<8)|(udp_length>>8);
5357 	x25_udp_pkt->udp_pkt.udp_length = temp;
5358 
5359 	/* swap UDP ports */
5360 	temp = x25_udp_pkt->udp_pkt.udp_src_port;
5361 	x25_udp_pkt->udp_pkt.udp_src_port =
5362 			x25_udp_pkt->udp_pkt.udp_dst_port;
5363 	x25_udp_pkt->udp_pkt.udp_dst_port = temp;
5364 
5365 
5366 
5367 	/* add UDP pseudo header */
5368 	temp = 0x1100;
5369 	*((unsigned short *)
5370 		(x25_udp_pkt->data+mbox_len+even_bound)) = temp;
5371 	temp = (udp_length<<8)|(udp_length>>8);
5372 	*((unsigned short *)
5373 		(x25_udp_pkt->data+mbox_len+even_bound+2)) = temp;
5374 
5375 	/* calculate UDP checksum */
5376 	x25_udp_pkt->udp_pkt.udp_checksum = 0;
5377 
5378 	x25_udp_pkt->udp_pkt.udp_checksum =
5379 		calc_checksum(&data[UDP_OFFSET], udp_length+UDP_OFFSET);
5380 
5381 	/* fill in IP length */
5382 	ip_length = len;
5383 	temp = (ip_length<<8)|(ip_length>>8);
5384 	x25_udp_pkt->ip_pkt.total_length = temp;
5385 
5386 	/* swap IP addresses */
5387 	ip_temp = x25_udp_pkt->ip_pkt.ip_src_address;
5388 	x25_udp_pkt->ip_pkt.ip_src_address =
5389 				x25_udp_pkt->ip_pkt.ip_dst_address;
5390 	x25_udp_pkt->ip_pkt.ip_dst_address = ip_temp;
5391 
5392 
5393 	/* fill in IP checksum */
5394 	x25_udp_pkt->ip_pkt.hdr_checksum = 0;
5395 	x25_udp_pkt->ip_pkt.hdr_checksum = calc_checksum(data, sizeof(ip_pkt_t));
5396 
5397 	return len;
5398 } /* reply_udp */
5399 
calc_checksum(char * data,int len)5400 unsigned short calc_checksum (char *data, int len)
5401 {
5402 	unsigned short temp;
5403 	unsigned long sum=0;
5404 	int i;
5405 
5406 	for( i = 0; i <len; i+=2 ) {
5407 		memcpy(&temp,&data[i],2);
5408 		sum += (unsigned long)temp;
5409 	}
5410 
5411 	while (sum >> 16 ) {
5412 		sum = (sum & 0xffffUL) + (sum >> 16);
5413 	}
5414 
5415 	temp = (unsigned short)sum;
5416 	temp = ~temp;
5417 
5418 	if( temp == 0 )
5419 		temp = 0xffff;
5420 
5421 	return temp;
5422 }
5423 
5424 /*=============================================================================
5425  * Store a UDP management packet for later processing.
5426  */
5427 
store_udp_mgmt_pkt(int udp_type,char udp_pkt_src,sdla_t * card,netdevice_t * dev,struct sk_buff * skb,int lcn)5428 static int store_udp_mgmt_pkt(int udp_type, char udp_pkt_src, sdla_t* card,
5429                                 netdevice_t *dev, struct sk_buff *skb, int lcn)
5430 {
5431         int udp_pkt_stored = 0;
5432 
5433         if(!card->u.x.udp_pkt_lgth && (skb->len <= MAX_LGTH_UDP_MGNT_PKT)){
5434                 card->u.x.udp_pkt_lgth = skb->len;
5435                 card->u.x.udp_type = udp_type;
5436                 card->u.x.udp_pkt_src = udp_pkt_src;
5437                 card->u.x.udp_lcn = lcn;
5438 		card->u.x.udp_dev = dev;
5439                 memcpy(card->u.x.udp_pkt_data, skb->data, skb->len);
5440                 card->u.x.timer_int_enabled |= TMR_INT_ENABLED_UDP_PKT;
5441                 udp_pkt_stored = 1;
5442 
5443         }else{
5444                 printk(KERN_INFO "%s: ERROR: UDP packet not stored for LCN %d\n",
5445 							card->devname,lcn);
5446 	}
5447 
5448         if(udp_pkt_src == UDP_PKT_FRM_STACK){
5449                 wan_dev_kfree_skb(skb, FREE_WRITE);
5450 	}else{
5451                 wan_dev_kfree_skb(skb, FREE_READ);
5452 	}
5453 
5454         return(udp_pkt_stored);
5455 }
5456 
5457 
5458 
5459 /*=============================================================================
5460  * Initial the ppp_private_area structure.
5461  */
init_x25_channel_struct(x25_channel_t * chan)5462 static void init_x25_channel_struct( x25_channel_t *chan )
5463 {
5464 	memset(&chan->if_send_stat.if_send_entry,0,sizeof(if_send_stat_t));
5465 	memset(&chan->rx_intr_stat.rx_intr_no_socket,0,sizeof(rx_intr_stat_t));
5466 	memset(&chan->pipe_mgmt_stat.UDP_PIPE_mgmt_kmalloc_err,0,sizeof(pipe_mgmt_stat_t));
5467 }
5468 
5469 /*============================================================================
5470  * Initialize Global Statistics
5471  */
init_global_statistics(sdla_t * card)5472 static void init_global_statistics( sdla_t *card )
5473 {
5474 	memset(&card->statistics.isr_entry,0,sizeof(global_stats_t));
5475 }
5476 
5477 
5478 /*===============================================================
5479  * SMP Support
5480  * ==============================================================*/
5481 
S508_S514_lock(sdla_t * card,unsigned long * smp_flags)5482 static void S508_S514_lock(sdla_t *card, unsigned long *smp_flags)
5483 {
5484 	spin_lock_irqsave(&card->wandev.lock, *smp_flags);
5485 }
S508_S514_unlock(sdla_t * card,unsigned long * smp_flags)5486 static void S508_S514_unlock(sdla_t *card, unsigned long *smp_flags)
5487 {
5488 	spin_unlock_irqrestore(&card->wandev.lock, *smp_flags);
5489 }
5490 
5491 /*===============================================================
5492  * x25_timer_routine
5493  *
5494  * 	A more efficient polling routine.  Each half a second
5495  * 	queue a polling task. We want to do the polling in a
5496  * 	task not timer, because timer runs in interrupt time.
5497  *
5498  * 	FIXME Polling should be rethinked.
5499  *==============================================================*/
5500 
x25_timer_routine(unsigned long data)5501 static void x25_timer_routine(unsigned long data)
5502 {
5503 	sdla_t *card = (sdla_t*)data;
5504 
5505 	if (!card->wandev.dev){
5506 		printk(KERN_INFO "%s: Stopping the X25 Poll Timer: No Dev.\n",
5507 				card->devname);
5508 		return;
5509 	}
5510 
5511 	if (card->open_cnt != card->u.x.num_of_ch){
5512 		printk(KERN_INFO "%s: Stopping the X25 Poll Timer: Interface down.\n",
5513 				card->devname);
5514 		return;
5515 	}
5516 
5517 	if (test_bit(PERI_CRIT,&card->wandev.critical)){
5518 		printk(KERN_INFO "%s: Stopping the X25 Poll Timer: Shutting down.\n",
5519 				card->devname);
5520 		return;
5521 	}
5522 
5523 	if (!test_and_set_bit(POLL_CRIT,&card->wandev.critical)){
5524 		trigger_x25_poll(card);
5525 	}
5526 
5527 	card->u.x.x25_timer.expires=jiffies+(HZ>>1);
5528 	add_timer(&card->u.x.x25_timer);
5529 	return;
5530 }
5531 
disable_comm_shutdown(sdla_t * card)5532 void disable_comm_shutdown(sdla_t *card)
5533 {
5534 	TX25Mbox* mbox = card->mbox;
5535 	int err;
5536 
5537 	/* Turn of interrutps */
5538 	mbox->data[0] = 0;
5539 	if (card->hw.fwid == SFID_X25_508){
5540 		mbox->data[1] = card->hw.irq;
5541 		mbox->data[2] = 2;
5542 		mbox->cmd.length = 3;
5543 	}else {
5544 	 	mbox->cmd.length  = 1;
5545 	}
5546 	mbox->cmd.command = X25_SET_INTERRUPT_MODE;
5547 	err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
5548 	if (err)
5549 		printk(KERN_INFO "INTERRUPT OFF FAIED %x\n",err);
5550 
5551 	/* Bring down HDLC */
5552 	mbox->cmd.command = X25_HDLC_LINK_CLOSE;
5553 	mbox->cmd.length  = 0;
5554 	err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
5555 	if (err)
5556 		printk(KERN_INFO "LINK CLOSED FAILED %x\n",err);
5557 
5558 
5559 	/* Brind down DTR */
5560 	mbox->data[0] = 0;
5561 	mbox->data[2] = 0;
5562 	mbox->data[1] = 0x01;
5563 	mbox->cmd.length  = 3;
5564 	mbox->cmd.command = X25_SET_GLOBAL_VARS;
5565 	err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
5566 	if (err)
5567 		printk(KERN_INFO "DTR DOWN FAILED %x\n",err);
5568 
5569 }
5570 
5571 MODULE_LICENSE("GPL");
5572 
5573 /****** End *****************************************************************/
5574