v2.4.8 -> v2.4.8.1
[opensuse:kernel.git] / drivers / net / wan / sdla_ppp.c
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/version.h>
94 #include <linux/kernel.h>       /* printk(), and other useful stuff */
95 #include <linux/stddef.h>       /* offsetof(), etc. */
96 #include <linux/errno.h>        /* return codes */
97 #include <linux/string.h>       /* inline memset(), etc. */
98 #include <linux/slab.h> /* kmalloc(), kfree() */
99 #include <linux/wanrouter.h>    /* WAN router definitions */
100 #include <linux/wanpipe.h>      /* WANPIPE common user API definitions */
101 #include <linux/if_arp.h>       /* ARPHRD_* defines */
102 #include <asm/byteorder.h>      /* htons(), etc. */
103 #include <linux/in.h>           /* sockaddr_in */
104 #include <linux/inet.h>         /* in_aton(), in_ntoa() prototypes */
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  */
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(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  */
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  */
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(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  */
658 static int del_if(wan_device_t *wandev, netdevice_t *dev)
659 {
660         return 0;
661 }
662
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 ????
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  */
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  */
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  */
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  */
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)
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
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  */
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  */
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
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  */
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
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 */
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)
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  */
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 */
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 */
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  */
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  */
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  */
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  */
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  */
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  */
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
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  */
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  */
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  */
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  */
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  */
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
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
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
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  */
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                                 printk(KERN_INFO "%s: Assigned Lcl. Addr: %s\n", 
2272                                         card->devname, in_ntoa(ifa->ifa_local));
2273                                 printk(KERN_INFO "%s: Assigned Rmt. Addr: %s\n", 
2274                                                 card->devname, in_ntoa(ifa->ifa_address));
2275                         }else{
2276                                 printk(KERN_INFO 
2277                                 "%s: Error: Failed to add a route for PPP interface %s\n",
2278                                         card->devname,dev->name);       
2279                         }
2280                 }
2281         }
2282 #else
2283
2284         if ((card->u.p.ip_mode == WANOPT_PPP_PEER) &&
2285             (flags->ip_state == 0x09)){ 
2286         
2287                 if (ppp_priv_area->ip_local == 0)
2288                         return;
2289         
2290                 printk(KERN_INFO "%s: IPCP State Opened.\n", card->devname);
2291                 if (read_info( card )) {
2292                         printk(KERN_INFO 
2293                                 "%s: An error occurred in IP assignment.\n", 
2294                                 card->devname);
2295                 } else {
2296                         printk(KERN_INFO "%s: Assigned Lcl. Addr: %s\n", 
2297                                         card->devname, in_ntoa(dev->pa_addr));
2298                         printk(KERN_INFO "%s: Assigned Rmt. Addr: %s\n", 
2299                                         card->devname, in_ntoa(dev->pa_dstaddr));
2300                 }
2301         }
2302
2303 #endif
2304
2305 }
2306
2307 /*============================================================================
2308  * Monitor physical link disconnected phase.
2309  *  o if interface is up and the hold-down timeout has expired, then retry
2310  *    connection.
2311  */
2312 static void retrigger_comm(sdla_t *card)
2313 {
2314         netdevice_t *dev = card->wandev.dev;
2315
2316         if (dev && ((jiffies - card->state_tick) > HOLD_DOWN_TIME)) {
2317
2318                 wanpipe_set_state(card, WAN_CONNECTING);
2319
2320                 if(ppp_comm_enable(card) == CMD_OK){
2321                         init_ppp_tx_rx_buff( card );
2322                 }                
2323         }
2324 }
2325
2326 /****** Miscellaneous Functions *********************************************/
2327
2328 /*============================================================================
2329  * Configure S508 adapter.
2330  */
2331 static int config508(netdevice_t *dev, sdla_t *card)
2332 {
2333         ppp508_conf_t cfg;
2334 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2335         struct in_device *in_dev = dev->ip_ptr;
2336 #endif
2337         ppp_private_area_t *ppp_priv_area = dev->priv;
2338
2339         /* Prepare PPP configuration structure */
2340         memset(&cfg, 0, sizeof(ppp508_conf_t));
2341
2342         if (card->wandev.clocking)
2343                 cfg.line_speed = card->wandev.bps;
2344
2345         if (card->wandev.interface == WANOPT_RS232)
2346                 cfg.conf_flags |= INTERFACE_LEVEL_RS232;
2347
2348
2349         cfg.conf_flags  |= DONT_TERMINATE_LNK_MAX_CONFIG; /*send Configure-Request packets forever*/
2350         cfg.txbuf_percent       = PERCENT_TX_BUFF;      /* % of Tx bufs */
2351         cfg.mtu_local           = card->wandev.mtu;
2352         cfg.mtu_remote          = card->wandev.mtu;                  /*    Default   */
2353         cfg.restart_tmr         = TIME_BETWEEN_CONF_REQ;             /*    30 = 3sec */
2354         cfg.auth_rsrt_tmr       = TIME_BETWEEN_PAP_CHAP_REQ;         /*    30 = 3sec */
2355         cfg.auth_wait_tmr       = WAIT_PAP_CHAP_WITHOUT_REPLY;       /*   300 = 30s  */
2356         cfg.mdm_fail_tmr        = WAIT_AFTER_DCD_CTS_LOW;            /*     5 = 0.5s */
2357         cfg.dtr_drop_tmr        = TIME_DCD_CTS_LOW_AFTER_LNK_DOWN;   /*    10 = 1s   */
2358         cfg.connect_tmout       = WAIT_DCD_HIGH_AFTER_ENABLE_COMM;   /*   900 = 90s  */
2359         cfg.conf_retry          = MAX_CONF_REQ_WITHOUT_REPLY;        /*    10 = 1s   */
2360         cfg.term_retry          = MAX_TERM_REQ_WITHOUT_REPLY;        /*     2 times  */
2361         cfg.fail_retry          = NUM_CONF_NAK_WITHOUT_REPLY;        /*     5 times  */
2362         cfg.auth_retry          = NUM_AUTH_REQ_WITHOUT_REPLY;        /*     10 times */   
2363
2364
2365         if( !card->u.p.authenticator ) {
2366                 printk(KERN_INFO "%s: Device is not configured as an authenticator\n", 
2367                                 card->devname);
2368                 cfg.auth_options = NO_AUTHENTICATION;
2369         }else{
2370                 printk(KERN_INFO "%s: Device is configured as an authenticator\n", 
2371                                 card->devname);
2372                 cfg.auth_options = INBOUND_AUTH;
2373         }
2374
2375         if( ppp_priv_area->pap == WANOPT_YES){
2376                 cfg.auth_options |=PAP_AUTH;
2377                 printk(KERN_INFO "%s: Pap enabled\n", card->devname);
2378         }
2379         if( ppp_priv_area->chap == WANOPT_YES){
2380                 cfg.auth_options |= CHAP_AUTH;
2381                 printk(KERN_INFO "%s: Chap enabled\n", card->devname);
2382         }
2383
2384
2385         if (ppp_priv_area->enable_IPX == WANOPT_YES){
2386                 printk(KERN_INFO "%s: Enabling IPX Protocol\n",card->devname);
2387                 cfg.ipx_options         = ENABLE_IPX | ROUTING_PROT_DEFAULT;
2388         }else{
2389                 cfg.ipx_options         = DISABLE_IPX;
2390         }
2391
2392         switch (card->u.p.ip_mode) {
2393         
2394                 case WANOPT_PPP_STATIC:
2395
2396                         printk(KERN_INFO "%s: PPP IP Mode: STATIC\n",card->devname);
2397                         cfg.ip_options          = L_AND_R_IP_NO_ASSIG | 
2398                                                             ENABLE_IP;
2399 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2400                         cfg.ip_local            = in_dev->ifa_list->ifa_local;
2401                         cfg.ip_remote           = in_dev->ifa_list->ifa_address;
2402 #else
2403                         cfg.ip_local            = dev->pa_addr;
2404                         cfg.ip_remote           = dev->pa_dstaddr;
2405 #endif
2406
2407                         /* Debugging code used to check that IP addresses
2408                          * obtained from the kernel are correct */
2409
2410                         {
2411                         char laddr[20];
2412                         char raddr[20];
2413                         strcpy(laddr,in_ntoa(cfg.ip_local));
2414                         strcpy(raddr,in_ntoa(cfg.ip_remote));
2415
2416                         NEX_PRINTK(KERN_INFO "Local %s Remote %s Name %s\n",
2417                                         laddr,raddr, dev->name);
2418                         
2419                         }       
2420                         break;
2421
2422                 case WANOPT_PPP_HOST:
2423
2424                         printk(KERN_INFO "%s: PPP IP Mode: HOST\n",card->devname);
2425                         cfg.ip_options          = L_IP_LOCAL_ASSIG |
2426                                                   R_IP_LOCAL_ASSIG | 
2427                                                   ENABLE_IP;
2428 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2429                         cfg.ip_local            = in_dev->ifa_list->ifa_local;
2430                         cfg.ip_remote           = in_dev->ifa_list->ifa_address;
2431 #else
2432                         cfg.ip_local            = dev->pa_addr;
2433                         cfg.ip_remote           = dev->pa_dstaddr;
2434 #endif
2435
2436                         /* Debugging code used to check that IP addresses
2437                          * obtained from the kernel are correct */
2438                         {
2439                         char laddr[20];
2440                         char raddr[20];
2441                         strcpy(laddr,in_ntoa(cfg.ip_local));
2442                         strcpy(raddr,in_ntoa(cfg.ip_remote));
2443
2444                         NEX_PRINTK (KERN_INFO "Local %s Remote %s Name %s\n",
2445                                         laddr,raddr, dev->name);
2446                         
2447                         }       
2448
2449
2450                         break;
2451         
2452                 case WANOPT_PPP_PEER:
2453
2454 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2455
2456                         printk(KERN_INFO "%s: PPP IP Mode: PEER\n",card->devname);
2457                         cfg.ip_options          = L_IP_REMOTE_ASSIG | 
2458                                                   R_IP_REMOTE_ASSIG | 
2459                                                           ENABLE_IP;
2460                         cfg.ip_local            = 0x00;
2461                         cfg.ip_remote           = 0x00;
2462                         break;
2463
2464 #else
2465
2466                         /* No PEER support for 2.0.X kernels, drop down to default
2467                          * condition */
2468
2469                         printk(KERN_INFO "%s: ERROR, PEER mode is not supported in 2.0.X kernels\n",
2470                                                 card->devname);
2471                         
2472 #endif
2473
2474                 default:
2475                         printk(KERN_INFO "%s: ERROR: Unsuported PPP Mode Selected\n",
2476                                         card->devname);
2477                         printk(KERN_INFO "%s:        PPP IP Modes: STATIC, PEER or HOST\n",
2478                                         card->devname); 
2479                         return 1;
2480         }
2481
2482         return ppp_configure(card, &cfg);
2483 }
2484
2485 /*============================================================================
2486  * Show disconnection cause.
2487  */
2488 static void show_disc_cause(sdla_t *card, unsigned cause)
2489 {
2490         if (cause & 0x0802) 
2491
2492                 printk(KERN_INFO "%s: link terminated by peer\n", 
2493                         card->devname);
2494
2495         else if (cause & 0x0004) 
2496
2497                 printk(KERN_INFO "%s: link terminated by user\n", 
2498                         card->devname);
2499
2500         else if (cause & 0x0008) 
2501
2502                 printk(KERN_INFO "%s: authentication failed\n", card->devname);
2503         
2504         else if (cause & 0x0010) 
2505
2506                 printk(KERN_INFO 
2507                         "%s: authentication protocol negotiation failed\n", 
2508                         card->devname);
2509
2510         else if (cause & 0x0020) 
2511                 
2512                 printk(KERN_INFO
2513                 "%s: peer's request for authentication rejected\n",
2514                 card->devname);
2515
2516         else if (cause & 0x0040) 
2517         
2518                 printk(KERN_INFO "%s: MRU option rejected by peer\n", 
2519                 card->devname);
2520
2521         else if (cause & 0x0080) 
2522         
2523                 printk(KERN_INFO "%s: peer's MRU was too small\n", 
2524                 card->devname);
2525
2526         else if (cause & 0x0100) 
2527
2528                 printk(KERN_INFO "%s: failed to negotiate peer's LCP options\n",
2529                 card->devname);
2530
2531         else if (cause & 0x0200) 
2532                 
2533                 printk(KERN_INFO "%s: failed to negotiate peer's IPCP options\n"
2534                 , card->devname);
2535
2536         else if (cause & 0x0400) 
2537
2538                 printk(KERN_INFO 
2539                         "%s: failed to negotiate peer's IPXCP options\n",
2540                         card->devname);
2541 }
2542
2543 /*=============================================================================
2544  * Process UDP call of type PTPIPEAB.
2545  */
2546 static void process_udp_mgmt_pkt(sdla_t *card, netdevice_t *dev, 
2547                                  ppp_private_area_t *ppp_priv_area ) 
2548 {
2549         unsigned char buf2[5];
2550         unsigned char *buf;
2551         unsigned int frames, len;
2552         struct sk_buff *new_skb;
2553         unsigned short data_length, buffer_length, real_len;
2554         unsigned long data_ptr;
2555         int udp_mgmt_req_valid = 1;
2556         ppp_mbox_t *mbox = card->mbox;
2557         struct timeval tv;
2558         int err;
2559         ppp_udp_pkt_t *ppp_udp_pkt = (ppp_udp_pkt_t*)&ppp_priv_area->udp_pkt_data;
2560
2561         memcpy(&buf2, &card->wandev.udp_port, 2 );
2562
2563
2564         if(ppp_priv_area->udp_pkt_src == UDP_PKT_FRM_NETWORK) {
2565
2566                 switch(ppp_udp_pkt->cblock.command) {
2567
2568                         case PPIPE_GET_IBA_DATA:
2569                         case PPP_READ_CONFIG:
2570                         case PPP_GET_CONNECTION_INFO:
2571                         case PPIPE_ROUTER_UP_TIME:
2572                         case PPP_READ_STATISTICS:
2573                         case PPP_READ_ERROR_STATS:
2574                         case PPP_READ_PACKET_STATS:
2575                         case PPP_READ_LCP_STATS:
2576                         case PPP_READ_IPCP_STATS:
2577                         case PPP_READ_IPXCP_STATS:
2578                         case PPP_READ_PAP_STATS:
2579                         case PPP_READ_CHAP_STATS:
2580                         case PPP_READ_CODE_VERSION:
2581                                 udp_mgmt_req_valid = 1;
2582                                 break;
2583                            
2584                         default:
2585                                 udp_mgmt_req_valid = 0;
2586                                 break;
2587                 } 
2588         }
2589         
2590         if(!udp_mgmt_req_valid) {
2591             
2592                 /* set length to 0 */
2593                 ppp_udp_pkt->cblock.length = 0x00;
2594
2595                 /* set return code */
2596                 ppp_udp_pkt->cblock.result = 0xCD; 
2597                 ++ppp_priv_area->pipe_mgmt_stat.UDP_PIPE_mgmt_direction_err;
2598         
2599                 if (net_ratelimit()){   
2600                         printk(KERN_INFO 
2601                         "%s: Warning, Illegal UDP command attempted from network: %x\n",
2602                         card->devname,ppp_udp_pkt->cblock.command);
2603                 }
2604         } else {
2605                 /* Initialize the trace element */
2606                 trace_element_t trace_element;              
2607
2608                 switch (ppp_udp_pkt->cblock.command){
2609
2610                 /* PPIPE_ENABLE_TRACING */
2611                 case PPIPE_ENABLE_TRACING:
2612                         if (!card->TracingEnabled) {
2613                         
2614                                 /* OPERATE_DATALINE_MONITOR */
2615                                 mbox->cmd.command = PPP_DATALINE_MONITOR;
2616                                 mbox->cmd.length = 0x01;
2617                                 mbox->data[0] = ppp_udp_pkt->data[0];
2618                                 err = sdla_exec(mbox) ? 
2619                                         mbox->cmd.result : CMD_TIMEOUT;
2620            
2621                                 if (err != CMD_OK) { 
2622                                         
2623                                         ppp_error(card, err, mbox);
2624                                         card->TracingEnabled = 0;
2625                                 
2626                                         /* set the return code */
2627
2628                                         ppp_udp_pkt->cblock.result = mbox->cmd.result;
2629                                         mbox->cmd.length = 0;
2630                                         break;
2631                                 } 
2632
2633                                 sdla_peek(&card->hw, 0xC000, &buf2, 2);
2634                     
2635                                 ppp_priv_area->curr_trace_addr = 0;
2636                                 memcpy(&ppp_priv_area->curr_trace_addr, &buf2, 2);
2637                                 ppp_priv_area->start_trace_addr = 
2638                                                 ppp_priv_area->curr_trace_addr;
2639                                 ppp_priv_area->end_trace_addr = 
2640                                         ppp_priv_area->start_trace_addr + END_OFFSET;
2641                         
2642                                 /* MAX_SEND_BUFFER_SIZE - 28 (IP header) 
2643                                    - 32 (ppipemon CBLOCK) */
2644                                 available_buffer_space = MAX_LGTH_UDP_MGNT_PKT - 
2645                                                          sizeof(ip_pkt_t)-
2646                                                          sizeof(udp_pkt_t)-
2647                                                          sizeof(wp_mgmt_t)-
2648                                                          sizeof(cblock_t);
2649                         }
2650                         ppp_udp_pkt->cblock.result = 0;
2651                         mbox->cmd.length = 0;
2652                         card->TracingEnabled = 1;
2653                         break;
2654            
2655                 /* PPIPE_DISABLE_TRACING */
2656                 case PPIPE_DISABLE_TRACING:
2657