1 /*****************************************************************************
2 * sdla_ppp.c	WANPIPE(tm) Multiprotocol WAN Link Driver. PPP module.
3 *
4 * Author: 	Nenad Corbic <ncorbic@sangoma.com>
5 *
6 * Copyright:	(c) 1995-2001 Sangoma Technologies Inc.
7 *
8 *		This program is free software; you can redistribute it and/or
9 *		modify it under the terms of the GNU General Public License
10 *		as published by the Free Software Foundation; either version
11 *		2 of the License, or (at your option) any later version.
12 * ============================================================================
13 * Feb 28, 2001  Nenad Corbic	o Updated if_tx_timeout() routine for
14 * 				  2.4.X kernels.
15 * Nov 29, 2000  Nenad Corbic	o Added the 2.4.x kernel support:
16 * 				  get_ip_address() function has moved
17 * 				  into the ppp_poll() routine. It cannot
18 * 				  be called from an interrupt.
19 * Nov 07, 2000  Nenad Corbic	o Added security features for UDP debugging:
20 *                                 Deny all and specify allowed requests.
21 * May 02, 2000  Nenad Corbic	o Added the dynamic interface shutdown
22 *                                 option. When the link goes down, the
23 *                                 network interface IFF_UP flag is reset.
24 * Mar 06, 2000  Nenad Corbic	o Bug Fix: corrupted mbox recovery.
25 * Feb 25, 2000  Nenad Corbic    o Fixed the FT1 UDP debugger problem.
26 * Feb 09, 2000  Nenad Coribc    o Shutdown bug fix. update() was called
27 *                                 with NULL dev pointer: no check.
28 * Jan 24, 2000  Nenad Corbic    o Disabled use of CMD complete inter.
29 * Dev 15, 1999  Nenad Corbic    o Fixed up header files for 2.0.X kernels
30 * Oct 25, 1999  Nenad Corbic    o Support for 2.0.X kernels
31 *                                 Moved dynamic route processing into
32 *                                 a polling routine.
33 * Oct 07, 1999  Nenad Corbic    o Support for S514 PCI card.
34 *               Gideon Hack     o UPD and Updates executed using timer interrupt
35 * Sep 10, 1999  Nenad Corbic    o Fixed up the /proc statistics
36 * Jul 20, 1999  Nenad Corbic    o Remove the polling routines and use
37 *                                 interrupts instead.
38 * Sep 17, 1998	Jaspreet Singh	o Updates for 2.2.X Kernels.
39 * Aug 13, 1998	Jaspreet Singh	o Improved Line Tracing.
40 * Jun 22, 1998	David Fong	o Added remote IP address assignment
41 * Mar 15, 1998	Alan Cox	o 2.1.8x basic port.
42 * Apr 16, 1998	Jaspreet Singh	o using htons() for the IPX protocol.
43 * Dec 09, 1997	Jaspreet Singh	o Added PAP and CHAP.
44 *				o Implemented new routines like
45 *				  ppp_set_inbnd_auth(), ppp_set_outbnd_auth(),
46 *				  tokenize() and strstrip().
47 * Nov 27, 1997	Jaspreet Singh	o Added protection against enabling of irqs
48 *				  while they have been disabled.
49 * Nov 24, 1997  Jaspreet Singh  o Fixed another RACE condition caused by
50 *                                 disabling and enabling of irqs.
51 *                               o Added new counters for stats on disable/enable
52 *                                 IRQs.
53 * Nov 10, 1997	Jaspreet Singh	o Initialized 'skb->mac.raw' to 'skb->data'
54 *				  before every netif_rx().
55 *				o Free up the device structure in del_if().
56 * Nov 07, 1997	Jaspreet Singh	o Changed the delay to zero for Line tracing
57 *				  command.
58 * Oct 20, 1997 	Jaspreet Singh	o Added hooks in for Router UP time.
59 * Oct 16, 1997	Jaspreet Singh  o The critical flag is used to maintain flow
60 *				  control by avoiding RACE conditions.  The
61 *				  cli() and restore_flags() are taken out.
62 *				  A new structure, "ppp_private_area", is added
63 *				  to provide Driver Statistics.
64 * Jul 21, 1997 	Jaspreet Singh	o Protected calls to sdla_peek() by adding
65 *				  save_flags(), cli() and restore_flags().
66 * Jul 07, 1997	Jaspreet Singh  o Added configurable TTL for UDP packets
67 *				o Added ability to discard mulitcast and
68 *				  broacast source addressed packets.
69 * Jun 27, 1997 	Jaspreet Singh	o Added FT1 monitor capabilities
70 *				  New case (0x25) statement in if_send routine.
71 *				  Added a global variable rCount to keep track
72 *				  of FT1 status enabled on the board.
73 * May 22, 1997	Jaspreet Singh	o Added change in the PPP_SET_CONFIG command for
74 *				508 card to reflect changes in the new
75 *				ppp508.sfm for supporting:continous transmission
76 *				of Configure-Request packets without receiving a
77 *				reply
78 *				OR-ed 0x300 to conf_flags
79 *			        o Changed connect_tmout from 900 to 0
80 * May 21, 1997	Jaspreet Singh  o Fixed UDP Management for multiple boards
81 * Apr 25, 1997  Farhan Thawar    o added UDP Management stuff
82 * Mar 11, 1997  Farhan Thawar   Version 3.1.1
83 *                                o fixed (+1) bug in rx_intr()
84 *                                o changed if_send() to return 0 if
85 *                                  wandev.critical() is true
86 *                                o free socket buffer in if_send() if
87 *                                  returning 0
88 * Jan 15, 1997	Gene Kozin	Version 3.1.0
89 *				 o implemented exec() entry point
90 * Jan 06, 1997	Gene Kozin	Initial version.
91 *****************************************************************************/
92 
93 #include <linux/module.h>
94 #include <linux/version.h>
95 #include <linux/kernel.h>	/* printk(), and other useful stuff */
96 #include <linux/stddef.h>	/* offsetof(), etc. */
97 #include <linux/errno.h>	/* return codes */
98 #include <linux/string.h>	/* inline memset(), etc. */
99 #include <linux/slab.h>	/* kmalloc(), kfree() */
100 #include <linux/wanrouter.h>	/* WAN router definitions */
101 #include <linux/wanpipe.h>	/* WANPIPE common user API definitions */
102 #include <linux/if_arp.h>	/* ARPHRD_* defines */
103 #include <asm/byteorder.h>	/* htons(), etc. */
104 #include <linux/in.h>		/* sockaddr_in */
105 
106 
107 /* ---- 2.4.X KERNEL SUPPORT -----------------------*/
108 #if defined(LINUX_2_1) || defined(LINUX_2_4)
109  #include <asm/uaccess.h>
110  #include <linux/inetdevice.h>
111  #include <linux/netdevice.h>
112 #else
113  #include <asm/segment.h>
114  #include <net/route.h>          /* Adding new route entries : 2.0.X kernels */
115 #endif
116 
117 #include <linux/if.h>
118 #include <linux/sdla_ppp.h>		/* PPP firmware API definitions */
119 #include <linux/sdlasfm.h>		/* S514 Type Definition */
120 /****** Defines & Macros ****************************************************/
121 
122 #define	PPP_DFLT_MTU	1500		/* default MTU */
123 #define	PPP_MAX_MTU	4000		/* maximum MTU */
124 #define PPP_HDR_LEN	1
125 
126 #define MAX_IP_ERRORS 100
127 
128 #define	CONNECT_TIMEOUT	(90*HZ)		/* link connection timeout */
129 #define	HOLD_DOWN_TIME	(5*HZ)		/* link hold down time : Changed from 30 to 5 */
130 
131 /* For handle_IPXWAN() */
132 #define CVHexToAscii(b) (((unsigned char)(b) > (unsigned char)9) ? ((unsigned char)'A' + ((unsigned char)(b) - (unsigned char)10)) : ((unsigned char)'0' + (unsigned char)(b)))
133 
134 /* Macro for enabling/disabling debugging comments */
135 //#define NEX_DEBUG
136 #ifdef NEX_DEBUG
137 #define NEX_PRINTK(format, a...) printk(format, ## a)
138 #else
139 #define NEX_PRINTK(format, a...)
140 #endif /* NEX_DEBUG */
141 
142 #define DCD(a)   ( a & 0x08 ? "HIGH" : "LOW" )
143 #define CTS(a)   ( a & 0x20 ? "HIGH" : "LOW" )
144 #define LCP(a)   ( a == 0x09 ? "OPEN" : "CLOSED" )
145 #define IP(a)    ( a == 0x09 ? "ENABLED" : "DISABLED" )
146 
147 #define TMR_INT_ENABLED_UPDATE  	0x01
148 #define TMR_INT_ENABLED_PPP_EVENT	0x02
149 #define TMR_INT_ENABLED_UDP		0x04
150 #define TMR_INT_ENABLED_CONFIG		0x20
151 
152 /* Set Configuraton Command Definitions */
153 #define PERCENT_TX_BUFF			60
154 #define TIME_BETWEEN_CONF_REQ  		30
155 #define TIME_BETWEEN_PAP_CHAP_REQ	30
156 #define WAIT_PAP_CHAP_WITHOUT_REPLY     300
157 #define WAIT_AFTER_DCD_CTS_LOW          5
158 #define TIME_DCD_CTS_LOW_AFTER_LNK_DOWN 10
159 #define WAIT_DCD_HIGH_AFTER_ENABLE_COMM 900
160 #define MAX_CONF_REQ_WITHOUT_REPLY      10
161 #define MAX_TERM_REQ_WITHOUT_REPLY      2
162 #define NUM_CONF_NAK_WITHOUT_REPLY      5
163 #define NUM_AUTH_REQ_WITHOUT_REPLY      10
164 
165 #define END_OFFSET 0x1F0
166 #if LINUX_VERSION_CODE < 0x020125
167 #define test_and_set_bit set_bit
168 #define net_ratelimit() 1
169 #endif
170 
171 
172 /******Data Structures*****************************************************/
173 
174 /* This structure is placed in the private data area of the device structure.
175  * The card structure used to occupy the private area but now the following
176  * structure will incorporate the card structure along with PPP specific data
177  */
178 
179 typedef struct ppp_private_area
180 {
181 	netdevice_t *slave;
182 	sdla_t* card;
183 	unsigned long router_start_time;	/*router start time in sec */
184 	unsigned long tick_counter;		/*used for 5 second counter*/
185 	unsigned mc;				/*multicast support on or off*/
186 	unsigned char enable_IPX;
187 	unsigned long network_number;
188 	unsigned char pap;
189 	unsigned char chap;
190 	unsigned char sysname[31];		/* system name for in-bnd auth*/
191 	unsigned char userid[511];		/* list of user ids */
192 	unsigned char passwd[511];		/* list of passwords */
193 	unsigned protocol;			/* SKB Protocol */
194 	u32 ip_local;				/* Local IP Address */
195 	u32 ip_remote;				/* remote IP Address */
196 
197 	u32 ip_local_tmp;
198 	u32 ip_remote_tmp;
199 
200 	unsigned char timer_int_enabled;	/* Who enabled the timer inter*/
201 	unsigned char update_comms_stats;	/* Used by update function */
202 	unsigned long curr_trace_addr;		/* Trace information */
203 	unsigned long start_trace_addr;
204 	unsigned long end_trace_addr;
205 
206 	unsigned char interface_down;		/* Brind down interface when channel
207                                                    goes down */
208 	unsigned long config_wait_timeout;	/* After if_open() if in dynamic if mode,
209 						   wait a few seconds before configuring */
210 
211 	unsigned short udp_pkt_lgth;
212 	char  udp_pkt_src;
213       	char  udp_pkt_data[MAX_LGTH_UDP_MGNT_PKT];
214 
215 	/* PPP specific statistics */
216 
217 	if_send_stat_t if_send_stat;
218 	rx_intr_stat_t rx_intr_stat;
219 	pipe_mgmt_stat_t pipe_mgmt_stat;
220 
221 	unsigned long router_up_time;
222 
223 	/* Polling task queue. Each interface
224          * has its own task queue, which is used
225          * to defer events from the interrupt */
226 	struct tq_struct poll_task;
227 	struct timer_list poll_delay_timer;
228 
229 	u8 gateway;
230 	u8 config_ppp;
231 	u8 ip_error;
232 
233 }ppp_private_area_t;
234 
235 /* variable for keeping track of enabling/disabling FT1 monitor status */
236 static int rCount = 0;
237 
238 extern void disable_irq(unsigned int);
239 extern void enable_irq(unsigned int);
240 
241 /****** Function Prototypes *************************************************/
242 
243 /* WAN link driver entry points. These are called by the WAN router module. */
244 static int update(wan_device_t *wandev);
245 static int new_if(wan_device_t *wandev, netdevice_t *dev, wanif_conf_t *conf);
246 static int del_if(wan_device_t *wandev, netdevice_t *dev);
247 
248 /* WANPIPE-specific entry points */
249 static int wpp_exec (struct sdla *card, void *u_cmd, void *u_data);
250 
251 /* Network device interface */
252 static int if_init(netdevice_t *dev);
253 static int if_open(netdevice_t *dev);
254 static int if_close(netdevice_t *dev);
255 static int if_header(struct sk_buff *skb, netdevice_t *dev, unsigned short type,
256 		     void *daddr, void *saddr, unsigned len);
257 
258 #ifdef LINUX_2_4
259 static void if_tx_timeout (netdevice_t *dev);
260 #endif
261 
262 #if defined(LINUX_2_1) || defined(LINUX_2_4)
263 static int if_rebuild_hdr(struct sk_buff *skb);
264 static struct net_device_stats *if_stats(netdevice_t *dev);
265 #else
266 static struct enet_statistics *if_stats(netdevice_t *dev);
267 static int if_rebuild_hdr (void* hdr, netdevice_t* dev, unsigned long raddr,
268         struct sk_buff* skb);
269 #endif
270 static int if_send(struct sk_buff *skb, netdevice_t *dev);
271 
272 
273 /* PPP firmware interface functions */
274 static int ppp_read_version(sdla_t *card, char *str);
275 static int ppp_set_outbnd_auth(sdla_t *card, ppp_private_area_t *ppp_priv_area);
276 static int ppp_set_inbnd_auth(sdla_t *card, ppp_private_area_t *ppp_priv_area);
277 static int ppp_configure(sdla_t *card, void *data);
278 static int ppp_set_intr_mode(sdla_t *card, unsigned char mode);
279 static int ppp_comm_enable(sdla_t *card);
280 static int ppp_comm_disable(sdla_t *card);
281 static int ppp_comm_disable_shutdown(sdla_t *card);
282 static int ppp_get_err_stats(sdla_t *card);
283 static int ppp_send(sdla_t *card, void *data, unsigned len, unsigned proto);
284 static int ppp_error(sdla_t *card, int err, ppp_mbox_t *mb);
285 
286 static void wpp_isr(sdla_t *card);
287 static void rx_intr(sdla_t *card);
288 static void event_intr(sdla_t *card);
289 static void timer_intr(sdla_t *card);
290 
291 /* Background polling routines */
292 static void process_route(sdla_t *card);
293 static void retrigger_comm(sdla_t *card);
294 
295 /* Miscellaneous functions */
296 static int read_info( sdla_t *card );
297 static int read_connection_info (sdla_t *card);
298 static void remove_route( sdla_t *card );
299 static int config508(netdevice_t *dev, sdla_t *card);
300 static void show_disc_cause(sdla_t * card, unsigned cause);
301 static int reply_udp( unsigned char *data, unsigned int mbox_len );
302 static void process_udp_mgmt_pkt(sdla_t *card, netdevice_t *dev,
303 				ppp_private_area_t *ppp_priv_area);
304 static void init_ppp_tx_rx_buff( sdla_t *card );
305 static int intr_test( sdla_t *card );
306 static int udp_pkt_type( struct sk_buff *skb , sdla_t *card);
307 static void init_ppp_priv_struct( ppp_private_area_t *ppp_priv_area);
308 static void init_global_statistics( sdla_t *card );
309 static int tokenize(char *str, char **tokens);
310 static char* strstrip(char *str, char *s);
311 static int chk_bcast_mcast_addr(sdla_t* card, netdevice_t* dev,
312 				struct sk_buff *skb);
313 
314 static int config_ppp (sdla_t *);
315 static void ppp_poll(netdevice_t *);
316 static void trigger_ppp_poll(netdevice_t *);
317 static void ppp_poll_delay (unsigned long dev_ptr);
318 
319 
320 static int Read_connection_info;
321 static int Intr_test_counter;
322 static unsigned short available_buffer_space;
323 
324 
325 /* IPX functions */
326 static void switch_net_numbers(unsigned char *sendpacket, unsigned long network_number,
327 			       unsigned char incoming);
328 static int handle_IPXWAN(unsigned char *sendpacket, char *devname, unsigned char enable_PX,
329 			 unsigned long network_number, unsigned short proto);
330 
331 /* Lock Functions */
332 static void s508_lock (sdla_t *card, unsigned long *smp_flags);
333 static void s508_unlock (sdla_t *card, unsigned long *smp_flags);
334 
335 static int store_udp_mgmt_pkt(char udp_pkt_src, sdla_t* card,
336                                 struct sk_buff *skb, netdevice_t* dev,
337                                 ppp_private_area_t* ppp_priv_area );
338 static unsigned short calc_checksum (char *data, int len);
339 static void disable_comm (sdla_t *card);
340 static int detect_and_fix_tx_bug (sdla_t *card);
341 
342 /****** Public Functions ****************************************************/
343 
344 /*============================================================================
345  * PPP protocol initialization routine.
346  *
347  * This routine is called by the main WANPIPE module during setup.  At this
348  * point adapter is completely initialized and firmware is running.
349  *  o read firmware version (to make sure it's alive)
350  *  o configure adapter
351  *  o initialize protocol-specific fields of the adapter data space.
352  *
353  * Return:	0	o.k.
354  *		< 0	failure.
355  */
wpp_init(sdla_t * card,wandev_conf_t * conf)356 int wpp_init(sdla_t *card, wandev_conf_t *conf)
357 {
358 	ppp_flags_t *flags;
359 	union
360 	{
361 		char str[80];
362 	} u;
363 
364 	/* Verify configuration ID */
365 	if (conf->config_id != WANCONFIG_PPP) {
366 
367 		printk(KERN_INFO "%s: invalid configuration ID %u!\n",
368 			card->devname, conf->config_id);
369 		return -EINVAL;
370 
371 	}
372 
373 	/* Initialize miscellaneous pointers to structures on the adapter */
374 	switch (card->hw.type) {
375 
376 		case SDLA_S508:
377 			card->mbox =(void*)(card->hw.dpmbase + PPP508_MB_OFFS);
378 			card->flags=(void*)(card->hw.dpmbase + PPP508_FLG_OFFS);
379 			break;
380 
381 		case SDLA_S514:
382 			card->mbox =(void*)(card->hw.dpmbase + PPP514_MB_OFFS);
383 			card->flags=(void*)(card->hw.dpmbase + PPP514_FLG_OFFS);
384 			break;
385 
386 		default:
387 			return -EINVAL;
388 
389 	}
390 	flags = card->flags;
391 
392 	/* Read firmware version.  Note that when adapter initializes, it
393 	 * clears the mailbox, so it may appear that the first command was
394 	 * executed successfully when in fact it was merely erased. To work
395 	 * around this, we execute the first command twice.
396 	 */
397 	if (ppp_read_version(card, NULL) || ppp_read_version(card, u.str))
398 		return -EIO;
399 
400 	printk(KERN_INFO "%s: running PPP firmware v%s\n",card->devname, u.str);
401 	/* Adjust configuration and set defaults */
402 	card->wandev.mtu = (conf->mtu) ?
403 		min_t(unsigned int, conf->mtu, PPP_MAX_MTU) : PPP_DFLT_MTU;
404 
405 	card->wandev.bps	= conf->bps;
406 	card->wandev.interface	= conf->interface;
407 	card->wandev.clocking	= conf->clocking;
408 	card->wandev.station	= conf->station;
409 	card->isr		= &wpp_isr;
410 	card->poll		= NULL;
411 	card->exec		= &wpp_exec;
412 	card->wandev.update	= &update;
413 	card->wandev.new_if	= &new_if;
414 	card->wandev.del_if	= &del_if;
415         card->wandev.udp_port   = conf->udp_port;
416 	card->wandev.ttl	= conf->ttl;
417 	card->wandev.state      = WAN_DISCONNECTED;
418 	card->disable_comm	= &disable_comm;
419 	card->irq_dis_if_send_count = 0;
420         card->irq_dis_poll_count = 0;
421 	card->u.p.authenticator = conf->u.ppp.authenticator;
422 	card->u.p.ip_mode 	= conf->u.ppp.ip_mode ?
423 				 conf->u.ppp.ip_mode : WANOPT_PPP_STATIC;
424         card->TracingEnabled    = 0;
425 	Read_connection_info    = 1;
426 
427 	/* initialize global statistics */
428 	init_global_statistics( card );
429 
430 
431 
432 	if (!card->configured){
433 		int err;
434 
435 		Intr_test_counter = 0;
436 		err = intr_test(card);
437 
438 		if(err || (Intr_test_counter < MAX_INTR_TEST_COUNTER)) {
439 			printk("%s: Interrupt Test Failed, Counter: %i\n",
440 				card->devname, Intr_test_counter);
441 			printk( "%s: Please choose another interrupt\n",card->devname);
442 			return -EIO;
443 		}
444 
445 		printk(KERN_INFO "%s: Interrupt Test Passed, Counter: %i\n",
446 			card->devname, Intr_test_counter);
447 		card->configured = 1;
448 	}
449 
450 	ppp_set_intr_mode(card, PPP_INTR_TIMER);
451 
452 	/* Turn off the transmit and timer interrupt */
453 	flags->imask &= ~PPP_INTR_TIMER;
454 
455 	printk(KERN_INFO "\n");
456 
457 	return 0;
458 }
459 
460 /******* WAN Device Driver Entry Points *************************************/
461 
462 /*============================================================================
463  * Update device status & statistics.
464  */
update(wan_device_t * wandev)465 static int update(wan_device_t *wandev)
466 {
467 	sdla_t* card = wandev->private;
468  	netdevice_t* dev;
469         volatile ppp_private_area_t *ppp_priv_area;
470 	ppp_flags_t *flags = card->flags;
471 	unsigned long timeout;
472 
473 	/* sanity checks */
474 	if ((wandev == NULL) || (wandev->private == NULL))
475 		return -EFAULT;
476 
477 	if (wandev->state == WAN_UNCONFIGURED)
478 		return -ENODEV;
479 
480 	/* Shutdown bug fix. This function can be
481          * called with NULL dev pointer during
482          * shutdown
483 	 */
484 	if ((dev=card->wandev.dev) == NULL){
485 		return -ENODEV;
486 	}
487 
488 	if ((ppp_priv_area=dev->priv) == NULL){
489 		return -ENODEV;
490 	}
491 
492 	ppp_priv_area->update_comms_stats = 2;
493 	ppp_priv_area->timer_int_enabled |= TMR_INT_ENABLED_UPDATE;
494 	flags->imask |= PPP_INTR_TIMER;
495 
496 	/* wait a maximum of 1 second for the statistics to be updated */
497         timeout = jiffies;
498         for(;;) {
499 		if(ppp_priv_area->update_comms_stats == 0){
500 			break;
501 		}
502                 if ((jiffies - timeout) > (1 * HZ)){
503     			ppp_priv_area->update_comms_stats = 0;
504  			ppp_priv_area->timer_int_enabled &=
505 				~TMR_INT_ENABLED_UPDATE;
506  			return -EAGAIN;
507 		}
508         }
509 
510 	return 0;
511 }
512 
513 /*============================================================================
514  * Create new logical channel.
515  * This routine is called by the router when ROUTER_IFNEW IOCTL is being
516  * handled.
517  * o parse media- and hardware-specific configuration
518  * o make sure that a new channel can be created
519  * o allocate resources, if necessary
520  * o prepare network device structure for registaration.
521  *
522  * Return:	0	o.k.
523  *		< 0	failure (channel will not be created)
524  */
new_if(wan_device_t * wandev,netdevice_t * dev,wanif_conf_t * conf)525 static int new_if(wan_device_t *wandev, netdevice_t *dev, wanif_conf_t *conf)
526 {
527 	sdla_t *card = wandev->private;
528 	ppp_private_area_t *ppp_priv_area;
529 
530 	if (wandev->ndev)
531 		return -EEXIST;
532 
533 
534 	printk(KERN_INFO "%s: Configuring Interface: %s\n",
535 			card->devname, conf->name);
536 
537 	if ((conf->name[0] == '\0') || (strlen(conf->name) > WAN_IFNAME_SZ)) {
538 
539 		printk(KERN_INFO "%s: Invalid interface name!\n",
540 			card->devname);
541 		return -EINVAL;
542 
543 	}
544 
545 	/* allocate and initialize private data */
546 	ppp_priv_area = kmalloc(sizeof(ppp_private_area_t), GFP_KERNEL);
547 
548 	if( ppp_priv_area == NULL )
549 		return	-ENOMEM;
550 
551 	memset(ppp_priv_area, 0, sizeof(ppp_private_area_t));
552 
553 	ppp_priv_area->card = card;
554 
555 	/* initialize data */
556 	strcpy(card->u.p.if_name, conf->name);
557 
558 	/* initialize data in ppp_private_area structure */
559 
560 	init_ppp_priv_struct( ppp_priv_area );
561 
562 	ppp_priv_area->mc = conf->mc;
563 	ppp_priv_area->pap = conf->pap;
564 	ppp_priv_area->chap = conf->chap;
565 
566 	/* Option to bring down the interface when
567          * the link goes down */
568 	if (conf->if_down){
569 		set_bit(DYN_OPT_ON,&ppp_priv_area->interface_down);
570 		printk("%s: Dynamic interface configuration enabled\n",
571 			card->devname);
572 	}
573 
574 	/* If no user ids are specified */
575 	if(!strlen(conf->userid) && (ppp_priv_area->pap||ppp_priv_area->chap)){
576 		kfree(ppp_priv_area);
577 		return -EINVAL;
578 	}
579 
580 	/* If no passwords are specified */
581 	if(!strlen(conf->passwd) && (ppp_priv_area->pap||ppp_priv_area->chap)){
582 		kfree(ppp_priv_area);
583 		return -EINVAL;
584 	}
585 
586 	if(strlen(conf->sysname) > 31){
587 		kfree(ppp_priv_area);
588 		return -EINVAL;
589 	}
590 
591 	/* If no system name is specified */
592 	if(!strlen(conf->sysname) && (card->u.p.authenticator)){
593 		kfree(ppp_priv_area);
594 		return -EINVAL;
595 	}
596 
597 	/* copy the data into the ppp private structure */
598 	memcpy(ppp_priv_area->userid, conf->userid, strlen(conf->userid));
599 	memcpy(ppp_priv_area->passwd, conf->passwd, strlen(conf->passwd));
600 	memcpy(ppp_priv_area->sysname, conf->sysname, strlen(conf->sysname));
601 
602 
603 	ppp_priv_area->enable_IPX = conf->enable_IPX;
604 	if (conf->network_number){
605 		ppp_priv_area->network_number = conf->network_number;
606 	}else{
607 		ppp_priv_area->network_number = 0xDEADBEEF;
608 	}
609 
610 	/* Tells us that if this interface is a
611          * gateway or not */
612 	if ((ppp_priv_area->gateway = conf->gateway) == WANOPT_YES){
613 		printk(KERN_INFO "%s: Interface %s is set as a gateway.\n",
614 			card->devname,card->u.p.if_name);
615 	}
616 
617 	/* prepare network device data space for registration */
618 #ifdef LINUX_2_4
619  	strcpy(dev->name,card->u.p.if_name);
620 #else
621 	dev->name = (char *)kmalloc(strlen(card->u.p.if_name) + 2, GFP_KERNEL);
622 	if(dev->name == NULL)
623 	{
624 		kfree(ppp_priv_area);
625 		return -ENOMEM;
626 	}
627 	sprintf(dev->name, "%s", card->u.p.if_name);
628 #endif
629 
630 	dev->init = &if_init;
631 	dev->priv = ppp_priv_area;
632 	dev->mtu = min_t(unsigned int, dev->mtu, card->wandev.mtu);
633 
634 	/* Initialize the polling task routine */
635 #ifndef LINUX_2_4
636 	ppp_priv_area->poll_task.next = NULL;
637 #endif
638 	ppp_priv_area->poll_task.sync=0;
639 	ppp_priv_area->poll_task.routine = (void*)(void*)ppp_poll;
640 	ppp_priv_area->poll_task.data = dev;
641 
642 	/* Initialize the polling delay timer */
643 	init_timer(&ppp_priv_area->poll_delay_timer);
644 	ppp_priv_area->poll_delay_timer.data = (unsigned long)dev;
645 	ppp_priv_area->poll_delay_timer.function = ppp_poll_delay;
646 
647 
648 	/* Since we start with dummy IP addresses we can say
649 	 * that route exists */
650 	printk(KERN_INFO "\n");
651 
652 	return 0;
653 }
654 
655 /*============================================================================
656  * Delete logical channel.
657  */
del_if(wan_device_t * wandev,netdevice_t * dev)658 static int del_if(wan_device_t *wandev, netdevice_t *dev)
659 {
660 	return 0;
661 }
662 
disable_comm(sdla_t * card)663 static void disable_comm (sdla_t *card)
664 {
665 	ppp_comm_disable_shutdown(card);
666 	return;
667 }
668 
669 /****** WANPIPE-specific entry points ***************************************/
670 
671 /*============================================================================
672  * Execute adapter interface command.
673  */
674 
675 //FIXME: Why do we need this ????
wpp_exec(struct sdla * card,void * u_cmd,void * u_data)676 static int wpp_exec(struct sdla *card, void *u_cmd, void *u_data)
677 {
678 	ppp_mbox_t *mbox = card->mbox;
679 	int len;
680 
681 #if defined(LINUX_2_1) || defined(LINUX_2_4)
682 	if (copy_from_user((void*)&mbox->cmd, u_cmd, sizeof(ppp_cmd_t)))
683 		return -EFAULT;
684 
685 	len = mbox->cmd.length;
686 
687 	if (len) {
688 
689 		if( copy_from_user((void*)&mbox->data, u_data, len))
690 			return -EFAULT;
691 
692 	}
693 
694 	/* execute command */
695 	if (!sdla_exec(mbox))
696 		return -EIO;
697 
698 	/* return result */
699 	if( copy_to_user(u_cmd, (void*)&mbox->cmd, sizeof(ppp_cmd_t)))
700 		return -EFAULT;
701 	len = mbox->cmd.length;
702 
703 	if (len && u_data && copy_to_user(u_data, (void*)&mbox->data, len))
704 		return -EFAULT;
705 
706 #else
707 
708         if (!u_cmd || verify_area(VERIFY_WRITE, u_cmd, sizeof(ppp_cmd_t)))
709                 return -EFAULT;
710 
711         memcpy_fromfs((void*)&mbox->cmd, u_cmd, sizeof(ppp_cmd_t));
712 
713 	len = mbox->cmd.length;
714 
715         if (len) {
716 
717                 if (!u_data || verify_area(VERIFY_READ, u_data, len))
718                         return -EFAULT;
719         }
720 
721         /* execute command */
722         if (!sdla_exec(mbox))
723                	return -EIO;
724 
725         /* return result */
726         memcpy_tofs(u_cmd, (void*)&mbox->cmd, sizeof(ppp_cmd_t));
727         len = mbox->cmd.length;
728 
729         if (len && u_data && !verify_area(VERIFY_WRITE, u_data, len))
730                 memcpy_tofs(u_data, (void*)&mbox->data, len);
731 
732 
733 #endif
734 
735 	return 0;
736 }
737 
738 /****** Network Device Interface ********************************************/
739 
740 /*============================================================================
741  * Initialize Linux network interface.
742  *
743  * This routine is called only once for each interface, during Linux network
744  * interface registration.  Returning anything but zero will fail interface
745  * registration.
746  */
if_init(netdevice_t * dev)747 static int if_init(netdevice_t *dev)
748 {
749 	ppp_private_area_t *ppp_priv_area = dev->priv;
750 	sdla_t *card = ppp_priv_area->card;
751 	wan_device_t *wandev = &card->wandev;
752 #ifdef LINUX_2_0
753 	int i;
754 #endif
755 
756 	/* Initialize device driver entry points */
757 	dev->open		= &if_open;
758 	dev->stop		= &if_close;
759 	dev->hard_header	= &if_header;
760 	dev->rebuild_header	= &if_rebuild_hdr;
761 	dev->hard_start_xmit	= &if_send;
762 	dev->get_stats		= &if_stats;
763 #ifdef LINUX_2_4
764 	dev->tx_timeout		= &if_tx_timeout;
765 	dev->watchdog_timeo	= TX_TIMEOUT;
766 #endif
767 
768 	/* Initialize media-specific parameters */
769 	dev->type		= ARPHRD_PPP;	/* ARP h/w type */
770 	dev->flags		|= IFF_POINTOPOINT;
771 	dev->flags		|= IFF_NOARP;
772 
773 	/* Enable Mulitcasting if specified by user*/
774 	if (ppp_priv_area->mc == WANOPT_YES){
775 		dev->flags	|= IFF_MULTICAST;
776 	}
777 
778 #ifdef LINUX_2_0
779 	dev->family		= AF_INET;
780 #endif
781 	dev->mtu		= wandev->mtu;
782 	dev->hard_header_len	= PPP_HDR_LEN;	/* media header length */
783 
784 	/* Initialize hardware parameters (just for reference) */
785 	dev->irq		= wandev->irq;
786 	dev->dma		= wandev->dma;
787 	dev->base_addr		= wandev->ioport;
788 	dev->mem_start		= wandev->maddr;
789 	dev->mem_end		= wandev->maddr + wandev->msize - 1;
790 
791         /* Set transmit buffer queue length */
792         dev->tx_queue_len = 100;
793 
794 	/* Initialize socket buffers */
795       #if !defined(LINUX_2_1) && !defined(LINUX_2_4)
796         for (i = 0; i < DEV_NUMBUFFS; ++i)
797                 skb_queue_head_init(&dev->buffs[i]);
798       #endif
799 
800 	return 0;
801 }
802 
803 /*============================================================================
804  * Open network interface.
805  * o enable communications and interrupts.
806  * o prevent module from unloading by incrementing use count
807  *
808  * Return 0 if O.k. or errno.
809  */
if_open(netdevice_t * dev)810 static int if_open (netdevice_t *dev)
811 {
812 	ppp_private_area_t *ppp_priv_area = dev->priv;
813 	sdla_t *card = ppp_priv_area->card;
814 	struct timeval tv;
815 	//unsigned long smp_flags;
816 
817 	if (is_dev_running(dev))
818 		return -EBUSY;
819 
820 	wanpipe_open(card);
821 
822 #ifdef LINUX_2_4
823 	netif_start_queue(dev);
824 #else
825 	dev->interrupt = 0;
826 	dev->tbusy = 0;
827 	dev->start = 1;
828 #endif
829 
830 	do_gettimeofday( &tv );
831 	ppp_priv_area->router_start_time = tv.tv_sec;
832 
833 	/* We cannot configure the card here because we don't
834 	 * have access to the interface IP addresses.
835          * Once the interface initilization is complete, we will be
836          * able to access the IP addresses.  Therefore,
837          * configure the ppp link in the poll routine */
838 	set_bit(0,&ppp_priv_area->config_ppp);
839 	ppp_priv_area->config_wait_timeout=jiffies;
840 
841 	/* Start the PPP configuration after 1sec delay.
842 	 * This will give the interface initilization time
843 	 * to finish its configuration */
844 	del_timer(&ppp_priv_area->poll_delay_timer);
845 	ppp_priv_area->poll_delay_timer.expires = jiffies+HZ;
846 	add_timer(&ppp_priv_area->poll_delay_timer);
847 	return 0;
848 }
849 
850 /*============================================================================
851  * Close network interface.
852  * o if this is the last open, then disable communications and interrupts.
853  * o reset flags.
854  */
if_close(netdevice_t * dev)855 static int if_close(netdevice_t *dev)
856 {
857 	ppp_private_area_t *ppp_priv_area = dev->priv;
858 	sdla_t *card = ppp_priv_area->card;
859 
860 	stop_net_queue(dev);
861 #ifndef LINUX_2_4
862 	dev->start=0;
863 #endif
864 	wanpipe_close(card);
865 
866 	del_timer (&ppp_priv_area->poll_delay_timer);
867 	return 0;
868 }
869 
870 /*============================================================================
871  * Build media header.
872  *
873  * The trick here is to put packet type (Ethertype) into 'protocol' field of
874  * the socket buffer, so that we don't forget it.  If packet type is not
875  * supported, set skb->protocol to 0 and discard packet later.
876  *
877  * Return:	media header length.
878  */
if_header(struct sk_buff * skb,netdevice_t * dev,unsigned short type,void * daddr,void * saddr,unsigned len)879 static int if_header(struct sk_buff *skb, netdevice_t *dev,
880 	unsigned short type, void *daddr, void *saddr, unsigned len)
881 {
882 	switch (type)
883 	{
884 		case ETH_P_IP:
885 		case ETH_P_IPX:
886 			skb->protocol = htons(type);
887 			break;
888 
889 		default:
890 			skb->protocol = 0;
891 	}
892 
893 	return PPP_HDR_LEN;
894 }
895 
896 /*============================================================================
897  * Re-build media header.
898  *
899  * Return:	1	physical address resolved.
900  *		0	physical address not resolved
901  */
902 #if defined(LINUX_2_1) || defined(LINUX_2_4)
if_rebuild_hdr(struct sk_buff * skb)903 static int if_rebuild_hdr (struct sk_buff *skb)
904 {
905 	netdevice_t *dev = skb->dev;
906 	ppp_private_area_t *ppp_priv_area = dev->priv;
907 	sdla_t *card = ppp_priv_area->card;
908 
909 	printk(KERN_INFO "%s: rebuild_header() called for interface %s!\n",
910 		card->devname, dev->name);
911 	return 1;
912 }
913 
914 #else
if_rebuild_hdr(void * hdr,netdevice_t * dev,unsigned long raddr,struct sk_buff * skb)915 static int if_rebuild_hdr (void* hdr, netdevice_t* dev, unsigned long raddr,
916                            struct sk_buff* skb)
917 {
918 	return 1;
919 
920 }
921 #endif
922 
923 #ifdef LINUX_2_4
924 /*============================================================================
925  * Handle transmit timeout event from netif watchdog
926  */
if_tx_timeout(netdevice_t * dev)927 static void if_tx_timeout (netdevice_t *dev)
928 {
929     	ppp_private_area_t* chan = dev->priv;
930 	sdla_t *card = chan->card;
931 
932 	/* If our device stays busy for at least 5 seconds then we will
933 	 * kick start the device by making dev->tbusy = 0.  We expect
934 	 * that our device never stays busy more than 5 seconds. So this
935 	 * is only used as a last resort.
936 	 */
937 
938 	++ chan->if_send_stat.if_send_tbusy;
939 	++card->wandev.stats.collisions;
940 
941 	printk (KERN_INFO "%s: Transmit timed out on %s\n", card->devname,dev->name);
942 	++chan->if_send_stat.if_send_tbusy_timeout;
943 	netif_wake_queue (dev);
944 }
945 #endif
946 
947 
948 
949 /*============================================================================
950  * Send a packet on a network interface.
951  * o set tbusy flag (marks start of the transmission) to block a timer-based
952  *   transmit from overlapping.
953  * o check link state. If link is not up, then drop the packet.
954  * o execute adapter send command.
955  * o free socket buffer
956  *
957  * Return:	0	complete (socket buffer must be freed)
958  *		non-0	packet may be re-transmitted (tbusy must be set)
959  *
960  * Notes:
961  * 1. This routine is called either by the protocol stack or by the "net
962  *    bottom half" (with interrupts enabled).
963  * 2. Setting tbusy flag will inhibit further transmit requests from the
964  *    protocol stack and can be used for flow control with protocol layer.
965  */
if_send(struct sk_buff * skb,netdevice_t * dev)966 static int if_send (struct sk_buff *skb, netdevice_t *dev)
967 {
968 	ppp_private_area_t *ppp_priv_area = dev->priv;
969 	sdla_t *card = ppp_priv_area->card;
970 	unsigned char *sendpacket;
971 	unsigned long smp_flags;
972 	ppp_flags_t *flags = card->flags;
973 	int udp_type;
974 	int err=0;
975 
976 	++ppp_priv_area->if_send_stat.if_send_entry;
977 
978 #ifdef LINUX_2_4
979 	netif_stop_queue(dev);
980 #endif
981 
982 	if (skb == NULL) {
983 
984 		/* If we get here, some higher layer thinks we've missed an
985 		 * tx-done interrupt.
986 		 */
987 		printk(KERN_INFO "%s: interface %s got kicked!\n",
988 			card->devname, dev->name);
989 
990 		++ppp_priv_area->if_send_stat.if_send_skb_null;
991 
992 		wake_net_dev(dev);
993 		return 0;
994 	}
995 
996 #ifndef LINUX_2_4
997 	if (dev->tbusy) {
998 
999 		/* If our device stays busy for at least 5 seconds then we will
1000 		 * kick start the device by making dev->tbusy = 0.  We expect
1001 		 * that our device never stays busy more than 5 seconds. So this
1002 		 * is only used as a last resort.
1003 		 */
1004 
1005 		++ppp_priv_area->if_send_stat.if_send_tbusy;
1006         	++card->wandev.stats.collisions;
1007 
1008 		if ((jiffies - ppp_priv_area->tick_counter) < (5*HZ)) {
1009 			return 1;
1010 		}
1011 
1012 		printk (KERN_INFO "%s: Transmit times out on %s\n",card->devname,dev->name);
1013 
1014 		++ppp_priv_area->if_send_stat.if_send_tbusy_timeout;
1015 		++card->wandev.stats.collisions;
1016 
1017 		/* unbusy the card (because only one interface per card)*/
1018 		dev->tbusy = 0;
1019 	}
1020 #endif
1021 
1022 	sendpacket = skb->data;
1023 
1024 	udp_type = udp_pkt_type( skb, card );
1025 
1026 
1027 	if (udp_type == UDP_PTPIPE_TYPE){
1028 		if(store_udp_mgmt_pkt(UDP_PKT_FRM_STACK, card, skb, dev,
1029                 	              ppp_priv_area)){
1030 	               	flags->imask |= PPP_INTR_TIMER;
1031 		}
1032 		++ppp_priv_area->if_send_stat.if_send_PIPE_request;
1033 		start_net_queue(dev);
1034 		return 0;
1035 	}
1036 
1037 	/* Check for broadcast and multicast addresses
1038 	 * If found, drop (deallocate) a packet and return.
1039 	 */
1040 	if(chk_bcast_mcast_addr(card, dev, skb)){
1041 		++card->wandev.stats.tx_dropped;
1042 		wan_dev_kfree_skb(skb,FREE_WRITE);
1043 		start_net_queue(dev);
1044 		return 0;
1045 	}
1046 
1047 
1048  	if(card->hw.type != SDLA_S514){
1049 		s508_lock(card,&smp_flags);
1050 	}
1051 
1052     	if (test_and_set_bit(SEND_CRIT, (void*)&card->wandev.critical)) {
1053 
1054 		printk(KERN_INFO "%s: Critical in if_send: %lx\n",
1055 				card->wandev.name,card->wandev.critical);
1056 
1057 		++card->wandev.stats.tx_dropped;
1058 		++ppp_priv_area->if_send_stat.if_send_critical_non_ISR;
1059 		start_net_queue(dev);
1060 		goto if_send_exit_crit;
1061 	}
1062 
1063 	if (card->wandev.state != WAN_CONNECTED) {
1064 
1065 		++ppp_priv_area->if_send_stat.if_send_wan_disconnected;
1066         	++card->wandev.stats.tx_dropped;
1067 		start_net_queue(dev);
1068 
1069      	} else if (!skb->protocol) {
1070 		++ppp_priv_area->if_send_stat.if_send_protocol_error;
1071         	++card->wandev.stats.tx_errors;
1072 		start_net_queue(dev);
1073 
1074 	} else {
1075 
1076 		/*If it's IPX change the network numbers to 0 if they're ours.*/
1077 		if( skb->protocol == htons(ETH_P_IPX) ) {
1078 			if(ppp_priv_area->enable_IPX) {
1079 				switch_net_numbers( skb->data,
1080 					ppp_priv_area->network_number, 0);
1081 			} else {
1082 				++card->wandev.stats.tx_dropped;
1083 				start_net_queue(dev);
1084 				goto if_send_exit_crit;
1085 			}
1086 		}
1087 
1088 		if (ppp_send(card, skb->data, skb->len, skb->protocol)) {
1089 			stop_net_queue(dev);
1090 			++ppp_priv_area->if_send_stat.if_send_adptr_bfrs_full;
1091 			++ppp_priv_area->if_send_stat.if_send_tx_int_enabled;
1092 		} else {
1093 			++ppp_priv_area->if_send_stat.if_send_bfr_passed_to_adptr;
1094 			++card->wandev.stats.tx_packets;
1095 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1096 			card->wandev.stats.tx_bytes += skb->len;
1097 #endif
1098 			start_net_queue(dev);
1099 #ifdef LINUX_2_4
1100 			dev->trans_start = jiffies;
1101 #endif
1102 		}
1103     	}
1104 
1105 if_send_exit_crit:
1106 
1107 	if (!(err=is_queue_stopped(dev))){
1108       		wan_dev_kfree_skb(skb, FREE_WRITE);
1109 	}else{
1110 		ppp_priv_area->tick_counter = jiffies;
1111 		flags->imask |= PPP_INTR_TXRDY;	/* unmask Tx interrupts */
1112 	}
1113 
1114 	clear_bit(SEND_CRIT,&card->wandev.critical);
1115 	if(card->hw.type != SDLA_S514){
1116 		s508_unlock(card,&smp_flags);
1117 	}
1118 
1119 	return err;
1120 }
1121 
1122 
1123 /*=============================================================================
1124  * Store a UDP management packet for later processing.
1125  */
1126 
store_udp_mgmt_pkt(char udp_pkt_src,sdla_t * card,struct sk_buff * skb,netdevice_t * dev,ppp_private_area_t * ppp_priv_area)1127 static int store_udp_mgmt_pkt(char udp_pkt_src, sdla_t* card,
1128                                 struct sk_buff *skb, netdevice_t* dev,
1129                                 ppp_private_area_t* ppp_priv_area )
1130 {
1131 	int udp_pkt_stored = 0;
1132 
1133 	if(!ppp_priv_area->udp_pkt_lgth && (skb->len<=MAX_LGTH_UDP_MGNT_PKT)){
1134         	ppp_priv_area->udp_pkt_lgth = skb->len;
1135 		ppp_priv_area->udp_pkt_src = udp_pkt_src;
1136        		memcpy(ppp_priv_area->udp_pkt_data, skb->data, skb->len);
1137 		ppp_priv_area->timer_int_enabled |= TMR_INT_ENABLED_UDP;
1138 		ppp_priv_area->protocol = skb->protocol;
1139 		udp_pkt_stored = 1;
1140 	}else{
1141 		if (skb->len > MAX_LGTH_UDP_MGNT_PKT){
1142 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1143 			printk(KERN_INFO "%s: PIPEMON UDP request too long : %i\n",
1144 				card->devname, skb->len);
1145 #else
1146 			printk(KERN_INFO "%s: PIPEMON UDP request too long : %li\n",
1147 				card->devname, skb->len);
1148 #endif
1149 		}else{
1150 			printk(KERN_INFO "%s: PIPEMON UPD request already pending\n",
1151 				card->devname);
1152 		}
1153 		ppp_priv_area->udp_pkt_lgth = 0;
1154 	}
1155 
1156 	if(udp_pkt_src == UDP_PKT_FRM_STACK){
1157 		wan_dev_kfree_skb(skb, FREE_WRITE);
1158 	}else{
1159                 wan_dev_kfree_skb(skb, FREE_READ);
1160 	}
1161 
1162 	return(udp_pkt_stored);
1163 }
1164 
1165 
1166 
1167 /*============================================================================
1168  * Reply to UDP Management system.
1169  * Return length of reply.
1170  */
reply_udp(unsigned char * data,unsigned int mbox_len)1171 static int reply_udp( unsigned char *data, unsigned int mbox_len )
1172 {
1173 	unsigned short len, udp_length, temp, ip_length;
1174 	unsigned long ip_temp;
1175 	int even_bound = 0;
1176 	ppp_udp_pkt_t *p_udp_pkt = (ppp_udp_pkt_t *)data;
1177 
1178 	/* Set length of packet */
1179 	len = sizeof(ip_pkt_t)+
1180 	      sizeof(udp_pkt_t)+
1181 	      sizeof(wp_mgmt_t)+
1182 	      sizeof(cblock_t)+
1183 	      mbox_len;
1184 
1185 	/* fill in UDP reply */
1186   	p_udp_pkt->wp_mgmt.request_reply = UDPMGMT_REPLY;
1187 
1188 	/* fill in UDP length */
1189 	udp_length = sizeof(udp_pkt_t)+
1190 		     sizeof(wp_mgmt_t)+
1191 		     sizeof(cblock_t)+
1192 		     mbox_len;
1193 
1194 
1195 	/* put it on an even boundary */
1196 	if ( udp_length & 0x0001 ) {
1197 		udp_length += 1;
1198 		len += 1;
1199 		even_bound=1;
1200 	}
1201 
1202 	temp = (udp_length<<8)|(udp_length>>8);
1203 	p_udp_pkt->udp_pkt.udp_length = temp;
1204 
1205 
1206 	/* swap UDP ports */
1207 	temp = p_udp_pkt->udp_pkt.udp_src_port;
1208 	p_udp_pkt->udp_pkt.udp_src_port =
1209 			p_udp_pkt->udp_pkt.udp_dst_port;
1210 	p_udp_pkt->udp_pkt.udp_dst_port = temp;
1211 
1212 
1213 	/* add UDP pseudo header */
1214 	temp = 0x1100;
1215 	*((unsigned short *)(p_udp_pkt->data+mbox_len+even_bound)) = temp;
1216 	temp = (udp_length<<8)|(udp_length>>8);
1217 	*((unsigned short *)(p_udp_pkt->data+mbox_len+even_bound+2)) = temp;
1218 
1219 	/* calculate UDP checksum */
1220 	p_udp_pkt->udp_pkt.udp_checksum = 0;
1221 	p_udp_pkt->udp_pkt.udp_checksum =
1222 		calc_checksum(&data[UDP_OFFSET],udp_length+UDP_OFFSET);
1223 
1224 	/* fill in IP length */
1225 	ip_length = udp_length + sizeof(ip_pkt_t);
1226 	temp = (ip_length<<8)|(ip_length>>8);
1227   	p_udp_pkt->ip_pkt.total_length = temp;
1228 
1229 	/* swap IP addresses */
1230 	ip_temp = p_udp_pkt->ip_pkt.ip_src_address;
1231 	p_udp_pkt->ip_pkt.ip_src_address = p_udp_pkt->ip_pkt.ip_dst_address;
1232 	p_udp_pkt->ip_pkt.ip_dst_address = ip_temp;
1233 
1234 	/* fill in IP checksum */
1235 	p_udp_pkt->ip_pkt.hdr_checksum = 0;
1236 	p_udp_pkt->ip_pkt.hdr_checksum = calc_checksum(data,sizeof(ip_pkt_t));
1237 
1238 	return len;
1239 
1240 } /* reply_udp */
1241 
calc_checksum(char * data,int len)1242 unsigned short calc_checksum (char *data, int len)
1243 {
1244 	unsigned short temp;
1245 	unsigned long sum=0;
1246 	int i;
1247 
1248 	for( i = 0; i <len; i+=2 ) {
1249 		memcpy(&temp,&data[i],2);
1250 		sum += (unsigned long)temp;
1251 	}
1252 
1253 	while (sum >> 16 ) {
1254 		sum = (sum & 0xffffUL) + (sum >> 16);
1255 	}
1256 
1257 	temp = (unsigned short)sum;
1258 	temp = ~temp;
1259 
1260 	if( temp == 0 )
1261 		temp = 0xffff;
1262 
1263 	return temp;
1264 }
1265 
1266 /*
1267    If incoming is 0 (outgoing)- if the net numbers is ours make it 0
1268    if incoming is 1 - if the net number is 0 make it ours
1269 
1270 */
switch_net_numbers(unsigned char * sendpacket,unsigned long network_number,unsigned char incoming)1271 static void switch_net_numbers(unsigned char *sendpacket, unsigned long network_number, unsigned char incoming)
1272 {
1273 	unsigned long pnetwork_number;
1274 
1275 	pnetwork_number = (unsigned long)((sendpacket[6] << 24) +
1276 			  (sendpacket[7] << 16) + (sendpacket[8] << 8) +
1277 			  sendpacket[9]);
1278 
1279 	if (!incoming) {
1280 		//If the destination network number is ours, make it 0
1281 		if( pnetwork_number == network_number) {
1282 			sendpacket[6] = sendpacket[7] = sendpacket[8] =
1283 					 sendpacket[9] = 0x00;
1284 		}
1285 	} else {
1286 		//If the incoming network is 0, make it ours
1287 		if( pnetwork_number == 0) {
1288 			sendpacket[6] = (unsigned char)(network_number >> 24);
1289 			sendpacket[7] = (unsigned char)((network_number &
1290 					 0x00FF0000) >> 16);
1291 			sendpacket[8] = (unsigned char)((network_number &
1292 					 0x0000FF00) >> 8);
1293 			sendpacket[9] = (unsigned char)(network_number &
1294 					 0x000000FF);
1295 		}
1296 	}
1297 
1298 
1299 	pnetwork_number = (unsigned long)((sendpacket[18] << 24) +
1300 			  (sendpacket[19] << 16) + (sendpacket[20] << 8) +
1301 			  sendpacket[21]);
1302 
1303 	if( !incoming ) {
1304 		//If the source network is ours, make it 0
1305 		if( pnetwork_number == network_number) {
1306 			sendpacket[18] = sendpacket[19] = sendpacket[20] =
1307 					 sendpacket[21] = 0x00;
1308 		}
1309 	} else {
1310 		//If the source network is 0, make it ours
1311 		if( pnetwork_number == 0 ) {
1312 			sendpacket[18] = (unsigned char)(network_number >> 24);
1313 			sendpacket[19] = (unsigned char)((network_number &
1314 					 0x00FF0000) >> 16);
1315 			sendpacket[20] = (unsigned char)((network_number &
1316 					 0x0000FF00) >> 8);
1317 			sendpacket[21] = (unsigned char)(network_number &
1318 					 0x000000FF);
1319 		}
1320 	}
1321 } /* switch_net_numbers */
1322 
1323 /*============================================================================
1324  * Get ethernet-style interface statistics.
1325  * Return a pointer to struct net_device_stats.
1326  */
1327 #if defined(LINUX_2_1) || defined(LINUX_2_4)
if_stats(netdevice_t * dev)1328 static struct net_device_stats *if_stats(netdevice_t *dev)
1329 #else
1330 static struct enet_statistics *if_stats(netdevice_t *dev)
1331 #endif
1332 {
1333 
1334 	ppp_private_area_t *ppp_priv_area = dev->priv;
1335 	sdla_t* card;
1336 
1337 	if( ppp_priv_area == NULL )
1338 		return NULL;
1339 
1340 	card = ppp_priv_area->card;
1341 	return &card->wandev.stats;
1342 }
1343 
1344 /****** PPP Firmware Interface Functions ************************************/
1345 
1346 /*============================================================================
1347  * Read firmware code version.
1348  *	Put code version as ASCII string in str.
1349  */
ppp_read_version(sdla_t * card,char * str)1350 static int ppp_read_version(sdla_t *card, char *str)
1351 {
1352 	ppp_mbox_t *mb = card->mbox;
1353 	int err;
1354 
1355 	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1356 	mb->cmd.command = PPP_READ_CODE_VERSION;
1357 	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1358 
1359 	if (err != CMD_OK)
1360 
1361 		ppp_error(card, err, mb);
1362 
1363 	else if (str) {
1364 
1365 		int len = mb->cmd.length;
1366 
1367 		memcpy(str, mb->data, len);
1368 		str[len] = '\0';
1369 
1370 	}
1371 
1372 	return err;
1373 }
1374 /*===========================================================================
1375  * Set Out-Bound Authentication.
1376 */
ppp_set_outbnd_auth(sdla_t * card,ppp_private_area_t * ppp_priv_area)1377 static int ppp_set_outbnd_auth (sdla_t *card, ppp_private_area_t *ppp_priv_area)
1378 {
1379 	ppp_mbox_t *mb = card->mbox;
1380 	int err;
1381 
1382 	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1383 	memset(&mb->data, 0, (strlen(ppp_priv_area->userid) +
1384 					strlen(ppp_priv_area->passwd) + 2 ) );
1385 	memcpy(mb->data, ppp_priv_area->userid, strlen(ppp_priv_area->userid));
1386 	memcpy((mb->data + strlen(ppp_priv_area->userid) + 1),
1387 		ppp_priv_area->passwd, strlen(ppp_priv_area->passwd));
1388 
1389 	mb->cmd.length  = strlen(ppp_priv_area->userid) +
1390 					strlen(ppp_priv_area->passwd) + 2 ;
1391 
1392 	mb->cmd.command = PPP_SET_OUTBOUND_AUTH;
1393 
1394 	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1395 
1396 	if (err != CMD_OK)
1397 		ppp_error(card, err, mb);
1398 
1399 	return err;
1400 }
1401 
1402 /*===========================================================================
1403  * Set In-Bound Authentication.
1404 */
ppp_set_inbnd_auth(sdla_t * card,ppp_private_area_t * ppp_priv_area)1405 static int ppp_set_inbnd_auth (sdla_t *card, ppp_private_area_t *ppp_priv_area)
1406 {
1407 	ppp_mbox_t *mb = card->mbox;
1408 	int err, i;
1409 	char* user_tokens[32];
1410 	char* pass_tokens[32];
1411 	int userids, passwds;
1412 	int add_ptr;
1413 
1414 	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1415 	memset(&mb->data, 0, 1008);
1416 	memcpy(mb->data, ppp_priv_area->sysname,
1417 						strlen(ppp_priv_area->sysname));
1418 
1419 	/* Parse the userid string and the password string and build a string
1420 	   to copy it to the data area of the command structure.   The string
1421 	   will look like "SYS_NAME<NULL>USER1<NULL>PASS1<NULL>USER2<NULL>PASS2
1422 	   ....<NULL> "
1423 	 */
1424 	userids = tokenize( ppp_priv_area->userid, user_tokens);
1425 	passwds = tokenize( ppp_priv_area->passwd, pass_tokens);
1426 
1427 	if (userids != passwds){
1428 		printk(KERN_INFO "%s: Number of passwords does not equal the number of user ids\n", card->devname);
1429 		return 1;
1430 	}
1431 
1432 	add_ptr = strlen(ppp_priv_area->sysname) + 1;
1433 	for (i=0; i<userids; i++){
1434 		memcpy((mb->data + add_ptr), user_tokens[i],
1435 							strlen(user_tokens[i]));
1436 		memcpy((mb->data + add_ptr + strlen(user_tokens[i]) + 1),
1437 					pass_tokens[i], strlen(pass_tokens[i]));
1438 		add_ptr = add_ptr + strlen(user_tokens[i]) + 1 +
1439 						strlen(pass_tokens[i]) + 1;
1440 	}
1441 
1442 	mb->cmd.length  = add_ptr + 1;
1443 	mb->cmd.command = PPP_SET_INBOUND_AUTH;
1444 
1445 	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1446 
1447 	if (err != CMD_OK)
1448 		ppp_error(card, err, mb);
1449 
1450 	return err;
1451 }
1452 
1453 
1454 /*============================================================================
1455  * Tokenize string.
1456  *      Parse a string of the following syntax:
1457  *              <arg1>,<arg2>,...
1458  *      and fill array of tokens with pointers to string elements.
1459  *
1460  */
tokenize(char * str,char ** tokens)1461 static int tokenize (char *str, char **tokens)
1462 {
1463         int cnt = 0;
1464 
1465         tokens[0] = strtok(str, "/");
1466         while (tokens[cnt] && (cnt < 32 - 1))
1467         {
1468                 tokens[cnt] = strstrip(tokens[cnt], " \t");
1469                 tokens[++cnt] = strtok(NULL, "/");
1470         }
1471 	return cnt;
1472 }
1473 
1474 /*============================================================================
1475  * Strip leading and trailing spaces off the string str.
1476  */
strstrip(char * str,char * s)1477 static char* strstrip (char *str, char* s)
1478 {
1479         char *eos = str + strlen(str);          /* -> end of string */
1480 
1481         while (*str && strchr(s, *str))
1482                 ++str                           /* strip leading spaces */
1483         ;
1484         while ((eos > str) && strchr(s, *(eos - 1)))
1485                 --eos                           /* strip trailing spaces */
1486         ;
1487         *eos = '\0';
1488         return str;
1489 }
1490 /*============================================================================
1491  * Configure PPP firmware.
1492  */
ppp_configure(sdla_t * card,void * data)1493 static int ppp_configure(sdla_t *card, void *data)
1494 {
1495 	ppp_mbox_t *mb = card->mbox;
1496 	int data_len = sizeof(ppp508_conf_t);
1497 	int err;
1498 
1499 	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1500 	memcpy(mb->data, data, data_len);
1501 	mb->cmd.length  = data_len;
1502 	mb->cmd.command = PPP_SET_CONFIG;
1503 	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1504 
1505 	if (err != CMD_OK)
1506 		ppp_error(card, err, mb);
1507 
1508 	return err;
1509 }
1510 
1511 /*============================================================================
1512  * Set interrupt mode.
1513  */
ppp_set_intr_mode(sdla_t * card,unsigned char mode)1514 static int ppp_set_intr_mode(sdla_t *card, unsigned char mode)
1515 {
1516 	ppp_mbox_t *mb = card->mbox;
1517         ppp_intr_info_t *ppp_intr_data = (ppp_intr_info_t *) &mb->data[0];
1518 	int err;
1519 
1520 	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1521 	ppp_intr_data->i_enable = mode;
1522 
1523 	ppp_intr_data->irq = card->hw.irq;
1524 	mb->cmd.length = 2;
1525 
1526        /* If timer has been enabled, set the timer delay to 1sec */
1527        if (mode & 0x80){
1528        		ppp_intr_data->timer_len = 250; //5;//100; //250;
1529                 mb->cmd.length = 4;
1530         }
1531 
1532 	mb->cmd.command = PPP_SET_INTR_FLAGS;
1533 	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1534 
1535 	if (err != CMD_OK)
1536 		ppp_error(card, err, mb);
1537 
1538 
1539 	return err;
1540 }
1541 
1542 /*============================================================================
1543  * Enable communications.
1544  */
ppp_comm_enable(sdla_t * card)1545 static int ppp_comm_enable(sdla_t *card)
1546 {
1547 	ppp_mbox_t *mb = card->mbox;
1548 	int err;
1549 
1550 	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1551 	mb->cmd.command = PPP_COMM_ENABLE;
1552 	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1553 
1554 	if (err != CMD_OK)
1555 		ppp_error(card, err, mb);
1556 	else
1557 		card->u.p.comm_enabled = 1;
1558 
1559 	return err;
1560 }
1561 
1562 /*============================================================================
1563  * Disable communications.
1564  */
ppp_comm_disable(sdla_t * card)1565 static int ppp_comm_disable(sdla_t *card)
1566 {
1567 	ppp_mbox_t *mb = card->mbox;
1568 	int err;
1569 
1570 	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1571 	mb->cmd.command = PPP_COMM_DISABLE;
1572 	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1573 	if (err != CMD_OK)
1574 		ppp_error(card, err, mb);
1575 	else
1576 		card->u.p.comm_enabled = 0;
1577 
1578 	return err;
1579 }
1580 
ppp_comm_disable_shutdown(sdla_t * card)1581 static int ppp_comm_disable_shutdown(sdla_t *card)
1582 {
1583 	ppp_mbox_t *mb = card->mbox;
1584 	ppp_intr_info_t *ppp_intr_data;
1585 	int err;
1586 
1587 	if (!mb){
1588 		return 1;
1589 	}
1590 
1591 	ppp_intr_data = (ppp_intr_info_t *) &mb->data[0];
1592 
1593 	/* Disable all interrupts */
1594 	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1595 	ppp_intr_data->i_enable = 0;
1596 
1597 	ppp_intr_data->irq = card->hw.irq;
1598 	mb->cmd.length = 2;
1599 
1600 	mb->cmd.command = PPP_SET_INTR_FLAGS;
1601 	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1602 
1603 	/* Disable communicatinons */
1604 	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1605 	mb->cmd.command = PPP_COMM_DISABLE;
1606 	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1607 
1608 	card->u.p.comm_enabled = 0;
1609 
1610 	return 0;
1611 }
1612 
1613 
1614 
1615 /*============================================================================
1616  * Get communications error statistics.
1617  */
ppp_get_err_stats(sdla_t * card)1618 static int ppp_get_err_stats(sdla_t *card)
1619 {
1620 	ppp_mbox_t *mb = card->mbox;
1621 	int err;
1622 
1623 	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
1624 	mb->cmd.command = PPP_READ_ERROR_STATS;
1625 	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
1626 
1627 	if (err == CMD_OK) {
1628 
1629 		ppp_err_stats_t* stats = (void*)mb->data;
1630 		card->wandev.stats.rx_over_errors    = stats->rx_overrun;
1631 		card->wandev.stats.rx_crc_errors     = stats->rx_bad_crc;
1632 		card->wandev.stats.rx_missed_errors  = stats->rx_abort;
1633 		card->wandev.stats.rx_length_errors  = stats->rx_lost;
1634 		card->wandev.stats.tx_aborted_errors = stats->tx_abort;
1635 
1636 	} else
1637 		ppp_error(card, err, mb);
1638 
1639 	return err;
1640 }
1641 
1642 /*============================================================================
1643  * Send packet.
1644  *	Return:	0 - o.k.
1645  *		1 - no transmit buffers available
1646  */
ppp_send(sdla_t * card,void * data,unsigned len,unsigned proto)1647 static int ppp_send (sdla_t *card, void *data, unsigned len, unsigned proto)
1648 {
1649 	ppp_buf_ctl_t *txbuf = card->u.p.txbuf;
1650 
1651 	if (txbuf->flag)
1652                 return 1;
1653 
1654 	sdla_poke(&card->hw, txbuf->buf.ptr, data, len);
1655 
1656 	txbuf->length = len;		/* frame length */
1657 
1658 	if (proto == htons(ETH_P_IPX))
1659 		txbuf->proto = 0x01;	/* protocol ID */
1660 	else
1661 		txbuf->proto = 0x00;	/* protocol ID */
1662 
1663 	txbuf->flag = 1;		/* start transmission */
1664 
1665 	/* Update transmit buffer control fields */
1666 	card->u.p.txbuf = ++txbuf;
1667 
1668 	if ((void*)txbuf > card->u.p.txbuf_last)
1669 		card->u.p.txbuf = card->u.p.txbuf_base;
1670 
1671 	return 0;
1672 }
1673 
1674 /****** Firmware Error Handler **********************************************/
1675 
1676 /*============================================================================
1677  * Firmware error handler.
1678  *	This routine is called whenever firmware command returns non-zero
1679  *	return code.
1680  *
1681  * Return zero if previous command has to be cancelled.
1682  */
ppp_error(sdla_t * card,int err,ppp_mbox_t * mb)1683 static int ppp_error(sdla_t *card, int err, ppp_mbox_t *mb)
1684 {
1685 	unsigned cmd = mb->cmd.command;
1686 
1687 	switch (err) {
1688 
1689 		case CMD_TIMEOUT:
1690 			printk(KERN_ERR "%s: command 0x%02X timed out!\n",
1691 				card->devname, cmd);
1692 			break;
1693 
1694 		default:
1695 			printk(KERN_INFO "%s: command 0x%02X returned 0x%02X!\n"
1696 				, card->devname, cmd, err);
1697 	}
1698 
1699 	return 0;
1700 }
1701 
1702 /****** Interrupt Handlers **************************************************/
1703 
1704 /*============================================================================
1705  * PPP interrupt service routine.
1706  */
wpp_isr(sdla_t * card)1707 static void wpp_isr (sdla_t *card)
1708 {
1709 	ppp_flags_t *flags = card->flags;
1710 	char *ptr = &flags->iflag;
1711 	netdevice_t *dev = card->wandev.dev;
1712 	int i;
1713 
1714 	card->in_isr = 1;
1715 	++card->statistics.isr_entry;
1716 
1717 	if (!dev && flags->iflag != PPP_INTR_CMD){
1718 		card->in_isr = 0;
1719 		flags->iflag = 0;
1720 		return;
1721 	}
1722 
1723 	if (test_bit(PERI_CRIT, (void*)&card->wandev.critical)) {
1724 		card->in_isr = 0;
1725 		flags->iflag = 0;
1726 		return;
1727 	}
1728 
1729 
1730 	if(card->hw.type != SDLA_S514){
1731 		if (test_bit(SEND_CRIT, (void*)&card->wandev.critical)) {
1732 			++card->statistics.isr_already_critical;
1733 			printk (KERN_INFO "%s: Critical while in ISR!\n",
1734 					card->devname);
1735 			card->in_isr = 0;
1736 			flags->iflag = 0;
1737 			return;
1738 		}
1739 	}
1740 
1741 	switch (flags->iflag) {
1742 
1743 		case PPP_INTR_RXRDY:	/* receive interrupt  0x01  (bit 0)*/
1744 			++card->statistics.isr_rx;
1745 			rx_intr(card);
1746 			break;
1747 
1748 		case PPP_INTR_TXRDY:	/* transmit interrupt  0x02 (bit 1)*/
1749 			++card->statistics.isr_tx;
1750 			flags->imask &= ~PPP_INTR_TXRDY;
1751 			wake_net_dev(dev);
1752 			break;
1753 
1754 		case PPP_INTR_CMD:      /* interface command completed */
1755 			++Intr_test_counter;
1756 			++card->statistics.isr_intr_test;
1757 			break;
1758 
1759 		case PPP_INTR_MODEM:    /* modem status change (DCD, CTS) 0x04 (bit 2)*/
1760 		case PPP_INTR_DISC:  	/* Data link disconnected 0x10  (bit 4)*/
1761 		case PPP_INTR_OPEN:   	/* Data link open 0x20  (bit 5)*/
1762 		case PPP_INTR_DROP_DTR:	/* DTR drop timeout expired  0x40 bit 6 */
1763 			event_intr(card);
1764 			break;
1765 
1766 		case PPP_INTR_TIMER:
1767 			timer_intr(card);
1768 			break;
1769 
1770 		default:	/* unexpected interrupt */
1771 			++card->statistics.isr_spurious;
1772 			printk(KERN_INFO "%s: spurious interrupt 0x%02X!\n",
1773 				card->devname, flags->iflag);
1774 			printk(KERN_INFO "%s: ID Bytes = ",card->devname);
1775 	 		for(i = 0; i < 8; i ++)
1776 				printk(KERN_INFO "0x%02X ", *(ptr + 0x28 + i));
1777 			printk(KERN_INFO "\n");
1778 	}
1779 
1780 	card->in_isr = 0;
1781 	flags->iflag = 0;
1782 	return;
1783 }
1784 
1785 /*============================================================================
1786  * Receive interrupt handler.
1787  */
rx_intr(sdla_t * card)1788 static void rx_intr(sdla_t *card)
1789 {
1790 	ppp_buf_ctl_t *rxbuf = card->rxmb;
1791 	netdevice_t *dev = card->wandev.dev;
1792 	ppp_private_area_t *ppp_priv_area;
1793 	struct sk_buff *skb;
1794 	unsigned len;
1795 	void *buf;
1796 	int i;
1797         ppp_flags_t *flags = card->flags;
1798         char *ptr = &flags->iflag;
1799 	int udp_type;
1800 
1801 
1802 	if (rxbuf->flag != 0x01) {
1803 
1804 		printk(KERN_INFO
1805 			"%s: corrupted Rx buffer @ 0x%X, flag = 0x%02X!\n",
1806 			card->devname, (unsigned)rxbuf, rxbuf->flag);
1807 
1808 		printk(KERN_INFO "%s: ID Bytes = ",card->devname);
1809 
1810 		for(i = 0; i < 8; i ++)
1811 			printk(KERN_INFO "0x%02X ", *(ptr + 0x28 + i));
1812 		printk(KERN_INFO "\n");
1813 
1814 		++card->statistics.rx_intr_corrupt_rx_bfr;
1815 
1816 
1817 		/* Bug Fix: Mar 6 2000
1818                  * If we get a corrupted mailbox, it means that driver
1819                  * is out of sync with the firmware. There is no recovery.
1820                  * If we don't turn off all interrupts for this card
1821                  * the machine will crash.
1822                  */
1823 		printk(KERN_INFO "%s: Critical router failure ...!!!\n", card->devname);
1824 		printk(KERN_INFO "Please contact Sangoma Technologies !\n");
1825 		ppp_set_intr_mode(card,0);
1826 		return;
1827 	}
1828 
1829 	if (dev && is_dev_running(dev) && dev->priv){
1830 
1831 		len  = rxbuf->length;
1832 		ppp_priv_area = dev->priv;
1833 
1834 		/* Allocate socket buffer */
1835 		skb = dev_alloc_skb(len);
1836 
1837 		if (skb != NULL) {
1838 
1839 			/* Copy data to the socket buffer */
1840 			unsigned addr = rxbuf->buf.ptr;
1841 
1842 			if ((addr + len) > card->u.p.rx_top + 1) {
1843 
1844 				unsigned tmp = card->u.p.rx_top - addr + 1;
1845 				buf = skb_put(skb, tmp);
1846 				sdla_peek(&card->hw, addr, buf, tmp);
1847 				addr = card->u.p.rx_base;
1848 				len -= tmp;
1849 			}
1850 			buf = skb_put(skb, len);
1851 			sdla_peek(&card->hw, addr, buf, len);
1852 
1853 			/* Decapsulate packet */
1854         		switch (rxbuf->proto) {
1855 
1856 				case 0x00:
1857 					skb->protocol = htons(ETH_P_IP);
1858 					break;
1859 
1860 				case 0x01:
1861 					skb->protocol = htons(ETH_P_IPX);
1862 					break;
1863 			}
1864 
1865 			udp_type = udp_pkt_type( skb, card );
1866 
1867 			if (udp_type == UDP_PTPIPE_TYPE){
1868 
1869 				/* Handle a UDP Request in Timer Interrupt */
1870 				if(store_udp_mgmt_pkt(UDP_PKT_FRM_NETWORK, card, skb, dev,
1871                 	              			ppp_priv_area)){
1872 	               			flags->imask |= PPP_INTR_TIMER;
1873 				}
1874 				++ppp_priv_area->rx_intr_stat.rx_intr_PIPE_request;
1875 
1876 
1877 			} else if (handle_IPXWAN(skb->data,card->devname,
1878 						 ppp_priv_area->enable_IPX,
1879 						 ppp_priv_area->network_number,
1880 						 skb->protocol)) {
1881 
1882 				/* Handle an IPXWAN packet */
1883 				if( ppp_priv_area->enable_IPX) {
1884 
1885 					/* Make sure we are not already sending */
1886 					if (!test_bit(SEND_CRIT, &card->wandev.critical)){
1887 					 	ppp_send(card, skb->data, skb->len, htons(ETH_P_IPX));
1888 					}
1889 					wan_dev_kfree_skb(skb,FREE_READ);
1890 
1891 				} else {
1892 					++card->wandev.stats.rx_dropped;
1893 				}
1894 			} else {
1895 				/* Pass data up the protocol stack */
1896 	    			skb->dev = dev;
1897 				skb->mac.raw  = skb->data;
1898 
1899 			    	++card->wandev.stats.rx_packets;
1900 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1901 				card->wandev.stats.rx_bytes += skb->len;
1902 #endif
1903 		    		++ppp_priv_area->rx_intr_stat.rx_intr_bfr_passed_to_stack;
1904 				netif_rx(skb);
1905 			}
1906 
1907 		} else {
1908 
1909 			if (net_ratelimit()){
1910 				printk(KERN_INFO "%s: no socket buffers available!\n",
1911 					card->devname);
1912 			}
1913 			++card->wandev.stats.rx_dropped;
1914 			++ppp_priv_area->rx_intr_stat.rx_intr_no_socket;
1915 		}
1916 
1917 	} else {
1918 		++card->statistics.rx_intr_dev_not_started;
1919 	}
1920 
1921 	/* Release buffer element and calculate a pointer to the next one */
1922 	rxbuf->flag = 0x00;
1923 	card->rxmb = ++rxbuf;
1924 	if ((void*)rxbuf > card->u.p.rxbuf_last)
1925 		card->rxmb = card->u.p.rxbuf_base;
1926 }
1927 
1928 
event_intr(sdla_t * card)1929 void event_intr (sdla_t *card)
1930 {
1931 
1932  	netdevice_t* dev = card->wandev.dev;
1933         ppp_private_area_t* ppp_priv_area = dev->priv;
1934 	volatile ppp_flags_t *flags = card->flags;
1935 
1936 	switch (flags->iflag){
1937 
1938 		case PPP_INTR_MODEM:    /* modem status change (DCD, CTS) 0x04  (bit 2)*/
1939 
1940 			if (net_ratelimit()){
1941 				printk (KERN_INFO "%s: Modem status: DCD=%s CTS=%s\n",
1942 					card->devname, DCD(flags->mstatus), CTS(flags->mstatus));
1943 			}
1944 			break;
1945 
1946 		case PPP_INTR_DISC:  	/* Data link disconnected 0x10  (bit 4)*/
1947 
1948 			NEX_PRINTK (KERN_INFO "Data link disconnected intr Cause %X\n",
1949 					       flags->disc_cause);
1950 
1951 			if (flags->disc_cause &
1952 				(PPP_LOCAL_TERMINATION | PPP_DCD_CTS_DROP |
1953 				PPP_REMOTE_TERMINATION)) {
1954 
1955 				if (card->u.p.ip_mode == WANOPT_PPP_PEER) {
1956 					set_bit(0,&Read_connection_info);
1957 				}
1958 				wanpipe_set_state(card, WAN_DISCONNECTED);
1959 
1960 				show_disc_cause(card, flags->disc_cause);
1961 				ppp_priv_area->timer_int_enabled |= TMR_INT_ENABLED_PPP_EVENT;
1962 				flags->imask |= PPP_INTR_TIMER;
1963 				trigger_ppp_poll(dev);
1964 			}
1965 			break;
1966 
1967 		case PPP_INTR_OPEN:   	/* Data link open 0x20  (bit 5)*/
1968 
1969 			NEX_PRINTK (KERN_INFO "%s: PPP Link Open, LCP=%s IP=%s\n",
1970 					card->devname,LCP(flags->lcp_state),
1971 					IP(flags->ip_state));
1972 
1973 			if (flags->lcp_state == 0x09 &&
1974                            (flags->ip_state == 0x09 || flags->ipx_state == 0x09)){
1975 
1976                                 /* Initialize the polling timer and set the state
1977                                  * to WAN_CONNNECTED */
1978 
1979 
1980 				/* BUG FIX: When the protocol restarts, during heavy
1981                                  * traffic, board tx buffers and driver tx buffers
1982                                  * can go out of sync.  This checks the condition
1983                                  * and if the tx buffers are out of sync, the
1984                                  * protocols are restarted.
1985                                  * I don't know why the board tx buffer is out
1986                                  * of sync. It could be that a packets is tx
1987                                  * while the link is down, but that is not
1988                                  * possible. The other possiblility is that the
1989                                  * firmware doesn't reinitialize properly.
1990                                  * FIXME: A better fix should be found.
1991                                  */
1992 				if (detect_and_fix_tx_bug(card)){
1993 
1994 					ppp_comm_disable(card);
1995 
1996 					wanpipe_set_state(card, WAN_DISCONNECTED);
1997 
1998 					ppp_priv_area->timer_int_enabled |=
1999 						TMR_INT_ENABLED_PPP_EVENT;
2000 					flags->imask |= PPP_INTR_TIMER;
2001 					break;
2002 				}
2003 
2004 				card->state_tick = jiffies;
2005 				wanpipe_set_state(card, WAN_CONNECTED);
2006 
2007 				NEX_PRINTK(KERN_INFO "CON: L Tx: %lx  B Tx: %lx || L Rx %lx B Rx %lx\n",
2008 					(unsigned long)card->u.p.txbuf, *card->u.p.txbuf_next,
2009 					(unsigned long)card->rxmb, *card->u.p.rxbuf_next);
2010 
2011 				/* Tell timer interrupt that PPP event occured */
2012 				ppp_priv_area->timer_int_enabled |= TMR_INT_ENABLED_PPP_EVENT;
2013 				flags->imask |= PPP_INTR_TIMER;
2014 
2015 				/* If we are in PEER mode, we must first obtain the
2016 				 * IP information and then go into the poll routine */
2017 				if (card->u.p.ip_mode != WANOPT_PPP_PEER){
2018 					trigger_ppp_poll(dev);
2019 				}
2020 			}
2021                    	break;
2022 
2023 		case PPP_INTR_DROP_DTR:		/* DTR drop timeout expired  0x40 bit 6 */
2024 
2025 			NEX_PRINTK(KERN_INFO "DTR Drop Timeout Interrrupt \n");
2026 
2027 			if (card->u.p.ip_mode == WANOPT_PPP_PEER) {
2028 				set_bit(0,&Read_connection_info);
2029 			}
2030 
2031 			wanpipe_set_state(card, WAN_DISCONNECTED);
2032 
2033 			show_disc_cause(card, flags->disc_cause);
2034 			ppp_priv_area->timer_int_enabled |= TMR_INT_ENABLED_PPP_EVENT;
2035 			flags->imask |= PPP_INTR_TIMER;
2036 			trigger_ppp_poll(dev);
2037 			break;
2038 
2039 		default:
2040 			printk(KERN_INFO "%s: Error, Invalid PPP Event\n",card->devname);
2041 	}
2042 }
2043 
2044 
2045 
2046 /* TIMER INTERRUPT */
2047 
timer_intr(sdla_t * card)2048 void timer_intr (sdla_t *card)
2049 {
2050 
2051         netdevice_t* dev = card->wandev.dev;
2052         ppp_private_area_t* ppp_priv_area = dev->priv;
2053 	ppp_flags_t *flags = card->flags;
2054 
2055 
2056 	if (ppp_priv_area->timer_int_enabled & TMR_INT_ENABLED_CONFIG){
2057 		if (!config_ppp(card)){
2058 			ppp_priv_area->timer_int_enabled &=
2059 					~TMR_INT_ENABLED_CONFIG;
2060 		}
2061 	}
2062 
2063 	/* Update statistics */
2064 	if (ppp_priv_area->timer_int_enabled & TMR_INT_ENABLED_UPDATE){
2065 		ppp_get_err_stats(card);
2066                 if(!(--ppp_priv_area->update_comms_stats)){
2067 			ppp_priv_area->timer_int_enabled &=
2068 				~TMR_INT_ENABLED_UPDATE;
2069 		}
2070 	}
2071 
2072 	/* PPIPEMON UDP request */
2073 
2074 	if (ppp_priv_area->timer_int_enabled & TMR_INT_ENABLED_UDP){
2075 		process_udp_mgmt_pkt(card,dev, ppp_priv_area);
2076 		ppp_priv_area->timer_int_enabled &= ~TMR_INT_ENABLED_UDP;
2077 	}
2078 
2079 	/* PPP Event */
2080 	if (ppp_priv_area->timer_int_enabled & TMR_INT_ENABLED_PPP_EVENT){
2081 
2082 		if (card->wandev.state == WAN_DISCONNECTED){
2083 			retrigger_comm(card);
2084 		}
2085 
2086 		/* If the state is CONNECTING, it means that communicatins were
2087 	 	 * enabled. When the remote side enables its comminication we
2088 	 	 * should get an interrupt PPP_INTR_OPEN, thus turn off polling
2089 		 */
2090 
2091 		else if (card->wandev.state == WAN_CONNECTING){
2092 			/* Turn off the timer interrupt */
2093 			ppp_priv_area->timer_int_enabled &= ~TMR_INT_ENABLED_PPP_EVENT;
2094 		}
2095 
2096 		/* If state is connected and we are in PEER mode
2097 	 	 * poll for an IP address which will be provided by remote end.
2098 	 	 */
2099 		else if ((card->wandev.state == WAN_CONNECTED &&
2100 		  	  card->u.p.ip_mode == WANOPT_PPP_PEER) &&
2101 		  	  test_bit(0,&Read_connection_info)){
2102 
2103 			card->state_tick = jiffies;
2104 			if (read_connection_info (card)){
2105 				printk(KERN_INFO "%s: Failed to read PEER IP Addresses\n",
2106 					card->devname);
2107 			}else{
2108 				clear_bit(0,&Read_connection_info);
2109 				set_bit(1,&Read_connection_info);
2110 				trigger_ppp_poll(dev);
2111 			}
2112 		}else{
2113 			//FIXME Put the comment back int
2114 			ppp_priv_area->timer_int_enabled &= ~TMR_INT_ENABLED_PPP_EVENT;
2115 		}
2116 
2117 	}/* End of PPP_EVENT */
2118 
2119 
2120 	/* Only disable the timer interrupt if there are no udp, statistic */
2121 	/* updates or events pending */
2122         if(!ppp_priv_area->timer_int_enabled) {
2123                 flags->imask &= ~PPP_INTR_TIMER;
2124         }
2125 }
2126 
2127 
handle_IPXWAN(unsigned char * sendpacket,char * devname,unsigned char enable_IPX,unsigned long network_number,unsigned short proto)2128 static int handle_IPXWAN(unsigned char *sendpacket, char *devname, unsigned char enable_IPX, unsigned long network_number, unsigned short proto)
2129 {
2130 	int i;
2131 
2132 	if( proto == htons(ETH_P_IPX) ) {
2133 		//It's an IPX packet
2134 		if(!enable_IPX) {
2135 			//Return 1 so we don't pass it up the stack.
2136 			return 1;
2137 		}
2138 	} else {
2139 		//It's not IPX so pass it up the stack.
2140 		return 0;
2141 	}
2142 
2143 	if( sendpacket[16] == 0x90 &&
2144 	    sendpacket[17] == 0x04)
2145 	{
2146 		//It's IPXWAN
2147 
2148 		if( sendpacket[2] == 0x02 &&
2149 		    sendpacket[34] == 0x00)
2150 		{
2151 			//It's a timer request packet
2152 			printk(KERN_INFO "%s: Received IPXWAN Timer Request packet\n",devname);
2153 
2154 			//Go through the routing options and answer no to every
2155 			//option except Unnumbered RIP/SAP
2156 			for(i = 41; sendpacket[i] == 0x00; i += 5)
2157 			{
2158 				//0x02 is the option for Unnumbered RIP/SAP
2159 				if( sendpacket[i + 4] != 0x02)
2160 				{
2161 					sendpacket[i + 1] = 0;
2162 				}
2163 			}
2164 
2165 			//Skip over the extended Node ID option
2166 			if( sendpacket[i] == 0x04 )
2167 			{
2168 				i += 8;
2169 			}
2170 
2171 			//We also want to turn off all header compression opt.
2172 			for(; sendpacket[i] == 0x80 ;)
2173 			{
2174 				sendpacket[i + 1] = 0;
2175 				i += (sendpacket[i + 2] << 8) + (sendpacket[i + 3]) + 4;
2176 			}
2177 
2178 			//Set the packet type to timer response
2179 			sendpacket[34] = 0x01;
2180 
2181 			printk(KERN_INFO "%s: Sending IPXWAN Timer Response\n",devname);
2182 		}
2183 		else if( sendpacket[34] == 0x02 )
2184 		{
2185 			//This is an information request packet
2186 			printk(KERN_INFO "%s: Received IPXWAN Information Request packet\n",devname);
2187 
2188 			//Set the packet type to information response
2189 			sendpacket[34] = 0x03;
2190 
2191 			//Set the router name
2192 			sendpacket[51] = 'P';
2193 			sendpacket[52] = 'T';
2194 			sendpacket[53] = 'P';
2195 			sendpacket[54] = 'I';
2196 			sendpacket[55] = 'P';
2197 			sendpacket[56] = 'E';
2198 			sendpacket[57] = '-';
2199 			sendpacket[58] = CVHexToAscii(network_number >> 28);
2200 			sendpacket[59] = CVHexToAscii((network_number & 0x0F000000)>> 24);
2201 			sendpacket[60] = CVHexToAscii((network_number & 0x00F00000)>> 20);
2202 			sendpacket[61] = CVHexToAscii((network_number & 0x000F0000)>> 16);
2203 			sendpacket[62] = CVHexToAscii((network_number & 0x0000F000)>> 12);
2204 			sendpacket[63] = CVHexToAscii((network_number & 0x00000F00)>> 8);
2205 			sendpacket[64] = CVHexToAscii((network_number & 0x000000F0)>> 4);
2206 			sendpacket[65] = CVHexToAscii(network_number & 0x0000000F);
2207 			for(i = 66; i < 99; i+= 1)
2208 			{
2209 				sendpacket[i] = 0;
2210 			}
2211 
2212 			printk(KERN_INFO "%s: Sending IPXWAN Information Response packet\n",devname);
2213 		}
2214 		else
2215 		{
2216 			printk(KERN_INFO "%s: Unknown IPXWAN packet!\n",devname);
2217 			return 0;
2218 		}
2219 
2220 		//Set the WNodeID to our network address
2221 		sendpacket[35] = (unsigned char)(network_number >> 24);
2222 		sendpacket[36] = (unsigned char)((network_number & 0x00FF0000) >> 16);
2223 		sendpacket[37] = (unsigned char)((network_number & 0x0000FF00) >> 8);
2224 		sendpacket[38] = (unsigned char)(network_number & 0x000000FF);
2225 
2226 		return 1;
2227 	} else {
2228 		//If we get here's its an IPX-data packet, so it'll get passed up the stack.
2229 
2230 		//switch the network numbers
2231 		switch_net_numbers(sendpacket, network_number, 1);
2232 		return 0;
2233 	}
2234 }
2235 
2236 /****** Background Polling Routines  ****************************************/
2237 
2238 /* All polling functions are invoked by the TIMER interrupt in the wpp_isr
2239  * routine.
2240  */
2241 
2242 /*============================================================================
2243  * Monitor active link phase.
2244  */
process_route(sdla_t * card)2245 static void process_route (sdla_t *card)
2246 {
2247 	ppp_flags_t *flags = card->flags;
2248 	netdevice_t *dev = card->wandev.dev;
2249 	ppp_private_area_t *ppp_priv_area = dev->priv;
2250 
2251 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2252 
2253 	if ((card->u.p.ip_mode == WANOPT_PPP_PEER) &&
2254 	    (flags->ip_state == 0x09)){
2255 
2256 		/* We get ip_local from the firmware in PEER mode.
2257 	         * Therefore, if ip_local is 0, we failed to obtain
2258          	 * the remote IP address. */
2259 		if (ppp_priv_area->ip_local == 0)
2260 			return;
2261 
2262 		printk(KERN_INFO "%s: IPCP State Opened.\n", card->devname);
2263 		if (read_info( card )) {
2264    			printk(KERN_INFO
2265 				"%s: An error occurred in IP assignment.\n",
2266 				card->devname);
2267 		} else {
2268 			struct in_device *in_dev = dev->ip_ptr;
2269 			if (in_dev != NULL ) {
2270 				struct in_ifaddr *ifa = in_dev->ifa_list;
2271 
2272 				printk(KERN_INFO "%s: Assigned Lcl. Addr: %u.%u.%u.%u\n",
2273 					card->devname, NIPQUAD(ifa->ifa_local));
2274 				printk(KERN_INFO "%s: Assigned Rmt. Addr: %u.%u.%u.%u\n",
2275 						card->devname, NIPQUAD(ifa->ifa_address));
2276 			}else{
2277 				printk(KERN_INFO
2278 				"%s: Error: Failed to add a route for PPP interface %s\n",
2279 					card->devname,dev->name);
2280 			}
2281 		}
2282 	}
2283 #else
2284 
2285 	if ((card->u.p.ip_mode == WANOPT_PPP_PEER) &&
2286 	    (flags->ip_state == 0x09)){
2287 
2288 		if (ppp_priv_area->ip_local == 0)
2289 			return;
2290 
2291 		printk(KERN_INFO "%s: IPCP State Opened.\n", card->devname);
2292 		if (read_info( card )) {
2293 			printk(KERN_INFO
2294 				"%s: An error occurred in IP assignment.\n",
2295 				card->devname);
2296 		} else {
2297 			printk(KERN_INFO "%s: Assigned Lcl. Addr: %u.%u.%u.%u\n",
2298 					card->devname, NIPQUAD(dev->pa_addr));
2299 			printk(KERN_INFO "%s: Assigned Rmt. Addr: %u.%u.%u.%U\n",
2300 					card->devname, NIPQUAD(dev->pa_dstaddr));
2301 		}
2302 	}
2303 
2304 #endif
2305 
2306 }
2307 
2308 /*============================================================================
2309  * Monitor physical link disconnected phase.
2310  *  o if interface is up and the hold-down timeout has expired, then retry
2311  *    connection.
2312  */
retrigger_comm(sdla_t * card)2313 static void retrigger_comm(sdla_t *card)
2314 {
2315 	netdevice_t *dev = card->wandev.dev;
2316 
2317 	if (dev && ((jiffies - card->state_tick) > HOLD_DOWN_TIME)) {
2318 
2319 		wanpipe_set_state(card, WAN_CONNECTING);
2320 
2321 		if(ppp_comm_enable(card) == CMD_OK){
2322 			init_ppp_tx_rx_buff( card );
2323 		}
2324 	}
2325 }
2326 
2327 /****** Miscellaneous Functions *********************************************/
2328 
2329 /*============================================================================
2330  * Configure S508 adapter.
2331  */
config508(netdevice_t * dev,sdla_t * card)2332 static int config508(netdevice_t *dev, sdla_t *card)
2333 {
2334 	ppp508_conf_t cfg;
2335 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2336 	struct in_device *in_dev = dev->ip_ptr;
2337 #endif
2338 	ppp_private_area_t *ppp_priv_area = dev->priv;
2339 
2340 	/* Prepare PPP configuration structure */
2341 	memset(&cfg, 0, sizeof(ppp508_conf_t));
2342 
2343 	if (card->wandev.clocking)
2344 		cfg.line_speed = card->wandev.bps;
2345 
2346 	if (card->wandev.interface == WANOPT_RS232)
2347 		cfg.conf_flags |= INTERFACE_LEVEL_RS232;
2348 
2349 
2350         cfg.conf_flags 	|= DONT_TERMINATE_LNK_MAX_CONFIG; /*send Configure-Request packets forever*/
2351 	cfg.txbuf_percent	= PERCENT_TX_BUFF;	/* % of Tx bufs */
2352 	cfg.mtu_local		= card->wandev.mtu;
2353 	cfg.mtu_remote		= card->wandev.mtu;                  /*    Default   */
2354 	cfg.restart_tmr		= TIME_BETWEEN_CONF_REQ;  	     /*    30 = 3sec */
2355 	cfg.auth_rsrt_tmr	= TIME_BETWEEN_PAP_CHAP_REQ;         /*    30 = 3sec */
2356 	cfg.auth_wait_tmr	= WAIT_PAP_CHAP_WITHOUT_REPLY;       /*   300 = 30s  */
2357 	cfg.mdm_fail_tmr	= WAIT_AFTER_DCD_CTS_LOW;            /*     5 = 0.5s */
2358 	cfg.dtr_drop_tmr	= TIME_DCD_CTS_LOW_AFTER_LNK_DOWN;   /*    10 = 1s   */
2359 	cfg.connect_tmout	= WAIT_DCD_HIGH_AFTER_ENABLE_COMM;   /*   900 = 90s  */
2360 	cfg.conf_retry		= MAX_CONF_REQ_WITHOUT_REPLY;        /*    10 = 1s   */
2361 	cfg.term_retry		= MAX_TERM_REQ_WITHOUT_REPLY;	     /*     2 times  */
2362 	cfg.fail_retry		= NUM_CONF_NAK_WITHOUT_REPLY;        /*     5 times  */
2363 	cfg.auth_retry		= NUM_AUTH_REQ_WITHOUT_REPLY;        /*     10 times */
2364 
2365 
2366 	if( !card->u.p.authenticator ) {
2367 		printk(KERN_INFO "%s: Device is not configured as an authenticator\n",
2368 				card->devname);
2369 		cfg.auth_options = NO_AUTHENTICATION;
2370 	}else{
2371 		printk(KERN_INFO "%s: Device is configured as an authenticator\n",
2372 				card->devname);
2373 		cfg.auth_options = INBOUND_AUTH;
2374 	}
2375 
2376 	if( ppp_priv_area->pap == WANOPT_YES){
2377 		cfg.auth_options |=PAP_AUTH;
2378 		printk(KERN_INFO "%s: Pap enabled\n", card->devname);
2379 	}
2380 	if( ppp_priv_area->chap == WANOPT_YES){
2381 		cfg.auth_options |= CHAP_AUTH;
2382 		printk(KERN_INFO "%s: Chap enabled\n", card->devname);
2383 	}
2384 
2385 
2386 	if (ppp_priv_area->enable_IPX == WANOPT_YES){
2387 		printk(KERN_INFO "%s: Enabling IPX Protocol\n",card->devname);
2388 		cfg.ipx_options		= ENABLE_IPX | ROUTING_PROT_DEFAULT;
2389 	}else{
2390 		cfg.ipx_options 	= DISABLE_IPX;
2391 	}
2392 
2393 	switch (card->u.p.ip_mode) {
2394 
2395 		case WANOPT_PPP_STATIC:
2396 
2397 			printk(KERN_INFO "%s: PPP IP Mode: STATIC\n",card->devname);
2398 			cfg.ip_options		= L_AND_R_IP_NO_ASSIG |
2399 							    ENABLE_IP;
2400 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2401 			cfg.ip_local		= in_dev->ifa_list->ifa_local;
2402 			cfg.ip_remote		= in_dev->ifa_list->ifa_address;
2403 #else
2404 			cfg.ip_local		= dev->pa_addr;
2405 			cfg.ip_remote		= dev->pa_dstaddr;
2406 #endif
2407 
2408 			/* Debugging code used to check that IP addresses
2409                          * obtained from the kernel are correct */
2410 
2411                         NEX_PRINTK(KERN_INFO "Local %u.%u.%u.%u Remote %u.%u.%u.%u Name %s\n",
2412 					NIPQUAD(ip_local),NIPQUAD(ip_remote), dev->name);
2413 			break;
2414 
2415 		case WANOPT_PPP_HOST:
2416 
2417 			printk(KERN_INFO "%s: PPP IP Mode: HOST\n",card->devname);
2418 			cfg.ip_options		= L_IP_LOCAL_ASSIG |
2419 						  R_IP_LOCAL_ASSIG |
2420 						  ENABLE_IP;
2421 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2422 			cfg.ip_local		= in_dev->ifa_list->ifa_local;
2423 			cfg.ip_remote		= in_dev->ifa_list->ifa_address;
2424 #else
2425 			cfg.ip_local		= dev->pa_addr;
2426 			cfg.ip_remote		= dev->pa_dstaddr;
2427 #endif
2428 
2429 			/* Debugging code used to check that IP addresses
2430                          * obtained from the kernel are correct */
2431                         NEX_PRINTK (KERN_INFO "Local %u.%u.%u.%u Remote %u.%u.%u.%u Name %s\n",
2432 					NIPQUAD(ip_local),NIPQUAD(ip_remote), dev->name);
2433 
2434 			break;
2435 
2436 		case WANOPT_PPP_PEER:
2437 
2438 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2439 
2440 			printk(KERN_INFO "%s: PPP IP Mode: PEER\n",card->devname);
2441 			cfg.ip_options		= L_IP_REMOTE_ASSIG |
2442 						  R_IP_REMOTE_ASSIG |
2443 							  ENABLE_IP;
2444 			cfg.ip_local		= 0x00;
2445 			cfg.ip_remote		= 0x00;
2446 			break;
2447 
2448 #else
2449 
2450 			/* No PEER support for 2.0.X kernels, drop down to default
2451    			 * condition */
2452 
2453 			printk(KERN_INFO "%s: ERROR, PEER mode is not supported in 2.0.X kernels\n",
2454 						card->devname);
2455 
2456 #endif
2457 
2458 		default:
2459 			printk(KERN_INFO "%s: ERROR: Unsupported PPP Mode Selected\n",
2460 					card->devname);
2461 			printk(KERN_INFO "%s:        PPP IP Modes: STATIC, PEER or HOST\n",
2462 					card->devname);
2463 			return 1;
2464 	}
2465 
2466 	return ppp_configure(card, &cfg);
2467 }
2468 
2469 /*============================================================================
2470  * Show disconnection cause.
2471  */
show_disc_cause(sdla_t * card,unsigned cause)2472 static void show_disc_cause(sdla_t *card, unsigned cause)
2473 {
2474 	if (cause & 0x0802)
2475 
2476 		printk(KERN_INFO "%s: link terminated by peer\n",
2477 			card->devname);
2478 
2479 	else if (cause & 0x0004)
2480 
2481 		printk(KERN_INFO "%s: link terminated by user\n",
2482 			card->devname);
2483 
2484 	else if (cause & 0x0008)
2485 
2486 		printk(KERN_INFO "%s: authentication failed\n", card->devname);
2487 
2488 	else if (cause & 0x0010)
2489 
2490 		printk(KERN_INFO
2491 			"%s: authentication protocol negotiation failed\n",
2492 			card->devname);
2493 
2494 	else if (cause & 0x0020)
2495 
2496 		printk(KERN_INFO
2497 		"%s: peer's request for authentication rejected\n",
2498 		card->devname);
2499 
2500 	else if (cause & 0x0040)
2501 
2502 		printk(KERN_INFO "%s: MRU option rejected by peer\n",
2503 		card->devname);
2504 
2505 	else if (cause & 0x0080)
2506 
2507 		printk(KERN_INFO "%s: peer's MRU was too small\n",
2508 		card->devname);
2509 
2510 	else if (cause & 0x0100)
2511 
2512 		printk(KERN_INFO "%s: failed to negotiate peer's LCP options\n",
2513 		card->devname);
2514 
2515 	else if (cause & 0x0200)
2516 
2517 		printk(KERN_INFO "%s: failed to negotiate peer's IPCP options\n"
2518 		, card->devname);
2519 
2520 	else if (cause & 0x0400)
2521 
2522 		printk(KERN_INFO
2523 			"%s: failed to negotiate peer's IPXCP options\n",
2524 			card->devname);
2525 }
2526 
2527 /*=============================================================================
2528  * Process UDP call of type PTPIPEAB.
2529  */
process_udp_mgmt_pkt(sdla_t * card,netdevice_t * dev,ppp_private_area_t * ppp_priv_area)2530 static void process_udp_mgmt_pkt(sdla_t *card, netdevice_t *dev,
2531 				 ppp_private_area_t *ppp_priv_area )
2532 {
2533 	unsigned char buf2[5];
2534 	unsigned char *buf;
2535 	unsigned int frames, len;
2536 	struct sk_buff *new_skb;
2537 	unsigned short data_length, buffer_length, real_len;
2538 	unsigned long data_ptr;
2539 	int udp_mgmt_req_valid = 1;
2540 	ppp_mbox_t *mbox = card->mbox;
2541 	struct timeval tv;
2542 	int err;
2543 	ppp_udp_pkt_t *ppp_udp_pkt = (ppp_udp_pkt_t*)&ppp_priv_area->udp_pkt_data;
2544 
2545 	memcpy(&buf2, &card->wandev.udp_port, 2 );
2546 
2547 
2548 	if(ppp_priv_area->udp_pkt_src == UDP_PKT_FRM_NETWORK) {
2549 
2550 		switch(ppp_udp_pkt->cblock.command) {
2551 
2552 			case PPIPE_GET_IBA_DATA:
2553 			case PPP_READ_CONFIG:
2554 			case PPP_GET_CONNECTION_INFO:
2555 			case PPIPE_ROUTER_UP_TIME:
2556 			case PPP_READ_STATISTICS:
2557 			case PPP_READ_ERROR_STATS:
2558 			case PPP_READ_PACKET_STATS:
2559 			case PPP_READ_LCP_STATS:
2560 			case PPP_READ_IPCP_STATS:
2561 			case PPP_READ_IPXCP_STATS:
2562 			case PPP_READ_PAP_STATS:
2563 			case PPP_READ_CHAP_STATS:
2564 			case PPP_READ_CODE_VERSION:
2565 				udp_mgmt_req_valid = 1;
2566 				break;
2567 
2568 			default:
2569 				udp_mgmt_req_valid = 0;
2570 				break;
2571 		}
2572 	}
2573 
2574   	if(!udp_mgmt_req_valid) {
2575 
2576 		/* set length to 0 */
2577     		ppp_udp_pkt->cblock.length = 0x00;
2578 
2579     		/* set return code */
2580     		ppp_udp_pkt->cblock.result = 0xCD;
2581 		++ppp_priv_area->pipe_mgmt_stat.UDP_PIPE_mgmt_direction_err;
2582 
2583 		if (net_ratelimit()){
2584 			printk(KERN_INFO
2585 			"%s: Warning, Illegal UDP command attempted from network: %x\n",
2586 			card->devname,ppp_udp_pkt->cblock.command);
2587 		}
2588    	} else {
2589 		/* Initialize the trace element */
2590 		trace_element_t trace_element;
2591 
2592 		switch (ppp_udp_pkt->cblock.command){
2593 
2594 		/* PPIPE_ENABLE_TRACING */
2595     		case PPIPE_ENABLE_TRACING:
2596 			if (!card->TracingEnabled) {
2597 
2598 				/* OPERATE_DATALINE_MONITOR */
2599     				mbox->cmd.command = PPP_DATALINE_MONITOR;
2600     				mbox->cmd.length = 0x01;
2601     				mbox->data[0] = ppp_udp_pkt->data[0];
2602 	    			err = sdla_exec(mbox) ?
2603 					mbox->cmd.result : CMD_TIMEOUT;
2604 
2605 				if (err != CMD_OK) {
2606 
2607 					ppp_error(card, err, mbox);
2608 	        			card->TracingEnabled = 0;
2609 
2610 					/* set the return code */
2611 
2612 		        		ppp_udp_pkt->cblock.result = mbox->cmd.result;
2613 	        			mbox->cmd.length = 0;
2614 	        			break;
2615 	    			}
2616 
2617 				sdla_peek(&card->hw, 0xC000, &buf2, 2);
2618 
2619 				ppp_priv_area->curr_trace_addr = 0;
2620 		    		memcpy(&ppp_priv_area->curr_trace_addr, &buf2, 2);
2621 		    		ppp_priv_area->start_trace_addr =
2622 						ppp_priv_area->curr_trace_addr;
2623 				ppp_priv_area->end_trace_addr =
2624 					ppp_priv_area->start_trace_addr + END_OFFSET;
2625 
2626 				/* MAX_SEND_BUFFER_SIZE - 28 (IP header)
2627 				   - 32 (ppipemon CBLOCK) */
2628 		    		available_buffer_space = MAX_LGTH_UDP_MGNT_PKT -
2629 							 sizeof(ip_pkt_t)-
2630 							 sizeof(udp_pkt_t)-
2631 							 sizeof(wp_mgmt_t)-
2632 							 sizeof(cblock_t);
2633 	       	  	}
2634 	       	  	ppp_udp_pkt->cblock.result = 0;
2635 	       	  	mbox->cmd.length = 0;
2636 	       	  	card->TracingEnabled = 1;
2637 	       	  	break;
2638 
2639 		/* PPIPE_DISABLE_TRACING */
2640 		case PPIPE_DISABLE_TRACING:
2641 
2642 			if(card->TracingEnabled) {
2643 
2644 				/* OPERATE_DATALINE_MONITOR */
2645 		    		mbox->cmd.command = 0x33;
2646 		    		mbox->cmd.length = 1;
2647 		    		mbox->data[0] = 0x00;
2648 		    		err = sdla_exec(mbox) ?
2649 					mbox->cmd.result : CMD_TIMEOUT;
2650 
2651 			}
2652 
2653 			/*set return code*/
2654 			ppp_udp_pkt->cblock.result = 0;
2655 			mbox->cmd.length = 0;
2656 			card->TracingEnabled = 0;
2657 			break;
2658 
2659 		/* PPIPE_GET_TRACE_INFO */
2660 		case PPIPE_GET_TRACE_INFO:
2661 
2662 			if(!card->TracingEnabled) {
2663 				/* set return code */
2664 	    			ppp_udp_pkt->cblock.result = 1;
2665 	    			mbox->cmd.length = 0;
2666 			}
2667 
2668 			buffer_length = 0;
2669 
2670 			/* frames < 62, where 62 is the number of trace
2671 			   information elements.  There is in total 496
2672 			   bytes of space and each trace information
2673 			   element is 8 bytes.
2674 			 */
2675 			for ( frames=0; frames<62; frames++) {
2676 
2677 				trace_pkt_t *trace_pkt = (trace_pkt_t *)
2678 					&ppp_udp_pkt->data[buffer_length];
2679 
2680 				/* Read the whole trace packet */
2681 				sdla_peek(&card->hw, ppp_priv_area->curr_trace_addr,
2682 					  &trace_element, sizeof(trace_element_t));
2683 
2684 				/* no data on board so exit */
2685 				if( trace_element.opp_flag == 0x00 )
2686 					break;
2687 
2688 				data_ptr = trace_element.trace_data_ptr;
2689 
2690 				/* See if there is actual data on the trace buffer */
2691 				if (data_ptr){
2692 					data_length = trace_element.trace_length;
2693 				}else{
2694 					data_length = 0;
2695 					ppp_udp_pkt->data[0] |= 0x02;
2696 				}
2697 
2698 				//FIXME: Do we need this check
2699 				if ((available_buffer_space - buffer_length)
2700 				     < (sizeof(trace_element_t)+1)){
2701 
2702 					/*indicate we have more frames
2703 					 * on board and exit
2704 					 */
2705 					ppp_udp_pkt->data[0] |= 0x02;
2706 					break;
2707 				}
2708 
2709 				trace_pkt->status = trace_element.trace_type;
2710 				trace_pkt->time_stamp = trace_element.trace_time_stamp;
2711 				trace_pkt->real_length = trace_element.trace_length;
2712 
2713 				real_len = trace_element.trace_length;
2714 
2715 				if(data_ptr == 0){
2716 					trace_pkt->data_avail = 0x00;
2717 				}else{
2718 					/* we can take it next time */
2719 					if ((available_buffer_space - buffer_length)<
2720 						(real_len + sizeof(trace_pkt_t))){
2721 
2722 						ppp_udp_pkt->data[0] |= 0x02;
2723 						break;
2724 					}
2725 					trace_pkt->data_avail = 0x01;
2726 
2727 					/* get the data */
2728 					sdla_peek(&card->hw, data_ptr,
2729 						  &trace_pkt->data,
2730 						  real_len);
2731 				}
2732 				/* zero the opp flag to
2733 				   show we got the frame */
2734 				buf2[0] = 0x00;
2735 				sdla_poke(&card->hw, ppp_priv_area->curr_trace_addr,
2736 					  &buf2, 1);
2737 
2738 				/* now move onto the next
2739 				   frame */
2740 				ppp_priv_area->curr_trace_addr += 8;
2741 
2742 				/* check if we passed the last address */
2743 				if ( ppp_priv_area->curr_trace_addr >=
2744 					ppp_priv_area->end_trace_addr){
2745 
2746 					ppp_priv_area->curr_trace_addr =
2747 						ppp_priv_area->start_trace_addr;
2748 				}
2749 
2750 				/* update buffer length and make sure its even */
2751 
2752 				if ( trace_pkt->data_avail == 0x01 ) {
2753 					buffer_length += real_len - 1;
2754 				}
2755 
2756 				/* for the header */
2757 				buffer_length += 8;
2758 
2759 				if( buffer_length & 0x0001 )
2760 					buffer_length += 1;
2761 			}
2762 
2763 			/* ok now set the total number of frames passed
2764 			   in the high 5 bits */
2765 			ppp_udp_pkt->data[0] |= (frames << 2);
2766 
2767 			/* set the data length */
2768 			mbox->cmd.length = buffer_length;
2769 			ppp_udp_pkt->cblock.length = buffer_length;
2770 
2771 			/* set return code */
2772 			ppp_udp_pkt->cblock.result = 0;
2773 	      	  	break;
2774 
2775    		/* PPIPE_GET_IBA_DATA */
2776 		case PPIPE_GET_IBA_DATA:
2777 
2778 			mbox->cmd.length = 0x09;
2779 
2780 			sdla_peek(&card->hw, 0xF003, &ppp_udp_pkt->data,
2781 					mbox->cmd.length);
2782 
2783 			/* set the length of the data */
2784 			ppp_udp_pkt->cblock.length = 0x09;
2785 
2786 			/* set return code */
2787 			ppp_udp_pkt->cblock.result = 0x00;
2788 			ppp_udp_pkt->cblock.result = 0;
2789 			break;
2790 
2791 		/* PPIPE_FT1_READ_STATUS */
2792 		case PPIPE_FT1_READ_STATUS:
2793 			sdla_peek(&card->hw, 0xF020, &ppp_udp_pkt->data[0], 2);
2794 			ppp_udp_pkt->cblock.length = mbox->cmd.length = 2;
2795 			ppp_udp_pkt->cblock.result = 0;
2796 			break;
2797 
2798 		case PPIPE_FLUSH_DRIVER_STATS:
2799 			init_ppp_priv_struct( ppp_priv_area );
2800 			init_global_statistics( card );
2801 			mbox->cmd.length = 0;
2802 			ppp_udp_pkt->cblock.result = 0;
2803 			break;
2804 
2805 
2806 		case PPIPE_ROUTER_UP_TIME:
2807 
2808 			do_gettimeofday( &tv );
2809 			ppp_priv_area->router_up_time = tv.tv_sec -
2810 					ppp_priv_area->router_start_time;
2811 			*(unsigned long *)&ppp_udp_pkt->data = ppp_priv_area->router_up_time;
2812 			mbox->cmd.length = 4;
2813 			ppp_udp_pkt->cblock.result = 0;
2814 			break;
2815 
2816 				/* PPIPE_DRIVER_STATISTICS */
2817 		case PPIPE_DRIVER_STAT_IFSEND:
2818 			memcpy(&ppp_udp_pkt->data, &ppp_priv_area->if_send_stat,
2819 				sizeof(if_send_stat_t));
2820 
2821 
2822 			ppp_udp_pkt->cblock.result = 0;
2823 			ppp_udp_pkt->cblock.length = sizeof(if_send_stat_t);
2824 			mbox->cmd.length = sizeof(if_send_stat_t);
2825 			break;
2826 
2827 		case PPIPE_DRIVER_STAT_INTR:
2828 			memcpy(&ppp_udp_pkt->data, &card->statistics,
2829 				sizeof(global_stats_t));
2830 
2831 			memcpy(&ppp_udp_pkt->data+sizeof(global_stats_t),
2832 				&ppp_priv_area->rx_intr_stat,
2833 				sizeof(rx_intr_stat_t));
2834 
2835 			ppp_udp_pkt->cblock.result = 0;
2836 			ppp_udp_pkt->cblock.length = sizeof(global_stats_t)+
2837 						     sizeof(rx_intr_stat_t);
2838 			mbox->cmd.length = ppp_udp_pkt->cblock.length;
2839 			break;
2840 
2841 		case PPIPE_DRIVER_STAT_GEN:
2842 			memcpy( &ppp_udp_pkt->data,
2843 				&ppp_priv_area->pipe_mgmt_stat,
2844 				sizeof(pipe_mgmt_stat_t));
2845 
2846 			memcpy(&ppp_udp_pkt->data+sizeof(pipe_mgmt_stat_t),
2847 			       &card->statistics, sizeof(global_stats_t));
2848 
2849 			ppp_udp_pkt->cblock.result = 0;
2850 			ppp_udp_pkt->cblock.length = sizeof(global_stats_t)+
2851 						     sizeof(rx_intr_stat_t);
2852 			mbox->cmd.length = ppp_udp_pkt->cblock.length;
2853 			break;
2854 
2855 
2856 		/* FT1 MONITOR STATUS */
2857    		case FT1_MONITOR_STATUS_CTRL:
2858 
2859 			/* Enable FT1 MONITOR STATUS */
2860 	        	if( ppp_udp_pkt->data[0] == 1) {
2861 
2862 				if( rCount++ != 0 ) {
2863 		        		ppp_udp_pkt->cblock.result = 0;
2864 	          			mbox->cmd.length = 1;
2865 		  			break;
2866 		    		}
2867 	      		}
2868 
2869 	      		/* Disable FT1 MONITOR STATUS */
2870 	      		if( ppp_udp_pkt->data[0] == 0) {
2871 
2872 	      	   		if( --rCount != 0) {
2873 		  			ppp_udp_pkt->cblock.result = 0;
2874 		  			mbox->cmd.length = 1;
2875 		  			break;
2876 	   	    		}
2877 	      		}
2878 			goto udp_dflt_cmd;
2879 
2880 		/* WARNING: FIXME: This should be fixed.
2881 		 * The FT1 Status Ctrl doesn't have a break
2882                  * statment.  Thus, no code must be inserted
2883                  * HERE: between default and above case statement */
2884 
2885 		default:
2886 udp_dflt_cmd:
2887 
2888 			/* it's a board command */
2889 			mbox->cmd.command = ppp_udp_pkt->cblock.command;
2890 			mbox->cmd.length = ppp_udp_pkt->cblock.length;
2891 
2892 			if(mbox->cmd.length) {
2893 				memcpy(&mbox->data,(unsigned char *)ppp_udp_pkt->data,
2894 				       mbox->cmd.length);
2895 	      		}
2896 
2897 			/* run the command on the board */
2898 			err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2899 
2900 			if (err != CMD_OK) {
2901 
2902 		    		ppp_error(card, err, mbox);
2903 		    		++ppp_priv_area->pipe_mgmt_stat.
2904 					 UDP_PIPE_mgmt_adptr_cmnd_timeout;
2905 				break;
2906 			}
2907 
2908 		  	++ppp_priv_area->pipe_mgmt_stat.UDP_PIPE_mgmt_adptr_cmnd_OK;
2909 
2910 			/* copy the result back to our buffer */
2911 			memcpy(&ppp_udp_pkt->cblock,mbox, sizeof(cblock_t));
2912 
2913 			if(mbox->cmd.length) {
2914 				memcpy(&ppp_udp_pkt->data,&mbox->data,mbox->cmd.length);
2915 			}
2916 
2917 		} /* end of switch */
2918      	} /* end of else */
2919 
2920      	/* Fill UDP TTL */
2921      	ppp_udp_pkt->ip_pkt.ttl = card->wandev.ttl;
2922      	len = reply_udp(ppp_priv_area->udp_pkt_data, mbox->cmd.length);
2923 
2924      	if (ppp_priv_area->udp_pkt_src == UDP_PKT_FRM_NETWORK) {
2925 
2926 		/* Make sure we are not already sending */
2927 		if (!test_bit(SEND_CRIT,&card->wandev.critical)){
2928 			++ppp_priv_area->pipe_mgmt_stat.UDP_PIPE_mgmt_passed_to_adptr;
2929 			ppp_send(card,ppp_priv_area->udp_pkt_data,len,ppp_priv_area->protocol);
2930 		}
2931 
2932 	} else {
2933 
2934 		/* Pass it up the stack
2935     		   Allocate socket buffer */
2936 		if ((new_skb = dev_alloc_skb(len)) != NULL) {
2937 
2938 			/* copy data into new_skb */
2939 
2940   	    		buf = skb_put(new_skb, len);
2941   	    		memcpy(buf,ppp_priv_area->udp_pkt_data, len);
2942 
2943 	    		++ppp_priv_area->pipe_mgmt_stat.UDP_PIPE_mgmt_passed_to_stack;
2944 
2945             		/* Decapsulate packet and pass it up the protocol
2946 			   stack */
2947 	    		new_skb->protocol = htons(ETH_P_IP);
2948             		new_skb->dev = dev;
2949 	    		new_skb->mac.raw  = new_skb->data;
2950 			netif_rx(new_skb);
2951 
2952 		} else {
2953 
2954 			++ppp_priv_area->pipe_mgmt_stat.UDP_PIPE_mgmt_no_socket;
2955 			printk(KERN_INFO "no socket buffers available!\n");
2956   		}
2957     	}
2958 
2959 	ppp_priv_area->udp_pkt_lgth = 0;
2960 
2961 	return;
2962 }
2963 
2964 /*=============================================================================
2965  * Initial the ppp_private_area structure.
2966  */
init_ppp_priv_struct(ppp_private_area_t * ppp_priv_area)2967 static void init_ppp_priv_struct( ppp_private_area_t *ppp_priv_area )
2968 {
2969 
2970 	memset(&ppp_priv_area->if_send_stat, 0, sizeof(if_send_stat_t));
2971 	memset(&ppp_priv_area->rx_intr_stat, 0, sizeof(rx_intr_stat_t));
2972 	memset(&ppp_priv_area->pipe_mgmt_stat, 0, sizeof(pipe_mgmt_stat_t));
2973 }
2974 
2975 /*============================================================================
2976  * Initialize Global Statistics
2977  */
init_global_statistics(sdla_t * card)2978 static void init_global_statistics( sdla_t *card )
2979 {
2980 	memset(&card->statistics, 0, sizeof(global_stats_t));
2981 }
2982 
2983 /*============================================================================
2984  * Initialize Receive and Transmit Buffers.
2985  */
init_ppp_tx_rx_buff(sdla_t * card)2986 static void init_ppp_tx_rx_buff( sdla_t *card )
2987 {
2988 	ppp508_buf_info_t* info;
2989 
2990 	if (card->hw.type == SDLA_S514) {
2991 
2992 		info = (void*)(card->hw.dpmbase + PPP514_BUF_OFFS);
2993 
2994        		card->u.p.txbuf_base = (void*)(card->hw.dpmbase +
2995 			info->txb_ptr);
2996 
2997                 card->u.p.txbuf_last = (ppp_buf_ctl_t*)card->u.p.txbuf_base +
2998                         (info->txb_num - 1);
2999 
3000                 card->u.p.rxbuf_base = (void*)(card->hw.dpmbase +
3001                         info->rxb_ptr);
3002 
3003                 card->u.p.rxbuf_last = (ppp_buf_ctl_t*)card->u.p.rxbuf_base +
3004                         (info->rxb_num - 1);
3005 
3006 	} else {
3007 
3008 		info = (void*)(card->hw.dpmbase + PPP508_BUF_OFFS);
3009 
3010 		card->u.p.txbuf_base = (void*)(card->hw.dpmbase +
3011 			(info->txb_ptr - PPP508_MB_VECT));
3012 
3013 		card->u.p.txbuf_last = (ppp_buf_ctl_t*)card->u.p.txbuf_base +
3014 			(info->txb_num - 1);
3015 
3016 		card->u.p.rxbuf_base = (void*)(card->hw.dpmbase +
3017 			(info->rxb_ptr - PPP508_MB_VECT));
3018 
3019 		card->u.p.rxbuf_last = (ppp_buf_ctl_t*)card->u.p.rxbuf_base +
3020 			(info->rxb_num - 1);
3021 	}
3022 
3023 	card->u.p.txbuf_next = (unsigned long*)&info->txb_nxt;
3024 	card->u.p.rxbuf_next = (unsigned long*)&info->rxb1_ptr;
3025 
3026 	card->u.p.rx_base = info->rxb_base;
3027         card->u.p.rx_top  = info->rxb_end;
3028 
3029 	card->u.p.txbuf = card->u.p.txbuf_base;
3030 	card->rxmb = card->u.p.rxbuf_base;
3031 
3032 }
3033 
3034 /*=============================================================================
3035  * Read Connection Information (ie for Remote IP address assginment).
3036  * Called when ppp interface connected.
3037  */
read_info(sdla_t * card)3038 static int read_info( sdla_t *card )
3039 {
3040 	netdevice_t *dev = card->wandev.dev;
3041 	ppp_private_area_t *ppp_priv_area = dev->priv;
3042 	int err;
3043 
3044 #if defined(LINUX_2_1) || defined(LINUX_2_4)
3045 	struct ifreq if_info;
3046 	struct sockaddr_in *if_data1, *if_data2;
3047 	mm_segment_t fs;
3048 #else
3049 #ifdef _DYNAMIC_ROUTE_20X_SUPPORT_
3050         struct rtentry route;
3051 #endif
3052 #endif
3053 
3054 
3055 
3056 #if defined(LINUX_2_1) || defined(LINUX_2_4)
3057 
3058 	/* Set Local and remote addresses */
3059 	memset(&if_info, 0, sizeof(if_info));
3060 	strcpy(if_info.ifr_name, dev->name);
3061 
3062 
3063 	fs = get_fs();
3064 	set_fs(get_ds());     /* get user space block */
3065 
3066 	/* Change the local and remote ip address of the interface.
3067 	 * This will also add in the destination route.
3068 	 */
3069 	if_data1 = (struct sockaddr_in *)&if_info.ifr_addr;
3070 	if_data1->sin_addr.s_addr = ppp_priv_area->ip_local;
3071 	if_data1->sin_family = AF_INET;
3072 	err = devinet_ioctl( SIOCSIFADDR, &if_info );
3073 	if_data2 = (struct sockaddr_in *)&if_info.ifr_dstaddr;
3074 	if_data2->sin_addr.s_addr = ppp_priv_area->ip_remote;
3075 	if_data2->sin_family = AF_INET;
3076 	err = devinet_ioctl( SIOCSIFDSTADDR, &if_info );
3077 
3078 	set_fs(fs);           /* restore old block */
3079 
3080 #else
3081 	/* FIXME: Dynamic Routing in 2.0.X kernels is not
3082          * supported. Sorry ! I'll come back to it when I get
3083          * a chance. */
3084 
3085 	printk(KERN_INFO "%s: ERROR, Dynamic routing is not supported in 2.0.X kernels\n",
3086 				card->devname);
3087 	printk(KERN_INFO "%s:        Please use the STATIC IP mode!\n",
3088 				card->devname);
3089 
3090 	err = 0;
3091 
3092 #ifdef _DYNAMIC_ROUTE_20X_SUPPORT_
3093 	dev->pa_dstaddr = ppp_priv_area->ip_remote;
3094 	dev->pa_addr = ppp_priv_area->ip_local;
3095 
3096 	memset(&route, 0, sizeof(route));
3097 	route.rt_dev = dev->name;
3098 	route.rt_flags = 0;
3099 	((struct sockaddr_in *)&(route.rt_dst))->sin_addr.s_addr =
3100 		dev->pa_dstaddr;
3101 	((struct sockaddr_in *)&(route.rt_dst))->sin_family = AF_INET;
3102 	((struct sockaddr_in *)&(route.rt_genmask))->sin_addr.s_addr =
3103 			0xFFFFFFFF;
3104 	((struct sockaddr_in *)&(route.rt_genmask))->sin_family =
3105 			AF_INET;
3106 
3107 	err = ip_rt_new(&route);
3108 
3109 #endif
3110 
3111 #endif
3112 
3113 	if (err) {
3114 		printk (KERN_INFO "%s: Adding of route failed: %i\n",
3115 			card->devname,err);
3116 		printk (KERN_INFO "%s:	Local : %u.%u.%u.%u\n",
3117 			card->devname,NIPQUAD(ppp_priv_area->ip_local));
3118 		printk (KERN_INFO "%s:	Remote: %u.%u.%u.%u\n",
3119 			card->devname,NIPQUAD(ppp_priv_area->ip_remote));
3120 	}
3121 	return err;
3122 }
3123 
3124 /*=============================================================================
3125  * Remove Dynamic Route.
3126  * Called when ppp interface disconnected.
3127  */
3128 
remove_route(sdla_t * card)3129 static void remove_route( sdla_t *card )
3130 {
3131 
3132 	netdevice_t *dev = card->wandev.dev;
3133 	long ip_addr;
3134 	int err;
3135 
3136 #if defined(LINUX_2_1) || defined(LINUX_2_4)
3137         mm_segment_t fs;
3138 	struct ifreq if_info;
3139 	struct sockaddr_in *if_data1;
3140         struct in_device *in_dev = dev->ip_ptr;
3141         struct in_ifaddr *ifa = in_dev->ifa_list;
3142 #else
3143 	unsigned long fs = 0;
3144         struct rtentry route;
3145 #endif
3146 
3147 
3148 #if defined(LINUX_2_1) || defined(LINUX_2_4)
3149 	ip_addr = ifa->ifa_local;
3150 
3151 	/* Set Local and remote addresses */
3152 	memset(&if_info, 0, sizeof(if_info));
3153 	strcpy(if_info.ifr_name, dev->name);
3154 #endif
3155 
3156 
3157 	fs = get_fs();
3158        	set_fs(get_ds());     /* get user space block */
3159 
3160 
3161 #if defined(LINUX_2_1) || defined(LINUX_2_4)
3162 	/* Change the local ip address of the interface to 0.
3163 	 * This will also delete the destination route.
3164 	 */
3165 	if_data1 = (struct sockaddr_in *)&if_info.ifr_addr;
3166 	if_data1->sin_addr.s_addr = 0;
3167 	if_data1->sin_family = AF_INET;
3168 	err = devinet_ioctl( SIOCSIFADDR, &if_info );
3169 #else
3170 
3171 	ip_addr = dev->pa_addr;
3172 	dev->pa_dstaddr = 0;
3173 
3174 	memset(&route, 0, sizeof(route));
3175 	route.rt_dev = dev->name;
3176 	route.rt_flags = 0;
3177 	((struct sockaddr_in *)&(route.rt_dst))->sin_addr.s_addr =
3178 			dev->pa_dstaddr;
3179 	((struct sockaddr_in *)&(route.rt_dst))->sin_family = AF_INET;
3180 	((struct sockaddr_in *)&(route.rt_genmask))->sin_addr.s_addr =
3181 			0xFFFFFFFF;
3182         ((struct sockaddr_in *)&(route.rt_genmask))->sin_family =
3183 			AF_INET;
3184 
3185 
3186 	err = ip_rt_kill(&route);
3187 
3188 #endif
3189 
3190         set_fs(fs);           /* restore old block */
3191 
3192 
3193 	if (err) {
3194 		printk (KERN_INFO "%s: Deleting dynamic route failed %d!\n",
3195 			 card->devname, err);
3196 		return;
3197 	}else{
3198 		printk (KERN_INFO "%s: PPP Deleting dynamic route %u.%u.%u.%u successfuly\n",
3199 			card->devname, NIPQUAD(ip_addr));
3200 	}
3201 	return;
3202 }
3203 
3204 /*=============================================================================
3205  * Perform the Interrupt Test by running the READ_CODE_VERSION command MAX_INTR
3206  * _TEST_COUNTER times.
3207  */
intr_test(sdla_t * card)3208 static int intr_test( sdla_t *card )
3209 {
3210 	ppp_mbox_t *mb = card->mbox;
3211 	int err,i;
3212 
3213 	err = ppp_set_intr_mode( card, 0x08 );
3214 
3215 	if (err == CMD_OK) {
3216 
3217 		for (i = 0; i < MAX_INTR_TEST_COUNTER; i ++) {
3218 			/* Run command READ_CODE_VERSION */
3219 			memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
3220 			mb->cmd.length  = 0;
3221 			mb->cmd.command = PPP_READ_CODE_VERSION;
3222 			err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
3223 			if (err != CMD_OK)
3224 				ppp_error(card, err, mb);
3225 		}
3226 	}
3227 	else return err;
3228 
3229 	err = ppp_set_intr_mode( card, 0 );
3230 	if (err != CMD_OK)
3231 		return err;
3232 
3233 	return 0;
3234 }
3235 
3236 /*==============================================================================
3237  * Determine what type of UDP call it is. DRVSTATS or PTPIPEAB ?
3238  */
udp_pkt_type(struct sk_buff * skb,sdla_t * card)3239 static int udp_pkt_type( struct sk_buff *skb, sdla_t *card )
3240 {
3241 	unsigned char *sendpacket;
3242 	unsigned char buf2[5];
3243 	ppp_udp_pkt_t *ppp_udp_pkt = (ppp_udp_pkt_t *)skb->data;
3244 
3245 	sendpacket = skb->data;
3246 	memcpy(&buf2, &card->wandev.udp_port, 2);
3247 
3248 	if( 	ppp_udp_pkt->ip_pkt.ver_inet_hdr_length  == 0x45 &&        /* IP packet */
3249 		sendpacket[9]  == 0x11 &&        /* UDP packet */
3250 		sendpacket[22] == buf2[1] &&     /* UDP Port */
3251 		sendpacket[23] == buf2[0] &&
3252 		sendpacket[36] == 0x01 ) {
3253 
3254 		if (    sendpacket[28] == 0x50 &&    /* PTPIPEAB: Signature */
3255 			sendpacket[29] == 0x54 &&
3256 			sendpacket[30] == 0x50 &&
3257 			sendpacket[31] == 0x49 &&
3258 			sendpacket[32] == 0x50 &&
3259 			sendpacket[33] == 0x45 &&
3260 			sendpacket[34] == 0x41 &&
3261 			sendpacket[35] == 0x42 ){
3262 
3263 			return UDP_PTPIPE_TYPE;
3264 
3265 		} else if(sendpacket[28] == 0x44 &&  /* DRVSTATS: Signature */
3266 			sendpacket[29] == 0x52 &&
3267       			sendpacket[30] == 0x56 &&
3268       			sendpacket[31] == 0x53 &&
3269       			sendpacket[32] == 0x54 &&
3270       			sendpacket[33] == 0x41 &&
3271       			sendpacket[34] == 0x54 &&
3272       			sendpacket[35] == 0x53 ){
3273 
3274 			return UDP_DRVSTATS_TYPE;
3275 
3276 		} else
3277 			return UDP_INVALID_TYPE;
3278 
3279 	} else
3280 		return UDP_INVALID_TYPE;
3281 
3282 }
3283 
3284 /*============================================================================
3285  * Check to see if the packet to be transmitted contains a broadcast or
3286  * multicast source IP address.
3287  */
3288 
chk_bcast_mcast_addr(sdla_t * card,netdevice_t * dev,struct sk_buff * skb)3289 static int chk_bcast_mcast_addr(sdla_t *card, netdevice_t* dev,
3290 				struct sk_buff *skb)
3291 {
3292 	u32 src_ip_addr;
3293         u32 broadcast_ip_addr = 0;
3294 #if defined(LINUX_2_1) || defined(LINUX_2_4)
3295         struct in_device *in_dev;
3296 #endif
3297         /* read the IP source address from the outgoing packet */
3298         src_ip_addr = *(u32 *)(skb->data + 12);
3299 
3300 	/* read the IP broadcast address for the device */
3301 #if defined(LINUX_2_1) || defined(LINUX_2_4)
3302         in_dev = dev->ip_ptr;
3303         if(in_dev != NULL) {
3304                 struct in_ifaddr *ifa= in_dev->ifa_list;
3305                 if(ifa != NULL)
3306                         broadcast_ip_addr = ifa->ifa_broadcast;
3307                 else
3308                         return 0;
3309         }
3310 #else
3311         broadcast_ip_addr = dev->pa_brdaddr;
3312 #endif
3313 
3314         /* check if the IP Source Address is a Broadcast address */
3315         if((dev->flags & IFF_BROADCAST) && (src_ip_addr == broadcast_ip_addr)) {
3316                 printk(KERN_INFO "%s: Broadcast Source Address silently discarded\n",
3317 				card->devname);
3318                 return 1;
3319         }
3320 
3321         /* check if the IP Source Address is a Multicast address */
3322         if((ntohl(src_ip_addr) >= 0xE0000001) &&
3323 		(ntohl(src_ip_addr) <= 0xFFFFFFFE)) {
3324                 printk(KERN_INFO "%s: Multicast Source Address silently discarded\n",
3325 				card->devname);
3326                 return 1;
3327         }
3328 
3329         return 0;
3330 }
3331 
s508_lock(sdla_t * card,unsigned long * smp_flags)3332 void s508_lock (sdla_t *card, unsigned long *smp_flags)
3333 {
3334 #if defined(CONFIG_SMP) || defined(LINUX_2_4)
3335 	spin_lock_irqsave(&card->wandev.lock, *smp_flags);
3336 #else
3337 	disable_irq(card->hw.irq);
3338 #endif
3339 }
3340 
s508_unlock(sdla_t * card,unsigned long * smp_flags)3341 void s508_unlock (sdla_t *card, unsigned long *smp_flags)
3342 {
3343 #if defined(CONFIG_SMP) || defined(LINUX_2_4)
3344         spin_unlock_irqrestore(&card->wandev.lock, *smp_flags);
3345 #else
3346 	enable_irq(card->hw.irq);
3347 #endif
3348 }
3349 
read_connection_info(sdla_t * card)3350 static int read_connection_info (sdla_t *card)
3351 {
3352 	ppp_mbox_t *mb = card->mbox;
3353 	netdevice_t *dev = card->wandev.dev;
3354 	ppp_private_area_t *ppp_priv_area = dev->priv;
3355 	ppp508_connect_info_t *ppp508_connect_info;
3356 	int err;
3357 
3358 	memset(&mb->cmd, 0, sizeof(ppp_cmd_t));
3359 	mb->cmd.length  = 0;
3360 	mb->cmd.command = PPP_GET_CONNECTION_INFO;
3361 	err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
3362 
3363 	if (err != CMD_OK) {
3364 		ppp_error(card, err, mb);
3365 		ppp_priv_area->ip_remote = 0;
3366 		ppp_priv_area->ip_local = 0;
3367 	}
3368 	else {
3369 		ppp508_connect_info = (ppp508_connect_info_t *)mb->data;
3370 		ppp_priv_area->ip_remote = ppp508_connect_info->ip_remote;
3371 		ppp_priv_area->ip_local = ppp508_connect_info->ip_local;
3372 
3373 		NEX_PRINTK(KERN_INFO "READ CONNECTION GOT IP ADDRESS %x, %x\n",
3374 				ppp_priv_area->ip_remote,
3375 				ppp_priv_area->ip_local);
3376 	}
3377 
3378 	return err;
3379 }
3380 
3381 /*===============================================================================
3382  * config_ppp
3383  *
3384  *	Configure the ppp protocol and enable communications.
3385  *
3386  *   	The if_open function binds this function to the poll routine.
3387  *      Therefore, this function will run every time the ppp interface
3388  *      is brought up.
3389  *
3390  *	If the communications are not enabled, proceed to configure
3391  *      the card and enable communications.
3392  *
3393  *      If the communications are enabled, it means that the interface
3394  *      was shutdown by ether the user or driver. In this case, we
3395  *      have to check that the IP addresses have not changed.  If
3396  *      the IP addresses changed, we have to reconfigure the firmware
3397  *      and update the changed IP addresses.  Otherwise, just exit.
3398  */
config_ppp(sdla_t * card)3399 static int config_ppp (sdla_t *card)
3400 {
3401 
3402 	netdevice_t *dev = card->wandev.dev;
3403 	ppp_flags_t *flags = card->flags;
3404 	ppp_private_area_t *ppp_priv_area = dev->priv;
3405 
3406 	if (card->u.p.comm_enabled){
3407 
3408 		if (ppp_priv_area->ip_local_tmp != ppp_priv_area->ip_local ||
3409 		    ppp_priv_area->ip_remote_tmp != ppp_priv_area->ip_remote){
3410 
3411 			/* The IP addersses have changed, we must
3412                          * stop the communications and reconfigure
3413                          * the card. Reason: the firmware must know
3414                          * the local and remote IP addresses. */
3415 			disable_comm(card);
3416 			wanpipe_set_state(card, WAN_DISCONNECTED);
3417 			printk(KERN_INFO
3418 				"%s: IP addresses changed!\n",
3419 					card->devname);
3420 			printk(KERN_INFO "%s: Restarting communications ...\n",
3421 					card->devname);
3422 		}else{
3423 			/* IP addresses are the same and the link is up,
3424                          * we dont have to do anything here. Therefore, exit */
3425 			return 0;
3426 		}
3427 	}
3428 
3429 	/* Record the new IP addreses */
3430 	ppp_priv_area->ip_local = ppp_priv_area->ip_local_tmp;
3431 	ppp_priv_area->ip_remote = ppp_priv_area->ip_remote_tmp;
3432 
3433 	if (config508(dev, card)){
3434 		printk(KERN_INFO "%s: Failed to configure PPP device\n",
3435 			card->devname);
3436 		return 0;
3437 	}
3438 
3439 	if (ppp_set_intr_mode(card, PPP_INTR_RXRDY|
3440 			    		PPP_INTR_TXRDY|
3441 				    	PPP_INTR_MODEM|
3442 				    	PPP_INTR_DISC |
3443 				    	PPP_INTR_OPEN |
3444 				    	PPP_INTR_DROP_DTR |
3445 					PPP_INTR_TIMER)) {
3446 
3447 		printk(KERN_INFO "%s: Failed to configure board interrupts !\n",
3448 			card->devname);
3449 		return 0;
3450 	}
3451 
3452         /* Turn off the transmit and timer interrupt */
3453 	flags->imask &= ~(PPP_INTR_TXRDY | PPP_INTR_TIMER) ;
3454 
3455 
3456 	/* If you are not the authenticator and any one of the protocol is
3457 	 * enabled then we call the set_out_bound_authentication.
3458 	 */
3459 	if ( !card->u.p.authenticator  && (ppp_priv_area->pap || ppp_priv_area->chap)) {
3460 		if ( ppp_set_outbnd_auth(card, ppp_priv_area) ){
3461 			printk(KERN_INFO "%s: Outbound authentication failed !\n",
3462 				card->devname);
3463 			return 0;
3464 		}
3465 	}
3466 
3467 	/* If you are the authenticator and any one of the protocol is enabled
3468 	 * then we call the set_in_bound_authentication.
3469 	 */
3470 	if (card->u.p.authenticator && (ppp_priv_area->pap || ppp_priv_area->chap)){
3471 		if (ppp_set_inbnd_auth(card, ppp_priv_area)){
3472 			printk(KERN_INFO "%s: Inbound authentication failed !\n",
3473 				card->devname);
3474 			return 0;
3475 		}
3476 	}
3477 
3478 	/* If we fail to enable communications here it's OK,
3479 	 * since the DTR timer will cause a disconnected, which
3480 	 * will retrigger communication in timer_intr() */
3481 	if (ppp_comm_enable(card) == CMD_OK) {
3482 		wanpipe_set_state(card, WAN_CONNECTING);
3483 		init_ppp_tx_rx_buff(card);
3484 	}
3485 
3486 	return 0;
3487 }
3488 
3489 /*============================================================
3490  * ppp_poll
3491  *
3492  * Rationale:
3493  * 	We cannot manipulate the routing tables, or
3494  *      ip addresses withing the interrupt. Therefore
3495  *      we must perform such actons outside an interrupt
3496  *      at a later time.
3497  *
3498  * Description:
3499  *	PPP polling routine, responsible for
3500  *     	shutting down interfaces upon disconnect
3501  *     	and adding/removing routes.
3502  *
3503  * Usage:
3504  * 	This function is executed for each ppp
3505  * 	interface through a tq_schedule bottom half.
3506  *
3507  *      trigger_ppp_poll() function is used to kick
3508  *      the ppp_poll routine.
3509  */
ppp_poll(netdevice_t * dev)3510 static void ppp_poll (netdevice_t *dev)
3511 {
3512 	ppp_private_area_t *ppp_priv_area;
3513 	sdla_t *card;
3514 	u8 check_gateway=0;
3515 	ppp_flags_t *flags;
3516 
3517 	if (!dev || (ppp_priv_area = dev->priv) == NULL)
3518 		return;
3519 
3520 	card = ppp_priv_area->card;
3521 	flags = card->flags;
3522 
3523 	/* Shutdown is in progress, stop what you are
3524 	 * doing and get out */
3525 	if (test_bit(PERI_CRIT,&card->wandev.critical)){
3526 		clear_bit(POLL_CRIT,&card->wandev.critical);
3527 		return;
3528 	}
3529 
3530 	/* if_open() function has triggered the polling routine
3531 	 * to determine the configured IP addresses.  Once the
3532 	 * addresses are found, trigger the chdlc configuration */
3533 	if (test_bit(0,&ppp_priv_area->config_ppp)){
3534 
3535 		ppp_priv_area->ip_local_tmp  = get_ip_address(dev,WAN_LOCAL_IP);
3536 		ppp_priv_area->ip_remote_tmp = get_ip_address(dev,WAN_POINTOPOINT_IP);
3537 
3538 		if (ppp_priv_area->ip_local_tmp == ppp_priv_area->ip_remote_tmp &&
3539 	            card->u.p.ip_mode == WANOPT_PPP_HOST){
3540 
3541 			if (++ppp_priv_area->ip_error > MAX_IP_ERRORS){
3542 				printk(KERN_INFO "\n%s: --- WARNING ---\n",
3543 						card->devname);
3544 				printk(KERN_INFO "%s: The local IP address is the same as the\n",
3545 						card->devname);
3546 				printk(KERN_INFO "%s: Point-to-Point IP address.\n",
3547 						card->devname);
3548 				printk(KERN_INFO "%s: --- WARNING ---\n\n",
3549 						card->devname);
3550 			}else{
3551 				clear_bit(POLL_CRIT,&card->wandev.critical);
3552 				ppp_priv_area->poll_delay_timer.expires = jiffies+HZ;
3553 				add_timer(&ppp_priv_area->poll_delay_timer);
3554 				return;
3555 			}
3556 		}
3557 
3558 		ppp_priv_area->timer_int_enabled |= TMR_INT_ENABLED_CONFIG;
3559 		flags->imask |= PPP_INTR_TIMER;
3560 		ppp_priv_area->ip_error=0;
3561 
3562 		clear_bit(0,&ppp_priv_area->config_ppp);
3563 		clear_bit(POLL_CRIT,&card->wandev.critical);
3564 		return;
3565 	}
3566 
3567 	/* Dynamic interface implementation, as well as dynamic
3568 	 * routing.  */
3569 
3570 	switch (card->wandev.state) {
3571 
3572 	case WAN_DISCONNECTED:
3573 
3574 		/* If the dynamic interface configuration is on, and interface
3575 		 * is up, then bring down the netowrk interface */
3576 
3577 		if (test_bit(DYN_OPT_ON,&ppp_priv_area->interface_down) &&
3578 		    !test_bit(DEV_DOWN,&ppp_priv_area->interface_down)	&&
3579 		    card->wandev.dev->flags & IFF_UP){
3580 
3581 			printk(KERN_INFO "%s: Interface %s down.\n",
3582 				card->devname,card->wandev.dev->name);
3583 			change_dev_flags(card->wandev.dev,
3584 					(card->wandev.dev->flags&~IFF_UP));
3585 			set_bit(DEV_DOWN,&ppp_priv_area->interface_down);
3586 		}else{
3587 			/* We need to check if the local IP address is
3588                	   	 * zero. If it is, we shouldn't try to remove it.
3589                  	 * For some reason the kernel crashes badly if
3590                  	 * we try to remove the route twice */
3591 
3592 			if (card->wandev.dev->flags & IFF_UP &&
3593 		    	    get_ip_address(card->wandev.dev,WAN_LOCAL_IP) &&
3594 		    	    card->u.p.ip_mode == WANOPT_PPP_PEER){
3595 
3596 				remove_route(card);
3597 			}
3598 		}
3599 		break;
3600 
3601 	case WAN_CONNECTED:
3602 
3603 		/* In SMP machine this code can execute before the interface
3604 		 * comes up.  In this case, we must make sure that we do not
3605 		 * try to bring up the interface before dev_open() is finished */
3606 
3607 
3608 		/* DEV_DOWN will be set only when we bring down the interface
3609 		 * for the very first time. This way we know that it was us
3610 		 * that brought the interface down */
3611 
3612 		if (test_bit(DYN_OPT_ON,&ppp_priv_area->interface_down) &&
3613 	            test_bit(DEV_DOWN,  &ppp_priv_area->interface_down) &&
3614  		    !(card->wandev.dev->flags & IFF_UP)){
3615 
3616 			printk(KERN_INFO "%s: Interface %s up.\n",
3617 				card->devname,card->wandev.dev->name);
3618 
3619 			change_dev_flags(card->wandev.dev,(card->wandev.dev->flags|IFF_UP));
3620 			clear_bit(DEV_DOWN,&ppp_priv_area->interface_down);
3621 			check_gateway=1;
3622 		}
3623 
3624 		if ((card->u.p.ip_mode == WANOPT_PPP_PEER) &&
3625 		    test_bit(1,&Read_connection_info)) {
3626 
3627 			process_route(card);
3628 			clear_bit(1,&Read_connection_info);
3629 			check_gateway=1;
3630 		}
3631 
3632 		if (ppp_priv_area->gateway && check_gateway)
3633 			add_gateway(card,dev);
3634 
3635 		break;
3636 	}
3637 	clear_bit(POLL_CRIT,&card->wandev.critical);
3638 	return;
3639 }
3640 
3641 /*============================================================
3642  * trigger_ppp_poll
3643  *
3644  * Description:
3645  * 	Add a ppp_poll() task into a tq_scheduler bh handler
3646  *      for a specific interface.  This will kick
3647  *      the ppp_poll() routine at a later time.
3648  *
3649  * Usage:
3650  * 	Interrupts use this to defer a taks to
3651  *      a polling routine.
3652  *
3653  */
3654 
trigger_ppp_poll(netdevice_t * dev)3655 static void trigger_ppp_poll (netdevice_t *dev)
3656 {
3657 	ppp_private_area_t *ppp_priv_area;
3658 	if ((ppp_priv_area=dev->priv) != NULL){
3659 
3660 		sdla_t *card = ppp_priv_area->card;
3661 
3662 		if (test_bit(PERI_CRIT,&card->wandev.critical)){
3663 			return;
3664 		}
3665 
3666 		if (test_and_set_bit(POLL_CRIT,&card->wandev.critical)){
3667 			return;
3668 		}
3669 
3670 #ifdef LINUX_2_4
3671 		schedule_task(&ppp_priv_area->poll_task);
3672 #else
3673 		queue_task(&ppp_priv_area->poll_task, &tq_scheduler);
3674 #endif
3675 	}
3676 	return;
3677 }
3678 
ppp_poll_delay(unsigned long dev_ptr)3679 static void ppp_poll_delay (unsigned long dev_ptr)
3680 {
3681 	netdevice_t *dev = (netdevice_t *)dev_ptr;
3682 	trigger_ppp_poll(dev);
3683 }
3684 
3685 /*============================================================
3686  * detect_and_fix_tx_bug
3687  *
3688  * Description:
3689  *	On connect, if the board tx buffer ptr is not the same
3690  *      as the driver tx buffer ptr, we found a firmware bug.
3691  *      Report the bug to the above layer.  To fix the
3692  *      error restart communications again.
3693  *
3694  * Usage:
3695  *
3696  */
3697 
detect_and_fix_tx_bug(sdla_t * card)3698 static int detect_and_fix_tx_bug (sdla_t *card)
3699 {
3700 	if (((unsigned long)card->u.p.txbuf_base&0xFFF) != ((*card->u.p.txbuf_next)&0xFFF)){
3701 		NEX_PRINTK(KERN_INFO "Major Error, Fix the bug\n");
3702 		return 1;
3703 	}
3704 	return 0;
3705 }
3706 
3707 MODULE_LICENSE("GPL");
3708 
3709 /****** End *****************************************************************/
3710