sdla_ft1.c 9.8 KB

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