650c9c02f223bca915825723d72942e5382aed99
[safe/jmp/linux-2.6] / drivers / staging / cxt1e1 / musycc.c
1 /*
2  * $Id: musycc.c,v 2.1 2007/08/15 23:32:17 rickd PMCC4_3_1B $
3  */
4
5 unsigned int max_intcnt = 0;
6 unsigned int max_bh = 0;
7
8 /*-----------------------------------------------------------------------------
9  * musycc.c -
10  *
11  * Copyright (C) 2007  One Stop Systems, Inc.
12  * Copyright (C) 2003-2006  SBE, Inc.
13  *
14  *   This program is free software; you can redistribute it and/or modify
15  *   it under the terms of the GNU General Public License as published by
16  *   the Free Software Foundation; either version 2 of the License, or
17  *   (at your option) any later version.
18  *
19  *   This program is distributed in the hope that it will be useful,
20  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   GNU General Public License for more details.
23  *
24  * For further information, contact via email: support@onestopsystems.com
25  * One Stop Systems, Inc.  Escondido, California  U.S.A.
26  *-----------------------------------------------------------------------------
27  * RCS info:
28  * RCS revision: $Revision: 2.1 $
29  * Last changed on $Date: 2007/08/15 23:32:17 $
30  * Changed by $Author: rickd $
31  *-----------------------------------------------------------------------------
32  * $Log: musycc.c,v $
33  * Revision 2.1  2007/08/15 23:32:17  rickd
34  * Use 'if 0' instead of GNU comment delimeter to avoid line wrap induced compiler errors.
35  *
36  * Revision 2.0  2007/08/15 22:13:20  rickd
37  * Update to printf pointer %p usage and correct some UINT to ULONG for
38  * 64bit comptibility.
39  *
40  * Revision 1.7  2006/04/21 00:56:40  rickd
41  * workqueue files now prefixed with <sbecom> prefix.
42  *
43  * Revision 1.6  2005/10/27 18:54:19  rickd
44  * Clean out old code.  Default to HDLC_FCS16, not TRANS.
45  *
46  * Revision 1.5  2005/10/17 23:55:28  rickd
47  * Initial port of NCOMM support patches from original work found
48  * in pmc_c4t1e1 as updated by NCOMM.  Ref: CONFIG_SBE_PMCC4_NCOMM.
49  *
50  * Revision 1.4  2005/10/13 20:35:25  rickd
51  * Cleanup warning for unused <flags> variable.
52  *
53  * Revision 1.3  2005/10/13 19:19:22  rickd
54  * Disable redundant driver removal cleanup code.
55  *
56  * Revision 1.2  2005/10/11 18:36:16  rickd
57  * Clean up warning messages caused by de-implemented some <flags> associated
58  * with spin_lock() removals.
59  *
60  * Revision 1.1  2005/10/05 00:45:28  rickd
61  * Re-enable xmit on flow-controlled and full channel to fix restart hang.
62  * Add some temp spin-lock debug code (rld_spin_owner).
63  *
64  * Revision 1.0  2005/09/28 00:10:06  rickd
65  * Initial release for C4T1E1 support. Lots of transparent
66  * mode updates.
67  *
68  *-----------------------------------------------------------------------------
69  */
70
71 char        SBEid_pmcc4_musyccc[] =
72 "@(#)musycc.c - $Revision: 2.1 $      (c) Copyright 2004-2006 SBE, Inc.";
73
74
75 #include <linux/types.h>
76 #include "pmcc4_sysdep.h"
77 #include <linux/kernel.h>
78 #include <linux/errno.h>
79 #include <linux/init.h>
80 #include "sbecom_inline_linux.h"
81 #include "libsbew.h"
82 #include "pmcc4_private.h"
83 #include "pmcc4.h"
84 #include "musycc.h"
85
86 #ifdef SBE_INCLUDE_SYMBOLS
87 #define STATIC
88 #else
89 #define STATIC  static
90 #endif
91
92 #define sd_find_chan(ci,ch)   c4_find_chan(ch)
93
94
95 /*******************************************************************/
96 /* global driver variables */
97 extern ci_t *c4_list;
98 extern int  drvr_state;
99 extern int  log_level;
100
101 extern int  max_mru;
102 extern int  max_mtu;
103 extern int  max_rxdesc_used;
104 extern int  max_txdesc_used;
105 extern ci_t *CI;                /* dummy pointr to board ZEROE's data - DEBUG
106                                  * USAGE */
107
108
109 /*******************************************************************/
110 /* forward references */
111 void        c4_fifo_free (mpi_t *, int);
112 void        c4_wk_chan_restart (mch_t *);
113 void        musycc_bh_tx_eom (mpi_t *, int);
114 int         musycc_chan_up (ci_t *, int);
115 status_t __init musycc_init (ci_t *);
116 STATIC void __init musycc_init_port (mpi_t *);
117 void        musycc_intr_bh_tasklet (ci_t *);
118 void        musycc_serv_req (mpi_t *, u_int32_t);
119 void        musycc_update_timeslots (mpi_t *);
120
121 /*******************************************************************/
122
123 #if 1
124 STATIC int
125 musycc_dump_rxbuffer_ring (mch_t * ch, int lockit)
126 {
127     struct mdesc *m;
128     unsigned long flags = 0;
129
130     u_int32_t status;
131     int         n;
132
133     if (lockit)
134     {
135         spin_lock_irqsave (&ch->ch_rxlock, flags);
136     }
137     if (ch->rxd_num == 0)
138     {
139         printk ("  ZERO receive buffers allocated for this channel.");
140     } else
141     {
142         FLUSH_MEM_READ ();
143         m = &ch->mdr[ch->rxix_irq_srv];
144         for (n = ch->rxd_num; n; n--)
145         {
146             status = le32_to_cpu (m->status);
147             {
148                 printk ("%c  %08lx[%2d]: sts %08x (%c%c%c%c:%d.) Data [%08x] Next [%08x]\n",
149                         (m == &ch->mdr[ch->rxix_irq_srv]) ? 'F' : ' ',
150                         (unsigned long) m, n,
151                         status,
152                         m->data ? (status & HOST_RX_OWNED ? 'H' : 'M') : '-',
153                         status & POLL_DISABLED ? 'P' : '-',
154                         status & EOBIRQ_ENABLE ? 'b' : '-',
155                         status & EOMIRQ_ENABLE ? 'm' : '-',
156                         status & LENGTH_MASK,
157                         le32_to_cpu (m->data), le32_to_cpu (m->next));
158 #ifdef RLD_DUMP_BUFDATA
159                 {
160                     u_int32_t  *dp;
161                     int         len = status & LENGTH_MASK;
162
163 #if 1
164                     if (m->data && (status & HOST_RX_OWNED))
165 #else
166                     if (m->data)    /* always dump regardless of valid RX
167                                      * data */
168 #endif
169                     {
170                         dp = (u_int32_t *) OS_phystov ((void *) (le32_to_cpu (m->data)));
171                         if (len >= 0x10)
172                             printk ("    %x[%x]: %08X %08X %08X %08x\n", (u_int32_t) dp, len,
173                                     *dp, *(dp + 1), *(dp + 2), *(dp + 3));
174                         else if (len >= 0x08)
175                             printk ("    %x[%x]: %08X %08X\n", (u_int32_t) dp, len,
176                                     *dp, *(dp + 1));
177                         else
178                             printk ("    %x[%x]: %08X\n", (u_int32_t) dp, len, *dp);
179                     }
180                 }
181 #endif
182             }
183             m = m->snext;
184         }
185     }                               /* -for- */
186     printk ("\n");
187
188     if (lockit)
189     {
190         spin_unlock_irqrestore (&ch->ch_rxlock, flags);
191     }
192     return 0;
193 }
194 #endif
195
196 #if 1
197 STATIC int
198 musycc_dump_txbuffer_ring (mch_t * ch, int lockit)
199 {
200     struct mdesc *m;
201     unsigned long flags = 0;
202     u_int32_t   status;
203     int         n;
204
205     if (lockit)
206     {
207         spin_lock_irqsave (&ch->ch_txlock, flags);
208     }
209     if (ch->txd_num == 0)
210     {
211         printk ("  ZERO transmit buffers allocated for this channel.");
212     } else
213     {
214         FLUSH_MEM_READ ();
215         m = ch->txd_irq_srv;
216         for (n = ch->txd_num; n; n--)
217         {
218             status = le32_to_cpu (m->status);
219             {
220                 printk ("%c%c %08lx[%2d]: sts %08x (%c%c%c%c:%d.) Data [%08x] Next [%08x]\n",
221                         (m == ch->txd_usr_add) ? 'F' : ' ',
222                         (m == ch->txd_irq_srv) ? 'L' : ' ',
223                         (unsigned long) m, n,
224                         status,
225                      m->data ? (status & MUSYCC_TX_OWNED ? 'M' : 'H') : '-',
226                         status & POLL_DISABLED ? 'P' : '-',
227                         status & EOBIRQ_ENABLE ? 'b' : '-',
228                         status & EOMIRQ_ENABLE ? 'm' : '-',
229                         status & LENGTH_MASK,
230                         le32_to_cpu (m->data), le32_to_cpu (m->next));
231 #ifdef RLD_DUMP_BUFDATA
232                 {
233                     u_int32_t  *dp;
234                     int         len = status & LENGTH_MASK;
235
236                     if (m->data)
237                     {
238                         dp = (u_int32_t *) OS_phystov ((void *) (le32_to_cpu (m->data)));
239                         if (len >= 0x10)
240                             printk ("    %x[%x]: %08X %08X %08X %08x\n", (u_int32_t) dp, len,
241                                     *dp, *(dp + 1), *(dp + 2), *(dp + 3));
242                         else if (len >= 0x08)
243                             printk ("    %x[%x]: %08X %08X\n", (u_int32_t) dp, len,
244                                     *dp, *(dp + 1));
245                         else
246                             printk ("    %x[%x]: %08X\n", (u_int32_t) dp, len, *dp);
247                     }
248                 }
249 #endif
250             }
251             m = m->snext;
252         }
253     }                               /* -for- */
254     printk ("\n");
255
256     if (lockit)
257     {
258         spin_unlock_irqrestore (&ch->ch_txlock, flags);
259     }
260     return 0;
261 }
262 #endif
263
264
265 /*
266  * The following supports a backdoor debug facility which can be used to
267  * display the state of a board's channel.
268  */
269
270 status_t
271 musycc_dump_ring (ci_t * ci, unsigned int chan)
272 {
273     mch_t      *ch;
274
275     if (chan >= MAX_CHANS_USED)
276     {
277         return SBE_DRVR_FAIL;       /* E2BIG */
278     }
279     {
280         int         bh;
281
282         bh = atomic_read (&ci->bh_pending);
283         printk (">> bh_pend %d [%d] ihead %d itail %d [%d] th_cnt %d bh_cnt %d wdcnt %d note %d\n",
284                 bh, max_bh, ci->iqp_headx, ci->iqp_tailx, max_intcnt,
285                 ci->intlog.drvr_intr_thcount,
286                 ci->intlog.drvr_intr_bhcount,
287                 ci->wdcount, ci->wd_notify);
288         max_bh = 0;                 /* reset counter */
289         max_intcnt = 0;             /* reset counter */
290     }
291
292     if (!(ch = sd_find_chan (dummy, chan)))
293     {
294         printk (">> musycc_dump_ring: channel %d not up.\n", chan);
295         return ENOENT;
296     }
297     printk (">> CI %p CHANNEL %3d @ %p: state %x status/p %x/%x\n", ci, chan, ch, ch->state,
298             ch->status, ch->p.status);
299     printk ("--------------------------------\nTX Buffer Ring - Channel %d, txd_num %d. (bd/ch pend %d %d), TXD required %d, txpkt %lu\n",
300             chan, ch->txd_num,
301             (u_int32_t) atomic_read (&ci->tx_pending), (u_int32_t) atomic_read (&ch->tx_pending), ch->txd_required, ch->s.tx_packets);
302     printk ("++ User 0x%p IRQ_SRV 0x%p USR_ADD 0x%p QStopped %x, start_tx %x tx_full %d txd_free %d mode %x\n",
303             ch->user, ch->txd_irq_srv, ch->txd_usr_add,
304             sd_queue_stopped (ch->user),
305             ch->ch_start_tx, ch->tx_full, ch->txd_free, ch->p.chan_mode);
306     musycc_dump_txbuffer_ring (ch, 1);
307     printk ("RX Buffer Ring - Channel %d, rxd_num %d. IRQ_SRV[%d] 0x%p, start_rx %x rxpkt %lu\n",
308             chan, ch->rxd_num, ch->rxix_irq_srv,
309             &ch->mdr[ch->rxix_irq_srv], ch->ch_start_rx, ch->s.rx_packets);
310     musycc_dump_rxbuffer_ring (ch, 1);
311
312     return SBE_DRVR_SUCCESS;
313 }
314
315
316 status_t
317 musycc_dump_rings (ci_t * ci, unsigned int start_chan)
318 {
319     unsigned int chan;
320
321     for (chan = start_chan; chan < (start_chan + 5); chan++)
322         musycc_dump_ring (ci, chan);
323     return SBE_DRVR_SUCCESS;
324 }
325
326
327 /*
328  * NOTE on musycc_init_mdt():  These MUSYCC writes are only operational after
329  * a MUSYCC GROUP_INIT command has been issued.
330  */
331
332 void
333 musycc_init_mdt (mpi_t * pi)
334 {
335     u_int32_t  *addr, cfg;
336     int         i;
337
338     /*
339      * This Idle Code insertion takes effect prior to channel's first
340      * transmitted  message.  After that, each message contains its own Idle
341      * Code information which is to be issued after the message is
342      * transmitted (Ref.MUSYCC 5.2.2.3: MCENBL bit in Group Configuration
343      * Descriptor).
344      */
345
346     addr = (u_int32_t *) ((u_long) pi->reg + MUSYCC_MDT_BASE03_ADDR);
347     cfg = CFG_CH_FLAG_7E << IDLE_CODE;
348
349     for (i = 0; i < 32; addr++, i++)
350     {
351         pci_write_32 (addr, cfg);
352     }
353 }
354
355
356 /* Set TX thp to the next unprocessed md */
357
358 void
359 musycc_update_tx_thp (mch_t * ch)
360 {
361     struct mdesc *md;
362     unsigned long flags;
363
364     spin_lock_irqsave (&ch->ch_txlock, flags);
365     while (1)
366     {
367         md = ch->txd_irq_srv;
368         FLUSH_MEM_READ ();
369         if (!md->data)
370         {
371             /* No MDs with buffers to process */
372             spin_unlock_irqrestore (&ch->ch_txlock, flags);
373             return;
374         }
375         if ((le32_to_cpu (md->status)) & MUSYCC_TX_OWNED)
376         {
377             /* this is the MD to restart TX with */
378             break;
379         }
380         /*
381          * Otherwise, we have a valid, host-owned message descriptor which
382          * has been successfully transmitted and whose buffer can be freed,
383          * so... process this MD, it's owned by the host.  (This might give
384          * as a new, updated txd_irq_srv.)
385          */
386         musycc_bh_tx_eom (ch->up, ch->gchan);
387     }
388     md = ch->txd_irq_srv;
389     ch->up->regram->thp[ch->gchan] = cpu_to_le32 (OS_vtophys (md));
390     FLUSH_MEM_WRITE ();
391
392     if (ch->tx_full)
393     {
394         ch->tx_full = 0;
395         ch->txd_required = 0;
396         sd_enable_xmit (ch->user);  /* re-enable to catch flow controlled
397                                      * channel */
398     }
399     spin_unlock_irqrestore (&ch->ch_txlock, flags);
400
401 #ifdef RLD_TRANS_DEBUG
402     printk ("++ musycc_update_tx_thp[%d]: setting thp = %p, sts %x\n", ch->channum, md, md->status);
403 #endif
404 }
405
406
407 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,41)
408 /*
409  * This is the workq task executed by the OS when our queue_work() is
410  * scheduled and run.  It can fire off either RX or TX ACTIVATION depending
411  * upon the channel's ch_start_tx and ch_start_rx variables.  This routine
412  * is implemented as a work queue so that the call to the service request is
413  * able to sleep, awaiting an interrupt acknowledgment response (SACK) from
414  * the hardware.
415  */
416
417 void
418 musycc_wq_chan_restart (void *arg)      /* channel private structure */
419 {
420     mch_t      *ch;
421     mpi_t      *pi;
422     struct mdesc *md;
423 #if 0
424     unsigned long flags;
425 #endif
426
427     ch = container_of(arg, struct c4_chan_info, ch_work);
428     pi = ch->up;
429
430 #ifdef RLD_TRANS_DEBUG
431     printk ("wq_chan_restart[%d]: start_RT[%d/%d] status %x\n",
432             ch->channum, ch->ch_start_rx, ch->ch_start_tx, ch->status);
433
434 #endif
435
436     /**********************************/
437     /** check for RX restart request **/
438     /**********************************/
439
440     if ((ch->ch_start_rx) && (ch->status & RX_ENABLED))
441     {
442
443         ch->ch_start_rx = 0;
444 #if defined(RLD_TRANS_DEBUG) || defined(RLD_RXACT_DEBUG)
445         {
446             static int  hereb4 = 7;
447
448             if (hereb4)             /* RLD DEBUG */
449             {
450                 hereb4--;
451 #ifdef RLD_TRANS_DEBUG
452                 md = &ch->mdr[ch->rxix_irq_srv];
453                 printk ("++ musycc_wq_chan_restart[%d] CHAN RX ACTIVATE: rxix_irq_srv %d, md %p sts %x, rxpkt %lu\n",
454                 ch->channum, ch->rxix_irq_srv, md, le32_to_cpu (md->status),
455                         ch->s.rx_packets);
456 #elif defined(RLD_RXACT_DEBUG)
457                 md = &ch->mdr[ch->rxix_irq_srv];
458                 printk ("++ musycc_wq_chan_restart[%d] CHAN RX ACTIVATE: rxix_irq_srv %d, md %p sts %x, rxpkt %lu\n",
459                 ch->channum, ch->rxix_irq_srv, md, le32_to_cpu (md->status),
460                         ch->s.rx_packets);
461                 musycc_dump_rxbuffer_ring (ch, 1);      /* RLD DEBUG */
462 #endif
463             }
464         }
465 #endif
466         musycc_serv_req (pi, SR_CHANNEL_ACTIVATE | SR_RX_DIRECTION | ch->gchan);
467     }
468     /**********************************/
469     /** check for TX restart request **/
470     /**********************************/
471
472     if ((ch->ch_start_tx) && (ch->status & TX_ENABLED))
473     {
474         /* find next unprocessed message, then set TX thp to it */
475         musycc_update_tx_thp (ch);
476
477 #if 0
478         spin_lock_irqsave (&ch->ch_txlock, flags);
479 #endif
480         md = ch->txd_irq_srv;
481         if (!md)
482         {
483 #ifdef RLD_TRANS_DEBUG
484             printk ("-- musycc_wq_chan_restart[%d]: WARNING, starting NULL md\n", ch->channum);
485 #endif
486 #if 0
487             spin_unlock_irqrestore (&ch->ch_txlock, flags);
488 #endif
489         } else if (md->data && ((le32_to_cpu (md->status)) & MUSYCC_TX_OWNED))
490         {
491             ch->ch_start_tx = 0;
492 #if 0
493             spin_unlock_irqrestore (&ch->ch_txlock, flags);   /* allow interrupts for service request */
494 #endif
495 #ifdef RLD_TRANS_DEBUG
496             printk ("++ musycc_wq_chan_restart() CHAN TX ACTIVATE: chan %d txd_irq_srv %p = sts %x, txpkt %lu\n",
497                     ch->channum, ch->txd_irq_srv, ch->txd_irq_srv->status, ch->s.tx_packets);
498 #endif
499             musycc_serv_req (pi, SR_CHANNEL_ACTIVATE | SR_TX_DIRECTION | ch->gchan);
500         }
501 #ifdef RLD_RESTART_DEBUG
502         else
503         {
504             /* retain request to start until retried and we have data to xmit */
505             printk ("-- musycc_wq_chan_restart[%d]: DELAYED due to md %p sts %x data %x, start_tx %x\n",
506                     ch->channum, md,
507                     le32_to_cpu (md->status),
508                     le32_to_cpu (md->data), ch->ch_start_tx);
509             musycc_dump_txbuffer_ring (ch, 0);
510 #if 0
511             spin_unlock_irqrestore (&ch->ch_txlock, flags);   /* allow interrupts for service request */
512 #endif
513         }
514 #endif
515     }
516 }
517 #endif
518
519
520  /*
521   * Channel restart either fires of a workqueue request (2.6) or lodges a
522   * watchdog activation sequence (2.4).
523   */
524
525 void
526 musycc_chan_restart (mch_t * ch)
527 {
528 #ifdef RLD_RESTART_DEBUG
529     printk ("++ musycc_chan_restart[%d]: txd_irq_srv @ %p = sts %x\n",
530             ch->channum, ch->txd_irq_srv, ch->txd_irq_srv->status);
531 #endif
532
533 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,41)
534     /* 2.6 - find next unprocessed message, then set TX thp to it */
535 #ifdef RLD_RESTART_DEBUG
536     printk (">> musycc_chan_restart: scheduling Chan %x workQ @ %p\n", ch->channum, &ch->ch_work);
537 #endif
538     c4_wk_chan_restart (ch);        /* work queue mechanism fires off: Ref:
539                                      * musycc_wq_chan_restart () */
540
541 #else
542
543
544     /* 2.4 - find next unprocessed message, then set TX thp to it */
545 #ifdef RLD_RESTART_DEBUG
546     printk (">> musycc_chan_restart: scheduling Chan %x start_tx %x\n", ch->channum, ch->ch_start_tx);
547 #endif
548     /* restart transmission from background loop */
549     ch->up->up->wd_notify = WD_NOTIFY_1TX;
550 #endif
551 }
552
553
554 #if 0
555 void
556 musycc_cleanup (ci_t * ci)
557 {
558     mpi_t      *pi;
559     int         i, j;
560
561     /* free up driver resources */
562     ci->state = C_INIT;             /* mark as hardware not available */
563
564     for (i = 0; i < ci->max_ports; i++)
565     {
566         pi = &ci->port[i];
567 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,41)
568         c4_wq_port_cleanup (pi);
569 #endif
570         for (j = 0; j < MUSYCC_NCHANS; j++)
571         {
572             if (pi->chan[j])
573                 OS_kfree (pi->chan[j]); /* free mch_t struct */
574         }
575         OS_kfree (pi->regram_saved);
576     }
577 #if 0
578     /* obsolete - watchdog is now static w/in ci_t */
579     OS_free_watchdog (ci->wd);
580 #endif
581     OS_kfree (ci->iqd_p_saved);
582     OS_kfree (ci);
583 }
584 #endif
585
586 void
587 rld_put_led (mpi_t * pi, u_int32_t ledval)
588 {
589     static u_int32_t led = 0;
590
591     if (ledval == 0)
592         led = 0;
593     else
594         led |= ledval;
595
596     pci_write_32 ((u_int32_t *) &pi->up->cpldbase->leds, led);  /* RLD DEBUG TRANHANG */
597 }
598
599
600 #define MUSYCC_SR_RETRY_CNT  9
601
602 void
603 musycc_serv_req (mpi_t * pi, u_int32_t req)
604 {
605     volatile u_int32_t r;
606     int         rcnt;
607
608     /*
609      * PORT NOTE: Semaphore protect service loop guarantees only a single
610      * operation at a time.  Per MUSYCC Manual - "Issuing service requests to
611      * the same channel group without first receiving ACK from each request
612      * may cause the host to lose track of which service request has been
613      * acknowledged."
614      */
615
616     SD_SEM_TAKE (&pi->sr_sem_busy, "serv");     /* only 1 thru here, per
617                                                  * group */
618
619     if (pi->sr_last == req)
620     {
621 #ifdef RLD_TRANS_DEBUG
622         printk (">> same SR, Port %d Req %x\n", pi->portnum, req);
623 #endif
624
625         /*
626          * The most likely repeated request is the channel activation command
627          * which follows the occurrence of a Transparent mode TX ONR or a
628          * BUFF error.  If the previous command was a CHANNEL ACTIVATE,
629          * precede it with a NOOP command in order maintain coherent control
630          * of this current (re)ACTIVATE.
631          */
632
633         r = (pi->sr_last & ~SR_GCHANNEL_MASK);
634         if ((r == (SR_CHANNEL_ACTIVATE | SR_TX_DIRECTION)) ||
635             (r == (SR_CHANNEL_ACTIVATE | SR_RX_DIRECTION)))
636         {
637 #ifdef RLD_TRANS_DEBUG
638             printk (">> same CHAN ACT SR, Port %d Req %x => issue SR_NOOP CMD\n", pi->portnum, req);
639 #endif
640             SD_SEM_GIVE (&pi->sr_sem_busy);     /* allow this next request */
641             musycc_serv_req (pi, SR_NOOP);
642             SD_SEM_TAKE (&pi->sr_sem_busy, "serv");     /* relock & continue w/
643                                                          * original req */
644         } else if (req == SR_NOOP)
645         {
646             /* no need to issue back-to-back SR_NOOP commands at this time */
647 #ifdef RLD_TRANS_DEBUG
648             printk (">> same Port SR_NOOP skipped, Port %d\n", pi->portnum);
649 #endif
650             SD_SEM_GIVE (&pi->sr_sem_busy);     /* allow this next request */
651             return;
652         }
653     }
654     rcnt = 0;
655     pi->sr_last = req;
656 rewrite:
657     pci_write_32 ((u_int32_t *) &pi->reg->srd, req);
658     FLUSH_MEM_WRITE ();
659
660     /*
661      * Per MUSYCC Manual, Section 6.1,2 - "When writing an SCR service
662      * request, the host must ensure at least one PCI bus clock cycle has
663      * elapsed before writing another service request.  To meet this minimum
664      * elapsed service request write timing interval, it is recommended that
665      * the host follow any SCR write with another operation which reads from
666      * the same address."
667      */
668     r = pci_read_32 ((u_int32_t *) &pi->reg->srd);      /* adhere to write
669                                                          * timing imposition */
670
671
672     if ((r != req) && (req != SR_CHIP_RESET) && (++rcnt <= MUSYCC_SR_RETRY_CNT))
673     {
674         if (log_level >= LOG_MONITOR)
675             printk ("%s: %d - reissue srv req/last %x/%x (hdw reads %x), Chan %d.\n",
676                     pi->up->devname, rcnt, req, pi->sr_last, r,
677                     (pi->portnum * MUSYCC_NCHANS) + (req & 0x1f));
678         OS_uwait_dummy ();          /* this delay helps reduce reissue counts
679                                      * (reason not yet researched) */
680         goto rewrite;
681     }
682     if (rcnt > MUSYCC_SR_RETRY_CNT)
683     {
684         printk (KERN_WARNING "%s: failed service request (#%d)= %x, group %d.\n",
685                 pi->up->devname, MUSYCC_SR_RETRY_CNT, req, pi->portnum);
686         SD_SEM_GIVE (&pi->sr_sem_busy); /* allow any next request */
687         return;
688     }
689     if (req == SR_CHIP_RESET)
690     {
691         /*
692          * PORT NOTE: the CHIP_RESET command is NOT ack'd by the MUSYCC, thus
693          * the upcoming delay is used.  Though the MUSYCC documentation
694          * suggests a read-after-write would supply the required delay, it's
695          * unclear what CPU/BUS clock speeds might have been assumed when
696          * suggesting this 'lack of ACK' workaround.  Thus the use of uwait.
697          */
698         OS_uwait (100000, "icard"); /* 100ms */
699     } else
700     {
701         FLUSH_MEM_READ ();
702         SD_SEM_TAKE (&pi->sr_sem_wait, "sakack");       /* sleep until SACK
703                                                          * interrupt occurs */
704     }
705     SD_SEM_GIVE (&pi->sr_sem_busy); /* allow any next request */
706 }
707
708
709 #ifdef  SBE_PMCC4_ENABLE
710 void
711 musycc_update_timeslots (mpi_t * pi)
712 {
713     int         i, ch;
714     char        e1mode = IS_FRAME_ANY_E1 (pi->p.port_mode);
715
716     for (i = 0; i < 32; i++)
717     {
718         int         usedby = 0, last = 0, ts, j, bits[8];
719
720         u_int8_t lastval = 0;
721
722         if (((i == 0) && e1mode) || /* disable if  E1 mode */
723             ((i == 16) && ((pi->p.port_mode == CFG_FRAME_E1CRC_CAS) || (pi->p.port_mode == CFG_FRAME_E1CRC_CAS_AMI)))
724             || ((i > 23) && (!e1mode))) /* disable if T1 mode */
725         {
726             pi->tsm[i] = 0xff;      /* make tslot unavailable for this mode */
727         } else
728         {
729             pi->tsm[i] = 0x00;      /* make tslot available for assignment */
730         }
731         for (j = 0; j < 8; j++)
732             bits[j] = -1;
733         for (ch = 0; ch < MUSYCC_NCHANS; ch++)
734         {
735             if ((pi->chan[ch]->state == UP) && (pi->chan[ch]->p.bitmask[i]))
736             {
737                 usedby++;
738                 last = ch;
739                 lastval = pi->chan[ch]->p.bitmask[i];
740                 for (j = 0; j < 8; j++)
741                     if (lastval & (1 << j))
742                         bits[j] = ch;
743                 pi->tsm[i] |= lastval;
744             }
745         }
746         if (!usedby)
747             ts = 0;
748         else if ((usedby == 1) && (lastval == 0xff))
749             ts = (4 << 5) | last;
750         else if ((usedby == 1) && (lastval == 0x7f))
751             ts = (5 << 5) | last;
752         else
753         {
754             int         idx;
755
756             if (bits[0] < 0)
757                 ts = (6 << 5) | (idx = last);
758             else
759                 ts = (7 << 5) | (idx = bits[0]);
760             for (j = 1; j < 8; j++)
761             {
762                 pi->regram->rscm[idx * 8 + j] = (bits[j] < 0) ? 0 : (0x80 | bits[j]);
763                 pi->regram->tscm[idx * 8 + j] = (bits[j] < 0) ? 0 : (0x80 | bits[j]);
764             }
765         }
766         pi->regram->rtsm[i] = ts;
767         pi->regram->ttsm[i] = ts;
768     }
769     FLUSH_MEM_WRITE ();
770
771     musycc_serv_req (pi, SR_TIMESLOT_MAP | SR_RX_DIRECTION);
772     musycc_serv_req (pi, SR_TIMESLOT_MAP | SR_TX_DIRECTION);
773     musycc_serv_req (pi, SR_SUBCHANNEL_MAP | SR_RX_DIRECTION);
774     musycc_serv_req (pi, SR_SUBCHANNEL_MAP | SR_TX_DIRECTION);
775 }
776 #endif
777
778
779 #ifdef SBE_WAN256T3_ENABLE
780 void
781 musycc_update_timeslots (mpi_t * pi)
782 {
783     mch_t      *ch;
784
785     u_int8_t    ts, hmask, tsen;
786     int         gchan;
787     int         i;
788
789 #ifdef SBE_PMCC4_ENABLE
790     hmask = (0x1f << pi->up->p.hypersize) & 0x1f;
791 #endif
792 #ifdef SBE_WAN256T3_ENABLE
793     hmask = (0x1f << hyperdummy) & 0x1f;
794 #endif
795     for (i = 0; i < 128; i++)
796     {
797         gchan = ((pi->portnum * MUSYCC_NCHANS) + (i & hmask)) % MUSYCC_NCHANS;
798         ch = pi->chan[gchan];
799         if (ch->p.mode_56k)
800             tsen = MODE_56KBPS;
801         else
802             tsen = MODE_64KBPS;     /* also the default */
803         ts = ((pi->portnum % 4) == (i / 32)) ? (tsen << 5) | (i & hmask) : 0;
804         pi->regram->rtsm[i] = ts;
805         pi->regram->ttsm[i] = ts;
806     }
807     FLUSH_MEM_WRITE ();
808     musycc_serv_req (pi, SR_TIMESLOT_MAP | SR_RX_DIRECTION);
809     musycc_serv_req (pi, SR_TIMESLOT_MAP | SR_TX_DIRECTION);
810 }
811 #endif
812
813
814  /*
815   * This routine converts a generic library channel configuration parameter
816   * into a hardware specific register value (IE. MUSYCC CCD Register).
817   */
818 u_int32_t
819 musycc_chan_proto (int proto)
820 {
821     int         reg;
822
823     switch (proto)
824     {
825     case CFG_CH_PROTO_TRANS:        /* 0 */
826         reg = MUSYCC_CCD_TRANS;
827         break;
828     case CFG_CH_PROTO_SS7:          /* 1 */
829         reg = MUSYCC_CCD_SS7;
830         break;
831     default:
832     case CFG_CH_PROTO_ISLP_MODE:   /* 4 */
833     case CFG_CH_PROTO_HDLC_FCS16:  /* 2 */
834         reg = MUSYCC_CCD_HDLC_FCS16;
835         break;
836     case CFG_CH_PROTO_HDLC_FCS32:  /* 3 */
837         reg = MUSYCC_CCD_HDLC_FCS32;
838         break;
839     }
840
841     return reg;
842 }
843
844 #ifdef SBE_WAN256T3_ENABLE
845 STATIC void __init
846 musycc_init_port (mpi_t * pi)
847 {
848     pci_write_32 ((u_int32_t *) &pi->reg->gbp, OS_vtophys (pi->regram));
849
850     pi->regram->grcd =
851         __constant_cpu_to_le32 (MUSYCC_GRCD_RX_ENABLE |
852                                 MUSYCC_GRCD_TX_ENABLE |
853                                 MUSYCC_GRCD_SF_ALIGN |
854                                 MUSYCC_GRCD_SUBCHAN_DISABLE |
855                                 MUSYCC_GRCD_OOFMP_DISABLE |
856                                 MUSYCC_GRCD_COFAIRQ_DISABLE |
857                                 MUSYCC_GRCD_MC_ENABLE |
858                        (MUSYCC_GRCD_POLLTH_32 << MUSYCC_GRCD_POLLTH_SHIFT));
859
860     pi->regram->pcd =
861         __constant_cpu_to_le32 (MUSYCC_PCD_E1X4_MODE |
862                                 MUSYCC_PCD_TXDATA_RISING |
863                                 MUSYCC_PCD_TX_DRIVEN);
864
865     /* Message length descriptor */
866     pi->regram->mld = __constant_cpu_to_le32 (max_mru | (max_mru << 16));
867     FLUSH_MEM_WRITE ();
868
869     musycc_serv_req (pi, SR_GROUP_INIT | SR_RX_DIRECTION);
870     musycc_serv_req (pi, SR_GROUP_INIT | SR_TX_DIRECTION);
871
872     musycc_init_mdt (pi);
873
874     musycc_update_timeslots (pi);
875 }
876 #endif
877
878
879 status_t    __init
880 musycc_init (ci_t * ci)
881 {
882     char       *regaddr;        /* temp for address boundary calculations */
883     int         i, gchan;
884
885     OS_sem_init (&ci->sem_wdbusy, SEM_AVAILABLE);       /* watchdog exclusion */
886
887     /*
888      * Per MUSYCC manual, Section 6.3.4 - "The host must allocate a dword
889      * aligned memory segment for interrupt queue pointers."
890      */
891
892 #define INT_QUEUE_BOUNDARY  4
893
894     regaddr = OS_kmalloc ((INT_QUEUE_SIZE + 1) * sizeof (u_int32_t));
895     if (regaddr == 0)
896         return ENOMEM;
897     ci->iqd_p_saved = regaddr;      /* save orig value for free's usage */
898     ci->iqd_p = (u_int32_t *) ((unsigned long) (regaddr + INT_QUEUE_BOUNDARY - 1) &
899                                (~(INT_QUEUE_BOUNDARY - 1)));    /* this calculates
900                                                                  * closest boundary */
901
902     for (i = 0; i < INT_QUEUE_SIZE; i++)
903     {
904         ci->iqd_p[i] = __constant_cpu_to_le32 (INT_EMPTY_ENTRY);
905     }
906
907     for (i = 0; i < ci->max_port; i++)
908     {
909         mpi_t      *pi = &ci->port[i];
910
911         /*
912          * Per MUSYCC manual, Section 6.3.2 - "The host must allocate a 2KB
913          * bound memory segment for Channel Group 0."
914          */
915
916 #define GROUP_BOUNDARY   0x800
917
918         regaddr = OS_kmalloc (sizeof (struct musycc_groupr) + GROUP_BOUNDARY);
919         if (regaddr == 0)
920         {
921             for (gchan = 0; gchan < i; gchan++)
922             {
923                 pi = &ci->port[gchan];
924                 OS_kfree (pi->reg);
925                 pi->reg = 0;
926             }
927             return ENOMEM;
928         }
929         pi->regram_saved = regaddr; /* save orig value for free's usage */
930         pi->regram = (struct musycc_groupr *) ((unsigned long) (regaddr + GROUP_BOUNDARY - 1) &
931                                                (~(GROUP_BOUNDARY - 1)));        /* this calculates
932                                                                                  * closest boundary */
933     }
934
935     /* any board centric MUSYCC commands will use group ZERO as its "home" */
936     ci->regram = ci->port[0].regram;
937     musycc_serv_req (&ci->port[0], SR_CHIP_RESET);
938
939     pci_write_32 ((u_int32_t *) &ci->reg->gbp, OS_vtophys (ci->regram));
940     pci_flush_write (ci);
941 #ifdef CONFIG_SBE_PMCC4_NCOMM
942     ci->regram->__glcd = __constant_cpu_to_le32 (GCD_MAGIC);
943 #else
944     /* standard driver POLLS for INTB via CPLD register */
945     ci->regram->__glcd = __constant_cpu_to_le32 (GCD_MAGIC | MUSYCC_GCD_INTB_DISABLE);
946 #endif
947
948     ci->regram->__iqp = cpu_to_le32 (OS_vtophys (&ci->iqd_p[0]));
949     ci->regram->__iql = __constant_cpu_to_le32 (INT_QUEUE_SIZE - 1);
950     pci_write_32 ((u_int32_t *) &ci->reg->dacbp, 0);
951     FLUSH_MEM_WRITE ();
952
953     ci->state = C_RUNNING;          /* mark as full interrupt processing
954                                      * available */
955
956     musycc_serv_req (&ci->port[0], SR_GLOBAL_INIT);     /* FIRST INTERRUPT ! */
957
958     /* sanity check settable parameters */
959
960     if (max_mru > 0xffe)
961     {
962         printk (KERN_WARNING "%s: Maximum allowed MRU exceeded, resetting %d to %d.\n",
963                 THIS_MODULE->name, max_mru, 0xffe);
964         max_mru = 0xffe;
965     }
966     if (max_mtu > 0xffe)
967     {
968         printk (KERN_WARNING "%s: Maximum allowed MTU exceeded, resetting %d to %d.\n",
969                 THIS_MODULE->name, max_mtu, 0xffe);
970         max_mtu = 0xffe;
971     }
972 #ifdef SBE_WAN256T3_ENABLE
973     for (i = 0; i < MUSYCC_NPORTS; i++)
974         musycc_init_port (&ci->port[i]);
975 #endif
976
977     return SBE_DRVR_SUCCESS;        /* no error */
978 }
979
980
981 void
982 musycc_bh_tx_eom (mpi_t * pi, int gchan)
983 {
984     mch_t      *ch;
985     struct mdesc *md;
986
987 #if 0
988 #ifndef SBE_ISR_INLINE
989     unsigned long flags;
990
991 #endif
992 #endif
993     volatile u_int32_t status;
994
995     ch = pi->chan[gchan];
996     if (ch == 0 || ch->state != UP)
997     {
998         if (log_level >= LOG_ERROR)
999             printk ("%s: intr: xmit EOM on uninitialized channel %d\n", pi->up->devname, gchan);
1000     }
1001     if (ch == 0 || ch->mdt == 0)
1002         return;                     /* note: mdt==0 implies a malloc()
1003                                      * failure w/in chan_up() routine */
1004
1005 #if 0
1006 #ifdef SBE_ISR_INLINE
1007     spin_lock_irq (&ch->ch_txlock);
1008 #else
1009     spin_lock_irqsave (&ch->ch_txlock, flags);
1010 #endif
1011 #endif
1012     do
1013     {
1014         FLUSH_MEM_READ ();
1015         md = ch->txd_irq_srv;
1016         status = le32_to_cpu (md->status);
1017
1018         /*
1019          * Note: Per MUSYCC Ref 6.4.9, the host does not poll a host-owned
1020          * Transmit Buffer Descriptor during Transparent Mode.
1021          */
1022         if (status & MUSYCC_TX_OWNED)
1023         {
1024             int         readCount, loopCount;
1025
1026             /***********************************************************/
1027             /* HW Bug Fix                                              */
1028             /* ----------                                              */
1029             /* Under certain PCI Bus loading conditions, the data      */
1030             /* associated with an update of Shared Memory is delayed   */
1031             /* relative to its PCI Interrupt.  This is caught when     */
1032             /* the host determines it does not yet OWN the descriptor. */
1033             /***********************************************************/
1034
1035             readCount = 0;
1036             while (status & MUSYCC_TX_OWNED)
1037             {
1038                 for (loopCount = 0; loopCount < 0x30; loopCount++)
1039                     OS_uwait_dummy ();  /* use call to avoid optimization
1040                                          * removal of dummy delay */
1041                 FLUSH_MEM_READ ();
1042                 status = le32_to_cpu (md->status);
1043                 if (readCount++ > 40)
1044                     break;          /* don't wait any longer */
1045             }
1046             if (status & MUSYCC_TX_OWNED)
1047             {
1048                 if (log_level >= LOG_MONITOR)
1049                 {
1050                     printk ("%s: Port %d Chan %2d - unexpected TX msg ownership intr (md %p sts %x)\n",
1051                      pi->up->devname, pi->portnum, ch->channum, md, status);
1052                     printk ("++ User 0x%p IRQ_SRV 0x%p USR_ADD 0x%p QStopped %x, start_tx %x tx_full %d txd_free %d mode %x\n",
1053                             ch->user, ch->txd_irq_srv, ch->txd_usr_add,
1054                             sd_queue_stopped (ch->user),
1055                             ch->ch_start_tx, ch->tx_full, ch->txd_free, ch->p.chan_mode);
1056                     musycc_dump_txbuffer_ring (ch, 0);
1057                 }
1058                 break;              /* Not our mdesc, done */
1059             } else
1060             {
1061                 if (log_level >= LOG_MONITOR)
1062                     printk ("%s: Port %d Chan %2d - recovered TX msg ownership [%d] (md %p sts %x)\n",
1063                             pi->up->devname, pi->portnum, ch->channum, readCount, md, status);
1064             }
1065         }
1066         ch->txd_irq_srv = md->snext;
1067
1068         md->data = 0;
1069         if (md->mem_token != 0)
1070         {
1071             /* upcount channel */
1072             atomic_sub (OS_mem_token_tlen (md->mem_token), &ch->tx_pending);
1073             /* upcount card */
1074             atomic_sub (OS_mem_token_tlen (md->mem_token), &pi->up->tx_pending);
1075 #ifdef SBE_WAN256T3_ENABLE
1076             if (!atomic_read (&pi->up->tx_pending))
1077                 wan256t3_led (pi->up, LED_TX, 0);
1078 #endif
1079
1080 #ifdef CONFIG_SBE_WAN256T3_NCOMM
1081             /* callback that our packet was sent */
1082             {
1083                 int         hdlcnum = (pi->portnum * 32 + gchan);
1084
1085                 if (hdlcnum >= 228)
1086                 {
1087                     if (nciProcess_TX_complete)
1088                         (*nciProcess_TX_complete) (hdlcnum,
1089                                                    getuserbychan (gchan));
1090                 }
1091             }
1092 #endif                              /*** CONFIG_SBE_WAN256T3_NCOMM ***/
1093
1094             OS_mem_token_free_irq (md->mem_token);
1095             md->mem_token = 0;
1096         }
1097         md->status = 0;
1098 #ifdef RLD_TXFULL_DEBUG
1099         if (log_level >= LOG_MONITOR2)
1100             printk ("~~ tx_eom: tx_full %x  txd_free %d -> %d\n", ch->tx_full, ch->txd_free, ch->txd_free + 1);
1101 #endif
1102         ++ch->txd_free;
1103         FLUSH_MEM_WRITE ();
1104
1105         if ((ch->p.chan_mode != CFG_CH_PROTO_TRANS) && (status & EOBIRQ_ENABLE))
1106         {
1107             if (log_level >= LOG_MONITOR)
1108                 printk ("%s: Mode (%x) incorrect EOB status (%x)\n",
1109                         pi->up->devname, ch->p.chan_mode, status);
1110             if ((status & EOMIRQ_ENABLE) == 0)
1111                 break;
1112         }
1113     }
1114     while ((ch->p.chan_mode != CFG_CH_PROTO_TRANS) && ((status & EOMIRQ_ENABLE) == 0));
1115     /*
1116      * NOTE: (The above 'while' is coupled w/ previous 'do', way above.) Each
1117      * Transparent data buffer has the EOB bit, and NOT the EOM bit, set and
1118      * will furthermore have a separate IQD associated with each messages
1119      * buffer.
1120      */
1121
1122     FLUSH_MEM_READ ();
1123     /*
1124      * Smooth flow control hysterisis by maintaining task stoppage until half
1125      * the available write buffers are available.
1126      */
1127     if (ch->tx_full && (ch->txd_free >= (ch->txd_num / 2)))
1128     {
1129         /*
1130          * Then, only releave task stoppage if we actually have enough
1131          * buffers to service the last requested packet.  It may require MORE
1132          * than half the available!
1133          */
1134         if (ch->txd_free >= ch->txd_required)
1135         {
1136
1137 #ifdef RLD_TXFULL_DEBUG
1138             if (log_level >= LOG_MONITOR2)
1139                 printk ("tx_eom[%d]: enable xmit tx_full no more, txd_free %d txd_num/2 %d\n",
1140                         ch->channum,
1141                         ch->txd_free, ch->txd_num / 2);
1142 #endif
1143             ch->tx_full = 0;
1144             ch->txd_required = 0;
1145             sd_enable_xmit (ch->user);  /* re-enable to catch flow controlled
1146                                          * channel */
1147         }
1148     }
1149 #ifdef RLD_TXFULL_DEBUG
1150     else if (ch->tx_full)
1151     {
1152         if (log_level >= LOG_MONITOR2)
1153             printk ("tx_eom[%d]: bypass TX enable though room available? (txd_free %d txd_num/2 %d)\n",
1154                     ch->channum,
1155                     ch->txd_free, ch->txd_num / 2);
1156     }
1157 #endif
1158
1159     FLUSH_MEM_WRITE ();
1160 #if 0
1161 #ifdef SBE_ISR_INLINE
1162     spin_unlock_irq (&ch->ch_txlock);
1163 #else
1164     spin_unlock_irqrestore (&ch->ch_txlock, flags);
1165 #endif
1166 #endif
1167 }
1168
1169
1170 STATIC void
1171 musycc_bh_rx_eom (mpi_t * pi, int gchan)
1172 {
1173     mch_t      *ch;
1174     void       *m, *m2;
1175     struct mdesc *md;
1176     volatile u_int32_t status;
1177     u_int32_t   error;
1178
1179     ch = pi->chan[gchan];
1180     if (ch == 0 || ch->state != UP)
1181     {
1182         if (log_level > LOG_ERROR)
1183             printk ("%s: intr: receive EOM on uninitialized channel %d\n", pi->up->devname, gchan);
1184         return;
1185     }
1186     if (ch->mdr == 0)
1187         return;                     /* can this happen ? */
1188
1189     for (;;)
1190     {
1191         FLUSH_MEM_READ ();
1192         md = &ch->mdr[ch->rxix_irq_srv];
1193         status = le32_to_cpu (md->status);
1194         if (!(status & HOST_RX_OWNED))
1195             break;                  /* Not our mdesc, done */
1196         m = md->mem_token;
1197         error = (status >> 16) & 0xf;
1198         if (error == 0)
1199         {
1200 #ifdef CONFIG_SBE_WAN256T3_NCOMM
1201             int         hdlcnum = (pi->portnum * 32 + gchan);
1202
1203             /*
1204              * if the packet number belongs to NCOMM, then send it to the TMS
1205              * driver
1206              */
1207             if (hdlcnum >= 228)
1208             {
1209                 if (nciProcess_RX_packet)
1210                     (*nciProcess_RX_packet) (hdlcnum, status & 0x3fff, m, ch->user);
1211             } else
1212 #endif                              /*** CONFIG_SBE_WAN256T3_NCOMM ***/
1213
1214             {
1215                 if ((m2 = OS_mem_token_alloc (max_mru)))
1216                 {
1217                     /* substitute the mbuf+cluster */
1218                     md->mem_token = m2;
1219                     md->data = cpu_to_le32 (OS_vtophys (OS_mem_token_data (m2)));
1220
1221                     /* pass the received mbuf upward */
1222                     sd_recv_consume (m, status & LENGTH_MASK, ch->user);
1223                     ch->s.rx_packets++;
1224                     ch->s.rx_bytes += status & LENGTH_MASK;
1225                 } else
1226                 {
1227                     ch->s.rx_dropped++;
1228                 }
1229             }
1230         } else if (error == ERR_FCS)
1231         {
1232             ch->s.rx_crc_errors++;
1233         } else if (error == ERR_ALIGN)
1234         {
1235             ch->s.rx_missed_errors++;
1236         } else if (error == ERR_ABT)
1237         {
1238             ch->s.rx_missed_errors++;
1239         } else if (error == ERR_LNG)
1240         {
1241             ch->s.rx_length_errors++;
1242         } else if (error == ERR_SHT)
1243         {
1244             ch->s.rx_length_errors++;
1245         }
1246         FLUSH_MEM_WRITE ();
1247         status = max_mru;
1248         if (ch->p.chan_mode == CFG_CH_PROTO_TRANS)
1249             status |= EOBIRQ_ENABLE;
1250         md->status = cpu_to_le32 (status);
1251
1252         /* Check next mdesc in the ring */
1253         if (++ch->rxix_irq_srv >= ch->rxd_num)
1254             ch->rxix_irq_srv = 0;
1255         FLUSH_MEM_WRITE ();
1256     }
1257 }
1258
1259
1260 irqreturn_t
1261 musycc_intr_th_handler (void *devp)
1262 {
1263     ci_t       *ci = (ci_t *) devp;
1264     volatile u_int32_t status, currInt = 0;
1265     u_int32_t   nextInt, intCnt;
1266
1267     /*
1268      * Hardware not available, potential interrupt hang.  But since interrupt
1269      * might be shared, just return.
1270      */
1271     if (ci->state == C_INIT)
1272     {
1273         return IRQ_NONE;
1274     }
1275     /*
1276      * Marked as hardware available. Don't service interrupts, just clear the
1277      * event.
1278      */
1279
1280     if (ci->state == C_IDLE)
1281     {
1282         status = pci_read_32 ((u_int32_t *) &ci->reg->isd);
1283
1284         /* clear the interrupt but process nothing else */
1285         pci_write_32 ((u_int32_t *) &ci->reg->isd, status);
1286         return IRQ_HANDLED;
1287     }
1288     FLUSH_PCI_READ ();
1289     FLUSH_MEM_READ ();
1290
1291     status = pci_read_32 ((u_int32_t *) &ci->reg->isd);
1292     nextInt = INTRPTS_NEXTINT (status);
1293     intCnt = INTRPTS_INTCNT (status);
1294     ci->intlog.drvr_intr_thcount++;
1295
1296     /*********************************************************/
1297     /* HW Bug Fix                                            */
1298     /* ----------                                            */
1299     /* Under certain PCI Bus loading conditions, the         */
1300     /* MUSYCC looses the data associated with an update      */
1301     /* of its ISD and erroneously returns the immediately    */
1302     /* preceding 'nextInt' value.  However, the 'intCnt'     */
1303     /* value appears to be correct.  By not starting service */
1304     /* where the 'missing' 'nextInt' SHOULD point causes     */
1305     /* the IQD not to be serviced - the 'not serviced'       */
1306     /* entries then remain and continue to increase as more  */
1307     /* incorrect ISD's are encountered.                      */
1308     /*********************************************************/
1309
1310     if (nextInt != INTRPTS_NEXTINT (ci->intlog.this_status_new))
1311     {
1312         if (log_level >= LOG_MONITOR)
1313         {
1314             printk ("%s: note - updated ISD from %08x to %08x\n",
1315                     ci->devname, status,
1316               (status & (~INTRPTS_NEXTINT_M)) | ci->intlog.this_status_new);
1317         }
1318         /*
1319          * Replace bogus status with software corrected value.
1320          *
1321          * It's not known whether, during this problem occurrence, if the
1322          * INTFULL bit is correctly reported or not.
1323          */
1324         status = (status & (~INTRPTS_NEXTINT_M)) | (ci->intlog.this_status_new);
1325         nextInt = INTRPTS_NEXTINT (status);
1326     }
1327     /**********************************************/
1328     /* Cn847x Bug Fix                             */
1329     /* --------------                             */
1330     /* Fix for inability to write back same index */
1331     /* as read for a full interrupt queue.        */
1332     /**********************************************/
1333
1334     if (intCnt == INT_QUEUE_SIZE)
1335     {
1336         currInt = ((intCnt - 1) + nextInt) & (INT_QUEUE_SIZE - 1);
1337     } else
1338         /************************************************/
1339         /* Interrupt Write Location Issues              */
1340         /* -------------------------------              */
1341         /* When the interrupt status descriptor is      */
1342         /* written, the interrupt line is de-asserted   */
1343         /* by the Cn847x.  In the case of MIPS          */
1344         /* microprocessors, this must occur at the      */
1345         /* beginning of the interrupt handler so that   */
1346         /* the interrupt handle is not re-entered due   */
1347         /* to interrupt dis-assertion latency.          */
1348         /* In the case of all other processors, this    */
1349         /* action should occur at the end of the        */
1350         /* interrupt handler to avoid overwriting the   */
1351         /* interrupt queue.                             */
1352         /************************************************/
1353
1354     if (intCnt)
1355     {
1356         currInt = (intCnt + nextInt) & (INT_QUEUE_SIZE - 1);
1357     } else
1358     {
1359         /*
1360          * NOTE: Servicing an interrupt whose ISD contains a count of ZERO
1361          * can be indicative of a Shared Interrupt chain.  Our driver can be
1362          * called from the system's interrupt handler as a matter of the OS
1363          * walking the chain.  As the chain is walked, the interrupt will
1364          * eventually be serviced by the correct driver/handler.
1365          */
1366 #if 0
1367         /* chained interrupt = not ours */
1368         printk (">> %s: intCnt NULL, sts %x, possibly a chained interrupt!\n",
1369                 ci->devname, status);
1370 #endif
1371         return IRQ_NONE;
1372     }
1373
1374     ci->iqp_tailx = currInt;
1375
1376     currInt <<= INTRPTS_NEXTINT_S;
1377     ci->intlog.last_status_new = ci->intlog.this_status_new;
1378     ci->intlog.this_status_new = currInt;
1379
1380     if ((log_level >= LOG_WARN) && (status & INTRPTS_INTFULL_M))
1381     {
1382         printk ("%s: Interrupt queue full condition occurred\n", ci->devname);
1383     }
1384     if (log_level >= LOG_DEBUG)
1385         printk ("%s: interrupts pending, isd @ 0x%p: %x curr %d cnt %d NEXT %d\n",
1386                 ci->devname, &ci->reg->isd,
1387         status, nextInt, intCnt, (intCnt + nextInt) & (INT_QUEUE_SIZE - 1));
1388
1389     FLUSH_MEM_WRITE ();
1390 #if defined(SBE_ISR_TASKLET)
1391     pci_write_32 ((u_int32_t *) &ci->reg->isd, currInt);
1392     atomic_inc (&ci->bh_pending);
1393     tasklet_schedule (&ci->ci_musycc_isr_tasklet);
1394 #elif defined(SBE_ISR_IMMEDIATE)
1395     pci_write_32 ((u_int32_t *) &ci->reg->isd, currInt);
1396     atomic_inc (&ci->bh_pending);
1397     queue_task (&ci->ci_musycc_isr_tq, &tq_immediate);
1398     mark_bh (IMMEDIATE_BH);
1399 #elif defined(SBE_ISR_INLINE)
1400     (void) musycc_intr_bh_tasklet (ci);
1401     pci_write_32 ((u_int32_t *) &ci->reg->isd, currInt);
1402 #endif
1403     return IRQ_HANDLED;
1404 }
1405
1406
1407 #if defined(SBE_ISR_IMMEDIATE)
1408 unsigned long
1409 #else
1410 void
1411 #endif
1412 musycc_intr_bh_tasklet (ci_t * ci)
1413 {
1414     mpi_t      *pi;
1415     mch_t      *ch;
1416     unsigned int intCnt;
1417     volatile u_int32_t currInt = 0;
1418     volatile unsigned int headx, tailx;
1419     int         readCount, loopCount;
1420     int         group, gchan, event, err, tx;
1421     u_int32_t   badInt = INT_EMPTY_ENTRY;
1422     u_int32_t   badInt2 = INT_EMPTY_ENTRY2;
1423
1424     /*
1425      * Hardware not available, potential interrupt hang.  But since interrupt
1426      * might be shared, just return.
1427      */
1428     if ((drvr_state != SBE_DRVR_AVAILABLE) || (ci->state == C_INIT))
1429     {
1430 #if defined(SBE_ISR_IMMEDIATE)
1431         return 0L;
1432 #else
1433         return;
1434 #endif
1435     }
1436 #if defined(SBE_ISR_TASKLET) || defined(SBE_ISR_IMMEDIATE)
1437     if (drvr_state != SBE_DRVR_AVAILABLE)
1438     {
1439 #if defined(SBE_ISR_TASKLET)
1440         return;
1441 #elif defined(SBE_ISR_IMMEDIATE)
1442         return 0L;
1443 #endif
1444     }
1445 #elif defined(SBE_ISR_INLINE)
1446     /* no semaphore taken, no double checks */
1447 #endif
1448
1449     ci->intlog.drvr_intr_bhcount++;
1450     FLUSH_MEM_READ ();
1451     {
1452         unsigned int bh = atomic_read (&ci->bh_pending);
1453
1454         max_bh = max (bh, max_bh);
1455     }
1456     atomic_set (&ci->bh_pending, 0);/* if here, no longer pending */
1457     while ((headx = ci->iqp_headx) != (tailx = ci->iqp_tailx))
1458     {
1459         intCnt = (tailx >= headx) ? (tailx - headx) : (tailx - headx + INT_QUEUE_SIZE);
1460         currInt = le32_to_cpu (ci->iqd_p[headx]);
1461
1462         max_intcnt = max (intCnt, max_intcnt);  /* RLD DEBUG */
1463
1464         /**************************************************/
1465         /* HW Bug Fix                                     */
1466         /* ----------                                     */
1467         /* The following code checks for the condition    */
1468         /* of interrupt assertion before interrupt        */
1469         /* queue update.  This is a problem on several    */
1470         /* PCI-Local bridge chips found on some products. */
1471         /**************************************************/
1472
1473         readCount = 0;
1474         if ((currInt == badInt) || (currInt == badInt2))
1475             ci->intlog.drvr_int_failure++;
1476
1477         while ((currInt == badInt) || (currInt == badInt2))
1478         {
1479             for (loopCount = 0; loopCount < 0x30; loopCount++)
1480                 OS_uwait_dummy ();  /* use call to avoid optimization removal
1481                                      * of dummy delay */
1482             FLUSH_MEM_READ ();
1483             currInt = le32_to_cpu (ci->iqd_p[headx]);
1484             if (readCount++ > 20)
1485                 break;
1486         }
1487
1488         if ((currInt == badInt) || (currInt == badInt2))        /* catch failure of Bug
1489                                                                  * Fix checking */
1490         {
1491             if (log_level >= LOG_WARN)
1492                 printk ("%s: Illegal Interrupt Detected @ 0x%p, mod %d.)\n",
1493                         ci->devname, &ci->iqd_p[headx], headx);
1494
1495             /*
1496              * If the descriptor has not recovered, then leaving the EMPTY
1497              * entry set will not signal to the MUSYCC that this descriptor
1498              * has been serviced. The Interrupt Queue can then start loosing
1499              * available descriptors and MUSYCC eventually encounters and
1500              * reports the INTFULL condition.  Per manual, changing any bit
1501              * marks descriptor as available, thus the use of different
1502              * EMPTY_ENTRY values.
1503              */
1504
1505             if (currInt == badInt)
1506             {
1507                 ci->iqd_p[headx] = __constant_cpu_to_le32 (INT_EMPTY_ENTRY2);
1508             } else
1509             {
1510                 ci->iqd_p[headx] = __constant_cpu_to_le32 (INT_EMPTY_ENTRY);
1511             }
1512             ci->iqp_headx = (headx + 1) & (INT_QUEUE_SIZE - 1); /* insure wrapness */
1513             FLUSH_MEM_WRITE ();
1514             FLUSH_MEM_READ ();
1515             continue;
1516         }
1517         group = INTRPT_GRP (currInt);
1518         gchan = INTRPT_CH (currInt);
1519         event = INTRPT_EVENT (currInt);
1520         err = INTRPT_ERROR (currInt);
1521         tx = currInt & INTRPT_DIR_M;
1522
1523         ci->iqd_p[headx] = __constant_cpu_to_le32 (INT_EMPTY_ENTRY);
1524         FLUSH_MEM_WRITE ();
1525
1526         if (log_level >= LOG_DEBUG)
1527         {
1528             if (err != 0)
1529                 printk (" %08x -> err: %2d,", currInt, err);
1530
1531             printk ("+ interrupt event: %d, grp: %d, chan: %2d, side: %cX\n",
1532                     event, group, gchan, tx ? 'T' : 'R');
1533         }
1534         pi = &ci->port[group];      /* notice that here we assume 1-1 group -
1535                                      * port mapping */
1536         ch = pi->chan[gchan];
1537         switch (event)
1538         {
1539         case EVE_SACK:              /* Service Request Acknowledge */
1540             if (log_level >= LOG_DEBUG)
1541             {
1542                 volatile u_int32_t r;
1543
1544                 r = pci_read_32 ((u_int32_t *) &pi->reg->srd);
1545                 printk ("- SACK cmd: %08x (hdw= %08x)\n", pi->sr_last, r);
1546             }
1547             SD_SEM_GIVE (&pi->sr_sem_wait);     /* wake up waiting process */
1548             break;
1549         case EVE_CHABT:     /* Change To Abort Code (0x7e -> 0xff) */
1550         case EVE_CHIC:              /* Change To Idle Code (0xff -> 0x7e) */
1551             break;
1552         case EVE_EOM:               /* End Of Message */
1553         case EVE_EOB:               /* End Of Buffer (Transparent mode) */
1554             if (tx)
1555             {
1556                 musycc_bh_tx_eom (pi, gchan);
1557             } else
1558             {
1559                 musycc_bh_rx_eom (pi, gchan);
1560             }
1561 #if 0
1562             break;
1563 #else
1564             /*
1565              * MUSYCC Interrupt Descriptor section states that EOB and EOM
1566              * can be combined with the NONE error (as well as others).  So
1567              * drop thru to catch this...
1568              */
1569 #endif
1570         case EVE_NONE:
1571             if (err == ERR_SHT)
1572             {
1573                 ch->s.rx_length_errors++;
1574             }
1575             break;
1576         default:
1577             if (log_level >= LOG_WARN)
1578                 printk ("%s: unexpected interrupt event: %d, iqd[%d]: %08x, port: %d\n", ci->devname,
1579                         event, headx, currInt, group);
1580             break;
1581         }                           /* switch on event */
1582
1583
1584         /*
1585          * Per MUSYCC Manual, Section 6.4.8.3 [Transmit Errors], TX errors
1586          * are service-affecting and require action to resume normal
1587          * bit-level processing.
1588          */
1589
1590         switch (err)
1591         {
1592         case ERR_ONR:
1593             /*
1594              * Per MUSYCC manual, Section  6.4.8.3 [Transmit Errors], this
1595              * error requires Transmit channel reactivation.
1596              *
1597              * Per MUSYCC manual, Section  6.4.8.4 [Receive Errors], this error
1598              * requires Receive channel reactivation.
1599              */
1600             if (tx)
1601             {
1602
1603                 /*
1604                  * TX ONR Error only occurs when channel is configured for
1605                  * Transparent Mode.  However, this code will catch and
1606                  * re-activate on ANY TX ONR error.
1607                  */
1608
1609                 /*
1610                  * Set flag to re-enable on any next transmit attempt.
1611                  */
1612                 ch->ch_start_tx = CH_START_TX_ONR;
1613
1614                 {
1615 #ifdef RLD_TRANS_DEBUG
1616                     if (1 || log_level >= LOG_MONITOR)
1617 #else
1618                     if (log_level >= LOG_MONITOR)
1619 #endif
1620                     {
1621                         printk ("%s: TX buffer underflow [ONR] on channel %d, mode %x QStopped %x free %d\n",
1622                                 ci->devname, ch->channum, ch->p.chan_mode, sd_queue_stopped (ch->user), ch->txd_free);
1623 #ifdef RLD_DEBUG
1624                         if (ch->p.chan_mode == 2)       /* problem = ONR on HDLC
1625                                                          * mode */
1626                         {
1627                             printk ("++ Failed Last %x Next %x QStopped %x, start_tx %x tx_full %d txd_free %d mode %x\n",
1628                                     (u_int32_t) ch->txd_irq_srv, (u_int32_t) ch->txd_usr_add,
1629                                     sd_queue_stopped (ch->user),
1630                                     ch->ch_start_tx, ch->tx_full, ch->txd_free, ch->p.chan_mode);
1631                             musycc_dump_txbuffer_ring (ch, 0);
1632                         }
1633 #endif
1634                     }
1635                 }
1636             } else                  /* RX buffer overrun */
1637             {
1638                 /*
1639                  * Per MUSYCC manual, Section 6.4.8.4 [Receive Errors],
1640                  * channel recovery for this RX ONR error IS required.  It is
1641                  * also suggested to increase the number of receive buffers
1642                  * for this channel.  Receive channel reactivation IS
1643                  * required, and data has been lost.
1644                  */
1645                 ch->s.rx_over_errors++;
1646                 ch->ch_start_rx = CH_START_RX_ONR;
1647
1648                 if (log_level >= LOG_WARN)
1649                 {
1650                     printk ("%s: RX buffer overflow [ONR] on channel %d, mode %x\n",
1651                             ci->devname, ch->channum, ch->p.chan_mode);
1652                     //musycc_dump_rxbuffer_ring (ch, 0);        /* RLD DEBUG */
1653                 }
1654             }
1655             musycc_chan_restart (ch);
1656             break;
1657         case ERR_BUF:
1658             if (tx)
1659             {
1660                 ch->s.tx_fifo_errors++;
1661                 ch->ch_start_tx = CH_START_TX_BUF;
1662                 /*
1663                  * Per MUSYCC manual, Section  6.4.8.3 [Transmit Errors],
1664                  * this BUFF error requires Transmit channel reactivation.
1665                  */
1666                 if (log_level >= LOG_MONITOR)
1667                     printk ("%s: TX buffer underrun [BUFF] on channel %d, mode %x\n",
1668                             ci->devname, ch->channum, ch->p.chan_mode);
1669             } else                  /* RX buffer overrun */
1670             {
1671                 ch->s.rx_over_errors++;
1672                 /*
1673                  * Per MUSYCC manual, Section 6.4.8.4 [Receive Errors], HDLC
1674                  * mode requires NO recovery for this RX BUFF error is
1675                  * required.  It is suggested to increase the FIFO buffer
1676                  * space for this channel.  Receive channel reactivation is
1677                  * not required, but data has been lost.
1678                  */
1679                 if (log_level >= LOG_WARN)
1680                     printk ("%s: RX buffer overrun [BUFF] on channel %d, mode %x\n",
1681                             ci->devname, ch->channum, ch->p.chan_mode);
1682                 /*
1683                  * Per MUSYCC manual, Section 6.4.9.4 [Receive Errors],
1684                  * Transparent mode DOES require recovery for the RX BUFF
1685                  * error.  It is suggested to increase the FIFO buffer space
1686                  * for this channel.  Receive channel reactivation IS
1687                  * required and data has been lost.
1688                  */
1689                 if (ch->p.chan_mode == CFG_CH_PROTO_TRANS)
1690                     ch->ch_start_rx = CH_START_RX_BUF;
1691             }
1692
1693             if (tx || (ch->p.chan_mode == CFG_CH_PROTO_TRANS))
1694                 musycc_chan_restart (ch);
1695             break;
1696         default:
1697             break;
1698         }                           /* switch on err */
1699
1700         /* Check for interrupt lost condition */
1701         if ((currInt & INTRPT_ILOST_M) && (log_level >= LOG_ERROR))
1702         {
1703             printk ("%s: Interrupt queue overflow - ILOST asserted\n",
1704                     ci->devname);
1705         }
1706         ci->iqp_headx = (headx + 1) & (INT_QUEUE_SIZE - 1);     /* insure wrapness */
1707         FLUSH_MEM_WRITE ();
1708         FLUSH_MEM_READ ();
1709     }                               /* while */
1710     if ((log_level >= LOG_MONITOR2) && (ci->iqp_headx != ci->iqp_tailx))
1711     {
1712         int         bh;
1713
1714         bh = atomic_read (&CI->bh_pending);
1715         printk ("_bh_: late arrivals, head %d != tail %d, pending %d\n",
1716                 ci->iqp_headx, ci->iqp_tailx, bh);
1717     }
1718 #if defined(SBE_ISR_IMMEDIATE)
1719     return 0L;
1720 #endif
1721     /* else, nothing returned */
1722 }
1723
1724 #if 0
1725 int         __init
1726 musycc_new_chan (ci_t * ci, int channum, void *user)
1727 {
1728     mch_t      *ch;
1729
1730     ch = ci->port[channum / MUSYCC_NCHANS].chan[channum % MUSYCC_NCHANS];
1731
1732     if (ch->state != UNASSIGNED)
1733         return EEXIST;
1734     /* NOTE: mch_t already cleared during OS_kmalloc() */
1735     ch->state = DOWN;
1736     ch->user = user;
1737 #if 0
1738     ch->status = 0;
1739     ch->p.status = 0;
1740     ch->p.intr_mask = 0;
1741 #endif
1742     ch->p.chan_mode = CFG_CH_PROTO_HDLC_FCS16;
1743     ch->p.idlecode = CFG_CH_FLAG_7E;
1744     ch->p.pad_fill_count = 2;
1745     spin_lock_init (&ch->ch_rxlock);
1746     spin_lock_init (&ch->ch_txlock);
1747
1748     return 0;
1749 }
1750 #endif
1751
1752
1753 #ifdef SBE_PMCC4_ENABLE
1754 status_t
1755 musycc_chan_down (ci_t * dummy, int channum)
1756 {
1757     mpi_t      *pi;
1758     mch_t      *ch;
1759     int         i, gchan;
1760
1761     if (!(ch = sd_find_chan (dummy, channum)))
1762         return EINVAL;
1763     pi = ch->up;
1764     gchan = ch->gchan;
1765
1766     /* Deactivate the channel */
1767     musycc_serv_req (pi, SR_CHANNEL_DEACTIVATE | SR_RX_DIRECTION | gchan);
1768     ch->ch_start_rx = 0;
1769     musycc_serv_req (pi, SR_CHANNEL_DEACTIVATE | SR_TX_DIRECTION | gchan);
1770     ch->ch_start_tx = 0;
1771
1772     if (ch->state == DOWN)
1773         return 0;
1774     ch->state = DOWN;
1775
1776     pi->regram->thp[gchan] = 0;
1777     pi->regram->tmp[gchan] = 0;
1778     pi->regram->rhp[gchan] = 0;
1779     pi->regram->rmp[gchan] = 0;
1780     FLUSH_MEM_WRITE ();
1781     for (i = 0; i < ch->txd_num; i++)
1782     {
1783         if (ch->mdt[i].mem_token != 0)
1784             OS_mem_token_free (ch->mdt[i].mem_token);
1785     }
1786
1787     for (i = 0; i < ch->rxd_num; i++)
1788     {
1789         if (ch->mdr[i].mem_token != 0)
1790             OS_mem_token_free (ch->mdr[i].mem_token);
1791     }
1792
1793     OS_kfree (ch->mdr);
1794     ch->mdr = 0;
1795     ch->rxd_num = 0;
1796     OS_kfree (ch->mdt);
1797     ch->mdt = 0;
1798     ch->txd_num = 0;
1799
1800     musycc_update_timeslots (pi);
1801     c4_fifo_free (pi, ch->gchan);
1802
1803     pi->openchans--;
1804     return 0;
1805 }
1806 #endif
1807
1808
1809 int
1810 musycc_del_chan (ci_t * ci, int channum)
1811 {
1812     mch_t      *ch;
1813
1814     if ((channum < 0) || (channum >= (MUSYCC_NPORTS * MUSYCC_NCHANS)))  /* sanity chk param */
1815         return ECHRNG;
1816     if (!(ch = sd_find_chan (ci, channum)))
1817         return ENOENT;
1818     if (ch->state == UP)
1819         musycc_chan_down (ci, channum);
1820     ch->state = UNASSIGNED;
1821     return 0;
1822 }
1823
1824
1825 int
1826 musycc_del_chan_stats (ci_t * ci, int channum)
1827 {
1828     mch_t      *ch;
1829
1830     if (channum < 0 || channum >= (MUSYCC_NPORTS * MUSYCC_NCHANS))      /* sanity chk param */
1831         return ECHRNG;
1832     if (!(ch = sd_find_chan (ci, channum)))
1833         return ENOENT;
1834
1835     memset (&ch->s, 0, sizeof (struct sbecom_chan_stats));
1836     return 0;
1837 }
1838
1839
1840 int
1841 musycc_start_xmit (ci_t * ci, int channum, void *mem_token)
1842 {
1843     mch_t      *ch;
1844     struct mdesc *md;
1845     void       *m2;
1846 #if 0
1847     unsigned long flags;
1848 #endif
1849     int         txd_need_cnt;
1850     u_int32_t   len;
1851
1852     if (!(ch = sd_find_chan (ci, channum)))
1853         return ENOENT;
1854
1855     if (ci->state != C_RUNNING)     /* full interrupt processing available */
1856         return EINVAL;
1857     if (ch->state != UP)
1858         return EINVAL;
1859
1860     if (!(ch->status & TX_ENABLED))
1861         return EROFS;               /* how else to flag unwritable state ? */
1862
1863 #ifdef RLD_TRANS_DEBUGx
1864     if (1 || log_level >= LOG_MONITOR2)
1865 #else
1866     if (log_level >= LOG_MONITOR2)
1867 #endif
1868     {
1869         printk ("++ start_xmt[%d]: state %x start %x full %d free %d required %d stopped %x\n",
1870                 channum, ch->state, ch->ch_start_tx, ch->tx_full,
1871                 ch->txd_free, ch->txd_required, sd_queue_stopped (ch->user));
1872     }
1873     /***********************************************/
1874     /** Determine total amount of data to be sent **/
1875     /***********************************************/
1876     m2 = mem_token;
1877     txd_need_cnt = 0;
1878     for (len = OS_mem_token_tlen (m2); len > 0;
1879          m2 = (void *) OS_mem_token_next (m2))
1880     {
1881         if (!OS_mem_token_len (m2))
1882             continue;
1883         txd_need_cnt++;
1884         len -= OS_mem_token_len (m2);
1885     }
1886
1887     if (txd_need_cnt == 0)
1888     {
1889         if (log_level >= LOG_MONITOR2)
1890             printk ("%s channel %d: no TX data in User buffer\n", ci->devname, channum);
1891         OS_mem_token_free (mem_token);
1892         return 0;                   /* no data to send */
1893     }
1894     /*************************************************/
1895     /** Are there sufficient descriptors available? **/
1896     /*************************************************/
1897     if (txd_need_cnt > ch->txd_num) /* never enough descriptors for this
1898                                      * large a buffer */
1899     {
1900         if (log_level >= LOG_DEBUG)
1901         {
1902             printk ("start_xmit: discarding buffer, insufficient descriptor cnt %d, need %d.\n",
1903                     ch->txd_num, txd_need_cnt + 1);
1904         }
1905         ch->s.tx_dropped++;
1906         OS_mem_token_free (mem_token);
1907         return 0;
1908     }
1909 #if 0
1910     spin_lock_irqsave (&ch->ch_txlock, flags);
1911 #endif
1912     /************************************************************/
1913     /** flow control the line if not enough descriptors remain **/
1914     /************************************************************/
1915     if (txd_need_cnt > ch->txd_free)
1916     {
1917         if (log_level >= LOG_MONITOR2)
1918         {
1919             printk ("start_xmit[%d]: EBUSY - need more descriptors, have %d of %d need %d\n",
1920                     channum, ch->txd_free, ch->txd_num, txd_need_cnt);
1921         }
1922         ch->tx_full = 1;
1923         ch->txd_required = txd_need_cnt;
1924         sd_disable_xmit (ch->user);
1925 #if 0
1926         spin_unlock_irqrestore (&ch->ch_txlock, flags);
1927 #endif
1928         return EBUSY;               /* tell user to try again later */
1929     }
1930     /**************************************************/
1931     /** Put the user data into MUSYCC data buffer(s) **/
1932     /**************************************************/
1933     m2 = mem_token;
1934     md = ch->txd_usr_add;           /* get current available descriptor */
1935
1936     for (len = OS_mem_token_tlen (m2); len > 0; m2 = OS_mem_token_next (m2))
1937     {
1938         int         u = OS_mem_token_len (m2);
1939
1940         if (!u)
1941             continue;
1942         len -= u;
1943
1944         /*
1945          * Enable following chunks, yet wait to enable the FIRST chunk until
1946          * after ALL subsequent chunks are setup.
1947          */
1948         if (md != ch->txd_usr_add)  /* not first chunk */
1949             u |= MUSYCC_TX_OWNED;   /* transfer ownership from HOST to MUSYCC */
1950
1951         if (len)                    /* not last chunk */
1952             u |= EOBIRQ_ENABLE;
1953         else if (ch->p.chan_mode == CFG_CH_PROTO_TRANS)
1954         {
1955             /*
1956              * Per MUSYCC Ref 6.4.9 for Transparent Mode, the host must
1957              * always clear EOMIRQ_ENABLE in every Transmit Buffer Descriptor
1958              * (IE. don't set herein).
1959              */
1960             u |= EOBIRQ_ENABLE;
1961         } else
1962             u |= EOMIRQ_ENABLE;     /* EOM, last HDLC chunk */
1963
1964
1965         /* last chunk in hdlc mode */
1966         u |= (ch->p.idlecode << IDLE_CODE);
1967         if (ch->p.pad_fill_count)
1968         {
1969 #if 0
1970             /* NOOP NOTE: u_int8_t cannot be > 0xFF */
1971             /* sanitize pad_fill_count for maximums allowed by hardware */
1972             if (ch->p.pad_fill_count > EXTRA_FLAGS_MASK)
1973                 ch->p.pad_fill_count = EXTRA_FLAGS_MASK;
1974 #endif
1975             u |= (PADFILL_ENABLE | (ch->p.pad_fill_count << EXTRA_FLAGS));
1976         }
1977         md->mem_token = len ? 0 : mem_token;    /* Fill in mds on last
1978                                                  * segment, others set ZERO
1979                                                  * so that entire token is
1980                                                  * removed ONLY when ALL
1981                                                  * segments have been
1982                                                  * transmitted. */
1983
1984         md->data = cpu_to_le32 (OS_vtophys (OS_mem_token_data (m2)));
1985         FLUSH_MEM_WRITE ();
1986         md->status = cpu_to_le32 (u);
1987         --ch->txd_free;
1988         md = md->snext;
1989     }
1990     FLUSH_MEM_WRITE ();
1991
1992
1993     /*
1994      * Now transfer ownership of first chunk from HOST to MUSYCC in order to
1995      * fire-off this XMIT.
1996      */
1997     ch->txd_usr_add->status |= __constant_cpu_to_le32 (MUSYCC_TX_OWNED);
1998     FLUSH_MEM_WRITE ();
1999     ch->txd_usr_add = md;
2000
2001     len = OS_mem_token_tlen (mem_token);
2002     atomic_add (len, &ch->tx_pending);
2003     atomic_add (len, &ci->tx_pending);
2004     ch->s.tx_packets++;
2005     ch->s.tx_bytes += len;
2006 #if 0
2007     spin_unlock_irqrestore (&ch->ch_txlock, flags);   /* allow pending
2008                                                        * interrupt to sneak
2009                                                        * thru */
2010 #endif
2011
2012     /*
2013      * If an ONR was seen, then channel requires poking to restart
2014      * transmission.
2015      */
2016     if (ch->ch_start_tx)
2017     {
2018 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,41)
2019         SD_SEM_TAKE (&ci->sem_wdbusy, "_wd_");  /* only 1 thru here, per
2020                                                  * board */
2021         if ((ch->ch_start_tx == CH_START_TX_ONR) && (ch->p.chan_mode == CFG_CH_PROTO_TRANS))
2022         {
2023             /* ONR restart transmission from background loop */
2024             ci->wd_notify = WD_NOTIFY_ONR;      /* enabled global watchdog
2025                                                  * scan-thru  */
2026         } else
2027         {
2028             /* start first transmission from background loop */
2029             ci->wd_notify = WD_NOTIFY_1TX;      /* enabled global watchdog
2030                                                  * scan-thru  */
2031         }
2032         musycc_chan_restart (ch);
2033         SD_SEM_GIVE (&ci->sem_wdbusy);
2034 #else
2035         musycc_chan_restart (ch);
2036 #endif
2037     }
2038 #ifdef SBE_WAN256T3_ENABLE
2039     wan256t3_led (ci, LED_TX, LEDV_G);
2040 #endif
2041     return 0;
2042 }
2043
2044
2045 #if 0
2046 int
2047 musycc_set_chan (ci_t * ci, int channum, struct sbecom_chan_param * p)
2048 {
2049     mch_t      *ch;
2050     int         rok = 0;
2051     int         n = 0;
2052
2053     if (channum < 0 || channum >= (MUSYCC_NPORTS * MUSYCC_NCHANS))      /* sanity chk param */
2054         return ECHRNG;
2055     if (!(ch = sd_find_chan (ci, channum)))
2056         return ENOENT;
2057     if (ch->channum != p->channum)
2058         return EINVAL;
2059     if (sd_line_is_ok (ch->user))
2060     {
2061         rok = 1;
2062         sd_line_is_down (ch->user);
2063     }
2064     if (ch->state == UP &&          /* bring down in current configuration */
2065         (ch->p.status != p->status ||
2066          ch->p.chan_mode != p->chan_mode ||
2067          ch->p.intr_mask != p->intr_mask ||
2068          ch->txd_free < ch->txd_num))
2069     {
2070         if ((n = musycc_chan_down (ci, channum)))
2071             return n;
2072         if (ch->p.mode_56k != p->mode_56k)
2073         {
2074             ch->p = *p;             /* copy in new parameters */
2075             musycc_update_timeslots (&ci->port[ch->channum / MUSYCC_NCHANS]);
2076         } else
2077             ch->p = *p;             /* copy in new parameters */
2078         if ((n = musycc_chan_up (ci, channum)))
2079             return n;
2080         sd_enable_xmit (ch->user);  /* re-enable to catch flow controlled
2081                                      * channel */
2082     } else
2083     {
2084         if (ch->p.mode_56k != p->mode_56k)
2085         {
2086             ch->p = *p;             /* copy in new parameters */
2087             musycc_update_timeslots (&ci->port[ch->channum / MUSYCC_NCHANS]);
2088         } else
2089             ch->p = *p;             /* copy in new parameters */
2090     }
2091
2092     if (rok)
2093         sd_line_is_up (ch->user);
2094     return 0;
2095 }
2096 #endif
2097
2098
2099 int
2100 musycc_get_chan (ci_t * ci, int channum, struct sbecom_chan_param * p)
2101 {
2102     mch_t      *ch;
2103
2104 #if 0
2105     if (channum < 0 || channum >= (MUSYCC_NPORTS * MUSYCC_NCHANS))      /* sanity chk param */
2106         return ECHRNG;
2107 #endif
2108     if (!(ch = sd_find_chan (ci, channum)))
2109         return ENOENT;
2110     *p = ch->p;
2111     return 0;
2112 }
2113
2114
2115 int
2116 musycc_get_chan_stats (ci_t * ci, int channum, struct sbecom_chan_stats * p)
2117 {
2118     mch_t      *ch;
2119
2120     if (channum < 0 || channum >= (MUSYCC_NPORTS * MUSYCC_NCHANS))      /* sanity chk param */
2121         return ECHRNG;
2122     if (!(ch = sd_find_chan (ci, channum)))
2123         return ENOENT;
2124     *p = ch->s;
2125     p->tx_pending = atomic_read (&ch->tx_pending);
2126     return 0;
2127 }
2128
2129
2130
2131 #ifdef SBE_WAN256T3_ENABLE
2132 int
2133 musycc_chan_down (ci_t * ci, int channum)
2134 {
2135     mch_t      *ch;
2136     mpi_t      *pi;
2137     int         i, gchan;
2138
2139     if (!(ch = sd_find_chan (ci, channum)))
2140         return EINVAL;
2141     pi = ch->up;
2142     gchan = ch->gchan;
2143
2144     /* Deactivate the channel */
2145     musycc_serv_req (pi, SR_CHANNEL_DEACTIVATE | SR_RX_DIRECTION | gchan);
2146     ch->ch_start_rx = 0;
2147     musycc_serv_req (pi, SR_CHANNEL_DEACTIVATE | SR_TX_DIRECTION | gchan);
2148     ch->ch_start_tx = 0;
2149
2150     if (ch->state == DOWN)
2151         return 0;
2152     ch->state = DOWN;
2153
2154     pi->regram->thp[gchan] = 0;
2155     pi->regram->tmp[gchan] = 0;
2156     pi->regram->rhp[gchan] = 0;
2157     pi->regram->rmp[gchan] = 0;
2158     FLUSH_MEM_WRITE ();
2159     for (i = 0; i < ch->txd_num; i++)
2160     {
2161         if (ch->mdt[i].mem_token != 0)
2162             OS_mem_token_free (ch->mdt[i].mem_token);
2163     }
2164
2165     for (i = 0; i < ch->rxd_num; i++)
2166     {
2167         if (ch->mdr[i].mem_token != 0)
2168             OS_mem_token_free (ch->mdr[i].mem_token);
2169     }
2170
2171     OS_kfree (ch->mdt);
2172     ch->mdt = 0;
2173     OS_kfree (ch->mdr);
2174     ch->mdr = 0;
2175
2176     return 0;
2177 }
2178 #endif
2179
2180 /*** End-of-File ***/