sdla_x25.c 149 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3
/*****************************************************************************
* sdla_x25.c	WANPIPE(tm) Multiprotocol WAN Link Driver.  X.25 module.
*
Linus Torvalds's avatar
Linus Torvalds committed
4
* Author:	Nenad Corbic	<ncorbic@sangoma.com>
Linus Torvalds's avatar
Linus Torvalds committed
5
*
Linus Torvalds's avatar
Linus Torvalds committed
6
* Copyright:	(c) 1995-2001 Sangoma Technologies Inc.
Linus Torvalds's avatar
Linus Torvalds committed
7 8 9 10 11 12
*
*		This program is free software; you can redistribute it and/or
*		modify it under the terms of the GNU General Public License
*		as published by the Free Software Foundation; either version
*		2 of the License, or (at your option) any later version.
* ============================================================================
Linus Torvalds's avatar
Linus Torvalds committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
* Apr 03, 2001  Nenad Corbic	 o Fixed the rx_skb=NULL bug in x25 in rx_intr().
* Dec 26, 2000  Nenad Corbic	 o Added a new polling routine, that uses
*                                  a kernel timer (more efficient).
* Dec 25, 2000  Nenad Corbic	 o Updated for 2.4.X kernel
* Jul 26, 2000  Nenad Corbic	 o Increased the local packet buffering
* 				   for API to 4096+header_size. 
* Jul 17, 2000  Nenad Corbic	 o Fixed the x25 startup bug. Enable 
* 				   communications only after all interfaces
* 				   come up.  HIGH SVC/PVC is used to calculate
* 				   the number of channels.
*                                  Enable protocol only after all interfaces
*                                  are enabled.
* Jul 10, 2000	Nenad Corbic	 o Fixed the M_BIT bug. 
* Apr 25, 2000  Nenad Corbic	 o Pass Modem messages to the API.
*                                  Disable idle timeout in X25 API.
* Apr 14, 2000  Nenad Corbic	 o Fixed: Large LCN number support.
*                                  Maximum LCN number is 4095.
*                                  Maximum number of X25 channels is 255.
* Apr 06, 2000  Nenad Corbic	 o Added SMP Support.
* Mar 29, 2000  Nenad Corbic	 o Added support for S514 PCI Card
* Mar 23, 2000  Nenad Corbic	 o Improved task queue, BH handling.
* Mar 14, 2000  Nenad Corbic  	 o Updated Protocol Violation handling
*                                  routines.  Bug Fix.
* Mar 10, 2000  Nenad Corbic	 o Bug Fix: corrupted mbox recovery.
* Mar 09, 2000  Nenad Corbic     o Fixed the auto HDLC bug.
* Mar 08, 2000	Nenad Corbic     o Fixed LAPB HDLC startup problems.
*                                  Application must bring the link up 
*                                  before tx/rx, and bring the 
*                                  link down on close().
* Mar 06, 2000	Nenad Corbic	 o Added an option for logging call setup 
*                                  information. 
* Feb 29, 2000  Nenad Corbic 	 o Added support for LAPB HDLC API
* Feb 25, 2000  Nenad Corbic     o Fixed the modem failure handling.
*                                  No Modem OOB message will be passed 
*                                  to the user.
* Feb 21, 2000  Nenad Corbic 	 o Added Xpipemon Debug Support
* Dec 30, 1999 	Nenad Corbic	 o Socket based X25API 
* Sep 17, 1998	Jaspreet Singh	 o Updates for 2.2.X  kernel
* Mar 15, 1998	Alan Cox	 o 2.1.x porting
* Dec 19, 1997	Jaspreet Singh	 o Added multi-channel IPX support
Linus Torvalds's avatar
Linus Torvalds committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
* Nov 27, 1997	Jaspreet Singh	 o Added protection against enabling of irqs
*				   when they are disabled.
* Nov 17, 1997  Farhan Thawar    o Added IPX support
*				 o Changed if_send() to now buffer packets when
*				   the board is busy
*				 o Removed queueing of packets via the polling
*				   routing
*				 o Changed if_send() critical flags to properly
*				   handle race conditions
* Nov 06, 1997  Farhan Thawar    o Added support for SVC timeouts
*				 o Changed PVC encapsulation to ETH_P_IP
* Jul 21, 1997  Jaspreet Singh	 o Fixed freeing up of buffers using kfree()
*				   when packets are received.
* Mar 11, 1997  Farhan Thawar   Version 3.1.1
*                                o added support for V35
*                                o changed if_send() to return 0 if
*                                  wandev.critical() is true
*                                o free socket buffer in if_send() if
*                                  returning 0
*                                o added support for single '@' address to
*                                  accept all incoming calls
*                                o fixed bug in set_chan_state() to disconnect
* Jan 15, 1997	Gene Kozin	Version 3.1.0
*				 o implemented exec() entry point
* Jan 07, 1997	Gene Kozin	Initial version.
*****************************************************************************/

Linus Torvalds's avatar
Linus Torvalds committed
80 81 82
/*======================================================
 * 	Includes 
 *=====================================================*/
Linus Torvalds's avatar
Linus Torvalds committed
83

Linus Torvalds's avatar
Linus Torvalds committed
84
#include <linux/module.h>
Linus Torvalds's avatar
Linus Torvalds committed
85 86 87 88
#include <linux/kernel.h>	/* printk(), and other useful stuff */
#include <linux/stddef.h>	/* offsetof(), etc. */
#include <linux/errno.h>	/* return codes */
#include <linux/string.h>	/* inline memset(), etc. */
Linus Torvalds's avatar
Linus Torvalds committed
89
#include <linux/ctype.h>
Linus Torvalds's avatar
Linus Torvalds committed
90
#include <linux/slab.h>	/* kmalloc(), kfree() */
Linus Torvalds's avatar
Linus Torvalds committed
91 92 93
#include <linux/wanrouter.h>	/* WAN router definitions */
#include <linux/wanpipe.h>	/* WANPIPE common user API definitions */
#include <asm/byteorder.h>	/* htons(), etc. */
Linus Torvalds's avatar
Linus Torvalds committed
94 95 96
#include <asm/atomic.h>
#include <linux/delay.h>	/* Experimental delay */

97
#include <asm/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
98 99 100

#include <linux/if.h>
#include <linux/if_arp.h>
Linus Torvalds's avatar
Linus Torvalds committed
101
#include <linux/sdla_x25.h>	/* X.25 firmware API definitions */
Linus Torvalds's avatar
Linus Torvalds committed
102 103 104 105 106 107 108
#include <linux/if_wanpipe_common.h>
#include <linux/if_wanpipe.h>


/*======================================================
 * 	Defines & Macros 
 *=====================================================*/
Linus Torvalds's avatar
Linus Torvalds committed
109 110 111 112 113 114 115 116 117 118 119 120


#define	CMD_OK		0		/* normal firmware return code */
#define	CMD_TIMEOUT	0xFF		/* firmware command timed out */
#define	MAX_CMD_RETRY	10		/* max number of firmware retries */

#define	X25_CHAN_MTU	4096		/* unfragmented logical channel MTU */
#define	X25_HRDHDR_SZ	7		/* max encapsulation header size */
#define	X25_CONCT_TMOUT	(90*HZ)		/* link connection timeout */
#define	X25_RECON_TMOUT	(10*HZ)		/* link connection timeout */
#define	CONNECT_TIMEOUT	(90*HZ)		/* link connection timeout */
#define	HOLD_DOWN_TIME	(30*HZ)		/* link hold down time */
Linus Torvalds's avatar
Linus Torvalds committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
#define MAX_BH_BUFF	10
#define M_BIT		0x01	

//#define PRINT_DEBUG 1
#ifdef PRINT_DEBUG
#define DBG_PRINTK(format, a...) printk(format, ## a)
#else
#define DBG_PRINTK(format, a...)
#endif  

#define TMR_INT_ENABLED_POLL_ACTIVE      0x01
#define TMR_INT_ENABLED_POLL_CONNECT_ON  0x02
#define TMR_INT_ENABLED_POLL_CONNECT_OFF 0x04
#define TMR_INT_ENABLED_POLL_DISCONNECT  0x08
#define TMR_INT_ENABLED_CMD_EXEC	 0x10
#define TMR_INT_ENABLED_UPDATE		 0x20
#define TMR_INT_ENABLED_UDP_PKT		 0x40

#define MAX_X25_ADDR_SIZE	16
#define MAX_X25_DATA_SIZE 	129
#define MAX_X25_FACL_SIZE	110

#define TRY_CMD_AGAIN	2
#define DELAY_RESULT    1
#define RETURN_RESULT   0

#define DCD(x) (x & 0x03 ? "HIGH" : "LOW")
#define CTS(x) (x & 0x05 ? "HIGH" : "LOW")


/* Driver will not write log messages about 
 * modem status if defined.*/
#define MODEM_NOT_LOG 1

/*==================================================== 
 * 	For IPXWAN 
 *===================================================*/
Linus Torvalds's avatar
Linus Torvalds committed
158 159 160 161

#define CVHexToAscii(b) (((unsigned char)(b) > (unsigned char)9) ? ((unsigned char)'A' + ((unsigned char)(b) - (unsigned char)10)) : ((unsigned char)'0' + (unsigned char)(b)))


Linus Torvalds's avatar
Linus Torvalds committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
/*====================================================
 *           MEMORY DEBUGGING FUNCTION
 *====================================================

#define KMEM_SAFETYZONE 8

static void * dbg_kmalloc(unsigned int size, int prio, int line) {
	int i = 0;
	void * v = kmalloc(size+sizeof(unsigned int)+2*KMEM_SAFETYZONE*8,prio);
	char * c1 = v;	
	c1 += sizeof(unsigned int);
	*((unsigned int *)v) = size;

	for (i = 0; i < KMEM_SAFETYZONE; i++) {
		c1[0] = 'D'; c1[1] = 'E'; c1[2] = 'A'; c1[3] = 'D';
		c1[4] = 'B'; c1[5] = 'E'; c1[6] = 'E'; c1[7] = 'F';
		c1 += 8;
	}
	c1 += size;
	for (i = 0; i < KMEM_SAFETYZONE; i++) {
		c1[0] = 'M'; c1[1] = 'U'; c1[2] = 'N'; c1[3] = 'G';
		c1[4] = 'W'; c1[5] = 'A'; c1[6] = 'L'; c1[7] = 'L';
		c1 += 8;
	}
	v = ((char *)v) + sizeof(unsigned int) + KMEM_SAFETYZONE*8;
	printk(KERN_INFO "line %d  kmalloc(%d,%d) = %p\n",line,size,prio,v);
	return v;
}
static void dbg_kfree(void * v, int line) {
	unsigned int * sp = (unsigned int *)(((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8));
	unsigned int size = *sp;
	char * c1 = ((char *)v) - KMEM_SAFETYZONE*8;
	int i = 0;
	for (i = 0; i < KMEM_SAFETYZONE; i++) {
		if (   c1[0] != 'D' || c1[1] != 'E' || c1[2] != 'A' || c1[3] != 'D'
		    || c1[4] != 'B' || c1[5] != 'E' || c1[6] != 'E' || c1[7] != 'F') {
			printk(KERN_INFO "kmalloced block at %p has been corrupted (underrun)!\n",v);
			printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2x\n", i*8,
			                c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
		}
		c1 += 8;
	}
	c1 += size;
	for (i = 0; i < KMEM_SAFETYZONE; i++) {
		if (   c1[0] != 'M' || c1[1] != 'U' || c1[2] != 'N' || c1[3] != 'G'
		    || c1[4] != 'W' || c1[5] != 'A' || c1[6] != 'L' || c1[7] != 'L'
		   ) {
			printk(KERN_INFO "kmalloced block at %p has been corrupted (overrun):\n",v);
			printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2x\n", i*8,
			                c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
		}
		c1 += 8;
	}
	printk(KERN_INFO "line %d  kfree(%p)\n",line,v);
	v = ((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8);
	kfree(v);
}

#define kmalloc(x,y) dbg_kmalloc(x,y,__LINE__)
#define kfree(x) dbg_kfree(x,__LINE__)

==============================================================*/



/*===============================================
 * 	Data Structures 
 *===============================================*/


/*========================================================
 * Name: 	x25_channel
 *
 * Purpose:	To hold private informaton for each  
 *              logical channel.
 *		
 * Rationale:  	Per-channel debugging is possible if each 
 *              channel has its own private area.
 *	
 * Assumptions:
 *
243
 * Description:	This is an extention of the struct net_device
Linus Torvalds's avatar
Linus Torvalds committed
244 245 246 247
 *              we create for each network interface to keep 
 *              the rest of X.25 channel-specific data. 
 *
 * Construct:	Typedef
Linus Torvalds's avatar
Linus Torvalds committed
248 249 250
 */
typedef struct x25_channel
{
Linus Torvalds's avatar
Linus Torvalds committed
251
	wanpipe_common_t common;	/* common area for x25api and socket */
Linus Torvalds's avatar
Linus Torvalds committed
252 253 254 255 256 257 258 259 260 261 262 263 264
	char name[WAN_IFNAME_SZ+1];	/* interface name, ASCIIZ */
	char addr[WAN_ADDRESS_SZ+1];	/* media address, ASCIIZ */
	unsigned tx_pkt_size;
	unsigned short protocol;	/* ethertype, 0 - multiplexed */
	char drop_sequence;		/* mark sequence for dropping */
	unsigned long state_tick;	/* time of the last state change */
	unsigned idle_timeout;		/* sec, before disconnecting */
	unsigned long i_timeout_sofar;  /* # of sec's we've been idle */
	unsigned hold_timeout;		/* sec, before re-connecting */
	unsigned long tick_counter;	/* counter for transmit time out */
	char devtint;			/* Weather we should dev_tint() */
	struct sk_buff* rx_skb;		/* receive socket buffer */
	struct sk_buff* tx_skb;		/* transmit socket buffer */
Linus Torvalds's avatar
Linus Torvalds committed
265 266 267 268 269 270 271

	bh_data_t *bh_head;	  	  /* Circular buffer for x25api_bh */
	unsigned long  tq_working;
	volatile int  bh_write;
	volatile int  bh_read;
	atomic_t  bh_buff_used;

Linus Torvalds's avatar
Linus Torvalds committed
272
	sdla_t* card;			/* -> owner */
273
	struct net_device *dev;		/* -> bound devce */
Linus Torvalds's avatar
Linus Torvalds committed
274

Linus Torvalds's avatar
Linus Torvalds committed
275
	int ch_idx;
Linus Torvalds's avatar
Linus Torvalds committed
276 277
	unsigned char enable_IPX;
	unsigned long network_number;
Linus Torvalds's avatar
Linus Torvalds committed
278
	struct net_device_stats ifstats;	/* interface statistics */
Linus Torvalds's avatar
Linus Torvalds committed
279 280 281 282 283 284 285 286 287 288 289
	unsigned short transmit_length;
	unsigned short tx_offset;
	char transmit_buffer[X25_CHAN_MTU+sizeof(x25api_hdr_t)];

	if_send_stat_t   if_send_stat;
        rx_intr_stat_t   rx_intr_stat;
        pipe_mgmt_stat_t pipe_mgmt_stat;    

	unsigned long router_start_time; /* Router start time in seconds */
	unsigned long router_up_time;
	
Linus Torvalds's avatar
Linus Torvalds committed
290 291
} x25_channel_t;

Linus Torvalds's avatar
Linus Torvalds committed
292 293 294
/* FIXME Take this out */

#ifdef NEX_OLD_CALL_INFO
Linus Torvalds's avatar
Linus Torvalds committed
295 296
typedef struct x25_call_info
{
Linus Torvalds's avatar
Linus Torvalds committed
297 298 299 300 301
	char dest[17];			PACKED;/* ASCIIZ destination address */
	char src[17];			PACKED;/* ASCIIZ source address */
	char nuser;			PACKED;/* number of user data bytes */
	unsigned char user[127];	PACKED;/* user data */
	char nfacil;			PACKED;/* number of facilities */
Linus Torvalds's avatar
Linus Torvalds committed
302 303
	struct
	{
Linus Torvalds's avatar
Linus Torvalds committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317
		unsigned char code;     PACKED;
		unsigned char parm;     PACKED;
	} facil[64];			        /* facilities */
} x25_call_info_t;
#else
typedef struct x25_call_info
{
	char dest[MAX_X25_ADDR_SIZE]		PACKED;/* ASCIIZ destination address */
	char src[MAX_X25_ADDR_SIZE]		PACKED;/* ASCIIZ source address */
	unsigned char nuser			PACKED;
	unsigned char user[MAX_X25_DATA_SIZE]	PACKED;/* user data */
	unsigned char nfacil			PACKED;
	unsigned char facil[MAX_X25_FACL_SIZE]	PACKED;
	unsigned short lcn             		PACKED;
Linus Torvalds's avatar
Linus Torvalds committed
318
} x25_call_info_t;
Linus Torvalds's avatar
Linus Torvalds committed
319 320
#endif

Linus Torvalds's avatar
Linus Torvalds committed
321

Linus Torvalds's avatar
Linus Torvalds committed
322 323 324 325
  
/*===============================================
 *	Private Function Prototypes
 *==============================================*/
Linus Torvalds's avatar
Linus Torvalds committed
326

Linus Torvalds's avatar
Linus Torvalds committed
327 328 329 330 331

/*================================================= 
 * WAN link driver entry points. These are 
 * called by the WAN router module.
 */
332
static int update(struct wan_device* wandev);
333
static int new_if(struct wan_device* wandev, struct net_device* dev,
334
		  wanif_conf_t* conf);
335
static int del_if(struct wan_device* wandev, struct net_device* dev);
Linus Torvalds's avatar
Linus Torvalds committed
336 337
static void disable_comm (sdla_t* card);
static void disable_comm_shutdown(sdla_t *card);
Linus Torvalds's avatar
Linus Torvalds committed
338

Linus Torvalds's avatar
Linus Torvalds committed
339 340 341 342 343


/*================================================= 
 *	WANPIPE-specific entry points 
 */
Linus Torvalds's avatar
Linus Torvalds committed
344
static int wpx_exec (struct sdla* card, void* u_cmd, void* u_data);
345 346 347
static void x25api_bh(struct net_device *dev);
static int x25api_bh_cleanup(struct net_device *dev);
static int bh_enqueue(struct net_device *dev, struct sk_buff *skb);
Linus Torvalds's avatar
Linus Torvalds committed
348

Linus Torvalds's avatar
Linus Torvalds committed
349

Linus Torvalds's avatar
Linus Torvalds committed
350 351 352
/*=================================================  
 * 	Network device interface 
 */
353 354 355 356
static int if_init(struct net_device* dev);
static int if_open(struct net_device* dev);
static int if_close(struct net_device* dev);
static int if_header(struct sk_buff* skb, struct net_device* dev,
Linus Torvalds's avatar
Linus Torvalds committed
357 358
	unsigned short type, void* daddr, void* saddr, unsigned len);
static int if_rebuild_hdr (struct sk_buff* skb);
359 360
static int if_send(struct sk_buff* skb, struct net_device* dev);
static struct net_device_stats *if_stats(struct net_device* dev);
Linus Torvalds's avatar
Linus Torvalds committed
361

