Home | History | Annotate | Line # | Download | only in ic
iha.c revision 1.4.2.1
      1  1.4.2.1    lukem /*	$NetBSD: iha.c,v 1.4.2.1 2001/08/03 04:13:00 lukem Exp $ */
      2      1.1  tsutsui /*
      3      1.1  tsutsui  * Initio INI-9xxxU/UW SCSI Device Driver
      4      1.1  tsutsui  *
      5      1.1  tsutsui  * Copyright (c) 2000 Ken Westerback
      6      1.1  tsutsui  * All rights reserved.
      7      1.1  tsutsui  *
      8      1.1  tsutsui  * Redistribution and use in source and binary forms, with or without
      9      1.1  tsutsui  * modification, are permitted provided that the following conditions
     10      1.1  tsutsui  * are met:
     11      1.1  tsutsui  * 1. Redistributions of source code must retain the above copyright
     12      1.1  tsutsui  *    notice, this list of conditions and the following disclaimer,
     13      1.1  tsutsui  *    without modification, immediately at the beginning of the file.
     14      1.1  tsutsui  * 2. The name of the author may not be used to endorse or promote products
     15      1.1  tsutsui  *    derived from this software without specific prior written permission.
     16      1.1  tsutsui  *
     17      1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18      1.1  tsutsui  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19      1.1  tsutsui  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20      1.1  tsutsui  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
     21      1.1  tsutsui  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22      1.1  tsutsui  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     23      1.1  tsutsui  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24      1.1  tsutsui  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     25      1.1  tsutsui  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     26      1.1  tsutsui  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     27      1.1  tsutsui  * THE POSSIBILITY OF SUCH DAMAGE.
     28      1.1  tsutsui  *
     29      1.1  tsutsui  *-------------------------------------------------------------------------
     30      1.1  tsutsui  *
     31      1.1  tsutsui  * Ported from i91u.c, provided by Initio Corporation, which credits:
     32      1.1  tsutsui  *
     33  1.4.2.1    lukem  * Device driver for the INI-9XXXU/UW or INIC-940/950 PCI SCSI Controller.
     34      1.1  tsutsui  *
     35      1.1  tsutsui  * FreeBSD
     36      1.1  tsutsui  *
     37      1.1  tsutsui  *  Written for 386bsd and FreeBSD by
     38      1.1  tsutsui  *	Winston Hung		<winstonh (at) initio.com>
     39      1.1  tsutsui  *
     40      1.1  tsutsui  * Copyright (c) 1997-99 Initio Corp.  All rights reserved.
     41      1.1  tsutsui  *
     42      1.1  tsutsui  *-------------------------------------------------------------------------
     43      1.1  tsutsui  */
     44      1.1  tsutsui 
     45      1.1  tsutsui /*
     46      1.1  tsutsui  * Ported to NetBSD by Izumi Tsutsui <tsutsui (at) ceres.dti.ne.jp> from OpenBSD:
     47      1.1  tsutsui  * $OpenBSD: iha.c,v 1.3 2001/02/20 00:47:33 krw Exp $
     48      1.1  tsutsui  */
     49      1.1  tsutsui 
     50      1.1  tsutsui #include <sys/param.h>
     51      1.1  tsutsui #include <sys/systm.h>
     52      1.1  tsutsui #include <sys/kernel.h>
     53      1.1  tsutsui #include <sys/buf.h>
     54      1.1  tsutsui #include <sys/device.h>
     55      1.1  tsutsui #include <sys/malloc.h>
     56      1.1  tsutsui 
     57      1.1  tsutsui #include <uvm/uvm_extern.h>
     58      1.1  tsutsui 
     59      1.1  tsutsui #include <machine/bus.h>
     60      1.1  tsutsui #include <machine/intr.h>
     61      1.1  tsutsui 
     62      1.1  tsutsui #include <dev/scsipi/scsi_all.h>
     63      1.1  tsutsui #include <dev/scsipi/scsipi_all.h>
     64      1.1  tsutsui #include <dev/scsipi/scsiconf.h>
     65      1.1  tsutsui #include <dev/scsipi/scsi_message.h>
     66      1.1  tsutsui 
     67      1.1  tsutsui #include <dev/ic/ihareg.h>
     68      1.1  tsutsui #include <dev/ic/ihavar.h>
     69      1.1  tsutsui 
     70      1.1  tsutsui /*
     71      1.1  tsutsui  * SCSI Rate Table, indexed by FLAG_SCSI_RATE field of
     72      1.1  tsutsui  * tcs flags.
     73      1.1  tsutsui  */
     74  1.4.2.1    lukem static const u_int8_t iha_rate_tbl[8] = {
     75      1.1  tsutsui 	/* fast 20		  */
     76      1.1  tsutsui 	/* nanosecond divide by 4 */
     77      1.1  tsutsui 	12,	/* 50ns,  20M	  */
     78      1.1  tsutsui 	18,	/* 75ns,  13.3M	  */
     79      1.1  tsutsui 	25,	/* 100ns, 10M	  */
     80      1.1  tsutsui 	31,	/* 125ns, 8M	  */
     81      1.1  tsutsui 	37,	/* 150ns, 6.6M	  */
     82      1.1  tsutsui 	43,	/* 175ns, 5.7M	  */
     83      1.1  tsutsui 	50,	/* 200ns, 5M	  */
     84      1.1  tsutsui 	62	/* 250ns, 4M	  */
     85      1.1  tsutsui };
     86      1.1  tsutsui 
     87  1.4.2.1    lukem #ifdef notused
     88      1.1  tsutsui static u_int16_t eeprom_default[EEPROM_SIZE] = {
     89      1.1  tsutsui 	/* -- Header ------------------------------------ */
     90      1.1  tsutsui 	/* signature */
     91      1.1  tsutsui 	EEP_SIGNATURE,
     92      1.1  tsutsui 	/* size, revision */
     93      1.1  tsutsui 	EEP_WORD(EEPROM_SIZE * 2, 0x01),
     94      1.1  tsutsui 	/* -- Host Adapter Structure -------------------- */
     95      1.1  tsutsui 	/* model */
     96      1.1  tsutsui 	0x0095,
     97      1.1  tsutsui 	/* model info, number of channel */
     98      1.1  tsutsui 	EEP_WORD(0x00, 1),
     99      1.1  tsutsui 	/* BIOS config */
    100      1.1  tsutsui 	EEP_BIOSCFG_DEFAULT,
    101      1.1  tsutsui 	/* host adapter config */
    102      1.1  tsutsui 	0,
    103      1.1  tsutsui 
    104      1.1  tsutsui 	/* -- eeprom_adapter[0] ------------------------------- */
    105      1.1  tsutsui 	/* ID, adapter config 1 */
    106      1.1  tsutsui 	EEP_WORD(7, CFG_DEFAULT),
    107      1.1  tsutsui 	/* adapter config 2, number of targets */
    108      1.1  tsutsui 	EEP_WORD(0x00, 8),
    109      1.1  tsutsui 	/* target flags */
    110      1.1  tsutsui 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    111      1.1  tsutsui 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    112      1.1  tsutsui 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    113      1.1  tsutsui 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    114      1.1  tsutsui 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    115      1.1  tsutsui 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    116      1.1  tsutsui 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    117      1.1  tsutsui 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    118      1.1  tsutsui 
    119      1.1  tsutsui 	/* -- eeprom_adapter[1] ------------------------------- */
    120      1.1  tsutsui 	/* ID, adapter config 1 */
    121      1.1  tsutsui 	EEP_WORD(7, CFG_DEFAULT),
    122      1.1  tsutsui 	/* adapter config 2, number of targets */
    123      1.1  tsutsui 	EEP_WORD(0x00, 8),
    124      1.1  tsutsui 	/* target flags */
    125      1.1  tsutsui 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    126      1.1  tsutsui 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    127      1.1  tsutsui 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    128      1.1  tsutsui 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    129      1.1  tsutsui 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    130      1.1  tsutsui 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    131      1.1  tsutsui 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    132      1.1  tsutsui 	EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
    133      1.1  tsutsui 	/* reserved[5] */
    134      1.1  tsutsui 	0, 0, 0, 0, 0,
    135      1.1  tsutsui 	/* checksum */
    136      1.1  tsutsui 	0
    137      1.1  tsutsui };
    138  1.4.2.1    lukem #endif
    139      1.1  tsutsui 
    140  1.4.2.1    lukem static u_int8_t iha_data_over_run(struct iha_scsi_req_q *);
    141      1.1  tsutsui 
    142  1.4.2.1    lukem static int iha_push_sense_request(struct iha_softc *, struct iha_scsi_req_q *);
    143  1.4.2.1    lukem static void iha_timeout(void *);
    144  1.4.2.1    lukem static int iha_alloc_sglist(struct iha_softc *);
    145  1.4.2.1    lukem 
    146  1.4.2.1    lukem static void iha_read_eeprom(struct iha_softc *, struct iha_eeprom *);
    147  1.4.2.1    lukem static int iha_se2_rd_all(struct iha_softc *, u_int16_t *);
    148  1.4.2.1    lukem static void iha_se2_instr(struct iha_softc *, int);
    149  1.4.2.1    lukem static u_int16_t iha_se2_rd(struct iha_softc *, int);
    150  1.4.2.1    lukem #ifdef notused
    151  1.4.2.1    lukem static void iha_se2_update_all(struct iha_softc *);
    152  1.4.2.1    lukem static void iha_se2_wr(struct iha_softc *, int, u_int16_t);
    153  1.4.2.1    lukem #endif
    154      1.1  tsutsui 
    155  1.4.2.1    lukem static void iha_reset_scsi_bus(struct iha_softc *);
    156  1.4.2.1    lukem static void iha_reset_chip(struct iha_softc *);
    157  1.4.2.1    lukem static void iha_reset_dma(struct iha_softc *);
    158      1.1  tsutsui 
    159  1.4.2.1    lukem static void iha_reset_tcs(struct tcs *, u_int8_t);
    160      1.1  tsutsui 
    161  1.4.2.1    lukem static void iha_done_scb(struct iha_softc *, struct iha_scsi_req_q *);
    162  1.4.2.1    lukem static void iha_exec_scb(struct iha_softc *, struct iha_scsi_req_q *);
    163      1.1  tsutsui 
    164  1.4.2.1    lukem static void iha_main(struct iha_softc *);
    165  1.4.2.1    lukem static void iha_scsi(struct iha_softc *);
    166      1.1  tsutsui 
    167  1.4.2.1    lukem static int  iha_wait(struct iha_softc *, u_int8_t);
    168      1.1  tsutsui 
    169  1.4.2.1    lukem static __inline void iha_mark_busy_scb(struct iha_scsi_req_q *);
    170      1.1  tsutsui 
    171  1.4.2.1    lukem static void iha_append_free_scb(struct iha_softc *, struct iha_scsi_req_q *);
    172  1.4.2.1    lukem static void iha_append_done_scb(struct iha_softc *, struct iha_scsi_req_q *,
    173      1.1  tsutsui     u_int8_t);
    174  1.4.2.1    lukem static __inline struct iha_scsi_req_q *iha_pop_done_scb(struct iha_softc *);
    175      1.1  tsutsui 
    176  1.4.2.1    lukem static __inline void iha_append_pend_scb(struct iha_softc *,
    177      1.2  tsutsui     struct iha_scsi_req_q *);
    178  1.4.2.1    lukem static __inline void iha_push_pend_scb(struct iha_softc *,
    179      1.2  tsutsui     struct iha_scsi_req_q *);
    180  1.4.2.1    lukem static __inline void iha_del_pend_scb(struct iha_softc *,
    181      1.2  tsutsui     struct iha_scsi_req_q *);
    182  1.4.2.1    lukem static struct iha_scsi_req_q *iha_find_pend_scb(struct iha_softc *);
    183      1.1  tsutsui 
    184  1.4.2.1    lukem static void iha_sync_done(struct iha_softc *);
    185  1.4.2.1    lukem static void iha_wide_done(struct iha_softc *);
    186  1.4.2.1    lukem static void iha_bad_seq(struct iha_softc *);
    187  1.4.2.1    lukem 
    188  1.4.2.1    lukem static int iha_next_state(struct iha_softc *);
    189  1.4.2.1    lukem static int iha_state_1(struct iha_softc *);
    190  1.4.2.1    lukem static int iha_state_2(struct iha_softc *);
    191  1.4.2.1    lukem static int iha_state_3(struct iha_softc *);
    192  1.4.2.1    lukem static int iha_state_4(struct iha_softc *);
    193  1.4.2.1    lukem static int iha_state_5(struct iha_softc *);
    194  1.4.2.1    lukem static int iha_state_6(struct iha_softc *);
    195  1.4.2.1    lukem static int iha_state_8(struct iha_softc *);
    196      1.1  tsutsui 
    197  1.4.2.1    lukem static void iha_set_ssig(struct iha_softc *, u_int8_t, u_int8_t);
    198      1.1  tsutsui 
    199  1.4.2.1    lukem static int iha_xpad_in(struct iha_softc *);
    200  1.4.2.1    lukem static int iha_xpad_out(struct iha_softc *);
    201      1.1  tsutsui 
    202  1.4.2.1    lukem static int iha_xfer_data(struct iha_softc *, struct iha_scsi_req_q *,
    203      1.1  tsutsui     int direction);
    204      1.1  tsutsui 
    205  1.4.2.1    lukem static int iha_status_msg(struct iha_softc *);
    206      1.1  tsutsui 
    207  1.4.2.1    lukem static int iha_msgin(struct iha_softc *);
    208  1.4.2.1    lukem static int iha_msgin_sdtr(struct iha_softc *);
    209  1.4.2.1    lukem static int iha_msgin_extended(struct iha_softc *);
    210  1.4.2.1    lukem static int iha_msgin_ignore_wid_resid(struct iha_softc *);
    211  1.4.2.1    lukem 
    212  1.4.2.1    lukem static int  iha_msgout(struct iha_softc *, u_int8_t);
    213  1.4.2.1    lukem static int  iha_msgout_extended(struct iha_softc *);
    214  1.4.2.1    lukem static void iha_msgout_abort(struct iha_softc *, u_int8_t);
    215  1.4.2.1    lukem static int  iha_msgout_reject(struct iha_softc *);
    216  1.4.2.1    lukem static int  iha_msgout_sdtr(struct iha_softc *);
    217  1.4.2.1    lukem static int  iha_msgout_wdtr(struct iha_softc *);
    218      1.1  tsutsui 
    219  1.4.2.1    lukem static void iha_select(struct iha_softc *, struct iha_scsi_req_q *, u_int8_t);
    220      1.1  tsutsui 
    221  1.4.2.1    lukem static void iha_busfree(struct iha_softc *);
    222  1.4.2.1    lukem static int  iha_resel(struct iha_softc *);
    223      1.1  tsutsui 
    224  1.4.2.1    lukem static void iha_abort_xs(struct iha_softc *, struct scsipi_xfer *, u_int8_t);
    225      1.1  tsutsui 
    226      1.1  tsutsui static void iha_minphys(struct buf *);
    227      1.1  tsutsui void iha_scsipi_request(struct scsipi_channel *, scsipi_adapter_req_t,
    228      1.1  tsutsui     void *arg);
    229      1.1  tsutsui 
    230      1.1  tsutsui /*
    231      1.1  tsutsui  * iha_intr - the interrupt service routine for the iha driver
    232      1.1  tsutsui  */
    233      1.1  tsutsui int
    234      1.1  tsutsui iha_intr(arg)
    235      1.1  tsutsui 	void *arg;
    236      1.1  tsutsui {
    237      1.1  tsutsui 	bus_space_tag_t iot;
    238      1.1  tsutsui 	bus_space_handle_t ioh;
    239      1.1  tsutsui 	struct iha_softc *sc;
    240      1.1  tsutsui 	int s;
    241      1.1  tsutsui 
    242      1.1  tsutsui 	sc  = (struct iha_softc *)arg;
    243      1.1  tsutsui 	iot = sc->sc_iot;
    244      1.1  tsutsui 	ioh = sc->sc_ioh;
    245      1.1  tsutsui 
    246      1.1  tsutsui 	if ((bus_space_read_1(iot, ioh, TUL_STAT0) & INTPD) == 0)
    247      1.1  tsutsui 		return (0);
    248      1.1  tsutsui 
    249      1.1  tsutsui 	s = splbio(); /* XXX - Or are interrupts off when ISR's are called? */
    250      1.1  tsutsui 
    251      1.1  tsutsui 	if (sc->sc_semaph != SEMAPH_IN_MAIN) {
    252      1.1  tsutsui 		/* XXX - need these inside a splbio()/splx()? */
    253      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_IMSK, MASK_ALL);
    254      1.1  tsutsui 		sc->sc_semaph = SEMAPH_IN_MAIN;
    255      1.1  tsutsui 
    256  1.4.2.1    lukem 		iha_main(sc);
    257      1.1  tsutsui 
    258      1.1  tsutsui 		sc->sc_semaph = ~SEMAPH_IN_MAIN;
    259      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_IMSK, (MASK_ALL & ~MSCMP));
    260      1.1  tsutsui 	}
    261      1.1  tsutsui 
    262      1.1  tsutsui 	splx(s);
    263      1.1  tsutsui 
    264      1.1  tsutsui 	return (1);
    265      1.1  tsutsui }
    266      1.1  tsutsui 
    267      1.1  tsutsui void
    268      1.1  tsutsui iha_scsipi_request(chan, req, arg)
    269      1.1  tsutsui 	struct scsipi_channel *chan;
    270      1.1  tsutsui 	scsipi_adapter_req_t req;
    271      1.1  tsutsui 	void *arg;
    272      1.1  tsutsui {
    273      1.1  tsutsui 	struct scsipi_xfer *xs;
    274      1.1  tsutsui 	struct scsipi_periph *periph;
    275      1.1  tsutsui 	struct iha_scsi_req_q *scb;
    276      1.1  tsutsui 	struct iha_softc *sc;
    277      1.1  tsutsui 	int error, flags, s;
    278      1.1  tsutsui 
    279      1.1  tsutsui 	sc = (struct iha_softc *)chan->chan_adapter->adapt_dev;
    280      1.1  tsutsui 
    281      1.1  tsutsui 	switch (req) {
    282      1.1  tsutsui 	case ADAPTER_REQ_RUN_XFER:
    283      1.1  tsutsui 		xs = arg;
    284      1.1  tsutsui 		periph = xs->xs_periph;
    285      1.1  tsutsui 		flags = xs->xs_control;
    286      1.1  tsutsui 
    287      1.1  tsutsui 		if (xs->cmdlen > sizeof(struct scsi_generic) ||
    288      1.1  tsutsui 		    periph->periph_target >= IHA_MAX_TARGETS) {
    289      1.1  tsutsui 			xs->error = XS_DRIVER_STUFFUP;
    290      1.1  tsutsui 			return;
    291      1.1  tsutsui 		}
    292      1.1  tsutsui 
    293      1.1  tsutsui 		s = splbio();
    294      1.1  tsutsui 		scb = TAILQ_FIRST(&sc->sc_freescb);
    295      1.1  tsutsui 		if (scb != NULL) {
    296      1.1  tsutsui 			scb->status = STATUS_RENT;
    297      1.1  tsutsui 			TAILQ_REMOVE(&sc->sc_freescb, scb, chain);
    298      1.1  tsutsui 		}
    299      1.1  tsutsui #ifdef DIAGNOSTIC
    300      1.1  tsutsui 		else {
    301      1.1  tsutsui 			scsipi_printaddr(periph);
    302      1.1  tsutsui 			printf("unable to allocate scb\n");
    303      1.1  tsutsui 			panic("iha_scsipi_request");
    304      1.1  tsutsui 		}
    305      1.1  tsutsui #endif
    306      1.1  tsutsui 		splx(s);
    307      1.1  tsutsui 
    308      1.1  tsutsui 		scb->target = periph->periph_target;
    309      1.1  tsutsui 		scb->lun = periph->periph_lun;
    310      1.1  tsutsui 		scb->tcs = &sc->sc_tcs[scb->target];
    311      1.1  tsutsui 		scb->flags = xs->xs_control; /* XXX */
    312      1.1  tsutsui 		scb->scb_id = MSG_IDENTIFY(periph->periph_lun,
    313      1.1  tsutsui 		    (xs->xs_control & XS_CTL_REQSENSE) == 0);
    314      1.1  tsutsui 
    315      1.1  tsutsui 		scb->xs = xs;
    316      1.1  tsutsui 		scb->timeout = xs->timeout;
    317      1.1  tsutsui 		scb->cmdlen = xs->cmdlen;
    318      1.1  tsutsui 		memcpy(&scb->cmd, xs->cmd, xs->cmdlen);
    319      1.1  tsutsui 
    320      1.1  tsutsui 		scb->buflen = xs->datalen;
    321      1.1  tsutsui 
    322      1.1  tsutsui 		if (scb->buflen > 0) {
    323      1.1  tsutsui 			error = bus_dmamap_load(sc->sc_dmat, scb->dmap,
    324      1.1  tsutsui 			    xs->data, scb->buflen, NULL,
    325  1.4.2.1    lukem 			    ((xs->xs_control & XS_CTL_NOSLEEP) ?
    326  1.4.2.1    lukem 			     BUS_DMA_NOWAIT : BUS_DMA_WAITOK) |
    327  1.4.2.1    lukem 			    BUS_DMA_STREAMING |
    328  1.4.2.1    lukem 			    ((xs->xs_control & XS_CTL_DATA_IN) ?
    329  1.4.2.1    lukem 			     BUS_DMA_READ : BUS_DMA_WRITE));
    330      1.1  tsutsui 
    331      1.1  tsutsui 			if (error) {
    332      1.1  tsutsui 				printf("%s: error %d loading dma map\n",
    333      1.1  tsutsui 				    sc->sc_dev.dv_xname, error);
    334  1.4.2.1    lukem 				iha_append_free_scb(sc, scb);
    335      1.1  tsutsui 				xs->error = XS_DRIVER_STUFFUP;
    336      1.1  tsutsui 				scsipi_done(xs);
    337      1.1  tsutsui 				return;
    338      1.1  tsutsui 			}
    339      1.1  tsutsui 			bus_dmamap_sync(sc->sc_dmat, scb->dmap,
    340      1.1  tsutsui 			    0, scb->dmap->dm_mapsize,
    341      1.1  tsutsui 			    (xs->xs_control & XS_CTL_DATA_IN) ?
    342      1.1  tsutsui 			    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    343      1.1  tsutsui 		}
    344      1.1  tsutsui 
    345  1.4.2.1    lukem 		iha_exec_scb(sc, scb);
    346      1.1  tsutsui 		return;
    347      1.1  tsutsui 
    348      1.1  tsutsui 	case ADAPTER_REQ_GROW_RESOURCES:
    349      1.1  tsutsui 		return; /* XXX */
    350      1.1  tsutsui 
    351      1.1  tsutsui 	case ADAPTER_REQ_SET_XFER_MODE:
    352      1.1  tsutsui 		return; /* XXX */
    353      1.1  tsutsui 	}
    354      1.1  tsutsui }
    355      1.1  tsutsui 
    356      1.1  tsutsui void
    357      1.1  tsutsui iha_attach(sc)
    358      1.1  tsutsui 	struct iha_softc *sc;
    359      1.1  tsutsui {
    360      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
    361      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
    362      1.1  tsutsui 	struct iha_scsi_req_q *scb;
    363      1.1  tsutsui 	struct iha_eeprom eeprom;
    364      1.1  tsutsui 	struct eeprom_adapter *conf;
    365      1.1  tsutsui 	int i, error, reg;
    366      1.1  tsutsui 
    367  1.4.2.1    lukem 	iha_read_eeprom(sc, &eeprom);
    368      1.1  tsutsui 
    369      1.1  tsutsui 	conf = &eeprom.adapter[0];
    370      1.1  tsutsui 
    371      1.1  tsutsui 	/*
    372  1.4.2.1    lukem 	 * fill in the rest of the iha_softc fields
    373      1.1  tsutsui 	 */
    374      1.1  tsutsui 	sc->sc_id = CFG_ID(conf->config1);
    375      1.1  tsutsui 	sc->sc_semaph = ~SEMAPH_IN_MAIN;
    376      1.1  tsutsui 	sc->sc_status0 = 0;
    377      1.1  tsutsui 	sc->sc_actscb = NULL;
    378      1.1  tsutsui 
    379      1.1  tsutsui 	TAILQ_INIT(&sc->sc_freescb);
    380      1.1  tsutsui 	TAILQ_INIT(&sc->sc_pendscb);
    381      1.1  tsutsui 	TAILQ_INIT(&sc->sc_donescb);
    382  1.4.2.1    lukem 	error = iha_alloc_sglist(sc);
    383      1.1  tsutsui 	if (error != 0) {
    384      1.1  tsutsui 		printf(": cannot allocate sglist\n");
    385      1.1  tsutsui 		return;
    386      1.1  tsutsui 	}
    387      1.1  tsutsui 
    388      1.1  tsutsui 	sc->sc_scb = malloc(sizeof(struct iha_scsi_req_q) * IHA_MAX_SCB,
    389      1.1  tsutsui 	    M_DEVBUF, M_NOWAIT);
    390      1.1  tsutsui 	if (sc->sc_scb == NULL) {
    391      1.1  tsutsui 		printf(": cannot allocate SCB\n");
    392      1.1  tsutsui 		return;
    393      1.1  tsutsui 	}
    394      1.4  thorpej 	memset(sc->sc_scb, 0, sizeof(struct iha_scsi_req_q) * IHA_MAX_SCB);
    395      1.1  tsutsui 
    396      1.1  tsutsui 	for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++) {
    397      1.1  tsutsui 		scb->scb_tagid = i;
    398      1.1  tsutsui 		scb->sgoffset = IHA_SG_SIZE * i;
    399      1.1  tsutsui 		scb->sglist = &sc->sc_sglist[i].sg_element[0];
    400      1.1  tsutsui 		scb->sg_addr =
    401      1.1  tsutsui 		    sc->sc_dmamap->dm_segs[0].ds_addr + scb->sgoffset;
    402      1.1  tsutsui 
    403      1.1  tsutsui 		error = bus_dmamap_create(sc->sc_dmat,
    404      1.1  tsutsui 		    (IHA_MAX_SG_ENTRIES - 1) * PAGE_SIZE, IHA_MAX_SG_ENTRIES,
    405      1.1  tsutsui 		    (IHA_MAX_SG_ENTRIES - 1) * PAGE_SIZE, 0,
    406      1.1  tsutsui 		    BUS_DMA_NOWAIT, &scb->dmap);
    407      1.1  tsutsui 
    408      1.1  tsutsui 		if (error != 0) {
    409      1.1  tsutsui 			printf(": couldn't create SCB DMA map, error = %d\n",
    410      1.1  tsutsui 			    error);
    411      1.1  tsutsui 			return;
    412      1.1  tsutsui 		}
    413      1.1  tsutsui 		TAILQ_INSERT_TAIL(&sc->sc_freescb, scb, chain);
    414      1.1  tsutsui 	}
    415      1.1  tsutsui 
    416      1.1  tsutsui 	/* Mask all the interrupts */
    417      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_IMSK, MASK_ALL);
    418      1.1  tsutsui 
    419      1.1  tsutsui 	/* Stop any I/O and reset the scsi module */
    420  1.4.2.1    lukem 	iha_reset_dma(sc);
    421      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SCTRL0, RSMOD);
    422      1.1  tsutsui 
    423      1.1  tsutsui 	/* Program HBA's SCSI ID */
    424      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SID, sc->sc_id << 4);
    425      1.1  tsutsui 
    426      1.1  tsutsui 	/*
    427      1.1  tsutsui 	 * Configure the channel as requested by the NVRAM settings read
    428  1.4.2.1    lukem 	 * by iha_read_eeprom() above.
    429      1.1  tsutsui 	 */
    430      1.1  tsutsui 
    431      1.1  tsutsui 	sc->sc_sconf1 = SCONFIG0DEFAULT;
    432      1.1  tsutsui 	if ((conf->config1 & CFG_EN_PAR) != 0)
    433      1.1  tsutsui 		sc->sc_sconf1 |= SPCHK;
    434      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SCONFIG0, sc->sc_sconf1);
    435      1.1  tsutsui 
    436      1.1  tsutsui 	/* set selection time out 250 ms */
    437      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_STIMO, STIMO_250MS);
    438      1.1  tsutsui 
    439      1.1  tsutsui 	/* Enable desired SCSI termination configuration read from eeprom */
    440      1.1  tsutsui 	reg = 0;
    441      1.1  tsutsui 	if (conf->config1 & CFG_ACT_TERM1)
    442      1.1  tsutsui 		reg |= ENTMW;
    443      1.1  tsutsui 	if (conf->config1 & CFG_ACT_TERM2)
    444      1.1  tsutsui 		reg |= ENTM;
    445      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_DCTRL0, reg);
    446      1.1  tsutsui 
    447      1.1  tsutsui 	reg = bus_space_read_1(iot, ioh, TUL_GCTRL1) & ~ATDEN;
    448      1.1  tsutsui 	if (conf->config1 & CFG_AUTO_TERM)
    449      1.1  tsutsui 		reg |= ATDEN;
    450      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_GCTRL1, reg);
    451      1.1  tsutsui 
    452      1.1  tsutsui 	for (i = 0; i < IHA_MAX_TARGETS / 2; i++) {
    453      1.1  tsutsui 		sc->sc_tcs[i * 2    ].flags = EEP_LBYTE(conf->tflags[i]);
    454      1.1  tsutsui 		sc->sc_tcs[i * 2 + 1].flags = EEP_HBYTE(conf->tflags[i]);
    455  1.4.2.1    lukem 		iha_reset_tcs(&sc->sc_tcs[i * 2    ], sc->sc_sconf1);
    456  1.4.2.1    lukem 		iha_reset_tcs(&sc->sc_tcs[i * 2 + 1], sc->sc_sconf1);
    457      1.1  tsutsui 	}
    458      1.1  tsutsui 
    459  1.4.2.1    lukem 	iha_reset_chip(sc);
    460      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SIEN, ALL_INTERRUPTS);
    461      1.1  tsutsui 
    462      1.1  tsutsui 	/*
    463      1.1  tsutsui 	 * fill in the adapter.
    464      1.1  tsutsui 	 */
    465      1.1  tsutsui 	sc->sc_adapter.adapt_dev = &sc->sc_dev;
    466      1.1  tsutsui 	sc->sc_adapter.adapt_nchannels = 1;
    467      1.1  tsutsui 	sc->sc_adapter.adapt_openings = IHA_MAX_SCB;
    468      1.1  tsutsui 	sc->sc_adapter.adapt_max_periph = IHA_MAX_SCB;
    469      1.1  tsutsui 	sc->sc_adapter.adapt_ioctl = NULL;
    470      1.1  tsutsui 	sc->sc_adapter.adapt_minphys = iha_minphys;
    471      1.1  tsutsui 	sc->sc_adapter.adapt_request = iha_scsipi_request;
    472      1.1  tsutsui 
    473      1.1  tsutsui 	/*
    474      1.1  tsutsui 	 * fill in the channel.
    475      1.1  tsutsui 	 */
    476      1.1  tsutsui 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
    477      1.1  tsutsui 	sc->sc_channel.chan_bustype = &scsi_bustype;
    478      1.1  tsutsui 	sc->sc_channel.chan_channel = 0;
    479      1.1  tsutsui 	sc->sc_channel.chan_ntargets = CFG_TARGET(conf->config2);
    480      1.1  tsutsui 	sc->sc_channel.chan_nluns = 8;
    481      1.1  tsutsui 	sc->sc_channel.chan_id = sc->sc_id;
    482      1.1  tsutsui 
    483      1.1  tsutsui 	/*
    484      1.1  tsutsui 	 * Now try to attach all the sub devices.
    485      1.1  tsutsui 	 */
    486      1.1  tsutsui 	config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
    487      1.1  tsutsui }
    488      1.1  tsutsui 
    489      1.1  tsutsui /*
    490      1.1  tsutsui  * iha_minphys - reduce bp->b_bcount to something less than
    491      1.1  tsutsui  *		 or equal to the largest I/O possible through
    492      1.1  tsutsui  *		 the adapter. Called from higher layers
    493      1.1  tsutsui  *		 via sc->sc_adapter.scsi_minphys.
    494      1.1  tsutsui  */
    495      1.1  tsutsui static void
    496      1.1  tsutsui iha_minphys(bp)
    497      1.1  tsutsui 	struct buf *bp;
    498      1.1  tsutsui {
    499      1.1  tsutsui 	if (bp->b_bcount > ((IHA_MAX_SG_ENTRIES - 1) * PAGE_SIZE))
    500      1.1  tsutsui 		bp->b_bcount = ((IHA_MAX_SG_ENTRIES - 1) * PAGE_SIZE);
    501      1.1  tsutsui 
    502      1.1  tsutsui 	minphys(bp);
    503      1.1  tsutsui }
    504      1.1  tsutsui 
    505      1.1  tsutsui /*
    506  1.4.2.1    lukem  * iha_reset_dma - abort any active DMA xfer, reset tulip FIFO.
    507      1.1  tsutsui  */
    508      1.1  tsutsui static void
    509  1.4.2.1    lukem iha_reset_dma(sc)
    510      1.1  tsutsui 	struct iha_softc *sc;
    511      1.1  tsutsui {
    512      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
    513      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
    514      1.1  tsutsui 
    515      1.1  tsutsui 	if ((bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND) != 0) {
    516      1.1  tsutsui 		/* if DMA xfer is pending, abort DMA xfer */
    517      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_DCMD, ABTXFR);
    518      1.1  tsutsui 		/* wait Abort DMA xfer done */
    519      1.1  tsutsui 		while ((bus_space_read_1(iot, ioh, TUL_ISTUS0) & DABT) == 0)
    520      1.1  tsutsui 			;
    521      1.1  tsutsui 	}
    522      1.1  tsutsui 
    523      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
    524      1.1  tsutsui }
    525      1.1  tsutsui 
    526      1.1  tsutsui /*
    527  1.4.2.1    lukem  * iha_append_free_scb - append the supplied SCB to the tail of the
    528      1.1  tsutsui  *			 sc_freescb queue after clearing and resetting
    529      1.1  tsutsui  *			 everything possible.
    530      1.1  tsutsui  */
    531      1.1  tsutsui static void
    532  1.4.2.1    lukem iha_append_free_scb(sc, scb)
    533      1.1  tsutsui 	struct iha_softc *sc;
    534      1.1  tsutsui 	struct iha_scsi_req_q *scb;
    535      1.1  tsutsui {
    536      1.1  tsutsui 	int s;
    537      1.1  tsutsui 
    538      1.1  tsutsui 	s = splbio();
    539      1.1  tsutsui 
    540      1.1  tsutsui 	if (scb == sc->sc_actscb)
    541      1.1  tsutsui 		sc->sc_actscb = NULL;
    542      1.1  tsutsui 
    543      1.1  tsutsui 	scb->status = STATUS_QUEUED;
    544      1.1  tsutsui 	scb->ha_stat = HOST_OK;
    545      1.1  tsutsui 	scb->ta_stat  = SCSI_OK;
    546      1.1  tsutsui 
    547      1.1  tsutsui 	scb->nextstat = 0;
    548      1.1  tsutsui 	scb->sg_index = 0;
    549      1.1  tsutsui 	scb->sg_max = 0;
    550      1.1  tsutsui 	scb->flags = 0;
    551      1.1  tsutsui 	scb->target = 0;
    552      1.1  tsutsui 	scb->lun = 0;
    553      1.1  tsutsui 	scb->buflen = 0;
    554      1.1  tsutsui 	scb->sg_size = 0;
    555      1.1  tsutsui 	scb->cmdlen = 0;
    556      1.1  tsutsui 	scb->scb_id = 0;
    557      1.1  tsutsui 	scb->scb_tagmsg = 0;
    558      1.1  tsutsui 	scb->timeout = 0;
    559      1.1  tsutsui 	scb->bufaddr = 0;
    560      1.1  tsutsui 
    561      1.1  tsutsui 	scb->xs = NULL;
    562      1.1  tsutsui 	scb->tcs = NULL;
    563      1.1  tsutsui 
    564      1.4  thorpej 	memset(scb->cmd, 0, sizeof(scb->cmd));
    565      1.4  thorpej 	memset(scb->sglist, 0, sizeof(scb->sglist));
    566      1.1  tsutsui 
    567      1.1  tsutsui 	/*
    568      1.1  tsutsui 	 * scb_tagid, sg_addr, sglist
    569      1.1  tsutsui 	 * SCB_SensePtr are set at initialization
    570      1.1  tsutsui 	 * and never change
    571      1.1  tsutsui 	 */
    572      1.1  tsutsui 
    573      1.1  tsutsui 	TAILQ_INSERT_TAIL(&sc->sc_freescb, scb, chain);
    574      1.1  tsutsui 
    575      1.1  tsutsui 	splx(s);
    576      1.1  tsutsui }
    577      1.1  tsutsui 
    578      1.2  tsutsui static __inline void
    579  1.4.2.1    lukem iha_append_pend_scb(sc, scb)
    580      1.1  tsutsui 	struct iha_softc *sc;
    581      1.1  tsutsui 	struct iha_scsi_req_q *scb;
    582      1.1  tsutsui {
    583      1.1  tsutsui 	/* ASSUMPTION: only called within a splbio()/splx() pair */
    584      1.1  tsutsui 
    585      1.1  tsutsui 	if (scb == sc->sc_actscb)
    586      1.1  tsutsui 		sc->sc_actscb = NULL;
    587      1.1  tsutsui 
    588      1.1  tsutsui 	scb->status = STATUS_QUEUED;
    589      1.1  tsutsui 
    590      1.1  tsutsui 	TAILQ_INSERT_TAIL(&sc->sc_pendscb, scb, chain);
    591      1.1  tsutsui }
    592      1.1  tsutsui 
    593      1.2  tsutsui static __inline void
    594  1.4.2.1    lukem iha_push_pend_scb(sc, scb)
    595      1.1  tsutsui 	struct iha_softc *sc;
    596      1.1  tsutsui 	struct iha_scsi_req_q *scb;
    597      1.1  tsutsui {
    598      1.1  tsutsui 	int s;
    599      1.1  tsutsui 
    600      1.1  tsutsui 	s = splbio();
    601      1.1  tsutsui 
    602      1.1  tsutsui 	if (scb == sc->sc_actscb)
    603      1.1  tsutsui 		sc->sc_actscb = NULL;
    604      1.1  tsutsui 
    605      1.1  tsutsui 	scb->status = STATUS_QUEUED;
    606      1.1  tsutsui 
    607      1.1  tsutsui 	TAILQ_INSERT_HEAD(&sc->sc_pendscb, scb, chain);
    608      1.1  tsutsui 
    609      1.1  tsutsui 	splx(s);
    610      1.1  tsutsui }
    611      1.1  tsutsui 
    612      1.1  tsutsui /*
    613  1.4.2.1    lukem  * iha_find_pend_scb - scan the pending queue for a SCB that can be
    614      1.1  tsutsui  *		       processed immediately. Return NULL if none found
    615      1.1  tsutsui  *		       and a pointer to the SCB if one is found. If there
    616      1.1  tsutsui  *		       is an active SCB, return NULL!
    617      1.1  tsutsui  */
    618      1.1  tsutsui static struct iha_scsi_req_q *
    619  1.4.2.1    lukem iha_find_pend_scb(sc)
    620      1.1  tsutsui 	struct iha_softc *sc;
    621      1.1  tsutsui {
    622      1.1  tsutsui 	struct iha_scsi_req_q *scb;
    623      1.1  tsutsui 	struct tcs *tcs;
    624      1.1  tsutsui 	int s;
    625      1.1  tsutsui 
    626      1.1  tsutsui 	s = splbio();
    627      1.1  tsutsui 
    628      1.1  tsutsui 	if (sc->sc_actscb != NULL)
    629      1.1  tsutsui 		scb = NULL;
    630      1.1  tsutsui 
    631      1.1  tsutsui 	else
    632      1.1  tsutsui 		TAILQ_FOREACH(scb, &sc->sc_pendscb, chain) {
    633      1.1  tsutsui 			if ((scb->flags & XS_CTL_RESET) != 0)
    634      1.1  tsutsui 				/* ALWAYS willing to reset a device */
    635      1.1  tsutsui 				break;
    636      1.1  tsutsui 
    637      1.1  tsutsui 			tcs = scb->tcs;
    638      1.1  tsutsui 
    639      1.1  tsutsui 			if ((scb->scb_tagmsg) != 0) {
    640      1.1  tsutsui 				/*
    641      1.1  tsutsui 				 * A Tagged I/O. OK to start If no
    642      1.1  tsutsui 				 * non-tagged I/O is active on the same
    643      1.1  tsutsui 				 * target
    644      1.1  tsutsui 				 */
    645      1.1  tsutsui 				if (tcs->ntagscb == NULL)
    646      1.1  tsutsui 					break;
    647      1.1  tsutsui 
    648      1.1  tsutsui 			} else	if (scb->cmd[0] == REQUEST_SENSE) {
    649      1.1  tsutsui 				/*
    650      1.1  tsutsui 				 * OK to do a non-tagged request sense
    651      1.1  tsutsui 				 * even if a non-tagged I/O has been
    652      1.1  tsutsui 				 * started, 'cuz we don't allow any
    653      1.1  tsutsui 				 * disconnect during a request sense op
    654      1.1  tsutsui 				 */
    655      1.1  tsutsui 				break;
    656      1.1  tsutsui 
    657      1.1  tsutsui 			} else	if (tcs->tagcnt == 0) {
    658      1.1  tsutsui 				/*
    659      1.1  tsutsui 				 * No tagged I/O active on this target,
    660      1.1  tsutsui 				 * ok to start a non-tagged one if one
    661      1.1  tsutsui 				 * is not already active
    662      1.1  tsutsui 				 */
    663      1.1  tsutsui 				if (tcs->ntagscb == NULL)
    664      1.1  tsutsui 					break;
    665      1.1  tsutsui 			}
    666      1.1  tsutsui 		}
    667      1.1  tsutsui 
    668      1.1  tsutsui 	splx(s);
    669      1.1  tsutsui 
    670      1.1  tsutsui 	return (scb);
    671      1.1  tsutsui }
    672      1.1  tsutsui 
    673      1.1  tsutsui /*
    674  1.4.2.1    lukem  * iha_del_pend_scb - remove scb from sc_pendscb
    675      1.1  tsutsui  */
    676      1.2  tsutsui static __inline void
    677  1.4.2.1    lukem iha_del_pend_scb(sc, scb)
    678      1.1  tsutsui 	struct iha_softc *sc;
    679      1.1  tsutsui 	struct iha_scsi_req_q *scb;
    680      1.1  tsutsui {
    681      1.1  tsutsui 	int s;
    682      1.1  tsutsui 
    683      1.1  tsutsui 	s = splbio();
    684      1.1  tsutsui 
    685      1.1  tsutsui 	TAILQ_REMOVE(&sc->sc_pendscb, scb, chain);
    686      1.1  tsutsui 
    687      1.1  tsutsui 	splx(s);
    688      1.1  tsutsui }
    689      1.1  tsutsui 
    690      1.2  tsutsui static __inline void
    691  1.4.2.1    lukem iha_mark_busy_scb(scb)
    692      1.1  tsutsui 	struct iha_scsi_req_q *scb;
    693      1.1  tsutsui {
    694      1.1  tsutsui 	int  s;
    695      1.1  tsutsui 
    696      1.1  tsutsui 	s = splbio();
    697      1.1  tsutsui 
    698      1.1  tsutsui 	scb->status = STATUS_BUSY;
    699      1.1  tsutsui 
    700      1.1  tsutsui 	if (scb->scb_tagmsg == 0)
    701      1.1  tsutsui 		scb->tcs->ntagscb = scb;
    702      1.1  tsutsui 	else
    703      1.1  tsutsui 		scb->tcs->tagcnt++;
    704      1.1  tsutsui 
    705      1.1  tsutsui 	splx(s);
    706      1.1  tsutsui }
    707      1.1  tsutsui 
    708      1.1  tsutsui static void
    709  1.4.2.1    lukem iha_append_done_scb(sc, scb, hastat)
    710      1.1  tsutsui 	struct iha_softc *sc;
    711      1.1  tsutsui 	struct iha_scsi_req_q *scb;
    712      1.1  tsutsui 	u_int8_t hastat;
    713      1.1  tsutsui {
    714      1.1  tsutsui 	struct tcs *tcs;
    715      1.1  tsutsui 	int s;
    716      1.1  tsutsui 
    717      1.1  tsutsui 	s = splbio();
    718      1.1  tsutsui 
    719      1.1  tsutsui 	if (scb->xs != NULL)
    720      1.1  tsutsui 		callout_stop(&scb->xs->xs_callout);
    721      1.1  tsutsui 
    722      1.1  tsutsui 	if (scb == sc->sc_actscb)
    723      1.1  tsutsui 		sc->sc_actscb = NULL;
    724      1.1  tsutsui 
    725      1.1  tsutsui 	tcs = scb->tcs;
    726      1.1  tsutsui 
    727      1.1  tsutsui 	if (scb->scb_tagmsg != 0) {
    728      1.1  tsutsui 		if (tcs->tagcnt)
    729      1.1  tsutsui 			tcs->tagcnt--;
    730      1.1  tsutsui 	} else if (tcs->ntagscb == scb)
    731      1.1  tsutsui 		tcs->ntagscb = NULL;
    732      1.1  tsutsui 
    733      1.1  tsutsui 	scb->status = STATUS_QUEUED;
    734      1.1  tsutsui 	scb->ha_stat = hastat;
    735      1.1  tsutsui 
    736      1.1  tsutsui 	TAILQ_INSERT_TAIL(&sc->sc_donescb, scb, chain);
    737      1.1  tsutsui 
    738      1.1  tsutsui 	splx(s);
    739      1.1  tsutsui }
    740      1.1  tsutsui 
    741      1.2  tsutsui static __inline struct iha_scsi_req_q *
    742  1.4.2.1    lukem iha_pop_done_scb(sc)
    743      1.1  tsutsui 	struct iha_softc *sc;
    744      1.1  tsutsui {
    745      1.1  tsutsui 	struct iha_scsi_req_q *scb;
    746      1.1  tsutsui 	int s;
    747      1.1  tsutsui 
    748      1.1  tsutsui 	s = splbio();
    749      1.1  tsutsui 
    750      1.1  tsutsui 	scb = TAILQ_FIRST(&sc->sc_donescb);
    751      1.1  tsutsui 
    752      1.1  tsutsui 	if (scb != NULL) {
    753      1.1  tsutsui 		scb->status = STATUS_RENT;
    754      1.1  tsutsui 		TAILQ_REMOVE(&sc->sc_donescb, scb, chain);
    755      1.1  tsutsui 	}
    756      1.1  tsutsui 
    757      1.1  tsutsui 	splx(s);
    758      1.1  tsutsui 
    759      1.1  tsutsui 	return (scb);
    760      1.1  tsutsui }
    761      1.1  tsutsui 
    762      1.1  tsutsui /*
    763  1.4.2.1    lukem  * iha_abort_xs - find the SCB associated with the supplied xs and
    764      1.1  tsutsui  *                stop all processing on it, moving it to the done
    765      1.1  tsutsui  *                queue with the supplied host status value.
    766      1.1  tsutsui  */
    767      1.1  tsutsui static void
    768  1.4.2.1    lukem iha_abort_xs(sc, xs, hastat)
    769      1.1  tsutsui 	struct iha_softc *sc;
    770      1.1  tsutsui 	struct scsipi_xfer *xs;
    771      1.1  tsutsui 	u_int8_t hastat;
    772      1.1  tsutsui {
    773      1.1  tsutsui 	struct iha_scsi_req_q *scb;
    774      1.1  tsutsui 	int i, s;
    775      1.1  tsutsui 
    776      1.1  tsutsui 	s = splbio();
    777      1.1  tsutsui 
    778      1.1  tsutsui 	/* Check the pending queue for the SCB pointing to xs */
    779      1.1  tsutsui 
    780      1.1  tsutsui 	TAILQ_FOREACH(scb, &sc->sc_pendscb, chain)
    781      1.1  tsutsui 		if (scb->xs == xs) {
    782  1.4.2.1    lukem 			iha_del_pend_scb(sc, scb);
    783  1.4.2.1    lukem 			iha_append_done_scb(sc, scb, hastat);
    784      1.1  tsutsui 			splx(s);
    785      1.1  tsutsui 			return;
    786      1.1  tsutsui 		}
    787      1.1  tsutsui 
    788      1.1  tsutsui 	/*
    789      1.1  tsutsui 	 * If that didn't work, check all BUSY/SELECTING SCB's for one
    790      1.1  tsutsui 	 * pointing to xs
    791      1.1  tsutsui 	 */
    792      1.1  tsutsui 
    793      1.1  tsutsui 	for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++)
    794      1.1  tsutsui 		switch (scb->status) {
    795      1.1  tsutsui 		case STATUS_BUSY:
    796      1.1  tsutsui 		case STATUS_SELECT:
    797      1.1  tsutsui 			if (scb->xs == xs) {
    798  1.4.2.1    lukem 				iha_append_done_scb(sc, scb, hastat);
    799      1.1  tsutsui 				splx(s);
    800      1.1  tsutsui 				return;
    801      1.1  tsutsui 			}
    802      1.1  tsutsui 			break;
    803      1.1  tsutsui 		default:
    804      1.1  tsutsui 			break;
    805      1.1  tsutsui 		}
    806      1.1  tsutsui 
    807      1.1  tsutsui 	splx(s);
    808      1.1  tsutsui }
    809      1.1  tsutsui 
    810      1.1  tsutsui /*
    811  1.4.2.1    lukem  * iha_bad_seq - a SCSI bus phase was encountered out of the
    812      1.1  tsutsui  *               correct/expected sequence. Reset the SCSI bus.
    813      1.1  tsutsui  */
    814      1.1  tsutsui static void
    815  1.4.2.1    lukem iha_bad_seq(sc)
    816      1.1  tsutsui 	struct iha_softc *sc;
    817      1.1  tsutsui {
    818      1.1  tsutsui 	struct iha_scsi_req_q *scb = sc->sc_actscb;
    819      1.1  tsutsui 
    820      1.1  tsutsui 	if (scb != NULL)
    821  1.4.2.1    lukem 		iha_append_done_scb(sc, scb, HOST_BAD_PHAS);
    822      1.1  tsutsui 
    823  1.4.2.1    lukem 	iha_reset_scsi_bus(sc);
    824  1.4.2.1    lukem 	iha_reset_chip(sc);
    825      1.1  tsutsui }
    826      1.1  tsutsui 
    827      1.1  tsutsui /*
    828  1.4.2.1    lukem  * iha_push_sense_request - obtain auto sense data by pushing the
    829      1.1  tsutsui  *			    SCB needing it back onto the pending
    830      1.1  tsutsui  *			    queue with a REQUEST_SENSE CDB.
    831      1.1  tsutsui  */
    832      1.1  tsutsui static int
    833  1.4.2.1    lukem iha_push_sense_request(sc, scb)
    834      1.1  tsutsui 	struct iha_softc *sc;
    835      1.1  tsutsui 	struct iha_scsi_req_q *scb;
    836      1.1  tsutsui {
    837      1.1  tsutsui 	struct scsipi_xfer *xs = scb->xs;
    838      1.1  tsutsui 	struct scsipi_periph *periph = xs->xs_periph;
    839      1.1  tsutsui 	struct scsipi_sense *ss = (struct scsipi_sense *)scb->cmd;
    840      1.1  tsutsui 	int lun = periph->periph_lun;
    841      1.1  tsutsui 	int err;
    842      1.1  tsutsui 
    843      1.1  tsutsui 	ss->opcode = REQUEST_SENSE;
    844      1.1  tsutsui 	ss->byte2 = lun << SCSI_CMD_LUN_SHIFT;
    845      1.1  tsutsui 	ss->unused[0] = ss->unused[1] = 0;
    846      1.1  tsutsui 	ss->length = sizeof(struct scsipi_sense_data);
    847      1.1  tsutsui 	ss->control = 0;
    848      1.1  tsutsui 
    849      1.1  tsutsui 	scb->flags &= ~(FLAG_SG | XS_CTL_DATA_OUT);
    850      1.1  tsutsui 	scb->flags |= FLAG_RSENS | XS_CTL_DATA_IN;
    851      1.1  tsutsui 
    852      1.1  tsutsui 	scb->scb_id &= ~MSG_IDENTIFY_DISCFLAG;
    853      1.1  tsutsui 
    854      1.1  tsutsui 	scb->scb_tagmsg = 0;
    855      1.1  tsutsui 	scb->ta_stat = SCSI_OK;
    856      1.1  tsutsui 
    857      1.1  tsutsui 	scb->cmdlen = sizeof(struct scsipi_sense);
    858      1.1  tsutsui 	scb->buflen = ss->length;
    859      1.1  tsutsui 
    860      1.1  tsutsui 	err = bus_dmamap_load(sc->sc_dmat, scb->dmap,
    861  1.4.2.1    lukem 	    &xs->sense.scsi_sense, scb->buflen, NULL,
    862  1.4.2.1    lukem 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
    863      1.1  tsutsui 	if (err != 0) {
    864      1.1  tsutsui 		printf("iha_push_sense_request: cannot bus_dmamap_load()\n");
    865      1.1  tsutsui 		xs->error = XS_DRIVER_STUFFUP;
    866      1.1  tsutsui 		return 1;
    867      1.1  tsutsui 	}
    868      1.1  tsutsui 	bus_dmamap_sync(sc->sc_dmat, scb->dmap,
    869      1.1  tsutsui 	    0, scb->buflen, BUS_DMASYNC_PREREAD);
    870      1.1  tsutsui 
    871      1.1  tsutsui 	/* XXX What about queued command? */
    872  1.4.2.1    lukem 	iha_exec_scb(sc, scb);
    873      1.1  tsutsui 
    874      1.1  tsutsui 	return 0;
    875      1.1  tsutsui }
    876      1.1  tsutsui 
    877      1.1  tsutsui /*
    878  1.4.2.1    lukem  * iha_main - process the active SCB, taking one off pending and making it
    879      1.1  tsutsui  *	      active if necessary, and any done SCB's created as
    880      1.1  tsutsui  *	      a result until there are no interrupts pending and no pending
    881      1.1  tsutsui  *	      SCB's that can be started.
    882      1.1  tsutsui  */
    883      1.1  tsutsui static void
    884  1.4.2.1    lukem iha_main(sc)
    885      1.1  tsutsui 	struct iha_softc *sc;
    886      1.1  tsutsui {
    887      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
    888      1.1  tsutsui 	bus_space_handle_t ioh =sc->sc_ioh;
    889      1.1  tsutsui 	struct iha_scsi_req_q *scb;
    890      1.1  tsutsui 
    891      1.1  tsutsui 	for (;;) {
    892  1.4.2.1    lukem 		iha_scsi(sc);
    893      1.1  tsutsui 
    894  1.4.2.1    lukem 		while ((scb = iha_pop_done_scb(sc)) != NULL)
    895  1.4.2.1    lukem 			iha_done_scb(sc, scb);
    896      1.1  tsutsui 
    897      1.1  tsutsui 		/*
    898      1.1  tsutsui 		 * If there are no interrupts pending, or we can't start
    899      1.1  tsutsui 		 * a pending sc, break out of the for(;;). Otherwise
    900      1.1  tsutsui 		 * continue the good work with another call to
    901  1.4.2.1    lukem 		 * iha_scsi().
    902      1.1  tsutsui 		 */
    903      1.1  tsutsui 		if (((bus_space_read_1(iot, ioh, TUL_STAT0) & INTPD) == 0)
    904  1.4.2.1    lukem 		    && (iha_find_pend_scb(sc) == NULL))
    905      1.1  tsutsui 			break;
    906      1.1  tsutsui 	}
    907      1.1  tsutsui }
    908      1.1  tsutsui 
    909      1.1  tsutsui /*
    910  1.4.2.1    lukem  * iha_scsi - service any outstanding interrupts. If there are none, try to
    911      1.1  tsutsui  *            start another SCB currently in the pending queue.
    912      1.1  tsutsui  */
    913      1.1  tsutsui static void
    914  1.4.2.1    lukem iha_scsi(sc)
    915      1.1  tsutsui 	struct iha_softc *sc;
    916      1.1  tsutsui {
    917      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
    918      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
    919      1.1  tsutsui 	struct iha_scsi_req_q *scb;
    920      1.1  tsutsui 	struct tcs *tcs;
    921      1.1  tsutsui 	u_int8_t stat;
    922      1.1  tsutsui 
    923      1.1  tsutsui 	/* service pending interrupts asap */
    924      1.1  tsutsui 
    925      1.1  tsutsui 	stat = bus_space_read_1(iot, ioh, TUL_STAT0);
    926      1.1  tsutsui 	if ((stat & INTPD) != 0) {
    927      1.1  tsutsui 		sc->sc_status0 = stat;
    928      1.1  tsutsui 		sc->sc_status1 = bus_space_read_1(iot, ioh, TUL_STAT1);
    929      1.1  tsutsui 		sc->sc_sistat = bus_space_read_1(iot, ioh, TUL_SISTAT);
    930      1.1  tsutsui 
    931      1.1  tsutsui 		sc->sc_phase = sc->sc_status0 & PH_MASK;
    932      1.1  tsutsui 
    933      1.1  tsutsui 		if ((sc->sc_sistat & SRSTD) != 0) {
    934  1.4.2.1    lukem 			iha_reset_scsi_bus(sc);
    935      1.1  tsutsui 			return;
    936      1.1  tsutsui 		}
    937      1.1  tsutsui 
    938      1.1  tsutsui 		if ((sc->sc_sistat & RSELED) != 0) {
    939  1.4.2.1    lukem 			iha_resel(sc);
    940      1.1  tsutsui 			return;
    941      1.1  tsutsui 		}
    942      1.1  tsutsui 
    943      1.1  tsutsui 		if ((sc->sc_sistat & (STIMEO | DISCD)) != 0) {
    944  1.4.2.1    lukem 			iha_busfree(sc);
    945      1.1  tsutsui 			return;
    946      1.1  tsutsui 		}
    947      1.1  tsutsui 
    948      1.1  tsutsui 		if ((sc->sc_sistat & (SCMDN | SBSRV)) != 0) {
    949  1.4.2.1    lukem 			iha_next_state(sc);
    950      1.1  tsutsui 			return;
    951      1.1  tsutsui 		}
    952      1.1  tsutsui 
    953      1.1  tsutsui 		if ((sc->sc_sistat & SELED) != 0)
    954  1.4.2.1    lukem 			iha_set_ssig(sc, 0, 0);
    955      1.1  tsutsui 	}
    956      1.1  tsutsui 
    957      1.1  tsutsui 	/*
    958      1.1  tsutsui 	 * There were no interrupts pending which required action elsewhere, so
    959      1.1  tsutsui 	 * see if it is possible to start the selection phase on a pending SCB
    960      1.1  tsutsui 	 */
    961  1.4.2.1    lukem 	if ((scb = iha_find_pend_scb(sc)) == NULL)
    962      1.1  tsutsui 		return;
    963      1.1  tsutsui 
    964      1.1  tsutsui 	tcs = scb->tcs;
    965      1.1  tsutsui 
    966      1.1  tsutsui 	/* program HBA's SCSI ID & target SCSI ID */
    967      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SID, (sc->sc_id << 4) | scb->target);
    968      1.1  tsutsui 
    969      1.1  tsutsui 	if ((scb->flags & XS_CTL_RESET) == 0) {
    970      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
    971      1.1  tsutsui 
    972      1.1  tsutsui 		if ((tcs->flags & FLAG_NO_NEG_SYNC) == 0 ||
    973      1.1  tsutsui 		    (tcs->flags & FLAG_NO_NEG_WIDE) == 0)
    974  1.4.2.1    lukem 			iha_select(sc, scb, SELATNSTOP);
    975      1.1  tsutsui 
    976      1.1  tsutsui 		else if (scb->scb_tagmsg != 0)
    977  1.4.2.1    lukem 			iha_select(sc, scb, SEL_ATN3);
    978      1.1  tsutsui 
    979      1.1  tsutsui 		else
    980  1.4.2.1    lukem 			iha_select(sc, scb, SEL_ATN);
    981      1.1  tsutsui 
    982      1.1  tsutsui 	} else {
    983  1.4.2.1    lukem 		iha_select(sc, scb, SELATNSTOP);
    984      1.1  tsutsui 		scb->nextstat = 8;
    985      1.1  tsutsui 	}
    986      1.1  tsutsui 
    987      1.1  tsutsui 	if ((scb->flags & XS_CTL_POLL) != 0) {
    988      1.1  tsutsui 		for (; scb->timeout > 0; scb->timeout--) {
    989  1.4.2.1    lukem 			if (iha_wait(sc, NO_OP) == -1)
    990      1.1  tsutsui 				break;
    991  1.4.2.1    lukem 			if (iha_next_state(sc) == -1)
    992      1.1  tsutsui 				break;
    993      1.1  tsutsui 			delay(1000); /* Only happens in boot, so it's ok */
    994      1.1  tsutsui 		}
    995      1.1  tsutsui 
    996      1.1  tsutsui 		/*
    997      1.1  tsutsui 		 * Since done queue processing not done until AFTER this
    998      1.1  tsutsui 		 * function returns, scb is on the done queue, not
    999      1.1  tsutsui 		 * the free queue at this point and still has valid data
   1000      1.1  tsutsui 		 *
   1001      1.1  tsutsui 		 * Conversely, xs->error has not been set yet
   1002      1.1  tsutsui 		 */
   1003      1.1  tsutsui 		if (scb->timeout == 0)
   1004  1.4.2.1    lukem 			iha_timeout(scb);
   1005      1.1  tsutsui 	}
   1006      1.1  tsutsui }
   1007      1.1  tsutsui 
   1008      1.1  tsutsui /*
   1009  1.4.2.1    lukem  * iha_data_over_run - return HOST_OK for all SCSI opcodes where BufLen
   1010      1.1  tsutsui  *		       is an 'Allocation Length'. All other SCSI opcodes
   1011      1.1  tsutsui  *		       get HOST_DO_DU as they SHOULD have xferred all the
   1012      1.1  tsutsui  *		       data requested.
   1013      1.1  tsutsui  *
   1014      1.1  tsutsui  *		       The list of opcodes using 'Allocation Length' was
   1015      1.1  tsutsui  *		       found by scanning all the SCSI-3 T10 drafts. See
   1016      1.1  tsutsui  *		       www.t10.org for the curious with a .pdf reader.
   1017      1.1  tsutsui  */
   1018      1.1  tsutsui static u_int8_t
   1019  1.4.2.1    lukem iha_data_over_run(scb)
   1020      1.1  tsutsui 	struct iha_scsi_req_q *scb;
   1021      1.1  tsutsui {
   1022      1.1  tsutsui 	switch (scb->cmd[0]) {
   1023      1.1  tsutsui 	case 0x03: /* Request Sense                   SPC-2 */
   1024      1.1  tsutsui 	case 0x12: /* Inquiry                         SPC-2 */
   1025      1.1  tsutsui 	case 0x1a: /* Mode Sense (6 byte version)     SPC-2 */
   1026      1.1  tsutsui 	case 0x1c: /* Receive Diagnostic Results      SPC-2 */
   1027      1.1  tsutsui 	case 0x23: /* Read Format Capacities          MMC-2 */
   1028      1.1  tsutsui 	case 0x29: /* Read Generation                 SBC   */
   1029      1.1  tsutsui 	case 0x34: /* Read Position                   SSC-2 */
   1030      1.1  tsutsui 	case 0x37: /* Read Defect Data                SBC   */
   1031      1.1  tsutsui 	case 0x3c: /* Read Buffer                     SPC-2 */
   1032      1.1  tsutsui 	case 0x42: /* Read Sub Channel                MMC-2 */
   1033      1.1  tsutsui 	case 0x43: /* Read TOC/PMA/ATIP               MMC   */
   1034      1.1  tsutsui 
   1035      1.1  tsutsui 	/* XXX - 2 with same opcode of 0x44? */
   1036      1.1  tsutsui 	case 0x44: /* Read Header/Read Density Suprt  MMC/SSC*/
   1037      1.1  tsutsui 
   1038      1.1  tsutsui 	case 0x46: /* Get Configuration               MMC-2 */
   1039      1.1  tsutsui 	case 0x4a: /* Get Event/Status Notification   MMC-2 */
   1040      1.1  tsutsui 	case 0x4d: /* Log Sense                       SPC-2 */
   1041      1.1  tsutsui 	case 0x51: /* Read Disc Information           MMC   */
   1042      1.1  tsutsui 	case 0x52: /* Read Track Information          MMC   */
   1043      1.1  tsutsui 	case 0x59: /* Read Master CUE                 MMC   */
   1044      1.1  tsutsui 	case 0x5a: /* Mode Sense (10 byte version)    SPC-2 */
   1045      1.1  tsutsui 	case 0x5c: /* Read Buffer Capacity            MMC   */
   1046      1.1  tsutsui 	case 0x5e: /* Persistant Reserve In           SPC-2 */
   1047      1.1  tsutsui 	case 0x84: /* Receive Copy Results            SPC-2 */
   1048      1.1  tsutsui 	case 0xa0: /* Report LUNs                     SPC-2 */
   1049      1.1  tsutsui 	case 0xa3: /* Various Report requests         SBC-2/SCC-2*/
   1050      1.1  tsutsui 	case 0xa4: /* Report Key                      MMC-2 */
   1051      1.1  tsutsui 	case 0xad: /* Read DVD Structure              MMC-2 */
   1052      1.1  tsutsui 	case 0xb4: /* Read Element Status (Attached)  SMC   */
   1053      1.1  tsutsui 	case 0xb5: /* Request Volume Element Address  SMC   */
   1054      1.1  tsutsui 	case 0xb7: /* Read Defect Data (12 byte ver.) SBC   */
   1055      1.1  tsutsui 	case 0xb8: /* Read Element Status (Independ.) SMC   */
   1056      1.1  tsutsui 	case 0xba: /* Report Redundancy               SCC-2 */
   1057      1.1  tsutsui 	case 0xbd: /* Mechanism Status                MMC   */
   1058      1.1  tsutsui 	case 0xbe: /* Report Basic Redundancy         SCC-2 */
   1059      1.1  tsutsui 
   1060      1.1  tsutsui 		return (HOST_OK);
   1061      1.1  tsutsui 		break;
   1062      1.1  tsutsui 
   1063      1.1  tsutsui 	default:
   1064      1.1  tsutsui 		return (HOST_DO_DU);
   1065      1.1  tsutsui 		break;
   1066      1.1  tsutsui 	}
   1067      1.1  tsutsui }
   1068      1.1  tsutsui 
   1069      1.1  tsutsui /*
   1070  1.4.2.1    lukem  * iha_next_state - prcess the current SCB as requested in it's
   1071      1.1  tsutsui  *                  nextstat member.
   1072      1.1  tsutsui  */
   1073      1.1  tsutsui static int
   1074  1.4.2.1    lukem iha_next_state(sc)
   1075      1.1  tsutsui 	struct iha_softc *sc;
   1076      1.1  tsutsui {
   1077      1.1  tsutsui 
   1078      1.1  tsutsui 	if (sc->sc_actscb == NULL)
   1079      1.1  tsutsui 		return (-1);
   1080      1.1  tsutsui 
   1081      1.1  tsutsui 	switch (sc->sc_actscb->nextstat) {
   1082      1.1  tsutsui 	case 1:
   1083  1.4.2.1    lukem 		if (iha_state_1(sc) == 3)
   1084      1.1  tsutsui 			goto state_3;
   1085      1.1  tsutsui 		break;
   1086      1.1  tsutsui 
   1087      1.1  tsutsui 	case 2:
   1088  1.4.2.1    lukem 		switch (iha_state_2(sc)) {
   1089      1.1  tsutsui 		case 3:
   1090      1.1  tsutsui 			goto state_3;
   1091      1.1  tsutsui 		case 4:
   1092      1.1  tsutsui 			goto state_4;
   1093      1.1  tsutsui 		default:
   1094      1.1  tsutsui 			break;
   1095      1.1  tsutsui 		}
   1096      1.1  tsutsui 		break;
   1097      1.1  tsutsui 
   1098      1.1  tsutsui 	case 3:
   1099      1.1  tsutsui 	state_3:
   1100  1.4.2.1    lukem 		if (iha_state_3(sc) == 4)
   1101      1.1  tsutsui 			goto state_4;
   1102      1.1  tsutsui 		break;
   1103      1.1  tsutsui 
   1104      1.1  tsutsui 	case 4:
   1105      1.1  tsutsui 	state_4:
   1106  1.4.2.1    lukem 		switch (iha_state_4(sc)) {
   1107      1.1  tsutsui 		case 0:
   1108      1.1  tsutsui 			return (0);
   1109      1.1  tsutsui 		case 6:
   1110      1.1  tsutsui 			goto state_6;
   1111      1.1  tsutsui 		default:
   1112      1.1  tsutsui 			break;
   1113      1.1  tsutsui 		}
   1114      1.1  tsutsui 		break;
   1115      1.1  tsutsui 
   1116      1.1  tsutsui 	case 5:
   1117  1.4.2.1    lukem 		switch (iha_state_5(sc)) {
   1118      1.1  tsutsui 		case 4:
   1119      1.1  tsutsui 			goto state_4;
   1120      1.1  tsutsui 		case 6:
   1121      1.1  tsutsui 			goto state_6;
   1122      1.1  tsutsui 		default:
   1123      1.1  tsutsui 			break;
   1124      1.1  tsutsui 		}
   1125      1.1  tsutsui 		break;
   1126      1.1  tsutsui 
   1127      1.1  tsutsui 	case 6:
   1128      1.1  tsutsui 	state_6:
   1129  1.4.2.1    lukem 		iha_state_6(sc);
   1130      1.1  tsutsui 		break;
   1131      1.1  tsutsui 
   1132      1.1  tsutsui 	case 8:
   1133  1.4.2.1    lukem 		iha_state_8(sc);
   1134      1.1  tsutsui 		break;
   1135      1.1  tsutsui 
   1136      1.1  tsutsui 	default:
   1137      1.1  tsutsui #ifdef IHA_DEBUG_STATE
   1138      1.1  tsutsui 		printf("[debug] -unknown state: %i-\n",
   1139      1.1  tsutsui 		    sc->sc_actscb->nextstat);
   1140      1.1  tsutsui #endif
   1141  1.4.2.1    lukem 		iha_bad_seq(sc);
   1142      1.1  tsutsui 		break;
   1143      1.1  tsutsui 	}
   1144      1.1  tsutsui 
   1145      1.1  tsutsui 	return (-1);
   1146      1.1  tsutsui }
   1147      1.1  tsutsui 
   1148      1.1  tsutsui /*
   1149  1.4.2.1    lukem  * iha_state_1 - selection is complete after a SELATNSTOP. If the target
   1150      1.1  tsutsui  *               has put the bus into MSG_OUT phase start wide/sync
   1151      1.1  tsutsui  *               negotiation. Otherwise clear the FIFO and go to state 3,
   1152      1.1  tsutsui  *	    	 which will send the SCSI CDB to the target.
   1153      1.1  tsutsui  */
   1154      1.1  tsutsui static int
   1155  1.4.2.1    lukem iha_state_1(sc)
   1156      1.1  tsutsui 	struct iha_softc *sc;
   1157      1.1  tsutsui {
   1158      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1159      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1160      1.1  tsutsui 	struct iha_scsi_req_q *scb = sc->sc_actscb;
   1161      1.1  tsutsui 	struct tcs *tcs;
   1162      1.1  tsutsui 	int flags;
   1163      1.1  tsutsui 
   1164  1.4.2.1    lukem 	iha_mark_busy_scb(scb);
   1165      1.1  tsutsui 
   1166      1.1  tsutsui 	tcs = scb->tcs;
   1167      1.1  tsutsui 
   1168      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
   1169      1.1  tsutsui 
   1170      1.1  tsutsui 	/*
   1171      1.1  tsutsui 	 * If we are in PHASE_MSG_OUT, send
   1172      1.1  tsutsui 	 *     a) IDENT message (with tags if appropriate)
   1173      1.1  tsutsui 	 *     b) WDTR if the target is configured to negotiate wide xfers
   1174      1.1  tsutsui 	 *     ** OR **
   1175      1.1  tsutsui 	 *     c) SDTR if the target is configured to negotiate sync xfers
   1176      1.1  tsutsui 	 *	  but not wide ones
   1177      1.1  tsutsui 	 *
   1178      1.1  tsutsui 	 * If we are NOT, then the target is not asking for anything but
   1179      1.1  tsutsui 	 * the data/command, so go straight to state 3.
   1180      1.1  tsutsui 	 */
   1181      1.1  tsutsui 	if (sc->sc_phase == PHASE_MSG_OUT) {
   1182      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_SCTRL1, (ESBUSIN | EHRSL));
   1183      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_id);
   1184      1.1  tsutsui 
   1185      1.1  tsutsui 		if (scb->scb_tagmsg != 0) {
   1186      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_SFIFO,
   1187      1.1  tsutsui 			    scb->scb_tagmsg);
   1188      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_SFIFO,
   1189      1.1  tsutsui 			    scb->scb_tagid);
   1190      1.1  tsutsui 		}
   1191      1.1  tsutsui 
   1192      1.1  tsutsui 		flags = tcs->flags;
   1193      1.1  tsutsui 		if ((flags & FLAG_NO_NEG_WIDE) == 0) {
   1194  1.4.2.1    lukem 			if (iha_msgout_wdtr(sc) == -1)
   1195      1.1  tsutsui 				return (-1);
   1196      1.1  tsutsui 		} else if ((flags & FLAG_NO_NEG_SYNC) == 0) {
   1197  1.4.2.1    lukem 			if (iha_msgout_sdtr(sc) == -1)
   1198      1.1  tsutsui 				return (-1);
   1199      1.1  tsutsui 		}
   1200      1.1  tsutsui 
   1201      1.1  tsutsui 	} else {
   1202      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1203  1.4.2.1    lukem 		iha_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
   1204      1.1  tsutsui 	}
   1205      1.1  tsutsui 
   1206      1.1  tsutsui 	return (3);
   1207      1.1  tsutsui }
   1208      1.1  tsutsui 
   1209      1.1  tsutsui /*
   1210  1.4.2.1    lukem  * iha_state_2 - selection is complete after a SEL_ATN or SEL_ATN3. If the SCSI
   1211      1.1  tsutsui  *		 CDB has already been send, go to state 4 to start the data
   1212      1.1  tsutsui  *		 xfer. Otherwise reset the FIFO and go to state 3, sending
   1213      1.1  tsutsui  *		 the SCSI CDB.
   1214      1.1  tsutsui  */
   1215      1.1  tsutsui static int
   1216  1.4.2.1    lukem iha_state_2(sc)
   1217      1.1  tsutsui 	struct iha_softc *sc;
   1218      1.1  tsutsui {
   1219      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1220      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1221      1.1  tsutsui 	struct iha_scsi_req_q *scb = sc->sc_actscb;
   1222      1.1  tsutsui 
   1223  1.4.2.1    lukem 	iha_mark_busy_scb(scb);
   1224      1.1  tsutsui 
   1225      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SCONFIG0, scb->tcs->sconfig0);
   1226      1.1  tsutsui 
   1227      1.1  tsutsui 	if ((sc->sc_status1 & CPDNE) != 0)
   1228      1.1  tsutsui 		return (4);
   1229      1.1  tsutsui 
   1230      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1231      1.1  tsutsui 
   1232  1.4.2.1    lukem 	iha_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
   1233      1.1  tsutsui 
   1234      1.1  tsutsui 	return (3);
   1235      1.1  tsutsui }
   1236      1.1  tsutsui 
   1237      1.1  tsutsui /*
   1238  1.4.2.1    lukem  * iha_state_3 - send the SCSI CDB to the target, processing any status
   1239      1.1  tsutsui  *		 or other messages received until that is done or
   1240      1.1  tsutsui  *		 abandoned.
   1241      1.1  tsutsui  */
   1242      1.1  tsutsui static int
   1243  1.4.2.1    lukem iha_state_3(sc)
   1244      1.1  tsutsui 	struct iha_softc *sc;
   1245      1.1  tsutsui {
   1246      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1247      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1248      1.1  tsutsui 	struct iha_scsi_req_q *scb = sc->sc_actscb;
   1249      1.1  tsutsui 	int flags;
   1250      1.1  tsutsui 
   1251      1.1  tsutsui 	for (;;) {
   1252      1.1  tsutsui 		switch (sc->sc_phase) {
   1253      1.1  tsutsui 		case PHASE_CMD_OUT:
   1254      1.1  tsutsui 			bus_space_write_multi_1(iot, ioh, TUL_SFIFO,
   1255      1.1  tsutsui 			    scb->cmd, scb->cmdlen);
   1256  1.4.2.1    lukem 			if (iha_wait(sc, XF_FIFO_OUT) == -1)
   1257      1.1  tsutsui 				return (-1);
   1258      1.1  tsutsui 			else if (sc->sc_phase == PHASE_CMD_OUT) {
   1259  1.4.2.1    lukem 				iha_bad_seq(sc);
   1260      1.1  tsutsui 				return (-1);
   1261      1.1  tsutsui 			} else
   1262      1.1  tsutsui 				return (4);
   1263      1.1  tsutsui 
   1264      1.1  tsutsui 		case PHASE_MSG_IN:
   1265      1.1  tsutsui 			scb->nextstat = 3;
   1266  1.4.2.1    lukem 			if (iha_msgin(sc) == -1)
   1267      1.1  tsutsui 				return (-1);
   1268      1.1  tsutsui 			break;
   1269      1.1  tsutsui 
   1270      1.1  tsutsui 		case PHASE_STATUS_IN:
   1271  1.4.2.1    lukem 			if (iha_status_msg(sc) == -1)
   1272      1.1  tsutsui 				return (-1);
   1273      1.1  tsutsui 			break;
   1274      1.1  tsutsui 
   1275      1.1  tsutsui 		case PHASE_MSG_OUT:
   1276      1.1  tsutsui 			flags = scb->tcs->flags;
   1277      1.1  tsutsui 			if ((flags & FLAG_NO_NEG_SYNC) != 0) {
   1278  1.4.2.1    lukem 				if (iha_msgout(sc, MSG_NOOP) == -1)
   1279      1.1  tsutsui 					return (-1);
   1280  1.4.2.1    lukem 			} else if (iha_msgout_sdtr(sc) == -1)
   1281      1.1  tsutsui 				return (-1);
   1282      1.1  tsutsui 			break;
   1283      1.1  tsutsui 
   1284      1.1  tsutsui 		default:
   1285      1.1  tsutsui 			printf("[debug] -s3- bad phase = %d\n", sc->sc_phase);
   1286  1.4.2.1    lukem 			iha_bad_seq(sc);
   1287      1.1  tsutsui 			return (-1);
   1288      1.1  tsutsui 		}
   1289      1.1  tsutsui 	}
   1290      1.1  tsutsui }
   1291      1.1  tsutsui 
   1292      1.1  tsutsui /*
   1293  1.4.2.1    lukem  * iha_state_4 - start a data xfer. Handle any bus state
   1294      1.1  tsutsui  *               transitions until PHASE_DATA_IN/_OUT
   1295      1.1  tsutsui  *               or the attempt is abandoned. If there is
   1296      1.1  tsutsui  *               no data to xfer, go to state 6 and finish
   1297      1.1  tsutsui  *               processing the current SCB.
   1298      1.1  tsutsui  */
   1299      1.1  tsutsui static int
   1300  1.4.2.1    lukem iha_state_4(sc)
   1301      1.1  tsutsui 	struct iha_softc *sc;
   1302      1.1  tsutsui {
   1303      1.1  tsutsui 	struct iha_scsi_req_q *scb = sc->sc_actscb;
   1304      1.1  tsutsui 
   1305      1.1  tsutsui 	if ((scb->flags & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) ==
   1306      1.1  tsutsui 	    (XS_CTL_DATA_IN | XS_CTL_DATA_OUT))
   1307      1.1  tsutsui 		return (6); /* Both dir flags set => NO xfer was requested */
   1308      1.1  tsutsui 
   1309      1.1  tsutsui 	for (;;) {
   1310      1.1  tsutsui 		if (scb->buflen == 0)
   1311      1.1  tsutsui 			return (6);
   1312      1.1  tsutsui 
   1313      1.1  tsutsui 		switch (sc->sc_phase) {
   1314      1.1  tsutsui 		case PHASE_STATUS_IN:
   1315      1.1  tsutsui 			if ((scb->flags & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT))
   1316      1.1  tsutsui 			    != 0)
   1317  1.4.2.1    lukem 				scb->ha_stat = iha_data_over_run(scb);
   1318  1.4.2.1    lukem 			if ((iha_status_msg(sc)) == -1)
   1319      1.1  tsutsui 				return (-1);
   1320      1.1  tsutsui 			break;
   1321      1.1  tsutsui 
   1322      1.1  tsutsui 		case PHASE_MSG_IN:
   1323      1.1  tsutsui 			scb->nextstat = 4;
   1324  1.4.2.1    lukem 			if (iha_msgin(sc) == -1)
   1325      1.1  tsutsui 				return (-1);
   1326      1.1  tsutsui 			break;
   1327      1.1  tsutsui 
   1328      1.1  tsutsui 		case PHASE_MSG_OUT:
   1329      1.1  tsutsui 			if ((sc->sc_status0 & SPERR) != 0) {
   1330      1.1  tsutsui 				scb->buflen = 0;
   1331      1.1  tsutsui 				scb->ha_stat = HOST_SPERR;
   1332  1.4.2.1    lukem 				if (iha_msgout(sc, MSG_INITIATOR_DET_ERR) == -1)
   1333      1.1  tsutsui 					return (-1);
   1334      1.1  tsutsui 				else
   1335      1.1  tsutsui 					return (6);
   1336      1.1  tsutsui 			} else {
   1337  1.4.2.1    lukem 				if (iha_msgout(sc, MSG_NOOP) == -1)
   1338      1.1  tsutsui 					return (-1);
   1339      1.1  tsutsui 			}
   1340      1.1  tsutsui 			break;
   1341      1.1  tsutsui 
   1342      1.1  tsutsui 		case PHASE_DATA_IN:
   1343  1.4.2.1    lukem 			return (iha_xfer_data(sc, scb, XS_CTL_DATA_IN));
   1344      1.1  tsutsui 
   1345      1.1  tsutsui 		case PHASE_DATA_OUT:
   1346  1.4.2.1    lukem 			return (iha_xfer_data(sc, scb, XS_CTL_DATA_OUT));
   1347      1.1  tsutsui 
   1348      1.1  tsutsui 		default:
   1349  1.4.2.1    lukem 			iha_bad_seq(sc);
   1350      1.1  tsutsui 			return (-1);
   1351      1.1  tsutsui 		}
   1352      1.1  tsutsui 	}
   1353      1.1  tsutsui }
   1354      1.1  tsutsui 
   1355      1.1  tsutsui /*
   1356  1.4.2.1    lukem  * iha_state_5 - handle the partial or final completion of the current
   1357      1.1  tsutsui  *		 data xfer. If DMA is still active stop it. If there is
   1358      1.1  tsutsui  *		 more data to xfer, go to state 4 and start the xfer.
   1359      1.1  tsutsui  *		 If not go to state 6 and finish the SCB.
   1360      1.1  tsutsui  */
   1361      1.1  tsutsui static int
   1362  1.4.2.1    lukem iha_state_5(sc)
   1363      1.1  tsutsui 	struct iha_softc *sc;
   1364      1.1  tsutsui {
   1365      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1366      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1367      1.1  tsutsui 	struct iha_scsi_req_q *scb = sc->sc_actscb;
   1368      1.1  tsutsui 	struct iha_sg_element *sg;
   1369      1.1  tsutsui 	u_int32_t cnt;
   1370      1.1  tsutsui 	u_int8_t period, stat;
   1371      1.1  tsutsui 	long xcnt;  /* cannot use unsigned!! see code: if (xcnt < 0) */
   1372      1.1  tsutsui 	int i;
   1373      1.1  tsutsui 
   1374      1.1  tsutsui 	cnt = bus_space_read_4(iot, ioh, TUL_STCNT0) & TCNT;
   1375      1.1  tsutsui 
   1376      1.1  tsutsui 	/*
   1377      1.1  tsutsui 	 * Stop any pending DMA activity and check for parity error.
   1378      1.1  tsutsui 	 */
   1379      1.1  tsutsui 
   1380      1.1  tsutsui 	if ((bus_space_read_1(iot, ioh, TUL_DCMD) & XDIR) != 0) {
   1381      1.1  tsutsui 		/* Input Operation */
   1382      1.1  tsutsui 		if ((sc->sc_status0 & SPERR) != 0)
   1383      1.1  tsutsui 			scb->ha_stat = HOST_SPERR;
   1384      1.1  tsutsui 
   1385      1.1  tsutsui 		if ((bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND) != 0) {
   1386      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_DCTRL0,
   1387      1.1  tsutsui 			    bus_space_read_1(iot, ioh, TUL_DCTRL0) | SXSTP);
   1388      1.1  tsutsui 			while (bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND)
   1389      1.1  tsutsui 				;
   1390      1.1  tsutsui 		}
   1391      1.1  tsutsui 
   1392      1.1  tsutsui 	} else {
   1393      1.1  tsutsui 		/* Output Operation */
   1394      1.1  tsutsui 		if ((sc->sc_status1 & SXCMP) == 0) {
   1395      1.1  tsutsui 			period = scb->tcs->syncm;
   1396      1.1  tsutsui 			if ((period & PERIOD_WIDE_SCSI) != 0)
   1397      1.1  tsutsui 				cnt += (bus_space_read_1(iot, ioh,
   1398      1.1  tsutsui 				    TUL_SFIFOCNT) & FIFOC) * 2;
   1399      1.1  tsutsui 			else
   1400      1.1  tsutsui 				cnt += bus_space_read_1(iot, ioh,
   1401      1.1  tsutsui 				    TUL_SFIFOCNT) & FIFOC;
   1402      1.1  tsutsui 		}
   1403      1.1  tsutsui 
   1404      1.1  tsutsui 		if ((bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND) != 0) {
   1405      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_DCMD, ABTXFR);
   1406      1.1  tsutsui 			do
   1407      1.1  tsutsui 				stat = bus_space_read_1(iot, ioh, TUL_ISTUS0);
   1408      1.1  tsutsui 			while ((stat & DABT) == 0);
   1409      1.1  tsutsui 		}
   1410      1.1  tsutsui 
   1411      1.1  tsutsui 		if ((cnt == 1) && (sc->sc_phase == PHASE_DATA_OUT)) {
   1412  1.4.2.1    lukem 			if (iha_wait(sc, XF_FIFO_OUT) == -1)
   1413      1.1  tsutsui 				return (-1);
   1414      1.1  tsutsui 			cnt = 0;
   1415      1.1  tsutsui 
   1416      1.1  tsutsui 		} else if ((sc->sc_status1 & SXCMP) == 0)
   1417      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1418      1.1  tsutsui 	}
   1419      1.1  tsutsui 
   1420      1.1  tsutsui 	if (cnt == 0) {
   1421      1.1  tsutsui 		scb->buflen = 0;
   1422      1.1  tsutsui 		return (6);
   1423      1.1  tsutsui 	}
   1424      1.1  tsutsui 
   1425      1.1  tsutsui 	/* Update active data pointer and restart the I/O at the new point */
   1426      1.1  tsutsui 
   1427      1.1  tsutsui 	xcnt = scb->buflen - cnt;	/* xcnt == bytes xferred */
   1428      1.1  tsutsui 	scb->buflen = cnt;	  	/* cnt  == bytes left    */
   1429      1.1  tsutsui 
   1430      1.1  tsutsui 	if ((scb->flags & FLAG_SG) != 0) {
   1431      1.1  tsutsui 		sg = &scb->sglist[scb->sg_index];
   1432      1.1  tsutsui 		for (i = scb->sg_index; i < scb->sg_max; sg++, i++) {
   1433      1.1  tsutsui 			xcnt -= le32toh(sg->sg_len);
   1434      1.1  tsutsui 			if (xcnt < 0) {
   1435      1.1  tsutsui 				xcnt += le32toh(sg->sg_len);
   1436      1.1  tsutsui 
   1437      1.1  tsutsui 				sg->sg_addr =
   1438      1.1  tsutsui 				    htole32(le32toh(sg->sg_addr) + xcnt);
   1439      1.1  tsutsui 				sg->sg_len =
   1440      1.1  tsutsui 				    htole32(le32toh(sg->sg_len) - xcnt);
   1441      1.1  tsutsui 				bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   1442      1.1  tsutsui 				    scb->sgoffset, IHA_SG_SIZE,
   1443      1.1  tsutsui 				    BUS_DMASYNC_PREWRITE);
   1444      1.1  tsutsui 
   1445      1.1  tsutsui 				scb->bufaddr += (i - scb->sg_index) *
   1446      1.1  tsutsui 				    sizeof(struct iha_sg_element);
   1447      1.1  tsutsui 				scb->sg_size = scb->sg_max - i;
   1448      1.1  tsutsui 				scb->sg_index = i;
   1449      1.1  tsutsui 
   1450      1.1  tsutsui 				return (4);
   1451      1.1  tsutsui 			}
   1452      1.1  tsutsui 		}
   1453      1.1  tsutsui 		return (6);
   1454      1.1  tsutsui 
   1455      1.1  tsutsui 	} else
   1456      1.1  tsutsui 		scb->bufaddr += xcnt;
   1457      1.1  tsutsui 
   1458      1.1  tsutsui 	return (4);
   1459      1.1  tsutsui }
   1460      1.1  tsutsui 
   1461      1.1  tsutsui /*
   1462  1.4.2.1    lukem  * iha_state_6 - finish off the active scb (may require several
   1463      1.1  tsutsui  *		 iterations if PHASE_MSG_IN) and return -1 to indicate
   1464      1.1  tsutsui  *		 the bus is free.
   1465      1.1  tsutsui  */
   1466      1.1  tsutsui static int
   1467  1.4.2.1    lukem iha_state_6(sc)
   1468      1.1  tsutsui 	struct iha_softc *sc;
   1469      1.1  tsutsui {
   1470      1.1  tsutsui 
   1471      1.1  tsutsui 	for (;;) {
   1472      1.1  tsutsui 		switch (sc->sc_phase) {
   1473      1.1  tsutsui 		case PHASE_STATUS_IN:
   1474  1.4.2.1    lukem 			if (iha_status_msg(sc) == -1)
   1475      1.1  tsutsui 				return (-1);
   1476      1.1  tsutsui 			break;
   1477      1.1  tsutsui 
   1478      1.1  tsutsui 		case PHASE_MSG_IN:
   1479      1.1  tsutsui 			sc->sc_actscb->nextstat = 6;
   1480  1.4.2.1    lukem 			if ((iha_msgin(sc)) == -1)
   1481      1.1  tsutsui 				return (-1);
   1482      1.1  tsutsui 			break;
   1483      1.1  tsutsui 
   1484      1.1  tsutsui 		case PHASE_MSG_OUT:
   1485  1.4.2.1    lukem 			if ((iha_msgout(sc, MSG_NOOP)) == -1)
   1486      1.1  tsutsui 				return (-1);
   1487      1.1  tsutsui 			break;
   1488      1.1  tsutsui 
   1489      1.1  tsutsui 		case PHASE_DATA_IN:
   1490  1.4.2.1    lukem 			if (iha_xpad_in(sc) == -1)
   1491      1.1  tsutsui 				return (-1);
   1492      1.1  tsutsui 			break;
   1493      1.1  tsutsui 
   1494      1.1  tsutsui 		case PHASE_DATA_OUT:
   1495  1.4.2.1    lukem 			if (iha_xpad_out(sc) == -1)
   1496      1.1  tsutsui 				return (-1);
   1497      1.1  tsutsui 			break;
   1498      1.1  tsutsui 
   1499      1.1  tsutsui 		default:
   1500  1.4.2.1    lukem 			iha_bad_seq(sc);
   1501      1.1  tsutsui 			return (-1);
   1502      1.1  tsutsui 		}
   1503      1.1  tsutsui 	}
   1504      1.1  tsutsui }
   1505      1.1  tsutsui 
   1506      1.1  tsutsui /*
   1507  1.4.2.1    lukem  * iha_state_8 - reset the active device and all busy SCBs using it
   1508      1.1  tsutsui  */
   1509      1.1  tsutsui static int
   1510  1.4.2.1    lukem iha_state_8(sc)
   1511      1.1  tsutsui 	struct iha_softc *sc;
   1512      1.1  tsutsui {
   1513      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1514      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1515      1.1  tsutsui 	struct iha_scsi_req_q *scb;
   1516      1.1  tsutsui 	int i;
   1517      1.1  tsutsui 	u_int8_t tar;
   1518      1.1  tsutsui 
   1519      1.1  tsutsui 	if (sc->sc_phase == PHASE_MSG_OUT) {
   1520      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_BUS_DEV_RESET);
   1521      1.1  tsutsui 
   1522      1.1  tsutsui 		scb = sc->sc_actscb;
   1523      1.1  tsutsui 
   1524      1.1  tsutsui 		/* This SCB finished correctly -- resetting the device */
   1525  1.4.2.1    lukem 		iha_append_done_scb(sc, scb, HOST_OK);
   1526      1.1  tsutsui 
   1527  1.4.2.1    lukem 		iha_reset_tcs(scb->tcs, sc->sc_sconf1);
   1528      1.1  tsutsui 
   1529      1.1  tsutsui 		tar = scb->target;
   1530      1.1  tsutsui 		for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++)
   1531      1.1  tsutsui 			if (scb->target == tar)
   1532      1.1  tsutsui 				switch (scb->status) {
   1533      1.1  tsutsui 				case STATUS_BUSY:
   1534  1.4.2.1    lukem 					iha_append_done_scb(sc,
   1535      1.1  tsutsui 					    scb, HOST_DEV_RST);
   1536      1.1  tsutsui 					break;
   1537      1.1  tsutsui 
   1538      1.1  tsutsui 				case STATUS_SELECT:
   1539  1.4.2.1    lukem 					iha_push_pend_scb(sc, scb);
   1540      1.1  tsutsui 					break;
   1541      1.1  tsutsui 
   1542      1.1  tsutsui 				default:
   1543      1.1  tsutsui 					break;
   1544      1.1  tsutsui 				}
   1545      1.1  tsutsui 
   1546      1.1  tsutsui 		sc->sc_flags |= FLAG_EXPECT_DISC;
   1547      1.1  tsutsui 
   1548  1.4.2.1    lukem 		if (iha_wait(sc, XF_FIFO_OUT) == -1)
   1549      1.1  tsutsui 			return (-1);
   1550      1.1  tsutsui 	}
   1551      1.1  tsutsui 
   1552  1.4.2.1    lukem 	iha_bad_seq(sc);
   1553      1.1  tsutsui 	return (-1);
   1554      1.1  tsutsui }
   1555      1.1  tsutsui 
   1556      1.1  tsutsui /*
   1557  1.4.2.1    lukem  * iha_xfer_data - initiate the DMA xfer of the data
   1558      1.1  tsutsui  */
   1559      1.1  tsutsui static int
   1560  1.4.2.1    lukem iha_xfer_data(sc, scb, direction)
   1561      1.1  tsutsui 	struct iha_softc *sc;
   1562      1.1  tsutsui 	struct iha_scsi_req_q *scb;
   1563      1.1  tsutsui 	int direction;
   1564      1.1  tsutsui {
   1565      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1566      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1567      1.1  tsutsui 	u_int32_t xferlen;
   1568      1.1  tsutsui 	u_int8_t xfertype;
   1569      1.1  tsutsui 
   1570      1.1  tsutsui 	if ((scb->flags & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) != direction)
   1571      1.1  tsutsui 		return (6); /* wrong direction, abandon I/O */
   1572      1.1  tsutsui 
   1573      1.1  tsutsui 	bus_space_write_4(iot, ioh, TUL_STCNT0, scb->buflen);
   1574      1.1  tsutsui 
   1575      1.1  tsutsui 	if ((scb->flags & FLAG_SG) == 0) {
   1576      1.1  tsutsui 		xferlen = scb->buflen;
   1577      1.1  tsutsui 		xfertype = (direction == XS_CTL_DATA_IN) ? ST_X_IN : ST_X_OUT;
   1578      1.1  tsutsui 
   1579      1.1  tsutsui 	} else {
   1580      1.1  tsutsui 		xferlen = scb->sg_size * sizeof(struct iha_sg_element);
   1581      1.1  tsutsui 		xfertype = (direction == XS_CTL_DATA_IN) ? ST_SG_IN : ST_SG_OUT;
   1582      1.1  tsutsui 	}
   1583      1.1  tsutsui 
   1584      1.1  tsutsui 	bus_space_write_4(iot, ioh, TUL_DXC,  xferlen);
   1585      1.1  tsutsui 	bus_space_write_4(iot, ioh, TUL_DXPA, scb->bufaddr);
   1586      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_DCMD, xfertype);
   1587      1.1  tsutsui 
   1588      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SCMD,
   1589      1.1  tsutsui 	    (direction == XS_CTL_DATA_IN) ? XF_DMA_IN : XF_DMA_OUT);
   1590      1.1  tsutsui 
   1591      1.1  tsutsui 	scb->nextstat = 5;
   1592      1.1  tsutsui 
   1593      1.1  tsutsui 	return (0);
   1594      1.1  tsutsui }
   1595      1.1  tsutsui 
   1596      1.1  tsutsui static int
   1597  1.4.2.1    lukem iha_xpad_in(sc)
   1598      1.1  tsutsui 	struct iha_softc *sc;
   1599      1.1  tsutsui {
   1600      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1601      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1602      1.1  tsutsui 	struct iha_scsi_req_q *scb = sc->sc_actscb;
   1603      1.1  tsutsui 
   1604      1.1  tsutsui 	if ((scb->flags & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) != 0)
   1605      1.1  tsutsui 		scb->ha_stat = HOST_DO_DU;
   1606      1.1  tsutsui 
   1607      1.1  tsutsui 	for (;;) {
   1608      1.1  tsutsui 		if ((scb->tcs->syncm & PERIOD_WIDE_SCSI) != 0)
   1609      1.1  tsutsui 			bus_space_write_4(iot, ioh, TUL_STCNT0, 2);
   1610      1.1  tsutsui 		else
   1611      1.1  tsutsui 			bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
   1612      1.1  tsutsui 
   1613  1.4.2.1    lukem 		switch (iha_wait(sc, XF_FIFO_IN)) {
   1614      1.1  tsutsui 		case -1:
   1615      1.1  tsutsui 			return (-1);
   1616      1.1  tsutsui 
   1617      1.1  tsutsui 		case PHASE_DATA_IN:
   1618      1.1  tsutsui 			bus_space_read_1(iot, ioh, TUL_SFIFO);
   1619      1.1  tsutsui 			break;
   1620      1.1  tsutsui 
   1621      1.1  tsutsui 		default:
   1622      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1623      1.1  tsutsui 			return (6);
   1624      1.1  tsutsui 		}
   1625      1.1  tsutsui 	}
   1626      1.1  tsutsui }
   1627      1.1  tsutsui 
   1628      1.1  tsutsui static int
   1629  1.4.2.1    lukem iha_xpad_out(sc)
   1630      1.1  tsutsui 	struct iha_softc *sc;
   1631      1.1  tsutsui {
   1632      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1633      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1634      1.1  tsutsui 	struct iha_scsi_req_q *scb = sc->sc_actscb;
   1635      1.1  tsutsui 
   1636      1.1  tsutsui 	if ((scb->flags & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) != 0)
   1637      1.1  tsutsui 		scb->ha_stat = HOST_DO_DU;
   1638      1.1  tsutsui 
   1639      1.1  tsutsui 	for (;;) {
   1640      1.1  tsutsui 		if ((scb->tcs->syncm & PERIOD_WIDE_SCSI) != 0)
   1641      1.1  tsutsui 			bus_space_write_4(iot, ioh, TUL_STCNT0, 2);
   1642      1.1  tsutsui 		else
   1643      1.1  tsutsui 			bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
   1644      1.1  tsutsui 
   1645      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_SFIFO, 0);
   1646      1.1  tsutsui 
   1647  1.4.2.1    lukem 		switch (iha_wait(sc, XF_FIFO_OUT)) {
   1648      1.1  tsutsui 		case -1:
   1649      1.1  tsutsui 			return (-1);
   1650      1.1  tsutsui 
   1651      1.1  tsutsui 		case PHASE_DATA_OUT:
   1652      1.1  tsutsui 			break;
   1653      1.1  tsutsui 
   1654      1.1  tsutsui 		default:
   1655      1.1  tsutsui 			/* Disable wide CPU to allow read 16 bits */
   1656      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
   1657      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1658      1.1  tsutsui 			return (6);
   1659      1.1  tsutsui 		}
   1660      1.1  tsutsui 	}
   1661      1.1  tsutsui }
   1662      1.1  tsutsui 
   1663      1.1  tsutsui static int
   1664  1.4.2.1    lukem iha_status_msg(sc)
   1665      1.1  tsutsui 	struct iha_softc *sc;
   1666      1.1  tsutsui {
   1667      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1668      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1669      1.1  tsutsui 	struct iha_scsi_req_q *scb;
   1670      1.1  tsutsui 	u_int8_t msg;
   1671      1.1  tsutsui 	int phase;
   1672      1.1  tsutsui 
   1673  1.4.2.1    lukem 	if ((phase = iha_wait(sc, CMD_COMP)) == -1)
   1674      1.1  tsutsui 		return (-1);
   1675      1.1  tsutsui 
   1676      1.1  tsutsui 	scb = sc->sc_actscb;
   1677      1.1  tsutsui 
   1678      1.1  tsutsui 	scb->ta_stat = bus_space_read_1(iot, ioh, TUL_SFIFO);
   1679      1.1  tsutsui 
   1680      1.1  tsutsui 	if (phase == PHASE_MSG_OUT) {
   1681      1.1  tsutsui 		if ((sc->sc_status0 & SPERR) == 0)
   1682      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_NOOP);
   1683      1.1  tsutsui 		else
   1684      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_SFIFO,
   1685      1.1  tsutsui 			    MSG_PARITY_ERROR);
   1686      1.1  tsutsui 
   1687  1.4.2.1    lukem 		return (iha_wait(sc, XF_FIFO_OUT));
   1688      1.1  tsutsui 
   1689      1.1  tsutsui 	} else if (phase == PHASE_MSG_IN) {
   1690      1.1  tsutsui 		msg = bus_space_read_1(iot, ioh, TUL_SFIFO);
   1691      1.1  tsutsui 
   1692      1.1  tsutsui 		if ((sc->sc_status0 & SPERR) != 0)
   1693  1.4.2.1    lukem 			switch (iha_wait(sc, MSG_ACCEPT)) {
   1694      1.1  tsutsui 			case -1:
   1695      1.1  tsutsui 				return (-1);
   1696      1.1  tsutsui 			case PHASE_MSG_OUT:
   1697      1.1  tsutsui 				bus_space_write_1(iot, ioh, TUL_SFIFO,
   1698      1.1  tsutsui 				    MSG_PARITY_ERROR);
   1699  1.4.2.1    lukem 				return (iha_wait(sc, XF_FIFO_OUT));
   1700      1.1  tsutsui 			default:
   1701  1.4.2.1    lukem 				iha_bad_seq(sc);
   1702      1.1  tsutsui 				return (-1);
   1703      1.1  tsutsui 			}
   1704      1.1  tsutsui 
   1705      1.1  tsutsui 		if (msg == MSG_CMDCOMPLETE) {
   1706      1.1  tsutsui 			if ((scb->ta_stat &
   1707      1.1  tsutsui 			    (SCSI_INTERM | SCSI_BUSY)) == SCSI_INTERM) {
   1708  1.4.2.1    lukem 				iha_bad_seq(sc);
   1709      1.1  tsutsui 				return (-1);
   1710      1.1  tsutsui 			}
   1711      1.1  tsutsui 			sc->sc_flags |= FLAG_EXPECT_DONE_DISC;
   1712      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1713  1.4.2.1    lukem 			return (iha_wait(sc, MSG_ACCEPT));
   1714      1.1  tsutsui 		}
   1715      1.1  tsutsui 
   1716      1.1  tsutsui 		if ((msg == MSG_LINK_CMD_COMPLETE)
   1717      1.1  tsutsui 		    || (msg == MSG_LINK_CMD_COMPLETEF)) {
   1718      1.1  tsutsui 			if ((scb->ta_stat &
   1719      1.1  tsutsui 			    (SCSI_INTERM | SCSI_BUSY)) == SCSI_INTERM)
   1720  1.4.2.1    lukem 				return (iha_wait(sc, MSG_ACCEPT));
   1721      1.1  tsutsui 		}
   1722      1.1  tsutsui 	}
   1723      1.1  tsutsui 
   1724  1.4.2.1    lukem 	iha_bad_seq(sc);
   1725      1.1  tsutsui 	return (-1);
   1726      1.1  tsutsui }
   1727      1.1  tsutsui 
   1728      1.1  tsutsui /*
   1729  1.4.2.1    lukem  * iha_busfree - SCSI bus free detected as a result of a TIMEOUT or
   1730      1.1  tsutsui  *		 DISCONNECT interrupt. Reset the tulip FIFO and
   1731      1.1  tsutsui  *		 SCONFIG0 and enable hardware reselect. Move any active
   1732      1.1  tsutsui  *		 SCB to sc_donescb list. Return an appropriate host status
   1733      1.1  tsutsui  *		 if an I/O was active.
   1734      1.1  tsutsui  */
   1735      1.1  tsutsui static void
   1736  1.4.2.1    lukem iha_busfree(sc)
   1737      1.1  tsutsui 	struct iha_softc *sc;
   1738      1.1  tsutsui {
   1739      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1740      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1741      1.1  tsutsui 	struct iha_scsi_req_q *scb;
   1742      1.1  tsutsui 
   1743      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1744      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SCONFIG0, SCONFIG0DEFAULT);
   1745      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
   1746      1.1  tsutsui 
   1747      1.1  tsutsui 	scb = sc->sc_actscb;
   1748      1.1  tsutsui 
   1749      1.1  tsutsui 	if (scb != NULL) {
   1750      1.1  tsutsui 		if (scb->status == STATUS_SELECT)
   1751      1.1  tsutsui 			/* selection timeout   */
   1752  1.4.2.1    lukem 			iha_append_done_scb(sc, scb, HOST_SEL_TOUT);
   1753      1.1  tsutsui 		else
   1754      1.1  tsutsui 			/* Unexpected bus free */
   1755  1.4.2.1    lukem 			iha_append_done_scb(sc, scb, HOST_BAD_PHAS);
   1756      1.1  tsutsui 	}
   1757      1.1  tsutsui }
   1758      1.1  tsutsui 
   1759      1.1  tsutsui static void
   1760  1.4.2.1    lukem iha_reset_scsi_bus(sc)
   1761      1.1  tsutsui 	struct iha_softc *sc;
   1762      1.1  tsutsui {
   1763      1.1  tsutsui 	struct iha_scsi_req_q *scb;
   1764      1.1  tsutsui 	struct tcs *tcs;
   1765      1.1  tsutsui 	int i, s;
   1766      1.1  tsutsui 
   1767      1.1  tsutsui 	s = splbio();
   1768      1.1  tsutsui 
   1769  1.4.2.1    lukem 	iha_reset_dma(sc);
   1770      1.1  tsutsui 
   1771      1.1  tsutsui 	for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++)
   1772      1.1  tsutsui 		switch (scb->status) {
   1773      1.1  tsutsui 		case STATUS_BUSY:
   1774  1.4.2.1    lukem 			iha_append_done_scb(sc, scb, HOST_SCSI_RST);
   1775      1.1  tsutsui 			break;
   1776      1.1  tsutsui 
   1777      1.1  tsutsui 		case STATUS_SELECT:
   1778  1.4.2.1    lukem 			iha_push_pend_scb(sc, scb);
   1779      1.1  tsutsui 			break;
   1780      1.1  tsutsui 
   1781      1.1  tsutsui 		default:
   1782      1.1  tsutsui 			break;
   1783      1.1  tsutsui 		}
   1784      1.1  tsutsui 
   1785      1.1  tsutsui 	for (i = 0, tcs = sc->sc_tcs; i < IHA_MAX_TARGETS; i++, tcs++)
   1786  1.4.2.1    lukem 		iha_reset_tcs(tcs, sc->sc_sconf1);
   1787      1.1  tsutsui 
   1788      1.1  tsutsui 	splx(s);
   1789      1.1  tsutsui }
   1790      1.1  tsutsui 
   1791      1.1  tsutsui /*
   1792  1.4.2.1    lukem  * iha_resel - handle a detected SCSI bus reselection request.
   1793      1.1  tsutsui  */
   1794      1.1  tsutsui static int
   1795  1.4.2.1    lukem iha_resel(sc)
   1796      1.1  tsutsui 	struct iha_softc *sc;
   1797      1.1  tsutsui {
   1798      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1799      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1800      1.1  tsutsui 	struct iha_scsi_req_q *scb;
   1801      1.1  tsutsui 	struct tcs *tcs;
   1802      1.1  tsutsui 	u_int8_t tag, target, lun, msg, abortmsg;
   1803      1.1  tsutsui 
   1804      1.1  tsutsui 	if (sc->sc_actscb != NULL) {
   1805      1.1  tsutsui 		if ((sc->sc_actscb->status == STATUS_SELECT))
   1806  1.4.2.1    lukem 			iha_push_pend_scb(sc, sc->sc_actscb);
   1807  1.4.2.1    lukem 		sc->sc_actscb = NULL;
   1808      1.1  tsutsui 	}
   1809      1.1  tsutsui 
   1810      1.1  tsutsui 	target = bus_space_read_1(iot, ioh, TUL_SBID);
   1811      1.1  tsutsui 	lun = bus_space_read_1(iot, ioh, TUL_SALVC) & MSG_IDENTIFY_LUNMASK;
   1812      1.1  tsutsui 
   1813      1.1  tsutsui 	tcs = &sc->sc_tcs[target];
   1814      1.1  tsutsui 
   1815      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
   1816      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
   1817      1.1  tsutsui 
   1818      1.1  tsutsui 	abortmsg = MSG_ABORT; /* until a valid tag has been obtained */
   1819      1.1  tsutsui 
   1820      1.1  tsutsui 	if (tcs->ntagscb != NULL)
   1821      1.1  tsutsui 		/* There is a non-tagged I/O active on the target */
   1822      1.1  tsutsui 		scb = tcs->ntagscb;
   1823      1.1  tsutsui 
   1824      1.1  tsutsui 	else {
   1825      1.1  tsutsui 		/*
   1826      1.1  tsutsui 		 * Since there is no active non-tagged operation
   1827      1.1  tsutsui 		 * read the tag type, the tag itself, and find
   1828      1.1  tsutsui 		 * the appropriate scb by indexing sc_scb with
   1829      1.1  tsutsui 		 * the tag.
   1830      1.1  tsutsui 		 */
   1831      1.1  tsutsui 
   1832  1.4.2.1    lukem 		switch (iha_wait(sc, MSG_ACCEPT)) {
   1833      1.1  tsutsui 		case -1:
   1834      1.1  tsutsui 			return (-1);
   1835      1.1  tsutsui 		case PHASE_MSG_IN:
   1836      1.1  tsutsui 			bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
   1837  1.4.2.1    lukem 			if ((iha_wait(sc, XF_FIFO_IN)) == -1)
   1838      1.1  tsutsui 				return (-1);
   1839      1.1  tsutsui 			break;
   1840      1.1  tsutsui 		default:
   1841      1.1  tsutsui 			goto abort;
   1842      1.1  tsutsui 		}
   1843      1.1  tsutsui 
   1844      1.1  tsutsui 		msg = bus_space_read_1(iot, ioh, TUL_SFIFO); /* Read Tag Msg */
   1845      1.1  tsutsui 
   1846      1.1  tsutsui 		if ((msg < MSG_SIMPLE_Q_TAG) || (msg > MSG_ORDERED_Q_TAG))
   1847      1.1  tsutsui 			goto abort;
   1848      1.1  tsutsui 
   1849  1.4.2.1    lukem 		switch (iha_wait(sc, MSG_ACCEPT)) {
   1850      1.1  tsutsui 		case -1:
   1851      1.1  tsutsui 			return (-1);
   1852      1.1  tsutsui 		case PHASE_MSG_IN:
   1853      1.1  tsutsui 			bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
   1854  1.4.2.1    lukem 			if ((iha_wait(sc, XF_FIFO_IN)) == -1)
   1855      1.1  tsutsui 				return (-1);
   1856      1.1  tsutsui 			break;
   1857      1.1  tsutsui 		default:
   1858      1.1  tsutsui 			goto abort;
   1859      1.1  tsutsui 		}
   1860      1.1  tsutsui 
   1861      1.1  tsutsui 		tag  = bus_space_read_1(iot, ioh, TUL_SFIFO); /* Read Tag ID */
   1862      1.1  tsutsui 		scb = &sc->sc_scb[tag];
   1863      1.1  tsutsui 
   1864      1.1  tsutsui 		abortmsg = MSG_ABORT_TAG; /* Now that we have valdid tag! */
   1865      1.1  tsutsui 	}
   1866      1.1  tsutsui 
   1867      1.1  tsutsui 	if ((scb->target != target)
   1868      1.1  tsutsui 	    || (scb->lun != lun)
   1869      1.1  tsutsui 	    || (scb->status != STATUS_BUSY)) {
   1870      1.1  tsutsui  abort:
   1871  1.4.2.1    lukem 		iha_msgout_abort(sc, abortmsg);
   1872      1.1  tsutsui 		return (-1);
   1873      1.1  tsutsui 	}
   1874      1.1  tsutsui 
   1875      1.1  tsutsui 	sc->sc_actscb = scb;
   1876      1.1  tsutsui 
   1877  1.4.2.1    lukem 	if (iha_wait(sc, MSG_ACCEPT) == -1)
   1878      1.1  tsutsui 		return (-1);
   1879      1.1  tsutsui 
   1880  1.4.2.1    lukem 	return (iha_next_state(sc));
   1881      1.1  tsutsui }
   1882      1.1  tsutsui 
   1883      1.1  tsutsui static int
   1884  1.4.2.1    lukem iha_msgin(sc)
   1885      1.1  tsutsui 	struct iha_softc *sc;
   1886      1.1  tsutsui {
   1887      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1888      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1889      1.1  tsutsui 	int flags;
   1890      1.1  tsutsui 	int phase;
   1891      1.1  tsutsui 	u_int8_t msg;
   1892      1.1  tsutsui 
   1893      1.1  tsutsui 	for (;;) {
   1894      1.1  tsutsui 		if ((bus_space_read_1(iot, ioh, TUL_SFIFOCNT) & FIFOC) > 0)
   1895      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1896      1.1  tsutsui 
   1897      1.1  tsutsui 		bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
   1898      1.1  tsutsui 
   1899  1.4.2.1    lukem 		phase = iha_wait(sc, XF_FIFO_IN);
   1900      1.1  tsutsui 		msg = bus_space_read_1(iot, ioh, TUL_SFIFO);
   1901      1.1  tsutsui 
   1902      1.1  tsutsui 		switch (msg) {
   1903      1.1  tsutsui 		case MSG_DISCONNECT:
   1904      1.1  tsutsui 			sc->sc_flags |= FLAG_EXPECT_DISC;
   1905  1.4.2.1    lukem 			if (iha_wait(sc, MSG_ACCEPT) != -1)
   1906  1.4.2.1    lukem 				iha_bad_seq(sc);
   1907      1.1  tsutsui 			phase = -1;
   1908      1.1  tsutsui 			break;
   1909      1.1  tsutsui 		case MSG_SAVEDATAPOINTER:
   1910      1.1  tsutsui 		case MSG_RESTOREPOINTERS:
   1911      1.1  tsutsui 		case MSG_NOOP:
   1912  1.4.2.1    lukem 			phase = iha_wait(sc, MSG_ACCEPT);
   1913      1.1  tsutsui 			break;
   1914      1.1  tsutsui 		case MSG_MESSAGE_REJECT:
   1915      1.1  tsutsui 			/* XXX - need to clear FIFO like other 'Clear ATN'?*/
   1916  1.4.2.1    lukem 			iha_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
   1917      1.1  tsutsui 			flags = sc->sc_actscb->tcs->flags;
   1918      1.1  tsutsui 			if ((flags & FLAG_NO_NEG_SYNC) == 0)
   1919  1.4.2.1    lukem 				iha_set_ssig(sc, REQ | BSY | SEL, ATN);
   1920  1.4.2.1    lukem 			phase = iha_wait(sc, MSG_ACCEPT);
   1921      1.1  tsutsui 			break;
   1922      1.1  tsutsui 		case MSG_EXTENDED:
   1923  1.4.2.1    lukem 			phase = iha_msgin_extended(sc);
   1924      1.1  tsutsui 			break;
   1925      1.1  tsutsui 		case MSG_IGN_WIDE_RESIDUE:
   1926  1.4.2.1    lukem 			phase = iha_msgin_ignore_wid_resid(sc);
   1927      1.1  tsutsui 			break;
   1928      1.1  tsutsui 		case MSG_CMDCOMPLETE:
   1929      1.1  tsutsui 			sc->sc_flags |= FLAG_EXPECT_DONE_DISC;
   1930      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1931  1.4.2.1    lukem 			phase = iha_wait(sc, MSG_ACCEPT);
   1932      1.1  tsutsui 			if (phase != -1) {
   1933  1.4.2.1    lukem 				iha_bad_seq(sc);
   1934      1.1  tsutsui 				return (-1);
   1935      1.1  tsutsui 			}
   1936      1.1  tsutsui 			break;
   1937      1.1  tsutsui 		default:
   1938  1.4.2.1    lukem 			printf("[debug] iha_msgin: bad msg type: %d\n", msg);
   1939  1.4.2.1    lukem 			phase = iha_msgout_reject(sc);
   1940      1.1  tsutsui 			break;
   1941      1.1  tsutsui 		}
   1942      1.1  tsutsui 
   1943      1.1  tsutsui 		if (phase != PHASE_MSG_IN)
   1944      1.1  tsutsui 			return (phase);
   1945      1.1  tsutsui 	}
   1946      1.1  tsutsui 	/* NOTREACHED */
   1947      1.1  tsutsui }
   1948      1.1  tsutsui 
   1949      1.1  tsutsui static int
   1950  1.4.2.1    lukem iha_msgin_ignore_wid_resid(sc)
   1951      1.1  tsutsui 	struct iha_softc *sc;
   1952      1.1  tsutsui {
   1953      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1954      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1955      1.1  tsutsui 	int phase;
   1956      1.1  tsutsui 
   1957  1.4.2.1    lukem 	phase = iha_wait(sc, MSG_ACCEPT);
   1958      1.1  tsutsui 
   1959      1.1  tsutsui 	if (phase == PHASE_MSG_IN) {
   1960  1.4.2.1    lukem 		phase = iha_wait(sc, XF_FIFO_IN);
   1961      1.1  tsutsui 
   1962  1.4.2.1    lukem 		if (phase != -1) {
   1963  1.4.2.1    lukem 			bus_space_write_1(iot, ioh, TUL_SFIFO, 0);
   1964  1.4.2.1    lukem 			bus_space_read_1(iot, ioh, TUL_SFIFO);
   1965  1.4.2.1    lukem 			bus_space_read_1(iot, ioh, TUL_SFIFO);
   1966      1.1  tsutsui 
   1967  1.4.2.1    lukem 			phase = iha_wait(sc, MSG_ACCEPT);
   1968  1.4.2.1    lukem 		}
   1969      1.1  tsutsui 	}
   1970  1.4.2.1    lukem 
   1971  1.4.2.1    lukem 	return (phase);
   1972      1.1  tsutsui }
   1973      1.1  tsutsui 
   1974      1.1  tsutsui static int
   1975  1.4.2.1    lukem iha_msgin_extended(sc)
   1976      1.1  tsutsui 	struct iha_softc *sc;
   1977      1.1  tsutsui {
   1978      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1979      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1980      1.1  tsutsui 	int flags, i, phase, msglen, msgcode;
   1981      1.1  tsutsui 
   1982      1.1  tsutsui 	/*
   1983      1.1  tsutsui 	 * XXX - can we just stop reading and reject, or do we have to
   1984      1.1  tsutsui 	 *	 read all input, discarding the excess, and then reject
   1985      1.1  tsutsui 	 */
   1986      1.1  tsutsui 	for (i = 0; i < IHA_MAX_EXTENDED_MSG; i++) {
   1987  1.4.2.1    lukem 		phase = iha_wait(sc, MSG_ACCEPT);
   1988      1.1  tsutsui 
   1989      1.1  tsutsui 		if (phase != PHASE_MSG_IN)
   1990      1.1  tsutsui 			return (phase);
   1991      1.1  tsutsui 
   1992      1.1  tsutsui 		bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
   1993      1.1  tsutsui 
   1994  1.4.2.1    lukem 		if (iha_wait(sc, XF_FIFO_IN) == -1)
   1995      1.1  tsutsui 			return (-1);
   1996      1.1  tsutsui 
   1997      1.1  tsutsui 		sc->sc_msg[i] = bus_space_read_1(iot, ioh, TUL_SFIFO);
   1998      1.1  tsutsui 
   1999      1.1  tsutsui 		if (sc->sc_msg[0] == i)
   2000      1.1  tsutsui 			break;
   2001      1.1  tsutsui 	}
   2002      1.1  tsutsui 
   2003      1.1  tsutsui 	msglen	= sc->sc_msg[0];
   2004      1.1  tsutsui 	msgcode = sc->sc_msg[1];
   2005      1.1  tsutsui 
   2006      1.1  tsutsui 	if ((msglen == MSG_EXT_SDTR_LEN) && (msgcode == MSG_EXT_SDTR)) {
   2007  1.4.2.1    lukem 		if (iha_msgin_sdtr(sc) == 0) {
   2008  1.4.2.1    lukem 			iha_sync_done(sc);
   2009  1.4.2.1    lukem 			return (iha_wait(sc, MSG_ACCEPT));
   2010      1.1  tsutsui 		}
   2011      1.1  tsutsui 
   2012  1.4.2.1    lukem 		iha_set_ssig(sc, REQ | BSY | SEL, ATN);
   2013      1.1  tsutsui 
   2014  1.4.2.1    lukem 		phase = iha_wait(sc, MSG_ACCEPT);
   2015      1.1  tsutsui 		if (phase != PHASE_MSG_OUT)
   2016      1.1  tsutsui 			return (phase);
   2017      1.1  tsutsui 
   2018      1.1  tsutsui 		/* Clear FIFO for important message - final SYNC offer */
   2019      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   2020      1.1  tsutsui 
   2021  1.4.2.1    lukem 		iha_sync_done(sc); /* This is our final offer */
   2022      1.1  tsutsui 
   2023      1.1  tsutsui 	} else if ((msglen == MSG_EXT_WDTR_LEN) && (msgcode == MSG_EXT_WDTR)) {
   2024      1.1  tsutsui 
   2025      1.1  tsutsui 		flags = sc->sc_actscb->tcs->flags;
   2026      1.1  tsutsui 
   2027      1.1  tsutsui 		if ((flags & FLAG_NO_WIDE) != 0)
   2028      1.1  tsutsui 			sc->sc_msg[2] = 0;	/* Offer async xfers only    */
   2029      1.1  tsutsui 
   2030      1.1  tsutsui 		else if (sc->sc_msg[2] > 2)	/* BAD MSG: 2 is max  value  */
   2031  1.4.2.1    lukem 			return (iha_msgout_reject(sc));
   2032      1.1  tsutsui 
   2033      1.1  tsutsui 		else if (sc->sc_msg[2] == 2)	/* a request for 32 bit xfers*/
   2034      1.1  tsutsui 			sc->sc_msg[2] = 1;	/* Offer 16 instead	     */
   2035      1.1  tsutsui 
   2036      1.1  tsutsui 		else {
   2037  1.4.2.1    lukem 			iha_wide_done(sc);
   2038      1.1  tsutsui 			if ((flags & FLAG_NO_NEG_SYNC) == 0)
   2039  1.4.2.1    lukem 				iha_set_ssig(sc, REQ | BSY | SEL, ATN);
   2040  1.4.2.1    lukem 			return (iha_wait(sc, MSG_ACCEPT));
   2041      1.1  tsutsui 		}
   2042      1.1  tsutsui 
   2043  1.4.2.1    lukem 		iha_set_ssig(sc, REQ | BSY | SEL, ATN);
   2044      1.1  tsutsui 
   2045  1.4.2.1    lukem 		phase = iha_wait(sc, MSG_ACCEPT);
   2046      1.1  tsutsui 		if (phase != PHASE_MSG_OUT)
   2047      1.1  tsutsui 			return (phase);
   2048      1.1  tsutsui 	} else
   2049  1.4.2.1    lukem 		return (iha_msgout_reject(sc));
   2050      1.1  tsutsui 
   2051  1.4.2.1    lukem 	return (iha_msgout_extended(sc));
   2052      1.1  tsutsui }
   2053      1.1  tsutsui 
   2054      1.1  tsutsui /*
   2055  1.4.2.1    lukem  * iha_msgin_sdtr - check SDTR msg in sc_msg. If the offer is
   2056      1.1  tsutsui  *		    acceptable leave sc_msg as is and return 0.
   2057      1.1  tsutsui  *		    If the negotiation must continue, modify sc_msg
   2058      1.1  tsutsui  *		    as needed and return 1. Else return 0.
   2059      1.1  tsutsui  */
   2060      1.1  tsutsui static int
   2061  1.4.2.1    lukem iha_msgin_sdtr(sc)
   2062      1.1  tsutsui 	struct iha_softc *sc;
   2063      1.1  tsutsui {
   2064      1.1  tsutsui 	int flags;
   2065      1.1  tsutsui 	int newoffer;
   2066      1.1  tsutsui 	u_int8_t default_period;
   2067      1.1  tsutsui 
   2068      1.1  tsutsui 	flags = sc->sc_actscb->tcs->flags;
   2069      1.1  tsutsui 
   2070  1.4.2.1    lukem 	default_period = iha_rate_tbl[flags & FLAG_SCSI_RATE];
   2071      1.1  tsutsui 
   2072      1.1  tsutsui 	if (sc->sc_msg[3] == 0) /* target offered async only. Accept it. */
   2073      1.1  tsutsui 		return (0);
   2074      1.1  tsutsui 
   2075      1.1  tsutsui 	newoffer = 0;
   2076      1.1  tsutsui 
   2077      1.1  tsutsui 	if ((flags & FLAG_NO_SYNC) != 0) {
   2078      1.1  tsutsui 		sc->sc_msg[3] = 0;
   2079      1.1  tsutsui 		newoffer   = 1;
   2080      1.1  tsutsui 	}
   2081      1.1  tsutsui 
   2082      1.1  tsutsui 	if (sc->sc_msg[3] > IHA_MAX_OFFSET) {
   2083      1.1  tsutsui 		sc->sc_msg[3] = IHA_MAX_OFFSET;
   2084      1.1  tsutsui 		newoffer   = 1;
   2085      1.1  tsutsui 	}
   2086      1.1  tsutsui 
   2087      1.1  tsutsui 	if (sc->sc_msg[2] < default_period) {
   2088      1.1  tsutsui 		sc->sc_msg[2] = default_period;
   2089      1.1  tsutsui 		newoffer   = 1;
   2090      1.1  tsutsui 	}
   2091      1.1  tsutsui 
   2092      1.1  tsutsui 	if (sc->sc_msg[2] >= 59) { /* XXX magic */
   2093      1.1  tsutsui 		sc->sc_msg[3] = 0;
   2094      1.1  tsutsui 		newoffer   = 1;
   2095      1.1  tsutsui 	}
   2096      1.1  tsutsui 
   2097      1.1  tsutsui 	return (newoffer);
   2098      1.1  tsutsui }
   2099      1.1  tsutsui 
   2100      1.1  tsutsui static int
   2101  1.4.2.1    lukem iha_msgout(sc, msg)
   2102      1.1  tsutsui 	struct iha_softc *sc;
   2103      1.1  tsutsui 	u_int8_t msg;
   2104      1.1  tsutsui {
   2105      1.1  tsutsui 
   2106      1.1  tsutsui 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, TUL_SFIFO, msg);
   2107      1.1  tsutsui 
   2108  1.4.2.1    lukem 	return (iha_wait(sc, XF_FIFO_OUT));
   2109      1.1  tsutsui }
   2110      1.1  tsutsui 
   2111      1.1  tsutsui static void
   2112  1.4.2.1    lukem iha_msgout_abort(sc, aborttype)
   2113      1.1  tsutsui 	struct iha_softc *sc;
   2114      1.1  tsutsui 	u_int8_t aborttype;
   2115      1.1  tsutsui {
   2116      1.1  tsutsui 
   2117  1.4.2.1    lukem 	iha_set_ssig(sc, REQ | BSY | SEL, ATN);
   2118      1.1  tsutsui 
   2119  1.4.2.1    lukem 	switch (iha_wait(sc, MSG_ACCEPT)) {
   2120      1.1  tsutsui 	case -1:
   2121      1.1  tsutsui 		break;
   2122      1.1  tsutsui 
   2123      1.1  tsutsui 	case PHASE_MSG_OUT:
   2124      1.1  tsutsui 		sc->sc_flags |= FLAG_EXPECT_DISC;
   2125  1.4.2.1    lukem 		if (iha_msgout(sc, aborttype) != -1)
   2126  1.4.2.1    lukem 			iha_bad_seq(sc);
   2127      1.1  tsutsui 		break;
   2128      1.1  tsutsui 
   2129      1.1  tsutsui 	default:
   2130  1.4.2.1    lukem 		iha_bad_seq(sc);
   2131      1.1  tsutsui 		break;
   2132      1.1  tsutsui 	}
   2133      1.1  tsutsui }
   2134      1.1  tsutsui 
   2135      1.1  tsutsui static int
   2136  1.4.2.1    lukem iha_msgout_reject(sc)
   2137      1.1  tsutsui 	struct iha_softc *sc;
   2138      1.1  tsutsui {
   2139      1.1  tsutsui 
   2140  1.4.2.1    lukem 	iha_set_ssig(sc, REQ | BSY | SEL, ATN);
   2141      1.1  tsutsui 
   2142  1.4.2.1    lukem 	if (iha_wait(sc, MSG_ACCEPT) == PHASE_MSG_OUT)
   2143  1.4.2.1    lukem 		return (iha_msgout(sc, MSG_MESSAGE_REJECT));
   2144      1.1  tsutsui 
   2145  1.4.2.1    lukem 	return (-1);
   2146      1.1  tsutsui }
   2147      1.1  tsutsui 
   2148      1.1  tsutsui static int
   2149  1.4.2.1    lukem iha_msgout_extended(sc)
   2150      1.1  tsutsui 	struct iha_softc *sc;
   2151      1.1  tsutsui {
   2152      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2153      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2154      1.1  tsutsui 	int phase;
   2155      1.1  tsutsui 
   2156      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXTENDED);
   2157      1.1  tsutsui 
   2158  1.4.2.1    lukem 	bus_space_write_multi_1(iot, ioh, TUL_SFIFO,
   2159  1.4.2.1    lukem 	    sc->sc_msg, sc->sc_msg[0] + 1);
   2160  1.4.2.1    lukem 
   2161  1.4.2.1    lukem 	phase = iha_wait(sc, XF_FIFO_OUT);
   2162      1.1  tsutsui 
   2163      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   2164  1.4.2.1    lukem 	iha_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
   2165      1.1  tsutsui 
   2166      1.1  tsutsui 	return (phase);
   2167      1.1  tsutsui }
   2168      1.1  tsutsui 
   2169      1.1  tsutsui static int
   2170  1.4.2.1    lukem iha_msgout_wdtr(sc)
   2171      1.1  tsutsui 	struct iha_softc *sc;
   2172      1.1  tsutsui {
   2173      1.1  tsutsui 
   2174  1.4.2.1    lukem 	sc->sc_actscb->tcs->flags |= FLAG_WIDE_DONE;
   2175      1.1  tsutsui 
   2176  1.4.2.1    lukem 	sc->sc_msg[0] = MSG_EXT_WDTR_LEN;
   2177  1.4.2.1    lukem 	sc->sc_msg[1] = MSG_EXT_WDTR;
   2178  1.4.2.1    lukem 	sc->sc_msg[2] = MSG_EXT_WDTR_BUS_16_BIT;
   2179      1.1  tsutsui 
   2180  1.4.2.1    lukem 	return (iha_msgout_extended(sc));
   2181  1.4.2.1    lukem }
   2182  1.4.2.1    lukem 
   2183  1.4.2.1    lukem static int
   2184  1.4.2.1    lukem iha_msgout_sdtr(sc)
   2185  1.4.2.1    lukem 	struct iha_softc *sc;
   2186  1.4.2.1    lukem {
   2187  1.4.2.1    lukem 	int rateindex;
   2188      1.1  tsutsui 
   2189  1.4.2.1    lukem 	rateindex = sc->sc_actscb->tcs->flags & FLAG_SCSI_RATE;
   2190      1.1  tsutsui 
   2191  1.4.2.1    lukem 	sc->sc_msg[0] = MSG_EXT_SDTR_LEN;
   2192  1.4.2.1    lukem 	sc->sc_msg[1] = MSG_EXT_SDTR;
   2193  1.4.2.1    lukem 	sc->sc_msg[2] = iha_rate_tbl[rateindex];
   2194  1.4.2.1    lukem 	sc->sc_msg[3] = IHA_MAX_OFFSET; /* REQ/ACK */
   2195      1.1  tsutsui 
   2196  1.4.2.1    lukem 	return (iha_msgout_extended(sc));
   2197      1.1  tsutsui }
   2198      1.1  tsutsui 
   2199      1.1  tsutsui static void
   2200  1.4.2.1    lukem iha_wide_done(sc)
   2201      1.1  tsutsui 	struct iha_softc *sc;
   2202      1.1  tsutsui {
   2203      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2204      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2205      1.1  tsutsui 	struct tcs *tcs = sc->sc_actscb->tcs;
   2206      1.1  tsutsui 
   2207      1.1  tsutsui 	tcs->syncm = 0;
   2208      1.1  tsutsui 	tcs->period = 0;
   2209      1.1  tsutsui 	tcs->offset = 0;
   2210      1.1  tsutsui 
   2211      1.1  tsutsui 	if (sc->sc_msg[2] != 0)
   2212      1.1  tsutsui 		tcs->syncm |= PERIOD_WIDE_SCSI;
   2213      1.1  tsutsui 
   2214      1.1  tsutsui 	tcs->sconfig0 &= ~ALTPD;
   2215      1.1  tsutsui 	tcs->flags &= ~FLAG_SYNC_DONE;
   2216      1.1  tsutsui 	tcs->flags |=  FLAG_WIDE_DONE;
   2217      1.1  tsutsui 
   2218      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
   2219      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
   2220      1.1  tsutsui }
   2221      1.1  tsutsui 
   2222      1.1  tsutsui static void
   2223  1.4.2.1    lukem iha_sync_done(sc)
   2224      1.1  tsutsui 	struct iha_softc *sc;
   2225      1.1  tsutsui {
   2226      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2227      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2228      1.1  tsutsui 	struct tcs *tcs = sc->sc_actscb->tcs;
   2229      1.1  tsutsui 	int i;
   2230      1.1  tsutsui 
   2231      1.1  tsutsui 	if ((tcs->flags & FLAG_SYNC_DONE) == 0) {
   2232      1.1  tsutsui 		tcs->period = sc->sc_msg[2];
   2233      1.1  tsutsui 		tcs->offset = sc->sc_msg[3];
   2234      1.1  tsutsui 		if (tcs->offset != 0) {
   2235      1.1  tsutsui 			tcs->syncm |= tcs->offset;
   2236      1.1  tsutsui 
   2237      1.1  tsutsui 			/* pick the highest possible rate */
   2238      1.1  tsutsui 			for (i = 0; i < 8; i++)
   2239  1.4.2.1    lukem 				if (iha_rate_tbl[i] >= tcs->period)
   2240      1.1  tsutsui 					break;
   2241      1.1  tsutsui 
   2242      1.1  tsutsui 			tcs->syncm |= (i << 4);
   2243      1.1  tsutsui 			tcs->sconfig0 |= ALTPD;
   2244      1.1  tsutsui 		}
   2245      1.1  tsutsui 
   2246      1.1  tsutsui 		tcs->flags |= FLAG_SYNC_DONE;
   2247      1.1  tsutsui 
   2248      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
   2249      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
   2250      1.1  tsutsui 	}
   2251      1.1  tsutsui }
   2252      1.1  tsutsui 
   2253      1.1  tsutsui void
   2254  1.4.2.1    lukem iha_reset_chip(sc)
   2255      1.1  tsutsui 	struct iha_softc *sc;
   2256      1.1  tsutsui {
   2257      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2258      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2259      1.1  tsutsui 
   2260      1.1  tsutsui 	/* reset tulip chip */
   2261      1.1  tsutsui 
   2262      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SCTRL0, RSCSI);
   2263      1.1  tsutsui 
   2264      1.1  tsutsui 	do {
   2265      1.1  tsutsui 		sc->sc_sistat = bus_space_read_1(iot, ioh, TUL_SISTAT);
   2266      1.1  tsutsui 	} while ((sc->sc_sistat & SRSTD) == 0);
   2267      1.1  tsutsui 
   2268  1.4.2.1    lukem 	iha_set_ssig(sc, 0, 0);
   2269      1.1  tsutsui 
   2270      1.1  tsutsui 	bus_space_read_1(iot, ioh, TUL_SISTAT); /* Clear any active interrupt*/
   2271      1.1  tsutsui }
   2272      1.1  tsutsui 
   2273      1.1  tsutsui static void
   2274  1.4.2.1    lukem iha_select(sc, scb, select_type)
   2275      1.1  tsutsui 	struct iha_softc *sc;
   2276      1.1  tsutsui 	struct iha_scsi_req_q *scb;
   2277      1.1  tsutsui 	u_int8_t select_type;
   2278      1.1  tsutsui {
   2279      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2280      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2281      1.1  tsutsui 
   2282      1.1  tsutsui 	switch (select_type) {
   2283      1.1  tsutsui 	case SEL_ATN:
   2284      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_id);
   2285      1.1  tsutsui 		bus_space_write_multi_1(iot, ioh, TUL_SFIFO,
   2286      1.1  tsutsui 		    scb->cmd, scb->cmdlen);
   2287      1.1  tsutsui 
   2288      1.1  tsutsui 		scb->nextstat = 2;
   2289      1.1  tsutsui 		break;
   2290      1.1  tsutsui 
   2291      1.1  tsutsui 	case SELATNSTOP:
   2292      1.1  tsutsui 		scb->nextstat = 1;
   2293      1.1  tsutsui 		break;
   2294      1.1  tsutsui 
   2295      1.1  tsutsui 	case SEL_ATN3:
   2296      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_id);
   2297      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_tagmsg);
   2298      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_tagid);
   2299      1.1  tsutsui 
   2300      1.1  tsutsui 		bus_space_write_multi_1(iot, ioh, TUL_SFIFO, scb->cmd,
   2301      1.1  tsutsui 		    scb->cmdlen);
   2302      1.1  tsutsui 
   2303      1.1  tsutsui 		scb->nextstat = 2;
   2304      1.1  tsutsui 		break;
   2305      1.1  tsutsui 
   2306      1.1  tsutsui 	default:
   2307  1.4.2.1    lukem 		printf("[debug] iha_select() - unknown select type = 0x%02x\n",
   2308      1.1  tsutsui 		    select_type);
   2309      1.1  tsutsui 		return;
   2310      1.1  tsutsui 	}
   2311      1.1  tsutsui 
   2312  1.4.2.1    lukem 	iha_del_pend_scb(sc, scb);
   2313      1.1  tsutsui 	scb->status = STATUS_SELECT;
   2314      1.1  tsutsui 
   2315      1.1  tsutsui 	sc->sc_actscb = scb;
   2316      1.1  tsutsui 
   2317      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SCMD, select_type);
   2318      1.1  tsutsui }
   2319      1.1  tsutsui 
   2320      1.1  tsutsui /*
   2321  1.4.2.1    lukem  * iha_wait - wait for an interrupt to service or a SCSI bus phase change
   2322      1.1  tsutsui  *            after writing the supplied command to the tulip chip. If
   2323      1.1  tsutsui  *            the command is NO_OP, skip the command writing.
   2324      1.1  tsutsui  */
   2325      1.1  tsutsui static int
   2326  1.4.2.1    lukem iha_wait(sc, cmd)
   2327      1.1  tsutsui 	struct iha_softc *sc;
   2328      1.1  tsutsui 	u_int8_t cmd;
   2329      1.1  tsutsui {
   2330      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2331      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2332      1.1  tsutsui 
   2333      1.1  tsutsui 	if (cmd != NO_OP)
   2334      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_SCMD, cmd);
   2335      1.1  tsutsui 
   2336      1.1  tsutsui 	/*
   2337      1.1  tsutsui 	 * Have to do this here, in addition to in iha_isr, because
   2338      1.1  tsutsui 	 * interrupts might be turned off when we get here.
   2339      1.1  tsutsui 	 */
   2340      1.1  tsutsui 	do {
   2341      1.1  tsutsui 		sc->sc_status0 = bus_space_read_1(iot, ioh, TUL_STAT0);
   2342      1.1  tsutsui 	} while ((sc->sc_status0 & INTPD) == 0);
   2343      1.1  tsutsui 
   2344      1.1  tsutsui 	sc->sc_status1 = bus_space_read_1(iot, ioh, TUL_STAT1);
   2345      1.1  tsutsui 	sc->sc_sistat = bus_space_read_1(iot, ioh, TUL_SISTAT);
   2346      1.1  tsutsui 
   2347      1.1  tsutsui 	sc->sc_phase = sc->sc_status0 & PH_MASK;
   2348      1.1  tsutsui 
   2349      1.1  tsutsui 	if ((sc->sc_sistat & SRSTD) != 0) {
   2350      1.1  tsutsui 		/* SCSI bus reset interrupt */
   2351  1.4.2.1    lukem 		iha_reset_scsi_bus(sc);
   2352      1.1  tsutsui 		return (-1);
   2353      1.1  tsutsui 	}
   2354      1.1  tsutsui 
   2355      1.1  tsutsui 	if ((sc->sc_sistat & RSELED) != 0)
   2356      1.1  tsutsui 		/* Reselection interrupt */
   2357  1.4.2.1    lukem 		return (iha_resel(sc));
   2358      1.1  tsutsui 
   2359      1.1  tsutsui 	if ((sc->sc_sistat & STIMEO) != 0) {
   2360      1.1  tsutsui 		/* selected/reselected timeout interrupt */
   2361  1.4.2.1    lukem 		iha_busfree(sc);
   2362      1.1  tsutsui 		return (-1);
   2363      1.1  tsutsui 	}
   2364      1.1  tsutsui 
   2365      1.1  tsutsui 	if ((sc->sc_sistat & DISCD) != 0) {
   2366      1.1  tsutsui 		/* BUS disconnection interrupt */
   2367      1.1  tsutsui 		if ((sc->sc_flags & FLAG_EXPECT_DONE_DISC) != 0) {
   2368      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   2369      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_SCONFIG0,
   2370      1.1  tsutsui 			    SCONFIG0DEFAULT);
   2371      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
   2372  1.4.2.1    lukem 			iha_append_done_scb(sc, sc->sc_actscb, HOST_OK);
   2373      1.1  tsutsui 			sc->sc_flags &= ~FLAG_EXPECT_DONE_DISC;
   2374      1.1  tsutsui 
   2375      1.1  tsutsui 		} else if ((sc->sc_flags & FLAG_EXPECT_DISC) != 0) {
   2376      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   2377      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_SCONFIG0,
   2378      1.1  tsutsui 			    SCONFIG0DEFAULT);
   2379      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
   2380      1.1  tsutsui 			sc->sc_actscb = NULL;
   2381      1.1  tsutsui 			sc->sc_flags &= ~FLAG_EXPECT_DISC;
   2382      1.1  tsutsui 
   2383      1.1  tsutsui 		} else
   2384  1.4.2.1    lukem 			iha_busfree(sc);
   2385      1.1  tsutsui 
   2386      1.1  tsutsui 		return (-1);
   2387      1.1  tsutsui 	}
   2388      1.1  tsutsui 
   2389      1.1  tsutsui 	return (sc->sc_phase);
   2390      1.1  tsutsui }
   2391      1.1  tsutsui 
   2392      1.1  tsutsui /*
   2393  1.4.2.1    lukem  * iha_done_scb - We have a scb which has been processed by the
   2394      1.1  tsutsui  *                adaptor, now we look to see how the operation went.
   2395      1.1  tsutsui  */
   2396      1.1  tsutsui static void
   2397  1.4.2.1    lukem iha_done_scb(sc, scb)
   2398      1.1  tsutsui 	struct iha_softc *sc;
   2399      1.1  tsutsui 	struct iha_scsi_req_q *scb;
   2400      1.1  tsutsui {
   2401      1.1  tsutsui 	struct scsipi_xfer *xs = scb->xs;
   2402      1.1  tsutsui 
   2403      1.1  tsutsui 	if (xs != NULL) {
   2404      1.3  thorpej 		/* Cancel the timeout. */
   2405      1.3  thorpej 		callout_stop(&xs->xs_callout);
   2406      1.3  thorpej 
   2407      1.1  tsutsui 		if (xs->datalen > 0) {
   2408      1.1  tsutsui 			bus_dmamap_sync(sc->sc_dmat, scb->dmap,
   2409      1.1  tsutsui 			    0, scb->dmap->dm_mapsize,
   2410      1.1  tsutsui 			    (xs->xs_control & XS_CTL_DATA_IN) ?
   2411      1.1  tsutsui 			    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   2412      1.1  tsutsui 			bus_dmamap_unload(sc->sc_dmat, scb->dmap);
   2413      1.1  tsutsui 		}
   2414      1.1  tsutsui 
   2415      1.1  tsutsui 		xs->status = scb->ta_stat;
   2416      1.1  tsutsui 
   2417      1.1  tsutsui 		switch (scb->ha_stat) {
   2418      1.1  tsutsui 		case HOST_OK:
   2419      1.1  tsutsui 			switch (scb->ta_stat) {
   2420      1.1  tsutsui 			case SCSI_OK:
   2421      1.1  tsutsui 			case SCSI_CONDITION_MET:
   2422      1.1  tsutsui 			case SCSI_INTERM:
   2423      1.1  tsutsui 			case SCSI_INTERM_COND_MET:
   2424      1.1  tsutsui 				xs->resid = scb->buflen;
   2425      1.1  tsutsui 				xs->error = XS_NOERROR;
   2426      1.1  tsutsui 				if ((scb->flags & FLAG_RSENS) != 0)
   2427      1.1  tsutsui 					xs->error = XS_SENSE;
   2428      1.1  tsutsui 				break;
   2429      1.1  tsutsui 
   2430      1.1  tsutsui 			case SCSI_RESV_CONFLICT:
   2431      1.1  tsutsui 			case SCSI_BUSY:
   2432      1.1  tsutsui 			case SCSI_QUEUE_FULL:
   2433      1.1  tsutsui 				xs->error = XS_BUSY;
   2434      1.1  tsutsui 				break;
   2435      1.1  tsutsui 
   2436      1.1  tsutsui 			case SCSI_TERMINATED:
   2437      1.1  tsutsui 			case SCSI_ACA_ACTIVE:
   2438      1.1  tsutsui 			case SCSI_CHECK:
   2439      1.1  tsutsui 				scb->tcs->flags &=
   2440      1.1  tsutsui 				    ~(FLAG_SYNC_DONE | FLAG_WIDE_DONE);
   2441      1.1  tsutsui 
   2442      1.1  tsutsui 				if ((scb->flags & FLAG_RSENS) != 0 ||
   2443  1.4.2.1    lukem 				    iha_push_sense_request(sc, scb) != 0) {
   2444      1.1  tsutsui 					scb->flags &= FLAG_RSENS;
   2445      1.1  tsutsui 					printf("%s: request sense failed\n",
   2446      1.1  tsutsui 					    sc->sc_dev.dv_xname);
   2447      1.1  tsutsui 					xs->error = XS_DRIVER_STUFFUP;
   2448      1.1  tsutsui 					break;
   2449      1.1  tsutsui 				}
   2450      1.1  tsutsui 
   2451      1.1  tsutsui 				xs->error = XS_SENSE;
   2452      1.1  tsutsui 				return;
   2453      1.1  tsutsui 
   2454      1.1  tsutsui 			default:
   2455      1.1  tsutsui 				xs->error = XS_DRIVER_STUFFUP;
   2456      1.1  tsutsui 				break;
   2457      1.1  tsutsui 			}
   2458      1.1  tsutsui 			break;
   2459      1.1  tsutsui 
   2460      1.1  tsutsui 		case HOST_SEL_TOUT:
   2461      1.1  tsutsui 			xs->error = XS_SELTIMEOUT;
   2462      1.1  tsutsui 			break;
   2463      1.1  tsutsui 
   2464      1.1  tsutsui 		case HOST_SCSI_RST:
   2465      1.1  tsutsui 		case HOST_DEV_RST:
   2466      1.1  tsutsui 			xs->error = XS_RESET;
   2467      1.1  tsutsui 			break;
   2468      1.1  tsutsui 
   2469      1.1  tsutsui 		case HOST_SPERR:
   2470      1.1  tsutsui 			printf("%s: SCSI Parity error detected\n",
   2471      1.1  tsutsui 			    sc->sc_dev.dv_xname);
   2472      1.1  tsutsui 			xs->error = XS_DRIVER_STUFFUP;
   2473      1.1  tsutsui 			break;
   2474      1.1  tsutsui 
   2475      1.1  tsutsui 		case HOST_TIMED_OUT:
   2476      1.1  tsutsui 			xs->error = XS_TIMEOUT;
   2477      1.1  tsutsui 			break;
   2478      1.1  tsutsui 
   2479      1.1  tsutsui 		case HOST_DO_DU:
   2480      1.1  tsutsui 		case HOST_BAD_PHAS:
   2481      1.1  tsutsui 		default:
   2482      1.1  tsutsui 			xs->error = XS_DRIVER_STUFFUP;
   2483      1.1  tsutsui 			break;
   2484      1.1  tsutsui 		}
   2485      1.1  tsutsui 
   2486      1.1  tsutsui 		scsipi_done(xs);
   2487      1.1  tsutsui 	}
   2488      1.1  tsutsui 
   2489  1.4.2.1    lukem 	iha_append_free_scb(sc, scb);
   2490      1.1  tsutsui }
   2491      1.1  tsutsui 
   2492      1.1  tsutsui static void
   2493  1.4.2.1    lukem iha_timeout(arg)
   2494      1.1  tsutsui 	void *arg;
   2495      1.1  tsutsui {
   2496      1.1  tsutsui 	struct iha_scsi_req_q *scb = (struct iha_scsi_req_q *)arg;
   2497      1.1  tsutsui 	struct scsipi_xfer *xs = scb->xs;
   2498      1.1  tsutsui 	struct scsipi_periph *periph = xs->xs_periph;
   2499      1.1  tsutsui 	struct iha_softc *sc;
   2500      1.1  tsutsui 
   2501      1.1  tsutsui 	sc = (void *)periph->periph_channel->chan_adapter->adapt_dev;
   2502      1.1  tsutsui 
   2503      1.1  tsutsui 	if (xs == NULL)
   2504  1.4.2.1    lukem 		printf("[debug] iha_timeout called with xs == NULL\n");
   2505      1.1  tsutsui 
   2506      1.1  tsutsui 	else {
   2507      1.1  tsutsui 		scsipi_printaddr(periph);
   2508      1.1  tsutsui 		printf("SCSI OpCode 0x%02x timed out\n", xs->cmd->opcode);
   2509      1.1  tsutsui 
   2510  1.4.2.1    lukem 		iha_abort_xs(sc, xs, HOST_TIMED_OUT);
   2511      1.1  tsutsui 	}
   2512      1.1  tsutsui }
   2513      1.1  tsutsui 
   2514      1.1  tsutsui static void
   2515  1.4.2.1    lukem iha_exec_scb(sc, scb)
   2516      1.1  tsutsui 	struct iha_softc *sc;
   2517      1.1  tsutsui 	struct iha_scsi_req_q *scb;
   2518      1.1  tsutsui {
   2519      1.1  tsutsui 	bus_space_tag_t iot;
   2520      1.1  tsutsui 	bus_space_handle_t ioh;
   2521      1.1  tsutsui 	bus_dmamap_t dm;
   2522      1.1  tsutsui 	struct scsipi_xfer *xs = scb->xs;
   2523      1.1  tsutsui 	int nseg, s;
   2524      1.1  tsutsui 
   2525      1.1  tsutsui 	dm = scb->dmap;
   2526      1.1  tsutsui 	nseg = dm->dm_nsegs;
   2527      1.1  tsutsui 
   2528      1.1  tsutsui 	if (nseg > 1) {
   2529      1.1  tsutsui 		struct iha_sg_element *sg = scb->sglist;
   2530      1.1  tsutsui 		int i;
   2531      1.1  tsutsui 
   2532      1.1  tsutsui 		for (i = 0; i < nseg; i++) {
   2533      1.1  tsutsui 			sg[i].sg_len = htole32(dm->dm_segs[i].ds_len);
   2534      1.1  tsutsui 			sg[i].sg_addr = htole32(dm->dm_segs[i].ds_addr);
   2535      1.1  tsutsui 		}
   2536      1.1  tsutsui 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   2537      1.1  tsutsui 		    scb->sgoffset, IHA_SG_SIZE,
   2538      1.1  tsutsui 		    BUS_DMASYNC_PREWRITE);
   2539      1.1  tsutsui 
   2540      1.1  tsutsui 		scb->flags |= FLAG_SG; /* XXX */
   2541      1.1  tsutsui 		scb->sg_size = scb->sg_max = nseg;
   2542      1.1  tsutsui 
   2543      1.1  tsutsui 		scb->bufaddr = scb->sg_addr;
   2544      1.1  tsutsui 	} else
   2545      1.1  tsutsui 		scb->bufaddr = dm->dm_segs[0].ds_addr;
   2546      1.1  tsutsui 
   2547      1.1  tsutsui 	if ((xs->xs_control & XS_CTL_POLL) == 0) {
   2548      1.1  tsutsui 		int timeout = xs->timeout;
   2549      1.1  tsutsui 		timeout = (timeout > 100000) ?
   2550      1.1  tsutsui 		    timeout / 1000 * hz : timeout * hz / 1000;
   2551      1.1  tsutsui 		if (timeout == 0)
   2552      1.1  tsutsui 			timeout = 1;
   2553  1.4.2.1    lukem 		callout_reset(&xs->xs_callout, timeout, iha_timeout, scb);
   2554      1.1  tsutsui 	}
   2555      1.1  tsutsui 
   2556      1.1  tsutsui 	s = splbio();
   2557      1.1  tsutsui 
   2558      1.1  tsutsui 	if (((scb->flags & XS_RESET) != 0) || (scb->cmd[0] == REQUEST_SENSE))
   2559  1.4.2.1    lukem 		iha_push_pend_scb(sc, scb);   /* Insert SCB at head of Pend */
   2560      1.1  tsutsui 	else
   2561  1.4.2.1    lukem 		iha_append_pend_scb(sc, scb); /* Append SCB to tail of Pend */
   2562      1.1  tsutsui 
   2563      1.1  tsutsui 	/*
   2564  1.4.2.1    lukem 	 * Run through iha_main() to ensure something is active, if
   2565      1.1  tsutsui 	 * only this new SCB.
   2566      1.1  tsutsui 	 */
   2567      1.1  tsutsui 	if (sc->sc_semaph != SEMAPH_IN_MAIN) {
   2568      1.1  tsutsui 		iot = sc->sc_iot;
   2569      1.1  tsutsui 		ioh = sc->sc_ioh;
   2570      1.1  tsutsui 
   2571      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_IMSK, MASK_ALL);
   2572      1.1  tsutsui 		sc->sc_semaph = SEMAPH_IN_MAIN;;
   2573      1.1  tsutsui 
   2574      1.1  tsutsui 		splx(s);
   2575  1.4.2.1    lukem 		iha_main(sc);
   2576      1.1  tsutsui 		s = splbio();
   2577      1.1  tsutsui 
   2578      1.1  tsutsui 		sc->sc_semaph = ~SEMAPH_IN_MAIN;;
   2579      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_IMSK, (MASK_ALL & ~MSCMP));
   2580      1.1  tsutsui 	}
   2581      1.1  tsutsui 
   2582      1.1  tsutsui 	splx(s);
   2583      1.1  tsutsui }
   2584      1.1  tsutsui 
   2585      1.1  tsutsui 
   2586      1.1  tsutsui /*
   2587  1.4.2.1    lukem  * iha_set_ssig - read the current scsi signal mask, then write a new
   2588      1.1  tsutsui  *		  one which turns off/on the specified signals.
   2589      1.1  tsutsui  */
   2590      1.1  tsutsui static void
   2591  1.4.2.1    lukem iha_set_ssig(sc, offsigs, onsigs)
   2592      1.1  tsutsui 	struct iha_softc *sc;
   2593      1.1  tsutsui 	u_int8_t offsigs, onsigs;
   2594      1.1  tsutsui {
   2595      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2596      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2597      1.1  tsutsui 	u_int8_t currsigs;
   2598      1.1  tsutsui 
   2599      1.1  tsutsui 	currsigs = bus_space_read_1(iot, ioh, TUL_SSIGI);
   2600      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_SSIGO, (currsigs & ~offsigs) | onsigs);
   2601      1.1  tsutsui }
   2602      1.1  tsutsui 
   2603      1.1  tsutsui /*
   2604  1.4.2.1    lukem  * iha_alloc_sglist - allocate and map sglist for SCB's
   2605      1.1  tsutsui  */
   2606      1.1  tsutsui static int
   2607  1.4.2.1    lukem iha_alloc_sglist(sc)
   2608      1.1  tsutsui 	struct iha_softc *sc;
   2609      1.1  tsutsui {
   2610      1.1  tsutsui 	bus_dma_segment_t seg;
   2611      1.1  tsutsui 	int error, rseg;
   2612      1.1  tsutsui 
   2613      1.1  tsutsui 	/*
   2614      1.1  tsutsui 	 * Allocate dma-safe memory for the SCB's sglist
   2615      1.1  tsutsui 	 */
   2616      1.1  tsutsui 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
   2617      1.1  tsutsui 	    IHA_SG_SIZE * IHA_MAX_SCB,
   2618      1.1  tsutsui 	    PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
   2619      1.1  tsutsui 		printf(": unable to allocate sglist, error = %d\n", error);
   2620      1.1  tsutsui 		return (error);
   2621      1.1  tsutsui 	}
   2622      1.1  tsutsui 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
   2623      1.1  tsutsui 	    IHA_SG_SIZE * IHA_MAX_SCB, (caddr_t *)&sc->sc_sglist,
   2624      1.1  tsutsui 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
   2625      1.1  tsutsui 		printf(": unable to map sglist, error = %d\n", error);
   2626      1.1  tsutsui 		return (error);
   2627      1.1  tsutsui 	}
   2628      1.1  tsutsui 
   2629      1.1  tsutsui 	/*
   2630      1.1  tsutsui 	 * Create and load the DMA map used for the SCBs
   2631      1.1  tsutsui 	 */
   2632      1.1  tsutsui 	if ((error = bus_dmamap_create(sc->sc_dmat,
   2633      1.1  tsutsui 	    IHA_SG_SIZE * IHA_MAX_SCB, 1, IHA_SG_SIZE * IHA_MAX_SCB,
   2634      1.1  tsutsui 	    0, BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
   2635      1.1  tsutsui 		printf(": unable to create control DMA map, error = %d\n",
   2636      1.1  tsutsui 		    error);
   2637      1.1  tsutsui 		return (error);
   2638      1.1  tsutsui 	}
   2639      1.1  tsutsui 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
   2640      1.1  tsutsui 	    sc->sc_sglist, IHA_SG_SIZE * IHA_MAX_SCB,
   2641      1.1  tsutsui 	    NULL, BUS_DMA_NOWAIT)) != 0) {
   2642      1.1  tsutsui 		printf(": unable to load control DMA map, error = %d\n", error);
   2643      1.1  tsutsui 		return (error);
   2644      1.1  tsutsui 	}
   2645      1.1  tsutsui 
   2646      1.4  thorpej 	memset(sc->sc_sglist, 0, IHA_SG_SIZE * IHA_MAX_SCB);
   2647      1.1  tsutsui 
   2648      1.1  tsutsui 	return (0);
   2649      1.1  tsutsui }
   2650      1.1  tsutsui 
   2651      1.1  tsutsui /*
   2652  1.4.2.1    lukem  * iha_read_eeprom - read Serial EEPROM value & set to defaults
   2653      1.1  tsutsui  *		     if required. XXX - Writing does NOT work!
   2654      1.1  tsutsui  */
   2655      1.1  tsutsui void
   2656  1.4.2.1    lukem iha_read_eeprom(sc, eeprom)
   2657      1.1  tsutsui 	struct iha_softc *sc;
   2658      1.1  tsutsui 	struct iha_eeprom *eeprom;
   2659      1.1  tsutsui {
   2660      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2661      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2662      1.1  tsutsui 	u_int16_t *buf = (u_int16_t *)eeprom;
   2663      1.1  tsutsui 	u_int8_t gctrl;
   2664      1.1  tsutsui 
   2665  1.4.2.1    lukem 	/* Enable EEProm programming */
   2666      1.1  tsutsui 	gctrl = bus_space_read_1(iot, ioh, TUL_GCTRL0) | EEPRG;
   2667      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_GCTRL0, gctrl);
   2668      1.1  tsutsui 
   2669  1.4.2.1    lukem 	/* Read EEProm */
   2670  1.4.2.1    lukem 	if (iha_se2_rd_all(sc, buf) == 0)
   2671  1.4.2.1    lukem 		panic("%s: cannot read EEPROM\n", sc->sc_dev.dv_xname);
   2672      1.1  tsutsui 
   2673  1.4.2.1    lukem 	/* Disable EEProm programming */
   2674      1.1  tsutsui 	gctrl = bus_space_read_1(iot, ioh, TUL_GCTRL0) & ~EEPRG;
   2675      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_GCTRL0, gctrl);
   2676      1.1  tsutsui }
   2677      1.1  tsutsui 
   2678  1.4.2.1    lukem #ifdef notused
   2679      1.1  tsutsui /*
   2680  1.4.2.1    lukem  * iha_se2_update_all - Update SCSI H/A configuration parameters from
   2681      1.1  tsutsui  *			serial EEPROM Setup default pattern. Only
   2682      1.1  tsutsui  *			change those values different from the values
   2683  1.4.2.1    lukem  *			in iha_eeprom.
   2684      1.1  tsutsui  */
   2685      1.1  tsutsui void
   2686  1.4.2.1    lukem iha_se2_update_all(sc)
   2687      1.1  tsutsui 	struct iha_softc *sc;
   2688      1.1  tsutsui {
   2689      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2690      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2691      1.1  tsutsui 	u_int16_t *np;
   2692      1.1  tsutsui 	u_int32_t chksum;
   2693      1.1  tsutsui 	int i;
   2694      1.1  tsutsui 
   2695      1.1  tsutsui 	/* Enable erase/write state of EEPROM */
   2696  1.4.2.1    lukem 	iha_se2_instr(sc, ENABLE_ERASE);
   2697      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
   2698      1.1  tsutsui 	EEP_WAIT();
   2699      1.1  tsutsui 
   2700      1.1  tsutsui 	np = (u_int16_t *)&eeprom_default;
   2701      1.1  tsutsui 
   2702      1.1  tsutsui 	for (i = 0, chksum = 0; i < EEPROM_SIZE - 1; i++) {
   2703  1.4.2.1    lukem 		iha_se2_wr(sc, i, *np);
   2704      1.1  tsutsui 		chksum += *np++;
   2705      1.1  tsutsui 	}
   2706      1.1  tsutsui 
   2707      1.1  tsutsui 	chksum &= 0x0000ffff;
   2708  1.4.2.1    lukem 	iha_se2_wr(sc, 31, chksum);
   2709      1.1  tsutsui 
   2710      1.1  tsutsui 	/* Disable erase/write state of EEPROM */
   2711  1.4.2.1    lukem 	iha_se2_instr(sc, 0);
   2712      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
   2713      1.1  tsutsui 	EEP_WAIT();
   2714      1.1  tsutsui }
   2715      1.1  tsutsui 
   2716      1.1  tsutsui /*
   2717  1.4.2.1    lukem  * iha_se2_wr - write the given 16 bit value into the Serial EEPROM
   2718      1.1  tsutsui  *		at the specified offset
   2719      1.1  tsutsui  */
   2720      1.1  tsutsui void
   2721  1.4.2.1    lukem iha_se2_wr(sc, addr, writeword)
   2722      1.1  tsutsui 	struct iha_softc *sc;
   2723      1.1  tsutsui 	int addr;
   2724      1.1  tsutsui 	u_int16_t writeword;
   2725      1.1  tsutsui {
   2726      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2727      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2728      1.1  tsutsui 	int i, bit;
   2729      1.1  tsutsui 
   2730      1.1  tsutsui 	/* send 'WRITE' Instruction == address | WRITE bit */
   2731  1.4.2.1    lukem 	iha_se2_instr(sc, addr | WRITE);
   2732      1.1  tsutsui 
   2733      1.1  tsutsui 	for (i = 16; i > 0; i--) {
   2734      1.1  tsutsui 		if (writeword & (1 << (i - 1)))
   2735      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRDO);
   2736      1.1  tsutsui 		else
   2737      1.1  tsutsui 			bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
   2738      1.1  tsutsui 		EEP_WAIT();
   2739      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRCK);
   2740      1.1  tsutsui 		EEP_WAIT();
   2741      1.1  tsutsui 	}
   2742      1.1  tsutsui 
   2743      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
   2744      1.1  tsutsui 	EEP_WAIT();
   2745      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
   2746      1.1  tsutsui 	EEP_WAIT();
   2747      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
   2748      1.1  tsutsui 	EEP_WAIT();
   2749      1.1  tsutsui 
   2750      1.1  tsutsui 	for (;;) {
   2751      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRCK);
   2752      1.1  tsutsui 		EEP_WAIT();
   2753      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
   2754      1.1  tsutsui 		EEP_WAIT();
   2755      1.1  tsutsui 		bit = bus_space_read_1(iot, ioh, TUL_NVRAM) & NVRDI;
   2756      1.1  tsutsui 		EEP_WAIT();
   2757      1.1  tsutsui 		if (bit != 0)
   2758      1.1  tsutsui 			break; /* write complete */
   2759      1.1  tsutsui 	}
   2760      1.1  tsutsui 
   2761      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
   2762      1.1  tsutsui }
   2763  1.4.2.1    lukem #endif
   2764      1.1  tsutsui 
   2765      1.1  tsutsui /*
   2766  1.4.2.1    lukem  * iha_se2_rd - read & return the 16 bit value at the specified
   2767      1.1  tsutsui  *		offset in the Serial E2PROM
   2768      1.1  tsutsui  *
   2769      1.1  tsutsui  */
   2770      1.1  tsutsui u_int16_t
   2771  1.4.2.1    lukem iha_se2_rd(sc, addr)
   2772      1.1  tsutsui 	struct iha_softc *sc;
   2773      1.1  tsutsui 	int addr;
   2774      1.1  tsutsui {
   2775      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2776      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2777      1.1  tsutsui 	int i, bit;
   2778      1.1  tsutsui 	u_int16_t readword;
   2779      1.1  tsutsui 
   2780      1.1  tsutsui 	/* Send 'READ' instruction == address | READ bit */
   2781  1.4.2.1    lukem 	iha_se2_instr(sc, addr | READ);
   2782      1.1  tsutsui 
   2783      1.1  tsutsui 	readword = 0;
   2784      1.1  tsutsui 	for (i = 16; i > 0; i--) {
   2785      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRCK);
   2786      1.1  tsutsui 		EEP_WAIT();
   2787      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
   2788      1.1  tsutsui 		EEP_WAIT();
   2789      1.1  tsutsui 		/* sample data after the following edge of clock     */
   2790      1.1  tsutsui 		bit = bus_space_read_1(iot, ioh, TUL_NVRAM) & NVRDI ? 1 : 0;
   2791      1.1  tsutsui 		EEP_WAIT();
   2792      1.1  tsutsui 
   2793      1.1  tsutsui 		readword |= bit << (i - 1);
   2794      1.1  tsutsui 	}
   2795      1.1  tsutsui 
   2796      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
   2797      1.1  tsutsui 
   2798      1.1  tsutsui 	return (readword);
   2799      1.1  tsutsui }
   2800      1.1  tsutsui 
   2801      1.1  tsutsui /*
   2802  1.4.2.1    lukem  * iha_se2_rd_all - Read SCSI H/A config parameters from serial EEPROM
   2803      1.1  tsutsui  */
   2804      1.1  tsutsui int
   2805  1.4.2.1    lukem iha_se2_rd_all(sc, buf)
   2806      1.1  tsutsui 	struct iha_softc *sc;
   2807      1.1  tsutsui 	u_int16_t *buf;
   2808      1.1  tsutsui {
   2809      1.1  tsutsui 	struct iha_eeprom *eeprom = (struct iha_eeprom *)buf;
   2810      1.1  tsutsui 	u_int32_t chksum;
   2811      1.1  tsutsui 	int i;
   2812      1.1  tsutsui 
   2813      1.1  tsutsui 	for (i = 0, chksum = 0; i < EEPROM_SIZE - 1; i++) {
   2814  1.4.2.1    lukem 		*buf = iha_se2_rd(sc, i);
   2815      1.1  tsutsui 		chksum += *buf++;
   2816      1.1  tsutsui 	}
   2817  1.4.2.1    lukem 	*buf = iha_se2_rd(sc, 31); /* read checksum from EEPROM */
   2818      1.1  tsutsui 
   2819  1.4.2.1    lukem 	chksum &= 0x0000ffff; /* lower 16 bits */
   2820      1.1  tsutsui 
   2821      1.1  tsutsui 	return (eeprom->signature == EEP_SIGNATURE) &&
   2822      1.1  tsutsui 	    (eeprom->checksum == chksum);
   2823      1.1  tsutsui }
   2824      1.1  tsutsui 
   2825      1.1  tsutsui /*
   2826  1.4.2.1    lukem  * iha_se2_instr - write an octet to serial E2PROM one bit at a time
   2827      1.1  tsutsui  */
   2828      1.1  tsutsui void
   2829  1.4.2.1    lukem iha_se2_instr(sc, instr)
   2830      1.1  tsutsui 	struct iha_softc *sc;
   2831      1.1  tsutsui 	int instr;
   2832      1.1  tsutsui {
   2833      1.1  tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2834      1.1  tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2835      1.1  tsutsui 	int b, i;
   2836      1.1  tsutsui 
   2837      1.1  tsutsui 	b = NVRCS | NVRDO; /* Write the start bit (== 1) */
   2838      1.1  tsutsui 
   2839      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, b);
   2840      1.1  tsutsui 	EEP_WAIT();
   2841      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, b | NVRCK);
   2842      1.1  tsutsui 	EEP_WAIT();
   2843      1.1  tsutsui 
   2844      1.1  tsutsui 	for (i = 8; i > 0; i--) {
   2845      1.1  tsutsui 		if (instr & (1 << (i - 1)))
   2846      1.1  tsutsui 			b = NVRCS | NVRDO; /* Write a 1 bit */
   2847      1.1  tsutsui 		else
   2848      1.1  tsutsui 			b = NVRCS;	   /* Write a 0 bit */
   2849      1.1  tsutsui 
   2850      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_NVRAM, b);
   2851      1.1  tsutsui 		EEP_WAIT();
   2852      1.1  tsutsui 		bus_space_write_1(iot, ioh, TUL_NVRAM, b | NVRCK);
   2853      1.1  tsutsui 		EEP_WAIT();
   2854      1.1  tsutsui 	}
   2855      1.1  tsutsui 
   2856      1.1  tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
   2857      1.1  tsutsui }
   2858      1.1  tsutsui 
   2859      1.1  tsutsui /*
   2860  1.4.2.1    lukem  * iha_reset_tcs - reset the target control structure pointed
   2861      1.1  tsutsui  *		   to by tcs to default values. tcs flags
   2862      1.1  tsutsui  *		   only has the negotiation done bits reset as
   2863      1.1  tsutsui  *		   the other bits are fixed at initialization.
   2864      1.1  tsutsui  */
   2865      1.1  tsutsui void
   2866  1.4.2.1    lukem iha_reset_tcs(tcs, config0)
   2867      1.1  tsutsui 	struct tcs *tcs;
   2868      1.1  tsutsui 	u_int8_t config0;
   2869      1.1  tsutsui {
   2870      1.1  tsutsui 
   2871      1.1  tsutsui 	tcs->flags &= ~(FLAG_SYNC_DONE | FLAG_WIDE_DONE);
   2872      1.1  tsutsui 	tcs->period = 0;
   2873      1.1  tsutsui 	tcs->offset = 0;
   2874      1.1  tsutsui 	tcs->tagcnt = 0;
   2875      1.1  tsutsui 	tcs->ntagscb  = NULL;
   2876      1.1  tsutsui 	tcs->syncm = 0;
   2877      1.1  tsutsui 	tcs->sconfig0 = config0;
   2878      1.1  tsutsui }
   2879