sdla_fr.c 137 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6
/*****************************************************************************
* sdla_fr.c	WANPIPE(tm) Multiprotocol WAN Link Driver. Frame relay module.
*
* Author(s):	Nenad Corbic  <ncorbic@sangoma.com>
*		Gideon Hack
*
Linus Torvalds's avatar
Linus Torvalds committed
7
* Copyright:	(c) 1995-2001 Sangoma Technologies Inc.
Linus Torvalds's avatar
Linus Torvalds committed
8 9 10 11 12 13
*
*		This program is free software; you can redistribute it and/or
*		modify it under the terms of the GNU General Public License
*		as published by the Free Software Foundation; either version
*		2 of the License, or (at your option) any later version.
* ============================================================================
Linus Torvalds's avatar
Linus Torvalds committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
* Nov 23, 2000  Nenad Corbic    o Added support for 2.4.X kernels
* Nov 15, 2000  David Rokavarg  
*               Nenad Corbic	o Added frame relay bridging support.
* 				  Original code from Mark Wells and Kristian Hoffmann has
* 				  been integrated into the frame relay driver.
* Nov 13, 2000  Nenad Corbic    o Added true interface type encoding option.
* 				  Tcpdump doesn't support Frame Relay inteface
* 				  types, to fix this true type option will set
* 				  the interface type to RAW IP mode.
* Nov 07, 2000  Nenad Corbic	o Added security features for UDP debugging:
*                                 Deny all and specify allowed requests.
* Nov 06, 2000  Nenad Corbic	o Wanpipe interfaces conform to raw packet interfaces.  
*                                 Moved the if_header into the if_send() routine.
*                                 The if_header() was breaking the libpcap 
*                                 support. i.e. support for tcpdump, ethereal ...
* Oct 12. 2000  Nenad Corbic    o Added error message in fr_configure
* Jul 31, 2000  Nenad Corbic	o Fixed the Router UP Time.
* Apr 28, 2000  Nenad Corbic	o Added the option to shutdown an interface
*                                 when the channel gets disconnected.
* Apr 28, 2000  Nenad Corbic 	o Added M.Grants patch: disallow duplicate
*                                 interface setups. 
* Apr 25, 2000  Nenad Corbic	o Added M.Grants patch: dynamically add/remove 
*                                 new dlcis/interfaces.
* Mar 23, 2000  Nenad Corbic 	o Improved task queue, bh handling.
* Mar 16, 2000	Nenad Corbic	o Added Inverse ARP support
* Mar 13, 2000  Nenad Corbic	o Added new socket API support.
* Mar 06, 2000  Nenad Corbic	o Bug Fix: corrupted mbox recovery.
* Feb 24, 2000  Nenad Corbic    o Fixed up FT1 UDP debugging problem.
* Dev 15, 1999  Nenad Corbic    o Fixed up header files for 2.0.X kernels
*
Linus Torvalds's avatar
Linus Torvalds committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
* 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.
* Nov 24, 1997	Jaspreet Singh	o Added counters to avoid enabling of interrupts
*				  when they have been disabled by another
*				  interface or routine (eg. wpf_poll).
* Nov 06, 1997	Jaspreet Singh	o Added INTR_TEST_MODE to avoid polling	
*				  routine disable interrupts during interrupt
*				  testing.
* Oct 20, 1997  Jaspreet Singh  o Added hooks in for Router UP time.
* Oct 16, 1997  Jaspreet Singh  o The critical flag is used to maintain flow
*                                 control by avoiding RACE conditions.  The
*                                 cli() and restore_flags() are taken out.
*                                 The fr_channel structure is appended for 
*                                 Driver Statistics.
* Oct 15, 1997  Farhan Thawar    o updated if_send() and receive for IPX
* Aug 29, 1997  Farhan Thawar    o Removed most of the cli() and sti()
*                                o Abstracted the UDP management stuff
*                                o Now use tbusy and critical more intelligently
* Jul 21, 1997  Jaspreet Singh	 o Can configure T391, T392, N391, N392 & N393
*				   through router.conf.
*				 o Protected calls to sdla_peek() by adDing 
*				   save_flags(), cli() and restore_flags().
*				 o Added error message for Inactive DLCIs in
*				   fr_event() and update_chan_state().
*				 o Fixed freeing up of buffers using kfree() 
*			           when packets are received.
* Jul 07, 1997	Jaspreet Singh	 o Added configurable TTL for UDP packets 
*				 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
*			 	   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
*				   the second board always stopped transmitting
*				   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) 
*				   forever.
*				   The code got into this stage due to an 
110
*				   interrupt occurring within the if clause for 
Linus Torvalds's avatar
Linus Torvalds committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
*				   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.
*				   The Fix checks to see if Transmit interrupts
*				   are disabled then do not make dev->tbusy = 1
* 	   			   Introduced a global variable: int_occur and
*				   added tx_int_enabled in the wan_device 
*				   structure.	
* May 21, 1997  Jaspreet Singh   o Fixed UDP Management for multiple
*                                  boards.
*
* Apr 25, 1997  Farhan Thawar    o added UDP Management stuff
*                                o fixed bug in if_send() and tx_intr() to
*                                  sleep and wakeup all devices
* Mar 11, 1997  Farhan Thawar   Version 3.1.1
*                                o fixed (+1) bug in fr508_rx_intr()
*                                o changed if_send() to return 0 if
*                                  wandev.critical() is true
*                                o free socket buffer in if_send() if
*                                  returning 0 
*                                o added tx_intr() routine
* Jan 30, 1997	Gene Kozin	Version 3.1.0
*				 o implemented exec() entry point
*				 o fixed a bug causing driver configured as
*				   a FR switch to be stuck in WAN_
*				   mode
* Jan 02, 1997	Gene Kozin	Initial version.
*****************************************************************************/

Linus Torvalds's avatar
Linus Torvalds committed
141
#include <linux/module.h>
Linus Torvalds's avatar
Linus Torvalds committed
142
#include <linux/version.h>
Linus Torvalds's avatar
Linus Torvalds committed
143 144 145 146
#include <linux/kernel.h>	/* printk(), and other useful stuff */
#include <linux/stddef.h>	/* offsetof(), etc. */
#include <linux/errno.h>	/* return codes */
#include <linux/string.h>	/* inline memset(), etc. */
Linus Torvalds's avatar
Linus Torvalds committed
147
#include <linux/slab.h>	/* kmalloc(), kfree() */
Linus Torvalds's avatar
Linus Torvalds committed
148 149 150 151 152 153 154
#include <linux/wanrouter.h>	/* WAN router definitions */
#include <linux/wanpipe.h>	/* WANPIPE common user API definitions */
#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 */	
#include <linux/in.h>		/* sockaddr_in */
Linus Torvalds's avatar
Linus Torvalds committed
155 156
#include <asm/errno.h>

Linus Torvalds's avatar
Linus Torvalds committed
157 158 159
#include <linux/ip.h>
#include <linux/if.h>

Linus Torvalds's avatar
Linus Torvalds committed
160 161 162 163 164
#include <linux/if_wanpipe_common.h>	/* Wanpipe Socket */
#include <linux/if_wanpipe.h>	

#include <linux/sdla_fr.h>		/* frame relay firmware API definitions */

165 166 167
#include <asm/uaccess.h>
#include <linux/inetdevice.h>
#include <linux/netdevice.h>
Linus Torvalds's avatar
Linus Torvalds committed
168 169 170 171 172

#include <net/route.h>          	/* Dynamic Route Creation */
#include <linux/etherdevice.h>		/* eth_type_trans() used for bridging */
#include <linux/random.h>

Linus Torvalds's avatar
Linus Torvalds committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
/****** 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 */

/* 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

#define FRAME_RELAY_API 1
Linus Torvalds's avatar
Linus Torvalds committed
194
#define MAX_BH_BUFF	10
Linus Torvalds's avatar
Linus Torvalds committed
195 196 197 198 199 200

/* 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 *****************************************************/

Linus Torvalds's avatar
Linus Torvalds committed
201
/* This is an extention of the 'struct device' we create for each network
Linus Torvalds's avatar
Linus Torvalds committed
202 203 204 205
 * interface to keep the rest of channel-specific data.
 */
typedef struct fr_channel
{
Linus Torvalds's avatar
Linus Torvalds committed
206
	wanpipe_common_t common;
Linus Torvalds's avatar
Linus Torvalds committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	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 tick_counter;	/* counter for transmit time out */
	char dev_pending_devtint;	/* interface pending dev_tint() */
	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 */
	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 */ 
228
	long inarp_ready;		/* Ready to send requests */
Linus Torvalds's avatar
Linus Torvalds committed
229 230
	int inarp_interval;		/* Time between InArp Requests */
	unsigned long inarp_tick;	/* InArp jiffies tick counter */
231
	long interface_down;		/* Bring interface down on disconnect */
Linus Torvalds's avatar
Linus Torvalds committed
232 233 234 235 236 237 238
	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 long router_up_time;

	unsigned short transmit_length;
Linus Torvalds's avatar
Linus Torvalds committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	struct sk_buff *delay_skb;

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

	/* Polling task queue. Each interface
         * has its own task queue, which is used
         * to defer events from the interrupt */
	struct tq_struct fr_poll_task;
	struct timer_list fr_arp_timer;

	u32 ip_local;
	u32 ip_remote;
255 256
	long config_dlci;
	long unconfig_dlci;
Linus Torvalds's avatar
Linus Torvalds committed
257 258 259 260 261 262 263 264 265 266

	/* Whether this interface should be setup as a gateway.
	 * Used by dynamic route setup code */
	u8  gateway;

	/* True interface type */
	u8 true_if_encoding;
	u8 fr_header[FR_HEADER_LEN];
	char fr_header_len;

Linus Torvalds's avatar
Linus Torvalds committed
267 268 269 270 271 272 273
} fr_channel_t;

/* Route Flag options */
#define NO_ROUTE	0x00
#define ADD_ROUTE 	0x01
#define ROUTE_ADDED	0x02
#define REMOVE_ROUTE 	0x03
Linus Torvalds's avatar
Linus Torvalds committed
274
#define ARP_REQ		0x04
Linus Torvalds's avatar
Linus Torvalds committed
275 276 277 278 279 280 281 282 283

/* 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
Linus Torvalds's avatar
Linus Torvalds committed
284 285 286 287
#define TMR_INT_ENABLED_ARP	0x04
#define TMR_INT_ENABLED_UPDATE_STATE 	0x08
#define TMR_INT_ENABLED_CONFIG	0x10
#define TMR_INT_ENABLED_UNCONFIG	0x20
Linus Torvalds's avatar
Linus Torvalds committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321


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;
} 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; 

/* variable for keeping track of enabling/disabling FT1 monitor status */
static int rCount = 0;

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

/* variable for keeping track of number of interrupts generated during 
 * interrupt test routine 
 */
static int Intr_test_counter;
Linus Torvalds's avatar
Linus Torvalds committed
322

Linus Torvalds's avatar
Linus Torvalds committed
323 324 325
/****** Function Prototypes *************************************************/

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

/* WANPIPE-specific entry points */
static int wpf_exec(struct sdla *card, void *u_cmd, void *u_data);

/* Network device interface */
336 337 338
static int if_init(struct net_device *dev);
static int if_open(struct net_device *dev);
static int if_close(struct net_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
339

340
static void if_tx_timeout(struct net_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
341 342 343

static int if_rebuild_hdr (struct sk_buff *skb);

344 345
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,
Linus Torvalds's avatar
Linus Torvalds committed
346
                                struct sk_buff *skb);
347
static struct net_device_stats *if_stats(struct net_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

/* Interrupt handlers */
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_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);
Linus Torvalds's avatar
Linus Torvalds committed
363
static void fr_comm_disable(sdla_t *card);
Linus Torvalds's avatar
Linus Torvalds committed
364 365 366 367 368 369 370 371
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);
Linus Torvalds's avatar
Linus Torvalds committed
372 373 374 375 376 377 378
static int fr_send_data_header(sdla_t *card, int dlci, unsigned char attr, int len,
	void *buf,unsigned char hdr_len);
static unsigned int fr_send_hdr(sdla_t *card, int dlci, unsigned int offset);

static int check_dlci_config (sdla_t *card, fr_channel_t *chan);
static void initialize_rx_tx_buffers (sdla_t *card);

Linus Torvalds's avatar
Linus Torvalds committed
379 380 381 382 383 384 385

/* 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);

/* Miscellaneous functions */
386 387 388
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);
Linus Torvalds's avatar
Linus Torvalds committed
389 390 391 392 393 394 395 396
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 );
397 398
static int setup_for_delayed_transmit(struct net_device* dev,
				      struct sk_buff *skb);
Linus Torvalds's avatar
Linus Torvalds committed
399

400 401
struct net_device *move_dev_to_next(sdla_t *card, struct net_device *dev);
static int check_tx_status(sdla_t *card, struct net_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
402 403 404

/* Frame Relay Socket API */
static void trigger_fr_bh (fr_channel_t *);
405 406 407
static void fr_bh(struct net_device *dev);
static int fr_bh_cleanup(struct net_device *dev);
static int bh_enqueue(struct net_device *dev, struct sk_buff *skb);
Linus Torvalds's avatar
Linus Torvalds committed
408

409 410 411
static void trigger_fr_poll(struct net_device *dev);
static void fr_poll(struct net_device *dev);
//static void add_gateway(struct net_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
412

413
static void trigger_unconfig_fr(struct net_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
414 415 416 417
static void unconfig_fr (sdla_t *);

static void trigger_config_fr (sdla_t *);
static void config_fr (sdla_t *);
Linus Torvalds's avatar
Linus Torvalds committed
418 419 420


/* Inverse ARP and Dynamic routing functions */
421
int process_ARP(arphdr_1490_t *ArpPacket, sdla_t *card, struct net_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
422
int is_arp(void *buf);
423
int send_inarp_request(sdla_t *card, struct net_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
424

425
static void trigger_fr_arp(struct net_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
426 427
static void fr_arp (unsigned long data);

Linus Torvalds's avatar
Linus Torvalds committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446

/* Udp management functions */
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);

/* 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);
447 448
static int setup_fr_header(struct sk_buff** skb,
			   struct net_device* dev, char op_mode);
Linus Torvalds's avatar
Linus Torvalds committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468


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

/*============================================================================
 * Frame relay protocol initialization routine.
 *
 * This routine is called by the main WANPIPE module during setup.  At this
 * point adapter is completely initialized and firmware is running.
 *  o read firmware version (to make sure it's alive)
 *  o configure adapter
 *  o initialize protocol-specific fields of the adapter data space.
 *
 * Return:	0	o.k.
 *		< 0	failure.
 */
int wpf_init(sdla_t *card, wandev_conf_t *conf)
{

	int err;
Linus Torvalds's avatar
Linus Torvalds committed
469
	fr508_flags_t* flags;
Linus Torvalds's avatar
Linus Torvalds committed
470 471 472 473 474 475

	union
	{
		char str[80];
		fr_conf_t cfg;
	} u;
Linus Torvalds's avatar
Linus Torvalds committed
476

Linus Torvalds's avatar
Linus Torvalds committed
477 478 479
	fr_buf_info_t* buf_info;
	int i;

Linus Torvalds's avatar
Linus Torvalds committed
480 481 482

	printk(KERN_INFO "\n");

Linus Torvalds's avatar
Linus Torvalds committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
	/* Verify configuration ID */
	if (conf->config_id != WANCONFIG_FR) {
		
		printk(KERN_INFO "%s: invalid configuration ID %u!\n",
			card->devname, conf->config_id);
		return -EINVAL;
	
	}

	/* Initialize protocol-specific fields of adapter data space */
	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);
			if(card->hw.type == SDLA_S514) {
				card->mbox += FR_MB_VECTOR;
                                card->flags += FR_MB_VECTOR;
			}
                        card->isr = &fr_isr;
			break;

		default:
			return -EINVAL;
	}

Linus Torvalds's avatar
Linus Torvalds committed
511 512
	flags = card->flags;

Linus Torvalds's avatar
Linus Torvalds committed
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
	/* 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);

	/* Adjust configuration */
	conf->mtu += FR_HEADER_LEN;
	conf->mtu = (conf->mtu >= MIN_LGTH_FR_DATA_CFG) ?
Linus Torvalds's avatar
Linus Torvalds committed
528
			min_t(unsigned int, conf->mtu, FR_MAX_NO_DATA_BYTES_IN_FRAME) :
Linus Torvalds's avatar
Linus Torvalds committed
529 530
                        FR_CHANNEL_MTU + FR_HEADER_LEN;
     
Linus Torvalds's avatar
Linus Torvalds committed
531
	conf->bps = min_t(unsigned int, conf->bps, 2048000);
Linus Torvalds's avatar
Linus Torvalds committed
532 533 534 535 536 537 538

	/* Initialze the configuration structure sent to the board to zero */
	memset(&u.cfg, 0, sizeof(u.cfg));

	memset(card->u.f.dlci_to_dev_map, 0, sizeof(card->u.f.dlci_to_dev_map));
 	
	/* Configure adapter firmware */
Linus Torvalds's avatar
Linus Torvalds committed
539

Linus Torvalds's avatar
Linus Torvalds committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
	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_NO:
			u.cfg.options |= 0x0800; 
			break;
		default:
			printk(KERN_INFO "%s: Illegal Signalling option\n",
					card->wandev.name);
			return -EINVAL;
	}


	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 {

		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. 
		 */

Linus Torvalds's avatar
Linus Torvalds committed
598
		card->u.f.dlci_num  = min_t(unsigned int, max_t(unsigned int, conf->u.fr.dlci_num, 1), 100);
Linus Torvalds's avatar
Linus Torvalds committed
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
	
		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)
Linus Torvalds's avatar
Linus Torvalds committed
615
		u.cfg.t391 = min_t(unsigned int, conf->u.fr.t391, 30);
Linus Torvalds's avatar
Linus Torvalds committed
616 617 618 619
	else
		u.cfg.t391 = 5;

	if (conf->u.fr.t392)
Linus Torvalds's avatar
Linus Torvalds committed
620
		u.cfg.t392 = min_t(unsigned int, conf->u.fr.t392, 30);
Linus Torvalds's avatar
Linus Torvalds committed
621 622 623 624
	else
		u.cfg.t392 = 15;

	if (conf->u.fr.n391)
Linus Torvalds's avatar
Linus Torvalds committed
625
		u.cfg.n391 = min_t(unsigned int, conf->u.fr.n391, 255);
Linus Torvalds's avatar
Linus Torvalds committed
626 627 628 629
	else
		u.cfg.n391 = 2;

	if (conf->u.fr.n392)
Linus Torvalds's avatar
Linus Torvalds committed
630
		u.cfg.n392 = min_t(unsigned int, conf->u.fr.n392, 10);
Linus Torvalds's avatar
Linus Torvalds committed
631 632 633 634
	else
		u.cfg.n392 = 3;	

	if (conf->u.fr.n393)
Linus Torvalds's avatar
Linus Torvalds committed
635
		u.cfg.n393 = min_t(unsigned int, conf->u.fr.n393, 10);
Linus Torvalds's avatar
Linus Torvalds committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
	else
		u.cfg.n393 = 4;

	if (fr_configure(card, &u.cfg))
		return -EIO;

	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);
Linus Torvalds's avatar
Linus Torvalds committed
656
	}else{	
Linus Torvalds's avatar
Linus Torvalds committed
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
		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;       
Linus Torvalds's avatar
Linus Torvalds committed
690 691
	card->disable_comm	= &disable_comm;	
	card->u.f.arp_dev 	= NULL;
Linus Torvalds's avatar
Linus Torvalds committed
692 693 694 695 696 697 698 699 700 701 702

	/* Intialize global statistics for a card */
	init_global_statistics( card );

        card->TracingEnabled          = 0;

	/* Interrupt Test */
	Intr_test_counter = 0;
	card->intr_mode = INTR_TEST_MODE;
	err = intr_test( card );

Linus Torvalds's avatar
Linus Torvalds committed
703 704 705
	printk(KERN_INFO "%s: End of Interrupt Test rc=0x%x  count=%i\n",
			card->devname,err,Intr_test_counter); 
	
Linus Torvalds's avatar
Linus Torvalds committed
706
	if (err || (Intr_test_counter < MAX_INTR_TEST_COUNTER)) {
Linus Torvalds's avatar
Linus Torvalds committed
707
		printk(KERN_ERR "%s: Interrupt Test Failed, Counter: %i\n", 
Linus Torvalds's avatar
Linus Torvalds committed
708
			card->devname, Intr_test_counter);
Linus Torvalds's avatar
Linus Torvalds committed
709
		printk(KERN_ERR "Please choose another interrupt\n");
Linus Torvalds's avatar
Linus Torvalds committed
710 711 712 713 714 715 716 717
		err = -EIO;
		return err;
	}

	printk(KERN_INFO "%s: Interrupt Test Passed, Counter: %i\n",
			card->devname, Intr_test_counter);


Linus Torvalds's avatar
Linus Torvalds committed
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
	/* Apr 28 2000. Nenad Corbic
	 * Enable commnunications here, not in if_open or new_if, since
         * interfaces come down when the link is disconnected. 
         */
	 
	/* 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)) {
		return -EIO;
	}

	flags->imask &= ~(FR_INTR_TXRDY | FR_INTR_TIMER);
 
	if (fr_comm_enable(card)) {
		return -EIO;
	}	
	wanpipe_set_state(card, WAN_CONNECTED);
	spin_lock_init(&card->u.f.if_send_lock);
	
	printk(KERN_INFO "\n");

Linus Torvalds's avatar
Linus Torvalds committed
744 745 746 747 748 749 750 751
        return 0;
}

/******* WAN Device Driver Entry Points *************************************/

/*============================================================================
 * Update device status & statistics.
 */
752
static int update(struct wan_device* wandev)
Linus Torvalds's avatar
Linus Torvalds committed
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
{
	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;

	card = wandev->private;
	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;
		}
        }
