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