Debian Codegraph @

drivers/dahdi/wctc4xxp/base.c

raw symbols SCIP
1 /* Wildcard TC400B Driver
2 *
3 * Copyright (C) 2006-2012, Digium, Inc.
4 *
5 * All rights reserved.
6 *
7 */
8
9 /*
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
15 *
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2 as published by the
18 * Free Software Foundation. See the LICENSE file included with
19 * this program for more details.
20 */
21
22 #include <linux/kernel.h>
23 #include <linux/errno.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/kmod.h>
28 #include <linux/sched.h>
29
30 #include <linux/pci.h>
31 #include <linux/interrupt.h>
32 #include <linux/delay.h>
33 #include <linux/jiffies.h>
34 #include <linux/moduleparam.h>
35 #include <linux/firmware.h>
36 #include <linux/if_ether.h>
37 #include <linux/ip.h>
38 #include <linux/udp.h>
39 #include <linux/etherdevice.h>
40 #include <linux/timer.h>
41
42 #include <stdbool.h>
43
44 #include <dahdi/kernel.h>
45
46 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
47 #include <linux/sched/signal.h>
48 #endif /* 4.11.0 */
49
50
51 #include <linux/io.h>
52
53 /* COMPILE TIME OPTIONS =================================================== */
54
55 #define INTERRUPT 0
56 #define WORKQUEUE 1
57 #define TASKLET 2
58
59 /* Define if you want a debug attribute to test alert processing. */
60 #undef EXPORT_FOR_ALERT_ATTRIBUTE
61
62 #ifndef DEFERRED_PROCESSING
63 # define DEFERRED_PROCESSING WORKQUEUE
64 #endif
65
66 #if DEFERRED_PROCESSING == INTERRUPT
67 # define ALLOC_FLAGS GFP_ATOMIC
68 #elif DEFERRED_PROCESSING == TASKLET
69 # define ALLOC_FLAGS GFP_ATOMIC
70 #else
71 # define ALLOC_FLAGS GFP_KERNEL
72 #endif
73
74 #define WARN_ALWAYS() WARN_ON(1)
75
76 #define DTE_DEBUG(_dbgmask, _fmt, _args...) \
77 if ((debug & _dbgmask) == (_dbgmask)) { \
78 dev_info(&(wc)->pdev->dev, _fmt, ## _args); \
79 } \
80
81
82 /* define CONFIG_WCTC4XXP_POLLING to operate in a pure polling mode. This is
83 * was placed in as a debugging tool for a particluar system that wasn't
84 * routing the interrupt properly. Therefore it is off by default and the
85 * driver must be recompiled to enable it. */
86 #undef CONFIG_WCTC4XXP_POLLING
87
88 /* The total number of active channels over which the driver will start polling
89 * the card every 10 ms. */
90 #define POLLING_CALL_THRESHOLD 40
91
92 #define INVALID 999 /* Used to mark invalid channels, commands, etc.. */
93 #define MAX_CHANNEL_PACKETS 5
94
95 #define G729_LENGTH 20
96 #define G723_LENGTH 30
97
98 #define G729_SAMPLES 160 /* G.729 */
99 #define G723_SAMPLES 240 /* G.723.1 */
100
101 #define G729_BYTES 20 /* G.729 */
102 #define G723_6K_BYTES 24 /* G.723.1 at 6.3kb/s */
103 #define G723_5K_BYTES 20 /* G.723.1 at 5.3kb/s */
104 #define G723_SID_BYTES 4 /* G.723.1 SID frame */
105
106 #define MAX_CAPTURED_PACKETS 5000
107
108 /* The following bit fields are used to set the various debug levels. */
109 #define DTE_DEBUG_GENERAL (1 << 0) /* 1 */
110 #define DTE_DEBUG_CHANNEL_SETUP (1 << 1) /* 2 */
111 #define DTE_DEBUG_RTP_TX (1 << 2) /* 4 */
112 #define DTE_DEBUG_RTP_RX (1 << 3) /* 8 */
113 #define DTE_DEBUG_RX_TIMEOUT (1 << 4) /* 16 */
114 #define DTE_DEBUG_NETWORK_IF (1 << 5) /* 32 */
115 #define DTE_DEBUG_NETWORK_EARLY (1 << 6) /* 64 */
116 #define DTE_DEBUG_ETH_STATS (1 << 7) /* 128 */
117
118 static int debug;
119 static char *mode;
120
121 static spinlock_t wctc4xxp_list_lock;
122 static struct list_head wctc4xxp_list;
123
124 #define ETH_P_CSM_ENCAPS 0x889B
125
126 struct rtphdr {
127 #if defined(__LITTLE_ENDIAN_BITFIELD)
128 __u8 csrc_count:4;
129 __u8 extension:1;
130 __u8 padding:1;
131 __u8 ver:2;
132 __u8 type:7;
133 __u8 marker:1;
134 #elif defined(__BIG_ENDIAN_BITFIELD)
135 __u8 ver:2;
136 __u8 padding:1;
137 __u8 extension:1;
138 __u8 csrc_count:4;
139 __u8 marker:1;
140 __u8 type:7;
141 #else
142 #error "Please fix <asm/byteorder.h>"
143 #endif
144 __be16 seqno;
145 __be32 timestamp;
146 __be32 ssrc;
147 } __attribute__((packed));
148
149 struct rtp_packet {
150 struct ethhdr ethhdr;
151 struct iphdr iphdr;
152 struct udphdr udphdr;
153 struct rtphdr rtphdr;
154 __u8 payload[0];
155 } __attribute__((packed));
156
157 struct csm_encaps_cmd {
158 /* COMMON PART OF PAYLOAD HEADER */
159 __u8 length;
160 __u8 index;
161 __u8 type;
162 __u8 class;
163 __le16 function;
164 __le16 reserved;
165 __le16 params[0];
166 } __attribute__((packed));
167
168 /* Ethernet packet type for communication control information to the DTE. */
169 struct csm_encaps_hdr {
170 struct ethhdr ethhdr;
171 /* CSM_ENCAPS HEADER */
172 __be16 op_code;
173 __u8 seq_num;
174 __u8 control;
175 __be16 channel;
176 /* There is always at least one command. */
177 struct csm_encaps_cmd cmd;
178 } __attribute__((packed));
179
180 #define CONTROL_PACKET_OPCODE 0x0001
181 /* Control bits */
182 #define LITTLE_ENDIAN 0x01
183 #define SUPPRESS_ACK 0x40
184 #define MESSAGE_PACKET 0x80
185
186 #define SUPERVISOR_CHANNEL 0xffff
187
188 /* Supervisor function codes */
189 #define SUPVSR_CREATE_CHANNEL 0x0010
190
191 #define MONITOR_LIVE_INDICATION_TYPE 0x75
192 #define VOIP_VCEINFO_TYPE 0x0e
193 #define CONFIG_CHANGE_TYPE 0x00
194 #define CONFIG_CHANNEL_CLASS 0x02
195 #define CONFIG_DEVICE_CLASS 0x06
196
197 /* Individual channel config commands */
198 #define MAX_FRAME_SIZE 1518
199 #define SFRAME_SIZE MAX_FRAME_SIZE
200
201 #define DEFAULT_RX_DRING_SIZE (1 << 6) /* Must be a power of two */
202
203 /* Keep the TX ring shorter in order to reduce the amount of time needed to
204 * bring up channels when sending high priority csm_encaps packets. */
205 #define DEFAULT_TX_DRING_SIZE (1 << 4) /* Must be a power of two */
206 #define MIN_PACKET_LEN 64
207
208 /* Transcoder buffer (tcb) */
209 struct tcb {
210 void *data;
211 struct list_head node;
212 unsigned long timeout;
213 unsigned long retries;
214 #define TX_COMPLETE (1 << 1)
215 #define DO_NOT_CAPTURE (1 << 2)
216 #define WAIT_FOR_ACK (1 << 3)
217 #define WAIT_FOR_RESPONSE (1 << 4)
218 #define DTE_CMD_TIMEOUT (1 << 5)
219 u16 flags;
220 u16 next_index;
221 struct completion *complete;
222 struct tcb *response;
223 struct channel_pvt *cpvt;
224 /* The number of bytes available in data. */
225 int data_len;
226 };
227
228 static inline const struct csm_encaps_hdr *
229 response_header(struct tcb *cmd)
230 {
231 BUG_ON(!cmd->response);
232 return (const struct csm_encaps_hdr *)(cmd)->response->data;
233 }
234
235 static inline void
236 initialize_cmd(struct tcb *cmd, unsigned long cmd_flags)
237 {
238 INIT_LIST_HEAD(&cmd->node);
239 cmd->flags = cmd_flags;
240 }
241
242 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
243 /*! Used to allocate commands to submit to the dte. */
244 kmem_cache_t *cmd_cache;
245 #else
246 /*! Used to allocate commands to submit to the dte. */
247 static struct kmem_cache *cmd_cache;
248 #endif
249
250 static inline struct tcb *
251 __alloc_cmd(size_t size, gfp_t alloc_flags, unsigned long cmd_flags)
252 {
253 struct tcb *cmd;
254
255 if (unlikely(size > SFRAME_SIZE))
256 return NULL;
257 if (size < MIN_PACKET_LEN)
258 size = MIN_PACKET_LEN;
259 cmd = kmem_cache_alloc(cmd_cache, alloc_flags);
260 if (likely(cmd)) {
261 memset(cmd, 0, sizeof(*cmd));
262 cmd->data = kzalloc(size, alloc_flags);
263 if (unlikely(!cmd->data)) {
264 kmem_cache_free(cmd_cache, cmd);
265 return NULL;
266 }
267 cmd->data_len = size;
268 initialize_cmd(cmd, cmd_flags);
269 }
270 return cmd;
271 }
272
273 static struct tcb *
274 alloc_cmd(size_t size)
275 {
276 return __alloc_cmd(size, GFP_KERNEL, 0);
277 }
278
279 static void
280 __free_cmd(struct tcb *cmd)
281 {
282 if (cmd)
283 kfree(cmd->data);
284 kmem_cache_free(cmd_cache, cmd);
285 return;
286 }
287
288 static void
289 free_cmd(struct tcb *cmd)
290 {
291 if (cmd->response)
292 __free_cmd(cmd->response);
293 __free_cmd(cmd);
294 }
295
296 struct channel_stats {
297 atomic_t packets_sent;
298 atomic_t packets_received;
299 };
300
301 struct channel_pvt {
302 spinlock_t lock; /* Lock for this structure */
303 struct wcdte *wc;
304 u16 seqno;
305 u8 cmd_seqno;
306 u8 ssrc;
307 u8 last_rx_seq_num;
308 u16 timeslot_in_num; /* DTE timeslot to receive from */
309 u16 timeslot_out_num; /* DTE timeslot to send data to */
310 u16 chan_in_num; /* DTE channel to receive from */
311 u16 chan_out_num; /* DTE channel to send data to */
312 u32 last_timestamp;
313 struct {
314 u8 encoder:1; /* If we're an encoder */
315 };
316 struct channel_stats stats;
317 struct list_head rx_queue; /* Transcoded packets for this channel. */
318
319 /* Used to prevent user space from flooding the firmware. */
320 long samples_in_flight;
321 unsigned long send_time;
322 };
323
324 struct wcdte {
325 char board_name[40];
326 const char *variety;
327 int pos;
328 struct list_head node;
329 spinlock_t reglock;
330 wait_queue_head_t waitq;
331 struct mutex chanlock;
332 #define DTE_READY 1
333 #define DTE_SHUTDOWN 2
334 #define DTE_POLLING 3
335 #define DTE_RELOAD 4
336 unsigned long flags;
337
338 /* This is a device-global list of commands that are waiting to be
339 * transmited (and did not fit on the transmit descriptor ring) */
340 spinlock_t cmd_list_lock;
341 struct list_head cmd_list;
342 struct list_head waiting_for_response_list;
343
344 spinlock_t rx_list_lock;
345 struct list_head rx_list;
346 spinlock_t rx_lock;
347
348 unsigned int seq_num;
349 int last_rx_seq_num;
350 unsigned char numchannels;
351 unsigned char complexname[40];
352
353 /* This section contains the members necessary to communicate with the
354 * physical interface to the transcoding engine. */
355 struct pci_dev *pdev;
356 unsigned int intmask;
357 void __iomem *iobase;
358 struct wctc4xxp_descriptor_ring *txd;
359 struct wctc4xxp_descriptor_ring *rxd;
360
361 struct dahdi_transcoder *uencode;
362 struct dahdi_transcoder *udecode;
363 struct channel_pvt *encoders;
364 struct channel_pvt *decoders;
365
366 #if DEFERRED_PROCESSING == WORKQUEUE
367 struct work_struct deferred_work;
368 #endif
369
370 /*
371 * This section contains the members necessary for exporting the
372 * network interface to the host system. This is only used for
373 * debugging purposes.
374 *
375 */
376 struct sk_buff_head captured_packets;
377 struct net_device *netdev;
378 struct net_device_stats net_stats;
379 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
380 struct napi_struct napi;
381 #endif
382 struct timer_list watchdog;
383 u16 open_channels;
384 unsigned long reported_packet_errors;
385 };
386
387 struct wcdte_netdev_priv {
388 struct wcdte *wc;
389 };
390
391 static inline struct wcdte *
392 wcdte_from_netdev(struct net_device *netdev)
393 {
394 struct wcdte_netdev_priv *priv;
395 priv = netdev_priv(netdev);
396 return priv->wc;
397 }
398
399
400 static inline void wctc4xxp_set_ready(struct wcdte *wc)
401 {
402 set_bit(DTE_READY, &wc->flags);
403 }
404
405 static inline int wctc4xxp_is_ready(struct wcdte *wc)
406 {
407 return test_bit(DTE_READY, &wc->flags);
408 }
409
410 #define DTE_FORMAT_ULAW 0x00
411 #define DTE_FORMAT_G723_1 0x04
412 #define DTE_FORMAT_ALAW 0x08
413 #define DTE_FORMAT_G729A 0x12
414 #define DTE_FORMAT_UNDEF 0xFF
415
416 static inline u8 wctc4xxp_dahdifmt_to_dtefmt(unsigned int fmt)
417 {
418 u8 pt;
419
420 switch (fmt) {
421 case DAHDI_FORMAT_G723_1:
422 pt = DTE_FORMAT_G723_1;
423 break;
424 case DAHDI_FORMAT_ULAW:
425 pt = DTE_FORMAT_ULAW;
426 break;
427 case DAHDI_FORMAT_ALAW:
428 pt = DTE_FORMAT_ALAW;
429 break;
430 case DAHDI_FORMAT_G729A:
431 pt = DTE_FORMAT_G729A;
432 break;
433 default:
434 pt = DTE_FORMAT_UNDEF;
435 break;
436 }
437
438 return pt;
439 }
440
441 static struct sk_buff *
442 tcb_to_skb(struct net_device *netdev, const struct tcb *cmd)
443 {
444 struct sk_buff *skb;
445 skb = alloc_skb(cmd->data_len, in_atomic() ? GFP_ATOMIC : GFP_KERNEL);
446 if (skb) {
447 skb->dev = netdev;
448 skb_put(skb, cmd->data_len);
449 memcpy(skb->data, cmd->data, cmd->data_len);
450 skb->protocol = eth_type_trans(skb, netdev);
451 }
452 return skb;
453 }
454
455 /**
456 * wctc4xxp_skb_to_cmd - Convert a socket buffer (skb) to a tcb
457 * @wc: The transcoder that we're going to send this command to.
458 * @skb: socket buffer to convert.
459 *
460 */
461 static struct tcb *
462 wctc4xxp_skb_to_cmd(struct wcdte *wc, const struct sk_buff *skb)
463 {
464 const gfp_t alloc_flags = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
465 struct tcb *cmd;
466 cmd = __alloc_cmd(skb->len, alloc_flags, 0);
467 if (cmd) {
468 int res;
469 cmd->data_len = skb->len;
470 res = skb_copy_bits(skb, 0, cmd->data, cmd->data_len);
471 if (res) {
472 dev_warn(&wc->pdev->dev,
473 "Failed call to skb_copy_bits.\n");
474 free_cmd(cmd);
475 cmd = NULL;
476 }
477 }
478 return cmd;
479 }
480
481 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0)
482 static void
483 wctc4xxp_net_set_multi(struct net_device *netdev)
484 {
485 struct wcdte *wc = wcdte_from_netdev(netdev);
486 DTE_DEBUG(DTE_DEBUG_GENERAL, "%s promiscuity:%d\n",
487 __func__, netdev->promiscuity);
488 }
489 #else
490 static void
491 wctc4xxp_set_rx_mode(struct net_device *netdev)
492 {
493 struct wcdte *wc = wcdte_from_netdev(netdev);
494 DTE_DEBUG(DTE_DEBUG_GENERAL, "%s promiscuity:%d\n",
495 __func__, netdev->promiscuity);
496 }
497 #endif
498
499 static int
500 wctc4xxp_net_up(struct net_device *netdev)
501 {
502 struct wcdte *wc = wcdte_from_netdev(netdev);
503 DTE_DEBUG(DTE_DEBUG_GENERAL, "%s\n", __func__);
504 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
505 netif_poll_enable(netdev);
506 #else
507 napi_enable(&wc->napi);
508 #endif
509 return 0;
510 }
511
512 static int
513 wctc4xxp_net_down(struct net_device *netdev)
514 {
515 struct wcdte *wc = wcdte_from_netdev(netdev);
516 DTE_DEBUG(DTE_DEBUG_GENERAL, "%s\n", __func__);
517 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
518 netif_poll_disable(netdev);
519 #else
520 napi_disable(&wc->napi);
521 #endif
522 return 0;
523 }
524
525 static void wctc4xxp_transmit_cmd(struct wcdte *, struct tcb *);
526
527 static int
528 wctc4xxp_net_hard_start_xmit(struct sk_buff *skb, struct net_device *netdev)
529 {
530 struct wcdte *wc = wcdte_from_netdev(netdev);
531 struct tcb *cmd;
532
533 /* We set DO_NOT_CAPTURE because this packet was already captured by
534 * in code higher up in the networking stack. We don't want to
535 * capture it twice.
536 */
537 cmd = wctc4xxp_skb_to_cmd(wc, skb);
538 if (cmd) {
539 cmd->flags |= DO_NOT_CAPTURE;
540 wctc4xxp_transmit_cmd(wc, cmd);
541 }
542
543 dev_kfree_skb_any(skb);
544 return NETDEV_TX_OK;
545 }
546
547 static int
548 wctc4xxp_net_receive(struct wcdte *wc, int max)
549 {
550 int count = 0;
551 struct sk_buff *skb;
552 WARN_ON(0 == max);
553 while ((skb = skb_dequeue(&wc->captured_packets))) {
554 netif_receive_skb(skb);
555 if (++count >= max)
556 break;
557 }
558 return count;
559 }
560
561 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
562 static int
563 wctc4xxp_poll(struct net_device *netdev, int *budget)
564 {
565 struct wcdte *wc = wcdte_from_netdev(netdev);
566 int count = 0;
567 int quota = min(netdev->quota, *budget);
568
569 count = wctc4xxp_net_receive(wc, quota);
570
571 *budget -= count;
572 netdev->quota -= count;
573
574 if (!skb_queue_len(&wc->captured_packets)) {
575 netif_rx_complete(netdev);
576 return 0;
577 } else {
578 return -1;
579 }
580 }
581 #else
582 static int
583 wctc4xxp_poll(struct napi_struct *napi, int budget)
584 {
585 struct wcdte *wc = container_of(napi, struct wcdte, napi);
586 int count;
587
588 count = wctc4xxp_net_receive(wc, budget);
589
590 if (!skb_queue_len(&wc->captured_packets)) {
591 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
592 netif_rx_complete(wc->netdev, &wc->napi);
593 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
594 netif_rx_complete(&wc->napi);
595 #else
596 napi_complete(&wc->napi);
597 #endif
598 }
599 return count;
600 }
601 #endif
602
603 static struct net_device_stats *
604 wctc4xxp_net_get_stats(struct net_device *netdev)
605 {
606 struct wcdte *wc = wcdte_from_netdev(netdev);
607 return &wc->net_stats;
608 }
609
610 /* Wait until this device is put into promiscuous mode, or we timeout. */
611 static void
612 wctc4xxp_net_waitfor_promiscuous(struct wcdte *wc)
613 {
614 unsigned int seconds = 15;
615 unsigned long start = jiffies;
616 struct net_device *netdev = wc->netdev;
617
618 dev_info(&wc->pdev->dev,
619 "Waiting %d seconds for adapter to be placed in " \
620 "promiscuous mode for early trace.\n", seconds);
621
622 while (!netdev->promiscuity) {
623 if (signal_pending(current)) {
624 dev_info(&wc->pdev->dev,
625 "Aborting wait due to signal.\n");
626 break;
627 }
628 msleep(100);
629 if (time_after(jiffies, start + (seconds * HZ))) {
630 dev_info(&wc->pdev->dev,
631 "Aborting wait due to timeout.\n");
632 break;
633 }
634 }
635 }
636
637 #ifdef HAVE_NET_DEVICE_OPS
638 static const struct net_device_ops wctc4xxp_netdev_ops = {
639 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0)
640 .ndo_set_multicast_list = &wctc4xxp_net_set_multi,
641 #else
642 .ndo_set_rx_mode = &wctc4xxp_set_rx_mode,
643 #endif
644 .ndo_open = &wctc4xxp_net_up,
645 .ndo_stop = &wctc4xxp_net_down,
646 .ndo_start_xmit = &wctc4xxp_net_hard_start_xmit,
647 .ndo_get_stats = &wctc4xxp_net_get_stats,
648 };
649 #endif
650
651 /**
652 * wctc4xxp_net_register - Register a new network interface.
653 * @wc: transcoder card to register the interface for.
654 *
655 * The network interface is primarily used for debugging in order to watch the
656 * traffic between the transcoder and the host.
657 *
658 */
659 static int
660 wctc4xxp_net_register(struct wcdte *wc)
661 {
662 int res;
663 struct net_device *netdev;
664 struct wcdte_netdev_priv *priv;
665 const char our_mac[] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
666
667 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
668 netdev = alloc_netdev(sizeof(*priv), wc->board_name,
669 NET_NAME_UNKNOWN, ether_setup);
670 #else
671 netdev = alloc_netdev(sizeof(*priv), wc->board_name, ether_setup);
672 #endif
673
674 if (!netdev)
675 return -ENOMEM;
676 priv = netdev_priv(netdev);
677 priv->wc = wc;
678 memcpy(netdev->dev_addr, our_mac, sizeof(our_mac));
679
680 # ifdef HAVE_NET_DEVICE_OPS
681 netdev->netdev_ops = &wctc4xxp_netdev_ops;
682 # else
683 netdev->set_multicast_list = &wctc4xxp_net_set_multi;
684 netdev->open = &wctc4xxp_net_up;
685 netdev->stop = &wctc4xxp_net_down;
686 netdev->hard_start_xmit = &wctc4xxp_net_hard_start_xmit;
687 netdev->get_stats = &wctc4xxp_net_get_stats;
688 # endif
689
690 netdev->promiscuity = 0;
691 netdev->flags |= IFF_NOARP;
692
693 # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
694 netdev->poll = &wctc4xxp_poll;
695 netdev->weight = 64;
696 # else
697 netif_napi_add(netdev, &wc->napi, &wctc4xxp_poll, 64);
698 # endif
699
700 res = register_netdev(netdev);
701 if (res) {
702 dev_warn(&wc->pdev->dev,
703 "Failed to register network device %s.\n",
704 wc->board_name);
705 goto error_sw;
706 }
707
708 wc->netdev = netdev;
709 skb_queue_head_init(&wc->captured_packets);
710
711 if (debug & DTE_DEBUG_NETWORK_EARLY)
712 wctc4xxp_net_waitfor_promiscuous(wc);
713
714 dev_info(&wc->pdev->dev,
715 "Created network device %s for debug.\n", wc->board_name);
716 return 0;
717
718 error_sw:
719 if (netdev)
720 free_netdev(netdev);
721 return res;
722 }
723
724 static void
725 wctc4xxp_net_unregister(struct wcdte *wc)
726 {
727 struct sk_buff *skb;
728
729 if (!wc->netdev)
730 return;
731 unregister_netdev(wc->netdev);
732 while ((skb = skb_dequeue(&wc->captured_packets)))
733 kfree_skb(skb);
734 free_netdev(wc->netdev);
735 wc->netdev = NULL;
736 }
737
738
739 /**
740 * wctc4xxp_net_capture_cmd - Send a tcb to the network stack.
741 * @wc: transcoder that received the command.
742 * @cmd: command to send to network stack.
743 *
744 */
745 static void
746 wctc4xxp_net_capture_cmd(struct wcdte *wc, const struct tcb *cmd)
747 {
748 struct sk_buff *skb;
749 struct net_device *netdev = wc->netdev;
750
751 if (!netdev)
752 return;
753
754 /* No need to capture if there isn't anyone listening. */
755 if (!(netdev->flags & IFF_UP))
756 return;
757
758 if (skb_queue_len(&wc->captured_packets) > MAX_CAPTURED_PACKETS) {
759 WARN_ON_ONCE(1);
760 return;
761 }
762
763 skb = tcb_to_skb(netdev, cmd);
764 if (!skb)
765 return;
766
767 skb_queue_tail(&wc->captured_packets, skb);
768 # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
769 netif_rx_schedule(netdev);
770 # elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
771 netif_rx_schedule(netdev, &wc->napi);
772 # elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
773 netif_rx_schedule(&wc->napi);
774 # else
775 napi_schedule(&wc->napi);
776 # endif
777 return;
778 }
779
780
781 /*! In-memory structure shared by the host and the adapter. */
782 struct wctc4xxp_descriptor {
783 __le32 des0;
784 __le32 des1;
785 __le32 buffer1;
786 __le32 container; /* Unused */
787 } __attribute__((packed));
788
789 struct wctc4xxp_descriptor_ring {
790 /* Pointer to an array of descriptors to give to hardware. */
791 struct wctc4xxp_descriptor *desc;
792 /* Read completed buffers from the head. */
793 unsigned int head;
794 /* Write ready buffers to the tail. */
795 unsigned int tail;
796 /* Array to save the kernel virtual address of pending commands. */
797 struct tcb **pending;
798 /* PCI Bus address of the descriptor list. */
799 dma_addr_t desc_dma;
800 /*! either DMA_FROM_DEVICE or DMA_TO_DEVICE */
801 unsigned int direction;
802 /*! The number of buffers currently submitted to the hardware. */
803 unsigned int count;
804 /*! The number of bytes to pad each descriptor for cache alignment. */
805 unsigned int padding;
806 /*! Protects this structure from concurrent access. */
807 spinlock_t lock;
808 /*! PCI device for the card associated with this ring. */
809 struct pci_dev *pdev;
810 /*! The size of the dring. */
811 unsigned long size;
812 /*! Total number of packets completed. */
813 unsigned long packet_count;
814 /*! Total number of packets with errors. */
815 unsigned long packet_errors;
816 };
817
818 /**
819 * wctc4xxp_descriptor - Returns the desriptor at index.
820 * @dr: The descriptor ring we're using.
821 * @index: index of the descriptor we want.
822 *
823 * We need this function because we do not know what the padding on the
824 * descriptors will be. Otherwise, we would just use an array.
825 */
826 static inline struct wctc4xxp_descriptor *
827 wctc4xxp_descriptor(struct wctc4xxp_descriptor_ring *dr, int index)
828 {
829 return (struct wctc4xxp_descriptor *)((u8 *)dr->desc +
830 ((sizeof(*dr->desc) + dr->padding) * index));
831 }
832
833 static int
834 wctc4xxp_initialize_descriptor_ring(struct pci_dev *pdev,
835 struct wctc4xxp_descriptor_ring *dr, u32 des1, unsigned int direction,
836 unsigned long size)
837 {
838 int i;
839 const u32 END_OF_RING = 0x02000000;
840 u8 cache_line_size = 0;
841 int add_padding;
842 struct wctc4xxp_descriptor *d = NULL;
843
844 BUG_ON(!pdev);
845 BUG_ON(!dr);
846
847 if (pci_read_config_byte(pdev, 0x0c, &cache_line_size))
848 return -EIO;
849
850 memset(dr, 0, sizeof(*dr));
851 dr->size = size;
852
853 /*
854 * Add some padding to each descriptor to ensure that they are
855 * aligned on host system cache-line boundaries, but only for the
856 * cache-line sizes that we support.
857 *
858 */
859 add_padding = (0x08 == cache_line_size) ||
860 (0x10 == cache_line_size) ||
861 (0x20 == cache_line_size);
862 if (add_padding)
863 dr->padding = (cache_line_size*sizeof(u32)) - sizeof(*d);
864
865 dr->pending = kmalloc(sizeof(struct tcb *) * dr->size, GFP_KERNEL);
866 if (!dr->pending)
867 return -ENOMEM;
868
869 dr->desc = pci_alloc_consistent(pdev,
870 (sizeof(*d)+dr->padding)*dr->size, &dr->desc_dma);
871 if (!dr->desc) {
872 kfree(dr->pending);
873 return -ENOMEM;
874 }
875
876 memset(dr->desc, 0, (sizeof(*d) + dr->padding) * dr->size);
877 for (i = 0; i < dr->size; ++i) {
878 d = wctc4xxp_descriptor(dr, i);
879 memset(d, 0, sizeof(*d));
880 d->des1 = cpu_to_le32(des1);
881 }
882
883 d->des1 |= cpu_to_le32(END_OF_RING);
884 dr->direction = direction;
885 spin_lock_init(&dr->lock);
886 dr->pdev = pdev;
887 return 0;
888 }
889
890 #define OWN_BIT cpu_to_le32(0x80000000)
891 #define OWNED(_d_) (((_d_)->des0)&OWN_BIT)
892 #define SET_OWNED(_d_) do { wmb(); (_d_)->des0 |= OWN_BIT; wmb(); } while (0)
893
894 static const unsigned int BUFFER1_SIZE_MASK = 0x7ff;
895
896 static int
897 wctc4xxp_submit(struct wctc4xxp_descriptor_ring *dr, struct tcb *c)
898 {
899 volatile struct wctc4xxp_descriptor *d;
900 unsigned int len;
901 unsigned long flags;
902
903 WARN_ON(!c);
904 len = (c->data_len < MIN_PACKET_LEN) ? MIN_PACKET_LEN : c->data_len;
905 if (c->data_len > MAX_FRAME_SIZE) {
906 WARN_ON_ONCE(!"Invalid command length passed\n");
907 c->data_len = MAX_FRAME_SIZE;
908 }
909
910 spin_lock_irqsave(&dr->lock, flags);
911 d = wctc4xxp_descriptor(dr, dr->tail);
912 WARN_ON(!d);
913 if (d->buffer1) {
914 spin_unlock_irqrestore(&dr->lock, flags);
915 /* Do not overwrite a buffer that is still in progress. */
916 return -EBUSY;
917 }
918 d->des1 &= cpu_to_le32(~(BUFFER1_SIZE_MASK));
919 d->des1 |= cpu_to_le32(len & BUFFER1_SIZE_MASK);
920 d->buffer1 = cpu_to_le32(pci_map_single(dr->pdev, c->data,
921 SFRAME_SIZE, dr->direction));
922
923 SET_OWNED(d); /* That's it until the hardware is done with it. */
924 dr->pending[dr->tail] = c;
925 dr->tail = (dr->tail + 1) & (dr->size-1);
926 ++dr->count;
927 spin_unlock_irqrestore(&dr->lock, flags);
928 return 0;
929 }
930
931 static inline struct tcb*
932 wctc4xxp_retrieve(struct wctc4xxp_descriptor_ring *dr)
933 {
934 volatile struct wctc4xxp_descriptor *d;
935 struct tcb *c;
936 unsigned int head = dr->head;
937 unsigned long flags;
938 u32 des0;
939 spin_lock_irqsave(&dr->lock, flags);
940 d = wctc4xxp_descriptor(dr, head);
941 if (d->buffer1 && !OWNED(d)) {
942 pci_unmap_single(dr->pdev, le32_to_cpu(d->buffer1),
943 SFRAME_SIZE, dr->direction);
944 c = dr->pending[head];
945 WARN_ON(!c);
946 dr->head = (++head) & (dr->size-1);
947 d->buffer1 = 0;
948 --dr->count;
949 WARN_ON(!c);
950 des0 = le32_to_cpu(d->des0);
951 c->data_len = (des0 >> 16) & BUFFER1_SIZE_MASK;
952 if (des0 & (1<<15)) {
953 ++dr->packet_errors;
954 /* The upper layers won't be able to do anything with
955 * this packet. Free it up and log the error. */
956 free_cmd(c);
957 c = NULL;
958 } else {
959 ++dr->packet_count;
960 WARN_ON(c->data_len > SFRAME_SIZE);
961 }
962 } else {
963 c = NULL;
964 }
965 spin_unlock_irqrestore(&dr->lock, flags);
966 return c;
967 }
968
969 static inline int wctc4xxp_getcount(struct wctc4xxp_descriptor_ring *dr)
970 {
971 int count;
972 unsigned long flags;
973 spin_lock_irqsave(&dr->lock, flags);
974 count = dr->count;
975 spin_unlock_irqrestore(&dr->lock, flags);
976 return count;
977 }
978
979 static inline int wctc4xxp_get_packet_count(struct wctc4xxp_descriptor_ring *dr)
980 {
981 unsigned long count;
982 unsigned long flags;
983 spin_lock_irqsave(&dr->lock, flags);
984 count = dr->packet_count;
985 spin_unlock_irqrestore(&dr->lock, flags);
986 return count;
987 }
988
989 static inline int
990 wctc4xxp_get_packet_errors(struct wctc4xxp_descriptor_ring *dr)
991 {
992 unsigned long count;
993 unsigned long flags;
994 spin_lock_irqsave(&dr->lock, flags);
995 count = dr->packet_errors;
996 spin_unlock_irqrestore(&dr->lock, flags);
997 return count;
998 }
999
1000 static inline void
1001 wctc4xxp_set_packet_count(struct wctc4xxp_descriptor_ring *dr,
1002 unsigned long count)
1003 {
1004 unsigned long flags;
1005 spin_lock_irqsave(&dr->lock, flags);
1006 dr->packet_count = count;
1007 spin_unlock_irqrestore(&dr->lock, flags);
1008 }
1009
1010 static inline void
1011 __wctc4xxp_setctl(struct wcdte *wc, unsigned int addr, unsigned int val)
1012 {
1013 writel(val, wc->iobase + addr);
1014 readl(wc->iobase + addr);
1015 }
1016
1017 static inline unsigned int
1018 __wctc4xxp_getctl(struct wcdte *wc, unsigned int addr)
1019 {
1020 return readl(wc->iobase + addr);
1021 }
1022
1023 static inline void
1024 wctc4xxp_setctl(struct wcdte *wc, unsigned int addr, unsigned int val)
1025 {
1026 unsigned long flags;
1027 spin_lock_irqsave(&wc->reglock, flags);
1028 __wctc4xxp_setctl(wc, addr, val);
1029 spin_unlock_irqrestore(&wc->reglock, flags);
1030 }
1031
1032 static inline void
1033 wctc4xxp_receive_demand_poll(struct wcdte *wc)
1034 {
1035 __wctc4xxp_setctl(wc, 0x0010, 0x00000000);
1036 }
1037
1038 static inline void
1039 wctc4xxp_transmit_demand_poll(struct wcdte *wc)
1040 {
1041 return;
1042 # if 0
1043 __wctc4xxp_setctl(wc, 0x0008, 0x00000000);
1044
1045 /* \todo Investigate why this register needs to be written twice in
1046 * order to get it to poll reliably. So far, most of the problems
1047 * I've seen with timeouts had more to do with an untransmitted
1048 * packet sitting in the outbound descriptor list as opposed to any
1049 * problem with the dte firmware.
1050 */
1051 __wctc4xxp_setctl(wc, 0x0008, 0x00000000);
1052 #endif
1053 }
1054
1055 #define LENGTH_WITH_N_PARAMETERS(__n) (((__n) * sizeof(u16)) + \
1056 sizeof(struct csm_encaps_cmd))
1057
1058 static const u8 dst_mac[6] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
1059 static const u8 src_mac[6] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
1060
1061 static int wctc4xxp_transmit_cmd_and_wait(struct wcdte *wc, struct tcb *cmd);
1062
1063 static void
1064 setup_common_header(struct wcdte *wc, struct csm_encaps_hdr *hdr)
1065 {
1066 memcpy(hdr->ethhdr.h_dest, dst_mac, sizeof(dst_mac));
1067 memcpy(hdr->ethhdr.h_source, src_mac, sizeof(src_mac));
1068 hdr->ethhdr.h_proto = cpu_to_be16(ETH_P_CSM_ENCAPS);
1069 }
1070
1071 static void
1072 setup_supervisor_header(struct wcdte *wc, struct csm_encaps_hdr *hdr)
1073 {
1074 setup_common_header(wc, hdr);
1075
1076 hdr->op_code = cpu_to_be16(CONTROL_PACKET_OPCODE);
1077 hdr->control = LITTLE_ENDIAN;
1078 hdr->seq_num = (wc->seq_num++)&0xf;
1079 hdr->channel = cpu_to_be16(SUPERVISOR_CHANNEL);
1080 }
1081
1082 static void
1083 create_supervisor_cmd(struct wcdte *wc, struct tcb *cmd, u8 type, u8 class,
1084 u16 function, const u16 *parameters, const int num_parameters)
1085 {
1086 struct csm_encaps_hdr *hdr = cmd->data;
1087 int i;
1088
1089 if (cmd->response) {
1090 free_cmd(cmd->response);
1091 cmd->response = NULL;
1092 }
1093
1094 setup_supervisor_header(wc, hdr);
1095
1096 hdr->cmd.length = LENGTH_WITH_N_PARAMETERS(num_parameters);
1097 hdr->cmd.index = 0;
1098 hdr->cmd.type = type;
1099 hdr->cmd.class = class;
1100 hdr->cmd.function = cpu_to_le16(function);
1101 hdr->cmd.reserved = 0;
1102
1103 for (i = 0; i < num_parameters; ++i)
1104 hdr->cmd.params[i] = cpu_to_le16(parameters[i]);
1105
1106 cmd->flags = WAIT_FOR_RESPONSE;
1107 cmd->data_len = sizeof(struct csm_encaps_hdr) -
1108 sizeof(struct csm_encaps_cmd) +
1109 hdr->cmd.length;
1110 cmd->cpvt = NULL;
1111 }
1112
1113 static void
1114 setup_channel_header(struct channel_pvt *pvt, struct tcb *cmd)
1115 {
1116 struct csm_encaps_hdr *hdr = cmd->data;
1117
1118 if (cmd->response) {
1119 free_cmd(cmd->response);
1120 cmd->response = NULL;
1121 }
1122
1123 setup_common_header(pvt->wc, hdr);
1124 hdr->op_code = cpu_to_be16(CONTROL_PACKET_OPCODE);
1125 hdr->seq_num = (pvt->cmd_seqno++)&0xf;
1126 hdr->channel = cpu_to_be16(pvt->chan_in_num);
1127
1128 cmd->flags = WAIT_FOR_RESPONSE;
1129 cmd->data_len = sizeof(struct csm_encaps_hdr) -
1130 sizeof(struct csm_encaps_cmd);
1131 cmd->cpvt = pvt;
1132 cmd->next_index = 0;
1133 }
1134
1135
1136 static void
1137 append_channel_cmd(struct tcb *cmd, u8 type, u8 class, u16 function,
1138 const u16 *parameters, int num_parameters)
1139 {
1140 int i;
1141 struct csm_encaps_cmd *csm_cmd = cmd->data + cmd->data_len;
1142
1143 csm_cmd->length = LENGTH_WITH_N_PARAMETERS(num_parameters);
1144 csm_cmd->index = cmd->next_index++;
1145 csm_cmd->type = type;
1146 csm_cmd->class = class;
1147 csm_cmd->function = cpu_to_le16(function);
1148 csm_cmd->reserved = 0;
1149
1150 for (i = 0; i < num_parameters; ++i)
1151 csm_cmd->params[i] = cpu_to_le16(parameters[i]);
1152
1153 cmd->data_len += csm_cmd->length;
1154 /* Pad it out to a DW boundary */
1155 if (cmd->data_len % 4)
1156 cmd->data_len += 4 - (cmd->data_len % 4);
1157 WARN_ON(cmd->data_len >= SFRAME_SIZE);
1158 }
1159
1160 static void
1161 create_channel_cmd(struct channel_pvt *pvt, struct tcb *cmd, u8 type, u8 class,
1162 u16 function, const u16 *parameters, int num_parameters)
1163 {
1164 setup_channel_header(pvt, cmd);
1165 append_channel_cmd(cmd, type, class, function, parameters,
1166 num_parameters);
1167 }
1168
1169 static int
1170 send_create_channel_cmd(struct wcdte *wc, struct tcb *cmd, u16 timeslot,
1171 u16 *channel_number)
1172 {
1173 int res;
1174 const u16 parameters[] = {0x0002, timeslot};
1175
1176 create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1177 CONFIG_DEVICE_CLASS, SUPVSR_CREATE_CHANNEL,
1178 parameters, ARRAY_SIZE(parameters));
1179
1180 res = wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1181 if (res)
1182 return res;
1183
1184 if (0x0000 != response_header(cmd)->cmd.params[0]) {
1185 if (printk_ratelimit()) {
1186 dev_warn(&wc->pdev->dev,
1187 "Failed to create channel in timeslot " \
1188 "%d. Response from DTE was (%04x).\n",
1189 timeslot, response_header(cmd)->cmd.params[0]);
1190 }
1191 free_cmd(cmd->response);
1192 cmd->response = NULL;
1193 return -EIO;
1194 }
1195
1196 *channel_number = le16_to_cpu(response_header(cmd)->cmd.params[1]);
1197 free_cmd(cmd->response);
1198 cmd->response = NULL;
1199 return 0;
1200 }
1201
1202 static int
1203 send_set_arm_clk_cmd(struct wcdte *wc, struct tcb *cmd)
1204 {
1205 const u16 parameters[] = {0x012c, 0x0000};
1206 create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1207 CONFIG_DEVICE_CLASS, 0x0411, parameters,
1208 ARRAY_SIZE(parameters));
1209 return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1210 }
1211
1212 static int
1213 send_set_spu_clk_cmd(struct wcdte *wc, struct tcb *cmd)
1214 {
1215 const u16 parameters[] = {0x012c, 0x0000};
1216 create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1217 CONFIG_DEVICE_CLASS, 0x0412, parameters,
1218 ARRAY_SIZE(parameters));
1219 return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1220 }
1221
1222 static int
1223 send_tdm_select_bus_mode_cmd(struct wcdte *wc, struct tcb *cmd)
1224 {
1225 const u16 parameters[] = {0x0004};
1226 create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1227 CONFIG_DEVICE_CLASS, 0x0417, parameters,
1228 ARRAY_SIZE(parameters));
1229 return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1230 }
1231
1232 static int
1233 send_set_eth_header_cmd(struct wcdte *wc, struct tcb *cmd,
1234 const u8 *host_mac, const u8 *assigned_mac)
1235 {
1236 u16 parameters[8];
1237 u16 *part;
1238
1239 parameters[0] = 0x0001;
1240 part = (u16 *)host_mac;
1241 parameters[1] = part[0];
1242 parameters[2] = part[1];
1243 parameters[3] = part[2];
1244 part = (u16 *)assigned_mac;
1245 parameters[4] = part[0];
1246 parameters[5] = part[1];
1247 parameters[6] = part[2];
1248 parameters[7] = 0x0008;
1249
1250 create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1251 CONFIG_DEVICE_CLASS, 0x0100, parameters,
1252 ARRAY_SIZE(parameters));
1253 return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1254 }
1255
1256 static int
1257 send_supvsr_setup_tdm_parms(struct wcdte *wc, struct tcb *cmd,
1258 u8 bus_number)
1259 {
1260 const u16 parameters[] = {0x8380, 0x0c00, 0, (bus_number << 2)&0xc};
1261 create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1262 CONFIG_DEVICE_CLASS, 0x0407, parameters,
1263 ARRAY_SIZE(parameters));
1264 return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1265 }
1266
1267 static int
1268 send_ip_service_config_cmd(struct wcdte *wc, struct tcb *cmd)
1269 {
1270 const u16 parameters[] = {0x0200};
1271 create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1272 CONFIG_DEVICE_CLASS, 0x0302, parameters,
1273 ARRAY_SIZE(parameters));
1274 return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1275 }
1276
1277 static int
1278 send_arp_service_config_cmd(struct wcdte *wc, struct tcb *cmd)
1279 {
1280 const u16 parameters[] = {0x0001};
1281 create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1282 CONFIG_DEVICE_CLASS, 0x0105, parameters,
1283 ARRAY_SIZE(parameters));
1284 return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1285 }
1286
1287 static int
1288 send_icmp_service_config_cmd(struct wcdte *wc, struct tcb *cmd)
1289 {
1290 const u16 parameters[] = {0xff01};
1291 create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1292 CONFIG_DEVICE_CLASS, 0x0304, parameters,
1293 ARRAY_SIZE(parameters));
1294 return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1295 }
1296
1297 static int
1298 send_device_set_country_code_cmd(struct wcdte *wc, struct tcb *cmd)
1299 {
1300 const u16 parameters[] = {0x0000};
1301 create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1302 CONFIG_DEVICE_CLASS, 0x041b, parameters,
1303 ARRAY_SIZE(parameters));
1304 return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1305 }
1306
1307 static int
1308 send_spu_features_control_cmd(struct wcdte *wc, struct tcb *cmd, u16 options)
1309 {
1310 const u16 parameters[] = {options};
1311 create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1312 CONFIG_DEVICE_CLASS, 0x0013, parameters,
1313 ARRAY_SIZE(parameters));
1314 return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1315 }
1316
1317 /* Allows sending more than one CSM_ENCAPS packet in a single ethernet frame. */
1318 static int send_csme_multi_cmd(struct wcdte *wc, struct tcb *cmd)
1319 {
1320 const u16 parameters[] = {0x1};
1321 create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1322 CONFIG_DEVICE_CLASS, 0x010a, parameters,
1323 ARRAY_SIZE(parameters));
1324 return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1325 }
1326
1327 static int
1328 send_tdm_opt_cmd(struct wcdte *wc, struct tcb *cmd)
1329 {
1330 const u16 parameters[] = {0x0000};
1331 create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1332 CONFIG_DEVICE_CLASS, 0x0435, parameters,
1333 ARRAY_SIZE(parameters));
1334 return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1335 }
1336
1337 static int
1338 send_destroy_channel_cmd(struct wcdte *wc, struct tcb *cmd, u16 channel)
1339 {
1340 int res;
1341 u16 result;
1342 const u16 parameters[] = {channel};
1343 create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1344 CONFIG_DEVICE_CLASS, 0x0011, parameters,
1345 ARRAY_SIZE(parameters));
1346 res = wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1347 if (res)
1348 return res;
1349 /* Let's check the response for any error codes.... */
1350 result = le16_to_cpu(response_header(cmd)->cmd.params[0]);
1351 if (0x0000 != result) {
1352 dev_err(&wc->pdev->dev,
1353 "Failed to destroy channel %04d (%04x)\n",
1354 channel, result);
1355 return -EIO;
1356 }
1357 return 0;
1358 }
1359
1360 static void
1361 append_set_ip_hdr_channel_cmd(struct tcb *cmd)
1362 {
1363 const u16 parameters[] = {0, 0x0045, 0, 0, 0x0040, 0x1180, 0,
1364 0xa8c0, 0x0309, 0xa8c0, 0x0309,
1365 swab16(cmd->cpvt->timeslot_out_num + 0x5000),
1366 swab16(cmd->cpvt->timeslot_in_num + 0x5000),
1367 0, 0};
1368 append_channel_cmd(cmd, CONFIG_CHANGE_TYPE, CONFIG_CHANNEL_CLASS,
1369 0x9000, parameters, ARRAY_SIZE(parameters));
1370 }
1371
1372 static void
1373 append_voip_vceopt_cmd(struct tcb *cmd, u16 length)
1374 {
1375 const u16 parameters[] = {((length << 8)|0x21), 0x1c00,
1376 0x0004, 0, 0};
1377 append_channel_cmd(cmd, CONFIG_CHANGE_TYPE, CONFIG_CHANNEL_CLASS,
1378 0x8001, parameters, ARRAY_SIZE(parameters));
1379 }
1380
1381 static void
1382 append_voip_tonectl_cmd(struct tcb *cmd)
1383 {
1384 const u16 parameters[] = {0};
1385 append_channel_cmd(cmd, CONFIG_CHANGE_TYPE, CONFIG_CHANNEL_CLASS,
1386 0x805b, parameters, ARRAY_SIZE(parameters));
1387 }
1388
1389 static void
1390 append_voip_dtmfopt_cmd(struct tcb *cmd)
1391 {
1392 const u16 parameters[] = {0x0008};
1393 append_channel_cmd(cmd, CONFIG_CHANGE_TYPE, CONFIG_CHANNEL_CLASS,
1394 0x8002, parameters, ARRAY_SIZE(parameters));
1395 }
1396
1397 static void
1398 append_voip_indctrl_cmd(struct tcb *cmd)
1399 {
1400 const u16 parameters[] = {0x0007};
1401 append_channel_cmd(cmd, CONFIG_CHANGE_TYPE, CONFIG_CHANNEL_CLASS,
1402 0x8084, parameters, ARRAY_SIZE(parameters));
1403 }
1404
1405 static void
1406 send_voip_vopena_cmd(struct channel_pvt *pvt, struct tcb *cmd, u8 format)
1407 {
1408 const u16 parameters[] = {1, ((format<<8)|0x80), 0, 0, 0,
1409 0x3412, 0x7856};
1410 create_channel_cmd(pvt, cmd, CONFIG_CHANGE_TYPE, CONFIG_CHANNEL_CLASS,
1411 0x8000, parameters, ARRAY_SIZE(parameters));
1412 wctc4xxp_transmit_cmd(pvt->wc, cmd);
1413 }
1414
1415 static int
1416 send_voip_vopena_close_cmd(struct channel_pvt *pvt, struct tcb *cmd)
1417 {
1418 int res;
1419 const u16 parameters[] = {0};
1420 create_channel_cmd(pvt, cmd, CONFIG_CHANGE_TYPE, CONFIG_CHANNEL_CLASS,
1421 0x8000, parameters, ARRAY_SIZE(parameters));
1422 res = wctc4xxp_transmit_cmd_and_wait(pvt->wc, cmd);
1423 if (res)
1424 return res;
1425 /* Let's check the response for any error codes.... */
1426 if (0x0000 != response_header(cmd)->cmd.params[0]) {
1427 WARN_ON(1);
1428 return -EIO;
1429 }
1430 return 0;
1431 }
1432
1433 static int
1434 send_ip_options_cmd(struct wcdte *wc, struct tcb *cmd)
1435 {
1436 const u16 parameters[] = {0x0002};
1437 create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1438 CONFIG_DEVICE_CLASS, 0x0306, parameters,
1439 ARRAY_SIZE(parameters));
1440 return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1441 }
1442
1443 static int
1444 _send_trans_connect_cmd(struct wcdte *wc, struct tcb *cmd, u16 enable, u16
1445 encoder_channel, u16 decoder_channel, u16 encoder_format,
1446 u16 decoder_format)
1447 {
1448 int res;
1449 const u16 parameters[] = {enable, encoder_channel, encoder_format,
1450 decoder_channel, decoder_format};
1451 create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1452 CONFIG_DEVICE_CLASS, 0x9322, parameters,
1453 ARRAY_SIZE(parameters));
1454 res = wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1455 if (res)
1456 return res;
1457
1458 /* Let's check the response for any error codes.... */
1459 if (0x0000 != response_header(cmd)->cmd.params[0]) {
1460 WARN_ON(1);
1461 return -EIO;
1462 }
1463 return 0;
1464 }
1465
1466 static int
1467 send_trans_connect_cmd(struct wcdte *wc, struct tcb *cmd, const u16
1468 encoder_channel, const u16 decoder_channel, const u16 encoder_format,
1469 const u16 decoder_format)
1470 {
1471 return _send_trans_connect_cmd(wc, cmd, 1, encoder_channel,
1472 decoder_channel, encoder_format, decoder_format);
1473 }
1474
1475 static int
1476 send_trans_disconnect_cmd(struct wcdte *wc, struct tcb *cmd, const u16
1477 encoder_channel, const u16 decoder_channel, const u16 encoder_format,
1478 const u16 decoder_format)
1479 {
1480 return _send_trans_connect_cmd(wc, cmd, 0, encoder_channel,
1481 decoder_channel, encoder_format, decoder_format);
1482 }
1483
1484 static int
1485 send_voip_vceinfo_cmd(struct channel_pvt *pvt, struct tcb *cmd)
1486 {
1487 int res;
1488 const u16 parameters[] = {0};
1489 static const int CONFIG_CHANNEL_STATS_CLASS = 1;
1490 create_channel_cmd(pvt, cmd,
1491 VOIP_VCEINFO_TYPE, CONFIG_CHANNEL_STATS_CLASS,
1492 0x0000, parameters, 0);
1493 res = wctc4xxp_transmit_cmd_and_wait(pvt->wc, cmd);
1494 return res;
1495 }
1496
1497 static int
1498 send_eth_statistics_cmd(struct wcdte *wc, struct tcb *cmd)
1499 {
1500 int res;
1501 const u16 parameters[] = {0};
1502
1503 create_supervisor_cmd(wc, cmd, 0x00, 0x05, 0x0000,
1504 parameters, ARRAY_SIZE(parameters));
1505 res = wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1506 if (res)
1507 return -EIO;
1508 if (0x0000 != response_header(cmd)->cmd.params[0]) {
1509 dev_info(&wc->pdev->dev,
1510 "Failed to get ethernet stats: 0x%04x\n",
1511 response_header(cmd)->cmd.params[0]);
1512 res = -EIO;
1513 }
1514 return res;
1515 }
1516
1517 static void wctc4xxp_match_packet_counts(struct wcdte *wc)
1518 {
1519 struct tcb *cmd = alloc_cmd(SFRAME_SIZE);
1520 int res;
1521 u32 *parms;
1522
1523 res = send_eth_statistics_cmd(wc, cmd);
1524 if (0 == res) {
1525 parms = (u32 *)(&response_header(cmd)->cmd.params[0]);
1526 wctc4xxp_set_packet_count(wc->rxd, parms[1]);
1527 wctc4xxp_set_packet_count(wc->txd, parms[2]);
1528 }
1529 free_cmd(cmd);
1530 }
1531
1532 static inline u32 wctc4xxp_bytes_to_samples(u32 fmt, size_t count)
1533 {
1534 switch (fmt) {
1535 case DAHDI_FORMAT_G723_1:
1536 return count * (G723_SAMPLES/G723_5K_BYTES);
1537 case DAHDI_FORMAT_ULAW:
1538 case DAHDI_FORMAT_ALAW:
1539 return count;
1540 case DAHDI_FORMAT_G729A:
1541 return count * (G729_SAMPLES/G729_BYTES);
1542 default:
1543 WARN_ON(1);
1544 return 0;
1545 }
1546 }
1547
1548 static struct tcb *
1549 wctc4xxp_create_rtp_cmd(struct wcdte *wc, struct dahdi_transcoder_channel *dtc,
1550 size_t inbytes)
1551 {
1552 struct channel_pvt *cpvt = dtc->pvt;
1553 struct rtp_packet *packet;
1554 struct tcb *cmd;
1555
1556 cmd = alloc_cmd(sizeof(*packet) + inbytes);
1557 if (!cmd)
1558 return NULL;
1559
1560 cmd->cpvt = cpvt;
1561 packet = cmd->data;
1562
1563 BUG_ON(cmd->data_len < sizeof(*packet));
1564
1565 /* setup the ethernet header */
1566 memcpy(packet->ethhdr.h_dest, dst_mac, sizeof(dst_mac));
1567 memcpy(packet->ethhdr.h_source, src_mac, sizeof(src_mac));
1568 packet->ethhdr.h_proto = cpu_to_be16(ETH_P_IP);
1569
1570 /* setup the IP header */
1571 packet->iphdr.ihl = 5;
1572 packet->iphdr.version = 4;
1573 packet->iphdr.tos = 0;
1574 packet->iphdr.tot_len = cpu_to_be16(inbytes+40);
1575 packet->iphdr.id = 0;
1576 packet->iphdr.frag_off = cpu_to_be16(0x4000);
1577 packet->iphdr.ttl = 64;
1578 packet->iphdr.protocol = 0x11; /* UDP */
1579 packet->iphdr.check = 0;
1580 packet->iphdr.saddr = cpu_to_be32(0xc0a80903);
1581 packet->iphdr.daddr = cpu_to_be32(0xc0a80903);
1582
1583 packet->iphdr.check = ip_fast_csum((void *)&packet->iphdr,
1584 packet->iphdr.ihl);
1585
1586 /* setup the UDP header */
1587 packet->udphdr.source = cpu_to_be16(cpvt->timeslot_out_num + 0x5000);
1588 packet->udphdr.dest = cpu_to_be16(cpvt->timeslot_in_num + 0x5000);
1589 packet->udphdr.len = cpu_to_be16(inbytes + sizeof(struct rtphdr) +
1590 sizeof(struct udphdr));
1591 packet->udphdr.check = 0;
1592
1593 /* Setup the RTP header */
1594 packet->rtphdr.ver = 2;
1595 packet->rtphdr.padding = 0;
1596 packet->rtphdr.extension = 0;
1597 packet->rtphdr.csrc_count = 0;
1598 packet->rtphdr.marker = 0;
1599 packet->rtphdr.type = wctc4xxp_dahdifmt_to_dtefmt(dtc->srcfmt);
1600 packet->rtphdr.seqno = cpu_to_be16(cpvt->seqno);
1601 packet->rtphdr.timestamp = cpu_to_be32(cpvt->last_timestamp);
1602 packet->rtphdr.ssrc = cpu_to_be32(cpvt->ssrc);
1603
1604 cpvt->last_timestamp += wctc4xxp_bytes_to_samples(dtc->srcfmt,
1605 inbytes);
1606
1607 WARN_ON(cmd->data_len > SFRAME_SIZE);
1608 return cmd;
1609 }
1610 static void
1611 wctc4xxp_cleanup_descriptor_ring(struct wctc4xxp_descriptor_ring *dr)
1612 {
1613 int i;
1614 struct wctc4xxp_descriptor *d;
1615
1616 if (!dr || !dr->desc)
1617 return;
1618
1619 for (i = 0; i < dr->size; ++i) {
1620 d = wctc4xxp_descriptor(dr, i);
1621 if (d->buffer1) {
1622 pci_unmap_single(dr->pdev, d->buffer1,
1623 SFRAME_SIZE, dr->direction);
1624 d->buffer1 = 0;
1625 /* Commands will also be sitting on the waiting for
1626 * response list, so we want to make sure to delete
1627 * them from that list as well. */
1628 list_del_init(&(dr->pending[i])->node);
1629 free_cmd(dr->pending[i]);
1630 dr->pending[i] = NULL;
1631 }
1632 }
1633 dr->head = 0;
1634 dr->tail = 0;
1635 dr->count = 0;
1636 pci_free_consistent(dr->pdev, (sizeof(*d)+dr->padding) * dr->size,
1637 dr->desc, dr->desc_dma);
1638 kfree(dr->pending);
1639 }
1640
1641 static void wctc4xxp_timeout_all_commands(struct wcdte *wc)
1642 {
1643 struct tcb *cmd;
1644 struct tcb *temp;
1645 unsigned long flags;
1646 LIST_HEAD(local_list);
1647
1648 spin_lock_irqsave(&wc->cmd_list_lock, flags);
1649 list_splice_init(&wc->waiting_for_response_list, &local_list);
1650 list_splice_init(&wc->cmd_list, &local_list);
1651 spin_unlock_irqrestore(&wc->cmd_list_lock, flags);
1652
1653 list_for_each_entry_safe(cmd, temp, &local_list, node) {
1654 list_del_init(&cmd->node);
1655 if (cmd->complete) {
1656 cmd->flags |= DTE_CMD_TIMEOUT;
1657 complete(cmd->complete);
1658 } else {
1659 free_cmd(cmd);
1660 }
1661 }
1662 }
1663
1664 static void wctc4xxp_cleanup_command_list(struct wcdte *wc)
1665 {
1666 struct tcb *cmd;
1667 unsigned long flags;
1668 LIST_HEAD(local_list);
1669
1670 spin_lock_irqsave(&wc->cmd_list_lock, flags);
1671 list_splice_init(&wc->cmd_list, &local_list);
1672 list_splice_init(&wc->waiting_for_response_list, &local_list);
1673 list_splice_init(&wc->rx_list, &local_list);
1674 spin_unlock_irqrestore(&wc->cmd_list_lock, flags);
1675
1676 while (!list_empty(&local_list)) {
1677 cmd = list_entry(local_list.next, struct tcb, node);
1678 list_del_init(&cmd->node);
1679 free_cmd(cmd);
1680 }
1681 }
1682
1683 static inline bool is_rtp_packet(const struct tcb *cmd)
1684 {
1685 const struct ethhdr *ethhdr = cmd->data;
1686 return (cpu_to_be16(ETH_P_IP) == ethhdr->h_proto);
1687 }
1688
1689 static void
1690 wctc4xxp_transmit_cmd(struct wcdte *wc, struct tcb *cmd)
1691 {
1692 int res;
1693 unsigned long flags;
1694
1695 /* If we're shutdown all commands will timeout. Just complete the
1696 * command here with the timeout flag */
1697 if (unlikely(test_bit(DTE_SHUTDOWN, &wc->flags))) {
1698 if (cmd->complete) {
1699 cmd->flags |= DTE_CMD_TIMEOUT;
1700 list_del_init(&cmd->node);
1701 complete(cmd->complete);
1702 } else {
1703 list_del(&cmd->node);
1704 free_cmd(cmd);
1705 }
1706 return;
1707 }
1708
1709 if (cmd->data_len < MIN_PACKET_LEN) {
1710 memset((u8 *)(cmd->data) + cmd->data_len, 0,
1711 MIN_PACKET_LEN-cmd->data_len);
1712 cmd->data_len = MIN_PACKET_LEN;
1713 }
1714 WARN_ON(cmd->response);
1715 WARN_ON(cmd->flags & TX_COMPLETE);
1716 cmd->timeout = jiffies + HZ/4;
1717
1718 spin_lock_irqsave(&wc->cmd_list_lock, flags);
1719 if (cmd->flags & (WAIT_FOR_ACK | WAIT_FOR_RESPONSE)) {
1720 if (cmd->flags & WAIT_FOR_RESPONSE) {
1721 /* We don't need both an ACK and a response. Let's
1722 * tell the DTE not to generate an ACK, and we'll just
1723 * retry if we do not get the response within the
1724 * timeout period. */
1725 struct csm_encaps_hdr *hdr = cmd->data;
1726 hdr->control |= SUPPRESS_ACK;
1727 }
1728 WARN_ON(!list_empty(&cmd->node));
1729 list_add_tail(&cmd->node, &wc->waiting_for_response_list);
1730 mod_timer(&wc->watchdog, jiffies + HZ/2);
1731 }
1732 if (!list_empty(&wc->cmd_list)) {
1733 if (is_rtp_packet(cmd))
1734 list_add_tail(&cmd->node, &wc->cmd_list);
1735 else
1736 list_move(&cmd->node, &wc->cmd_list);
1737 spin_unlock_irqrestore(&wc->cmd_list_lock, flags);
1738 return;
1739 }
1740 res = wctc4xxp_submit(wc->txd, cmd);
1741 if (-EBUSY == res) {
1742 /* Looks like we're out of room in the descriptor
1743 * ring. We'll add this command to the pending list
1744 * and the interrupt service routine will pull from
1745 * this list as it clears up room in the descriptor
1746 * ring. */
1747 list_move_tail(&cmd->node, &wc->cmd_list);
1748 } else if (0 == res) {
1749 if (!(cmd->flags & DO_NOT_CAPTURE))
1750 wctc4xxp_net_capture_cmd(wc, cmd);
1751 wctc4xxp_transmit_demand_poll(wc);
1752 } else {
1753 /* Unknown return value... */
1754 WARN_ON(1);
1755 }
1756 spin_unlock_irqrestore(&wc->cmd_list_lock, flags);
1757 }
1758
1759 static int
1760 wctc4xxp_transmit_cmd_and_wait(struct wcdte *wc, struct tcb *cmd)
1761 {
1762 DECLARE_COMPLETION_ONSTACK(done);
1763 cmd->complete = &done;
1764 wctc4xxp_transmit_cmd(wc, cmd);
1765 wait_for_completion(&done);
1766 cmd->complete = NULL;
1767 if (cmd->flags & DTE_CMD_TIMEOUT) {
1768 DTE_DEBUG(DTE_DEBUG_GENERAL, "Timeout waiting for command.\n");
1769 return -EIO;
1770 }
1771 return 0;
1772 }
1773
1774 static int wctc4xxp_create_channel_pair(struct wcdte *wc,
1775 struct channel_pvt *cpvt, u8 simple, u8 complicated);
1776 static int wctc4xxp_destroy_channel_pair(struct wcdte *wc,
1777 struct channel_pvt *cpvt);
1778
1779 static void
1780 wctc4xxp_init_state(struct channel_pvt *cpvt, int encoder,
1781 unsigned int channel, struct wcdte *wc)
1782 {
1783 memset(cpvt, 0, sizeof(*cpvt));
1784 cpvt->encoder = encoder;
1785 cpvt->wc = wc;
1786 cpvt->chan_in_num = INVALID;
1787 cpvt->chan_out_num = INVALID;
1788 cpvt->ssrc = 0x78;
1789 cpvt->timeslot_in_num = channel*2;
1790 cpvt->timeslot_out_num = channel*2;
1791 cpvt->last_rx_seq_num = 0xff;
1792 if (encoder)
1793 ++cpvt->timeslot_out_num;
1794 else
1795 ++cpvt->timeslot_in_num;
1796 spin_lock_init(&cpvt->lock);
1797 INIT_LIST_HEAD(&cpvt->rx_queue);
1798 }
1799
1800 static unsigned int
1801 wctc4xxp_getctl(struct wcdte *wc, unsigned int addr)
1802 {
1803 unsigned int val;
1804 unsigned long flags;
1805 spin_lock_irqsave(&wc->reglock, flags);
1806 val = __wctc4xxp_getctl(wc, addr);
1807 spin_unlock_irqrestore(&wc->reglock, flags);
1808 return val;
1809 }
1810
1811 static void
1812 wctc4xxp_cleanup_channel_private(struct wcdte *wc,
1813 struct dahdi_transcoder_channel *dtc)
1814 {
1815 struct tcb *cmd, *temp;
1816 struct channel_pvt *cpvt = dtc->pvt;
1817 unsigned long flags;
1818 LIST_HEAD(local_list);
1819
1820 /* Once we cleanup this channel, we do not want any queued packets
1821 * waiting to be transmitted. Anything on the hardware descriptor ring
1822 * will be flushed by the csm_encaps command to shutdown the channel. */
1823 spin_lock_irqsave(&wc->cmd_list_lock, flags);
1824 list_for_each_entry_safe(cmd, temp, &wc->cmd_list, node) {
1825 if (cmd->cpvt == cpvt)
1826 list_move(&cmd->node, &local_list);
1827 }
1828 spin_unlock_irqrestore(&wc->cmd_list_lock, flags);
1829
1830 spin_lock_irqsave(&cpvt->lock, flags);
1831 list_splice_init(&cpvt->rx_queue, &local_list);
1832 dahdi_tc_clear_data_waiting(dtc);
1833 cpvt->samples_in_flight = 0;
1834 spin_unlock_irqrestore(&cpvt->lock, flags);
1835
1836 memset(&cpvt->stats, 0, sizeof(cpvt->stats));
1837 list_for_each_entry_safe(cmd, temp, &local_list, node) {
1838 list_del(&cmd->node);
1839 free_cmd(cmd);
1840 }
1841 }
1842
1843 static void
1844 wctc4xxp_mark_channel_complement_built(struct wcdte *wc,
1845 struct dahdi_transcoder_channel *dtc)
1846 {
1847 int index;
1848 struct channel_pvt *cpvt = dtc->pvt;
1849 struct dahdi_transcoder_channel *compl_dtc;
1850 struct channel_pvt *compl_cpvt;
1851
1852 BUG_ON(!cpvt);
1853 index = cpvt->timeslot_in_num/2;
1854 BUG_ON(index >= wc->numchannels);
1855 if (cpvt->encoder)
1856 compl_dtc = &(wc->udecode->channels[index]);
1857 else
1858 compl_dtc = &(wc->uencode->channels[index]);
1859
1860 /* It shouldn't already have been built... */
1861 WARN_ON(dahdi_tc_is_built(compl_dtc));
1862 compl_dtc->built_fmts = dtc->dstfmt | dtc->srcfmt;
1863 compl_cpvt = compl_dtc->pvt;
1864 DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP,
1865 "dtc: %p is the complement to %p\n", compl_dtc, dtc);
1866 compl_cpvt->chan_in_num = cpvt->chan_out_num;
1867 compl_cpvt->chan_out_num = cpvt->chan_in_num;
1868 dahdi_tc_set_built(compl_dtc);
1869 wctc4xxp_cleanup_channel_private(wc, dtc);
1870 }
1871
1872 static int
1873 do_channel_allocate(struct dahdi_transcoder_channel *dtc)
1874 {
1875 struct channel_pvt *cpvt = dtc->pvt;
1876 struct wcdte *wc = cpvt->wc;
1877 u8 wctc4xxp_srcfmt; /* Digium Transcoder Engine Source Format */
1878 u8 wctc4xxp_dstfmt; /* Digium Transcoder Engine Dest Format */
1879 int res;
1880
1881 /* Check again to see if the channel was built after grabbing the
1882 * channel lock, in case the previous holder of the lock
1883 * built this channel as a complement to itself. */
1884 if (dahdi_tc_is_built(dtc)) {
1885 DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP,
1886 "Allocating channel %p which is already built.\n", dtc);
1887 return 0;
1888 }
1889
1890 DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP,
1891 "Entering %s for channel %p.\n", __func__, dtc);
1892 /* Anything on the rx queue now is old news... */
1893 wctc4xxp_cleanup_channel_private(wc, dtc);
1894 DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP,
1895 "Allocating a new channel: %p.\n", dtc);
1896 wctc4xxp_srcfmt = wctc4xxp_dahdifmt_to_dtefmt(dtc->srcfmt);
1897 wctc4xxp_dstfmt = wctc4xxp_dahdifmt_to_dtefmt(dtc->dstfmt);
1898 res = wctc4xxp_create_channel_pair(wc, cpvt, wctc4xxp_srcfmt,
1899 wctc4xxp_dstfmt);
1900 if (res) {
1901 dev_err(&wc->pdev->dev, "Failed to create channel pair.\n");
1902 /* A failure to create a channel pair is normally a critical
1903 * error in the firmware state. Reload the firmware when this
1904 * handle is closed. */
1905 set_bit(DTE_RELOAD, &wc->flags);
1906 set_bit(DTE_SHUTDOWN, &wc->flags);
1907 return res;
1908 }
1909 /* Mark this channel as built */
1910 dahdi_tc_set_built(dtc);
1911 dtc->built_fmts = dtc->dstfmt | dtc->srcfmt;
1912 DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP,
1913 "Channel %p has dstfmt=%x and srcfmt=%x\n", dtc, dtc->dstfmt,
1914 dtc->srcfmt);
1915 /* Mark the channel complement (other half of encoder/decoder pair) as
1916 * built */
1917 wctc4xxp_mark_channel_complement_built(wc, dtc);
1918 dahdi_transcoder_alert(dtc);
1919 return 0;
1920 }
1921
1922 static void
1923 wctc4xxp_setintmask(struct wcdte *wc, unsigned int intmask)
1924 {
1925 wc->intmask = intmask;
1926 wctc4xxp_setctl(wc, 0x0038, intmask);
1927 }
1928
1929 static const u32 DEFAULT_INTERRUPTS = 0x0001a0c0;
1930
1931 static void
1932 wctc4xxp_enable_interrupts(struct wcdte *wc)
1933 {
1934 wctc4xxp_setintmask(wc, DEFAULT_INTERRUPTS);
1935 }
1936
1937 static void
1938 wctc4xxp_disable_interrupts(struct wcdte *wc)
1939 {
1940 /* Disable interrupts */
1941 wctc4xxp_setintmask(wc, 0x00000000);
1942 wctc4xxp_setctl(wc, 0x0084, 0x00000000);
1943 }
1944
1945 static void
1946 wctc4xxp_enable_polling(struct wcdte *wc)
1947 {
1948 set_bit(DTE_POLLING, &wc->flags);
1949 wctc4xxp_setctl(wc, 0x0058, 0x10003);
1950 /* Enable the general purpose timer interrupt. */
1951 wctc4xxp_setintmask(wc, (DEFAULT_INTERRUPTS | (1 << 11)) & ~0x41);
1952 }
1953
1954 static int wctc4xxp_reset_driver_state(struct wcdte *wc);
1955
1956 static bool wctc4xxp_need_firmware_reload(struct wcdte *wc)
1957 {
1958 return !!test_bit(DTE_RELOAD, &wc->flags) &&
1959 (1 == wc->open_channels);
1960 }
1961
1962 static int wctc4xxp_reload_firmware(struct wcdte *wc)
1963 {
1964 int res;
1965 clear_bit(DTE_SHUTDOWN, &wc->flags);
1966 res = wctc4xxp_reset_driver_state(wc);
1967 if (res)
1968 set_bit(DTE_SHUTDOWN, &wc->flags);
1969 else
1970 clear_bit(DTE_RELOAD, &wc->flags);
1971 return res;
1972 }
1973
1974 static int
1975 wctc4xxp_operation_allocate(struct dahdi_transcoder_channel *dtc)
1976 {
1977 int res = 0;
1978 struct wcdte *wc = ((struct channel_pvt *)(dtc->pvt))->wc;
1979
1980 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1981 mutex_lock(&wc->chanlock);
1982 #else
1983 res = mutex_lock_killable(&wc->chanlock);
1984 if (res)
1985 return res;
1986 #endif
1987
1988 ++wc->open_channels;
1989
1990 if (test_bit(DTE_SHUTDOWN, &wc->flags)) {
1991 res = -EIO;
1992 if (wctc4xxp_need_firmware_reload(wc))
1993 res = wctc4xxp_reload_firmware(wc);
1994 } else if (wctc4xxp_need_firmware_reload(wc)) {
1995 res = wctc4xxp_reload_firmware(wc);
1996 }
1997
1998 if (res)
1999 goto error_exit;
2000
2001 if (wc->open_channels > POLLING_CALL_THRESHOLD) {
2002 if (!test_bit(DTE_POLLING, &wc->flags))
2003 wctc4xxp_enable_polling(wc);
2004 }
2005
2006 if (dahdi_tc_is_built(dtc)) {
2007 DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP,
2008 "Allocating channel %p which is already built.\n", dtc);
2009 res = 0;
2010 } else {
2011 res = do_channel_allocate(dtc);
2012 }
2013
2014 error_exit:
2015 mutex_unlock(&wc->chanlock);
2016 return res;
2017 }
2018
2019 static void
2020 wctc4xxp_disable_polling(struct wcdte *wc)
2021 {
2022 clear_bit(DTE_POLLING, &wc->flags);
2023 wctc4xxp_setctl(wc, 0x0058, 0x0);
2024 wctc4xxp_enable_interrupts(wc);
2025 }
2026
2027 static void wctc4xxp_check_for_rx_errors(struct wcdte *wc)
2028 {
2029 /* get_packet_errors() returns the accumulated total errors */
2030 unsigned long errors = wctc4xxp_get_packet_errors(wc->rxd);
2031
2032 /* Print warning when the number of errors changes */
2033 if (wc->reported_packet_errors != errors) {
2034 if (printk_ratelimit()) {
2035 dev_err(&wc->pdev->dev,
2036 "%lu errored receive packets.\n",
2037 errors - wc->reported_packet_errors);
2038 wc->reported_packet_errors = errors;
2039 }
2040 }
2041 }
2042
2043 static int
2044 wctc4xxp_operation_release(struct dahdi_transcoder_channel *dtc)
2045 {
2046 int res = 0;
2047 int index;
2048 /* This is the 'complimentary channel' to dtc. I.e., if dtc is an
2049 * encoder, compl_dtc is the decoder and vice-versa */
2050 struct dahdi_transcoder_channel *compl_dtc;
2051 struct channel_pvt *compl_cpvt;
2052 struct channel_pvt *cpvt = dtc->pvt;
2053 struct wcdte *wc = cpvt->wc;
2054 int packets_received, packets_sent;
2055
2056 BUG_ON(!cpvt);
2057 BUG_ON(!wc);
2058
2059 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
2060 mutex_lock(&wc->chanlock);
2061 #else
2062 res = mutex_lock_killable(&wc->chanlock);
2063 if (res)
2064 return res;
2065 #endif
2066
2067 if (test_bit(DTE_SHUTDOWN, &wc->flags)) {
2068 /* On shutdown, if we reload the firmware we will reset the
2069 * state of all the channels. Therefore we do not want to
2070 * process any of the channel release logic even if the firmware
2071 * was reloaded successfully. */
2072 if (wctc4xxp_need_firmware_reload(wc))
2073 wctc4xxp_reload_firmware(wc);
2074 res = -EIO;
2075 } else if (wctc4xxp_need_firmware_reload(wc)) {
2076 wctc4xxp_reload_firmware(wc);
2077 res = -EIO;
2078 }
2079
2080 if (wc->open_channels) {
2081 --wc->open_channels;
2082
2083 #if !defined(CONFIG_WCTC4XXP_POLLING)
2084 if (wc->open_channels < POLLING_CALL_THRESHOLD) {
2085 if (test_bit(DTE_POLLING, &wc->flags))
2086 wctc4xxp_disable_polling(wc);
2087 }
2088 #endif
2089 }
2090
2091 if (res)
2092 goto error_exit;
2093
2094 packets_received = atomic_read(&cpvt->stats.packets_received);
2095 packets_sent = atomic_read(&cpvt->stats.packets_sent);
2096
2097 DTE_DEBUG(DTE_DEBUG_ETH_STATS,
2098 "%s channel %d sent %d packets and received %d packets.\n",
2099 (cpvt->encoder) ? "encoder" : "decoder", cpvt->chan_out_num,
2100 packets_sent, packets_received);
2101
2102
2103 /* Remove any packets that are waiting on the outbound queue. */
2104 dahdi_tc_clear_busy(dtc);
2105 wctc4xxp_cleanup_channel_private(wc, dtc);
2106 index = cpvt->timeslot_in_num/2;
2107 BUG_ON(index >= wc->numchannels);
2108 if (cpvt->encoder)
2109 compl_dtc = &(wc->udecode->channels[index]);
2110 else
2111 compl_dtc = &(wc->uencode->channels[index]);
2112 BUG_ON(!compl_dtc);
2113 if (!dahdi_tc_is_built(compl_dtc)) {
2114 DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP,
2115 "Releasing a channel that was never built.\n");
2116 res = 0;
2117 goto error_exit;
2118 }
2119 /* If the channel complement (other half of the encoder/decoder pair) is
2120 * being used. */
2121 if (dahdi_tc_is_busy(compl_dtc)) {
2122 res = 0;
2123 goto error_exit;
2124 }
2125 res = wctc4xxp_destroy_channel_pair(wc, cpvt);
2126 if (res)
2127 goto error_exit;
2128
2129 DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP, "Releasing channel: %p\n", dtc);
2130 /* Mark this channel as not built */
2131 dahdi_tc_clear_built(dtc);
2132 dtc->built_fmts = 0;
2133 cpvt->chan_in_num = INVALID;
2134 cpvt->chan_out_num = INVALID;
2135 /* Mark the channel complement as not built */
2136 dahdi_tc_clear_built(compl_dtc);
2137 compl_dtc->built_fmts = 0;
2138 compl_cpvt = compl_dtc->pvt;
2139 compl_cpvt->chan_in_num = INVALID;
2140 compl_cpvt->chan_out_num = INVALID;
2141
2142 wctc4xxp_check_for_rx_errors(wc);
2143
2144 error_exit:
2145 mutex_unlock(&wc->chanlock);
2146 return res;
2147 }
2148
2149 static inline struct tcb*
2150 get_ready_cmd(struct dahdi_transcoder_channel *dtc)
2151 {
2152 struct channel_pvt *cpvt = dtc->pvt;
2153 struct tcb *cmd;
2154 unsigned long flags;
2155 spin_lock_irqsave(&cpvt->lock, flags);
2156 if (!list_empty(&cpvt->rx_queue)) {
2157 WARN_ON(!dahdi_tc_is_data_waiting(dtc));
2158 cmd = list_entry(cpvt->rx_queue.next, struct tcb, node);
2159 list_del_init(&cmd->node);
2160 } else {
2161 cmd = NULL;
2162 }
2163 if (list_empty(&cpvt->rx_queue))
2164 dahdi_tc_clear_data_waiting(dtc);
2165 spin_unlock_irqrestore(&cpvt->lock, flags);
2166 return cmd;
2167 }
2168
2169 static int
2170 wctc4xxp_handle_receive_ring(struct wcdte *wc)
2171 {
2172 struct tcb *cmd;
2173 unsigned long flags;
2174 unsigned int count = 0;
2175
2176 /* If we can't grab this lock, another thread must already be checking
2177 * the receive ring...so we should just finish up, and we'll try again
2178 * later. */
2179 #if defined(spin_trylock_irqsave)
2180 if (!spin_trylock_irqsave(&wc->rx_lock, flags))
2181 return 0;
2182 #else
2183 if (spin_is_locked(&wc->rx_lock))
2184 return 0;
2185 spin_lock_irqsave(&wc->rx_lock, flags);
2186 #endif
2187
2188 while ((cmd = wctc4xxp_retrieve(wc->rxd))) {
2189 ++count;
2190 spin_lock(&wc->rx_list_lock);
2191 list_add_tail(&cmd->node, &wc->rx_list);
2192 spin_unlock(&wc->rx_list_lock);
2193 cmd = __alloc_cmd(SFRAME_SIZE, GFP_ATOMIC, 0);
2194 if (!cmd) {
2195 dev_err(&wc->pdev->dev,
2196 "Out of memory in %s.\n", __func__);
2197 } else {
2198 if (wctc4xxp_submit(wc->rxd, cmd)) {
2199 dev_err(&wc->pdev->dev, "Failed submit in %s\n",
2200 __func__);
2201 free_cmd(cmd);
2202 }
2203 }
2204 }
2205 spin_unlock_irqrestore(&wc->rx_lock, flags);
2206 return count;
2207 }
2208
2209 /* Called with a buffer in which to copy a transcoded frame. */
2210 static ssize_t
2211 wctc4xxp_read(struct file *file, char __user *frame, size_t count, loff_t *ppos)
2212 {
2213 ssize_t ret;
2214 struct dahdi_transcoder_channel *dtc = file->private_data;
2215 struct channel_pvt *cpvt = dtc->pvt;
2216 struct wcdte *wc = cpvt->wc;
2217 struct tcb *cmd;
2218 struct rtp_packet *packet;
2219 ssize_t payload_bytes;
2220 ssize_t returned_bytes = 0;
2221 unsigned long flags;
2222
2223 BUG_ON(!dtc);
2224 BUG_ON(!cpvt);
2225
2226 if (unlikely(test_bit(DTE_SHUTDOWN, &wc->flags))) {
2227 /* The shudown flags can also be set if there is a
2228 * catastrophic failure. */
2229 return -EIO;
2230 }
2231
2232 cmd = get_ready_cmd(dtc);
2233 if (!cmd) {
2234 if (file->f_flags & O_NONBLOCK)
2235 return -EAGAIN;
2236 ret = wait_event_interruptible(dtc->ready,
2237 dahdi_tc_is_data_waiting(dtc));
2238 if (-ERESTARTSYS == ret)
2239 return -EINTR;
2240 /* List went not empty. */
2241 cmd = get_ready_cmd(dtc);
2242 }
2243
2244 do {
2245 BUG_ON(!cmd);
2246 packet = cmd->data;
2247
2248 payload_bytes = be16_to_cpu(packet->udphdr.len) -
2249 sizeof(struct rtphdr) -
2250 sizeof(struct udphdr);
2251
2252 if (count < (payload_bytes + returned_bytes)) {
2253 if (returned_bytes) {
2254 /* If we have already returned at least one
2255 * packets worth of data, we'll add this next
2256 * packet to the head of the receive queue so
2257 * it will be picked up next time. */
2258 spin_lock_irqsave(&cpvt->lock, flags);
2259 list_add(&cmd->node, &cpvt->rx_queue);
2260 dahdi_tc_set_data_waiting(dtc);
2261 spin_unlock_irqrestore(&cpvt->lock, flags);
2262 return returned_bytes;
2263 }
2264
2265 if (printk_ratelimit()) {
2266 dev_err(&wc->pdev->dev,
2267 "Cannot copy %zd bytes into %zd byte user " \
2268 "buffer.\n", payload_bytes, count);
2269 }
2270 free_cmd(cmd);
2271 return -EFBIG;
2272 }
2273
2274 atomic_inc(&cpvt->stats.packets_received);
2275
2276 ret = copy_to_user(&frame[returned_bytes],
2277 &packet->payload[0], payload_bytes);
2278 if (unlikely(ret)) {
2279 dev_err(&wc->pdev->dev, "Failed to copy data in %s\n",
2280 __func__);
2281 free_cmd(cmd);
2282 return -EFAULT;
2283 }
2284
2285 returned_bytes += payload_bytes;
2286
2287 free_cmd(cmd);
2288
2289 } while ((cmd = get_ready_cmd(dtc)));
2290
2291 return returned_bytes;
2292 }
2293
2294 /* Called with a frame in the srcfmt to be transcoded into the dstfmt. */
2295 static ssize_t
2296 wctc4xxp_write(struct file *file, const char __user *frame,
2297 size_t count, loff_t *ppos)
2298 {
2299 struct dahdi_transcoder_channel *dtc = file->private_data;
2300 struct channel_pvt *cpvt = dtc->pvt;
2301 struct wcdte *wc = cpvt->wc;
2302 struct tcb *cmd;
2303 u32 samples;
2304 unsigned long flags;
2305 const unsigned long MAX_SAMPLES_IN_FLIGHT = 640;
2306 const unsigned long MAX_RTP_PAYLOAD = 500;
2307
2308 BUG_ON(!cpvt);
2309 BUG_ON(!wc);
2310
2311 if (unlikely(test_bit(DTE_SHUTDOWN, &wc->flags)))
2312 return -EIO;
2313
2314 if (!test_bit(DAHDI_TC_FLAG_CHAN_BUILT, &dtc->flags))
2315 return -EAGAIN;
2316
2317 if (count < 2) {
2318 DTE_DEBUG(DTE_DEBUG_GENERAL,
2319 "Cannot request to transcode a packet that is less than " \
2320 "2 bytes.\n");
2321 return -EINVAL;
2322 }
2323
2324 if (count > MAX_RTP_PAYLOAD) {
2325 DTE_DEBUG(DTE_DEBUG_GENERAL,
2326 "Cannot transcode packet of %Zu bytes. This exceeds the maximum size of %lu bytes.\n",
2327 count, MAX_RTP_PAYLOAD);
2328 return -EINVAL;
2329 }
2330
2331 if (DAHDI_FORMAT_G723_1 == dtc->srcfmt) {
2332 if ((G723_5K_BYTES != count) && (G723_6K_BYTES != count) &&
2333 (G723_SID_BYTES != count)) {
2334 DTE_DEBUG(DTE_DEBUG_GENERAL,
2335 "Trying to transcode packet into G723 format " \
2336 "that is %Zu bytes instead of the expected " \
2337 "%d/%d/%d bytes.\n", count, G723_5K_BYTES,
2338 G723_6K_BYTES, G723_SID_BYTES);
2339 return -EINVAL;
2340 }
2341 }
2342
2343 /* Do not flood the firmware with packets. This can result in out of
2344 * memory conditions in the firmware. */
2345 spin_lock_irqsave(&cpvt->lock, flags);
2346 if (time_after(jiffies, cpvt->send_time)) {
2347 cpvt->samples_in_flight = max(0L,
2348 cpvt->samples_in_flight - 160L);
2349 }
2350 samples = wctc4xxp_bytes_to_samples(dtc->srcfmt, count);
2351 if ((cpvt->samples_in_flight + samples) > MAX_SAMPLES_IN_FLIGHT) {
2352 spin_unlock_irqrestore(&cpvt->lock, flags);
2353 /* This should most likely be an error, but it results in
2354 * codec_dahdi spamming when it's not set to wait for new
2355 * packets. Instead we will silently drop the bytes. */
2356 return count;
2357 }
2358 cpvt->send_time = jiffies + msecs_to_jiffies(20);
2359 spin_unlock_irqrestore(&cpvt->lock, flags);
2360
2361 cmd = wctc4xxp_create_rtp_cmd(wc, dtc, count);
2362 if (!cmd)
2363 return -ENOMEM;
2364 /* Copy the data directly from user space into the command buffer. */
2365 if (copy_from_user(&((struct rtp_packet *)(cmd->data))->payload[0],
2366 frame, count)) {
2367 dev_err(&wc->pdev->dev,
2368 "Failed to copy packet from userspace.\n");
2369 free_cmd(cmd);
2370 return -EFAULT;
2371 }
2372 cpvt->seqno += 1;
2373
2374 DTE_DEBUG(DTE_DEBUG_RTP_TX,
2375 "Sending packet of %Zu byte on channel (%p).\n", count, dtc);
2376
2377 atomic_inc(&cpvt->stats.packets_sent);
2378 wctc4xxp_transmit_cmd(wc, cmd);
2379
2380 return count;
2381 }
2382
2383 static void
2384 wctc4xxp_send_ack(struct wcdte *wc, u8 seqno, __be16 channel, __le16 function)
2385 {
2386 struct tcb *cmd;
2387 struct csm_encaps_hdr *hdr;
2388 cmd = __alloc_cmd(sizeof(*hdr), ALLOC_FLAGS, 0);
2389 if (!cmd) {
2390 WARN_ON(1);
2391 return;
2392 }
2393 hdr = cmd->data;
2394 BUG_ON(sizeof(*hdr) > cmd->data_len);
2395 setup_common_header(wc, hdr);
2396 hdr->op_code = cpu_to_be16(0x0001);
2397 hdr->seq_num = seqno;
2398 hdr->control = 0xe0;
2399 hdr->channel = channel;
2400 hdr->cmd.function = function;
2401
2402 wctc4xxp_transmit_cmd(wc, cmd);
2403 }
2404
2405
2406 static void do_rx_response_packet(struct wcdte *wc, struct tcb *cmd)
2407 {
2408 struct csm_encaps_hdr *rxhdr;
2409 const struct csm_encaps_hdr *listhdr;
2410 struct tcb *pos, *temp;
2411 unsigned long flags;
2412 bool handled = false;
2413 rxhdr = cmd->data;
2414
2415 /* Check if duplicated response on the supervisor channel. */
2416 if (SUPERVISOR_CHANNEL == rxhdr->channel) {
2417 if (rxhdr->seq_num == wc->last_rx_seq_num) {
2418 free_cmd(cmd);
2419 return;
2420 }
2421 wc->last_rx_seq_num = rxhdr->seq_num;
2422 }
2423
2424 spin_lock_irqsave(&wc->cmd_list_lock, flags);
2425 list_for_each_entry_safe(pos, temp,
2426 &wc->waiting_for_response_list, node) {
2427 listhdr = pos->data;
2428 if ((listhdr->cmd.function == rxhdr->cmd.function) &&
2429 (listhdr->channel == rxhdr->channel)) {
2430
2431 /* If this is a channel command, do not complete it if
2432 * the seq_num is the same as previous. */
2433 if (pos->cpvt) {
2434 if (rxhdr->seq_num ==
2435 pos->cpvt->last_rx_seq_num) {
2436 break;
2437 }
2438 pos->cpvt->last_rx_seq_num = rxhdr->seq_num;
2439 }
2440
2441 list_del_init(&pos->node);
2442 pos->flags &= ~(WAIT_FOR_RESPONSE);
2443 pos->response = cmd;
2444 /* If this isn't TX_COMPLETE yet, then this packet will
2445 * be completed in service_tx_ring. */
2446 if (pos->flags & TX_COMPLETE && pos->complete)
2447 complete(pos->complete);
2448 handled = true;
2449
2450 break;
2451 }
2452 }
2453 spin_unlock_irqrestore(&wc->cmd_list_lock, flags);
2454
2455 if (!handled) {
2456 DTE_DEBUG(DTE_DEBUG_GENERAL,
2457 "Freeing unhandled response ch:(%04x)\n",
2458 be16_to_cpu(rxhdr->channel));
2459 free_cmd(cmd);
2460 }
2461 }
2462
2463 static void
2464 do_rx_ack_packet(struct wcdte *wc, struct tcb *cmd)
2465 {
2466 const struct csm_encaps_hdr *listhdr, *rxhdr;
2467 struct tcb *pos, *temp;
2468 unsigned long flags;
2469
2470 rxhdr = cmd->data;
2471
2472 spin_lock_irqsave(&wc->cmd_list_lock, flags);
2473 list_for_each_entry_safe(pos, temp,
2474 &wc->waiting_for_response_list, node) {
2475 listhdr = pos->data;
2476 if (cpu_to_be16(0xefed) == listhdr->ethhdr.h_proto) {
2477 wc->seq_num = (rxhdr->seq_num + 1) & 0xff;
2478 WARN_ON(!(pos->complete));
2479 WARN_ON(!(pos->flags & TX_COMPLETE));
2480 list_del_init(&pos->node);
2481 if (pos->complete)
2482 complete(pos->complete);
2483 } else if ((listhdr->seq_num == rxhdr->seq_num) &&
2484 (listhdr->channel == rxhdr->channel)) {
2485 if (pos->flags & WAIT_FOR_RESPONSE) {
2486 pos->flags &= ~(WAIT_FOR_ACK);
2487 } else {
2488 list_del_init(&pos->node);
2489
2490 if (pos->complete) {
2491 WARN_ON(!(pos->flags & TX_COMPLETE));
2492 complete(pos->complete);
2493 } else {
2494 free_cmd(pos);
2495 }
2496 }
2497 break;
2498 }
2499 }
2500 spin_unlock_irqrestore(&wc->cmd_list_lock, flags);
2501
2502 /* There is never a reason to store up the ack packets. */
2503 free_cmd(cmd);
2504 }
2505
2506 static inline int
2507 is_response(const struct csm_encaps_hdr *hdr)
2508 {
2509 return ((0x02 == hdr->cmd.type) ||
2510 (0x04 == hdr->cmd.type) ||
2511 (0x0e == hdr->cmd.type) ||
2512 (0x00 == hdr->cmd.type)) ? 1 : 0;
2513 }
2514
2515 static void
2516 print_command(struct wcdte *wc, const struct csm_encaps_hdr *hdr)
2517 {
2518 int i, curlength;
2519 char *buffer;
2520 const int BUFFER_SIZE = 1024;
2521 int parameters = ((hdr->cmd.length - 8)/sizeof(__le16));
2522
2523 buffer = kzalloc(BUFFER_SIZE + 1, GFP_ATOMIC);
2524 if (!buffer) {
2525 dev_info(&wc->pdev->dev, "Failed print_command\n");
2526 return;
2527 }
2528 curlength = snprintf(buffer, BUFFER_SIZE,
2529 "opcode: %04x seq: %02x control: %02x "
2530 "channel: %04x ", be16_to_cpu(hdr->op_code),
2531 hdr->seq_num, hdr->control, be16_to_cpu(hdr->channel));
2532 curlength += snprintf(buffer + curlength, BUFFER_SIZE - curlength,
2533 "length: %02x index: %02x type: %02x "
2534 "class: %02x function: %04x",
2535 hdr->cmd.length, hdr->cmd.index, hdr->cmd.type, hdr->cmd.class,
2536 le16_to_cpu(hdr->cmd.function));
2537 for (i = 0; i < parameters; ++i) {
2538 curlength += snprintf(buffer + curlength,
2539 BUFFER_SIZE - curlength, " %04x",
2540 le16_to_cpu(hdr->cmd.params[i]));
2541 }
2542 dev_info(&wc->pdev->dev, "%s\n", buffer);
2543 kfree(buffer);
2544 }
2545
2546 static inline void wctc4xxp_reset_processor(struct wcdte *wc)
2547 {
2548 wctc4xxp_setctl(wc, 0x00A0, 0x04000000);
2549 }
2550
2551
2552 static void handle_csm_alert(struct wcdte *wc,
2553 const struct csm_encaps_hdr *hdr)
2554 {
2555 const struct csm_encaps_cmd *c = &hdr->cmd;
2556 if (c->function == 0x0000) {
2557 u16 alert_type = le16_to_cpu(c->params[0]);
2558 u16 action_required = le16_to_cpu(c->params[1]) >> 8;
2559 const bool fatal_error = action_required != 0;
2560
2561 dev_err(&wc->pdev->dev,
2562 "Received alert (0x%04x) from dsp. Firmware will be reloaded when possible.\n",
2563 alert_type);
2564
2565 if (fatal_error) {
2566 /* If any fatal errors are reported we'll just shut
2567 * everything down so that we do not hang up any user
2568 * process trying to wait for commands to complete. */
2569 wctc4xxp_reset_processor(wc);
2570 set_bit(DTE_RELOAD, &wc->flags);
2571 set_bit(DTE_SHUTDOWN, &wc->flags);
2572 wctc4xxp_timeout_all_commands(wc);
2573 } else {
2574 /* For non-fatal errors we'll try to proceed and reload
2575 * the firmware when all open channels are closed. This
2576 * will prevent impacting any normal calls in progress.
2577 *
2578 */
2579 set_bit(DTE_RELOAD, &wc->flags);
2580 }
2581 } else {
2582 if (debug) {
2583 dev_warn(&wc->pdev->dev,
2584 "Received diagnostic message:\n");
2585 }
2586 }
2587
2588 if (debug) {
2589 print_command(wc, hdr);
2590 }
2591 }
2592
2593 static void
2594 receive_csm_encaps_packet(struct wcdte *wc, struct tcb *cmd)
2595 {
2596 const struct csm_encaps_hdr *hdr = cmd->data;
2597 const struct csm_encaps_cmd *c = &hdr->cmd;
2598
2599 if (!(hdr->control & MESSAGE_PACKET)) {
2600 const bool suppress_ack = ((hdr->control & SUPPRESS_ACK) > 0);
2601
2602 if (!suppress_ack) {
2603 wctc4xxp_send_ack(wc, hdr->seq_num, hdr->channel,
2604 c->function);
2605 }
2606
2607 if (is_response(hdr)) {
2608
2609 do_rx_response_packet(wc, cmd);
2610
2611 } else if (0xc1 == c->type) {
2612
2613 if (0x75 == c->class) {
2614 dev_warn(&wc->pdev->dev,
2615 "Received alert (0x%04x) from dsp\n",
2616 le16_to_cpu(c->params[0]));
2617 }
2618 free_cmd(cmd);
2619 } else if (0xd4 == c->type) {
2620 if (c->params[0] != le16_to_cpu(0xffff)) {
2621 dev_warn(&wc->pdev->dev,
2622 "DTE Failed self test (%04x).\n",
2623 le16_to_cpu(c->params[0]));
2624 } else if ((c->params[1] != le16_to_cpu(0x000c)) &&
2625 (c->params[1] != le16_to_cpu(0x010c))) {
2626 dev_warn(&wc->pdev->dev,
2627 "Unexpected ERAM status (%04x).\n",
2628 le16_to_cpu(c->params[1]));
2629 } else {
2630 wctc4xxp_set_ready(wc);
2631 wake_up(&wc->waitq);
2632 }
2633 free_cmd(cmd);
2634 } else if (MONITOR_LIVE_INDICATION_TYPE == c->type) {
2635 handle_csm_alert(wc, hdr);
2636 free_cmd(cmd);
2637 } else {
2638 dev_warn(&wc->pdev->dev,
2639 "Unknown command type received. %02x\n",
2640 c->type);
2641 free_cmd(cmd);
2642 }
2643 } else {
2644 do_rx_ack_packet(wc, cmd);
2645 }
2646 }
2647
2648 static void
2649 queue_rtp_packet(struct wcdte *wc, struct tcb *cmd)
2650 {
2651 unsigned index;
2652 struct dahdi_transcoder_channel *dtc;
2653 struct channel_pvt *cpvt;
2654 struct rtp_packet *packet = cmd->data;
2655 unsigned long flags;
2656 long samples;
2657
2658 if (unlikely(ip_fast_csum((void *)(&packet->iphdr),
2659 packet->iphdr.ihl))) {
2660 DTE_DEBUG(DTE_DEBUG_GENERAL,
2661 "Invalid checksum in RTP packet %04x\n",
2662 ip_fast_csum((void *)(&packet->iphdr),
2663 packet->iphdr.ihl));
2664 free_cmd(cmd);
2665 return;
2666 }
2667
2668 index = (be16_to_cpu(packet->udphdr.dest) - 0x5000) / 2;
2669 if (unlikely(!(index < wc->numchannels))) {
2670 dev_err(&wc->pdev->dev,
2671 "Invalid channel number in response from DTE.\n");
2672 free_cmd(cmd);
2673 return;
2674 }
2675
2676 switch (packet->rtphdr.type) {
2677 case 0x00:
2678 case 0x08:
2679 dtc = &(wc->udecode->channels[index]);
2680 break;
2681 case 0x04:
2682 case 0x12:
2683 dtc = &(wc->uencode->channels[index]);
2684 break;
2685 default:
2686 dev_err(&wc->pdev->dev, "Unknown codec in packet (0x%02x).\n",\
2687 packet->rtphdr.type);
2688 free_cmd(cmd);
2689 return;
2690 }
2691
2692 cpvt = dtc->pvt;
2693 if (!dahdi_tc_is_busy(dtc)) {
2694 free_cmd(cmd);
2695 return;
2696 }
2697
2698 spin_lock_irqsave(&cpvt->lock, flags);
2699 samples = wctc4xxp_bytes_to_samples(dtc->dstfmt,
2700 be16_to_cpu(packet->udphdr.len) -
2701 sizeof(struct rtphdr) - sizeof(struct udphdr));
2702 cpvt->samples_in_flight = max(cpvt->samples_in_flight - samples, 0L);
2703 list_add_tail(&cmd->node, &cpvt->rx_queue);
2704 dahdi_tc_set_data_waiting(dtc);
2705 spin_unlock_irqrestore(&cpvt->lock, flags);
2706 dahdi_transcoder_alert(dtc);
2707 return;
2708 }
2709
2710 static void service_tx_ring(struct wcdte *wc)
2711 {
2712 struct tcb *cmd;
2713 unsigned long flags;
2714 spin_lock_irqsave(&wc->cmd_list_lock, flags);
2715 while ((cmd = wctc4xxp_retrieve(wc->txd))) {
2716 cmd->flags |= TX_COMPLETE;
2717 if (!(cmd->flags & (WAIT_FOR_ACK | WAIT_FOR_RESPONSE))) {
2718 /* If we're not waiting for an ACK or Response from
2719 * the DTE, this message should not be sitting on any
2720 * lists. */
2721 WARN_ON(!list_empty(&cmd->node));
2722 if (cmd->complete) {
2723 WARN_ON(!(cmd->flags & TX_COMPLETE));
2724 complete(cmd->complete);
2725 } else {
2726 free_cmd(cmd);
2727 }
2728 }
2729
2730 /* We've freed up a spot in the hardware ring buffer. If
2731 * another packet is queued up, let's submit it to the
2732 * hardware. */
2733 if (!list_empty(&wc->cmd_list)) {
2734 cmd = list_entry(wc->cmd_list.next, struct tcb, node);
2735 list_del_init(&cmd->node);
2736 if (cmd->flags & (WAIT_FOR_ACK | WAIT_FOR_RESPONSE)) {
2737 list_add_tail(&cmd->node,
2738 &wc->waiting_for_response_list);
2739 }
2740 wctc4xxp_submit(wc->txd, cmd);
2741 }
2742 }
2743 spin_unlock_irqrestore(&wc->cmd_list_lock, flags);
2744 }
2745
2746 static void service_rx_ring(struct wcdte *wc)
2747 {
2748 struct tcb *cmd;
2749 unsigned long flags;
2750 LIST_HEAD(local_list);
2751 spin_lock_irqsave(&wc->rx_list_lock, flags);
2752 list_splice_init(&wc->rx_list, &local_list);
2753 spin_unlock_irqrestore(&wc->rx_list_lock, flags);
2754
2755 /*
2756 * Process the received packets
2757 */
2758 while (!list_empty(&local_list)) {
2759 const struct ethhdr *ethhdr;
2760
2761 cmd = container_of(local_list.next, struct tcb, node);
2762 ethhdr = (const struct ethhdr *)(cmd->data);
2763 list_del_init(&cmd->node);
2764
2765 wctc4xxp_net_capture_cmd(wc, cmd);
2766 if (cpu_to_be16(ETH_P_IP) == ethhdr->h_proto) {
2767 queue_rtp_packet(wc, cmd);
2768 } else if (cpu_to_be16(ETH_P_CSM_ENCAPS) == ethhdr->h_proto) {
2769 receive_csm_encaps_packet(wc, cmd);
2770 } else {
2771 DTE_DEBUG(DTE_DEBUG_GENERAL,
2772 "Unknown packet protocol received: %04x.\n",
2773 be16_to_cpu(ethhdr->h_proto));
2774 free_cmd(cmd);
2775 }
2776 }
2777 wctc4xxp_receive_demand_poll(wc);
2778 }
2779
2780 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
2781 static void deferred_work_func(void *param)
2782 {
2783 struct wcdte *wc = param;
2784 #else
2785 static void deferred_work_func(struct work_struct *work)
2786 {
2787 struct wcdte *wc = container_of(work, struct wcdte, deferred_work);
2788 #endif
2789 service_rx_ring(wc);
2790 }
2791
2792 DAHDI_IRQ_HANDLER(wctc4xxp_interrupt)
2793 {
2794 struct wcdte *wc = dev_id;
2795 bool packets_to_process = false;
2796 u32 ints;
2797 #define NORMAL_INTERRUPT_SUMMARY (1<<16)
2798 #define ABNORMAL_INTERRUPT_SUMMARY (1<<15)
2799
2800 #define TX_COMPLETE_INTERRUPT 0x00000001
2801 #define RX_COMPLETE_INTERRUPT 0x00000040
2802 #define TIMER_INTERRUPT (1<<11)
2803 #define NORMAL_INTERRUPTS (TX_COMPLETE_INTERRUPT | RX_COMPLETE_INTERRUPT | \
2804 TIMER_INTERRUPT)
2805
2806 /* Read and clear interrupts */
2807 ints = __wctc4xxp_getctl(wc, 0x0028);
2808
2809 if (!(ints & (NORMAL_INTERRUPT_SUMMARY|ABNORMAL_INTERRUPT_SUMMARY)))
2810 return IRQ_NONE;
2811
2812 /* Clear all the pending interrupts. */
2813 __wctc4xxp_setctl(wc, 0x0028, ints);
2814
2815 if (ints & (RX_COMPLETE_INTERRUPT | TIMER_INTERRUPT)) {
2816 packets_to_process = wctc4xxp_handle_receive_ring(wc) > 0;
2817 service_tx_ring(wc);
2818
2819 #if DEFERRED_PROCESSING == WORKQUEUE
2820 if (packets_to_process)
2821 schedule_work(&wc->deferred_work);
2822 #elif DEFERRED_PROCESSING == INTERRUPT
2823 #error "You will need to change the locks if you want to run the processing " \
2824 "in the interrupt handler."
2825 #else
2826 #error "Define a deferred processing function in kernel/wctc4xxp/wctc4xxp.h"
2827 #endif
2828
2829 } else {
2830 if ((ints & 0x00008000) && debug)
2831 dev_info(&wc->pdev->dev, "Abnormal Interrupt.\n");
2832
2833 if (ints & 0x00002000)
2834 dev_err(&wc->pdev->dev, "Fatal Bus Error Detected.\n");
2835
2836 if ((ints & 0x00000100) && debug)
2837 dev_info(&wc->pdev->dev, "Receive Stopped INT\n");
2838
2839 if ((ints & 0x00000080) && debug) {
2840 dev_info(&wc->pdev->dev,
2841 "Receive Desciptor Unavailable INT " \
2842 "(%d)\n", wctc4xxp_getcount(wc->rxd));
2843 }
2844
2845 if ((ints & 0x00000020) && debug)
2846 dev_info(&wc->pdev->dev, "Transmit Under-flow INT\n");
2847
2848 if ((ints & 0x00000008) && debug)
2849 dev_info(&wc->pdev->dev, "Jabber Timer Time-out INT\n");
2850
2851 if ((ints & 0x00000002) && debug) {
2852 dev_info(&wc->pdev->dev,
2853 "Transmit Processor Stopped INT\n");
2854 }
2855 }
2856 return IRQ_HANDLED;
2857 }
2858
2859 static int
2860 wctc4xxp_hardware_init(struct wcdte *wc)
2861 {
2862 /* Hardware stuff */
2863 enum {
2864 /* Software Reset */
2865 SWR = (1 << 0),
2866 /* Bus Arbitration (1 for priority transmit) */
2867 BAR = (1 << 1),
2868 /* Memory Write Invalidate */
2869 MWI = (1 << 24),
2870 /* Memory Read Line */
2871 MRL = (1 << 23),
2872 /* Descriptor Skip Length */
2873 DSLShift = 2,
2874 /* Cache Alignment */
2875 CALShift = 14,
2876 /* Transmit Auto Pollling */
2877 TAPShift = 17,
2878 };
2879 u32 reg;
2880 unsigned long newjiffies;
2881 u8 cache_line_size;
2882 const u32 DEFAULT_PCI_ACCESS = (MWI | (11 << TAPShift));
2883
2884 if (pci_read_config_byte(wc->pdev, 0x0c, &cache_line_size))
2885 return -EIO;
2886
2887 switch (cache_line_size) {
2888 case 0x08:
2889 reg = DEFAULT_PCI_ACCESS | (0x1 << CALShift);
2890 break;
2891 case 0x10:
2892 reg = DEFAULT_PCI_ACCESS | (0x2 << CALShift);
2893 break;
2894 case 0x20:
2895 reg = DEFAULT_PCI_ACCESS | (0x3 << CALShift);
2896 break;
2897 default:
2898 reg = (11 << TAPShift);
2899 break;
2900 }
2901
2902 reg |= ((wc->txd->padding / sizeof(u32)) << 2) & 0x7c;
2903
2904 /* Reset the DTE... */
2905 wctc4xxp_setctl(wc, 0x0000, reg | 1);
2906 newjiffies = jiffies + HZ; /* One second timeout */
2907 /* ...and wait for it to come out of reset. */
2908 while (((wctc4xxp_getctl(wc, 0x0000)) & 0x00000001) &&
2909 (newjiffies > jiffies))
2910 msleep(1);
2911
2912 wctc4xxp_setctl(wc, 0x0000, reg | 0x60000);
2913
2914 /* Configure watchdogs, access, etc */
2915 wctc4xxp_setctl(wc, 0x0030, 0x00280048);
2916 wctc4xxp_setctl(wc, 0x0078, 0x00000013);
2917 reg = wctc4xxp_getctl(wc, 0x00fc);
2918 wctc4xxp_setctl(wc, 0x00fc, (reg & ~0x7) | 0x7);
2919 reg = wctc4xxp_getctl(wc, 0x00fc);
2920 return 0;
2921 }
2922
2923 static void
2924 wctc4xxp_start_dma(struct wcdte *wc)
2925 {
2926 int res;
2927 int i;
2928 u32 reg;
2929 struct tcb *cmd;
2930
2931 for (i = 0; i < wc->rxd->size; ++i) {
2932 cmd = alloc_cmd(SFRAME_SIZE);
2933 if (!cmd) {
2934 WARN_ALWAYS();
2935 return;
2936 }
2937 WARN_ON(SFRAME_SIZE != cmd->data_len);
2938 res = wctc4xxp_submit(wc->rxd, cmd);
2939 if (res) {
2940 /* When we're starting the DMA, we should always be
2941 * able to fill the ring....so something is wrong
2942 * here. */
2943 WARN_ALWAYS();
2944 free_cmd(cmd);
2945 break;
2946 }
2947 }
2948 wmb();
2949 wctc4xxp_setctl(wc, 0x0020, wc->txd->desc_dma);
2950 wctc4xxp_setctl(wc, 0x0018, wc->rxd->desc_dma);
2951
2952 /* Start receiver/transmitter */
2953 reg = wctc4xxp_getctl(wc, 0x0030);
2954 wctc4xxp_setctl(wc, 0x0030, reg | 0x00002002);
2955 wctc4xxp_receive_demand_poll(wc);
2956 reg = wctc4xxp_getctl(wc, 0x0028);
2957 wctc4xxp_setctl(wc, 0x0028, reg);
2958
2959 }
2960
2961 static void
2962 _wctc4xxp_stop_dma(struct wcdte *wc)
2963 {
2964 /* Disable interrupts and reset */
2965 unsigned int reg;
2966 /* Disable interrupts */
2967 wctc4xxp_setintmask(wc, 0x00000000);
2968 wctc4xxp_setctl(wc, 0x0084, 0x00000000);
2969 wctc4xxp_setctl(wc, 0x0048, 0x00000000);
2970 /* Reset the part to be on the safe side */
2971 reg = wctc4xxp_getctl(wc, 0x0000);
2972 reg |= 0x00000001;
2973 wctc4xxp_setctl(wc, 0x0000, reg);
2974 }
2975
2976 static void
2977 wctc4xxp_stop_dma(struct wcdte *wc)
2978 {
2979 unsigned long newjiffies;
2980
2981 _wctc4xxp_stop_dma(wc);
2982 newjiffies = jiffies + HZ; /* One second timeout */
2983 /* We'll wait here for the part to come out of reset */
2984 while (((wctc4xxp_getctl(wc, 0x0000)) & 0x00000001) &&
2985 (newjiffies > jiffies))
2986 msleep(1);
2987 }
2988
2989 #define MDIO_SHIFT_CLK 0x10000
2990 #define MDIO_DATA_WRITE1 0x20000
2991 #define MDIO_ENB 0x00000
2992 #define MDIO_ENB_IN 0x40000
2993 #define MDIO_DATA_READ 0x80000
2994
2995 static int
2996 wctc4xxp_read_phy(struct wcdte *wc, int location)
2997 {
2998 int i;
2999 long mdio_addr = 0x0048;
3000 int read_cmd = (0xf6 << 10) | (1 << 5) | location;
3001 int retval = 0;
3002
3003 /* Establish sync by sending at least 32 logic ones. */
3004 for (i = 32; i >= 0; --i) {
3005 wctc4xxp_setctl(wc, mdio_addr,
3006 MDIO_ENB | MDIO_DATA_WRITE1);
3007 wctc4xxp_getctl(wc, mdio_addr);
3008 wctc4xxp_setctl(wc, mdio_addr,
3009 MDIO_ENB | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK);
3010 wctc4xxp_getctl(wc, mdio_addr);
3011 }
3012
3013 /* Shift the read command bits out. */
3014 for (i = 17; i >= 0; --i) {
3015 int dataval = (read_cmd & (1 << i)) ? MDIO_DATA_WRITE1 : 0;
3016
3017 wctc4xxp_setctl(wc, mdio_addr, MDIO_ENB | dataval);
3018 wctc4xxp_getctl(wc, mdio_addr);
3019 wctc4xxp_setctl(wc, mdio_addr,
3020 MDIO_ENB | dataval | MDIO_SHIFT_CLK);
3021 wctc4xxp_getctl(wc, mdio_addr);
3022 }
3023
3024 /* Read the two transition, 16 data, and wire-idle bits. */
3025 for (i = 19; i > 0; --i) {
3026 wctc4xxp_setctl(wc, mdio_addr, MDIO_ENB_IN);
3027 wctc4xxp_getctl(wc, mdio_addr);
3028 retval = (retval << 1) |
3029 ((wctc4xxp_getctl(wc, mdio_addr) & MDIO_DATA_READ) ?
3030 1 : 0);
3031 wctc4xxp_setctl(wc, mdio_addr, MDIO_ENB_IN | MDIO_SHIFT_CLK);
3032 wctc4xxp_getctl(wc, mdio_addr);
3033 }
3034 retval = (retval>>1) & 0xffff;
3035 return retval;
3036 }
3037
3038 static void
3039 wctc4xxp_write_phy(struct wcdte *wc, int location, int value)
3040 {
3041 int i;
3042 int cmd = (0x5002 << 16) | (1 << 23) | (location<<18) | value;
3043 long mdio_addr = 0x0048;
3044
3045 /* Establish sync by sending 32 logic ones. */
3046 for (i = 32; i >= 0; --i) {
3047 wctc4xxp_setctl(wc, mdio_addr, MDIO_ENB | MDIO_DATA_WRITE1);
3048 wctc4xxp_getctl(wc, mdio_addr);
3049 wctc4xxp_setctl(wc, mdio_addr,
3050 MDIO_ENB | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK);
3051 wctc4xxp_getctl(wc, mdio_addr);
3052 }
3053 /* Shift the command bits out. */
3054 for (i = 31; i >= 0; --i) {
3055 int dataval = (cmd & (1 << i)) ? MDIO_DATA_WRITE1 : 0;
3056 wctc4xxp_setctl(wc, mdio_addr, MDIO_ENB | dataval);
3057 wctc4xxp_getctl(wc, mdio_addr);
3058 wctc4xxp_setctl(wc, mdio_addr,
3059 MDIO_ENB | dataval | MDIO_SHIFT_CLK);
3060 wctc4xxp_getctl(wc, mdio_addr);
3061 }
3062 /* Clear out extra bits. */
3063 for (i = 2; i > 0; --i) {
3064 wctc4xxp_setctl(wc, mdio_addr, MDIO_ENB_IN);
3065 wctc4xxp_getctl(wc, mdio_addr);
3066 wctc4xxp_setctl(wc, mdio_addr, MDIO_ENB_IN | MDIO_SHIFT_CLK);
3067 wctc4xxp_getctl(wc, mdio_addr);
3068 }
3069 return;
3070 }
3071
3072 static int
3073 wctc4xxp_wait_for_link(struct wcdte *wc)
3074 {
3075 int reg;
3076 unsigned int delay_count = 0;
3077 do {
3078 reg = wctc4xxp_getctl(wc, 0x00fc);
3079 msleep(2);
3080 delay_count++;
3081
3082 if (delay_count >= 5000) {
3083 dev_err(&wc->pdev->dev,
3084 "Failed to link to DTE processor!\n");
3085 return -EIO;
3086 }
3087 } while ((reg & 0xE0000000) != 0xE0000000);
3088 return 0;
3089 }
3090
3091 static int
3092 wctc4xxp_load_firmware(struct wcdte *wc, const struct firmware *firmware)
3093 {
3094 unsigned int byteloc;
3095 unsigned int length;
3096 struct tcb *cmd;
3097 DECLARE_COMPLETION_ONSTACK(done);
3098
3099 byteloc = 17;
3100
3101 cmd = alloc_cmd(SFRAME_SIZE);
3102 if (!cmd)
3103 return -ENOMEM;
3104
3105 #if defined(CONFIG_WCTC4XXP_POLLING)
3106 wctc4xxp_enable_polling(wc);
3107 #endif
3108
3109 clear_bit(DTE_READY, &wc->flags);
3110
3111 while (byteloc < (firmware->size-20)) {
3112 length = (firmware->data[byteloc] << 8) |
3113 firmware->data[byteloc+1];
3114 byteloc += 2;
3115 cmd->data_len = length;
3116 BUG_ON(length > cmd->data_len);
3117 memcpy(cmd->data, &firmware->data[byteloc], length);
3118 byteloc += length;
3119 cmd->flags = WAIT_FOR_ACK;
3120 cmd->complete = &done;
3121 wctc4xxp_transmit_cmd(wc, cmd);
3122 wait_for_completion(&done);
3123 if (cmd->flags & DTE_CMD_TIMEOUT) {
3124 free_cmd(cmd);
3125 dev_err(&wc->pdev->dev, "Failed to load firmware.\n");
3126 #if defined(CONFIG_WCTC4XXP_POLLING)
3127 wctc4xxp_disable_polling(wc);
3128 #endif
3129 return -EIO;
3130 }
3131 }
3132 free_cmd(cmd);
3133 if (!wait_event_timeout(wc->waitq, wctc4xxp_is_ready(wc), 15*HZ)) {
3134 dev_err(&wc->pdev->dev, "Failed to boot firmware.\n");
3135 #if defined(CONFIG_WCTC4XXP_POLLING)
3136 wctc4xxp_disable_polling(wc);
3137 #endif
3138 return -EIO;
3139 }
3140
3141 #if defined(CONFIG_WCTC4XXP_POLLING)
3142 wctc4xxp_disable_polling(wc);
3143 #endif
3144 return 0;
3145 }
3146
3147 static int
3148 wctc4xxp_turn_off_booted_led(struct wcdte *wc)
3149 {
3150 int ret = 0;
3151 int reg;
3152 /* Turn off auto negotiation */
3153 wctc4xxp_write_phy(wc, 0, 0x2100);
3154 DTE_DEBUG(DTE_DEBUG_GENERAL, "PHY register 0 = %X\n",
3155 wctc4xxp_read_phy(wc, 0));
3156
3157 wctc4xxp_reset_processor(wc);
3158
3159 /* Wait 4 ms to ensure processor reset */
3160 msleep(4);
3161
3162 /* Clear reset */
3163 wctc4xxp_setctl(wc, 0x00A0, 0x04080000);
3164
3165 /* Wait for the ethernet link */
3166 ret = wctc4xxp_wait_for_link(wc);
3167 if (ret)
3168 return ret;
3169
3170 /* Turn off booted LED */
3171 wctc4xxp_setctl(wc, 0x00A0, 0x04084000);
3172 reg = wctc4xxp_getctl(wc, 0x00fc);
3173 DTE_DEBUG(DTE_DEBUG_GENERAL, "LINK STATUS: reg(0xfc) = %X\n", reg);
3174
3175 reg = wctc4xxp_getctl(wc, 0x00A0);
3176
3177 return ret;
3178 }
3179
3180 static void
3181 wctc4xxp_turn_on_booted_led(struct wcdte *wc)
3182 {
3183 wctc4xxp_setctl(wc, 0x00A0, 0x04080000);
3184 }
3185
3186 static int
3187 wctc4xxp_boot_processor(struct wcdte *wc, const struct firmware *firmware)
3188 {
3189 int ret;
3190
3191 wctc4xxp_turn_off_booted_led(wc);
3192 ret = wctc4xxp_load_firmware(wc, firmware);
3193 if (ret)
3194 return ret;
3195
3196 wctc4xxp_turn_on_booted_led(wc);
3197
3198 DTE_DEBUG(DTE_DEBUG_GENERAL, "Successfully booted DTE processor.\n");
3199 return 0;
3200 }
3201
3202 static void
3203 setup_half_channel(struct channel_pvt *pvt, struct tcb *cmd, u16 length)
3204 {
3205 setup_channel_header(pvt, cmd);
3206
3207 append_set_ip_hdr_channel_cmd(cmd);
3208 append_voip_vceopt_cmd(cmd, length);
3209 append_voip_tonectl_cmd(cmd);
3210 append_voip_dtmfopt_cmd(cmd);
3211 append_voip_indctrl_cmd(cmd);
3212
3213 /* To indicate the end of multiple messages. */
3214 cmd->data_len += 4;
3215 WARN_ON(cmd->data_len >= SFRAME_SIZE);
3216
3217 wctc4xxp_transmit_cmd(pvt->wc, cmd);
3218 }
3219
3220 static int wctc4xxp_setup_channels(struct wcdte *wc,
3221 struct channel_pvt *encoder_pvt,
3222 struct channel_pvt *decoder_pvt,
3223 u16 length)
3224 {
3225 int res = 0;
3226 struct tcb *encoder_cmd;
3227 struct tcb *decoder_cmd;
3228 DECLARE_COMPLETION_ONSTACK(encoder_done);
3229 DECLARE_COMPLETION_ONSTACK(decoder_done);
3230
3231 encoder_cmd = alloc_cmd(SFRAME_SIZE);
3232 decoder_cmd = alloc_cmd(SFRAME_SIZE);
3233
3234 if (!encoder_cmd || !decoder_cmd) {
3235 res = -ENOMEM;
3236 goto error_exit;
3237 }
3238
3239 encoder_cmd->complete = &encoder_done;
3240 decoder_cmd->complete = &decoder_done;
3241
3242 setup_half_channel(encoder_pvt, encoder_cmd, length);
3243 setup_half_channel(decoder_pvt, decoder_cmd, length);
3244
3245 wait_for_completion(&decoder_done);
3246 wait_for_completion(&encoder_done);
3247
3248 if (encoder_cmd->flags & DTE_CMD_TIMEOUT ||
3249 decoder_cmd->flags & DTE_CMD_TIMEOUT) {
3250 DTE_DEBUG(DTE_DEBUG_GENERAL, "Timeout waiting for command.\n");
3251 res = -EIO;
3252 }
3253
3254 if ((0x0000 != response_header(encoder_cmd)->cmd.params[0]) ||
3255 (0x0000 != response_header(encoder_cmd)->cmd.params[0]))
3256 res = -EIO;
3257
3258 error_exit:
3259 free_cmd(encoder_cmd);
3260 free_cmd(decoder_cmd);
3261 return res;
3262 }
3263
3264 static int wctc4xxp_enable_channels(struct wcdte *wc,
3265 struct channel_pvt *encoder_pvt,
3266 struct channel_pvt *decoder_pvt,
3267 u8 complicated, u8 simple)
3268 {
3269 int res = 0;
3270 struct tcb *encoder_cmd;
3271 struct tcb *decoder_cmd;
3272 DECLARE_COMPLETION_ONSTACK(encoder_done);
3273 DECLARE_COMPLETION_ONSTACK(decoder_done);
3274
3275 encoder_cmd = alloc_cmd(SFRAME_SIZE);
3276 decoder_cmd = alloc_cmd(SFRAME_SIZE);
3277
3278 if (!encoder_cmd || !decoder_cmd) {
3279 res = -ENOMEM;
3280 goto error_exit;
3281 }
3282
3283 encoder_cmd->complete = &encoder_done;
3284 decoder_cmd->complete = &decoder_done;
3285
3286 send_voip_vopena_cmd(encoder_pvt, encoder_cmd, complicated);
3287 send_voip_vopena_cmd(decoder_pvt, decoder_cmd, simple);
3288
3289 wait_for_completion(&decoder_done);
3290 wait_for_completion(&encoder_done);
3291
3292 if ((0x0000 != response_header(encoder_cmd)->cmd.params[0]) ||
3293 (0x0000 != response_header(decoder_cmd)->cmd.params[0]))
3294 res = -EIO;
3295
3296 error_exit:
3297 free_cmd(encoder_cmd);
3298 free_cmd(decoder_cmd);
3299 return res;
3300 }
3301
3302 static int
3303 wctc4xxp_create_channel_pair(struct wcdte *wc, struct channel_pvt *cpvt,
3304 u8 simple, u8 complicated)
3305 {
3306 struct channel_pvt *encoder_pvt, *decoder_pvt;
3307 u16 encoder_timeslot, decoder_timeslot;
3308 u16 encoder_channel, decoder_channel;
3309 struct tcb *cmd;
3310 u16 length;
3311
3312 cmd = alloc_cmd(SFRAME_SIZE);
3313 if (!cmd)
3314 return -ENOMEM;
3315
3316 BUG_ON(!wc || !cpvt);
3317 if (cpvt->encoder) {
3318 encoder_timeslot = cpvt->timeslot_in_num;
3319 decoder_timeslot = cpvt->timeslot_out_num;
3320 } else {
3321 u8 temp;
3322 encoder_timeslot = cpvt->timeslot_out_num;
3323 decoder_timeslot = cpvt->timeslot_in_num;
3324 temp = simple;
3325 simple = complicated;
3326 complicated = temp;
3327 }
3328
3329 length = (DTE_FORMAT_G729A == complicated) ? G729_LENGTH :
3330 (DTE_FORMAT_G723_1 == complicated) ? G723_LENGTH : 0;
3331
3332
3333 BUG_ON(encoder_timeslot/2 >= wc->numchannels);
3334 BUG_ON(decoder_timeslot/2 >= wc->numchannels);
3335 encoder_pvt = wc->uencode->channels[encoder_timeslot/2].pvt;
3336 decoder_pvt = wc->udecode->channels[decoder_timeslot/2].pvt;
3337 BUG_ON(!encoder_pvt);
3338 BUG_ON(!decoder_pvt);
3339 encoder_pvt->last_rx_seq_num = 0xff;
3340 decoder_pvt->last_rx_seq_num = 0xff;
3341
3342 WARN_ON(encoder_timeslot == decoder_timeslot);
3343 /* First, let's create two channels, one for the simple -> complex
3344 * encoder and another for the complex->simple decoder. */
3345 if (send_create_channel_cmd(wc, cmd, encoder_timeslot,
3346 &encoder_channel))
3347 goto error_exit;
3348
3349 if (send_create_channel_cmd(wc, cmd, decoder_timeslot,
3350 &decoder_channel))
3351 goto error_exit;
3352
3353 DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP,
3354 "DTE is using the following channels encoder_channel: " \
3355 "%d decoder_channel: %d\n", encoder_channel, decoder_channel);
3356
3357 WARN_ON(encoder_channel == decoder_channel);
3358 /* Now set all the default parameters for the encoder. */
3359 encoder_pvt->chan_in_num = encoder_channel;
3360 encoder_pvt->chan_out_num = decoder_channel;
3361
3362 decoder_pvt->chan_in_num = decoder_channel;
3363 decoder_pvt->chan_out_num = encoder_channel;
3364
3365 if (wctc4xxp_setup_channels(wc, encoder_pvt, decoder_pvt, length))
3366 goto error_exit;
3367
3368 if (send_trans_connect_cmd(wc, cmd, encoder_channel,
3369 decoder_channel, complicated, simple))
3370 goto error_exit;
3371
3372 if (wctc4xxp_enable_channels(wc, encoder_pvt, decoder_pvt,
3373 complicated, simple))
3374 goto error_exit;
3375
3376 DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP,
3377 "DTE has completed setup and connected the " \
3378 "two channels together.\n");
3379
3380 free_cmd(cmd);
3381 return 0;
3382 error_exit:
3383 free_cmd(cmd);
3384 return -EIO;
3385 }
3386
3387 static void print_vceinfo_packet(struct wcdte *wc, struct tcb *cmd)
3388 {
3389 int i;
3390 struct device *const dev = &wc->pdev->dev;
3391
3392 static const struct {
3393 const char *name;
3394 bool show;
3395 } PARAMETERS[] = {
3396 { "Format Revision ", false},
3397 { "Reserved ", false},
3398 { "Call Timer (seconds) ", false},
3399 { "Current Playout Delay [to PCM] ", false},
3400 { "Minimum Playout Delay [to PCM] ", false},
3401 { "Maximum Playout Delay [to PCM] ", false},
3402 { "Clock Offset ", false},
3403 { "PeakJitter (ms) ", true},
3404 { "Interpolative Concealment [to PCM] ", false},
3405 { "Silence Concealment [to PCM] ", false},
3406 { "Jitter Buffer Overflow Discard [from IP] ", true},
3407 { "End-point Detection Errors ", true},
3408 { "Number of Tx Voice Packets [to IP] ", true},
3409 { "Number of Tx Signalling Packets [to IP] ", true},
3410 { "Number of Tx Comfort Noise Packets [to IP] ", true},
3411 { "Total Transmit Duration [to IP] ", true},
3412 { "Voice Transmit Duration [to IP] ", true},
3413 { "Number of Rx Voice Packets [from IP] ", true},
3414 { "Number of Rx Signalling Packets [from IP] ", true},
3415 { "Number of Rx Comfort Noise Packets [from IP] ", true},
3416 { "Total Receive Duration [from IP] ", true},
3417 { "Voice Receive Duration [from IP] ", true},
3418 { "Packets Out of Sequence [from IP] ", true},
3419 { "Bad Protocol Headers [from IP] ", true},
3420 { "Late Packets [from IP] ", true},
3421 { "Reserved (Early Packets) always zero ", false},
3422 { "Number of Rx Voice bytes ", true},
3423 { "Number of Lost Packets [from IP] ", true},
3424 { "Current Transmit Power [from PCM] ", false},
3425 { "Mean Transmit Power [from PCM] ", false},
3426 { "Current Receive Power [to PCM] ", false},
3427 { "Mean Receive Power [to PCM] ", false},
3428 { "Background Noise [to PCM] ", false},
3429 { "ERL Level [to PCM] ", false},
3430 { "ACOM Level [from PCM] ", false},
3431 { "Current Transmit Activity [from PCM] ", false},
3432 { "Current Receive Activity [to PCM] ", false},
3433 { "Discarded Unexpected Packets ", true},
3434 { "Discard Packets Due to Rx Disabled ", true},
3435 { "Discarded Duplicate Packets ", true},
3436 { "Discarded Packets Due to Incorrect Payload Length ", true},
3437 { "Discarded Packets Due to Channel Inactive ", true},
3438 { "Discarded Packets Due to Insufficient Memory ", true}
3439 };
3440
3441 u32 *parms = (u32 *)(&response_header(cmd)->cmd.params[0]);
3442 for (i = 0; i < 43; ++i) {
3443 if (PARAMETERS[i].show)
3444 dev_info(dev, "%s%d\n", PARAMETERS[i].name, parms[i]);
3445 }
3446 }
3447
3448 static void print_eth_statistics_packet(struct wcdte *wc, struct tcb *cmd)
3449 {
3450 int i;
3451 struct device *const dev = &wc->pdev->dev;
3452
3453 static const struct {
3454 const char *name;
3455 bool show;
3456 } PARAMETERS[] = {
3457 { "Format Revision ", true},
3458 { "Emitted Frames ", true},
3459 { "Received Frames ", true},
3460 { "Unknown Packet Type ", true},
3461 { "Received Broadcast Packets ", true},
3462 { "Unknown Broadcast ", true},
3463 { "Emitted VLAN frames ", true},
3464 { "Received VLAN frames ", true},
3465 { "Received VLAN frames with E-RIF ", true}
3466 };
3467
3468 u32 *parms = (u32 *)(&response_header(cmd)->cmd.params[0]);
3469 for (i = 0; i < sizeof(PARAMETERS)/sizeof(PARAMETERS[0]); ++i) {
3470 if (PARAMETERS[i].show)
3471 dev_info(dev, "%s%d\n", PARAMETERS[i].name, parms[i]);
3472 }
3473 }
3474
3475 static int
3476 wctc4xxp_destroy_channel_pair(struct wcdte *wc, struct channel_pvt *cpvt)
3477 {
3478 struct dahdi_transcoder_channel *dtc1, *dtc2;
3479 struct channel_pvt *encoder_pvt, *decoder_pvt;
3480 int chan1, chan2, timeslot1, timeslot2;
3481 struct tcb *cmd;
3482
3483 cmd = alloc_cmd(SFRAME_SIZE);
3484 if (!cmd)
3485 return -ENOMEM;
3486
3487 if (cpvt->encoder) {
3488 chan1 = cpvt->chan_in_num;
3489 timeslot1 = cpvt->timeslot_in_num;
3490 chan2 = cpvt->chan_out_num;
3491 timeslot2 = cpvt->timeslot_out_num;
3492 } else {
3493 chan1 = cpvt->chan_out_num;
3494 timeslot1 = cpvt->timeslot_out_num;
3495 chan2 = cpvt->chan_in_num;
3496 timeslot2 = cpvt->timeslot_in_num;
3497 }
3498
3499 if (timeslot1/2 >= wc->numchannels || timeslot2/2 >= wc->numchannels) {
3500 dev_warn(&wc->pdev->dev,
3501 "Invalid channel numbers in %s. chan1:%d chan2: %d\n",
3502 __func__, timeslot1/2, timeslot2/2);
3503 return 0;
3504 }
3505
3506 dtc1 = &(wc->uencode->channels[timeslot1/2]);
3507 dtc2 = &(wc->udecode->channels[timeslot2/2]);
3508 encoder_pvt = dtc1->pvt;
3509 decoder_pvt = dtc2->pvt;
3510
3511 if (debug & DTE_DEBUG_ETH_STATS) {
3512 if (send_voip_vceinfo_cmd(encoder_pvt, cmd))
3513 goto error_exit;
3514 dev_warn(&wc->pdev->dev,
3515 "****************************************\n");
3516 dev_warn(&wc->pdev->dev,
3517 "Encoder stats (ch: %d):\n",
3518 encoder_pvt->timeslot_in_num);
3519 print_vceinfo_packet(wc, cmd);
3520
3521 if (send_voip_vceinfo_cmd(decoder_pvt, cmd))
3522 goto error_exit;
3523 dev_warn(&wc->pdev->dev,
3524 "****************************************\n");
3525 dev_warn(&wc->pdev->dev,
3526 "Decoder stats (ch: %d):\n",
3527 decoder_pvt->timeslot_in_num);
3528 print_vceinfo_packet(wc, cmd);
3529 }
3530
3531 if (send_voip_vopena_close_cmd(encoder_pvt, cmd))
3532 goto error_exit;
3533 if (send_voip_vopena_close_cmd(decoder_pvt, cmd))
3534 goto error_exit;
3535 if (send_trans_disconnect_cmd(wc, cmd, chan1, chan2, 0, 0))
3536 goto error_exit;
3537 if (send_destroy_channel_cmd(wc, cmd, chan1))
3538 goto error_exit;
3539 if (send_destroy_channel_cmd(wc, cmd, chan2))
3540 goto error_exit;
3541
3542 if (debug & DTE_DEBUG_ETH_STATS) {
3543 if (send_eth_statistics_cmd(wc, cmd))
3544 goto error_exit;
3545 print_eth_statistics_packet(wc, cmd);
3546 dev_info(&wc->pdev->dev, "AN983 tx packets: %d rx packets: %d\n",
3547 wctc4xxp_get_packet_count(wc->txd),
3548 wctc4xxp_get_packet_count(wc->rxd));
3549 }
3550
3551 free_cmd(cmd);
3552 return 0;
3553 error_exit:
3554 free_cmd(cmd);
3555 return -1;
3556 }
3557
3558
3559 static int wctc4xxp_setup_device(struct wcdte *wc)
3560 {
3561 struct tcb *cmd;
3562 int tdm_bus;
3563
3564 cmd = alloc_cmd(SFRAME_SIZE);
3565 if (!cmd)
3566 return -ENOMEM;
3567
3568 if (send_set_arm_clk_cmd(wc, cmd))
3569 goto error_exit;
3570
3571 if (send_set_spu_clk_cmd(wc, cmd))
3572 goto error_exit;
3573
3574 if (send_tdm_select_bus_mode_cmd(wc, cmd))
3575 goto error_exit;
3576
3577 for (tdm_bus = 0; tdm_bus < 4; ++tdm_bus) {
3578 if (send_supvsr_setup_tdm_parms(wc, cmd, tdm_bus))
3579 goto error_exit;
3580 }
3581
3582 if (send_set_eth_header_cmd(wc, cmd, src_mac, dst_mac))
3583 goto error_exit;
3584
3585 if (send_ip_service_config_cmd(wc, cmd))
3586 goto error_exit;
3587
3588 if (send_arp_service_config_cmd(wc, cmd))
3589 goto error_exit;
3590
3591 if (send_icmp_service_config_cmd(wc, cmd))
3592 goto error_exit;
3593
3594 if (send_device_set_country_code_cmd(wc, cmd))
3595 goto error_exit;
3596
3597 if (send_spu_features_control_cmd(wc, cmd, 0x02))
3598 goto error_exit;
3599
3600 if (send_ip_options_cmd(wc, cmd))
3601 goto error_exit;
3602
3603 if (send_spu_features_control_cmd(wc, cmd, 0x04))
3604 goto error_exit;
3605
3606 if (send_csme_multi_cmd(wc, cmd))
3607 goto error_exit;
3608
3609 if (send_tdm_opt_cmd(wc, cmd))
3610 goto error_exit;
3611
3612 free_cmd(cmd);
3613 return 0;
3614 error_exit:
3615 free_cmd(cmd);
3616 return -1;
3617 }
3618
3619 static void wctc4xxp_setup_file_operations(struct file_operations *fops)
3620 {
3621 fops->owner = THIS_MODULE;
3622 fops->read = wctc4xxp_read;
3623 fops->write = wctc4xxp_write;
3624 }
3625
3626 static int
3627 initialize_channel_pvt(struct wcdte *wc, int encoder,
3628 struct channel_pvt **cpvt)
3629 {
3630 int chan;
3631 *cpvt = kmalloc(sizeof(struct channel_pvt) * wc->numchannels,
3632 GFP_KERNEL);
3633 if (!(*cpvt))
3634 return -ENOMEM;
3635 for (chan = 0; chan < wc->numchannels; ++chan)
3636 wctc4xxp_init_state((*cpvt) + chan, encoder, chan, wc);
3637 return 0;
3638 }
3639
3640 static int
3641 initialize_transcoder(struct wcdte *wc, unsigned int srcfmts,
3642 unsigned int dstfmts, struct channel_pvt *pvts,
3643 struct dahdi_transcoder **zt)
3644 {
3645 int chan;
3646 *zt = dahdi_transcoder_alloc(wc->numchannels);
3647 if (!(*zt))
3648 return -ENOMEM;
3649 (*zt)->srcfmts = srcfmts;
3650 (*zt)->dstfmts = dstfmts;
3651 (*zt)->allocate = wctc4xxp_operation_allocate;
3652 (*zt)->release = wctc4xxp_operation_release;
3653 wctc4xxp_setup_file_operations(&((*zt)->fops));
3654 for (chan = 0; chan < wc->numchannels; ++chan)
3655 (*zt)->channels[chan].pvt = &pvts[chan];
3656 return 0;
3657 }
3658
3659 static int initialize_encoders(struct wcdte *wc, unsigned int complexfmts)
3660 {
3661 int res;
3662 res = initialize_channel_pvt(wc, 1, &wc->encoders);
3663 if (res)
3664 return res;
3665
3666 res = initialize_transcoder(wc, DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW,
3667 complexfmts, wc->encoders, &wc->uencode);
3668 if (res)
3669 return res;
3670 sprintf(wc->uencode->name, "DTE Encoder");
3671 return res;
3672 }
3673
3674 static int
3675 initialize_decoders(struct wcdte *wc, unsigned int complexfmts)
3676 {
3677 int res;
3678 res = initialize_channel_pvt(wc, 0, &wc->decoders);
3679 if (res)
3680 return res;
3681
3682 res = initialize_transcoder(wc, complexfmts,
3683 DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW,
3684 wc->decoders, &wc->udecode);
3685 if (res)
3686 return res;
3687
3688 sprintf(wc->udecode->name, "DTE Decoder");
3689 return res;
3690 }
3691
3692 static void
3693 wctc4xxp_send_commands(struct wcdte *wc, struct list_head *to_send)
3694 {
3695 struct tcb *cmd;
3696 while (!list_empty(to_send)) {
3697 cmd = container_of(to_send->next, struct tcb, node);
3698 list_del_init(&cmd->node);
3699 wctc4xxp_transmit_cmd(wc, cmd);
3700 }
3701 }
3702
3703 static void
3704 wctc4xxp_watchdog(unsigned long data)
3705 {
3706 struct wcdte *wc = (struct wcdte *)data;
3707 struct tcb *cmd, *temp;
3708 LIST_HEAD(cmds_to_retry);
3709 const int MAX_RETRIES = 5;
3710 int reschedule_timer = 0;
3711 unsigned long flags;
3712
3713 service_tx_ring(wc);
3714
3715 spin_lock_irqsave(&wc->cmd_list_lock, flags);
3716 /* Go through the list of messages that are waiting for responses from
3717 * the DTE, and complete or retry any that have timed out. */
3718 list_for_each_entry_safe(cmd, temp,
3719 &wc->waiting_for_response_list, node) {
3720
3721 if (!time_after(jiffies, cmd->timeout))
3722 continue;
3723
3724 if (++cmd->retries > MAX_RETRIES) {
3725 if (!(cmd->flags & TX_COMPLETE)) {
3726
3727 cmd->flags |= DTE_CMD_TIMEOUT;
3728 list_del_init(&cmd->node);
3729 if (cmd->complete)
3730 complete(cmd->complete);
3731
3732 wctc4xxp_reset_processor(wc);
3733 set_bit(DTE_SHUTDOWN, &wc->flags);
3734 spin_unlock_irqrestore(&wc->cmd_list_lock,
3735 flags);
3736 _wctc4xxp_stop_dma(wc);
3737 dev_err(&wc->pdev->dev,
3738 "Board malfunctioning. Halting operation.\n");
3739 reschedule_timer = 0;
3740 spin_lock_irqsave(&wc->cmd_list_lock, flags);
3741 break;
3742 }
3743 /* ERROR: We've retried the command and
3744 * haven't received the ACK or the response.
3745 */
3746 cmd->flags |= DTE_CMD_TIMEOUT;
3747 list_del_init(&cmd->node);
3748 if (cmd->complete)
3749 complete(cmd->complete);
3750 } else if (cmd->flags & TX_COMPLETE) {
3751 /* Move this to the local list because we're
3752 * going to resend it once we free the locks
3753 */
3754 list_move_tail(&cmd->node, &cmds_to_retry);
3755 cmd->flags &= ~(TX_COMPLETE);
3756 } else {
3757 /* The command is still sitting on the tx
3758 * descriptor ring. We don't want to move it
3759 * off any lists, lets just reset the timeout
3760 * and tell the hardware to look for another
3761 * command . */
3762 dev_warn(&wc->pdev->dev,
3763 "Retrying command that was still on descriptor list.\n");
3764 cmd->timeout = jiffies + HZ/4;
3765 wctc4xxp_transmit_demand_poll(wc);
3766 reschedule_timer = 1;
3767 }
3768 }
3769 spin_unlock_irqrestore(&wc->cmd_list_lock, flags);
3770
3771 if (list_empty(&cmds_to_retry) && reschedule_timer)
3772 mod_timer(&wc->watchdog, jiffies + HZ/2);
3773 else if (!list_empty(&cmds_to_retry))
3774 wctc4xxp_send_commands(wc, &cmds_to_retry);
3775 }
3776
3777 /**
3778 * Insert an struct wcdte on the global list in sorted order
3779 *
3780 */
3781 static int __devinit
3782 wctc4xxp_add_to_device_list(struct wcdte *wc)
3783 {
3784 struct wcdte *cur;
3785 int pos = 0;
3786 INIT_LIST_HEAD(&wc->node);
3787 spin_lock(&wctc4xxp_list_lock);
3788 list_for_each_entry(cur, &wctc4xxp_list, node) {
3789 if (cur->pos != pos) {
3790 /* Add the new entry before the one here */
3791 list_add_tail(&wc->node, &cur->node);
3792 break;
3793 } else {
3794 ++pos;
3795 }
3796 }
3797 /* If we didn't already add the new entry to the list, add it now */
3798 if (list_empty(&wc->node))
3799 list_add_tail(&wc->node, &wctc4xxp_list);
3800 spin_unlock(&wctc4xxp_list_lock);
3801 return pos;
3802 }
3803
3804 static void wctc4xxp_remove_from_device_list(struct wcdte *wc)
3805 {
3806 spin_lock(&wctc4xxp_list_lock);
3807 list_del(&wc->node);
3808 spin_unlock(&wctc4xxp_list_lock);
3809 }
3810
3811 struct wctc4xxp_desc {
3812 const char *short_name;
3813 const char *long_name;
3814 };
3815
3816 static struct wctc4xxp_desc wctc400p = {
3817 .short_name = "tc400b",
3818 .long_name = "Wildcard TC400P+TC400M",
3819 };
3820
3821 static struct wctc4xxp_desc wctce400 = {
3822 .short_name = "tce400",
3823 .long_name = "Wildcard TCE400+TC400M",
3824 };
3825
3826 static void wctc4xxp_cleanup_channels(struct wcdte *wc);
3827
3828 static int wctc4xxp_reset_and_reload_firmware(struct wcdte *wc,
3829 const struct firmware *firmware)
3830 {
3831 int res;
3832
3833 wctc4xxp_cleanup_command_list(wc);
3834 wctc4xxp_cleanup_channels(wc);
3835
3836 res = wctc4xxp_boot_processor(wc, firmware);
3837 if (res)
3838 return res;
3839
3840 #if defined(CONFIG_WCTC4XXP_POLLING)
3841 wctc4xxp_enable_polling(wc);
3842 #endif
3843 res = wctc4xxp_setup_device(wc);
3844 if (res) {
3845 dev_err(&wc->pdev->dev, "Failed to setup DTE\n");
3846 return res;
3847 }
3848
3849 return 0;
3850 }
3851
3852 static int wctc4xxp_reset_driver_state(struct wcdte *wc)
3853 {
3854 int res;
3855 struct firmware embedded_firmware;
3856 unsigned long flags;
3857 const struct firmware *firmware = &embedded_firmware;
3858 #if !defined(HOTPLUG_FIRMWARE)
3859 extern void _binary_dahdi_fw_tc400m_bin_size;
3860 extern u8 _binary_dahdi_fw_tc400m_bin_start[];
3861 embedded_firmware.data = _binary_dahdi_fw_tc400m_bin_start;
3862 embedded_firmware.size = (size_t) &_binary_dahdi_fw_tc400m_bin_size;
3863 #else
3864 static const char tc400m_firmware[] = "dahdi-fw-tc400m.bin";
3865
3866 res = request_firmware(&firmware, tc400m_firmware, &wc->pdev->dev);
3867 if (res || !firmware) {
3868 dev_err(&wc->pdev->dev,
3869 "Firmware %s not available from userspace. (%d)\n",
3870 tc400m_firmware, res);
3871 return res;
3872 }
3873 #endif
3874 res = wctc4xxp_reset_and_reload_firmware(wc, firmware);
3875 if (firmware != &embedded_firmware)
3876 release_firmware(firmware);
3877 spin_lock_irqsave(&wc->rxd->lock, flags);
3878 wc->rxd->packet_errors = 0;
3879 wc->reported_packet_errors = 0;
3880 spin_unlock_irqrestore(&wc->rxd->lock, flags);
3881 return res;
3882 }
3883
3884 #ifdef EXPORT_FOR_ALERT_ATTRIBUTE
3885
3886 static ssize_t wctc4xxp_force_alert_store(struct device *dev,
3887 struct device_attribute *attr,
3888 const char *buf, size_t count)
3889 {
3890 int res;
3891 unsigned int alert_type;
3892 struct wcdte *wc = dev_get_drvdata(dev);
3893 struct tcb *cmd = alloc_cmd(SFRAME_SIZE);
3894 u16 parameters[] = {0};
3895 if (!cmd)
3896 return -ENOMEM;
3897
3898 res = sscanf(buf, "%x", &alert_type);
3899 if (1 != res) {
3900 free_cmd(cmd);
3901 return -EINVAL;
3902 }
3903
3904 dev_info(&wc->pdev->dev, "Forcing alert type: 0x%x\n", alert_type);
3905 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
3906 mutex_lock(&wc->chanlock);
3907 #else
3908 res = mutex_lock_killable(&wc->chanlock);
3909 if (res) {
3910 free_cmd(cmd);
3911 return -EAGAIN;
3912 }
3913 #endif
3914
3915
3916 parameters[0] = alert_type;
3917
3918 create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
3919 CONFIG_DEVICE_CLASS, 0x0409, parameters,
3920 ARRAY_SIZE(parameters));
3921
3922 wctc4xxp_transmit_cmd(wc, cmd);
3923
3924 mutex_unlock(&wc->chanlock);
3925
3926 return count;
3927 }
3928
3929 static DEVICE_ATTR(force_alert, 0200, NULL, wctc4xxp_force_alert_store);
3930
3931 static void wctc4xxp_create_sysfs_files(struct wcdte *wc)
3932 {
3933 int ret;
3934 ret = device_create_file(&wc->pdev->dev, &dev_attr_force_alert);
3935 if (ret) {
3936 dev_info(&wc->pdev->dev,
3937 "Failed to create device attributes.\n");
3938 }
3939 }
3940
3941 static void wctc4xxp_remove_sysfs_files(struct wcdte *wc)
3942 {
3943 device_remove_file(&wc->pdev->dev, &dev_attr_force_alert);
3944 }
3945
3946 #else
3947 static inline void wctc4xxp_create_sysfs_files(struct wcdte *wc) { return; }
3948 static inline void wctc4xxp_remove_sysfs_files(struct wcdte *wc) { return; }
3949 #endif
3950
3951 static int __devinit
3952 wctc4xxp_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3953 {
3954 int res, reg, position_on_list;
3955 struct wcdte *wc = NULL;
3956 struct wctc4xxp_desc *d = (struct wctc4xxp_desc *)ent->driver_data;
3957 unsigned char g729_numchannels, g723_numchannels, min_numchannels;
3958 unsigned char wctc4xxp_firmware_ver, wctc4xxp_firmware_ver_minor;
3959 unsigned int complexfmts;
3960 struct firmware embedded_firmware;
3961 const struct firmware *firmware = &embedded_firmware;
3962 #if !defined(HOTPLUG_FIRMWARE)
3963 extern void _binary_dahdi_fw_tc400m_bin_size;
3964 extern u8 _binary_dahdi_fw_tc400m_bin_start[];
3965 #else
3966 static const char tc400m_firmware[] = "dahdi-fw-tc400m.bin";
3967 #endif
3968
3969 /* ------------------------------------------------------------------
3970 * Setup the pure software constructs internal to this driver.
3971 * --------------------------------------------------------------- */
3972
3973 wc = kzalloc(sizeof(*wc), GFP_KERNEL);
3974 if (!wc)
3975 return -ENOMEM;
3976
3977 position_on_list = wctc4xxp_add_to_device_list(wc);
3978 snprintf(wc->board_name, sizeof(wc->board_name)-1, "%s%d",
3979 d->short_name, position_on_list);
3980 wc->iobase = pci_iomap(pdev, 1, 0);
3981 wc->pdev = pdev;
3982 wc->pos = position_on_list;
3983 wc->variety = d->long_name;
3984 wc->last_rx_seq_num = -1;
3985
3986 if (!request_mem_region(pci_resource_start(pdev, 1),
3987 pci_resource_len(pdev, 1), wc->board_name)) {
3988 dev_err(&pdev->dev, "IO Registers are in use by another "
3989 "module.\n");
3990 wctc4xxp_remove_from_device_list(wc);
3991 kfree(wc);
3992 return -EIO;
3993 }
3994
3995 mutex_init(&wc->chanlock);
3996 spin_lock_init(&wc->reglock);
3997 spin_lock_init(&wc->cmd_list_lock);
3998 spin_lock_init(&wc->rx_list_lock);
3999 spin_lock_init(&wc->rx_lock);
4000 INIT_LIST_HEAD(&wc->cmd_list);
4001 INIT_LIST_HEAD(&wc->waiting_for_response_list);
4002 INIT_LIST_HEAD(&wc->rx_list);
4003 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
4004 INIT_WORK(&wc->deferred_work, deferred_work_func, wc);
4005 #else
4006 INIT_WORK(&wc->deferred_work, deferred_work_func);
4007 #endif
4008 init_waitqueue_head(&wc->waitq);
4009
4010 if (pci_set_dma_mask(wc->pdev, DMA_BIT_MASK(32))) {
4011 release_mem_region(pci_resource_start(wc->pdev, 1),
4012 pci_resource_len(wc->pdev, 1));
4013 if (wc->iobase)
4014 pci_iounmap(wc->pdev, wc->iobase);
4015 pci_clear_mwi(wc->pdev);
4016 dev_warn(&wc->pdev->dev, "No suitable DMA available.\n");
4017 return -EIO;
4018 }
4019
4020 wc->txd = kmalloc(sizeof(*wc->txd), GFP_KERNEL);
4021 if (!wc->txd) {
4022 res = -ENOMEM;
4023 goto error_exit_swinit;
4024 }
4025
4026 res = wctc4xxp_initialize_descriptor_ring(wc->pdev, wc->txd,
4027 0xe0800000, DMA_TO_DEVICE, DEFAULT_TX_DRING_SIZE);
4028 if (res)
4029 goto error_exit_swinit;
4030
4031 wc->rxd = kmalloc(sizeof(*wc->rxd), GFP_KERNEL);
4032 if (!wc->rxd) {
4033 res = -ENOMEM;
4034 goto error_exit_swinit;
4035 }
4036
4037 res = wctc4xxp_initialize_descriptor_ring(wc->pdev, wc->rxd, 0,
4038 DMA_FROM_DEVICE, DEFAULT_RX_DRING_SIZE);
4039 if (res)
4040 goto error_exit_swinit;
4041
4042 #if defined(HOTPLUG_FIRMWARE)
4043 res = request_firmware(&firmware, tc400m_firmware, &wc->pdev->dev);
4044 if (res || !firmware) {
4045 dev_err(&wc->pdev->dev,
4046 "Firmware %s not available from userspace. (%d)\n",
4047 tc400m_firmware, res);
4048 goto error_exit_swinit;
4049 }
4050 #else
4051 embedded_firmware.data = _binary_dahdi_fw_tc400m_bin_start;
4052 embedded_firmware.size = (size_t) &_binary_dahdi_fw_tc400m_bin_size;
4053 #endif
4054
4055 wctc4xxp_firmware_ver = firmware->data[0];
4056 wctc4xxp_firmware_ver_minor = firmware->data[16];
4057 g729_numchannels = firmware->data[1];
4058 g723_numchannels = firmware->data[2];
4059
4060 min_numchannels = min(g723_numchannels, g729_numchannels);
4061
4062 if (!mode || strlen(mode) < 4) {
4063 sprintf(wc->complexname, "G.729a / G.723.1");
4064 complexfmts = DAHDI_FORMAT_G729A | DAHDI_FORMAT_G723_1;
4065 wc->numchannels = min_numchannels;
4066 } else if (mode[3] == '9') { /* "G.729" */
4067 sprintf(wc->complexname, "G.729a");
4068 complexfmts = DAHDI_FORMAT_G729A;
4069 wc->numchannels = g729_numchannels;
4070 } else if (mode[3] == '3') { /* "G.723.1" */
4071 sprintf(wc->complexname, "G.723.1");
4072 complexfmts = DAHDI_FORMAT_G723_1;
4073 wc->numchannels = g723_numchannels;
4074 } else {
4075 sprintf(wc->complexname, "G.729a / G.723.1");
4076 complexfmts = DAHDI_FORMAT_G729A | DAHDI_FORMAT_G723_1;
4077 wc->numchannels = min_numchannels;
4078 }
4079
4080 res = initialize_encoders(wc, complexfmts);
4081 if (res)
4082 goto error_exit_swinit;
4083 res = initialize_decoders(wc, complexfmts);
4084 if (res)
4085 goto error_exit_swinit;
4086
4087 if (DTE_DEBUG_NETWORK_IF & debug) {
4088 res = wctc4xxp_net_register(wc);
4089 if (res)
4090 goto error_exit_swinit;
4091 }
4092
4093 # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
4094 wc->watchdog.function = wctc4xxp_watchdog;
4095 wc->watchdog.data = (unsigned long)wc;
4096 init_timer(&wc->watchdog);
4097 # else
4098 setup_timer(&wc->watchdog, wctc4xxp_watchdog, (unsigned long)wc);
4099 # endif
4100
4101 /* ------------------------------------------------------------------
4102 * Load the firmware and start the DTE.
4103 * --------------------------------------------------------------- */
4104
4105 res = pci_enable_device(pdev);
4106 if (res) {
4107 dev_err(&wc->pdev->dev, "Failed to enable device.\n");
4108 goto error_exit_swinit;;
4109 }
4110 res = pci_set_mwi(wc->pdev);
4111 if (res) {
4112 dev_warn(&wc->pdev->dev,
4113 "Failed to set Memory-Write Invalidate Command Bit..\n");
4114 }
4115 pci_set_master(pdev);
4116 pci_set_drvdata(pdev, wc);
4117 res = request_irq(pdev->irq, wctc4xxp_interrupt,
4118 IRQF_SHARED, wc->board_name, wc);
4119 if (res) {
4120 dev_err(&wc->pdev->dev,
4121 "Unable to request IRQ %d\n", pdev->irq);
4122 if (firmware != &embedded_firmware)
4123 release_firmware(firmware);
4124 goto error_exit_hwinit;
4125 }
4126 res = wctc4xxp_hardware_init(wc);
4127 if (res) {
4128 if (firmware != &embedded_firmware)
4129 release_firmware(firmware);
4130 goto error_exit_hwinit;
4131 }
4132 wctc4xxp_enable_interrupts(wc);
4133 wctc4xxp_start_dma(wc);
4134
4135 res = wctc4xxp_reset_and_reload_firmware(wc, firmware);
4136 if (firmware != &embedded_firmware)
4137 release_firmware(firmware);
4138 if (res)
4139 goto error_exit_hwinit;
4140
4141 /* \todo Read firmware version directly from tc400b.*/
4142 dev_info(&wc->pdev->dev, "(%s) Transcoder support LOADED " \
4143 "(firm ver = %d.%d)\n", wc->complexname, wctc4xxp_firmware_ver,
4144 wctc4xxp_firmware_ver_minor);
4145
4146 reg = wctc4xxp_getctl(wc, 0x00fc);
4147
4148 DTE_DEBUG(DTE_DEBUG_GENERAL,
4149 "debug: (post-boot) Reg fc is %08x\n", reg);
4150
4151 dev_info(&wc->pdev->dev, "Installed a Wildcard TC: %s\n", wc->variety);
4152 DTE_DEBUG(DTE_DEBUG_GENERAL, "Operating in DEBUG mode.\n");
4153 dahdi_transcoder_register(wc->uencode);
4154 dahdi_transcoder_register(wc->udecode);
4155
4156 wctc4xxp_match_packet_counts(wc);
4157
4158 wctc4xxp_create_sysfs_files(wc);
4159
4160 return 0;
4161
4162 error_exit_hwinit:
4163 #if defined(CONFIG_WCTC4XXP_POLLING)
4164 wctc4xxp_disable_polling(wc);
4165 #endif
4166 wctc4xxp_stop_dma(wc);
4167 wctc4xxp_cleanup_command_list(wc);
4168 free_irq(pdev->irq, wc);
4169 pci_set_drvdata(pdev, NULL);
4170 error_exit_swinit:
4171 wctc4xxp_net_unregister(wc);
4172 kfree(wc->encoders);
4173 kfree(wc->decoders);
4174 dahdi_transcoder_free(wc->uencode);
4175 dahdi_transcoder_free(wc->udecode);
4176 wctc4xxp_cleanup_descriptor_ring(wc->txd);
4177 kfree(wc->txd);
4178 wctc4xxp_cleanup_descriptor_ring(wc->rxd);
4179 kfree(wc->rxd);
4180 release_mem_region(pci_resource_start(wc->pdev, 1),
4181 pci_resource_len(wc->pdev, 1));
4182 if (wc->iobase)
4183 pci_iounmap(wc->pdev, wc->iobase);
4184 pci_clear_mwi(wc->pdev);
4185 wctc4xxp_remove_from_device_list(wc);
4186 kfree(wc);
4187 return res;
4188 }
4189
4190 static void wctc4xxp_cleanup_channels(struct wcdte *wc)
4191 {
4192 int i;
4193 struct dahdi_transcoder_channel *dtc_en, *dtc_de;
4194 struct channel_pvt *cpvt;
4195
4196 for (i = 0; i < wc->numchannels; ++i) {
4197 dtc_en = &(wc->uencode->channels[i]);
4198 wctc4xxp_cleanup_channel_private(wc, dtc_en);
4199 dahdi_tc_clear_busy(dtc_en);
4200 dahdi_tc_clear_built(dtc_en);
4201
4202 dtc_en->built_fmts = 0;
4203 cpvt = dtc_en->pvt;
4204 cpvt->chan_in_num = INVALID;
4205 cpvt->chan_out_num = INVALID;
4206
4207
4208 dtc_de = &(wc->udecode->channels[i]);
4209 wctc4xxp_cleanup_channel_private(wc, dtc_de);
4210 dahdi_tc_clear_busy(dtc_de);
4211 dahdi_tc_clear_built(dtc_de);
4212
4213 dtc_de->built_fmts = 0;
4214 cpvt = dtc_de->pvt;
4215 cpvt->chan_in_num = INVALID;
4216 cpvt->chan_out_num = INVALID;
4217 }
4218 }
4219
4220 static void __devexit wctc4xxp_remove_one(struct pci_dev *pdev)
4221 {
4222 struct wcdte *wc = pci_get_drvdata(pdev);
4223
4224 if (!wc)
4225 return;
4226
4227 wctc4xxp_remove_sysfs_files(wc);
4228
4229 wctc4xxp_remove_from_device_list(wc);
4230
4231 set_bit(DTE_SHUTDOWN, &wc->flags);
4232 if (del_timer_sync(&wc->watchdog))
4233 del_timer_sync(&wc->watchdog);
4234
4235 /* This should already be stopped, but it doesn't hurt to make sure. */
4236 clear_bit(DTE_POLLING, &wc->flags);
4237 wctc4xxp_net_unregister(wc);
4238
4239 /* Stop any DMA */
4240 wctc4xxp_stop_dma(wc);
4241
4242 /* In case hardware is still there */
4243 wctc4xxp_disable_interrupts(wc);
4244
4245 free_irq(pdev->irq, wc);
4246
4247 /* There isn't anything that would run in the workqueue that will wait
4248 * on an interrupt. */
4249
4250 dahdi_transcoder_unregister(wc->udecode);
4251 dahdi_transcoder_unregister(wc->uencode);
4252
4253 /* Free Resources */
4254 release_mem_region(pci_resource_start(wc->pdev, 1),
4255 pci_resource_len(wc->pdev, 1));
4256 if (wc->iobase)
4257 pci_iounmap(wc->pdev, wc->iobase);
4258 pci_clear_mwi(wc->pdev);
4259 wctc4xxp_cleanup_descriptor_ring(wc->txd);
4260 kfree(wc->txd);
4261 wctc4xxp_cleanup_descriptor_ring(wc->rxd);
4262 kfree(wc->rxd);
4263
4264 wctc4xxp_cleanup_command_list(wc);
4265 wctc4xxp_cleanup_channels(wc);
4266
4267 pci_set_drvdata(pdev, NULL);
4268
4269 dahdi_transcoder_free(wc->uencode);
4270 dahdi_transcoder_free(wc->udecode);
4271 kfree(wc->encoders);
4272 kfree(wc->decoders);
4273 kfree(wc);
4274 }
4275
4276 static DEFINE_PCI_DEVICE_TABLE(wctc4xxp_pci_tbl) = {
4277 { 0xd161, 0x3400, PCI_ANY_ID, PCI_ANY_ID,
4278 0, 0, (unsigned long) &wctc400p }, /* Digium board */
4279 { 0xd161, 0x8004, PCI_ANY_ID, PCI_ANY_ID,
4280 0, 0, (unsigned long) &wctce400 }, /* Digium board */
4281 { 0 }
4282 };
4283
4284 MODULE_DEVICE_TABLE(pci, wctc4xxp_pci_tbl);
4285
4286 static int wctc4xxp_suspend(struct pci_dev *pdev, pm_message_t state)
4287 {
4288 return -ENOSYS;
4289 }
4290
4291 static struct pci_driver wctc4xxp_driver = {
4292 .name = "wctc4xxp",
4293 .probe = wctc4xxp_init_one,
4294 .remove = __devexit_p(wctc4xxp_remove_one),
4295 .id_table = wctc4xxp_pci_tbl,
4296 .suspend = wctc4xxp_suspend,
4297 };
4298
4299 static int __init wctc4xxp_init(void)
4300 {
4301 int res;
4302 unsigned long cache_flags;
4303
4304 #if defined(CONFIG_SLUB) && (LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 22))
4305 cache_flags = SLAB_HWCACHE_ALIGN | SLAB_STORE_USER | SLAB_DEBUG_FREE;
4306 #else
4307 cache_flags = SLAB_HWCACHE_ALIGN;
4308 #endif
4309
4310 # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 23)
4311 cmd_cache = kmem_cache_create(THIS_MODULE->name, sizeof(struct tcb),
4312 0, cache_flags, NULL, NULL);
4313 # else
4314 cmd_cache = kmem_cache_create(THIS_MODULE->name, sizeof(struct tcb),
4315 0, cache_flags, NULL);
4316 # endif
4317
4318 if (!cmd_cache)
4319 return -ENOMEM;
4320 spin_lock_init(&wctc4xxp_list_lock);
4321 INIT_LIST_HEAD(&wctc4xxp_list);
4322 res = dahdi_pci_module(&wctc4xxp_driver);
4323 if (res) {
4324 kmem_cache_destroy(cmd_cache);
4325 return -ENODEV;
4326 }
4327 return 0;
4328 }
4329
4330 static void __exit wctc4xxp_cleanup(void)
4331 {
4332 pci_unregister_driver(&wctc4xxp_driver);
4333 kmem_cache_destroy(cmd_cache);
4334 }
4335
4336 module_param(debug, int, S_IRUGO | S_IWUSR);
4337 module_param(mode, charp, S_IRUGO);
4338 MODULE_PARM_DESC(mode, "'g729', 'g723.1', or 'any'. Default 'any'.");
4339 MODULE_DESCRIPTION("Wildcard TC400P+TC400M Driver");
4340 MODULE_AUTHOR("Digium Incorporated <[email protected]>");
4341 MODULE_LICENSE("GPL");
4342
4343 module_init(wctc4xxp_init);
4344 module_exit(wctc4xxp_cleanup);