Linus Torvalds's avatar
Linus Torvalds committed
781

Linus Torvalds's avatar
Linus Torvalds committed
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
	return 0;
}

/*============================================================================
 * Create new logical channel.
 * This routine is called by the router when ROUTER_IFNEW IOCTL is being
 * handled.
 * o parse media- and hardware-specific configuration
 * o make sure that a new channel can be created
 * o allocate resources, if necessary
 * o prepare network device structure for registaration.
 *
 * Return:	0	o.k.
 *		< 0	failure (channel will not be created)
 */
797
static int new_if(struct wan_device* wandev, struct net_device* dev,
798
		  wanif_conf_t* conf)
Linus Torvalds's avatar
Linus Torvalds committed
799 800 801 802 803
{
	sdla_t* card = wandev->private;
	fr_channel_t* chan;
	int dlci = 0;
	int err = 0;
Linus Torvalds's avatar
Linus Torvalds committed
804

Linus Torvalds's avatar
Linus Torvalds committed
805 806 807
	
	if ((conf->name[0] == '\0') || (strlen(conf->name) > WAN_IFNAME_SZ)) {
		
Linus Torvalds's avatar
Linus Torvalds committed
808
		printk(KERN_INFO "%s: Invalid interface name!\n",
Linus Torvalds's avatar
Linus Torvalds committed
809 810 811 812 813 814 815 816 817 818 819 820 821
			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;
Linus Torvalds's avatar
Linus Torvalds committed
822

Linus Torvalds's avatar
Linus Torvalds committed
823 824 825 826 827 828 829 830 831 832 833 834
	/* verify media address */
	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
Linus Torvalds's avatar
Linus Torvalds committed
835
				"%s: Invalid DLCI %u on interface %s!\n",
Linus Torvalds's avatar
Linus Torvalds committed
836 837 838 839 840 841
				wandev->name, dlci, chan->name);
			err = -EINVAL;
		}

	} else {
		printk(KERN_ERR
Linus Torvalds's avatar
Linus Torvalds committed
842
			"%s: Invalid media address on interface %s!\n",
Linus Torvalds's avatar
Linus Torvalds committed
843 844 845 846
			wandev->name, chan->name);
		err = -EINVAL;
	}

Linus Torvalds's avatar
Linus Torvalds committed
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
	if ((chan->true_if_encoding = conf->true_if_encoding) == WANOPT_YES){
		printk(KERN_INFO 
			"%s: Enabling, true interface type encoding.\n",
			card->devname);
	}
	


    /* Setup wanpipe as a router (WANPIPE) even if it is
	 * a bridged DLCI, or as an API 
	 */
        if (strcmp(conf->usedby, "WANPIPE")  == 0  || 
	    strcmp(conf->usedby, "BRIDGE")   == 0  ||
	    strcmp(conf->usedby, "BRIDGE_N") == 0){
		
		if(strcmp(conf->usedby, "WANPIPE") == 0){
			chan->common.usedby = WANPIPE;
			
	                printk(KERN_INFO "%s: Running in WANPIPE mode.\n", 
					card->devname);
			
		}else if(strcmp(conf->usedby, "BRIDGE") == 0){
			
			chan->common.usedby = BRIDGE;
			
			printk(KERN_INFO "%s: Running in WANPIPE (BRIDGE) mode.\n", 
					card->devname);
		}else if( strcmp(conf->usedby, "BRIDGE_N") == 0 ){
			
			chan->common.usedby = BRIDGE_NODE;
Linus Torvalds's avatar
Linus Torvalds committed
877
		
Linus Torvalds's avatar
Linus Torvalds committed
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
			printk(KERN_INFO "%s: Running in WANPIPE (BRIDGE_NODE) mode.\n", 
					card->devname);
		}

		if (!err){
			/* Dynamic interface configuration option.
			 * On disconnect, if the options is selected,
			 * the interface will be brought down */
			if (conf->if_down == WANOPT_YES){ 
				set_bit(DYN_OPT_ON,&chan->interface_down);
				printk(KERN_INFO 
				    "%s: Dynamic interface configuration enabled.\n",
					card->devname);
			}
		}
Linus Torvalds's avatar
Linus Torvalds committed
893 894 895

        } else if(strcmp(conf->usedby, "API") == 0){

Linus Torvalds's avatar
Linus Torvalds committed
896 897
                chan->common.usedby = API;
                printk(KERN_INFO "%s: Running in API mode.\n",
Linus Torvalds's avatar
Linus Torvalds committed
898 899 900 901 902 903 904 905 906 907 908 909 910 911
			wandev->name);
        }

	if (err) {
		
		kfree(chan);
		return err;
	}

	/* place cir,be,bc and other channel specific information into the
	 * chan structure 
         */
	if (conf->cir) {

Linus Torvalds's avatar
Linus Torvalds committed
912 913
		chan->cir = max_t(unsigned int, 1,
				min_t(unsigned int, conf->cir, 512));
Linus Torvalds's avatar
Linus Torvalds committed
914 915 916 917 918 919 920 921 922 923
		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){
Linus Torvalds's avatar
Linus Torvalds committed
924 925
			chan->be = max_t(unsigned int,
				       0, min_t(unsigned int, conf->be, 511));
Linus Torvalds's avatar
Linus Torvalds committed
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
		}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;

	if (conf->inarp == WANOPT_YES){
Linus Torvalds's avatar
Linus Torvalds committed
945 946 947
		printk(KERN_INFO "%s: Inverse ARP Support Enabled\n",card->devname);
		chan->inarp = conf->inarp ? INARP_REQUEST : INARP_NONE;
		chan->inarp_interval = conf->inarp_interval ? conf->inarp_interval : 10;
Linus Torvalds's avatar
Linus Torvalds committed
948
	}else{
Linus Torvalds's avatar
Linus Torvalds committed
949
		printk(KERN_INFO "%s: Inverse ARP Support Disabled\n",card->devname);
Linus Torvalds's avatar
Linus Torvalds committed
950 951 952 953
		chan->inarp = INARP_NONE;
		chan->inarp_interval = 10;
	}

Linus Torvalds's avatar
Linus Torvalds committed
954

Linus Torvalds's avatar
Linus Torvalds committed
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
	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 */
Linus Torvalds's avatar
Linus Torvalds committed
981 982
	strcpy(dev->name,chan->name);
	
Linus Torvalds's avatar
Linus Torvalds committed
983 984 985
	dev->init = &if_init;
	dev->priv = chan;

Linus Torvalds's avatar
Linus Torvalds committed
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
	/* Initialize FR Polling Task Queue
         * We need a poll routine for each network
         * interface. 
         */
	chan->fr_poll_task.sync = 0;
	chan->fr_poll_task.routine = (void *)(void *)fr_poll;
	chan->fr_poll_task.data = dev;

	init_timer(&chan->fr_arp_timer);
	chan->fr_arp_timer.data=(unsigned long)dev;
	chan->fr_arp_timer.function = fr_arp;

	wandev->new_if_cnt++;

	/* Tells us that if this interface is a
         * gateway or not */
	if ((chan->gateway = conf->gateway) == WANOPT_YES){
		printk(KERN_INFO "%s: Interface %s is set as a gateway.\n",
			card->devname,dev->name);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1006

Linus Torvalds's avatar
Linus Torvalds committed
1007 1008 1009 1010 1011
	/* M. Grant Patch Apr 28 2000 
         * Disallow duplicate dlci configurations. */
	if (card->u.f.dlci_to_dev_map[chan->dlci] != NULL) {
		kfree(chan);
		return -EBUSY;
Linus Torvalds's avatar
Linus Torvalds committed
1012 1013
	}

Linus Torvalds's avatar
Linus Torvalds committed
1014 1015 1016 1017 1018 1019 1020
	/* Configure this dlci at a later date, when
         * the interface comes up. i.e. when if_open() 
         * executes */
	set_bit(0,&chan->config_dlci);
	
	printk(KERN_INFO "\n");

Linus Torvalds's avatar
Linus Torvalds committed
1021 1022 1023 1024 1025 1026
	return 0;
}

/*============================================================================
 * Delete logical channel.
 */
