[PATCH] g_file_storage: fix obscure race condition
[linux/fpc-iii.git] / drivers / isdn / sc / packet.c
blobf50defc38ae5b47baac92c6117b39d9a3bab1b04
1 /* $Id: packet.c,v 1.5.8.1 2001/09/23 22:24:59 kai Exp $
3 * Copyright (C) 1996 SpellCaster Telecommunications Inc.
5 * This software may be used and distributed according to the terms
6 * of the GNU General Public License, incorporated herein by reference.
8 * For more information, please contact gpl-info@spellcast.com or write:
10 * SpellCaster Telecommunications Inc.
11 * 5621 Finch Avenue East, Unit #3
12 * Scarborough, Ontario Canada
13 * M1B 2T9
14 * +1 (416) 297-8565
15 * +1 (416) 297-6433 Facsimile
18 #include "includes.h"
19 #include "hardware.h"
20 #include "message.h"
21 #include "card.h"
23 extern board *sc_adapter[];
24 extern unsigned int cinst;
26 extern int get_card_from_id(int);
27 extern int indicate_status(int, int,ulong, char*);
28 extern void memcpy_toshmem(int, void *, const void *, size_t);
29 extern void memcpy_fromshmem(int, void *, const void *, size_t);
30 extern int sendmessage(int, unsigned int, unsigned int, unsigned int,
31 unsigned int, unsigned int, unsigned int, unsigned int *);
33 int sndpkt(int devId, int channel, struct sk_buff *data)
35 LLData ReqLnkWrite;
36 int status;
37 int card;
38 unsigned long len;
40 card = get_card_from_id(devId);
42 if(!IS_VALID_CARD(card)) {
43 pr_debug("invalid param: %d is not a valid card id\n", card);
44 return -ENODEV;
47 pr_debug("%s: sndpkt: frst = 0x%x nxt = %d f = %d n = %d\n",
48 sc_adapter[card]->devicename,
49 sc_adapter[card]->channel[channel].first_sendbuf,
50 sc_adapter[card]->channel[channel].next_sendbuf,
51 sc_adapter[card]->channel[channel].free_sendbufs,
52 sc_adapter[card]->channel[channel].num_sendbufs);
54 if(!sc_adapter[card]->channel[channel].free_sendbufs) {
55 pr_debug("%s: out of TX buffers\n",
56 sc_adapter[card]->devicename);
57 return -EINVAL;
60 if(data->len > BUFFER_SIZE) {
61 pr_debug("%s: data overflows buffer size (data > buffer)\n",
62 sc_adapter[card]->devicename);
63 return -EINVAL;
66 ReqLnkWrite.buff_offset = sc_adapter[card]->channel[channel].next_sendbuf *
67 BUFFER_SIZE + sc_adapter[card]->channel[channel].first_sendbuf;
68 ReqLnkWrite.msg_len = data->len; /* sk_buff size */
69 pr_debug("%s: writing %d bytes to buffer offset 0x%x\n",
70 sc_adapter[card]->devicename,
71 ReqLnkWrite.msg_len, ReqLnkWrite.buff_offset);
72 memcpy_toshmem(card, (char *)ReqLnkWrite.buff_offset, data->data, ReqLnkWrite.msg_len);
75 * sendmessage
77 pr_debug("%s: sndpkt size=%d, buf_offset=0x%x buf_indx=%d\n",
78 sc_adapter[card]->devicename,
79 ReqLnkWrite.msg_len, ReqLnkWrite.buff_offset,
80 sc_adapter[card]->channel[channel].next_sendbuf);
82 status = sendmessage(card, CEPID, ceReqTypeLnk, ceReqClass1, ceReqLnkWrite,
83 channel+1, sizeof(LLData), (unsigned int*)&ReqLnkWrite);
84 len = data->len;
85 if(status) {
86 pr_debug("%s: failed to send packet, status = %d\n",
87 sc_adapter[card]->devicename, status);
88 return -1;
90 else {
91 sc_adapter[card]->channel[channel].free_sendbufs--;
92 sc_adapter[card]->channel[channel].next_sendbuf =
93 ++sc_adapter[card]->channel[channel].next_sendbuf ==
94 sc_adapter[card]->channel[channel].num_sendbufs ? 0 :
95 sc_adapter[card]->channel[channel].next_sendbuf;
96 pr_debug("%s: packet sent successfully\n", sc_adapter[card]->devicename);
97 dev_kfree_skb(data);
98 indicate_status(card,ISDN_STAT_BSENT,channel, (char *)&len);
100 return len;
103 void rcvpkt(int card, RspMessage *rcvmsg)
105 LLData newll;
106 struct sk_buff *skb;
108 if(!IS_VALID_CARD(card)) {
109 pr_debug("invalid param: %d is not a valid card id\n", card);
110 return;
113 switch(rcvmsg->rsp_status){
114 case 0x01:
115 case 0x02:
116 case 0x70:
117 pr_debug("%s: error status code: 0x%x\n",
118 sc_adapter[card]->devicename, rcvmsg->rsp_status);
119 return;
120 case 0x00:
121 if (!(skb = dev_alloc_skb(rcvmsg->msg_data.response.msg_len))) {
122 printk(KERN_WARNING "%s: rcvpkt out of memory, dropping packet\n",
123 sc_adapter[card]->devicename);
124 return;
126 skb_put(skb, rcvmsg->msg_data.response.msg_len);
127 pr_debug("%s: getting data from offset: 0x%x\n",
128 sc_adapter[card]->devicename,
129 rcvmsg->msg_data.response.buff_offset);
130 memcpy_fromshmem(card,
131 skb_put(skb, rcvmsg->msg_data.response.msg_len),
132 (char *)rcvmsg->msg_data.response.buff_offset,
133 rcvmsg->msg_data.response.msg_len);
134 sc_adapter[card]->card->rcvcallb_skb(sc_adapter[card]->driverId,
135 rcvmsg->phy_link_no-1, skb);
137 case 0x03:
139 * Recycle the buffer
141 pr_debug("%s: buffer size : %d\n",
142 sc_adapter[card]->devicename, BUFFER_SIZE);
143 /* memset_shmem(card, rcvmsg->msg_data.response.buff_offset, 0, BUFFER_SIZE); */
144 newll.buff_offset = rcvmsg->msg_data.response.buff_offset;
145 newll.msg_len = BUFFER_SIZE;
146 pr_debug("%s: recycled buffer at offset 0x%x size %d\n",
147 sc_adapter[card]->devicename,
148 newll.buff_offset, newll.msg_len);
149 sendmessage(card, CEPID, ceReqTypeLnk, ceReqClass1, ceReqLnkRead,
150 rcvmsg->phy_link_no, sizeof(LLData), (unsigned int *)&newll);
155 int setup_buffers(int card, int c)
157 unsigned int nBuffers, i, cBase;
158 unsigned int buffer_size;
159 LLData RcvBuffOffset;
161 if(!IS_VALID_CARD(card)) {
162 pr_debug("invalid param: %d is not a valid card id\n", card);
163 return -ENODEV;
167 * Calculate the buffer offsets (send/recv/send/recv)
169 pr_debug("%s: setting up channel buffer space in shared RAM\n",
170 sc_adapter[card]->devicename);
171 buffer_size = BUFFER_SIZE;
172 nBuffers = ((sc_adapter[card]->ramsize - BUFFER_BASE) / buffer_size) / 2;
173 nBuffers = nBuffers > BUFFERS_MAX ? BUFFERS_MAX : nBuffers;
174 pr_debug("%s: calculating buffer space: %d buffers, %d big\n",
175 sc_adapter[card]->devicename,
176 nBuffers, buffer_size);
177 if(nBuffers < 2) {
178 pr_debug("%s: not enough buffer space\n",
179 sc_adapter[card]->devicename);
180 return -1;
182 cBase = (nBuffers * buffer_size) * (c - 1);
183 pr_debug("%s: channel buffer offset from shared RAM: 0x%x\n",
184 sc_adapter[card]->devicename, cBase);
185 sc_adapter[card]->channel[c-1].first_sendbuf = BUFFER_BASE + cBase;
186 sc_adapter[card]->channel[c-1].num_sendbufs = nBuffers / 2;
187 sc_adapter[card]->channel[c-1].free_sendbufs = nBuffers / 2;
188 sc_adapter[card]->channel[c-1].next_sendbuf = 0;
189 pr_debug("%s: send buffer setup complete: first=0x%x n=%d f=%d, nxt=%d\n",
190 sc_adapter[card]->devicename,
191 sc_adapter[card]->channel[c-1].first_sendbuf,
192 sc_adapter[card]->channel[c-1].num_sendbufs,
193 sc_adapter[card]->channel[c-1].free_sendbufs,
194 sc_adapter[card]->channel[c-1].next_sendbuf);
197 * Prep the receive buffers
199 pr_debug("%s: adding %d RecvBuffers:\n",
200 sc_adapter[card]->devicename, nBuffers /2);
201 for (i = 0 ; i < nBuffers / 2; i++) {
202 RcvBuffOffset.buff_offset =
203 ((sc_adapter[card]->channel[c-1].first_sendbuf +
204 (nBuffers / 2) * buffer_size) + (buffer_size * i));
205 RcvBuffOffset.msg_len = buffer_size;
206 pr_debug("%s: adding RcvBuffer #%d offset=0x%x sz=%d bufsz:%d\n",
207 sc_adapter[card]->devicename,
208 i + 1, RcvBuffOffset.buff_offset,
209 RcvBuffOffset.msg_len,buffer_size);
210 sendmessage(card, CEPID, ceReqTypeLnk, ceReqClass1, ceReqLnkRead,
211 c, sizeof(LLData), (unsigned int *)&RcvBuffOffset);
213 return 0;