wctc4xxp: Trivial fix typo that was preventing firmware load.
[dahdi/linux.git] / drivers / dahdi / wctc4xxp / base.c
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 #include <linux/pci.h>
30 #include <linux/interrupt.h>
31 #include <linux/delay.h>
32 #include <linux/jiffies.h>
33 #include <linux/moduleparam.h>
34 #include <linux/firmware.h>
35 #include <linux/if_ether.h>
36 #include <linux/ip.h>
37 #include <linux/udp.h>
38 #include <linux/etherdevice.h>
39 #include <linux/timer.h>
40
41 #include <stdbool.h>
42
43 #include "dahdi/kernel.h"
44
45 #include <linux/io.h>
46
47 /* COMPILE TIME OPTIONS =================================================== */
48
49 #define INTERRUPT 0
50 #define WORKQUEUE 1
51 #define TASKLET   2
52
53 #ifndef DEFERRED_PROCESSING
54 #       define DEFERRED_PROCESSING WORKQUEUE
55 #endif
56
57 #if DEFERRED_PROCESSING == INTERRUPT
58 #       define ALLOC_FLAGS GFP_ATOMIC
59 #elif DEFERRED_PROCESSING == TASKLET
60 #       define ALLOC_FLAGS GFP_ATOMIC
61 #else
62 #       define ALLOC_FLAGS GFP_KERNEL
63 #endif
64
65 #define WARN_ALWAYS() WARN_ON(1)
66
67 #define DTE_DEBUG(_dbgmask, _fmt, _args...)                             \
68         if ((debug & _dbgmask) == (_dbgmask)) {                         \
69                 dev_info(&(wc)->pdev->dev, _fmt, ## _args);             \
70         }                                                               \
71
72
73 /* define CONFIG_WCTC4XXP_POLLING to operate in a pure polling mode.  This is
74  * was placed in as a debugging tool for a particluar system that wasn't
75  * routing the interrupt properly. Therefore it is off by default and the
76  * driver must be recompiled to enable it. */
77 #undef CONFIG_WCTC4XXP_POLLING
78
79 /* The total number of active channels over which the driver will start polling
80  * the card every 10 ms. */
81 #define POLLING_CALL_THRESHOLD 40
82
83 #define INVALID 999 /* Used to mark invalid channels, commands, etc.. */
84 #define MAX_CHANNEL_PACKETS  5
85
86 #define G729_LENGTH     20
87 #define G723_LENGTH     30
88
89 #define G729_SAMPLES    160     /* G.729 */
90 #define G723_SAMPLES    240     /* G.723.1 */
91
92 #define G729_BYTES      20      /* G.729 */
93 #define G723_6K_BYTES   24      /* G.723.1 at 6.3kb/s */
94 #define G723_5K_BYTES   20      /* G.723.1 at 5.3kb/s */
95 #define G723_SID_BYTES  4       /* G.723.1 SID frame */
96
97 #define MAX_CAPTURED_PACKETS 5000
98
99 /* The following bit fields are used to set the various debug levels. */
100 #define DTE_DEBUG_GENERAL       (1 << 0) /* 1  */
101 #define DTE_DEBUG_CHANNEL_SETUP (1 << 1) /* 2  */
102 #define DTE_DEBUG_RTP_TX        (1 << 2) /* 4  */
103 #define DTE_DEBUG_RTP_RX        (1 << 3) /* 8  */
104 #define DTE_DEBUG_RX_TIMEOUT    (1 << 4) /* 16 */
105 #define DTE_DEBUG_NETWORK_IF    (1 << 5) /* 32 */
106 #define DTE_DEBUG_NETWORK_EARLY (1 << 6) /* 64 */
107 #define DTE_DEBUG_ETH_STATS     (1 << 7) /* 128 */
108
109 static int debug;
110 static char *mode;
111
112 static spinlock_t wctc4xxp_list_lock;
113 static struct list_head wctc4xxp_list;
114
115 #define ETH_P_CSM_ENCAPS 0x889B
116
117 struct rtphdr {
118 #if defined(__LITTLE_ENDIAN_BITFIELD)
119         __u8    csrc_count:4;
120         __u8    extension:1;
121         __u8    padding:1;
122         __u8    ver:2;
123         __u8    type:7;
124         __u8    marker:1;
125 #elif defined(__BIG_ENDIAN_BITFIELD)
126         __u8    ver:2;
127         __u8    padding:1;
128         __u8    extension:1;
129         __u8    csrc_count:4;
130         __u8    marker:1;
131         __u8    type:7;
132 #else
133 #error "Please fix <asm/byteorder.h>"
134 #endif
135         __be16  seqno;
136         __be32  timestamp;
137         __be32  ssrc;
138 } __attribute__((packed));
139
140 struct rtp_packet {
141         struct ethhdr ethhdr;
142         struct iphdr  iphdr;
143         struct udphdr udphdr;
144         struct rtphdr rtphdr;
145         __u8   payload[0];
146 } __attribute__((packed));
147
148 struct csm_encaps_cmd {
149         /* COMMON PART OF PAYLOAD HEADER */
150         __u8   length;
151         __u8   index;
152         __u8   type;
153         __u8   class;
154         __le16 function;
155         __le16 reserved;
156         __le16 params[0];
157 } __attribute__((packed));
158
159 /* Ethernet packet type for communication control information to the DTE. */
160 struct csm_encaps_hdr {
161         struct ethhdr ethhdr;
162         /* CSM_ENCAPS HEADER */
163         __be16 op_code;
164         __u8   seq_num;
165         __u8   control;
166         __be16 channel;
167         /* There is always at least one command. */
168         struct csm_encaps_cmd cmd;
169 } __attribute__((packed));
170
171 #define CONTROL_PACKET_OPCODE  0x0001
172 /* Control bits */
173 #define LITTLE_ENDIAN   0x01
174 #define SUPPRESS_ACK    0x40
175 #define MESSAGE_PACKET  0x80
176
177 #define SUPERVISOR_CHANNEL 0xffff
178
179 /* Supervisor function codes */
180 #define SUPVSR_CREATE_CHANNEL   0x0010
181
182 #define MONITOR_LIVE_INDICATION_TYPE 0x75
183 #define VOIP_VCEINFO_TYPE       0x0e
184 #define CONFIG_CHANGE_TYPE      0x00
185 #define CONFIG_CHANNEL_CLASS    0x02
186 #define CONFIG_DEVICE_CLASS     0x06
187
188 /* Individual channel config commands */
189 #define MAX_FRAME_SIZE 1518
190 #define SFRAME_SIZE MAX_FRAME_SIZE
191
192 #define DEFAULT_RX_DRING_SIZE (1 << 6) /* Must be a power of two */
193
194 /* Keep the TX ring shorter in order to reduce the amount of time needed to
195  * bring up channels when sending high priority csm_encaps packets. */
196 #define DEFAULT_TX_DRING_SIZE (1 << 4) /* Must be a power of two */
197 #define MIN_PACKET_LEN  64
198
199 /* Transcoder buffer (tcb) */
200 struct tcb {
201         void *data;
202         struct list_head node;
203         unsigned long timeout;
204         unsigned long retries;
205 #define TX_COMPLETE             (1 << 1)
206 #define DO_NOT_CAPTURE          (1 << 2)
207 #define WAIT_FOR_ACK            (1 << 3)
208 #define WAIT_FOR_RESPONSE       (1 << 4)
209 #define DTE_CMD_TIMEOUT         (1 << 5)
210         u16 flags;
211         u16 next_index;
212         struct completion *complete;
213         struct tcb *response;
214         struct channel_pvt *cpvt;
215         /* The number of bytes available in data. */
216         int data_len;
217 };
218
219 static inline const struct csm_encaps_hdr *
220 response_header(struct tcb *cmd)
221 {
222         BUG_ON(!cmd->response);
223         return (const struct csm_encaps_hdr *)(cmd)->response->data;
224 }
225
226 static inline void
227 initialize_cmd(struct tcb *cmd, unsigned long cmd_flags)
228 {
229         INIT_LIST_HEAD(&cmd->node);
230         cmd->flags = cmd_flags;
231 }
232
233 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
234 /*! Used to allocate commands to submit to the dte. */
235 kmem_cache_t *cmd_cache;
236 #else
237 /*! Used to allocate commands to submit to the dte. */
238 static struct kmem_cache *cmd_cache;
239 #endif
240
241 static inline struct tcb *
242 __alloc_cmd(size_t size, gfp_t alloc_flags, unsigned long cmd_flags)
243 {
244         struct tcb *cmd;
245
246         if (unlikely(size > SFRAME_SIZE))
247                 return NULL;
248         if (size < MIN_PACKET_LEN)
249                 size = MIN_PACKET_LEN;
250         cmd = kmem_cache_alloc(cmd_cache, alloc_flags);
251         if (likely(cmd)) {
252                 memset(cmd, 0, sizeof(*cmd));
253                 cmd->data = kzalloc(size, alloc_flags);
254                 if (unlikely(!cmd->data)) {
255                         kmem_cache_free(cmd_cache, cmd);
256                         return NULL;
257                 }
258                 cmd->data_len = size;
259                 initialize_cmd(cmd, cmd_flags);
260         }
261         return cmd;
262 }
263
264 static struct tcb *
265 alloc_cmd(size_t size)
266 {
267         return __alloc_cmd(size, GFP_KERNEL, 0);
268 }
269
270 static void
271 __free_cmd(struct tcb *cmd)
272 {
273         if (cmd)
274                 kfree(cmd->data);
275         kmem_cache_free(cmd_cache, cmd);
276         return;
277 }
278
279 static void
280 free_cmd(struct tcb *cmd)
281 {
282         if (cmd->response)
283                 __free_cmd(cmd->response);
284         __free_cmd(cmd);
285 }
286
287 struct channel_stats {
288         atomic_t packets_sent;
289         atomic_t packets_received;
290 };
291
292 struct channel_pvt {
293         spinlock_t lock;        /* Lock for this structure */
294         struct wcdte *wc;
295         u16 seqno;
296         u8 cmd_seqno;
297         u8 ssrc;
298         u8 last_rx_seq_num;
299         u16 timeslot_in_num;    /* DTE timeslot to receive from */
300         u16 timeslot_out_num;   /* DTE timeslot to send data to */
301         u16 chan_in_num;        /* DTE channel to receive from */
302         u16 chan_out_num;       /* DTE channel to send data to */
303         u32 last_timestamp;
304         struct {
305                 u8 encoder:1;   /* If we're an encoder */
306         };
307         struct channel_stats stats;
308         struct list_head rx_queue; /* Transcoded packets for this channel. */
309
310         /* Used to prevent user space from flooding the firmware. */
311         struct list_head node;
312         long samples_in_flight;
313         unsigned long send_time;
314 };
315
316 struct wcdte {
317         char board_name[40];
318         const char *variety;
319         int pos;
320         struct list_head node;
321         spinlock_t reglock;
322         wait_queue_head_t waitq;
323         struct mutex chanlock;
324 #define DTE_READY       1
325 #define DTE_SHUTDOWN    2
326 #define DTE_POLLING     3
327         unsigned long flags;
328
329         /* This is a device-global list of commands that are waiting to be
330          * transmited (and did not fit on the transmit descriptor ring) */
331         spinlock_t cmd_list_lock;
332         struct list_head cmd_list;
333         struct list_head waiting_for_response_list;
334
335         spinlock_t rx_list_lock;
336         struct list_head rx_list;
337         spinlock_t rx_lock;
338
339         unsigned int seq_num;
340         int last_rx_seq_num;
341         unsigned char numchannels;
342         unsigned char complexname[40];
343
344         /* This section contains the members necessary to communicate with the
345          * physical interface to the transcoding engine.  */
346         struct pci_dev *pdev;
347         unsigned int   intmask;
348         void __iomem    *iobase;
349         struct wctc4xxp_descriptor_ring *txd;
350         struct wctc4xxp_descriptor_ring *rxd;
351
352         struct dahdi_transcoder *uencode;
353         struct dahdi_transcoder *udecode;
354         struct channel_pvt *encoders;
355         struct channel_pvt *decoders;
356
357 #if DEFERRED_PROCESSING == WORKQUEUE
358         struct work_struct deferred_work;
359 #endif
360
361         /*
362          * This section contains the members necessary for exporting the
363          * network interface to the host system.  This is only used for
364          * debugging purposes.
365          *
366          */
367         struct sk_buff_head captured_packets;
368         struct net_device *netdev;
369         struct net_device_stats net_stats;
370 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
371         struct napi_struct napi;
372 #endif
373         struct timer_list watchdog;
374         u16 open_channels;
375 #if HZ > 100
376         unsigned long jiffies_at_last_poll;
377 #endif
378 };
379
380 struct wcdte_netdev_priv {
381         struct wcdte *wc;
382 };
383
384 static inline struct wcdte *
385 wcdte_from_netdev(struct net_device *netdev)
386 {
387         struct wcdte_netdev_priv *priv;
388         priv = netdev_priv(netdev);
389         return priv->wc;
390 }
391
392
393 static inline void wctc4xxp_set_ready(struct wcdte *wc)
394 {
395         set_bit(DTE_READY, &wc->flags);
396 }
397
398 static inline int wctc4xxp_is_ready(struct wcdte *wc)
399 {
400         return test_bit(DTE_READY, &wc->flags);
401 }
402
403 #define DTE_FORMAT_ULAW   0x00
404 #define DTE_FORMAT_G723_1 0x04
405 #define DTE_FORMAT_ALAW   0x08
406 #define DTE_FORMAT_G729A  0x12
407 #define DTE_FORMAT_UNDEF  0xFF
408
409 static inline u8 wctc4xxp_dahdifmt_to_dtefmt(unsigned int fmt)
410 {
411         u8 pt;
412
413         switch (fmt) {
414         case DAHDI_FORMAT_G723_1:
415                 pt = DTE_FORMAT_G723_1;
416                 break;
417         case DAHDI_FORMAT_ULAW:
418                 pt = DTE_FORMAT_ULAW;
419                 break;
420         case DAHDI_FORMAT_ALAW:
421                 pt = DTE_FORMAT_ALAW;
422                 break;
423         case DAHDI_FORMAT_G729A:
424                 pt = DTE_FORMAT_G729A;
425                 break;
426         default:
427                 pt = DTE_FORMAT_UNDEF;
428                 break;
429         }
430
431         return pt;
432 }
433
434 static struct sk_buff *
435 tcb_to_skb(struct net_device *netdev, const struct tcb *cmd)
436 {
437         struct sk_buff *skb;
438         skb = alloc_skb(cmd->data_len, in_atomic() ? GFP_ATOMIC : GFP_KERNEL);
439         if (skb) {
440                 skb->dev = netdev;
441                 skb_put(skb, cmd->data_len);
442                 memcpy(skb->data, cmd->data, cmd->data_len);
443                 skb->protocol = eth_type_trans(skb, netdev);
444         }
445         return skb;
446 }
447
448 /**
449  * wctc4xxp_skb_to_cmd - Convert a socket buffer (skb) to a tcb
450  * @wc: The transcoder that we're going to send this command to.
451  * @skb: socket buffer to convert.
452  *
453  */
454 static struct tcb *
455 wctc4xxp_skb_to_cmd(struct wcdte *wc, const struct sk_buff *skb)
456 {
457         const gfp_t alloc_flags = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
458         struct tcb *cmd;
459         cmd = __alloc_cmd(skb->len, alloc_flags, 0);
460         if (cmd) {
461                 int res;
462                 cmd->data_len = skb->len;
463                 res = skb_copy_bits(skb, 0, cmd->data, cmd->data_len);
464                 if (res) {
465                         dev_warn(&wc->pdev->dev,
466                            "Failed call to skb_copy_bits.\n");
467                         free_cmd(cmd);
468                         cmd = NULL;
469                 }
470         }
471         return cmd;
472 }
473
474 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0)
475 static void
476 wctc4xxp_net_set_multi(struct net_device *netdev)
477 {
478         struct wcdte *wc = wcdte_from_netdev(netdev);
479         DTE_DEBUG(DTE_DEBUG_GENERAL, "%s promiscuity:%d\n",
480            __func__, netdev->promiscuity);
481 }
482 #else
483 static void
484 wctc4xxp_set_rx_mode(struct net_device *netdev)
485 {
486         struct wcdte *wc = wcdte_from_netdev(netdev);
487         DTE_DEBUG(DTE_DEBUG_GENERAL, "%s promiscuity:%d\n",
488            __func__, netdev->promiscuity);
489 }
490 #endif
491
492 static int
493 wctc4xxp_net_up(struct net_device *netdev)
494 {
495         struct wcdte *wc = wcdte_from_netdev(netdev);
496         DTE_DEBUG(DTE_DEBUG_GENERAL, "%s\n", __func__);
497 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
498         netif_poll_enable(netdev);
499 #else
500         napi_enable(&wc->napi);
501 #endif
502         return 0;
503 }
504
505 static int
506 wctc4xxp_net_down(struct net_device *netdev)
507 {
508         struct wcdte *wc = wcdte_from_netdev(netdev);
509         DTE_DEBUG(DTE_DEBUG_GENERAL, "%s\n", __func__);
510 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
511         netif_poll_disable(netdev);
512 #else
513         napi_disable(&wc->napi);
514 #endif
515         return 0;
516 }
517
518 static void wctc4xxp_transmit_cmd(struct wcdte *, struct tcb *);
519
520 static int
521 wctc4xxp_net_hard_start_xmit(struct sk_buff *skb, struct net_device *netdev)
522 {
523         struct wcdte *wc = wcdte_from_netdev(netdev);
524         struct tcb *cmd;
525
526         /* We set DO_NOT_CAPTURE because this packet was already captured by
527          * in code higher up in the networking stack.  We don't want to
528          * capture it twice.
529          */
530         cmd = wctc4xxp_skb_to_cmd(wc, skb);
531         if (cmd) {
532                 cmd->flags |= DO_NOT_CAPTURE;
533                 wctc4xxp_transmit_cmd(wc, cmd);
534         }
535
536         dev_kfree_skb_any(skb);
537         return NETDEV_TX_OK;
538 }
539
540 static int
541 wctc4xxp_net_receive(struct wcdte *wc, int max)
542 {
543         int count = 0;
544         struct sk_buff *skb;
545         WARN_ON(0 == max);
546         while ((skb = skb_dequeue(&wc->captured_packets))) {
547                 netif_receive_skb(skb);
548                 if (++count >= max)
549                         break;
550         }
551         return count;
552 }
553
554 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
555 static int
556 wctc4xxp_poll(struct net_device *netdev, int *budget)
557 {
558         struct wcdte *wc = wcdte_from_netdev(netdev);
559         int count = 0;
560         int quota = min(netdev->quota, *budget);
561
562         count = wctc4xxp_net_receive(wc, quota);
563
564         *budget -=       count;
565         netdev->quota -= count;
566
567         if (!skb_queue_len(&wc->captured_packets)) {
568                 netif_rx_complete(netdev);
569                 return 0;
570         } else {
571                 return -1;
572         }
573 }
574 #else
575 static int
576 wctc4xxp_poll(struct napi_struct *napi, int budget)
577 {
578         struct wcdte *wc = container_of(napi, struct wcdte, napi);
579         int count;
580
581         count = wctc4xxp_net_receive(wc, budget);
582
583         if (!skb_queue_len(&wc->captured_packets)) {
584 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
585                 netif_rx_complete(wc->netdev, &wc->napi);
586 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
587                 netif_rx_complete(&wc->napi);
588 #else
589                 napi_complete(&wc->napi);
590 #endif
591         }
592         return count;
593 }
594 #endif
595
596 static struct net_device_stats *
597 wctc4xxp_net_get_stats(struct net_device *netdev)
598 {
599         struct wcdte *wc = wcdte_from_netdev(netdev);
600         return &wc->net_stats;
601 }
602
603 /* Wait until this device is put into promiscuous mode, or we timeout. */
604 static void
605 wctc4xxp_net_waitfor_promiscuous(struct wcdte *wc)
606 {
607         unsigned int seconds = 15;
608         unsigned long start = jiffies;
609         struct net_device *netdev = wc->netdev;
610
611         dev_info(&wc->pdev->dev,
612            "Waiting %d seconds for adapter to be placed in " \
613            "promiscuous mode for early trace.\n", seconds);
614
615         while (!netdev->promiscuity) {
616                 if (signal_pending(current)) {
617                         dev_info(&wc->pdev->dev,
618                            "Aborting wait due to signal.\n");
619                         break;
620                 }
621                 msleep(100);
622                 if (time_after(jiffies, start + (seconds * HZ))) {
623                         dev_info(&wc->pdev->dev,
624                            "Aborting wait due to timeout.\n");
625                         break;
626                 }
627         }
628 }
629
630 #ifdef HAVE_NET_DEVICE_OPS
631 static const struct net_device_ops wctc4xxp_netdev_ops = {
632 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0)
633         .ndo_set_multicast_list = &wctc4xxp_net_set_multi,
634 #else
635         .ndo_set_rx_mode = &wctc4xxp_set_rx_mode,
636 #endif
637         .ndo_open = &wctc4xxp_net_up,
638         .ndo_stop = &wctc4xxp_net_down,
639         .ndo_start_xmit = &wctc4xxp_net_hard_start_xmit,
640         .ndo_get_stats = &wctc4xxp_net_get_stats,
641 };
642 #endif
643
644 /**
645  * wctc4xxp_net_register - Register a new network interface.
646  * @wc: transcoder card to register the interface for.
647  *
648  * The network interface is primarily used for debugging in order to watch the
649  * traffic between the transcoder and the host.
650  *
651  */
652 static int
653 wctc4xxp_net_register(struct wcdte *wc)
654 {
655         int res;
656         struct net_device *netdev;
657         struct wcdte_netdev_priv *priv;
658         const char our_mac[] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
659
660         netdev = alloc_netdev(sizeof(*priv), wc->board_name, ether_setup);
661         if (!netdev)
662                 return -ENOMEM;
663         priv = netdev_priv(netdev);
664         priv->wc = wc;
665         memcpy(netdev->dev_addr, our_mac, sizeof(our_mac));
666
667 #       ifdef HAVE_NET_DEVICE_OPS
668         netdev->netdev_ops = &wctc4xxp_netdev_ops;
669 #       else
670         netdev->set_multicast_list = &wctc4xxp_net_set_multi;
671         netdev->open = &wctc4xxp_net_up;
672         netdev->stop = &wctc4xxp_net_down;
673         netdev->hard_start_xmit = &wctc4xxp_net_hard_start_xmit;
674         netdev->get_stats = &wctc4xxp_net_get_stats;
675 #       endif
676
677         netdev->promiscuity = 0;
678         netdev->flags |= IFF_NOARP;
679
680 #       if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
681         netdev->poll = &wctc4xxp_poll;
682         netdev->weight = 64;
683 #       else
684         netif_napi_add(netdev, &wc->napi, &wctc4xxp_poll, 64);
685 #       endif
686
687         res = register_netdev(netdev);
688         if (res) {
689                 dev_warn(&wc->pdev->dev,
690                    "Failed to register network device %s.\n",
691                    wc->board_name);
692                 goto error_sw;
693         }
694
695         wc->netdev = netdev;
696         skb_queue_head_init(&wc->captured_packets);
697
698         if (debug & DTE_DEBUG_NETWORK_EARLY)
699                 wctc4xxp_net_waitfor_promiscuous(wc);
700
701         dev_info(&wc->pdev->dev,
702            "Created network device %s for debug.\n", wc->board_name);
703         return 0;
704
705 error_sw:
706         if (netdev)
707                 free_netdev(netdev);
708         return res;
709 }
710
711 static void
712 wctc4xxp_net_unregister(struct wcdte *wc)
713 {
714         struct sk_buff *skb;
715
716         if (!wc->netdev)
717                 return;
718         unregister_netdev(wc->netdev);
719         while ((skb = skb_dequeue(&wc->captured_packets)))
720                 kfree_skb(skb);
721         free_netdev(wc->netdev);
722         wc->netdev = NULL;
723 }
724
725
726 /**
727  * wctc4xxp_net_capture_cmd - Send a tcb to the network stack.
728  * @wc: transcoder that received the command.
729  * @cmd: command to send to network stack.
730  *
731  */
732 static void
733 wctc4xxp_net_capture_cmd(struct wcdte *wc, const struct tcb *cmd)
734 {
735         struct sk_buff *skb;
736         struct net_device *netdev = wc->netdev;
737
738         if (!netdev)
739                 return;
740
741         /* No need to capture if there isn't anyone listening. */
742         if (!(netdev->flags & IFF_UP))
743                 return;
744
745         if (skb_queue_len(&wc->captured_packets) > MAX_CAPTURED_PACKETS) {
746                 WARN_ON_ONCE(1);
747                 return;
748         }
749
750         skb = tcb_to_skb(netdev, cmd);
751         if (!skb)
752                 return;
753
754         skb_queue_tail(&wc->captured_packets, skb);
755 #       if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
756         netif_rx_schedule(netdev);
757 #       elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
758         netif_rx_schedule(netdev, &wc->napi);
759 #       elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
760         netif_rx_schedule(&wc->napi);
761 #       else
762         napi_schedule(&wc->napi);
763 #       endif
764         return;
765 }
766
767
768 /*! In-memory structure shared by the host and the adapter. */
769 struct wctc4xxp_descriptor {
770         __le32 des0;
771         __le32 des1;
772         __le32 buffer1;
773         __le32 container; /* Unused */
774 } __attribute__((packed));
775
776 struct wctc4xxp_descriptor_ring {
777         /* Pointer to an array of descriptors to give to hardware. */
778         struct wctc4xxp_descriptor *desc;
779         /* Read completed buffers from the head. */
780         unsigned int    head;
781         /* Write ready buffers to the tail. */
782         unsigned int    tail;
783         /* Array to save the kernel virtual address of pending commands. */
784         struct tcb **pending;
785         /* PCI Bus address of the descriptor list. */
786         dma_addr_t      desc_dma;
787         /*! either DMA_FROM_DEVICE or DMA_TO_DEVICE */
788         unsigned int    direction;
789         /*! The number of buffers currently submitted to the hardware. */
790         unsigned int    count;
791         /*! The number of bytes to pad each descriptor for cache alignment. */
792         unsigned int    padding;
793         /*! Protects this structure from concurrent access. */
794         spinlock_t      lock;
795         /*! PCI device for the card associated with this ring. */
796         struct pci_dev  *pdev;
797         /*! The size of the dring. */
798         unsigned long size;
799         /*! Total number of packets completed. */
800         unsigned long packet_count;
801         /*! Total number of packets with errors. */
802         unsigned long packet_errors;
803 };
804
805 /**
806  * wctc4xxp_descriptor - Returns the desriptor at index.
807  * @dr: The descriptor ring we're using.
808  * @index: index of the descriptor we want.
809  *
810  * We need this function because we do not know what the padding on the
811  * descriptors will be.  Otherwise, we would just use an array.
812  */
813 static inline struct wctc4xxp_descriptor *
814 wctc4xxp_descriptor(struct wctc4xxp_descriptor_ring *dr, int index)
815 {
816         return (struct wctc4xxp_descriptor *)((u8 *)dr->desc +
817                 ((sizeof(*dr->desc) + dr->padding) * index));
818 }
819
820 static int
821 wctc4xxp_initialize_descriptor_ring(struct pci_dev *pdev,
822         struct wctc4xxp_descriptor_ring *dr, u32 des1, unsigned int direction,
823         unsigned long size)
824 {
825         int i;
826         const u32 END_OF_RING = 0x02000000;
827         u8 cache_line_size = 0;
828         int add_padding;
829         struct wctc4xxp_descriptor *d = NULL;
830
831         BUG_ON(!pdev);
832         BUG_ON(!dr);
833
834         if (pci_read_config_byte(pdev, 0x0c, &cache_line_size))
835                 return -EIO;
836
837         memset(dr, 0, sizeof(*dr));
838         dr->size = size;
839
840         /*
841          * Add some padding to each descriptor to ensure that they are
842          * aligned on host system cache-line boundaries, but only for the
843          * cache-line sizes that we support.
844          *
845          */
846         add_padding =   (0x08 == cache_line_size) ||
847                         (0x10 == cache_line_size) ||
848                         (0x20 == cache_line_size);
849         if (add_padding)
850                 dr->padding = (cache_line_size*sizeof(u32)) - sizeof(*d);
851
852         dr->pending = kmalloc(sizeof(struct tcb *) * dr->size, GFP_KERNEL);
853         if (!dr->pending)
854                 return -ENOMEM;
855
856         dr->desc = pci_alloc_consistent(pdev,
857                         (sizeof(*d)+dr->padding)*dr->size, &dr->desc_dma);
858         if (!dr->desc) {
859                 kfree(dr->pending);
860                 return -ENOMEM;
861         }
862
863         memset(dr->desc, 0, (sizeof(*d) + dr->padding) * dr->size);
864         for (i = 0; i < dr->size; ++i) {
865                 d = wctc4xxp_descriptor(dr, i);
866                 memset(d, 0, sizeof(*d));
867                 d->des1 = cpu_to_le32(des1);
868         }
869
870         d->des1 |= cpu_to_le32(END_OF_RING);
871         dr->direction = direction;
872         spin_lock_init(&dr->lock);
873         dr->pdev = pdev;
874         return 0;
875 }
876
877 #define OWN_BIT cpu_to_le32(0x80000000)
878 #define OWNED(_d_) (((_d_)->des0)&OWN_BIT)
879 #define SET_OWNED(_d_) do { wmb(); (_d_)->des0 |= OWN_BIT; wmb(); } while (0)
880
881 static const unsigned int BUFFER1_SIZE_MASK = 0x7ff;
882
883 static int
884 wctc4xxp_submit(struct wctc4xxp_descriptor_ring *dr, struct tcb *c)
885 {
886         volatile struct wctc4xxp_descriptor *d;
887         unsigned int len;
888         unsigned long flags;
889
890         WARN_ON(!c);
891         len = (c->data_len < MIN_PACKET_LEN) ? MIN_PACKET_LEN : c->data_len;
892         if (c->data_len > MAX_FRAME_SIZE) {
893                 WARN_ON_ONCE(!"Invalid command length passed\n");
894                 c->data_len = MAX_FRAME_SIZE;
895         }
896
897         spin_lock_irqsave(&dr->lock, flags);
898         d = wctc4xxp_descriptor(dr, dr->tail);
899         WARN_ON(!d);
900         if (d->buffer1) {
901                 spin_unlock_irqrestore(&dr->lock, flags);
902                 /* Do not overwrite a buffer that is still in progress. */
903                 return -EBUSY;
904         }
905         d->des1 &= cpu_to_le32(~(BUFFER1_SIZE_MASK));
906         d->des1 |= cpu_to_le32(len & BUFFER1_SIZE_MASK);
907         d->buffer1 = cpu_to_le32(pci_map_single(dr->pdev, c->data,
908                         SFRAME_SIZE, dr->direction));
909
910         SET_OWNED(d); /* That's it until the hardware is done with it. */
911         dr->pending[dr->tail] = c;
912         dr->tail = (dr->tail + 1) & (dr->size-1);
913         ++dr->count;
914         spin_unlock_irqrestore(&dr->lock, flags);
915         return 0;
916 }
917
918 static inline struct tcb*
919 wctc4xxp_retrieve(struct wctc4xxp_descriptor_ring *dr)
920 {
921         volatile struct wctc4xxp_descriptor *d;
922         struct tcb *c;
923         unsigned int head = dr->head;
924         unsigned long flags;
925         u32 des0;
926         spin_lock_irqsave(&dr->lock, flags);
927         d = wctc4xxp_descriptor(dr, head);
928         if (d->buffer1 && !OWNED(d)) {
929                 pci_unmap_single(dr->pdev, le32_to_cpu(d->buffer1),
930                         SFRAME_SIZE, dr->direction);
931                 c = dr->pending[head];
932                 WARN_ON(!c);
933                 dr->head = (++head) & (dr->size-1);
934                 d->buffer1 = 0;
935                 --dr->count;
936                 WARN_ON(!c);
937                 des0 = le32_to_cpu(d->des0);
938                 c->data_len = (des0 >> 16) & BUFFER1_SIZE_MASK;
939                 if (des0 & (1<<15)) {
940                         ++dr->packet_errors;
941                         /* The upper layers won't be able to do anything with
942                          * this packet. Free it up and log the error. */
943                         free_cmd(c);
944                         c = NULL;
945                 } else {
946                         ++dr->packet_count;
947                         WARN_ON(c->data_len > SFRAME_SIZE);
948                 }
949         } else {
950                 c = NULL;
951         }
952         spin_unlock_irqrestore(&dr->lock, flags);
953         return c;
954 }
955
956 static inline int wctc4xxp_getcount(struct wctc4xxp_descriptor_ring *dr)
957 {
958         int count;
959         unsigned long flags;
960         spin_lock_irqsave(&dr->lock, flags);
961         count = dr->count;
962         spin_unlock_irqrestore(&dr->lock, flags);
963         return count;
964 }
965
966 static inline int wctc4xxp_get_packet_count(struct wctc4xxp_descriptor_ring *dr)
967 {
968         unsigned long count;
969         unsigned long flags;
970         spin_lock_irqsave(&dr->lock, flags);
971         count = dr->packet_count;
972         spin_unlock_irqrestore(&dr->lock, flags);
973         return count;
974 }
975
976 static inline int
977 wctc4xxp_get_packet_errors(struct wctc4xxp_descriptor_ring *dr)
978 {
979         unsigned long count;
980         unsigned long flags;
981         spin_lock_irqsave(&dr->lock, flags);
982         count = dr->packet_errors;
983         spin_unlock_irqrestore(&dr->lock, flags);
984         return count;
985 }
986
987 static inline void
988 wctc4xxp_set_packet_count(struct wctc4xxp_descriptor_ring *dr,
989                           unsigned long count)
990 {
991         unsigned long flags;
992         spin_lock_irqsave(&dr->lock, flags);
993         dr->packet_count = count;
994         spin_unlock_irqrestore(&dr->lock, flags);
995 }
996
997 static inline void
998 __wctc4xxp_setctl(struct wcdte *wc, unsigned int addr, unsigned int val)
999 {
1000         writel(val, wc->iobase + addr);
1001         readl(wc->iobase + addr);
1002 }
1003
1004 static inline unsigned int
1005 __wctc4xxp_getctl(struct wcdte *wc, unsigned int addr)
1006 {
1007         return readl(wc->iobase + addr);
1008 }
1009
1010 static inline void
1011 wctc4xxp_setctl(struct wcdte *wc, unsigned int addr, unsigned int val)
1012 {
1013         unsigned long flags;
1014         spin_lock_irqsave(&wc->reglock, flags);
1015         __wctc4xxp_setctl(wc, addr, val);
1016         spin_unlock_irqrestore(&wc->reglock, flags);
1017 }
1018
1019 static inline void
1020 wctc4xxp_receive_demand_poll(struct wcdte *wc)
1021 {
1022         __wctc4xxp_setctl(wc, 0x0010, 0x00000000);
1023 }
1024
1025 static inline void
1026 wctc4xxp_transmit_demand_poll(struct wcdte *wc)
1027 {
1028         return;
1029 # if 0
1030         __wctc4xxp_setctl(wc, 0x0008, 0x00000000);
1031
1032         /* \todo Investigate why this register needs to be written twice in
1033          * order to get it to poll reliably.  So far, most of the problems
1034          * I've seen with timeouts had more to do with an untransmitted
1035          * packet sitting in the outbound descriptor list as opposed to any
1036          * problem with the dte firmware.
1037          */
1038         __wctc4xxp_setctl(wc, 0x0008, 0x00000000);
1039 #endif
1040 }
1041
1042 #define LENGTH_WITH_N_PARAMETERS(__n) (((__n) * sizeof(u16)) + \
1043                                         sizeof(struct csm_encaps_cmd))
1044
1045 static const u8 dst_mac[6] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
1046 static const u8 src_mac[6] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
1047
1048 static int wctc4xxp_transmit_cmd_and_wait(struct wcdte *wc, struct tcb *cmd);
1049
1050 static void
1051 setup_common_header(struct wcdte *wc, struct csm_encaps_hdr *hdr)
1052 {
1053         memcpy(hdr->ethhdr.h_dest, dst_mac, sizeof(dst_mac));
1054         memcpy(hdr->ethhdr.h_source, src_mac, sizeof(src_mac));
1055         hdr->ethhdr.h_proto = cpu_to_be16(ETH_P_CSM_ENCAPS);
1056 }
1057
1058 static void
1059 setup_supervisor_header(struct wcdte *wc, struct csm_encaps_hdr *hdr)
1060 {
1061         setup_common_header(wc, hdr);
1062
1063         hdr->op_code = cpu_to_be16(CONTROL_PACKET_OPCODE);
1064         hdr->control = LITTLE_ENDIAN;
1065         hdr->seq_num = (wc->seq_num++)&0xf;
1066         hdr->channel = cpu_to_be16(SUPERVISOR_CHANNEL);
1067 }
1068
1069 static void
1070 create_supervisor_cmd(struct wcdte *wc, struct tcb *cmd, u8 type, u8 class,
1071         u16 function, const u16 *parameters, const int num_parameters)
1072 {
1073         struct csm_encaps_hdr *hdr = cmd->data;
1074         int i;
1075
1076         if (cmd->response) {
1077                 free_cmd(cmd->response);
1078                 cmd->response = NULL;
1079         }
1080
1081         setup_supervisor_header(wc, hdr);
1082
1083         hdr->cmd.length =       LENGTH_WITH_N_PARAMETERS(num_parameters);
1084         hdr->cmd.index =        0;
1085         hdr->cmd.type =         type;
1086         hdr->cmd.class =        class;
1087         hdr->cmd.function =     cpu_to_le16(function);
1088         hdr->cmd.reserved =     0;
1089
1090         for (i = 0; i < num_parameters; ++i)
1091                 hdr->cmd.params[i] = cpu_to_le16(parameters[i]);
1092
1093         cmd->flags = WAIT_FOR_RESPONSE;
1094         cmd->data_len = sizeof(struct csm_encaps_hdr) -
1095                         sizeof(struct csm_encaps_cmd) +
1096                         hdr->cmd.length;
1097         cmd->cpvt = NULL;
1098 }
1099
1100 static void
1101 setup_channel_header(struct channel_pvt *pvt, struct tcb *cmd)
1102 {
1103         struct csm_encaps_hdr *hdr = cmd->data;
1104
1105         if (cmd->response) {
1106                 free_cmd(cmd->response);
1107                 cmd->response = NULL;
1108         }
1109
1110         setup_common_header(pvt->wc, hdr);
1111         hdr->op_code = cpu_to_be16(CONTROL_PACKET_OPCODE);
1112         hdr->seq_num = (pvt->cmd_seqno++)&0xf;
1113         hdr->channel = cpu_to_be16(pvt->chan_in_num);
1114
1115         cmd->flags = WAIT_FOR_RESPONSE;
1116         cmd->data_len = sizeof(struct csm_encaps_hdr) -
1117                                 sizeof(struct csm_encaps_cmd);
1118         cmd->cpvt = pvt;
1119         cmd->next_index = 0;
1120 }
1121
1122
1123 static void
1124 append_channel_cmd(struct tcb *cmd, u8 type, u8 class, u16 function,
1125                    const u16 *parameters, int num_parameters)
1126 {
1127         int i;
1128         struct csm_encaps_cmd *csm_cmd = cmd->data + cmd->data_len;
1129
1130         csm_cmd->length =       LENGTH_WITH_N_PARAMETERS(num_parameters);
1131         csm_cmd->index =        cmd->next_index++;
1132         csm_cmd->type =         type;
1133         csm_cmd->class =        class;
1134         csm_cmd->function =     cpu_to_le16(function);
1135         csm_cmd->reserved =     0;
1136
1137         for (i = 0; i < num_parameters; ++i)
1138                 csm_cmd->params[i] = cpu_to_le16(parameters[i]);
1139
1140         cmd->data_len += csm_cmd->length;
1141         /* Pad it out to a DW boundary */
1142         if (cmd->data_len % 4)
1143                 cmd->data_len += 4 - (cmd->data_len % 4);
1144         WARN_ON(cmd->data_len >= SFRAME_SIZE);
1145 }
1146
1147 static void
1148 create_channel_cmd(struct channel_pvt *pvt, struct tcb *cmd, u8 type, u8 class,
1149         u16 function, const u16 *parameters, int num_parameters)
1150 {
1151         setup_channel_header(pvt, cmd);
1152         append_channel_cmd(cmd, type, class, function, parameters,
1153                            num_parameters);
1154 }
1155
1156 static int
1157 send_create_channel_cmd(struct wcdte *wc, struct tcb *cmd, u16 timeslot,
1158         u16 *channel_number)
1159 {
1160         int res;
1161         const u16 parameters[] = {0x0002, timeslot};
1162
1163         create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1164                 CONFIG_DEVICE_CLASS, SUPVSR_CREATE_CHANNEL,
1165                 parameters, ARRAY_SIZE(parameters));
1166
1167         res = wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1168         if (res)
1169                 return res;
1170
1171         if (0x0000 != response_header(cmd)->cmd.params[0]) {
1172                 if (printk_ratelimit()) {
1173                         dev_warn(&wc->pdev->dev,
1174                                  "Failed to create channel in timeslot " \
1175                                  "%d.  Response from DTE was (%04x).\n",
1176                                  timeslot, response_header(cmd)->cmd.params[0]);
1177                 }
1178                 free_cmd(cmd->response);
1179                 cmd->response = NULL;
1180                 return -EIO;
1181         }
1182
1183         *channel_number = le16_to_cpu(response_header(cmd)->cmd.params[1]);
1184         free_cmd(cmd->response);
1185         cmd->response = NULL;
1186         return 0;
1187 }
1188
1189 static int
1190 send_set_arm_clk_cmd(struct wcdte *wc, struct tcb *cmd)
1191 {
1192         const u16 parameters[] = {0x012c, 0x0000};
1193         create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1194                 CONFIG_DEVICE_CLASS, 0x0411, parameters,
1195                 ARRAY_SIZE(parameters));
1196         return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1197 }
1198
1199 static int
1200 send_set_spu_clk_cmd(struct wcdte *wc, struct tcb *cmd)
1201 {
1202         const u16 parameters[] = {0x012c, 0x0000};
1203         create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1204                 CONFIG_DEVICE_CLASS, 0x0412, parameters,
1205                 ARRAY_SIZE(parameters));
1206         return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1207 }
1208
1209 static int
1210 send_tdm_select_bus_mode_cmd(struct wcdte *wc, struct tcb *cmd)
1211 {
1212         const u16 parameters[] = {0x0004};
1213         create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1214                 CONFIG_DEVICE_CLASS, 0x0417, parameters,
1215                 ARRAY_SIZE(parameters));
1216         return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1217 }
1218
1219 static int
1220 send_set_eth_header_cmd(struct wcdte *wc, struct tcb *cmd,
1221         const u8 *host_mac, const u8 *assigned_mac)
1222 {
1223         u16 parameters[8];
1224         u16 *part;
1225
1226         parameters[0] = 0x0001;
1227         part = (u16 *)host_mac;
1228         parameters[1] = part[0];
1229         parameters[2] = part[1];
1230         parameters[3] = part[2];
1231         part = (u16 *)assigned_mac;
1232         parameters[4] = part[0];
1233         parameters[5] = part[1];
1234         parameters[6] = part[2];
1235         parameters[7] = 0x0008;
1236
1237         create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1238                 CONFIG_DEVICE_CLASS, 0x0100, parameters,
1239                 ARRAY_SIZE(parameters));
1240         return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1241 }
1242
1243 static int
1244 send_supvsr_setup_tdm_parms(struct wcdte *wc, struct tcb *cmd,
1245         u8 bus_number)
1246 {
1247         const u16 parameters[] = {0x8380, 0x0c00, 0, (bus_number << 2)&0xc};
1248         create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1249                 CONFIG_DEVICE_CLASS, 0x0407, parameters,
1250                 ARRAY_SIZE(parameters));
1251         return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1252 }
1253
1254 static int
1255 send_ip_service_config_cmd(struct wcdte *wc, struct tcb *cmd)
1256 {
1257         const u16 parameters[] = {0x0200};
1258         create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1259                 CONFIG_DEVICE_CLASS, 0x0302, parameters,
1260                 ARRAY_SIZE(parameters));
1261         return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1262 }
1263
1264 static int
1265 send_arp_service_config_cmd(struct wcdte *wc, struct tcb *cmd)
1266 {
1267         const u16 parameters[] = {0x0001};
1268         create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1269                 CONFIG_DEVICE_CLASS, 0x0105, parameters,
1270                 ARRAY_SIZE(parameters));
1271         return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1272 }
1273
1274 static int
1275 send_icmp_service_config_cmd(struct wcdte *wc, struct tcb *cmd)
1276 {
1277         const u16 parameters[] = {0xff01};
1278         create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1279                 CONFIG_DEVICE_CLASS, 0x0304, parameters,
1280                 ARRAY_SIZE(parameters));
1281         return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1282 }
1283
1284 static int
1285 send_device_set_country_code_cmd(struct wcdte *wc, struct tcb *cmd)
1286 {
1287         const u16 parameters[] = {0x0000};
1288         create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1289                 CONFIG_DEVICE_CLASS, 0x041b, parameters,
1290                 ARRAY_SIZE(parameters));
1291         return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1292 }
1293
1294 static int
1295 send_spu_features_control_cmd(struct wcdte *wc, struct tcb *cmd, u16 options)
1296 {
1297         const u16 parameters[] = {options};
1298         create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1299                 CONFIG_DEVICE_CLASS, 0x0013, parameters,
1300                 ARRAY_SIZE(parameters));
1301         return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1302 }
1303
1304 /* Allows sending more than one CSM_ENCAPS packet in a single ethernet frame. */
1305 static int send_csme_multi_cmd(struct wcdte *wc, struct tcb *cmd)
1306 {
1307         const u16 parameters[] = {0x1};
1308         create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1309                 CONFIG_DEVICE_CLASS, 0x010a, parameters,
1310                 ARRAY_SIZE(parameters));
1311         return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1312 }
1313
1314 static int
1315 send_tdm_opt_cmd(struct wcdte *wc, struct tcb *cmd)
1316 {
1317         const u16 parameters[] = {0x0000};
1318         create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1319                 CONFIG_DEVICE_CLASS, 0x0435, parameters,
1320                 ARRAY_SIZE(parameters));
1321         return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1322 }
1323
1324 static int
1325 send_destroy_channel_cmd(struct wcdte *wc, struct tcb *cmd, u16 channel)
1326 {
1327         int res;
1328         u16 result;
1329         const u16 parameters[] = {channel};
1330         create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1331                 CONFIG_DEVICE_CLASS, 0x0011, parameters,
1332                 ARRAY_SIZE(parameters));
1333         res = wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1334         if (res)
1335                 return res;
1336         /* Let's check the response for any error codes.... */
1337         result = le16_to_cpu(response_header(cmd)->cmd.params[0]);
1338         if (0x0000 != result) {
1339                 dev_err(&wc->pdev->dev,
1340                         "Failed to destroy channel %04d (%04x)\n",
1341                         channel, result);
1342                 return -EIO;
1343         }
1344         return 0;
1345 }
1346
1347 static void
1348 append_set_ip_hdr_channel_cmd(struct tcb *cmd)
1349 {
1350         const u16 parameters[] = {0, 0x0045, 0, 0, 0x0040, 0x1180, 0,
1351                 0xa8c0, 0x0309, 0xa8c0, 0x0309,
1352                 swab16(cmd->cpvt->timeslot_out_num + 0x5000),
1353                 swab16(cmd->cpvt->timeslot_in_num + 0x5000),
1354                 0, 0};
1355         append_channel_cmd(cmd, CONFIG_CHANGE_TYPE, CONFIG_CHANNEL_CLASS,
1356                 0x9000, parameters, ARRAY_SIZE(parameters));
1357 }
1358
1359 static void
1360 append_voip_vceopt_cmd(struct tcb *cmd, u16 length)
1361 {
1362         const u16 parameters[] = {((length << 8)|0x21), 0x1c00,
1363                                   0x0004, 0, 0};
1364         append_channel_cmd(cmd, CONFIG_CHANGE_TYPE, CONFIG_CHANNEL_CLASS,
1365                 0x8001, parameters, ARRAY_SIZE(parameters));
1366 }
1367
1368 static void
1369 append_voip_tonectl_cmd(struct tcb *cmd)
1370 {
1371         const u16 parameters[] = {0};
1372         append_channel_cmd(cmd, CONFIG_CHANGE_TYPE, CONFIG_CHANNEL_CLASS,
1373                 0x805b, parameters, ARRAY_SIZE(parameters));
1374 }
1375
1376 static void
1377 append_voip_dtmfopt_cmd(struct tcb *cmd)
1378 {
1379         const u16 parameters[] = {0x0008};
1380         append_channel_cmd(cmd, CONFIG_CHANGE_TYPE, CONFIG_CHANNEL_CLASS,
1381                 0x8002, parameters, ARRAY_SIZE(parameters));
1382 }
1383
1384 static void
1385 append_voip_indctrl_cmd(struct tcb *cmd)
1386 {
1387         const u16 parameters[] = {0x0007};
1388         append_channel_cmd(cmd, CONFIG_CHANGE_TYPE, CONFIG_CHANNEL_CLASS,
1389                 0x8084, parameters, ARRAY_SIZE(parameters));
1390 }
1391
1392 static void
1393 send_voip_vopena_cmd(struct channel_pvt *pvt, struct tcb *cmd, u8 format)
1394 {
1395         const u16 parameters[] = {1, ((format<<8)|0x80), 0, 0, 0,
1396                 0x3412, 0x7856};
1397         create_channel_cmd(pvt, cmd, CONFIG_CHANGE_TYPE, CONFIG_CHANNEL_CLASS,
1398                 0x8000, parameters, ARRAY_SIZE(parameters));
1399         wctc4xxp_transmit_cmd(pvt->wc, cmd);
1400 }
1401
1402 static int
1403 send_voip_vopena_close_cmd(struct channel_pvt *pvt, struct tcb *cmd)
1404 {
1405         int res;
1406         const u16 parameters[] = {0};
1407         create_channel_cmd(pvt, cmd, CONFIG_CHANGE_TYPE, CONFIG_CHANNEL_CLASS,
1408                 0x8000, parameters, ARRAY_SIZE(parameters));
1409         res = wctc4xxp_transmit_cmd_and_wait(pvt->wc, cmd);
1410         if (res)
1411                 return res;
1412         /* Let's check the response for any error codes.... */
1413         if (0x0000 != response_header(cmd)->cmd.params[0]) {
1414                 WARN_ON(1);
1415                 return -EIO;
1416         }
1417         return 0;
1418 }
1419
1420 static int
1421 send_ip_options_cmd(struct wcdte *wc, struct tcb *cmd)
1422 {
1423         const u16 parameters[] = {0x0002};
1424         create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1425                 CONFIG_DEVICE_CLASS, 0x0306, parameters,
1426                 ARRAY_SIZE(parameters));
1427         return wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1428 }
1429
1430 static int
1431 _send_trans_connect_cmd(struct wcdte *wc, struct tcb *cmd, u16 enable, u16
1432         encoder_channel, u16 decoder_channel, u16 encoder_format,
1433         u16 decoder_format)
1434 {
1435         int res;
1436         const u16 parameters[] = {enable, encoder_channel, encoder_format,
1437                 decoder_channel, decoder_format};
1438         create_supervisor_cmd(wc, cmd, CONFIG_CHANGE_TYPE,
1439                 CONFIG_DEVICE_CLASS, 0x9322, parameters,
1440                 ARRAY_SIZE(parameters));
1441         res = wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1442         if (res)
1443                 return res;
1444
1445         /* Let's check the response for any error codes.... */
1446         if (0x0000 != response_header(cmd)->cmd.params[0]) {
1447                 WARN_ON(1);
1448                 return -EIO;
1449         }
1450         return 0;
1451 }
1452
1453 static int
1454 send_trans_connect_cmd(struct wcdte *wc, struct tcb *cmd, const u16
1455         encoder_channel, const u16 decoder_channel, const u16 encoder_format,
1456         const u16 decoder_format)
1457 {
1458         return _send_trans_connect_cmd(wc, cmd, 1, encoder_channel,
1459                 decoder_channel, encoder_format, decoder_format);
1460 }
1461
1462 static int
1463 send_trans_disconnect_cmd(struct wcdte *wc, struct tcb *cmd, const u16
1464         encoder_channel, const u16 decoder_channel, const u16 encoder_format,
1465         const u16 decoder_format)
1466 {
1467         return _send_trans_connect_cmd(wc, cmd, 0, encoder_channel,
1468                 decoder_channel, encoder_format, decoder_format);
1469 }
1470
1471 static int
1472 send_voip_vceinfo_cmd(struct channel_pvt *pvt, struct tcb *cmd)
1473 {
1474         int res;
1475         const u16 parameters[] = {0};
1476         static const int CONFIG_CHANNEL_STATS_CLASS = 1;
1477         create_channel_cmd(pvt, cmd,
1478                 VOIP_VCEINFO_TYPE, CONFIG_CHANNEL_STATS_CLASS,
1479                 0x0000, parameters, 0);
1480         res = wctc4xxp_transmit_cmd_and_wait(pvt->wc, cmd);
1481         return res;
1482 }
1483
1484 static int
1485 send_eth_statistics_cmd(struct wcdte *wc, struct tcb *cmd)
1486 {
1487         int res;
1488         const u16 parameters[] = {0};
1489
1490         create_supervisor_cmd(wc, cmd, 0x00, 0x05, 0x0000,
1491                 parameters, ARRAY_SIZE(parameters));
1492         res = wctc4xxp_transmit_cmd_and_wait(wc, cmd);
1493         if (res)
1494                 return -EIO;
1495         if (0x0000 != response_header(cmd)->cmd.params[0]) {
1496                 dev_info(&wc->pdev->dev,
1497                          "Failed to get ethernet stats: 0x%04x\n",
1498                          response_header(cmd)->cmd.params[0]);
1499                 res = -EIO;
1500         }
1501         return res;
1502 }
1503
1504 static void wctc4xxp_match_packet_counts(struct wcdte *wc)
1505 {
1506         struct tcb *cmd  = alloc_cmd(SFRAME_SIZE);
1507         int res;
1508         u32 *parms;
1509
1510         res = send_eth_statistics_cmd(wc, cmd);
1511         if (0 == res) {
1512                 parms = (u32 *)(&response_header(cmd)->cmd.params[0]);
1513                 wctc4xxp_set_packet_count(wc->rxd, parms[1]);
1514                 wctc4xxp_set_packet_count(wc->txd, parms[2]);
1515         }
1516         free_cmd(cmd);
1517 }
1518
1519 static inline u32 wctc4xxp_bytes_to_samples(u32 fmt, size_t count)
1520 {
1521         switch (fmt) {
1522         case DAHDI_FORMAT_G723_1:
1523                 return count * (G723_SAMPLES/G723_5K_BYTES);
1524         case DAHDI_FORMAT_ULAW:
1525         case DAHDI_FORMAT_ALAW:
1526                 return count;
1527         case DAHDI_FORMAT_G729A:
1528                 return count * (G729_SAMPLES/G729_BYTES);
1529         default:
1530                 WARN_ON(1);
1531                 return 0;
1532         }
1533 }
1534
1535 static struct tcb *
1536 wctc4xxp_create_rtp_cmd(struct wcdte *wc, struct dahdi_transcoder_channel *dtc,
1537         size_t inbytes)
1538 {
1539         struct channel_pvt *cpvt = dtc->pvt;
1540         struct rtp_packet *packet;
1541         struct tcb *cmd;
1542
1543         cmd = alloc_cmd(sizeof(*packet) + inbytes);
1544         if (!cmd)
1545                 return NULL;
1546
1547         cmd->cpvt = cpvt;
1548         packet = cmd->data;
1549
1550         BUG_ON(cmd->data_len < sizeof(*packet));
1551
1552         /* setup the ethernet header */
1553         memcpy(packet->ethhdr.h_dest, dst_mac, sizeof(dst_mac));
1554         memcpy(packet->ethhdr.h_source, src_mac, sizeof(src_mac));
1555         packet->ethhdr.h_proto = cpu_to_be16(ETH_P_IP);
1556
1557         /* setup the IP header */
1558         packet->iphdr.ihl =             5;
1559         packet->iphdr.version =         4;
1560         packet->iphdr.tos =             0;
1561         packet->iphdr.tot_len =         cpu_to_be16(inbytes+40);
1562         packet->iphdr.id =              0;
1563         packet->iphdr.frag_off =        cpu_to_be16(0x4000);
1564         packet->iphdr.ttl =             64;
1565         packet->iphdr.protocol =        0x11; /* UDP */
1566         packet->iphdr.check =           0;
1567         packet->iphdr.saddr =           cpu_to_be32(0xc0a80903);
1568         packet->iphdr.daddr =           cpu_to_be32(0xc0a80903);
1569
1570         packet->iphdr.check =   ip_fast_csum((void *)&packet->iphdr,
1571                                         packet->iphdr.ihl);
1572
1573         /* setup the UDP header */
1574         packet->udphdr.source = cpu_to_be16(cpvt->timeslot_out_num + 0x5000);
1575         packet->udphdr.dest =   cpu_to_be16(cpvt->timeslot_in_num + 0x5000);
1576         packet->udphdr.len  =   cpu_to_be16(inbytes + sizeof(struct rtphdr) +
1577                                         sizeof(struct udphdr));
1578         packet->udphdr.check =  0;
1579
1580         /* Setup the RTP header */
1581         packet->rtphdr.ver =        2;
1582         packet->rtphdr.padding =    0;
1583         packet->rtphdr.extension =  0;
1584         packet->rtphdr.csrc_count = 0;
1585         packet->rtphdr.marker =     0;
1586         packet->rtphdr.type =       wctc4xxp_dahdifmt_to_dtefmt(dtc->srcfmt);
1587         packet->rtphdr.seqno =      cpu_to_be16(cpvt->seqno);
1588         packet->rtphdr.timestamp =  cpu_to_be32(cpvt->last_timestamp);
1589         packet->rtphdr.ssrc =       cpu_to_be32(cpvt->ssrc);
1590
1591         cpvt->last_timestamp +=     wctc4xxp_bytes_to_samples(dtc->srcfmt,
1592                                                               inbytes);
1593
1594         WARN_ON(cmd->data_len > SFRAME_SIZE);
1595         return cmd;
1596 }
1597 static void
1598 wctc4xxp_cleanup_descriptor_ring(struct wctc4xxp_descriptor_ring *dr)
1599 {
1600         int i;
1601         struct wctc4xxp_descriptor *d;
1602
1603         if (!dr || !dr->desc)
1604                 return;
1605
1606         for (i = 0; i < dr->size; ++i) {
1607                 d = wctc4xxp_descriptor(dr, i);
1608                 if (d->buffer1) {
1609                         pci_unmap_single(dr->pdev, d->buffer1,
1610                                 SFRAME_SIZE, dr->direction);
1611                         d->buffer1 = 0;
1612                         /* Commands will also be sitting on the waiting for
1613                          * response list, so we want to make sure to delete
1614                          * them from that list as well. */
1615                         list_del_init(&(dr->pending[i])->node);
1616                         free_cmd(dr->pending[i]);
1617                         dr->pending[i] = NULL;
1618                 }
1619         }
1620         dr->head = 0;
1621         dr->tail = 0;
1622         dr->count = 0;
1623         pci_free_consistent(dr->pdev, (sizeof(*d)+dr->padding) * dr->size,
1624                 dr->desc, dr->desc_dma);
1625         kfree(dr->pending);
1626 }
1627
1628 static void wctc4xxp_timeout_all_commands(struct wcdte *wc)
1629 {
1630         struct tcb *cmd;
1631         struct tcb *temp;
1632         unsigned long flags;
1633         LIST_HEAD(local_list);
1634
1635         spin_lock_irqsave(&wc->cmd_list_lock, flags);
1636         list_splice_init(&wc->waiting_for_response_list, &local_list);
1637         list_splice_init(&wc->cmd_list, &local_list);
1638         spin_unlock_irqrestore(&wc->cmd_list_lock, flags);
1639
1640         list_for_each_entry_safe(cmd, temp, &local_list, node) {
1641                 list_del_init(&cmd->node);
1642                 if (cmd->complete) {
1643                         cmd->flags |= DTE_CMD_TIMEOUT;
1644                         complete(cmd->complete);
1645                 } else {
1646                         free_cmd(cmd);
1647                 }
1648         }
1649 }
1650
1651 static void wctc4xxp_cleanup_command_list(struct wcdte *wc)
1652 {
1653         struct tcb *cmd;
1654         unsigned long flags;
1655         LIST_HEAD(local_list);
1656
1657         spin_lock_irqsave(&wc->cmd_list_lock, flags);
1658         list_splice_init(&wc->cmd_list, &local_list);
1659         list_splice_init(&wc->waiting_for_response_list, &local_list);
1660         list_splice_init(&wc->rx_list, &local_list);
1661         spin_unlock_irqrestore(&wc->cmd_list_lock, flags);
1662
1663         while (!list_empty(&local_list)) {
1664                 cmd = list_entry(local_list.next, struct tcb, node);
1665                 list_del_init(&cmd->node);
1666                 free_cmd(cmd);
1667         }
1668 }
1669
1670 static inline bool is_rtp_packet(const struct tcb *cmd)
1671 {
1672         const struct ethhdr *ethhdr = cmd->data;
1673         return (cpu_to_be16(ETH_P_IP) == ethhdr->h_proto);
1674 }
1675
1676 static void
1677 wctc4xxp_transmit_cmd(struct wcdte *wc, struct tcb *cmd)
1678 {
1679         int res;
1680         unsigned long flags;
1681
1682         /* If we're shutdown all commands will timeout. Just complete the
1683          * command here with the timeout flag */
1684         if (unlikely(test_bit(DTE_SHUTDOWN, &wc->flags))) {
1685                 if (cmd->complete) {
1686                         cmd->flags |= DTE_CMD_TIMEOUT;
1687                         list_del_init(&cmd->node);
1688                         complete(cmd->complete);
1689                 } else {
1690                         list_del(&cmd->node);
1691                         free_cmd(cmd);
1692                 }
1693                 return;
1694         }
1695
1696         if (cmd->data_len < MIN_PACKET_LEN) {
1697                 memset((u8 *)(cmd->data) + cmd->data_len, 0,
1698                        MIN_PACKET_LEN-cmd->data_len);
1699                 cmd->data_len = MIN_PACKET_LEN;
1700         }
1701         WARN_ON(cmd->response);
1702         WARN_ON(cmd->flags & TX_COMPLETE);
1703         cmd->timeout = jiffies + HZ/4;
1704
1705         spin_lock_irqsave(&wc->cmd_list_lock, flags);
1706         if (cmd->flags & (WAIT_FOR_ACK | WAIT_FOR_RESPONSE)) {
1707                 if (cmd->flags & WAIT_FOR_RESPONSE) {
1708                         /* We don't need both an ACK and a response.  Let's
1709                          * tell the DTE not to generate an ACK, and we'll just
1710                          * retry if we do not get the response within the
1711                          * timeout period. */
1712                         struct csm_encaps_hdr *hdr = cmd->data;
1713                         hdr->control |= SUPPRESS_ACK;
1714                 }
1715                 WARN_ON(!list_empty(&cmd->node));
1716                 list_add_tail(&cmd->node, &wc->waiting_for_response_list);
1717                 mod_timer(&wc->watchdog, jiffies + HZ/2);
1718         }
1719         if (!list_empty(&wc->cmd_list)) {
1720                 if (is_rtp_packet(cmd))
1721                         list_add_tail(&cmd->node, &wc->cmd_list);
1722                 else
1723                         list_move(&cmd->node, &wc->cmd_list);
1724                 spin_unlock_irqrestore(&wc->cmd_list_lock, flags);
1725                 return;
1726         }
1727         res = wctc4xxp_submit(wc->txd, cmd);
1728         if (-EBUSY == res) {
1729                 /* Looks like we're out of room in the descriptor
1730                  * ring.  We'll add this command to the pending list
1731                  * and the interrupt service routine will pull from
1732                  * this list as it clears up room in the descriptor
1733                  * ring. */
1734                 list_move_tail(&cmd->node, &wc->cmd_list);
1735         } else if (0 == res) {
1736                 if (!(cmd->flags & DO_NOT_CAPTURE))
1737                         wctc4xxp_net_capture_cmd(wc, cmd);
1738                 wctc4xxp_transmit_demand_poll(wc);
1739         } else {
1740                 /* Unknown return value... */
1741                 WARN_ON(1);
1742         }
1743         spin_unlock_irqrestore(&wc->cmd_list_lock, flags);
1744 }
1745
1746 static int
1747 wctc4xxp_transmit_cmd_and_wait(struct wcdte *wc, struct tcb *cmd)
1748 {
1749         DECLARE_COMPLETION_ONSTACK(done);
1750         cmd->complete = &done;
1751         wctc4xxp_transmit_cmd(wc, cmd);
1752         wait_for_completion(&done);
1753         cmd->complete = NULL;
1754         if (cmd->flags & DTE_CMD_TIMEOUT) {
1755                 DTE_DEBUG(DTE_DEBUG_GENERAL, "Timeout waiting for command.\n");
1756                 return -EIO;
1757         }
1758         return 0;
1759 }
1760
1761 static int wctc4xxp_create_channel_pair(struct wcdte *wc,
1762                 struct channel_pvt *cpvt, u8 simple, u8 complicated);
1763 static int wctc4xxp_destroy_channel_pair(struct wcdte *wc,
1764                 struct channel_pvt *cpvt);
1765
1766 static void
1767 wctc4xxp_init_state(struct channel_pvt *cpvt, int encoder,
1768         unsigned int channel, struct wcdte *wc)
1769 {
1770         memset(cpvt, 0, sizeof(*cpvt));
1771         cpvt->encoder = encoder;
1772         cpvt->wc = wc;
1773         cpvt->chan_in_num = INVALID;
1774         cpvt->chan_out_num = INVALID;
1775         cpvt->ssrc = 0x78;
1776         cpvt->timeslot_in_num = channel*2;
1777         cpvt->timeslot_out_num = channel*2;
1778         cpvt->last_rx_seq_num = 0xff;
1779         if (encoder)
1780                 ++cpvt->timeslot_out_num;
1781         else
1782                 ++cpvt->timeslot_in_num;
1783         spin_lock_init(&cpvt->lock);
1784         INIT_LIST_HEAD(&cpvt->rx_queue);
1785 }
1786
1787 static unsigned int
1788 wctc4xxp_getctl(struct wcdte *wc, unsigned int addr)
1789 {
1790         unsigned int val;
1791         unsigned long flags;
1792         spin_lock_irqsave(&wc->reglock, flags);
1793         val = __wctc4xxp_getctl(wc, addr);
1794         spin_unlock_irqrestore(&wc->reglock, flags);
1795         return val;
1796 }
1797
1798 static void
1799 wctc4xxp_cleanup_channel_private(struct wcdte *wc,
1800         struct dahdi_transcoder_channel *dtc)
1801 {
1802         struct tcb *cmd, *temp;
1803         struct channel_pvt *cpvt = dtc->pvt;
1804         unsigned long flags;
1805         LIST_HEAD(local_list);
1806
1807         /* Once we cleanup this channel, we do not want any queued packets
1808          * waiting to be transmitted. Anything on the hardware descriptor ring
1809          * will be flushed by the csm_encaps command to shutdown the channel. */
1810         spin_lock_irqsave(&wc->cmd_list_lock, flags);
1811         list_for_each_entry_safe(cmd, temp, &wc->cmd_list, node) {
1812                 if (cmd->cpvt == cpvt)
1813                         list_move(&cmd->node, &local_list);
1814         }
1815         spin_unlock_irqrestore(&wc->cmd_list_lock, flags);
1816
1817         spin_lock_irqsave(&cpvt->lock, flags);
1818         list_splice_init(&cpvt->rx_queue, &local_list);
1819         dahdi_tc_clear_data_waiting(dtc);
1820         cpvt->samples_in_flight = 0;
1821         spin_unlock_irqrestore(&cpvt->lock, flags);
1822
1823         memset(&cpvt->stats, 0, sizeof(cpvt->stats));
1824         list_for_each_entry_safe(cmd, temp, &local_list, node) {
1825                 list_del(&cmd->node);
1826                 free_cmd(cmd);
1827         }
1828 }
1829
1830 static void
1831 wctc4xxp_mark_channel_complement_built(struct wcdte *wc,
1832         struct dahdi_transcoder_channel *dtc)
1833 {
1834         int index;
1835         struct channel_pvt *cpvt = dtc->pvt;
1836         struct dahdi_transcoder_channel *compl_dtc;
1837         struct channel_pvt *compl_cpvt;
1838
1839         BUG_ON(!cpvt);
1840         index = cpvt->timeslot_in_num/2;
1841         BUG_ON(index >= wc->numchannels);
1842         if (cpvt->encoder)
1843                 compl_dtc = &(wc->udecode->channels[index]);
1844         else
1845                 compl_dtc = &(wc->uencode->channels[index]);
1846
1847         /* It shouldn't already have been built... */
1848         WARN_ON(dahdi_tc_is_built(compl_dtc));
1849         compl_dtc->built_fmts = dtc->dstfmt | dtc->srcfmt;
1850         compl_cpvt = compl_dtc->pvt;
1851         DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP,
1852                 "dtc: %p is the complement to %p\n", compl_dtc, dtc);
1853         compl_cpvt->chan_in_num = cpvt->chan_out_num;
1854         compl_cpvt->chan_out_num = cpvt->chan_in_num;
1855         dahdi_tc_set_built(compl_dtc);
1856         wctc4xxp_cleanup_channel_private(wc, dtc);
1857 }
1858
1859 static int
1860 do_channel_allocate(struct dahdi_transcoder_channel *dtc)
1861 {
1862         struct channel_pvt *cpvt = dtc->pvt;
1863         struct wcdte *wc = cpvt->wc;
1864         u8 wctc4xxp_srcfmt; /* Digium Transcoder Engine Source Format */
1865         u8 wctc4xxp_dstfmt; /* Digium Transcoder Engine Dest Format */
1866         int res;
1867
1868         /* Check again to see if the channel was built after grabbing the
1869          * channel lock, in case the previous holder of the lock
1870          * built this channel as a complement to itself. */
1871         if (dahdi_tc_is_built(dtc)) {
1872                 DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP,
1873                   "Allocating channel %p which is already built.\n", dtc);
1874                 return 0;
1875         }
1876
1877         DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP,
1878                 "Entering %s for channel %p.\n", __func__, dtc);
1879         /* Anything on the rx queue now is old news... */
1880         wctc4xxp_cleanup_channel_private(wc, dtc);
1881         DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP,
1882                 "Allocating a new channel: %p.\n", dtc);
1883         wctc4xxp_srcfmt = wctc4xxp_dahdifmt_to_dtefmt(dtc->srcfmt);
1884         wctc4xxp_dstfmt = wctc4xxp_dahdifmt_to_dtefmt(dtc->dstfmt);
1885         res = wctc4xxp_create_channel_pair(wc, cpvt, wctc4xxp_srcfmt,
1886                 wctc4xxp_dstfmt);
1887         if (res) {
1888                 /* There was a problem creating the channel.... */
1889                 dev_err(&wc->pdev->dev, "Failed to create channel pair.\n");
1890                 return res;
1891         }
1892         /* Mark this channel as built */
1893         dahdi_tc_set_built(dtc);
1894         dtc->built_fmts = dtc->dstfmt | dtc->srcfmt;
1895         DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP,
1896           "Channel %p has dstfmt=%x and srcfmt=%x\n", dtc, dtc->dstfmt,
1897           dtc->srcfmt);
1898         /* Mark the channel complement (other half of encoder/decoder pair) as
1899          * built */
1900         wctc4xxp_mark_channel_complement_built(wc, dtc);
1901         dahdi_transcoder_alert(dtc);
1902         return 0;
1903 }
1904
1905 static void
1906 wctc4xxp_setintmask(struct wcdte *wc, unsigned int intmask)
1907 {
1908         wc->intmask = intmask;
1909         wctc4xxp_setctl(wc, 0x0038, intmask);
1910 }
1911
1912 static const u32 DEFAULT_INTERRUPTS = 0x0001a0c0;
1913
1914 static void
1915 wctc4xxp_enable_interrupts(struct wcdte *wc)
1916 {
1917         wctc4xxp_setintmask(wc, DEFAULT_INTERRUPTS);
1918 }
1919
1920 static void
1921 wctc4xxp_disable_interrupts(struct wcdte *wc)
1922 {
1923         /* Disable interrupts */
1924         wctc4xxp_setintmask(wc, 0x00000000);
1925         wctc4xxp_setctl(wc, 0x0084, 0x00000000);
1926 }
1927
1928 static void
1929 wctc4xxp_enable_polling(struct wcdte *wc)
1930 {
1931         set_bit(DTE_POLLING, &wc->flags);
1932         wctc4xxp_setctl(wc, 0x0058, 0x10003);
1933         /* Enable the general purpose timer interrupt. */
1934         wctc4xxp_setintmask(wc, (DEFAULT_INTERRUPTS | (1 << 11)) & ~0x41);
1935 }
1936
1937 static int
1938 wctc4xxp_operation_allocate(struct dahdi_transcoder_channel *dtc)
1939 {
1940         int res;
1941         struct wcdte *wc = ((struct channel_pvt *)(dtc->pvt))->wc;
1942
1943 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1944         mutex_lock(&wc->chanlock);
1945 #else
1946         res = mutex_lock_killable(&wc->chanlock);
1947         if (res)
1948                 return res;
1949 #endif
1950
1951         if (unlikely(test_bit(DTE_SHUTDOWN, &wc->flags))) {
1952                 /* The shudown flags can also be set if there is a
1953                  * catastrophic failure. */
1954                 res = -EIO;
1955                 goto error_exit;
1956         }
1957
1958         ++wc->open_channels;
1959         if (wc->open_channels > POLLING_CALL_THRESHOLD) {
1960                 if (!test_bit(DTE_POLLING, &wc->flags))
1961                         wctc4xxp_enable_polling(wc);
1962         }
1963
1964         if (dahdi_tc_is_built(dtc)) {
1965                 DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP,
1966                   "Allocating channel %p which is already built.\n", dtc);
1967                 res = 0;
1968         } else {
1969                 res = do_channel_allocate(dtc);
1970         }
1971
1972 error_exit:
1973         mutex_unlock(&wc->chanlock);
1974         return res;
1975 }
1976
1977 static void
1978 wctc4xxp_disable_polling(struct wcdte *wc)
1979 {
1980         clear_bit(DTE_POLLING, &wc->flags);
1981         wctc4xxp_setctl(wc, 0x0058, 0x0);
1982         wctc4xxp_enable_interrupts(wc);
1983 }
1984
1985 static void wctc4xxp_check_for_rx_errors(struct wcdte *wc)
1986 {
1987         static unsigned long last_errors = 0;
1988         unsigned long errors = wctc4xxp_get_packet_errors(wc->rxd);
1989         if (last_errors != errors) {
1990                 if (printk_ratelimit()) {
1991                         dev_err(&wc->pdev->dev,
1992                                 "%lu errored receive packets.\n",
1993                                 errors - last_errors);
1994                         last_errors = errors;
1995                 }
1996         }
1997 }
1998
1999 static int
2000 wctc4xxp_operation_release(struct dahdi_transcoder_channel *dtc)
2001 {
2002         int res;
2003         int index;
2004         /* This is the 'complimentary channel' to dtc.  I.e., if dtc is an
2005          * encoder, compl_dtc is the decoder and vice-versa */
2006         struct dahdi_transcoder_channel *compl_dtc;
2007         struct channel_pvt *compl_cpvt;
2008         struct channel_pvt *cpvt = dtc->pvt;
2009         struct wcdte *wc = cpvt->wc;
2010         int packets_received, packets_sent;
2011
2012         BUG_ON(!cpvt);
2013         BUG_ON(!wc);
2014
2015 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
2016         mutex_lock(&wc->chanlock);
2017 #else
2018         res = mutex_lock_killable(&wc->chanlock);
2019         if (res)
2020                 return res;
2021 #endif
2022
2023         if (unlikely(test_bit(DTE_SHUTDOWN, &wc->flags))) {
2024                 /* The shudown flags can also be set if there is a
2025                  * catastrophic failure. */
2026                 res = -EIO;
2027                 goto error_exit;
2028         }
2029
2030         if (wc->open_channels) {
2031                 --wc->open_channels;
2032
2033 #if !defined(CONFIG_WCTC4XXP_POLLING)
2034                 if (wc->open_channels < POLLING_CALL_THRESHOLD) {
2035                         if (test_bit(DTE_POLLING, &wc->flags))
2036                                 wctc4xxp_disable_polling(wc);
2037                 }
2038 #endif
2039         }
2040
2041         packets_received = atomic_read(&cpvt->stats.packets_received);
2042         packets_sent = atomic_read(&cpvt->stats.packets_sent);
2043
2044         DTE_DEBUG(DTE_DEBUG_ETH_STATS,
2045                 "%s channel %d sent %d packets and received %d packets.\n",
2046                 (cpvt->encoder) ?  "encoder" : "decoder", cpvt->chan_out_num,
2047                 packets_sent, packets_received);
2048
2049
2050         /* Remove any packets that are waiting on the outbound queue. */
2051         dahdi_tc_clear_busy(dtc);
2052         wctc4xxp_cleanup_channel_private(wc, dtc);
2053         index = cpvt->timeslot_in_num/2;
2054         BUG_ON(index >= wc->numchannels);
2055         if (cpvt->encoder)
2056                 compl_dtc = &(wc->udecode->channels[index]);
2057         else
2058                 compl_dtc = &(wc->uencode->channels[index]);
2059         BUG_ON(!compl_dtc);
2060         if (!dahdi_tc_is_built(compl_dtc)) {
2061                 DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP,
2062                         "Releasing a channel that was never built.\n");
2063                 res = 0;
2064                 goto error_exit;
2065         }
2066         /* If the channel complement (other half of the encoder/decoder pair) is
2067          * being used. */
2068         if (dahdi_tc_is_busy(compl_dtc)) {
2069                 res = 0;
2070                 goto error_exit;
2071         }
2072         res = wctc4xxp_destroy_channel_pair(wc, cpvt);
2073         if (res)
2074                 goto error_exit;
2075
2076         DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP, "Releasing channel: %p\n", dtc);
2077         /* Mark this channel as not built */
2078         dahdi_tc_clear_built(dtc);
2079         dtc->built_fmts = 0;
2080         cpvt->chan_in_num = INVALID;
2081         cpvt->chan_out_num = INVALID;
2082         /* Mark the channel complement as not built */
2083         dahdi_tc_clear_built(compl_dtc);
2084         compl_dtc->built_fmts = 0;
2085         compl_cpvt = compl_dtc->pvt;
2086         compl_cpvt->chan_in_num = INVALID;
2087         compl_cpvt->chan_out_num = INVALID;
2088
2089         wctc4xxp_check_for_rx_errors(wc);
2090
2091 error_exit:
2092         mutex_unlock(&wc->chanlock);
2093         return res;
2094 }
2095
2096 static inline struct tcb*
2097 get_ready_cmd(struct dahdi_transcoder_channel *dtc)
2098 {
2099         struct channel_pvt *cpvt = dtc->pvt;
2100         struct tcb *cmd;
2101         unsigned long flags;
2102         spin_lock_irqsave(&cpvt->lock, flags);
2103         if (!list_empty(&cpvt->rx_queue)) {
2104                 WARN_ON(!dahdi_tc_is_data_waiting(dtc));
2105                 cmd = list_entry(cpvt->rx_queue.next, struct tcb, node);
2106                 list_del_init(&cmd->node);
2107         } else {
2108                 cmd = NULL;
2109         }
2110         if (list_empty(&cpvt->rx_queue))
2111                 dahdi_tc_clear_data_waiting(dtc);
2112         spin_unlock_irqrestore(&cpvt->lock, flags);
2113         return cmd;
2114 }
2115
2116 static int
2117 wctc4xxp_handle_receive_ring(struct wcdte *wc)
2118 {
2119         struct tcb *cmd;
2120         unsigned long flags;
2121         unsigned int count = 0;
2122
2123         /* If we can't grab this lock, another thread must already be checking
2124          * the receive ring...so we should just finish up, and we'll try again
2125          * later. */
2126 #if defined(spin_trylock_irqsave)
2127         if (!spin_trylock_irqsave(&wc->rx_lock, flags))
2128                 return 0;
2129 #else
2130         if (spin_is_locked(&wc->rx_lock))
2131                 return 0;
2132         spin_lock_irqsave(&wc->rx_lock, flags);
2133 #endif
2134
2135         while ((cmd = wctc4xxp_retrieve(wc->rxd))) {
2136                 ++count;
2137                 spin_lock(&wc->rx_list_lock);
2138                 list_add_tail(&cmd->node, &wc->rx_list);
2139                 spin_unlock(&wc->rx_list_lock);
2140                 cmd = __alloc_cmd(SFRAME_SIZE, GFP_ATOMIC, 0);
2141                 if (!cmd) {
2142                         dev_err(&wc->pdev->dev,
2143                                 "Out of memory in %s.\n", __func__);
2144                 } else {
2145                         if (wctc4xxp_submit(wc->rxd, cmd)) {
2146                                 dev_err(&wc->pdev->dev, "Failed submit in %s\n",
2147                                         __func__);
2148                                 free_cmd(cmd);
2149                         }
2150                 }
2151         }
2152         spin_unlock_irqrestore(&wc->rx_lock, flags);
2153         return count;
2154 }
2155
2156 /* Called with a buffer in which to copy a transcoded frame. */
2157 static ssize_t
2158 wctc4xxp_read(struct file *file, char __user *frame, size_t count, loff_t *ppos)
2159 {
2160         ssize_t ret;
2161         struct dahdi_transcoder_channel *dtc = file->private_data;
2162         struct channel_pvt *cpvt = dtc->pvt;
2163         struct wcdte *wc = cpvt->wc;
2164         struct tcb *cmd;
2165         struct rtp_packet *packet;
2166         ssize_t payload_bytes;
2167         ssize_t returned_bytes = 0;
2168         unsigned long flags;
2169
2170         BUG_ON(!dtc);
2171         BUG_ON(!cpvt);
2172
2173         if (unlikely(test_bit(DTE_SHUTDOWN, &wc->flags))) {
2174                 /* The shudown flags can also be set if there is a
2175                  * catastrophic failure. */
2176                 return -EIO;
2177         }
2178
2179         cmd = get_ready_cmd(dtc);
2180         if (!cmd) {
2181                 if (file->f_flags & O_NONBLOCK)
2182                         return -EAGAIN;
2183                 ret = wait_event_interruptible(dtc->ready,
2184                                 dahdi_tc_is_data_waiting(dtc));
2185                 if (-ERESTARTSYS == ret)
2186                         return -EINTR;
2187                 /* List went not empty. */
2188                 cmd = get_ready_cmd(dtc);
2189         }
2190
2191         do {
2192                 BUG_ON(!cmd);
2193                 packet = cmd->data;
2194
2195                 payload_bytes = be16_to_cpu(packet->udphdr.len) -
2196                                         sizeof(struct rtphdr) -
2197                                         sizeof(struct udphdr);
2198
2199                 if (count < (payload_bytes + returned_bytes)) {
2200                         if (returned_bytes) {
2201                                 /* If we have already returned at least one
2202                                  * packets worth of data, we'll add this next
2203                                  * packet to the head of the receive queue so
2204                                  * it will be picked up next time. */
2205                                 spin_lock_irqsave(&cpvt->lock, flags);
2206                                 list_add(&cmd->node, &cpvt->rx_queue);
2207                                 dahdi_tc_set_data_waiting(dtc);
2208                                 spin_unlock_irqrestore(&cpvt->lock, flags);
2209                                 return returned_bytes;
2210                         }
2211
2212                         if (printk_ratelimit()) {
2213                                 dev_err(&wc->pdev->dev,
2214                                   "Cannot copy %zd bytes into %zd byte user " \
2215                                   "buffer.\n", payload_bytes, count);
2216                         }
2217                         free_cmd(cmd);
2218                         return -EFBIG;
2219                 }
2220
2221                 atomic_inc(&cpvt->stats.packets_received);
2222
2223                 ret = copy_to_user(&frame[returned_bytes],
2224                                    &packet->payload[0], payload_bytes);
2225                 if (unlikely(ret)) {
2226                         dev_err(&wc->pdev->dev, "Failed to copy data in %s\n",
2227                                    __func__);
2228                         free_cmd(cmd);
2229                         return -EFAULT;
2230                 }
2231
2232                 returned_bytes += payload_bytes;
2233
2234                 free_cmd(cmd);
2235
2236         } while ((cmd = get_ready_cmd(dtc)));
2237
2238         return returned_bytes;
2239 }
2240
2241 /* Called with a frame in the srcfmt to be transcoded into the dstfmt. */
2242 static ssize_t
2243 wctc4xxp_write(struct file *file, const char __user *frame,
2244         size_t count, loff_t *ppos)
2245 {
2246         struct dahdi_transcoder_channel *dtc = file->private_data;
2247         struct channel_pvt *cpvt = dtc->pvt;
2248         struct wcdte *wc = cpvt->wc;
2249         struct tcb *cmd;
2250         u32 samples;
2251         unsigned long flags;
2252         const unsigned long MAX_SAMPLES_IN_FLIGHT = 640;
2253
2254         BUG_ON(!cpvt);
2255         BUG_ON(!wc);
2256
2257         if (unlikely(test_bit(DTE_SHUTDOWN, &wc->flags)))
2258                 return -EIO;
2259
2260         if (!test_bit(DAHDI_TC_FLAG_CHAN_BUILT, &dtc->flags))
2261                 return -EAGAIN;
2262
2263         if (count < 2) {
2264                 DTE_DEBUG(DTE_DEBUG_GENERAL,
2265                    "Cannot request to transcode a packet that is less than " \
2266                    "2 bytes.\n");
2267                 return -EINVAL;
2268         }
2269
2270         if (unlikely(count > SFRAME_SIZE - sizeof(struct rtp_packet))) {
2271                 DTE_DEBUG(DTE_DEBUG_GENERAL,
2272                    "Cannot transcode packet of %Zu bytes. This exceeds the " \
2273                    "maximum size of %Zu bytes.\n", count,
2274                    SFRAME_SIZE - sizeof(struct rtp_packet));
2275                 return -EINVAL;
2276         }
2277
2278         if (DAHDI_FORMAT_G723_1 == dtc->srcfmt) {
2279                 if ((G723_5K_BYTES != count) && (G723_6K_BYTES != count) &&
2280                     (G723_SID_BYTES != count)) {
2281                         DTE_DEBUG(DTE_DEBUG_GENERAL,
2282                            "Trying to transcode packet into G723 format " \
2283                            "that is %Zu bytes instead of the expected " \
2284                            "%d/%d/%d bytes.\n", count, G723_5K_BYTES,
2285                            G723_6K_BYTES, G723_SID_BYTES);
2286                         return -EINVAL;
2287                 }
2288         }
2289
2290         /* Do not flood the firmware with packets. This can result in out of
2291          * memory conditions in the firmware. */
2292         spin_lock_irqsave(&cpvt->lock, flags);
2293         if (time_after(jiffies, cpvt->send_time)) {
2294                 cpvt->samples_in_flight = max(0L,
2295                                               cpvt->samples_in_flight - 160L);
2296         }
2297         samples = wctc4xxp_bytes_to_samples(dtc->srcfmt, count);
2298         if ((cpvt->samples_in_flight + samples) > MAX_SAMPLES_IN_FLIGHT) {
2299                 spin_unlock_irqrestore(&cpvt->lock, flags);
2300                 /* This should most likely be an error, but it results in
2301                  * codec_dahdi spamming when it's not set to wait for new
2302                  * packets. Instead we will silently drop the bytes. */
2303                 return count;
2304         }
2305         cpvt->send_time = jiffies + msecs_to_jiffies(20);
2306         spin_unlock_irqrestore(&cpvt->lock, flags);
2307
2308         cmd = wctc4xxp_create_rtp_cmd(wc, dtc, count);
2309         if (!cmd)
2310                 return -ENOMEM;
2311         /* Copy the data directly from user space into the command buffer. */
2312         if (copy_from_user(&((struct rtp_packet *)(cmd->data))->payload[0],
2313                 frame, count)) {
2314                 dev_err(&wc->pdev->dev,
2315                         "Failed to copy packet from userspace.\n");
2316                 free_cmd(cmd);
2317                 return -EFAULT;
2318         }
2319         cpvt->seqno += 1;
2320
2321         DTE_DEBUG(DTE_DEBUG_RTP_TX,
2322             "Sending packet of %Zu byte on channel (%p).\n", count, dtc);
2323
2324         atomic_inc(&cpvt->stats.packets_sent);
2325         wctc4xxp_transmit_cmd(wc, cmd);
2326
2327         return count;
2328 }
2329
2330 static void
2331 wctc4xxp_send_ack(struct wcdte *wc, u8 seqno, __be16 channel, __le16 function)
2332 {
2333         struct tcb *cmd;
2334         struct csm_encaps_hdr *hdr;
2335         cmd = __alloc_cmd(sizeof(*hdr), ALLOC_FLAGS, 0);
2336         if (!cmd) {
2337                 WARN_ON(1);
2338                 return;
2339         }
2340         hdr = cmd->data;
2341         BUG_ON(sizeof(*hdr) > cmd->data_len);
2342         setup_common_header(wc, hdr);
2343         hdr->op_code = cpu_to_be16(0x0001);
2344         hdr->seq_num = seqno;
2345         hdr->control = 0xe0;
2346         hdr->channel = channel;
2347         hdr->cmd.function = function;
2348
2349         wctc4xxp_transmit_cmd(wc, cmd);
2350 }
2351
2352
2353 static void do_rx_response_packet(struct wcdte *wc, struct tcb *cmd)
2354 {
2355         struct csm_encaps_hdr *rxhdr;
2356         const struct csm_encaps_hdr *listhdr;
2357         struct tcb *pos, *temp;
2358         unsigned long flags;
2359         bool handled = false;
2360         rxhdr = cmd->data;
2361
2362         /* Check if duplicated response on the supervisor channel. */
2363         if (SUPERVISOR_CHANNEL == rxhdr->channel) {
2364                 if (rxhdr->seq_num == wc->last_rx_seq_num) {
2365                         free_cmd(cmd);
2366                         return;
2367                 }
2368                 wc->last_rx_seq_num = rxhdr->seq_num;
2369         }
2370
2371         spin_lock_irqsave(&wc->cmd_list_lock, flags);
2372         list_for_each_entry_safe(pos, temp,
2373                 &wc->waiting_for_response_list, node) {
2374                 listhdr = pos->data;
2375                 if ((listhdr->cmd.function == rxhdr->cmd.function) &&
2376                     (listhdr->channel == rxhdr->channel)) {
2377
2378                         /* If this is a channel command, do not complete it if
2379                          * the seq_num is the same as previous. */
2380                         if (pos->cpvt) {
2381                                 if (rxhdr->seq_num ==
2382                                     pos->cpvt->last_rx_seq_num) {
2383                                         break;
2384                                 }
2385                                 pos->cpvt->last_rx_seq_num = rxhdr->seq_num;
2386                         }
2387
2388                         list_del_init(&pos->node);
2389                         pos->flags &= ~(WAIT_FOR_RESPONSE);
2390                         pos->response = cmd;
2391                         /* If this isn't TX_COMPLETE yet, then this packet will
2392                          * be completed in service_tx_ring. */
2393                         if (pos->flags & TX_COMPLETE && pos->complete)
2394                                 complete(pos->complete);
2395                         handled = true;
2396
2397                         break;
2398                 }
2399         }
2400         spin_unlock_irqrestore(&wc->cmd_list_lock, flags);
2401
2402         if (!handled) {
2403                 DTE_DEBUG(DTE_DEBUG_GENERAL,
2404                         "Freeing unhandled response ch:(%04x)\n",
2405                         be16_to_cpu(rxhdr->channel));
2406                 free_cmd(cmd);
2407         }
2408 }
2409
2410 static void
2411 do_rx_ack_packet(struct wcdte *wc, struct tcb *cmd)
2412 {
2413         const struct csm_encaps_hdr *listhdr, *rxhdr;
2414         struct tcb *pos, *temp;
2415         unsigned long flags;
2416
2417         rxhdr = cmd->data;
2418
2419         spin_lock_irqsave(&wc->cmd_list_lock, flags);
2420         list_for_each_entry_safe(pos, temp,
2421                 &wc->waiting_for_response_list, node) {
2422                 listhdr = pos->data;
2423                 if (cpu_to_be16(0xefed) == listhdr->ethhdr.h_proto) {
2424                         wc->seq_num = (rxhdr->seq_num + 1) & 0xff;
2425                         WARN_ON(!(pos->complete));
2426                         WARN_ON(!(pos->flags & TX_COMPLETE));
2427                         list_del_init(&pos->node);
2428                         if (pos->complete)
2429                                 complete(pos->complete);
2430                 } else if ((listhdr->seq_num == rxhdr->seq_num) &&
2431                            (listhdr->channel == rxhdr->channel)) {
2432                         if (pos->flags & WAIT_FOR_RESPONSE) {
2433                                 pos->flags &= ~(WAIT_FOR_ACK);
2434                         } else {
2435                                 list_del_init(&pos->node);
2436
2437                                 if (pos->complete) {
2438                                         WARN_ON(!(pos->flags & TX_COMPLETE));
2439                                         complete(pos->complete);
2440                                 } else {
2441                                         free_cmd(pos);
2442                                 }
2443                         }
2444                         break;
2445                 }
2446         }
2447         spin_unlock_irqrestore(&wc->cmd_list_lock, flags);
2448
2449         /* There is never a reason to store up the ack packets. */
2450         free_cmd(cmd);
2451 }
2452
2453 static inline int
2454 is_response(const struct csm_encaps_hdr *hdr)
2455 {
2456         return ((0x02 == hdr->cmd.type) ||
2457                 (0x04 == hdr->cmd.type) ||
2458                 (0x0e == hdr->cmd.type) ||
2459                 (0x00 == hdr->cmd.type)) ? 1 : 0;
2460 }
2461
2462 static void
2463 print_command(struct wcdte *wc, const struct tcb *cmd)
2464 {
2465         int i, curlength;
2466         const struct csm_encaps_hdr *hdr = cmd->data;
2467         char *buffer;
2468         const int BUFFER_SIZE = 1024;
2469         int parameters = ((hdr->cmd.length - 8)/sizeof(__le16));
2470
2471         buffer = kzalloc(BUFFER_SIZE + 1, GFP_ATOMIC);
2472         if (!buffer) {
2473                 dev_info(&wc->pdev->dev, "Failed print_command\n");
2474                 return;
2475         }
2476         curlength = snprintf(buffer, BUFFER_SIZE,
2477                 "opcode: %04x seq: %02x control: %02x "
2478                 "channel: %04x ", be16_to_cpu(hdr->op_code),
2479                 hdr->seq_num, hdr->control, be16_to_cpu(hdr->channel));
2480         curlength += snprintf(buffer + curlength, BUFFER_SIZE - curlength,
2481                 "length: %02x index: %02x type: %02x "
2482                 "class: %02x function: %04x",
2483                 hdr->cmd.length, hdr->cmd.index, hdr->cmd.type, hdr->cmd.class,
2484                 le16_to_cpu(hdr->cmd.function));
2485         for (i = 0; i < parameters; ++i) {
2486                 curlength += snprintf(buffer + curlength,
2487                         BUFFER_SIZE - curlength, " %04x",
2488                         le16_to_cpu(hdr->cmd.params[i]));
2489         }
2490         dev_info(&wc->pdev->dev, "%s\n", buffer);
2491         kfree(buffer);
2492 }
2493
2494 static inline void wctc4xxp_reset_processor(struct wcdte *wc)
2495 {
2496         wctc4xxp_setctl(wc, 0x00A0, 0x04000000);
2497 }
2498
2499 static void
2500 receive_csm_encaps_packet(struct wcdte *wc, struct tcb *cmd)
2501 {
2502         const struct csm_encaps_hdr *hdr = cmd->data;
2503         const struct csm_encaps_cmd *c = &hdr->cmd;
2504
2505         if (!(hdr->control & MESSAGE_PACKET)) {
2506                 const bool suppress_ack = ((hdr->control & SUPPRESS_ACK) > 0);
2507
2508                 if (!suppress_ack) {
2509                         wctc4xxp_send_ack(wc, hdr->seq_num, hdr->channel,
2510                                           c->function);
2511                 }
2512
2513                 if (is_response(hdr)) {
2514
2515                         do_rx_response_packet(wc, cmd);
2516
2517                 } else if (0xc1 == c->type) {
2518
2519                         if (0x75 == c->class) {
2520                                 dev_warn(&wc->pdev->dev,
2521                                    "Received alert (0x%04x) from dsp\n",
2522                                    le16_to_cpu(c->params[0]));
2523                         }
2524                         free_cmd(cmd);
2525                 } else if (0xd4 == c->type) {
2526                         if (c->params[0] != le16_to_cpu(0xffff)) {
2527                                 dev_warn(&wc->pdev->dev,
2528                                    "DTE Failed self test (%04x).\n",
2529                                    le16_to_cpu(c->params[0]));
2530                         } else if ((c->params[1] != le16_to_cpu(0x000c)) &&
2531                                 (c->params[1] != le16_to_cpu(0x010c))) {
2532                                 dev_warn(&wc->pdev->dev,
2533                                    "Unexpected ERAM status (%04x).\n",
2534                                    le16_to_cpu(c->params[1]));
2535                         } else {
2536                                 wctc4xxp_set_ready(wc);
2537                                 wake_up(&wc->waitq);
2538                         }
2539                         free_cmd(cmd);
2540                 } else if (MONITOR_LIVE_INDICATION_TYPE == c->type) {
2541
2542                         if (c->function == 0x0000) {
2543                                 u16 alert_type = le16_to_cpu(c->params[0]);
2544                                 dev_err(&wc->pdev->dev,
2545                                         "Received alert (0x%04x) from dsp. Please reload driver.\n",
2546                                         alert_type);
2547
2548                                 wctc4xxp_reset_processor(wc);
2549                                 set_bit(DTE_SHUTDOWN, &wc->flags);
2550                                 wctc4xxp_timeout_all_commands(wc);
2551                         } else {
2552                                 dev_warn(&wc->pdev->dev,
2553                                          "Received diagnostic message:\n");
2554                         }
2555                         print_command(wc, cmd);
2556                         free_cmd(cmd);
2557                 } else {
2558                         dev_warn(&wc->pdev->dev,
2559                                  "Unknown command type received. %02x\n",
2560                                  c->type);
2561                         free_cmd(cmd);
2562                 }
2563         } else {
2564                 do_rx_ack_packet(wc, cmd);
2565         }
2566 }
2567
2568 static void
2569 queue_rtp_packet(struct wcdte *wc, struct tcb *cmd)
2570 {
2571         unsigned index;
2572         struct dahdi_transcoder_channel *dtc;
2573         struct channel_pvt *cpvt;
2574         struct rtp_packet *packet = cmd->data;
2575         unsigned long flags;
2576         long samples;
2577
2578         if (unlikely(ip_fast_csum((void *)(&packet->iphdr),
2579                 packet->iphdr.ihl))) {
2580                 DTE_DEBUG(DTE_DEBUG_GENERAL,
2581                         "Invalid checksum in RTP packet %04x\n",
2582                         ip_fast_csum((void *)(&packet->iphdr),
2583                         packet->iphdr.ihl));
2584                 free_cmd(cmd);
2585                 return;
2586         }
2587
2588         index = (be16_to_cpu(packet->udphdr.dest) - 0x5000) / 2;
2589         if (unlikely(!(index < wc->numchannels))) {
2590                 dev_err(&wc->pdev->dev,
2591                   "Invalid channel number in response from DTE.\n");
2592                 free_cmd(cmd);
2593                 return;
2594         }
2595
2596         switch (packet->rtphdr.type) {
2597         case 0x00:
2598         case 0x08:
2599                 dtc = &(wc->udecode->channels[index]);
2600                 break;
2601         case 0x04:
2602         case 0x12:
2603                 dtc = &(wc->uencode->channels[index]);
2604                 break;
2605         default:
2606                 dev_err(&wc->pdev->dev, "Unknown codec in packet (0x%02x).\n",\
2607                         packet->rtphdr.type);
2608                 free_cmd(cmd);
2609                 return;
2610         }
2611
2612         cpvt = dtc->pvt;
2613         if (!dahdi_tc_is_busy(dtc)) {
2614                 free_cmd(cmd);
2615                 return;
2616         }
2617
2618         spin_lock_irqsave(&cpvt->lock, flags);
2619         samples = wctc4xxp_bytes_to_samples(dtc->dstfmt,
2620                         be16_to_cpu(packet->udphdr.len) -
2621                         sizeof(struct rtphdr) - sizeof(struct udphdr));
2622         cpvt->samples_in_flight = max(cpvt->samples_in_flight - samples, 0L);
2623         list_add_tail(&cmd->node, &cpvt->rx_queue);
2624         dahdi_tc_set_data_waiting(dtc);
2625         spin_unlock_irqrestore(&cpvt->lock, flags);
2626         dahdi_transcoder_alert(dtc);
2627         return;
2628 }
2629
2630 static void service_tx_ring(struct wcdte *wc)
2631 {
2632         struct tcb *cmd;
2633         unsigned long flags;
2634         spin_lock_irqsave(&wc->cmd_list_lock, flags);
2635         while ((cmd = wctc4xxp_retrieve(wc->txd))) {
2636                 cmd->flags |= TX_COMPLETE;
2637                 if (!(cmd->flags & (WAIT_FOR_ACK | WAIT_FOR_RESPONSE))) {
2638                         /* If we're not waiting for an ACK or Response from
2639                          * the DTE, this message should not be sitting on any
2640                          * lists. */
2641                         WARN_ON(!list_empty(&cmd->node));
2642                         if (cmd->complete) {
2643                                 WARN_ON(!(cmd->flags & TX_COMPLETE));
2644                                 complete(cmd->complete);
2645                         } else {
2646                                 free_cmd(cmd);
2647                         }
2648                 }
2649
2650                 /* We've freed up a spot in the hardware ring buffer.  If
2651                  * another packet is queued up, let's submit it to the
2652                  * hardware. */
2653                 if (!list_empty(&wc->cmd_list)) {
2654                         cmd = list_entry(wc->cmd_list.next, struct tcb, node);
2655                         list_del_init(&cmd->node);
2656                         if (cmd->flags & (WAIT_FOR_ACK | WAIT_FOR_RESPONSE)) {
2657                                 list_add_tail(&cmd->node,
2658                                               &wc->waiting_for_response_list);
2659                         }
2660                         wctc4xxp_submit(wc->txd, cmd);
2661                 }
2662         }
2663         spin_unlock_irqrestore(&wc->cmd_list_lock, flags);
2664 }
2665
2666 static void service_rx_ring(struct wcdte *wc)
2667 {
2668         struct tcb *cmd;
2669         unsigned long flags;
2670         LIST_HEAD(local_list);
2671         spin_lock_irqsave(&wc->rx_list_lock, flags);
2672         list_splice_init(&wc->rx_list, &local_list);
2673         spin_unlock_irqrestore(&wc->rx_list_lock, flags);
2674
2675         /*
2676          * Process the received packets
2677          */
2678         while (!list_empty(&local_list)) {
2679                 const struct ethhdr *ethhdr;
2680
2681                 cmd = container_of(local_list.next, struct tcb, node);
2682                 ethhdr = (const struct ethhdr *)(cmd->data);
2683                 list_del_init(&cmd->node);
2684
2685                 wctc4xxp_net_capture_cmd(wc, cmd);
2686                 if (cpu_to_be16(ETH_P_IP) == ethhdr->h_proto) {
2687                         queue_rtp_packet(wc, cmd);
2688                 } else if (cpu_to_be16(ETH_P_CSM_ENCAPS) == ethhdr->h_proto) {
2689                         receive_csm_encaps_packet(wc, cmd);
2690                 } else {
2691                         DTE_DEBUG(DTE_DEBUG_GENERAL,
2692                            "Unknown packet protocol received: %04x.\n",
2693                            be16_to_cpu(ethhdr->h_proto));
2694                         free_cmd(cmd);
2695                 }
2696         }
2697         wctc4xxp_receive_demand_poll(wc);
2698 }
2699
2700 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
2701 static void deferred_work_func(void *param)
2702 {
2703         struct wcdte *wc = param;
2704 #else
2705 static void deferred_work_func(struct work_struct *work)
2706 {
2707         struct wcdte *wc = container_of(work, struct wcdte, deferred_work);
2708 #endif
2709         service_rx_ring(wc);
2710 }
2711
2712 DAHDI_IRQ_HANDLER(wctc4xxp_interrupt)
2713 {
2714         struct wcdte *wc = dev_id;
2715         bool packets_to_process = false;
2716         u32 ints;
2717 #define NORMAL_INTERRUPT_SUMMARY (1<<16)
2718 #define ABNORMAL_INTERRUPT_SUMMARY (1<<15)
2719
2720 #define TX_COMPLETE_INTERRUPT 0x00000001
2721 #define RX_COMPLETE_INTERRUPT 0x00000040
2722 #define TIMER_INTERRUPT       (1<<11)
2723 #define NORMAL_INTERRUPTS (TX_COMPLETE_INTERRUPT | RX_COMPLETE_INTERRUPT | \
2724                            TIMER_INTERRUPT)
2725
2726         /* Read and clear interrupts */
2727         ints = __wctc4xxp_getctl(wc, 0x0028);
2728
2729         if (!(ints & (NORMAL_INTERRUPT_SUMMARY|ABNORMAL_INTERRUPT_SUMMARY)))
2730                 return IRQ_NONE;
2731
2732         /* Clear all the pending interrupts. */
2733         __wctc4xxp_setctl(wc, 0x0028, ints);
2734
2735         if (ints & (RX_COMPLETE_INTERRUPT | TIMER_INTERRUPT)) {
2736                 packets_to_process = wctc4xxp_handle_receive_ring(wc) > 0;
2737                 service_tx_ring(wc);
2738
2739 #if DEFERRED_PROCESSING == WORKQUEUE
2740                 if (packets_to_process)
2741                         schedule_work(&wc->deferred_work);
2742 #elif DEFERRED_PROCESSING == INTERRUPT
2743 #error "You will need to change the locks if you want to run the processing " \
2744                 "in the interrupt handler."
2745 #else
2746 #error "Define a deferred processing function in kernel/wctc4xxp/wctc4xxp.h"
2747 #endif
2748
2749         } else {
2750                 if ((ints & 0x00008000) && debug)
2751                         dev_info(&wc->pdev->dev, "Abnormal Interrupt.\n");
2752
2753                 if (ints & 0x00002000)
2754                         dev_err(&wc->pdev->dev, "Fatal Bus Error Detected.\n");
2755
2756                 if ((ints & 0x00000100) && debug)
2757                         dev_info(&wc->pdev->dev, "Receive Stopped INT\n");
2758
2759                 if ((ints & 0x00000080) && debug) {
2760                         dev_info(&wc->pdev->dev,
2761                                  "Receive Desciptor Unavailable INT " \
2762                                  "(%d)\n", wctc4xxp_getcount(wc->rxd));
2763                 }
2764
2765                 if ((ints & 0x00000020) && debug)
2766                         dev_info(&wc->pdev->dev, "Transmit Under-flow INT\n");
2767
2768                 if ((ints & 0x00000008) && debug)
2769                         dev_info(&wc->pdev->dev, "Jabber Timer Time-out INT\n");
2770
2771                 if ((ints & 0x00000002) && debug) {
2772                         dev_info(&wc->pdev->dev,
2773                                  "Transmit Processor Stopped INT\n");
2774                 }
2775         }
2776         return IRQ_HANDLED;
2777 }
2778
2779 static int
2780 wctc4xxp_hardware_init(struct wcdte *wc)
2781 {
2782         /* Hardware stuff */
2783         enum {
2784                 /* Software Reset */
2785                 SWR             = (1 << 0),
2786                 /* Bus Arbitration (1 for priority transmit) */
2787                 BAR             = (1 << 1),
2788                 /* Memory Write Invalidate */
2789                 MWI             = (1 << 24),
2790                 /* Memory Read Line */
2791                 MRL             = (1 << 23),
2792                 /* Descriptor Skip Length */
2793                 DSLShift        = 2,
2794                 /* Cache Alignment */
2795                 CALShift        = 14,
2796                 /* Transmit Auto Pollling */
2797                 TAPShift        = 17,
2798         };
2799         u32 reg;
2800         unsigned long newjiffies;
2801         u8 cache_line_size;
2802         const u32 DEFAULT_PCI_ACCESS = (MWI | (11 << TAPShift));
2803
2804         if (pci_read_config_byte(wc->pdev, 0x0c, &cache_line_size))
2805                 return -EIO;
2806
2807         switch (cache_line_size) {
2808         case 0x08:
2809                 reg = DEFAULT_PCI_ACCESS | (0x1 << CALShift);
2810                 break;
2811         case 0x10:
2812                 reg = DEFAULT_PCI_ACCESS | (0x2 << CALShift);
2813                 break;
2814         case 0x20:
2815                 reg = DEFAULT_PCI_ACCESS | (0x3 << CALShift);
2816                 break;
2817         default:
2818                 reg = (11 << TAPShift);
2819                 break;
2820         }
2821
2822         reg |= ((wc->txd->padding / sizeof(u32)) << 2) & 0x7c;
2823
2824         /* Reset the DTE... */
2825         wctc4xxp_setctl(wc, 0x0000, reg | 1);
2826         newjiffies = jiffies + HZ; /* One second timeout */
2827         /* ...and wait for it to come out of reset. */
2828         while (((wctc4xxp_getctl(wc, 0x0000)) & 0x00000001) &&
2829                 (newjiffies > jiffies))
2830                 msleep(1);
2831
2832         wctc4xxp_setctl(wc, 0x0000, reg | 0x60000);
2833
2834         /* Configure watchdogs, access, etc */
2835         wctc4xxp_setctl(wc, 0x0030, 0x00280048);
2836         wctc4xxp_setctl(wc, 0x0078, 0x00000013);
2837         reg = wctc4xxp_getctl(wc, 0x00fc);
2838         wctc4xxp_setctl(wc, 0x00fc, (reg & ~0x7) | 0x7);
2839         reg = wctc4xxp_getctl(wc, 0x00fc);
2840         return 0;
2841 }
2842
2843 static void
2844 wctc4xxp_start_dma(struct wcdte *wc)
2845 {
2846         int res;
2847         int i;
2848         u32 reg;
2849         struct tcb *cmd;
2850
2851         for (i = 0; i < wc->rxd->size; ++i) {
2852                 cmd = alloc_cmd(SFRAME_SIZE);
2853                 if (!cmd) {
2854                         WARN_ALWAYS();
2855                         return;
2856                 }
2857                 WARN_ON(SFRAME_SIZE != cmd->data_len);
2858                 res = wctc4xxp_submit(wc->rxd, cmd);
2859                 if (res) {
2860                         /* When we're starting the DMA, we should always be
2861                          * able to fill the ring....so something is wrong
2862                          * here. */
2863                         WARN_ALWAYS();
2864                         free_cmd(cmd);
2865                         break;
2866                 }
2867         }
2868         wmb();
2869         wctc4xxp_setctl(wc, 0x0020, wc->txd->desc_dma);
2870         wctc4xxp_setctl(wc, 0x0018, wc->rxd->desc_dma);
2871
2872         /* Start receiver/transmitter */
2873         reg = wctc4xxp_getctl(wc, 0x0030);
2874         wctc4xxp_setctl(wc, 0x0030, reg | 0x00002002);
2875         wctc4xxp_receive_demand_poll(wc);
2876         reg = wctc4xxp_getctl(wc, 0x0028);
2877         wctc4xxp_setctl(wc, 0x0028, reg);
2878
2879 }
2880
2881 static void
2882 _wctc4xxp_stop_dma(struct wcdte *wc)
2883 {
2884         /* Disable interrupts and reset */
2885         unsigned int reg;
2886         /* Disable interrupts */
2887         wctc4xxp_setintmask(wc, 0x00000000);
2888         wctc4xxp_setctl(wc, 0x0084, 0x00000000);
2889         wctc4xxp_setctl(wc, 0x0048, 0x00000000);
2890         /* Reset the part to be on the safe side */
2891         reg = wctc4xxp_getctl(wc, 0x0000);
2892         reg |= 0x00000001;
2893         wctc4xxp_setctl(wc, 0x0000, reg);
2894 }
2895
2896 static void
2897 wctc4xxp_stop_dma(struct wcdte *wc)
2898 {
2899         unsigned long newjiffies;
2900
2901         _wctc4xxp_stop_dma(wc);
2902         newjiffies = jiffies + HZ; /* One second timeout */
2903         /* We'll wait here for the part to come out of reset */
2904         while (((wctc4xxp_getctl(wc, 0x0000)) & 0x00000001) &&
2905                 (newjiffies > jiffies))
2906                         msleep(1);
2907 }
2908
2909 #define MDIO_SHIFT_CLK          0x10000
2910 #define MDIO_DATA_WRITE1        0x20000
2911 #define MDIO_ENB                0x00000
2912 #define MDIO_ENB_IN             0x40000
2913 #define MDIO_DATA_READ          0x80000
2914
2915 static int
2916 wctc4xxp_read_phy(struct wcdte *wc, int location)
2917 {
2918         int i;
2919         long mdio_addr = 0x0048;
2920         int read_cmd = (0xf6 << 10) | (1 << 5) | location;
2921         int retval = 0;
2922
2923         /* Establish sync by sending at least 32 logic ones. */
2924         for (i = 32; i >= 0; --i) {
2925                 wctc4xxp_setctl(wc, mdio_addr,
2926                         MDIO_ENB | MDIO_DATA_WRITE1);
2927                 wctc4xxp_getctl(wc, mdio_addr);
2928                 wctc4xxp_setctl(wc, mdio_addr,
2929                         MDIO_ENB | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK);
2930                 wctc4xxp_getctl(wc, mdio_addr);
2931         }
2932
2933         /* Shift the read command bits out. */
2934         for (i = 17; i >= 0; --i) {
2935                 int dataval = (read_cmd & (1 << i)) ? MDIO_DATA_WRITE1 : 0;
2936
2937                 wctc4xxp_setctl(wc, mdio_addr, MDIO_ENB | dataval);
2938                 wctc4xxp_getctl(wc, mdio_addr);
2939                 wctc4xxp_setctl(wc, mdio_addr,
2940                         MDIO_ENB | dataval | MDIO_SHIFT_CLK);
2941                 wctc4xxp_getctl(wc, mdio_addr);
2942         }
2943
2944         /* Read the two transition, 16 data, and wire-idle bits. */
2945         for (i = 19; i > 0; --i) {
2946                 wctc4xxp_setctl(wc, mdio_addr, MDIO_ENB_IN);
2947                 wctc4xxp_getctl(wc, mdio_addr);
2948                 retval = (retval << 1) |
2949                         ((wctc4xxp_getctl(wc, mdio_addr) & MDIO_DATA_READ) ?
2950                         1 : 0);
2951                 wctc4xxp_setctl(wc, mdio_addr, MDIO_ENB_IN | MDIO_SHIFT_CLK);
2952                 wctc4xxp_getctl(wc, mdio_addr);
2953         }
2954         retval = (retval>>1) & 0xffff;
2955         return retval;
2956 }
2957
2958 static void
2959 wctc4xxp_write_phy(struct wcdte *wc, int location, int value)
2960 {
2961         int i;
2962         int cmd = (0x5002 << 16) | (1 << 23) | (location<<18) | value;
2963         long mdio_addr = 0x0048;
2964
2965         /* Establish sync by sending 32 logic ones. */
2966         for (i = 32; i >= 0; --i) {
2967                 wctc4xxp_setctl(wc, mdio_addr, MDIO_ENB | MDIO_DATA_WRITE1);
2968                 wctc4xxp_getctl(wc, mdio_addr);
2969                 wctc4xxp_setctl(wc, mdio_addr,
2970                         MDIO_ENB | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK);
2971                 wctc4xxp_getctl(wc, mdio_addr);
2972         }
2973         /* Shift the command bits out. */
2974         for (i = 31; i >= 0; --i) {
2975                 int dataval = (cmd & (1 << i)) ? MDIO_DATA_WRITE1 : 0;
2976                 wctc4xxp_setctl(wc, mdio_addr, MDIO_ENB | dataval);
2977                 wctc4xxp_getctl(wc, mdio_addr);
2978                 wctc4xxp_setctl(wc, mdio_addr,
2979                         MDIO_ENB | dataval | MDIO_SHIFT_CLK);
2980                 wctc4xxp_getctl(wc, mdio_addr);
2981         }
2982         /* Clear out extra bits. */
2983         for (i = 2; i > 0; --i) {
2984                 wctc4xxp_setctl(wc, mdio_addr, MDIO_ENB_IN);
2985                 wctc4xxp_getctl(wc, mdio_addr);
2986                 wctc4xxp_setctl(wc, mdio_addr, MDIO_ENB_IN | MDIO_SHIFT_CLK);
2987                 wctc4xxp_getctl(wc, mdio_addr);
2988         }
2989         return;
2990 }
2991
2992 static int
2993 wctc4xxp_wait_for_link(struct wcdte *wc)
2994 {
2995         int reg;
2996         unsigned int delay_count = 0;
2997         do {
2998                 reg = wctc4xxp_getctl(wc, 0x00fc);
2999                 msleep(2);
3000                 delay_count++;
3001
3002                 if (delay_count >= 5000) {
3003                         dev_err(&wc->pdev->dev,
3004                                 "Failed to link to DTE processor!\n");
3005                         return -EIO;
3006                 }
3007         } while ((reg & 0xE0000000) != 0xE0000000);
3008         return 0;
3009 }
3010
3011 static int
3012 wctc4xxp_load_firmware(struct wcdte *wc, const struct firmware *firmware)
3013 {
3014         unsigned int byteloc;
3015         unsigned int length;
3016         struct tcb *cmd;
3017         DECLARE_COMPLETION_ONSTACK(done);
3018
3019         byteloc = 17;
3020
3021         cmd = alloc_cmd(SFRAME_SIZE);
3022         if (!cmd)
3023                 return -ENOMEM;
3024
3025 #if defined(CONFIG_WCTC4XXP_POLLING)
3026         wctc4xxp_enable_polling(wc);
3027 #endif
3028
3029         while (byteloc < (firmware->size-20)) {
3030                 length = (firmware->data[byteloc] << 8) |
3031                                 firmware->data[byteloc+1];
3032                 byteloc += 2;
3033                 cmd->data_len = length;
3034                 BUG_ON(length > cmd->data_len);
3035                 memcpy(cmd->data, &firmware->data[byteloc], length);
3036                 byteloc += length;
3037                 cmd->flags = WAIT_FOR_ACK;
3038                 cmd->complete = &done;
3039                 wctc4xxp_transmit_cmd(wc, cmd);
3040                 wait_for_completion(&done);
3041                 if (cmd->flags & DTE_CMD_TIMEOUT) {
3042                         free_cmd(cmd);
3043                         dev_err(&wc->pdev->dev, "Failed to load firmware.\n");
3044 #if defined(CONFIG_WCTC4XXP_POLLING)
3045                         wctc4xxp_disable_polling(wc);
3046 #endif
3047                         return -EIO;
3048                 }
3049         }
3050         free_cmd(cmd);
3051         if (!wait_event_timeout(wc->waitq, wctc4xxp_is_ready(wc), 15*HZ)) {
3052                 dev_err(&wc->pdev->dev, "Failed to boot firmware.\n");
3053 #if defined(CONFIG_WCTC4XXP_POLLING)
3054                 wctc4xxp_disable_polling(wc);
3055 #endif
3056                 return -EIO;
3057         }
3058
3059 #if defined(CONFIG_WCTC4XXP_POLLING)
3060         wctc4xxp_disable_polling(wc);
3061 #endif
3062         return 0;
3063 }
3064
3065 static int
3066 wctc4xxp_turn_off_booted_led(struct wcdte *wc)
3067 {
3068         int ret = 0;
3069         int reg;
3070         /* Turn off auto negotiation */
3071         wctc4xxp_write_phy(wc, 0, 0x2100);
3072         DTE_DEBUG(DTE_DEBUG_GENERAL, "PHY register 0 = %X\n",
3073            wctc4xxp_read_phy(wc, 0));
3074
3075         wctc4xxp_reset_processor(wc);
3076
3077         /* Wait 4 ms to ensure processor reset */
3078         msleep(4);
3079
3080         /* Clear reset */
3081         wctc4xxp_setctl(wc, 0x00A0, 0x04080000);
3082
3083         /* Wait for the ethernet link */
3084         ret = wctc4xxp_wait_for_link(wc);
3085         if (ret)
3086                 return ret;
3087
3088         /* Turn off booted LED */
3089         wctc4xxp_setctl(wc, 0x00A0, 0x04084000);
3090         reg = wctc4xxp_getctl(wc, 0x00fc);
3091         DTE_DEBUG(DTE_DEBUG_GENERAL, "LINK STATUS: reg(0xfc) = %X\n", reg);
3092
3093         reg = wctc4xxp_getctl(wc, 0x00A0);
3094
3095         return ret;
3096 }
3097
3098 static void
3099 wctc4xxp_turn_on_booted_led(struct wcdte *wc)
3100 {
3101         wctc4xxp_setctl(wc, 0x00A0, 0x04080000);
3102 }
3103
3104 static int
3105 wctc4xxp_boot_processor(struct wcdte *wc, const struct firmware *firmware)
3106 {
3107         int ret;
3108
3109         wctc4xxp_turn_off_booted_led(wc);
3110
3111         ret = wctc4xxp_load_firmware(wc, firmware);
3112         if (ret)
3113                 return ret;
3114
3115         wctc4xxp_turn_on_booted_led(wc);
3116
3117         DTE_DEBUG(DTE_DEBUG_GENERAL, "Successfully booted DTE processor.\n");
3118         return 0;
3119 }
3120
3121 static void
3122 setup_half_channel(struct channel_pvt *pvt, struct tcb *cmd, u16 length)
3123 {
3124         setup_channel_header(pvt, cmd);
3125
3126         append_set_ip_hdr_channel_cmd(cmd);
3127         append_voip_vceopt_cmd(cmd, length);
3128         append_voip_tonectl_cmd(cmd);
3129         append_voip_dtmfopt_cmd(cmd);
3130         append_voip_indctrl_cmd(cmd);
3131
3132         /* To indicate the end of multiple messages. */
3133         cmd->data_len += 4;
3134         WARN_ON(cmd->data_len >= SFRAME_SIZE);
3135
3136         wctc4xxp_transmit_cmd(pvt->wc, cmd);
3137 }
3138
3139 static int wctc4xxp_setup_channels(struct wcdte *wc,
3140                                    struct channel_pvt *encoder_pvt,
3141                                    struct channel_pvt *decoder_pvt,
3142                                    u16 length)
3143 {
3144         int res = 0;
3145         struct tcb *encoder_cmd;
3146         struct tcb *decoder_cmd;
3147         DECLARE_COMPLETION_ONSTACK(encoder_done);
3148         DECLARE_COMPLETION_ONSTACK(decoder_done);
3149
3150         encoder_cmd = alloc_cmd(SFRAME_SIZE);
3151         decoder_cmd = alloc_cmd(SFRAME_SIZE);
3152
3153         if (!encoder_cmd || !decoder_cmd) {
3154                 res = -ENOMEM;
3155                 goto error_exit;
3156         }
3157
3158         encoder_cmd->complete = &encoder_done;
3159         decoder_cmd->complete = &decoder_done;
3160
3161         setup_half_channel(encoder_pvt, encoder_cmd, length);
3162         setup_half_channel(decoder_pvt, decoder_cmd, length);
3163
3164         wait_for_completion(&decoder_done);
3165         wait_for_completion(&encoder_done);
3166
3167         if (encoder_cmd->flags & DTE_CMD_TIMEOUT ||
3168             decoder_cmd->flags & DTE_CMD_TIMEOUT) {
3169                 DTE_DEBUG(DTE_DEBUG_GENERAL, "Timeout waiting for command.\n");
3170                 res = -EIO;
3171         }
3172
3173         if ((0x0000 != response_header(encoder_cmd)->cmd.params[0]) ||
3174             (0x0000 != response_header(encoder_cmd)->cmd.params[0]))
3175                 res = -EIO;
3176
3177 error_exit:
3178         free_cmd(encoder_cmd);
3179         free_cmd(decoder_cmd);
3180         return res;
3181 }
3182
3183 static int wctc4xxp_enable_channels(struct wcdte *wc,
3184                                     struct channel_pvt *encoder_pvt,
3185                                     struct channel_pvt *decoder_pvt,
3186                                     u8 complicated, u8 simple)
3187 {
3188         int res = 0;
3189         struct tcb *encoder_cmd;
3190         struct tcb *decoder_cmd;
3191         DECLARE_COMPLETION_ONSTACK(encoder_done);
3192         DECLARE_COMPLETION_ONSTACK(decoder_done);
3193
3194         encoder_cmd = alloc_cmd(SFRAME_SIZE);
3195         decoder_cmd = alloc_cmd(SFRAME_SIZE);
3196
3197         if (!encoder_cmd || !decoder_cmd) {
3198                 res = -ENOMEM;
3199                 goto error_exit;
3200         }
3201
3202         encoder_cmd->complete = &encoder_done;
3203         decoder_cmd->complete = &decoder_done;
3204
3205         send_voip_vopena_cmd(encoder_pvt, encoder_cmd, complicated);
3206         send_voip_vopena_cmd(decoder_pvt, decoder_cmd, simple);
3207
3208         wait_for_completion(&decoder_done);
3209         wait_for_completion(&encoder_done);
3210
3211         if ((0x0000 != response_header(encoder_cmd)->cmd.params[0]) ||
3212             (0x0000 != response_header(decoder_cmd)->cmd.params[0]))
3213                 res = -EIO;
3214
3215 error_exit:
3216         free_cmd(encoder_cmd);
3217         free_cmd(decoder_cmd);
3218         return res;
3219 }
3220
3221 static int
3222 wctc4xxp_create_channel_pair(struct wcdte *wc, struct channel_pvt *cpvt,
3223         u8 simple, u8 complicated)
3224 {
3225         struct channel_pvt *encoder_pvt, *decoder_pvt;
3226         u16 encoder_timeslot, decoder_timeslot;
3227         u16 encoder_channel, decoder_channel;
3228         struct tcb *cmd;
3229         u16 length;
3230
3231         cmd = alloc_cmd(SFRAME_SIZE);
3232         if (!cmd)
3233                 return -ENOMEM;
3234
3235         BUG_ON(!wc || !cpvt);
3236         if (cpvt->encoder) {
3237                 encoder_timeslot = cpvt->timeslot_in_num;
3238                 decoder_timeslot = cpvt->timeslot_out_num;
3239         } else {
3240                 u8 temp;
3241                 encoder_timeslot = cpvt->timeslot_out_num;
3242                 decoder_timeslot = cpvt->timeslot_in_num;
3243                 temp = simple;
3244                 simple = complicated;
3245                 complicated = temp;
3246         }
3247
3248         length = (DTE_FORMAT_G729A == complicated) ? G729_LENGTH :
3249                 (DTE_FORMAT_G723_1 == complicated) ? G723_LENGTH : 0;
3250
3251
3252         BUG_ON(encoder_timeslot/2 >= wc->numchannels);
3253         BUG_ON(decoder_timeslot/2 >= wc->numchannels);
3254         encoder_pvt = wc->uencode->channels[encoder_timeslot/2].pvt;
3255         decoder_pvt = wc->udecode->channels[decoder_timeslot/2].pvt;
3256         BUG_ON(!encoder_pvt);
3257         BUG_ON(!decoder_pvt);
3258         encoder_pvt->last_rx_seq_num = 0xff;
3259         decoder_pvt->last_rx_seq_num = 0xff;
3260
3261         WARN_ON(encoder_timeslot == decoder_timeslot);
3262         /* First, let's create two channels, one for the simple -> complex
3263          * encoder and another for the complex->simple decoder. */
3264         if (send_create_channel_cmd(wc, cmd, encoder_timeslot,
3265                 &encoder_channel))
3266                 goto error_exit;
3267
3268         if (send_create_channel_cmd(wc, cmd, decoder_timeslot,
3269                 &decoder_channel))
3270                 goto error_exit;
3271
3272         DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP,
3273            "DTE is using the following channels encoder_channel: " \
3274            "%d decoder_channel: %d\n", encoder_channel, decoder_channel);
3275
3276         WARN_ON(encoder_channel == decoder_channel);
3277         /* Now set all the default parameters for the encoder. */
3278         encoder_pvt->chan_in_num = encoder_channel;
3279         encoder_pvt->chan_out_num = decoder_channel;
3280
3281         decoder_pvt->chan_in_num = decoder_channel;
3282         decoder_pvt->chan_out_num = encoder_channel;
3283
3284         if (wctc4xxp_setup_channels(wc, encoder_pvt, decoder_pvt, length))
3285                 goto error_exit;
3286
3287         if (send_trans_connect_cmd(wc, cmd, encoder_channel,
3288                 decoder_channel, complicated, simple))
3289                 goto error_exit;
3290
3291         if (wctc4xxp_enable_channels(wc, encoder_pvt, decoder_pvt,
3292                                      complicated, simple))
3293                 goto error_exit;
3294
3295         DTE_DEBUG(DTE_DEBUG_CHANNEL_SETUP,
3296           "DTE has completed setup and connected the " \
3297           "two channels together.\n");
3298
3299         free_cmd(cmd);
3300         return 0;
3301 error_exit:
3302         free_cmd(cmd);
3303         return -EIO;
3304 }
3305
3306 static void print_vceinfo_packet(struct wcdte *wc, struct tcb *cmd)
3307 {
3308         int i;
3309         struct device *const dev = &wc->pdev->dev;
3310
3311         static const struct {
3312                 const char *name;
3313                 bool show;
3314         } PARAMETERS[] = {
3315                 { "Format Revision                                   ", false},
3316                 { "Reserved                                          ", false},
3317                 { "Call Timer (seconds)                              ", false},
3318                 { "Current Playout Delay [to PCM]                    ", false},
3319                 { "Minimum Playout Delay [to PCM]                    ", false},
3320                 { "Maximum Playout Delay [to PCM]                    ", false},
3321                 { "Clock Offset                                      ", false},
3322                 { "PeakJitter (ms)                                   ", true},
3323                 { "Interpolative Concealment [to PCM]                ", false},
3324                 { "Silence Concealment [to PCM]                      ", false},
3325                 { "Jitter Buffer Overflow Discard [from IP]          ", true},
3326                 { "End-point Detection Errors                        ", true},
3327                 { "Number of Tx Voice Packets [to IP]                ", true},