1027
static int del_if(struct wan_device* wandev, struct net_device* dev)
Linus Torvalds's avatar
Linus Torvalds committed
1028
{
Linus Torvalds's avatar
Linus Torvalds committed
1029 1030
	fr_channel_t* chan = dev->priv;
	unsigned long smp_flags=0;
Linus Torvalds's avatar
Linus Torvalds committed
1031

Linus Torvalds's avatar
Linus Torvalds committed
1032 1033 1034 1035 1036 1037 1038 1039 1040
	/* This interface is dead, make sure the 
	 * ARP timer is stopped */
	del_timer(&chan->fr_arp_timer);
	
	/* If we are a NODE, we must unconfigure this DLCI
	 * Trigger an unconfigure command that will
	 * be executed in timer interrupt. We must wait
	 * for the command to complete. */
	trigger_unconfig_fr(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1041

Linus Torvalds's avatar
Linus Torvalds committed
1042 1043 1044
	lock_adapter_irq(&wandev->lock, &smp_flags);
	wandev->new_if_cnt--;
	unlock_adapter_irq(&wandev->lock, &smp_flags);
Linus Torvalds's avatar
Linus Torvalds committed
1045 1046 1047 1048

	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070

/*=====================================================================
 * disable_comm
 *
 * Description:
 *	Disable communications.
 * 	This code runs in shutdown (sdlamain.c)
 *      under critical flag. Therefore it is not
 *      necessary to set a critical flag here 
 *
 * Usage:
 * 	Commnunications are disabled only on a card
 *      shutdown.
 */

static void disable_comm (sdla_t *card)
{
	printk(KERN_INFO "%s: Disabling Communications!\n",
			card->devname);
	fr_comm_disable(card);
}

Linus Torvalds's avatar
Linus Torvalds committed
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
/****** WANPIPE-specific entry points ***************************************/

/*============================================================================
 * Execute adapter interface command.
 */
static int wpf_exec (struct sdla* card, void* u_cmd, void* u_data)
{
	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;
	
	/* execute command */
	do
	{
		memcpy(&mbox->cmd, &cmd, sizeof(cmd));
		
		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 */
	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))
		return -EFAULT;
	return 0;
}

/****** Network Device Interface ********************************************/

/*============================================================================
 * Initialize Linux network interface.
 *
 * This routine is called only once for each interface, during Linux network
 * interface registration.  Returning anything but zero will fail interface
 * registration.
 */
1123
static int if_init(struct net_device* dev)
Linus Torvalds's avatar
Linus Torvalds committed
1124 1125 1126
{
	fr_channel_t* chan = dev->priv;
	sdla_t* card = chan->card;
1127
	struct wan_device* wandev = &card->wandev;
Linus Torvalds's avatar
Linus Torvalds committed
1128 1129 1130 1131

	/* Initialize device driver entry points */
	dev->open		= &if_open;
	dev->stop		= &if_close;
Linus Torvalds's avatar
Linus Torvalds committed
1132
	dev->hard_header	= NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1133 1134 1135 1136 1137
	dev->rebuild_header	= &if_rebuild_hdr;
	dev->hard_start_xmit	= &if_send;
	dev->get_stats		= &if_stats;
	dev->tx_timeout		= &if_tx_timeout;
	dev->watchdog_timeo	= TX_TIMEOUT;
Linus Torvalds's avatar
Linus Torvalds committed
1138 1139
	
	if (chan->common.usedby == WANPIPE || chan->common.usedby == API){
1140

Linus Torvalds's avatar
Linus Torvalds committed
1141 1142 1143 1144 1145 1146 1147 1148 1149
		/* Initialize media-specific parameters */
		if (chan->true_if_encoding){
			dev->type 		= ARPHRD_DLCI;  /* This breaks tcpdump */
		}else{
			dev->type		= ARPHRD_PPP; 	/* ARP h/w type */
		}
		
		dev->flags		|= IFF_POINTOPOINT;
		dev->flags		|= IFF_NOARP;
Linus Torvalds's avatar
Linus Torvalds committed
1150

Linus Torvalds's avatar
Linus Torvalds committed
1151 1152 1153 1154
		/* Enable Multicast addressing */
		if (chan->mc == WANOPT_YES){
			dev->flags 	|= IFF_MULTICAST;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1155

Linus Torvalds's avatar
Linus Torvalds committed
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
		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->common.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);

		/* Set transmit buffer queue length */
        	dev->tx_queue_len = 100;

	}else{
Linus Torvalds's avatar
Linus Torvalds committed
1173

Linus Torvalds's avatar
Linus Torvalds committed
1174 1175 1176 1177 1178 1179 1180 1181
		/* Setup the interface for Bridging */
		int hw_addr=0;
		ether_setup(dev);
		
		/* Use a random number to generate the MAC address */
		memcpy(dev->dev_addr, "\xFE\xFC\x00\x00\x00\x00", 6);
		get_random_bytes(&hw_addr, sizeof(hw_addr));
		*(int *)(dev->dev_addr + 2) += hw_addr;
Linus Torvalds's avatar
Linus Torvalds committed
1182
	}
Linus Torvalds's avatar
Linus Torvalds committed
1183
		
Linus Torvalds's avatar
Linus Torvalds committed
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
	/* Initialize hardware parameters (just for reference) */
	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;

	return 0;
}

/*============================================================================
 * Open network interface.
 * o if this is the first open, then enable communications and interrupts.
 * o prevent module from unloading by incrementing use count
 *
 * Return 0 if O.k. or errno.
 */
1201
static int if_open(struct net_device* dev)
Linus Torvalds's avatar
Linus Torvalds committed
1202 1203 1204 1205 1206 1207
{
	fr_channel_t* chan = dev->priv;
	sdla_t* card = chan->card;
	int err = 0;
	struct timeval tv;

1208
	if (netif_running(dev))
Linus Torvalds's avatar
Linus Torvalds committed
1209
		return -EBUSY;
Linus Torvalds's avatar
Linus Torvalds committed
1210
	
Linus Torvalds's avatar
Linus Torvalds committed
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
	/* Initialize the task queue */
	chan->tq_working=0;

	chan->common.wanpipe_task.sync = 0;
	chan->common.wanpipe_task.routine = (void *)(void *)fr_bh;
	chan->common.wanpipe_task.data = dev;

	/* Allocate and initialize BH circular buffer */
	chan->bh_head = kmalloc((sizeof(bh_data_t)*MAX_BH_BUFF),GFP_ATOMIC);
	memset(chan->bh_head,0,(sizeof(bh_data_t)*MAX_BH_BUFF));
	atomic_set(&chan->bh_buff_used, 0);

Linus Torvalds's avatar
Linus Torvalds committed
1223
	netif_start_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1224

Linus Torvalds's avatar
Linus Torvalds committed
1225 1226 1227
	wanpipe_open(card);
	do_gettimeofday( &tv );
	chan->router_start_time = tv.tv_sec;
Linus Torvalds's avatar
Linus Torvalds committed
1228 1229 1230 1231 1232 1233 1234
	
	if (test_bit(0,&chan->config_dlci)){
		trigger_config_fr (card);
	}else if (chan->inarp == INARP_REQUEST){
		trigger_fr_arp(dev);
	}
	
Linus Torvalds's avatar
Linus Torvalds committed
1235 1236 1237 1238 1239 1240 1241 1242
	return err;
}

/*============================================================================
 * Close network interface.
 * o if this is the last open, then disable communications and interrupts.
 * o reset flags.
 */
1243
static int if_close(struct net_device* dev)
Linus Torvalds's avatar
Linus Torvalds committed
1244 1245 1246 1247
{
	fr_channel_t* chan = dev->priv;
	sdla_t* card = chan->card;

Linus Torvalds's avatar
Linus Torvalds committed
1248 1249 1250
	if (chan->inarp == INARP_CONFIGURED) {
		chan->inarp = INARP_REQUEST;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1251

1252
	netif_stop_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
	wanpipe_close(card);

	return 0;
}

/*============================================================================
 * Re-build media header.
 *
 * Return:	1	physical address resolved.
 *		0	physical address not resolved
 */
static int if_rebuild_hdr (struct sk_buff* skb)
{
1266
	struct net_device *dev = skb->dev;
Linus Torvalds's avatar
Linus Torvalds committed
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
	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);
	return 1;
}

/*============================================================================
 * Handle transmit timeout event from netif watchdog
 */
1278
static void if_tx_timeout(struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
1279 1280
{
    	fr_channel_t* chan = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
1281
	sdla_t *card = chan->card;
Linus Torvalds's avatar
Linus Torvalds committed
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291

	/* 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->drvstats_if_send.if_send_tbusy++;
	++chan->ifstats.collisions;

Linus Torvalds's avatar
Linus Torvalds committed
1292 1293
	printk (KERN_INFO "%s: Transmit timed out on %s\n", 
			card->devname, dev->name);
Linus Torvalds's avatar
Linus Torvalds committed
1294
	chan->drvstats_if_send.if_send_tbusy_timeout++;
Linus Torvalds's avatar
Linus Torvalds committed
1295
	netif_wake_queue (dev);
Linus Torvalds's avatar
Linus Torvalds committed
1296 1297

}
1298

Linus Torvalds's avatar
Linus Torvalds committed
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315

/*============================================================================
 * 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.
 * o free socket buffer
 *
 * Return:	0	complete (socket buffer must be freed)
 *		non-0	packet may be re-transmitted (tbusy must be set)
 *
 * Notes:
 * 1. This routine is called either by the protocol stack or by the "net
 *    bottom half" (with interrupts enabled).
Linus Torvalds's avatar
Linus Torvalds committed
1316
 * 
1317
 * 2. Using netif_start_queue() and netif_stop_queue()
Linus Torvalds's avatar
Linus Torvalds committed
1318 1319
 *    will inhibit further transmit requests from the protocol stack 
 *    and can be used for flow control with protocol layer.
Linus Torvalds's avatar
Linus Torvalds committed
1320
 */
1321
static int if_send(struct sk_buff* skb, struct net_device* dev)
Linus Torvalds's avatar
Linus Torvalds committed
1322 1323 1324 1325 1326 1327
{
    	fr_channel_t* chan = dev->priv;
    	sdla_t* card = chan->card;
        int err;
    	unsigned char *sendpacket;
    	fr508_flags_t* adptr_flags = card->flags;
1328 1329
	int udp_type;
	long delay_tx_queued = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1330
	unsigned long smp_flags=0;
Linus Torvalds's avatar
Linus Torvalds committed
1331
	unsigned char attr = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1332 1333 1334

	chan->drvstats_if_send.if_send_entry++;

Linus Torvalds's avatar
Linus Torvalds committed
1335 1336
	netif_stop_queue(dev);
	
Linus Torvalds's avatar
Linus Torvalds committed
1337 1338 1339 1340 1341 1342 1343
        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 ++;
Linus Torvalds's avatar
Linus Torvalds committed
1344

1345
		netif_wake_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1346 1347 1348 1349 1350 1351 1352 1353 1354
		return 0;
	}

	/* If a peripheral task is running just drop packets */
	if (test_bit(PERI_CRIT, &card->wandev.critical)){
		
		printk(KERN_INFO "%s: Critical in if_send(): Peripheral running!\n",
				card->devname);
		
1355 1356
		dev_kfree_skb_any(skb);
		netif_start_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1357 1358 1359 1360 1361 1362 1363 1364
		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".
	*/
Linus Torvalds's avatar
Linus Torvalds committed
1365
	set_bit(SEND_TXIRQ_CRIT, (void*)&card->wandev.critical);
Linus Torvalds's avatar
Linus Torvalds committed
1366
        if(chan->transmit_length) {
1367
		netif_stop_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1368 1369
		chan->tick_counter = jiffies;
 		clear_bit(SEND_TXIRQ_CRIT, (void*)&card->wandev.critical);
Linus Torvalds's avatar
Linus Torvalds committed
1370 1371
		return 1;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
       	clear_bit(SEND_TXIRQ_CRIT, (void*)&card->wandev.critical);
 
	/* Move the if_header() code to here. By inserting frame
	 * relay header in if_header() we would break the
	 * tcpdump and other packet sniffers */
	chan->fr_header_len = setup_fr_header(&skb,dev,chan->common.usedby);
	if (chan->fr_header_len < 0 ){
		++chan->ifstats.tx_dropped;
		++card->wandev.stats.tx_dropped;
		
1382 1383
		dev_kfree_skb_any(skb);
		netif_start_queue(dev);	
Linus Torvalds's avatar
Linus Torvalds committed
1384 1385 1386
		return 0;
	}

Linus Torvalds's avatar
Linus Torvalds committed
1387 1388 1389 1390 1391
	sendpacket = skb->data;

	udp_type = udp_pkt_type(skb, card);

        if(udp_type != UDP_INVALID_TYPE) {
Linus Torvalds's avatar
Linus Torvalds committed
1392
		if(store_udp_mgmt_pkt(udp_type, UDP_PKT_FRM_STACK, card, skb,
Linus Torvalds's avatar
Linus Torvalds committed
1393 1394 1395 1396 1397 1398 1399
                        chan->dlci)) {
                        adptr_flags->imask |= FR_INTR_TIMER;
                        if (udp_type == UDP_FPIPE_TYPE){
                                chan->drvstats_if_send.
					if_send_PIPE_request ++;
			}
                }
1400
		netif_start_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1401 1402 1403 1404
		return 0;
	}

	//FIXME: can we do better than sendpacket[2]?
Linus Torvalds's avatar
Linus Torvalds committed
1405 1406
  	if ((chan->common.usedby == WANPIPE) && (sendpacket[2] == 0x45)) {
		
Linus Torvalds's avatar
Linus Torvalds committed
1407 1408
               	/* check to see if the source IP address is a broadcast or */
                /* multicast IP address */
Linus Torvalds's avatar
Linus Torvalds committed
1409 1410 1411
                if(chk_bcast_mcast_addr(card, dev, skb)){
            		++chan->ifstats.tx_dropped;
			++card->wandev.stats.tx_dropped;
1412 1413
                	dev_kfree_skb_any(skb);
			netif_start_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1414 1415
			return 0;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1416 1417
	}

Linus Torvalds's avatar
Linus Torvalds committed
1418 1419
	
	/* Lock the S514/S508 card: SMP Supported */
Linus Torvalds's avatar
Linus Torvalds committed
1420 1421
    	s508_s514_lock(card,&smp_flags);

Linus Torvalds's avatar
Linus Torvalds committed
1422 1423
	if (test_and_set_bit(SEND_CRIT, (void*)&card->wandev.critical)) {
		
Linus Torvalds's avatar
Linus Torvalds committed
1424 1425
		chan->drvstats_if_send.if_send_critical_non_ISR ++;
		chan->ifstats.tx_dropped ++;
Linus Torvalds's avatar
Linus Torvalds committed
1426 1427 1428
		printk(KERN_INFO "%s Critical in IF_SEND: if_send() already running!\n", 
				card->devname);
		goto if_send_start_and_exit;
Linus Torvalds's avatar
Linus Torvalds committed
1429
	}
Linus Torvalds's avatar
Linus Torvalds committed
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
	
	/* API packet check: minimum packet size must be greater than 
	 * 16 byte API header */
	if((chan->common.usedby == API) && (skb->len <= sizeof(api_tx_hdr_t))) {
		++chan->ifstats.tx_dropped;
		++card->wandev.stats.tx_dropped;
	    
		
		goto if_send_start_and_exit;

 	}else{
		/* During API transmission, get rid of the API header */
		if (chan->common.usedby == API) {
			api_tx_hdr_t* api_tx_hdr;
			api_tx_hdr = (api_tx_hdr_t*)&skb->data[0x00];
			attr = api_tx_hdr->attr;
			skb_pull(skb,sizeof(api_tx_hdr_t));
		}
	}

Linus Torvalds's avatar
Linus Torvalds committed
1450 1451 1452 1453 1454
	if (card->wandev.state != WAN_CONNECTED) {
		chan->drvstats_if_send.if_send_wan_disconnected ++;
		++chan->ifstats.tx_dropped;
        	++card->wandev.stats.tx_dropped;
	
Linus Torvalds's avatar
Linus Torvalds committed
1455
	} else if (chan->common.state != WAN_CONNECTED) {
Linus Torvalds's avatar
Linus Torvalds committed
1456
		chan->drvstats_if_send.if_send_dlci_disconnected ++;
Linus Torvalds's avatar
Linus Torvalds committed
1457 1458 1459 1460 1461

		/* Update the DLCI state in timer interrupt */
		card->u.f.timer_int_enabled |= TMR_INT_ENABLED_UPDATE_STATE;	
		adptr_flags->imask |= FR_INTR_TIMER;

Linus Torvalds's avatar
Linus Torvalds committed
1462 1463
        	++chan->ifstats.tx_dropped;
        	++card->wandev.stats.tx_dropped;
Linus Torvalds's avatar
Linus Torvalds committed
1464
		
Linus Torvalds's avatar
Linus Torvalds committed
1465
	} else if (!is_tx_ready(card, chan)) {
Linus Torvalds's avatar
Linus Torvalds committed
1466 1467 1468 1469
		/* No tx buffers available, store for delayed transmit */
		if (!setup_for_delayed_transmit(dev, skb)){
			set_bit(1,&delay_tx_queued);
		}
Linus Torvalds's avatar
Linus Torvalds committed
1470
		chan->drvstats_if_send.if_send_no_bfrs++;
Linus Torvalds's avatar
Linus Torvalds committed
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
		
	} else if (!skb->protocol) {
		/* No protocols drop packet */
		chan->drvstats_if_send.if_send_protocol_error ++;
		++card->wandev.stats.tx_errors;
	
	} else if (test_bit(ARP_CRIT,&card->wandev.critical)){
		/* We are trying to send an ARP Packet, block IP data until
		 * ARP is sent */
		++chan->ifstats.tx_dropped;
        	++card->wandev.stats.tx_dropped;
		
	} else {
		//FIXME: IPX is not implemented in this version of Frame Relay ?
		if((chan->common.usedby == WANPIPE) &&
		 	sendpacket[1] == 0x00 &&
Linus Torvalds's avatar
Linus Torvalds committed
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
		    	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);
			}
Linus Torvalds's avatar
Linus Torvalds committed
1500 1501 1502
			
		}else{
        		err = fr_send_data_header(card, chan->dlci, attr, skb->len, skb->data, chan->fr_header_len);
Linus Torvalds's avatar
Linus Torvalds committed
1503 1504 1505 1506
			if (err) {
				switch(err) {
				case FRRES_CIR_OVERFLOW:
				case FRRES_BUFFER_OVERFLOW:
Linus Torvalds's avatar
Linus Torvalds committed
1507 1508 1509
                			if (!setup_for_delayed_transmit(dev, skb)){
						set_bit(1,&delay_tx_queued);
					}
Linus Torvalds's avatar
Linus Torvalds committed
1510 1511
           				chan->drvstats_if_send.
						if_send_adptr_bfrs_full ++;
Linus Torvalds's avatar
Linus Torvalds committed
1512 1513 1514 1515 1516 1517 1518 1519 1520
					break;
					
				case FRRES_TOO_LONG:
					if (net_ratelimit()){
						printk(KERN_INFO 
						"%s: Error: Frame too long, transmission failed %i\n",
						 card->devname, (unsigned int)skb->len);
					}
					/* Drop down to default */
Linus Torvalds's avatar
Linus Torvalds committed
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
				default:
					chan->drvstats_if_send.
						if_send_dlci_disconnected ++;
        				++chan->ifstats.tx_dropped;
        				++card->wandev.stats.tx_dropped;
					break;
				}
			} else {
				chan->drvstats_if_send.
					if_send_bfr_passed_to_adptr++;
				++chan->ifstats.tx_packets;
				++card->wandev.stats.tx_packets;
Linus Torvalds's avatar
Linus Torvalds committed
1533 1534 1535 1536
				
                                chan->ifstats.tx_bytes += skb->len;
                                card->wandev.stats.tx_bytes += skb->len;
				dev->trans_start = jiffies;
Linus Torvalds's avatar
Linus Torvalds committed
1537 1538 1539 1540
			}
		}
	}

Linus Torvalds's avatar
Linus Torvalds committed
1541 1542
if_send_start_and_exit:

1543
	netif_start_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1544 1545 1546 1547 1548
	
	/* If we queued the packet for transmission, we must not
	 * deallocate it. The packet is unlinked from the IP stack
	 * not copied. Therefore, we must keep the original packet */
	if (!test_bit(1,&delay_tx_queued)) {
1549
                dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1550 1551 1552 1553
	}else{
		adptr_flags->imask |= FR_INTR_TXRDY;
		card->u.f.tx_interrupts_pending ++;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1554

Linus Torvalds's avatar
Linus Torvalds committed
1555
        clear_bit(SEND_CRIT, (void*)&card->wandev.critical);
Linus Torvalds's avatar
Linus Torvalds committed
1556 1557 1558

	s508_s514_unlock(card,&smp_flags);

Linus Torvalds's avatar
Linus Torvalds committed
1559
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1560 1561 1562 1563 1564
}



/*============================================================================
1565
 * Setup so that a frame can be transmitted on the occurrence of a transmit
Linus Torvalds's avatar
Linus Torvalds committed
1566 1567
 * interrupt.
 */
1568 1569
static int setup_for_delayed_transmit(struct net_device* dev,
				      struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
1570 1571 1572
{
        fr_channel_t* chan = dev->priv;
        sdla_t* card = chan->card;
Linus Torvalds's avatar
Linus Torvalds committed
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
        fr_dlci_interface_t* dlci_interface;
	int len = skb->len;

	/* Check that the dlci is properly configured,
         * before using tx interrupt */
	if (!chan->dlci_int_interface){
		if (net_ratelimit()){ 
			printk(KERN_INFO 
				"%s: ERROR on DLCI %i: Not configured properly !\n",
					card->devname, chan->dlci);
			printk(KERN_INFO "%s: Please contact Sangoma Technologies\n",
					card->devname);
		}
		return 1;
	}
		
	dlci_interface = chan->dlci_int_interface;
Linus Torvalds's avatar
Linus Torvalds committed
1590 1591 1592 1593

        if(chan->transmit_length) {
                printk(KERN_INFO "%s: Big mess in setup_for_del...\n",
				card->devname);
Linus Torvalds's avatar
Linus Torvalds committed
1594
                return 1;
Linus Torvalds's avatar
Linus Torvalds committed
1595 1596 1597 1598
        }

	if(len > FR_MAX_NO_DATA_BYTES_IN_FRAME) {
		//FIXME: increment some statistic */
Linus Torvalds's avatar
Linus Torvalds committed
1599
		return 1;
Linus Torvalds's avatar
Linus Torvalds committed
1600 1601
	}

Linus Torvalds's avatar
Linus Torvalds committed
1602 1603
	skb_unlink(skb);
	
Linus Torvalds's avatar
Linus Torvalds committed
1604
        chan->transmit_length = len;
Linus Torvalds's avatar
Linus Torvalds committed
1605 1606
	chan->delay_skb = skb;
        
Linus Torvalds's avatar
Linus Torvalds committed
1607 1608 1609
        dlci_interface->gen_interrupt |= FR_INTR_TXRDY;
        dlci_interface->packet_length = len;

Linus Torvalds's avatar
Linus Torvalds committed
1610 1611
	/* Turn on TX interrupt at the end of if_send */
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1612 1613 1614 1615 1616 1617 1618 1619 1620
}


/*============================================================================
 * 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.
 */

1621
static int chk_bcast_mcast_addr(sdla_t *card, struct net_device* dev,
Linus Torvalds's avatar
Linus Torvalds committed
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
                                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 */
Linus Torvalds's avatar
Linus Torvalds committed
1643 1644
        if((dev->flags & IFF_BROADCAST) && (src_ip_addr == broadcast_ip_addr)) {
                printk(KERN_INFO
Linus Torvalds's avatar
Linus Torvalds committed
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
                        "%s: Broadcast Source Address silently discarded\n",
                        card->devname);
                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);
                return 1;
        }

        return 0;
}

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

  
	fr_udp_pkt_t *fr_udp_pkt = (fr_udp_pkt_t *)data; 

	/* Set length of packet */
Linus Torvalds's avatar
Linus Torvalds committed
1676
	len = //sizeof(fr_encap_hdr_t)+
Linus Torvalds's avatar
Linus Torvalds committed
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
	      sizeof(ip_pkt_t)+ 
	      sizeof(udp_pkt_t)+
	      sizeof(wp_mgmt_t)+
	      sizeof(cblock_t)+
	      mbox_len;
 

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


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

	temp = (udp_length<<8)|(udp_length>>8);
	fr_udp_pkt->udp_pkt.udp_length = temp;
	 
	/* swap UDP ports */
	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;
	*((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 */
	fr_udp_pkt->udp_pkt.udp_checksum = 0;

	fr_udp_pkt->udp_pkt.udp_checksum = 
Linus Torvalds's avatar
Linus Torvalds committed
1724
		calc_checksum(&data[UDP_OFFSET/*+sizeof(fr_encap_hdr_t)*/],
Linus Torvalds's avatar
Linus Torvalds committed
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
			      udp_length+UDP_OFFSET);

	/* fill in IP length */
	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 */
	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 */
	fr_udp_pkt->ip_pkt.hdr_checksum = 0;
	fr_udp_pkt->ip_pkt.hdr_checksum = 
Linus Torvalds's avatar
Linus Torvalds committed
1742
		calc_checksum(&data[/*sizeof(fr_encap_hdr_t)*/0],
Linus Torvalds's avatar
Linus Torvalds committed
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
		      	      sizeof(ip_pkt_t));

	return len;
} /* reply_udp */

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

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

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

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

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

	return temp;	
}

/*
   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]);

	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;
		}
	} 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);
		}
	}


	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;
		}
	} 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);
		}
	}
} /* switch_net_numbers */

/*============================================================================
 * Get ethernet-style interface statistics.
Linus Torvalds's avatar
Linus Torvalds committed
1831
 * Return a pointer to struct enet_statistics.
Linus Torvalds's avatar
Linus Torvalds committed
1832
 */
1833
static struct net_device_stats *if_stats(struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
{
	fr_channel_t* chan = dev->priv;
	
	if(chan == NULL)
		return NULL;

	return &chan->ifstats;
}

/****** Interrupt Handlers **************************************************/

/*============================================================================
Linus Torvalds's avatar
Linus Torvalds committed
1846 1847 1848 1849 1850 1851
 * fr_isr:	S508 frame relay interrupt service routine.
 *
 * Description:
 *	Frame relay main interrupt service route. This
 *      function check the interrupt type and takes
 *      the appropriate action.
Linus Torvalds's avatar
Linus Torvalds committed
1852 1853 1854 1855 1856 1857 1858 1859 1860
 */
static void fr_isr (sdla_t* card)
{
	fr508_flags_t* flags = card->flags;
	char *ptr = &flags->iflag;
	int i,err;
	fr_mbox_t* mbox = card->mbox;

	/* This flag prevents nesting of interrupts.  See sdla_isr() routine
Linus Torvalds's avatar
Linus Torvalds committed
1861
         * in sdlamain.c.  */
Linus Torvalds's avatar
Linus Torvalds committed
1862 1863 1864 1865 1866
	card->in_isr = 1;
	
	++card->statistics.isr_entry;


Linus Torvalds's avatar
Linus Torvalds committed
1867 1868 1869 1870 1871 1872 1873
	/* All peripheral (configuraiton, re-configuration) events
	 * take presidence over the ISR.  Thus, retrigger */
	if (test_bit(PERI_CRIT, (void*)&card->wandev.critical)) {
		++card->statistics.isr_already_critical;
		goto fr_isr_exit;
	}
	
Linus Torvalds's avatar
Linus Torvalds committed
1874
        if(card->hw.type != SDLA_S514) {
Linus Torvalds's avatar
Linus Torvalds committed
1875 1876 1877
		if (test_bit(SEND_CRIT, (void*)&card->wandev.critical)) {
                        printk(KERN_INFO "%s: Critical while in ISR: If Send Running!\n",
                                card->devname);
Linus Torvalds's avatar
Linus Torvalds committed
1878
			++card->statistics.isr_already_critical;
Linus Torvalds's avatar
Linus Torvalds committed
1879
			goto fr_isr_exit;
Linus Torvalds's avatar
Linus Torvalds committed
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
		}
	}

	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;

Linus Torvalds's avatar
Linus Torvalds committed
1896
                case FR_INTR_READY:  	
Linus Torvalds's avatar
Linus Torvalds committed
1897 1898 1899 1900
	    		Intr_test_counter++;
			++card->statistics.isr_intr_test;
	    		break;	

1901
                case FR_INTR_DLC: /* Event interrupt occurred */
Linus Torvalds's avatar
Linus Torvalds committed
1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
			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 ++)
				printk(KERN_INFO "0x%02X ", *(ptr + 0x28 + i));
	   	 	printk(KERN_INFO "\n");	
            
			break;
    	}

Linus Torvalds's avatar
Linus Torvalds committed
1927 1928
fr_isr_exit:
	
Linus Torvalds's avatar
Linus Torvalds committed
1929 1930
	card->in_isr = 0;
	flags->iflag = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1931
	return;
Linus Torvalds's avatar
Linus Torvalds committed
1932 1933 1934 1935
}



Linus Torvalds's avatar
Linus Torvalds committed
1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
/*===========================================================
 * rx_intr	Receive interrupt handler.
 *
 * Description
 * 	Upon receiveing an interrupt: 
 *	1. Check that the firmware is in sync with 
 *     	   the driver. 
 *      2. Find an appropriate network interface
 *         based on the received dlci number.
 *	3. Check that the netowrk interface exists
 *         and that it's setup properly.
 *	4. Copy the data into an skb buffer.
 *	5. Check the packet type and take
 *         appropriate acton: UPD, API, ARP or Data.
Linus Torvalds's avatar
Linus Torvalds committed
1950
 */
Linus Torvalds's avatar
Linus Torvalds committed
1951

Linus Torvalds's avatar
Linus Torvalds committed
1952 1953 1954 1955 1956 1957 1958
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;
1959
	struct net_device* dev;
Linus Torvalds's avatar
Linus Torvalds committed
1960 1961 1962 1963
	void* buf;
	unsigned dlci, len, offs, len_incl_hdr;
	int i, udp_type;	

Linus Torvalds's avatar
Linus Torvalds committed
1964 1965

	/* Check that firmware buffers are in sync */
Linus Torvalds's avatar
Linus Torvalds committed
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
	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;
Linus Torvalds's avatar
Linus Torvalds committed
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987

		/* Bug Fix: Mar 6 2000
                 * If we get a corrupted mailbox, it means that driver 
                 * is out of sync with the firmware. There is no recovery.
                 * If we don't turn off all interrupts for this card
                 * the machine will crash. 
                 */
		printk(KERN_INFO "%s: Critical router failure ...!!!\n", card->devname);
		printk(KERN_INFO "Please contact Sangoma Technologies !\n");
		fr_set_intr_mode(card, 0, 0, 0);	
Linus Torvalds's avatar
Linus Torvalds committed
1988 1989 1990 1991 1992 1993 1994
		return;
	}

	len  = frbuf->length;
	dlci = frbuf->dlci;
	offs = frbuf->offset;

Linus Torvalds's avatar
Linus Torvalds committed
1995
	/* Find the network interface for this packet */
Linus Torvalds's avatar
Linus Torvalds committed
1996
	dev = find_channel(card, dlci);
Linus Torvalds's avatar
Linus Torvalds committed
1997
   
Linus Torvalds's avatar
Linus Torvalds committed
1998

Linus Torvalds's avatar
Linus Torvalds committed
1999 2000 2001 2002 2003
	/* Check that the network interface is active and
         * properly setup */
	if (dev == NULL) {
   		if( net_ratelimit()) { 
			printk(KERN_INFO "%s: received data on unconfigured DLCI %d!\n",
Linus Torvalds's avatar
Linus Torvalds committed
2004
                                                card->devname, dlci);
Linus Torvalds's avatar
Linus Torvalds committed
2005
		}
Linus Torvalds's avatar
Linus Torvalds committed
2006
		++card->statistics.rx_intr_on_orphaned_DLCI; 
Linus Torvalds's avatar
Linus Torvalds committed
2007 2008 2009
		++card->wandev.stats.rx_dropped;
		goto rx_done;
	}
Linus Torvalds's avatar
Linus Torvalds committed
2010

Linus Torvalds's avatar
Linus Torvalds committed
2011 2012 2013 2014 2015 2016 2017 2018 2019
	if ((chan = dev->priv) == NULL){
		if( net_ratelimit()) { 
			printk(KERN_INFO "%s: received data on unconfigured DLCI %d!\n",
                                                card->devname, dlci);
		}
		++card->statistics.rx_intr_on_orphaned_DLCI; 
		++card->wandev.stats.rx_dropped;
		goto rx_done;
	}
Linus Torvalds's avatar
Linus Torvalds committed
2020

Linus Torvalds's avatar
Linus Torvalds committed
2021
	skb = dev_alloc_skb(len); 
Linus Torvalds's avatar
Linus Torvalds committed
2022

2023
	if (!netif_running(dev) || (skb == NULL)){
Linus Torvalds's avatar
Linus Torvalds committed
2024

Linus Torvalds's avatar
Linus Torvalds committed
2025 2026 2027 2028
		++chan->ifstats.rx_dropped;
	
		if(skb == NULL) {
			if (net_ratelimit()) { 
Linus Torvalds's avatar
Linus Torvalds committed
2029
				printk(KERN_INFO 
Linus Torvalds's avatar
Linus Torvalds committed
2030 2031
					"%s: no socket buffers available!\n", 
						card->devname);
Linus Torvalds's avatar
Linus Torvalds committed
2032
			}
Linus Torvalds's avatar
Linus Torvalds committed
2033 2034
			chan->drvstats_rx_intr.rx_intr_no_socket ++;
		} 
Linus Torvalds's avatar
Linus Torvalds committed
2035

2036
		if (!netif_running(dev)){
Linus Torvalds's avatar
Linus Torvalds committed
2037 2038 2039
			chan->drvstats_rx_intr.
				rx_intr_dev_not_started ++;
			if (skb){
2040
				dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
2041 2042 2043 2044
			}
		}
		goto rx_done;
	}
Linus Torvalds's avatar
Linus Torvalds committed
2045

Linus Torvalds's avatar
Linus Torvalds committed
2046 2047 2048
	/* Copy data from the board into the socket buffer */
	if ((offs + len) > card->u.f.rx_top + 1) {
		unsigned tmp = card->u.f.rx_top - offs + 1;
Linus Torvalds's avatar
Linus Torvalds committed
2049

Linus Torvalds's avatar
Linus Torvalds committed
2050 2051 2052 2053 2054
		buf = skb_put(skb, tmp);
		sdla_peek(&card->hw, offs, buf, tmp);
		offs = card->u.f.rx_base;
		len -= tmp;
	}
Linus Torvalds's avatar
Linus Torvalds committed
2055

Linus Torvalds's avatar
Linus Torvalds committed
2056 2057
	buf = skb_put(skb, len);
	sdla_peek(&card->hw, offs, buf, len);
Linus Torvalds's avatar
Linus Torvalds committed
2058 2059


Linus Torvalds's avatar
Linus Torvalds committed
2060 2061
	/* We got the packet from the bard. 
         * Check the packet type and take appropriate action */
Linus Torvalds's avatar
Linus Torvalds committed
2062

Linus Torvalds's avatar
Linus Torvalds committed
2063
	udp_type = udp_pkt_type( skb, card );
Linus Torvalds's avatar
Linus Torvalds committed
2064

Linus Torvalds's avatar
Linus Torvalds committed
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
	if(udp_type != UDP_INVALID_TYPE) {

		/* UDP Debug packet received, store the
		 * packet and handle it in timer interrupt */

		skb_pull(skb, 1); 
		if (wanrouter_type_trans(skb, dev)){ 
			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;
Linus Torvalds's avatar
Linus Torvalds committed
2078
				}
Linus Torvalds's avatar
Linus Torvalds committed
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
			}
		}

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

		/* We are in API mode. 
                 * Add an API header to the RAW packet
                 * and queue it into a circular buffer.
                 * Then kick the fr_bh() bottom half handler */

		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(ETH_P_IP);
		skb->mac.raw  = skb->data;
		skb->dev      = dev;
		skb->pkt_type = WAN_PACKET_DATA;

		bh_enqueue(dev, skb);

		trigger_fr_bh(chan);

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

		//FIXME: Frame Relay IPX is not supported, Yet !
		//if (chan->enable_IPX) {
		//	fr_send(card, dlci, 0, skb->len,skb->data);
		//}
2117
		dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131

	} else if (is_arp(skb->data)) {

		/* ARP support enabled Mar 16 2000 
		 * Process incoming ARP reply/request, setup
		 * dynamic routes. */ 

		if (process_ARP((arphdr_1490_t *)skb->data, card, dev)) {
			if (net_ratelimit()){  
				printk (KERN_INFO 
				   "%s: Error processing ARP Packet.\n", 
					card->devname);
			}
		}
2132
		dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
2133 2134 2135 2136 2137 2138 2139

	} else if (skb->data[0] != 0x03) {

		if (net_ratelimit()) { 
			printk(KERN_INFO "%s: Non IETF packet discarded.\n", 
				card->devname);
		}
2140
		dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168

	} else {

		len_incl_hdr = skb->len;
		/* Decapsulate packet and pass it up the
		   protocol stack */
		skb->dev = dev;
		
		if (chan->common.usedby == BRIDGE || chan->common.usedby == BRIDGE_NODE){
		
			/* Make sure it's an Ethernet frame, otherwise drop it */
			if (!memcmp(skb->data, "\x03\x00\x80\x00\x80\xC2\x00\x07", 8)) {
				skb_pull(skb, 8);
				skb->protocol=eth_type_trans(skb,dev);
			}else{
				++chan->drvstats_rx_intr.rx_intr_bfr_not_passed_to_stack;
				++chan->ifstats.rx_errors;
				++card->wandev.stats.rx_errors;
				goto rx_done;
			}
		}else{
		
			/* remove hardware header */
			buf = skb_pull(skb, 1); 
			
			if (!wanrouter_type_trans(skb, dev)) {
				
				/* can't decapsulate packet */
2169
				dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180

				++chan->drvstats_rx_intr.rx_intr_bfr_not_passed_to_stack;
				++chan->ifstats.rx_errors;
				++card->wandev.stats.rx_errors;
				goto rx_done;	
			}
			skb->mac.raw = skb->data;
		} 
		

		/* Send a packed up the IP stack */
2181
		skb->dev->last_rx = jiffies;
Linus Torvalds's avatar
Linus Torvalds committed
2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
		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;
	}

