| 1 |
/* |
| 2 |
* Dynamic Span Interface for DAHDI (Multi-Span Ethernet Interface) |
| 3 |
* |
| 4 |
* Written by Joseph Benden <[email protected]> |
| 5 |
* |
| 6 |
* Copyright (C) 2007-2010, Thralling Penguin LLC. |
| 7 |
* |
| 8 |
* All rights reserved. |
| 9 |
* |
| 10 |
* This program is free software; you can redistribute it and/or modify |
| 11 |
* it under the terms of the GNU General Public License as published by |
| 12 |
* the Free Software Foundation; either version 2 of the License, or |
| 13 |
* (at your option) any later version. |
| 14 |
* |
| 15 |
* This program is distributed in the hope that it will be useful, |
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
* GNU General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU General Public License |
| 21 |
* along with this program; if not, write to the Free Software |
| 22 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <linux/kernel.h> |
| 27 |
#include <linux/errno.h> |
| 28 |
#include <linux/module.h> |
| 29 |
#include <linux/init.h> |
| 30 |
#include <linux/spinlock.h> |
| 31 |
#include <linux/slab.h> |
| 32 |
#include <linux/kmod.h> |
| 33 |
#include <linux/sched.h> |
| 34 |
#include <linux/interrupt.h> |
| 35 |
#include <linux/moduleparam.h> |
| 36 |
#include <linux/netdevice.h> |
| 37 |
#include <linux/notifier.h> |
| 38 |
#include <linux/crc32.h> |
| 39 |
#include <linux/seq_file.h> |
| 40 |
|
| 41 |
/** |
| 42 |
* Undefine USE_PROC_FS, if you do not want the /proc/dahdi/dynamic-ethmf |
| 43 |
* support. Undefining this would give a slight performance increase. |
| 44 |
*/ |
| 45 |
#define USE_PROC_FS |
| 46 |
|
| 47 |
#ifdef USE_PROC_FS |
| 48 |
# include <linux/proc_fs.h> |
| 49 |
# include <asm/atomic.h> |
| 50 |
#endif |
| 51 |
|
| 52 |
#ifdef CONFIG_DEVFS_FS |
| 53 |
# include <linux/devfs_fs_kernel.h> |
| 54 |
#endif |
| 55 |
|
| 56 |
#include <dahdi/kernel.h> |
| 57 |
#include <dahdi/user.h> |
| 58 |
|
| 59 |
#define ETH_P_ZTDETH 0xd00d |
| 60 |
#define ETHMF_MAX_PER_SPAN_GROUP 8 |
| 61 |
#define ETHMF_MAX_GROUPS 16 |
| 62 |
#define ETHMF_FLAG_IGNORE_CHAN0 (1 << 3) |
| 63 |
#define ETHMF_MAX_SPANS 4 |
| 64 |
|
| 65 |
struct ztdeth_header { |
| 66 |
unsigned short subaddr; |
| 67 |
}; |
| 68 |
|
| 69 |
/* Timer for enabling spans - used to combat a lock problem */ |
| 70 |
static struct timer_list timer; |
| 71 |
|
| 72 |
/* Whether or not the timer has been deleted */ |
| 73 |
static atomic_t timer_deleted = ATOMIC_INIT(0); |
| 74 |
|
| 75 |
/* Global error counter */ |
| 76 |
static atomic_t errcount = ATOMIC_INIT(0); |
| 77 |
|
| 78 |
/* Whether or not we are in shutdown */ |
| 79 |
static atomic_t shutdown = ATOMIC_INIT(0); |
| 80 |
|
| 81 |
static struct sk_buff_head skbs; |
| 82 |
|
| 83 |
#ifdef USE_PROC_FS |
| 84 |
struct ethmf_group { |
| 85 |
unsigned int hash_addr; |
| 86 |
atomic_t spans; |
| 87 |
atomic_t rxframecount; |
| 88 |
atomic_t txframecount; |
| 89 |
atomic_t rxbytecount; |
| 90 |
atomic_t txbytecount; |
| 91 |
atomic_t devupcount; |
| 92 |
atomic_t devdowncount; |
| 93 |
}; |
| 94 |
static struct ethmf_group ethmf_groups[ETHMF_MAX_GROUPS]; |
| 95 |
#endif |
| 96 |
|
| 97 |
struct ztdeth { |
| 98 |
/* Destination MAC address */ |
| 99 |
unsigned char addr[ETH_ALEN]; |
| 100 |
/* Destination MAC address hash value */ |
| 101 |
unsigned int addr_hash; |
| 102 |
/* span sub-address, in network byte order */ |
| 103 |
unsigned short subaddr; |
| 104 |
/* DAHDI span associated with this TDMoE-mf span */ |
| 105 |
struct dahdi_span *span; |
| 106 |
/* Ethernet interface name */ |
| 107 |
char ethdev[IFNAMSIZ]; |
| 108 |
/* Ethernet device reference */ |
| 109 |
struct net_device *dev; |
| 110 |
/* trx buffer */ |
| 111 |
unsigned char *msgbuf; |
| 112 |
/* trx buffer length */ |
| 113 |
int msgbuf_len; |
| 114 |
/* wether or not this frame is ready for trx */ |
| 115 |
atomic_t ready; |
| 116 |
/* delay counter, to ensure all spans are added, prior to usage */ |
| 117 |
atomic_t delay; |
| 118 |
/* rvc buffer */ |
| 119 |
unsigned char *rcvbuf; |
| 120 |
/* the number of channels in this span */ |
| 121 |
int real_channels; |
| 122 |
/* use padding if 1, else no padding */ |
| 123 |
atomic_t no_front_padding; |
| 124 |
/* counter to pseudo lock the rcvbuf */ |
| 125 |
atomic_t refcnt; |
| 126 |
|
| 127 |
struct list_head list; |
| 128 |
}; |
| 129 |
|
| 130 |
/** |
| 131 |
* Lock for adding and removing items in ethmf_list |
| 132 |
*/ |
| 133 |
static DEFINE_SPINLOCK(ethmf_lock); |
| 134 |
|
| 135 |
/** |
| 136 |
* The active list of all running spans |
| 137 |
*/ |
| 138 |
static LIST_HEAD(ethmf_list); |
| 139 |
|
| 140 |
static inline void ethmf_errors_inc(void) |
| 141 |
{ |
| 142 |
#ifdef USE_PROC_FS |
| 143 |
atomic_inc(&errcount); |
| 144 |
#endif |
| 145 |
} |
| 146 |
|
| 147 |
#ifdef USE_PROC_FS |
| 148 |
static inline int hashaddr_to_index(unsigned int hash_addr) |
| 149 |
{ |
| 150 |
int i, z = -1; |
| 151 |
for (i = 0; i < ETHMF_MAX_GROUPS; ++i) { |
| 152 |
if (z == -1 && ethmf_groups[i].hash_addr == 0) |
| 153 |
z = i; |
| 154 |
if (ethmf_groups[i].hash_addr == hash_addr) |
| 155 |
return i; |
| 156 |
} |
| 157 |
if (z != -1) { |
| 158 |
ethmf_groups[z].hash_addr = hash_addr; |
| 159 |
} |
| 160 |
return z; |
| 161 |
} |
| 162 |
#endif |
| 163 |
|
| 164 |
/** |
| 165 |
* Find the Ztdeth Struct and DAHDI span for a given MAC address and subaddr. |
| 166 |
* |
| 167 |
* NOTE: RCU read lock must already be held. |
| 168 |
*/ |
| 169 |
static inline void find_ethmf(const unsigned char *addr, |
| 170 |
const unsigned short subaddr, struct ztdeth **ze, |
| 171 |
struct dahdi_span **span) |
| 172 |
{ |
| 173 |
struct ztdeth *z; |
| 174 |
|
| 175 |
list_for_each_entry_rcu(z, ðmf_list, list) { |
| 176 |
if (!atomic_read(&z->delay)) { |
| 177 |
if (!memcmp(addr, z->addr, ETH_ALEN) |
| 178 |
&& z->subaddr == subaddr) { |
| 179 |
*ze = z; |
| 180 |
*span = z->span; |
| 181 |
return; |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
/* no results */ |
| 187 |
*ze = NULL; |
| 188 |
*span = NULL; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Determines if all spans are ready for transmit. If all spans are ready, |
| 193 |
* we return the number of spans which indeed are ready and populate the |
| 194 |
* array of pointers to those spans.. |
| 195 |
* |
| 196 |
* NOTE: RCU read lock must already be held. |
| 197 |
*/ |
| 198 |
static inline int ethmf_trx_spans_ready(unsigned int addr_hash, struct ztdeth *(*ready_spans)[ETHMF_MAX_PER_SPAN_GROUP]) |
| 199 |
{ |
| 200 |
struct ztdeth *t; |
| 201 |
int span_count = 0, spans_ready = 0; |
| 202 |
|
| 203 |
list_for_each_entry_rcu(t, ðmf_list, list) { |
| 204 |
if (!atomic_read(&t->delay) && t->addr_hash == addr_hash) { |
| 205 |
++span_count; |
| 206 |
if (atomic_read(&t->ready)) { |
| 207 |
short subaddr = ntohs(t->subaddr); |
| 208 |
if (subaddr < ETHMF_MAX_PER_SPAN_GROUP) { |
| 209 |
(*ready_spans)[subaddr] = t; |
| 210 |
++spans_ready; |
| 211 |
} else { |
| 212 |
printk(KERN_ERR "More than %d spans per multi-frame group are not currently supported.", |
| 213 |
ETHMF_MAX_PER_SPAN_GROUP); |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
if (span_count && spans_ready && span_count == spans_ready) { |
| 220 |
return spans_ready; |
| 221 |
} |
| 222 |
return 0; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Ethernet receiving side processing function. |
| 227 |
*/ |
| 228 |
static int ztdethmf_rcv(struct sk_buff *skb, struct net_device *dev, |
| 229 |
struct packet_type *pt, struct net_device *orig_dev) |
| 230 |
{ |
| 231 |
int num_spans = 0, span_index = 0; |
| 232 |
unsigned char *data; |
| 233 |
struct dahdi_span *span; |
| 234 |
struct ztdeth *z = NULL; |
| 235 |
struct ztdeth_header *zh; |
| 236 |
unsigned int samples, channels, rbslen, flags; |
| 237 |
unsigned int skip = 0; |
| 238 |
|
| 239 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22) |
| 240 |
zh = (struct ztdeth_header *) skb_network_header(skb); |
| 241 |
#else |
| 242 |
zh = (struct ztdeth_header *) skb->nh.raw; |
| 243 |
#endif |
| 244 |
if (ntohs(zh->subaddr) & 0x8000) { |
| 245 |
/* got a multi-span frame */ |
| 246 |
num_spans = ntohs(zh->subaddr) & 0xFF; |
| 247 |
|
| 248 |
/* Currently max of 4 spans supported */ |
| 249 |
if (unlikely(num_spans > ETHMF_MAX_SPANS)) { |
| 250 |
kfree_skb(skb); |
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
skb_pull(skb, sizeof(struct ztdeth_header)); |
| 255 |
#ifdef NEW_SKB_LINEARIZE |
| 256 |
if (skb_is_nonlinear(skb)) |
| 257 |
skb_linearize(skb); |
| 258 |
#else |
| 259 |
if (skb_is_nonlinear(skb)) |
| 260 |
skb_linearize(skb, GFP_KERNEL); |
| 261 |
#endif |
| 262 |
data = (unsigned char *) skb->data; |
| 263 |
|
| 264 |
rcu_read_lock(); |
| 265 |
do { |
| 266 |
find_ethmf(eth_hdr(skb)->h_source, |
| 267 |
htons(span_index), &z, &span); |
| 268 |
if (unlikely(!z || !span)) { |
| 269 |
/* The recv'd span does not belong to us */ |
| 270 |
/* ethmf_errors_inc(); */ |
| 271 |
++span_index; |
| 272 |
continue; |
| 273 |
} |
| 274 |
|
| 275 |
samples = data[(span_index * 6)] & 0xFF; |
| 276 |
flags = data[((span_index * 6) + 1)] & 0xFF; |
| 277 |
channels = data[((span_index * 6) + 5)] & 0xFF; |
| 278 |
|
| 279 |
/* Precomputed defaults for most typical values */ |
| 280 |
if (channels == 24) |
| 281 |
rbslen = 12; |
| 282 |
else if (channels == 31) |
| 283 |
rbslen = 16; |
| 284 |
else |
| 285 |
rbslen = ((channels + 3) / 4) * 2; |
| 286 |
|
| 287 |
if (unlikely(samples != 8 || channels >= 32 || channels == 0)) { |
| 288 |
ethmf_errors_inc(); |
| 289 |
++span_index; |
| 290 |
continue; |
| 291 |
} |
| 292 |
|
| 293 |
if (atomic_dec_and_test(&z->refcnt) == 0) { |
| 294 |
memcpy(z->rcvbuf, data + 6*span_index, 6); /* TDM Header */ |
| 295 |
/* |
| 296 |
* If we ignore channel zero we must skip the first eight bytes and |
| 297 |
* ensure that ztdynamic doesn't get confused by this new flag |
| 298 |
*/ |
| 299 |
if (flags & ETHMF_FLAG_IGNORE_CHAN0) { |
| 300 |
skip = 8; |
| 301 |
|
| 302 |
/* Remove this flag since ztdynamic may not understand it */ |
| 303 |
z->rcvbuf[1] = flags & ~(ETHMF_FLAG_IGNORE_CHAN0); |
| 304 |
|
| 305 |
/* Additionally, now we will transmit with front padding */ |
| 306 |
atomic_set(&z->no_front_padding, 0); |
| 307 |
} else { |
| 308 |
/* Disable front padding if we recv'd a packet without it */ |
| 309 |
atomic_set(&z->no_front_padding, 1); |
| 310 |
} |
| 311 |
memcpy(z->rcvbuf + 6, data + 6*num_spans + 16 |
| 312 |
*span_index, rbslen); /* RBS Header */ |
| 313 |
|
| 314 |
/* 256 == 32*8; if padding lengths change, this must be modified */ |
| 315 |
memcpy(z->rcvbuf + 6 + rbslen, data + 6*num_spans + 16 |
| 316 |
*num_spans + (256)*span_index + skip, channels |
| 317 |
* 8); /* Payload */ |
| 318 |
|
| 319 |
dahdi_dynamic_receive(span, z->rcvbuf, 6 + rbslen |
| 320 |
+ channels*8); |
| 321 |
} else { |
| 322 |
ethmf_errors_inc(); |
| 323 |
printk(KERN_INFO "TDMoE span overflow detected. Span %d was dropped.", span_index); |
| 324 |
} |
| 325 |
atomic_inc(&z->refcnt); |
| 326 |
|
| 327 |
#ifdef USE_PROC_FS |
| 328 |
if (span_index == 0) { |
| 329 |
atomic_inc(&(ethmf_groups[hashaddr_to_index(z->addr_hash)].rxframecount)); |
| 330 |
atomic_add(skb->len + z->dev->hard_header_len + |
| 331 |
sizeof(struct ztdeth_header), |
| 332 |
&(ethmf_groups[hashaddr_to_index(z->addr_hash)].rxbytecount)); |
| 333 |
} |
| 334 |
#endif |
| 335 |
++span_index; |
| 336 |
} while (!atomic_read(&shutdown) && span_index < num_spans); |
| 337 |
rcu_read_unlock(); |
| 338 |
} |
| 339 |
|
| 340 |
kfree_skb(skb); |
| 341 |
return 0; |
| 342 |
} |
| 343 |
|
| 344 |
static int ztdethmf_notifier(struct notifier_block *block, unsigned long event, |
| 345 |
void *ptr) |
| 346 |
{ |
| 347 |
struct net_device *dev = ptr; |
| 348 |
struct ztdeth *z; |
| 349 |
|
| 350 |
switch (event) { |
| 351 |
case NETDEV_GOING_DOWN: |
| 352 |
case NETDEV_DOWN: |
| 353 |
rcu_read_lock(); |
| 354 |
list_for_each_entry_rcu(z, ðmf_list, list) { |
| 355 |
/* Note that the device no longer exists */ |
| 356 |
if (z->dev == dev) { |
| 357 |
z->dev = NULL; |
| 358 |
#ifdef USE_PROC_FS |
| 359 |
atomic_inc(&(ethmf_groups[hashaddr_to_index(z->addr_hash)].devdowncount)); |
| 360 |
#endif |
| 361 |
} |
| 362 |
} |
| 363 |
rcu_read_unlock(); |
| 364 |
break; |
| 365 |
case NETDEV_UP: |
| 366 |
rcu_read_lock(); |
| 367 |
list_for_each_entry_rcu(z, ðmf_list, list) { |
| 368 |
/* Now that the device exists again, use it */ |
| 369 |
if (!strcmp(z->ethdev, dev->name)) { |
| 370 |
z->dev = dev; |
| 371 |
#ifdef USE_PROC_FS |
| 372 |
atomic_inc(&(ethmf_groups[hashaddr_to_index(z->addr_hash)].devupcount)); |
| 373 |
#endif |
| 374 |
} |
| 375 |
} |
| 376 |
rcu_read_unlock(); |
| 377 |
break; |
| 378 |
} |
| 379 |
return 0; |
| 380 |
} |
| 381 |
|
| 382 |
static void ztdethmf_transmit(struct dahdi_dynamic *dyn, u8 *msg, size_t msglen) |
| 383 |
{ |
| 384 |
struct ztdeth *z = dyn->pvt, *ready_spans[ETHMF_MAX_PER_SPAN_GROUP]; |
| 385 |
struct sk_buff *skb; |
| 386 |
struct ztdeth_header *zh; |
| 387 |
struct net_device *dev; |
| 388 |
unsigned char addr[ETH_ALEN]; |
| 389 |
int spans_ready = 0, index = 0; |
| 390 |
|
| 391 |
if (atomic_read(&shutdown)) |
| 392 |
return; |
| 393 |
|
| 394 |
rcu_read_lock(); |
| 395 |
|
| 396 |
if (unlikely(!z || !z->dev)) { |
| 397 |
rcu_read_unlock(); |
| 398 |
return; |
| 399 |
} |
| 400 |
|
| 401 |
if (!atomic_read(&z->ready)) { |
| 402 |
if (atomic_inc_return(&z->ready) == 1) { |
| 403 |
memcpy(z->msgbuf, msg, msglen); |
| 404 |
z->msgbuf_len = msglen; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
spans_ready = ethmf_trx_spans_ready(z->addr_hash, &ready_spans); |
| 409 |
if (spans_ready) { |
| 410 |
int pad[ETHMF_MAX_SPANS], rbs[ETHMF_MAX_SPANS]; |
| 411 |
|
| 412 |
dev = z->dev; |
| 413 |
memcpy(addr, z->addr, sizeof(z->addr)); |
| 414 |
|
| 415 |
for (index = 0; index < spans_ready; index++) { |
| 416 |
int chan = ready_spans[index]->real_channels; |
| 417 |
/* By default we pad to 32 channels, but if |
| 418 |
* no_front_padding is false then we have a pad |
| 419 |
* in the front of 8 bytes, so this implies one |
| 420 |
* less channel |
| 421 |
*/ |
| 422 |
if (atomic_read(&(ready_spans[index]->no_front_padding))) |
| 423 |
pad[index] = (32 - chan)*8; |
| 424 |
else |
| 425 |
pad[index] = (31 - chan)*8; |
| 426 |
|
| 427 |
if (chan == 24) |
| 428 |
rbs[index] = 12; |
| 429 |
else if (chan == 31) |
| 430 |
rbs[index] = 16; |
| 431 |
else |
| 432 |
/* Shouldn't this be index, not spans_ready? */ |
| 433 |
rbs[spans_ready] = ((chan + 3) / 4) * 2; |
| 434 |
} |
| 435 |
|
| 436 |
/* Allocate the standard size for a 32-chan frame */ |
| 437 |
skb = dev_alloc_skb(1112 + dev->hard_header_len |
| 438 |
+ sizeof(struct ztdeth_header) + 32); |
| 439 |
if (unlikely(!skb)) { |
| 440 |
rcu_read_unlock(); |
| 441 |
ethmf_errors_inc(); |
| 442 |
return; |
| 443 |
} |
| 444 |
|
| 445 |
/* Reserve header space */ |
| 446 |
skb_reserve(skb, dev->hard_header_len |
| 447 |
+ sizeof(struct ztdeth_header)); |
| 448 |
/* copy each spans header */ |
| 449 |
for (index = 0; index < spans_ready; index++) { |
| 450 |
if (!atomic_read(&(ready_spans[index]->no_front_padding))) |
| 451 |
ready_spans[index]->msgbuf[1] |
| 452 |
|= ETHMF_FLAG_IGNORE_CHAN0; |
| 453 |
|
| 454 |
memcpy(skb_put(skb, 6), ready_spans[index]->msgbuf, 6); |
| 455 |
} |
| 456 |
|
| 457 |
/* copy each spans RBS payload */ |
| 458 |
for (index = 0; index < spans_ready; index++) { |
| 459 |
memcpy(skb_put(skb, 16), ready_spans[index]->msgbuf + 6, |
| 460 |
rbs[index]); |
| 461 |
} |
| 462 |
|
| 463 |
/* copy each spans data/voice payload */ |
| 464 |
for (index = 0; index < spans_ready; index++) { |
| 465 |
int chan = ready_spans[index]->real_channels; |
| 466 |
if (!atomic_read(&(ready_spans[index]->no_front_padding))) { |
| 467 |
/* This adds an additional (padded) channel to our total */ |
| 468 |
memset(skb_put(skb, 8), 0xA5, 8); /* ETHMF_IGNORE_CHAN0 */ |
| 469 |
} |
| 470 |
memcpy(skb_put(skb, chan*8), ready_spans[index]->msgbuf |
| 471 |
+ (6 + rbs[index]), chan*8); |
| 472 |
if (pad[index] > 0) { |
| 473 |
memset(skb_put(skb, pad[index]), 0xDD, pad[index]); |
| 474 |
} |
| 475 |
|
| 476 |
/* mark span as ready for new data/voice */ |
| 477 |
atomic_set(&(ready_spans[index]->ready), 0); |
| 478 |
} |
| 479 |
|
| 480 |
/* Throw on header */ |
| 481 |
zh = (struct ztdeth_header *)skb_push(skb, |
| 482 |
sizeof(struct ztdeth_header)); |
| 483 |
zh->subaddr = htons((unsigned short)(0x8000 | (unsigned char)(spans_ready & 0xFF))); |
| 484 |
|
| 485 |
/* Setup protocol type */ |
| 486 |
skb->protocol = __constant_htons(ETH_P_ZTDETH); |
| 487 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22) |
| 488 |
skb_set_network_header(skb, 0); |
| 489 |
#else |
| 490 |
skb->nh.raw = skb->data; |
| 491 |
#endif |
| 492 |
skb->dev = dev; |
| 493 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) |
| 494 |
dev_hard_header(skb, dev, ETH_P_ZTDETH, addr, dev->dev_addr, skb->len); |
| 495 |
#else |
| 496 |
if (dev->hard_header) |
| 497 |
dev->hard_header(skb, dev, ETH_P_ZTDETH, addr, |
| 498 |
dev->dev_addr, skb->len); |
| 499 |
#endif |
| 500 |
/* queue frame for delivery */ |
| 501 |
if (dev) { |
| 502 |
skb_queue_tail(&skbs, skb); |
| 503 |
} |
| 504 |
#ifdef USE_PROC_FS |
| 505 |
atomic_inc(&(ethmf_groups[hashaddr_to_index(z->addr_hash)].txframecount)); |
| 506 |
atomic_add(skb->len, &(ethmf_groups[hashaddr_to_index(z->addr_hash)].txbytecount)); |
| 507 |
#endif |
| 508 |
} |
| 509 |
|
| 510 |
rcu_read_unlock(); |
| 511 |
|
| 512 |
return; |
| 513 |
} |
| 514 |
|
| 515 |
static int ztdethmf_flush(void) |
| 516 |
{ |
| 517 |
struct sk_buff *skb; |
| 518 |
|
| 519 |
/* Handle all transmissions now */ |
| 520 |
while ((skb = skb_dequeue(&skbs))) { |
| 521 |
dev_queue_xmit(skb); |
| 522 |
} |
| 523 |
return 0; |
| 524 |
} |
| 525 |
|
| 526 |
static struct packet_type ztdethmf_ptype = { |
| 527 |
.type = __constant_htons(ETH_P_ZTDETH), /* Protocol */ |
| 528 |
.dev = NULL, /* Device (NULL = wildcard) */ |
| 529 |
.func = ztdethmf_rcv, /* Receiver */ |
| 530 |
}; |
| 531 |
|
| 532 |
static void ztdethmf_destroy(struct dahdi_dynamic *dyn) |
| 533 |
{ |
| 534 |
struct ztdeth *z = dyn->pvt; |
| 535 |
unsigned long flags; |
| 536 |
|
| 537 |
atomic_set(&shutdown, 1); |
| 538 |
synchronize_rcu(); |
| 539 |
|
| 540 |
spin_lock_irqsave(ðmf_lock, flags); |
| 541 |
list_del_rcu(&z->list); |
| 542 |
spin_unlock_irqrestore(ðmf_lock, flags); |
| 543 |
synchronize_rcu(); |
| 544 |
atomic_dec(&(ethmf_groups[hashaddr_to_index(z->addr_hash)].spans)); |
| 545 |
|
| 546 |
if (z) { /* Successfully removed */ |
| 547 |
printk(KERN_INFO "Removed interface for %s\n", |
| 548 |
z->span->name); |
| 549 |
kfree(z->msgbuf); |
| 550 |
kfree(z); |
| 551 |
} else { |
| 552 |
if (z && z->span && z->span->name) { |
| 553 |
printk(KERN_ERR "Cannot find interface for %s\n", |
| 554 |
z->span->name); |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
static int ztdethmf_create(struct dahdi_dynamic *dyn, const char *addr) |
| 560 |
{ |
| 561 |
struct ztdeth *z; |
| 562 |
char src[256]; |
| 563 |
char *src_ptr; |
| 564 |
int x, bufsize, num_matched; |
| 565 |
unsigned long flags; |
| 566 |
struct dahdi_span *const span = &dyn->span; |
| 567 |
|
| 568 |
BUG_ON(!span); |
| 569 |
BUG_ON(!addr); |
| 570 |
|
| 571 |
z = kmalloc(sizeof(struct ztdeth), GFP_KERNEL); |
| 572 |
if (!z) |
| 573 |
return -ENOMEM; |
| 574 |
|
| 575 |
/* Zero it out */ |
| 576 |
memset(z, 0, sizeof(struct ztdeth)); |
| 577 |
|
| 578 |
/* set a delay for xmit/recv to workaround Zaptel problems */ |
| 579 |
atomic_set(&z->delay, 4); |
| 580 |
|
| 581 |
/* create a msg buffer. MAX OF 31 CHANNELS!!!! */ |
| 582 |
bufsize = 31 * DAHDI_CHUNKSIZE + 31 / 4 + 48; |
| 583 |
z->msgbuf = kmalloc(bufsize, GFP_KERNEL); |
| 584 |
z->rcvbuf = kmalloc(bufsize, GFP_KERNEL); |
| 585 |
|
| 586 |
/* Address should be <dev>/<macaddr>/subaddr */ |
| 587 |
strlcpy(src, addr, sizeof(src)); |
| 588 |
/* replace all / with space; otherwise kernel sscanf does not work */ |
| 589 |
src_ptr = src; |
| 590 |
while (*src_ptr) { |
| 591 |
if (*src_ptr == '/') |
| 592 |
*src_ptr = ' '; |
| 593 |
++src_ptr; |
| 594 |
} |
| 595 |
num_matched = sscanf(src, |
| 596 |
"%16s %hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hu", |
| 597 |
z->ethdev, &z->addr[0], &z->addr[1], |
| 598 |
&z->addr[2], &z->addr[3], &z->addr[4], |
| 599 |
&z->addr[5], &z->subaddr); |
| 600 |
if (8 != num_matched) { |
| 601 |
printk(KERN_ERR "Only matched %d entries in '%s'\n", num_matched, src); |
| 602 |
printk(KERN_ERR "Invalid TDMoE Multiframe address: %s\n", addr); |
| 603 |
kfree(z); |
| 604 |
return -EINVAL; |
| 605 |
} |
| 606 |
z->dev = dev_get_by_name( |
| 607 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) |
| 608 |
&init_net, |
| 609 |
#endif |
| 610 |
z->ethdev); |
| 611 |
if (!z->dev) { |
| 612 |
printk(KERN_ERR "TDMoE Multiframe: Invalid device '%s'\n", z->ethdev); |
| 613 |
kfree(z); |
| 614 |
return -EINVAL; |
| 615 |
} |
| 616 |
z->span = span; |
| 617 |
z->subaddr = htons(z->subaddr); |
| 618 |
z->addr_hash = crc32_le(0, z->addr, ETH_ALEN); |
| 619 |
z->real_channels = span->channels; |
| 620 |
|
| 621 |
src[0] = '\0'; |
| 622 |
for (x = 0; x < 5; x++) |
| 623 |
sprintf(src + strlen(src), "%02x:", z->dev->dev_addr[x]); |
| 624 |
sprintf(src + strlen(src), "%02x", z->dev->dev_addr[5]); |
| 625 |
|
| 626 |
printk(KERN_INFO "TDMoEmf: Added new interface for %s at %s " |
| 627 |
"(addr=%s, src=%s, subaddr=%d)\n", span->name, z->dev->name, |
| 628 |
addr, src, ntohs(z->subaddr)); |
| 629 |
|
| 630 |
atomic_set(&z->ready, 0); |
| 631 |
atomic_set(&z->refcnt, 0); |
| 632 |
|
| 633 |
spin_lock_irqsave(ðmf_lock, flags); |
| 634 |
list_add_rcu(&z->list, ðmf_list); |
| 635 |
spin_unlock_irqrestore(ðmf_lock, flags); |
| 636 |
atomic_inc(&(ethmf_groups[hashaddr_to_index(z->addr_hash)].spans)); |
| 637 |
|
| 638 |
/* enable the timer for enabling the spans */ |
| 639 |
mod_timer(&timer, jiffies + HZ); |
| 640 |
atomic_set(&shutdown, 0); |
| 641 |
dyn->pvt = z; |
| 642 |
return 0; |
| 643 |
} |
| 644 |
|
| 645 |
static struct dahdi_dynamic_driver ztd_ethmf = { |
| 646 |
.owner = THIS_MODULE, |
| 647 |
.name = "ethmf", |
| 648 |
.desc = "Ethernet", |
| 649 |
.create = ztdethmf_create, |
| 650 |
.destroy = ztdethmf_destroy, |
| 651 |
.transmit = ztdethmf_transmit, |
| 652 |
.flush = ztdethmf_flush, |
| 653 |
}; |
| 654 |
|
| 655 |
static struct notifier_block ztdethmf_nblock = { |
| 656 |
.notifier_call = ztdethmf_notifier, |
| 657 |
}; |
| 658 |
|
| 659 |
/** |
| 660 |
* Decrements each delay counter in the ethmf_list and returns the number of |
| 661 |
* delay counters that are not equal to zero. |
| 662 |
*/ |
| 663 |
static int ethmf_delay_dec(void) |
| 664 |
{ |
| 665 |
struct ztdeth *z; |
| 666 |
int count_nonzero = 0; |
| 667 |
|
| 668 |
rcu_read_lock(); |
| 669 |
list_for_each_entry_rcu(z, ðmf_list, list) { |
| 670 |
if (atomic_read(&z->delay)) { |
| 671 |
atomic_dec(&z->delay); |
| 672 |
++count_nonzero; |
| 673 |
} else |
| 674 |
atomic_set(&z->delay, 0); |
| 675 |
} |
| 676 |
rcu_read_unlock(); |
| 677 |
return count_nonzero; |
| 678 |
} |
| 679 |
|
| 680 |
/** |
| 681 |
* Timer callback function to allow all spans to be added, prior to any of |
| 682 |
* them being used. |
| 683 |
*/ |
| 684 |
static void timer_callback(unsigned long param) |
| 685 |
{ |
| 686 |
if (ethmf_delay_dec()) { |
| 687 |
if (!atomic_read(&timer_deleted)) { |
| 688 |
timer.expires = jiffies + HZ; |
| 689 |
add_timer(&timer); |
| 690 |
} |
| 691 |
} else { |
| 692 |
printk(KERN_INFO "All TDMoE multiframe span groups are active.\n"); |
| 693 |
del_timer(&timer); |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
#ifdef USE_PROC_FS |
| 698 |
static struct proc_dir_entry *proc_entry; |
| 699 |
static const char *ztdethmf_procname = "dahdi/dynamic-ethmf"; |
| 700 |
|
| 701 |
static int ztdethmf_proc_show(struct seq_file *sfile, void *not_used) |
| 702 |
{ |
| 703 |
struct ztdeth *z = NULL; |
| 704 |
int i = 0; |
| 705 |
unsigned int group = 0, c = 0; |
| 706 |
|
| 707 |
rcu_read_lock(); |
| 708 |
|
| 709 |
seq_printf(sfile, "Errors: %d\n\n", atomic_read(&errcount)); |
| 710 |
|
| 711 |
for (group = 0; group < ETHMF_MAX_GROUPS; ++group) { |
| 712 |
if (atomic_read(&(ethmf_groups[group].spans))) { |
| 713 |
seq_printf(sfile, "Group #%d (0x%x)\n", i++, |
| 714 |
ethmf_groups[group].hash_addr); |
| 715 |
seq_printf(sfile, "Spans: %d\n", |
| 716 |
atomic_read(&(ethmf_groups[group].spans))); |
| 717 |
|
| 718 |
c = 1; |
| 719 |
list_for_each_entry_rcu(z, ðmf_list, list) { |
| 720 |
if (z->addr_hash == ethmf_groups[group].hash_addr) { |
| 721 |
if (c == 1) { |
| 722 |
seq_printf(sfile, |
| 723 |
" Device: %s (MAC: %02x:%02x:%02x:%02x:%02x:%02x)\n", |
| 724 |
z->ethdev, |
| 725 |
z->addr[0], z->addr[1], z->addr[2], |
| 726 |
z->addr[3], z->addr[4], z->addr[5]); |
| 727 |
} |
| 728 |
seq_printf(sfile, " Span %d: subaddr=%u ready=%d delay=%d real_channels=%d no_front_padding=%d\n", |
| 729 |
c++, ntohs(z->subaddr), |
| 730 |
atomic_read(&z->ready), atomic_read(&z->delay), |
| 731 |
z->real_channels, atomic_read(&z->no_front_padding)); |
| 732 |
} |
| 733 |
} |
| 734 |
seq_printf(sfile, " Device UPs: %u\n", |
| 735 |
atomic_read(&(ethmf_groups[group].devupcount))); |
| 736 |
seq_printf(sfile, " Device DOWNs: %u\n", |
| 737 |
atomic_read(&(ethmf_groups[group].devdowncount))); |
| 738 |
seq_printf(sfile, " Rx Frames: %u\n", |
| 739 |
atomic_read(&(ethmf_groups[group].rxframecount))); |
| 740 |
seq_printf(sfile, " Tx Frames: %u\n", |
| 741 |
atomic_read(&(ethmf_groups[group].txframecount))); |
| 742 |
seq_printf(sfile, " Rx Bytes: %u\n", |
| 743 |
atomic_read(&(ethmf_groups[group].rxbytecount))); |
| 744 |
seq_printf(sfile, " Tx Bytes: %u\n", |
| 745 |
atomic_read(&(ethmf_groups[group].txbytecount))); |
| 746 |
} |
| 747 |
} |
| 748 |
rcu_read_unlock(); |
| 749 |
return 0; |
| 750 |
} |
| 751 |
|
| 752 |
static int ztdethmf_proc_open(struct inode *inode, struct file *file) |
| 753 |
{ |
| 754 |
return single_open(file, ztdethmf_proc_show, NULL); |
| 755 |
} |
| 756 |
|
| 757 |
static const struct file_operations ztdethmf_proc_fops = { |
| 758 |
.open = ztdethmf_proc_open, |
| 759 |
.read = seq_read, |
| 760 |
.llseek = seq_lseek, |
| 761 |
.release = seq_release, |
| 762 |
}; |
| 763 |
#endif |
| 764 |
|
| 765 |
static int __init ztdethmf_init(void) |
| 766 |
{ |
| 767 |
init_timer(&timer); |
| 768 |
timer.expires = jiffies + HZ; |
| 769 |
timer.function = &timer_callback; |
| 770 |
if (!timer_pending(&timer)) |
| 771 |
add_timer(&timer); |
| 772 |
|
| 773 |
dev_add_pack(&ztdethmf_ptype); |
| 774 |
register_netdevice_notifier(&ztdethmf_nblock); |
| 775 |
dahdi_dynamic_register_driver(&ztd_ethmf); |
| 776 |
|
| 777 |
skb_queue_head_init(&skbs); |
| 778 |
|
| 779 |
#ifdef USE_PROC_FS |
| 780 |
proc_entry = proc_create_data(ztdethmf_procname, 0444, NULL, |
| 781 |
&ztdethmf_proc_fops, NULL); |
| 782 |
if (!proc_entry) { |
| 783 |
printk(KERN_ALERT "create_proc_read_entry failed.\n"); |
| 784 |
} |
| 785 |
#endif |
| 786 |
|
| 787 |
return 0; |
| 788 |
} |
| 789 |
|
| 790 |
static void __exit ztdethmf_exit(void) |
| 791 |
{ |
| 792 |
atomic_set(&timer_deleted, 1); |
| 793 |
del_timer_sync(&timer); |
| 794 |
|
| 795 |
dev_remove_pack(&ztdethmf_ptype); |
| 796 |
unregister_netdevice_notifier(&ztdethmf_nblock); |
| 797 |
dahdi_dynamic_unregister_driver(&ztd_ethmf); |
| 798 |
|
| 799 |
#ifdef USE_PROC_FS |
| 800 |
if (proc_entry) |
| 801 |
remove_proc_entry(ztdethmf_procname, NULL); |
| 802 |
#endif |
| 803 |
} |
| 804 |
|
| 805 |
MODULE_DESCRIPTION("DAHDI Dynamic TDMoEmf Support"); |
| 806 |
MODULE_AUTHOR("Joseph Benden <[email protected]>"); |
| 807 |
#ifdef MODULE_LICENSE |
| 808 |
MODULE_LICENSE("GPL"); |
| 809 |
#endif |
| 810 |
|
| 811 |
module_init(ztdethmf_init); |
| 812 |
module_exit(ztdethmf_exit); |