v2.4.8 -> v2.4.8.1
[opensuse:kernel.git] / drivers / net / wan / sdla_chdlc.c
1 /*****************************************************************************
2 * sdla_chdlc.c  WANPIPE(tm) Multiprotocol WAN Link Driver. Cisco HDLC module.
3 *
4 * Authors:      Nenad Corbic <ncorbic@sangoma.com>
5 *               Gideon Hack  
6 *
7 * Copyright:    (c) 1995-2001 Sangoma Technologies Inc.
8 *
9 *               This program is free software; you can redistribute it and/or
10 *               modify it under the terms of the GNU General Public License
11 *               as published by the Free Software Foundation; either version
12 *               2 of the License, or (at your option) any later version.
13 * ============================================================================
14 * Feb 28, 2001  Nenad Corbic    Updated if_tx_timeout() routine for 
15 *                               2.4.X kernels.
16 * Jan 25, 2001  Nenad Corbic    Added a TTY Sync serial driver over the
17 *                               HDLC streaming protocol
18 *                               Added a TTY Async serial driver over the
19 *                               Async protocol.
20 * Dec 15, 2000  Nenad Corbic    Updated for 2.4.X Kernel support
21 * Nov 13, 2000  Nenad Corbic    Added true interface type encoding option.
22 *                               Tcpdump doesn't support CHDLC inteface
23 *                               types, to fix this "true type" option will set
24 *                               the interface type to RAW IP mode.
25 * Nov 07, 2000  Nenad Corbic    Added security features for UDP debugging:
26 *                               Deny all and specify allowed requests.
27 * Jun 20, 2000  Nenad Corbic    Fixed the API IP ERROR bug. Caused by the 
28 *                               latest update.
29 * May 09, 2000  Nenad Corbic    Option to bring down an interface
30 *                               upon disconnect.
31 * Mar 23, 2000  Nenad Corbic    Improved task queue, bh handling.
32 * Mar 16, 2000  Nenad Corbic    Fixed the SLARP Dynamic IP addressing.
33 * Mar 06, 2000  Nenad Corbic    Bug Fix: corrupted mbox recovery.
34 * Feb 10, 2000  Gideon Hack     Added ASYNC support.
35 * Feb 09, 2000  Nenad Corbic    Fixed two shutdown bugs in update() and
36 *                               if_stats() functions.
37 * Jan 24, 2000  Nenad Corbic    Fixed a startup wanpipe state racing,  
38 *                               condition between if_open and isr. 
39 * Jan 10, 2000  Nenad Corbic    Added new socket API support.
40 * Dev 15, 1999  Nenad Corbic    Fixed up header files for 2.0.X kernels
41 * Nov 20, 1999  Nenad Corbic    Fixed zero length API bug.
42 * Sep 30, 1999  Nenad Corbic    Fixed dynamic IP and route setup.
43 * Sep 23, 1999  Nenad Corbic    Added SMP support, fixed tracing 
44 * Sep 13, 1999  Nenad Corbic    Split up Port 0 and 1 into separate devices.
45 * Jun 02, 1999  Gideon Hack     Added support for the S514 adapter.
46 * Oct 30, 1998  Jaspreet Singh  Added Support for CHDLC API (HDLC STREAMING).
47 * Oct 28, 1998  Jaspreet Singh  Added Support for Dual Port CHDLC.
48 * Aug 07, 1998  David Fong      Initial version.
49 *****************************************************************************/
50
51 #include <linux/version.h>
52 #include <linux/kernel.h>       /* printk(), and other useful stuff */
53 #include <linux/stddef.h>       /* offsetof(), etc. */
54 #include <linux/errno.h>        /* return codes */
55 #include <linux/string.h>       /* inline memset(), etc. */
56 #include <linux/slab.h> /* kmalloc(), kfree() */
57 #include <linux/wanrouter.h>    /* WAN router definitions */
58 #include <linux/wanpipe.h>      /* WANPIPE common user API definitions */
59 #include <linux/if_arp.h>       /* ARPHRD_* defines */
60
61
62 #if defined(LINUX_2_1) || defined(LINUX_2_4)
63  #include <asm/uaccess.h>
64  #include <linux/inetdevice.h>
65  #include <linux/netdevice.h>
66 #else                           
67  #include <asm/segment.h>
68  #include <net/route.h>          /* Adding new route entries : 2.0.X kernels */
69 #endif
70
71 #include <linux/in.h>           /* sockaddr_in */
72 #include <linux/inet.h> 
73 #include <linux/if.h>
74 #include <asm/byteorder.h>      /* htons(), etc. */
75 #include <linux/sdlapci.h>
76 #include <asm/io.h>
77
78 #include <linux/sdla_chdlc.h>           /* CHDLC firmware API definitions */
79 #include <linux/sdla_asy.h>             /* CHDLC (async) API definitions */
80
81 #include <linux/if_wanpipe_common.h>    /* Socket Driver common area */
82 #include <linux/if_wanpipe.h>           
83
84 /* TTY Includes */
85 #include <linux/tty.h>
86 #include <linux/tty_flip.h>
87 #include <linux/serial.h>
88
89
90 /****** Defines & Macros ****************************************************/
91
92 /* reasons for enabling the timer interrupt on the adapter */
93 #define TMR_INT_ENABLED_UDP             0x01
94 #define TMR_INT_ENABLED_UPDATE          0x02
95 #define TMR_INT_ENABLED_CONFIG          0x10
96
97 #define MAX_IP_ERRORS   10
98
99 #define TTY_CHDLC_MAX_MTU       2000
100 #define CHDLC_DFLT_DATA_LEN     1500            /* default MTU */
101 #define CHDLC_HDR_LEN           1
102
103 #define CHDLC_API 0x01
104
105 #define PORT(x)   (x == 0 ? "PRIMARY" : "SECONDARY" )
106 #define MAX_BH_BUFF     10
107
108 //#define PRINT_DEBUG
109 #ifdef PRINT_DEBUG
110 #define dbg_printk(format, a...) printk(format, ## a)
111 #else
112 #define dbg_printk(format, a...)
113 #endif  
114
115 /******Data Structures*****************************************************/
116
117 /* This structure is placed in the private data area of the device structure.
118  * The card structure used to occupy the private area but now the following 
119  * structure will incorporate the card structure along with CHDLC specific data
120  */
121
122 typedef struct chdlc_private_area
123 {
124         wanpipe_common_t common;
125         sdla_t          *card;
126         int             TracingEnabled;         /* For enabling Tracing */
127         unsigned long   curr_trace_addr;        /* Used for Tracing */
128         unsigned long   start_trace_addr;
129         unsigned long   end_trace_addr;
130         unsigned long   base_addr_trace_buffer;
131         unsigned long   end_addr_trace_buffer;
132         unsigned short  number_trace_elements;
133         unsigned        available_buffer_space;
134         unsigned long   router_start_time;
135         unsigned char   route_status;
136         unsigned char   route_removed;
137         unsigned long   tick_counter;           /* For 5s timeout counter */
138         unsigned long   router_up_time;
139         u32             IP_address;             /* IP addressing */
140         u32             IP_netmask;
141         u32             ip_local;
142         u32             ip_remote;
143         u32             ip_local_tmp;
144         u32             ip_remote_tmp;
145         u8              ip_error;
146         u8              config_chdlc;
147         u8              config_chdlc_timeout;
148         unsigned char  mc;                      /* Mulitcast support on/off */
149         unsigned short udp_pkt_lgth;            /* udp packet processing */
150         char udp_pkt_src;
151         char udp_pkt_data[MAX_LGTH_UDP_MGNT_PKT];
152         unsigned short timer_int_enabled;
153         char update_comms_stats;                /* updating comms stats */
154
155 #if defined(LINUX_2_1) || defined(LINUX_2_4)
156         bh_data_t *bh_head;               /* Circular buffer for chdlc_bh */
157         unsigned long  tq_working;
158         volatile int  bh_write;
159         volatile int  bh_read;
160         atomic_t  bh_buff_used;
161 #endif
162         
163         unsigned char interface_down;
164
165         /* Polling task queue. Each interface
166          * has its own task queue, which is used
167          * to defer events from the interrupt */
168         struct tq_struct poll_task;
169         struct timer_list poll_delay_timer;
170
171         u8 gateway;
172         u8 true_if_encoding;
173         //FIXME: add driver stats as per frame relay!
174
175 } chdlc_private_area_t;
176
177 /* Route Status options */
178 #define NO_ROUTE        0x00
179 #define ADD_ROUTE       0x01
180 #define ROUTE_ADDED     0x02
181 #define REMOVE_ROUTE    0x03
182
183
184 /* variable for keeping track of enabling/disabling FT1 monitor status */
185 static int rCount = 0;
186
187 /* variable for tracking how many interfaces to open for WANPIPE on the
188    two ports */
189
190 extern void disable_irq(unsigned int);
191 extern void enable_irq(unsigned int);
192
193 /****** Function Prototypes *************************************************/
194 /* WAN link driver entry points. These are called by the WAN router module. */
195 static int update (wan_device_t* wandev);
196 static int new_if (wan_device_t* wandev, netdevice_t* dev,
197         wanif_conf_t* conf);
198
199 /* Network device interface */
200 static int if_init   (netdevice_t* dev);
201 static int if_open   (netdevice_t* dev);
202 static int if_close  (netdevice_t* dev);
203 static int if_header (struct sk_buff* skb, netdevice_t* dev,
204         unsigned short type, void* daddr, void* saddr, unsigned len);
205
206 #if defined(LINUX_2_1) || defined(LINUX_2_4)
207   static int if_rebuild_hdr (struct sk_buff *skb);
208   static struct net_device_stats* if_stats (netdevice_t* dev);
209
210 #else
211   static int if_rebuild_hdr (void* hdr, netdevice_t* dev, unsigned long raddr,
212         struct sk_buff* skb);
213   static struct enet_statistics* if_stats (netdevice_t* dev);
214 #endif
215   
216 static int if_send (struct sk_buff* skb, netdevice_t* dev);
217
218 /* CHDLC Firmware interface functions */
219 static int chdlc_configure      (sdla_t* card, void* data);
220 static int chdlc_comm_enable    (sdla_t* card);
221 static int chdlc_read_version   (sdla_t* card, char* str);
222 static int chdlc_set_intr_mode  (sdla_t* card, unsigned mode);
223 static int chdlc_send (sdla_t* card, void* data, unsigned len);
224 static int chdlc_read_comm_err_stats (sdla_t* card);
225 static int chdlc_read_op_stats (sdla_t* card);
226 static int chdlc_error (sdla_t *card, int err, CHDLC_MAILBOX_STRUCT *mb);
227
228
229 static int chdlc_disable_comm_shutdown (sdla_t *card);
230 #ifdef LINUX_2_4
231   static void if_tx_timeout (netdevice_t *dev);
232 #endif
233
234 /* Miscellaneous CHDLC Functions */
235 static int set_chdlc_config (sdla_t* card);
236 static void init_chdlc_tx_rx_buff( sdla_t* card);
237 static int process_chdlc_exception(sdla_t *card);
238 static int process_global_exception(sdla_t *card);
239 static int update_comms_stats(sdla_t* card,
240         chdlc_private_area_t* chdlc_priv_area);
241 static int configure_ip (sdla_t* card);
242 static int unconfigure_ip (sdla_t* card);
243 static void process_route(sdla_t *card);
244 static void port_set_state (sdla_t *card, int);
245 static int config_chdlc (sdla_t *card);
246 static void disable_comm (sdla_t *card);
247
248 static void trigger_chdlc_poll (netdevice_t *);
249 static void chdlc_poll (netdevice_t *);
250 static void chdlc_poll_delay (unsigned long dev_ptr);
251
252
253 /* Miscellaneous asynchronous interface Functions */
254 static int set_asy_config (sdla_t* card);
255 static int asy_comm_enable (sdla_t* card);
256
257 /* Interrupt handlers */
258 static void wpc_isr (sdla_t* card);
259 static void rx_intr (sdla_t* card);
260 static void timer_intr(sdla_t *);
261
262 #if defined(LINUX_2_1) || defined(LINUX_2_4)
263   /* Bottom half handlers */
264   static void chdlc_bh (netdevice_t *);
265   static int chdlc_bh_cleanup (netdevice_t *);
266   static int bh_enqueue (netdevice_t *, struct sk_buff *);
267 #endif
268
269 /* Miscellaneous functions */
270 static int chk_bcast_mcast_addr(sdla_t* card, netdevice_t* dev,
271                                 struct sk_buff *skb);
272 static int reply_udp( unsigned char *data, unsigned int mbox_len );
273 static int intr_test( sdla_t* card);
274 static int udp_pkt_type( struct sk_buff *skb , sdla_t* card);
275 static int store_udp_mgmt_pkt(char udp_pkt_src, sdla_t* card,
276                                 struct sk_buff *skb, netdevice_t* dev,
277                                 chdlc_private_area_t* chdlc_priv_area);
278 static int process_udp_mgmt_pkt(sdla_t* card, netdevice_t* dev,  
279                                 chdlc_private_area_t* chdlc_priv_area);
280 static unsigned short calc_checksum (char *, int);
281 static void s508_lock (sdla_t *card, unsigned long *smp_flags);
282 static void s508_unlock (sdla_t *card, unsigned long *smp_flags);
283
284
285 static int  Intr_test_counter;
286
287 /* TTY Global Definitions */
288
289 #if defined(LINUX_2_4) || defined(LINUX_2_1)
290
291 #define NR_PORTS 4
292 #define WAN_TTY_MAJOR 226
293 #define WAN_TTY_MINOR 0
294
295 #define WAN_CARD(port) (tty_card_map[port])
296 #define MIN_PORT 0
297 #define MAX_PORT NR_PORTS-1 
298
299 #define CRC_LENGTH 2
300
301 static int wanpipe_tty_init(sdla_t *card);
302 static void wanpipe_tty_receive(sdla_t *, unsigned, unsigned int);
303 static void wanpipe_tty_trigger_poll(sdla_t *card);
304
305 static struct tty_driver serial_driver, callout_driver;
306 static int serial_refcount=1;
307 static int tty_init_cnt=0;
308
309 static struct serial_state rs_table[NR_PORTS];
310 static struct tty_struct *serial_table[NR_PORTS];
311 static struct termios *serial_termios[NR_PORTS];
312 static struct termios *serial_termios_locked[NR_PORTS];
313
314 static char tty_driver_mode=WANOPT_TTY_SYNC;
315
316 static char *opt_decode[] = {"NONE","CRTSCTS","XONXOFF-RX",
317                              "CRTSCTS XONXOFF-RX","XONXOFF-TX",
318                              "CRTSCTS XONXOFF-TX","CRTSCTS XONXOFF"};
319 static char *p_decode[] = {"NONE","ODD","EVEN"};
320
321 static void* tty_card_map[NR_PORTS] = {NULL,NULL,NULL,NULL};
322
323 #endif
324
325
326 /****** Public Functions ****************************************************/
327
328 /*============================================================================
329  * Cisco HDLC protocol initialization routine.
330  *
331  * This routine is called by the main WANPIPE module during setup.  At this
332  * point adapter is completely initialized and firmware is running.
333  *  o read firmware version (to make sure it's alive)
334  *  o configure adapter
335  *  o initialize protocol-specific fields of the adapter data space.
336  *
337  * Return:      0       o.k.
338  *              < 0     failure.
339  */
340 int wpc_init (sdla_t* card, wandev_conf_t* conf)
341 {
342         unsigned char port_num;
343         int err;
344         unsigned long max_permitted_baud = 0;
345         SHARED_MEMORY_INFO_STRUCT *flags;
346
347         union
348                 {
349                 char str[80];
350                 } u;
351         volatile CHDLC_MAILBOX_STRUCT* mb;
352         CHDLC_MAILBOX_STRUCT* mb1;
353         unsigned long timeout;
354
355         /* Verify configuration ID */
356         if (conf->config_id != WANCONFIG_CHDLC) {
357                 printk(KERN_INFO "%s: invalid configuration ID %u!\n",
358                                   card->devname, conf->config_id);
359                 return -EINVAL;
360         }
361
362         /* Find out which Port to use */
363         if ((conf->comm_port == WANOPT_PRI) || (conf->comm_port == WANOPT_SEC)){
364                 if (card->next){
365
366                         if (conf->comm_port != card->next->u.c.comm_port){
367                                 card->u.c.comm_port = conf->comm_port;
368                         }else{
369                                 printk(KERN_INFO "%s: ERROR - %s port used!\n",
370                                         card->wandev.name, PORT(conf->comm_port));
371                                 return -EINVAL;
372                         }
373                 }else{
374                         card->u.c.comm_port = conf->comm_port;
375                 }
376         }else{
377                 printk(KERN_INFO "%s: ERROR - Invalid Port Selected!\n",
378                                         card->wandev.name);
379                 return -EINVAL;
380         }
381         
382
383         /* Initialize protocol-specific fields */
384         if(card->hw.type != SDLA_S514){
385
386                 if (card->u.c.comm_port == WANOPT_PRI){ 
387                         card->mbox  = (void *) card->hw.dpmbase;
388                 }else{
389                         card->mbox  = (void *) card->hw.dpmbase + 
390                                 SEC_BASE_ADDR_MB_STRUCT - PRI_BASE_ADDR_MB_STRUCT;
391                 }       
392         }else{ 
393                 /* for a S514 adapter, set a pointer to the actual mailbox in the */
394                 /* allocated virtual memory area */
395                 if (card->u.c.comm_port == WANOPT_PRI){
396                         card->mbox = (void *) card->hw.dpmbase + PRI_BASE_ADDR_MB_STRUCT;
397                 }else{
398                         card->mbox = (void *) card->hw.dpmbase + SEC_BASE_ADDR_MB_STRUCT;
399                 }       
400         }
401
402         mb = mb1 = card->mbox;
403
404         if (!card->configured){
405
406                 /* The board will place an 'I' in the return code to indicate that it is
407                 ready to accept commands.  We expect this to be completed in less
408                 than 1 second. */
409
410                 timeout = jiffies;
411                 while (mb->return_code != 'I')  /* Wait 1s for board to initialize */
412                         if ((jiffies - timeout) > 1*HZ) break;
413
414                 if (mb->return_code != 'I') {
415                         printk(KERN_INFO
416                                 "%s: Initialization not completed by adapter\n",
417                                 card->devname);
418                         printk(KERN_INFO "Please contact Sangoma representative.\n");
419                         return -EIO;
420                 }
421         }
422
423         /* Read firmware version.  Note that when adapter initializes, it
424          * clears the mailbox, so it may appear that the first command was
425          * executed successfully when in fact it was merely erased. To work
426          * around this, we execute the first command twice.
427          */
428
429         if (chdlc_read_version(card, u.str))
430                 return -EIO;
431
432         printk(KERN_INFO "%s: Running Cisco HDLC firmware v%s\n",
433                 card->devname, u.str); 
434
435         card->isr                       = &wpc_isr;
436         card->poll                      = NULL;
437         card->exec                      = NULL;
438         card->wandev.update             = &update;
439         card->wandev.new_if             = &new_if;
440         card->wandev.del_if             = NULL;
441         card->wandev.udp_port           = conf->udp_port;
442         card->disable_comm              = &disable_comm;
443         card->wandev.new_if_cnt = 0;
444
445         /* reset the number of times the 'update()' proc has been called */
446         card->u.c.update_call_count = 0;
447         
448         card->wandev.ttl = conf->ttl;
449         card->wandev.interface = conf->interface; 
450
451         if ((card->u.c.comm_port == WANOPT_SEC && conf->interface == WANOPT_V35)&&
452             card->hw.type != SDLA_S514){
453                 printk(KERN_INFO "%s: ERROR - V35 Interface not supported on S508 %s port \n",
454                         card->devname, PORT(card->u.c.comm_port));
455                 return -EIO;
456         }
457
458         card->wandev.clocking = conf->clocking;
459
460         port_num = card->u.c.comm_port;
461
462         /* in API mode, we can configure for "receive only" buffering */
463         if(card->hw.type == SDLA_S514) {
464                 card->u.c.receive_only = conf->receive_only;
465                 if(conf->receive_only) {
466                         printk(KERN_INFO
467                                 "%s: Configured for 'receive only' mode\n",
468                                 card->devname);
469                 }
470         }
471
472         /* Setup Port Bps */
473
474         if(card->wandev.clocking) {
475                 if((port_num == WANOPT_PRI) || card->u.c.receive_only) {
476                         /* For Primary Port 0 */
477                         max_permitted_baud =
478                                 (card->hw.type == SDLA_S514) ?
479                                 PRI_MAX_BAUD_RATE_S514 : 
480                                 PRI_MAX_BAUD_RATE_S508;
481
482                 }else if(port_num == WANOPT_SEC) {
483                         /* For Secondary Port 1 */
484                         max_permitted_baud =
485                                (card->hw.type == SDLA_S514) ?
486                                 SEC_MAX_BAUD_RATE_S514 :
487                                 SEC_MAX_BAUD_RATE_S508;
488                         }
489   
490                         if(conf->bps > max_permitted_baud) {
491                                 conf->bps = max_permitted_baud;
492                                 printk(KERN_INFO "%s: Baud too high!\n",
493                                         card->wandev.name);
494                                 printk(KERN_INFO "%s: Baud rate set to %lu bps\n", 
495                                         card->wandev.name, max_permitted_baud);
496                         }
497                         card->wandev.bps = conf->bps;
498         }else{
499                 card->wandev.bps = 0;
500         }
501
502         /* Setup the Port MTU */
503         if((port_num == WANOPT_PRI) || card->u.c.receive_only) {
504
505                 /* For Primary Port 0 */
506                 card->wandev.mtu =
507                         (conf->mtu >= MIN_LGTH_CHDLC_DATA_CFG) ?
508                         min(conf->mtu, PRI_MAX_NO_DATA_BYTES_IN_FRAME) :
509                         CHDLC_DFLT_DATA_LEN;
510         } else if(port_num == WANOPT_SEC) { 
511                 /* For Secondary Port 1 */
512                 card->wandev.mtu =
513                         (conf->mtu >= MIN_LGTH_CHDLC_DATA_CFG) ?
514                         min(conf->mtu, SEC_MAX_NO_DATA_BYTES_IN_FRAME) :
515                         CHDLC_DFLT_DATA_LEN;
516         }
517
518         /* Set up the interrupt status area */
519         /* Read the CHDLC Configuration and obtain: 
520          *      Ptr to shared memory infor struct
521          * Use this pointer to calculate the value of card->u.c.flags !
522          */
523         mb1->buffer_length = 0;
524         mb1->command = READ_CHDLC_CONFIGURATION;
525         err = sdla_exec(mb1) ? mb1->return_code : CMD_TIMEOUT;
526         if(err != COMMAND_OK) {
527                 if(card->hw.type != SDLA_S514)
528                         enable_irq(card->hw.irq);
529
530                 chdlc_error(card, err, mb1);
531                 return -EIO;
532         }
533
534         if(card->hw.type == SDLA_S514){
535                 card->u.c.flags = (void *)(card->hw.dpmbase +
536                         (((CHDLC_CONFIGURATION_STRUCT *)mb1->data)->
537                         ptr_shared_mem_info_struct));
538         }else{
539                 card->u.c.flags = (void *)(card->hw.dpmbase +
540                         (((CHDLC_CONFIGURATION_STRUCT *)mb1->data)->
541                         ptr_shared_mem_info_struct % SDLA_WINDOWSIZE));
542         }
543
544         flags = card->u.c.flags;
545         
546         /* This is for the ports link state */
547         card->wandev.state = WAN_DUALPORT;
548         card->u.c.state = WAN_DISCONNECTED;
549
550
551         if (!card->wandev.piggyback){   
552                 int err;
553
554                 /* Perform interrupt testing */
555                 err = intr_test(card);
556
557                 if(err || (Intr_test_counter < MAX_INTR_TEST_COUNTER)) { 
558                         printk(KERN_INFO "%s: Interrupt test failed (%i)\n",
559                                         card->devname, Intr_test_counter);
560                         printk(KERN_INFO "%s: Please choose another interrupt\n",
561                                         card->devname);
562                         return -EIO;
563                 }
564                 
565                 printk(KERN_INFO "%s: Interrupt test passed (%i)\n", 
566                                 card->devname, Intr_test_counter);
567                 card->configured = 1;
568         }
569
570         if ((card->tty_opt=conf->tty) == WANOPT_YES){
571 #if defined(LINUX_2_4) || defined(LINUX_2_1)    
572                 int err;
573                 card->tty_minor = conf->tty_minor;
574
575                 /* On ASYNC connections internal clocking 
576                  * is mandatory */
577                 if ((card->u.c.async_mode = conf->tty_mode)){
578                         card->wandev.clocking = 1;
579                 }
580                 err=wanpipe_tty_init(card);
581                 if (err){
582                         return err;
583                 }
584 #else
585                 printk(KERN_INFO "%s: Error: TTY driver is not supported on 2.0.X kernels!\n",
586                                 card->devname); 
587                 return -EINVAL;  
588 #endif
589         }else{
590         
591
592                 if (chdlc_set_intr_mode(card, APP_INT_ON_TIMER)){
593                         printk (KERN_INFO "%s: 
594                                 Failed to set interrupt triggers!\n",
595                                 card->devname);
596                         return -EIO;    
597                 }
598         
599                 /* Mask the Timer interrupt */
600                 flags->interrupt_info_struct.interrupt_permission &= 
601                         ~APP_INT_ON_TIMER;
602         }
603
604         /* If we are using CHDLC in backup mode, this flag will
605          * indicate not to look for IP addresses in config_chdlc()*/
606         card->u.c.backup = conf->backup;
607         
608         printk(KERN_INFO "\n");
609
610         return 0;
611 }
612
613 /******* WAN Device Driver Entry Points *************************************/
614
615 /*============================================================================
616  * Update device status & statistics
617  * This procedure is called when updating the PROC file system and returns
618  * various communications statistics. These statistics are accumulated from 3 
619  * different locations:
620  *      1) The 'if_stats' recorded for the device.
621  *      2) Communication error statistics on the adapter.
622  *      3) CHDLC operational statistics on the adapter.
623  * The board level statistics are read during a timer interrupt. Note that we 
624  * read the error and operational statistics during consecitive timer ticks so
625  * as to minimize the time that we are inside the interrupt handler.
626  *
627  */
628 static int update (wan_device_t* wandev)
629 {
630         sdla_t* card = wandev->private;
631         netdevice_t* dev;
632         volatile chdlc_private_area_t* chdlc_priv_area;
633         SHARED_MEMORY_INFO_STRUCT *flags;
634         unsigned long timeout;
635
636         /* sanity checks */
637         if((wandev == NULL) || (wandev->private == NULL))
638                 return -EFAULT;
639         
640         if(wandev->state == WAN_UNCONFIGURED)
641                 return -ENODEV;
642
643         /* more sanity checks */
644         if(!card->u.c.flags)
645                 return -ENODEV;
646
647         if(test_bit(PERI_CRIT, (void*)&card->wandev.critical))
648                 return -EAGAIN;
649
650         if((dev=card->wandev.dev) == NULL)
651                 return -ENODEV;
652
653         if((chdlc_priv_area=dev->priv) == NULL)
654                 return -ENODEV;
655
656         flags = card->u.c.flags;
657         if(chdlc_priv_area->update_comms_stats){
658                 return -EAGAIN;
659         }
660                         
661         /* we will need 2 timer interrupts to complete the */
662         /* reading of the statistics */
663         chdlc_priv_area->update_comms_stats = 2;
664         flags->interrupt_info_struct.interrupt_permission |= APP_INT_ON_TIMER;
665         chdlc_priv_area->timer_int_enabled = TMR_INT_ENABLED_UPDATE;
666   
667         /* wait a maximum of 1 second for the statistics to be updated */ 
668         timeout = jiffies;
669         for(;;) {
670                 if(chdlc_priv_area->update_comms_stats == 0)
671                         break;
672                 if ((jiffies - timeout) > (1 * HZ)){
673                         chdlc_priv_area->update_comms_stats = 0;
674                         chdlc_priv_area->timer_int_enabled &=
675                                 ~TMR_INT_ENABLED_UPDATE; 
676                         return -EAGAIN;
677                 }
678         }
679
680         return 0;
681 }
682
683
684 /*============================================================================
685  * Create new logical channel.
686  * This routine is called by the router when ROUTER_IFNEW IOCTL is being
687  * handled.
688  * o parse media- and hardware-specific configuration
689  * o make sure that a new channel can be created
690  * o allocate resources, if necessary
691  * o prepare network device structure for registaration.
692  *
693  * Return:      0       o.k.
694  *              < 0     failure (channel will not be created)
695  */
696 static int new_if (wan_device_t* wandev, netdevice_t* dev, wanif_conf_t* conf)
697 {
698         sdla_t* card = wandev->private;
699         chdlc_private_area_t* chdlc_priv_area;
700
701
702         printk(KERN_INFO "%s: Configuring Interface: %s\n",
703                         card->devname, conf->name);
704  
705         if ((conf->name[0] == '\0') || (strlen(conf->name) > WAN_IFNAME_SZ)) {
706                 printk(KERN_INFO "%s: Invalid interface name!\n",
707                         card->devname);
708                 return -EINVAL;
709         }
710                 
711         /* allocate and initialize private data */
712         chdlc_priv_area = kmalloc(sizeof(chdlc_private_area_t), GFP_KERNEL);
713         
714         if(chdlc_priv_area == NULL) 
715                 return -ENOMEM;
716
717         memset(chdlc_priv_area, 0, sizeof(chdlc_private_area_t));
718
719         chdlc_priv_area->card = card; 
720         chdlc_priv_area->common.sk = NULL;
721         chdlc_priv_area->common.func = NULL;    
722
723         /* initialize data */
724         strcpy(card->u.c.if_name, conf->name);
725
726         if(card->wandev.new_if_cnt > 0) {
727                 kfree(chdlc_priv_area);
728                 return -EEXIST;
729         }
730
731         card->wandev.new_if_cnt++;
732
733         chdlc_priv_area->TracingEnabled = 0;
734         chdlc_priv_area->route_status = NO_ROUTE;
735         chdlc_priv_area->route_removed = 0;
736
737         card->u.c.async_mode = conf->async_mode;
738         
739         /* setup for asynchronous mode */
740         if(conf->async_mode) {
741                 printk(KERN_INFO "%s: Configuring for asynchronous mode\n",
742                         wandev->name);
743
744                 if(card->u.c.comm_port == WANOPT_PRI) {
745                         printk(KERN_INFO
746                                 "%s:Asynchronous mode on secondary port only\n",
747                                         wandev->name);
748                         kfree(chdlc_priv_area);
749                         return -EINVAL;
750                 }
751
752                 if(strcmp(conf->usedby, "WANPIPE") == 0) {
753                         printk(KERN_INFO
754                                 "%s: Running in WANIPE Async Mode\n",                                                           wandev->name);
755                         card->u.c.usedby = WANPIPE;
756                 }else{
757                         card->u.c.usedby = API;
758                 }
759
760                 if(!card->wandev.clocking) {
761                         printk(KERN_INFO
762                                 "%s: Asynch. clocking must be 'Internal'\n",
763                                 wandev->name);
764                         kfree(chdlc_priv_area);
765                         return -EINVAL;
766                 }
767
768                 if((card->wandev.bps < MIN_ASY_BAUD_RATE) ||
769                         (card->wandev.bps > MAX_ASY_BAUD_RATE)) {
770                         printk(KERN_INFO "%s: Selected baud rate is invalid.\n",
771                                 wandev->name);
772                         printk(KERN_INFO "Must be between %u and %u bps.\n",
773                                 MIN_ASY_BAUD_RATE, MAX_ASY_BAUD_RATE);
774                         kfree(chdlc_priv_area);
775                         return -EINVAL;
776                 }
777
778                 card->u.c.api_options = 0;
779                 if (conf->asy_data_trans == WANOPT_YES) {
780                         card->u.c.api_options |= ASY_RX_DATA_TRANSPARENT;
781                 }
782                 
783                 card->u.c.protocol_options = 0;
784                 if (conf->rts_hs_for_receive == WANOPT_YES) {
785                         card->u.c.protocol_options |= ASY_RTS_HS_FOR_RX;
786                 }
787                 if (conf->xon_xoff_hs_for_receive == WANOPT_YES) {
788                         card->u.c.protocol_options |= ASY_XON_XOFF_HS_FOR_RX;
789                 }
790                 if (conf->xon_xoff_hs_for_transmit == WANOPT_YES) {
791                         card->u.c.protocol_options |= ASY_XON_XOFF_HS_FOR_TX;
792                 }
793                 if (conf->dcd_hs_for_transmit == WANOPT_YES) {
794                         card->u.c.protocol_options |= ASY_DCD_HS_FOR_TX;
795                 }
796                 if (conf->cts_hs_for_transmit == WANOPT_YES) {
797                         card->u.c.protocol_options |= ASY_CTS_HS_FOR_TX;
798                 }
799
800                 card->u.c.tx_bits_per_char = conf->tx_bits_per_char;
801                 card->u.c.rx_bits_per_char = conf->rx_bits_per_char;
802                 card->u.c.stop_bits = conf->stop_bits;
803                 card->u.c.parity = conf->parity;
804                 card->u.c.break_timer = conf->break_timer;
805                 card->u.c.inter_char_timer = conf->inter_char_timer;
806                 card->u.c.rx_complete_length = conf->rx_complete_length;
807                 card->u.c.xon_char = conf->xon_char;
808
809         } else {        /* setup for synchronous mode */
810
811                 card->u.c.protocol_options = 0;
812                 if (conf->ignore_dcd == WANOPT_YES){
813                         card->u.c.protocol_options |= IGNORE_DCD_FOR_LINK_STAT;
814                 }
815                 if (conf->ignore_cts == WANOPT_YES){
816                         card->u.c.protocol_options |= IGNORE_CTS_FOR_LINK_STAT;
817                 }
818
819                 if (conf->ignore_keepalive == WANOPT_YES) {
820                         card->u.c.protocol_options |=
821                                 IGNORE_KPALV_FOR_LINK_STAT;
822                         card->u.c.kpalv_tx  = MIN_Tx_KPALV_TIMER; 
823                         card->u.c.kpalv_rx  = MIN_Rx_KPALV_TIMER; 
824                         card->u.c.kpalv_err = MIN_KPALV_ERR_TOL; 
825
826                 } else {   /* Do not ignore keepalives */
827                         card->u.c.kpalv_tx =
828                                 ((conf->keepalive_tx_tmr - MIN_Tx_KPALV_TIMER)
829                                 >= 0) ?
830                                 min(conf->keepalive_tx_tmr,MAX_Tx_KPALV_TIMER) :
831                                 DEFAULT_Tx_KPALV_TIMER;
832
833                         card->u.c.kpalv_rx =
834                                 ((conf->keepalive_rx_tmr - MIN_Rx_KPALV_TIMER)
835                                 >= 0) ?
836                                 min(conf->keepalive_rx_tmr,MAX_Rx_KPALV_TIMER) :
837                                 DEFAULT_Rx_KPALV_TIMER;
838
839                         card->u.c.kpalv_err =
840                                 ((conf->keepalive_err_margin-MIN_KPALV_ERR_TOL)
841                                 >= 0) ?
842                                 min(conf->keepalive_err_margin,
843                                 MAX_KPALV_ERR_TOL) : 
844                                 DEFAULT_KPALV_ERR_TOL;
845                 }
846
847                 /* Setup slarp timer to control delay between slarps */
848                 card->u.c.slarp_timer = 
849                         ((conf->slarp_timer - MIN_SLARP_REQ_TIMER) >= 0) ?
850                         min (conf->slarp_timer, MAX_SLARP_REQ_TIMER) :
851                         DEFAULT_SLARP_REQ_TIMER;
852
853 #ifdef LINUX_2_0
854                 if (card->u.c.slarp_timer){
855                         printk(KERN_INFO 
856                                 "%s: Error: Dynamic IP support not available for 2.0.X kernels\n",
857                                         card->devname);
858                         printk(KERN_INFO "%s:        Defaulting to Static IP addressing\n",
859                                                 card->devname);
860                 }
861                 card->u.c.slarp_timer=0;
862 #endif  
863
864
865                 if (conf->hdlc_streaming == WANOPT_YES) {
866                         printk(KERN_INFO "%s: Enabling HDLC STREAMING Mode\n",
867                                 wandev->name);
868                         card->u.c.protocol_options = HDLC_STREAMING_MODE;
869                 }
870
871                 if ((chdlc_priv_area->true_if_encoding = conf->true_if_encoding) == WANOPT_YES){
872                         printk(KERN_INFO 
873                                 "%s: Enabling, true interface type encoding.\n",
874                                 card->devname);
875                 }
876                 
877                 /* Setup wanpipe as a router (WANPIPE) or as an API */
878                 if( strcmp(conf->usedby, "WANPIPE") == 0) {
879
880                         printk(KERN_INFO "%s: Running in WANPIPE mode!\n",
881                                 wandev->name);
882                         card->u.c.usedby = WANPIPE;
883
884                         /* Option to bring down the interface when 
885                          * the link goes down */
886                         if (conf->if_down){
887                                 set_bit(DYN_OPT_ON,&chdlc_priv_area->interface_down);
888                                 printk(KERN_INFO 
889                                  "%s: Dynamic interface configuration enabled\n",
890                                    card->devname);
891                         } 
892
893                 } else if( strcmp(conf->usedby, "API") == 0) {
894 #if defined(LINUX_2_1) || defined(LINUX_2_4) 
895                         card->u.c.usedby = API;
896                         printk(KERN_INFO "%s: Running in API mode !\n",
897                                 wandev->name);
898 #else
899                         printk(KERN_INFO "%s: API Mode is not supported for kernels lower than 2.2.X!\n",
900                                 wandev->name);
901                         printk(KERN_INFO "%s: Please upgrade to a 2.2.X kernel fro the API support\n",
902                                 wandev->name);
903                         kfree(chdlc_priv_area);
904                         return -EINVAL;
905 #endif
906                 }
907         }
908
909 #if defined(LINUX_2_1) || defined(LINUX_2_4)
910         /* Tells us that if this interface is a
911          * gateway or not */
912         if ((chdlc_priv_area->gateway = conf->gateway) == WANOPT_YES){
913                 printk(KERN_INFO "%s: Interface %s is set as a gateway.\n",
914                         card->devname,card->u.c.if_name);
915         }
916 #endif
917
918         /* Get Multicast Information */
919         chdlc_priv_area->mc = conf->mc;
920
921         /* prepare network device data space for registration */
922 #ifdef LINUX_2_4
923         strcpy(dev->name,card->u.c.if_name);
924 #else
925         dev->name = (char *)kmalloc(strlen(card->u.c.if_name) + 2, GFP_KERNEL); 
926         sprintf(dev->name, "%s", card->u.c.if_name);
927 #endif
928
929         dev->init = &if_init;
930         dev->priv = chdlc_priv_area;
931
932         /* Initialize the polling task routine */
933 #ifndef LINUX_2_4
934         chdlc_priv_area->poll_task.next = NULL;
935 #endif
936         chdlc_priv_area->poll_task.sync=0;
937         chdlc_priv_area->poll_task.routine = (void*)(void*)chdlc_poll;
938         chdlc_priv_area->poll_task.data = dev;
939
940         /* Initialize the polling delay timer */
941         init_timer(&chdlc_priv_area->poll_delay_timer);
942         chdlc_priv_area->poll_delay_timer.data = (unsigned long)dev;
943         chdlc_priv_area->poll_delay_timer.function = chdlc_poll_delay;
944         
945         printk(KERN_INFO "\n");
946
947         return 0;
948 }
949
950
951 /****** Network Device Interface ********************************************/
952
953 /*============================================================================
954  * Initialize Linux network interface.
955  *
956  * This routine is called only once for each interface, during Linux network
957  * interface registration.  Returning anything but zero will fail interface
958  * registration.
959  */
960 static int if_init (netdevice_t* dev)
961         {
962         chdlc_private_area_t* chdlc_priv_area = dev->priv;
963         sdla_t* card = chdlc_priv_area->card;
964         wan_device_t* wandev = &card->wandev;
965 #ifdef LINUX_2_0
966         int i;
967 #endif
968
969         /* Initialize device driver entry points */
970         dev->open               = &if_open;
971         dev->stop               = &if_close;
972         dev->hard_header        = &if_header;
973         dev->rebuild_header     = &if_rebuild_hdr;
974         dev->hard_start_xmit    = &if_send;
975         dev->get_stats          = &if_stats;
976 #ifdef LINUX_2_4
977         dev->tx_timeout         = &if_tx_timeout;
978         dev->watchdog_timeo     = TX_TIMEOUT;
979 #endif
980
981         
982         /* Initialize media-specific parameters */
983         dev->flags              |= IFF_POINTOPOINT;
984         dev->flags              |= IFF_NOARP;
985
986         /* Enable Mulitcasting if user selected */
987         if (chdlc_priv_area->mc == WANOPT_YES){
988                 dev->flags      |= IFF_MULTICAST;
989         }
990         
991 #ifdef LINUX_2_0
992         dev->family             = AF_INET;
993 #endif  
994
995         if (chdlc_priv_area->true_if_encoding){
996 #if defined(LINUX_2_1) || defined(LINUX_2_4)
997                 dev->type       = ARPHRD_HDLC; /* This breaks the tcpdump */
998 #else
999                 dev->type       = ARPHRD_PPP;
1000 #endif
1001         }else{
1002                 dev->type       = ARPHRD_PPP;
1003         }
1004         
1005         dev->mtu                = card->wandev.mtu;
1006         /* for API usage, add the API header size to the requested MTU size */
1007         if(card->u.c.usedby == API) {
1008                 dev->mtu += sizeof(api_tx_hdr_t);
1009         }
1010  
1011         dev->hard_header_len    = CHDLC_HDR_LEN;
1012
1013         /* Initialize hardware parameters */
1014         dev->irq        = wandev->irq;
1015         dev->dma        = wandev->dma;
1016         dev->base_addr  = wandev->ioport;
1017         dev->mem_start  = wandev->maddr;
1018         dev->mem_end    = wandev->maddr + wandev->msize - 1;
1019
1020         /* Set transmit buffer queue length 
1021          * If too low packets will not be retransmitted 
1022          * by stack.
1023          */
1024         dev->tx_queue_len = 100;
1025    
1026         /* Initialize socket buffers */
1027 #if !defined(LINUX_2_1) && !defined(LINUX_2_4)
1028         for (i = 0; i < DEV_NUMBUFFS; ++i)
1029                 skb_queue_head_init(&dev->buffs[i]);
1030 #endif
1031         return 0;
1032 }
1033
1034 /*============================================================================
1035  * Open network interface.
1036  * o enable communications and interrupts.
1037  * o prevent module from unloading by incrementing use count
1038  *
1039  * Return 0 if O.k. or errno.
1040  */
1041 static int if_open (netdevice_t* dev)
1042 {
1043         chdlc_private_area_t* chdlc_priv_area = dev->priv;
1044         sdla_t* card = chdlc_priv_area->card;
1045         struct timeval tv;
1046         int err = 0;
1047
1048         /* Only one open per interface is allowed */
1049
1050         if (is_dev_running(dev))
1051                 return -EBUSY;
1052
1053 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1054         /* Initialize the task queue */
1055         chdlc_priv_area->tq_working=0;
1056
1057 #ifndef LINUX_2_4
1058         chdlc_priv_area->common.wanpipe_task.next = NULL;
1059 #endif
1060         chdlc_priv_area->common.wanpipe_task.sync = 0;
1061         chdlc_priv_area->common.wanpipe_task.routine = (void *)(void *)chdlc_bh;
1062         chdlc_priv_area->common.wanpipe_task.data = dev;
1063
1064         /* Allocate and initialize BH circular buffer */
1065         /* Add 1 to MAX_BH_BUFF so we don't have test with (MAX_BH_BUFF-1) */
1066         chdlc_priv_area->bh_head = kmalloc((sizeof(bh_data_t)*(MAX_BH_BUFF+1)),GFP_ATOMIC);
1067         memset(chdlc_priv_area->bh_head,0,(sizeof(bh_data_t)*(MAX_BH_BUFF+1)));
1068         atomic_set(&chdlc_priv_area->bh_buff_used, 0);
1069 #endif
1070  
1071         do_gettimeofday(&tv);
1072         chdlc_priv_area->router_start_time = tv.tv_sec;
1073
1074 #ifdef LINUX_2_4
1075         netif_start_queue(dev);
1076 #else
1077         dev->interrupt = 0;
1078         dev->tbusy = 0;
1079         dev->start = 1;
1080 #endif
1081
1082         wanpipe_open(card);
1083
1084         /* TTY is configured during wanpipe_set_termios
1085          * call, not here */
1086         if (card->tty_opt)
1087                 return err;
1088         
1089         set_bit(0,&chdlc_priv_area->config_chdlc);
1090         chdlc_priv_area->config_chdlc_timeout=jiffies;
1091         del_timer(&chdlc_priv_area->poll_delay_timer);
1092
1093         /* Start the CHDLC configuration after 1sec delay.
1094          * This will give the interface initilization time
1095          * to finish its configuration */
1096         chdlc_priv_area->poll_delay_timer.expires=jiffies+HZ;
1097         add_timer(&chdlc_priv_area->poll_delay_timer);
1098         return err;
1099 }
1100
1101 /*============================================================================
1102  * Close network interface.
1103  * o if this is the last close, then disable communications and interrupts.
1104  * o reset flags.
1105  */
1106 static int if_close (netdevice_t* dev)
1107 {
1108         chdlc_private_area_t* chdlc_priv_area = dev->priv;
1109         sdla_t* card = chdlc_priv_area->card;
1110
1111 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1112
1113         if (chdlc_priv_area->bh_head){
1114                 int i;
1115                 struct sk_buff *skb;
1116         
1117                 for (i=0; i<(MAX_BH_BUFF+1); i++){
1118                         skb = ((bh_data_t *)&chdlc_priv_area->bh_head[i])->skb;
1119                         if (skb != NULL){
1120                                 wan_dev_kfree_skb(skb, FREE_READ);
1121                         }
1122                 }
1123                 kfree(chdlc_priv_area->bh_head);
1124                 chdlc_priv_area->bh_head=NULL;
1125         }
1126 #endif
1127
1128         stop_net_queue(dev);
1129 #ifndef LINUX_2_4
1130         dev->start=0;
1131 #endif
1132         wanpipe_close(card);
1133         del_timer(&chdlc_priv_area->poll_delay_timer);
1134         return 0;
1135 }
1136
1137 static void disable_comm (sdla_t *card)
1138 {
1139         SHARED_MEMORY_INFO_STRUCT *flags = card->u.c.flags;
1140         
1141         if (card->u.c.comm_enabled){
1142                 chdlc_disable_comm_shutdown (card);
1143         }else{
1144                 flags->interrupt_info_struct.interrupt_permission = 0;  
1145         }
1146
1147 #if defined(LINUX_2_4) || defined(LINUX_2_1)    
1148         if (!tty_init_cnt)
1149                 return;
1150
1151         if (card->tty_opt){
1152                 struct serial_state * state;
1153                 if (!(--tty_init_cnt)){
1154                         int e1,e2;
1155                         *serial_driver.refcount=0;
1156                         
1157                         if ((e1 = tty_unregister_driver(&serial_driver)))
1158                                 printk("SERIAL: failed to unregister serial driver (%d)\n",
1159                                        e1);
1160                         if ((e2 = tty_unregister_driver(&callout_driver)))
1161                                 printk("SERIAL: failed to unregister callout driver (%d)\n", 
1162                                        e2);
1163                         printk(KERN_INFO "%s: Unregistering TTY Driver, Major %i\n",
1164                                         card->devname,WAN_TTY_MAJOR);
1165                 }
1166                 card->tty=NULL;
1167                 tty_card_map[card->tty_minor]=NULL;
1168                 state = &rs_table[card->tty_minor];
1169                 memset(state,0,sizeof(state));
1170         }
1171 #endif
1172         return;
1173 }
1174
1175
1176 /*============================================================================
1177  * Build media header.
1178  *
1179  * The trick here is to put packet type (Ethertype) into 'protocol' field of
1180  * the socket buffer, so that we don't forget it.  If packet type is not
1181  * supported, set skb->protocol to 0 and discard packet later.
1182  *
1183  * Return:      media header length.
1184  */
1185 static int if_header (struct sk_buff* skb, netdevice_t* dev,
1186         unsigned short type, void* daddr, void* saddr, unsigned len)
1187 {
1188         skb->protocol = htons(type);
1189
1190         return CHDLC_HDR_LEN;
1191 }
1192
1193
1194 #ifdef LINUX_2_4
1195 /*============================================================================
1196  * Handle transmit timeout event from netif watchdog
1197  */
1198 static void if_tx_timeout (netdevice_t *dev)
1199 {
1200         chdlc_private_area_t* chan = dev->priv;
1201         sdla_t *card = chan->card;
1202         
1203         /* If our device stays busy for at least 5 seconds then we will
1204          * kick start the device by making dev->tbusy = 0.  We expect
1205          * that our device never stays busy more than 5 seconds. So this                 
1206          * is only used as a last resort.
1207          */
1208
1209         ++card->wandev.stats.collisions;
1210
1211         printk (KERN_INFO "%s: Transmit timed out on %s\n", card->devname,dev->name);
1212         netif_wake_queue (dev);
1213 }
1214 #endif
1215
1216
1217
1218 /*============================================================================
1219  * Re-build media header.
1220  *
1221  * Return:      1       physical address resolved.
1222  *              0       physical address not resolved
1223  */
1224 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1225 static int if_rebuild_hdr (struct sk_buff *skb)
1226 {
1227         return 1;
1228 }
1229 #else
1230 static int if_rebuild_hdr (void* hdr, netdevice_t* dev, unsigned long raddr,
1231                            struct sk_buff* skb)
1232 {
1233         return 1;
1234 }
1235 #endif
1236
1237 /*============================================================================
1238  * Send a packet on a network interface.
1239  * o set tbusy flag (marks start of the transmission) to block a timer-based
1240  *   transmit from overlapping.
1241  * o check link state. If link is not up, then drop the packet.
1242  * o execute adapter send command.
1243  * o free socket buffer
1244  *
1245  * Return:      0       complete (socket buffer must be freed)
1246  *              non-0   packet may be re-transmitted (tbusy must be set)
1247  *
1248  * Notes:
1249  * 1. This routine is called either by the protocol stack or by the "net
1250  *    bottom half" (with interrupts enabled).
1251  * 2. Setting tbusy flag will inhibit further transmit requests from the
1252  *    protocol stack and can be used for flow control with protocol layer.
1253  */
1254 static int if_send (struct sk_buff* skb, netdevice_t* dev)
1255 {
1256         chdlc_private_area_t *chdlc_priv_area = dev->priv;
1257         sdla_t *card = chdlc_priv_area->card;
1258         SHARED_MEMORY_INFO_STRUCT *flags = card->u.c.flags;
1259         INTERRUPT_INFORMATION_STRUCT *chdlc_int = &flags->interrupt_info_struct;
1260         int udp_type = 0;
1261         unsigned long smp_flags;
1262         int err=0;
1263
1264 #ifdef LINUX_2_4
1265         netif_stop_queue(dev);
1266 #endif
1267         
1268         if (skb == NULL){
1269                 /* If we get here, some higher layer thinks we've missed an
1270                  * tx-done interrupt.
1271                  */
1272                 printk(KERN_INFO "%s: interface %s got kicked!\n",
1273                         card->devname, dev->name);
1274
1275                 wake_net_dev(dev);
1276                 return 0;
1277         }
1278
1279 #ifndef LINUX_2_4
1280         if (dev->tbusy){
1281
1282                 /* If our device stays busy for at least 5 seconds then we will
1283                  * kick start the device by making dev->tbusy = 0.  We expect 
1284                  * that our device never stays busy more than 5 seconds. So this
1285                  * is only used as a last resort. 
1286                  */
1287                 ++card->wandev.stats.collisions;
1288                 if((jiffies - chdlc_priv_area->tick_counter) < (5 * HZ)) {
1289                         return 1;
1290                 }
1291
1292                 printk (KERN_INFO "%s: Transmit timeout !\n",
1293                         card->devname);
1294
1295                 /* unbusy the interface */
1296                 clear_bit(0,&dev->tbusy);
1297         }
1298 #endif
1299
1300         if (ntohs(skb->protocol) != htons(PVC_PROT)){
1301
1302                 /* check the udp packet type */
1303                 
1304                 udp_type = udp_pkt_type(skb, card);
1305
1306                 if (udp_type == UDP_CPIPE_TYPE){
1307                         if(store_udp_mgmt_pkt(UDP_PKT_FRM_STACK, card, skb, dev,
1308                                 chdlc_priv_area)){
1309                                 chdlc_int->interrupt_permission |=
1310                                         APP_INT_ON_TIMER;
1311                         }
1312                         start_net_queue(dev);
1313                         return 0;
1314                 }
1315
1316                 /* check to see if the source IP address is a broadcast or */
1317                 /* multicast IP address */
1318                 if(chk_bcast_mcast_addr(card, dev, skb)){
1319                         ++card->wandev.stats.tx_dropped;
1320                         wan_dev_kfree_skb(skb,FREE_WRITE);
1321                         start_net_queue(dev);
1322                         return 0;
1323                 }
1324         }
1325
1326         /* Lock the 508 Card: SMP is supported */
1327         if(card->hw.type != SDLA_S514){
1328                 s508_lock(card,&smp_flags);
1329         } 
1330
1331         if(test_and_set_bit(SEND_CRIT, (void*)&card->wandev.critical)) {
1332         
1333                 printk(KERN_INFO "%s: Critical in if_send: %lx\n",
1334                                         card->wandev.name,card->wandev.critical);
1335                 ++card->wandev.stats.tx_dropped;
1336                 start_net_queue(dev);
1337                 goto if_send_exit_crit;
1338         }
1339
1340         if(card->u.c.state != WAN_CONNECTED){
1341                 ++card->wandev.stats.tx_dropped;
1342                 start_net_queue(dev);
1343                 
1344         }else if(!skb->protocol){
1345                 ++card->wandev.stats.tx_errors;
1346                 start_net_queue(dev);
1347                 
1348         }else {
1349                 void* data = skb->data;
1350                 unsigned len = skb->len;
1351                 unsigned char attr;
1352
1353                 /* If it's an API packet pull off the API
1354                  * header. Also check that the packet size
1355                  * is larger than the API header
1356                  */
1357                 if (card->u.c.usedby == API){
1358                         api_tx_hdr_t* api_tx_hdr;
1359
1360                         /* discard the frame if we are configured for */
1361                         /* 'receive only' mode or if there is no data */
1362                         if (card->u.c.receive_only ||
1363                                 (len <= sizeof(api_tx_hdr_t))) {
1364                                 
1365                                 ++card->wandev.stats.tx_dropped;
1366                                 start_net_queue(dev);
1367                                 goto if_send_exit_crit;
1368                         }
1369                                 
1370                         api_tx_hdr = (api_tx_hdr_t *)data;
1371                         attr = api_tx_hdr->attr;
1372                         data += sizeof(api_tx_hdr_t);
1373                         len -= sizeof(api_tx_hdr_t);
1374                 }
1375
1376                 if(chdlc_send(card, data, len)) {
1377                         stop_net_queue(dev);
1378                 }else{
1379                         ++card->wandev.stats.tx_packets;
1380 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1381                         card->wandev.stats.tx_bytes += len;
1382 #endif
1383                         
1384                         start_net_queue(dev);
1385                         
1386 #ifdef LINUX_2_4
1387                         dev->trans_start = jiffies;
1388 #endif
1389                 }       
1390         }
1391
1392 if_send_exit_crit:
1393         
1394         if (!(err=is_queue_stopped(dev))) {
1395                 wan_dev_kfree_skb(skb, FREE_WRITE);
1396         }else{
1397                 chdlc_priv_area->tick_counter = jiffies;
1398                 chdlc_int->interrupt_permission |= APP_INT_ON_TX_FRAME;
1399         }
1400
1401         clear_bit(SEND_CRIT, (void*)&card->wandev.critical);
1402         if(card->hw.type != SDLA_S514){
1403                 s508_unlock(card,&smp_flags);
1404         }
1405         
1406         return err;
1407 }
1408
1409
1410 /*============================================================================
1411  * Check to see if the packet to be transmitted contains a broadcast or
1412  * multicast source IP address.
1413  */
1414
1415 static int chk_bcast_mcast_addr(sdla_t *card, netdevice_t* dev,
1416                                 struct sk_buff *skb)
1417 {
1418         u32 src_ip_addr;
1419         u32 broadcast_ip_addr = 0;
1420 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1421         struct in_device *in_dev;
1422 #endif
1423         /* read the IP source address from the outgoing packet */
1424         src_ip_addr = *(u32 *)(skb->data + 12);
1425
1426         /* read the IP broadcast address for the device */
1427 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1428         in_dev = dev->ip_ptr;
1429         if(in_dev != NULL) {
1430                 struct in_ifaddr *ifa= in_dev->ifa_list;
1431                 if(ifa != NULL)
1432                         broadcast_ip_addr = ifa->ifa_broadcast;
1433                 else
1434                         return 0;
1435         }
1436 #else
1437         broadcast_ip_addr = dev->pa_brdaddr;
1438 #endif
1439  
1440         /* check if the IP Source Address is a Broadcast address */
1441         if((dev->flags & IFF_BROADCAST) && (src_ip_addr == broadcast_ip_addr)) {
1442                 printk(KERN_INFO "%s: Broadcast Source Address silently discarded\n",
1443                                 card->devname);
1444                 return 1;
1445         } 
1446
1447         /* check if the IP Source Address is a Multicast address */
1448         if((ntohl(src_ip_addr) >= 0xE0000001) &&
1449                 (ntohl(src_ip_addr) <= 0xFFFFFFFE)) {
1450                 printk(KERN_INFO "%s: Multicast Source Address silently discarded\n",
1451                                 card->devname);
1452                 return 1;
1453         }
1454
1455         return 0;
1456 }
1457
1458
1459 /*============================================================================
1460  * Reply to UDP Management system.
1461  * Return length of reply.
1462  */
1463 static int reply_udp( unsigned char *data, unsigned int mbox_len )
1464 {
1465
1466         unsigned short len, udp_length, temp, ip_length;
1467         unsigned long ip_temp;
1468         int even_bound = 0;
1469         chdlc_udp_pkt_t *c_udp_pkt = (chdlc_udp_pkt_t *)data;
1470          
1471         /* Set length of packet */
1472         len = sizeof(ip_pkt_t)+ 
1473               sizeof(udp_pkt_t)+
1474               sizeof(wp_mgmt_t)+
1475               sizeof(cblock_t)+
1476               sizeof(trace_info_t)+ 
1477               mbox_len;
1478
1479         /* fill in UDP reply */
1480         c_udp_pkt->wp_mgmt.request_reply = UDPMGMT_REPLY;
1481    
1482         /* fill in UDP length */
1483         udp_length = sizeof(udp_pkt_t)+ 
1484                      sizeof(wp_mgmt_t)+
1485                      sizeof(cblock_t)+
1486                      sizeof(trace_info_t)+
1487                      mbox_len; 
1488
1489         /* put it on an even boundary */
1490         if ( udp_length & 0x0001 ) {
1491                 udp_length += 1;
1492                 len += 1;
1493                 even_bound = 1;
1494         }  
1495
1496         temp = (udp_length<<8)|(udp_length>>8);
1497         c_udp_pkt->udp_pkt.udp_length = temp;
1498                  
1499         /* swap UDP ports */
1500         temp = c_udp_pkt->udp_pkt.udp_src_port;
1501         c_udp_pkt->udp_pkt.udp_src_port = 
1502                         c_udp_pkt->udp_pkt.udp_dst_port; 
1503         c_udp_pkt->udp_pkt.udp_dst_port = temp;
1504
1505         /* add UDP pseudo header */
1506         temp = 0x1100;
1507         *((unsigned short *)(c_udp_pkt->data+mbox_len+even_bound)) = temp;      
1508         temp = (udp_length<<8)|(udp_length>>8);
1509         *((unsigned short *)(c_udp_pkt->data+mbox_len+even_bound+2)) = temp;
1510
1511                  
1512         /* calculate UDP checksum */
1513         c_udp_pkt->udp_pkt.udp_checksum = 0;
1514         c_udp_pkt->udp_pkt.udp_checksum = calc_checksum(&data[UDP_OFFSET],udp_length+UDP_OFFSET);
1515
1516         /* fill in IP length */
1517         ip_length = len;
1518         temp = (ip_length<<8)|(ip_length>>8);
1519         c_udp_pkt->ip_pkt.total_length = temp;
1520   
1521         /* swap IP addresses */
1522         ip_temp = c_udp_pkt->ip_pkt.ip_src_address;
1523         c_udp_pkt->ip_pkt.ip_src_address = c_udp_pkt->ip_pkt.ip_dst_address;
1524         c_udp_pkt->ip_pkt.ip_dst_address = ip_temp;
1525
1526         /* fill in IP checksum */
1527         c_udp_pkt->ip_pkt.hdr_checksum = 0;
1528         c_udp_pkt->ip_pkt.hdr_checksum = calc_checksum(data,sizeof(ip_pkt_t));
1529
1530         return len;
1531
1532 } /* reply_udp */
1533
1534 unsigned short calc_checksum (char *data, int len)
1535 {
1536         unsigned short temp; 
1537         unsigned long sum=0;
1538         int i;
1539
1540         for( i = 0; i <len; i+=2 ) {
1541                 memcpy(&temp,&data[i],2);
1542                 sum += (unsigned long)temp;
1543         }
1544
1545         while (sum >> 16 ) {
1546                 sum = (sum & 0xffffUL) + (sum >> 16);
1547         }
1548
1549         temp = (unsigned short)sum;
1550         temp = ~temp;
1551
1552         if( temp == 0 ) 
1553                 temp = 0xffff;
1554
1555         return temp;    
1556 }
1557
1558
1559 /*============================================================================
1560  * Get ethernet-style interface statistics.
1561  * Return a pointer to struct enet_statistics.
1562  */
1563 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1564 static struct net_device_stats* if_stats (netdevice_t* dev)
1565 {
1566         sdla_t *my_card;
1567         chdlc_private_area_t* chdlc_priv_area;
1568
1569         if ((chdlc_priv_area=dev->priv) == NULL)
1570                 return NULL;
1571
1572         my_card = chdlc_priv_area->card;
1573         return &my_card->wandev.stats; 
1574 }
1575 #else
1576 static struct enet_statistics* if_stats (netdevice_t* dev)
1577 {
1578         sdla_t *my_card;
1579         chdlc_private_area_t* chdlc_priv_area = dev->priv;
1580
1581         if ((chdlc_priv_area=dev->priv) == NULL)
1582                 return NULL;
1583
1584         my_card = chdlc_priv_area->card;
1585         return &my_card->wandev.stats;
1586 }
1587 #endif
1588
1589 /****** Cisco HDLC Firmware Interface Functions *******************************/
1590
1591 /*============================================================================
1592  * Read firmware code version.
1593  *      Put code version as ASCII string in str. 
1594  */
1595 static int chdlc_read_version (sdla_t* card, char* str)
1596 {
1597         CHDLC_MAILBOX_STRUCT* mb = card->mbox;
1598         int len;
1599         char err;
1600         mb->buffer_length = 0;
1601         mb->command = READ_CHDLC_CODE_VERSION;
1602         err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
1603
1604         if(err != COMMAND_OK) {
1605                 chdlc_error(card,err,mb);
1606         }
1607         else if (str) {  /* is not null */
1608                 len = mb->buffer_length;
1609                 memcpy(str, mb->data, len);
1610                 str[len] = '\0';
1611         }
1612         return (err);
1613 }
1614
1615 /*-----------------------------------------------------------------------------
1616  *  Configure CHDLC firmware.
1617  */
1618 static int chdlc_configure (sdla_t* card, void* data)
1619 {
1620         int err;
1621         CHDLC_MAILBOX_STRUCT *mailbox = card->mbox;
1622         int data_length = sizeof(CHDLC_CONFIGURATION_STRUCT);
1623         
1624         mailbox->buffer_length = data_length;  
1625         memcpy(mailbox->data, data, data_length);
1626         mailbox->command = SET_CHDLC_CONFIGURATION;
1627         err = sdla_exec(mailbox) ? mailbox->return_code : CMD_TIMEOUT;
1628         
1629         if (err != COMMAND_OK) chdlc_error (card, err, mailbox);
1630                            
1631         return err;
1632 }
1633
1634
1635 /*============================================================================
1636  * Set interrupt mode -- HDLC Version.
1637  */
1638
1639 static int chdlc_set_intr_mode (sdla_t* card, unsigned mode)
1640 {
1641         CHDLC_MAILBOX_STRUCT* mb = card->mbox;
1642         CHDLC_INT_TRIGGERS_STRUCT* int_data =
1643                  (CHDLC_INT_TRIGGERS_STRUCT *)mb->data;
1644         int err;
1645
1646         int_data->CHDLC_interrupt_triggers      = mode;
1647         int_data->IRQ                           = card->hw.irq;
1648         int_data->interrupt_timer               = 1;
1649    
1650         mb->buffer_length = sizeof(CHDLC_INT_TRIGGERS_STRUCT);
1651         mb->command = SET_CHDLC_INTERRUPT_TRIGGERS;
1652         err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
1653         if (err != COMMAND_OK)
1654                 chdlc_error (card, err, mb);
1655         return err;
1656 }
1657
1658
1659 /*===========================================================
1660  * chdlc_disable_comm_shutdown
1661  *
1662  * Shutdown() disables the communications. We must
1663  * have a sparate functions, because we must not
1664  * call chdlc_error() hander since the private
1665  * area has already been replaced */
1666
1667 static int chdlc_disable_comm_shutdown (sdla_t *card)
1668 {
1669         CHDLC_MAILBOX_STRUCT* mb = card->mbox;
1670         CHDLC_INT_TRIGGERS_STRUCT* int_data =
1671                  (CHDLC_INT_TRIGGERS_STRUCT *)mb->data;
1672         int err;
1673
1674         /* Disable Interrutps */
1675         int_data->CHDLC_interrupt_triggers      = 0;
1676         int_data->IRQ                           = card->hw.irq;
1677         int_data->interrupt_timer               = 1;
1678    
1679         mb->buffer_length = sizeof(CHDLC_INT_TRIGGERS_STRUCT);
1680         mb->command = SET_CHDLC_INTERRUPT_TRIGGERS;
1681         err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
1682
1683         /* Disable Communications */
1684
1685         if (card->u.c.async_mode) {
1686                 mb->command = DISABLE_ASY_COMMUNICATIONS;
1687         }else{
1688                 mb->command = DISABLE_CHDLC_COMMUNICATIONS;
1689         }
1690         
1691         mb->buffer_length = 0;
1692         err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
1693         
1694         card->u.c.comm_enabled = 0;
1695         
1696         return 0;
1697 }
1698
1699 /*============================================================================
1700  * Enable communications.
1701  */
1702
1703 static int chdlc_comm_enable (sdla_t* card)
1704 {
1705         int err;
1706         CHDLC_MAILBOX_STRUCT* mb = card->mbox;
1707
1708         mb->buffer_length = 0;
1709         mb->command = ENABLE_CHDLC_COMMUNICATIONS;
1710         err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
1711         if (err != COMMAND_OK)
1712                 chdlc_error(card, err, mb);
1713         else
1714                 card->u.c.comm_enabled = 1;
1715         
1716         return err;
1717 }
1718
1719 /*============================================================================
1720  * Read communication error statistics.
1721  */
1722 static int chdlc_read_comm_err_stats (sdla_t* card)
1723 {
1724         int err;
1725         CHDLC_MAILBOX_STRUCT* mb = card->mbox;
1726
1727         mb->buffer_length = 0;
1728         mb->command = READ_COMMS_ERROR_STATS;
1729         err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
1730         if (err != COMMAND_OK)
1731                 chdlc_error(card,err,mb);
1732         return err;
1733 }
1734
1735
1736 /*============================================================================
1737  * Read CHDLC operational statistics.
1738  */
1739 static int chdlc_read_op_stats (sdla_t* card)
1740 {
1741         int err;
1742         CHDLC_MAILBOX_STRUCT* mb = card->mbox;
1743
1744         mb->buffer_length = 0;
1745         mb->command = READ_CHDLC_OPERATIONAL_STATS;
1746         err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
1747         if (err != COMMAND_OK)
1748                 chdlc_error(card,err,mb);
1749         return err;
1750 }
1751
1752
1753 /*============================================================================
1754  * Update communications error and general packet statistics.
1755  */
1756 static int update_comms_stats(sdla_t* card,
1757         chdlc_private_area_t* chdlc_priv_area)
1758 {
1759         CHDLC_MAILBOX_STRUCT* mb = card->mbox;
1760         COMMS_ERROR_STATS_STRUCT* err_stats;
1761         CHDLC_OPERATIONAL_STATS_STRUCT *op_stats;
1762
1763         /* on the first timer interrupt, read the comms error statistics */
1764         if(chdlc_priv_area->update_comms_stats == 2) {
1765                 if(chdlc_read_comm_err_stats(card))
1766                         return 1;
1767                 err_stats = (COMMS_ERROR_STATS_STRUCT *)mb->data;
1768                 card->wandev.stats.rx_over_errors = 
1769                                 err_stats->Rx_overrun_err_count;
1770                 card->wandev.stats.rx_crc_errors = 
1771                                 err_stats->CRC_err_count;
1772                 card->wandev.stats.rx_frame_errors = 
1773                                 err_stats->Rx_abort_count;
1774                 card->wandev.stats.rx_fifo_errors = 
1775                                 err_stats->Rx_dis_pri_bfrs_full_count; 
1776                 card->wandev.stats.rx_missed_errors =
1777                                 card->wandev.stats.rx_fifo_errors;
1778                 card->wandev.stats.tx_aborted_errors =
1779                                 err_stats->sec_Tx_abort_count;
1780         }
1781
1782         /* on the second timer interrupt, read the operational statistics */
1783         else {
1784                 if(chdlc_read_op_stats(card))
1785                         return 1;
1786                 op_stats = (CHDLC_OPERATIONAL_STATS_STRUCT *)mb->data;
1787                 card->wandev.stats.rx_length_errors =
1788                         (op_stats->Rx_Data_discard_short_count +
1789                         op_stats->Rx_Data_discard_long_count);
1790         }
1791
1792         return 0;
1793 }
1794
1795 /*============================================================================
1796  * Send packet.
1797  *      Return: 0 - o.k.
1798  *              1 - no transmit buffers available
1799  */
1800 static int chdlc_send (sdla_t* card, void* data, unsigned len)
1801 {
1802         CHDLC_DATA_TX_STATUS_EL_STRUCT *txbuf = card->u.c.txbuf;
1803
1804         if (txbuf->opp_flag)
1805                 return 1;
1806         
1807         sdla_poke(&card->hw, txbuf->ptr_data_bfr, data, len);
1808
1809         txbuf->frame_length = len;
1810         txbuf->opp_flag = 1;            /* start transmission */
1811         
1812         /* Update transmit buffer control fields */
1813         card->u.c.txbuf = ++txbuf;
1814         
1815         if ((void*)txbuf > card->u.c.txbuf_last)
1816                 card->u.c.txbuf = card->u.c.txbuf_base;
1817         
1818         return 0;
1819 }
1820
1821 /****** Firmware Error Handler **********************************************/
1822
1823 /*============================================================================
1824  * Firmware error handler.
1825  *      This routine is called whenever firmware command returns non-zero
1826  *      return code.
1827  *
1828  * Return zero if previous command has to be cancelled.
1829  */
1830 static int chdlc_error (sdla_t *card, int err, CHDLC_MAILBOX_STRUCT *mb)
1831 {
1832         unsigned cmd = mb->command;
1833
1834         switch (err) {
1835
1836         case CMD_TIMEOUT:
1837                 printk(KERN_INFO "%s: command 0x%02X timed out!\n",
1838                         card->devname, cmd);
1839                 break;
1840
1841         case S514_BOTH_PORTS_SAME_CLK_MODE:
1842                 if(cmd == SET_CHDLC_CONFIGURATION) {
1843                         printk(KERN_INFO
1844                          "%s: Configure both ports for the same clock source\n",
1845                                 card->devname);
1846                         break;
1847                 }
1848
1849         default:
1850                 printk(KERN_INFO "%s: command 0x%02X returned 0x%02X!\n",
1851                         card->devname, cmd, err);
1852         }
1853
1854         return 0;
1855 }
1856
1857 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1858 /********** Bottom Half Handlers ********************************************/
1859
1860 /* NOTE: There is no API, BH support for Kernels lower than 2.2.X.
1861  *       DO NOT INSERT ANY CODE HERE, NOTICE THE 
1862  *       PREPROCESSOR STATEMENT ABOVE, UNLESS YOU KNOW WHAT YOU ARE
1863  *       DOING */
1864
1865 static void chdlc_bh (netdevice_t * dev)
1866 {
1867         chdlc_private_area_t* chan = dev->priv;
1868         sdla_t *card = chan->card;
1869         struct sk_buff *skb;
1870
1871         if (atomic_read(&chan->bh_buff_used) == 0){
1872                 clear_bit(0, &chan->tq_working);
1873                 return;
1874         }
1875
1876         while (atomic_read(&chan->bh_buff_used)){
1877
1878                 skb  = ((bh_data_t *)&chan->bh_head[chan->bh_read])->skb;
1879
1880                 if (skb != NULL){
1881
1882                         if (chan->common.sk == NULL || chan->common.func == NULL){
1883                                 ++card->wandev.stats.rx_dropped;
1884                                 wan_dev_kfree_skb(skb, FREE_READ);
1885                                 chdlc_bh_cleanup(dev);
1886                                 continue;
1887                         }
1888
1889                         if (chan->common.func(skb,dev,chan->common.sk) != 0){
1890                                 /* Sock full cannot send, queue us for another
1891                                  * try */
1892                                 atomic_set(&chan->common.receive_block,1);
1893                                 return;
1894                         }else{
1895                                 chdlc_bh_cleanup(dev);
1896                         }
1897                 }else{
1898                         chdlc_bh_cleanup(dev);
1899                 }
1900         }       
1901         clear_bit(0, &chan->tq_working);
1902
1903         return;
1904 }
1905
1906 static int chdlc_bh_cleanup (netdevice_t *dev)
1907 {
1908         chdlc_private_area_t* chan = dev->priv;
1909
1910         ((bh_data_t *)&chan->bh_head[chan->bh_read])->skb = NULL;
1911
1912         if (chan->bh_read == MAX_BH_BUFF){
1913                 chan->bh_read=0;
1914         }else{
1915                 ++chan->bh_read;        
1916         }
1917
1918         atomic_dec(&chan->bh_buff_used);
1919         return 0;
1920 }
1921
1922
1923
1924 static int bh_enqueue (netdevice_t *dev, struct sk_buff *skb)
1925 {
1926         /* Check for full */
1927         chdlc_private_area_t* chan = dev->priv;
1928         sdla_t *card = chan->card;
1929
1930         if (atomic_read(&chan->bh_buff_used) == (MAX_BH_BUFF+1)){
1931                 ++card->wandev.stats.rx_dropped;
1932                 wan_dev_kfree_skb(skb, FREE_READ);
1933                 return 1; 
1934         }
1935
1936         ((bh_data_t *)&chan->bh_head[chan->bh_write])->skb = skb;
1937
1938         if (chan->bh_write == MAX_BH_BUFF){
1939                 chan->bh_write=0;
1940         }else{
1941                 ++chan->bh_write;
1942         }
1943
1944         atomic_inc(&chan->bh_buff_used);
1945
1946         return 0;
1947 }
1948
1949 /* END OF API BH Support */
1950
1951 #endif
1952
1953 /****** Interrupt Handlers **************************************************/
1954
1955 /*============================================================================
1956  * Cisco HDLC interrupt service routine.
1957  */
1958 static void wpc_isr (sdla_t* card)
1959 {
1960         netdevice_t* dev;
1961         SHARED_MEMORY_INFO_STRUCT* flags = NULL;
1962         int i;
1963         sdla_t *my_card;
1964
1965
1966         /* Check for which port the interrupt has been generated
1967          * Since Secondary Port is piggybacking on the Primary
1968          * the check must be done here. 
1969          */
1970
1971         flags = card->u.c.flags;
1972         if (!flags->interrupt_info_struct.interrupt_type){
1973                 /* Check for a second port (piggybacking) */
1974                 if ((my_card = card->next)){
1975                         flags = my_card->u.c.flags;
1976                         if (flags->interrupt_info_struct.interrupt_type){
1977                                 card = my_card;
1978                                 card->isr(card);
1979                                 return;
1980                         }
1981                 }
1982         }
1983
1984         flags = card->u.c.flags;
1985         card->in_isr = 1;
1986         dev = card->wandev.dev;
1987         
1988         /* If we get an interrupt with no network device, stop the interrupts
1989          * and issue an error */
1990         if (!card->tty_opt && !dev && 
1991             flags->interrupt_info_struct.interrupt_type != 
1992                 COMMAND_COMPLETE_APP_INT_PEND){
1993
1994                 goto isr_done;
1995         }
1996         
1997         /* if critical due to peripheral operations
1998          * ie. update() or getstats() then reset the interrupt and
1999          * wait for the board to retrigger.
2000          */
2001         if(test_bit(PERI_CRIT, (void*)&card->wandev.critical)) {
2002                 printk(KERN_INFO "ISR CRIT TO PERI\n");
2003                 goto isr_done;
2004         }
2005
2006         /* On a 508 Card, if critical due to if_send 
2007          * Major Error !!! */
2008         if(card->hw.type != SDLA_S514) {
2009                 if(test_bit(SEND_CRIT, (void*)&card->wandev.critical)) {
2010                         printk(KERN_INFO "%s: Critical while in ISR: %lx\n",
2011                                 card->devname, card->wandev.critical);
2012                         card->in_isr = 0;
2013                         flags->interrupt_info_struct.interrupt_type = 0;
2014                         return;
2015                 }
2016         }
2017
2018         switch(flags->interrupt_info_struct.interrupt_type) {
2019
2020         case RX_APP_INT_PEND:   /* 0x01: receive interrupt */
2021                 rx_intr(card);
2022                 break;
2023
2024         case TX_APP_INT_PEND:   /* 0x02: transmit interrupt */
2025                 flags->interrupt_info_struct.interrupt_permission &=
2026                          ~APP_INT_ON_TX_FRAME;
2027
2028 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2029
2030                 if (card->tty_opt){
2031                         wanpipe_tty_trigger_poll(card);
2032                         break;
2033                 }
2034
2035                 if (dev && is_queue_stopped(dev)){
2036                         if (card->u.c.usedby == API){
2037                                 start_net_queue(dev);
2038                                 wakeup_sk_bh(dev);
2039                         }else{
2040                                 wake_net_dev(dev);
2041                         }
2042                 }
2043 #else
2044                 wake_net_dev(dev);
2045 #endif
2046                 break;
2047
2048         case COMMAND_COMPLETE_APP_INT_PEND:/* 0x04: cmd cplt */
2049                 ++ Intr_test_counter;
2050                 break;
2051
2052         case CHDLC_EXCEP_COND_APP_INT_PEND:     /* 0x20 */
2053                 process_chdlc_exception(card);
2054                 break;
2055
2056         case GLOBAL_EXCEP_COND_APP_INT_PEND:
2057                 process_global_exception(card);
2058                 break;
2059
2060         case TIMER_APP_INT_PEND:
2061                 timer_intr(card);
2062                 break;
2063
2064         default:
2065                 printk(KERN_INFO "%s: spurious interrupt 0x%02X!\n", 
2066                         card->devname,
2067                         flags->interrupt_info_struct.interrupt_type);
2068                 printk(KERN_INFO "Code name: ");
2069                 for(i = 0; i < 4; i ++)
2070                         printk(KERN_INFO "%c",
2071                                 flags->global_info_struct.codename[i]); 
2072                 printk(KERN_INFO "\nCode version: ");
2073                 for(i = 0; i < 4; i ++)
2074                         printk(KERN_INFO "%c", 
2075                                 flags->global_info_struct.codeversion[i]); 
2076                 printk(KERN_INFO "\n"); 
2077                 break;
2078         }
2079
2080 isr_done:
2081
2082         card->in_isr = 0;
2083         flags->interrupt_info_struct.interrupt_type = 0;
2084         return;
2085 }
2086
2087 /*============================================================================
2088  * Receive interrupt handler.
2089  */
2090 static void rx_intr (sdla_t* card)
2091 {
2092         netdevice_t *dev;
2093         chdlc_private_area_t *chdlc_priv_area;
2094         SHARED_MEMORY_INFO_STRUCT *flags = card->u.c.flags;
2095         CHDLC_DATA_RX_STATUS_EL_STRUCT *rxbuf = card->u.c.rxmb;
2096         struct sk_buff *skb;
2097         unsigned len;
2098         unsigned addr = rxbuf->ptr_data_bfr;
2099         void *buf;
2100         int i,udp_type;
2101
2102         if (rxbuf->opp_flag != 0x01) {
2103                 printk(KERN_INFO 
2104                         "%s: corrupted Rx buffer @ 0x%X, flag = 0x%02X!\n", 
2105                         card->devname, (unsigned)rxbuf, rxbuf->opp_flag);
2106                 printk(KERN_INFO "Code name: ");
2107                 for(i = 0; i < 4; i ++)
2108                         printk(KERN_INFO "%c",
2109                                 flags->global_info_struct.codename[i]);
2110                 printk(KERN_INFO "\nCode version: ");
2111                 for(i = 0; i < 4; i ++)
2112                         printk(KERN_INFO "%c",
2113                                 flags->global_info_struct.codeversion[i]);
2114                 printk(KERN_INFO "\n");
2115
2116
2117                 /* Bug Fix: Mar 6 2000
2118                  * If we get a corrupted mailbox, it measn that driver 
2119                  * is out of sync with the firmware. There is no recovery.
2120                  * If we don't turn off all interrupts for this card
2121                  * the machine will crash. 
2122                  */
2123                 printk(KERN_INFO "%s: Critical router failure ...!!!\n", card->devname);
2124                 printk(KERN_INFO "Please contact Sangoma Technologies !\n");
2125                 chdlc_set_intr_mode(card,0);    
2126                 return;
2127         }
2128
2129         len  = rxbuf->frame_length;
2130
2131 #if defined(LINUX_2_4) || defined(LINUX_2_1)    
2132         if (card->tty_opt){
2133
2134                 if (rxbuf->error_flag){ 
2135                         goto rx_exit;
2136                 }
2137
2138                 if (len <= CRC_LENGTH){
2139                         goto rx_exit;
2140                 }
2141                 
2142                 if (!card->u.c.async_mode){
2143                         len -= CRC_LENGTH;
2144                 }
2145
2146                 wanpipe_tty_receive(card,addr,len);
2147                 goto rx_exit;
2148         }
2149 #endif
2150
2151         dev = card->wandev.dev;
2152
2153         if (!dev){
2154                 goto rx_exit;
2155         }
2156
2157         if (!is_dev_running(dev))
2158                 goto rx_exit;
2159
2160         chdlc_priv_area = dev->priv;
2161
2162         
2163         /* Allocate socket buffer */
2164         skb = dev_alloc_skb(len);
2165
2166         if (skb == NULL) {
2167                 printk(KERN_INFO "%s: no socket buffers available!\n",
2168                                         card->devname);
2169                 ++card->wandev.stats.rx_dropped;
2170                 goto rx_exit;
2171         }
2172
2173         /* Copy data to the socket buffer */
2174         if((addr + len) > card->u.c.rx_top + 1) {
2175                 unsigned tmp = card->u.c.rx_top - addr + 1;
2176                 buf = skb_put(skb, tmp);
2177                 sdla_peek(&card->hw, addr, buf, tmp);
2178                 addr = card->u.c.rx_base;
2179                 len -= tmp;
2180         }
2181                 
2182         buf = skb_put(skb, len);
2183         sdla_peek(&card->hw, addr, buf, len);
2184
2185         skb->protocol = htons(ETH_P_IP);
2186
2187         card->wandev.stats.rx_packets ++;
2188 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2189         card->wandev.stats.rx_bytes += skb->len;
2190 #endif
2191         udp_type = udp_pkt_type( skb, card );
2192
2193         if(udp_type == UDP_CPIPE_TYPE) {
2194                 if(store_udp_mgmt_pkt(UDP_PKT_FRM_NETWORK,
2195                                       card, skb, dev, chdlc_priv_area)) {
2196                         flags->interrupt_info_struct.
2197                                                 interrupt_permission |= 
2198                                                         APP_INT_ON_TIMER; 
2199                 }
2200 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2201         } else if(card->u.c.usedby == API) {
2202
2203                 api_rx_hdr_t* api_rx_hdr;
2204                 skb_push(skb, sizeof(api_rx_hdr_t));
2205                 api_rx_hdr = (api_rx_hdr_t*)&skb->data[0x00];
2206                 api_rx_hdr->error_flag = rxbuf->error_flag;
2207                 api_rx_hdr->time_stamp = rxbuf->time_stamp;
2208
2209                 skb->protocol = htons(PVC_PROT);
2210                 skb->mac.raw  = skb->data;
2211                 skb->dev      = dev;
2212                 skb->pkt_type = WAN_PACKET_DATA;
2213
2214                 bh_enqueue(dev, skb);
2215
2216                 if (!test_and_set_bit(0,&chdlc_priv_area->tq_working)){
2217                         wanpipe_queue_tq(&chdlc_priv_area->common.wanpipe_task);
2218                         wanpipe_mark_bh();
2219                 }
2220 #endif
2221         }else{
2222                 /* FIXME: we should check to see if the received packet is a 
2223                           multicast packet so that we can increment the multicast 
2224                           statistic
2225                           ++ chdlc_priv_area->if_stats.multicast;
2226                 */
2227                 /* Pass it up the protocol stack */
2228         
2229                 skb->dev = dev;
2230                 skb->mac.raw  = skb->data;
2231                 netif_rx(skb);
2232         }
2233
2234 rx_exit:
2235         /* Release buffer element and calculate a pointer to the next one */
2236         rxbuf->opp_flag = 0x00;
2237         card->u.c.rxmb = ++ rxbuf;
2238         if((void*)rxbuf > card->u.c.rxbuf_last){
2239                 card->u.c.rxmb = card->u.c.rxbuf_base;
2240         }
2241 }
2242
2243 /*============================================================================
2244  * Timer interrupt handler.
2245  * The timer interrupt is used for two purposes:
2246  *    1) Processing udp calls from 'cpipemon'.
2247  *    2) Reading board-level statistics for updating the proc file system.
2248  */
2249 void timer_intr(sdla_t *card)
2250 {
2251         netdevice_t* dev;
2252         chdlc_private_area_t* chdlc_priv_area = NULL;
2253         SHARED_MEMORY_INFO_STRUCT* flags = NULL;
2254
2255         if ((dev = card->wandev.dev)==NULL){
2256                 flags = card->u.c.flags;
2257                 flags->interrupt_info_struct.interrupt_permission &=
2258                         ~APP_INT_ON_TIMER;
2259                 return;
2260         }
2261         
2262         chdlc_priv_area = dev->priv;
2263
2264         if (chdlc_priv_area->timer_int_enabled & TMR_INT_ENABLED_CONFIG) {
2265                 if (!config_chdlc(card)){
2266                         chdlc_priv_area->timer_int_enabled &= ~TMR_INT_ENABLED_CONFIG;
2267                 }
2268         }
2269
2270         /* process a udp call if pending */
2271         if(chdlc_priv_area->timer_int_enabled & TMR_INT_ENABLED_UDP) {
2272                 process_udp_mgmt_pkt(card, dev,
2273                        chdlc_priv_area);
2274                 chdlc_priv_area->timer_int_enabled &= ~TMR_INT_ENABLED_UDP;
2275         }
2276
2277         /* read the communications statistics if required */
2278         if(chdlc_priv_area->timer_int_enabled & TMR_INT_ENABLED_UPDATE) {
2279                 update_comms_stats(card, chdlc_priv_area);
2280                 if(!(-- chdlc_priv_area->update_comms_stats)) {
2281                         chdlc_priv_area->timer_int_enabled &= 
2282                                 ~TMR_INT_ENABLED_UPDATE;
2283                 }
2284         }
2285
2286         /* only disable the timer interrupt if there are no udp or statistic */
2287         /* updates pending */
2288         if(!chdlc_priv_area->timer_int_enabled) {
2289                 flags = card->u.c.flags;
2290                 flags->interrupt_info_struct.interrupt_permission &=
2291                         ~APP_INT_ON_TIMER;
2292         }
2293 }
2294
2295 /*------------------------------------------------------------------------------
2296   Miscellaneous Functions
2297         - set_chdlc_config() used to set configuration options on the board
2298 ------------------------------------------------------------------------------*/
2299
2300 static int set_chdlc_config(sdla_t* card)
2301 {
2302         CHDLC_CONFIGURATION_STRUCT cfg;
2303
2304         memset(&cfg, 0, sizeof(CHDLC_CONFIGURATION_STRUCT));
2305
2306         if(card->wandev.clocking){
2307                 cfg.baud_rate = card->wandev.bps;
2308         }
2309                 
2310         cfg.line_config_options = (card->wandev.interface == WANOPT_RS232) ?
2311                 INTERFACE_LEVEL_RS232 : INTERFACE_LEVEL_V35;
2312
2313         cfg.modem_config_options        = 0;
2314         cfg.modem_status_timer          = 100;
2315
2316         cfg.CHDLC_protocol_options      = card->u.c.protocol_options;
2317
2318         if (card->tty_opt){
2319                 cfg.CHDLC_API_options   = DISCARD_RX_ERROR_FRAMES;
2320         }
2321         
2322         cfg.percent_data_buffer_for_Tx  = (card->u.c.receive_only) ? 0 : 50;
2323         cfg.CHDLC_statistics_options    = (CHDLC_TX_DATA_BYTE_COUNT_STAT |
2324                 CHDLC_RX_DATA_BYTE_COUNT_STAT);
2325         
2326         if (card->tty_opt){
2327                 card->wandev.mtu = TTY_CHDLC_MAX_MTU;
2328         }
2329         cfg.max_CHDLC_data_field_length = card->wandev.mtu;
2330         cfg.transmit_keepalive_timer    = card->u.c.kpalv_tx;
2331         cfg.receive_keepalive_timer     = card->u.c.kpalv_rx;
2332         cfg.keepalive_error_tolerance   = card->u.c.kpalv_err;
2333         cfg.SLARP_request_timer         = card->u.c.slarp_timer;
2334
2335         if (cfg.SLARP_request_timer) {
2336                 cfg.IP_address          = 0;
2337                 cfg.IP_netmask          = 0;
2338                 
2339         }else if (card->wandev.dev){
2340                 netdevice_t * dev = card->wandev.dev;
2341                 chdlc_private_area_t *chdlc_priv_area = dev->priv;
2342                 
2343 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2344                 struct in_device *in_dev = dev->ip_ptr;
2345
2346                 if(in_dev != NULL) {
2347                         struct in_ifaddr *ifa = in_dev->ifa_list;
2348
2349                         if (ifa != NULL ) {
2350                                 cfg.IP_address  = ntohl(ifa->ifa_local);
2351                                 cfg.IP_netmask  = ntohl(ifa->ifa_mask); 
2352                                 chdlc_priv_area->IP_address = ntohl(ifa->ifa_local);
2353                                 chdlc_priv_area->IP_netmask = ntohl(ifa->ifa_mask); 
2354                         }
2355                 }
2356 #else
2357                 cfg.IP_address          = ntohl(dev->pa_addr);
2358                 cfg.IP_netmask          = ntohl(dev->pa_mask);
2359                 chdlc_priv_area->IP_address = ntohl(dev->pa_addr);
2360                 chdlc_priv_area->IP_netmask = ntohl(dev->pa_mask);
2361 #endif
2362
2363                 /* FIXME: We must re-think this message in next release
2364                 if((cfg.IP_address & 0x000000FF) > 2) {
2365                         printk(KERN_WARNING "\n");
2366                         printk(KERN_WARNING "  WARNING:%s configured with an\n",
2367                                 card->devname);
2368                         printk(KERN_WARNING "  invalid local IP address.\n");
2369                         printk(KERN_WARNING "  Slarp pragmatics will fail.\n");
2370                         printk(KERN_WARNING "  IP address should be of the\n");
2371                         printk(KERN_WARNING "  format A.B.C.1 or A.B.C.2.\n");
2372                 }
2373                 */              
2374         }
2375         
2376         return chdlc_configure(card, &cfg);
2377 }
2378
2379
2380 /*-----------------------------------------------------------------------------
2381    set_asy_config() used to set asynchronous configuration options on the board
2382 ------------------------------------------------------------------------------*/
2383
2384 static int set_asy_config(sdla_t* card)
2385 {
2386
2387         ASY_CONFIGURATION_STRUCT cfg;
2388         CHDLC_MAILBOX_STRUCT *mailbox = card->mbox;
2389         int err;
2390
2391         memset(&cfg, 0, sizeof(ASY_CONFIGURATION_STRUCT));
2392
2393         if(card->wandev.clocking)
2394                 cfg.baud_rate = card->wandev.bps;
2395
2396         cfg.line_config_options = (card->wandev.interface == WANOPT_RS232) ?
2397                 INTERFACE_LEVEL_RS232 : INTERFACE_LEVEL_V35;
2398
2399         cfg.modem_config_options        = 0;
2400         cfg.asy_API_options             = card->u.c.api_options;
2401         cfg.asy_protocol_options        = card->u.c.protocol_options;
2402         cfg.Tx_bits_per_char            = card->u.c.tx_bits_per_char;
2403         cfg.Rx_bits_per_char            = card->u.c.rx_bits_per_char;
2404         cfg.stop_bits                   = card->u.c.stop_bits;
2405         cfg.parity                      = card->u.c.parity;
2406         cfg.break_timer                 = card->u.c.break_timer;
2407         cfg.asy_Rx_inter_char_timer     = card->u.c.inter_char_timer; 
2408         cfg.asy_Rx_complete_length      = card->u.c.rx_complete_length; 
2409         cfg.XON_char                    = card->u.c.xon_char;
2410         cfg.XOFF_char                   = card->u.c.xoff_char;
2411         cfg.asy_statistics_options      = (CHDLC_TX_DATA_BYTE_COUNT_STAT |
2412                 CHDLC_RX_DATA_BYTE_COUNT_STAT);
2413
2414         mailbox->buffer_length = sizeof(ASY_CONFIGURATION_STRUCT);
2415         memcpy(mailbox->data, &cfg, mailbox->buffer_length);
2416         mailbox->command = SET_ASY_CONFIGURATION;
2417         err = sdla_exec(mailbox) ? mailbox->return_code : CMD_TIMEOUT;
2418         if (err != COMMAND_OK) 
2419                 chdlc_error (card, err, mailbox);
2420         return err;
2421 }
2422
2423 /*============================================================================
2424  * Enable asynchronous communications.
2425  */
2426
2427 static int asy_comm_enable (sdla_t* card)
2428 {
2429
2430         int err;
2431         CHDLC_MAILBOX_STRUCT* mb = card->mbox;
2432
2433         mb->buffer_length = 0;
2434         mb->command = ENABLE_ASY_COMMUNICATIONS;
2435         err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
2436         if (err != COMMAND_OK && card->wandev.dev)
2437                 chdlc_error(card, err, mb);
2438         
2439         if (!err)
2440                 card->u.c.comm_enabled = 1;
2441
2442         return err;
2443 }
2444
2445 /*============================================================================
2446  * Process global exception condition
2447  */
2448 static int process_global_exception(sdla_t *card)
2449 {
2450         CHDLC_MAILBOX_STRUCT* mbox = card->mbox;
2451         int err;
2452
2453         mbox->buffer_length = 0;
2454         mbox->command = READ_GLOBAL_EXCEPTION_CONDITION;
2455         err = sdla_exec(mbox) ? mbox->return_code : CMD_TIMEOUT;
2456
2457         if(err != CMD_TIMEOUT ){
2458         
2459                 switch(mbox->return_code) {
2460          
2461                 case EXCEP_MODEM_STATUS_CHANGE:
2462
2463                         printk(KERN_INFO "%s: Modem status change\n",
2464                                 card->devname);
2465
2466                         switch(mbox->data[0] & (DCD_HIGH | CTS_HIGH)) {
2467                                 case (DCD_HIGH):
2468                                         printk(KERN_INFO "%s: DCD high, CTS low\n",card->devname);
2469                                         break;
2470                                 case (CTS_HIGH):
2471                                         printk(KERN_INFO "%s: DCD low, CTS high\n",card->devname); 
2472                                         break;
2473                                 case ((DCD_HIGH | CTS_HIGH)):
2474                                         printk(KERN_INFO "%s: DCD high, CTS high\n",card->devname);
2475                                         break;
2476                                 default:
2477                                         printk(KERN_INFO "%s: DCD low, CTS low\n",card->devname);
2478                                         break;
2479                         }
2480                         break;
2481
2482                 case EXCEP_TRC_DISABLED:
2483                         printk(KERN_INFO "%s: Line trace disabled\n",
2484                                 card->devname);
2485                         break;
2486
2487                 case EXCEP_IRQ_TIMEOUT:
2488                         printk(KERN_INFO "%s: IRQ timeout occurred\n",
2489                                 card->devname); 
2490                         break;
2491
2492                 case 0x17:
2493                         if (card->tty_opt){
2494                                 if (card->tty && card->tty_open){ 
2495                                         printk(KERN_INFO 
2496                                                 "%s: Modem Hangup Exception: Hanging Up!\n",
2497                                                 card->devname);
2498                                         tty_hangup(card->tty);
2499                                 }
2500                                 break;
2501                         }
2502
2503                         /* If TTY is not used just drop throught */
2504                         
2505                 default:
2506                         printk(KERN_INFO "%s: Global exception %x\n",
2507                                 card->devname, mbox->return_code);
2508                         break;
2509                 }
2510         }
2511         return 0;
2512 }
2513
2514
2515 /*============================================================================
2516  * Process chdlc exception condition
2517  */
2518 static int process_chdlc_exception(sdla_t *card)
2519 {
2520         CHDLC_MAILBOX_STRUCT* mb = card->mbox;
2521         int err;
2522
2523         mb->buffer_length = 0;
2524         mb->command = READ_CHDLC_EXCEPTION_CONDITION;
2525         err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
2526         if(err != CMD_TIMEOUT) {
2527         
2528                 switch (err) {
2529
2530                 case EXCEP_LINK_ACTIVE:
2531                         port_set_state(card, WAN_CONNECTED);
2532                         trigger_chdlc_poll(card->wandev.dev);
2533                         break;
2534
2535                 case EXCEP_LINK_INACTIVE_MODEM:
2536                         port_set_state(card, WAN_DISCONNECTED);
2537                         unconfigure_ip(card);
2538                         trigger_chdlc_poll(card->wandev.dev);
2539                         break;
2540
2541                 case EXCEP_LINK_INACTIVE_KPALV:
2542                         port_set_state(card, WAN_DISCONNECTED);
2543                         printk(KERN_INFO "%s: Keepalive timer expired.\n",
2544                                                 card->devname);
2545                         unconfigure_ip(card);
2546                         trigger_chdlc_poll(card->wandev.dev);
2547                         break;
2548
2549                 case EXCEP_IP_ADDRESS_DISCOVERED:
2550                         if (configure_ip(card)) 
2551                                 return -1;
2552                         break;
2553
2554                 case EXCEP_LOOPBACK_CONDITION:
2555                         printk(KERN_INFO "%s: Loopback Condition Detected.\n",
2556                                                 card->devname);
2557                         break;
2558
2559                 case NO_CHDLC_EXCEP_COND_TO_REPORT:
2560                         printk(KERN_INFO "%s: No exceptions reported.\n",
2561                                                 card->devname);
2562                         break;
2563                 }
2564
2565         }
2566         return 0;
2567 }
2568
2569
2570 /*============================================================================
2571  * Configure IP from SLARP negotiation
2572  * This adds dynamic routes when SLARP has provided valid addresses
2573  */
2574
2575 static int configure_ip (sdla_t* card)
2576 {
2577         netdevice_t *dev = card->wandev.dev;
2578         chdlc_private_area_t *chdlc_priv_area;
2579         char err;
2580
2581         if (!dev)
2582                 return 0;
2583
2584         chdlc_priv_area = dev->priv;
2585         
2586         
2587         /* set to discover */
2588         if(card->u.c.slarp_timer != 0x00) {
2589                 CHDLC_MAILBOX_STRUCT* mb = card->mbox;
2590                 CHDLC_CONFIGURATION_STRUCT *cfg;
2591
2592                 mb->buffer_length = 0;
2593                 mb->command = READ_CHDLC_CONFIGURATION;
2594                 err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
2595         
2596                 if(err != COMMAND_OK) {
2597                         chdlc_error(card,err,mb);
2598                         return -1;
2599                 }
2600
2601                 cfg = (CHDLC_CONFIGURATION_STRUCT *)mb->data;
2602                 chdlc_priv_area->IP_address = cfg->IP_address;
2603                 chdlc_priv_area->IP_netmask = cfg->IP_netmask;
2604
2605                 /* Set flag to add route */
2606                 chdlc_priv_area->route_status = ADD_ROUTE;
2607
2608                 /* The idea here is to add the route in the poll routine.
2609                 This way, we aren't in interrupt context when adding routes */
2610                 trigger_chdlc_poll(dev);
2611         }
2612
2613         return 0;
2614 }
2615
2616
2617 /*============================================================================
2618  * Un-Configure IP negotiated by SLARP
2619  * This removes dynamic routes when the link becomes inactive.
2620  */
2621
2622 static int unconfigure_ip (sdla_t* card)
2623 {
2624         netdevice_t *dev = card->wandev.dev;
2625         chdlc_private_area_t *chdlc_priv_area;
2626
2627         if (!dev)
2628                 return 0;
2629
2630         chdlc_priv_area= dev->priv;
2631         
2632         if (chdlc_priv_area->route_status == ROUTE_ADDED) {
2633
2634                 /* Note: If this function is called, the 
2635                  * port state has been DISCONNECTED.  This state
2636                  * change will trigger a poll_disconnected 
2637                  * function, that will check for this condition. 
2638                  */
2639                 chdlc_priv_area->route_status = REMOVE_ROUTE;
2640
2641         }
2642         return 0;
2643 }
2644
2645 /*============================================================================
2646  * Routine to add/remove routes 
2647  * Called like a polling routine when Routes are flagged to be added/removed.
2648  */
2649
2650 static void process_route (sdla_t *card)
2651 {
2652         netdevice_t *dev = card->wandev.dev;
2653         unsigned char port_num;
2654         chdlc_private_area_t *chdlc_priv_area = NULL;
2655         u32 local_IP_addr = 0;
2656         u32 remote_IP_addr = 0;
2657         u32 IP_netmask, IP_addr;
2658         int err = 0;
2659 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2660         struct in_device *in_dev;
2661         mm_segment_t fs;
2662         struct ifreq if_info;
2663         struct sockaddr_in *if_data1, *if_data2;
2664 #else
2665         unsigned long fs = 0;
2666         struct rtentry route;
2667 #endif
2668         
2669         chdlc_priv_area = dev->priv;
2670         port_num = card->u.c.comm_port;
2671
2672         /* Bug Fix Mar 16 2000
2673          * AND the IP address to the Mask before checking
2674          * the last two bits. */
2675
2676         if((chdlc_priv_area->route_status == ADD_ROUTE) &&
2677                 ((chdlc_priv_area->IP_address & ~chdlc_priv_area->IP_netmask) > 2)) {
2678
2679                 printk(KERN_INFO "%s: Dynamic route failure.\n",card->devname);
2680
2681                 if(card->u.c.slarp_timer) {
2682
2683                         printk(KERN_INFO "%s: Bad IP address %s received\n",
2684                                 card->devname,
2685                                 in_ntoa(ntohl(chdlc_priv_area->IP_address)));
2686                         printk(KERN_INFO "%s: from remote station.\n",
2687                                 card->devname);
2688
2689                 }else{ 
2690
2691                         printk(KERN_INFO "%s: Bad IP address %s issued\n",
2692                                 card->devname,
2693                                 in_ntoa(ntohl(chdlc_priv_area->IP_address)));
2694                         printk(KERN_INFO "%s: to remote station. Local\n",
2695                                 card->devname);
2696                         printk(KERN_INFO "%s: IP address must be A.B.C.1\n",
2697                                 card->devname);
2698                         printk(KERN_INFO "%s: or A.B.C.2.\n",card->devname);
2699                 }
2700