rx_done:
Linus Torvalds's avatar
Linus Torvalds committed
2192 2193 2194 2195 2196 2197 2198 2199 2200

       	/* 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;

}

Linus Torvalds's avatar
Linus Torvalds committed
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
/*==================================================================
 * tx_intr:	Transmit interrupt handler.
 *
 * Rationale:
 *      If the board is busy transmitting, if_send() will
 *      buffers a single packet and turn on
 *      the tx interrupt. Tx interrupt will be called
 *      by the board, once the firmware can send more
 *      data. Thus, no polling is required.	 
 *
 * Description:
 *	Tx interrupt is called for each 
 *      configured dlci channel. Thus: 
 * 	1. Obtain the netowrk interface based on the
 *         dlci number.
 *      2. Check that network interface is up and
 *         properly setup.
 * 	3. Check for a buffered packed.
 *      4. Transmit the packed.
 *	5. If we are in WANPIPE mode, mark the 
 *         NET_BH handler. 
 *      6. If we are in API mode, kick
 *         the AF_WANPIPE socket for more data. 
 *	   
Linus Torvalds's avatar
Linus Torvalds committed
2225 2226 2227 2228 2229
 */
static void tx_intr(sdla_t *card)
{
        fr508_flags_t* flags = card->flags;
        fr_tx_buf_ctl_t* bctl;
2230
        struct net_device* dev;
Linus Torvalds's avatar
Linus Torvalds committed
2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241
        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);
Linus Torvalds's avatar
Linus Torvalds committed
2242 2243 2244 2245
	if (dev == NULL){
		printk(KERN_INFO "NO DEV IN TX Interrupt\n");	
		goto end_of_tx_intr;
	}
Linus Torvalds's avatar
Linus Torvalds committed
2246

Linus Torvalds's avatar
Linus Torvalds committed
2247 2248 2249 2250 2251 2252
        if ((chan = dev->priv) == NULL){
		printk(KERN_INFO "NO CHAN IN TX Interrupt\n");	
		goto end_of_tx_intr;
	}

        if(!chan->transmit_length || !chan->delay_skb) {
Linus Torvalds's avatar
Linus Torvalds committed
2253 2254
                printk(KERN_INFO "%s: tx int error - transmit length zero\n",
				card->wandev.name);
Linus Torvalds's avatar
Linus Torvalds committed
2255
                goto end_of_tx_intr;
Linus Torvalds's avatar
Linus Torvalds committed
2256 2257 2258 2259 2260 2261
        }

	/* 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. 
	*/
Linus Torvalds's avatar
Linus Torvalds committed
2262 2263
	if (test_bit(SEND_TXIRQ_CRIT, (void*)&card->wandev.critical)) {

Linus Torvalds's avatar
Linus Torvalds committed
2264 2265 2266
		fr_dlci_interface_t* dlci_interface = chan->dlci_int_interface;
		bctl->flag = 0xA0;
		dlci_interface->gen_interrupt |= FR_INTR_TXRDY;
Linus Torvalds's avatar
Linus Torvalds committed
2267
		return;
Linus Torvalds's avatar
Linus Torvalds committed
2268

Linus Torvalds's avatar
Linus Torvalds committed
2269
 	}else{
Linus Torvalds's avatar
Linus Torvalds committed
2270
        	bctl->dlci = flags->dlci;
Linus Torvalds's avatar
Linus Torvalds committed
2271 2272 2273 2274 2275
	        bctl->length = chan->transmit_length+chan->fr_header_len;
        	sdla_poke(&card->hw, 
		          fr_send_hdr(card,bctl->dlci,bctl->offset), 
			  chan->delay_skb->data,
 	              	  chan->delay_skb->len);
Linus Torvalds's avatar
Linus Torvalds committed
2276 2277 2278 2279 2280 2281
	        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;
Linus Torvalds's avatar
Linus Torvalds committed
2282 2283 2284 2285

		/* We must free an sk buffer, which we used
		 * for delayed transmission; Otherwise, the sock
		 * will run out of memory */
2286
                dev_kfree_skb_any(chan->delay_skb);
Linus Torvalds's avatar
Linus Torvalds committed
2287

Linus Torvalds's avatar
Linus Torvalds committed
2288 2289
		chan->delay_skb = NULL;				
        	chan->transmit_length = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2290

Linus Torvalds's avatar
Linus Torvalds committed
2291 2292
		dev->trans_start = jiffies;

2293
		if (netif_queue_stopped(dev)){
Linus Torvalds's avatar
Linus Torvalds committed
2294 2295
			/* If using API, than wakeup socket BH handler */
			if (chan->common.usedby == API){
2296
				netif_start_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
2297 2298
				wakeup_sk_bh(dev);
			}else{
2299
				netif_wake_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
2300 2301
			}
		}
Linus Torvalds's avatar
Linus Torvalds committed
2302
	}
Linus Torvalds's avatar
Linus Torvalds committed
2303 2304 2305 2306 2307 2308 2309 2310 2311

end_of_tx_intr:

 	/* 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;


Linus Torvalds's avatar
Linus Torvalds committed
2312 2313 2314 2315
}


/*============================================================================
Linus Torvalds's avatar
Linus Torvalds committed
2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331
 * timer_intr:	Timer interrupt handler.
 *
 * Rationale:
 *	All commans must be executed within the timer
 *      interrupt since no two commands should execute
 *      at the same time.
 *
 * Description:
 *	The timer interrupt is used to:
 *    	1. Processing udp calls from 'fpipemon'.
 *    	2. Processing update calls from /proc file system
 *   	3. Reading board-level statistics for 
 *         updating the proc file system.
 *    	4. Sending inverse ARP request packets.
 *	5. Configure a dlci/channel.
 *	6. Unconfigure a dlci/channel. (Node only)
Linus Torvalds's avatar
Linus Torvalds committed
2332
 */
Linus Torvalds's avatar
Linus Torvalds committed
2333

Linus Torvalds's avatar
Linus Torvalds committed
2334 2335 2336 2337
static void timer_intr(sdla_t *card)
{
	fr508_flags_t* flags = card->flags;

Linus Torvalds's avatar
Linus Torvalds committed
2338 2339
	/* UDP Debuging: fpipemon call */
        if (card->u.f.timer_int_enabled & TMR_INT_ENABLED_UDP) {
Linus Torvalds's avatar
Linus Torvalds committed
2340 2341 2342 2343 2344 2345 2346 2347
		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;
			}
		}
        }

