2 * Middle-level code for Cronyx Tau32-PCI adapters.
4 * Copyright (C) 2004 Cronyx Engineering
5 * Copyright (C) 2004 Roman Kurakin <rik@FreeBSD.org>
7 * This software is distributed with NO WARRANTIES, not even the implied
8 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 * Authors grant any other persons or organisations a permission to use,
11 * modify and redistribute this software in source and binary forms,
12 * as long as this message is kept with the software, all derivative
13 * works or modified versions.
15 * $Cronyx: ceddk.c,v 1.2.6.2 2005/11/17 16:04:13 rik Exp $
18 #include <sys/cdefs.h>
19 __FBSDID("$FreeBSD$");
21 #include <dev/cx/machdep.h>
22 #include <dev/ce/ceddk.h>
24 #undef CE_DDK_DEBUG_ENABLED
25 #ifdef CE_DDK_DEBUG_ENABLED
27 # define CE_DDK_DEBUG(b,c,s) \
30 printf("ce%d-%d: ",(b)->num,(c)->num); \
32 printf("ce%d-*: ",(b)->num); \
37 # define CE_DDK_DEBUG(b,c,s) do {} while (0)
40 # define CE_DDK_DEBUG(b,c,s) do {} while (0)
48 printf ("%s: >> enter (%16llx) %d\n", __FUNCTION__, rdtsc (), enter); \
51 #define EXIT(val...) \
54 printf ("%s: << exit (%16llx) %d line %d\n", __FUNCTION__, rdtsc (), enter, __LINE__); \
61 #define EXIT(val...) \
62 do {return val;} while (0)
65 #define CE_ENQUEUE(list,item) \
67 TAU32_UserRequest **last; \
70 last = &(*last)->next; \
73 (item)->next = NULL; \
76 #define CE_ENQUEUE_HEAD(list,item) \
78 (item)->next = list; \
82 #define CE_DEQUEUE(list,item) \
86 list = (item)->next; \
90 #define CE_PREREQUEST(b,c,list,item) \
94 CE_DDK_DEBUG (b, c, ("Fatal error, no free structs " \
95 "for UserRequest (%s:%d)\n", \
96 __FUNCTION__, __LINE__)); \
100 #define CE_DUMP_QUEUE(list) \
102 TAU32_UserRequest *item; \
106 printf ("item%d: %p\n", i, item); \
112 #define CE_FIND_ITEM(list,item,flag) \
114 TAU32_UserRequest *citem; \
116 for (citem = list; citem; citem = citem->next) { \
117 if (citem == item) { \
124 #define CE_LAST_ITEM(list,item) \
126 TAU32_UserRequest **last; \
128 while ((*last) && (*last)->next) { \
129 last = &(*last)->next; \
134 #define CE_ASSERT(a) \
137 printf ("ASSERT: %d %s\n", __LINE__, #a); \
138 __asm __volatile ("int $3"); \
142 static void _ce_set_ts (ce_chan_t *c, unsigned long ts);
143 static void _ce_submit_configure_e1 (ce_chan_t *c, char *rname);
145 #ifdef CE_DDK_DEBUG_ENABLED
146 static char *ce_err2str (unsigned long err)
149 case TAU32_SUCCESSFUL:
151 case TAU32_ERROR_ALLOCATION:
152 return "Allocation error, not enough tx/rx descriptors";
153 case TAU32_ERROR_BUS:
154 return "PEB could not access to host memory by PCI bus for load/store information";
155 case TAU32_ERROR_FAIL:
156 return "PEB action request failed";
157 case TAU32_ERROR_TIMEOUT:
158 return "PEB action request timeout";
159 case TAU32_ERROR_CANCELLED:
160 return "request has been canceled";
161 case TAU32_ERROR_TX_UNDERFLOW:
162 return "transmission underflow";
163 case TAU32_ERROR_TX_PROTOCOL:
164 return "TX_PROTOCOL";
165 case TAU32_ERROR_RX_OVERFLOW:
166 return "RX_OVERFLOW";
167 case TAU32_ERROR_RX_ABORT:
169 case TAU32_ERROR_RX_CRC:
171 case TAU32_ERROR_RX_SHORT:
173 case TAU32_ERROR_RX_SYNC:
175 case TAU32_ERROR_RX_FRAME:
177 case TAU32_ERROR_RX_LONG:
179 case TAU32_ERROR_RX_SPLIT:
180 return "frame has splitted between two requests due rx-gap allocation";
181 case TAU32_ERROR_RX_UNFIT:
182 return "frame can't be fit into request buffer";
183 case TAU32_ERROR_TSP:
185 case TAU32_ERROR_RSP:
187 case TAU32_ERROR_INT_OVER_TX:
188 return "ERROR INT OVER TX";
189 case TAU32_ERROR_INT_OVER_RX:
190 return "ERROR INT OVER RX";
191 case TAU32_ERROR_INT_STORM:
193 case TAU32_ERROR_INT_E1LOST:
194 return "ERROR_E1LOST";
196 return ("Unknown error");
201 void ce_set_dtr (ce_chan_t *c, int on)
206 void ce_set_rts (ce_chan_t *c, int on)
211 static void TAU32_CALLBACK_TYPE ce_on_receive
212 (TAU32_UserContext *pContext, TAU32_UserRequest *req)
214 ce_buf_item_t *item = (ce_buf_item_t *)req;
221 if (!req || !req->sys) {
225 c = (ce_chan_t *)req->sys;
228 len = req->Io.Rx.Received;
229 error = req->ErrorCode;
232 if (error == TAU32_SUCCESSFUL) {
233 if (req->Io.Rx.FrameEnd) {
236 CE_DDK_DEBUG (b, c, ("No FrameEnd\n"));
237 /* probably do something in some cases*/
241 c->receive (c, item->buf, len);
242 } else if (error & TAU32_ERROR_BUS) {
245 c->error (c, CE_OVERRUN);
247 CE_DDK_DEBUG (b, c, ("Another receive error: %x\n", error));
248 /* Do some procesing */
251 CE_ASSERT (!req->pInternal);
252 CE_ENQUEUE (c->rx_queue, req);
253 while (c->rx_queue) {
254 CE_DEQUEUE (c->rx_queue, req);
256 item = (ce_buf_item_t *)req;
257 req->Command = TAU32_Rx_Data;
258 req->Io.Rx.Channel = c->num;
259 req->pCallback = ce_on_receive;
260 req->Io.Rx.BufferLength = BUFSZ+4;
261 req->Io.Rx.PhysicalDataAddress = item->phys;
262 if (!TAU32_SubmitRequest (b->ddk.pControllerObject, req)) {
263 CE_DDK_DEBUG (b, c, ("RX submition failure\n"));
265 CE_ENQUEUE_HEAD (c->rx_queue, req);
272 static void TAU32_CALLBACK_TYPE ce_on_transmit
273 (TAU32_UserContext *pContext, TAU32_UserRequest *req)
280 if (!req || !req->sys) {
284 c = (ce_chan_t *)req->sys;
286 len = req->Io.Tx.Transmitted;
287 error = req->ErrorCode;
290 if (error == TAU32_SUCCESSFUL) {
293 } else if (error & TAU32_ERROR_BUS) {
296 c->error (c, CE_UNDERRUN);
298 CE_DDK_DEBUG (c->board, c, ("Another transmit error: %x\n",
300 /* Do some procesing */
303 CE_ENQUEUE (c->tx_queue, req);
307 c->transmit (c, 0, len);
311 int ce_transmit_space (ce_chan_t *c)
313 return c->tx_pending < (TAU32_IO_QUEUE);
316 int ce_send_packet (ce_chan_t *c, unsigned char *buf, int len, void *tag)
318 TAU32_UserRequest *req;
323 if (!ce_transmit_space (c)) {
327 if (len <= 0 || len > BUFSZ) {
331 CE_DEQUEUE (c->tx_queue, req);
333 item = (ce_buf_item_t *)req;
335 if (buf != item->buf)
336 memcpy (item->buf, buf, len);
338 CE_ASSERT (!req->pInternal);
340 req->Command = TAU32_Tx_Data | TAU32_Tx_FrameEnd;
341 req->Io.Tx.Channel = c->num;
342 req->pCallback = ce_on_transmit;
343 req->Io.Tx.DataLength = len;
344 req->Io.Tx.PhysicalDataAddress = item->phys;
346 if (!TAU32_SubmitRequest (c->board->ddk.pControllerObject, req)) {
347 CE_DDK_DEBUG (c->board, c, ("Can't submit packet for "
349 CE_ENQUEUE_HEAD (c->tx_queue, req);
356 static void TAU32_CALLBACK_TYPE ce_on_config
357 (TAU32_UserContext *pContext, TAU32_UserRequest *req)
359 ce_board_t *b = (ce_board_t *) pContext;
363 CE_DDK_DEBUG (b, (ce_chan_t*)0, ("Config request failure: %lx\n",
368 static void TAU32_CALLBACK_TYPE ce_on_config_stop
369 (TAU32_UserContext *pContext, TAU32_UserRequest *req)
372 TAU32_UserRequest *rreq;
373 ce_board_t *b = (ce_board_t *) pContext;
374 ce_chan_t *c = b->chan + req->Io.ChannelNumber;
377 /* Stop all requests */
378 CE_ASSERT (0);/* Buggy */
379 CE_LAST_ITEM (c->rx_queue, rreq);
380 /* A little hacky, try to guess which is a first */
381 first = rreq ? (c->rx_item - (ce_buf_item_t *)rreq) + 1 : 0;
382 for (i = 0; i < TAU32_IO_QUEUE; i++) {
384 rreq = &c->rx_item[(i + first) % TAU32_IO_QUEUE].req;
385 CE_FIND_ITEM (c->rx_queue, rreq, is_pending);
388 TAU32_CancelRequest (b->ddk.pControllerObject, rreq, 1);
389 rreq->Command = TAU32_Rx_Data;
390 rreq->Io.Rx.Channel = c->num;
391 rreq->Io.Rx.BufferLength = BUFSZ+4;
392 rreq->Io.Rx.PhysicalDataAddress = ((ce_buf_item_t *)rreq)->phys;
394 if (!TAU32_SubmitRequest (b->ddk.pControllerObject, rreq)) {
395 CE_ASSERT (0);/* Buggy */
402 /* c->rx_pending = 0;*/
406 static int ce_cfg_submit (ce_board_t *b)
408 TAU32_UserRequest *req;
411 CE_DEQUEUE (b->cr.queue, req);
413 CE_ASSERT (!req->pInternal);
415 req->pCallback = ce_on_config;
418 CE_DDK_DEBUG (b, (ce_chan_t *)0, ("config request pending: %d\n",
421 if (!TAU32_SubmitRequest (b->ddk.pControllerObject, req)) {
422 CE_ENQUEUE_HEAD (b->cr.queue, req);
423 CE_DDK_DEBUG (b, (ce_chan_t *)0, ("Fail to submit config request\n"));
431 void ce_init_board (ce_board_t *b)
437 for (i = 0; i < CONFREQSZ; i++) {
438 CE_ENQUEUE (b->cr.queue, b->cr.req + i);
441 b->chan[0].config = TAU32_ais_on_loss;
443 /* lloop = off, rloop = off */
444 b->chan[0].config |= TAU32_LineNormal;
445 b->chan[0].lloop = 0;
446 b->chan[0].rloop = 0;
448 /* unfram=off, scrambler=off, use16=off, crc4=off,
449 higain=off, monitor=off*/
450 b->chan[0].config |= (b->ddk.Interfaces == 2 ? TAU32_framed_cas_cross :
451 TAU32_framed_cas_set);
452 b->chan[0].unfram = 0;
453 b->chan[0].scrambler = 0;
454 b->chan[0].use16 = 0;
456 b->chan[0].higain = 0;
457 b->chan[0].monitor = 0;
459 if (b->ddk.Interfaces == 2) {
460 b->chan[1].config = TAU32_ais_on_loss;
461 /* lloop = off, rloop = off */
462 b->chan[1].config |= TAU32_LineNormal;
463 /* unfram=off, scrambler=off, use16=off, crc4=off,
464 higain=off, monitor=off*/
465 b->chan[1].config |= TAU32_framed_cas_cross;
466 b->chan[1].unfram = 0;
467 b->chan[1].scrambler = 0;
468 b->chan[1].use16 = 0;
470 b->chan[1].higain = 0;
471 b->chan[1].monitor = 0;
474 for (i = 0; i < NCHAN; i++) {
475 /* Chan0 ts=1-15,17-31, Chan1 ts=1-2 */
476 b->chan[i].type = i < b->ddk.Interfaces ? T_E1 : T_DATA;
477 b->chan[i].ts = (i == 0 ? 0xfffefffe :
479 (b->ddk.Interfaces == 2 ? 0x6: 0)));
480 b->chan[i].dir = (b->ddk.Interfaces == 2) ? (i%2) : 0;
481 b->chan[i].mtu = 1504;
486 /* We must have some here */
488 req->Command = TAU32_Configure_E1;
489 req->Io.InterfaceConfig.Interface = TAU32_E1_A;
490 req->Io.InterfaceConfig.Config = b->chan[0].config;
491 req->Io.InterfaceConfig.UnframedTsMask = 0;
492 if (!ce_cfg_submit (b)) {
493 CE_DDK_DEBUG (b, b->chan + 0,
494 ("Submit request failure, line %d\n",
498 if (b->ddk.Interfaces == 2) {
500 /* We must have some here */
502 req->Command = TAU32_Configure_E1;
503 req->Io.InterfaceConfig.Interface = TAU32_E1_B;
504 req->Io.InterfaceConfig.Config = b->chan[1].config;
505 req->Io.InterfaceConfig.UnframedTsMask = 0;
506 if (!ce_cfg_submit (b)) {
507 CE_DDK_DEBUG (b, b->chan + 1,
508 ("Submit request failure, line %d\n",
513 /* Set default cross matrix */
514 for (i = 0; i < 32; i++) {
516 b->dxc[i] = TAU32_CROSS_OFF;
518 b->dxc[i + 32] = i + 64;
520 b->dxc[i + 64] = i + 32;
523 /* We have only mux mode for now. Later we will also have cross mode */
527 void ce_start_chan (ce_chan_t *c, int tx, int rx, ce_buf_t *cb,
531 ce_board_t *b = c->board;
533 /* c->config = TAU32_ais_on_loss | TAU32_framed_cas_cross;*/
536 CE_DDK_DEBUG (b, c, ("ce_buf_t virt:%p phys:%p\n", cb,
538 c->tx_item = cb->tx_item;
539 c->rx_item = cb->rx_item;
542 for (i = 0; i < TAU32_IO_QUEUE; i++) {
543 c->tx_item[i].phys = phys +
544 ((char *)(c->tx_item[i].buf)-(char *)cb);
545 c->rx_item[i].phys = phys +
546 ((char *)(c->rx_item[i].buf)-(char *)cb);
547 cb->tx_item[i].req.sys = c;
548 cb->rx_item[i].req.sys = c;
549 CE_DDK_DEBUG (b, c, ("tx_item[%d].buf virt:%p phys:%p\n",
550 i, c->tx_item[i].buf,
551 (void *)c->tx_item[i].phys));
552 CE_DDK_DEBUG (b, c, ("rx_item[%d].buf virt:%p phys:%p\n",
553 i, c->rx_item[i].buf,
554 (void *)c->rx_item[i].phys));
555 CE_ENQUEUE (c->rx_queue, &c->rx_item[i].req);
556 CE_ENQUEUE (c->tx_queue, &c->tx_item[i].req);
565 TAU32_UserRequest *req;
567 CE_DEQUEUE (c->rx_queue, req);
570 item = (ce_buf_item_t *) req;
571 CE_ASSERT (c->rx_pending < TAU32_IO_QUEUE);
572 req->Command = TAU32_Rx_Data;
573 req->Io.Rx.Channel = c->num;
574 req->pCallback = ce_on_receive;
575 req->Io.Rx.BufferLength = c->mtu + (c->phony ? 0 : 4);
576 req->Io.Rx.PhysicalDataAddress = item->phys;
578 if (!TAU32_SubmitRequest (b->ddk.pControllerObject, req)) {
579 CE_DDK_DEBUG (b, c, ("Faild to submit rx request\n"));
580 /*XXXRIK: shouldn't happen, but ... */
588 TAU32_UserRequest *req;
589 CE_PREREQUEST (b, c, b->cr.queue, req);
592 req->Command = TAU32_Configure_Commit |
593 (tx ? TAU32_Tx_Start : 0) |
594 (rx ? TAU32_Rx_Start : 0);
595 req->Io.ChannelNumber = c->num;
596 if (!ce_cfg_submit (b)) {
597 CE_DDK_DEBUG (b, c, ("Can't start chan\n"));
598 /* Do some error processing */
603 /* If we run just after ce_board_init we have prope values.
604 * Else I hope you didn't set ts to incorrect value.
606 _ce_set_ts (c, c->ts);
607 if (c->num < b->ddk.Interfaces) {
608 /* The same for other modes. We don't check them.
609 * We hope that config is correctly set. Just as we have
610 * after ce_board_init. If channel was stoped we hope that
611 * it's config was not broken just after it and we didn't
612 * brake it before start.
614 _ce_submit_configure_e1 (c, "start_init");
618 void ce_stop_chan (ce_chan_t *c)
620 ce_board_t *b = c->board;
621 TAU32_UserRequest *req;
622 CE_DEQUEUE (b->cr.queue, req);
624 /* XXXRIK: This function should be for completeness, but for now I
625 * don't use it. I just started to write and haven't finished it yet.
626 * It is VERY BUGGY!!! Do not use it. If you really need
627 * it ask me to fix it or rewrite it by yourself.
628 * Note: most buggy part of it in ce_on_config_stop!
631 CE_DDK_DEBUG (b, c, ("Fatal error, no free structs for "
632 "UserRequest (%s:%d)\n", __FUNCTION__, __LINE__));
635 // req->Command = TAU32_Configure_Commit |
636 // TAU32_Tx_Stop | TAU32_Rx_Stop;
638 req->Io.ChannelNumber = c->num;
639 req->pCallback = ce_on_config_stop;
642 if (!TAU32_SubmitRequest (b->ddk.pControllerObject, req)) {
643 CE_ENQUEUE_HEAD (b->cr.queue, req);
644 CE_DDK_DEBUG (b, c, ("Can't stop chan\n"));
650 void ce_register_transmit (ce_chan_t *c,
651 void (*func) (ce_chan_t*, void*, int))
656 void ce_register_receive (ce_chan_t *c,
657 void (*func) (ce_chan_t*, unsigned char*, int))
662 void ce_register_error (ce_chan_t *c,
663 void (*func) (ce_chan_t*, int))
668 void TAU32_CALLBACK_TYPE ce_error_callback (TAU32_UserContext *pContext,
669 int Item, unsigned NotifyBits)
671 ce_board_t *b = (ce_board_t *) pContext;
673 if (NotifyBits & (TAU32_ERROR_FAIL | TAU32_ERROR_TIMEOUT
674 | TAU32_ERROR_INT_OVER_TX | TAU32_ERROR_INT_OVER_RX
675 | TAU32_ERROR_INT_STORM)) {
676 /* Fatal: adapter failure, need reset & restart */
677 /* RIKXXX: probably I should add CE_FAILURE for ce_error */
678 CE_DDK_DEBUG (b, (ce_chan_t *)0, ("Err, disable interrupts: %s\n",
679 ce_err2str (NotifyBits)));
680 /* TAU32_DisableInterrupts (b->ddk.pControllerObject);*/
685 ce_chan_t *c = b->chan + Item;
686 if (NotifyBits & TAU32_ERROR_TX_UNDERFLOW) {
689 c->error (c, CE_UNDERRUN);
691 if (NotifyBits & TAU32_ERROR_RX_OVERFLOW) {
694 c->error (c, CE_OVERFLOW);
696 if (NotifyBits & (TAU32_ERROR_RX_FRAME | TAU32_ERROR_RX_ABORT |
697 TAU32_ERROR_RX_SHORT | TAU32_ERROR_RX_LONG |
698 TAU32_ERROR_RX_SYNC | TAU32_ERROR_RX_SPLIT |
699 TAU32_ERROR_RX_UNFIT)) {
701 CE_DDK_DEBUG (b, c, ("Frame error: %x\n", NotifyBits));
703 c->error (c, CE_FRAME);
705 if(NotifyBits & TAU32_ERROR_RX_CRC) {
708 c->error (c, CE_CRC);
711 CE_DDK_DEBUG (b, (ce_chan_t *)0, ("Another error: %x\n",
713 /* Adapter error, do smth */
718 void TAU32_CALLBACK_TYPE ce_status_callback(TAU32_UserContext *pContext,
719 int Item, unsigned NotifyBits)
721 ce_board_t *b = (ce_board_t *) pContext;
725 ce_chan_t *c = b->chan + Item;
726 c->acc_status |= b->ddk.InterfacesInfo[Item].Status;
727 /* CE_DDK_DEBUG (b, c, ("Current status: %x\n", c->acc_status));*/
729 CE_DDK_DEBUG (b, (ce_chan_t *)0, ("Another status: %x\n", NotifyBits));
730 /* Adapter status, do smth. */
735 int ce_get_cd (ce_chan_t *c)
737 unsigned int e1status = c->board->ddk.InterfacesInfo[c->dir].Status;
739 return (c->ts && !(e1status & (TAU32_RCL | TAU32_E1OFF)));
742 int ce_get_cts (ce_chan_t *c)
747 int ce_get_dsr (ce_chan_t *c)
752 void ce_e1_timer (ce_chan_t *c)
754 unsigned bpv, fas, crc4, ebit, pcv, oof, css;
755 unsigned int acc_status;
756 ce_board_t *b = c->board;
757 TAU32_E1_State *state;
759 if (c->num >= b->ddk.Interfaces)
762 state = &b->ddk.InterfacesInfo[c->num];
763 acc_status = c->acc_status;
765 /* Clear acc_status */
766 c->acc_status = b->ddk.InterfacesInfo[c->num].Status;
769 * During the first second after the channel startup
770 * the status registers are not stable yet,
771 * we will so skip the first second. */
773 if (! c->totsec && c->cursec <= 1)
778 /* Compute the SNMP-compatible channel status. */
781 if (acc_status & TAU32_RCL)
782 c->status |= ESTS_LOS; /* loss of signal */
783 if (acc_status & TAU32_RUA1)
784 c->status |= ESTS_AIS; /* receiving all ones */
786 /* Get error counters. */
787 bpv = state->RxViolations;
794 if (! c->use16 && (acc_status & TAU32_RSA1))
795 c->status |= ESTS_AIS16; /* signaling all ones */
796 if (! c->use16 && (acc_status & TAU32_RDMA))
797 c->status |= ESTS_FARLOMF; /* alarm in timeslot 16 */
798 if (acc_status & TAU32_RRA)
799 c->status |= ESTS_FARLOF; /* far loss of framing */
801 if (acc_status & TAU32_RFAS) {
802 c->status |= ESTS_LOF; /* loss of framing */
803 ++oof; /* out of framing */
805 if ((! c->use16 && (acc_status & TAU32_RCAS)) ||
806 (c->crc4 && (acc_status & TAU32_RCRC4))) {
807 c->status |= ESTS_LOMF; /* loss of multiframing */
808 ++oof; /* out of framing */
810 fas = state->FasErrors;
811 crc4 = state->Crc4Errors;
812 ebit = state->FarEndBlockErrors;
814 /* Controlled slip second -- any slip event. */
815 css = state->TransmitSlips + state->ReceiveSlips;
819 state->RxViolations = 0;
820 state->FasErrors = 0;
821 state->Crc4Errors = 0;
822 state->FarEndBlockErrors = 0;
823 state->TransmitSlips = 0;
824 state->ReceiveSlips = 0;
826 if (c->status & ESTS_LOS)
827 c->status = ESTS_LOS;
828 else if (c->status & ESTS_AIS)
829 c->status = ESTS_AIS;
830 else if (c->status & ESTS_LOF)
831 c->status = ESTS_LOF;
832 else if (c->status & ESTS_LOMF)
833 c->status &= ~(ESTS_FARLOMF | ESTS_AIS16);
836 c->status = ESTS_NOALARM;
838 c->currnt.bpv += bpv;
839 c->currnt.fse += fas;
841 c->currnt.crce += crc4;
842 c->currnt.rcrce += ebit;
845 /* Path code violation is frame sync error if CRC4 disabled,
846 * or CRC error if CRC4 enabled. */
851 /* Unavaiable second -- receiving all ones, or
852 * loss of carrier, or loss of signal. */
853 if (acc_status & (TAU32_RUA1 | TAU32_RCL))
854 /* Unavailable second -- no other counters. */
857 /* Line errored second -- any BPV. */
861 /* Errored second -- any PCV, or out of frame sync,
862 * or any slip events. */
863 if (pcv || oof || css)
866 /* Severely errored framing second -- out of frame sync. */
870 /* Severely errored seconds --
871 * 832 or more PCVs, or 2048 or more BPVs. */
872 if (bpv >= 2048 || pcv >= 832)
875 /* Bursty errored seconds --
876 * no SES and more than 1 PCV. */
880 /* Collect data for computing
881 * degraded minutes. */
883 c->degerr += bpv + pcv;
887 /* Degraded minutes -- having error rate more than 10e-6,
888 * not counting unavailable and severely errored seconds. */
889 if (c->cursec % 60 == 0) {
890 if (c->degerr > c->degsec * 2048 / 1000)
896 /* Rotate statistics every 15 minutes. */
897 if (c->cursec > 15*60) {
901 c->interval[i] = c->interval[i-1];
902 c->interval[0] = c->currnt;
904 /* Accumulate total statistics. */
905 c->total.bpv += c->currnt.bpv;
906 c->total.fse += c->currnt.fse;
907 c->total.crce += c->currnt.crce;
908 c->total.rcrce += c->currnt.rcrce;
909 c->total.uas += c->currnt.uas;
910 c->total.les += c->currnt.les;
911 c->total.es += c->currnt.es;
912 c->total.bes += c->currnt.bes;
913 c->total.ses += c->currnt.ses;
914 c->total.oofs += c->currnt.oofs;
915 c->total.css += c->currnt.css;
916 c->total.dm += c->currnt.dm;
930 c->totsec += c->cursec;
935 void ce_set_baud (ce_chan_t *c, unsigned long baud)
937 TAU32_UserRequest *req;
938 ce_board_t *b = c->board;
939 unsigned long cfg = c->config & ~TAU32_framing_mode_mask;
941 unsigned long kbps = (baud + 32000) / 64000 * 64;
943 if (!c->unfram || c->num != 0 ||
944 baud == c->baud || b->cr.pending >= CONFREQSZ)
947 if (!kbps || kbps > 1024) {
949 cfg |= TAU32_unframed_2048;
950 } else if (kbps > 512) {
952 cfg |= TAU32_unframed_1024;
953 } else if (kbps > 256) {
955 cfg |= TAU32_unframed_512;
956 } else if (kbps > 128) {
958 cfg |= TAU32_unframed_256;
959 } else if (kbps > 64) {
961 cfg |= TAU32_unframed_128;
964 cfg |= TAU32_unframed_64;
967 /* _ce_set_ts () will set proper baud */
969 CE_PREREQUEST (b, c, b->cr.queue, req);
972 req->Command = TAU32_Configure_E1;
973 req->Io.InterfaceConfig.Interface = TAU32_E1_A;
974 req->Io.InterfaceConfig.Config = cfg;
975 req->Io.InterfaceConfig.UnframedTsMask = ts;
976 if (ce_cfg_submit (b)) {
983 void ce_set_lloop (ce_chan_t *c, unsigned char on)
985 TAU32_UserRequest *req;
986 unsigned long cfg = c->config & ~(TAU32_line_mode_mask | TAU32_ais_on_loss);
987 ce_board_t *b = c->board;
989 if (c->num >= b->ddk.Interfaces || b->cr.pending >= CONFREQSZ)
995 cfg |= on ? TAU32_LineLoopInt : (TAU32_LineNormal | TAU32_ais_on_loss);
996 CE_PREREQUEST (b, c, b->cr.queue, req);
999 req->Command = TAU32_Configure_E1;
1000 req->Io.InterfaceConfig.Interface = c->num ? TAU32_E1_B : TAU32_E1_A;
1001 req->Io.InterfaceConfig.Config = cfg;
1002 req->Io.InterfaceConfig.UnframedTsMask = c->ts;
1003 CE_DDK_DEBUG (b, c, ("Submit lloop\n"));
1004 if (ce_cfg_submit (b)) {
1005 c->lloop = on ? 1 : 0;
1010 void ce_set_rloop (ce_chan_t *c, unsigned char on)
1012 TAU32_UserRequest *req;
1013 unsigned long cfg = c->config & ~TAU32_line_mode_mask;
1014 ce_board_t *b = c->board;
1016 if (c->num >= b->ddk.Interfaces || b->cr.pending >= CONFREQSZ)
1022 cfg |= on ? TAU32_LineLoopExt : TAU32_LineNormal;
1023 CE_PREREQUEST (b, c, b->cr.queue, req);
1026 req->Command = TAU32_Configure_E1;
1027 req->Io.InterfaceConfig.Interface = c->num ? TAU32_E1_B : TAU32_E1_A;
1028 req->Io.InterfaceConfig.Config = cfg;
1029 req->Io.InterfaceConfig.UnframedTsMask = c->ts;
1030 CE_DDK_DEBUG (b, c, ("Submit rloop\n"));
1031 if (ce_cfg_submit (b)) {
1032 c->rloop = on ? 1 : 0;
1037 void ce_set_higain (ce_chan_t *c, unsigned char on)
1039 TAU32_UserRequest *req;
1040 unsigned long cfg = c->config & ~TAU32_higain;
1041 ce_board_t *b = c->board;
1043 if (c->num >= b->ddk.Interfaces || b->cr.pending >= CONFREQSZ)
1046 if (on == c->higain)
1049 cfg |= on ? TAU32_higain : 0;
1050 CE_PREREQUEST (b, c, b->cr.queue, req);
1053 req->Command = TAU32_Configure_E1;
1054 req->Io.InterfaceConfig.Interface = c->num ? TAU32_E1_B : TAU32_E1_A;
1055 req->Io.InterfaceConfig.Config = cfg;
1056 req->Io.InterfaceConfig.UnframedTsMask = c->ts;
1057 CE_DDK_DEBUG (b, c, ("Submit higain\n"));
1058 if (ce_cfg_submit (b)) {
1059 c->higain = on ? 1 : 0;
1064 static void _ce_set_ts (ce_chan_t *c, unsigned long ts)
1066 TAU32_UserRequest *req;
1067 ce_board_t *b = c->board;
1068 unsigned long mask = 0, omask = 0;
1069 int nts = 0, ots = 0, pts = 0;
1072 if (b->cr.pending >= CONFREQSZ)
1076 * pts - number of busy "peb" ts
1077 * ots - current (old) busy ts
1080 for (i = 0; i < 32; i++) {
1081 if (c->ts & (1ul << i))
1083 if (ts & (1ul << i))
1085 if (b->dxc[i] != TAU32_CROSS_OFF)
1089 CE_DDK_DEBUG (b, c, ("pts: %d ots: %d nts: %d ts: %lx\n", pts, ots, nts,
1091 /* 32 - all busy + my old busy == free */
1092 if (32 - pts + ots - nts < 0)
1095 /* Ok. We have enougth "peb" ts. Clean old. */
1096 /* We start from zero, cause this is peb cells */
1097 for (i = 0; i < 32; i++) {
1098 int tin = b->dxc[i];
1100 if (tin < (c->dir?64:32) || tin > (c->dir?95:63))
1102 if (c->ts & (1ul << t)) {
1103 b->dxc[tin] = TAU32_CROSS_OFF;
1104 b->dxc[i] = TAU32_CROSS_OFF;
1105 if (b->dxc[t + 32] == TAU32_CROSS_OFF &&
1106 b->dxc[t + 64] == TAU32_CROSS_OFF) {
1107 b->dxc[t + 32] = t + 64;
1108 b->dxc[t + 64] = t + 32;
1110 omask |= (1ul << t);
1116 for (i = 0; i < 32; i++) {
1117 if ((ts & (1ul << i)) == 0)
1119 while (b->dxc[k] != TAU32_CROSS_OFF) {
1123 CE_DDK_DEBUG (b, c, ("TS count overflow\n"));
1127 b->dxc[k] = (c->dir?64:32) + i;
1128 b->dxc[(c->dir?64:32) + i] = k;
1129 if (b->dxc[(c->dir?32:64) + i] == (c->dir?64:32) + i)
1130 b->dxc[(c->dir?32:64) + i] = TAU32_CROSS_OFF;
1135 c->baud = nts*64000;
1137 CE_PREREQUEST (b, c, b->cr.queue, req);
1141 req->Command = TAU32_Timeslots_Channel | TAU32_Configure_Commit;
1142 req->Io.ChannelNumber = c->num;
1143 req->Io.ChannelConfig.AssignedTsMask = mask;
1150 CE_DDK_DEBUG (b, c, ("ts=%lx mask=%lx omask=%lx pmask=%lx\n", c->ts,
1151 mask, omask, b->pmask));
1152 CE_DDK_DEBUG (b, c, ("Crossmatrix table:\n"));
1154 #ifdef CE_DDK_DEBUG_ENABLED
1155 for (i = 0; i < 32*3; i++) {
1156 printf ("%3d\t%s", b->dxc[i], (i%8==7)?"\n":"");
1157 printf ("%s",(i%32==31)?"\n":"");
1161 CE_DDK_DEBUG (b, c, ("Submit tsmask\n"));
1162 if (!ce_cfg_submit (b)) {
1163 CE_DDK_DEBUG (b, c, ("Fail to submit tsmask\n"));
1164 /* Do some error processing */
1168 CE_DDK_DEBUG (b, c, ("SetCrossMatrix\n"));
1169 if (!TAU32_SetCrossMatrix(b->ddk.pControllerObject, b->dxc, b->pmask)) {
1170 CE_DDK_DEBUG (b, c, ("Faild to SetCrossMatrix\n"));
1171 /* Do some error processing */
1176 void ce_set_ts (ce_chan_t *c, unsigned long ts)
1178 ce_board_t *b = c->board;
1181 if (c->ts == ts || b->chan->unfram)
1186 if (!b->chan[c->dir].use16)
1189 for (x = b->chan; x < b->chan + NCHAN; x++) {
1190 if (x == c || x->dir != c->dir)
1198 void ce_set_unfram (ce_chan_t *c, unsigned char on)
1200 TAU32_UserRequest *req;
1201 ce_board_t *b = c->board;
1202 unsigned long cfg = c->config & ~TAU32_framing_mode_mask;
1205 if (c->num != 0 || b->cr.pending + 2*32 + 3>= CONFREQSZ)
1210 if (on == c->unfram)
1215 for (i = 1; i < TAU32_CHANNELS; i++) {
1216 ce_set_ts (b->chan + i, 0);
1217 ce_set_phony (b->chan + i, 0);
1219 ce_set_use16 (b->chan + 0, 0);
1220 ce_set_use16 (b->chan + 1, 0);
1221 /* Get current value, previous ce_set request may change it */
1222 cfg = c->config & ~TAU32_framing_mode_mask;
1223 cfg |= TAU32_unframed_2048;
1225 _ce_set_ts (b->chan, ~0ul);
1227 /* XXXRIK: Do extra checks on config queue size*/
1228 if (b->ddk.Interfaces) {
1229 CE_PREREQUEST (b, c, b->cr.queue, req);
1232 req->Command = TAU32_Configure_E1;
1233 req->Io.InterfaceConfig.Interface = TAU32_E1_B;
1234 req->Io.InterfaceConfig.Config = TAU32_LineOff;
1235 req->Io.InterfaceConfig.UnframedTsMask = 0;
1236 CE_DDK_DEBUG (b, c, ("unfram: B line off\n"));
1239 CE_PREREQUEST (b, c, b->cr.queue, req);
1242 req->Command = TAU32_Configure_E1;
1243 req->Io.InterfaceConfig.Interface = TAU32_E1_A;
1244 req->Io.InterfaceConfig.Config = cfg;
1245 req->Io.InterfaceConfig.UnframedTsMask = c->ts;
1246 CE_DDK_DEBUG (b, c, ("Submit unfram\n"));
1249 cfg |= TAU32_framed_cas_cross;
1250 CE_PREREQUEST (b, c, b->cr.queue, req);
1253 req->Command = TAU32_Configure_E1;
1254 req->Io.InterfaceConfig.Interface = TAU32_E1_ALL;
1255 req->Io.InterfaceConfig.Config = cfg;
1256 req->Io.InterfaceConfig.UnframedTsMask = 0;
1257 CE_DDK_DEBUG (b, c, ("Submit framed\n"));
1264 void ce_set_phony (ce_chan_t *c, unsigned char on)
1266 TAU32_UserRequest *req;
1267 ce_board_t *b = c->board;
1268 unsigned long mask = 0;
1271 if ((c->phony && on) || (c->phony == 0 && on == 0) ||
1272 b->cr.pending >= CONFREQSZ)
1275 CE_PREREQUEST (b, c, b->cr.queue, req);
1279 req->Command = TAU32_Configure_Channel;
1280 req->Io.InterfaceConfig.Config = on ? TAU32_TMA :
1281 (TAU32_HDLC | TAU32_fr_rx_splitcheck | TAU32_fr_rx_fitcheck);
1282 req->Io.ChannelNumber = c->num;
1283 CE_DDK_DEBUG (b, c, ("Submit phony\n"));
1284 if (!ce_cfg_submit (b)) {
1285 /* Do some error processing */
1289 for (i = 0; i < 32; i++) {
1290 int t = b->dxc[i] % 32;
1291 if (b->dxc[i] < (c->dir?64:32) || b->dxc[i] > (c->dir?95:63))
1293 if (c->ts & (1ul << t))
1297 CE_DDK_DEBUG (b, c, ("phony mask:%lx\n", mask));
1305 c->phony = on ? 1 : 0;
1307 CE_DDK_DEBUG (b, c, ("Submit (setcrosmatrix) phony\n"));
1308 if (!TAU32_SetCrossMatrix(b->ddk.pControllerObject, b->dxc, b->pmask)) {
1309 /* Do some error processing */
1314 void ce_set_scrambler (ce_chan_t *c, unsigned char on)
1316 TAU32_UserRequest *req;
1317 unsigned long cfg = c->config & ~TAU32_scrambler;
1318 ce_board_t *b = c->board;
1320 if (c->num != 0 || c->unfram == 0 || b->cr.pending >= CONFREQSZ)
1323 if (on == c->scrambler)
1326 cfg |= on ? TAU32_scrambler : 0;
1327 CE_PREREQUEST (b, c, b->cr.queue, req);
1330 req->Command = TAU32_Configure_E1;
1331 req->Io.InterfaceConfig.Interface = c->num ? TAU32_E1_B : TAU32_E1_A;
1332 req->Io.InterfaceConfig.Config = cfg;
1333 req->Io.InterfaceConfig.UnframedTsMask = c->ts;
1334 CE_DDK_DEBUG (b, c, ("Submit scrambler\n"));
1335 if (ce_cfg_submit (b)) {
1336 c->scrambler = on ? 1 : 0;
1341 void ce_set_monitor (ce_chan_t *c, unsigned char on)
1343 TAU32_UserRequest *req;
1344 unsigned long cfg = c->config & ~TAU32_monitor;
1345 ce_board_t *b = c->board;
1347 if (c->num >= b->ddk.Interfaces || b->cr.pending >= CONFREQSZ)
1350 if (on == c->monitor)
1353 cfg |= on ? TAU32_monitor : 0;
1354 CE_PREREQUEST (b, c, b->cr.queue, req);
1357 req->Command = TAU32_Configure_E1;
1358 req->Io.InterfaceConfig.Interface = c->num ? TAU32_E1_B : TAU32_E1_A;
1359 req->Io.InterfaceConfig.Config = cfg;
1360 req->Io.InterfaceConfig.UnframedTsMask = c->ts;
1361 CE_DDK_DEBUG (b, c, ("Submit monitor\n"));
1362 if (ce_cfg_submit (b)) {
1363 c->monitor = on ? 1 : 0;
1368 static void _ce_submit_configure_e1 (ce_chan_t *c, char *rname)
1370 TAU32_UserRequest *req;
1371 ce_board_t *b = c->board;
1373 CE_PREREQUEST (b, c, b->cr.queue, req);
1376 req->Command = TAU32_Configure_E1;
1377 req->Io.InterfaceConfig.Interface = c->num == 0 ? TAU32_E1_A : TAU32_E1_B;
1378 req->Io.InterfaceConfig.Config = c->config;
1379 req->Io.InterfaceConfig.UnframedTsMask = c->ts;
1380 CE_DDK_DEBUG (b, c, ("Submit %s\n", rname ? rname : ""));
1381 if (!ce_cfg_submit (b)) {
1382 CE_DDK_DEBUG (b, c, ("Fail to submit %s\n", rname?rname:""));
1383 /* Do some error processing */
1388 void ce_set_use16 (ce_chan_t *c, unsigned char on)
1390 ce_board_t *b = c->board;
1392 unsigned long cfg[2];
1395 if (c->num >= b->ddk.Interfaces || b->cr.pending + 2 >= CONFREQSZ)
1398 cfg[0] = b->chan[0].config & ~TAU32_framing_mode_mask;
1399 cfg[1] = b->chan[1].config & ~TAU32_framing_mode_mask;
1403 if (c->use16 == on || b->chan->unfram)
1406 use[0] = b->chan[0].use16;
1407 use[1] = b->chan[1].use16;
1412 if (b->ddk.Interfaces == 1) {
1413 cfg[0] |= on ? TAU32_framed_cas_set : TAU32_framed_no_cas;
1415 if (use[0] == 0 && use[1] == 0) {
1416 cfg[0] |= TAU32_framed_cas_cross;
1417 cfg[1] |= TAU32_framed_cas_cross;
1418 } else if (use[0] == 0) {
1419 cfg[0] |= TAU32_framed_cas_set;
1420 cfg[1] |= TAU32_framed_no_cas;
1421 } else if (use[1] == 0) {
1422 cfg[0] |= TAU32_framed_no_cas;
1423 cfg[1] |= TAU32_framed_cas_set;
1425 cfg[0] |= TAU32_framed_no_cas;
1426 cfg[1] |= TAU32_framed_no_cas;
1432 for (x = b->chan; !on && x < b->chan + NCHAN; x++) {
1433 if (x->dir == c->num && x->ts & (1ul<<16)) {
1434 ce_set_ts (x, x->ts);
1439 if (cfg[0] != b->chan[0].config) {
1440 b->chan[0].config = cfg[0];
1441 _ce_submit_configure_e1 (b->chan + 0, "use16");
1444 if (cfg[1] != b->chan[1].config) {
1445 b->chan[1].config = cfg[1];
1446 _ce_submit_configure_e1 (b->chan + 1, "use16");
1450 void ce_set_crc4 (ce_chan_t *c, unsigned char on)
1452 TAU32_UserRequest *req;
1453 unsigned long cfg = c->config & ~TAU32_crc4_mf;
1454 ce_board_t *b = c->board;
1456 if (c->num >= b->ddk.Interfaces || b->cr.pending >= CONFREQSZ)
1459 if (on == c->crc4 || b->chan->unfram)
1462 cfg |= on ? TAU32_crc4_mf : 0;
1463 CE_PREREQUEST (b, c, b->cr.queue, req);
1466 req->Command = TAU32_Configure_E1;
1467 req->Io.InterfaceConfig.Interface = c->num ? TAU32_E1_B : TAU32_E1_A;
1468 req->Io.InterfaceConfig.Config = cfg;
1469 req->Io.InterfaceConfig.UnframedTsMask = c->ts;
1470 CE_DDK_DEBUG (b, c, ("Submit crc4\n"));
1471 if (ce_cfg_submit (b)) {
1472 c->crc4 = on ? 1 : 0;
1477 void ce_set_gsyn (ce_chan_t *c, int syn)
1479 ce_board_t *b = c->board;
1482 if (c->num >= b->ddk.Interfaces)
1485 if (syn == GSYN_RCV)
1486 syn = c->num ? GSYN_RCV1 : GSYN_RCV0;
1489 default: mode = TAU32_SYNC_INTERNAL; break;
1490 case GSYN_RCV0: mode = TAU32_SYNC_RCV_A; break;
1491 case GSYN_RCV1: mode = TAU32_SYNC_RCV_B; break;
1494 CE_DDK_DEBUG (b, c, ("Set Sync Mode\n"));
1495 if (TAU32_SetSyncMode (b->ddk.pControllerObject, mode)) {
1496 b->chan->gsyn = syn;
1497 if (b->ddk.Interfaces > 1)
1498 (b->chan + 1)->gsyn = syn;
1502 int ce_get_cable (ce_chan_t *c)
1504 ce_board_t *b = c->board;
1505 if (c->num >= b->ddk.Interfaces)
1511 void ce_set_dir (ce_chan_t *c, int dir)
1513 ce_board_t *b = c->board;
1515 if (b->cr.pending + 1>= CONFREQSZ || c->dir == dir)