Home | History | Annotate | Line # | Download | only in boot
sc.c revision 1.9
      1  1.9  tsutsui /*	$NetBSD: sc.c,v 1.9 2014/01/10 11:12:03 tsutsui Exp $	*/
      2  1.1  tsutsui 
      3  1.1  tsutsui /*
      4  1.1  tsutsui  * Copyright (c) 1992 OMRON Corporation.
      5  1.1  tsutsui  *
      6  1.1  tsutsui  * This code is derived from software contributed to Berkeley by
      7  1.1  tsutsui  * OMRON Corporation.
      8  1.1  tsutsui  *
      9  1.1  tsutsui  * Redistribution and use in source and binary forms, with or without
     10  1.1  tsutsui  * modification, are permitted provided that the following conditions
     11  1.1  tsutsui  * are met:
     12  1.1  tsutsui  * 1. Redistributions of source code must retain the above copyright
     13  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer.
     14  1.1  tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  tsutsui  *    documentation and/or other materials provided with the distribution.
     17  1.1  tsutsui  * 3. All advertising materials mentioning features or use of this software
     18  1.1  tsutsui  *    must display the following acknowledgement:
     19  1.1  tsutsui  *	This product includes software developed by the University of
     20  1.1  tsutsui  *	California, Berkeley and its contributors.
     21  1.1  tsutsui  * 4. Neither the name of the University nor the names of its contributors
     22  1.1  tsutsui  *    may be used to endorse or promote products derived from this software
     23  1.1  tsutsui  *    without specific prior written permission.
     24  1.1  tsutsui  *
     25  1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  1.1  tsutsui  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1  tsutsui  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1  tsutsui  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  1.1  tsutsui  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1  tsutsui  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1  tsutsui  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1  tsutsui  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1  tsutsui  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1  tsutsui  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1  tsutsui  * SUCH DAMAGE.
     36  1.1  tsutsui  *
     37  1.1  tsutsui  *	@(#)sc.c	8.1 (Berkeley) 6/10/93
     38  1.1  tsutsui  */
     39  1.1  tsutsui /*
     40  1.1  tsutsui  * Copyright (c) 1992, 1993
     41  1.1  tsutsui  *	The Regents of the University of California.  All rights reserved.
     42  1.1  tsutsui  *
     43  1.1  tsutsui  * This code is derived from software contributed to Berkeley by
     44  1.1  tsutsui  * OMRON Corporation.
     45  1.1  tsutsui  *
     46  1.1  tsutsui  * Redistribution and use in source and binary forms, with or without
     47  1.1  tsutsui  * modification, are permitted provided that the following conditions
     48  1.1  tsutsui  * are met:
     49  1.1  tsutsui  * 1. Redistributions of source code must retain the above copyright
     50  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer.
     51  1.1  tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     52  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer in the
     53  1.1  tsutsui  *    documentation and/or other materials provided with the distribution.
     54  1.1  tsutsui  * 3. Neither the name of the University nor the names of its contributors
     55  1.1  tsutsui  *    may be used to endorse or promote products derived from this software
     56  1.1  tsutsui  *    without specific prior written permission.
     57  1.1  tsutsui  *
     58  1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  1.1  tsutsui  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  1.1  tsutsui  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  1.1  tsutsui  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  1.1  tsutsui  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  1.1  tsutsui  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  1.1  tsutsui  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  1.1  tsutsui  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  1.1  tsutsui  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  1.1  tsutsui  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  1.1  tsutsui  * SUCH DAMAGE.
     69  1.1  tsutsui  *
     70  1.1  tsutsui  *	@(#)sc.c	8.1 (Berkeley) 6/10/93
     71  1.1  tsutsui  */
     72  1.1  tsutsui 
     73  1.1  tsutsui /*
     74  1.1  tsutsui  * sc.c -- SCSI Protocole Controller (SPC)  driver
     75  1.1  tsutsui  * remaked by A.Fujita, MAR-11-199
     76  1.1  tsutsui  */
     77  1.1  tsutsui 
     78  1.1  tsutsui 
     79  1.3  tsutsui #define NSC	2
     80  1.1  tsutsui 
     81  1.1  tsutsui #include <sys/param.h>
     82  1.1  tsutsui #include <luna68k/stand/boot/samachdep.h>
     83  1.1  tsutsui #include <luna68k/stand/boot/scsireg.h>
     84  1.1  tsutsui #include <luna68k/stand/boot/scsivar.h>
     85  1.1  tsutsui 
     86  1.1  tsutsui #define SCSI_ID		7
     87  1.1  tsutsui 
     88  1.9  tsutsui static void screset(struct scsi_softc *);
     89  1.9  tsutsui static void scprobe(struct scsi_softc *, uint, uint);
     90  1.1  tsutsui static int issue_select(struct scsidevice *, u_char);
     91  1.1  tsutsui static void ixfer_start(struct scsidevice *, int, u_char, int);
     92  1.1  tsutsui static void ixfer_out(struct scsidevice *, int, u_char *);
     93  1.1  tsutsui static void ixfer_in(struct scsidevice *, int, u_char *);
     94  1.1  tsutsui static int scrun(int, int, u_char *, int, u_char *, int, volatile int *);
     95  1.1  tsutsui static int scfinish(int);
     96  1.9  tsutsui static void scabort(struct scsi_softc *);
     97  1.1  tsutsui 
     98  1.1  tsutsui struct	scsi_softc scsi_softc[NSC];
     99  1.1  tsutsui 
    100  1.1  tsutsui /*
    101  1.1  tsutsui  * Initialize SPC & Data Structure
    102  1.1  tsutsui  */
    103  1.1  tsutsui 
    104  1.1  tsutsui int
    105  1.9  tsutsui scinit(int ctlr, void *addr)
    106  1.1  tsutsui {
    107  1.3  tsutsui 	struct scsi_softc *hs;
    108  1.9  tsutsui 	uint id;
    109  1.3  tsutsui 
    110  1.9  tsutsui 	if (ctlr < 0 || ctlr >= NSC)
    111  1.3  tsutsui 		return 0;
    112  1.3  tsutsui 
    113  1.9  tsutsui 	hs = &scsi_softc[ctlr];
    114  1.9  tsutsui 	hs->sc_ctlr   = ctlr;
    115  1.9  tsutsui 	hs->sc_spc    = addr;
    116  1.1  tsutsui 
    117  1.1  tsutsui 	hs->sc_flags  = 0;
    118  1.1  tsutsui 	hs->sc_phase  = BUS_FREE_PHASE;
    119  1.1  tsutsui 	hs->sc_target = SCSI_ID;
    120  1.1  tsutsui 
    121  1.1  tsutsui 	hs->sc_cdb    = NULL;
    122  1.1  tsutsui 	hs->sc_cdblen = 0;
    123  1.1  tsutsui 	hs->sc_buf    = NULL;
    124  1.1  tsutsui 	hs->sc_len    = 0;
    125  1.1  tsutsui 	hs->sc_lock   = NULL;
    126  1.1  tsutsui 
    127  1.1  tsutsui 	hs->sc_stat   = 0;
    128  1.1  tsutsui 	hs->sc_msg[0] = 0;
    129  1.1  tsutsui 
    130  1.9  tsutsui 	screset(hs);
    131  1.9  tsutsui 
    132  1.9  tsutsui 	for (id = 0; id < 7; id++)
    133  1.9  tsutsui 		scprobe(hs, id, 0);
    134  1.9  tsutsui 
    135  1.1  tsutsui 	return(1);
    136  1.1  tsutsui }
    137  1.1  tsutsui 
    138  1.1  tsutsui void
    139  1.9  tsutsui screset(struct scsi_softc *hs)
    140  1.1  tsutsui {
    141  1.9  tsutsui 	struct scsidevice *hd = hs->sc_spc;
    142  1.1  tsutsui 
    143  1.9  tsutsui 	printf("sc%d at 0x%08lx: ", hs->sc_ctlr, (u_long)hs->sc_spc);
    144  1.1  tsutsui 
    145  1.1  tsutsui 	/*
    146  1.1  tsutsui 	 * Disable interrupts then reset the FUJI chip.
    147  1.1  tsutsui 	 */
    148  1.1  tsutsui 
    149  1.1  tsutsui 	hd->scsi_sctl = SCTL_DISABLE | SCTL_CTRLRST;
    150  1.1  tsutsui 	hd->scsi_scmd = 0;
    151  1.1  tsutsui 	hd->scsi_pctl = 0;
    152  1.1  tsutsui 	hd->scsi_temp = 0;
    153  1.1  tsutsui 	hd->scsi_tch  = 0;
    154  1.1  tsutsui 	hd->scsi_tcm  = 0;
    155  1.1  tsutsui 	hd->scsi_tcl  = 0;
    156  1.1  tsutsui 	hd->scsi_ints = 0;
    157  1.1  tsutsui 
    158  1.1  tsutsui 	/* We can use Asynchronous Transfer only */
    159  1.1  tsutsui 	printf("async");
    160  1.1  tsutsui 
    161  1.1  tsutsui 	/*
    162  1.1  tsutsui 	 * Configure MB89352 with its SCSI address, all
    163  1.1  tsutsui 	 * interrupts enabled & appropriate parity.
    164  1.1  tsutsui 	 */
    165  1.1  tsutsui 	hd->scsi_bdid = SCSI_ID;
    166  1.1  tsutsui 	hd->scsi_sctl = SCTL_DISABLE | SCTL_ABRT_ENAB|
    167  1.1  tsutsui 			SCTL_PARITY_ENAB | SCTL_RESEL_ENAB |
    168  1.1  tsutsui 			SCTL_INTR_ENAB;
    169  1.1  tsutsui 	printf(", parity");
    170  1.1  tsutsui 
    171  1.1  tsutsui 	DELAY(400);
    172  1.1  tsutsui 	hd->scsi_sctl &= ~SCTL_DISABLE;
    173  1.1  tsutsui 
    174  1.9  tsutsui 	printf(", ID %d\n", SCSI_ID);
    175  1.9  tsutsui }
    176  1.9  tsutsui 
    177  1.9  tsutsui bool
    178  1.9  tsutsui scident(uint ctlr, uint target, uint lun, struct scsi_inquiry *inqout,
    179  1.9  tsutsui     uint32_t *capout)
    180  1.9  tsutsui {
    181  1.9  tsutsui 	struct scsi_inquiry inqbuf;
    182  1.9  tsutsui 	struct scsi_generic_cdb inq = {
    183  1.9  tsutsui 		6,
    184  1.9  tsutsui 		{ CMD_INQUIRY, 0, 0, 0, sizeof(inqbuf), 0 }
    185  1.9  tsutsui 	};
    186  1.9  tsutsui 	uint32_t capbuf[2];
    187  1.9  tsutsui 	struct scsi_generic_cdb cap = {
    188  1.9  tsutsui 		10,
    189  1.9  tsutsui 		{ CMD_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
    190  1.9  tsutsui 	};
    191  1.9  tsutsui 	int i;
    192  1.9  tsutsui 	int tries = 10;
    193  1.9  tsutsui 
    194  1.9  tsutsui 	/*
    195  1.9  tsutsui 	 * See if unit exists and is a disk then read block size & nblocks.
    196  1.9  tsutsui 	 */
    197  1.9  tsutsui 	while ((i = scsi_test_unit_rdy(ctlr, target, lun)) != 0) {
    198  1.9  tsutsui 		if (i < 0 || --tries < 0)
    199  1.9  tsutsui 			return false;
    200  1.9  tsutsui 		if (i == STS_CHECKCOND) {
    201  1.9  tsutsui 			u_char sensebuf[8];
    202  1.9  tsutsui 			struct scsi_xsense *sp = (struct scsi_xsense *)sensebuf;
    203  1.9  tsutsui 
    204  1.9  tsutsui 			scsi_request_sense(ctlr, target, lun, sensebuf, 8);
    205  1.9  tsutsui 			if (sp->class == 7 && sp->key == 6)
    206  1.9  tsutsui 				/* drive doing an RTZ -- give it a while */
    207  1.9  tsutsui 				DELAY(1000000);
    208  1.9  tsutsui 		}
    209  1.9  tsutsui 		DELAY(1000);
    210  1.9  tsutsui 	}
    211  1.9  tsutsui 	if (scsi_immed_command(ctlr, target, lun, &inq, (u_char *)&inqbuf,
    212  1.9  tsutsui 			       sizeof(inqbuf)) ||
    213  1.9  tsutsui 	    scsi_immed_command(ctlr, target, lun, &cap, (u_char *)&capbuf,
    214  1.9  tsutsui 			       sizeof(capbuf)))
    215  1.9  tsutsui 		/* doesn't exist or not a CCS device */
    216  1.9  tsutsui 		return false;
    217  1.9  tsutsui 
    218  1.9  tsutsui 	switch (inqbuf.type) {
    219  1.9  tsutsui 	case 0:		/* disk */
    220  1.9  tsutsui 	case 4:		/* WORM */
    221  1.9  tsutsui 	case 5:		/* CD-ROM */
    222  1.9  tsutsui 	case 7:		/* Magneto-optical */
    223  1.9  tsutsui 		break;
    224  1.9  tsutsui 	default:	/* not a disk */
    225  1.9  tsutsui 		return false;
    226  1.9  tsutsui 	}
    227  1.9  tsutsui 
    228  1.9  tsutsui 	if (inqout != NULL)
    229  1.9  tsutsui 		*inqout = inqbuf;
    230  1.9  tsutsui 	if (capout != NULL) {
    231  1.9  tsutsui 		/* assume big endian */
    232  1.9  tsutsui 		capout[0] = capbuf[0];
    233  1.9  tsutsui 		capout[1] = capbuf[1];
    234  1.9  tsutsui 	}
    235  1.9  tsutsui 
    236  1.9  tsutsui 	return true;
    237  1.9  tsutsui }
    238  1.9  tsutsui 
    239  1.9  tsutsui static void
    240  1.9  tsutsui scprobe(struct scsi_softc *hs, uint target, uint lun)
    241  1.9  tsutsui {
    242  1.9  tsutsui 	struct scsi_inquiry inqbuf;
    243  1.9  tsutsui 	uint32_t capbuf[2];
    244  1.9  tsutsui 	char idstr[32];
    245  1.9  tsutsui 	int i;
    246  1.9  tsutsui 
    247  1.9  tsutsui 	if (!scident(hs->sc_ctlr, target, lun, &inqbuf, capbuf))
    248  1.9  tsutsui 		return;
    249  1.9  tsutsui 
    250  1.9  tsutsui 	memcpy(idstr, &inqbuf.vendor_id, 28);
    251  1.9  tsutsui 	for (i = 27; i > 23; --i)
    252  1.9  tsutsui 		if (idstr[i] != ' ')
    253  1.9  tsutsui 			break;
    254  1.9  tsutsui 	idstr[i + 1] = '\0';
    255  1.9  tsutsui 	for (i = 23; i > 7; --i)
    256  1.9  tsutsui 		if (idstr[i] != ' ')
    257  1.9  tsutsui 			break;
    258  1.9  tsutsui 	idstr[i + 1] = '\0';
    259  1.9  tsutsui 	for (i = 7; i >= 0; --i)
    260  1.9  tsutsui 		if (idstr[i] != ' ')
    261  1.9  tsutsui 			break;
    262  1.9  tsutsui 	idstr[i + 1] = '\0';
    263  1.9  tsutsui 
    264  1.9  tsutsui 	printf(" ID %d: %s %s rev %s", target, idstr, &idstr[8], &idstr[24]);
    265  1.9  tsutsui 	printf(", %d bytes/sect x %d sectors\n", capbuf[1], capbuf[0]);
    266  1.1  tsutsui }
    267  1.1  tsutsui 
    268  1.1  tsutsui 
    269  1.1  tsutsui /*
    270  1.1  tsutsui  * SPC Arbitration/Selection routine
    271  1.1  tsutsui  */
    272  1.1  tsutsui 
    273  1.1  tsutsui int
    274  1.1  tsutsui issue_select(struct scsidevice *hd, u_char target)
    275  1.1  tsutsui {
    276  1.1  tsutsui 	hd->scsi_pctl = 0;
    277  1.1  tsutsui 	hd->scsi_temp = (1 << SCSI_ID) | (1 << target);
    278  1.1  tsutsui 
    279  1.5  tsutsui 	/* select timeout is hardcoded to 250ms */
    280  1.5  tsutsui 	hd->scsi_tch = 2;
    281  1.5  tsutsui 	hd->scsi_tcm = 113;
    282  1.5  tsutsui 	hd->scsi_tcl = 3;
    283  1.1  tsutsui 
    284  1.1  tsutsui 	hd->scsi_scmd = SCMD_SELECT;
    285  1.1  tsutsui 
    286  1.1  tsutsui 	return (1);
    287  1.1  tsutsui }
    288  1.1  tsutsui 
    289  1.1  tsutsui 
    290  1.1  tsutsui /*
    291  1.1  tsutsui  * SPC Manual Transfer routines
    292  1.1  tsutsui  */
    293  1.1  tsutsui 
    294  1.1  tsutsui /* not yet */
    295  1.1  tsutsui 
    296  1.1  tsutsui 
    297  1.1  tsutsui /*
    298  1.1  tsutsui  * SPC Program Transfer routines
    299  1.1  tsutsui  */
    300  1.1  tsutsui 
    301  1.1  tsutsui void
    302  1.1  tsutsui ixfer_start(struct scsidevice *hd, int len, u_char phase, int wait)
    303  1.1  tsutsui {
    304  1.1  tsutsui 	hd->scsi_tch  = ((len & 0xff0000) >> 16);
    305  1.1  tsutsui 	hd->scsi_tcm  = ((len & 0x00ff00) >>  8);
    306  1.1  tsutsui 	hd->scsi_tcl  =  (len & 0x0000ff);
    307  1.1  tsutsui 	hd->scsi_pctl = phase;
    308  1.1  tsutsui 	hd->scsi_scmd = SCMD_XFR | SCMD_PROG_XFR;
    309  1.1  tsutsui }
    310  1.1  tsutsui 
    311  1.1  tsutsui void
    312  1.1  tsutsui ixfer_out(struct scsidevice *hd, int len, u_char *buf)
    313  1.1  tsutsui {
    314  1.1  tsutsui 	for(; len > 0; len--) {
    315  1.1  tsutsui 		while (hd->scsi_ssts & SSTS_DREG_FULL) {
    316  1.1  tsutsui 			DELAY(5);
    317  1.1  tsutsui 		}
    318  1.1  tsutsui 		hd->scsi_dreg = *buf++;
    319  1.1  tsutsui 	}
    320  1.1  tsutsui }
    321  1.1  tsutsui 
    322  1.1  tsutsui void
    323  1.1  tsutsui ixfer_in(struct scsidevice *hd, int len, u_char *buf)
    324  1.1  tsutsui {
    325  1.1  tsutsui 	for (; len > 0; len--) {
    326  1.1  tsutsui 		while (hd->scsi_ssts & SSTS_DREG_EMPTY) {
    327  1.1  tsutsui 			DELAY(5);
    328  1.1  tsutsui 		}
    329  1.1  tsutsui 		*buf++ = hd->scsi_dreg;
    330  1.1  tsutsui 	}
    331  1.1  tsutsui }
    332  1.1  tsutsui 
    333  1.1  tsutsui 
    334  1.1  tsutsui /*
    335  1.1  tsutsui  * SPC drive routines
    336  1.1  tsutsui  */
    337  1.1  tsutsui 
    338  1.1  tsutsui int
    339  1.9  tsutsui scrun(int ctlr, int target, u_char *cdb, int cdblen, u_char *buf, int len,
    340  1.1  tsutsui     volatile int *lock)
    341  1.1  tsutsui {
    342  1.6  tsutsui 	struct scsi_softc *hs;
    343  1.6  tsutsui 	struct scsidevice *hd;
    344  1.6  tsutsui 
    345  1.6  tsutsui 	if (ctlr < 0 || ctlr >= NSC)
    346  1.6  tsutsui 		return 0;
    347  1.6  tsutsui 
    348  1.6  tsutsui 	hs = &scsi_softc[ctlr];
    349  1.9  tsutsui 	hd = hs->sc_spc;
    350  1.9  tsutsui 	if (hd == NULL)
    351  1.9  tsutsui 		return 0;
    352  1.1  tsutsui 
    353  1.1  tsutsui 	if (hd->scsi_ssts & (SSTS_INITIATOR|SSTS_TARGET|SSTS_BUSY))
    354  1.1  tsutsui 		return(0);
    355  1.1  tsutsui 
    356  1.1  tsutsui 	hs->sc_flags  = 0;
    357  1.1  tsutsui 	hs->sc_phase  = ARB_SEL_PHASE;
    358  1.9  tsutsui 	hs->sc_target = target;
    359  1.1  tsutsui 
    360  1.1  tsutsui 	hs->sc_cdb    = cdb;
    361  1.1  tsutsui 	hs->sc_cdblen = cdblen;
    362  1.1  tsutsui 	hs->sc_buf    = buf;
    363  1.1  tsutsui 	hs->sc_len    = len;
    364  1.1  tsutsui 	hs->sc_lock   = lock;
    365  1.1  tsutsui 
    366  1.1  tsutsui 	hs->sc_stat   = 0;
    367  1.1  tsutsui 	hs->sc_msg[0] = 0;
    368  1.1  tsutsui 
    369  1.1  tsutsui 	*(hs->sc_lock) = SC_IN_PROGRESS;
    370  1.1  tsutsui 	issue_select(hd, hs->sc_target);
    371  1.1  tsutsui 
    372  1.1  tsutsui 	return(1);
    373  1.1  tsutsui }
    374  1.1  tsutsui 
    375  1.1  tsutsui int
    376  1.1  tsutsui scfinish(int ctlr)
    377  1.1  tsutsui {
    378  1.1  tsutsui 	struct scsi_softc *hs = &scsi_softc[ctlr];
    379  1.1  tsutsui 	int status = hs->sc_stat;
    380  1.1  tsutsui 
    381  1.1  tsutsui 	hs->sc_flags  = 0;
    382  1.1  tsutsui 	hs->sc_phase  = BUS_FREE_PHASE;
    383  1.1  tsutsui 	hs->sc_target = SCSI_ID;
    384  1.1  tsutsui 
    385  1.1  tsutsui 	hs->sc_cdb    = NULL;
    386  1.1  tsutsui 	hs->sc_cdblen = 0;
    387  1.1  tsutsui 	hs->sc_buf    = NULL;
    388  1.1  tsutsui 	hs->sc_len    = 0;
    389  1.1  tsutsui 	hs->sc_lock   = NULL;
    390  1.1  tsutsui 
    391  1.1  tsutsui 	hs->sc_stat   = 0;
    392  1.1  tsutsui 	hs->sc_msg[0] = 0;
    393  1.1  tsutsui 
    394  1.1  tsutsui 	return(status);
    395  1.1  tsutsui }
    396  1.1  tsutsui 
    397  1.1  tsutsui void
    398  1.9  tsutsui scabort(struct scsi_softc *hs)
    399  1.1  tsutsui {
    400  1.9  tsutsui 	struct scsidevice *hd = hs->sc_spc;
    401  1.1  tsutsui 	int len;
    402  1.1  tsutsui 	u_char junk;
    403  1.1  tsutsui 
    404  1.1  tsutsui 	printf("sc%d: abort  phase=0x%x, ssts=0x%x, ints=0x%x\n",
    405  1.9  tsutsui 		hs->sc_ctlr, hd->scsi_psns, hd->scsi_ssts,
    406  1.1  tsutsui 		hd->scsi_ints);
    407  1.1  tsutsui 
    408  1.1  tsutsui 	if (hd->scsi_ints != 0)
    409  1.1  tsutsui 		hd->scsi_ints = hd->scsi_ints;
    410  1.1  tsutsui 
    411  1.1  tsutsui 	if (hd->scsi_psns == 0 || (hd->scsi_ssts & SSTS_INITIATOR) == 0)
    412  1.1  tsutsui 		/* no longer connected to scsi target */
    413  1.1  tsutsui 		return;
    414  1.1  tsutsui 
    415  1.1  tsutsui 	/* get the number of bytes remaining in current xfer + fudge */
    416  1.1  tsutsui 	len = (hd->scsi_tch << 16) | (hd->scsi_tcm << 8) | hd->scsi_tcl;
    417  1.1  tsutsui 
    418  1.1  tsutsui 	/* for that many bus cycles, try to send an abort msg */
    419  1.1  tsutsui 	for (len += 1024; (hd->scsi_ssts & SSTS_INITIATOR) && --len >= 0; ) {
    420  1.1  tsutsui 		hd->scsi_scmd = SCMD_SET_ATN;
    421  1.1  tsutsui 
    422  1.1  tsutsui 		while ((hd->scsi_psns & PSNS_REQ) == 0) {
    423  1.1  tsutsui 			if (! (hd->scsi_ssts & SSTS_INITIATOR))
    424  1.1  tsutsui 				goto out;
    425  1.1  tsutsui 			DELAY(1);
    426  1.1  tsutsui 		}
    427  1.1  tsutsui 
    428  1.1  tsutsui 		if ((hd->scsi_psns & PHASE) == MESG_OUT_PHASE)
    429  1.1  tsutsui 			hd->scsi_scmd = SCMD_RST_ATN;
    430  1.1  tsutsui 		hd->scsi_pctl = hs->sc_phase = hd->scsi_psns & PHASE;
    431  1.1  tsutsui 
    432  1.1  tsutsui 		if (hd->scsi_psns & PHASE_IO) {
    433  1.1  tsutsui 			/* one of the input phases - read & discard a byte */
    434  1.1  tsutsui 			hd->scsi_scmd = SCMD_SET_ACK;
    435  1.1  tsutsui 			while (hd->scsi_psns & PSNS_REQ)
    436  1.1  tsutsui 				DELAY(1);
    437  1.1  tsutsui 			junk = hd->scsi_temp;
    438  1.1  tsutsui 		} else {
    439  1.1  tsutsui 			/* one of the output phases - send an abort msg */
    440  1.1  tsutsui 			hd->scsi_temp = MSG_ABORT;
    441  1.1  tsutsui 			hd->scsi_scmd = SCMD_SET_ACK;
    442  1.1  tsutsui 			while (hd->scsi_psns & PSNS_REQ)
    443  1.1  tsutsui 				DELAY(1);
    444  1.1  tsutsui 		}
    445  1.1  tsutsui 
    446  1.1  tsutsui 		hd->scsi_scmd = SCMD_RST_ACK;
    447  1.1  tsutsui 	}
    448  1.1  tsutsui out:
    449  1.1  tsutsui 	/*
    450  1.1  tsutsui 	 * Either the abort was successful & the bus is disconnected or
    451  1.1  tsutsui 	 * the device didn't listen.  If the latter, announce the problem.
    452  1.1  tsutsui 	 * Either way, reset the card & the SPC.
    453  1.1  tsutsui 	 */
    454  1.1  tsutsui 	if (len < 0 && hs)
    455  1.1  tsutsui 		printf("sc%d: abort failed.  phase=0x%x, ssts=0x%x\n",
    456  1.9  tsutsui 			hs->sc_ctlr, hd->scsi_psns, hd->scsi_ssts);
    457  1.1  tsutsui }
    458  1.1  tsutsui 
    459  1.1  tsutsui 
    460  1.1  tsutsui /*
    461  1.1  tsutsui  * SCSI Command Handler
    462  1.1  tsutsui  */
    463  1.1  tsutsui 
    464  1.1  tsutsui int
    465  1.9  tsutsui scsi_test_unit_rdy(int ctlr, int target, int lun)
    466  1.1  tsutsui {
    467  1.1  tsutsui 	static struct scsi_cdb6 cdb = { CMD_TEST_UNIT_READY };
    468  1.1  tsutsui 	int status;
    469  1.1  tsutsui 	volatile int lock;
    470  1.1  tsutsui 
    471  1.1  tsutsui #ifdef DEBUG
    472  1.9  tsutsui 	printf("scsi_test_unit_rdy( %d, %d, %d): Start\n", ctlr, target, lun);
    473  1.1  tsutsui #endif
    474  1.1  tsutsui 
    475  1.9  tsutsui 	cdb.lun = lun;
    476  1.1  tsutsui 
    477  1.9  tsutsui 	if (!(scrun(ctlr, target, (void *)&cdb, 6, NULL, 0, &lock))) {
    478  1.1  tsutsui #ifdef DEBUG
    479  1.1  tsutsui 		printf("scsi_test_unit_rdy: Command Transfer Failed.\n");
    480  1.1  tsutsui #endif
    481  1.1  tsutsui 		return(-1);
    482  1.1  tsutsui 	}
    483  1.1  tsutsui 
    484  1.1  tsutsui 	while ((lock == SC_IN_PROGRESS) || (lock == SC_DISCONNECTED))
    485  1.1  tsutsui 		DELAY(10);
    486  1.1  tsutsui 
    487  1.1  tsutsui 	status = scfinish(ctlr);
    488  1.1  tsutsui 
    489  1.1  tsutsui 	if (lock == SC_IO_COMPLETE) {
    490  1.1  tsutsui #ifdef DEBUG
    491  1.1  tsutsui 		printf("scsi_test_unit_rdy: Status -- 0x%x\n", status);
    492  1.1  tsutsui #endif
    493  1.1  tsutsui 		return(status);
    494  1.1  tsutsui 	} else {
    495  1.1  tsutsui 		return(lock);
    496  1.1  tsutsui 	}
    497  1.1  tsutsui }
    498  1.1  tsutsui 
    499  1.1  tsutsui int
    500  1.9  tsutsui scsi_request_sense(int ctlr, int target, int lun, u_char *buf, unsigned int len)
    501  1.1  tsutsui {
    502  1.1  tsutsui 	static struct scsi_cdb6 cdb = {	CMD_REQUEST_SENSE };
    503  1.1  tsutsui 	int status;
    504  1.1  tsutsui 	volatile int lock;
    505  1.1  tsutsui 
    506  1.1  tsutsui #ifdef DEBUG
    507  1.1  tsutsui 	printf("scsi_request_sense: Start\n");
    508  1.1  tsutsui #endif
    509  1.1  tsutsui 
    510  1.7  tsutsui 	/* Request Sense        */
    511  1.7  tsutsui 	/* Additional Sens Length*/
    512  1.7  tsutsui 	/* cdbAllocation Length */
    513  1.7  tsutsui 	/*          */
    514  1.1  tsutsui 
    515  1.7  tsutsui 	/* Addtional Sens Field */
    516  1.7  tsutsui 	/* len            */
    517  1.1  tsutsui 
    518  1.9  tsutsui 	cdb.lun = lun;
    519  1.1  tsutsui 	cdb.len = len;
    520  1.1  tsutsui 
    521  1.9  tsutsui 	if (!(scrun(ctlr, target, (void *)&cdb, 6, buf, len, &lock))) {
    522  1.1  tsutsui #ifdef DEBUG
    523  1.1  tsutsui 		printf("scsi_request_sense: Command Transfer Failed.\n");
    524  1.1  tsutsui #endif
    525  1.1  tsutsui 		return(-1);
    526  1.1  tsutsui 	}
    527  1.1  tsutsui 
    528  1.1  tsutsui 	while ((lock == SC_IN_PROGRESS) || (lock == SC_DISCONNECTED))
    529  1.1  tsutsui 		DELAY(10);
    530  1.1  tsutsui 
    531  1.1  tsutsui 	status = scfinish(ctlr);
    532  1.1  tsutsui 
    533  1.1  tsutsui 	if (lock == SC_IO_COMPLETE) {
    534  1.1  tsutsui #ifdef DEBUG
    535  1.1  tsutsui 		printf("scsi_request_sense: Status -- 0x%x\n", status);
    536  1.1  tsutsui #endif
    537  1.1  tsutsui 		return(status);
    538  1.1  tsutsui 	} else {
    539  1.1  tsutsui 		return(lock);
    540  1.1  tsutsui 	}
    541  1.1  tsutsui }
    542  1.1  tsutsui 
    543  1.1  tsutsui int
    544  1.9  tsutsui scsi_immed_command(int ctlr, int target, int lun, struct scsi_generic_cdb *cdb,
    545  1.1  tsutsui     u_char *buf, unsigned int len)
    546  1.1  tsutsui {
    547  1.1  tsutsui 	int status;
    548  1.1  tsutsui 	volatile int lock;
    549  1.1  tsutsui 
    550  1.1  tsutsui #ifdef DEBUG
    551  1.1  tsutsui 	printf("scsi_immed_command( %d, %d, %d, cdb(%d), buf, %d): Start\n",
    552  1.9  tsutsui 	       ctlr, target, lun, cdb->len, len);
    553  1.1  tsutsui #endif
    554  1.1  tsutsui 
    555  1.9  tsutsui 	cdb->cdb[1] |= lun << 5;
    556  1.1  tsutsui 
    557  1.9  tsutsui 	if (!(scrun(ctlr, target, (void *)&cdb->cdb[0], cdb->len, buf, len, &lock))) {
    558  1.1  tsutsui #ifdef DEBUG
    559  1.1  tsutsui 		printf("scsi_immed_command: Command Transfer Failed.\n");
    560  1.1  tsutsui #endif
    561  1.1  tsutsui 		return(-1);
    562  1.1  tsutsui 	}
    563  1.1  tsutsui 
    564  1.1  tsutsui 	while ((lock == SC_IN_PROGRESS) || (lock == SC_DISCONNECTED))
    565  1.1  tsutsui 		DELAY(10);
    566  1.1  tsutsui 
    567  1.1  tsutsui 	status = scfinish(ctlr);
    568  1.1  tsutsui 
    569  1.1  tsutsui 	if (lock == SC_IO_COMPLETE) {
    570  1.1  tsutsui #ifdef DEBUG
    571  1.1  tsutsui 		printf("scsi_immed_command: Status -- 0x%x\n", status);
    572  1.1  tsutsui #endif
    573  1.1  tsutsui 		return(status);
    574  1.1  tsutsui 	} else {
    575  1.1  tsutsui 		return(lock);
    576  1.1  tsutsui 	}
    577  1.1  tsutsui }
    578  1.1  tsutsui 
    579  1.1  tsutsui int
    580  1.9  tsutsui scsi_format_unit(int ctlr, int target, int lun)
    581  1.1  tsutsui {
    582  1.1  tsutsui 	static struct scsi_cdb6 cdb = { CMD_FORMAT_UNIT, 0, 0, 0, 0, 0 };
    583  1.1  tsutsui 	int status;
    584  1.1  tsutsui 	volatile int lock;
    585  1.1  tsutsui #ifdef DEBUG
    586  1.1  tsutsui 	int count = 0;
    587  1.1  tsutsui #endif
    588  1.1  tsutsui 
    589  1.1  tsutsui #ifdef DEBUG
    590  1.9  tsutsui 	printf("scsi_format_unit( %d, %d, %d): Start\n", ctlr, target, lun);
    591  1.1  tsutsui #endif
    592  1.1  tsutsui 
    593  1.9  tsutsui 	cdb.lun = lun;
    594  1.1  tsutsui 
    595  1.9  tsutsui 	if (!(scrun(ctlr, target, (void *)&cdb, 6, (u_char *) 0, 0, &lock))) {
    596  1.1  tsutsui #ifdef DEBUG
    597  1.1  tsutsui 		printf("scsi_format_unit: Command Transfer Failed.\n");
    598  1.1  tsutsui #endif
    599  1.1  tsutsui 		return(-1);
    600  1.1  tsutsui 	}
    601  1.1  tsutsui 
    602  1.1  tsutsui 	while ((lock == SC_IN_PROGRESS) || (lock == SC_DISCONNECTED)) {
    603  1.1  tsutsui 		DELAY(1000000);
    604  1.1  tsutsui #ifdef DEBUG
    605  1.1  tsutsui 		if ((++count % 60) == 0)
    606  1.1  tsutsui 			printf("scsi_format_unit: %d\n", count / 60);
    607  1.1  tsutsui #endif
    608  1.1  tsutsui 	}
    609  1.1  tsutsui 
    610  1.1  tsutsui 	status = scfinish(ctlr);
    611  1.1  tsutsui 
    612  1.1  tsutsui 	if (lock == SC_IO_COMPLETE) {
    613  1.1  tsutsui #ifdef DEBUG
    614  1.1  tsutsui 		printf("scsi_format_unit: Status -- 0x%x\n", status);
    615  1.1  tsutsui #endif
    616  1.1  tsutsui 		return(status);
    617  1.1  tsutsui 	} else {
    618  1.1  tsutsui 		return(lock);
    619  1.1  tsutsui 	}
    620  1.1  tsutsui }
    621  1.1  tsutsui 
    622  1.1  tsutsui 
    623  1.1  tsutsui /*
    624  1.1  tsutsui  * Interrupt Routine
    625  1.1  tsutsui  */
    626  1.1  tsutsui 
    627  1.1  tsutsui int
    628  1.1  tsutsui scintr(void)
    629  1.1  tsutsui {
    630  1.1  tsutsui 	struct scsi_softc *hs;
    631  1.1  tsutsui 	struct scsidevice *hd;
    632  1.1  tsutsui 	u_char ints, temp;
    633  1.1  tsutsui 	int i;
    634  1.1  tsutsui 	u_char *buf;
    635  1.1  tsutsui 	int len;
    636  1.1  tsutsui 
    637  1.1  tsutsui 	for (i = 0; i < NSC; i++) {
    638  1.1  tsutsui 		hs = &scsi_softc[i];
    639  1.9  tsutsui 		hd = hs->sc_spc;
    640  1.1  tsutsui 		if ((ints = hd->scsi_ints) != 0)
    641  1.1  tsutsui 			goto get_intr;
    642  1.1  tsutsui 	}
    643  1.1  tsutsui 
    644  1.1  tsutsui 	/* Unknown Interrupt occured */
    645  1.1  tsutsui 	return -1;
    646  1.1  tsutsui 
    647  1.1  tsutsui 
    648  1.1  tsutsui 	/*
    649  1.1  tsutsui 	 * Interrupt
    650  1.1  tsutsui 	 */
    651  1.1  tsutsui 
    652  1.1  tsutsui  get_intr:
    653  1.1  tsutsui #ifdef DEBUG
    654  1.1  tsutsui 	printf("scintr: INTS 0x%x, SSTS 0x%x,  PCTL 0x%x,  PSNS 0x%x    0x%x\n",
    655  1.1  tsutsui 	        ints, hd->scsi_ssts, hd->scsi_pctl, hd->scsi_psns,
    656  1.1  tsutsui 	        hs->sc_phase);
    657  1.1  tsutsui #endif
    658  1.1  tsutsui 	if (ints & INTS_RESEL) {
    659  1.1  tsutsui 		if (hs->sc_phase == BUS_FREE_PHASE) {
    660  1.1  tsutsui 			temp = hd->scsi_temp & ~(1 << SCSI_ID);
    661  1.1  tsutsui 			for (i = 0; temp != 1; i++) {
    662  1.1  tsutsui 				temp >>= 1;
    663  1.1  tsutsui 			}
    664  1.1  tsutsui 			hs->sc_target = i;
    665  1.1  tsutsui 			*(hs->sc_lock) = SC_IN_PROGRESS;
    666  1.4  tsutsui 		} else
    667  1.1  tsutsui 			goto abort;
    668  1.1  tsutsui 	} else if (ints & INTS_DISCON) {
    669  1.1  tsutsui 		if ((hs->sc_msg[0] == MSG_CMD_COMPLETE) || (hs->sc_msg[0] == MSG_DISCONNECT)) {
    670  1.1  tsutsui 			hs->sc_phase  = BUS_FREE_PHASE;
    671  1.1  tsutsui 			hs->sc_target = SCSI_ID;
    672  1.4  tsutsui 			if (hs->sc_msg[0] == MSG_CMD_COMPLETE)
    673  1.1  tsutsui 				/* SCSI IO complete */
    674  1.1  tsutsui 				*(hs->sc_lock) = SC_IO_COMPLETE;
    675  1.1  tsutsui 			else
    676  1.1  tsutsui 				/* Cisconnected from Target */
    677  1.1  tsutsui 				*(hs->sc_lock) = SC_DISCONNECTED;
    678  1.1  tsutsui 			hd->scsi_ints = ints;
    679  1.1  tsutsui 			return 0;
    680  1.1  tsutsui 		} else
    681  1.1  tsutsui 			goto abort;
    682  1.1  tsutsui 	} else if (ints & INTS_CMD_DONE) {
    683  1.1  tsutsui 		if (hs->sc_phase == BUS_FREE_PHASE)
    684  1.1  tsutsui 			goto abort;
    685  1.1  tsutsui 		else if (hs->sc_phase == MESG_IN_PHASE) {
    686  1.1  tsutsui 			hd->scsi_scmd = SCMD_RST_ACK;
    687  1.1  tsutsui 			hd->scsi_ints = ints;
    688  1.1  tsutsui 			hs->sc_phase  = hd->scsi_psns & PHASE;
    689  1.1  tsutsui 			return 0;
    690  1.1  tsutsui 		}
    691  1.1  tsutsui 		if (hs->sc_flags & SC_SEL_TIMEOUT)
    692  1.1  tsutsui 			hs->sc_flags &= ~SC_SEL_TIMEOUT;
    693  1.1  tsutsui 	} else if (ints & INTS_SRV_REQ) {
    694  1.1  tsutsui 		if (hs->sc_phase != MESG_IN_PHASE)
    695  1.1  tsutsui 			goto abort;
    696  1.1  tsutsui 	} else if (ints & INTS_TIMEOUT) {
    697  1.1  tsutsui 		if (hs->sc_phase == ARB_SEL_PHASE) {
    698  1.1  tsutsui 			if (hs->sc_flags & SC_SEL_TIMEOUT) {
    699  1.1  tsutsui 				hs->sc_flags &= ~SC_SEL_TIMEOUT;
    700  1.1  tsutsui 				hs->sc_phase  = BUS_FREE_PHASE;
    701  1.1  tsutsui 				hs->sc_target = SCSI_ID;
    702  1.1  tsutsui 				/* Such SCSI Device is not conected. */
    703  1.1  tsutsui 				*(hs->sc_lock) = SC_DEV_NOT_FOUND;
    704  1.1  tsutsui 				hd->scsi_ints = ints;
    705  1.1  tsutsui 				return 0;
    706  1.1  tsutsui 			} else {
    707  1.1  tsutsui 				/* wait more 250 usec */
    708  1.1  tsutsui 				hs->sc_flags |= SC_SEL_TIMEOUT;
    709  1.1  tsutsui 				hd->scsi_temp = 0;
    710  1.1  tsutsui 				hd->scsi_tch  = 0;
    711  1.1  tsutsui 				hd->scsi_tcm  = 0x06;
    712  1.1  tsutsui 				hd->scsi_tcl  = 0x40;
    713  1.1  tsutsui 				hd->scsi_ints = ints;
    714  1.1  tsutsui 				return 0;
    715  1.1  tsutsui 			}
    716  1.1  tsutsui 		} else
    717  1.1  tsutsui 			goto abort;
    718  1.1  tsutsui 	} else
    719  1.1  tsutsui 		goto abort;
    720  1.1  tsutsui 
    721  1.1  tsutsui 	hd->scsi_ints = ints;
    722  1.1  tsutsui 
    723  1.1  tsutsui 	/*
    724  1.1  tsutsui 	 * Next SCSI Transfer
    725  1.1  tsutsui 	 */
    726  1.1  tsutsui 
    727  1.1  tsutsui 	while ((hd->scsi_psns & PSNS_REQ) == 0) {
    728  1.1  tsutsui 		DELAY(1);
    729  1.1  tsutsui 	}
    730  1.1  tsutsui 
    731  1.1  tsutsui 	hs->sc_phase = hd->scsi_psns & PHASE;
    732  1.1  tsutsui 
    733  1.1  tsutsui 	if ((hs->sc_phase == DATA_OUT_PHASE) || (hs->sc_phase == DATA_IN_PHASE)) {
    734  1.1  tsutsui 		len = hs->sc_len;
    735  1.1  tsutsui 		buf = hs->sc_buf;
    736  1.1  tsutsui 	} else if (hs->sc_phase == CMD_PHASE) {
    737  1.1  tsutsui 		len = hs->sc_cdblen;
    738  1.1  tsutsui 		buf = hs->sc_cdb;
    739  1.1  tsutsui 	} else if (hs->sc_phase == STATUS_PHASE) {
    740  1.1  tsutsui 		len = 1;
    741  1.1  tsutsui 		buf = &hs->sc_stat;
    742  1.1  tsutsui 	} else {
    743  1.1  tsutsui 		len = 1;
    744  1.1  tsutsui 		buf = hs->sc_msg;
    745  1.1  tsutsui 	}
    746  1.1  tsutsui 
    747  1.1  tsutsui 	ixfer_start(hd, len, hs->sc_phase, 0);
    748  1.1  tsutsui 	if (hs->sc_phase & PHASE_IO)
    749  1.1  tsutsui 		ixfer_in(hd, len, buf);
    750  1.1  tsutsui 	else
    751  1.1  tsutsui 		ixfer_out(hd, len, buf);
    752  1.1  tsutsui 
    753  1.1  tsutsui 	return 0;
    754  1.1  tsutsui 
    755  1.1  tsutsui 	/*
    756  1.1  tsutsui 	 * SCSI Abort
    757  1.1  tsutsui 	 */
    758  1.1  tsutsui  abort:
    759  1.1  tsutsui 	/* SCSI IO failed */
    760  1.9  tsutsui 	scabort(hs);
    761  1.1  tsutsui 	hd->scsi_ints = ints;
    762  1.1  tsutsui 	*(hs->sc_lock) = SC_IO_FAILED;
    763  1.1  tsutsui 	return -1;
    764  1.1  tsutsui }
    765