Linus Torvalds's avatar
Linus Torvalds committed
2348 2349
	/* /proc update call : triggered from update() */
	if (card->u.f.timer_int_enabled & TMR_INT_ENABLED_UPDATE) {
Linus Torvalds's avatar
Linus Torvalds committed
2350 2351 2352 2353 2354 2355
		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;
	}

Linus Torvalds's avatar
Linus Torvalds committed
2356 2357 2358
	/* Update the channel state call.  This is call is
         * triggered by if_send() function */
	if (card->u.f.timer_int_enabled & TMR_INT_ENABLED_UPDATE_STATE){
2359
		struct net_device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
2360
		if (card->wandev.state == WAN_CONNECTED){
2361 2362
			for (dev = card->wandev.dev; dev;
			     dev = *((struct net_device **)dev->priv)){
Linus Torvalds's avatar
Linus Torvalds committed
2363 2364 2365 2366 2367 2368 2369 2370
				fr_channel_t *chan = dev->priv;	
				if (chan->common.state != WAN_CONNECTED){
					update_chan_state(dev);
				}
			}
		}
		card->u.f.timer_int_enabled &= ~TMR_INT_ENABLED_UPDATE_STATE;
	}
Linus Torvalds's avatar
Linus Torvalds committed
2371

Linus Torvalds's avatar
Linus Torvalds committed
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387
	/* configure a dlci/channel */
	if (card->u.f.timer_int_enabled & TMR_INT_ENABLED_CONFIG){
		config_fr(card);
		card->u.f.timer_int_enabled &= ~TMR_INT_ENABLED_CONFIG;
	}

	/* unconfigure a dlci/channel */
	if (card->u.f.timer_int_enabled & TMR_INT_ENABLED_UNCONFIG){
		unconfig_fr(card);
		card->u.f.timer_int_enabled &= ~TMR_INT_ENABLED_UNCONFIG;
	}

	
	/* Transmit ARP packets */
	if (card->u.f.timer_int_enabled & TMR_INT_ENABLED_ARP){
		int i=0;
2388
		struct net_device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
2389

Linus Torvalds's avatar
Linus Torvalds committed
2390 2391
		if (card->u.f.arp_dev == NULL)
			card->u.f.arp_dev = card->wandev.dev;
Linus Torvalds's avatar
Linus Torvalds committed
2392

Linus Torvalds's avatar
Linus Torvalds committed
2393
		dev = card->u.f.arp_dev;
Linus Torvalds's avatar
Linus Torvalds committed
2394

Linus Torvalds's avatar
Linus Torvalds committed
2395
		for (;;){ 
Linus Torvalds's avatar
Linus Torvalds committed
2396

Linus Torvalds's avatar
Linus Torvalds committed
2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424
			fr_channel_t *chan = dev->priv;

			/* If the interface is brought down cancel sending In-ARPs */
			if (!(dev->flags&IFF_UP)){
				clear_bit(0,&chan->inarp_ready);	
			}

			if (test_bit(0,&chan->inarp_ready)){

				if (check_tx_status(card,dev)){
					set_bit(ARP_CRIT,&card->wandev.critical);
					break;
				}

				if (!send_inarp_request(card,dev)){
					trigger_fr_arp(dev);
					chan->inarp_tick = jiffies;
				}

				clear_bit(0,&chan->inarp_ready);
				dev = move_dev_to_next(card,dev);
				break;
			}
			dev = move_dev_to_next(card,dev);

			if (++i == card->wandev.new_if_cnt){
				card->u.f.timer_int_enabled &= ~TMR_INT_ENABLED_ARP;
				break;
Linus Torvalds's avatar
Linus Torvalds committed
2425 2426
			}
		}
Linus Torvalds's avatar
Linus Torvalds committed
2427
		card->u.f.arp_dev = dev;
Linus Torvalds's avatar
Linus Torvalds committed
2428
	}
Linus Torvalds's avatar
Linus Torvalds committed
2429

Linus Torvalds's avatar
Linus Torvalds committed
2430 2431 2432 2433 2434 2435
        if(!card->u.f.timer_int_enabled)
                flags->imask &= ~FR_INTR_TIMER;
}


/*============================================================================
Linus Torvalds's avatar
Linus Torvalds committed
2436 2437 2438 2439 2440
 * spur_intr:	Spurious interrupt handler.
 * 
 * Description:
 *  	We don't know this interrupt.
 *      Print a warning.
Linus Torvalds's avatar
Linus Torvalds committed
2441
 */
Linus Torvalds's avatar
Linus Torvalds committed
2442

Linus Torvalds's avatar
Linus Torvalds committed
2443 2444
static void spur_intr (sdla_t* card)
{
Linus Torvalds's avatar
Linus Torvalds committed
2445 2446 2447
	if (net_ratelimit()){ 
		printk(KERN_INFO "%s: spurious interrupt!\n", card->devname);
	}
Linus Torvalds's avatar
Linus Torvalds committed
2448 2449
}

Linus Torvalds's avatar
Linus Torvalds committed
2450

Linus Torvalds's avatar
Linus Torvalds committed
2451 2452 2453 2454 2455 2456
//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
 */
Linus Torvalds's avatar
Linus Torvalds committed
2457 2458 2459
static int handle_IPXWAN(unsigned char *sendpacket, 
			 char *devname, unsigned char enable_IPX, 
			 unsigned long network_number)
Linus Torvalds's avatar
Linus Torvalds committed
2460 2461 2462
{
	int i;

Linus Torvalds's avatar
Linus Torvalds committed
2463 2464
	if( sendpacket[1] == 0x00 && sendpacket[2] == 0x80 &&
	    sendpacket[6] == 0x81 && sendpacket[7] == 0x37) { 
Linus Torvalds's avatar
Linus Torvalds committed
2465 2466

		/* It's an IPX packet */
Linus Torvalds's avatar
Linus Torvalds committed
2467
		if (!enable_IPX){
Linus Torvalds's avatar
Linus Torvalds committed
2468 2469
			/* Return 1 so we don't pass it up the stack. */
			//FIXME: Take this out when IPX is fixed
Linus Torvalds's avatar
Linus Torvalds committed
2470 2471
			if (net_ratelimit()){ 
				printk (KERN_INFO 
Linus Torvalds's avatar
Linus Torvalds committed
2472 2473
				"%s: WARNING: Unsupported IPX packet received and dropped\n",
					devname);
Linus Torvalds's avatar
Linus Torvalds committed
2474
			}
Linus Torvalds's avatar
Linus Torvalds committed
2475 2476 2477 2478 2479 2480 2481
			return 1;
		}
	} else {
		/* It's not IPX so return and pass it up the stack. */
		return 0;
	}

Linus Torvalds's avatar
Linus Torvalds committed
2482
	if( sendpacket[24] == 0x90 && sendpacket[25] == 0x04){
Linus Torvalds's avatar
Linus Torvalds committed
2483 2484
		/* It's IPXWAN */

Linus Torvalds's avatar
Linus Torvalds committed
2485 2486
		if( sendpacket[10] == 0x02 && sendpacket[42] == 0x00){

Linus Torvalds's avatar
Linus Torvalds committed
2487
			/* It's a timer request packet */
Linus Torvalds's avatar
Linus Torvalds committed
2488 2489
			printk(KERN_INFO "%s: Received IPXWAN Timer Request packet\n",
					devname);
Linus Torvalds's avatar
Linus Torvalds committed
2490 2491 2492 2493

			/* Go through the routing options and answer no to every
			 * option except Unnumbered RIP/SAP
			 */
Linus Torvalds's avatar
Linus Torvalds committed
2494
			for(i = 49; sendpacket[i] == 0x00; i += 5){
Linus Torvalds's avatar
Linus Torvalds committed
2495
				/* 0x02 is the option for Unnumbered RIP/SAP */
Linus Torvalds's avatar
Linus Torvalds committed
2496
				if( sendpacket[i + 4] != 0x02){
Linus Torvalds's avatar
Linus Torvalds committed
2497 2498 2499 2500 2501
					sendpacket[i + 1] = 0;
				}
			}

			/* Skip over the extended Node ID option */
Linus Torvalds's avatar
Linus Torvalds committed
2502
			if( sendpacket[i] == 0x04 ){
Linus Torvalds's avatar
Linus Torvalds committed
2503 2504 2505 2506 2507
				i += 8;
			}

			/* We also want to turn off all header compression opt.
			 */
Linus Torvalds's avatar
Linus Torvalds committed
2508
			for(; sendpacket[i] == 0x80 ;){
Linus Torvalds's avatar
Linus Torvalds committed
2509 2510 2511 2512 2513 2514 2515
				sendpacket[i + 1] = 0;
				i += (sendpacket[i + 2] << 8) + (sendpacket[i + 3]) + 4;
			}

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

Linus Torvalds's avatar
Linus Torvalds committed
2516 2517 2518 2519 2520
			printk(KERN_INFO "%s: Sending IPXWAN Timer Response\n",
					devname);

		} else if( sendpacket[42] == 0x02 ){

Linus Torvalds's avatar
Linus Torvalds committed
2521
			/* This is an information request packet */
Linus Torvalds's avatar
Linus Torvalds committed
2522 2523 2524
			printk(KERN_INFO 
				"%s: Received IPXWAN Information Request packet\n",
						devname);
Linus Torvalds's avatar
Linus Torvalds committed
2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548

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

			/* Set the router name */
			sendpacket[59] = 'F';
			sendpacket[60] = 'P';
			sendpacket[61] = 'I';
			sendpacket[62] = 'P';
			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[72] = CVHexToAscii(network_number & 0x0000000F);
			for(i = 73; i < 107; i+= 1)
			{
				sendpacket[i] = 0;
			}

Linus Torvalds's avatar
Linus Torvalds committed
2549 2550 2551 2552
			printk(KERN_INFO "%s: Sending IPXWAN Information Response packet\n",
					devname);
		} else {

Linus Torvalds's avatar
Linus Torvalds committed
2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
			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);

		return 1;
	}

2566
	/* If we get here, it's an IPX-data packet so it'll get passed up the 
Linus Torvalds's avatar
Linus Torvalds committed
2567 2568 2569 2570 2571 2572 2573
	 * stack.
	 * switch the network numbers 
	 */
	switch_net_numbers(sendpacket, network_number ,1);
	return 0;
}
/*============================================================================
Linus Torvalds's avatar
Linus Torvalds committed
2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590
 * process_route
 * 
 * Rationale:
 *	If the interface goes down, or we receive an ARP request,
 *      we have to change the network interface ip addresses.
 * 	This cannot be done within the interrupt.
 *
 * Description:
 *
 * 	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.
 *
 * Usage:
 *	This function is called by fr_poll() polling funtion.
 */
Linus Torvalds's avatar
Linus Torvalds committed
2591

2592
static void process_route(struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
2593
{
Linus Torvalds's avatar
Linus Torvalds committed
2594 2595 2596 2597 2598 2599 2600 2601 2602
	fr_channel_t *chan = dev->priv;
	sdla_t *card = chan->card;

	struct ifreq if_info;
	struct sockaddr_in *if_data;
	mm_segment_t fs = get_fs();
	u32 ip_tmp;
	int err;

Linus Torvalds's avatar
Linus Torvalds committed
2603

Linus Torvalds's avatar
Linus Torvalds committed
2604
	switch(chan->route_flag){
Linus Torvalds's avatar
Linus Torvalds committed
2605

Linus Torvalds's avatar
Linus Torvalds committed
2606 2607 2608 2609 2610
	case ADD_ROUTE:
				
		/* Set remote addresses */
		memset(&if_info, 0, sizeof(if_info));
		strcpy(if_info.ifr_name, dev->name);
Linus Torvalds's avatar
Linus Torvalds committed
2611

Linus Torvalds's avatar
Linus Torvalds committed
2612
		set_fs(get_ds());     /* get user space block */ 
Linus Torvalds's avatar
Linus Torvalds committed
2613
		
Linus Torvalds's avatar
Linus Torvalds committed
2614 2615 2616 2617
		if_data = (struct sockaddr_in *)&if_info.ifr_dstaddr;
		if_data->sin_addr.s_addr = chan->ip_remote;
		if_data->sin_family = AF_INET;
		err = devinet_ioctl( SIOCSIFDSTADDR, &if_info );
Linus Torvalds's avatar
Linus Torvalds committed
2618

Linus Torvalds's avatar
Linus Torvalds committed
2619 2620 2621 2622 2623 2624
		set_fs(fs);           /* restore old block */

		if (err) {
			printk(KERN_INFO 
				"%s: Route Add failed.  Error: %d\n", 
					card->devname,err);
2625 2626
			printk(KERN_INFO "%s: Address: %u.%u.%u.%u\n",
				chan->name, NIPQUAD(chan->ip_remote));
Linus Torvalds's avatar
Linus Torvalds committed
2627 2628

		}else {
2629
			printk(KERN_INFO "%s: Route Added Successfully: %u.%u.%u.%u\n",
2630
				card->devname,NIPQUAD(chan->ip_remote));
Linus Torvalds's avatar
Linus Torvalds committed
2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655
			chan->route_flag = ROUTE_ADDED;
		}
		break;

	case REMOVE_ROUTE:

		/* Set remote addresses */
		memset(&if_info, 0, sizeof(if_info));
		strcpy(if_info.ifr_name, dev->name);

		ip_tmp = get_ip_address(dev,WAN_POINTOPOINT_IP);	

		set_fs(get_ds());     /* get user space block */ 
		
		if_data = (struct sockaddr_in *)&if_info.ifr_dstaddr;
		if_data->sin_addr.s_addr = 0;
		if_data->sin_family = AF_INET;
		err = devinet_ioctl( SIOCSIFDSTADDR, &if_info );

		set_fs(fs);    
		
		if (err) {
			printk(KERN_INFO 
				"%s: Deleting of route failed.  Error: %d\n", 
					card->devname,err);
2656 2657
			printk(KERN_INFO "%s: Address: %u.%u.%u.%u\n",
				dev->name,NIPQUAD(chan->ip_remote) );
Linus Torvalds's avatar
Linus Torvalds committed
2658 2659

		} else {
2660 2661
			printk(KERN_INFO "%s: Route Removed Sucessfuly: %u.%u.%u.%u\n", 
				card->devname,NIPQUAD(ip_tmp));
Linus Torvalds's avatar
Linus Torvalds committed
2662 2663 2664 2665 2666 2667
			chan->route_flag = NO_ROUTE;
		}
		break;

	} /* Case Statement */

Linus Torvalds's avatar
Linus Torvalds committed
2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723
}



/****** 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)
{
	fr_mbox_t* mbox = card->mbox;
	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		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) {
		int len = mbox->cmd.length;
		memcpy(str, mbox->data, len);
	        str[len] = '\0';
	}
	return err;
}

/*============================================================================
 * Set global configuration.
 */
static int fr_configure (sdla_t* card, fr_conf_t *conf)
{
	fr_mbox_t* mbox = card->mbox;
	int retry = MAX_CMD_RETRY;
	int dlci_num = card->u.f.dlci_num;
	int err, i;

	do
	{
		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]; 
		
		mbox->cmd.command = FR_SET_CONFIG;
		mbox->cmd.length =
			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));
Linus Torvalds's avatar
Linus Torvalds committed
2724 2725 2726 2727 2728 2729

	/*NC Oct 12 2000 */
	if (err != CMD_OK){
		printk(KERN_ERR "%s: Frame Relay Configuration Failed: rc=0x%x\n",
				card->devname,err);
	}
Linus Torvalds's avatar
Linus Torvalds committed
2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803
	
	return err;
}

/*============================================================================
 * Set DLCI configuration.
 */
static int fr_dlci_configure (sdla_t* card, fr_dlc_conf_t *conf, unsigned dlci)
{
	fr_mbox_t* mbox = card->mbox;
	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		memcpy(mbox->data, conf, sizeof(fr_dlc_conf_t));
		mbox->cmd.dlci = (unsigned short) dlci; 
		mbox->cmd.command = FR_SET_CONFIG;
		mbox->cmd.length = sizeof(fr_dlc_conf_t);
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	} while (err && retry--);
	
	return err;
}
/*============================================================================
 * Set interrupt mode.
 */
static int fr_set_intr_mode (sdla_t* card, unsigned mode, unsigned mtu,
	unsigned short timeout)
{
	fr_mbox_t* mbox = card->mbox;
	fr508_intr_ctl_t* ictl = (void*)mbox->data;
	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		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));
	
	return err;
}

/*============================================================================
 * Enable communications.
 */
static int fr_comm_enable (sdla_t* card)
{
	fr_mbox_t* mbox = card->mbox;
	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		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));
	
	return err;
}

/*============================================================================
Linus Torvalds's avatar
Linus Torvalds committed
2804 2805 2806 2807 2808
 * fr_comm_disable 
 *
 * Warning: This functin is called by the shutdown() procedure. It is void
 *          since dev->priv are has already been deallocated and no
 *          error checking is possible using fr_event() function.
Linus Torvalds's avatar
Linus Torvalds committed
2809
 */
Linus Torvalds's avatar
Linus Torvalds committed
2810
static void fr_comm_disable (sdla_t* card)
Linus Torvalds's avatar
Linus Torvalds committed
2811 2812 2813 2814 2815 2816 2817 2818 2819 2820
{
	fr_mbox_t* mbox = card->mbox;
	int retry = MAX_CMD_RETRY;
	int err;

	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;
Linus Torvalds's avatar
Linus Torvalds committed
2821
	} while (err && retry--);
Linus Torvalds's avatar
Linus Torvalds committed
2822
	
Linus Torvalds's avatar
Linus Torvalds committed
2823 2824 2825 2826 2827 2828 2829 2830 2831 2832
	retry = MAX_CMD_RETRY;
	
	do
	{
		mbox->cmd.command = FR_COMM_DISABLE;
		mbox->cmd.length = 0;
		err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	} while (err && retry--);

	return;
Linus Torvalds's avatar
Linus Torvalds committed
2833 2834
}

Linus Torvalds's avatar
Linus Torvalds committed
2835 2836


Linus Torvalds's avatar
Linus Torvalds committed
2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989
/*============================================================================
 * Get communications error statistics. 
 */
static int fr_get_err_stats (sdla_t* card)
{
	fr_mbox_t* mbox = card->mbox;
	int retry = MAX_CMD_RETRY;
	int err;


	do
	{
		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;
		card->wandev.stats.tx_aborted_errors = stats->tx_aborts;
	
	}

	return err;
}

/*============================================================================
 * Get statistics. 
 */
static int fr_get_stats (sdla_t* card)
{
	fr_mbox_t* mbox = card->mbox;
	int retry = MAX_CMD_RETRY;
	int err;


	do
	{
		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;
		card->wandev.stats.rx_frame_errors = stats->rx_bad_format;
		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)
{
	fr_mbox_t* mbox = card->mbox;
	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		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));
	
	return err;
}

