sdla_x25.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:151k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2. * sdla_x25.c WANPIPE(tm) Multiprotocol WAN Link Driver.  X.25 module.
  3. *
  4. * Author: Nenad Corbic <ncorbic@sangoma.com>
  5. *
  6. * Copyright: (c) 1995-2001 Sangoma Technologies Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. * ============================================================================
  13. * Apr 03, 2001  Nenad Corbic  o Fixed the rx_skb=NULL bug in x25 in rx_intr().
  14. * Dec 26, 2000  Nenad Corbic  o Added a new polling routine, that uses
  15. *                                  a kernel timer (more efficient).
  16. * Dec 25, 2000  Nenad Corbic  o Updated for 2.4.X kernel
  17. * Jul 26, 2000  Nenad Corbic  o Increased the local packet buffering
  18. *     for API to 4096+header_size. 
  19. * Jul 17, 2000  Nenad Corbic  o Fixed the x25 startup bug. Enable 
  20. *     communications only after all interfaces
  21. *     come up.  HIGH SVC/PVC is used to calculate
  22. *     the number of channels.
  23. *                                  Enable protocol only after all interfaces
  24. *                                  are enabled.
  25. * Jul 10, 2000 Nenad Corbic  o Fixed the M_BIT bug. 
  26. * Apr 25, 2000  Nenad Corbic  o Pass Modem messages to the API.
  27. *                                  Disable idle timeout in X25 API.
  28. * Apr 14, 2000  Nenad Corbic  o Fixed: Large LCN number support.
  29. *                                  Maximum LCN number is 4095.
  30. *                                  Maximum number of X25 channels is 255.
  31. * Apr 06, 2000  Nenad Corbic  o Added SMP Support.
  32. * Mar 29, 2000  Nenad Corbic  o Added support for S514 PCI Card
  33. * Mar 23, 2000  Nenad Corbic  o Improved task queue, BH handling.
  34. * Mar 14, 2000  Nenad Corbic    o Updated Protocol Violation handling
  35. *                                  routines.  Bug Fix.
  36. * Mar 10, 2000  Nenad Corbic  o Bug Fix: corrupted mbox recovery.
  37. * Mar 09, 2000  Nenad Corbic     o Fixed the auto HDLC bug.
  38. * Mar 08, 2000 Nenad Corbic     o Fixed LAPB HDLC startup problems.
  39. *                                  Application must bring the link up 
  40. *                                  before tx/rx, and bring the 
  41. *                                  link down on close().
  42. * Mar 06, 2000 Nenad Corbic  o Added an option for logging call setup 
  43. *                                  information. 
  44. * Feb 29, 2000  Nenad Corbic   o Added support for LAPB HDLC API
  45. * Feb 25, 2000  Nenad Corbic     o Fixed the modem failure handling.
  46. *                                  No Modem OOB message will be passed 
  47. *                                  to the user.
  48. * Feb 21, 2000  Nenad Corbic   o Added Xpipemon Debug Support
  49. * Dec 30, 1999  Nenad Corbic  o Socket based X25API 
  50. * Sep 17, 1998 Jaspreet Singh  o Updates for 2.2.X  kernel
  51. * Mar 15, 1998 Alan Cox  o 2.1.x porting
  52. * Dec 19, 1997 Jaspreet Singh  o Added multi-channel IPX support
  53. * Nov 27, 1997 Jaspreet Singh  o Added protection against enabling of irqs
  54. *    when they are disabled.
  55. * Nov 17, 1997  Farhan Thawar    o Added IPX support
  56. *  o Changed if_send() to now buffer packets when
  57. *    the board is busy
  58. *  o Removed queueing of packets via the polling
  59. *    routing
  60. *  o Changed if_send() critical flags to properly
  61. *    handle race conditions
  62. * Nov 06, 1997  Farhan Thawar    o Added support for SVC timeouts
  63. *  o Changed PVC encapsulation to ETH_P_IP
  64. * Jul 21, 1997  Jaspreet Singh  o Fixed freeing up of buffers using kfree()
  65. *    when packets are received.
  66. * Mar 11, 1997  Farhan Thawar   Version 3.1.1
  67. *                                o added support for V35
  68. *                                o changed if_send() to return 0 if
  69. *                                  wandev.critical() is true
  70. *                                o free socket buffer in if_send() if
  71. *                                  returning 0
  72. *                                o added support for single '@' address to
  73. *                                  accept all incoming calls
  74. *                                o fixed bug in set_chan_state() to disconnect
  75. * Jan 15, 1997 Gene Kozin Version 3.1.0
  76. *  o implemented exec() entry point
  77. * Jan 07, 1997 Gene Kozin Initial version.
  78. *****************************************************************************/
  79. /*======================================================
  80.  *  Includes 
  81.  *=====================================================*/
  82. #include <linux/module.h>
  83. #include <linux/version.h>
  84. #include <linux/kernel.h> /* printk(), and other useful stuff */
  85. #include <linux/stddef.h> /* offsetof(), etc. */
  86. #include <linux/errno.h> /* return codes */
  87. #include <linux/string.h> /* inline memset(), etc. */
  88. #include <linux/ctype.h>
  89. #include <linux/slab.h> /* kmalloc(), kfree() */
  90. #include <linux/wanrouter.h> /* WAN router definitions */
  91. #include <linux/wanpipe.h> /* WANPIPE common user API definitions */
  92. #include <asm/byteorder.h> /* htons(), etc. */
  93. #include <asm/atomic.h>
  94. #include <linux/delay.h> /* Experimental delay */
  95. #if defined(LINUX_2_1) || defined(LINUX_2_4)
  96.  #include <asm/uaccess.h>
  97. #else
  98.  #include <asm/segment.h>
  99.  #include <net/route.h>
  100. #endif
  101. #include <linux/if.h>
  102. #include <linux/if_arp.h>
  103. #include <linux/sdla_x25.h> /* X.25 firmware API definitions */
  104. #include <linux/if_wanpipe_common.h>
  105. #include <linux/if_wanpipe.h>
  106. /*======================================================
  107.  *  Defines & Macros 
  108.  *=====================================================*/
  109. #define CMD_OK 0 /* normal firmware return code */
  110. #define CMD_TIMEOUT 0xFF /* firmware command timed out */
  111. #define MAX_CMD_RETRY 10 /* max number of firmware retries */
  112. #define X25_CHAN_MTU 4096 /* unfragmented logical channel MTU */
  113. #define X25_HRDHDR_SZ 7 /* max encapsulation header size */
  114. #define X25_CONCT_TMOUT (90*HZ) /* link connection timeout */
  115. #define X25_RECON_TMOUT (10*HZ) /* link connection timeout */
  116. #define CONNECT_TIMEOUT (90*HZ) /* link connection timeout */
  117. #define HOLD_DOWN_TIME (30*HZ) /* link hold down time */
  118. #define MAX_BH_BUFF 10
  119. #define M_BIT 0x01
  120. //#define PRINT_DEBUG 1
  121. #ifdef PRINT_DEBUG
  122. #define DBG_PRINTK(format, a...) printk(format, ## a)
  123. #else
  124. #define DBG_PRINTK(format, a...)
  125. #endif  
  126. #define TMR_INT_ENABLED_POLL_ACTIVE      0x01
  127. #define TMR_INT_ENABLED_POLL_CONNECT_ON  0x02
  128. #define TMR_INT_ENABLED_POLL_CONNECT_OFF 0x04
  129. #define TMR_INT_ENABLED_POLL_DISCONNECT  0x08
  130. #define TMR_INT_ENABLED_CMD_EXEC  0x10
  131. #define TMR_INT_ENABLED_UPDATE  0x20
  132. #define TMR_INT_ENABLED_UDP_PKT  0x40
  133. #define MAX_X25_ADDR_SIZE 16
  134. #define MAX_X25_DATA_SIZE  129
  135. #define MAX_X25_FACL_SIZE 110
  136. #define TRY_CMD_AGAIN 2
  137. #define DELAY_RESULT    1
  138. #define RETURN_RESULT   0
  139. #define DCD(x) (x & 0x03 ? "HIGH" : "LOW")
  140. #define CTS(x) (x & 0x05 ? "HIGH" : "LOW")
  141. /* Driver will not write log messages about 
  142.  * modem status if defined.*/
  143. #define MODEM_NOT_LOG 1
  144. /*==================================================== 
  145.  *  For IPXWAN 
  146.  *===================================================*/
  147. #define CVHexToAscii(b) (((unsigned char)(b) > (unsigned char)9) ? ((unsigned char)'A' + ((unsigned char)(b) - (unsigned char)10)) : ((unsigned char)'0' + (unsigned char)(b)))
  148. /*====================================================
  149.  *           MEMORY DEBUGGING FUNCTION
  150.  *====================================================
  151. #define KMEM_SAFETYZONE 8
  152. static void * dbg_kmalloc(unsigned int size, int prio, int line) {
  153. int i = 0;
  154. void * v = kmalloc(size+sizeof(unsigned int)+2*KMEM_SAFETYZONE*8,prio);
  155. char * c1 = v;
  156. c1 += sizeof(unsigned int);
  157. *((unsigned int *)v) = size;
  158. for (i = 0; i < KMEM_SAFETYZONE; i++) {
  159. c1[0] = 'D'; c1[1] = 'E'; c1[2] = 'A'; c1[3] = 'D';
  160. c1[4] = 'B'; c1[5] = 'E'; c1[6] = 'E'; c1[7] = 'F';
  161. c1 += 8;
  162. }
  163. c1 += size;
  164. for (i = 0; i < KMEM_SAFETYZONE; i++) {
  165. c1[0] = 'M'; c1[1] = 'U'; c1[2] = 'N'; c1[3] = 'G';
  166. c1[4] = 'W'; c1[5] = 'A'; c1[6] = 'L'; c1[7] = 'L';
  167. c1 += 8;
  168. }
  169. v = ((char *)v) + sizeof(unsigned int) + KMEM_SAFETYZONE*8;
  170. printk(KERN_INFO "line %d  kmalloc(%d,%d) = %pn",line,size,prio,v);
  171. return v;
  172. }
  173. static void dbg_kfree(void * v, int line) {
  174. unsigned int * sp = (unsigned int *)(((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8));
  175. unsigned int size = *sp;
  176. char * c1 = ((char *)v) - KMEM_SAFETYZONE*8;
  177. int i = 0;
  178. for (i = 0; i < KMEM_SAFETYZONE; i++) {
  179. if (   c1[0] != 'D' || c1[1] != 'E' || c1[2] != 'A' || c1[3] != 'D'
  180.     || c1[4] != 'B' || c1[5] != 'E' || c1[6] != 'E' || c1[7] != 'F') {
  181. printk(KERN_INFO "kmalloced block at %p has been corrupted (underrun)!n",v);
  182. printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2xn", i*8,
  183.                 c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
  184. }
  185. c1 += 8;
  186. }
  187. c1 += size;
  188. for (i = 0; i < KMEM_SAFETYZONE; i++) {
  189. if (   c1[0] != 'M' || c1[1] != 'U' || c1[2] != 'N' || c1[3] != 'G'
  190.     || c1[4] != 'W' || c1[5] != 'A' || c1[6] != 'L' || c1[7] != 'L'
  191.    ) {
  192. printk(KERN_INFO "kmalloced block at %p has been corrupted (overrun):n",v);
  193. printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2xn", i*8,
  194.                 c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
  195. }
  196. c1 += 8;
  197. }
  198. printk(KERN_INFO "line %d  kfree(%p)n",line,v);
  199. v = ((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8);
  200. kfree(v);
  201. }
  202. #define kmalloc(x,y) dbg_kmalloc(x,y,__LINE__)
  203. #define kfree(x) dbg_kfree(x,__LINE__)
  204. ==============================================================*/
  205. /*===============================================
  206.  *  Data Structures 
  207.  *===============================================*/
  208. /*========================================================
  209.  * Name:  x25_channel
  210.  *
  211.  * Purpose: To hold private informaton for each  
  212.  *              logical channel.
  213.  *
  214.  * Rationale:   Per-channel debugging is possible if each 
  215.  *              channel has its own private area.
  216.  *
  217.  * Assumptions:
  218.  *
  219.  * Description: This is an extention of the 'netdevice_t' 
  220.  *              we create for each network interface to keep 
  221.  *              the rest of X.25 channel-specific data. 
  222.  *
  223.  * Construct: Typedef
  224.  */
  225. typedef struct x25_channel
  226. {
  227. wanpipe_common_t common; /* common area for x25api and socket */
  228. char name[WAN_IFNAME_SZ+1]; /* interface name, ASCIIZ */
  229. char addr[WAN_ADDRESS_SZ+1]; /* media address, ASCIIZ */
  230. unsigned tx_pkt_size;
  231. unsigned short protocol; /* ethertype, 0 - multiplexed */
  232. char drop_sequence; /* mark sequence for dropping */
  233. unsigned long state_tick; /* time of the last state change */
  234. unsigned idle_timeout; /* sec, before disconnecting */
  235. unsigned long i_timeout_sofar;  /* # of sec's we've been idle */
  236. unsigned hold_timeout; /* sec, before re-connecting */
  237. unsigned long tick_counter; /* counter for transmit time out */
  238. char devtint; /* Weather we should dev_tint() */
  239. struct sk_buff* rx_skb; /* receive socket buffer */
  240. struct sk_buff* tx_skb; /* transmit socket buffer */
  241. bh_data_t *bh_head;      /* Circular buffer for x25api_bh */
  242. unsigned long  tq_working;
  243. volatile int  bh_write;
  244. volatile int  bh_read;
  245. atomic_t  bh_buff_used;
  246. sdla_t* card; /* -> owner */
  247. netdevice_t *dev; /* -> bound devce */
  248. int ch_idx;
  249. unsigned char enable_IPX;
  250. unsigned long network_number;
  251. #if defined(LINUX_2_1) || defined(LINUX_2_4)
  252. struct net_device_stats ifstats; /* interface statistics */
  253. #else
  254. struct enet_statistics ifstats;
  255. #endif
  256. unsigned short transmit_length;
  257. unsigned short tx_offset;
  258. char transmit_buffer[X25_CHAN_MTU+sizeof(x25api_hdr_t)];
  259. if_send_stat_t   if_send_stat;
  260.         rx_intr_stat_t   rx_intr_stat;
  261.         pipe_mgmt_stat_t pipe_mgmt_stat;    
  262. unsigned long router_start_time; /* Router start time in seconds */
  263. unsigned long router_up_time;
  264. } x25_channel_t;
  265. /* FIXME Take this out */
  266. #ifdef NEX_OLD_CALL_INFO
  267. typedef struct x25_call_info
  268. {
  269. char dest[17]; PACKED;/* ASCIIZ destination address */
  270. char src[17]; PACKED;/* ASCIIZ source address */
  271. char nuser; PACKED;/* number of user data bytes */
  272. unsigned char user[127]; PACKED;/* user data */
  273. char nfacil; PACKED;/* number of facilities */
  274. struct
  275. {
  276. unsigned char code;     PACKED;
  277. unsigned char parm;     PACKED;
  278. } facil[64];         /* facilities */
  279. } x25_call_info_t;
  280. #else
  281. typedef struct x25_call_info
  282. {
  283. char dest[MAX_X25_ADDR_SIZE] PACKED;/* ASCIIZ destination address */
  284. char src[MAX_X25_ADDR_SIZE] PACKED;/* ASCIIZ source address */
  285. unsigned char nuser PACKED;
  286. unsigned char user[MAX_X25_DATA_SIZE] PACKED;/* user data */
  287. unsigned char nfacil PACKED;
  288. unsigned char facil[MAX_X25_FACL_SIZE] PACKED;
  289. unsigned short lcn              PACKED;
  290. } x25_call_info_t;
  291. #endif
  292.   
  293. /*===============================================
  294.  * Private Function Prototypes
  295.  *==============================================*/
  296. /*================================================= 
  297.  * WAN link driver entry points. These are 
  298.  * called by the WAN router module.
  299.  */
  300. static int update (wan_device_t* wandev);
  301. static int new_if (wan_device_t* wandev, netdevice_t* dev,
  302. wanif_conf_t* conf);
  303. static int del_if (wan_device_t* wandev, netdevice_t* dev);
  304. static void disable_comm (sdla_t* card);
  305. static void disable_comm_shutdown(sdla_t *card);
  306. /*================================================= 
  307.  * WANPIPE-specific entry points 
  308.  */
  309. static int wpx_exec (struct sdla* card, void* u_cmd, void* u_data);
  310. static void x25api_bh (netdevice_t *);
  311. static int x25api_bh_cleanup (netdevice_t *);
  312. static int bh_enqueue (netdevice_t *, struct sk_buff *);
  313. /*=================================================  
  314.  *  Network device interface 
  315.  */
  316. static int if_init   (netdevice_t* dev);
  317. static int if_open   (netdevice_t* dev);
  318. static int if_close  (netdevice_t* dev);
  319. static int if_header (struct sk_buff* skb, netdevice_t* dev,
  320. unsigned short type, void* daddr, void* saddr, unsigned len);
  321. static int if_rebuild_hdr (struct sk_buff* skb);
  322. static int if_send (struct sk_buff* skb, netdevice_t* dev);
  323. static struct net_device_stats *if_stats (netdevice_t* dev);
  324. #ifdef LINUX_2_4
  325. static void if_tx_timeout (netdevice_t *dev);
  326. #endif
  327. /*=================================================  
  328.  *  Interrupt handlers 
  329.  */
  330. static void wpx_isr (sdla_t *);
  331. static void rx_intr (sdla_t *);
  332. static void tx_intr (sdla_t *);
  333. static void status_intr (sdla_t *);
  334. static void event_intr (sdla_t *);
  335. static void spur_intr (sdla_t *);
  336. static void timer_intr  (sdla_t *);
  337. static int tx_intr_send(sdla_t *, netdevice_t *);
  338. static netdevice_t * move_dev_to_next (sdla_t *, netdevice_t *);
  339. /*=================================================  
  340.  * Background polling routines 
  341.  */
  342. static void wpx_poll (sdla_t* card);
  343. static void poll_disconnected (sdla_t* card);
  344. static void poll_connecting (sdla_t* card);
  345. static void poll_active (sdla_t* card);
  346. static void trigger_x25_poll(sdla_t *card);
  347. static void x25_timer_routine(unsigned long data);
  348. /*=================================================  
  349.  * X.25 firmware interface functions 
  350.  */
  351. static int x25_get_version (sdla_t* card, char* str);
  352. static int x25_configure (sdla_t* card, TX25Config* conf);
  353. static int hdlc_configure (sdla_t* card, TX25Config* conf);
  354. static int set_hdlc_level (sdla_t* card);
  355. static int x25_get_err_stats (sdla_t* card);
  356. static int x25_get_stats (sdla_t* card);
  357. static int x25_set_intr_mode (sdla_t* card, int mode);
  358. static int x25_close_hdlc (sdla_t* card);
  359. static int x25_open_hdlc (sdla_t* card);
  360. static int x25_setup_hdlc (sdla_t* card);
  361. static int x25_set_dtr (sdla_t* card, int dtr);
  362. static int x25_get_chan_conf (sdla_t* card, x25_channel_t* chan);
  363. static int x25_place_call (sdla_t* card, x25_channel_t* chan);
  364. static int x25_accept_call (sdla_t* card, int lcn, int qdm);
  365. static int x25_clear_call (sdla_t* card, int lcn, int cause, int diagn);
  366. static int x25_send (sdla_t* card, int lcn, int qdm, int len, void* buf);
  367. static int x25_fetch_events (sdla_t* card);
  368. static int x25_error (sdla_t* card, int err, int cmd, int lcn);
  369. /*=================================================  
  370.  * X.25 asynchronous event handlers 
  371.  */
  372. static int incoming_call (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
  373. static int call_accepted (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
  374. static int call_cleared (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
  375. static int timeout_event (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
  376. static int restart_event (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
  377. /*=================================================  
  378.  * Miscellaneous functions 
  379.  */
  380. static int connect (sdla_t* card);
  381. static int disconnect (sdla_t* card);
  382. static netdevice_t* get_dev_by_lcn(wan_device_t* wandev, unsigned lcn);
  383. static int chan_connect (netdevice_t* dev);
  384. static int chan_disc (netdevice_t* dev);
  385. static void set_chan_state (netdevice_t* dev, int state);
  386. static int chan_send (netdevice_t* , void* , unsigned, unsigned char);
  387. static unsigned char bps_to_speed_code (unsigned long bps);
  388. static unsigned int dec_to_uint (unsigned char* str, int len);
  389. static unsigned int hex_to_uint (unsigned char*, int);
  390. static void parse_call_info (unsigned char*, x25_call_info_t*);
  391. static netdevice_t * find_channel(sdla_t *, unsigned);
  392. static void bind_lcn_to_dev (sdla_t *, netdevice_t *,unsigned);
  393. static void setup_for_delayed_transmit (netdevice_t*, void*, unsigned);
  394. /*=================================================  
  395.  *      X25 API Functions 
  396.  */
  397. static int wanpipe_pull_data_in_skb (sdla_t *, netdevice_t *, struct sk_buff **);
  398. static void timer_intr_exec(sdla_t *, unsigned char);
  399. static int execute_delayed_cmd (sdla_t*, netdevice_t *, mbox_cmd_t *,char);
  400. static int api_incoming_call (sdla_t*, TX25Mbox *, int);
  401. static int alloc_and_init_skb_buf (sdla_t *,struct sk_buff **, int);
  402. static void send_delayed_cmd_result(sdla_t *, netdevice_t *dev, TX25Mbox*);
  403. static int clear_confirm_event (sdla_t *, TX25Mbox*);
  404. static void send_oob_msg (sdla_t *, netdevice_t *, TX25Mbox *);
  405. static int timer_intr_cmd_exec(sdla_t *card);
  406. static void api_oob_event (sdla_t *card,TX25Mbox *mbox);
  407. static int check_bad_command (sdla_t *, netdevice_t *);
  408. static int channel_disconnect (sdla_t*, netdevice_t *);
  409. static void hdlc_link_down (sdla_t*);
  410. /*=================================================
  411.  *     XPIPEMON Functions
  412.  */
  413. static int process_udp_mgmt_pkt(sdla_t *);
  414. static int udp_pkt_type( struct sk_buff *, sdla_t*);
  415. static int reply_udp( unsigned char *, unsigned int); 
  416. static void init_x25_channel_struct( x25_channel_t *);
  417. static void init_global_statistics( sdla_t *);
  418. static int store_udp_mgmt_pkt(int, char, sdla_t*, netdevice_t *, struct sk_buff *, int);
  419. static unsigned short calc_checksum (char *, int);
  420. /*================================================= 
  421.  * IPX functions 
  422.  */
  423. static void switch_net_numbers(unsigned char *, unsigned long, unsigned char);
  424. static int handle_IPXWAN(unsigned char *, char *, unsigned char , 
  425.  unsigned long , unsigned short );
  426. extern void disable_irq(unsigned int);
  427. extern void enable_irq(unsigned int);
  428. static void S508_S514_lock(sdla_t *, unsigned long *);
  429. static void S508_S514_unlock(sdla_t *, unsigned long *);
  430. /*=================================================  
  431.  *  Global Variables 
  432.  *=================================================*/
  433. /*================================================= 
  434.  * Public Functions 
  435.  *=================================================*/
  436. /*===================================================================
  437.  * wpx_init: X.25 Protocol Initialization routine.
  438.  *
  439.  * Purpose: To initialize the protocol/firmware.
  440.  * 
  441.  * Rationale: This function is called by setup() function, in
  442.  *              sdlamain.c, to dynamically setup the x25 protocol.
  443.  * This is the first protocol specific function, which
  444.  *              executes once on startup.
  445.  *                
  446.  * Description: This procedure initializes the x25 firmware and
  447.  *     sets up the mailbox, transmit and receive buffer
  448.  *              pointers. It also initializes all debugging structures
  449.  *              and sets up the X25 environment.
  450.  *
  451.  * Sets up hardware options defined by user in [wanpipe#] 
  452.  * section of wanpipe#.conf configuration file. 
  453.  *
  454.  *  At this point adapter is completely initialized 
  455.  *       and X.25 firmware is running.
  456.  *   o read firmware version (to make sure it's alive)
  457.  *   o configure adapter
  458.  *   o initialize protocol-specific fields of the 
  459.  *                adapter data space.
  460.  *
  461.  * Called by: setup() function in sdlamain.c
  462.  *
  463.  * Assumptions: None
  464.  *
  465.  * Warnings: None
  466.  *
  467.  * Return:  0 o.k.
  468.  *   < 0 failure.
  469.  */
  470. int wpx_init (sdla_t* card, wandev_conf_t* conf)
  471. {
  472. union{
  473. char str[80];
  474. TX25Config cfg;
  475. } u;
  476. /* Verify configuration ID */
  477. if (conf->config_id != WANCONFIG_X25){
  478. printk(KERN_INFO "%s: invalid configuration ID %u!n",
  479. card->devname, conf->config_id)
  480. ;
  481. return -EINVAL;
  482. }
  483. /* Initialize protocol-specific fields */
  484. card->mbox  = (void*)(card->hw.dpmbase + X25_MBOX_OFFS);
  485. card->rxmb  = (void*)(card->hw.dpmbase + X25_RXMBOX_OFFS);
  486. card->flags = (void*)(card->hw.dpmbase + X25_STATUS_OFFS);
  487. /* Initialize for S514 Card */
  488. if(card->hw.type == SDLA_S514) {
  489. card->mbox += X25_MB_VECTOR;
  490. card->flags += X25_MB_VECTOR;
  491. card->rxmb += X25_MB_VECTOR;
  492. }
  493. /* Read firmware version.  Note that when adapter initializes, it
  494.  * clears the mailbox, so it may appear that the first command was
  495.  * executed successfully when in fact it was merely erased. To work
  496.  * around this, we execute the first command twice.
  497.  */
  498. if (x25_get_version(card, NULL) || x25_get_version(card, u.str))
  499. return -EIO;
  500. /* X25 firmware can run ether in X25 or LAPB HDLC mode.
  501.          * Check the user defined option and configure accordingly */
  502. if (conf->u.x25.LAPB_hdlc_only == WANOPT_YES){
  503. if (set_hdlc_level(card) != CMD_OK){
  504. return -EIO;
  505. }else{
  506. printk(KERN_INFO "%s: running LAP_B HDLC firmware v%sn",
  507. card->devname, u.str);
  508. }
  509. card->u.x.LAPB_hdlc = 1;
  510. }else{
  511. printk(KERN_INFO "%s: running X.25 firmware v%sn",
  512. card->devname, u.str);
  513. card->u.x.LAPB_hdlc = 0;
  514. }
  515. /* Configure adapter. Here we set resonable defaults, then parse
  516.  * device configuration structure and set configuration options.
  517.  * Most configuration options are verified and corrected (if
  518.  * necessary) since we can't rely on the adapter to do so.
  519.  */
  520. memset(&u.cfg, 0, sizeof(u.cfg));
  521. u.cfg.t1 = 3;
  522. u.cfg.n2 = 10;
  523. u.cfg.autoHdlc = 1; /* automatic HDLC connection */
  524. u.cfg.hdlcWindow = 7;
  525. u.cfg.pktWindow = 2;
  526. u.cfg.station = 1; /* DTE */
  527. u.cfg.options = 0x0090; /* disable D-bit pragmatics */
  528. u.cfg.ccittCompat = 1988;
  529. u.cfg.t10t20 = 30;
  530. u.cfg.t11t21 = 30;
  531. u.cfg.t12t22 = 30;
  532. u.cfg.t13t23 = 30;
  533. u.cfg.t16t26 = 30;
  534. u.cfg.t28 = 30;
  535. u.cfg.r10r20 = 5;
  536. u.cfg.r12r22 = 5;
  537. u.cfg.r13r23 = 5;
  538. u.cfg.responseOpt = 1; /* RR's after every packet */
  539. if (card->u.x.LAPB_hdlc){
  540. u.cfg.hdlcMTU = 1027;
  541. }
  542. if (conf->u.x25.x25_conf_opt){
  543. u.cfg.options = conf->u.x25.x25_conf_opt;
  544. }
  545. if (conf->clocking != WANOPT_EXTERNAL)
  546. u.cfg.baudRate = bps_to_speed_code(conf->bps);
  547. if (conf->station != WANOPT_DTE){
  548. u.cfg.station = 0; /* DCE mode */
  549. }
  550.         if (conf->interface != WANOPT_RS232 ){
  551.         u.cfg.hdlcOptions |= 0x80;      /* V35 mode */
  552. /* adjust MTU */
  553. if (!conf->mtu || (conf->mtu >= 1024))
  554. card->wandev.mtu = 1024;
  555. else if (conf->mtu >= 512)
  556. card->wandev.mtu = 512;
  557. else if (conf->mtu >= 256)
  558. card->wandev.mtu = 256;
  559. else if (conf->mtu >= 128)
  560. card->wandev.mtu = 128;
  561. else 
  562. card->wandev.mtu = 64;
  563. u.cfg.defPktSize = u.cfg.pktMTU = card->wandev.mtu;
  564. if (conf->u.x25.hi_pvc){
  565. card->u.x.hi_pvc = min_t(unsigned int, conf->u.x25.hi_pvc, MAX_LCN_NUM);
  566. card->u.x.lo_pvc = min_t(unsigned int, conf->u.x25.lo_pvc, card->u.x.hi_pvc);
  567. }
  568. if (conf->u.x25.hi_svc){
  569. card->u.x.hi_svc = min_t(unsigned int, conf->u.x25.hi_svc, MAX_LCN_NUM);
  570. card->u.x.lo_svc = min_t(unsigned int, conf->u.x25.lo_svc, card->u.x.hi_svc);
  571. }
  572. /* Figure out the total number of channels to configure */
  573. card->u.x.num_of_ch = 0;
  574. if (card->u.x.hi_svc != 0){
  575. card->u.x.num_of_ch = (card->u.x.hi_svc - card->u.x.lo_svc) + 1;
  576. }
  577. if (card->u.x.hi_pvc != 0){
  578. card->u.x.num_of_ch += (card->u.x.hi_pvc - card->u.x.lo_pvc) + 1;
  579. }
  580. if (card->u.x.num_of_ch == 0){
  581. printk(KERN_INFO "%s: ERROR, Minimum number of PVC/SVC channels is 1 !n"
  582.  "%s: Please set the Lowest/Highest PVC/SVC values !n",
  583.  card->devname,card->devname);
  584. return -ECHRNG;
  585. }
  586. u.cfg.loPVC = card->u.x.lo_pvc;
  587. u.cfg.hiPVC = card->u.x.hi_pvc;
  588. u.cfg.loTwoWaySVC = card->u.x.lo_svc;
  589. u.cfg.hiTwoWaySVC = card->u.x.hi_svc;
  590. if (conf->u.x25.hdlc_window)
  591. u.cfg.hdlcWindow = min_t(unsigned int, conf->u.x25.hdlc_window, 7);
  592. if (conf->u.x25.pkt_window)
  593. u.cfg.pktWindow = min_t(unsigned int, conf->u.x25.pkt_window, 7);
  594. if (conf->u.x25.t1)
  595. u.cfg.t1 = min_t(unsigned int, conf->u.x25.t1, 30);
  596. if (conf->u.x25.t2)
  597. u.cfg.t2 = min_t(unsigned int, conf->u.x25.t2, 29);
  598. if (conf->u.x25.t4)
  599. u.cfg.t4 = min_t(unsigned int, conf->u.x25.t4, 240);
  600. if (conf->u.x25.n2)
  601. u.cfg.n2 = min_t(unsigned int, conf->u.x25.n2, 30);
  602. if (conf->u.x25.t10_t20)
  603. u.cfg.t10t20 = min_t(unsigned int, conf->u.x25.t10_t20,255);
  604. if (conf->u.x25.t11_t21)
  605. u.cfg.t11t21 = min_t(unsigned int, conf->u.x25.t11_t21,255);
  606. if (conf->u.x25.t12_t22)
  607. u.cfg.t12t22 = min_t(unsigned int, conf->u.x25.t12_t22,255);
  608. if (conf->u.x25.t13_t23)
  609. u.cfg.t13t23 = min_t(unsigned int, conf->u.x25.t13_t23,255);
  610. if (conf->u.x25.t16_t26)
  611. u.cfg.t16t26 = min_t(unsigned int, conf->u.x25.t16_t26, 255);
  612. if (conf->u.x25.t28)
  613. u.cfg.t28 = min_t(unsigned int, conf->u.x25.t28, 255);
  614. if (conf->u.x25.r10_r20)
  615. u.cfg.r10r20 = min_t(unsigned int, conf->u.x25.r10_r20,250);
  616. if (conf->u.x25.r12_r22)
  617. u.cfg.r12r22 = min_t(unsigned int, conf->u.x25.r12_r22,250);
  618. if (conf->u.x25.r13_r23)
  619. u.cfg.r13r23 = min_t(unsigned int, conf->u.x25.r13_r23,250);
  620. if (conf->u.x25.ccitt_compat)
  621. u.cfg.ccittCompat = conf->u.x25.ccitt_compat;
  622. /* initialize adapter */
  623. if (card->u.x.LAPB_hdlc){
  624. if (hdlc_configure(card, &u.cfg) != CMD_OK)
  625. return -EIO;
  626. }else{
  627. if (x25_configure(card, &u.cfg) != CMD_OK)
  628. return -EIO;
  629. }
  630. if ((x25_close_hdlc(card) != CMD_OK) || /* close HDLC link */
  631.     (x25_set_dtr(card, 0) != CMD_OK)) /* drop DTR */
  632. return -EIO;
  633. /* Initialize protocol-specific fields of adapter data space */
  634. card->wandev.bps = conf->bps;
  635. card->wandev.interface = conf->interface;
  636. card->wandev.clocking = conf->clocking;
  637. card->wandev.station = conf->station;
  638. card->isr = &wpx_isr;
  639. card->poll = NULL; //&wpx_poll;
  640. card->disable_comm = &disable_comm;
  641. card->exec = &wpx_exec;
  642. card->wandev.update = &update;
  643. card->wandev.new_if = &new_if;
  644. card->wandev.del_if = &del_if;
  645. /* WARNING: This function cannot exit with an error
  646.  *          after the change of state */
  647. card->wandev.state = WAN_DISCONNECTED;
  648. card->wandev.enable_tx_int = 0;
  649. card->irq_dis_if_send_count = 0;
  650.         card->irq_dis_poll_count = 0;
  651. card->u.x.tx_dev = NULL;
  652. card->u.x.no_dev = 0;
  653. /* Configure for S514 PCI Card */
  654. if (card->hw.type == SDLA_S514) {
  655. card->u.x.hdlc_buf_status = 
  656. (volatile unsigned char *)
  657. (card->hw.dpmbase + X25_MB_VECTOR+ X25_MISC_HDLC_BITS);
  658. }else{
  659. card->u.x.hdlc_buf_status = 
  660. (volatile unsigned char *)(card->hw.dpmbase + X25_MISC_HDLC_BITS); 
  661. }
  662. card->u.x.poll_device=NULL;
  663. card->wandev.udp_port = conf->udp_port;
  664. /* Enable or disable call setup logging */
  665. if (conf->u.x25.logging == WANOPT_YES){
  666. printk(KERN_INFO "%s: Enabling Call Logging.n",
  667. card->devname);
  668. card->u.x.logging = 1;
  669. }else{
  670. card->u.x.logging = 0;
  671. }
  672. /* Enable or disable modem status reporting */
  673. if (conf->u.x25.oob_on_modem == WANOPT_YES){
  674. printk(KERN_INFO "%s: Enabling OOB on Modem change.n",
  675. card->devname);
  676. card->u.x.oob_on_modem = 1;
  677. }else{
  678. card->u.x.oob_on_modem = 0;
  679. }
  680. init_global_statistics(card);
  681. #ifndef LINUX_2_4
  682. card->u.x.x25_poll_task.next = NULL;
  683. #endif
  684. card->u.x.x25_poll_task.sync=0;
  685. card->u.x.x25_poll_task.routine = (void*)(void*)wpx_poll;
  686. card->u.x.x25_poll_task.data = card;
  687. init_timer(&card->u.x.x25_timer);
  688. card->u.x.x25_timer.data = (unsigned long)card;
  689. card->u.x.x25_timer.function = x25_timer_routine;
  690. return 0;
  691. }
  692. /*=========================================================
  693.  * WAN Device Driver Entry Points 
  694.  *========================================================*/
  695. /*============================================================
  696.  * Name: update(),  Update device status & statistics.
  697.  *
  698.  * Purpose: To provide debugging and statitical
  699.  *              information to the /proc file system.
  700.  *              /proc/net/wanrouter/wanpipe#
  701.  *              
  702.  * Rationale: The /proc file system is used to collect
  703.  *              information about the kernel and drivers.
  704.  *              Using the /proc file system the user
  705.  *              can see exactly what the sangoma drivers are
  706.  *              doing. And in what state they are in. 
  707.  *                
  708.  * Description: Collect all driver statistical information
  709.  *              and pass it to the top laywer. 
  710.  *
  711.  * Since we have to execute a debugging command, 
  712.  *              to obtain firmware statitics, we trigger a 
  713.  *              UPDATE function within the timer interrtup.
  714.  *              We wait until the timer update is complete.
  715.  *              Once complete return the appropriate return
  716.  *              code to indicate that the update was successful.
  717.  *              
  718.  * Called by: device_stat() in wanmain.c
  719.  *
  720.  * Assumptions:
  721.  *
  722.  * Warnings: This function will degrade the performance
  723.  *              of the router, since it uses the mailbox. 
  724.  *
  725.  * Return:  0  OK
  726.  *  <0 Failed (or busy).
  727.  */
  728. static int update (wan_device_t* wandev)
  729. {
  730. volatile sdla_t* card;
  731. TX25Status* status;
  732. unsigned long timeout;
  733. /* sanity checks */
  734. if ((wandev == NULL) || (wandev->private == NULL))
  735. return -EFAULT;
  736. if (wandev->state == WAN_UNCONFIGURED)
  737. return -ENODEV;
  738. if (test_bit(SEND_CRIT, (void*)&wandev->critical))
  739. return -EAGAIN;
  740. if (!wandev->dev)
  741. return -ENODEV;
  742. card = wandev->private;
  743. status = card->flags;
  744. card->u.x.timer_int_enabled |= TMR_INT_ENABLED_UPDATE;
  745. status->imask |= INTR_ON_TIMER;
  746. timeout = jiffies;
  747. for (;;){
  748. if (!(card->u.x.timer_int_enabled & TMR_INT_ENABLED_UPDATE)){
  749. break;
  750. }
  751. if ((jiffies-timeout) > 1*HZ){
  752. card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_UPDATE;
  753. return -EAGAIN;
  754. }
  755. }
  756. return 0;
  757. }
  758. /*===================================================================
  759.  * Name: new_if
  760.  *
  761.  * Purpose: To allocate and initialize resources for a 
  762.  *              new logical channel.  
  763.  * 
  764.  * Rationale: A new channel can be added dynamically via
  765.  *              ioctl call.
  766.  *                
  767.  * Description: Allocate a private channel structure, x25_channel_t.
  768.  * Parse the user interface options from wanpipe#.conf 
  769.  * configuration file. 
  770.  * Bind the private are into the network device private
  771.  *              area pointer (dev->priv).
  772.  * Prepare the network device structure for registration.
  773.  *
  774.  * Called by: ROUTER_IFNEW Ioctl call, from wanrouter_ioctl() 
  775.  *              (wanmain.c)
  776.  *
  777.  * Assumptions: None
  778.  *
  779.  * Warnings: None
  780.  *
  781.  * Return:  0  Ok
  782.  * <0  Failed (channel will not be created)
  783.  */
  784. static int new_if (wan_device_t* wandev, netdevice_t* dev, wanif_conf_t* conf)
  785. {
  786. sdla_t* card = wandev->private;
  787. x25_channel_t* chan;
  788. int err = 0;
  789. if ((conf->name[0] == '') || (strlen(conf->name) > WAN_IFNAME_SZ)){
  790. printk(KERN_INFO "%s: invalid interface name!n",
  791. card->devname);
  792. return -EINVAL;
  793. }
  794. if(card->wandev.new_if_cnt++ > 0 && card->u.x.LAPB_hdlc) {
  795. printk(KERN_INFO "%s: Error: Running LAPB HDLC Mode !n",
  796. card->devname);
  797. printk(KERN_INFO 
  798. "%s: Maximum number of network interfaces must be one !n",
  799. card->devname);
  800. return -EEXIST;
  801. }
  802. /* allocate and initialize private data */
  803. chan = kmalloc(sizeof(x25_channel_t), GFP_ATOMIC);
  804. if (chan == NULL){
  805. return -ENOMEM;
  806. }
  807. memset(chan, 0, sizeof(x25_channel_t));
  808. /* Bug Fix: Seg Err on PVC startup
  809.  * It must be here since bind_lcn_to_dev expects 
  810.  * it bellow */
  811. dev->priv = chan;
  812. strcpy(chan->name, conf->name);
  813. chan->card = card;
  814. chan->dev = dev;
  815. chan->common.sk = NULL;
  816. chan->common.func = NULL;
  817. chan->common.rw_bind = 0;
  818. chan->tx_skb = chan->rx_skb = NULL;
  819. /* verify media address */
  820. if (conf->addr[0] == '@'){ /* SVC */
  821. chan->common.svc = 1;
  822. strncpy(chan->addr, &conf->addr[1], WAN_ADDRESS_SZ);
  823. /* Set channel timeouts (default if not specified) */
  824. chan->idle_timeout = (conf->idle_timeout) ? 
  825. conf->idle_timeout : 90;
  826. chan->hold_timeout = (conf->hold_timeout) ? 
  827. conf->hold_timeout : 10;
  828. }else if (is_digit(conf->addr[0])){ /* PVC */
  829. int lcn = dec_to_uint(conf->addr, 0);
  830. if ((lcn >= card->u.x.lo_pvc) && (lcn <= card->u.x.hi_pvc)){
  831. bind_lcn_to_dev (card, dev, lcn);
  832. }else{
  833. printk(KERN_ERR
  834. "%s: PVC %u is out of range on interface %s!n",
  835. wandev->name, lcn, chan->name);
  836. err = -EINVAL;
  837. }
  838. }else{
  839. printk(KERN_ERR
  840. "%s: invalid media address on interface %s!n",
  841. wandev->name, chan->name);
  842. err = -EINVAL;
  843. }
  844. if(strcmp(conf->usedby, "WANPIPE") == 0){
  845.                 printk(KERN_INFO "%s: Running in WANPIPE mode %sn",
  846. wandev->name, chan->name);
  847.                 chan->common.usedby = WANPIPE;
  848. chan->protocol = htons(ETH_P_IP);
  849.         }else if(strcmp(conf->usedby, "API") == 0){
  850. chan->common.usedby = API;
  851.                 printk(KERN_INFO "%s: Running in API mode %sn",
  852. wandev->name, chan->name);
  853. chan->protocol = htons(X25_PROT);
  854. }
  855. if (err){
  856. kfree(chan);
  857. dev->priv = NULL;
  858. return err;
  859. }
  860. chan->enable_IPX = conf->enable_IPX;
  861. if (chan->enable_IPX)
  862. chan->protocol = htons(ETH_P_IPX);
  863. if (conf->network_number)
  864. chan->network_number = conf->network_number;
  865. else
  866. chan->network_number = 0xDEADBEEF;
  867. /* prepare network device data space for registration */
  868. #ifdef LINUX_2_4
  869. strcpy(dev->name,chan->name);
  870. #else
  871. dev->name = (char *)kmalloc(strlen(chan->name) + 2, GFP_KERNEL); 
  872. if(dev->name == NULL)
  873. {
  874. kfree(chan);
  875. dev->priv = NULL;
  876. return -ENOMEM;
  877. }
  878. sprintf(dev->name, "%s", chan->name);
  879. #endif
  880. dev->init = &if_init;
  881. init_x25_channel_struct(chan);
  882. return 0;
  883. }
  884. /*===================================================================
  885.  * Name: del_if(),  Remove a logical channel.  
  886.  *
  887.  * Purpose: To dynamically remove a logical channel.
  888.  * 
  889.  * Rationale: Each logical channel should be dynamically
  890.  *              removable. This functin is called by an 
  891.  *              IOCTL_IFDEL ioctl call or shutdown(). 
  892.  *                
  893.  * Description: Do nothing.
  894.  *
  895.  * Called by: IOCTL_IFDEL : wanrouter_ioctl() from wanmain.c
  896.  *              shutdown() from sdlamain.c
  897.  *
  898.  * Assumptions: 
  899.  *
  900.  * Warnings:
  901.  *
  902.  * Return:  0 Ok. Void function.
  903.  */
  904. //FIXME Del IF Should be taken out now.
  905. static int del_if (wan_device_t* wandev, netdevice_t* dev)
  906. {
  907. return 0;
  908. }
  909. /*============================================================
  910.  * Name: wpx_exec
  911.  *
  912.  * Description: Execute adapter interface command.
  913.  *  This option is currently dissabled.
  914.  *===========================================================*/
  915. static int wpx_exec (struct sdla* card, void* u_cmd, void* u_data)
  916. {
  917.         return 0;
  918. }
  919. /*============================================================
  920.  * Name: disable_comm
  921.  *
  922.  * Description: Disable communications during shutdown.
  923.  *              Dont check return code because there is 
  924.  *              nothing we can do about it.  
  925.  *
  926.  * Warning: Dev and private areas are gone at this point.
  927.  *===========================================================*/
  928. static void disable_comm(sdla_t* card)
  929. {
  930. disable_comm_shutdown(card);
  931. del_timer(&card->u.x.x25_timer);
  932. return;
  933. }
  934. /*============================================================
  935.  * Network Device Interface 
  936.  *===========================================================*/
  937. /*===================================================================
  938.  * Name: if_init(),   Netowrk Interface Initialization   
  939.  *
  940.  * Purpose: To initialize a network interface device structure.
  941.  * 
  942.  * Rationale: During network interface startup, the if_init
  943.  *              is called by the kernel to initialize the
  944.  *              netowrk device structure.  Thus a driver
  945.  *              can customze a network device. 
  946.  *                
  947.  * Description: Initialize the netowrk device call back
  948.  *              routines.  This is where we tell the kernel
  949.  *              which function to use when it wants to send
  950.  *              via our interface. 
  951.  * Furthermore, we initialize the device flags, 
  952.  *              MTU and physical address of the board.
  953.  *
  954.  * Called by: Kernel (/usr/src/linux/net/core/dev.c)
  955.  *  (dev->init())
  956.  *
  957.  * Assumptions: None
  958.  *
  959.  * Warnings: None
  960.  *
  961.  * Return:  0  Ok : Void function.
  962.  */
  963. static int if_init (netdevice_t* dev)
  964. {
  965. x25_channel_t* chan = dev->priv;
  966. sdla_t* card = chan->card;
  967. wan_device_t* wandev = &card->wandev;
  968. #ifdef LINUX_2_0
  969. int i;
  970. #endif
  971. /* Initialize device driver entry points */
  972. dev->open = &if_open;
  973. dev->stop = &if_close;
  974. dev->hard_header = &if_header;
  975. dev->rebuild_header = &if_rebuild_hdr;
  976. dev->hard_start_xmit = &if_send;
  977. dev->get_stats = &if_stats;
  978. #ifdef LINUX_2_4
  979. dev->tx_timeout = &if_tx_timeout;
  980. dev->watchdog_timeo = TX_TIMEOUT;
  981. #endif
  982. /* Initialize media-specific parameters */
  983. #if defined(LINUX_2_1) || defined(LINUX_2_4)
  984. dev->type = ARPHRD_PPP; /* ARP h/w type */
  985. #else
  986.         dev->family             = AF_INET;      /* address family */
  987.         dev->type               = ARPHRD_PPP;   /* no x25 type */
  988. #endif
  989. dev->flags |= IFF_POINTOPOINT;
  990. dev->flags |= IFF_NOARP;
  991. if (chan->common.usedby == API){
  992. dev->mtu = X25_CHAN_MTU+sizeof(x25api_hdr_t);
  993. }else{
  994. dev->mtu = card->wandev.mtu; 
  995. }
  996. dev->hard_header_len = X25_HRDHDR_SZ; /* media header length */
  997. dev->addr_len = 2; /* hardware address length */
  998. if (!chan->common.svc){
  999. *(unsigned short*)dev->dev_addr = htons(chan->common.lcn);
  1000. }
  1001. /* Initialize hardware parameters (just for reference) */
  1002. dev->irq = wandev->irq;
  1003. dev->dma = wandev->dma;
  1004. dev->base_addr = wandev->ioport;
  1005. dev->mem_start = (unsigned long)wandev->maddr;
  1006. dev->mem_end = wandev->maddr + wandev->msize - 1;
  1007.         /* Set transmit buffer queue length */
  1008.         dev->tx_queue_len = 100;
  1009. /* Initialize socket buffers */
  1010. #if !defined(LINUX_2_1) && !defined(LINUX_2_4)
  1011.         for (i = 0; i < DEV_NUMBUFFS; ++i)
  1012.                 skb_queue_head_init(&dev->buffs[i]);
  1013. #endif
  1014. /* FIXME Why are we doing this */
  1015. set_chan_state(dev, WAN_DISCONNECTED);
  1016. return 0;
  1017. }
  1018. /*===================================================================
  1019.  * Name: if_open(),   Open/Bring up the Netowrk Interface 
  1020.  *
  1021.  * Purpose: To bring up a network interface.
  1022.  * 
  1023.  * Rationale:
  1024.  *                
  1025.  * Description: Open network interface.
  1026.  *  o prevent module from unloading by incrementing use count
  1027.  *  o if link is disconnected then initiate connection
  1028.  *
  1029.  * Called by: Kernel (/usr/src/linux/net/core/dev.c)
  1030.  *  (dev->open())
  1031.  *
  1032.  * Assumptions: None
  1033.  *
  1034.  * Warnings: None
  1035.  *
  1036.  * Return:  0  Ok
  1037.  *  <0  Failur: Interface will not come up.
  1038.  */
  1039. static int if_open (netdevice_t* dev)
  1040. {
  1041. x25_channel_t* chan = dev->priv;
  1042. sdla_t* card = chan->card;
  1043. struct timeval tv;
  1044. unsigned long smp_flags;
  1045. if (is_dev_running(dev))
  1046. return -EBUSY;
  1047. chan->tq_working = 0;
  1048. /* Initialize the task queue */
  1049. #ifndef LINUX_2_4
  1050. chan->common.wanpipe_task.next = NULL;
  1051. #endif
  1052. chan->common.wanpipe_task.sync = 0;
  1053. chan->common.wanpipe_task.routine = (void *)(void *)x25api_bh;
  1054. chan->common.wanpipe_task.data = dev;
  1055. /* Allocate and initialize BH circular buffer */
  1056. /* Add 1 to MAX_BH_BUFF so we don't have test with (MAX_BH_BUFF-1) */
  1057. chan->bh_head = kmalloc((sizeof(bh_data_t)*(MAX_BH_BUFF+1)),GFP_ATOMIC);
  1058. if (chan->bh_head == NULL){
  1059. printk(KERN_INFO "%s: ERROR, failed to allocate memory ! BH_BUFFERS !n",
  1060. card->devname);
  1061. return -ENOBUFS;
  1062. }
  1063. memset(chan->bh_head,0,(sizeof(bh_data_t)*(MAX_BH_BUFF+1)));
  1064. atomic_set(&chan->bh_buff_used, 0);
  1065. /* Increment the number of interfaces */
  1066. ++card->u.x.no_dev;
  1067. wanpipe_open(card);
  1068. /* LAPB protocol only uses one interface, thus
  1069.  * start the protocol after it comes up. */
  1070. if (card->u.x.LAPB_hdlc){
  1071. if (card->open_cnt == 1){
  1072. TX25Status* status = card->flags;
  1073. S508_S514_lock(card, &smp_flags);
  1074. x25_set_intr_mode(card, INTR_ON_TIMER); 
  1075. status->imask &= ~INTR_ON_TIMER;
  1076. S508_S514_unlock(card, &smp_flags);
  1077. }
  1078. }else{
  1079. /* X25 can have multiple interfaces thus, start the 
  1080.  * protocol once all interfaces are up */
  1081. //FIXME: There is a bug here. If interface is
  1082. //brought down and up, it will try to enable comm.
  1083. if (card->open_cnt == card->u.x.num_of_ch){
  1084. S508_S514_lock(card, &smp_flags);
  1085. connect(card);
  1086. S508_S514_unlock(card, &smp_flags);
  1087. del_timer(&card->u.x.x25_timer);
  1088. card->u.x.x25_timer.expires=jiffies+HZ;
  1089. add_timer(&card->u.x.x25_timer);
  1090. }
  1091. }
  1092. /* Device is not up untill the we are in connected state */
  1093. do_gettimeofday( &tv );
  1094. chan->router_start_time = tv.tv_sec;
  1095. #ifdef LINUX_2_4
  1096. netif_start_queue(dev);
  1097. #else
  1098. dev->interrupt = 0;
  1099. dev->tbusy = 0;
  1100. dev->start = 1;
  1101. #endif
  1102. return 0;
  1103. }
  1104. /*===================================================================
  1105.  * Name: if_close(),   Close/Bring down the Netowrk Interface 
  1106.  *
  1107.  * Purpose: To bring down a network interface.
  1108.  * 
  1109.  * Rationale:
  1110.  *                
  1111.  * Description: Close network interface.
  1112.  *  o decrement use module use count
  1113.  *
  1114.  * Called by: Kernel (/usr/src/linux/net/core/dev.c)
  1115.  *  (dev->close())
  1116.  * ifconfig <name> down: will trigger the kernel
  1117.  *              which will call this function.
  1118.  *
  1119.  * Assumptions: None
  1120.  *
  1121.  * Warnings: None
  1122.  *
  1123.  * Return:  0  Ok
  1124.  *  <0  Failure: Interface will not exit properly.
  1125.  */
  1126. static int if_close (netdevice_t* dev)
  1127. {
  1128. x25_channel_t* chan = dev->priv;
  1129. sdla_t* card = chan->card;
  1130. unsigned long smp_flags;
  1131. stop_net_queue(dev);
  1132. #ifndef LINUX_2_4
  1133. dev->start=0;
  1134. #endif
  1135. if ((chan->common.state == WAN_CONNECTED) || 
  1136.     (chan->common.state == WAN_CONNECTING)){
  1137. S508_S514_lock(card, &smp_flags);
  1138. chan_disc(dev);
  1139. S508_S514_unlock(card, &smp_flags);
  1140. }
  1141. wanpipe_close(card);
  1142. S508_S514_lock(card, &smp_flags);
  1143. if (chan->bh_head){
  1144. int i;
  1145. struct sk_buff *skb;
  1146. for (i=0; i<(MAX_BH_BUFF+1); i++){
  1147. skb = ((bh_data_t *)&chan->bh_head[i])->skb;
  1148. if (skb != NULL){
  1149.                  wan_dev_kfree_skb(skb, FREE_READ);
  1150. }
  1151. }
  1152. kfree(chan->bh_head);
  1153. chan->bh_head=NULL;
  1154. }
  1155. S508_S514_unlock(card, &smp_flags);
  1156. /* If this is the last close, disconnect physical link */
  1157. if (!card->open_cnt){
  1158. S508_S514_lock(card, &smp_flags);
  1159. disconnect(card);
  1160. x25_set_intr_mode(card, 0);
  1161. S508_S514_unlock(card, &smp_flags);
  1162. }
  1163. /* Decrement the number of interfaces */
  1164. --card->u.x.no_dev;
  1165. return 0;
  1166. }
  1167. /*======================================================================
  1168.  *  Build media header.
  1169.  *  o encapsulate packet according to encapsulation type.
  1170.  *
  1171.  *  The trick here is to put packet type (Ethertype) into 'protocol' 
  1172.  *      field of the socket buffer, so that we don't forget it.  
  1173.  *      If encapsulation fails, set skb->protocol to 0 and discard 
  1174.  *      packet later.
  1175.  *
  1176.  *  Return: media header length.
  1177.  *======================================================================*/
  1178. static int if_header (struct sk_buff* skb, netdevice_t* dev,
  1179. unsigned short type, void* daddr, void* saddr, unsigned len)
  1180. {
  1181. x25_channel_t* chan = dev->priv;
  1182. int hdr_len = dev->hard_header_len;
  1183. skb->protocol = htons(type);
  1184. if (!chan->protocol){
  1185. hdr_len = wanrouter_encapsulate(skb, dev, type);
  1186. if (hdr_len < 0){
  1187. hdr_len = 0;
  1188. skb->protocol = htons(0);
  1189. }
  1190. }
  1191. return hdr_len;
  1192. }
  1193. /*===============================================================
  1194.  *  Re-build media header.
  1195.  *
  1196.  *  Return: 1 physical address resolved.
  1197.  * 0 physical address not resolved
  1198.  *==============================================================*/
  1199. static int if_rebuild_hdr (struct sk_buff* skb)
  1200. {
  1201. netdevice_t *dev = skb->dev; 
  1202. x25_channel_t* chan = dev->priv;
  1203. sdla_t* card = chan->card;
  1204. printk(KERN_INFO "%s: rebuild_header() called for interface %s!n",
  1205. card->devname, dev->name);
  1206. return 1;
  1207. }
  1208. #ifdef LINUX_2_4
  1209. /*============================================================================
  1210.  * Handle transmit timeout event from netif watchdog
  1211.  */
  1212. static void if_tx_timeout (netdevice_t *dev)
  1213. {
  1214.      x25_channel_t* chan = dev->priv;
  1215. sdla_t *card = chan->card;
  1216. /* If our device stays busy for at least 5 seconds then we will
  1217.  * kick start the device by making dev->tbusy = 0.  We expect
  1218.  * that our device never stays busy more than 5 seconds. So this                 
  1219.  * is only used as a last resort.
  1220.  */
  1221. ++chan->if_send_stat.if_send_tbusy_timeout;
  1222. printk (KERN_INFO "%s: Transmit timed out on %sn", 
  1223. card->devname, dev->name);
  1224. netif_wake_queue (dev);
  1225. }
  1226. #endif
  1227. /*=========================================================================
  1228.  *  Send a packet on a network interface.
  1229.  *  o set tbusy flag (marks start of the transmission).
  1230.  *  o check link state. If link is not up, then drop the packet.
  1231.  *  o check channel status. If it's down then initiate a call.
  1232.  *  o pass a packet to corresponding WAN device.
  1233.  *  o free socket buffer
  1234.  *
  1235.  *  Return: 0 complete (socket buffer must be freed)
  1236.  * non-0 packet may be re-transmitted (tbusy must be set)
  1237.  *
  1238.  *  Notes:
  1239.  *  1. This routine is called either by the protocol stack or by the "net
  1240.  *     bottom half" (with interrupts enabled).
  1241.  *  2. Setting tbusy flag will inhibit further transmit requests from the
  1242.  *     protocol stack and can be used for flow control with protocol layer.
  1243.  *
  1244.  *========================================================================*/
  1245. static int if_send (struct sk_buff* skb, netdevice_t* dev)
  1246. {
  1247. x25_channel_t* chan = dev->priv;
  1248. sdla_t* card = chan->card;
  1249. TX25Status* status = card->flags;
  1250. int udp_type;
  1251. unsigned long smp_flags=0;
  1252. ++chan->if_send_stat.if_send_entry;
  1253. #ifdef LINUX_2_4
  1254. netif_stop_queue(dev);
  1255. #endif
  1256. /* No need to check frame length, since socket code
  1257.          * will perform the check for us */
  1258. #ifndef LINUX_2_4
  1259. if (dev->tbusy){
  1260. netdevice_t *dev2;
  1261. ++chan->if_send_stat.if_send_tbusy;
  1262. if ((jiffies - chan->tick_counter) < (5*HZ)){
  1263. return 1;
  1264. }
  1265. printk(KERN_INFO "%s: Transmit time out %s!n",
  1266. card->devname, dev->name);
  1267. for( dev2 = card->wandev.dev; dev2; 
  1268.      dev2 = *((netdevice_t**)dev2->priv)){
  1269.          dev2->tbusy = 0;
  1270. }
  1271. ++chan->if_send_stat.if_send_tbusy_timeout;
  1272. }
  1273. #endif
  1274. chan->tick_counter = jiffies;
  1275. /* Critical region starts here */
  1276. S508_S514_lock(card, &smp_flags);
  1277. if (test_and_set_bit(SEND_CRIT, (void*)&card->wandev.critical)){
  1278. printk(KERN_INFO "Hit critical in if_send()! %lxn",card->wandev.critical);
  1279. goto if_send_crit_exit;
  1280. }
  1281. udp_type = udp_pkt_type(skb, card);
  1282.         if(udp_type != UDP_INVALID_TYPE) {
  1283.                 if(store_udp_mgmt_pkt(udp_type, UDP_PKT_FRM_STACK, card, dev, skb,
  1284.                         chan->common.lcn)) {
  1285.                         status->imask |= INTR_ON_TIMER;
  1286.                         if (udp_type == UDP_XPIPE_TYPE){
  1287.                                 chan->if_send_stat.if_send_PIPE_request++;
  1288. }
  1289.                 }
  1290. start_net_queue(dev);
  1291. clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
  1292. S508_S514_unlock(card, &smp_flags);
  1293. return 0;
  1294. }
  1295. if (chan->transmit_length){
  1296. //FIXME: This check doesn't make sense any more
  1297. if (chan->common.state != WAN_CONNECTED){
  1298. chan->transmit_length=0;
  1299. atomic_set(&chan->common.driver_busy,0);
  1300. }else{
  1301. stop_net_queue(dev);
  1302. ++card->u.x.tx_interrupts_pending;
  1303.         status->imask |= INTR_ON_TX_FRAME;
  1304. clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
  1305. S508_S514_unlock(card, &smp_flags);
  1306. return 1;
  1307. }
  1308. }
  1309. if (card->wandev.state != WAN_CONNECTED){
  1310. ++chan->ifstats.tx_dropped;
  1311. ++card->wandev.stats.tx_dropped;
  1312. ++chan->if_send_stat.if_send_wan_disconnected;
  1313. }else if ( chan->protocol && (chan->protocol != skb->protocol)){
  1314. printk(KERN_INFO
  1315. "%s: unsupported Ethertype 0x%04X on interface %s!n",
  1316. chan->name, htons(skb->protocol), dev->name);
  1317. printk(KERN_INFO "PROTO %Xn", htons(chan->protocol));
  1318. ++chan->ifstats.tx_errors;
  1319. ++chan->ifstats.tx_dropped;
  1320. ++card->wandev.stats.tx_dropped;
  1321. ++chan->if_send_stat.if_send_protocol_error;
  1322. }else switch (chan->common.state){
  1323. case WAN_DISCONNECTED:
  1324. /* Try to establish connection. If succeded, then start
  1325.  * transmission, else drop a packet.
  1326.  */
  1327. if (chan->common.usedby == API){
  1328. ++chan->ifstats.tx_dropped;
  1329. ++card->wandev.stats.tx_dropped;
  1330. break;
  1331. }else{
  1332. if (chan_connect(dev) != 0){
  1333. ++chan->ifstats.tx_dropped;
  1334. ++card->wandev.stats.tx_dropped;
  1335. break;
  1336. }
  1337. }
  1338. /* fall through */
  1339. case WAN_CONNECTED:
  1340. if( skb->protocol == htons(ETH_P_IPX)) {
  1341. if(chan->enable_IPX) {
  1342. switch_net_numbers( skb->data, 
  1343. chan->network_number, 0);
  1344. } else {
  1345. ++card->wandev.stats.tx_dropped;
  1346. ++chan->ifstats.tx_dropped;
  1347. ++chan->if_send_stat.if_send_protocol_error;
  1348. goto if_send_crit_exit;
  1349. }
  1350. }
  1351. /* We never drop here, if cannot send than, copy
  1352.                  * a packet into a transmit buffer 
  1353.                          */
  1354. chan_send(dev, skb->data, skb->len, 0);
  1355. break;
  1356. default:
  1357. ++chan->ifstats.tx_dropped;
  1358. ++card->wandev.stats.tx_dropped;
  1359. break;
  1360. }
  1361. if_send_crit_exit:
  1362.         wan_dev_kfree_skb(skb, FREE_WRITE);
  1363. start_net_queue(dev);
  1364. clear_bit(SEND_CRIT,(void*)&card->wandev.critical);
  1365. S508_S514_unlock(card, &smp_flags);
  1366. return 0;
  1367. }
  1368. /*============================================================================
  1369.  * Setup so that a frame can be transmitted on the occurence of a transmit
  1370.  * interrupt.
  1371.  *===========================================================================*/
  1372. static void setup_for_delayed_transmit (netdevice_t* dev, void* buf,
  1373. unsigned len)
  1374. {
  1375.         x25_channel_t* chan = dev->priv;
  1376.         sdla_t* card = chan->card;
  1377. TX25Status* status = card->flags;
  1378. ++chan->if_send_stat.if_send_adptr_bfrs_full;
  1379.         if(chan->transmit_length) {
  1380.                 printk(KERN_INFO "%s: Error, transmit length set in delayed transmit!n",
  1381. card->devname);
  1382.                 return;
  1383.         }
  1384. if (chan->common.usedby == API){
  1385. if (len > X25_CHAN_MTU+sizeof(x25api_hdr_t)) {
  1386. ++chan->ifstats.tx_dropped;
  1387. ++card->wandev.stats.tx_dropped;
  1388. printk(KERN_INFO "%s: Length is too big for delayed transmitn",
  1389. card->devname);
  1390. return;
  1391. }
  1392. }else{
  1393. if (len > X25_MAX_DATA) {
  1394. ++chan->ifstats.tx_dropped;
  1395. ++card->wandev.stats.tx_dropped;
  1396. printk(KERN_INFO "%s: Length is too big for delayed transmitn",
  1397. card->devname);
  1398. return;
  1399. }
  1400. }
  1401.         chan->transmit_length = len;
  1402. atomic_set(&chan->common.driver_busy,1);
  1403.         memcpy(chan->transmit_buffer, buf, len);
  1404. ++chan->if_send_stat.if_send_tx_int_enabled;
  1405. /* Enable Transmit Interrupt */
  1406. ++card->u.x.tx_interrupts_pending;
  1407.         status->imask |= INTR_ON_TX_FRAME;
  1408. }
  1409. /*===============================================================
  1410.  * net_device_stats
  1411.  *
  1412.  *  Get ethernet-style interface statistics.
  1413.  *  Return a pointer to struct enet_statistics.
  1414.  *
  1415.  *==============================================================*/
  1416. static struct net_device_stats *if_stats (netdevice_t* dev)
  1417. {
  1418. x25_channel_t *chan = dev->priv;
  1419. if(chan == NULL)
  1420. return NULL;
  1421. return &chan->ifstats;
  1422. }
  1423. /*
  1424.  * Interrupt Handlers 
  1425.  */
  1426. /*
  1427.  * X.25 Interrupt Service Routine.
  1428.  */
  1429. static void wpx_isr (sdla_t* card)
  1430. {
  1431. TX25Status* status = card->flags;
  1432. card->in_isr = 1;
  1433. ++card->statistics.isr_entry;
  1434. if (test_bit(PERI_CRIT,(void*)&card->wandev.critical)){
  1435. card->in_isr=0;
  1436. status->iflags = 0;
  1437. return;
  1438. }
  1439. if (test_bit(SEND_CRIT, (void*)&card->wandev.critical)){
  1440.   printk(KERN_INFO "%s: wpx_isr: wandev.critical set to 0x%02lx, int type = 0x%02xn", 
  1441. card->devname, card->wandev.critical, status->iflags);
  1442. card->in_isr = 0;
  1443. status->iflags = 0;
  1444. return;
  1445. }
  1446. /* For all interrupts set the critical flag to CRITICAL_RX_INTR.
  1447.          * If the if_send routine is called with this flag set it will set
  1448.          * the enable transmit flag to 1. (for a delayed interrupt)
  1449.          */
  1450. switch (status->iflags){
  1451. case RX_INTR_PENDING: /* receive interrupt */
  1452. rx_intr(card);
  1453. break;
  1454. case TX_INTR_PENDING: /* transmit interrupt */
  1455. tx_intr(card);
  1456. break;
  1457. case MODEM_INTR_PENDING: /* modem status interrupt */
  1458. status_intr(card);
  1459. break;
  1460. case X25_ASY_TRANS_INTR_PENDING: /* network event interrupt */
  1461. event_intr(card);
  1462. break;
  1463. case TIMER_INTR_PENDING:
  1464. timer_intr(card);
  1465. break;
  1466. default: /* unwanted interrupt */
  1467. spur_intr(card);
  1468. }
  1469. card->in_isr = 0;
  1470. status->iflags = 0; /* clear interrupt condition */
  1471. }
  1472. /*
  1473.  *  Receive interrupt handler.
  1474.  *  This routine handles fragmented IP packets using M-bit according to the
  1475.  *  RFC1356.
  1476.  *  o map ligical channel number to network interface.
  1477.  *  o allocate socket buffer or append received packet to the existing one.
  1478.  *  o if M-bit is reset (i.e. it's the last packet in a sequence) then 
  1479.  *    decapsulate packet and pass socket buffer to the protocol stack.
  1480.  *
  1481.  *  Notes:
  1482.  *  1. When allocating a socket buffer, if M-bit is set then more data is
  1483.  *     coming and we have to allocate buffer for the maximum IP packet size
  1484.  *     expected on this channel.
  1485.  *  2. If something goes wrong and X.25 packet has to be dropped (e.g. no
  1486.  *     socket buffers available) the whole packet sequence must be discarded.
  1487.  */
  1488. static void rx_intr (sdla_t* card)
  1489. {
  1490. TX25Mbox* rxmb = card->rxmb;
  1491. unsigned lcn = rxmb->cmd.lcn;
  1492. netdevice_t* dev = find_channel(card,lcn);
  1493. x25_channel_t* chan;
  1494. struct sk_buff* skb=NULL;
  1495. if (dev == NULL){
  1496. /* Invalid channel, discard packet */
  1497. printk(KERN_INFO "%s: receiving on orphaned LCN %d!n",
  1498. card->devname, lcn);
  1499. return;
  1500. }
  1501. chan = dev->priv;
  1502. chan->i_timeout_sofar = jiffies;
  1503. /* Copy the data from the board, into an
  1504.          * skb buffer 
  1505.  */
  1506. if (wanpipe_pull_data_in_skb(card,dev,&skb)){
  1507. ++chan->ifstats.rx_dropped;
  1508. ++card->wandev.stats.rx_dropped;
  1509. ++chan->rx_intr_stat.rx_intr_no_socket;
  1510. ++chan->rx_intr_stat.rx_intr_bfr_not_passed_to_stack;
  1511. return;
  1512. }
  1513. dev->last_rx = jiffies; /* timestamp */
  1514. /* ------------ API ----------------*/
  1515. if (chan->common.usedby == API){
  1516. if (bh_enqueue(dev, skb)){
  1517. ++chan->ifstats.rx_dropped;
  1518. ++card->wandev.stats.rx_dropped;
  1519. ++chan->rx_intr_stat.rx_intr_bfr_not_passed_to_stack;
  1520. wan_dev_kfree_skb(skb, FREE_READ);
  1521. return;
  1522. }
  1523. ++chan->ifstats.rx_packets;
  1524. #if defined(LINUX_2_1) || defined(LINUX_2_4)
  1525. chan->ifstats.rx_bytes += skb->len;
  1526. #endif
  1527. chan->rx_skb = NULL;
  1528. if (!test_and_set_bit(0, &chan->tq_working)){
  1529. wanpipe_queue_tq(&chan->common.wanpipe_task);
  1530. wanpipe_mark_bh();
  1531. }
  1532. return;
  1533. }
  1534. /* ------------- WANPIPE -------------------*/
  1535. /* set rx_skb to NULL so we won't access it later when kernel already owns it */
  1536. chan->rx_skb=NULL;
  1537. /* Decapsulate packet, if necessary */
  1538. if (!skb->protocol && !wanrouter_type_trans(skb, dev)){
  1539. /* can't decapsulate packet */
  1540.                 wan_dev_kfree_skb(skb, FREE_READ);
  1541. ++chan->ifstats.rx_errors;
  1542. ++chan->ifstats.rx_dropped;
  1543. ++card->wandev.stats.rx_dropped;
  1544. ++chan->rx_intr_stat.rx_intr_bfr_not_passed_to_stack;
  1545. }else{
  1546. if( handle_IPXWAN(skb->data, chan->name, 
  1547.   chan->enable_IPX, chan->network_number, 
  1548.   skb->protocol)){
  1549. if( chan->enable_IPX ){
  1550. if(chan_send(dev, skb->data, skb->len,0)){
  1551. chan->tx_skb = skb;
  1552. }else{
  1553.                                         wan_dev_kfree_skb(skb, FREE_WRITE);
  1554. ++chan->rx_intr_stat.rx_intr_bfr_not_passed_to_stack;
  1555. }
  1556. }else{
  1557. /* increment IPX packet dropped statistic */
  1558. ++chan->ifstats.rx_dropped;
  1559. ++chan->rx_intr_stat.rx_intr_bfr_not_passed_to_stack;
  1560. }
  1561. }else{
  1562. skb->mac.raw = skb->data;
  1563. #if defined(LINUX_2_1) || defined(LINUX_2_4)
  1564. chan->ifstats.rx_bytes += skb->len;
  1565. #endif
  1566. ++chan->ifstats.rx_packets;
  1567. ++chan->rx_intr_stat.rx_intr_bfr_passed_to_stack;
  1568. netif_rx(skb);
  1569. }
  1570. }
  1571. return;
  1572. }
  1573. static int wanpipe_pull_data_in_skb (sdla_t *card, netdevice_t *dev, struct sk_buff **skb)
  1574. {
  1575. void *bufptr;
  1576. TX25Mbox* rxmb = card->rxmb;
  1577. unsigned len = rxmb->cmd.length; /* packet length */
  1578. unsigned qdm = rxmb->cmd.qdm; /* Q,D and M bits */
  1579. x25_channel_t *chan = dev->priv;
  1580. struct sk_buff *new_skb = *skb;
  1581. if (chan->common.usedby == WANPIPE){
  1582. if (chan->drop_sequence){
  1583. if (!(qdm & 0x01)){ 
  1584. chan->drop_sequence = 0;
  1585. }
  1586. return 1;
  1587. }
  1588. new_skb = chan->rx_skb;
  1589. }else{
  1590. /* Add on the API header to the received
  1591.                  * data 
  1592.  */
  1593. len += sizeof(x25api_hdr_t);
  1594. }
  1595. if (new_skb == NULL){
  1596. int bufsize;
  1597. if (chan->common.usedby == WANPIPE){
  1598. bufsize = (qdm & 0x01) ? dev->mtu : len;
  1599. }else{
  1600. bufsize = len;
  1601. }
  1602. /* Allocate new socket buffer */
  1603. new_skb = dev_alloc_skb(bufsize + dev->hard_header_len);
  1604. if (new_skb == NULL){
  1605. printk(KERN_INFO "%s: no socket buffers available!n",
  1606. card->devname);
  1607. chan->drop_sequence = 1; /* set flag */
  1608. ++chan->ifstats.rx_dropped;
  1609. return 1;
  1610. }
  1611. }
  1612. if (skb_tailroom(new_skb) < len){
  1613. /* No room for the packet. Call off the whole thing! */
  1614.                 wan_dev_kfree_skb(new_skb, FREE_READ);
  1615. if (chan->common.usedby == WANPIPE){
  1616. chan->rx_skb = NULL;
  1617. if (qdm & 0x01){ 
  1618. chan->drop_sequence = 1;
  1619. }
  1620. }
  1621. printk(KERN_INFO "%s: unexpectedly long packet sequence "
  1622. "on interface %s!n", card->devname, dev->name);
  1623. ++chan->ifstats.rx_length_errors;
  1624. return 1;
  1625. }
  1626. bufptr = skb_put(new_skb,len);
  1627. if (chan->common.usedby == API){
  1628. /* Fill in the x25api header 
  1629.  */
  1630. x25api_t * api_data = (x25api_t*)bufptr;
  1631. api_data->hdr.qdm = rxmb->cmd.qdm;
  1632. api_data->hdr.cause = rxmb->cmd.cause;
  1633. api_data->hdr.diagn = rxmb->cmd.diagn;
  1634. api_data->hdr.length = rxmb->cmd.length;
  1635. memcpy(api_data->data, rxmb->data, rxmb->cmd.length);
  1636. }else{
  1637. memcpy(bufptr, rxmb->data, len);
  1638. }
  1639. new_skb->dev = dev;
  1640. if (chan->common.usedby == API){
  1641. new_skb->mac.raw = new_skb->data;
  1642. new_skb->protocol = htons(X25_PROT);
  1643. new_skb->pkt_type = WAN_PACKET_DATA;
  1644. }else{
  1645. new_skb->protocol = chan->protocol;
  1646. chan->rx_skb = new_skb;
  1647. }
  1648. /* If qdm bit is set, more data is coming 
  1649.          * thus, exit and wait for more data before
  1650.          * sending the packet up. (Used by router only) 
  1651.  */
  1652. if ((qdm & 0x01) && (chan->common.usedby == WANPIPE)) 
  1653. return 1;
  1654. *skb = new_skb; 
  1655. return 0;
  1656. }
  1657. /*===============================================================
  1658.  * tx_intr
  1659.  *  
  1660.  *  Transmit interrupt handler.
  1661.  * For each dev, check that there is something to send.
  1662.  * If data available, transmit. 
  1663.  *
  1664.  *===============================================================*/
  1665. static void tx_intr (sdla_t* card)
  1666. {
  1667. netdevice_t *dev;
  1668. TX25Status* status = card->flags;
  1669. unsigned char more_to_tx=0;
  1670. x25_channel_t *chan=NULL;
  1671. int i=0;
  1672. if (card->u.x.tx_dev == NULL){
  1673. card->u.x.tx_dev = card->wandev.dev;
  1674. }
  1675. dev = card->u.x.tx_dev;
  1676. for (;;){
  1677. chan = dev->priv;
  1678. if (chan->transmit_length){
  1679. /* Device was set to transmit, check if the TX
  1680.                          * buffers are available 
  1681.  */
  1682. if (chan->common.state != WAN_CONNECTED){
  1683. chan->transmit_length = 0;
  1684. atomic_set(&chan->common.driver_busy,0);
  1685. chan->tx_offset=0;
  1686. if (is_queue_stopped(dev)){
  1687. if (chan->common.usedby == API){
  1688. start_net_queue(dev);
  1689. wakeup_sk_bh(dev);
  1690. }else{
  1691. wake_net_dev(dev);
  1692. }
  1693. }
  1694. dev = move_dev_to_next(card,dev);
  1695. break;
  1696. }
  1697. if ((status->cflags[chan->ch_idx] & 0x40 || card->u.x.LAPB_hdlc) && 
  1698.      (*card->u.x.hdlc_buf_status & 0x40) ){
  1699. /* Tx buffer available, we can send */
  1700. if (tx_intr_send(card, dev)){
  1701. more_to_tx=1;
  1702. }
  1703. /* If more than one interface present, move the
  1704.                                  * device pointer to the next interface, so on the 
  1705.                                  * next TX interrupt we will try sending from it. 
  1706.                                  */
  1707. dev = move_dev_to_next(card,dev);
  1708. break;
  1709. }else{
  1710. /* Tx buffers not available, but device set
  1711.                                  * the TX interrupt.  Set more_to_tx and try  
  1712.                                  * to transmit for other devices.
  1713.  */
  1714. more_to_tx=1;
  1715. dev = move_dev_to_next(card,dev);
  1716. }
  1717. }else{
  1718. /* This device was not set to transmit,
  1719.                          * go to next 
  1720.  */
  1721. dev = move_dev_to_next(card,dev);
  1722. }
  1723. if (++i == card->u.x.no_dev){
  1724. if (!more_to_tx){
  1725. DBG_PRINTK(KERN_INFO "%s: Nothing to Send in TX INTRn",
  1726. card->devname);
  1727. }
  1728. break;
  1729. }
  1730. } //End of FOR
  1731. card->u.x.tx_dev = dev;
  1732. if (!more_to_tx){
  1733. /* if any other interfaces have transmit interrupts pending, */
  1734. /* do not disable the global transmit interrupt */
  1735. if (!(--card->u.x.tx_interrupts_pending)){
  1736. status->imask &= ~INTR_ON_TX_FRAME;
  1737. }
  1738. }
  1739. return;
  1740. }
  1741. /*===============================================================
  1742.  * move_dev_to_next
  1743.  *  
  1744.  *
  1745.  *===============================================================*/
  1746. netdevice_t * move_dev_to_next (sdla_t *card, netdevice_t *dev)
  1747. {
  1748. if (card->u.x.no_dev != 1){
  1749. if (*((netdevice_t**)dev->priv) == NULL){
  1750. return card->wandev.dev;
  1751. }else{
  1752. return *((netdevice_t**)dev->priv);
  1753. }
  1754. }
  1755. return dev;
  1756. }
  1757. /*===============================================================
  1758.  *  tx_intr_send
  1759.  *  
  1760.  *
  1761.  *===============================================================*/
  1762. static int tx_intr_send(sdla_t *card, netdevice_t *dev)
  1763. {
  1764. x25_channel_t* chan = dev->priv; 
  1765. if (chan_send (dev,chan->transmit_buffer,chan->transmit_length,1)){
  1766.  
  1767.                 /* Packet was split up due to its size, do not disable
  1768.                  * tx_intr 
  1769.                  */
  1770. return 1;
  1771. }
  1772. chan->transmit_length=0;
  1773. atomic_set(&chan->common.driver_busy,0);
  1774. chan->tx_offset=0;
  1775. /* If we are in API mode, wakeup the 
  1776.          * sock BH handler, not the NET_BH */
  1777. if (is_queue_stopped(dev)){
  1778. if (chan->common.usedby == API){
  1779. start_net_queue(dev);
  1780. wakeup_sk_bh(dev);
  1781. }else{
  1782. wake_net_dev(dev);
  1783. }
  1784. }
  1785. return 0;
  1786. }
  1787. /*===============================================================
  1788.  * timer_intr
  1789.  *  
  1790.  *  Timer interrupt handler.
  1791.  * Check who called the timer interrupt and perform
  1792.  *      action accordingly.
  1793.  *
  1794.  *===============================================================*/
  1795. static void timer_intr (sdla_t *card)
  1796. {
  1797. TX25Status* status = card->flags;
  1798. if (card->u.x.timer_int_enabled & TMR_INT_ENABLED_CMD_EXEC){
  1799. if (timer_intr_cmd_exec(card) == 0){
  1800. card->u.x.timer_int_enabled &=
  1801. ~TMR_INT_ENABLED_CMD_EXEC;
  1802. }
  1803. }else  if(card->u.x.timer_int_enabled & TMR_INT_ENABLED_UDP_PKT) {
  1804. if ((*card->u.x.hdlc_buf_status & 0x40) && 
  1805.     card->u.x.udp_type == UDP_XPIPE_TYPE){
  1806.                      if(process_udp_mgmt_pkt(card)) {
  1807.                 card->u.x.timer_int_enabled &= 
  1808. ~TMR_INT_ENABLED_UDP_PKT;
  1809. }
  1810. }
  1811. }else if (card->u.x.timer_int_enabled & TMR_INT_ENABLED_POLL_ACTIVE) {
  1812. netdevice_t *dev = card->u.x.poll_device;
  1813. x25_channel_t *chan = NULL;
  1814. if (!dev){
  1815. card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_POLL_ACTIVE;
  1816. return;
  1817. }
  1818. chan = dev->priv;
  1819. printk(KERN_INFO 
  1820. "%s: Closing down Idle link %s on LCN %dn",
  1821. card->devname,chan->name,chan->common.lcn); 
  1822. chan->i_timeout_sofar = jiffies;
  1823. chan_disc(dev);
  1824.           card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_POLL_ACTIVE;
  1825. card->u.x.poll_device=NULL;
  1826. }else if (card->u.x.timer_int_enabled & TMR_INT_ENABLED_POLL_CONNECT_ON) {
  1827. wanpipe_set_state(card, WAN_CONNECTED);
  1828. if (card->u.x.LAPB_hdlc){
  1829. netdevice_t *dev = card->wandev.dev;
  1830. set_chan_state(dev,WAN_CONNECTED);
  1831. send_delayed_cmd_result(card,dev,card->mbox);
  1832. }
  1833. /* 0x8F enable all interrupts */
  1834. x25_set_intr_mode(card, INTR_ON_RX_FRAME|
  1835. INTR_ON_TX_FRAME|
  1836. INTR_ON_MODEM_STATUS_CHANGE|
  1837. //INTR_ON_COMMAND_COMPLETE|
  1838. X25_ASY_TRANS_INTR_PENDING |
  1839. INTR_ON_TIMER |
  1840. DIRECT_RX_INTR_USAGE
  1841. ); 
  1842. status->imask &= ~INTR_ON_TX_FRAME; /* mask Tx interrupts */
  1843. card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_POLL_CONNECT_ON;
  1844. }else if (card->u.x.timer_int_enabled & TMR_INT_ENABLED_POLL_CONNECT_OFF) {
  1845. //printk(KERN_INFO "Poll connect, Turning OFFn");
  1846. disconnect(card);
  1847. card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_POLL_CONNECT_OFF;
  1848. }else if (card->u.x.timer_int_enabled & TMR_INT_ENABLED_POLL_DISCONNECT) {
  1849. //printk(KERN_INFO "POll disconnect, trying to connectn");
  1850. connect(card);
  1851. card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_POLL_DISCONNECT;
  1852. }else if (card->u.x.timer_int_enabled & TMR_INT_ENABLED_UPDATE){
  1853. if (*card->u.x.hdlc_buf_status & 0x40){
  1854. x25_get_err_stats(card);
  1855. x25_get_stats(card);
  1856. card->u.x.timer_int_enabled &= ~TMR_INT_ENABLED_UPDATE;
  1857. }
  1858. }
  1859. if(!card->u.x.timer_int_enabled){
  1860. //printk(KERN_INFO "Turning Timer Off n");
  1861.                 status->imask &= ~INTR_ON_TIMER;
  1862. }
  1863. }
  1864. /*====================================================================
  1865.  *  Modem status interrupt handler.
  1866.  *===================================================================*/
  1867. static void status_intr (sdla_t* card)
  1868. {
  1869. /* Added to avoid Modem status message flooding */
  1870. static TX25ModemStatus last_stat;
  1871. TX25Mbox* mbox = card->mbox;
  1872. TX25ModemStatus *modem_status;
  1873. netdevice_t *dev;
  1874. x25_channel_t *chan;
  1875. int err;
  1876. memset(&mbox->cmd, 0, sizeof(TX25Cmd));
  1877. mbox->cmd.command = X25_READ_MODEM_STATUS;
  1878. err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
  1879. if (err){ 
  1880. x25_error(card, err, X25_READ_MODEM_STATUS, 0);
  1881. }else{
  1882. modem_status = (TX25ModemStatus*)mbox->data;
  1883.             /* Check if the last status was the same
  1884.              * if it was, do NOT print message again */
  1885. if (last_stat.status != modem_status->status){
  1886.       printk(KERN_INFO "%s: Modem Status Change: DCD=%s, CTS=%sn",
  1887. card->devname,DCD(modem_status->status),CTS(modem_status->status));
  1888. last_stat.status = modem_status->status;
  1889. if (card->u.x.oob_on_modem){
  1890. mbox->cmd.pktType = mbox->cmd.command;
  1891. mbox->cmd.result = 0x08;
  1892. /* Send a OOB to all connected sockets */
  1893. for (dev = card->wandev.dev; dev; dev = *((netdevice_t**)dev->priv)){
  1894. chan=dev->priv;
  1895. if (chan->common.usedby == API){
  1896. send_oob_msg(card,dev,mbox);
  1897. }
  1898. }
  1899. /* The modem OOB message will probably kill the
  1900.  * the link. If we don't clear the flag here,
  1901.  * a deadlock could occur */ 
  1902. if (atomic_read(&card->u.x.command_busy)){
  1903. atomic_set(&card->u.x.command_busy,0);
  1904. }
  1905. }
  1906. }
  1907. }
  1908. memset(&mbox->cmd, 0, sizeof(TX25Cmd));
  1909. mbox->cmd.command = X25_HDLC_LINK_STATUS;
  1910. err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
  1911. if (err){ 
  1912. x25_error(card, err, X25_HDLC_LINK_STATUS, 0);
  1913. }
  1914. }
  1915. /*====================================================================
  1916.  *  Network event interrupt handler.
  1917.  *===================================================================*/
  1918. static void event_intr (sdla_t* card)
  1919. {
  1920. x25_fetch_events(card);
  1921. }
  1922. /*====================================================================
  1923.  *  Spurious interrupt handler.
  1924.  *  o print a warning
  1925.  *  o  
  1926.  *====================================================================*/
  1927. static void spur_intr (sdla_t* card)
  1928. {
  1929. printk(KERN_INFO "%s: spurious interrupt!n", card->devname);
  1930. }
  1931. /*
  1932.  * Background Polling Routines  
  1933.  */
  1934. /*====================================================================
  1935.  *  Main polling routine.
  1936.  *  This routine is repeatedly called by the WANPIPE 'thead' to allow for
  1937.  *  time-dependent housekeeping work.
  1938.  *
  1939.  *  Notes:
  1940.  *  1. This routine may be called on interrupt context with all interrupts
  1941.  *     enabled. Beware!
  1942.  *====================================================================*/
  1943. static void wpx_poll (sdla_t *card)
  1944. {
  1945. if (!card->wandev.dev){
  1946. goto wpx_poll_exit;
  1947. }
  1948. if (card->open_cnt != card->u.x.num_of_ch){
  1949. goto wpx_poll_exit;
  1950. }
  1951. if (test_bit(PERI_CRIT,&card->wandev.critical)){
  1952. goto wpx_poll_exit;
  1953. }
  1954. if (test_bit(SEND_CRIT,&card->wandev.critical)){
  1955. goto wpx_poll_exit;
  1956. }
  1957. switch(card->wandev.state){
  1958. case WAN_CONNECTED:
  1959. poll_active(card);
  1960. break;
  1961. case WAN_CONNECTING:
  1962. poll_connecting(card);
  1963. break;
  1964. case WAN_DISCONNECTED:
  1965. poll_disconnected(card);
  1966. break;
  1967. }
  1968. wpx_poll_exit:
  1969. clear_bit(POLL_CRIT,&card->wandev.critical);
  1970. return;
  1971. }
  1972. static void trigger_x25_poll(sdla_t *card)
  1973. {
  1974. #ifdef LINUX_2_4
  1975. schedule_task(&card->u.x.x25_poll_task);
  1976. #else
  1977. queue_task(&card->u.x.x25_poll_task, &tq_scheduler);
  1978. #endif
  1979. }
  1980. /*====================================================================
  1981.  *  Handle physical link establishment phase.
  1982.  *  o if connection timed out, disconnect the link.
  1983.  *===================================================================*/
  1984. static void poll_connecting (sdla_t* card)
  1985. {
  1986. volatile TX25Status* status = card->flags;
  1987. if (status->gflags & X25_HDLC_ABM){
  1988. timer_intr_exec (card, TMR_INT_ENABLED_POLL_CONNECT_ON);
  1989. }else if ((jiffies - card->state_tick) > CONNECT_TIMEOUT){
  1990. timer_intr_exec (card, TMR_INT_ENABLED_POLL_CONNECT_OFF);
  1991. }
  1992. }
  1993. /*====================================================================
  1994.  *  Handle physical link disconnected phase.
  1995.  *  o if hold-down timeout has expired and there are open interfaces, 
  1996.  * connect link.
  1997.  *===================================================================*/
  1998. static void poll_disconnected (sdla_t* card)
  1999. {
  2000. netdevice_t *dev; 
  2001. x25_channel_t *chan;
  2002. TX25Status* status = card->flags;
  2003. if (!card->u.x.LAPB_hdlc && card->open_cnt && 
  2004.     ((jiffies - card->state_tick) > HOLD_DOWN_TIME)){
  2005. timer_intr_exec(card, TMR_INT_ENABLED_POLL_DISCONNECT);
  2006. }
  2007. if ((dev=card->wandev.dev) == NULL)
  2008. return;
  2009. if ((chan=dev->priv) == NULL)
  2010. return;
  2011. if (chan->common.usedby == API && 
  2012.     atomic_read(&chan->common.command) && 
  2013.     card->u.x.LAPB_hdlc){
  2014. if (!(card->u.x.timer_int_enabled & TMR_INT_ENABLED_CMD_EXEC)) 
  2015. card->u.x.timer_int_enabled |= TMR_INT_ENABLED_CMD_EXEC;
  2016. if (!(status->imask & INTR_ON_TIMER))
  2017. status->imask |= INTR_ON_TIMER;
  2018. }
  2019. }
  2020. /*====================================================================
  2021.  *  Handle active link phase.
  2022.  *  o fetch X.25 asynchronous events.
  2023.  *  o kick off transmission on all interfaces.
  2024.  *===================================================================*/
  2025. static void poll_active (sdla_t* card)
  2026. {
  2027. netdevice_t* dev;
  2028. TX25Status* status = card->flags;
  2029. for (dev = card->wandev.dev; dev; dev = *((netdevice_t**)dev->priv)){
  2030. x25_channel_t* chan = dev->priv;
  2031. /* If SVC has been idle long enough, close virtual circuit */
  2032. if ( chan->common.svc && 
  2033.      chan->common.state == WAN_CONNECTED &&
  2034.      chan->common.usedby == WANPIPE ){
  2035. if( (jiffies - chan->i_timeout_sofar) / HZ > chan->idle_timeout ){
  2036. /* Close svc */
  2037. card->u.x.poll_device=dev;
  2038. timer_intr_exec (card, TMR_INT_ENABLED_POLL_ACTIVE);
  2039. }
  2040. }
  2041. #ifdef PRINT_DEBUG
  2042. chan->ifstats.tx_compressed = atomic_read(&chan->common.command);
  2043. chan->ifstats.tx_errors = chan->common.state;
  2044. chan->ifstats.rx_fifo_errors = atomic_read(&card->u.x.command_busy);
  2045. ++chan->ifstats.tx_bytes;
  2046. chan->ifstats.rx_fifo_errors=atomic_read(&chan->common.disconnect);
  2047. chan->ifstats.multicast=atomic_read(&chan->bh_buff_used);
  2048. chan->ifstats.rx_length_errors=*card->u.x.hdlc_buf_status;
  2049. #endif
  2050. if (chan->common.usedby == API && 
  2051.     atomic_read(&chan->common.command) && 
  2052.             !card->u.x.LAPB_hdlc){
  2053. if (!(card->u.x.timer_int_enabled & TMR_INT_ENABLED_CMD_EXEC)) 
  2054. card->u.x.timer_int_enabled |= TMR_INT_ENABLED_CMD_EXEC;
  2055. if (!(status->imask & INTR_ON_TIMER))
  2056. status->imask |= INTR_ON_TIMER;
  2057. }
  2058. if ((chan->common.usedby == API) && 
  2059.      atomic_read(&chan->common.disconnect)){
  2060. if (chan->common.state == WAN_DISCONNECTED){
  2061. atomic_set(&chan->common.disconnect,0);
  2062. return;
  2063. }
  2064. atomic_set(&chan->common.command,X25_CLEAR_CALL);
  2065. if (!(card->u.x.timer_int_enabled & TMR_INT_ENABLED_CMD_EXEC)) 
  2066. card->u.x.timer_int_enabled |= TMR_INT_ENABLED_CMD_EXEC;
  2067. if (!(status->imask & INTR_ON_TIMER))
  2068. status->imask |= INTR_ON_TIMER;
  2069. }
  2070. }
  2071. }
  2072. static void timer_intr_exec(sdla_t *card, unsigned char TYPE)
  2073. {
  2074. TX25Status* status = card->flags;
  2075. card->u.x.timer_int_enabled |= TYPE;
  2076. if (!(status->imask & INTR_ON_TIMER))
  2077. status->imask |= INTR_ON_TIMER;
  2078. }
  2079. /*==================================================================== 
  2080.  * SDLA Firmware-Specific Functions 
  2081.  *
  2082.  *  Almost all X.25 commands can unexpetedly fail due to so called 'X.25
  2083.  *  asynchronous events' such as restart, interrupt, incoming call request,
  2084.  *  call clear request, etc.  They can't be ignored and have to be delt with
  2085.  *  immediately.  To tackle with this problem we execute each interface 
  2086.  *  command in a loop until good return code is received or maximum number 
  2087.  *  of retries is reached.  Each interface command returns non-zero return 
  2088.  *  code, an asynchronous event/error handler x25_error() is called.
  2089.  *====================================================================*/
  2090. /*====================================================================
  2091.  *  Read X.25 firmware version.
  2092.  * Put code version as ASCII string in str. 
  2093.  *===================================================================*/
  2094. static int x25_get_version (sdla_t* card, char* str)
  2095. {
  2096. TX25Mbox* mbox = card->mbox;
  2097.    int retry = MAX_CMD_RETRY;
  2098. int err;
  2099. do
  2100. {
  2101. memset(&mbox->cmd, 0, sizeof(TX25Cmd));
  2102. mbox->cmd.command = X25_READ_CODE_VERSION;
  2103. err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
  2104. } while (err && retry-- &&
  2105.  x25_error(card, err, X25_READ_CODE_VERSION, 0));
  2106. if (!err && str)
  2107. {
  2108. int len = mbox->cmd.length;
  2109. memcpy(str, mbox->data, len);
  2110. str[len] = '';
  2111. }
  2112. return err;
  2113. }
  2114. /*====================================================================
  2115.  *  Configure adapter.
  2116.  *===================================================================*/
  2117. static int x25_configure (sdla_t* card, TX25Config* conf)
  2118. {
  2119. TX25Mbox* mbox = card->mbox;
  2120.    int retry = MAX_CMD_RETRY;
  2121. int err;
  2122. do{
  2123. memset(&mbox->cmd, 0, sizeof(TX25Cmd));
  2124. memcpy(mbox->data, (void*)conf, sizeof(TX25Config));
  2125. mbox->cmd.length  = sizeof(TX25Config);
  2126. mbox->cmd.command = X25_SET_CONFIGURATION;
  2127. err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
  2128. } while (err && retry-- && x25_error(card, err, X25_SET_CONFIGURATION, 0));
  2129. return err;
  2130. }
  2131. /*====================================================================
  2132.  *  Configure adapter for HDLC only.
  2133.  *===================================================================*/
  2134. static int hdlc_configure (sdla_t* card, TX25Config* conf)
  2135. {
  2136. TX25Mbox* mbox = card->mbox;
  2137.    int retry = MAX_CMD_RETRY;
  2138. int err;
  2139. do{
  2140. memset(&mbox->cmd, 0, sizeof(TX25Cmd));
  2141. memcpy(mbox->data, (void*)conf, sizeof(TX25Config));
  2142. mbox->cmd.length  = sizeof(TX25Config);
  2143. mbox->cmd.command = X25_HDLC_SET_CONFIG;
  2144. err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
  2145. } while (err && retry-- && x25_error(card, err, X25_SET_CONFIGURATION, 0));
  2146. return err;
  2147. }
  2148. static int set_hdlc_level (sdla_t* card)
  2149. {
  2150. TX25Mbox* mbox = card->mbox;
  2151.    int retry = MAX_CMD_RETRY;
  2152. int err;
  2153. do{
  2154. memset(&mbox->cmd, 0, sizeof(TX25Cmd));
  2155. mbox->cmd.command = SET_PROTOCOL_LEVEL;
  2156. mbox->cmd.length = 1;
  2157. mbox->data[0] = HDLC_LEVEL; //| DO_HDLC_LEVEL_ERROR_CHECKING; 
  2158. err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
  2159. } while (err && retry-- && x25_error(card, err, SET_PROTOCOL_LEVEL, 0));
  2160. return err;
  2161. }
  2162. /*====================================================================
  2163.  * Get communications error statistics.
  2164.  *====================================================================*/
  2165. static int x25_get_err_stats (sdla_t* card)
  2166. {
  2167. TX25Mbox* mbox = card->mbox;
  2168.    int retry = MAX_CMD_RETRY;
  2169. int err;
  2170. do
  2171. {
  2172. memset(&mbox->cmd, 0, sizeof(TX25Cmd));
  2173. mbox->cmd.command = X25_HDLC_READ_COMM_ERR;
  2174. err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
  2175. } while (err && retry-- && x25_error(card, err, X25_HDLC_READ_COMM_ERR, 0));
  2176. if (!err)
  2177. {
  2178. THdlcCommErr* stats = (void*)mbox->data;
  2179. card->wandev.stats.rx_over_errors    = stats->rxOverrun;
  2180. card->wandev.stats.rx_crc_errors     = stats->rxBadCrc;
  2181. card->wandev.stats.rx_missed_errors  = stats->rxAborted;
  2182. card->wandev.stats.tx_aborted_errors = stats->txAborted;
  2183. }
  2184. return err;
  2185. }
  2186. /*====================================================================
  2187.  *  Get protocol statistics.
  2188.  *===================================================================*/
  2189. static int x25_get_stats (sdla_t* card)
  2190. {
  2191. TX25Mbox* mbox = card->mbox;
  2192.    int retry = MAX_CMD_RETRY;
  2193. int err;
  2194. do
  2195. {
  2196. memset(&mbox->cmd, 0, sizeof(TX25Cmd));
  2197. mbox->cmd.command = X25_READ_STATISTICS;
  2198. err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
  2199. } while (err && retry-- && x25_error(card, err, X25_READ_STATISTICS, 0)) ;
  2200. if (!err)
  2201. {
  2202. TX25Stats* stats = (void*)mbox->data;
  2203. card->wandev.stats.rx_packets = stats->rxData;
  2204. card->wandev.stats.tx_packets = stats->txData;
  2205. }
  2206. return err;
  2207. }
  2208. /*====================================================================
  2209.  *  Close HDLC link.
  2210.  *===================================================================*/
  2211. static int x25_close_hdlc (sdla_t* card)
  2212. {
  2213. TX25Mbox* mbox = card->mbox;
  2214.    int retry = MAX_CMD_RETRY;
  2215. int err;
  2216. do
  2217. {
  2218. memset(&mbox->cmd, 0, sizeof(TX25Cmd));
  2219. mbox->cmd.command = X25_HDLC_LINK_CLOSE;
  2220. err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
  2221. } while (err && retry-- && x25_error(card, err, X25_HDLC_LINK_CLOSE, 0));
  2222. return err;
  2223. }
  2224. /*====================================================================
  2225.  *  Open HDLC link.
  2226.  *===================================================================*/
  2227. static int x25_open_hdlc (sdla_t* card)
  2228. {
  2229. TX25Mbox* mbox = card->mbox;
  2230.    int retry = MAX_CMD_RETRY;
  2231. int err;
  2232. do
  2233. {
  2234. memset(&mbox->cmd, 0, sizeof(TX25Cmd));
  2235. mbox->cmd.command = X25_HDLC_LINK_OPEN;
  2236. err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
  2237. } while (err && retry-- && x25_error(card, err, X25_HDLC_LINK_OPEN, 0));
  2238. return err;
  2239. }
  2240. /*=====================================================================
  2241.  * Setup HDLC link.
  2242.  *====================================================================*/
  2243. static int x25_setup_hdlc (sdla_t* card)
  2244. {
  2245. TX25Mbox* mbox = card->mbox;
  2246.    int retry = MAX_CMD_RETRY;
  2247. int err;
  2248. do
  2249. {
  2250. memset(&mbox->cmd, 0, sizeof(TX25Cmd));
  2251. mbox->cmd.command = X25_HDLC_LINK_SETUP;
  2252. err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
  2253. } while (err && retry-- && x25_error(card, err, X25_HDLC_LINK_SETUP, 0));
  2254. return err;
  2255. }
  2256. /*====================================================================
  2257.  * Set (raise/drop) DTR.
  2258.  *===================================================================*/
  2259. static int x25_set_dtr (sdla_t* card, int dtr)
  2260. {
  2261. TX25Mbox* mbox = card->mbox;
  2262.    int retry = MAX_CMD_RETRY;
  2263. int err;
  2264. do
  2265. {
  2266. memset(&mbox->cmd, 0, sizeof(TX25Cmd));
  2267. mbox->data[0] = 0;
  2268. mbox->data[2] = 0;
  2269. mbox->data[1] = dtr ? 0x02 : 0x01;
  2270. mbox->cmd.length  = 3;
  2271. mbox->cmd.command = X25_SET_GLOBAL_VARS;
  2272. err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
  2273. } while (err && retry-- && x25_error(card, err, X25_SET_GLOBAL_VARS, 0));
  2274. return err;
  2275. }
  2276. /*====================================================================
  2277.  *  Set interrupt mode.
  2278.  *===================================================================*/
  2279. static int x25_set_intr_mode (sdla_t* card, int mode)
  2280. {
  2281. TX25Mbox* mbox = card->mbox;
  2282.    int retry = MAX_CMD_RETRY;
  2283. int err;
  2284. do
  2285. {
  2286. memset(&mbox->cmd, 0, sizeof(TX25Cmd));
  2287. mbox->data[0] = mode;
  2288. if (card->hw.fwid == SFID_X25_508){
  2289. mbox->data[1] = card->hw.irq;
  2290. mbox->data[2] = 2;
  2291. mbox->cmd.length = 3;
  2292. }else {
  2293.   mbox->cmd.length  = 1;
  2294. }
  2295. mbox->cmd.command = X25_SET_INTERRUPT_MODE;
  2296. err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
  2297. } while (err && retry-- && x25_error(card, err, X25_SET_INTERRUPT_MODE, 0));
  2298. return err;
  2299. }
  2300. /*====================================================================
  2301.  *  Read X.25 channel configuration.
  2302.  *===================================================================*/
  2303. static int x25_get_chan_conf (sdla_t* card, x25_channel_t* chan)
  2304. {
  2305. TX25Mbox* mbox = card->mbox;
  2306.    int retry = MAX_CMD_RETRY;
  2307. int lcn = chan->common.lcn;
  2308. int err;
  2309. do{
  2310. memset(&mbox->cmd, 0, sizeof(TX25Cmd));
  2311. mbox->cmd.lcn     = lcn;
  2312. mbox->cmd.command = X25_READ_CHANNEL_CONFIG;
  2313. err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
  2314. } while (err && retry-- && x25_error(card, err, X25_READ_CHANNEL_CONFIG, lcn));
  2315. if (!err)
  2316. {
  2317. TX25Status* status = card->flags;
  2318. /* calculate an offset into the array of status bytes */
  2319. if (card->u.x.hi_svc <= X25_MAX_CHAN){ 
  2320. chan->ch_idx = lcn - 1;
  2321. }else{
  2322. int offset;
  2323. /* FIX: Apr 14 2000 : Nenad Corbic
  2324.  * The data field was being compared to 0x1F using
  2325.                          * '&&' instead of '&'. 
  2326.  * This caused X25API to fail for LCNs greater than 255.
  2327.  */