v2.4.8 -> v2.4.8.1
[opensuse:kernel.git] / drivers / net / wan / sdla_fr.c
1 /*****************************************************************************
2 * sdla_fr.c     WANPIPE(tm) Multiprotocol WAN Link Driver. Frame relay module.
3 *
4 * Author(s):    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 * Nov 23, 2000  Nenad Corbic    o Added support for 2.4.X kernels
15 * Nov 15, 2000  David Rokavarg  
16 *               Nenad Corbic    o Added frame relay bridging support.
17 *                                 Original code from Mark Wells and Kristian Hoffmann has
18 *                                 been integrated into the frame relay driver.
19 * Nov 13, 2000  Nenad Corbic    o Added true interface type encoding option.
20 *                                 Tcpdump doesn't support Frame Relay inteface
21 *                                 types, to fix this true type option will set
22 *                                 the interface type to RAW IP mode.
23 * Nov 07, 2000  Nenad Corbic    o Added security features for UDP debugging:
24 *                                 Deny all and specify allowed requests.
25 * Nov 06, 2000  Nenad Corbic    o Wanpipe interfaces conform to raw packet interfaces.  
26 *                                 Moved the if_header into the if_send() routine.
27 *                                 The if_header() was breaking the libpcap 
28 *                                 support. i.e. support for tcpdump, ethereal ...
29 * Oct 12. 2000  Nenad Corbic    o Added error message in fr_configure
30 * Jul 31, 2000  Nenad Corbic    o Fixed the Router UP Time.
31 * Apr 28, 2000  Nenad Corbic    o Added the option to shutdown an interface
32 *                                 when the channel gets disconnected.
33 * Apr 28, 2000  Nenad Corbic    o Added M.Grants patch: disallow duplicate
34 *                                 interface setups. 
35 * Apr 25, 2000  Nenad Corbic    o Added M.Grants patch: dynamically add/remove 
36 *                                 new dlcis/interfaces.
37 * Mar 23, 2000  Nenad Corbic    o Improved task queue, bh handling.
38 * Mar 16, 2000  Nenad Corbic    o Added Inverse ARP support
39 * Mar 13, 2000  Nenad Corbic    o Added new socket API support.
40 * Mar 06, 2000  Nenad Corbic    o Bug Fix: corrupted mbox recovery.
41 * Feb 24, 2000  Nenad Corbic    o Fixed up FT1 UDP debugging problem.
42 * Dev 15, 1999  Nenad Corbic    o Fixed up header files for 2.0.X kernels
43 *
44 * Nov 08, 1999  Nenad Corbic    o Combined all debug UDP calls into one function
45 *                               o Removed the ARP support. This has to be done
46 *                                 in the next version.
47 *                               o Only a Node can implement NO signalling.
48 *                                 Initialize DLCI during if_open() if NO 
49 *                                 signalling.
50 *                               o Took out IPX support, implement in next
51 *                                 version
52 * Sep 29, 1999  Nenad Corbic    o Added SMP support and changed the update
53 *                                 function to use timer interrupt.
54 *                               o Fixed the CIR bug:  Set the value of BC
55 *                                 to CIR when the CIR is enabled.
56 *                               o Updated comments, statistics and tracing.
57 * Jun 02, 1999  Gideon Hack     o Updated for S514 support.
58 * Sep 18, 1998  Jaspreet Singh  o Updated for 2.2.X kernels.
59 * Jul 31, 1998  Jaspreet Singh  o Removed wpf_poll routine.  The channel/DLCI 
60 *                                 status is received through an event interrupt.
61 * Jul 08, 1998  David Fong      o Added inverse ARP support.
62 * Mar 26, 1997  Jaspreet Singh  o Returning return codes for failed UDP cmds.
63 * Jan 28, 1997  Jaspreet Singh  o Improved handling of inactive DLCIs.
64 * Dec 30, 1997  Jaspreet Singh  o Replaced dev_tint() with mark_bh(NET_BH)
65 * Dec 16, 1997  Jaspreet Singh  o Implemented Multiple IPX support.
66 * Nov 26, 1997  Jaspreet Singh  o Improved load sharing with multiple boards
67 *                               o Added Cli() to protect enabling of interrupts
68 *                                 while polling is called.
69 * Nov 24, 1997  Jaspreet Singh  o Added counters to avoid enabling of interrupts
70 *                                 when they have been disabled by another
71 *                                 interface or routine (eg. wpf_poll).
72 * Nov 06, 1997  Jaspreet Singh  o Added INTR_TEST_MODE to avoid polling 
73 *                                 routine disable interrupts during interrupt
74 *                                 testing.
75 * Oct 20, 1997  Jaspreet Singh  o Added hooks in for Router UP time.
76 * Oct 16, 1997  Jaspreet Singh  o The critical flag is used to maintain flow
77 *                                 control by avoiding RACE conditions.  The
78 *                                 cli() and restore_flags() are taken out.
79 *                                 The fr_channel structure is appended for 
80 *                                 Driver Statistics.
81 * Oct 15, 1997  Farhan Thawar    o updated if_send() and receive for IPX
82 * Aug 29, 1997  Farhan Thawar    o Removed most of the cli() and sti()
83 *                                o Abstracted the UDP management stuff
84 *                                o Now use tbusy and critical more intelligently
85 * Jul 21, 1997  Jaspreet Singh   o Can configure T391, T392, N391, N392 & N393
86 *                                  through router.conf.
87 *                                o Protected calls to sdla_peek() by adDing 
88 *                                  save_flags(), cli() and restore_flags().
89 *                                o Added error message for Inactive DLCIs in
90 *                                  fr_event() and update_chan_state().
91 *                                o Fixed freeing up of buffers using kfree() 
92 *                                  when packets are received.
93 * Jul 07, 1997  Jaspreet Singh   o Added configurable TTL for UDP packets 
94 *                                o Added ability to discard multicast and 
95 *                                  broadcast source addressed packets
96 * Jun 27, 1997  Jaspreet Singh   o Added FT1 monitor capabilities 
97 *                                  New case (0x44) statement in if_send routine 
98 *                                  Added a global variable rCount to keep track
99 *                                  of FT1 status enabled on the board.
100 * May 29, 1997  Jaspreet Singh   o Fixed major Flow Control Problem
101 *                                  With multiple boards a problem was seen where
102 *                                  the second board always stopped transmitting
103 *                                  packet after running for a while. The code
104 *                                  got into a stage where the interrupts were
105 *                                  disabled and dev->tbusy was set to 1.
106 *                                  This caused the If_send() routine to get into
107 *                                  the if clause for it(0,dev->tbusy) 
108 *                                  forever.
109 *                                  The code got into this stage due to an 
110 *                                  interrupt occuring within the if clause for 
111 *                                  set_bit(0,dev->tbusy).  Since an interrupt 
112 *                                  disables furhter transmit interrupt and 
113 *                                  makes dev->tbusy = 0, this effect was undone 
114 *                                  by making dev->tbusy = 1 in the if clause.
115 *                                  The Fix checks to see if Transmit interrupts
116 *                                  are disabled then do not make dev->tbusy = 1
117 *                                  Introduced a global variable: int_occur and
118 *                                  added tx_int_enabled in the wan_device 
119 *                                  structure.   
120 * May 21, 1997  Jaspreet Singh   o Fixed UDP Management for multiple
121 *                                  boards.
122 *
123 * Apr 25, 1997  Farhan Thawar    o added UDP Management stuff
124 *                                o fixed bug in if_send() and tx_intr() to
125 *                                  sleep and wakeup all devices
126 * Mar 11, 1997  Farhan Thawar   Version 3.1.1
127 *                                o fixed (+1) bug in fr508_rx_intr()
128 *                                o changed if_send() to return 0 if
129 *                                  wandev.critical() is true
130 *                                o free socket buffer in if_send() if
131 *                                  returning 0 
132 *                                o added tx_intr() routine
133 * Jan 30, 1997  Gene Kozin      Version 3.1.0
134 *                                o implemented exec() entry point
135 *                                o fixed a bug causing driver configured as
136 *                                  a FR switch to be stuck in WAN_
137 *                                  mode
138 * Jan 02, 1997  Gene Kozin      Initial version.
139 *****************************************************************************/
140
141 #include <linux/version.h>
142 #include <linux/kernel.h>       /* printk(), and other useful stuff */
143 #include <linux/stddef.h>       /* offsetof(), etc. */
144 #include <linux/errno.h>        /* return codes */
145 #include <linux/string.h>       /* inline memset(), etc. */
146 #include <linux/slab.h> /* kmalloc(), kfree() */
147 #include <linux/wanrouter.h>    /* WAN router definitions */
148 #include <linux/wanpipe.h>      /* WANPIPE common user API definitions */
149 #include <linux/if_arp.h>       /* ARPHRD_* defines */
150 #include <asm/byteorder.h>      /* htons(), etc. */
151 #include <asm/io.h>             /* for inb(), outb(), etc. */
152 #include <linux/time.h>         /* for do_gettimeofday */       
153 #include <linux/in.h>           /* sockaddr_in */
154 #include <linux/inet.h>         /* in_ntoa(), etc... */
155 #include <asm/errno.h>
156
157 #include <linux/ip.h>
158 #include <linux/if.h>
159
160 #include <linux/if_wanpipe_common.h>    /* Wanpipe Socket */
161 #include <linux/if_wanpipe.h>   
162
163 #include <linux/sdla_fr.h>              /* frame relay firmware API definitions */
164
165 #if defined(LINUX_2_1) || defined(LINUX_2_4)
166  #include <asm/uaccess.h>
167  #include <linux/inetdevice.h>
168  #include <linux/netdevice.h>
169
170 #else
171  #include <asm/segment.h>
172 #endif
173
174 #include <net/route.h>                  /* Dynamic Route Creation */
175 #include <linux/etherdevice.h>          /* eth_type_trans() used for bridging */
176 #include <linux/random.h>
177
178 /****** Defines & Macros ****************************************************/
179
180 #define MAX_CMD_RETRY   10              /* max number of firmware retries */
181
182 #define FR_HEADER_LEN   8               /* max encapsulation header size */
183 #define FR_CHANNEL_MTU  1500            /* unfragmented logical channel MTU */
184
185 /* Q.922 frame types */
186 #define Q922_UI         0x03            /* Unnumbered Info frame */
187 #define Q922_XID        0xAF            
188
189 /* DLCI configured or not */
190 #define DLCI_NOT_CONFIGURED     0x00
191 #define DLCI_CONFIG_PENDING     0x01
192 #define DLCI_CONFIGURED         0x02
193
194 /* CIR enabled or not */
195 #define CIR_ENABLED     0x00
196 #define CIR_DISABLED    0x01
197
198 #define FRAME_RELAY_API 1
199 #define MAX_BH_BUFF     10
200
201 /* For handle_IPXWAN() */
202 #define CVHexToAscii(b) (((unsigned char)(b) > (unsigned char)9) ? ((unsigned char)'A' + ((unsigned char)(b) - (unsigned char)10)) : ((unsigned char)'0' + (unsigned char)(b)))
203  
204 /****** Data Structures *****************************************************/
205
206 /* This is an extention of the 'struct device' we create for each network
207  * interface to keep the rest of channel-specific data.
208  */
209 typedef struct fr_channel
210 {
211         wanpipe_common_t common;
212         char name[WAN_IFNAME_SZ+1];     /* interface name, ASCIIZ */
213         unsigned dlci_configured  ;     /* check whether configured or not */
214         unsigned cir_status;            /* check whether CIR enabled or not */
215         unsigned dlci;                  /* logical channel number */
216         unsigned cir;                   /* committed information rate */
217         unsigned bc;                    /* committed burst size */
218         unsigned be;                    /* excess burst size */
219         unsigned mc;                    /* multicast support on or off */
220         unsigned tx_int_status;         /* Transmit Interrupt Status */ 
221         unsigned short pkt_length;      /* Packet Length */
222         unsigned long router_start_time;/* Router start time in seconds */
223         unsigned long tick_counter;     /* counter for transmit time out */
224         char dev_pending_devtint;       /* interface pending dev_tint() */
225         void *dlci_int_interface;       /* pointer to the DLCI Interface */ 
226         unsigned long IB_addr;          /* physical address of Interface Byte */
227         unsigned long state_tick;       /* time of the last state change */
228         unsigned char enable_IPX;       /* Enable/Disable the use of IPX */
229         unsigned long network_number;   /* Internal Network Number for IPX*/
230         sdla_t *card;                   /* -> owner */
231         unsigned route_flag;            /* Add/Rem dest addr in route tables */
232         unsigned inarp;                 /* Inverse Arp Request status */ 
233         unsigned char inarp_ready;      /* Ready to send requests */
234         int inarp_interval;             /* Time between InArp Requests */
235         unsigned long inarp_tick;       /* InArp jiffies tick counter */
236         unsigned char interface_down;   /* Bring interface down on disconnect */
237       #if defined(LINUX_2_1) || defined(LINUX_2_4)
238         struct net_device_stats ifstats;        /* interface statistics */
239       #else
240         struct enet_statistics ifstats;
241       #endif    
242         if_send_stat_t drvstats_if_send;
243         rx_intr_stat_t drvstats_rx_intr;
244         pipe_mgmt_stat_t drvstats_gen;
245         unsigned long router_up_time;
246
247         unsigned short transmit_length;
248         struct sk_buff *delay_skb;
249
250
251       #if defined(LINUX_2_1) || defined(LINUX_2_4)
252         bh_data_t *bh_head;               /* Circular buffer for chdlc_bh */
253         unsigned long  tq_working;
254         volatile int  bh_write;
255         volatile int  bh_read;
256         atomic_t  bh_buff_used;
257       #endif
258
259         /* Polling task queue. Each interface
260          * has its own task queue, which is used
261          * to defer events from the interrupt */
262         struct tq_struct fr_poll_task;
263         struct timer_list fr_arp_timer;
264
265         u32 ip_local;
266         u32 ip_remote;
267         u8  config_dlci;
268         u32 unconfig_dlci;
269
270         /* Whether this interface should be setup as a gateway.
271          * Used by dynamic route setup code */
272         u8  gateway;
273
274         /* True interface type */
275         u8 true_if_encoding;
276         u8 fr_header[FR_HEADER_LEN];
277         char fr_header_len;
278
279 } fr_channel_t;
280
281 /* Route Flag options */
282 #define NO_ROUTE        0x00
283 #define ADD_ROUTE       0x01
284 #define ROUTE_ADDED     0x02
285 #define REMOVE_ROUTE    0x03
286 #define ARP_REQ         0x04
287
288 /* inarp options */
289 #define INARP_NONE              0x00
290 #define INARP_REQUEST           0x01
291 #define INARP_CONFIGURED        0x02
292
293 /* reasons for enabling the timer interrupt on the adapter */
294 #define TMR_INT_ENABLED_UDP     0x01
295 #define TMR_INT_ENABLED_UPDATE  0x02
296 #define TMR_INT_ENABLED_ARP     0x04
297 #define TMR_INT_ENABLED_UPDATE_STATE    0x08
298 #define TMR_INT_ENABLED_CONFIG  0x10
299 #define TMR_INT_ENABLED_UNCONFIG        0x20
300
301
302 typedef struct dlci_status
303 {
304         unsigned short dlci     PACKED;
305         unsigned char state     PACKED;
306 } dlci_status_t;
307
308 typedef struct dlci_IB_mapping
309 {
310         unsigned short dlci             PACKED;
311         unsigned long  addr_value       PACKED;
312 } dlci_IB_mapping_t;
313
314 /* This structure is used for DLCI list Tx interrupt mode.  It is used to
315    enable interrupt bit and set the packet length for transmission
316  */
317 typedef struct fr_dlci_interface 
318 {
319         unsigned char gen_interrupt     PACKED;
320         unsigned short packet_length    PACKED;
321         unsigned char reserved          PACKED;
322 } fr_dlci_interface_t; 
323
324 /* variable for keeping track of enabling/disabling FT1 monitor status */
325 static int rCount = 0;
326
327 extern void disable_irq(unsigned int);
328 extern void enable_irq(unsigned int);
329
330 /* variable for keeping track of number of interrupts generated during 
331  * interrupt test routine 
332  */
333 static int Intr_test_counter;
334
335 /****** Function Prototypes *************************************************/
336
337 /* WAN link driver entry points. These are called by the WAN router module. */
338 static int update(wan_device_t *wandev);
339 static int new_if(wan_device_t *wandev, netdevice_t *dev, wanif_conf_t *conf);
340 static int del_if(wan_device_t *wandev, netdevice_t *dev);
341 static void disable_comm (sdla_t *card);
342
343 /* WANPIPE-specific entry points */
344 static int wpf_exec(struct sdla *card, void *u_cmd, void *u_data);
345
346 /* Network device interface */
347 static int if_init(netdevice_t *dev);
348 static int if_open(netdevice_t *dev);
349 static int if_close(netdevice_t *dev);
350
351
352 #ifdef LINUX_2_4
353 static void if_tx_timeout (netdevice_t *dev);
354 #endif
355
356 #if defined(LINUX_2_1) || defined(LINUX_2_4)
357 static int if_rebuild_hdr (struct sk_buff *skb);
358 #else
359 static int if_rebuild_hdr (void* hdr, netdevice_t* dev, unsigned long raddr,
360         struct sk_buff* skb);
361 #endif
362
363 static int if_send(struct sk_buff *skb, netdevice_t *dev);
364 static int chk_bcast_mcast_addr(sdla_t *card, netdevice_t* dev,
365                                 struct sk_buff *skb);
366 #if defined(LINUX_2_1) || defined(LINUX_2_4)
367 static struct net_device_stats *if_stats(netdevice_t *dev);
368 #else
369 static struct enet_statistics* if_stats (netdevice_t* dev);
370 #endif
371
372 /* Interrupt handlers */
373 static void fr_isr(sdla_t *card);
374 static void rx_intr(sdla_t *card);
375 static void tx_intr(sdla_t *card);
376 static void timer_intr(sdla_t *card);
377 static void spur_intr(sdla_t *card);
378
379 /* Frame relay firmware interface functions */
380 static int fr_read_version(sdla_t *card, char *str);
381 static int fr_configure(sdla_t *card, fr_conf_t *conf);
382 static int fr_dlci_configure(sdla_t *card, fr_dlc_conf_t *conf, unsigned dlci);
383 static int fr_init_dlci (sdla_t *card, fr_channel_t *chan);
384 static int fr_set_intr_mode (sdla_t *card, unsigned mode, unsigned mtu, unsigned short timeout);
385 static int fr_comm_enable(sdla_t *card);
386 static void fr_comm_disable(sdla_t *card);
387 static int fr_get_err_stats(sdla_t *card);
388 static int fr_get_stats(sdla_t *card);
389 static int fr_add_dlci(sdla_t *card, int dlci);
390 static int fr_activate_dlci(sdla_t *card, int dlci);
391 static int fr_delete_dlci (sdla_t* card, int dlci);
392 static int fr_issue_isf(sdla_t *card, int isf);
393 static int fr_send(sdla_t *card, int dlci, unsigned char attr, int len,
394         void *buf);
395 static int fr_send_data_header(sdla_t *card, int dlci, unsigned char attr, int len,
396         void *buf,unsigned char hdr_len);
397 static unsigned int fr_send_hdr(sdla_t *card, int dlci, unsigned int offset);
398
399 static int check_dlci_config (sdla_t *card, fr_channel_t *chan);
400 static void initialize_rx_tx_buffers (sdla_t *card);
401
402
403 /* Firmware asynchronous event handlers */
404 static int fr_event(sdla_t *card, int event, fr_mbox_t *mbox);
405 static int fr_modem_failure(sdla_t *card, fr_mbox_t *mbox);
406 static int fr_dlci_change(sdla_t *card, fr_mbox_t *mbox);
407
408 /* Miscellaneous functions */
409 static int update_chan_state(netdevice_t *dev);
410 static void set_chan_state(netdevice_t *dev, int state);
411 static netdevice_t *find_channel(sdla_t *card, unsigned dlci);
412 static int is_tx_ready(sdla_t *card, fr_channel_t *chan);
413 static unsigned int dec_to_uint(unsigned char *str, int len);
414 static int reply_udp( unsigned char *data, unsigned int mbox_len );
415
416 static int intr_test( sdla_t* card );
417 static void init_chan_statistics( fr_channel_t* chan );
418 static void init_global_statistics( sdla_t* card );
419 static void read_DLCI_IB_mapping( sdla_t* card, fr_channel_t* chan );
420 static int setup_for_delayed_transmit(netdevice_t* dev, struct sk_buff *skb);
421
422 netdevice_t * move_dev_to_next (sdla_t *, netdevice_t *);
423 static int check_tx_status(sdla_t *, netdevice_t *);
424
425 #if defined(LINUX_2_1) || defined(LINUX_2_4)
426 /* Frame Relay Socket API */
427 static void trigger_fr_bh (fr_channel_t *);
428 static void fr_bh (netdevice_t *);
429 static int fr_bh_cleanup (netdevice_t *);
430 static int bh_enqueue (netdevice_t *, struct sk_buff *);
431 #endif
432
433 static void trigger_fr_poll (netdevice_t *);
434 static void fr_poll (netdevice_t *);
435 //static void add_gateway (netdevice_t *);
436
437 static void trigger_unconfig_fr (netdevice_t *dev);
438 static void unconfig_fr (sdla_t *);
439
440 static void trigger_config_fr (sdla_t *);
441 static void config_fr (sdla_t *);
442
443
444 /* Inverse ARP and Dynamic routing functions */
445 int process_ARP(arphdr_1490_t *ArpPacket, sdla_t *card, netdevice_t *dev);
446 int is_arp(void *buf);
447 int send_inarp_request(sdla_t *card, netdevice_t *dev);
448
449 static void trigger_fr_arp (netdevice_t *);
450 static void fr_arp (unsigned long data);
451
452
453 /* Udp management functions */
454 static int process_udp_mgmt_pkt(sdla_t *card);
455 static int udp_pkt_type( struct sk_buff *skb, sdla_t *card );
456 static int store_udp_mgmt_pkt(int udp_type, char udp_pkt_src, sdla_t* card,
457                                 struct sk_buff *skb, int dlci);
458
459 /* IPX functions */
460 static void switch_net_numbers(unsigned char *sendpacket,
461         unsigned long network_number, unsigned char incoming);
462
463 static int handle_IPXWAN(unsigned char *sendpacket, char *devname,
464         unsigned char enable_IPX, unsigned long network_number);
465
466 /* Lock Functions: SMP supported */
467 void    s508_s514_unlock(sdla_t *card, unsigned long *smp_flags);
468 void    s508_s514_lock(sdla_t *card, unsigned long *smp_flags);
469
470 unsigned short calc_checksum (char *, int);
471 static int setup_fr_header(struct sk_buff** skb, netdevice_t* dev, char op_mode);
472
473
474 /****** Public Functions ****************************************************/
475
476 /*============================================================================
477  * Frame relay protocol initialization routine.
478  *
479  * This routine is called by the main WANPIPE module during setup.  At this
480  * point adapter is completely initialized and firmware is running.
481  *  o read firmware version (to make sure it's alive)
482  *  o configure adapter
483  *  o initialize protocol-specific fields of the adapter data space.
484  *
485  * Return:      0       o.k.
486  *              < 0     failure.
487  */
488 int wpf_init(sdla_t *card, wandev_conf_t *conf)
489 {
490
491         int err;
492         fr508_flags_t* flags;
493
494         union
495         {
496                 char str[80];
497                 fr_conf_t cfg;
498         } u;
499
500         fr_buf_info_t* buf_info;
501         int i;
502
503
504         printk(KERN_INFO "\n");
505
506         /* Verify configuration ID */
507         if (conf->config_id != WANCONFIG_FR) {
508                 
509                 printk(KERN_INFO "%s: invalid configuration ID %u!\n",
510                         card->devname, conf->config_id);
511                 return -EINVAL;
512         
513         }
514
515         /* Initialize protocol-specific fields of adapter data space */
516         switch (card->hw.fwid) {
517         
518                 case SFID_FR508:
519                         card->mbox  = (void*)(card->hw.dpmbase + 
520                                         FR508_MBOX_OFFS);
521                         card->flags = (void*)(card->hw.dpmbase + 
522                                         FR508_FLAG_OFFS);
523                         if(card->hw.type == SDLA_S514) {
524                                 card->mbox += FR_MB_VECTOR;
525                                 card->flags += FR_MB_VECTOR;
526                         }
527                         card->isr = &fr_isr;
528                         break;
529
530                 default:
531                         return -EINVAL;
532         }
533
534         flags = card->flags;
535
536         /* Read firmware version.  Note that when adapter initializes, it
537          * clears the mailbox, so it may appear that the first command was
538          * executed successfully when in fact it was merely erased. To work
539          * around this, we execute the first command twice.
540          */
541
542         if (fr_read_version(card, NULL) || fr_read_version(card, u.str))
543                 return -EIO;
544
545         printk(KERN_INFO "%s: running frame relay firmware v%s\n",
546                 card->devname, u.str);
547
548         /* Adjust configuration */
549         conf->mtu += FR_HEADER_LEN;
550         conf->mtu = (conf->mtu >= MIN_LGTH_FR_DATA_CFG) ?
551                         min(conf->mtu, FR_MAX_NO_DATA_BYTES_IN_FRAME) :
552                         FR_CHANNEL_MTU + FR_HEADER_LEN;
553      
554         conf->bps = min(conf->bps, 2048000);
555
556         /* Initialze the configuration structure sent to the board to zero */
557         memset(&u.cfg, 0, sizeof(u.cfg));
558
559         memset(card->u.f.dlci_to_dev_map, 0, sizeof(card->u.f.dlci_to_dev_map));
560         
561         /* Configure adapter firmware */
562
563         u.cfg.mtu       = conf->mtu;
564         u.cfg.kbps      = conf->bps / 1000;
565
566         u.cfg.cir_fwd = u.cfg.cir_bwd = 16;
567         u.cfg.bc_fwd  = u.cfg.bc_bwd = 16;
568         
569         u.cfg.options   = 0x0000;
570         printk(KERN_INFO "%s: Global CIR enabled by Default\n", card->devname);
571         
572         switch (conf->u.fr.signalling) {
573
574                 case WANOPT_FR_ANSI:
575                         u.cfg.options = 0x0000; 
576                         break;          
577         
578                 case WANOPT_FR_Q933:    
579                         u.cfg.options |= 0x0200; 
580                         break;
581         
582                 case WANOPT_FR_LMI:     
583                         u.cfg.options |= 0x0400; 
584                         break;
585
586                 case WANOPT_NO:
587                         u.cfg.options |= 0x0800; 
588                         break;
589                 default:
590                         printk(KERN_INFO "%s: Illegal Signalling option\n",
591                                         card->wandev.name);
592                         return -EINVAL;
593         }
594
595
596         card->wandev.signalling = conf->u.fr.signalling;
597
598         if (conf->station == WANOPT_CPE) {
599
600
601                 if (conf->u.fr.signalling == WANOPT_NO){
602                         printk(KERN_INFO 
603                                 "%s: ERROR - For NO signalling, station must be set to Node!",
604                                          card->devname);
605                         return -EINVAL;
606                 }
607
608                 u.cfg.station = 0;
609                 u.cfg.options |= 0x8000;        /* auto config DLCI */
610                 card->u.f.dlci_num  = 0;
611         
612         } else {
613
614                 u.cfg.station = 1;      /* switch emulation mode */
615
616                 /* For switch emulation we have to create a list of dlci(s)
617                  * that will be sent to be global SET_DLCI_CONFIGURATION 
618                  * command in fr_configure() routine. 
619                  */
620
621                 card->u.f.dlci_num  = min(max(conf->u.fr.dlci_num, 1), 100);
622         
623                 for ( i = 0; i < card->u.f.dlci_num; i++) {
624
625                         card->u.f.node_dlci[i] = (unsigned short) 
626                                 conf->u.fr.dlci[i] ? conf->u.fr.dlci[i] : 16;
627         
628                 }
629         }
630
631         if (conf->clocking == WANOPT_INTERNAL)
632                 u.cfg.port |= 0x0001;
633
634         if (conf->interface == WANOPT_RS232)
635                 u.cfg.port |= 0x0002;
636
637         if (conf->u.fr.t391)
638                 u.cfg.t391 = min(conf->u.fr.t391, 30);
639         else
640                 u.cfg.t391 = 5;
641
642         if (conf->u.fr.t392)
643                 u.cfg.t392 = min(conf->u.fr.t392, 30);
644         else
645                 u.cfg.t392 = 15;
646
647         if (conf->u.fr.n391)
648                 u.cfg.n391 = min(conf->u.fr.n391, 255);
649         else
650                 u.cfg.n391 = 2;
651
652         if (conf->u.fr.n392)
653                 u.cfg.n392 = min(conf->u.fr.n392, 10);
654         else
655                 u.cfg.n392 = 3; 
656
657         if (conf->u.fr.n393)
658                 u.cfg.n393 = min(conf->u.fr.n393, 10);
659         else
660                 u.cfg.n393 = 4;
661
662         if (fr_configure(card, &u.cfg))
663                 return -EIO;
664
665         if (card->hw.type == SDLA_S514) {
666         
667                 buf_info = (void*)(card->hw.dpmbase + FR_MB_VECTOR +
668                         FR508_RXBC_OFFS);
669
670                 card->rxmb = (void*)(buf_info->rse_next + card->hw.dpmbase);
671
672                 card->u.f.rxmb_base =
673                         (void*)(buf_info->rse_base + card->hw.dpmbase); 
674
675                 card->u.f.rxmb_last =
676                         (void*)(buf_info->rse_base +
677                         (buf_info->rse_num - 1) * sizeof(fr_rx_buf_ctl_t) +
678                         card->hw.dpmbase);
679         }else{  
680                 buf_info = (void*)(card->hw.dpmbase + FR508_RXBC_OFFS);
681
682                 card->rxmb = (void*)(buf_info->rse_next -
683                         FR_MB_VECTOR + card->hw.dpmbase);
684                 
685                 card->u.f.rxmb_base =
686                         (void*)(buf_info->rse_base -
687                         FR_MB_VECTOR + card->hw.dpmbase);
688                 
689                 card->u.f.rxmb_last =
690                         (void*)(buf_info->rse_base +
691                         (buf_info->rse_num - 1) * sizeof(fr_rx_buf_ctl_t) -
692                         FR_MB_VECTOR + card->hw.dpmbase);
693         }
694
695         card->u.f.rx_base = buf_info->buf_base;
696         card->u.f.rx_top  = buf_info->buf_top;
697
698         card->u.f.tx_interrupts_pending = 0;
699
700         card->wandev.mtu        = conf->mtu;
701         card->wandev.bps        = conf->bps;
702         card->wandev.interface  = conf->interface;
703         card->wandev.clocking   = conf->clocking;
704         card->wandev.station    = conf->station;
705         card->poll              = NULL; 
706         card->exec              = &wpf_exec;
707         card->wandev.update     = &update;
708         card->wandev.new_if     = &new_if;
709         card->wandev.del_if     = &del_if;
710         card->wandev.state      = WAN_DISCONNECTED;
711         card->wandev.ttl        = conf->ttl;
712         card->wandev.udp_port   = conf->udp_port;       
713         card->disable_comm      = &disable_comm;        
714         card->u.f.arp_dev       = NULL;
715
716         /* Intialize global statistics for a card */
717         init_global_statistics( card );
718
719         card->TracingEnabled          = 0;
720
721         /* Interrupt Test */
722         Intr_test_counter = 0;
723         card->intr_mode = INTR_TEST_MODE;
724         err = intr_test( card );
725
726         printk(KERN_INFO "%s: End of Interrupt Test rc=0x%x  count=%i\n",
727                         card->devname,err,Intr_test_counter); 
728         
729         if (err || (Intr_test_counter < MAX_INTR_TEST_COUNTER)) {
730                 printk(KERN_ERR "%s: Interrupt Test Failed, Counter: %i\n", 
731                         card->devname, Intr_test_counter);
732                 printk(KERN_ERR "Please choose another interrupt\n");
733                 err = -EIO;
734                 return err;
735         }
736
737         printk(KERN_INFO "%s: Interrupt Test Passed, Counter: %i\n",
738                         card->devname, Intr_test_counter);
739
740
741         /* Apr 28 2000. Nenad Corbic
742          * Enable commnunications here, not in if_open or new_if, since
743          * interfaces come down when the link is disconnected. 
744          */
745          
746         /* If you enable comms and then set ints, you get a Tx int as you
747          * perform the SET_INT_TRIGGERS command. So, we only set int
748          * triggers and then adjust the interrupt mask (to disable Tx ints)
749          * before enabling comms. 
750          */     
751         if (fr_set_intr_mode(card, (FR_INTR_RXRDY | FR_INTR_TXRDY |
752                 FR_INTR_DLC | FR_INTR_TIMER | FR_INTR_TX_MULT_DLCIs) ,
753                 card->wandev.mtu, 0)) {
754                 return -EIO;
755         }
756
757         flags->imask &= ~(FR_INTR_TXRDY | FR_INTR_TIMER);
758  
759         if (fr_comm_enable(card)) {
760                 return -EIO;
761         }       
762         wanpipe_set_state(card, WAN_CONNECTED);
763         spin_lock_init(&card->u.f.if_send_lock);
764         
765         printk(KERN_INFO "\n");
766
767         return 0;
768 }
769
770 /******* WAN Device Driver Entry Points *************************************/
771
772 /*============================================================================
773  * Update device status & statistics.
774  */
775 static int update (wan_device_t* wandev)
776 {
777         volatile sdla_t* card;
778         unsigned long timeout;
779         fr508_flags_t* flags;
780
781         /* sanity checks */
782         if ((wandev == NULL) || (wandev->private == NULL))
783                 return -EFAULT;
784
785         if (wandev->state == WAN_UNCONFIGURED)
786                 return -ENODEV;
787
788         card = wandev->private;
789         flags = card->flags;
790
791
792         card->u.f.update_comms_stats = 1;
793         card->u.f.timer_int_enabled |= TMR_INT_ENABLED_UPDATE;
794         flags->imask |= FR_INTR_TIMER;
795         timeout = jiffies;
796         for(;;) {
797                 if(card->u.f.update_comms_stats == 0)
798                         break;
799                 if ((jiffies - timeout) > (1 * HZ)){
800                         card->u.f.update_comms_stats = 0;
801                         return -EAGAIN;
802                 }
803         }
804
805         return 0;
806 }
807
808 /*============================================================================
809  * Create new logical channel.
810  * This routine is called by the router when ROUTER_IFNEW IOCTL is being
811  * handled.
812  * o parse media- and hardware-specific configuration
813  * o make sure that a new channel can be created
814  * o allocate resources, if necessary
815  * o prepare network device structure for registaration.
816  *
817  * Return:      0       o.k.
818  *              < 0     failure (channel will not be created)
819  */
820 static int new_if (wan_device_t* wandev, netdevice_t* dev, wanif_conf_t* conf)
821 {
822         sdla_t* card = wandev->private;
823         fr_channel_t* chan;
824         int dlci = 0;
825         int err = 0;
826
827         
828         if ((conf->name[0] == '\0') || (strlen(conf->name) > WAN_IFNAME_SZ)) {
829                 
830                 printk(KERN_INFO "%s: Invalid interface name!\n",
831                         card->devname);
832                 return -EINVAL;
833         }
834
835         /* allocate and initialize private data */
836         chan = kmalloc(sizeof(fr_channel_t), GFP_KERNEL);
837
838         if (chan == NULL)
839                 return -ENOMEM;
840
841         memset(chan, 0, sizeof(fr_channel_t));
842         strcpy(chan->name, conf->name);
843         chan->card = card;
844
845         /* verify media address */
846         if (is_digit(conf->addr[0])) {
847
848                 dlci = dec_to_uint(conf->addr, 0);
849
850                 if (dlci && (dlci <= HIGHEST_VALID_DLCI)) {
851                 
852                         chan->dlci = dlci;
853                 
854                 } else {
855                 
856                         printk(KERN_ERR
857                                 "%s: Invalid DLCI %u on interface %s!\n",
858                                 wandev->name, dlci, chan->name);
859                         err = -EINVAL;
860                 }
861
862         } else {
863                 printk(KERN_ERR
864                         "%s: Invalid media address on interface %s!\n",
865                         wandev->name, chan->name);
866                 err = -EINVAL;
867         }
868
869         if ((chan->true_if_encoding = conf->true_if_encoding) == WANOPT_YES){
870                 printk(KERN_INFO 
871                         "%s: Enabling, true interface type encoding.\n",
872                         card->devname);
873         }
874         
875
876
877     /* Setup wanpipe as a router (WANPIPE) even if it is
878          * a bridged DLCI, or as an API 
879          */
880         if (strcmp(conf->usedby, "WANPIPE")  == 0  || 
881             strcmp(conf->usedby, "BRIDGE")   == 0  ||
882             strcmp(conf->usedby, "BRIDGE_N") == 0){
883                 
884                 if(strcmp(conf->usedby, "WANPIPE") == 0){
885                         chan->common.usedby = WANPIPE;
886                         
887                         printk(KERN_INFO "%s: Running in WANPIPE mode.\n", 
888                                         card->devname);
889                         
890                 }else if(strcmp(conf->usedby, "BRIDGE") == 0){
891                         
892                         chan->common.usedby = BRIDGE;
893                         
894 #if defined(LINUX_2_1) || defined(LINUX_2_4) 
895                         printk(KERN_INFO "%s: Running in WANPIPE (BRIDGE) mode.\n", 
896                                         card->devname);
897 #else
898                         printk(KERN_INFO "%s: WANPIPE Bridging mode not supported in 2.0.X kernels.\n",
899                                         card->devname);
900                         err = -EPROTONOSUPPORT;
901 #endif
902                 }else if( strcmp(conf->usedby, "BRIDGE_N") == 0 ){
903                         
904                         chan->common.usedby = BRIDGE_NODE;
905                 
906 #if defined(LINUX_2_1) || defined(LINUX_2_4)
907                         printk(KERN_INFO "%s: Running in WANPIPE (BRIDGE_NODE) mode.\n", 
908                                         card->devname);
909 #else
910                         printk(KERN_INFO "%s: WANPIPE Bridging mode not supported in 2.0.X kernels.\n",
911                                         card->devname);
912                         err = -EPROTONOSUPPORT;
913 #endif
914                 }
915
916                 if (!err){
917                         /* Dynamic interface configuration option.
918                          * On disconnect, if the options is selected,
919                          * the interface will be brought down */
920                         if (conf->if_down == WANOPT_YES){ 
921                                 set_bit(DYN_OPT_ON,&chan->interface_down);
922                                 printk(KERN_INFO 
923                                     "%s: Dynamic interface configuration enabled.\n",
924                                         card->devname);
925                         }
926                 }
927
928         } else if(strcmp(conf->usedby, "API") == 0){
929
930 #if defined(LINUX_2_1) || defined(LINUX_2_4) 
931                 chan->common.usedby = API;
932                 printk(KERN_INFO "%s: Running in API mode.\n",
933                         wandev->name);
934 #else
935                 printk(KERN_INFO "%s: The API Mode is not supported for"
936                                  "kernels lower than 2.2.X !\n",
937                                         wandev->name);
938                 printk(KERN_INFO "%s: Please upgrade to a 2.2.X kernel for the API support\n",
939                                         wandev->name);
940                 err = -EINVAL;
941 #endif
942         }
943
944         if (err) {
945                 
946                 kfree(chan);
947                 return err;
948         }
949
950         /* place cir,be,bc and other channel specific information into the
951          * chan structure 
952          */
953         if (conf->cir) {
954
955                 chan->cir = max( 1, min( conf->cir, 512 ) );
956                 chan->cir_status = CIR_ENABLED; 
957
958                 
959                 /* If CIR is enabled, force BC to equal CIR
960                  * this solves number of potential problems if CIR is 
961                  * set and BC is not 
962                  */
963                 chan->bc = chan->cir;
964
965                 if (conf->be){
966                         chan->be = max( 0, min( conf->be, 511) ); 
967                 }else{  
968                         conf->be = 0;
969                 }
970
971                 printk (KERN_INFO "%s: CIR enabled for DLCI %i \n",
972                                 wandev->name,chan->dlci);
973                 printk (KERN_INFO "%s:     CIR = %i ; BC = %i ; BE = %i\n",
974                                 wandev->name,chan->cir,chan->bc,chan->be);
975
976
977         }else{
978                 chan->cir_status = CIR_DISABLED;
979                 printk (KERN_INFO "%s: CIR disabled for DLCI %i\n",
980                                 wandev->name,chan->dlci);
981         }
982
983         chan->mc = conf->mc;
984
985         if (conf->inarp == WANOPT_YES){
986 #if defined(LINUX_2_1) || defined(LINUX_2_4)
987                 printk(KERN_INFO "%s: Inverse ARP Support Enabled\n",card->devname);
988                 chan->inarp = conf->inarp ? INARP_REQUEST : INARP_NONE;
989                 chan->inarp_interval = conf->inarp_interval ? conf->inarp_interval : 10;
990 #else
991                 printk(KERN_INFO "%s: Warning, Inverse ARP Support not available for 2.0.X kernels!\n",
992                                 card->devname);
993                 chan->inarp = INARP_NONE;
994                 chan->inarp_interval = 10;
995 #endif
996         }else{
997                 printk(KERN_INFO "%s: Inverse ARP Support Disabled\n",card->devname);
998                 chan->inarp = INARP_NONE;
999                 chan->inarp_interval = 10;
1000         }
1001
1002
1003         chan->dlci_configured = DLCI_NOT_CONFIGURED;    
1004
1005
1006         /*FIXME: IPX disabled in this WANPIPE version */
1007         if (conf->enable_IPX == WANOPT_YES){
1008                 printk(KERN_INFO "%s: ERROR - This version of WANPIPE doesn't support IPX\n",
1009                                 card->devname);
1010                 kfree(chan);
1011                 return -EINVAL;
1012         }else{
1013                 chan->enable_IPX = WANOPT_NO;
1014         }       
1015
1016         if (conf->network_number){
1017                 chan->network_number = conf->network_number;
1018         }else{
1019                 chan->network_number = 0xDEADBEEF;
1020         }
1021
1022         chan->route_flag = NO_ROUTE;
1023         
1024         init_chan_statistics(chan);
1025
1026         chan->transmit_length = 0;
1027
1028         /* prepare network device data space for registration */
1029 #ifdef LINUX_2_4
1030         strcpy(dev->name,chan->name);
1031 #else
1032         dev->name = (char *)kmalloc(strlen(chan->name) + 2, GFP_KERNEL); 
1033         if(dev->name == NULL)
1034         {
1035                 kfree(chan);
1036                 return -ENOMEM;
1037         }
1038         sprintf(dev->name, "%s", chan->name);
1039 #endif
1040         
1041         dev->init = &if_init;
1042         dev->priv = chan;
1043
1044         /* Initialize FR Polling Task Queue
1045          * We need a poll routine for each network
1046          * interface. 
1047          */
1048 #ifndef LINUX_2_4
1049         chan->fr_poll_task.next = NULL;
1050 #endif
1051         chan->fr_poll_task.sync = 0;
1052         chan->fr_poll_task.routine = (void *)(void *)fr_poll;
1053         chan->fr_poll_task.data = dev;
1054
1055         init_timer(&chan->fr_arp_timer);
1056         chan->fr_arp_timer.data=(unsigned long)dev;
1057         chan->fr_arp_timer.function = fr_arp;
1058
1059         wandev->new_if_cnt++;
1060
1061         /* Tells us that if this interface is a
1062          * gateway or not */
1063         if ((chan->gateway = conf->gateway) == WANOPT_YES){
1064                 printk(KERN_INFO "%s: Interface %s is set as a gateway.\n",
1065                         card->devname,dev->name);
1066         }
1067
1068         /* M. Grant Patch Apr 28 2000 
1069          * Disallow duplicate dlci configurations. */
1070         if (card->u.f.dlci_to_dev_map[chan->dlci] != NULL) {
1071                 kfree(chan);
1072                 return -EBUSY;
1073         }
1074
1075         /* Configure this dlci at a later date, when
1076          * the interface comes up. i.e. when if_open() 
1077          * executes */
1078         set_bit(0,&chan->config_dlci);
1079         
1080         printk(KERN_INFO "\n");
1081
1082         return 0;
1083 }
1084
1085 /*============================================================================
1086  * Delete logical channel.
1087  */
1088 static int del_if (wan_device_t* wandev, netdevice_t* dev)
1089 {
1090         fr_channel_t* chan = dev->priv;
1091         unsigned long smp_flags=0;
1092
1093         /* This interface is dead, make sure the 
1094          * ARP timer is stopped */
1095         del_timer(&chan->fr_arp_timer);
1096         
1097         /* If we are a NODE, we must unconfigure this DLCI
1098          * Trigger an unconfigure command that will
1099          * be executed in timer interrupt. We must wait
1100          * for the command to complete. */
1101         trigger_unconfig_fr(dev);
1102
1103         lock_adapter_irq(&wandev->lock, &smp_flags);
1104         wandev->new_if_cnt--;
1105         unlock_adapter_irq(&wandev->lock, &smp_flags);
1106
1107         return 0;
1108 }
1109
1110
1111 /*=====================================================================
1112  * disable_comm
1113  *
1114  * Description:
1115  *      Disable communications.
1116  *      This code runs in shutdown (sdlamain.c)
1117  *      under critical flag. Therefore it is not
1118  *      necessary to set a critical flag here 
1119  *
1120  * Usage:
1121  *      Commnunications are disabled only on a card
1122  *      shutdown.
1123  */
1124
1125 static void disable_comm (sdla_t *card)
1126 {
1127         printk(KERN_INFO "%s: Disabling Communications!\n",
1128                         card->devname);
1129         fr_comm_disable(card);
1130 }
1131
1132 /****** WANPIPE-specific entry points ***************************************/
1133
1134 /*============================================================================
1135  * Execute adapter interface command.
1136  */
1137 static int wpf_exec (struct sdla* card, void* u_cmd, void* u_data)
1138 {
1139         fr_mbox_t* mbox = card->mbox;
1140         int retry = MAX_CMD_RETRY;
1141         int err, len;
1142         fr_cmd_t cmd;
1143
1144 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1145
1146         if(copy_from_user((void*)&cmd, u_cmd, sizeof(cmd)))
1147                 return -EFAULT;
1148         
1149         /* execute command */
1150         do
1151         {
1152                 memcpy(&mbox->cmd, &cmd, sizeof(cmd));
1153                 
1154                 if (cmd.length){
1155                         if( copy_from_user((void*)&mbox->data, u_data, cmd.length))
1156                                 return -EFAULT;
1157                 }
1158                 
1159                 if (sdla_exec(mbox))
1160                         err = mbox->cmd.result;
1161
1162                 else return -EIO;
1163         
1164         } while (err && retry-- && fr_event(card, err, mbox));
1165
1166         /* return result */
1167         if (copy_to_user(u_cmd, (void*)&mbox->cmd, sizeof(fr_cmd_t)))
1168                 return -EFAULT;
1169
1170         len = mbox->cmd.length;
1171
1172         if (len && u_data && !copy_to_user(u_data, (void*)&mbox->data, len))
1173                 return -EFAULT;
1174         return 0;
1175
1176 #else
1177         if (!u_cmd || verify_area(VERIFY_WRITE, u_cmd, sizeof(fr_cmd_t)))
1178                 return -EFAULT;
1179
1180         memcpy_fromfs((void*)&cmd, u_cmd, sizeof(cmd));
1181
1182         if (cmd.length) {
1183
1184                 if (!u_data || verify_area(VERIFY_READ, u_data, cmd.length))
1185                         return -EFAULT;
1186         }
1187
1188         /* execute command */
1189         do
1190         {
1191                 memcpy(&mbox->cmd, &cmd, sizeof(cmd));
1192
1193                 if (cmd.length)
1194                         memcpy_fromfs((void*)&mbox->data, u_data, cmd.length);
1195
1196                 if (sdla_exec(mbox))
1197                         err = mbox->cmd.result;
1198
1199                 else return -EIO;
1200         } while (err && retry-- && fr_event(card, err, mbox));
1201
1202         /* return result */
1203         memcpy_tofs(u_cmd, (void*)&mbox->cmd, sizeof(fr_cmd_t));
1204         len = mbox->cmd.length;
1205
1206         if (len && u_data && !verify_area(VERIFY_WRITE, u_data, len))
1207                 memcpy_tofs(u_data, (void*)&mbox->data, len);
1208
1209         return 0;
1210
1211 #endif
1212
1213 }
1214
1215 /****** Network Device Interface ********************************************/
1216
1217 /*============================================================================
1218  * Initialize Linux network interface.
1219  *
1220  * This routine is called only once for each interface, during Linux network
1221  * interface registration.  Returning anything but zero will fail interface
1222  * registration.
1223  */
1224 static int if_init (netdevice_t* dev)
1225 {
1226         fr_channel_t* chan = dev->priv;
1227         sdla_t* card = chan->card;
1228         wan_device_t* wandev = &card->wandev;
1229 #ifdef LINUX_2_0
1230         int i;
1231 #endif
1232
1233         /* Initialize device driver entry points */
1234         dev->open               = &if_open;
1235         dev->stop               = &if_close;
1236         dev->hard_header        = NULL;
1237         dev->rebuild_header     = &if_rebuild_hdr;
1238         dev->hard_start_xmit    = &if_send;
1239         dev->get_stats          = &if_stats;
1240 #ifdef LINUX_2_4
1241         dev->tx_timeout         = &if_tx_timeout;
1242         dev->watchdog_timeo     = TX_TIMEOUT;
1243 #endif
1244         
1245         if (chan->common.usedby == WANPIPE || chan->common.usedby == API){
1246 #ifdef LINUX_2_0
1247                 dev->family = AF_INET;
1248 #endif
1249                 /* Initialize media-specific parameters */
1250                 if (chan->true_if_encoding){
1251                         dev->type               = ARPHRD_DLCI;  /* This breaks tcpdump */
1252                 }else{
1253                         dev->type               = ARPHRD_PPP;   /* ARP h/w type */
1254                 }
1255                 
1256                 dev->flags              |= IFF_POINTOPOINT;
1257                 dev->flags              |= IFF_NOARP;
1258
1259                 /* Enable Multicast addressing */
1260                 if (chan->mc == WANOPT_YES){
1261                         dev->flags      |= IFF_MULTICAST;
1262                 }
1263
1264                 dev->mtu                = wandev->mtu - FR_HEADER_LEN;
1265                 /* For an API, the maximum number of bytes that the stack will pass
1266                    to the driver is (dev->mtu + dev->hard_header_len). So, adjust the
1267                    mtu so that a frame of maximum size can be transmitted by the API. 
1268                 */
1269                 if(chan->common.usedby == API) {
1270                         dev->mtu += (sizeof(api_tx_hdr_t) - FR_HEADER_LEN);
1271                 }
1272                 
1273                 dev->hard_header_len    = FR_HEADER_LEN;/* media header length */
1274                 dev->addr_len           = 2;            /* hardware address length */
1275                 *(unsigned short*)dev->dev_addr = htons(chan->dlci);
1276
1277                 /* Set transmit buffer queue length */
1278                 dev->tx_queue_len = 100;
1279
1280                         /* Initialize socket buffers */
1281 #if !defined(LINUX_2_1) && !defined(LINUX_2_4)
1282                 for (i = 0; i < DEV_NUMBUFFS; ++i)
1283                         skb_queue_head_init(&dev->buffs[i]);
1284 #endif
1285         }else{
1286
1287                 /* Setup the interface for Bridging */
1288                 int hw_addr=0;
1289                 ether_setup(dev);
1290                 
1291                 /* Use a random number to generate the MAC address */
1292                 memcpy(dev->dev_addr, "\xFE\xFC\x00\x00\x00\x00", 6);
1293                 get_random_bytes(&hw_addr, sizeof(hw_addr));
1294                 *(int *)(dev->dev_addr + 2) += hw_addr;
1295         }
1296                 
1297         /* Initialize hardware parameters (just for reference) */
1298         dev->irq        = wandev->irq;
1299         dev->dma        = wandev->dma;
1300         dev->base_addr  = wandev->ioport;
1301         dev->mem_start  = wandev->maddr;
1302         dev->mem_end    = wandev->maddr + wandev->msize - 1;
1303
1304         return 0;
1305 }
1306
1307 /*============================================================================
1308  * Open network interface.
1309  * o if this is the first open, then enable communications and interrupts.
1310  * o prevent module from unloading by incrementing use count
1311  *
1312  * Return 0 if O.k. or errno.
1313  */
1314 static int if_open (netdevice_t* dev)
1315 {
1316         fr_channel_t* chan = dev->priv;
1317         sdla_t* card = chan->card;
1318         int err = 0;
1319         struct timeval tv;
1320
1321         if (is_dev_running(dev))
1322                 return -EBUSY;
1323         
1324 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1325         /* Initialize the task queue */
1326         chan->tq_working=0;
1327
1328 #ifndef LINUX_2_4
1329         chan->common.wanpipe_task.next = NULL;
1330 #endif
1331         chan->common.wanpipe_task.sync = 0;
1332         chan->common.wanpipe_task.routine = (void *)(void *)fr_bh;
1333         chan->common.wanpipe_task.data = dev;
1334
1335         /* Allocate and initialize BH circular buffer */
1336         chan->bh_head = kmalloc((sizeof(bh_data_t)*MAX_BH_BUFF),GFP_ATOMIC);
1337         memset(chan->bh_head,0,(sizeof(bh_data_t)*MAX_BH_BUFF));
1338         atomic_set(&chan->bh_buff_used, 0);
1339 #endif
1340
1341 #ifdef LINUX_2_4
1342         netif_start_queue(dev);
1343 #else   
1344         dev->interrupt = 0;
1345         dev->tbusy = 0;
1346         dev->start = 1;
1347 #endif
1348
1349         wanpipe_open(card);
1350         do_gettimeofday( &tv );
1351         chan->router_start_time = tv.tv_sec;
1352         
1353         if (test_bit(0,&chan->config_dlci)){
1354                 trigger_config_fr (card);
1355         }else if (chan->inarp == INARP_REQUEST){
1356                 trigger_fr_arp(dev);
1357         }
1358         
1359         return err;
1360 }
1361
1362 /*============================================================================
1363  * Close network interface.
1364  * o if this is the last open, then disable communications and interrupts.
1365  * o reset flags.
1366  */
1367 static int if_close (netdevice_t* dev)
1368 {
1369         fr_channel_t* chan = dev->priv;
1370         sdla_t* card = chan->card;
1371
1372         if (chan->inarp == INARP_CONFIGURED) {
1373                 chan->inarp = INARP_REQUEST;
1374         }
1375
1376         stop_net_queue(dev);
1377 #ifndef LINUX_2_4
1378         dev->start=0;
1379 #endif
1380         wanpipe_close(card);
1381
1382         return 0;
1383 }
1384
1385 /*============================================================================
1386  * Re-build media header.
1387  *
1388  * Return:      1       physical address resolved.
1389  *              0       physical address not resolved
1390  */
1391 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1392 static int if_rebuild_hdr (struct sk_buff* skb)
1393 {
1394 #else
1395 static int if_rebuild_hdr (void* hdr, netdevice_t* dev, unsigned long raddr,
1396                            struct sk_buff* skb)
1397 {
1398 #endif
1399
1400 #if defined(LINUX_2_1) || defined(LINUX_2_4) 
1401         netdevice_t *dev = skb->dev;
1402 #endif
1403         fr_channel_t* chan = dev->priv;
1404         sdla_t* card = chan->card;
1405
1406         printk(KERN_INFO "%s: rebuild_header() called for interface %s!\n",
1407                 card->devname, dev->name);
1408         return 1;
1409 }
1410
1411 #ifdef LINUX_2_4
1412 /*============================================================================
1413  * Handle transmit timeout event from netif watchdog
1414  */
1415 static void if_tx_timeout (netdevice_t *dev)
1416 {
1417         fr_channel_t* chan = dev->priv;
1418         sdla_t *card = chan->card;
1419
1420         /* If our device stays busy for at least 5 seconds then we will
1421          * kick start the device by making dev->tbusy = 0.  We expect
1422          * that our device never stays busy more than 5 seconds. So this                 
1423          * is only used as a last resort.
1424          */
1425
1426         chan->drvstats_if_send.if_send_tbusy++;
1427         ++chan->ifstats.collisions;
1428
1429         printk (KERN_INFO "%s: Transmit timed out on %s\n", 
1430                         card->devname, dev->name);
1431         chan->drvstats_if_send.if_send_tbusy_timeout++;
1432         netif_wake_queue (dev);
1433
1434 }
1435 #endif
1436
1437 /*============================================================================
1438  * Send a packet on a network interface.
1439  * o set tbusy flag (marks start of the transmission) to block a timer-based
1440  *   transmit from overlapping.
1441  * o set critical flag when accessing board.
1442  * o check link state. If link is not up, then drop the packet.
1443  * o check channel status. If it's down then initiate a call.
1444  * o pass a packet to corresponding WAN device.
1445  * o free socket buffer
1446  *
1447  * Return:      0       complete (socket buffer must be freed)
1448  *              non-0   packet may be re-transmitted (tbusy must be set)
1449  *
1450  * Notes:
1451  * 1. This routine is called either by the protocol stack or by the "net
1452  *    bottom half" (with interrupts enabled).
1453  * 
1454  * 2. Using the start_net_queue() and stop_net_queue() MACROS
1455  *    will inhibit further transmit requests from the protocol stack 
1456  *    and can be used for flow control with protocol layer.
1457  */
1458 static int if_send (struct sk_buff* skb, netdevice_t* dev)
1459 {
1460         fr_channel_t* chan = dev->priv;
1461         sdla_t* card = chan->card;
1462         int err;
1463         unsigned char *sendpacket;
1464         fr508_flags_t* adptr_flags = card->flags;
1465         int udp_type, delay_tx_queued=0;
1466         unsigned long smp_flags=0;
1467         unsigned char attr = 0;
1468
1469         chan->drvstats_if_send.if_send_entry++;
1470
1471 #ifdef LINUX_2_4
1472         netif_stop_queue(dev);
1473 #endif
1474         
1475         if (skb == NULL) {             
1476                 /* if we get here, some higher layer thinks we've missed an
1477                  * tx-done interrupt.
1478                  */
1479                 printk(KERN_INFO "%s: interface %s got kicked!\n", 
1480                         card->devname, dev->name);
1481                 chan->drvstats_if_send.if_send_skb_null ++;
1482
1483                 wake_net_dev(dev);
1484                 return 0;
1485         }
1486
1487         /* If a peripheral task is running just drop packets */
1488         if (test_bit(PERI_CRIT, &card->wandev.critical)){
1489                 
1490                 printk(KERN_INFO "%s: Critical in if_send(): Peripheral running!\n",
1491                                 card->devname);
1492                 
1493                 wan_dev_kfree_skb(skb,FREE_WRITE);
1494                 start_net_queue(dev);
1495                 return 0;
1496         }
1497
1498         /* We must set the 'tbusy' flag if we already have a packet queued for
1499            transmission in the transmit interrupt handler. However, we must
1500            ensure that the transmit interrupt does not reset the 'tbusy' flag
1501            just before we set it, as this will result in a "transmit timeout".
1502         */
1503         set_bit(SEND_TXIRQ_CRIT, (void*)&card->wandev.critical);
1504         if(chan->transmit_length) {
1505                 stop_net_queue(dev);
1506                 chan->tick_counter = jiffies;
1507                 clear_bit(SEND_TXIRQ_CRIT, (void*)&card->wandev.critical);
1508                 return 1;
1509         }
1510         clear_bit(SEND_TXIRQ_CRIT, (void*)&card->wandev.critical);
1511  
1512 #ifndef LINUX_2_4
1513         if (dev->tbusy) {
1514
1515                 /* If our device stays busy for at least 5 seconds then we will
1516                  * kick start the device by making dev->tbusy = 0.  We expect
1517                  * that our device never stays busy more than 5 seconds. So this                 
1518                  * is only used as a last resort.
1519                  */
1520                 
1521                 chan->drvstats_if_send.if_send_tbusy++;
1522                 ++chan->ifstats.collisions;
1523
1524                 if ((jiffies - chan->tick_counter) < (5 * HZ)) {
1525                         return 1;
1526                 }
1527
1528                 printk(KERN_INFO "%s: Transmit timed out on %s\n", 
1529                                 card->devname, chan->name);
1530                 chan->drvstats_if_send.if_send_tbusy_timeout ++;
1531                 dev->tbusy = 0;
1532         }
1533 #endif
1534
1535         
1536         /* Move the if_header() code to here. By inserting frame
1537          * relay header in if_header() we would break the
1538          * tcpdump and other packet sniffers */
1539         chan->fr_header_len = setup_fr_header(&skb,dev,chan->common.usedby);
1540         if (chan->fr_header_len < 0 ){
1541                 ++chan->ifstats.tx_dropped;
1542                 ++card->wandev.stats.tx_dropped;
1543                 
1544                 wan_dev_kfree_skb(skb,FREE_WRITE);
1545                 start_net_queue(dev);   
1546                 return 0;
1547         }
1548
1549         sendpacket = skb->data;
1550
1551         udp_type = udp_pkt_type(skb, card);
1552
1553         if(udp_type != UDP_INVALID_TYPE) {
1554                 if(store_udp_mgmt_pkt(udp_type, UDP_PKT_FRM_STACK, card, skb,
1555                         chan->dlci)) {
1556                         adptr_flags->imask |= FR_INTR_TIMER;
1557                         if (udp_type == UDP_FPIPE_TYPE){
1558                                 chan->drvstats_if_send.
1559                                         if_send_PIPE_request ++;
1560                         }
1561                 }
1562                 start_net_queue(dev);
1563                 return 0;
1564         }
1565
1566         //FIXME: can we do better than sendpacket[2]?
1567         if ((chan->common.usedby == WANPIPE) && (sendpacket[2] == 0x45)) {
1568                 
1569                 /* check to see if the source IP address is a broadcast or */
1570                 /* multicast IP address */
1571                 if(chk_bcast_mcast_addr(card, dev, skb)){
1572                         ++chan->ifstats.tx_dropped;
1573                         ++card->wandev.stats.tx_dropped;
1574                         wan_dev_kfree_skb(skb, FREE_WRITE);
1575                         start_net_queue(dev);
1576                         return 0;
1577                 }
1578         }
1579
1580         
1581         /* Lock the S514/S508 card: SMP Supported */
1582         s508_s514_lock(card,&smp_flags);
1583
1584         if (test_and_set_bit(SEND_CRIT, (void*)&card->wandev.critical)) {
1585                 
1586                 chan->drvstats_if_send.if_send_critical_non_ISR ++;
1587                 chan->ifstats.tx_dropped ++;
1588                 printk(KERN_INFO "%s Critical in IF_SEND: if_send() already running!\n", 
1589                                 card->devname);
1590                 goto if_send_start_and_exit;
1591         }
1592         
1593         /* API packet check: minimum packet size must be greater than 
1594          * 16 byte API header */
1595         if((chan->common.usedby == API) && (skb->len <= sizeof(api_tx_hdr_t))) {
1596                 ++chan->ifstats.tx_dropped;
1597                 ++card->wandev.stats.tx_dropped;
1598             
1599                 
1600                 goto if_send_start_and_exit;
1601
1602         }else{
1603                 /* During API transmission, get rid of the API header */
1604                 if (chan->common.usedby == API) {
1605                         api_tx_hdr_t* api_tx_hdr;
1606                         api_tx_hdr = (api_tx_hdr_t*)&skb->data[0x00];
1607                         attr = api_tx_hdr->attr;
1608                         skb_pull(skb,sizeof(api_tx_hdr_t));
1609                 }
1610         }
1611
1612         if (card->wandev.state != WAN_CONNECTED) {
1613                 chan->drvstats_if_send.if_send_wan_disconnected ++;
1614                 ++chan->ifstats.tx_dropped;
1615                 ++card->wandev.stats.tx_dropped;
1616         
1617         } else if (chan->common.state != WAN_CONNECTED) {
1618                 chan->drvstats_if_send.if_send_dlci_disconnected ++;
1619
1620                 /* Update the DLCI state in timer interrupt */
1621                 card->u.f.timer_int_enabled |= TMR_INT_ENABLED_UPDATE_STATE;    
1622                 adptr_flags->imask |= FR_INTR_TIMER;
1623
1624                 ++chan->ifstats.tx_dropped;
1625                 ++card->wandev.stats.tx_dropped;
1626                 
1627         } else if (!is_tx_ready(card, chan)) {
1628                 /* No tx buffers available, store for delayed transmit */
1629                 if (!setup_for_delayed_transmit(dev, skb)){
1630                         set_bit(1,&delay_tx_queued);
1631                 }
1632                 chan->drvstats_if_send.if_send_no_bfrs++;
1633                 
1634         } else if (!skb->protocol) {
1635                 /* No protocols drop packet */
1636                 chan->drvstats_if_send.if_send_protocol_error ++;
1637                 ++card->wandev.stats.tx_errors;
1638         
1639         } else if (test_bit(ARP_CRIT,&card->wandev.critical)){
1640                 /* We are trying to send an ARP Packet, block IP data until
1641                  * ARP is sent */
1642                 ++chan->ifstats.tx_dropped;
1643                 ++card->wandev.stats.tx_dropped;
1644                 
1645         } else {
1646                 //FIXME: IPX is not implemented in this version of Frame Relay ?
1647                 if((chan->common.usedby == WANPIPE) &&
1648                         sendpacket[1] == 0x00 &&
1649                         sendpacket[2] == 0x80 &&
1650                         sendpacket[6] == 0x81 &&
1651                         sendpacket[7] == 0x37) {
1652                         
1653                         if( chan->enable_IPX ) {
1654                                 switch_net_numbers(sendpacket, 
1655                                                 chan->network_number, 0);
1656                         } else {
1657                                 //FIXME: Take this out when IPX is fixed 
1658                                 printk(KERN_INFO 
1659                                 "%s: WARNING: Unsupported IPX data in send, packet dropped\n",
1660                                         card->devname);
1661                         }
1662                         
1663                 }else{
1664                         err = fr_send_data_header(card, chan->dlci, attr, skb->len, skb->data, chan->fr_header_len);
1665                         if (err) {
1666                                 switch(err) {
1667                                 case FRRES_CIR_OVERFLOW:
1668                                 case FRRES_BUFFER_OVERFLOW:
1669                                         if (!setup_for_delayed_transmit(dev, skb)){
1670                                                 set_bit(1,&delay_tx_queued);
1671                                         }
1672                                         chan->drvstats_if_send.
1673                                                 if_send_adptr_bfrs_full ++;
1674                                         break;
1675                                         
1676                                 case FRRES_TOO_LONG:
1677                                         if (net_ratelimit()){
1678                                                 printk(KERN_INFO 
1679                                                 "%s: Error: Frame too long, transmission failed %i\n",
1680                                                  card->devname, (unsigned int)skb->len);
1681                                         }
1682                                         /* Drop down to default */
1683                                 default:
1684                                         chan->drvstats_if_send.
1685                                                 if_send_dlci_disconnected ++;
1686                                         ++chan->ifstats.tx_dropped;
1687                                         ++card->wandev.stats.tx_dropped;
1688                                         break;
1689                                 }
1690                         } else {
1691                                 chan->drvstats_if_send.
1692                                         if_send_bfr_passed_to_adptr++;
1693                                 ++chan->ifstats.tx_packets;
1694                                 ++card->wandev.stats.tx_packets;
1695                                 
1696 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1697                                 chan->ifstats.tx_bytes += skb->len;
1698                                 card->wandev.stats.tx_bytes += skb->len;
1699 #endif
1700 #ifdef LINUX_2_4
1701                                 dev->trans_start = jiffies;
1702 #endif
1703                         }
1704                 }
1705         }
1706
1707 if_send_start_and_exit:
1708
1709         start_net_queue(dev);
1710         
1711         /* If we queued the packet for transmission, we must not
1712          * deallocate it. The packet is unlinked from the IP stack
1713          * not copied. Therefore, we must keep the original packet */
1714         if (!test_bit(1,&delay_tx_queued)) {
1715                 wan_dev_kfree_skb(skb, FREE_WRITE);
1716         }else{
1717                 adptr_flags->imask |= FR_INTR_TXRDY;
1718                 card->u.f.tx_interrupts_pending ++;
1719         }
1720
1721         clear_bit(SEND_CRIT, (void*)&card->wandev.critical);
1722
1723         s508_s514_unlock(card,&smp_flags);
1724
1725         return 0;
1726 }
1727
1728
1729
1730 /*============================================================================
1731  * Setup so that a frame can be transmitted on the occurence of a transmit
1732  * interrupt.
1733  */
1734 static int setup_for_delayed_transmit (netdevice_t* dev, struct sk_buff *skb)
1735 {
1736         fr_channel_t* chan = dev->priv;
1737         sdla_t* card = chan->card;
1738         fr_dlci_interface_t* dlci_interface;
1739         int len = skb->len;
1740
1741         /* Check that the dlci is properly configured,
1742          * before using tx interrupt */
1743         if (!chan->dlci_int_interface){
1744                 if (net_ratelimit()){ 
1745                         printk(KERN_INFO 
1746                                 "%s: ERROR on DLCI %i: Not configured properly !\n",
1747                                         card->devname, chan->dlci);
1748                         printk(KERN_INFO "%s: Please contact Sangoma Technologies\n",
1749                                         card->devname);
1750                 }
1751                 return 1;
1752         }
1753                 
1754         dlci_interface = chan->dlci_int_interface;
1755
1756         if(chan->transmit_length) {
1757                 printk(KERN_INFO "%s: Big mess in setup_for_del...\n",
1758                                 card->devname);
1759                 return 1;
1760         }
1761
1762         if(len > FR_MAX_NO_DATA_BYTES_IN_FRAME) {
1763                 //FIXME: increment some statistic */
1764                 return 1;
1765         }
1766
1767         skb_unlink(skb);
1768         
1769         chan->transmit_length = len;
1770         chan->delay_skb = skb;
1771         
1772         dlci_interface->gen_interrupt |= FR_INTR_TXRDY;
1773         dlci_interface->packet_length = len;
1774
1775         /* Turn on TX interrupt at the end of if_send */
1776         return 0;
1777 }
1778
1779
1780 /*============================================================================
1781  * Check to see if the packet to be transmitted contains a broadcast or
1782  * multicast source IP address.
1783  * Return 0 if not broadcast/multicast address, otherwise return 1.
1784  */
1785
1786 static int chk_bcast_mcast_addr(sdla_t *card, netdevice_t* dev,
1787                                 struct sk_buff *skb)
1788 {
1789         u32 src_ip_addr;
1790         u32 broadcast_ip_addr = 0;
1791 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1792         struct in_device *in_dev;
1793 #endif
1794         fr_channel_t* chan = dev->priv;
1795  
1796         /* read the IP source address from the outgoing packet */
1797         src_ip_addr = *(u32 *)(skb->data + 14);
1798
1799         /* read the IP broadcast address for the device */
1800 #if defined(LINUX_2_1) || defined(LINUX_2_4)
1801         in_dev = dev->ip_ptr;
1802         if(in_dev != NULL) {
1803                 struct in_ifaddr *ifa= in_dev->ifa_list;
1804                 if(ifa != NULL)
1805                         broadcast_ip_addr = ifa->ifa_broadcast;
1806                 else
1807                         return 0;
1808         }
1809 #else
1810         broadcast_ip_addr = dev->pa_brdaddr;
1811 #endif
1812
1813         /* check if the IP Source Address is a Broadcast address */
1814         if((dev->flags & IFF_BROADCAST) && (src_ip_addr == broadcast_ip_addr)) {
1815                 printk(KERN_INFO
1816                         "%s: Broadcast Source Address silently discarded\n",
1817                         card->devname);
1818                 return 1;
1819         }
1820
1821         /* check if the IP Source Address is a Multicast address */
1822         if((chan->mc == WANOPT_NO) && (ntohl(src_ip_addr) >= 0xE0000001) &&
1823                 (ntohl(src_ip_addr) <= 0xFFFFFFFE)) {
1824                 printk(KERN_INFO
1825                         "%s: Multicast Source Address silently discarded\n",
1826                         card->devname);
1827                 return 1;
1828         }
1829
1830         return 0;
1831 }
1832
1833 /*============================================================================
1834  * Reply to UDP Management system.
1835  * Return nothing.
1836  */
1837 static int reply_udp( unsigned char *data, unsigned int mbox_len ) 
1838 {
1839         unsigned short len, udp_length, temp, ip_length;
1840         unsigned long ip_temp;
1841         int even_bound = 0;
1842
1843   
1844         fr_udp_pkt_t *fr_udp_pkt = (fr_udp_pkt_t *)data; 
1845
1846         /* Set length of packet */
1847         len = //sizeof(fr_encap_hdr_t)+
1848               sizeof(ip_pkt_t)+ 
1849               sizeof(udp_pkt_t)+
1850               sizeof(wp_mgmt_t)+
1851               sizeof(cblock_t)+
1852               mbox_len;
1853  
1854
1855         /* fill in UDP reply */
1856         fr_udp_pkt->wp_mgmt.request_reply = UDPMGMT_REPLY;
1857   
1858         /* fill in UDP length */
1859         udp_length = sizeof(udp_pkt_t)+ 
1860                      sizeof(wp_mgmt_t)+
1861                      sizeof(cblock_t)+
1862                      mbox_len; 
1863
1864
1865         /* put it on an even boundary */
1866         if ( udp_length & 0x0001 ) {
1867                 udp_length += 1;
1868                 len += 1;
1869                 even_bound = 1;
1870         }
1871
1872         temp = (udp_length<<8)|(udp_length>>8);
1873         fr_udp_pkt->udp_pkt.udp_length = temp;
1874          
1875         /* swap UDP ports */
1876         temp = fr_udp_pkt->udp_pkt.udp_src_port;
1877         fr_udp_pkt->udp_pkt.udp_src_port = 
1878                         fr_udp_pkt->udp_pkt.udp_dst_port; 
1879         fr_udp_pkt->udp_pkt.udp_dst_port = temp;
1880
1881
1882
1883         /* add UDP pseudo header */
1884         temp = 0x1100;
1885         *((unsigned short *)
1886                 (fr_udp_pkt->data+mbox_len+even_bound)) = temp; 
1887         temp = (udp_length<<8)|(udp_length>>8);
1888         *((unsigned short *)
1889                 (fr_udp_pkt->data+mbox_len+even_bound+2)) = temp;
1890                  
1891         /* calculate UDP checksum */
1892         fr_udp_pkt->udp_pkt.udp_checksum = 0;
1893
1894         fr_udp_pkt->udp_pkt.udp_checksum = 
1895                 calc_checksum(&data[UDP_OFFSET/*+sizeof(fr_encap_hdr_t)*/],
1896                               udp_length+UDP_OFFSET);
1897
1898         /* fill in IP length */
1899         ip_length = udp_length + sizeof(ip_pkt_t);
1900         temp = (ip_length<<8)|(ip_length>>8);
1901         fr_udp_pkt->ip_pkt.total_length = temp;
1902   
1903         /* swap IP addresses */
1904         ip_temp = fr_udp_pkt->ip_pkt.ip_src_address;
1905         fr_udp_pkt->ip_pkt.ip_src_address = 
1906                                 fr_udp_pkt->ip_pkt.ip_dst_address;
1907         fr_udp_pkt->ip_pkt.ip_dst_address = ip_temp;
1908
1909                  
1910         /* fill in IP checksum */
1911         fr_udp_pkt->ip_pkt.hdr_checksum = 0;
1912         fr_udp_pkt->ip_pkt.hdr_checksum = 
1913                 calc_checksum(&data[/*sizeof(fr_encap_hdr_t)*/0],
1914                               sizeof(ip_pkt_t));
1915
1916         return len;
1917 } /* reply_udp */
1918
1919 unsigned short calc_checksum (char *data, int len)
1920 {
1921         unsigned short temp; 
1922         unsigned long sum=0;
1923         int i;
1924
1925         for( i = 0; i <len; i+=2 ) {
1926                 memcpy(&temp,&data[i],2);
1927                 sum += (unsigned long)temp;
1928         }
1929
1930         while (sum >> 16 ) {
1931                 sum = (sum & 0xffffUL) + (sum >> 16);
1932         }
1933
1934         temp = (unsigned short)sum;
1935         temp = ~temp;
1936
1937         if( temp == 0 ) 
1938                 temp = 0xffff;
1939
1940         return temp;    
1941 }
1942
1943 /*
1944    If incoming is 0 (outgoing)- if the net numbers is ours make it 0
1945    if incoming is 1 - if the net number is 0 make it ours 
1946
1947 */
1948 static void switch_net_numbers(unsigned char *sendpacket, unsigned long network_number, unsigned char incoming)
1949 {
1950         unsigned long pnetwork_number;
1951
1952         pnetwork_number = (unsigned long)((sendpacket[14] << 24) + 
1953                           (sendpacket[15] << 16) + (sendpacket[16] << 8) + 
1954                           sendpacket[17]);
1955
1956         if (!incoming) {
1957                 /* If the destination network number is ours, make it 0 */
1958                 if( pnetwork_number == network_number) {
1959                         sendpacket[14] = sendpacket[15] = sendpacket[16] = 
1960                                          sendpacket[17] = 0x00;
1961                 }
1962         } else {
1963                 /* If the incoming network is 0, make it ours */
1964                 if( pnetwork_number == 0) {
1965                         sendpacket[14] = (unsigned char)(network_number >> 24);
1966                         sendpacket[15] = (unsigned char)((network_number & 
1967                                          0x00FF0000) >> 16);
1968                         sendpacket[16] = (unsigned char)((network_number & 
1969                                          0x0000FF00) >> 8);
1970                         sendpacket[17] = (unsigned char)(network_number & 
1971                                          0x000000FF);
1972                 }
1973         }
1974
1975
1976         pnetwork_number = (unsigned long)((sendpacket[26] << 24) + 
1977                           (sendpacket[27] << 16) + (sendpacket[28] << 8) + 
1978                           sendpacket[29]);
1979
1980         if( !incoming ) {
1981                 /* If the source network is ours, make it 0 */
1982                 if( pnetwork_number == network_number) {
1983                         sendpacket[26] = sendpacket[27] = sendpacket[28] = 
1984                                          sendpacket[29] = 0x00;
1985                 }
1986         } else {
1987                 /* If the source network is 0, make it ours */
1988                 if( pnetwork_number == 0 ) {
1989                         sendpacket[26] = (unsigned char)(network_number >> 24);
1990                         sendpacket[27] = (unsigned char)((network_number & 
1991                                          0x00FF0000) >> 16);
1992                         sendpacket[28] = (unsigned char)((network_number & 
1993                                          0x0000FF00) >> 8);
1994                         sendpacket[29] = (unsigned char)(network_number & 
1995                                          0x000000FF);
1996                 }
1997         }
1998 } /* switch_net_numbers */
1999
2000 /*============================================================================
2001  * Get ethernet-style interface statistics.
2002  * Return a pointer to struct enet_statistics.
2003  */
2004 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2005 static struct net_device_stats *if_stats(netdevice_t *dev)
2006 #else
2007 static struct enet_statistics* if_stats (netdevice_t* dev)
2008 #endif
2009 {
2010         fr_channel_t* chan = dev->priv;
2011         
2012         if(chan == NULL)
2013                 return NULL;
2014
2015         return &chan->ifstats;
2016 }
2017
2018 /****** Interrupt Handlers **************************************************/
2019
2020 /*============================================================================
2021  * fr_isr:      S508 frame relay interrupt service routine.
2022  *
2023  * Description:
2024  *      Frame relay main interrupt service route. This
2025  *      function check the interrupt type and takes
2026  *      the appropriate action.
2027  */
2028 static void fr_isr (sdla_t* card)
2029 {
2030         fr508_flags_t* flags = card->flags;
2031         char *ptr = &flags->iflag;
2032         int i,err;
2033         fr_mbox_t* mbox = card->mbox;
2034
2035         /* This flag prevents nesting of interrupts.  See sdla_isr() routine
2036          * in sdlamain.c.  */
2037         card->in_isr = 1;
2038         
2039         ++card->statistics.isr_entry;
2040
2041
2042         /* All peripheral (configuraiton, re-configuration) events
2043          * take presidence over the ISR.  Thus, retrigger */
2044         if (test_bit(PERI_CRIT, (void*)&card->wandev.critical)) {
2045                 ++card->statistics.isr_already_critical;
2046                 goto fr_isr_exit;
2047         }
2048         
2049         if(card->hw.type != SDLA_S514) {
2050                 if (test_bit(SEND_CRIT, (void*)&card->wandev.critical)) {
2051                         printk(KERN_INFO "%s: Critical while in ISR: If Send Running!\n",
2052                                 card->devname);
2053                         ++card->statistics.isr_already_critical;
2054                         goto fr_isr_exit;
2055                 }
2056         }
2057
2058         switch (flags->iflag) {
2059
2060                 case FR_INTR_RXRDY:  /* receive interrupt */
2061                         ++card->statistics.isr_rx;
2062                         rx_intr(card);
2063                         break;
2064
2065
2066                 case FR_INTR_TXRDY:  /* transmit interrupt */
2067                         ++ card->statistics.isr_tx; 
2068                         tx_intr(card); 
2069                         break;
2070
2071                 case FR_INTR_READY:     
2072                         Intr_test_counter++;
2073                         ++card->statistics.isr_intr_test;
2074                         break;  
2075
2076                 case FR_INTR_DLC: /* Event interrupt occured */
2077                         mbox->cmd.command = FR_READ_STATUS;
2078                         mbox->cmd.length = 0;
2079                         err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2080                         if (err)
2081                                 fr_event(card, err, mbox);
2082                         break;
2083
2084                 case FR_INTR_TIMER:  /* Timer interrupt */
2085                         timer_intr(card);
2086                         break;
2087         
2088                 default:
2089                         ++card->statistics.isr_spurious;
2090                         spur_intr(card);
2091                         printk(KERN_INFO "%s: Interrupt Type 0x%02X!\n", 
2092                                 card->devname, flags->iflag);
2093             
2094                         printk(KERN_INFO "%s: ID Bytes = ",card->devname);
2095                         for(i = 0; i < 8; i ++)
2096                                 printk(KERN_INFO "0x%02X ", *(ptr + 0x28 + i));
2097                         printk(KERN_INFO "\n"); 
2098             
2099                         break;
2100         }
2101
2102 fr_isr_exit:
2103         
2104         card->in_isr = 0;
2105         flags->iflag = 0;
2106         return;
2107 }
2108
2109
2110
2111 /*===========================================================
2112  * rx_intr      Receive interrupt handler.
2113  *
2114  * Description
2115  *      Upon receiveing an interrupt: 
2116  *      1. Check that the firmware is in sync with 
2117  *         the driver. 
2118  *      2. Find an appropriate network interface
2119  *         based on the received dlci number.
2120  *      3. Check that the netowrk interface exists
2121  *         and that it's setup properly.
2122  *      4. Copy the data into an skb buffer.
2123  *      5. Check the packet type and take
2124  *         appropriate acton: UPD, API, ARP or Data.
2125  */
2126
2127 static void rx_intr (sdla_t* card)
2128 {
2129         fr_rx_buf_ctl_t* frbuf = card->rxmb;
2130         fr508_flags_t* flags = card->flags;
2131         fr_channel_t* chan;
2132         char *ptr = &flags->iflag;
2133         struct sk_buff* skb;
2134         netdevice_t* dev;
2135         void* buf;
2136         unsigned dlci, len, offs, len_incl_hdr;
2137         int i, udp_type;        
2138
2139
2140         /* Check that firmware buffers are in sync */
2141         if (frbuf->flag != 0x01) {
2142
2143                 printk(KERN_INFO 
2144                         "%s: corrupted Rx buffer @ 0x%X, flag = 0x%02X!\n", 
2145                         card->devname, (unsigned)frbuf, frbuf->flag);
2146       
2147                 printk(KERN_INFO "%s: ID Bytes = ",card->devname);
2148                 for(i = 0; i < 8; i ++)
2149                         printk(KERN_INFO "0x%02X ", *(ptr + 0x28 + i));
2150                 printk(KERN_INFO "\n");
2151         
2152                 ++card->statistics.rx_intr_corrupt_rx_bfr;
2153
2154                 /* Bug Fix: Mar 6 2000
2155                  * If we get a corrupted mailbox, it means that driver 
2156                  * is out of sync with the firmware. There is no recovery.
2157                  * If we don't turn off all interrupts for this card
2158                  * the machine will crash. 
2159                  */
2160                 printk(KERN_INFO "%s: Critical router failure ...!!!\n", card->devname);
2161                 printk(KERN_INFO "Please contact Sangoma Technologies !\n");
2162                 fr_set_intr_mode(card, 0, 0, 0);        
2163                 return;
2164         }
2165
2166         len  = frbuf->length;
2167         dlci = frbuf->dlci;
2168         offs = frbuf->offset;
2169
2170         /* Find the network interface for this packet */
2171         dev = find_channel(card, dlci);
2172    
2173
2174         /* Check that the network interface is active and
2175          * properly setup */
2176         if (dev == NULL) {
2177                 if( net_ratelimit()) { 
2178                         printk(KERN_INFO "%s: received data on unconfigured DLCI %d!\n",
2179                                                 card->devname, dlci);
2180                 }
2181                 ++card->statistics.rx_intr_on_orphaned_DLCI; 
2182                 ++card->wandev.stats.rx_dropped;
2183                 goto rx_done;
2184         }
2185
2186         if ((chan = dev->priv) == NULL){
2187                 if( net_ratelimit()) { 
2188                         printk(KERN_INFO "%s: received data on unconfigured DLCI %d!\n",
2189                                                 card->devname, dlci);
2190                 }
2191                 ++card->statistics.rx_intr_on_orphaned_DLCI; 
2192                 ++card->wandev.stats.rx_dropped;
2193                 goto rx_done;
2194         }
2195
2196         skb = dev_alloc_skb(len); 
2197
2198         if (!is_dev_running(dev) || (skb == NULL)){
2199
2200                 ++chan->ifstats.rx_dropped;
2201         
2202                 if(skb == NULL) {
2203                         if (net_ratelimit()) { 
2204                                 printk(KERN_INFO 
2205                                         "%s: no socket buffers available!\n", 
2206                                                 card->devname);
2207                         }
2208                         chan->drvstats_rx_intr.rx_intr_no_socket ++;
2209                 } 
2210
2211                 if (!is_dev_running(dev)){
2212                         chan->drvstats_rx_intr.
2213                                 rx_intr_dev_not_started ++;
2214                         if (skb){
2215                                 wan_dev_kfree_skb(skb, FREE_READ);
2216                         }
2217                 }
2218                 goto rx_done;
2219         }
2220
2221         /* Copy data from the board into the socket buffer */
2222         if ((offs + len) > card->u.f.rx_top + 1) {
2223                 unsigned tmp = card->u.f.rx_top - offs + 1;
2224
2225                 buf = skb_put(skb, tmp);
2226                 sdla_peek(&card->hw, offs, buf, tmp);
2227                 offs = card->u.f.rx_base;
2228                 len -= tmp;
2229         }
2230
2231         buf = skb_put(skb, len);
2232         sdla_peek(&card->hw, offs, buf, len);
2233
2234
2235         /* We got the packet from the bard. 
2236          * Check the packet type and take appropriate action */
2237
2238         udp_type = udp_pkt_type( skb, card );
2239
2240         if(udp_type != UDP_INVALID_TYPE) {
2241
2242                 /* UDP Debug packet received, store the
2243                  * packet and handle it in timer interrupt */
2244
2245                 skb_pull(skb, 1); 
2246                 if (wanrouter_type_trans(skb, dev)){ 
2247                         if(store_udp_mgmt_pkt(udp_type,UDP_PKT_FRM_NETWORK,card,skb,dlci)){
2248
2249                                 flags->imask |= FR_INTR_TIMER;
2250
2251                                 if (udp_type == UDP_FPIPE_TYPE){
2252                                         ++chan->drvstats_rx_intr.rx_intr_PIPE_request;
2253                                 }
2254                         }
2255                 }
2256
2257 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2258         }else if (chan->common.usedby == API) {
2259
2260                 /* We are in API mode. 
2261                  * Add an API header to the RAW packet
2262                  * and queue it into a circular buffer.
2263                  * Then kick the fr_bh() bottom half handler */
2264
2265                 api_rx_hdr_t* api_rx_hdr;
2266                 chan->drvstats_rx_intr.rx_intr_bfr_passed_to_stack ++;
2267                 chan->ifstats.rx_packets ++;
2268                 card->wandev.stats.rx_packets ++;
2269
2270                 chan->ifstats.rx_bytes += skb->len;
2271                 card->wandev.stats.rx_bytes += skb->len;
2272
2273                 skb_push(skb, sizeof(api_rx_hdr_t));
2274                 api_rx_hdr = (api_rx_hdr_t*)&skb->data[0x00];
2275                 api_rx_hdr->attr = frbuf->attr;
2276                 api_rx_hdr->time_stamp = frbuf->tmstamp;
2277
2278                 skb->protocol = htons(ETH_P_IP);
2279                 skb->mac.raw  = skb->data;
2280                 skb->dev      = dev;
2281                 skb->pkt_type = WAN_PACKET_DATA;
2282
2283                 bh_enqueue(dev, skb);
2284
2285                 trigger_fr_bh(chan);
2286         #endif
2287
2288         }else if (handle_IPXWAN(skb->data,chan->name,chan->enable_IPX, chan->network_number)){
2289
2290                 //FIXME: Frame Relay IPX is not supported, Yet !
2291                 //if (chan->enable_IPX) {
2292                 //      fr_send(card, dlci, 0, skb->len,skb->data);
2293                 //}
2294                 wan_dev_kfree_skb(skb, FREE_READ);
2295
2296         } else if (is_arp(skb->data)) {
2297
2298                 /* ARP support enabled Mar 16 2000 
2299                  * Process incoming ARP reply/request, setup
2300                  * dynamic routes. */ 
2301
2302                 if (process_ARP((arphdr_1490_t *)skb->data, card, dev)) {
2303                         if (net_ratelimit()){  
2304                                 printk (KERN_INFO 
2305                                    "%s: Error processing ARP Packet.\n", 
2306                                         card->devname);
2307                         }
2308                 }
2309                 wan_dev_kfree_skb(skb, FREE_READ);
2310
2311         } else if (skb->data[0] != 0x03) {
2312
2313                 if (net_ratelimit()) { 
2314                         printk(KERN_INFO "%s: Non IETF packet discarded.\n", 
2315                                 card->devname);
2316                 }
2317                 wan_dev_kfree_skb(skb, FREE_READ);
2318
2319         } else {
2320
2321                 len_incl_hdr = skb->len;
2322                 /* Decapsulate packet and pass it up the
2323                    protocol stack */
2324                 skb->dev = dev;
2325                 
2326                 if (chan->common.usedby == BRIDGE || chan->common.usedby == BRIDGE_NODE){
2327                 
2328                         /* Make sure it's an Ethernet frame, otherwise drop it */
2329                         if (!memcmp(skb->data, "\x03\x00\x80\x00\x80\xC2\x00\x07", 8)) {
2330                                 skb_pull(skb, 8);
2331                                 skb->protocol=eth_type_trans(skb,dev);
2332                         }else{
2333                                 ++chan->drvstats_rx_intr.rx_intr_bfr_not_passed_to_stack;
2334                                 ++chan->ifstats.rx_errors;
2335                                 ++card->wandev.stats.rx_errors;
2336                                 goto rx_done;
2337                         }
2338                 }else{
2339                 
2340                         /* remove hardware header */
2341                         buf = skb_pull(skb, 1); 
2342                         
2343                         if (!wanrouter_type_trans(skb, dev)) {
2344                                 
2345                                 /* can't decapsulate packet */
2346                                 wan_dev_kfree_skb(skb, FREE_READ);
2347
2348                                 ++chan->drvstats_rx_intr.rx_intr_bfr_not_passed_to_stack;
2349                                 ++chan->ifstats.rx_errors;
2350                                 ++card->wandev.stats.rx_errors;
2351                                 goto rx_done;   
2352                         }
2353                         skb->mac.raw = skb->data;
2354                 } 
2355                 
2356
2357                 /* Send a packed up the IP stack */
2358                 netif_rx(skb);
2359                 ++chan->drvstats_rx_intr.rx_intr_bfr_passed_to_stack;
2360                 ++chan->ifstats.rx_packets;
2361                 ++card->wandev.stats.rx_packets;
2362
2363 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2364                 chan->ifstats.rx_bytes += len_incl_hdr;
2365                 card->wandev.stats.rx_bytes += len_incl_hdr;
2366 #endif
2367         }
2368
2369 rx_done:
2370
2371         /* Release buffer element and calculate a pointer to the next one */ 
2372         frbuf->flag = 0;
2373         card->rxmb = ++frbuf;
2374         if ((void*)frbuf > card->u.f.rxmb_last)
2375                 card->rxmb = card->u.f.rxmb_base;
2376
2377 }
2378
2379 /*==================================================================
2380  * tx_intr:     Transmit interrupt handler.
2381  *
2382  * Rationale:
2383  *      If the board is busy transmitting, if_send() will
2384  *      buffers a single packet and turn on
2385  *      the tx interrupt. Tx interrupt will be called
2386  *      by the board, once the firmware can send more
2387  *      data. Thus, no polling is required.      
2388  *
2389  * Description:
2390  *      Tx interrupt is called for each 
2391  *      configured dlci channel. Thus: 
2392  *      1. Obtain the netowrk interface based on the
2393  *         dlci number.
2394  *      2. Check that network interface is up and
2395  *         properly setup.
2396  *      3. Check for a buffered packed.
2397  *      4. Transmit the packed.
2398  *      5. If we are in WANPIPE mode, mark the 
2399  *         NET_BH handler. 
2400  *      6. If we are in API mode, kick
2401  *         the AF_WANPIPE socket for more data. 
2402  *         
2403  */
2404 static void tx_intr(sdla_t *card)
2405 {
2406         fr508_flags_t* flags = card->flags;
2407         fr_tx_buf_ctl_t* bctl;
2408         netdevice_t* dev;
2409         fr_channel_t* chan;
2410
2411         if(card->hw.type == SDLA_S514){
2412                 bctl = (void*)(flags->tse_offs + card->hw.dpmbase);
2413         }else{
2414                 bctl = (void*)(flags->tse_offs - FR_MB_VECTOR +
2415                         card->hw.dpmbase);
2416         }
2417
2418         /* Find the structure and make it unbusy */
2419         dev = find_channel(card, flags->dlci);
2420         if (dev == NULL){
2421                 printk(KERN_INFO "NO DEV IN TX Interrupt\n");   
2422                 goto end_of_tx_intr;
2423         }
2424
2425         if ((chan = dev->priv) == NULL){
2426                 printk(KERN_INFO "NO CHAN IN TX Interrupt\n");  
2427                 goto end_of_tx_intr;
2428         }
2429
2430         if(!chan->transmit_length || !chan->delay_skb) {
2431                 printk(KERN_INFO "%s: tx int error - transmit length zero\n",
2432                                 card->wandev.name);
2433                 goto end_of_tx_intr;
2434         }
2435
2436         /* If the 'if_send()' procedure is currently checking the 'tbusy'
2437            status, then we cannot transmit. Instead, we configure the microcode
2438            so as to re-issue this transmit interrupt at a later stage. 
2439         */
2440         if (test_bit(SEND_TXIRQ_CRIT, (void*)&card->wandev.critical)) {
2441
2442                 fr_dlci_interface_t* dlci_interface = chan->dlci_int_interface;
2443                 bctl->flag = 0xA0;
2444                 dlci_interface->gen_interrupt |= FR_INTR_TXRDY;
2445                 return;
2446
2447         }else{
2448                 bctl->dlci = flags->dlci;
2449                 bctl->length = chan->transmit_length+chan->fr_header_len;
2450                 sdla_poke(&card->hw, 
2451                           fr_send_hdr(card,bctl->dlci,bctl->offset), 
2452                           chan->delay_skb->data,
2453                           chan->delay_skb->len);
2454                 bctl->flag = 0xC0;
2455
2456                 ++chan->ifstats.tx_packets;
2457                 ++card->wandev.stats.tx_packets;
2458 #if defined(LINUX_2_1) || defined(LINUX_2_4)
2459                 chan->ifstats.tx_bytes += chan->transmit_length;
2460                 card->wandev.stats.tx_bytes += chan->transmit_length;
2461 #endif
2462
2463                 /* We must free an sk buffer, which we used
2464                  * for delayed transmission; Otherwise, the sock
2465                  * will run out of memory */
2466                 wan_dev_kfree_skb(chan->delay_skb, FREE_WRITE);
2467
2468                 chan->delay_skb = NULL;                         
2469                 chan->transmit_length = 0;
2470
2471 #ifdef LINUX_2_4
2472                 dev->trans_start = jiffies;
2473 #endif
2474
2475 #ifdef LINUX_2_0
2476                 wake_net_dev(dev);
2477 #else
2478                 if (is_queue_stopped(dev)){
2479                         /* If using API, than wakeup socket BH handler */
2480                         if (chan->common.usedby == API){
2481                                 start_net_queue(dev);
2482                                 wakeup_sk_bh(dev);
2483                         }else{
2484                                 wake_net_dev(dev);
2485                         }
2486                 }
2487 #endif
2488         }
2489
2490 end_of_tx_intr:
2491
2492         /* if any other interfaces have transmit interrupts pending, 
2493          * do not disable the global transmit interrupt */
2494         if(!(-- card->u.f.tx_interrupts_pending))
2495                 flags->imask &= ~FR_INTR_TXRDY;
2496
2497
2498 }
2499
2500
2501 /*============================================================================
2502  * timer_intr:  Timer interrupt handler.
2503  *
2504  * Rationale:
2505  *      All commans must be executed within the timer
2506  *      interrupt since no two commands should execute
2507  *      at the same time.
2508  *
2509  * Description:
2510  *      The timer interrupt is used to:
2511  *      1. Processing udp calls from 'fpipemon'.
2512  *      2. Processing update calls from /proc file system
2513  *      3. Reading board-level statistics for 
2514  *         updating the proc file system.
2515  *      4. Sending inverse ARP request packets.
2516  *      5. Configure a dlci/channel.
2517  *      6. Unconfigure a dlci/channel. (Node only)
2518  */
2519
2520 static void timer_intr(sdla_t *card)
2521 {
2522         fr508_flags_t* flags = card->flags;
2523
2524         /* UDP Debuging: fpipemon call */
2525         if (card->u.f.timer_int_enabled & TMR_INT_ENABLED_UDP) {
2526                 if(card->u.f.udp_type == UDP_FPIPE_TYPE) {
2527                         if(process_udp_mgmt_pkt(card)) {
2528                                 card->u.f.timer_int_enabled &=
2529                                         ~TMR_INT_ENABLED_UDP;
2530                         }
2531                 }
2532         }
2533
2534         /* /proc update call : triggered from update() */
2535         if (card->u.f.timer_int_enabled & TMR_INT_ENABLED_UPDATE) {
2536                 fr_get_err_stats(card);
2537                 fr_get_stats(card);
2538                 card->u.f.update_comms_stats = 0;
2539                 card->u.f.timer_int_enabled &= ~TMR_INT_ENABLED_UPDATE;
2540         }
2541
2542         /* Update the channel state call.  This is call is
2543          * triggered by if_send() function */
2544         if (card->u.f.timer_int_enabled & TMR_INT_ENABLED_UPDATE_STATE){
2545                 netdevice_t *dev;
2546                 if (card->wandev.state == WAN_CONNECTED){
2547                         for (dev=card->wandev.dev; dev; dev = *((netdevice_t **)dev->priv)){
2548                                 fr_channel_t *chan = dev->priv; 
2549                                 if (chan->common.state != WAN_CONNECTED){
2550                                         update_chan_state(dev);
2551                                 }
2552                         }
2553                 }
2554                 card->u.f.timer_int_enabled &= ~TMR_INT_ENABLED_UPDATE_STATE;
2555         }
2556
2557         /* configure a dlci/channel */
2558         if (card->u.f.timer_int_enabled & TMR_INT_ENABLED_CONFIG){
2559                 config_fr(card);
2560                 card->u.f.timer_int_enabled &= ~TMR_INT_ENABLED_CONFIG;
2561         }
2562
2563         /* unconfigure a dlci/channel */
2564         if (card->u.f.timer_int_enabled & TMR_INT_ENABLED_UNCONFIG){
2565                 unconfig_fr(card);
2566                 card->u.f.timer_int_enabled &= ~TMR_INT_ENABLED_UNCONFIG;
2567         }
2568
2569         
2570         /* Transmit ARP packets */
2571         if (card->u.f.timer_int_enabled & TMR_INT_ENABLED_ARP){
2572                 int i=0;
2573                 netdevice_t *dev;
2574
2575                 if (card->u.f.arp_dev == NULL)
2576                         card->u.f.arp_dev = card->wandev.dev;
2577
2578                 dev = card->u.f.arp_dev;
2579
2580                 for (;;){ 
2581
2582                         fr_channel_t *chan = dev->priv;
2583
2584                         /* If the interface is brought down cancel sending In-ARPs */
2585                         if (!(dev->flags&IFF_UP)){
2586                                 clear_bit(0,&chan->inarp_ready);        
2587                         }
2588
2589                         if (test_bit(0,&chan->inarp_ready)){
2590
2591                                 if (check_tx_status(card,dev)){
2592                                         set_bit(ARP_CRIT,&card->wandev.critical);
2593                                         break;
2594                                 }
2595
2596                                 if (!send_inarp_request(card,dev)){
2597                                         trigger_fr_arp(dev);
2598                                         chan->inarp_tick = jiffies;
2599                                 }
2600
2601                                 clear_bit(0,&chan->inarp_ready);
2602                                 dev = move_dev_to_next(card,dev);
2603                                 break;
2604                         }
2605                         dev = move_dev_to_next(card,dev);
2606
2607                         if (++i == card->wandev.new_if_cnt){
2608                                 card->u.f.timer_int_enabled &= ~TMR_INT_ENABLED_ARP;
2609                                 break;
2610                         }
2611                 }
2612                 card->u.f.arp_dev = dev;
2613         }
2614
2615         if(!card->u.f.timer_int_enabled)
2616                 flags->imask &= ~FR_INTR_TIMER;
2617 }
2618
2619
2620 /*============================================================================
2621  * spur_intr:   Spurious interrupt handler.
2622  * 
2623  * Description:
2624  *      We don't know this interrupt.
2625  *      Print a warning.
2626  */
2627
2628 static void spur_intr (sdla_t* card)
2629 {
2630         if (net_ratelimit()){ 
2631                 printk(KERN_INFO "%s: spurious interrupt!\n", card->devname);
2632         }
2633 }
2634
2635
2636 //FIXME: Fix the IPX in next version
2637 /*===========================================================================
2638  *  Return 0 for non-IPXWAN packet
2639  *         1 for IPXWAN packet or IPX is not enabled!
2640  *  FIXME: Use a IPX structure here not offsets
2641  */
2642 static int handle_IPXWAN(unsigned char *sendpacket, 
2643                          char *devname, unsigned char enable_IPX, 
2644                          unsigned long network_number)
2645 {
2646         int i;
2647
2648         if( sendpacket[1] == 0x00 && sendpacket[2] == 0x80 &&
2649             sendpacket[6] == 0x81 && sendpacket[7] == 0x37) { 
2650
2651                 /* It's an IPX packet */
2652                 if (!enable_IPX){
2653                         /* Return 1 so we don't pass it up the stack. */
2654                         //FIXME: Take this out when IPX is fixed
2655                         if (net_ratelimit()){ 
2656                                 printk (KERN_INFO 
2657                                 "%s: WARNING: Unsupported IPX packet received and dropped\n",
2658                                         devname);
2659                         }
2660                         return 1;
2661                 }
2662         } else {
2663                 /* It's not IPX so return and pass it up the stack. */
2664                 return 0;
2665         }
2666
2667         if( sendpacket[24] == 0x90 && sendpacket[25] == 0x04){
2668                 /* It's IPXWAN */
2669
2670                 if( sendpacket[10] == 0x02 && sendpacket[42] == 0x00){
2671
2672                         /* It's a timer request packet */
2673                         printk(KERN_INFO "%s: Received IPXWAN Timer Request packet\n",
2674                                         devname);