Home | History | Annotate | Line # | Download | only in common
sd.c revision 1.6.4.2
      1  1.6.4.2     yamt /*	$NetBSD: sd.c,v 1.6.4.2 2007/09/03 14:25:32 yamt Exp $	*/
      2      1.1  thorpej 
      3      1.1  thorpej /*
      4      1.1  thorpej  * Copyright (c) 1990, 1993
      5      1.1  thorpej  *	The Regents of the University of California.  All rights reserved.
      6      1.3      agc  *
      7      1.3      agc  * This code is derived from software contributed to Berkeley by
      8      1.3      agc  * Van Jacobson of Lawrence Berkeley Laboratory and the Systems
      9      1.3      agc  * Programming Group of the University of Utah Computer Science Department.
     10      1.3      agc  *
     11      1.3      agc  * Redistribution and use in source and binary forms, with or without
     12      1.3      agc  * modification, are permitted provided that the following conditions
     13      1.3      agc  * are met:
     14      1.3      agc  * 1. Redistributions of source code must retain the above copyright
     15      1.3      agc  *    notice, this list of conditions and the following disclaimer.
     16      1.3      agc  * 2. Redistributions in binary form must reproduce the above copyright
     17      1.3      agc  *    notice, this list of conditions and the following disclaimer in the
     18      1.3      agc  *    documentation and/or other materials provided with the distribution.
     19      1.3      agc  * 3. Neither the name of the University nor the names of its contributors
     20      1.3      agc  *    may be used to endorse or promote products derived from this software
     21      1.3      agc  *    without specific prior written permission.
     22      1.3      agc  *
     23      1.3      agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24      1.3      agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25      1.3      agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26      1.3      agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27      1.3      agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28      1.3      agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29      1.3      agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30      1.3      agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31      1.3      agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32      1.3      agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33      1.3      agc  * SUCH DAMAGE.
     34      1.3      agc  *
     35      1.3      agc  * from: Utah $Hdr: sd.c 1.9 92/12/21$
     36      1.3      agc  *
     37      1.3      agc  *	@(#)sd.c	8.1 (Berkeley) 6/10/93
     38      1.3      agc  */
     39      1.3      agc /*
     40      1.3      agc  * Copyright (c) 1988 University of Utah.
     41      1.1  thorpej  *
     42      1.1  thorpej  * This code is derived from software contributed to Berkeley by
     43      1.1  thorpej  * Van Jacobson of Lawrence Berkeley Laboratory and the Systems
     44      1.1  thorpej  * Programming Group of the University of Utah Computer Science Department.
     45      1.1  thorpej  *
     46      1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     47      1.1  thorpej  * modification, are permitted provided that the following conditions
     48      1.1  thorpej  * are met:
     49      1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     50      1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     51      1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     52      1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     53      1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     54      1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     55      1.1  thorpej  *    must display the following acknowledgement:
     56      1.1  thorpej  *	This product includes software developed by the University of
     57      1.1  thorpej  *	California, Berkeley and its contributors.
     58      1.1  thorpej  * 4. Neither the name of the University nor the names of its contributors
     59      1.1  thorpej  *    may be used to endorse or promote products derived from this software
     60      1.1  thorpej  *    without specific prior written permission.
     61      1.1  thorpej  *
     62      1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     63      1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64      1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65      1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     66      1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67      1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68      1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69      1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70      1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71      1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72      1.1  thorpej  * SUCH DAMAGE.
     73      1.1  thorpej  *
     74      1.1  thorpej  * from: Utah $Hdr: sd.c 1.9 92/12/21$
     75      1.1  thorpej  *
     76      1.1  thorpej  *	@(#)sd.c	8.1 (Berkeley) 6/10/93
     77      1.1  thorpej  */
     78      1.1  thorpej 
     79      1.1  thorpej /*
     80      1.1  thorpej  * SCSI CCS disk driver
     81      1.1  thorpej  */
     82      1.1  thorpej 
     83      1.1  thorpej #include <sys/param.h>
     84      1.1  thorpej #include <sys/disklabel.h>
     85      1.1  thorpej 
     86      1.4  tsutsui #include <machine/stdarg.h>
     87      1.4  tsutsui 
     88      1.1  thorpej #include <lib/libsa/stand.h>
     89      1.1  thorpej 
     90      1.1  thorpej #include <hp300/stand/common/samachdep.h>
     91      1.4  tsutsui #include <hp300/stand/common/conf.h>
     92      1.1  thorpej 
     93      1.1  thorpej #define _IOCTL_
     94      1.2  tsutsui #include <hp300/stand/common/scsireg.h>
     95      1.4  tsutsui #include <hp300/stand/common/scsivar.h>
     96      1.1  thorpej 
     97      1.1  thorpej struct	sdminilabel {
     98      1.1  thorpej 	u_short	npart;
     99      1.1  thorpej 	u_long	offset[MAXPARTITIONS];
    100      1.1  thorpej };
    101      1.1  thorpej 
    102      1.1  thorpej struct	sd_softc {
    103      1.1  thorpej 	int	sc_ctlr;
    104      1.1  thorpej 	int	sc_unit;
    105      1.1  thorpej 	int	sc_part;
    106      1.1  thorpej 	char	sc_retry;
    107      1.1  thorpej 	char	sc_alive;
    108      1.1  thorpej 	short	sc_blkshift;
    109      1.1  thorpej 	struct	sdminilabel sc_pinfo;
    110      1.4  tsutsui };
    111      1.1  thorpej 
    112      1.1  thorpej #define	SDRETRY		2
    113      1.1  thorpej 
    114      1.4  tsutsui static int sdinit(int ,int);
    115      1.4  tsutsui static int sdgetinfo(struct sd_softc *);
    116      1.4  tsutsui 
    117      1.4  tsutsui struct	disklabel sdlabel;
    118      1.4  tsutsui struct sd_softc sd_softc[NSCSI][NSD];
    119      1.4  tsutsui 
    120      1.4  tsutsui static int
    121      1.6  tsutsui sdinit(int ctlr, int unit)
    122      1.1  thorpej {
    123      1.4  tsutsui 	struct sd_softc *ss = &sd_softc[ctlr][unit];
    124      1.1  thorpej 	u_char stat;
    125      1.1  thorpej 	int capbuf[2];
    126      1.1  thorpej 
    127      1.1  thorpej 	stat = scsi_test_unit_rdy(ctlr, unit);
    128      1.1  thorpej 	if (stat) {
    129      1.1  thorpej 		/* drive may be doing RTZ - wait a bit */
    130      1.1  thorpej 		if (stat == STS_CHECKCOND) {
    131      1.1  thorpej 			DELAY(1000000);
    132      1.1  thorpej 			stat = scsi_test_unit_rdy(ctlr, unit);
    133      1.1  thorpej 		}
    134      1.1  thorpej 		if (stat) {
    135      1.1  thorpej 			printf("sd(%d,%d,0,0): init failed (stat=%x)\n",
    136      1.1  thorpej 			       ctlr, unit, stat);
    137      1.4  tsutsui 			return 0;
    138      1.1  thorpej 		}
    139      1.1  thorpej 	}
    140      1.1  thorpej 	/*
    141      1.1  thorpej 	 * try to get the drive block size.
    142      1.1  thorpej 	 */
    143      1.1  thorpej 	capbuf[0] = 0;
    144      1.1  thorpej 	capbuf[1] = 0;
    145      1.1  thorpej 	stat = scsi_read_capacity(ctlr, unit,
    146      1.1  thorpej 				  (u_char *)capbuf, sizeof(capbuf));
    147      1.1  thorpej 	if (stat == 0) {
    148      1.1  thorpej 		if (capbuf[1] > DEV_BSIZE)
    149      1.1  thorpej 			for (; capbuf[1] > DEV_BSIZE; capbuf[1] >>= 1)
    150      1.1  thorpej 				++ss->sc_blkshift;
    151      1.1  thorpej 	}
    152      1.1  thorpej 	ss->sc_alive = 1;
    153      1.4  tsutsui 	return 1;
    154      1.1  thorpej }
    155      1.1  thorpej 
    156      1.1  thorpej char io_buf[MAXBSIZE];
    157      1.1  thorpej 
    158      1.4  tsutsui static int
    159      1.6  tsutsui sdgetinfo(struct sd_softc *ss)
    160      1.1  thorpej {
    161      1.4  tsutsui 	struct sdminilabel *pi = &ss->sc_pinfo;
    162      1.4  tsutsui 	struct disklabel *lp = &sdlabel;
    163      1.4  tsutsui 	char *msg;
    164      1.4  tsutsui 	int err, savepart;
    165      1.1  thorpej 	size_t i;
    166      1.1  thorpej 
    167  1.6.4.2     yamt 	memset((void *)lp, 0, sizeof *lp);
    168      1.1  thorpej 	lp->d_secsize = (DEV_BSIZE << ss->sc_blkshift);
    169      1.1  thorpej 
    170      1.1  thorpej 	/* Disklabel is always from RAW_PART. */
    171      1.1  thorpej 	savepart = ss->sc_part;
    172      1.1  thorpej 	ss->sc_part = RAW_PART;
    173      1.1  thorpej 	err = sdstrategy(ss, F_READ, LABELSECTOR,
    174      1.1  thorpej 	    lp->d_secsize ? lp->d_secsize : DEV_BSIZE, io_buf, &i);
    175      1.1  thorpej 	ss->sc_part = savepart;
    176      1.1  thorpej 
    177      1.1  thorpej 	if (err) {
    178      1.1  thorpej 		printf("sdgetinfo: sdstrategy error %d\n", err);
    179      1.4  tsutsui 		return 0;
    180      1.1  thorpej 	}
    181      1.1  thorpej 
    182      1.1  thorpej 	msg = getdisklabel(io_buf, lp);
    183      1.1  thorpej 	if (msg) {
    184      1.5  thorpej 		printf("sd(%d,%d,%d): WARNING: %s\n",
    185      1.1  thorpej 		       ss->sc_ctlr, ss->sc_unit, ss->sc_part, msg);
    186      1.1  thorpej 		pi->npart = 3;
    187      1.1  thorpej 		pi->offset[0] = pi->offset[1] = -1;
    188      1.1  thorpej 		pi->offset[2] = 0;
    189      1.1  thorpej 	} else {
    190      1.1  thorpej 		pi->npart = lp->d_npartitions;
    191      1.1  thorpej 		for (i = 0; i < pi->npart; i++)
    192      1.1  thorpej 			pi->offset[i] = lp->d_partitions[i].p_size == 0 ?
    193      1.1  thorpej 				-1 : lp->d_partitions[i].p_offset;
    194      1.1  thorpej 	}
    195      1.4  tsutsui 	return 1;
    196      1.1  thorpej }
    197      1.1  thorpej 
    198      1.4  tsutsui int
    199      1.4  tsutsui sdopen(struct open_file *f, ...)
    200      1.4  tsutsui {
    201      1.4  tsutsui 	va_list ap;
    202      1.1  thorpej 	int ctlr, unit, part;
    203      1.4  tsutsui 	struct sd_softc *ss;
    204      1.4  tsutsui 
    205      1.4  tsutsui 	va_start(ap, f);
    206      1.4  tsutsui 	ctlr = va_arg(ap, int);
    207      1.4  tsutsui 	unit = va_arg(ap, int);
    208      1.4  tsutsui 	part = va_arg(ap, int);
    209      1.4  tsutsui 	va_end(ap);
    210      1.1  thorpej 
    211      1.1  thorpej #ifdef SD_DEBUG
    212      1.1  thorpej 	if (debug)
    213      1.1  thorpej 	printf("sdopen: ctlr=%d unit=%d part=%d\n",
    214      1.1  thorpej 	    ctlr, unit, part);
    215      1.1  thorpej #endif
    216      1.1  thorpej 
    217      1.1  thorpej 	if (ctlr >= NSCSI || scsialive(ctlr) == 0)
    218      1.4  tsutsui 		return EADAPT;
    219      1.1  thorpej 	if (unit >= NSD)
    220      1.4  tsutsui 		return ECTLR;
    221      1.1  thorpej 	ss = &sd_softc[ctlr][unit];
    222      1.1  thorpej 	ss->sc_part = part;
    223      1.1  thorpej 	ss->sc_unit = unit;
    224      1.1  thorpej 	ss->sc_ctlr = ctlr;
    225      1.1  thorpej 	if (ss->sc_alive == 0) {
    226      1.1  thorpej 		if (sdinit(ctlr, unit) == 0)
    227      1.4  tsutsui 			return ENXIO;
    228      1.1  thorpej 		if (sdgetinfo(ss) == 0)
    229      1.4  tsutsui 			return ERDLAB;
    230      1.1  thorpej 	}
    231      1.1  thorpej 	if (part != RAW_PART &&     /* always allow RAW_PART to be opened */
    232      1.1  thorpej 	    (part >= ss->sc_pinfo.npart || ss->sc_pinfo.offset[part] == -1))
    233      1.4  tsutsui 		return EPART;
    234      1.1  thorpej 	f->f_devdata = (void *)ss;
    235      1.4  tsutsui 	return 0;
    236      1.1  thorpej }
    237      1.1  thorpej 
    238      1.4  tsutsui int
    239      1.6  tsutsui sdclose(struct open_file *f)
    240      1.1  thorpej {
    241      1.1  thorpej 	struct sd_softc *ss = f->f_devdata;
    242      1.1  thorpej 
    243      1.1  thorpej 	/*
    244      1.1  thorpej 	 * Mark the disk `not alive' so that the disklabel
    245      1.1  thorpej 	 * will be re-loaded at next open.
    246      1.1  thorpej 	 */
    247      1.4  tsutsui 	memset(ss, 0, sizeof(sd_softc));
    248      1.1  thorpej 	f->f_devdata = NULL;
    249      1.1  thorpej 
    250      1.4  tsutsui 	return 0;
    251      1.1  thorpej }
    252      1.1  thorpej 
    253      1.4  tsutsui int
    254      1.6  tsutsui sdstrategy(void *devdata, int func, daddr_t dblk, size_t size, void *v_buf,
    255      1.6  tsutsui     size_t *rsize)
    256      1.1  thorpej {
    257      1.4  tsutsui 	struct sd_softc *ss = devdata;
    258  1.6.4.1     yamt 	uint8_t *buf = v_buf;
    259      1.4  tsutsui 	int ctlr = ss->sc_ctlr;
    260      1.4  tsutsui 	int unit = ss->sc_unit;
    261      1.1  thorpej 	u_int nblk = size >> ss->sc_blkshift;
    262      1.1  thorpej 	daddr_t blk;
    263  1.6.4.1     yamt 	int stat;
    264      1.1  thorpej 
    265      1.1  thorpej 	if (size == 0)
    266      1.4  tsutsui 		return 0;
    267      1.1  thorpej 
    268      1.1  thorpej 	/*
    269      1.1  thorpej 	 * Don't do partition translation on the `raw partition'.
    270      1.1  thorpej 	 */
    271      1.1  thorpej 	blk = (dblk + ((ss->sc_part == RAW_PART) ? 0 :
    272      1.1  thorpej 	    ss->sc_pinfo.offset[ss->sc_part])) >> ss->sc_blkshift;
    273      1.1  thorpej 
    274      1.1  thorpej 	ss->sc_retry = 0;
    275      1.1  thorpej 
    276      1.1  thorpej #ifdef SD_DEBUG
    277      1.1  thorpej 	if (debug)
    278      1.1  thorpej 	printf("sdstrategy(%d,%d): size=%d blk=%d nblk=%d\n",
    279      1.1  thorpej 	    ctlr, unit, size, blk, nblk);
    280      1.1  thorpej #endif
    281      1.1  thorpej 
    282      1.1  thorpej retry:
    283      1.1  thorpej 	if (func == F_READ)
    284      1.1  thorpej 		stat = scsi_tt_read(ctlr, unit, buf, size, blk, nblk);
    285      1.1  thorpej 	else
    286      1.1  thorpej 		stat = scsi_tt_write(ctlr, unit, buf, size, blk, nblk);
    287      1.1  thorpej 	if (stat) {
    288      1.1  thorpej 		printf("sd(%d,%d,%d): block=%x, error=0x%x\n",
    289      1.1  thorpej 		       ctlr, unit, ss->sc_part, blk, stat);
    290      1.1  thorpej 		if (++ss->sc_retry > SDRETRY)
    291      1.4  tsutsui 			return EIO;
    292      1.1  thorpej 		goto retry;
    293      1.1  thorpej 	}
    294      1.1  thorpej 	*rsize = size;
    295      1.1  thorpej 
    296      1.4  tsutsui 	return 0;
    297      1.1  thorpej }
    298