/*============================================================================
 * Activate DLCI(s) (Access Node only!). 
 * This routine will perform the ACTIVATE_DLCIs command with a DLCI number. 
 */
static int fr_activate_dlci (sdla_t* card, int dlci)
{
	fr_mbox_t* mbox = card->mbox;
	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		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));
	
	return err;
}

/*============================================================================
 * Delete DLCI(s) (Access Node only!). 
 * This routine will perform the DELETE_DLCIs command with a DLCI number. 
 */
static int fr_delete_dlci (sdla_t* card, int dlci)
{
	fr_mbox_t* mbox = card->mbox;
	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		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));
	
	return err;
}



/*============================================================================
 * Issue in-channel signalling frame. 
 */
static int fr_issue_isf (sdla_t* card, int isf)
{
	fr_mbox_t* mbox = card->mbox;
	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		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));
	
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
2990 2991 2992

static unsigned int fr_send_hdr (sdla_t*card, int dlci, unsigned int offset)
{
2993
	struct net_device *dev = find_channel(card,dlci);	
Linus Torvalds's avatar
Linus Torvalds committed
2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005
	fr_channel_t *chan;

	if (!dev || !(chan=dev->priv))
		return offset;
	
	if (chan->fr_header_len){
		sdla_poke(&card->hw, offset, chan->fr_header, chan->fr_header_len);
	}
	
	return offset+chan->fr_header_len;
}

Linus Torvalds's avatar
Linus Torvalds committed
3006 3007 3008
/*============================================================================
 * Send a frame on a selected DLCI.  
 */
Linus Torvalds's avatar
Linus Torvalds committed
3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041
static int fr_send_data_header (sdla_t* card, int dlci, unsigned char attr, int len,
	void *buf, unsigned char hdr_len)
{
	fr_mbox_t* mbox = card->mbox + 0x800;
	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		mbox->cmd.dlci    = dlci;
		mbox->cmd.attr    = attr;
		mbox->cmd.length  = len+hdr_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_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, fr_send_hdr(card,dlci,frbuf->offset), buf, len);
		frbuf->flag = 0x01;
	}

	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074
static int fr_send (sdla_t* card, int dlci, unsigned char attr, int len,
	void *buf)
{
	fr_mbox_t* mbox = card->mbox + 0x800;
	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		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_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;
}

Linus Torvalds's avatar
Linus Torvalds committed
3075

Linus Torvalds's avatar
Linus Torvalds committed
3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095
/****** Firmware Asynchronous Event Handlers ********************************/

/*============================================================================
 * 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)
{
	fr508_flags_t* flags = card->flags;
	char *ptr = &flags->iflag;
	int i;

	switch (event) {

		case FRRES_MODEM_FAILURE:
			return fr_modem_failure(card, mbox);

3096 3097
		case FRRES_CHANNEL_DOWN: {
			struct net_device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
3098 3099

			/* Remove all routes from associated DLCI's */
3100 3101
			for (dev = card->wandev.dev; dev;
			     dev = *((struct net_device **)dev->priv)) {
Linus Torvalds's avatar
Linus Torvalds committed
3102 3103 3104 3105 3106 3107 3108 3109 3110
				fr_channel_t *chan = dev->priv;
				if (chan->route_flag == ROUTE_ADDED) {
					chan->route_flag = REMOVE_ROUTE;
				}

				if (chan->inarp == INARP_CONFIGURED) {
					chan->inarp = INARP_REQUEST;
				}

Linus Torvalds's avatar
Linus Torvalds committed
3111 3112 3113 3114 3115
				/* If the link becomes disconnected then,
                                 * all channels will be disconnected
                                 * as well.
                                 */
				set_chan_state(dev,WAN_DISCONNECTED);
Linus Torvalds's avatar
Linus Torvalds committed
3116
			}
Linus Torvalds's avatar
Linus Torvalds committed
3117
				
Linus Torvalds's avatar
Linus Torvalds committed
3118 3119 3120 3121
			wanpipe_set_state(card, WAN_DISCONNECTED);
			return 1;
			}

3122 3123
		case FRRES_CHANNEL_UP: {
			struct net_device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
3124

Linus Torvalds's avatar
Linus Torvalds committed
3125 3126
			/* FIXME: Only startup devices that are on the list */
			
3127 3128
			for (dev = card->wandev.dev; dev;
			     dev = *((struct net_device **)dev->priv)) {
Linus Torvalds's avatar
Linus Torvalds committed
3129 3130
				
				set_chan_state(dev,WAN_CONNECTED);
Linus Torvalds's avatar
Linus Torvalds committed
3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159
			}

			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);
			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 + 0x18 + i));
	   	 	printk(KERN_INFO "\n");	
            
			break;

		case FRRES_DLCI_INACTIVE:
			break;
 
		case FRRES_CIR_OVERFLOW:
			break;
Linus Torvalds's avatar
Linus Torvalds committed
3160
			
Linus Torvalds's avatar
Linus Torvalds committed
3161 3162
		case FRRES_BUFFER_OVERFLOW:
			break; 
Linus Torvalds's avatar
Linus Torvalds committed
3163
			
Linus Torvalds's avatar
Linus Torvalds committed
3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201
		default:
			printk(KERN_INFO "%s: command 0x%02X returned 0x%02X!\n"
				, card->devname, mbox->cmd.command, event);
	}

	return 0;
}

/*============================================================================
 * Handle modem error.
 *
 * Return zero if previous command has to be cancelled.
 */
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){
		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)
{
	dlci_status_t* status = (void*)mbox->data;
	int cnt = mbox->cmd.length / sizeof(dlci_status_t);
	fr_channel_t *chan;
3202
	struct net_device* dev2;
Linus Torvalds's avatar
Linus Torvalds committed
3203 3204 3205 3206 3207
	

	for (; cnt; --cnt, ++status) {

		unsigned short dlci= status->dlci;
3208
		struct net_device* dev = find_channel(card, dlci);
Linus Torvalds's avatar
Linus Torvalds committed
3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224
		
		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 is inactive!\n",
					card->devname, dlci);

3225
				if (dev && netif_running(dev))
Linus Torvalds's avatar
Linus Torvalds committed
3226 3227 3228 3229 3230 3231 3232 3233 3234
					set_chan_state(dev, WAN_DISCONNECTED);
			}
	
			if (status->state & FR_DLCI_DELETED) {

				printk(KERN_INFO
					"%s: DLCI %u has been deleted!\n",
					card->devname, dlci);

3235
				if (dev && netif_running(dev)){
Linus Torvalds's avatar
Linus Torvalds committed
3236

Linus Torvalds's avatar
Linus Torvalds committed
3237 3238 3239 3240
					fr_channel_t *chan = dev->priv;

					if (chan->route_flag == ROUTE_ADDED) {
						chan->route_flag = REMOVE_ROUTE;
Linus Torvalds's avatar
Linus Torvalds committed
3241 3242
						/* The state change will trigger
                                                 * the fr polling routine */
Linus Torvalds's avatar
Linus Torvalds committed
3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259
					}

					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;
Linus Torvalds's avatar
Linus Torvalds committed
3260 3261
	
				set_chan_state(dev, WAN_CONNECTED);
Linus Torvalds's avatar
Linus Torvalds committed
3262 3263 3264 3265 3266
		
			}
		}
	}
	
3267 3268
	for (dev2 = card->wandev.dev; dev2;
	     dev2 = *((struct net_device **)dev2->priv)){
Linus Torvalds's avatar
Linus Torvalds committed
3269
		
Linus Torvalds's avatar
Linus Torvalds committed
3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285
		chan = dev2->priv;
	
		if (chan->dlci_configured == DLCI_CONFIG_PENDING) {
			if (fr_init_dlci(card, chan)){
				return 1;
			}
		}

	}
	return 1;
}


static int fr_init_dlci (sdla_t *card, fr_channel_t *chan)
{
	fr_dlc_conf_t cfg;
Linus Torvalds's avatar
Linus Torvalds committed
3286
	
Linus Torvalds's avatar
Linus Torvalds committed
3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303
	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)){
Linus Torvalds's avatar
Linus Torvalds committed
3304 3305 3306
		printk(KERN_INFO 
			"%s: DLCI Configure failed for %d\n",
				card->devname, chan->dlci);
Linus Torvalds's avatar
Linus Torvalds committed
3307 3308 3309 3310 3311 3312
		return 1;	
	}
	
	chan->dlci_configured = DLCI_CONFIGURED;

	/* Read the interface byte mapping into the channel 
Linus Torvalds's avatar
Linus Torvalds committed
3313
	 * structure.
Linus Torvalds's avatar
Linus Torvalds committed
3314 3315 3316 3317 3318 3319 3320 3321 3322 3323
	 */
	read_DLCI_IB_mapping( card, chan );

	return 0;
}
/******* Miscellaneous ******************************************************/

/*============================================================================
 * Update channel state. 
 */
3324
static int update_chan_state(struct net_device* dev)
Linus Torvalds's avatar
Linus Torvalds committed
3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339
{
	fr_channel_t* chan = dev->priv;
	sdla_t* card = chan->card;
	fr_mbox_t* mbox = card->mbox;
	int retry = MAX_CMD_RETRY;
	int err;

	do
	{
		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));

	if (!err) {
Linus Torvalds's avatar
Linus Torvalds committed
3340
		
Linus Torvalds's avatar
Linus Torvalds committed
3341 3342
		unsigned short* list = (void*)mbox->data;
		int cnt = mbox->cmd.length / sizeof(short);
Linus Torvalds's avatar
Linus Torvalds committed
3343 3344 3345
		
		err=1;
		
Linus Torvalds's avatar
Linus Torvalds committed
3346 3347 3348 3349
		for (; cnt; --cnt, ++list) {

			if (*list == chan->dlci) {
 				set_chan_state(dev, WAN_CONNECTED);
Linus Torvalds's avatar
Linus Torvalds committed
3350 3351 3352 3353 3354 3355 3356 3357 3358


				/* May 23 2000. NC
				 * When a dlci is added or restarted,
                                 * the dlci_int_interface pointer must
				 * be reinitialized.  */
				if (!chan->dlci_int_interface){
					err=fr_init_dlci (card,chan);
				}
Linus Torvalds's avatar
Linus Torvalds committed
3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369
				break;
			}
		}
	}

	return err;
}

/*============================================================================
 * Set channel state.
 */
3370
static void set_chan_state(struct net_device* dev, int state)
Linus Torvalds's avatar
Linus Torvalds committed
3371 3372 3373 3374
{
	fr_channel_t* chan = dev->priv;
	sdla_t* card = chan->card;

Linus Torvalds's avatar
Linus Torvalds committed
3375
	if (chan->common.state != state) {
Linus Torvalds's avatar
Linus Torvalds committed
3376 3377 3378 3379 3380 3381 3382

		switch (state) {

			case WAN_CONNECTED:
				printk(KERN_INFO
					"%s: Interface %s: DLCI %d connected\n",
					card->devname, dev->name, chan->dlci);
Linus Torvalds's avatar
Linus Torvalds committed
3383 3384 3385 3386 3387 3388

				/* If the interface was previoulsy down,
                                 * bring it up, since the channel is active */

				trigger_fr_poll (dev);
				trigger_fr_arp  (dev);
Linus Torvalds's avatar
Linus Torvalds committed
3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400
				break;

			case WAN_CONNECTING:
				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: DLCI %d disconnected!\n",
					card->devname, dev->name, chan->dlci);
Linus Torvalds's avatar
Linus Torvalds committed
3401 3402 3403 3404
			
				/* If the interface is up, bring it down,
                                 * since the channel is now disconnected */
				trigger_fr_poll (dev);
Linus Torvalds's avatar
Linus Torvalds committed
3405 3406 3407
				break;
		}

Linus Torvalds's avatar
Linus Torvalds committed
3408
		chan->common.state = state;
Linus Torvalds's avatar
Linus Torvalds committed
3409 3410 3411 3412 3413 3414 3415
	}

	chan->state_tick = jiffies;
}

/*============================================================================
 * Find network device by its channel number.
Linus Torvalds's avatar
Linus Torvalds committed
3416 3417 3418 3419 3420 3421
 *
 * We need this critical flag because we change
 * the dlci_to_dev_map outside the interrupt.
 *
 * NOTE: del_if() functions updates this array, it uses
 *       the spin locks to avoid corruption.
Linus Torvalds's avatar
Linus Torvalds committed
3422
 */
3423
static struct net_device* find_channel(sdla_t* card, unsigned dlci)
Linus Torvalds's avatar
Linus Torvalds committed
3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478
{
	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.
 *
 * Return:	1 - Tx buffer(s) available
 *		0 - no buffers available
 */
static int is_tx_ready (sdla_t* card, fr_channel_t* chan)
{
	unsigned char sb;

        if(card->hw.type == SDLA_S514)
		return 1;

	sb = inb(card->hw.port);
	if (sb & 0x02) 
		return 1;

	return 0;
}

/*============================================================================
 * 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)
{
	unsigned val;

	if (!len) 
		len = strlen(str);

	for (val = 0; len && is_digit(*str); ++str, --len)
		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;
Linus Torvalds's avatar
Linus Torvalds committed
3479
	
3480
	struct net_device *dev = find_channel(card, dlci);
Linus Torvalds's avatar
Linus Torvalds committed
3481 3482 3483 3484 3485 3486 3487
	fr_channel_t *chan;
	
	if (!dev || !(chan=dev->priv))
		return 1;
	
        if(!card->u.f.udp_pkt_lgth && (skb->len <= MAX_LGTH_UDP_MGNT_PKT)){
                card->u.f.udp_pkt_lgth = skb->len + chan->fr_header_len;
Linus Torvalds's avatar
Linus Torvalds committed
3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499
                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);
	}

Linus Torvalds's avatar
Linus Torvalds committed
3500
        if(udp_pkt_src == UDP_PKT_FRM_STACK){
3501
                dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
3502
	}else{
3503
                dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
3504 3505
	}
		
Linus Torvalds's avatar
Linus Torvalds committed
3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525
        return(udp_pkt_stored);
}


/*==============================================================================
 * Process UDP call of type FPIPE8ND
 */
static int process_udp_mgmt_pkt(sdla_t* card)
{

	int c_retry = MAX_CMD_RETRY;
	unsigned char *buf;
	unsigned char frames;
	unsigned int len;
	unsigned short buffer_length;
	struct sk_buff *new_skb;
	fr_mbox_t* mbox = card->mbox;
	int err;
	struct timeval tv;
	int udp_mgmt_req_valid = 1;
3526
        struct net_device* dev;
Linus Torvalds's avatar
Linus Torvalds committed
3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538
        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);
Linus Torvalds's avatar
Linus Torvalds committed
3539 3540 3541 3542 3543 3544 3545 3546
	if (!dev){
		card->u.f.udp_pkt_lgth = 0;
		return 1;
	}
        if ((chan = dev->priv) == NULL){
		card->u.f.udp_pkt_lgth = 0;
		return 1;
	}
Linus Torvalds's avatar
Linus Torvalds committed
3547 3548 3549 3550 3551 3552 3553

	/* 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) {
Linus Torvalds's avatar
Linus Torvalds committed
3554 3555 3556
		if (check_tx_status(card,dev)){
			card->u.f.udp_pkt_lgth = 0;
			return 1;
Linus Torvalds's avatar
Linus Torvalds committed
3557 3558 3559 3560 3561
		}
        }

        fr_udp_pkt = (fr_udp_pkt_t *)card->u.f.udp_pkt_data;

Linus Torvalds's avatar
Linus Torvalds committed
3562 3563 3564
	if(udp_pkt_src == UDP_PKT_FRM_NETWORK) {
	
		switch(fr_udp_pkt->cblock.command) {
Linus Torvalds's avatar
Linus Torvalds committed
3565

Linus Torvalds's avatar
Linus Torvalds committed
3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577
			case FR_READ_MODEM_STATUS:
			case FR_READ_STATUS:
			case FPIPE_ROUTER_UP_TIME:
			case FR_READ_ERROR_STATS:
			case FPIPE_DRIVER_STAT_GEN:
			case FR_READ_STATISTICS:
			case FR_READ_ADD_DLC_STATS:
			case FR_READ_CONFIG:
			case FR_READ_CODE_VERSION:
				udp_mgmt_req_valid = 1;
				break;
			default:
Linus Torvalds's avatar
Linus Torvalds committed
3578 3579
				udp_mgmt_req_valid = 0;
				break;
Linus Torvalds's avatar
Linus Torvalds committed
3580 3581
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
3582 3583 3584 3585 3586 3587

	if(!udp_mgmt_req_valid) {
		/* set length to 0 */
		fr_udp_pkt->cblock.length = 0;
		/* set return code */
		fr_udp_pkt->cblock.result = 0xCD; 
Linus Torvalds's avatar
Linus Torvalds committed
3588 3589 3590 3591 3592 3593 3594 3595 3596
		
		chan->drvstats_gen.UDP_PIPE_mgmt_direction_err ++;

		if (net_ratelimit()){	
			printk(KERN_INFO 
			"%s: Warning, Illegal UDP command attempted from network: %x\n",
			card->devname,fr_udp_pkt->cblock.command);
		}
		
Linus Torvalds's avatar
Linus Torvalds committed
3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634
	} 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;
				}

				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 -
Linus Torvalds's avatar
Linus Torvalds committed
3635
					//sizeof(fr_encap_hdr_t) -
Linus Torvalds's avatar
Linus Torvalds committed
3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764
					sizeof(ip_pkt_t) -
					sizeof(udp_pkt_t) -
					sizeof(wp_mgmt_t) -
					sizeof(cblock_t));

				/* set return code */
				fr_udp_pkt->cblock.result = 0;
			
			} 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;
				}
                                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);
Linus Torvalds's avatar
Linus Torvalds committed
3765
			fr_udp_pkt->cblock.length = mbox->cmd.length = 2;
Linus Torvalds's avatar
Linus Torvalds committed
3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780
			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;	
Linus Torvalds's avatar
Linus Torvalds committed
3781 3782
			mbox->cmd.length = fr_udp_pkt->cblock.length = 4;
			fr_udp_pkt->cblock.result = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3783 3784 3785 3786 3787 3788
			break;

		case FPIPE_DRIVER_STAT_IFSEND:
			memcpy(fr_udp_pkt->data,
				&chan->drvstats_if_send.if_send_entry,
				sizeof(if_send_stat_t));
Linus Torvalds's avatar
Linus Torvalds committed
3789 3790
			mbox->cmd.length = fr_udp_pkt->cblock.length =sizeof(if_send_stat_t);	
			fr_udp_pkt->cblock.result = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3791 3792 3793
			break;
	
		case FPIPE_DRIVER_STAT_INTR:
Linus Torvalds's avatar
Linus Torvalds committed
3794

Linus Torvalds's avatar
Linus Torvalds committed
3795 3796 3797
			memcpy(fr_udp_pkt->data,
                                &card->statistics.isr_entry,
                                sizeof(global_stats_t));
