Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 1 | /* |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 19 | #include <linux/mISDNif.h> |
| 20 | #include <linux/kthread.h> |
Frederic Weisbecker | 6fac482 | 2012-11-13 14:20:55 +0100 | [diff] [blame] | 21 | #include <linux/sched.h> |
Ingo Molnar | 32ef551 | 2017-02-05 11:48:36 +0100 | [diff] [blame] | 22 | #include <linux/sched/cputime.h> |
Ingo Molnar | f361bf4 | 2017-02-03 23:47:37 +0100 | [diff] [blame] | 23 | #include <linux/signal.h> |
| 24 | |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 25 | #include "core.h" |
| 26 | |
| 27 | static u_int *debug; |
| 28 | |
| 29 | static inline void |
| 30 | _queue_message(struct mISDNstack *st, struct sk_buff *skb) |
| 31 | { |
| 32 | struct mISDNhead *hh = mISDN_HEAD_P(skb); |
| 33 | |
| 34 | if (*debug & DEBUG_QUEUE_FUNC) |
| 35 | printk(KERN_DEBUG "%s prim(%x) id(%x) %p\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 36 | __func__, hh->prim, hh->id, skb); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 37 | skb_queue_tail(&st->msgq, skb); |
| 38 | if (likely(!test_bit(mISDN_STACK_STOPPED, &st->status))) { |
| 39 | test_and_set_bit(mISDN_STACK_WORK, &st->status); |
| 40 | wake_up_interruptible(&st->workq); |
| 41 | } |
| 42 | } |
| 43 | |
Hannes Eder | 5b83435 | 2008-12-12 21:15:17 -0800 | [diff] [blame] | 44 | static int |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 45 | mISDN_queue_message(struct mISDNchannel *ch, struct sk_buff *skb) |
| 46 | { |
| 47 | _queue_message(ch->st, skb); |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static struct mISDNchannel * |
| 52 | get_channel4id(struct mISDNstack *st, u_int id) |
| 53 | { |
| 54 | struct mISDNchannel *ch; |
| 55 | |
| 56 | mutex_lock(&st->lmutex); |
| 57 | list_for_each_entry(ch, &st->layer2, list) { |
| 58 | if (id == ch->nr) |
| 59 | goto unlock; |
| 60 | } |
| 61 | ch = NULL; |
| 62 | unlock: |
| 63 | mutex_unlock(&st->lmutex); |
| 64 | return ch; |
| 65 | } |
| 66 | |
| 67 | static void |
| 68 | send_socklist(struct mISDN_sock_list *sl, struct sk_buff *skb) |
| 69 | { |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 70 | struct sock *sk; |
| 71 | struct sk_buff *cskb = NULL; |
| 72 | |
| 73 | read_lock(&sl->lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 74 | sk_for_each(sk, &sl->head) { |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 75 | if (sk->sk_state != MISDN_BOUND) |
| 76 | continue; |
| 77 | if (!cskb) |
| 78 | cskb = skb_copy(skb, GFP_KERNEL); |
| 79 | if (!cskb) { |
| 80 | printk(KERN_WARNING "%s no skb\n", __func__); |
| 81 | break; |
| 82 | } |
| 83 | if (!sock_queue_rcv_skb(sk, cskb)) |
| 84 | cskb = NULL; |
| 85 | } |
| 86 | read_unlock(&sl->lock); |
| 87 | if (cskb) |
| 88 | dev_kfree_skb(cskb); |
| 89 | } |
| 90 | |
| 91 | static void |
| 92 | send_layer2(struct mISDNstack *st, struct sk_buff *skb) |
| 93 | { |
| 94 | struct sk_buff *cskb; |
| 95 | struct mISDNhead *hh = mISDN_HEAD_P(skb); |
| 96 | struct mISDNchannel *ch; |
| 97 | int ret; |
| 98 | |
| 99 | if (!st) |
| 100 | return; |
| 101 | mutex_lock(&st->lmutex); |
| 102 | if ((hh->id & MISDN_ID_ADDR_MASK) == MISDN_ID_ANY) { /* L2 for all */ |
| 103 | list_for_each_entry(ch, &st->layer2, list) { |
| 104 | if (list_is_last(&ch->list, &st->layer2)) { |
| 105 | cskb = skb; |
| 106 | skb = NULL; |
| 107 | } else { |
| 108 | cskb = skb_copy(skb, GFP_KERNEL); |
| 109 | } |
| 110 | if (cskb) { |
| 111 | ret = ch->send(ch, cskb); |
| 112 | if (ret) { |
| 113 | if (*debug & DEBUG_SEND_ERR) |
| 114 | printk(KERN_DEBUG |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 115 | "%s ch%d prim(%x) addr(%x)" |
| 116 | " err %d\n", |
| 117 | __func__, ch->nr, |
| 118 | hh->prim, ch->addr, ret); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 119 | dev_kfree_skb(cskb); |
| 120 | } |
| 121 | } else { |
| 122 | printk(KERN_WARNING "%s ch%d addr %x no mem\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 123 | __func__, ch->nr, ch->addr); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 124 | goto out; |
| 125 | } |
| 126 | } |
| 127 | } else { |
| 128 | list_for_each_entry(ch, &st->layer2, list) { |
| 129 | if ((hh->id & MISDN_ID_ADDR_MASK) == ch->addr) { |
| 130 | ret = ch->send(ch, skb); |
| 131 | if (!ret) |
| 132 | skb = NULL; |
| 133 | goto out; |
| 134 | } |
| 135 | } |
| 136 | ret = st->dev->teimgr->ctrl(st->dev->teimgr, CHECK_DATA, skb); |
| 137 | if (!ret) |
| 138 | skb = NULL; |
| 139 | else if (*debug & DEBUG_SEND_ERR) |
| 140 | printk(KERN_DEBUG |
Julia Lawall | 1b9faf5 | 2012-07-08 01:37:38 +0000 | [diff] [blame] | 141 | "%s mgr prim(%x) err %d\n", |
| 142 | __func__, hh->prim, ret); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 143 | } |
| 144 | out: |
| 145 | mutex_unlock(&st->lmutex); |
| 146 | if (skb) |
| 147 | dev_kfree_skb(skb); |
| 148 | } |
| 149 | |
| 150 | static inline int |
| 151 | send_msg_to_layer(struct mISDNstack *st, struct sk_buff *skb) |
| 152 | { |
| 153 | struct mISDNhead *hh = mISDN_HEAD_P(skb); |
| 154 | struct mISDNchannel *ch; |
| 155 | int lm; |
| 156 | |
| 157 | lm = hh->prim & MISDN_LAYERMASK; |
| 158 | if (*debug & DEBUG_QUEUE_FUNC) |
| 159 | printk(KERN_DEBUG "%s prim(%x) id(%x) %p\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 160 | __func__, hh->prim, hh->id, skb); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 161 | if (lm == 0x1) { |
| 162 | if (!hlist_empty(&st->l1sock.head)) { |
| 163 | __net_timestamp(skb); |
| 164 | send_socklist(&st->l1sock, skb); |
| 165 | } |
| 166 | return st->layer1->send(st->layer1, skb); |
| 167 | } else if (lm == 0x2) { |
| 168 | if (!hlist_empty(&st->l1sock.head)) |
| 169 | send_socklist(&st->l1sock, skb); |
| 170 | send_layer2(st, skb); |
| 171 | return 0; |
| 172 | } else if (lm == 0x4) { |
| 173 | ch = get_channel4id(st, hh->id); |
| 174 | if (ch) |
| 175 | return ch->send(ch, skb); |
| 176 | else |
| 177 | printk(KERN_WARNING |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 178 | "%s: dev(%s) prim(%x) id(%x) no channel\n", |
| 179 | __func__, dev_name(&st->dev->dev), hh->prim, |
| 180 | hh->id); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 181 | } else if (lm == 0x8) { |
| 182 | WARN_ON(lm == 0x8); |
| 183 | ch = get_channel4id(st, hh->id); |
| 184 | if (ch) |
| 185 | return ch->send(ch, skb); |
| 186 | else |
| 187 | printk(KERN_WARNING |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 188 | "%s: dev(%s) prim(%x) id(%x) no channel\n", |
| 189 | __func__, dev_name(&st->dev->dev), hh->prim, |
| 190 | hh->id); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 191 | } else { |
| 192 | /* broadcast not handled yet */ |
| 193 | printk(KERN_WARNING "%s: dev(%s) prim %x not delivered\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 194 | __func__, dev_name(&st->dev->dev), hh->prim); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 195 | } |
| 196 | return -ESRCH; |
| 197 | } |
| 198 | |
| 199 | static void |
| 200 | do_clear_stack(struct mISDNstack *st) |
| 201 | { |
| 202 | } |
| 203 | |
| 204 | static int |
| 205 | mISDNStackd(void *data) |
| 206 | { |
| 207 | struct mISDNstack *st = data; |
Frederic Weisbecker | 6fac482 | 2012-11-13 14:20:55 +0100 | [diff] [blame] | 208 | #ifdef MISDN_MSG_STATS |
Frederic Weisbecker | c0e7a50 | 2017-01-31 04:09:26 +0100 | [diff] [blame] | 209 | u64 utime, stime; |
Frederic Weisbecker | 6fac482 | 2012-11-13 14:20:55 +0100 | [diff] [blame] | 210 | #endif |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 211 | int err = 0; |
| 212 | |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 213 | sigfillset(¤t->blocked); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 214 | if (*debug & DEBUG_MSG_THREAD) |
Matthias Urlichs | 837468d | 2008-08-16 00:04:33 +0200 | [diff] [blame] | 215 | printk(KERN_DEBUG "mISDNStackd %s started\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 216 | dev_name(&st->dev->dev)); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 217 | |
| 218 | if (st->notify != NULL) { |
| 219 | complete(st->notify); |
| 220 | st->notify = NULL; |
| 221 | } |
| 222 | |
| 223 | for (;;) { |
| 224 | struct sk_buff *skb; |
| 225 | |
| 226 | if (unlikely(test_bit(mISDN_STACK_STOPPED, &st->status))) { |
| 227 | test_and_clear_bit(mISDN_STACK_WORK, &st->status); |
| 228 | test_and_clear_bit(mISDN_STACK_RUNNING, &st->status); |
| 229 | } else |
| 230 | test_and_set_bit(mISDN_STACK_RUNNING, &st->status); |
| 231 | while (test_bit(mISDN_STACK_WORK, &st->status)) { |
| 232 | skb = skb_dequeue(&st->msgq); |
| 233 | if (!skb) { |
| 234 | test_and_clear_bit(mISDN_STACK_WORK, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 235 | &st->status); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 236 | /* test if a race happens */ |
| 237 | skb = skb_dequeue(&st->msgq); |
| 238 | if (!skb) |
| 239 | continue; |
| 240 | test_and_set_bit(mISDN_STACK_WORK, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 241 | &st->status); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 242 | } |
| 243 | #ifdef MISDN_MSG_STATS |
| 244 | st->msg_cnt++; |
| 245 | #endif |
| 246 | err = send_msg_to_layer(st, skb); |
| 247 | if (unlikely(err)) { |
| 248 | if (*debug & DEBUG_SEND_ERR) |
| 249 | printk(KERN_DEBUG |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 250 | "%s: %s prim(%x) id(%x) " |
| 251 | "send call(%d)\n", |
| 252 | __func__, dev_name(&st->dev->dev), |
| 253 | mISDN_HEAD_PRIM(skb), |
| 254 | mISDN_HEAD_ID(skb), err); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 255 | dev_kfree_skb(skb); |
| 256 | continue; |
| 257 | } |
| 258 | if (unlikely(test_bit(mISDN_STACK_STOPPED, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 259 | &st->status))) { |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 260 | test_and_clear_bit(mISDN_STACK_WORK, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 261 | &st->status); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 262 | test_and_clear_bit(mISDN_STACK_RUNNING, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 263 | &st->status); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 264 | break; |
| 265 | } |
| 266 | } |
| 267 | if (test_bit(mISDN_STACK_CLEARING, &st->status)) { |
| 268 | test_and_set_bit(mISDN_STACK_STOPPED, &st->status); |
| 269 | test_and_clear_bit(mISDN_STACK_RUNNING, &st->status); |
| 270 | do_clear_stack(st); |
| 271 | test_and_clear_bit(mISDN_STACK_CLEARING, &st->status); |
| 272 | test_and_set_bit(mISDN_STACK_RESTART, &st->status); |
| 273 | } |
| 274 | if (test_and_clear_bit(mISDN_STACK_RESTART, &st->status)) { |
| 275 | test_and_clear_bit(mISDN_STACK_STOPPED, &st->status); |
| 276 | test_and_set_bit(mISDN_STACK_RUNNING, &st->status); |
| 277 | if (!skb_queue_empty(&st->msgq)) |
| 278 | test_and_set_bit(mISDN_STACK_WORK, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 279 | &st->status); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 280 | } |
| 281 | if (test_bit(mISDN_STACK_ABORT, &st->status)) |
| 282 | break; |
| 283 | if (st->notify != NULL) { |
| 284 | complete(st->notify); |
| 285 | st->notify = NULL; |
| 286 | } |
| 287 | #ifdef MISDN_MSG_STATS |
| 288 | st->sleep_cnt++; |
| 289 | #endif |
| 290 | test_and_clear_bit(mISDN_STACK_ACTIVE, &st->status); |
| 291 | wait_event_interruptible(st->workq, (st->status & |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 292 | mISDN_STACK_ACTION_MASK)); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 293 | if (*debug & DEBUG_MSG_THREAD) |
| 294 | printk(KERN_DEBUG "%s: %s wake status %08lx\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 295 | __func__, dev_name(&st->dev->dev), st->status); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 296 | test_and_set_bit(mISDN_STACK_ACTIVE, &st->status); |
| 297 | |
| 298 | test_and_clear_bit(mISDN_STACK_WAKEUP, &st->status); |
| 299 | |
| 300 | if (test_bit(mISDN_STACK_STOPPED, &st->status)) { |
| 301 | test_and_clear_bit(mISDN_STACK_RUNNING, &st->status); |
| 302 | #ifdef MISDN_MSG_STATS |
| 303 | st->stopped_cnt++; |
| 304 | #endif |
| 305 | } |
| 306 | } |
| 307 | #ifdef MISDN_MSG_STATS |
| 308 | printk(KERN_DEBUG "mISDNStackd daemon for %s proceed %d " |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 309 | "msg %d sleep %d stopped\n", |
| 310 | dev_name(&st->dev->dev), st->msg_cnt, st->sleep_cnt, |
| 311 | st->stopped_cnt); |
Frederic Weisbecker | c0e7a50 | 2017-01-31 04:09:26 +0100 | [diff] [blame] | 312 | task_cputime(st->thread, &utime, &stime); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 313 | printk(KERN_DEBUG |
Frederic Weisbecker | c0e7a50 | 2017-01-31 04:09:26 +0100 | [diff] [blame] | 314 | "mISDNStackd daemon for %s utime(%llu) stime(%llu)\n", |
Frederic Weisbecker | 6fac482 | 2012-11-13 14:20:55 +0100 | [diff] [blame] | 315 | dev_name(&st->dev->dev), utime, stime); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 316 | printk(KERN_DEBUG |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 317 | "mISDNStackd daemon for %s nvcsw(%ld) nivcsw(%ld)\n", |
| 318 | dev_name(&st->dev->dev), st->thread->nvcsw, st->thread->nivcsw); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 319 | printk(KERN_DEBUG "mISDNStackd daemon for %s killed now\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 320 | dev_name(&st->dev->dev)); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 321 | #endif |
| 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); |
| 327 | st->thread = NULL; |
| 328 | if (st->notify != NULL) { |
| 329 | complete(st->notify); |
| 330 | st->notify = NULL; |
| 331 | } |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | static int |
| 336 | l1_receive(struct mISDNchannel *ch, struct sk_buff *skb) |
| 337 | { |
| 338 | if (!ch->st) |
| 339 | return -ENODEV; |
| 340 | __net_timestamp(skb); |
| 341 | _queue_message(ch->st, skb); |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | void |
| 346 | set_channel_address(struct mISDNchannel *ch, u_int sapi, u_int tei) |
| 347 | { |
| 348 | ch->addr = sapi | (tei << 8); |
| 349 | } |
| 350 | |
| 351 | void |
| 352 | __add_layer2(struct mISDNchannel *ch, struct mISDNstack *st) |
| 353 | { |
| 354 | list_add_tail(&ch->list, &st->layer2); |
| 355 | } |
| 356 | |
| 357 | void |
| 358 | add_layer2(struct mISDNchannel *ch, struct mISDNstack *st) |
| 359 | { |
| 360 | mutex_lock(&st->lmutex); |
| 361 | __add_layer2(ch, st); |
| 362 | mutex_unlock(&st->lmutex); |
| 363 | } |
| 364 | |
| 365 | static int |
| 366 | st_own_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg) |
| 367 | { |
Dan Carpenter | 08cb3f6 | 2009-11-04 08:27:09 -0800 | [diff] [blame] | 368 | if (!ch->st || !ch->st->layer1) |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 369 | return -EINVAL; |
| 370 | return ch->st->layer1->ctrl(ch->st->layer1, cmd, arg); |
| 371 | } |
| 372 | |
| 373 | int |
| 374 | create_stack(struct mISDNdevice *dev) |
| 375 | { |
| 376 | struct mISDNstack *newst; |
| 377 | int err; |
| 378 | DECLARE_COMPLETION_ONSTACK(done); |
| 379 | |
| 380 | newst = kzalloc(sizeof(struct mISDNstack), GFP_KERNEL); |
| 381 | if (!newst) { |
| 382 | printk(KERN_ERR "kmalloc mISDN_stack failed\n"); |
| 383 | return -ENOMEM; |
| 384 | } |
| 385 | newst->dev = dev; |
| 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); |
| 392 | dev->D.st = newst; |
| 393 | err = create_teimanager(dev); |
| 394 | if (err) { |
| 395 | printk(KERN_ERR "kmalloc teimanager failed\n"); |
| 396 | kfree(newst); |
| 397 | return err; |
| 398 | } |
| 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) |
Matthias Urlichs | 837468d | 2008-08-16 00:04:33 +0200 | [diff] [blame] | 410 | printk(KERN_DEBUG "%s: st(%s)\n", __func__, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 411 | dev_name(&newst->dev->dev)); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 412 | newst->notify = &done; |
| 413 | newst->thread = kthread_run(mISDNStackd, (void *)newst, "mISDN_%s", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 414 | dev_name(&newst->dev->dev)); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 415 | if (IS_ERR(newst->thread)) { |
| 416 | err = PTR_ERR(newst->thread); |
| 417 | printk(KERN_ERR |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 418 | "mISDN:cannot create kernel thread for %s (%d)\n", |
| 419 | dev_name(&newst->dev->dev), err); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 420 | delete_teimanager(dev->teimgr); |
| 421 | kfree(newst); |
| 422 | } else |
| 423 | wait_for_completion(&done); |
| 424 | return err; |
| 425 | } |
| 426 | |
| 427 | int |
| 428 | connect_layer1(struct mISDNdevice *dev, struct mISDNchannel *ch, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 429 | u_int protocol, struct sockaddr_mISDN *adr) |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 430 | { |
| 431 | struct mISDN_sock *msk = container_of(ch, struct mISDN_sock, ch); |
| 432 | struct channel_req rq; |
| 433 | int err; |
| 434 | |
| 435 | |
| 436 | if (*debug & DEBUG_CORE_FUNC) |
| 437 | printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 438 | __func__, dev_name(&dev->dev), protocol, adr->dev, |
| 439 | adr->channel, adr->sapi, adr->tei); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 440 | switch (protocol) { |
| 441 | case ISDN_P_NT_S0: |
| 442 | case ISDN_P_NT_E1: |
| 443 | case ISDN_P_TE_S0: |
| 444 | case ISDN_P_TE_E1: |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 445 | ch->recv = mISDN_queue_message; |
| 446 | ch->peer = &dev->D.st->own; |
| 447 | ch->st = dev->D.st; |
| 448 | rq.protocol = protocol; |
Martin Bachem | 1f28fa19 | 2008-09-03 15:17:45 +0200 | [diff] [blame] | 449 | rq.adr.channel = adr->channel; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 450 | err = dev->D.ctrl(&dev->D, OPEN_CHANNEL, &rq); |
Andreas Eversberg | 9a81255 | 2008-09-14 14:42:18 +0200 | [diff] [blame] | 451 | printk(KERN_DEBUG "%s: ret %d (dev %d)\n", __func__, err, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 452 | dev->id); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 453 | if (err) |
| 454 | return 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); |
| 458 | break; |
| 459 | default: |
| 460 | return -ENOPROTOOPT; |
| 461 | } |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | int |
| 466 | connect_Bstack(struct mISDNdevice *dev, struct mISDNchannel *ch, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 467 | u_int protocol, struct sockaddr_mISDN *adr) |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 468 | { |
| 469 | struct channel_req rq, rq2; |
| 470 | int pmask, err; |
| 471 | struct Bprotocol *bp; |
| 472 | |
| 473 | if (*debug & DEBUG_CORE_FUNC) |
| 474 | printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 475 | __func__, dev_name(&dev->dev), protocol, |
| 476 | adr->dev, adr->channel, adr->sapi, |
| 477 | adr->tei); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 478 | ch->st = dev->D.st; |
| 479 | pmask = 1 << (protocol & ISDN_P_B_MASK); |
| 480 | if (pmask & dev->Bprotocols) { |
| 481 | rq.protocol = protocol; |
| 482 | rq.adr = *adr; |
| 483 | err = dev->D.ctrl(&dev->D, OPEN_CHANNEL, &rq); |
| 484 | if (err) |
| 485 | return err; |
| 486 | ch->recv = rq.ch->send; |
| 487 | ch->peer = rq.ch; |
| 488 | rq.ch->recv = ch->send; |
| 489 | rq.ch->peer = ch; |
| 490 | rq.ch->st = dev->D.st; |
| 491 | } else { |
| 492 | bp = get_Bprotocol4mask(pmask); |
| 493 | if (!bp) |
| 494 | return -ENOPROTOOPT; |
| 495 | rq2.protocol = protocol; |
| 496 | rq2.adr = *adr; |
| 497 | rq2.ch = ch; |
| 498 | err = bp->create(&rq2); |
| 499 | if (err) |
| 500 | return err; |
| 501 | ch->recv = rq2.ch->send; |
| 502 | ch->peer = rq2.ch; |
| 503 | rq2.ch->st = dev->D.st; |
| 504 | rq.protocol = rq2.protocol; |
| 505 | rq.adr = *adr; |
| 506 | err = dev->D.ctrl(&dev->D, OPEN_CHANNEL, &rq); |
| 507 | if (err) { |
| 508 | rq2.ch->ctrl(rq2.ch, CLOSE_CHANNEL, NULL); |
| 509 | return err; |
| 510 | } |
| 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; |
| 516 | } |
| 517 | ch->protocol = protocol; |
| 518 | ch->nr = rq.ch->nr; |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | int |
| 523 | create_l2entity(struct mISDNdevice *dev, struct mISDNchannel *ch, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 524 | u_int protocol, struct sockaddr_mISDN *adr) |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 525 | { |
| 526 | struct channel_req rq; |
| 527 | int err; |
| 528 | |
| 529 | if (*debug & DEBUG_CORE_FUNC) |
| 530 | printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 531 | __func__, dev_name(&dev->dev), protocol, |
| 532 | adr->dev, adr->channel, adr->sapi, |
| 533 | adr->tei); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 534 | rq.protocol = ISDN_P_TE_S0; |
| 535 | if (dev->Dprotocols & (1 << ISDN_P_TE_E1)) |
| 536 | rq.protocol = ISDN_P_TE_E1; |
| 537 | switch (protocol) { |
| 538 | case ISDN_P_LAPD_NT: |
| 539 | rq.protocol = ISDN_P_NT_S0; |
| 540 | if (dev->Dprotocols & (1 << ISDN_P_NT_E1)) |
| 541 | rq.protocol = ISDN_P_NT_E1; |
| 542 | case ISDN_P_LAPD_TE: |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 543 | ch->recv = mISDN_queue_message; |
| 544 | ch->peer = &dev->D.st->own; |
| 545 | ch->st = dev->D.st; |
| 546 | rq.adr.channel = 0; |
| 547 | err = dev->D.ctrl(&dev->D, OPEN_CHANNEL, &rq); |
| 548 | printk(KERN_DEBUG "%s: ret 1 %d\n", __func__, err); |
| 549 | if (err) |
| 550 | break; |
| 551 | rq.protocol = protocol; |
| 552 | rq.adr = *adr; |
| 553 | rq.ch = ch; |
| 554 | err = dev->teimgr->ctrl(dev->teimgr, OPEN_CHANNEL, &rq); |
| 555 | printk(KERN_DEBUG "%s: ret 2 %d\n", __func__, err); |
| 556 | if (!err) { |
| 557 | if ((protocol == ISDN_P_LAPD_NT) && !rq.ch) |
| 558 | break; |
| 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 */ |
| 563 | } |
| 564 | break; |
| 565 | default: |
| 566 | err = -EPROTONOSUPPORT; |
| 567 | } |
| 568 | return err; |
| 569 | } |
| 570 | |
| 571 | void |
| 572 | delete_channel(struct mISDNchannel *ch) |
| 573 | { |
| 574 | struct mISDN_sock *msk = container_of(ch, struct mISDN_sock, ch); |
| 575 | struct mISDNchannel *pch; |
| 576 | |
| 577 | if (!ch->st) { |
| 578 | printk(KERN_WARNING "%s: no stack\n", __func__); |
| 579 | return; |
| 580 | } |
| 581 | if (*debug & DEBUG_CORE_FUNC) |
| 582 | printk(KERN_DEBUG "%s: st(%s) protocol(%x)\n", __func__, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 583 | dev_name(&ch->st->dev->dev), ch->protocol); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 584 | if (ch->protocol >= ISDN_P_B_START) { |
| 585 | if (ch->peer) { |
| 586 | ch->peer->ctrl(ch->peer, CLOSE_CHANNEL, NULL); |
| 587 | ch->peer = NULL; |
| 588 | } |
| 589 | return; |
| 590 | } |
| 591 | switch (ch->protocol) { |
| 592 | case ISDN_P_NT_S0: |
| 593 | case ISDN_P_TE_S0: |
| 594 | case ISDN_P_NT_E1: |
| 595 | case ISDN_P_TE_E1: |
| 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); |
| 600 | break; |
| 601 | case ISDN_P_LAPD_TE: |
| 602 | pch = get_channel4id(ch->st, ch->nr); |
| 603 | if (pch) { |
| 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); |
| 610 | } else |
| 611 | printk(KERN_WARNING "%s: no l2 channel\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 612 | __func__); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 613 | break; |
| 614 | case ISDN_P_LAPD_NT: |
| 615 | pch = ch->st->dev->teimgr; |
| 616 | if (pch) { |
| 617 | pch->ctrl(pch, CLOSE_CHANNEL, NULL); |
| 618 | } else |
| 619 | printk(KERN_WARNING "%s: no l2 channel\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 620 | __func__); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 621 | break; |
| 622 | default: |
| 623 | break; |
| 624 | } |
| 625 | return; |
| 626 | } |
| 627 | |
| 628 | void |
| 629 | delete_stack(struct mISDNdevice *dev) |
| 630 | { |
| 631 | struct mISDNstack *st = dev->D.st; |
| 632 | DECLARE_COMPLETION_ONSTACK(done); |
| 633 | |
| 634 | if (*debug & DEBUG_CORE_FUNC) |
| 635 | printk(KERN_DEBUG "%s: st(%s)\n", __func__, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 636 | dev_name(&st->dev->dev)); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 637 | if (dev->teimgr) |
| 638 | delete_teimanager(dev->teimgr); |
| 639 | if (st->thread) { |
| 640 | if (st->notify) { |
| 641 | printk(KERN_WARNING "%s: notifier in use\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 642 | __func__); |
| 643 | complete(st->notify); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 644 | } |
| 645 | st->notify = &done; |
| 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); |
| 650 | } |
| 651 | if (!list_empty(&st->layer2)) |
| 652 | printk(KERN_WARNING "%s: layer2 list not empty\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 653 | __func__); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 654 | if (!hlist_empty(&st->l1sock.head)) |
| 655 | printk(KERN_WARNING "%s: layer1 list not empty\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 656 | __func__); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 657 | kfree(st); |
| 658 | } |
| 659 | |
| 660 | void |
| 661 | mISDN_initstack(u_int *dp) |
| 662 | { |
| 663 | debug = dp; |
| 664 | } |