1 /* $Id: message.c,v 1.1.4.1 2001/11/20 14:19:37 kai Exp $
2  *
3  * functions for sending and receiving control messages
4  *
5  * Copyright (C) 1996  SpellCaster Telecommunications Inc.
6  *
7  * This software may be used and distributed according to the terms
8  * of the GNU General Public License, incorporated herein by reference.
9  *
10  * For more information, please contact gpl-info@spellcast.com or write:
11  *
12  *     SpellCaster Telecommunications Inc.
13  *     5621 Finch Avenue East, Unit #3
14  *     Scarborough, Ontario  Canada
15  *     M1B 2T9
16  *     +1 (416) 297-8565
17  *     +1 (416) 297-6433 Facsimile
18  */
19 
20 #define __NO_VERSION__
21 #include "includes.h"
22 #include "hardware.h"
23 #include "message.h"
24 #include "card.h"
25 
26 extern board *adapter[];
27 extern unsigned int cinst;
28 
29 /*
30  * Obligatory function prototypes
31  */
32 extern int indicate_status(int,ulong,char*);
33 extern int scm_command(isdn_ctrl *);
34 extern void *memcpy_fromshmem(int, void *, const void *, size_t);
35 
36 
37 /*
38  * receive a message from the board
39  */
receivemessage(int card,RspMessage * rspmsg)40 int receivemessage(int card, RspMessage *rspmsg)
41 {
42 	DualPortMemory *dpm;
43 	unsigned long flags;
44 
45 	if (!IS_VALID_CARD(card)) {
46 		pr_debug("Invalid param: %d is not a valid card id\n", card);
47 		return -EINVAL;
48 	}
49 
50 	pr_debug("%s: Entered receivemessage\n",adapter[card]->devicename);
51 
52 	/*
53 	 * See if there are messages waiting
54 	 */
55 	if (inb(adapter[card]->ioport[FIFO_STATUS]) & RF_HAS_DATA) {
56 		/*
57 		 * Map in the DPM to the base page and copy the message
58 		 */
59 		save_flags(flags);
60 		cli();
61 		outb((adapter[card]->shmem_magic >> 14) | 0x80,
62 			adapter[card]->ioport[adapter[card]->shmem_pgport]);
63 		dpm = (DualPortMemory *) adapter[card]->rambase;
64 		memcpy_fromio(rspmsg, &(dpm->rsp_queue[dpm->rsp_tail]),
65 			MSG_LEN);
66 		dpm->rsp_tail = (dpm->rsp_tail+1) % MAX_MESSAGES;
67 		inb(adapter[card]->ioport[FIFO_READ]);
68 		restore_flags(flags);
69 
70 		/*
71 		 * Tell the board that the message is received
72 		 */
73 		pr_debug("%s: Received Message seq:%d pid:%d time:%d cmd:%d "
74 				"cnt:%d (type,class,code):(%d,%d,%d) "
75 				"link:%d stat:0x%x\n",
76 					adapter[card]->devicename,
77 					rspmsg->sequence_no,
78 					rspmsg->process_id,
79 					rspmsg->time_stamp,
80 					rspmsg->cmd_sequence_no,
81 					rspmsg->msg_byte_cnt,
82 					rspmsg->type,
83 					rspmsg->class,
84 					rspmsg->code,
85 					rspmsg->phy_link_no,
86 					rspmsg->rsp_status);
87 
88 		return 0;
89 	}
90 	return -ENOMSG;
91 }
92 
93 /*
94  * send a message to the board
95  */
sendmessage(int card,unsigned int procid,unsigned int type,unsigned int class,unsigned int code,unsigned int link,unsigned int data_len,unsigned int * data)96 int sendmessage(int card,
97 		unsigned int procid,
98 		unsigned int type,
99 		unsigned int class,
100 		unsigned int code,
101 		unsigned int link,
102 		unsigned int data_len,
103 		unsigned int *data)
104 {
105 	DualPortMemory *dpm;
106 	ReqMessage sndmsg;
107 	unsigned long flags;
108 
109 	if (!IS_VALID_CARD(card)) {
110 		pr_debug("Invalid param: %d is not a valid card id\n", card);
111 		return -EINVAL;
112 	}
113 
114 	/*
115 	 * Make sure we only send CEPID messages when the engine is up
116 	 * and CMPID messages when it is down
117 	 */
118 	if(adapter[card]->EngineUp && procid == CMPID) {
119 		pr_debug("%s: Attempt to send CM message with engine up\n",
120 			adapter[card]->devicename);
121 		return -ESRCH;
122 	}
123 
124 	if(!adapter[card]->EngineUp && procid == CEPID) {
125 		pr_debug("%s: Attempt to send CE message with engine down\n",
126 			adapter[card]->devicename);
127 		return -ESRCH;
128 	}
129 
130 	memset(&sndmsg, 0, MSG_LEN);
131 	sndmsg.msg_byte_cnt = 4;
132 	sndmsg.type = type;
133 	sndmsg.class = class;
134 	sndmsg.code = code;
135 	sndmsg.phy_link_no = link;
136 
137 	if (data_len > 0) {
138 		if (data_len > MSG_DATA_LEN)
139 			data_len = MSG_DATA_LEN;
140 		memcpy(&(sndmsg.msg_data), data, data_len);
141 		sndmsg.msg_byte_cnt = data_len + 8;
142 	}
143 
144 	sndmsg.process_id = procid;
145 	sndmsg.sequence_no = adapter[card]->seq_no++ % 256;
146 
147 	/*
148 	 * wait for an empty slot in the queue
149 	 */
150 	while (!(inb(adapter[card]->ioport[FIFO_STATUS]) & WF_NOT_FULL))
151 		udelay(1);
152 
153 	/*
154 	 * Disable interrupts and map in shared memory
155 	 */
156 	save_flags(flags);
157 	cli();
158 	outb((adapter[card]->shmem_magic >> 14) | 0x80,
159 		adapter[card]->ioport[adapter[card]->shmem_pgport]);
160 	dpm = (DualPortMemory *) adapter[card]->rambase;	/* Fix me */
161 	memcpy_toio(&(dpm->req_queue[dpm->req_head]),&sndmsg,MSG_LEN);
162 	dpm->req_head = (dpm->req_head+1) % MAX_MESSAGES;
163 	outb(sndmsg.sequence_no, adapter[card]->ioport[FIFO_WRITE]);
164 	restore_flags(flags);
165 
166 	pr_debug("%s: Sent Message seq:%d pid:%d time:%d "
167 			"cnt:%d (type,class,code):(%d,%d,%d) "
168 			"link:%d\n ",
169 				adapter[card]->devicename,
170 				sndmsg.sequence_no,
171 				sndmsg.process_id,
172 				sndmsg.time_stamp,
173 				sndmsg.msg_byte_cnt,
174 				sndmsg.type,
175 				sndmsg.class,
176 				sndmsg.code,
177 				sndmsg.phy_link_no);
178 
179 	return 0;
180 }
181 
send_and_receive(int card,unsigned int procid,unsigned char type,unsigned char class,unsigned char code,unsigned char link,unsigned char data_len,unsigned char * data,RspMessage * mesgdata,int timeout)182 int send_and_receive(int card,
183 		unsigned int procid,
184 		unsigned char type,
185 		unsigned char class,
186 		unsigned char code,
187 		unsigned char link,
188 	 	unsigned char data_len,
189 		unsigned char *data,
190 		RspMessage *mesgdata,
191 		int timeout)
192 {
193 	int retval;
194 	int tries;
195 
196 	if (!IS_VALID_CARD(card)) {
197 		pr_debug("Invalid param: %d is not a valid card id\n", card);
198 		return -EINVAL;
199 	}
200 
201 	adapter[card]->want_async_messages = 1;
202 	retval = sendmessage(card, procid, type, class, code, link,
203 			data_len, (unsigned int *) data);
204 
205 	if (retval) {
206 		pr_debug("%s: SendMessage failed in SAR\n",
207 			adapter[card]->devicename);
208 		adapter[card]->want_async_messages = 0;
209 		return -EIO;
210 	}
211 
212 	tries = 0;
213 	/* wait for the response */
214 	while (tries < timeout) {
215 		set_current_state(TASK_INTERRUPTIBLE);
216 		schedule_timeout(1);
217 
218 		pr_debug("SAR waiting..\n");
219 
220 		/*
221 		 * See if we got our message back
222 		 */
223 		if ((adapter[card]->async_msg.type == type) &&
224 		    (adapter[card]->async_msg.class == class) &&
225 		    (adapter[card]->async_msg.code == code) &&
226 		    (adapter[card]->async_msg.phy_link_no == link)) {
227 
228 			/*
229 			 * Got it!
230 			 */
231 			pr_debug("%s: Got ASYNC message\n",
232 				adapter[card]->devicename);
233 			memcpy(mesgdata, &(adapter[card]->async_msg),
234 				sizeof(RspMessage));
235 			adapter[card]->want_async_messages = 0;
236 			return 0;
237 		}
238 
239    		tries++;
240 	}
241 
242 	pr_debug("%s: SAR message timeout\n", adapter[card]->devicename);
243 	adapter[card]->want_async_messages = 0;
244 	return -ETIME;
245 }
246