Home | History | Annotate | Line # | Download | only in ic
adw.c revision 1.52
      1  1.52       chs /* $NetBSD: adw.c,v 1.52 2012/10/27 17:18:18 chs Exp $	 */
      2   1.1     dante 
      3   1.1     dante /*
      4   1.1     dante  * Generic driver for the Advanced Systems Inc. SCSI controllers
      5   1.1     dante  *
      6  1.13     dante  * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
      7   1.1     dante  * All rights reserved.
      8   1.1     dante  *
      9   1.1     dante  * Author: Baldassare Dante Profeta <dante (at) mclink.it>
     10   1.1     dante  *
     11   1.1     dante  * Redistribution and use in source and binary forms, with or without
     12   1.1     dante  * modification, are permitted provided that the following conditions
     13   1.1     dante  * are met:
     14   1.1     dante  * 1. Redistributions of source code must retain the above copyright
     15   1.1     dante  *    notice, this list of conditions and the following disclaimer.
     16   1.1     dante  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1     dante  *    notice, this list of conditions and the following disclaimer in the
     18   1.1     dante  *    documentation and/or other materials provided with the distribution.
     19   1.1     dante  * 3. All advertising materials mentioning features or use of this software
     20   1.1     dante  *    must display the following acknowledgement:
     21   1.1     dante  *        This product includes software developed by the NetBSD
     22   1.1     dante  *        Foundation, Inc. and its contributors.
     23   1.1     dante  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24   1.1     dante  *    contributors may be used to endorse or promote products derived
     25   1.1     dante  *    from this software without specific prior written permission.
     26   1.1     dante  *
     27   1.1     dante  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28   1.1     dante  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29   1.1     dante  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30   1.1     dante  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31   1.1     dante  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32   1.1     dante  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33   1.1     dante  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34   1.1     dante  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35   1.1     dante  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36   1.1     dante  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37   1.1     dante  * POSSIBILITY OF SUCH DAMAGE.
     38   1.1     dante  */
     39  1.36     lukem 
     40  1.36     lukem #include <sys/cdefs.h>
     41  1.52       chs __KERNEL_RCSID(0, "$NetBSD: adw.c,v 1.52 2012/10/27 17:18:18 chs Exp $");
     42   1.1     dante 
     43   1.1     dante #include <sys/param.h>
     44   1.1     dante #include <sys/systm.h>
     45  1.15   thorpej #include <sys/callout.h>
     46   1.1     dante #include <sys/kernel.h>
     47   1.1     dante #include <sys/errno.h>
     48   1.1     dante #include <sys/ioctl.h>
     49   1.1     dante #include <sys/device.h>
     50   1.1     dante #include <sys/malloc.h>
     51   1.1     dante #include <sys/buf.h>
     52   1.1     dante #include <sys/proc.h>
     53   1.1     dante 
     54  1.48        ad #include <sys/bus.h>
     55  1.48        ad #include <sys/intr.h>
     56   1.1     dante 
     57   1.1     dante #include <dev/scsipi/scsi_all.h>
     58   1.1     dante #include <dev/scsipi/scsipi_all.h>
     59   1.1     dante #include <dev/scsipi/scsiconf.h>
     60   1.1     dante 
     61   1.1     dante #include <dev/ic/adwlib.h>
     62  1.22     dante #include <dev/ic/adwmcode.h>
     63   1.1     dante #include <dev/ic/adw.h>
     64   1.1     dante 
     65   1.1     dante #ifndef DDB
     66  1.11     dante #define	Debugger()	panic("should call debugger here (adw.c)")
     67   1.2     dante #endif				/* ! DDB */
     68   1.1     dante 
     69   1.1     dante /******************************************************************************/
     70   1.1     dante 
     71   1.1     dante 
     72  1.30     lukem static int adw_alloc_controls(ADW_SOFTC *);
     73  1.30     lukem static int adw_alloc_carriers(ADW_SOFTC *);
     74  1.30     lukem static int adw_create_ccbs(ADW_SOFTC *, ADW_CCB *, int);
     75  1.30     lukem static void adw_free_ccb(ADW_SOFTC *, ADW_CCB *);
     76  1.30     lukem static void adw_reset_ccb(ADW_CCB *);
     77  1.30     lukem static int adw_init_ccb(ADW_SOFTC *, ADW_CCB *);
     78  1.30     lukem static ADW_CCB *adw_get_ccb(ADW_SOFTC *);
     79  1.30     lukem static int adw_queue_ccb(ADW_SOFTC *, ADW_CCB *);
     80  1.30     lukem 
     81  1.30     lukem static void adw_scsipi_request(struct scsipi_channel *,
     82  1.30     lukem 	scsipi_adapter_req_t, void *);
     83  1.30     lukem static int adw_build_req(ADW_SOFTC *, ADW_CCB *);
     84  1.30     lukem static void adw_build_sglist(ADW_CCB *, ADW_SCSI_REQ_Q *, ADW_SG_BLOCK *);
     85  1.30     lukem static void adwminphys(struct buf *);
     86  1.30     lukem static void adw_isr_callback(ADW_SOFTC *, ADW_SCSI_REQ_Q *);
     87  1.30     lukem static void adw_async_callback(ADW_SOFTC *, u_int8_t);
     88  1.30     lukem 
     89  1.30     lukem static void adw_print_info(ADW_SOFTC *, int);
     90  1.30     lukem 
     91  1.30     lukem static int adw_poll(ADW_SOFTC *, struct scsipi_xfer *, int);
     92  1.30     lukem static void adw_timeout(void *);
     93  1.30     lukem static void adw_reset_bus(ADW_SOFTC *);
     94   1.1     dante 
     95   1.1     dante 
     96   1.1     dante /******************************************************************************/
     97  1.22     dante /*                       DMA Mapping for Control Blocks                       */
     98   1.1     dante /******************************************************************************/
     99   1.1     dante 
    100   1.1     dante 
    101   1.1     dante static int
    102  1.30     lukem adw_alloc_controls(ADW_SOFTC *sc)
    103   1.1     dante {
    104   1.1     dante 	bus_dma_segment_t seg;
    105   1.1     dante 	int             error, rseg;
    106   1.1     dante 
    107   1.1     dante 	/*
    108  1.13     dante          * Allocate the control structure.
    109   1.1     dante          */
    110   1.1     dante 	if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct adw_control),
    111  1.26   thorpej 			   PAGE_SIZE, 0, &seg, 1, &rseg,
    112  1.26   thorpej 			   BUS_DMA_NOWAIT)) != 0) {
    113  1.52       chs 		aprint_error_dev(sc->sc_dev, "unable to allocate control structures,"
    114  1.49    cegger 		       " error = %d\n", error);
    115   1.1     dante 		return (error);
    116   1.1     dante 	}
    117   1.1     dante 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    118  1.47  christos 		   sizeof(struct adw_control), (void **) & sc->sc_control,
    119   1.1     dante 				 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    120  1.52       chs 		aprint_error_dev(sc->sc_dev, "unable to map control structures, error = %d\n",
    121  1.49    cegger 		       error);
    122   1.1     dante 		return (error);
    123   1.1     dante 	}
    124  1.13     dante 
    125   1.1     dante 	/*
    126   1.1     dante          * Create and load the DMA map used for the control blocks.
    127   1.1     dante          */
    128   1.1     dante 	if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct adw_control),
    129   1.1     dante 			   1, sizeof(struct adw_control), 0, BUS_DMA_NOWAIT,
    130   1.1     dante 				       &sc->sc_dmamap_control)) != 0) {
    131  1.52       chs 		aprint_error_dev(sc->sc_dev, "unable to create control DMA map, error = %d\n",
    132  1.49    cegger 		       error);
    133   1.1     dante 		return (error);
    134   1.1     dante 	}
    135   1.1     dante 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_control,
    136   1.1     dante 			   sc->sc_control, sizeof(struct adw_control), NULL,
    137   1.1     dante 				     BUS_DMA_NOWAIT)) != 0) {
    138  1.52       chs 		aprint_error_dev(sc->sc_dev, "unable to load control DMA map, error = %d\n",
    139  1.49    cegger 		       error);
    140   1.1     dante 		return (error);
    141   1.1     dante 	}
    142  1.13     dante 
    143  1.13     dante 	return (0);
    144  1.13     dante }
    145  1.13     dante 
    146  1.13     dante 
    147  1.13     dante static int
    148  1.30     lukem adw_alloc_carriers(ADW_SOFTC *sc)
    149  1.13     dante {
    150  1.13     dante 	bus_dma_segment_t seg;
    151  1.13     dante 	int             error, rseg;
    152  1.13     dante 
    153  1.13     dante 	/*
    154  1.13     dante          * Allocate the control structure.
    155  1.13     dante          */
    156  1.19     dante 	sc->sc_control->carriers = malloc(sizeof(ADW_CARRIER) * ADW_MAX_CARRIER,
    157  1.13     dante 			M_DEVBUF, M_WAITOK);
    158  1.13     dante 	if(!sc->sc_control->carriers) {
    159  1.52       chs 		aprint_error_dev(sc->sc_dev,
    160  1.49    cegger 		    "malloc() failed in allocating carrier structures\n");
    161  1.18   thorpej 		return (ENOMEM);
    162  1.13     dante 	}
    163  1.13     dante 
    164  1.13     dante 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    165  1.19     dante 			sizeof(ADW_CARRIER) * ADW_MAX_CARRIER,
    166  1.19     dante 			0x10, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    167  1.52       chs 		aprint_error_dev(sc->sc_dev, "unable to allocate carrier structures,"
    168  1.49    cegger 		       " error = %d\n", error);
    169  1.13     dante 		return (error);
    170  1.13     dante 	}
    171  1.13     dante 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    172  1.19     dante 			sizeof(ADW_CARRIER) * ADW_MAX_CARRIER,
    173  1.47  christos 			(void **) &sc->sc_control->carriers,
    174  1.13     dante 			BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    175  1.52       chs 		aprint_error_dev(sc->sc_dev, "unable to map carrier structures,"
    176  1.49    cegger 			" error = %d\n", error);
    177  1.13     dante 		return (error);
    178  1.13     dante 	}
    179  1.13     dante 
    180  1.13     dante 	/*
    181  1.13     dante          * Create and load the DMA map used for the control blocks.
    182  1.13     dante          */
    183  1.13     dante 	if ((error = bus_dmamap_create(sc->sc_dmat,
    184  1.19     dante 			sizeof(ADW_CARRIER) * ADW_MAX_CARRIER, 1,
    185  1.19     dante 			sizeof(ADW_CARRIER) * ADW_MAX_CARRIER, 0,BUS_DMA_NOWAIT,
    186  1.13     dante 			&sc->sc_dmamap_carrier)) != 0) {
    187  1.52       chs 		aprint_error_dev(sc->sc_dev, "unable to create carriers DMA map,"
    188  1.49    cegger 			" error = %d\n", error);
    189  1.13     dante 		return (error);
    190  1.13     dante 	}
    191  1.13     dante 	if ((error = bus_dmamap_load(sc->sc_dmat,
    192  1.13     dante 			sc->sc_dmamap_carrier, sc->sc_control->carriers,
    193  1.19     dante 			sizeof(ADW_CARRIER) * ADW_MAX_CARRIER, NULL,
    194  1.13     dante 			BUS_DMA_NOWAIT)) != 0) {
    195  1.52       chs 		aprint_error_dev(sc->sc_dev, "unable to load carriers DMA map,"
    196  1.49    cegger 			" error = %d\n", error);
    197  1.13     dante 		return (error);
    198  1.13     dante 	}
    199  1.13     dante 
    200   1.1     dante 	return (0);
    201   1.1     dante }
    202   1.1     dante 
    203   1.1     dante 
    204  1.22     dante /******************************************************************************/
    205  1.22     dante /*                           Control Blocks routines                          */
    206  1.22     dante /******************************************************************************/
    207  1.13     dante 
    208  1.13     dante 
    209  1.13     dante /*
    210   1.1     dante  * Create a set of ccbs and add them to the free list.  Called once
    211   1.1     dante  * by adw_init().  We return the number of CCBs successfully created.
    212   1.1     dante  */
    213   1.1     dante static int
    214  1.30     lukem adw_create_ccbs(ADW_SOFTC *sc, ADW_CCB *ccbstore, int count)
    215   1.1     dante {
    216   1.1     dante 	ADW_CCB        *ccb;
    217   1.1     dante 	int             i, error;
    218   1.1     dante 
    219   1.1     dante 	for (i = 0; i < count; i++) {
    220   1.1     dante 		ccb = &ccbstore[i];
    221   1.1     dante 		if ((error = adw_init_ccb(sc, ccb)) != 0) {
    222  1.52       chs 			aprint_error_dev(sc->sc_dev, "unable to initialize ccb, error = %d\n",
    223  1.49    cegger 			       error);
    224   1.1     dante 			return (i);
    225   1.1     dante 		}
    226   1.1     dante 		TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, chain);
    227   1.1     dante 	}
    228   1.1     dante 
    229   1.1     dante 	return (i);
    230   1.1     dante }
    231   1.1     dante 
    232   1.1     dante 
    233   1.1     dante /*
    234   1.1     dante  * A ccb is put onto the free list.
    235   1.1     dante  */
    236   1.1     dante static void
    237  1.30     lukem adw_free_ccb(ADW_SOFTC *sc, ADW_CCB *ccb)
    238   1.1     dante {
    239   1.1     dante 	int             s;
    240   1.1     dante 
    241   1.1     dante 	s = splbio();
    242   1.1     dante 
    243   1.1     dante 	adw_reset_ccb(ccb);
    244   1.1     dante 	TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
    245   1.1     dante 
    246   1.1     dante 	splx(s);
    247   1.1     dante }
    248   1.1     dante 
    249   1.1     dante 
    250   1.1     dante static void
    251  1.30     lukem adw_reset_ccb(ADW_CCB *ccb)
    252   1.1     dante {
    253   1.1     dante 
    254   1.1     dante 	ccb->flags = 0;
    255   1.1     dante }
    256   1.1     dante 
    257   1.1     dante 
    258   1.1     dante static int
    259  1.30     lukem adw_init_ccb(ADW_SOFTC *sc, ADW_CCB *ccb)
    260   1.1     dante {
    261   1.7     dante 	int	hashnum, error;
    262   1.1     dante 
    263   1.1     dante 	/*
    264   1.1     dante          * Create the DMA map for this CCB.
    265   1.1     dante          */
    266   1.1     dante 	error = bus_dmamap_create(sc->sc_dmat,
    267   1.1     dante 				  (ADW_MAX_SG_LIST - 1) * PAGE_SIZE,
    268   1.1     dante 			 ADW_MAX_SG_LIST, (ADW_MAX_SG_LIST - 1) * PAGE_SIZE,
    269   1.1     dante 		   0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ccb->dmamap_xfer);
    270   1.1     dante 	if (error) {
    271  1.52       chs 		aprint_error_dev(sc->sc_dev, "unable to create CCB DMA map, error = %d\n",
    272  1.49    cegger 		       error);
    273   1.1     dante 		return (error);
    274   1.1     dante 	}
    275   1.7     dante 
    276   1.7     dante 	/*
    277   1.7     dante 	 * put in the phystokv hash table
    278   1.7     dante 	 * Never gets taken out.
    279   1.7     dante 	 */
    280  1.35    briggs 	ccb->hashkey = htole32(sc->sc_dmamap_control->dm_segs[0].ds_addr +
    281  1.35    briggs 	    ADW_CCB_OFF(ccb));
    282   1.7     dante 	hashnum = CCB_HASH(ccb->hashkey);
    283   1.7     dante 	ccb->nexthash = sc->sc_ccbhash[hashnum];
    284   1.7     dante 	sc->sc_ccbhash[hashnum] = ccb;
    285   1.1     dante 	adw_reset_ccb(ccb);
    286   1.1     dante 	return (0);
    287   1.1     dante }
    288   1.1     dante 
    289   1.1     dante 
    290   1.1     dante /*
    291   1.1     dante  * Get a free ccb
    292   1.1     dante  *
    293   1.1     dante  * If there are none, see if we can allocate a new one
    294   1.1     dante  */
    295   1.1     dante static ADW_CCB *
    296  1.30     lukem adw_get_ccb(ADW_SOFTC *sc)
    297   1.1     dante {
    298   1.1     dante 	ADW_CCB        *ccb = 0;
    299   1.1     dante 	int             s;
    300   1.1     dante 
    301   1.1     dante 	s = splbio();
    302   1.1     dante 
    303  1.29    bouyer 	ccb = sc->sc_free_ccb.tqh_first;
    304  1.29    bouyer 	if (ccb != NULL) {
    305  1.29    bouyer 		TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
    306  1.29    bouyer 		ccb->flags |= CCB_ALLOC;
    307   1.1     dante 	}
    308   1.1     dante 	splx(s);
    309   1.1     dante 	return (ccb);
    310   1.1     dante }
    311   1.1     dante 
    312   1.1     dante 
    313   1.1     dante /*
    314   1.7     dante  * Given a physical address, find the ccb that it corresponds to.
    315   1.7     dante  */
    316   1.7     dante ADW_CCB *
    317  1.30     lukem adw_ccb_phys_kv(ADW_SOFTC *sc, u_int32_t ccb_phys)
    318   1.7     dante {
    319   1.7     dante 	int hashnum = CCB_HASH(ccb_phys);
    320   1.7     dante 	ADW_CCB *ccb = sc->sc_ccbhash[hashnum];
    321   1.7     dante 
    322   1.7     dante 	while (ccb) {
    323   1.7     dante 		if (ccb->hashkey == ccb_phys)
    324   1.7     dante 			break;
    325   1.7     dante 		ccb = ccb->nexthash;
    326   1.7     dante 	}
    327   1.7     dante 	return (ccb);
    328   1.7     dante }
    329   1.7     dante 
    330   1.7     dante 
    331   1.7     dante /*
    332   1.1     dante  * Queue a CCB to be sent to the controller, and send it if possible.
    333   1.1     dante  */
    334  1.13     dante static int
    335  1.30     lukem adw_queue_ccb(ADW_SOFTC *sc, ADW_CCB *ccb)
    336   1.1     dante {
    337  1.19     dante 	int		errcode = ADW_SUCCESS;
    338   1.1     dante 
    339  1.29    bouyer 	TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
    340   1.1     dante 
    341  1.13     dante 	while ((ccb = sc->sc_waiting_ccb.tqh_first) != NULL) {
    342   1.1     dante 
    343  1.29    bouyer 		TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
    344  1.22     dante 		errcode = AdwExeScsiQueue(sc, &ccb->scsiq);
    345  1.13     dante 		switch(errcode) {
    346  1.13     dante 		case ADW_SUCCESS:
    347  1.13     dante 			break;
    348   1.1     dante 
    349  1.13     dante 		case ADW_BUSY:
    350  1.13     dante 			printf("ADW_BUSY\n");
    351  1.13     dante 			return(ADW_BUSY);
    352  1.13     dante 
    353  1.13     dante 		case ADW_ERROR:
    354  1.13     dante 			printf("ADW_ERROR\n");
    355  1.13     dante 			return(ADW_ERROR);
    356  1.13     dante 		}
    357  1.11     dante 
    358  1.19     dante 		TAILQ_INSERT_TAIL(&sc->sc_pending_ccb, ccb, chain);
    359   1.1     dante 
    360  1.12   thorpej 		if ((ccb->xs->xs_control & XS_CTL_POLL) == 0)
    361  1.15   thorpej 			callout_reset(&ccb->xs->xs_callout,
    362  1.38    bouyer 			    mstohz(ccb->timeout), adw_timeout, ccb);
    363   1.1     dante 	}
    364  1.13     dante 
    365  1.13     dante 	return(errcode);
    366   1.1     dante }
    367   1.1     dante 
    368   1.1     dante 
    369   1.1     dante /******************************************************************************/
    370  1.22     dante /*                       SCSI layer interfacing routines                      */
    371   1.1     dante /******************************************************************************/
    372   1.1     dante 
    373   1.1     dante 
    374   1.1     dante int
    375  1.30     lukem adw_init(ADW_SOFTC *sc)
    376   1.1     dante {
    377   1.2     dante 	u_int16_t       warn_code;
    378   1.1     dante 
    379   1.1     dante 
    380   1.1     dante 	sc->cfg.lib_version = (ADW_LIB_VERSION_MAJOR << 8) |
    381   1.2     dante 		ADW_LIB_VERSION_MINOR;
    382   1.1     dante 	sc->cfg.chip_version =
    383   1.1     dante 		ADW_GET_CHIP_VERSION(sc->sc_iot, sc->sc_ioh, sc->bus_type);
    384   1.1     dante 
    385   1.1     dante 	/*
    386   1.1     dante 	 * Reset the chip to start and allow register writes.
    387   1.1     dante 	 */
    388   1.1     dante 	if (ADW_FIND_SIGNATURE(sc->sc_iot, sc->sc_ioh) == 0) {
    389   1.1     dante 		panic("adw_init: adw_find_signature failed");
    390   1.2     dante 	} else {
    391  1.22     dante 		AdwResetChip(sc->sc_iot, sc->sc_ioh);
    392   1.1     dante 
    393  1.23     dante 		warn_code = AdwInitFromEEPROM(sc);
    394  1.13     dante 
    395  1.22     dante 		if (warn_code & ADW_WARN_EEPROM_CHKSUM)
    396  1.52       chs 			aprint_error_dev(sc->sc_dev, "Bad checksum found. "
    397  1.49    cegger 			       "Setting default values\n");
    398  1.22     dante 		if (warn_code & ADW_WARN_EEPROM_TERMINATION)
    399  1.52       chs 			aprint_error_dev(sc->sc_dev, "Bad bus termination setting."
    400  1.49    cegger 			       "Using automatic termination.\n");
    401   1.1     dante 	}
    402   1.1     dante 
    403  1.13     dante 	sc->isr_callback = (ADW_CALLBACK) adw_isr_callback;
    404  1.13     dante 	sc->async_callback = (ADW_CALLBACK) adw_async_callback;
    405   1.1     dante 
    406  1.16     dante 	return 0;
    407   1.1     dante }
    408   1.1     dante 
    409   1.1     dante 
    410   1.1     dante void
    411  1.30     lukem adw_attach(ADW_SOFTC *sc)
    412   1.1     dante {
    413  1.29    bouyer 	struct scsipi_adapter *adapt = &sc->sc_adapter;
    414  1.29    bouyer 	struct scsipi_channel *chan = &sc->sc_channel;
    415  1.29    bouyer 	int             ncontrols, error;
    416   1.1     dante 
    417  1.13     dante 	TAILQ_INIT(&sc->sc_free_ccb);
    418  1.13     dante 	TAILQ_INIT(&sc->sc_waiting_ccb);
    419  1.19     dante 	TAILQ_INIT(&sc->sc_pending_ccb);
    420  1.13     dante 
    421  1.13     dante 	/*
    422  1.13     dante          * Allocate the Control Blocks.
    423  1.13     dante          */
    424  1.13     dante 	error = adw_alloc_controls(sc);
    425  1.13     dante 	if (error)
    426  1.13     dante 		return; /* (error) */ ;
    427  1.13     dante 
    428  1.32   thorpej 	memset(sc->sc_control, 0, sizeof(struct adw_control));
    429  1.13     dante 
    430  1.13     dante 	/*
    431  1.13     dante 	 * Create and initialize the Control Blocks.
    432  1.13     dante 	 */
    433  1.29    bouyer 	ncontrols = adw_create_ccbs(sc, sc->sc_control->ccbs, ADW_MAX_CCB);
    434  1.29    bouyer 	if (ncontrols == 0) {
    435  1.52       chs 		aprint_error_dev(sc->sc_dev, "unable to create Control Blocks\n");
    436  1.13     dante 		return; /* (ENOMEM) */ ;
    437  1.29    bouyer 	} else if (ncontrols != ADW_MAX_CCB) {
    438  1.52       chs 		aprint_error_dev(sc->sc_dev, "WARNING: only %d of %d Control Blocks"
    439  1.13     dante 		       " created\n",
    440  1.49    cegger 		       ncontrols, ADW_MAX_CCB);
    441  1.13     dante 	}
    442  1.13     dante 
    443  1.13     dante 	/*
    444  1.13     dante 	 * Create and initialize the Carriers.
    445  1.13     dante 	 */
    446  1.13     dante 	error = adw_alloc_carriers(sc);
    447  1.13     dante 	if (error)
    448  1.13     dante 		return; /* (error) */ ;
    449  1.13     dante 
    450  1.21     dante 	/*
    451  1.21     dante 	 * Zero's the freeze_device status
    452  1.21     dante 	 */
    453  1.32   thorpej 	 memset(sc->sc_freeze_dev, 0, sizeof(sc->sc_freeze_dev));
    454  1.13     dante 
    455   1.1     dante 	/*
    456  1.16     dante 	 * Initialize the adapter
    457   1.1     dante 	 */
    458  1.23     dante 	switch (AdwInitDriver(sc)) {
    459  1.22     dante 	case ADW_IERR_BIST_PRE_TEST:
    460  1.19     dante 		panic("%s: BIST pre-test error",
    461  1.52       chs 		      device_xname(sc->sc_dev));
    462  1.19     dante 		break;
    463  1.19     dante 
    464  1.22     dante 	case ADW_IERR_BIST_RAM_TEST:
    465  1.19     dante 		panic("%s: BIST RAM test error",
    466  1.52       chs 		      device_xname(sc->sc_dev));
    467  1.19     dante 		break;
    468  1.19     dante 
    469  1.22     dante 	case ADW_IERR_MCODE_CHKSUM:
    470   1.2     dante 		panic("%s: Microcode checksum error",
    471  1.52       chs 		      device_xname(sc->sc_dev));
    472   1.2     dante 		break;
    473   1.2     dante 
    474  1.22     dante 	case ADW_IERR_ILLEGAL_CONNECTION:
    475   1.2     dante 		panic("%s: All three connectors are in use",
    476  1.52       chs 		      device_xname(sc->sc_dev));
    477   1.2     dante 		break;
    478   1.2     dante 
    479  1.22     dante 	case ADW_IERR_REVERSED_CABLE:
    480   1.2     dante 		panic("%s: Cable is reversed",
    481  1.52       chs 		      device_xname(sc->sc_dev));
    482   1.2     dante 		break;
    483   1.2     dante 
    484  1.22     dante 	case ADW_IERR_HVD_DEVICE:
    485  1.19     dante 		panic("%s: HVD attached to LVD connector",
    486  1.52       chs 		      device_xname(sc->sc_dev));
    487  1.19     dante 		break;
    488  1.19     dante 
    489  1.22     dante 	case ADW_IERR_SINGLE_END_DEVICE:
    490   1.2     dante 		panic("%s: single-ended device is attached to"
    491   1.2     dante 		      " one of the connectors",
    492  1.52       chs 		      device_xname(sc->sc_dev));
    493   1.2     dante 		break;
    494  1.13     dante 
    495  1.22     dante 	case ADW_IERR_NO_CARRIER:
    496  1.22     dante 		panic("%s: unable to create Carriers",
    497  1.52       chs 		      device_xname(sc->sc_dev));
    498  1.13     dante 		break;
    499  1.13     dante 
    500  1.22     dante 	case ADW_WARN_BUSRESET_ERROR:
    501  1.52       chs 		aprint_error_dev(sc->sc_dev, "WARNING: Bus Reset Error\n");
    502  1.13     dante 		break;
    503   1.1     dante 	}
    504   1.1     dante 
    505   1.4   thorpej 	/*
    506  1.29    bouyer 	 * Fill in the scsipi_adapter.
    507   1.4   thorpej 	 */
    508  1.29    bouyer 	memset(adapt, 0, sizeof(*adapt));
    509  1.52       chs 	adapt->adapt_dev = sc->sc_dev;
    510  1.29    bouyer 	adapt->adapt_nchannels = 1;
    511  1.29    bouyer 	adapt->adapt_openings = ncontrols;
    512  1.29    bouyer 	adapt->adapt_max_periph = adapt->adapt_openings;
    513  1.29    bouyer 	adapt->adapt_request = adw_scsipi_request;
    514  1.29    bouyer 	adapt->adapt_minphys = adwminphys;
    515   1.1     dante 
    516   1.1     dante 	/*
    517  1.29    bouyer 	 * Fill in the scsipi_channel.
    518  1.29    bouyer 	 */
    519  1.29    bouyer 	memset(chan, 0, sizeof(*chan));
    520  1.45     perry 	chan->chan_adapter = adapt;
    521  1.29    bouyer 	chan->chan_bustype = &scsi_bustype;
    522  1.29    bouyer 	chan->chan_channel = 0;
    523  1.29    bouyer 	chan->chan_ntargets = ADW_MAX_TID + 1;
    524  1.41   mycroft 	chan->chan_nluns = 8;
    525  1.29    bouyer 	chan->chan_id = sc->chip_scsi_id;
    526   1.1     dante 
    527  1.52       chs 	config_found(sc->sc_dev, &sc->sc_channel, scsiprint);
    528   1.1     dante }
    529   1.1     dante 
    530   1.1     dante 
    531   1.1     dante static void
    532  1.30     lukem adwminphys(struct buf *bp)
    533   1.1     dante {
    534   1.1     dante 
    535   1.1     dante 	if (bp->b_bcount > ((ADW_MAX_SG_LIST - 1) * PAGE_SIZE))
    536   1.1     dante 		bp->b_bcount = ((ADW_MAX_SG_LIST - 1) * PAGE_SIZE);
    537   1.1     dante 	minphys(bp);
    538   1.1     dante }
    539   1.1     dante 
    540   1.1     dante 
    541   1.1     dante /*
    542   1.2     dante  * start a scsi operation given the command and the data address.
    543   1.2     dante  * Also needs the unit, target and lu.
    544   1.1     dante  */
    545  1.29    bouyer static void
    546  1.30     lukem adw_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    547  1.30     lukem 	void *arg)
    548  1.29    bouyer {
    549   1.1     dante 	struct scsipi_xfer *xs;
    550  1.52       chs 	ADW_SOFTC      *sc = device_private(chan->chan_adapter->adapt_dev);
    551   1.1     dante 	ADW_CCB        *ccb;
    552  1.29    bouyer 	int            s, retry;
    553   1.1     dante 
    554  1.29    bouyer 	switch (req) {
    555  1.29    bouyer 	case ADAPTER_REQ_RUN_XFER:
    556  1.29    bouyer 		xs = arg;
    557   1.1     dante 
    558  1.29    bouyer 		/*
    559  1.29    bouyer 		 * get a ccb to use. If the transfer
    560  1.29    bouyer 		 * is from a buf (possibly from interrupt time)
    561  1.29    bouyer 		 * then we can't allow it to sleep
    562  1.29    bouyer 		 */
    563   1.1     dante 
    564  1.29    bouyer 		ccb = adw_get_ccb(sc);
    565  1.29    bouyer #ifdef DIAGNOSTIC
    566   1.1     dante 		/*
    567  1.29    bouyer                  * This should never happen as we track the resources
    568  1.29    bouyer 		 * in the mid-layer.
    569   1.1     dante                  */
    570  1.29    bouyer 		if (ccb == NULL) {
    571  1.29    bouyer 			scsipi_printaddr(xs->xs_periph);
    572  1.29    bouyer 			printf("unable to allocate ccb\n");
    573  1.29    bouyer 			panic("adw_scsipi_request");
    574   1.1     dante 		}
    575  1.29    bouyer #endif
    576   1.1     dante 
    577  1.29    bouyer 		ccb->xs = xs;
    578  1.29    bouyer 		ccb->timeout = xs->timeout;
    579   1.1     dante 
    580  1.29    bouyer 		if (adw_build_req(sc, ccb)) {
    581  1.29    bouyer 			s = splbio();
    582  1.29    bouyer 			retry = adw_queue_ccb(sc, ccb);
    583   1.1     dante 			splx(s);
    584   1.1     dante 
    585  1.29    bouyer 			switch(retry) {
    586  1.29    bouyer 			case ADW_BUSY:
    587  1.29    bouyer 				xs->error = XS_RESOURCE_SHORTAGE;
    588  1.29    bouyer 				adw_free_ccb(sc, ccb);
    589  1.29    bouyer 				scsipi_done(xs);
    590  1.29    bouyer 				return;
    591   1.1     dante 
    592  1.29    bouyer 			case ADW_ERROR:
    593  1.29    bouyer 				xs->error = XS_DRIVER_STUFFUP;
    594  1.29    bouyer 				adw_free_ccb(sc, ccb);
    595  1.29    bouyer 				scsipi_done(xs);
    596  1.29    bouyer 				return;
    597  1.29    bouyer 			}
    598  1.29    bouyer 			if ((xs->xs_control & XS_CTL_POLL) == 0)
    599  1.29    bouyer 				return;
    600  1.29    bouyer 			/*
    601  1.29    bouyer 			 * Not allowed to use interrupts, poll for completion.
    602  1.29    bouyer 			 */
    603  1.29    bouyer 			if (adw_poll(sc, xs, ccb->timeout)) {
    604  1.29    bouyer 				adw_timeout(ccb);
    605  1.29    bouyer 				if (adw_poll(sc, xs, ccb->timeout))
    606  1.29    bouyer 					adw_timeout(ccb);
    607  1.29    bouyer 			}
    608  1.13     dante 		}
    609  1.29    bouyer 		return;
    610   1.1     dante 
    611  1.29    bouyer 	case ADAPTER_REQ_GROW_RESOURCES:
    612  1.29    bouyer 		/* XXX Not supported. */
    613  1.29    bouyer 		return;
    614   1.1     dante 
    615  1.29    bouyer 	case ADAPTER_REQ_SET_XFER_MODE:
    616  1.45     perry 		/* XXX XXX XXX */
    617  1.29    bouyer 		return;
    618   1.1     dante 	}
    619   1.1     dante }
    620   1.1     dante 
    621   1.1     dante 
    622   1.1     dante /*
    623   1.1     dante  * Build a request structure for the Wide Boards.
    624   1.1     dante  */
    625   1.1     dante static int
    626  1.30     lukem adw_build_req(ADW_SOFTC *sc, ADW_CCB *ccb)
    627   1.1     dante {
    628  1.29    bouyer 	struct scsipi_xfer *xs = ccb->xs;
    629  1.29    bouyer 	struct scsipi_periph *periph = xs->xs_periph;
    630   1.2     dante 	bus_dma_tag_t   dmat = sc->sc_dmat;
    631   1.2     dante 	ADW_SCSI_REQ_Q *scsiqp;
    632   1.2     dante 	int             error;
    633   1.1     dante 
    634   1.1     dante 	scsiqp = &ccb->scsiq;
    635  1.32   thorpej 	memset(scsiqp, 0, sizeof(ADW_SCSI_REQ_Q));
    636   1.1     dante 
    637   1.1     dante 	/*
    638   1.7     dante 	 * Set the ADW_SCSI_REQ_Q 'ccb_ptr' to point to the
    639   1.7     dante 	 * physical CCB structure.
    640   1.1     dante 	 */
    641  1.10   thorpej 	scsiqp->ccb_ptr = ccb->hashkey;
    642   1.1     dante 
    643   1.1     dante 	/*
    644   1.1     dante 	 * Build the ADW_SCSI_REQ_Q request.
    645   1.1     dante 	 */
    646   1.1     dante 
    647   1.1     dante 	/*
    648   1.1     dante 	 * Set CDB length and copy it to the request structure.
    649  1.16     dante 	 * For wide  boards a CDB length maximum of 16 bytes
    650  1.16     dante 	 * is supported.
    651   1.1     dante 	 */
    652  1.31   thorpej 	memcpy(&scsiqp->cdb, xs->cmd, ((scsiqp->cdb_len = xs->cmdlen) <= 12)?
    653  1.16     dante 			xs->cmdlen : 12 );
    654  1.16     dante 	if(xs->cmdlen > 12)
    655  1.31   thorpej 		memcpy(&scsiqp->cdb16, &(xs->cmd[12]), xs->cmdlen - 12);
    656   1.1     dante 
    657  1.29    bouyer 	scsiqp->target_id = periph->periph_target;
    658  1.29    bouyer 	scsiqp->target_lun = periph->periph_lun;
    659   1.1     dante 
    660   1.7     dante 	scsiqp->vsense_addr = &ccb->scsi_sense;
    661  1.35    briggs 	scsiqp->sense_addr = htole32(sc->sc_dmamap_control->dm_segs[0].ds_addr +
    662  1.35    briggs 			ADW_CCB_OFF(ccb) + offsetof(struct adw_ccb, scsi_sense));
    663  1.44   thorpej 	scsiqp->sense_len = sizeof(struct scsi_sense_data);
    664   1.1     dante 
    665   1.1     dante 	/*
    666   1.1     dante 	 * Build ADW_SCSI_REQ_Q for a scatter-gather buffer command.
    667   1.1     dante 	 */
    668   1.1     dante 	if (xs->datalen) {
    669   1.1     dante 		/*
    670   1.1     dante                  * Map the DMA transfer.
    671   1.1     dante                  */
    672   1.1     dante #ifdef TFS
    673  1.12   thorpej 		if (xs->xs_control & SCSI_DATA_UIO) {
    674  1.29    bouyer 			error = bus_dmamap_load_uio(dmat,
    675  1.29    bouyer 				ccb->dmamap_xfer, (struct uio *) xs->data,
    676  1.29    bouyer 			        ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
    677  1.33   thorpej 			         BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
    678  1.33   thorpej 				 ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
    679  1.33   thorpej 				  BUS_DMA_WRITE));
    680   1.1     dante 		} else
    681  1.13     dante #endif		/* TFS */
    682   1.1     dante 		{
    683  1.29    bouyer 			error = bus_dmamap_load(dmat,
    684  1.29    bouyer 			      ccb->dmamap_xfer, xs->data, xs->datalen, NULL,
    685  1.29    bouyer 			      ((xs->xs_control & XS_CTL_NOSLEEP) ?
    686  1.29    bouyer 			       BUS_DMA_NOWAIT : BUS_DMA_WAITOK) |
    687  1.33   thorpej 			       BUS_DMA_STREAMING |
    688  1.33   thorpej 			       ((xs->xs_control & XS_CTL_DATA_IN) ?
    689  1.33   thorpej 			        BUS_DMA_READ : BUS_DMA_WRITE));
    690   1.1     dante 		}
    691   1.1     dante 
    692  1.29    bouyer 		switch (error) {
    693  1.29    bouyer 		case 0:
    694  1.29    bouyer 			break;
    695  1.29    bouyer 		case ENOMEM:
    696  1.29    bouyer 		case EAGAIN:
    697  1.29    bouyer 			xs->error = XS_RESOURCE_SHORTAGE;
    698  1.45     perry 			goto out_bad;
    699   1.1     dante 
    700  1.29    bouyer 		default:
    701   1.1     dante 			xs->error = XS_DRIVER_STUFFUP;
    702  1.52       chs 			aprint_error_dev(sc->sc_dev, "error %d loading DMA map\n",
    703  1.49    cegger 			    error);
    704  1.29    bouyer out_bad:
    705   1.1     dante 			adw_free_ccb(sc, ccb);
    706  1.29    bouyer 			scsipi_done(xs);
    707  1.29    bouyer 			return(0);
    708   1.1     dante 		}
    709  1.29    bouyer 
    710   1.1     dante 		bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
    711  1.29    bouyer 		    ccb->dmamap_xfer->dm_mapsize,
    712  1.29    bouyer 		    (xs->xs_control & XS_CTL_DATA_IN) ?
    713  1.29    bouyer 		    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    714   1.1     dante 
    715   1.1     dante 		/*
    716   1.1     dante 		 * Build scatter-gather list.
    717   1.1     dante 		 */
    718  1.35    briggs 		scsiqp->data_cnt = htole32(xs->datalen);
    719   1.7     dante 		scsiqp->vdata_addr = xs->data;
    720  1.35    briggs 		scsiqp->data_addr = htole32(ccb->dmamap_xfer->dm_segs[0].ds_addr);
    721  1.32   thorpej 		memset(ccb->sg_block, 0,
    722  1.32   thorpej 		    sizeof(ADW_SG_BLOCK) * ADW_NUM_SG_BLOCK);
    723   1.7     dante 		adw_build_sglist(ccb, scsiqp, ccb->sg_block);
    724   1.1     dante 	} else {
    725   1.1     dante 		/*
    726   1.1     dante                  * No data xfer, use non S/G values.
    727   1.1     dante                  */
    728   1.1     dante 		scsiqp->data_cnt = 0;
    729   1.1     dante 		scsiqp->vdata_addr = 0;
    730   1.1     dante 		scsiqp->data_addr = 0;
    731   1.1     dante 	}
    732   1.1     dante 
    733   1.1     dante 	return (1);
    734   1.1     dante }
    735   1.1     dante 
    736   1.1     dante 
    737   1.1     dante /*
    738   1.1     dante  * Build scatter-gather list for Wide Boards.
    739   1.1     dante  */
    740   1.1     dante static void
    741  1.30     lukem adw_build_sglist(ADW_CCB *ccb, ADW_SCSI_REQ_Q *scsiqp, ADW_SG_BLOCK *sg_block)
    742   1.1     dante {
    743   1.9   thorpej 	u_long          sg_block_next_addr;	/* block and its next */
    744   1.9   thorpej 	u_int32_t       sg_block_physical_addr;
    745  1.13     dante 	int             i;	/* how many SG entries */
    746   1.1     dante 	bus_dma_segment_t *sg_list = &ccb->dmamap_xfer->dm_segs[0];
    747   1.2     dante 	int             sg_elem_cnt = ccb->dmamap_xfer->dm_nsegs;
    748   1.1     dante 
    749   1.1     dante 
    750   1.9   thorpej 	sg_block_next_addr = (u_long) sg_block;	/* allow math operation */
    751  1.35    briggs 	sg_block_physical_addr = le32toh(ccb->hashkey) +
    752  1.10   thorpej 	    offsetof(struct adw_ccb, sg_block[0]);
    753  1.35    briggs 	scsiqp->sg_real_addr = htole32(sg_block_physical_addr);
    754   1.1     dante 
    755   1.1     dante 	/*
    756  1.40       wiz 	 * If there are more than NO_OF_SG_PER_BLOCK DMA segments (hw sg-list)
    757   1.1     dante 	 * then split the request into multiple sg-list blocks.
    758   1.1     dante 	 */
    759   1.1     dante 
    760   1.2     dante 	do {
    761   1.2     dante 		for (i = 0; i < NO_OF_SG_PER_BLOCK; i++) {
    762  1.35    briggs 			sg_block->sg_list[i].sg_addr = htole32(sg_list->ds_addr);
    763  1.35    briggs 			sg_block->sg_list[i].sg_count = htole32(sg_list->ds_len);
    764   1.1     dante 
    765   1.2     dante 			if (--sg_elem_cnt == 0) {
    766   1.1     dante 				/* last entry, get out */
    767  1.27   hpeyerl 				sg_block->sg_cnt = i + 1;
    768  1.42      fvdl 				sg_block->sg_ptr = 0; /* next link = NULL */
    769   1.1     dante 				return;
    770   1.1     dante 			}
    771   1.1     dante 			sg_list++;
    772   1.1     dante 		}
    773   1.1     dante 		sg_block_next_addr += sizeof(ADW_SG_BLOCK);
    774   1.1     dante 		sg_block_physical_addr += sizeof(ADW_SG_BLOCK);
    775   1.1     dante 
    776  1.13     dante 		sg_block->sg_cnt = NO_OF_SG_PER_BLOCK;
    777  1.35    briggs 		sg_block->sg_ptr = htole32(sg_block_physical_addr);
    778   1.2     dante 		sg_block = (ADW_SG_BLOCK *) sg_block_next_addr;	/* virt. addr */
    779  1.10   thorpej 	} while (1);
    780   1.1     dante }
    781   1.1     dante 
    782   1.1     dante 
    783  1.22     dante /******************************************************************************/
    784  1.22     dante /*                       Interrupts and TimeOut routines                      */
    785  1.22     dante /******************************************************************************/
    786  1.22     dante 
    787  1.22     dante 
    788   1.1     dante int
    789  1.30     lukem adw_intr(void *arg)
    790   1.1     dante {
    791   1.1     dante 	ADW_SOFTC      *sc = arg;
    792   1.1     dante 
    793   1.1     dante 
    794  1.22     dante 	if(AdwISR(sc) != ADW_FALSE) {
    795  1.16     dante 		return (1);
    796  1.13     dante 	}
    797   1.1     dante 
    798  1.16     dante 	return (0);
    799   1.1     dante }
    800   1.1     dante 
    801   1.1     dante 
    802   1.1     dante /*
    803   1.1     dante  * Poll a particular unit, looking for a particular xs
    804   1.1     dante  */
    805   1.1     dante static int
    806  1.30     lukem adw_poll(ADW_SOFTC *sc, struct scsipi_xfer *xs, int count)
    807   1.1     dante {
    808   1.1     dante 
    809   1.1     dante 	/* timeouts are in msec, so we loop in 1000 usec cycles */
    810   1.1     dante 	while (count) {
    811   1.1     dante 		adw_intr(sc);
    812  1.12   thorpej 		if (xs->xs_status & XS_STS_DONE)
    813   1.1     dante 			return (0);
    814   1.1     dante 		delay(1000);	/* only happens in boot so ok */
    815   1.1     dante 		count--;
    816   1.1     dante 	}
    817   1.1     dante 	return (1);
    818   1.1     dante }
    819   1.1     dante 
    820   1.1     dante 
    821   1.1     dante static void
    822  1.30     lukem adw_timeout(void *arg)
    823   1.1     dante {
    824   1.1     dante 	ADW_CCB        *ccb = arg;
    825   1.1     dante 	struct scsipi_xfer *xs = ccb->xs;
    826  1.29    bouyer 	struct scsipi_periph *periph = xs->xs_periph;
    827  1.29    bouyer 	ADW_SOFTC      *sc =
    828  1.52       chs 	    device_private(periph->periph_channel->chan_adapter->adapt_dev);
    829   1.1     dante 	int             s;
    830   1.1     dante 
    831  1.29    bouyer 	scsipi_printaddr(periph);
    832   1.1     dante 	printf("timed out");
    833   1.1     dante 
    834   1.1     dante 	s = splbio();
    835   1.1     dante 
    836  1.11     dante 	if (ccb->flags & CCB_ABORTED) {
    837  1.11     dante 	/*
    838  1.11     dante 	 * Abort Timed Out
    839  1.19     dante 	 *
    840  1.20     dante 	 * No more opportunities. Lets try resetting the bus and
    841  1.20     dante 	 * reinitialize the host adapter.
    842  1.11     dante 	 */
    843  1.19     dante 		callout_stop(&xs->xs_callout);
    844  1.11     dante 		printf(" AGAIN. Resetting SCSI Bus\n");
    845  1.22     dante 		adw_reset_bus(sc);
    846  1.19     dante 		splx(s);
    847  1.19     dante 		return;
    848  1.19     dante 	} else if (ccb->flags & CCB_ABORTING) {
    849  1.19     dante 	/*
    850  1.20     dante 	 * Abort the operation that has timed out.
    851  1.19     dante 	 *
    852  1.19     dante 	 * Second opportunity.
    853  1.19     dante 	 */
    854  1.19     dante 		printf("\n");
    855  1.19     dante 		xs->error = XS_TIMEOUT;
    856  1.19     dante 		ccb->flags |= CCB_ABORTED;
    857  1.19     dante #if 0
    858  1.19     dante 		/*
    859  1.19     dante 		 * - XXX - 3.3a microcode is BROKEN!!!
    860  1.19     dante 		 *
    861  1.19     dante 		 * We cannot abort a CCB, so we can only hope the command
    862  1.19     dante 		 * get completed before the next timeout, otherwise a
    863  1.19     dante 		 * Bus Reset will arrive inexorably.
    864  1.19     dante 		 */
    865  1.19     dante 		/*
    866  1.19     dante 		 * ADW_ABORT_CCB() makes the board to generate an interrupt
    867  1.19     dante 		 *
    868  1.19     dante 		 * - XXX - The above assertion MUST be verified (and this
    869  1.19     dante 		 *         code changed as well [callout_*()]), when the
    870  1.19     dante 		 *         ADW_ABORT_CCB will be working again
    871  1.19     dante 		 */
    872  1.19     dante 		ADW_ABORT_CCB(sc, ccb);
    873  1.19     dante #endif
    874  1.19     dante 		/*
    875  1.19     dante 		 * waiting for multishot callout_reset() let's restart it
    876  1.43       wiz 		 * by hand so the next time a timeout event will occur
    877  1.19     dante 		 * we will reset the bus.
    878  1.19     dante 		 */
    879  1.19     dante 		callout_reset(&xs->xs_callout,
    880  1.38    bouyer 			    mstohz(ccb->timeout), adw_timeout, ccb);
    881   1.1     dante 	} else {
    882  1.11     dante 	/*
    883  1.20     dante 	 * Abort the operation that has timed out.
    884  1.19     dante 	 *
    885  1.19     dante 	 * First opportunity.
    886  1.11     dante 	 */
    887   1.1     dante 		printf("\n");
    888  1.11     dante 		xs->error = XS_TIMEOUT;
    889  1.11     dante 		ccb->flags |= CCB_ABORTING;
    890  1.19     dante #if 0
    891  1.19     dante 		/*
    892  1.19     dante 		 * - XXX - 3.3a microcode is BROKEN!!!
    893  1.19     dante 		 *
    894  1.19     dante 		 * We cannot abort a CCB, so we can only hope the command
    895  1.19     dante 		 * get completed before the next 2 timeout, otherwise a
    896  1.19     dante 		 * Bus Reset will arrive inexorably.
    897  1.19     dante 		 */
    898  1.19     dante 		/*
    899  1.19     dante 		 * ADW_ABORT_CCB() makes the board to generate an interrupt
    900  1.19     dante 		 *
    901  1.19     dante 		 * - XXX - The above assertion MUST be verified (and this
    902  1.19     dante 		 *         code changed as well [callout_*()]), when the
    903  1.19     dante 		 *         ADW_ABORT_CCB will be working again
    904  1.19     dante 		 */
    905   1.1     dante 		ADW_ABORT_CCB(sc, ccb);
    906  1.19     dante #endif
    907  1.19     dante 		/*
    908  1.19     dante 		 * waiting for multishot callout_reset() let's restart it
    909  1.20     dante 		 * by hand so to give a second opportunity to the command
    910  1.20     dante 		 * which timed-out.
    911  1.19     dante 		 */
    912  1.19     dante 		callout_reset(&xs->xs_callout,
    913  1.38    bouyer 			    mstohz(ccb->timeout), adw_timeout, ccb);
    914   1.1     dante 	}
    915   1.1     dante 
    916   1.1     dante 	splx(s);
    917   1.1     dante }
    918   1.1     dante 
    919   1.1     dante 
    920  1.21     dante static void
    921  1.30     lukem adw_reset_bus(ADW_SOFTC *sc)
    922  1.21     dante {
    923  1.21     dante 	ADW_CCB	*ccb;
    924  1.21     dante 	int	 s;
    925  1.29    bouyer 	struct scsipi_xfer *xs;
    926  1.21     dante 
    927  1.21     dante 	s = splbio();
    928  1.22     dante 	AdwResetSCSIBus(sc);
    929  1.21     dante 	while((ccb = TAILQ_LAST(&sc->sc_pending_ccb,
    930  1.21     dante 			adw_pending_ccb)) != NULL) {
    931  1.21     dante 		callout_stop(&ccb->xs->xs_callout);
    932  1.21     dante 		TAILQ_REMOVE(&sc->sc_pending_ccb, ccb, chain);
    933  1.29    bouyer 		xs = ccb->xs;
    934  1.29    bouyer 		adw_free_ccb(sc, ccb);
    935  1.29    bouyer 		xs->error = XS_RESOURCE_SHORTAGE;
    936  1.29    bouyer 		scsipi_done(xs);
    937  1.21     dante 	}
    938  1.21     dante 	splx(s);
    939  1.21     dante }
    940  1.21     dante 
    941  1.21     dante 
    942   1.1     dante /******************************************************************************/
    943  1.19     dante /*              Host Adapter and Peripherals Information Routines             */
    944  1.19     dante /******************************************************************************/
    945  1.19     dante 
    946  1.19     dante 
    947  1.19     dante static void
    948  1.30     lukem adw_print_info(ADW_SOFTC *sc, int tid)
    949  1.19     dante {
    950  1.19     dante 	bus_space_tag_t iot = sc->sc_iot;
    951  1.19     dante 	bus_space_handle_t ioh = sc->sc_ioh;
    952  1.19     dante 	u_int16_t wdtr_able, wdtr_done, wdtr;
    953  1.19     dante     	u_int16_t sdtr_able, sdtr_done, sdtr, period;
    954  1.20     dante 	static int wdtr_reneg = 0, sdtr_reneg = 0;
    955  1.20     dante 
    956  1.20     dante 	if (tid == 0){
    957  1.20     dante 		wdtr_reneg = sdtr_reneg = 0;
    958  1.20     dante 	}
    959  1.19     dante 
    960  1.52       chs 	printf("%s: target %d ", device_xname(sc->sc_dev), tid);
    961  1.19     dante 
    962  1.22     dante 	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE, wdtr_able);
    963  1.19     dante 	if(wdtr_able & ADW_TID_TO_TIDMASK(tid)) {
    964  1.22     dante 		ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_DONE, wdtr_done);
    965  1.22     dante 		ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_DEVICE_HSHK_CFG_TABLE +
    966  1.19     dante 			(2 * tid), wdtr);
    967  1.19     dante 		printf("using %d-bits wide, ", (wdtr & 0x8000)? 16 : 8);
    968  1.19     dante 		if((wdtr_done & ADW_TID_TO_TIDMASK(tid)) == 0)
    969  1.19     dante 			wdtr_reneg = 1;
    970  1.19     dante 	} else {
    971  1.19     dante 		printf("wide transfers disabled, ");
    972  1.19     dante 	}
    973  1.19     dante 
    974  1.22     dante 	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE, sdtr_able);
    975  1.19     dante 	if(sdtr_able & ADW_TID_TO_TIDMASK(tid)) {
    976  1.22     dante 		ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_DONE, sdtr_done);
    977  1.22     dante 		ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_DEVICE_HSHK_CFG_TABLE +
    978  1.19     dante 			(2 * tid), sdtr);
    979  1.19     dante 		sdtr &=  ~0x8000;
    980  1.19     dante 		if((sdtr & 0x1F) != 0) {
    981  1.19     dante 			if((sdtr & 0x1F00) == 0x1100){
    982  1.19     dante 				printf("80.0 MHz");
    983  1.19     dante 			} else if((sdtr & 0x1F00) == 0x1000){
    984  1.19     dante 				printf("40.0 MHz");
    985  1.19     dante 			} else {
    986  1.19     dante 				/* <= 20.0 MHz */
    987  1.19     dante 				period = (((sdtr >> 8) * 25) + 50)/4;
    988  1.19     dante 				if(period == 0) {
    989  1.19     dante 					/* Should never happen. */
    990  1.19     dante 					printf("? MHz");
    991  1.19     dante 				} else {
    992  1.19     dante 					printf("%d.%d MHz", 250/period,
    993  1.19     dante 						ADW_TENTHS(250, period));
    994  1.19     dante 				}
    995  1.19     dante 			}
    996  1.19     dante 			printf(" synchronous transfers\n");
    997  1.19     dante 		} else {
    998  1.19     dante 			printf("asynchronous transfers\n");
    999  1.19     dante 		}
   1000  1.19     dante 		if((sdtr_done & ADW_TID_TO_TIDMASK(tid)) == 0)
   1001  1.19     dante 			sdtr_reneg = 1;
   1002  1.19     dante 	} else {
   1003  1.19     dante 		printf("synchronous transfers disabled\n");
   1004  1.19     dante 	}
   1005  1.19     dante 
   1006  1.19     dante 	if(wdtr_reneg || sdtr_reneg) {
   1007  1.52       chs 		printf("%s: target %d %s", device_xname(sc->sc_dev), tid,
   1008  1.19     dante 			(wdtr_reneg)? ((sdtr_reneg)? "wide/sync" : "wide") :
   1009  1.19     dante 			((sdtr_reneg)? "sync" : "") );
   1010  1.19     dante 		printf(" renegotiation pending before next command.\n");
   1011  1.19     dante 	}
   1012  1.45     perry }
   1013  1.19     dante 
   1014  1.19     dante 
   1015  1.19     dante /******************************************************************************/
   1016  1.19     dante /*                        WIDE boards Interrupt callbacks                     */
   1017   1.1     dante /******************************************************************************/
   1018   1.1     dante 
   1019   1.1     dante 
   1020   1.1     dante /*
   1021  1.22     dante  * adw_isr_callback() - Second Level Interrupt Handler called by AdwISR()
   1022   1.1     dante  *
   1023   1.1     dante  * Interrupt callback function for the Wide SCSI Adv Library.
   1024  1.19     dante  *
   1025  1.19     dante  * Notice:
   1026  1.22     dante  * Interrupts are disabled by the caller (AdwISR() function), and will be
   1027  1.19     dante  * enabled at the end of the caller.
   1028   1.1     dante  */
   1029   1.1     dante static void
   1030  1.30     lukem adw_isr_callback(ADW_SOFTC *sc, ADW_SCSI_REQ_Q *scsiq)
   1031   1.1     dante {
   1032   1.2     dante 	bus_dma_tag_t   dmat = sc->sc_dmat;
   1033   1.7     dante 	ADW_CCB        *ccb;
   1034   1.7     dante 	struct scsipi_xfer *xs;
   1035  1.44   thorpej 	struct scsi_sense_data *s1, *s2;
   1036   1.1     dante 
   1037   1.7     dante 
   1038   1.7     dante 	ccb = adw_ccb_phys_kv(sc, scsiq->ccb_ptr);
   1039  1.11     dante 
   1040  1.15   thorpej 	callout_stop(&ccb->xs->xs_callout);
   1041  1.11     dante 
   1042   1.7     dante 	xs = ccb->xs;
   1043   1.1     dante 
   1044   1.1     dante 	/*
   1045   1.1     dante          * If we were a data transfer, unload the map that described
   1046   1.1     dante          * the data buffer.
   1047   1.1     dante          */
   1048   1.1     dante 	if (xs->datalen) {
   1049   1.1     dante 		bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
   1050   1.1     dante 				ccb->dmamap_xfer->dm_mapsize,
   1051  1.12   thorpej 			 (xs->xs_control & XS_CTL_DATA_IN) ?
   1052  1.12   thorpej 			 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1053   1.1     dante 		bus_dmamap_unload(dmat, ccb->dmamap_xfer);
   1054   1.1     dante 	}
   1055  1.20     dante 
   1056   1.1     dante 	if ((ccb->flags & CCB_ALLOC) == 0) {
   1057  1.52       chs 		aprint_error_dev(sc->sc_dev, "exiting ccb not allocated!\n");
   1058   1.1     dante 		Debugger();
   1059   1.1     dante 		return;
   1060   1.1     dante 	}
   1061  1.20     dante 
   1062   1.1     dante 	/*
   1063   1.1     dante 	 * 'done_status' contains the command's ending status.
   1064  1.43       wiz 	 * 'host_status' contains the host adapter status.
   1065  1.20     dante 	 * 'scsi_status' contains the scsi peripheral status.
   1066   1.1     dante 	 */
   1067  1.21     dante 	if ((scsiq->host_status == QHSTA_NO_ERROR) &&
   1068  1.21     dante 	   ((scsiq->done_status == QD_NO_ERROR) ||
   1069  1.22     dante 	    (scsiq->done_status == QD_WITH_ERROR))) {
   1070  1.34     dante 		switch (scsiq->scsi_status) {
   1071  1.21     dante 		case SCSI_STATUS_GOOD:
   1072  1.21     dante 			if ((scsiq->cdb[0] == INQUIRY) &&
   1073  1.21     dante 			    (scsiq->target_lun == 0)) {
   1074  1.21     dante 				adw_print_info(sc, scsiq->target_id);
   1075  1.21     dante 			}
   1076  1.21     dante 			xs->error = XS_NOERROR;
   1077  1.35    briggs 			xs->resid = le32toh(scsiq->data_cnt);
   1078  1.21     dante 			sc->sc_freeze_dev[scsiq->target_id] = 0;
   1079  1.21     dante 			break;
   1080  1.21     dante 
   1081  1.21     dante 		case SCSI_STATUS_CHECK_CONDITION:
   1082  1.21     dante 		case SCSI_STATUS_CMD_TERMINATED:
   1083  1.21     dante 			s1 = &ccb->scsi_sense;
   1084  1.21     dante 			s2 = &xs->sense.scsi_sense;
   1085  1.21     dante 			*s2 = *s1;
   1086  1.21     dante 			xs->error = XS_SENSE;
   1087  1.21     dante 			sc->sc_freeze_dev[scsiq->target_id] = 1;
   1088  1.21     dante 			break;
   1089  1.21     dante 
   1090  1.21     dante 		default:
   1091  1.21     dante 			xs->error = XS_BUSY;
   1092  1.21     dante 			sc->sc_freeze_dev[scsiq->target_id] = 1;
   1093  1.21     dante 			break;
   1094  1.20     dante 		}
   1095  1.21     dante 	} else if (scsiq->done_status == QD_ABORTED_BY_HOST) {
   1096  1.21     dante 		xs->error = XS_DRIVER_STUFFUP;
   1097  1.21     dante 	} else {
   1098  1.21     dante 		switch (scsiq->host_status) {
   1099  1.21     dante 		case QHSTA_M_SEL_TIMEOUT:
   1100  1.21     dante 			xs->error = XS_SELTIMEOUT;
   1101  1.21     dante 			break;
   1102  1.21     dante 
   1103  1.21     dante 		case QHSTA_M_SXFR_OFF_UFLW:
   1104  1.21     dante 		case QHSTA_M_SXFR_OFF_OFLW:
   1105  1.21     dante 		case QHSTA_M_DATA_OVER_RUN:
   1106  1.52       chs 			aprint_error_dev(sc->sc_dev, "Overrun/Overflow/Underflow condition\n");
   1107  1.21     dante 			xs->error = XS_DRIVER_STUFFUP;
   1108  1.21     dante 			break;
   1109  1.21     dante 
   1110  1.21     dante 		case QHSTA_M_SXFR_DESELECTED:
   1111  1.21     dante 		case QHSTA_M_UNEXPECTED_BUS_FREE:
   1112  1.52       chs 			aprint_error_dev(sc->sc_dev, "Unexpected BUS free\n");
   1113  1.21     dante 			xs->error = XS_DRIVER_STUFFUP;
   1114  1.21     dante 			break;
   1115  1.21     dante 
   1116  1.21     dante 		case QHSTA_M_SCSI_BUS_RESET:
   1117  1.21     dante 		case QHSTA_M_SCSI_BUS_RESET_UNSOL:
   1118  1.52       chs 			aprint_error_dev(sc->sc_dev, "BUS Reset\n");
   1119  1.21     dante 			xs->error = XS_DRIVER_STUFFUP;
   1120  1.21     dante 			break;
   1121   1.1     dante 
   1122  1.21     dante 		case QHSTA_M_BUS_DEVICE_RESET:
   1123  1.52       chs 			aprint_error_dev(sc->sc_dev, "Device Reset\n");
   1124  1.21     dante 			xs->error = XS_DRIVER_STUFFUP;
   1125  1.21     dante 			break;
   1126  1.20     dante 
   1127  1.21     dante 		case QHSTA_M_QUEUE_ABORTED:
   1128  1.52       chs 			aprint_error_dev(sc->sc_dev, "Queue Aborted\n");
   1129  1.21     dante 			xs->error = XS_DRIVER_STUFFUP;
   1130   1.1     dante 			break;
   1131   1.1     dante 
   1132  1.20     dante 		case QHSTA_M_SXFR_SDMA_ERR:
   1133  1.21     dante 		case QHSTA_M_SXFR_SXFR_PERR:
   1134  1.21     dante 		case QHSTA_M_RDMA_PERR:
   1135  1.20     dante 			/*
   1136  1.21     dante 			 * DMA Error. This should *NEVER* happen!
   1137  1.20     dante 			 *
   1138  1.20     dante 			 * Lets try resetting the bus and reinitialize
   1139  1.20     dante 			 * the host adapter.
   1140  1.20     dante 			 */
   1141  1.52       chs 			aprint_error_dev(sc->sc_dev, "DMA Error. Reseting bus\n");
   1142  1.22     dante 			TAILQ_REMOVE(&sc->sc_pending_ccb, ccb, chain);
   1143  1.22     dante 			adw_reset_bus(sc);
   1144  1.21     dante 			xs->error = XS_BUSY;
   1145  1.22     dante 			goto done;
   1146  1.45     perry 
   1147  1.21     dante 		case QHSTA_M_WTM_TIMEOUT:
   1148  1.21     dante 		case QHSTA_M_SXFR_WD_TMO:
   1149  1.21     dante 			/* The SCSI bus hung in a phase */
   1150  1.21     dante 			printf("%s: Watch Dog timer expired. Reseting bus\n",
   1151  1.52       chs 				device_xname(sc->sc_dev));
   1152  1.22     dante 			TAILQ_REMOVE(&sc->sc_pending_ccb, ccb, chain);
   1153  1.22     dante 			adw_reset_bus(sc);
   1154  1.21     dante 			xs->error = XS_BUSY;
   1155  1.22     dante 			goto done;
   1156  1.21     dante 
   1157  1.21     dante 		case QHSTA_M_SXFR_XFR_PH_ERR:
   1158  1.52       chs 			aprint_error_dev(sc->sc_dev, "Transfer Error\n");
   1159  1.21     dante 			xs->error = XS_DRIVER_STUFFUP;
   1160  1.21     dante 			break;
   1161  1.21     dante 
   1162  1.21     dante 		case QHSTA_M_BAD_CMPL_STATUS_IN:
   1163  1.21     dante 			/* No command complete after a status message */
   1164  1.21     dante 			printf("%s: Bad Completion Status\n",
   1165  1.52       chs 				device_xname(sc->sc_dev));
   1166  1.21     dante 			xs->error = XS_DRIVER_STUFFUP;
   1167  1.21     dante 			break;
   1168  1.21     dante 
   1169  1.21     dante 		case QHSTA_M_AUTO_REQ_SENSE_FAIL:
   1170  1.52       chs 			aprint_error_dev(sc->sc_dev, "Auto Sense Failed\n");
   1171  1.21     dante 			xs->error = XS_DRIVER_STUFFUP;
   1172  1.21     dante 			break;
   1173  1.21     dante 
   1174  1.21     dante 		case QHSTA_M_INVALID_DEVICE:
   1175  1.52       chs 			aprint_error_dev(sc->sc_dev, "Invalid Device\n");
   1176  1.21     dante 			xs->error = XS_DRIVER_STUFFUP;
   1177  1.21     dante 			break;
   1178  1.11     dante 
   1179  1.21     dante 		case QHSTA_M_NO_AUTO_REQ_SENSE:
   1180  1.21     dante 			/*
   1181  1.21     dante 			 * User didn't request sense, but we got a
   1182  1.21     dante 			 * check condition.
   1183  1.21     dante 			 */
   1184  1.52       chs 			aprint_error_dev(sc->sc_dev, "Unexpected Check Condition\n");
   1185   1.1     dante 			xs->error = XS_DRIVER_STUFFUP;
   1186   1.1     dante 			break;
   1187   1.1     dante 
   1188  1.21     dante 		case QHSTA_M_SXFR_UNKNOWN_ERROR:
   1189  1.52       chs 			aprint_error_dev(sc->sc_dev, "Unknown Error\n");
   1190  1.21     dante 			xs->error = XS_DRIVER_STUFFUP;
   1191  1.21     dante 			break;
   1192  1.11     dante 
   1193  1.21     dante 		default:
   1194  1.21     dante 			panic("%s: Unhandled Host Status Error %x",
   1195  1.52       chs 			      device_xname(sc->sc_dev), scsiq->host_status);
   1196  1.21     dante 		}
   1197   1.1     dante 	}
   1198   1.1     dante 
   1199  1.19     dante 	TAILQ_REMOVE(&sc->sc_pending_ccb, ccb, chain);
   1200  1.22     dante done:	adw_free_ccb(sc, ccb);
   1201   1.1     dante 	scsipi_done(xs);
   1202  1.11     dante }
   1203  1.11     dante 
   1204  1.11     dante 
   1205  1.13     dante /*
   1206  1.22     dante  * adw_async_callback() - Adv Library asynchronous event callback function.
   1207  1.13     dante  */
   1208  1.11     dante static void
   1209  1.30     lukem adw_async_callback(ADW_SOFTC *sc, u_int8_t code)
   1210  1.11     dante {
   1211  1.13     dante 	switch (code) {
   1212  1.13     dante 	case ADV_ASYNC_SCSI_BUS_RESET_DET:
   1213  1.21     dante 		/* The firmware detected a SCSI Bus reset. */
   1214  1.52       chs 		printf("%s: SCSI Bus reset detected\n", device_xname(sc->sc_dev));
   1215  1.13     dante 		break;
   1216  1.13     dante 
   1217  1.13     dante 	case ADV_ASYNC_RDMA_FAILURE:
   1218  1.13     dante 		/*
   1219  1.13     dante 		 * Handle RDMA failure by resetting the SCSI Bus and
   1220  1.19     dante 		 * possibly the chip if it is unresponsive.
   1221  1.13     dante 		 */
   1222  1.20     dante 		printf("%s: RDMA failure. Resetting the SCSI Bus and"
   1223  1.52       chs 				" the adapter\n", device_xname(sc->sc_dev));
   1224  1.22     dante 		AdwResetSCSIBus(sc);
   1225  1.13     dante 		break;
   1226  1.13     dante 
   1227  1.13     dante 	case ADV_HOST_SCSI_BUS_RESET:
   1228  1.21     dante 		/* Host generated SCSI bus reset occurred. */
   1229  1.19     dante 		printf("%s: Host generated SCSI bus reset occurred\n",
   1230  1.52       chs 				device_xname(sc->sc_dev));
   1231  1.19     dante 		break;
   1232  1.19     dante 
   1233  1.19     dante 	case ADV_ASYNC_CARRIER_READY_FAILURE:
   1234  1.21     dante 		/* Carrier Ready failure. */
   1235  1.52       chs 		printf("%s: Carrier Ready failure!\n", device_xname(sc->sc_dev));
   1236  1.19     dante 		break;
   1237  1.13     dante 
   1238  1.13     dante 	default:
   1239  1.13     dante 		break;
   1240  1.13     dante 	}
   1241   1.1     dante }
   1242