Home | History | Annotate | Line # | Download | only in dev
sw.c revision 1.19.16.1
      1  1.19.16.1       mjf /*	$NetBSD: sw.c,v 1.19.16.1 2008/06/02 13:22:40 mjf Exp $	*/
      2        1.1        pk 
      3        1.1        pk /*-
      4        1.1        pk  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5        1.1        pk  * All rights reserved.
      6        1.1        pk  *
      7        1.1        pk  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1        pk  * by Adam Glass, David Jones, Gordon W. Ross, and Jason R. Thorpe.
      9        1.1        pk  *
     10        1.1        pk  * Redistribution and use in source and binary forms, with or without
     11        1.1        pk  * modification, are permitted provided that the following conditions
     12        1.1        pk  * are met:
     13        1.1        pk  * 1. Redistributions of source code must retain the above copyright
     14        1.1        pk  *    notice, this list of conditions and the following disclaimer.
     15        1.1        pk  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1        pk  *    notice, this list of conditions and the following disclaimer in the
     17        1.1        pk  *    documentation and/or other materials provided with the distribution.
     18        1.1        pk  *
     19        1.1        pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.1        pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.1        pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.1        pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.1        pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.1        pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.1        pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.1        pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.1        pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.1        pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.1        pk  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1        pk  */
     31        1.1        pk 
     32        1.1        pk /*
     33        1.1        pk  * This file contains only the machine-dependent parts of the
     34        1.1        pk  * Sun4 SCSI driver.  (Autoconfig stuff and DMA functions.)
     35        1.1        pk  * The machine-independent parts are in ncr5380sbc.c
     36        1.1        pk  *
     37        1.1        pk  * Supported hardware includes:
     38        1.1        pk  * Sun "SCSI Weird" on OBIO (sw: Sun 4/100-series)
     39        1.1        pk  * Sun SCSI-3 on VME (si: Sun 4/200-series, others)
     40        1.1        pk  *
     41        1.1        pk  * The VME variant has a bit to enable or disable the DMA engine,
     42        1.1        pk  * but that bit also gates the interrupt line from the NCR5380!
     43        1.1        pk  * Therefore, in order to get any interrupt from the 5380, (i.e.
     44        1.1        pk  * for reselect) one must clear the DMA engine transfer count and
     45        1.1        pk  * then enable DMA.  This has the further complication that you
     46        1.1        pk  * CAN NOT touch the NCR5380 while the DMA enable bit is set, so
     47        1.1        pk  * we have to turn DMA back off before we even look at the 5380.
     48        1.1        pk  *
     49        1.1        pk  * What wonderfully whacky hardware this is!
     50        1.1        pk  *
     51        1.1        pk  * David Jones wrote the initial version of this module for NetBSD/sun3,
     52        1.1        pk  * which included support for the VME adapter only. (no reselection).
     53        1.1        pk  *
     54        1.1        pk  * Gordon Ross added support for the Sun 3 OBIO adapter, and re-worked
     55        1.1        pk  * both the VME and OBIO code to support disconnect/reselect.
     56        1.1        pk  * (Required figuring out the hardware "features" noted above.)
     57        1.1        pk  *
     58        1.1        pk  * The autoconfiguration boilerplate came from Adam Glass.
     59        1.1        pk  *
     60        1.1        pk  * Jason R. Thorpe ported the autoconfiguration and VME portions to
     61        1.1        pk  * NetBSD/sparc, and added initial support for the 4/100 "SCSI Weird",
     62        1.1        pk  * a wacky OBIO variant of the VME SCSI-3.  Many thanks to Chuck Cranor
     63        1.1        pk  * for lots of helpful tips and suggestions.  Thanks also to Paul Kranenburg
     64        1.1        pk  * and Chris Torek for bits of insight needed along the way.  Thanks to
     65        1.1        pk  * David Gilbert and Andrew Gillham who risked filesystem life-and-limb
     66        1.1        pk  * for the sake of testing.  Andrew Gillham helped work out the bugs
     67        1.1        pk  * the 4/100 DMA code.
     68        1.1        pk  */
     69        1.1        pk 
     70        1.1        pk /*
     71        1.1        pk  * NOTE: support for the 4/100 "SCSI Weird" is not complete!  DMA
     72        1.1        pk  * works, but interrupts (and, thus, reselection) don't.  I don't know
     73        1.1        pk  * why, and I don't have a machine to test this on further.
     74        1.1        pk  *
     75        1.1        pk  * DMA, DMA completion interrupts, and reselection work fine on my
     76        1.1        pk  * 4/260 with modern SCSI-II disks attached.  I've had reports of
     77        1.1        pk  * reselection failing on Sun Shoebox-type configurations where
     78        1.1        pk  * there are multiple non-SCSI devices behind Emulex or Adaptec
     79        1.1        pk  * bridges.  These devices pre-date the SCSI-I spec, and might not
     80       1.19   jnemeth  * behave the way the 5380 code expects.  For this reason, only
     81        1.1        pk  * DMA is enabled by default in this driver.
     82        1.1        pk  *
     83       1.14    keihan  *	Jason R. Thorpe <thorpej (at) NetBSD.org>
     84        1.1        pk  *	December 8, 1995
     85        1.1        pk  */
     86       1.13     lukem 
     87       1.13     lukem #include <sys/cdefs.h>
     88  1.19.16.1       mjf __KERNEL_RCSID(0, "$NetBSD: sw.c,v 1.19.16.1 2008/06/02 13:22:40 mjf Exp $");
     89        1.1        pk 
     90        1.1        pk #include "opt_ddb.h"
     91        1.1        pk 
     92        1.1        pk #include <sys/types.h>
     93        1.1        pk #include <sys/param.h>
     94        1.1        pk #include <sys/systm.h>
     95        1.1        pk #include <sys/kernel.h>
     96        1.1        pk #include <sys/malloc.h>
     97        1.1        pk #include <sys/errno.h>
     98        1.1        pk #include <sys/device.h>
     99        1.1        pk #include <sys/buf.h>
    100        1.1        pk 
    101        1.1        pk #include <machine/bus.h>
    102        1.3        pk #include <machine/intr.h>
    103        1.1        pk #include <machine/autoconf.h>
    104        1.1        pk 
    105        1.1        pk #include <dev/scsipi/scsi_all.h>
    106        1.1        pk #include <dev/scsipi/scsipi_all.h>
    107        1.1        pk #include <dev/scsipi/scsipi_debug.h>
    108        1.1        pk #include <dev/scsipi/scsiconf.h>
    109        1.1        pk 
    110        1.1        pk #ifndef DDB
    111        1.1        pk #define	Debugger()
    112        1.1        pk #endif
    113        1.1        pk 
    114        1.1        pk #ifndef DEBUG
    115        1.1        pk #define DEBUG XXX
    116        1.1        pk #endif
    117        1.1        pk 
    118        1.1        pk #define COUNT_SW_LEFTOVERS	XXX	/* See sw DMA completion code */
    119        1.1        pk 
    120        1.1        pk #include <dev/ic/ncr5380reg.h>
    121        1.1        pk #include <dev/ic/ncr5380var.h>
    122        1.1        pk 
    123        1.1        pk #include <sparc/dev/swreg.h>
    124        1.1        pk 
    125        1.1        pk /*
    126        1.1        pk  * Transfers smaller than this are done using PIO
    127        1.1        pk  * (on assumption they're not worth DMA overhead)
    128        1.1        pk  */
    129        1.1        pk #define	MIN_DMA_LEN 128
    130        1.1        pk 
    131        1.1        pk /*
    132        1.1        pk  * Transfers lager than 65535 bytes need to be split-up.
    133        1.1        pk  * (Some of the FIFO logic has only 16 bits counters.)
    134        1.1        pk  * Make the size an integer multiple of the page size
    135        1.1        pk  * to avoid buf/cluster remap problems.  (paranoid?)
    136        1.1        pk  */
    137        1.1        pk #define	MAX_DMA_LEN 0xE000
    138        1.1        pk 
    139        1.1        pk #ifdef	DEBUG
    140        1.1        pk int sw_debug = 0;
    141        1.1        pk #endif
    142        1.1        pk 
    143        1.1        pk /*
    144        1.1        pk  * This structure is used to keep track of mapped DMA requests.
    145        1.1        pk  */
    146        1.1        pk struct sw_dma_handle {
    147        1.1        pk 	int 		dh_flags;
    148        1.1        pk #define	SIDH_BUSY	0x01		/* This DH is in use */
    149        1.1        pk #define	SIDH_OUT	0x02		/* DMA does data out (write) */
    150        1.1        pk 	u_char		*dh_addr;	/* KVA of start of buffer */
    151        1.1        pk 	int 		dh_maplen;	/* Original data length */
    152        1.1        pk 	long		dh_startingpa;	/* PA of buffer; for "sw" */
    153        1.1        pk 	bus_dmamap_t	dh_dmamap;
    154        1.1        pk #define dh_dvma	dh_dmamap->dm_segs[0].ds_addr /* VA of buffer in DVMA space */
    155        1.1        pk };
    156        1.1        pk 
    157        1.1        pk /*
    158        1.1        pk  * The first structure member has to be the ncr5380_softc
    159        1.1        pk  * so we can just cast to go back and fourth between them.
    160        1.1        pk  */
    161        1.1        pk struct sw_softc {
    162        1.1        pk 	struct ncr5380_softc	ncr_sc;
    163        1.1        pk 	bus_space_tag_t		sc_bustag;	/* bus tags */
    164        1.1        pk 	bus_dma_tag_t		sc_dmatag;
    165        1.1        pk 
    166        1.1        pk 	struct sw_dma_handle *sc_dma;
    167        1.1        pk 	int		sc_xlen;	/* length of current DMA segment. */
    168        1.1        pk 	int		sc_options;	/* options for this instance. */
    169        1.1        pk };
    170        1.1        pk 
    171        1.1        pk /*
    172        1.1        pk  * Options.  By default, DMA is enabled and DMA completion interrupts
    173        1.1        pk  * and reselect are disabled.  You may enable additional features
    174        1.1        pk  * the `flags' directive in your kernel's configuration file.
    175        1.1        pk  *
    176        1.1        pk  * Alternatively, you can patch your kernel with DDB or some other
    177        1.1        pk  * mechanism.  The sc_options member of the softc is OR'd with
    178        1.1        pk  * the value in sw_options.
    179        1.1        pk  *
    180        1.1        pk  * On the "sw", interrupts (and thus) reselection don't work, so they're
    181        1.1        pk  * disabled by default.  DMA is still a little dangerous, too.
    182        1.1        pk  *
    183        1.1        pk  * Note, there's a separate sw_options to make life easier.
    184        1.1        pk  */
    185        1.1        pk #define	SW_ENABLE_DMA	0x01	/* Use DMA (maybe polled) */
    186        1.1        pk #define	SW_DMA_INTR	0x02	/* DMA completion interrupts */
    187        1.1        pk #define	SW_DO_RESELECT	0x04	/* Allow disconnect/reselect */
    188        1.1        pk #define	SW_OPTIONS_MASK	(SW_ENABLE_DMA|SW_DMA_INTR|SW_DO_RESELECT)
    189        1.1        pk #define SW_OPTIONS_BITS	"\10\3RESELECT\2DMA_INTR\1DMA"
    190        1.1        pk int sw_options = SW_ENABLE_DMA;
    191        1.1        pk 
    192  1.19.16.1       mjf static int	sw_match(device_t, cfdata_t, void *);
    193  1.19.16.1       mjf static void	sw_attach(device_t, device_t, void *);
    194       1.15       uwe static int	sw_intr(void *);
    195       1.15       uwe static void	sw_reset_adapter(struct ncr5380_softc *);
    196       1.15       uwe static void	sw_minphys(struct buf *);
    197       1.15       uwe 
    198       1.15       uwe void	sw_dma_alloc(struct ncr5380_softc *);
    199       1.15       uwe void	sw_dma_free(struct ncr5380_softc *);
    200       1.15       uwe void	sw_dma_poll(struct ncr5380_softc *);
    201       1.15       uwe 
    202       1.15       uwe void	sw_dma_setup(struct ncr5380_softc *);
    203       1.15       uwe void	sw_dma_start(struct ncr5380_softc *);
    204       1.15       uwe void	sw_dma_eop(struct ncr5380_softc *);
    205       1.15       uwe void	sw_dma_stop(struct ncr5380_softc *);
    206        1.1        pk 
    207       1.15       uwe void	sw_intr_on(struct ncr5380_softc *);
    208       1.15       uwe void	sw_intr_off(struct ncr5380_softc *);
    209        1.1        pk 
    210        1.1        pk /* Shorthand bus space access */
    211        1.1        pk #define SWREG_READ(sc, index) \
    212        1.1        pk 	bus_space_read_4((sc)->sc_regt, (sc)->sc_regh, index)
    213        1.1        pk #define SWREG_WRITE(sc, index, v) \
    214        1.1        pk 	bus_space_write_4((sc)->sc_regt, (sc)->sc_regh, index, v)
    215        1.1        pk 
    216        1.1        pk 
    217        1.1        pk /* The Sun "SCSI Weird" 4/100 obio controller. */
    218  1.19.16.1       mjf CFATTACH_DECL_NEW(sw, sizeof(struct sw_softc),
    219       1.10   thorpej     sw_match, sw_attach, NULL, NULL);
    220        1.1        pk 
    221        1.1        pk static int
    222  1.19.16.1       mjf sw_match(device_t parent, cfdata_t cf, void *aux)
    223        1.1        pk {
    224        1.1        pk 	union obio_attach_args *uoba = aux;
    225        1.1        pk 	struct obio4_attach_args *oba;
    226        1.1        pk 
    227        1.1        pk 	/* Nothing but a Sun 4/100 is going to have these devices. */
    228        1.1        pk 	if (cpuinfo.cpu_type != CPUTYP_4_100)
    229        1.1        pk 		return (0);
    230        1.1        pk 
    231        1.1        pk 	if (uoba->uoba_isobio4 == 0)
    232        1.1        pk 		return (0);
    233        1.1        pk 
    234        1.1        pk 	/* Make sure there is something there... */
    235        1.1        pk 	oba = &uoba->uoba_oba4;
    236        1.6        pk 	return (bus_space_probe(oba->oba_bustag, oba->oba_paddr,
    237        1.1        pk 				1,	/* probe size */
    238        1.1        pk 				1,	/* offset */
    239        1.1        pk 				0,	/* flags */
    240        1.1        pk 				NULL, NULL));
    241        1.1        pk }
    242        1.1        pk 
    243        1.1        pk static void
    244  1.19.16.1       mjf sw_attach(device_t parent, device_t self, void *aux)
    245        1.1        pk {
    246  1.19.16.1       mjf 	struct sw_softc *sc = device_private(self);
    247        1.1        pk 	struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
    248        1.1        pk 	union obio_attach_args *uoba = aux;
    249        1.1        pk 	struct obio4_attach_args *oba = &uoba->uoba_oba4;
    250        1.1        pk 	bus_space_handle_t bh;
    251        1.1        pk 	char bits[64];
    252        1.1        pk 	int i;
    253        1.1        pk 
    254  1.19.16.1       mjf 	ncr_sc->sc_dev = self;
    255        1.1        pk 	sc->sc_dmatag = oba->oba_dmatag;
    256        1.1        pk 
    257        1.1        pk 	/* Map the controller registers. */
    258        1.6        pk 	if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
    259        1.6        pk 			  SWREG_BANK_SZ,
    260        1.6        pk 			  BUS_SPACE_MAP_LINEAR,
    261        1.6        pk 			  &bh) != 0) {
    262  1.19.16.1       mjf 		aprint_error(": cannot map registers\n");
    263        1.1        pk 		return;
    264        1.1        pk 	}
    265        1.1        pk 
    266        1.1        pk 	ncr_sc->sc_regt = oba->oba_bustag;
    267        1.1        pk 	ncr_sc->sc_regh = bh;
    268        1.1        pk 
    269        1.1        pk 	sc->sc_options = sw_options;
    270        1.1        pk 
    271        1.1        pk 	ncr_sc->sc_dma_setup = sw_dma_setup;
    272        1.1        pk 	ncr_sc->sc_dma_start = sw_dma_start;
    273        1.1        pk 	ncr_sc->sc_dma_eop   = sw_dma_stop;
    274        1.1        pk 	ncr_sc->sc_dma_stop  = sw_dma_stop;
    275        1.1        pk 	ncr_sc->sc_intr_on   = sw_intr_on;
    276        1.1        pk 	ncr_sc->sc_intr_off  = sw_intr_off;
    277        1.1        pk 
    278        1.1        pk 	/*
    279        1.1        pk 	 * Establish interrupt channel.
    280        1.1        pk 	 * Default interrupt priority always is 3.  At least, that's
    281        1.1        pk 	 * what my board seems to be at.  --thorpej
    282        1.1        pk 	 */
    283        1.1        pk 	if (oba->oba_pri == -1)
    284        1.1        pk 		oba->oba_pri = 3;
    285        1.1        pk 
    286       1.11        pk 	(void)bus_intr_establish(oba->oba_bustag, oba->oba_pri, IPL_BIO,
    287        1.1        pk 				 sw_intr, sc);
    288        1.1        pk 
    289  1.19.16.1       mjf 	aprint_normal(" pri %d\n", oba->oba_pri);
    290        1.1        pk 
    291        1.1        pk 
    292        1.1        pk 	/*
    293        1.1        pk 	 * Pull in the options flags.  Allow the user to completely
    294        1.1        pk 	 * override the default values.
    295        1.1        pk 	 */
    296  1.19.16.1       mjf 	if ((device_cfdata(self)->cf_flags & SW_OPTIONS_MASK) != 0)
    297        1.1        pk 		sc->sc_options =
    298  1.19.16.1       mjf 		    device_cfdata(self)->cf_flags & SW_OPTIONS_MASK;
    299        1.1        pk 
    300        1.1        pk 	/*
    301        1.1        pk 	 * Initialize fields used by the MI code
    302        1.1        pk 	 */
    303        1.1        pk 
    304        1.1        pk 	/* NCR5380 register bank offsets */
    305        1.1        pk 	ncr_sc->sci_r0 = 0;
    306        1.1        pk 	ncr_sc->sci_r1 = 1;
    307        1.1        pk 	ncr_sc->sci_r2 = 2;
    308        1.1        pk 	ncr_sc->sci_r3 = 3;
    309        1.1        pk 	ncr_sc->sci_r4 = 4;
    310        1.1        pk 	ncr_sc->sci_r5 = 5;
    311        1.1        pk 	ncr_sc->sci_r6 = 6;
    312        1.1        pk 	ncr_sc->sci_r7 = 7;
    313        1.1        pk 
    314        1.1        pk 	ncr_sc->sc_rev = NCR_VARIANT_NCR5380;
    315        1.1        pk 
    316        1.1        pk 	/*
    317        1.1        pk 	 * MD function pointers used by the MI code.
    318        1.1        pk 	 */
    319        1.1        pk 	ncr_sc->sc_pio_out = ncr5380_pio_out;
    320        1.1        pk 	ncr_sc->sc_pio_in =  ncr5380_pio_in;
    321        1.1        pk 	ncr_sc->sc_dma_alloc = sw_dma_alloc;
    322        1.1        pk 	ncr_sc->sc_dma_free  = sw_dma_free;
    323        1.1        pk 	ncr_sc->sc_dma_poll  = sw_dma_poll;
    324        1.1        pk 
    325        1.1        pk 	ncr_sc->sc_flags = 0;
    326        1.1        pk 	if ((sc->sc_options & SW_DO_RESELECT) == 0)
    327        1.1        pk 		ncr_sc->sc_no_disconnect = 0xFF;
    328        1.1        pk 	if ((sc->sc_options & SW_DMA_INTR) == 0)
    329        1.1        pk 		ncr_sc->sc_flags |= NCR5380_FORCE_POLLING;
    330        1.1        pk 	ncr_sc->sc_min_dma_len = MIN_DMA_LEN;
    331        1.1        pk 
    332        1.1        pk 
    333        1.1        pk 	/*
    334        1.1        pk 	 * Allocate DMA handles.
    335        1.1        pk 	 */
    336        1.1        pk 	i = SCI_OPENINGS * sizeof(struct sw_dma_handle);
    337        1.1        pk 	sc->sc_dma = (struct sw_dma_handle *)malloc(i, M_DEVBUF, M_NOWAIT);
    338        1.1        pk 	if (sc->sc_dma == NULL)
    339       1.12       wiz 		panic("sw: DMA handle malloc failed");
    340        1.1        pk 
    341        1.1        pk 	for (i = 0; i < SCI_OPENINGS; i++) {
    342        1.1        pk 		sc->sc_dma[i].dh_flags = 0;
    343        1.1        pk 
    344        1.1        pk 		/* Allocate a DMA handle */
    345        1.1        pk 		if (bus_dmamap_create(
    346        1.1        pk 				sc->sc_dmatag,	/* tag */
    347        1.1        pk 				MAXPHYS,	/* size */
    348        1.1        pk 				1,		/* nsegments */
    349        1.1        pk 				MAXPHYS,	/* maxsegsz */
    350        1.1        pk 				0,		/* boundary */
    351        1.1        pk 				BUS_DMA_NOWAIT,
    352        1.1        pk 				&sc->sc_dma[i].dh_dmamap) != 0) {
    353        1.1        pk 
    354  1.19.16.1       mjf 			aprint_error_dev(self, "DMA buffer map create error\n");
    355        1.1        pk 			return;
    356        1.1        pk 		}
    357        1.1        pk 	}
    358        1.1        pk 
    359        1.1        pk 	if (sc->sc_options) {
    360  1.19.16.1       mjf 		aprint_normal_dev(self, "options=%s\n",
    361  1.19.16.1       mjf 		    bitmask_snprintf(sc->sc_options, SW_OPTIONS_BITS,
    362  1.19.16.1       mjf 		    bits, sizeof(bits)));
    363        1.1        pk 	}
    364        1.1        pk 
    365        1.4    bouyer 	ncr_sc->sc_channel.chan_id = 7;
    366        1.4    bouyer 	ncr_sc->sc_adapter.adapt_minphys = sw_minphys;
    367        1.1        pk 
    368        1.1        pk 	/* Initialize sw board */
    369        1.1        pk 	sw_reset_adapter(ncr_sc);
    370        1.1        pk 
    371        1.1        pk 	/* Attach the ncr5380 chip driver */
    372        1.1        pk 	ncr5380_attach(ncr_sc);
    373        1.1        pk }
    374        1.1        pk 
    375        1.1        pk static void
    376        1.1        pk sw_minphys(struct buf *bp)
    377        1.1        pk {
    378  1.19.16.1       mjf 
    379        1.1        pk 	if (bp->b_bcount > MAX_DMA_LEN) {
    380        1.1        pk #ifdef DEBUG
    381        1.1        pk 		if (sw_debug) {
    382        1.1        pk 			printf("sw_minphys len = 0x%x.\n", MAX_DMA_LEN);
    383        1.1        pk 			Debugger();
    384        1.1        pk 		}
    385        1.1        pk #endif
    386        1.1        pk 		bp->b_bcount = MAX_DMA_LEN;
    387        1.1        pk 	}
    388        1.4    bouyer 	minphys(bp);
    389        1.1        pk }
    390        1.1        pk 
    391        1.1        pk #define CSR_WANT (SW_CSR_SBC_IP | SW_CSR_DMA_IP | \
    392        1.1        pk 	SW_CSR_DMA_CONFLICT | SW_CSR_DMA_BUS_ERR )
    393        1.1        pk 
    394        1.1        pk static int
    395        1.1        pk sw_intr(void *arg)
    396        1.1        pk {
    397        1.1        pk 	struct sw_softc *sc = arg;
    398  1.19.16.1       mjf 	struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
    399        1.1        pk 	int dma_error, claimed;
    400        1.1        pk 	u_short csr;
    401        1.1        pk 
    402        1.1        pk 	claimed = 0;
    403        1.1        pk 	dma_error = 0;
    404        1.1        pk 
    405        1.1        pk 	/* SBC interrupt? DMA interrupt? */
    406        1.1        pk 	csr = SWREG_READ(ncr_sc, SWREG_CSR);
    407        1.1        pk 
    408        1.1        pk 	NCR_TRACE("sw_intr: csr=0x%x\n", csr);
    409        1.1        pk 
    410        1.1        pk 	if (csr & SW_CSR_DMA_CONFLICT) {
    411        1.1        pk 		dma_error |= SW_CSR_DMA_CONFLICT;
    412  1.19.16.1       mjf 		printf("%s: DMA conflict\n", __func__);
    413        1.1        pk 	}
    414        1.1        pk 	if (csr & SW_CSR_DMA_BUS_ERR) {
    415        1.1        pk 		dma_error |= SW_CSR_DMA_BUS_ERR;
    416  1.19.16.1       mjf 		printf("%s: DMA bus error\n", __func__);
    417        1.1        pk 	}
    418        1.1        pk 	if (dma_error) {
    419        1.1        pk 		if (sc->ncr_sc.sc_state & NCR_DOINGDMA)
    420        1.1        pk 			sc->ncr_sc.sc_state |= NCR_ABORTING;
    421        1.1        pk 		/* Make sure we will call the main isr. */
    422        1.1        pk 		csr |= SW_CSR_DMA_IP;
    423        1.1        pk 	}
    424        1.1        pk 
    425        1.1        pk 	if (csr & (SW_CSR_SBC_IP | SW_CSR_DMA_IP)) {
    426        1.1        pk 		claimed = ncr5380_intr(&sc->ncr_sc);
    427        1.1        pk #ifdef DEBUG
    428        1.1        pk 		if (!claimed) {
    429  1.19.16.1       mjf 			printf("%s: spurious from SBC\n", __func__);
    430        1.1        pk 			if (sw_debug & 4) {
    431        1.1        pk 				Debugger();	/* XXX */
    432        1.1        pk 			}
    433        1.1        pk 		}
    434        1.1        pk #endif
    435        1.1        pk 	}
    436        1.1        pk 
    437  1.19.16.1       mjf 	return claimed;
    438        1.1        pk }
    439        1.1        pk 
    440        1.1        pk 
    441        1.1        pk static void
    442        1.1        pk sw_reset_adapter(struct ncr5380_softc *ncr_sc)
    443        1.1        pk {
    444        1.1        pk 
    445        1.1        pk #ifdef	DEBUG
    446        1.1        pk 	if (sw_debug) {
    447  1.19.16.1       mjf 		printf("%s\n", __func__);
    448        1.1        pk 	}
    449        1.1        pk #endif
    450        1.1        pk 
    451        1.1        pk 	/*
    452        1.1        pk 	 * The reset bits in the CSR are active low.
    453        1.1        pk 	 */
    454        1.1        pk 	SWREG_WRITE(ncr_sc, SWREG_CSR, 0);
    455        1.1        pk 	delay(10);
    456        1.1        pk 	SWREG_WRITE(ncr_sc, SWREG_CSR, SW_CSR_SCSI_RES);
    457        1.1        pk 
    458        1.1        pk 	SWREG_WRITE(ncr_sc, SWREG_DMA_ADDR, 0);
    459        1.1        pk 	SWREG_WRITE(ncr_sc, SWREG_DMA_CNT, 0);
    460        1.1        pk 	delay(10);
    461        1.1        pk 	SWREG_WRITE(ncr_sc, SWREG_CSR, SW_CSR_SCSI_RES | SW_CSR_INTR_EN);
    462        1.1        pk 
    463        1.1        pk 	SCI_CLR_INTR(ncr_sc);
    464        1.1        pk }
    465        1.1        pk 
    466        1.1        pk 
    467        1.1        pk /*****************************************************************
    468        1.1        pk  * Common functions for DMA
    469        1.1        pk  ****************************************************************/
    470        1.1        pk 
    471        1.1        pk /*
    472        1.1        pk  * Allocate a DMA handle and put it in sc->sc_dma.  Prepare
    473        1.1        pk  * for DMA transfer.  On the Sun4, this means mapping the buffer
    474        1.1        pk  * into DVMA space.
    475        1.1        pk  */
    476        1.1        pk void
    477       1.15       uwe sw_dma_alloc(struct ncr5380_softc *ncr_sc)
    478        1.1        pk {
    479        1.1        pk 	struct sw_softc *sc = (struct sw_softc *)ncr_sc;
    480        1.1        pk 	struct sci_req *sr = ncr_sc->sc_current;
    481        1.1        pk 	struct scsipi_xfer *xs = sr->sr_xs;
    482        1.1        pk 	struct sw_dma_handle *dh;
    483        1.1        pk 	int i, xlen;
    484        1.1        pk 	u_long addr;
    485        1.1        pk 
    486        1.1        pk #ifdef DIAGNOSTIC
    487        1.1        pk 	if (sr->sr_dma_hand != NULL)
    488  1.19.16.1       mjf 		panic("%s: already have DMA handle", __func__);
    489        1.1        pk #endif
    490        1.1        pk 
    491        1.1        pk #if 1	/* XXX - Temporary */
    492        1.1        pk 	/* XXX - In case we think DMA is completely broken... */
    493        1.1        pk 	if ((sc->sc_options & SW_ENABLE_DMA) == 0)
    494        1.1        pk 		return;
    495        1.1        pk #endif
    496        1.1        pk 
    497  1.19.16.1       mjf 	addr = (u_long)ncr_sc->sc_dataptr;
    498        1.1        pk 	xlen = ncr_sc->sc_datalen;
    499        1.1        pk 
    500        1.1        pk 	/* If the DMA start addr is misaligned then do PIO */
    501        1.1        pk 	if ((addr & 1) || (xlen & 1)) {
    502  1.19.16.1       mjf 		printf("%s: misaligned.\n", __func__);
    503        1.1        pk 		return;
    504        1.1        pk 	}
    505        1.1        pk 
    506        1.1        pk 	/* Make sure our caller checked sc_min_dma_len. */
    507        1.1        pk 	if (xlen < MIN_DMA_LEN)
    508  1.19.16.1       mjf 		panic("%s: xlen=0x%x", __func__, xlen);
    509        1.1        pk 
    510        1.1        pk 	/* Find free DMA handle.  Guaranteed to find one since we have
    511        1.1        pk 	   as many DMA handles as the driver has processes. */
    512        1.1        pk 	for (i = 0; i < SCI_OPENINGS; i++) {
    513        1.1        pk 		if ((sc->sc_dma[i].dh_flags & SIDH_BUSY) == 0)
    514        1.1        pk 			goto found;
    515        1.1        pk 	}
    516        1.1        pk 	panic("sw: no free DMA handles.");
    517        1.1        pk 
    518        1.1        pk found:
    519        1.1        pk 	dh = &sc->sc_dma[i];
    520        1.1        pk 	dh->dh_flags = SIDH_BUSY;
    521        1.1        pk 	dh->dh_addr = (u_char *)addr;
    522        1.1        pk 	dh->dh_maplen  = xlen;
    523        1.1        pk 
    524        1.1        pk 	/* Copy the "write" flag for convenience. */
    525        1.1        pk 	if ((xs->xs_control & XS_CTL_DATA_OUT) != 0)
    526        1.1        pk 		dh->dh_flags |= SIDH_OUT;
    527        1.1        pk 
    528        1.1        pk 	/*
    529        1.1        pk 	 * Double-map the buffer into DVMA space.  If we can't re-map
    530        1.1        pk 	 * the buffer, we print a warning and fall back to PIO mode.
    531        1.1        pk 	 *
    532        1.1        pk 	 * NOTE: it is not safe to sleep here!
    533        1.1        pk 	 */
    534        1.1        pk 	if (bus_dmamap_load(sc->sc_dmatag, dh->dh_dmamap,
    535       1.18  christos 			    (void *)addr, xlen, NULL, BUS_DMA_NOWAIT) != 0) {
    536        1.1        pk 		/* Can't remap segment */
    537  1.19.16.1       mjf 		printf("%s: can't remap 0x%lx/0x%x, doing PIO\n",
    538  1.19.16.1       mjf 		    __func__, addr, dh->dh_maplen);
    539        1.1        pk 		dh->dh_flags = 0;
    540        1.1        pk 		return;
    541        1.1        pk 	}
    542        1.1        pk 	bus_dmamap_sync(sc->sc_dmatag, dh->dh_dmamap, addr, xlen,
    543        1.1        pk 			(dh->dh_flags & SIDH_OUT)
    544        1.1        pk 				? BUS_DMASYNC_PREWRITE
    545        1.1        pk 				: BUS_DMASYNC_PREREAD);
    546        1.1        pk 
    547        1.1        pk 	/* success */
    548        1.1        pk 	sr->sr_dma_hand = dh;
    549        1.1        pk }
    550        1.1        pk 
    551        1.1        pk 
    552        1.1        pk void
    553       1.15       uwe sw_dma_free(struct ncr5380_softc *ncr_sc)
    554        1.1        pk {
    555        1.1        pk 	struct sw_softc *sc = (struct sw_softc *)ncr_sc;
    556        1.1        pk 	struct sci_req *sr = ncr_sc->sc_current;
    557        1.1        pk 	struct sw_dma_handle *dh = sr->sr_dma_hand;
    558        1.1        pk 
    559        1.1        pk #ifdef DIAGNOSTIC
    560        1.1        pk 	if (dh == NULL)
    561  1.19.16.1       mjf 		panic("%s: no DMA handle", __func__);
    562        1.1        pk #endif
    563        1.1        pk 
    564        1.1        pk 	if (ncr_sc->sc_state & NCR_DOINGDMA)
    565  1.19.16.1       mjf 		panic("%s: free while in progress", __func__);
    566        1.1        pk 
    567        1.1        pk 	if (dh->dh_flags & SIDH_BUSY) {
    568        1.1        pk 		/* Give back the DVMA space. */
    569        1.1        pk 		bus_dmamap_sync(sc->sc_dmatag, dh->dh_dmamap,
    570        1.1        pk 				dh->dh_dvma, dh->dh_maplen,
    571        1.1        pk 				(dh->dh_flags & SIDH_OUT)
    572        1.1        pk 					? BUS_DMASYNC_POSTWRITE
    573        1.1        pk 					: BUS_DMASYNC_POSTREAD);
    574        1.1        pk 		bus_dmamap_unload(sc->sc_dmatag, dh->dh_dmamap);
    575        1.1        pk 		dh->dh_flags = 0;
    576        1.1        pk 	}
    577        1.1        pk 	sr->sr_dma_hand = NULL;
    578        1.1        pk }
    579        1.1        pk 
    580        1.1        pk 
    581        1.1        pk /*
    582        1.1        pk  * Poll (spin-wait) for DMA completion.
    583        1.1        pk  * Called right after xx_dma_start(), and
    584        1.1        pk  * xx_dma_stop() will be called next.
    585        1.1        pk  * Same for either VME or OBIO.
    586        1.1        pk  */
    587        1.1        pk void
    588       1.15       uwe sw_dma_poll(struct ncr5380_softc *ncr_sc)
    589        1.1        pk {
    590        1.1        pk 	struct sci_req *sr = ncr_sc->sc_current;
    591        1.1        pk 	int tmo, csr_mask, csr;
    592        1.1        pk 
    593        1.1        pk 	/* Make sure DMA started successfully. */
    594        1.1        pk 	if (ncr_sc->sc_state & NCR_ABORTING)
    595        1.1        pk 		return;
    596        1.1        pk 
    597        1.1        pk 	csr_mask = SW_CSR_SBC_IP | SW_CSR_DMA_IP |
    598  1.19.16.1       mjf 	    SW_CSR_DMA_CONFLICT | SW_CSR_DMA_BUS_ERR;
    599        1.1        pk 
    600        1.1        pk 	tmo = 50000;	/* X100 = 5 sec. */
    601        1.1        pk 	for (;;) {
    602        1.1        pk 		csr = SWREG_READ(ncr_sc, SWREG_CSR);
    603        1.1        pk 		if (csr & csr_mask)
    604        1.1        pk 			break;
    605        1.1        pk 		if (--tmo <= 0) {
    606        1.1        pk 			printf("%s: DMA timeout (while polling)\n",
    607  1.19.16.1       mjf 			    device_xname(ncr_sc->sc_dev));
    608        1.1        pk 			/* Indicate timeout as MI code would. */
    609        1.1        pk 			sr->sr_flags |= SR_OVERDUE;
    610        1.1        pk 			break;
    611        1.1        pk 		}
    612        1.1        pk 		delay(100);
    613        1.1        pk 	}
    614        1.1        pk 
    615        1.1        pk #ifdef	DEBUG
    616        1.1        pk 	if (sw_debug) {
    617  1.19.16.1       mjf 		printf("%s: done, csr=0x%x\n", __func__, csr);
    618        1.1        pk 	}
    619        1.1        pk #endif
    620        1.1        pk }
    621        1.1        pk 
    622        1.1        pk 
    623        1.1        pk /*
    624        1.1        pk  * This is called when the bus is going idle,
    625        1.1        pk  * so we want to enable the SBC interrupts.
    626        1.1        pk  * That is controlled by the DMA enable!
    627        1.1        pk  * Who would have guessed!
    628        1.1        pk  * What a NASTY trick!
    629        1.1        pk  *
    630        1.1        pk  * XXX THIS MIGHT NOT WORK RIGHT!
    631        1.1        pk  */
    632        1.1        pk void
    633       1.15       uwe sw_intr_on(struct ncr5380_softc *ncr_sc)
    634        1.1        pk {
    635       1.15       uwe 	uint32_t csr;
    636        1.1        pk 
    637        1.1        pk 	sw_dma_setup(ncr_sc);
    638        1.1        pk 	csr = SWREG_READ(ncr_sc, SWREG_CSR);
    639        1.1        pk 	csr |= SW_CSR_DMA_EN;	/* XXX - this bit is for vme only?! */
    640        1.1        pk 	SWREG_WRITE(ncr_sc, SWREG_CSR, csr);
    641        1.1        pk }
    642        1.1        pk 
    643        1.1        pk /*
    644        1.1        pk  * This is called when the bus is idle and we are
    645        1.1        pk  * about to start playing with the SBC chip.
    646        1.1        pk  *
    647        1.1        pk  * XXX THIS MIGHT NOT WORK RIGHT!
    648        1.1        pk  */
    649        1.1        pk void
    650       1.15       uwe sw_intr_off(struct ncr5380_softc *ncr_sc)
    651        1.1        pk {
    652       1.15       uwe 	uint32_t csr;
    653        1.1        pk 
    654        1.1        pk 	csr = SWREG_READ(ncr_sc, SWREG_CSR);
    655        1.1        pk 	csr &= ~SW_CSR_DMA_EN;
    656        1.1        pk 	SWREG_WRITE(ncr_sc, SWREG_CSR, csr);
    657        1.1        pk }
    658        1.1        pk 
    659        1.1        pk 
    660        1.1        pk /*
    661        1.1        pk  * This function is called during the COMMAND or MSG_IN phase
    662        1.5       wiz  * that precedes a DATA_IN or DATA_OUT phase, in case we need
    663        1.1        pk  * to setup the DMA engine before the bus enters a DATA phase.
    664        1.1        pk  *
    665        1.1        pk  * On the OBIO version we just clear the DMA count and address
    666        1.1        pk  * here (to make sure it stays idle) and do the real setup
    667        1.1        pk  * later, in dma_start.
    668        1.1        pk  */
    669        1.1        pk void
    670       1.15       uwe sw_dma_setup(struct ncr5380_softc *ncr_sc)
    671        1.1        pk {
    672       1.15       uwe 	uint32_t csr;
    673        1.1        pk 
    674        1.1        pk 	/* No FIFO to reset on "sw". */
    675        1.1        pk 
    676        1.1        pk 	/* Set direction (assume recv here) */
    677        1.1        pk 	csr = SWREG_READ(ncr_sc, SWREG_CSR);
    678        1.1        pk 	csr &= ~SW_CSR_SEND;
    679        1.1        pk 	SWREG_WRITE(ncr_sc, SWREG_CSR, csr);
    680        1.1        pk 
    681        1.1        pk 	SWREG_WRITE(ncr_sc, SWREG_DMA_ADDR, 0);
    682        1.1        pk 	SWREG_WRITE(ncr_sc, SWREG_DMA_CNT, 0);
    683        1.1        pk }
    684        1.1        pk 
    685        1.1        pk 
    686        1.1        pk void
    687       1.15       uwe sw_dma_start(struct ncr5380_softc *ncr_sc)
    688        1.1        pk {
    689        1.1        pk 	struct sw_softc *sc = (struct sw_softc *)ncr_sc;
    690        1.1        pk 	struct sci_req *sr = ncr_sc->sc_current;
    691        1.1        pk 	struct sw_dma_handle *dh = sr->sr_dma_hand;
    692        1.1        pk 	u_long dva;
    693        1.1        pk 	int xlen, adj, adjlen;
    694        1.1        pk 	u_int mode;
    695       1.15       uwe 	uint32_t csr;
    696        1.1        pk 
    697        1.1        pk 	/*
    698        1.1        pk 	 * Get the DVMA mapping for this segment.
    699        1.1        pk 	 */
    700        1.1        pk 	dva = (u_long)(dh->dh_dvma);
    701        1.1        pk 	if (dva & 1)
    702  1.19.16.1       mjf 		panic("%s: bad dva=0x%lx", __func__, dva);
    703        1.1        pk 
    704        1.1        pk 	xlen = ncr_sc->sc_datalen;
    705        1.1        pk 	xlen &= ~1;
    706        1.1        pk 	sc->sc_xlen = xlen;	/* XXX: or less... */
    707        1.1        pk 
    708        1.1        pk #ifdef	DEBUG
    709        1.1        pk 	if (sw_debug & 2) {
    710  1.19.16.1       mjf 		printf("%s: dh=%p, dva=0x%lx, xlen=%d\n",
    711  1.19.16.1       mjf 		    __func__, dh, dva, xlen);
    712        1.1        pk 	}
    713        1.1        pk #endif
    714        1.1        pk 
    715        1.1        pk 	/*
    716        1.1        pk 	 * Set up the DMA controller.
    717        1.1        pk 	 * Note that (dh->dh_len < sc_datalen)
    718        1.1        pk 	 */
    719        1.1        pk 
    720        1.1        pk 	/* Set direction (send/recv) */
    721        1.1        pk 	csr = SWREG_READ(ncr_sc, SWREG_CSR);
    722        1.1        pk 	if (dh->dh_flags & SIDH_OUT) {
    723        1.1        pk 		csr |= SW_CSR_SEND;
    724        1.1        pk 	} else {
    725        1.1        pk 		csr &= ~SW_CSR_SEND;
    726        1.1        pk 	}
    727        1.1        pk 	SWREG_WRITE(ncr_sc, SWREG_CSR, csr);
    728        1.1        pk 
    729        1.1        pk 	/*
    730        1.1        pk 	 * The "sw" needs longword aligned transfers.  We
    731        1.1        pk 	 * detect a shortword aligned transfer here, and adjust the
    732        1.1        pk 	 * DMA transfer by 2 bytes.  These two bytes are read/written
    733        1.1        pk 	 * in PIO mode just before the DMA is started.
    734        1.1        pk 	 */
    735        1.1        pk 	adj = 0;
    736        1.1        pk 	if (dva & 2) {
    737        1.1        pk 		adj = 2;
    738        1.1        pk #ifdef DEBUG
    739        1.1        pk 		if (sw_debug & 2)
    740  1.19.16.1       mjf 			printf("%s: adjusted up %d bytes\n", __func__, adj);
    741        1.1        pk #endif
    742        1.1        pk 	}
    743        1.1        pk 
    744        1.1        pk 	/* We have to frob the address on the "sw". */
    745        1.1        pk 	dh->dh_startingpa = (dva | 0xF00000);
    746        1.1        pk 	SWREG_WRITE(ncr_sc, SWREG_DMA_ADDR, (u_int)(dh->dh_startingpa + adj));
    747        1.1        pk 	SWREG_WRITE(ncr_sc, SWREG_DMA_CNT, xlen - adj);
    748        1.1        pk 
    749        1.1        pk 	/*
    750        1.1        pk 	 * Acknowledge the phase change.  (After DMA setup!)
    751        1.1        pk 	 * Put the SBIC into DMA mode, and start the transfer.
    752        1.1        pk 	 */
    753        1.1        pk 	if (dh->dh_flags & SIDH_OUT) {
    754        1.1        pk 		NCR5380_WRITE(ncr_sc, sci_tcmd, PHASE_DATA_OUT);
    755        1.1        pk 		if (adj) {
    756        1.1        pk 			adjlen = ncr5380_pio_out(ncr_sc, PHASE_DATA_OUT,
    757        1.1        pk 			    adj, dh->dh_addr);
    758        1.1        pk 			if (adjlen != adj)
    759        1.1        pk 				printf("%s: bad outgoing adj, %d != %d\n",
    760  1.19.16.1       mjf 				    device_xname(ncr_sc->sc_dev), adjlen, adj);
    761        1.1        pk 		}
    762        1.1        pk 		SCI_CLR_INTR(ncr_sc);
    763        1.1        pk 		NCR5380_WRITE(ncr_sc, sci_icmd, SCI_ICMD_DATA);
    764        1.1        pk 		mode = NCR5380_READ(ncr_sc, sci_mode);
    765        1.1        pk 		mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
    766        1.1        pk 		NCR5380_WRITE(ncr_sc, sci_mode, mode);
    767        1.1        pk 		NCR5380_WRITE(ncr_sc, sci_dma_send, 0); 	/* start it */
    768        1.1        pk 	} else {
    769        1.1        pk 		NCR5380_WRITE(ncr_sc, sci_tcmd, PHASE_DATA_IN);
    770        1.1        pk 		if (adj) {
    771        1.1        pk 			adjlen = ncr5380_pio_in(ncr_sc, PHASE_DATA_IN,
    772        1.1        pk 			    adj, dh->dh_addr);
    773        1.1        pk 			if (adjlen != adj)
    774        1.1        pk 				printf("%s: bad incoming adj, %d != %d\n",
    775  1.19.16.1       mjf 				    device_xname(ncr_sc->sc_dev), adjlen, adj);
    776        1.1        pk 		}
    777        1.1        pk 		SCI_CLR_INTR(ncr_sc);
    778        1.1        pk 		NCR5380_WRITE(ncr_sc, sci_icmd, 0);
    779        1.1        pk 		mode = NCR5380_READ(ncr_sc, sci_mode);
    780        1.1        pk 		mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
    781        1.1        pk 		NCR5380_WRITE(ncr_sc, sci_mode, mode);
    782        1.1        pk 		NCR5380_WRITE(ncr_sc, sci_irecv, 0); 	/* start it */
    783        1.1        pk 	}
    784        1.1        pk 
    785        1.1        pk 	/* Let'er rip! */
    786        1.1        pk 	csr |= SW_CSR_DMA_EN;
    787        1.1        pk 	SWREG_WRITE(ncr_sc, SWREG_CSR, csr);
    788        1.1        pk 
    789        1.1        pk 	ncr_sc->sc_state |= NCR_DOINGDMA;
    790        1.1        pk 
    791        1.1        pk #ifdef	DEBUG
    792        1.1        pk 	if (sw_debug & 2) {
    793  1.19.16.1       mjf 		printf("%s: started, flags=0x%x\n",
    794  1.19.16.1       mjf 		    __func__, ncr_sc->sc_state);
    795        1.1        pk 	}
    796        1.1        pk #endif
    797        1.1        pk }
    798        1.1        pk 
    799        1.1        pk 
    800        1.1        pk void
    801       1.15       uwe sw_dma_eop(struct ncr5380_softc *ncr_sc)
    802        1.1        pk {
    803        1.1        pk 
    804        1.1        pk 	/* Not needed - DMA was stopped prior to examining sci_csr */
    805        1.1        pk }
    806        1.1        pk 
    807        1.1        pk #if (defined(DEBUG) || defined(DIAGNOSTIC)) && !defined(COUNT_SW_LEFTOVERS)
    808        1.1        pk #define COUNT_SW_LEFTOVERS
    809        1.1        pk #endif
    810        1.1        pk #ifdef COUNT_SW_LEFTOVERS
    811        1.1        pk /*
    812        1.1        pk  * Let's find out how often these occur.  Read these with DDB from time
    813        1.1        pk  * to time.
    814        1.1        pk  */
    815        1.1        pk int	sw_3_leftover = 0;
    816        1.1        pk int	sw_2_leftover = 0;
    817        1.1        pk int	sw_1_leftover = 0;
    818        1.1        pk int	sw_0_leftover = 0;
    819        1.1        pk #endif
    820        1.1        pk 
    821        1.1        pk void
    822       1.15       uwe sw_dma_stop(struct ncr5380_softc *ncr_sc)
    823        1.1        pk {
    824        1.1        pk 	struct sci_req *sr = ncr_sc->sc_current;
    825        1.1        pk 	struct sw_dma_handle *dh = sr->sr_dma_hand;
    826        1.1        pk 	int ntrans = 0, dva;
    827        1.1        pk 	u_int mode;
    828       1.15       uwe 	uint32_t csr;
    829        1.1        pk 
    830        1.1        pk 	if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
    831        1.1        pk #ifdef	DEBUG
    832  1.19.16.1       mjf 		printf("%s: DMA not running\n", __func__);
    833        1.1        pk #endif
    834        1.1        pk 		return;
    835        1.1        pk 	}
    836        1.1        pk 	ncr_sc->sc_state &= ~NCR_DOINGDMA;
    837        1.1        pk 
    838        1.1        pk 	/* First, halt the DMA engine. */
    839        1.1        pk 	csr = SWREG_READ(ncr_sc, SWREG_CSR);
    840        1.1        pk 	csr &= ~SW_CSR_DMA_EN;
    841        1.1        pk 	SWREG_WRITE(ncr_sc, SWREG_CSR, csr);
    842        1.1        pk 
    843        1.1        pk 	/*
    844        1.1        pk 	 * XXX HARDWARE BUG!
    845        1.1        pk 	 * Apparently, some early 4/100 SCSI controllers had a hardware
    846        1.1        pk 	 * bug that caused the controller to do illegal memory access.
    847        1.1        pk 	 * We see this as SW_CSR_DMA_BUS_ERR (makes sense).  To work around
    848        1.1        pk 	 * this, we simply need to clean up after ourselves ... there will
    849        1.1        pk 	 * be as many as 3 bytes left over.  Since we clean up "left-over"
    850        1.1        pk 	 * bytes on every read anyway, we just continue to chug along
    851        1.1        pk 	 * if SW_CSR_DMA_BUS_ERR is asserted.  (This was probably worked
    852        1.1        pk 	 * around in hardware later with the "left-over byte" indicator
    853        1.1        pk 	 * in the VME controller.)
    854        1.1        pk 	 */
    855        1.1        pk #if 0
    856       1.16       uwe 	if (csr & (SW_CSR_DMA_CONFLICT | SW_CSR_DMA_BUS_ERR))
    857        1.1        pk #else
    858       1.16       uwe 	if (csr & (SW_CSR_DMA_CONFLICT))
    859        1.1        pk #endif
    860       1.16       uwe 	{
    861        1.1        pk 		printf("sw: DMA error, csr=0x%x, reset\n", csr);
    862        1.1        pk 		sr->sr_xs->error = XS_DRIVER_STUFFUP;
    863        1.1        pk 		ncr_sc->sc_state |= NCR_ABORTING;
    864        1.1        pk 		sw_reset_adapter(ncr_sc);
    865        1.1        pk 	}
    866        1.1        pk 
    867        1.1        pk 	/* Note that timeout may have set the error flag. */
    868        1.1        pk 	if (ncr_sc->sc_state & NCR_ABORTING)
    869        1.1        pk 		goto out;
    870        1.1        pk 
    871        1.1        pk 	/*
    872        1.1        pk 	 * Now try to figure out how much actually transferred
    873        1.1        pk 	 *
    874        1.1        pk 	 * The "sw" doesn't have a FIFO or a bcr, so we've stored
    875        1.1        pk 	 * the starting PA of the transfer in the DMA handle,
    876        1.1        pk 	 * and subtract it from the ending PA left in the dma_addr
    877        1.1        pk 	 * register.
    878        1.1        pk 	 */
    879        1.1        pk 	dva = SWREG_READ(ncr_sc, SWREG_DMA_ADDR);
    880        1.1        pk 	ntrans = (dva - dh->dh_startingpa);
    881        1.1        pk 
    882        1.1        pk #ifdef	DEBUG
    883        1.1        pk 	if (sw_debug & 2) {
    884  1.19.16.1       mjf 		printf("%s: ntrans=0x%x\n", __func__, ntrans);
    885        1.1        pk 	}
    886        1.1        pk #endif
    887        1.1        pk 
    888        1.1        pk 	if (ntrans > ncr_sc->sc_datalen)
    889  1.19.16.1       mjf 		panic("%s: excess transfer", __func__);
    890        1.1        pk 
    891        1.1        pk 	/* Adjust data pointer */
    892        1.1        pk 	ncr_sc->sc_dataptr += ntrans;
    893        1.1        pk 	ncr_sc->sc_datalen -= ntrans;
    894        1.1        pk 
    895        1.1        pk 	/*
    896        1.1        pk 	 * After a read, we may need to clean-up
    897        1.1        pk 	 * "Left-over bytes"  (yuck!)  The "sw" doesn't
    898        1.1        pk 	 * have a "left-over" indicator, so we have to so
    899        1.1        pk 	 * this no matter what.  Ick.
    900        1.1        pk 	 */
    901        1.1        pk 	if ((dh->dh_flags & SIDH_OUT) == 0) {
    902        1.1        pk 		char *cp = ncr_sc->sc_dataptr;
    903       1.15       uwe 		uint32_t bpr;
    904        1.1        pk 
    905        1.1        pk 		bpr = SWREG_READ(ncr_sc, SWREG_BPR);
    906        1.1        pk 
    907        1.1        pk 		switch (dva & 3) {
    908        1.1        pk 		case 3:
    909        1.1        pk 			cp[0] = (bpr & 0xff000000) >> 24;
    910        1.1        pk 			cp[1] = (bpr & 0x00ff0000) >> 16;
    911        1.1        pk 			cp[2] = (bpr & 0x0000ff00) >> 8;
    912        1.1        pk #ifdef COUNT_SW_LEFTOVERS
    913        1.1        pk 			++sw_3_leftover;
    914        1.1        pk #endif
    915        1.1        pk 			break;
    916        1.1        pk 
    917        1.1        pk 		case 2:
    918        1.1        pk 			cp[0] = (bpr & 0xff000000) >> 24;
    919        1.1        pk 			cp[1] = (bpr & 0x00ff0000) >> 16;
    920        1.1        pk #ifdef COUNT_SW_LEFTOVERS
    921        1.1        pk 			++sw_2_leftover;
    922        1.1        pk #endif
    923        1.1        pk 			break;
    924        1.1        pk 
    925        1.1        pk 		case 1:
    926        1.1        pk 			cp[0] = (bpr & 0xff000000) >> 24;
    927        1.1        pk #ifdef COUNT_SW_LEFTOVERS
    928        1.1        pk 			++sw_1_leftover;
    929        1.1        pk #endif
    930        1.1        pk 			break;
    931        1.1        pk 
    932        1.1        pk #ifdef COUNT_SW_LEFTOVERS
    933        1.1        pk 		default:
    934        1.1        pk 			++sw_0_leftover;
    935        1.1        pk 			break;
    936        1.1        pk #endif
    937        1.1        pk 		}
    938        1.1        pk 	}
    939        1.1        pk 
    940        1.1        pk  out:
    941        1.1        pk 	SWREG_WRITE(ncr_sc, SWREG_DMA_ADDR, 0);
    942        1.1        pk 	SWREG_WRITE(ncr_sc, SWREG_DMA_CNT, 0);
    943        1.1        pk 
    944        1.1        pk 	/* Put SBIC back in PIO mode. */
    945        1.1        pk 	mode = NCR5380_READ(ncr_sc, sci_mode);
    946        1.1        pk 	mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE);
    947        1.1        pk 	NCR5380_WRITE(ncr_sc, sci_mode, mode);
    948        1.1        pk 	NCR5380_WRITE(ncr_sc, sci_icmd, 0);
    949        1.1        pk 
    950        1.1        pk #ifdef DEBUG
    951        1.1        pk 	if (sw_debug & 2) {
    952  1.19.16.1       mjf 		printf("%s: ntrans=0x%x\n", __func__, ntrans);
    953        1.1        pk 	}
    954        1.1        pk #endif
    955        1.1        pk }
    956