Merge master.kernel.org:/pub/scm/linux/kernel/git/jejb/scsi-for-linus-2.6
[linux-2.6] / 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 occurring 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/module.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/workqueue.h>
150 #include <linux/if_arp.h>       /* ARPHRD_* defines */
151 #include <asm/byteorder.h>      /* htons(), etc. */
152 #include <asm/io.h>             /* for inb(), outb(), etc. */
153 #include <linux/time.h>         /* for do_gettimeofday */       
154 #include <linux/in.h>           /* sockaddr_in */
155 #include <linux/jiffies.h>      /* time_after() macro */
156 #include <asm/errno.h>
157
158 #include <linux/ip.h>
159 #include <linux/if.h>
160
161 #include <linux/if_wanpipe_common.h>    /* Wanpipe Socket */
162 #include <linux/if_wanpipe.h>   
163
164 #include <linux/sdla_fr.h>              /* frame relay firmware API definitions */
165
166 #include <asm/uaccess.h>
167 #include <linux/inetdevice.h>
168 #include <linux/netdevice.h>
169
170 #include <net/route.h>                  /* Dynamic Route Creation */
171 #include <linux/etherdevice.h>          /* eth_type_trans() used for bridging */
172 #include <linux/random.h>
173
174 /****** Defines & Macros ****************************************************/
175
176 #define MAX_CMD_RETRY   10              /* max number of firmware retries */
177
178 #define FR_HEADER_LEN   8               /* max encapsulation header size */
179 #define FR_CHANNEL_MTU  1500            /* unfragmented logical channel MTU */
180
181 /* Q.922 frame types */
182 #define Q922_UI         0x03            /* Unnumbered Info frame */
183 #define Q922_XID        0xAF            
184
185 /* DLCI configured or not */
186 #define DLCI_NOT_CONFIGURED     0x00
187 #define DLCI_CONFIG_PENDING     0x01
188 #define DLCI_CONFIGURED         0x02
189
190 /* CIR enabled or not */
191 #define CIR_ENABLED     0x00
192 #define CIR_DISABLED    0x01
193
194 #define FRAME_RELAY_API 1
195 #define MAX_BH_BUFF     10
196
197 /* For handle_IPXWAN() */
198 #define CVHexToAscii(b) (((unsigned char)(b) > (unsigned char)9) ? ((unsigned char)'A' + ((unsigned char)(b) - (unsigned char)10)) : ((unsigned char)'0' + (unsigned char)(b)))
199  
200 /****** Data Structures *****************************************************/
201
202 /* This is an extention of the 'struct device' we create for each network
203  * interface to keep the rest of channel-specific data.
204  */
205 typedef struct fr_channel
206 {
207         wanpipe_common_t common;
208         char name[WAN_IFNAME_SZ+1];     /* interface name, ASCIIZ */
209         unsigned dlci_configured  ;     /* check whether configured or not */
210         unsigned cir_status;            /* check whether CIR enabled or not */
211         unsigned dlci;                  /* logical channel number */
212         unsigned cir;                   /* committed information rate */
213         unsigned bc;                    /* committed burst size */
214         unsigned be;                    /* excess burst size */
215         unsigned mc;                    /* multicast support on or off */
216         unsigned tx_int_status;         /* Transmit Interrupt Status */ 
217         unsigned short pkt_length;      /* Packet Length */
218         unsigned long router_start_time;/* Router start time in seconds */
219         unsigned long tick_counter;     /* counter for transmit time out */
220         char dev_pending_devtint;       /* interface pending dev_tint() */
221         void *dlci_int_interface;       /* pointer to the DLCI Interface */ 
222         unsigned long IB_addr;          /* physical address of Interface Byte */
223         unsigned long state_tick;       /* time of the last state change */
224         unsigned char enable_IPX;       /* Enable/Disable the use of IPX */
225         unsigned long network_number;   /* Internal Network Number for IPX*/
226         sdla_t *card;                   /* -> owner */
227         unsigned route_flag;            /* Add/Rem dest addr in route tables */
228         unsigned inarp;                 /* Inverse Arp Request status */ 
229         long inarp_ready;               /* Ready to send requests */
230         int inarp_interval;             /* Time between InArp Requests */
231         unsigned long inarp_tick;       /* InArp jiffies tick counter */
232         long interface_down;            /* Bring interface down on disconnect */
233         struct net_device_stats ifstats;        /* interface statistics */
234         if_send_stat_t drvstats_if_send;
235         rx_intr_stat_t drvstats_rx_intr;
236         pipe_mgmt_stat_t drvstats_gen;
237         unsigned long router_up_time;
238
239         unsigned short transmit_length;
240         struct sk_buff *delay_skb;
241
242         bh_data_t *bh_head;               /* Circular buffer for chdlc_bh */
243         unsigned long  tq_working;
244         volatile int  bh_write;
245         volatile int  bh_read;
246         atomic_t  bh_buff_used;
247
248         /* Polling task queue. Each interface
249          * has its own task queue, which is used
250          * to defer events from the interrupt */
251         struct work_struct fr_poll_work;
252         struct timer_list fr_arp_timer;
253
254         u32 ip_local;
255         u32 ip_remote;
256         long config_dlci;
257         long unconfig_dlci;
258
259         /* Whether this interface should be setup as a gateway.
260          * Used by dynamic route setup code */
261         u8  gateway;
262
263         /* True interface type */
264         u8 true_if_encoding;
265         u8 fr_header[FR_HEADER_LEN];
266         char fr_header_len;
267
268 } fr_channel_t;
269
270 /* Route Flag options */
271 #define NO_ROUTE        0x00
272 #define ADD_ROUTE       0x01
273 #define ROUTE_ADDED     0x02
274 #define REMOVE_ROUTE    0x03
275 #define ARP_REQ         0x04
276
277 /* inarp options */
278 #define INARP_NONE              0x00
279 #define INARP_REQUEST           0x01
280 #define INARP_CONFIGURED        0x02
281
282 /* reasons for enabling the timer interrupt on the adapter */
283 #define TMR_INT_ENABLED_UDP     0x01
284 #define TMR_INT_ENABLED_UPDATE  0x02
285 #define TMR_INT_ENABLED_ARP     0x04
286 #define TMR_INT_ENABLED_UPDATE_STATE    0x08
287 #define TMR_INT_ENABLED_CONFIG  0x10
288 #define TMR_INT_ENABLED_UNCONFIG        0x20
289
290
291 typedef struct dlci_status
292 {
293         unsigned short dlci     PACKED;
294         unsigned char state     PACKED;
295 } dlci_status_t;
296
297 typedef struct dlci_IB_mapping
298 {
299         unsigned short dlci             PACKED;
300         unsigned long  addr_value       PACKED;
301 } dlci_IB_mapping_t;
302
303 /* This structure is used for DLCI list Tx interrupt mode.  It is used to
304    enable interrupt bit and set the packet length for transmission
305  */
306 typedef struct fr_dlci_interface 
307 {
308         unsigned char gen_interrupt     PACKED;
309         unsigned short packet_length    PACKED;
310         unsigned char reserved          PACKED;
311 } fr_dlci_interface_t; 
312
313 /* variable for keeping track of enabling/disabling FT1 monitor status */
314 static int rCount = 0;
315
316 extern void disable_irq(unsigned int);
317 extern void enable_irq(unsigned int);
318
319 /* variable for keeping track of number of interrupts generated during 
320  * interrupt test routine 
321  */
322 static int Intr_test_counter;
323
324 /****** Function Prototypes *************************************************/
325
326 /* WAN link driver entry points. These are called by the WAN router module. */
327 static int update(struct wan_device *wandev);
328 static int new_if(struct wan_device *wandev, struct net_device *dev,
329                   wanif_conf_t *conf);
330 static int del_if(struct wan_device *wandev, struct net_device *dev);
331 static void disable_comm (sdla_t *card);
332
333 /* WANPIPE-specific entry points */
334 static int wpf_exec(struct sdla *card, void *u_cmd, void *u_data);
335
336 /* Network device interface */
337 static int if_init(struct net_device *dev);
338 static int if_open(struct net_device *dev);
339 static int if_close(struct net_device *dev);
340
341 static void if_tx_timeout(struct net_device *dev);
342
343 static int if_rebuild_hdr (struct sk_buff *skb);
344
345 static int if_send(struct sk_buff *skb, struct net_device *dev);
346 static int chk_bcast_mcast_addr(sdla_t *card, struct net_device* dev,
347                                 struct sk_buff *skb);
348 static struct net_device_stats *if_stats(struct net_device *dev);
349
350 /* Interrupt handlers */
351 static void fr_isr(sdla_t *card);
352 static void rx_intr(sdla_t *card);
353 static void tx_intr(sdla_t *card);
354 static void timer_intr(sdla_t *card);
355 static void spur_intr(sdla_t *card);
356
357 /* Frame relay firmware interface functions */
358 static int fr_read_version(sdla_t *card, char *str);
359 static int fr_configure(sdla_t *card, fr_conf_t *conf);
360 static int fr_dlci_configure(sdla_t *card, fr_dlc_conf_t *conf, unsigned dlci);
361 static int fr_init_dlci (sdla_t *card, fr_channel_t *chan);
362 static int fr_set_intr_mode (sdla_t *card, unsigned mode, unsigned mtu, unsigned short timeout);
363 static int fr_comm_enable(sdla_t *card);
364 static void fr_comm_disable(sdla_t *card);
365 static int fr_get_err_stats(sdla_t *card);
366 static int fr_get_stats(sdla_t *card);
367 static int fr_add_dlci(sdla_t *card, int dlci);
368 static int fr_activate_dlci(sdla_t *card, int dlci);
369 static int fr_delete_dlci (sdla_t* card, int dlci);
370 static int fr_issue_isf(sdla_t *card, int isf);
371 static int fr_send(sdla_t *card, int dlci, unsigned char attr, int len,
372         void *buf);
373 static int fr_send_data_header(sdla_t *card, int dlci, unsigned char attr, int len,
374         void *buf,unsigned char hdr_len);
375 static unsigned int fr_send_hdr(sdla_t *card, int dlci, unsigned int offset);
376
377 static int check_dlci_config (sdla_t *card, fr_channel_t *chan);
378 static void initialize_rx_tx_buffers (sdla_t *card);
379
380
381 /* Firmware asynchronous event handlers */
382 static int fr_event(sdla_t *card, int event, fr_mbox_t *mbox);
383 static int fr_modem_failure(sdla_t *card, fr_mbox_t *mbox);
384 static int fr_dlci_change(sdla_t *card, fr_mbox_t *mbox);
385
386 /* Miscellaneous functions */
387 static int update_chan_state(struct net_device *dev);
388 static void set_chan_state(struct net_device *dev, int state);
389 static struct net_device *find_channel(sdla_t *card, unsigned dlci);
390 static int is_tx_ready(sdla_t *card, fr_channel_t *chan);
391 static unsigned int dec_to_uint(unsigned char *str, int len);
392 static int reply_udp( unsigned char *data, unsigned int mbox_len );
393
394 static int intr_test( sdla_t* card );
395 static void init_chan_statistics( fr_channel_t* chan );
396 static void init_global_statistics( sdla_t* card );
397 static void read_DLCI_IB_mapping( sdla_t* card, fr_channel_t* chan );
398 static int setup_for_delayed_transmit(struct net_device* dev,
399                                       struct sk_buff *skb);
400
401 struct net_device *move_dev_to_next(sdla_t *card, struct net_device *dev);
402 static int check_tx_status(sdla_t *card, struct net_device *dev);
403
404 /* Frame Relay Socket API */
405 static void trigger_fr_bh (fr_channel_t *);
406 static void fr_bh(struct net_device *dev);
407 static int fr_bh_cleanup(struct net_device *dev);
408 static int bh_enqueue(struct net_device *dev, struct sk_buff *skb);
409
410 static void trigger_fr_poll(struct net_device *dev);
411 static void fr_poll(struct net_device *dev);
412 //static void add_gateway(struct net_device *dev);
413
414 static void trigger_unconfig_fr(struct net_device *dev);
415 static void unconfig_fr (sdla_t *);
416
417 static void trigger_config_fr (sdla_t *);
418 static void config_fr (sdla_t *);
419
420
421 /* Inverse ARP and Dynamic routing functions */
422 int process_ARP(arphdr_1490_t *ArpPacket, sdla_t *card, struct net_device *dev);
423 int is_arp(void *buf);
424 int send_inarp_request(sdla_t *card, struct net_device *dev);
425
426 static void trigger_fr_arp(struct net_device *dev);
427 static void fr_arp (unsigned long data);
428
429
430 /* Udp management functions */
431 static int process_udp_mgmt_pkt(sdla_t *card);
432 static int udp_pkt_type( struct sk_buff *skb, sdla_t *card );
433 static int store_udp_mgmt_pkt(int udp_type, char udp_pkt_src, sdla_t* card,
434                                 struct sk_buff *skb, int dlci);
435
436 /* IPX functions */
437 static void switch_net_numbers(unsigned char *sendpacket,
438         unsigned long network_number, unsigned char incoming);
439
440 static int handle_IPXWAN(unsigned char *sendpacket, char *devname,
441         unsigned char enable_IPX, unsigned long network_number);
442
443 /* Lock Functions: SMP supported */
444 void    s508_s514_unlock(sdla_t *card, unsigned long *smp_flags);
445 void    s508_s514_lock(sdla_t *card, unsigned long *smp_flags);
446
447 unsigned short calc_checksum (char *, int);
448 static int setup_fr_header(struct sk_buff** skb,
449                            struct net_device* dev, char op_mode);
450
451
452 /****** Public Functions ****************************************************/
453
454 /*============================================================================
455  * Frame relay protocol initialization routine.
456  *
457  * This routine is called by the main WANPIPE module during setup.  At this
458  * point adapter is completely initialized and firmware is running.
459  *  o read firmware version (to make sure it's alive)
460  *  o configure adapter
461  *  o initialize protocol-specific fields of the adapter data space.
462  *
463  * Return:      0       o.k.
464  *              < 0     failure.
465  */
466 int wpf_init(sdla_t *card, wandev_conf_t *conf)
467 {
468
469         int err;
470         fr508_flags_t* flags;
471
472         union
473         {
474                 char str[80];
475                 fr_conf_t cfg;
476         } u;
477
478         fr_buf_info_t* buf_info;
479         int i;
480
481
482         printk(KERN_INFO "\n");
483
484         /* Verify configuration ID */
485         if (conf->config_id != WANCONFIG_FR) {
486                 
487                 printk(KERN_INFO "%s: invalid configuration ID %u!\n",
488                         card->devname, conf->config_id);
489                 return -EINVAL;
490         
491         }
492
493         /* Initialize protocol-specific fields of adapter data space */
494         switch (card->hw.fwid) {
495         
496                 case SFID_FR508:
497                         card->mbox  = (void*)(card->hw.dpmbase + 
498                                         FR508_MBOX_OFFS);
499                         card->flags = (void*)(card->hw.dpmbase + 
500                                         FR508_FLAG_OFFS);
501                         if(card->hw.type == SDLA_S514) {
502                                 card->mbox += FR_MB_VECTOR;
503                                 card->flags += FR_MB_VECTOR;
504                         }
505                         card->isr = &fr_isr;
506                         break;
507
508                 default:
509                         return -EINVAL;
510         }
511
512         flags = card->flags;
513
514         /* Read firmware version.  Note that when adapter initializes, it
515          * clears the mailbox, so it may appear that the first command was
516          * executed successfully when in fact it was merely erased. To work
517          * around this, we execute the first command twice.
518          */
519
520         if (fr_read_version(card, NULL) || fr_read_version(card, u.str))
521                 return -EIO;
522
523         printk(KERN_INFO "%s: running frame relay firmware v%s\n",
524                 card->devname, u.str);
525
526         /* Adjust configuration */
527         conf->mtu += FR_HEADER_LEN;
528         conf->mtu = (conf->mtu >= MIN_LGTH_FR_DATA_CFG) ?
529                         min_t(unsigned int, conf->mtu, FR_MAX_NO_DATA_BYTES_IN_FRAME) :
530                         FR_CHANNEL_MTU + FR_HEADER_LEN;
531      
532         conf->bps = min_t(unsigned int, conf->bps, 2048000);
533
534         /* Initialze the configuration structure sent to the board to zero */
535         memset(&u.cfg, 0, sizeof(u.cfg));
536
537         memset(card->u.f.dlci_to_dev_map, 0, sizeof(card->u.f.dlci_to_dev_map));
538         
539         /* Configure adapter firmware */
540
541         u.cfg.mtu       = conf->mtu;
542         u.cfg.kbps      = conf->bps / 1000;
543
544         u.cfg.cir_fwd = u.cfg.cir_bwd = 16;
545         u.cfg.bc_fwd  = u.cfg.bc_bwd = 16;
546         
547         u.cfg.options   = 0x0000;
548         printk(KERN_INFO "%s: Global CIR enabled by Default\n", card->devname);
549         
550         switch (conf->u.fr.signalling) {
551
552                 case WANOPT_FR_ANSI:
553                         u.cfg.options = 0x0000; 
554                         break;          
555         
556                 case WANOPT_FR_Q933:    
557                         u.cfg.options |= 0x0200; 
558                         break;
559         
560                 case WANOPT_FR_LMI:     
561                         u.cfg.options |= 0x0400; 
562                         break;
563
564                 case WANOPT_NO:
565                         u.cfg.options |= 0x0800; 
566                         break;
567                 default:
568                         printk(KERN_INFO "%s: Illegal Signalling option\n",
569                                         card->wandev.name);
570                         return -EINVAL;
571         }
572
573
574         card->wandev.signalling = conf->u.fr.signalling;
575
576         if (conf->station == WANOPT_CPE) {
577
578
579                 if (conf->u.fr.signalling == WANOPT_NO){
580                         printk(KERN_INFO 
581                                 "%s: ERROR - For NO signalling, station must be set to Node!",
582                                          card->devname);
583                         return -EINVAL;
584                 }
585
586                 u.cfg.station = 0;
587                 u.cfg.options |= 0x8000;        /* auto config DLCI */
588                 card->u.f.dlci_num  = 0;
589         
590         } else {
591
592                 u.cfg.station = 1;      /* switch emulation mode */
593
594                 /* For switch emulation we have to create a list of dlci(s)
595                  * that will be sent to be global SET_DLCI_CONFIGURATION 
596                  * command in fr_configure() routine. 
597                  */
598
599                 card->u.f.dlci_num  = min_t(unsigned int, max_t(unsigned int, conf->u.fr.dlci_num, 1), 100);
600         
601                 for ( i = 0; i < card->u.f.dlci_num; i++) {
602
603                         card->u.f.node_dlci[i] = (unsigned short) 
604                                 conf->u.fr.dlci[i] ? conf->u.fr.dlci[i] : 16;
605         
606                 }
607         }
608
609         if (conf->clocking == WANOPT_INTERNAL)
610                 u.cfg.port |= 0x0001;
611
612         if (conf->interface == WANOPT_RS232)
613                 u.cfg.port |= 0x0002;
614
615         if (conf->u.fr.t391)
616                 u.cfg.t391 = min_t(unsigned int, conf->u.fr.t391, 30);
617         else
618                 u.cfg.t391 = 5;
619
620         if (conf->u.fr.t392)
621                 u.cfg.t392 = min_t(unsigned int, conf->u.fr.t392, 30);
622         else
623                 u.cfg.t392 = 15;
624
625         if (conf->u.fr.n391)
626                 u.cfg.n391 = min_t(unsigned int, conf->u.fr.n391, 255);
627         else
628                 u.cfg.n391 = 2;
629
630         if (conf->u.fr.n392)
631                 u.cfg.n392 = min_t(unsigned int, conf->u.fr.n392, 10);
632         else
633                 u.cfg.n392 = 3; 
634
635         if (conf->u.fr.n393)
636                 u.cfg.n393 = min_t(unsigned int, conf->u.fr.n393, 10);
637         else
638                 u.cfg.n393 = 4;
639
640         if (fr_configure(card, &u.cfg))
641                 return -EIO;
642
643         if (card->hw.type == SDLA_S514) {
644         
645                 buf_info = (void*)(card->hw.dpmbase + FR_MB_VECTOR +
646                         FR508_RXBC_OFFS);
647
648                 card->rxmb = (void*)(buf_info->rse_next + card->hw.dpmbase);
649
650                 card->u.f.rxmb_base =
651                         (void*)(buf_info->rse_base + card->hw.dpmbase); 
652
653                 card->u.f.rxmb_last =
654                         (void*)(buf_info->rse_base +
655                         (buf_info->rse_num - 1) * sizeof(fr_rx_buf_ctl_t) +
656                         card->hw.dpmbase);
657         }else{  
658                 buf_info = (void*)(card->hw.dpmbase + FR508_RXBC_OFFS);
659
660                 card->rxmb = (void*)(buf_info->rse_next -
661                         FR_MB_VECTOR + card->hw.dpmbase);
662                 
663                 card->u.f.rxmb_base =
664                         (void*)(buf_info->rse_base -
665                         FR_MB_VECTOR + card->hw.dpmbase);
666                 
667                 card->u.f.rxmb_last =
668                         (void*)(buf_info->rse_base +
669                         (buf_info->rse_num - 1) * sizeof(fr_rx_buf_ctl_t) -
670                         FR_MB_VECTOR + card->hw.dpmbase);
671         }
672
673         card->u.f.rx_base = buf_info->buf_base;
674         card->u.f.rx_top  = buf_info->buf_top;
675
676         card->u.f.tx_interrupts_pending = 0;
677
678         card->wandev.mtu        = conf->mtu;
679         card->wandev.bps        = conf->bps;
680         card->wandev.interface  = conf->interface;
681         card->wandev.clocking   = conf->clocking;
682         card->wandev.station    = conf->station;
683         card->poll              = NULL; 
684         card->exec              = &wpf_exec;
685         card->wandev.update     = &update;
686         card->wandev.new_if     = &new_if;
687         card->wandev.del_if     = &del_if;
688         card->wandev.state      = WAN_DISCONNECTED;
689         card->wandev.ttl        = conf->ttl;
690         card->wandev.udp_port   = conf->udp_port;       
691         card->disable_comm      = &disable_comm;        
692         card->u.f.arp_dev       = NULL;
693
694         /* Intialize global statistics for a card */
695         init_global_statistics( card );
696
697         card->TracingEnabled          = 0;
698
699         /* Interrupt Test */
700         Intr_test_counter = 0;
701         card->intr_mode = INTR_TEST_MODE;
702         err = intr_test( card );
703
704         printk(KERN_INFO "%s: End of Interrupt Test rc=0x%x  count=%i\n",
705                         card->devname,err,Intr_test_counter); 
706         
707         if (err || (Intr_test_counter < MAX_INTR_TEST_COUNTER)) {
708                 printk(KERN_ERR "%s: Interrupt Test Failed, Counter: %i\n", 
709                         card->devname, Intr_test_counter);
710                 printk(KERN_ERR "Please choose another interrupt\n");
711                 err = -EIO;
712                 return err;
713         }
714
715         printk(KERN_INFO "%s: Interrupt Test Passed, Counter: %i\n",
716                         card->devname, Intr_test_counter);
717
718
719         /* Apr 28 2000. Nenad Corbic
720          * Enable commnunications here, not in if_open or new_if, since
721          * interfaces come down when the link is disconnected. 
722          */
723          
724         /* If you enable comms and then set ints, you get a Tx int as you
725          * perform the SET_INT_TRIGGERS command. So, we only set int
726          * triggers and then adjust the interrupt mask (to disable Tx ints)
727          * before enabling comms. 
728          */     
729         if (fr_set_intr_mode(card, (FR_INTR_RXRDY | FR_INTR_TXRDY |
730                 FR_INTR_DLC | FR_INTR_TIMER | FR_INTR_TX_MULT_DLCIs) ,
731                 card->wandev.mtu, 0)) {
732                 return -EIO;
733         }
734
735         flags->imask &= ~(FR_INTR_TXRDY | FR_INTR_TIMER);
736  
737         if (fr_comm_enable(card)) {
738                 return -EIO;
739         }       
740         wanpipe_set_state(card, WAN_CONNECTED);
741         spin_lock_init(&card->u.f.if_send_lock);
742         
743         printk(KERN_INFO "\n");
744
745         return 0;
746 }
747
748 /******* WAN Device Driver Entry Points *************************************/
749
750 /*============================================================================
751  * Update device status & statistics.
752  */
753 static int update(struct wan_device* wandev)
754 {
755         volatile sdla_t* card;
756         unsigned long timeout;
757         fr508_flags_t* flags;
758
759         /* sanity checks */
760         if ((wandev == NULL) || (wandev->private == NULL))
761                 return -EFAULT;
762
763         if (wandev->state == WAN_UNCONFIGURED)
764                 return -ENODEV;
765
766         card = wandev->private;
767         flags = card->flags;
768
769
770         card->u.f.update_comms_stats = 1;
771         card->u.f.timer_int_enabled |= TMR_INT_ENABLED_UPDATE;
772         flags->imask |= FR_INTR_TIMER;
773         timeout = jiffies;
774         for(;;) {
775                 if(card->u.f.update_comms_stats == 0)
776                         break;
777                 if (time_after(jiffies, timeout + 1 * HZ)){
778                         card->u.f.update_comms_stats = 0;
779                         return -EAGAIN;
780                 }
781         }
782
783         return 0;
784 }
785
786 /*============================================================================
787  * Create new logical channel.
788  * This routine is called by the router when ROUTER_IFNEW IOCTL is being
789  * handled.
790  * o parse media- and hardware-specific configuration
791  * o make sure that a new channel can be created
792  * o allocate resources, if necessary
793  * o prepare network device structure for registaration.
794  *
795  * Return:      0       o.k.
796  *              < 0     failure (channel will not be created)
797  */
798 static int new_if(struct wan_device* wandev, struct net_device* dev,
799                   wanif_conf_t* conf)
800 {
801         sdla_t* card = wandev->private;
802         fr_channel_t* chan;
803         int dlci = 0;
804         int err = 0;
805
806         
807         if ((conf->name[0] == '\0') || (strlen(conf->name) > WAN_IFNAME_SZ)) {
808                 
809                 printk(KERN_INFO "%s: Invalid interface name!\n",
810                         card->devname);
811                 return -EINVAL;
812         }
813
814         /* allocate and initialize private data */
815         chan = kmalloc(sizeof(fr_channel_t), GFP_KERNEL);
816
817         if (chan == NULL)
818                 return -ENOMEM;
819
820         memset(chan, 0, sizeof(fr_channel_t));
821         strcpy(chan->name, conf->name);
822         chan->card = card;
823
824         /* verify media address */
825         if (is_digit(conf->addr[0])) {
826
827                 dlci = dec_to_uint(conf->addr, 0);
828
829                 if (dlci && (dlci <= HIGHEST_VALID_DLCI)) {
830                 
831                         chan->dlci = dlci;
832                 
833                 } else {
834                 
835                         printk(KERN_ERR
836                                 "%s: Invalid DLCI %u on interface %s!\n",
837                                 wandev->name, dlci, chan->name);
838                         err = -EINVAL;
839                 }
840
841         } else {
842                 printk(KERN_ERR
843                         "%s: Invalid media address on interface %s!\n",
844                         wandev->name, chan->name);
845                 err = -EINVAL;
846         }
847
848         if ((chan->true_if_encoding = conf->true_if_encoding) == WANOPT_YES){
849                 printk(KERN_INFO 
850                         "%s: Enabling, true interface type encoding.\n",
851                         card->devname);
852         }
853         
854
855
856     /* Setup wanpipe as a router (WANPIPE) even if it is
857          * a bridged DLCI, or as an API 
858          */
859         if (strcmp(conf->usedby, "WANPIPE")  == 0  || 
860             strcmp(conf->usedby, "BRIDGE")   == 0  ||
861             strcmp(conf->usedby, "BRIDGE_N") == 0){
862                 
863                 if(strcmp(conf->usedby, "WANPIPE") == 0){
864                         chan->common.usedby = WANPIPE;
865                         
866                         printk(KERN_INFO "%s: Running in WANPIPE mode.\n", 
867                                         card->devname);
868                         
869                 }else if(strcmp(conf->usedby, "BRIDGE") == 0){
870                         
871                         chan->common.usedby = BRIDGE;
872                         
873                         printk(KERN_INFO "%s: Running in WANPIPE (BRIDGE) mode.\n", 
874                                         card->devname);
875                 }else if( strcmp(conf->usedby, "BRIDGE_N") == 0 ){
876                         
877                         chan->common.usedby = BRIDGE_NODE;
878                 
879                         printk(KERN_INFO "%s: Running in WANPIPE (BRIDGE_NODE) mode.\n", 
880                                         card->devname);
881                 }
882
883                 if (!err){
884                         /* Dynamic interface configuration option.
885                          * On disconnect, if the options is selected,
886                          * the interface will be brought down */
887                         if (conf->if_down == WANOPT_YES){ 
888                                 set_bit(DYN_OPT_ON,&chan->interface_down);
889                                 printk(KERN_INFO 
890                                     "%s: Dynamic interface configuration enabled.\n",
891                                         card->devname);
892                         }
893                 }
894
895         } else if(strcmp(conf->usedby, "API") == 0){
896
897                 chan->common.usedby = API;
898                 printk(KERN_INFO "%s: Running in API mode.\n",
899                         wandev->name);
900         }
901
902         if (err) {
903                 
904                 kfree(chan);
905                 return err;
906         }
907
908         /* place cir,be,bc and other channel specific information into the
909          * chan structure 
910          */
911         if (conf->cir) {
912
913                 chan->cir = max_t(unsigned int, 1,
914                                 min_t(unsigned int, conf->cir, 512));
915                 chan->cir_status = CIR_ENABLED; 
916
917                 
918                 /* If CIR is enabled, force BC to equal CIR
919                  * this solves number of potential problems if CIR is 
920                  * set and BC is not 
921                  */
922                 chan->bc = chan->cir;
923
924                 if (conf->be){
925                         chan->be = max_t(unsigned int,
926                                        0, min_t(unsigned int, conf->be, 511));
927                 }else{  
928                         conf->be = 0;
929                 }
930
931                 printk (KERN_INFO "%s: CIR enabled for DLCI %i \n",
932                                 wandev->name,chan->dlci);
933                 printk (KERN_INFO "%s:     CIR = %i ; BC = %i ; BE = %i\n",
934                                 wandev->name,chan->cir,chan->bc,chan->be);
935
936
937         }else{
938                 chan->cir_status = CIR_DISABLED;
939                 printk (KERN_INFO "%s: CIR disabled for DLCI %i\n",
940                                 wandev->name,chan->dlci);
941         }
942
943         chan->mc = conf->mc;
944
945         if (conf->inarp == WANOPT_YES){
946                 printk(KERN_INFO "%s: Inverse ARP Support Enabled\n",card->devname);
947                 chan->inarp = conf->inarp ? INARP_REQUEST : INARP_NONE;
948                 chan->inarp_interval = conf->inarp_interval ? conf->inarp_interval : 10;
949         }else{
950                 printk(KERN_INFO "%s: Inverse ARP Support Disabled\n",card->devname);
951                 chan->inarp = INARP_NONE;
952                 chan->inarp_interval = 10;
953         }
954
955
956         chan->dlci_configured = DLCI_NOT_CONFIGURED;    
957
958
959         /*FIXME: IPX disabled in this WANPIPE version */
960         if (conf->enable_IPX == WANOPT_YES){
961                 printk(KERN_INFO "%s: ERROR - This version of WANPIPE doesn't support IPX\n",
962                                 card->devname);
963                 kfree(chan);
964                 return -EINVAL;
965         }else{
966                 chan->enable_IPX = WANOPT_NO;
967         }       
968
969         if (conf->network_number){
970                 chan->network_number = conf->network_number;
971         }else{
972                 chan->network_number = 0xDEADBEEF;
973         }
974
975         chan->route_flag = NO_ROUTE;
976         
977         init_chan_statistics(chan);
978
979         chan->transmit_length = 0;
980
981         /* prepare network device data space for registration */
982         strcpy(dev->name,chan->name);
983         
984         dev->init = &if_init;
985         dev->priv = chan;
986
987         /* Initialize FR Polling Task Queue
988          * We need a poll routine for each network
989          * interface. 
990          */
991         INIT_WORK(&chan->fr_poll_work, (void *)fr_poll, dev);
992
993         init_timer(&chan->fr_arp_timer);
994         chan->fr_arp_timer.data=(unsigned long)dev;
995         chan->fr_arp_timer.function = fr_arp;
996
997         wandev->new_if_cnt++;
998
999         /* Tells us that if this interface is a
1000          * gateway or not */
1001         if ((chan->gateway = conf->gateway) == WANOPT_YES){
1002                 printk(KERN_INFO "%s: Interface %s is set as a gateway.\n",
1003                         card->devname,dev->name);
1004         }
1005
1006         /* M. Grant Patch Apr 28 2000 
1007          * Disallow duplicate dlci configurations. */
1008         if (card->u.f.dlci_to_dev_map[chan->dlci] != NULL) {
1009                 kfree(chan);
1010                 return -EBUSY;
1011         }
1012
1013         /* Configure this dlci at a later date, when
1014          * the interface comes up. i.e. when if_open() 
1015          * executes */
1016         set_bit(0,&chan->config_dlci);
1017         
1018         printk(KERN_INFO "\n");
1019
1020         return 0;
1021 }
1022
1023 /*============================================================================
1024  * Delete logical channel.
1025  */
1026 static int del_if(struct wan_device* wandev, struct net_device* dev)
1027 {
1028         fr_channel_t* chan = dev->priv;
1029         unsigned long smp_flags=0;
1030
1031         /* This interface is dead, make sure the 
1032          * ARP timer is stopped */
1033         del_timer(&chan->fr_arp_timer);
1034         
1035         /* If we are a NODE, we must unconfigure this DLCI
1036          * Trigger an unconfigure command that will
1037          * be executed in timer interrupt. We must wait
1038          * for the command to complete. */
1039         trigger_unconfig_fr(dev);
1040
1041         lock_adapter_irq(&wandev->lock, &smp_flags);
1042         wandev->new_if_cnt--;
1043         unlock_adapter_irq(&wandev->lock, &smp_flags);
1044
1045         return 0;
1046 }
1047
1048
1049 /*=====================================================================
1050  * disable_comm
1051  *
1052  * Description:
1053  *      Disable communications.
1054  *      This code runs in shutdown (sdlamain.c)
1055  *      under critical flag. Therefore it is not
1056  *      necessary to set a critical flag here 
1057  *
1058  * Usage:
1059  *      Commnunications are disabled only on a card
1060  *      shutdown.
1061  */
1062
1063 static void disable_comm (sdla_t *card)
1064 {
1065         printk(KERN_INFO "%s: Disabling Communications!\n",
1066                         card->devname);
1067         fr_comm_disable(card);
1068 }
1069
1070 /****** WANPIPE-specific entry points ***************************************/
1071
1072 /*============================================================================
1073  * Execute adapter interface command.
1074  */
1075 static int wpf_exec (struct sdla* card, void* u_cmd, void* u_data)
1076 {
1077         fr_mbox_t* mbox = card->mbox;
1078         int retry = MAX_CMD_RETRY;
1079         int err, len;
1080         fr_cmd_t cmd;
1081
1082         if(copy_from_user((void*)&cmd, u_cmd, sizeof(cmd)))
1083                 return -EFAULT;
1084         
1085         /* execute command */
1086         do
1087         {
1088                 memcpy(&mbox->cmd, &cmd, sizeof(cmd));
1089                 
1090                 if (cmd.length){
1091                         if( copy_from_user((void*)&mbox->data, u_data, cmd.length))
1092                                 return -EFAULT;
1093                 }
1094                 
1095                 if (sdla_exec(mbox))
1096                         err = mbox->cmd.result;
1097
1098                 else return -EIO;
1099         
1100         } while (err && retry-- && fr_event(card, err, mbox));
1101
1102         /* return result */
1103         if (copy_to_user(u_cmd, (void*)&mbox->cmd, sizeof(fr_cmd_t)))
1104                 return -EFAULT;
1105
1106         len = mbox->cmd.length;
1107
1108         if (len && u_data && !copy_to_user(u_data, (void*)&mbox->data, len))
1109                 return -EFAULT;
1110         return 0;
1111 }
1112
1113 /****** Network Device Interface ********************************************/
1114
1115 /*============================================================================
1116  * Initialize Linux network interface.
1117  *
1118  * This routine is called only once for each interface, during Linux network
1119  * interface registration.  Returning anything but zero will fail interface
1120  * registration.
1121  */
1122 static int if_init(struct net_device* dev)
1123 {
1124         fr_channel_t* chan = dev->priv;
1125         sdla_t* card = chan->card;
1126         struct wan_device* wandev = &card->wandev;
1127
1128         /* Initialize device driver entry points */
1129         dev->open               = &if_open;
1130         dev->stop               = &if_close;
1131         dev->hard_header        = NULL;
1132         dev->rebuild_header     = &if_rebuild_hdr;
1133         dev->hard_start_xmit    = &if_send;
1134         dev->get_stats          = &if_stats;
1135         dev->tx_timeout         = &if_tx_timeout;
1136         dev->watchdog_timeo     = TX_TIMEOUT;
1137         
1138         if (chan->common.usedby == WANPIPE || chan->common.usedby == API){
1139
1140                 /* Initialize media-specific parameters */
1141                 if (chan->true_if_encoding){
1142                         dev->type               = ARPHRD_DLCI;  /* This breaks tcpdump */
1143                 }else{
1144                         dev->type               = ARPHRD_PPP;   /* ARP h/w type */
1145                 }
1146                 
1147                 dev->flags              |= IFF_POINTOPOINT;
1148                 dev->flags              |= IFF_NOARP;
1149
1150                 /* Enable Multicast addressing */
1151                 if (chan->mc == WANOPT_YES){
1152                         dev->flags      |= IFF_MULTICAST;
1153                 }
1154
1155                 dev->mtu                = wandev->mtu - FR_HEADER_LEN;
1156                 /* For an API, the maximum number of bytes that the stack will pass
1157                    to the driver is (dev->mtu + dev->hard_header_len). So, adjust the
1158                    mtu so that a frame of maximum size can be transmitted by the API. 
1159                 */
1160                 if(chan->common.usedby == API) {
1161                         dev->mtu += (sizeof(api_tx_hdr_t) - FR_HEADER_LEN);
1162                 }
1163                 
1164                 dev->hard_header_len    = FR_HEADER_LEN;/* media header length */
1165                 dev->addr_len           = 2;            /* hardware address length */
1166                 *(unsigned short*)dev->dev_addr = htons(chan->dlci);
1167
1168                 /* Set transmit buffer queue length */
1169                 dev->tx_queue_len = 100;
1170
1171         }else{
1172
1173                 /* Setup the interface for Bridging */
1174                 int hw_addr=0;
1175                 ether_setup(dev);
1176                 
1177                 /* Use a random number to generate the MAC address */
1178                 memcpy(dev->dev_addr, "\xFE\xFC\x00\x00\x00\x00", 6);
1179                 get_random_bytes(&hw_addr, sizeof(hw_addr));
1180                 *(int *)(dev->dev_addr + 2) += hw_addr;
1181         }
1182                 
1183         /* Initialize hardware parameters (just for reference) */
1184         dev->irq        = wandev->irq;
1185         dev->dma        = wandev->dma;
1186         dev->base_addr  = wandev->ioport;
1187         dev->mem_start  = wandev->maddr;
1188         dev->mem_end    = wandev->maddr + wandev->msize - 1;
1189         SET_MODULE_OWNER(dev);
1190
1191         return 0;
1192 }
1193
1194 /*============================================================================
1195  * Open network interface.
1196  * o if this is the first open, then enable communications and interrupts.
1197  * o prevent module from unloading by incrementing use count
1198  *
1199  * Return 0 if O.k. or errno.
1200  */
1201 static int if_open(struct net_device* dev)
1202 {
1203         fr_channel_t* chan = dev->priv;
1204         sdla_t* card = chan->card;
1205         int err = 0;
1206         struct timeval tv;
1207
1208         if (netif_running(dev))
1209                 return -EBUSY;
1210         
1211         /* Initialize the task queue */
1212         chan->tq_working=0;
1213
1214         INIT_WORK(&chan->common.wanpipe_work, (void *)fr_bh, dev);
1215
1216         /* Allocate and initialize BH circular buffer */
1217         chan->bh_head = kmalloc((sizeof(bh_data_t)*MAX_BH_BUFF),GFP_ATOMIC);
1218         memset(chan->bh_head,0,(sizeof(bh_data_t)*MAX_BH_BUFF));
1219         atomic_set(&chan->bh_buff_used, 0);
1220
1221         netif_start_queue(dev);
1222
1223         wanpipe_open(card);
1224         do_gettimeofday( &tv );
1225         chan->router_start_time = tv.tv_sec;
1226         
1227         if (test_bit(0,&chan->config_dlci)){
1228                 trigger_config_fr (card);
1229         }else if (chan->inarp == INARP_REQUEST){
1230                 trigger_fr_arp(dev);
1231         }
1232         
1233         return err;
1234 }
1235
1236 /*============================================================================
1237  * Close network interface.
1238  * o if this is the last open, then disable communications and interrupts.
1239  * o reset flags.
1240  */
1241 static int if_close(struct net_device* dev)
1242 {
1243         fr_channel_t* chan = dev->priv;
1244         sdla_t* card = chan->card;
1245
1246         if (chan->inarp == INARP_CONFIGURED) {
1247                 chan->inarp = INARP_REQUEST;
1248         }
1249
1250         netif_stop_queue(dev);
1251         wanpipe_close(card);
1252
1253         return 0;
1254 }
1255
1256 /*============================================================================
1257  * Re-build media header.
1258  *
1259  * Return:      1       physical address resolved.
1260  *              0       physical address not resolved
1261  */
1262 static int if_rebuild_hdr (struct sk_buff* skb)
1263 {
1264         struct net_device *dev = skb->dev;
1265         fr_channel_t* chan = dev->priv;
1266         sdla_t* card = chan->card;
1267
1268         printk(KERN_INFO "%s: rebuild_header() called for interface %s!\n",
1269                 card->devname, dev->name);
1270         return 1;
1271 }
1272
1273 /*============================================================================
1274  * Handle transmit timeout event from netif watchdog
1275  */
1276 static void if_tx_timeout(struct net_device *dev)
1277 {
1278         fr_channel_t* chan = dev->priv;
1279         sdla_t *card = chan->card;
1280
1281         /* If our device stays busy for at least 5 seconds then we will
1282          * kick start the device by making dev->tbusy = 0.  We expect
1283          * that our device never stays busy more than 5 seconds. So this                 
1284          * is only used as a last resort.
1285          */
1286
1287         chan->drvstats_if_send.if_send_tbusy++;
1288         ++chan->ifstats.collisions;
1289
1290         printk (KERN_INFO "%s: Transmit timed out on %s\n", 
1291                         card->devname, dev->name);
1292         chan->drvstats_if_send.if_send_tbusy_timeout++;
1293         netif_wake_queue (dev);
1294
1295 }
1296
1297
1298 /*============================================================================
1299  * Send a packet on a network interface.
1300  * o set tbusy flag (marks start of the transmission) to block a timer-based
1301  *   transmit from overlapping.
1302  * o set critical flag when accessing board.
1303  * o check link state. If link is not up, then drop the packet.
1304  * o check channel status. If it's down then initiate a call.
1305  * o pass a packet to corresponding WAN device.
1306  * o free socket buffer
1307  *
1308  * Return:      0       complete (socket buffer must be freed)
1309  *              non-0   packet may be re-transmitted (tbusy must be set)
1310  *
1311  * Notes:
1312  * 1. This routine is called either by the protocol stack or by the "net
1313  *    bottom half" (with interrupts enabled).
1314  * 
1315  * 2. Using netif_start_queue() and netif_stop_queue()
1316  *    will inhibit further transmit requests from the protocol stack 
1317  *    and can be used for flow control with protocol layer.
1318  */
1319 static int if_send(struct sk_buff* skb, struct net_device* dev)
1320 {
1321         fr_channel_t* chan = dev->priv;
1322         sdla_t* card = chan->card;
1323         int err;
1324         unsigned char *sendpacket;
1325         fr508_flags_t* adptr_flags = card->flags;
1326         int udp_type;
1327         long delay_tx_queued = 0;
1328         unsigned long smp_flags=0;
1329         unsigned char attr = 0;
1330
1331         chan->drvstats_if_send.if_send_entry++;
1332
1333         netif_stop_queue(dev);
1334         
1335         if (skb == NULL) {             
1336                 /* if we get here, some higher layer thinks we've missed an
1337                  * tx-done interrupt.
1338                  */
1339                 printk(KERN_INFO "%s: interface %s got kicked!\n", 
1340                         card->devname, dev->name);
1341                 chan->drvstats_if_send.if_send_skb_null ++;
1342
1343                 netif_wake_queue(dev);
1344                 return 0;
1345         }
1346
1347         /* If a peripheral task is running just drop packets */
1348         if (test_bit(PERI_CRIT, &card->wandev.critical)){
1349                 
1350                 printk(KERN_INFO "%s: Critical in if_send(): Peripheral running!\n",
1351                                 card->devname);
1352                 
1353                 dev_kfree_skb_any(skb);
1354                 netif_start_queue(dev);
1355                 return 0;
1356         }
1357
1358         /* We must set the 'tbusy' flag if we already have a packet queued for
1359            transmission in the transmit interrupt handler. However, we must
1360            ensure that the transmit interrupt does not reset the 'tbusy' flag
1361            just before we set it, as this will result in a "transmit timeout".
1362         */
1363         set_bit(SEND_TXIRQ_CRIT, (void*)&card->wandev.critical);
1364         if(chan->transmit_length) {
1365                 netif_stop_queue(dev);
1366                 chan->tick_counter = jiffies;
1367                 clear_bit(SEND_TXIRQ_CRIT, (void*)&card->wandev.critical);
1368                 return 1;
1369         }
1370         clear_bit(SEND_TXIRQ_CRIT, (void*)&card->wandev.critical);
1371  
1372         /* Move the if_header() code to here. By inserting frame
1373          * relay header in if_header() we would break the
1374          * tcpdump and other packet sniffers */
1375         chan->fr_header_len = setup_fr_header(&skb,dev,chan->common.usedby);
1376         if (chan->fr_header_len < 0 ){
1377                 ++chan->ifstats.tx_dropped;
1378                 ++card->wandev.stats.tx_dropped;
1379                 
1380                 dev_kfree_skb_any(skb);
1381                 netif_start_queue(dev); 
1382                 return 0;
1383         }
1384
1385         sendpacket = skb->data;
1386
1387         udp_type = udp_pkt_type(skb, card);
1388
1389         if(udp_type != UDP_INVALID_TYPE) {
1390                 if(store_udp_mgmt_pkt(udp_type, UDP_PKT_FRM_STACK, card, skb,
1391                         chan->dlci)) {
1392                         adptr_flags->imask |= FR_INTR_TIMER;
1393                         if (udp_type == UDP_FPIPE_TYPE){
1394                                 chan->drvstats_if_send.
1395                                         if_send_PIPE_request ++;
1396                         }
1397                 }
1398                 netif_start_queue(dev);
1399                 return 0;
1400         }
1401
1402         //FIXME: can we do better than sendpacket[2]?
1403         if ((chan->common.usedby == WANPIPE) && (sendpacket[2] == 0x45)) {
1404                 
1405                 /* check to see if the source IP address is a broadcast or */
1406                 /* multicast IP address */
1407                 if(chk_bcast_mcast_addr(card, dev, skb)){
1408                         ++chan->ifstats.tx_dropped;
1409                         ++card->wandev.stats.tx_dropped;
1410                         dev_kfree_skb_any(skb);
1411                         netif_start_queue(dev);
1412                         return 0;
1413                 }
1414         }
1415
1416         
1417         /* Lock the S514/S508 card: SMP Supported */
1418         s508_s514_lock(card,&smp_flags);
1419
1420         if (test_and_set_bit(SEND_CRIT, (void*)&card->wandev.critical)) {
1421                 
1422                 chan->drvstats_if_send.if_send_critical_non_ISR ++;
1423                 chan->ifstats.tx_dropped ++;
1424                 printk(KERN_INFO "%s Critical in IF_SEND: if_send() already running!\n", 
1425                                 card->devname);
1426                 goto if_send_start_and_exit;
1427         }
1428         
1429         /* API packet check: minimum packet size must be greater than 
1430          * 16 byte API header */
1431         if((chan->common.usedby == API) && (skb->len <= sizeof(api_tx_hdr_t))) {
1432                 ++chan->ifstats.tx_dropped;
1433                 ++card->wandev.stats.tx_dropped;
1434             
1435                 
1436                 goto if_send_start_and_exit;
1437
1438         }else{
1439                 /* During API transmission, get rid of the API header */
1440                 if (chan->common.usedby == API) {
1441                         api_tx_hdr_t* api_tx_hdr;
1442                         api_tx_hdr = (api_tx_hdr_t*)&skb->data[0x00];
1443                         attr = api_tx_hdr->attr;
1444                         skb_pull(skb,sizeof(api_tx_hdr_t));
1445                 }
1446         }
1447
1448         if (card->wandev.state != WAN_CONNECTED) {
1449                 chan->drvstats_if_send.if_send_wan_disconnected ++;
1450                 ++chan->ifstats.tx_dropped;
1451                 ++card->wandev.stats.tx_dropped;
1452         
1453         } else if (chan->common.state != WAN_CONNECTED) {
1454                 chan->drvstats_if_send.if_send_dlci_disconnected ++;
1455
1456                 /* Update the DLCI state in timer interrupt */
1457                 card->u.f.timer_int_enabled |= TMR_INT_ENABLED_UPDATE_STATE;    
1458                 adptr_flags->imask |= FR_INTR_TIMER;
1459
1460                 ++chan->ifstats.tx_dropped;
1461                 ++card->wandev.stats.tx_dropped;
1462                 
1463         } else if (!is_tx_ready(card, chan)) {
1464                 /* No tx buffers available, store for delayed transmit */
1465                 if (!setup_for_delayed_transmit(dev, skb)){
1466                         set_bit(1,&delay_tx_queued);
1467                 }
1468                 chan->drvstats_if_send.if_send_no_bfrs++;
1469                 
1470         } else if (!skb->protocol) {
1471                 /* No protocols drop packet */
1472                 chan->drvstats_if_send.if_send_protocol_error ++;
1473                 ++card->wandev.stats.tx_errors;
1474         
1475         } else if (test_bit(ARP_CRIT,&card->wandev.critical)){
1476                 /* We are trying to send an ARP Packet, block IP data until
1477                  * ARP is sent */
1478                 ++chan->ifstats.tx_dropped;
1479                 ++card->wandev.stats.tx_dropped;
1480                 
1481         } else {
1482                 //FIXME: IPX is not implemented in this version of Frame Relay ?
1483                 if((chan->common.usedby == WANPIPE) &&
1484                         sendpacket[1] == 0x00 &&
1485                         sendpacket[2] == 0x80 &&
1486                         sendpacket[6] == 0x81 &&
1487                         sendpacket[7] == 0x37) {
1488                         
1489                         if( chan->enable_IPX ) {
1490                                 switch_net_numbers(sendpacket, 
1491                                                 chan->network_number, 0);
1492                         } else {
1493                                 //FIXME: Take this out when IPX is fixed 
1494                                 printk(KERN_INFO 
1495                                 "%s: WARNING: Unsupported IPX data in send, packet dropped\n",
1496                                         card->devname);
1497                         }
1498                         
1499                 }else{
1500                         err = fr_send_data_header(card, chan->dlci, attr, skb->len, skb->data, chan->fr_header_len);
1501                         if (err) {
1502                                 switch(err) {
1503                                 case FRRES_CIR_OVERFLOW:
1504                                 case FRRES_BUFFER_OVERFLOW:
1505                                         if (!setup_for_delayed_transmit(dev, skb)){
1506                                                 set_bit(1,&delay_tx_queued);
1507                                         }
1508                                         chan->drvstats_if_send.
1509                                                 if_send_adptr_bfrs_full ++;
1510                                         break;
1511                                         
1512                                 case FRRES_TOO_LONG:
1513                                         if (net_ratelimit()){
1514                                                 printk(KERN_INFO 
1515                                                 "%s: Error: Frame too long, transmission failed %i\n",
1516                                                  card->devname, (unsigned int)skb->len);
1517                                         }
1518                                         /* Drop down to default */
1519                                 default:
1520                                         chan->drvstats_if_send.
1521                                                 if_send_dlci_disconnected ++;
1522                                         ++chan->ifstats.tx_dropped;
1523                                         ++card->wandev.stats.tx_dropped;
1524                                         break;
1525                                 }
1526                         } else {
1527                                 chan->drvstats_if_send.
1528                                         if_send_bfr_passed_to_adptr++;
1529                                 ++chan->ifstats.tx_packets;
1530                                 ++card->wandev.stats.tx_packets;
1531                                 
1532                                 chan->ifstats.tx_bytes += skb->len;
1533                                 card->wandev.stats.tx_bytes += skb->len;
1534                                 dev->trans_start = jiffies;
1535                         }
1536                 }
1537         }
1538
1539 if_send_start_and_exit:
1540
1541         netif_start_queue(dev);
1542         
1543         /* If we queued the packet for transmission, we must not
1544          * deallocate it. The packet is unlinked from the IP stack
1545          * not copied. Therefore, we must keep the original packet */
1546         if (!test_bit(1,&delay_tx_queued)) {
1547                 dev_kfree_skb_any(skb);
1548         }else{
1549                 adptr_flags->imask |= FR_INTR_TXRDY;
1550                 card->u.f.tx_interrupts_pending ++;
1551         }
1552
1553         clear_bit(SEND_CRIT, (void*)&card->wandev.critical);
1554
1555         s508_s514_unlock(card,&smp_flags);
1556
1557         return 0;
1558 }
1559
1560
1561
1562 /*============================================================================
1563  * Setup so that a frame can be transmitted on the occurrence of a transmit
1564  * interrupt.
1565  */
1566 static int setup_for_delayed_transmit(struct net_device* dev,
1567                                       struct sk_buff *skb)
1568 {
1569         fr_channel_t* chan = dev->priv;
1570         sdla_t* card = chan->card;
1571         fr_dlci_interface_t* dlci_interface;
1572         int len = skb->len;
1573
1574         /* Check that the dlci is properly configured,
1575          * before using tx interrupt */
1576         if (!chan->dlci_int_interface){
1577                 if (net_ratelimit()){ 
1578                         printk(KERN_INFO 
1579                                 "%s: ERROR on DLCI %i: Not configured properly !\n",
1580                                         card->devname, chan->dlci);
1581                         printk(KERN_INFO "%s: Please contact Sangoma Technologies\n",
1582                                         card->devname);
1583                 }
1584                 return 1;
1585         }
1586                 
1587         dlci_interface = chan->dlci_int_interface;
1588
1589         if(chan->transmit_length) {
1590                 printk(KERN_INFO "%s: Big mess in setup_for_del...\n",
1591                                 card->devname);
1592                 return 1;
1593         }
1594
1595         if(len > FR_MAX_NO_DATA_BYTES_IN_FRAME) {
1596                 //FIXME: increment some statistic */
1597                 return 1;
1598         }
1599
1600         skb_unlink(skb);
1601         
1602         chan->transmit_length = len;
1603         chan->delay_skb = skb;
1604         
1605         dlci_interface->gen_interrupt |= FR_INTR_TXRDY;
1606         dlci_interface->packet_length = len;
1607
1608         /* Turn on TX interrupt at the end of if_send */
1609         return 0;
1610 }
1611
1612
1613 /*============================================================================
1614  * Check to see if the packet to be transmitted contains a broadcast or
1615  * multicast source IP address.
1616  * Return 0 if not broadcast/multicast address, otherwise return 1.
1617  */
1618
1619 static int chk_bcast_mcast_addr(sdla_t *card, struct net_device* dev,
1620                                 struct sk_buff *skb)
1621 {
1622         u32 src_ip_addr;
1623         u32 broadcast_ip_addr = 0;
1624         struct in_device *in_dev;
1625         fr_channel_t* chan = dev->priv;
1626  
1627         /* read the IP source address from the outgoing packet */
1628         src_ip_addr = *(u32 *)(skb->data + 14);
1629
1630         /* read the IP broadcast address for the device */
1631         in_dev = dev->ip_ptr;
1632         if(in_dev != NULL) {
1633                 struct in_ifaddr *ifa= in_dev->ifa_list;
1634                 if(ifa != NULL)
1635                         broadcast_ip_addr = ifa->ifa_broadcast;
1636                 else
1637                         return 0;
1638         }
1639
1640         /* check if the IP Source Address is a Broadcast address */
1641         if((dev->flags & IFF_BROADCAST) && (src_ip_addr == broadcast_ip_addr)) {
1642                 printk(KERN_INFO
1643                         "%s: Broadcast Source Address silently discarded\n",
1644                         card->devname);
1645                 return 1;
1646         }
1647
1648         /* check if the IP Source Address is a Multicast address */
1649         if((chan->mc == WANOPT_NO) && (ntohl(src_ip_addr) >= 0xE0000001) &&
1650                 (ntohl(src_ip_addr) <= 0xFFFFFFFE)) {
1651                 printk(KERN_INFO
1652                         "%s: Multicast Source Address silently discarded\n",
1653                         card->devname);
1654                 return 1;
1655         }
1656
1657         return 0;
1658 }
1659
1660 /*============================================================================
1661  * Reply to UDP Management system.
1662  * Return nothing.
1663  */
1664 static int reply_udp( unsigned char *data, unsigned int mbox_len ) 
1665 {
1666         unsigned short len, udp_length, temp, ip_length;
1667         unsigned long ip_temp;
1668         int even_bound = 0;
1669
1670   
1671         fr_udp_pkt_t *fr_udp_pkt = (fr_udp_pkt_t *)data; 
1672
1673         /* Set length of packet */
1674         len = //sizeof(fr_encap_hdr_t)+
1675               sizeof(ip_pkt_t)+ 
1676               sizeof(udp_pkt_t)+
1677               sizeof(wp_mgmt_t)+
1678               sizeof(cblock_t)+
1679               mbox_len;
1680  
1681
1682         /* fill in UDP reply */
1683         fr_udp_pkt->wp_mgmt.request_reply = UDPMGMT_REPLY;
1684   
1685         /* fill in UDP length */
1686         udp_length = sizeof(udp_pkt_t)+ 
1687                      sizeof(wp_mgmt_t)+
1688                      sizeof(cblock_t)+
1689                      mbox_len; 
1690
1691
1692         /* put it on an even boundary */
1693         if ( udp_length & 0x0001 ) {
1694                 udp_length += 1;
1695                 len += 1;
1696                 even_bound = 1;
1697         }
1698
1699         temp = (udp_length<<8)|(udp_length>>8);
1700         fr_udp_pkt->udp_pkt.udp_length = temp;
1701          
1702         /* swap UDP ports */
1703         temp = fr_udp_pkt->udp_pkt.udp_src_port;
1704         fr_udp_pkt->udp_pkt.udp_src_port = 
1705                         fr_udp_pkt->udp_pkt.udp_dst_port; 
1706         fr_udp_pkt->udp_pkt.udp_dst_port = temp;
1707
1708
1709
1710         /* add UDP pseudo header */
1711         temp = 0x1100;
1712         *((unsigned short *)
1713                 (fr_udp_pkt->data+mbox_len+even_bound)) = temp; 
1714         temp = (udp_length<<8)|(udp_length>>8);
1715         *((unsigned short *)
1716                 (fr_udp_pkt->data+mbox_len+even_bound+2)) = temp;
1717                  
1718         /* calculate UDP checksum */
1719         fr_udp_pkt->udp_pkt.udp_checksum = 0;
1720
1721         fr_udp_pkt->udp_pkt.udp_checksum = 
1722                 calc_checksum(&data[UDP_OFFSET/*+sizeof(fr_encap_hdr_t)*/],
1723                               udp_length+UDP_OFFSET);
1724
1725         /* fill in IP length */
1726         ip_length = udp_length + sizeof(ip_pkt_t);
1727         temp = (ip_length<<8)|(ip_length>>8);
1728         fr_udp_pkt->ip_pkt.total_length = temp;
1729   
1730         /* swap IP addresses */
1731         ip_temp = fr_udp_pkt->ip_pkt.ip_src_address;
1732         fr_udp_pkt->ip_pkt.ip_src_address = 
1733                                 fr_udp_pkt->ip_pkt.ip_dst_address;
1734         fr_udp_pkt->ip_pkt.ip_dst_address = ip_temp;
1735
1736                  
1737         /* fill in IP checksum */
1738         fr_udp_pkt->ip_pkt.hdr_checksum = 0;
1739         fr_udp_pkt->ip_pkt.hdr_checksum = 
1740                 calc_checksum(&data[/*sizeof(fr_encap_hdr_t)*/0],
1741                               sizeof(ip_pkt_t));
1742
1743         return len;
1744 } /* reply_udp */
1745
1746 unsigned short calc_checksum (char *data, int len)
1747 {
1748         unsigned short temp; 
1749         unsigned long sum=0;
1750         int i;
1751
1752         for( i = 0; i <len; i+=2 ) {
1753                 memcpy(&temp,&data[i],2);
1754                 sum += (unsigned long)temp;
1755         }
1756
1757         while (sum >> 16 ) {
1758                 sum = (sum & 0xffffUL) + (sum >> 16);
1759         }
1760
1761         temp = (unsigned short)sum;
1762         temp = ~temp;
1763
1764         if( temp == 0 ) 
1765                 temp = 0xffff;
1766
1767         return temp;    
1768 }
1769
1770 /*
1771    If incoming is 0 (outgoing)- if the net numbers is ours make it 0
1772    if incoming is 1 - if the net number is 0 make it ours 
1773
1774 */
1775 static void switch_net_numbers(unsigned char *sendpacket, unsigned long network_number, unsigned char incoming)
1776 {
1777         unsigned long pnetwork_number;
1778
1779         pnetwork_number = (unsigned long)((sendpacket[14] << 24) + 
1780                           (sendpacket[15] << 16) + (sendpacket[16] << 8) + 
1781                           sendpacket[17]);
1782
1783         if (!incoming) {
1784                 /* If the destination network number is ours, make it 0 */
1785                 if( pnetwork_number == network_number) {
1786                         sendpacket[14] = sendpacket[15] = sendpacket[16] = 
1787                                          sendpacket[17] = 0x00;
1788                 }
1789         } else {
1790                 /* If the incoming network is 0, make it ours */
1791                 if( pnetwork_number == 0) {
1792                         sendpacket[14] = (unsigned char)(network_number >> 24);
1793                         sendpacket[15] = (unsigned char)((network_number & 
1794                                          0x00FF0000) >> 16);
1795                         sendpacket[16] = (unsigned char)((network_number & 
1796                                          0x0000FF00) >> 8);
1797                         sendpacket[17] = (unsigned char)(network_number & 
1798                                          0x000000FF);
1799                 }
1800         }
1801
1802
1803         pnetwork_number = (unsigned long)((sendpacket[26] << 24) + 
1804                           (sendpacket[27] << 16) + (sendpacket[28] << 8) + 
1805                           sendpacket[29]);
1806
1807         if( !incoming ) {
1808                 /* If the source network is ours, make it 0 */
1809                 if( pnetwork_number == network_number) {
1810                         sendpacket[26] = sendpacket[27] = sendpacket[28] = 
1811                                          sendpacket[29] = 0x00;
1812                 }
1813         } else {
1814                 /* If the source network is 0, make it ours */
1815                 if( pnetwork_number == 0 ) {
1816                         sendpacket[26] = (unsigned char)(network_number >> 24);
1817                         sendpacket[27] = (unsigned char)((network_number & 
1818                                          0x00FF0000) >> 16);
1819                         sendpacket[28] = (unsigned char)((network_number & 
1820                                          0x0000FF00) >> 8);
1821                         sendpacket[29] = (unsigned char)(network_number & 
1822                                          0x000000FF);
1823                 }
1824         }
1825 } /* switch_net_numbers */
1826
1827 /*============================================================================
1828  * Get ethernet-style interface statistics.
1829  * Return a pointer to struct enet_statistics.
1830  */
1831 static struct net_device_stats *if_stats(struct net_device *dev)
1832 {
1833         fr_channel_t* chan = dev->priv;
1834         
1835         if(chan == NULL)
1836                 return NULL;
1837
1838         return &chan->ifstats;
1839 }
1840
1841 /****** Interrupt Handlers **************************************************/
1842
1843 /*============================================================================
1844  * fr_isr:      S508 frame relay interrupt service routine.
1845  *
1846  * Description:
1847  *      Frame relay main interrupt service route. This
1848  *      function check the interrupt type and takes
1849  *      the appropriate action.
1850  */
1851 static void fr_isr (sdla_t* card)
1852 {
1853         fr508_flags_t* flags = card->flags;
1854         char *ptr = &flags->iflag;
1855         int i,err;
1856         fr_mbox_t* mbox = card->mbox;
1857
1858         /* This flag prevents nesting of interrupts.  See sdla_isr() routine
1859          * in sdlamain.c.  */
1860         card->in_isr = 1;
1861         
1862         ++card->statistics.isr_entry;
1863
1864
1865         /* All peripheral (configuraiton, re-configuration) events
1866          * take presidence over the ISR.  Thus, retrigger */
1867         if (test_bit(PERI_CRIT, (void*)&card->wandev.critical)) {
1868                 ++card->statistics.isr_already_critical;
1869                 goto fr_isr_exit;
1870         }
1871         
1872         if(card->hw.type != SDLA_S514) {
1873                 if (test_bit(SEND_CRIT, (void*)&card->wandev.critical)) {
1874                         printk(KERN_INFO "%s: Critical while in ISR: If Send Running!\n",
1875                                 card->devname);
1876                         ++card->statistics.isr_already_critical;
1877                         goto fr_isr_exit;
1878                 }
1879         }
1880
1881         switch (flags->iflag) {
1882
1883                 case FR_INTR_RXRDY:  /* receive interrupt */
1884                         ++card->statistics.isr_rx;
1885                         rx_intr(card);
1886                         break;
1887
1888
1889                 case FR_INTR_TXRDY:  /* transmit interrupt */
1890                         ++ card->statistics.isr_tx; 
1891                         tx_intr(card); 
1892                         break;
1893
1894                 case FR_INTR_READY:     
1895                         Intr_test_counter++;
1896                         ++card->statistics.isr_intr_test;
1897                         break;  
1898
1899                 case FR_INTR_DLC: /* Event interrupt occurred */
1900                         mbox->cmd.command = FR_READ_STATUS;
1901                         mbox->cmd.length = 0;
1902                         err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1903                         if (err)
1904                                 fr_event(card, err, mbox);
1905                         break;
1906
1907                 case FR_INTR_TIMER:  /* Timer interrupt */
1908                         timer_intr(card);
1909                         break;
1910         
1911                 default:
1912                         ++card->statistics.isr_spurious;
1913                         spur_intr(card);
1914                         printk(KERN_INFO "%s: Interrupt Type 0x%02X!\n", 
1915                                 card->devname, flags->iflag);
1916             
1917                         printk(KERN_INFO "%s: ID Bytes = ",card->devname);
1918                         for(i = 0; i < 8; i ++)
1919                                 printk(KERN_INFO "0x%02X ", *(ptr + 0x28 + i));
1920                         printk(KERN_INFO "\n"); 
1921             
1922                         break;
1923         }
1924
1925 fr_isr_exit:
1926         
1927         card->in_isr = 0;
1928         flags->iflag = 0;
1929         return;
1930 }
1931
1932
1933
1934 /*===========================================================
1935  * rx_intr      Receive interrupt handler.
1936  *
1937  * Description
1938  *      Upon receiveing an interrupt: 
1939  *      1. Check that the firmware is in sync with 
1940  *         the driver. 
1941  *      2. Find an appropriate network interface
1942  *         based on the received dlci number.
1943  *      3. Check that the netowrk interface exists
1944  *         and that it's setup properly.
1945  *      4. Copy the data into an skb buffer.
1946  *      5. Check the packet type and take
1947  *         appropriate acton: UPD, API, ARP or Data.
1948  */
1949
1950 static void rx_intr (sdla_t* card)
1951 {
1952         fr_rx_buf_ctl_t* frbuf = card->rxmb;
1953         fr508_flags_t* flags = card->flags;
1954         fr_channel_t* chan;
1955         char *ptr = &flags->iflag;
1956         struct sk_buff* skb;
1957         struct net_device* dev;
1958         void* buf;
1959         unsigned dlci, len, offs, len_incl_hdr;
1960         int i, udp_type;        
1961
1962
1963         /* Check that firmware buffers are in sync */
1964         if (frbuf->flag != 0x01) {
1965
1966                 printk(KERN_INFO 
1967                         "%s: corrupted Rx buffer @ 0x%X, flag = 0x%02X!\n", 
1968                         card->devname, (unsigned)frbuf, frbuf->flag);
1969       
1970                 printk(KERN_INFO "%s: ID Bytes = ",card->devname);
1971                 for(i = 0; i < 8; i ++)
1972                         printk(KERN_INFO "0x%02X ", *(ptr + 0x28 + i));
1973                 printk(KERN_INFO "\n");
1974         
1975                 ++card->statistics.rx_intr_corrupt_rx_bfr;
1976
1977                 /* Bug Fix: Mar 6 2000
1978                  * If we get a corrupted mailbox, it means that driver 
1979                  * is out of sync with the firmware. There is no recovery.
1980                  * If we don't turn off all interrupts for this card
1981                  * the machine will crash. 
1982                  */
1983                 printk(KERN_INFO "%s: Critical router failure ...!!!\n", card->devname);
1984                 printk(KERN_INFO "Please contact Sangoma Technologies !\n");
1985                 fr_set_intr_mode(card, 0, 0, 0);        
1986                 return;
1987         }
1988
1989         len  = frbuf->length;
1990         dlci = frbuf->dlci;
1991         offs = frbuf->offset;
1992
1993         /* Find the network interface for this packet */
1994         dev = find_channel(card, dlci);
1995    
1996
1997         /* Check that the network interface is active and
1998          * properly setup */
1999         if (dev == NULL) {
2000                 if( net_ratelimit()) { 
2001                         printk(KERN_INFO "%s: received data on unconfigured DLCI %d!\n",
2002                                                 card->devname, dlci);
2003                 }
2004                 ++card->statistics.rx_intr_on_orphaned_DLCI; 
2005                 ++card->wandev.stats.rx_dropped;
2006                 goto rx_done;
2007         }
2008
2009         if ((chan = dev->priv) == NULL){
2010                 if( net_ratelimit()) { 
2011                         printk(KERN_INFO "%s: received data on unconfigured DLCI %d!\n",
2012                                                 card->devname, dlci);
2013                 }
2014                 ++card->statistics.rx_intr_on_orphaned_DLCI; 
2015                 ++card->wandev.stats.rx_dropped;
2016                 goto rx_done;
2017         }
2018
2019         skb = dev_alloc_skb(len); 
2020
2021         if (!netif_running(dev) || (skb == NULL)){
2022
2023                 ++chan->ifstats.rx_dropped;
2024         
2025                 if(skb == NULL) {
2026                         if (net_ratelimit()) { 
2027                                 printk(KERN_INFO 
2028                                         "%s: no socket buffers available!\n", 
2029                                                 card->devname);
2030                         }
2031                         chan->drvstats_rx_intr.rx_intr_no_socket ++;
2032                 } 
2033
2034                 if (!netif_running(dev)){
2035                         chan->drvstats_rx_intr.
2036                                 rx_intr_dev_not_started ++;
2037                         if (skb){
2038                                 dev_kfree_skb_any(skb);
2039                         }
2040                 }
2041                 goto rx_done;
2042         }
2043
2044         /* Copy data from the board into the socket buffer */
2045         if ((offs + len) > card->u.f.rx_top + 1) {
2046                 unsigned tmp = card->u.f.rx_top - offs + 1;
2047
2048                 buf = skb_put(skb, tmp);
2049                 sdla_peek(&card->hw, offs, buf, tmp);
2050                 offs = card->u.f.rx_base;
2051                 len -= tmp;
2052         }
2053
2054         buf = skb_put(skb, len);
2055         sdla_peek(&card->hw, offs, buf, len);
2056
2057
2058         /* We got the packet from the bard. 
2059          * Check the packet type and take appropriate action */
2060
2061         udp_type = udp_pkt_type( skb, card );
2062
2063         if(udp_type != UDP_INVALID_TYPE) {
2064
2065                 /* UDP Debug packet received, store the
2066                  * packet and handle it in timer interrupt */
2067
2068                 skb_pull(skb, 1); 
2069                 if (wanrouter_type_trans(skb, dev)){ 
2070                         if(store_udp_mgmt_pkt(udp_type,UDP_PKT_FRM_NETWORK,card,skb,dlci)){
2071
2072                                 flags->imask |= FR_INTR_TIMER;
2073
2074                                 if (udp_type == UDP_FPIPE_TYPE){
2075                                         ++chan->drvstats_rx_intr.rx_intr_PIPE_request;
2076                                 }
2077                         }
2078                 }
2079
2080         }else if (chan->common.usedby == API) {
2081
2082                 /* We are in API mode. 
2083                  * Add an API header to the RAW packet
2084                  * and queue it into a circular buffer.
2085                  * Then kick the fr_bh() bottom half handler */
2086
2087                 api_rx_hdr_t* api_rx_hdr;
2088                 chan->drvstats_rx_intr.rx_intr_bfr_passed_to_stack ++;
2089                 chan->ifstats.rx_packets ++;
2090                 card->wandev.stats.rx_packets ++;
2091
2092                 chan->ifstats.rx_bytes += skb->len;
2093                 card->wandev.stats.rx_bytes += skb->len;
2094
2095                 skb_push(skb, sizeof(api_rx_hdr_t));
2096                 api_rx_hdr = (api_rx_hdr_t*)&skb->data[0x00];
2097                 api_rx_hdr->attr = frbuf->attr;
2098                 api_rx_hdr->time_stamp = frbuf->tmstamp;
2099
2100                 skb->protocol = htons(ETH_P_IP);
2101                 skb->mac.raw  = skb->data;
2102                 skb->dev      = dev;
2103                 skb->pkt_type = WAN_PACKET_DATA;
2104
2105                 bh_enqueue(dev, skb);
2106
2107                 trigger_fr_bh(chan);
2108
2109         }else if (handle_IPXWAN(skb->data,chan->name,chan->enable_IPX, chan->network_number)){
2110
2111                 //FIXME: Frame Relay IPX is not supported, Yet !
2112                 //if (chan->enable_IPX) {
2113                 //      fr_send(card, dlci, 0, skb->len,skb->data);
2114                 //}
2115                 dev_kfree_skb_any(skb);
2116
2117         } else if (is_arp(skb->data)) {
2118
2119                 /* ARP support enabled Mar 16 2000 
2120                  * Process incoming ARP reply/request, setup
2121                  * dynamic routes. */ 
2122
2123                 if (process_ARP((arphdr_1490_t *)skb->data, card, dev)) {
2124                         if (net_ratelimit()){  
2125                                 printk (KERN_INFO 
2126                                    "%s: Error processing ARP Packet.\n", 
2127                                         card->devname);
2128                         }
2129                 }
2130                 dev_kfree_skb_any(skb);
2131
2132         } else if (skb->data[0] != 0x03) {
2133
2134                 if (net_ratelimit()) { 
2135                         printk(KERN_INFO "%s: Non IETF packet discarded.\n", 
2136                                 card->devname);
2137                 }
2138                 dev_kfree_skb_any(skb);
2139
2140         } else {
2141
2142                 len_incl_hdr = skb->len;
2143                 /* Decapsulate packet and pass it up the
2144                    protocol stack */
2145                 skb->dev = dev;
2146                 
2147                 if (chan->common.usedby == BRIDGE || chan->common.usedby == BRIDGE_NODE){
2148                 
2149                         /* Make sure it's an Ethernet frame, otherwise drop it */
2150                         if (!memcmp(skb->data, "\x03\x00\x80\x00\x80\xC2\x00\x07", 8)) {
2151                                 skb_pull(skb, 8);
2152                                 skb->protocol=eth_type_trans(skb,dev);
2153                         }else{
2154                                 ++chan->drvstats_rx_intr.rx_intr_bfr_not_passed_to_stack;
2155                                 ++chan->ifstats.rx_errors;
2156                                 ++card->wandev.stats.rx_errors;
2157                                 goto rx_done;
2158                         }
2159                 }else{
2160                 
2161                         /* remove hardware header */
2162                         buf = skb_pull(skb, 1); 
2163                         
2164                         if (!wanrouter_type_trans(skb, dev)) {
2165                                 
2166                                 /* can't decapsulate packet */
2167                                 dev_kfree_skb_any(skb);
2168
2169                                 ++chan->drvstats_rx_intr.rx_intr_bfr_not_passed_to_stack;
2170                                 ++chan->ifstats.rx_errors;
2171                                 ++card->wandev.stats.rx_errors;
2172                                 goto rx_done;   
2173                         }
2174                         skb->mac.raw = skb->data;
2175                 } 
2176                 
2177
2178                 /* Send a packet up the IP stack */
2179                 skb->dev->last_rx = jiffies;
2180                 netif_rx(skb);
2181                 ++chan->drvstats_rx_intr.rx_intr_bfr_passed_to_stack;
2182                 ++chan->ifstats.rx_packets;
2183                 ++card->wandev.stats.rx_packets;
2184
2185                 chan->ifstats.rx_bytes += len_incl_hdr;
2186                 card->wandev.stats.rx_bytes += len_incl_hdr;
2187         }
2188
2189 rx_done:
2190
2191         /* Release buffer element and calculate a pointer to the next one */ 
2192         frbuf->flag = 0;
2193         card->rxmb = ++frbuf;
2194         if ((void*)frbuf > card->u.f.rxmb_last)
2195                 card->rxmb = card->u.f.rxmb_base;
2196
2197 }
2198
2199 /*==================================================================
2200  * tx_intr:     Transmit interrupt handler.
2201  *
2202  * Rationale:
2203  *      If the board is busy transmitting, if_send() will
2204  *      buffers a single packet and turn on
2205  *      the tx interrupt. Tx interrupt will be called
2206  *      by the board, once the firmware can send more
2207  *      data. Thus, no polling is required.      
2208  *
2209  * Description:
2210  *      Tx interrupt is called for each 
2211  *      configured dlci channel. Thus: 
2212  *      1. Obtain the netowrk interface based on the
2213  *         dlci number.
2214  *      2. Check that network interface is up and
2215  *         properly setup.
2216  *      3. Check for a buffered packet.
2217  *      4. Transmit the packet.
2218  *      5. If we are in WANPIPE mode, mark the 
2219  *         NET_BH handler. 
2220  *      6. If we are in API mode, kick
2221  *         the AF_WANPIPE socket for more data. 
2222  *         
2223  */
2224 static void tx_intr(sdla_t *card)
2225 {
2226         fr508_flags_t* flags = card->flags;
2227         fr_tx_buf_ctl_t* bctl;
2228         struct net_device* dev;
2229         fr_channel_t* chan;
2230
2231         if(card->hw.type == SDLA_S514){
2232                 bctl = (void*)(flags->tse_offs + card->hw.dpmbase);
2233         }else{
2234                 bctl = (void*)(flags->tse_offs - FR_MB_VECTOR +
2235                         card->hw.dpmbase);
2236         }
2237
2238         /* Find the structure and make it unbusy */
2239         dev = find_channel(card, flags->dlci);
2240         if (dev == NULL){
2241                 printk(KERN_INFO "NO DEV IN TX Interrupt\n");   
2242                 goto end_of_tx_intr;
2243         }
2244
2245         if ((chan = dev->priv) == NULL){
2246                 printk(KERN_INFO "NO CHAN IN TX Interrupt\n");  
2247                 goto end_of_tx_intr;
2248         }
2249
2250         if(!chan->transmit_length || !chan->delay_skb) {
2251                 printk(KERN_INFO "%s: tx int error - transmit length zero\n",
2252                                 card->wandev.name);
2253                 goto end_of_tx_intr;
2254         }
2255
2256         /* If the 'if_send()' procedure is currently checking the 'tbusy'
2257            status, then we cannot transmit. Instead, we configure the microcode
2258            so as to re-issue this transmit interrupt at a later stage. 
2259         */
2260         if (test_bit(SEND_TXIRQ_CRIT, (void*)&card->wandev.critical)) {
2261
2262                 fr_dlci_interface_t* dlci_interface = chan->dlci_int_interface;
2263                 bctl->flag = 0xA0;
2264                 dlci_interface->gen_interrupt |= FR_INTR_TXRDY;
2265                 return;
2266
2267         }else{
2268                 bctl->dlci = flags->dlci;
2269                 bctl->length = chan->transmit_length+chan->fr_header_len;
2270                 sdla_poke(&card->hw, 
2271                           fr_send_hdr(card,bctl->dlci,bctl->offset), 
2272                           chan->delay_skb->data,
2273                           chan->delay_skb->len);
2274                 bctl->flag = 0xC0;
2275
2276                 ++chan->ifstats.tx_packets;
2277                 ++card->wandev.stats.tx_packets;
2278                 chan->ifstats.tx_bytes += chan->transmit_length;
2279                 card->wandev.stats.tx_bytes += chan->transmit_length;
2280
2281                 /* We must free an sk buffer, which we used
2282                  * for delayed transmission; Otherwise, the sock
2283                  * will run out of memory */
2284                 dev_kfree_skb_any(chan->delay_skb);
2285
2286                 chan->delay_skb = NULL;                         
2287                 chan->transmit_length = 0;
2288
2289                 dev->trans_start = jiffies;
2290
2291                 if (netif_queue_stopped(dev)){
2292                         /* If using API, than wakeup socket BH handler */
2293                         if (chan->common.usedby == API){
2294                                 netif_start_queue(dev);
2295                                 wakeup_sk_bh(dev);
2296                         }else{
2297                                 netif_wake_queue(dev);
2298                         }
2299                 }
2300         }
2301
2302 end_of_tx_intr:
2303
2304         /* if any other interfaces have transmit interrupts pending, 
2305          * do not disable the global transmit interrupt */
2306         if(!(-- card->u.f.tx_interrupts_pending))
2307                 flags->imask &= ~FR_INTR_TXRDY;
2308
2309
2310 }
2311
2312
2313 /*============================================================================
2314  * timer_intr:  Timer interrupt handler.
2315  *
2316  * Rationale:
2317  *      All commans must be executed within the timer
2318  *      interrupt since no two commands should execute
2319  *      at the same time.
2320  *
2321  * Description:
2322  *      The timer interrupt is used to:
2323  *      1. Processing udp calls from 'fpipemon'.
2324  *      2. Processing update calls from /proc file system
2325  *      3. Reading board-level statistics for 
2326  *         updating the proc file system.
2327  *      4. Sending inverse ARP request packets.
2328  *      5. Configure a dlci/channel.
2329  *      6. Unconfigure a dlci/channel. (Node only)
2330  */
2331
2332 static void timer_intr(sdla_t *card)
2333 {
2334         fr508_flags_t* flags = card->flags;
2335
2336         /* UDP Debuging: fpipemon call */
2337         if (card->u.f.timer_int_enabled & TMR_INT_ENABLED_UDP) {
2338                 if(card->u.f.udp_type == UDP_FPIPE_TYPE) {
2339                         if(process_udp_mgmt_pkt(card)) {
2340                                 card->u.f.timer_int_enabled &=
2341                                         ~TMR_INT_ENABLED_UDP;
2342                         }
2343                 }
2344         }
2345
2346         /* /proc update call : triggered from update() */
2347         if (card->u.f.timer_int_enabled & TMR_INT_ENABLED_UPDATE) {
2348                 fr_get_err_stats(card);
2349                 fr_get_stats(card);
2350                 card->u.f.update_comms_stats = 0;
2351                 card->u.f.timer_int_enabled &= ~TMR_INT_ENABLED_UPDATE;
2352         }
2353
2354         /* Update the channel state call.  This is call is
2355          * triggered by if_send() function */
2356         if (card->u.f.timer_int_enabled & TMR_INT_ENABLED_UPDATE_STATE){
2357                 struct net_device *dev;
2358                 if (card->wandev.state == WAN_CONNECTED){
2359                         for (dev = card->wandev.dev; dev;
2360                              dev = *((struct net_device **)dev->priv)){
2361                                 fr_channel_t *chan = dev->priv; 
2362                                 if (chan->common.state != WAN_CONNECTED){
2363                                         update_chan_state(dev);
2364                                 }
2365                         }
2366                 }
2367                 card->u.f.timer_int_enabled &= ~TMR_INT_ENABLED_UPDATE_STATE;
2368         }
2369
2370         /* configure a dlci/channel */
2371         if (card->u.f.timer_int_enabled & TMR_INT_ENABLED_CONFIG){
2372                 config_fr(card);
2373                 card->u.f.timer_int_enabled &= ~TMR_INT_ENABLED_CONFIG;
2374         }
2375
2376         /* unconfigure a dlci/channel */
2377         if (card->u.f.timer_int_enabled & TMR_INT_ENABLED_UNCONFIG){
2378                 unconfig_fr(card);
2379                 card->u.f.timer_int_enabled &= ~TMR_INT_ENABLED_UNCONFIG;
2380         }
2381
2382         
2383         /* Transmit ARP packets */
2384         if (card->u.f.timer_int_enabled & TMR_INT_ENABLED_ARP){
2385                 int i=0;
2386                 struct net_device *dev;
2387
2388                 if (card->u.f.arp_dev == NULL)
2389                         card->u.f.arp_dev = card->wandev.dev;
2390
2391                 dev = card->u.f.arp_dev;
2392
2393                 for (;;){ 
2394
2395                         fr_channel_t *chan = dev->priv;
2396
2397                         /* If the interface is brought down cancel sending In-ARPs */
2398                         if (!(dev->flags&IFF_UP)){
2399                                 clear_bit(0,&chan->inarp_ready);        
2400                         }
2401
2402                         if (test_bit(0,&chan->inarp_ready)){
2403
2404                                 if (check_tx_status(card,dev)){
2405                                         set_bit(ARP_CRIT,&card->wandev.critical);
2406                                         break;
2407                                 }
2408
2409                                 if (!send_inarp_request(card,dev)){
2410                                         trigger_fr_arp(dev);
2411                                         chan->inarp_tick = jiffies;
2412                                 }
2413
2414                                 clear_bit(0,&chan->inarp_ready);
2415                                 dev = move_dev_to_next(card,dev);
2416                                 break;
2417                         }
2418                         dev = move_dev_to_next(card,dev);
2419
2420                         if (++i == card->wandev.new_if_cnt){
2421                                 card->u.f.timer_int_enabled &= ~TMR_INT_ENABLED_ARP;
2422                                 break;
2423                         }
2424                 }
2425                 card->u.f.arp_dev = dev;
2426         }
2427
2428         if(!card->u.f.timer_int_enabled)
2429                 flags->imask &= ~FR_INTR_TIMER;
2430 }
2431
2432
2433 /*============================================================================
2434  * spur_intr:   Spurious interrupt handler.
2435  * 
2436  * Description:
2437  *      We don't know this interrupt.
2438  *      Print a warning.
2439  */
2440
2441 static void spur_intr (sdla_t* card)
2442 {
2443         if (net_ratelimit()){ 
2444                 printk(KERN_INFO "%s: spurious interrupt!\n", card->devname);
2445         }
2446 }
2447
2448
2449 //FIXME: Fix the IPX in next version
2450 /*===========================================================================
2451  *  Return 0 for non-IPXWAN packet
2452  *         1 for IPXWAN packet or IPX is not enabled!
2453  *  FIXME: Use a IPX structure here not offsets
2454  */
2455 static int handle_IPXWAN(unsigned char *sendpacket, 
2456                          char *devname, unsigned char enable_IPX, 
2457                          unsigned long network_number)
2458 {
2459         int i;
2460
2461         if( sendpacket[1] == 0x00 && sendpacket[2] == 0x80 &&
2462             sendpacket[6] == 0x81 && sendpacket[7] == 0x37) { 
2463
2464                 /* It's an IPX packet */
2465                 if (!enable_IPX){
2466                         /* Return 1 so we don't pass it up the stack. */
2467                         //FIXME: Take this out when IPX is fixed
2468                         if (net_ratelimit()){ 
2469                                 printk (KERN_INFO 
2470                                 "%s: WARNING: Unsupported IPX packet received and dropped\n",
2471                                         devname);
2472                         }
2473                         return 1;
2474                 }
2475         } else {
2476                 /* It's not IPX so return and pass it up the stack. */
2477                 return 0;
2478         }
2479
2480         if( sendpacket[24] == 0x90 && sendpacket[25] == 0x04){
2481                 /* It's IPXWAN */
2482
2483                 if( sendpacket[10] == 0x02 && sendpacket[42] == 0x00){
2484
2485                         /* It's a timer request packet */
2486                         printk(KERN_INFO "%s: Received IPXWAN Timer Request packet\n",
2487                                         devname);
2488
2489                         /* Go through the routing options and answer no to every
2490                          * option except Unnumbered RIP/SAP
2491                          */
2492                         for(i = 49; sendpacket[i] == 0x00; i += 5){
2493                                 /* 0x02 is the option for Unnumbered RIP/SAP */
2494                                 if( sendpacket[i + 4] != 0x02){
2495                                         sendpacket[i + 1] = 0;
2496                                 }
2497                         }
2498
2499                         /* Skip over the extended Node ID option */
2500                         if( sendpacket[i] == 0x04 ){
2501                                 i += 8;
2502                         }
2503
2504                         /* We also want to turn off all header compression opt.
2505                          */
2506                         for(; sendpacket[i] == 0x80 ;){
2507                                 sendpacket[i + 1] = 0;
2508                                 i += (sendpacket[i + 2] << 8) + (sendpacket[i + 3]) + 4;
2509                         }
2510
2511                         /* Set the packet type to timer response */
2512                         sendpacket[42] = 0x01;
2513
2514                         printk(KERN_INFO "%s: Sending IPXWAN Timer Response\n",
2515                                         devname);
2516
2517                 } else if( sendpacket[42] == 0x02 ){
2518
2519                         /* This is an information request packet */
2520                         printk(KERN_INFO 
2521                                 "%s: Received IPXWAN Information Request packet\n",
2522                                                 devname);
2523
2524                         /* Set the packet type to information response */
2525                         sendpacket[42] = 0x03;
2526
2527                         /* Set the router name */
2528                         sendpacket[59] = 'F';
2529                         sendpacket[60] = 'P';
2530                         sendpacket[61] = 'I';
2531                         sendpacket[62] = 'P';
2532                         sendpacket[63] = 'E';
2533                         sendpacket[64] = '-';
2534                         sendpacket[65] = CVHexToAscii(network_number >> 28);
2535                         sendpacket[66] = CVHexToAscii((network_number & 0x0F000000)>> 24);
2536                         sendpacket[67] = CVHexToAscii((network_number & 0x00F00000)>> 20);
2537                         sendpacket[68] = CVHexToAscii((network_number & 0x000F0000)>> 16);
2538                         sendpacket[69] = CVHexToAscii((network_number & 0x0000F000)>> 12);
2539                         sendpacket[70] = CVHexToAscii((network_number & 0x00000F00)>> 8);
2540                         sendpacket[71] = CVHexToAscii((network_number & 0x000000F0)>> 4);
2541                         sendpacket[72] = CVHexToAscii(network_number & 0x0000000F);
2542                         for(i = 73; i < 107; i+= 1)
2543                         {
2544                                 sendpacket[i] = 0;
2545                         }
2546
2547                         printk(KERN_INFO "%s: Sending IPXWAN Information Response packet\n",
2548                                         devname);
2549                 } else {
2550
2551                         printk(KERN_INFO "%s: Unknown IPXWAN packet!\n",devname);
2552                         return 0;
2553                 }
2554
2555                 /* Set the WNodeID to our network address */
2556                 sendpacket[43] = (unsigned char)(network_number >> 24);
2557                 sendpacket[44] = (unsigned char)((network_number & 0x00FF0000) >> 16);
2558                 sendpacket[45] = (unsigned char)((network_number & 0x0000FF00) >> 8);
2559                 sendpacket[46] = (unsigned char)(network_number & 0x000000FF);
2560
2561                 return 1;
2562         }
2563
2564         /* If we get here, it's an IPX-data packet so it'll get passed up the 
2565          * stack.
2566          * switch the network numbers 
2567          */
2568         switch_net_numbers(sendpacket, network_number ,1);
2569         return 0;
2570 }
2571 /*============================================================================
2572  * process_route
2573  * 
2574  * Rationale:
2575  *      If the interface goes down, or we receive an ARP request,
2576  *      we have to change the network interface ip addresses.
2577  *      This cannot be done within the interrupt.
2578  *
2579  * Description:
2580  *
2581  *      This routine is called as a polling routine to dynamically 
2582  *      add/delete routes negotiated by inverse ARP.  It is in this 
2583  *      "task" because we don't want routes to be added while in 
2584  *      interrupt context.
2585  *
2586  * Usage:
2587  *      This function is called by fr_poll() polling funtion.
2588  */
2589
2590 static void process_route(struct net_device *dev)
2591 {
2592         fr_channel_t *chan = dev->priv;
2593         sdla_t *card = chan->card;
2594
2595         struct ifreq if_info;
2596         struct sockaddr_in *if_data;
2597         mm_segment_t fs = get_fs();
2598         u32 ip_tmp;
2599         int err;
2600
2601
2602         switch(chan->route_flag){
2603
2604         case ADD_ROUTE:
2605                                 
2606                 /* Set remote addresses */
2607                 memset(&if_info, 0, sizeof(if_info));
2608                 strcpy(if_info.ifr_name, dev->name);
2609
2610                 set_fs(get_ds());     /* get user space block */ 
2611                 
2612                 if_data = (struct sockaddr_in *)&if_info.ifr_dstaddr;
2613                 if_data->sin_addr.s_addr = chan->ip_remote;
2614                 if_data->sin_family = AF_INET;
2615                 err = devinet_ioctl( SIOCSIFDSTADDR, &if_info );
2616
2617                 set_fs(fs);           /* restore old block */
2618
2619                 if (err) {
2620                         printk(KERN_INFO 
2621                                 "%s: Route Add failed.  Error: %d\n", 
2622                                         card->devname,err);
2623                         printk(KERN_INFO "%s: Address: %u.%u.%u.%u\n",
2624                                 chan->name, NIPQUAD(chan->ip_remote));
2625
2626                 }else {
2627                         printk(KERN_INFO "%s: Route Added Successfully: %u.%u.%u.%u\n",
2628                                 card->devname,NIPQUAD(chan->ip_remote));
2629                         chan->route_flag = ROUTE_ADDED;
2630                 }
2631                 break;
2632
2633         case REMOVE_ROUTE:
2634
2635                 /* Set remote addresses */
2636                 memset(&if_info, 0, sizeof(if_info));
2637                 strcpy(if_info.ifr_name, dev->name);
2638
2639                 ip_tmp = get_ip_address(dev,WAN_POINTOPOINT_IP);        
2640
2641                 set_fs(get_ds());     /* get user space block */ 
2642                 
2643                 if_data = (struct sockaddr_in *)&if_info.ifr_dstaddr;
2644                 if_data->sin_addr.s_addr = 0;
2645                 if_data->sin_family = AF_INET;
2646                 err = devinet_ioctl( SIOCSIFDSTADDR, &if_info );
2647
2648                 set_fs(fs);    
2649                 
2650                 if (err) {
2651                         printk(KERN_INFO 
2652                                 "%s: Deleting of route failed.  Error: %d\n", 
2653                                         card->devname,err);
2654                         printk(KERN_INFO "%s: Address: %u.%u.%u.%u\n",
2655                                 dev->name,NIPQUAD(chan->ip_remote) );
2656
2657                 } else {
2658                         printk(KERN_INFO "%s: Route Removed Sucessfuly: %u.%u.%u.%u\n", 
2659                                 card->devname,NIPQUAD(ip_tmp));
2660                         chan->route_flag = NO_ROUTE;
2661                 }
2662                 break;
2663
2664         } /* Case Statement */
2665
2666 }
2667
2668
2669
2670 /****** Frame Relay Firmware-Specific Functions *****************************/
2671
2672 /*============================================================================
2673  * Read firmware code version.
2674  * o fill string str with firmware version info. 
2675  */
2676 static int fr_read_version (sdla_t* card, char* str)
2677 {
2678         fr_mbox_t* mbox = card->mbox;
2679         int retry = MAX_CMD_RETRY;
2680         int err;
2681
2682         do
2683         {
2684                 mbox->cmd.command = FR_READ_CODE_VERSION;
2685                 mbox->cmd.length = 0;
2686                 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2687         } while (err && retry-- && fr_event(card, err, mbox));
2688         
2689         if (!err && str) {
2690                 int len = mbox->cmd.length;
2691                 memcpy(str, mbox->data, len);
2692                 str[len] = '\0';
2693         }
2694         return err;
2695 }
2696
2697 /*============================================================================
2698  * Set global configuration.
2699  */
2700 static int fr_configure (sdla_t* card, fr_conf_t *conf)
2701 {
2702         fr_mbox_t* mbox = card->mbox;
2703         int retry = MAX_CMD_RETRY;
2704         int dlci_num = card->u.f.dlci_num;
2705         int err, i;
2706
2707         do
2708         {
2709                 memcpy(mbox->data, conf, sizeof(fr_conf_t));
2710
2711                 if (dlci_num) for (i = 0; i < dlci_num; ++i)
2712                         ((fr_conf_t*)mbox->data)->dlci[i] = 
2713                                         card->u.f.node_dlci[i]; 
2714                 
2715                 mbox->cmd.command = FR_SET_CONFIG;
2716                 mbox->cmd.length =
2717                         sizeof(fr_conf_t) + dlci_num * sizeof(short);
2718
2719                 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2720         
2721         } while (err && retry-- && fr_event(card, err, mbox));
2722
2723         /*NC Oct 12 2000 */
2724         if (err != CMD_OK){
2725                 printk(KERN_ERR "%s: Frame Relay Configuration Failed: rc=0x%x\n",
2726                                 card->devname,err);
2727         }
2728         
2729         return err;
2730 }
2731
2732 /*============================================================================
2733  * Set DLCI configuration.
2734  */
2735 static int fr_dlci_configure (sdla_t* card, fr_dlc_conf_t *conf, unsigned dlci)
2736 {
2737         fr_mbox_t* mbox = card->mbox;
2738         int retry = MAX_CMD_RETRY;
2739         int err;
2740
2741         do
2742         {
2743                 memcpy(mbox->data, conf, sizeof(fr_dlc_conf_t));
2744                 mbox->cmd.dlci = (unsigned short) dlci; 
2745                 mbox->cmd.command = FR_SET_CONFIG;
2746                 mbox->cmd.length = sizeof(fr_dlc_conf_t);
2747                 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2748         } while (err && retry--);
2749         
2750         return err;
2751 }
2752 /*============================================================================
2753  * Set interrupt mode.
2754  */
2755 static int fr_set_intr_mode (sdla_t* card, unsigned mode, unsigned mtu,
2756         unsigned short timeout)
2757 {
2758         fr_mbox_t* mbox = card->mbox;
2759         fr508_intr_ctl_t* ictl = (void*)mbox->data;
2760         int retry = MAX_CMD_RETRY;
2761         int err;
2762
2763         do
2764         {
2765                 memset(ictl, 0, sizeof(fr508_intr_ctl_t));
2766                 ictl->mode   = mode;
2767                 ictl->tx_len = mtu;
2768                 ictl->irq    = card->hw.irq;
2769
2770                 /* indicate timeout on timer */
2771                 if (mode & 0x20) ictl->timeout = timeout; 
2772
2773                 mbox->cmd.length = sizeof(fr508_intr_ctl_t);
2774                 mbox->cmd.command = FR_SET_INTR_MODE;
2775                 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2776
2777         } while (err && retry-- && fr_event(card, err, mbox));
2778         
2779         return err;
2780 }
2781
2782 /*============================================================================
2783  * Enable communications.
2784  */
2785 static int fr_comm_enable (sdla_t* card)
2786 {
2787         fr_mbox_t* mbox = card->mbox;
2788         int retry = MAX_CMD_RETRY;
2789         int err;
2790
2791         do
2792         {
2793                 mbox->cmd.command = FR_COMM_ENABLE;
2794                 mbox->cmd.length = 0;
2795                 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2796         } while (err && retry-- && fr_event(card, err, mbox));
2797         
2798         return err;
2799 }
2800
2801 /*============================================================================
2802  * fr_comm_disable 
2803  *
2804  * Warning: This functin is called by the shutdown() procedure. It is void
2805  *          since dev->priv are has already been deallocated and no
2806  *          error checking is possible using fr_event() function.
2807  */
2808 static void fr_comm_disable (sdla_t* card)
2809 {
2810         fr_mbox_t* mbox = card->mbox;
2811         int retry = MAX_CMD_RETRY;
2812         int err;
2813
2814         do {
2815         mbox->cmd.command = FR_SET_MODEM_STATUS;
2816         mbox->cmd.length = 1;
2817         mbox->data[0] = 0;
2818         err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2819         } while (err && retry--);
2820         
2821         retry = MAX_CMD_RETRY;
2822         
2823         do
2824         {
2825                 mbox->cmd.command = FR_COMM_DISABLE;
2826                 mbox->cmd.length = 0;
2827                 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2828         } while (err && retry--);
2829
2830         return;
2831 }
2832
2833
2834
2835 /*============================================================================
2836  * Get communications error statistics. 
2837  */
2838 static int fr_get_err_stats (sdla_t* card)
2839 {
2840         fr_mbox_t* mbox = card->mbox;
2841         int retry = MAX_CMD_RETRY;
2842         int err;
2843
2844
2845         do
2846         {
2847                 mbox->cmd.command = FR_READ_ERROR_STATS;
2848                 mbox->cmd.length = 0;
2849                 mbox->cmd.dlci = 0;
2850                 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2851         } while (err && retry-- && fr_event(card, err, mbox));
2852
2853         if (!err) {
2854                 fr_comm_stat_t* stats = (void*)mbox->data;
2855                 card->wandev.stats.rx_over_errors    = stats->rx_overruns;
2856                 card->wandev.stats.rx_crc_errors     = stats->rx_bad_crc;
2857                 card->wandev.stats.rx_missed_errors  = stats->rx_aborts;
2858                 card->wandev.stats.rx_length_errors  = stats->rx_too_long;
2859                 card->wandev.stats.tx_aborted_errors = stats->tx_aborts;
2860         
2861         }
2862
2863         return err;
2864 }
2865
2866 /*============================================================================
2867  * Get statistics. 
2868  */
2869 static int fr_get_stats (sdla_t* card)
2870 {
2871         fr_mbox_t* mbox = card->mbox;
2872         int retry = MAX_CMD_RETRY;
2873         int err;
2874
2875
2876         do
2877         {
2878                 mbox->cmd.command = FR_READ_STATISTICS;
2879                 mbox->cmd.length = 0;
2880                 mbox->cmd.dlci = 0;
2881                 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2882         } while (err && retry-- && fr_event(card, err, mbox));
2883
2884         if (!err) {
2885                 fr_link_stat_t* stats = (void*)mbox->data;
2886                 card->wandev.stats.rx_frame_errors = stats->rx_bad_format;
2887                 card->wandev.stats.rx_dropped =
2888                         stats->rx_dropped + stats->rx_dropped2;
2889         }
2890
2891         return err;
2892 }
2893
2894 /*============================================================================
2895  * Add DLCI(s) (Access Node only!).
2896  * This routine will perform the ADD_DLCIs command for the specified DLCI.
2897  */
2898 static int fr_add_dlci (sdla_t* card, int dlci)
2899 {
2900         fr_mbox_t* mbox = card->mbox;
2901         int retry = MAX_CMD_RETRY;
2902         int err;
2903
2904         do
2905         {
2906                 unsigned short* dlci_list = (void*)mbox->data;
2907
2908                 mbox->cmd.length  = sizeof(short);
2909                 dlci_list[0] = dlci;
2910                 mbox->cmd.command = FR_ADD_DLCI;
2911                 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2912
2913         } while (err && retry-- && fr_event(card, err, mbox));
2914         
2915         return err;
2916 }
2917
2918 /*============================================================================
2919  * Activate DLCI(s) (Access Node only!). 
2920  * This routine will perform the ACTIVATE_DLCIs command with a DLCI number. 
2921  */
2922 static int fr_activate_dlci (sdla_t* card, int dlci)
2923 {
2924         fr_mbox_t* mbox = card->mbox;
2925         int retry = MAX_CMD_RETRY;
2926         int err;
2927
2928         do
2929         {
2930                 unsigned short* dlci_list = (void*)mbox->data;
2931
2932                 mbox->cmd.length  = sizeof(short);
2933                 dlci_list[0] = dlci;
2934                 mbox->cmd.command = FR_ACTIVATE_DLCI;
2935                 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2936
2937         } while (err && retry-- && fr_event(card, err, mbox));
2938         
2939         return err;
2940 }
2941
2942 /*============================================================================
2943  * Delete DLCI(s) (Access Node only!). 
2944  * This routine will perform the DELETE_DLCIs command with a DLCI number. 
2945  */
2946 static int fr_delete_dlci (sdla_t* card, int dlci)
2947 {
2948         fr_mbox_t* mbox = card->mbox;
2949         int retry = MAX_CMD_RETRY;
2950         int err;
2951
2952         do
2953         {
2954                 unsigned short* dlci_list = (void*)mbox->data;
2955
2956                 mbox->cmd.length  = sizeof(short);
2957                 dlci_list[0] = dlci;
2958                 mbox->cmd.command = FR_DELETE_DLCI;
2959                 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2960
2961         } while (err && retry-- && fr_event(card, err, mbox));
2962         
2963         return err;
2964 }
2965
2966
2967
2968 /*============================================================================
2969  * Issue in-channel signalling frame. 
2970  */
2971 static int fr_issue_isf (sdla_t* card, int isf)
2972 {
2973         fr_mbox_t* mbox = card->mbox;
2974         int retry = MAX_CMD_RETRY;
2975         int err;
2976
2977         do
2978         {
2979                 mbox->data[0] = isf;
2980                 mbox->cmd.length  = 1;
2981                 mbox->cmd.command = FR_ISSUE_IS_FRAME;
2982                 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2983         } while (err && retry-- && fr_event(card, err, mbox));
2984         
2985         return err;
2986 }
2987
2988
2989 static unsigned int fr_send_hdr (sdla_t*card, int dlci, unsigned int offset)
2990 {
2991         struct net_device *dev = find_channel(card,dlci);       
2992         fr_channel_t *chan;
2993
2994         if (!dev || !(chan=dev->priv))
2995                 return offset;
2996         
2997         if (chan->fr_header_len){
2998                 sdla_poke(&card->hw, offset, chan->fr_header, chan->fr_header_len);
2999         }
3000         
3001         return offset+chan->fr_header_len;
3002 }
3003
3004 /*============================================================================
3005  * Send a frame on a selected DLCI.  
3006  */
3007 static int fr_send_data_header (sdla_t* card, int dlci, unsigned char attr, int len,
3008         void *buf, unsigned char hdr_len)
3009 {
3010         fr_mbox_t* mbox = card->mbox + 0x800;
3011         int retry = MAX_CMD_RETRY;
3012         int err;
3013
3014         do
3015         {
3016                 mbox->cmd.dlci    = dlci;
3017                 mbox->cmd.attr    = attr;
3018                 mbox->cmd.length  = len+hdr_len;
3019                 mbox->cmd.command = FR_WRITE;
3020                 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
3021         } while (err && retry-- && fr_event(card, err, mbox));
3022
3023         if (!err) {
3024                 fr_tx_buf_ctl_t* frbuf;
3025  
3026                 if(card->hw.type == SDLA_S514)
3027                         frbuf = (void*)(*(unsigned long*)mbox->data +
3028                                 card->hw.dpmbase);
3029                 else
3030                         frbuf = (void*)(*(unsigned long*)mbox->data -
3031                                 FR_MB_VECTOR + card->hw.dpmbase);
3032
3033                 sdla_poke(&card->hw, fr_send_hdr(card,dlci,frbuf->offset), buf, len);
3034                 frbuf->flag = 0x01;
3035         }
3036
3037         return err;
3038 }
3039
3040 static int fr_send (sdla_t* card, int dlci, unsigned char attr, int len,
3041         void *buf)
3042 {
3043         fr_mbox_t* mbox = card->mbox + 0x800;
3044         int retry = MAX_CMD_RETRY;
3045         int err;
3046
3047         do
3048         {
3049                 mbox->cmd.dlci    = dlci;
3050                 mbox->cmd.attr    = attr;
3051                 mbox->cmd.length  = len;
3052                 mbox->cmd.command = FR_WRITE;
3053                 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
3054         } while (err && retry-- && fr_event(card, err, mbox));
3055
3056         if (!err) {
3057                 fr_tx_buf_ctl_t* frbuf;
3058  
3059                 if(card->hw.type == SDLA_S514)
3060                         frbuf = (void*)(*(unsigned long*)mbox->data +
3061                                 card->hw.dpmbase);
3062                 else
3063                         frbuf = (void*)(*(unsigned long*)mbox->data -
3064                                 FR_MB_VECTOR + card->hw.dpmbase);
3065
3066                 sdla_poke(&card->hw, frbuf->offset, buf, len);
3067                 frbuf->flag = 0x01;
3068         }
3069
3070         return err;
3071 }
3072
3073
3074 /****** Firmware Asynchronous Event Handlers ********************************/
3075
3076 /*============================================================================
3077  * Main asyncronous event/error handler.
3078  *      This routine is called whenever firmware command returns non-zero
3079  *      return code.
3080  *
3081  * Return zero if previous command has to be cancelled.
3082  */
3083 static int fr_event (sdla_t *card, int event, fr_mbox_t* mbox)
3084 {
3085         fr508_flags_t* flags = card->flags;
3086         char *ptr = &flags->iflag;
3087         int i;
3088
3089         switch (event) {
3090
3091                 case FRRES_MODEM_FAILURE:
3092                         return fr_modem_failure(card, mbox);
3093
3094                 case FRRES_CHANNEL_DOWN: {
3095                         struct net_device *dev;
3096
3097                         /* Remove all routes from associated DLCI's */
3098                         for (dev = card->wandev.dev; dev;
3099                              dev = *((struct net_device **)dev->priv)) {
3100                                 fr_channel_t *chan = dev->priv;
3101                                 if (chan->route_flag == ROUTE_ADDED) {
3102                                         chan->route_flag = REMOVE_ROUTE;
3103                                 }
3104
3105                                 if (chan->inarp == INARP_CONFIGURED) {
3106                                         chan->inarp = INARP_REQUEST;
3107                                 }
3108
3109                                 /* If the link becomes disconnected then,
3110                                  * all channels will be disconnected
3111                                  * as well.
3112                                  */
3113                                 set_chan_state(dev,WAN_DISCONNECTED);
3114                         }
3115                                 
3116                         wanpipe_set_state(card, WAN_DISCONNECTED);
3117                         return 1;
3118                         }
3119
3120                 case FRRES_CHANNEL_UP: {
3121                         struct net_device *dev;
3122
3123                         /* FIXME: Only startup devices that are on the list */
3124                         
3125                         for (dev = card->wandev.dev; dev;
3126                              dev = *((struct net_device **)dev->priv)) {
3127                                 
3128                                 set_chan_state(dev,WAN_CONNECTED);
3129                         }
3130
3131                         wanpipe_set_state(card, WAN_CONNECTED);
3132                         return 1;
3133                         }
3134
3135                 case FRRES_DLCI_CHANGE:
3136                         return fr_dlci_change(card, mbox);
3137
3138                 case FRRES_DLCI_MISMATCH:
3139                         printk(KERN_INFO "%s: DLCI list mismatch!\n", 
3140                                 card->devname);
3141                         return 1;
3142
3143                 case CMD_TIMEOUT:
3144                         printk(KERN_ERR "%s: command 0x%02X timed out!\n",
3145                                 card->devname, mbox->cmd.command);
3146                         printk(KERN_INFO "%s: ID Bytes = ",card->devname);
3147                         for(i = 0; i < 8; i ++)
3148                                 printk(KERN_INFO "0x%02X ", *(ptr + 0x18 + i));
3149                         printk(KERN_INFO "\n"); 
3150             
3151                         break;
3152
3153                 case FRRES_DLCI_INACTIVE:
3154                         break;
3155  
3156                 case FRRES_CIR_OVERFLOW:
3157                         break;
3158                         
3159                 case FRRES_BUFFER_OVERFLOW:
3160                         break; 
3161                         
3162                 default:
3163                         printk(KERN_INFO "%s: command 0x%02X returned 0x%02X!\n"
3164                                 , card->devname, mbox->cmd.command, event);
3165         }
3166
3167         return 0;
3168 }
3169
3170 /*============================================================================
3171  * Handle modem error.
3172  *
3173  * Return zero if previous command has to be cancelled.
3174  */
3175 static int fr_modem_failure (sdla_t *card, fr_mbox_t* mbox)
3176 {
3177         printk(KERN_INFO "%s: physical link down! (modem error 0x%02X)\n",
3178                 card->devname, mbox->data[0]);
3179
3180         switch (mbox->cmd.command){
3181                 case FR_WRITE:
3182         
3183                 case FR_READ:
3184                         return 0;
3185         }
3186         
3187         return 1;
3188 }
3189
3190 /*============================================================================
3191  * Handle DLCI status change.
3192  *
3193  * Return zero if previous command has to be cancelled.
3194  */
3195 static int fr_dlci_change (sdla_t *card, fr_mbox_t* mbox)
3196 {
3197         dlci_status_t* status = (void*)mbox->data;
3198         int cnt = mbox->cmd.length / sizeof(dlci_status_t);
3199         fr_channel_t *chan;
3200         struct net_device* dev2;
3201         
3202
3203         for (; cnt; --cnt, ++status) {
3204
3205                 unsigned short dlci= status->dlci;
3206                 struct net_device* dev = find_channel(card, dlci);
3207                 
3208                 if (dev == NULL){
3209                         printk(KERN_INFO 
3210                                 "%s: CPE contains unconfigured DLCI= %d\n", 
3211                                 card->devname, dlci);   
3212
3213                       printk(KERN_INFO
3214                                 "%s: unconfigured DLCI %d reported by network\n"
3215                                 , card->devname, dlci);
3216  
3217                 }else{
3218                         if (status->state == FR_LINK_INOPER) {
3219                                 printk(KERN_INFO
3220                                         "%s: DLCI %u is inactive!\n",
3221                                         card->devname, dlci);
3222
3223                                 if (dev && netif_running(dev))
3224                                         set_chan_state(dev, WAN_DISCONNECTED);
3225                         }
3226         
3227                         if (status->state & FR_DLCI_DELETED) {
3228
3229                                 printk(KERN_INFO
3230                                         "%s: DLCI %u has been deleted!\n",
3231                                         card->devname, dlci);
3232
3233                                 if (dev && netif_running(dev)){
3234
3235                                         fr_channel_t *chan = dev->priv;
3236
3237                                         if (chan->route_flag == ROUTE_ADDED) {
3238                                                 chan->route_flag = REMOVE_ROUTE;
3239                                                 /* The state change will trigger
3240                                                  * the fr polling routine */
3241                                         }
3242
3243                                         if (chan->inarp == INARP_CONFIGURED) {
3244                                                 chan->inarp = INARP_REQUEST;
3245                                         }
3246
3247                                         set_chan_state(dev, WAN_DISCONNECTED);
3248                                 }
3249
3250                         } else if (status->state & FR_DLCI_ACTIVE) {
3251
3252                                 chan = dev->priv;
3253                         
3254                                 /* This flag is used for configuring specific 
3255                                    DLCI(s) when they become active.
3256                                 */ 
3257                                 chan->dlci_configured = DLCI_CONFIG_PENDING;
3258         
3259                                 set_chan_state(dev, WAN_CONNECTED);
3260                 
3261                         }
3262                 }
3263         }
3264         
3265         for (dev2 = card->wandev.dev; dev2;
3266              dev2 = *((struct net_device **)dev2->priv)){
3267                 
3268                 chan = dev2->priv;
3269         
3270                 if (chan->dlci_configured == DLCI_CONFIG_PENDING) {
3271                         if (fr_init_dlci(card, chan)){
3272                                 return 1;
3273                         }
3274                 }
3275
3276         }
3277         return 1;
3278 }
3279
3280
3281 static int fr_init_dlci (sdla_t *card, fr_channel_t *chan)
3282 {
3283         fr_dlc_conf_t cfg;
3284         
3285         memset(&cfg, 0, sizeof(cfg));
3286
3287         if ( chan->cir_status == CIR_DISABLED) {
3288
3289                 cfg.cir_fwd = cfg.cir_bwd  = 16;
3290                 cfg.bc_fwd = cfg.bc_bwd = 16;
3291                 cfg.conf_flags = 0x0001;        
3292
3293         }else if (chan->cir_status == CIR_ENABLED) {
3294         
3295                 cfg.cir_fwd = cfg.cir_bwd = chan->cir;
3296                 cfg.bc_fwd  = cfg.bc_bwd  = chan->bc;
3297                 cfg.be_fwd  = cfg.be_bwd  = chan->be;
3298                 cfg.conf_flags = 0x0000;
3299         }
3300         
3301         if (fr_dlci_configure( card, &cfg , chan->dlci)){
3302                 printk(KERN_INFO 
3303                         "%s: DLCI Configure failed for %d\n",
3304                                 card->devname, chan->dlci);
3305                 return 1;       
3306         }
3307         
3308         chan->dlci_configured = DLCI_CONFIGURED;
3309
3310         /* Read the interface byte mapping into the channel 
3311          * structure.
3312          */
3313         read_DLCI_IB_mapping( card, chan );
3314
3315         return 0;
3316 }
3317 /******* Miscellaneous ******************************************************/
3318
3319 /*============================================================================
3320  * Update channel state. 
3321  */
3322 static int update_chan_state(struct net_device* dev)
3323 {
3324         fr_channel_t* chan = dev->priv;
3325         sdla_t* card = chan->card;
3326         fr_mbox_t* mbox = card->mbox;
3327         int retry = MAX_CMD_RETRY;
3328         int err;
3329
3330         do
3331         {
3332                 mbox->cmd.command = FR_LIST_ACTIVE_DLCI;
3333                 mbox->cmd.length = 0;
3334                 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
3335         } while (err && retry-- && fr_event(card, err, mbox));
3336
3337         if (!err) {
3338                 
3339                 unsigned short* list = (void*)mbox->data;
3340                 int cnt = mbox->cmd.length / sizeof(short);
3341                 
3342                 err=1;
3343                 
3344                 for (; cnt; --cnt, ++list) {
3345
3346                         if (*list == chan->dlci) {
3347                                 set_chan_state(dev, WAN_CONNECTED);
3348
3349
3350                                 /* May 23 2000. NC
3351                                  * When a dlci is added or restarted,
3352                                  * the dlci_int_interface pointer must
3353                                  * be reinitialized.  */
3354                                 if (!chan->dlci_int_interface){
3355                                         err=fr_init_dlci (card,chan);
3356                                 }
3357                                 break;
3358                         }
3359                 }
3360         }
3361
3362         return err;
3363 }
3364
3365 /*============================================================================
3366  * Set channel state.
3367  */
3368 static void set_chan_state(struct net_device* dev, int state)
3369 {
3370         fr_channel_t* chan = dev->priv;
3371         sdla_t* card = chan->card;
3372
3373         if (chan->common.state != state) {
3374
3375                 switch (state) {
3376
3377                         case WAN_CONNECTED:
3378                                 printk(KERN_INFO
3379                                         "%s: Interface %s: DLCI %d connected\n",
3380                                         card->devname, dev->name, chan->dlci);
3381
3382                                 /* If the interface was previoulsy down,
3383                                  * bring it up, since the channel is active */
3384
3385                                 trigger_fr_poll (dev);
3386                                 trigger_fr_arp  (dev);
3387                                 break;
3388
3389                         case WAN_CONNECTING:
3390                                 printk(KERN_INFO 
3391                                       "%s: Interface %s: DLCI %d connecting\n",
3392                                         card->devname, dev->name, chan->dlci);
3393                                 break;
3394
3395                         case WAN_DISCONNECTED:
3396                                 printk (KERN_INFO 
3397                                     "%s: Interface %s: DLCI %d disconnected!\n",
3398                                         card->devname, dev->name, chan->dlci);
3399                         
3400                                 /* If the interface is up, bring it down,
3401                                  * since the channel is now disconnected */
3402                                 trigger_fr_poll (dev);
3403                                 break;
3404                 }
3405
3406                 chan->common.state = state;
3407         }
3408
3409         chan->state_tick = jiffies;
3410 }
3411
3412 /*============================================================================
3413  * Find network device by its channel number.
3414  *
3415  * We need this critical flag because we change
3416  * the dlci_to_dev_map outside the interrupt.
3417  *
3418  * NOTE: del_if() functions updates this array, it uses
3419  *       the spin locks to avoid corruption.
3420  */
3421 static struct net_device* find_channel(sdla_t* card, unsigned dlci)
3422 {
3423         if(dlci > HIGHEST_VALID_DLCI)
3424                 return NULL;
3425
3426         return(card->u.f.dlci_to_dev_map[dlci]);
3427 }
3428
3429 /*============================================================================
3430  * Check to see if a frame can be sent. If no transmit buffers available,
3431  * enable transmit interrupts.
3432  *
3433  * Return:      1 - Tx buffer(s) available
3434  *              0 - no buffers available
3435  */
3436 static int is_tx_ready (sdla_t* card, fr_channel_t* chan)
3437 {
3438         unsigned char sb;
3439
3440         if(card->hw.type == SDLA_S514)
3441                 return 1;
3442
3443         sb = inb(card->hw.port);
3444         if (sb & 0x02) 
3445                 return 1;
3446
3447         return 0;
3448 }
3449
3450 /*============================================================================
3451  * Convert decimal string to unsigned integer.
3452  * If len != 0 then only 'len' characters of the string are converted.
3453  */
3454 static unsigned int dec_to_uint (unsigned char* str, int len)
3455 {
3456         unsigned val;
3457
3458         if (!len) 
3459                 len = strlen(str);
3460
3461         for (val = 0; len && is_digit(*str); ++str, --len)
3462                 val = (val * 10) + (*str - (unsigned)'0');
3463
3464         return val;
3465 }
3466
3467
3468
3469 /*=============================================================================
3470  * Store a UDP management packet for later processing.
3471  */
3472
3473 static int store_udp_mgmt_pkt(int udp_type, char udp_pkt_src, sdla_t* card,
3474                                 struct sk_buff *skb, int dlci)
3475 {
3476         int udp_pkt_stored = 0;
3477         
3478         struct net_device *dev = find_channel(card, dlci);
3479         fr_channel_t *chan;
3480         
3481         if (!dev || !(chan=dev->priv))
3482                 return 1;
3483         
3484         if(!card->u.f.udp_pkt_lgth && (skb->len <= MAX_LGTH_UDP_MGNT_PKT)){
3485                 card->u.f.udp_pkt_lgth = skb->len + chan->fr_header_len;
3486                 card->u.f.udp_type = udp_type;
3487                 card->u.f.udp_pkt_src = udp_pkt_src;
3488                 card->u.f.udp_dlci = dlci;
3489                 memcpy(card->u.f.udp_pkt_data, skb->data, skb->len);
3490                 card->u.f.timer_int_enabled |= TMR_INT_ENABLED_UDP;
3491                 udp_pkt_stored = 1;
3492
3493         }else{
3494                 printk(KERN_INFO "ERROR: UDP packet not stored for DLCI %d\n", 
3495                                                         dlci);
3496         }
3497
3498         if(udp_pkt_src == UDP_PKT_FRM_STACK){
3499                 dev_kfree_skb_any(skb);
3500         }else{
3501                 dev_kfree_skb_any(skb);
3502         }
3503                 
3504         return(udp_pkt_stored);
3505 }
3506
3507
3508 /*==============================================================================
3509  * Process UDP call of type FPIPE8ND
3510  */
3511 static int process_udp_mgmt_pkt(sdla_t* card)
3512 {
3513
3514         int c_retry = MAX_CMD_RETRY;
3515         unsigned char *buf;
3516         unsigned char frames;
3517         unsigned int len;
3518         unsigned short buffer_length;
3519         struct sk_buff *new_skb;
3520         fr_mbox_t* mbox = card->mbox;
3521         int err;
3522         struct timeval tv;
3523         int udp_mgmt_req_valid = 1;
3524         struct net_device* dev;
3525         fr_channel_t* chan;
3526         fr_udp_pkt_t *fr_udp_pkt;
3527         unsigned short num_trc_els;
3528         fr_trc_el_t* ptr_trc_el;
3529         fr_trc_el_t trc_el;
3530         fpipemon_trc_t* fpipemon_trc;
3531
3532         char udp_pkt_src = card->u.f.udp_pkt_src; 
3533         int dlci = card->u.f.udp_dlci;
3534
3535         /* Find network interface for this packet */
3536         dev = find_channel(card, dlci);
3537         if (!dev){
3538                 card->u.f.udp_pkt_lgth = 0;
3539                 return 1;
3540         }
3541         if ((chan = dev->priv) == NULL){
3542                 card->u.f.udp_pkt_lgth = 0;
3543                 return 1;
3544         }
3545
3546         /* If the UDP packet is from the network, we are going to have to 
3547            transmit a response. Before doing so, we must check to see that
3548            we are not currently transmitting a frame (in 'if_send()') and
3549            that we are not already in a 'delayed transmit' state.
3550         */
3551         if(udp_pkt_src == UDP_PKT_FRM_NETWORK) {
3552                 if (check_tx_status(card,dev)){
3553                         card->u.f.udp_pkt_lgth = 0;
3554                         return 1;
3555                 }
3556         }
3557
3558         fr_udp_pkt = (fr_udp_pkt_t *)card->u.f.udp_pkt_data;
3559
3560         if(udp_pkt_src == UDP_PKT_FRM_NETWORK) {
3561         
3562                 switch(fr_udp_pkt->cblock.command) {
3563
3564                         case FR_READ_MODEM_STATUS:
3565                         case FR_READ_STATUS:
3566                         case FPIPE_ROUTER_UP_TIME:
3567                         case FR_READ_ERROR_STATS:
3568                         case FPIPE_DRIVER_STAT_GEN:
3569                         case FR_READ_STATISTICS:
3570                         case FR_READ_ADD_DLC_STATS:
3571                         case FR_READ_CONFIG:
3572                         case FR_READ_CODE_VERSION:
3573                                 udp_mgmt_req_valid = 1;
3574                                 break;
3575                         default:
3576                                 udp_mgmt_req_valid = 0;
3577                                 break;
3578                 }
3579         }
3580
3581         if(!udp_mgmt_req_valid) {
3582                 /* set length to 0 */
3583                 fr_udp_pkt->cblock.length = 0;
3584                 /* set return code */
3585                 fr_udp_pkt->cblock.result = 0xCD; 
3586                 
3587                 chan->drvstats_gen.UDP_PIPE_mgmt_direction_err ++;
3588
3589                 if (net_ratelimit()){   
3590                         printk(KERN_INFO 
3591                         "%s: Warning, Illegal UDP command attempted from network: %x\n",
3592                         card->devname,fr_udp_pkt->cblock.command);
3593                 }
3594                 
3595         } else {   
3596            
3597                 switch(fr_udp_pkt->cblock.command) {
3598
3599                 case FPIPE_ENABLE_TRACING:
3600                         if(!card->TracingEnabled) {
3601                                 do {
3602                                         mbox->cmd.command = FR_SET_TRACE_CONFIG;
3603                                         mbox->cmd.length = 1;
3604                                         mbox->cmd.dlci = 0x00;
3605                                         mbox->data[0] = fr_udp_pkt->data[0] | 
3606                                                 RESET_TRC;
3607                                         err = sdla_exec(mbox) ? 
3608                                                         mbox->cmd.result : CMD_TIMEOUT;
3609                                 } while (err && c_retry-- && fr_event(card, err,
3610                                          mbox));
3611
3612                                 if(err) {
3613                                         card->TracingEnabled = 0;
3614                                         /* set the return code */
3615                                         fr_udp_pkt->cblock.result =
3616                                                 mbox->cmd.result;
3617                                         mbox->cmd.length = 0;
3618                                         break;
3619                                 }
3620
3621                                 sdla_peek(&card->hw, NO_TRC_ELEMENTS_OFF,
3622                                                 &num_trc_els, 2);
3623                                 sdla_peek(&card->hw, BASE_TRC_ELEMENTS_OFF,
3624                                                 &card->u.f.trc_el_base, 4);
3625                                 card->u.f.curr_trc_el = card->u.f.trc_el_base;
3626                                 card->u.f.trc_el_last = card->u.f.curr_trc_el +
3627                                                         ((num_trc_els - 1) * 
3628                                                         sizeof(fr_trc_el_t));
3629    
3630                                 /* Calculate the maximum trace data area in */
3631                                 /* the UDP packet */
3632                                 card->u.f.trc_bfr_space=(MAX_LGTH_UDP_MGNT_PKT -
3633                                         //sizeof(fr_encap_hdr_t) -
3634                                         sizeof(ip_pkt_t) -
3635                                         sizeof(udp_pkt_t) -
3636                                         sizeof(wp_mgmt_t) -
3637                                         sizeof(cblock_t));
3638
3639                                 /* set return code */
3640                                 fr_udp_pkt->cblock.result = 0;
3641                         
3642                         } else {
3643                                 /* set return code to line trace already 
3644                                    enabled */
3645                                 fr_udp_pkt->cblock.result = 1;
3646                         }
3647
3648                         mbox->cmd.length = 0;
3649                         card->TracingEnabled = 1;
3650                         break;
3651
3652
3653                 case FPIPE_DISABLE_TRACING:
3654                         if(card->TracingEnabled) {
3655                         
3656                                 do {
3657                                         mbox->cmd.command = FR_SET_TRACE_CONFIG;
3658                                         mbox->cmd.length = 1;
3659                                         mbox->cmd.dlci = 0x00;
3660                                         mbox->data[0] = ~ACTIVATE_TRC;
3661                                         err = sdla_exec(mbox) ? 
3662                                                         mbox->cmd.result : CMD_TIMEOUT;
3663                                 } while (err && c_retry-- && fr_event(card, err, mbox));
3664                         }
3665
3666                         /* set return code */
3667                         fr_udp_pkt->cblock.result = 0;
3668                         mbox->cmd.length = 0;
3669                         card->TracingEnabled = 0;
3670                         break;
3671
3672                 case FPIPE_GET_TRACE_INFO:
3673
3674                         /* Line trace cannot be performed on the 502 */
3675                         if(!card->TracingEnabled) {
3676                                 /* set return code */
3677                                 fr_udp_pkt->cblock.result = 1;
3678                                 mbox->cmd.length = 0;
3679                                 break;
3680                         }
3681
3682                         ptr_trc_el = (void *)card->u.f.curr_trc_el;
3683
3684                         buffer_length = 0;
3685                         fr_udp_pkt->data[0x00] = 0x00;
3686
3687                         for(frames = 0; frames < MAX_FRMS_TRACED; frames ++) {
3688
3689                                 sdla_peek(&card->hw, (unsigned long)ptr_trc_el,
3690                                           (void *)&trc_el.flag,
3691                                           sizeof(fr_trc_el_t));
3692                                 if(trc_el.flag == 0x00) {
3693                                         break;
3694                                 }
3695                                 if((card->u.f.trc_bfr_space - buffer_length)
3696                                         < sizeof(fpipemon_trc_hdr_t)) { 
3697                                         fr_udp_pkt->data[0x00] |= MORE_TRC_DATA;
3698                                         break;
3699                                 }
3700
3701                                 fpipemon_trc = 
3702                                         (fpipemon_trc_t *)&fr_udp_pkt->data[buffer_length]; 
3703                                 fpipemon_trc->fpipemon_trc_hdr.status =
3704                                         trc_el.attr;
3705                                 fpipemon_trc->fpipemon_trc_hdr.tmstamp =
3706                                         trc_el.tmstamp;
3707                                 fpipemon_trc->fpipemon_trc_hdr.length = 
3708                                         trc_el.length;
3709
3710                                 if(!trc_el.offset || !trc_el.length) {
3711
3712                                         fpipemon_trc->fpipemon_trc_hdr.data_passed = 0x00;
3713
3714                                 }else if((trc_el.length + sizeof(fpipemon_trc_hdr_t) + 1) >
3715                                         (card->u.f.trc_bfr_space - buffer_length)){
3716
3717                                         fpipemon_trc->fpipemon_trc_hdr.data_passed = 0x00;
3718                                         fr_udp_pkt->data[0x00] |= MORE_TRC_DATA;
3719  
3720                                 }else {
3721                                         fpipemon_trc->fpipemon_trc_hdr.data_passed = 0x01;
3722                                         sdla_peek(&card->hw, trc_el.offset,
3723                                                   fpipemon_trc->data,
3724                                                   trc_el.length);
3725                                 }                       
3726
3727                                 trc_el.flag = 0x00;
3728                                 sdla_poke(&card->hw, (unsigned long)ptr_trc_el,
3729                                           &trc_el.flag, 1);
3730                                
3731                                 ptr_trc_el ++;
3732                                 if((void *)ptr_trc_el > card->u.f.trc_el_last)
3733                                         ptr_trc_el = (void*)card->u.f.trc_el_base;
3734
3735                                 buffer_length += sizeof(fpipemon_trc_hdr_t);
3736                                 if(fpipemon_trc->fpipemon_trc_hdr.data_passed) {
3737                                         buffer_length += trc_el.length;
3738                                 }
3739
3740                                 if(fr_udp_pkt->data[0x00] & MORE_TRC_DATA) {
3741                                         break;
3742                                 }
3743                         }
3744                       
3745                         if(frames == MAX_FRMS_TRACED) {
3746                                 fr_udp_pkt->data[0x00] |= MORE_TRC_DATA;
3747                         }
3748              
3749                         card->u.f.curr_trc_el = (void *)ptr_trc_el;
3750
3751                         /* set the total number of frames passed */
3752                         fr_udp_pkt->data[0x00] |=
3753                                 ((frames << 1) & (MAX_FRMS_TRACED << 1));
3754
3755                         /* set the data length and return code */
3756                         fr_udp_pkt->cblock.length = mbox->cmd.length = buffer_length;
3757                         fr_udp_pkt->cblock.result = 0;
3758                         break;
3759
3760                 case FPIPE_FT1_READ_STATUS:
3761                         sdla_peek(&card->hw, 0xF020,
3762                                 &fr_udp_pkt->data[0x00] , 2);
3763                         fr_udp_pkt->cblock.length = mbox->cmd.length = 2;
3764                         fr_udp_pkt->cblock.result = 0;
3765                         break;
3766
3767                 case FPIPE_FLUSH_DRIVER_STATS:
3768                         init_chan_statistics(chan);
3769                         init_global_statistics(card);
3770                         mbox->cmd.length = 0;
3771                         break;
3772                 
3773                 case FPIPE_ROUTER_UP_TIME:
3774                         do_gettimeofday(&tv);
3775                         chan->router_up_time = tv.tv_sec - 
3776                                                 chan->router_start_time;
3777                         *(unsigned long *)&fr_udp_pkt->data =
3778                                 chan->router_up_time;   
3779                         mbox->cmd.length = fr_udp_pkt->cblock.length = 4;
3780                         fr_udp_pkt->cblock.result = 0;
3781                         break;
3782
3783                 case FPIPE_DRIVER_STAT_IFSEND:
3784                         memcpy(fr_udp_pkt->data,
3785                                 &chan->drvstats_if_send.if_send_entry,
3786                                 sizeof(if_send_stat_t));
3787                         mbox->cmd.length = fr_udp_pkt->cblock.length =sizeof(if_send_stat_t);   
3788                         fr_udp_pkt->cblock.result = 0;
3789                         break;
3790         
3791                 case FPIPE_DRIVER_STAT_INTR:
3792
3793                         memcpy(fr_udp_pkt->data,
3794                                 &card->statistics.isr_entry,
3795                                 sizeof(global_stats_t));
3796
3797                         memcpy(&fr_udp_pkt->data[sizeof(global_stats_t)],
3798                                 &chan->drvstats_rx_intr.rx_intr_no_socket,
3799                                 sizeof(rx_intr_stat_t));
3800
3801                         mbox->cmd.length = fr_udp_pkt->cblock.length = 
3802                                         sizeof(global_stats_t) +
3803                                         sizeof(rx_intr_stat_t);
3804                         fr_udp_pkt->cblock.result = 0;
3805                         break;
3806
3807                 case FPIPE_DRIVER_STAT_GEN:
3808                         memcpy(fr_udp_pkt->data,
3809                                 &chan->drvstats_gen.UDP_PIPE_mgmt_kmalloc_err,
3810                                 sizeof(pipe_mgmt_stat_t));
3811
3812                         memcpy(&fr_udp_pkt->data[sizeof(pipe_mgmt_stat_t)],
3813                                &card->statistics, sizeof(global_stats_t));
3814
3815                         mbox->cmd.length = fr_udp_pkt->cblock.length = sizeof(global_stats_t)+
3816                                                      sizeof(rx_intr_stat_t);
3817                         fr_udp_pkt->cblock.result = 0;
3818                         break;
3819
3820
3821                 case FR_FT1_STATUS_CTRL:
3822                         if(fr_udp_pkt->data[0] == 1) {
3823                                 if(rCount++ != 0 ){
3824                                         fr_udp_pkt->cblock.result = 0;
3825                                         mbox->cmd.length = 1;
3826                                         break;
3827                                 } 
3828                         }
3829            
3830                         /* Disable FT1 MONITOR STATUS */
3831                         if(fr_udp_pkt->data[0] == 0) {
3832                                 if( --rCount != 0) {
3833                                         fr_udp_pkt->cblock.result = 0;
3834                                         mbox->cmd.length = 1;
3835                                         break;
3836                                 } 
3837                         }  
3838                         goto udp_mgmt_dflt;
3839
3840                         
3841                 default:
3842 udp_mgmt_dflt:
3843                         do {
3844                                 memcpy(&mbox->cmd,
3845                                         &fr_udp_pkt->cblock.command,
3846                                         sizeof(fr_cmd_t));
3847                                 if(mbox->cmd.length) {
3848                                         memcpy(&mbox->data,
3849                                                 (char *)fr_udp_pkt->data,
3850                                                 mbox->cmd.length);
3851                                 }
3852                                 
3853                                 err = sdla_exec(mbox) ? mbox->cmd.result : 
3854                                         CMD_TIMEOUT;
3855                         } while (err && c_retry-- && fr_event(card, err, mbox));
3856
3857                         if(!err)
3858                                 chan->drvstats_gen.
3859                                         UDP_PIPE_mgmt_adptr_cmnd_OK ++;
3860                         else
3861                                 chan->drvstats_gen.
3862                                         UDP_PIPE_mgmt_adptr_cmnd_timeout ++;
3863
3864                         /* copy the result back to our buffer */
3865                         memcpy(&fr_udp_pkt->cblock.command,
3866                                 &mbox->cmd, sizeof(fr_cmd_t));
3867
3868                         if(mbox->cmd.length) {
3869                                 memcpy(&fr_udp_pkt->data,
3870                                         &mbox->data, mbox->cmd.length);
3871                         }
3872                 } 
3873         }
3874    
3875         /* Fill UDP TTL */
3876         fr_udp_pkt->ip_pkt.ttl = card->wandev.ttl;
3877         len = reply_udp(card->u.f.udp_pkt_data, mbox->cmd.length);
3878
3879         if(udp_pkt_src == UDP_PKT_FRM_NETWORK) {
3880
3881                 chan->fr_header_len=2;
3882                 chan->fr_header[0]=Q922_UI;
3883                 chan->fr_header[1]=NLPID_IP;
3884                         
3885                 err = fr_send_data_header(card, dlci, 0, len, 
3886                         card->u.f.udp_pkt_data,chan->fr_header_len);
3887                 if (err){ 
3888                         chan->drvstats_gen.UDP_PIPE_mgmt_adptr_send_passed ++;
3889                 }else{
3890                         chan->drvstats_gen.UDP_PIPE_mgmt_adptr_send_failed ++;
3891                 }
3892                 
3893         } else {
3894                 /* Allocate socket buffer */
3895                 if((new_skb = dev_alloc_skb(len)) != NULL) {
3896
3897                         /* copy data into new_skb */
3898                         buf = skb_put(new_skb, len);
3899                         memcpy(buf, card->u.f.udp_pkt_data, len);
3900         
3901                         chan->drvstats_gen.
3902                                 UDP_PIPE_mgmt_passed_to_stack ++;
3903                         new_skb->dev = dev;
3904                         new_skb->protocol = htons(ETH_P_IP);
3905                         new_skb->mac.raw = new_skb->data;
3906                         netif_rx(new_skb);
3907                 
3908                 } else {
3909                         chan->drvstats_gen.UDP_PIPE_mgmt_no_socket ++;
3910                         printk(KERN_INFO 
3911                         "%s: UDP mgmt cmnd, no socket buffers available!\n", 
3912                         card->devname);
3913                 }
3914         }
3915
3916         card->u.f.udp_pkt_lgth = 0;
3917
3918         return 1;
3919 }
3920
3921 /*==============================================================================
3922  * Send Inverse ARP Request
3923  */
3924
3925 int send_inarp_request(sdla_t *card, struct net_device *dev)
3926 {
3927         int err=0;
3928
3929         arphdr_1490_t *ArpPacket;
3930         arphdr_fr_t *arphdr;
3931         fr_channel_t *chan = dev->priv;
3932         struct in_device *in_dev;
3933
3934         in_dev = dev->ip_ptr;
3935
3936         if(in_dev != NULL ) {   
3937
3938                 ArpPacket = kmalloc(sizeof(arphdr_1490_t) + sizeof(arphdr_fr_t), GFP_ATOMIC);
3939                 /* SNAP Header indicating ARP */
3940                 ArpPacket->control      = 0x03;
3941                 ArpPacket->pad          = 0x00;
3942                 ArpPacket->NLPID        = 0x80;
3943                 ArpPacket->OUI[0]       = 0;
3944                 ArpPacket->OUI[1]       = 0;
3945                 ArpPacket->OUI[2]       = 0;
3946                 ArpPacket->PID          = 0x0608;
3947
3948                 arphdr = (arphdr_fr_t *)(ArpPacket + 1); // Go to ARP Packet
3949
3950                 /* InARP request */             
3951                 arphdr->ar_hrd = 0x0F00;        /* Frame Relay HW type */
3952                 arphdr->ar_pro = 0x0008;        /* IP Protocol         */
3953                 arphdr->ar_hln = 2;             /* HW addr length      */
3954                 arphdr->ar_pln = 4;             /* IP addr length      */
3955                 arphdr->ar_op = htons(0x08);    /* InARP Request       */
3956                 arphdr->ar_sha = 0;             /* src HW DLCI - Doesn't matter */
3957                 if(in_dev->ifa_list != NULL)
3958                         arphdr->ar_sip = in_dev->ifa_list->ifa_local;  /* Local Address       */else
3959                         arphdr->ar_sip = 0;
3960                 arphdr->ar_tha = 0;             /* dst HW DLCI - Doesn't matter */
3961                 arphdr->ar_tip = 0;             /* Remote Address -- what we want */
3962
3963                 err = fr_send(card, chan->dlci, 0, sizeof(arphdr_1490_t) + sizeof(arphdr_fr_t),
3964                                         (void *)ArpPacket);
3965
3966                 if (!err){
3967                         printk(KERN_INFO "\n%s: Sending InARP request on DLCI %d.\n", 
3968                                 card->devname, chan->dlci);
3969                         clear_bit(ARP_CRIT,&card->wandev.critical);
3970                 }
3971
3972                 kfree(ArpPacket);
3973         }else{
3974                 printk(KERN_INFO "%s: INARP ERROR: %s doesn't have a local IP address!\n",
3975                                 card->devname,dev->name);
3976                 return 1;
3977         }
3978
3979         return 0;
3980 }
3981         
3982
3983 /*==============================================================================
3984  * Check packet for ARP Type
3985  */
3986
3987 int is_arp(void *buf)
3988 {
3989         arphdr_1490_t *arphdr = (arphdr_1490_t *)buf;
3990         
3991         if (arphdr->pad   == 0x00  &&
3992             arphdr->NLPID == 0x80  &&
3993             arphdr->PID   == 0x0608) 
3994                 return 1;
3995         else return 0;
3996 }
3997
3998 /*==============================================================================
3999  * Process ARP Packet Type
4000  */
4001
4002 int process_ARP(arphdr_1490_t *ArpPacket, sdla_t *card, struct net_device* dev)
4003 {
4004
4005
4006         arphdr_fr_t *arphdr = (arphdr_fr_t *)(ArpPacket + 1); /* Skip header */
4007         fr_rx_buf_ctl_t* frbuf = card->rxmb;
4008         struct in_device *in_dev;
4009         fr_channel_t *chan = dev->priv;         
4010         
4011         /* Before we transmit ARP packet, we must check 
4012          * to see that we are not currently transmitting a 
4013          * frame (in 'if_send()') and that we are not 
4014          * already in a 'delayed transmit' state. */
4015         if (check_tx_status(card,dev)){
4016                 if (net_ratelimit()){   
4017                         printk(KERN_INFO "%s: Disabling comminication to process ARP\n",
4018                                         card->devname);
4019                 }
4020                 set_bit(ARP_CRIT,&card->wandev.critical);
4021                 return 0;
4022         }
4023
4024         in_dev = dev->ip_ptr;
4025
4026         /* Check that IP addresses exist for our network address */
4027         if (in_dev == NULL || in_dev->ifa_list == NULL) 
4028                 return -1;
4029
4030         switch (ntohs(arphdr->ar_op)) {
4031
4032         case 0x08:  // Inverse ARP request  -- Send Reply, add route.
4033                         
4034                 /* Check for valid Address */
4035                 printk(KERN_INFO "%s: Recvd PtP addr -InArp Req: %u.%u.%u.%u\n", 
4036                         card->devname, NIPQUAD(arphdr->ar_sip));
4037
4038
4039                 /* Check that the network address is the same as ours, only
4040                  * if the netowrk mask is not 255.255.255.255. Otherwise
4041                  * this check would not make sense */
4042
4043                 if (in_dev->ifa_list->ifa_mask != 0xFFFFFFFF && 
4044                     (in_dev->ifa_list->ifa_mask & arphdr->ar_sip) != 
4045                     (in_dev->ifa_list->ifa_mask & in_dev->ifa_list->ifa_local)){
4046                         printk(KERN_INFO 
4047                                 "%s: Invalid PtP address. %u.%u.%u.%u  InARP ignored.\n", 
4048                                         card->devname,NIPQUAD(arphdr->ar_sip));
4049
4050                         printk(KERN_INFO "%s: mask %u.%u.%u.%u\n", 
4051                                 card->devname, NIPQUAD(in_dev->ifa_list->ifa_mask));
4052                                 printk(KERN_INFO "%s: local %u.%u.%u.%u\n", 
4053                                 card->devname,NIPQUAD(in_dev->ifa_list->ifa_local));
4054                         return -1;
4055                 }
4056
4057                 if (in_dev->ifa_list->ifa_local == arphdr->ar_sip){
4058                         printk(KERN_INFO 
4059                                 "%s: Local addr = PtP addr.  InARP ignored.\n", 
4060                                         card->devname);
4061                         return -1;
4062                 }
4063         
4064                 arphdr->ar_op = htons(0x09);    /* InARP Reply */
4065
4066                 /* Set addresses */
4067                 arphdr->ar_tip = arphdr->ar_sip;
4068                 arphdr->ar_sip = in_dev->ifa_list->ifa_local;
4069
4070                 chan->ip_local = in_dev->ifa_list->ifa_local;
4071                 chan->ip_remote = arphdr->ar_sip;
4072
4073                 fr_send(card, frbuf->dlci, 0, frbuf->length, (void *)ArpPacket);
4074
4075                 if (test_bit(ARP_CRIT,&card->wandev.critical)){
4076                         if (net_ratelimit()){   
4077                                 printk(KERN_INFO "%s: ARP Processed Enabling Communication!\n",
4078                                         card->devname);
4079                         }
4080                 }
4081                 clear_bit(ARP_CRIT,&card->wandev.critical);
4082                 
4083                 chan->ip_local = in_dev->ifa_list->ifa_local;
4084                 chan->ip_remote = arphdr->ar_sip;
4085
4086                 /* Add Route Flag */
4087                 /* The route will be added in the polling routine so
4088                    that it is not interrupt context. */
4089
4090                 chan->route_flag = ADD_ROUTE;
4091                 trigger_fr_poll (dev);
4092
4093                 break;
4094
4095         case 0x09:  // Inverse ARP reply
4096
4097                 /* Check for valid Address */
4098                 printk(KERN_INFO "%s: Recvd PtP addr %u.%u.%u.%u -InArp Reply\n", 
4099                                 card->devname, NIPQUAD(arphdr->ar_sip));
4100
4101
4102                 /* Compare network addresses, only if network mask
4103                  * is not 255.255.255.255  It would not make sense
4104                  * to perform this test if the mask was all 1's */
4105
4106                 if (in_dev->ifa_list->ifa_mask != 0xffffffff &&
4107                     (in_dev->ifa_list->ifa_mask & arphdr->ar_sip) != 
4108                         (in_dev->ifa_list->ifa_mask & in_dev->ifa_list->ifa_local)) {
4109
4110                         printk(KERN_INFO "%s: Invalid PtP address.  InARP ignored.\n", 
4111                                         card->devname);
4112                         return -1;
4113                 }
4114
4115                 /* Make sure that the received IP address is not
4116                  * the same as our own local address */
4117                 if (in_dev->ifa_list->ifa_local == arphdr->ar_sip) {
4118                         printk(KERN_INFO "%s: Local addr = PtP addr.  InARP ignored.\n", 
4119                                 card->devname);
4120                         return -1;
4121                 }                       
4122
4123                 chan->ip_local  = in_dev->ifa_list->ifa_local;
4124                 chan->ip_remote = arphdr->ar_sip;
4125
4126                 /* Add Route Flag */
4127                 /* The route will be added in the polling routine so
4128                    that it is not interrupt context. */
4129
4130                 chan->route_flag = ADD_ROUTE;
4131                 chan->inarp = INARP_CONFIGURED;
4132                 trigger_fr_poll(dev);
4133                 
4134                 break;
4135         default:
4136                 break; // ARP's and RARP's -- Shouldn't happen.
4137         }
4138
4139         return 0;       
4140 }
4141
4142
4143 /*============================================================
4144  * trigger_fr_arp
4145  *
4146  * Description:
4147  *      Add an fr_arp() task into a arp
4148  *      timer handler for a specific dlci/interface.  
4149  *      This will kick the fr_arp() routine 
4150  *      within the specified time interval. 
4151  *
4152  * Usage:
4153  *      This timer is used to send ARP requests at
4154  *      certain time intervals. 
4155  *      Called by an interrupt to request an action
4156  *      at a later date.
4157  */     
4158
4159 static void trigger_fr_arp(struct net_device *dev)
4160 {
4161         fr_channel_t* chan = dev->priv;
4162
4163         mod_timer(&chan->fr_arp_timer, jiffies + chan->inarp_interval * HZ);
4164         return;
4165 }
4166
4167
4168
4169 /*==============================================================================
4170  * ARP Request Action
4171  *
4172  *      This funciton is called by timer interrupt to send an arp request
4173  *      to the remote end.
4174  */
4175
4176 static void fr_arp (unsigned long data)
4177 {
4178         struct net_device *dev = (struct net_device *)data;
4179         fr_channel_t *chan = dev->priv;
4180         volatile sdla_t *card = chan->card;
4181         fr508_flags_t* flags = card->flags;
4182
4183         /* Send ARP packets for all devs' until
4184          * ARP state changes to CONFIGURED */
4185
4186         if (chan->inarp == INARP_REQUEST &&
4187             chan->common.state == WAN_CONNECTED && 
4188             card->wandev.state == WAN_CONNECTED){
4189                 set_bit(0,&chan->inarp_ready);
4190                 card->u.f.timer_int_enabled |= TMR_INT_ENABLED_ARP;
4191                 flags->imask |= FR_INTR_TIMER;  
4192         }
4193  
4194         return;
4195 }
4196         
4197
4198 /*==============================================================================
4199  * Perform the Interrupt Test by running the READ_CODE_VERSION command MAX_INTR_
4200  * TEST_COUNTER times.
4201  */
4202 static int intr_test( sdla_t* card )
4203 {
4204         fr_mbox_t* mb = card->mbox;
4205         int err,i;
4206
4207         err = fr_set_intr_mode(card, FR_INTR_READY, card->wandev.mtu, 0 );
4208         
4209         if (err == CMD_OK) {
4210
4211                 for ( i = 0; i < MAX_INTR_TEST_COUNTER; i++ ) {
4212                         /* Run command READ_CODE_VERSION */
4213                         mb->cmd.length  = 0;
4214                         mb->cmd.command = FR_READ_CODE_VERSION;
4215                         err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
4216                         if (err != CMD_OK) 
4217                                 fr_event(card, err, mb);
4218                 }
4219         
4220         } else {
4221                 return err;     
4222         }
4223
4224         err = fr_set_intr_mode( card, 0, card->wandev.mtu, 0 );
4225
4226         if( err != CMD_OK ) 
4227                 return err;
4228
4229         return 0;
4230 }
4231
4232 /*==============================================================================
4233  * Determine what type of UDP call it is. FPIPE8ND ?
4234  */
4235 static int udp_pkt_type( struct sk_buff *skb, sdla_t* card )
4236 {
4237         fr_udp_pkt_t *fr_udp_pkt = (fr_udp_pkt_t *)skb->data;
4238
4239         /* Quick HACK */
4240         
4241         
4242         if((fr_udp_pkt->ip_pkt.protocol == UDPMGMT_UDP_PROTOCOL) &&
4243                 (fr_udp_pkt->ip_pkt.ver_inet_hdr_length == 0x45) &&
4244                 (fr_udp_pkt->udp_pkt.udp_dst_port == 
4245                 ntohs(card->wandev.udp_port)) &&
4246                 (fr_udp_pkt->wp_mgmt.request_reply == 
4247                 UDPMGMT_REQUEST)) {
4248                         if(!strncmp(fr_udp_pkt->wp_mgmt.signature,
4249                                 UDPMGMT_FPIPE_SIGNATURE, 8)){
4250                                 return UDP_FPIPE_TYPE;
4251                         }
4252         }
4253         return UDP_INVALID_TYPE;
4254 }
4255
4256
4257 /*==============================================================================
4258  * Initializes the Statistics values in the fr_channel structure.
4259  */
4260 void init_chan_statistics( fr_channel_t* chan)
4261 {
4262         memset(&chan->drvstats_if_send.if_send_entry, 0,
4263                 sizeof(if_send_stat_t));
4264         memset(&chan->drvstats_rx_intr.rx_intr_no_socket, 0,
4265                 sizeof(rx_intr_stat_t));
4266         memset(&chan->drvstats_gen.UDP_PIPE_mgmt_kmalloc_err, 0,
4267                 sizeof(pipe_mgmt_stat_t));
4268 }
4269         
4270 /*==============================================================================
4271  * Initializes the Statistics values in the Sdla_t structure.
4272  */
4273 void init_global_statistics( sdla_t* card )
4274 {
4275         /* Intialize global statistics for a card */
4276         memset(&card->statistics.isr_entry, 0, sizeof(global_stats_t));
4277 }
4278
4279 static void read_DLCI_IB_mapping( sdla_t* card, fr_channel_t* chan )
4280 {
4281         fr_mbox_t* mbox = card->mbox;
4282         int retry = MAX_CMD_RETRY;      
4283         dlci_IB_mapping_t* result; 
4284         int err, counter, found;        
4285
4286         do {
4287                 mbox->cmd.command = FR_READ_DLCI_IB_MAPPING;
4288                 mbox->cmd.length = 0;   
4289                 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
4290         } while (err && retry-- && fr_event(card, err, mbox));
4291
4292         if( mbox->cmd.result != 0){
4293                 printk(KERN_INFO "%s: Read DLCI IB Mapping failed\n", 
4294                         chan->name);
4295         }
4296
4297         counter = mbox->cmd.length / sizeof(dlci_IB_mapping_t);
4298         result = (void *)mbox->data;
4299         
4300         found = 0;
4301         for (; counter; --counter, ++result) {
4302                 if ( result->dlci == chan->dlci ) {
4303                         chan->IB_addr = result->addr_value;
4304                         if(card->hw.type == SDLA_S514){
4305                                 chan->dlci_int_interface =
4306                                         (void*)(card->hw.dpmbase +
4307                                         chan->IB_addr);
4308                         }else{ 
4309                                 chan->dlci_int_interface = 
4310                                         (void*)(card->hw.dpmbase + 
4311                                         (chan->IB_addr & 0x00001FFF));
4312
4313                         }
4314                         found = 1;
4315                         break;  
4316                 } 
4317         }
4318         if (!found)
4319                 printk( KERN_INFO "%s: DLCI %d not found by IB MAPPING cmd\n", 
4320                 card->devname, chan->dlci);
4321 }
4322
4323
4324
4325 void s508_s514_lock(sdla_t *card, unsigned long *smp_flags)
4326 {
4327         if (card->hw.type != SDLA_S514){
4328
4329                 spin_lock_irqsave(&card->wandev.lock, *smp_flags);
4330         }else{
4331                 spin_lock(&card->u.f.if_send_lock);
4332         }
4333         return;
4334 }
4335
4336
4337 void s508_s514_unlock(sdla_t *card, unsigned long *smp_flags)
4338 {
4339         if (card->hw.type != SDLA_S514){
4340
4341                 spin_unlock_irqrestore (&card->wandev.lock, *smp_flags);
4342         }else{
4343                 spin_unlock(&card->u.f.if_send_lock);
4344         }
4345         return;
4346 }
4347
4348
4349
4350 /*----------------------------------------------------------------------
4351                   RECEIVE INTERRUPT: BOTTOM HALF HANDLERS 
4352  ----------------------------------------------------------------------*/
4353
4354
4355 /*========================================================
4356  * bh_enqueue
4357  *
4358  * Description:
4359  *      Insert a received packet into a circular
4360  *      rx queue.  This packet will be picked up 
4361  *      by fr_bh() and sent up the stack to the
4362  *      user.
4363  *              
4364  * Usage: 
4365  *      This function is called by rx interrupt,
4366  *      in API mode.
4367  *
4368  */
4369
4370 static int bh_enqueue(struct net_device *dev, struct sk_buff *skb)
4371 {
4372         /* Check for full */
4373         fr_channel_t* chan = dev->priv;
4374         sdla_t *card = chan->card;
4375
4376
4377         if (atomic_read(&chan->bh_buff_used) == MAX_BH_BUFF){
4378                 ++card->wandev.stats.rx_dropped;
4379                 dev_kfree_skb_any(skb);
4380                 return 1; 
4381         }
4382
4383         ((bh_data_t *)&chan->bh_head[chan->bh_write])->skb = skb;
4384
4385         if (chan->bh_write == (MAX_BH_BUFF-1)){
4386                 chan->bh_write=0;
4387         }else{
4388                 ++chan->bh_write;
4389         }
4390
4391         atomic_inc(&chan->bh_buff_used);
4392
4393         return 0;
4394 }
4395
4396
4397 /*========================================================
4398  * trigger_fr_bh
4399  *
4400  * Description:
4401  *      Kick the fr_bh() handler
4402  *
4403  * Usage:
4404  *      rx interrupt calls this function during
4405  *      the API mode. 
4406  */
4407
4408 static void trigger_fr_bh (fr_channel_t *chan)
4409 {
4410         if (!test_and_set_bit(0,&chan->tq_working)){
4411                 wanpipe_queue_work(&chan->common.wanpipe_work);
4412         }
4413 }
4414
4415
4416 /*========================================================
4417  * fr_bh
4418  *
4419  * Description:
4420  *      Frame relay receive BH handler. 
4421  *      Dequeue data from the BH circular 
4422  *      buffer and pass it up the API sock.
4423  *              
4424  * Rationale: 
4425  *      This fuction is used to offload the 
4426  *      rx_interrupt during API operation mode.  
4427  *      The fr_bh() function executes for each 
4428  *      dlci/interface.  
4429  * 
4430  *      Once receive interrupt copies data from the
4431  *      card into an skb buffer, the skb buffer
4432  *      is appended to a circular BH buffer.
4433  *      Then the interrupt kicks fr_bh() to finish the
4434  *      job at a later time (not within the interrupt).
4435  *       
4436  * Usage:
4437  *      Interrupts use this to defer a task to 
4438  *      a polling routine.
4439  *
4440  */     
4441
4442 static void fr_bh(struct net_device * dev)
4443 {
4444         fr_channel_t* chan = dev->priv;
4445         sdla_t *card = chan->card;
4446         struct sk_buff *skb;
4447
4448         if (atomic_read(&chan->bh_buff_used) == 0){
4449                 clear_bit(0, &chan->tq_working);
4450                 return;
4451         }
4452
4453         while (atomic_read(&chan->bh_buff_used)){
4454
4455                 if (chan->common.sk == NULL || chan->common.func == NULL){
4456                         clear_bit(0, &chan->tq_working);
4457                         return;
4458                 }
4459
4460                 skb  = ((bh_data_t *)&chan->bh_head[chan->bh_read])->skb;
4461
4462                 if (skb != NULL){
4463
4464                         if (chan->common.sk == NULL || chan->common.func == NULL){
4465                                 ++card->wandev.stats.rx_dropped;
4466                                 ++chan->ifstats.rx_dropped;
4467                                 dev_kfree_skb_any(skb);
4468                                 fr_bh_cleanup(dev);
4469                                 continue;
4470                         }
4471
4472                         if (chan->common.func(skb,dev,chan->common.sk) != 0){
4473                                 /* Sock full cannot send, queue us for
4474                                  * another try */
4475                                 atomic_set(&chan->common.receive_block,1);
4476                                 return;
4477                         }else{
4478                                 fr_bh_cleanup(dev);
4479                         }
4480                 }else{
4481                         fr_bh_cleanup(dev);
4482                 }
4483         }       
4484         clear_bit(0, &chan->tq_working);
4485
4486         return;
4487 }
4488
4489 static int fr_bh_cleanup(struct net_device *dev)
4490 {
4491         fr_channel_t* chan = dev->priv;
4492
4493         ((bh_data_t *)&chan->bh_head[chan->bh_read])->skb = NULL;
4494
4495         if (chan->bh_read == (MAX_BH_BUFF-1)){
4496                 chan->bh_read=0;
4497         }else{
4498                 ++chan->bh_read;        
4499         }
4500
4501         atomic_dec(&chan->bh_buff_used);
4502         return 0;
4503 }
4504
4505
4506 /*----------------------------------------------------------------------
4507                POLL BH HANDLERS AND KICK ROUTINES 
4508  ----------------------------------------------------------------------*/
4509
4510 /*============================================================
4511  * trigger_fr_poll
4512  *
4513  * Description:
4514  *      Add a fr_poll() task into a tq_scheduler bh handler
4515  *      for a specific dlci/interface.  This will kick
4516  *      the fr_poll() routine at a later time. 
4517  *
4518  * Usage:
4519  *      Interrupts use this to defer a taks to 
4520  *      a polling routine.
4521  *
4522  */     
4523 static void trigger_fr_poll(struct net_device *dev)
4524 {
4525         fr_channel_t* chan = dev->priv;
4526         schedule_work(&chan->fr_poll_work);
4527         return;
4528 }
4529
4530
4531 /*============================================================
4532  * fr_poll
4533  *      
4534  * Rationale:
4535  *      We cannot manipulate the routing tables, or
4536  *      ip addresses withing the interrupt. Therefore
4537  *      we must perform such actons outside an interrupt 
4538  *      at a later time. 
4539  *
4540  * Description: 
4541  *      Frame relay polling routine, responsible for 
4542  *      shutting down interfaces upon disconnect
4543  *      and adding/removing routes. 
4544  *      
4545  * Usage:        
4546  *      This function is executed for each frame relay
4547  *      dlci/interface through a tq_schedule bottom half.
4548  *      
4549  *      trigger_fr_poll() function is used to kick
4550  *      the fr_poll routine.  
4551  */
4552
4553 static void fr_poll(struct net_device *dev)
4554 {
4555
4556         fr_channel_t* chan;
4557         sdla_t *card;
4558         u8 check_gateway=0;
4559
4560         if (!dev || (chan = dev->priv) == NULL)
4561                 return;
4562
4563         card = chan->card;
4564         
4565         /* (Re)Configuraiton is in progress, stop what you are 
4566          * doing and get out */
4567         if (test_bit(PERI_CRIT,&card->wandev.critical)){
4568                 return;
4569         }
4570
4571         switch (chan->common.state){
4572
4573         case WAN_DISCONNECTED:
4574
4575                 if (test_bit(DYN_OPT_ON,&chan->interface_down) &&
4576                     !test_bit(DEV_DOWN, &chan->interface_down) &&
4577                     dev->flags&IFF_UP){
4578
4579                         printk(KERN_INFO "%s: Interface %s is Down.\n", 
4580                                 card->devname,dev->name);
4581                         change_dev_flags(dev,dev->flags&~IFF_UP);
4582                         set_bit(DEV_DOWN, &chan->interface_down);
4583                         chan->route_flag = NO_ROUTE;
4584                         
4585                 }else{
4586                         if (chan->inarp != INARP_NONE)
4587                                 process_route(dev);     
4588                 }
4589                 break;
4590
4591         case WAN_CONNECTED:
4592
4593                 if (test_bit(DYN_OPT_ON,&chan->interface_down) &&
4594                     test_bit(DEV_DOWN, &chan->interface_down) &&
4595                     !(dev->flags&IFF_UP)){
4596
4597                         printk(KERN_INFO "%s: Interface %s is Up.\n", 
4598                                         card->devname,dev->name);
4599
4600                         change_dev_flags(dev,dev->flags|IFF_UP);
4601                         clear_bit(DEV_DOWN, &chan->interface_down);
4602                         check_gateway=1;
4603                 }
4604
4605                 if (chan->inarp != INARP_NONE){
4606                         process_route(dev);
4607                         check_gateway=1;
4608                 }
4609
4610                 if (chan->gateway && check_gateway)
4611                         add_gateway(card,dev);
4612
4613                 break;
4614
4615         }
4616
4617         return; 
4618 }
4619
4620 /*==============================================================
4621  * check_tx_status
4622  *
4623  * Rationale:
4624  *      We cannot transmit from an interrupt while
4625  *      the if_send is transmitting data.  Therefore,
4626  *      we must check whether the tx buffers are
4627  *      begin used, before we transmit from an
4628  *      interrupt.      
4629  * 
4630  * Description: 
4631  *      Checks whether it's safe to use the transmit 
4632  *      buffers. 
4633  *
4634  * Usage:
4635  *      ARP and UDP handling routines use this function
4636  *      because, they need to transmit data during
4637  *      an interrupt.
4638  */
4639
4640 static int check_tx_status(sdla_t *card, struct net_device *dev)
4641 {
4642
4643         if (card->hw.type == SDLA_S514){
4644                 if (test_bit(SEND_CRIT, (void*)&card->wandev.critical) ||
4645                         test_bit(SEND_TXIRQ_CRIT, (void*)&card->wandev.critical)) {
4646                         return 1;
4647                 }
4648         }
4649
4650         if (netif_queue_stopped(dev) || (card->u.f.tx_interrupts_pending))
4651                 return 1; 
4652
4653         return 0;
4654 }
4655
4656 /*===============================================================
4657  * move_dev_to_next
4658  *  
4659  * Description:
4660  *      Move the dev pointer to the next location in the
4661  *      link list.  Check if we are at the end of the 
4662  *      list, if so start from the begining.
4663  *
4664  * Usage:
4665  *      Timer interrupt uses this function to efficiently
4666  *      step through the devices that need to send ARP data.
4667  *
4668  */
4669
4670 struct net_device *move_dev_to_next(sdla_t *card, struct net_device *dev)
4671 {
4672         if (card->wandev.new_if_cnt != 1){
4673                 if (!*((struct net_device **)dev->priv))
4674                         return card->wandev.dev;
4675                 else
4676                         return *((struct net_device **)dev->priv);
4677         }
4678         return dev;
4679 }
4680
4681 /*==============================================================
4682  * trigger_config_fr
4683  *
4684  * Rationale:
4685  *      All commands must be performed inside of a  
4686  *      interrupt.   
4687  *
4688  * Description:
4689  *      Kick the config_fr() routine throught the
4690  *      timer interrupt.
4691  */
4692
4693
4694 static void trigger_config_fr (sdla_t *card)
4695 {
4696         fr508_flags_t* flags = card->flags;
4697
4698         card->u.f.timer_int_enabled |= TMR_INT_ENABLED_CONFIG;
4699         flags->imask |= FR_INTR_TIMER;
4700 }
4701
4702
4703 /*==============================================================
4704  * config_fr
4705  *
4706  * Rationale:
4707  *      All commands must be performed inside of a  
4708  *      interrupt.  
4709  &
4710  * Description: 
4711  *      Configure a DLCI. This function is executed
4712  *      by a timer_interrupt.  The if_open() function
4713  *      triggers it.
4714  *
4715  * Usage:
4716  *      new_if() collects all data necessary to
4717  *      configure the DLCI. It sets the chan->dlci_ready 
4718  *      bit.  When the if_open() function is executed
4719  *      it checks this bit, and if its set it triggers
4720  *      the timer interrupt to execute the config_fr()
4721  *      function.
4722  */
4723
4724 static void config_fr (sdla_t *card)
4725 {
4726         struct net_device *dev;
4727         fr_channel_t *chan;
4728
4729         for (dev = card->wandev.dev; dev;
4730              dev = *((struct net_device **)dev->priv)) {
4731         
4732                 if ((chan=dev->priv) == NULL)
4733                         continue;
4734                 
4735                 if (!test_bit(0,&chan->config_dlci))
4736                         continue;
4737
4738                 clear_bit(0,&chan->config_dlci);
4739
4740                 /* If signalling is set to NO, then setup 
4741                  * DLCI addresses right away.  Don't have to wait for
4742                  * link to connect. 
4743                  */
4744                 if (card->wandev.signalling == WANOPT_NO){
4745                         printk(KERN_INFO "%s: Signalling set to NO: Mapping DLCI's\n",
4746                                         card->wandev.name);
4747                         if (fr_init_dlci(card,chan)){
4748                                 printk(KERN_INFO "%s: ERROR: Failed to configure DLCI %i !\n",
4749                                         card->devname, chan->dlci);
4750                                 return;
4751                         }
4752                 }
4753
4754                 if (card->wandev.station == WANOPT_CPE) {
4755         
4756                         update_chan_state(dev); 
4757                         
4758                         /* CPE: issue full status enquiry */
4759                         fr_issue_isf(card, FR_ISF_FSE);
4760
4761                 } else {        
4762                         /* FR switch: activate DLCI(s) */
4763         
4764                         /* For Switch emulation we have to ADD and ACTIVATE
4765                          * the DLCI(s) that were configured with the SET_DLCI_
4766                          * CONFIGURATION command. Add and Activate will fail if
4767                          * DLCI specified is not included in the list.
4768                          *
4769                          * Also If_open is called once for each interface. But
4770                          * it does not get in here for all the interface. So
4771                          * we have to pass the entire list of DLCI(s) to add 
4772                          * activate routines.  
4773                          */ 
4774                         
4775                         if (!check_dlci_config (card, chan)){
4776                                 fr_add_dlci(card, chan->dlci);
4777                                 fr_activate_dlci(card, chan->dlci);
4778                         }
4779                 }
4780
4781                 card->u.f.dlci_to_dev_map[chan->dlci] = dev;
4782         }
4783         return;
4784 }
4785
4786
4787 /*==============================================================
4788  * config_fr
4789  *
4790  * Rationale:
4791  *      All commands must be executed during an interrupt.
4792  * 
4793  * Description: 
4794  *      Trigger uncofig_fr() function through 
4795  *      the timer interrupt.
4796  *
4797  */
4798
4799 static void trigger_unconfig_fr(struct net_device *dev)
4800 {
4801         fr_channel_t *chan = dev->priv;
4802         volatile sdla_t *card = chan->card;
4803         unsigned long timeout;
4804         fr508_flags_t* flags = card->flags;
4805         int reset_critical=0;
4806         
4807         if (test_bit(PERI_CRIT,(void*)&card->wandev.critical)){
4808                 clear_bit(PERI_CRIT,(void*)&card->wandev.critical);
4809                 reset_critical=1;
4810         }
4811                 
4812         /* run unconfig_dlci() function 
4813          * throught the timer interrupt */
4814         set_bit(0,(void*)&chan->unconfig_dlci);
4815         card->u.f.timer_int_enabled |= TMR_INT_ENABLED_UNCONFIG;
4816         flags->imask |= FR_INTR_TIMER;
4817
4818         /* Wait for the command to complete */
4819         timeout = jiffies;
4820         for(;;) {
4821
4822                 if(!(card->u.f.timer_int_enabled & TMR_INT_ENABLED_UNCONFIG))
4823                         break;
4824
4825                 if (time_after(jiffies, timeout + 1 * HZ)){
4826                         card->u.f.timer_int_enabled &= ~TMR_INT_ENABLED_UNCONFIG;
4827                         printk(KERN_INFO "%s: Failed to delete DLCI %i\n",
4828                                 card->devname,chan->dlci);
4829                         break;
4830                 }
4831         }
4832
4833         if (reset_critical){
4834                 set_bit(PERI_CRIT,(void*)&card->wandev.critical);
4835         }
4836 }
4837
4838 /*==============================================================
4839  * unconfig_fr
4840  *
4841  * Rationale:
4842  *      All commands must be executed during an interrupt.
4843  * 
4844  * Description: 
4845  *      Remove the dlci from firmware.
4846  *      This funciton is used in NODE shutdown.
4847  */
4848
4849 static void unconfig_fr (sdla_t *card)
4850 {
4851         struct net_device *dev;
4852         fr_channel_t *chan;
4853
4854         for (dev = card->wandev.dev; dev;
4855              dev = *((struct net_device **)dev->priv)){
4856         
4857                 if ((chan=dev->priv) == NULL)
4858                         continue;
4859                 
4860                 if (!test_bit(0,&chan->unconfig_dlci))
4861                         continue;
4862
4863                 clear_bit(0,&chan->unconfig_dlci);
4864
4865                 if (card->wandev.station == WANOPT_NODE){
4866                         printk(KERN_INFO "%s: Unconfiguring DLCI %i\n",
4867                                         card->devname,chan->dlci);
4868                         fr_delete_dlci(card,chan->dlci);
4869                 }
4870                 card->u.f.dlci_to_dev_map[chan->dlci] = NULL;
4871         }
4872 }
4873
4874 static int setup_fr_header(struct sk_buff **skb_orig, struct net_device* dev,
4875                            char op_mode)
4876 {
4877         struct sk_buff *skb = *skb_orig;
4878         fr_channel_t *chan=dev->priv;
4879
4880         if (op_mode == WANPIPE){
4881
4882                 chan->fr_header[0]=Q922_UI;
4883                 
4884                 switch (htons(skb->protocol)){
4885                         
4886                 case ETH_P_IP:
4887                         chan->fr_header[1]=NLPID_IP;
4888                         break;
4889                 default:
4890                         return -EINVAL;
4891                 }
4892                         
4893                 return 2;
4894         }
4895
4896         /* If we are in bridging mode, we must apply
4897          * an Ethernet header */
4898         if (op_mode == BRIDGE || op_mode == BRIDGE_NODE){
4899
4900
4901                 /* Encapsulate the packet as a bridged Ethernet frame. */
4902 #ifdef DEBUG
4903                 printk(KERN_INFO "%s: encapsulating skb for frame relay\n", 
4904                         dev->name);
4905 #endif
4906                 
4907                 chan->fr_header[0] = 0x03;
4908                 chan->fr_header[1] = 0x00;
4909                 chan->fr_header[2] = 0x80;
4910                 chan->fr_header[3] = 0x00;
4911                 chan->fr_header[4] = 0x80;
4912                 chan->fr_header[5] = 0xC2;
4913                 chan->fr_header[6] = 0x00;
4914                 chan->fr_header[7] = 0x07;
4915
4916                 /* Yuck. */
4917                 skb->protocol = ETH_P_802_3;
4918                 return 8;
4919
4920         }
4921                 
4922         return 0;
4923 }
4924
4925
4926 static int check_dlci_config (sdla_t *card, fr_channel_t *chan)
4927 {
4928         fr_mbox_t* mbox = card->mbox;
4929         int err=0;
4930         fr_conf_t *conf=NULL;
4931         unsigned short dlci_num = chan->dlci;
4932         int dlci_offset=0;
4933         struct net_device *dev = NULL;
4934         
4935         mbox->cmd.command = FR_READ_CONFIG;
4936         mbox->cmd.length = 0;
4937         mbox->cmd.dlci = dlci_num;      
4938
4939         err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
4940         
4941         if (err == CMD_OK){
4942                 return 0;
4943         }
4944
4945         for (dev = card->wandev.dev; dev;
4946              dev=*((struct net_device **)dev->priv))
4947                 set_chan_state(dev,WAN_DISCONNECTED);
4948         
4949         printk(KERN_INFO "DLCI %i Not configured, configuring\n",dlci_num);
4950         
4951         mbox->cmd.command = FR_COMM_DISABLE;
4952         mbox->cmd.length = 0;
4953         err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
4954         if (err != CMD_OK){
4955                 fr_event(card, err, mbox);
4956                 return 2;
4957         }
4958
4959         printk(KERN_INFO "Disabled Communications \n");
4960         
4961         mbox->cmd.command = FR_READ_CONFIG;
4962         mbox->cmd.length = 0;
4963         mbox->cmd.dlci = 0;     
4964
4965         err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
4966         
4967         if (err != CMD_OK){
4968                 fr_event(card, err, mbox);
4969                 return 2;
4970         }
4971         
4972         conf = (fr_conf_t *)mbox->data;
4973
4974         dlci_offset=0;
4975         for (dev = card->wandev.dev; dev;
4976              dev = *((struct net_device **)dev->priv)) {
4977                 fr_channel_t *chan_tmp = dev->priv;
4978                 conf->dlci[dlci_offset] = chan_tmp->dlci;               
4979                 dlci_offset++;
4980         }
4981         
4982         printk(KERN_INFO "Got Fr configuration Buffer Length is %x Dlci %i Dlci Off %i\n",
4983                 mbox->cmd.length,
4984                 mbox->cmd.length > 0x20 ? conf->dlci[0] : -1, 
4985                 dlci_offset );
4986         
4987         mbox->cmd.length = 0x20 + dlci_offset*2;
4988
4989         mbox->cmd.command = FR_SET_CONFIG;
4990         mbox->cmd.dlci = 0; 
4991
4992         err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
4993
4994         if (err != CMD_OK){
4995                 fr_event(card, err, mbox);
4996                 return 2;
4997         }
4998
4999         initialize_rx_tx_buffers (card);
5000
5001         
5002         printk(KERN_INFO "Configuraiton Succeded for new DLCI %i\n",dlci_num);
5003
5004         if (fr_comm_enable (card)){
5005                 return 2;
5006         }
5007
5008         printk(KERN_INFO "Enabling Communications \n");
5009
5010         for (dev = card->wandev.dev; dev;
5011              dev = *((struct net_device **)dev->priv)) {
5012                 fr_channel_t *chan_tmp = dev->priv;
5013                 fr_init_dlci(card,chan_tmp);
5014                 fr_add_dlci(card, chan_tmp->dlci);
5015                 fr_activate_dlci(card, chan_tmp->dlci);
5016         }
5017
5018         printk(KERN_INFO "END OF CONFIGURAITON %i\n",dlci_num);
5019         
5020         return 1;
5021 }
5022
5023 static void initialize_rx_tx_buffers (sdla_t *card)
5024 {
5025         fr_buf_info_t* buf_info;
5026         
5027         if (card->hw.type == SDLA_S514) {
5028         
5029                 buf_info = (void*)(card->hw.dpmbase + FR_MB_VECTOR +
5030                         FR508_RXBC_OFFS);
5031
5032                 card->rxmb = (void*)(buf_info->rse_next + card->hw.dpmbase);
5033
5034                 card->u.f.rxmb_base =
5035                         (void*)(buf_info->rse_base + card->hw.dpmbase); 
5036
5037                 card->u.f.rxmb_last =
5038                         (void*)(buf_info->rse_base +
5039                         (buf_info->rse_num - 1) * sizeof(fr_rx_buf_ctl_t) +
5040                         card->hw.dpmbase);
5041         }else{  
5042                 buf_info = (void*)(card->hw.dpmbase + FR508_RXBC_OFFS);
5043
5044                 card->rxmb = (void*)(buf_info->rse_next -
5045                         FR_MB_VECTOR + card->hw.dpmbase);
5046                 
5047                 card->u.f.rxmb_base =
5048                         (void*)(buf_info->rse_base -
5049                         FR_MB_VECTOR + card->hw.dpmbase);
5050                 
5051                 card->u.f.rxmb_last =
5052                         (void*)(buf_info->rse_base +
5053                         (buf_info->rse_num - 1) * sizeof(fr_rx_buf_ctl_t) -
5054                         FR_MB_VECTOR + card->hw.dpmbase);
5055         }
5056
5057         card->u.f.rx_base = buf_info->buf_base;
5058         card->u.f.rx_top  = buf_info->buf_top;
5059
5060         card->u.f.tx_interrupts_pending = 0;
5061
5062         return;
5063 }
5064
5065         
5066
5067 MODULE_LICENSE("GPL");
5068
5069 /****** End *****************************************************************/