Linus Torvalds's avatar
Linus Torvalds committed
3798

Linus Torvalds's avatar
Linus Torvalds committed
3799 3800 3801
                        memcpy(&fr_udp_pkt->data[sizeof(global_stats_t)],
                                &chan->drvstats_rx_intr.rx_intr_no_socket,
                                sizeof(rx_intr_stat_t));
Linus Torvalds's avatar
Linus Torvalds committed
3802 3803 3804

			mbox->cmd.length = fr_udp_pkt->cblock.length = 
					sizeof(global_stats_t) +
Linus Torvalds's avatar
Linus Torvalds committed
3805
					sizeof(rx_intr_stat_t);
Linus Torvalds's avatar
Linus Torvalds committed
3806
			fr_udp_pkt->cblock.result = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3807 3808 3809 3810 3811 3812 3813 3814 3815 3816
			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));

Linus Torvalds's avatar
Linus Torvalds committed
3817
                        mbox->cmd.length = fr_udp_pkt->cblock.length = sizeof(global_stats_t)+
Linus Torvalds's avatar
Linus Torvalds committed
3818
                                                     sizeof(rx_intr_stat_t);
Linus Torvalds's avatar
Linus Torvalds committed
3819
			fr_udp_pkt->cblock.result = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3820
                        break;
Linus Torvalds's avatar
Linus Torvalds committed
3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842


		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;
				} 
			}
           
			/* 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;
				} 
			}  
			goto udp_mgmt_dflt;

			
Linus Torvalds's avatar
Linus Torvalds committed
3843
		default:
Linus Torvalds's avatar
Linus Torvalds committed
3844
udp_mgmt_dflt:
Linus Torvalds's avatar
Linus Torvalds committed
3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881
 			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) {
Linus Torvalds's avatar
Linus Torvalds committed
3882 3883 3884 3885 3886 3887 3888 3889

		chan->fr_header_len=2;
		chan->fr_header[0]=Q922_UI;
		chan->fr_header[1]=NLPID_IP;
			
		err = fr_send_data_header(card, dlci, 0, len, 
			card->u.f.udp_pkt_data,chan->fr_header_len);
		if (err){ 
Linus Torvalds's avatar
Linus Torvalds committed
3890
			chan->drvstats_gen.UDP_PIPE_mgmt_adptr_send_passed ++;
Linus Torvalds's avatar
Linus Torvalds committed
3891
		}else{
Linus Torvalds's avatar
Linus Torvalds committed
3892
			chan->drvstats_gen.UDP_PIPE_mgmt_adptr_send_failed ++;
Linus Torvalds's avatar
Linus Torvalds committed
3893 3894
		}
		
Linus Torvalds's avatar
Linus Torvalds committed
3895 3896 3897 3898 3899 3900 3901 3902
	} 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, card->u.f.udp_pkt_data, len);
        
Linus Torvalds's avatar
Linus Torvalds committed
3903 3904
			chan->drvstats_gen.
				UDP_PIPE_mgmt_passed_to_stack ++;
Linus Torvalds's avatar
Linus Torvalds committed
3905
			new_skb->dev = dev;
Linus Torvalds's avatar
Linus Torvalds committed
3906 3907 3908
			new_skb->protocol = htons(ETH_P_IP);
			new_skb->mac.raw = new_skb->data;
			netif_rx(new_skb);
Linus Torvalds's avatar
Linus Torvalds committed
3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926
            	
		} 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;
}

/*==============================================================================
 * Send Inverse ARP Request
 */

3927
int send_inarp_request(sdla_t *card, struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
3928
{
Linus Torvalds's avatar
Linus Torvalds committed
3929 3930
	int err=0;

Linus Torvalds's avatar
Linus Torvalds committed
3931 3932 3933 3934 3935 3936
	arphdr_1490_t *ArpPacket;
	arphdr_fr_t *arphdr;
	fr_channel_t *chan = dev->priv;
	struct in_device *in_dev;

	in_dev = dev->ip_ptr;
Linus Torvalds's avatar
Linus Torvalds committed
3937

Linus Torvalds's avatar
Linus Torvalds committed
3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964
	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 */

Linus Torvalds's avatar
Linus Torvalds committed
3965 3966 3967 3968 3969 3970 3971 3972 3973
		err = fr_send(card, chan->dlci, 0, sizeof(arphdr_1490_t) + sizeof(arphdr_fr_t),
		   			(void *)ArpPacket);

		if (!err){
			printk(KERN_INFO "\n%s: Sending InARP request on DLCI %d.\n", 
				card->devname, chan->dlci);
			clear_bit(ARP_CRIT,&card->wandev.critical);
		}

Linus Torvalds's avatar
Linus Torvalds committed
3974
		kfree(ArpPacket);
Linus Torvalds's avatar
Linus Torvalds committed
3975 3976 3977 3978
	}else{
		printk(KERN_INFO "%s: INARP ERROR: %s doesn't have a local IP address!\n",
				card->devname,dev->name);
		return 1;
Linus Torvalds's avatar
Linus Torvalds committed
3979 3980
	}

Linus Torvalds's avatar
Linus Torvalds committed
3981
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003
}
	

/*==============================================================================
 * Check packet for ARP Type
 */

int is_arp(void *buf)
{
	arphdr_1490_t *arphdr = (arphdr_1490_t *)buf;
	
	if (arphdr->pad   == 0x00  &&
	    arphdr->NLPID == 0x80  &&
	    arphdr->PID   == 0x0608) 
		return 1;
	else return 0;
}

/*==============================================================================
 * Process ARP Packet Type
 */

4004
int process_ARP(arphdr_1490_t *ArpPacket, sdla_t *card, struct net_device* dev)
Linus Torvalds's avatar
Linus Torvalds committed
4005 4006
{

Linus Torvalds's avatar
Linus Torvalds committed
4007

Linus Torvalds's avatar
Linus Torvalds committed
4008 4009 4010
	arphdr_fr_t *arphdr = (arphdr_fr_t *)(ArpPacket + 1); /* Skip header */
	fr_rx_buf_ctl_t* frbuf = card->rxmb;
	struct in_device *in_dev;
Linus Torvalds's avatar
Linus Torvalds committed
4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024
	fr_channel_t *chan = dev->priv;		
	
	/* Before we transmit ARP packet, 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 (check_tx_status(card,dev)){
		if (net_ratelimit()){ 	
			printk(KERN_INFO "%s: Disabling comminication to process ARP\n",
					card->devname);
		}
		set_bit(ARP_CRIT,&card->wandev.critical);
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
4025 4026 4027

	in_dev = dev->ip_ptr;

Linus Torvalds's avatar
Linus Torvalds committed
4028 4029 4030 4031 4032 4033 4034
	/* Check that IP addresses exist for our network address */
	if (in_dev == NULL || in_dev->ifa_list == NULL) 
		return -1;

	switch (ntohs(arphdr->ar_op)) {

	case 0x08:  // Inverse ARP request  -- Send Reply, add route.
Linus Torvalds's avatar
Linus Torvalds committed
4035
			
Linus Torvalds's avatar
Linus Torvalds committed
4036
		/* Check for valid Address */
4037 4038
		printk(KERN_INFO "%s: Recvd PtP addr -InArp Req: %u.%u.%u.%u\n", 
			card->devname, NIPQUAD(arphdr->ar_sip));
Linus Torvalds's avatar
Linus Torvalds committed
4039 4040


Linus Torvalds's avatar
Linus Torvalds committed
4041 4042 4043
		/* Check that the network address is the same as ours, only
                 * if the netowrk mask is not 255.255.255.255. Otherwise
                 * this check would not make sense */
Linus Torvalds's avatar
Linus Torvalds committed
4044

Linus Torvalds's avatar
Linus Torvalds committed
4045 4046 4047 4048
		if (in_dev->ifa_list->ifa_mask != 0xFFFFFFFF && 
		    (in_dev->ifa_list->ifa_mask & arphdr->ar_sip) != 
		    (in_dev->ifa_list->ifa_mask & in_dev->ifa_list->ifa_local)){
			printk(KERN_INFO 
4049 4050
				"%s: Invalid PtP address. %u.%u.%u.%u  InARP ignored.\n", 
					card->devname,NIPQUAD(arphdr->ar_sip));
Linus Torvalds's avatar
Linus Torvalds committed
4051

4052
			printk(KERN_INFO "%s: mask %u.%u.%u.%u\n", 
4053
				card->devname, NIPQUAD(in_dev->ifa_list->ifa_mask));
4054
				printk(KERN_INFO "%s: local %u.%u.%u.%u\n", 
4055
				card->devname,NIPQUAD(in_dev->ifa_list->ifa_local));
Linus Torvalds's avatar
Linus Torvalds committed
4056 4057
			return -1;
		}
Linus Torvalds's avatar
Linus Torvalds committed
4058

Linus Torvalds's avatar
Linus Torvalds committed
4059 4060 4061 4062 4063 4064
		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;
		}
Linus Torvalds's avatar
Linus Torvalds committed
4065
	
Linus Torvalds's avatar
Linus Torvalds committed
4066
		arphdr->ar_op = htons(0x09);	/* InARP Reply */
Linus Torvalds's avatar
Linus Torvalds committed
4067

Linus Torvalds's avatar
Linus Torvalds committed
4068 4069 4070
		/* Set addresses */
		arphdr->ar_tip = arphdr->ar_sip;
		arphdr->ar_sip = in_dev->ifa_list->ifa_local;
Linus Torvalds's avatar
Linus Torvalds committed
4071

Linus Torvalds's avatar
Linus Torvalds committed
4072 4073
		chan->ip_local = in_dev->ifa_list->ifa_local;
		chan->ip_remote = arphdr->ar_sip;
Linus Torvalds's avatar
Linus Torvalds committed
4074

Linus Torvalds's avatar
Linus Torvalds committed
4075
		fr_send(card, frbuf->dlci, 0, frbuf->length, (void *)ArpPacket);
Linus Torvalds's avatar
Linus Torvalds committed
4076

Linus Torvalds's avatar
Linus Torvalds committed
4077 4078 4079 4080 4081 4082 4083 4084 4085 4086
		if (test_bit(ARP_CRIT,&card->wandev.critical)){
			if (net_ratelimit()){ 	
				printk(KERN_INFO "%s: ARP Processed Enabling Communication!\n",
					card->devname);
			}
		}
		clear_bit(ARP_CRIT,&card->wandev.critical);
		
		chan->ip_local = in_dev->ifa_list->ifa_local;
		chan->ip_remote = arphdr->ar_sip;
Linus Torvalds's avatar
Linus Torvalds committed
4087

Linus Torvalds's avatar
Linus Torvalds committed
4088 4089 4090
		/* Add Route Flag */
		/* The route will be added in the polling routine so
		   that it is not interrupt context. */
Linus Torvalds's avatar
Linus Torvalds committed
4091

Linus Torvalds's avatar
Linus Torvalds committed
4092 4093
		chan->route_flag = ADD_ROUTE;
		trigger_fr_poll (dev);
Linus Torvalds's avatar
Linus Torvalds committed
4094

Linus Torvalds's avatar
Linus Torvalds committed
4095
		break;
Linus Torvalds's avatar
Linus Torvalds committed
4096

Linus Torvalds's avatar
Linus Torvalds committed
4097
	case 0x09:  // Inverse ARP reply
Linus Torvalds's avatar
Linus Torvalds committed
4098

Linus Torvalds's avatar
Linus Torvalds committed
4099
		/* Check for valid Address */
4100 4101
		printk(KERN_INFO "%s: Recvd PtP addr %u.%u.%u.%u -InArp Reply\n", 
				card->devname, NIPQUAD(arphdr->ar_sip));
Linus Torvalds's avatar
Linus Torvalds committed
4102 4103


Linus Torvalds's avatar
Linus Torvalds committed
4104 4105 4106
		/* Compare network addresses, only if network mask
                 * is not 255.255.255.255  It would not make sense
                 * to perform this test if the mask was all 1's */
Linus Torvalds's avatar
Linus Torvalds committed
4107

Linus Torvalds's avatar
Linus Torvalds committed
4108 4109 4110
		if (in_dev->ifa_list->ifa_mask != 0xffffffff &&
		    (in_dev->ifa_list->ifa_mask & arphdr->ar_sip) != 
			(in_dev->ifa_list->ifa_mask & in_dev->ifa_list->ifa_local)) {
Linus Torvalds's avatar
Linus Torvalds committed
4111

Linus Torvalds's avatar
Linus Torvalds committed
4112 4113 4114
			printk(KERN_INFO "%s: Invalid PtP address.  InARP ignored.\n", 
					card->devname);
			return -1;
Linus Torvalds's avatar
Linus Torvalds committed
4115 4116
		}

Linus Torvalds's avatar
Linus Torvalds committed
4117 4118 4119 4120 4121 4122 4123
		/* Make sure that the received IP address is not
                 * the same as our own local address */
		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;
		}			
Linus Torvalds's avatar
Linus Torvalds committed
4124

Linus Torvalds's avatar
Linus Torvalds committed
4125 4126
		chan->ip_local  = in_dev->ifa_list->ifa_local;
		chan->ip_remote = arphdr->ar_sip;
Linus Torvalds's avatar
Linus Torvalds committed
4127

Linus Torvalds's avatar
Linus Torvalds committed
4128 4129 4130
		/* Add Route Flag */
		/* The route will be added in the polling routine so
		   that it is not interrupt context. */
Linus Torvalds's avatar
Linus Torvalds committed
4131

Linus Torvalds's avatar
Linus Torvalds committed
4132 4133 4134 4135 4136
		chan->route_flag = ADD_ROUTE;
		chan->inarp = INARP_CONFIGURED;
		trigger_fr_poll(dev);
		
		break;
Linus Torvalds's avatar
Linus Torvalds committed
4137 4138
	default:
		break; // ARP's and RARP's -- Shouldn't happen.
Linus Torvalds's avatar
Linus Torvalds committed
4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160
	}

	return 0;	
}


/*============================================================
 * trigger_fr_arp
 *
 * Description:
 * 	Add an fr_arp() task into a arp
 *      timer handler for a specific dlci/interface.  
 *      This will kick the fr_arp() routine 
 *      within the specified time interval. 
 *
 * Usage:
 * 	This timer is used to send ARP requests at
 *      certain time intervals. 
 * 	Called by an interrupt to request an action
 *      at a later date.
 */	

4161
static void trigger_fr_arp(struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181
{
	fr_channel_t* chan = dev->priv;

	del_timer(&chan->fr_arp_timer);
	chan->fr_arp_timer.expires = jiffies + (chan->inarp_interval * HZ);
	add_timer(&chan->fr_arp_timer);
	return;
}



/*==============================================================================
 * ARP Request Action
 *
 *	This funciton is called by timer interrupt to send an arp request
 *      to the remote end.
 */

static void fr_arp (unsigned long data)
{
4182
	struct net_device *dev = (struct net_device *)data;
Linus Torvalds's avatar
Linus Torvalds committed
4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211
	fr_channel_t *chan = dev->priv;
	volatile sdla_t *card = chan->card;
	fr508_flags_t* flags = card->flags;

	/* Send ARP packets for all devs' until
         * ARP state changes to CONFIGURED */

	if (chan->inarp == INARP_REQUEST &&
	    chan->common.state == WAN_CONNECTED && 
	    card->wandev.state == WAN_CONNECTED){
		set_bit(0,&chan->inarp_ready);
		card->u.f.timer_int_enabled |= TMR_INT_ENABLED_ARP;
		flags->imask |= FR_INTR_TIMER;	
	}
 
	return;
}
	

/*==============================================================================
 * 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;

        err = fr_set_intr_mode(card, FR_INTR_READY, card->wandev.mtu, 0 );
Linus Torvalds's avatar
Linus Torvalds committed
4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242
	
	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;	
	}

	err = fr_set_intr_mode( card, 0, card->wandev.mtu, 0 );

	if( err != CMD_OK ) 
		return err;

	return 0;
}

/*==============================================================================
 * Determine what type of UDP call it is. FPIPE8ND ?
 */
static int udp_pkt_type( struct sk_buff *skb, sdla_t* card )
{
	fr_udp_pkt_t *fr_udp_pkt = (fr_udp_pkt_t *)skb->data;

Linus Torvalds's avatar
Linus Torvalds committed
4243 4244 4245
	/* Quick HACK */
	
	
Linus Torvalds's avatar
Linus Torvalds committed
4246 4247 4248 4249 4250 4251 4252
        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,
Linus Torvalds's avatar
Linus Torvalds committed
4253
                                UDPMGMT_FPIPE_SIGNATURE, 8)){
Linus Torvalds's avatar
Linus Torvalds committed
4254
                                return UDP_FPIPE_TYPE;
Linus Torvalds's avatar
Linus Torvalds committed
4255
			}
Linus Torvalds's avatar
Linus Torvalds committed
4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326
	}
        return UDP_INVALID_TYPE;
}


/*==============================================================================
 * Initializes the Statistics values in the fr_channel structure.
 */
void init_chan_statistics( fr_channel_t* chan)
{
        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 )
{
	/* Intialize global statistics for a card */
        memset(&card->statistics.isr_entry, 0, sizeof(global_stats_t));
}

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 {
		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);
	}

	counter = mbox->cmd.length / sizeof(dlci_IB_mapping_t);
	result = (void *)mbox->data;
	
	found = 0;
	for (; counter; --counter, ++result) {
		if ( result->dlci == chan->dlci ) {
			chan->IB_addr = result->addr_value;
			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;	
		} 
	}
	if (!found)
		printk( KERN_INFO "%s: DLCI %d not found by IB MAPPING cmd\n", 
		card->devname, chan->dlci);
}

Linus Torvalds's avatar
Linus Torvalds committed
4327 4328


Linus Torvalds's avatar
Linus Torvalds committed
4329 4330 4331
void s508_s514_lock(sdla_t *card, unsigned long *smp_flags)
{
	if (card->hw.type != SDLA_S514){
Linus Torvalds's avatar
Linus Torvalds committed
4332 4333 4334 4335

		spin_lock_irqsave(&card->wandev.lock, *smp_flags);
	}else{
		spin_lock(&card->u.f.if_send_lock);
Linus Torvalds's avatar
Linus Torvalds committed
4336
	}
Linus Torvalds's avatar
Linus Torvalds committed
4337
	return;
Linus Torvalds's avatar
Linus Torvalds committed
4338 4339
}

Linus Torvalds's avatar
Linus Torvalds committed
4340

Linus Torvalds's avatar
Linus Torvalds committed
4341 4342 4343
void s508_s514_unlock(sdla_t *card, unsigned long *smp_flags)
{
	if (card->hw.type != SDLA_S514){
Linus Torvalds's avatar
Linus Torvalds committed
4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373

		spin_unlock_irqrestore (&card->wandev.lock, *smp_flags);
	}else{
		spin_unlock(&card->u.f.if_send_lock);
	}
	return;
}



