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