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