3 * Author Karsten Keil <kkeil@novell.com>
5 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
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.
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.
18 #include <linux/slab.h>
19 #include <linux/mISDNif.h>
20 #include <linux/kthread.h>
21 #include <linux/smp_lock.h>
27 _queue_message(struct mISDNstack
*st
, struct sk_buff
*skb
)
29 struct mISDNhead
*hh
= mISDN_HEAD_P(skb
);
31 if (*debug
& DEBUG_QUEUE_FUNC
)
32 printk(KERN_DEBUG
"%s prim(%x) id(%x) %p\n",
33 __func__
, hh
->prim
, hh
->id
, skb
);
34 skb_queue_tail(&st
->msgq
, skb
);
35 if (likely(!test_bit(mISDN_STACK_STOPPED
, &st
->status
))) {
36 test_and_set_bit(mISDN_STACK_WORK
, &st
->status
);
37 wake_up_interruptible(&st
->workq
);
42 mISDN_queue_message(struct mISDNchannel
*ch
, struct sk_buff
*skb
)
44 _queue_message(ch
->st
, skb
);
48 static struct mISDNchannel
*
49 get_channel4id(struct mISDNstack
*st
, u_int id
)
51 struct mISDNchannel
*ch
;
53 mutex_lock(&st
->lmutex
);
54 list_for_each_entry(ch
, &st
->layer2
, list
) {
60 mutex_unlock(&st
->lmutex
);
65 send_socklist(struct mISDN_sock_list
*sl
, struct sk_buff
*skb
)
67 struct hlist_node
*node
;
69 struct sk_buff
*cskb
= NULL
;
72 sk_for_each(sk
, node
, &sl
->head
) {
73 if (sk
->sk_state
!= MISDN_BOUND
)
76 cskb
= skb_copy(skb
, GFP_KERNEL
);
78 printk(KERN_WARNING
"%s no skb\n", __func__
);
81 if (!sock_queue_rcv_skb(sk
, cskb
))
84 read_unlock(&sl
->lock
);
90 send_layer2(struct mISDNstack
*st
, struct sk_buff
*skb
)
93 struct mISDNhead
*hh
= mISDN_HEAD_P(skb
);
94 struct mISDNchannel
*ch
;
99 mutex_lock(&st
->lmutex
);
100 if ((hh
->id
& MISDN_ID_ADDR_MASK
) == MISDN_ID_ANY
) { /* L2 for all */
101 list_for_each_entry(ch
, &st
->layer2
, list
) {
102 if (list_is_last(&ch
->list
, &st
->layer2
)) {
106 cskb
= skb_copy(skb
, GFP_KERNEL
);
109 ret
= ch
->send(ch
, cskb
);
111 if (*debug
& DEBUG_SEND_ERR
)
113 "%s ch%d prim(%x) addr(%x)"
116 hh
->prim
, ch
->addr
, ret
);
120 printk(KERN_WARNING
"%s ch%d addr %x no mem\n",
121 __func__
, ch
->nr
, ch
->addr
);
126 list_for_each_entry(ch
, &st
->layer2
, list
) {
127 if ((hh
->id
& MISDN_ID_ADDR_MASK
) == ch
->addr
) {
128 ret
= ch
->send(ch
, skb
);
134 ret
= st
->dev
->teimgr
->ctrl(st
->dev
->teimgr
, CHECK_DATA
, skb
);
137 else if (*debug
& DEBUG_SEND_ERR
)
139 "%s ch%d mgr prim(%x) addr(%x) err %d\n",
140 __func__
, ch
->nr
, hh
->prim
, ch
->addr
, ret
);
143 mutex_unlock(&st
->lmutex
);
149 send_msg_to_layer(struct mISDNstack
*st
, struct sk_buff
*skb
)
151 struct mISDNhead
*hh
= mISDN_HEAD_P(skb
);
152 struct mISDNchannel
*ch
;
155 lm
= hh
->prim
& MISDN_LAYERMASK
;
156 if (*debug
& DEBUG_QUEUE_FUNC
)
157 printk(KERN_DEBUG
"%s prim(%x) id(%x) %p\n",
158 __func__
, hh
->prim
, hh
->id
, skb
);
160 if (!hlist_empty(&st
->l1sock
.head
)) {
161 __net_timestamp(skb
);
162 send_socklist(&st
->l1sock
, skb
);
164 return st
->layer1
->send(st
->layer1
, skb
);
165 } else if (lm
== 0x2) {
166 if (!hlist_empty(&st
->l1sock
.head
))
167 send_socklist(&st
->l1sock
, skb
);
168 send_layer2(st
, skb
);
170 } else if (lm
== 0x4) {
171 ch
= get_channel4id(st
, hh
->id
);
173 return ch
->send(ch
, skb
);
176 "%s: dev(%s) prim(%x) id(%x) no channel\n",
177 __func__
, dev_name(&st
->dev
->dev
), hh
->prim
,
179 } else if (lm
== 0x8) {
181 ch
= get_channel4id(st
, hh
->id
);
183 return ch
->send(ch
, skb
);
186 "%s: dev(%s) prim(%x) id(%x) no channel\n",
187 __func__
, dev_name(&st
->dev
->dev
), hh
->prim
,
190 /* broadcast not handled yet */
191 printk(KERN_WARNING
"%s: dev(%s) prim %x not delivered\n",
192 __func__
, dev_name(&st
->dev
->dev
), hh
->prim
);
198 do_clear_stack(struct mISDNstack
*st
)
203 mISDNStackd(void *data
)
205 struct mISDNstack
*st
= data
;
211 sigfillset(¤t
->blocked
);
215 if (*debug
& DEBUG_MSG_THREAD
)
216 printk(KERN_DEBUG
"mISDNStackd %s started\n",
217 dev_name(&st
->dev
->dev
));
219 if (st
->notify
!= NULL
) {
220 complete(st
->notify
);
227 if (unlikely(test_bit(mISDN_STACK_STOPPED
, &st
->status
))) {
228 test_and_clear_bit(mISDN_STACK_WORK
, &st
->status
);
229 test_and_clear_bit(mISDN_STACK_RUNNING
, &st
->status
);
231 test_and_set_bit(mISDN_STACK_RUNNING
, &st
->status
);
232 while (test_bit(mISDN_STACK_WORK
, &st
->status
)) {
233 skb
= skb_dequeue(&st
->msgq
);
235 test_and_clear_bit(mISDN_STACK_WORK
,
237 /* test if a race happens */
238 skb
= skb_dequeue(&st
->msgq
);
241 test_and_set_bit(mISDN_STACK_WORK
,
244 #ifdef MISDN_MSG_STATS
247 err
= send_msg_to_layer(st
, skb
);
249 if (*debug
& DEBUG_SEND_ERR
)
251 "%s: %s prim(%x) id(%x) "
253 __func__
, dev_name(&st
->dev
->dev
),
254 mISDN_HEAD_PRIM(skb
),
255 mISDN_HEAD_ID(skb
), err
);
259 if (unlikely(test_bit(mISDN_STACK_STOPPED
,
261 test_and_clear_bit(mISDN_STACK_WORK
,
263 test_and_clear_bit(mISDN_STACK_RUNNING
,
268 if (test_bit(mISDN_STACK_CLEARING
, &st
->status
)) {
269 test_and_set_bit(mISDN_STACK_STOPPED
, &st
->status
);
270 test_and_clear_bit(mISDN_STACK_RUNNING
, &st
->status
);
272 test_and_clear_bit(mISDN_STACK_CLEARING
, &st
->status
);
273 test_and_set_bit(mISDN_STACK_RESTART
, &st
->status
);
275 if (test_and_clear_bit(mISDN_STACK_RESTART
, &st
->status
)) {
276 test_and_clear_bit(mISDN_STACK_STOPPED
, &st
->status
);
277 test_and_set_bit(mISDN_STACK_RUNNING
, &st
->status
);
278 if (!skb_queue_empty(&st
->msgq
))
279 test_and_set_bit(mISDN_STACK_WORK
,
282 if (test_bit(mISDN_STACK_ABORT
, &st
->status
))
284 if (st
->notify
!= NULL
) {
285 complete(st
->notify
);
288 #ifdef MISDN_MSG_STATS
291 test_and_clear_bit(mISDN_STACK_ACTIVE
, &st
->status
);
292 wait_event_interruptible(st
->workq
, (st
->status
&
293 mISDN_STACK_ACTION_MASK
));
294 if (*debug
& DEBUG_MSG_THREAD
)
295 printk(KERN_DEBUG
"%s: %s wake status %08lx\n",
296 __func__
, dev_name(&st
->dev
->dev
), st
->status
);
297 test_and_set_bit(mISDN_STACK_ACTIVE
, &st
->status
);
299 test_and_clear_bit(mISDN_STACK_WAKEUP
, &st
->status
);
301 if (test_bit(mISDN_STACK_STOPPED
, &st
->status
)) {
302 test_and_clear_bit(mISDN_STACK_RUNNING
, &st
->status
);
303 #ifdef MISDN_MSG_STATS
308 #ifdef MISDN_MSG_STATS
309 printk(KERN_DEBUG
"mISDNStackd daemon for %s proceed %d "
310 "msg %d sleep %d stopped\n",
311 dev_name(&st
->dev
->dev
), st
->msg_cnt
, st
->sleep_cnt
,
314 "mISDNStackd daemon for %s utime(%ld) stime(%ld)\n",
315 dev_name(&st
->dev
->dev
), st
->thread
->utime
, st
->thread
->stime
);
317 "mISDNStackd daemon for %s nvcsw(%ld) nivcsw(%ld)\n",
318 dev_name(&st
->dev
->dev
), st
->thread
->nvcsw
, st
->thread
->nivcsw
);
319 printk(KERN_DEBUG
"mISDNStackd daemon for %s killed now\n",
320 dev_name(&st
->dev
->dev
));
322 test_and_set_bit(mISDN_STACK_KILLED
, &st
->status
);
323 test_and_clear_bit(mISDN_STACK_RUNNING
, &st
->status
);
324 test_and_clear_bit(mISDN_STACK_ACTIVE
, &st
->status
);
325 test_and_clear_bit(mISDN_STACK_ABORT
, &st
->status
);
326 skb_queue_purge(&st
->msgq
);
328 if (st
->notify
!= NULL
) {
329 complete(st
->notify
);
336 l1_receive(struct mISDNchannel
*ch
, struct sk_buff
*skb
)
340 __net_timestamp(skb
);
341 _queue_message(ch
->st
, skb
);
346 set_channel_address(struct mISDNchannel
*ch
, u_int sapi
, u_int tei
)
348 ch
->addr
= sapi
| (tei
<< 8);
352 __add_layer2(struct mISDNchannel
*ch
, struct mISDNstack
*st
)
354 list_add_tail(&ch
->list
, &st
->layer2
);
358 add_layer2(struct mISDNchannel
*ch
, struct mISDNstack
*st
)
360 mutex_lock(&st
->lmutex
);
361 __add_layer2(ch
, st
);
362 mutex_unlock(&st
->lmutex
);
366 st_own_ctrl(struct mISDNchannel
*ch
, u_int cmd
, void *arg
)
368 if (!ch
->st
|| !ch
->st
->layer1
)
370 return ch
->st
->layer1
->ctrl(ch
->st
->layer1
, cmd
, arg
);
374 create_stack(struct mISDNdevice
*dev
)
376 struct mISDNstack
*newst
;
378 DECLARE_COMPLETION_ONSTACK(done
);
380 newst
= kzalloc(sizeof(struct mISDNstack
), GFP_KERNEL
);
382 printk(KERN_ERR
"kmalloc mISDN_stack failed\n");
386 INIT_LIST_HEAD(&newst
->layer2
);
387 INIT_HLIST_HEAD(&newst
->l1sock
.head
);
388 rwlock_init(&newst
->l1sock
.lock
);
389 init_waitqueue_head(&newst
->workq
);
390 skb_queue_head_init(&newst
->msgq
);
391 mutex_init(&newst
->lmutex
);
393 err
= create_teimanager(dev
);
395 printk(KERN_ERR
"kmalloc teimanager failed\n");
399 dev
->teimgr
->peer
= &newst
->own
;
400 dev
->teimgr
->recv
= mISDN_queue_message
;
401 dev
->teimgr
->st
= newst
;
402 newst
->layer1
= &dev
->D
;
403 dev
->D
.recv
= l1_receive
;
404 dev
->D
.peer
= &newst
->own
;
405 newst
->own
.st
= newst
;
406 newst
->own
.ctrl
= st_own_ctrl
;
407 newst
->own
.send
= mISDN_queue_message
;
408 newst
->own
.recv
= mISDN_queue_message
;
409 if (*debug
& DEBUG_CORE_FUNC
)
410 printk(KERN_DEBUG
"%s: st(%s)\n", __func__
,
411 dev_name(&newst
->dev
->dev
));
412 newst
->notify
= &done
;
413 newst
->thread
= kthread_run(mISDNStackd
, (void *)newst
, "mISDN_%s",
414 dev_name(&newst
->dev
->dev
));
415 if (IS_ERR(newst
->thread
)) {
416 err
= PTR_ERR(newst
->thread
);
418 "mISDN:cannot create kernel thread for %s (%d)\n",
419 dev_name(&newst
->dev
->dev
), err
);
420 delete_teimanager(dev
->teimgr
);
423 wait_for_completion(&done
);
428 connect_layer1(struct mISDNdevice
*dev
, struct mISDNchannel
*ch
,
429 u_int protocol
, struct sockaddr_mISDN
*adr
)
431 struct mISDN_sock
*msk
= container_of(ch
, struct mISDN_sock
, ch
);
432 struct channel_req rq
;
436 if (*debug
& DEBUG_CORE_FUNC
)
437 printk(KERN_DEBUG
"%s: %s proto(%x) adr(%d %d %d %d)\n",
438 __func__
, dev_name(&dev
->dev
), protocol
, adr
->dev
,
439 adr
->channel
, adr
->sapi
, adr
->tei
);
445 ch
->recv
= mISDN_queue_message
;
446 ch
->peer
= &dev
->D
.st
->own
;
448 rq
.protocol
= protocol
;
449 rq
.adr
.channel
= adr
->channel
;
450 err
= dev
->D
.ctrl(&dev
->D
, OPEN_CHANNEL
, &rq
);
451 printk(KERN_DEBUG
"%s: ret %d (dev %d)\n", __func__
, err
,
455 write_lock_bh(&dev
->D
.st
->l1sock
.lock
);
456 sk_add_node(&msk
->sk
, &dev
->D
.st
->l1sock
.head
);
457 write_unlock_bh(&dev
->D
.st
->l1sock
.lock
);
466 connect_Bstack(struct mISDNdevice
*dev
, struct mISDNchannel
*ch
,
467 u_int protocol
, struct sockaddr_mISDN
*adr
)
469 struct channel_req rq
, rq2
;
471 struct Bprotocol
*bp
;
473 if (*debug
& DEBUG_CORE_FUNC
)
474 printk(KERN_DEBUG
"%s: %s proto(%x) adr(%d %d %d %d)\n",
475 __func__
, dev_name(&dev
->dev
), protocol
,
476 adr
->dev
, adr
->channel
, adr
->sapi
,
479 pmask
= 1 << (protocol
& ISDN_P_B_MASK
);
480 if (pmask
& dev
->Bprotocols
) {
481 rq
.protocol
= protocol
;
483 err
= dev
->D
.ctrl(&dev
->D
, OPEN_CHANNEL
, &rq
);
486 ch
->recv
= rq
.ch
->send
;
488 rq
.ch
->recv
= ch
->send
;
490 rq
.ch
->st
= dev
->D
.st
;
492 bp
= get_Bprotocol4mask(pmask
);
495 rq2
.protocol
= protocol
;
498 err
= bp
->create(&rq2
);
501 ch
->recv
= rq2
.ch
->send
;
503 rq2
.ch
->st
= dev
->D
.st
;
504 rq
.protocol
= rq2
.protocol
;
506 err
= dev
->D
.ctrl(&dev
->D
, OPEN_CHANNEL
, &rq
);
508 rq2
.ch
->ctrl(rq2
.ch
, CLOSE_CHANNEL
, NULL
);
511 rq2
.ch
->recv
= rq
.ch
->send
;
512 rq2
.ch
->peer
= rq
.ch
;
513 rq
.ch
->recv
= rq2
.ch
->send
;
514 rq
.ch
->peer
= rq2
.ch
;
515 rq
.ch
->st
= dev
->D
.st
;
517 ch
->protocol
= protocol
;
523 create_l2entity(struct mISDNdevice
*dev
, struct mISDNchannel
*ch
,
524 u_int protocol
, struct sockaddr_mISDN
*adr
)
526 struct channel_req rq
;
529 if (*debug
& DEBUG_CORE_FUNC
)
530 printk(KERN_DEBUG
"%s: %s proto(%x) adr(%d %d %d %d)\n",
531 __func__
, dev_name(&dev
->dev
), protocol
,
532 adr
->dev
, adr
->channel
, adr
->sapi
,
534 rq
.protocol
= ISDN_P_TE_S0
;
535 if (dev
->Dprotocols
& (1 << ISDN_P_TE_E1
))
536 rq
.protocol
= ISDN_P_TE_E1
;
539 rq
.protocol
= ISDN_P_NT_S0
;
540 if (dev
->Dprotocols
& (1 << ISDN_P_NT_E1
))
541 rq
.protocol
= ISDN_P_NT_E1
;
543 ch
->recv
= mISDN_queue_message
;
544 ch
->peer
= &dev
->D
.st
->own
;
547 err
= dev
->D
.ctrl(&dev
->D
, OPEN_CHANNEL
, &rq
);
548 printk(KERN_DEBUG
"%s: ret 1 %d\n", __func__
, err
);
551 rq
.protocol
= protocol
;
554 err
= dev
->teimgr
->ctrl(dev
->teimgr
, OPEN_CHANNEL
, &rq
);
555 printk(KERN_DEBUG
"%s: ret 2 %d\n", __func__
, err
);
557 if ((protocol
== ISDN_P_LAPD_NT
) && !rq
.ch
)
559 add_layer2(rq
.ch
, dev
->D
.st
);
560 rq
.ch
->recv
= mISDN_queue_message
;
561 rq
.ch
->peer
= &dev
->D
.st
->own
;
562 rq
.ch
->ctrl(rq
.ch
, OPEN_CHANNEL
, NULL
); /* can't fail */
566 err
= -EPROTONOSUPPORT
;
572 delete_channel(struct mISDNchannel
*ch
)
574 struct mISDN_sock
*msk
= container_of(ch
, struct mISDN_sock
, ch
);
575 struct mISDNchannel
*pch
;
578 printk(KERN_WARNING
"%s: no stack\n", __func__
);
581 if (*debug
& DEBUG_CORE_FUNC
)
582 printk(KERN_DEBUG
"%s: st(%s) protocol(%x)\n", __func__
,
583 dev_name(&ch
->st
->dev
->dev
), ch
->protocol
);
584 if (ch
->protocol
>= ISDN_P_B_START
) {
586 ch
->peer
->ctrl(ch
->peer
, CLOSE_CHANNEL
, NULL
);
591 switch (ch
->protocol
) {
596 write_lock_bh(&ch
->st
->l1sock
.lock
);
597 sk_del_node_init(&msk
->sk
);
598 write_unlock_bh(&ch
->st
->l1sock
.lock
);
599 ch
->st
->dev
->D
.ctrl(&ch
->st
->dev
->D
, CLOSE_CHANNEL
, NULL
);
602 pch
= get_channel4id(ch
->st
, ch
->nr
);
604 mutex_lock(&ch
->st
->lmutex
);
605 list_del(&pch
->list
);
606 mutex_unlock(&ch
->st
->lmutex
);
607 pch
->ctrl(pch
, CLOSE_CHANNEL
, NULL
);
608 pch
= ch
->st
->dev
->teimgr
;
609 pch
->ctrl(pch
, CLOSE_CHANNEL
, NULL
);
611 printk(KERN_WARNING
"%s: no l2 channel\n",
615 pch
= ch
->st
->dev
->teimgr
;
617 pch
->ctrl(pch
, CLOSE_CHANNEL
, NULL
);
619 printk(KERN_WARNING
"%s: no l2 channel\n",
629 delete_stack(struct mISDNdevice
*dev
)
631 struct mISDNstack
*st
= dev
->D
.st
;
632 DECLARE_COMPLETION_ONSTACK(done
);
634 if (*debug
& DEBUG_CORE_FUNC
)
635 printk(KERN_DEBUG
"%s: st(%s)\n", __func__
,
636 dev_name(&st
->dev
->dev
));
638 delete_teimanager(dev
->teimgr
);
641 printk(KERN_WARNING
"%s: notifier in use\n",
643 complete(st
->notify
);
646 test_and_set_bit(mISDN_STACK_ABORT
, &st
->status
);
647 test_and_set_bit(mISDN_STACK_WAKEUP
, &st
->status
);
648 wake_up_interruptible(&st
->workq
);
649 wait_for_completion(&done
);
651 if (!list_empty(&st
->layer2
))
652 printk(KERN_WARNING
"%s: layer2 list not empty\n",
654 if (!hlist_empty(&st
->l1sock
.head
))
655 printk(KERN_WARNING
"%s: layer1 list not empty\n",
661 mISDN_initstack(u_int
*dp
)