Home | History | Annotate | Line # | Download | only in ic
iha.c revision 1.31.8.2
      1  1.31.8.2      yamt /*	$NetBSD: iha.c,v 1.31.8.2 2006/05/24 10:57:41 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.31.8.2      yamt __KERNEL_RCSID(0, "$NetBSD: iha.c,v 1.31.8.2 2006/05/24 10:57:41 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.28   thorpej #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.31     perry 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.31     perry static inline void iha_append_pend_scb(struct iha_softc *, struct iha_scb *);
    142      1.31     perry static inline void iha_push_pend_scb(struct iha_softc *, struct iha_scb *);
    143      1.31     perry static inline void iha_del_pend_scb(struct iha_softc *, struct iha_scb *);
    144      1.31     perry static inline void iha_mark_busy_scb(struct iha_scb *);
    145      1.15   tsutsui 
    146      1.31     perry 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.31     perry 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.28   thorpej 			} 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.31     perry 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.31     perry 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.31     perry 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.31     perry 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.31     perry 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 		else {
    683      1.15   tsutsui 			printf("unable to allocate scb\n");
    684  1.31.8.2      yamt #ifdef DIAGNOSTIC
    685  1.31.8.2      yamt 			scsipi_printaddr(periph);
    686      1.15   tsutsui 			panic("iha_scsipi_request");
    687  1.31.8.2      yamt #else
    688  1.31.8.2      yamt 			splx(s);
    689  1.31.8.2      yamt 			return;
    690      1.15   tsutsui #endif
    691  1.31.8.2      yamt 		}
    692      1.15   tsutsui 		splx(s);
    693      1.15   tsutsui 
    694      1.15   tsutsui 		scb->target = periph->periph_target;
    695      1.15   tsutsui 		scb->lun = periph->periph_lun;
    696      1.15   tsutsui 		scb->tcs = &sc->sc_tcs[scb->target];
    697      1.15   tsutsui 		scb->scb_id = MSG_IDENTIFY(periph->periph_lun,
    698      1.15   tsutsui 		    (xs->xs_control & XS_CTL_REQSENSE) == 0);
    699      1.15   tsutsui 
    700      1.15   tsutsui 		scb->xs = xs;
    701      1.15   tsutsui 		scb->cmdlen = xs->cmdlen;
    702      1.15   tsutsui 		memcpy(&scb->cmd, xs->cmd, xs->cmdlen);
    703      1.15   tsutsui 		scb->buflen = xs->datalen;
    704      1.15   tsutsui 		scb->flags = 0;
    705      1.15   tsutsui 		if (xs->xs_control & XS_CTL_DATA_OUT)
    706      1.15   tsutsui 			scb->flags |= FLAG_DATAOUT;
    707      1.15   tsutsui 		if (xs->xs_control & XS_CTL_DATA_IN)
    708      1.15   tsutsui 			scb->flags |= FLAG_DATAIN;
    709      1.15   tsutsui 
    710      1.15   tsutsui 		if (scb->flags & (FLAG_DATAIN | FLAG_DATAOUT)) {
    711      1.15   tsutsui 			error = bus_dmamap_load(sc->sc_dmat, scb->dmap,
    712      1.15   tsutsui 			    xs->data, scb->buflen, NULL,
    713      1.15   tsutsui 			    ((xs->xs_control & XS_CTL_NOSLEEP) ?
    714      1.15   tsutsui 			     BUS_DMA_NOWAIT : BUS_DMA_WAITOK) |
    715      1.15   tsutsui 			    BUS_DMA_STREAMING |
    716      1.15   tsutsui 			    ((scb->flags & FLAG_DATAIN) ?
    717      1.15   tsutsui 			     BUS_DMA_READ : BUS_DMA_WRITE));
    718       1.1   tsutsui 
    719      1.15   tsutsui 			if (error) {
    720      1.22       wiz 				printf("%s: error %d loading DMA map\n",
    721      1.15   tsutsui 				    sc->sc_dev.dv_xname, error);
    722      1.15   tsutsui 				iha_append_free_scb(sc, scb);
    723      1.15   tsutsui 				xs->error = XS_DRIVER_STUFFUP;
    724      1.15   tsutsui 				scsipi_done(xs);
    725      1.15   tsutsui 				return;
    726      1.15   tsutsui 			}
    727      1.15   tsutsui 			bus_dmamap_sync(sc->sc_dmat, scb->dmap,
    728      1.15   tsutsui 			    0, scb->dmap->dm_mapsize,
    729      1.15   tsutsui 			    (scb->flags & FLAG_DATAIN) ?
    730      1.15   tsutsui 			    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    731      1.15   tsutsui 		}
    732       1.1   tsutsui 
    733      1.15   tsutsui 		iha_exec_scb(sc, scb);
    734      1.15   tsutsui 		return;
    735       1.1   tsutsui 
    736      1.15   tsutsui 	case ADAPTER_REQ_GROW_RESOURCES:
    737      1.15   tsutsui 		return; /* XXX */
    738       1.1   tsutsui 
    739      1.15   tsutsui 	case ADAPTER_REQ_SET_XFER_MODE:
    740      1.15   tsutsui 		{
    741      1.15   tsutsui 			struct tcs *tcs;
    742      1.15   tsutsui 			struct scsipi_xfer_mode *xm = arg;
    743       1.1   tsutsui 
    744      1.15   tsutsui 			tcs = &sc->sc_tcs[xm->xm_target];
    745       1.1   tsutsui 
    746      1.15   tsutsui 			if ((xm->xm_mode & PERIPH_CAP_WIDE16) != 0 &&
    747      1.15   tsutsui 			    (tcs->flags & FLAG_NO_WIDE) == 0)
    748      1.15   tsutsui 				tcs->flags &= ~(FLAG_WIDE_DONE|FLAG_SYNC_DONE);
    749       1.1   tsutsui 
    750      1.15   tsutsui 			if ((xm->xm_mode & PERIPH_CAP_SYNC) != 0 &&
    751      1.15   tsutsui 			    (tcs->flags & FLAG_NO_SYNC) == 0)
    752      1.15   tsutsui 				tcs->flags &= ~FLAG_SYNC_DONE;
    753       1.1   tsutsui 
    754      1.15   tsutsui 			/*
    755      1.15   tsutsui 			 * If we're not going to negotiate, send the
    756      1.15   tsutsui 			 * notification now, since it won't happen later.
    757      1.15   tsutsui 			 */
    758      1.15   tsutsui 			if ((tcs->flags & (FLAG_WIDE_DONE|FLAG_SYNC_DONE)) ==
    759      1.15   tsutsui 			    (FLAG_WIDE_DONE|FLAG_SYNC_DONE))
    760      1.15   tsutsui 				iha_update_xfer_mode(sc, xm->xm_target);
    761       1.1   tsutsui 
    762      1.15   tsutsui 			return;
    763      1.15   tsutsui 		}
    764      1.15   tsutsui 	}
    765       1.1   tsutsui }
    766       1.1   tsutsui 
    767      1.15   tsutsui void
    768      1.25   tsutsui iha_update_xfer_mode(struct iha_softc *sc, int target)
    769       1.1   tsutsui {
    770      1.15   tsutsui 	struct tcs *tcs = &sc->sc_tcs[target];
    771      1.15   tsutsui 	struct scsipi_xfer_mode xm;
    772       1.1   tsutsui 
    773      1.15   tsutsui 	xm.xm_target = target;
    774      1.15   tsutsui 	xm.xm_mode = 0;
    775      1.15   tsutsui 	xm.xm_period = 0;
    776      1.15   tsutsui 	xm.xm_offset = 0;
    777       1.1   tsutsui 
    778      1.15   tsutsui 	if (tcs->syncm & PERIOD_WIDE_SCSI)
    779      1.15   tsutsui 		xm.xm_mode |= PERIPH_CAP_WIDE16;
    780       1.1   tsutsui 
    781      1.15   tsutsui 	if (tcs->period) {
    782      1.15   tsutsui 		xm.xm_mode |= PERIPH_CAP_SYNC;
    783      1.15   tsutsui 		xm.xm_period = tcs->period;
    784      1.15   tsutsui 		xm.xm_offset = tcs->offset;
    785      1.15   tsutsui 	}
    786       1.1   tsutsui 
    787      1.15   tsutsui 	scsipi_async_event(&sc->sc_channel, ASYNC_EVENT_XFER_MODE, &xm);
    788       1.1   tsutsui }
    789       1.1   tsutsui 
    790      1.15   tsutsui static void
    791      1.25   tsutsui iha_reset_scsi_bus(struct iha_softc *sc)
    792       1.1   tsutsui {
    793      1.15   tsutsui 	struct iha_scb *scb;
    794       1.1   tsutsui 	struct tcs *tcs;
    795      1.15   tsutsui 	int i, s;
    796       1.1   tsutsui 
    797       1.1   tsutsui 	s = splbio();
    798       1.1   tsutsui 
    799      1.15   tsutsui 	iha_reset_dma(sc);
    800       1.1   tsutsui 
    801      1.15   tsutsui 	for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++)
    802      1.15   tsutsui 		switch (scb->status) {
    803      1.15   tsutsui 		case STATUS_BUSY:
    804      1.15   tsutsui 			iha_append_done_scb(sc, scb, HOST_SCSI_RST);
    805      1.15   tsutsui 			break;
    806       1.1   tsutsui 
    807      1.15   tsutsui 		case STATUS_SELECT:
    808      1.15   tsutsui 			iha_push_pend_scb(sc, scb);
    809      1.15   tsutsui 			break;
    810       1.1   tsutsui 
    811      1.15   tsutsui 		default:
    812      1.15   tsutsui 			break;
    813      1.15   tsutsui 		}
    814       1.1   tsutsui 
    815      1.15   tsutsui 	for (i = 0, tcs = sc->sc_tcs; i < IHA_MAX_TARGETS; i++, tcs++)
    816      1.15   tsutsui 		iha_reset_tcs(tcs, sc->sc_sconf1);
    817       1.1   tsutsui 
    818       1.1   tsutsui 	splx(s);
    819       1.1   tsutsui }
    820       1.1   tsutsui 
    821      1.15   tsutsui void
    822      1.25   tsutsui iha_reset_chip(struct iha_softc *sc)
    823       1.1   tsutsui {
    824      1.15   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
    825      1.15   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
    826      1.15   tsutsui 
    827      1.15   tsutsui 	/* reset tulip chip */
    828      1.15   tsutsui 
    829      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_SCTRL0, RSCSI);
    830       1.1   tsutsui 
    831      1.15   tsutsui 	do {
    832      1.15   tsutsui 		sc->sc_sistat = bus_space_read_1(iot, ioh, TUL_SISTAT);
    833      1.15   tsutsui 	} while ((sc->sc_sistat & SRSTD) == 0);
    834       1.1   tsutsui 
    835      1.15   tsutsui 	iha_set_ssig(sc, 0, 0);
    836       1.1   tsutsui 
    837  1.31.8.2      yamt 	/* Clear any active interrupt*/
    838  1.31.8.2      yamt 	(void)bus_space_read_1(iot, ioh, TUL_SISTAT);
    839       1.1   tsutsui }
    840       1.1   tsutsui 
    841      1.15   tsutsui /*
    842      1.15   tsutsui  * iha_reset_dma - abort any active DMA xfer, reset tulip FIFO.
    843      1.15   tsutsui  */
    844      1.15   tsutsui static void
    845      1.25   tsutsui iha_reset_dma(struct iha_softc *sc)
    846       1.1   tsutsui {
    847      1.15   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
    848      1.15   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
    849       1.1   tsutsui 
    850      1.15   tsutsui 	if ((bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND) != 0) {
    851      1.15   tsutsui 		/* if DMA xfer is pending, abort DMA xfer */
    852      1.15   tsutsui 		bus_space_write_1(iot, ioh, TUL_DCMD, ABTXFR);
    853      1.15   tsutsui 		/* wait Abort DMA xfer done */
    854      1.15   tsutsui 		while ((bus_space_read_1(iot, ioh, TUL_ISTUS0) & DABT) == 0)
    855      1.15   tsutsui 			;
    856      1.15   tsutsui 	}
    857       1.1   tsutsui 
    858      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
    859       1.1   tsutsui }
    860       1.1   tsutsui 
    861      1.15   tsutsui /*
    862      1.15   tsutsui  * iha_reset_tcs - reset the target control structure pointed
    863      1.15   tsutsui  *		   to by tcs to default values. tcs flags
    864      1.15   tsutsui  *		   only has the negotiation done bits reset as
    865      1.15   tsutsui  *		   the other bits are fixed at initialization.
    866      1.15   tsutsui  */
    867       1.1   tsutsui static void
    868      1.27   tsutsui iha_reset_tcs(struct tcs *tcs, uint8_t config0)
    869       1.1   tsutsui {
    870       1.1   tsutsui 
    871      1.15   tsutsui 	tcs->flags &= ~(FLAG_SYNC_DONE | FLAG_WIDE_DONE);
    872      1.15   tsutsui 	tcs->period = 0;
    873      1.15   tsutsui 	tcs->offset = 0;
    874      1.15   tsutsui 	tcs->tagcnt = 0;
    875      1.15   tsutsui 	tcs->ntagscb  = NULL;
    876      1.15   tsutsui 	tcs->syncm = 0;
    877      1.15   tsutsui 	tcs->sconfig0 = config0;
    878       1.1   tsutsui }
    879       1.1   tsutsui 
    880       1.1   tsutsui /*
    881       1.7   tsutsui  * iha_main - process the active SCB, taking one off pending and making it
    882       1.1   tsutsui  *	      active if necessary, and any done SCB's created as
    883       1.1   tsutsui  *	      a result until there are no interrupts pending and no pending
    884       1.1   tsutsui  *	      SCB's that can be started.
    885       1.1   tsutsui  */
    886       1.1   tsutsui static void
    887      1.25   tsutsui iha_main(struct iha_softc *sc)
    888       1.1   tsutsui {
    889       1.1   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
    890       1.1   tsutsui 	bus_space_handle_t ioh =sc->sc_ioh;
    891      1.15   tsutsui 	struct iha_scb *scb;
    892       1.1   tsutsui 
    893       1.1   tsutsui 	for (;;) {
    894       1.7   tsutsui 		iha_scsi(sc);
    895       1.1   tsutsui 
    896       1.7   tsutsui 		while ((scb = iha_pop_done_scb(sc)) != NULL)
    897       1.7   tsutsui 			iha_done_scb(sc, scb);
    898       1.1   tsutsui 
    899       1.1   tsutsui 		/*
    900       1.1   tsutsui 		 * If there are no interrupts pending, or we can't start
    901       1.1   tsutsui 		 * a pending sc, break out of the for(;;). Otherwise
    902       1.1   tsutsui 		 * continue the good work with another call to
    903       1.7   tsutsui 		 * iha_scsi().
    904       1.1   tsutsui 		 */
    905       1.1   tsutsui 		if (((bus_space_read_1(iot, ioh, TUL_STAT0) & INTPD) == 0)
    906       1.7   tsutsui 		    && (iha_find_pend_scb(sc) == NULL))
    907       1.1   tsutsui 			break;
    908       1.1   tsutsui 	}
    909       1.1   tsutsui }
    910       1.1   tsutsui 
    911       1.1   tsutsui /*
    912       1.7   tsutsui  * iha_scsi - service any outstanding interrupts. If there are none, try to
    913       1.1   tsutsui  *            start another SCB currently in the pending queue.
    914       1.1   tsutsui  */
    915       1.1   tsutsui static void
    916      1.25   tsutsui iha_scsi(struct iha_softc *sc)
    917       1.1   tsutsui {
    918       1.1   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
    919       1.1   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
    920      1.15   tsutsui 	struct iha_scb *scb;
    921       1.1   tsutsui 	struct tcs *tcs;
    922      1.27   tsutsui 	uint8_t stat;
    923       1.1   tsutsui 
    924       1.1   tsutsui 	/* service pending interrupts asap */
    925       1.1   tsutsui 
    926       1.1   tsutsui 	stat = bus_space_read_1(iot, ioh, TUL_STAT0);
    927       1.1   tsutsui 	if ((stat & INTPD) != 0) {
    928       1.1   tsutsui 		sc->sc_status0 = stat;
    929       1.1   tsutsui 		sc->sc_status1 = bus_space_read_1(iot, ioh, TUL_STAT1);
    930       1.1   tsutsui 		sc->sc_sistat = bus_space_read_1(iot, ioh, TUL_SISTAT);
    931       1.1   tsutsui 
    932       1.1   tsutsui 		sc->sc_phase = sc->sc_status0 & PH_MASK;
    933       1.1   tsutsui 
    934       1.1   tsutsui 		if ((sc->sc_sistat & SRSTD) != 0) {
    935       1.7   tsutsui 			iha_reset_scsi_bus(sc);
    936       1.1   tsutsui 			return;
    937       1.1   tsutsui 		}
    938       1.1   tsutsui 
    939       1.1   tsutsui 		if ((sc->sc_sistat & RSELED) != 0) {
    940       1.7   tsutsui 			iha_resel(sc);
    941       1.1   tsutsui 			return;
    942       1.1   tsutsui 		}
    943       1.1   tsutsui 
    944       1.1   tsutsui 		if ((sc->sc_sistat & (STIMEO | DISCD)) != 0) {
    945       1.7   tsutsui 			iha_busfree(sc);
    946       1.1   tsutsui 			return;
    947       1.1   tsutsui 		}
    948       1.1   tsutsui 
    949       1.1   tsutsui 		if ((sc->sc_sistat & (SCMDN | SBSRV)) != 0) {
    950       1.7   tsutsui 			iha_next_state(sc);
    951       1.1   tsutsui 			return;
    952       1.1   tsutsui 		}
    953       1.1   tsutsui 
    954       1.1   tsutsui 		if ((sc->sc_sistat & SELED) != 0)
    955       1.7   tsutsui 			iha_set_ssig(sc, 0, 0);
    956       1.1   tsutsui 	}
    957       1.1   tsutsui 
    958       1.1   tsutsui 	/*
    959       1.1   tsutsui 	 * There were no interrupts pending which required action elsewhere, so
    960       1.1   tsutsui 	 * see if it is possible to start the selection phase on a pending SCB
    961       1.1   tsutsui 	 */
    962       1.7   tsutsui 	if ((scb = iha_find_pend_scb(sc)) == NULL)
    963       1.1   tsutsui 		return;
    964       1.1   tsutsui 
    965       1.1   tsutsui 	tcs = scb->tcs;
    966       1.1   tsutsui 
    967       1.1   tsutsui 	/* program HBA's SCSI ID & target SCSI ID */
    968       1.1   tsutsui 	bus_space_write_1(iot, ioh, TUL_SID, (sc->sc_id << 4) | scb->target);
    969       1.1   tsutsui 
    970      1.12   tsutsui 	if ((scb->xs->xs_control & XS_CTL_RESET) == 0) {
    971       1.1   tsutsui 		bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
    972       1.1   tsutsui 
    973       1.1   tsutsui 		if ((tcs->flags & FLAG_NO_NEG_SYNC) == 0 ||
    974       1.1   tsutsui 		    (tcs->flags & FLAG_NO_NEG_WIDE) == 0)
    975       1.7   tsutsui 			iha_select(sc, scb, SELATNSTOP);
    976       1.1   tsutsui 
    977       1.1   tsutsui 		else if (scb->scb_tagmsg != 0)
    978       1.7   tsutsui 			iha_select(sc, scb, SEL_ATN3);
    979       1.1   tsutsui 
    980       1.1   tsutsui 		else
    981       1.7   tsutsui 			iha_select(sc, scb, SEL_ATN);
    982       1.1   tsutsui 
    983       1.1   tsutsui 	} else {
    984       1.7   tsutsui 		iha_select(sc, scb, SELATNSTOP);
    985       1.1   tsutsui 		scb->nextstat = 8;
    986       1.1   tsutsui 	}
    987       1.1   tsutsui 
    988      1.12   tsutsui 	if ((scb->xs->xs_control & XS_CTL_POLL) != 0) {
    989      1.12   tsutsui 		int timeout;
    990      1.12   tsutsui 		for (timeout = scb->xs->timeout; timeout > 0; timeout--) {
    991       1.7   tsutsui 			if (iha_wait(sc, NO_OP) == -1)
    992       1.1   tsutsui 				break;
    993       1.7   tsutsui 			if (iha_next_state(sc) == -1)
    994       1.1   tsutsui 				break;
    995       1.1   tsutsui 			delay(1000); /* Only happens in boot, so it's ok */
    996       1.1   tsutsui 		}
    997       1.1   tsutsui 
    998       1.1   tsutsui 		/*
    999       1.1   tsutsui 		 * Since done queue processing not done until AFTER this
   1000       1.1   tsutsui 		 * function returns, scb is on the done queue, not
   1001       1.1   tsutsui 		 * the free queue at this point and still has valid data
   1002       1.1   tsutsui 		 *
   1003       1.1   tsutsui 		 * Conversely, xs->error has not been set yet
   1004       1.1   tsutsui 		 */
   1005      1.12   tsutsui 		if (timeout == 0)
   1006       1.7   tsutsui 			iha_timeout(scb);
   1007       1.1   tsutsui 	}
   1008       1.1   tsutsui }
   1009       1.1   tsutsui 
   1010      1.15   tsutsui static void
   1011      1.27   tsutsui iha_select(struct iha_softc *sc, struct iha_scb *scb, uint8_t select_type)
   1012       1.1   tsutsui {
   1013      1.15   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1014      1.15   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1015      1.15   tsutsui 
   1016      1.15   tsutsui 	switch (select_type) {
   1017      1.15   tsutsui 	case SEL_ATN:
   1018      1.15   tsutsui 		bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_id);
   1019      1.15   tsutsui 		bus_space_write_multi_1(iot, ioh, TUL_SFIFO,
   1020      1.15   tsutsui 		    scb->cmd, scb->cmdlen);
   1021      1.15   tsutsui 
   1022      1.15   tsutsui 		scb->nextstat = 2;
   1023      1.15   tsutsui 		break;
   1024      1.15   tsutsui 
   1025      1.15   tsutsui 	case SELATNSTOP:
   1026      1.15   tsutsui 		scb->nextstat = 1;
   1027      1.15   tsutsui 		break;
   1028       1.1   tsutsui 
   1029      1.15   tsutsui 	case SEL_ATN3:
   1030      1.15   tsutsui 		bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_id);
   1031      1.15   tsutsui 		bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_tagmsg);
   1032      1.15   tsutsui 		bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_tagid);
   1033       1.1   tsutsui 
   1034      1.15   tsutsui 		bus_space_write_multi_1(iot, ioh, TUL_SFIFO, scb->cmd,
   1035      1.15   tsutsui 		    scb->cmdlen);
   1036       1.1   tsutsui 
   1037      1.15   tsutsui 		scb->nextstat = 2;
   1038       1.1   tsutsui 		break;
   1039       1.1   tsutsui 
   1040       1.1   tsutsui 	default:
   1041      1.15   tsutsui 		printf("[debug] iha_select() - unknown select type = 0x%02x\n",
   1042      1.15   tsutsui 		    select_type);
   1043      1.15   tsutsui 		return;
   1044       1.1   tsutsui 	}
   1045      1.15   tsutsui 
   1046      1.15   tsutsui 	iha_del_pend_scb(sc, scb);
   1047      1.15   tsutsui 	scb->status = STATUS_SELECT;
   1048      1.15   tsutsui 
   1049      1.15   tsutsui 	sc->sc_actscb = scb;
   1050      1.15   tsutsui 
   1051      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_SCMD, select_type);
   1052       1.1   tsutsui }
   1053       1.1   tsutsui 
   1054       1.1   tsutsui /*
   1055      1.15   tsutsui  * iha_wait - wait for an interrupt to service or a SCSI bus phase change
   1056      1.15   tsutsui  *            after writing the supplied command to the tulip chip. If
   1057      1.15   tsutsui  *            the command is NO_OP, skip the command writing.
   1058       1.1   tsutsui  */
   1059       1.1   tsutsui static int
   1060      1.27   tsutsui iha_wait(struct iha_softc *sc, uint8_t cmd)
   1061       1.1   tsutsui {
   1062      1.15   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1063      1.15   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1064       1.1   tsutsui 
   1065      1.15   tsutsui 	if (cmd != NO_OP)
   1066      1.15   tsutsui 		bus_space_write_1(iot, ioh, TUL_SCMD, cmd);
   1067       1.1   tsutsui 
   1068      1.15   tsutsui 	/*
   1069      1.15   tsutsui 	 * Have to do this here, in addition to in iha_isr, because
   1070      1.15   tsutsui 	 * interrupts might be turned off when we get here.
   1071      1.15   tsutsui 	 */
   1072      1.15   tsutsui 	do {
   1073      1.15   tsutsui 		sc->sc_status0 = bus_space_read_1(iot, ioh, TUL_STAT0);
   1074      1.15   tsutsui 	} while ((sc->sc_status0 & INTPD) == 0);
   1075      1.15   tsutsui 
   1076      1.15   tsutsui 	sc->sc_status1 = bus_space_read_1(iot, ioh, TUL_STAT1);
   1077      1.15   tsutsui 	sc->sc_sistat = bus_space_read_1(iot, ioh, TUL_SISTAT);
   1078      1.15   tsutsui 
   1079      1.15   tsutsui 	sc->sc_phase = sc->sc_status0 & PH_MASK;
   1080       1.1   tsutsui 
   1081      1.15   tsutsui 	if ((sc->sc_sistat & SRSTD) != 0) {
   1082      1.15   tsutsui 		/* SCSI bus reset interrupt */
   1083      1.15   tsutsui 		iha_reset_scsi_bus(sc);
   1084      1.15   tsutsui 		return (-1);
   1085      1.15   tsutsui 	}
   1086       1.1   tsutsui 
   1087      1.15   tsutsui 	if ((sc->sc_sistat & RSELED) != 0)
   1088      1.15   tsutsui 		/* Reselection interrupt */
   1089      1.15   tsutsui 		return (iha_resel(sc));
   1090       1.1   tsutsui 
   1091      1.15   tsutsui 	if ((sc->sc_sistat & STIMEO) != 0) {
   1092      1.15   tsutsui 		/* selected/reselected timeout interrupt */
   1093      1.15   tsutsui 		iha_busfree(sc);
   1094      1.15   tsutsui 		return (-1);
   1095      1.15   tsutsui 	}
   1096       1.1   tsutsui 
   1097      1.15   tsutsui 	if ((sc->sc_sistat & DISCD) != 0) {
   1098      1.15   tsutsui 		/* BUS disconnection interrupt */
   1099      1.15   tsutsui 		if ((sc->sc_flags & FLAG_EXPECT_DONE_DISC) != 0) {
   1100      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1101      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_SCONFIG0,
   1102      1.15   tsutsui 			    SCONFIG0DEFAULT);
   1103      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
   1104      1.15   tsutsui 			iha_append_done_scb(sc, sc->sc_actscb, HOST_OK);
   1105      1.15   tsutsui 			sc->sc_flags &= ~FLAG_EXPECT_DONE_DISC;
   1106       1.1   tsutsui 
   1107      1.15   tsutsui 		} else if ((sc->sc_flags & FLAG_EXPECT_DISC) != 0) {
   1108      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1109      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_SCONFIG0,
   1110      1.15   tsutsui 			    SCONFIG0DEFAULT);
   1111      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
   1112      1.15   tsutsui 			sc->sc_actscb = NULL;
   1113      1.15   tsutsui 			sc->sc_flags &= ~FLAG_EXPECT_DISC;
   1114       1.1   tsutsui 
   1115      1.15   tsutsui 		} else
   1116      1.15   tsutsui 			iha_busfree(sc);
   1117       1.1   tsutsui 
   1118      1.15   tsutsui 		return (-1);
   1119       1.1   tsutsui 	}
   1120       1.1   tsutsui 
   1121      1.15   tsutsui 	return (sc->sc_phase);
   1122       1.1   tsutsui }
   1123       1.1   tsutsui 
   1124      1.15   tsutsui static void
   1125      1.25   tsutsui iha_exec_scb(struct iha_softc *sc, struct iha_scb *scb)
   1126       1.1   tsutsui {
   1127      1.15   tsutsui 	bus_space_tag_t iot;
   1128      1.15   tsutsui 	bus_space_handle_t ioh;
   1129      1.15   tsutsui 	bus_dmamap_t dm;
   1130      1.15   tsutsui 	struct scsipi_xfer *xs = scb->xs;
   1131      1.15   tsutsui 	int nseg, s;
   1132      1.15   tsutsui 
   1133      1.15   tsutsui 	dm = scb->dmap;
   1134      1.15   tsutsui 	nseg = dm->dm_nsegs;
   1135      1.15   tsutsui 
   1136      1.15   tsutsui 	if (nseg > 1) {
   1137      1.15   tsutsui 		struct iha_sg_element *sg = scb->sglist;
   1138      1.15   tsutsui 		int i;
   1139      1.15   tsutsui 
   1140      1.15   tsutsui 		for (i = 0; i < nseg; i++) {
   1141      1.15   tsutsui 			sg[i].sg_len = htole32(dm->dm_segs[i].ds_len);
   1142      1.15   tsutsui 			sg[i].sg_addr = htole32(dm->dm_segs[i].ds_addr);
   1143      1.15   tsutsui 		}
   1144      1.15   tsutsui 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   1145      1.15   tsutsui 		    scb->sgoffset, IHA_SG_SIZE,
   1146      1.15   tsutsui 		    BUS_DMASYNC_PREWRITE);
   1147      1.15   tsutsui 
   1148      1.15   tsutsui 		scb->flags |= FLAG_SG;
   1149      1.15   tsutsui 		scb->sg_size = scb->sg_max = nseg;
   1150      1.15   tsutsui 		scb->sg_index = 0;
   1151      1.15   tsutsui 
   1152      1.15   tsutsui 		scb->bufaddr = scb->sg_addr;
   1153      1.15   tsutsui 	} else
   1154      1.15   tsutsui 		scb->bufaddr = dm->dm_segs[0].ds_addr;
   1155       1.1   tsutsui 
   1156      1.15   tsutsui 	if ((xs->xs_control & XS_CTL_POLL) == 0) {
   1157      1.17    bouyer 		int timeout = mstohz(xs->timeout);
   1158      1.15   tsutsui 		if (timeout == 0)
   1159      1.15   tsutsui 			timeout = 1;
   1160      1.15   tsutsui 		callout_reset(&xs->xs_callout, timeout, iha_timeout, scb);
   1161      1.15   tsutsui 	}
   1162       1.1   tsutsui 
   1163      1.15   tsutsui 	s = splbio();
   1164       1.1   tsutsui 
   1165      1.15   tsutsui 	if (((scb->xs->xs_control & XS_RESET) != 0) ||
   1166      1.28   thorpej 	    (scb->cmd[0] == SCSI_REQUEST_SENSE))
   1167      1.15   tsutsui 		iha_push_pend_scb(sc, scb);   /* Insert SCB at head of Pend */
   1168      1.15   tsutsui 	else
   1169      1.15   tsutsui 		iha_append_pend_scb(sc, scb); /* Append SCB to tail of Pend */
   1170       1.1   tsutsui 
   1171       1.1   tsutsui 	/*
   1172      1.15   tsutsui 	 * Run through iha_main() to ensure something is active, if
   1173      1.15   tsutsui 	 * only this new SCB.
   1174       1.1   tsutsui 	 */
   1175      1.15   tsutsui 	if (sc->sc_semaph != SEMAPH_IN_MAIN) {
   1176      1.15   tsutsui 		iot = sc->sc_iot;
   1177      1.15   tsutsui 		ioh = sc->sc_ioh;
   1178       1.1   tsutsui 
   1179      1.15   tsutsui 		bus_space_write_1(iot, ioh, TUL_IMSK, MASK_ALL);
   1180      1.21    simonb 		sc->sc_semaph = SEMAPH_IN_MAIN;
   1181       1.1   tsutsui 
   1182      1.15   tsutsui 		splx(s);
   1183      1.15   tsutsui 		iha_main(sc);
   1184      1.15   tsutsui 		s = splbio();
   1185       1.1   tsutsui 
   1186      1.21    simonb 		sc->sc_semaph = ~SEMAPH_IN_MAIN;
   1187      1.15   tsutsui 		bus_space_write_1(iot, ioh, TUL_IMSK, (MASK_ALL & ~MSCMP));
   1188       1.1   tsutsui 	}
   1189       1.1   tsutsui 
   1190      1.15   tsutsui 	splx(s);
   1191       1.1   tsutsui }
   1192       1.1   tsutsui 
   1193       1.1   tsutsui /*
   1194      1.15   tsutsui  * iha_done_scb - We have a scb which has been processed by the
   1195      1.15   tsutsui  *                adaptor, now we look to see how the operation went.
   1196       1.1   tsutsui  */
   1197      1.15   tsutsui static void
   1198      1.25   tsutsui iha_done_scb(struct iha_softc *sc, struct iha_scb *scb)
   1199       1.1   tsutsui {
   1200      1.15   tsutsui 	struct scsipi_xfer *xs = scb->xs;
   1201      1.15   tsutsui 
   1202      1.15   tsutsui 	if (xs != NULL) {
   1203      1.15   tsutsui 		/* Cancel the timeout. */
   1204      1.15   tsutsui 		callout_stop(&xs->xs_callout);
   1205      1.15   tsutsui 
   1206      1.15   tsutsui 		if (scb->flags & (FLAG_DATAIN | FLAG_DATAOUT)) {
   1207      1.15   tsutsui 			bus_dmamap_sync(sc->sc_dmat, scb->dmap,
   1208      1.15   tsutsui 			    0, scb->dmap->dm_mapsize,
   1209      1.15   tsutsui 			    (scb->flags & FLAG_DATAIN) ?
   1210      1.15   tsutsui 			    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1211      1.15   tsutsui 			bus_dmamap_unload(sc->sc_dmat, scb->dmap);
   1212      1.15   tsutsui 		}
   1213       1.1   tsutsui 
   1214      1.15   tsutsui 		xs->status = scb->ta_stat;
   1215       1.1   tsutsui 
   1216      1.15   tsutsui 		switch (scb->ha_stat) {
   1217      1.15   tsutsui 		case HOST_OK:
   1218      1.15   tsutsui 			switch (scb->ta_stat) {
   1219      1.15   tsutsui 			case SCSI_OK:
   1220      1.15   tsutsui 			case SCSI_CONDITION_MET:
   1221      1.15   tsutsui 			case SCSI_INTERM:
   1222      1.15   tsutsui 			case SCSI_INTERM_COND_MET:
   1223      1.15   tsutsui 				xs->resid = scb->buflen;
   1224      1.15   tsutsui 				xs->error = XS_NOERROR;
   1225      1.15   tsutsui 				if ((scb->flags & FLAG_RSENS) != 0)
   1226      1.15   tsutsui 					xs->error = XS_SENSE;
   1227      1.15   tsutsui 				break;
   1228       1.1   tsutsui 
   1229      1.15   tsutsui 			case SCSI_RESV_CONFLICT:
   1230      1.15   tsutsui 			case SCSI_BUSY:
   1231      1.15   tsutsui 			case SCSI_QUEUE_FULL:
   1232      1.15   tsutsui 				xs->error = XS_BUSY;
   1233      1.15   tsutsui 				break;
   1234       1.1   tsutsui 
   1235      1.15   tsutsui 			case SCSI_TERMINATED:
   1236      1.15   tsutsui 			case SCSI_ACA_ACTIVE:
   1237      1.15   tsutsui 			case SCSI_CHECK:
   1238      1.15   tsutsui 				scb->tcs->flags &=
   1239      1.15   tsutsui 				    ~(FLAG_SYNC_DONE | FLAG_WIDE_DONE);
   1240       1.1   tsutsui 
   1241      1.15   tsutsui 				if ((scb->flags & FLAG_RSENS) != 0 ||
   1242      1.15   tsutsui 				    iha_push_sense_request(sc, scb) != 0) {
   1243      1.15   tsutsui 					scb->flags &= ~FLAG_RSENS;
   1244      1.15   tsutsui 					printf("%s: request sense failed\n",
   1245      1.15   tsutsui 					    sc->sc_dev.dv_xname);
   1246      1.15   tsutsui 					xs->error = XS_DRIVER_STUFFUP;
   1247      1.15   tsutsui 					break;
   1248      1.15   tsutsui 				}
   1249       1.1   tsutsui 
   1250      1.15   tsutsui 				xs->error = XS_SENSE;
   1251      1.15   tsutsui 				return;
   1252       1.1   tsutsui 
   1253      1.15   tsutsui 			default:
   1254      1.15   tsutsui 				xs->error = XS_DRIVER_STUFFUP;
   1255      1.15   tsutsui 				break;
   1256      1.15   tsutsui 			}
   1257      1.15   tsutsui 			break;
   1258       1.1   tsutsui 
   1259      1.15   tsutsui 		case HOST_SEL_TOUT:
   1260      1.15   tsutsui 			xs->error = XS_SELTIMEOUT;
   1261      1.15   tsutsui 			break;
   1262       1.1   tsutsui 
   1263      1.15   tsutsui 		case HOST_SCSI_RST:
   1264      1.15   tsutsui 		case HOST_DEV_RST:
   1265      1.15   tsutsui 			xs->error = XS_RESET;
   1266       1.1   tsutsui 			break;
   1267       1.1   tsutsui 
   1268      1.15   tsutsui 		case HOST_SPERR:
   1269      1.15   tsutsui 			printf("%s: SCSI Parity error detected\n",
   1270      1.15   tsutsui 			    sc->sc_dev.dv_xname);
   1271      1.15   tsutsui 			xs->error = XS_DRIVER_STUFFUP;
   1272       1.1   tsutsui 			break;
   1273       1.1   tsutsui 
   1274      1.15   tsutsui 		case HOST_TIMED_OUT:
   1275      1.15   tsutsui 			xs->error = XS_TIMEOUT;
   1276       1.1   tsutsui 			break;
   1277       1.1   tsutsui 
   1278      1.15   tsutsui 		case HOST_DO_DU:
   1279      1.15   tsutsui 		case HOST_BAD_PHAS:
   1280       1.1   tsutsui 		default:
   1281      1.15   tsutsui 			xs->error = XS_DRIVER_STUFFUP;
   1282      1.15   tsutsui 			break;
   1283       1.1   tsutsui 		}
   1284      1.15   tsutsui 
   1285      1.15   tsutsui 		scsipi_done(xs);
   1286       1.1   tsutsui 	}
   1287      1.15   tsutsui 
   1288      1.15   tsutsui 	iha_append_free_scb(sc, scb);
   1289       1.1   tsutsui }
   1290       1.1   tsutsui 
   1291       1.1   tsutsui /*
   1292      1.15   tsutsui  * iha_push_sense_request - obtain auto sense data by pushing the
   1293      1.15   tsutsui  *			    SCB needing it back onto the pending
   1294      1.15   tsutsui  *			    queue with a REQUEST_SENSE CDB.
   1295       1.1   tsutsui  */
   1296       1.1   tsutsui static int
   1297      1.25   tsutsui iha_push_sense_request(struct iha_softc *sc, struct iha_scb *scb)
   1298       1.1   tsutsui {
   1299      1.15   tsutsui 	struct scsipi_xfer *xs = scb->xs;
   1300      1.15   tsutsui 	struct scsipi_periph *periph = xs->xs_periph;
   1301      1.28   thorpej 	struct scsi_request_sense *ss = (struct scsi_request_sense *)scb->cmd;
   1302      1.15   tsutsui 	int lun = periph->periph_lun;
   1303      1.15   tsutsui 	int err;
   1304      1.15   tsutsui 
   1305      1.28   thorpej 	memset(ss, 0, sizeof(*ss));
   1306      1.28   thorpej 	ss->opcode = SCSI_REQUEST_SENSE;
   1307      1.15   tsutsui 	ss->byte2 = lun << SCSI_CMD_LUN_SHIFT;
   1308      1.28   thorpej 	ss->length = sizeof(struct scsi_sense_data);
   1309      1.15   tsutsui 
   1310      1.15   tsutsui 	scb->flags = FLAG_RSENS | FLAG_DATAIN;
   1311      1.15   tsutsui 
   1312      1.15   tsutsui 	scb->scb_id &= ~MSG_IDENTIFY_DISCFLAG;
   1313      1.15   tsutsui 
   1314      1.15   tsutsui 	scb->scb_tagmsg = 0;
   1315      1.15   tsutsui 	scb->ta_stat = SCSI_OK;
   1316       1.1   tsutsui 
   1317      1.28   thorpej 	scb->cmdlen = sizeof(struct scsi_request_sense);
   1318      1.15   tsutsui 	scb->buflen = ss->length;
   1319      1.15   tsutsui 
   1320      1.15   tsutsui 	err = bus_dmamap_load(sc->sc_dmat, scb->dmap,
   1321      1.15   tsutsui 	    &xs->sense.scsi_sense, scb->buflen, NULL,
   1322      1.15   tsutsui 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
   1323      1.15   tsutsui 	if (err != 0) {
   1324      1.15   tsutsui 		printf("iha_push_sense_request: cannot bus_dmamap_load()\n");
   1325      1.15   tsutsui 		xs->error = XS_DRIVER_STUFFUP;
   1326      1.15   tsutsui 		return 1;
   1327      1.15   tsutsui 	}
   1328      1.15   tsutsui 	bus_dmamap_sync(sc->sc_dmat, scb->dmap,
   1329      1.15   tsutsui 	    0, scb->buflen, BUS_DMASYNC_PREREAD);
   1330       1.1   tsutsui 
   1331      1.15   tsutsui 	/* XXX What about queued command? */
   1332      1.15   tsutsui 	iha_exec_scb(sc, scb);
   1333       1.1   tsutsui 
   1334      1.15   tsutsui 	return 0;
   1335      1.15   tsutsui }
   1336       1.1   tsutsui 
   1337      1.15   tsutsui static void
   1338      1.25   tsutsui iha_timeout(void *arg)
   1339      1.15   tsutsui {
   1340      1.15   tsutsui 	struct iha_scb *scb = (struct iha_scb *)arg;
   1341      1.15   tsutsui 	struct scsipi_xfer *xs = scb->xs;
   1342  1.31.8.1      yamt 	struct scsipi_periph *periph;
   1343      1.15   tsutsui 	struct iha_softc *sc;
   1344       1.1   tsutsui 
   1345  1.31.8.1      yamt 	if (xs == NULL) {
   1346      1.15   tsutsui 		printf("[debug] iha_timeout called with xs == NULL\n");
   1347  1.31.8.1      yamt 		return;
   1348  1.31.8.1      yamt 	}
   1349       1.1   tsutsui 
   1350  1.31.8.1      yamt 	periph = xs->xs_periph;
   1351       1.1   tsutsui 
   1352  1.31.8.1      yamt 	sc = (void *)periph->periph_channel->chan_adapter->adapt_dev;
   1353  1.31.8.1      yamt 
   1354  1.31.8.1      yamt 	scsipi_printaddr(periph);
   1355  1.31.8.1      yamt 	printf("SCSI OpCode 0x%02x timed out\n", xs->cmd->opcode);
   1356  1.31.8.1      yamt 	iha_abort_xs(sc, xs, HOST_TIMED_OUT);
   1357       1.1   tsutsui }
   1358       1.1   tsutsui 
   1359       1.1   tsutsui /*
   1360      1.15   tsutsui  * iha_abort_xs - find the SCB associated with the supplied xs and
   1361      1.15   tsutsui  *                stop all processing on it, moving it to the done
   1362      1.15   tsutsui  *                queue with the supplied host status value.
   1363       1.1   tsutsui  */
   1364      1.15   tsutsui static void
   1365      1.27   tsutsui iha_abort_xs(struct iha_softc *sc, struct scsipi_xfer *xs, uint8_t hastat)
   1366       1.1   tsutsui {
   1367      1.15   tsutsui 	struct iha_scb *scb;
   1368      1.15   tsutsui 	int i, s;
   1369      1.15   tsutsui 
   1370      1.15   tsutsui 	s = splbio();
   1371      1.15   tsutsui 
   1372      1.15   tsutsui 	/* Check the pending queue for the SCB pointing to xs */
   1373       1.1   tsutsui 
   1374      1.15   tsutsui 	TAILQ_FOREACH(scb, &sc->sc_pendscb, chain)
   1375      1.15   tsutsui 		if (scb->xs == xs) {
   1376      1.15   tsutsui 			iha_del_pend_scb(sc, scb);
   1377      1.15   tsutsui 			iha_append_done_scb(sc, scb, hastat);
   1378      1.15   tsutsui 			splx(s);
   1379      1.15   tsutsui 			return;
   1380      1.15   tsutsui 		}
   1381       1.1   tsutsui 
   1382       1.1   tsutsui 	/*
   1383      1.15   tsutsui 	 * If that didn't work, check all BUSY/SELECTING SCB's for one
   1384      1.15   tsutsui 	 * pointing to xs
   1385       1.1   tsutsui 	 */
   1386       1.1   tsutsui 
   1387      1.15   tsutsui 	for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++)
   1388      1.15   tsutsui 		switch (scb->status) {
   1389      1.15   tsutsui 		case STATUS_BUSY:
   1390      1.15   tsutsui 		case STATUS_SELECT:
   1391      1.15   tsutsui 			if (scb->xs == xs) {
   1392      1.15   tsutsui 				iha_append_done_scb(sc, scb, hastat);
   1393      1.15   tsutsui 				splx(s);
   1394      1.15   tsutsui 				return;
   1395      1.15   tsutsui 			}
   1396      1.15   tsutsui 			break;
   1397      1.15   tsutsui 		default:
   1398      1.15   tsutsui 			break;
   1399       1.1   tsutsui 		}
   1400       1.1   tsutsui 
   1401      1.15   tsutsui 	splx(s);
   1402      1.15   tsutsui }
   1403       1.1   tsutsui 
   1404      1.15   tsutsui /*
   1405      1.15   tsutsui  * iha_data_over_run - return HOST_OK for all SCSI opcodes where BufLen
   1406      1.15   tsutsui  *		       is an 'Allocation Length'. All other SCSI opcodes
   1407      1.15   tsutsui  *		       get HOST_DO_DU as they SHOULD have xferred all the
   1408      1.15   tsutsui  *		       data requested.
   1409      1.15   tsutsui  *
   1410      1.15   tsutsui  *		       The list of opcodes using 'Allocation Length' was
   1411      1.15   tsutsui  *		       found by scanning all the SCSI-3 T10 drafts. See
   1412      1.15   tsutsui  *		       www.t10.org for the curious with a .pdf reader.
   1413      1.15   tsutsui  */
   1414      1.27   tsutsui static uint8_t
   1415      1.25   tsutsui iha_data_over_run(struct iha_scb *scb)
   1416      1.15   tsutsui {
   1417      1.15   tsutsui 	switch (scb->cmd[0]) {
   1418      1.15   tsutsui 	case 0x03: /* Request Sense                   SPC-2 */
   1419      1.15   tsutsui 	case 0x12: /* Inquiry                         SPC-2 */
   1420      1.15   tsutsui 	case 0x1a: /* Mode Sense (6 byte version)     SPC-2 */
   1421      1.15   tsutsui 	case 0x1c: /* Receive Diagnostic Results      SPC-2 */
   1422      1.15   tsutsui 	case 0x23: /* Read Format Capacities          MMC-2 */
   1423      1.15   tsutsui 	case 0x29: /* Read Generation                 SBC   */
   1424      1.15   tsutsui 	case 0x34: /* Read Position                   SSC-2 */
   1425      1.15   tsutsui 	case 0x37: /* Read Defect Data                SBC   */
   1426      1.15   tsutsui 	case 0x3c: /* Read Buffer                     SPC-2 */
   1427      1.15   tsutsui 	case 0x42: /* Read Sub Channel                MMC-2 */
   1428      1.15   tsutsui 	case 0x43: /* Read TOC/PMA/ATIP               MMC   */
   1429       1.1   tsutsui 
   1430      1.15   tsutsui 	/* XXX - 2 with same opcode of 0x44? */
   1431      1.15   tsutsui 	case 0x44: /* Read Header/Read Density Suprt  MMC/SSC*/
   1432       1.1   tsutsui 
   1433      1.15   tsutsui 	case 0x46: /* Get Configuration               MMC-2 */
   1434      1.15   tsutsui 	case 0x4a: /* Get Event/Status Notification   MMC-2 */
   1435      1.15   tsutsui 	case 0x4d: /* Log Sense                       SPC-2 */
   1436      1.15   tsutsui 	case 0x51: /* Read Disc Information           MMC   */
   1437      1.15   tsutsui 	case 0x52: /* Read Track Information          MMC   */
   1438      1.15   tsutsui 	case 0x59: /* Read Master CUE                 MMC   */
   1439      1.15   tsutsui 	case 0x5a: /* Mode Sense (10 byte version)    SPC-2 */
   1440      1.15   tsutsui 	case 0x5c: /* Read Buffer Capacity            MMC   */
   1441      1.24       wiz 	case 0x5e: /* Persistent Reserve In           SPC-2 */
   1442      1.15   tsutsui 	case 0x84: /* Receive Copy Results            SPC-2 */
   1443      1.15   tsutsui 	case 0xa0: /* Report LUNs                     SPC-2 */
   1444      1.15   tsutsui 	case 0xa3: /* Various Report requests         SBC-2/SCC-2*/
   1445      1.15   tsutsui 	case 0xa4: /* Report Key                      MMC-2 */
   1446      1.15   tsutsui 	case 0xad: /* Read DVD Structure              MMC-2 */
   1447      1.15   tsutsui 	case 0xb4: /* Read Element Status (Attached)  SMC   */
   1448      1.15   tsutsui 	case 0xb5: /* Request Volume Element Address  SMC   */
   1449      1.15   tsutsui 	case 0xb7: /* Read Defect Data (12 byte ver.) SBC   */
   1450      1.15   tsutsui 	case 0xb8: /* Read Element Status (Independ.) SMC   */
   1451      1.15   tsutsui 	case 0xba: /* Report Redundancy               SCC-2 */
   1452      1.15   tsutsui 	case 0xbd: /* Mechanism Status                MMC   */
   1453      1.15   tsutsui 	case 0xbe: /* Report Basic Redundancy         SCC-2 */
   1454       1.1   tsutsui 
   1455      1.15   tsutsui 		return (HOST_OK);
   1456       1.1   tsutsui 
   1457      1.15   tsutsui 	default:
   1458      1.15   tsutsui 		return (HOST_DO_DU);
   1459      1.15   tsutsui 	}
   1460       1.1   tsutsui }
   1461       1.1   tsutsui 
   1462       1.1   tsutsui /*
   1463      1.24       wiz  * iha_next_state - process the current SCB as requested in its
   1464      1.15   tsutsui  *                  nextstat member.
   1465       1.1   tsutsui  */
   1466       1.1   tsutsui static int
   1467      1.25   tsutsui iha_next_state(struct iha_softc *sc)
   1468       1.1   tsutsui {
   1469       1.1   tsutsui 
   1470      1.15   tsutsui 	if (sc->sc_actscb == NULL)
   1471      1.15   tsutsui 		return (-1);
   1472       1.1   tsutsui 
   1473      1.15   tsutsui 	switch (sc->sc_actscb->nextstat) {
   1474      1.15   tsutsui 	case 1:
   1475      1.15   tsutsui 		if (iha_state_1(sc) == 3)
   1476      1.15   tsutsui 			goto state_3;
   1477      1.15   tsutsui 		break;
   1478       1.1   tsutsui 
   1479      1.15   tsutsui 	case 2:
   1480      1.15   tsutsui 		switch (iha_state_2(sc)) {
   1481      1.15   tsutsui 		case 3:
   1482      1.15   tsutsui 			goto state_3;
   1483      1.15   tsutsui 		case 4:
   1484      1.15   tsutsui 			goto state_4;
   1485      1.15   tsutsui 		default:
   1486       1.1   tsutsui 			break;
   1487      1.15   tsutsui 		}
   1488      1.15   tsutsui 		break;
   1489       1.1   tsutsui 
   1490      1.15   tsutsui 	case 3:
   1491      1.15   tsutsui 	state_3:
   1492      1.15   tsutsui 		if (iha_state_3(sc) == 4)
   1493      1.15   tsutsui 			goto state_4;
   1494      1.15   tsutsui 		break;
   1495       1.1   tsutsui 
   1496      1.15   tsutsui 	case 4:
   1497      1.15   tsutsui 	state_4:
   1498      1.15   tsutsui 		switch (iha_state_4(sc)) {
   1499      1.15   tsutsui 		case 0:
   1500      1.15   tsutsui 			return (0);
   1501      1.15   tsutsui 		case 6:
   1502      1.15   tsutsui 			goto state_6;
   1503      1.15   tsutsui 		default:
   1504       1.1   tsutsui 			break;
   1505      1.15   tsutsui 		}
   1506      1.15   tsutsui 		break;
   1507       1.1   tsutsui 
   1508      1.15   tsutsui 	case 5:
   1509      1.15   tsutsui 		switch (iha_state_5(sc)) {
   1510      1.15   tsutsui 		case 4:
   1511      1.15   tsutsui 			goto state_4;
   1512      1.15   tsutsui 		case 6:
   1513      1.15   tsutsui 			goto state_6;
   1514       1.1   tsutsui 		default:
   1515      1.15   tsutsui 			break;
   1516       1.1   tsutsui 		}
   1517      1.15   tsutsui 		break;
   1518      1.15   tsutsui 
   1519      1.15   tsutsui 	case 6:
   1520      1.15   tsutsui 	state_6:
   1521      1.15   tsutsui 		iha_state_6(sc);
   1522      1.15   tsutsui 		break;
   1523      1.15   tsutsui 
   1524      1.15   tsutsui 	case 8:
   1525      1.15   tsutsui 		iha_state_8(sc);
   1526      1.15   tsutsui 		break;
   1527      1.15   tsutsui 
   1528      1.15   tsutsui 	default:
   1529      1.15   tsutsui #ifdef IHA_DEBUG_STATE
   1530      1.15   tsutsui 		printf("[debug] -unknown state: %i-\n",
   1531      1.15   tsutsui 		    sc->sc_actscb->nextstat);
   1532      1.15   tsutsui #endif
   1533      1.15   tsutsui 		iha_bad_seq(sc);
   1534      1.15   tsutsui 		break;
   1535       1.1   tsutsui 	}
   1536      1.15   tsutsui 
   1537      1.15   tsutsui 	return (-1);
   1538       1.1   tsutsui }
   1539       1.1   tsutsui 
   1540       1.1   tsutsui /*
   1541      1.15   tsutsui  * iha_state_1 - selection is complete after a SELATNSTOP. If the target
   1542      1.15   tsutsui  *               has put the bus into MSG_OUT phase start wide/sync
   1543      1.15   tsutsui  *               negotiation. Otherwise clear the FIFO and go to state 3,
   1544      1.15   tsutsui  *	    	 which will send the SCSI CDB to the target.
   1545       1.1   tsutsui  */
   1546       1.1   tsutsui static int
   1547      1.25   tsutsui iha_state_1(struct iha_softc *sc)
   1548       1.1   tsutsui {
   1549       1.1   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1550       1.1   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1551      1.15   tsutsui 	struct iha_scb *scb = sc->sc_actscb;
   1552      1.15   tsutsui 	struct tcs *tcs;
   1553      1.15   tsutsui 	int flags;
   1554       1.1   tsutsui 
   1555      1.15   tsutsui 	iha_mark_busy_scb(scb);
   1556       1.1   tsutsui 
   1557      1.15   tsutsui 	tcs = scb->tcs;
   1558       1.1   tsutsui 
   1559      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
   1560       1.1   tsutsui 
   1561      1.15   tsutsui 	/*
   1562      1.15   tsutsui 	 * If we are in PHASE_MSG_OUT, send
   1563      1.15   tsutsui 	 *     a) IDENT message (with tags if appropriate)
   1564      1.15   tsutsui 	 *     b) WDTR if the target is configured to negotiate wide xfers
   1565      1.15   tsutsui 	 *     ** OR **
   1566      1.15   tsutsui 	 *     c) SDTR if the target is configured to negotiate sync xfers
   1567      1.15   tsutsui 	 *	  but not wide ones
   1568      1.15   tsutsui 	 *
   1569      1.15   tsutsui 	 * If we are NOT, then the target is not asking for anything but
   1570      1.15   tsutsui 	 * the data/command, so go straight to state 3.
   1571      1.15   tsutsui 	 */
   1572      1.15   tsutsui 	if (sc->sc_phase == PHASE_MSG_OUT) {
   1573      1.15   tsutsui 		bus_space_write_1(iot, ioh, TUL_SCTRL1, (ESBUSIN | EHRSL));
   1574      1.15   tsutsui 		bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_id);
   1575       1.1   tsutsui 
   1576      1.15   tsutsui 		if (scb->scb_tagmsg != 0) {
   1577      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_SFIFO,
   1578      1.15   tsutsui 			    scb->scb_tagmsg);
   1579      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_SFIFO,
   1580      1.15   tsutsui 			    scb->scb_tagid);
   1581      1.15   tsutsui 		}
   1582       1.1   tsutsui 
   1583      1.15   tsutsui 		flags = tcs->flags;
   1584      1.15   tsutsui 		if ((flags & FLAG_NO_NEG_WIDE) == 0) {
   1585      1.15   tsutsui 			if (iha_msgout_wdtr(sc) == -1)
   1586      1.15   tsutsui 				return (-1);
   1587      1.15   tsutsui 		} else if ((flags & FLAG_NO_NEG_SYNC) == 0) {
   1588      1.15   tsutsui 			if (iha_msgout_sdtr(sc) == -1)
   1589      1.15   tsutsui 				return (-1);
   1590      1.15   tsutsui 		}
   1591       1.1   tsutsui 
   1592      1.15   tsutsui 	} else {
   1593      1.15   tsutsui 		bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1594      1.15   tsutsui 		iha_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
   1595       1.1   tsutsui 	}
   1596       1.1   tsutsui 
   1597      1.15   tsutsui 	return (3);
   1598       1.1   tsutsui }
   1599       1.1   tsutsui 
   1600       1.1   tsutsui /*
   1601      1.15   tsutsui  * iha_state_2 - selection is complete after a SEL_ATN or SEL_ATN3. If the SCSI
   1602      1.15   tsutsui  *		 CDB has already been send, go to state 4 to start the data
   1603      1.15   tsutsui  *		 xfer. Otherwise reset the FIFO and go to state 3, sending
   1604      1.15   tsutsui  *		 the SCSI CDB.
   1605       1.1   tsutsui  */
   1606       1.1   tsutsui static int
   1607      1.25   tsutsui iha_state_2(struct iha_softc *sc)
   1608       1.1   tsutsui {
   1609       1.1   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1610       1.1   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1611      1.15   tsutsui 	struct iha_scb *scb = sc->sc_actscb;
   1612       1.1   tsutsui 
   1613      1.15   tsutsui 	iha_mark_busy_scb(scb);
   1614       1.1   tsutsui 
   1615      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_SCONFIG0, scb->tcs->sconfig0);
   1616       1.1   tsutsui 
   1617      1.15   tsutsui 	if ((sc->sc_status1 & CPDNE) != 0)
   1618      1.15   tsutsui 		return (4);
   1619       1.1   tsutsui 
   1620      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1621       1.1   tsutsui 
   1622      1.15   tsutsui 	iha_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
   1623       1.1   tsutsui 
   1624      1.15   tsutsui 	return (3);
   1625       1.1   tsutsui }
   1626       1.1   tsutsui 
   1627      1.15   tsutsui /*
   1628      1.15   tsutsui  * iha_state_3 - send the SCSI CDB to the target, processing any status
   1629      1.15   tsutsui  *		 or other messages received until that is done or
   1630      1.15   tsutsui  *		 abandoned.
   1631      1.15   tsutsui  */
   1632       1.1   tsutsui static int
   1633      1.25   tsutsui iha_state_3(struct iha_softc *sc)
   1634       1.1   tsutsui {
   1635       1.1   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1636       1.1   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1637      1.15   tsutsui 	struct iha_scb *scb = sc->sc_actscb;
   1638      1.15   tsutsui 	int flags;
   1639       1.1   tsutsui 
   1640      1.15   tsutsui 	for (;;) {
   1641      1.15   tsutsui 		switch (sc->sc_phase) {
   1642      1.15   tsutsui 		case PHASE_CMD_OUT:
   1643      1.15   tsutsui 			bus_space_write_multi_1(iot, ioh, TUL_SFIFO,
   1644      1.15   tsutsui 			    scb->cmd, scb->cmdlen);
   1645      1.15   tsutsui 			if (iha_wait(sc, XF_FIFO_OUT) == -1)
   1646      1.15   tsutsui 				return (-1);
   1647      1.15   tsutsui 			else if (sc->sc_phase == PHASE_CMD_OUT) {
   1648      1.15   tsutsui 				iha_bad_seq(sc);
   1649      1.15   tsutsui 				return (-1);
   1650      1.15   tsutsui 			} else
   1651      1.15   tsutsui 				return (4);
   1652       1.1   tsutsui 
   1653      1.15   tsutsui 		case PHASE_MSG_IN:
   1654      1.15   tsutsui 			scb->nextstat = 3;
   1655      1.15   tsutsui 			if (iha_msgin(sc) == -1)
   1656      1.15   tsutsui 				return (-1);
   1657      1.15   tsutsui 			break;
   1658       1.1   tsutsui 
   1659      1.15   tsutsui 		case PHASE_STATUS_IN:
   1660      1.15   tsutsui 			if (iha_status_msg(sc) == -1)
   1661      1.15   tsutsui 				return (-1);
   1662      1.15   tsutsui 			break;
   1663       1.1   tsutsui 
   1664      1.15   tsutsui 		case PHASE_MSG_OUT:
   1665      1.15   tsutsui 			flags = scb->tcs->flags;
   1666      1.15   tsutsui 			if ((flags & FLAG_NO_NEG_SYNC) != 0) {
   1667      1.15   tsutsui 				if (iha_msgout(sc, MSG_NOOP) == -1)
   1668      1.15   tsutsui 					return (-1);
   1669      1.15   tsutsui 			} else if (iha_msgout_sdtr(sc) == -1)
   1670      1.15   tsutsui 				return (-1);
   1671       1.1   tsutsui 			break;
   1672       1.1   tsutsui 
   1673       1.1   tsutsui 		default:
   1674      1.15   tsutsui 			printf("[debug] -s3- bad phase = %d\n", sc->sc_phase);
   1675      1.15   tsutsui 			iha_bad_seq(sc);
   1676      1.15   tsutsui 			return (-1);
   1677       1.1   tsutsui 		}
   1678       1.1   tsutsui 	}
   1679       1.1   tsutsui }
   1680       1.1   tsutsui 
   1681      1.15   tsutsui /*
   1682      1.15   tsutsui  * iha_state_4 - start a data xfer. Handle any bus state
   1683      1.15   tsutsui  *               transitions until PHASE_DATA_IN/_OUT
   1684      1.15   tsutsui  *               or the attempt is abandoned. If there is
   1685      1.15   tsutsui  *               no data to xfer, go to state 6 and finish
   1686      1.15   tsutsui  *               processing the current SCB.
   1687      1.15   tsutsui  */
   1688       1.1   tsutsui static int
   1689      1.25   tsutsui iha_state_4(struct iha_softc *sc)
   1690       1.1   tsutsui {
   1691      1.15   tsutsui 	struct iha_scb *scb = sc->sc_actscb;
   1692       1.1   tsutsui 
   1693      1.15   tsutsui 	if ((scb->flags & (FLAG_DATAIN | FLAG_DATAOUT)) ==
   1694      1.15   tsutsui 	    (FLAG_DATAIN | FLAG_DATAOUT))
   1695      1.15   tsutsui 		return (6); /* Both dir flags set => NO xfer was requested */
   1696       1.1   tsutsui 
   1697       1.1   tsutsui 	for (;;) {
   1698      1.15   tsutsui 		if (scb->buflen == 0)
   1699      1.15   tsutsui 			return (6);
   1700      1.15   tsutsui 
   1701      1.15   tsutsui 		switch (sc->sc_phase) {
   1702      1.15   tsutsui 		case PHASE_STATUS_IN:
   1703      1.15   tsutsui 			if ((scb->flags & (FLAG_DATAIN | FLAG_DATAOUT)) != 0)
   1704      1.15   tsutsui 				scb->ha_stat = iha_data_over_run(scb);
   1705      1.15   tsutsui 			if ((iha_status_msg(sc)) == -1)
   1706      1.15   tsutsui 				return (-1);
   1707      1.15   tsutsui 			break;
   1708      1.15   tsutsui 
   1709      1.15   tsutsui 		case PHASE_MSG_IN:
   1710      1.15   tsutsui 			scb->nextstat = 4;
   1711      1.15   tsutsui 			if (iha_msgin(sc) == -1)
   1712      1.15   tsutsui 				return (-1);
   1713      1.15   tsutsui 			break;
   1714       1.1   tsutsui 
   1715      1.15   tsutsui 		case PHASE_MSG_OUT:
   1716      1.15   tsutsui 			if ((sc->sc_status0 & SPERR) != 0) {
   1717      1.15   tsutsui 				scb->buflen = 0;
   1718      1.15   tsutsui 				scb->ha_stat = HOST_SPERR;
   1719      1.15   tsutsui 				if (iha_msgout(sc, MSG_INITIATOR_DET_ERR) == -1)
   1720      1.15   tsutsui 					return (-1);
   1721      1.15   tsutsui 				else
   1722      1.15   tsutsui 					return (6);
   1723      1.15   tsutsui 			} else {
   1724      1.15   tsutsui 				if (iha_msgout(sc, MSG_NOOP) == -1)
   1725      1.15   tsutsui 					return (-1);
   1726      1.15   tsutsui 			}
   1727      1.15   tsutsui 			break;
   1728       1.1   tsutsui 
   1729      1.15   tsutsui 		case PHASE_DATA_IN:
   1730      1.15   tsutsui 			return (iha_xfer_data(sc, scb, FLAG_DATAIN));
   1731       1.1   tsutsui 
   1732       1.1   tsutsui 		case PHASE_DATA_OUT:
   1733      1.15   tsutsui 			return (iha_xfer_data(sc, scb, FLAG_DATAOUT));
   1734       1.1   tsutsui 
   1735       1.1   tsutsui 		default:
   1736      1.15   tsutsui 			iha_bad_seq(sc);
   1737      1.15   tsutsui 			return (-1);
   1738       1.1   tsutsui 		}
   1739       1.1   tsutsui 	}
   1740       1.1   tsutsui }
   1741       1.1   tsutsui 
   1742      1.15   tsutsui /*
   1743      1.15   tsutsui  * iha_state_5 - handle the partial or final completion of the current
   1744      1.15   tsutsui  *		 data xfer. If DMA is still active stop it. If there is
   1745      1.15   tsutsui  *		 more data to xfer, go to state 4 and start the xfer.
   1746      1.15   tsutsui  *		 If not go to state 6 and finish the SCB.
   1747      1.15   tsutsui  */
   1748       1.1   tsutsui static int
   1749      1.25   tsutsui iha_state_5(struct iha_softc *sc)
   1750       1.1   tsutsui {
   1751       1.1   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1752       1.1   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1753      1.15   tsutsui 	struct iha_scb *scb = sc->sc_actscb;
   1754      1.15   tsutsui 	struct iha_sg_element *sg;
   1755      1.27   tsutsui 	uint32_t cnt;
   1756      1.27   tsutsui 	uint8_t period, stat;
   1757      1.15   tsutsui 	long xcnt;  /* cannot use unsigned!! see code: if (xcnt < 0) */
   1758      1.15   tsutsui 	int i;
   1759       1.1   tsutsui 
   1760      1.15   tsutsui 	cnt = bus_space_read_4(iot, ioh, TUL_STCNT0) & TCNT;
   1761       1.1   tsutsui 
   1762      1.15   tsutsui 	/*
   1763      1.15   tsutsui 	 * Stop any pending DMA activity and check for parity error.
   1764      1.15   tsutsui 	 */
   1765       1.1   tsutsui 
   1766      1.15   tsutsui 	if ((bus_space_read_1(iot, ioh, TUL_DCMD) & XDIR) != 0) {
   1767      1.15   tsutsui 		/* Input Operation */
   1768      1.15   tsutsui 		if ((sc->sc_status0 & SPERR) != 0)
   1769      1.15   tsutsui 			scb->ha_stat = HOST_SPERR;
   1770       1.1   tsutsui 
   1771      1.15   tsutsui 		if ((bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND) != 0) {
   1772      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_DCTRL0,
   1773      1.15   tsutsui 			    bus_space_read_1(iot, ioh, TUL_DCTRL0) | SXSTP);
   1774      1.15   tsutsui 			while (bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND)
   1775      1.15   tsutsui 				;
   1776      1.15   tsutsui 		}
   1777       1.1   tsutsui 
   1778      1.15   tsutsui 	} else {
   1779      1.15   tsutsui 		/* Output Operation */
   1780      1.15   tsutsui 		if ((sc->sc_status1 & SXCMP) == 0) {
   1781      1.15   tsutsui 			period = scb->tcs->syncm;
   1782      1.15   tsutsui 			if ((period & PERIOD_WIDE_SCSI) != 0)
   1783      1.15   tsutsui 				cnt += (bus_space_read_1(iot, ioh,
   1784      1.15   tsutsui 				    TUL_SFIFOCNT) & FIFOC) * 2;
   1785      1.15   tsutsui 			else
   1786      1.15   tsutsui 				cnt += bus_space_read_1(iot, ioh,
   1787      1.15   tsutsui 				    TUL_SFIFOCNT) & FIFOC;
   1788      1.15   tsutsui 		}
   1789       1.1   tsutsui 
   1790      1.15   tsutsui 		if ((bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND) != 0) {
   1791      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_DCMD, ABTXFR);
   1792      1.15   tsutsui 			do
   1793      1.15   tsutsui 				stat = bus_space_read_1(iot, ioh, TUL_ISTUS0);
   1794      1.15   tsutsui 			while ((stat & DABT) == 0);
   1795      1.15   tsutsui 		}
   1796       1.1   tsutsui 
   1797      1.15   tsutsui 		if ((cnt == 1) && (sc->sc_phase == PHASE_DATA_OUT)) {
   1798      1.15   tsutsui 			if (iha_wait(sc, XF_FIFO_OUT) == -1)
   1799       1.1   tsutsui 				return (-1);
   1800      1.15   tsutsui 			cnt = 0;
   1801      1.15   tsutsui 
   1802      1.15   tsutsui 		} else if ((sc->sc_status1 & SXCMP) == 0)
   1803      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   1804      1.15   tsutsui 	}
   1805      1.15   tsutsui 
   1806      1.15   tsutsui 	if (cnt == 0) {
   1807      1.15   tsutsui 		scb->buflen = 0;
   1808      1.15   tsutsui 		return (6);
   1809      1.15   tsutsui 	}
   1810      1.15   tsutsui 
   1811      1.15   tsutsui 	/* Update active data pointer and restart the I/O at the new point */
   1812      1.15   tsutsui 
   1813      1.15   tsutsui 	xcnt = scb->buflen - cnt;	/* xcnt == bytes xferred */
   1814      1.15   tsutsui 	scb->buflen = cnt;	  	/* cnt  == bytes left    */
   1815      1.15   tsutsui 
   1816      1.15   tsutsui 	if ((scb->flags & FLAG_SG) != 0) {
   1817      1.15   tsutsui 		sg = &scb->sglist[scb->sg_index];
   1818      1.15   tsutsui 		for (i = scb->sg_index; i < scb->sg_max; sg++, i++) {
   1819      1.15   tsutsui 			xcnt -= le32toh(sg->sg_len);
   1820      1.15   tsutsui 			if (xcnt < 0) {
   1821      1.15   tsutsui 				xcnt += le32toh(sg->sg_len);
   1822      1.15   tsutsui 
   1823      1.15   tsutsui 				sg->sg_addr =
   1824      1.15   tsutsui 				    htole32(le32toh(sg->sg_addr) + xcnt);
   1825      1.15   tsutsui 				sg->sg_len =
   1826      1.15   tsutsui 				    htole32(le32toh(sg->sg_len) - xcnt);
   1827      1.15   tsutsui 				bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   1828      1.15   tsutsui 				    scb->sgoffset, IHA_SG_SIZE,
   1829      1.15   tsutsui 				    BUS_DMASYNC_PREWRITE);
   1830      1.15   tsutsui 
   1831      1.15   tsutsui 				scb->bufaddr += (i - scb->sg_index) *
   1832      1.15   tsutsui 				    sizeof(struct iha_sg_element);
   1833      1.15   tsutsui 				scb->sg_size = scb->sg_max - i;
   1834      1.15   tsutsui 				scb->sg_index = i;
   1835       1.1   tsutsui 
   1836      1.15   tsutsui 				return (4);
   1837       1.1   tsutsui 			}
   1838       1.1   tsutsui 		}
   1839      1.15   tsutsui 		return (6);
   1840       1.1   tsutsui 
   1841      1.15   tsutsui 	} else
   1842      1.15   tsutsui 		scb->bufaddr += xcnt;
   1843       1.1   tsutsui 
   1844      1.15   tsutsui 	return (4);
   1845       1.1   tsutsui }
   1846       1.1   tsutsui 
   1847       1.1   tsutsui /*
   1848      1.15   tsutsui  * iha_state_6 - finish off the active scb (may require several
   1849      1.15   tsutsui  *		 iterations if PHASE_MSG_IN) and return -1 to indicate
   1850      1.15   tsutsui  *		 the bus is free.
   1851       1.1   tsutsui  */
   1852      1.15   tsutsui static int
   1853      1.25   tsutsui iha_state_6(struct iha_softc *sc)
   1854       1.1   tsutsui {
   1855       1.1   tsutsui 
   1856      1.15   tsutsui 	for (;;) {
   1857      1.15   tsutsui 		switch (sc->sc_phase) {
   1858      1.15   tsutsui 		case PHASE_STATUS_IN:
   1859      1.15   tsutsui 			if (iha_status_msg(sc) == -1)
   1860      1.15   tsutsui 				return (-1);
   1861      1.15   tsutsui 			break;
   1862       1.1   tsutsui 
   1863      1.15   tsutsui 		case PHASE_MSG_IN:
   1864      1.15   tsutsui 			sc->sc_actscb->nextstat = 6;
   1865      1.15   tsutsui 			if ((iha_msgin(sc)) == -1)
   1866      1.15   tsutsui 				return (-1);
   1867      1.15   tsutsui 			break;
   1868       1.1   tsutsui 
   1869      1.15   tsutsui 		case PHASE_MSG_OUT:
   1870      1.15   tsutsui 			if ((iha_msgout(sc, MSG_NOOP)) == -1)
   1871      1.15   tsutsui 				return (-1);
   1872      1.15   tsutsui 			break;
   1873       1.1   tsutsui 
   1874      1.15   tsutsui 		case PHASE_DATA_IN:
   1875      1.15   tsutsui 			if (iha_xpad_in(sc) == -1)
   1876      1.15   tsutsui 				return (-1);
   1877       1.1   tsutsui 			break;
   1878       1.1   tsutsui 
   1879      1.15   tsutsui 		case PHASE_DATA_OUT:
   1880      1.15   tsutsui 			if (iha_xpad_out(sc) == -1)
   1881      1.15   tsutsui 				return (-1);
   1882       1.1   tsutsui 			break;
   1883       1.1   tsutsui 
   1884       1.1   tsutsui 		default:
   1885      1.15   tsutsui 			iha_bad_seq(sc);
   1886      1.15   tsutsui 			return (-1);
   1887       1.1   tsutsui 		}
   1888      1.15   tsutsui 	}
   1889       1.1   tsutsui }
   1890       1.1   tsutsui 
   1891       1.1   tsutsui /*
   1892      1.15   tsutsui  * iha_state_8 - reset the active device and all busy SCBs using it
   1893       1.1   tsutsui  */
   1894       1.1   tsutsui static int
   1895      1.25   tsutsui iha_state_8(struct iha_softc *sc)
   1896       1.1   tsutsui {
   1897       1.1   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1898       1.1   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1899      1.15   tsutsui 	struct iha_scb *scb;
   1900      1.15   tsutsui 	int i;
   1901      1.27   tsutsui 	uint8_t tar;
   1902       1.1   tsutsui 
   1903      1.15   tsutsui 	if (sc->sc_phase == PHASE_MSG_OUT) {
   1904      1.15   tsutsui 		bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_BUS_DEV_RESET);
   1905       1.1   tsutsui 
   1906      1.15   tsutsui 		scb = sc->sc_actscb;
   1907       1.1   tsutsui 
   1908      1.15   tsutsui 		/* This SCB finished correctly -- resetting the device */
   1909      1.15   tsutsui 		iha_append_done_scb(sc, scb, HOST_OK);
   1910       1.1   tsutsui 
   1911      1.15   tsutsui 		iha_reset_tcs(scb->tcs, sc->sc_sconf1);
   1912       1.1   tsutsui 
   1913      1.15   tsutsui 		tar = scb->target;
   1914      1.15   tsutsui 		for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++)
   1915      1.15   tsutsui 			if (scb->target == tar)
   1916      1.15   tsutsui 				switch (scb->status) {
   1917      1.15   tsutsui 				case STATUS_BUSY:
   1918      1.15   tsutsui 					iha_append_done_scb(sc,
   1919      1.15   tsutsui 					    scb, HOST_DEV_RST);
   1920      1.15   tsutsui 					break;
   1921       1.1   tsutsui 
   1922      1.15   tsutsui 				case STATUS_SELECT:
   1923      1.15   tsutsui 					iha_push_pend_scb(sc, scb);
   1924      1.15   tsutsui 					break;
   1925       1.1   tsutsui 
   1926      1.15   tsutsui 				default:
   1927      1.15   tsutsui 					break;
   1928      1.15   tsutsui 				}
   1929       1.1   tsutsui 
   1930      1.15   tsutsui 		sc->sc_flags |= FLAG_EXPECT_DISC;
   1931       1.1   tsutsui 
   1932      1.15   tsutsui 		if (iha_wait(sc, XF_FIFO_OUT) == -1)
   1933       1.1   tsutsui 			return (-1);
   1934       1.1   tsutsui 	}
   1935       1.1   tsutsui 
   1936      1.15   tsutsui 	iha_bad_seq(sc);
   1937      1.15   tsutsui 	return (-1);
   1938       1.1   tsutsui }
   1939       1.1   tsutsui 
   1940      1.15   tsutsui /*
   1941      1.15   tsutsui  * iha_xfer_data - initiate the DMA xfer of the data
   1942      1.15   tsutsui  */
   1943       1.1   tsutsui static int
   1944      1.25   tsutsui iha_xfer_data(struct iha_softc *sc, struct iha_scb *scb, int direction)
   1945       1.1   tsutsui {
   1946       1.1   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1947       1.1   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1948      1.27   tsutsui 	uint32_t xferlen;
   1949      1.27   tsutsui 	uint8_t xfercmd;
   1950      1.15   tsutsui 
   1951      1.15   tsutsui 	if ((scb->flags & (FLAG_DATAIN | FLAG_DATAOUT)) != direction)
   1952      1.15   tsutsui 		return (6); /* wrong direction, abandon I/O */
   1953      1.15   tsutsui 
   1954      1.15   tsutsui 	bus_space_write_4(iot, ioh, TUL_STCNT0, scb->buflen);
   1955      1.15   tsutsui 
   1956      1.15   tsutsui 	xfercmd = STRXFR;
   1957      1.15   tsutsui 	if (direction == FLAG_DATAIN)
   1958      1.15   tsutsui 		xfercmd |= XDIR;
   1959       1.1   tsutsui 
   1960      1.15   tsutsui 	if (scb->flags & FLAG_SG) {
   1961      1.15   tsutsui 		xferlen = scb->sg_size * sizeof(struct iha_sg_element);
   1962      1.15   tsutsui 		xfercmd |= SGXFR;
   1963      1.15   tsutsui 	} else
   1964      1.15   tsutsui 		xferlen = scb->buflen;
   1965       1.1   tsutsui 
   1966      1.15   tsutsui 	bus_space_write_4(iot, ioh, TUL_DXC,  xferlen);
   1967      1.15   tsutsui 	bus_space_write_4(iot, ioh, TUL_DXPA, scb->bufaddr);
   1968      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_DCMD, xfercmd);
   1969       1.1   tsutsui 
   1970      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_SCMD,
   1971      1.15   tsutsui 	    (direction == FLAG_DATAIN) ? XF_DMA_IN : XF_DMA_OUT);
   1972       1.1   tsutsui 
   1973      1.15   tsutsui 	scb->nextstat = 5;
   1974       1.1   tsutsui 
   1975      1.15   tsutsui 	return (0);
   1976       1.1   tsutsui }
   1977       1.1   tsutsui 
   1978       1.1   tsutsui static int
   1979      1.25   tsutsui iha_xpad_in(struct iha_softc *sc)
   1980       1.1   tsutsui {
   1981       1.1   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   1982       1.1   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   1983      1.15   tsutsui 	struct iha_scb *scb = sc->sc_actscb;
   1984      1.15   tsutsui 
   1985      1.15   tsutsui 	if ((scb->flags & (FLAG_DATAIN | FLAG_DATAOUT)) != 0)
   1986      1.15   tsutsui 		scb->ha_stat = HOST_DO_DU;
   1987       1.1   tsutsui 
   1988      1.15   tsutsui 	for (;;) {
   1989      1.15   tsutsui 		if ((scb->tcs->syncm & PERIOD_WIDE_SCSI) != 0)
   1990      1.15   tsutsui 			bus_space_write_4(iot, ioh, TUL_STCNT0, 2);
   1991      1.15   tsutsui 		else
   1992      1.15   tsutsui 			bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
   1993       1.1   tsutsui 
   1994      1.15   tsutsui 		switch (iha_wait(sc, XF_FIFO_IN)) {
   1995      1.15   tsutsui 		case -1:
   1996      1.15   tsutsui 			return (-1);
   1997       1.1   tsutsui 
   1998      1.15   tsutsui 		case PHASE_DATA_IN:
   1999  1.31.8.2      yamt 			(void)bus_space_read_1(iot, ioh, TUL_SFIFO);
   2000      1.15   tsutsui 			break;
   2001       1.1   tsutsui 
   2002      1.15   tsutsui 		default:
   2003      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   2004      1.15   tsutsui 			return (6);
   2005       1.6   tsutsui 		}
   2006       1.1   tsutsui 	}
   2007       1.1   tsutsui }
   2008       1.1   tsutsui 
   2009       1.1   tsutsui static int
   2010      1.25   tsutsui iha_xpad_out(struct iha_softc *sc)
   2011       1.1   tsutsui {
   2012       1.1   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2013       1.1   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2014      1.15   tsutsui 	struct iha_scb *scb = sc->sc_actscb;
   2015       1.1   tsutsui 
   2016      1.15   tsutsui 	if ((scb->flags & (FLAG_DATAIN | FLAG_DATAOUT)) != 0)
   2017      1.15   tsutsui 		scb->ha_stat = HOST_DO_DU;
   2018       1.1   tsutsui 
   2019      1.15   tsutsui 	for (;;) {
   2020      1.15   tsutsui 		if ((scb->tcs->syncm & PERIOD_WIDE_SCSI) != 0)
   2021      1.15   tsutsui 			bus_space_write_4(iot, ioh, TUL_STCNT0, 2);
   2022      1.15   tsutsui 		else
   2023      1.15   tsutsui 			bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
   2024       1.1   tsutsui 
   2025      1.15   tsutsui 		bus_space_write_1(iot, ioh, TUL_SFIFO, 0);
   2026       1.1   tsutsui 
   2027      1.15   tsutsui 		switch (iha_wait(sc, XF_FIFO_OUT)) {
   2028      1.15   tsutsui 		case -1:
   2029       1.1   tsutsui 			return (-1);
   2030       1.1   tsutsui 
   2031      1.15   tsutsui 		case PHASE_DATA_OUT:
   2032       1.1   tsutsui 			break;
   2033       1.1   tsutsui 
   2034      1.15   tsutsui 		default:
   2035      1.15   tsutsui 			/* Disable wide CPU to allow read 16 bits */
   2036      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
   2037      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   2038      1.15   tsutsui 			return (6);
   2039       1.1   tsutsui 		}
   2040      1.15   tsutsui 	}
   2041      1.15   tsutsui }
   2042       1.1   tsutsui 
   2043      1.15   tsutsui static int
   2044      1.25   tsutsui iha_status_msg(struct iha_softc *sc)
   2045      1.15   tsutsui {
   2046      1.15   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2047      1.15   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2048      1.15   tsutsui 	struct iha_scb *scb;
   2049      1.27   tsutsui 	uint8_t msg;
   2050      1.15   tsutsui 	int phase;
   2051       1.1   tsutsui 
   2052      1.15   tsutsui 	if ((phase = iha_wait(sc, CMD_COMP)) == -1)
   2053      1.15   tsutsui 		return (-1);
   2054       1.1   tsutsui 
   2055      1.15   tsutsui 	scb = sc->sc_actscb;
   2056       1.1   tsutsui 
   2057      1.15   tsutsui 	scb->ta_stat = bus_space_read_1(iot, ioh, TUL_SFIFO);
   2058       1.1   tsutsui 
   2059      1.15   tsutsui 	if (phase == PHASE_MSG_OUT) {
   2060      1.15   tsutsui 		if ((sc->sc_status0 & SPERR) == 0)
   2061      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_NOOP);
   2062      1.15   tsutsui 		else
   2063      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_SFIFO,
   2064      1.15   tsutsui 			    MSG_PARITY_ERROR);
   2065       1.1   tsutsui 
   2066      1.15   tsutsui 		return (iha_wait(sc, XF_FIFO_OUT));
   2067       1.1   tsutsui 
   2068      1.15   tsutsui 	} else if (phase == PHASE_MSG_IN) {
   2069      1.15   tsutsui 		msg = bus_space_read_1(iot, ioh, TUL_SFIFO);
   2070       1.1   tsutsui 
   2071      1.15   tsutsui 		if ((sc->sc_status0 & SPERR) != 0)
   2072      1.15   tsutsui 			switch (iha_wait(sc, MSG_ACCEPT)) {
   2073      1.15   tsutsui 			case -1:
   2074      1.15   tsutsui 				return (-1);
   2075      1.15   tsutsui 			case PHASE_MSG_OUT:
   2076      1.15   tsutsui 				bus_space_write_1(iot, ioh, TUL_SFIFO,
   2077      1.15   tsutsui 				    MSG_PARITY_ERROR);
   2078      1.15   tsutsui 				return (iha_wait(sc, XF_FIFO_OUT));
   2079      1.15   tsutsui 			default:
   2080      1.15   tsutsui 				iha_bad_seq(sc);
   2081      1.15   tsutsui 				return (-1);
   2082      1.15   tsutsui 			}
   2083       1.1   tsutsui 
   2084      1.15   tsutsui 		if (msg == MSG_CMDCOMPLETE) {
   2085      1.15   tsutsui 			if ((scb->ta_stat &
   2086      1.15   tsutsui 			    (SCSI_INTERM | SCSI_BUSY)) == SCSI_INTERM) {
   2087      1.15   tsutsui 				iha_bad_seq(sc);
   2088      1.15   tsutsui 				return (-1);
   2089      1.15   tsutsui 			}
   2090      1.15   tsutsui 			sc->sc_flags |= FLAG_EXPECT_DONE_DISC;
   2091      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   2092       1.7   tsutsui 			return (iha_wait(sc, MSG_ACCEPT));
   2093       1.1   tsutsui 		}
   2094       1.1   tsutsui 
   2095      1.15   tsutsui 		if ((msg == MSG_LINK_CMD_COMPLETE)
   2096      1.15   tsutsui 		    || (msg == MSG_LINK_CMD_COMPLETEF)) {
   2097      1.15   tsutsui 			if ((scb->ta_stat &
   2098      1.15   tsutsui 			    (SCSI_INTERM | SCSI_BUSY)) == SCSI_INTERM)
   2099      1.15   tsutsui 				return (iha_wait(sc, MSG_ACCEPT));
   2100      1.15   tsutsui 		}
   2101      1.15   tsutsui 	}
   2102       1.1   tsutsui 
   2103      1.15   tsutsui 	iha_bad_seq(sc);
   2104      1.15   tsutsui 	return (-1);
   2105       1.1   tsutsui }
   2106       1.1   tsutsui 
   2107       1.1   tsutsui /*
   2108      1.15   tsutsui  * iha_busfree - SCSI bus free detected as a result of a TIMEOUT or
   2109      1.15   tsutsui  *		 DISCONNECT interrupt. Reset the tulip FIFO and
   2110      1.15   tsutsui  *		 SCONFIG0 and enable hardware reselect. Move any active
   2111      1.15   tsutsui  *		 SCB to sc_donescb list. Return an appropriate host status
   2112      1.15   tsutsui  *		 if an I/O was active.
   2113       1.1   tsutsui  */
   2114      1.15   tsutsui static void
   2115      1.25   tsutsui iha_busfree(struct iha_softc *sc)
   2116       1.1   tsutsui {
   2117      1.15   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2118      1.15   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2119      1.15   tsutsui 	struct iha_scb *scb;
   2120       1.1   tsutsui 
   2121      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   2122      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_SCONFIG0, SCONFIG0DEFAULT);
   2123      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
   2124       1.1   tsutsui 
   2125      1.15   tsutsui 	scb = sc->sc_actscb;
   2126       1.1   tsutsui 
   2127      1.15   tsutsui 	if (scb != NULL) {
   2128      1.15   tsutsui 		if (scb->status == STATUS_SELECT)
   2129      1.15   tsutsui 			/* selection timeout   */
   2130      1.15   tsutsui 			iha_append_done_scb(sc, scb, HOST_SEL_TOUT);
   2131      1.15   tsutsui 		else
   2132      1.15   tsutsui 			/* Unexpected bus free */
   2133      1.15   tsutsui 			iha_append_done_scb(sc, scb, HOST_BAD_PHAS);
   2134       1.1   tsutsui 	}
   2135       1.1   tsutsui }
   2136       1.1   tsutsui 
   2137      1.15   tsutsui /*
   2138      1.15   tsutsui  * iha_resel - handle a detected SCSI bus reselection request.
   2139      1.15   tsutsui  */
   2140       1.1   tsutsui static int
   2141      1.25   tsutsui iha_resel(struct iha_softc *sc)
   2142       1.1   tsutsui {
   2143      1.15   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2144      1.15   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2145      1.15   tsutsui 	struct iha_scb *scb;
   2146      1.15   tsutsui 	struct tcs *tcs;
   2147      1.27   tsutsui 	uint8_t tag, target, lun, msg, abortmsg;
   2148       1.1   tsutsui 
   2149      1.15   tsutsui 	if (sc->sc_actscb != NULL) {
   2150      1.15   tsutsui 		if ((sc->sc_actscb->status == STATUS_SELECT))
   2151      1.15   tsutsui 			iha_push_pend_scb(sc, sc->sc_actscb);
   2152      1.15   tsutsui 		sc->sc_actscb = NULL;
   2153       1.1   tsutsui 	}
   2154       1.1   tsutsui 
   2155      1.15   tsutsui 	target = bus_space_read_1(iot, ioh, TUL_SBID);
   2156      1.20       jmc 	lun = bus_space_read_1(iot, ioh, TUL_SALVC) & IHA_MSG_IDENTIFY_LUNMASK;
   2157       1.1   tsutsui 
   2158      1.15   tsutsui 	tcs = &sc->sc_tcs[target];
   2159       1.1   tsutsui 
   2160      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
   2161      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
   2162       1.1   tsutsui 
   2163      1.15   tsutsui 	abortmsg = MSG_ABORT; /* until a valid tag has been obtained */
   2164       1.1   tsutsui 
   2165      1.15   tsutsui 	if (tcs->ntagscb != NULL)
   2166      1.15   tsutsui 		/* There is a non-tagged I/O active on the target */
   2167      1.15   tsutsui 		scb = tcs->ntagscb;
   2168       1.1   tsutsui 
   2169      1.15   tsutsui 	else {
   2170      1.15   tsutsui 		/*
   2171      1.15   tsutsui 		 * Since there is no active non-tagged operation
   2172      1.15   tsutsui 		 * read the tag type, the tag itself, and find
   2173      1.15   tsutsui 		 * the appropriate scb by indexing sc_scb with
   2174      1.15   tsutsui 		 * the tag.
   2175      1.15   tsutsui 		 */
   2176       1.1   tsutsui 
   2177      1.15   tsutsui 		switch (iha_wait(sc, MSG_ACCEPT)) {
   2178      1.15   tsutsui 		case -1:
   2179      1.15   tsutsui 			return (-1);
   2180      1.15   tsutsui 		case PHASE_MSG_IN:
   2181      1.15   tsutsui 			bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
   2182      1.15   tsutsui 			if ((iha_wait(sc, XF_FIFO_IN)) == -1)
   2183      1.15   tsutsui 				return (-1);
   2184      1.15   tsutsui 			break;
   2185      1.15   tsutsui 		default:
   2186      1.15   tsutsui 			goto abort;
   2187      1.15   tsutsui 		}
   2188       1.1   tsutsui 
   2189      1.15   tsutsui 		msg = bus_space_read_1(iot, ioh, TUL_SFIFO); /* Read Tag Msg */
   2190       1.1   tsutsui 
   2191      1.15   tsutsui 		if ((msg < MSG_SIMPLE_Q_TAG) || (msg > MSG_ORDERED_Q_TAG))
   2192      1.15   tsutsui 			goto abort;
   2193       1.1   tsutsui 
   2194      1.15   tsutsui 		switch (iha_wait(sc, MSG_ACCEPT)) {
   2195      1.15   tsutsui 		case -1:
   2196      1.15   tsutsui 			return (-1);
   2197      1.15   tsutsui 		case PHASE_MSG_IN:
   2198      1.15   tsutsui 			bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
   2199      1.15   tsutsui 			if ((iha_wait(sc, XF_FIFO_IN)) == -1)
   2200      1.15   tsutsui 				return (-1);
   2201      1.15   tsutsui 			break;
   2202      1.15   tsutsui 		default:
   2203      1.15   tsutsui 			goto abort;
   2204      1.15   tsutsui 		}
   2205       1.6   tsutsui 
   2206      1.15   tsutsui 		tag  = bus_space_read_1(iot, ioh, TUL_SFIFO); /* Read Tag ID */
   2207      1.15   tsutsui 		scb = &sc->sc_scb[tag];
   2208       1.1   tsutsui 
   2209      1.15   tsutsui 		abortmsg = MSG_ABORT_TAG; /* Now that we have valdid tag! */
   2210      1.15   tsutsui 	}
   2211       1.1   tsutsui 
   2212      1.15   tsutsui 	if ((scb->target != target)
   2213      1.15   tsutsui 	    || (scb->lun != lun)
   2214      1.15   tsutsui 	    || (scb->status != STATUS_BUSY)) {
   2215      1.15   tsutsui  abort:
   2216      1.15   tsutsui 		iha_msgout_abort(sc, abortmsg);
   2217      1.15   tsutsui 		return (-1);
   2218      1.15   tsutsui 	}
   2219       1.1   tsutsui 
   2220      1.15   tsutsui 	sc->sc_actscb = scb;
   2221       1.1   tsutsui 
   2222      1.15   tsutsui 	if (iha_wait(sc, MSG_ACCEPT) == -1)
   2223      1.15   tsutsui 		return (-1);
   2224       1.1   tsutsui 
   2225      1.15   tsutsui 	return (iha_next_state(sc));
   2226       1.1   tsutsui }
   2227       1.1   tsutsui 
   2228      1.15   tsutsui static int
   2229      1.25   tsutsui iha_msgin(struct iha_softc *sc)
   2230       1.1   tsutsui {
   2231       1.1   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2232       1.1   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2233      1.15   tsutsui 	int flags;
   2234      1.15   tsutsui 	int phase;
   2235      1.27   tsutsui 	uint8_t msg;
   2236       1.1   tsutsui 
   2237      1.15   tsutsui 	for (;;) {
   2238      1.15   tsutsui 		if ((bus_space_read_1(iot, ioh, TUL_SFIFOCNT) & FIFOC) > 0)
   2239      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   2240       1.1   tsutsui 
   2241      1.15   tsutsui 		bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
   2242       1.1   tsutsui 
   2243      1.15   tsutsui 		phase = iha_wait(sc, XF_FIFO_IN);
   2244      1.15   tsutsui 		msg = bus_space_read_1(iot, ioh, TUL_SFIFO);
   2245       1.1   tsutsui 
   2246      1.15   tsutsui 		switch (msg) {
   2247      1.15   tsutsui 		case MSG_DISCONNECT:
   2248      1.15   tsutsui 			sc->sc_flags |= FLAG_EXPECT_DISC;
   2249      1.15   tsutsui 			if (iha_wait(sc, MSG_ACCEPT) != -1)
   2250      1.15   tsutsui 				iha_bad_seq(sc);
   2251      1.15   tsutsui 			phase = -1;
   2252      1.15   tsutsui 			break;
   2253      1.15   tsutsui 		case MSG_SAVEDATAPOINTER:
   2254      1.15   tsutsui 		case MSG_RESTOREPOINTERS:
   2255      1.15   tsutsui 		case MSG_NOOP:
   2256      1.15   tsutsui 			phase = iha_wait(sc, MSG_ACCEPT);
   2257      1.15   tsutsui 			break;
   2258      1.15   tsutsui 		case MSG_MESSAGE_REJECT:
   2259      1.15   tsutsui 			/* XXX - need to clear FIFO like other 'Clear ATN'?*/
   2260      1.15   tsutsui 			iha_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
   2261      1.15   tsutsui 			flags = sc->sc_actscb->tcs->flags;
   2262      1.15   tsutsui 			if ((flags & FLAG_NO_NEG_SYNC) == 0)
   2263      1.15   tsutsui 				iha_set_ssig(sc, REQ | BSY | SEL, ATN);
   2264      1.15   tsutsui 			phase = iha_wait(sc, MSG_ACCEPT);
   2265      1.15   tsutsui 			break;
   2266      1.15   tsutsui 		case MSG_EXTENDED:
   2267      1.15   tsutsui 			phase = iha_msgin_extended(sc);
   2268      1.15   tsutsui 			break;
   2269      1.15   tsutsui 		case MSG_IGN_WIDE_RESIDUE:
   2270      1.15   tsutsui 			phase = iha_msgin_ignore_wid_resid(sc);
   2271      1.15   tsutsui 			break;
   2272      1.15   tsutsui 		case MSG_CMDCOMPLETE:
   2273      1.15   tsutsui 			sc->sc_flags |= FLAG_EXPECT_DONE_DISC;
   2274      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   2275      1.15   tsutsui 			phase = iha_wait(sc, MSG_ACCEPT);
   2276      1.15   tsutsui 			if (phase != -1) {
   2277      1.15   tsutsui 				iha_bad_seq(sc);
   2278      1.15   tsutsui 				return (-1);
   2279      1.15   tsutsui 			}
   2280      1.15   tsutsui 			break;
   2281      1.15   tsutsui 		default:
   2282      1.15   tsutsui 			printf("[debug] iha_msgin: bad msg type: %d\n", msg);
   2283      1.15   tsutsui 			phase = iha_msgout_reject(sc);
   2284      1.15   tsutsui 			break;
   2285      1.15   tsutsui 		}
   2286      1.13   tsutsui 
   2287      1.15   tsutsui 		if (phase != PHASE_MSG_IN)
   2288      1.15   tsutsui 			return (phase);
   2289      1.15   tsutsui 	}
   2290      1.15   tsutsui 	/* NOTREACHED */
   2291       1.1   tsutsui }
   2292       1.1   tsutsui 
   2293      1.15   tsutsui static int
   2294      1.25   tsutsui iha_msgin_extended(struct iha_softc *sc)
   2295       1.1   tsutsui {
   2296       1.1   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2297       1.1   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2298      1.15   tsutsui 	int flags, i, phase, msglen, msgcode;
   2299       1.1   tsutsui 
   2300      1.15   tsutsui 	/*
   2301      1.15   tsutsui 	 * XXX - can we just stop reading and reject, or do we have to
   2302      1.15   tsutsui 	 *	 read all input, discarding the excess, and then reject
   2303      1.15   tsutsui 	 */
   2304      1.15   tsutsui 	for (i = 0; i < IHA_MAX_EXTENDED_MSG; i++) {
   2305      1.15   tsutsui 		phase = iha_wait(sc, MSG_ACCEPT);
   2306      1.13   tsutsui 
   2307      1.15   tsutsui 		if (phase != PHASE_MSG_IN)
   2308      1.15   tsutsui 			return (phase);
   2309      1.13   tsutsui 
   2310      1.15   tsutsui 		bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
   2311       1.1   tsutsui 
   2312      1.15   tsutsui 		if (iha_wait(sc, XF_FIFO_IN) == -1)
   2313      1.15   tsutsui 			return (-1);
   2314       1.1   tsutsui 
   2315      1.15   tsutsui 		sc->sc_msg[i] = bus_space_read_1(iot, ioh, TUL_SFIFO);
   2316       1.1   tsutsui 
   2317      1.15   tsutsui 		if (sc->sc_msg[0] == i)
   2318      1.15   tsutsui 			break;
   2319      1.15   tsutsui 	}
   2320       1.1   tsutsui 
   2321      1.15   tsutsui 	msglen	= sc->sc_msg[0];
   2322      1.15   tsutsui 	msgcode = sc->sc_msg[1];
   2323       1.1   tsutsui 
   2324      1.15   tsutsui 	if ((msglen == MSG_EXT_SDTR_LEN) && (msgcode == MSG_EXT_SDTR)) {
   2325      1.15   tsutsui 		if (iha_msgin_sdtr(sc) == 0) {
   2326      1.15   tsutsui 			iha_sync_done(sc);
   2327      1.15   tsutsui 			return (iha_wait(sc, MSG_ACCEPT));
   2328      1.15   tsutsui 		}
   2329       1.1   tsutsui 
   2330      1.15   tsutsui 		iha_set_ssig(sc, REQ | BSY | SEL, ATN);
   2331       1.1   tsutsui 
   2332      1.15   tsutsui 		phase = iha_wait(sc, MSG_ACCEPT);
   2333      1.15   tsutsui 		if (phase != PHASE_MSG_OUT)
   2334      1.15   tsutsui 			return (phase);
   2335       1.1   tsutsui 
   2336      1.15   tsutsui 		/* Clear FIFO for important message - final SYNC offer */
   2337      1.15   tsutsui 		bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   2338       1.1   tsutsui 
   2339      1.15   tsutsui 		iha_sync_done(sc); /* This is our final offer */
   2340       1.1   tsutsui 
   2341      1.15   tsutsui 	} else if ((msglen == MSG_EXT_WDTR_LEN) && (msgcode == MSG_EXT_WDTR)) {
   2342       1.1   tsutsui 
   2343      1.15   tsutsui 		flags = sc->sc_actscb->tcs->flags;
   2344       1.1   tsutsui 
   2345      1.15   tsutsui 		if ((flags & FLAG_NO_WIDE) != 0)
   2346      1.15   tsutsui 			/* Offer 8bit xfers only */
   2347      1.15   tsutsui 			sc->sc_msg[2] = MSG_EXT_WDTR_BUS_8_BIT;
   2348       1.1   tsutsui 
   2349      1.15   tsutsui 		else if (sc->sc_msg[2] > MSG_EXT_WDTR_BUS_32_BIT)
   2350      1.15   tsutsui 			/* BAD MSG */
   2351      1.15   tsutsui 			return (iha_msgout_reject(sc));
   2352       1.1   tsutsui 
   2353      1.15   tsutsui 		else if (sc->sc_msg[2] == MSG_EXT_WDTR_BUS_32_BIT)
   2354      1.15   tsutsui 			/* Offer 16bit instead */
   2355      1.15   tsutsui 			sc->sc_msg[2] = MSG_EXT_WDTR_BUS_16_BIT;
   2356       1.1   tsutsui 
   2357      1.15   tsutsui 		else {
   2358      1.15   tsutsui 			iha_wide_done(sc);
   2359      1.15   tsutsui 			if ((flags & FLAG_NO_NEG_SYNC) == 0)
   2360      1.15   tsutsui 				iha_set_ssig(sc, REQ | BSY | SEL, ATN);
   2361      1.15   tsutsui 			return (iha_wait(sc, MSG_ACCEPT));
   2362      1.15   tsutsui 		}
   2363       1.1   tsutsui 
   2364      1.15   tsutsui 		iha_set_ssig(sc, REQ | BSY | SEL, ATN);
   2365       1.1   tsutsui 
   2366      1.15   tsutsui 		phase = iha_wait(sc, MSG_ACCEPT);
   2367      1.15   tsutsui 		if (phase != PHASE_MSG_OUT)
   2368      1.15   tsutsui 			return (phase);
   2369      1.15   tsutsui 	} else
   2370      1.15   tsutsui 		return (iha_msgout_reject(sc));
   2371       1.1   tsutsui 
   2372      1.15   tsutsui 	return (iha_msgout_extended(sc));
   2373       1.1   tsutsui }
   2374       1.1   tsutsui 
   2375       1.1   tsutsui /*
   2376      1.15   tsutsui  * iha_msgin_sdtr - check SDTR msg in sc_msg. If the offer is
   2377      1.15   tsutsui  *		    acceptable leave sc_msg as is and return 0.
   2378      1.15   tsutsui  *		    If the negotiation must continue, modify sc_msg
   2379      1.15   tsutsui  *		    as needed and return 1. Else return 0.
   2380       1.1   tsutsui  */
   2381       1.1   tsutsui static int
   2382      1.25   tsutsui iha_msgin_sdtr(struct iha_softc *sc)
   2383       1.1   tsutsui {
   2384      1.15   tsutsui 	int flags;
   2385      1.15   tsutsui 	int newoffer;
   2386      1.27   tsutsui 	uint8_t default_period;
   2387       1.1   tsutsui 
   2388      1.15   tsutsui 	flags = sc->sc_actscb->tcs->flags;
   2389       1.1   tsutsui 
   2390      1.15   tsutsui 	default_period = iha_rate_tbl[flags & FLAG_SCSI_RATE];
   2391       1.1   tsutsui 
   2392      1.15   tsutsui 	if (sc->sc_msg[3] == 0)
   2393      1.15   tsutsui 		/* target offered async only. Accept it. */
   2394      1.15   tsutsui 		return (0);
   2395       1.1   tsutsui 
   2396      1.15   tsutsui 	newoffer = 0;
   2397       1.1   tsutsui 
   2398      1.15   tsutsui 	if ((flags & FLAG_NO_SYNC) != 0) {
   2399      1.15   tsutsui 		sc->sc_msg[3] = 0;
   2400      1.15   tsutsui 		newoffer = 1;
   2401       1.1   tsutsui 	}
   2402       1.1   tsutsui 
   2403      1.15   tsutsui 	if (sc->sc_msg[3] > IHA_MAX_OFFSET) {
   2404      1.15   tsutsui 		sc->sc_msg[3] = IHA_MAX_OFFSET;
   2405      1.15   tsutsui 		newoffer = 1;
   2406      1.15   tsutsui 	}
   2407       1.1   tsutsui 
   2408      1.15   tsutsui 	if (sc->sc_msg[2] < default_period) {
   2409      1.15   tsutsui 		sc->sc_msg[2] = default_period;
   2410      1.15   tsutsui 		newoffer = 1;
   2411       1.1   tsutsui 	}
   2412       1.1   tsutsui 
   2413      1.15   tsutsui 	if (sc->sc_msg[2] > IHA_MAX_PERIOD) {
   2414      1.15   tsutsui 		/* Use async */
   2415      1.15   tsutsui 		sc->sc_msg[3] = 0;
   2416      1.15   tsutsui 		newoffer = 1;
   2417       1.1   tsutsui 	}
   2418       1.1   tsutsui 
   2419      1.15   tsutsui 	return (newoffer);
   2420       1.1   tsutsui }
   2421       1.1   tsutsui 
   2422      1.15   tsutsui static int
   2423      1.25   tsutsui iha_msgin_ignore_wid_resid(struct iha_softc *sc)
   2424       1.1   tsutsui {
   2425      1.15   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2426      1.15   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2427      1.15   tsutsui 	int phase;
   2428      1.15   tsutsui 
   2429      1.15   tsutsui 	phase = iha_wait(sc, MSG_ACCEPT);
   2430      1.15   tsutsui 
   2431      1.15   tsutsui 	if (phase == PHASE_MSG_IN) {
   2432      1.15   tsutsui 		phase = iha_wait(sc, XF_FIFO_IN);
   2433       1.1   tsutsui 
   2434      1.15   tsutsui 		if (phase != -1) {
   2435      1.15   tsutsui 			bus_space_write_1(iot, ioh, TUL_SFIFO, 0);
   2436  1.31.8.2      yamt 			(void)bus_space_read_1(iot, ioh, TUL_SFIFO);
   2437  1.31.8.2      yamt 			(void)bus_space_read_1(iot, ioh, TUL_SFIFO);
   2438       1.3   thorpej 
   2439      1.15   tsutsui 			phase = iha_wait(sc, MSG_ACCEPT);
   2440       1.1   tsutsui 		}
   2441      1.15   tsutsui 	}
   2442      1.15   tsutsui 
   2443      1.15   tsutsui 	return (phase);
   2444      1.15   tsutsui }
   2445       1.1   tsutsui 
   2446      1.15   tsutsui static int
   2447      1.27   tsutsui iha_msgout(struct iha_softc *sc, uint8_t msg)
   2448      1.15   tsutsui {
   2449       1.1   tsutsui 
   2450      1.15   tsutsui 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, TUL_SFIFO, msg);
   2451       1.1   tsutsui 
   2452      1.15   tsutsui 	return (iha_wait(sc, XF_FIFO_OUT));
   2453      1.15   tsutsui }
   2454       1.1   tsutsui 
   2455      1.15   tsutsui static void
   2456      1.27   tsutsui iha_msgout_abort(struct iha_softc *sc, uint8_t aborttype)
   2457      1.15   tsutsui {
   2458       1.1   tsutsui 
   2459      1.15   tsutsui 	iha_set_ssig(sc, REQ | BSY | SEL, ATN);
   2460       1.1   tsutsui 
   2461      1.15   tsutsui 	switch (iha_wait(sc, MSG_ACCEPT)) {
   2462      1.15   tsutsui 	case -1:
   2463      1.15   tsutsui 		break;
   2464       1.1   tsutsui 
   2465      1.15   tsutsui 	case PHASE_MSG_OUT:
   2466      1.15   tsutsui 		sc->sc_flags |= FLAG_EXPECT_DISC;
   2467      1.15   tsutsui 		if (iha_msgout(sc, aborttype) != -1)
   2468      1.15   tsutsui 			iha_bad_seq(sc);
   2469      1.15   tsutsui 		break;
   2470       1.1   tsutsui 
   2471      1.15   tsutsui 	default:
   2472      1.15   tsutsui 		iha_bad_seq(sc);
   2473      1.15   tsutsui 		break;
   2474      1.15   tsutsui 	}
   2475      1.15   tsutsui }
   2476       1.1   tsutsui 
   2477      1.15   tsutsui static int
   2478      1.25   tsutsui iha_msgout_reject(struct iha_softc *sc)
   2479      1.15   tsutsui {
   2480       1.1   tsutsui 
   2481      1.15   tsutsui 	iha_set_ssig(sc, REQ | BSY | SEL, ATN);
   2482       1.1   tsutsui 
   2483      1.15   tsutsui 	if (iha_wait(sc, MSG_ACCEPT) == PHASE_MSG_OUT)
   2484      1.15   tsutsui 		return (iha_msgout(sc, MSG_MESSAGE_REJECT));
   2485       1.1   tsutsui 
   2486      1.15   tsutsui 	return (-1);
   2487      1.15   tsutsui }
   2488       1.1   tsutsui 
   2489      1.15   tsutsui static int
   2490      1.25   tsutsui iha_msgout_extended(struct iha_softc *sc)
   2491      1.15   tsutsui {
   2492      1.15   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2493      1.15   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2494      1.15   tsutsui 	int phase;
   2495       1.1   tsutsui 
   2496      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXTENDED);
   2497       1.1   tsutsui 
   2498      1.15   tsutsui 	bus_space_write_multi_1(iot, ioh, TUL_SFIFO,
   2499      1.15   tsutsui 	    sc->sc_msg, sc->sc_msg[0] + 1);
   2500       1.1   tsutsui 
   2501      1.15   tsutsui 	phase = iha_wait(sc, XF_FIFO_OUT);
   2502       1.1   tsutsui 
   2503      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
   2504      1.15   tsutsui 	iha_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
   2505       1.1   tsutsui 
   2506      1.15   tsutsui 	return (phase);
   2507       1.1   tsutsui }
   2508       1.1   tsutsui 
   2509      1.15   tsutsui static int
   2510      1.25   tsutsui iha_msgout_wdtr(struct iha_softc *sc)
   2511       1.1   tsutsui {
   2512       1.1   tsutsui 
   2513      1.15   tsutsui 	sc->sc_actscb->tcs->flags |= FLAG_WIDE_DONE;
   2514       1.1   tsutsui 
   2515      1.15   tsutsui 	sc->sc_msg[0] = MSG_EXT_WDTR_LEN;
   2516      1.15   tsutsui 	sc->sc_msg[1] = MSG_EXT_WDTR;
   2517      1.15   tsutsui 	sc->sc_msg[2] = MSG_EXT_WDTR_BUS_16_BIT;
   2518       1.1   tsutsui 
   2519      1.15   tsutsui 	return (iha_msgout_extended(sc));
   2520      1.15   tsutsui }
   2521       1.1   tsutsui 
   2522      1.15   tsutsui static int
   2523      1.25   tsutsui iha_msgout_sdtr(struct iha_softc *sc)
   2524      1.15   tsutsui {
   2525      1.15   tsutsui 	struct tcs *tcs = sc->sc_actscb->tcs;
   2526       1.1   tsutsui 
   2527      1.15   tsutsui 	tcs->flags |= FLAG_SYNC_DONE;
   2528       1.1   tsutsui 
   2529      1.15   tsutsui 	sc->sc_msg[0] = MSG_EXT_SDTR_LEN;
   2530      1.15   tsutsui 	sc->sc_msg[1] = MSG_EXT_SDTR;
   2531      1.15   tsutsui 	sc->sc_msg[2] = iha_rate_tbl[tcs->flags & FLAG_SCSI_RATE];
   2532      1.15   tsutsui 	sc->sc_msg[3] = IHA_MAX_OFFSET; /* REQ/ACK */
   2533       1.1   tsutsui 
   2534      1.15   tsutsui 	return (iha_msgout_extended(sc));
   2535      1.15   tsutsui }
   2536       1.1   tsutsui 
   2537      1.15   tsutsui static void
   2538      1.25   tsutsui iha_wide_done(struct iha_softc *sc)
   2539      1.15   tsutsui {
   2540      1.15   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2541      1.15   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2542      1.15   tsutsui 	struct tcs *tcs = sc->sc_actscb->tcs;
   2543       1.1   tsutsui 
   2544      1.15   tsutsui 	tcs->syncm = 0;
   2545      1.15   tsutsui 	tcs->period = 0;
   2546      1.15   tsutsui 	tcs->offset = 0;
   2547       1.1   tsutsui 
   2548      1.15   tsutsui 	if (sc->sc_msg[2] != 0)
   2549      1.15   tsutsui 		tcs->syncm |= PERIOD_WIDE_SCSI;
   2550       1.1   tsutsui 
   2551      1.15   tsutsui 	tcs->sconfig0 &= ~ALTPD;
   2552      1.15   tsutsui 	tcs->flags &= ~FLAG_SYNC_DONE;
   2553      1.15   tsutsui 	tcs->flags |=  FLAG_WIDE_DONE;
   2554       1.1   tsutsui 
   2555      1.15   tsutsui 	iha_update_xfer_mode(sc, sc->sc_actscb->target);
   2556       1.1   tsutsui 
   2557      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
   2558      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
   2559       1.1   tsutsui }
   2560       1.1   tsutsui 
   2561       1.1   tsutsui static void
   2562      1.25   tsutsui iha_sync_done(struct iha_softc *sc)
   2563       1.1   tsutsui {
   2564       1.1   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2565       1.1   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2566      1.15   tsutsui 	struct tcs *tcs = sc->sc_actscb->tcs;
   2567      1.15   tsutsui 	int i;
   2568      1.15   tsutsui 
   2569      1.15   tsutsui 	tcs->period = sc->sc_msg[2];
   2570      1.15   tsutsui 	tcs->offset = sc->sc_msg[3];
   2571      1.15   tsutsui 	if (tcs->offset != 0) {
   2572      1.15   tsutsui 		tcs->syncm |= tcs->offset;
   2573      1.15   tsutsui 
   2574      1.15   tsutsui 		/* pick the highest possible rate */
   2575      1.15   tsutsui 		for (i = 0; i < sizeof(iha_rate_tbl); i++)
   2576      1.15   tsutsui 			if (iha_rate_tbl[i] >= tcs->period)
   2577      1.15   tsutsui 				break;
   2578      1.15   tsutsui 
   2579      1.15   tsutsui 		tcs->syncm |= (i << 4);
   2580      1.15   tsutsui 		tcs->sconfig0 |= ALTPD;
   2581      1.15   tsutsui 	}
   2582      1.15   tsutsui 
   2583      1.15   tsutsui 	tcs->flags |= FLAG_SYNC_DONE;
   2584      1.15   tsutsui 
   2585      1.15   tsutsui 	iha_update_xfer_mode(sc, sc->sc_actscb->target);
   2586       1.1   tsutsui 
   2587      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
   2588      1.15   tsutsui 	bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
   2589       1.1   tsutsui }
   2590       1.1   tsutsui 
   2591       1.1   tsutsui /*
   2592      1.15   tsutsui  * iha_bad_seq - a SCSI bus phase was encountered out of the
   2593      1.15   tsutsui  *               correct/expected sequence. Reset the SCSI bus.
   2594       1.1   tsutsui  */
   2595      1.15   tsutsui static void
   2596      1.25   tsutsui iha_bad_seq(struct iha_softc *sc)
   2597       1.1   tsutsui {
   2598      1.15   tsutsui 	struct iha_scb *scb = sc->sc_actscb;
   2599       1.1   tsutsui 
   2600      1.15   tsutsui 	if (scb != NULL)
   2601      1.15   tsutsui 		iha_append_done_scb(sc, scb, HOST_BAD_PHAS);
   2602       1.1   tsutsui 
   2603      1.15   tsutsui 	iha_reset_scsi_bus(sc);
   2604      1.15   tsutsui 	iha_reset_chip(sc);
   2605       1.1   tsutsui }
   2606       1.1   tsutsui 
   2607       1.1   tsutsui /*
   2608       1.7   tsutsui  * iha_read_eeprom - read Serial EEPROM value & set to defaults
   2609       1.1   tsutsui  *		     if required. XXX - Writing does NOT work!
   2610       1.1   tsutsui  */
   2611      1.15   tsutsui static void
   2612      1.25   tsutsui iha_read_eeprom(struct iha_softc *sc, struct iha_eeprom *eeprom)
   2613       1.1   tsutsui {
   2614       1.1   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2615       1.1   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2616      1.29  christos 	uint16_t *tbuf = (uint16_t *)eeprom;
   2617      1.27   tsutsui 	uint8_t gctrl;
   2618       1.1   tsutsui 
   2619       1.7   tsutsui 	/* Enable EEProm programming */
   2620       1.1   tsutsui 	gctrl = bus_space_read_1(iot, ioh, TUL_GCTRL0) | EEPRG;
   2621       1.1   tsutsui 	bus_space_write_1(iot, ioh, TUL_GCTRL0, gctrl);
   2622       1.1   tsutsui 
   2623       1.7   tsutsui 	/* Read EEProm */
   2624      1.29  christos 	if (iha_se2_rd_all(sc, tbuf) == 0)
   2625      1.18    provos 		panic("%s: cannot read EEPROM", sc->sc_dev.dv_xname);
   2626       1.1   tsutsui 
   2627       1.7   tsutsui 	/* Disable EEProm programming */
   2628       1.1   tsutsui 	gctrl = bus_space_read_1(iot, ioh, TUL_GCTRL0) & ~EEPRG;
   2629       1.1   tsutsui 	bus_space_write_1(iot, ioh, TUL_GCTRL0, gctrl);
   2630       1.1   tsutsui }
   2631       1.1   tsutsui 
   2632       1.7   tsutsui #ifdef notused
   2633       1.1   tsutsui /*
   2634       1.7   tsutsui  * iha_se2_update_all - Update SCSI H/A configuration parameters from
   2635       1.1   tsutsui  *			serial EEPROM Setup default pattern. Only
   2636       1.1   tsutsui  *			change those values different from the values
   2637       1.7   tsutsui  *			in iha_eeprom.
   2638       1.1   tsutsui  */
   2639      1.15   tsutsui static void
   2640      1.25   tsutsui iha_se2_update_all(struct iha_softc *sc)
   2641       1.1   tsutsui {
   2642       1.1   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2643       1.1   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2644      1.27   tsutsui 	uint16_t *np;
   2645      1.27   tsutsui 	uint32_t chksum;
   2646       1.1   tsutsui 	int i;
   2647       1.1   tsutsui 
   2648       1.1   tsutsui 	/* Enable erase/write state of EEPROM */
   2649       1.7   tsutsui 	iha_se2_instr(sc, ENABLE_ERASE);
   2650       1.1   tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
   2651       1.1   tsutsui 	EEP_WAIT();
   2652       1.1   tsutsui 
   2653      1.27   tsutsui 	np = (uint16_t *)&eeprom_default;
   2654       1.1   tsutsui 
   2655       1.1   tsutsui 	for (i = 0, chksum = 0; i < EEPROM_SIZE - 1; i++) {
   2656       1.7   tsutsui 		iha_se2_wr(sc, i, *np);
   2657       1.1   tsutsui 		chksum += *np++;
   2658       1.1   tsutsui 	}
   2659       1.1   tsutsui 
   2660       1.1   tsutsui 	chksum &= 0x0000ffff;
   2661       1.7   tsutsui 	iha_se2_wr(sc, 31, chksum);
   2662       1.1   tsutsui 
   2663       1.1   tsutsui 	/* Disable erase/write state of EEPROM */
   2664       1.7   tsutsui 	iha_se2_instr(sc, 0);
   2665       1.1   tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
   2666       1.1   tsutsui 	EEP_WAIT();
   2667       1.1   tsutsui }
   2668       1.1   tsutsui 
   2669       1.1   tsutsui /*
   2670       1.7   tsutsui  * iha_se2_wr - write the given 16 bit value into the Serial EEPROM
   2671       1.1   tsutsui  *		at the specified offset
   2672       1.1   tsutsui  */
   2673      1.15   tsutsui static void
   2674      1.27   tsutsui iha_se2_wr(struct iha_softc *sc, int addr, uint16_t writeword)
   2675       1.1   tsutsui {
   2676       1.1   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2677       1.1   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2678       1.1   tsutsui 	int i, bit;
   2679       1.1   tsutsui 
   2680       1.1   tsutsui 	/* send 'WRITE' Instruction == address | WRITE bit */
   2681       1.7   tsutsui 	iha_se2_instr(sc, addr | WRITE);
   2682       1.1   tsutsui 
   2683       1.1   tsutsui 	for (i = 16; i > 0; i--) {
   2684       1.1   tsutsui 		if (writeword & (1 << (i - 1)))
   2685       1.1   tsutsui 			bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRDO);
   2686       1.1   tsutsui 		else
   2687       1.1   tsutsui 			bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
   2688       1.1   tsutsui 		EEP_WAIT();
   2689       1.1   tsutsui 		bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRCK);
   2690       1.1   tsutsui 		EEP_WAIT();
   2691       1.1   tsutsui 	}
   2692       1.1   tsutsui 
   2693       1.1   tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
   2694       1.1   tsutsui 	EEP_WAIT();
   2695       1.1   tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
   2696       1.1   tsutsui 	EEP_WAIT();
   2697       1.1   tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
   2698       1.1   tsutsui 	EEP_WAIT();
   2699       1.1   tsutsui 
   2700       1.1   tsutsui 	for (;;) {
   2701       1.1   tsutsui 		bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRCK);
   2702       1.1   tsutsui 		EEP_WAIT();
   2703       1.1   tsutsui 		bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
   2704       1.1   tsutsui 		EEP_WAIT();
   2705       1.1   tsutsui 		bit = bus_space_read_1(iot, ioh, TUL_NVRAM) & NVRDI;
   2706       1.1   tsutsui 		EEP_WAIT();
   2707       1.1   tsutsui 		if (bit != 0)
   2708       1.1   tsutsui 			break; /* write complete */
   2709       1.1   tsutsui 	}
   2710       1.1   tsutsui 
   2711       1.1   tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
   2712       1.1   tsutsui }
   2713       1.7   tsutsui #endif
   2714       1.1   tsutsui 
   2715       1.1   tsutsui /*
   2716       1.7   tsutsui  * iha_se2_rd - read & return the 16 bit value at the specified
   2717       1.1   tsutsui  *		offset in the Serial E2PROM
   2718       1.1   tsutsui  *
   2719       1.1   tsutsui  */
   2720      1.27   tsutsui static uint16_t
   2721      1.25   tsutsui iha_se2_rd(struct iha_softc *sc, int addr)
   2722       1.1   tsutsui {
   2723       1.1   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2724       1.1   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2725       1.1   tsutsui 	int i, bit;
   2726      1.27   tsutsui 	uint16_t readword;
   2727       1.1   tsutsui 
   2728       1.1   tsutsui 	/* Send 'READ' instruction == address | READ bit */
   2729       1.7   tsutsui 	iha_se2_instr(sc, addr | READ);
   2730       1.1   tsutsui 
   2731       1.1   tsutsui 	readword = 0;
   2732       1.1   tsutsui 	for (i = 16; i > 0; i--) {
   2733       1.1   tsutsui 		bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRCK);
   2734       1.1   tsutsui 		EEP_WAIT();
   2735       1.1   tsutsui 		bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
   2736       1.1   tsutsui 		EEP_WAIT();
   2737       1.1   tsutsui 		/* sample data after the following edge of clock     */
   2738       1.1   tsutsui 		bit = bus_space_read_1(iot, ioh, TUL_NVRAM) & NVRDI ? 1 : 0;
   2739       1.1   tsutsui 		EEP_WAIT();
   2740       1.1   tsutsui 
   2741       1.1   tsutsui 		readword |= bit << (i - 1);
   2742       1.1   tsutsui 	}
   2743       1.1   tsutsui 
   2744       1.1   tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
   2745       1.1   tsutsui 
   2746       1.1   tsutsui 	return (readword);
   2747       1.1   tsutsui }
   2748       1.1   tsutsui 
   2749       1.1   tsutsui /*
   2750       1.7   tsutsui  * iha_se2_rd_all - Read SCSI H/A config parameters from serial EEPROM
   2751       1.1   tsutsui  */
   2752      1.15   tsutsui static int
   2753      1.29  christos iha_se2_rd_all(struct iha_softc *sc, uint16_t *tbuf)
   2754       1.1   tsutsui {
   2755      1.29  christos 	struct iha_eeprom *eeprom = (struct iha_eeprom *)tbuf;
   2756      1.27   tsutsui 	uint32_t chksum;
   2757       1.1   tsutsui 	int i;
   2758       1.1   tsutsui 
   2759       1.1   tsutsui 	for (i = 0, chksum = 0; i < EEPROM_SIZE - 1; i++) {
   2760      1.29  christos 		*tbuf = iha_se2_rd(sc, i);
   2761      1.29  christos 		chksum += *tbuf++;
   2762       1.1   tsutsui 	}
   2763      1.29  christos 	*tbuf = iha_se2_rd(sc, 31); /* read checksum from EEPROM */
   2764       1.1   tsutsui 
   2765       1.6   tsutsui 	chksum &= 0x0000ffff; /* lower 16 bits */
   2766       1.1   tsutsui 
   2767       1.1   tsutsui 	return (eeprom->signature == EEP_SIGNATURE) &&
   2768       1.1   tsutsui 	    (eeprom->checksum == chksum);
   2769       1.1   tsutsui }
   2770       1.1   tsutsui 
   2771       1.1   tsutsui /*
   2772       1.7   tsutsui  * iha_se2_instr - write an octet to serial E2PROM one bit at a time
   2773       1.1   tsutsui  */
   2774      1.15   tsutsui static void
   2775      1.25   tsutsui iha_se2_instr(struct iha_softc *sc, int instr)
   2776       1.1   tsutsui {
   2777       1.1   tsutsui 	bus_space_tag_t iot = sc->sc_iot;
   2778       1.1   tsutsui 	bus_space_handle_t ioh = sc->sc_ioh;
   2779       1.1   tsutsui 	int b, i;
   2780       1.1   tsutsui 
   2781       1.1   tsutsui 	b = NVRCS | NVRDO; /* Write the start bit (== 1) */
   2782       1.1   tsutsui 
   2783       1.1   tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, b);
   2784       1.1   tsutsui 	EEP_WAIT();
   2785       1.1   tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, b | NVRCK);
   2786       1.1   tsutsui 	EEP_WAIT();
   2787       1.1   tsutsui 
   2788       1.1   tsutsui 	for (i = 8; i > 0; i--) {
   2789       1.1   tsutsui 		if (instr & (1 << (i - 1)))
   2790       1.1   tsutsui 			b = NVRCS | NVRDO; /* Write a 1 bit */
   2791       1.1   tsutsui 		else
   2792       1.1   tsutsui 			b = NVRCS;	   /* Write a 0 bit */
   2793       1.1   tsutsui 
   2794       1.1   tsutsui 		bus_space_write_1(iot, ioh, TUL_NVRAM, b);
   2795       1.1   tsutsui 		EEP_WAIT();
   2796       1.1   tsutsui 		bus_space_write_1(iot, ioh, TUL_NVRAM, b | NVRCK);
   2797       1.1   tsutsui 		EEP_WAIT();
   2798       1.1   tsutsui 	}
   2799       1.1   tsutsui 
   2800       1.1   tsutsui 	bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
   2801       1.1   tsutsui }
   2802