Home | History | Annotate | Line # | Download | only in dev
ncr5380.c revision 1.47.8.2
      1  1.47.8.2  nathanw /*	$NetBSD: ncr5380.c,v 1.47.8.2 2002/10/18 02:38:20 nathanw Exp $	*/
      2  1.47.8.2  nathanw 
      3  1.47.8.2  nathanw /*
      4  1.47.8.2  nathanw  * Copyright (c) 1995 Leo Weppelman.
      5  1.47.8.2  nathanw  * All rights reserved.
      6  1.47.8.2  nathanw  *
      7  1.47.8.2  nathanw  * Redistribution and use in source and binary forms, with or without
      8  1.47.8.2  nathanw  * modification, are permitted provided that the following conditions
      9  1.47.8.2  nathanw  * are met:
     10  1.47.8.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     11  1.47.8.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     12  1.47.8.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.47.8.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     14  1.47.8.2  nathanw  *    documentation and/or other materials provided with the distribution.
     15  1.47.8.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     16  1.47.8.2  nathanw  *    must display the following acknowledgement:
     17  1.47.8.2  nathanw  *      This product includes software developed by Leo Weppelman.
     18  1.47.8.2  nathanw  * 4. The name of the author may not be used to endorse or promote products
     19  1.47.8.2  nathanw  *    derived from this software without specific prior written permission
     20  1.47.8.2  nathanw  *
     21  1.47.8.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.47.8.2  nathanw  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.47.8.2  nathanw  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.47.8.2  nathanw  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.47.8.2  nathanw  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.47.8.2  nathanw  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.47.8.2  nathanw  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.47.8.2  nathanw  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.47.8.2  nathanw  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.47.8.2  nathanw  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.47.8.2  nathanw  */
     32  1.47.8.2  nathanw 
     33  1.47.8.2  nathanw /*
     34  1.47.8.2  nathanw  * Bit mask of targets you want debugging to be shown
     35  1.47.8.2  nathanw  */
     36  1.47.8.2  nathanw u_char	dbg_target_mask = 0x7f;
     37  1.47.8.2  nathanw 
     38  1.47.8.2  nathanw /*
     39  1.47.8.2  nathanw  * Set bit for target when parity checking must be disabled.
     40  1.47.8.2  nathanw  * My (LWP) Maxtor 7245S  seems to generate parity errors on about 50%
     41  1.47.8.2  nathanw  * of all transfers while the data is correct!?
     42  1.47.8.2  nathanw  */
     43  1.47.8.2  nathanw u_char	ncr5380_no_parchk = 0xff;
     44  1.47.8.2  nathanw 
     45  1.47.8.2  nathanw #ifdef	AUTO_SENSE
     46  1.47.8.2  nathanw 
     47  1.47.8.2  nathanw /*
     48  1.47.8.2  nathanw  * Bit masks of targets that accept linked commands, and those
     49  1.47.8.2  nathanw  * that we've already checked out.  Some devices will report
     50  1.47.8.2  nathanw  * that they support linked commands when they have problems with
     51  1.47.8.2  nathanw  * them.  By default, don't try them on any devices.  Allow an
     52  1.47.8.2  nathanw  * option to override.
     53  1.47.8.2  nathanw  */
     54  1.47.8.2  nathanw u_char	ncr_will_link = 0x00;
     55  1.47.8.2  nathanw #ifdef	TRY_SCSI_LINKED_COMMANDS
     56  1.47.8.2  nathanw u_char	ncr_test_link = ((~TRY_SCSI_LINKED_COMMANDS) & 0x7f);
     57  1.47.8.2  nathanw #else
     58  1.47.8.2  nathanw u_char	ncr_test_link = 0x7f;
     59  1.47.8.2  nathanw #endif
     60  1.47.8.2  nathanw 
     61  1.47.8.2  nathanw #endif	/* AUTO_SENSE */
     62  1.47.8.2  nathanw 
     63  1.47.8.2  nathanw /*
     64  1.47.8.2  nathanw  * This is the default sense-command we send.
     65  1.47.8.2  nathanw  */
     66  1.47.8.2  nathanw static	u_char	sense_cmd[] = {
     67  1.47.8.2  nathanw 		REQUEST_SENSE, 0, 0, 0, sizeof(struct scsipi_sense_data), 0
     68  1.47.8.2  nathanw };
     69  1.47.8.2  nathanw 
     70  1.47.8.2  nathanw /*
     71  1.47.8.2  nathanw  * True if the main co-routine is running
     72  1.47.8.2  nathanw  */
     73  1.47.8.2  nathanw static volatile int	main_running = 0;
     74  1.47.8.2  nathanw 
     75  1.47.8.2  nathanw /*
     76  1.47.8.2  nathanw  * Mask of targets selected
     77  1.47.8.2  nathanw  */
     78  1.47.8.2  nathanw static u_char	busy;
     79  1.47.8.2  nathanw 
     80  1.47.8.2  nathanw static void	ncr5380_minphys __P((struct buf *bp));
     81  1.47.8.2  nathanw static void	ncr5380_scsi_request __P((struct scsipi_channel *,
     82  1.47.8.2  nathanw 					scsipi_adapter_req_t, void *));
     83  1.47.8.2  nathanw static void	ncr5380_show_scsi_cmd __P((struct scsipi_xfer *xs));
     84  1.47.8.2  nathanw 
     85  1.47.8.2  nathanw static SC_REQ	req_queue[NREQ];
     86  1.47.8.2  nathanw static SC_REQ	*free_head = NULL;	/* Free request structures	*/
     87  1.47.8.2  nathanw 
     88  1.47.8.2  nathanw 
     89  1.47.8.2  nathanw /*
     90  1.47.8.2  nathanw  * Inline functions:
     91  1.47.8.2  nathanw  */
     92  1.47.8.2  nathanw 
     93  1.47.8.2  nathanw /*
     94  1.47.8.2  nathanw  * Determine the size of a SCSI command.
     95  1.47.8.2  nathanw  */
     96  1.47.8.2  nathanw extern __inline__ int command_size(opcode)
     97  1.47.8.2  nathanw u_char	opcode;
     98  1.47.8.2  nathanw {
     99  1.47.8.2  nathanw 	switch ((opcode >> 4) & 0xf) {
    100  1.47.8.2  nathanw 		case 0:
    101  1.47.8.2  nathanw 		case 1:
    102  1.47.8.2  nathanw 			return (6);
    103  1.47.8.2  nathanw 		case 2:
    104  1.47.8.2  nathanw 		case 3:
    105  1.47.8.2  nathanw 			return (10);
    106  1.47.8.2  nathanw 	}
    107  1.47.8.2  nathanw 	return (12);
    108  1.47.8.2  nathanw }
    109  1.47.8.2  nathanw 
    110  1.47.8.2  nathanw 
    111  1.47.8.2  nathanw /*
    112  1.47.8.2  nathanw  * Wait for request-line to become active. When it doesn't return 0.
    113  1.47.8.2  nathanw  * Otherwise return != 0.
    114  1.47.8.2  nathanw  * The timeouts in the 'wait_req_*' functions are arbitrary and rather
    115  1.47.8.2  nathanw  * large. In 99% of the invocations nearly no timeout is needed but in
    116  1.47.8.2  nathanw  * some cases (especially when using my tapedrive, a Tandberg 3600) the
    117  1.47.8.2  nathanw  * device is busy internally and the first SCSI-phase will be delayed.
    118  1.47.8.2  nathanw  *
    119  1.47.8.2  nathanw  * -- A sleeping Fujitsu M2512 is even worse; try 2.5 sec     -hf 20 Jun
    120  1.47.8.2  nathanw  */
    121  1.47.8.2  nathanw extern __inline__ int wait_req_true(void)
    122  1.47.8.2  nathanw {
    123  1.47.8.2  nathanw 	int	timeout = 2500000;
    124  1.47.8.2  nathanw 
    125  1.47.8.2  nathanw 	while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
    126  1.47.8.2  nathanw 		delay(1);
    127  1.47.8.2  nathanw 	return (GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ);
    128  1.47.8.2  nathanw }
    129  1.47.8.2  nathanw 
    130  1.47.8.2  nathanw /*
    131  1.47.8.2  nathanw  * Wait for request-line to become inactive. When it doesn't return 0.
    132  1.47.8.2  nathanw  * Otherwise return != 0.
    133  1.47.8.2  nathanw  */
    134  1.47.8.2  nathanw extern __inline__ int wait_req_false(void)
    135  1.47.8.2  nathanw {
    136  1.47.8.2  nathanw 	int	timeout = 2500000;
    137  1.47.8.2  nathanw 
    138  1.47.8.2  nathanw 	while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
    139  1.47.8.2  nathanw 		delay(1);
    140  1.47.8.2  nathanw 	return (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ));
    141  1.47.8.2  nathanw }
    142  1.47.8.2  nathanw 
    143  1.47.8.2  nathanw extern __inline__ void ack_message()
    144  1.47.8.2  nathanw {
    145  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_ICOM, 0);
    146  1.47.8.2  nathanw }
    147  1.47.8.2  nathanw 
    148  1.47.8.2  nathanw extern __inline__ void nack_message(SC_REQ *reqp, u_char msg)
    149  1.47.8.2  nathanw {
    150  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
    151  1.47.8.2  nathanw 	reqp->msgout = msg;
    152  1.47.8.2  nathanw }
    153  1.47.8.2  nathanw 
    154  1.47.8.2  nathanw extern __inline__ void finish_req(SC_REQ *reqp)
    155  1.47.8.2  nathanw {
    156  1.47.8.2  nathanw 	int			sps;
    157  1.47.8.2  nathanw 	struct scsipi_xfer	*xs = reqp->xs;
    158  1.47.8.2  nathanw 
    159  1.47.8.2  nathanw #ifdef REAL_DMA
    160  1.47.8.2  nathanw 	/*
    161  1.47.8.2  nathanw 	 * If we bounced, free the bounce buffer
    162  1.47.8.2  nathanw 	 */
    163  1.47.8.2  nathanw 	if (reqp->dr_flag & DRIVER_BOUNCING)
    164  1.47.8.2  nathanw 		free_bounceb(reqp->bounceb);
    165  1.47.8.2  nathanw #endif /* REAL_DMA */
    166  1.47.8.2  nathanw #ifdef DBG_REQ
    167  1.47.8.2  nathanw 	if (dbg_target_mask & (1 << reqp->targ_id))
    168  1.47.8.2  nathanw 		show_request(reqp, "DONE");
    169  1.47.8.2  nathanw #endif
    170  1.47.8.2  nathanw #ifdef DBG_ERR_RET
    171  1.47.8.2  nathanw 	if (reqp->xs->error != 0)
    172  1.47.8.2  nathanw 		show_request(reqp, "ERR_RET");
    173  1.47.8.2  nathanw #endif
    174  1.47.8.2  nathanw 	/*
    175  1.47.8.2  nathanw 	 * Return request to free-q
    176  1.47.8.2  nathanw 	 */
    177  1.47.8.2  nathanw 	sps = splbio();
    178  1.47.8.2  nathanw 	reqp->next = free_head;
    179  1.47.8.2  nathanw 	free_head  = reqp;
    180  1.47.8.2  nathanw 	splx(sps);
    181  1.47.8.2  nathanw 
    182  1.47.8.2  nathanw 	xs->xs_status |= XS_STS_DONE;
    183  1.47.8.2  nathanw 	if (!(reqp->dr_flag & DRIVER_LINKCHK))
    184  1.47.8.2  nathanw 		scsipi_done(xs);
    185  1.47.8.2  nathanw }
    186  1.47.8.2  nathanw 
    187  1.47.8.2  nathanw /*
    188  1.47.8.2  nathanw  * Auto config stuff....
    189  1.47.8.2  nathanw  */
    190  1.47.8.2  nathanw void	ncr_attach __P((struct device *, struct device *, void *));
    191  1.47.8.2  nathanw int	ncr_match __P((struct device *, struct cfdata *, void *));
    192  1.47.8.2  nathanw 
    193  1.47.8.2  nathanw /*
    194  1.47.8.2  nathanw  * Tricks to make driver-name configurable
    195  1.47.8.2  nathanw  */
    196  1.47.8.2  nathanw #define CFNAME(n)	__CONCAT(n,_cd)
    197  1.47.8.2  nathanw #define CANAME(n)	__CONCAT(n,_ca)
    198  1.47.8.2  nathanw #define CFSTRING(n)	__STRING(n)
    199  1.47.8.2  nathanw #define	CFDRNAME(n)	n
    200  1.47.8.2  nathanw 
    201  1.47.8.2  nathanw CFATTACH_DECL(CFDRNAME(DRNAME), sizeof(struct ncr_softc),
    202  1.47.8.2  nathanw     ncr_match, ncr_attach, NULL, NULL);
    203  1.47.8.2  nathanw 
    204  1.47.8.2  nathanw extern struct cfdriver CFNAME(DRNAME);
    205  1.47.8.2  nathanw 
    206  1.47.8.2  nathanw int
    207  1.47.8.2  nathanw ncr_match(parent, cf, aux)
    208  1.47.8.2  nathanw 	struct device *parent;
    209  1.47.8.2  nathanw 	struct cfdata *cf;
    210  1.47.8.2  nathanw 	void *aux;
    211  1.47.8.2  nathanw {
    212  1.47.8.2  nathanw 	return (machine_match(parent, cf, aux, &CFNAME(DRNAME)));
    213  1.47.8.2  nathanw }
    214  1.47.8.2  nathanw 
    215  1.47.8.2  nathanw void
    216  1.47.8.2  nathanw ncr_attach(pdp, dp, auxp)
    217  1.47.8.2  nathanw struct device	*pdp, *dp;
    218  1.47.8.2  nathanw void		*auxp;
    219  1.47.8.2  nathanw {
    220  1.47.8.2  nathanw 	struct ncr_softc	*sc;
    221  1.47.8.2  nathanw 	int			i;
    222  1.47.8.2  nathanw 
    223  1.47.8.2  nathanw 	sc = (struct ncr_softc *)dp;
    224  1.47.8.2  nathanw 
    225  1.47.8.2  nathanw 	sc->sc_adapter.adapt_dev = &sc->sc_dev;
    226  1.47.8.2  nathanw 	sc->sc_adapter.adapt_openings = 7;
    227  1.47.8.2  nathanw 	sc->sc_adapter.adapt_max_periph = 1;
    228  1.47.8.2  nathanw 	sc->sc_adapter.adapt_ioctl = NULL;
    229  1.47.8.2  nathanw 	sc->sc_adapter.adapt_minphys = ncr5380_minphys;
    230  1.47.8.2  nathanw 	sc->sc_adapter.adapt_request = ncr5380_scsi_request;
    231  1.47.8.2  nathanw 
    232  1.47.8.2  nathanw 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
    233  1.47.8.2  nathanw 	sc->sc_channel.chan_bustype = &scsi_bustype;
    234  1.47.8.2  nathanw 	sc->sc_channel.chan_channel = 0;
    235  1.47.8.2  nathanw 	sc->sc_channel.chan_ntargets = 8;
    236  1.47.8.2  nathanw 	sc->sc_channel.chan_nluns = 8;
    237  1.47.8.2  nathanw 	sc->sc_channel.chan_id = 7;
    238  1.47.8.2  nathanw 
    239  1.47.8.2  nathanw 	/*
    240  1.47.8.2  nathanw 	 * bitmasks
    241  1.47.8.2  nathanw 	 */
    242  1.47.8.2  nathanw 	sc->sc_noselatn = 0;
    243  1.47.8.2  nathanw 	sc->sc_selected = 0;
    244  1.47.8.2  nathanw 
    245  1.47.8.2  nathanw 	/*
    246  1.47.8.2  nathanw 	 * Initialize machine-type specific things...
    247  1.47.8.2  nathanw 	 */
    248  1.47.8.2  nathanw 	scsi_mach_init(sc);
    249  1.47.8.2  nathanw 	printf("\n");
    250  1.47.8.2  nathanw 
    251  1.47.8.2  nathanw 	/*
    252  1.47.8.2  nathanw 	 * Initialize request queue freelist.
    253  1.47.8.2  nathanw 	 */
    254  1.47.8.2  nathanw 	for (i = 0; i < NREQ; i++) {
    255  1.47.8.2  nathanw 		req_queue[i].next = free_head;
    256  1.47.8.2  nathanw 		free_head = &req_queue[i];
    257  1.47.8.2  nathanw 	}
    258  1.47.8.2  nathanw 
    259  1.47.8.2  nathanw 	/*
    260  1.47.8.2  nathanw 	 * Initialize the host adapter
    261  1.47.8.2  nathanw 	 */
    262  1.47.8.2  nathanw 	scsi_idisable();
    263  1.47.8.2  nathanw 	ENABLE_NCR5380(sc);
    264  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_ICOM, 0);
    265  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_MODE, IMODE_BASE);
    266  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_TCOM, 0);
    267  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_IDSTAT, 0);
    268  1.47.8.2  nathanw 	scsi_ienable();
    269  1.47.8.2  nathanw 
    270  1.47.8.2  nathanw 	/*
    271  1.47.8.2  nathanw 	 * attach all scsi units on us
    272  1.47.8.2  nathanw 	 */
    273  1.47.8.2  nathanw 	config_found(dp, &sc->sc_channel, scsiprint);
    274  1.47.8.2  nathanw }
    275  1.47.8.2  nathanw 
    276  1.47.8.2  nathanw /*
    277  1.47.8.2  nathanw  * End of auto config stuff....
    278  1.47.8.2  nathanw  */
    279  1.47.8.2  nathanw 
    280  1.47.8.2  nathanw /*
    281  1.47.8.2  nathanw  * Carry out a request from the high level driver.
    282  1.47.8.2  nathanw  */
    283  1.47.8.2  nathanw static void
    284  1.47.8.2  nathanw ncr5380_scsi_request(chan, req, arg)
    285  1.47.8.2  nathanw 	struct scsipi_channel *chan;
    286  1.47.8.2  nathanw 	scsipi_adapter_req_t req;
    287  1.47.8.2  nathanw 	void *arg;
    288  1.47.8.2  nathanw {
    289  1.47.8.2  nathanw 	struct scsipi_xfer *xs;
    290  1.47.8.2  nathanw 	struct scsipi_periph *periph;
    291  1.47.8.2  nathanw 	struct ncr_softc *sc = (void *)chan->chan_adapter->adapt_dev;
    292  1.47.8.2  nathanw 	int	sps;
    293  1.47.8.2  nathanw 	SC_REQ	*reqp, *link, *tmp;
    294  1.47.8.2  nathanw 	int	flags = xs->xs_control;
    295  1.47.8.2  nathanw 
    296  1.47.8.2  nathanw 	switch (req) {
    297  1.47.8.2  nathanw 	case ADAPTER_REQ_RUN_XFER:
    298  1.47.8.2  nathanw 		xs = arg;
    299  1.47.8.2  nathanw 		periph = xs->xs_periph;
    300  1.47.8.2  nathanw 
    301  1.47.8.2  nathanw 		/*
    302  1.47.8.2  nathanw 		 * We do not queue RESET commands
    303  1.47.8.2  nathanw 		 */
    304  1.47.8.2  nathanw 		if (flags & XS_CTL_RESET) {
    305  1.47.8.2  nathanw 			scsi_reset_verbose(sc, "Got reset-command");
    306  1.47.8.2  nathanw 			scsipi_done(xs);
    307  1.47.8.2  nathanw 			return;
    308  1.47.8.2  nathanw 		}
    309  1.47.8.2  nathanw 
    310  1.47.8.2  nathanw 		/*
    311  1.47.8.2  nathanw 		 * Get a request block
    312  1.47.8.2  nathanw 		 */
    313  1.47.8.2  nathanw 		sps = splbio();
    314  1.47.8.2  nathanw 		if ((reqp = free_head) == 0) {
    315  1.47.8.2  nathanw 			xs->error = XS_RESOURCE_SHORTAGE;
    316  1.47.8.2  nathanw 			scsipi_done(xs);
    317  1.47.8.2  nathanw 			return;
    318  1.47.8.2  nathanw 		}
    319  1.47.8.2  nathanw 		free_head  = reqp->next;
    320  1.47.8.2  nathanw 		reqp->next = NULL;
    321  1.47.8.2  nathanw 		splx(sps);
    322  1.47.8.2  nathanw 
    323  1.47.8.2  nathanw 		/*
    324  1.47.8.2  nathanw 		 * Initialize our private fields
    325  1.47.8.2  nathanw 		 */
    326  1.47.8.2  nathanw 		reqp->dr_flag   = (flags & XS_CTL_POLL) ? DRIVER_NOINT : 0;
    327  1.47.8.2  nathanw 		reqp->phase     = NR_PHASE;
    328  1.47.8.2  nathanw 		reqp->msgout    = MSG_NOOP;
    329  1.47.8.2  nathanw 		reqp->status    = SCSGOOD;
    330  1.47.8.2  nathanw 		reqp->message   = 0xff;
    331  1.47.8.2  nathanw 		reqp->link      = NULL;
    332  1.47.8.2  nathanw 		reqp->xs        = xs;
    333  1.47.8.2  nathanw 		reqp->targ_id   = xs->xs_periph->periph_target;
    334  1.47.8.2  nathanw 		reqp->targ_lun  = xs->xs_periph->periph_lun;
    335  1.47.8.2  nathanw 		reqp->xdata_ptr = (u_char*)xs->data;
    336  1.47.8.2  nathanw 		reqp->xdata_len = xs->datalen;
    337  1.47.8.2  nathanw 		memcpy(&reqp->xcmd, xs->cmd, sizeof(struct scsi_generic));
    338  1.47.8.2  nathanw 		reqp->xcmd.bytes[0] |= reqp->targ_lun << 5;
    339  1.47.8.2  nathanw 
    340  1.47.8.2  nathanw #ifdef REAL_DMA
    341  1.47.8.2  nathanw 	/*
    342  1.47.8.2  nathanw 	 * Check if DMA can be used on this request
    343  1.47.8.2  nathanw 	 */
    344  1.47.8.2  nathanw 	if (scsi_dmaok(reqp))
    345  1.47.8.2  nathanw 		reqp->dr_flag |= DRIVER_DMAOK;
    346  1.47.8.2  nathanw #endif /* REAL_DMA */
    347  1.47.8.2  nathanw 
    348  1.47.8.2  nathanw 	/*
    349  1.47.8.2  nathanw 	 * Insert the command into the issue queue. Note that 'REQUEST SENSE'
    350  1.47.8.2  nathanw 	 * commands are inserted at the head of the queue since any command
    351  1.47.8.2  nathanw 	 * will clear the existing contingent allegience condition and the sense
    352  1.47.8.2  nathanw 	 * data is only valid while the condition exists.
    353  1.47.8.2  nathanw 	 * When possible, link the command to a previous command to the same
    354  1.47.8.2  nathanw 	 * target. This is not very sensible when AUTO_SENSE is not defined!
    355  1.47.8.2  nathanw 	 * Interrupts are disabled while we are fiddling with the issue-queue.
    356  1.47.8.2  nathanw 	 */
    357  1.47.8.2  nathanw 	sps = splbio();
    358  1.47.8.2  nathanw 	link = NULL;
    359  1.47.8.2  nathanw 	if ((issue_q == NULL) || (reqp->xcmd.opcode == REQUEST_SENSE)) {
    360  1.47.8.2  nathanw 		reqp->next = issue_q;
    361  1.47.8.2  nathanw 		issue_q    = reqp;
    362  1.47.8.2  nathanw 	}
    363  1.47.8.2  nathanw 	else {
    364  1.47.8.2  nathanw 		tmp  = issue_q;
    365  1.47.8.2  nathanw 		do {
    366  1.47.8.2  nathanw 		    if (!link && (tmp->targ_id == reqp->targ_id) && !tmp->link)
    367  1.47.8.2  nathanw 				link = tmp;
    368  1.47.8.2  nathanw 		} while (tmp->next && (tmp = tmp->next));
    369  1.47.8.2  nathanw 		tmp->next = reqp;
    370  1.47.8.2  nathanw #ifdef AUTO_SENSE
    371  1.47.8.2  nathanw 		if (link && (ncr_will_link & (1<<reqp->targ_id))) {
    372  1.47.8.2  nathanw 			link->link = reqp;
    373  1.47.8.2  nathanw 			link->xcmd.bytes[link->xs->cmdlen-2] |= 1;
    374  1.47.8.2  nathanw 		}
    375  1.47.8.2  nathanw #endif
    376  1.47.8.2  nathanw 	}
    377  1.47.8.2  nathanw #ifdef AUTO_SENSE
    378  1.47.8.2  nathanw 	/*
    379  1.47.8.2  nathanw 	 * If we haven't already, check the target for link support.
    380  1.47.8.2  nathanw 	 * Do this by prefixing the current command with a dummy
    381  1.47.8.2  nathanw 	 * Request_Sense command, link the dummy to the current
    382  1.47.8.2  nathanw 	 * command, and insert the dummy command at the head of the
    383  1.47.8.2  nathanw 	 * issue queue.  Set the DRIVER_LINKCHK flag so that we'll
    384  1.47.8.2  nathanw 	 * ignore the results of the dummy command, since we only
    385  1.47.8.2  nathanw 	 * care about whether it was accepted or not.
    386  1.47.8.2  nathanw 	 */
    387  1.47.8.2  nathanw 	if (!link && !(ncr_test_link & (1<<reqp->targ_id)) &&
    388  1.47.8.2  nathanw 	    (tmp = free_head) && !(reqp->dr_flag & DRIVER_NOINT)) {
    389  1.47.8.2  nathanw 		free_head = tmp->next;
    390  1.47.8.2  nathanw 		tmp->dr_flag = (reqp->dr_flag & ~DRIVER_DMAOK) | DRIVER_LINKCHK;
    391  1.47.8.2  nathanw 		tmp->phase = NR_PHASE;
    392  1.47.8.2  nathanw 		tmp->msgout = MSG_NOOP;
    393  1.47.8.2  nathanw 		tmp->status = SCSGOOD;
    394  1.47.8.2  nathanw 		tmp->xs = reqp->xs;
    395  1.47.8.2  nathanw 		tmp->targ_id = reqp->targ_id;
    396  1.47.8.2  nathanw 		tmp->targ_lun = reqp->targ_lun;
    397  1.47.8.2  nathanw 		bcopy(sense_cmd, &tmp->xcmd, sizeof(sense_cmd));
    398  1.47.8.2  nathanw 		tmp->xdata_ptr = (u_char *)&tmp->xs->sense.scsi_sense;
    399  1.47.8.2  nathanw 		tmp->xdata_len = sizeof(tmp->xs->sense.scsi_sense);
    400  1.47.8.2  nathanw 		ncr_test_link |= 1<<tmp->targ_id;
    401  1.47.8.2  nathanw 		tmp->link = reqp;
    402  1.47.8.2  nathanw 		tmp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1;
    403  1.47.8.2  nathanw 		tmp->next = issue_q;
    404  1.47.8.2  nathanw 		issue_q = tmp;
    405  1.47.8.2  nathanw #ifdef DBG_REQ
    406  1.47.8.2  nathanw 			if (dbg_target_mask & (1 << tmp->targ_id))
    407  1.47.8.2  nathanw 				show_request(tmp, "LINKCHK");
    408  1.47.8.2  nathanw #endif
    409  1.47.8.2  nathanw 		}
    410  1.47.8.2  nathanw #endif
    411  1.47.8.2  nathanw 		splx(sps);
    412  1.47.8.2  nathanw 
    413  1.47.8.2  nathanw #ifdef DBG_REQ
    414  1.47.8.2  nathanw 		if (dbg_target_mask & (1 << reqp->targ_id))
    415  1.47.8.2  nathanw 			show_request(reqp, (reqp->xcmd.opcode == REQUEST_SENSE) ?
    416  1.47.8.2  nathanw 								"HEAD":"TAIL");
    417  1.47.8.2  nathanw #endif
    418  1.47.8.2  nathanw 
    419  1.47.8.2  nathanw 		run_main(sc);
    420  1.47.8.2  nathanw 		return;
    421  1.47.8.2  nathanw 
    422  1.47.8.2  nathanw 	case ADAPTER_REQ_GROW_RESOURCES:
    423  1.47.8.2  nathanw 		/* XXX Not supported. */
    424  1.47.8.2  nathanw 		return;
    425  1.47.8.2  nathanw 
    426  1.47.8.2  nathanw 	case ADAPTER_REQ_SET_XFER_MODE:
    427  1.47.8.2  nathanw 		/* XXX Not supported. */
    428  1.47.8.2  nathanw 		return;
    429  1.47.8.2  nathanw 	}
    430  1.47.8.2  nathanw }
    431  1.47.8.2  nathanw 
    432  1.47.8.2  nathanw static void
    433  1.47.8.2  nathanw ncr5380_minphys(struct buf *bp)
    434  1.47.8.2  nathanw {
    435  1.47.8.2  nathanw     if (bp->b_bcount > MIN_PHYS)
    436  1.47.8.2  nathanw 	bp->b_bcount = MIN_PHYS;
    437  1.47.8.2  nathanw     minphys(bp);
    438  1.47.8.2  nathanw }
    439  1.47.8.2  nathanw #undef MIN_PHYS
    440  1.47.8.2  nathanw 
    441  1.47.8.2  nathanw static void
    442  1.47.8.2  nathanw ncr5380_show_scsi_cmd(struct scsipi_xfer *xs)
    443  1.47.8.2  nathanw {
    444  1.47.8.2  nathanw 	u_char	*b = (u_char *) xs->cmd;
    445  1.47.8.2  nathanw 	int	i  = 0;
    446  1.47.8.2  nathanw 
    447  1.47.8.2  nathanw 	scsipi_printaddr(xs->xs_periph);
    448  1.47.8.2  nathanw 	if (!(xs->xs_control & XS_CTL_RESET)) {
    449  1.47.8.2  nathanw 		while (i < xs->cmdlen) {
    450  1.47.8.2  nathanw 			if (i)
    451  1.47.8.2  nathanw 				printf(",");
    452  1.47.8.2  nathanw 			printf("%x",b[i++]);
    453  1.47.8.2  nathanw 		}
    454  1.47.8.2  nathanw 		printf("-\n");
    455  1.47.8.2  nathanw 	}
    456  1.47.8.2  nathanw 	else {
    457  1.47.8.2  nathanw 		printf("-RESET-\n");
    458  1.47.8.2  nathanw 	}
    459  1.47.8.2  nathanw }
    460  1.47.8.2  nathanw 
    461  1.47.8.2  nathanw /*
    462  1.47.8.2  nathanw  * The body of the driver.
    463  1.47.8.2  nathanw  */
    464  1.47.8.2  nathanw static void
    465  1.47.8.2  nathanw scsi_main(sc)
    466  1.47.8.2  nathanw struct ncr_softc *sc;
    467  1.47.8.2  nathanw {
    468  1.47.8.2  nathanw 	SC_REQ	*req, *prev;
    469  1.47.8.2  nathanw 	int	itype;
    470  1.47.8.2  nathanw 	int	sps;
    471  1.47.8.2  nathanw 
    472  1.47.8.2  nathanw 	/*
    473  1.47.8.2  nathanw 	 * While running in the driver SCSI-interrupts are disabled.
    474  1.47.8.2  nathanw 	 */
    475  1.47.8.2  nathanw 	scsi_idisable();
    476  1.47.8.2  nathanw 	ENABLE_NCR5380(sc);
    477  1.47.8.2  nathanw 
    478  1.47.8.2  nathanw 	PID("scsi_main1");
    479  1.47.8.2  nathanw 	for (;;) {
    480  1.47.8.2  nathanw 	    sps = splbio();
    481  1.47.8.2  nathanw 	    if (!connected) {
    482  1.47.8.2  nathanw 		/*
    483  1.47.8.2  nathanw 		 * Check if it is fair keep any exclusive access to DMA
    484  1.47.8.2  nathanw 		 * claimed. If not, stop queueing new jobs so the discon_q
    485  1.47.8.2  nathanw 		 * will be eventually drained and DMA can be given up.
    486  1.47.8.2  nathanw 		 */
    487  1.47.8.2  nathanw 		if (!fair_to_keep_dma())
    488  1.47.8.2  nathanw 			goto main_exit;
    489  1.47.8.2  nathanw 
    490  1.47.8.2  nathanw 		/*
    491  1.47.8.2  nathanw 		 * Search through the issue-queue for a command
    492  1.47.8.2  nathanw 		 * destined for a target that isn't busy.
    493  1.47.8.2  nathanw 		 */
    494  1.47.8.2  nathanw 		prev = NULL;
    495  1.47.8.2  nathanw 		for (req=issue_q; req != NULL; prev = req, req = req->next) {
    496  1.47.8.2  nathanw 			if (!(busy & (1 << req->targ_id))) {
    497  1.47.8.2  nathanw 				/*
    498  1.47.8.2  nathanw 				 * Found one, remove it from the issue queue
    499  1.47.8.2  nathanw 				 */
    500  1.47.8.2  nathanw 				if (prev == NULL)
    501  1.47.8.2  nathanw 					issue_q = req->next;
    502  1.47.8.2  nathanw 				else prev->next = req->next;
    503  1.47.8.2  nathanw 				req->next = NULL;
    504  1.47.8.2  nathanw 				break;
    505  1.47.8.2  nathanw 			}
    506  1.47.8.2  nathanw 		}
    507  1.47.8.2  nathanw 
    508  1.47.8.2  nathanw 		/*
    509  1.47.8.2  nathanw 		 * When a request has just ended, we get here before an other
    510  1.47.8.2  nathanw 		 * device detects that the bus is free and that it can
    511  1.47.8.2  nathanw 		 * reconnect. The problem is that when this happens, we always
    512  1.47.8.2  nathanw 		 * baffle the device because our (initiator) id is higher. This
    513  1.47.8.2  nathanw 		 * can cause a sort of starvation on slow devices. So we check
    514  1.47.8.2  nathanw 		 * for a pending reselection here.
    515  1.47.8.2  nathanw 		 * Note that 'connected' will be non-null if the reselection
    516  1.47.8.2  nathanw 		 * succeeds.
    517  1.47.8.2  nathanw 		 */
    518  1.47.8.2  nathanw 		if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
    519  1.47.8.2  nathanw 						== (SC_S_SEL|SC_S_IO)){
    520  1.47.8.2  nathanw 			if (req != NULL) {
    521  1.47.8.2  nathanw 				req->next = issue_q;
    522  1.47.8.2  nathanw 				issue_q = req;
    523  1.47.8.2  nathanw 			}
    524  1.47.8.2  nathanw 			splx(sps);
    525  1.47.8.2  nathanw 
    526  1.47.8.2  nathanw 			reselect(sc);
    527  1.47.8.2  nathanw 			scsi_clr_ipend();
    528  1.47.8.2  nathanw 			goto connected;
    529  1.47.8.2  nathanw 		}
    530  1.47.8.2  nathanw 
    531  1.47.8.2  nathanw 		/*
    532  1.47.8.2  nathanw 		 * The host is not connected and there is no request
    533  1.47.8.2  nathanw 		 * pending, exit.
    534  1.47.8.2  nathanw 		 */
    535  1.47.8.2  nathanw 		if (req == NULL) {
    536  1.47.8.2  nathanw 			PID("scsi_main2");
    537  1.47.8.2  nathanw 			goto main_exit;
    538  1.47.8.2  nathanw 		}
    539  1.47.8.2  nathanw 
    540  1.47.8.2  nathanw 		/*
    541  1.47.8.2  nathanw 		 * Re-enable interrupts before handling the request.
    542  1.47.8.2  nathanw 		 */
    543  1.47.8.2  nathanw 		splx(sps);
    544  1.47.8.2  nathanw 
    545  1.47.8.2  nathanw #ifdef DBG_REQ
    546  1.47.8.2  nathanw 		if (dbg_target_mask & (1 << req->targ_id))
    547  1.47.8.2  nathanw 			show_request(req, "TARGET");
    548  1.47.8.2  nathanw #endif
    549  1.47.8.2  nathanw 		/*
    550  1.47.8.2  nathanw 		 * We found a request. Try to connect to the target. If the
    551  1.47.8.2  nathanw 		 * initiator fails arbitration, the command is put back in the
    552  1.47.8.2  nathanw 		 * issue queue.
    553  1.47.8.2  nathanw 		 */
    554  1.47.8.2  nathanw 		if (scsi_select(req, 0)) {
    555  1.47.8.2  nathanw 			sps = splbio();
    556  1.47.8.2  nathanw 			req->next = issue_q;
    557  1.47.8.2  nathanw 			issue_q = req;
    558  1.47.8.2  nathanw 			splx(sps);
    559  1.47.8.2  nathanw #ifdef DBG_REQ
    560  1.47.8.2  nathanw 			if (dbg_target_mask & (1 << req->targ_id))
    561  1.47.8.2  nathanw 				ncr_tprint(req, "Select failed\n");
    562  1.47.8.2  nathanw #endif
    563  1.47.8.2  nathanw 		}
    564  1.47.8.2  nathanw 	    }
    565  1.47.8.2  nathanw 	    else splx(sps);
    566  1.47.8.2  nathanw connected:
    567  1.47.8.2  nathanw 	    if (connected) {
    568  1.47.8.2  nathanw 		/*
    569  1.47.8.2  nathanw 		 * If the host is currently connected but a 'real-dma' transfer
    570  1.47.8.2  nathanw 		 * is in progress, the 'end-of-dma' interrupt restarts main.
    571  1.47.8.2  nathanw 		 * So quit.
    572  1.47.8.2  nathanw 		 */
    573  1.47.8.2  nathanw 		sps = splbio();
    574  1.47.8.2  nathanw 		if (connected && (connected->dr_flag & DRIVER_IN_DMA)) {
    575  1.47.8.2  nathanw 			PID("scsi_main3");
    576  1.47.8.2  nathanw 			goto main_exit;
    577  1.47.8.2  nathanw 		}
    578  1.47.8.2  nathanw 		splx(sps);
    579  1.47.8.2  nathanw 
    580  1.47.8.2  nathanw 		/*
    581  1.47.8.2  nathanw 		 * Let the target guide us through the bus-phases
    582  1.47.8.2  nathanw 		 */
    583  1.47.8.2  nathanw 		while (information_transfer(sc) == -1)
    584  1.47.8.2  nathanw 			;
    585  1.47.8.2  nathanw 	    }
    586  1.47.8.2  nathanw 	}
    587  1.47.8.2  nathanw 	/* NEVER TO REACH HERE */
    588  1.47.8.2  nathanw 	panic("ncr5380-SCSI: not designed to come here");
    589  1.47.8.2  nathanw 
    590  1.47.8.2  nathanw main_exit:
    591  1.47.8.2  nathanw 	/*
    592  1.47.8.2  nathanw 	 * We enter here with interrupts disabled. We are about to exit main
    593  1.47.8.2  nathanw 	 * so interrupts should be re-enabled. Because interrupts are edge
    594  1.47.8.2  nathanw 	 * triggered, we could already have missed the interrupt. Therefore
    595  1.47.8.2  nathanw 	 * we check the IRQ-line here and re-enter when we really missed a
    596  1.47.8.2  nathanw 	 * valid interrupt.
    597  1.47.8.2  nathanw 	 */
    598  1.47.8.2  nathanw 	PID("scsi_main4");
    599  1.47.8.2  nathanw 	scsi_ienable();
    600  1.47.8.2  nathanw 
    601  1.47.8.2  nathanw 	/*
    602  1.47.8.2  nathanw 	 * If we're not currently connected, enable reselection
    603  1.47.8.2  nathanw 	 * interrupts.
    604  1.47.8.2  nathanw 	 */
    605  1.47.8.2  nathanw 	if (!connected)
    606  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_IDSTAT, SC_HOST_ID);
    607  1.47.8.2  nathanw 
    608  1.47.8.2  nathanw 	if (scsi_ipending()) {
    609  1.47.8.2  nathanw 		if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
    610  1.47.8.2  nathanw 			scsi_idisable();
    611  1.47.8.2  nathanw 			splx(sps);
    612  1.47.8.2  nathanw 
    613  1.47.8.2  nathanw 			if (itype == INTR_RESEL)
    614  1.47.8.2  nathanw 				reselect(sc);
    615  1.47.8.2  nathanw #ifdef REAL_DMA
    616  1.47.8.2  nathanw 			else dma_ready();
    617  1.47.8.2  nathanw #else
    618  1.47.8.2  nathanw 			else {
    619  1.47.8.2  nathanw 				if (pdma_ready())
    620  1.47.8.2  nathanw 					goto connected;
    621  1.47.8.2  nathanw 				panic("Got DMA interrupt without DMA");
    622  1.47.8.2  nathanw 			}
    623  1.47.8.2  nathanw #endif
    624  1.47.8.2  nathanw 			scsi_clr_ipend();
    625  1.47.8.2  nathanw 			goto connected;
    626  1.47.8.2  nathanw 		}
    627  1.47.8.2  nathanw 	}
    628  1.47.8.2  nathanw 	reconsider_dma();
    629  1.47.8.2  nathanw 
    630  1.47.8.2  nathanw 	main_running = 0;
    631  1.47.8.2  nathanw 	splx(sps);
    632  1.47.8.2  nathanw 	PID("scsi_main5");
    633  1.47.8.2  nathanw }
    634  1.47.8.2  nathanw 
    635  1.47.8.2  nathanw #ifdef REAL_DMA
    636  1.47.8.2  nathanw /*
    637  1.47.8.2  nathanw  * The SCSI-DMA interrupt.
    638  1.47.8.2  nathanw  * This interrupt can only be triggered when running in non-polled DMA
    639  1.47.8.2  nathanw  * mode. When DMA is not active, it will be silently ignored, it is usually
    640  1.47.8.2  nathanw  * to late because the EOP interrupt of the controller happens just a tiny
    641  1.47.8.2  nathanw  * bit earlier. It might become usefull when scatter/gather is implemented,
    642  1.47.8.2  nathanw  * because in that case only part of the DATAIN/DATAOUT transfer is taken
    643  1.47.8.2  nathanw  * out of a single buffer.
    644  1.47.8.2  nathanw  */
    645  1.47.8.2  nathanw static void
    646  1.47.8.2  nathanw ncr_dma_intr(sc)
    647  1.47.8.2  nathanw struct ncr_softc *sc;
    648  1.47.8.2  nathanw {
    649  1.47.8.2  nathanw 	SC_REQ	*reqp;
    650  1.47.8.2  nathanw 	int	dma_done;
    651  1.47.8.2  nathanw 
    652  1.47.8.2  nathanw 	PID("ncr_dma_intr");
    653  1.47.8.2  nathanw 	if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
    654  1.47.8.2  nathanw 		scsi_idisable();
    655  1.47.8.2  nathanw 		if (!(dma_done = dma_ready())) {
    656  1.47.8.2  nathanw 			transfer_dma(reqp, reqp->phase, 0);
    657  1.47.8.2  nathanw 			return;
    658  1.47.8.2  nathanw 		}
    659  1.47.8.2  nathanw 		run_main(sc);
    660  1.47.8.2  nathanw 	}
    661  1.47.8.2  nathanw }
    662  1.47.8.2  nathanw #endif /* REAL_DMA */
    663  1.47.8.2  nathanw 
    664  1.47.8.2  nathanw /*
    665  1.47.8.2  nathanw  * The SCSI-controller interrupt. This interrupt occurs on reselections and
    666  1.47.8.2  nathanw  * at the end of non-polled DMA-interrupts. It is assumed to be called from
    667  1.47.8.2  nathanw  * the machine-dependent hardware interrupt.
    668  1.47.8.2  nathanw  */
    669  1.47.8.2  nathanw static void
    670  1.47.8.2  nathanw ncr_ctrl_intr(sc)
    671  1.47.8.2  nathanw struct ncr_softc *sc;
    672  1.47.8.2  nathanw {
    673  1.47.8.2  nathanw 	int	itype;
    674  1.47.8.2  nathanw 
    675  1.47.8.2  nathanw 	while (scsi_ipending()) {
    676  1.47.8.2  nathanw 		scsi_idisable();
    677  1.47.8.2  nathanw 		if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
    678  1.47.8.2  nathanw 			if (itype == INTR_RESEL)
    679  1.47.8.2  nathanw 				reselect(sc);
    680  1.47.8.2  nathanw 			else {
    681  1.47.8.2  nathanw #ifdef REAL_DMA
    682  1.47.8.2  nathanw 			    int	dma_done;
    683  1.47.8.2  nathanw 			    if (!(dma_done = dma_ready())) {
    684  1.47.8.2  nathanw 				transfer_dma(connected, connected->phase, 0);
    685  1.47.8.2  nathanw 				return;
    686  1.47.8.2  nathanw 			    }
    687  1.47.8.2  nathanw #else
    688  1.47.8.2  nathanw 			    if (pdma_ready())
    689  1.47.8.2  nathanw 				return;
    690  1.47.8.2  nathanw 			    panic("Got DMA interrupt without DMA");
    691  1.47.8.2  nathanw #endif
    692  1.47.8.2  nathanw 			}
    693  1.47.8.2  nathanw 			scsi_clr_ipend();
    694  1.47.8.2  nathanw 		}
    695  1.47.8.2  nathanw 		run_main(sc);
    696  1.47.8.2  nathanw 		return;
    697  1.47.8.2  nathanw 	}
    698  1.47.8.2  nathanw 	PID("ncr_ctrl_intr1");
    699  1.47.8.2  nathanw }
    700  1.47.8.2  nathanw 
    701  1.47.8.2  nathanw /*
    702  1.47.8.2  nathanw  * Initiate a connection path between the host and the target. The function
    703  1.47.8.2  nathanw  * first goes into arbitration for the SCSI-bus. When this succeeds, the target
    704  1.47.8.2  nathanw  * is selected and an 'IDENTIFY' message is send.
    705  1.47.8.2  nathanw  * Returns -1 when the arbitration failed. Otherwise 0 is returned. When
    706  1.47.8.2  nathanw  * the target does not respond (to either selection or 'MESSAGE OUT') the
    707  1.47.8.2  nathanw  * 'done' function is executed.
    708  1.47.8.2  nathanw  * The result code given by the driver can be influenced by setting 'code'
    709  1.47.8.2  nathanw  * to a non-zero value. This is the case when 'select' is called by abort.
    710  1.47.8.2  nathanw  */
    711  1.47.8.2  nathanw static int
    712  1.47.8.2  nathanw scsi_select(reqp, code)
    713  1.47.8.2  nathanw SC_REQ	*reqp;
    714  1.47.8.2  nathanw int	code;
    715  1.47.8.2  nathanw {
    716  1.47.8.2  nathanw 	u_char			tmp[1];
    717  1.47.8.2  nathanw 	u_char			phase;
    718  1.47.8.2  nathanw 	u_long			cnt;
    719  1.47.8.2  nathanw 	int			sps;
    720  1.47.8.2  nathanw 	u_int8_t		atn_flag;
    721  1.47.8.2  nathanw 	u_int8_t		targ_bit;
    722  1.47.8.2  nathanw 	struct ncr_softc	*sc;
    723  1.47.8.2  nathanw 
    724  1.47.8.2  nathanw 	sc = (void *)reqp->xs->xs_periph->periph_channel->chan_adapter->adapt_dev;
    725  1.47.8.2  nathanw 	DBG_SELPRINT ("Starting arbitration\n", 0);
    726  1.47.8.2  nathanw 	PID("scsi_select1");
    727  1.47.8.2  nathanw 
    728  1.47.8.2  nathanw 	sps = splbio();
    729  1.47.8.2  nathanw 
    730  1.47.8.2  nathanw 	/*
    731  1.47.8.2  nathanw 	 * Prevent a race condition here. If a reslection interrupt occurred
    732  1.47.8.2  nathanw 	 * between the decision to pick a new request and the call to select,
    733  1.47.8.2  nathanw 	 * we abort the selection.
    734  1.47.8.2  nathanw 	 * Interrupts are lowered when the 5380 is setup to arbitrate for the
    735  1.47.8.2  nathanw 	 * bus.
    736  1.47.8.2  nathanw 	 */
    737  1.47.8.2  nathanw 	if (connected) {
    738  1.47.8.2  nathanw 		splx(sps);
    739  1.47.8.2  nathanw 		PID("scsi_select2");
    740  1.47.8.2  nathanw 		return (-1);
    741  1.47.8.2  nathanw 	}
    742  1.47.8.2  nathanw 
    743  1.47.8.2  nathanw 	/*
    744  1.47.8.2  nathanw 	 * Set phase bits to 0, otherwise the 5380 won't drive the bus during
    745  1.47.8.2  nathanw 	 * selection.
    746  1.47.8.2  nathanw 	 */
    747  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_TCOM, 0);
    748  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_ICOM, 0);
    749  1.47.8.2  nathanw 
    750  1.47.8.2  nathanw 	/*
    751  1.47.8.2  nathanw 	 * Arbitrate for the bus.
    752  1.47.8.2  nathanw 	 */
    753  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_DATA, SC_HOST_ID);
    754  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_MODE, SC_ARBIT);
    755  1.47.8.2  nathanw 
    756  1.47.8.2  nathanw 	splx(sps);
    757  1.47.8.2  nathanw 
    758  1.47.8.2  nathanw 	cnt = 10;
    759  1.47.8.2  nathanw 	while (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP) && --cnt)
    760  1.47.8.2  nathanw 		delay(1);
    761  1.47.8.2  nathanw 
    762  1.47.8.2  nathanw 	if (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP)) {
    763  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
    764  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_ICOM, 0);
    765  1.47.8.2  nathanw 		DBG_SELPRINT ("Arbitration lost, bus not free\n",0);
    766  1.47.8.2  nathanw 		PID("scsi_select3");
    767  1.47.8.2  nathanw 		return (-1);
    768  1.47.8.2  nathanw 	}
    769  1.47.8.2  nathanw 
    770  1.47.8.2  nathanw 	/* The arbitration delay is 2.2 usecs */
    771  1.47.8.2  nathanw 	delay(3);
    772  1.47.8.2  nathanw 
    773  1.47.8.2  nathanw 	/*
    774  1.47.8.2  nathanw 	 * Check the result of the arbitration. If we failed, return -1.
    775  1.47.8.2  nathanw 	 */
    776  1.47.8.2  nathanw 	if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
    777  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
    778  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_ICOM, 0);
    779  1.47.8.2  nathanw 		PID("scsi_select4");
    780  1.47.8.2  nathanw 		return (-1);
    781  1.47.8.2  nathanw 	}
    782  1.47.8.2  nathanw 
    783  1.47.8.2  nathanw 	/*
    784  1.47.8.2  nathanw 	 * The spec requires that we should read the data register to
    785  1.47.8.2  nathanw 	 * check for higher id's and check the SC_LA again.
    786  1.47.8.2  nathanw 	 */
    787  1.47.8.2  nathanw 	tmp[0] = GET_5380_REG(NCR5380_DATA);
    788  1.47.8.2  nathanw 	if (tmp[0] & ~((SC_HOST_ID << 1) - 1)) {
    789  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
    790  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_ICOM, 0);
    791  1.47.8.2  nathanw 		DBG_SELPRINT ("Arbitration lost, higher id present\n",0);
    792  1.47.8.2  nathanw 		PID("scsi_select5");
    793  1.47.8.2  nathanw 		return (-1);
    794  1.47.8.2  nathanw 	}
    795  1.47.8.2  nathanw 	if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
    796  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
    797  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_ICOM, 0);
    798  1.47.8.2  nathanw 		DBG_SELPRINT ("Arbitration lost,deassert SC_ARBIT\n",0);
    799  1.47.8.2  nathanw 		PID("scsi_select6");
    800  1.47.8.2  nathanw 		return (-1);
    801  1.47.8.2  nathanw 	}
    802  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_ICOM, SC_A_SEL | SC_A_BSY);
    803  1.47.8.2  nathanw 	if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
    804  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
    805  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_ICOM, 0);
    806  1.47.8.2  nathanw 		DBG_SELPRINT ("Arbitration lost, deassert SC_A_SEL\n", 0);
    807  1.47.8.2  nathanw 		PID("scsi_select7");
    808  1.47.8.2  nathanw 		return (-1);
    809  1.47.8.2  nathanw 	}
    810  1.47.8.2  nathanw 	/* Bus settle delay + Bus clear delay = 1.2 usecs */
    811  1.47.8.2  nathanw 	delay(2);
    812  1.47.8.2  nathanw 	DBG_SELPRINT ("Arbitration complete\n", 0);
    813  1.47.8.2  nathanw 
    814  1.47.8.2  nathanw 	/*
    815  1.47.8.2  nathanw 	 * Now that we won the arbitration, start the selection.
    816  1.47.8.2  nathanw 	 */
    817  1.47.8.2  nathanw 	targ_bit = 1 << reqp->targ_id;
    818  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_DATA, SC_HOST_ID | targ_bit);
    819  1.47.8.2  nathanw 
    820  1.47.8.2  nathanw 	if (sc->sc_noselatn & targ_bit)
    821  1.47.8.2  nathanw 		atn_flag = 0;
    822  1.47.8.2  nathanw 	else
    823  1.47.8.2  nathanw 		atn_flag = SC_A_ATN;
    824  1.47.8.2  nathanw 
    825  1.47.8.2  nathanw 	/*
    826  1.47.8.2  nathanw 	 * Raise ATN while SEL is true before BSY goes false from arbitration,
    827  1.47.8.2  nathanw 	 * since this is the only way to guarantee that we'll get a MESSAGE OUT
    828  1.47.8.2  nathanw 	 * phase immediately after the selection.
    829  1.47.8.2  nathanw 	 */
    830  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_ICOM, SC_A_BSY | SC_A_SEL | atn_flag | SC_ADTB);
    831  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_MODE, IMODE_BASE);
    832  1.47.8.2  nathanw 
    833  1.47.8.2  nathanw 	/*
    834  1.47.8.2  nathanw 	 * Turn off reselection interrupts
    835  1.47.8.2  nathanw 	 */
    836  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_IDSTAT, 0);
    837  1.47.8.2  nathanw 
    838  1.47.8.2  nathanw 	/*
    839  1.47.8.2  nathanw 	 * Reset BSY. The delay following it, surpresses a glitch in the
    840  1.47.8.2  nathanw 	 * 5380 which causes us to see our own BSY signal instead of that of
    841  1.47.8.2  nathanw 	 * the target.
    842  1.47.8.2  nathanw 	 */
    843  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_ICOM, SC_A_SEL | atn_flag | SC_ADTB);
    844  1.47.8.2  nathanw 	delay(1);
    845  1.47.8.2  nathanw 
    846  1.47.8.2  nathanw 	/*
    847  1.47.8.2  nathanw 	 * Wait for the target to react, the specs call for a timeout of
    848  1.47.8.2  nathanw 	 * 250 ms.
    849  1.47.8.2  nathanw 	 */
    850  1.47.8.2  nathanw 	cnt = 25000;
    851  1.47.8.2  nathanw 	while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) && --cnt)
    852  1.47.8.2  nathanw 		delay(10);
    853  1.47.8.2  nathanw 
    854  1.47.8.2  nathanw 	if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
    855  1.47.8.2  nathanw 		/*
    856  1.47.8.2  nathanw 		 * There is no reaction from the target, start the selection
    857  1.47.8.2  nathanw 		 * timeout procedure. We release the databus but keep SEL
    858  1.47.8.2  nathanw 		 * asserted. After that we wait a 'selection abort time' (200
    859  1.47.8.2  nathanw 		 * usecs) and 2 deskew delays (90 ns) and check BSY again.
    860  1.47.8.2  nathanw 		 * When BSY is asserted, we assume the selection succeeded,
    861  1.47.8.2  nathanw 		 * otherwise we release the bus.
    862  1.47.8.2  nathanw 		 */
    863  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_ICOM, SC_A_SEL | atn_flag);
    864  1.47.8.2  nathanw 		delay(201);
    865  1.47.8.2  nathanw 		if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
    866  1.47.8.2  nathanw 			SET_5380_REG(NCR5380_ICOM, 0);
    867  1.47.8.2  nathanw 			reqp->xs->error      = code ? code : XS_SELTIMEOUT;
    868  1.47.8.2  nathanw 			DBG_SELPRINT ("Target %d not responding to sel\n",
    869  1.47.8.2  nathanw 								reqp->targ_id);
    870  1.47.8.2  nathanw 			if (reqp->dr_flag & DRIVER_LINKCHK)
    871  1.47.8.2  nathanw 				ncr_test_link &= ~(1<<reqp->targ_id);
    872  1.47.8.2  nathanw 			finish_req(reqp);
    873  1.47.8.2  nathanw 			PID("scsi_select8");
    874  1.47.8.2  nathanw 			return (0);
    875  1.47.8.2  nathanw 		}
    876  1.47.8.2  nathanw 	}
    877  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_ICOM, atn_flag);
    878  1.47.8.2  nathanw 
    879  1.47.8.2  nathanw 	DBG_SELPRINT ("Target %d responding to select.\n", reqp->targ_id);
    880  1.47.8.2  nathanw 
    881  1.47.8.2  nathanw 	/*
    882  1.47.8.2  nathanw 	 * The SCSI-interrupts are disabled while a request is being handled.
    883  1.47.8.2  nathanw 	 */
    884  1.47.8.2  nathanw 	scsi_idisable();
    885  1.47.8.2  nathanw 
    886  1.47.8.2  nathanw 	/*
    887  1.47.8.2  nathanw 	 * If we did not request ATN, then don't try to send IDENTIFY.
    888  1.47.8.2  nathanw 	 */
    889  1.47.8.2  nathanw 	if (atn_flag == 0) {
    890  1.47.8.2  nathanw 		reqp->phase = PH_CMD;
    891  1.47.8.2  nathanw 		goto identify_failed;
    892  1.47.8.2  nathanw 	}
    893  1.47.8.2  nathanw 
    894  1.47.8.2  nathanw 	/*
    895  1.47.8.2  nathanw 	 * Here we prepare to send an 'IDENTIFY' message.
    896  1.47.8.2  nathanw 	 * Allow disconnect only when interrups are allowed.
    897  1.47.8.2  nathanw 	 */
    898  1.47.8.2  nathanw 	tmp[0] = MSG_IDENTIFY(reqp->targ_lun,
    899  1.47.8.2  nathanw 			(reqp->dr_flag & DRIVER_NOINT) ? 0 : 1);
    900  1.47.8.2  nathanw 	cnt    = 1;
    901  1.47.8.2  nathanw 	phase  = PH_MSGOUT;
    902  1.47.8.2  nathanw 
    903  1.47.8.2  nathanw 	/*
    904  1.47.8.2  nathanw 	 * Since we followed the SCSI-spec and raised ATN while SEL was true
    905  1.47.8.2  nathanw 	 * but before BSY was false during the selection, a 'MESSAGE OUT'
    906  1.47.8.2  nathanw 	 * phase should follow.  Unfortunately, this does not happen on
    907  1.47.8.2  nathanw 	 * all targets (Asante ethernet devices, for example), so we must
    908  1.47.8.2  nathanw 	 * check the actual mode if the message transfer fails--if the
    909  1.47.8.2  nathanw 	 * new phase is PH_CMD and has never been successfully selected
    910  1.47.8.2  nathanw 	 * w/ATN in the past, then we assume that it is an old device
    911  1.47.8.2  nathanw 	 * that doesn't support select w/ATN.
    912  1.47.8.2  nathanw 	 */
    913  1.47.8.2  nathanw 	if (transfer_pio(&phase, tmp, &cnt, 0) || cnt) {
    914  1.47.8.2  nathanw 
    915  1.47.8.2  nathanw 		if ((phase == PH_CMD) && !(sc->sc_selected & targ_bit)) {
    916  1.47.8.2  nathanw 			DBG_SELPRINT ("Target %d: not responding to ATN.\n",
    917  1.47.8.2  nathanw 							reqp->targ_id);
    918  1.47.8.2  nathanw 			sc->sc_noselatn |= targ_bit;
    919  1.47.8.2  nathanw 			reqp->phase = PH_CMD;
    920  1.47.8.2  nathanw 			goto identify_failed;
    921  1.47.8.2  nathanw 		}
    922  1.47.8.2  nathanw 
    923  1.47.8.2  nathanw 		DBG_SELPRINT ("Target %d: failed to send identify\n",
    924  1.47.8.2  nathanw 							reqp->targ_id);
    925  1.47.8.2  nathanw 		/*
    926  1.47.8.2  nathanw 		 * Try to disconnect from the target.  We cannot leave
    927  1.47.8.2  nathanw 		 * it just hanging here.
    928  1.47.8.2  nathanw 		 */
    929  1.47.8.2  nathanw 		if (!reach_msg_out(sc, sizeof(struct scsi_generic))) {
    930  1.47.8.2  nathanw 			u_long	len   = 1;
    931  1.47.8.2  nathanw 			u_char	phase = PH_MSGOUT;
    932  1.47.8.2  nathanw 			u_char	msg   = MSG_ABORT;
    933  1.47.8.2  nathanw 
    934  1.47.8.2  nathanw 			transfer_pio(&phase, &msg, &len, 0);
    935  1.47.8.2  nathanw 		}
    936  1.47.8.2  nathanw 		else scsi_reset_verbose(sc, "Connected to unidentified target");
    937  1.47.8.2  nathanw 
    938  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_ICOM, 0);
    939  1.47.8.2  nathanw 		reqp->xs->error = code ? code : XS_DRIVER_STUFFUP;
    940  1.47.8.2  nathanw 		finish_req(reqp);
    941  1.47.8.2  nathanw 		PID("scsi_select9");
    942  1.47.8.2  nathanw 		return (0);
    943  1.47.8.2  nathanw 	}
    944  1.47.8.2  nathanw 	reqp->phase = PH_MSGOUT;
    945  1.47.8.2  nathanw 
    946  1.47.8.2  nathanw identify_failed:
    947  1.47.8.2  nathanw 	sc->sc_selected |= targ_bit;
    948  1.47.8.2  nathanw 
    949  1.47.8.2  nathanw #ifdef notyet /* LWP: Do we need timeouts in the driver? */
    950  1.47.8.2  nathanw 	/*
    951  1.47.8.2  nathanw 	 * Command is connected, start timer ticking.
    952  1.47.8.2  nathanw 	 */
    953  1.47.8.2  nathanw 	ccb_p->xtimeout = ccb_p->timeout + Lbolt;
    954  1.47.8.2  nathanw #endif
    955  1.47.8.2  nathanw 
    956  1.47.8.2  nathanw 	connected  = reqp;
    957  1.47.8.2  nathanw 	busy      |= targ_bit;
    958  1.47.8.2  nathanw 	PID("scsi_select10");
    959  1.47.8.2  nathanw 	return (0);
    960  1.47.8.2  nathanw }
    961  1.47.8.2  nathanw 
    962  1.47.8.2  nathanw /*
    963  1.47.8.2  nathanw  * Return codes:
    964  1.47.8.2  nathanw  *	 0: Job has finished or disconnected, find something else
    965  1.47.8.2  nathanw  *	-1: keep on calling information_transfer() from scsi_main()
    966  1.47.8.2  nathanw  */
    967  1.47.8.2  nathanw static int
    968  1.47.8.2  nathanw information_transfer(sc)
    969  1.47.8.2  nathanw struct ncr_softc *sc;
    970  1.47.8.2  nathanw {
    971  1.47.8.2  nathanw 	SC_REQ	*reqp = connected;
    972  1.47.8.2  nathanw 	u_char	tmp, phase;
    973  1.47.8.2  nathanw 	u_long	len;
    974  1.47.8.2  nathanw 
    975  1.47.8.2  nathanw 	PID("info_transf1");
    976  1.47.8.2  nathanw 	/*
    977  1.47.8.2  nathanw 	 * Clear pending interrupts from 5380-chip.
    978  1.47.8.2  nathanw 	 */
    979  1.47.8.2  nathanw 	scsi_clr_ipend();
    980  1.47.8.2  nathanw 
    981  1.47.8.2  nathanw 	/*
    982  1.47.8.2  nathanw 	 * The SCSI-spec requires BSY to be true while connected to a target,
    983  1.47.8.2  nathanw 	 * loosing it means we lost the target...
    984  1.47.8.2  nathanw 	 * Also REQ needs to be asserted here to indicate that the bus-phase
    985  1.47.8.2  nathanw 	 * is valid. When the target does not supply REQ within a 'reasonable'
    986  1.47.8.2  nathanw 	 * amount of time, it's probably lost in it's own maze of twisting
    987  1.47.8.2  nathanw 	 * passages, we have to reset the bus to free it.
    988  1.47.8.2  nathanw 	 */
    989  1.47.8.2  nathanw 	if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)
    990  1.47.8.2  nathanw 		wait_req_true();
    991  1.47.8.2  nathanw 	tmp = GET_5380_REG(NCR5380_IDSTAT);
    992  1.47.8.2  nathanw 
    993  1.47.8.2  nathanw 
    994  1.47.8.2  nathanw 	if ((tmp & (SC_S_BSY|SC_S_REQ)) != (SC_S_BSY|SC_S_REQ)) {
    995  1.47.8.2  nathanw 		busy           &= ~(1 << reqp->targ_id);
    996  1.47.8.2  nathanw 		connected       = NULL;
    997  1.47.8.2  nathanw 		reqp->xs->error = XS_TIMEOUT;
    998  1.47.8.2  nathanw 		finish_req(reqp);
    999  1.47.8.2  nathanw 		if (!(tmp & SC_S_REQ))
   1000  1.47.8.2  nathanw 			scsi_reset_verbose(sc,
   1001  1.47.8.2  nathanw 					   "Timeout waiting for phase-change");
   1002  1.47.8.2  nathanw 		PID("info_transf2");
   1003  1.47.8.2  nathanw 		return (0);
   1004  1.47.8.2  nathanw 	}
   1005  1.47.8.2  nathanw 
   1006  1.47.8.2  nathanw 	phase = (tmp >> 2) & 7;
   1007  1.47.8.2  nathanw 	if (phase != reqp->phase) {
   1008  1.47.8.2  nathanw 		reqp->phase = phase;
   1009  1.47.8.2  nathanw 		DBG_INFPRINT(show_phase, reqp, phase);
   1010  1.47.8.2  nathanw 	}
   1011  1.47.8.2  nathanw 	else {
   1012  1.47.8.2  nathanw 		/*
   1013  1.47.8.2  nathanw 		 * Same data-phase. If same error give up
   1014  1.47.8.2  nathanw 		 */
   1015  1.47.8.2  nathanw 		if ((reqp->msgout == MSG_ABORT)
   1016  1.47.8.2  nathanw 		     && ((phase == PH_DATAOUT) || (phase == PH_DATAIN))) {
   1017  1.47.8.2  nathanw 			busy     &= ~(1 << reqp->targ_id);
   1018  1.47.8.2  nathanw 			connected = NULL;
   1019  1.47.8.2  nathanw 			finish_req(reqp);
   1020  1.47.8.2  nathanw 			scsi_reset_verbose(sc, "Failure to abort command");
   1021  1.47.8.2  nathanw 			return (0);
   1022  1.47.8.2  nathanw 		}
   1023  1.47.8.2  nathanw 	}
   1024  1.47.8.2  nathanw 
   1025  1.47.8.2  nathanw 	switch (phase) {
   1026  1.47.8.2  nathanw 	    case PH_DATAOUT:
   1027  1.47.8.2  nathanw #ifdef DBG_NOWRITE
   1028  1.47.8.2  nathanw 		ncr_tprint(reqp, "NOWRITE set -- write attempt aborted.");
   1029  1.47.8.2  nathanw 		reqp->msgout = MSG_ABORT;
   1030  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
   1031  1.47.8.2  nathanw 		return (-1);
   1032  1.47.8.2  nathanw #endif /* DBG_NOWRITE */
   1033  1.47.8.2  nathanw 		/*
   1034  1.47.8.2  nathanw 		 * If this is the first write using DMA, fill
   1035  1.47.8.2  nathanw 		 * the bounce buffer.
   1036  1.47.8.2  nathanw 		 */
   1037  1.47.8.2  nathanw 		if (reqp->xdata_ptr == reqp->xs->data) { /* XXX */
   1038  1.47.8.2  nathanw 		    if (reqp->dr_flag & DRIVER_BOUNCING)
   1039  1.47.8.2  nathanw 			bcopy(reqp->xdata_ptr, reqp->bounceb, reqp->xdata_len);
   1040  1.47.8.2  nathanw 		}
   1041  1.47.8.2  nathanw 
   1042  1.47.8.2  nathanw 	   case PH_DATAIN:
   1043  1.47.8.2  nathanw 		if (reqp->xdata_len <= 0) {
   1044  1.47.8.2  nathanw 			/*
   1045  1.47.8.2  nathanw 			 * Target keeps requesting data. Try to get into
   1046  1.47.8.2  nathanw 			 * message-out phase by feeding/taking 100 byte.
   1047  1.47.8.2  nathanw 			 */
   1048  1.47.8.2  nathanw 			ncr_tprint(reqp, "Target requests too much data\n");
   1049  1.47.8.2  nathanw 			reqp->msgout = MSG_ABORT;
   1050  1.47.8.2  nathanw 			SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
   1051  1.47.8.2  nathanw 			reach_msg_out(sc, 100);
   1052  1.47.8.2  nathanw 			return (-1);
   1053  1.47.8.2  nathanw 		}
   1054  1.47.8.2  nathanw #ifdef REAL_DMA
   1055  1.47.8.2  nathanw 		if (reqp->dr_flag & DRIVER_DMAOK) {
   1056  1.47.8.2  nathanw 			int poll = REAL_DMA_POLL|(reqp->dr_flag & DRIVER_NOINT);
   1057  1.47.8.2  nathanw 			transfer_dma(reqp, phase, poll);
   1058  1.47.8.2  nathanw 			if (!poll)
   1059  1.47.8.2  nathanw 				return (0);
   1060  1.47.8.2  nathanw 		}
   1061  1.47.8.2  nathanw 		else
   1062  1.47.8.2  nathanw #endif
   1063  1.47.8.2  nathanw 		{
   1064  1.47.8.2  nathanw 			PID("info_transf3");
   1065  1.47.8.2  nathanw 			len = reqp->xdata_len;
   1066  1.47.8.2  nathanw #ifdef USE_PDMA
   1067  1.47.8.2  nathanw 			if (transfer_pdma(&phase, reqp->xdata_ptr, &len) == 0)
   1068  1.47.8.2  nathanw 				return (0);
   1069  1.47.8.2  nathanw #else
   1070  1.47.8.2  nathanw 			transfer_pio(&phase, reqp->xdata_ptr, &len, 0);
   1071  1.47.8.2  nathanw #endif
   1072  1.47.8.2  nathanw 			reqp->xdata_ptr += reqp->xdata_len - len;
   1073  1.47.8.2  nathanw 			reqp->xdata_len  = len;
   1074  1.47.8.2  nathanw 		}
   1075  1.47.8.2  nathanw 		return (-1);
   1076  1.47.8.2  nathanw 	   case PH_MSGIN:
   1077  1.47.8.2  nathanw 		/*
   1078  1.47.8.2  nathanw 		 * We only expect single byte messages here.
   1079  1.47.8.2  nathanw 		 */
   1080  1.47.8.2  nathanw 		len = 1;
   1081  1.47.8.2  nathanw 		transfer_pio(&phase, &tmp, &len, 1);
   1082  1.47.8.2  nathanw 		reqp->message = tmp;
   1083  1.47.8.2  nathanw 		return (handle_message(reqp, tmp));
   1084  1.47.8.2  nathanw 	   case PH_MSGOUT:
   1085  1.47.8.2  nathanw 		len = 1;
   1086  1.47.8.2  nathanw 		transfer_pio(&phase, &reqp->msgout, &len, 0);
   1087  1.47.8.2  nathanw 		if (reqp->msgout == MSG_ABORT) {
   1088  1.47.8.2  nathanw 			busy     &= ~(1 << reqp->targ_id);
   1089  1.47.8.2  nathanw 			connected = NULL;
   1090  1.47.8.2  nathanw 			if (!reqp->xs->error)
   1091  1.47.8.2  nathanw 				reqp->xs->error = XS_DRIVER_STUFFUP;
   1092  1.47.8.2  nathanw 			finish_req(reqp);
   1093  1.47.8.2  nathanw 			PID("info_transf4");
   1094  1.47.8.2  nathanw 			return (0);
   1095  1.47.8.2  nathanw 		}
   1096  1.47.8.2  nathanw 		reqp->msgout = MSG_NOOP;
   1097  1.47.8.2  nathanw 		return (-1);
   1098  1.47.8.2  nathanw 	   case PH_CMD :
   1099  1.47.8.2  nathanw 		len = command_size(reqp->xcmd.opcode);
   1100  1.47.8.2  nathanw 		transfer_pio(&phase, (u_char *)&reqp->xcmd, &len, 0);
   1101  1.47.8.2  nathanw 		PID("info_transf5");
   1102  1.47.8.2  nathanw 		return (-1);
   1103  1.47.8.2  nathanw 	   case PH_STATUS:
   1104  1.47.8.2  nathanw 		len = 1;
   1105  1.47.8.2  nathanw 		transfer_pio(&phase, &tmp, &len, 0);
   1106  1.47.8.2  nathanw 		reqp->status = tmp;
   1107  1.47.8.2  nathanw 		PID("info_transf6");
   1108  1.47.8.2  nathanw 		return (-1);
   1109  1.47.8.2  nathanw 	   default :
   1110  1.47.8.2  nathanw 		ncr_tprint(reqp, "Unknown phase\n");
   1111  1.47.8.2  nathanw 	}
   1112  1.47.8.2  nathanw 	PID("info_transf7");
   1113  1.47.8.2  nathanw 	return (-1);
   1114  1.47.8.2  nathanw }
   1115  1.47.8.2  nathanw 
   1116  1.47.8.2  nathanw /*
   1117  1.47.8.2  nathanw  * Handle the message 'msg' send to us by the target.
   1118  1.47.8.2  nathanw  * Return values:
   1119  1.47.8.2  nathanw  *	 0 : The current command has completed.
   1120  1.47.8.2  nathanw  *	-1 : Get on to the next phase.
   1121  1.47.8.2  nathanw  */
   1122  1.47.8.2  nathanw static int
   1123  1.47.8.2  nathanw handle_message(reqp, msg)
   1124  1.47.8.2  nathanw SC_REQ	*reqp;
   1125  1.47.8.2  nathanw u_int	msg;
   1126  1.47.8.2  nathanw {
   1127  1.47.8.2  nathanw 	int	sps;
   1128  1.47.8.2  nathanw 	SC_REQ	*prev, *req;
   1129  1.47.8.2  nathanw 
   1130  1.47.8.2  nathanw 	PID("hmessage1");
   1131  1.47.8.2  nathanw 	switch (msg) {
   1132  1.47.8.2  nathanw 		/*
   1133  1.47.8.2  nathanw 		 * Linking lets us reduce the time required to get
   1134  1.47.8.2  nathanw 		 * the next command to the device, skipping the arbitration
   1135  1.47.8.2  nathanw 		 * and selection time. In the current implementation,
   1136  1.47.8.2  nathanw 		 * we merely have to start the next command pointed
   1137  1.47.8.2  nathanw 		 * to by 'next_link'.
   1138  1.47.8.2  nathanw 		 */
   1139  1.47.8.2  nathanw 		case MSG_LINK_CMD_COMPLETE:
   1140  1.47.8.2  nathanw 		case MSG_LINK_CMD_COMPLETEF:
   1141  1.47.8.2  nathanw 			if (reqp->link == NULL) {
   1142  1.47.8.2  nathanw 				ncr_tprint(reqp, "No link for linked command");
   1143  1.47.8.2  nathanw 				nack_message(reqp, MSG_ABORT);
   1144  1.47.8.2  nathanw 				PID("hmessage2");
   1145  1.47.8.2  nathanw 				return (-1);
   1146  1.47.8.2  nathanw 			}
   1147  1.47.8.2  nathanw 			ack_message();
   1148  1.47.8.2  nathanw 			if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
   1149  1.47.8.2  nathanw 				reqp->xs->resid = reqp->xdata_len;
   1150  1.47.8.2  nathanw 				reqp->xs->error = 0;
   1151  1.47.8.2  nathanw 			}
   1152  1.47.8.2  nathanw 
   1153  1.47.8.2  nathanw #ifdef AUTO_SENSE
   1154  1.47.8.2  nathanw 			if (check_autosense(reqp, 1) == -1)
   1155  1.47.8.2  nathanw 				return (-1);
   1156  1.47.8.2  nathanw #endif /* AUTO_SENSE */
   1157  1.47.8.2  nathanw 
   1158  1.47.8.2  nathanw #ifdef DBG_REQ
   1159  1.47.8.2  nathanw 			if (dbg_target_mask & (1 << reqp->targ_id))
   1160  1.47.8.2  nathanw 				show_request(reqp->link, "LINK");
   1161  1.47.8.2  nathanw #endif
   1162  1.47.8.2  nathanw 			connected = reqp->link;
   1163  1.47.8.2  nathanw 
   1164  1.47.8.2  nathanw 			/*
   1165  1.47.8.2  nathanw 			 * Unlink the 'linked' request from the issue_q
   1166  1.47.8.2  nathanw 			 */
   1167  1.47.8.2  nathanw 			sps  = splbio();
   1168  1.47.8.2  nathanw 			prev = NULL;
   1169  1.47.8.2  nathanw 			req  = issue_q;
   1170  1.47.8.2  nathanw 			for (; req != NULL; prev = req, req = req->next) {
   1171  1.47.8.2  nathanw 				if (req == connected)
   1172  1.47.8.2  nathanw 					break;
   1173  1.47.8.2  nathanw 			}
   1174  1.47.8.2  nathanw 			if (req == NULL)
   1175  1.47.8.2  nathanw 				panic("Inconsistent issue_q");
   1176  1.47.8.2  nathanw 			if (prev == NULL)
   1177  1.47.8.2  nathanw 				issue_q = req->next;
   1178  1.47.8.2  nathanw 			else prev->next = req->next;
   1179  1.47.8.2  nathanw 			req->next = NULL;
   1180  1.47.8.2  nathanw 			splx(sps);
   1181  1.47.8.2  nathanw 
   1182  1.47.8.2  nathanw 			finish_req(reqp);
   1183  1.47.8.2  nathanw 			PID("hmessage3");
   1184  1.47.8.2  nathanw 			return (-1);
   1185  1.47.8.2  nathanw 		case MSG_ABORT:
   1186  1.47.8.2  nathanw 		case MSG_CMDCOMPLETE:
   1187  1.47.8.2  nathanw 			ack_message();
   1188  1.47.8.2  nathanw 			connected = NULL;
   1189  1.47.8.2  nathanw 			busy     &= ~(1 << reqp->targ_id);
   1190  1.47.8.2  nathanw 			if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
   1191  1.47.8.2  nathanw 				reqp->xs->resid = reqp->xdata_len;
   1192  1.47.8.2  nathanw 				reqp->xs->error = 0;
   1193  1.47.8.2  nathanw 			}
   1194  1.47.8.2  nathanw 
   1195  1.47.8.2  nathanw #ifdef AUTO_SENSE
   1196  1.47.8.2  nathanw 			if (check_autosense(reqp, 0) == -1) {
   1197  1.47.8.2  nathanw 				PID("hmessage4");
   1198  1.47.8.2  nathanw 				return (0);
   1199  1.47.8.2  nathanw 			}
   1200  1.47.8.2  nathanw #endif /* AUTO_SENSE */
   1201  1.47.8.2  nathanw 
   1202  1.47.8.2  nathanw 			finish_req(reqp);
   1203  1.47.8.2  nathanw 			PID("hmessage5");
   1204  1.47.8.2  nathanw 			return (0);
   1205  1.47.8.2  nathanw 		case MSG_MESSAGE_REJECT:
   1206  1.47.8.2  nathanw 			ack_message();
   1207  1.47.8.2  nathanw 			PID("hmessage6");
   1208  1.47.8.2  nathanw 			return (-1);
   1209  1.47.8.2  nathanw 		case MSG_DISCONNECT:
   1210  1.47.8.2  nathanw 			ack_message();
   1211  1.47.8.2  nathanw #ifdef DBG_REQ
   1212  1.47.8.2  nathanw 			if (dbg_target_mask & (1 << reqp->targ_id))
   1213  1.47.8.2  nathanw 				show_request(reqp, "DISCON");
   1214  1.47.8.2  nathanw #endif
   1215  1.47.8.2  nathanw 			sps = splbio();
   1216  1.47.8.2  nathanw 			connected  = NULL;
   1217  1.47.8.2  nathanw 			reqp->next = discon_q;
   1218  1.47.8.2  nathanw 			discon_q   = reqp;
   1219  1.47.8.2  nathanw 			splx(sps);
   1220  1.47.8.2  nathanw 			PID("hmessage7");
   1221  1.47.8.2  nathanw 			return (0);
   1222  1.47.8.2  nathanw 		case MSG_SAVEDATAPOINTER:
   1223  1.47.8.2  nathanw 		case MSG_RESTOREPOINTERS:
   1224  1.47.8.2  nathanw 			/*
   1225  1.47.8.2  nathanw 			 * We save pointers implicitely at disconnect.
   1226  1.47.8.2  nathanw 			 * So we can ignore these messages.
   1227  1.47.8.2  nathanw 			 */
   1228  1.47.8.2  nathanw 			ack_message();
   1229  1.47.8.2  nathanw 			PID("hmessage8");
   1230  1.47.8.2  nathanw 			return (-1);
   1231  1.47.8.2  nathanw 		case MSG_EXTENDED:
   1232  1.47.8.2  nathanw 			nack_message(reqp, MSG_MESSAGE_REJECT);
   1233  1.47.8.2  nathanw 			PID("hmessage9");
   1234  1.47.8.2  nathanw 			return (-1);
   1235  1.47.8.2  nathanw 		default:
   1236  1.47.8.2  nathanw 			if ((msg & 0x80) && !(msg & 0x18)) {	/* IDENTIFY */
   1237  1.47.8.2  nathanw 				PID("hmessage10");
   1238  1.47.8.2  nathanw 				ack_message();
   1239  1.47.8.2  nathanw 				return (0);
   1240  1.47.8.2  nathanw 			} else {
   1241  1.47.8.2  nathanw 				ncr_tprint(reqp,
   1242  1.47.8.2  nathanw 					   "Unknown message %x.  Rejecting.\n",
   1243  1.47.8.2  nathanw 					   msg);
   1244  1.47.8.2  nathanw 				nack_message(reqp, MSG_MESSAGE_REJECT);
   1245  1.47.8.2  nathanw 			}
   1246  1.47.8.2  nathanw 			return (-1);
   1247  1.47.8.2  nathanw 	}
   1248  1.47.8.2  nathanw 	PID("hmessage11");
   1249  1.47.8.2  nathanw 	return (-1);
   1250  1.47.8.2  nathanw }
   1251  1.47.8.2  nathanw 
   1252  1.47.8.2  nathanw /*
   1253  1.47.8.2  nathanw  * Handle reselection. If a valid reconnection occurs, connected
   1254  1.47.8.2  nathanw  * points at the reconnected command. The command is removed from the
   1255  1.47.8.2  nathanw  * disconnected queue.
   1256  1.47.8.2  nathanw  */
   1257  1.47.8.2  nathanw static void
   1258  1.47.8.2  nathanw reselect(sc)
   1259  1.47.8.2  nathanw struct ncr_softc *sc;
   1260  1.47.8.2  nathanw {
   1261  1.47.8.2  nathanw 	u_char	phase;
   1262  1.47.8.2  nathanw 	u_long	len;
   1263  1.47.8.2  nathanw 	u_char	msg;
   1264  1.47.8.2  nathanw 	u_char	target_mask;
   1265  1.47.8.2  nathanw 	int	abort = 0;
   1266  1.47.8.2  nathanw 	SC_REQ	*tmp, *prev;
   1267  1.47.8.2  nathanw 
   1268  1.47.8.2  nathanw 	PID("reselect1");
   1269  1.47.8.2  nathanw 	target_mask = GET_5380_REG(NCR5380_DATA) & ~SC_HOST_ID;
   1270  1.47.8.2  nathanw 
   1271  1.47.8.2  nathanw 	/*
   1272  1.47.8.2  nathanw 	 * At this point, we have detected that our SCSI-id is on the bus,
   1273  1.47.8.2  nathanw 	 * SEL is true and BSY was false for at least one bus settle
   1274  1.47.8.2  nathanw 	 * delay (400 ns.).
   1275  1.47.8.2  nathanw 	 * We must assert BSY ourselves, until the target drops the SEL signal.
   1276  1.47.8.2  nathanw 	 * The SCSI-spec specifies no maximum time for this, so we have to
   1277  1.47.8.2  nathanw 	 * choose something long enough to suit all targets.
   1278  1.47.8.2  nathanw 	 */
   1279  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_ICOM, SC_A_BSY);
   1280  1.47.8.2  nathanw 	len = 250000;
   1281  1.47.8.2  nathanw 	while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) && (len > 0)) {
   1282  1.47.8.2  nathanw 		delay(1);
   1283  1.47.8.2  nathanw 		len--;
   1284  1.47.8.2  nathanw 	}
   1285  1.47.8.2  nathanw 	if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) {
   1286  1.47.8.2  nathanw 		/* Damn SEL isn't dropping */
   1287  1.47.8.2  nathanw 		scsi_reset_verbose(sc, "Target won't drop SEL during Reselect");
   1288  1.47.8.2  nathanw 		return;
   1289  1.47.8.2  nathanw 	}
   1290  1.47.8.2  nathanw 
   1291  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_ICOM, 0);
   1292  1.47.8.2  nathanw 
   1293  1.47.8.2  nathanw 	/*
   1294  1.47.8.2  nathanw 	 * Check if the reselection is still valid. Check twice because
   1295  1.47.8.2  nathanw 	 * of possible line glitches - cheaper than delay(1) and we need
   1296  1.47.8.2  nathanw 	 * only a few nanoseconds.
   1297  1.47.8.2  nathanw 	 */
   1298  1.47.8.2  nathanw 	if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
   1299  1.47.8.2  nathanw 	    if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
   1300  1.47.8.2  nathanw 		ncr_aprint(sc, "Stepped into the reselection timeout\n");
   1301  1.47.8.2  nathanw 		return;
   1302  1.47.8.2  nathanw 	    }
   1303  1.47.8.2  nathanw 	}
   1304  1.47.8.2  nathanw 
   1305  1.47.8.2  nathanw 	/*
   1306  1.47.8.2  nathanw 	 * Get the expected identify message.
   1307  1.47.8.2  nathanw 	 */
   1308  1.47.8.2  nathanw 	phase = PH_MSGIN;
   1309  1.47.8.2  nathanw 	len   = 1;
   1310  1.47.8.2  nathanw 	transfer_pio(&phase, &msg, &len, 0);
   1311  1.47.8.2  nathanw 	if (len || !MSG_ISIDENTIFY(msg)) {
   1312  1.47.8.2  nathanw 		ncr_aprint(sc, "Expecting IDENTIFY, got 0x%x\n", msg);
   1313  1.47.8.2  nathanw 		abort = 1;
   1314  1.47.8.2  nathanw 		tmp = NULL;
   1315  1.47.8.2  nathanw 	}
   1316  1.47.8.2  nathanw 	else {
   1317  1.47.8.2  nathanw 	    /*
   1318  1.47.8.2  nathanw 	     * Find the command reconnecting
   1319  1.47.8.2  nathanw 	     */
   1320  1.47.8.2  nathanw 	    for (tmp = discon_q, prev = NULL; tmp; prev = tmp, tmp = tmp->next){
   1321  1.47.8.2  nathanw 		if (target_mask == (1 << tmp->targ_id)) {
   1322  1.47.8.2  nathanw 			if (prev)
   1323  1.47.8.2  nathanw 				prev->next = tmp->next;
   1324  1.47.8.2  nathanw 			else discon_q = tmp->next;
   1325  1.47.8.2  nathanw 			tmp->next = NULL;
   1326  1.47.8.2  nathanw 			break;
   1327  1.47.8.2  nathanw 		}
   1328  1.47.8.2  nathanw 	    }
   1329  1.47.8.2  nathanw 	    if (tmp == NULL) {
   1330  1.47.8.2  nathanw 		ncr_aprint(sc, "No disconnected job for targetmask %x\n",
   1331  1.47.8.2  nathanw 								target_mask);
   1332  1.47.8.2  nathanw 		abort = 1;
   1333  1.47.8.2  nathanw 	    }
   1334  1.47.8.2  nathanw 	}
   1335  1.47.8.2  nathanw 	if (abort) {
   1336  1.47.8.2  nathanw 		msg   = MSG_ABORT;
   1337  1.47.8.2  nathanw 		len   = 1;
   1338  1.47.8.2  nathanw 		phase = PH_MSGOUT;
   1339  1.47.8.2  nathanw 
   1340  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
   1341  1.47.8.2  nathanw 		if (transfer_pio(&phase, &msg, &len, 0) || len)
   1342  1.47.8.2  nathanw 			scsi_reset_verbose(sc, "Failure to abort reselection");
   1343  1.47.8.2  nathanw 	}
   1344  1.47.8.2  nathanw 	else {
   1345  1.47.8.2  nathanw 		connected = tmp;
   1346  1.47.8.2  nathanw #ifdef DBG_REQ
   1347  1.47.8.2  nathanw 		if (dbg_target_mask & (1 << tmp->targ_id))
   1348  1.47.8.2  nathanw 			show_request(tmp, "RECON");
   1349  1.47.8.2  nathanw #endif
   1350  1.47.8.2  nathanw 	}
   1351  1.47.8.2  nathanw 	PID("reselect2");
   1352  1.47.8.2  nathanw }
   1353  1.47.8.2  nathanw 
   1354  1.47.8.2  nathanw /*
   1355  1.47.8.2  nathanw  * Transfer data in a given phase using programmed I/O.
   1356  1.47.8.2  nathanw  * Returns -1 when a different phase is entered without transferring the
   1357  1.47.8.2  nathanw  * maximum number of bytes, 0 if all bytes transferred or exit is in the same
   1358  1.47.8.2  nathanw  * phase.
   1359  1.47.8.2  nathanw  */
   1360  1.47.8.2  nathanw static int
   1361  1.47.8.2  nathanw transfer_pio(phase, data, len, dont_drop_ack)
   1362  1.47.8.2  nathanw u_char	*phase;
   1363  1.47.8.2  nathanw u_char	*data;
   1364  1.47.8.2  nathanw u_long	*len;
   1365  1.47.8.2  nathanw int	dont_drop_ack;
   1366  1.47.8.2  nathanw {
   1367  1.47.8.2  nathanw 	u_int	cnt = *len;
   1368  1.47.8.2  nathanw 	u_char	ph  = *phase;
   1369  1.47.8.2  nathanw 	u_char	tmp, new_icom;
   1370  1.47.8.2  nathanw 
   1371  1.47.8.2  nathanw 	DBG_PIOPRINT ("SCSI: transfer_pio start: phase: %d, len: %d\n", ph,cnt);
   1372  1.47.8.2  nathanw 	PID("tpio1");
   1373  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_TCOM, ph);
   1374  1.47.8.2  nathanw 	do {
   1375  1.47.8.2  nathanw 	    if (!wait_req_true()) {
   1376  1.47.8.2  nathanw 		DBG_PIOPRINT ("SCSI: transfer_pio: missing REQ\n", 0, 0);
   1377  1.47.8.2  nathanw 		break;
   1378  1.47.8.2  nathanw 	    }
   1379  1.47.8.2  nathanw 	    if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != ph) {
   1380  1.47.8.2  nathanw 		DBG_PIOPRINT ("SCSI: transfer_pio: phase mismatch\n", 0, 0);
   1381  1.47.8.2  nathanw 		break;
   1382  1.47.8.2  nathanw 	    }
   1383  1.47.8.2  nathanw 	    if (PH_IN(ph)) {
   1384  1.47.8.2  nathanw 		*data++ = GET_5380_REG(NCR5380_DATA);
   1385  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_ICOM, SC_A_ACK);
   1386  1.47.8.2  nathanw 		if ((cnt == 1) && dont_drop_ack)
   1387  1.47.8.2  nathanw 			new_icom = SC_A_ACK;
   1388  1.47.8.2  nathanw 		else new_icom = 0;
   1389  1.47.8.2  nathanw 	    }
   1390  1.47.8.2  nathanw 	    else {
   1391  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_DATA, *data++);
   1392  1.47.8.2  nathanw 
   1393  1.47.8.2  nathanw 		/*
   1394  1.47.8.2  nathanw 		 * The SCSI-standard suggests that in the 'MESSAGE OUT' phase,
   1395  1.47.8.2  nathanw 		 * the initiator should drop ATN on the last byte of the
   1396  1.47.8.2  nathanw 		 * message phase after REQ has been asserted for the handshake
   1397  1.47.8.2  nathanw 		 * but before the initiator raises ACK.
   1398  1.47.8.2  nathanw 		 */
   1399  1.47.8.2  nathanw 		if (!( (ph == PH_MSGOUT) && (cnt > 1) )) {
   1400  1.47.8.2  nathanw 			SET_5380_REG(NCR5380_ICOM, SC_ADTB);
   1401  1.47.8.2  nathanw 			SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ACK);
   1402  1.47.8.2  nathanw 			new_icom = 0;
   1403  1.47.8.2  nathanw 		}
   1404  1.47.8.2  nathanw 		else {
   1405  1.47.8.2  nathanw 			SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ATN);
   1406  1.47.8.2  nathanw 			SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN|SC_A_ACK);
   1407  1.47.8.2  nathanw 			new_icom = SC_A_ATN;
   1408  1.47.8.2  nathanw 		}
   1409  1.47.8.2  nathanw 	    }
   1410  1.47.8.2  nathanw 	    if (!wait_req_false()) {
   1411  1.47.8.2  nathanw 		DBG_PIOPRINT ("SCSI: transfer_pio - REQ not dropping\n", 0, 0);
   1412  1.47.8.2  nathanw 		break;
   1413  1.47.8.2  nathanw 	    }
   1414  1.47.8.2  nathanw 	    SET_5380_REG(NCR5380_ICOM, new_icom);
   1415  1.47.8.2  nathanw 
   1416  1.47.8.2  nathanw 	} while (--cnt);
   1417  1.47.8.2  nathanw 
   1418  1.47.8.2  nathanw 	if ((tmp = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
   1419  1.47.8.2  nathanw 		*phase = (tmp >> 2) & 7;
   1420  1.47.8.2  nathanw 	else *phase = NR_PHASE;
   1421  1.47.8.2  nathanw 	*len = cnt;
   1422  1.47.8.2  nathanw 	DBG_PIOPRINT ("SCSI: transfer_pio done: phase: %d, len: %d\n",
   1423  1.47.8.2  nathanw 								*phase, cnt);
   1424  1.47.8.2  nathanw 	PID("tpio2");
   1425  1.47.8.2  nathanw 	if (!cnt || (*phase == ph))
   1426  1.47.8.2  nathanw 		return (0);
   1427  1.47.8.2  nathanw 	return (-1);
   1428  1.47.8.2  nathanw }
   1429  1.47.8.2  nathanw 
   1430  1.47.8.2  nathanw #ifdef REAL_DMA
   1431  1.47.8.2  nathanw /*
   1432  1.47.8.2  nathanw  * Start a DMA-transfer on the device using the current pointers.
   1433  1.47.8.2  nathanw  * If 'poll' is true, the function busy-waits until DMA has completed.
   1434  1.47.8.2  nathanw  */
   1435  1.47.8.2  nathanw static void
   1436  1.47.8.2  nathanw transfer_dma(reqp, phase, poll)
   1437  1.47.8.2  nathanw SC_REQ	*reqp;
   1438  1.47.8.2  nathanw u_int	phase;
   1439  1.47.8.2  nathanw int	poll;
   1440  1.47.8.2  nathanw {
   1441  1.47.8.2  nathanw 	int	dma_done;
   1442  1.47.8.2  nathanw 	u_char	mbase = 0;
   1443  1.47.8.2  nathanw 	int	sps;
   1444  1.47.8.2  nathanw 
   1445  1.47.8.2  nathanw again:
   1446  1.47.8.2  nathanw 	PID("tdma1");
   1447  1.47.8.2  nathanw 
   1448  1.47.8.2  nathanw 	/*
   1449  1.47.8.2  nathanw 	 * We should be in phase, otherwise we are not allowed to
   1450  1.47.8.2  nathanw 	 * drive the bus.
   1451  1.47.8.2  nathanw 	 */
   1452  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_TCOM, phase);
   1453  1.47.8.2  nathanw 
   1454  1.47.8.2  nathanw 	/*
   1455  1.47.8.2  nathanw 	 * Defer interrupts until DMA is fully running.
   1456  1.47.8.2  nathanw 	 */
   1457  1.47.8.2  nathanw 	sps = splbio();
   1458  1.47.8.2  nathanw 
   1459  1.47.8.2  nathanw 	/*
   1460  1.47.8.2  nathanw 	 * Clear pending interrupts and parity errors.
   1461  1.47.8.2  nathanw 	 */
   1462  1.47.8.2  nathanw 	scsi_clr_ipend();
   1463  1.47.8.2  nathanw 
   1464  1.47.8.2  nathanw 	if (!poll) {
   1465  1.47.8.2  nathanw 		/*
   1466  1.47.8.2  nathanw 		 * Enable SCSI interrupts and set IN_DMA flag, set 'mbase'
   1467  1.47.8.2  nathanw 		 * to the interrupts we want enabled.
   1468  1.47.8.2  nathanw 		 */
   1469  1.47.8.2  nathanw 		scsi_ienable();
   1470  1.47.8.2  nathanw 		reqp->dr_flag |= DRIVER_IN_DMA;
   1471  1.47.8.2  nathanw 		mbase = SC_E_EOPI | SC_MON_BSY;
   1472  1.47.8.2  nathanw 	}
   1473  1.47.8.2  nathanw 	else scsi_idisable();
   1474  1.47.8.2  nathanw 	mbase |=  IMODE_BASE | SC_M_DMA;
   1475  1.47.8.2  nathanw 	scsi_dma_setup(reqp, phase, mbase);
   1476  1.47.8.2  nathanw 
   1477  1.47.8.2  nathanw 	splx(sps);
   1478  1.47.8.2  nathanw 
   1479  1.47.8.2  nathanw 	if (poll) {
   1480  1.47.8.2  nathanw 		/*
   1481  1.47.8.2  nathanw 		 * On polled-dma transfers, we wait here until the
   1482  1.47.8.2  nathanw 		 * 'end-of-dma' condition occurs.
   1483  1.47.8.2  nathanw 		 */
   1484  1.47.8.2  nathanw 		poll_edma(reqp);
   1485  1.47.8.2  nathanw 		if (!(dma_done = dma_ready()))
   1486  1.47.8.2  nathanw 			goto again;
   1487  1.47.8.2  nathanw 	}
   1488  1.47.8.2  nathanw 	PID("tdma2");
   1489  1.47.8.2  nathanw }
   1490  1.47.8.2  nathanw 
   1491  1.47.8.2  nathanw /*
   1492  1.47.8.2  nathanw  * Check results of a DMA data-transfer.
   1493  1.47.8.2  nathanw  */
   1494  1.47.8.2  nathanw static int
   1495  1.47.8.2  nathanw dma_ready()
   1496  1.47.8.2  nathanw {
   1497  1.47.8.2  nathanw 	SC_REQ	*reqp = connected;
   1498  1.47.8.2  nathanw 	int	dmstat, is_edma;
   1499  1.47.8.2  nathanw 	long	bytes_left, bytes_done;
   1500  1.47.8.2  nathanw 
   1501  1.47.8.2  nathanw 	is_edma = get_dma_result(reqp, &bytes_left);
   1502  1.47.8.2  nathanw 	dmstat  = GET_5380_REG(NCR5380_DMSTAT);
   1503  1.47.8.2  nathanw 
   1504  1.47.8.2  nathanw 	/*
   1505  1.47.8.2  nathanw 	 * Check if the call is sensible and not caused by any spurious
   1506  1.47.8.2  nathanw 	 * interrupt.
   1507  1.47.8.2  nathanw 	 */
   1508  1.47.8.2  nathanw 	if (!is_edma && !(dmstat & (SC_END_DMA|SC_BSY_ERR))
   1509  1.47.8.2  nathanw 		     && (dmstat & SC_PHS_MTCH) ) {
   1510  1.47.8.2  nathanw 		ncr_tprint(reqp, "dma_ready: spurious call "
   1511  1.47.8.2  nathanw 				 "(dm:%x,last_hit: %s)\n",
   1512  1.47.8.2  nathanw #ifdef DBG_PID
   1513  1.47.8.2  nathanw 					dmstat, last_hit[DBG_PID-1]);
   1514  1.47.8.2  nathanw #else
   1515  1.47.8.2  nathanw 					dmstat, "unknown");
   1516  1.47.8.2  nathanw #endif
   1517  1.47.8.2  nathanw 		return (0);
   1518  1.47.8.2  nathanw 	}
   1519  1.47.8.2  nathanw 
   1520  1.47.8.2  nathanw 	/*
   1521  1.47.8.2  nathanw 	 * Clear all (pending) interrupts.
   1522  1.47.8.2  nathanw 	 */
   1523  1.47.8.2  nathanw 	scsi_clr_ipend();
   1524  1.47.8.2  nathanw 
   1525  1.47.8.2  nathanw 	/*
   1526  1.47.8.2  nathanw 	 * Update various transfer-pointers/lengths
   1527  1.47.8.2  nathanw 	 */
   1528  1.47.8.2  nathanw 	bytes_done = reqp->dm_cur->dm_count - bytes_left;
   1529  1.47.8.2  nathanw 
   1530  1.47.8.2  nathanw 	if ((reqp->dr_flag & DRIVER_BOUNCING) && (PH_IN(reqp->phase))) {
   1531  1.47.8.2  nathanw 		/*
   1532  1.47.8.2  nathanw 		 * Copy the bytes read until now from the bounce buffer
   1533  1.47.8.2  nathanw 		 * to the 'real' destination. Flush the data-cache
   1534  1.47.8.2  nathanw 		 * before copying.
   1535  1.47.8.2  nathanw 		 */
   1536  1.47.8.2  nathanw 		PCIA();
   1537  1.47.8.2  nathanw 		bcopy(reqp->bouncerp, reqp->xdata_ptr, bytes_done);
   1538  1.47.8.2  nathanw 		reqp->bouncerp += bytes_done;
   1539  1.47.8.2  nathanw 	}
   1540  1.47.8.2  nathanw 
   1541  1.47.8.2  nathanw 	reqp->xdata_ptr  = &reqp->xdata_ptr[bytes_done];	/* XXX */
   1542  1.47.8.2  nathanw 	reqp->xdata_len -= bytes_done;				/* XXX */
   1543  1.47.8.2  nathanw 	if ((reqp->dm_cur->dm_count -= bytes_done) == 0)
   1544  1.47.8.2  nathanw 		reqp->dm_cur++;
   1545  1.47.8.2  nathanw 	else reqp->dm_cur->dm_addr += bytes_done;
   1546  1.47.8.2  nathanw 
   1547  1.47.8.2  nathanw 	if (PH_IN(reqp->phase) && (dmstat & SC_PAR_ERR)) {
   1548  1.47.8.2  nathanw 		if (!(ncr5380_no_parchk & (1 << reqp->targ_id))) {
   1549  1.47.8.2  nathanw 			ncr_tprint(reqp, "parity error in data-phase\n");
   1550  1.47.8.2  nathanw 			reqp->xs->error = XS_TIMEOUT;
   1551  1.47.8.2  nathanw 		}
   1552  1.47.8.2  nathanw 	}
   1553  1.47.8.2  nathanw 
   1554  1.47.8.2  nathanw 	/*
   1555  1.47.8.2  nathanw 	 * DMA mode should always be reset even when we will continue with the
   1556  1.47.8.2  nathanw 	 * next chain. It is also essential to clear the MON_BUSY because
   1557  1.47.8.2  nathanw 	 * when LOST_BUSY is unexpectedly set, we will not be able to drive
   1558  1.47.8.2  nathanw 	 * the bus....
   1559  1.47.8.2  nathanw 	 */
   1560  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_MODE, IMODE_BASE);
   1561  1.47.8.2  nathanw 
   1562  1.47.8.2  nathanw 
   1563  1.47.8.2  nathanw 	if ((dmstat & SC_BSY_ERR) || !(dmstat & SC_PHS_MTCH)
   1564  1.47.8.2  nathanw 		 || (reqp->dm_cur > reqp->dm_last) || (reqp->xs->error)) {
   1565  1.47.8.2  nathanw 
   1566  1.47.8.2  nathanw 		/*
   1567  1.47.8.2  nathanw 		 * Tell interrupt functions DMA mode has ended.
   1568  1.47.8.2  nathanw 		 */
   1569  1.47.8.2  nathanw 		reqp->dr_flag &= ~DRIVER_IN_DMA;
   1570  1.47.8.2  nathanw 
   1571  1.47.8.2  nathanw 		/*
   1572  1.47.8.2  nathanw 		 * Clear mode and icom
   1573  1.47.8.2  nathanw 		 */
   1574  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
   1575  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_ICOM, 0);
   1576  1.47.8.2  nathanw 
   1577  1.47.8.2  nathanw 		if (dmstat & SC_BSY_ERR) {
   1578  1.47.8.2  nathanw 			if (!reqp->xs->error)
   1579  1.47.8.2  nathanw 				reqp->xs->error = XS_TIMEOUT;
   1580  1.47.8.2  nathanw 			finish_req(reqp);
   1581  1.47.8.2  nathanw 			PID("dma_ready1");
   1582  1.47.8.2  nathanw 			return (1);
   1583  1.47.8.2  nathanw 		}
   1584  1.47.8.2  nathanw 
   1585  1.47.8.2  nathanw 		if (reqp->xs->error != 0) {
   1586  1.47.8.2  nathanw ncr_tprint(reqp, "dma-ready: code = %d\n", reqp->xs->error); /* LWP */
   1587  1.47.8.2  nathanw 			reqp->msgout = MSG_ABORT;
   1588  1.47.8.2  nathanw 			SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
   1589  1.47.8.2  nathanw 		}
   1590  1.47.8.2  nathanw 		PID("dma_ready2");
   1591  1.47.8.2  nathanw 		return (1);
   1592  1.47.8.2  nathanw 	}
   1593  1.47.8.2  nathanw 	return (0);
   1594  1.47.8.2  nathanw }
   1595  1.47.8.2  nathanw #endif /* REAL_DMA */
   1596  1.47.8.2  nathanw 
   1597  1.47.8.2  nathanw static int
   1598  1.47.8.2  nathanw check_autosense(reqp, linked)
   1599  1.47.8.2  nathanw SC_REQ	*reqp;
   1600  1.47.8.2  nathanw int	linked;
   1601  1.47.8.2  nathanw {
   1602  1.47.8.2  nathanw 	int	sps;
   1603  1.47.8.2  nathanw 
   1604  1.47.8.2  nathanw 	/*
   1605  1.47.8.2  nathanw 	 * If this is the driver's Link Check for this target, ignore
   1606  1.47.8.2  nathanw 	 * the results of the command.  All we care about is whether we
   1607  1.47.8.2  nathanw 	 * got here from a LINK_CMD_COMPLETE or CMD_COMPLETE message.
   1608  1.47.8.2  nathanw 	 */
   1609  1.47.8.2  nathanw 	PID("linkcheck");
   1610  1.47.8.2  nathanw 	if (reqp->dr_flag & DRIVER_LINKCHK) {
   1611  1.47.8.2  nathanw 		if (linked)
   1612  1.47.8.2  nathanw 			ncr_will_link |= 1<<reqp->targ_id;
   1613  1.47.8.2  nathanw 		else ncr_tprint(reqp, "Does not support linked commands\n");
   1614  1.47.8.2  nathanw 		return (0);
   1615  1.47.8.2  nathanw 	}
   1616  1.47.8.2  nathanw 	/*
   1617  1.47.8.2  nathanw 	 * If we not executing an auto-sense and the status code
   1618  1.47.8.2  nathanw 	 * is request-sense, we automatically issue a request
   1619  1.47.8.2  nathanw 	 * sense command.
   1620  1.47.8.2  nathanw 	 */
   1621  1.47.8.2  nathanw 	PID("cautos1");
   1622  1.47.8.2  nathanw 	if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
   1623  1.47.8.2  nathanw 		switch (reqp->status & SCSMASK) {
   1624  1.47.8.2  nathanw 		    case SCSCHKC:
   1625  1.47.8.2  nathanw 			bcopy(sense_cmd, &reqp->xcmd, sizeof(sense_cmd));
   1626  1.47.8.2  nathanw 			reqp->xdata_ptr = (u_char *)&reqp->xs->sense.scsi_sense;
   1627  1.47.8.2  nathanw 			reqp->xdata_len = sizeof(reqp->xs->sense.scsi_sense);
   1628  1.47.8.2  nathanw 			reqp->dr_flag  |= DRIVER_AUTOSEN;
   1629  1.47.8.2  nathanw 			reqp->dr_flag  &= ~DRIVER_DMAOK;
   1630  1.47.8.2  nathanw 			if (!linked) {
   1631  1.47.8.2  nathanw 				sps = splbio();
   1632  1.47.8.2  nathanw 				reqp->next = issue_q;
   1633  1.47.8.2  nathanw 				issue_q    = reqp;
   1634  1.47.8.2  nathanw 				splx(sps);
   1635  1.47.8.2  nathanw 			}
   1636  1.47.8.2  nathanw 			else reqp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1;
   1637  1.47.8.2  nathanw 
   1638  1.47.8.2  nathanw #ifdef DBG_REQ
   1639  1.47.8.2  nathanw 			bzero(reqp->xdata_ptr, reqp->xdata_len);
   1640  1.47.8.2  nathanw 			if (dbg_target_mask & (1 << reqp->targ_id))
   1641  1.47.8.2  nathanw 				show_request(reqp, "AUTO-SENSE");
   1642  1.47.8.2  nathanw #endif
   1643  1.47.8.2  nathanw 			PID("cautos2");
   1644  1.47.8.2  nathanw 			return (-1);
   1645  1.47.8.2  nathanw 		    case SCSBUSY:
   1646  1.47.8.2  nathanw 			reqp->xs->error = XS_BUSY;
   1647  1.47.8.2  nathanw 			return (0);
   1648  1.47.8.2  nathanw 		}
   1649  1.47.8.2  nathanw 	}
   1650  1.47.8.2  nathanw 	else {
   1651  1.47.8.2  nathanw 		/*
   1652  1.47.8.2  nathanw 		 * An auto-sense has finished
   1653  1.47.8.2  nathanw 		 */
   1654  1.47.8.2  nathanw 		if ((reqp->status & SCSMASK) != SCSGOOD)
   1655  1.47.8.2  nathanw 			reqp->xs->error = XS_DRIVER_STUFFUP; /* SC_E_AUTOSEN; */
   1656  1.47.8.2  nathanw 		else reqp->xs->error = XS_SENSE;
   1657  1.47.8.2  nathanw 		reqp->status = SCSCHKC;
   1658  1.47.8.2  nathanw 	}
   1659  1.47.8.2  nathanw 	PID("cautos3");
   1660  1.47.8.2  nathanw 	return (0);
   1661  1.47.8.2  nathanw }
   1662  1.47.8.2  nathanw 
   1663  1.47.8.2  nathanw static int
   1664  1.47.8.2  nathanw reach_msg_out(sc, len)
   1665  1.47.8.2  nathanw struct ncr_softc *sc;
   1666  1.47.8.2  nathanw u_long		 len;
   1667  1.47.8.2  nathanw {
   1668  1.47.8.2  nathanw 	u_char	phase;
   1669  1.47.8.2  nathanw 	u_char	data;
   1670  1.47.8.2  nathanw 	u_long	n = len;
   1671  1.47.8.2  nathanw 
   1672  1.47.8.2  nathanw 	ncr_aprint(sc, "Trying to reach Message-out phase\n");
   1673  1.47.8.2  nathanw 	if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
   1674  1.47.8.2  nathanw 		phase = (phase >> 2) & 7;
   1675  1.47.8.2  nathanw 	else return (-1);
   1676  1.47.8.2  nathanw 	ncr_aprint(sc, "Trying to reach Message-out phase, now: %d\n", phase);
   1677  1.47.8.2  nathanw 	if (phase == PH_MSGOUT)
   1678  1.47.8.2  nathanw 		return (0);
   1679  1.47.8.2  nathanw 
   1680  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_TCOM, phase);
   1681  1.47.8.2  nathanw 
   1682  1.47.8.2  nathanw 	do {
   1683  1.47.8.2  nathanw 		if (!wait_req_true())
   1684  1.47.8.2  nathanw 			break;
   1685  1.47.8.2  nathanw 		if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != phase)
   1686  1.47.8.2  nathanw 			break;
   1687  1.47.8.2  nathanw 		if (PH_IN(phase)) {
   1688  1.47.8.2  nathanw 			data = GET_5380_REG(NCR5380_DATA);
   1689  1.47.8.2  nathanw 			SET_5380_REG(NCR5380_ICOM, SC_A_ACK | SC_A_ATN);
   1690  1.47.8.2  nathanw 		}
   1691  1.47.8.2  nathanw 		else {
   1692  1.47.8.2  nathanw 			SET_5380_REG(NCR5380_DATA, 0);
   1693  1.47.8.2  nathanw 			SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ACK|SC_A_ATN);
   1694  1.47.8.2  nathanw 		}
   1695  1.47.8.2  nathanw 		if (!wait_req_false())
   1696  1.47.8.2  nathanw 			break;
   1697  1.47.8.2  nathanw 		SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
   1698  1.47.8.2  nathanw 	} while (--n);
   1699  1.47.8.2  nathanw 
   1700  1.47.8.2  nathanw 	if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ) {
   1701  1.47.8.2  nathanw 		phase = (phase >> 2) & 7;
   1702  1.47.8.2  nathanw 		if (phase == PH_MSGOUT) {
   1703  1.47.8.2  nathanw 			ncr_aprint(sc, "Message-out phase reached after "
   1704  1.47.8.2  nathanw 					"%ld bytes.\n", len - n);
   1705  1.47.8.2  nathanw 			return (0);
   1706  1.47.8.2  nathanw 		}
   1707  1.47.8.2  nathanw 	}
   1708  1.47.8.2  nathanw 	return (-1);
   1709  1.47.8.2  nathanw }
   1710  1.47.8.2  nathanw 
   1711  1.47.8.2  nathanw void
   1712  1.47.8.2  nathanw scsi_reset()
   1713  1.47.8.2  nathanw {
   1714  1.47.8.2  nathanw 	SC_REQ	*tmp, *next;
   1715  1.47.8.2  nathanw 	int	sps;
   1716  1.47.8.2  nathanw 
   1717  1.47.8.2  nathanw 	PID("scsi_reset1");
   1718  1.47.8.2  nathanw 	sps = splbio();
   1719  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_ICOM, SC_A_RST);
   1720  1.47.8.2  nathanw 	delay(100);
   1721  1.47.8.2  nathanw 	SET_5380_REG(NCR5380_ICOM, 0);
   1722  1.47.8.2  nathanw 	scsi_clr_ipend();
   1723  1.47.8.2  nathanw 
   1724  1.47.8.2  nathanw 	/*
   1725  1.47.8.2  nathanw 	 * None of the jobs in the discon_q will ever be reconnected,
   1726  1.47.8.2  nathanw 	 * notify this to the higher level code.
   1727  1.47.8.2  nathanw 	 */
   1728  1.47.8.2  nathanw 	for (tmp = discon_q; tmp ;) {
   1729  1.47.8.2  nathanw 		next = tmp->next;
   1730  1.47.8.2  nathanw 		tmp->next = NULL;
   1731  1.47.8.2  nathanw 		tmp->xs->error = XS_TIMEOUT;
   1732  1.47.8.2  nathanw 		busy &= ~(1 << tmp->targ_id);
   1733  1.47.8.2  nathanw 		finish_req(tmp);
   1734  1.47.8.2  nathanw 		tmp = next;
   1735  1.47.8.2  nathanw 	}
   1736  1.47.8.2  nathanw 	discon_q = NULL;
   1737  1.47.8.2  nathanw 
   1738  1.47.8.2  nathanw 	/*
   1739  1.47.8.2  nathanw 	 * The current job will never finish either.
   1740  1.47.8.2  nathanw 	 * The problem is that we can't finish the job because an instance
   1741  1.47.8.2  nathanw 	 * of main is running on it. Our best guess is that the job is currently
   1742  1.47.8.2  nathanw 	 * doing REAL-DMA. In that case 'dma_ready()' should correctly finish
   1743  1.47.8.2  nathanw 	 * the job because it detects BSY-loss.
   1744  1.47.8.2  nathanw 	 */
   1745  1.47.8.2  nathanw 	if ((tmp = connected) != NULL) {
   1746  1.47.8.2  nathanw 		if (tmp->dr_flag & DRIVER_IN_DMA) {
   1747  1.47.8.2  nathanw 			tmp->xs->error = XS_DRIVER_STUFFUP;
   1748  1.47.8.2  nathanw #ifdef REAL_DMA
   1749  1.47.8.2  nathanw 			dma_ready();
   1750  1.47.8.2  nathanw #endif
   1751  1.47.8.2  nathanw 		}
   1752  1.47.8.2  nathanw 	}
   1753  1.47.8.2  nathanw 	splx(sps);
   1754  1.47.8.2  nathanw 	PID("scsi_reset2");
   1755  1.47.8.2  nathanw 
   1756  1.47.8.2  nathanw 	/*
   1757  1.47.8.2  nathanw 	 * Give the attached devices some time to handle the reset. This
   1758  1.47.8.2  nathanw 	 * value is arbitrary but should be relatively long.
   1759  1.47.8.2  nathanw 	 */
   1760  1.47.8.2  nathanw 	delay(100000);
   1761  1.47.8.2  nathanw }
   1762  1.47.8.2  nathanw 
   1763  1.47.8.2  nathanw static void
   1764  1.47.8.2  nathanw scsi_reset_verbose(sc, why)
   1765  1.47.8.2  nathanw struct ncr_softc *sc;
   1766  1.47.8.2  nathanw const char	 *why;
   1767  1.47.8.2  nathanw {
   1768  1.47.8.2  nathanw 	ncr_aprint(sc, "Resetting SCSI-bus (%s)\n", why);
   1769  1.47.8.2  nathanw 
   1770  1.47.8.2  nathanw 	scsi_reset();
   1771  1.47.8.2  nathanw }
   1772  1.47.8.2  nathanw 
   1773  1.47.8.2  nathanw /*
   1774  1.47.8.2  nathanw  * Check validity of the IRQ set by the 5380. If the interrupt is valid,
   1775  1.47.8.2  nathanw  * the appropriate action is carried out (reselection or DMA ready) and
   1776  1.47.8.2  nathanw  * INTR_RESEL or INTR_DMA is returned. Otherwise a console notice is written
   1777  1.47.8.2  nathanw  * and INTR_SPURIOUS is returned.
   1778  1.47.8.2  nathanw  */
   1779  1.47.8.2  nathanw static int
   1780  1.47.8.2  nathanw check_intr(sc)
   1781  1.47.8.2  nathanw struct ncr_softc *sc;
   1782  1.47.8.2  nathanw {
   1783  1.47.8.2  nathanw 	SC_REQ	*reqp;
   1784  1.47.8.2  nathanw 
   1785  1.47.8.2  nathanw 	if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
   1786  1.47.8.2  nathanw 							==(SC_S_SEL|SC_S_IO))
   1787  1.47.8.2  nathanw 		return (INTR_RESEL);
   1788  1.47.8.2  nathanw 	else {
   1789  1.47.8.2  nathanw 		if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)){
   1790  1.47.8.2  nathanw 			reqp->dr_flag &= ~DRIVER_IN_DMA;
   1791  1.47.8.2  nathanw 			return (INTR_DMA);
   1792  1.47.8.2  nathanw 		}
   1793  1.47.8.2  nathanw 	}
   1794  1.47.8.2  nathanw 	scsi_clr_ipend();
   1795  1.47.8.2  nathanw 	printf("-->");
   1796  1.47.8.2  nathanw 	scsi_show();
   1797  1.47.8.2  nathanw 	ncr_aprint(sc, "Spurious interrupt.\n");
   1798  1.47.8.2  nathanw 	return (INTR_SPURIOUS);
   1799  1.47.8.2  nathanw }
   1800  1.47.8.2  nathanw 
   1801  1.47.8.2  nathanw #ifdef REAL_DMA
   1802  1.47.8.2  nathanw /*
   1803  1.47.8.2  nathanw  * Check if DMA can be used for this request. This function also builds
   1804  1.47.8.2  nathanw  * the dma-chain.
   1805  1.47.8.2  nathanw  */
   1806  1.47.8.2  nathanw static int
   1807  1.47.8.2  nathanw scsi_dmaok(reqp)
   1808  1.47.8.2  nathanw SC_REQ	*reqp;
   1809  1.47.8.2  nathanw {
   1810  1.47.8.2  nathanw 	u_long			phy_buf;
   1811  1.47.8.2  nathanw 	u_long			phy_len;
   1812  1.47.8.2  nathanw 	void			*req_addr;
   1813  1.47.8.2  nathanw 	u_long			req_len;
   1814  1.47.8.2  nathanw 	struct dma_chain	*dm;
   1815  1.47.8.2  nathanw 
   1816  1.47.8.2  nathanw 	/*
   1817  1.47.8.2  nathanw 	 * Initialize locals and requests' DMA-chain.
   1818  1.47.8.2  nathanw 	 */
   1819  1.47.8.2  nathanw 	req_len        = reqp->xdata_len;
   1820  1.47.8.2  nathanw 	req_addr       = (void*)reqp->xdata_ptr;
   1821  1.47.8.2  nathanw 	dm             = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
   1822  1.47.8.2  nathanw 	dm->dm_count   = dm->dm_addr = 0;
   1823  1.47.8.2  nathanw 	reqp->dr_flag &= ~DRIVER_BOUNCING;
   1824  1.47.8.2  nathanw 
   1825  1.47.8.2  nathanw 	/*
   1826  1.47.8.2  nathanw 	 * Do not accept zero length DMA.
   1827  1.47.8.2  nathanw 	 */
   1828  1.47.8.2  nathanw 	if (req_len == 0)
   1829  1.47.8.2  nathanw 		return (0);
   1830  1.47.8.2  nathanw 
   1831  1.47.8.2  nathanw 	/*
   1832  1.47.8.2  nathanw 	 * LWP: I think that this restriction is not strictly nessecary.
   1833  1.47.8.2  nathanw 	 */
   1834  1.47.8.2  nathanw 	if ((req_len & 0x1) || ((u_int)req_addr & 0x3))
   1835  1.47.8.2  nathanw 		return (0);
   1836  1.47.8.2  nathanw 
   1837  1.47.8.2  nathanw 	/*
   1838  1.47.8.2  nathanw 	 * Build the DMA-chain.
   1839  1.47.8.2  nathanw 	 */
   1840  1.47.8.2  nathanw 	dm->dm_addr = phy_buf = kvtop(req_addr);
   1841  1.47.8.2  nathanw 	while (req_len) {
   1842  1.47.8.2  nathanw 		if (req_len < (phy_len = NBPG - m68k_page_offset(req_addr)))
   1843  1.47.8.2  nathanw 			phy_len = req_len;
   1844  1.47.8.2  nathanw 
   1845  1.47.8.2  nathanw 		req_addr     += phy_len;
   1846  1.47.8.2  nathanw 		req_len      -= phy_len;
   1847  1.47.8.2  nathanw 		dm->dm_count += phy_len;
   1848  1.47.8.2  nathanw 
   1849  1.47.8.2  nathanw 		if (req_len) {
   1850  1.47.8.2  nathanw 			u_long	tmp = kvtop(req_addr);
   1851  1.47.8.2  nathanw 
   1852  1.47.8.2  nathanw 			if ((phy_buf + phy_len) != tmp) {
   1853  1.47.8.2  nathanw 			    if (wrong_dma_range(reqp, dm)) {
   1854  1.47.8.2  nathanw 				if (reqp->dr_flag & DRIVER_BOUNCING)
   1855  1.47.8.2  nathanw 					goto bounceit;
   1856  1.47.8.2  nathanw 				return (0);
   1857  1.47.8.2  nathanw 			    }
   1858  1.47.8.2  nathanw 
   1859  1.47.8.2  nathanw 			    if (++dm >= &reqp->dm_chain[MAXDMAIO]) {
   1860  1.47.8.2  nathanw 				ncr_tprint(reqp,"dmaok: DMA chain too long!\n");
   1861  1.47.8.2  nathanw 				return (0);
   1862  1.47.8.2  nathanw 			    }
   1863  1.47.8.2  nathanw 			    dm->dm_count = 0;
   1864  1.47.8.2  nathanw 			    dm->dm_addr  = tmp;
   1865  1.47.8.2  nathanw 			}
   1866  1.47.8.2  nathanw 			phy_buf = tmp;
   1867  1.47.8.2  nathanw 		}
   1868  1.47.8.2  nathanw 	}
   1869  1.47.8.2  nathanw         if (wrong_dma_range(reqp, dm)) {
   1870  1.47.8.2  nathanw 		if (reqp->dr_flag & DRIVER_BOUNCING)
   1871  1.47.8.2  nathanw 			goto bounceit;
   1872  1.47.8.2  nathanw 		return (0);
   1873  1.47.8.2  nathanw 	}
   1874  1.47.8.2  nathanw 	reqp->dm_last = dm;
   1875  1.47.8.2  nathanw 	return (1);
   1876  1.47.8.2  nathanw 
   1877  1.47.8.2  nathanw bounceit:
   1878  1.47.8.2  nathanw 	if ((reqp->bounceb = alloc_bounceb(reqp->xdata_len)) == NULL) {
   1879  1.47.8.2  nathanw 		/*
   1880  1.47.8.2  nathanw 		 * If we can't get a bounce buffer, forget DMA
   1881  1.47.8.2  nathanw 		 */
   1882  1.47.8.2  nathanw 		reqp->dr_flag &= ~DRIVER_BOUNCING;
   1883  1.47.8.2  nathanw 		return(0);
   1884  1.47.8.2  nathanw 	}
   1885  1.47.8.2  nathanw 	/*
   1886  1.47.8.2  nathanw 	 * Initialize a single DMA-range containing the bounced request
   1887  1.47.8.2  nathanw 	 */
   1888  1.47.8.2  nathanw 	dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
   1889  1.47.8.2  nathanw 	dm->dm_addr    = kvtop(reqp->bounceb);
   1890  1.47.8.2  nathanw 	dm->dm_count   = reqp->xdata_len;
   1891  1.47.8.2  nathanw 	reqp->bouncerp = reqp->bounceb;
   1892  1.47.8.2  nathanw 
   1893  1.47.8.2  nathanw 	return (1);
   1894  1.47.8.2  nathanw }
   1895  1.47.8.2  nathanw #endif /* REAL_DMA */
   1896  1.47.8.2  nathanw 
   1897  1.47.8.2  nathanw static void
   1898  1.47.8.2  nathanw run_main(sc)
   1899  1.47.8.2  nathanw struct ncr_softc *sc;
   1900  1.47.8.2  nathanw {
   1901  1.47.8.2  nathanw 	int	sps = splbio();
   1902  1.47.8.2  nathanw 
   1903  1.47.8.2  nathanw 	if (!main_running) {
   1904  1.47.8.2  nathanw 		/*
   1905  1.47.8.2  nathanw 		 * If shared resources are required, claim them
   1906  1.47.8.2  nathanw 		 * before entering 'scsi_main'. If we can't get them
   1907  1.47.8.2  nathanw 		 * now, assume 'run_main' will be called when the resource
   1908  1.47.8.2  nathanw 		 * becomes available.
   1909  1.47.8.2  nathanw 		 */
   1910  1.47.8.2  nathanw 		if (!claimed_dma()) {
   1911  1.47.8.2  nathanw 			splx(sps);
   1912  1.47.8.2  nathanw 			return;
   1913  1.47.8.2  nathanw 		}
   1914  1.47.8.2  nathanw 		main_running = 1;
   1915  1.47.8.2  nathanw 		splx(sps);
   1916  1.47.8.2  nathanw 		scsi_main(sc);
   1917  1.47.8.2  nathanw 	}
   1918  1.47.8.2  nathanw 	else splx(sps);
   1919  1.47.8.2  nathanw }
   1920  1.47.8.2  nathanw 
   1921  1.47.8.2  nathanw /*
   1922  1.47.8.2  nathanw  * Prefix message with full target info.
   1923  1.47.8.2  nathanw  */
   1924  1.47.8.2  nathanw static void
   1925  1.47.8.2  nathanw ncr_tprint(SC_REQ *reqp, char *fmt, ...)
   1926  1.47.8.2  nathanw {
   1927  1.47.8.2  nathanw 	va_list	ap;
   1928  1.47.8.2  nathanw 
   1929  1.47.8.2  nathanw 	va_start(ap, fmt);
   1930  1.47.8.2  nathanw 	scsipi_printaddr(reqp->xs->xs_periph);
   1931  1.47.8.2  nathanw 	vprintf(fmt, ap);
   1932  1.47.8.2  nathanw 	va_end(ap);
   1933  1.47.8.2  nathanw }
   1934  1.47.8.2  nathanw 
   1935  1.47.8.2  nathanw /*
   1936  1.47.8.2  nathanw  * Prefix message with adapter info.
   1937  1.47.8.2  nathanw  */
   1938  1.47.8.2  nathanw static void
   1939  1.47.8.2  nathanw ncr_aprint(struct ncr_softc *sc, char *fmt, ...)
   1940  1.47.8.2  nathanw {
   1941  1.47.8.2  nathanw 	va_list	ap;
   1942  1.47.8.2  nathanw 
   1943  1.47.8.2  nathanw 	va_start(ap, fmt);
   1944  1.47.8.2  nathanw 	printf("%s: ", sc->sc_dev.dv_xname);
   1945  1.47.8.2  nathanw 	vprintf(fmt, ap);
   1946  1.47.8.2  nathanw 	va_end(ap);
   1947  1.47.8.2  nathanw }
   1948  1.47.8.2  nathanw /****************************************************************************
   1949  1.47.8.2  nathanw  *		Start Debugging Functions				    *
   1950  1.47.8.2  nathanw  ****************************************************************************/
   1951  1.47.8.2  nathanw static void
   1952  1.47.8.2  nathanw show_data_sense(xs)
   1953  1.47.8.2  nathanw struct scsipi_xfer	*xs;
   1954  1.47.8.2  nathanw {
   1955  1.47.8.2  nathanw 	u_char	*p1, *p2;
   1956  1.47.8.2  nathanw 	int	i;
   1957  1.47.8.2  nathanw 	int	sz;
   1958  1.47.8.2  nathanw 
   1959  1.47.8.2  nathanw 	p1 = (u_char *) xs->cmd;
   1960  1.47.8.2  nathanw 	p2 = (u_char *)&xs->sense.scsi_sense;
   1961  1.47.8.2  nathanw 	if(*p2 == 0)
   1962  1.47.8.2  nathanw 		return;	/* No(n)sense */
   1963  1.47.8.2  nathanw 	printf("cmd[%d,%d]: ", xs->cmdlen, sz = command_size(*p1));
   1964  1.47.8.2  nathanw 	for (i = 0; i < sz; i++)
   1965  1.47.8.2  nathanw 		printf("%x ", p1[i]);
   1966  1.47.8.2  nathanw 	printf("\nsense: ");
   1967  1.47.8.2  nathanw 	for (i = 0; i < sizeof(xs->sense.scsi_sense); i++)
   1968  1.47.8.2  nathanw 		printf("%x ", p2[i]);
   1969  1.47.8.2  nathanw 	printf("\n");
   1970  1.47.8.2  nathanw }
   1971  1.47.8.2  nathanw 
   1972  1.47.8.2  nathanw static void
   1973  1.47.8.2  nathanw show_request(reqp, qtxt)
   1974  1.47.8.2  nathanw SC_REQ	*reqp;
   1975  1.47.8.2  nathanw char	*qtxt;
   1976  1.47.8.2  nathanw {
   1977  1.47.8.2  nathanw 	printf("REQ-%s: %d %p[%ld] cmd[0]=%x S=%x M=%x R=%x resid=%d dr_flag=%x %s\n",
   1978  1.47.8.2  nathanw 			qtxt, reqp->targ_id, reqp->xdata_ptr, reqp->xdata_len,
   1979  1.47.8.2  nathanw 			reqp->xcmd.opcode, reqp->status, reqp->message,
   1980  1.47.8.2  nathanw 			reqp->xs->error, reqp->xs->resid, reqp->dr_flag,
   1981  1.47.8.2  nathanw 			reqp->link ? "L":"");
   1982  1.47.8.2  nathanw 	if (reqp->status == SCSCHKC)
   1983  1.47.8.2  nathanw 		show_data_sense(reqp->xs);
   1984  1.47.8.2  nathanw }
   1985  1.47.8.2  nathanw 
   1986  1.47.8.2  nathanw static char *sig_names[] = {
   1987  1.47.8.2  nathanw 	"PAR", "SEL", "I/O", "C/D", "MSG", "REQ", "BSY", "RST",
   1988  1.47.8.2  nathanw 	"ACK", "ATN", "LBSY", "PMATCH", "IRQ", "EPAR", "DREQ", "EDMA"
   1989  1.47.8.2  nathanw };
   1990  1.47.8.2  nathanw 
   1991  1.47.8.2  nathanw static void
   1992  1.47.8.2  nathanw show_signals(dmstat, idstat)
   1993  1.47.8.2  nathanw u_char	dmstat, idstat;
   1994  1.47.8.2  nathanw {
   1995  1.47.8.2  nathanw 	u_short	tmp, mask;
   1996  1.47.8.2  nathanw 	int	j, need_pipe;
   1997  1.47.8.2  nathanw 
   1998  1.47.8.2  nathanw 	tmp = idstat | ((dmstat & 3) << 8);
   1999  1.47.8.2  nathanw 	printf("Bus signals (%02x/%02x): ", idstat, dmstat & 3);
   2000  1.47.8.2  nathanw 	for (mask = 1, j = need_pipe = 0; mask <= tmp; mask <<= 1, j++) {
   2001  1.47.8.2  nathanw 		if (tmp & mask)
   2002  1.47.8.2  nathanw 			printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
   2003  1.47.8.2  nathanw 	}
   2004  1.47.8.2  nathanw 	printf("\nDma status (%02x): ", dmstat);
   2005  1.47.8.2  nathanw 	for (mask = 4, j = 10, need_pipe = 0; mask <= dmstat; mask <<= 1, j++) {
   2006  1.47.8.2  nathanw 		if (dmstat & mask)
   2007  1.47.8.2  nathanw 			printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
   2008  1.47.8.2  nathanw 	}
   2009  1.47.8.2  nathanw 	printf("\n");
   2010  1.47.8.2  nathanw }
   2011  1.47.8.2  nathanw 
   2012  1.47.8.2  nathanw void
   2013  1.47.8.2  nathanw scsi_show()
   2014  1.47.8.2  nathanw {
   2015  1.47.8.2  nathanw 	SC_REQ	*tmp;
   2016  1.47.8.2  nathanw 	int	sps = splhigh();
   2017  1.47.8.2  nathanw 	u_char	idstat, dmstat;
   2018  1.47.8.2  nathanw #ifdef	DBG_PID
   2019  1.47.8.2  nathanw 	int	i;
   2020  1.47.8.2  nathanw #endif
   2021  1.47.8.2  nathanw 
   2022  1.47.8.2  nathanw 	printf("scsi_show: scsi_main is%s running\n",
   2023  1.47.8.2  nathanw 		main_running ? "" : " not");
   2024  1.47.8.2  nathanw 	for (tmp = issue_q; tmp; tmp = tmp->next)
   2025  1.47.8.2  nathanw 		show_request(tmp, "ISSUED");
   2026  1.47.8.2  nathanw 	for (tmp = discon_q; tmp; tmp = tmp->next)
   2027  1.47.8.2  nathanw 		show_request(tmp, "DISCONNECTED");
   2028  1.47.8.2  nathanw 	if (connected)
   2029  1.47.8.2  nathanw 		show_request(connected, "CONNECTED");
   2030  1.47.8.2  nathanw 	idstat = GET_5380_REG(NCR5380_IDSTAT);
   2031  1.47.8.2  nathanw 	dmstat = GET_5380_REG(NCR5380_DMSTAT);
   2032  1.47.8.2  nathanw 	show_signals(dmstat, idstat);
   2033  1.47.8.2  nathanw 	if (connected)
   2034  1.47.8.2  nathanw 		printf("phase = %d, ", connected->phase);
   2035  1.47.8.2  nathanw 	printf("busy:%x, spl:%04x\n", busy, sps);
   2036  1.47.8.2  nathanw #ifdef	DBG_PID
   2037  1.47.8.2  nathanw 	for (i=0; i<DBG_PID; i++)
   2038  1.47.8.2  nathanw 		printf("\t%d\t%s\n", i, last_hit[i]);
   2039  1.47.8.2  nathanw #endif
   2040  1.47.8.2  nathanw 
   2041  1.47.8.2  nathanw 	splx(sps);
   2042  1.47.8.2  nathanw }
   2043