sdla_ft1.c 9.8 KB

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