362
static void if_tx_timeout(struct net_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
363 364 365 366 367 368 369 370 371 372 373 374

/*=================================================  
 * 	Interrupt handlers 
 */
static void wpx_isr	(sdla_t *);
static void rx_intr	(sdla_t *);
static void tx_intr	(sdla_t *);
static void status_intr	(sdla_t *);
static void event_intr	(sdla_t *);
static void spur_intr	(sdla_t *);
static void timer_intr  (sdla_t *);

375 376 377
static int tx_intr_send(sdla_t *card, struct net_device *dev);
static struct net_device *move_dev_to_next(sdla_t *card,
					   struct net_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
378 379 380 381

/*=================================================  
 *	Background polling routines 
 */
Linus Torvalds's avatar
Linus Torvalds committed
382 383 384 385
static void wpx_poll (sdla_t* card);
static void poll_disconnected (sdla_t* card);
static void poll_connecting (sdla_t* card);
static void poll_active (sdla_t* card);
Linus Torvalds's avatar
Linus Torvalds committed
386 387 388
static void trigger_x25_poll(sdla_t *card);
static void x25_timer_routine(unsigned long data);

Linus Torvalds's avatar
Linus Torvalds committed
389

Linus Torvalds's avatar
Linus Torvalds committed
390 391 392 393

/*=================================================  
 *	X.25 firmware interface functions 
 */
Linus Torvalds's avatar
Linus Torvalds committed
394 395
static int x25_get_version (sdla_t* card, char* str);
static int x25_configure (sdla_t* card, TX25Config* conf);
Linus Torvalds's avatar
Linus Torvalds committed
396 397
static int hdlc_configure (sdla_t* card, TX25Config* conf);
static int set_hdlc_level (sdla_t* card);
Linus Torvalds's avatar
Linus Torvalds committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
static int x25_get_err_stats (sdla_t* card);
static int x25_get_stats (sdla_t* card);
static int x25_set_intr_mode (sdla_t* card, int mode);
static int x25_close_hdlc (sdla_t* card);
static int x25_open_hdlc (sdla_t* card);
static int x25_setup_hdlc (sdla_t* card);
static int x25_set_dtr (sdla_t* card, int dtr);
static int x25_get_chan_conf (sdla_t* card, x25_channel_t* chan);
static int x25_place_call (sdla_t* card, x25_channel_t* chan);
static int x25_accept_call (sdla_t* card, int lcn, int qdm);
static int x25_clear_call (sdla_t* card, int lcn, int cause, int diagn);
static int x25_send (sdla_t* card, int lcn, int qdm, int len, void* buf);
static int x25_fetch_events (sdla_t* card);
static int x25_error (sdla_t* card, int err, int cmd, int lcn);

Linus Torvalds's avatar
Linus Torvalds committed
413 414 415
/*=================================================  
 *	X.25 asynchronous event handlers 
 */
Linus Torvalds's avatar
Linus Torvalds committed
416 417 418 419 420 421
static int incoming_call (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
static int call_accepted (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
static int call_cleared (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
static int timeout_event (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
static int restart_event (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);

Linus Torvalds's avatar
Linus Torvalds committed
422 423 424 425

/*=================================================  
 *	Miscellaneous functions 
 */
Linus Torvalds's avatar
Linus Torvalds committed
426 427
static int connect (sdla_t* card);
static int disconnect (sdla_t* card);
428 429 430 431 432 433 434
static struct net_device* get_dev_by_lcn(struct wan_device* wandev,
					 unsigned lcn);
static int chan_connect(struct net_device* dev);
static int chan_disc(struct net_device* dev);
static void set_chan_state(struct net_device* dev, int state);
static int chan_send(struct net_device *dev, void* buff, unsigned data_len,
		     unsigned char tx_intr);
Linus Torvalds's avatar
Linus Torvalds committed
435 436
static unsigned char bps_to_speed_code (unsigned long bps);
static unsigned int dec_to_uint (unsigned char* str, int len);
Linus Torvalds's avatar
Linus Torvalds committed
437 438
static unsigned int hex_to_uint (unsigned char*, int);
static void parse_call_info (unsigned char*, x25_call_info_t*);
439 440 441 442
static struct net_device *find_channel(sdla_t *card, unsigned lcn);
static void bind_lcn_to_dev(sdla_t *card, struct net_device *dev, unsigned lcn);
static void setup_for_delayed_transmit(struct net_device *dev,
				       void *buf, unsigned len);
Linus Torvalds's avatar
Linus Torvalds committed
443 444 445 446 447


/*=================================================  
 *      X25 API Functions 
 */
448 449
static int wanpipe_pull_data_in_skb(sdla_t *card, struct net_device *dev,
				    struct sk_buff **);
Linus Torvalds's avatar
Linus Torvalds committed
450
static void timer_intr_exec(sdla_t *, unsigned char);
451 452
static int execute_delayed_cmd(sdla_t *card, struct net_device *dev,
			       mbox_cmd_t *usr_cmd, char bad_cmd);
Linus Torvalds's avatar
Linus Torvalds committed
453 454
static int api_incoming_call (sdla_t*, TX25Mbox *, int);
static int alloc_and_init_skb_buf (sdla_t *,struct sk_buff **, int);
455 456
static void send_delayed_cmd_result(sdla_t *card, struct net_device *dev,
				    TX25Mbox* mbox);
Linus Torvalds's avatar
Linus Torvalds committed
457
static int clear_confirm_event (sdla_t *, TX25Mbox*);
458
static void send_oob_msg (sdla_t *card, struct net_device *dev, TX25Mbox *mbox);
Linus Torvalds's avatar
Linus Torvalds committed
459 460
static int timer_intr_cmd_exec(sdla_t *card);
static void api_oob_event (sdla_t *card,TX25Mbox *mbox);
461 462
static int check_bad_command(sdla_t *card, struct net_device *dev);
static int channel_disconnect(sdla_t* card, struct net_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
463 464 465 466 467 468 469 470 471 472
static void hdlc_link_down (sdla_t*);

/*=================================================
 *     XPIPEMON Functions
 */
static int process_udp_mgmt_pkt(sdla_t *);
static int udp_pkt_type( struct sk_buff *, sdla_t*);
static int reply_udp( unsigned char *, unsigned int); 
static void init_x25_channel_struct( x25_channel_t *);
static void init_global_statistics( sdla_t *);
473 474 475
static int store_udp_mgmt_pkt(int udp_type, char udp_pkt_src, sdla_t *card,
			      struct net_device *dev,
			      struct sk_buff *skb, int lcn);
Linus Torvalds's avatar
Linus Torvalds committed
476 477
static unsigned short calc_checksum (char *, int);

Linus Torvalds's avatar
Linus Torvalds committed
478

Linus Torvalds's avatar
Linus Torvalds committed
479 480 481 482 483 484 485

/*================================================= 
 *	IPX functions 
 */
static void switch_net_numbers(unsigned char *, unsigned long, unsigned char);
static int handle_IPXWAN(unsigned char *, char *, unsigned char , 
			 unsigned long , unsigned short );
Linus Torvalds's avatar
Linus Torvalds committed
486 487 488 489

extern void disable_irq(unsigned int);
extern void enable_irq(unsigned int);

Linus Torvalds's avatar
Linus Torvalds committed
490 491
static void S508_S514_lock(sdla_t *, unsigned long *);
static void S508_S514_unlock(sdla_t *, unsigned long *);
Linus Torvalds's avatar
Linus Torvalds committed
492 493


Linus Torvalds's avatar
Linus Torvalds committed
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
/*=================================================  
 * 	Global Variables 
 *=================================================*/



/*================================================= 
 *	Public Functions 
 *=================================================*/




/*===================================================================
 * wpx_init:	X.25 Protocol Initialization routine.
 *
 * Purpose:	To initialize the protocol/firmware.
 * 
 * Rationale:	This function is called by setup() function, in
 *              sdlamain.c, to dynamically setup the x25 protocol.
 *		This is the first protocol specific function, which
 *              executes once on startup.
 *                
 * Description:	This procedure initializes the x25 firmware and
 *    		sets up the mailbox, transmit and receive buffer
 *              pointers. It also initializes all debugging structures
 *              and sets up the X25 environment.
 *
 *		Sets up hardware options defined by user in [wanpipe#] 
 *		section of wanpipe#.conf configuration file. 
 *
 * 		At this point adapter is completely initialized 
 *      	and X.25 firmware is running.
 *  		o read firmware version (to make sure it's alive)
 *  		o configure adapter
 *  		o initialize protocol-specific fields of the 
 *                adapter data space.
 *
 * Called by:	setup() function in sdlamain.c
 *
 * Assumptions:	None
Linus Torvalds's avatar
Linus Torvalds committed
535
 *
Linus Torvalds's avatar
Linus Torvalds committed
536
 * Warnings:	None
Linus Torvalds's avatar
Linus Torvalds committed
537
 *
Linus Torvalds's avatar
Linus Torvalds committed
538 539
 * Return: 	0	o.k.
 *	 	< 0	failure.
Linus Torvalds's avatar
Linus Torvalds committed
540
 */
Linus Torvalds's avatar
Linus Torvalds committed
541

Linus Torvalds's avatar
Linus Torvalds committed
542 543
int wpx_init (sdla_t* card, wandev_conf_t* conf)
{
Linus Torvalds's avatar
Linus Torvalds committed
544
	union{
Linus Torvalds's avatar
Linus Torvalds committed
545 546 547 548 549
		char str[80];
		TX25Config cfg;
	} u;

	/* Verify configuration ID */
Linus Torvalds's avatar
Linus Torvalds committed
550
	if (conf->config_id != WANCONFIG_X25){
Linus Torvalds's avatar
Linus Torvalds committed
551 552 553 554 555 556 557 558 559 560 561
		printk(KERN_INFO "%s: invalid configuration ID %u!\n",
			card->devname, conf->config_id)
		;
		return -EINVAL;
	}

	/* Initialize protocol-specific fields */
	card->mbox  = (void*)(card->hw.dpmbase + X25_MBOX_OFFS);
	card->rxmb  = (void*)(card->hw.dpmbase + X25_RXMBOX_OFFS);
	card->flags = (void*)(card->hw.dpmbase + X25_STATUS_OFFS);

Linus Torvalds's avatar
Linus Torvalds committed
562 563 564 565 566 567 568 569
	/* Initialize for S514 Card */
	if(card->hw.type == SDLA_S514) {
		card->mbox += X25_MB_VECTOR;
		card->flags += X25_MB_VECTOR;
		card->rxmb += X25_MB_VECTOR;
	}


Linus Torvalds's avatar
Linus Torvalds committed
570 571 572 573 574 575
	/* Read firmware version.  Note that when adapter initializes, it
	 * clears the mailbox, so it may appear that the first command was
	 * executed successfully when in fact it was merely erased. To work
	 * around this, we execute the first command twice.
	 */
	if (x25_get_version(card, NULL) || x25_get_version(card, u.str))
Linus Torvalds's avatar
Linus Torvalds committed
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
		return -EIO;


	/* X25 firmware can run ether in X25 or LAPB HDLC mode.
         * Check the user defined option and configure accordingly */
	if (conf->u.x25.LAPB_hdlc_only == WANOPT_YES){
		if (set_hdlc_level(card) != CMD_OK){
			return -EIO;	
		}else{
			printk(KERN_INFO "%s: running LAP_B HDLC firmware v%s\n",
				card->devname, u.str);
		}
		card->u.x.LAPB_hdlc = 1;
	}else{
		printk(KERN_INFO "%s: running X.25 firmware v%s\n",
				card->devname, u.str);
		card->u.x.LAPB_hdlc = 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
594 595 596 597

	/* Configure adapter. Here we set resonable defaults, then parse
	 * device configuration structure and set configuration options.
	 * Most configuration options are verified and corrected (if
Linus Torvalds's avatar
Linus Torvalds committed
598
	 * necessary) since we can't rely on the adapter to do so.
Linus Torvalds's avatar
Linus Torvalds committed
599 600 601 602 603 604 605 606
	 */
	memset(&u.cfg, 0, sizeof(u.cfg));
	u.cfg.t1		= 3;
	u.cfg.n2		= 10;
	u.cfg.autoHdlc		= 1;		/* automatic HDLC connection */
	u.cfg.hdlcWindow	= 7;
	u.cfg.pktWindow		= 2;
	u.cfg.station		= 1;		/* DTE */
Linus Torvalds's avatar
Linus Torvalds committed
607
	u.cfg.options		= 0x0090;	/* disable D-bit pragmatics */
Linus Torvalds's avatar
Linus Torvalds committed
608 609 610 611 612 613 614 615 616 617 618 619
	u.cfg.ccittCompat	= 1988;
	u.cfg.t10t20		= 30;
	u.cfg.t11t21		= 30;
	u.cfg.t12t22		= 30;
	u.cfg.t13t23		= 30;
	u.cfg.t16t26		= 30;
	u.cfg.t28		= 30;
	u.cfg.r10r20		= 5;
	u.cfg.r12r22		= 5;
	u.cfg.r13r23		= 5;
	u.cfg.responseOpt	= 1;		/* RR's after every packet */

Linus Torvalds's avatar
Linus Torvalds committed
620 621 622 623 624 625 626 627
	if (card->u.x.LAPB_hdlc){
		u.cfg.hdlcMTU = 1027;
	}

	if (conf->u.x25.x25_conf_opt){
		u.cfg.options = conf->u.x25.x25_conf_opt;
	}

Linus Torvalds's avatar
Linus Torvalds committed
628
	if (conf->clocking != WANOPT_EXTERNAL)
Linus Torvalds's avatar
Linus Torvalds committed
629 630 631
		u.cfg.baudRate = bps_to_speed_code(conf->bps);

	if (conf->station != WANOPT_DTE){
Linus Torvalds's avatar
Linus Torvalds committed
632 633
		u.cfg.station = 0;		/* DCE mode */
	}
Linus Torvalds's avatar
Linus Torvalds committed
634 635

        if (conf->interface != WANOPT_RS232 ){
Linus Torvalds's avatar
Linus Torvalds committed
636 637
	        u.cfg.hdlcOptions |= 0x80;      /* V35 mode */
	} 
Linus Torvalds's avatar
Linus Torvalds committed
638

Linus Torvalds's avatar
Linus Torvalds committed
639 640
	/* adjust MTU */
	if (!conf->mtu || (conf->mtu >= 1024))
Linus Torvalds's avatar
Linus Torvalds committed
641
		card->wandev.mtu = 1024;
Linus Torvalds's avatar
Linus Torvalds committed
642
	else if (conf->mtu >= 512)
Linus Torvalds's avatar
Linus Torvalds committed
643
		card->wandev.mtu = 512;
Linus Torvalds's avatar
Linus Torvalds committed
644
	else if (conf->mtu >= 256)
Linus Torvalds's avatar
Linus Torvalds committed
645
		card->wandev.mtu = 256;
Linus Torvalds's avatar
Linus Torvalds committed
646
	else if (conf->mtu >= 128)
Linus Torvalds's avatar
Linus Torvalds committed
647 648 649 650
		card->wandev.mtu = 128;
	else 
		card->wandev.mtu = 64;

Linus Torvalds's avatar
Linus Torvalds committed
651 652
	u.cfg.defPktSize = u.cfg.pktMTU = card->wandev.mtu;

Linus Torvalds's avatar
Linus Torvalds committed
653
	if (conf->u.x25.hi_pvc){
Linus Torvalds's avatar
Linus Torvalds committed
654 655
		card->u.x.hi_pvc = min_t(unsigned int, conf->u.x25.hi_pvc, MAX_LCN_NUM);
		card->u.x.lo_pvc = min_t(unsigned int, conf->u.x25.lo_pvc, card->u.x.hi_pvc);
Linus Torvalds's avatar
Linus Torvalds committed
656
	}
Linus Torvalds's avatar
Linus Torvalds committed
657 658

	if (conf->u.x25.hi_svc){
Linus Torvalds's avatar
Linus Torvalds committed
659 660
		card->u.x.hi_svc = min_t(unsigned int, conf->u.x25.hi_svc, MAX_LCN_NUM);
		card->u.x.lo_svc = min_t(unsigned int, conf->u.x25.lo_svc, card->u.x.hi_svc);
Linus Torvalds's avatar
Linus Torvalds committed
661
	}
Linus Torvalds's avatar
Linus Torvalds committed
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680

	/* Figure out the total number of channels to configure */
	card->u.x.num_of_ch = 0;
	if (card->u.x.hi_svc != 0){
		card->u.x.num_of_ch = (card->u.x.hi_svc - card->u.x.lo_svc) + 1;
	}
	if (card->u.x.hi_pvc != 0){
		card->u.x.num_of_ch += (card->u.x.hi_pvc - card->u.x.lo_pvc) + 1;
	}

	if (card->u.x.num_of_ch == 0){
		printk(KERN_INFO "%s: ERROR, Minimum number of PVC/SVC channels is 1 !\n"
				 "%s: Please set the Lowest/Highest PVC/SVC values !\n",
				 card->devname,card->devname);
		return -ECHRNG;
	}
	
	u.cfg.loPVC = card->u.x.lo_pvc;
	u.cfg.hiPVC = card->u.x.hi_pvc;
Linus Torvalds's avatar
Linus Torvalds committed
681 682 683 684
	u.cfg.loTwoWaySVC = card->u.x.lo_svc;
	u.cfg.hiTwoWaySVC = card->u.x.hi_svc;

	if (conf->u.x25.hdlc_window)
Linus Torvalds's avatar
Linus Torvalds committed
685
		u.cfg.hdlcWindow = min_t(unsigned int, conf->u.x25.hdlc_window, 7);
Linus Torvalds's avatar
Linus Torvalds committed
686
	if (conf->u.x25.pkt_window)
Linus Torvalds's avatar
Linus Torvalds committed
687
		u.cfg.pktWindow = min_t(unsigned int, conf->u.x25.pkt_window, 7);
Linus Torvalds's avatar
Linus Torvalds committed
688

Linus Torvalds's avatar
Linus Torvalds committed
689
	if (conf->u.x25.t1)
Linus Torvalds's avatar
Linus Torvalds committed
690
		u.cfg.t1 = min_t(unsigned int, conf->u.x25.t1, 30);
Linus Torvalds's avatar
Linus Torvalds committed
691
	if (conf->u.x25.t2)
Linus Torvalds's avatar
Linus Torvalds committed
692
		u.cfg.t2 = min_t(unsigned int, conf->u.x25.t2, 29);
Linus Torvalds's avatar
Linus Torvalds committed
693
	if (conf->u.x25.t4)
Linus Torvalds's avatar
Linus Torvalds committed
694
		u.cfg.t4 = min_t(unsigned int, conf->u.x25.t4, 240);
Linus Torvalds's avatar
Linus Torvalds committed
695
	if (conf->u.x25.n2)
Linus Torvalds's avatar
Linus Torvalds committed
696
		u.cfg.n2 = min_t(unsigned int, conf->u.x25.n2, 30);
Linus Torvalds's avatar
Linus Torvalds committed
697 698

	if (conf->u.x25.t10_t20)
Linus Torvalds's avatar
Linus Torvalds committed
699
		u.cfg.t10t20 = min_t(unsigned int, conf->u.x25.t10_t20,255);
Linus Torvalds's avatar
Linus Torvalds committed
700
	if (conf->u.x25.t11_t21)
Linus Torvalds's avatar
Linus Torvalds committed
701
		u.cfg.t11t21 = min_t(unsigned int, conf->u.x25.t11_t21,255);
Linus Torvalds's avatar
Linus Torvalds committed
702
	if (conf->u.x25.t12_t22)
Linus Torvalds's avatar
Linus Torvalds committed
703
		u.cfg.t12t22 = min_t(unsigned int, conf->u.x25.t12_t22,255);
Linus Torvalds's avatar
Linus Torvalds committed
704
	if (conf->u.x25.t13_t23)	
Linus Torvalds's avatar
Linus Torvalds committed
705
		u.cfg.t13t23 = min_t(unsigned int, conf->u.x25.t13_t23,255);
Linus Torvalds's avatar
Linus Torvalds committed
706
	if (conf->u.x25.t16_t26)
Linus Torvalds's avatar
Linus Torvalds committed
707
		u.cfg.t16t26 = min_t(unsigned int, conf->u.x25.t16_t26, 255);
Linus Torvalds's avatar
Linus Torvalds committed
708
	if (conf->u.x25.t28)
Linus Torvalds's avatar
Linus Torvalds committed
709
		u.cfg.t28 = min_t(unsigned int, conf->u.x25.t28, 255);
Linus Torvalds's avatar
Linus Torvalds committed
710 711

	if (conf->u.x25.r10_r20)
Linus Torvalds's avatar
Linus Torvalds committed
712
		u.cfg.r10r20 = min_t(unsigned int, conf->u.x25.r10_r20,250);
Linus Torvalds's avatar
Linus Torvalds committed
713
	if (conf->u.x25.r12_r22)
Linus Torvalds's avatar
Linus Torvalds committed
714
		u.cfg.r12r22 = min_t(unsigned int, conf->u.x25.r12_r22,250);
Linus Torvalds's avatar
Linus Torvalds committed
715
	if (conf->u.x25.r13_r23)
Linus Torvalds's avatar
Linus Torvalds committed
716
		u.cfg.r13r23 = min_t(unsigned int, conf->u.x25.r13_r23,250);
Linus Torvalds's avatar
Linus Torvalds committed
717 718


Linus Torvalds's avatar
Linus Torvalds committed
719
	if (conf->u.x25.ccitt_compat)
Linus Torvalds's avatar
Linus Torvalds committed
720
		u.cfg.ccittCompat = conf->u.x25.ccitt_compat;
Linus Torvalds's avatar
Linus Torvalds committed
721 722

	/* initialize adapter */
Linus Torvalds's avatar
Linus Torvalds committed
723 724 725 726 727 728 729 730 731
	if (card->u.x.LAPB_hdlc){
		if (hdlc_configure(card, &u.cfg) != CMD_OK)
			return -EIO;
	}else{
		if (x25_configure(card, &u.cfg) != CMD_OK)
			return -EIO;
	}

	if ((x25_close_hdlc(card) != CMD_OK) ||		/* close HDLC link */
Linus Torvalds's avatar
Linus Torvalds committed
732
	    (x25_set_dtr(card, 0) != CMD_OK))		/* drop DTR */
Linus Torvalds's avatar
Linus Torvalds committed
733
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
734 735 736 737 738 739 740

	/* Initialize protocol-specific fields of adapter data space */
	card->wandev.bps	= conf->bps;
	card->wandev.interface	= conf->interface;
	card->wandev.clocking	= conf->clocking;
	card->wandev.station	= conf->station;
	card->isr		= &wpx_isr;
Linus Torvalds's avatar
Linus Torvalds committed
741 742
	card->poll		= NULL; //&wpx_poll;
	card->disable_comm	= &disable_comm;
Linus Torvalds's avatar
Linus Torvalds committed
743 744 745 746
	card->exec		= &wpx_exec;
	card->wandev.update	= &update;
	card->wandev.new_if	= &new_if;
	card->wandev.del_if	= &del_if;
Linus Torvalds's avatar
Linus Torvalds committed
747 748 749

	/* WARNING: This function cannot exit with an error
	 *          after the change of state */
Linus Torvalds's avatar
Linus Torvalds committed
750
	card->wandev.state	= WAN_DISCONNECTED;
Linus Torvalds's avatar
Linus Torvalds committed
751
	
Linus Torvalds's avatar
Linus Torvalds committed
752 753 754
	card->wandev.enable_tx_int = 0;
	card->irq_dis_if_send_count = 0;
        card->irq_dis_poll_count = 0;
Linus Torvalds's avatar
Linus Torvalds committed
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
	card->u.x.tx_dev = NULL;
	card->u.x.no_dev = 0;


	/* Configure for S514 PCI Card */
	if (card->hw.type == SDLA_S514) {
		card->u.x.hdlc_buf_status = 
			(volatile unsigned char *)
				(card->hw.dpmbase + X25_MB_VECTOR+ X25_MISC_HDLC_BITS);
	}else{
		card->u.x.hdlc_buf_status = 
			(volatile unsigned char *)(card->hw.dpmbase + X25_MISC_HDLC_BITS); 
	}

	card->u.x.poll_device=NULL;
	card->wandev.udp_port = conf->udp_port;

	/* Enable or disable call setup logging */
	if (conf->u.x25.logging == WANOPT_YES){
		printk(KERN_INFO "%s: Enabling Call Logging.\n",
			card->devname);
		card->u.x.logging = 1;
	}else{	
		card->u.x.logging = 0;
	}

	/* Enable or disable modem status reporting */
	if (conf->u.x25.oob_on_modem == WANOPT_YES){
		printk(KERN_INFO "%s: Enabling OOB on Modem change.\n",
			card->devname);
		card->u.x.oob_on_modem = 1;
	}else{
		card->u.x.oob_on_modem = 0;
	}
	
	init_global_statistics(card);	

	card->u.x.x25_poll_task.sync=0;
	card->u.x.x25_poll_task.routine = (void*)(void*)wpx_poll;
	card->u.x.x25_poll_task.data = card;

	init_timer(&card->u.x.x25_timer);
	card->u.x.x25_timer.data = (unsigned long)card;
	card->u.x.x25_timer.function = x25_timer_routine;
Linus Torvalds's avatar
Linus Torvalds committed
799 800 801 802
	
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
803 804 805
/*=========================================================
 *	WAN Device Driver Entry Points 
 *========================================================*/
Linus Torvalds's avatar
Linus Torvalds committed
806

Linus Torvalds's avatar
Linus Torvalds committed
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
/*============================================================
 * Name:	update(),  Update device status & statistics.
 *
 * Purpose:	To provide debugging and statitical
 *              information to the /proc file system.
 *              /proc/net/wanrouter/wanpipe#
 *              	
 * Rationale:	The /proc file system is used to collect
 *              information about the kernel and drivers.
 *              Using the /proc file system the user
 *              can see exactly what the sangoma drivers are
 *              doing. And in what state they are in. 
 *                
 * Description: Collect all driver statistical information
 *              and pass it to the top laywer. 
 *		
 *		Since we have to execute a debugging command, 
 *              to obtain firmware statitics, we trigger a 
 *              UPDATE function within the timer interrtup.
 *              We wait until the timer update is complete.
 *              Once complete return the appropriate return
 *              code to indicate that the update was successful.
 *              
 * Called by:	device_stat() in wanmain.c
 *
 * Assumptions:	
 *
 * Warnings:	This function will degrade the performance
 *              of the router, since it uses the mailbox. 
 *
 * Return: 	0 	OK
 * 		<0	Failed (or busy).
Linus Torvalds's avatar
Linus Torvalds committed
839
 */
Linus Torvalds's avatar
Linus Torvalds committed
840

841
static int update(struct wan_device* wandev)
Linus Torvalds's avatar
Linus Torvalds committed
842
{
Linus Torvalds's avatar
Linus Torvalds committed
843 844 845
	volatile sdla_t* card;
	TX25Status* status;
	unsigned long timeout;
Linus Torvalds's avatar
Linus Torvalds committed
846 847 848 849

	/* sanity checks */
	if ((wandev == NULL) || (wandev->private == NULL))
		return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
850

Linus Torvalds's avatar
Linus Torvalds committed
851 852
	if (wandev->state == WAN_UNCONFIGURED)
		return -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
853 854

	if (test_bit(SEND_CRIT, (void*)&wandev->critical))
Linus Torvalds's avatar
Linus Torvalds committed
855
		return -EAGAIN;
Linus Torvalds's avatar
Linus Torvalds committed
856 857 858 859

	if (!wandev->dev)
		return -ENODEV;
	
Linus Torvalds's avatar
Linus Torvalds committed
860
	card = wandev->private;
Linus Torvalds's avatar
Linus Torvalds committed
861
	status = card->flags;
Linus Torvalds's avatar
Linus Torvalds committed
862

Linus Torvalds's avatar
Linus Torvalds committed
863 864 865 866 867 868 869 870 871 872 873 874 875
	card->u.x.timer_int_enabled |= TMR_INT_ENABLED_UPDATE;
	status->imask |= INTR_ON_TIMER;
	timeout = jiffies;	

	for (;;){
		if (!(card->u.x.timer_int_enabled & TMR_INT_ENABLED_UPDATE)){	
			break;
		}
		if ((jiffies-timeout) > 1*HZ){
			card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_UPDATE;
			return -EAGAIN;
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
876 877 878
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904

/*===================================================================
 * Name:	new_if
 *
 * Purpose:	To allocate and initialize resources for a 
 *              new logical channel.  
 * 
 * Rationale:	A new channel can be added dynamically via
 *              ioctl call.
 *                
 * Description:	Allocate a private channel structure, x25_channel_t.
 *		Parse the user interface options from wanpipe#.conf 
 *		configuration file. 
 *		Bind the private are into the network device private
 *              area pointer (dev->priv).
 *		Prepare the network device structure for registration.
 *
 * Called by:	ROUTER_IFNEW Ioctl call, from wanrouter_ioctl() 
 *              (wanmain.c)
 *
 * Assumptions: None
 *
 * Warnings:	None
 *
 * Return: 	0 	Ok
 *		<0 	Failed (channel will not be created)
Linus Torvalds's avatar
Linus Torvalds committed
905
 */
906
static int new_if(struct wan_device* wandev, struct net_device* dev,
907
		  wanif_conf_t* conf)
Linus Torvalds's avatar
Linus Torvalds committed
908 909 910 911 912
{
	sdla_t* card = wandev->private;
	x25_channel_t* chan;
	int err = 0;

Linus Torvalds's avatar
Linus Torvalds committed
913
	if ((conf->name[0] == '\0') || (strlen(conf->name) > WAN_IFNAME_SZ)){
Linus Torvalds's avatar
Linus Torvalds committed
914
		printk(KERN_INFO "%s: invalid interface name!\n",
Linus Torvalds's avatar
Linus Torvalds committed
915
			card->devname);
Linus Torvalds's avatar
Linus Torvalds committed
916 917 918
		return -EINVAL;
	}

Linus Torvalds's avatar
Linus Torvalds committed
919 920 921 922 923 924 925 926 927
	if(card->wandev.new_if_cnt++ > 0 && card->u.x.LAPB_hdlc) {
		printk(KERN_INFO "%s: Error: Running LAPB HDLC Mode !\n",
						card->devname);
		printk(KERN_INFO 
			"%s: Maximum number of network interfaces must be one !\n",
						card->devname);
		return -EEXIST;
	}

Linus Torvalds's avatar
Linus Torvalds committed
928
	/* allocate and initialize private data */
Linus Torvalds's avatar
Linus Torvalds committed
929 930 931 932 933
	chan = kmalloc(sizeof(x25_channel_t), GFP_ATOMIC);
	if (chan == NULL){
		return -ENOMEM;
	}
	
Linus Torvalds's avatar
Linus Torvalds committed
934
	memset(chan, 0, sizeof(x25_channel_t));
Linus Torvalds's avatar
Linus Torvalds committed
935 936 937 938 939 940

	/* Bug Fix: Seg Err on PVC startup
	 * It must be here since bind_lcn_to_dev expects 
	 * it bellow */
	dev->priv = chan;
	
Linus Torvalds's avatar
Linus Torvalds committed
941 942
	strcpy(chan->name, conf->name);
	chan->card = card;
Linus Torvalds's avatar
Linus Torvalds committed
943 944 945 946
	chan->dev = dev;
	chan->common.sk = NULL;
	chan->common.func = NULL;
	chan->common.rw_bind = 0;
Linus Torvalds's avatar
Linus Torvalds committed
947 948 949
	chan->tx_skb = chan->rx_skb = NULL;

	/* verify media address */
Linus Torvalds's avatar
Linus Torvalds committed
950 951
	if (conf->addr[0] == '@'){		/* SVC */
		chan->common.svc = 1;
Linus Torvalds's avatar
Linus Torvalds committed
952 953 954
		strncpy(chan->addr, &conf->addr[1], WAN_ADDRESS_SZ);

		/* Set channel timeouts (default if not specified) */
Linus Torvalds's avatar
Linus Torvalds committed
955 956 957 958 959 960
		chan->idle_timeout = (conf->idle_timeout) ? 
					conf->idle_timeout : 90;
		chan->hold_timeout = (conf->hold_timeout) ? 
					conf->hold_timeout : 10;

	}else if (is_digit(conf->addr[0])){	/* PVC */
Linus Torvalds's avatar
Linus Torvalds committed
961 962
		int lcn = dec_to_uint(conf->addr, 0);

Linus Torvalds's avatar
Linus Torvalds committed
963 964 965
		if ((lcn >= card->u.x.lo_pvc) && (lcn <= card->u.x.hi_pvc)){
			bind_lcn_to_dev (card, dev, lcn);
		}else{
Linus Torvalds's avatar
Linus Torvalds committed
966 967
			printk(KERN_ERR
				"%s: PVC %u is out of range on interface %s!\n",
Linus Torvalds's avatar
Linus Torvalds committed
968
				wandev->name, lcn, chan->name);
Linus Torvalds's avatar
Linus Torvalds committed
969 970
			err = -EINVAL;
		}
Linus Torvalds's avatar
Linus Torvalds committed
971
	}else{
Linus Torvalds's avatar
Linus Torvalds committed
972 973
		printk(KERN_ERR
			"%s: invalid media address on interface %s!\n",
Linus Torvalds's avatar
Linus Torvalds committed
974
			wandev->name, chan->name);
Linus Torvalds's avatar
Linus Torvalds committed
975 976
		err = -EINVAL;
	}
Linus Torvalds's avatar
Linus Torvalds committed
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992

	if(strcmp(conf->usedby, "WANPIPE") == 0){
                printk(KERN_INFO "%s: Running in WANPIPE mode %s\n",
			wandev->name, chan->name);
                chan->common.usedby = WANPIPE;
		chan->protocol = htons(ETH_P_IP);

        }else if(strcmp(conf->usedby, "API") == 0){
		chan->common.usedby = API;
                printk(KERN_INFO "%s: Running in API mode %s\n",
			wandev->name, chan->name);
		chan->protocol = htons(X25_PROT);
	}


	if (err){
Linus Torvalds's avatar
Linus Torvalds committed
993
		kfree(chan);
Linus Torvalds's avatar
Linus Torvalds committed
994
		dev->priv = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
995 996
		return err;
	}
Linus Torvalds's avatar
Linus Torvalds committed
997 998 999 1000 1001 1002 1003 1004 1005 1006
	
	chan->enable_IPX = conf->enable_IPX;
	
	if (chan->enable_IPX)
		chan->protocol = htons(ETH_P_IPX);
	
	if (conf->network_number)
		chan->network_number = conf->network_number;
	else
		chan->network_number = 0xDEADBEEF;
Linus Torvalds's avatar
Linus Torvalds committed
1007 1008

	/* prepare network device data space for registration */
Linus Torvalds's avatar
Linus Torvalds committed
1009
	strcpy(dev->name,chan->name);
1010

Linus Torvalds's avatar
Linus Torvalds committed
1011
	dev->init = &if_init;
Linus Torvalds's avatar
Linus Torvalds committed
1012 1013 1014

	init_x25_channel_struct(chan);

Linus Torvalds's avatar
Linus Torvalds committed
1015 1016 1017
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
/*===================================================================
 * Name:	del_if(),  Remove a logical channel.	 
 *
 * Purpose:	To dynamically remove a logical channel.
 * 
 * Rationale:	Each logical channel should be dynamically
 *              removable. This functin is called by an 
 *              IOCTL_IFDEL ioctl call or shutdown(). 
 *                
 * Description: Do nothing.
 *
 * Called by:	IOCTL_IFDEL : wanrouter_ioctl() from wanmain.c
 *              shutdown() from sdlamain.c
 *
 * Assumptions: 
 *
 * Warnings:
 *
 * Return: 	0 Ok. Void function.
Linus Torvalds's avatar
Linus Torvalds committed
1037
 */
Linus Torvalds's avatar
Linus Torvalds committed
1038 1039 1040

//FIXME Del IF Should be taken out now.

1041
static int del_if(struct wan_device* wandev, struct net_device* dev)
Linus Torvalds's avatar
Linus Torvalds committed
1042 1043 1044 1045 1046
{
	return 0;
}


Linus Torvalds's avatar
Linus Torvalds committed
1047 1048 1049 1050 1051 1052
/*============================================================
 * Name:	wpx_exec
 *
 * Description:	Execute adapter interface command.
 * 		This option is currently dissabled.
 *===========================================================*/
Linus Torvalds's avatar
Linus Torvalds committed
1053 1054 1055

static int wpx_exec (struct sdla* card, void* u_cmd, void* u_data)
{
Linus Torvalds's avatar
Linus Torvalds committed
1056 1057
        return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
1058

Linus Torvalds's avatar
Linus Torvalds committed
1059 1060 1061 1062 1063 1064 1065 1066 1067
/*============================================================
 * Name:	disable_comm	
 *
 * Description:	Disable communications during shutdown.
 *              Dont check return code because there is 
 *              nothing we can do about it.  
 *
 * Warning:	Dev and private areas are gone at this point.
 *===========================================================*/
Linus Torvalds's avatar
Linus Torvalds committed
1068

Linus Torvalds's avatar
Linus Torvalds committed
1069 1070 1071 1072 1073
static void disable_comm(sdla_t* card)
{
	disable_comm_shutdown(card);
	del_timer(&card->u.x.x25_timer);
	return;
Linus Torvalds's avatar
Linus Torvalds committed
1074 1075 1076
}


Linus Torvalds's avatar
Linus Torvalds committed
1077 1078 1079 1080 1081 1082
/*============================================================
 *	Network Device Interface 
 *===========================================================*/

/*===================================================================
 * Name:	if_init(),   Netowrk Interface Initialization 	 
Linus Torvalds's avatar
Linus Torvalds committed
1083
 *
Linus Torvalds's avatar
Linus Torvalds committed
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
 * Purpose:	To initialize a network interface device structure.
 * 
 * Rationale:	During network interface startup, the if_init
 *              is called by the kernel to initialize the
 *              netowrk device structure.  Thus a driver
 *              can customze a network device. 
 *                
 * Description:	Initialize the netowrk device call back
 *              routines.  This is where we tell the kernel
 *              which function to use when it wants to send
 *              via our interface. 
 *		Furthermore, we initialize the device flags, 
 *              MTU and physical address of the board.
 *
 * Called by:	Kernel (/usr/src/linux/net/core/dev.c)
 * 		(dev->init())
 *
 * Assumptions: None
 *	
 * Warnings:	None
 *
 * Return: 	0 	Ok : Void function.
Linus Torvalds's avatar
Linus Torvalds committed
1106
 */
1107
static int if_init(struct net_device* dev)
Linus Torvalds's avatar
Linus Torvalds committed
1108 1109 1110
{
	x25_channel_t* chan = dev->priv;
	sdla_t* card = chan->card;
1111
	struct wan_device* wandev = &card->wandev;
Linus Torvalds's avatar
Linus Torvalds committed
1112 1113 1114 1115 1116 1117 1118 1119

	/* Initialize device driver entry points */
	dev->open		= &if_open;
	dev->stop		= &if_close;
	dev->hard_header	= &if_header;
	dev->rebuild_header	= &if_rebuild_hdr;
	dev->hard_start_xmit	= &if_send;
	dev->get_stats		= &if_stats;
Linus Torvalds's avatar
Linus Torvalds committed
1120 1121 1122
	dev->tx_timeout		= &if_tx_timeout;
	dev->watchdog_timeo	= TX_TIMEOUT;

Linus Torvalds's avatar
Linus Torvalds committed
1123
	/* Initialize media-specific parameters */
Linus Torvalds's avatar
Linus Torvalds committed
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
	dev->type		= ARPHRD_PPP;		/* ARP h/w type */
	dev->flags		|= IFF_POINTOPOINT;
	dev->flags		|= IFF_NOARP;

	if (chan->common.usedby == API){
		dev->mtu	= X25_CHAN_MTU+sizeof(x25api_hdr_t);
	}else{
		dev->mtu	= card->wandev.mtu; 	
	}
	
Linus Torvalds's avatar
Linus Torvalds committed
1134 1135
	dev->hard_header_len	= X25_HRDHDR_SZ; /* media header length */
	dev->addr_len		= 2;		/* hardware address length */
Linus Torvalds's avatar
Linus Torvalds committed
1136 1137 1138 1139 1140
	
	if (!chan->common.svc){
		*(unsigned short*)dev->dev_addr = htons(chan->common.lcn);
	}
	
Linus Torvalds's avatar
Linus Torvalds committed
1141 1142 1143 1144 1145
	/* Initialize hardware parameters (just for reference) */
	dev->irq	= wandev->irq;
	dev->dma	= wandev->dma;
	dev->base_addr	= wandev->ioport;
	dev->mem_start	= (unsigned long)wandev->maddr;
Linus Torvalds's avatar
Linus Torvalds committed
1146
	dev->mem_end	= wandev->maddr + wandev->msize - 1;
Linus Torvalds's avatar
Linus Torvalds committed
1147 1148

        /* Set transmit buffer queue length */
Linus Torvalds's avatar
Linus Torvalds committed
1149
        dev->tx_queue_len = 100;
1150
	SET_MODULE_OWNER(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1151

Linus Torvalds's avatar
Linus Torvalds committed
1152
	/* FIXME Why are we doing this */
Linus Torvalds's avatar
Linus Torvalds committed
1153 1154 1155 1156
	set_chan_state(dev, WAN_DISCONNECTED);
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170

/*===================================================================
 * Name:	if_open(),   Open/Bring up the Netowrk Interface 
 *
 * Purpose:	To bring up a network interface.
 * 
 * Rationale:	
 *                
 * Description:	Open network interface.
 * 		o prevent module from unloading by incrementing use count
 * 		o if link is disconnected then initiate connection
 *
 * Called by:	Kernel (/usr/src/linux/net/core/dev.c)
 * 		(dev->open())
Linus Torvalds's avatar
Linus Torvalds committed
1171
 *
Linus Torvalds's avatar
Linus Torvalds committed
1172 1173 1174 1175 1176 1177
 * Assumptions: None
 *	
 * Warnings:	None
 *
 * Return: 	0 	Ok
 * 		<0 	Failur: Interface will not come up.
Linus Torvalds's avatar
Linus Torvalds committed
1178
 */
Linus Torvalds's avatar
Linus Torvalds committed
1179

1180
static int if_open(struct net_device* dev)
Linus Torvalds's avatar
Linus Torvalds committed
1181 1182 1183
{
	x25_channel_t* chan = dev->priv;
	sdla_t* card = chan->card;
Linus Torvalds's avatar
Linus Torvalds committed
1184 1185
	struct timeval tv;
	unsigned long smp_flags;
Linus Torvalds's avatar
Linus Torvalds committed
1186
	
1187
	if (netif_running(dev))
Linus Torvalds's avatar
Linus Torvalds committed
1188
		return -EBUSY;
Linus Torvalds's avatar
Linus Torvalds committed
1189

Linus Torvalds's avatar
Linus Torvalds committed
1190
	chan->tq_working = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1191

Linus Torvalds's avatar
Linus Torvalds committed
1192 1193 1194 1195
	/* Initialize the task queue */
	chan->common.wanpipe_task.sync = 0;
	chan->common.wanpipe_task.routine = (void *)(void *)x25api_bh;
	chan->common.wanpipe_task.data = dev;
Linus Torvalds's avatar
Linus Torvalds committed
1196

Linus Torvalds's avatar
Linus Torvalds committed
1197 1198 1199
	/* Allocate and initialize BH circular buffer */
	/* Add 1 to MAX_BH_BUFF so we don't have test with (MAX_BH_BUFF-1) */
	chan->bh_head = kmalloc((sizeof(bh_data_t)*(MAX_BH_BUFF+1)),GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
1200

Linus Torvalds's avatar
Linus Torvalds committed
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
	if (chan->bh_head == NULL){
		printk(KERN_INFO "%s: ERROR, failed to allocate memory ! BH_BUFFERS !\n",
				card->devname);

		return -ENOBUFS;
	}
	memset(chan->bh_head,0,(sizeof(bh_data_t)*(MAX_BH_BUFF+1)));
	atomic_set(&chan->bh_buff_used, 0);

	/* Increment the number of interfaces */
	++card->u.x.no_dev;
	
	wanpipe_open(card);

	/* LAPB protocol only uses one interface, thus
	 * start the protocol after it comes up. */
	if (card->u.x.LAPB_hdlc){
		if (card->open_cnt == 1){
			TX25Status* status = card->flags;
			S508_S514_lock(card, &smp_flags);
			x25_set_intr_mode(card, INTR_ON_TIMER); 
			status->imask &= ~INTR_ON_TIMER;
			S508_S514_unlock(card, &smp_flags);
		}
	}else{
		/* X25 can have multiple interfaces thus, start the 
		 * protocol once all interfaces are up */

		//FIXME: There is a bug here. If interface is
		//brought down and up, it will try to enable comm.
		if (card->open_cnt == card->u.x.num_of_ch){

			S508_S514_lock(card, &smp_flags);
			connect(card);
			S508_S514_unlock(card, &smp_flags);

1237
			mod_timer(&card->u.x.x25_timer, jiffies + HZ);
Linus Torvalds's avatar
Linus Torvalds committed
1238 1239
		}
	}
1240
	/* Device is not up until the we are in connected state */
Linus Torvalds's avatar
Linus Torvalds committed
1241 1242 1243 1244
	do_gettimeofday( &tv );
	chan->router_start_time = tv.tv_sec;

	netif_start_queue(dev);
1245

Linus Torvalds's avatar
Linus Torvalds committed
1246 1247
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
1248

Linus Torvalds's avatar
Linus Torvalds committed
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
/*===================================================================
 * Name:	if_close(),   Close/Bring down the Netowrk Interface 
 *
 * Purpose:	To bring down a network interface.
 * 
 * Rationale:	
 *                
 * Description:	Close network interface.
 * 		o decrement use module use count
 *
 * Called by:	Kernel (/usr/src/linux/net/core/dev.c)
 * 		(dev->close())
 *		ifconfig <name> down: will trigger the kernel
 *              which will call this function.
 *
 * Assumptions: None
 *	
 * Warnings:	None
 *
 * Return: 	0 	Ok
 * 		<0 	Failure: Interface will not exit properly.
 */
1271
static int if_close(struct net_device* dev)
Linus Torvalds's avatar
Linus Torvalds committed
1272 1273 1274 1275 1276
{
	x25_channel_t* chan = dev->priv;
	sdla_t* card = chan->card;
	unsigned long smp_flags;
	
1277
	netif_stop_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1278

Linus Torvalds's avatar
Linus Torvalds committed
1279 1280 1281
	if ((chan->common.state == WAN_CONNECTED) || 
	    (chan->common.state == WAN_CONNECTING)){
		S508_S514_lock(card, &smp_flags);
Linus Torvalds's avatar
Linus Torvalds committed
1282
		chan_disc(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1283 1284 1285
		S508_S514_unlock(card, &smp_flags);
	}

Linus Torvalds's avatar
Linus Torvalds committed
1286 1287
	wanpipe_close(card);

Linus Torvalds's avatar
Linus Torvalds committed
1288 1289 1290 1291 1292 1293 1294 1295
	S508_S514_lock(card, &smp_flags);
	if (chan->bh_head){
		int i;
		struct sk_buff *skb;
	
		for (i=0; i<(MAX_BH_BUFF+1); i++){
			skb = ((bh_data_t *)&chan->bh_head[i])->skb;
			if (skb != NULL){
1296
                		dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1297 1298 1299 1300 1301 1302 1303
			}
		}
		kfree(chan->bh_head);
		chan->bh_head=NULL;
	}
	S508_S514_unlock(card, &smp_flags);

Linus Torvalds's avatar
Linus Torvalds committed
1304
	/* If this is the last close, disconnect physical link */
Linus Torvalds's avatar
Linus Torvalds committed
1305 1306
	if (!card->open_cnt){
		S508_S514_lock(card, &smp_flags);
Linus Torvalds's avatar
Linus Torvalds committed
1307
		disconnect(card);
Linus Torvalds's avatar
Linus Torvalds committed
1308 1309 1310 1311 1312 1313
		x25_set_intr_mode(card, 0);
		S508_S514_unlock(card, &smp_flags);
	}
	
	/* Decrement the number of interfaces */
	--card->u.x.no_dev;
Linus Torvalds's avatar
Linus Torvalds committed
1314 1315 1316
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1317 1318 1319
/*======================================================================
 * 	Build media header.
 * 	o encapsulate packet according to encapsulation type.
Linus Torvalds's avatar
Linus Torvalds committed
1320
 *
Linus Torvalds's avatar
Linus Torvalds committed
1321 1322 1323 1324
 * 	The trick here is to put packet type (Ethertype) into 'protocol' 
 *      field of the socket buffer, so that we don't forget it.  
 *      If encapsulation fails, set skb->protocol to 0 and discard 
 *      packet later.
Linus Torvalds's avatar
Linus Torvalds committed
1325
 *
Linus Torvalds's avatar
Linus Torvalds committed
1326 1327 1328
 * 	Return:		media header length.
 *======================================================================*/

1329 1330 1331
static int if_header(struct sk_buff* skb, struct net_device* dev,
		     unsigned short type, void* daddr, void* saddr,
		     unsigned len)
Linus Torvalds's avatar
Linus Torvalds committed
1332 1333 1334
{
	x25_channel_t* chan = dev->priv;
	int hdr_len = dev->hard_header_len;
Linus Torvalds's avatar
Linus Torvalds committed
1335 1336 1337 1338 1339
	
	skb->protocol = htons(type);
	if (!chan->protocol){
		hdr_len = wanrouter_encapsulate(skb, dev, type);
		if (hdr_len < 0){
Linus Torvalds's avatar
Linus Torvalds committed
1340
			hdr_len = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1341
			skb->protocol = htons(0);
Linus Torvalds's avatar
Linus Torvalds committed
1342 1343 1344 1345 1346
		}
	}
	return hdr_len;
}

Linus Torvalds's avatar
Linus Torvalds committed
1347 1348
/*===============================================================
 * 	Re-build media header.
Linus Torvalds's avatar
Linus Torvalds committed
1349
 *
Linus Torvalds's avatar
Linus Torvalds committed
1350 1351 1352 1353
 * 	Return:		1	physical address resolved.
 *			0	physical address not resolved
 *==============================================================*/

Linus Torvalds's avatar
Linus Torvalds committed
1354 1355
static int if_rebuild_hdr (struct sk_buff* skb)
{
1356
	struct net_device *dev = skb->dev; 
Linus Torvalds's avatar
Linus Torvalds committed
1357 1358 1359 1360 1361 1362 1363 1364
	x25_channel_t* chan = dev->priv;
	sdla_t* card = chan->card;

	printk(KERN_INFO "%s: rebuild_header() called for interface %s!\n",
		card->devname, dev->name);
	return 1;
}

Linus Torvalds's avatar
Linus Torvalds committed
1365

Linus Torvalds's avatar
Linus Torvalds committed
1366
/*============================================================================
Linus Torvalds's avatar
Linus Torvalds committed
1367 1368
 * Handle transmit timeout event from netif watchdog
 */
1369
static void if_tx_timeout(struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
{
    	x25_channel_t* chan = dev->priv;
	sdla_t *card = chan->card;

	/* If our device stays busy for at least 5 seconds then we will
	 * kick start the device by making dev->tbusy = 0.  We expect
	 * that our device never stays busy more than 5 seconds. So this                 
	 * is only used as a last resort.
	 */

	++chan->if_send_stat.if_send_tbusy_timeout;
	printk (KERN_INFO "%s: Transmit timed out on %s\n", 
			card->devname, dev->name);
	netif_wake_queue (dev);
}


/*=========================================================================
 * 	Send a packet on a network interface.
 * 	o set tbusy flag (marks start of the transmission).
 * 	o check link state. If link is not up, then drop the packet.
 * 	o check channel status. If it's down then initiate a call.
 * 	o pass a packet to corresponding WAN device.
 * 	o free socket buffer
 *
 * 	Return:	0	complete (socket buffer must be freed)
Linus Torvalds's avatar
Linus Torvalds committed
1396 1397
 *		non-0	packet may be re-transmitted (tbusy must be set)
 *
Linus Torvalds's avatar
Linus Torvalds committed
1398 1399 1400 1401 1402 1403 1404
 * 	Notes:
 * 	1. This routine is called either by the protocol stack or by the "net
 *    	bottom half" (with interrupts enabled).
 * 	2. Setting tbusy flag will inhibit further transmit requests from the
 *    	protocol stack and can be used for flow control with protocol layer.
 *
 *========================================================================*/
Linus Torvalds's avatar
Linus Torvalds committed
1405

1406
static int if_send(struct sk_buff* skb, struct net_device* dev)
Linus Torvalds's avatar
Linus Torvalds committed
1407 1408 1409 1410
{
	x25_channel_t* chan = dev->priv;
	sdla_t* card = chan->card;
	TX25Status* status = card->flags;
Linus Torvalds's avatar
Linus Torvalds committed
1411 1412
	int udp_type;
	unsigned long smp_flags=0;
Linus Torvalds's avatar
Linus Torvalds committed
1413

Linus Torvalds's avatar
Linus Torvalds committed
1414 1415 1416 1417 1418 1419 1420
	++chan->if_send_stat.if_send_entry;

	netif_stop_queue(dev);

	/* No need to check frame length, since socket code
         * will perform the check for us */

Linus Torvalds's avatar
Linus Torvalds committed
1421
	chan->tick_counter = jiffies;
Linus Torvalds's avatar
Linus Torvalds committed
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
	
	/* Critical region starts here */
	S508_S514_lock(card, &smp_flags);
	
	if (test_and_set_bit(SEND_CRIT, (void*)&card->wandev.critical)){
		printk(KERN_INFO "Hit critical in if_send()! %lx\n",card->wandev.critical);
		goto if_send_crit_exit;
	}
	
	udp_type = udp_pkt_type(skb, card);
Linus Torvalds's avatar
Linus Torvalds committed
1432

Linus Torvalds's avatar
Linus Torvalds committed
1433
        if(udp_type != UDP_INVALID_TYPE) {
Linus Torvalds's avatar
Linus Torvalds committed
1434

Linus Torvalds's avatar
Linus Torvalds committed
1435 1436
                if(store_udp_mgmt_pkt(udp_type, UDP_PKT_FRM_STACK, card, dev, skb,
                        chan->common.lcn)) {
Linus Torvalds's avatar
Linus Torvalds committed
1437

Linus Torvalds's avatar
Linus Torvalds committed
1438 1439 1440 1441 1442
                        status->imask |= INTR_ON_TIMER;
                        if (udp_type == UDP_XPIPE_TYPE){
                                chan->if_send_stat.if_send_PIPE_request++;
			}
               	}
1443
		netif_start_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1444 1445 1446
		clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
		S508_S514_unlock(card, &smp_flags);
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1447 1448
	}

Linus Torvalds's avatar
Linus Torvalds committed
1449 1450 1451 1452 1453 1454
	if (chan->transmit_length){
		//FIXME: This check doesn't make sense any more
		if (chan->common.state != WAN_CONNECTED){
			chan->transmit_length=0;
			atomic_set(&chan->common.driver_busy,0);
		}else{
1455
			netif_stop_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1456 1457 1458 1459 1460 1461 1462
			++card->u.x.tx_interrupts_pending;
		        status->imask |= INTR_ON_TX_FRAME;
			clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
			S508_S514_unlock(card, &smp_flags);
			return 1;
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
1463

Linus Torvalds's avatar
Linus Torvalds committed
1464
	if (card->wandev.state != WAN_CONNECTED){
Linus Torvalds's avatar
Linus Torvalds committed
1465
		++chan->ifstats.tx_dropped;
Linus Torvalds's avatar
Linus Torvalds committed
1466 1467 1468 1469
		++card->wandev.stats.tx_dropped;
		++chan->if_send_stat.if_send_wan_disconnected;	
		
	}else if ( chan->protocol && (chan->protocol != skb->protocol)){
Linus Torvalds's avatar
Linus Torvalds committed
1470 1471
		printk(KERN_INFO
			"%s: unsupported Ethertype 0x%04X on interface %s!\n",
Linus Torvalds's avatar
Linus Torvalds committed
1472 1473 1474
			chan->name, htons(skb->protocol), dev->name);
		
		printk(KERN_INFO "PROTO %Xn", htons(chan->protocol));
Linus Torvalds's avatar
Linus Torvalds committed
1475
		++chan->ifstats.tx_errors;
Linus Torvalds's avatar
Linus Torvalds committed
1476 1477 1478 1479 1480 1481
		++chan->ifstats.tx_dropped;
		++card->wandev.stats.tx_dropped;
		++chan->if_send_stat.if_send_protocol_error;
		
	}else switch (chan->common.state){

Linus Torvalds's avatar
Linus Torvalds committed
1482 1483 1484 1485
		case WAN_DISCONNECTED:
			/* Try to establish connection. If succeded, then start
			 * transmission, else drop a packet.
			 */
Linus Torvalds's avatar
Linus Torvalds committed
1486
			if (chan->common.usedby == API){
Linus Torvalds's avatar
Linus Torvalds committed
1487 1488 1489
				++chan->ifstats.tx_dropped;
				++card->wandev.stats.tx_dropped;
				break;
Linus Torvalds's avatar
Linus Torvalds committed
1490 1491 1492 1493 1494 1495
			}else{
				if (chan_connect(dev) != 0){
					++chan->ifstats.tx_dropped;
					++card->wandev.stats.tx_dropped;
					break;
				}
Linus Torvalds's avatar
Linus Torvalds committed
1496 1497 1498 1499
			}
			/* fall through */

		case WAN_CONNECTED:
Linus Torvalds's avatar
Linus Torvalds committed
1500 1501
			if( skb->protocol == htons(ETH_P_IPX)) {
				if(chan->enable_IPX) {
Linus Torvalds's avatar
Linus Torvalds committed
1502
					switch_net_numbers( skb->data, 
Linus Torvalds's avatar
Linus Torvalds committed
1503 1504
						chan->network_number, 0);
				} else {
Linus Torvalds's avatar
Linus Torvalds committed
1505 1506
					++card->wandev.stats.tx_dropped;
					++chan->ifstats.tx_dropped;
Linus Torvalds's avatar
Linus Torvalds committed
1507 1508
					++chan->if_send_stat.if_send_protocol_error;
					goto if_send_crit_exit;
Linus Torvalds's avatar
Linus Torvalds committed
1509 1510
				}
			}
Linus Torvalds's avatar
Linus Torvalds committed
1511 1512 1513 1514
			/* We never drop here, if cannot send than, copy
	                 * a packet into a transmit buffer 
                         */
			chan_send(dev, skb->data, skb->len, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1515 1516 1517 1518 1519
			break;

		default:
			++chan->ifstats.tx_dropped;	
			++card->wandev.stats.tx_dropped;
Linus Torvalds's avatar
Linus Torvalds committed
1520
			break;
Linus Torvalds's avatar
Linus Torvalds committed
1521 1522
	}

Linus Torvalds's avatar
Linus Torvalds committed
1523 1524 1525

if_send_crit_exit:
	
1526
       	dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1527

1528
	netif_start_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1529 1530 1531
	clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
	S508_S514_unlock(card, &smp_flags);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1532 1533 1534
}

/*============================================================================
1535
 * Setup so that a frame can be transmitted on the occurrence of a transmit
Linus Torvalds's avatar
Linus Torvalds committed
1536 1537 1538
 * interrupt.
 *===========================================================================*/

1539 1540
static void setup_for_delayed_transmit(struct net_device* dev, void* buf,
				       unsigned len)
Linus Torvalds's avatar
Linus Torvalds committed
1541
{
Linus Torvalds's avatar
Linus Torvalds committed
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
        x25_channel_t* chan = dev->priv;
        sdla_t* card = chan->card;
	TX25Status* status = card->flags;

	++chan->if_send_stat.if_send_adptr_bfrs_full;

        if(chan->transmit_length) {
                printk(KERN_INFO "%s: Error, transmit length set in delayed transmit!\n",
				card->devname);
                return;
        }

	if (chan->common.usedby == API){
		if (len > X25_CHAN_MTU+sizeof(x25api_hdr_t)) {
			++chan->ifstats.tx_dropped;	
			++card->wandev.stats.tx_dropped;
			printk(KERN_INFO "%s: Length is too big for delayed transmit\n",
				card->devname);
			return;
		}
	}else{
		if (len > X25_MAX_DATA) {
			++chan->ifstats.tx_dropped;	
			++card->wandev.stats.tx_dropped;
			printk(KERN_INFO "%s: Length is too big for delayed transmit\n",
				card->devname);
			return;
		}
	}

        chan->transmit_length = len;
	atomic_set(&chan->common.driver_busy,1);
        memcpy(chan->transmit_buffer, buf, len);

	++chan->if_send_stat.if_send_tx_int_enabled;

	/* Enable Transmit Interrupt */
	++card->u.x.tx_interrupts_pending;
        status->imask |= INTR_ON_TX_FRAME;
}


/*===============================================================
 * net_device_stats
 *
 * 	Get ethernet-style interface statistics.
 * 	Return a pointer to struct enet_statistics.
 *
 *==============================================================*/
1591
static struct net_device_stats *if_stats(struct net_device* dev)
Linus Torvalds's avatar
Linus Torvalds committed
1592 1593 1594 1595
{
	x25_channel_t *chan = dev->priv;

	if(chan == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
1596
		return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1597

Linus Torvalds's avatar
Linus Torvalds committed
1598 1599 1600 1601
	return &chan->ifstats;
}


Linus Torvalds's avatar
Linus Torvalds committed
1602 1603 1604 1605 1606
/*
 *	Interrupt Handlers 
 */

/*
Linus Torvalds's avatar
Linus Torvalds committed
1607 1608
 * X.25 Interrupt Service Routine.
 */
Linus Torvalds's avatar
Linus Torvalds committed
1609

Linus Torvalds's avatar
Linus Torvalds committed
1610 1611 1612 1613 1614
static void wpx_isr (sdla_t* card)
{
	TX25Status* status = card->flags;

	card->in_isr = 1;
Linus Torvalds's avatar
Linus Torvalds committed
1615
	++card->statistics.isr_entry;
Linus Torvalds's avatar
Linus Torvalds committed
1616

Linus Torvalds's avatar
Linus Torvalds committed
1617 1618 1619 1620 1621 1622 1623
	if (test_bit(PERI_CRIT,(void*)&card->wandev.critical)){
		card->in_isr=0;
		status->iflags = 0;
		return;
	}
	
	if (test_bit(SEND_CRIT, (void*)&card->wandev.critical)){
Linus Torvalds's avatar
Linus Torvalds committed
1624

Linus Torvalds's avatar
Linus Torvalds committed
1625 1626
 		printk(KERN_INFO "%s: wpx_isr: wandev.critical set to 0x%02lx, int type = 0x%02x\n", 
			card->devname, card->wandev.critical, status->iflags);
Linus Torvalds's avatar
Linus Torvalds committed
1627
		card->in_isr = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1628
		status->iflags = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1629 1630 1631 1632 1633 1634 1635
		return;
	}

	/* For all interrupts set the critical flag to CRITICAL_RX_INTR.
         * If the if_send routine is called with this flag set it will set
         * the enable transmit flag to 1. (for a delayed interrupt)
         */
Linus Torvalds's avatar
Linus Torvalds committed
1636
	switch (status->iflags){
Linus Torvalds's avatar
Linus Torvalds committed
1637

Linus Torvalds's avatar
Linus Torvalds committed
1638
		case RX_INTR_PENDING:		/* receive interrupt */
Linus Torvalds's avatar
Linus Torvalds committed
1639 1640 1641
			rx_intr(card);
			break;

Linus Torvalds's avatar
Linus Torvalds committed
1642
		case TX_INTR_PENDING:		/* transmit interrupt */
Linus Torvalds's avatar
Linus Torvalds committed
1643 1644 1645
			tx_intr(card);
			break;

Linus Torvalds's avatar
Linus Torvalds committed
1646
		case MODEM_INTR_PENDING:	/* modem status interrupt */
Linus Torvalds's avatar
Linus Torvalds committed
1647 1648 1649
			status_intr(card);
			break;

Linus Torvalds's avatar
Linus Torvalds committed
1650
		case X25_ASY_TRANS_INTR_PENDING:	/* network event interrupt */
Linus Torvalds's avatar
Linus Torvalds committed
1651 1652 1653
			event_intr(card);
			break;

Linus Torvalds's avatar
Linus Torvalds committed
1654 1655 1656 1657
		case TIMER_INTR_PENDING:
			timer_intr(card);
			break;

Linus Torvalds's avatar
Linus Torvalds committed
1658 1659 1660
		default:		/* unwanted interrupt */
			spur_intr(card);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1661

Linus Torvalds's avatar
Linus Torvalds committed
1662 1663 1664 1665
	card->in_isr = 0;
	status->iflags = 0;	/* clear interrupt condition */
}

Linus Torvalds's avatar
Linus Torvalds committed
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
/*
 * 	Receive interrupt handler.
 * 	This routine handles fragmented IP packets using M-bit according to the
 * 	RFC1356.
 * 	o map ligical channel number to network interface.
 * 	o allocate socket buffer or append received packet to the existing one.
 * 	o if M-bit is reset (i.e. it's the last packet in a sequence) then 
 *   	decapsulate packet and pass socket buffer to the protocol stack.
 *
 * 	Notes:
 * 	1. When allocating a socket buffer, if M-bit is set then more data is
 *    	coming and we have to allocate buffer for the maximum IP packet size
 *    	expected on this channel.
 * 	2. If something goes wrong and X.25 packet has to be dropped (e.g. no
 *    	socket buffers available) the whole packet sequence must be discarded.
Linus Torvalds's avatar
Linus Torvalds committed
1681 1682 1683 1684 1685
 */

static void rx_intr (sdla_t* card)
{
	TX25Mbox* rxmb = card->rxmb;
Linus Torvalds's avatar
Linus Torvalds committed
1686
	unsigned lcn = rxmb->cmd.lcn;
1687
	struct net_device* dev = find_channel(card,lcn);
Linus Torvalds's avatar
Linus Torvalds committed
1688
	x25_channel_t* chan;
Linus Torvalds's avatar
Linus Torvalds committed
1689
	struct sk_buff* skb=NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1690

Linus Torvalds's avatar
Linus Torvalds committed
1691
	if (dev == NULL){
Linus Torvalds's avatar
Linus Torvalds committed
1692 1693 1694 1695 1696 1697 1698 1699
		/* Invalid channel, discard packet */
		printk(KERN_INFO "%s: receiving on orphaned LCN %d!\n",
			card->devname, lcn);
		return;
	}

	chan = dev->priv;
	chan->i_timeout_sofar = jiffies;
Linus Torvalds's avatar
Linus Torvalds committed
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709


	/* Copy the data from the board, into an
         * skb buffer 
	 */
	if (wanpipe_pull_data_in_skb(card,dev,&skb)){
		++chan->ifstats.rx_dropped;
		++card->wandev.stats.rx_dropped;
		++chan->rx_intr_stat.rx_intr_no_socket;
		++chan->rx_intr_stat.rx_intr_bfr_not_passed_to_stack;
Linus Torvalds's avatar
Linus Torvalds committed
1710 1711 1712
		return;
	}

Linus Torvalds's avatar
Linus Torvalds committed
1713
	dev->last_rx = jiffies;		/* timestamp */
Linus Torvalds's avatar
Linus Torvalds committed
1714

Linus Torvalds's avatar
Linus Torvalds committed
1715 1716 1717 1718 1719 1720

	/* ------------ API ----------------*/

	if (chan->common.usedby == API){

		if (bh_enqueue(dev, skb)){
Linus Torvalds's avatar
Linus Torvalds committed
1721
			++chan->ifstats.rx_dropped;
Linus Torvalds's avatar
Linus Torvalds committed
1722 1723
			++card->wandev.stats.rx_dropped;
			++chan->rx_intr_stat.rx_intr_bfr_not_passed_to_stack;
1724
			dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1725
			return;
Linus Torvalds's avatar
Linus Torvalds committed
1726
		}		
Linus Torvalds's avatar
Linus Torvalds committed
1727

Linus Torvalds's avatar
Linus Torvalds committed
1728 1729 1730
		++chan->ifstats.rx_packets;
		chan->ifstats.rx_bytes += skb->len;
		
Linus Torvalds's avatar
Linus Torvalds committed
1731

Linus Torvalds's avatar
Linus Torvalds committed
1732 1733 1734 1735 1736
		chan->rx_skb = NULL;
		if (!test_and_set_bit(0, &chan->tq_working)){
			wanpipe_queue_tq(&chan->common.wanpipe_task);
			wanpipe_mark_bh();
		}
Linus Torvalds's avatar
Linus Torvalds committed
1737 1738 1739 1740
		return;
	}


Linus Torvalds's avatar
Linus Torvalds committed
1741 1742 1743 1744 1745
	/* ------------- WANPIPE -------------------*/
	
	/* set rx_skb to NULL so we won't access it later when kernel already owns it */
	chan->rx_skb=NULL;
	
Linus Torvalds's avatar
Linus Torvalds committed
1746
	/* Decapsulate packet, if necessary */
Linus Torvalds's avatar
Linus Torvalds committed
1747
	if (!skb->protocol && !wanrouter_type_trans(skb, dev)){
Linus Torvalds's avatar
Linus Torvalds committed
1748
		/* can't decapsulate packet */
1749
                dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1750
		++chan->ifstats.rx_errors;
Linus Torvalds's avatar
Linus Torvalds committed
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
		++chan->ifstats.rx_dropped;
		++card->wandev.stats.rx_dropped;
		++chan->rx_intr_stat.rx_intr_bfr_not_passed_to_stack;

	}else{
		if( handle_IPXWAN(skb->data, chan->name, 
				  chan->enable_IPX, chan->network_number, 
				  skb->protocol)){

			if( chan->enable_IPX ){
				if(chan_send(dev, skb->data, skb->len,0)){
Linus Torvalds's avatar
Linus Torvalds committed
1762
					chan->tx_skb = skb;
Linus Torvalds's avatar
Linus Torvalds committed
1763
				}else{
1764
                                        dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1765
					++chan->rx_intr_stat.rx_intr_bfr_not_passed_to_stack;
Linus Torvalds's avatar
Linus Torvalds committed
1766
				}
Linus Torvalds's avatar
Linus Torvalds committed
1767 1768 1769 1770
			}else{
				/* increment IPX packet dropped statistic */
				++chan->ifstats.rx_dropped;
				++chan->rx_intr_stat.rx_intr_bfr_not_passed_to_stack;
Linus Torvalds's avatar
Linus Torvalds committed
1771
			}
Linus Torvalds's avatar
Linus Torvalds committed
1772 1773
		}else{
			skb->mac.raw = skb->data;
Linus Torvalds's avatar
Linus Torvalds committed
1774
			chan->ifstats.rx_bytes += skb->len;
Linus Torvalds's avatar
Linus Torvalds committed
1775 1776 1777
			++chan->ifstats.rx_packets;
			++chan->rx_intr_stat.rx_intr_bfr_passed_to_stack;
			netif_rx(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1778 1779
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
1780 1781
	
	return;
Linus Torvalds's avatar
Linus Torvalds committed
1782 1783 1784
}


1785 1786
static int wanpipe_pull_data_in_skb(sdla_t *card, struct net_device *dev,
				    struct sk_buff **skb)
Linus Torvalds's avatar
Linus Torvalds committed
1787
{
Linus Torvalds's avatar
Linus Torvalds committed
1788 1789 1790 1791 1792 1793
	void *bufptr;
	TX25Mbox* rxmb = card->rxmb;
	unsigned len = rxmb->cmd.length;	/* packet length */
	unsigned qdm = rxmb->cmd.qdm;		/* Q,D and M bits */
	x25_channel_t *chan = dev->priv;
	struct sk_buff *new_skb = *skb;
Linus Torvalds's avatar
Linus Torvalds committed
1794

Linus Torvalds's avatar
Linus Torvalds committed
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
	if (chan->common.usedby == WANPIPE){
		if (chan->drop_sequence){
			if (!(qdm & 0x01)){ 
				chan->drop_sequence = 0;
			}
			return 1;
		}
		new_skb = chan->rx_skb;
	}else{
		/* Add on the API header to the received
                 * data 
		 */
		len += sizeof(x25api_hdr_t);
Linus Torvalds's avatar
Linus Torvalds committed
1808 1809
	}

Linus Torvalds's avatar
Linus Torvalds committed
1810 1811
	if (new_skb == NULL){
		int bufsize;
Linus Torvalds's avatar
Linus Torvalds committed
1812

Linus Torvalds's avatar
Linus Torvalds committed
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
		if (chan->common.usedby == WANPIPE){
			bufsize = (qdm & 0x01) ? dev->mtu : len;
		}else{
			bufsize = len;
		}

		/* Allocate new socket buffer */
		new_skb = dev_alloc_skb(bufsize + dev->hard_header_len);
		if (new_skb == NULL){
			printk(KERN_INFO "%s: no socket buffers available!\n",
				card->devname);
			chan->drop_sequence = 1;	/* set flag */
			++chan->ifstats.rx_dropped;
			return 1;
		}
	}

	if (skb_tailroom(new_skb) < len){
		/* No room for the packet. Call off the whole thing! */
1832
                dev_kfree_skb_any(new_skb);
Linus Torvalds's avatar
Linus Torvalds committed
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
		if (chan->common.usedby == WANPIPE){
			chan->rx_skb = NULL;
			if (qdm & 0x01){ 
				chan->drop_sequence = 1;
			}
		}

		printk(KERN_INFO "%s: unexpectedly long packet sequence "
			"on interface %s!\n", card->devname, dev->name);
		++chan->ifstats.rx_length_errors;
		return 1;
	}

	bufptr = skb_put(new_skb,len);


	if (chan->common.usedby == API){
		/* Fill in the x25api header 
		 */
		x25api_t * api_data = (x25api_t*)bufptr;
		api_data->hdr.qdm = rxmb->cmd.qdm;
		api_data->hdr.cause = rxmb->cmd.cause;
		api_data->hdr.diagn = rxmb->cmd.diagn;
		api_data->hdr.length = rxmb->cmd.length;
		memcpy(api_data->data, rxmb->data, rxmb->cmd.length);
	}else{
		memcpy(bufptr, rxmb->data, len);
	}

	new_skb->dev = dev;

	if (chan->common.usedby == API){
		new_skb->mac.raw = new_skb->data;
		new_skb->protocol = htons(X25_PROT);
		new_skb->pkt_type = WAN_PACKET_DATA;
	}else{
		new_skb->protocol = chan->protocol;
		chan->rx_skb = new_skb;
	}

	/* If qdm bit is set, more data is coming 
         * thus, exit and wait for more data before
         * sending the packet up. (Used by router only) 
	 */
	if ((qdm & 0x01) && (chan->common.usedby == WANPIPE)) 
		return 1;	

	*skb = new_skb; 

	return 0;
}

/*===============================================================
 * tx_intr
 *  
 * 	Transmit interrupt handler.
 *	For each dev, check that there is something to send.
 *	If data available, transmit. 	
 *
 *===============================================================*/

static void tx_intr (sdla_t* card)
{
1896
	struct net_device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918
	TX25Status* status = card->flags;
	unsigned char more_to_tx=0;
	x25_channel_t *chan=NULL;
	int i=0;	

	if (card->u.x.tx_dev == NULL){
		card->u.x.tx_dev = card->wandev.dev;
	}

	dev = card->u.x.tx_dev;

	for (;;){

		chan = dev->priv;
		if (chan->transmit_length){
			/* Device was set to transmit, check if the TX
                         * buffers are available 
			 */		
			if (chan->common.state != WAN_CONNECTED){
				chan->transmit_length = 0;
				atomic_set(&chan->common.driver_busy,0);
				chan->tx_offset=0;
1919
				if (netif_queue_stopped(dev)){
Linus Torvalds's avatar
Linus Torvalds committed
1920
					if (chan->common.usedby == API){
1921
						netif_start_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1922 1923
						wakeup_sk_bh(dev);
					}else{
1924
						netif_wake_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
					}
				}
				dev = move_dev_to_next(card,dev);
				break;
			}				

			if ((status->cflags[chan->ch_idx] & 0x40 || card->u.x.LAPB_hdlc) && 
			     (*card->u.x.hdlc_buf_status & 0x40) ){
				/* Tx buffer available, we can send */
				
				if (tx_intr_send(card, dev)){
					more_to_tx=1;
				}

				/* If more than one interface present, move the
                                 * device pointer to the next interface, so on the 
                                 * next TX interrupt we will try sending from it. 
                                 */
				dev = move_dev_to_next(card,dev);
				break;
			}else{
				/* Tx buffers not available, but device set
                                 * the TX interrupt.  Set more_to_tx and try  
                                 * to transmit for other devices.
				 */
				more_to_tx=1;
				dev = move_dev_to_next(card,dev);
			}

		}else{
			/* This device was not set to transmit,
                         * go to next 
			 */
			dev = move_dev_to_next(card,dev);
		}	

		if (++i == card->u.x.no_dev){
			if (!more_to_tx){
				DBG_PRINTK(KERN_INFO "%s: Nothing to Send in TX INTR\n",
					card->devname);
			}
			break;
		}

	} //End of FOR

	card->u.x.tx_dev = dev;
	
	if (!more_to_tx){
		/* if any other interfaces have transmit interrupts pending, */
		/* do not disable the global transmit interrupt */
		if (!(--card->u.x.tx_interrupts_pending)){
			status->imask &= ~INTR_ON_TX_FRAME;
		}
	}
	return;
}

/*===============================================================
 * move_dev_to_next
 *  
 *
 *===============================================================*/


1990
struct net_device *move_dev_to_next(sdla_t *card, struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
1991 1992
{
	if (card->u.x.no_dev != 1){
1993
		if (!*((struct net_device **)dev->priv))
Linus Torvalds's avatar
Linus Torvalds committed
1994
			return card->wandev.dev;
1995 1996
		else
			return *((struct net_device **)dev->priv);
Linus Torvalds's avatar
Linus Torvalds committed
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
	}
	return dev;
}

/*===============================================================
 *  tx_intr_send
 *  
 *
 *===============================================================*/

2007
static int tx_intr_send(sdla_t *card, struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024
{
	x25_channel_t* chan = dev->priv; 

	if (chan_send (dev,chan->transmit_buffer,chan->transmit_length,1)){
		 
                /* Packet was split up due to its size, do not disable
                 * tx_intr 
                 */
		return 1;
	}

	chan->transmit_length=0;
	atomic_set(&chan->common.driver_busy,0);
	chan->tx_offset=0;

	/* If we are in API mode, wakeup the 
         * sock BH handler, not the NET_BH */
2025
	if (netif_queue_stopped(dev)){
Linus Torvalds's avatar
Linus Torvalds committed
2026
		if (chan->common.usedby == API){
2027
			netif_start_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
2028 2029
			wakeup_sk_bh(dev);
		}else{
2030
			netif_wake_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069
		}
	}
	return 0;
}


/*===============================================================
 * timer_intr
 *  
 * 	Timer interrupt handler.
 *	Check who called the timer interrupt and perform
 *      action accordingly.
 *
 *===============================================================*/

static void timer_intr (sdla_t *card)
{
	TX25Status* status = card->flags;

	if (card->u.x.timer_int_enabled & TMR_INT_ENABLED_CMD_EXEC){

		if (timer_intr_cmd_exec(card) == 0){
			card->u.x.timer_int_enabled &=
				~TMR_INT_ENABLED_CMD_EXEC;
		}

	}else  if(card->u.x.timer_int_enabled & TMR_INT_ENABLED_UDP_PKT) {

		if ((*card->u.x.hdlc_buf_status & 0x40) && 
		    card->u.x.udp_type == UDP_XPIPE_TYPE){

                    	if(process_udp_mgmt_pkt(card)) {
		                card->u.x.timer_int_enabled &= 
					~TMR_INT_ENABLED_UDP_PKT;
			}
		}

	}else if (card->u.x.timer_int_enabled & TMR_INT_ENABLED_POLL_ACTIVE) {

2070
		struct net_device *dev = card->u.x.poll_device;
Linus Torvalds's avatar
Linus Torvalds committed
2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
		x25_channel_t *chan = NULL;

		if (!dev){
			card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_POLL_ACTIVE;
			return;
		}
		chan = dev->priv;

		printk(KERN_INFO 
			"%s: Closing down Idle link %s on LCN %d\n",
					card->devname,chan->name,chan->common.lcn); 
		chan->i_timeout_sofar = jiffies;
		chan_disc(dev);	
         	card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_POLL_ACTIVE;
		card->u.x.poll_device=NULL;

	}else if (card->u.x.timer_int_enabled & TMR_INT_ENABLED_POLL_CONNECT_ON) {

		wanpipe_set_state(card, WAN_CONNECTED);
		if (card->u.x.LAPB_hdlc){
2091
			struct net_device *dev = card->wandev.dev;
Linus Torvalds's avatar
Linus Torvalds committed
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
			set_chan_state(dev,WAN_CONNECTED);
			send_delayed_cmd_result(card,dev,card->mbox);	
		}

		/* 0x8F enable all interrupts */
		x25_set_intr_mode(card, INTR_ON_RX_FRAME|	
					INTR_ON_TX_FRAME|
					INTR_ON_MODEM_STATUS_CHANGE|
					//INTR_ON_COMMAND_COMPLETE|
					X25_ASY_TRANS_INTR_PENDING |
					INTR_ON_TIMER |
					DIRECT_RX_INTR_USAGE
				); 

		status->imask &= ~INTR_ON_TX_FRAME;	/* mask Tx interrupts */
		card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_POLL_CONNECT_ON;

	}else if (card->u.x.timer_int_enabled & TMR_INT_ENABLED_POLL_CONNECT_OFF) {

		//printk(KERN_INFO "Poll connect, Turning OFF\n");
		disconnect(card);
		card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_POLL_CONNECT_OFF;

	}else if (card->u.x.timer_int_enabled & TMR_INT_ENABLED_POLL_DISCONNECT) {

		//printk(KERN_INFO "POll disconnect, trying to connect\n");
		connect(card);
		card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_POLL_DISCONNECT;

	}else if (card->u.x.timer_int_enabled & TMR_INT_ENABLED_UPDATE){

		if (*card->u.x.hdlc_buf_status & 0x40){
			x25_get_err_stats(card);
			x25_get_stats(card);
			card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_UPDATE;
		}
	}

	if(!card->u.x.timer_int_enabled){
		//printk(KERN_INFO "Turning Timer Off \n");
                status->imask &= ~INTR_ON_TIMER;	
	}
}

/*====================================================================
 * 	Modem status interrupt handler.
 *===================================================================*/
static void status_intr (sdla_t* card)
{

	/* Added to avoid Modem status message flooding */
	static TX25ModemStatus last_stat;

	TX25Mbox* mbox = card->mbox;
	TX25ModemStatus *modem_status;
2147
	struct net_device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
	x25_channel_t *chan;
	int err;

	memset(&mbox->cmd, 0, sizeof(TX25Cmd));
	mbox->cmd.command = X25_READ_MODEM_STATUS;
	err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	if (err){ 
		x25_error(card, err, X25_READ_MODEM_STATUS, 0);
	}else{
	
		modem_status = (TX25ModemStatus*)mbox->data;	
	
           	/* Check if the last status was the same
           	 * if it was, do NOT print message again */
	
		if (last_stat.status != modem_status->status){

	     		printk(KERN_INFO "%s: Modem Status Change: DCD=%s, CTS=%s\n",
				card->devname,DCD(modem_status->status),CTS(modem_status->status));

			last_stat.status = modem_status->status;
		
			if (card->u.x.oob_on_modem){

				mbox->cmd.pktType = mbox->cmd.command;
				mbox->cmd.result = 0x08;

				/* Send a OOB to all connected sockets */
2176 2177
				for (dev = card->wandev.dev; dev;
				     dev = *((struct net_device**)dev->priv)) {
Linus Torvalds's avatar
Linus Torvalds committed
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215
					chan=dev->priv;
					if (chan->common.usedby == API){
						send_oob_msg(card,dev,mbox);				
					}
				}

				/* The modem OOB message will probably kill the
				 * the link. If we don't clear the flag here,
				 * a deadlock could occur */ 
				if (atomic_read(&card->u.x.command_busy)){
					atomic_set(&card->u.x.command_busy,0);
				}
			}
		}
	}

	memset(&mbox->cmd, 0, sizeof(TX25Cmd));
	mbox->cmd.command = X25_HDLC_LINK_STATUS;
	err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	if (err){ 
		x25_error(card, err, X25_HDLC_LINK_STATUS, 0);
	}

}

/*====================================================================
 * 	Network event interrupt handler.
 *===================================================================*/
static void event_intr (sdla_t* card)
{
	x25_fetch_events(card);
}

/*====================================================================
 * 	Spurious interrupt handler.
 * 	o print a warning
 * 	o	 
 *====================================================================*/
Linus Torvalds's avatar
Linus Torvalds committed
2216 2217 2218 2219 2220 2221 2222

static void spur_intr (sdla_t* card)
{
	printk(KERN_INFO "%s: spurious interrupt!\n", card->devname);
}


Linus Torvalds's avatar
Linus Torvalds committed
2223 2224
/*
 *	Background Polling Routines  
Linus Torvalds's avatar
Linus Torvalds committed
2225 2226
 */

Linus Torvalds's avatar
Linus Torvalds committed
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237
/*====================================================================
 * 	Main polling routine.
 * 	This routine is repeatedly called by the WANPIPE 'thead' to allow for
 * 	time-dependent housekeeping work.
 *
 * 	Notes:
 * 	1. This routine may be called on interrupt context with all interrupts
 *    	enabled. Beware!
 *====================================================================*/

static void wpx_poll (sdla_t *card)
Linus Torvalds's avatar
Linus Torvalds committed
2238
{
Linus Torvalds's avatar
Linus Torvalds committed
2239 2240 2241
	if (!card->wandev.dev){
		goto wpx_poll_exit;
	}
Linus Torvalds's avatar
Linus Torvalds committed
2242

Linus Torvalds's avatar
Linus Torvalds committed
2243 2244 2245 2246 2247 2248 2249
	if (card->open_cnt != card->u.x.num_of_ch){
		goto wpx_poll_exit;
	}
	
	if (test_bit(PERI_CRIT,&card->wandev.critical)){
		goto wpx_poll_exit;
	}
Linus Torvalds's avatar
Linus Torvalds committed
2250

Linus Torvalds's avatar
Linus Torvalds committed
2251 2252
	if (test_bit(SEND_CRIT,&card->wandev.critical)){
		goto wpx_poll_exit;
Linus Torvalds's avatar
Linus Torvalds committed
2253 2254
	}

Linus Torvalds's avatar
Linus Torvalds committed
2255
	switch(card->wandev.state){
Linus Torvalds's avatar
Linus Torvalds committed
2256 2257 2258 2259 2260 2261 2262 2263 2264 2265
		case WAN_CONNECTED:
			poll_active(card);
			break;

		case WAN_CONNECTING:
			poll_connecting(card);
			break;

		case WAN_DISCONNECTED:
			poll_disconnected(card);
Linus Torvalds's avatar
Linus Torvalds committed
2266
			break;
Linus Torvalds's avatar
Linus Torvalds committed
2267
	}
Linus Torvalds's avatar
Linus Torvalds committed
2268 2269 2270 2271

wpx_poll_exit:
	clear_bit(POLL_CRIT,&card->wandev.critical);
	return;
Linus Torvalds's avatar
Linus Torvalds committed
2272 2273
}

Linus Torvalds's avatar
Linus Torvalds committed
2274 2275 2276 2277 2278 2279 2280 2281 2282 2283
static void trigger_x25_poll(sdla_t *card)
{
	schedule_task(&card->u.x.x25_poll_task);
}

/*====================================================================
 * 	Handle physical link establishment phase.
 * 	o if connection timed out, disconnect the link.
 *===================================================================*/

Linus Torvalds's avatar
Linus Torvalds committed
2284 2285
static void poll_connecting (sdla_t* card)
{
Linus Torvalds's avatar
Linus Torvalds committed
2286 2287 2288 2289 2290 2291 2292 2293 2294
	volatile TX25Status* status = card->flags;

	if (status->gflags & X25_HDLC_ABM){

		timer_intr_exec (card, TMR_INT_ENABLED_POLL_CONNECT_ON);

	}else if ((jiffies - card->state_tick) > CONNECT_TIMEOUT){

		timer_intr_exec (card, TMR_INT_ENABLED_POLL_CONNECT_OFF);
Linus Torvalds's avatar
Linus Torvalds committed
2295 2296 2297 2298

	}
}

Linus Torvalds's avatar
Linus Torvalds committed
2299 2300 2301 2302 2303 2304
/*====================================================================
 * 	Handle physical link disconnected phase.
 * 	o if hold-down timeout has expired and there are open interfaces, 
 *	connect link.
 *===================================================================*/

Linus Torvalds's avatar
Linus Torvalds committed
2305 2306
static void poll_disconnected (sdla_t* card)
{
2307
	struct net_device *dev; 
Linus Torvalds's avatar
Linus Torvalds committed
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
	x25_channel_t *chan;
	TX25Status* status = card->flags;

	if (!card->u.x.LAPB_hdlc && card->open_cnt && 
	    ((jiffies - card->state_tick) > HOLD_DOWN_TIME)){
		timer_intr_exec(card, TMR_INT_ENABLED_POLL_DISCONNECT);
	}


	if ((dev=card->wandev.dev) == NULL)
		return;

	if ((chan=dev->priv) == NULL)
		return;

	if (chan->common.usedby == API && 
	    atomic_read(&chan->common.command) && 
	    card->u.x.LAPB_hdlc){

		if (!(card->u.x.timer_int_enabled & TMR_INT_ENABLED_CMD_EXEC)) 
			card->u.x.timer_int_enabled |= TMR_INT_ENABLED_CMD_EXEC;

		if (!(status->imask & INTR_ON_TIMER))
			status->imask |= INTR_ON_TIMER;
	}	

Linus Torvalds's avatar
Linus Torvalds committed
2334 2335
}

Linus Torvalds's avatar
Linus Torvalds committed
2336 2337 2338 2339 2340 2341
/*====================================================================
 * 	Handle active link phase.
 * 	o fetch X.25 asynchronous events.
 * 	o kick off transmission on all interfaces.
 *===================================================================*/

Linus Torvalds's avatar
Linus Torvalds committed
2342 2343
static void poll_active (sdla_t* card)
{
2344
	struct net_device* dev;
Linus Torvalds's avatar
Linus Torvalds committed
2345
	TX25Status* status = card->flags;
Linus Torvalds's avatar
Linus Torvalds committed
2346

2347 2348
	for (dev = card->wandev.dev; dev;
	     dev = *((struct net_device **)dev->priv)){
Linus Torvalds's avatar
Linus Torvalds committed
2349 2350 2351
		x25_channel_t* chan = dev->priv;

		/* If SVC has been idle long enough, close virtual circuit */
Linus Torvalds's avatar
Linus Torvalds committed
2352 2353 2354 2355 2356
		if ( chan->common.svc && 
		     chan->common.state == WAN_CONNECTED &&
		     chan->common.usedby == WANPIPE ){
		
			if( (jiffies - chan->i_timeout_sofar) / HZ > chan->idle_timeout ){
Linus Torvalds's avatar
Linus Torvalds committed
2357
				/* Close svc */
Linus Torvalds's avatar
Linus Torvalds committed
2358 2359
				card->u.x.poll_device=dev;
				timer_intr_exec	(card, TMR_INT_ENABLED_POLL_ACTIVE);
Linus Torvalds's avatar
Linus Torvalds committed
2360 2361 2362
			}
		}

Linus Torvalds's avatar
Linus Torvalds committed
2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399
#ifdef PRINT_DEBUG
		chan->ifstats.tx_compressed = atomic_read(&chan->common.command);
		chan->ifstats.tx_errors = chan->common.state;
		chan->ifstats.rx_fifo_errors = atomic_read(&card->u.x.command_busy);
		++chan->ifstats.tx_bytes;

		chan->ifstats.rx_fifo_errors=atomic_read(&chan->common.disconnect);
		chan->ifstats.multicast=atomic_read(&chan->bh_buff_used);
		chan->ifstats.rx_length_errors=*card->u.x.hdlc_buf_status;
#endif	

		if (chan->common.usedby == API && 
		    atomic_read(&chan->common.command) && 
	            !card->u.x.LAPB_hdlc){

			if (!(card->u.x.timer_int_enabled & TMR_INT_ENABLED_CMD_EXEC)) 
				card->u.x.timer_int_enabled |= TMR_INT_ENABLED_CMD_EXEC;

			if (!(status->imask & INTR_ON_TIMER))
				status->imask |= INTR_ON_TIMER;
		}	

		if ((chan->common.usedby == API) && 
		     atomic_read(&chan->common.disconnect)){

			if (chan->common.state == WAN_DISCONNECTED){
				atomic_set(&chan->common.disconnect,0);
				return;
			}

			atomic_set(&chan->common.command,X25_CLEAR_CALL);
			if (!(card->u.x.timer_int_enabled & TMR_INT_ENABLED_CMD_EXEC)) 
				card->u.x.timer_int_enabled |= TMR_INT_ENABLED_CMD_EXEC;

			if (!(status->imask & INTR_ON_TIMER))
				status->imask |= INTR_ON_TIMER;
		}
Linus Torvalds's avatar
Linus Torvalds committed
2400 2401 2402
	}
}

Linus Torvalds's avatar
Linus Torvalds committed
2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427
static void timer_intr_exec(sdla_t *card, unsigned char TYPE)
{
	TX25Status* status = card->flags;
	card->u.x.timer_int_enabled |= TYPE;
	if (!(status->imask & INTR_ON_TIMER))
		status->imask |= INTR_ON_TIMER;
}


/*==================================================================== 
 * SDLA Firmware-Specific Functions 
 *
 *  Almost all X.25 commands can unexpetedly fail due to so called 'X.25
 *  asynchronous events' such as restart, interrupt, incoming call request,
 *  call clear request, etc.  They can't be ignored and have to be delt with
 *  immediately.  To tackle with this problem we execute each interface 
 *  command in a loop until good return code is received or maximum number 
 *  of retries is reached.  Each interface command returns non-zero return 
 *  code, an asynchronous event/error handler x25_error() is called.
 *====================================================================*/

/*====================================================================
 * 	Read X.25 firmware version.
 *		Put code version as ASCII string in str. 
 *===================================================================*/
Linus Torvalds's avatar
Linus Torvalds committed
2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445

static int x25_get_version (sdla_t* card, char* str)
{
	TX25Mbox* mbox = card->mbox;
  	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
		mbox->cmd.command = X25_READ_CODE_VERSION;
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	} while (err && retry-- &&
		 x25_error(card, err, X25_READ_CODE_VERSION, 0));

	if (!err && str)
	{
		int len = mbox->cmd.length;
Linus Torvalds's avatar
Linus Torvalds committed
2446

Linus Torvalds's avatar
Linus Torvalds committed
2447 2448 2449 2450 2451 2452
		memcpy(str, mbox->data, len);
		str[len] = '\0';
	}
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
2453 2454 2455
/*====================================================================
 * 	Configure adapter.
 *===================================================================*/
Linus Torvalds's avatar
Linus Torvalds committed
2456 2457 2458 2459 2460 2461 2462

static int x25_configure (sdla_t* card, TX25Config* conf)
{
	TX25Mbox* mbox = card->mbox;
  	int retry = MAX_CMD_RETRY;
	int err;

Linus Torvalds's avatar
Linus Torvalds committed
2463
	do{
Linus Torvalds's avatar
Linus Torvalds committed
2464 2465 2466 2467 2468 2469 2470 2471 2472
		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
		memcpy(mbox->data, (void*)conf, sizeof(TX25Config));
		mbox->cmd.length  = sizeof(TX25Config);
		mbox->cmd.command = X25_SET_CONFIGURATION;
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	} while (err && retry-- && x25_error(card, err, X25_SET_CONFIGURATION, 0));
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
2473 2474 2475 2476 2477
/*====================================================================
 * 	Configure adapter for HDLC only.
 *===================================================================*/

static int hdlc_configure (sdla_t* card, TX25Config* conf)
Linus Torvalds's avatar
Linus Torvalds committed
2478 2479 2480 2481 2482
{
	TX25Mbox* mbox = card->mbox;
  	int retry = MAX_CMD_RETRY;
	int err;

Linus Torvalds's avatar
Linus Torvalds committed
2483
	do{
Linus Torvalds's avatar
Linus Torvalds committed
2484
		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
Linus Torvalds's avatar
Linus Torvalds committed
2485 2486 2487
		memcpy(mbox->data, (void*)conf, sizeof(TX25Config));
		mbox->cmd.length  = sizeof(TX25Config);
		mbox->cmd.command = X25_HDLC_SET_CONFIG;
Linus Torvalds's avatar
Linus Torvalds committed
2488
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
Linus Torvalds's avatar
Linus Torvalds committed
2489
	} while (err && retry-- && x25_error(card, err, X25_SET_CONFIGURATION, 0));
Linus Torvalds's avatar
Linus Torvalds committed
2490

Linus Torvalds's avatar
Linus Torvalds committed
2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532
	return err;
}

static int set_hdlc_level (sdla_t* card)
{

	TX25Mbox* mbox = card->mbox;
  	int retry = MAX_CMD_RETRY;
	int err;

	do{
		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
		mbox->cmd.command = SET_PROTOCOL_LEVEL;
		mbox->cmd.length = 1;
		mbox->data[0] = HDLC_LEVEL; //| DO_HDLC_LEVEL_ERROR_CHECKING; 	
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	} while (err && retry-- && x25_error(card, err, SET_PROTOCOL_LEVEL, 0));

	return err;
}



/*====================================================================
 * Get communications error statistics.
 *====================================================================*/

static int x25_get_err_stats (sdla_t* card)
{
	TX25Mbox* mbox = card->mbox;
  	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
		mbox->cmd.command = X25_HDLC_READ_COMM_ERR;
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	} while (err && retry-- && x25_error(card, err, X25_HDLC_READ_COMM_ERR, 0));
	
	if (!err)
	{
Linus Torvalds's avatar
Linus Torvalds committed
2533 2534 2535 2536 2537 2538 2539 2540 2541 2542
		THdlcCommErr* stats = (void*)mbox->data;

		card->wandev.stats.rx_over_errors    = stats->rxOverrun;
		card->wandev.stats.rx_crc_errors     = stats->rxBadCrc;
		card->wandev.stats.rx_missed_errors  = stats->rxAborted;
		card->wandev.stats.tx_aborted_errors = stats->txAborted;
	}
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
2543 2544 2545 2546
/*====================================================================
 * 	Get protocol statistics.
 *===================================================================*/

Linus Torvalds's avatar
Linus Torvalds committed
2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557
static int x25_get_stats (sdla_t* card)
{
	TX25Mbox* mbox = card->mbox;
  	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
		mbox->cmd.command = X25_READ_STATISTICS;
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
Linus Torvalds's avatar
Linus Torvalds committed
2558
	} while (err && retry-- && x25_error(card, err, X25_READ_STATISTICS, 0)) ;
Linus Torvalds's avatar
Linus Torvalds committed
2559 2560 2561 2562 2563 2564
	
	if (!err)
	{
		TX25Stats* stats = (void*)mbox->data;

		card->wandev.stats.rx_packets = stats->rxData;
Linus Torvalds's avatar
Linus Torvalds committed
2565
		card->wandev.stats.tx_packets = stats->txData;
Linus Torvalds's avatar
Linus Torvalds committed
2566 2567 2568 2569
	}
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
2570 2571 2572 2573
/*====================================================================
 * 	Close HDLC link.
 *===================================================================*/

Linus Torvalds's avatar
Linus Torvalds committed
2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585
static int x25_close_hdlc (sdla_t* card)
{
	TX25Mbox* mbox = card->mbox;
  	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
		mbox->cmd.command = X25_HDLC_LINK_CLOSE;
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	} while (err && retry-- && x25_error(card, err, X25_HDLC_LINK_CLOSE, 0));
Linus Torvalds's avatar
Linus Torvalds committed
2586
	
Linus Torvalds's avatar
Linus Torvalds committed
2587 2588 2589
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
2590 2591 2592 2593 2594

/*====================================================================
 * 	Open HDLC link.
 *===================================================================*/

Linus Torvalds's avatar
Linus Torvalds committed
2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606
static int x25_open_hdlc (sdla_t* card)
{
	TX25Mbox* mbox = card->mbox;
  	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
		mbox->cmd.command = X25_HDLC_LINK_OPEN;
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	} while (err && retry-- && x25_error(card, err, X25_HDLC_LINK_OPEN, 0));
Linus Torvalds's avatar
Linus Torvalds committed
2607

Linus Torvalds's avatar
Linus Torvalds committed
2608 2609 2610
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
2611
/*=====================================================================
Linus Torvalds's avatar
Linus Torvalds committed
2612
 * Setup HDLC link.
Linus Torvalds's avatar
Linus Torvalds committed
2613
 *====================================================================*/
Linus Torvalds's avatar
Linus Torvalds committed
2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629
static int x25_setup_hdlc (sdla_t* card)
{
	TX25Mbox* mbox = card->mbox;
  	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
		mbox->cmd.command = X25_HDLC_LINK_SETUP;
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	} while (err && retry-- && x25_error(card, err, X25_HDLC_LINK_SETUP, 0));
	
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
2630
/*====================================================================
Linus Torvalds's avatar
Linus Torvalds committed
2631
 * Set (raise/drop) DTR.
Linus Torvalds's avatar
Linus Torvalds committed
2632 2633
 *===================================================================*/

Linus Torvalds's avatar
Linus Torvalds committed
2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649
static int x25_set_dtr (sdla_t* card, int dtr)
{
	TX25Mbox* mbox = card->mbox;
  	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
		mbox->data[0] = 0;
		mbox->data[2] = 0;
		mbox->data[1] = dtr ? 0x02 : 0x01;
		mbox->cmd.length  = 3;
		mbox->cmd.command = X25_SET_GLOBAL_VARS;
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	} while (err && retry-- && x25_error(card, err, X25_SET_GLOBAL_VARS, 0));
Linus Torvalds's avatar
Linus Torvalds committed
2650
	
Linus Torvalds's avatar
Linus Torvalds committed
2651 2652 2653
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
2654 2655 2656 2657
/*====================================================================
 * 	Set interrupt mode.
 *===================================================================*/

Linus Torvalds's avatar
Linus Torvalds committed
2658 2659 2660 2661 2662 2663 2664 2665 2666 2667
static int x25_set_intr_mode (sdla_t* card, int mode)
{
	TX25Mbox* mbox = card->mbox;
  	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
		mbox->data[0] = mode;
Linus Torvalds's avatar
Linus Torvalds committed
2668
		if (card->hw.fwid == SFID_X25_508){
Linus Torvalds's avatar
Linus Torvalds committed
2669
			mbox->data[1] = card->hw.irq;
Linus Torvalds's avatar
Linus Torvalds committed
2670 2671 2672 2673
			mbox->data[2] = 2;
			mbox->cmd.length = 3;
		}else {
		 	mbox->cmd.length  = 1;
Linus Torvalds's avatar
Linus Torvalds committed
2674 2675 2676
		}
		mbox->cmd.command = X25_SET_INTERRUPT_MODE;
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
Linus Torvalds's avatar
Linus Torvalds committed
2677 2678
	} while (err && retry-- && x25_error(card, err, X25_SET_INTERRUPT_MODE, 0));
	
Linus Torvalds's avatar
Linus Torvalds committed
2679 2680 2681
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
2682 2683 2684 2685
/*====================================================================
 * 	Read X.25 channel configuration.
 *===================================================================*/

Linus Torvalds's avatar
Linus Torvalds committed
2686 2687 2688 2689
static int x25_get_chan_conf (sdla_t* card, x25_channel_t* chan)
{
	TX25Mbox* mbox = card->mbox;
  	int retry = MAX_CMD_RETRY;
Linus Torvalds's avatar
Linus Torvalds committed
2690
	int lcn = chan->common.lcn;
Linus Torvalds's avatar
Linus Torvalds committed
2691 2692
	int err;

Linus Torvalds's avatar
Linus Torvalds committed
2693
	do{
Linus Torvalds's avatar
Linus Torvalds committed
2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704
		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
		mbox->cmd.lcn     = lcn;
		mbox->cmd.command = X25_READ_CHANNEL_CONFIG;
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	} while (err && retry-- && x25_error(card, err, X25_READ_CHANNEL_CONFIG, lcn));

	if (!err)
	{
		TX25Status* status = card->flags;

		/* calculate an offset into the array of status bytes */
Linus Torvalds's avatar
Linus Torvalds committed
2705 2706
		if (card->u.x.hi_svc <= X25_MAX_CHAN){ 

Linus Torvalds's avatar
Linus Torvalds committed
2707
			chan->ch_idx = lcn - 1;
Linus Torvalds's avatar
Linus Torvalds committed
2708 2709

		}else{
Linus Torvalds's avatar
Linus Torvalds committed
2710 2711
			int offset;

Linus Torvalds's avatar
Linus Torvalds committed
2712 2713 2714 2715 2716 2717
			/* FIX: Apr 14 2000 : Nenad Corbic
			 * The data field was being compared to 0x1F using
                         * '&&' instead of '&'. 
			 * This caused X25API to fail for LCNs greater than 255.
			 */
			switch (mbox->data[0] & 0x1F)
Linus Torvalds's avatar
Linus Torvalds committed
2718
			{
Linus Torvalds's avatar
Linus Torvalds committed
2719
				case 0x01: 
Linus Torvalds's avatar
Linus Torvalds committed
2720
					offset = status->pvc_map; break;
Linus Torvalds's avatar
Linus Torvalds committed
2721
				case 0x03: 
Linus Torvalds's avatar
Linus Torvalds committed
2722
					offset = status->icc_map; break;
Linus Torvalds's avatar
Linus Torvalds committed
2723
				case 0x07: 
Linus Torvalds's avatar
Linus Torvalds committed
2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735
					offset = status->twc_map; break;
				case 0x0B: 
					offset = status->ogc_map; break;
				default: 
					offset = 0;
			}
			chan->ch_idx = lcn - 1 - offset;
		}

		/* get actual transmit packet size on this channel */
		switch(mbox->data[1] & 0x38)
		{
Linus Torvalds's avatar
Linus Torvalds committed
2736 2737
			case 0x00: 
				chan->tx_pkt_size = 16; 
Linus Torvalds's avatar
Linus Torvalds committed
2738
				break;
Linus Torvalds's avatar
Linus Torvalds committed
2739 2740
			case 0x08: 
				chan->tx_pkt_size = 32; 
Linus Torvalds's avatar
Linus Torvalds committed
2741
				break;
Linus Torvalds's avatar
Linus Torvalds committed
2742 2743
			case 0x10: 
				chan->tx_pkt_size = 64; 
Linus Torvalds's avatar
Linus Torvalds committed
2744
				break;
Linus Torvalds's avatar
Linus Torvalds committed
2745 2746
			case 0x18: 
				chan->tx_pkt_size = 128; 
Linus Torvalds's avatar
Linus Torvalds committed
2747
				break;
Linus Torvalds's avatar
Linus Torvalds committed
2748 2749
			case 0x20: 
				chan->tx_pkt_size = 256; 
Linus Torvalds's avatar
Linus Torvalds committed
2750
				break;
Linus Torvalds's avatar
Linus Torvalds committed
2751 2752
			case 0x28: 
				chan->tx_pkt_size = 512; 
Linus Torvalds's avatar
Linus Torvalds committed
2753
				break;
Linus Torvalds's avatar
Linus Torvalds committed
2754 2755
			case 0x30: 
				chan->tx_pkt_size = 1024; 
Linus Torvalds's avatar
Linus Torvalds committed
2756 2757
				break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
2758 2759 2760
		if (card->u.x.logging)
			printk(KERN_INFO "%s: X.25 packet size on LCN %d is %d.\n",
				card->devname, lcn, chan->tx_pkt_size);
Linus Torvalds's avatar
Linus Torvalds committed
2761 2762 2763 2764
	}
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
2765 2766 2767
/*====================================================================
 * 	Place X.25 call.
 *====================================================================*/
Linus Torvalds's avatar
Linus Torvalds committed
2768 2769 2770 2771 2772 2773 2774 2775

static int x25_place_call (sdla_t* card, x25_channel_t* chan)
{
	TX25Mbox* mbox = card->mbox;
  	int retry = MAX_CMD_RETRY;
	int err;
	char str[64];

Linus Torvalds's avatar
Linus Torvalds committed
2776 2777 2778 2779 2780 2781 2782 2783 2784

	if (chan->protocol == htons(ETH_P_IP)){
		sprintf(str, "-d%s -uCC", chan->addr);
	
	}else if (chan->protocol == htons(ETH_P_IPX)){
		sprintf(str, "-d%s -u800000008137", chan->addr);
	
	}
	
Linus Torvalds's avatar
Linus Torvalds committed
2785 2786 2787 2788 2789 2790 2791 2792 2793
	do
	{
		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
		strcpy(mbox->data, str);
		mbox->cmd.length  = strlen(str);
		mbox->cmd.command = X25_PLACE_CALL;
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	} while (err && retry-- && x25_error(card, err, X25_PLACE_CALL, 0));

Linus Torvalds's avatar
Linus Torvalds committed
2794 2795
	if (!err){
		bind_lcn_to_dev (card, chan->dev, mbox->cmd.lcn);
Linus Torvalds's avatar
Linus Torvalds committed
2796 2797 2798 2799
	}
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
2800 2801 2802
/*====================================================================
 * 	Accept X.25 call.
 *====================================================================*/
Linus Torvalds's avatar
Linus Torvalds committed
2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817

static int x25_accept_call (sdla_t* card, int lcn, int qdm)
{
	TX25Mbox* mbox = card->mbox;
  	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
		mbox->cmd.lcn     = lcn;
		mbox->cmd.qdm     = qdm;
		mbox->cmd.command = X25_ACCEPT_CALL;
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	} while (err && retry-- && x25_error(card, err, X25_ACCEPT_CALL, lcn));
Linus Torvalds's avatar
Linus Torvalds committed
2818
	
Linus Torvalds's avatar
Linus Torvalds committed
2819 2820 2821
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
2822 2823 2824 2825
/*====================================================================
 * 	Clear X.25 call.
 *====================================================================*/

Linus Torvalds's avatar
Linus Torvalds committed
2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840
static int x25_clear_call (sdla_t* card, int lcn, int cause, int diagn)
{
	TX25Mbox* mbox = card->mbox;
  	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
		mbox->cmd.lcn     = lcn;
		mbox->cmd.cause   = cause;
		mbox->cmd.diagn   = diagn;
		mbox->cmd.command = X25_CLEAR_CALL;
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	} while (err && retry-- && x25_error(card, err, X25_CLEAR_CALL, lcn));
Linus Torvalds's avatar
Linus Torvalds committed
2841
	
Linus Torvalds's avatar
Linus Torvalds committed
2842 2843 2844
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
2845 2846 2847 2848
/*====================================================================
 * 	Send X.25 data packet.
 *====================================================================*/

Linus Torvalds's avatar
Linus Torvalds committed
2849 2850 2851 2852 2853
static int x25_send (sdla_t* card, int lcn, int qdm, int len, void* buf)
{
	TX25Mbox* mbox = card->mbox;
  	int retry = MAX_CMD_RETRY;
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
2854 2855 2856 2857 2858 2859 2860
	unsigned char cmd;
		
	if (card->u.x.LAPB_hdlc)
		cmd = X25_HDLC_WRITE;
	else
		cmd = X25_WRITE;

Linus Torvalds's avatar
Linus Torvalds committed
2861 2862 2863 2864 2865 2866
	do
	{
		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
		memcpy(mbox->data, buf, len);
		mbox->cmd.length  = len;
		mbox->cmd.lcn     = lcn;
Linus Torvalds's avatar
Linus Torvalds committed
2867 2868 2869 2870 2871 2872 2873 2874

		if (card->u.x.LAPB_hdlc){
			mbox->cmd.pf = qdm;
		}else{			
			mbox->cmd.qdm = qdm;
		}

		mbox->cmd.command = cmd;
Linus Torvalds's avatar
Linus Torvalds committed
2875
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
Linus Torvalds's avatar
Linus Torvalds committed
2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886
	} while (err && retry-- && x25_error(card, err, cmd , lcn));


	/* If buffers are busy the return code for LAPB HDLC is
         * 1. The above functions are looking for return code
         * of X25RES_NOT_READY if busy. */

	if (card->u.x.LAPB_hdlc && err == 1){
		err = X25RES_NOT_READY;
	}

Linus Torvalds's avatar
Linus Torvalds committed
2887 2888 2889
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
2890 2891 2892 2893
/*====================================================================
 * 	Fetch X.25 asynchronous events.
 *===================================================================*/

Linus Torvalds's avatar
Linus Torvalds committed
2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904
static int x25_fetch_events (sdla_t* card)
{
	TX25Status* status = card->flags;
	TX25Mbox* mbox = card->mbox;
	int err = 0;

	if (status->gflags & 0x20)
	{
		memset(&mbox->cmd, 0, sizeof(TX25Cmd));
		mbox->cmd.command = X25_IS_DATA_AVAILABLE;
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
Linus Torvalds's avatar
Linus Torvalds committed
2905
 		if (err) x25_error(card, err, X25_IS_DATA_AVAILABLE, 0);
Linus Torvalds's avatar
Linus Torvalds committed
2906 2907 2908 2909
	}
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924
/*====================================================================
 * 	X.25 asynchronous event/error handler.
 *		This routine is called each time interface command returns 
 *		non-zero return code to handle X.25 asynchronous events and 
 *		common errors. Return non-zero to repeat command or zero to 
 *		cancel it.
 *
 * 	Notes:
 * 	1. This function may be called recursively, as handling some of the
 *    	asynchronous events (e.g. call request) requires execution of the
 *    	interface command(s) that, in turn, may also return asynchronous
 *    	events.  To avoid re-entrancy problems we copy mailbox to dynamically
 *    	allocated memory before processing events.
 *====================================================================*/

Linus Torvalds's avatar
Linus Torvalds committed
2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938
static int x25_error (sdla_t* card, int err, int cmd, int lcn)
{
	int retry = 1;
	unsigned dlen = ((TX25Mbox*)card->mbox)->cmd.length;
	TX25Mbox* mb;

	mb = kmalloc(sizeof(TX25Mbox) + dlen, GFP_ATOMIC);
	if (mb == NULL)
	{
		printk(KERN_ERR "%s: x25_error() out of memory!\n",
			card->devname);
		return 0;
	}
	memcpy(mb, card->mbox, sizeof(TX25Mbox) + dlen);
Linus Torvalds's avatar
Linus Torvalds committed
2939
	switch (err){
Linus Torvalds's avatar
Linus Torvalds committed
2940

Linus Torvalds's avatar
Linus Torvalds committed
2941
	case X25RES_ASYNC_PACKET:	/* X.25 asynchronous packet was received */
Linus Torvalds's avatar
Linus Torvalds committed
2942

Linus Torvalds's avatar
Linus Torvalds committed
2943
		mb->data[dlen] = '\0';
Linus Torvalds's avatar
Linus Torvalds committed
2944

Linus Torvalds's avatar
Linus Torvalds committed
2945
		switch (mb->cmd.pktType & 0x7F){
Linus Torvalds's avatar
Linus Torvalds committed
2946

Linus Torvalds's avatar
Linus Torvalds committed
2947 2948
		case ASE_CALL_RQST:		/* incoming call */
			retry = incoming_call(card, cmd, lcn, mb);
Linus Torvalds's avatar
Linus Torvalds committed
2949 2950
			break;

Linus Torvalds's avatar
Linus Torvalds committed
2951 2952
		case ASE_CALL_ACCEPTED:		/* connected */
			retry = call_accepted(card, cmd, lcn, mb);
Linus Torvalds's avatar
Linus Torvalds committed
2953 2954
			break;

Linus Torvalds's avatar
Linus Torvalds committed
2955 2956
		case ASE_CLEAR_RQST:		/* call clear request */
			retry = call_cleared(card, cmd, lcn, mb);
Linus Torvalds's avatar
Linus Torvalds committed
2957 2958
			break;

Linus Torvalds's avatar
Linus Torvalds committed
2959 2960 2961 2962 2963 2964
		case ASE_RESET_RQST:		/* reset request */
			printk(KERN_INFO "%s: X.25 reset request on LCN %d! "
				"Cause:0x%02X Diagn:0x%02X\n",
				card->devname, mb->cmd.lcn, mb->cmd.cause,
				mb->cmd.diagn);
			api_oob_event (card,mb);
Linus Torvalds's avatar
Linus Torvalds committed
2965 2966
			break;

Linus Torvalds's avatar
Linus Torvalds committed
2967 2968
		case ASE_RESTART_RQST:		/* restart request */
			retry = restart_event(card, cmd, lcn, mb);
Linus Torvalds's avatar
Linus Torvalds committed
2969 2970
			break;

Linus Torvalds's avatar
Linus Torvalds committed
2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994
		case ASE_CLEAR_CONFRM:
			if (clear_confirm_event (card,mb))
				break;

			/* I use the goto statement here so if 
	                 * somebody inserts code between the
        	         * case and default, we will not have
                	 * ghost problems */

			goto dflt_1;

		default:
dflt_1:
			printk(KERN_INFO "%s: X.25 event 0x%02X on LCN %d! "
				"Cause:0x%02X Diagn:0x%02X\n",
				card->devname, mb->cmd.pktType,
				mb->cmd.lcn, mb->cmd.cause, mb->cmd.diagn);
		}
		break;

	case X25RES_PROTO_VIOLATION:	/* X.25 protocol violation indication */

		/* Bug Fix: Mar 14 2000
                 * The Protocol violation error conditions were  
2995
                 * not handled previously */
Linus Torvalds's avatar
Linus Torvalds committed
2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008

		switch (mb->cmd.pktType & 0x7F){

		case PVE_CLEAR_RQST:	/* Clear request */		
			retry = call_cleared(card, cmd, lcn, mb);
			break;	

		case PVE_RESET_RQST:	/* Reset request */
			printk(KERN_INFO "%s: X.25 reset request on LCN %d! "
				"Cause:0x%02X Diagn:0x%02X\n",
				card->devname, mb->cmd.lcn, mb->cmd.cause,
				mb->cmd.diagn);
			api_oob_event (card,mb);
Linus Torvalds's avatar
Linus Torvalds committed
3009 3010
			break;

Linus Torvalds's avatar
Linus Torvalds committed
3011 3012
		case PVE_RESTART_RQST:	/* Restart request */
			retry = restart_event(card, cmd, lcn, mb);
Linus Torvalds's avatar
Linus Torvalds committed
3013 3014
			break;

Linus Torvalds's avatar
Linus Torvalds committed
3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037
		default :
			printk(KERN_INFO
				"%s: X.25 protocol violation on LCN %d! "
				"Packet:0x%02X Cause:0x%02X Diagn:0x%02X\n",
				card->devname, mb->cmd.lcn,
				mb->cmd.pktType & 0x7F, mb->cmd.cause, mb->cmd.diagn);
			api_oob_event(card,mb);
		}
		break;

	case 0x42:	/* X.25 timeout */
		retry = timeout_event(card, cmd, lcn, mb);
		break;

	case 0x43:	/* X.25 retry limit exceeded */
		printk(KERN_INFO
			"%s: exceeded X.25 retry limit on LCN %d! "
			"Packet:0x%02X Diagn:0x%02X\n", card->devname,
			mb->cmd.lcn, mb->cmd.pktType, mb->cmd.diagn)
		;
		break;

	case 0x08:	/* modem failure */
Linus Torvalds's avatar
Linus Torvalds committed
3038
#ifndef MODEM_NOT_LOG
Linus Torvalds's avatar
Linus Torvalds committed
3039
		printk(KERN_INFO "%s: modem failure!\n", card->devname);
Linus Torvalds's avatar
Linus Torvalds committed
3040
#endif /* MODEM_NOT_LOG */
Linus Torvalds's avatar
Linus Torvalds committed
3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068
		api_oob_event(card,mb);
		break;

	case 0x09:	/* N2 retry limit */
		printk(KERN_INFO "%s: exceeded HDLC retry limit!\n",
			card->devname);
		api_oob_event(card,mb);
		break;

	case 0x06:	/* unnumbered frame was received while in ABM */
		printk(KERN_INFO "%s: received Unnumbered frame 0x%02X!\n",
			card->devname, mb->data[0]);
		api_oob_event(card,mb);
		break;

	case CMD_TIMEOUT:
		printk(KERN_ERR "%s: command 0x%02X timed out!\n",
			card->devname, cmd)
		;
		retry = 0;	/* abort command */
		break;

	case X25RES_NOT_READY:
		retry = 1;
		break;

	case 0x01:
		if (card->u.x.LAPB_hdlc)
Linus Torvalds's avatar
Linus Torvalds committed
3069 3070
			break;

Linus Torvalds's avatar
Linus Torvalds committed
3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084
		if (mb->cmd.command == 0x16)
			break;
		/* I use the goto statement here so if 
                 * somebody inserts code between the
                 * case and default, we will not have
                 * ghost problems */
		goto dflt_2;

	default:
dflt_2:
		printk(KERN_INFO "%s: command 0x%02X returned 0x%02X! Lcn %i\n",
			card->devname, cmd, err, mb->cmd.lcn)
		;
		retry = 0;	/* abort command */
Linus Torvalds's avatar
Linus Torvalds committed
3085 3086 3087 3088 3089
	}
	kfree(mb);
	return retry;
}

Linus Torvalds's avatar
Linus Torvalds committed
3090 3091 3092 3093 3094
/*==================================================================== 
 *	X.25 Asynchronous Event Handlers
 * 	These functions are called by the x25_error() and should return 0, if
 * 	the command resulting in the asynchronous event must be aborted.
 *====================================================================*/
Linus Torvalds's avatar
Linus Torvalds committed
3095

Linus Torvalds's avatar
Linus Torvalds committed
3096 3097 3098 3099


/*====================================================================
 *Handle X.25 incoming call request.
Linus Torvalds's avatar
Linus Torvalds committed
3100 3101
 *	RFC 1356 establishes the following rules:
 *	1. The first octet in the Call User Data (CUD) field of the call
Linus Torvalds's avatar
Linus Torvalds committed
3102 3103 3104 3105 3106 3107 3108
 *     	   request packet contains NLPID identifying protocol encapsulation
 * 	2. Calls MUST NOT be accepted unless router supports requested
 *   	   protocol encapsulation.
 *	3. A diagnostic code 249 defined by ISO/IEC 8208 may be used 
 *	   when clearing a call because protocol encapsulation is not 
 *	   supported.
 *	4. If an incoming call is received while a call request is 
3109
 *	   pending (i.e. call collision has occurred), the incoming call 
Linus Torvalds's avatar
Linus Torvalds committed
3110 3111
 *	   shall be rejected and call request shall be retried.
 *====================================================================*/
Linus Torvalds's avatar
Linus Torvalds committed
3112 3113 3114

static int incoming_call (sdla_t* card, int cmd, int lcn, TX25Mbox* mb)
{
3115
	struct wan_device* wandev = &card->wandev;
Linus Torvalds's avatar
Linus Torvalds committed
3116
	int new_lcn = mb->cmd.lcn;
3117
	struct net_device* dev = get_dev_by_lcn(wandev, new_lcn);
Linus Torvalds's avatar
Linus Torvalds committed
3118 3119
	x25_channel_t* chan = NULL;
	int accept = 0;		/* set to '1' if o.k. to accept call */
Linus Torvalds's avatar
Linus Torvalds committed
3120
	unsigned int user_data;
Linus Torvalds's avatar
Linus Torvalds committed
3121
	x25_call_info_t* info;
Linus Torvalds's avatar
Linus Torvalds committed
3122
	
Linus Torvalds's avatar
Linus Torvalds committed
3123 3124 3125 3126 3127 3128
	/* Make sure there is no call collision */
	if (dev != NULL)
	{
		printk(KERN_INFO
			"%s: X.25 incoming call collision on LCN %d!\n",
			card->devname, new_lcn);
Linus Torvalds's avatar
Linus Torvalds committed
3129

Linus Torvalds's avatar
Linus Torvalds committed
3130 3131 3132 3133 3134
		x25_clear_call(card, new_lcn, 0, 0);
		return 1;
	}

	/* Make sure D bit is not set in call request */
Linus Torvalds's avatar
Linus Torvalds committed
3135 3136 3137 3138 3139 3140 3141 3142 3143 3144
//FIXME: THIS IS NOT TURE !!!! TAKE IT OUT
//	if (mb->cmd.qdm & 0x02)
//	{
//		printk(KERN_INFO
//			"%s: X.25 incoming call on LCN %d with D-bit set!\n",
//			card->devname, new_lcn);
//
//		x25_clear_call(card, new_lcn, 0, 0);
//		return 1;
//	}
Linus Torvalds's avatar
Linus Torvalds committed
3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155

	/* Parse call request data */
	info = kmalloc(sizeof(x25_call_info_t), GFP_ATOMIC);
	if (info == NULL)
	{
		printk(KERN_ERR
			"%s: not enough memory to parse X.25 incoming call "
			"on LCN %d!\n", card->devname, new_lcn);
		x25_clear_call(card, new_lcn, 0, 0);
		return 1;
	}
Linus Torvalds's avatar
Linus Torvalds committed
3156
 
Linus Torvalds's avatar
Linus Torvalds committed
3157
	parse_call_info(mb->data, info);
Linus Torvalds's avatar
Linus Torvalds committed
3158 3159 3160 3161 3162 3163 3164 3165 3166

	if (card->u.x.logging)
		printk(KERN_INFO "\n%s: X.25 incoming call on LCN %d!\n",
			card->devname, new_lcn);

	/* Conver the first two ASCII characters into an
         * interger. Used to check the incoming protocol 
         */
	user_data = hex_to_uint(info->user,2);
Linus Torvalds's avatar
Linus Torvalds committed
3167 3168

	/* Find available channel */
3169
	for (dev = wandev->dev; dev; dev = *((struct net_device **)dev->priv)) {
Linus Torvalds's avatar
Linus Torvalds committed
3170 3171
		chan = dev->priv;

Linus Torvalds's avatar
Linus Torvalds committed
3172
		if (chan->common.usedby == API)
Linus Torvalds's avatar
Linus Torvalds committed
3173
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188

		if (!chan->common.svc || (chan->common.state != WAN_DISCONNECTED))
			continue;

		if (user_data == NLPID_IP && chan->protocol != htons(ETH_P_IP)){
			printk(KERN_INFO "IP packet but configured for IPX : %x, %x\n",
				       htons(chan->protocol), info->user[0]);
			continue;
		}
	
		if (user_data == NLPID_SNAP && chan->protocol != htons(ETH_P_IPX)){
			printk(KERN_INFO "IPX packet but configured for IP: %x\n",
				       htons(chan->protocol));
			continue;
		}
Linus Torvalds's avatar
Linus Torvalds committed
3189 3190
		if (strcmp(info->src, chan->addr) == 0)
			break;
Linus Torvalds's avatar
Linus Torvalds committed
3191

Linus Torvalds's avatar
Linus Torvalds committed
3192 3193 3194 3195 3196
	        /* If just an '@' is specified, accept all incoming calls */
	        if (strcmp(chan->addr, "") == 0)
	                break;
	}

Linus Torvalds's avatar
Linus Torvalds committed
3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216
	if (dev == NULL){

		/* If the call is not for any WANPIPE interfaces
                 * check to see if there is an API listening queue
                 * waiting for data. If there is send the packet
                 * up the stack.
                 */
		if (card->sk != NULL && card->func != NULL){
			if (api_incoming_call(card,mb,new_lcn)){
				x25_clear_call(card, new_lcn, 0, 0);
			}
			accept = 0;
		}else{
			printk(KERN_INFO "%s: no channels available!\n",
				card->devname);
			
			x25_clear_call(card, new_lcn, 0, 0);
		}

	}else if (info->nuser == 0){
Linus Torvalds's avatar
Linus Torvalds committed
3217 3218 3219

		printk(KERN_INFO
			"%s: no user data in incoming call on LCN %d!\n",
Linus Torvalds's avatar
Linus Torvalds committed
3220 3221
			card->devname, new_lcn)
		;
Linus Torvalds's avatar
Linus Torvalds committed
3222
		x25_clear_call(card, new_lcn, 0, 0);
Linus Torvalds's avatar
Linus Torvalds committed
3223 3224 3225

	}else switch (info->user[0]){

Linus Torvalds's avatar
Linus Torvalds committed
3226
		case 0:		/* multiplexed */
Linus Torvalds's avatar
Linus Torvalds committed
3227
			chan->protocol = htons(0);
Linus Torvalds's avatar
Linus Torvalds committed
3228 3229 3230 3231 3232 3233 3234 3235 3236 3237
			accept = 1;
			break;

		case NLPID_IP:	/* IP datagrams */
			accept = 1;
			break;

		case NLPID_SNAP: /* IPX datagrams */
			accept = 1;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
3238

Linus Torvalds's avatar
Linus Torvalds committed
3239 3240 3241 3242 3243 3244
		default:
			printk(KERN_INFO
				"%s: unsupported NLPID 0x%02X in incoming call "
				"on LCN %d!\n", card->devname, info->user[0], new_lcn);
			x25_clear_call(card, new_lcn, 0, 249);
	}
Linus Torvalds's avatar
Linus Torvalds committed
3245 3246
	
	if (accept && (x25_accept_call(card, new_lcn, 0) == CMD_OK)){
Linus Torvalds's avatar
Linus Torvalds committed
3247

Linus Torvalds's avatar
Linus Torvalds committed
3248 3249
		bind_lcn_to_dev (card, chan->dev, new_lcn);
		
Linus Torvalds's avatar
Linus Torvalds committed
3250 3251
		if (x25_get_chan_conf(card, chan) == CMD_OK)
			set_chan_state(dev, WAN_CONNECTED);
Linus Torvalds's avatar
Linus Torvalds committed
3252
		else 
Linus Torvalds's avatar
Linus Torvalds committed
3253 3254 3255 3256 3257 3258
			x25_clear_call(card, new_lcn, 0, 0);
	}
	kfree(info);
	return 1;
}

Linus Torvalds's avatar
Linus Torvalds committed
3259 3260 3261
/*====================================================================
 * 	Handle accepted call.
 *====================================================================*/
Linus Torvalds's avatar
Linus Torvalds committed
3262 3263 3264 3265

static int call_accepted (sdla_t* card, int cmd, int lcn, TX25Mbox* mb)
{
	unsigned new_lcn = mb->cmd.lcn;
3266
	struct net_device* dev = find_channel(card, new_lcn);
Linus Torvalds's avatar
Linus Torvalds committed
3267 3268
	x25_channel_t* chan;

Linus Torvalds's avatar
Linus Torvalds committed
3269
	if (dev == NULL){
Linus Torvalds's avatar
Linus Torvalds committed
3270 3271 3272 3273 3274 3275 3276
		printk(KERN_INFO
			"%s: clearing orphaned connection on LCN %d!\n",
			card->devname, new_lcn);
		x25_clear_call(card, new_lcn, 0, 0);
		return 1;
	}

Linus Torvalds's avatar
Linus Torvalds committed
3277 3278 3279 3280
	if (card->u.x.logging)	
		printk(KERN_INFO "%s: X.25 call accepted on Dev %s and LCN %d!\n",
			card->devname, dev->name, new_lcn);

Linus Torvalds's avatar
Linus Torvalds committed
3281 3282 3283 3284 3285 3286 3287
	/* Get channel configuration and notify router */
	chan = dev->priv;
	if (x25_get_chan_conf(card, chan) != CMD_OK)
	{
		x25_clear_call(card, new_lcn, 0, 0);
		return 1;
	}
Linus Torvalds's avatar
Linus Torvalds committed
3288

Linus Torvalds's avatar
Linus Torvalds committed
3289
	set_chan_state(dev, WAN_CONNECTED);
Linus Torvalds's avatar
Linus Torvalds committed
3290 3291 3292 3293 3294 3295

	if (chan->common.usedby == API){
		send_delayed_cmd_result(card,dev,mb);
		bind_lcn_to_dev (card, dev, new_lcn);
	}

Linus Torvalds's avatar
Linus Torvalds committed
3296 3297 3298
	return 1;
}

Linus Torvalds's avatar
Linus Torvalds committed
3299 3300 3301
/*====================================================================
 * 	Handle cleared call.
 *====================================================================*/
Linus Torvalds's avatar
Linus Torvalds committed
3302 3303 3304 3305

static int call_cleared (sdla_t* card, int cmd, int lcn, TX25Mbox* mb)
{
	unsigned new_lcn = mb->cmd.lcn;
3306
	struct net_device* dev = find_channel(card, new_lcn);
Linus Torvalds's avatar
Linus Torvalds committed
3307 3308
	x25_channel_t *chan;
	unsigned char old_state;
Linus Torvalds's avatar
Linus Torvalds committed
3309

Linus Torvalds's avatar
Linus Torvalds committed
3310 3311
	if (card->u.x.logging){
		printk(KERN_INFO "%s: X.25 clear request on LCN %d! Cause:0x%02X "
Linus Torvalds's avatar
Linus Torvalds committed
3312 3313
		"Diagn:0x%02X\n",
		card->devname, new_lcn, mb->cmd.cause, mb->cmd.diagn);
Linus Torvalds's avatar
Linus Torvalds committed
3314 3315 3316 3317 3318
	}

	if (dev == NULL){ 
		printk(KERN_INFO "%s: X.25 clear request : No device for clear\n",
				card->devname);
Linus Torvalds's avatar
Linus Torvalds committed
3319
		return 1;
Linus Torvalds's avatar
Linus Torvalds committed
3320 3321 3322 3323 3324 3325
	}

	chan=dev->priv;

	old_state = chan->common.state;

Linus Torvalds's avatar
Linus Torvalds committed
3326
	set_chan_state(dev, WAN_DISCONNECTED);
Linus Torvalds's avatar
Linus Torvalds committed
3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340

	if (chan->common.usedby == API){

		switch (old_state){
		
		case WAN_CONNECTING:
			send_delayed_cmd_result(card,dev,mb);
			break;
		case WAN_CONNECTED:
			send_oob_msg(card,dev,mb);				
			break;
		}
	}
	
Linus Torvalds's avatar
Linus Torvalds committed
3341 3342 3343
	return ((cmd == X25_WRITE) && (lcn == new_lcn)) ? 0 : 1;
}

Linus Torvalds's avatar
Linus Torvalds committed
3344 3345 3346 3347
/*====================================================================
 * 	Handle X.25 restart event.
 *====================================================================*/

Linus Torvalds's avatar
Linus Torvalds committed
3348 3349
static int restart_event (sdla_t* card, int cmd, int lcn, TX25Mbox* mb)
{
3350
	struct wan_device* wandev = &card->wandev;
3351
	struct net_device* dev;
Linus Torvalds's avatar
Linus Torvalds committed
3352 3353
	x25_channel_t *chan;
	unsigned char old_state;
Linus Torvalds's avatar
Linus Torvalds committed
3354 3355 3356 3357 3358 3359

	printk(KERN_INFO
		"%s: X.25 restart request! Cause:0x%02X Diagn:0x%02X\n",
		card->devname, mb->cmd.cause, mb->cmd.diagn);

	/* down all logical channels */
3360
	for (dev = wandev->dev; dev; dev = *((struct net_device **)dev->priv)) {
Linus Torvalds's avatar
Linus Torvalds committed
3361 3362
		chan=dev->priv;
		old_state = chan->common.state;
Linus Torvalds's avatar
Linus Torvalds committed
3363 3364 3365

		set_chan_state(dev, WAN_DISCONNECTED);

Linus Torvalds's avatar
Linus Torvalds committed
3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377
		if (chan->common.usedby == API){
			switch (old_state){
		
			case WAN_CONNECTING:
				send_delayed_cmd_result(card,dev,mb);
				break;
			case WAN_CONNECTED:
				send_oob_msg(card,dev,mb);				
				break;
			}
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
3378 3379 3380
	return (cmd == X25_WRITE) ? 0 : 1;
}

Linus Torvalds's avatar
Linus Torvalds committed
3381
/*====================================================================
Linus Torvalds's avatar
Linus Torvalds committed
3382
 * Handle timeout event.
Linus Torvalds's avatar
Linus Torvalds committed
3383 3384
 *====================================================================*/

Linus Torvalds's avatar
Linus Torvalds committed
3385 3386 3387 3388 3389 3390
static int timeout_event (sdla_t* card, int cmd, int lcn, TX25Mbox* mb)
{
	unsigned new_lcn = mb->cmd.lcn;

	if (mb->cmd.pktType == 0x05)	/* call request time out */
	{
3391
		struct net_device* dev = find_channel(card,new_lcn);
Linus Torvalds's avatar
Linus Torvalds committed
3392 3393 3394

		printk(KERN_INFO "%s: X.25 call timed timeout on LCN %d!\n",
			card->devname, new_lcn);
Linus Torvalds's avatar
Linus Torvalds committed
3395 3396 3397

		if (dev){
			x25_channel_t *chan = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
3398
			set_chan_state(dev, WAN_DISCONNECTED);
Linus Torvalds's avatar
Linus Torvalds committed
3399 3400 3401 3402 3403 3404 3405

			if (chan->common.usedby == API){
				send_delayed_cmd_result(card,dev,card->mbox);
			}
		}
	}else{ 
		printk(KERN_INFO "%s: X.25 packet 0x%02X timeout on LCN %d!\n",
Linus Torvalds's avatar
Linus Torvalds committed
3406
		card->devname, mb->cmd.pktType, new_lcn);
Linus Torvalds's avatar
Linus Torvalds committed
3407
	}
Linus Torvalds's avatar
Linus Torvalds committed
3408 3409 3410
	return 1;
}

Linus Torvalds's avatar
Linus Torvalds committed
3411 3412 3413
/* 
 *	Miscellaneous 
 */
Linus Torvalds's avatar
Linus Torvalds committed
3414

Linus Torvalds's avatar
Linus Torvalds committed
3415 3416 3417
/*====================================================================
 * 	Establish physical connection.
 * 	o open HDLC and raise DTR
Linus Torvalds's avatar
Linus Torvalds committed
3418
 *
Linus Torvalds's avatar
Linus Torvalds committed
3419 3420 3421 3422 3423
 * 	Return:		0	connection established
 *			1	connection is in progress
 *			<0	error
 *===================================================================*/

Linus Torvalds's avatar
Linus Torvalds committed
3424 3425
static int connect (sdla_t* card)
{
Linus Torvalds's avatar
Linus Torvalds committed
3426 3427
	TX25Status* status = card->flags;

Linus Torvalds's avatar
Linus Torvalds committed
3428 3429
	if (x25_open_hdlc(card) || x25_setup_hdlc(card))
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
3430

Linus Torvalds's avatar
Linus Torvalds committed
3431
	wanpipe_set_state(card, WAN_CONNECTING);
Linus Torvalds's avatar
Linus Torvalds committed
3432 3433 3434 3435

	x25_set_intr_mode(card, INTR_ON_TIMER); 
	status->imask &= ~INTR_ON_TIMER;

Linus Torvalds's avatar
Linus Torvalds committed
3436 3437 3438
	return 1;
}

Linus Torvalds's avatar
Linus Torvalds committed
3439 3440 3441 3442
/*
 * 	Tear down physical connection.
 * 	o close HDLC link
 * 	o drop DTR
Linus Torvalds's avatar
Linus Torvalds committed
3443
 *
Linus Torvalds's avatar
Linus Torvalds committed
3444 3445
 * 	Return:		0
 *			<0	error
Linus Torvalds's avatar
Linus Torvalds committed
3446
 */
Linus Torvalds's avatar
Linus Torvalds committed
3447

Linus Torvalds's avatar
Linus Torvalds committed
3448 3449 3450
static int disconnect (sdla_t* card)
{
	wanpipe_set_state(card, WAN_DISCONNECTED);
Linus Torvalds's avatar
Linus Torvalds committed
3451 3452 3453
	x25_set_intr_mode(card, INTR_ON_TIMER);	/* disable all interrupt except timer */
	x25_close_hdlc(card);			/* close HDLC link */
	x25_set_dtr(card, 0);			/* drop DTR */
Linus Torvalds's avatar
Linus Torvalds committed
3454 3455 3456
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
3457
/*
Linus Torvalds's avatar
Linus Torvalds committed
3458 3459
 * Find network device by its channel number.
 */
Linus Torvalds's avatar
Linus Torvalds committed
3460

3461 3462
static struct net_device* get_dev_by_lcn(struct wan_device* wandev,
					 unsigned lcn)
Linus Torvalds's avatar
Linus Torvalds committed
3463
{
3464
	struct net_device* dev;
Linus Torvalds's avatar
Linus Torvalds committed
3465

3466
	for (dev = wandev->dev; dev; dev = *((struct net_device **)dev->priv))
Linus Torvalds's avatar
Linus Torvalds committed
3467
		if (((x25_channel_t*)dev->priv)->common.lcn == lcn) 
Linus Torvalds's avatar
Linus Torvalds committed
3468 3469 3470 3471
			break;
	return dev;
}

Linus Torvalds's avatar
Linus Torvalds committed
3472 3473 3474 3475
/*
 * 	Initiate connection on the logical channel.
 * 	o for PVC we just get channel configuration
 * 	o for SVCs place an X.25 call
Linus Torvalds's avatar
Linus Torvalds committed
3476
 *
Linus Torvalds's avatar
Linus Torvalds committed
3477 3478 3479
 * 	Return:		0	connected
 *			>0	connection in progress
 *			<0	failure
Linus Torvalds's avatar
Linus Torvalds committed
3480
 */
Linus Torvalds's avatar
Linus Torvalds committed
3481

3482
static int chan_connect(struct net_device* dev)
Linus Torvalds's avatar
Linus Torvalds committed
3483 3484 3485 3486
{
	x25_channel_t* chan = dev->priv;
	sdla_t* card = chan->card;

Linus Torvalds's avatar
Linus Torvalds committed
3487 3488 3489 3490
	if (chan->common.svc && chan->common.usedby == WANPIPE){
		if (!chan->addr[0]){
			printk(KERN_INFO "%s: No Destination Address\n",
					card->devname);
Linus Torvalds's avatar
Linus Torvalds committed
3491
			return -EINVAL;	/* no destination address */
Linus Torvalds's avatar
Linus Torvalds committed
3492
		}
Linus Torvalds's avatar
Linus Torvalds committed
3493 3494
		printk(KERN_INFO "%s: placing X.25 call to %s ...\n",
			card->devname, chan->addr);
Linus Torvalds's avatar
Linus Torvalds committed
3495

Linus Torvalds's avatar
Linus Torvalds committed
3496 3497
		if (x25_place_call(card, chan) != CMD_OK)
			return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
3498

Linus Torvalds's avatar
Linus Torvalds committed
3499 3500
		set_chan_state(dev, WAN_CONNECTING);
		return 1;
Linus Torvalds's avatar
Linus Torvalds committed
3501
	}else{
Linus Torvalds's avatar
Linus Torvalds committed
3502 3503
		if (x25_get_chan_conf(card, chan) != CMD_OK)
			return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
3504

Linus Torvalds's avatar
Linus Torvalds committed
3505 3506 3507 3508 3509
		set_chan_state(dev, WAN_CONNECTED);
	}
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
3510 3511 3512
/*
 * 	Disconnect logical channel.
 * 	o if SVC then clear X.25 call
Linus Torvalds's avatar
Linus Torvalds committed
3513
 */
Linus Torvalds's avatar
Linus Torvalds committed
3514

3515
static int chan_disc(struct net_device* dev)
Linus Torvalds's avatar
Linus Torvalds committed
3516 3517 3518
{
	x25_channel_t* chan = dev->priv;

Linus Torvalds's avatar
Linus Torvalds committed
3519 3520 3521 3522 3523 3524 3525 3526 3527 3528
	if (chan->common.svc){ 
		x25_clear_call(chan->card, chan->common.lcn, 0, 0);

		/* For API we disconnect on clear
                 * confirmation. 
                 */
		if (chan->common.usedby == API)
			return 0;
	}

Linus Torvalds's avatar
Linus Torvalds committed
3529
	set_chan_state(dev, WAN_DISCONNECTED);
Linus Torvalds's avatar
Linus Torvalds committed
3530
	
Linus Torvalds's avatar
Linus Torvalds committed
3531 3532 3533
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
3534 3535
/*
 * 	Set logical channel state.
Linus Torvalds's avatar
Linus Torvalds committed
3536
 */
Linus Torvalds's avatar
Linus Torvalds committed
3537

3538
static void set_chan_state(struct net_device* dev, int state)
Linus Torvalds's avatar
Linus Torvalds committed
3539 3540 3541 3542 3543 3544 3545
{
	x25_channel_t* chan = dev->priv;
	sdla_t* card = chan->card;
	unsigned long flags;

	save_flags(flags);
	cli();
Linus Torvalds's avatar
Linus Torvalds committed
3546
	if (chan->common.state != state)
Linus Torvalds's avatar
Linus Torvalds committed
3547 3548 3549 3550
	{
		switch (state)
		{
			case WAN_CONNECTED:
Linus Torvalds's avatar
Linus Torvalds committed
3551 3552 3553 3554 3555 3556
				if (card->u.x.logging){
					printk (KERN_INFO 
						"%s: interface %s connected, lcn %i !\n", 
						card->devname, dev->name,chan->common.lcn);
				}
				*(unsigned short*)dev->dev_addr = htons(chan->common.lcn);
Linus Torvalds's avatar
Linus Torvalds committed
3557
				chan->i_timeout_sofar = jiffies;
Linus Torvalds's avatar
Linus Torvalds committed
3558 3559 3560 3561

				/* LAPB is PVC Based */
				if (card->u.x.LAPB_hdlc)
					chan->common.svc=0;
Linus Torvalds's avatar
Linus Torvalds committed
3562 3563 3564
				break;

			case WAN_CONNECTING:
Linus Torvalds's avatar
Linus Torvalds committed
3565 3566 3567 3568 3569
				if (card->u.x.logging){
					printk (KERN_INFO 
						"%s: interface %s connecting, lcn %i ...\n", 
						card->devname, dev->name, chan->common.lcn);
				}
Linus Torvalds's avatar
Linus Torvalds committed
3570 3571 3572
				break;

			case WAN_DISCONNECTED:
Linus Torvalds's avatar
Linus Torvalds committed
3573 3574 3575 3576 3577 3578 3579 3580
				if (card->u.x.logging){
					printk (KERN_INFO 
						"%s: interface %s disconnected, lcn %i !\n", 
						card->devname, dev->name,chan->common.lcn);
				}
				atomic_set(&chan->common.disconnect,0);
				
				if (chan->common.svc) {
Linus Torvalds's avatar
Linus Torvalds committed
3581
					*(unsigned short*)dev->dev_addr = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3582 3583
					card->u.x.svc_to_dev_map[(chan->common.lcn%X25_MAX_CHAN)]=NULL;
		                	chan->common.lcn = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3584
				}
Linus Torvalds's avatar
Linus Torvalds committed
3585 3586 3587 3588 3589

				if (chan->transmit_length){
					chan->transmit_length=0;
					atomic_set(&chan->common.driver_busy,0);
					chan->tx_offset=0;
3590 3591
					if (netif_queue_stopped(dev)){
						netif_wake_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603
					}
				}
				atomic_set(&chan->common.command,0);
				break;

			case WAN_DISCONNECTING:
				if (card->u.x.logging){
					printk (KERN_INFO 
					"\n%s: interface %s disconnecting, lcn %i ...\n", 
					card->devname, dev->name,chan->common.lcn);
				}
				atomic_set(&chan->common.disconnect,0);
Linus Torvalds's avatar
Linus Torvalds committed
3604 3605
				break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
3606
		chan->common.state = state;
Linus Torvalds's avatar
Linus Torvalds committed
3607 3608 3609 3610 3611
	}
	chan->state_tick = jiffies;
	restore_flags(flags);
}

Linus Torvalds's avatar
Linus Torvalds committed
3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625
/*
 * 	Send packet on a logical channel.
 *		When this function is called, tx_skb field of the channel data 
 *		space points to the transmit socket buffer.  When transmission 
 *		is complete, release socket buffer and reset 'tbusy' flag.
 *
 * 	Return:		0	- transmission complete
 *			1	- busy
 *
 * 	Notes:
 * 	1. If packet length is greater than MTU for this channel, we'll fragment
 *    	the packet into 'complete sequence' using M-bit.
 * 	2. When transmission is complete, an event notification should be issued
 *    	to the router.
Linus Torvalds's avatar
Linus Torvalds committed
3626
 */
Linus Torvalds's avatar
Linus Torvalds committed
3627

3628 3629
static int chan_send(struct net_device* dev, void* buff, unsigned data_len,
		     unsigned char tx_intr)
Linus Torvalds's avatar
Linus Torvalds committed
3630 3631 3632 3633
{
	x25_channel_t* chan = dev->priv;
	sdla_t* card = chan->card;
	TX25Status* status = card->flags;
Linus Torvalds's avatar
Linus Torvalds committed
3634 3635
	unsigned len=0, qdm=0, res=0, orig_len = 0;
	void *data;
Linus Torvalds's avatar
Linus Torvalds committed
3636 3637

	/* Check to see if channel is ready */
Linus Torvalds's avatar
Linus Torvalds committed
3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652
	if ((!(status->cflags[chan->ch_idx] & 0x40) && !card->u.x.LAPB_hdlc)  || 
             !(*card->u.x.hdlc_buf_status & 0x40)){ 
            
		if (!tx_intr){
			setup_for_delayed_transmit (dev, buff, data_len);
			return 0;
		}else{
			/* By returning 0 to tx_intr the packet will be dropped */
			++card->wandev.stats.tx_dropped;
			++chan->ifstats.tx_dropped;
			printk(KERN_INFO "%s: ERROR, Tx intr could not send, dropping %s:\n", 
				card->devname,dev->name);
			++chan->if_send_stat.if_send_bfr_not_passed_to_adptr;
			return 0;
		}
Linus Torvalds's avatar
Linus Torvalds committed
3653
	}
Linus Torvalds's avatar
Linus Torvalds committed
3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678

	if (chan->common.usedby == API){
		/* Remove the API Header */
		x25api_hdr_t *api_data = (x25api_hdr_t *)buff;

		/* Set the qdm bits from the packet header 
                 * User has the option to set the qdm bits
                 */
		qdm = api_data->qdm;

		orig_len = len = data_len - sizeof(x25api_hdr_t);
		data = (unsigned char*)buff + sizeof(x25api_hdr_t);
	}else{
		data = buff;
		orig_len = len = data_len;
	}	

	if (tx_intr){
		/* We are in tx_intr, minus the tx_offset from 
                 * the total length. The tx_offset part of the
		 * data has already been sent. Also, move the 
		 * data pointer to proper offset location.
                 */
		len -= chan->tx_offset;
		data = (unsigned char*)data + chan->tx_offset;
Linus Torvalds's avatar
Linus Torvalds committed
3679
	}
Linus Torvalds's avatar
Linus Torvalds committed
3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695
		
	/* Check if the packet length is greater than MTU
         * If YES: Cut the len to MTU and set the M bit 
         */
	if (len > chan->tx_pkt_size && !card->u.x.LAPB_hdlc){
		len = chan->tx_pkt_size;
		qdm |= M_BIT;		
	} 


	/* Pass only first three bits of the qdm byte to the send
         * routine. In case user sets any other bit which might
         * cause errors. 
         */

	switch(x25_send(card, chan->common.lcn, (qdm&0x07), len, data)){
Linus Torvalds's avatar
Linus Torvalds committed
3696 3697
		case 0x00:	/* success */
			chan->i_timeout_sofar = jiffies;
Linus Torvalds's avatar
Linus Torvalds committed
3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746

			dev->trans_start=jiffies;
			
			if ((qdm & M_BIT) && !card->u.x.LAPB_hdlc){
				if (!tx_intr){
					/* The M bit was set, which means that part of the
                                         * packet has been sent. Copy the packet into a buffer
				         * and set the offset to len, so on next tx_inter 
					 * the packet will be sent using the below offset.
					 */
					chan->tx_offset += len;

					++chan->ifstats.tx_packets;
					chan->ifstats.tx_bytes += len;
					
					if (chan->tx_offset < orig_len){
						setup_for_delayed_transmit (dev, buff, data_len);
					}
					res=0;
				}else{
					/* We are already in tx_inter, thus data is already
                                         * in the buffer. Update the offset and wait for
                                         * next tx_intr. We add on to the offset, since data can
                                         * be X number of times larger than max data size.
					 */
					++chan->ifstats.tx_packets;
					chan->ifstats.tx_bytes += len;
					
					++chan->if_send_stat.if_send_bfr_passed_to_adptr;
					chan->tx_offset += len;

					/* The user can set the qdm bit as well.
                                         * If the entire packet was sent and qdm is still
                                         * set, than it's the user who has set the M bit. In that,
                                         * case indicate that the packet was send by returning 
					 * 0 and wait for a new packet. Otherwise, wait for next
                                         * tx interrupt to send the rest of the packet */

					if (chan->tx_offset < orig_len){
						res=1;
					}else{	
						res=0;
					}
				}
			}else{
				++chan->ifstats.tx_packets;
				chan->ifstats.tx_bytes += len;
				++chan->if_send_stat.if_send_bfr_passed_to_adptr;
				res=0;
Linus Torvalds's avatar
Linus Torvalds committed
3747 3748 3749 3750
			}
			break;

		case 0x33:	/* Tx busy */
Linus Torvalds's avatar
Linus Torvalds committed
3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765
			if (tx_intr){
				printk(KERN_INFO "%s: Tx_intr: Big Error dropping packet %s\n",
						card->devname,dev->name);
				++chan->ifstats.tx_dropped;
				++card->wandev.stats.tx_dropped;
				++chan->if_send_stat.if_send_bfr_not_passed_to_adptr;
				res=0;
			}else{
				DBG_PRINTK(KERN_INFO 
					"%s: Send: Big Error should have tx: storring %s\n",
						card->devname,dev->name);
				setup_for_delayed_transmit (dev, buff, data_len);	
				res=1;
			}
			break;
Linus Torvalds's avatar
Linus Torvalds committed
3766 3767 3768

		default:	/* failure */
			++chan->ifstats.tx_errors;
Linus Torvalds's avatar
Linus Torvalds committed
3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782
			if (tx_intr){
				printk(KERN_INFO "%s: Tx_intr: Failure to send, dropping %s\n",
					card->devname,dev->name);
				++chan->ifstats.tx_dropped;
				++card->wandev.stats.tx_dropped;
				++chan->if_send_stat.if_send_bfr_not_passed_to_adptr;
				res=0;
			}else{
				DBG_PRINTK(KERN_INFO "%s: Send: Failure to send !!!, storing %s\n",
					card->devname,dev->name);			
				setup_for_delayed_transmit (dev, buff, data_len);
				res=1;
			}
			break;	
Linus Torvalds's avatar
Linus Torvalds committed
3783
	}
Linus Torvalds's avatar
Linus Torvalds committed
3784
	return res;
Linus Torvalds's avatar
Linus Torvalds committed
3785 3786
}

Linus Torvalds's avatar
Linus Torvalds committed
3787 3788 3789

/*
 * 	Parse X.25 call request data and fill x25_call_info_t structure.
Linus Torvalds's avatar
Linus Torvalds committed
3790 3791 3792 3793 3794 3795 3796 3797
 */

static void parse_call_info (unsigned char* str, x25_call_info_t* info)
{
	memset(info, 0, sizeof(x25_call_info_t));
	for (; *str; ++str)
	{
		int i;
Linus Torvalds's avatar
Linus Torvalds committed
3798 3799 3800 3801 3802 3803 3804 3805
		unsigned char ch;

		if (*str == '-') switch (str[1]) {

			/* Take minus 2 off the maximum size so that 
                         * last byte is 0. This way we can use string
                         * manipulaton functions on call information.
                         */
Linus Torvalds's avatar
Linus Torvalds committed
3806 3807

			case 'd':	/* destination address */
Linus Torvalds's avatar
Linus Torvalds committed
3808
				for (i = 0; i < (MAX_X25_ADDR_SIZE-2); ++i){
Linus Torvalds's avatar
Linus Torvalds committed
3809
					ch = str[2+i];
Linus Torvalds's avatar
Linus Torvalds committed
3810
					if (isspace(ch)) break;
Linus Torvalds's avatar
Linus Torvalds committed
3811 3812 3813
					info->dest[i] = ch;
				}
				break;
Linus Torvalds's avatar
Linus Torvalds committed
3814

Linus Torvalds's avatar
Linus Torvalds committed
3815
			case 's':	/* source address */
Linus Torvalds's avatar
Linus Torvalds committed
3816
				for (i = 0; i < (MAX_X25_ADDR_SIZE-2); ++i){
Linus Torvalds's avatar
Linus Torvalds committed
3817
					ch = str[2+i];
Linus Torvalds's avatar
Linus Torvalds committed
3818
					if (isspace(ch)) break;
Linus Torvalds's avatar
Linus Torvalds committed
3819 3820 3821 3822 3823
					info->src[i] = ch;
				}
				break;

			case 'u':	/* user data */
Linus Torvalds's avatar
Linus Torvalds committed
3824 3825 3826 3827
				for (i = 0; i < (MAX_X25_DATA_SIZE-2); ++i){
					ch = str[2+i];
					if (isspace(ch)) break;
					info->user[i] = ch; 
Linus Torvalds's avatar
Linus Torvalds committed
3828 3829 3830 3831 3832
				}
				info->nuser = i;
				break;

			case 'f':	/* facilities */
Linus Torvalds's avatar
Linus Torvalds committed
3833 3834 3835 3836
				for (i = 0; i < (MAX_X25_FACL_SIZE-2); ++i){
					ch = str[2+i];
					if (isspace(ch)) break;
					info->facil[i] = ch;
Linus Torvalds's avatar
Linus Torvalds committed
3837 3838 3839 3840 3841 3842 3843
				}
				info->nfacil = i;
				break;
		}
	}
}

Linus Torvalds's avatar
Linus Torvalds committed
3844 3845
/*
 * 	Convert line speed in bps to a number used by S502 code.
Linus Torvalds's avatar
Linus Torvalds committed
3846
 */
Linus Torvalds's avatar
Linus Torvalds committed
3847

Linus Torvalds's avatar
Linus Torvalds committed
3848 3849 3850 3851
static unsigned char bps_to_speed_code (unsigned long bps)
{
	unsigned char	number;

Linus Torvalds's avatar
Linus Torvalds committed
3852
	if (bps <= 1200)        number = 0x01;
Linus Torvalds's avatar
Linus Torvalds committed
3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868
	else if (bps <= 2400)   number = 0x02;
	else if (bps <= 4800)   number = 0x03;
	else if (bps <= 9600)   number = 0x04;
	else if (bps <= 19200)  number = 0x05;
	else if (bps <= 38400)  number = 0x06;
	else if (bps <= 45000)  number = 0x07;
	else if (bps <= 56000)  number = 0x08;
	else if (bps <= 64000)  number = 0x09;
	else if (bps <= 74000)  number = 0x0A;
	else if (bps <= 112000) number = 0x0B;
	else if (bps <= 128000) number = 0x0C;
	else number = 0x0D;

	return number;
}

Linus Torvalds's avatar
Linus Torvalds committed
3869 3870 3871
/*
 * 	Convert decimal string to unsigned integer.
 * 	If len != 0 then only 'len' characters of the string are converted.
Linus Torvalds's avatar
Linus Torvalds committed
3872
 */
Linus Torvalds's avatar
Linus Torvalds committed
3873

Linus Torvalds's avatar
Linus Torvalds committed
3874 3875 3876 3877
static unsigned int dec_to_uint (unsigned char* str, int len)
{
	unsigned val;

Linus Torvalds's avatar
Linus Torvalds committed
3878 3879 3880
	if (!len) 
		len = strlen(str);

Linus Torvalds's avatar
Linus Torvalds committed
3881 3882
	for (val = 0; len && is_digit(*str); ++str, --len)
		val = (val * 10) + (*str - (unsigned)'0');
Linus Torvalds's avatar
Linus Torvalds committed
3883
	
Linus Torvalds's avatar
Linus Torvalds committed
3884 3885 3886
	return val;
}

Linus Torvalds's avatar
Linus Torvalds committed
3887 3888 3889
/*
 * 	Convert hex string to unsigned integer.
 * 	If len != 0 then only 'len' characters of the string are conferted.
Linus Torvalds's avatar
Linus Torvalds committed
3890
 */
Linus Torvalds's avatar
Linus Torvalds committed
3891

Linus Torvalds's avatar
Linus Torvalds committed
3892 3893 3894 3895
static unsigned int hex_to_uint (unsigned char* str, int len)
{
	unsigned val, ch;

Linus Torvalds's avatar
Linus Torvalds committed
3896 3897 3898
	if (!len) 
		len = strlen(str);

Linus Torvalds's avatar
Linus Torvalds committed
3899 3900 3901 3902 3903 3904 3905
	for (val = 0; len; ++str, --len)
	{
		ch = *str;
		if (is_digit(ch))
			val = (val << 4) + (ch - (unsigned)'0');
		else if (is_hex_digit(ch))
			val = (val << 4) + ((ch & 0xDF) - (unsigned)'A' + 10);
Linus Torvalds's avatar
Linus Torvalds committed
3906
		else break;
Linus Torvalds's avatar
Linus Torvalds committed
3907 3908 3909 3910 3911 3912 3913 3914 3915
	}
	return val;
}


static int handle_IPXWAN(unsigned char *sendpacket, char *devname, unsigned char enable_IPX, unsigned long network_number, unsigned short proto)
{
	int i;

Linus Torvalds's avatar
Linus Torvalds committed
3916
	if( proto == ETH_P_IPX) {
Linus Torvalds's avatar
Linus Torvalds committed
3917 3918 3919 3920 3921 3922
		/* It's an IPX packet */
		if(!enable_IPX) {
			/* Return 1 so we don't pass it up the stack. */
			return 1;
		}
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
3923
		/* It's not IPX so pass it up the stack.*/ 
Linus Torvalds's avatar
Linus Torvalds committed
3924 3925 3926 3927 3928 3929
		return 0;
	}

	if( sendpacket[16] == 0x90 &&
	    sendpacket[17] == 0x04)
	{
Linus Torvalds's avatar
Linus Torvalds committed
3930
		/* It's IPXWAN  */
Linus Torvalds's avatar
Linus Torvalds committed
3931 3932 3933 3934 3935 3936 3937 3938

		if( sendpacket[2] == 0x02 &&
		    sendpacket[34] == 0x00)
		{
			/* It's a timer request packet */
			printk(KERN_INFO "%s: Received IPXWAN Timer Request packet\n",devname);

			/* Go through the routing options and answer no to every
Linus Torvalds's avatar
Linus Torvalds committed
3939 3940
			 * option except Unnumbered RIP/SAP
			 */
Linus Torvalds's avatar
Linus Torvalds committed
3941 3942 3943 3944
			for(i = 41; sendpacket[i] == 0x00; i += 5)
			{
				/* 0x02 is the option for Unnumbered RIP/SAP */
				if( sendpacket[i + 4] != 0x02)
Linus Torvalds's avatar
Linus Torvalds committed
3945
				{
Linus Torvalds's avatar
Linus Torvalds committed
3946
					sendpacket[i + 1] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3947
				}
Linus Torvalds's avatar
Linus Torvalds committed
3948 3949 3950 3951
			}

			/* Skip over the extended Node ID option */
			if( sendpacket[i] == 0x04 )
Linus Torvalds's avatar
Linus Torvalds committed
3952
			{
Linus Torvalds's avatar
Linus Torvalds committed
3953
				i += 8;
Linus Torvalds's avatar
Linus Torvalds committed
3954
			}
Linus Torvalds's avatar
Linus Torvalds committed
3955

Linus Torvalds's avatar
Linus Torvalds committed
3956
			/* We also want to turn off all header compression opt. 			 */ 
Linus Torvalds's avatar
Linus Torvalds committed
3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992
			for(; sendpacket[i] == 0x80 ;)
			{
				sendpacket[i + 1] = 0;
				i += (sendpacket[i + 2] << 8) + (sendpacket[i + 3]) + 4;
			}

			/* Set the packet type to timer response */
			sendpacket[34] = 0x01;

			printk(KERN_INFO "%s: Sending IPXWAN Timer Response\n",devname);
		}
		else if( sendpacket[34] == 0x02 )
		{
			/* This is an information request packet */
			printk(KERN_INFO "%s: Received IPXWAN Information Request packet\n",devname);

			/* Set the packet type to information response */
			sendpacket[34] = 0x03;

			/* Set the router name */
			sendpacket[51] = 'X';
			sendpacket[52] = 'T';
			sendpacket[53] = 'P';
			sendpacket[54] = 'I';
			sendpacket[55] = 'P';
			sendpacket[56] = 'E';
			sendpacket[57] = '-';
			sendpacket[58] = CVHexToAscii(network_number >> 28);
			sendpacket[59] = CVHexToAscii((network_number & 0x0F000000)>> 24);
			sendpacket[60] = CVHexToAscii((network_number & 0x00F00000)>> 20);
			sendpacket[61] = CVHexToAscii((network_number & 0x000F0000)>> 16);
			sendpacket[62] = CVHexToAscii((network_number & 0x0000F000)>> 12);
			sendpacket[63] = CVHexToAscii((network_number & 0x00000F00)>> 8);
			sendpacket[64] = CVHexToAscii((network_number & 0x000000F0)>> 4);
			sendpacket[65] = CVHexToAscii(network_number & 0x0000000F);
			for(i = 66; i < 99; i+= 1)
Linus Torvalds's avatar
Linus Torvalds committed
3993
			{
Linus Torvalds's avatar
Linus Torvalds committed
3994
				sendpacket[i] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3995
			}
Linus Torvalds's avatar
Linus Torvalds committed
3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012

			printk(KERN_INFO "%s: Sending IPXWAN Information Response packet\n",devname);
		}
		else
		{
			printk(KERN_INFO "%s: Unknown IPXWAN packet!\n",devname);
			return 0;
		}

		/* Set the WNodeID to our network address */
		sendpacket[35] = (unsigned char)(network_number >> 24);
		sendpacket[36] = (unsigned char)((network_number & 0x00FF0000) >> 16);
		sendpacket[37] = (unsigned char)((network_number & 0x0000FF00) >> 8);
		sendpacket[38] = (unsigned char)(network_number & 0x000000FF);

		return 1;
	} else {
4013
		/*If we get here it's an IPX-data packet, so it'll get passed up the stack.
Linus Torvalds's avatar
Linus Torvalds committed
4014 4015
		 */
		/* switch the network numbers */
Linus Torvalds's avatar
Linus Torvalds committed
4016 4017 4018 4019 4020 4021
		switch_net_numbers(sendpacket, network_number, 1);	
		return 0;
	}
}

/*
Linus Torvalds's avatar
Linus Torvalds committed
4022 4023 4024
 *  	If incoming is 0 (outgoing)- if the net numbers is ours make it 0
 *  	if incoming is 1 - if the net number is 0 make it ours 
 */
Linus Torvalds's avatar
Linus Torvalds committed
4025 4026 4027 4028 4029 4030 4031 4032

static void switch_net_numbers(unsigned char *sendpacket, unsigned long network_number, unsigned char incoming)
{
	unsigned long pnetwork_number;

	pnetwork_number = (unsigned long)((sendpacket[6] << 24) + 
			  (sendpacket[7] << 16) + (sendpacket[8] << 8) + 
			  sendpacket[9]);
Linus Torvalds's avatar
Linus Torvalds committed
4033
	
Linus Torvalds's avatar
Linus Torvalds committed
4034

Linus Torvalds's avatar
Linus Torvalds committed
4035 4036 4037
	if (!incoming) {
		/*If the destination network number is ours, make it 0 */
		if( pnetwork_number == network_number) {
Linus Torvalds's avatar
Linus Torvalds committed
4038 4039 4040
			sendpacket[6] = sendpacket[7] = sendpacket[8] = 
					 sendpacket[9] = 0x00;
		}
Linus Torvalds's avatar
Linus Torvalds committed
4041
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
4042
		/* If the incoming network is 0, make it ours */
Linus Torvalds's avatar
Linus Torvalds committed
4043
		if( pnetwork_number == 0) {
Linus Torvalds's avatar
Linus Torvalds committed
4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057
			sendpacket[6] = (unsigned char)(network_number >> 24);
			sendpacket[7] = (unsigned char)((network_number & 
					 0x00FF0000) >> 16);
			sendpacket[8] = (unsigned char)((network_number & 
					 0x0000FF00) >> 8);
			sendpacket[9] = (unsigned char)(network_number & 
					 0x000000FF);
		}
	}


	pnetwork_number = (unsigned long)((sendpacket[18] << 24) + 
			  (sendpacket[19] << 16) + (sendpacket[20] << 8) + 
			  sendpacket[21]);
Linus Torvalds's avatar
Linus Torvalds committed
4058 4059 4060
	
	
	if( !incoming ) {
Linus Torvalds's avatar
Linus Torvalds committed
4061
		/* If the source network is ours, make it 0 */
Linus Torvalds's avatar
Linus Torvalds committed
4062
		if( pnetwork_number == network_number) {
Linus Torvalds's avatar
Linus Torvalds committed
4063
			sendpacket[18] = sendpacket[19] = sendpacket[20] = 
Linus Torvalds's avatar
Linus Torvalds committed
4064
				 sendpacket[21] = 0x00;
Linus Torvalds's avatar
Linus Torvalds committed
4065
		}
Linus Torvalds's avatar
Linus Torvalds committed
4066
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
4067
		/* If the source network is 0, make it ours */
Linus Torvalds's avatar
Linus Torvalds committed
4068
		if( pnetwork_number == 0 ) {
Linus Torvalds's avatar
Linus Torvalds committed
4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080
			sendpacket[18] = (unsigned char)(network_number >> 24);
			sendpacket[19] = (unsigned char)((network_number & 
					 0x00FF0000) >> 16);
			sendpacket[20] = (unsigned char)((network_number & 
					 0x0000FF00) >> 8);
			sendpacket[21] = (unsigned char)(network_number & 
					 0x000000FF);
		}
	}
} /* switch_net_numbers */


Linus Torvalds's avatar
Linus Torvalds committed
4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095


/********************* X25API SPECIFIC FUNCTIONS ****************/


/*===============================================================
 *  find_channel
 *
 *	Manages the lcn to device map. It increases performance
 *      because it eliminates the need to search through the link  
 *      list for a device which is bounded to a specific lcn.
 *
 *===============================================================*/


4096
struct net_device *find_channel(sdla_t *card, unsigned lcn)
Linus Torvalds's avatar
Linus Torvalds committed
4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142
{
	if (card->u.x.LAPB_hdlc){

		return card->wandev.dev;

	}else{
		/* We don't know whether the incoming lcn
                 * is a PVC or an SVC channel. But we do know that
                 * the lcn cannot be for both the PVC and the SVC
                 * channel.

		 * If the lcn number is greater or equal to 255, 
                 * take the modulo 255 of that number. We only have
                 * 255 locations, thus higher numbers must be mapped
                 * to a number between 0 and 245. 

		 * We must separate pvc's and svc's since two don't
                 * have to be contiguous.  Meaning pvc's can start
                 * from 1 to 10 and svc's can start from 256 to 266.
                 * But 256%255 is 1, i.e. CONFLICT.
		 */


		/* Highest LCN number must be less or equal to 4096 */
		if ((lcn <= MAX_LCN_NUM) && (lcn > 0)){

			if (lcn < X25_MAX_CHAN){
				if (card->u.x.svc_to_dev_map[lcn])
					return card->u.x.svc_to_dev_map[lcn];

				if (card->u.x.pvc_to_dev_map[lcn])
					return card->u.x.pvc_to_dev_map[lcn];
			
			}else{
				int new_lcn = lcn%X25_MAX_CHAN;
				if (card->u.x.svc_to_dev_map[new_lcn])
					return card->u.x.svc_to_dev_map[new_lcn];

				if (card->u.x.pvc_to_dev_map[new_lcn])
					return card->u.x.pvc_to_dev_map[new_lcn];
			}
		}
		return NULL;
	}
}

4143
void bind_lcn_to_dev(sdla_t *card, struct net_device *dev, unsigned lcn)
Linus Torvalds's avatar
Linus Torvalds committed
4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169
{
	x25_channel_t *chan = dev->priv;

	/* Modulo the lcn number by X25_MAX_CHAN (255)
	 * because the lcn number can be greater than 255 
         *
	 * We need to split svc and pvc since they don't have
         * to be contigous. 
	 */

	if (chan->common.svc){
		card->u.x.svc_to_dev_map[(lcn % X25_MAX_CHAN)] = dev;
	}else{
		card->u.x.pvc_to_dev_map[(lcn % X25_MAX_CHAN)] = dev;
	}
	chan->common.lcn = lcn;
}



/*===============================================================
 * x25api_bh 
 *
 *
 *==============================================================*/

4170
static void x25api_bh(struct net_device* dev)
Linus Torvalds's avatar
Linus Torvalds committed
4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212
{
	x25_channel_t* chan = dev->priv;
	sdla_t* card = chan->card;
	struct sk_buff *skb;

	if (atomic_read(&chan->bh_buff_used) == 0){
		printk(KERN_INFO "%s: BH Buffer Empty in BH\n",
				card->devname);
		clear_bit(0, &chan->tq_working);
		return;
	}

	while (atomic_read(&chan->bh_buff_used)){

		/* If the sock is in the process of unlinking the
		 * driver from the socket, we must get out. 
		 * This never happends but is a sanity check. */
		if (test_bit(0,&chan->common.common_critical)){
			clear_bit(0, &chan->tq_working);
			return;
		}
		
		/* If LAPB HDLC, do not drop packets if socket is
                 * not connected.  Let the buffer fill up and
                 * turn off rx interrupt */
		if (card->u.x.LAPB_hdlc){
			if (chan->common.sk == NULL || chan->common.func == NULL){
				clear_bit(0, &chan->tq_working);			
				return;
			}
		}

		skb  = ((bh_data_t *)&chan->bh_head[chan->bh_read])->skb;

		if (skb == NULL){
			printk(KERN_INFO "%s: BH Skb empty for read %i\n",
					card->devname,chan->bh_read);
		}else{
			
			if (chan->common.sk == NULL || chan->common.func == NULL){
				printk(KERN_INFO "%s: BH: Socket disconnected, dropping\n",
						card->devname);
4213
				dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245
				x25api_bh_cleanup(dev);
				++chan->ifstats.rx_dropped;
				++chan->rx_intr_stat.rx_intr_bfr_not_passed_to_stack;
				continue;
			}


			if (chan->common.func(skb,dev,chan->common.sk) != 0){
				/* Sock full cannot send, queue us for another
                                 * try 
				 */
				printk(KERN_INFO "%s: BH: !!! Packet failed to send !!!!! \n",
						card->devname);
				atomic_set(&chan->common.receive_block,1);
				return;
			}else{
				x25api_bh_cleanup(dev);
				++chan->rx_intr_stat.rx_intr_bfr_passed_to_stack;
			}
		}
	}	
	clear_bit(0, &chan->tq_working);

	return;
}

/*===============================================================
 * x25api_bh_cleanup 
 *
 *
 *==============================================================*/

4246
static int x25api_bh_cleanup(struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284
{
	x25_channel_t* chan = dev->priv;
	sdla_t *card = chan->card;
	TX25Status* status = card->flags;


	((bh_data_t *)&chan->bh_head[chan->bh_read])->skb = NULL;

	if (chan->bh_read == MAX_BH_BUFF){
		chan->bh_read=0;
	}else{
		++chan->bh_read;	
	}

	/* If the Receive interrupt was off, it means
         * that we filled up our circular buffer. Check    
         * that we have space in the buffer. If so 
         * turn the RX interrupt back on. 
	 */
	if (!(status->imask & INTR_ON_RX_FRAME)){
		if (atomic_read(&chan->bh_buff_used) < (MAX_BH_BUFF+1)){
			printk(KERN_INFO "%s: BH: Turning on the interrupt\n",
					card->devname);
			status->imask |= INTR_ON_RX_FRAME;
		}
	}	

	atomic_dec(&chan->bh_buff_used);
	return 0;
}


/*===============================================================
 * bh_enqueue 
 *
 *
 *==============================================================*/

4285
static int bh_enqueue(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324
{
	x25_channel_t* chan = dev->priv;
	sdla_t *card = chan->card;
	TX25Status* status = card->flags;

	if (atomic_read(&chan->bh_buff_used) == (MAX_BH_BUFF+1)){
		printk(KERN_INFO "%s: Bottom half buffer FULL\n",
				card->devname);
		return 1; 
	}

	((bh_data_t *)&chan->bh_head[chan->bh_write])->skb = skb;

	if (chan->bh_write == MAX_BH_BUFF){
		chan->bh_write=0;
	}else{
		++chan->bh_write;
	}

	atomic_inc(&chan->bh_buff_used);

	if (atomic_read(&chan->bh_buff_used) == (MAX_BH_BUFF+1)){
		printk(KERN_INFO "%s: Buffer is now full, Turning off RX Intr\n",
				card->devname);
		status->imask &= ~INTR_ON_RX_FRAME;
	}

	return 0;
}


/*===============================================================
 * timer_intr_cmd_exec
 *  
 *	Called by timer interrupt to execute a command
 *===============================================================*/

static int timer_intr_cmd_exec (sdla_t* card)
{
4325
	struct net_device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451
	unsigned char more_to_exec=0;
	volatile x25_channel_t *chan=NULL;
	int i=0,bad_cmd=0,err=0;	

	if (card->u.x.cmd_dev == NULL){
		card->u.x.cmd_dev = card->wandev.dev;
	}

	dev = card->u.x.cmd_dev;

	for (;;){

		chan = dev->priv;
		
		if (atomic_read(&chan->common.command)){ 

			bad_cmd = check_bad_command(card,dev);

			if ((!chan->common.mbox || atomic_read(&chan->common.disconnect)) && 
			     !bad_cmd){

				/* Socket has died or exited, We must bring the
                                 * channel down before anybody else tries to 
                                 * use it */
				err = channel_disconnect(card,dev);
			}else{
			        err = execute_delayed_cmd(card, dev,
							 (mbox_cmd_t*)chan->common.mbox,
							  bad_cmd);
			}

			switch (err){

			case RETURN_RESULT:

				/* Return the result to the socket without
                                 * delay. NO_WAIT Command */	
				atomic_set(&chan->common.command,0);
				if (atomic_read(&card->u.x.command_busy))
					atomic_set(&card->u.x.command_busy,0);

				send_delayed_cmd_result(card,dev,card->mbox);

				more_to_exec=0;
				break;
			case DELAY_RESULT:
		
				/* Wait for the remote to respond, before
                                 * sending the result up to the socket.
                                 * WAIT command */
				if (atomic_read(&card->u.x.command_busy))
					atomic_set(&card->u.x.command_busy,0);
				
				atomic_set(&chan->common.command,0);
				more_to_exec=0;
				break;
			default:

				/* If command could not be executed for
                                 * some reason (i.e return code 0x33 busy)
                                 * set the more_to_exec bit which will
                                 * indicate that this command must be exectued
                                 * again during next timer interrupt 
				 */
				more_to_exec=1;
				if (atomic_read(&card->u.x.command_busy) == 0)
					atomic_set(&card->u.x.command_busy,1);
				break;
			}

			bad_cmd=0;

			/* If flags is set, there are no hdlc buffers,
                         * thus, wait for the next pass and try the
                         * same command again. Otherwise, start searching 
                         * from next device on the next pass. 
			 */
			if (!more_to_exec){
				dev = move_dev_to_next(card,dev);
			}
			break;
		}else{
			/* This device has nothing to execute,
                         * go to next. 
			 */
			if (atomic_read(&card->u.x.command_busy))
					atomic_set(&card->u.x.command_busy,0);
			dev = move_dev_to_next(card,dev);
		}	

		if (++i == card->u.x.no_dev){
			if (!more_to_exec){
				DBG_PRINTK(KERN_INFO "%s: Nothing to execute in Timer\n",
					card->devname);
				if (atomic_read(&card->u.x.command_busy)){
					atomic_set(&card->u.x.command_busy,0);
				}
			}
			break;
		}

	} //End of FOR

	card->u.x.cmd_dev = dev;
	
	if (more_to_exec){
		/* If more commands are pending, do not turn off timer 
                 * interrupt */
		return 1;
	}else{
		/* No more commands, turn off timer interrupt */
		return 0;
	}	
}

/*===============================================================
 * execute_delayed_cmd 
 *
 *	Execute an API command which was passed down from the
 *      sock.  Sock is very limited in which commands it can
 *      execute.  Wait and No Wait commands are supported.  
 *      Place Call, Clear Call and Reset wait commands, where
 *      Accept Call is a no_wait command.
 *
 *===============================================================*/

4452 4453
static int execute_delayed_cmd(sdla_t* card, struct net_device *dev,
			       mbox_cmd_t *usr_cmd, char bad_cmd)
Linus Torvalds's avatar
Linus Torvalds committed
4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646
{
	TX25Mbox* mbox = card->mbox;
	int err;
	x25_channel_t *chan = dev->priv;
	int delay=RETURN_RESULT;

	if (!(*card->u.x.hdlc_buf_status & 0x40) && !bad_cmd){
		return TRY_CMD_AGAIN;
	}

	/* This way a command is guaranteed to be executed for
         * a specific lcn, the network interface is bound to. */
	usr_cmd->cmd.lcn = chan->common.lcn;
	

	/* If channel is pvc, instead of place call
         * run x25_channel configuration. If running LAPB HDLC
         * enable communications. 
         */
	if ((!chan->common.svc) && (usr_cmd->cmd.command == X25_PLACE_CALL)){

		if (card->u.x.LAPB_hdlc){
			DBG_PRINTK(KERN_INFO "LAPB: Connecting\n");
			connect(card);
			set_chan_state(dev,WAN_CONNECTING);
			return DELAY_RESULT;
		}else{
			DBG_PRINTK(KERN_INFO "%s: PVC is CONNECTING\n",card->devname);
			if (x25_get_chan_conf(card, chan) == CMD_OK){
				set_chan_state(dev, WAN_CONNECTED);
			}else{ 
				set_chan_state(dev, WAN_DISCONNECTED);
			}
			return RETURN_RESULT;
		}
	}

	/* Copy the socket mbox command onto the board */

	memcpy(&mbox->cmd, &usr_cmd->cmd, sizeof(TX25Cmd));
	if (usr_cmd->cmd.length){
		memcpy(mbox->data, usr_cmd->data, usr_cmd->cmd.length);
	}

	/* Check if command is bad. We need to copy the cmd into
         * the buffer regardless since we return the, mbox to
         * the user */
	if (bad_cmd){
		mbox->cmd.result=0x01;
		return RETURN_RESULT;
	}

	err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;

	if (err != CMD_OK && err != X25RES_NOT_READY)
		x25_error(card, err, usr_cmd->cmd.command, usr_cmd->cmd.lcn);

	if (mbox->cmd.result == X25RES_NOT_READY){
		return TRY_CMD_AGAIN;
	}

	switch (mbox->cmd.command){

	case X25_PLACE_CALL:
		
		switch (mbox->cmd.result){

		case CMD_OK:

			/* Check if Place call is a wait command or a 
               	  	 * no wait command */
			if (atomic_read(&chan->common.command) & 0x80)
 				delay=RETURN_RESULT;
			else
				delay=DELAY_RESULT;
		

			DBG_PRINTK(KERN_INFO "\n%s: PLACE CALL Binding dev %s to lcn %i\n",
					card->devname,dev->name, mbox->cmd.lcn);
		
			bind_lcn_to_dev (card, dev, mbox->cmd.lcn);
			set_chan_state(dev, WAN_CONNECTING);
			break;


		default:
			delay=RETURN_RESULT;
			set_chan_state(dev, WAN_DISCONNECTED);
			break;
		}
		break;

	case X25_ACCEPT_CALL: 
		
		switch (mbox->cmd.result){

		case CMD_OK:

			DBG_PRINTK(KERN_INFO "\n%s: ACCEPT Binding dev %s to lcn %i\n",
				card->devname,dev->name,mbox->cmd.lcn);

			bind_lcn_to_dev (card, dev, mbox->cmd.lcn);

			if (x25_get_chan_conf(card, chan) == CMD_OK){

				set_chan_state(dev, WAN_CONNECTED);
				delay=RETURN_RESULT;

			}else{ 
				if (x25_clear_call(card, usr_cmd->cmd.lcn, 0, 0) == CMD_OK){
					/* if clear is successful, wait for clear confirm 
					 */ 
					delay=DELAY_RESULT;
				}else{
					/* Do not change the state here. If we fail 
					 * the accept the return code is send up 
					 *the stack, which will ether retry
                               	  	 * or clear the call 
					 */
					DBG_PRINTK(KERN_INFO 
						"%s: ACCEPT: STATE MAY BE CURRUPTED 2 !!!!!\n",
						card->devname);
					delay=RETURN_RESULT;
				}
			}
			break;


		case X25RES_ASYNC_PACKET:
			delay=TRY_CMD_AGAIN;
			break;

		default: 
			DBG_PRINTK(KERN_INFO "%s: ACCEPT FAILED\n",card->devname);
			if (x25_clear_call(card, usr_cmd->cmd.lcn, 0, 0) == CMD_OK){
				delay=DELAY_RESULT;
			}else{
				/* Do not change the state here. If we fail the accept. The
                                 * return code is send up the stack, which will ether retry
                                 * or clear the call */
				DBG_PRINTK(KERN_INFO 
					"%s: ACCEPT: STATE MAY BE CORRUPTED 1 !!!!!\n",
						card->devname);
				delay=RETURN_RESULT;
			}
		}
		break;

	case X25_CLEAR_CALL:

		switch (mbox->cmd.result){

		case CMD_OK:
			DBG_PRINTK(KERN_INFO 
					"CALL CLEAR OK: Dev %s Mbox Lcn %i  Chan Lcn %i\n",
					dev->name,mbox->cmd.lcn,chan->common.lcn);
			set_chan_state(dev, WAN_DISCONNECTING);
			delay = DELAY_RESULT;
			break;

		case X25RES_CHANNEL_IN_USE:
		case X25RES_ASYNC_PACKET:
			delay = TRY_CMD_AGAIN;
			break;
			
		case X25RES_LINK_NOT_IN_ABM:
		case X25RES_INVAL_LCN:
		case X25RES_INVAL_STATE:
			set_chan_state(dev, WAN_DISCONNECTED);
			delay = RETURN_RESULT;
			break;
		
		default:
			/* If command did not execute because of user
                         * fault, do not change the state. This will
                         * signal the socket that clear command failed.
                         * User can retry or close the socket.
                         * When socket gets killed, it will set the 
                         * chan->disconnect which will signal
                         * driver to clear the call */
			printk(KERN_INFO "%s: Clear Command Failed, Rc %x\n",
				card->devname,mbox->cmd.command); 
			delay = RETURN_RESULT;
		}
		break;
	}	

	return delay;
}

/*===============================================================
 * api_incoming_call 
 *
4647
 *	Pass an incoming call request up the listening
Linus Torvalds's avatar
Linus Torvalds committed
4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670
 *      sock.  If the API sock is not listening reject the
 *      call.
 *
 *===============================================================*/

static int api_incoming_call (sdla_t* card, TX25Mbox *mbox, int lcn)
{
	struct sk_buff *skb;
	int len = sizeof(TX25Cmd)+mbox->cmd.length;

	if (alloc_and_init_skb_buf(card, &skb, len)){
		printk(KERN_INFO "%s: API incoming call, no memory\n",card->devname);
		return 1;
	}

	memcpy(skb_put(skb,len),&mbox->cmd,len);

	skb->mac.raw = skb->data;
	skb->protocol = htons(X25_PROT);
	skb->pkt_type = WAN_PACKET_ASYNC;

	if (card->func(skb,card->sk) < 0){
		printk(KERN_INFO "%s: MAJOR ERROR: Failed to send up place call \n",card->devname);
4671
                dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
4672 4673 4674 4675 4676 4677 4678 4679 4680 4681
		return 1;
	}

	return 0;
}

/*===============================================================
 * send_delayed_cmd_result
 *
 *	Wait commands like PLEACE CALL or CLEAR CALL must wait
4682
 *      until the result arrives. This function passes
Linus Torvalds's avatar
Linus Torvalds committed
4683 4684 4685
 *      the result to a waiting sock. 
 *
 *===============================================================*/
4686 4687
static void send_delayed_cmd_result(sdla_t *card, struct net_device *dev,
				    TX25Mbox* mbox)
Linus Torvalds's avatar
Linus Torvalds committed
4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741
{
	x25_channel_t *chan = dev->priv;
	mbox_cmd_t *usr_cmd = (mbox_cmd_t *)chan->common.mbox;
	struct sk_buff *skb;
	int len=sizeof(unsigned char);

	atomic_set(&chan->common.command,0);

	/* If the sock is in the process of unlinking the
	 * driver from the socket, we must get out. 
	 * This never happends but is a sanity check. */
	if (test_bit(0,&chan->common.common_critical)){
		return;
	}

	if (!usr_cmd || !chan->common.sk || !chan->common.func){
		DBG_PRINTK(KERN_INFO "Delay result: Sock not bounded sk: %u, func: %u, mbox: %u\n",
			(unsigned int)chan->common.sk,
			(unsigned int)chan->common.func,
			(unsigned int)usr_cmd); 
		return;
	}

	memcpy(&usr_cmd->cmd, &mbox->cmd, sizeof(TX25Cmd)); 
	if (mbox->cmd.length > 0){
		memcpy(usr_cmd->data, mbox->data, mbox->cmd.length);
	}

	if (alloc_and_init_skb_buf(card,&skb,len)){
		printk(KERN_INFO "Delay result: No sock buffers\n");
		return;
	}

	memcpy(skb_put(skb,len),&mbox->cmd.command,len);
	
	skb->mac.raw = skb->data;
	skb->pkt_type = WAN_PACKET_CMD;
			
	chan->common.func(skb,dev,chan->common.sk);
}

/*===============================================================
 * clear_confirm_event
 *
 * 	Pass the clear confirmation event up the sock. The
 *      API will disconnect only after the clear confirmation
 *      has been received. 
 *
 *      Depending on the state, clear confirmation could 
 *      be an OOB event, or a result of an API command.
 *===============================================================*/

static int clear_confirm_event (sdla_t *card, TX25Mbox* mb)
{
4742
	struct net_device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789
	x25_channel_t *chan;
	unsigned char old_state;	

	dev = find_channel(card,mb->cmd.lcn);
	if (!dev){
		DBG_PRINTK(KERN_INFO "%s: *** GOT CLEAR BUT NO DEV %i\n",
				card->devname,mb->cmd.lcn);
		return 0;
	}

	chan=dev->priv;
	DBG_PRINTK(KERN_INFO "%s: GOT CLEAR CONFIRM %s:  Mbox lcn %i  Chan lcn %i\n",
			card->devname, dev->name, mb->cmd.lcn, chan->common.lcn);

	/* If not API fall through to default. 
	 * If API, send the result to a waiting
         * socket.
	 */
	
	old_state = chan->common.state;
	set_chan_state(dev, WAN_DISCONNECTED);

	if (chan->common.usedby == API){
		switch (old_state) {

		case WAN_DISCONNECTING:
		case WAN_CONNECTING:
			send_delayed_cmd_result(card,dev,mb);
			break;
		case WAN_CONNECTED:
			send_oob_msg(card,dev,mb);
			break;
		}
		return 1;
	}

	return 0;
}

/*===============================================================
 * send_oob_msg
 *
 *    Construct an NEM Message and pass it up the connected
 *    sock. If the sock is not bounded discard the NEM.
 *
 *===============================================================*/

4790
static void send_oob_msg(sdla_t *card, struct net_device *dev, TX25Mbox *mbox)
Linus Torvalds's avatar
Linus Torvalds committed
4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838
{
	x25_channel_t *chan = dev->priv;
	mbox_cmd_t *usr_cmd = (mbox_cmd_t *)chan->common.mbox;
	struct sk_buff *skb;
	int len=sizeof(x25api_hdr_t)+mbox->cmd.length;
	x25api_t *api_hdr;

	/* If the sock is in the process of unlinking the
	 * driver from the socket, we must get out. 
	 * This never happends but is a sanity check. */
	if (test_bit(0,&chan->common.common_critical)){
		return;
	}

	if (!usr_cmd || !chan->common.sk || !chan->common.func){
		DBG_PRINTK(KERN_INFO "OOB MSG: Sock not bounded\n"); 
		return;
	}

	memcpy(&usr_cmd->cmd, &mbox->cmd, sizeof(TX25Cmd)); 
	if (mbox->cmd.length > 0){
		memcpy(usr_cmd->data, mbox->data, mbox->cmd.length);
	}

	if (alloc_and_init_skb_buf(card,&skb,len)){
		printk(KERN_INFO "%s: OOB MSG: No sock buffers\n",card->devname);
		return;
	}

	api_hdr = (x25api_t*)skb_put(skb,len); 
	api_hdr->hdr.pktType = mbox->cmd.pktType & 0x7F;
	api_hdr->hdr.qdm     = mbox->cmd.qdm;
	api_hdr->hdr.cause   = mbox->cmd.cause;
	api_hdr->hdr.diagn   = mbox->cmd.diagn;
	api_hdr->hdr.length  = mbox->cmd.length;
	api_hdr->hdr.result  = mbox->cmd.result;
	api_hdr->hdr.lcn     = mbox->cmd.lcn;

	if (mbox->cmd.length > 0){
		memcpy(api_hdr->data,mbox->data,mbox->cmd.length);
	}
	
	skb->mac.raw = skb->data;
	skb->pkt_type = WAN_PACKET_ERR;
			
	if (chan->common.func(skb,dev,chan->common.sk) < 0){
		if (bh_enqueue(dev,skb)){
			printk(KERN_INFO "%s: Dropping OOB MSG\n",card->devname);
4839
                	dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866
		}
	}

	DBG_PRINTK(KERN_INFO "%s: OOB MSG OK, %s, lcn %i\n",
			card->devname, dev->name, mbox->cmd.lcn);
}	

/*===============================================================
 *  alloc_and_init_skb_buf 
 *
 *	Allocate and initialize an skb buffer. 
 *
 *===============================================================*/

static int alloc_and_init_skb_buf (sdla_t *card, struct sk_buff **skb, int len)
{
	struct sk_buff *new_skb = *skb;

	new_skb = dev_alloc_skb(len + X25_HRDHDR_SZ);
	if (new_skb == NULL){
		printk(KERN_INFO "%s: no socket buffers available!\n",
			card->devname);
		return 1;
	}

	if (skb_tailroom(new_skb) < len){
		/* No room for the packet. Call off the whole thing! */
4867
                dev_kfree_skb_any(new_skb);
Linus Torvalds's avatar
Linus Torvalds committed
4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887
		printk(KERN_INFO "%s: Listen: unexpectedly long packet sequence\n"
			,card->devname);
		*skb = NULL;
		return 1;
	}

	*skb = new_skb;
	return 0;

}

/*===============================================================
 *  api_oob_event 
 *
 *	Send an OOB event up to the sock 
 *
 *===============================================================*/

static void api_oob_event (sdla_t *card,TX25Mbox *mbox)
{
4888
	struct net_device *dev = find_channel(card, mbox->cmd.lcn);
Linus Torvalds's avatar
Linus Torvalds committed
4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903
	x25_channel_t *chan;

	if (!dev)
		return;

	chan=dev->priv;

	if (chan->common.usedby == API)
		send_oob_msg(card,dev,mbox);
	
}




4904
static int channel_disconnect(sdla_t* card, struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977
{

	int err;
	x25_channel_t *chan = dev->priv;

	DBG_PRINTK(KERN_INFO "%s: TIMER: %s, Device down disconnecting\n",
				card->devname,dev->name);

	if (chan->common.svc){
		err = x25_clear_call(card,chan->common.lcn,0,0);
	}else{
		/* If channel is PVC or LAPB HDLC, there is no call
                 * to be cleared, thus drop down to the default
                 * area 
	         */
		err = 1;
	}

	switch (err){
	
		case X25RES_CHANNEL_IN_USE:	
		case X25RES_NOT_READY:
			err = TRY_CMD_AGAIN;
			break;
		case CMD_OK:
			DBG_PRINTK(KERN_INFO "CALL CLEAR OK: Dev %s Chan Lcn %i\n",
						dev->name,chan->common.lcn);

			set_chan_state(dev,WAN_DISCONNECTING);
			atomic_set(&chan->common.command,0);
			err = DELAY_RESULT;
			break;
		default:
			/* If LAPB HDLC protocol, bring the whole link down
                         * once the application terminates 
			 */

			set_chan_state(dev,WAN_DISCONNECTED);

			if (card->u.x.LAPB_hdlc){
				DBG_PRINTK(KERN_INFO "LAPB: Disconnecting Link\n");
				hdlc_link_down (card);
			}
			atomic_set(&chan->common.command,0);
			err = RETURN_RESULT;
			break;
	}

	return err;
}

static void hdlc_link_down (sdla_t *card)
{
	TX25Mbox* mbox = card->mbox;
	int retry = 5;
	int err=0;

	do {
		memset(mbox,0,sizeof(TX25Mbox));
		mbox->cmd.command = X25_HDLC_LINK_DISC;
		mbox->cmd.length = 1;
		mbox->data[0]=0;
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;

	} while (err && retry-- && x25_error(card, err, X25_HDLC_LINK_DISC, 0));

	if (err)
		printk(KERN_INFO "%s: Hdlc Link Down Failed %x\n",card->devname,err);

	disconnect (card);
	
}

4978
static int check_bad_command(sdla_t* card, struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030
{
	x25_channel_t *chan = dev->priv;
	int bad_cmd = 0;

	switch (atomic_read(&chan->common.command)&0x7F){

		case X25_PLACE_CALL:
			if (chan->common.state != WAN_DISCONNECTED)
				bad_cmd=1;
			break;
		case X25_CLEAR_CALL:
			if (chan->common.state == WAN_DISCONNECTED)
				bad_cmd=1;
			break;
		case X25_ACCEPT_CALL:
			if (chan->common.state != WAN_CONNECTING)
				bad_cmd=1;
			break;
		case X25_RESET:
			if (chan->common.state != WAN_CONNECTED)
				bad_cmd=1;
			break;
		default:
			bad_cmd=1;
			break;
	}

	if (bad_cmd){
		printk(KERN_INFO "%s: Invalid State, BAD Command %x, dev %s, lcn %i, st %i\n", 
			card->devname,atomic_read(&chan->common.command),dev->name, 
			chan->common.lcn, chan->common.state);
	}

	return bad_cmd;
}



/*************************** XPIPEMON FUNCTIONS **************************/

/*==============================================================================
 * Process UDP call of type XPIPE
 */

static int process_udp_mgmt_pkt(sdla_t *card)
{
	int            c_retry = MAX_CMD_RETRY;
	unsigned int   len;
	struct sk_buff *new_skb;
	TX25Mbox       *mbox = card->mbox;
	int            err;
	int            udp_mgmt_req_valid = 1;
5031
	struct net_device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354
        x25_channel_t  *chan;
	unsigned short lcn;
	struct timeval tv;
	

	x25_udp_pkt_t *x25_udp_pkt;
	x25_udp_pkt = (x25_udp_pkt_t *)card->u.x.udp_pkt_data;

	dev = card->u.x.udp_dev;
	chan = dev->priv;
	lcn = chan->common.lcn;

	switch(x25_udp_pkt->cblock.command) {
            
		/* XPIPE_ENABLE_TRACE */
		case XPIPE_ENABLE_TRACING:

		/* XPIPE_GET_TRACE_INFO */
		case XPIPE_GET_TRACE_INFO:
 
		/* SET FT1 MODE */
		case XPIPE_SET_FT1_MODE:
           
			if(card->u.x.udp_pkt_src == UDP_PKT_FRM_NETWORK) {
                    		++chan->pipe_mgmt_stat.UDP_PIPE_mgmt_direction_err;
				udp_mgmt_req_valid = 0;
				break;
			}

		/* XPIPE_FT1_READ_STATUS */
		case XPIPE_FT1_READ_STATUS:

		/* FT1 MONITOR STATUS */
		case XPIPE_FT1_STATUS_CTRL:
			if(card->hw.fwid !=  SFID_X25_508) {
				++chan->pipe_mgmt_stat.UDP_PIPE_mgmt_adptr_type_err;
				udp_mgmt_req_valid = 0;
				break;
			}
		default:
			break;
       	}

	if(!udp_mgmt_req_valid) {
           	/* set length to 0 */
		x25_udp_pkt->cblock.length = 0;
		/* set return code */
		x25_udp_pkt->cblock.result = (card->hw.fwid != SFID_X25_508) ? 0x1F : 0xCD;
		
	} else {   
        
		switch (x25_udp_pkt->cblock.command) {
    
	
		case XPIPE_FLUSH_DRIVER_STATS:
			init_x25_channel_struct(chan);
			init_global_statistics(card);
			mbox->cmd.length = 0;
			break;


		case XPIPE_DRIVER_STAT_IFSEND:
			memcpy(x25_udp_pkt->data, &chan->if_send_stat, sizeof(if_send_stat_t));
			mbox->cmd.length = sizeof(if_send_stat_t);
			x25_udp_pkt->cblock.length =  mbox->cmd.length;	
			break;
	
		case XPIPE_DRIVER_STAT_INTR:
			memcpy(&x25_udp_pkt->data[0], &card->statistics, sizeof(global_stats_t));
                        memcpy(&x25_udp_pkt->data[sizeof(global_stats_t)],
                                &chan->rx_intr_stat, sizeof(rx_intr_stat_t));
			
			mbox->cmd.length = sizeof(global_stats_t) +
					sizeof(rx_intr_stat_t);
			x25_udp_pkt->cblock.length =  mbox->cmd.length;
			break;

		case XPIPE_DRIVER_STAT_GEN:
                        memcpy(x25_udp_pkt->data,
                                &chan->pipe_mgmt_stat.UDP_PIPE_mgmt_kmalloc_err,
                                sizeof(pipe_mgmt_stat_t));

                        memcpy(&x25_udp_pkt->data[sizeof(pipe_mgmt_stat_t)],
                               &card->statistics, sizeof(global_stats_t));

                        x25_udp_pkt->cblock.result = 0;
                        x25_udp_pkt->cblock.length = sizeof(global_stats_t)+
                                                     sizeof(rx_intr_stat_t);
                        mbox->cmd.length = x25_udp_pkt->cblock.length;
                        break;

		case XPIPE_ROUTER_UP_TIME:
			do_gettimeofday(&tv);
			chan->router_up_time = tv.tv_sec - chan->router_start_time;
    	                *(unsigned long *)&x25_udp_pkt->data = chan->router_up_time;	
			x25_udp_pkt->cblock.length = mbox->cmd.length = 4;
			x25_udp_pkt->cblock.result = 0;
			break;
	
		default :

			do {
				memcpy(&mbox->cmd, &x25_udp_pkt->cblock.command, sizeof(TX25Cmd));
				if(mbox->cmd.length){ 
					memcpy(&mbox->data, 
					       (char *)x25_udp_pkt->data, 
					       mbox->cmd.length);
				}	
		
				err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
			} while (err && c_retry-- && x25_error(card, err, mbox->cmd.command, 0));


			if ( err == CMD_OK || 
			    (err == 1 && 
			     (mbox->cmd.command == 0x06 || 
			      mbox->cmd.command == 0x16)  ) ){

				++chan->pipe_mgmt_stat.UDP_PIPE_mgmt_adptr_cmnd_OK;
			} else {
				++chan->pipe_mgmt_stat.UDP_PIPE_mgmt_adptr_cmnd_timeout;
			}

			  /* copy the result back to our buffer */
			memcpy(&x25_udp_pkt->cblock.command, &mbox->cmd, sizeof(TX25Cmd));

      	         	if(mbox->cmd.length) {
        	               memcpy(&x25_udp_pkt->data, &mbox->data, mbox->cmd.length);
			}
			break;

		} //switch

        }
    
        /* Fill UDP TTL */

	x25_udp_pkt->ip_pkt.ttl = card->wandev.ttl;
        len = reply_udp(card->u.x.udp_pkt_data, mbox->cmd.length);


        if(card->u.x.udp_pkt_src == UDP_PKT_FRM_NETWORK) {
		
		err = x25_send(card, lcn, 0, len, card->u.x.udp_pkt_data);
		if (!err) 
			++chan->pipe_mgmt_stat.UDP_PIPE_mgmt_adptr_send_passed;
		else
			++chan->pipe_mgmt_stat.UDP_PIPE_mgmt_adptr_send_failed;
	
	} else {

		/* Allocate socket buffer */
		if((new_skb = dev_alloc_skb(len)) != NULL) {
			void *buf;

			/* copy data into new_skb */
			buf = skb_put(new_skb, len);
			memcpy(buf, card->u.x.udp_pkt_data, len);
        
			/* Decapsulate packet and pass it up the protocol 
			   stack */
			new_skb->dev = dev;
	
			if (chan->common.usedby == API)
                        	new_skb->protocol = htons(X25_PROT);
			else 
				new_skb->protocol = htons(ETH_P_IP);
	
                        new_skb->mac.raw = new_skb->data;

			netif_rx(new_skb);
			++chan->pipe_mgmt_stat.UDP_PIPE_mgmt_passed_to_stack;
            	
		} else {
			++chan->pipe_mgmt_stat.UDP_PIPE_mgmt_no_socket;
			printk(KERN_INFO 
			"%s: UDP mgmt cmnd, no socket buffers available!\n", 
			card->devname);
            	}
        }

	card->u.x.udp_pkt_lgth = 0;

	return 1;
}


/*==============================================================================
 * Determine what type of UDP call it is. DRVSTATS or XPIPE8ND ?
 */
static int udp_pkt_type( struct sk_buff *skb, sdla_t* card )
{
	x25_udp_pkt_t *x25_udp_pkt = (x25_udp_pkt_t *)skb->data;

        if((x25_udp_pkt->ip_pkt.protocol == UDPMGMT_UDP_PROTOCOL) &&
		(x25_udp_pkt->ip_pkt.ver_inet_hdr_length == 0x45) &&
		(x25_udp_pkt->udp_pkt.udp_dst_port == ntohs(card->wandev.udp_port)) &&
		(x25_udp_pkt->wp_mgmt.request_reply == UDPMGMT_REQUEST)) {

                        if(!strncmp(x25_udp_pkt->wp_mgmt.signature,
                                UDPMGMT_XPIPE_SIGNATURE, 8)){
                                return UDP_XPIPE_TYPE;
			}else{
				printk(KERN_INFO "%s: UDP Packet, Failed Signature !\n",
					card->devname);
			}
	}

        return UDP_INVALID_TYPE;
}


/*============================================================================
 * Reply to UDP Management system.
 * Return nothing.
 */
static int reply_udp( unsigned char *data, unsigned int mbox_len ) 
{
	unsigned short len, udp_length, temp, ip_length;
	unsigned long ip_temp;
	int even_bound = 0;

  
	x25_udp_pkt_t *x25_udp_pkt = (x25_udp_pkt_t *)data; 

	/* Set length of packet */
	len = sizeof(ip_pkt_t)+ 
	      sizeof(udp_pkt_t)+
	      sizeof(wp_mgmt_t)+
	      sizeof(cblock_t)+
	      mbox_len;
 

	/* fill in UDP reply */
	x25_udp_pkt->wp_mgmt.request_reply = UDPMGMT_REPLY;
  
	/* fill in UDP length */
	udp_length = sizeof(udp_pkt_t)+ 
		     sizeof(wp_mgmt_t)+
		     sizeof(cblock_t)+
		     mbox_len; 


	/* put it on an even boundary */
	if ( udp_length & 0x0001 ) {
		udp_length += 1;
		len += 1;
		even_bound = 1;
	}

	temp = (udp_length<<8)|(udp_length>>8);
	x25_udp_pkt->udp_pkt.udp_length = temp;
	 
	/* swap UDP ports */
	temp = x25_udp_pkt->udp_pkt.udp_src_port;
	x25_udp_pkt->udp_pkt.udp_src_port = 
			x25_udp_pkt->udp_pkt.udp_dst_port; 
	x25_udp_pkt->udp_pkt.udp_dst_port = temp;



	/* add UDP pseudo header */
	temp = 0x1100;
	*((unsigned short *)
		(x25_udp_pkt->data+mbox_len+even_bound)) = temp;	
	temp = (udp_length<<8)|(udp_length>>8);
	*((unsigned short *)
		(x25_udp_pkt->data+mbox_len+even_bound+2)) = temp;
		 
	/* calculate UDP checksum */
	x25_udp_pkt->udp_pkt.udp_checksum = 0;

	x25_udp_pkt->udp_pkt.udp_checksum = 
		calc_checksum(&data[UDP_OFFSET], udp_length+UDP_OFFSET);

	/* fill in IP length */
	ip_length = len;
	temp = (ip_length<<8)|(ip_length>>8);
	x25_udp_pkt->ip_pkt.total_length = temp;
  
	/* swap IP addresses */
	ip_temp = x25_udp_pkt->ip_pkt.ip_src_address;
	x25_udp_pkt->ip_pkt.ip_src_address = 
				x25_udp_pkt->ip_pkt.ip_dst_address;
	x25_udp_pkt->ip_pkt.ip_dst_address = ip_temp;

		 
	/* fill in IP checksum */
	x25_udp_pkt->ip_pkt.hdr_checksum = 0;
	x25_udp_pkt->ip_pkt.hdr_checksum = calc_checksum(data, sizeof(ip_pkt_t));

	return len;
} /* reply_udp */

unsigned short calc_checksum (char *data, int len)
{
	unsigned short temp; 
	unsigned long sum=0;
	int i;

	for( i = 0; i <len; i+=2 ) {
		memcpy(&temp,&data[i],2);
		sum += (unsigned long)temp;
	}

	while (sum >> 16 ) {
		sum = (sum & 0xffffUL) + (sum >> 16);
	}

	temp = (unsigned short)sum;
	temp = ~temp;

	if( temp == 0 ) 
		temp = 0xffff;

	return temp;	
}

/*=============================================================================
 * Store a UDP management packet for later processing.
 */

static int store_udp_mgmt_pkt(int udp_type, char udp_pkt_src, sdla_t* card,
5355 5356
			      struct net_device *dev, struct sk_buff *skb,
			      int lcn)
Linus Torvalds's avatar
Linus Torvalds committed
5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375
{
        int udp_pkt_stored = 0;

        if(!card->u.x.udp_pkt_lgth && (skb->len <= MAX_LGTH_UDP_MGNT_PKT)){
                card->u.x.udp_pkt_lgth = skb->len;
                card->u.x.udp_type = udp_type;
                card->u.x.udp_pkt_src = udp_pkt_src;
                card->u.x.udp_lcn = lcn;
		card->u.x.udp_dev = dev;
                memcpy(card->u.x.udp_pkt_data, skb->data, skb->len);
                card->u.x.timer_int_enabled |= TMR_INT_ENABLED_UDP_PKT;
                udp_pkt_stored = 1;

        }else{
                printk(KERN_INFO "%s: ERROR: UDP packet not stored for LCN %d\n", 
							card->devname,lcn);
	}

        if(udp_pkt_src == UDP_PKT_FRM_STACK){
5376
                dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
5377
	}else{
5378
                dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497
	}

        return(udp_pkt_stored);
}



/*=============================================================================
 * Initial the ppp_private_area structure.
 */
static void init_x25_channel_struct( x25_channel_t *chan )
{
	memset(&chan->if_send_stat.if_send_entry,0,sizeof(if_send_stat_t));
	memset(&chan->rx_intr_stat.rx_intr_no_socket,0,sizeof(rx_intr_stat_t));
	memset(&chan->pipe_mgmt_stat.UDP_PIPE_mgmt_kmalloc_err,0,sizeof(pipe_mgmt_stat_t));
}

/*============================================================================
 * Initialize Global Statistics
 */
static void init_global_statistics( sdla_t *card )
{
	memset(&card->statistics.isr_entry,0,sizeof(global_stats_t));
}


/*===============================================================
 * SMP Support
 * ==============================================================*/

static void S508_S514_lock(sdla_t *card, unsigned long *smp_flags)
{
	spin_lock_irqsave(&card->wandev.lock, *smp_flags);
}
static void S508_S514_unlock(sdla_t *card, unsigned long *smp_flags)
{
	spin_unlock_irqrestore(&card->wandev.lock, *smp_flags);
}

/*===============================================================
 * x25_timer_routine
 *
 * 	A more efficient polling routine.  Each half a second
 * 	queue a polling task. We want to do the polling in a 
 * 	task not timer, because timer runs in interrupt time.
 *
 * 	FIXME Polling should be rethinked.
 *==============================================================*/

static void x25_timer_routine(unsigned long data)
{
	sdla_t *card = (sdla_t*)data;

	if (!card->wandev.dev){
		printk(KERN_INFO "%s: Stopping the X25 Poll Timer: No Dev.\n",
				card->devname);
		return;
	}

	if (card->open_cnt != card->u.x.num_of_ch){
		printk(KERN_INFO "%s: Stopping the X25 Poll Timer: Interface down.\n",
				card->devname);
		return;
	}

	if (test_bit(PERI_CRIT,&card->wandev.critical)){
		printk(KERN_INFO "%s: Stopping the X25 Poll Timer: Shutting down.\n",
				card->devname);
		return;
	}
	
	if (!test_and_set_bit(POLL_CRIT,&card->wandev.critical)){
		trigger_x25_poll(card);
	}
	
	card->u.x.x25_timer.expires=jiffies+(HZ>>1);
	add_timer(&card->u.x.x25_timer);
	return;
}

void disable_comm_shutdown(sdla_t *card)
{
	TX25Mbox* mbox = card->mbox;
	int err;

	/* Turn of interrutps */
	mbox->data[0] = 0;
	if (card->hw.fwid == SFID_X25_508){
		mbox->data[1] = card->hw.irq;
		mbox->data[2] = 2;
		mbox->cmd.length = 3;
	}else {
	 	mbox->cmd.length  = 1;
	}
	mbox->cmd.command = X25_SET_INTERRUPT_MODE;
	err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	if (err)
		printk(KERN_INFO "INTERRUPT OFF FAIED %x\n",err);

	/* Bring down HDLC */
	mbox->cmd.command = X25_HDLC_LINK_CLOSE;
	mbox->cmd.length  = 0;
	err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	if (err)
		printk(KERN_INFO "LINK CLOSED FAILED %x\n",err);


	/* Brind down DTR */
	mbox->data[0] = 0;
	mbox->data[2] = 0;
	mbox->data[1] = 0x01;
	mbox->cmd.length  = 3;
	mbox->cmd.command = X25_SET_GLOBAL_VARS;
	err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	if (err)
		printk(KERN_INFO "DTR DOWN FAILED %x\n",err);

}

Linus Torvalds's avatar
Linus Torvalds committed
5498 5499
MODULE_LICENSE("GPL");

Linus Torvalds's avatar
Linus Torvalds committed
5500
/****** End *****************************************************************/