2 * Copyright (c) 2000, 2001 Sergio Prallon. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
31 *---------------------------------------------------------------------------
33 * i4b_itjc_pci.c: NetJet-S hardware driver
34 * ----------------------------------------
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.11 2005/06/03 16:50:07 dillon Exp $
39 * last edit-date: [Thu Jan 11 11:29:38 2001]
41 *---------------------------------------------------------------------------*/
49 #include <sys/param.h>
50 #include <sys/kernel.h>
51 #include <sys/systm.h>
54 #include <machine/clock.h>
55 #include <machine/bus_pio.h>
56 #include <machine/bus.h>
57 #include <machine/resource.h>
61 #include <bus/pci/pcireg.h>
62 #include <bus/pci/pcivar.h>
64 #include <sys/socket.h>
65 #include <sys/thread2.h>
68 #include <net/i4b/include/machine/i4b_debug.h>
69 #include <net/i4b/include/machine/i4b_ioctl.h>
70 #include <net/i4b/include/machine/i4b_trace.h>
72 #include "../../include/i4b_global.h"
73 #include "../../include/i4b_mbuf.h"
75 #include "../i4b_l1.h"
77 #include "i4b_hdlc.h" /* XXXXXXXXXXXXXXXXXXXXXXXX */
79 #include "../isic/i4b_isic.h"
80 #include "../isic/i4b_isac.h"
82 #include "i4b_itjc_ext.h"
84 #define PCI_TJNET_VID (0xe159)
85 #define PCI_TJ300_DID (0x0001)
92 static int itjc_probe(device_t dev);
93 static int itjc_attach(device_t dev);
94 static void itjc_shutdown(device_t dev);
95 static void itjc_intr(void *xsc);
96 static int itjc_dma_start(struct l1_softc *sc);
97 static void itjc_dma_stop(struct l1_softc *sc);
98 static void itjc_isac_intr(struct l1_softc *sc);
99 static void itjc_init_linktab(struct l1_softc *sc);
100 static void itjc_bchannel_setup(int unit, int h_chan, int bprot,
102 static void itjc_bchannel_stat(int unit, int h_chan, bchan_statistics_t *bsp);
106 * Shorter names to bus resource manager routines.
109 #define itjc_bus_setup(sc) \
110 bus_space_handle_t h = \
111 rman_get_bushandle((sc)->sc_resources.io_base[0]); \
112 bus_space_tag_t t = \
113 rman_get_bustag((sc)->sc_resources.io_base[0]);
115 #define itjc_read_1(port) (bus_space_read_1(t, h, (port)))
116 #define itjc_read_4(port) (bus_space_read_4(t, h, (port)))
117 #define itjc_write_1(port, data) (bus_space_write_1(t, h, (port), (data)))
118 #define itjc_write_4(port, data) (bus_space_write_4(t, h, (port), (data)))
119 #define itjc_read_multi_1(port, buf, size) \
120 (bus_space_read_multi_1(t, h, (port), (buf), (size)))
121 #define itjc_write_multi_1(port, buf, size) \
122 (bus_space_write_multi_1(t, h, (port), (buf), (size)))
125 /*---------------------------------------------------------------------------*
126 * Glue data to register ourselves as a PCI device driver.
127 *---------------------------------------------------------------------------*/
129 static device_method_t itjc_pci_methods[] =
131 /* Device interface */
132 DEVMETHOD(device_probe, itjc_probe),
133 DEVMETHOD(device_attach, itjc_attach),
134 DEVMETHOD(device_shutdown, itjc_shutdown),
137 DEVMETHOD(bus_print_child, bus_generic_print_child),
138 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
143 static driver_t itjc_pci_driver =
147 sizeof(struct l1_softc)
150 static devclass_t itjc_pci_devclass;
152 DRIVER_MODULE(netjet, pci, itjc_pci_driver, itjc_pci_devclass, 0, 0);
155 * Jump table for multiplex routines.
158 struct i4b_l1mux_func itjc_l1mux_func =
162 itjc_mph_command_req,
164 itjc_ph_activate_req,
167 struct l1_softc *itjc_scp[ITJC_MAXUNIT];
170 /*---------------------------------------------------------------------------*
171 * Tiger300/320 PCI ASIC registers.
172 *---------------------------------------------------------------------------*/
175 * Register offsets from i/o base.
179 TIGER_RESET_PIB_CL_TIME = 0x00,
180 TIGER_DMA_OPER = 0x01,
181 TIGER_AUX_PORT_CNTL = 0x02,
182 TIGER_AUX_PORT_DATA = 0x03,
183 TIGER_INT0_MASK = 0x04,
184 TIGER_INT1_MASK = 0x05,
185 TIGER_INT0_STATUS = 0x06,
186 TIGER_INT1_STATUS = 0x07,
187 TIGER_DMA_WR_START_ADDR = 0x08,
188 TIGER_DMA_WR_INT_ADDR = 0x0C,
189 TIGER_DMA_WR_END_ADDR = 0x10,
190 TIGER_DMA_WR_CURR_ADDR = 0x14,
191 TIGER_DMA_RD_START_ADDR = 0x18,
192 TIGER_DMA_RD_INT_ADDR = 0x1C,
193 TIGER_DMA_RD_END_ADDR = 0x20,
194 TIGER_DMA_RD_CURR_ADDR = 0x24,
195 TIGER_PULSE_COUNTER = 0x28,
199 * Bits on the above registers.
204 /* Reset and PIB Cycle Timing */
206 TIGER_DMA_OP_MODE_MASK = 0x80,
207 TIGER_SELF_ADDR_DMA = 0x00, /* Wrap around ending addr */
208 TIGER_NORMAL_DMA = 0x80, /* Stop at ending addr */
210 TIGER_DMA_INT_MODE_MASK = 0x40,
211 TIGER_DONT_LATCH_DMA_INT= 0x00, /* Bits on int0 status will be
212 set only while curr addr
213 equals int or end addr */
214 TIGER_LATCH_DMA_INT = 0x40, /* Bits on int0 status remain
215 set until cleared by CPU */
217 TIGER_PIB_CYCLE_TIMING_MASK = 0x30,
218 TIGER_PIB_3_CYCLES = 0x00,
219 TIGER_PIB_5_CYCLES = 0x01,
220 TIGER_PIB_12_CYCLES = 0x10,
222 TIGER_RESET_MASK = 0x0F,
223 TIGER_RESET_PULSE_COUNT = 0x08,
224 TIGER_RESET_SERIAL_PORT = 0x04,
225 TIGER_RESET_DMA_LOGIC = 0x02,
226 TIGER_RESET_EXTERNAL = 0x01,
227 TIGER_RESET_ALL = 0x0F,
230 TIGER_DMA_RESTART_MASK = 0x02,
231 TIGER_HOLD_DMA = 0x00,
232 TIGER_RESTART_DMA = 0x00,
234 TIGER_DMA_ENABLE_MASK = 0x01,
235 TIGER_ENABLE_DMA = 0x01,
236 TIGER_DISABLE_DMA = 0x00,
238 /* AUX Port Control & Data plus Interrupt 1 Mask & Status */
239 TIGER_AUX_7_MASK = 0x80,
240 TIGER_AUX_6_MASK = 0x40,
241 TIGER_AUX_5_MASK = 0x20,
242 TIGER_AUX_4_MASK = 0x10,
243 TIGER_ISAC_INT_MASK = 0x10,
244 TIGER_AUX_3_MASK = 0x08,
245 TIGER_AUX_2_MASK = 0x04,
246 TIGER_AUX_1_MASK = 0x02,
247 TIGER_AUX_0_MASK = 0x01,
249 /* AUX Port Control */
250 TIGER_AUX_7_IS_INPUT = 0x00,
251 TIGER_AUX_7_IS_OUTPUT = 0x80,
252 TIGER_AUX_6_IS_INPUT = 0x00,
253 TIGER_AUX_6_IS_OUTPUT = 0x40,
254 TIGER_AUX_5_IS_INPUT = 0x00,
255 TIGER_AUX_5_IS_OUTPUT = 0x20,
256 TIGER_AUX_4_IS_INPUT = 0x00,
257 TIGER_AUX_4_IS_OUTPUT = 0x10,
258 TIGER_AUX_3_IS_INPUT = 0x00,
259 TIGER_AUX_3_IS_OUTPUT = 0x80,
260 TIGER_AUX_2_IS_INPUT = 0x00,
261 TIGER_AUX_2_IS_OUTPUT = 0x40,
262 TIGER_AUX_1_IS_INPUT = 0x00,
263 TIGER_AUX_1_IS_OUTPUT = 0x20,
264 TIGER_AUX_0_IS_INPUT = 0x00,
265 TIGER_AUX_0_IS_OUTPUT = 0x10,
266 TIGER_AUX_NJ_DEFAULT = 0xEF, /* All but ISAC int is output */
268 /* Interrupt 0 Mask & Status */
269 TIGER_PCI_TARGET_ABORT_INT_MASK = 0x20,
270 TIGER_NO_TGT_ABORT_INT = 0x00,
271 TIGER_TARGET_ABORT_INT = 0x20,
272 TIGER_PCI_MASTER_ABORT_INT_MASK = 0x10,
273 TIGER_NO_MST_ABORT_INT = 0x00,
274 TIGER_MASTER_ABORT_INT = 0x10,
275 TIGER_DMA_RD_END_INT_MASK = 0x08,
276 TIGER_NO_RD_END_INT = 0x00,
277 TIGER_RD_END_INT = 0x08,
278 TIGER_DMA_RD_INT_INT_MASK = 0x04,
279 TIGER_NO_RD_INT_INT = 0x00,
280 TIGER_RD_INT_INT = 0x04,
281 TIGER_DMA_WR_END_INT_MASK = 0x02,
282 TIGER_NO_WR_END_INT = 0x00,
283 TIGER_WR_END_INT = 0x02,
284 TIGER_DMA_WR_INT_INT_MASK = 0x01,
285 TIGER_NO_WR_INT_INT = 0x00,
286 TIGER_WR_INT_INT = 0x01,
288 /* Interrupt 1 Mask & Status */
289 TIGER_NO_AUX_7_INT = 0x00,
290 TIGER_AUX_7_INT = 0x80,
291 TIGER_NO_AUX_6_INT = 0x00,
292 TIGER_AUX_6_INT = 0x40,
293 TIGER_NO_AUX_5_INT = 0x00,
294 TIGER_AUX_5_INT = 0x20,
295 TIGER_NO_AUX_4_INT = 0x00,
296 TIGER_AUX_4_INT = 0x10,
297 TIGER_NO_ISAC_INT = 0x00,
298 TIGER_ISAC_INT = 0x10,
299 TIGER_NO_AUX_3_INT = 0x00,
300 TIGER_AUX_3_INT = 0x08,
301 TIGER_NO_AUX_2_INT = 0x00,
302 TIGER_AUX_2_INT = 0x04,
303 TIGER_NO_AUX_1_INT = 0x00,
304 TIGER_AUX_1_INT = 0x02,
305 TIGER_NO_AUX_0_INT = 0x00,
306 TIGER_AUX_0_INT = 0x01
310 * Peripheral Interface Bus definitions. This is an ISA like bus
311 * created by the Tiger ASIC to keep ISA chips like the ISAC happy
312 * on a PCI environment.
314 * Since the PIB only supplies 4 addressing lines, the 2 higher bits
315 * (A4 & A5) of the ISAC register addresses are wired on the 2 lower
316 * AUX lines. Another restriction is that all I/O to the PIB (8bit
317 * wide) is mapped on the PCI side as 32bit data. So the PCI address
318 * of a given ISAC register has to be multiplied by 4 before being
319 * added to the PIB base offset.
321 enum tiger_pib_regs_defs
323 /* Offset from the I/O base to the ISAC registers. */
325 PIB_LO_ADDR_MASK = 0x0F,
326 PIB_HI_ADDR_MASK = 0x30,
327 PIB_LO_ADDR_SHIFT = 2, /* Align on dword boundary */
328 PIB_HI_ADDR_SHIFT = 4 /* Right shift to AUX_1 & AUX_0 */
332 #define itjc_set_pib_addr_msb(a) \
334 itjc_write_1(TIGER_AUX_PORT_DATA, \
335 ((a) & PIB_HI_ADDR_MASK) >> PIB_HI_ADDR_SHIFT) \
338 #define itjc_pib_2_pci(a) \
340 (((a) & PIB_LO_ADDR_MASK) << PIB_LO_ADDR_SHIFT) + PIB_OFFSET \
343 #define itjc_get_dma_offset(ctx,reg) \
345 (u_int16_t)((bus_addr_t)itjc_read_4((reg)) - (ctx)->bus_addr) \
350 * IOM-2 serial channel 0 DMA data ring buffers.
352 * The Tiger300/320 ASIC do not nothing more than transfer via DMA the
353 * first 32 bits of every IOM-2 frame on the serial interface to the
354 * ISAC. So we have no framing/deframing facilities like we would have
355 * with an HSCX, having to do the job with CPU cycles. On the plus side
356 * we are able to specify large rings which can limit the occurrence of
362 ITJC_RING_SLOT_WORDS = 64,
363 ITJC_RING_WORDS = 3 * ITJC_RING_SLOT_WORDS,
364 ITJC_RING_SLOT_BYTES = 4 * ITJC_RING_SLOT_WORDS,
365 ITJC_RING_BYTES = 4 * ITJC_RING_WORDS,
366 ITJC_DMA_POOL_WORDS = 2 * ITJC_RING_WORDS,
367 ITJC_DMA_POOL_BYTES = 4 * ITJC_DMA_POOL_WORDS
370 #define itjc_ring_add(x, d) (((x) + 4 * (d)) % ITJC_RING_BYTES)
371 #define itjc_ring_sub(x, d) (((x) + ITJC_RING_BYTES - 4 * (d)) \
380 HSCX_CH_A = 0, /* For compatibility reasons. */
386 ITJC_TEL_SILENCE_BYTE = 0x00,
387 ITJC_HDLC_FLAG_BYTE = 0x7E,
388 ITJC_HDLC_ABORT_BYTE = 0xFF
392 * Hardware DMA control block (one per card).
396 ITJC_DS_LOAD_FAILED = -1,
416 dma_context [ ITJC_MAXUNIT ];
419 * B-channel DMA control blocks (4 per card -- 1 RX & 1 TX per channel).
445 u_int8_t hdlc_blevel;
447 dma_rx_state_t state;
455 u_int16_t next_write;
457 u_int16_t hdlc_blevel;
460 u_int16_t next_frame;
463 dma_tx_state_t state;
468 dma_rx_context [ ITJC_MAXUNIT ] [ 2 ];
471 dma_tx_context [ ITJC_MAXUNIT ] [ 2 ];
474 * Used by the mbuf handling functions.
485 /*---------------------------------------------------------------------------*
486 * itjc_map_callback - get DMA bus address from resource mgr.
487 *---------------------------------------------------------------------------*/
489 itjc_map_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
491 dma_context_t *ctx = (dma_context_t *)arg;
496 ctx->state = ITJC_DS_LOAD_FAILED;
500 ctx->bus_addr = segs->ds_addr;
501 ctx->state = ITJC_DS_STOPPED;
505 /*---------------------------------------------------------------------------*
506 * itjc_dma_start - Complete DMA setup & start the Tiger DMA engine.
507 *---------------------------------------------------------------------------*/
509 itjc_dma_start(struct l1_softc *sc)
511 int unit = sc->sc_unit;
512 dma_context_t *ctx = &dma_context[unit];
513 dma_rx_context_t *rxc = &dma_rx_context[unit][0];
514 dma_tx_context_t *txc = &dma_tx_context[unit][0];
522 /* See if it is already running. */
524 if (ctx->state == ITJC_DS_RUNNING)
527 if (ctx->state == ITJC_DS_LOAD_FAILED)
529 NDBGL1(L1_ERROR, "itjc%d: dma_start: DMA map loading "
530 "failed (error=%d).\n", unit, ctx->error);
534 if (ctx->state != ITJC_DS_STOPPED)
536 NDBGL1(L1_ERROR, "itjc%d: dma_start: Unexpected DMA "
537 "state (%d).\n", unit, ctx->state);
542 * Initialize the DMA control structures (hardware & B-channel).
546 txc->ring = ctx->pool + TIGER_CH_A;
547 rxc->ring = ctx->pool + TIGER_CH_A + ITJC_RING_BYTES;
550 rxc->bus_addr = ba + ITJC_RING_BYTES;
554 txc->ring = ctx->pool + TIGER_CH_B;
555 rxc->ring = ctx->pool + TIGER_CH_B + ITJC_RING_BYTES;
558 rxc->bus_addr = ba + ITJC_RING_BYTES;
561 * Fill the DMA ring buffers with IOM-2 channel 0 frames made of
562 * idle/abort sequences for the B & D channels and NOP for IOM-2
563 * cmd/ind, monitor handshake & data.
565 pool_end = (u_int32_t *)ctx->pool + ITJC_DMA_POOL_WORDS;
566 for (ip = (u_int32_t *)ctx->pool; ip < pool_end; ++ip)
570 * Program the Tiger DMA gears.
573 itjc_write_4(TIGER_DMA_WR_START_ADDR, ba);
574 itjc_write_4(TIGER_DMA_WR_INT_ADDR, ba + ITJC_RING_SLOT_BYTES - 4);
575 itjc_write_4(TIGER_DMA_WR_END_ADDR, ba + ITJC_RING_BYTES - 4);
577 ba += ITJC_RING_BYTES;
579 itjc_write_4(TIGER_DMA_RD_START_ADDR, ba);
580 itjc_write_4(TIGER_DMA_RD_INT_ADDR, ba + ITJC_RING_SLOT_BYTES * 2 - 4);
581 itjc_write_4(TIGER_DMA_RD_END_ADDR, ba + ITJC_RING_BYTES - 4);
583 itjc_write_1(TIGER_INT0_MASK,
584 TIGER_WR_END_INT | TIGER_WR_INT_INT | TIGER_RD_INT_INT);
586 itjc_write_1(TIGER_DMA_OPER, TIGER_ENABLE_DMA);
589 * See if it really started.
591 ba = itjc_read_4(TIGER_DMA_RD_CURR_ADDR);
592 for (i = 0; i < 10; ++i)
594 DELAY(SEC_DELAY/1000);
595 if (ba != itjc_read_4(TIGER_DMA_RD_CURR_ADDR))
597 ctx->state = ITJC_DS_RUNNING;
602 NDBGL1(L1_ERROR, "itjc%d: dma_start: DMA start failed.\n ", unit);
607 /*---------------------------------------------------------------------------*
608 * itjc_dma_stop - Stop the Tiger DMA engine.
609 *---------------------------------------------------------------------------*/
611 itjc_dma_stop(struct l1_softc *sc)
613 dma_context_t *ctx = &dma_context[sc->sc_unit];
617 /* Only stop the DMA if it is running. */
619 if (ctx->state != ITJC_DS_RUNNING)
622 itjc_write_1(TIGER_DMA_OPER, TIGER_DISABLE_DMA);
623 DELAY(SEC_DELAY/1000);
625 ctx->state = ITJC_DS_STOPPED;
629 /*---------------------------------------------------------------------------*
630 * itjc_bchannel_dma_setup - The DMA side of itjc_bchannel_setup.
631 *---------------------------------------------------------------------------*/
633 itjc_bchannel_dma_setup(struct l1_softc *sc, int h_chan, int activate)
635 dma_rx_context_t *rxc = &dma_rx_context[sc->sc_unit][h_chan];
636 dma_tx_context_t *txc = &dma_tx_context[sc->sc_unit][h_chan];
638 l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
650 * Get the DMA engine going if it's not running already.
654 rxc->hdlc_len = rxc->hdlc_tmp = rxc->hdlc_crc = 0;
655 rxc->hdlc_ib = rxc->hdlc_blevel = rxc->hdlc_flag = 0;
657 txc->hdlc_tmp = txc->hdlc_blevel = txc->hdlc_crc = 0;
662 if (chan->bprot == BPROT_NONE)
663 fill_byte = ITJC_TEL_SILENCE_BYTE;
665 fill_byte = ITJC_HDLC_ABORT_BYTE;
667 ring_end = rxc->ring + ITJC_RING_BYTES;
668 for (cp = rxc->ring; cp < ring_end; cp += 4)
671 ring_end = txc->ring + ITJC_RING_BYTES;
672 for (cp = txc->ring; cp < ring_end; cp += 4)
676 itjc_get_dma_offset(rxc, TIGER_DMA_RD_CURR_ADDR);
678 txc->next_frame = txc->next_write =
679 itjc_get_dma_offset(txc, TIGER_DMA_WR_CURR_ADDR);
681 rxc->state = ITJC_RS_ACTIVE;
682 txc->state = ITJC_TS_AFTER_XDU;
686 dma_rx_context_t *rxc2;
688 txc->state = ITJC_TS_IDLE;
689 rxc->state = ITJC_RS_IDLE;
691 rxc2 = &dma_rx_context[sc->sc_unit][0];
693 if (rxc2->state == ITJC_RS_IDLE
694 && rxc2[1].state == ITJC_RS_IDLE)
702 /*---------------------------------------------------------------------------*
703 * Mbuf & if_queues management routines.
704 *---------------------------------------------------------------------------*/
707 itjc_get_rx_mbuf(l1_bchan_state_t *chan, u_int8_t **dst_end_p,
710 struct mbuf *mbuf = chan->in_mbuf;
712 if (mbuf == NULL && which == ITJC_MB_NEW)
714 if ((mbuf = i4b_Bgetmbuf(BCH_MAX_DATALEN)) == NULL)
715 panic("itjc_get_rx_mbuf: cannot allocate mbuf!");
717 chan->in_mbuf = mbuf;
718 chan->in_cbptr = (u_int8_t *)mbuf->m_data;
722 if (dst_end_p != NULL)
725 *dst_end_p = (u_int8_t *)(mbuf->m_data)
731 return chan->in_cbptr;
736 itjc_save_rx_mbuf(l1_bchan_state_t *chan, u_int8_t * dst)
738 struct mbuf *mbuf = chan->in_mbuf;
740 if (dst != NULL && mbuf != NULL)
742 chan->in_cbptr = dst;
743 chan->in_len = dst - (u_int8_t *)mbuf->m_data;
745 else if (dst == NULL && mbuf == NULL)
747 chan->in_cbptr = NULL;
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);
758 itjc_free_rx_mbuf(l1_bchan_state_t *chan)
760 struct mbuf *mbuf = chan->in_mbuf;
765 chan->in_mbuf = NULL;
766 chan->in_cbptr = NULL;
772 itjc_put_rx_mbuf(struct l1_softc *sc, l1_bchan_state_t *chan, u_int16_t len)
775 struct mbuf *mbuf = chan->in_mbuf;
776 u_int8_t *data = mbuf->m_data;
779 mbuf->m_pkthdr.len = mbuf->m_len = len;
781 if (sc->sc_trace & TRACE_B_RX)
783 hdr.unit = L0ITJCUNIT(sc->sc_unit);
784 hdr.type = (chan->channel == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
786 hdr.count = ++sc->sc_trace_bcount;
788 i4b_l1_trace_ind(&hdr, len, data);
791 if (chan->bprot == BPROT_NONE)
793 activity = ! i4b_l1_bchan_tel_silence(data, len);
795 /* move rx'd data to rx queue */
797 if (!IF_QFULL(&chan->rx_queue))
799 IF_ENQUEUE(&chan->rx_queue, mbuf);
810 chan->rxcount += len;
812 (*chan->isic_drvr_linktab->bch_rx_data_ready)
813 (chan->isic_drvr_linktab->unit);
817 (*chan->isic_drvr_linktab->bch_activity)
818 (chan->isic_drvr_linktab->unit, ACT_RX);
820 chan->in_mbuf = NULL;
821 chan->in_cbptr = NULL;
826 #define itjc_free_tx_mbufs(chan) \
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; \
836 itjc_get_tx_mbuf(struct l1_softc *sc, l1_bchan_state_t *chan,
837 u_int8_t **src_p, which_mb_t which)
840 struct mbuf *mbuf = chan->out_mbuf_cur;
841 u_int8_t activity = 1;
850 *src_p = chan->out_mbuf_cur_ptr;
851 return chan->out_mbuf_cur_len;
859 chan->txcount += mbuf->m_len;
867 chan->out_mbuf_cur_ptr = *src_p = NULL;
868 chan->out_mbuf_cur_len = 0;
870 if (chan->out_mbuf_head != NULL)
872 i4b_Bfreembuf(chan->out_mbuf_head);
873 chan->out_mbuf_head = NULL;
880 chan->txcount += mbuf->m_len;
883 if (chan->out_mbuf_head != NULL)
884 i4b_Bfreembuf(chan->out_mbuf_head);
886 IF_DEQUEUE(&chan->tx_queue, mbuf);
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;
894 chan->state &= ~(HSCX_TX_ACTIVE);
896 (*chan->isic_drvr_linktab->bch_tx_queue_empty)
897 (chan->isic_drvr_linktab->unit);
902 chan->out_mbuf_head = 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;
909 chan->state |= HSCX_TX_ACTIVE;
911 if (sc->sc_trace & TRACE_B_TX)
913 hdr.unit = L0ITJCUNIT(sc->sc_unit);
914 hdr.type = (chan->channel == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
916 hdr.count = ++sc->sc_trace_bcount;
918 i4b_l1_trace_ind(&hdr, len, data);
921 if (chan->bprot == BPROT_NONE)
922 activity = ! i4b_l1_bchan_tel_silence(data, len);
925 (*chan->isic_drvr_linktab->bch_activity)
926 (chan->isic_drvr_linktab->unit, ACT_TX);
933 #define itjc_save_tx_mbuf(chan, src, dst) \
935 (chan)->out_mbuf_cur != NULL ? \
937 (chan)->out_mbuf_cur_ptr = (src), \
938 (chan)->out_mbuf_cur_len = (len) \
945 /*---------------------------------------------------------------------------*
946 * B-channel interrupt service routines.
947 *---------------------------------------------------------------------------*/
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.
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)
976 * Since the hardware is running, be conservative and assume
977 * the pointer location has a `fuzy' error factor.
979 dma = itjc_get_dma_offset(ctx, TIGER_DMA_WR_CURR_ADDR);
981 dma_h = itjc_ring_add(dma, 1);
983 dst_end = itjc_ring_sub(dma_l, ITJC_RING_SLOT_WORDS);
985 if (ctx->state != ITJC_TS_ACTIVE)
987 xdu = (ctx->state == ITJC_TS_AFTER_XDU);
988 dst = itjc_ring_add(dma_h, 4);
993 * Check for xmit underruns.
995 xdu_l = dst = ctx->next_write;
996 xdu_h = itjc_ring_add(dst, ITJC_RING_SLOT_WORDS);
999 xdu = (xdu_l <= dma_l && dma_l < xdu_h)
1000 || (xdu_l <= dma_h && dma_h < xdu_h);
1002 xdu = (xdu_l <= dma_l || dma_l < xdu_h)
1003 || (xdu_l <= dma_h || dma_h < xdu_h);
1007 ctx->state = ITJC_TS_AFTER_XDU;
1009 dst = itjc_ring_add(dma_h, 4);
1011 else if (tx_restart)
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.
1021 xdu_l = dst = ctx->next_frame;
1022 xdu_h = itjc_ring_add(dst, ITJC_RING_SLOT_WORDS);
1025 xdu = (xdu_l <= dma_l && dma_l < xdu_h)
1026 || (xdu_l <= dma_h && dma_h < xdu_h);
1028 xdu = (xdu_l <= dma_l || dma_l < xdu_h)
1029 || (xdu_l <= dma_h || dma_h < xdu_h);
1032 dst = itjc_ring_add(dma_h, 4);
1041 if (dst_end_p != NULL)
1042 *dst_end_p = dst_end;
1044 ctx->next_write = dst_end;
1050 #define itjc_rotate_hdlc_flag(blevel) \
1051 ((u_int8_t)(0x7E7E >> (8 - (u_int8_t)((blevel) >> 8))))
1055 itjc_dma_rx_intr(struct l1_softc *sc, l1_bchan_state_t *chan,
1056 dma_rx_context_t *ctx)
1075 if (ctx->state == ITJC_RS_IDLE)
1079 dma = itjc_get_dma_offset(ctx, TIGER_DMA_RD_CURR_ADDR);
1080 dma = itjc_ring_sub(dma, 1);
1081 src = ctx->next_read;
1083 if (chan->bprot == BPROT_NONE)
1085 dst = itjc_get_rx_mbuf(chan, &dst_end, ITJC_MB_CURR);
1090 dst = itjc_get_rx_mbuf(chan, &dst_end,
1094 src = itjc_ring_add(src, 1);
1098 itjc_put_rx_mbuf(sc, chan, BCH_MAX_DATALEN);
1099 dst = dst_end = NULL;
1102 ctx->next_read = src;
1103 itjc_save_rx_mbuf(chan, dst);
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;
1114 dst = itjc_get_rx_mbuf(chan, NULL, ITJC_MB_CURR);
1118 HDLC_DECODE(*dst++, len, tmp, tmp2, blevel, ib, crc, flag,
1121 src = itjc_ring_add(src, 1);
1125 panic("itjc_dma_rx_intr: nfrcmd with "
1126 "valid current frame");
1128 dst = itjc_get_rx_mbuf(chan, &dst_end, ITJC_MB_NEW);
1129 len = dst_end - dst;
1132 len = BCH_MAX_DATALEN - len;
1134 if ((!len) || (len > BCH_MAX_DATALEN))
1137 * NOTE: frames without any data, only crc
1138 * field, should be silently discared.
1140 NDBGL1(L1_S_MSG, "itjc_dma_rx_intr: "
1141 "bad frame (len=%d, unit=%d)",
1144 itjc_free_rx_mbuf(chan);
1152 "CRC (crc=0x%04x, len=%d, unit=%d)",
1153 crc, len, sc->sc_unit);
1155 itjc_free_rx_mbuf(chan);
1160 itjc_put_rx_mbuf(sc, chan, len);
1167 NDBGL1(L1_S_ERR, "Read Abort (unit=%d)", sc->sc_unit);
1169 itjc_free_rx_mbuf(chan);
1174 NDBGL1(L1_S_ERR, "RDO (unit=%d) dma=%d src=%d",
1175 sc->sc_unit, dma, src);
1177 itjc_free_rx_mbuf(chan);
1185 itjc_save_rx_mbuf(chan, dst);
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;
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.
1203 itjc_hdlc_encode(struct l1_softc *sc, l1_bchan_state_t *chan,
1204 dma_tx_context_t * ctx)
1213 u_int16_t saved_len,
1227 saved_len = len = itjc_get_tx_mbuf(sc, chan, &src, ITJC_MB_CURR);
1229 filled = ctx->filled;
1230 flag = ctx->hdlc_flag;
1232 if (src == NULL && flag == 2 && filled >= ITJC_RING_WORDS)
1235 tx_restart = (flag == 2 && src != NULL);
1236 xdu = itjc_xdu(sc, chan, ctx, &dst, &dst_end, tx_restart);
1241 crc = ctx->hdlc_crc;
1242 tmp = ctx->hdlc_tmp;
1243 blevel = ctx->hdlc_blevel;
1249 NDBGL1(L1_H_XFRERR, "XDU");
1253 * Abort the current frame and
1254 * prepare for a full restart.
1256 itjc_free_tx_mbufs(chan);
1257 saved_len = len = filled = 0;
1258 flag = (u_int8_t)-2;
1260 else if (filled < ITJC_RING_SLOT_WORDS)
1263 * A little garbage may have been retransmitted.
1264 * Send an abort before any new data.
1267 flag = (u_int8_t)-2;
1274 while (dst != dst_end)
1277 *src++, len, tmp, tmp2, blevel, ib, crc, flag,
1279 if ((len = saved_len) == 0)
1280 len = itjc_get_tx_mbuf(sc, chan, &src,
1285 ctx->next_frame = dst;
1287 flag_byte = itjc_rotate_hdlc_flag(blevel);
1289 for (dst_end1 = itjc_ring_sub(dst_end, 1);
1291 dst = itjc_ring_add(dst, 1))
1293 ring[dst] = flag_byte;
1300 ctx->state = ITJC_TS_ACTIVE;
1304 len = itjc_get_tx_mbuf(sc, chan, &src, ITJC_MB_NEXT);
1307 ring[dst] = (u_int8_t)tmp;
1308 dst = itjc_ring_add(dst, 1);
1313 ctx->hdlc_blevel = blevel;
1314 ctx->hdlc_flag = flag;
1315 ctx->hdlc_tmp = tmp;
1316 ctx->hdlc_crc = crc;
1319 ctx->filled = filled;
1320 ctx->next_write = dst;
1322 itjc_save_tx_mbuf(chan, src, len);
1327 itjc_dma_tx_intr(struct l1_softc *sc, l1_bchan_state_t *chan,
1328 dma_tx_context_t * ctx)
1341 if (ctx->state == ITJC_TS_IDLE)
1344 if (chan->bprot != BPROT_NONE)
1346 itjc_hdlc_encode(sc, chan, ctx);
1351 filled = ctx->filled;
1353 len = itjc_get_tx_mbuf(sc, chan, &src, ITJC_MB_CURR);
1355 if (len == 0 && filled >= ITJC_RING_WORDS)
1358 xdu = itjc_xdu(sc, chan, ctx, &dst, &dst_end, len != 0);
1360 if (xdu && filled < ITJC_RING_WORDS)
1362 NDBGL1(L1_H_XFRERR, "XDU");
1370 ctx->state = ITJC_TS_ACTIVE;
1372 data_end = src + len;
1373 while (dst != dst_end)
1375 ring[dst] = *src++; --len;
1377 dst = itjc_ring_add(dst, 1);
1379 if (src >= data_end)
1381 len = itjc_get_tx_mbuf(sc, chan, &src, ITJC_MB_NEXT);
1383 len = itjc_get_tx_mbuf(sc, chan,
1391 data_end = src + len;
1395 itjc_save_tx_mbuf(chan, src, len);
1400 ctx->next_frame = dst;
1402 for (; dst != dst_end; dst = itjc_ring_add(dst, 1))
1404 ring[dst] = ITJC_TEL_SILENCE_BYTE;
1408 ctx->next_write = dst;
1409 ctx->filled = filled;
1416 /*---------------------------------------------------------------------------*
1417 * NetJet fifo read/write routines.
1418 *---------------------------------------------------------------------------*/
1421 itjc_read_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
1425 if (what != ISIC_WHAT_ISAC)
1426 panic("itjc_write_fifo: Trying to read from HSCX fifo.\n");
1428 itjc_set_pib_addr_msb(0);
1429 itjc_read_multi_1(PIB_OFFSET, buf, size);
1434 itjc_write_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
1438 if (what != ISIC_WHAT_ISAC)
1439 panic("itjc_write_fifo: Trying to write to HSCX fifo.\n");
1441 itjc_set_pib_addr_msb(0);
1442 itjc_write_multi_1(PIB_OFFSET, buf, size);
1446 /*---------------------------------------------------------------------------*
1447 * Read an ISAC register.
1448 *---------------------------------------------------------------------------*/
1450 itjc_read_reg(struct l1_softc *sc, int what, bus_size_t offs)
1454 if (what != ISIC_WHAT_ISAC)
1456 panic("itjc_read_reg: what(%d) != ISIC_WHAT_ISAC\n",
1461 itjc_set_pib_addr_msb(offs);
1462 return itjc_read_1(itjc_pib_2_pci(offs));
1466 /*---------------------------------------------------------------------------*
1467 * Write an ISAC register.
1468 *---------------------------------------------------------------------------*/
1470 itjc_write_reg(struct l1_softc *sc, int what, bus_size_t offs, u_int8_t data)
1474 if (what != ISIC_WHAT_ISAC)
1476 panic("itjc_write_reg: what(%d) != ISIC_WHAT_ISAC\n",
1481 itjc_set_pib_addr_msb(offs);
1482 itjc_write_1(itjc_pib_2_pci(offs), data);
1486 /*---------------------------------------------------------------------------*
1487 * itjc_probe - probe for a card.
1488 *---------------------------------------------------------------------------*/
1489 static int itjc_probe(device_t dev)
1491 u_int16_t vid = pci_get_vendor(dev),
1492 did = pci_get_device(dev);
1494 if ((vid == PCI_TJNET_VID) && (did == PCI_TJ300_DID))
1496 device_set_desc(dev, "NetJet-S");
1504 /*---------------------------------------------------------------------------*
1505 * itjc_attach - attach a (previously probed) card.
1506 *---------------------------------------------------------------------------*/
1508 itjc_attach(device_t dev)
1510 bus_space_handle_t h;
1513 struct l1_softc *sc = device_get_softc(dev);
1515 u_int16_t vid = pci_get_vendor(dev),
1516 did = pci_get_device(dev);
1518 int unit = device_get_unit(dev),
1524 dma_context_t *ctx = &dma_context[unit];
1527 bzero(sc, sizeof(struct l1_softc));
1529 /* Probably not really required. */
1530 if (unit > ITJC_MAXUNIT)
1532 printf("itjc%d: Error, unit > ITJC_MAXUNIT!\n", unit);
1537 if (!(vid == PCI_TJNET_VID && did == PCI_TJ300_DID))
1539 printf("itjc%d: unknown device (%04X,%04X)!\n", unit, vid, did);
1543 itjc_scp[unit] = sc;
1545 sc->sc_resources.io_rid[0] = PCIR_MAPS+0;
1546 sc->sc_resources.io_base[0] = bus_alloc_resource(dev, SYS_RES_IOPORT,
1547 &sc->sc_resources.io_rid[0], 0, ~0, 1, RF_ACTIVE);
1549 if (sc->sc_resources.io_base[0] == NULL)
1551 printf("itjc%d: couldn't map IO port\n", unit);
1556 h = rman_get_bushandle(sc->sc_resources.io_base[0]);
1557 t = rman_get_bustag(sc->sc_resources.io_base[0]);
1561 /* Allocate interrupt. */
1562 sc->sc_resources.irq_rid = 0;
1563 sc->sc_resources.irq = bus_alloc_resource(dev, SYS_RES_IRQ,
1564 &sc->sc_resources.irq_rid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
1566 if (sc->sc_resources.irq == NULL)
1568 printf("itjc%d: couldn't map interrupt\n", unit);
1575 error = bus_setup_intr(dev, sc->sc_resources.irq, INTR_TYPE_NET,
1576 itjc_intr, sc, &ih, NULL);
1580 printf("itjc%d: couldn't set up irq handler\n", unit);
1586 * Reset the ASIC & the ISAC.
1588 itjc_write_1(TIGER_RESET_PIB_CL_TIME, TIGER_RESET_ALL);
1590 DELAY(SEC_DELAY/100); /* Give it 10 ms to reset ...*/
1592 itjc_write_1(TIGER_RESET_PIB_CL_TIME,
1593 TIGER_SELF_ADDR_DMA | TIGER_PIB_3_CYCLES);
1595 DELAY(SEC_DELAY/100); /* ... and more 10 to recover. */
1598 * First part of DMA initialization. Create & map the memory
1599 * pool that will be used to bear the rx & tx ring buffers.
1601 ctx->state = ITJC_DS_LOADING;
1603 error = bus_dma_tag_create(
1607 BUS_SPACE_MAXADDR_32BIT, /* lowaddr*/
1608 BUS_SPACE_MAXADDR, /* highaddr*/
1610 NULL, /* filterarg*/
1611 ITJC_DMA_POOL_BYTES, /* maxsize*/
1613 ITJC_DMA_POOL_BYTES, /* maxsegsz*/
1614 BUS_DMA_ALLOCNOW | BUS_DMA_COHERENT, /* flags*/
1619 printf("itjc%d: couldn't create bus DMA tag.\n", unit);
1625 error = bus_dmamem_alloc(
1626 ctx->tag, /* DMA tag */
1627 (void **)&ctx->pool, /* KV addr of the allocated memory */
1628 BUS_DMA_NOWAIT | BUS_DMA_COHERENT, /* flags */
1629 &ctx->map); /* KV <-> PCI map */
1635 * Load the KV <-> PCI map so the device sees the same
1636 * memory segment as pointed by pool. Note: since the
1637 * load may happen assyncronously (completion indicated by
1638 * the execution of the callback function) we have to
1639 * delay the initialization of the DMA engine to a moment we
1640 * actually have the proper bus addresses to feed the Tiger
1641 * and our DMA control blocks. This will be done in
1642 * itjc_bchannel_setup via a call to itjc_dma_start.
1645 ctx->tag, /* DMA tag */
1646 ctx->map, /* DMA map */
1647 ctx->pool, /* KV addr of buffer */
1648 ITJC_DMA_POOL_BYTES, /* buffer size */
1649 itjc_map_callback, /* this receive the bus addr/error */
1650 ctx, /* callback aux arg */
1656 * Setup the AUX port so we can talk to the ISAC.
1658 itjc_write_1(TIGER_AUX_PORT_CNTL, TIGER_AUX_NJ_DEFAULT);
1659 itjc_write_1(TIGER_INT1_MASK, TIGER_ISAC_INT);
1662 * From now on, almost like a `normal' ISIC driver.
1667 ISAC_BASE = (caddr_t)ISIC_WHAT_ISAC;
1669 HSCX_A_BASE = (caddr_t)ISIC_WHAT_HSCXA;
1670 HSCX_B_BASE = (caddr_t)ISIC_WHAT_HSCXB;
1672 /* setup access routines */
1674 sc->clearirq = NULL;
1675 sc->readreg = itjc_read_reg;
1676 sc->writereg = itjc_write_reg;
1678 sc->readfifo = itjc_read_fifo;
1679 sc->writefifo = itjc_write_fifo;
1681 /* setup card type */
1683 sc->sc_cardtyp = CARD_TYPEP_NETJET_S;
1685 /* setup IOM bus type */
1687 sc->sc_bustyp = BUS_TYPE_IOM2;
1689 /* set up some other miscellaneous things */
1691 sc->sc_bfifolen = 2 * ITJC_RING_SLOT_WORDS;
1693 printf("itjc%d: ISAC 2186 Version 1.1 (IOM-2)\n", unit);
1698 /* init the "HSCX" */
1699 itjc_bchannel_setup(sc->sc_unit, HSCX_CH_A, BPROT_NONE, 0);
1701 itjc_bchannel_setup(sc->sc_unit, HSCX_CH_B, BPROT_NONE, 0);
1703 /* can't use the normal B-Channel stuff */
1704 itjc_init_linktab(sc);
1706 /* set trace level */
1708 sc->sc_trace = TRACE_OFF;
1710 sc->sc_state = ISAC_IDLE;
1719 sc->sc_freeflag = 0;
1721 sc->sc_obuf2 = NULL;
1722 sc->sc_freeflag2 = 0;
1724 callout_init(&sc->sc_T3_timeout);
1725 callout_init(&sc->sc_T4_timeout);
1727 /* init higher protocol layers */
1729 i4b_l1_mph_status_ind(L0ITJCUNIT(sc->sc_unit), STI_ATTACH,
1730 sc->sc_cardtyp, &itjc_l1mux_func);
1736 switch (res_init_level)
1739 bus_dmamap_unload(ctx->tag, ctx->map);
1743 bus_dmamem_free(ctx->tag, ctx->pool, ctx->map);
1744 bus_dmamap_destroy(ctx->tag, ctx->map);
1748 bus_dma_tag_destroy(ctx->tag);
1752 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_resources.irq);
1756 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_MAPS+0,
1757 sc->sc_resources.io_base[0]);
1764 itjc_scp[unit] = NULL;
1771 /*---------------------------------------------------------------------------*
1772 * itjc_intr - main interrupt service routine.
1773 *---------------------------------------------------------------------------*/
1775 itjc_intr(void *xsc)
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];
1785 /* Honor interrupts from successfully configured cards only. */
1786 if (dma->state < ITJC_DS_STOPPED)
1789 /* First, we check the ISAC... */
1790 if (! (itjc_read_1(TIGER_AUX_PORT_DATA) & TIGER_ISAC_INT_MASK))
1792 itjc_write_1(TIGER_INT1_STATUS, TIGER_ISAC_INT);
1793 NDBGL1(L1_H_IRQ, "ISAC");
1797 /* ... after what we always have a look at the DMA rings. */
1799 NDBGL1(L1_H_IRQ, "Tiger");
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);
1806 itjc_dma_rx_intr(sc, chan, rxc);
1807 itjc_dma_tx_intr(sc, chan, txc);
1809 ++chan; ++rxc; ++txc;
1811 itjc_dma_rx_intr(sc, chan, rxc);
1812 itjc_dma_tx_intr(sc, chan, txc);
1816 /*---------------------------------------------------------------------------*
1817 * itjc_bchannel_setup - (Re)initialize and start/stop a Bchannel.
1818 *---------------------------------------------------------------------------*/
1820 itjc_bchannel_setup(int unit, int h_chan, int bprot, int activate)
1822 #if defined(__DragonFly__) || defined(__FreeBSD__)
1823 struct l1_softc *sc = itjc_scp[unit];
1825 struct l1_softc *sc = isic_find_sc(unit);
1827 l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
1831 NDBGL1(L1_BCHAN, "unit=%d, channel=%d, %s",
1832 unit, h_chan, activate ? "activate" : "deactivate");
1835 * If we are deactivating the channel, we have to stop
1836 * the DMA before we reset the channel control structures.
1839 itjc_bchannel_dma_setup(sc, h_chan, activate);
1843 chan->state = HSCX_IDLE;
1845 chan->unit = sc->sc_unit; /* unit number */
1846 chan->channel = h_chan; /* B channel */
1847 chan->bprot = bprot; /* B channel protocol */
1851 i4b_Bcleanifq(&chan->rx_queue); /* clean rx queue */
1853 chan->rx_queue.ifq_maxlen = IFQ_MAXLEN;
1855 chan->rxcount = 0; /* reset rx counter */
1857 i4b_Bfreembuf(chan->in_mbuf); /* clean rx mbuf */
1859 chan->in_mbuf = NULL; /* reset mbuf ptr */
1860 chan->in_cbptr = NULL; /* reset mbuf curr ptr */
1861 chan->in_len = 0; /* reset mbuf data len */
1863 /* transmitter part */
1865 i4b_Bcleanifq(&chan->tx_queue); /* clean tx queue */
1867 chan->tx_queue.ifq_maxlen = IFQ_MAXLEN;
1869 chan->txcount = 0; /* reset tx counter */
1871 i4b_Bfreembuf(chan->out_mbuf_head); /* clean tx mbuf */
1873 chan->out_mbuf_head = NULL; /* reset head mbuf ptr */
1874 chan->out_mbuf_cur = NULL; /* reset current mbuf ptr */
1875 chan->out_mbuf_cur_ptr = NULL; /* reset current mbuf data ptr */
1876 chan->out_mbuf_cur_len = 0; /* reset current mbuf data cnt */
1879 * Only setup & start the DMA after all other channel
1880 * control structures are in place.
1883 itjc_bchannel_dma_setup(sc, h_chan, activate);
1889 /*---------------------------------------------------------------------------*
1890 * itjc_bchannel_start - Signal us we have more data to send.
1891 *---------------------------------------------------------------------------*/
1893 itjc_bchannel_start(int unit, int h_chan)
1897 * I disabled this routine because it was causing crashes when
1898 * this driver was used with the ISP (kernel SPPP) protocol driver.
1899 * The scenario is reproductible:
1900 * Use the -link1 (dial on demand) ifconfig option.
1901 * Start an interactive TCP connection to somewhere.
1902 * Wait until the PPP connection times out and is dropped.
1903 * Try to send something on the TCP connection.
1904 * The machine will print some garbage and halt or reboot
1905 * (no panic messages).
1907 * I've nailed down the problem to the fact that this routine
1908 * was being called before the B channel had been setup again.
1910 * For now, I don't have a good solution other than this one.
1911 * But, don't despair. The impact of it is unnoticeable.
1914 #if defined(__DragonFly__) || defined(__FreeBSD__)
1915 struct l1_softc *sc = itjc_scp[unit];
1917 struct l1_softc *sc = isic_find_sc(unit);
1919 l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
1920 dma_tx_context_t *txc = &dma_tx_context[unit][h_chan];
1924 if (chan->state & HSCX_TX_ACTIVE)
1930 itjc_dma_tx_intr(sc, chan, txc);
1937 /*---------------------------------------------------------------------------*
1938 * itjc_shutdown - Stop the driver and reset the card.
1939 *---------------------------------------------------------------------------*/
1941 itjc_shutdown(device_t dev)
1943 struct l1_softc *sc = device_get_softc(dev);
1948 * Stop the DMA the nice and easy way.
1950 itjc_bchannel_setup(sc->sc_unit, 0, BPROT_NONE, 0);
1951 itjc_bchannel_setup(sc->sc_unit, 1, BPROT_NONE, 0);
1956 itjc_write_1(TIGER_RESET_PIB_CL_TIME, TIGER_RESET_ALL);
1958 DELAY(SEC_DELAY/100); /* Give it 10 ms to reset ...*/
1960 itjc_write_1(TIGER_RESET_PIB_CL_TIME,
1961 TIGER_SELF_ADDR_DMA | TIGER_LATCH_DMA_INT | TIGER_PIB_3_CYCLES);
1963 DELAY(SEC_DELAY/100); /* ... and more 10 to recover */
1967 /*---------------------------------------------------------------------------*
1968 * itjc_ret_linktab - Return the address of itjc drivers linktab.
1969 *---------------------------------------------------------------------------*/
1971 itjc_ret_linktab(int unit, int channel)
1973 #if defined(__DragonFly__) || defined(__FreeBSD__)
1974 struct l1_softc *sc = itjc_scp[unit];
1976 struct l1_softc *sc = isic_find_sc(unit);
1978 l1_bchan_state_t *chan = &sc->sc_chan[channel];
1980 return(&chan->isic_isdn_linktab);
1983 /*---------------------------------------------------------------------------*
1984 * itjc_set_linktab - Set the driver linktab in the b channel softc.
1985 *---------------------------------------------------------------------------*/
1987 itjc_set_linktab(int unit, int channel, drvr_link_t *dlt)
1989 #if defined(__DragonFly__) || defined(__FreeBSD__)
1990 struct l1_softc *sc = itjc_scp[unit];
1992 struct l1_softc *sc = isic_find_sc(unit);
1994 l1_bchan_state_t *chan = &sc->sc_chan[channel];
1996 chan->isic_drvr_linktab = dlt;
2000 /*---------------------------------------------------------------------------*
2001 * itjc_init_linktab - Initialize our local linktab.
2002 *---------------------------------------------------------------------------*/
2004 itjc_init_linktab(struct l1_softc *sc)
2006 l1_bchan_state_t *chan = &sc->sc_chan[HSCX_CH_A];
2007 isdn_link_t *lt = &chan->isic_isdn_linktab;
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)
2013 ctrl_types[CTRL_PASSIVE].set_linktab = itjc_set_linktab;
2014 ctrl_types[CTRL_PASSIVE].get_linktab = itjc_ret_linktab;
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;
2025 /* used by non-HDLC data transfers, i.e. telephony drivers */
2026 lt->rx_queue = &chan->rx_queue;
2028 /* used by HDLC data transfers, i.e. ipr and isp drivers */
2029 lt->rx_mbuf = &chan->in_mbuf;
2031 chan = &sc->sc_chan[HSCX_CH_B];
2032 lt = &chan->isic_isdn_linktab;
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;
2041 /* used by non-HDLC data transfers, i.e. telephony drivers */
2042 lt->rx_queue = &chan->rx_queue;
2044 /* used by HDLC data transfers, i.e. ipr and isp drivers */
2045 lt->rx_mbuf = &chan->in_mbuf;
2049 /*---------------------------------------------------------------------------*
2050 * itjc_bchannel_stat - Collect link statistics for a given B channel.
2051 *---------------------------------------------------------------------------*/
2053 itjc_bchannel_stat(int unit, int h_chan, bchan_statistics_t *bsp)
2055 #if defined(__DragonFly__) || defined(__FreeBSD__)
2056 struct l1_softc *sc = itjc_scp[unit];
2058 struct l1_softc *sc = isic_find_sc(unit);
2060 l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
2064 bsp->outbytes = chan->txcount;
2065 bsp->inbytes = chan->rxcount;
2074 /*---------------------------------------------------------------------------*
2075 * Netjet - ISAC interrupt routine.
2076 *---------------------------------------------------------------------------*/
2078 itjc_isac_intr(struct l1_softc *sc)
2084 /* get isac irq status */
2085 irq_stat = ISAC_READ(I_ISTA);
2088 itjc_isac_irq(sc, irq_stat); /* isac handler */
2092 ISAC_WRITE(I_MASK, 0xff);
2096 ISAC_WRITE(I_MASK, ISAC_IMASK);
2100 /*---------------------------------------------------------------------------*
2101 * itjc_recover - Try to recover from ISAC irq lockup.
2102 *---------------------------------------------------------------------------*/
2104 itjc_recover(struct l1_softc *sc)
2108 /* get isac irq status */
2110 byte = ISAC_READ(I_ISTA);
2112 NDBGL1(L1_ERROR, " ISAC: ISTA = 0x%x", byte);
2114 if(byte & ISAC_ISTA_EXI)
2115 NDBGL1(L1_ERROR, " ISAC: EXIR = 0x%x", (u_char)ISAC_READ(I_EXIR));
2117 if(byte & ISAC_ISTA_CISQ)
2119 byte = ISAC_READ(I_CIRR);
2121 NDBGL1(L1_ERROR, " ISAC: CISQ = 0x%x", byte);
2123 if(byte & ISAC_CIRR_SQC)
2124 NDBGL1(L1_ERROR, " ISAC: SQRR = 0x%x", (u_char)ISAC_READ(I_SQRR));
2127 NDBGL1(L1_ERROR, " ISAC: IMASK = 0x%x", ISAC_IMASK);
2129 ISAC_WRITE(I_MASK, 0xff);
2131 ISAC_WRITE(I_MASK, ISAC_IMASK);
2134 #endif /* NITJC > 0 */