xref: /netbsd-src/sys/dev/vme/si.c (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1 /*	$NetBSD: si.c,v 1.21 2008/04/28 20:24:01 martin Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996,2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Adam Glass, David Jones, Gordon W. Ross, Jason R. Thorpe and
9  * Paul Kranenburg.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * This file contains VME bus-dependent of the `si' SCSI adapter.
35  * This hardware is frequently found on Sun 3 and Sun 4 machines.
36  *
37  * The SCSI machinery on this adapter is implemented by an NCR5380,
38  * which is taken care of by the chipset driver in /sys/dev/ic/ncr5380sbc.c
39  *
40  * The logic has a bit to enable or disable the DMA engine,
41  * but that bit also gates the interrupt line from the NCR5380!
42  * Therefore, in order to get any interrupt from the 5380, (i.e.
43  * for reselect) one must clear the DMA engine transfer count and
44  * then enable DMA.  This has the further complication that you
45  * CAN NOT touch the NCR5380 while the DMA enable bit is set, so
46  * we have to turn DMA back off before we even look at the 5380.
47  *
48  * What wonderfully whacky hardware this is!
49  *
50  */
51 
52 /*
53  * This driver originated as an MD implementation for the sun3 and sun4
54  * ports. The notes pertaining to that history are included below.
55  *
56  * David Jones wrote the initial version of this module for NetBSD/sun3,
57  * which included support for the VME adapter only. (no reselection).
58  *
59  * Gordon Ross added support for the Sun 3 OBIO adapter, and re-worked
60  * both the VME and OBIO code to support disconnect/reselect.
61  * (Required figuring out the hardware "features" noted above.)
62  *
63  * The autoconfiguration boilerplate came from Adam Glass.
64  *
65  * Jason R. Thorpe ported the autoconfiguration and VME portions to
66  * NetBSD/sparc, and added initial support for the 4/100 "SCSI Weird",
67  * a wacky OBIO variant of the VME SCSI-3.  Many thanks to Chuck Cranor
68  * for lots of helpful tips and suggestions.  Thanks also to Paul Kranenburg
69  * and Chris Torek for bits of insight needed along the way.  Thanks to
70  * David Gilbert and Andrew Gillham who risked filesystem life-and-limb
71  * for the sake of testing.  Andrew Gillham helped work out the bugs
72  * the 4/100 DMA code.
73  */
74 
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: si.c,v 1.21 2008/04/28 20:24:01 martin Exp $");
77 
78 #include "opt_ddb.h"
79 
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
83 #include <sys/malloc.h>
84 #include <sys/errno.h>
85 #include <sys/device.h>
86 #include <sys/buf.h>
87 
88 #include <sys/bus.h>
89 #include <sys/intr.h>
90 
91 #include <dev/vme/vmereg.h>
92 #include <dev/vme/vmevar.h>
93 
94 #include <dev/scsipi/scsi_all.h>
95 #include <dev/scsipi/scsipi_all.h>
96 #include <dev/scsipi/scsipi_debug.h>
97 #include <dev/scsipi/scsiconf.h>
98 
99 #ifndef Debugger
100 #define	Debugger()
101 #endif
102 
103 #ifndef DEBUG
104 #define DEBUG XXX
105 #endif
106 
107 #include <dev/ic/ncr5380reg.h>
108 #include <dev/ic/ncr5380var.h>
109 
110 #include <dev/vme/sireg.h>
111 
112 /*
113  * Transfers smaller than this are done using PIO
114  * (on assumption they're not worth DMA overhead)
115  */
116 #define	MIN_DMA_LEN 128
117 
118 #ifdef	DEBUG
119 int si_debug = 0;
120 #endif
121 
122 /*
123  * This structure is used to keep track of mapped DMA requests.
124  */
125 struct si_dma_handle {
126 	int 		dh_flags;
127 #define	SIDH_BUSY	0x01		/* This DH is in use */
128 #define	SIDH_OUT	0x02		/* DMA does data out (write) */
129 	int 		dh_maplen;	/* Original data length */
130 	bus_dmamap_t	dh_dmamap;
131 #define dh_dvma	dh_dmamap->dm_segs[0].ds_addr /* VA of buffer in DVMA space */
132 };
133 
134 /*
135  * The first structure member has to be the ncr5380_softc
136  * so we can just cast to go back and fourth between them.
137  */
138 struct si_softc {
139 	struct ncr5380_softc	ncr_sc;
140 	bus_space_tag_t		sc_bustag;	/* bus tags */
141 	bus_dma_tag_t		sc_dmatag;
142 	vme_chipset_tag_t	sc_vctag;
143 
144 	int		sc_adapter_iv_am; /* int. vec + address modifier */
145 	struct si_dma_handle *sc_dma;
146 	int		sc_xlen;	/* length of current DMA segment. */
147 	int		sc_options;	/* options for this instance. */
148 };
149 
150 /*
151  * Options.  By default, DMA is enabled and DMA completion interrupts
152  * and reselect are disabled.  You may enable additional features
153  * the `flags' directive in your kernel's configuration file.
154  *
155  * Alternatively, you can patch your kernel with DDB or some other
156  * mechanism.  The sc_options member of the softc is OR'd with
157  * the value in si_options.
158  *
159  * Note, there's a separate sw_options to make life easier.
160  */
161 #define	SI_ENABLE_DMA	0x01	/* Use DMA (maybe polled) */
162 #define	SI_DMA_INTR	0x02	/* DMA completion interrupts */
163 #define	SI_DO_RESELECT	0x04	/* Allow disconnect/reselect */
164 #define	SI_OPTIONS_MASK	(SI_ENABLE_DMA|SI_DMA_INTR|SI_DO_RESELECT)
165 #define SI_OPTIONS_BITS	"\10\3RESELECT\2DMA_INTR\1DMA"
166 int si_options = SI_ENABLE_DMA|SI_DMA_INTR|SI_DO_RESELECT;
167 
168 static int	si_match(device_t, cfdata_t, void *);
169 static void	si_attach(device_t, device_t, void *);
170 static int	si_intr(void *);
171 static void	si_reset_adapter(struct ncr5380_softc *);
172 
173 void	si_dma_alloc(struct ncr5380_softc *);
174 void	si_dma_free(struct ncr5380_softc *);
175 void	si_dma_poll(struct ncr5380_softc *);
176 
177 void	si_dma_setup(struct ncr5380_softc *);
178 void	si_dma_start(struct ncr5380_softc *);
179 void	si_dma_eop(struct ncr5380_softc *);
180 void	si_dma_stop(struct ncr5380_softc *);
181 
182 void	si_intr_on (struct ncr5380_softc *);
183 void	si_intr_off(struct ncr5380_softc *);
184 
185 /*
186  * Shorthand bus space access
187  * XXX - must look into endian issues here.
188  */
189 #define SIREG_READ(sc, index) \
190 	bus_space_read_2((sc)->sc_regt, (sc)->sc_regh, index)
191 #define SIREG_WRITE(sc, index, v) \
192 	bus_space_write_2((sc)->sc_regt, (sc)->sc_regh, index, v)
193 
194 
195 /* Auto-configuration glue. */
196 CFATTACH_DECL_NEW(si, sizeof(struct si_softc),
197     si_match, si_attach, NULL, NULL);
198 
199 static int
200 si_match(device_t parent, cfdata_t cf, void *aux)
201 {
202 	struct vme_attach_args	*va = aux;
203 	vme_chipset_tag_t	ct = va->va_vct;
204         vme_am_t		mod;
205         vme_addr_t		vme_addr;
206 
207 	/* Make sure there is something there... */
208 	mod = VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
209 	vme_addr = va->r[0].offset;
210 
211 	if (vme_probe(ct, vme_addr, 1, mod, VME_D8, NULL, 0) != 0)
212 		return 0;
213 
214 	/*
215 	 * If this is a VME SCSI board, we have to determine whether
216 	 * it is an "sc" (Sun2) or "si" (Sun3) SCSI board.  This can
217 	 * be determined using the fact that the "sc" board occupies
218 	 * 4K bytes in VME space but the "si" board occupies 2K bytes.
219 	 */
220 	return vme_probe(ct, vme_addr + 0x801, 1, mod, VME_D8, NULL, 0) != 0;
221 }
222 
223 static void
224 si_attach(device_t parent, device_t self, void *aux)
225 {
226 	struct si_softc		*sc = device_private(self);
227 	struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
228 	struct vme_attach_args	*va = aux;
229 	vme_chipset_tag_t	ct = va->va_vct;
230 	bus_space_tag_t		bt;
231 	bus_space_handle_t	bh;
232 	vme_mapresc_t resc;
233 	vme_intr_handle_t	ih;
234 	vme_am_t		mod;
235 	char bits[64];
236 	int i;
237 
238 	ncr_sc->sc_dev = self;
239 	sc->sc_dmatag = va->va_bdt;
240 	sc->sc_vctag = ct;
241 
242 	mod = VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
243 
244 	if (vme_space_map(ct, va->r[0].offset, SIREG_BANK_SZ,
245 			  mod, VME_D8, 0, &bt, &bh, &resc) != 0)
246 		panic("%s: vme_space_map", device_xname(self));
247 
248 	ncr_sc->sc_regt = bt;
249 	ncr_sc->sc_regh = bh;
250 
251 	sc->sc_options = si_options;
252 
253 	ncr_sc->sc_dma_setup = si_dma_setup;
254 	ncr_sc->sc_dma_start = si_dma_start;
255 	ncr_sc->sc_dma_eop   = si_dma_stop;
256 	ncr_sc->sc_dma_stop  = si_dma_stop;
257 
258 	vme_intr_map(ct, va->ilevel, va->ivector, &ih);
259 	vme_intr_establish(ct, ih, IPL_BIO, si_intr, sc);
260 
261 	aprint_normal("\n");
262 
263 	sc->sc_adapter_iv_am = (mod << 8) | (va->ivector & 0xFF);
264 
265 	/*
266 	 * Pull in the options flags.  Allow the user to completely
267 	 * override the default values.
268 	 */
269 	if ((device_cfdata(self)->cf_flags & SI_OPTIONS_MASK) != 0)
270 		sc->sc_options =
271 		    device_cfdata(self)->cf_flags & SI_OPTIONS_MASK;
272 
273 	/*
274 	 * Initialize fields used by the MI code
275 	 */
276 
277 	/* NCR5380 register bank offsets */
278 	ncr_sc->sci_r0 = 0;
279 	ncr_sc->sci_r1 = 1;
280 	ncr_sc->sci_r2 = 2;
281 	ncr_sc->sci_r3 = 3;
282 	ncr_sc->sci_r4 = 4;
283 	ncr_sc->sci_r5 = 5;
284 	ncr_sc->sci_r6 = 6;
285 	ncr_sc->sci_r7 = 7;
286 
287 	ncr_sc->sc_rev = NCR_VARIANT_NCR5380;
288 
289 	/*
290 	 * MD function pointers used by the MI code.
291 	 */
292 	ncr_sc->sc_pio_out = ncr5380_pio_out;
293 	ncr_sc->sc_pio_in =  ncr5380_pio_in;
294 	ncr_sc->sc_dma_alloc = si_dma_alloc;
295 	ncr_sc->sc_dma_free  = si_dma_free;
296 	ncr_sc->sc_dma_poll  = si_dma_poll;
297 
298 	ncr_sc->sc_flags = 0;
299 	if ((sc->sc_options & SI_DO_RESELECT) == 0)
300 		ncr_sc->sc_no_disconnect = 0xFF;
301 	if ((sc->sc_options & SI_DMA_INTR) == 0)
302 		ncr_sc->sc_flags |= NCR5380_FORCE_POLLING;
303 	ncr_sc->sc_min_dma_len = MIN_DMA_LEN;
304 
305 	/*
306 	 * Allocate DMA handles.
307 	 */
308 	i = SCI_OPENINGS * sizeof(struct si_dma_handle);
309 	sc->sc_dma = malloc(i, M_DEVBUF, M_NOWAIT);
310 	if (sc->sc_dma == NULL)
311 		panic("si: DMA handle malloc failed");
312 
313 	for (i = 0; i < SCI_OPENINGS; i++) {
314 		sc->sc_dma[i].dh_flags = 0;
315 
316 		/* Allocate a DMA handle */
317 		if (vme_dmamap_create(
318 				sc->sc_vctag,	/* VME chip tag */
319 				MAXPHYS,	/* size */
320 				VME_AM_A24,	/* address modifier */
321 				VME_D16,	/* data size */
322 				0,		/* swap */
323 				1,		/* nsegments */
324 				MAXPHYS,	/* maxsegsz */
325 				0,		/* boundary */
326 				BUS_DMA_NOWAIT,
327 				&sc->sc_dma[i].dh_dmamap) != 0) {
328 
329 			aprint_error_dev(self, "DMA buffer map create error\n");
330 			return;
331 		}
332 	}
333 
334 	if (sc->sc_options) {
335 		aprint_normal_dev(self, "options=%s\n",
336 		    bitmask_snprintf(sc->sc_options, SI_OPTIONS_BITS,
337 		    bits, sizeof(bits)));
338 	}
339 
340 	ncr_sc->sc_channel.chan_id = 7;
341 	ncr_sc->sc_adapter.adapt_minphys = minphys;
342 
343 	/*
344 	 *  Initialize si board itself.
345 	 */
346 	si_reset_adapter(ncr_sc);
347 	ncr5380_attach(ncr_sc);
348 
349 	if (sc->sc_options & SI_DO_RESELECT) {
350 		/*
351 		 * Need to enable interrupts (and DMA!)
352 		 * on this H/W for reselect to work.
353 		 */
354 		ncr_sc->sc_intr_on   = si_intr_on;
355 		ncr_sc->sc_intr_off  = si_intr_off;
356 	}
357 }
358 
359 #define CSR_WANT (SI_CSR_SBC_IP | SI_CSR_DMA_IP | \
360 	SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR )
361 
362 static int
363 si_intr(void *arg)
364 {
365 	struct si_softc *sc = arg;
366 	struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
367 	int dma_error, claimed;
368 	uint16_t csr;
369 
370 	claimed = 0;
371 	dma_error = 0;
372 
373 	/* SBC interrupt? DMA interrupt? */
374 	csr = SIREG_READ(ncr_sc, SIREG_CSR);
375 
376 	NCR_TRACE("si_intr: csr=0x%x\n", csr);
377 
378 	if (csr & SI_CSR_DMA_CONFLICT) {
379 		dma_error |= SI_CSR_DMA_CONFLICT;
380 		printf("%s: DMA conflict\n", __func__);
381 	}
382 	if (csr & SI_CSR_DMA_BUS_ERR) {
383 		dma_error |= SI_CSR_DMA_BUS_ERR;
384 		printf("%s: DMA bus error\n", __func__);
385 	}
386 	if (dma_error) {
387 		if (sc->ncr_sc.sc_state & NCR_DOINGDMA)
388 			sc->ncr_sc.sc_state |= NCR_ABORTING;
389 		/* Make sure we will call the main isr. */
390 		csr |= SI_CSR_DMA_IP;
391 	}
392 
393 	if (csr & (SI_CSR_SBC_IP | SI_CSR_DMA_IP)) {
394 		claimed = ncr5380_intr(&sc->ncr_sc);
395 #ifdef DEBUG
396 		if (!claimed) {
397 			printf("%s: spurious from SBC\n", __func__);
398 			if (si_debug & 4) {
399 				Debugger();	/* XXX */
400 			}
401 		}
402 #endif
403 	}
404 
405 	return claimed;
406 }
407 
408 
409 static void
410 si_reset_adapter(struct ncr5380_softc *ncr_sc)
411 {
412 	struct si_softc *sc = (struct si_softc *)ncr_sc;
413 
414 #ifdef	DEBUG
415 	if (si_debug) {
416 		printf("%s\n", __func__);
417 	}
418 #endif
419 
420 	/*
421 	 * The SCSI3 controller has an 8K FIFO to buffer data between the
422 	 * 5380 and the DMA.  Make sure it starts out empty.
423 	 *
424 	 * The reset bits in the CSR are active low.
425 	 */
426 	SIREG_WRITE(ncr_sc, SIREG_CSR, 0);
427 	delay(10);
428 	SIREG_WRITE(ncr_sc, SIREG_CSR,
429 	    SI_CSR_FIFO_RES | SI_CSR_SCSI_RES | SI_CSR_INTR_EN);
430 	delay(10);
431 
432 	SIREG_WRITE(ncr_sc, SIREG_FIFO_CNT, 0);
433 	SIREG_WRITE(ncr_sc, SIREG_DMA_ADDRH, 0);
434 	SIREG_WRITE(ncr_sc, SIREG_DMA_ADDRL, 0);
435 	SIREG_WRITE(ncr_sc, SIREG_DMA_CNTH, 0);
436 	SIREG_WRITE(ncr_sc, SIREG_DMA_CNTL, 0);
437 	SIREG_WRITE(ncr_sc, SIREG_IV_AM, sc->sc_adapter_iv_am);
438 	SIREG_WRITE(ncr_sc, SIREG_FIFO_CNTH, 0);
439 
440 	SCI_CLR_INTR(ncr_sc);
441 }
442 
443 /*****************************************************************
444  * Common functions for DMA
445  ****************************************************************/
446 
447 /*
448  * Allocate a DMA handle and put it in sc->sc_dma.  Prepare
449  * for DMA transfer.
450  */
451 void
452 si_dma_alloc(struct ncr5380_softc *ncr_sc)
453 {
454 	struct si_softc *sc = (struct si_softc *)ncr_sc;
455 	struct sci_req *sr = ncr_sc->sc_current;
456 	struct scsipi_xfer *xs = sr->sr_xs;
457 	struct si_dma_handle *dh;
458 	int i, xlen;
459 	u_long addr;
460 
461 #ifdef DIAGNOSTIC
462 	if (sr->sr_dma_hand != NULL)
463 		panic("%s: already have DMA handle", __func__);
464 #endif
465 
466 #if 1	/* XXX - Temporary */
467 	/* XXX - In case we think DMA is completely broken... */
468 	if ((sc->sc_options & SI_ENABLE_DMA) == 0)
469 		return;
470 #endif
471 
472 	addr = (u_long)ncr_sc->sc_dataptr;
473 	xlen = ncr_sc->sc_datalen;
474 
475 	/* If the DMA start addr is misaligned then do PIO */
476 	if ((addr & 1) || (xlen & 1)) {
477 		printf("%s: misaligned.\n", __func__);
478 		return;
479 	}
480 
481 	/* Make sure our caller checked sc_min_dma_len. */
482 	if (xlen < MIN_DMA_LEN)
483 		panic("%s: xlen=0x%x", __func__, xlen);
484 
485 	/* Find free DMA handle.  Guaranteed to find one since we have
486 	   as many DMA handles as the driver has processes. */
487 	for (i = 0; i < SCI_OPENINGS; i++) {
488 		if ((sc->sc_dma[i].dh_flags & SIDH_BUSY) == 0)
489 			goto found;
490 	}
491 	panic("si: no free DMA handles.");
492 
493 found:
494 	dh = &sc->sc_dma[i];
495 	dh->dh_flags = SIDH_BUSY;
496 	dh->dh_maplen  = xlen;
497 
498 	/* Copy the "write" flag for convenience. */
499 	if ((xs->xs_control & XS_CTL_DATA_OUT) != 0)
500 		dh->dh_flags |= SIDH_OUT;
501 
502 	/*
503 	 * Double-map the buffer into DVMA space.  If we can't re-map
504 	 * the buffer, we print a warning and fall back to PIO mode.
505 	 *
506 	 * NOTE: it is not safe to sleep here!
507 	 */
508 	if (bus_dmamap_load(sc->sc_dmatag, dh->dh_dmamap,
509 			    (void *)addr, xlen, NULL, BUS_DMA_NOWAIT) != 0) {
510 		/* Can't remap segment */
511 		printf("%s: can't remap 0x%lx/0x%x, doing PIO\n",
512 		    __func__, addr, dh->dh_maplen);
513 		dh->dh_flags = 0;
514 		return;
515 	}
516 	bus_dmamap_sync(sc->sc_dmatag, dh->dh_dmamap, addr, xlen,
517 			(dh->dh_flags & SIDH_OUT)
518 				? BUS_DMASYNC_PREWRITE
519 				: BUS_DMASYNC_PREREAD);
520 
521 	/* success */
522 	sr->sr_dma_hand = dh;
523 }
524 
525 
526 void
527 si_dma_free(struct ncr5380_softc *ncr_sc)
528 {
529 	struct si_softc *sc = (struct si_softc *)ncr_sc;
530 	struct sci_req *sr = ncr_sc->sc_current;
531 	struct si_dma_handle *dh = sr->sr_dma_hand;
532 
533 #ifdef DIAGNOSTIC
534 	if (dh == NULL)
535 		panic("%s: no DMA handle", __func__);
536 #endif
537 
538 	if (ncr_sc->sc_state & NCR_DOINGDMA)
539 		panic("%s: free while in progress", __func__);
540 
541 	if (dh->dh_flags & SIDH_BUSY) {
542 		/* Give back the DVMA space. */
543 		bus_dmamap_sync(sc->sc_dmatag, dh->dh_dmamap,
544 				dh->dh_dvma, dh->dh_maplen,
545 				(dh->dh_flags & SIDH_OUT)
546 					? BUS_DMASYNC_POSTWRITE
547 					: BUS_DMASYNC_POSTREAD);
548 		bus_dmamap_unload(sc->sc_dmatag, dh->dh_dmamap);
549 		dh->dh_flags = 0;
550 	}
551 	sr->sr_dma_hand = NULL;
552 }
553 
554 
555 /*
556  * Poll (spin-wait) for DMA completion.
557  * Called right after xx_dma_start(), and
558  * xx_dma_stop() will be called next.
559  * Same for either VME or OBIO.
560  */
561 void
562 si_dma_poll(struct ncr5380_softc *ncr_sc)
563 {
564 	struct sci_req *sr = ncr_sc->sc_current;
565 	int tmo, csr_mask, csr;
566 
567 	/* Make sure DMA started successfully. */
568 	if (ncr_sc->sc_state & NCR_ABORTING)
569 		return;
570 
571 	csr_mask = SI_CSR_SBC_IP | SI_CSR_DMA_IP |
572 		SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR;
573 
574 	tmo = 50000;	/* X100 = 5 sec. */
575 	for (;;) {
576 		csr = SIREG_READ(ncr_sc, SIREG_CSR);
577 		if (csr & csr_mask)
578 			break;
579 		if (--tmo <= 0) {
580 			printf("%s: DMA timeout (while polling)\n",
581 			    device_xname(ncr_sc->sc_dev));
582 			/* Indicate timeout as MI code would. */
583 			sr->sr_flags |= SR_OVERDUE;
584 			break;
585 		}
586 		delay(100);
587 	}
588 
589 #ifdef	DEBUG
590 	if (si_debug) {
591 		printf("%s: done, csr=0x%x\n", __func__, csr);
592 	}
593 #endif
594 }
595 
596 
597 /*****************************************************************
598  * VME functions for DMA
599  ****************************************************************/
600 
601 
602 /*
603  * This is called when the bus is going idle,
604  * so we want to enable the SBC interrupts.
605  * That is controlled by the DMA enable!
606  * Who would have guessed!
607  * What a NASTY trick!
608  */
609 void
610 si_intr_on(struct ncr5380_softc *ncr_sc)
611 {
612 	uint16_t csr;
613 
614 	/* Clear DMA start address and counters */
615 	SIREG_WRITE(ncr_sc, SIREG_DMA_ADDRH, 0);
616 	SIREG_WRITE(ncr_sc, SIREG_DMA_ADDRL, 0);
617 	SIREG_WRITE(ncr_sc, SIREG_DMA_CNTH, 0);
618 	SIREG_WRITE(ncr_sc, SIREG_DMA_CNTL, 0);
619 
620 	/* Enter receive mode (for safety) and enable DMA engine */
621 	csr = SIREG_READ(ncr_sc, SIREG_CSR);
622 	csr &= ~SI_CSR_SEND;
623 	csr |= SI_CSR_DMA_EN;
624 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
625 }
626 
627 /*
628  * This is called when the bus is idle and we are
629  * about to start playing with the SBC chip.
630  */
631 void
632 si_intr_off(struct ncr5380_softc *ncr_sc)
633 {
634 	uint16_t csr;
635 
636 	csr = SIREG_READ(ncr_sc, SIREG_CSR);
637 	csr &= ~SI_CSR_DMA_EN;
638 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
639 }
640 
641 /*
642  * This function is called during the COMMAND or MSG_IN phase
643  * that precedes a DATA_IN or DATA_OUT phase, in case we need
644  * to setup the DMA engine before the bus enters a DATA phase.
645  *
646  * XXX: The VME adapter appears to suppress SBC interrupts
647  * when the FIFO is not empty or the FIFO count is non-zero!
648  *
649  * On the VME version we just clear the DMA count and address
650  * here (to make sure it stays idle) and do the real setup
651  * later, in dma_start.
652  */
653 void
654 si_dma_setup(struct ncr5380_softc *ncr_sc)
655 {
656 	struct si_softc *sc = (struct si_softc *)ncr_sc;
657 	struct sci_req *sr = ncr_sc->sc_current;
658 	struct si_dma_handle *dh = sr->sr_dma_hand;
659 	uint16_t csr;
660 	u_long dva;
661 	int xlen;
662 
663 	/*
664 	 * Set up the DMA controller.
665 	 * Note that (dh->dh_len < sc_datalen)
666 	 */
667 
668 	csr = SIREG_READ(ncr_sc, SIREG_CSR);
669 
670 	/* Disable DMA while we're setting up the transfer */
671 	csr &= ~SI_CSR_DMA_EN;
672 
673 	/* Reset the FIFO */
674 	csr &= ~SI_CSR_FIFO_RES;		/* active low */
675 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
676 	csr |= SI_CSR_FIFO_RES;
677 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
678 
679 	/*
680 	 * Get the DVMA mapping for this segment.
681 	 */
682 	dva = (u_long)(dh->dh_dvma);
683 	if (dva & 1)
684 		panic("%s: bad dmaaddr=0x%lx", __func__, dva);
685 	xlen = ncr_sc->sc_datalen;
686 	xlen &= ~1;
687 	sc->sc_xlen = xlen;	/* XXX: or less... */
688 
689 #ifdef	DEBUG
690 	if (si_debug & 2) {
691 		printf("%s: dh=%p, dmaaddr=0x%lx, xlen=%d\n",
692 		    __func__, dh, dva, xlen);
693 	}
694 #endif
695 	/* Set direction (send/recv) */
696 	if (dh->dh_flags & SIDH_OUT) {
697 		csr |= SI_CSR_SEND;
698 	} else {
699 		csr &= ~SI_CSR_SEND;
700 	}
701 
702 	/* Set byte-packing control */
703 	if (dva & 2) {
704 		csr |= SI_CSR_BPCON;
705 	} else {
706 		csr &= ~SI_CSR_BPCON;
707 	}
708 
709 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
710 
711 	/* Load start address */
712 	SIREG_WRITE(ncr_sc, SIREG_DMA_ADDRH, (uint16_t)(dva >> 16));
713 	SIREG_WRITE(ncr_sc, SIREG_DMA_ADDRL, (uint16_t)(dva & 0xFFFF));
714 
715 	/* Clear DMA counters; these will be set in si_dma_start() */
716 	SIREG_WRITE(ncr_sc, SIREG_DMA_CNTH, 0);
717 	SIREG_WRITE(ncr_sc, SIREG_DMA_CNTL, 0);
718 
719 	/* Clear FIFO counter. (also hits dma_count) */
720 	SIREG_WRITE(ncr_sc, SIREG_FIFO_CNTH, 0);
721 	SIREG_WRITE(ncr_sc, SIREG_FIFO_CNT, 0);
722 }
723 
724 
725 void
726 si_dma_start(struct ncr5380_softc *ncr_sc)
727 {
728 	struct si_softc *sc = (struct si_softc *)ncr_sc;
729 	struct sci_req *sr = ncr_sc->sc_current;
730 	struct si_dma_handle *dh = sr->sr_dma_hand;
731 	int xlen;
732 	u_int mode;
733 	uint16_t csr;
734 
735 	xlen = sc->sc_xlen;
736 
737 	/* Load transfer length */
738 	SIREG_WRITE(ncr_sc, SIREG_DMA_CNTH, (uint16_t)(xlen >> 16));
739 	SIREG_WRITE(ncr_sc, SIREG_DMA_CNTL, (uint16_t)(xlen & 0xFFFF));
740 	SIREG_WRITE(ncr_sc, SIREG_FIFO_CNTH, (uint16_t)(xlen >> 16));
741 	SIREG_WRITE(ncr_sc, SIREG_FIFO_CNT, (uint16_t)(xlen & 0xFFFF));
742 
743 	/*
744 	 * Acknowledge the phase change.  (After DMA setup!)
745 	 * Put the SBIC into DMA mode, and start the transfer.
746 	 */
747 	if (dh->dh_flags & SIDH_OUT) {
748 		NCR5380_WRITE(ncr_sc, sci_tcmd, PHASE_DATA_OUT);
749 		SCI_CLR_INTR(ncr_sc);
750 		NCR5380_WRITE(ncr_sc, sci_icmd, SCI_ICMD_DATA);
751 
752 		mode = NCR5380_READ(ncr_sc, sci_mode);
753 		mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
754 		NCR5380_WRITE(ncr_sc, sci_mode, mode);
755 
756 		NCR5380_WRITE(ncr_sc, sci_dma_send, 0); /* start it */
757 	} else {
758 		NCR5380_WRITE(ncr_sc, sci_tcmd, PHASE_DATA_IN);
759 		SCI_CLR_INTR(ncr_sc);
760 		NCR5380_WRITE(ncr_sc, sci_icmd, 0);
761 
762 		mode = NCR5380_READ(ncr_sc, sci_mode);
763 		mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
764 		NCR5380_WRITE(ncr_sc, sci_mode, mode);
765 
766 		NCR5380_WRITE(ncr_sc, sci_irecv, 0); /* start it */
767 	}
768 
769 	ncr_sc->sc_state |= NCR_DOINGDMA;
770 
771 	/* Enable DMA engine */
772 	csr = SIREG_READ(ncr_sc, SIREG_CSR);
773 	csr |= SI_CSR_DMA_EN;
774 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
775 
776 #ifdef	DEBUG
777 	if (si_debug & 2) {
778 		printf("%s: started, flags=0x%x\n",
779 		    __func__, ncr_sc->sc_state);
780 	}
781 #endif
782 }
783 
784 
785 void
786 si_dma_eop(struct ncr5380_softc *ncr_sc)
787 {
788 
789 	/* Not needed - DMA was stopped prior to examining sci_csr */
790 }
791 
792 
793 void
794 si_dma_stop(struct ncr5380_softc *ncr_sc)
795 {
796 	struct si_softc *sc = (struct si_softc *)ncr_sc;
797 	struct sci_req *sr = ncr_sc->sc_current;
798 	struct si_dma_handle *dh = sr->sr_dma_hand;
799 	int resid, ntrans;
800 	uint16_t csr;
801 	u_int mode;
802 
803 	if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
804 #ifdef	DEBUG
805 		printf("%s: DMA not running\n", __func__);
806 #endif
807 		return;
808 	}
809 
810 	ncr_sc->sc_state &= ~NCR_DOINGDMA;
811 
812 	csr = SIREG_READ(ncr_sc, SIREG_CSR);
813 
814 	/* First, halt the DMA engine. */
815 	csr &= ~SI_CSR_DMA_EN;
816 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
817 
818 	if (csr & (SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR)) {
819 		printf("si: DMA error, csr=0x%x, reset\n", csr);
820 		sr->sr_xs->error = XS_DRIVER_STUFFUP;
821 		ncr_sc->sc_state |= NCR_ABORTING;
822 		si_reset_adapter(ncr_sc);
823 	}
824 
825 	/* Note that timeout may have set the error flag. */
826 	if (ncr_sc->sc_state & NCR_ABORTING)
827 		goto out;
828 
829 	/*
830 	 * Now try to figure out how much actually transferred
831 	 *
832 	 * The fifo_count does not reflect how many bytes were
833 	 * actually transferred for VME.
834 	 *
835 	 * SCSI-3 VME interface is a little funny on writes:
836 	 * if we have a disconnect, the DMA has overshot by
837 	 * one byte and the resid needs to be incremented.
838 	 * Only happens for partial transfers.
839 	 * (Thanks to Matt Jacob)
840 	 */
841 
842 	resid = SIREG_READ(ncr_sc, SIREG_FIFO_CNTH) << 16;
843 	resid |= SIREG_READ(ncr_sc, SIREG_FIFO_CNT) & 0xFFFF;
844 	if (dh->dh_flags & SIDH_OUT)
845 		if ((resid > 0) && (resid < sc->sc_xlen))
846 			resid++;
847 	ntrans = sc->sc_xlen - resid;
848 
849 #ifdef	DEBUG
850 	if (si_debug & 2) {
851 		printf("%s: resid=0x%x ntrans=0x%x\n",
852 		    __func__, resid, ntrans);
853 	}
854 #endif
855 
856 	if (ntrans > ncr_sc->sc_datalen)
857 		panic("%s: excess transfer", __func__);
858 
859 	/* Adjust data pointer */
860 	ncr_sc->sc_dataptr += ntrans;
861 	ncr_sc->sc_datalen -= ntrans;
862 
863 #ifdef	DEBUG
864 	if (si_debug & 2) {
865 		printf("%s: ntrans=0x%x\n", __func__, ntrans);
866 	}
867 #endif
868 
869 	/*
870 	 * After a read, we may need to clean-up
871 	 * "Left-over bytes" (yuck!)
872 	 */
873 	if (((dh->dh_flags & SIDH_OUT) == 0) &&
874 		((csr & SI_CSR_LOB) != 0)) {
875 		uint8_t *cp = ncr_sc->sc_dataptr;
876 		uint16_t bprh, bprl;
877 
878 		bprh = SIREG_READ(ncr_sc, SIREG_BPRH);
879 		bprl = SIREG_READ(ncr_sc, SIREG_BPRL);
880 
881 #ifdef DEBUG
882 		printf("si: got left-over bytes: bprh=%x, bprl=%x, csr=%x\n",
883 			bprh, bprl, csr);
884 #endif
885 
886 		if (csr & SI_CSR_BPCON) {
887 			/* have SI_CSR_BPCON */
888 			cp[-1] = (bprl & 0xff00) >> 8;
889 		} else {
890 			switch (csr & SI_CSR_LOB) {
891 			case SI_CSR_LOB_THREE:
892 				cp[-3] = (bprh & 0xff00) >> 8;
893 				cp[-2] = (bprh & 0x00ff);
894 				cp[-1] = (bprl & 0xff00) >> 8;
895 				break;
896 			case SI_CSR_LOB_TWO:
897 				cp[-2] = (bprh & 0xff00) >> 8;
898 				cp[-1] = (bprh & 0x00ff);
899 				break;
900 			case SI_CSR_LOB_ONE:
901 				cp[-1] = (bprh & 0xff00) >> 8;
902 				break;
903 			}
904 		}
905 	}
906 
907 out:
908 	SIREG_WRITE(ncr_sc, SIREG_DMA_ADDRH, 0);
909 	SIREG_WRITE(ncr_sc, SIREG_DMA_ADDRL, 0);
910 
911 	SIREG_WRITE(ncr_sc, SIREG_DMA_CNTH, 0);
912 	SIREG_WRITE(ncr_sc, SIREG_DMA_CNTL, 0);
913 
914 	SIREG_WRITE(ncr_sc, SIREG_FIFO_CNTH, 0);
915 	SIREG_WRITE(ncr_sc, SIREG_FIFO_CNT, 0);
916 
917 	mode = NCR5380_READ(ncr_sc, sci_mode);
918 	/* Put SBIC back in PIO mode. */
919 	mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE);
920 	NCR5380_WRITE(ncr_sc, sci_mode, mode);
921 	NCR5380_WRITE(ncr_sc, sci_icmd, 0);
922 }
923