File: /usr/src/linux/drivers/net/wan/sdla_ft1.c

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