Home | History | Annotate | Line # | Download | only in boot
sc.c revision 1.13
      1  1.13  tsutsui /*	$NetBSD: sc.c,v 1.13 2015/02/14 05:58:02 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.12  tsutsui static int issue_select(struct scsidevice *, uint8_t);
     91  1.12  tsutsui static void ixfer_start(struct scsidevice *, int, uint8_t, int);
     92  1.12  tsutsui static void ixfer_out(struct scsidevice *, int, uint8_t *);
     93  1.12  tsutsui static void ixfer_in(struct scsidevice *, int, uint8_t *);
     94  1.12  tsutsui static int scrun(int, int, uint8_t *, int, uint8_t *, 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.10  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.12  tsutsui 			uint8_t 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.12  tsutsui 	if (scsi_immed_command(ctlr, target, lun, &inq, (uint8_t *)&inqbuf,
    212   1.9  tsutsui 			       sizeof(inqbuf)) ||
    213  1.12  tsutsui 	    scsi_immed_command(ctlr, target, lun, &cap, (uint8_t *)&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.12  tsutsui issue_select(struct scsidevice *hd, uint8_t target)
    275   1.1  tsutsui {
    276  1.10  tsutsui 
    277   1.1  tsutsui 	hd->scsi_pctl = 0;
    278   1.1  tsutsui 	hd->scsi_temp = (1 << SCSI_ID) | (1 << target);
    279   1.1  tsutsui 
    280   1.5  tsutsui 	/* select timeout is hardcoded to 250ms */
    281   1.5  tsutsui 	hd->scsi_tch = 2;
    282   1.5  tsutsui 	hd->scsi_tcm = 113;
    283   1.5  tsutsui 	hd->scsi_tcl = 3;
    284   1.1  tsutsui 
    285   1.1  tsutsui 	hd->scsi_scmd = SCMD_SELECT;
    286   1.1  tsutsui 
    287  1.10  tsutsui 	return 1;
    288   1.1  tsutsui }
    289   1.1  tsutsui 
    290   1.1  tsutsui 
    291   1.1  tsutsui /*
    292   1.1  tsutsui  * SPC Manual Transfer routines
    293   1.1  tsutsui  */
    294   1.1  tsutsui 
    295   1.1  tsutsui /* not yet */
    296   1.1  tsutsui 
    297   1.1  tsutsui 
    298   1.1  tsutsui /*
    299   1.1  tsutsui  * SPC Program Transfer routines
    300   1.1  tsutsui  */
    301   1.1  tsutsui 
    302   1.1  tsutsui void
    303  1.12  tsutsui ixfer_start(struct scsidevice *hd, int len, uint8_t phase, int wait)
    304   1.1  tsutsui {
    305  1.10  tsutsui 
    306   1.1  tsutsui 	hd->scsi_tch  = ((len & 0xff0000) >> 16);
    307   1.1  tsutsui 	hd->scsi_tcm  = ((len & 0x00ff00) >>  8);
    308   1.1  tsutsui 	hd->scsi_tcl  =  (len & 0x0000ff);
    309   1.1  tsutsui 	hd->scsi_pctl = phase;
    310   1.1  tsutsui 	hd->scsi_scmd = SCMD_XFR | SCMD_PROG_XFR;
    311   1.1  tsutsui }
    312   1.1  tsutsui 
    313   1.1  tsutsui void
    314  1.12  tsutsui ixfer_out(struct scsidevice *hd, int len, uint8_t *buf)
    315   1.1  tsutsui {
    316  1.10  tsutsui 
    317  1.10  tsutsui 	for (; len > 0; len--) {
    318   1.1  tsutsui 		while (hd->scsi_ssts & SSTS_DREG_FULL) {
    319   1.1  tsutsui 			DELAY(5);
    320   1.1  tsutsui 		}
    321   1.1  tsutsui 		hd->scsi_dreg = *buf++;
    322   1.1  tsutsui 	}
    323   1.1  tsutsui }
    324   1.1  tsutsui 
    325   1.1  tsutsui void
    326  1.12  tsutsui ixfer_in(struct scsidevice *hd, int len, uint8_t *buf)
    327   1.1  tsutsui {
    328  1.10  tsutsui 
    329   1.1  tsutsui 	for (; len > 0; len--) {
    330   1.1  tsutsui 		while (hd->scsi_ssts & SSTS_DREG_EMPTY) {
    331   1.1  tsutsui 			DELAY(5);
    332   1.1  tsutsui 		}
    333   1.1  tsutsui 		*buf++ = hd->scsi_dreg;
    334   1.1  tsutsui 	}
    335   1.1  tsutsui }
    336   1.1  tsutsui 
    337   1.1  tsutsui 
    338   1.1  tsutsui /*
    339   1.1  tsutsui  * SPC drive routines
    340   1.1  tsutsui  */
    341   1.1  tsutsui 
    342   1.1  tsutsui int
    343  1.12  tsutsui scrun(int ctlr, int target, uint8_t *cdb, int cdblen, uint8_t *buf, int len,
    344   1.1  tsutsui     volatile int *lock)
    345   1.1  tsutsui {
    346   1.6  tsutsui 	struct scsi_softc *hs;
    347   1.6  tsutsui 	struct scsidevice *hd;
    348   1.6  tsutsui 
    349   1.6  tsutsui 	if (ctlr < 0 || ctlr >= NSC)
    350   1.6  tsutsui 		return 0;
    351   1.6  tsutsui 
    352   1.6  tsutsui 	hs = &scsi_softc[ctlr];
    353   1.9  tsutsui 	hd = hs->sc_spc;
    354   1.9  tsutsui 	if (hd == NULL)
    355   1.9  tsutsui 		return 0;
    356   1.1  tsutsui 
    357  1.13  tsutsui 	if ((hd->scsi_ssts & (SSTS_INITIATOR | SSTS_TARGET | SSTS_BUSY)) != 0)
    358  1.10  tsutsui 		return 0;
    359   1.1  tsutsui 
    360   1.1  tsutsui 	hs->sc_flags  = 0;
    361   1.1  tsutsui 	hs->sc_phase  = ARB_SEL_PHASE;
    362   1.9  tsutsui 	hs->sc_target = target;
    363   1.1  tsutsui 
    364   1.1  tsutsui 	hs->sc_cdb    = cdb;
    365   1.1  tsutsui 	hs->sc_cdblen = cdblen;
    366   1.1  tsutsui 	hs->sc_buf    = buf;
    367   1.1  tsutsui 	hs->sc_len    = len;
    368   1.1  tsutsui 	hs->sc_lock   = lock;
    369   1.1  tsutsui 
    370   1.1  tsutsui 	hs->sc_stat   = 0;
    371   1.1  tsutsui 	hs->sc_msg[0] = 0;
    372   1.1  tsutsui 
    373   1.1  tsutsui 	*(hs->sc_lock) = SC_IN_PROGRESS;
    374   1.1  tsutsui 	issue_select(hd, hs->sc_target);
    375   1.1  tsutsui 
    376  1.10  tsutsui 	return 1;
    377   1.1  tsutsui }
    378   1.1  tsutsui 
    379   1.1  tsutsui int
    380   1.1  tsutsui scfinish(int ctlr)
    381   1.1  tsutsui {
    382   1.1  tsutsui 	struct scsi_softc *hs = &scsi_softc[ctlr];
    383   1.1  tsutsui 	int status = hs->sc_stat;
    384   1.1  tsutsui 
    385   1.1  tsutsui 	hs->sc_flags  = 0;
    386   1.1  tsutsui 	hs->sc_phase  = BUS_FREE_PHASE;
    387   1.1  tsutsui 	hs->sc_target = SCSI_ID;
    388   1.1  tsutsui 
    389   1.1  tsutsui 	hs->sc_cdb    = NULL;
    390   1.1  tsutsui 	hs->sc_cdblen = 0;
    391   1.1  tsutsui 	hs->sc_buf    = NULL;
    392   1.1  tsutsui 	hs->sc_len    = 0;
    393   1.1  tsutsui 	hs->sc_lock   = NULL;
    394   1.1  tsutsui 
    395   1.1  tsutsui 	hs->sc_stat   = 0;
    396   1.1  tsutsui 	hs->sc_msg[0] = 0;
    397   1.1  tsutsui 
    398  1.10  tsutsui 	return status;
    399   1.1  tsutsui }
    400   1.1  tsutsui 
    401   1.1  tsutsui void
    402   1.9  tsutsui scabort(struct scsi_softc *hs)
    403   1.1  tsutsui {
    404   1.9  tsutsui 	struct scsidevice *hd = hs->sc_spc;
    405   1.1  tsutsui 	int len;
    406   1.1  tsutsui 
    407   1.1  tsutsui 	printf("sc%d: abort  phase=0x%x, ssts=0x%x, ints=0x%x\n",
    408  1.13  tsutsui 	    hs->sc_ctlr, hd->scsi_psns, hd->scsi_ssts, hd->scsi_ints);
    409   1.1  tsutsui 
    410   1.1  tsutsui 	if (hd->scsi_ints != 0)
    411   1.1  tsutsui 		hd->scsi_ints = hd->scsi_ints;
    412   1.1  tsutsui 
    413   1.1  tsutsui 	if (hd->scsi_psns == 0 || (hd->scsi_ssts & SSTS_INITIATOR) == 0)
    414   1.1  tsutsui 		/* no longer connected to scsi target */
    415   1.1  tsutsui 		return;
    416   1.1  tsutsui 
    417   1.1  tsutsui 	/* get the number of bytes remaining in current xfer + fudge */
    418   1.1  tsutsui 	len = (hd->scsi_tch << 16) | (hd->scsi_tcm << 8) | hd->scsi_tcl;
    419   1.1  tsutsui 
    420   1.1  tsutsui 	/* for that many bus cycles, try to send an abort msg */
    421  1.13  tsutsui 	for (len += 1024;
    422  1.13  tsutsui 	    ((hd->scsi_ssts & SSTS_INITIATOR)) != 0 && --len >= 0;) {
    423   1.1  tsutsui 		hd->scsi_scmd = SCMD_SET_ATN;
    424   1.1  tsutsui 
    425   1.1  tsutsui 		while ((hd->scsi_psns & PSNS_REQ) == 0) {
    426  1.10  tsutsui 			if ((hd->scsi_ssts & SSTS_INITIATOR) == 0)
    427   1.1  tsutsui 				goto out;
    428   1.1  tsutsui 			DELAY(1);
    429   1.1  tsutsui 		}
    430   1.1  tsutsui 
    431   1.1  tsutsui 		if ((hd->scsi_psns & PHASE) == MESG_OUT_PHASE)
    432   1.1  tsutsui 			hd->scsi_scmd = SCMD_RST_ATN;
    433   1.1  tsutsui 		hd->scsi_pctl = hs->sc_phase = hd->scsi_psns & PHASE;
    434   1.1  tsutsui 
    435   1.1  tsutsui 		if (hd->scsi_psns & PHASE_IO) {
    436   1.1  tsutsui 			/* one of the input phases - read & discard a byte */
    437   1.1  tsutsui 			hd->scsi_scmd = SCMD_SET_ACK;
    438  1.13  tsutsui 			while ((hd->scsi_psns & PSNS_REQ) != 0)
    439   1.1  tsutsui 				DELAY(1);
    440  1.11  tsutsui 			(void)hd->scsi_temp;
    441   1.1  tsutsui 		} else {
    442   1.1  tsutsui 			/* one of the output phases - send an abort msg */
    443   1.1  tsutsui 			hd->scsi_temp = MSG_ABORT;
    444   1.1  tsutsui 			hd->scsi_scmd = SCMD_SET_ACK;
    445  1.13  tsutsui 			while ((hd->scsi_psns & PSNS_REQ) != 0)
    446   1.1  tsutsui 				DELAY(1);
    447   1.1  tsutsui 		}
    448   1.1  tsutsui 
    449   1.1  tsutsui 		hd->scsi_scmd = SCMD_RST_ACK;
    450   1.1  tsutsui 	}
    451   1.1  tsutsui out:
    452   1.1  tsutsui 	/*
    453   1.1  tsutsui 	 * Either the abort was successful & the bus is disconnected or
    454   1.1  tsutsui 	 * the device didn't listen.  If the latter, announce the problem.
    455   1.1  tsutsui 	 * Either way, reset the card & the SPC.
    456   1.1  tsutsui 	 */
    457   1.1  tsutsui 	if (len < 0 && hs)
    458   1.1  tsutsui 		printf("sc%d: abort failed.  phase=0x%x, ssts=0x%x\n",
    459  1.13  tsutsui 		    hs->sc_ctlr, hd->scsi_psns, hd->scsi_ssts);
    460   1.1  tsutsui }
    461   1.1  tsutsui 
    462   1.1  tsutsui 
    463   1.1  tsutsui /*
    464   1.1  tsutsui  * SCSI Command Handler
    465   1.1  tsutsui  */
    466   1.1  tsutsui 
    467   1.1  tsutsui int
    468   1.9  tsutsui scsi_test_unit_rdy(int ctlr, int target, int lun)
    469   1.1  tsutsui {
    470   1.1  tsutsui 	static struct scsi_cdb6 cdb = { CMD_TEST_UNIT_READY };
    471   1.1  tsutsui 	int status;
    472   1.1  tsutsui 	volatile int lock;
    473   1.1  tsutsui 
    474   1.1  tsutsui #ifdef DEBUG
    475   1.9  tsutsui 	printf("scsi_test_unit_rdy( %d, %d, %d): Start\n", ctlr, target, lun);
    476   1.1  tsutsui #endif
    477   1.1  tsutsui 
    478   1.9  tsutsui 	cdb.lun = lun;
    479   1.1  tsutsui 
    480  1.13  tsutsui 	if (scrun(ctlr, target, (void *)&cdb, 6, NULL, 0, &lock) == 0) {
    481   1.1  tsutsui #ifdef DEBUG
    482   1.1  tsutsui 		printf("scsi_test_unit_rdy: Command Transfer Failed.\n");
    483   1.1  tsutsui #endif
    484  1.10  tsutsui 		return -1;
    485   1.1  tsutsui 	}
    486   1.1  tsutsui 
    487   1.1  tsutsui 	while ((lock == SC_IN_PROGRESS) || (lock == SC_DISCONNECTED))
    488   1.1  tsutsui 		DELAY(10);
    489   1.1  tsutsui 
    490   1.1  tsutsui 	status = scfinish(ctlr);
    491   1.1  tsutsui 
    492   1.1  tsutsui 	if (lock == SC_IO_COMPLETE) {
    493   1.1  tsutsui #ifdef DEBUG
    494   1.1  tsutsui 		printf("scsi_test_unit_rdy: Status -- 0x%x\n", status);
    495   1.1  tsutsui #endif
    496  1.10  tsutsui 		return status;
    497   1.1  tsutsui 	} else {
    498  1.10  tsutsui 		return lock;
    499   1.1  tsutsui 	}
    500   1.1  tsutsui }
    501   1.1  tsutsui 
    502   1.1  tsutsui int
    503  1.12  tsutsui scsi_request_sense(int ctlr, int target, int lun, uint8_t *buf,
    504  1.12  tsutsui     unsigned int len)
    505   1.1  tsutsui {
    506   1.1  tsutsui 	static struct scsi_cdb6 cdb = {	CMD_REQUEST_SENSE };
    507   1.1  tsutsui 	int status;
    508   1.1  tsutsui 	volatile int lock;
    509   1.1  tsutsui 
    510   1.1  tsutsui #ifdef DEBUG
    511   1.1  tsutsui 	printf("scsi_request_sense: Start\n");
    512   1.1  tsutsui #endif
    513   1.1  tsutsui 
    514   1.7  tsutsui 	/* Request Sense        */
    515   1.7  tsutsui 	/* Additional Sens Length*/
    516   1.7  tsutsui 	/* cdbAllocation Length */
    517   1.7  tsutsui 	/*          */
    518   1.1  tsutsui 
    519   1.7  tsutsui 	/* Addtional Sens Field */
    520   1.7  tsutsui 	/* len            */
    521   1.1  tsutsui 
    522   1.9  tsutsui 	cdb.lun = lun;
    523   1.1  tsutsui 	cdb.len = len;
    524   1.1  tsutsui 
    525  1.13  tsutsui 	if (scrun(ctlr, target, (void *)&cdb, 6, buf, len, &lock) == 0) {
    526   1.1  tsutsui #ifdef DEBUG
    527   1.1  tsutsui 		printf("scsi_request_sense: Command Transfer Failed.\n");
    528   1.1  tsutsui #endif
    529  1.10  tsutsui 		return -1;
    530   1.1  tsutsui 	}
    531   1.1  tsutsui 
    532   1.1  tsutsui 	while ((lock == SC_IN_PROGRESS) || (lock == SC_DISCONNECTED))
    533   1.1  tsutsui 		DELAY(10);
    534   1.1  tsutsui 
    535   1.1  tsutsui 	status = scfinish(ctlr);
    536   1.1  tsutsui 
    537   1.1  tsutsui 	if (lock == SC_IO_COMPLETE) {
    538   1.1  tsutsui #ifdef DEBUG
    539   1.1  tsutsui 		printf("scsi_request_sense: Status -- 0x%x\n", status);
    540   1.1  tsutsui #endif
    541  1.10  tsutsui 		return status;
    542   1.1  tsutsui 	} else {
    543  1.10  tsutsui 		return lock;
    544   1.1  tsutsui 	}
    545   1.1  tsutsui }
    546   1.1  tsutsui 
    547   1.1  tsutsui int
    548   1.9  tsutsui scsi_immed_command(int ctlr, int target, int lun, struct scsi_generic_cdb *cdb,
    549  1.12  tsutsui     uint8_t *buf, unsigned int len)
    550   1.1  tsutsui {
    551   1.1  tsutsui 	int status;
    552   1.1  tsutsui 	volatile int lock;
    553   1.1  tsutsui 
    554   1.1  tsutsui #ifdef DEBUG
    555   1.1  tsutsui 	printf("scsi_immed_command( %d, %d, %d, cdb(%d), buf, %d): Start\n",
    556  1.13  tsutsui 	    ctlr, target, lun, cdb->len, len);
    557   1.1  tsutsui #endif
    558   1.1  tsutsui 
    559   1.9  tsutsui 	cdb->cdb[1] |= lun << 5;
    560   1.1  tsutsui 
    561  1.13  tsutsui 	if (scrun(ctlr, target, (void *)&cdb->cdb[0], cdb->len, buf, len,
    562  1.13  tsutsui 	    &lock) == 0) {
    563   1.1  tsutsui #ifdef DEBUG
    564   1.1  tsutsui 		printf("scsi_immed_command: Command Transfer Failed.\n");
    565   1.1  tsutsui #endif
    566  1.10  tsutsui 		return -1;
    567   1.1  tsutsui 	}
    568   1.1  tsutsui 
    569   1.1  tsutsui 	while ((lock == SC_IN_PROGRESS) || (lock == SC_DISCONNECTED))
    570   1.1  tsutsui 		DELAY(10);
    571   1.1  tsutsui 
    572   1.1  tsutsui 	status = scfinish(ctlr);
    573   1.1  tsutsui 
    574   1.1  tsutsui 	if (lock == SC_IO_COMPLETE) {
    575   1.1  tsutsui #ifdef DEBUG
    576   1.1  tsutsui 		printf("scsi_immed_command: Status -- 0x%x\n", status);
    577   1.1  tsutsui #endif
    578  1.10  tsutsui 		return status;
    579   1.1  tsutsui 	} else {
    580  1.10  tsutsui 		return lock;
    581   1.1  tsutsui 	}
    582   1.1  tsutsui }
    583   1.1  tsutsui 
    584   1.1  tsutsui int
    585   1.9  tsutsui scsi_format_unit(int ctlr, int target, int lun)
    586   1.1  tsutsui {
    587   1.1  tsutsui 	static struct scsi_cdb6 cdb = { CMD_FORMAT_UNIT, 0, 0, 0, 0, 0 };
    588   1.1  tsutsui 	int status;
    589   1.1  tsutsui 	volatile int lock;
    590   1.1  tsutsui #ifdef DEBUG
    591   1.1  tsutsui 	int count = 0;
    592   1.1  tsutsui #endif
    593   1.1  tsutsui 
    594   1.1  tsutsui #ifdef DEBUG
    595   1.9  tsutsui 	printf("scsi_format_unit( %d, %d, %d): Start\n", ctlr, target, lun);
    596   1.1  tsutsui #endif
    597   1.1  tsutsui 
    598   1.9  tsutsui 	cdb.lun = lun;
    599   1.1  tsutsui 
    600  1.13  tsutsui 	if (scrun(ctlr, target, (void *)&cdb, 6, NULL, 0, &lock) == 0) {
    601   1.1  tsutsui #ifdef DEBUG
    602   1.1  tsutsui 		printf("scsi_format_unit: Command Transfer Failed.\n");
    603   1.1  tsutsui #endif
    604  1.10  tsutsui 		return -1;
    605   1.1  tsutsui 	}
    606   1.1  tsutsui 
    607   1.1  tsutsui 	while ((lock == SC_IN_PROGRESS) || (lock == SC_DISCONNECTED)) {
    608   1.1  tsutsui 		DELAY(1000000);
    609   1.1  tsutsui #ifdef DEBUG
    610   1.1  tsutsui 		if ((++count % 60) == 0)
    611   1.1  tsutsui 			printf("scsi_format_unit: %d\n", count / 60);
    612   1.1  tsutsui #endif
    613   1.1  tsutsui 	}
    614   1.1  tsutsui 
    615   1.1  tsutsui 	status = scfinish(ctlr);
    616   1.1  tsutsui 
    617   1.1  tsutsui 	if (lock == SC_IO_COMPLETE) {
    618   1.1  tsutsui #ifdef DEBUG
    619   1.1  tsutsui 		printf("scsi_format_unit: Status -- 0x%x\n", status);
    620   1.1  tsutsui #endif
    621  1.10  tsutsui 		return status;
    622   1.1  tsutsui 	} else {
    623  1.10  tsutsui 		return lock;
    624   1.1  tsutsui 	}
    625   1.1  tsutsui }
    626   1.1  tsutsui 
    627   1.1  tsutsui 
    628   1.1  tsutsui /*
    629   1.1  tsutsui  * Interrupt Routine
    630   1.1  tsutsui  */
    631   1.1  tsutsui 
    632   1.1  tsutsui int
    633   1.1  tsutsui scintr(void)
    634   1.1  tsutsui {
    635   1.1  tsutsui 	struct scsi_softc *hs;
    636   1.1  tsutsui 	struct scsidevice *hd;
    637  1.12  tsutsui 	uint8_t ints, temp;
    638   1.1  tsutsui 	int i;
    639  1.12  tsutsui 	uint8_t *buf;
    640   1.1  tsutsui 	int len;
    641   1.1  tsutsui 
    642   1.1  tsutsui 	for (i = 0; i < NSC; i++) {
    643   1.1  tsutsui 		hs = &scsi_softc[i];
    644   1.9  tsutsui 		hd = hs->sc_spc;
    645   1.1  tsutsui 		if ((ints = hd->scsi_ints) != 0)
    646   1.1  tsutsui 			goto get_intr;
    647   1.1  tsutsui 	}
    648   1.1  tsutsui 
    649   1.1  tsutsui 	/* Unknown Interrupt occured */
    650   1.1  tsutsui 	return -1;
    651   1.1  tsutsui 
    652   1.1  tsutsui 
    653   1.1  tsutsui 	/*
    654   1.1  tsutsui 	 * Interrupt
    655   1.1  tsutsui 	 */
    656   1.1  tsutsui 
    657   1.1  tsutsui  get_intr:
    658   1.1  tsutsui #ifdef DEBUG
    659   1.1  tsutsui 	printf("scintr: INTS 0x%x, SSTS 0x%x,  PCTL 0x%x,  PSNS 0x%x    0x%x\n",
    660  1.13  tsutsui 	    ints, hd->scsi_ssts, hd->scsi_pctl, hd->scsi_psns, hs->sc_phase);
    661   1.1  tsutsui #endif
    662   1.1  tsutsui 	if (ints & INTS_RESEL) {
    663   1.1  tsutsui 		if (hs->sc_phase == BUS_FREE_PHASE) {
    664   1.1  tsutsui 			temp = hd->scsi_temp & ~(1 << SCSI_ID);
    665   1.1  tsutsui 			for (i = 0; temp != 1; i++) {
    666   1.1  tsutsui 				temp >>= 1;
    667   1.1  tsutsui 			}
    668   1.1  tsutsui 			hs->sc_target = i;
    669   1.1  tsutsui 			*(hs->sc_lock) = SC_IN_PROGRESS;
    670   1.4  tsutsui 		} else
    671   1.1  tsutsui 			goto abort;
    672   1.1  tsutsui 	} else if (ints & INTS_DISCON) {
    673  1.10  tsutsui 		if ((hs->sc_msg[0] == MSG_CMD_COMPLETE) ||
    674  1.10  tsutsui 		    (hs->sc_msg[0] == MSG_DISCONNECT)) {
    675   1.1  tsutsui 			hs->sc_phase  = BUS_FREE_PHASE;
    676   1.1  tsutsui 			hs->sc_target = SCSI_ID;
    677  1.13  tsutsui 			if (hs->sc_msg[0] == MSG_CMD_COMPLETE) {
    678   1.1  tsutsui 				/* SCSI IO complete */
    679   1.1  tsutsui 				*(hs->sc_lock) = SC_IO_COMPLETE;
    680  1.13  tsutsui 			} else {
    681  1.13  tsutsui 				/* Disconnected from Target */
    682   1.1  tsutsui 				*(hs->sc_lock) = SC_DISCONNECTED;
    683  1.13  tsutsui 			}
    684   1.1  tsutsui 			hd->scsi_ints = ints;
    685   1.1  tsutsui 			return 0;
    686   1.1  tsutsui 		} else
    687   1.1  tsutsui 			goto abort;
    688   1.1  tsutsui 	} else if (ints & INTS_CMD_DONE) {
    689   1.1  tsutsui 		if (hs->sc_phase == BUS_FREE_PHASE)
    690   1.1  tsutsui 			goto abort;
    691   1.1  tsutsui 		else if (hs->sc_phase == MESG_IN_PHASE) {
    692   1.1  tsutsui 			hd->scsi_scmd = SCMD_RST_ACK;
    693   1.1  tsutsui 			hd->scsi_ints = ints;
    694   1.1  tsutsui 			hs->sc_phase  = hd->scsi_psns & PHASE;
    695   1.1  tsutsui 			return 0;
    696   1.1  tsutsui 		}
    697   1.1  tsutsui 		if (hs->sc_flags & SC_SEL_TIMEOUT)
    698   1.1  tsutsui 			hs->sc_flags &= ~SC_SEL_TIMEOUT;
    699   1.1  tsutsui 	} else if (ints & INTS_SRV_REQ) {
    700   1.1  tsutsui 		if (hs->sc_phase != MESG_IN_PHASE)
    701   1.1  tsutsui 			goto abort;
    702   1.1  tsutsui 	} else if (ints & INTS_TIMEOUT) {
    703   1.1  tsutsui 		if (hs->sc_phase == ARB_SEL_PHASE) {
    704   1.1  tsutsui 			if (hs->sc_flags & SC_SEL_TIMEOUT) {
    705   1.1  tsutsui 				hs->sc_flags &= ~SC_SEL_TIMEOUT;
    706   1.1  tsutsui 				hs->sc_phase  = BUS_FREE_PHASE;
    707   1.1  tsutsui 				hs->sc_target = SCSI_ID;
    708  1.13  tsutsui 				/* Such SCSI Device is not connected. */
    709   1.1  tsutsui 				*(hs->sc_lock) = SC_DEV_NOT_FOUND;
    710   1.1  tsutsui 				hd->scsi_ints = ints;
    711   1.1  tsutsui 				return 0;
    712   1.1  tsutsui 			} else {
    713   1.1  tsutsui 				/* wait more 250 usec */
    714   1.1  tsutsui 				hs->sc_flags |= SC_SEL_TIMEOUT;
    715   1.1  tsutsui 				hd->scsi_temp = 0;
    716   1.1  tsutsui 				hd->scsi_tch  = 0;
    717   1.1  tsutsui 				hd->scsi_tcm  = 0x06;
    718   1.1  tsutsui 				hd->scsi_tcl  = 0x40;
    719   1.1  tsutsui 				hd->scsi_ints = ints;
    720   1.1  tsutsui 				return 0;
    721   1.1  tsutsui 			}
    722   1.1  tsutsui 		} else
    723   1.1  tsutsui 			goto abort;
    724   1.1  tsutsui 	} else
    725   1.1  tsutsui 		goto abort;
    726   1.1  tsutsui 
    727   1.1  tsutsui 	hd->scsi_ints = ints;
    728   1.1  tsutsui 
    729   1.1  tsutsui 	/*
    730   1.1  tsutsui 	 * Next SCSI Transfer
    731   1.1  tsutsui 	 */
    732   1.1  tsutsui 
    733   1.1  tsutsui 	while ((hd->scsi_psns & PSNS_REQ) == 0) {
    734   1.1  tsutsui 		DELAY(1);
    735   1.1  tsutsui 	}
    736   1.1  tsutsui 
    737   1.1  tsutsui 	hs->sc_phase = hd->scsi_psns & PHASE;
    738   1.1  tsutsui 
    739  1.10  tsutsui 	if ((hs->sc_phase == DATA_OUT_PHASE) ||
    740  1.10  tsutsui 	    (hs->sc_phase == DATA_IN_PHASE)) {
    741   1.1  tsutsui 		len = hs->sc_len;
    742   1.1  tsutsui 		buf = hs->sc_buf;
    743   1.1  tsutsui 	} else if (hs->sc_phase == CMD_PHASE) {
    744   1.1  tsutsui 		len = hs->sc_cdblen;
    745   1.1  tsutsui 		buf = hs->sc_cdb;
    746   1.1  tsutsui 	} else if (hs->sc_phase == STATUS_PHASE) {
    747   1.1  tsutsui 		len = 1;
    748   1.1  tsutsui 		buf = &hs->sc_stat;
    749   1.1  tsutsui 	} else {
    750   1.1  tsutsui 		len = 1;
    751   1.1  tsutsui 		buf = hs->sc_msg;
    752   1.1  tsutsui 	}
    753   1.1  tsutsui 
    754   1.1  tsutsui 	ixfer_start(hd, len, hs->sc_phase, 0);
    755   1.1  tsutsui 	if (hs->sc_phase & PHASE_IO)
    756   1.1  tsutsui 		ixfer_in(hd, len, buf);
    757   1.1  tsutsui 	else
    758   1.1  tsutsui 		ixfer_out(hd, len, buf);
    759   1.1  tsutsui 
    760   1.1  tsutsui 	return 0;
    761   1.1  tsutsui 
    762   1.1  tsutsui 	/*
    763   1.1  tsutsui 	 * SCSI Abort
    764   1.1  tsutsui 	 */
    765   1.1  tsutsui  abort:
    766   1.1  tsutsui 	/* SCSI IO failed */
    767   1.9  tsutsui 	scabort(hs);
    768   1.1  tsutsui 	hd->scsi_ints = ints;
    769   1.1  tsutsui 	*(hs->sc_lock) = SC_IO_FAILED;
    770   1.1  tsutsui 	return -1;
    771   1.1  tsutsui }
    772