1 /* $Id: message.c,v 1.5.8.2 2001/09/23 22:24:59 kai Exp $
3 * functions for sending and receiving control messages
5 * Copyright (C) 1996 SpellCaster Telecommunications Inc.
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
10 * For more information, please contact gpl-info@spellcast.com or write:
12 * SpellCaster Telecommunications Inc.
13 * 5621 Finch Avenue East, Unit #3
14 * Scarborough, Ontario Canada
17 * +1 (416) 297-6433 Facsimile
19 #include <linux/sched.h>
26 * receive a message from the board
28 int receivemessage(int card
, RspMessage
*rspmsg
)
33 if (!IS_VALID_CARD(card
)) {
34 pr_debug("Invalid param: %d is not a valid card id\n", card
);
38 pr_debug("%s: Entered receivemessage\n",
39 sc_adapter
[card
]->devicename
);
42 * See if there are messages waiting
44 if (inb(sc_adapter
[card
]->ioport
[FIFO_STATUS
]) & RF_HAS_DATA
) {
46 * Map in the DPM to the base page and copy the message
48 spin_lock_irqsave(&sc_adapter
[card
]->lock
, flags
);
49 outb((sc_adapter
[card
]->shmem_magic
>> 14) | 0x80,
50 sc_adapter
[card
]->ioport
[sc_adapter
[card
]->shmem_pgport
]);
51 dpm
= (DualPortMemory
*) sc_adapter
[card
]->rambase
;
52 memcpy_fromio(rspmsg
, &(dpm
->rsp_queue
[dpm
->rsp_tail
]),
54 dpm
->rsp_tail
= (dpm
->rsp_tail
+1) % MAX_MESSAGES
;
55 inb(sc_adapter
[card
]->ioport
[FIFO_READ
]);
56 spin_unlock_irqrestore(&sc_adapter
[card
]->lock
, flags
);
58 * Tell the board that the message is received
60 pr_debug("%s: Received Message seq:%d pid:%d time:%d cmd:%d "
61 "cnt:%d (type,class,code):(%d,%d,%d) "
62 "link:%d stat:0x%x\n",
63 sc_adapter
[card
]->devicename
,
67 rspmsg
->cmd_sequence_no
,
81 * send a message to the board
83 int sendmessage(int card
,
89 unsigned int data_len
,
96 if (!IS_VALID_CARD(card
)) {
97 pr_debug("Invalid param: %d is not a valid card id\n", card
);
102 * Make sure we only send CEPID messages when the engine is up
103 * and CMPID messages when it is down
105 if(sc_adapter
[card
]->EngineUp
&& procid
== CMPID
) {
106 pr_debug("%s: Attempt to send CM message with engine up\n",
107 sc_adapter
[card
]->devicename
);
111 if(!sc_adapter
[card
]->EngineUp
&& procid
== CEPID
) {
112 pr_debug("%s: Attempt to send CE message with engine down\n",
113 sc_adapter
[card
]->devicename
);
117 memset(&sndmsg
, 0, MSG_LEN
);
118 sndmsg
.msg_byte_cnt
= 4;
120 sndmsg
.class = class;
122 sndmsg
.phy_link_no
= link
;
125 if (data_len
> MSG_DATA_LEN
)
126 data_len
= MSG_DATA_LEN
;
127 memcpy(&(sndmsg
.msg_data
), data
, data_len
);
128 sndmsg
.msg_byte_cnt
= data_len
+ 8;
131 sndmsg
.process_id
= procid
;
132 sndmsg
.sequence_no
= sc_adapter
[card
]->seq_no
++ % 256;
135 * wait for an empty slot in the queue
137 while (!(inb(sc_adapter
[card
]->ioport
[FIFO_STATUS
]) & WF_NOT_FULL
))
141 * Disable interrupts and map in shared memory
143 spin_lock_irqsave(&sc_adapter
[card
]->lock
, flags
);
144 outb((sc_adapter
[card
]->shmem_magic
>> 14) | 0x80,
145 sc_adapter
[card
]->ioport
[sc_adapter
[card
]->shmem_pgport
]);
146 dpm
= (DualPortMemory
*) sc_adapter
[card
]->rambase
; /* Fix me */
147 memcpy_toio(&(dpm
->req_queue
[dpm
->req_head
]),&sndmsg
,MSG_LEN
);
148 dpm
->req_head
= (dpm
->req_head
+1) % MAX_MESSAGES
;
149 outb(sndmsg
.sequence_no
, sc_adapter
[card
]->ioport
[FIFO_WRITE
]);
150 spin_unlock_irqrestore(&sc_adapter
[card
]->lock
, flags
);
152 pr_debug("%s: Sent Message seq:%d pid:%d time:%d "
153 "cnt:%d (type,class,code):(%d,%d,%d) "
155 sc_adapter
[card
]->devicename
,
168 int send_and_receive(int card
,
174 unsigned char data_len
,
176 RspMessage
*mesgdata
,
182 if (!IS_VALID_CARD(card
)) {
183 pr_debug("Invalid param: %d is not a valid card id\n", card
);
187 sc_adapter
[card
]->want_async_messages
= 1;
188 retval
= sendmessage(card
, procid
, type
, class, code
, link
,
189 data_len
, (unsigned int *) data
);
192 pr_debug("%s: SendMessage failed in SAR\n",
193 sc_adapter
[card
]->devicename
);
194 sc_adapter
[card
]->want_async_messages
= 0;
199 /* wait for the response */
200 while (tries
< timeout
) {
201 schedule_timeout_interruptible(1);
203 pr_debug("SAR waiting..\n");
206 * See if we got our message back
208 if ((sc_adapter
[card
]->async_msg
.type
== type
) &&
209 (sc_adapter
[card
]->async_msg
.class == class) &&
210 (sc_adapter
[card
]->async_msg
.code
== code
) &&
211 (sc_adapter
[card
]->async_msg
.phy_link_no
== link
)) {
216 pr_debug("%s: Got ASYNC message\n",
217 sc_adapter
[card
]->devicename
);
218 memcpy(mesgdata
, &(sc_adapter
[card
]->async_msg
),
220 sc_adapter
[card
]->want_async_messages
= 0;
227 pr_debug("%s: SAR message timeout\n", sc_adapter
[card
]->devicename
);
228 sc_adapter
[card
]->want_async_messages
= 0;