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