Debian Codegraph @

drivers/dahdi/wctc4xxp/base.c

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