Auto merge with /home/aegl/GIT/ia64-test
[linux-2.6] / drivers / isdn / sc / packet.c
1 /* $Id: packet.c,v 1.5.8.1 2001/09/23 22:24:59 kai Exp $
2  *
3  * Copyright (C) 1996  SpellCaster Telecommunications Inc.
4  *
5  * This software may be used and distributed according to the terms
6  * of the GNU General Public License, incorporated herein by reference.
7  *
8  * For more information, please contact gpl-info@spellcast.com or write:
9  *
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
16  */
17
18 #include "includes.h"
19 #include "hardware.h"
20 #include "message.h"
21 #include "card.h"
22
23 extern board *sc_adapter[];
24 extern unsigned int cinst;
25
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 *);
32
33 int sndpkt(int devId, int channel, struct sk_buff *data)
34 {
35         LLData  ReqLnkWrite;
36         int status;
37         int card;
38         unsigned long len;
39
40         card = get_card_from_id(devId);
41
42         if(!IS_VALID_CARD(card)) {
43                 pr_debug("invalid param: %d is not a valid card id\n", card);
44                 return -ENODEV;
45         }
46
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);
53
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;
58         }
59
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;
64         }
65
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);
73
74         /*
75          * sendmessage
76          */
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);
81
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;
89         }
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);
99         }
100         return len;
101 }
102
103 void rcvpkt(int card, RspMessage *rcvmsg)
104 {
105         LLData newll;
106         struct sk_buff *skb;
107
108         if(!IS_VALID_CARD(card)) {
109                 pr_debug("invalid param: %d is not a valid card id\n", card);
110                 return;
111         }
112
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;
125                 }
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);
136
137         case 0x03:
138                 /*
139                  * Recycle the buffer
140                  */
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);
151         }
152
153 }
154
155 int setup_buffers(int card, int c)
156 {
157         unsigned int nBuffers, i, cBase;
158         unsigned int buffer_size;
159         LLData  RcvBuffOffset;
160
161         if(!IS_VALID_CARD(card)) {
162                 pr_debug("invalid param: %d is not a valid card id\n", card);
163                 return -ENODEV;
164         }
165
166         /*
167          * Calculate the buffer offsets (send/recv/send/recv)
168          */
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;
181         }
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);
195
196         /*
197          * Prep the receive buffers
198          */
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);
212         } 
213         return 0;
214 }
215