patch-2.3.41 linux/drivers/net/wan/sdla_fr.c

Next file: linux/drivers/net/wan/sdla_ppp.c
Previous file: linux/drivers/net/wan/sdla_chdlc.c
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.3.40/linux/drivers/net/wan/sdla_fr.c linux/drivers/net/wan/sdla_fr.c
@@ -1,16 +1,38 @@
 /*****************************************************************************
 * sdla_fr.c	WANPIPE(tm) Multiprotocol WAN Link Driver. Frame relay module.
 *
-* Author(s):	Gene Kozin	
-*		Jaspreet Singh		<jaspreet@sangoma.com>
+* Author(s):	Nenad Corbic  <ncorbic@sangoma.com>
+*		Gideon Hack
 *
-* Copyright:	(c) 1995-1997 Sangoma Technologies Inc.
+* Copyright:	(c) 1995-1999 Sangoma Technologies Inc.
 *
 *		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.
 * ============================================================================
+* Nov 08, 1999  Nenad Corbic    o Combined all debug UDP calls into one function
+*                               o Removed the ARP support. This has to be done
+*                                 in the next version.
+*                               o Only a Node can implement NO signalling.
+*                                 Initialize DLCI during if_open() if NO 
+*				  signalling.
+*				o Took out IPX support, implement in next
+*                                 version
+* Sep 29, 1999  Nenad Corbic	o Added SMP support and changed the update
+*                                 function to use timer interrupt.
+*				o Fixed the CIR bug:  Set the value of BC
+*                                 to CIR when the CIR is enabled.
+*  				o Updated comments, statistics and tracing.
+* Jun 02, 1999	Gideon Hack	o Updated for S514 support.
+* Sep 18, 1998	Jaspreet Singh	o Updated for 2.2.X kernels.
+* Jul 31, 1998	Jaspreet Singh	o Removed wpf_poll routine.  The channel/DLCI 
+*				  status is received through an event interrupt.
+* Jul 08, 1998	David Fong	o Added inverse ARP support.
+* Mar 26, 1997	Jaspreet Singh	o Returning return codes for failed UDP cmds.
+* Jan 28, 1997	Jaspreet Singh  o Improved handling of inactive DLCIs.
+* Dec 30, 1997	Jaspreet Singh	o Replaced dev_tint() with mark_bh(NET_BH)
+* Dec 16, 1997	Jaspreet Singh	o Implemented Multiple IPX support.
 * Nov 26, 1997	Jaspreet Singh	o Improved load sharing with multiple boards
 *				o Added Cli() to protect enabling of interrupts
 *				  while polling is called.
@@ -42,7 +64,8 @@
 *				 o Added ability to discard multicast and 
 *				   broadcast source addressed packets
 * Jun 27, 1997	Jaspreet Singh	 o Added FT1 monitor capabilities 
-*				   New case (0x44) statement in if_send routine *				   Added a global variable rCount to keep track
+*				   New case (0x44) statement in if_send routine 
+*				   Added a global variable rCount to keep track
 *			 	   of FT1 status enabled on the board.
 * May 29, 1997	Jaspreet Singh	 o Fixed major Flow Control Problem
 *				   With multiple boards a problem was seen where
@@ -50,13 +73,15 @@
 *				   packet after running for a while. The code
 *				   got into a stage where the interrupts were
 *				   disabled and dev->tbusy was set to 1.
-*                  		   This caused the If_send() routine to get into*                                  the if clause for it(0,dev->tbusy) 
+*                  		   This caused the If_send() routine to get into
+*                                  the if clause for it(0,dev->tbusy) 
 *				   forever.
 *				   The code got into this stage due to an 
-*				   interrupt occurring within the if clause for 
+*				   interrupt occuring within the if clause for 
 *				   set_bit(0,dev->tbusy).  Since an interrupt 
 *				   disables furhter transmit interrupt and 
-* 				   makes dev->tbusy = 0, this effect was undone *                                  by making dev->tbusy = 1 in the if clause.
+* 				   makes dev->tbusy = 0, this effect was undone 
+*                                  by making dev->tbusy = 1 in the if clause.
 *				   The Fix checks to see if Transmit interrupts
 *				   are disabled then do not make dev->tbusy = 1
 * 	   			   Introduced a global variable: int_occur and
@@ -83,6 +108,7 @@
 * Jan 02, 1997	Gene Kozin	Initial version.
 *****************************************************************************/
 
+#include <linux/version.h>
 #include <linux/kernel.h>	/* printk(), and other useful stuff */
 #include <linux/stddef.h>	/* offsetof(), etc. */
 #include <linux/errno.h>	/* return codes */
@@ -93,140 +119,133 @@
 #include <linux/if_arp.h>	/* ARPHRD_* defines */
 #include <asm/byteorder.h>	/* htons(), etc. */
 #include <asm/io.h>		/* for inb(), outb(), etc. */
-#include <linux/time.h>		/* for do_gettimeofday */
-#define	_GNUC_
-#include <linux/sdla_fr.h>	/* frame relay firmware API definitions */
+#include <linux/time.h>	 	/* for do_gettimeofday */	
+#include <linux/in.h>		/* sockaddr_in */
+#include <linux/inet.h>		/* in_ntoa(), etc... */
 #include <asm/uaccess.h>
+#include <linux/inetdevice.h>
+#include <linux/ip.h>
+#include <net/route.h>          /* Dynamic Route Creation */
+#include <linux/if.h>
 
+#include <linux/sdla_fr.h>	/* frame relay firmware API definitions */
+#if LINUX_VERSION_CODE < 0x020125
+#define test_and_set_bit set_bit
+#endif
+ 
 /****** Defines & Macros ****************************************************/
 
-#define	MAX_CMD_RETRY	10	/* max number of firmware retries */
-#define	FR_HEADER_LEN	8	/* max encapsulation header size */
-#define	FR_CHANNEL_MTU	1500	/* unfragmented logical channel MTU */
+#define	MAX_CMD_RETRY	10		/* max number of firmware retries */
 
-/* Q.922 frame types */
+#define	FR_HEADER_LEN	8		/* max encapsulation header size */
+#define	FR_CHANNEL_MTU	1500		/* unfragmented logical channel MTU */
 
-#define	Q922_UI		0x03	/* Unnumbered Info frame */
-#define	Q922_XID	0xAF	/* ??? */
+/* Q.922 frame types */
+#define	Q922_UI		0x03		/* Unnumbered Info frame */
+#define	Q922_XID	0xAF		
 
 /* DLCI configured or not */
-
 #define DLCI_NOT_CONFIGURED	0x00
 #define DLCI_CONFIG_PENDING	0x01
 #define DLCI_CONFIGURED		0x02
 
 /* CIR enabled or not */
-
 #define CIR_ENABLED	0x00
 #define CIR_DISABLED	0x01
 
-/* Interrupt mode for DLCI = 0 */
-
-#define BUFFER_INTR_MODE	0x00
-#define DLCI_LIST_INTR_MODE	0x01
-
-/* Transmit Interrupt Status */
-
-#define DISABLED 		0x00
-#define WAITING_TO_BE_ENABLED	0x01
+#define WANPIPE 0x00
+#define API     0x01
+#define FRAME_RELAY_API 1
 
 /* For handle_IPXWAN() */
-
 #define CVHexToAscii(b) (((unsigned char)(b) > (unsigned char)9) ? ((unsigned char)'A' + ((unsigned char)(b) - (unsigned char)10)) : ((unsigned char)'0' + (unsigned char)(b)))
-
+ 
 /****** Data Structures *****************************************************/
 
 /* This is an extention of the 'struct net_device' we create for each network
  * interface to keep the rest of channel-specific data.
  */
