Home | History | Annotate | Line # | Download | only in boot
sd.c revision 1.8
      1  1.8  tsutsui /*	$NetBSD: sd.c,v 1.8 2014/01/03 07:17:19 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  *	@(#)sd.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  *	@(#)sd.c	8.1 (Berkeley) 6/10/93
     71  1.1  tsutsui  */
     72  1.1  tsutsui 
     73  1.1  tsutsui /*
     74  1.1  tsutsui  * sd.c -- SCSI DISK device driver
     75  1.1  tsutsui  * by A.Fujita, FEB-26-1992
     76  1.1  tsutsui  */
     77  1.1  tsutsui 
     78  1.1  tsutsui 
     79  1.1  tsutsui /*
     80  1.1  tsutsui  * SCSI CCS (Command Command Set) disk driver.
     81  1.1  tsutsui  */
     82  1.1  tsutsui #include <sys/param.h>
     83  1.1  tsutsui #include <sys/disklabel.h>
     84  1.1  tsutsui #include <luna68k/stand/boot/samachdep.h>
     85  1.1  tsutsui #include <luna68k/stand/boot/scsireg.h>
     86  1.1  tsutsui #include <luna68k/stand/boot/device.h>
     87  1.1  tsutsui 
     88  1.1  tsutsui struct	disklabel sdlabel[NSD];
     89  1.1  tsutsui 
     90  1.1  tsutsui struct	sd_softc {
     91  1.1  tsutsui 	struct	hp_device *sc_hd;
     92  1.1  tsutsui 	short	sc_flags;
     93  1.1  tsutsui 	short	sc_type;	/* drive type */
     94  1.1  tsutsui 	short	sc_punit;	/* physical unit (scsi lun) */
     95  1.1  tsutsui 	u_short	sc_bshift;	/* convert device blocks to DEV_BSIZE blks */
     96  1.1  tsutsui 	u_int	sc_blks;	/* number of blocks on device */
     97  1.1  tsutsui 	int	sc_blksize;	/* device block size in bytes */
     98  1.1  tsutsui };
     99  1.1  tsutsui 
    100  1.1  tsutsui struct sd_devdata {
    101  1.1  tsutsui 	int	unit;		/* drive number */
    102  1.1  tsutsui 	int	part;		/* partition */
    103  1.1  tsutsui };
    104  1.5  tsutsui 
    105  1.1  tsutsui static int sdinit(void *);
    106  1.1  tsutsui static int sdident(struct sd_softc *, struct hp_device *);
    107  1.1  tsutsui 
    108  1.1  tsutsui struct	driver sddriver = {
    109  1.2  tsutsui 	sdinit, "sd", NULL,
    110  1.1  tsutsui };
    111  1.1  tsutsui 
    112  1.1  tsutsui struct sd_softc sd_softc[NSD];
    113  1.1  tsutsui struct sd_devdata sd_devdata[NSD];
    114  1.1  tsutsui 
    115  1.1  tsutsui /* sc_flags values */
    116  1.1  tsutsui #define	SDF_ALIVE	0x1
    117  1.1  tsutsui 
    118  1.1  tsutsui #define	sdunit(x)	((minor(x) >> 3) & 0x7)
    119  1.1  tsutsui #define sdpart(x)	(minor(x) & 0x7)
    120  1.1  tsutsui 
    121  1.1  tsutsui static struct scsi_inquiry inqbuf;
    122  1.8  tsutsui static struct scsi_generic_cdb inq = {
    123  1.1  tsutsui 	6,
    124  1.1  tsutsui 	{ CMD_INQUIRY, 0, 0, 0, sizeof(inqbuf), 0 }
    125  1.1  tsutsui };
    126  1.1  tsutsui 
    127  1.1  tsutsui static u_long capbuf[2];
    128  1.8  tsutsui struct scsi_generic_cdb cap = {
    129  1.1  tsutsui 	10,
    130  1.1  tsutsui 	{ CMD_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
    131  1.1  tsutsui };
    132  1.1  tsutsui 
    133  1.1  tsutsui int
    134  1.1  tsutsui sdident(struct sd_softc *sc, struct hp_device *hd)
    135  1.1  tsutsui {
    136  1.1  tsutsui 	char idstr[32];
    137  1.1  tsutsui 	int unit;
    138  1.1  tsutsui 	int ctlr, slave;
    139  1.1  tsutsui 	int i;
    140  1.1  tsutsui 	int tries = 10;
    141  1.1  tsutsui 
    142  1.7  tsutsui 	ctlr = hd->hpd_ctlr;
    143  1.7  tsutsui 	slave = hd->hpd_slave;
    144  1.1  tsutsui 	unit = sc->sc_punit;
    145  1.1  tsutsui 
    146  1.1  tsutsui 	/*
    147  1.1  tsutsui 	 * See if unit exists and is a disk then read block size & nblocks.
    148  1.1  tsutsui 	 */
    149  1.1  tsutsui 	while ((i = scsi_test_unit_rdy(ctlr, slave, unit)) != 0) {
    150  1.1  tsutsui 		if (i < 0 || --tries < 0)
    151  1.1  tsutsui 			return (-1);
    152  1.1  tsutsui 		if (i == STS_CHECKCOND) {
    153  1.1  tsutsui 			u_char sensebuf[8];
    154  1.1  tsutsui 			struct scsi_xsense *sp = (struct scsi_xsense *)sensebuf;
    155  1.1  tsutsui 
    156  1.1  tsutsui 			scsi_request_sense(ctlr, slave, unit, sensebuf, 8);
    157  1.1  tsutsui 			if (sp->class == 7 && sp->key == 6)
    158  1.1  tsutsui 				/* drive doing an RTZ -- give it a while */
    159  1.1  tsutsui 				DELAY(1000000);
    160  1.1  tsutsui 		}
    161  1.1  tsutsui 		DELAY(1000);
    162  1.1  tsutsui 	}
    163  1.1  tsutsui 	if (scsi_immed_command(ctlr, slave, unit, &inq, (u_char *)&inqbuf,
    164  1.1  tsutsui 			       sizeof(inqbuf)) ||
    165  1.1  tsutsui 	    scsi_immed_command(ctlr, slave, unit, &cap, (u_char *)&capbuf,
    166  1.1  tsutsui 			       sizeof(capbuf)))
    167  1.1  tsutsui 		/* doesn't exist or not a CCS device */
    168  1.1  tsutsui 		return (-1);
    169  1.1  tsutsui 
    170  1.1  tsutsui 	switch (inqbuf.type) {
    171  1.1  tsutsui 	case 0:		/* disk */
    172  1.1  tsutsui 	case 4:		/* WORM */
    173  1.1  tsutsui 	case 5:		/* CD-ROM */
    174  1.1  tsutsui 	case 7:		/* Magneto-optical */
    175  1.1  tsutsui 		break;
    176  1.1  tsutsui 	default:	/* not a disk */
    177  1.1  tsutsui 		return (-1);
    178  1.1  tsutsui 	}
    179  1.1  tsutsui 	sc->sc_blks    = capbuf[0];
    180  1.1  tsutsui 	sc->sc_blksize = capbuf[1];
    181  1.1  tsutsui 
    182  1.1  tsutsui 	memcpy(idstr, &inqbuf.vendor_id, 28);
    183  1.1  tsutsui 	for (i = 27; i > 23; --i)
    184  1.1  tsutsui 		if (idstr[i] != ' ')
    185  1.1  tsutsui 			break;
    186  1.1  tsutsui 	idstr[i+1] = 0;
    187  1.1  tsutsui 	for (i = 23; i > 7; --i)
    188  1.1  tsutsui 		if (idstr[i] != ' ')
    189  1.1  tsutsui 			break;
    190  1.1  tsutsui 	idstr[i+1] = 0;
    191  1.1  tsutsui 	for (i = 7; i >= 0; --i)
    192  1.1  tsutsui 		if (idstr[i] != ' ')
    193  1.1  tsutsui 			break;
    194  1.1  tsutsui 	idstr[i+1] = 0;
    195  1.7  tsutsui 	printf("sd%d: %s %s rev %s", hd->hpd_unit, idstr, &idstr[8],
    196  1.1  tsutsui 	       &idstr[24]);
    197  1.1  tsutsui 
    198  1.4  tsutsui 	printf(", %d bytes/sect x %d sectors\n", sc->sc_blksize, sc->sc_blks);
    199  1.1  tsutsui 	if (sc->sc_blksize != DEV_BSIZE) {
    200  1.1  tsutsui 		if (sc->sc_blksize < DEV_BSIZE) {
    201  1.1  tsutsui 			printf("sd%d: need %d byte blocks - drive ignored\n",
    202  1.1  tsutsui 				unit, DEV_BSIZE);
    203  1.1  tsutsui 			return (-1);
    204  1.1  tsutsui 		}
    205  1.1  tsutsui 		for (i = sc->sc_blksize; i > DEV_BSIZE; i >>= 1)
    206  1.1  tsutsui 			++sc->sc_bshift;
    207  1.1  tsutsui 		sc->sc_blks <<= sc->sc_bshift;
    208  1.1  tsutsui 	}
    209  1.1  tsutsui 	return(inqbuf.type);
    210  1.1  tsutsui }
    211  1.1  tsutsui 
    212  1.1  tsutsui int
    213  1.1  tsutsui sdinit(void *arg)
    214  1.1  tsutsui {
    215  1.1  tsutsui 	struct hp_device *hd = arg;
    216  1.7  tsutsui 	struct sd_softc *sc = &sd_softc[hd->hpd_unit];
    217  1.1  tsutsui 	struct disklabel *lp;
    218  1.1  tsutsui 	char *msg;
    219  1.1  tsutsui 
    220  1.1  tsutsui #ifdef DEBUG
    221  1.7  tsutsui 	printf("sdinit: hd->hpd_unit = %d\n", hd->hpd_unit);
    222  1.7  tsutsui 	printf("sdinit: hd->hpd_ctlr = %d, hd->hpd_slave = %d\n",
    223  1.7  tsutsui 	       hd->hpd_ctlr, hd->hpd_slave);
    224  1.1  tsutsui #endif
    225  1.1  tsutsui 	sc->sc_hd = hd;
    226  1.6  tsutsui 	sc->sc_punit = 0;	/* XXX no LUN support yet */
    227  1.1  tsutsui 	sc->sc_type = sdident(sc, hd);
    228  1.1  tsutsui 	if (sc->sc_type < 0)
    229  1.1  tsutsui 		return(0);
    230  1.1  tsutsui 
    231  1.1  tsutsui 	/*
    232  1.1  tsutsui 	 * Use the default sizes until we've read the label,
    233  1.1  tsutsui 	 * or longer if there isn't one there.
    234  1.1  tsutsui 	 */
    235  1.7  tsutsui 	lp = &sdlabel[hd->hpd_unit];
    236  1.1  tsutsui 
    237  1.1  tsutsui 	if (lp->d_secpercyl == 0) {
    238  1.1  tsutsui 		lp->d_secsize = DEV_BSIZE;
    239  1.1  tsutsui 		lp->d_nsectors = 32;
    240  1.1  tsutsui 		lp->d_ntracks = 20;
    241  1.1  tsutsui 		lp->d_secpercyl = 32*20;
    242  1.1  tsutsui 		lp->d_npartitions = 1;
    243  1.1  tsutsui 		lp->d_partitions[0].p_offset = 0;
    244  1.1  tsutsui 		lp->d_partitions[0].p_size = LABELSECTOR + 1;
    245  1.1  tsutsui 	}
    246  1.1  tsutsui 
    247  1.1  tsutsui 	/*
    248  1.1  tsutsui 	 * read disklabel
    249  1.1  tsutsui 	 */
    250  1.7  tsutsui 	msg = readdisklabel(hd->hpd_ctlr, hd->hpd_slave, lp);
    251  1.1  tsutsui 	if (msg != NULL)
    252  1.7  tsutsui 		printf("sd%d: %s\n", hd->hpd_unit, msg);
    253  1.1  tsutsui 
    254  1.1  tsutsui 	sc->sc_flags = SDF_ALIVE;
    255  1.1  tsutsui 	return(1);
    256  1.1  tsutsui }
    257  1.1  tsutsui 
    258  1.1  tsutsui int
    259  1.1  tsutsui sdopen(struct open_file *f, ...)
    260  1.1  tsutsui {
    261  1.1  tsutsui 	va_list ap;
    262  1.1  tsutsui 	struct sd_devdata *sd;
    263  1.1  tsutsui 	int unit, part;
    264  1.1  tsutsui 
    265  1.1  tsutsui 	va_start(ap, f);
    266  1.1  tsutsui 	unit = va_arg(ap, int);
    267  1.1  tsutsui 	part = va_arg(ap, int);
    268  1.1  tsutsui 	va_end(ap);
    269  1.1  tsutsui 
    270  1.1  tsutsui 	if (unit < 0 || unit >= NSD)
    271  1.1  tsutsui 		return(-1);
    272  1.1  tsutsui 	if (part < 0 || part >= 8)
    273  1.1  tsutsui 		return(-1);
    274  1.1  tsutsui 
    275  1.1  tsutsui 	sd = &sd_devdata[unit];
    276  1.1  tsutsui 	sd->unit = unit;
    277  1.1  tsutsui 	sd->part = part;
    278  1.1  tsutsui 	f->f_devdata = (void *)sd;
    279  1.1  tsutsui 
    280  1.1  tsutsui 	return 0;
    281  1.1  tsutsui }
    282  1.1  tsutsui 
    283  1.1  tsutsui int
    284  1.1  tsutsui sdclose(struct open_file *f)
    285  1.1  tsutsui {
    286  1.1  tsutsui 	struct sd_devdata *sd = f->f_devdata;
    287  1.1  tsutsui 
    288  1.1  tsutsui 	sd->unit = -1;
    289  1.1  tsutsui 	sd->part = -1;
    290  1.1  tsutsui 	f->f_devdata = NULL;
    291  1.1  tsutsui 
    292  1.1  tsutsui 	return 0;
    293  1.1  tsutsui }
    294  1.1  tsutsui 
    295  1.8  tsutsui static struct scsi_generic_cdb cdb_read = {
    296  1.1  tsutsui 	10,
    297  1.1  tsutsui 	{ CMD_READ_EXT,  0, 0, 0, 0, 0, 0, 0, 0, 0 }
    298  1.1  tsutsui };
    299  1.1  tsutsui 
    300  1.8  tsutsui static struct scsi_generic_cdb cdb_write = {
    301  1.1  tsutsui 	6,
    302  1.1  tsutsui 	{ CMD_WRITE_EXT, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
    303  1.1  tsutsui };
    304  1.1  tsutsui 
    305  1.1  tsutsui int
    306  1.1  tsutsui sdstrategy(void *devdata, int func, daddr_t dblk, size_t size, void *v_buf,
    307  1.1  tsutsui     size_t *rsize)
    308  1.1  tsutsui {
    309  1.1  tsutsui 	struct sd_devdata *sd = devdata;
    310  1.1  tsutsui 	struct disklabel *lp;
    311  1.1  tsutsui 	uint8_t *buf = v_buf;
    312  1.1  tsutsui 	int unit = sd->unit;
    313  1.1  tsutsui 	int part = sd->part;
    314  1.1  tsutsui 	struct sd_softc *sc = &sd_softc[unit];
    315  1.8  tsutsui 	struct scsi_generic_cdb *cdb;
    316  1.1  tsutsui 	daddr_t blk;
    317  1.1  tsutsui 	u_int nblk  = size >> sc->sc_bshift;
    318  1.1  tsutsui 	int stat, ctlr, slave;
    319  1.1  tsutsui #ifdef DEBUG
    320  1.1  tsutsui 	int i;
    321  1.1  tsutsui #endif
    322  1.1  tsutsui 
    323  1.1  tsutsui 	if (unit < 0 || unit >= NSD)
    324  1.1  tsutsui 		return(-1);
    325  1.1  tsutsui 
    326  1.7  tsutsui 	ctlr  = sc->sc_hd->hpd_ctlr;
    327  1.7  tsutsui 	slave = sc->sc_hd->hpd_slave;
    328  1.1  tsutsui 
    329  1.1  tsutsui 	lp = &sdlabel[unit];
    330  1.1  tsutsui 	blk = dblk + (lp->d_partitions[part].p_offset >> sc->sc_bshift);
    331  1.1  tsutsui 
    332  1.1  tsutsui 	if (func == F_READ)
    333  1.1  tsutsui 		cdb = &cdb_read;
    334  1.1  tsutsui 	else
    335  1.1  tsutsui 		cdb = &cdb_write;
    336  1.1  tsutsui 
    337  1.1  tsutsui 	cdb->cdb[2] = (blk & 0xff000000) >> 24;
    338  1.1  tsutsui 	cdb->cdb[3] = (blk & 0x00ff0000) >> 16;
    339  1.1  tsutsui 	cdb->cdb[4] = (blk & 0x0000ff00) >>  8;
    340  1.1  tsutsui 	cdb->cdb[5] = (blk & 0x000000ff);
    341  1.1  tsutsui 
    342  1.1  tsutsui 	cdb->cdb[7] = ((nblk >> DEV_BSHIFT) & 0xff00) >> 8;
    343  1.1  tsutsui 	cdb->cdb[8] = ((nblk >> DEV_BSHIFT) & 0x00ff);
    344  1.1  tsutsui 
    345  1.1  tsutsui #ifdef DEBUG
    346  1.1  tsutsui 	printf("sdstrategy: unit = %d\n", unit);
    347  1.1  tsutsui 	printf("sdstrategy: blk = %lu (0x%lx), nblk = %u (0x%x)\n", (u_long)blk, (long)blk, nblk, nblk);
    348  1.1  tsutsui 	for (i = 0; i < 10; i++)
    349  1.1  tsutsui 		printf("sdstrategy: cdb[%d] = 0x%x\n", i, cdb->cdb[i]);
    350  1.1  tsutsui 	printf("sdstrategy: ctlr = %d, slave = %d\n", ctlr, slave);
    351  1.1  tsutsui #endif
    352  1.1  tsutsui 	stat = scsi_immed_command(ctlr, slave, sc->sc_punit, cdb, buf, size);
    353  1.1  tsutsui 	if (rsize)
    354  1.1  tsutsui 		*rsize = size;
    355  1.1  tsutsui 
    356  1.1  tsutsui 	return 0;
    357  1.1  tsutsui }
    358