Add __DragonFly__
[dragonfly.git] / sys / net / i4b / layer1 / itjc / i4b_itjc_pci.c
1 /*
2  *   Copyright (c) 2000, 2001 Sergio Prallon. All rights reserved.
3  *
4  *   Redistribution and use in source and binary forms, with or without
5  *   modification, are permitted provided that the following conditions
6  *   are met:
7  *
8  *   1. Redistributions of source code must retain the above copyright
9  *      notice, this list of conditions and the following disclaimer.
10  *   2. Redistributions in binary form must reproduce the above copyright
11  *      notice, this list of conditions and the following disclaimer in the
12  *      documentation and/or other materials provided with the distribution.
13  *   3. Neither the name of the author nor the names of any co-contributors
14  *      may be used to endorse or promote products derived from this software
15  *      without specific prior written permission.
16  *   4. Altered versions must be plainly marked as such, and must not be
17  *      misrepresented as being the original software and/or documentation.
18  *   
19  *   THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  *   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  *   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  *   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  *   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  *   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  *   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  *   SUCH DAMAGE.
30  *
31  *---------------------------------------------------------------------------
32  *
33  *      i4b_itjc_pci.c: NetJet-S hardware driver
34  *      ----------------------------------------
35  *
36  * $FreeBSD: src/sys/i4b/layer1/itjc/i4b_itjc_pci.c,v 1.1.2.1 2001/08/10 14:08:39 obrien Exp $
37  * $DragonFly: src/sys/net/i4b/layer1/itjc/i4b_itjc_pci.c,v 1.5 2004/02/13 17:45:50 joerg Exp $
38  *
39  *      last edit-date: [Thu Jan 11 11:29:38 2001]
40  *
41  *---------------------------------------------------------------------------*/
42
43 #include "use_itjc.h"
44 #include "use_pci.h"
45 #include "opt_i4b.h"
46
47 #if (NITJC > 0)
48
49 #include <sys/param.h>
50 #include <sys/kernel.h>
51 #include <sys/systm.h>
52 #include <sys/mbuf.h>
53
54 #include <machine/clock.h>
55 #include <machine/bus_pio.h>
56 #include <machine/bus.h>
57 #include <machine/resource.h>
58 #include <sys/bus.h>
59 #include <sys/rman.h>
60
61 #include <bus/pci/pcireg.h>
62 #include <bus/pci/pcivar.h>
63
64 #include <sys/socket.h>
65 #include <net/if.h>
66
67 #include <net/i4b/include/machine/i4b_debug.h>
68 #include <net/i4b/include/machine/i4b_ioctl.h>
69 #include <net/i4b/include/machine/i4b_trace.h>
70
71 #include "../../include/i4b_global.h"
72 #include "../../include/i4b_mbuf.h"
73
74 #include "../i4b_l1.h"
75
76 #include "i4b_hdlc.h"   /* XXXXXXXXXXXXXXXXXXXXXXXX */
77
78 #include "../isic/i4b_isic.h"
79 #include "../isic/i4b_isac.h"
80
81 #include "i4b_itjc_ext.h"
82
83 #define PCI_TJNET_VID (0xe159)
84 #define PCI_TJ300_DID (0x0001)
85
86
87 /*
88  * Function prototypes
89  */
90
91 static int  itjc_probe(device_t dev);
92 static int  itjc_attach(device_t dev);
93 static void itjc_shutdown(device_t dev);
94 static void itjc_intr(void *xsc);
95 static int  itjc_dma_start(struct l1_softc *sc);
96 static void itjc_dma_stop(struct l1_softc *sc);
97 static void itjc_isac_intr(struct l1_softc *sc);
98 static void itjc_init_linktab(struct l1_softc *sc);
99 static void itjc_bchannel_setup(int unit, int h_chan, int bprot, 
100         int activate);
101 static void itjc_bchannel_stat(int unit, int h_chan, bchan_statistics_t *bsp);
102
103
104 /*
105  * Shorter names to bus resource manager routines.
106  */
107
108 #define itjc_bus_setup(sc)                                              \
109         bus_space_handle_t h =                                          \
110                 rman_get_bushandle((sc)->sc_resources.io_base[0]);      \
111         bus_space_tag_t    t =                                          \
112                 rman_get_bustag((sc)->sc_resources.io_base[0]);
113
114 #define itjc_read_1(port)       (bus_space_read_1(t, h, (port)))
115 #define itjc_read_4(port)       (bus_space_read_4(t, h, (port)))
116 #define itjc_write_1(port, data) (bus_space_write_1(t, h, (port), (data)))
117 #define itjc_write_4(port, data) (bus_space_write_4(t, h, (port), (data)))
118 #define itjc_read_multi_1(port, buf, size)                              \
119         (bus_space_read_multi_1(t, h, (port), (buf), (size)))
120 #define itjc_write_multi_1(port, buf, size)                             \
121         (bus_space_write_multi_1(t, h, (port), (buf), (size)))
122
123
124 /*---------------------------------------------------------------------------*
125  *      Glue data to register ourselves as a PCI device driver.
126  *---------------------------------------------------------------------------*/
127
128 static device_method_t itjc_pci_methods[] =
129 {
130         /* Device interface */
131         DEVMETHOD(device_probe,         itjc_probe),
132         DEVMETHOD(device_attach,        itjc_attach),
133         DEVMETHOD(device_shutdown,      itjc_shutdown),
134
135         /* bus interface */
136         DEVMETHOD(bus_print_child,      bus_generic_print_child),
137         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
138
139         { 0, 0 }
140 };
141
142 static driver_t itjc_pci_driver =
143 {
144         "itjc",
145         itjc_pci_methods,
146         sizeof(struct l1_softc)
147 };
148
149 static devclass_t itjc_pci_devclass;
150
151 DRIVER_MODULE(netjet, pci, itjc_pci_driver, itjc_pci_devclass, 0, 0);
152
153 /*
154  * Jump table for multiplex routines.
155  */
156
157 struct i4b_l1mux_func itjc_l1mux_func =
158 {
159         itjc_ret_linktab,
160         itjc_set_linktab,
161         itjc_mph_command_req,
162         itjc_ph_data_req,
163         itjc_ph_activate_req,
164 };
165
166 struct l1_softc *itjc_scp[ITJC_MAXUNIT];
167
168
169 /*---------------------------------------------------------------------------*
170  *      Tiger300/320 PCI ASIC registers.
171  *---------------------------------------------------------------------------*/
172
173 /*
174  *      Register offsets from i/o base.
175  */
176 enum tiger_regs
177 {
178         TIGER_RESET_PIB_CL_TIME = 0x00,
179         TIGER_DMA_OPER          = 0x01,
180         TIGER_AUX_PORT_CNTL     = 0x02,
181         TIGER_AUX_PORT_DATA     = 0x03,
182         TIGER_INT0_MASK         = 0x04,
183         TIGER_INT1_MASK         = 0x05,
184         TIGER_INT0_STATUS       = 0x06,
185         TIGER_INT1_STATUS       = 0x07,
186         TIGER_DMA_WR_START_ADDR = 0x08,
187         TIGER_DMA_WR_INT_ADDR   = 0x0C,
188         TIGER_DMA_WR_END_ADDR   = 0x10,
189         TIGER_DMA_WR_CURR_ADDR  = 0x14,
190         TIGER_DMA_RD_START_ADDR = 0x18,
191         TIGER_DMA_RD_INT_ADDR   = 0x1C,
192         TIGER_DMA_RD_END_ADDR   = 0x20,
193         TIGER_DMA_RD_CURR_ADDR  = 0x24,
194         TIGER_PULSE_COUNTER     = 0x28,
195 };
196
197 /*
198  * Bits on the above registers.
199  */
200
201 enum tiger_reg_bits
202 {
203 /* Reset and PIB Cycle Timing */
204
205         TIGER_DMA_OP_MODE_MASK          = 0x80,
206                 TIGER_SELF_ADDR_DMA     = 0x00, /* Wrap around ending addr */
207                 TIGER_NORMAL_DMA        = 0x80, /* Stop at ending addr */
208
209         TIGER_DMA_INT_MODE_MASK         = 0x40,
210                 TIGER_DONT_LATCH_DMA_INT= 0x00, /* Bits on int0 status will be
211                                                    set only while curr addr
212                                                    equals int or end addr */
213                 TIGER_LATCH_DMA_INT     = 0x40, /* Bits on int0 status remain
214                                                    set until cleared by CPU */
215
216         TIGER_PIB_CYCLE_TIMING_MASK     = 0x30,
217                 TIGER_PIB_3_CYCLES      = 0x00,
218                 TIGER_PIB_5_CYCLES      = 0x01,
219                 TIGER_PIB_12_CYCLES     = 0x10,
220
221         TIGER_RESET_MASK                = 0x0F,
222                 TIGER_RESET_PULSE_COUNT = 0x08,
223                 TIGER_RESET_SERIAL_PORT = 0x04,
224                 TIGER_RESET_DMA_LOGIC   = 0x02,
225                 TIGER_RESET_EXTERNAL    = 0x01,
226                 TIGER_RESET_ALL         = 0x0F,
227         
228 /* DMA Operation */
229         TIGER_DMA_RESTART_MASK          = 0x02,
230                 TIGER_HOLD_DMA          = 0x00,
231                 TIGER_RESTART_DMA       = 0x00,
232
233         TIGER_DMA_ENABLE_MASK           = 0x01,
234                 TIGER_ENABLE_DMA        = 0x01,
235                 TIGER_DISABLE_DMA       = 0x00,
236
237 /* AUX Port Control & Data plus Interrupt 1 Mask & Status  */
238         TIGER_AUX_7_MASK                = 0x80,
239         TIGER_AUX_6_MASK                = 0x40,
240         TIGER_AUX_5_MASK                = 0x20,
241         TIGER_AUX_4_MASK                = 0x10,
242         TIGER_ISAC_INT_MASK             = 0x10,
243         TIGER_AUX_3_MASK                = 0x08,
244         TIGER_AUX_2_MASK                = 0x04,
245         TIGER_AUX_1_MASK                = 0x02,
246         TIGER_AUX_0_MASK                = 0x01,
247
248 /* AUX Port Control */
249                 TIGER_AUX_7_IS_INPUT    = 0x00,
250                 TIGER_AUX_7_IS_OUTPUT   = 0x80,
251                 TIGER_AUX_6_IS_INPUT    = 0x00,
252                 TIGER_AUX_6_IS_OUTPUT   = 0x40,
253                 TIGER_AUX_5_IS_INPUT    = 0x00,
254                 TIGER_AUX_5_IS_OUTPUT   = 0x20,
255                 TIGER_AUX_4_IS_INPUT    = 0x00,
256                 TIGER_AUX_4_IS_OUTPUT   = 0x10,
257                 TIGER_AUX_3_IS_INPUT    = 0x00,
258                 TIGER_AUX_3_IS_OUTPUT   = 0x80,
259                 TIGER_AUX_2_IS_INPUT    = 0x00,
260                 TIGER_AUX_2_IS_OUTPUT   = 0x40,
261                 TIGER_AUX_1_IS_INPUT    = 0x00,
262                 TIGER_AUX_1_IS_OUTPUT   = 0x20,
263                 TIGER_AUX_0_IS_INPUT    = 0x00,
264                 TIGER_AUX_0_IS_OUTPUT   = 0x10,
265                 TIGER_AUX_NJ_DEFAULT    = 0xEF, /* All but ISAC int is output */
266
267 /* Interrupt 0 Mask & Status */
268         TIGER_PCI_TARGET_ABORT_INT_MASK = 0x20,
269                 TIGER_NO_TGT_ABORT_INT  = 0x00,
270                 TIGER_TARGET_ABORT_INT  = 0x20,
271         TIGER_PCI_MASTER_ABORT_INT_MASK = 0x10,
272                 TIGER_NO_MST_ABORT_INT  = 0x00,
273                 TIGER_MASTER_ABORT_INT  = 0x10,
274         TIGER_DMA_RD_END_INT_MASK       = 0x08,
275                 TIGER_NO_RD_END_INT     = 0x00,
276                 TIGER_RD_END_INT        = 0x08,
277         TIGER_DMA_RD_INT_INT_MASK       = 0x04,
278                 TIGER_NO_RD_INT_INT     = 0x00,
279                 TIGER_RD_INT_INT        = 0x04,
280         TIGER_DMA_WR_END_INT_MASK       = 0x02,
281                 TIGER_NO_WR_END_INT     = 0x00,
282                 TIGER_WR_END_INT        = 0x02,
283         TIGER_DMA_WR_INT_INT_MASK       = 0x01,
284                 TIGER_NO_WR_INT_INT     = 0x00,
285                 TIGER_WR_INT_INT        = 0x01,
286
287 /* Interrupt 1 Mask & Status */
288                 TIGER_NO_AUX_7_INT      = 0x00,
289                 TIGER_AUX_7_INT         = 0x80,
290                 TIGER_NO_AUX_6_INT      = 0x00,
291                 TIGER_AUX_6_INT         = 0x40,
292                 TIGER_NO_AUX_5_INT      = 0x00,
293                 TIGER_AUX_5_INT         = 0x20,
294                 TIGER_NO_AUX_4_INT      = 0x00,
295                 TIGER_AUX_4_INT         = 0x10,
296                 TIGER_NO_ISAC_INT       = 0x00,
297                 TIGER_ISAC_INT          = 0x10,
298                 TIGER_NO_AUX_3_INT      = 0x00,
299                 TIGER_AUX_3_INT         = 0x08,
300                 TIGER_NO_AUX_2_INT      = 0x00,
301                 TIGER_AUX_2_INT         = 0x04,
302                 TIGER_NO_AUX_1_INT      = 0x00,
303                 TIGER_AUX_1_INT         = 0x02,
304                 TIGER_NO_AUX_0_INT      = 0x00,
305                 TIGER_AUX_0_INT         = 0x01
306 };
307
308 /*
309  * Peripheral Interface Bus definitions. This is an ISA like bus
310  * created by the Tiger ASIC to keep ISA chips like the ISAC happy
311  * on a PCI environment.
312  *
313  * Since the PIB only supplies 4 addressing lines, the 2 higher bits
314  * (A4 & A5) of the ISAC register addresses are wired on the 2 lower
315  * AUX lines. Another restriction is that all I/O to the PIB (8bit
316  * wide) is mapped on the PCI side as 32bit data. So the PCI address
317  * of a given ISAC register has to be multiplied by 4 before being
318  * added to the PIB base offset.
319  */
320 enum tiger_pib_regs_defs
321 {
322         /* Offset from the I/O base to the ISAC registers. */
323         PIB_OFFSET              = 0xC0,
324         PIB_LO_ADDR_MASK        = 0x0F,         
325         PIB_HI_ADDR_MASK        = 0x30,
326         PIB_LO_ADDR_SHIFT       = 2,    /* Align on dword boundary */
327         PIB_HI_ADDR_SHIFT       = 4     /* Right shift to AUX_1 & AUX_0 */
328 };
329
330
331 #define itjc_set_pib_addr_msb(a)                                        \
332 (                                                                       \
333         itjc_write_1(TIGER_AUX_PORT_DATA,                               \
334                 ((a) & PIB_HI_ADDR_MASK) >> PIB_HI_ADDR_SHIFT)          \
335 )
336
337 #define itjc_pib_2_pci(a)                                               \
338 (                                                                       \
339         (((a) & PIB_LO_ADDR_MASK) << PIB_LO_ADDR_SHIFT) + PIB_OFFSET    \
340 )
341
342 #define itjc_get_dma_offset(ctx,reg)                                    \
343 (                                                                       \
344         (u_int16_t)((bus_addr_t)itjc_read_4((reg)) - (ctx)->bus_addr)   \
345 )
346
347
348 /*
349  * IOM-2 serial channel 0 DMA data ring buffers.
350  *
351  * The Tiger300/320 ASIC do not nothing more than transfer via DMA the
352  * first 32 bits of every IOM-2 frame on the serial interface to the
353  * ISAC. So we have no framing/deframing facilities like we would have
354  * with an HSCX, having to do the job with CPU cycles. On the plus side
355  * we are able to specify large rings which can limit the occurrence of
356  * over/underruns.
357  */
358
359 enum
360 {
361         ITJC_RING_SLOT_WORDS    = 64,
362         ITJC_RING_WORDS         = 3 * ITJC_RING_SLOT_WORDS,
363         ITJC_RING_SLOT_BYTES    = 4 * ITJC_RING_SLOT_WORDS,
364         ITJC_RING_BYTES         = 4 * ITJC_RING_WORDS,
365         ITJC_DMA_POOL_WORDS     = 2 * ITJC_RING_WORDS,
366         ITJC_DMA_POOL_BYTES     = 4 * ITJC_DMA_POOL_WORDS
367 };
368
369 #define itjc_ring_add(x, d)     (((x) + 4 * (d)) % ITJC_RING_BYTES)
370 #define itjc_ring_sub(x, d)     (((x) + ITJC_RING_BYTES - 4 * (d))      \
371                                         % ITJC_RING_BYTES)
372
373
374 enum
375 {
376         TIGER_CH_A              = 0,
377         TIGER_CH_B              = 1,
378
379         HSCX_CH_A               = 0,    /* For compatibility reasons. */
380         HSCX_CH_B               = 1,
381 };
382
383 enum
384 {
385         ITJC_TEL_SILENCE_BYTE   = 0x00,
386         ITJC_HDLC_FLAG_BYTE     = 0x7E,
387         ITJC_HDLC_ABORT_BYTE    = 0xFF
388 };
389
390 /*
391  * Hardware DMA control block (one per card).
392  */
393 typedef enum
394 {
395         ITJC_DS_LOAD_FAILED     = -1,
396         ITJC_DS_FREE            =  0,
397         ITJC_DS_LOADING,
398         ITJC_DS_STOPPED,
399         ITJC_DS_RUNNING
400 }
401         dma_state_t;
402
403 typedef struct
404 {
405         dma_state_t     state;
406         u_int8_t        *pool;
407         bus_addr_t      bus_addr;
408         bus_dma_tag_t   tag;
409         bus_dmamap_t    map;
410         int             error;
411 }
412         dma_context_t;
413
414 dma_context_t
415         dma_context     [ ITJC_MAXUNIT ];
416
417 /*
418  * B-channel DMA control blocks (4 per card -- 1 RX & 1 TX per channel).
419  */
420 typedef enum
421 {
422         ITJC_RS_IDLE    = 0,
423         ITJC_RS_ACTIVE
424 }
425         dma_rx_state_t;
426
427 typedef enum
428 {
429         ITJC_TS_IDLE    = 0,
430         ITJC_TS_ACTIVE,
431         ITJC_TS_AFTER_XDU
432 }
433         dma_tx_state_t;
434
435 typedef struct
436 {
437         u_int8_t        *ring;
438         bus_addr_t      bus_addr;
439         u_int16_t       next_read;
440         u_int16_t       hdlc_len;
441         u_int16_t       hdlc_tmp;
442         u_int16_t       hdlc_crc;
443         u_int16_t       hdlc_ib;
444         u_int8_t        hdlc_blevel;
445         u_int8_t        hdlc_flag;
446         dma_rx_state_t  state;
447 }
448         dma_rx_context_t;
449
450 typedef struct
451 {
452         u_int8_t        *ring;
453         bus_addr_t      bus_addr;
454         u_int16_t       next_write;
455         u_int32_t       hdlc_tmp;
456         u_int16_t       hdlc_blevel;
457         u_int16_t       hdlc_crc;
458         u_int16_t       hdlc_ib;
459         u_int16_t       next_frame;
460         u_int16_t       filled;
461         u_int8_t        hdlc_flag;
462         dma_tx_state_t  state;
463 }
464         dma_tx_context_t;
465
466 dma_rx_context_t
467         dma_rx_context  [ ITJC_MAXUNIT ] [ 2 ];
468
469 dma_tx_context_t
470         dma_tx_context  [ ITJC_MAXUNIT ] [ 2 ];
471
472 /*
473  * Used by the mbuf handling functions.
474  */
475 typedef enum
476 {
477         ITJC_MB_CURR = 0,
478         ITJC_MB_NEXT = 1,
479         ITJC_MB_NEW  = 2
480 }
481         which_mb_t;
482
483
484 /*---------------------------------------------------------------------------*
485  *      itjc_map_callback - get DMA bus address from resource mgr.
486  *---------------------------------------------------------------------------*/
487 static void
488 itjc_map_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
489 {
490         dma_context_t           *ctx = (dma_context_t *)arg;
491
492         if (error)
493         {
494                 ctx->error = error;
495                 ctx->state = ITJC_DS_LOAD_FAILED;
496                 return;
497         }
498
499         ctx->bus_addr = segs->ds_addr;
500         ctx->state = ITJC_DS_STOPPED;
501 }
502
503
504 /*---------------------------------------------------------------------------*
505  *      itjc_dma_start - Complete DMA setup & start the Tiger DMA engine.
506  *---------------------------------------------------------------------------*/
507 static int
508 itjc_dma_start(struct l1_softc *sc)
509 {
510         int                     unit    = sc->sc_unit;
511         dma_context_t           *ctx    = &dma_context[unit];
512         dma_rx_context_t        *rxc    = &dma_rx_context[unit][0];
513         dma_tx_context_t        *txc    = &dma_tx_context[unit][0];
514         bus_addr_t              ba;
515         u_int8_t                i;
516         u_int32_t               *pool_end,
517                                 *ip;
518
519         itjc_bus_setup(sc);
520
521         /* See if it is already running. */
522
523         if (ctx->state == ITJC_DS_RUNNING)
524                 return 0;
525
526         if (ctx->state == ITJC_DS_LOAD_FAILED)
527         {
528                 NDBGL1(L1_ERROR, "itjc%d: dma_start: DMA map loading "
529                         "failed (error=%d).\n", unit, ctx->error);
530                 return 1;
531         }
532
533         if (ctx->state != ITJC_DS_STOPPED)
534         {
535                 NDBGL1(L1_ERROR, "itjc%d: dma_start: Unexpected DMA "
536                         "state (%d).\n", unit, ctx->state);
537                 return 1;
538         }
539
540         /*
541          * Initialize the DMA control structures (hardware & B-channel).
542          */
543         ba = ctx->bus_addr;
544
545         txc->ring = ctx->pool + TIGER_CH_A;
546         rxc->ring = ctx->pool + TIGER_CH_A + ITJC_RING_BYTES;
547
548         txc->bus_addr = ba;
549         rxc->bus_addr = ba + ITJC_RING_BYTES;
550
551         ++rxc; ++txc;
552
553         txc->ring = ctx->pool + TIGER_CH_B;
554         rxc->ring = ctx->pool + TIGER_CH_B + ITJC_RING_BYTES;
555
556         txc->bus_addr = ba;
557         rxc->bus_addr = ba + ITJC_RING_BYTES;
558
559         /*
560          * Fill the DMA ring buffers with IOM-2 channel 0 frames made of
561          * idle/abort sequences for the B & D channels and NOP for IOM-2
562          * cmd/ind, monitor handshake & data.
563          */
564         pool_end = (u_int32_t *)ctx->pool + ITJC_DMA_POOL_WORDS;
565         for (ip = (u_int32_t *)ctx->pool; ip < pool_end; ++ip)
566                 *ip = 0xFFFFFFFF;
567
568         /*
569          * Program the Tiger DMA gears.
570          */
571
572         itjc_write_4(TIGER_DMA_WR_START_ADDR, ba);
573         itjc_write_4(TIGER_DMA_WR_INT_ADDR, ba + ITJC_RING_SLOT_BYTES - 4);
574         itjc_write_4(TIGER_DMA_WR_END_ADDR, ba + ITJC_RING_BYTES - 4);
575
576         ba += ITJC_RING_BYTES;
577
578         itjc_write_4(TIGER_DMA_RD_START_ADDR, ba);
579         itjc_write_4(TIGER_DMA_RD_INT_ADDR, ba + ITJC_RING_SLOT_BYTES * 2 - 4);
580         itjc_write_4(TIGER_DMA_RD_END_ADDR, ba + ITJC_RING_BYTES - 4);
581
582         itjc_write_1(TIGER_INT0_MASK, 
583                 TIGER_WR_END_INT | TIGER_WR_INT_INT | TIGER_RD_INT_INT);
584
585         itjc_write_1(TIGER_DMA_OPER, TIGER_ENABLE_DMA);
586
587         /*
588          * See if it really started.
589          */
590         ba = itjc_read_4(TIGER_DMA_RD_CURR_ADDR);
591         for (i = 0; i < 10; ++i)
592         {
593                 DELAY(SEC_DELAY/1000);
594                 if (ba != itjc_read_4(TIGER_DMA_RD_CURR_ADDR))
595                 {
596                         ctx->state = ITJC_DS_RUNNING;
597                         return 0;
598                 }
599         }
600
601         NDBGL1(L1_ERROR, "itjc%d: dma_start: DMA start failed.\n ", unit);
602         return 1;
603 }
604
605
606 /*---------------------------------------------------------------------------*
607  *      itjc_dma_stop - Stop the Tiger DMA engine.
608  *---------------------------------------------------------------------------*/
609 void
610 itjc_dma_stop(struct l1_softc *sc)
611 {
612         dma_context_t           *ctx    = &dma_context[sc->sc_unit];
613
614         itjc_bus_setup(sc);
615
616         /* Only stop the DMA if it is running. */
617
618         if (ctx->state != ITJC_DS_RUNNING)
619                 return;
620
621         itjc_write_1(TIGER_DMA_OPER, TIGER_DISABLE_DMA);
622         DELAY(SEC_DELAY/1000);
623
624         ctx->state = ITJC_DS_STOPPED;
625 }
626
627
628 /*---------------------------------------------------------------------------*
629  *      itjc_bchannel_dma_setup - The DMA side of itjc_bchannel_setup.
630  *---------------------------------------------------------------------------*/
631 static void
632 itjc_bchannel_dma_setup(struct l1_softc *sc, int h_chan, int activate)
633 {
634         dma_rx_context_t        *rxc  = &dma_rx_context[sc->sc_unit][h_chan];
635         dma_tx_context_t        *txc  = &dma_tx_context[sc->sc_unit][h_chan];
636
637         l1_bchan_state_t        *chan = &sc->sc_chan[h_chan];
638
639         u_int8_t                fill_byte,
640                                 *ring_end,
641                                 *cp;
642
643         int                     s = SPLI4B();
644
645         itjc_bus_setup(sc);
646
647         if (activate)
648         {
649                 /*
650                  * Get the DMA engine going if it's not running already.
651                  */
652                 itjc_dma_start(sc);
653
654                 rxc->hdlc_len   = rxc->hdlc_tmp    = rxc->hdlc_crc  = 0;
655                 rxc->hdlc_ib    = rxc->hdlc_blevel = rxc->hdlc_flag = 0;
656
657                 txc->hdlc_tmp   = txc->hdlc_blevel = txc->hdlc_crc  = 0;
658                 txc->hdlc_ib    = 0;
659                 txc->hdlc_flag  = 2;
660                 txc->filled     = 0;
661
662                 if (chan->bprot == BPROT_NONE)
663                         fill_byte = ITJC_TEL_SILENCE_BYTE;
664                 else
665                         fill_byte = ITJC_HDLC_ABORT_BYTE;
666
667                 ring_end = rxc->ring + ITJC_RING_BYTES;
668                 for (cp = rxc->ring; cp < ring_end; cp += 4)
669                         *cp = fill_byte;
670
671                 ring_end = txc->ring + ITJC_RING_BYTES;
672                 for (cp = txc->ring; cp < ring_end; cp += 4)
673                         *cp = fill_byte;
674
675                 rxc->next_read  =
676                         itjc_get_dma_offset(rxc, TIGER_DMA_RD_CURR_ADDR);
677
678                 txc->next_frame = txc->next_write =
679                         itjc_get_dma_offset(txc, TIGER_DMA_WR_CURR_ADDR);
680
681                 rxc->state      = ITJC_RS_ACTIVE;
682                 txc->state      = ITJC_TS_AFTER_XDU;
683         }
684         else
685         {
686                 dma_rx_context_t        *rxc2;
687
688                 txc->state      = ITJC_TS_IDLE;
689                 rxc->state      = ITJC_RS_IDLE;
690
691                 rxc2 = &dma_rx_context[sc->sc_unit][0];
692
693                 if (rxc2->state == ITJC_RS_IDLE 
694                 && rxc2[1].state == ITJC_RS_IDLE)
695                         itjc_dma_stop(sc);
696         }
697
698         splx(s);
699 }
700
701
702 /*---------------------------------------------------------------------------*
703  *      Mbuf & if_queues management routines.
704  *---------------------------------------------------------------------------*/
705
706 static u_int8_t *
707 itjc_get_rx_mbuf(l1_bchan_state_t *chan, u_int8_t **dst_end_p,
708 which_mb_t which)
709 {
710         struct mbuf     *mbuf = chan->in_mbuf;
711
712         if (mbuf == NULL && which == ITJC_MB_NEW)
713         {
714                 if ((mbuf = i4b_Bgetmbuf(BCH_MAX_DATALEN)) == NULL)
715                         panic("itjc_get_rx_mbuf: cannot allocate mbuf!");
716
717                 chan->in_mbuf  = mbuf;
718                 chan->in_cbptr = (u_int8_t *)mbuf->m_data;
719                 chan->in_len   = 0;
720         }
721
722         if (dst_end_p != NULL)
723         {
724                 if (mbuf != NULL)
725                         *dst_end_p = (u_int8_t *)(mbuf->m_data)
726                                 + BCH_MAX_DATALEN;
727                 else
728                         *dst_end_p = NULL;
729         }
730
731         return chan->in_cbptr;
732 }
733
734
735 static void
736 itjc_save_rx_mbuf(l1_bchan_state_t *chan, u_int8_t * dst)
737 {
738         struct mbuf     *mbuf = chan->in_mbuf;
739
740         if (dst != NULL && mbuf != NULL)
741         {
742                 chan->in_cbptr = dst;
743                 chan->in_len   = dst - (u_int8_t *)mbuf->m_data;
744         }
745         else if (dst == NULL && mbuf == NULL)
746         {
747                 chan->in_cbptr = NULL;
748                 chan->in_len   = 0;
749         }
750         else
751                 panic("itjc_save_rx_mbuf: stale pointer dst=%p mbuf=%p "
752                         "in_cbptr=%p in_len=%d", dst, mbuf, 
753                         chan->in_cbptr, chan->in_len);
754 }
755
756
757 static void
758 itjc_free_rx_mbuf(l1_bchan_state_t *chan)
759 {
760         struct mbuf     *mbuf = chan->in_mbuf;
761
762         if (mbuf != NULL)
763                 i4b_Bfreembuf(mbuf);
764
765         chan->in_mbuf  = NULL;
766         chan->in_cbptr = NULL;
767         chan->in_len   = 0;
768 }
769
770
771 static void
772 itjc_put_rx_mbuf(struct l1_softc *sc, l1_bchan_state_t *chan, u_int16_t len)
773 {
774         i4b_trace_hdr_t hdr;
775         struct mbuf     *mbuf    = chan->in_mbuf;
776         u_int8_t        *data    = mbuf->m_data;
777         int             activity = 1;
778
779         mbuf->m_pkthdr.len = mbuf->m_len = len;
780
781         if (sc->sc_trace & TRACE_B_RX)
782         {
783                 hdr.unit = L0ITJCUNIT(sc->sc_unit);
784                 hdr.type = (chan->channel == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
785                 hdr.dir = FROM_NT;
786                 hdr.count = ++sc->sc_trace_bcount;
787                 MICROTIME(hdr.time);
788                 i4b_l1_trace_ind(&hdr, len, data);
789         }
790
791         if (chan->bprot == BPROT_NONE)
792         {
793                 activity = ! i4b_l1_bchan_tel_silence(data, len);
794                                 
795                 /* move rx'd data to rx queue */
796
797                 if (! _IF_QFULL(&chan->rx_queue))
798                 {
799                         IF_ENQUEUE(&chan->rx_queue, mbuf);
800                 }
801                 else
802                 {
803                         i4b_Bfreembuf(mbuf);
804                         len = 0;
805                 }
806         }
807
808         if (len != 0)
809         {
810                 chan->rxcount += len;
811
812                 (*chan->isic_drvr_linktab->bch_rx_data_ready)
813                         (chan->isic_drvr_linktab->unit);
814         }
815                                 
816         if (activity)
817                 (*chan->isic_drvr_linktab->bch_activity)
818                         (chan->isic_drvr_linktab->unit, ACT_RX);
819
820         chan->in_mbuf  = NULL;
821         chan->in_cbptr = NULL;
822         chan->in_len   = 0;
823 }
824
825
826 #define itjc_free_tx_mbufs(chan)                                        \
827 {                                                                       \
828         i4b_Bfreembuf((chan)->out_mbuf_head);                           \
829         (chan)->out_mbuf_cur = (chan)->out_mbuf_head = NULL;            \
830         (chan)->out_mbuf_cur_ptr = NULL;                                \
831         (chan)->out_mbuf_cur_len = 0;                                   \
832 }
833
834
835 static u_int16_t
836 itjc_get_tx_mbuf(struct l1_softc *sc, l1_bchan_state_t *chan,
837         u_int8_t **src_p, which_mb_t which)
838 {
839         i4b_trace_hdr_t hdr;
840         struct mbuf     *mbuf = chan->out_mbuf_cur;
841         u_int8_t        activity = 1;
842         u_int16_t       len;
843         void            *data;
844
845         switch (which)
846         {
847         case ITJC_MB_CURR:
848                 if (mbuf != NULL)
849                 {
850                         *src_p = chan->out_mbuf_cur_ptr;
851                         return   chan->out_mbuf_cur_len;
852                 }
853
854                 break;
855
856         case ITJC_MB_NEXT:
857                 if (mbuf != NULL)
858                 {
859                         chan->txcount += mbuf->m_len;
860
861                         mbuf = mbuf->m_next;
862
863                         if (mbuf != NULL)
864                                 goto new_mbuf;
865                 }
866
867                 chan->out_mbuf_cur_ptr = *src_p = NULL;
868                 chan->out_mbuf_cur_len = 0;
869
870                 if (chan->out_mbuf_head != NULL)
871                 {
872                         i4b_Bfreembuf(chan->out_mbuf_head);
873                         chan->out_mbuf_head = NULL;
874                 }
875
876                 return 0;
877
878         case ITJC_MB_NEW:
879                 if (mbuf != NULL)
880                         chan->txcount += mbuf->m_len;
881         }
882
883         if (chan->out_mbuf_head != NULL)
884                 i4b_Bfreembuf(chan->out_mbuf_head);
885
886         IF_DEQUEUE(&chan->tx_queue, mbuf);
887
888         if (mbuf == NULL)
889         {
890                 chan->out_mbuf_cur = chan->out_mbuf_head = NULL;
891                 chan->out_mbuf_cur_ptr = *src_p = NULL;
892                 chan->out_mbuf_cur_len = 0;
893
894                 chan->state &= ~(HSCX_TX_ACTIVE);
895
896                 (*chan->isic_drvr_linktab->bch_tx_queue_empty)
897                         (chan->isic_drvr_linktab->unit);
898
899                 return 0;
900         }
901
902         chan->out_mbuf_head = mbuf;
903
904 new_mbuf:
905         chan->out_mbuf_cur      = mbuf;
906         chan->out_mbuf_cur_ptr  = data = mbuf->m_data;
907         chan->out_mbuf_cur_len  = len  = mbuf->m_len;
908
909         chan->state |= HSCX_TX_ACTIVE;
910
911         if (sc->sc_trace & TRACE_B_TX)
912         {
913                 hdr.unit = L0ITJCUNIT(sc->sc_unit);
914                 hdr.type = (chan->channel == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
915                 hdr.dir = FROM_TE;
916                 hdr.count = ++sc->sc_trace_bcount;
917                 MICROTIME(hdr.time);
918                 i4b_l1_trace_ind(&hdr, len, data);
919         }
920
921         if (chan->bprot == BPROT_NONE)
922                 activity = ! i4b_l1_bchan_tel_silence(data, len);
923
924         if (activity)
925                 (*chan->isic_drvr_linktab->bch_activity)
926                         (chan->isic_drvr_linktab->unit, ACT_TX);
927
928         *src_p = data;
929         return len;
930 }
931
932
933 #define itjc_save_tx_mbuf(chan, src, dst)                               \
934 (                                                                       \
935         (chan)->out_mbuf_cur != NULL ?                                  \
936         (                                                               \
937                 (chan)->out_mbuf_cur_ptr = (src),                       \
938                 (chan)->out_mbuf_cur_len = (len)                        \
939         )                                                               \
940         :                                                               \
941                 0                                                       \
942 )
943
944
945 /*---------------------------------------------------------------------------*
946  *      B-channel interrupt service routines.
947  *---------------------------------------------------------------------------*/
948
949 /*
950  * Since the Tiger ASIC doesn't produce a XMIT underflow indication,
951  * we need to deduce it ourselves. This is somewhat tricky because we
952  * are dealing with modulo m arithmetic. The idea here is to have a
953  * "XDU zone" ahead of the writing pointer sized 1/3 of total ring
954  * length (a ring slot). If the hardware DMA pointer is found there we
955  * consider that a XDU has occurred. To complete the scheme, we never
956  * let the ring have more than 2 slots of (unsent) data and adjust the
957  * interrupt registers to cause an interrupt at every slot.
958  */
959 static u_int8_t
960 itjc_xdu(struct l1_softc *sc, l1_bchan_state_t *chan, dma_tx_context_t *ctx,
961 u_int16_t *dst_p, u_int16_t *dst_end_p, u_int8_t tx_restart)
962 {
963         u_int8_t        xdu;
964
965         u_int16_t       dst_end,
966                         dst,
967                         dma,
968                         dma_l,
969                         dma_h,
970                         xdu_l,
971                         xdu_h;
972
973         itjc_bus_setup(sc);
974
975         /*
976          * Since the hardware is running, be conservative and assume
977          * the pointer location has a `fuzy' error factor.
978          */
979         dma   = itjc_get_dma_offset(ctx, TIGER_DMA_WR_CURR_ADDR);
980         dma_l = dma;
981         dma_h = itjc_ring_add(dma, 1);
982
983         dst_end = itjc_ring_sub(dma_l, ITJC_RING_SLOT_WORDS);
984
985         if (ctx->state != ITJC_TS_ACTIVE)
986         {
987                 xdu = (ctx->state == ITJC_TS_AFTER_XDU);
988                 dst = itjc_ring_add(dma_h, 4);
989                 goto done;
990         }
991
992         /*
993          * Check for xmit underruns.
994          */
995         xdu_l = dst = ctx->next_write; 
996         xdu_h = itjc_ring_add(dst, ITJC_RING_SLOT_WORDS);
997
998         if (xdu_l < xdu_h)
999                 xdu =      (xdu_l <= dma_l && dma_l < xdu_h)
1000                         || (xdu_l <= dma_h && dma_h < xdu_h);
1001         else
1002                 xdu =      (xdu_l <= dma_l || dma_l < xdu_h)
1003                         || (xdu_l <= dma_h || dma_h < xdu_h);
1004
1005         if (xdu)
1006         {
1007                 ctx->state = ITJC_TS_AFTER_XDU;
1008
1009                 dst = itjc_ring_add(dma_h, 4);
1010         }
1011         else if (tx_restart)
1012         {
1013                 /*
1014                  * See if we still can restart from immediately
1015                  * after the last frame sent. It's a XDU test but
1016                  * using the real data end on the comparsions. We
1017                  * don't consider XDU an error here because we were
1018                  * just trying to avoid send a filling gap between
1019                  * frames. If it's already sent no harm is done.
1020                  */
1021                 xdu_l = dst = ctx->next_frame; 
1022                 xdu_h = itjc_ring_add(dst, ITJC_RING_SLOT_WORDS);
1023
1024                 if (xdu_l < xdu_h)
1025                         xdu =      (xdu_l <= dma_l && dma_l < xdu_h)
1026                                 || (xdu_l <= dma_h && dma_h < xdu_h);
1027                 else
1028                         xdu =      (xdu_l <= dma_l || dma_l < xdu_h)
1029                                 || (xdu_l <= dma_h || dma_h < xdu_h);
1030
1031                 if (xdu)
1032                         dst = itjc_ring_add(dma_h, 4);
1033
1034                 xdu = 0;
1035         }
1036
1037 done:
1038         if (dst_p != NULL)
1039                 *dst_p = dst;
1040         
1041         if (dst_end_p != NULL)
1042                 *dst_end_p = dst_end;
1043
1044         ctx->next_write = dst_end;
1045
1046         return xdu;
1047 }
1048
1049
1050 #define itjc_rotate_hdlc_flag(blevel)                                   \
1051         ((u_int8_t)(0x7E7E >> (8 - (u_int8_t)((blevel) >> 8))))
1052
1053
1054 static void
1055 itjc_dma_rx_intr(struct l1_softc *sc, l1_bchan_state_t *chan,
1056 dma_rx_context_t *ctx)
1057 {
1058         u_int8_t        *ring,
1059                         *dst,
1060                         *dst_end,
1061                         flag,
1062                         blevel;
1063
1064         u_int16_t       dma,
1065                         src,
1066                         tmp2,
1067                         tmp,
1068                         len,
1069                         crc,
1070                         ib;
1071         
1072         itjc_bus_setup(sc);
1073
1074
1075         if (ctx->state == ITJC_RS_IDLE)
1076                 return;
1077
1078         ring = ctx->ring;
1079         dma = itjc_get_dma_offset(ctx, TIGER_DMA_RD_CURR_ADDR);
1080         dma = itjc_ring_sub(dma, 1);
1081         src = ctx->next_read;
1082
1083         if (chan->bprot == BPROT_NONE)
1084         {
1085                 dst = itjc_get_rx_mbuf(chan, &dst_end, ITJC_MB_CURR);
1086
1087                 while (src != dma)
1088                 {
1089                         if (dst == NULL)
1090                                 dst = itjc_get_rx_mbuf(chan, &dst_end, 
1091                                         ITJC_MB_NEW);
1092
1093                         *dst++ = ring[src];
1094                         src = itjc_ring_add(src, 1);
1095
1096                         if (dst >= dst_end)
1097                         {
1098                                 itjc_put_rx_mbuf(sc, chan, BCH_MAX_DATALEN);
1099                                 dst = dst_end = NULL;
1100                         }
1101                 }
1102                 ctx->next_read = src;
1103                 itjc_save_rx_mbuf(chan, dst);
1104                 return;
1105         }
1106
1107         blevel = ctx->hdlc_blevel;
1108         flag   = ctx->hdlc_flag;
1109         len    = ctx->hdlc_len;
1110         tmp    = ctx->hdlc_tmp;
1111         crc    = ctx->hdlc_crc;
1112         ib     = ctx->hdlc_ib;
1113
1114         dst = itjc_get_rx_mbuf(chan, NULL, ITJC_MB_CURR);
1115
1116         while (src != dma)
1117         {
1118                 HDLC_DECODE(*dst++, len, tmp, tmp2, blevel, ib, crc, flag,
1119                 {/* rdd */
1120                         tmp2 = ring[src];
1121                         src = itjc_ring_add(src, 1);
1122                 },
1123                 {/* nfr */
1124                         if (dst != NULL)
1125                                 panic("itjc_dma_rx_intr: nfrcmd with "
1126                                         "valid current frame");
1127
1128                         dst = itjc_get_rx_mbuf(chan, &dst_end, ITJC_MB_NEW);
1129                         len = dst_end - dst;
1130                 },
1131                 {/* cfr */
1132                         len = BCH_MAX_DATALEN - len;
1133
1134                         if ((!len) || (len > BCH_MAX_DATALEN))
1135                         {
1136                                 /*
1137                                  * NOTE: frames without any data, only crc
1138                                  * field, should be silently discared.
1139                                  */
1140                                 NDBGL1(L1_S_MSG, "itjc_dma_rx_intr: "
1141                                         "bad frame (len=%d, unit=%d)",
1142                                         len, sc->sc_unit);
1143
1144                                 itjc_free_rx_mbuf(chan);
1145
1146                                 goto s0;
1147                         }
1148
1149                         if (crc)
1150                         {
1151                                 NDBGL1(L1_S_ERR,
1152                                         "CRC (crc=0x%04x, len=%d, unit=%d)",
1153                                         crc, len, sc->sc_unit);
1154
1155                                 itjc_free_rx_mbuf(chan);
1156
1157                                 goto s0;
1158                         }
1159
1160                         itjc_put_rx_mbuf(sc, chan, len);
1161
1162                 s0:
1163                         dst = NULL;
1164                         len = 0;
1165                 },
1166                 {/* rab */
1167                         NDBGL1(L1_S_ERR, "Read Abort (unit=%d)", sc->sc_unit);
1168
1169                         itjc_free_rx_mbuf(chan);
1170                         dst = NULL;
1171                         len = 0;
1172                 },
1173                 {/* rdo */
1174                         NDBGL1(L1_S_ERR, "RDO (unit=%d) dma=%d src=%d",
1175                                 sc->sc_unit, dma, src);
1176
1177                         itjc_free_rx_mbuf(chan);
1178                         dst = NULL;
1179                         len = 0;
1180                 },
1181                 continue,
1182                 d);
1183         }
1184
1185         itjc_save_rx_mbuf(chan, dst);
1186
1187         ctx->next_read  = src;
1188         ctx->hdlc_blevel= blevel;
1189         ctx->hdlc_flag  = flag;
1190         ctx->hdlc_len   = len;
1191         ctx->hdlc_tmp   = tmp;
1192         ctx->hdlc_crc   = crc;
1193         ctx->hdlc_ib    = ib;
1194 }
1195
1196
1197 /*
1198  * The HDLC side of itjc_dma_tx_intr. We made a separate function
1199  * to improve readability and (perhaps) help the compiler with
1200  * register allocation.
1201  */
1202 static void
1203 itjc_hdlc_encode(struct l1_softc *sc, l1_bchan_state_t *chan,
1204 dma_tx_context_t * ctx)
1205 {
1206         u_int8_t        *ring,
1207                         *src,
1208                         xdu,
1209                         flag,
1210                         flag_byte,
1211                         tx_restart;
1212
1213         u_int16_t       saved_len,
1214                         dst_end,
1215                         dst_end1,
1216                         dst,
1217                         filled,
1218                         blevel,
1219                         tmp2,
1220                         len,
1221                         crc,
1222                         ib;
1223
1224         u_int32_t       tmp;
1225
1226
1227         saved_len = len = itjc_get_tx_mbuf(sc, chan, &src, ITJC_MB_CURR);
1228
1229         filled = ctx->filled;
1230         flag   = ctx->hdlc_flag;
1231
1232         if (src == NULL && flag == 2 && filled >= ITJC_RING_WORDS)
1233                 return;
1234
1235         tx_restart = (flag == 2 && src != NULL);
1236         xdu = itjc_xdu(sc, chan, ctx, &dst, &dst_end, tx_restart);
1237
1238         ring   = ctx->ring;
1239
1240         ib     = ctx->hdlc_ib;
1241         crc    = ctx->hdlc_crc;
1242         tmp    = ctx->hdlc_tmp;
1243         blevel = ctx->hdlc_blevel;
1244
1245         if (xdu)
1246         {
1247                 if (flag != 2)
1248                 {
1249                         NDBGL1(L1_H_XFRERR, "XDU");
1250                         ++chan->stat_XDU;
1251
1252                         /*
1253                          * Abort the current frame and 
1254                          * prepare for a full restart.
1255                          */
1256                         itjc_free_tx_mbufs(chan);
1257                         saved_len = len = filled = 0;
1258                         flag = (u_int8_t)-2;
1259                 }
1260                 else if (filled < ITJC_RING_SLOT_WORDS)
1261                 {
1262                         /*
1263                          * A little garbage may have been retransmitted.
1264                          * Send an abort before any new data.
1265                          */
1266                         filled = 0;
1267                         flag = (u_int8_t)-2;
1268                 }
1269         }
1270
1271         if (flag != 3)
1272                 len = 0;
1273
1274         while (dst != dst_end)
1275         {
1276                 HDLC_ENCODE(
1277                 *src++, len, tmp, tmp2, blevel, ib, crc, flag,
1278                 {/* gfr */
1279                         if ((len = saved_len) == 0)
1280                                 len = itjc_get_tx_mbuf(sc, chan, &src,
1281                                         ITJC_MB_NEW);
1282
1283                         if (len == 0)
1284                         {
1285                                 ctx->next_frame = dst;
1286
1287                                 flag_byte = itjc_rotate_hdlc_flag(blevel);
1288
1289                                 for (dst_end1 = itjc_ring_sub(dst_end, 1);
1290                                 dst != dst_end1;
1291                                 dst = itjc_ring_add(dst, 1))
1292                                 {
1293                                         ring[dst] = flag_byte;
1294                                         ++filled;
1295                                 }
1296                         }
1297                         else
1298                                 filled = 0;
1299
1300                         ctx->state = ITJC_TS_ACTIVE;
1301                 },
1302                 {/* nmb */
1303                         saved_len = 0;
1304                         len = itjc_get_tx_mbuf(sc, chan, &src, ITJC_MB_NEXT);
1305                 },
1306                 {/* wrd */
1307                         ring[dst] = (u_int8_t)tmp;
1308                         dst = itjc_ring_add(dst, 1);
1309                 },
1310                 d1);
1311         }
1312
1313         ctx->hdlc_blevel = blevel;
1314         ctx->hdlc_flag   = flag;
1315         ctx->hdlc_tmp    = tmp;
1316         ctx->hdlc_crc    = crc;
1317         ctx->hdlc_ib     = ib;
1318
1319         ctx->filled = filled;
1320         ctx->next_write = dst;
1321
1322         itjc_save_tx_mbuf(chan, src, len);
1323 }
1324
1325
1326 static void
1327 itjc_dma_tx_intr(struct l1_softc *sc, l1_bchan_state_t *chan,
1328 dma_tx_context_t * ctx)
1329 {
1330         u_int8_t        *data_end,
1331                         *ring,
1332                         *src,
1333                         xdu;
1334
1335         u_int16_t       dst,
1336                         dst_end,
1337                         filled,
1338                         len;
1339
1340
1341         if (ctx->state == ITJC_TS_IDLE)
1342                 goto done;
1343
1344         if (chan->bprot != BPROT_NONE)
1345         {
1346                 itjc_hdlc_encode(sc, chan, ctx);
1347                 goto done;
1348         }
1349
1350         ring   = ctx->ring;
1351         filled = ctx->filled;
1352
1353         len = itjc_get_tx_mbuf(sc, chan, &src, ITJC_MB_CURR);
1354
1355         if (len == 0 && filled >= ITJC_RING_WORDS)
1356                 goto done;
1357
1358         xdu = itjc_xdu(sc, chan, ctx, &dst, &dst_end, len != 0);
1359
1360         if (xdu && filled < ITJC_RING_WORDS)
1361         {
1362                 NDBGL1(L1_H_XFRERR, "XDU");
1363                 ++chan->stat_XDU;
1364                 filled = 0;
1365         }
1366
1367         if (len == 0)
1368                 goto fill_ring;
1369
1370         ctx->state = ITJC_TS_ACTIVE;
1371
1372         data_end = src + len;
1373         while (dst != dst_end)
1374         {
1375                 ring[dst] = *src++; --len;
1376
1377                 dst = itjc_ring_add(dst, 1);
1378
1379                 if (src >= data_end)
1380                 {
1381                         len = itjc_get_tx_mbuf(sc, chan, &src, ITJC_MB_NEXT);
1382                         if (len == 0)
1383                                 len = itjc_get_tx_mbuf(sc, chan,
1384                                          &src, ITJC_MB_NEW);
1385
1386                         if (len == 0)
1387                         {
1388                                 data_end = NULL;
1389                                 break;
1390                         }
1391                         data_end = src + len;
1392                 }
1393         }
1394
1395         itjc_save_tx_mbuf(chan, src, len);
1396
1397         filled = 0;
1398
1399 fill_ring:
1400         ctx->next_frame = dst;
1401
1402         for (; dst != dst_end; dst = itjc_ring_add(dst, 1))
1403         {
1404                 ring[dst] = ITJC_TEL_SILENCE_BYTE;
1405                 ++filled;
1406         }
1407
1408         ctx->next_write = dst;
1409         ctx->filled = filled;
1410
1411 done:
1412 }
1413
1414
1415 /*---------------------------------------------------------------------------*
1416  *      NetJet fifo read/write routines.
1417  *---------------------------------------------------------------------------*/
1418
1419 static void
1420 itjc_read_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
1421 {
1422         itjc_bus_setup(sc);
1423
1424         if (what != ISIC_WHAT_ISAC)
1425                 panic("itjc_write_fifo: Trying to read from HSCX fifo.\n");
1426
1427         itjc_set_pib_addr_msb(0);
1428         itjc_read_multi_1(PIB_OFFSET, buf, size);
1429 }
1430
1431
1432 static void
1433 itjc_write_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
1434 {
1435         itjc_bus_setup(sc);
1436
1437         if (what != ISIC_WHAT_ISAC)
1438                 panic("itjc_write_fifo: Trying to write to HSCX fifo.\n");
1439
1440         itjc_set_pib_addr_msb(0);
1441         itjc_write_multi_1(PIB_OFFSET, buf, size);
1442 }
1443
1444
1445 /*---------------------------------------------------------------------------*
1446  *      Read an ISAC register.
1447  *---------------------------------------------------------------------------*/
1448 static u_int8_t
1449 itjc_read_reg(struct l1_softc *sc, int what, bus_size_t offs)
1450 {
1451         itjc_bus_setup(sc);
1452
1453         if (what != ISIC_WHAT_ISAC)
1454         {
1455                 panic("itjc_read_reg: what(%d) != ISIC_WHAT_ISAC\n",
1456                         what);
1457                 return 0;
1458         }
1459
1460         itjc_set_pib_addr_msb(offs);
1461         return itjc_read_1(itjc_pib_2_pci(offs));
1462 }
1463
1464
1465 /*---------------------------------------------------------------------------*
1466  *      Write an ISAC register.
1467  *---------------------------------------------------------------------------*/
1468 static void
1469 itjc_write_reg(struct l1_softc *sc, int what, bus_size_t offs, u_int8_t data)
1470 {
1471         itjc_bus_setup(sc);
1472
1473         if (what != ISIC_WHAT_ISAC)
1474         {
1475                 panic("itjc_write_reg: what(%d) != ISIC_WHAT_ISAC\n",
1476                         what);
1477                 return;
1478         }
1479
1480         itjc_set_pib_addr_msb(offs);
1481         itjc_write_1(itjc_pib_2_pci(offs), data);
1482 }
1483
1484
1485 /*---------------------------------------------------------------------------*
1486  *      itjc_probe - probe for a card.
1487  *---------------------------------------------------------------------------*/
1488 static int itjc_probe(device_t dev)
1489 {
1490         u_int16_t       vid = pci_get_vendor(dev),
1491                         did = pci_get_device(dev);
1492
1493         if ((vid == PCI_TJNET_VID) && (did == PCI_TJ300_DID))
1494         {
1495                 device_set_desc(dev, "NetJet-S");
1496                 return 0;
1497         }
1498
1499         return ENXIO;
1500 }
1501
1502
1503 /*---------------------------------------------------------------------------*
1504  *      itjc_attach - attach a (previously probed) card.
1505  *---------------------------------------------------------------------------*/
1506 int
1507 itjc_attach(device_t dev)
1508 {
1509         bus_space_handle_t      h;
1510         bus_space_tag_t         t; 
1511
1512         struct l1_softc         *sc = device_get_softc(dev);
1513
1514         u_int16_t               vid = pci_get_vendor(dev),
1515                                 did = pci_get_device(dev);
1516
1517         int                     unit = device_get_unit(dev),
1518                                 s = splimp(),
1519                                 res_init_level = 0,
1520                                 error = 0;
1521
1522         void                    *ih = 0;
1523
1524         dma_context_t           *ctx = &dma_context[unit];
1525
1526         bzero(sc, sizeof(struct l1_softc));
1527
1528         /* Probably not really required. */
1529         if (unit > ITJC_MAXUNIT)
1530         {
1531                 printf("itjc%d: Error, unit > ITJC_MAXUNIT!\n", unit);
1532                 splx(s);
1533                 return ENXIO;
1534         }
1535
1536         if (!(vid == PCI_TJNET_VID && did == PCI_TJ300_DID))
1537         {
1538                 printf("itjc%d: unknown device (%04X,%04X)!\n", unit, vid, did);
1539                 goto fail;
1540         }
1541
1542         itjc_scp[unit] = sc;
1543
1544         sc->sc_resources.io_rid[0] = PCIR_MAPS+0;
1545         sc->sc_resources.io_base[0] = bus_alloc_resource(dev, SYS_RES_IOPORT,
1546                 &sc->sc_resources.io_rid[0], 0, ~0, 1, RF_ACTIVE);
1547
1548         if (sc->sc_resources.io_base[0] == NULL)
1549         {
1550                 printf("itjc%d: couldn't map IO port\n", unit);
1551                 error = ENXIO;
1552                 goto fail;
1553         }
1554
1555         h = rman_get_bushandle(sc->sc_resources.io_base[0]);
1556         t = rman_get_bustag(sc->sc_resources.io_base[0]); 
1557
1558         ++res_init_level;
1559
1560         /* Allocate interrupt. */
1561         sc->sc_resources.irq_rid = 0;
1562         sc->sc_resources.irq = bus_alloc_resource(dev, SYS_RES_IRQ,
1563                 &sc->sc_resources.irq_rid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
1564
1565         if (sc->sc_resources.irq == NULL)
1566         {
1567                 printf("itjc%d: couldn't map interrupt\n", unit);
1568                 error = ENXIO;
1569                 goto fail;
1570         }
1571
1572         ++res_init_level;
1573
1574         error = bus_setup_intr(dev, sc->sc_resources.irq, INTR_TYPE_NET,
1575                          itjc_intr, sc, &ih);
1576
1577         if (error)
1578         {
1579                 printf("itjc%d: couldn't set up irq handler\n", unit);
1580                 error = ENXIO;
1581                 goto fail;
1582         }
1583
1584         /*
1585          * Reset the ASIC & the ISAC.
1586          */
1587         itjc_write_1(TIGER_RESET_PIB_CL_TIME, TIGER_RESET_ALL);
1588
1589         DELAY(SEC_DELAY/100); /* Give it 10 ms to reset ...*/
1590
1591         itjc_write_1(TIGER_RESET_PIB_CL_TIME,
1592                 TIGER_SELF_ADDR_DMA | TIGER_PIB_3_CYCLES);
1593
1594         DELAY(SEC_DELAY/100); /* ... and more 10 to recover. */
1595
1596         /*
1597          * First part of DMA initialization. Create & map the memory
1598          * pool that will be used to bear the rx & tx ring buffers.
1599          */
1600         ctx->state = ITJC_DS_LOADING;
1601
1602         error = bus_dma_tag_create(
1603                 NULL,                                   /* parent */
1604                 4,                                      /* alignment*/
1605                 0,                                      /* boundary*/
1606                 BUS_SPACE_MAXADDR_32BIT,                /* lowaddr*/    
1607                 BUS_SPACE_MAXADDR,                      /* highaddr*/
1608                 NULL,                                   /* filter*/
1609                 NULL,                                   /* filterarg*/
1610                 ITJC_DMA_POOL_BYTES,                    /* maxsize*/
1611                 1,                                      /* nsegments*/
1612                 ITJC_DMA_POOL_BYTES,                    /* maxsegsz*/
1613                 BUS_DMA_ALLOCNOW | BUS_DMA_COHERENT,    /* flags*/
1614                 &ctx->tag);
1615
1616         if (error)
1617         {
1618                 printf("itjc%d: couldn't create bus DMA tag.\n", unit);
1619                 goto fail;
1620         }
1621
1622         ++res_init_level;
1623
1624         error = bus_dmamem_alloc(
1625                 ctx->tag,                               /* DMA tag */
1626                 (void **)&ctx->pool,    /* KV addr of the allocated memory */
1627                 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,      /* flags */
1628                 &ctx->map);                             /* KV <-> PCI map */
1629
1630         if (error)
1631                 goto fail;
1632
1633         /*
1634          * Load the KV <-> PCI map so the device sees the same
1635          * memory segment as pointed by pool. Note: since the
1636          * load may happen assyncronously (completion indicated by
1637          * the execution of the callback function) we have to
1638          * delay the initialization of the DMA engine to a moment we
1639          * actually have the proper bus addresses to feed the Tiger
1640          * and our DMA control blocks. This will be done in
1641          * itjc_bchannel_setup via a call to itjc_dma_start.
1642          */
1643         bus_dmamap_load(
1644                 ctx->tag,               /* DMA tag */
1645                 ctx->map,               /* DMA map */
1646                 ctx->pool,              /* KV addr of buffer */
1647                 ITJC_DMA_POOL_BYTES,    /* buffer size */
1648                 itjc_map_callback,      /* this receive the bus addr/error */
1649                 ctx,                    /* callback aux arg */
1650                 0);                     /* flags */
1651
1652         ++res_init_level;
1653
1654         /*
1655          * Setup the AUX port so we can talk to the ISAC.
1656          */
1657         itjc_write_1(TIGER_AUX_PORT_CNTL, TIGER_AUX_NJ_DEFAULT);
1658         itjc_write_1(TIGER_INT1_MASK, TIGER_ISAC_INT);
1659
1660         /*
1661          * From now on, almost like a `normal' ISIC driver.
1662          */
1663
1664         sc->sc_unit = unit;
1665
1666         ISAC_BASE = (caddr_t)ISIC_WHAT_ISAC;
1667
1668         HSCX_A_BASE = (caddr_t)ISIC_WHAT_HSCXA;
1669         HSCX_B_BASE = (caddr_t)ISIC_WHAT_HSCXB;
1670
1671         /* setup access routines */
1672
1673         sc->clearirq = NULL;
1674         sc->readreg = itjc_read_reg;
1675         sc->writereg = itjc_write_reg;
1676
1677         sc->readfifo = itjc_read_fifo;
1678         sc->writefifo = itjc_write_fifo;
1679
1680         /* setup card type */
1681         
1682         sc->sc_cardtyp = CARD_TYPEP_NETJET_S;
1683
1684         /* setup IOM bus type */
1685         
1686         sc->sc_bustyp = BUS_TYPE_IOM2;
1687
1688         /* set up some other miscellaneous things */
1689         sc->sc_ipac = 0;
1690         sc->sc_bfifolen = 2 * ITJC_RING_SLOT_WORDS;
1691
1692         printf("itjc%d: ISAC 2186 Version 1.1 (IOM-2)\n", unit);
1693
1694         /* init the ISAC */
1695         itjc_isac_init(sc);
1696
1697         /* init the "HSCX" */
1698         itjc_bchannel_setup(sc->sc_unit, HSCX_CH_A, BPROT_NONE, 0);
1699         
1700         itjc_bchannel_setup(sc->sc_unit, HSCX_CH_B, BPROT_NONE, 0);
1701
1702         /* can't use the normal B-Channel stuff */
1703         itjc_init_linktab(sc);
1704
1705         /* set trace level */
1706
1707         sc->sc_trace = TRACE_OFF;
1708
1709         sc->sc_state = ISAC_IDLE;
1710
1711         sc->sc_ibuf = NULL;
1712         sc->sc_ib = NULL;
1713         sc->sc_ilen = 0;
1714
1715         sc->sc_obuf = NULL;
1716         sc->sc_op = NULL;
1717         sc->sc_ol = 0;
1718         sc->sc_freeflag = 0;
1719
1720         sc->sc_obuf2 = NULL;
1721         sc->sc_freeflag2 = 0;
1722
1723 #if defined(__DragonFly__) || (defined(__FreeBSD__) && __FreeBSD__ >=3)
1724         callout_handle_init(&sc->sc_T3_callout);
1725         callout_handle_init(&sc->sc_T4_callout);        
1726 #endif
1727         
1728         /* init higher protocol layers */
1729         
1730         i4b_l1_mph_status_ind(L0ITJCUNIT(sc->sc_unit), STI_ATTACH, 
1731                 sc->sc_cardtyp, &itjc_l1mux_func);
1732
1733         splx(s);
1734         return 0;
1735
1736   fail:
1737         switch (res_init_level)
1738         {
1739         case 5:
1740                 bus_dmamap_unload(ctx->tag, ctx->map);
1741                 /* FALL TRHU */
1742
1743         case 4:
1744                 bus_dmamem_free(ctx->tag, ctx->pool, ctx->map);
1745                 bus_dmamap_destroy(ctx->tag, ctx->map);
1746                 /* FALL TRHU */
1747
1748         case 3:
1749                 bus_dma_tag_destroy(ctx->tag);
1750                 /* FALL TRHU */
1751
1752         case 2:
1753                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_resources.irq);
1754                 /* FALL TRHU */
1755
1756         case 1:
1757                 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_MAPS+0,
1758                         sc->sc_resources.io_base[0]);
1759                 /* FALL TRHU */
1760
1761         case 0:
1762         }
1763
1764         itjc_scp[unit] = NULL;
1765
1766         splx(s);
1767         return error;
1768 }
1769
1770
1771 /*---------------------------------------------------------------------------*
1772  *      itjc_intr - main interrupt service routine.
1773  *---------------------------------------------------------------------------*/
1774 static void
1775 itjc_intr(void *xsc)
1776 {
1777         struct l1_softc         *sc     = xsc;
1778         l1_bchan_state_t        *chan   = &sc->sc_chan[0];
1779         dma_context_t           *dma    = &dma_context[sc->sc_unit];
1780         dma_rx_context_t        *rxc    = &dma_rx_context[sc->sc_unit][0];
1781         dma_tx_context_t        *txc    = &dma_tx_context[sc->sc_unit][0];
1782
1783         itjc_bus_setup(sc);
1784
1785         /* Honor interrupts from successfully configured cards only. */
1786         if (dma->state < ITJC_DS_STOPPED)
1787                 return;
1788
1789         /* First, we check the ISAC... */
1790         if (! (itjc_read_1(TIGER_AUX_PORT_DATA) & TIGER_ISAC_INT_MASK))
1791         {
1792                 itjc_write_1(TIGER_INT1_STATUS, TIGER_ISAC_INT);
1793                 NDBGL1(L1_H_IRQ, "ISAC");
1794                 itjc_isac_intr(sc);
1795         }
1796
1797         /* ... after what we always have a look at the DMA rings. */
1798
1799         NDBGL1(L1_H_IRQ, "Tiger");
1800
1801         itjc_read_1(TIGER_INT0_STATUS);
1802         itjc_write_1(TIGER_INT0_STATUS, TIGER_TARGET_ABORT_INT
1803                 | TIGER_MASTER_ABORT_INT | TIGER_RD_END_INT
1804                 | TIGER_RD_INT_INT       | TIGER_WR_END_INT | TIGER_WR_INT_INT);
1805
1806         itjc_dma_rx_intr(sc, chan, rxc);
1807         itjc_dma_tx_intr(sc, chan, txc);
1808
1809         ++chan; ++rxc; ++txc;
1810
1811         itjc_dma_rx_intr(sc, chan, rxc);
1812         itjc_dma_tx_intr(sc, chan, txc);
1813 }
1814
1815
1816 /*---------------------------------------------------------------------------*
1817  *      itjc_bchannel_setup - (Re)initialize and start/stop a Bchannel.
1818  *---------------------------------------------------------------------------*/
1819 static void
1820 itjc_bchannel_setup(int unit, int h_chan, int bprot, int activate)
1821 {
1822 #if defined(__DragonFly__) || defined(__FreeBSD__)
1823         struct l1_softc         *sc     = itjc_scp[unit];
1824 #else
1825         struct l1_softc         *sc     = isic_find_sc(unit);
1826 #endif
1827         l1_bchan_state_t        *chan   = &sc->sc_chan[h_chan];
1828         int                     s       = SPLI4B();
1829                 
1830         NDBGL1(L1_BCHAN, "unit=%d, channel=%d, %s",
1831                 unit, h_chan, activate ? "activate" : "deactivate");
1832
1833         /*
1834          * If we are deactivating the channel, we have to stop
1835          * the DMA before we reset the channel control structures.
1836          */
1837         if (! activate)
1838                 itjc_bchannel_dma_setup(sc, h_chan, activate); 
1839
1840         /* general part */
1841
1842         chan->state = HSCX_IDLE;
1843
1844         chan->unit = sc->sc_unit;       /* unit number */
1845         chan->channel = h_chan;         /* B channel */
1846         chan->bprot = bprot;            /* B channel protocol */
1847
1848         /* receiver part */
1849
1850         i4b_Bcleanifq(&chan->rx_queue); /* clean rx queue */
1851
1852         chan->rx_queue.ifq_maxlen = IFQ_MAXLEN;
1853
1854         chan->rxcount = 0;              /* reset rx counter */
1855         
1856         i4b_Bfreembuf(chan->in_mbuf);   /* clean rx mbuf */
1857
1858         chan->in_mbuf = NULL;           /* reset mbuf ptr */
1859         chan->in_cbptr = NULL;          /* reset mbuf curr ptr */
1860         chan->in_len = 0;               /* reset mbuf data len */
1861         
1862         /* transmitter part */
1863
1864         i4b_Bcleanifq(&chan->tx_queue); /* clean tx queue */
1865
1866         chan->tx_queue.ifq_maxlen = IFQ_MAXLEN;
1867         
1868         chan->txcount = 0;              /* reset tx counter */
1869         
1870         i4b_Bfreembuf(chan->out_mbuf_head);     /* clean tx mbuf */
1871
1872         chan->out_mbuf_head = NULL;     /* reset head mbuf ptr */
1873         chan->out_mbuf_cur = NULL;      /* reset current mbuf ptr */    
1874         chan->out_mbuf_cur_ptr = NULL;  /* reset current mbuf data ptr */
1875         chan->out_mbuf_cur_len = 0;     /* reset current mbuf data cnt */
1876
1877         /*
1878          * Only setup & start the DMA after all other channel
1879          * control structures are in place.
1880          */
1881         if (activate)
1882                 itjc_bchannel_dma_setup(sc, h_chan, activate); 
1883
1884         splx(s);
1885 }
1886
1887
1888 /*---------------------------------------------------------------------------*
1889  *      itjc_bchannel_start - Signal us we have more data to send.
1890  *---------------------------------------------------------------------------*/
1891 static void
1892 itjc_bchannel_start(int unit, int h_chan)
1893 {
1894 #if Buggy_code
1895         /*
1896          * I disabled this routine because it was causing crashes when
1897          * this driver was used with the ISP (kernel SPPP) protocol driver.
1898          * The scenario is reproductible:
1899          *      Use the -link1 (dial on demand) ifconfig option.
1900          *      Start an interactive  TCP connection to somewhere.
1901          *      Wait until the PPP connection times out and is dropped.
1902          *      Try to send something on the TCP connection.
1903          *      The machine will print some garbage and halt or reboot
1904          *      (no panic messages).
1905          *
1906          * I've nailed down the problem to the fact that this routine
1907          * was being called before the B channel had been setup again.
1908          *
1909          * For now, I don't have a good solution other than this one.
1910          * But, don't despair. The impact of it is unnoticeable.
1911          */
1912
1913 #if defined(__DragonFly__) || defined(__FreeBSD__)
1914         struct l1_softc  *sc    = itjc_scp[unit];
1915 #else
1916         struct l1_softc  *sc    = isic_find_sc(unit);
1917 #endif
1918         l1_bchan_state_t *chan  = &sc->sc_chan[h_chan];
1919
1920         int              s      = SPLI4B();
1921
1922         dma_tx_context_t *txc   = &dma_tx_context[unit][h_chan];
1923
1924         if (chan->state & HSCX_TX_ACTIVE)
1925         {
1926                 splx(s);
1927                 return;
1928         }
1929
1930         itjc_dma_tx_intr(sc, chan, txc);
1931
1932         splx(s);
1933 #endif
1934 }
1935
1936
1937 /*---------------------------------------------------------------------------*
1938  *      itjc_shutdown - Stop the driver and reset the card.
1939  *---------------------------------------------------------------------------*/
1940 static void
1941 itjc_shutdown(device_t dev)
1942 {
1943         struct l1_softc *sc = device_get_softc(dev);
1944
1945         itjc_bus_setup(sc);
1946
1947         /*
1948          * Stop the DMA the nice and easy way.
1949          */
1950         itjc_bchannel_setup(sc->sc_unit, 0, BPROT_NONE, 0);
1951         itjc_bchannel_setup(sc->sc_unit, 1, BPROT_NONE, 0);
1952
1953         /*
1954          * Reset the card.
1955          */
1956         itjc_write_1(TIGER_RESET_PIB_CL_TIME, TIGER_RESET_ALL);
1957
1958         DELAY(SEC_DELAY/100); /* Give it 10 ms to reset ...*/
1959
1960         itjc_write_1(TIGER_RESET_PIB_CL_TIME,
1961                 TIGER_SELF_ADDR_DMA | TIGER_LATCH_DMA_INT | TIGER_PIB_3_CYCLES);
1962
1963         DELAY(SEC_DELAY/100); /* ... and more 10 to recover */
1964 }
1965
1966
1967 /*---------------------------------------------------------------------------*
1968  *      itjc_ret_linktab - Return the address of itjc drivers linktab.
1969  *---------------------------------------------------------------------------*/
1970 isdn_link_t *
1971 itjc_ret_linktab(int unit, int channel)
1972 {
1973 #if defined(__DragonFly__) || defined(__FreeBSD__)
1974         struct l1_softc         *sc = itjc_scp[unit];
1975 #else
1976         struct l1_softc         *sc = isic_find_sc(unit);
1977 #endif
1978         l1_bchan_state_t        *chan = &sc->sc_chan[channel];
1979
1980         return(&chan->isic_isdn_linktab);
1981 }
1982  
1983 /*---------------------------------------------------------------------------*
1984  *      itjc_set_linktab - Set the driver linktab in the b channel softc.
1985  *---------------------------------------------------------------------------*/
1986 void
1987 itjc_set_linktab(int unit, int channel, drvr_link_t *dlt)
1988 {
1989 #if defined(__DragonFly__) || defined(__FreeBSD__)
1990         struct l1_softc *sc     = itjc_scp[unit];
1991 #else
1992         struct l1_softc *sc     = isic_find_sc(unit);
1993 #endif
1994         l1_bchan_state_t *chan  = &sc->sc_chan[channel];
1995
1996         chan->isic_drvr_linktab = dlt;
1997 }
1998
1999
2000 /*---------------------------------------------------------------------------*
2001  *      itjc_init_linktab - Initialize our local linktab.
2002  *---------------------------------------------------------------------------*/
2003 static void
2004 itjc_init_linktab(struct l1_softc *sc)
2005 {
2006         l1_bchan_state_t *chan = &sc->sc_chan[HSCX_CH_A];
2007         isdn_link_t *lt = &chan->isic_isdn_linktab;
2008
2009         /* make sure the hardware driver is known to layer 4 */
2010         /* avoid overwriting if already set */
2011         if (ctrl_types[CTRL_PASSIVE].set_linktab == NULL)
2012         {
2013                 ctrl_types[CTRL_PASSIVE].set_linktab = itjc_set_linktab;
2014                 ctrl_types[CTRL_PASSIVE].get_linktab = itjc_ret_linktab;
2015         }
2016
2017         /* local setup */
2018         lt->unit = sc->sc_unit;
2019         lt->channel = HSCX_CH_A;
2020         lt->bch_config = itjc_bchannel_setup;
2021         lt->bch_tx_start = itjc_bchannel_start;
2022         lt->bch_stat = itjc_bchannel_stat;
2023         lt->tx_queue = &chan->tx_queue;
2024
2025         /* used by non-HDLC data transfers, i.e. telephony drivers */
2026         lt->rx_queue = &chan->rx_queue;
2027
2028         /* used by HDLC data transfers, i.e. ipr and isp drivers */     
2029         lt->rx_mbuf = &chan->in_mbuf;   
2030                                                 
2031         chan = &sc->sc_chan[HSCX_CH_B];
2032         lt = &chan->isic_isdn_linktab;
2033
2034         lt->unit = sc->sc_unit;
2035         lt->channel = HSCX_CH_B;
2036         lt->bch_config = itjc_bchannel_setup;
2037         lt->bch_tx_start = itjc_bchannel_start;
2038         lt->bch_stat = itjc_bchannel_stat;
2039         lt->tx_queue = &chan->tx_queue;
2040
2041         /* used by non-HDLC data transfers, i.e. telephony drivers */
2042         lt->rx_queue = &chan->rx_queue;
2043
2044         /* used by HDLC data transfers, i.e. ipr and isp drivers */     
2045         lt->rx_mbuf = &chan->in_mbuf;   
2046 }
2047
2048
2049 /*---------------------------------------------------------------------------*
2050  *      itjc_bchannel_stat - Collect link statistics for a given B channel.
2051  *---------------------------------------------------------------------------*/
2052 static void
2053 itjc_bchannel_stat(int unit, int h_chan, bchan_statistics_t *bsp)
2054 {
2055 #if defined(__DragonFly__) || defined(__FreeBSD__)
2056         struct l1_softc *sc = itjc_scp[unit];
2057 #else
2058         struct l1_softc *sc = isic_find_sc(unit);
2059 #endif
2060         l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
2061         int s;
2062
2063         s = SPLI4B();
2064         
2065         bsp->outbytes = chan->txcount;
2066         bsp->inbytes = chan->rxcount;
2067
2068         chan->txcount = 0;
2069         chan->rxcount = 0;
2070
2071         splx(s);
2072 }
2073
2074
2075 /*---------------------------------------------------------------------------*
2076  *      Netjet - ISAC interrupt routine.
2077  *---------------------------------------------------------------------------*/
2078 static void
2079 itjc_isac_intr(struct l1_softc *sc)
2080 {
2081         register u_char irq_stat;
2082
2083         do
2084         {
2085                 /* get isac irq status */
2086                 irq_stat = ISAC_READ(I_ISTA);
2087
2088                 if(irq_stat)
2089                         itjc_isac_irq(sc, irq_stat); /* isac handler */
2090         }
2091         while(irq_stat);
2092
2093         ISAC_WRITE(I_MASK, 0xff);
2094
2095         DELAY(100);
2096
2097         ISAC_WRITE(I_MASK, ISAC_IMASK);
2098 }
2099
2100
2101 /*---------------------------------------------------------------------------*
2102  *      itjc_recover - Try to recover from ISAC irq lockup.
2103  *---------------------------------------------------------------------------*/
2104 void
2105 itjc_recover(struct l1_softc *sc)
2106 {
2107         u_char byte;
2108         
2109         /* get isac irq status */
2110
2111         byte = ISAC_READ(I_ISTA);
2112
2113         NDBGL1(L1_ERROR, "  ISAC: ISTA = 0x%x", byte);
2114         
2115         if(byte & ISAC_ISTA_EXI)
2116                 NDBGL1(L1_ERROR, "  ISAC: EXIR = 0x%x", (u_char)ISAC_READ(I_EXIR));
2117
2118         if(byte & ISAC_ISTA_CISQ)
2119         {
2120                 byte = ISAC_READ(I_CIRR);
2121         
2122                 NDBGL1(L1_ERROR, "  ISAC: CISQ = 0x%x", byte);
2123                 
2124                 if(byte & ISAC_CIRR_SQC)
2125                         NDBGL1(L1_ERROR, "  ISAC: SQRR = 0x%x", (u_char)ISAC_READ(I_SQRR));
2126         }
2127
2128         NDBGL1(L1_ERROR, "  ISAC: IMASK = 0x%x", ISAC_IMASK);
2129
2130         ISAC_WRITE(I_MASK, 0xff);       
2131         DELAY(100);
2132         ISAC_WRITE(I_MASK, ISAC_IMASK);
2133 }
2134
2135 #endif /* NITJC > 0 */