-typedef struct fr_channel {
-	char name[WAN_IFNAME_SZ + 1];	/* interface name, ASCIIZ */
-	unsigned dlci_configured;	/* check whether configured or not */
-	unsigned cir_status;	/* check whether CIR enabled or not */
-	unsigned dlci;		/* logical channel number */
-	unsigned cir;		/* committed information rate */
-	unsigned bc;		/* committed burst size */
-	unsigned be;		/* excess burst size */
-	unsigned mc;		/* multicast support on or off */
-	unsigned tx_int_status;	/* Transmit Interrupt Status */
+typedef struct fr_channel
+{
+	char name[WAN_IFNAME_SZ+1];	/* interface name, ASCIIZ */
+	unsigned dlci_configured  ;	/* check whether configured or not */
+	unsigned cir_status;		/* check whether CIR enabled or not */
+	unsigned dlci;			/* logical channel number */
+	unsigned cir;			/* committed information rate */
+	unsigned bc;			/* committed burst size */
+	unsigned be;			/* excess burst size */
+	unsigned mc;			/* multicast support on or off */
+	unsigned tx_int_status;		/* Transmit Interrupt Status */	
 	unsigned short pkt_length;	/* Packet Length */
-	unsigned long router_start_time;	/* Router start time in seconds */
+	unsigned long router_start_time;/* Router start time in seconds */
 	unsigned long tick_counter;	/* counter for transmit time out */
 	char dev_pending_devtint;	/* interface pending dev_tint() */
-	char state;		/* channel state */
-	void *dlci_int_interface;	/* pointer to the DLCI Interface */
-	unsigned long IB_addr;	/* physical address of Interface Byte */
+	char state;			/* channel state */
+	void *dlci_int_interface;	/* pointer to the DLCI Interface */ 
+	unsigned long IB_addr;		/* physical address of Interface Byte */
 	unsigned long state_tick;	/* time of the last state change */
-	sdla_t *card;		/* -> owner */
-	struct net_device_stats ifstats;		/* interface statistics */
-	unsigned long if_send_entry;
-	unsigned long if_send_skb_null;
-	unsigned long if_send_broadcast;
-	unsigned long if_send_multicast;
-	unsigned long if_send_critical_ISR;
-	unsigned long if_send_critical_non_ISR;
-	unsigned long if_send_busy;
-	unsigned long if_send_busy_timeout;
-	unsigned long if_send_FPIPE_request;
-	unsigned long if_send_DRVSTATS_request;
-	unsigned long if_send_wan_disconnected;
-	unsigned long if_send_dlci_disconnected;
-	unsigned long if_send_no_bfrs;
-	unsigned long if_send_adptr_bfrs_full;
-	unsigned long if_send_bfrs_passed_to_adptr;
-	unsigned long rx_intr_no_socket;
-	unsigned long rx_intr_dev_not_started;
-	unsigned long rx_intr_FPIPE_request;
-	unsigned long rx_intr_DRVSTATS_request;
-	unsigned long rx_intr_bfr_not_passed_to_stack;
-	unsigned long rx_intr_bfr_passed_to_stack;
-	unsigned long UDP_FPIPE_mgmt_kmalloc_err;
-	unsigned long UDP_FPIPE_mgmt_direction_err;
-	unsigned long UDP_FPIPE_mgmt_adptr_type_err;
-	unsigned long UDP_FPIPE_mgmt_adptr_cmnd_OK;
-	unsigned long UDP_FPIPE_mgmt_adptr_cmnd_timeout;
-	unsigned long UDP_FPIPE_mgmt_adptr_send_passed;
-	unsigned long UDP_FPIPE_mgmt_adptr_send_failed;
-	unsigned long UDP_FPIPE_mgmt_not_passed_to_stack;
-	unsigned long UDP_FPIPE_mgmt_passed_to_stack;
-	unsigned long UDP_FPIPE_mgmt_no_socket;
-	unsigned long UDP_DRVSTATS_mgmt_kmalloc_err;
-	unsigned long UDP_DRVSTATS_mgmt_adptr_cmnd_OK;
-	unsigned long UDP_DRVSTATS_mgmt_adptr_cmnd_timeout;
-	unsigned long UDP_DRVSTATS_mgmt_adptr_send_passed;
-	unsigned long UDP_DRVSTATS_mgmt_adptr_send_failed;
-	unsigned long UDP_DRVSTATS_mgmt_not_passed_to_stack;
-	unsigned long UDP_DRVSTATS_mgmt_passed_to_stack;
-	unsigned long UDP_DRVSTATS_mgmt_no_socket;
+	unsigned char enable_IPX;	/* Enable/Disable the use of IPX */
+	unsigned long network_number;	/* Internal Network Number for IPX*/
+	sdla_t *card;			/* -> owner */
+	unsigned route_flag;		/* Add/Rem dest addr in route tables */
+	unsigned inarp;			/* Inverse Arp Request status */ 
+	int inarp_interval;		/* Time between InArp Requests */
+	unsigned long inarp_tick;	/* InArp jiffies tick counter */
+	struct net_device_stats ifstats;	/* interface statistics */
+	if_send_stat_t drvstats_if_send;
+        rx_intr_stat_t drvstats_rx_intr;
+        pipe_mgmt_stat_t drvstats_gen;
+
+	unsigned char usedby;  /* Used by WANPIPE or API */
+
 	unsigned long router_up_time;
+
+	unsigned short transmit_length;
+	char transmit_buffer[FR_MAX_NO_DATA_BYTES_IN_FRAME];
 } fr_channel_t;
 
-typedef struct dlci_status {
-	unsigned short dlci PACKED;
-	unsigned char state PACKED;
+/* Route Flag options */
+#define NO_ROUTE	0x00
+#define ADD_ROUTE 	0x01
+#define ROUTE_ADDED	0x02
+#define REMOVE_ROUTE 	0x03
+
+/* inarp options */
+#define INARP_NONE		0x00
+#define INARP_REQUEST		0x01
+#define INARP_CONFIGURED	0x02
+
+/* reasons for enabling the timer interrupt on the adapter */
+#define TMR_INT_ENABLED_UDP   	0x01
+#define TMR_INT_ENABLED_UPDATE 	0x02
+
+
+typedef struct dlci_status
+{
+	unsigned short dlci	PACKED;
+	unsigned char state	PACKED;
 } dlci_status_t;
 
-typedef struct dlci_IB_mapping {
-	unsigned short dlci PACKED;
-	unsigned long addr_value PACKED;
+typedef struct dlci_IB_mapping
+{
+	unsigned short dlci		PACKED;
+	unsigned long  addr_value	PACKED;
 } dlci_IB_mapping_t;
 
 /* This structure is used for DLCI list Tx interrupt mode.  It is used to
    enable interrupt bit and set the packet length for transmission
  */
+typedef struct fr_dlci_interface 
+{
+	unsigned char gen_interrupt	PACKED;
+	unsigned short packet_length	PACKED;
+	unsigned char reserved		PACKED;
+} fr_dlci_interface_t; 
 
-typedef struct fr_dlci_interface {
-	unsigned char gen_interrupt PACKED;
-	unsigned short packet_length PACKED;
-	unsigned char reserved PACKED;
-} fr_dlci_interface_t;
-
-static unsigned short num_frames;
-static unsigned long curr_trace_addr;
-static unsigned long start_trace_addr;
-static unsigned short available_buffer_space;
-static char TracingEnabled; /* variable for keeping track of enabling/disabling FT1 monitor status */
+/* variable for keeping track of enabling/disabling FT1 monitor status */
 static int rCount = 0;
+
+extern int ip_rt_ioctl(unsigned int, void *);
 extern void disable_irq(unsigned int);
 extern void enable_irq(unsigned int);
 
@@ -234,70 +253,96 @@
  * interrupt test routine 
  */
 static int Intr_test_counter;
-
 /****** Function Prototypes *************************************************/
 
 /* WAN link driver entry points. These are called by the WAN router module. */
-static int update(wan_device_t * wandev);
-static int new_if(wan_device_t * wandev, struct net_device *dev,
-		  wanif_conf_t * conf);
-static int del_if(wan_device_t * wandev, struct net_device *dev);
+static int update(wan_device_t *wandev);
+static int new_if(wan_device_t *wandev, struct net_device *dev, wanif_conf_t *conf);
+static int del_if(wan_device_t *wandev, struct net_device *dev);
+
 /* WANPIPE-specific entry points */
 static int wpf_exec(struct sdla *card, void *u_cmd, void *u_data);
+
 /* Network device interface */
 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,
-	    unsigned short type, void *daddr, void *saddr, unsigned len);
+static int if_header(struct sk_buff *skb, struct net_device *dev, unsigned short type, void *daddr, void *saddr, unsigned len);
 static int if_rebuild_hdr(struct sk_buff *skb);
 static int if_send(struct sk_buff *skb, struct net_device *dev);
+static int chk_bcast_mcast_addr(sdla_t *card, struct net_device* dev,
+                                struct sk_buff *skb);
 static struct net_device_stats *if_stats(struct net_device *dev);
+
 /* Interrupt handlers */
-static void fr502_isr(sdla_t * card);
-static void fr508_isr(sdla_t * card);
-static void fr502_rx_intr(sdla_t * card);
-static void fr508_rx_intr(sdla_t * card);
-static void tx_intr(sdla_t * card);
-static void spur_intr(sdla_t * card);
-/* Background polling routines */
-static void wpf_poll(sdla_t * card);
+static void fr_isr(sdla_t *card);
+static void rx_intr(sdla_t *card);
+static void tx_intr(sdla_t *card);
+static void timer_intr(sdla_t *card);
+static void spur_intr(sdla_t *card);
+
 /* Frame relay firmware interface functions */
-static int fr_read_version(sdla_t * card, char *str);
-static int fr_configure(sdla_t * card, fr_conf_t * conf);
-static int fr_dlci_configure(sdla_t * card, fr_dlc_conf_t * conf, unsigned dlci);
-static int fr_set_intr_mode(sdla_t * card, unsigned mode, unsigned mtu);
-static int fr_comm_enable(sdla_t * card);
-static int fr_comm_disable(sdla_t * card);
-static int fr_get_err_stats(sdla_t * card);
-static int fr_get_stats(sdla_t * card);
-static int fr_add_dlci(sdla_t * card, int dlci, int num);
-static int fr_activate_dlci(sdla_t * card, int dlci, int num);
-static int fr_issue_isf(sdla_t * card, int isf);
-static int fr502_send(sdla_t * card, int dlci, int attr, int len, void *buf);
-static int fr508_send(sdla_t * card, int dlci, int attr, int len, void *buf);
+static int fr_read_version(sdla_t *card, char *str);
+static int fr_configure(sdla_t *card, fr_conf_t *conf);
+static int fr_dlci_configure(sdla_t *card, fr_dlc_conf_t *conf, unsigned dlci);
+static int fr_init_dlci (sdla_t *card, fr_channel_t *chan);
+static int fr_set_intr_mode (sdla_t *card, unsigned mode, unsigned mtu, unsigned short timeout);
+static int fr_comm_enable(sdla_t *card);
+static int fr_comm_disable(sdla_t *card);
+static int fr_get_err_stats(sdla_t *card);
+static int fr_get_stats(sdla_t *card);
+static int fr_add_dlci(sdla_t *card, int dlci);
+static int fr_activate_dlci(sdla_t *card, int dlci);
+static int fr_delete_dlci (sdla_t* card, int dlci);
+static int fr_issue_isf(sdla_t *card, int isf);
+static int fr_send(sdla_t *card, int dlci, unsigned char attr, int len,
+	void *buf);
+
 /* Firmware asynchronous event handlers */
-static int fr_event(sdla_t * card, int event, fr_mbox_t * mbox);
-static int fr_modem_failure(sdla_t * card, fr_mbox_t * mbox);
-static int fr_dlci_change(sdla_t * card, fr_mbox_t * mbox);
+static int fr_event(sdla_t *card, int event, fr_mbox_t *mbox);
+static int fr_modem_failure(sdla_t *card, fr_mbox_t *mbox);
+static int fr_dlci_change(sdla_t *card, fr_mbox_t *mbox);
+
 /* Miscellaneous functions */
 static int update_chan_state(struct net_device *dev);
 static void set_chan_state(struct net_device *dev, int state);
-static struct net_device *find_channel(sdla_t * card, unsigned dlci);
-static int is_tx_ready(sdla_t * card, fr_channel_t * chan);
+static struct net_device *find_channel(sdla_t *card, unsigned dlci);
+static int is_tx_ready(sdla_t *card, fr_channel_t *chan);
 static unsigned int dec_to_uint(unsigned char *str, int len);
-static int reply_udp(unsigned char *data, unsigned int mbox_len);
-static int intr_test(sdla_t * card);
-static void init_chan_statistics(fr_channel_t * chan);
-static void init_global_statistics(sdla_t * card);
-static void read_DLCI_IB_mapping(sdla_t * card, fr_channel_t * chan);
+static int reply_udp( unsigned char *data, unsigned int mbox_len );
+
+static int intr_test( sdla_t* card );
+static void init_chan_statistics( fr_channel_t* chan );
+static void init_global_statistics( sdla_t* card );
+static void read_DLCI_IB_mapping( sdla_t* card, fr_channel_t* chan );
+static void setup_for_delayed_transmit(struct net_device* dev, void* buf,
+	unsigned len);
+
+
+/* Inverse ARP and Dynamic routing functions */
+int process_ARP(arphdr_1490_t *ArpPacket, sdla_t *card, struct net_device *dev);
+int is_arp(void *buf);
+int send_inarp_request(sdla_t *card, struct net_device *dev);
+
 /* Udp management functions */
-static int process_udp_mgmt_pkt(char udp_pkt_src, sdla_t * card, struct sk_buff *skb, struct net_device *dev, int dlci, fr_channel_t * chan);
-static int process_udp_driver_call(char udp_pkt_src, sdla_t * card, struct sk_buff *skb, struct net_device *dev, int dlci, fr_channel_t * chan);
-static int udp_pkt_type(struct sk_buff *skb, sdla_t * card);
+static int process_udp_mgmt_pkt(sdla_t *card);
+static int udp_pkt_type( struct sk_buff *skb, sdla_t *card );
+static int store_udp_mgmt_pkt(int udp_type, char udp_pkt_src, sdla_t* card,
+                                struct sk_buff *skb, int dlci);
+
 /* IPX functions */
-static void switch_net_numbers(unsigned char *sendpacket, unsigned long network_number, unsigned char incoming);
-static int handle_IPXWAN(unsigned char *sendpacket, char *devname, unsigned char enable_IPX, unsigned long network_number);
+static void switch_net_numbers(unsigned char *sendpacket,
+	unsigned long network_number, unsigned char incoming);
+
+static int handle_IPXWAN(unsigned char *sendpacket, char *devname,
+	unsigned char enable_IPX, unsigned long network_number);
+
+/* Lock Functions: SMP supported */
+void 	s508_s514_unlock(sdla_t *card, unsigned long *smp_flags);
+void 	s508_s514_lock(sdla_t *card, unsigned long *smp_flags);
+
+unsigned short calc_checksum (char *, int);
+
 
 /****** Public Functions ****************************************************/
 
@@ -313,154 +358,251 @@
  * Return:	0	o.k.
  *		< 0	failure.
  */
-int wpf_init(sdla_t * card, wandev_conf_t * conf)
+int wpf_init(sdla_t *card, wandev_conf_t *conf)
 {
-	union {
+
+	int err;
+
+	union
+	{
 		char str[80];
 		fr_conf_t cfg;
 	} u;
+	fr_buf_info_t* buf_info;
 	int i;
+
 	/* Verify configuration ID */
-	if (conf->config_id != WANCONFIG_FR) 
-	{
+	if (conf->config_id != WANCONFIG_FR) {
+		
 		printk(KERN_INFO "%s: invalid configuration ID %u!\n",
-		       card->devname, conf->config_id);
+			card->devname, conf->config_id);
 		return -EINVAL;
+	
 	}
+
 	/* Initialize protocol-specific fields of adapter data space */
-	switch (card->hw.fwid) 
-	{
-		case SFID_FR502:
-			card->mbox = (void *) (card->hw.dpmbase + FR502_MBOX_OFFS);
-			card->rxmb = (void *) (card->hw.dpmbase + FR502_RXMB_OFFS);
-			card->flags = (void *) (card->hw.dpmbase + FR502_FLAG_OFFS);
-			card->isr = &fr502_isr;
-			break;
+	switch (card->hw.fwid) {
+	
 		case SFID_FR508:
-			card->mbox = (void *) (card->hw.dpmbase + FR508_MBOX_OFFS);
-			card->flags = (void *) (card->hw.dpmbase + FR508_FLAG_OFFS);
-			card->isr = &fr508_isr;
+			card->mbox  = (void*)(card->hw.dpmbase + 
+					FR508_MBOX_OFFS);
+			card->flags = (void*)(card->hw.dpmbase + 
+					FR508_FLAG_OFFS);
+			if(card->hw.type == SDLA_S514) {
+				card->mbox += FR_MB_VECTOR;
+                                card->flags += FR_MB_VECTOR;
+			}
+                        card->isr = &fr_isr;
 			break;
+
 		default:
 			return -EINVAL;
 	}
+
 	/* 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 (fr_read_version(card, NULL) || fr_read_version(card, u.str))
 		return -EIO;
+
 	printk(KERN_INFO "%s: running frame relay firmware v%s\n",
-	       card->devname, u.str);
+		card->devname, u.str);
+
 	/* Adjust configuration */
-	conf->mtu = max(min(conf->mtu, 4080), FR_CHANNEL_MTU + FR_HEADER_LEN);
+	conf->mtu += FR_HEADER_LEN;
+	conf->mtu = (conf->mtu >= MIN_LGTH_FR_DATA_CFG) ?
+			min(conf->mtu, FR_MAX_NO_DATA_BYTES_IN_FRAME) :
+                        FR_CHANNEL_MTU + FR_HEADER_LEN;
+     
 	conf->bps = min(conf->bps, 2048000);
-	/* Configure adapter firmware */
+
+	/* Initialze the configuration structure sent to the board to zero */
 	memset(&u.cfg, 0, sizeof(u.cfg));
-	u.cfg.mtu = conf->mtu;
-	u.cfg.kbps = conf->bps / 1000;
-	u.cfg.cir_fwd = u.cfg.cir_bwd = 16;
-	u.cfg.bc_fwd = u.cfg.bc_bwd = 16;
-	if (conf->station == WANOPT_CPE) 
-	{
-		u.cfg.options = 0x0080;
-		printk(KERN_INFO "%s: Global CIR enabled by Default\n", card->devname);
-	}
-	else
-	{
-		u.cfg.options = 0x0081;
-	}
-	switch (conf->u.fr.signalling) 
-	{
-		case WANOPT_FR_Q933:
-			u.cfg.options |= 0x0200;
+
+	memset(card->u.f.dlci_to_dev_map, 0, sizeof(card->u.f.dlci_to_dev_map));
+ 	
+	/* Configure adapter firmware */
+	
+	u.cfg.mtu	= conf->mtu;
+	u.cfg.kbps	= conf->bps / 1000;
+
+    	u.cfg.cir_fwd = u.cfg.cir_bwd = 16;
+        u.cfg.bc_fwd  = u.cfg.bc_bwd = 16;
+	
+	u.cfg.options	= 0x0000;
+	printk(KERN_INFO "%s: Global CIR enabled by Default\n", card->devname);
+	
+	switch (conf->u.fr.signalling) {
+
+		case WANOPT_FR_ANSI:
+			u.cfg.options = 0x0000; 
+			break;		
+	
+		case WANOPT_FR_Q933:	
+			u.cfg.options |= 0x0200; 
+			break;
+	
+		case WANOPT_FR_LMI:	
+			u.cfg.options |= 0x0400; 
 			break;
-		case WANOPT_FR_LMI:
-			u.cfg.options |= 0x0400;
+
+		case WANOPT_NO:
+			u.cfg.options |= 0x0800; 
 			break;
+		default:
+			printk(KERN_INFO "%s: Illegal Signalling option\n",
+					card->wandev.name);
+			return -EINVAL;
 	}
-	if (conf->station == WANOPT_CPE) 
-	{
+
+
+	card->wandev.signalling = conf->u.fr.signalling;
+
+	if (conf->station == WANOPT_CPE) {
+
+
+		if (conf->u.fr.signalling == WANOPT_NO){
+			printk(KERN_INFO 
+				"%s: ERROR - For NO signalling, station must be set to Node!",
+				 	 card->devname);
+			return -EINVAL;
+		}
+
+		u.cfg.station = 0;
 		u.cfg.options |= 0x8000;	/* auto config DLCI */
-		card->u.f.dlci_num = 0;
-	}
-	else
-	{
+		card->u.f.dlci_num  = 0;
+	
+	} else {
+
 		u.cfg.station = 1;	/* switch emulation mode */
+
 		/* For switch emulation we have to create a list of dlci(s)
 		 * that will be sent to be global SET_DLCI_CONFIGURATION 
 		 * command in fr_configure() routine. 
 		 */
-		card->u.f.dlci_num = min(max(conf->u.fr.dlci_num, 1), 100);
-		for (i = 0; i < card->u.f.dlci_num; i++) 
-		{
-			card->u.f.node_dlci[i] = (unsigned short)
-			    conf->u.fr.dlci[i] ? conf->u.fr.dlci[i] : 16;
+
+		card->u.f.dlci_num  = min(max(conf->u.fr.dlci_num, 1), 100);
+	
+		for ( i = 0; i < card->u.f.dlci_num; i++) {
+
+			card->u.f.node_dlci[i] = (unsigned short) 
+				conf->u.fr.dlci[i] ? conf->u.fr.dlci[i] : 16;
+	
 		}
 	}
+
 	if (conf->clocking == WANOPT_INTERNAL)
 		u.cfg.port |= 0x0001;
+
 	if (conf->interface == WANOPT_RS232)
 		u.cfg.port |= 0x0002;
+
 	if (conf->u.fr.t391)
 		u.cfg.t391 = min(conf->u.fr.t391, 30);
 	else
 		u.cfg.t391 = 5;
+
 	if (conf->u.fr.t392)
 		u.cfg.t392 = min(conf->u.fr.t392, 30);
 	else
 		u.cfg.t392 = 15;
+
 	if (conf->u.fr.n391)
 		u.cfg.n391 = min(conf->u.fr.n391, 255);
 	else
 		u.cfg.n391 = 2;
+
 	if (conf->u.fr.n392)
 		u.cfg.n392 = min(conf->u.fr.n392, 10);
 	else
-		u.cfg.n392 = 3;
+		u.cfg.n392 = 3;	
+
 	if (conf->u.fr.n393)
 		u.cfg.n393 = min(conf->u.fr.n393, 10);
 	else
 		u.cfg.n393 = 4;
+
 	if (fr_configure(card, &u.cfg))
 		return -EIO;
-	if (card->hw.fwid == SFID_FR508) 
-	{
-		fr_buf_info_t *buf_info =
-		(void *) (card->hw.dpmbase + FR508_RXBC_OFFS);
-		card->rxmb = (void *) (buf_info->rse_next - FR_MB_VECTOR + card->hw.dpmbase);
-		card->u.f.rxmb_base = (void *) (buf_info->rse_base - FR_MB_VECTOR + card->hw.dpmbase);
-		card->u.f.rxmb_last = (void *) (buf_info->rse_base + (buf_info->rse_num - 1) * 
-				sizeof(fr_buf_ctl_t) - FR_MB_VECTOR + card->hw.dpmbase);
-		card->u.f.rx_base = buf_info->buf_base;
-		card->u.f.rx_top = buf_info->buf_top;
-	}
-	card->wandev.mtu = conf->mtu;
-	card->wandev.bps = conf->bps;
-	card->wandev.interface = conf->interface;
-	card->wandev.clocking = conf->clocking;
-	card->wandev.station = conf->station;
-	card->poll = &wpf_poll;
-	card->exec = &wpf_exec;
-	card->wandev.update = &update;
-	card->wandev.new_if = &new_if;
-	card->wandev.del_if = &del_if;
-	card->wandev.state = WAN_DISCONNECTED;
-	card->wandev.ttl = conf->ttl;
-	card->wandev.udp_port = conf->udp_port;
-	card->wandev.enable_tx_int = 0;
-	card->irq_dis_if_send_count = 0;
-	card->irq_dis_poll_count = 0;
-	card->wandev.enable_IPX = conf->enable_IPX;
-	if (conf->network_number)
-		card->wandev.network_number = conf->network_number;
-	else
-		card->wandev.network_number = 0xDEADBEEF;
+
+	if (card->hw.type == SDLA_S514) {
+	
+                buf_info = (void*)(card->hw.dpmbase + FR_MB_VECTOR +
+			FR508_RXBC_OFFS);
+
+                card->rxmb = (void*)(buf_info->rse_next + card->hw.dpmbase);
+
+                card->u.f.rxmb_base =
+                        (void*)(buf_info->rse_base + card->hw.dpmbase); 
+
+                card->u.f.rxmb_last =
+                        (void*)(buf_info->rse_base +
+                        (buf_info->rse_num - 1) * sizeof(fr_rx_buf_ctl_t) +
+                        card->hw.dpmbase);
+	}
+
+	else {	
+		buf_info = (void*)(card->hw.dpmbase + FR508_RXBC_OFFS);
+
+		card->rxmb = (void*)(buf_info->rse_next -
+			FR_MB_VECTOR + card->hw.dpmbase);
+		
+		card->u.f.rxmb_base =
+			(void*)(buf_info->rse_base -
+			FR_MB_VECTOR + card->hw.dpmbase);
+		
+		card->u.f.rxmb_last =
+			(void*)(buf_info->rse_base +
+			(buf_info->rse_num - 1) * sizeof(fr_rx_buf_ctl_t) -
+			FR_MB_VECTOR + card->hw.dpmbase);
+	}
+
+	card->u.f.rx_base = buf_info->buf_base;
+	card->u.f.rx_top  = buf_info->buf_top;
+
+	card->u.f.tx_interrupts_pending = 0;
+
+	card->wandev.mtu	= conf->mtu;
+	card->wandev.bps	= conf->bps;
+	card->wandev.interface	= conf->interface;
+	card->wandev.clocking	= conf->clocking;
+	card->wandev.station	= conf->station;
+	card->poll		= NULL; 
+	card->exec		= &wpf_exec;
+	card->wandev.update	= &update;
+	card->wandev.new_if	= &new_if;
+	card->wandev.del_if	= &del_if;
+	card->wandev.state	= WAN_DISCONNECTED;
+	card->wandev.ttl	= conf->ttl;
+        card->wandev.udp_port 	= conf->udp_port;       
+
 	/* Intialize global statistics for a card */
-	init_global_statistics(card);
-	TracingEnabled = 0;
-	return 0;
+	init_global_statistics( card );
+
+        card->TracingEnabled          = 0;
+
+	/* Interrupt Test */
+	Intr_test_counter = 0;
+	card->intr_mode = INTR_TEST_MODE;
+	err = intr_test( card );
+
+	if (err || (Intr_test_counter < MAX_INTR_TEST_COUNTER)) {
+		printk( 
+			"%s: Interrupt Test Failed, Counter: %i\n", 
+			card->devname, Intr_test_counter);
+		printk( "Please choose another interrupt\n");
+		err = -EIO;
+		return err;
+	}
+
+	printk(KERN_INFO "%s: Interrupt Test Passed, Counter: %i\n",
+			card->devname, Intr_test_counter);
+
+
+        return 0;
 }
 
 /******* WAN Device Driver Entry Points *************************************/
@@ -468,21 +610,38 @@
 /*============================================================================
  * Update device status & statistics.
  */
-
-static int update(wan_device_t * wandev)
+static int update (wan_device_t* wandev)
 {
-	sdla_t *card;
+	volatile sdla_t* card;
+	unsigned long timeout;
+	fr508_flags_t* flags;
+
 	/* sanity checks */
 	if ((wandev == NULL) || (wandev->private == NULL))
 		return -EFAULT;
+
 	if (wandev->state == WAN_UNCONFIGURED)
 		return -ENODEV;
-	if (test_and_set_bit(0, (void *) &wandev->critical))
+
+	if (test_bit(1, (void*)&wandev->critical))
 		return -EAGAIN;
+
 	card = wandev->private;
-	fr_get_err_stats(card);
-	fr_get_stats(card);
-	wandev->critical = 0;
+	flags = card->flags;
+
+
+	card->u.f.update_comms_stats = 1;
+	card->u.f.timer_int_enabled |= TMR_INT_ENABLED_UPDATE;
+	flags->imask |= FR_INTR_TIMER;
+       	timeout = jiffies;
+       	for(;;) {
+		if(card->u.f.update_comms_stats == 0)
+			break;
+                if ((jiffies - timeout) > (1 * HZ)){
+    			card->u.f.update_comms_stats = 0;
+ 			return -EAGAIN;
+		}
+        }
 	return 0;
 }
 
@@ -498,85 +657,216 @@
  * Return:	0	o.k.
  *		< 0	failure (channel will not be created)
  */
-
-static int new_if(wan_device_t * wandev, struct net_device *dev, wanif_conf_t * conf)
+static int new_if (wan_device_t* wandev, struct net_device* dev, wanif_conf_t* conf)
 {
-	sdla_t *card = wandev->private;
-	fr_channel_t *chan;
+	sdla_t* card = wandev->private;
+	fr_channel_t* chan;
+	int dlci = 0;
 	int err = 0;
-	if ((conf->name[0] == '\0') || (strlen(conf->name) > WAN_IFNAME_SZ)) 
-	{
+	
+	if ((conf->name[0] == '\0') || (strlen(conf->name) > WAN_IFNAME_SZ)) {
+		
 		printk(KERN_INFO "%s: invalid interface name!\n",
-		       card->devname);
+			card->devname);
 		return -EINVAL;
 	}
+
 	/* allocate and initialize private data */
 	chan = kmalloc(sizeof(fr_channel_t), GFP_KERNEL);
+
 	if (chan == NULL)
 		return -ENOMEM;
+
 	memset(chan, 0, sizeof(fr_channel_t));
 	strcpy(chan->name, conf->name);
 	chan->card = card;
+ 
 	/* verify media address */
-	if (is_digit(conf->addr[0])) 
-	{
-		int dlci = dec_to_uint(conf->addr, 0);
-		if (dlci && (dlci <= 4095)) 
-		{
+	if (is_digit(conf->addr[0])) {
+
+		dlci = dec_to_uint(conf->addr, 0);
+
+		if (dlci && (dlci <= HIGHEST_VALID_DLCI)) {
+		
 			chan->dlci = dlci;
-		}
-		else
-		{
-			printk(KERN_ERR "%s: invalid DLCI %u on interface %s!\n",
-			       wandev->name, dlci, chan->name);
+		
+		} else {
+		
+			printk(KERN_ERR
+				"%s: invalid DLCI %u on interface %s!\n",
+				wandev->name, dlci, chan->name);
 			err = -EINVAL;
 		}
-	} 
-	else 
-	{
-		printk(KERN_ERR "%s: invalid media address on interface %s!\n",
-		       wandev->name, chan->name);
+
+	} else {
+		printk(KERN_ERR
+			"%s: invalid media address on interface %s!\n",
+			wandev->name, chan->name);
 		err = -EINVAL;
 	}
-	if (err) 
-	{
+
+        /* Setup wanpipe as a router (WANPIPE) or as an API */
+        if(strcmp(conf->usedby, "WANPIPE") == 0){
+                printk(KERN_INFO "%s: Running in WANPIPE mode %s\n",
+			wandev->name, chan->name);
+                chan->usedby = WANPIPE;
+
+        } else if(strcmp(conf->usedby, "API") == 0){
+
+#ifdef FRAME_RELAY_API
+                chan->usedby = API;
+                printk(KERN_INFO "%s: Running in API mode %s\n",
+			wandev->name, chan->name);
+#else
+                printk(KERN_INFO "%s: API Mode is not supported !\n",
+			wandev->name);
+                printk(KERN_INFO
+			"%s: API patch can be obtained from Sangoma Tech.\n",
+                        	wandev->name);
+                err = -EINVAL;
+#endif
+        }
+
+	if (err) {
+		
 		kfree(chan);
 		return err;
 	}
+
+	card->u.f.dlci_to_dev_map[dlci] = dev;
+ 
 	/* place cir,be,bc and other channel specific information into the
 	 * chan structure 
-	 */
-	if (conf->cir) 
-	{
-		chan->cir = max(1, min(conf->cir, 512));
-		chan->cir_status = CIR_ENABLED;
-		if (conf->bc)
-			chan->bc = max(1, min(conf->bc, 512));
-		if (conf->be)
-			chan->be = max(0, min(conf->be, 511));
-	}
-	else
+         */
+	if (conf->cir) {
+
+		chan->cir = max( 1, min( conf->cir, 512 ) );
+		chan->cir_status = CIR_ENABLED; 
+
+		
+		/* If CIR is enabled, force BC to equal CIR
+                 * this solves number of potential problems if CIR is 
+                 * set and BC is not 
+		 */
+		chan->bc = chan->cir;
+
+		if (conf->be){
+			chan->be = max( 0, min( conf->be, 511) ); 
+		}else{	
+			conf->be = 0;
+		}
+
+		printk (KERN_INFO "%s: CIR enabled for DLCI %i \n",
+				wandev->name,chan->dlci);
+		printk (KERN_INFO "%s:     CIR = %i ; BC = %i ; BE = %i\n",
+				wandev->name,chan->cir,chan->bc,chan->be);
+
+
+	}else{
 		chan->cir_status = CIR_DISABLED;
+		printk (KERN_INFO "%s: CIR disabled for DLCI %i\n",
+				wandev->name,chan->dlci);
+	}
+
 	chan->mc = conf->mc;
-	chan->dlci_configured = DLCI_NOT_CONFIGURED;
-	chan->tx_int_status = DISABLED;
+
+	/* FIXME: ARP is not supported by this frame relay verson */
+	if (conf->inarp == WANOPT_YES){
+		printk(KERN_INFO "%s: ERROR - This version of WANPIPE doesn't support ARPs\n",
+				card->devname);
+	
+		//chan->inarp = conf->inarp ? INARP_REQUEST : INARP_NONE;
+		//chan->inarp_interval = conf->inarp_interval ? conf->inarp_interval : 10;
+		kfree(chan);
+		return -EINVAL;
+	}else{
+		chan->inarp = INARP_NONE;
+		chan->inarp_interval = 10;
+	}
+
+	chan->dlci_configured = DLCI_NOT_CONFIGURED;	
+
+
+	/*FIXME: IPX disabled in this WANPIPE version */
+	if (conf->enable_IPX == WANOPT_YES){
+		printk(KERN_INFO "%s: ERROR - This version of WANPIPE doesn't support IPX\n",
+				card->devname);
+		kfree(chan);
+		return -EINVAL;
+	}else{
+		chan->enable_IPX = WANOPT_NO;
+	}	
+
+	if (conf->network_number){
+		chan->network_number = conf->network_number;
+	}else{
+		chan->network_number = 0xDEADBEEF;
+	}
+
+	chan->route_flag = NO_ROUTE;
+	
 	init_chan_statistics(chan);
+
+	chan->transmit_length = 0;
+
 	/* prepare network device data space for registration */
 	dev->name = chan->name;
 	dev->init = &if_init;
 	dev->priv = chan;
+
+
+	/* Enable Interrupts and Communications */
+	if (!wandev->new_if_cnt){
+		fr508_flags_t* flags = card->flags;
+
+		wandev->new_if_cnt++;
+
+		/* 
+		If you enable comms and then set ints, you get a Tx int as you
+		perform the SET_INT_TRIGGERS command. So, we only set int
+		triggers and then adjust the interrupt mask (to disable Tx ints)
+		before enabling comms. 
+		*/	
+                if (fr_set_intr_mode(card, (FR_INTR_RXRDY | FR_INTR_TXRDY |
+			FR_INTR_DLC | FR_INTR_TIMER | FR_INTR_TX_MULT_DLCIs) ,
+			card->wandev.mtu, 0)) {
+			kfree(chan);
+			return -EIO;
+		}
+
+		flags->imask &= ~(FR_INTR_TXRDY | FR_INTR_TIMER);
+ 
+		if (fr_comm_enable(card)) {
+			kfree(chan);
+			return -EIO;
+		}	
+		wanpipe_set_state(card, WAN_CONNECTED);
+	}
+
 	return 0;
 }
+
 /*============================================================================
  * Delete logical channel.
  */
-static int del_if(wan_device_t * wandev, struct net_device *dev)
+static int del_if (wan_device_t* wandev, struct net_device* dev)
 {
-	if (dev->priv) 
-	{
+	sdla_t *card = wandev->private;
+
+	/* Execute shutdown very first time we enter del_if */
+
+	if (!wandev->del_if_cnt) {
+		wandev->del_if_cnt++;
+		wanpipe_set_state(card, WAN_DISCONNECTED);
+		fr_set_intr_mode(card, 0, 0, 0);
+		fr_comm_disable(card);
+	}
+
+	if (dev->priv) {
 		kfree(dev->priv);
 		dev->priv = NULL;
 	}
+
 	return 0;
 }
 
@@ -585,41 +875,47 @@
 /*============================================================================
  * Execute adapter interface command.
  */
-static int wpf_exec(struct sdla *card, void *u_cmd, void *u_data)
+static int wpf_exec (struct sdla* card, void* u_cmd, void* u_data)
 {
-	fr_mbox_t *mbox = card->mbox;
+	fr_mbox_t* mbox = card->mbox;
 	int retry = MAX_CMD_RETRY;
 	int err, len;
 	fr_cmd_t cmd;
-	if(copy_from_user((void *) &cmd, u_cmd, sizeof(cmd)))
-		return -EFAULT;		
+
+	if(copy_from_user((void*)&cmd, u_cmd, sizeof(cmd)))
+		return -EFAULT;
+	
 	/* execute command */
-	do 
+	do
 	{
 		memcpy(&mbox->cmd, &cmd, sizeof(cmd));
-		if (cmd.length)
-		{
-			if(copy_from_user((void *) &mbox->data, u_data, cmd.length))
+		
+		if (cmd.length){
+			if( copy_from_user((void*)&mbox->data, u_data, cmd.length))
 				return -EFAULT;
 		}
+		
 		if (sdla_exec(mbox))
 			err = mbox->cmd.result;
-		else
-			return -EIO;
-	} 
-	while (err && retry-- && fr_event(card, err, mbox));
 
-	/* return result */
+		else return -EIO;
+	
+	} while (err && retry-- && fr_event(card, err, mbox));
 
-	if(copy_to_user(u_cmd, (void *) &mbox->cmd, sizeof(fr_cmd_t)))
+	/* return result */
+	if (copy_to_user(u_cmd, (void*)&mbox->cmd, sizeof(fr_cmd_t)))
 		return -EFAULT;
+
 	len = mbox->cmd.length;
-	if (len && u_data && copy_to_user(u_data, (void *) &mbox->data, len))
+
+	if (len && u_data && !copy_to_user(u_data, (void*)&mbox->data, len))
 		return -EFAULT;
 	return 0;
+
 }
 
 /****** Network Device Interface ********************************************/
+
 /*============================================================================
  * Initialize Linux network interface.
  *
@@ -627,35 +923,55 @@
  * interface registration.  Returning anything but zero will fail interface
  * registration.
  */
-static int if_init(struct net_device *dev)
+static int if_init (struct net_device* dev)
 {
-	fr_channel_t *chan = dev->priv;
-	sdla_t *card = chan->card;
-	wan_device_t *wandev = &card->wandev;
+	fr_channel_t* chan = dev->priv;
+	sdla_t* card = chan->card;
+	wan_device_t* wandev = &card->wandev;
 
 	/* 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;
+	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;
+
 	/* Initialize media-specific parameters */
-	dev->type = ARPHRD_DLCI;			/* ARP h/w type */
-	dev->mtu = FR_CHANNEL_MTU;
-	dev->hard_header_len = FR_HEADER_LEN;		/* media header length */
-	dev->addr_len = 2;				/* hardware address length */
-	*(unsigned short *) dev->dev_addr = htons(chan->dlci);
+	dev->type		= ARPHRD_DLCI;	/* ARP h/w type */
+	dev->flags		|= IFF_POINTOPOINT;	
+
+	/* Enable Multicast addressing */
+	if (chan->mc == WANOPT_YES){
+		dev->flags 	|= IFF_MULTICAST;
+	}
+
+	dev->mtu		= wandev->mtu - FR_HEADER_LEN;
+	/* For an API, the maximum number of bytes that the stack will pass
+	   to the driver is (dev->mtu + dev->hard_header_len). So, adjust the
+	   mtu so that a frame of maximum size can be transmitted by the API. 
+	*/
+	if(chan->usedby == API) {
+		dev->mtu += (sizeof(api_tx_hdr_t) - FR_HEADER_LEN);
+	}
+	
+	dev->hard_header_len	= FR_HEADER_LEN;/* media header length */
+	dev->addr_len		= 2; 		/* hardware address length */
+	*(unsigned short*)dev->dev_addr = htons(chan->dlci);
+
 	/* 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;
-	dev->mem_end = dev->mem_start + wandev->msize - 1;
-	/* Set transmit buffer queue length */
-	dev->tx_queue_len = 10;
+	dev->irq	= wandev->irq;
+	dev->dma	= wandev->dma;
+	dev->base_addr	= wandev->ioport;
+	dev->mem_start	= wandev->maddr;
+	dev->mem_end	= wandev->maddr + wandev->msize - 1;
+
+        /* Set transmit buffer queue length */
+        dev->tx_queue_len = 100;
+   
 	/* Initialize socket buffers */
 	dev_init_buffers(dev);
+
 	set_chan_state(dev, WAN_DISCONNECTED);
 	return 0;
 }
@@ -667,127 +983,62 @@
  *
  * Return 0 if O.k. or errno.
  */
-
-static int if_open(struct net_device *dev)
+static int if_open (struct net_device* dev)
 {
-	fr_channel_t *chan = dev->priv;
-	sdla_t *card = chan->card;
-	struct net_device *dev2;
+	fr_channel_t* chan = dev->priv;
+	sdla_t* card = chan->card;
 	int err = 0;
-	fr508_flags_t *flags = card->flags;
 	struct timeval tv;
+
 	if (dev->start)
-		return -EBUSY;	/* only one open is allowed */
-	if (test_and_set_bit(0, (void *) &card->wandev.critical))
+		return -EBUSY;		/* only one open is allowed */
+
+	if (test_and_set_bit(1, (void*)&card->wandev.critical))
 		return -EAGAIN;
-	if (!card->open_cnt) 
-	{
-		Intr_test_counter = 0;
-		card->intr_mode = INTR_TEST_MODE;
-		err = intr_test(card);
-		if ((err) || (Intr_test_counter != (MAX_INTR_TEST_COUNTER + 1))) {
-			printk(KERN_INFO
-			       "%s: Interrupt Test Failed, Counter: %i\n",
-			       card->devname, Intr_test_counter);
-			err = -EIO;
-			card->wandev.critical = 0;
-			return err;
-		}
-		printk(KERN_INFO "%s: Interrupt Test Passed, Counter: %i\n"
-		       ,card->devname, Intr_test_counter);
-		/* The following allocates and intializes a circular
-		 * link list of interfaces per card.
-		 */
-		card->devs_struct = kmalloc(sizeof(load_sharing_t), GFP_KERNEL);
-		if (card->devs_struct == NULL)
-			return -ENOMEM;
-		card->dev_to_devtint_next = card->devs_struct;
-		for (dev2 = card->wandev.dev; dev2; dev2 = dev2->slave) {
-			(card->devs_struct)->dev_ptr = dev2;
-			if (dev2->slave == NULL)
-				(card->devs_struct)->next = card->dev_to_devtint_next;
-			else {
-				(card->devs_struct)->next = kmalloc(
-				     sizeof(load_sharing_t), GFP_KERNEL);
-				if ((card->devs_struct)->next == NULL)
-					return -ENOMEM;
-				card->devs_struct = (card->devs_struct)->next;
-			}
-		}
-		card->devs_struct = card->dev_to_devtint_next;
-		card->intr_mode = BUFFER_INTR_MODE;
-		/* 
-		   check all the interfaces for the device to see if CIR has
-		   been enabled for any DLCI(s). If so then use the DLCI list
-		   Interrupt mode for fr_set_intr_mode(), otherwise use the                     default global interrupt mode
-		 */
-		for (dev2 = card->wandev.dev; dev2; dev2 = dev2->slave) {
-			if (((fr_channel_t *) dev2->priv)->cir_status
-			    == CIR_ENABLED) {
-				card->intr_mode = DLCI_LIST_INTR_MODE;
-				break;
-			}
-		}
-		/* 
-		   If you enable comms and then set ints, you get a Tx int as you
-		   perform the SET_INT_TRIGGERS command. So, we only set int
-		   triggers and then adjust the interrupt mask (to disable Tx ints)             before enabling comms. 
-		 */
-		if (card->intr_mode == BUFFER_INTR_MODE) {
-			if (fr_set_intr_mode(card, 0x03, card->wandev.mtu)) {
-				err = -EIO;
-				card->wandev.critical = 0;
-				return err;
-			}
-			printk(KERN_INFO
-			       "%s: Global Buffering Tx Interrupt Mode\n"
-			       ,card->devname);
-		} else if (card->intr_mode == DLCI_LIST_INTR_MODE) {
-			if (fr_set_intr_mode(card, 0x83, card->wandev.mtu)) {
-				err = -EIO;
-				card->wandev.critical = 0;
-				return err;
-			}
-			printk(KERN_INFO
-			       "%s: DLCI list Tx Interrupt Mode\n",
-			       card->devname);
-		}
-		flags->imask &= ~0x02;
-		if (fr_comm_enable(card)) {
-			err = -EIO;
-			card->wandev.critical = 0;
-			return err;
-		}
-		wanpipe_set_state(card, WAN_CONNECTED);
-		if (card->wandev.station == WANOPT_CPE) {
-			/* CPE: issue full status enquiry */
-			fr_issue_isf(card, FR_ISF_FSE);
-		} else {	/* FR switch: activate DLCI(s) */
-			/* For Switch emulation we have to ADD and ACTIVATE
-			 * the DLCI(s) that were configured with the SET_DLCI_
-			 * CONFIGURATION command. Add and Activate will fail if
-			 * DLCI specified is not included in the list.
-			 *
-			 * Also If_open is called once for each interface. But
-			 * it does not get in here for all the interface. So
-			 * we have to pass the entire list of DLCI(s) to add 
-			 * activate routines.  
-			 */
-			fr_add_dlci(card,
-			     card->u.f.node_dlci[0], card->u.f.dlci_num);
-			fr_activate_dlci(card,
-			     card->u.f.node_dlci[0], card->u.f.dlci_num);
+	
+
+	/* If signalling is set to NO, then setup 
+         * DLCI addresses right away.  Don't have to wait for
+	 * link to connect. 
+	 */
+	if (card->wandev.signalling == WANOPT_NO){
+		printk(KERN_INFO "%s: Signalling set to NO: Mapping DLCI's\n",
+					card->wandev.name);
+		if (fr_init_dlci(card,chan)){
+			return -EAGAIN;
 		}
 	}
-	dev->mtu = min(dev->mtu, card->wandev.mtu - FR_HEADER_LEN);
+
+	if (card->wandev.station == WANOPT_CPE) {
+
+		/* CPE: issue full status enquiry */
+		fr_issue_isf(card, FR_ISF_FSE);
+
+	} else {	/* FR switch: activate DLCI(s) */
+		
+		/* For Switch emulation we have to ADD and ACTIVATE
+		 * the DLCI(s) that were configured with the SET_DLCI_
+		 * CONFIGURATION command. Add and Activate will fail if
+		 * DLCI specified is not included in the list.
+		 *
+		 * Also If_open is called once for each interface. But
+		 * it does not get in here for all the interface. So
+	 	 * we have to pass the entire list of DLCI(s) to add 
+		 * activate routines.  
+		 */ 
+	
+		fr_add_dlci(card, chan->dlci);
+		fr_activate_dlci(card, chan->dlci);
+	}
+
 	dev->interrupt = 0;
 	dev->tbusy = 0;
 	dev->start = 1;
 	wanpipe_open(card);
 	update_chan_state(dev);
-	do_gettimeofday(&tv);
+	do_gettimeofday( &tv );
 	chan->router_start_time = tv.tv_sec;
-	card->wandev.critical = 0;
+        clear_bit(1, (void*)&card->wandev.critical);
 	return err;
 }
 
@@ -796,22 +1047,21 @@
  * o if this is the last open, then disable communications and interrupts.
  * o reset flags.
  */
-
-static int if_close(struct net_device *dev)
+static int if_close (struct net_device* dev)
 {
-	fr_channel_t *chan = dev->priv;
-	sdla_t *card = chan->card;
-	if (test_and_set_bit(0, (void *) &card->wandev.critical))
+	fr_channel_t* chan = dev->priv;
+	sdla_t* card = chan->card;
+
+	if (test_and_set_bit(1, (void*)&card->wandev.critical))
 		return -EAGAIN;
+
 	dev->start = 0;
 	wanpipe_close(card);
-	if (!card->open_cnt) 
-	{
-		wanpipe_set_state(card, WAN_DISCONNECTED);
-		fr_set_intr_mode(card, 0, 0);
-		fr_comm_disable(card);
+	if (card->wandev.station == WANOPT_NODE) {
+		fr_delete_dlci (card,chan->dlci);
 	}
-	card->wandev.critical = 0;
+
+        clear_bit(1, (void*)&card->wandev.critical);
 	return 0;
 }
 
@@ -825,15 +1075,15 @@
  *
  * Return:	media header length.
  */
-
-static int if_header(struct sk_buff *skb, struct net_device *dev,
-	     unsigned short type, void *daddr, void *saddr, unsigned len)
+static int if_header (struct sk_buff* skb, struct net_device* dev,
+	unsigned short type, void* daddr, void* saddr, unsigned len)
 {
 	int hdr_len = 0;
+	
 	skb->protocol = type;
 	hdr_len = wanrouter_encapsulate(skb, dev);
-	if (hdr_len < 0) 
-	{
+
+	if (hdr_len < 0) {
 		hdr_len = 0;
 		skb->protocol = 0;
 	}
@@ -849,14 +1099,14 @@
  * Return:	1	physical address resolved.
  *		0	physical address not resolved
  */
-
-static int if_rebuild_hdr(struct sk_buff *skb)
+static int if_rebuild_hdr (struct sk_buff* skb)
 {
-	struct net_device *dev=skb->dev;
-	fr_channel_t *chan = dev->priv;
-	sdla_t *card = chan->card;
+	struct net_device *dev = skb->dev;
+	fr_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);
+		card->devname, dev->name);
 	return 1;
 }
 
@@ -864,6 +1114,7 @@
  * Send a packet on a network interface.
  * o set tbusy flag (marks start of the transmission) to block a timer-based
  *   transmit from overlapping.
+ * o set critical flag when accessing board.
  * 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.
@@ -878,876 +1129,951 @@
  * 2. Setting tbusy flag will inhibit further transmit requests from the
  *    protocol stack and can be used for flow control with protocol layer.
  */
-
-static int if_send(struct sk_buff *skb, struct net_device *dev)
+static int if_send (struct sk_buff* skb, struct net_device* dev)
 {
-	fr_channel_t *chan = dev->priv;
-	sdla_t *card = chan->card;
-	int retry = 0, err;
-	unsigned char *sendpacket;
-	struct net_device *dev2;
-	unsigned long check_braddr, check_mcaddr;
-	fr508_flags_t *adptr_flags = card->flags;
+    	fr_channel_t* chan = dev->priv;
+    	sdla_t* card = chan->card;
+        int err;
+    	unsigned char *sendpacket;
+    	fr508_flags_t* adptr_flags = card->flags;
 	int udp_type, send_data;
-	fr_dlci_interface_t *dlci_interface = chan->dlci_int_interface;
-	unsigned long host_cpu_flags;
-	++chan->if_send_entry;
+	unsigned long smp_flags=0;
+	void* data;
+	unsigned len;
+
+	chan->drvstats_if_send.if_send_entry++;
+
+        if (skb == NULL) {             
+		/* if we get here, some higher layer thinks we've missed an
+		 * tx-done interrupt.
+		 */
+		printk(KERN_INFO "%s: interface %s got kicked!\n", 
+			card->devname, dev->name);
+		chan->drvstats_if_send.if_send_skb_null ++;
+		mark_bh(NET_BH);
+		return 0;
+	}
+
+	/* We must set the 'tbusy' flag if we already have a packet queued for
+	   transmission in the transmit interrupt handler. However, we must
+	   ensure that the transmit interrupt does not reset the 'tbusy' flag
+	   just before we set it, as this will result in a "transmit timeout".
+	*/
+	set_bit(2, (void*)&card->wandev.critical);
+        if(chan->transmit_length) {
+        	dev->tbusy = 1;
+	        chan->tick_counter = jiffies;
+ 		clear_bit(2, (void*)&card->wandev.critical);
+		return 1;
+	}
+       	clear_bit(2, (void*)&card->wandev.critical);
+  
+    	if (dev->tbusy) {
 
-	if (dev->tbusy) 
-	{
 		/* 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_busy;
+                 * 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->drvstats_if_send.if_send_tbusy++;
 		++chan->ifstats.collisions;
-		if ((jiffies - chan->tick_counter) < (5 * HZ))
+
+		if ((jiffies - chan->tick_counter) < (5 * HZ)) {
 			return 1;
+		}
 
 		printk(KERN_INFO "%s: Transmit timed out\n", chan->name);
-		++chan->if_send_busy_timeout;
-		/* unbusy all the interfaces on the card */
-		for (dev2 = card->wandev.dev; dev2; dev2 = dev2->slave)
-			dev2->tbusy = 0;
-	}
+		chan->drvstats_if_send.if_send_tbusy_timeout ++;
+		dev->tbusy = 0;
+    	}
+
+	data = skb->data;
 	sendpacket = skb->data;
+        len = skb->len;
+
 	udp_type = udp_pkt_type(skb, card);
-	if (udp_type == UDP_DRVSTATS_TYPE) 
-	{
-		++chan->if_send_DRVSTATS_request;
-		process_udp_driver_call(UDP_PKT_FRM_STACK, card, skb, dev, 0,
-					chan);
-		dev_kfree_skb(skb);
-		return 0;
-	}
-	else if (udp_type == UDP_FPIPE_TYPE)
-		++chan->if_send_FPIPE_request;
-	/* retreive source address in two forms: broadcast & multicast */
-	check_braddr = sendpacket[17];
-	check_mcaddr = sendpacket[14];
-	check_braddr = check_braddr << 8;
-	check_mcaddr = check_mcaddr << 8;
-	check_braddr |= sendpacket[16];
-	check_mcaddr |= sendpacket[15];
-	check_braddr = check_braddr << 8;
-	check_mcaddr = check_mcaddr << 8;
-	check_braddr |= sendpacket[15];
-	check_mcaddr |= sendpacket[16];
-	check_braddr = check_braddr << 8;
-	check_mcaddr = check_mcaddr << 8;
-	check_braddr |= sendpacket[14];
-	check_mcaddr |= sendpacket[17];
-	/* if the Source Address is a Multicast address */
-	if ((chan->mc == WANOPT_NO) && (check_mcaddr >= 0xE0000001) &&
-	    (check_mcaddr <= 0xFFFFFFFE)) 
-	{
-		printk(KERN_INFO "%s: Multicast Src. Addr. silently discarded\n"
-		       ,card->devname);
-		dev_kfree_skb(skb);
-		++chan->ifstats.tx_dropped;
-		++chan->if_send_multicast;
+
+        if(udp_type != UDP_INVALID_TYPE) {
+                if(store_udp_mgmt_pkt(udp_type, UDP_PKT_FRM_STACK, card, skb,
+                        chan->dlci)) {
+                        adptr_flags->imask |= FR_INTR_TIMER;
+                        if (udp_type == UDP_FPIPE_TYPE){
+                                chan->drvstats_if_send.
+					if_send_PIPE_request ++;
+			}
+                }
 		return 0;
 	}
-	disable_irq(card->hw.irq);
-	++card->irq_dis_if_send_count;
-	if (test_and_set_bit(0, (void *) &card->wandev.critical)) 
-	{
-		if (card->wandev.critical == CRITICAL_IN_ISR) 
-		{
-			++chan->if_send_critical_ISR;
-			if (card->intr_mode == DLCI_LIST_INTR_MODE) 
-			{
-				/* The enable_tx_int flag is set here so that if
-				 * the critical flag is set due to an interrupt 
-				 * then we want to enable transmit interrupts 
-				 * again.
-				 */
-				card->wandev.enable_tx_int = 1;
-				/* Setting this flag to WAITING_TO_BE_ENABLED 
-				 * specifies that interrupt bit has to be 
-				 * enabled for that particular interface. 
-				 * (delayed interrupt)
-				 */
-				chan->tx_int_status = WAITING_TO_BE_ENABLED;
-				/* This is used for enabling dynamic calculation
-				 * of CIRs relative to the packet length.
-				 */
-				chan->pkt_length = skb->len;
-				dev->tbusy = 1;
-				chan->tick_counter = jiffies;
-			}
-			else
-			{
-				card->wandev.enable_tx_int = 1;
-				dev->tbusy = 1;
-				chan->tick_counter = jiffies;
-			}
-			save_flags(host_cpu_flags);
-			cli();
-			if ((!(--card->irq_dis_if_send_count)) &&
-			    (!card->irq_dis_poll_count))
-				enable_irq(card->hw.irq);
-			restore_flags(host_cpu_flags);
-			return 1;
-		}
-		++chan->if_send_critical_non_ISR;
-		++chan->ifstats.tx_dropped;
+
+	if((chan->usedby == API) && (len <= sizeof(api_tx_hdr_t))) {
+		//FIXME: increment some error statistic
+                dev_kfree_skb(skb);
+                return 0;
+ 	}
+ 
+	//FIXME: can we do better than sendpacket[2]?
+  	if ((chan->usedby == WANPIPE) && (sendpacket[2] == 0x45)) {
+               	/* check to see if the source IP address is a broadcast or */
+                /* multicast IP address */
+                if(chk_bcast_mcast_addr(card, dev, skb))
+                        return 0;
+	}
+
+	/* Lock the 508 card: SMP Supported */
+    	s508_s514_lock(card,&smp_flags);
+
+	if (test_and_set_bit(0, (void*)&card->wandev.critical)) {
+		chan->drvstats_if_send.if_send_critical_non_ISR ++;
+		chan->ifstats.tx_dropped ++;
+		printk(KERN_INFO "%s Critical in IF_SEND %02X\n", 
+				card->devname, card->wandev.critical);
 		dev_kfree_skb(skb);
-		save_flags(host_cpu_flags);
-		cli();
-		if ((!(--card->irq_dis_if_send_count)) &&
-		    (!card->irq_dis_poll_count))
-			enable_irq(card->hw.irq);
-		restore_flags(host_cpu_flags);
+		/* Unlock the 508 card */
+               	s508_s514_unlock(card,&smp_flags);
 		return 0;
 	}
-	card->wandev.critical = 0x21;
-	if (udp_type == UDP_FPIPE_TYPE) 
-	{
-		err = process_udp_mgmt_pkt(UDP_PKT_FRM_STACK, card, skb,
-					   dev, 0, chan);
-	}
-	else if (card->wandev.state != WAN_CONNECTED) 
-	{
-		++chan->if_send_wan_disconnected;
+ 	
+	if (card->wandev.state != WAN_CONNECTED) {
+		chan->drvstats_if_send.if_send_wan_disconnected ++;
 		++chan->ifstats.tx_dropped;
-		++card->wandev.stats.tx_dropped;
-	}
-	else if (chan->state != WAN_CONNECTED) 
-	{
-		++chan->if_send_dlci_disconnected;
+        	++card->wandev.stats.tx_dropped;
+	
+	} else if (chan->state != WAN_CONNECTED) {
+		chan->drvstats_if_send.if_send_dlci_disconnected ++;
+		/* Critical area on 514, since disabl_irq is not used
+                 * thus, interrupt would execute a command at
+		 * the same time as if_send.
+                 */
+     		set_bit(1, (void*)&card->wandev.critical);
 		update_chan_state(dev);
-		++chan->ifstats.tx_dropped;
-		++card->wandev.stats.tx_dropped;
-	}
-	else if (!is_tx_ready(card, chan)) 
-	{
-		if (card->intr_mode == DLCI_LIST_INTR_MODE) 
-		{
-			dlci_interface->gen_interrupt |= 0x40;
-			dlci_interface->packet_length = skb->len;
-		}
-		dev->tbusy = 1;
-		chan->tick_counter = jiffies;
-		adptr_flags->imask |= 0x02;
-		++chan->if_send_no_bfrs;
-		retry = 1;
-	}
-	else
-	{
+		clear_bit(1, (void*)&card->wandev.critical);
+        	++chan->ifstats.tx_dropped;
+        	++card->wandev.stats.tx_dropped;
+
+	} else if (!is_tx_ready(card, chan)) {
+		setup_for_delayed_transmit(dev, data, len);
+		chan->drvstats_if_send.if_send_no_bfrs++;
+ 	} else {
 		send_data = 1;
-		/* If it's an IPX packet */
-		if (sendpacket[1] == 0x00 &&
-		    sendpacket[2] == 0x80 &&
-		    sendpacket[6] == 0x81 &&
-		    sendpacket[7] == 0x37) 
-		{
-			if (card->wandev.enable_IPX) 
-			{
-				switch_net_numbers(sendpacket,
-					 card->wandev.network_number, 0);
-			} 
-			else 
-			{
-				/* increment some statistic here! */
+		//FIXME: IPX is not implemented in this version of Frame Relay ?
+		if((chan->usedby == WANPIPE) &&
+		 	sendpacket[1] == 0x00 &&
+		    	sendpacket[2] == 0x80 &&
+		    	sendpacket[6] == 0x81 &&
+		    	sendpacket[7] == 0x37) {
+			
+			if( chan->enable_IPX ) {
+				switch_net_numbers(sendpacket, 
+						chan->network_number, 0);
+			} else {
+				//FIXME: Take this out when IPX is fixed 
+				printk(KERN_INFO 
+				"%s: WARNING: Unsupported IPX data in send, packet dropped\n",
+					card->devname);
 				send_data = 0;
 			}
 		}
-		if (send_data) 
-		{
-			err = (card->hw.fwid == SFID_FR508) ?
-			    fr508_send(card, chan->dlci, 0, skb->len, skb->data) :
-			    fr502_send(card, chan->dlci, 0, skb->len, skb->data);
-			if (err) 
-			{
-				if (card->intr_mode == DLCI_LIST_INTR_MODE) 
-				{
-					dlci_interface->gen_interrupt |= 0x40;
-					dlci_interface->packet_length = skb->len;
+
+		if (send_data) {
+			unsigned char attr = 0;
+
+			/* For an API transmission, get rid of the API header */
+                        if (chan->usedby == API) {
+                                api_tx_hdr_t* api_tx_hdr;
+           			api_tx_hdr = (api_tx_hdr_t*)&skb->data[0x00];
+                      		attr = api_tx_hdr->attr;
+				data += sizeof(api_tx_hdr_t);
+				len -= sizeof(api_tx_hdr_t);
+			}
+
+        		err = fr_send(card, chan->dlci, attr, len, data);
+			if (err) {
+				switch(err) {
+				case FRRES_CIR_OVERFLOW:
+				case FRRES_BUFFER_OVERFLOW:
+                			setup_for_delayed_transmit(dev, data,
+						len);
+           				chan->drvstats_if_send.
+						if_send_adptr_bfrs_full ++;
+					break;	
+				default:
+					chan->drvstats_if_send.
+						if_send_dlci_disconnected ++;
+        				++chan->ifstats.tx_dropped;
+        				++card->wandev.stats.tx_dropped;
+					break;
 				}
-				dev->tbusy = 1;
-				chan->tick_counter = jiffies;
-				adptr_flags->imask |= 0x02;
-				retry = 1;
-				++chan->if_send_adptr_bfrs_full;
-				++chan->ifstats.tx_errors;
-				++card->wandev.stats.tx_errors;
-			}
-			else 
-			{
-				++chan->if_send_bfrs_passed_to_adptr;
+			} else {
+				chan->drvstats_if_send.
+					if_send_bfr_passed_to_adptr++;
 				++chan->ifstats.tx_packets;
 				++card->wandev.stats.tx_packets;
-				chan->ifstats.tx_bytes += skb->len;
-				card->wandev.stats.tx_bytes += skb->len;
+                                chan->ifstats.tx_bytes += len;
+                                card->wandev.stats.tx_bytes += len;
 			}
 		}
 	}
-	if (!retry)
+
+        if (!dev->tbusy) {
 		dev_kfree_skb(skb);
+	}
 
-	card->wandev.critical = 0;
-	save_flags(host_cpu_flags);
-	cli();
-	if ((!(--card->irq_dis_if_send_count)) && (!card->irq_dis_poll_count))
-		enable_irq(card->hw.irq);
-	restore_flags(host_cpu_flags);
-	return retry;
+        clear_bit(0, (void*)&card->wandev.critical);
+
+	s508_s514_unlock(card,&smp_flags);
+
+        return (dev->tbusy);
+}
+
+
+
+/*============================================================================
+ * Setup so that a frame can be transmitted on the occurence of a transmit
+ * interrupt.
+ */
+static void setup_for_delayed_transmit (struct net_device* dev, void* buf,
+	unsigned len)
+{
+        fr_channel_t* chan = dev->priv;
+        sdla_t* card = chan->card;
+        fr508_flags_t* adptr_flags = card->flags;
+        fr_dlci_interface_t* dlci_interface = chan->dlci_int_interface;
+
+        if(chan->transmit_length) {
+                printk(KERN_INFO "%s: Big mess in setup_for_del...\n",
+				card->devname);
+                return;
+        }
+
+	if(len > FR_MAX_NO_DATA_BYTES_IN_FRAME) {
+		//FIXME: increment some statistic */
+		return;
+	}
+
+        chan->transmit_length = len;
+        memcpy(chan->transmit_buffer, buf, len);
+
+        dlci_interface->gen_interrupt |= FR_INTR_TXRDY;
+        dlci_interface->packet_length = len;
+        adptr_flags->imask |= FR_INTR_TXRDY;
+
+	card->u.f.tx_interrupts_pending ++;
+}
+
+
+/*============================================================================
+ * Check to see if the packet to be transmitted contains a broadcast or
+ * multicast source IP address.
+ * Return 0 if not broadcast/multicast address, otherwise return 1.
+ */
+
+static int chk_bcast_mcast_addr(sdla_t *card, struct net_device* dev,
+                                struct sk_buff *skb)
+{
+        u32 src_ip_addr;
+        u32 broadcast_ip_addr = 0;
+        struct in_device *in_dev;
+        fr_channel_t* chan = dev->priv;
+ 
+        /* read the IP source address from the outgoing packet */
+        src_ip_addr = *(u32 *)(skb->data + 14);
+
+        /* read the IP broadcast address for the device */
+        in_dev = dev->ip_ptr;
+        if(in_dev != NULL) {
+                struct in_ifaddr *ifa= in_dev->ifa_list;
+                if(ifa != NULL)
+                        broadcast_ip_addr = ifa->ifa_broadcast;
+                else
+                        return 0;
+        }
+
+        /* check if the IP Source Address is a Broadcast address */
+        if((dev->flags & IFF_BROADCAST) && (src_ip_addr == broadcast_ip_addr)) {                printk(KERN_INFO
+                        "%s: Broadcast Source Address silently discarded\n",
+                        card->devname);
+                dev_kfree_skb(skb);
+                ++ chan->ifstats.tx_dropped;
+                return 1;
+        }
+
+        /* check if the IP Source Address is a Multicast address */
+        if((chan->mc == WANOPT_NO) && (ntohl(src_ip_addr) >= 0xE0000001) &&
+                (ntohl(src_ip_addr) <= 0xFFFFFFFE)) {
+                printk(KERN_INFO
+                        "%s: Multicast Source Address silently discarded\n",
+                        card->devname);
+                dev_kfree_skb(skb);
+                ++ chan->ifstats.tx_dropped;
+                return 1;
+        }
+
+        return 0;
 }
 
 /*============================================================================
  * Reply to UDP Management system.
  * Return nothing.
  */
-
-static int reply_udp(unsigned char *data, unsigned int mbox_len)
+static int reply_udp( unsigned char *data, unsigned int mbox_len ) 
 {
-	unsigned short len, udp_length, temp, i, ip_length;
-	unsigned long sum;
+	unsigned short len, udp_length, temp, ip_length;
+	unsigned long ip_temp;
+	int even_bound = 0;
+
+  
+	fr_udp_pkt_t *fr_udp_pkt = (fr_udp_pkt_t *)data; 
+
 	/* Set length of packet */
-	len = mbox_len + 62;
+	len = sizeof(fr_encap_hdr_t)+
+	      sizeof(ip_pkt_t)+ 
+	      sizeof(udp_pkt_t)+
+	      sizeof(wp_mgmt_t)+
+	      sizeof(cblock_t)+
+	      mbox_len;
+ 
+
 	/* fill in UDP reply */
-	data[38] = 0x02;
+	fr_udp_pkt->wp_mgmt.request_reply = UDPMGMT_REPLY;
+  
 	/* fill in UDP length */
-	udp_length = mbox_len + 40;
+	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) 
-	{
+	if ( udp_length & 0x0001 ) {
 		udp_length += 1;
 		len += 1;
+		even_bound = 1;
 	}
-	temp = (udp_length << 8) | (udp_length >> 8);
-	memcpy(&data[26], &temp, 2);
+
+	temp = (udp_length<<8)|(udp_length>>8);
+	fr_udp_pkt->udp_pkt.udp_length = temp;
+	 
 	/* swap UDP ports */
-	memcpy(&temp, &data[22], 2);
-	memcpy(&data[22], &data[24], 2);
-	memcpy(&data[24], &temp, 2);
+	temp = fr_udp_pkt->udp_pkt.udp_src_port;
+	fr_udp_pkt->udp_pkt.udp_src_port = 
+			fr_udp_pkt->udp_pkt.udp_dst_port; 
+	fr_udp_pkt->udp_pkt.udp_dst_port = temp;
+
+
+
 	/* add UDP pseudo header */
 	temp = 0x1100;
-	memcpy(&data[udp_length + 22], &temp, 2);
-	temp = (udp_length << 8) | (udp_length >> 8);
-	memcpy(&data[udp_length + 24], &temp, 2);
+	*((unsigned short *)
+		(fr_udp_pkt->data+mbox_len+even_bound)) = temp;	
+	temp = (udp_length<<8)|(udp_length>>8);
+	*((unsigned short *)
+		(fr_udp_pkt->data+mbox_len+even_bound+2)) = temp;
+		 
 	/* calculate UDP checksum */
-	data[28] = data[29] = 0;
-	sum = 0;
-	for (i = 0; i < udp_length + 12; i += 2) 
-	{
-		memcpy(&temp, &data[14 + i], 2);
-		sum += (unsigned long) temp;
-	}
-	while (sum >> 16)
-		sum = (sum & 0xffffUL) + (sum >> 16);
+	fr_udp_pkt->udp_pkt.udp_checksum = 0;
+
+	fr_udp_pkt->udp_pkt.udp_checksum = 
+		calc_checksum(&data[UDP_OFFSET+sizeof(fr_encap_hdr_t)],
+			      udp_length+UDP_OFFSET);
 
-	temp = (unsigned short) sum;
-	temp = ~temp;
-	if (temp == 0)
-		temp = 0xffff;
-	memcpy(&data[28], &temp, 2);
 	/* fill in IP length */
-	ip_length = udp_length + 20;
-	temp = (ip_length << 8) | (ip_length >> 8);
-	memcpy(&data[4], &temp, 2);
+	ip_length = udp_length + sizeof(ip_pkt_t);
+	temp = (ip_length<<8)|(ip_length>>8);
+	fr_udp_pkt->ip_pkt.total_length = temp;
+  
 	/* swap IP addresses */
-	memcpy(&temp, &data[14], 2);
-	memcpy(&data[14], &data[18], 2);
-	memcpy(&data[18], &temp, 2);
-	memcpy(&temp, &data[16], 2);
-	memcpy(&data[16], &data[20], 2);
-	memcpy(&data[20], &temp, 2);
+	ip_temp = fr_udp_pkt->ip_pkt.ip_src_address;
+	fr_udp_pkt->ip_pkt.ip_src_address = 
+				fr_udp_pkt->ip_pkt.ip_dst_address;
+	fr_udp_pkt->ip_pkt.ip_dst_address = ip_temp;
+
+		 
 	/* fill in IP checksum */
-	data[12] = data[13] = 0;
-	sum = 0;
-	for (i = 0; i < 20; i += 2) 
-	{
-		memcpy(&temp, &data[2 + i], 2);
-		sum += (unsigned long) temp;
+	fr_udp_pkt->ip_pkt.hdr_checksum = 0;
+	fr_udp_pkt->ip_pkt.hdr_checksum = 
+		calc_checksum(&data[sizeof(fr_encap_hdr_t)],
+		      	      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)
+
+	while (sum >> 16 ) {
 		sum = (sum & 0xffffUL) + (sum >> 16);
-	temp = (unsigned short) sum;
+	}
+
+	temp = (unsigned short)sum;
 	temp = ~temp;
-	if (temp == 0)
+
+	if( temp == 0 ) 
 		temp = 0xffff;
-	memcpy(&data[12], &temp, 2);
-	return len;
-}				/* reply_udp */
+
+	return temp;	
+}
+
 /*
    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 
- */
 
+*/
 static void switch_net_numbers(unsigned char *sendpacket, unsigned long network_number, unsigned char incoming)
 {
 	unsigned long pnetwork_number;
-	pnetwork_number = (unsigned long) ((sendpacket[14] << 24) +
-			 (sendpacket[15] << 16) + (sendpacket[16] << 8) +
-					   sendpacket[17]);
+
+	pnetwork_number = (unsigned long)((sendpacket[14] << 24) + 
+			  (sendpacket[15] << 16) + (sendpacket[16] << 8) + 
+			  sendpacket[17]);
+
 	if (!incoming) {
 		/* If the destination network number is ours, make it 0 */
-		if (pnetwork_number == network_number) {
-			sendpacket[14] = sendpacket[15] = sendpacket[16] =
-			    sendpacket[17] = 0x00;
+		if( pnetwork_number == network_number) {
+			sendpacket[14] = sendpacket[15] = sendpacket[16] = 
+					 sendpacket[17] = 0x00;
 		}
 	} else {
 		/* If the incoming network is 0, make it ours */
-		if (pnetwork_number == 0) 
-		{
-			sendpacket[14] = (unsigned char) (network_number >> 24);
-			sendpacket[15] = (unsigned char) ((network_number &
-						      0x00FF0000) >> 16);
-			sendpacket[16] = (unsigned char) ((network_number &
-						       0x0000FF00) >> 8);
-			sendpacket[17] = (unsigned char) (network_number &
-							  0x000000FF);
+		if( pnetwork_number == 0) {
+			sendpacket[14] = (unsigned char)(network_number >> 24);
+			sendpacket[15] = (unsigned char)((network_number & 
+					 0x00FF0000) >> 16);
+			sendpacket[16] = (unsigned char)((network_number & 
+					 0x0000FF00) >> 8);
+			sendpacket[17] = (unsigned char)(network_number & 
+					 0x000000FF);
 		}
 	}
-	pnetwork_number = (unsigned long) ((sendpacket[26] << 24) +
-			 (sendpacket[27] << 16) + (sendpacket[28] << 8) +
-					   sendpacket[29]);
-	if (!incoming) {
+
+
+	pnetwork_number = (unsigned long)((sendpacket[26] << 24) + 
+			  (sendpacket[27] << 16) + (sendpacket[28] << 8) + 
+			  sendpacket[29]);
+
+	if( !incoming ) {
 		/* If the source network is ours, make it 0 */
-		if (pnetwork_number == network_number) 
-		{
-			sendpacket[26] = sendpacket[27] = sendpacket[28] =
-			    sendpacket[29] = 0x00;
+		if( pnetwork_number == network_number) {
+			sendpacket[26] = sendpacket[27] = sendpacket[28] = 
+					 sendpacket[29] = 0x00;
 		}
 	} else {
 		/* If the source network is 0, make it ours */
-		if (pnetwork_number == 0) {
-			sendpacket[26] = (unsigned char) (network_number >> 24);
-			sendpacket[27] = (unsigned char) ((network_number &
-						      0x00FF0000) >> 16);
-			sendpacket[28] = (unsigned char) ((network_number &
-						       0x0000FF00) >> 8);
-			sendpacket[29] = (unsigned char) (network_number &
-							  0x000000FF);
+		if( pnetwork_number == 0 ) {
+			sendpacket[26] = (unsigned char)(network_number >> 24);
+			sendpacket[27] = (unsigned char)((network_number & 
+					 0x00FF0000) >> 16);
+			sendpacket[28] = (unsigned char)((network_number & 
+					 0x0000FF00) >> 8);
+			sendpacket[29] = (unsigned char)(network_number & 
+					 0x000000FF);
 		}
 	}
-}				/* switch_net_numbers */
+} /* switch_net_numbers */
 
 /*============================================================================
- * Get Ethernet-style interface statistics.
- * Return a pointer to struct net_device_stats.
+ * Get ethernet-style interface statistics.
+ * Return a pointer to struct enet_statistics.
  */
-
-static struct net_device_stats *if_stats(struct net_device *dev)
+static struct enet_statistics* if_stats (struct net_device* dev)
 {
-	fr_channel_t *chan = dev->priv;
-	if(chan==NULL)
+	fr_channel_t* chan = dev->priv;
+	
+	if(chan == NULL)
 		return NULL;
-		
+
 	return &chan->ifstats;
 }
 
 /****** Interrupt Handlers **************************************************/
-/*============================================================================
- * S502 frame relay interrupt service routine.
- */
 
-static void fr502_isr(sdla_t * card)
-{
-	fr502_flags_t *flags = card->flags;
-	switch (flags->iflag) 
-	{
-		case 0x01:		/* receive interrupt */
-			fr502_rx_intr(card);
-			break;
-		case 0x02:		/* transmit interrupt */
-			flags->imask &= ~0x02;
-			tx_intr(card);
-			break;
-		default:
-			spur_intr(card);
-	}
-	flags->iflag = 0;
-}
 /*============================================================================
  * S508 frame relay interrupt service routine.
  */
-
-static void fr508_isr(sdla_t * card)
+static void fr_isr (sdla_t* card)
 {
-	fr508_flags_t *flags = card->flags;
-	fr_buf_ctl_t *bctl;
+	fr508_flags_t* flags = card->flags;
 	char *ptr = &flags->iflag;
-	struct net_device *dev = card->wandev.dev;
-	struct net_device *dev2;
-	int i;
-	unsigned long host_cpu_flags;
-	unsigned disable_tx_intr = 1;
-	fr_channel_t *chan;
-	fr_dlci_interface_t *dlci_interface;
+	int i,err;
+	fr_mbox_t* mbox = card->mbox;
+
 	/* This flag prevents nesting of interrupts.  See sdla_isr() routine
-	 * in sdlamain.c. 
+         * in sdlamain.c. 
 	 */
 	card->in_isr = 1;
+	
 	++card->statistics.isr_entry;
-	if (test_and_set_bit(0, (void *) &card->wandev.critical)) 
-	{
-		printk(KERN_INFO "fr508_isr: %s, wandev.critical set to 0x%02X, int type = 0x%02X\n", card->devname, card->wandev.critical, flags->iflag);
-		++card->statistics.isr_already_critical;
+
+	if(test_bit(1, (void*)&card->wandev.critical)) {
+		card->wandev.critical = 0;
+		flags->iflag = 0;
 		card->in_isr = 0;
 		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)
-	 */
-	card->wandev.critical = CRITICAL_IN_ISR;
-	card->dlci_int_mode_unbusy = 0;
-	card->buff_int_mode_unbusy = 0;
-	switch (flags->iflag) 
-	{
-		case 0x01:		/* receive interrupt */
-			++card->statistics.isr_rx;
-			fr508_rx_intr(card);
-			break;
-		case 0x02:		/* transmit interrupt */
-			++card->statistics.isr_tx;
-			bctl = (void *) (flags->tse_offs - FR_MB_VECTOR +
-				 card->hw.dpmbase);
-			bctl->flag = 0xA0;
-			if (card->intr_mode == DLCI_LIST_INTR_MODE) 
-			{
-				/* Find the structure and make it unbusy */
-				dev = find_channel(card, flags->dlci);
-				dev->tbusy = 0;
-				/* This is used to perform devtint at the
-				 * end of the isr 
-				 */
-				card->dlci_int_mode_unbusy = 1;
-				/* check to see if any other interfaces are
-				 * busy. If so then do not disable the tx
-				 * interrupts 
-				 */
-				for (dev2 = card->wandev.dev; dev2;
-					dev2 = dev2->slave) 
-				{
-					if (dev2->tbusy == 1) 
-					{
-						disable_tx_intr = 0;
-						break;
-					}
-				}
-				if (disable_tx_intr)
-					flags->imask &= ~0x02;
-			} 
-			else if (card->intr_mode == BUFFER_INTR_MODE) 
-			{
-				for (dev2 = card->wandev.dev; dev2;
-					dev2 = dev2->slave) 
-				{
-					if (!dev2 || !dev2->start) 
-					{
-						++card->statistics.tx_intr_dev_not_started;
-						continue;
-					}
-					if (dev2->tbusy) 
-					{
-						card->buff_int_mode_unbusy = 1;
-						((fr_channel_t *) dev2->priv)->dev_pending_devtint = 1;
-						dev2->tbusy = 0;
-					} 
-					else
-						((fr_channel_t *) dev2->priv)->dev_pending_devtint = 0;
-				}
-				flags->imask &= ~0x02;
-			}
-			break;
-		case 0x08:
-			Intr_test_counter++;
+
+        if(card->hw.type != SDLA_S514) {
+		if (test_and_set_bit(0, (void*)&card->wandev.critical)) {
+                        printk(KERN_INFO "%s: Critical while in ISR (0x%02X)\n",
+                                card->devname, card->wandev.critical);
+			++card->statistics.isr_already_critical;
+			card->in_isr = 0;
+			return;
+		}
+	}
+
+	switch (flags->iflag) {
+
+                case FR_INTR_RXRDY:  /* receive interrupt */
+	    		++card->statistics.isr_rx;
+          		rx_intr(card);
+            		break;
+
+
+                case FR_INTR_TXRDY:  /* transmit interrupt */
+	    		++ card->statistics.isr_tx; 
+			tx_intr(card); 
+            		break;
+
+                case FR_INTR_READY:
+	    		Intr_test_counter++;
 			++card->statistics.isr_intr_test;
+	    		break;	
+
+                case FR_INTR_DLC: /* Event interrupt occured */
+			mbox->cmd.command = FR_READ_STATUS;
+			mbox->cmd.length = 0;
+			err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
+			if (err)
+				fr_event(card, err, mbox);
+			break;
+
+                case FR_INTR_TIMER:  /* Timer interrupt */
+			timer_intr(card);
 			break;
+	
 		default:
-			++card->statistics.isr_spurious;
-			spur_intr(card);
-			printk(KERN_INFO "%s: Interrupt Type 0x%02X!\n",
-			       card->devname, flags->iflag);
-			printk(KERN_INFO "%s: ID Bytes = ", card->devname);
-			for (i = 0; i < 8; i++)
+	    		++card->statistics.isr_spurious;
+            		spur_intr(card);
+	    		printk(KERN_INFO "%s: Interrupt Type 0x%02X!\n", 
+				card->devname, flags->iflag);
+	    
+			printk(KERN_INFO "%s: ID Bytes = ",card->devname);
+ 	    		for(i = 0; i < 8; i ++)
 				printk(KERN_INFO "0x%02X ", *(ptr + 0x28 + i));
-			printk(KERN_INFO "\n");
+	   	 	printk(KERN_INFO "\n");	
+            
 			break;
-	}
-	card->wandev.critical = CRITICAL_INTR_HANDLED;
-	if (card->wandev.enable_tx_int) 
-	{
-		if (card->intr_mode == DLCI_LIST_INTR_MODE) 
-		{
-			for (dev2 = card->wandev.dev; dev2; dev2 = dev2->slave) 
-			{
-				chan = dev2->priv;
-				if (chan->tx_int_status == WAITING_TO_BE_ENABLED) 
-				{
-					dlci_interface = chan->dlci_int_interface;
-					dlci_interface->gen_interrupt |= 0x40;
-					dlci_interface->packet_length = chan->pkt_length;
-					chan->tx_int_status = DISABLED;
-				}
-			}
-		}
-		card->wandev.enable_tx_int = 0;
-		flags->imask |= 0x02;
-		++card->statistics.isr_enable_tx_int;
-	}
-	save_flags(host_cpu_flags);
-	cli();
+    	}
+
 	card->in_isr = 0;
-	card->wandev.critical = 0xD1;
 	flags->iflag = 0;
-	card->wandev.critical = 0;
-	restore_flags(host_cpu_flags);
-	/* Device is now ready to send. The instant this is executed the If_Send
-	   routine is called. That is why this is put at the bottom of the ISR
-	   to prevent a endless loop condition caused by repeated Interrupts and
-	   enable_tx_int flag.
-	 */
-	if (card->dlci_int_mode_unbusy)
-		mark_bh(NET_BH);
-	if (card->buff_int_mode_unbusy) 
-	{
-		for (;;) 
-		{
-			if (((fr_channel_t *) ((card->devs_struct)->dev_ptr)->priv)->dev_pending_devtint == 1) 
-			{
-				((fr_channel_t *) ((card->devs_struct)->dev_ptr)->priv)->dev_pending_devtint = 0;
-				mark_bh(NET_BH);
-			}
-			if ((card->devs_struct)->next == card->dev_to_devtint_next)
-				break;
-			card->devs_struct = (card->devs_struct)->next;
-		}
-		card->devs_struct = (card->dev_to_devtint_next)->next;
-		card->dev_to_devtint_next = card->devs_struct;
-	}
+        if(card->hw.type != SDLA_S514)
+                clear_bit(0, (void*)&card->wandev.critical);
 }
+
+
+
 /*============================================================================
  * Receive interrupt handler.
- */
+ * When a receive interrupt occurs do the following:
+ *	1- Find the structure for the dlci that the interrupt occured on
+ *      2- If it doesn't exist then print appropriate msg and goto step 8. 
+ * 	3- If it exist then copy data to a skb.
+ * 	4- If skb contains Sangoma UDP data then process them
+ *	5- If skb contains IPXWAN data then send IPXWAN reply packets
+ *	6- If skb contains Inverse Arp data then send Inv Arp replies
+ *	7- If skb contains any other data then decapsulate the packet and
+ *	   send it to the stack.
+ * 	8- Release the receive element and update receive pointers on the board 
+ */
+static void rx_intr (sdla_t* card)
+{
+	fr_rx_buf_ctl_t* frbuf = card->rxmb;
+	fr508_flags_t* flags = card->flags;
+	fr_channel_t* chan;
+	char *ptr = &flags->iflag;
+	struct sk_buff* skb;
+	struct net_device* dev;
+	void* buf;
+	unsigned dlci, len, offs, len_incl_hdr;
+	int i, udp_type;	
+
+	if (frbuf->flag != 0x01) {
+
+		printk(KERN_INFO 
+			"%s: corrupted Rx buffer @ 0x%X, flag = 0x%02X!\n", 
+			card->devname, (unsigned)frbuf, frbuf->flag);
+      
+		printk(KERN_INFO "%s: ID Bytes = ",card->devname);
+ 		for(i = 0; i < 8; i ++)
+			printk(KERN_INFO "0x%02X ", *(ptr + 0x28 + i));
+		printk(KERN_INFO "\n");
+	
+		++card->statistics.rx_intr_corrupt_rx_bfr;
+		return;
+	}
+
+	len  = frbuf->length;
+	dlci = frbuf->dlci;
+	offs = frbuf->offset;
 
-static void fr502_rx_intr(sdla_t * card)
-{
-	fr_mbox_t *mbox = card->rxmb;
-	struct sk_buff *skb;
-	struct net_device *dev;
-	fr_channel_t *chan;
-	unsigned dlci, len;
-	void *buf;
-	unsigned char *sendpacket;
-	unsigned char buf2[3];
-	int udp_type;
-	sdla_mapmem(&card->hw, FR502_RX_VECTOR);
-	dlci = mbox->cmd.dlci;
-	len = mbox->cmd.length;
 	/* Find network interface for this packet */
 	dev = find_channel(card, dlci);
-	if (dev == NULL) 
-	{
-		/* Invalid channel, discard packet */
-		printk(KERN_INFO "%s: receiving on orphaned DLCI %d!\n",
-		       card->devname, dlci);
-		sdla_mapmem(&card->hw, FR_MB_VECTOR);
-	}
-	chan = dev->priv;
-	if (!dev->start) 
-	{
-		++chan->ifstats.rx_dropped;
-		sdla_mapmem(&card->hw, FR_MB_VECTOR);
-	}
-	/* Allocate socket buffer */
-	skb = dev_alloc_skb(len);
-	if (skb == NULL) 
-	{
-		printk(KERN_INFO "%s: no socket buffers available!\n",
-		       card->devname);
-		++chan->ifstats.rx_dropped;
-		sdla_mapmem(&card->hw, FR_MB_VECTOR);
-	}
-	/* Copy data to the socket buffer */
-	buf = skb_put(skb, len);
-	memcpy(buf, mbox->data, len);
-	sdla_mapmem(&card->hw, FR_MB_VECTOR);
-	/* Check if it's a UDP management packet */
-	sendpacket = skb->data;
-	memcpy(&buf2, &card->wandev.udp_port, 2);
-	udp_type = udp_pkt_type(skb, card);
-	if ((udp_type == UDP_FPIPE_TYPE) || (udp_type == UDP_DRVSTATS_TYPE)) 
-	{
-		if (udp_type == UDP_DRVSTATS_TYPE) 
-		{
-			++chan->rx_intr_DRVSTATS_request;
-			process_udp_driver_call(UDP_PKT_FRM_NETWORK, card, skb,
-						dev, dlci, chan);
-		}
-		else
-		{
-			++chan->rx_intr_FPIPE_request;
-			process_udp_mgmt_pkt(UDP_PKT_FRM_NETWORK, card, skb,
-					     dev, dlci, chan);
-		}
-	}
-	else
-	{
-		/* Decapsulate packet and pass it up the protocol stack */
-		skb->dev = dev;
-		buf = skb_pull(skb, 1);		/* remove hardware header */
-		if (!wanrouter_type_trans(skb, dev)) 
-		{
-			/* can't decapsulate packet */
-			dev_kfree_skb(skb);
-			++chan->ifstats.rx_errors;
-			++card->wandev.stats.rx_errors;
-		}
-		else 
-		{
-			netif_rx(skb);
-			++chan->ifstats.rx_packets;
-			++card->wandev.stats.rx_packets;
-			chan->ifstats.rx_bytes += skb->len;
-			card->wandev.stats.rx_bytes += skb->len;
-		}
-	}
-	sdla_mapmem(&card->hw, FR_MB_VECTOR);
-}
-/*============================================================================
- * Receive interrupt handler.
- */
+        
+	if (dev == NULL) {
 
-static void fr508_rx_intr(sdla_t * card)
-{
-	fr_buf_ctl_t *frbuf = card->rxmb;
-	struct sk_buff *skb;
-	struct net_device *dev;
-	fr_channel_t *chan;
-	unsigned dlci, len, offs;
-	void *buf;
-	unsigned rx_count = 0;
-	fr508_flags_t *flags = card->flags;
-	char *ptr = &flags->iflag;
-	int i, err, udp_type;
-	if (frbuf->flag != 0x01) 
-	{
-		printk(KERN_INFO
-		       "%s: corrupted Rx buffer @ 0x%X, flag = 0x%02X!\n",
-		       card->devname, (unsigned) frbuf, frbuf->flag);
-		printk(KERN_INFO "%s: ID Bytes = ", card->devname);
-		for (i = 0; i < 8; i++)
-			printk(KERN_INFO "0x%02X ", *(ptr + 0x28 + i));
-		printk(KERN_INFO "\n");
-		++card->statistics.rx_intr_corrupt_rx_bfr;
-		return;
-	}
-	
-	do 
-	{
-		len = frbuf->length;
-		dlci = frbuf->dlci;
-		offs = frbuf->offset;
-		/* Find network interface for this packet */
-		dev = find_channel(card, dlci);
+		/* unconfigured DLCI, so discard packet */
+   		printk(KERN_INFO "%s: received data on unconfigured DLCI %d!\n",
+                                                card->devname, dlci);
+		++card->statistics.rx_intr_on_orphaned_DLCI; 
+
+       	} else {
 		chan = dev->priv;
-		if (dev == NULL) 
-		{
-			/* Invalid channel, discard packet */
-			printk(KERN_INFO "%s: receiving on orphaned DLCI %d!\n"
-			       ,card->devname, dlci);
-			++card->statistics.rx_intr_on_orphaned_DLCI;
-		}
-		else
-		{
-			skb = dev_alloc_skb(len);
-			if (!dev->start || (skb == NULL)) 
-			{
-				++chan->ifstats.rx_dropped;
-				if (dev->start) 
-				{
-					printk(KERN_INFO
-					       "%s: no socket buffers available!\n",
-					       card->devname);
-					++chan->rx_intr_no_socket;
-				} else
-					++chan->rx_intr_dev_not_started;
+
+   		skb = dev_alloc_skb(len); 
+
+		if (!dev->start || (skb == NULL)) { 
+			++chan->ifstats.rx_dropped;
+		
+			if(dev->start) {
+
+				printk(KERN_INFO 
+				"%s: no socket buffers available!\n", 
+				card->devname);
+	    			chan->drvstats_rx_intr.rx_intr_no_socket ++;
+                		
+			} else
+				chan->drvstats_rx_intr.
+					rx_intr_dev_not_started ++;
+		} else {
+			/* Copy data to the socket buffer */
+			if ((offs + len) > card->u.f.rx_top + 1) {
+				unsigned tmp = card->u.f.rx_top - offs + 1;
+
+				buf = skb_put(skb, tmp);
+				sdla_peek(&card->hw, offs, buf, tmp);
+				offs = card->u.f.rx_base;
+               			len -= tmp;
+               		}
+
+			buf = skb_put(skb, len);
+			sdla_peek(&card->hw, offs, buf, len);
+
+			udp_type = udp_pkt_type( skb, card );
+
+                        if(udp_type != UDP_INVALID_TYPE) {
+                                if(store_udp_mgmt_pkt(udp_type,
+                                        UDP_PKT_FRM_NETWORK, card, skb, dlci)) {
+                                        flags->imask |= FR_INTR_TIMER;
+                                        if (udp_type == UDP_FPIPE_TYPE){
+                                                chan->drvstats_rx_intr.
+						    rx_intr_PIPE_request ++;
+					}
+				}			
 			}
-			else
-			{
-				/* Copy data to the socket buffer */
-				if ((offs + len) > card->u.f.rx_top + 1) 
-				{
-					unsigned tmp = card->u.f.rx_top - offs + 1;
-					buf = skb_put(skb, tmp);
-					sdla_peek(&card->hw, offs, buf, tmp);
-					offs = card->u.f.rx_base;
-					len -= tmp;
-				}
-				buf = skb_put(skb, len);
-				sdla_peek(&card->hw, offs, buf, len);
-				udp_type = udp_pkt_type(skb, card);
-				if (udp_type == UDP_DRVSTATS_TYPE) 
-				{
-					++chan->rx_intr_DRVSTATS_request;
-					process_udp_driver_call(
-					  UDP_PKT_FRM_NETWORK, card, skb,
-							dev, dlci, chan);
+
+			else if (chan->usedby == API) {
+				api_rx_hdr_t* api_rx_hdr;
+				chan->drvstats_rx_intr.
+                                	rx_intr_bfr_passed_to_stack ++;
+				chan->ifstats.rx_packets ++;
+                                card->wandev.stats.rx_packets ++;
+				chan->ifstats.rx_bytes += skb->len;
+				card->wandev.stats.rx_bytes += skb->len;
+
+				skb_push(skb, sizeof(api_rx_hdr_t));
+				api_rx_hdr = (api_rx_hdr_t*)&skb->data[0x00];
+				api_rx_hdr->attr = frbuf->attr;
+				api_rx_hdr->time_stamp = frbuf->tmstamp;
+				skb->protocol = htons(0x16);
+				skb->pkt_type = PACKET_HOST;
+                                /* Pass it up the protocol stack */
+                                skb->dev = dev;
+                                skb->mac.raw  = skb->data;
+                                netif_rx(skb);
+
+			} else if (handle_IPXWAN(skb->data,chan->name,
+				chan->enable_IPX, chan->network_number)) {
+				if (chan->enable_IPX) {
+                                        fr_send(card, dlci, 0, skb->len,
+						skb->data);
 				}
-				else if (udp_type == UDP_FPIPE_TYPE) 
-				{
-					++chan->rx_intr_FPIPE_request;
-					err = process_udp_mgmt_pkt(
-					       UDP_PKT_FRM_NETWORK, card,
-						   skb, dev, dlci, chan);
+				dev_kfree_skb(skb);
+
+/*FIXME: Fix the ARPS in next release
+
+			} else if (is_arp(skb->data)) {
+				if (process_ARP((arphdr_1490_t *)skb->data, card, dev)) {
+					printk (KERN_INFO "%s: Error processing ARP Packet.\n", card->devname);
 				}
-				else if (handle_IPXWAN(skb->data, card->devname, card->wandev.enable_IPX, card->wandev.network_number)) 
-				{
-					if (card->wandev.enable_IPX) 
-						fr508_send(card, dlci, 0, skb->len, skb->data);
-				} 
-				else
-				{
-					/* Decapsulate packet and pass it up the
-					   protocol stack */
-					skb->dev = dev;
-					/* remove hardware header */
-					buf = skb_pull(skb, 1);
-					if (!wanrouter_type_trans(skb, dev)) 
-					{
-						/* can't decapsulate packet */
-						dev_kfree_skb(skb);
-						++chan->
-						    rx_intr_bfr_not_passed_to_stack;
-						++chan->
-						    ifstats.rx_errors;
-						++card->
-						    wandev.stats.rx_errors;
-					}
-					else
-					{
-						netif_rx(skb);
-						++chan->rx_intr_bfr_passed_to_stack;
-						++chan->ifstats.rx_packets;
-						++card->wandev.stats.rx_packets;
-						chan->ifstats.rx_bytes += skb->len;
-						card->wandev.stats.rx_bytes += skb->len;
-					}
+				dev_kfree_skb(skb);
+*/
+
+			} else if ( skb->data[0] != 0x03) {
+				printk(KERN_INFO "%s: Non IETF packet discarded.\n", card->devname);
+				dev_kfree_skb(skb);
+
+			} else {
+
+				len_incl_hdr = skb->len;
+                    		/* Decapsulate packet and pass it up the
+			   	   protocol stack */
+				skb->dev = dev;
+
+				/* remove hardware header */
+				buf = skb_pull(skb, 1); 
+
+				if (!wanrouter_type_trans(skb, dev)) {
+					
+					/* can't decapsulate packet */
+					dev_kfree_skb(skb);
+					chan->drvstats_rx_intr.
+					     rx_intr_bfr_not_passed_to_stack ++;
+ 					++ chan->ifstats.rx_errors;
+		                       	++ card->wandev.stats.rx_errors;
+					
+				} else {
+					netif_rx(skb);
+					chan->drvstats_rx_intr.
+						rx_intr_bfr_passed_to_stack ++;
+					++ chan->ifstats.rx_packets;
+					++ card->wandev.stats.rx_packets;
+                                        chan->ifstats.rx_bytes += len_incl_hdr;
+                                        card->wandev.stats.rx_bytes += 
+						len_incl_hdr;
 				}
-			}
-		}
-		/* Release buffer element and calculate a pointer to the next 
-		   one */
-		frbuf->flag = 0;
-		card->rxmb = ++frbuf;
-		if ((void *) frbuf > card->u.f.rxmb_last)
-			card->rxmb = card->u.f.rxmb_base;
-		/* The loop put in is temporary, that is why the break is
-		 * placed here. (?????)
-		 */
-		break;
-		frbuf = card->rxmb;
-	}
-	while (frbuf->flag && ((++rx_count) < 4));
+                	}
+            	}
+       	}
+
+       	/* Release buffer element and calculate a pointer to the next one */ 
+       	frbuf->flag = 0;
+	card->rxmb = ++frbuf;
+	if ((void*)frbuf > card->u.f.rxmb_last)
+		card->rxmb = card->u.f.rxmb_base;
+
 }
+
 /*============================================================================
  * Transmit interrupt handler.
- * o print a warning
- * o 
- * If number of spurious interrupts exceeded some limit, then ???
  */
-static void tx_intr(sdla_t * card)
+static void tx_intr(sdla_t *card)
 {
-	struct net_device *dev = card->wandev.dev;
-	if (card->intr_mode == BUFFER_INTR_MODE) 
-	{
-		for (; dev; dev = dev->slave) 
-		{
-			if (!dev || !dev->start) 
-			{
-				++card->statistics.tx_intr_dev_not_started;
-				continue;
+        fr508_flags_t* flags = card->flags;
+        fr_tx_buf_ctl_t* bctl;
+        struct net_device* dev = card->wandev.dev;
+        fr_channel_t* chan;
+
+        if(card->hw.type == SDLA_S514){
+                bctl = (void*)(flags->tse_offs + card->hw.dpmbase);
+        }else{
+                bctl = (void*)(flags->tse_offs - FR_MB_VECTOR +
+                        card->hw.dpmbase);
+	}
+
+        /* Find the structure and make it unbusy */
+        dev = find_channel(card, flags->dlci);
+        chan = dev->priv;
+
+        if(!chan->transmit_length) {
+                printk(KERN_INFO "%s: tx int error - transmit length zero\n",
+				card->wandev.name);
+                return;
+        }
+
+	/* If the 'if_send()' procedure is currently checking the 'tbusy'
+	   status, then we cannot transmit. Instead, we configure the microcode
+	   so as to re-issue this transmit interrupt at a later stage. 
+	*/
+	if (test_bit(2, (void*)&card->wandev.critical)) {
+		fr_dlci_interface_t* dlci_interface = chan->dlci_int_interface;
+		bctl->flag = 0xA0;
+		dlci_interface->gen_interrupt |= FR_INTR_TXRDY;
+		printk(KERN_INFO "%s: TX Interrupt Detected busy if_send\n",card->devname);
+
+ 	} else {
+        	bctl->dlci = flags->dlci;
+	        bctl->length = chan->transmit_length;
+        	sdla_poke(&card->hw, bctl->offset, chan->transmit_buffer,
+                	chan->transmit_length);
+	        bctl->flag = 0xC0;
+
+		++chan->ifstats.tx_packets;
+		++card->wandev.stats.tx_packets;
+		chan->ifstats.tx_bytes += chan->transmit_length;
+		card->wandev.stats.tx_bytes += chan->transmit_length;
+        	chan->transmit_length = 0;
+
+	        /* if any other interfaces have transmit interrupts pending, */
+		/* do not disable the global transmit interrupt */
+		if(!(-- card->u.f.tx_interrupts_pending))
+        	        flags->imask &= ~FR_INTR_TXRDY;
+
+ 		dev->tbusy = 0;
+		mark_bh(NET_BH);
+	}
+}
+
+
+/*============================================================================
+ * Timer interrupt handler.
+ FIXME: update comments as we modify the code
+ * The timer interrupt is used for three purposes:
+ *    1) Processing udp calls from 'fpipemon'.
+ *    2) Processing update calls from /proc file system
+ *    2) Reading board-level statistics for updating the proc file system.
+ *    3) Sending inverse ARP request packets.
+ */
+static void timer_intr(sdla_t *card)
+{
+	fr508_flags_t* flags = card->flags;
+
+        if(card->u.f.timer_int_enabled & TMR_INT_ENABLED_UDP) {
+		if(card->u.f.udp_type == UDP_FPIPE_TYPE) {
+                    	if(process_udp_mgmt_pkt(card)) {
+		                card->u.f.timer_int_enabled &=
+					~TMR_INT_ENABLED_UDP;
 			}
-			dev->tbusy = 0;
-			mark_bh(NET_BH);
 		}
+        }
+
+	if(card->u.f.timer_int_enabled & TMR_INT_ENABLED_UPDATE) {
+		fr_get_err_stats(card);
+		fr_get_stats(card);
+		card->u.f.update_comms_stats = 0;
+		card->u.f.timer_int_enabled &= ~TMR_INT_ENABLED_UPDATE;
 	}
-	else
-	{
-		dev->tbusy = 0;
-		mark_bh(NET_BH);
+
+
+//FIXME: Fix the dynamic IP addressing
+/*
+goto L4;
+
+	// Used to send inarp request at given interval 
+	if (card->wandev.state == WAN_CONNECTED) {
+        	int num_remaining = 0;
+                for (dev=card->wandev.dev;dev;dev=dev->slave) {
+                	fr_channel_t *chan = dev->priv;
+
+                        if (chan->inarp == INARP_REQUEST &&
+                        	chan->state == WAN_CONNECTED) {
+                                num_remaining++;
+
+                                if ((jiffies - chan->inarp_tick) > (chan->inarp_interval * HZ)) {
+                                	send_inarp_request(card,dev);
+                                	chan->inarp_tick = jiffies;
+                                }
+			}
+		}
+		if (!num_remaining) {   // no more to process 
+                        flags->imask &= ~FR_INTR_TIMER;
+		}
 	}
+L4:
+	;
+*/
+        if(!card->u.f.timer_int_enabled)
+                flags->imask &= ~FR_INTR_TIMER;
 }
 
+
 /*============================================================================
  * Spurious interrupt handler.
  * o print a warning
  * o 
- * If number of spurious interrupts exceeded some limit, then ???
  */
-
-static void spur_intr(sdla_t * card)
+static void spur_intr (sdla_t* card)
 {
 	printk(KERN_INFO "%s: spurious interrupt!\n", card->devname);
 }
 
-/*
-   Return 0 for non-IPXWAN packet
-   1 for IPXWAN packet or IPX is not enabled!
+//FIXME: Fix the IPX in next version
+/*===========================================================================
+ *  Return 0 for non-IPXWAN packet
+ *         1 for IPXWAN packet or IPX is not enabled!
+ *  FIXME: Use a IPX structure here not offsets
  */
-
 static int handle_IPXWAN(unsigned char *sendpacket, char *devname, unsigned char enable_IPX, unsigned long network_number)
 {
 	int i;
-	if (sendpacket[1] == 0x00 &&
+
+	if( sendpacket[1] == 0x00 &&
 	    sendpacket[2] == 0x80 &&
 	    sendpacket[6] == 0x81 &&
-	    sendpacket[7] == 0x37) 
-	{
+	    sendpacket[7] == 0x37) { 
+
 		/* It's an IPX packet */
-		if (!enable_IPX) {
+		if(!enable_IPX) {
 			/* Return 1 so we don't pass it up the stack. */
+			//FIXME: Take this out when IPX is fixed
+			printk (KERN_INFO 
+				"%s: WARNING: Unsupported IPX packet received and dropped\n",
+					devname);
 			return 1;
 		}
-	}
-	else
-	{
+	} else {
 		/* It's not IPX so return and pass it up the stack. */
 		return 0;
 	}
-	if (sendpacket[24] == 0x90 &&
-	    sendpacket[25] == 0x04) 
+
+	if( sendpacket[24] == 0x90 &&
+	    sendpacket[25] == 0x04)
 	{
 		/* It's IPXWAN */
-		if (sendpacket[10] == 0x02 &&
-		    sendpacket[42] == 0x00) 
+
+		if( sendpacket[10] == 0x02 &&
+		    sendpacket[42] == 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 */
-			/* option except Unnumbered RIP/SAP */
-			for (i = 49; sendpacket[i] == 0x00; i += 5) 
+			printk(KERN_INFO "%s: Received IPXWAN Timer Request packet\n",devname);
+
+			/* Go through the routing options and answer no to every
+			 * option except Unnumbered RIP/SAP
+			 */
+			for(i = 49; sendpacket[i] == 0x00; i += 5)
 			{
 				/* 0x02 is the option for Unnumbered RIP/SAP */
-				if (sendpacket[i + 4] != 0x02) 
+				if( sendpacket[i + 4] != 0x02)
 				{
 					sendpacket[i + 1] = 0;
 				}
 			}
+
 			/* Skip over the extended Node ID option */
-			if (sendpacket[i] == 0x04) 
+			if( sendpacket[i] == 0x04 )
+			{
 				i += 8;
-			/* We also want to turn off all header compression opt. */
-			for (; sendpacket[i] == 0x80;) 
+			}
+
+			/* We also want to turn off all header compression opt.
+			 */
+			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[42] = 0x01;
-			printk(KERN_INFO "%s: Sending IPXWAN Timer Response\n", devname);
+
+			printk(KERN_INFO "%s: Sending IPXWAN Timer Response\n",devname);
 		}
-		else if (sendpacket[42] == 0x02) 
+		else if( sendpacket[42] == 0x02 )
 		{
 			/* This is an information request packet */
-			printk(KERN_INFO "%s: Received IPXWAN Information Request packet\n", devname);
+			printk(KERN_INFO "%s: Received IPXWAN Information Request packet\n",devname);
+
 			/* Set the packet type to information response */
 			sendpacket[42] = 0x03;
+
 			/* Set the router name */
 			sendpacket[59] = 'F';
 			sendpacket[60] = 'P';
@@ -1756,465 +2082,558 @@
 			sendpacket[63] = 'E';
 			sendpacket[64] = '-';
 			sendpacket[65] = CVHexToAscii(network_number >> 28);
-			sendpacket[66] = CVHexToAscii((network_number & 0x0F000000) >> 24);
-			sendpacket[67] = CVHexToAscii((network_number & 0x00F00000) >> 20);
-			sendpacket[68] = CVHexToAscii((network_number & 0x000F0000) >> 16);
-			sendpacket[69] = CVHexToAscii((network_number & 0x0000F000) >> 12);
-			sendpacket[70] = CVHexToAscii((network_number & 0x00000F00) >> 8);
-			sendpacket[71] = CVHexToAscii((network_number & 0x000000F0) >> 4);
+			sendpacket[66] = CVHexToAscii((network_number & 0x0F000000)>> 24);
+			sendpacket[67] = CVHexToAscii((network_number & 0x00F00000)>> 20);
+			sendpacket[68] = CVHexToAscii((network_number & 0x000F0000)>> 16);
+			sendpacket[69] = CVHexToAscii((network_number & 0x0000F000)>> 12);
+			sendpacket[70] = CVHexToAscii((network_number & 0x00000F00)>> 8);
+			sendpacket[71] = CVHexToAscii((network_number & 0x000000F0)>> 4);
 			sendpacket[72] = CVHexToAscii(network_number & 0x0000000F);
-			for (i = 73; i < 107; i += 1) 
+			for(i = 73; i < 107; i+= 1)
+			{
 				sendpacket[i] = 0;
-			printk(KERN_INFO "%s: Sending IPXWAN Information Response packet\n", devname);
+			}
+
+			printk(KERN_INFO "%s: Sending IPXWAN Information Response packet\n",devname);
 		}
 		else
 		{
-			printk(KERN_INFO "%s: Unknown IPXWAN packet!\n", devname);
+			printk(KERN_INFO "%s: Unknown IPXWAN packet!\n",devname);
 			return 0;
 		}
+
 		/* Set the WNodeID to our network address */
-		sendpacket[43] = (unsigned char) (network_number >> 24);
-		sendpacket[44] = (unsigned char) ((network_number & 0x00FF0000) >> 16);
-		sendpacket[45] = (unsigned char) ((network_number & 0x0000FF00) >> 8);
-		sendpacket[46] = (unsigned char) (network_number & 0x000000FF);
+		sendpacket[43] = (unsigned char)(network_number >> 24);
+		sendpacket[44] = (unsigned char)((network_number & 0x00FF0000) >> 16);
+		sendpacket[45] = (unsigned char)((network_number & 0x0000FF00) >> 8);
+		sendpacket[46] = (unsigned char)(network_number & 0x000000FF);
+
 		return 1;
 	}
-	/* If we get here, its an IPX-data packet so it'll get passed up the stack. */
-	/* switch the network numbers */
-	switch_net_numbers(sendpacket, network_number, 1);
+
+	/* If we get here, its an IPX-data packet so it'll get passed up the 
+	 * stack.
+	 * switch the network numbers 
+	 */
+	switch_net_numbers(sendpacket, network_number ,1);
 	return 0;
 }
+/*============================================================================
+ * Process Route.
+ * This routine is called as a polling routine to dynamically add/delete routes
+ * negotiated by inverse ARP.  It is in this "task" because we don't want routes
+ * to be added while in interrupt context.
+*/
+
+static void process_route (sdla_t* card)
+{
+	struct net_device* dev;
+	struct in_device *in_dev;
+    	struct rtentry route;
+        int err = 0;
+        mm_segment_t fs;
+
+
+	/* Dynamic Route adding/removing */
+	for (dev = card->wandev.dev; dev ; dev = dev->slave) {
+	     if ( ((fr_channel_t*)dev->priv)->route_flag == ADD_ROUTE   ||
+		((fr_channel_t*)dev->priv)->route_flag == REMOVE_ROUTE ) {
+		fs = get_fs();
+		
+		in_dev = dev->ip_ptr;
 
-/****** Background Polling Routines  ****************************************/
+		if( in_dev != NULL && in_dev->ifa_list != NULL) {
+			memset(&route, 0, sizeof(route));
+			route.rt_dev   = dev->name;
+			route.rt_flags = 0;
+
+			((struct sockaddr_in *) &(route.rt_dst)) ->
+				sin_addr.s_addr=in_dev->ifa_list->ifa_address;
+			((struct sockaddr_in *) &(route.rt_dst)) ->
+				sin_family = AF_INET;
+			((struct sockaddr_in *) &(route.rt_genmask)) ->
+				sin_addr.s_addr = 0xFFFFFFFF;
+			((struct sockaddr_in *) &(route.rt_genmask)) ->
+				sin_family = AF_INET;
+
+			switch(((fr_channel_t*)dev->priv)->route_flag) {
+
+			case ADD_ROUTE:
+				set_fs(get_ds());     /* get user space block */
+				err = ip_rt_ioctl( SIOCADDRT, &route);	
+				set_fs(fs);	      /* restore old block */
+
+				if (err) {
+					printk(KERN_INFO "%s: Adding of route failed.  Error: %d\n", card->devname,err);
+					printk(KERN_INFO "%s: Address: %s\n",
+					((fr_channel_t*)dev->priv)->name,
+					in_ntoa(in_dev->ifa_list->ifa_address) );
+				}
+				else {
+					((fr_channel_t*)dev->priv)->
+						route_flag = ROUTE_ADDED;
+				}
+				break;
 
-/*============================================================================
- * Main polling routine.
- * This routine is repeatedly called by the WANPIPE 'thead' to allow for
- * time-dependent housekeeping work.
- *
- * o fetch asynchronous network events.
- *
- * Notes:
- * 1. This routine may be called on interrupt context with all interrupts
- *    enabled. Beware!
- */
+			case REMOVE_ROUTE:
+				set_fs(get_ds());     /* get user space block */
+				err = ip_rt_ioctl( SIOCDELRT, &route);
+				set_fs(fs);	      /* restore old block */
+
+				if (err) {
+			     		printk(KERN_INFO "%s: Deleting of route failed.  Error: %d\n", card->devname,err);
+			    		printk(KERN_INFO "%s: Address: %s\n",
+					dev->name,in_ntoa(in_dev->ifa_list->ifa_address) );
+				} else {
+			    	 	printk(KERN_INFO "%s: Removed route.\n",
+					((fr_channel_t*)dev->priv)->name);
+			     		((fr_channel_t*)dev->priv)->route_flag = NO_ROUTE;
+				}
+				break;
+			} /* Case Statement */
+		}
+	     } /* If ADD/DELETE ROUTE */
+	}  /* Device 'For' Loop */
 
-static void wpf_poll(sdla_t * card)
-{
-/*      struct net_device* dev = card->wandev.dev;  */
-	fr508_flags_t *flags = card->flags;
-	unsigned long host_cpu_flags;
-	++card->statistics.poll_entry;
-	if (((jiffies - card->state_tick) < HZ) ||
-	    (card->intr_mode == INTR_TEST_MODE))
-		return;
-	disable_irq(card->hw.irq);
-	++card->irq_dis_poll_count;
-	if (test_and_set_bit(0, (void *) &card->wandev.critical)) 
-	{
-		++card->statistics.poll_already_critical;
-		save_flags(host_cpu_flags);
-		cli();
-		if ((!card->irq_dis_if_send_count) &&
-		    (!(--card->irq_dis_poll_count)))
-			enable_irq(card->hw.irq);
-		restore_flags(host_cpu_flags);
-		return;
-	}
-	card->wandev.critical = 0x11;
-	++card->statistics.poll_processed;
-	/* This is to be changed later ??? */
-	/*
-	   if( dev && dev->tbusy && !(flags->imask & 0x02) ) {
-	   printk(KERN_INFO "%s: Wpf_Poll: tbusy = 0x01, imask = 0x%02X\n",             card->devname, flags->imask);
-	   }
-	 */
-	if (flags->event) 
-	{
-		fr_mbox_t *mbox = card->mbox;
-		int err;
-		memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
-		mbox->cmd.command = FR_READ_STATUS;
-		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
-		if (err)
-			fr_event(card, err, mbox);
-	}
-	card->wandev.critical = 0;
-	save_flags(host_cpu_flags);
-	cli();
-	if ((!card->irq_dis_if_send_count) && (!(--card->irq_dis_poll_count)))
-		enable_irq(card->hw.irq);
-	restore_flags(host_cpu_flags);
-	card->state_tick = jiffies;
+	card->poll = NULL;
 }
 
+
+
 /****** Frame Relay Firmware-Specific Functions *****************************/
 
 /*============================================================================
  * Read firmware code version.
  * o fill string str with firmware version info. 
  */
-
-static int fr_read_version(sdla_t * card, char *str)
+static int fr_read_version (sdla_t* card, char* str)
 {
-	fr_mbox_t *mbox = card->mbox;
+	fr_mbox_t* mbox = card->mbox;
 	int retry = MAX_CMD_RETRY;
 	int err;
-	do 
+
+	do
 	{
-		memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
 		mbox->cmd.command = FR_READ_CODE_VERSION;
+		mbox->cmd.length = 0;
 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
 	} while (err && retry-- && fr_event(card, err, mbox));
 	
-	if (!err && str) 
-	{
+	if (!err && str) {
 		int len = mbox->cmd.length;
 		memcpy(str, mbox->data, len);
-		str[len] = '\0';
+	        str[len] = '\0';
 	}
 	return err;
 }
+
 /*============================================================================
  * Set global configuration.
  */
- 
-static int fr_configure(sdla_t * card, fr_conf_t * conf)
+static int fr_configure (sdla_t* card, fr_conf_t *conf)
 {
-	fr_mbox_t *mbox = card->mbox;
+	fr_mbox_t* mbox = card->mbox;
 	int retry = MAX_CMD_RETRY;
 	int dlci_num = card->u.f.dlci_num;
 	int err, i;
-	do 
+
+	do
 	{
-		memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
 		memcpy(mbox->data, conf, sizeof(fr_conf_t));
-		if (dlci_num)
-			for (i = 0; i < dlci_num; ++i)
-				((fr_conf_t *) mbox->data)->dlci[i] =
-				    card->u.f.node_dlci[i];
+
+		if (dlci_num) for (i = 0; i < dlci_num; ++i)
+			((fr_conf_t*)mbox->data)->dlci[i] = 
+					card->u.f.node_dlci[i]; 
+		
 		mbox->cmd.command = FR_SET_CONFIG;
 		mbox->cmd.length =
-		    sizeof(fr_conf_t) + dlci_num * sizeof(short);
+			sizeof(fr_conf_t) + dlci_num * sizeof(short);
+
 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
-	}
-	while (err && retry-- && fr_event(card, err, mbox));
+	
+	} while (err && retry-- && fr_event(card, err, mbox));
 	
 	return err;
 }
+
 /*============================================================================
  * Set DLCI configuration.
  */
-static int fr_dlci_configure(sdla_t * card, fr_dlc_conf_t * conf, unsigned dlci)
+static int fr_dlci_configure (sdla_t* card, fr_dlc_conf_t *conf, unsigned dlci)
 {
-	fr_mbox_t *mbox = card->mbox;
+	fr_mbox_t* mbox = card->mbox;
 	int retry = MAX_CMD_RETRY;
 	int err;
+
 	do
 	{
-		memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
 		memcpy(mbox->data, conf, sizeof(fr_dlc_conf_t));
-		mbox->cmd.dlci = (unsigned short) dlci;
+		mbox->cmd.dlci = (unsigned short) dlci; 
 		mbox->cmd.command = FR_SET_CONFIG;
-		mbox->cmd.length = 0x0E;
+		mbox->cmd.length = sizeof(fr_dlc_conf_t);
 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
-	}
-	while (err && retry--);
+	} while (err && retry--);
+	
 	return err;
 }
 /*============================================================================
  * Set interrupt mode.
  */
-static int fr_set_intr_mode(sdla_t * card, unsigned mode, unsigned mtu)
+static int fr_set_intr_mode (sdla_t* card, unsigned mode, unsigned mtu,
+	unsigned short timeout)
 {
-	fr_mbox_t *mbox = card->mbox;
+	fr_mbox_t* mbox = card->mbox;
+	fr508_intr_ctl_t* ictl = (void*)mbox->data;
 	int retry = MAX_CMD_RETRY;
 	int err;
+
 	do
 	{
-		memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
-		if (card->hw.fwid == SFID_FR502) 
-		{
-			fr502_intr_ctl_t *ictl = (void *) mbox->data;
-			memset(ictl, 0, sizeof(fr502_intr_ctl_t));
-			ictl->mode = mode;
-			ictl->tx_len = mtu;
-			mbox->cmd.length = sizeof(fr502_intr_ctl_t);
-		}
-		else
-		{
-			fr508_intr_ctl_t *ictl = (void *) mbox->data;
-			memset(ictl, 0, sizeof(fr508_intr_ctl_t));
-			ictl->mode = mode;
-			ictl->tx_len = mtu;
-			ictl->irq = card->hw.irq;
-			mbox->cmd.length = sizeof(fr508_intr_ctl_t);
-		}
+		memset(ictl, 0, sizeof(fr508_intr_ctl_t));
+		ictl->mode   = mode;
+		ictl->tx_len = mtu;
+		ictl->irq    = card->hw.irq;
+
+		/* indicate timeout on timer */
+		if (mode & 0x20) ictl->timeout = timeout; 
+
+		mbox->cmd.length = sizeof(fr508_intr_ctl_t);
 		mbox->cmd.command = FR_SET_INTR_MODE;
 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
-	}
-	while (err && retry-- && fr_event(card, err, mbox));
+
+	} while (err && retry-- && fr_event(card, err, mbox));
 	
 	return err;
 }
+
 /*============================================================================
  * Enable communications.
  */
-static int fr_comm_enable(sdla_t * card)
+static int fr_comm_enable (sdla_t* card)
 {
-	fr_mbox_t *mbox = card->mbox;
+	fr_mbox_t* mbox = card->mbox;
 	int retry = MAX_CMD_RETRY;
 	int err;
-	do 
+
+	do
 	{
-		memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
 		mbox->cmd.command = FR_COMM_ENABLE;
+		mbox->cmd.length = 0;
 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
-	}
-	while (err && retry-- && fr_event(card, err, mbox));
+	} while (err && retry-- && fr_event(card, err, mbox));
 	
 	return err;
 }
+
 /*============================================================================
  * Disable communications. 
  */
-static int fr_comm_disable(sdla_t * card)
+static int fr_comm_disable (sdla_t* card)
 {
-	fr_mbox_t *mbox = card->mbox;
+	fr_mbox_t* mbox = card->mbox;
 	int retry = MAX_CMD_RETRY;
 	int err;
+
 	do
 	{
-		memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
 		mbox->cmd.command = FR_COMM_DISABLE;
+		mbox->cmd.length = 0;
 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
-	}
-	while (err && retry-- && fr_event(card, err, mbox));
+	} while (err && retry-- && fr_event(card, err, mbox));
+
+	retry = MAX_CMD_RETRY;
+
+	do {
+	mbox->cmd.command = FR_SET_MODEM_STATUS;
+	mbox->cmd.length = 1;
+	mbox->data[0] = 0;
+	err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
+	} while (err && retry-- && fr_event(card, err, mbox));
 	
 	return err;
 }
+
 /*============================================================================
  * Get communications error statistics. 
  */
-static int fr_get_err_stats(sdla_t * card)
+static int fr_get_err_stats (sdla_t* card)
 {
-	fr_mbox_t *mbox = card->mbox;
+	fr_mbox_t* mbox = card->mbox;
 	int retry = MAX_CMD_RETRY;
 	int err;
-	
+
+
 	do
 	{
-		memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
 		mbox->cmd.command = FR_READ_ERROR_STATS;
+		mbox->cmd.length = 0;
+		mbox->cmd.dlci = 0;
 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
-	}
-	while (err && retry-- && fr_event(card, err, mbox));
-	
-	if (!err) 
-	{
-		fr_comm_stat_t *stats = (void *) mbox->data;
-		card->wandev.stats.rx_over_errors = stats->rx_overruns;
-		card->wandev.stats.rx_crc_errors = stats->rx_bad_crc;
-		card->wandev.stats.rx_missed_errors = stats->rx_aborts;
-		card->wandev.stats.rx_length_errors = stats->rx_too_long;
+	} while (err && retry-- && fr_event(card, err, mbox));
+
+	if (!err) {
+		fr_comm_stat_t* stats = (void*)mbox->data;
+		card->wandev.stats.rx_over_errors    = stats->rx_overruns;
+		card->wandev.stats.rx_crc_errors     = stats->rx_bad_crc;
+		card->wandev.stats.rx_missed_errors  = stats->rx_aborts;
+		card->wandev.stats.rx_length_errors  = stats->rx_too_long;
 		card->wandev.stats.tx_aborted_errors = stats->tx_aborts;
+	
 	}
+
 	return err;
 }
+
 /*============================================================================
  * Get statistics. 
  */
-static int fr_get_stats(sdla_t * card)
+static int fr_get_stats (sdla_t* card)
 {
-	fr_mbox_t *mbox = card->mbox;
+	fr_mbox_t* mbox = card->mbox;
 	int retry = MAX_CMD_RETRY;
 	int err;
-	do 
+
+
+	do
 	{
-		memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
 		mbox->cmd.command = FR_READ_STATISTICS;
+		mbox->cmd.length = 0;
+		mbox->cmd.dlci = 0;
 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
-	}
-	while (err && retry-- && fr_event(card, err, mbox));
-	
-	if (!err) 
-	{
-		fr_link_stat_t *stats = (void *) mbox->data;
+	} while (err && retry-- && fr_event(card, err, mbox));
+
+	if (!err) {
+		fr_link_stat_t* stats = (void*)mbox->data;
 		card->wandev.stats.rx_frame_errors = stats->rx_bad_format;
-		card->wandev.stats.rx_dropped = stats->rx_dropped + stats->rx_dropped2;
+		card->wandev.stats.rx_dropped =
+			stats->rx_dropped + stats->rx_dropped2;
 	}
+
 	return err;
 }
+
 /*============================================================================
  * Add DLCI(s) (Access Node only!).
  * This routine will perform the ADD_DLCIs command for the specified DLCI.
  */
-static int fr_add_dlci(sdla_t * card, int dlci, int num)
+static int fr_add_dlci (sdla_t* card, int dlci)
 {
-	fr_mbox_t *mbox = card->mbox;
+	fr_mbox_t* mbox = card->mbox;
 	int retry = MAX_CMD_RETRY;
-	int err, i;
-	do 
+	int err;
+
+	do
 	{
-		unsigned short *dlci_list = (void *) mbox->data;
-		memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
-		for (i = 0; i < num; ++i)
-			dlci_list[i] = card->u.f.node_dlci[i];
-		mbox->cmd.length = num * sizeof(short);
+		unsigned short* dlci_list = (void*)mbox->data;
+
+		mbox->cmd.length  = sizeof(short);
+		dlci_list[0] = dlci;
 		mbox->cmd.command = FR_ADD_DLCI;
 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
-	}
-	while (err && retry-- && fr_event(card, err, mbox));
+
+	} while (err && retry-- && fr_event(card, err, mbox));
 	
 	return err;
 }
+
 /*============================================================================
  * Activate DLCI(s) (Access Node only!). 
- * This routine will perform the ACTIVATE_DLCIs command with a list of DLCIs. 
+ * This routine will perform the ACTIVATE_DLCIs command with a DLCI number. 
  */
-static int fr_activate_dlci(sdla_t * card, int dlci, int num)
+static int fr_activate_dlci (sdla_t* card, int dlci)
 {
-	fr_mbox_t *mbox = card->mbox;
+	fr_mbox_t* mbox = card->mbox;
 	int retry = MAX_CMD_RETRY;
-	int err, i;
+	int err;
+
 	do
 	{
-		unsigned short *dlci_list = (void *) mbox->data;
-		memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
-		for (i = 0; i < num; ++i)
-			dlci_list[i] = card->u.f.node_dlci[i];
-		mbox->cmd.length = num * sizeof(short);
+		unsigned short* dlci_list = (void*)mbox->data;
+
+		mbox->cmd.length  = sizeof(short);
+		dlci_list[0] = dlci;
 		mbox->cmd.command = FR_ACTIVATE_DLCI;
 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
-	}
-	while (err && retry-- && fr_event(card, err, mbox));
+
+	} while (err && retry-- && fr_event(card, err, mbox));
 	
 	return err;
 }
+
 /*============================================================================
- * Issue in-channel signalling frame. 
+ * Delete DLCI(s) (Access Node only!). 
+ * This routine will perform the DELETE_DLCIs command with a DLCI number. 
  */
-static int fr_issue_isf(sdla_t * card, int isf)
+static int fr_delete_dlci (sdla_t* card, int dlci)
 {
-	fr_mbox_t *mbox = card->mbox;
+	fr_mbox_t* mbox = card->mbox;
 	int retry = MAX_CMD_RETRY;
 	int err;
+
 	do
 	{
-		memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
-		mbox->data[0] = isf;
-		mbox->cmd.length = 1;
-		mbox->cmd.command = FR_ISSUE_IS_FRAME;
+		unsigned short* dlci_list = (void*)mbox->data;
+
+		mbox->cmd.length  = sizeof(short);
+		dlci_list[0] = dlci;
+		mbox->cmd.command = FR_DELETE_DLCI;
 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
-	}
-	while (err && retry-- && fr_event(card, err, mbox));
+
+	} while (err && retry-- && fr_event(card, err, mbox));
 	
 	return err;
 }
+
+
+
 /*============================================================================
- * Send a frame (S502 version). 
+ * Issue in-channel signalling frame. 
  */
-static int fr502_send(sdla_t * card, int dlci, int attr, int len, void *buf)
+static int fr_issue_isf (sdla_t* card, int isf)
 {
-	fr_mbox_t *mbox = card->mbox;
+	fr_mbox_t* mbox = card->mbox;
 	int retry = MAX_CMD_RETRY;
 	int err;
-	
-	do 
+
+	do
 	{
-		memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
-		memcpy(mbox->data, buf, len);
-		mbox->cmd.dlci = dlci;
-		mbox->cmd.attr = attr;
-		mbox->cmd.length = len;
-		mbox->cmd.command = FR_WRITE;
+		mbox->data[0] = isf;
+		mbox->cmd.length  = 1;
+		mbox->cmd.command = FR_ISSUE_IS_FRAME;
 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
-	}
-	while (err && retry-- && fr_event(card, err, mbox));
+	} while (err && retry-- && fr_event(card, err, mbox));
 	
 	return err;
 }
+
 /*============================================================================
- * Send a frame (S508 version). 
+ * Send a frame on a selected DLCI.  
  */
-static int fr508_send(sdla_t * card, int dlci, int attr, int len, void *buf)
+static int fr_send (sdla_t* card, int dlci, unsigned char attr, int len,
+	void *buf)
 {
-	fr_mbox_t *mbox = card->mbox;
+	fr_mbox_t* mbox = card->mbox + 0x800;
 	int retry = MAX_CMD_RETRY;
 	int err;
-	
+
 	do
 	{
-		memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
-		mbox->cmd.dlci = dlci;
-		mbox->cmd.attr = attr;
-		mbox->cmd.length = len;
+		mbox->cmd.dlci    = dlci;
+		mbox->cmd.attr    = attr;
+		mbox->cmd.length  = len;
 		mbox->cmd.command = FR_WRITE;
 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
-	}
-	while (err && retry-- && fr_event(card, err, mbox));
-	
-	if (!err) 
-	{
-		fr_buf_ctl_t *frbuf = (void *) (*(unsigned long *) mbox->data -
-					FR_MB_VECTOR + card->hw.dpmbase);
-		sdla_poke(&card->hw, frbuf->offset, buf, len);
-		frbuf->flag = 0x01;
-	}
+	} while (err && retry-- && fr_event(card, err, mbox));
+
+	if (!err) {
+
+		fr_tx_buf_ctl_t* frbuf;
+ 
+               	if(card->hw.type == SDLA_S514)
+			frbuf = (void*)(*(unsigned long*)mbox->data +
+                        	card->hw.dpmbase);
+		else
+			frbuf = (void*)(*(unsigned long*)mbox->data -
+                        	FR_MB_VECTOR + card->hw.dpmbase);
+
+		sdla_poke(&card->hw, frbuf->offset, buf, len);
+		frbuf->flag = 0x01;
+	}
+
 	return err;
 }
 
 /****** Firmware Asynchronous Event Handlers ********************************/
 
 /*============================================================================
- * Main asynchronous event/error handler.
+ * Main asyncronous event/error handler.
  *	This routine is called whenever firmware command returns non-zero
  *	return code.
  *
  * Return zero if previous command has to be cancelled.
  */
-
-static int fr_event(sdla_t * card, int event, fr_mbox_t * mbox)
+static int fr_event (sdla_t *card, int event, fr_mbox_t* mbox)
 {
-	fr508_flags_t *flags = card->flags;
+	fr508_flags_t* flags = card->flags;
 	char *ptr = &flags->iflag;
 	int i;
-	switch (event) 
-	{
+
+	switch (event) {
+
 		case FRRES_MODEM_FAILURE:
 			return fr_modem_failure(card, mbox);
+
 		case FRRES_CHANNEL_DOWN:
+			{
+			struct net_device *dev;
+
+			/* Remove all routes from associated DLCI's */
+			for (dev = card->wandev.dev; dev; dev = dev->slave) {
+				fr_channel_t *chan = dev->priv;
+				if (chan->route_flag == ROUTE_ADDED) {
+					chan->route_flag = REMOVE_ROUTE;
+					card->poll = &process_route;
+				}
+
+				if (chan->inarp == INARP_CONFIGURED) {
+					chan->inarp = INARP_REQUEST;
+				}
+			}
+
 			wanpipe_set_state(card, WAN_DISCONNECTED);
 			return 1;
+			}
+
 		case FRRES_CHANNEL_UP:
+			{
+			struct net_device *dev;
+			int num_requests = 0;
+
+			/* Remove all routes from associated DLCI's */
+			for (dev = card->wandev.dev; dev; dev = dev->slave) {
+				fr_channel_t *chan = dev->priv;	
+				if( chan->inarp == INARP_REQUEST ){
+					num_requests++;
+					chan->inarp_tick = jiffies;
+				}
+			}
+
+			/* Allow timer interrupts */
+			if (num_requests) flags->imask |= 0x20;	
 			wanpipe_set_state(card, WAN_CONNECTED);
 			return 1;
+			}
+
 		case FRRES_DLCI_CHANGE:
 			return fr_dlci_change(card, mbox);
+
 		case FRRES_DLCI_MISMATCH:
-			printk(KERN_INFO "%s: DLCI list mismatch!\n",
-			       card->devname);
+			printk(KERN_INFO "%s: DLCI list mismatch!\n", 
+				card->devname);
 			return 1;
+
 		case CMD_TIMEOUT:
 			printk(KERN_ERR "%s: command 0x%02X timed out!\n",
-			       card->devname, mbox->cmd.command);
-			printk(KERN_INFO "%s: ID Bytes = ", card->devname);
-			for (i = 0; i < 8; i++)
-				printk(KERN_INFO "0x%02X ", *(ptr + 0x28 + i));
-			printk(KERN_INFO "\n");
+				card->devname, mbox->cmd.command);
+			printk(KERN_INFO "%s: ID Bytes = ",card->devname);
+ 	    		for(i = 0; i < 8; i ++)
+				printk(KERN_INFO "0x%02X ", *(ptr + 0x18 + i));
+	   	 	printk(KERN_INFO "\n");	
+            
 			break;
+
 		case FRRES_DLCI_INACTIVE:
-			printk(KERN_ERR "%s: DLCI %u is inactive!\n",
-			       card->devname, mbox->cmd.dlci);
 			break;
+ 
 		case FRRES_CIR_OVERFLOW:
 			break;
 		case FRRES_BUFFER_OVERFLOW:
-			break;
+			break; 
 		default:
 			printk(KERN_INFO "%s: command 0x%02X returned 0x%02X!\n"
-			       ,card->devname, mbox->cmd.command, event);
+				, card->devname, mbox->cmd.command, event);
 	}
+
 	return 0;
 }
 
@@ -2223,178 +2642,228 @@
  *
  * Return zero if previous command has to be cancelled.
  */
-static int fr_modem_failure(sdla_t * card, fr_mbox_t * mbox)
+static int fr_modem_failure (sdla_t *card, fr_mbox_t* mbox)
 {
 	printk(KERN_INFO "%s: physical link down! (modem error 0x%02X)\n",
-	       card->devname, mbox->data[0]);
-	switch (mbox->cmd.command) 
-	{
+		card->devname, mbox->data[0]);
+
+	switch (mbox->cmd.command){
 		case FR_WRITE:
+	
 		case FR_READ:
 			return 0;
 	}
+	
 	return 1;
 }
+
 /*============================================================================
  * Handle DLCI status change.
  *
  * Return zero if previous command has to be cancelled.
  */
-static int fr_dlci_change(sdla_t * card, fr_mbox_t * mbox)
+static int fr_dlci_change (sdla_t *card, fr_mbox_t* mbox)
 {
-	dlci_status_t *status = (void *) mbox->data;
+	dlci_status_t* status = (void*)mbox->data;
 	int cnt = mbox->cmd.length / sizeof(dlci_status_t);
-	fr_dlc_conf_t cfg;
 	fr_channel_t *chan;
-	struct net_device *dev2;
-	for (; cnt; --cnt, ++status) 
-	{
-		unsigned short dlci = status->dlci;
-		struct net_device *dev = find_channel(card, dlci);
-		if (dev == NULL) 
-		{
-			printk(KERN_INFO
-			       "%s: CPE contains unconfigured DLCI= %d\n",
-			       card->devname, dlci);
-		}
-		else 
-		{
-			if (status->state & 0x01) 
-			{
+	struct net_device* dev2;
+	
+
+	for (; cnt; --cnt, ++status) {
+
+		unsigned short dlci= status->dlci;
+		struct net_device* dev = find_channel(card, dlci);
+		
+		if (dev == NULL){
+			printk(KERN_INFO 
+				"%s: CPE contains unconfigured DLCI= %d\n", 
+				card->devname, dlci); 	
+
+                      printk(KERN_INFO
+                                "%s: unconfigured DLCI %d reported by network\n"
+                                , card->devname, dlci);
+ 
+		}else{
+			if (status->state == FR_LINK_INOPER) {
 				printk(KERN_INFO
-				       "%s: DLCI %u has been deleted!\n",
-				       card->devname, dlci);
+					"%s: DLCI %u is inactive!\n",
+					card->devname, dlci);
+
 				if (dev && dev->start)
 					set_chan_state(dev, WAN_DISCONNECTED);
 			}
-			else if (status->state & 0x02) 
-			{
+	
+			if (status->state & FR_DLCI_DELETED) {
+
 				printk(KERN_INFO
-				       "%s: DLCI %u becomes active!\n",
-				       card->devname, dlci);
+					"%s: DLCI %u has been deleted!\n",
+					card->devname, dlci);
+
+				if (dev && dev->start) {
+					fr_channel_t *chan = dev->priv;
+
+					if (chan->route_flag == ROUTE_ADDED) {
+						chan->route_flag = REMOVE_ROUTE;
+						card->poll = &process_route;
+					}
+
+					if (chan->inarp == INARP_CONFIGURED) {
+						chan->inarp = INARP_REQUEST;
+					}
+
+					set_chan_state(dev, WAN_DISCONNECTED);
+				}
+
+			} else if (status->state & FR_DLCI_ACTIVE) {
+
 				chan = dev->priv;
+			
 				/* This flag is used for configuring specific 
 				   DLCI(s) when they become active.
-				 */
+			 	*/ 
 				chan->dlci_configured = DLCI_CONFIG_PENDING;
+			
 				if (dev && dev->start)
 					set_chan_state(dev, WAN_CONNECTED);
+		
 			}
 		}
 	}
-	for (dev2 = card->wandev.dev; dev2; dev2 = dev2->slave) 
-	{
+	
+	for (dev2 =card->wandev.dev; dev2; dev2 = dev2->slave){
+		
 		chan = dev2->priv;
-		if (chan->dlci_configured == DLCI_CONFIG_PENDING) 
-		{
-			memset(&cfg, 0, sizeof(cfg));
-			if (chan->cir_status == CIR_DISABLED) 
-			{
-				cfg.cir_fwd = cfg.cir_bwd = 16;
-				cfg.bc_fwd = cfg.bc_bwd = 16;
-				cfg.conf_flags = 0x0001;
-				printk(KERN_INFO "%s: CIR Disabled for %s\n",
-				       card->devname, chan->name);
-			} else if (chan->cir_status == CIR_ENABLED) {
-				cfg.cir_fwd = cfg.cir_bwd = chan->cir;
-				cfg.bc_fwd = cfg.bc_bwd = chan->bc;
-				cfg.be_fwd = cfg.be_bwd = chan->be;
-				cfg.conf_flags = 0x0000;
-				printk(KERN_INFO "%s: CIR Enabled for %s\n",
-				       card->devname, chan->name);
-			}
-			if (fr_dlci_configure(card, &cfg, chan->dlci)) 
-			{
-				printk(KERN_INFO
-				    "%s: DLCI Configure failed for %d\n",
-				       card->devname, chan->dlci);
+	
+		if (chan->dlci_configured == DLCI_CONFIG_PENDING) {
+			if (fr_init_dlci(card, chan)){
 				return 1;
 			}
-			chan->dlci_configured = DLCI_CONFIGURED;
-			/* Read the interface byte mapping into the channel 
-			   structure.
-			 */
-			if (card->intr_mode == DLCI_LIST_INTR_MODE)
-				read_DLCI_IB_mapping(card, chan);
 		}
+
 	}
 	return 1;
 }
+
+
+static int fr_init_dlci (sdla_t *card, fr_channel_t *chan)
+{
+	fr_dlc_conf_t cfg;
+	fr508_flags_t* flags = card->flags;
+
+	memset(&cfg, 0, sizeof(cfg));
+
+	if ( chan->cir_status == CIR_DISABLED) {
+
+		cfg.cir_fwd = cfg.cir_bwd  = 16;
+		cfg.bc_fwd = cfg.bc_bwd = 16;
+		cfg.conf_flags = 0x0001;	
+
+	}else if (chan->cir_status == CIR_ENABLED) {
+	
+		cfg.cir_fwd = cfg.cir_bwd = chan->cir;
+		cfg.bc_fwd  = cfg.bc_bwd  = chan->bc;
+		cfg.be_fwd  = cfg.be_bwd  = chan->be;
+		cfg.conf_flags = 0x0000;
+	}
+	
+	if (fr_dlci_configure( card, &cfg , chan->dlci)){
+	printk(KERN_INFO 
+		"%s: DLCI Configure failed for %d\n",
+			card->devname, chan->dlci);
+		return 1;	
+	}
+	
+	chan->dlci_configured = DLCI_CONFIGURED;
+
+	/* Allow timer interrupts */
+	if( chan->inarp == INARP_REQUEST && card->wandev.state == WAN_CONNECTED) {
+		chan->inarp_tick = jiffies;
+		flags->imask |= 0x20;	
+	}	
+
+	/* Read the interface byte mapping into the channel 
+	   structure.
+	 */
+	read_DLCI_IB_mapping( card, chan );
+
+	return 0;
+}
 /******* Miscellaneous ******************************************************/
 
 /*============================================================================
  * Update channel state. 
  */
-static int update_chan_state(struct net_device *dev)
+static int update_chan_state (struct net_device* dev)
 {
-	fr_channel_t *chan = dev->priv;
-	sdla_t *card = chan->card;
-	fr_mbox_t *mbox = card->mbox;
+	fr_channel_t* chan = dev->priv;
+	sdla_t* card = chan->card;
+	fr_mbox_t* mbox = card->mbox;
 	int retry = MAX_CMD_RETRY;
 	int err;
-	int dlci_found = 0;
 
-	do 
+	do
 	{
-		memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
 		mbox->cmd.command = FR_LIST_ACTIVE_DLCI;
+		mbox->cmd.length = 0;
 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
-	}
-	while (err && retry-- && fr_event(card, err, mbox));
+	} while (err && retry-- && fr_event(card, err, mbox));
 
-	if (!err) 
-	{
-		unsigned short *list = (void *) mbox->data;
+	if (!err) {
+
+		unsigned short* list = (void*)mbox->data;
 		int cnt = mbox->cmd.length / sizeof(short);
-		for (; cnt; --cnt, ++list) 
-		{
-			if (*list == chan->dlci) 
-			{
-				dlci_found = 1;
-				set_chan_state(dev, WAN_CONNECTED);
+
+		for (; cnt; --cnt, ++list) {
+
+			if (*list == chan->dlci) {
+ 				set_chan_state(dev, WAN_CONNECTED);
 				break;
 			}
 		}
-		if (!dlci_found)
-			printk(KERN_INFO "%s: DLCI %u is inactive\n",
-			       card->devname, chan->dlci);
 	}
-	
+
 	return err;
 }
+
 /*============================================================================
  * Set channel state.
  */
-static void set_chan_state(struct net_device *dev, int state)
+static void set_chan_state (struct net_device* dev, int state)
 {
-	fr_channel_t *chan = dev->priv;
-	sdla_t *card = chan->card;
+	fr_channel_t* chan = dev->priv;
+	sdla_t* card = chan->card;
 	unsigned long flags;
-	
+
 	save_flags(flags);
 	cli();
-	
-	if (chan->state != state) 
-	{
-		switch (state) 
-		{
+
+	if (chan->state != state) {
+
+		switch (state) {
+
 			case WAN_CONNECTED:
-				printk(KERN_INFO "%s: interface %s connected!\n"
-				       ,card->devname, dev->name);
+				printk(KERN_INFO
+					"%s: Interface %s: DLCI %d connected\n",
+					card->devname, dev->name, chan->dlci);
 				break;
+
 			case WAN_CONNECTING:
-				printk(KERN_INFO
-				       "%s: interface %s connecting...\n",
-				       card->devname, dev->name);
+				printk(KERN_INFO 
+				      "%s: Interface %s: DLCI %d connecting\n",
+					card->devname, dev->name, chan->dlci);
 				break;
+
 			case WAN_DISCONNECTED:
-				printk(KERN_INFO
-				       "%s: interface %s disconnected!\n",
-				       card->devname, dev->name);
+				printk (KERN_INFO 
+				    "%s: Interface %s: DLCI %d disconnected!\n",
+					card->devname, dev->name, chan->dlci);
 				break;
 		}
+
 		chan->state = state;
 	}
+
 	chan->state_tick = jiffies;
 	restore_flags(flags);
 }
@@ -2402,14 +2871,14 @@
 /*============================================================================
  * Find network device by its channel number.
  */
-static struct net_device *find_channel(sdla_t * card, unsigned dlci)
+static struct net_device* find_channel (sdla_t* card, unsigned dlci)
 {
-	struct net_device *dev;
-	for (dev = card->wandev.dev; dev; dev = dev->slave)
-		if (((fr_channel_t *) dev->priv)->dlci == dlci)
-			break;
-	return dev;
+	if(dlci > HIGHEST_VALID_DLCI)
+		return NULL;
+
+	return(card->u.f.dlci_to_dev_map[dlci]);
 }
+
 /*============================================================================
  * Check to see if a frame can be sent. If no transmit buffers available,
  * enable transmit interrupts.
@@ -2417,22 +2886,17 @@
  * Return:	1 - Tx buffer(s) available
  *		0 - no buffers available
  */
-
-static int is_tx_ready(sdla_t * card, fr_channel_t * chan)
+static int is_tx_ready (sdla_t* card, fr_channel_t* chan)
 {
-	if (card->hw.fwid == SFID_FR508) 
-	{
-		unsigned char sb = inb(card->hw.port);
-		if (sb & 0x02)
-			return 1;
-	}
-	else 
-	{
-		fr502_flags_t *flags = card->flags;
-		if (flags->tx_ready)
-			return 1;
-		flags->imask |= 0x02;
-	}
+	unsigned char sb;
+
+        if(card->hw.type == SDLA_S514)
+		return 1;
+
+	sb = inb(card->hw.port);
+	if (sb & 0x02) 
+		return 1;
+
 	return 0;
 }
 
@@ -2440,688 +2904,790 @@
  * Convert decimal string to unsigned integer.
  * If len != 0 then only 'len' characters of the string are converted.
  */
-static unsigned int dec_to_uint(unsigned char *str, int len)
+static unsigned int dec_to_uint (unsigned char* str, int len)
 {
 	unsigned val;
-	if (!len)
+
+	if (!len) 
 		len = strlen(str);
+
 	for (val = 0; len && is_digit(*str); ++str, --len)
-		val = (val * 10) + (*str - (unsigned) '0');
+		val = (val * 10) + (*str - (unsigned)'0');
+
 	return val;
 }
 
+
+
+/*=============================================================================
+ * Store a UDP management packet for later processing.
+ */
+
+static int store_udp_mgmt_pkt(int udp_type, char udp_pkt_src, sdla_t* card,
+                                struct sk_buff *skb, int dlci)
+{
+        int udp_pkt_stored = 0;
+
+        if(!card->u.f.udp_pkt_lgth &&(skb->len <= MAX_LGTH_UDP_MGNT_PKT)){
+                card->u.f.udp_pkt_lgth = skb->len;
+                card->u.f.udp_type = udp_type;
+                card->u.f.udp_pkt_src = udp_pkt_src;
+                card->u.f.udp_dlci = dlci;
+                memcpy(card->u.f.udp_pkt_data, skb->data, skb->len);
+                card->u.f.timer_int_enabled |= TMR_INT_ENABLED_UDP;
+                udp_pkt_stored = 1;
+
+        }else{
+                printk(KERN_INFO "ERROR: UDP packet not stored for DLCI %d\n", 
+							dlci);
+	}
+
+        dev_kfree_skb(skb);
+
+        return(udp_pkt_stored);
+}
+
+
 /*==============================================================================
  * Process UDP call of type FPIPE8ND
  */
-
-static int process_udp_mgmt_pkt(char udp_pkt_src, sdla_t * card, struct sk_buff *skb, struct net_device *dev, int dlci, fr_channel_t * chan)
+static int process_udp_mgmt_pkt(sdla_t* card)
 {
+
 	int c_retry = MAX_CMD_RETRY;
-	unsigned char *data;
 	unsigned char *buf;
-	unsigned char buf2[5];
-	unsigned int loops, frames, len;
-	unsigned long data_ptr;
-	unsigned short real_len, buffer_length;
+	unsigned char frames;
+	unsigned int len;
+	unsigned short buffer_length;
 	struct sk_buff *new_skb;
-	unsigned char *sendpacket;
-	fr_mbox_t *mbox = card->mbox;
+	fr_mbox_t* mbox = card->mbox;
 	int err;
 	struct timeval tv;
 	int udp_mgmt_req_valid = 1;
-	sendpacket = skb->data;
-	memcpy(&buf2, &card->wandev.udp_port, 2);
-	if ((data = kmalloc(2000, GFP_ATOMIC)) == NULL) 
-	{
-		printk(KERN_INFO
-		       "%s: Error allocating memory for UDP management cmnd 0x%02X",
-		       card->devname, data[47]);
-		++chan->UDP_FPIPE_mgmt_kmalloc_err;
-		return 1;
-	}
-	memcpy(data, sendpacket, skb->len);
-	switch (data[47]) 
-	{
-		/* FPIPE_ENABLE_TRACE */
-		case 0x41:
-		/* FPIPE_DISABLE_TRACE */
-		case 0x42:
-		/* FPIPE_GET_TRACE_INFO */
-		case 0x43:
-		/* SET FT1 MODE */
-		case 0x81:
-			if (udp_pkt_src == UDP_PKT_FRM_NETWORK) 
-			{
-				++chan->UDP_FPIPE_mgmt_direction_err;
+        struct net_device* dev;
+        fr_channel_t* chan;
+        fr_udp_pkt_t *fr_udp_pkt;
+	unsigned short num_trc_els;
+	fr_trc_el_t* ptr_trc_el;
+	fr_trc_el_t trc_el;
+	fpipemon_trc_t* fpipemon_trc;
+
+	char udp_pkt_src = card->u.f.udp_pkt_src; 
+	int dlci = card->u.f.udp_dlci;
+
+	/* Find network interface for this packet */
+	dev = find_channel(card, dlci);
+        chan = dev->priv;
+
+	/* If the UDP packet is from the network, we are going to have to 
+	   transmit a response. Before doing so, we must check to see that
+	   we are not currently transmitting a frame (in 'if_send()') and
+	   that we are not already in a 'delayed transmit' state.
+	*/
+	if(udp_pkt_src == UDP_PKT_FRM_NETWORK) {
+		if (test_bit(0, (void*)&card->wandev.critical) ||
+			test_bit(2, (void*)&card->wandev.critical)) {
+			return 0;
+  		}
+		if((dev->tbusy) || (card->u.f.tx_interrupts_pending)) {
+			return 0;
+		}
+        }
+
+        fr_udp_pkt = (fr_udp_pkt_t *)card->u.f.udp_pkt_data;
+
+	switch(fr_udp_pkt->cblock.command) {
+
+		case FPIPE_ENABLE_TRACING:
+		case FPIPE_DISABLE_TRACING:
+		case FPIPE_GET_TRACE_INFO:
+		case FR_SET_FT1_MODE:
+			if(udp_pkt_src == UDP_PKT_FRM_NETWORK) {
+                    		chan->drvstats_gen.
+					UDP_PIPE_mgmt_direction_err ++;
 				udp_mgmt_req_valid = 0;
 				break;
 			}
-		/* FPIPE_FT1_READ_STATUS */
-		case 0x44:
-		/* FT1 MONITOR STATUS */
-		case 0x80:
-			if (card->hw.fwid != SFID_FR508) 
-			{
-				++chan->UDP_FPIPE_mgmt_adptr_type_err;
-				udp_mgmt_req_valid = 0;
-			}
-			break;
+
 		default:
 			break;
-	}
-	if (!udp_mgmt_req_valid) 
-	{
+       	}
+
+	if(!udp_mgmt_req_valid) {
 		/* set length to 0 */
-		data[48] = data[49] = 0;
+		fr_udp_pkt->cblock.length = 0;
 		/* set return code */
-		data[50] = (card->hw.fwid != SFID_FR508) ? 0x1F : 0xCD;
-	}
-	else
-	{
-		switch (data[47]) 
-		{
-			/* FPIPE_ENABLE_TRACE */
-			case 0x41:
-				if (!TracingEnabled) 
-				{
-					do 
-					{
-						/* SET_TRACE_CONFIGURATION */
-						mbox->cmd.command = 0x60;
-						mbox->cmd.length = 1;
-						mbox->cmd.dlci = 0x00;
-						mbox->data[0] = 0x37;
-						err = sdla_exec(mbox) ?
-							mbox->cmd.result : CMD_TIMEOUT;
-					}
-					while (err && c_retry-- && fr_event(card, err, mbox));
-	
-					if (err) 
-					{
-						TracingEnabled = 0;
-						/* set the return code */
-						data[50] = mbox->cmd.result;
-						mbox->cmd.length = 0;
-						break;
-					}
-					/* get num_frames */
-					sdla_peek(&card->hw, 0x9000, &num_frames, 2);
-					sdla_peek(&card->hw, 0x9002, &curr_trace_addr,4);
-					start_trace_addr = curr_trace_addr;
-					/* MAX_SEND_BUFFER_SIZE - 
-					 * sizeof(UDP_MGMT_PACKET) - 41 */
-					available_buffer_space = 1926;
-					/* set return code */
-					data[50] = 0;
-				} 
-				else
-				{
-					/* set return code to line trace already 
-					   enabled */
-					data[50] = 1;
-				}
-				mbox->cmd.length = 0;
-				TracingEnabled = 1;
-				break;
-			/* FPIPE_DISABLE_TRACE */
-			case 0x42:
-				if (TracingEnabled) 
-				{
-					do 
-					{
-						/* SET_TRACE_CONFIGURATION */
-						mbox->cmd.command = 0x60;
-						mbox->cmd.length = 1;
-						mbox->cmd.dlci = 0x00;
-						mbox->data[0] = 0x36;
-						err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
-					}
-					while (err && c_retry-- && fr_event(card, err, mbox));
-				}
-				/* set return code */
-				data[50] = 0;
-				mbox->cmd.length = 0;
-				TracingEnabled = 0;
-				break;
-			/* FPIPE_GET_TRACE_INFO */
-			case 0x43:
-				/* Line trace cannot be performed on the 502 */
-				if (!TracingEnabled) 
-				{
-					/* set return code */
-					data[50] = 1;
+		fr_udp_pkt->cblock.result = 0xCD; 
+	} else {   
+           
+		switch(fr_udp_pkt->cblock.command) {
+
+		case FPIPE_ENABLE_TRACING:
+			if(!card->TracingEnabled) {
+				do {
+                       			mbox->cmd.command = FR_SET_TRACE_CONFIG;
+                       			mbox->cmd.length = 1;
+                     			mbox->cmd.dlci = 0x00;
+                   			mbox->data[0] = fr_udp_pkt->data[0] | 
+						RESET_TRC;
+                    			err = sdla_exec(mbox) ? 
+					     		mbox->cmd.result : CMD_TIMEOUT;
+                       		} while (err && c_retry-- && fr_event(card, err,
+					 mbox));
+
+                        	if(err) {
+					card->TracingEnabled = 0;
+					/* set the return code */
+					fr_udp_pkt->cblock.result =
+  						mbox->cmd.result;
 					mbox->cmd.length = 0;
 					break;
 				}
-				buffer_length = 0;
-				loops = (num_frames < 20) ? num_frames : 20;
-				for (frames = 0; frames < loops; frames += 1) 
-				{
-					sdla_peek(&card->hw, curr_trace_addr, &buf2, 1);
-					/* no data on board so exit */
-					if (buf2[0] == 0x00)
-						break;
-					/* 1+sizeof(FRAME_DATA) = 9 */
-					if ((available_buffer_space - buffer_length) < 9) 
-					{
-						/* indicate we have more frames on board
-						   and exit */
-						data[62] |= 0x02;
-						break;
-					}
-					/* get frame status */
-					sdla_peek(&card->hw, curr_trace_addr + 0x05, &data[62 + buffer_length], 1);
-					/* get time stamp */
-					sdla_peek(&card->hw, curr_trace_addr + 0x06, &data[66 + buffer_length], 2);
-					/* get frame length */
-					sdla_peek(&card->hw, curr_trace_addr + 0x01, &data[64 + buffer_length], 2);
-					/* get pointer to real data */
-					sdla_peek(&card->hw, curr_trace_addr + 0x0C,&data_ptr, 4);
-					/* see if we can fit the frame into the user buffer */
-					memcpy(&real_len, &data[64 + buffer_length], 2);
-					if (data_ptr == 0 || real_len + 8 > available_buffer_space) 
-					{
-						data[63 + buffer_length] = 0x00;
-					}
-					else
-					{
-						/* we can take it next time */
-						if (available_buffer_space - buffer_length < real_len + 8) 
-						{
-							data[62] |= 0x02;
-							break;
-						}
-						/* ok, get the frame */
-						data[63 + buffer_length] = 0x01;
-						/* get the data */
-						sdla_peek(&card->hw, data_ptr, &data[68 + buffer_length], real_len);
-						/* zero the opp flag to show we got the frame */
-						buf2[0] = 0x00;
-						sdla_poke(&card->hw, curr_trace_addr, &buf2, 1);
-						/* now move onto the next frame */
-						curr_trace_addr += 16;
-						/* check if we passed the last address */
-						if (curr_trace_addr >= (start_trace_addr + num_frames * 16)) 
-							curr_trace_addr = start_trace_addr;
-						/* update buffer length and make sure 
-						   its even */
-						if (data[63 + buffer_length] == 0x01) 
-							buffer_length += real_len - 1;
-						/* for the header */
-						buffer_length += 8;
-						if (buffer_length & 0x0001)
-							buffer_length += 1;
-					}
-				}
-				/* ok now set the total number of frames passed in the 
-				   high 5 bits */
-				data[62] = (frames << 3) | data[62];
-				/* set the data length */
-				mbox->cmd.length = buffer_length;
-				memcpy(&data[48], &buffer_length, 2);
-				data[50] = 0;
-				break;
-			/* FPIPE_FT1_READ_STATUS */
-			case 0x44:
-				sdla_peek(&card->hw, 0xF020, &data[62], 2);
-				data[48] = 2;
-				data[49] = 0;
-				data[50] = 0;
-				mbox->cmd.length = 2;
-				break;
-			/* FPIPE_FLUSH_DRIVER_STATS */
-			case 0x48:
-				init_chan_statistics(chan);
-				init_global_statistics(card);
-				mbox->cmd.length = 0;
-				break;
-			case 0x49:
-				do_gettimeofday(&tv);
-				chan->router_up_time = tv.tv_sec - chan->router_start_time;
-				*(unsigned long *) &data[62] = chan->router_up_time;
-				mbox->cmd.length = 4;
-				break;
-			/* FPIPE_KILL_BOARD */
-			case 0x50:
-				break;
-			/* FT1 MONITOR STATUS */
-			case 0x80:
-				if (data[62] == 1) 
-				{
-					if (rCount++ != 0) 
-					{
-						data[50] = 0;
-						mbox->cmd.length = 1;
-						break;
-					}
-				}
-				/* Disable FT1 MONITOR STATUS */
-				if (data[62] == 0) 
-				{
-					if (--rCount != 0) 
-					{
-						data[50] = 0;
-						mbox->cmd.length = 1;
-						break;
-					}
-				}
-			default:
-				do 
-				{
-					memcpy(&mbox->cmd, &sendpacket[47], sizeof(fr_cmd_t));
-					if (mbox->cmd.length) 
-						memcpy(&mbox->data, &sendpacket[62],mbox->cmd.length);
-					err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
-				}
-				while (err && c_retry-- && fr_event(card, err, mbox));
+
+				sdla_peek(&card->hw, NO_TRC_ELEMENTS_OFF,
+						&num_trc_els, 2);
+				sdla_peek(&card->hw, BASE_TRC_ELEMENTS_OFF,
+						&card->u.f.trc_el_base, 4);
+				card->u.f.curr_trc_el = card->u.f.trc_el_base;
+             			card->u.f.trc_el_last = card->u.f.curr_trc_el +
+							((num_trc_els - 1) * 
+							sizeof(fr_trc_el_t));
+   
+				/* Calculate the maximum trace data area in */
+				/* the UDP packet */
+				card->u.f.trc_bfr_space=(MAX_LGTH_UDP_MGNT_PKT -
+					sizeof(fr_encap_hdr_t) -
+					sizeof(ip_pkt_t) -
+					sizeof(udp_pkt_t) -
+					sizeof(wp_mgmt_t) -
+					sizeof(cblock_t));
+
+				/* set return code */
+				fr_udp_pkt->cblock.result = 0;
 			
-				if (!err) 
-				{
-					++chan->UDP_FPIPE_mgmt_adptr_cmnd_OK;
-					memcpy(data, sendpacket, skb->len);
-					memcpy(&data[47], &mbox->cmd, sizeof(fr_cmd_t));
-					if (mbox->cmd.length) 
-					{
-						memcpy(&data[62], &mbox->data,mbox->cmd.length);
-					}
+			} else {
+                        	/* set return code to line trace already 
+				   enabled */
+				fr_udp_pkt->cblock.result = 1;
+                    	}
+
+			mbox->cmd.length = 0;
+			card->TracingEnabled = 1;
+			break;
+
+
+                case FPIPE_DISABLE_TRACING:
+			if(card->TracingEnabled) {
+			
+				do {
+					mbox->cmd.command = FR_SET_TRACE_CONFIG;
+					mbox->cmd.length = 1;
+					mbox->cmd.dlci = 0x00;
+					mbox->data[0] = ~ACTIVATE_TRC;
+					err = sdla_exec(mbox) ? 
+							mbox->cmd.result : CMD_TIMEOUT;
+				} while (err && c_retry-- && fr_event(card, err, mbox));
+                    	}
+
+                    	/* set return code */
+			fr_udp_pkt->cblock.result = 0;
+			mbox->cmd.length = 0;
+			card->TracingEnabled = 0;
+			break;
+
+                case FPIPE_GET_TRACE_INFO:
+
+		        /* Line trace cannot be performed on the 502 */
+                        if(!card->TracingEnabled) {
+                                /* set return code */
+                                fr_udp_pkt->cblock.result = 1;
+                                mbox->cmd.length = 0;
+                                break;
+                        }
+
+			(void *)ptr_trc_el = card->u.f.curr_trc_el;
+
+                        buffer_length = 0;
+			fr_udp_pkt->data[0x00] = 0x00;
+
+                        for(frames = 0; frames < MAX_FRMS_TRACED; frames ++) {
+
+                                sdla_peek(&card->hw, (unsigned long)ptr_trc_el,
+					  (void *)&trc_el.flag,
+					  sizeof(fr_trc_el_t));
+                                if(trc_el.flag == 0x00) {
+                                        break;
 				}
-				else
-				{
-					++chan->UDP_FPIPE_mgmt_adptr_cmnd_timeout;
+                                if((card->u.f.trc_bfr_space - buffer_length)
+                                        < sizeof(fpipemon_trc_hdr_t)) { 
+                                        fr_udp_pkt->data[0x00] |= MORE_TRC_DATA;
+                                        break;
+                                }
+
+				fpipemon_trc = 
+					(fpipemon_trc_t *)&fr_udp_pkt->data[buffer_length]; 
+				fpipemon_trc->fpipemon_trc_hdr.status =
+					trc_el.attr;
+                            	fpipemon_trc->fpipemon_trc_hdr.tmstamp =
+					trc_el.tmstamp;
+                            	fpipemon_trc->fpipemon_trc_hdr.length = 
+					trc_el.length;
+
+                                if(!trc_el.offset || !trc_el.length) {
+
+                                     	fpipemon_trc->fpipemon_trc_hdr.data_passed = 0x00;
+
+ 				}else if((trc_el.length + sizeof(fpipemon_trc_hdr_t) + 1) >
+					(card->u.f.trc_bfr_space - buffer_length)){
+
+                                        fpipemon_trc->fpipemon_trc_hdr.data_passed = 0x00;
+                                    	fr_udp_pkt->data[0x00] |= MORE_TRC_DATA;
+ 
+                                }else {
+                                        fpipemon_trc->fpipemon_trc_hdr.data_passed = 0x01;
+                                        sdla_peek(&card->hw, trc_el.offset,
+                           			  fpipemon_trc->data,
+						  trc_el.length);
+				}			
+
+                                trc_el.flag = 0x00;
+                                sdla_poke(&card->hw, (unsigned long)ptr_trc_el,
+					  &trc_el.flag, 1);
+                               
+				ptr_trc_el ++;
+				if((void *)ptr_trc_el > card->u.f.trc_el_last)
+					(void*)ptr_trc_el = card->u.f.trc_el_base;
+
+				buffer_length += sizeof(fpipemon_trc_hdr_t);
+                               	if(fpipemon_trc->fpipemon_trc_hdr.data_passed) {
+                               		buffer_length += trc_el.length;
+                               	}
+
+				if(fr_udp_pkt->data[0x00] & MORE_TRC_DATA) {
+					break;
 				}
+                        }
+                      
+			if(frames == MAX_FRMS_TRACED) {
+                        	fr_udp_pkt->data[0x00] |= MORE_TRC_DATA;
+			}
+             
+			card->u.f.curr_trc_el = (void *)ptr_trc_el;
+
+                        /* set the total number of frames passed */
+			fr_udp_pkt->data[0x00] |=
+				((frames << 1) & (MAX_FRMS_TRACED << 1));
+
+                        /* set the data length and return code */
+			fr_udp_pkt->cblock.length = mbox->cmd.length = buffer_length;
+                        fr_udp_pkt->cblock.result = 0;
+                        break;
+
+                case FPIPE_FT1_READ_STATUS:
+			sdla_peek(&card->hw, 0xF020,
+				&fr_udp_pkt->data[0x00] , 2);
+			fr_udp_pkt->cblock.length = 2;
+			fr_udp_pkt->cblock.result = 0;
+			break;
+
+		case FPIPE_FLUSH_DRIVER_STATS:
+			init_chan_statistics(chan);
+			init_global_statistics(card);
+			mbox->cmd.length = 0;
+			break;
+		
+		case FPIPE_ROUTER_UP_TIME:
+			do_gettimeofday(&tv);
+			chan->router_up_time = tv.tv_sec - 
+						chan->router_start_time;
+    	                *(unsigned long *)&fr_udp_pkt->data =
+    				chan->router_up_time;	
+			mbox->cmd.length = 4;
+			break;
+
+
+		case FR_FT1_STATUS_CTRL:
+			if(fr_udp_pkt->data[0] == 1) {
+				if(rCount++ != 0 ){
+					fr_udp_pkt->cblock.result = 0;
+					mbox->cmd.length = 1;
+					break;
+				} 
 			}
-	}
-	/* Fill UDP TTL */
-	data[10] = card->wandev.ttl;
-	len = reply_udp(data, mbox->cmd.length);
-	if (udp_pkt_src == UDP_PKT_FRM_NETWORK) 
-	{
-		err = fr508_send(card, dlci, 0, len, data);
-		if (err)
-			++chan->UDP_FPIPE_mgmt_adptr_send_passed;
+           
+			/* Disable FT1 MONITOR STATUS */
+                        if(fr_udp_pkt->data[0] == 0) {
+				if( --rCount != 0) {
+                                        fr_udp_pkt->cblock.result = 0;
+					mbox->cmd.length = 1;
+					break;
+				} 
+			}  
+
+		case FPIPE_DRIVER_STAT_IFSEND:
+			memcpy(fr_udp_pkt->data,
+				&chan->drvstats_if_send.if_send_entry,
+				sizeof(if_send_stat_t));
+			mbox->cmd.length = sizeof(if_send_stat_t);	
+			break;
+	
+		case FPIPE_DRIVER_STAT_INTR:
+			memcpy(fr_udp_pkt->data,
+                                &card->statistics.isr_entry,
+                                sizeof(global_stats_t));
+                        memcpy(&fr_udp_pkt->data[sizeof(global_stats_t)],
+                                &chan->drvstats_rx_intr.rx_intr_no_socket,
+                                sizeof(rx_intr_stat_t));
+			mbox->cmd.length = sizeof(global_stats_t) +
+					sizeof(rx_intr_stat_t);
+			break;
+
+		case FPIPE_DRIVER_STAT_GEN:
+                        memcpy(fr_udp_pkt->data,
+                                &chan->drvstats_gen.UDP_PIPE_mgmt_kmalloc_err,
+                                sizeof(pipe_mgmt_stat_t));
+
+                        memcpy(&fr_udp_pkt->data[sizeof(pipe_mgmt_stat_t)],
+                               &card->statistics, sizeof(global_stats_t));
+
+                        fr_udp_pkt->cblock.result = 0;
+                        fr_udp_pkt->cblock.length = sizeof(global_stats_t)+
+                                                     sizeof(rx_intr_stat_t);
+                        mbox->cmd.length = fr_udp_pkt->cblock.length;
+                        break;
+		
+		default:
+ 			do {
+				memcpy(&mbox->cmd,
+					&fr_udp_pkt->cblock.command,
+					sizeof(fr_cmd_t));
+				if(mbox->cmd.length) {
+					memcpy(&mbox->data,
+						(char *)fr_udp_pkt->data,
+						mbox->cmd.length);
+				}
+ 				
+				err = sdla_exec(mbox) ? mbox->cmd.result : 
+					CMD_TIMEOUT;
+			} while (err && c_retry-- && fr_event(card, err, mbox));
+
+			if(!err)
+				chan->drvstats_gen.
+					UDP_PIPE_mgmt_adptr_cmnd_OK ++;
+			else
+                                chan->drvstats_gen.
+					UDP_PIPE_mgmt_adptr_cmnd_timeout ++;
+
+       	                /* copy the result back to our buffer */
+			memcpy(&fr_udp_pkt->cblock.command,
+				&mbox->cmd, sizeof(fr_cmd_t));
+
+                       	if(mbox->cmd.length) {
+                               	memcpy(&fr_udp_pkt->data,
+					&mbox->data, mbox->cmd.length);
+			}
+		} 
+        }
+   
+        /* Fill UDP TTL */
+        fr_udp_pkt->ip_pkt.ttl = card->wandev.ttl;
+        len = reply_udp(card->u.f.udp_pkt_data, mbox->cmd.length);
+
+        if(udp_pkt_src == UDP_PKT_FRM_NETWORK) {
+		
+                err = fr_send(card, dlci, 0, len, card->u.f.udp_pkt_data);
+		if (err) 
+			chan->drvstats_gen.UDP_PIPE_mgmt_adptr_send_passed ++;
 		else
-			++chan->UDP_FPIPE_mgmt_adptr_send_failed;
-		dev_kfree_skb(skb);
-	}
-	else 
-	{
+			chan->drvstats_gen.UDP_PIPE_mgmt_adptr_send_failed ++;
+	} else {
 		/* Allocate socket buffer */
-		if ((new_skb = dev_alloc_skb(len)) != NULL) 
-		{
+		if((new_skb = dev_alloc_skb(len)) != NULL) {
+
 			/* copy data into new_skb */
 			buf = skb_put(new_skb, len);
-			memcpy(buf, data, len);
+			memcpy(buf, card->u.f.udp_pkt_data, len);
+        
 			/* Decapsulate packet and pass it up the protocol 
 			   stack */
 			new_skb->dev = dev;
-			buf = skb_pull(new_skb, 1);	/* remove hardware header */
-			if (!wanrouter_type_trans(new_skb, dev)) 
-			{
-				++chan->UDP_FPIPE_mgmt_not_passed_to_stack;
+			buf = skb_pull(new_skb, 1);  /* remove hardware header*/
+			
+			if(!wanrouter_type_trans(new_skb, dev)) {
+
+				chan->drvstats_gen.
+					UDP_PIPE_mgmt_not_passed_to_stack ++;
 				/* can't decapsulate packet */
 				dev_kfree_skb(new_skb);
-			}
-			else 
-			{
-				++chan->UDP_FPIPE_mgmt_passed_to_stack;
+			} else {
+				chan->drvstats_gen.
+					UDP_PIPE_mgmt_passed_to_stack ++;
 				netif_rx(new_skb);
-			}
-		}
-		else 
-		{
-			++chan->UDP_FPIPE_mgmt_no_socket;
-			printk(KERN_INFO
-			       "%s: UDP mgmt cmnd, no socket buffers available!\n",
-			       card->devname);
-		}
-	}
-	kfree(data);
-	return 0;
+                	}
+            	
+		} else {
+			chan->drvstats_gen.UDP_PIPE_mgmt_no_socket ++;
+			printk(KERN_INFO 
+			"%s: UDP mgmt cmnd, no socket buffers available!\n", 
+			card->devname);
+            	}
+        }
+
+	card->u.f.udp_pkt_lgth = 0;
+
+	return 1;
 }
+
 /*==============================================================================
- * Perform the Interrupt Test by running the READ_CODE_VERSION command MAX_INTR_
- * TEST_COUNTER times.
+ * Send Inverse ARP Request
  */
- 
-static int intr_test(sdla_t * card)
+
+int send_inarp_request(sdla_t *card, struct net_device *dev)
 {
-	fr_mbox_t *mb = card->mbox;
-	int err, i;
-	/* The critical flag is unset here because we want to get into the
-	   ISR without the flag already set. The If_open sets the flag.
-	 */
-	card->wandev.critical = 0;
-	err = fr_set_intr_mode(card, 0x08, card->wandev.mtu);
-	if (err == CMD_OK) 
-	{
-		for (i = 0; i < MAX_INTR_TEST_COUNTER; i++) 
-		{
-			/* Run command READ_CODE_VERSION */
-			memset(&mb->cmd, 0, sizeof(fr_cmd_t));
-			mb->cmd.length = 0;
-			mb->cmd.command = 0x40;
-			err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
-			if (err != CMD_OK)
-				fr_event(card, err, mb);
-		}
-	} 
-	else 
-	{
-		return err;
+	arphdr_1490_t *ArpPacket;
+	arphdr_fr_t *arphdr;
+	fr_channel_t *chan = dev->priv;
+	struct in_device *in_dev;
+
+	in_dev = dev->ip_ptr;
+	
+	if(in_dev != NULL ) {	
+
+		ArpPacket = kmalloc(sizeof(arphdr_1490_t) + sizeof(arphdr_fr_t), GFP_ATOMIC);
+		/* SNAP Header indicating ARP */
+		ArpPacket->control	= 0x03;
+		ArpPacket->pad		= 0x00;
+		ArpPacket->NLPID	= 0x80;
+		ArpPacket->OUI[0]	= 0;
+		ArpPacket->OUI[1]	= 0;
+		ArpPacket->OUI[2]	= 0;
+		ArpPacket->PID		= 0x0608;
+
+		arphdr = (arphdr_fr_t *)(ArpPacket + 1); // Go to ARP Packet
+
+		/* InARP request */		
+		arphdr->ar_hrd = 0x0F00;	/* Frame Relay HW type */
+		arphdr->ar_pro = 0x0008;	/* IP Protocol	       */
+		arphdr->ar_hln = 2;		/* HW addr length      */
+		arphdr->ar_pln = 4;		/* IP addr length      */
+		arphdr->ar_op = htons(0x08);	/* InARP Request       */
+		arphdr->ar_sha = 0; 		/* src HW DLCI - Doesn't matter */
+		if(in_dev->ifa_list != NULL)
+			arphdr->ar_sip = in_dev->ifa_list->ifa_local;  /* Local Address       */else
+			arphdr->ar_sip = 0;
+		arphdr->ar_tha = 0; 		/* dst HW DLCI - Doesn't matter */
+		arphdr->ar_tip = 0;		/* Remote Address -- what we want */
+
+		printk(KERN_INFO "%s: Sending InARP request on DLCI %d.\n", card->devname, chan->dlci);
+                fr_send(card, chan->dlci, 0,
+		   sizeof(arphdr_1490_t) + sizeof(arphdr_fr_t),
+		   (void *)ArpPacket);
+		kfree(ArpPacket);
 	}
-	err = fr_set_intr_mode(card, 0, card->wandev.mtu);
-	if (err != CMD_OK)
-		return err;
-	card->wandev.critical = 1;
-	return 0;
+
+	return 1;
 }
-/*============================================================================
- * Process UDP call of type DRVSTATS.  
+	
+
+/*==============================================================================
+ * Check packet for ARP Type
  */
-static int process_udp_driver_call(char udp_pkt_src, sdla_t * card, struct sk_buff *skb, struct net_device *dev, int dlci, fr_channel_t * chan)
+
+int is_arp(void *buf)
 {
-	int c_retry = MAX_CMD_RETRY;
-	unsigned char *sendpacket;
-	unsigned char buf2[5];
-	unsigned char *data;
-	unsigned char *buf;
-	unsigned int len;
-	fr_mbox_t *mbox = card->mbox;
-	struct sk_buff *new_skb;
-	int err;
-	sendpacket = skb->data;
-	memcpy(&buf2, &card->wandev.udp_port, 2);
-	if ((data = kmalloc(2000, GFP_ATOMIC)) == NULL) 
-	{
-		printk(KERN_INFO
-		       "%s: Error allocating memory for UDP DRIVER STATS cmnd0x%02X"
-		       ,card->devname, data[45]);
-		++chan->UDP_DRVSTATS_mgmt_kmalloc_err;
+	arphdr_1490_t *arphdr = (arphdr_1490_t *)buf;
+	
+	if (arphdr->pad   == 0x00  &&
+	    arphdr->NLPID == 0x80  &&
+	    arphdr->PID   == 0x0608) 
 		return 1;
-	}
-	memcpy(data, sendpacket, skb->len);
-	switch (data[47]) 
-	{
-		case 0x45:
-			*(unsigned long *) &data[62] = chan->if_send_entry;
-			*(unsigned long *) &data[66] = chan->if_send_skb_null;
-			*(unsigned long *) &data[70] = chan->if_send_broadcast;
-			*(unsigned long *) &data[74] = chan->if_send_multicast;
-			*(unsigned long *) &data[78] = chan->if_send_critical_ISR;
-			*(unsigned long *) &data[82] = chan->if_send_critical_non_ISR;
-			*(unsigned long *) &data[86] = chan->if_send_busy;
-			*(unsigned long *) &data[90] = chan->if_send_busy_timeout;
-			*(unsigned long *) &data[94] = chan->if_send_DRVSTATS_request;
-			*(unsigned long *) &data[98] = chan->if_send_FPIPE_request;
-			*(unsigned long *) &data[102] = chan->if_send_wan_disconnected;
-			*(unsigned long *) &data[106] = chan->if_send_dlci_disconnected;
-			*(unsigned long *) &data[110] = chan->if_send_no_bfrs;
-			*(unsigned long *) &data[114] = chan->if_send_adptr_bfrs_full;
-			*(unsigned long *) &data[118] = chan->if_send_bfrs_passed_to_adptr;
-			*(unsigned long *) &data[120] = card->irq_dis_if_send_count;
-			mbox->cmd.length = 62;
-			break;
-		case 0x46:
-			*(unsigned long *) &data[62] = card->statistics.isr_entry;
-			*(unsigned long *) &data[66] = card->statistics.isr_already_critical;
-			*(unsigned long *) &data[70] = card->statistics.isr_rx;
-			*(unsigned long *) &data[74] = card->statistics.isr_tx;
-			*(unsigned long *) &data[78] = card->statistics.isr_intr_test;
-			*(unsigned long *) &data[82] = card->statistics.isr_spurious;
-			*(unsigned long *) &data[86] = card->statistics.isr_enable_tx_int;
-			*(unsigned long *) &data[90] = card->statistics.tx_intr_dev_not_started;
-			*(unsigned long *) &data[94] = card->statistics.rx_intr_corrupt_rx_bfr;
-			*(unsigned long *) &data[98] = card->statistics.rx_intr_on_orphaned_DLCI;
-			*(unsigned long *) &data[102] = chan->rx_intr_no_socket;
-			*(unsigned long *) &data[106] = chan->rx_intr_dev_not_started;
-			*(unsigned long *) &data[110] = chan->rx_intr_DRVSTATS_request;
-			*(unsigned long *) &data[114] = chan->rx_intr_FPIPE_request;
-			*(unsigned long *) &data[118] = chan->rx_intr_bfr_not_passed_to_stack;
-			*(unsigned long *) &data[122] = chan->rx_intr_bfr_passed_to_stack;
-			mbox->cmd.length = 64;
-			break;
-		case 0x47:
-			*(unsigned long *) &data[62] = chan->UDP_FPIPE_mgmt_kmalloc_err;
-			*(unsigned long *) &data[66] = chan->UDP_FPIPE_mgmt_adptr_type_err;
-			*(unsigned long *) &data[70] = chan->UDP_FPIPE_mgmt_direction_err;
-			*(unsigned long *) &data[74] = chan->UDP_FPIPE_mgmt_adptr_cmnd_timeout;
-			*(unsigned long *) &data[78] = chan->UDP_FPIPE_mgmt_adptr_cmnd_OK;
-			*(unsigned long *) &data[82] = chan->UDP_FPIPE_mgmt_adptr_send_passed;
-			*(unsigned long *) &data[86] = chan->UDP_FPIPE_mgmt_adptr_send_failed;
-			*(unsigned long *) &data[90] = chan->UDP_FPIPE_mgmt_no_socket;
-			*(unsigned long *) &data[94] = chan->UDP_FPIPE_mgmt_not_passed_to_stack;
-			*(unsigned long *) &data[98] = chan->UDP_FPIPE_mgmt_passed_to_stack;
-			*(unsigned long *) &data[102] = chan->UDP_DRVSTATS_mgmt_kmalloc_err;
-			*(unsigned long *) &data[106] = chan->UDP_DRVSTATS_mgmt_adptr_cmnd_timeout;
-			*(unsigned long *) &data[110] = chan->UDP_DRVSTATS_mgmt_adptr_cmnd_OK;
-			*(unsigned long *) &data[114] = chan->UDP_DRVSTATS_mgmt_adptr_send_passed;
-			*(unsigned long *) &data[118] = chan->UDP_DRVSTATS_mgmt_adptr_send_failed;
-			*(unsigned long *) &data[122] = chan->UDP_DRVSTATS_mgmt_no_socket;
-			*(unsigned long *) &data[126] = chan->UDP_DRVSTATS_mgmt_not_passed_to_stack;
-			*(unsigned long *) &data[130] = chan->UDP_DRVSTATS_mgmt_passed_to_stack;
-			*(unsigned long *) &data[134] = card->statistics.poll_entry;
-			*(unsigned long *) &data[138] = card->statistics.poll_already_critical;
-			*(unsigned long *) &data[142] = card->statistics.poll_processed;
-			*(unsigned long *) &data[144] = card->irq_dis_poll_count;
-			mbox->cmd.length = 86;
-			break;
-		default:
-			do 
-			{
-				memcpy(&mbox->cmd, &sendpacket[47], sizeof(fr_cmd_t));
-				if (mbox->cmd.length) 
-					memcpy(&mbox->data, &sendpacket[62], mbox->cmd.length);
-				err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
-			} 
-			while (err && c_retry-- && fr_event(card, err, mbox));
+	else return 0;
+}
+
+/*==============================================================================
+ * Process ARP Packet Type
+ */
+
+int process_ARP(arphdr_1490_t *ArpPacket, sdla_t *card, struct net_device* dev)
+{
+
+	arphdr_fr_t *arphdr = (arphdr_fr_t *)(ArpPacket + 1); /* Skip header */
+	fr_rx_buf_ctl_t* frbuf = card->rxmb;
+	struct in_device *in_dev;
+
+
+	in_dev = dev->ip_ptr;
+	if( in_dev != NULL && in_dev->ifa_list != NULL) {
+		switch (ntohs(arphdr->ar_op)) {
+
+		case 0x08:  // Inverse ARP request  -- Send Reply, add route.
 			
-			if (!err) 
-			{
-				++chan->UDP_DRVSTATS_mgmt_adptr_cmnd_OK;
-				memcpy(data, sendpacket, skb->len);
-				memcpy(&data[47], &mbox->cmd, sizeof(fr_cmd_t));
-				if (mbox->cmd.length) 
-					memcpy(&data[62], &mbox->data, mbox->cmd.length);
-			} 
-			else 
-			{
-				++chan->UDP_DRVSTATS_mgmt_adptr_cmnd_timeout;
+			/* Check for valid Address */
+			printk(KERN_INFO "%s: Recvd PtP addr %s -InArp Req\n", ((fr_channel_t *)dev->priv)->name, in_ntoa(arphdr->ar_sip));
+
+			if ((in_dev->ifa_list->ifa_mask & arphdr->ar_sip) != (in_dev->ifa_list->ifa_mask & in_dev->ifa_list->ifa_local)) {
+				printk(KERN_INFO "%s: Invalid PtP address.  InARP ignored.\n", card->devname);
+				printk(KERN_INFO "mask %X\n", in_dev->ifa_list->ifa_mask);
+				printk(KERN_INFO "local %X\n", in_dev->ifa_list->ifa_local);
+				return -1;
 			}
-	}
-	/* Fill UDP TTL */
-	data[10] = card->wandev.ttl;
-	len = reply_udp(data, mbox->cmd.length);
-	if (udp_pkt_src == UDP_PKT_FRM_NETWORK) 
-	{
-		err = fr508_send(card, dlci, 0, len, data);
-		if (err)
-			++chan->UDP_DRVSTATS_mgmt_adptr_send_failed;
-		else
-			++chan->UDP_DRVSTATS_mgmt_adptr_send_passed;
-		dev_kfree_skb(skb);
-	}
-	else
-	{
-		/* Allocate socket buffer */
-		if ((new_skb = dev_alloc_skb(len)) != NULL) 
-		{
-			/* copy data into new_skb */
-			buf = skb_put(new_skb, len);
-			memcpy(buf, data, len);
-			/* Decapsulate packet and pass it up the 
-			   protocol stack */
-			new_skb->dev = dev;
-			/* remove hardware header */
-			buf = skb_pull(new_skb, 1);
-			if (!wanrouter_type_trans(new_skb, dev)) 
+		
+			if (in_dev->ifa_list->ifa_local == arphdr->ar_sip) {
+				printk(KERN_INFO "%s: Local addr = PtP addr.  InARP ignored.\n", card->devname);
+				return -1;
+			}
+	
+			arphdr->ar_op = htons(0x09);	/* InARP Reply */
+
+			/* Set addresses */
+			arphdr->ar_tip = arphdr->ar_sip;
+			arphdr->ar_sip = in_dev->ifa_list->ifa_local;
+
+                        fr_send(card, frbuf->dlci, 0, frbuf->length, (void *)ArpPacket);
+
+			/* Modify Point-to-Point Address */
 			{
-				/* can't decapsulate packet */
-				++chan->UDP_DRVSTATS_mgmt_not_passed_to_stack;
-				dev_kfree_skb(new_skb);
+			struct ifreq if_info;
+			struct sockaddr_in *if_data;
+			mm_segment_t fs = get_fs();
+			int err;
+
+			/* Set remote addresses */
+			memset(&if_info, 0, sizeof(if_info));
+			strcpy(if_info.ifr_name, dev->name);
+
+          		set_fs(get_ds());     /* get user space block */ 
+	
+			if_data = (struct sockaddr_in *)&if_info.ifr_dstaddr;
+			if_data->sin_addr.s_addr = arphdr->ar_tip;
+			if_data->sin_family = AF_INET;
+			err = devinet_ioctl( SIOCSIFDSTADDR, &if_info );
+
+                	set_fs(fs);           /* restore old block */
 			}
-			else
+				
+			/* Add Route Flag */
+			/* The route will be added in the polling routine so
+			   that it is not interrupt context. */
+
+			((fr_channel_t *) dev->priv)->route_flag = ADD_ROUTE;
+			card->poll	= &process_route;
+
+			break;
+
+		case 0x09:  // Inverse ARP reply
+
+			/* Check for valid Address */
+			printk(KERN_INFO "%s: Recvd PtP addr %s -InArp Reply\n", ((fr_channel_t *)dev->priv)->name, in_ntoa(arphdr->ar_sip));
+
+			if ((in_dev->ifa_list->ifa_mask & arphdr->ar_sip) != (in_dev->ifa_list->ifa_mask & in_dev->ifa_list->ifa_local)) {
+				printk(KERN_INFO "%s: Invalid PtP address.  InARP ignored.\n", card->devname);
+				return -1;
+			}
+
+			if (in_dev->ifa_list->ifa_local == arphdr->ar_sip) {
+				printk(KERN_INFO "%s: Local addr = PtP addr.  InARP ignored.\n", card->devname);
+				return -1;
+			}			
+
+			/* Modify Point-to-Point Address */
 			{
-				++chan->UDP_DRVSTATS_mgmt_passed_to_stack;
-				netif_rx(new_skb);
+			struct ifreq if_info;
+			struct sockaddr_in *if_data;
+			mm_segment_t fs = get_fs();
+			int err;
+
+			/* Set remote addresses */
+			memset(&if_info, 0, sizeof(if_info));
+			strcpy(if_info.ifr_name, dev->name);
+
+          		set_fs(get_ds());     /* get user space block */ 
+	
+			if_data = (struct sockaddr_in *)&if_info.ifr_dstaddr;
+			if_data->sin_addr.s_addr = arphdr->ar_sip;
+			if_data->sin_family = AF_INET;
+			err = devinet_ioctl( SIOCSIFDSTADDR, &if_info );
+
+                	set_fs(fs);           /* restore old block */
 			}
+
+			/* Add Route Flag */
+			/* The route will be added in the polling routine so
+			   that it is not interrupt context. */
+
+			((fr_channel_t *) dev->priv)->route_flag = ADD_ROUTE;
+			((fr_channel_t *) dev->priv)->inarp = INARP_CONFIGURED;
+			card->poll	= &process_route;
+			
+			break;
+		default:  // ARP's and RARP's -- Shouldn't happen.
 		}
-		else
-		{
-			++chan->UDP_DRVSTATS_mgmt_no_socket;
-			printk(KERN_INFO "%s: UDP mgmt cmnd, no socket buffers available!\n", card->devname);
+	}
+
+	return 0;	
+}
+	
+
+/*==============================================================================
+ * Perform the Interrupt Test by running the READ_CODE_VERSION command MAX_INTR_
+ * TEST_COUNTER times.
+ */
+static int intr_test( sdla_t* card )
+{
+	fr_mbox_t* mb = card->mbox;
+	int err,i;
+
+	/* The critical flag is unset here because we want to get into the
+	   ISR without the flag already set. The If_open sets the flag.
+	*/
+        clear_bit(1, (void*)&card->wandev.critical);
+
+        err = fr_set_intr_mode(card, FR_INTR_READY, card->wandev.mtu, 0 );
+	
+	if (err == CMD_OK) {
+
+		for ( i = 0; i < MAX_INTR_TEST_COUNTER; i++ ) {
+ 			/* Run command READ_CODE_VERSION */
+			mb->cmd.length  = 0;
+			mb->cmd.command = FR_READ_CODE_VERSION;
+			err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
+			if (err != CMD_OK) 
+				fr_event(card, err, mb);
 		}
+	
+	} else {
+		return err;	
 	}
-	kfree(data);
+
+	err = fr_set_intr_mode( card, 0, card->wandev.mtu, 0 );
+
+	if( err != CMD_OK ) 
+		return err;
+
+	set_bit(1, (void*)&card->wandev.critical);
 	return 0;
 }
 
 /*==============================================================================
- * Determine what type of UDP call it is. DRVSTATS or FPIPE8ND ?
+ * Determine what type of UDP call it is. FPIPE8ND ?
  */
-
-static int udp_pkt_type(struct sk_buff *skb, sdla_t * card)
+static int udp_pkt_type( struct sk_buff *skb, sdla_t* card )
 {
-	unsigned char *sendpacket;
-	unsigned char buf2[5];
-	sendpacket = skb->data;
-	memcpy(&buf2, &card->wandev.udp_port, 2);
-	if (sendpacket[2] == 0x45 &&	/* IP packet */
-	    sendpacket[11] == 0x11 &&	/* UDP packet */
-	    sendpacket[24] == buf2[1] &&	/* UDP Port */
-	    sendpacket[25] == buf2[0] &&
-	    sendpacket[38] == 0x01) 
-	{
-		if (sendpacket[30] == 0x46 &&	/* FPIPE8ND: Signature */
-		    sendpacket[31] == 0x50 &&
-		    sendpacket[32] == 0x49 &&
-		    sendpacket[33] == 0x50 &&
-		    sendpacket[34] == 0x45 &&
-		    sendpacket[35] == 0x38 &&
-		    sendpacket[36] == 0x4E &&
-		    sendpacket[37] == 0x44) 
-		{
-			return UDP_FPIPE_TYPE;
-		} else if (sendpacket[30] == 0x44 &&	/* DRVSTATS: Signature */
-			   sendpacket[31] == 0x52 &&
-			   sendpacket[32] == 0x56 &&
-			   sendpacket[33] == 0x53 &&
-			   sendpacket[34] == 0x54 &&
-			   sendpacket[35] == 0x41 &&
-			   sendpacket[36] == 0x54 &&
-			   sendpacket[37] == 0x53) 
-		{
-			return UDP_DRVSTATS_TYPE;
-		}
-		else
-			return UDP_INVALID_TYPE;
+	fr_udp_pkt_t *fr_udp_pkt = (fr_udp_pkt_t *)skb->data;
+
+        if((fr_udp_pkt->ip_pkt.protocol == UDPMGMT_UDP_PROTOCOL) &&
+		(fr_udp_pkt->ip_pkt.ver_inet_hdr_length == 0x45) &&
+		(fr_udp_pkt->udp_pkt.udp_dst_port == 
+		ntohs(card->wandev.udp_port)) &&
+		(fr_udp_pkt->wp_mgmt.request_reply == 
+		UDPMGMT_REQUEST)) {
+                        if(!strncmp(fr_udp_pkt->wp_mgmt.signature,
+                                UDPMGMT_FPIPE_SIGNATURE, 8))
+                                return UDP_FPIPE_TYPE;
 	}
-	else
-		return UDP_INVALID_TYPE;
+
+        return UDP_INVALID_TYPE;
 }
+
+
 /*==============================================================================
  * Initializes the Statistics values in the fr_channel structure.
  */
- 
-void init_chan_statistics(fr_channel_t * chan)
+void init_chan_statistics( fr_channel_t* chan)
 {
-	chan->if_send_entry = 0;
-	chan->if_send_skb_null = 0;
-	chan->if_send_broadcast = 0;
-	chan->if_send_multicast = 0;
-	chan->if_send_critical_ISR = 0;
-	chan->if_send_critical_non_ISR = 0;
-	chan->if_send_busy = 0;
-	chan->if_send_busy_timeout = 0;
-	chan->if_send_FPIPE_request = 0;
-	chan->if_send_DRVSTATS_request = 0;
-	chan->if_send_wan_disconnected = 0;
-	chan->if_send_dlci_disconnected = 0;
-	chan->if_send_no_bfrs = 0;
-	chan->if_send_adptr_bfrs_full = 0;
-	chan->if_send_bfrs_passed_to_adptr = 0;
-	chan->rx_intr_no_socket = 0;
-	chan->rx_intr_dev_not_started = 0;
-	chan->rx_intr_FPIPE_request = 0;
-	chan->rx_intr_DRVSTATS_request = 0;
-	chan->rx_intr_bfr_not_passed_to_stack = 0;
-	chan->rx_intr_bfr_passed_to_stack = 0;
-	chan->UDP_FPIPE_mgmt_kmalloc_err = 0;
-	chan->UDP_FPIPE_mgmt_direction_err = 0;
-	chan->UDP_FPIPE_mgmt_adptr_type_err = 0;
-	chan->UDP_FPIPE_mgmt_adptr_cmnd_OK = 0;
-	chan->UDP_FPIPE_mgmt_adptr_cmnd_timeout = 0;
-	chan->UDP_FPIPE_mgmt_adptr_send_passed = 0;
-	chan->UDP_FPIPE_mgmt_adptr_send_failed = 0;
-	chan->UDP_FPIPE_mgmt_not_passed_to_stack = 0;
-	chan->UDP_FPIPE_mgmt_passed_to_stack = 0;
-	chan->UDP_FPIPE_mgmt_no_socket = 0;
-	chan->UDP_DRVSTATS_mgmt_kmalloc_err = 0;
-	chan->UDP_DRVSTATS_mgmt_adptr_cmnd_OK = 0;
-	chan->UDP_DRVSTATS_mgmt_adptr_cmnd_timeout = 0;
-	chan->UDP_DRVSTATS_mgmt_adptr_send_passed = 0;
-	chan->UDP_DRVSTATS_mgmt_adptr_send_failed = 0;
-	chan->UDP_DRVSTATS_mgmt_not_passed_to_stack = 0;
-	chan->UDP_DRVSTATS_mgmt_passed_to_stack = 0;
-	chan->UDP_DRVSTATS_mgmt_no_socket = 0;
+        memset(&chan->drvstats_if_send.if_send_entry, 0,
+		sizeof(if_send_stat_t));
+        memset(&chan->drvstats_rx_intr.rx_intr_no_socket, 0,
+                sizeof(rx_intr_stat_t));
+        memset(&chan->drvstats_gen.UDP_PIPE_mgmt_kmalloc_err, 0,
+                sizeof(pipe_mgmt_stat_t));
 }
+	
 /*==============================================================================
  * Initializes the Statistics values in the Sdla_t structure.
  */
-
-void init_global_statistics(sdla_t * card)
+void init_global_statistics( sdla_t* card )
 {
 	/* Intialize global statistics for a card */
-	card->statistics.isr_entry = 0;
-	card->statistics.isr_already_critical = 0;
-	card->statistics.isr_rx = 0;
-	card->statistics.isr_tx = 0;
-	card->statistics.isr_intr_test = 0;
-	card->statistics.isr_spurious = 0;
-	card->statistics.isr_enable_tx_int = 0;
-	card->statistics.rx_intr_corrupt_rx_bfr = 0;
-	card->statistics.rx_intr_on_orphaned_DLCI = 0;
-	card->statistics.tx_intr_dev_not_started = 0;
-	card->statistics.poll_entry = 0;
-	card->statistics.poll_already_critical = 0;
-	card->statistics.poll_processed = 0;
+        memset(&card->statistics.isr_entry, 0, sizeof(global_stats_t));
 }
 
-static void read_DLCI_IB_mapping(sdla_t * card, fr_channel_t * chan)
+static void read_DLCI_IB_mapping( sdla_t* card, fr_channel_t* chan )
 {
-	fr_mbox_t *mbox = card->mbox;
-	int retry = MAX_CMD_RETRY;
-	dlci_IB_mapping_t *result;
-	int err, counter, found;
-	do 
-	{
-		memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
+	fr_mbox_t* mbox = card->mbox;
+	int retry = MAX_CMD_RETRY;	
+	dlci_IB_mapping_t* result; 
+	int err, counter, found;	
+
+	do {
 		mbox->cmd.command = FR_READ_DLCI_IB_MAPPING;
+		mbox->cmd.length = 0;	
 		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
+	} while (err && retry-- && fr_event(card, err, mbox));
+
+	if( mbox->cmd.result != 0){
+		printk(KERN_INFO "%s: Read DLCI IB Mapping failed\n", 
+			chan->name);
 	}
-	while (err && retry-- && fr_event(card, err, mbox));
-	
-	if (mbox->cmd.result != 0)
-		printk(KERN_INFO "%s: Read DLCI IB Mapping failed\n", chan->name);
 
 	counter = mbox->cmd.length / sizeof(dlci_IB_mapping_t);
-	result = (void *) mbox->data;
+	result = (void *)mbox->data;
+	
 	found = 0;
-	for (; counter; --counter, ++result) 
-	{
-		if (result->dlci == chan->dlci) 
-		{
-			printk(KERN_INFO "%s: DLCI= %d, IB addr = %lx for %s\n"
-			 ,card->devname, result->dlci, result->addr_value ,chan->name);
+	for (; counter; --counter, ++result) {
+		if ( result->dlci == chan->dlci ) {
 			chan->IB_addr = result->addr_value;
-			chan->dlci_int_interface = (void *) (card->hw.dpmbase +
-					   (chan->IB_addr & 0x00001FFF));
+			if(card->hw.type == SDLA_S514){
+	             		chan->dlci_int_interface =
+					(void*)(card->hw.dpmbase +
+					chan->IB_addr);
+       			}else{ 
+				chan->dlci_int_interface = 
+					(void*)(card->hw.dpmbase + 
+					(chan->IB_addr & 0x00001FFF));
+
+			}
 			found = 1;
-			break;
-		}
+			break;	
+		} 
 	}
 	if (!found)
-		printk(KERN_INFO "%s: DLCI %d not found by IB MAPPING cmd\n",
-		       card->devname, chan->dlci);
+		printk( KERN_INFO "%s: DLCI %d not found by IB MAPPING cmd\n", 
+		card->devname, chan->dlci);
+}
+
+void s508_s514_lock(sdla_t *card, unsigned long *smp_flags)
+{
+
+	if (card->hw.type != SDLA_S514){
+#ifdef __SMP__
+		spin_lock_irqsave(&card->lock, *smp_flags);
+#else
+    		disable_irq(card->hw.irq);
+#endif
+	}
+#ifdef __SMP__
+	else{
+		spin_lock(&card->lock);
+	}	
+#endif
+}
+
+void s508_s514_unlock(sdla_t *card, unsigned long *smp_flags)
+{
+	if (card->hw.type != SDLA_S514){
+#ifdef __SMP__
+		spin_unlock_irqrestore(&card->lock, *smp_flags);
+#else
+    		enable_irq(card->hw.irq);
+#endif
+	}
+#ifdef __SMP__
+	else{
+		spin_unlock(&card->lock);
+	}	
+#endif
+
 }
 
 /****** End *****************************************************************/

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen (who was at: slshen@lbl.gov)