1997-05-06 01:09:13 +04:00
|
|
|
/* $NetBSD: sd.c,v 1.32 1997/05/05 21:09:13 thorpej Exp $ */
|
1994-10-26 10:22:45 +03:00
|
|
|
|
1993-05-13 17:56:20 +04:00
|
|
|
/*
|
1997-01-30 12:14:10 +03:00
|
|
|
* Copyright (c) 1996, 1997 Jason R. Thorpe. All rights reserved.
|
1994-05-23 09:58:16 +04:00
|
|
|
* Copyright (c) 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-05-13 17:56:20 +04:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Van Jacobson of Lawrence Berkeley Laboratory.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1994-10-26 10:22:45 +03:00
|
|
|
* @(#)sd.c 8.5 (Berkeley) 5/19/94
|
1993-05-13 17:56:20 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SCSI CCS (Command Command Set) disk driver.
|
|
|
|
*/
|
|
|
|
|
1994-02-10 16:59:25 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/buf.h>
|
1997-03-31 11:32:14 +04:00
|
|
|
#include <sys/device.h>
|
1996-01-08 01:01:38 +03:00
|
|
|
#include <sys/disk.h>
|
1997-03-31 11:32:14 +04:00
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
1994-02-10 16:59:25 +03:00
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/proc.h>
|
1997-03-31 11:32:14 +04:00
|
|
|
#include <sys/stat.h>
|
1993-05-13 17:56:20 +04:00
|
|
|
|
1994-02-10 16:59:25 +03:00
|
|
|
#include <hp300/dev/scsireg.h>
|
1997-01-30 12:14:10 +03:00
|
|
|
#include <hp300/dev/scsivar.h>
|
1994-05-23 09:58:16 +04:00
|
|
|
#include <hp300/dev/sdvar.h>
|
1994-02-10 16:59:25 +03:00
|
|
|
|
1994-05-23 09:58:16 +04:00
|
|
|
#include <vm/vm_param.h>
|
|
|
|
#include <vm/lock.h>
|
|
|
|
#include <vm/vm_prot.h>
|
|
|
|
#include <vm/pmap.h>
|
1993-05-13 17:56:20 +04:00
|
|
|
|
1997-05-06 01:09:13 +04:00
|
|
|
#include "opt_useleds.h"
|
|
|
|
|
|
|
|
#ifdef USELEDS
|
|
|
|
#include <hp300/hp300/leds.h>
|
|
|
|
#endif
|
|
|
|
|
1997-03-31 11:32:14 +04:00
|
|
|
/*
|
1993-05-13 17:56:20 +04:00
|
|
|
extern void disksort();
|
|
|
|
extern void biodone();
|
|
|
|
extern int physio();
|
|
|
|
extern void TBIS();
|
1997-03-31 11:32:14 +04:00
|
|
|
*/
|
1993-05-13 17:56:20 +04:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
int sdmatch __P((struct device *, struct cfdata *, void *));
|
|
|
|
void sdattach __P((struct device *, struct device *, void *));
|
1993-05-13 17:56:20 +04:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
struct cfattach sd_ca = {
|
|
|
|
sizeof(struct sd_softc), sdmatch, sdattach
|
1993-05-13 17:56:20 +04:00
|
|
|
};
|
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
struct cfdriver sd_cd = {
|
|
|
|
NULL, "sd", DV_DISK
|
|
|
|
};
|
|
|
|
|
1993-05-13 17:56:20 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
int sddebug = 1;
|
|
|
|
#define SDB_ERROR 0x01
|
|
|
|
#define SDB_PARTIAL 0x02
|
1994-05-23 09:58:16 +04:00
|
|
|
#define SDB_CAPACITY 0x04
|
1993-05-13 17:56:20 +04:00
|
|
|
#endif
|
|
|
|
|
1997-03-31 11:32:14 +04:00
|
|
|
static struct scsi_fmt_cdb sd_read_cmd = { 10, { CMD_READ_EXT } };
|
|
|
|
static struct scsi_fmt_cdb sd_write_cmd = { 10, { CMD_WRITE_EXT } };
|
1993-05-13 17:56:20 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Table of scsi commands users are allowed to access via "format"
|
|
|
|
* mode. 0 means not legal. 1 means "immediate" (doesn't need dma).
|
|
|
|
* -1 means needs dma and/or wait for intr.
|
|
|
|
*/
|
|
|
|
static char legal_cmds[256] = {
|
|
|
|
/***** 0 1 2 3 4 5 6 7 8 9 A B C D E F */
|
|
|
|
/*00*/ 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
/*10*/ 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
|
|
|
|
/*20*/ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
/*30*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
/*40*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
/*50*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
/*60*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
/*70*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
/*80*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
/*90*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
/*a0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
/*b0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
/*c0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
/*d0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
/*e0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
/*f0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
};
|
|
|
|
|
1997-03-31 11:32:14 +04:00
|
|
|
/* bdev_decl(sd); */
|
|
|
|
/* cdev_decl(sd); */
|
|
|
|
/* XXX we should use macros to do these... */
|
|
|
|
int sdopen __P((dev_t, int, int, struct proc *));
|
|
|
|
int sdclose __P((dev_t, int, int, struct proc *));
|
|
|
|
|
|
|
|
int sdioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
|
|
|
|
int sdread __P((dev_t, struct uio *, int));
|
|
|
|
void sdreset __P((struct sd_softc *));
|
|
|
|
int sdwrite __P((dev_t, struct uio *, int));
|
|
|
|
|
|
|
|
void sdstrategy __P((struct buf *));
|
|
|
|
int sddump __P((dev_t, daddr_t, caddr_t, size_t));
|
|
|
|
int sdsize __P((dev_t));
|
|
|
|
|
|
|
|
static void sdgetgeom __P((struct sd_softc *));
|
|
|
|
static void sdlblkstrat __P((struct buf *, int));
|
|
|
|
static int sderror __P((struct sd_softc *, int));
|
|
|
|
static void sdfinish __P((struct sd_softc *, struct buf *));
|
|
|
|
|
1995-10-15 13:03:18 +03:00
|
|
|
/*
|
|
|
|
* Perform a mode-sense on page 0x04 (rigid geometry).
|
|
|
|
*/
|
|
|
|
static void
|
1997-01-30 12:14:10 +03:00
|
|
|
sdgetgeom(sc)
|
1995-10-15 13:03:18 +03:00
|
|
|
struct sd_softc *sc;
|
|
|
|
{
|
|
|
|
struct scsi_mode_sense_geom {
|
|
|
|
struct scsi_modesense_hdr header;
|
|
|
|
struct scsi_geometry geom;
|
|
|
|
} sensebuf;
|
|
|
|
struct scsi_fmt_cdb modesense_geom = {
|
|
|
|
6,
|
1997-03-31 11:32:14 +04:00
|
|
|
{ CMD_MODE_SENSE, 0, 0x04, 0, sizeof(sensebuf), 0 }
|
1995-10-15 13:03:18 +03:00
|
|
|
};
|
|
|
|
int ctlr, slave, unit;
|
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
ctlr = sc->sc_dev.dv_parent->dv_unit;
|
|
|
|
slave = sc->sc_target;
|
|
|
|
unit = sc->sc_lun;
|
1995-10-15 13:03:18 +03:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
scsi_delay(-1); /* XXX */
|
1995-10-15 13:03:18 +03:00
|
|
|
(void)scsi_immed_command(ctlr, slave, unit, &modesense_geom,
|
|
|
|
(u_char *)&sensebuf, sizeof(sensebuf), B_READ);
|
1997-01-30 12:14:10 +03:00
|
|
|
scsi_delay(0); /* XXX */
|
1995-10-15 13:03:18 +03:00
|
|
|
|
|
|
|
sc->sc_heads = sensebuf.geom.heads;
|
|
|
|
sc->sc_cyls = (sensebuf.geom.cyl_ub << 16) |
|
|
|
|
(sensebuf.geom.cyl_mb << 8) | sensebuf.geom.cyl_lb;
|
|
|
|
}
|
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
int
|
|
|
|
sdmatch(parent, match, aux)
|
|
|
|
struct device *parent;
|
|
|
|
struct cfdata *match;
|
|
|
|
void *aux;
|
1993-05-13 17:56:20 +04:00
|
|
|
{
|
1997-01-30 12:14:10 +03:00
|
|
|
struct oscsi_attach_args *osa = aux;
|
1993-05-13 17:56:20 +04:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
switch (osa->osa_inqbuf->type) {
|
1993-05-13 17:56:20 +04:00
|
|
|
case 0: /* disk */
|
|
|
|
case 4: /* WORM */
|
|
|
|
case 5: /* CD-ROM */
|
|
|
|
case 7: /* Magneto-optical */
|
|
|
|
break;
|
|
|
|
default: /* not a disk */
|
1997-01-30 12:14:10 +03:00
|
|
|
return 0;
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
return (1);
|
|
|
|
}
|
1995-10-15 13:03:18 +03:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
void
|
|
|
|
sdattach(parent, self, aux)
|
|
|
|
struct device *parent, *self;
|
|
|
|
void *aux;
|
|
|
|
{
|
|
|
|
struct sd_softc *sc = (struct sd_softc *)self;
|
|
|
|
struct oscsi_attach_args *osa = aux;
|
1995-10-15 13:03:18 +03:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
/*
|
|
|
|
* XXX formerly 0 meant unused but now pid 0 can legitimately
|
|
|
|
* use this interface (sdgetcapacity).
|
|
|
|
*/
|
|
|
|
sc->sc_format_pid = -1;
|
|
|
|
sc->sc_flags = 0;
|
1995-10-15 13:03:18 +03:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
sc->sc_target = osa->osa_target;
|
|
|
|
sc->sc_lun = osa->osa_lun;
|
|
|
|
sc->sc_type = osa->osa_inqbuf->type;
|
1995-10-15 13:03:18 +03:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
if (osa->osa_inqbuf->qual & 0x80)
|
|
|
|
sc->sc_flags |= SDF_RMEDIA;
|
1993-05-13 17:56:20 +04:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
printf("\n");
|
1993-05-13 17:56:20 +04:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
/* Initialize the SCSI queue entry. */
|
|
|
|
sc->sc_sq.sq_softc = sc;
|
|
|
|
sc->sc_sq.sq_target = sc->sc_target;
|
|
|
|
sc->sc_sq.sq_lun = sc->sc_lun;
|
|
|
|
sc->sc_sq.sq_start = sdstart;
|
|
|
|
sc->sc_sq.sq_go = sdgo;
|
|
|
|
sc->sc_sq.sq_intr = sdintr;
|
1996-01-08 01:01:38 +03:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
if (sdgetcapacity(sc, NODEV) < 0) {
|
|
|
|
printf("%s: getcapacity failed!\n", sc->sc_dev.dv_xname);
|
|
|
|
return;
|
|
|
|
}
|
1996-01-08 01:01:38 +03:00
|
|
|
|
1994-05-23 09:58:16 +04:00
|
|
|
/*
|
1997-01-30 12:14:10 +03:00
|
|
|
* Print out some additional information.
|
1994-05-23 09:58:16 +04:00
|
|
|
*/
|
1997-01-30 12:14:10 +03:00
|
|
|
printf("%s: ", sc->sc_dev.dv_xname);
|
|
|
|
switch (sc->sc_type) {
|
|
|
|
case 4:
|
|
|
|
printf("WORM, ");
|
|
|
|
break;
|
1995-12-02 21:21:49 +03:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
case 5:
|
|
|
|
printf("CD-ROM, ");
|
|
|
|
break;
|
1995-12-02 21:21:49 +03:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
case 7:
|
|
|
|
printf("Magneto-optical, ");
|
|
|
|
break;
|
1995-12-02 21:21:49 +03:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
default:
|
|
|
|
printf("%d cylinders, %d heads, ",
|
|
|
|
sc->sc_cyls, sc->sc_heads);
|
|
|
|
}
|
|
|
|
if (sc->sc_blks)
|
|
|
|
printf("%d blocks, %d bytes/block\n",
|
|
|
|
sc->sc_blks >> sc->sc_bshift, sc->sc_blksize);
|
|
|
|
else
|
|
|
|
printf("drive empty\n");
|
1995-12-02 21:21:49 +03:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
/* Initialize the disk structure. */
|
|
|
|
sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname;
|
1993-05-13 17:56:20 +04:00
|
|
|
|
1996-01-08 01:01:38 +03:00
|
|
|
/* Attach the disk. */
|
|
|
|
disk_attach(&sc->sc_dkdev);
|
|
|
|
|
1994-05-23 09:58:16 +04:00
|
|
|
sc->sc_flags |= SDF_ALIVE;
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-01-30 12:14:10 +03:00
|
|
|
sdreset(sc)
|
1997-03-31 11:32:14 +04:00
|
|
|
struct sd_softc *sc;
|
1993-05-13 17:56:20 +04:00
|
|
|
{
|
1997-01-30 12:14:10 +03:00
|
|
|
sc->sc_stats.sdresets++;
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
|
|
|
|
1994-05-23 09:58:16 +04:00
|
|
|
/*
|
|
|
|
* Determine capacity of a drive.
|
|
|
|
* Returns -1 on a failure, 0 on success, 1 on a failure that is probably
|
|
|
|
* due to missing media.
|
|
|
|
*/
|
|
|
|
int
|
1997-01-30 12:14:10 +03:00
|
|
|
sdgetcapacity(sc, dev)
|
1994-05-23 09:58:16 +04:00
|
|
|
struct sd_softc *sc;
|
|
|
|
dev_t dev;
|
|
|
|
{
|
|
|
|
static struct scsi_fmt_cdb cap = {
|
|
|
|
10,
|
1997-03-31 11:32:14 +04:00
|
|
|
{ CMD_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
|
1994-05-23 09:58:16 +04:00
|
|
|
};
|
|
|
|
u_char *capbuf;
|
|
|
|
int i, capbufsize;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cannot use stack space for this buffer since stack KVA may not
|
|
|
|
* be valid (i.e. in context of this process) when the operation
|
|
|
|
* actually starts.
|
|
|
|
*/
|
|
|
|
capbufsize = 8;
|
|
|
|
capbuf = malloc(capbufsize, M_DEVBUF, M_WAITOK);
|
|
|
|
|
|
|
|
if (dev == NODEV) {
|
1997-01-30 12:14:10 +03:00
|
|
|
scsi_delay(-1); /* XXX */
|
|
|
|
i = scsi_immed_command(sc->sc_dev.dv_parent->dv_unit,
|
|
|
|
sc->sc_target, sc->sc_lun, &cap, capbuf,
|
|
|
|
capbufsize, B_READ);
|
|
|
|
scsi_delay(0); /* XXX */
|
1994-05-23 09:58:16 +04:00
|
|
|
} else {
|
|
|
|
struct buf *bp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX this is horrible
|
|
|
|
*/
|
|
|
|
if (sc->sc_format_pid >= 0)
|
|
|
|
panic("sdgetcapacity");
|
|
|
|
bp = malloc(sizeof *bp, M_DEVBUF, M_WAITOK);
|
|
|
|
sc->sc_format_pid = curproc->p_pid;
|
1997-01-30 12:14:10 +03:00
|
|
|
bcopy(&cap, &sc->sc_cmdstore, sizeof cap);
|
1994-05-23 09:58:16 +04:00
|
|
|
bp->b_dev = dev;
|
|
|
|
bp->b_flags = B_READ | B_BUSY;
|
|
|
|
bp->b_un.b_addr = (caddr_t)capbuf;
|
|
|
|
bp->b_bcount = capbufsize;
|
|
|
|
sdstrategy(bp);
|
1997-01-30 12:14:10 +03:00
|
|
|
i = biowait(bp) ? sc->sc_sensestore.status : 0;
|
1994-05-23 09:58:16 +04:00
|
|
|
free(bp, M_DEVBUF);
|
|
|
|
sc->sc_format_pid = -1;
|
|
|
|
}
|
|
|
|
if (i) {
|
|
|
|
if (i != STS_CHECKCOND || (sc->sc_flags & SDF_RMEDIA) == 0) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (sddebug & SDB_CAPACITY)
|
1996-10-13 07:14:05 +04:00
|
|
|
printf("%s: read_capacity returns %d\n",
|
1997-01-30 12:14:10 +03:00
|
|
|
sc->sc_dev.dv_xname, i);
|
1994-05-23 09:58:16 +04:00
|
|
|
#endif
|
|
|
|
free(capbuf, M_DEVBUF);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* XXX assume unformatted or non-existant media
|
|
|
|
*/
|
|
|
|
sc->sc_blks = 0;
|
|
|
|
sc->sc_blksize = DEV_BSIZE;
|
|
|
|
sc->sc_bshift = 0;
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (sddebug & SDB_CAPACITY)
|
1996-10-13 07:14:05 +04:00
|
|
|
printf("%s: removable media not present\n",
|
1997-01-30 12:14:10 +03:00
|
|
|
sc->sc_dev.dv_xname);
|
1994-05-23 09:58:16 +04:00
|
|
|
#endif
|
|
|
|
free(capbuf, M_DEVBUF);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
sc->sc_blks = *(u_int *)&capbuf[0];
|
|
|
|
sc->sc_blksize = *(int *)&capbuf[4];
|
|
|
|
free(capbuf, M_DEVBUF);
|
|
|
|
sc->sc_bshift = 0;
|
|
|
|
|
|
|
|
/* return value of read capacity is last valid block number */
|
|
|
|
sc->sc_blks++;
|
|
|
|
|
|
|
|
if (sc->sc_blksize != DEV_BSIZE) {
|
|
|
|
if (sc->sc_blksize < DEV_BSIZE) {
|
1996-10-13 07:14:05 +04:00
|
|
|
printf("%s: need at least %d byte blocks - %s\n",
|
1997-01-30 12:14:10 +03:00
|
|
|
sc->sc_dev.dv_xname, DEV_BSIZE, "drive ignored");
|
1994-05-23 09:58:16 +04:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
for (i = sc->sc_blksize; i > DEV_BSIZE; i >>= 1)
|
|
|
|
++sc->sc_bshift;
|
|
|
|
sc->sc_blks <<= sc->sc_bshift;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (sddebug & SDB_CAPACITY)
|
1997-01-30 12:14:10 +03:00
|
|
|
printf("%s: blks=%d, blksize=%d, bshift=%d\n",
|
|
|
|
sc->sc_dev.dv_xname, sc->sc_blks, sc->sc_blksize,
|
|
|
|
sc->sc_bshift);
|
1994-05-23 09:58:16 +04:00
|
|
|
#endif
|
1997-01-30 12:14:10 +03:00
|
|
|
sdgetgeom(sc);
|
1994-05-23 09:58:16 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read or constuct a disklabel
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sdgetinfo(dev)
|
|
|
|
dev_t dev;
|
|
|
|
{
|
|
|
|
int unit = sdunit(dev);
|
1997-01-30 12:14:10 +03:00
|
|
|
struct sd_softc *sc = sd_cd.cd_devs[unit];
|
1997-03-31 11:32:14 +04:00
|
|
|
struct disklabel *lp = sc->sc_dkdev.dk_label;
|
|
|
|
struct partition *pi;
|
|
|
|
char *msg;
|
1994-05-23 09:58:16 +04:00
|
|
|
#ifdef COMPAT_NOLABEL
|
|
|
|
int usedefault = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For CD-ROM just define a single partition
|
|
|
|
*/
|
|
|
|
if (sc->sc_type == 5)
|
|
|
|
usedefault = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bzero((caddr_t)lp, sizeof *lp);
|
|
|
|
msg = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If removable media or the size unavailable at boot time
|
|
|
|
* (i.e. unformatted hard disk), attempt to set the capacity
|
|
|
|
* now.
|
|
|
|
*/
|
|
|
|
if ((sc->sc_flags & SDF_RMEDIA) || sc->sc_blks == 0) {
|
1997-01-30 12:14:10 +03:00
|
|
|
switch (sdgetcapacity(sc, dev)) {
|
1994-05-23 09:58:16 +04:00
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
/*
|
|
|
|
* Hard error, just return (open will fail).
|
|
|
|
*/
|
|
|
|
return (EIO);
|
|
|
|
case 1:
|
|
|
|
/*
|
|
|
|
* XXX return 0 so open can continue just in case
|
|
|
|
* the media is unformatted and we want to format it.
|
|
|
|
* We set the error flag so they cannot do much else.
|
|
|
|
*/
|
|
|
|
sc->sc_flags |= SDF_ERROR;
|
|
|
|
msg = "unformatted/missing media";
|
|
|
|
#ifdef COMPAT_NOLABEL
|
|
|
|
usedefault = 0;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set some default values to use while reading the label
|
|
|
|
* (or to use if there isn't a label) and try reading it.
|
|
|
|
*/
|
|
|
|
if (msg == NULL) {
|
|
|
|
lp->d_type = DTYPE_SCSI;
|
|
|
|
lp->d_secsize = DEV_BSIZE;
|
|
|
|
lp->d_nsectors = 32;
|
|
|
|
lp->d_ntracks = 20;
|
|
|
|
lp->d_ncylinders = 1;
|
|
|
|
lp->d_secpercyl = 32*20;
|
|
|
|
lp->d_npartitions = 3;
|
|
|
|
lp->d_partitions[2].p_offset = 0;
|
|
|
|
/* XXX we can open a device even without SDF_ALIVE */
|
|
|
|
if (sc->sc_blksize == 0)
|
|
|
|
sc->sc_blksize = DEV_BSIZE;
|
|
|
|
/* XXX ensure size is at least one device block */
|
|
|
|
lp->d_partitions[2].p_size =
|
|
|
|
roundup(LABELSECTOR+1, btodb(sc->sc_blksize));
|
1995-12-09 10:31:03 +03:00
|
|
|
msg = readdisklabel(sdlabdev(dev), sdstrategy, lp, NULL);
|
1994-05-23 09:58:16 +04:00
|
|
|
if (msg == NULL)
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
pi = lp->d_partitions;
|
1997-01-30 12:14:10 +03:00
|
|
|
printf("%s: WARNING: %s, ", sc->sc_dev.dv_xname, msg);
|
1994-05-23 09:58:16 +04:00
|
|
|
#ifdef COMPAT_NOLABEL
|
|
|
|
if (usedefault) {
|
1996-10-13 07:14:05 +04:00
|
|
|
printf("using old default partitioning\n");
|
1994-05-23 09:58:16 +04:00
|
|
|
sdmakedisklabel(unit, lp);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
#endif
|
1996-10-13 07:14:05 +04:00
|
|
|
printf("defining `c' partition as entire disk\n");
|
1994-05-23 09:58:16 +04:00
|
|
|
pi[2].p_size = sc->sc_blks;
|
1994-05-27 21:18:50 +04:00
|
|
|
/* XXX reset other info since readdisklabel screws with it */
|
|
|
|
lp->d_npartitions = 3;
|
|
|
|
pi[0].p_size = 0;
|
1994-05-23 09:58:16 +04:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
1993-05-13 17:56:20 +04:00
|
|
|
int
|
|
|
|
sdopen(dev, flags, mode, p)
|
|
|
|
dev_t dev;
|
|
|
|
int flags, mode;
|
|
|
|
struct proc *p;
|
|
|
|
{
|
1997-03-31 11:32:14 +04:00
|
|
|
int unit = sdunit(dev);
|
1997-01-30 12:14:10 +03:00
|
|
|
struct sd_softc *sc;
|
1997-01-07 12:29:30 +03:00
|
|
|
int error, mask, part;
|
1993-05-13 17:56:20 +04:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
if (unit >= sd_cd.cd_ndevs ||
|
|
|
|
(sc = sd_cd.cd_devs[unit]) == NULL ||
|
|
|
|
(sc->sc_flags & SDF_ALIVE) == 0)
|
|
|
|
return (ENXIO);
|
1993-05-13 17:56:20 +04:00
|
|
|
|
1994-05-23 09:58:16 +04:00
|
|
|
/*
|
|
|
|
* Wait for any pending opens/closes to complete
|
|
|
|
*/
|
|
|
|
while (sc->sc_flags & (SDF_OPENING|SDF_CLOSING))
|
|
|
|
sleep((caddr_t)sc, PRIBIO);
|
1995-01-18 13:02:43 +03:00
|
|
|
|
1994-05-23 09:58:16 +04:00
|
|
|
/*
|
|
|
|
* On first open, get label and partition info.
|
|
|
|
* We may block reading the label, so be careful
|
|
|
|
* to stop any other opens.
|
|
|
|
*/
|
1996-01-08 01:01:38 +03:00
|
|
|
if (sc->sc_dkdev.dk_openmask == 0) {
|
1994-05-23 09:58:16 +04:00
|
|
|
sc->sc_flags |= SDF_OPENING;
|
|
|
|
error = sdgetinfo(dev);
|
|
|
|
sc->sc_flags &= ~SDF_OPENING;
|
|
|
|
wakeup((caddr_t)sc);
|
|
|
|
if (error)
|
|
|
|
return(error);
|
|
|
|
}
|
1996-01-08 01:01:38 +03:00
|
|
|
|
1997-01-07 12:29:30 +03:00
|
|
|
part = sdpart(dev);
|
|
|
|
mask = 1 << part;
|
|
|
|
|
|
|
|
/* Check that the partition exists. */
|
|
|
|
if (part != RAW_PART &&
|
|
|
|
(part >= sc->sc_dkdev.dk_label->d_npartitions ||
|
|
|
|
sc->sc_dkdev.dk_label->d_partitions[part].p_fstype == FS_UNUSED))
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
/* Ensure only one open at a time. */
|
|
|
|
switch (mode) {
|
|
|
|
case S_IFCHR:
|
1996-01-08 01:01:38 +03:00
|
|
|
sc->sc_dkdev.dk_copenmask |= mask;
|
1997-01-07 12:29:30 +03:00
|
|
|
break;
|
|
|
|
case S_IFBLK:
|
1996-01-08 01:01:38 +03:00
|
|
|
sc->sc_dkdev.dk_bopenmask |= mask;
|
1997-01-07 12:29:30 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
sc->sc_dkdev.dk_openmask =
|
|
|
|
sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
|
|
|
|
|
1994-05-23 09:58:16 +04:00
|
|
|
return(0);
|
|
|
|
}
|
1993-08-08 00:48:38 +04:00
|
|
|
|
1994-05-23 09:58:16 +04:00
|
|
|
int
|
|
|
|
sdclose(dev, flag, mode, p)
|
|
|
|
dev_t dev;
|
|
|
|
int flag, mode;
|
|
|
|
struct proc *p;
|
|
|
|
{
|
|
|
|
int unit = sdunit(dev);
|
1997-01-30 12:14:10 +03:00
|
|
|
struct sd_softc *sc = sd_cd.cd_devs[unit];
|
1997-03-31 11:32:14 +04:00
|
|
|
struct disk *dk = &sc->sc_dkdev;
|
1994-05-23 09:58:16 +04:00
|
|
|
int mask, s;
|
|
|
|
|
|
|
|
mask = 1 << sdpart(dev);
|
|
|
|
if (mode == S_IFCHR)
|
1996-01-08 01:01:38 +03:00
|
|
|
dk->dk_copenmask &= ~mask;
|
1994-05-23 09:58:16 +04:00
|
|
|
else
|
1996-01-08 01:01:38 +03:00
|
|
|
dk->dk_bopenmask &= ~mask;
|
|
|
|
dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
|
1994-05-23 09:58:16 +04:00
|
|
|
/*
|
|
|
|
* On last close, we wait for all activity to cease since
|
|
|
|
* the label/parition info will become invalid. Since we
|
|
|
|
* might sleep, we must block any opens while we are here.
|
|
|
|
* Note we don't have to about other closes since we know
|
|
|
|
* we are the last one.
|
|
|
|
*/
|
1996-01-08 01:01:38 +03:00
|
|
|
if (dk->dk_openmask == 0) {
|
1994-05-23 09:58:16 +04:00
|
|
|
sc->sc_flags |= SDF_CLOSING;
|
|
|
|
s = splbio();
|
1997-01-30 12:14:10 +03:00
|
|
|
while (sc->sc_tab.b_active) {
|
1994-05-23 09:58:16 +04:00
|
|
|
sc->sc_flags |= SDF_WANTED;
|
1997-01-30 12:14:10 +03:00
|
|
|
sleep((caddr_t)&sc->sc_tab, PRIBIO);
|
1993-08-08 00:48:38 +04:00
|
|
|
}
|
1994-05-23 09:58:16 +04:00
|
|
|
splx(s);
|
|
|
|
sc->sc_flags &= ~(SDF_CLOSING|SDF_WLABEL|SDF_ERROR);
|
|
|
|
wakeup((caddr_t)sc);
|
|
|
|
}
|
|
|
|
sc->sc_format_pid = -1;
|
1993-05-13 17:56:20 +04:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine is called for partial block transfers and non-aligned
|
|
|
|
* transfers (the latter only being possible on devices with a block size
|
|
|
|
* larger than DEV_BSIZE). The operation is performed in three steps
|
|
|
|
* using a locally allocated buffer:
|
|
|
|
* 1. transfer any initial partial block
|
|
|
|
* 2. transfer full blocks
|
|
|
|
* 3. transfer any final partial block
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
sdlblkstrat(bp, bsize)
|
1997-03-31 11:32:14 +04:00
|
|
|
struct buf *bp;
|
|
|
|
int bsize;
|
1993-05-13 17:56:20 +04:00
|
|
|
{
|
1997-01-30 12:14:10 +03:00
|
|
|
struct sd_softc *sc = sd_cd.cd_devs[sdunit(bp->b_dev)];
|
1997-03-31 11:32:14 +04:00
|
|
|
struct buf *cbp = (struct buf *)malloc(sizeof(struct buf),
|
1993-05-13 17:56:20 +04:00
|
|
|
M_DEVBUF, M_WAITOK);
|
|
|
|
caddr_t cbuf = (caddr_t)malloc(bsize, M_DEVBUF, M_WAITOK);
|
1997-03-31 11:32:14 +04:00
|
|
|
int bn, resid;
|
|
|
|
caddr_t addr;
|
1993-05-13 17:56:20 +04:00
|
|
|
|
|
|
|
bzero((caddr_t)cbp, sizeof(*cbp));
|
|
|
|
cbp->b_proc = curproc; /* XXX */
|
|
|
|
cbp->b_dev = bp->b_dev;
|
|
|
|
bn = bp->b_blkno;
|
|
|
|
resid = bp->b_bcount;
|
|
|
|
addr = bp->b_un.b_addr;
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (sddebug & SDB_PARTIAL)
|
1997-04-03 02:37:21 +04:00
|
|
|
printf("sdlblkstrat: bp %p flags %lx bn %x resid %x addr %p\n",
|
1993-05-13 17:56:20 +04:00
|
|
|
bp, bp->b_flags, bn, resid, addr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while (resid > 0) {
|
1997-03-31 11:32:14 +04:00
|
|
|
int boff = dbtob(bn) & (bsize - 1);
|
|
|
|
int count;
|
1993-05-13 17:56:20 +04:00
|
|
|
|
|
|
|
if (boff || resid < bsize) {
|
1997-01-30 12:14:10 +03:00
|
|
|
sc->sc_stats.sdpartials++;
|
1994-05-23 09:58:16 +04:00
|
|
|
count = min(resid, bsize - boff);
|
1993-05-13 17:56:20 +04:00
|
|
|
cbp->b_flags = B_BUSY | B_PHYS | B_READ;
|
|
|
|
cbp->b_blkno = bn - btodb(boff);
|
|
|
|
cbp->b_un.b_addr = cbuf;
|
|
|
|
cbp->b_bcount = bsize;
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (sddebug & SDB_PARTIAL)
|
1997-04-03 02:37:21 +04:00
|
|
|
printf(" readahead: bn %x cnt %x off %x addr %p\n",
|
1993-05-13 17:56:20 +04:00
|
|
|
cbp->b_blkno, count, boff, addr);
|
|
|
|
#endif
|
|
|
|
sdstrategy(cbp);
|
|
|
|
biowait(cbp);
|
|
|
|
if (cbp->b_flags & B_ERROR) {
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
bp->b_error = cbp->b_error;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (bp->b_flags & B_READ) {
|
|
|
|
bcopy(&cbuf[boff], addr, count);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
bcopy(addr, &cbuf[boff], count);
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (sddebug & SDB_PARTIAL)
|
1997-04-03 02:37:21 +04:00
|
|
|
printf(" writeback: bn %x cnt %x off %x addr %p\n",
|
1993-05-13 17:56:20 +04:00
|
|
|
cbp->b_blkno, count, boff, addr);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
count = resid & ~(bsize - 1);
|
|
|
|
cbp->b_blkno = bn;
|
|
|
|
cbp->b_un.b_addr = addr;
|
|
|
|
cbp->b_bcount = count;
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (sddebug & SDB_PARTIAL)
|
1997-04-03 02:37:21 +04:00
|
|
|
printf(" fulltrans: bn %x cnt %x addr %p\n",
|
1993-05-13 17:56:20 +04:00
|
|
|
cbp->b_blkno, count, addr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
cbp->b_flags = B_BUSY | B_PHYS | (bp->b_flags & B_READ);
|
|
|
|
sdstrategy(cbp);
|
|
|
|
biowait(cbp);
|
|
|
|
if (cbp->b_flags & B_ERROR) {
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
bp->b_error = cbp->b_error;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
bn += btodb(count);
|
|
|
|
resid -= count;
|
|
|
|
addr += count;
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (sddebug & SDB_PARTIAL)
|
1997-04-03 02:37:21 +04:00
|
|
|
printf(" done: bn %x resid %x addr %p\n",
|
1993-05-13 17:56:20 +04:00
|
|
|
bn, resid, addr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
free(cbuf, M_DEVBUF);
|
|
|
|
free(cbp, M_DEVBUF);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sdstrategy(bp)
|
1997-03-31 11:32:14 +04:00
|
|
|
struct buf *bp;
|
1993-05-13 17:56:20 +04:00
|
|
|
{
|
1994-05-23 09:58:16 +04:00
|
|
|
int unit = sdunit(bp->b_dev);
|
1997-01-30 12:14:10 +03:00
|
|
|
struct sd_softc *sc = sd_cd.cd_devs[unit];
|
1997-03-31 11:32:14 +04:00
|
|
|
struct buf *dp = &sc->sc_tab;
|
|
|
|
struct partition *pinfo;
|
|
|
|
daddr_t bn;
|
|
|
|
int sz, s;
|
1997-01-07 12:29:30 +03:00
|
|
|
int offset;
|
1993-05-13 17:56:20 +04:00
|
|
|
|
1994-05-23 09:58:16 +04:00
|
|
|
if (sc->sc_format_pid >= 0) {
|
1993-05-13 17:56:20 +04:00
|
|
|
if (sc->sc_format_pid != curproc->p_pid) { /* XXX */
|
|
|
|
bp->b_error = EPERM;
|
1994-05-23 09:58:16 +04:00
|
|
|
goto bad;
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
|
|
|
bp->b_cylin = 0;
|
|
|
|
} else {
|
1994-05-23 09:58:16 +04:00
|
|
|
if (sc->sc_flags & SDF_ERROR) {
|
|
|
|
bp->b_error = EIO;
|
|
|
|
goto bad;
|
|
|
|
}
|
1993-05-13 17:56:20 +04:00
|
|
|
bn = bp->b_blkno;
|
|
|
|
sz = howmany(bp->b_bcount, DEV_BSIZE);
|
1996-01-08 01:01:38 +03:00
|
|
|
pinfo = &sc->sc_dkdev.dk_label->d_partitions[sdpart(bp->b_dev)];
|
1997-01-07 12:29:30 +03:00
|
|
|
|
|
|
|
/* Don't perform partition translation on RAW_PART. */
|
|
|
|
offset = (sdpart(bp->b_dev) == RAW_PART) ? 0 : pinfo->p_offset;
|
|
|
|
|
|
|
|
if (sdpart(bp->b_dev) != RAW_PART) {
|
|
|
|
/*
|
|
|
|
* XXX This block of code belongs in
|
|
|
|
* XXX bounds_check_with_label()
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (bn < 0 || bn + sz > pinfo->p_size) {
|
|
|
|
sz = pinfo->p_size - bn;
|
|
|
|
if (sz == 0) {
|
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (sz < 0) {
|
|
|
|
bp->b_error = EINVAL;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
bp->b_bcount = dbtob(sz);
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
1997-01-07 12:29:30 +03:00
|
|
|
/*
|
|
|
|
* Check for write to write protected label
|
|
|
|
*/
|
|
|
|
if (bn + offset <= LABELSECTOR &&
|
1994-05-23 09:58:16 +04:00
|
|
|
#if LABELSECTOR != 0
|
1997-01-07 12:29:30 +03:00
|
|
|
bn + offset + sz > LABELSECTOR &&
|
1994-05-23 09:58:16 +04:00
|
|
|
#endif
|
1997-01-07 12:29:30 +03:00
|
|
|
!(bp->b_flags & B_READ) &&
|
|
|
|
!(sc->sc_flags & SDF_WLABEL)) {
|
|
|
|
bp->b_error = EROFS;
|
|
|
|
goto bad;
|
|
|
|
}
|
1994-05-23 09:58:16 +04:00
|
|
|
}
|
1993-05-13 17:56:20 +04:00
|
|
|
/*
|
|
|
|
* Non-aligned or partial-block transfers handled specially.
|
|
|
|
*/
|
|
|
|
s = sc->sc_blksize - 1;
|
|
|
|
if ((dbtob(bn) & s) || (bp->b_bcount & s)) {
|
|
|
|
sdlblkstrat(bp, sc->sc_blksize);
|
|
|
|
goto done;
|
|
|
|
}
|
1997-01-07 12:29:30 +03:00
|
|
|
bp->b_cylin = (bn + offset) >> sc->sc_bshift;
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
|
|
|
s = splbio();
|
|
|
|
disksort(dp, bp);
|
|
|
|
if (dp->b_active == 0) {
|
|
|
|
dp->b_active = 1;
|
|
|
|
sdustart(unit);
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
return;
|
1994-05-23 09:58:16 +04:00
|
|
|
bad:
|
|
|
|
bp->b_flags |= B_ERROR;
|
1993-05-13 17:56:20 +04:00
|
|
|
done:
|
|
|
|
biodone(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sdustart(unit)
|
1997-03-31 11:32:14 +04:00
|
|
|
int unit;
|
1993-05-13 17:56:20 +04:00
|
|
|
{
|
1997-01-30 12:14:10 +03:00
|
|
|
struct sd_softc *sc = sd_cd.cd_devs[unit];
|
|
|
|
|
|
|
|
if (scsireq(sc->sc_dev.dv_parent, &sc->sc_sq))
|
|
|
|
sdstart(sc);
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return:
|
|
|
|
* 0 if not really an error
|
|
|
|
* <0 if we should do a retry
|
|
|
|
* >0 if a fatal error
|
|
|
|
*/
|
|
|
|
static int
|
1997-01-30 12:14:10 +03:00
|
|
|
sderror(sc, stat)
|
|
|
|
struct sd_softc *sc;
|
|
|
|
int stat;
|
1993-05-13 17:56:20 +04:00
|
|
|
{
|
|
|
|
int cond = 1;
|
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
sc->sc_sensestore.status = stat;
|
1993-05-13 17:56:20 +04:00
|
|
|
if (stat & STS_CHECKCOND) {
|
|
|
|
struct scsi_xsense *sp;
|
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
scsi_request_sense(sc->sc_dev.dv_parent->dv_unit,
|
|
|
|
sc->sc_target, sc->sc_lun, sc->sc_sensestore.sense,
|
|
|
|
sizeof(sc->sc_sensestore.sense));
|
|
|
|
sp = (struct scsi_xsense *)(sc->sc_sensestore.sense);
|
|
|
|
printf("%s: scsi sense class %d, code %d", sc->sc_dev.dv_xname,
|
1993-05-13 17:56:20 +04:00
|
|
|
sp->class, sp->code);
|
|
|
|
if (sp->class == 7) {
|
1996-10-13 07:14:05 +04:00
|
|
|
printf(", key %d", sp->key);
|
1993-05-13 17:56:20 +04:00
|
|
|
if (sp->valid)
|
1996-10-13 07:14:05 +04:00
|
|
|
printf(", blk %d", *(int *)&sp->info1);
|
1993-05-13 17:56:20 +04:00
|
|
|
switch (sp->key) {
|
|
|
|
/* no sense, try again */
|
|
|
|
case 0:
|
|
|
|
cond = -1;
|
|
|
|
break;
|
|
|
|
/* recovered error, not a problem */
|
|
|
|
case 1:
|
|
|
|
cond = 0;
|
|
|
|
break;
|
1994-05-23 09:58:16 +04:00
|
|
|
/* possible media change */
|
|
|
|
case 6:
|
|
|
|
/*
|
|
|
|
* For removable media, if we are doing the
|
|
|
|
* first open (i.e. reading the label) go
|
|
|
|
* ahead and retry, otherwise someone has
|
|
|
|
* changed the media out from under us and
|
|
|
|
* we should abort any further operations
|
|
|
|
* until a close is done.
|
|
|
|
*/
|
|
|
|
if (sc->sc_flags & SDF_RMEDIA) {
|
|
|
|
if (sc->sc_flags & SDF_OPENING)
|
|
|
|
cond = -1;
|
|
|
|
else
|
|
|
|
sc->sc_flags |= SDF_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
|
|
|
}
|
1996-10-13 07:14:05 +04:00
|
|
|
printf("\n");
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
|
|
|
return(cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1997-01-30 12:14:10 +03:00
|
|
|
sdfinish(sc, bp)
|
|
|
|
struct sd_softc *sc;
|
|
|
|
struct buf *bp;
|
1993-05-13 17:56:20 +04:00
|
|
|
{
|
1997-03-31 11:32:14 +04:00
|
|
|
struct buf *dp = &sc->sc_tab;
|
1994-05-23 09:58:16 +04:00
|
|
|
|
|
|
|
dp->b_errcnt = 0;
|
|
|
|
dp->b_actf = bp->b_actf;
|
1993-05-13 17:56:20 +04:00
|
|
|
bp->b_resid = 0;
|
|
|
|
biodone(bp);
|
1997-01-30 12:14:10 +03:00
|
|
|
scsifree(sc->sc_dev.dv_parent, &sc->sc_sq);
|
1994-05-23 09:58:16 +04:00
|
|
|
if (dp->b_actf)
|
1997-01-30 12:14:10 +03:00
|
|
|
sdustart(sc->sc_dev.dv_unit);
|
1994-05-23 09:58:16 +04:00
|
|
|
else {
|
|
|
|
dp->b_active = 0;
|
|
|
|
if (sc->sc_flags & SDF_WANTED) {
|
|
|
|
sc->sc_flags &= ~SDF_WANTED;
|
|
|
|
wakeup((caddr_t)dp);
|
|
|
|
}
|
|
|
|
}
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-01-30 12:14:10 +03:00
|
|
|
sdstart(arg)
|
|
|
|
void *arg;
|
1993-05-13 17:56:20 +04:00
|
|
|
{
|
1997-01-30 12:14:10 +03:00
|
|
|
struct sd_softc *sc = arg;
|
1993-05-13 17:56:20 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* we have the SCSI bus -- in format mode, we may or may not need dma
|
|
|
|
* so check now.
|
|
|
|
*/
|
1997-01-30 12:14:10 +03:00
|
|
|
if (sc->sc_format_pid >= 0 && legal_cmds[sc->sc_cmdstore.cdb[0]] > 0) {
|
1997-03-31 11:32:14 +04:00
|
|
|
struct buf *bp = sc->sc_tab.b_actf;
|
|
|
|
int sts;
|
1993-05-13 17:56:20 +04:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
sc->sc_tab.b_errcnt = 0;
|
1994-05-23 09:58:16 +04:00
|
|
|
while (1) {
|
1997-01-30 12:14:10 +03:00
|
|
|
sts = scsi_immed_command(sc->sc_dev.dv_parent->dv_unit,
|
|
|
|
sc->sc_target, sc->sc_lun, &sc->sc_cmdstore,
|
|
|
|
bp->b_un.b_addr, bp->b_bcount,
|
|
|
|
bp->b_flags & B_READ);
|
|
|
|
sc->sc_sensestore.status = sts;
|
1994-05-23 09:58:16 +04:00
|
|
|
if ((sts & 0xfe) == 0 ||
|
1997-01-30 12:14:10 +03:00
|
|
|
(sts = sderror(sc, sts)) == 0)
|
1994-05-23 09:58:16 +04:00
|
|
|
break;
|
1997-01-30 12:14:10 +03:00
|
|
|
if (sts > 0 || sc->sc_tab.b_errcnt++ >= SDRETRY) {
|
1994-05-23 09:58:16 +04:00
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
bp->b_error = EIO;
|
|
|
|
break;
|
|
|
|
}
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
1997-01-30 12:14:10 +03:00
|
|
|
sdfinish(sc, bp);
|
1993-05-13 17:56:20 +04:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
} else if (scsiustart(sc->sc_dev.dv_parent->dv_unit))
|
|
|
|
sdgo(sc);
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-01-30 12:14:10 +03:00
|
|
|
sdgo(arg)
|
|
|
|
void *arg;
|
1993-05-13 17:56:20 +04:00
|
|
|
{
|
1997-01-30 12:14:10 +03:00
|
|
|
struct sd_softc *sc = arg;
|
1997-03-31 11:32:14 +04:00
|
|
|
struct buf *bp = sc->sc_tab.b_actf;
|
|
|
|
int pad;
|
|
|
|
struct scsi_fmt_cdb *cmd;
|
1993-05-13 17:56:20 +04:00
|
|
|
|
1994-05-23 09:58:16 +04:00
|
|
|
if (sc->sc_format_pid >= 0) {
|
1997-01-30 12:14:10 +03:00
|
|
|
cmd = &sc->sc_cmdstore;
|
1993-05-13 17:56:20 +04:00
|
|
|
pad = 0;
|
|
|
|
} else {
|
1994-05-23 09:58:16 +04:00
|
|
|
/*
|
|
|
|
* Drive is in an error state, abort all operations
|
|
|
|
*/
|
|
|
|
if (sc->sc_flags & SDF_ERROR) {
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
bp->b_error = EIO;
|
1997-01-30 12:14:10 +03:00
|
|
|
sdfinish(sc, bp);
|
1994-05-23 09:58:16 +04:00
|
|
|
return;
|
|
|
|
}
|
1993-05-13 17:56:20 +04:00
|
|
|
cmd = bp->b_flags & B_READ? &sd_read_cmd : &sd_write_cmd;
|
|
|
|
*(int *)(&cmd->cdb[2]) = bp->b_cylin;
|
|
|
|
pad = howmany(bp->b_bcount, sc->sc_blksize);
|
|
|
|
*(u_short *)(&cmd->cdb[7]) = pad;
|
|
|
|
pad = (bp->b_bcount & (sc->sc_blksize - 1)) != 0;
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (pad)
|
1997-04-03 02:37:21 +04:00
|
|
|
printf("%s: partial block xfer -- %lx bytes\n",
|
1997-01-30 12:14:10 +03:00
|
|
|
sc->sc_dev.dv_xname, bp->b_bcount);
|
1993-05-13 17:56:20 +04:00
|
|
|
#endif
|
1997-01-30 12:14:10 +03:00
|
|
|
sc->sc_stats.sdtransfers++;
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
1994-05-23 09:58:16 +04:00
|
|
|
#ifdef USELEDS
|
1997-05-06 01:09:13 +04:00
|
|
|
ledcontrol(0, 0, LED_DISK);
|
1994-05-23 09:58:16 +04:00
|
|
|
#endif
|
1997-01-30 12:14:10 +03:00
|
|
|
if (scsigo(sc->sc_dev.dv_parent->dv_unit, sc->sc_target, sc->sc_lun,
|
1996-01-08 01:01:38 +03:00
|
|
|
bp, cmd, pad) == 0) {
|
|
|
|
/* Instrumentation. */
|
|
|
|
disk_busy(&sc->sc_dkdev);
|
|
|
|
sc->sc_dkdev.dk_seek++; /* XXX */
|
1993-05-13 17:56:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (sddebug & SDB_ERROR)
|
1997-04-03 02:37:21 +04:00
|
|
|
printf("%s: sdstart: %s adr %p blk %ld len %ld ecnt %ld\n",
|
1997-01-30 12:14:10 +03:00
|
|
|
sc->sc_dev.dv_xname,
|
1995-12-02 21:21:49 +03:00
|
|
|
bp->b_flags & B_READ? "read" : "write",
|
1993-05-13 17:56:20 +04:00
|
|
|
bp->b_un.b_addr, bp->b_cylin, bp->b_bcount,
|
1997-01-30 12:14:10 +03:00
|
|
|
sc->sc_tab.b_errcnt);
|
1993-05-13 17:56:20 +04:00
|
|
|
#endif
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
bp->b_error = EIO;
|
1997-01-30 12:14:10 +03:00
|
|
|
sdfinish(sc, bp);
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1996-02-14 05:43:54 +03:00
|
|
|
sdintr(arg, stat)
|
|
|
|
void *arg;
|
1993-05-13 17:56:20 +04:00
|
|
|
int stat;
|
|
|
|
{
|
1997-03-31 11:32:14 +04:00
|
|
|
struct sd_softc *sc = arg;
|
|
|
|
struct buf *bp = sc->sc_tab.b_actf;
|
1993-05-13 17:56:20 +04:00
|
|
|
int cond;
|
|
|
|
|
|
|
|
if (bp == NULL) {
|
1997-01-30 12:14:10 +03:00
|
|
|
printf("%s: bp == NULL\n", sc->sc_dev.dv_xname);
|
1993-05-13 17:56:20 +04:00
|
|
|
return;
|
|
|
|
}
|
1996-01-08 01:01:38 +03:00
|
|
|
|
|
|
|
disk_unbusy(&sc->sc_dkdev, (bp->b_bcount - bp->b_resid));
|
|
|
|
|
1993-05-13 17:56:20 +04:00
|
|
|
if (stat) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (sddebug & SDB_ERROR)
|
1996-10-13 07:14:05 +04:00
|
|
|
printf("%s: sdintr: bad scsi status 0x%x\n",
|
1997-01-30 12:14:10 +03:00
|
|
|
sc->sc_dev.dv_xname, stat);
|
1993-05-13 17:56:20 +04:00
|
|
|
#endif
|
1997-01-30 12:14:10 +03:00
|
|
|
cond = sderror(sc, stat);
|
1993-05-13 17:56:20 +04:00
|
|
|
if (cond) {
|
1997-01-30 12:14:10 +03:00
|
|
|
if (cond < 0 && sc->sc_tab.b_errcnt++ < SDRETRY) {
|
1993-05-13 17:56:20 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (sddebug & SDB_ERROR)
|
1997-04-03 02:37:21 +04:00
|
|
|
printf("%s: retry #%ld\n",
|
1997-01-30 12:14:10 +03:00
|
|
|
sc->sc_dev.dv_xname,
|
|
|
|
sc->sc_tab.b_errcnt);
|
1993-05-13 17:56:20 +04:00
|
|
|
#endif
|
1997-01-30 12:14:10 +03:00
|
|
|
sdstart(sc);
|
1993-05-13 17:56:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
bp->b_error = EIO;
|
|
|
|
}
|
|
|
|
}
|
1997-01-30 12:14:10 +03:00
|
|
|
sdfinish(sc, bp);
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
|
|
|
|
1994-05-23 09:58:16 +04:00
|
|
|
int
|
|
|
|
sdread(dev, uio, flags)
|
|
|
|
dev_t dev;
|
|
|
|
struct uio *uio;
|
|
|
|
int flags;
|
|
|
|
{
|
1997-03-31 11:32:14 +04:00
|
|
|
int unit = sdunit(dev);
|
1997-01-30 12:14:10 +03:00
|
|
|
struct sd_softc *sc = sd_cd.cd_devs[unit];
|
1997-03-31 11:32:14 +04:00
|
|
|
int pid;
|
1994-05-23 09:58:16 +04:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
if ((pid = sc->sc_format_pid) >= 0 &&
|
1994-05-23 09:58:16 +04:00
|
|
|
pid != uio->uio_procp->p_pid)
|
|
|
|
return (EPERM);
|
|
|
|
|
|
|
|
return (physio(sdstrategy, NULL, dev, B_READ, minphys, uio));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sdwrite(dev, uio, flags)
|
|
|
|
dev_t dev;
|
|
|
|
struct uio *uio;
|
|
|
|
int flags;
|
|
|
|
{
|
1997-03-31 11:32:14 +04:00
|
|
|
int unit = sdunit(dev);
|
1997-01-30 12:14:10 +03:00
|
|
|
struct sd_softc *sc = sd_cd.cd_devs[unit];
|
1997-03-31 11:32:14 +04:00
|
|
|
int pid;
|
1994-05-23 09:58:16 +04:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
if ((pid = sc->sc_format_pid) >= 0 &&
|
1994-05-23 09:58:16 +04:00
|
|
|
pid != uio->uio_procp->p_pid)
|
|
|
|
return (EPERM);
|
|
|
|
|
|
|
|
return (physio(sdstrategy, NULL, dev, B_WRITE, minphys, uio));
|
|
|
|
}
|
|
|
|
|
1993-05-13 17:56:20 +04:00
|
|
|
int
|
|
|
|
sdioctl(dev, cmd, data, flag, p)
|
|
|
|
dev_t dev;
|
1997-03-31 11:32:14 +04:00
|
|
|
u_long cmd;
|
1993-05-13 17:56:20 +04:00
|
|
|
caddr_t data;
|
|
|
|
int flag;
|
|
|
|
struct proc *p;
|
|
|
|
{
|
1994-05-23 09:58:16 +04:00
|
|
|
int unit = sdunit(dev);
|
1997-01-30 12:14:10 +03:00
|
|
|
struct sd_softc *sc = sd_cd.cd_devs[unit];
|
1997-03-31 11:32:14 +04:00
|
|
|
struct disklabel *lp = sc->sc_dkdev.dk_label;
|
1994-01-25 06:19:15 +03:00
|
|
|
int error, flags;
|
1994-01-25 16:10:21 +03:00
|
|
|
|
1993-05-13 17:56:20 +04:00
|
|
|
switch (cmd) {
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
|
1994-01-25 06:19:15 +03:00
|
|
|
case DIOCGDINFO:
|
|
|
|
*(struct disklabel *)data = *lp;
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
case DIOCGPART:
|
|
|
|
((struct partinfo *)data)->disklab = lp;
|
|
|
|
((struct partinfo *)data)->part =
|
|
|
|
&lp->d_partitions[sdpart(dev)];
|
|
|
|
return (0);
|
|
|
|
|
1995-05-24 17:12:12 +04:00
|
|
|
case DIOCWLABEL:
|
|
|
|
if ((flag & FWRITE) == 0)
|
1995-06-02 21:51:21 +04:00
|
|
|
return (EBADF);
|
1994-01-25 06:19:15 +03:00
|
|
|
if (*(int *)data)
|
1994-05-23 09:58:16 +04:00
|
|
|
sc->sc_flags |= SDF_WLABEL;
|
1994-01-25 06:19:15 +03:00
|
|
|
else
|
1994-05-23 09:58:16 +04:00
|
|
|
sc->sc_flags &= ~SDF_WLABEL;
|
1994-01-25 06:19:15 +03:00
|
|
|
return (0);
|
|
|
|
|
1995-05-24 17:12:12 +04:00
|
|
|
case DIOCSDINFO:
|
|
|
|
if ((flag & FWRITE) == 0)
|
|
|
|
return (EBADF);
|
1994-01-25 06:19:15 +03:00
|
|
|
error = setdisklabel(lp, (struct disklabel *)data,
|
1994-02-22 10:17:22 +03:00
|
|
|
(sc->sc_flags & SDF_WLABEL) ? 0
|
1996-01-08 01:01:38 +03:00
|
|
|
: sc->sc_dkdev.dk_openmask,
|
1994-05-23 09:58:16 +04:00
|
|
|
(struct cpu_disklabel *)0);
|
1994-01-25 06:19:15 +03:00
|
|
|
return (error);
|
|
|
|
|
1995-05-24 17:12:12 +04:00
|
|
|
case DIOCWDINFO:
|
1994-01-25 06:19:15 +03:00
|
|
|
if ((flag & FWRITE) == 0)
|
|
|
|
return (EBADF);
|
|
|
|
error = setdisklabel(lp, (struct disklabel *)data,
|
1994-02-22 10:17:22 +03:00
|
|
|
(sc->sc_flags & SDF_WLABEL) ? 0
|
1996-01-08 01:01:38 +03:00
|
|
|
: sc->sc_dkdev.dk_openmask,
|
1994-05-23 09:58:16 +04:00
|
|
|
(struct cpu_disklabel *)0);
|
1994-01-25 06:19:15 +03:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
flags = sc->sc_flags;
|
1994-05-23 09:58:16 +04:00
|
|
|
sc->sc_flags = SDF_ALIVE | SDF_WLABEL;
|
|
|
|
error = writedisklabel(sdlabdev(dev), sdstrategy, lp,
|
|
|
|
(struct cpu_disklabel *)0);
|
1994-01-25 06:19:15 +03:00
|
|
|
sc->sc_flags = flags;
|
|
|
|
return (error);
|
1993-08-08 00:48:38 +04:00
|
|
|
|
1993-05-13 17:56:20 +04:00
|
|
|
case SDIOCSFORMAT:
|
|
|
|
/* take this device into or out of "format" mode */
|
|
|
|
if (suser(p->p_ucred, &p->p_acflag))
|
|
|
|
return(EPERM);
|
|
|
|
|
|
|
|
if (*(int *)data) {
|
1994-05-23 09:58:16 +04:00
|
|
|
if (sc->sc_format_pid >= 0)
|
1993-05-13 17:56:20 +04:00
|
|
|
return (EPERM);
|
|
|
|
sc->sc_format_pid = p->p_pid;
|
|
|
|
} else
|
1994-05-23 09:58:16 +04:00
|
|
|
sc->sc_format_pid = -1;
|
1993-05-13 17:56:20 +04:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
case SDIOCGFORMAT:
|
|
|
|
/* find out who has the device in format mode */
|
|
|
|
*(int *)data = sc->sc_format_pid;
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
case SDIOCSCSICOMMAND:
|
|
|
|
/*
|
|
|
|
* Save what user gave us as SCSI cdb to use with next
|
|
|
|
* read or write to the char device.
|
|
|
|
*/
|
|
|
|
if (sc->sc_format_pid != p->p_pid)
|
|
|
|
return (EPERM);
|
|
|
|
if (legal_cmds[((struct scsi_fmt_cdb *)data)->cdb[0]] == 0)
|
|
|
|
return (EINVAL);
|
1997-01-30 12:14:10 +03:00
|
|
|
bcopy(data, &sc->sc_cmdstore, sizeof(struct scsi_fmt_cdb));
|
1993-05-13 17:56:20 +04:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
case SDIOCSENSE:
|
|
|
|
/*
|
|
|
|
* return the SCSI sense data saved after the last
|
|
|
|
* operation that completed with "check condition" status.
|
|
|
|
*/
|
1997-01-30 12:14:10 +03:00
|
|
|
bcopy(&sc->sc_sensestore, data, sizeof(sc->sc_sensestore));
|
1993-05-13 17:56:20 +04:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
}
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sdsize(dev)
|
|
|
|
dev_t dev;
|
|
|
|
{
|
1997-03-31 11:32:14 +04:00
|
|
|
int unit = sdunit(dev);
|
1997-01-30 12:14:10 +03:00
|
|
|
struct sd_softc *sc = sd_cd.cd_devs[unit];
|
1994-05-23 09:58:16 +04:00
|
|
|
int psize, didopen = 0;
|
1993-05-13 17:56:20 +04:00
|
|
|
|
1997-01-30 12:14:10 +03:00
|
|
|
if (unit >= sd_cd.cd_ndevs ||
|
|
|
|
(sc = sd_cd.cd_devs[unit]) == NULL ||
|
|
|
|
(sc->sc_flags & SDF_ALIVE) == 0)
|
|
|
|
return (-1);
|
1993-05-13 17:56:20 +04:00
|
|
|
|
1994-05-23 09:58:16 +04:00
|
|
|
/*
|
|
|
|
* We get called very early on (via swapconf)
|
|
|
|
* without the device being open so we may need
|
|
|
|
* to handle it here.
|
|
|
|
*/
|
1996-01-08 01:01:38 +03:00
|
|
|
if (sc->sc_dkdev.dk_openmask == 0) {
|
1994-05-23 09:58:16 +04:00
|
|
|
if (sdopen(dev, FREAD|FWRITE, S_IFBLK, NULL))
|
|
|
|
return(-1);
|
|
|
|
didopen = 1;
|
|
|
|
}
|
1996-01-08 01:01:38 +03:00
|
|
|
psize = sc->sc_dkdev.dk_label->d_partitions[sdpart(dev)].p_size;
|
1994-05-23 09:58:16 +04:00
|
|
|
if (didopen)
|
|
|
|
(void) sdclose(dev, FREAD|FWRITE, S_IFBLK, NULL);
|
|
|
|
return (psize);
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
|
|
|
|
1996-10-06 04:14:12 +04:00
|
|
|
static int sddoingadump; /* simple mutex */
|
|
|
|
|
1993-05-13 17:56:20 +04:00
|
|
|
/*
|
|
|
|
* Non-interrupt driven, non-dma dump routine.
|
|
|
|
*/
|
|
|
|
int
|
1996-10-06 04:14:12 +04:00
|
|
|
sddump(dev, blkno, va, size)
|
1993-05-13 17:56:20 +04:00
|
|
|
dev_t dev;
|
1996-10-06 04:14:12 +04:00
|
|
|
daddr_t blkno;
|
|
|
|
caddr_t va;
|
|
|
|
size_t size;
|
1993-05-13 17:56:20 +04:00
|
|
|
{
|
1996-10-06 04:14:12 +04:00
|
|
|
int sectorsize; /* size of a disk sector */
|
|
|
|
int nsects; /* number of sectors in partition */
|
|
|
|
int sectoff; /* sector offset of partition */
|
|
|
|
int totwrt; /* total number of sectors left to write */
|
|
|
|
int nwrt; /* current number of sectors to write */
|
|
|
|
int unit, part;
|
|
|
|
struct sd_softc *sc;
|
|
|
|
struct disklabel *lp;
|
|
|
|
char stat;
|
|
|
|
|
|
|
|
/* Check for recursive dump; if so, punt. */
|
|
|
|
if (sddoingadump)
|
|
|
|
return (EFAULT);
|
|
|
|
sddoingadump = 1;
|
|
|
|
|
|
|
|
/* Decompose unit and partition. */
|
|
|
|
unit = sdunit(dev);
|
|
|
|
part = sdpart(dev);
|
|
|
|
|
|
|
|
/* Make sure device is ok. */
|
1997-01-30 12:14:10 +03:00
|
|
|
if (unit >= sd_cd.cd_ndevs ||
|
|
|
|
(sc = sd_cd.cd_devs[unit]) == NULL ||
|
|
|
|
(sc->sc_flags & SDF_ALIVE) == 0)
|
1996-10-06 04:14:12 +04:00
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert to disk sectors. Request must be a multiple of size.
|
|
|
|
*/
|
|
|
|
lp = sc->sc_dkdev.dk_label;
|
|
|
|
sectorsize = lp->d_secsize;
|
|
|
|
if ((size % sectorsize) != 0)
|
|
|
|
return (EFAULT);
|
|
|
|
totwrt = size / sectorsize;
|
|
|
|
blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */
|
|
|
|
|
|
|
|
nsects = lp->d_partitions[part].p_size;
|
|
|
|
sectoff = lp->d_partitions[part].p_offset;
|
|
|
|
|
|
|
|
/* Check transfer bounds against partition size. */
|
|
|
|
if ((blkno < 0) || (blkno + totwrt) > nsects)
|
1993-05-13 17:56:20 +04:00
|
|
|
return (EINVAL);
|
1996-10-06 04:14:12 +04:00
|
|
|
|
|
|
|
/* Offset block number to start of partition. */
|
|
|
|
blkno += sectoff;
|
|
|
|
|
|
|
|
while (totwrt > 0) {
|
|
|
|
nwrt = totwrt; /* XXX */
|
|
|
|
#ifndef SD_DUMP_NOT_TRUSTED
|
|
|
|
/*
|
|
|
|
* Send the data. Note the `0' argument for bshift;
|
|
|
|
* we've done the necessary conversion above.
|
|
|
|
*/
|
1997-01-30 12:14:10 +03:00
|
|
|
stat = scsi_tt_write(sc->sc_dev.dv_parent->dv_unit,
|
|
|
|
sc->sc_target, sc->sc_lun, va, nwrt * sectorsize,
|
|
|
|
blkno, 0);
|
1993-05-13 17:56:20 +04:00
|
|
|
if (stat) {
|
1996-10-13 07:14:05 +04:00
|
|
|
printf("\nsddump: scsi write error 0x%x\n", stat);
|
1993-05-13 17:56:20 +04:00
|
|
|
return (EIO);
|
|
|
|
}
|
1996-10-06 04:14:12 +04:00
|
|
|
#else /* SD_DUMP_NOT_TRUSTED */
|
|
|
|
/* Lets just talk about it first. */
|
1997-01-30 12:14:10 +03:00
|
|
|
printf("%s: dump addr %p, blk %d\n", sc->sc_dev.dv_xname,
|
1996-10-06 04:14:12 +04:00
|
|
|
va, blkno);
|
|
|
|
delay(500 * 1000); /* half a second */
|
|
|
|
#endif /* SD_DUMP_NOT_TRUSTED */
|
|
|
|
|
|
|
|
/* update block count */
|
|
|
|
totwrt -= nwrt;
|
|
|
|
blkno += nwrt;
|
|
|
|
va += sectorsize * nwrt;
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
1996-10-06 04:14:12 +04:00
|
|
|
sddoingadump = 0;
|
1993-05-13 17:56:20 +04:00
|
|
|
return (0);
|
|
|
|
}
|