/*----------------------------------------------------------------------
                  RECEIVE INTERRUPT: BOTTOM HALF HANDLERS 
 ----------------------------------------------------------------------*/


/*========================================================
 * bh_enqueue
 *
 * Description:
 *	Insert a received packed into a circular
 *      rx queue.  This packed will be picked up 
 *      by fr_bh() and sent up the stack to the
 *      user.
 *       	
 * Usage: 
 *	This function is called by rx interrupt,
 *      in API mode.
 *
 */

4374
static int bh_enqueue(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
4375 4376 4377 4378 4379 4380 4381 4382
{
	/* Check for full */
	fr_channel_t* chan = dev->priv;
	sdla_t *card = chan->card;


	if (atomic_read(&chan->bh_buff_used) == MAX_BH_BUFF){
		++card->wandev.stats.rx_dropped;
4383
		dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416
		return 1; 
	}

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

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

	atomic_inc(&chan->bh_buff_used);

	return 0;
}


/*========================================================
 * trigger_fr_bh
 *
 * Description:
 * 	Kick the fr_bh() handler
 *
 * Usage:
 *	rx interrupt calls this function during
 *      the API mode. 
 */

static void trigger_fr_bh (fr_channel_t *chan)
{
	if (!test_and_set_bit(0,&chan->tq_working)){
		wanpipe_queue_tq(&chan->common.wanpipe_task);
		wanpipe_mark_bh();
Linus Torvalds's avatar
Linus Torvalds committed
4417
	}
Linus Torvalds's avatar
Linus Torvalds committed
4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446
}


/*========================================================
 * fr_bh
 *
 * Description:
 *	Frame relay receive BH handler. 
 *	Dequeue data from the BH circular 
 *	buffer and pass it up the API sock.
 *       	
 * Rationale: 
 *	This fuction is used to offload the 
 *	rx_interrupt during API operation mode.  
 *	The fr_bh() function executes for each 
 *	dlci/interface.  
 * 
 *      Once receive interrupt copies data from the
 *      card into an skb buffer, the skb buffer
 *  	is appended to a circular BH buffer.
 *  	Then the interrupt kicks fr_bh() to finish the
 *      job at a later time (no within the interrupt).
 *       
 * Usage:
 * 	Interrupts use this to defer a taks to 
 *      a polling routine.
 *
 */	

4447
static void fr_bh(struct net_device * dev)
Linus Torvalds's avatar
Linus Torvalds committed
4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471
{
	fr_channel_t* chan = dev->priv;
	sdla_t *card = chan->card;
	struct sk_buff *skb;

	if (atomic_read(&chan->bh_buff_used) == 0){
		clear_bit(0, &chan->tq_working);
		return;
	}

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

		if (chan->common.sk == NULL || chan->common.func == NULL){
			clear_bit(0, &chan->tq_working);
			return;
		}

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

		if (skb != NULL){

			if (chan->common.sk == NULL || chan->common.func == NULL){
				++card->wandev.stats.rx_dropped;
				++chan->ifstats.rx_dropped;
4472
				dev_kfree_skb_any(skb);
Linus Torvalds's avatar
Linus Torvalds committed
4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487
				fr_bh_cleanup(dev);
				continue;
			}

			if (chan->common.func(skb,dev,chan->common.sk) != 0){
				/* Sock full cannot send, queue us for
                                 * another try */
				atomic_set(&chan->common.receive_block,1);
				return;
			}else{
				fr_bh_cleanup(dev);
			}
		}else{
			fr_bh_cleanup(dev);
		}
Linus Torvalds's avatar
Linus Torvalds committed
4488
	}	
Linus Torvalds's avatar
Linus Torvalds committed
4489 4490 4491 4492 4493
	clear_bit(0, &chan->tq_working);

	return;
}

4494
static int fr_bh_cleanup(struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508
{
	fr_channel_t* chan = dev->priv;

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

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

	atomic_dec(&chan->bh_buff_used);
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
4509

Linus Torvalds's avatar
Linus Torvalds committed
4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527

/*----------------------------------------------------------------------
               POLL BH HANDLERS AND KICK ROUTINES 
 ----------------------------------------------------------------------*/

/*============================================================
 * trigger_fr_poll
 *
 * Description:
 * 	Add a fr_poll() task into a tq_scheduler bh handler
 *      for a specific dlci/interface.  This will kick
 *      the fr_poll() routine at a later time. 
 *
 * Usage:
 * 	Interrupts use this to defer a taks to 
 *      a polling routine.
 *
 */	
4528
static void trigger_fr_poll(struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557
{
	fr_channel_t* chan = dev->priv;
	schedule_task(&chan->fr_poll_task);
	return;
}


/*============================================================
 * fr_poll
 *	
 * Rationale:
 * 	We cannot manipulate the routing tables, or
 *      ip addresses withing the interrupt. Therefore
 *      we must perform such actons outside an interrupt 
 *      at a later time. 
 *
 * Description:	
 *	Frame relay polling routine, responsible for 
 *     	shutting down interfaces upon disconnect
 *     	and adding/removing routes. 
 *      
 * Usage:        
 * 	This function is executed for each frame relay
 * 	dlci/interface through a tq_schedule bottom half.
 *      
 *      trigger_fr_poll() function is used to kick
 *      the fr_poll routine.  
 */

4558
static void fr_poll(struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644
{

	fr_channel_t* chan;
	sdla_t *card;
	u8 check_gateway=0;

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

	card = chan->card;
	
	/* (Re)Configuraiton is in progress, stop what you are 
	 * doing and get out */
	if (test_bit(PERI_CRIT,&card->wandev.critical)){
		return;
	}

	switch (chan->common.state){

	case WAN_DISCONNECTED:

		if (test_bit(DYN_OPT_ON,&chan->interface_down) &&
		    !test_bit(DEV_DOWN, &chan->interface_down) &&
		    dev->flags&IFF_UP){

			printk(KERN_INFO "%s: Interface %s is Down.\n", 
				card->devname,dev->name);
			change_dev_flags(dev,dev->flags&~IFF_UP);
			set_bit(DEV_DOWN, &chan->interface_down);
			chan->route_flag = NO_ROUTE;
			
		}else{
			if (chan->inarp != INARP_NONE)
				process_route(dev);	
		}
		break;

	case WAN_CONNECTED:

		if (test_bit(DYN_OPT_ON,&chan->interface_down) &&
		    test_bit(DEV_DOWN, &chan->interface_down) &&
		    !(dev->flags&IFF_UP)){

			printk(KERN_INFO "%s: Interface %s is Up.\n", 
					card->devname,dev->name);

			change_dev_flags(dev,dev->flags|IFF_UP);
			clear_bit(DEV_DOWN, &chan->interface_down);
			check_gateway=1;
		}

		if (chan->inarp != INARP_NONE){
			process_route(dev);
			check_gateway=1;
		}

		if (chan->gateway && check_gateway)
			add_gateway(card,dev);

		break;

	}

	return;	
}

/*==============================================================
 * check_tx_status
 *
 * Rationale:
 *	We cannot transmit from an interrupt while
 *      the if_send is transmitting data.  Therefore,
 *      we must check whether the tx buffers are
 *      begin used, before we transmit from an
 *      interrupt.	
 * 
 * Description:	
 *	Checks whether it's safe to use the transmit 
 *      buffers. 
 *
 * Usage:
 * 	ARP and UDP handling routines use this function
 *      because, they need to transmit data during
 *      an interrupt.
 */

4645
static int check_tx_status(sdla_t *card, struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
4646 4647 4648 4649 4650 4651 4652 4653 4654
{

	if (card->hw.type == SDLA_S514){
		if (test_bit(SEND_CRIT, (void*)&card->wandev.critical) ||
			test_bit(SEND_TXIRQ_CRIT, (void*)&card->wandev.critical)) {
			return 1;
		}
	}

4655
	if (netif_queue_stopped(dev) || (card->u.f.tx_interrupts_pending))
Linus Torvalds's avatar
Linus Torvalds committed
4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674
     		return 1; 

	return 0;
}

/*===============================================================
 * move_dev_to_next
 *  
 * Description:
 *	Move the dev pointer to the next location in the
 *      link list.  Check if we are at the end of the 
 *      list, if so start from the begining.
 *
 * Usage:
 * 	Timer interrupt uses this function to efficiently
 *      step through the devices that need to send ARP data.
 *
 */

4675
struct net_device *move_dev_to_next(sdla_t *card, struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
4676 4677
{
	if (card->wandev.new_if_cnt != 1){
4678
		if (!*((struct net_device **)dev->priv))
Linus Torvalds's avatar
Linus Torvalds committed
4679
			return card->wandev.dev;
4680 4681
		else
			return *((struct net_device **)dev->priv);
Linus Torvalds's avatar
Linus Torvalds committed
4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730
	}
	return dev;
}

/*==============================================================
 * trigger_config_fr
 *
 * Rationale:
 *	All commands must be performed inside of a  
 *      interrupt.   
 *
 * Description:
 *	Kick the config_fr() routine throught the
 *      timer interrupt.
 */


static void trigger_config_fr (sdla_t *card)
{
	fr508_flags_t* flags = card->flags;

	card->u.f.timer_int_enabled |= TMR_INT_ENABLED_CONFIG;
	flags->imask |= FR_INTR_TIMER;
}


/*==============================================================
 * config_fr
 *
 * Rationale:
 * 	All commands must be performed inside of a  
 *      interrupt.  
 &
 * Description:	
 * 	Configure a DLCI. This function is executed
 *      by a timer_interrupt.  The if_open() function
 *      triggers it.
 *
 * Usage:
 *	new_if() collects all data necessary to
 *      configure the DLCI. It sets the chan->dlci_ready 
 *      bit.  When the if_open() function is executed
 *      it checks this bit, and if its set it triggers
 *      the timer interrupt to execute the config_fr()
 *      function.
 */

static void config_fr (sdla_t *card)
{
4731
	struct net_device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
4732 4733
	fr_channel_t *chan;

4734 4735
	for (dev = card->wandev.dev; dev;
	     dev = *((struct net_device **)dev->priv)) {
Linus Torvalds's avatar
Linus Torvalds committed
4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803
	
		if ((chan=dev->priv) == NULL)
			continue;
		
		if (!test_bit(0,&chan->config_dlci))
			continue;

		clear_bit(0,&chan->config_dlci);

		/* 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)){
				printk(KERN_INFO "%s: ERROR: Failed to configure DLCI %i !\n",
					card->devname, chan->dlci);
				return;
			}
		}

		if (card->wandev.station == WANOPT_CPE) {
	
			update_chan_state(dev);	
			
			/* 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.  
			 */ 
			
			if (!check_dlci_config (card, chan)){
				fr_add_dlci(card, chan->dlci);
				fr_activate_dlci(card, chan->dlci);
			}
		}

		card->u.f.dlci_to_dev_map[chan->dlci] = dev;
	}
	return;
}


/*==============================================================
 * config_fr
 *
 * Rationale:
 *	All commands must be executed during an interrupt.
 * 
 * Description:	
 *	Trigger uncofig_fr() function through 
 *      the timer interrupt.
 *
 */

4804
static void trigger_unconfig_fr(struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855
{
	fr_channel_t *chan = dev->priv;
	volatile sdla_t *card = chan->card;
	u32 timeout;
	fr508_flags_t* flags = card->flags;
	int reset_critical=0;
	
	if (test_bit(PERI_CRIT,(void*)&card->wandev.critical)){
		clear_bit(PERI_CRIT,(void*)&card->wandev.critical);
		reset_critical=1;
	}
		
	/* run unconfig_dlci() function 
         * throught the timer interrupt */
	set_bit(0,(void*)&chan->unconfig_dlci);
	card->u.f.timer_int_enabled |= TMR_INT_ENABLED_UNCONFIG;
	flags->imask |= FR_INTR_TIMER;

	/* Wait for the command to complete */
	timeout = jiffies;
     	for(;;) {

		if(!(card->u.f.timer_int_enabled & TMR_INT_ENABLED_UNCONFIG))
			break;

             	if ((jiffies - timeout) > (1 * HZ)){
    			card->u.f.timer_int_enabled &= ~TMR_INT_ENABLED_UNCONFIG;
			printk(KERN_INFO "%s: Failed to delete DLCI %i\n",
				card->devname,chan->dlci);
 			break;
		}
	}

	if (reset_critical){
		set_bit(PERI_CRIT,(void*)&card->wandev.critical);
	}
}

/*==============================================================
 * unconfig_fr
 *
 * Rationale:
 *	All commands must be executed during an interrupt.
 * 
 * Description:	
 *	Remove the dlci from firmware.
 *	This funciton is used in NODE shutdown.
 */

static void unconfig_fr (sdla_t *card)
{
4856
	struct net_device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
4857 4858
	fr_channel_t *chan;

4859 4860
	for (dev = card->wandev.dev; dev;
	     dev = *((struct net_device **)dev->priv)){
Linus Torvalds's avatar
Linus Torvalds committed
4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878
	
		if ((chan=dev->priv) == NULL)
			continue;
		
		if (!test_bit(0,&chan->unconfig_dlci))
			continue;

		clear_bit(0,&chan->unconfig_dlci);

		if (card->wandev.station == WANOPT_NODE){
			printk(KERN_INFO "%s: Unconfiguring DLCI %i\n",
					card->devname,chan->dlci);
			fr_delete_dlci(card,chan->dlci);
		}
		card->u.f.dlci_to_dev_map[chan->dlci] = NULL;
	}
}

4879 4880
static int setup_fr_header(struct sk_buff **skb_orig, struct net_device* dev,
			   char op_mode)
Linus Torvalds's avatar
Linus Torvalds committed
4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906
{
	struct sk_buff *skb = *skb_orig;
	fr_channel_t *chan=dev->priv;

	if (op_mode == WANPIPE){

		chan->fr_header[0]=Q922_UI;
		
		switch (htons(skb->protocol)){
			
		case ETH_P_IP:
			chan->fr_header[1]=NLPID_IP;
			break;
		default:
			return -EINVAL;
		}
			
		return 2;
	}

	/* If we are in bridging mode, we must apply
	 * an Ethernet header */
	if (op_mode == BRIDGE || op_mode == BRIDGE_NODE){


		/* Encapsulate the packet as a bridged Ethernet frame. */
Linus Torvalds's avatar
Linus Torvalds committed
4907
#ifdef DEBUG
Linus Torvalds's avatar
Linus Torvalds committed
4908 4909
		printk(KERN_INFO "%s: encapsulating skb for frame relay\n", 
			dev->name);
Linus Torvalds's avatar
Linus Torvalds committed
4910
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937
		
		chan->fr_header[0] = 0x03;
		chan->fr_header[1] = 0x00;
		chan->fr_header[2] = 0x80;
		chan->fr_header[3] = 0x00;
		chan->fr_header[4] = 0x80;
		chan->fr_header[5] = 0xC2;
		chan->fr_header[6] = 0x00;
		chan->fr_header[7] = 0x07;

		/* Yuck. */
		skb->protocol = ETH_P_802_3;
		return 8;

	}
		
	return 0;
}


static int check_dlci_config (sdla_t *card, fr_channel_t *chan)
{
	fr_mbox_t* mbox = card->mbox;
	int err=0;
	fr_conf_t *conf=NULL;
	unsigned short dlci_num = chan->dlci;
	int dlci_offset=0;
4938
	struct net_device *dev = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949
	
	mbox->cmd.command = FR_READ_CONFIG;
	mbox->cmd.length = 0;
	mbox->cmd.dlci = dlci_num; 	

	err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	
	if (err == CMD_OK){
		return 0;
	}

4950 4951
	for (dev = card->wandev.dev; dev;
	     dev=*((struct net_device **)dev->priv))
Linus Torvalds's avatar
Linus Torvalds committed
4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979
		set_chan_state(dev,WAN_DISCONNECTED);
	
	printk(KERN_INFO "DLCI %i Not configured, configuring\n",dlci_num);
	
	mbox->cmd.command = FR_COMM_DISABLE;
	mbox->cmd.length = 0;
	err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	if (err != CMD_OK){
		fr_event(card, err, mbox);
		return 2;
	}

	printk(KERN_INFO "Disabled Communications \n");
	
	mbox->cmd.command = FR_READ_CONFIG;
	mbox->cmd.length = 0;
	mbox->cmd.dlci = 0; 	

	err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
	
	if (err != CMD_OK){
		fr_event(card, err, mbox);
		return 2;
	}
	
	conf = (fr_conf_t *)mbox->data;

	dlci_offset=0;
4980 4981
	for (dev = card->wandev.dev; dev;
	     dev = *((struct net_device **)dev->priv)) {
Linus Torvalds's avatar
Linus Torvalds committed
4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014
		fr_channel_t *chan_tmp = dev->priv;
		conf->dlci[dlci_offset] = chan_tmp->dlci;		
		dlci_offset++;
	}
	
	printk(KERN_INFO "Got Fr configuration Buffer Length is %x Dlci %i Dlci Off %i\n",
		mbox->cmd.length,
		mbox->cmd.length > 0x20 ? conf->dlci[0] : -1, 
		dlci_offset );
	
	mbox->cmd.length = 0x20 + dlci_offset*2;

	mbox->cmd.command = FR_SET_CONFIG;
	mbox->cmd.dlci = 0; 

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

	if (err != CMD_OK){
		fr_event(card, err, mbox);
		return 2;
	}

	initialize_rx_tx_buffers (card);

	
	printk(KERN_INFO "Configuraiton Succeded for new DLCI %i\n",dlci_num);

	if (fr_comm_enable (card)){
		return 2;
	}

	printk(KERN_INFO "Enabling Communications \n");

5015 5016
	for (dev = card->wandev.dev; dev;
	     dev = *((struct net_device **)dev->priv)) {
Linus Torvalds's avatar
Linus Torvalds committed
5017 5018 5019 5020 5021 5022 5023 5024 5025
		fr_channel_t *chan_tmp = dev->priv;
		fr_init_dlci(card,chan_tmp);
		fr_add_dlci(card, chan_tmp->dlci);
		fr_activate_dlci(card, chan_tmp->dlci);
	}

	printk(KERN_INFO "END OF CONFIGURAITON %i\n",dlci_num);
	
	return 1;
Linus Torvalds's avatar
Linus Torvalds committed
5026 5027
}

Linus Torvalds's avatar
Linus Torvalds committed
5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071
static void initialize_rx_tx_buffers (sdla_t *card)
{
	fr_buf_info_t* buf_info;
	
	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;

	return;
}

	

Linus Torvalds's avatar
Linus Torvalds committed
5072
MODULE_LICENSE("GPL");
Linus Torvalds's avatar
Linus Torvalds committed
5073

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