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