sdla_ft1.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:11k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2. * sdla_chdlc.c WANPIPE(tm) Multiprotocol WAN Link Driver. Cisco HDLC module.
  3. *
  4. * Authors:  Nenad Corbic <ncorbic@sangoma.com>
  5. * Gideon Hack  
  6. *
  7. * Copyright: (c) 1995-1999 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. * Sep 30, 1999  Nenad Corbic    Fixed dynamic IP and route setup.
  15. * Sep 23, 1999  Nenad Corbic    Added SMP support, fixed tracing 
  16. * Sep 13, 1999  Nenad Corbic Split up Port 0 and 1 into separate devices.
  17. * Jun 02, 1999  Gideon Hack     Added support for the S514 adapter.
  18. * Oct 30, 1998 Jaspreet Singh Added Support for CHDLC API (HDLC STREAMING).
  19. * Oct 28, 1998 Jaspreet Singh Added Support for Dual Port CHDLC.
  20. * Aug 07, 1998 David Fong Initial version.
  21. *****************************************************************************/
  22. #include <linux/module.h>
  23. #include <linux/version.h>
  24. #include <linux/kernel.h> /* printk(), and other useful stuff */
  25. #include <linux/stddef.h> /* offsetof(), etc. */
  26. #include <linux/errno.h> /* return codes */
  27. #include <linux/string.h> /* inline memset(), etc. */
  28. #include <linux/slab.h> /* kmalloc(), kfree() */
  29. #include <linux/wanrouter.h> /* WAN router definitions */
  30. #include <linux/wanpipe.h> /* WANPIPE common user API definitions */
  31. #include <linux/if_arp.h> /* ARPHRD_* defines */
  32. #include <linux/inetdevice.h>
  33. #include <asm/uaccess.h>
  34. #include <linux/in.h> /* sockaddr_in */
  35. #include <linux/inet.h>
  36. #include <linux/if.h>
  37. #include <asm/byteorder.h> /* htons(), etc. */
  38. #include <linux/sdlapci.h>
  39. #include <asm/io.h>
  40. #include <linux/sdla_chdlc.h> /* CHDLC firmware API definitions */
  41. /****** Defines & Macros ****************************************************/
  42. /* reasons for enabling the timer interrupt on the adapter */
  43. #define TMR_INT_ENABLED_UDP    0x0001
  44. #define TMR_INT_ENABLED_UPDATE 0x0002
  45.  
  46. #define CHDLC_DFLT_DATA_LEN 1500 /* default MTU */
  47. #define CHDLC_HDR_LEN 1
  48. #define IFF_POINTTOPOINT 0x10
  49. #define WANPIPE 0x00
  50. #define API 0x01
  51. #define CHDLC_API 0x01
  52. #define PORT(x)   (x == 0 ? "PRIMARY" : "SECONDARY" )
  53.  
  54. /******Data Structures*****************************************************/
  55. /* This structure is placed in the private data area of the device structure.
  56.  * The card structure used to occupy the private area but now the following 
  57.  * structure will incorporate the card structure along with CHDLC specific data
  58.  */
  59. typedef struct chdlc_private_area
  60. {
  61. netdevice_t  *slave;
  62. sdla_t *card;
  63. int  TracingEnabled; /* For enabling Tracing */
  64. unsigned long  curr_trace_addr; /* Used for Tracing */
  65. unsigned long  start_trace_addr;
  66. unsigned long  end_trace_addr;
  67. unsigned long  base_addr_trace_buffer;
  68. unsigned long  end_addr_trace_buffer;
  69. unsigned short  number_trace_elements;
  70. unsigned   available_buffer_space;
  71. unsigned long  router_start_time;
  72. unsigned char  route_status;
  73. unsigned char  route_removed;
  74. unsigned long  tick_counter; /* For 5s timeout counter */
  75. unsigned long  router_up_time;
  76.         u32             IP_address; /* IP addressing */
  77.         u32             IP_netmask;
  78. unsigned char  mc; /* Mulitcast support on/off */
  79. unsigned short udp_pkt_lgth; /* udp packet processing */
  80. char udp_pkt_src;
  81. char udp_pkt_data[MAX_LGTH_UDP_MGNT_PKT];
  82. unsigned short timer_int_enabled;
  83. char update_comms_stats; /* updating comms stats */
  84. //FIXME: add driver stats as per frame relay!
  85. } chdlc_private_area_t;
  86. /* Route Status options */
  87. #define NO_ROUTE 0x00
  88. #define ADD_ROUTE 0x01
  89. #define ROUTE_ADDED 0x02
  90. #define REMOVE_ROUTE 0x03
  91. /****** Function Prototypes *************************************************/
  92. /* WAN link driver entry points. These are called by the WAN router module. */
  93. static int wpft1_exec (struct sdla *card, void *u_cmd, void *u_data);
  94. static int chdlc_read_version (sdla_t* card, char* str);
  95. static int chdlc_error (sdla_t *card, int err, CHDLC_MAILBOX_STRUCT *mb);
  96. /****** Public Functions ****************************************************/
  97. /*============================================================================
  98.  * Cisco HDLC protocol initialization routine.
  99.  *
  100.  * This routine is called by the main WANPIPE module during setup.  At this
  101.  * point adapter is completely initialized and firmware is running.
  102.  *  o read firmware version (to make sure it's alive)
  103.  *  o configure adapter
  104.  *  o initialize protocol-specific fields of the adapter data space.
  105.  *
  106.  * Return: 0 o.k.
  107.  * < 0 failure.
  108.  */
  109. int wpft1_init (sdla_t* card, wandev_conf_t* conf)
  110. {
  111. unsigned char port_num;
  112. int err;
  113. union
  114. {
  115. char str[80];
  116. } u;
  117. volatile CHDLC_MAILBOX_STRUCT* mb;
  118. CHDLC_MAILBOX_STRUCT* mb1;
  119. unsigned long timeout;
  120. /* Verify configuration ID */
  121. if (conf->config_id != WANCONFIG_CHDLC) {
  122. printk(KERN_INFO "%s: invalid configuration ID %u!n",
  123.   card->devname, conf->config_id);
  124. return -EINVAL;
  125. }
  126. /* Use primary port */
  127. card->u.c.comm_port = 0;
  128. /* Initialize protocol-specific fields */
  129. if(card->hw.type != SDLA_S514){
  130. card->mbox  = (void *) card->hw.dpmbase;
  131. }else{ 
  132. card->mbox = (void *) card->hw.dpmbase + PRI_BASE_ADDR_MB_STRUCT;
  133. }
  134. mb = mb1 = card->mbox;
  135. if (!card->configured){
  136. /* The board will place an 'I' in the return code to indicate that it is
  137.     ready to accept commands.  We expect this to be completed in less
  138.             than 1 second. */
  139. timeout = jiffies;
  140. while (mb->return_code != 'I') /* Wait 1s for board to initialize */
  141. if ((jiffies - timeout) > 1*HZ) break;
  142. if (mb->return_code != 'I') {
  143. printk(KERN_INFO
  144. "%s: Initialization not completed by adaptern",
  145. card->devname);
  146. printk(KERN_INFO "Please contact Sangoma representative.n");
  147. return -EIO;
  148. }
  149. }
  150. /* Read firmware version.  Note that when adapter initializes, it
  151.  * clears the mailbox, so it may appear that the first command was
  152.  * executed successfully when in fact it was merely erased. To work
  153.  * around this, we execute the first command twice.
  154.  */
  155. if (chdlc_read_version(card, u.str))
  156. return -EIO;
  157. printk(KERN_INFO "%s: Running FT1 Configuration firmware v%sn",
  158. card->devname, u.str); 
  159. card->isr = NULL;
  160. card->poll = NULL;
  161. card->exec = &wpft1_exec;
  162. card->wandev.update = NULL;
  163.   card->wandev.new_if = NULL;
  164. card->wandev.del_if = NULL;
  165. card->wandev.state = WAN_DUALPORT;
  166. card->wandev.udp_port    = conf->udp_port;
  167. card->wandev.new_if_cnt = 0;
  168. /* This is for the ports link state */
  169. card->u.c.state = WAN_DISCONNECTED;
  170. /* reset the number of times the 'update()' proc has been called */
  171. card->u.c.update_call_count = 0;
  172. card->wandev.ttl = 0x7F;
  173. card->wandev.interface = 0; 
  174. card->wandev.clocking = 0;
  175. port_num = card->u.c.comm_port;
  176. /* Setup Port Bps */
  177.         card->wandev.bps = 0;
  178. card->wandev.mtu = MIN_LGTH_CHDLC_DATA_CFG;
  179. /* Set up the interrupt status area */
  180. /* Read the CHDLC Configuration and obtain: 
  181.  * Ptr to shared memory infor struct
  182.          * Use this pointer to calculate the value of card->u.c.flags !
  183.    */
  184. mb1->buffer_length = 0;
  185. mb1->command = READ_CHDLC_CONFIGURATION;
  186. err = sdla_exec(mb1) ? mb1->return_code : CMD_TIMEOUT;
  187. if(err != COMMAND_OK) {
  188. chdlc_error(card, err, mb1);
  189. return -EIO;
  190. }
  191. if(card->hw.type == SDLA_S514){
  192.                 card->u.c.flags = (void *)(card->hw.dpmbase +
  193.                 (((CHDLC_CONFIGURATION_STRUCT *)mb1->data)->
  194. ptr_shared_mem_info_struct));
  195.         }else{
  196.                 card->u.c.flags = (void *)(card->hw.dpmbase +
  197.                         (((CHDLC_CONFIGURATION_STRUCT *)mb1->data)->
  198. ptr_shared_mem_info_struct % SDLA_WINDOWSIZE));
  199. }
  200. card->wandev.state = WAN_FT1_READY;
  201. printk(KERN_INFO "%s: FT1 Config Ready !n",card->devname);
  202. return 0;
  203. }
  204. static int wpft1_exec(sdla_t *card, void *u_cmd, void *u_data)
  205. {
  206. CHDLC_MAILBOX_STRUCT* mbox = card->mbox;
  207. int len;
  208. #if defined(LINUX_2_1) || defined(LINUX_2_4)
  209. if (copy_from_user((void*)&mbox->command, u_cmd, sizeof(ft1_exec_cmd_t))){
  210. return -EFAULT;
  211. }
  212. len = mbox->buffer_length;
  213. if (len) {
  214. if( copy_from_user((void*)&mbox->data, u_data, len)){
  215. return -EFAULT;
  216. }
  217. }
  218. /* execute command */
  219. if (!sdla_exec(mbox)){
  220. return -EIO;
  221. }
  222. /* return result */
  223. if( copy_to_user(u_cmd, (void*)&mbox->command, sizeof(ft1_exec_cmd_t))){
  224. return -EFAULT;
  225. }
  226. len = mbox->buffer_length;
  227. if (len && u_data && copy_to_user(u_data, (void*)&mbox->data, len)){
  228. return -EFAULT;
  229. }
  230. #else
  231.         if (!u_cmd || verify_area(VERIFY_WRITE, u_cmd, sizeof(ft1_exec_cmd_t))){
  232.                 return -EFAULT;
  233. }
  234.         memcpy_fromfs((void*)&mbox->command, u_cmd, sizeof(ft1_exec_cmd_t));
  235. len = mbox->buffer_length;
  236.         if (len) {
  237.                 if (!u_data || verify_area(VERIFY_READ, u_data, len))
  238.                         return -EFAULT;
  239. memcpy_fromfs((void*)&mbox->data, u_data, len);
  240.         }
  241.         /* execute command */
  242.         if (!sdla_exec(mbox))
  243.                 return -EIO;
  244.         /* return result */
  245.         memcpy_tofs(u_cmd, (void*)&mbox->command, sizeof(ft1_exec_cmd_t));
  246.         len = mbox->buffer_length;
  247.         if (len && u_data && !verify_area(VERIFY_WRITE, u_data, len)){
  248.                 memcpy_tofs(u_data, (void*)&mbox->data, len);
  249. }
  250. #endif
  251. return 0;
  252. }
  253. /*============================================================================
  254.  * Read firmware code version.
  255.  * Put code version as ASCII string in str. 
  256.  */
  257. static int chdlc_read_version (sdla_t* card, char* str)
  258. {
  259. CHDLC_MAILBOX_STRUCT* mb = card->mbox;
  260. int len;
  261. char err;
  262. mb->buffer_length = 0;
  263. mb->command = READ_CHDLC_CODE_VERSION;
  264. err = sdla_exec(mb) ? mb->return_code : CMD_TIMEOUT;
  265. if(err != COMMAND_OK) {
  266. chdlc_error(card,err,mb);
  267. }
  268. else if (str) {  /* is not null */
  269. len = mb->buffer_length;
  270. memcpy(str, mb->data, len);
  271. str[len] = '';
  272. }
  273. return (err);
  274. }
  275. /*============================================================================
  276.  * Firmware error handler.
  277.  * This routine is called whenever firmware command returns non-zero
  278.  * return code.
  279.  *
  280.  * Return zero if previous command has to be cancelled.
  281.  */
  282. static int chdlc_error (sdla_t *card, int err, CHDLC_MAILBOX_STRUCT *mb)
  283. {
  284. unsigned cmd = mb->command;
  285. switch (err) {
  286. case CMD_TIMEOUT:
  287. printk(KERN_ERR "%s: command 0x%02X timed out!n",
  288. card->devname, cmd);
  289. break;
  290. case S514_BOTH_PORTS_SAME_CLK_MODE:
  291. if(cmd == SET_CHDLC_CONFIGURATION) {
  292. printk(KERN_INFO
  293.  "%s: Configure both ports for the same clock sourcen",
  294. card->devname);
  295. break;
  296. }
  297. default:
  298. printk(KERN_INFO "%s: command 0x%02X returned 0x%02X!n",
  299. card->devname, cmd, err);
  300. }
  301. return 0;
  302. }
  303. MODULE_LICENSE("GPL");