blob: e541b65f68badd836b948c73cff5095919e22721 [file] [log] [blame]
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001/*
2 *
3 * Author Karsten Keil <kkeil@novell.com>
4 *
5 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/gfp.h>
Karsten Keil1b2b03f2008-07-27 01:54:58 +020019#include <linux/module.h>
20#include <linux/mISDNhw.h>
21
22static void
23dchannel_bh(struct work_struct *ws)
24{
25 struct dchannel *dch = container_of(ws, struct dchannel, workq);
26 struct sk_buff *skb;
27 int err;
28
29 if (test_and_clear_bit(FLG_RECVQUEUE, &dch->Flags)) {
30 while ((skb = skb_dequeue(&dch->rqueue))) {
31 if (likely(dch->dev.D.peer)) {
32 err = dch->dev.D.recv(dch->dev.D.peer, skb);
33 if (err)
34 dev_kfree_skb(skb);
35 } else
36 dev_kfree_skb(skb);
37 }
38 }
39 if (test_and_clear_bit(FLG_PHCHANGE, &dch->Flags)) {
40 if (dch->phfunc)
41 dch->phfunc(dch);
42 }
43}
44
45static void
46bchannel_bh(struct work_struct *ws)
47{
48 struct bchannel *bch = container_of(ws, struct bchannel, workq);
49 struct sk_buff *skb;
50 int err;
51
52 if (test_and_clear_bit(FLG_RECVQUEUE, &bch->Flags)) {
53 while ((skb = skb_dequeue(&bch->rqueue))) {
Karsten Keil1b2b03f2008-07-27 01:54:58 +020054 bch->rcount--;
55 if (likely(bch->ch.peer)) {
56 err = bch->ch.recv(bch->ch.peer, skb);
57 if (err)
58 dev_kfree_skb(skb);
59 } else
60 dev_kfree_skb(skb);
61 }
62 }
63}
64
65int
66mISDN_initdchannel(struct dchannel *ch, int maxlen, void *phf)
67{
68 test_and_set_bit(FLG_HDLC, &ch->Flags);
69 ch->maxlen = maxlen;
70 ch->hw = NULL;
71 ch->rx_skb = NULL;
72 ch->tx_skb = NULL;
73 ch->tx_idx = 0;
74 ch->phfunc = phf;
75 skb_queue_head_init(&ch->squeue);
76 skb_queue_head_init(&ch->rqueue);
77 INIT_LIST_HEAD(&ch->dev.bchannels);
78 INIT_WORK(&ch->workq, dchannel_bh);
79 return 0;
80}
81EXPORT_SYMBOL(mISDN_initdchannel);
82
83int
Karsten Keil034005a2012-05-15 23:51:06 +000084mISDN_initbchannel(struct bchannel *ch, unsigned short maxlen,
85 unsigned short minlen)
Karsten Keil1b2b03f2008-07-27 01:54:58 +020086{
87 ch->Flags = 0;
Karsten Keil034005a2012-05-15 23:51:06 +000088 ch->minlen = minlen;
89 ch->next_minlen = minlen;
90 ch->init_minlen = minlen;
Karsten Keil1b2b03f2008-07-27 01:54:58 +020091 ch->maxlen = maxlen;
Karsten Keil034005a2012-05-15 23:51:06 +000092 ch->next_maxlen = maxlen;
93 ch->init_maxlen = maxlen;
Karsten Keil1b2b03f2008-07-27 01:54:58 +020094 ch->hw = NULL;
95 ch->rx_skb = NULL;
96 ch->tx_skb = NULL;
97 ch->tx_idx = 0;
98 skb_queue_head_init(&ch->rqueue);
99 ch->rcount = 0;
100 ch->next_skb = NULL;
101 INIT_WORK(&ch->workq, bchannel_bh);
102 return 0;
103}
104EXPORT_SYMBOL(mISDN_initbchannel);
105
106int
107mISDN_freedchannel(struct dchannel *ch)
108{
109 if (ch->tx_skb) {
110 dev_kfree_skb(ch->tx_skb);
111 ch->tx_skb = NULL;
112 }
113 if (ch->rx_skb) {
114 dev_kfree_skb(ch->rx_skb);
115 ch->rx_skb = NULL;
116 }
117 skb_queue_purge(&ch->squeue);
118 skb_queue_purge(&ch->rqueue);
Tejun Heo0d26aa72010-12-24 15:59:07 +0100119 flush_work_sync(&ch->workq);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200120 return 0;
121}
122EXPORT_SYMBOL(mISDN_freedchannel);
123
Karsten Keilfb286f02009-07-09 10:02:29 +0200124void
125mISDN_clear_bchannel(struct bchannel *ch)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200126{
127 if (ch->tx_skb) {
128 dev_kfree_skb(ch->tx_skb);
129 ch->tx_skb = NULL;
130 }
Karsten Keilfb286f02009-07-09 10:02:29 +0200131 ch->tx_idx = 0;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200132 if (ch->rx_skb) {
133 dev_kfree_skb(ch->rx_skb);
134 ch->rx_skb = NULL;
135 }
136 if (ch->next_skb) {
137 dev_kfree_skb(ch->next_skb);
138 ch->next_skb = NULL;
139 }
Karsten Keilfb286f02009-07-09 10:02:29 +0200140 test_and_clear_bit(FLG_TX_BUSY, &ch->Flags);
141 test_and_clear_bit(FLG_TX_NEXT, &ch->Flags);
142 test_and_clear_bit(FLG_ACTIVE, &ch->Flags);
Karsten Keil6d1ee482012-05-15 23:51:07 +0000143 test_and_clear_bit(FLG_FILLEMPTY, &ch->Flags);
144 test_and_clear_bit(FLG_TX_EMPTY, &ch->Flags);
Karsten Keil034005a2012-05-15 23:51:06 +0000145 ch->minlen = ch->init_minlen;
146 ch->next_minlen = ch->init_minlen;
147 ch->maxlen = ch->init_maxlen;
148 ch->next_maxlen = ch->init_maxlen;
Karsten Keilfb286f02009-07-09 10:02:29 +0200149}
150EXPORT_SYMBOL(mISDN_clear_bchannel);
151
152int
153mISDN_freebchannel(struct bchannel *ch)
154{
155 mISDN_clear_bchannel(ch);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200156 skb_queue_purge(&ch->rqueue);
157 ch->rcount = 0;
Tejun Heo0d26aa72010-12-24 15:59:07 +0100158 flush_work_sync(&ch->workq);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200159 return 0;
160}
161EXPORT_SYMBOL(mISDN_freebchannel);
162
Karsten Keil034005a2012-05-15 23:51:06 +0000163int
164mISDN_ctrl_bchannel(struct bchannel *bch, struct mISDN_ctrl_req *cq)
165{
166 int ret = 0;
167
168 switch (cq->op) {
169 case MISDN_CTRL_GETOP:
Karsten Keil6d1ee482012-05-15 23:51:07 +0000170 cq->op = MISDN_CTRL_RX_BUFFER | MISDN_CTRL_FILL_EMPTY;
171 break;
172 case MISDN_CTRL_FILL_EMPTY:
173 if (cq->p1) {
174 memset(bch->fill, cq->p2 & 0xff, MISDN_BCH_FILL_SIZE);
175 test_and_set_bit(FLG_FILLEMPTY, &bch->Flags);
176 } else {
177 test_and_clear_bit(FLG_FILLEMPTY, &bch->Flags);
178 }
Karsten Keil034005a2012-05-15 23:51:06 +0000179 break;
180 case MISDN_CTRL_RX_BUFFER:
181 if (cq->p2 > MISDN_CTRL_RX_SIZE_IGNORE)
182 bch->next_maxlen = cq->p2;
183 if (cq->p1 > MISDN_CTRL_RX_SIZE_IGNORE)
184 bch->next_minlen = cq->p1;
185 /* we return the old values */
186 cq->p1 = bch->minlen;
187 cq->p2 = bch->maxlen;
188 break;
189 default:
190 pr_info("mISDN unhandled control %x operation\n", cq->op);
191 ret = -EINVAL;
192 break;
193 }
194 return ret;
195}
196EXPORT_SYMBOL(mISDN_ctrl_bchannel);
197
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200198static inline u_int
199get_sapi_tei(u_char *p)
200{
201 u_int sapi, tei;
202
203 sapi = *p >> 2;
204 tei = p[1] >> 1;
205 return sapi | (tei << 8);
206}
207
208void
209recv_Dchannel(struct dchannel *dch)
210{
211 struct mISDNhead *hh;
212
213 if (dch->rx_skb->len < 2) { /* at least 2 for sapi / tei */
214 dev_kfree_skb(dch->rx_skb);
215 dch->rx_skb = NULL;
216 return;
217 }
218 hh = mISDN_HEAD_P(dch->rx_skb);
219 hh->prim = PH_DATA_IND;
220 hh->id = get_sapi_tei(dch->rx_skb->data);
221 skb_queue_tail(&dch->rqueue, dch->rx_skb);
222 dch->rx_skb = NULL;
223 schedule_event(dch, FLG_RECVQUEUE);
224}
225EXPORT_SYMBOL(recv_Dchannel);
226
227void
Martin Bachem1f28fa12008-09-03 15:17:45 +0200228recv_Echannel(struct dchannel *ech, struct dchannel *dch)
229{
230 struct mISDNhead *hh;
231
232 if (ech->rx_skb->len < 2) { /* at least 2 for sapi / tei */
233 dev_kfree_skb(ech->rx_skb);
234 ech->rx_skb = NULL;
235 return;
236 }
237 hh = mISDN_HEAD_P(ech->rx_skb);
238 hh->prim = PH_DATA_E_IND;
239 hh->id = get_sapi_tei(ech->rx_skb->data);
240 skb_queue_tail(&dch->rqueue, ech->rx_skb);
241 ech->rx_skb = NULL;
242 schedule_event(dch, FLG_RECVQUEUE);
243}
244EXPORT_SYMBOL(recv_Echannel);
245
246void
Karsten Keil034005a2012-05-15 23:51:06 +0000247recv_Bchannel(struct bchannel *bch, unsigned int id, bool force)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200248{
249 struct mISDNhead *hh;
250
Karsten Keil7206e652012-05-15 23:51:05 +0000251 /* if allocation did fail upper functions still may call us */
252 if (unlikely(!bch->rx_skb))
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200253 return;
Karsten Keil7206e652012-05-15 23:51:05 +0000254 if (unlikely(!bch->rx_skb->len)) {
255 /* we have no data to send - this may happen after recovery
256 * from overflow or too small allocation.
257 * We need to free the buffer here */
258 dev_kfree_skb(bch->rx_skb);
259 bch->rx_skb = NULL;
260 } else {
Karsten Keil034005a2012-05-15 23:51:06 +0000261 if (test_bit(FLG_TRANSPARENT, &bch->Flags) &&
262 (bch->rx_skb->len < bch->minlen) && !force)
263 return;
Karsten Keil7206e652012-05-15 23:51:05 +0000264 hh = mISDN_HEAD_P(bch->rx_skb);
265 hh->prim = PH_DATA_IND;
266 hh->id = id;
267 if (bch->rcount >= 64) {
268 printk(KERN_WARNING
269 "B%d receive queue overflow - flushing!\n",
270 bch->nr);
271 skb_queue_purge(&bch->rqueue);
272 }
273 bch->rcount++;
274 skb_queue_tail(&bch->rqueue, bch->rx_skb);
275 bch->rx_skb = NULL;
276 schedule_event(bch, FLG_RECVQUEUE);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200277 }
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200278}
279EXPORT_SYMBOL(recv_Bchannel);
280
281void
282recv_Dchannel_skb(struct dchannel *dch, struct sk_buff *skb)
283{
284 skb_queue_tail(&dch->rqueue, skb);
285 schedule_event(dch, FLG_RECVQUEUE);
286}
287EXPORT_SYMBOL(recv_Dchannel_skb);
288
289void
290recv_Bchannel_skb(struct bchannel *bch, struct sk_buff *skb)
291{
292 if (bch->rcount >= 64) {
Andreas Eversberg11618492008-08-06 19:13:07 +0200293 printk(KERN_WARNING "B-channel %p receive queue overflow, "
Joe Perches475be4d2012-02-19 19:52:38 -0800294 "flushing!\n", bch);
Andreas Eversberg11618492008-08-06 19:13:07 +0200295 skb_queue_purge(&bch->rqueue);
296 bch->rcount = 0;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200297 }
298 bch->rcount++;
299 skb_queue_tail(&bch->rqueue, skb);
300 schedule_event(bch, FLG_RECVQUEUE);
301}
302EXPORT_SYMBOL(recv_Bchannel_skb);
303
304static void
305confirm_Dsend(struct dchannel *dch)
306{
307 struct sk_buff *skb;
308
309 skb = _alloc_mISDN_skb(PH_DATA_CNF, mISDN_HEAD_ID(dch->tx_skb),
Joe Perches475be4d2012-02-19 19:52:38 -0800310 0, NULL, GFP_ATOMIC);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200311 if (!skb) {
312 printk(KERN_ERR "%s: no skb id %x\n", __func__,
Joe Perches475be4d2012-02-19 19:52:38 -0800313 mISDN_HEAD_ID(dch->tx_skb));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200314 return;
315 }
316 skb_queue_tail(&dch->rqueue, skb);
317 schedule_event(dch, FLG_RECVQUEUE);
318}
319
320int
321get_next_dframe(struct dchannel *dch)
322{
323 dch->tx_idx = 0;
324 dch->tx_skb = skb_dequeue(&dch->squeue);
325 if (dch->tx_skb) {
326 confirm_Dsend(dch);
327 return 1;
328 }
329 dch->tx_skb = NULL;
330 test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
331 return 0;
332}
333EXPORT_SYMBOL(get_next_dframe);
334
Karsten Keil8bfddfb2012-05-15 23:51:02 +0000335static void
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200336confirm_Bsend(struct bchannel *bch)
337{
338 struct sk_buff *skb;
339
Andreas Eversberg11618492008-08-06 19:13:07 +0200340 if (bch->rcount >= 64) {
341 printk(KERN_WARNING "B-channel %p receive queue overflow, "
Joe Perches475be4d2012-02-19 19:52:38 -0800342 "flushing!\n", bch);
Andreas Eversberg11618492008-08-06 19:13:07 +0200343 skb_queue_purge(&bch->rqueue);
344 bch->rcount = 0;
345 }
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200346 skb = _alloc_mISDN_skb(PH_DATA_CNF, mISDN_HEAD_ID(bch->tx_skb),
Joe Perches475be4d2012-02-19 19:52:38 -0800347 0, NULL, GFP_ATOMIC);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200348 if (!skb) {
349 printk(KERN_ERR "%s: no skb id %x\n", __func__,
Joe Perches475be4d2012-02-19 19:52:38 -0800350 mISDN_HEAD_ID(bch->tx_skb));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200351 return;
352 }
353 bch->rcount++;
354 skb_queue_tail(&bch->rqueue, skb);
355 schedule_event(bch, FLG_RECVQUEUE);
356}
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200357
358int
359get_next_bframe(struct bchannel *bch)
360{
361 bch->tx_idx = 0;
362 if (test_bit(FLG_TX_NEXT, &bch->Flags)) {
363 bch->tx_skb = bch->next_skb;
364 if (bch->tx_skb) {
365 bch->next_skb = NULL;
366 test_and_clear_bit(FLG_TX_NEXT, &bch->Flags);
Karsten Keil8bfddfb2012-05-15 23:51:02 +0000367 /* confirm imediately to allow next data */
368 confirm_Bsend(bch);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200369 return 1;
370 } else {
371 test_and_clear_bit(FLG_TX_NEXT, &bch->Flags);
372 printk(KERN_WARNING "B TX_NEXT without skb\n");
373 }
374 }
375 bch->tx_skb = NULL;
376 test_and_clear_bit(FLG_TX_BUSY, &bch->Flags);
377 return 0;
378}
379EXPORT_SYMBOL(get_next_bframe);
380
381void
382queue_ch_frame(struct mISDNchannel *ch, u_int pr, int id, struct sk_buff *skb)
383{
384 struct mISDNhead *hh;
385
386 if (!skb) {
387 _queue_data(ch, pr, id, 0, NULL, GFP_ATOMIC);
388 } else {
389 if (ch->peer) {
390 hh = mISDN_HEAD_P(skb);
391 hh->prim = pr;
392 hh->id = id;
393 if (!ch->recv(ch->peer, skb))
394 return;
395 }
396 dev_kfree_skb(skb);
397 }
398}
399EXPORT_SYMBOL(queue_ch_frame);
400
401int
402dchannel_senddata(struct dchannel *ch, struct sk_buff *skb)
403{
404 /* check oversize */
405 if (skb->len <= 0) {
406 printk(KERN_WARNING "%s: skb too small\n", __func__);
407 return -EINVAL;
408 }
409 if (skb->len > ch->maxlen) {
410 printk(KERN_WARNING "%s: skb too large(%d/%d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800411 __func__, skb->len, ch->maxlen);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200412 return -EINVAL;
413 }
414 /* HW lock must be obtained */
415 if (test_and_set_bit(FLG_TX_BUSY, &ch->Flags)) {
416 skb_queue_tail(&ch->squeue, skb);
417 return 0;
418 } else {
419 /* write to fifo */
420 ch->tx_skb = skb;
421 ch->tx_idx = 0;
422 return 1;
423 }
424}
425EXPORT_SYMBOL(dchannel_senddata);
426
427int
428bchannel_senddata(struct bchannel *ch, struct sk_buff *skb)
429{
430
431 /* check oversize */
432 if (skb->len <= 0) {
433 printk(KERN_WARNING "%s: skb too small\n", __func__);
434 return -EINVAL;
435 }
436 if (skb->len > ch->maxlen) {
437 printk(KERN_WARNING "%s: skb too large(%d/%d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800438 __func__, skb->len, ch->maxlen);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200439 return -EINVAL;
440 }
441 /* HW lock must be obtained */
442 /* check for pending next_skb */
443 if (ch->next_skb) {
444 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -0800445 "%s: next_skb exist ERROR (skb->len=%d next_skb->len=%d)\n",
446 __func__, skb->len, ch->next_skb->len);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200447 return -EBUSY;
448 }
449 if (test_and_set_bit(FLG_TX_BUSY, &ch->Flags)) {
450 test_and_set_bit(FLG_TX_NEXT, &ch->Flags);
451 ch->next_skb = skb;
452 return 0;
453 } else {
454 /* write to fifo */
455 ch->tx_skb = skb;
456 ch->tx_idx = 0;
Karsten Keil8bfddfb2012-05-15 23:51:02 +0000457 confirm_Bsend(ch);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200458 return 1;
459 }
460}
461EXPORT_SYMBOL(bchannel_senddata);
Karsten Keil7206e652012-05-15 23:51:05 +0000462
463/* The function allocates a new receive skb on demand with a size for the
464 * requirements of the current protocol. It returns the tailroom of the
465 * receive skb or an error.
466 */
467int
468bchannel_get_rxbuf(struct bchannel *bch, int reqlen)
469{
470 int len;
471
472 if (bch->rx_skb) {
473 len = skb_tailroom(bch->rx_skb);
474 if (len < reqlen) {
475 pr_warning("B%d no space for %d (only %d) bytes\n",
476 bch->nr, reqlen, len);
477 if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
478 /* send what we have now and try a new buffer */
Karsten Keil034005a2012-05-15 23:51:06 +0000479 recv_Bchannel(bch, 0, true);
Karsten Keil7206e652012-05-15 23:51:05 +0000480 } else {
481 /* on HDLC we have to drop too big frames */
482 return -EMSGSIZE;
483 }
484 } else {
485 return len;
486 }
487 }
Karsten Keil034005a2012-05-15 23:51:06 +0000488 /* update current min/max length first */
489 if (unlikely(bch->maxlen != bch->next_maxlen))
490 bch->maxlen = bch->next_maxlen;
491 if (unlikely(bch->minlen != bch->next_minlen))
492 bch->minlen = bch->next_minlen;
Karsten Keil7206e652012-05-15 23:51:05 +0000493 if (unlikely(reqlen > bch->maxlen))
494 return -EMSGSIZE;
Karsten Keil034005a2012-05-15 23:51:06 +0000495 if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
496 if (reqlen >= bch->minlen) {
497 len = reqlen;
498 } else {
499 len = 2 * bch->minlen;
500 if (len > bch->maxlen)
501 len = bch->maxlen;
502 }
503 } else {
504 /* with HDLC we do not know the length yet */
Karsten Keil7206e652012-05-15 23:51:05 +0000505 len = bch->maxlen;
Karsten Keil034005a2012-05-15 23:51:06 +0000506 }
Karsten Keil7206e652012-05-15 23:51:05 +0000507 bch->rx_skb = mI_alloc_skb(len, GFP_ATOMIC);
508 if (!bch->rx_skb) {
509 pr_warning("B%d receive no memory for %d bytes\n",
510 bch->nr, len);
511 len = -ENOMEM;
512 }
513 return len;
514}
515EXPORT_SYMBOL(bchannel_get_rxbuf);