📄 sdla_chdlc.c
字号:
/****************************************************************************** sdla_chdlc.c WANPIPE(tm) Multiprotocol WAN Link Driver. Cisco HDLC module.** Authors: Nenad Corbic <ncorbic@sangoma.com>* Gideon Hack ** Copyright: (c) 1995-2001 Sangoma Technologies Inc.** This program is free software; you can redistribute it and/or* modify it under the terms of the GNU General Public License* as published by the Free Software Foundation; either version* 2 of the License, or (at your option) any later version.* ============================================================================* Feb 28, 2001 Nenad Corbic Updated if_tx_timeout() routine for * 2.4.X kernels.* Jan 25, 2001 Nenad Corbic Added a TTY Sync serial driver over the* HDLC streaming protocol* Added a TTY Async serial driver over the* Async protocol.* Dec 15, 2000 Nenad Corbic Updated for 2.4.X Kernel support* Nov 13, 2000 Nenad Corbic Added true interface type encoding option.* Tcpdump doesn't support CHDLC inteface* types, to fix this "true type" option will set* the interface type to RAW IP mode.* Nov 07, 2000 Nenad Corbic Added security features for UDP debugging:* Deny all and specify allowed requests.* Jun 20, 2000 Nenad Corbic Fixed the API IP ERROR bug. Caused by the * latest update.* May 09, 2000 Nenad Corbic Option to bring down an interface* upon disconnect.* Mar 23, 2000 Nenad Corbic Improved task queue, bh handling.* Mar 16, 2000 Nenad Corbic Fixed the SLARP Dynamic IP addressing.* Mar 06, 2000 Nenad Corbic Bug Fix: corrupted mbox recovery.* Feb 10, 2000 Gideon Hack Added ASYNC support.* Feb 09, 2000 Nenad Corbic Fixed two shutdown bugs in update() and* if_stats() functions.* Jan 24, 2000 Nenad Corbic Fixed a startup wanpipe state racing, * condition between if_open and isr. * Jan 10, 2000 Nenad Corbic Added new socket API support.* Dev 15, 1999 Nenad Corbic Fixed up header files for 2.0.X kernels* Nov 20, 1999 Nenad Corbic Fixed zero length API bug.* Sep 30, 1999 Nenad Corbic Fixed dynamic IP and route setup.* Sep 23, 1999 Nenad Corbic Added SMP support, fixed tracing * Sep 13, 1999 Nenad Corbic Split up Port 0 and 1 into separate devices.* Jun 02, 1999 Gideon Hack Added support for the S514 adapter.* Oct 30, 1998 Jaspreet Singh Added Support for CHDLC API (HDLC STREAMING).* Oct 28, 1998 Jaspreet Singh Added Support for Dual Port CHDLC.* Aug 07, 1998 David Fong Initial version.*****************************************************************************/#include <linux/module.h>#include <linux/kernel.h> /* printk(), and other useful stuff */#include <linux/stddef.h> /* offsetof(), etc. */#include <linux/errno.h> /* return codes */#include <linux/string.h> /* inline memset(), etc. */#include <linux/slab.h> /* kmalloc(), kfree() */#include <linux/wanrouter.h> /* WAN router definitions */#include <linux/wanpipe.h> /* WANPIPE common user API definitions */#include <linux/if_arp.h> /* ARPHRD_* defines */#include <asm/uaccess.h>#include <linux/inetdevice.h>#include <linux/netdevice.h>#include <linux/in.h> /* sockaddr_in */#include <linux/inet.h> #include <linux/if.h>#include <asm/byteorder.h> /* htons(), etc. */#include <linux/sdlapci.h>#include <asm/io.h>#include <linux/sdla_chdlc.h> /* CHDLC firmware API definitions */#include <linux/sdla_asy.h> /* CHDLC (async) API definitions */#include <linux/if_wanpipe_common.h> /* Socket Driver common area */#include <linux/if_wanpipe.h> /* TTY Includes */#include <linux/tty.h>#include <linux/tty_flip.h>#include <linux/serial.h>/****** Defines & Macros ****************************************************//* reasons for enabling the timer interrupt on the adapter */#define TMR_INT_ENABLED_UDP 0x01#define TMR_INT_ENABLED_UPDATE 0x02#define TMR_INT_ENABLED_CONFIG 0x10#define MAX_IP_ERRORS 10#define TTY_CHDLC_MAX_MTU 2000#define CHDLC_DFLT_DATA_LEN 1500 /* default MTU */#define CHDLC_HDR_LEN 1#define CHDLC_API 0x01#define PORT(x) (x == 0 ? "PRIMARY" : "SECONDARY" )#define MAX_BH_BUFF 10//#define PRINT_DEBUG#ifdef PRINT_DEBUG#define dbg_printk(format, a...) printk(format, ## a)#else#define dbg_printk(format, a...)#endif /******Data Structures*****************************************************//* This structure is placed in the private data area of the device structure. * The card structure used to occupy the private area but now the following * structure will incorporate the card structure along with CHDLC specific data */typedef struct chdlc_private_area{ wanpipe_common_t common; sdla_t *card; int TracingEnabled; /* For enabling Tracing */ unsigned long curr_trace_addr; /* Used for Tracing */ unsigned long start_trace_addr; unsigned long end_trace_addr; unsigned long base_addr_trace_buffer; unsigned long end_addr_trace_buffer; unsigned short number_trace_elements; unsigned available_buffer_space; unsigned long router_start_time; unsigned char route_status; unsigned char route_removed; unsigned long tick_counter; /* For 5s timeout counter */ unsigned long router_up_time; u32 IP_address; /* IP addressing */ u32 IP_netmask; u32 ip_local; u32 ip_remote; u32 ip_local_tmp; u32 ip_remote_tmp; u8 ip_error; u8 config_chdlc; u8 config_chdlc_timeout; unsigned char mc; /* Mulitcast support on/off */ unsigned short udp_pkt_lgth; /* udp packet processing */ char udp_pkt_src; char udp_pkt_data[MAX_LGTH_UDP_MGNT_PKT]; unsigned short timer_int_enabled; char update_comms_stats; /* updating comms stats */ bh_data_t *bh_head; /* Circular buffer for chdlc_bh */ unsigned long tq_working; volatile int bh_write; volatile int bh_read; atomic_t bh_buff_used; unsigned char interface_down; /* Polling work queue entry. Each interface * has its own work queue entry, which is used * to defer events from the interrupt */ struct work_struct poll_work; struct timer_list poll_delay_timer; u8 gateway; u8 true_if_encoding; //FIXME: add driver stats as per frame relay!} chdlc_private_area_t;/* Route Status options */#define NO_ROUTE 0x00#define ADD_ROUTE 0x01#define ROUTE_ADDED 0x02#define REMOVE_ROUTE 0x03/* variable for keeping track of enabling/disabling FT1 monitor status */static int rCount = 0;/* variable for tracking how many interfaces to open for WANPIPE on the two ports */extern void disable_irq(unsigned int);extern void enable_irq(unsigned int);/****** Function Prototypes *************************************************//* WAN link driver entry points. These are called by the WAN router module. */static int update(struct wan_device* wandev);static int new_if(struct wan_device* wandev, struct net_device* dev, wanif_conf_t* conf);/* Network device interface */static int if_init(struct net_device* dev);static int if_open(struct net_device* dev);static int if_close(struct net_device* dev);static int if_header(struct sk_buff* skb, struct net_device* dev, unsigned short type, void* daddr, void* saddr, unsigned len);static int if_rebuild_hdr (struct sk_buff *skb);static struct net_device_stats* if_stats(struct net_device* dev); static int if_send(struct sk_buff* skb, struct net_device* dev);/* CHDLC Firmware interface functions */static int chdlc_configure (sdla_t* card, void* data);static int chdlc_comm_enable (sdla_t* card);static int chdlc_read_version (sdla_t* card, char* str);static int chdlc_set_intr_mode (sdla_t* card, unsigned mode);static int chdlc_send (sdla_t* card, void* data, unsigned len);static int chdlc_read_comm_err_stats (sdla_t* card);static int chdlc_read_op_stats (sdla_t* card);static int chdlc_error (sdla_t *card, int err, CHDLC_MAILBOX_STRUCT *mb);static int chdlc_disable_comm_shutdown (sdla_t *card);static void if_tx_timeout(struct net_device *dev);/* Miscellaneous CHDLC Functions */static int set_chdlc_config (sdla_t* card);static void init_chdlc_tx_rx_buff( sdla_t* card);static int process_chdlc_exception(sdla_t *card);static int process_global_exception(sdla_t *card);static int update_comms_stats(sdla_t* card, chdlc_private_area_t* chdlc_priv_area);static int configure_ip (sdla_t* card);static int unconfigure_ip (sdla_t* card);static void process_route(sdla_t *card);static void port_set_state (sdla_t *card, int);static int config_chdlc (sdla_t *card);static void disable_comm (sdla_t *card);static void trigger_chdlc_poll(struct net_device *dev);static void chdlc_poll(struct net_device *dev);static void chdlc_poll_delay (unsigned long dev_ptr);/* Miscellaneous asynchronous interface Functions */static int set_asy_config (sdla_t* card);static int asy_comm_enable (sdla_t* card);/* Interrupt handlers */static void wpc_isr (sdla_t* card);static void rx_intr (sdla_t* card);static void timer_intr(sdla_t *);/* Bottom half handlers */static void chdlc_work(struct net_device *dev);static int chdlc_work_cleanup(struct net_device *dev);static int bh_enqueue(struct net_device *dev, struct sk_buff *skb);/* Miscellaneous functions */static int chk_bcast_mcast_addr(sdla_t* card, struct net_device* dev, struct sk_buff *skb);static int reply_udp( unsigned char *data, unsigned int mbox_len );static int intr_test( sdla_t* card);static int udp_pkt_type( struct sk_buff *skb , sdla_t* card);static int store_udp_mgmt_pkt(char udp_pkt_src, sdla_t* card, struct sk_buff *skb, struct net_device* dev, chdlc_private_area_t* chdlc_priv_area);static int process_udp_mgmt_pkt(sdla_t* card, struct net_device* dev, chdlc_private_area_t* chdlc_priv_area);static unsigned short calc_checksum (char *, int);static void s508_lock (sdla_t *card, unsigned long *smp_flags);static void s508_unlock (sdla_t *card, unsigned long *smp_flags);static int Intr_test_counter;/* TTY Global Definitions */#define NR_PORTS 4#define WAN_TTY_MAJOR 226#define WAN_TTY_MINOR 0#define WAN_CARD(port) (tty_card_map[port])#define MIN_PORT 0#define MAX_PORT NR_PORTS-1 #define CRC_LENGTH 2static int wanpipe_tty_init(sdla_t *card);static void wanpipe_tty_receive(sdla_t *, unsigned, unsigned int);static void wanpipe_tty_trigger_poll(sdla_t *card);static struct tty_driver serial_driver;static int tty_init_cnt=0;static struct serial_state rs_table[NR_PORTS];static char tty_driver_mode=WANOPT_TTY_SYNC;static char *opt_decode[] = {"NONE","CRTSCTS","XONXOFF-RX", "CRTSCTS XONXOFF-RX","XONXOFF-TX", "CRTSCTS XONXOFF-TX","CRTSCTS XONXOFF"};static char *p_decode[] = {"NONE","ODD","EVEN"};static void* tty_card_map[NR_PORTS] = {NULL,NULL,NULL,NULL};/****** Public Functions ****************************************************//*============================================================================ * Cisco HDLC protocol initialization routine. * * This routine is called by the main WANPIPE module during setup. At this * point adapter is completely initialized and firmware is running. * o read firmware version (to make sure it's alive) * o configure adapter * o initialize protocol-specific fields of the adapter data space. * * Return: 0 o.k. * < 0 failure. */int wpc_init (sdla_t* card, wandev_conf_t* conf){ unsigned char port_num; int err; unsigned long max_permitted_baud = 0; SHARED_MEMORY_INFO_STRUCT *flags; union { char str[80]; } u; volatile CHDLC_MAILBOX_STRUCT* mb; CHDLC_MAILBOX_STRUCT* mb1; unsigned long timeout; /* Verify configuration ID */ if (conf->config_id != WANCONFIG_CHDLC) { printk(KERN_INFO "%s: invalid configuration ID %u!\n", card->devname, conf->config_id); return -EINVAL; } /* Find out which Port to use */ if ((conf->comm_port == WANOPT_PRI) || (conf->comm_port == WANOPT_SEC)){ if (card->next){ if (conf->comm_port != card->next->u.c.comm_port){ card->u.c.comm_port = conf->comm_port; }else{ printk(KERN_INFO "%s: ERROR - %s port used!\n", card->wandev.name, PORT(conf->comm_port)); return -EINVAL; } }else{ card->u.c.comm_port = conf->comm_port; } }else{ printk(KERN_INFO "%s: ERROR - Invalid Port Selected!\n", card->wandev.name); return -EINVAL; } /* Initialize protocol-specific fields */ if(card->hw.type != SDLA_S514){ if (card->u.c.comm_port == WANOPT_PRI){ card->mbox = (void *) card->hw.dpmbase; }else{ card->mbox = (void *) card->hw.dpmbase + SEC_BASE_ADDR_MB_STRUCT - PRI_BASE_ADDR_MB_STRUCT; } }else{ /* for a S514 adapter, set a pointer to the actual mailbox in the */ /* allocated virtual memory area */ if (card->u.c.comm_port == WANOPT_PRI){ card->mbox = (void *) card->hw.dpmbase + PRI_BASE_ADDR_MB_STRUCT; }else{ card->mbox = (void *) card->hw.dpmbase + SEC_BASE_ADDR_MB_STRUCT; } } mb = mb1 = card->mbox; if (!card->configured){ /* The board will place an 'I' in the return code to indicate that it is ready to accept commands. We expect this to be completed in less than 1 second. */ timeout = jiffies; while (mb->return_code != 'I') /* Wait 1s for board to initialize */ if ((jiffies - timeout) > 1*HZ) break; if (mb->return_code != 'I') { printk(KERN_INFO "%s: Initialization not completed by adapter\n", card->devname); printk(KERN_INFO "Please contact Sangoma representative.\n"); return -EIO; } } /* Read firmware version. Note that when adapter initializes, it * clears the mailbox, so it may appear that the first command was * executed successfully when in fact it was merely erased. To work * around this, we execute the first command twice. */ if (chdlc_read_version(card, u.str)) return -EIO; printk(KERN_INFO "%s: Running Cisco HDLC firmware v%s\n", card->devname, u.str); card->isr = &wpc_isr; card->poll = NULL; card->exec = NULL; card->wandev.update = &update; card->wandev.new_if = &new_if; card->wandev.del_if = NULL; card->wandev.udp_port = conf->udp_port; card->disable_comm = &disable_comm; card->wandev.new_if_cnt = 0; /* reset the number of times the 'update()' proc has been called */ card->u.c.update_call_count = 0; card->wandev.ttl = conf->ttl; card->wandev.interface = conf->interface; if ((card->u.c.comm_port == WANOPT_SEC && conf->interface == WANOPT_V35)&& card->hw.type != SDLA_S514){ printk(KERN_INFO "%s: ERROR - V35 Interface not supported on S508 %s port \n", card->devname, PORT(card->u.c.comm_port)); return -EIO; } card->wandev.clocking = conf->clocking; port_num = card->u.c.comm_port; /* in API mode, we can configure for "receive only" buffering */ if(card->hw.type == SDLA_S514) { card->u.c.receive_only = conf->receive_only; if(conf->receive_only) { printk(KERN_INFO "%s: Configured for 'receive only' mode\n", card->devname); } } /* Setup Port Bps */ if(card->wandev.clocking) { if((port_num == WANOPT_PRI) || card->u.c.receive_only) { /* For Primary Port 0 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -