141480Smckusick /* 241480Smckusick * Copyright (c) 1990 The Regents of the University of California. 341480Smckusick * All rights reserved. 441480Smckusick * 541480Smckusick * This code is derived from software contributed to Berkeley by 641480Smckusick * Van Jacobson of Lawrence Berkeley Laboratory. 741480Smckusick * 841480Smckusick * %sccs.include.redist.c% 941480Smckusick * 10*50039Skarels * @(#)sd.c 7.8 (Berkeley) 06/09/91 1141480Smckusick */ 1241480Smckusick 1341480Smckusick /* 1441480Smckusick * SCSI CCS (Command Command Set) disk driver. 1541480Smckusick */ 1641480Smckusick #include "sd.h" 1741480Smckusick #if NSD > 0 1841480Smckusick 1941480Smckusick #ifndef lint 2049304Shibler static char rcsid[] = "$Header: sd.c,v 1.15 91/04/24 11:54:30 mike Exp $"; 2141480Smckusick #endif 2241480Smckusick 2345788Sbostic #include "sys/param.h" 2445788Sbostic #include "sys/systm.h" 2545788Sbostic #include "sys/buf.h" 2645788Sbostic #include "sys/dkstat.h" 2745788Sbostic #include "sys/disklabel.h" 2845788Sbostic #include "sys/malloc.h" 2945788Sbostic #include "sys/proc.h" 3041480Smckusick 3149132Skarels #include "device.h" 3249132Skarels #include "scsireg.h" 3345788Sbostic #include "vm/vm_param.h" 3449132Skarels #include "vm/lock.h" 3549132Skarels #include "vm/vm_statistics.h" 3645788Sbostic #include "vm/pmap.h" 3745788Sbostic #include "vm/vm_prot.h" 3845750Smckusick 3941480Smckusick extern int scsi_test_unit_rdy(); 4041480Smckusick extern int scsi_request_sense(); 4141480Smckusick extern int scsi_inquiry(); 4241480Smckusick extern int scsi_read_capacity(); 4341480Smckusick extern int scsi_tt_write(); 4441480Smckusick extern int scsireq(); 4541480Smckusick extern int scsiustart(); 4641480Smckusick extern int scsigo(); 4741480Smckusick extern void scsifree(); 4841480Smckusick extern void scsireset(); 4949304Shibler extern void scsi_delay(); 5041480Smckusick 5141480Smckusick extern void disksort(); 5241480Smckusick extern void biodone(); 5341480Smckusick extern int physio(); 5441480Smckusick extern void TBIS(); 5541480Smckusick 5641480Smckusick int sdinit(); 5741480Smckusick void sdstrategy(), sdstart(), sdustart(), sdgo(), sdintr(); 5841480Smckusick 5941480Smckusick struct driver sddriver = { 6041480Smckusick sdinit, "sd", (int (*)())sdstart, (int (*)())sdgo, (int (*)())sdintr, 6141480Smckusick }; 6241480Smckusick 6341480Smckusick struct size { 6441480Smckusick u_long strtblk; 6541480Smckusick u_long endblk; 6641480Smckusick int nblocks; 6741480Smckusick }; 6841480Smckusick 6941480Smckusick struct sdinfo { 7041480Smckusick struct size part[8]; 7141480Smckusick }; 7241480Smckusick 7341480Smckusick /* 7441480Smckusick * since the SCSI standard tends to hide the disk structure, we define 7541480Smckusick * partitions in terms of DEV_BSIZE blocks. The default partition table 7641480Smckusick * (for an unlabeled disk) reserves 512K for a boot area, has an 8 meg 7741480Smckusick * root and 32 meg of swap. The rest of the space on the drive goes in 7841480Smckusick * the G partition. As usual, the C partition covers the entire disk 7941480Smckusick * (including the boot area). 8041480Smckusick */ 8141480Smckusick struct sdinfo sddefaultpart = { 8241480Smckusick 1024, 17408, 16384 , /* A */ 8341480Smckusick 17408, 82944, 65536 , /* B */ 8441480Smckusick 0, 0, 0 , /* C */ 8541480Smckusick 17408, 115712, 98304 , /* D */ 8641480Smckusick 115712, 218112, 102400 , /* E */ 8741480Smckusick 218112, 0, 0 , /* F */ 8841480Smckusick 82944, 0, 0 , /* G */ 8941480Smckusick 115712, 0, 0 , /* H */ 9041480Smckusick }; 9141480Smckusick 9241480Smckusick struct sd_softc { 9341480Smckusick struct hp_device *sc_hd; 9441480Smckusick struct devqueue sc_dq; 9541480Smckusick int sc_format_pid; /* process using "format" mode */ 9641480Smckusick short sc_flags; 9741480Smckusick short sc_type; /* drive type */ 9841480Smckusick short sc_punit; /* physical unit (scsi lun) */ 9941480Smckusick u_short sc_bshift; /* convert device blocks to DEV_BSIZE blks */ 10041480Smckusick u_int sc_blks; /* number of blocks on device */ 10141480Smckusick int sc_blksize; /* device block size in bytes */ 10241480Smckusick u_int sc_wpms; /* average xfer rate in 16 bit wds/sec. */ 10341480Smckusick struct sdinfo sc_info; /* drive partition table & label info */ 10441480Smckusick } sd_softc[NSD]; 10541480Smckusick 10641480Smckusick /* sc_flags values */ 10741480Smckusick #define SDF_ALIVE 0x1 10841480Smckusick 10941480Smckusick #ifdef DEBUG 11041480Smckusick int sddebug = 1; 11141480Smckusick #define SDB_ERROR 0x01 11241480Smckusick #define SDB_PARTIAL 0x02 11341480Smckusick #endif 11441480Smckusick 11541480Smckusick struct sdstats { 11641480Smckusick long sdresets; 11741480Smckusick long sdtransfers; 11841480Smckusick long sdpartials; 11941480Smckusick } sdstats[NSD]; 12041480Smckusick 12141480Smckusick struct buf sdtab[NSD]; 12241480Smckusick struct scsi_fmt_cdb sdcmd[NSD]; 12341480Smckusick struct scsi_fmt_sense sdsense[NSD]; 12441480Smckusick 12541480Smckusick static struct scsi_fmt_cdb sd_read_cmd = { 10, CMD_READ_EXT }; 12641480Smckusick static struct scsi_fmt_cdb sd_write_cmd = { 10, CMD_WRITE_EXT }; 12741480Smckusick 12849132Skarels #define sdunit(x) (minor(x) >> 3) 12941480Smckusick #define sdpart(x) (minor(x) & 0x7) 13041480Smckusick #define sdpunit(x) ((x) & 7) 13141480Smckusick #define b_cylin b_resid 13245750Smckusick 13341480Smckusick #define SDRETRY 2 13441480Smckusick 13541480Smckusick /* 13641480Smckusick * Table of scsi commands users are allowed to access via "format" 13741480Smckusick * mode. 0 means not legal. 1 means "immediate" (doesn't need dma). 13841480Smckusick * -1 means needs dma and/or wait for intr. 13941480Smckusick */ 14041480Smckusick static char legal_cmds[256] = { 14141480Smckusick /***** 0 1 2 3 4 5 6 7 8 9 A B C D E F */ 14241480Smckusick /*00*/ 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14341480Smckusick /*10*/ 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 14441480Smckusick /*20*/ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14541480Smckusick /*30*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14641480Smckusick /*40*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14741480Smckusick /*50*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14841480Smckusick /*60*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14941480Smckusick /*70*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15041480Smckusick /*80*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15141480Smckusick /*90*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15241480Smckusick /*a0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15341480Smckusick /*b0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15441480Smckusick /*c0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15541480Smckusick /*d0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15641480Smckusick /*e0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15741480Smckusick /*f0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15841480Smckusick }; 15941480Smckusick 16041480Smckusick static struct scsi_inquiry inqbuf; 16141480Smckusick static struct scsi_fmt_cdb inq = { 16241480Smckusick 6, 16341480Smckusick CMD_INQUIRY, 0, 0, 0, sizeof(inqbuf), 0 16441480Smckusick }; 16541480Smckusick 16641480Smckusick static u_char capbuf[8]; 16741480Smckusick struct scsi_fmt_cdb cap = { 16841480Smckusick 10, 16941480Smckusick CMD_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 17041480Smckusick }; 17141480Smckusick 17241480Smckusick static int 17341480Smckusick sdident(sc, hd) 17441480Smckusick struct sd_softc *sc; 17541480Smckusick struct hp_device *hd; 17641480Smckusick { 17741480Smckusick int unit; 17841480Smckusick register int ctlr, slave; 17941480Smckusick register int i; 18041480Smckusick register int tries = 10; 18149304Shibler char idstr[32]; 18245750Smckusick int ismo = 0; 18341480Smckusick 18441480Smckusick ctlr = hd->hp_ctlr; 18541480Smckusick slave = hd->hp_slave; 18641480Smckusick unit = sc->sc_punit; 18749304Shibler scsi_delay(-1); 18841480Smckusick 18941480Smckusick /* 19041480Smckusick * See if unit exists and is a disk then read block size & nblocks. 19141480Smckusick */ 19241480Smckusick while ((i = scsi_test_unit_rdy(ctlr, slave, unit)) != 0) { 19345750Smckusick if (i == -1 || --tries < 0) { 19445750Smckusick if (ismo) 19545750Smckusick break; 19641480Smckusick /* doesn't exist or not a CCS device */ 19749304Shibler goto failed; 19845750Smckusick } 19941480Smckusick if (i == STS_CHECKCOND) { 20041480Smckusick u_char sensebuf[128]; 20141480Smckusick struct scsi_xsense *sp = (struct scsi_xsense *)sensebuf; 20241480Smckusick 20341480Smckusick scsi_request_sense(ctlr, slave, unit, sensebuf, 20441480Smckusick sizeof(sensebuf)); 20545750Smckusick if (sp->class == 7) 20645750Smckusick switch (sp->key) { 20745750Smckusick /* not ready -- might be MO with no media */ 20845750Smckusick case 2: 20945750Smckusick if (sp->len == 12 && 21045750Smckusick sensebuf[12] == 10) /* XXX */ 21145750Smckusick ismo = 1; 21245750Smckusick break; 21341480Smckusick /* drive doing an RTZ -- give it a while */ 21445750Smckusick case 6: 21545750Smckusick DELAY(1000000); 21645750Smckusick break; 21745750Smckusick default: 21845750Smckusick break; 21945750Smckusick } 22041480Smckusick } 22141480Smckusick DELAY(1000); 22241480Smckusick } 22345750Smckusick /* 22445750Smckusick * Find out about device 22545750Smckusick */ 22645750Smckusick if (scsi_immed_command(ctlr, slave, unit, &inq, 22745750Smckusick (u_char *)&inqbuf, sizeof(inqbuf), B_READ)) 22849304Shibler goto failed; 22941480Smckusick switch (inqbuf.type) { 23041480Smckusick case 0: /* disk */ 23141480Smckusick case 4: /* WORM */ 23241480Smckusick case 5: /* CD-ROM */ 23341480Smckusick case 7: /* Magneto-optical */ 23441480Smckusick break; 23541480Smckusick default: /* not a disk */ 23649304Shibler goto failed; 23741480Smckusick } 23845750Smckusick /* 23949304Shibler * Get a usable id string 24045750Smckusick */ 24149304Shibler if (inqbuf.version != 1) { 24249304Shibler bcopy("UNKNOWN", &idstr[0], 8); 24349304Shibler bcopy("DRIVE TYPE", &idstr[8], 11); 24449304Shibler } else { 24549304Shibler bcopy((caddr_t)&inqbuf.vendor_id, (caddr_t)idstr, 28); 24649304Shibler for (i = 27; i > 23; --i) 24749304Shibler if (idstr[i] != ' ') 24849304Shibler break; 24949304Shibler idstr[i+1] = 0; 25049304Shibler for (i = 23; i > 7; --i) 25149304Shibler if (idstr[i] != ' ') 25249304Shibler break; 25349304Shibler idstr[i+1] = 0; 25449304Shibler for (i = 7; i >= 0; --i) 25549304Shibler if (idstr[i] != ' ') 25649304Shibler break; 25749304Shibler idstr[i+1] = 0; 25845750Smckusick } 25945750Smckusick i = scsi_immed_command(ctlr, slave, unit, &cap, 26045750Smckusick (u_char *)&capbuf, sizeof(capbuf), B_READ); 26145750Smckusick if (i) { 26249304Shibler if (i != STS_CHECKCOND || 26349304Shibler bcmp(&idstr[0], "HP", 3) || 26449304Shibler bcmp(&idstr[8], "S6300.650A", 11)) 26549304Shibler goto failed; 26645750Smckusick /* XXX unformatted or non-existant MO media; fake it */ 26749304Shibler sc->sc_blks = 318664; 26849304Shibler sc->sc_blksize = 1024; 26945750Smckusick } else { 27045750Smckusick sc->sc_blks = *(u_int *)&capbuf[0]; 27145750Smckusick sc->sc_blksize = *(int *)&capbuf[4]; 27245750Smckusick } 27345750Smckusick /* return value of read capacity is last valid block number */ 27445750Smckusick sc->sc_blks++; 27545750Smckusick 27641480Smckusick if (inqbuf.version != 1) 27741480Smckusick printf("sd%d: type 0x%x, qual 0x%x, ver %d", hd->hp_unit, 27841480Smckusick inqbuf.type, inqbuf.qual, inqbuf.version); 27949304Shibler else 28041480Smckusick printf("sd%d: %s %s rev %s", hd->hp_unit, idstr, &idstr[8], 28141480Smckusick &idstr[24]); 28241480Smckusick printf(", %d %d byte blocks\n", sc->sc_blks, sc->sc_blksize); 28341480Smckusick if (sc->sc_blksize != DEV_BSIZE) { 28441480Smckusick if (sc->sc_blksize < DEV_BSIZE) { 28541480Smckusick printf("sd%d: need %d byte blocks - drive ignored\n", 28641480Smckusick unit, DEV_BSIZE); 28749304Shibler goto failed; 28841480Smckusick } 28941480Smckusick for (i = sc->sc_blksize; i > DEV_BSIZE; i >>= 1) 29041480Smckusick ++sc->sc_bshift; 29141480Smckusick sc->sc_blks <<= sc->sc_bshift; 29241480Smckusick } 29341480Smckusick sc->sc_wpms = 32 * (60 * DEV_BSIZE / 2); /* XXX */ 29449304Shibler scsi_delay(0); 29541480Smckusick return(inqbuf.type); 29649304Shibler failed: 29749304Shibler scsi_delay(0); 29849304Shibler return(-1); 29941480Smckusick } 30041480Smckusick 30141480Smckusick int 30241480Smckusick sdinit(hd) 30341480Smckusick register struct hp_device *hd; 30441480Smckusick { 30541480Smckusick register struct sd_softc *sc = &sd_softc[hd->hp_unit]; 30641480Smckusick 30741480Smckusick sc->sc_hd = hd; 30841480Smckusick sc->sc_punit = sdpunit(hd->hp_flags); 30941480Smckusick sc->sc_type = sdident(sc, hd); 31041480Smckusick if (sc->sc_type < 0) 31141480Smckusick return(0); 31241480Smckusick sc->sc_dq.dq_ctlr = hd->hp_ctlr; 31341480Smckusick sc->sc_dq.dq_unit = hd->hp_unit; 31441480Smckusick sc->sc_dq.dq_slave = hd->hp_slave; 31541480Smckusick sc->sc_dq.dq_driver = &sddriver; 31641480Smckusick 31741480Smckusick /* 31841480Smckusick * If we don't have a disk label, build a default partition 31941480Smckusick * table with 'standard' size root & swap and everything else 32041480Smckusick * in the G partition. 32141480Smckusick */ 32241480Smckusick sc->sc_info = sddefaultpart; 32341480Smckusick /* C gets everything */ 32441480Smckusick sc->sc_info.part[2].nblocks = sc->sc_blks; 32541480Smckusick sc->sc_info.part[2].endblk = sc->sc_blks; 32641480Smckusick /* G gets from end of B to end of disk */ 32741480Smckusick sc->sc_info.part[6].nblocks = sc->sc_blks - sc->sc_info.part[1].endblk; 32841480Smckusick sc->sc_info.part[6].endblk = sc->sc_blks; 32941480Smckusick /* 33041480Smckusick * We also define the D, E and F paritions as an alternative to 33141480Smckusick * B and G. D is 48Mb, starts after A and is intended for swapping. 33241480Smckusick * E is 50Mb, starts after D and is intended for /usr. F starts 33341480Smckusick * after E and is what ever is left. 33441480Smckusick */ 33541480Smckusick if (sc->sc_blks >= sc->sc_info.part[4].endblk) { 33641480Smckusick sc->sc_info.part[5].nblocks = 33741480Smckusick sc->sc_blks - sc->sc_info.part[4].endblk; 33841480Smckusick sc->sc_info.part[5].endblk = sc->sc_blks; 33941480Smckusick } else { 34041480Smckusick sc->sc_info.part[5].strtblk = 0; 34141480Smckusick sc->sc_info.part[3] = sc->sc_info.part[5]; 34241480Smckusick sc->sc_info.part[4] = sc->sc_info.part[5]; 34341480Smckusick } 34441480Smckusick /* 34541480Smckusick * H is a single partition alternative to E and F. 34641480Smckusick */ 34741480Smckusick if (sc->sc_blks >= sc->sc_info.part[3].endblk) { 34841480Smckusick sc->sc_info.part[7].nblocks = 34941480Smckusick sc->sc_blks - sc->sc_info.part[3].endblk; 35041480Smckusick sc->sc_info.part[7].endblk = sc->sc_blks; 35141480Smckusick } else { 35241480Smckusick sc->sc_info.part[7].strtblk = 0; 35341480Smckusick } 35441480Smckusick 35541480Smckusick sc->sc_flags = SDF_ALIVE; 35641480Smckusick return(1); 35741480Smckusick } 35841480Smckusick 35941480Smckusick void 36041480Smckusick sdreset(sc, hd) 36141480Smckusick register struct sd_softc *sc; 36241480Smckusick register struct hp_device *hd; 36341480Smckusick { 36441480Smckusick sdstats[hd->hp_unit].sdresets++; 36541480Smckusick } 36641480Smckusick 36741480Smckusick int 36849132Skarels sdopen(dev, flags, mode, p) 36941480Smckusick dev_t dev; 37049132Skarels int flags, mode; 37149132Skarels struct proc *p; 37241480Smckusick { 37341480Smckusick register int unit = sdunit(dev); 37441480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 37541480Smckusick 37641480Smckusick if (unit >= NSD) 37741480Smckusick return(ENXIO); 37849132Skarels if ((sc->sc_flags & SDF_ALIVE) == 0 && suser(p->p_ucred, &p->p_acflag)) 37941480Smckusick return(ENXIO); 38041480Smckusick 38141480Smckusick if (sc->sc_hd->hp_dk >= 0) 38241480Smckusick dk_wpms[sc->sc_hd->hp_dk] = sc->sc_wpms; 38341480Smckusick return(0); 38441480Smckusick } 38541480Smckusick 38641480Smckusick /* 38741480Smckusick * This routine is called for partial block transfers and non-aligned 38841480Smckusick * transfers (the latter only being possible on devices with a block size 38941480Smckusick * larger than DEV_BSIZE). The operation is performed in three steps 39041480Smckusick * using a locally allocated buffer: 39141480Smckusick * 1. transfer any initial partial block 39241480Smckusick * 2. transfer full blocks 39341480Smckusick * 3. transfer any final partial block 39441480Smckusick */ 39541480Smckusick static void 39641480Smckusick sdlblkstrat(bp, bsize) 39741480Smckusick register struct buf *bp; 39841480Smckusick register int bsize; 39941480Smckusick { 40041480Smckusick register struct buf *cbp = (struct buf *)malloc(sizeof(struct buf), 40141480Smckusick M_DEVBUF, M_WAITOK); 40241480Smckusick caddr_t cbuf = (caddr_t)malloc(bsize, M_DEVBUF, M_WAITOK); 40341480Smckusick register int bn, resid; 40441480Smckusick register caddr_t addr; 40541480Smckusick 40641480Smckusick bzero((caddr_t)cbp, sizeof(*cbp)); 40749132Skarels cbp->b_proc = curproc; /* XXX */ 40841480Smckusick cbp->b_dev = bp->b_dev; 40941480Smckusick bn = bp->b_blkno; 41041480Smckusick resid = bp->b_bcount; 41141480Smckusick addr = bp->b_un.b_addr; 41241480Smckusick #ifdef DEBUG 41341480Smckusick if (sddebug & SDB_PARTIAL) 41441480Smckusick printf("sdlblkstrat: bp %x flags %x bn %x resid %x addr %x\n", 41541480Smckusick bp, bp->b_flags, bn, resid, addr); 41641480Smckusick #endif 41741480Smckusick 41841480Smckusick while (resid > 0) { 41941480Smckusick register int boff = dbtob(bn) & (bsize - 1); 42041480Smckusick register int count; 42141480Smckusick 42241480Smckusick if (boff || resid < bsize) { 42341480Smckusick sdstats[sdunit(bp->b_dev)].sdpartials++; 42441480Smckusick count = MIN(resid, bsize - boff); 42541480Smckusick cbp->b_flags = B_BUSY | B_PHYS | B_READ; 42641480Smckusick cbp->b_blkno = bn - btodb(boff); 42741480Smckusick cbp->b_un.b_addr = cbuf; 42841480Smckusick cbp->b_bcount = bsize; 42941480Smckusick #ifdef DEBUG 43041480Smckusick if (sddebug & SDB_PARTIAL) 43141480Smckusick printf(" readahead: bn %x cnt %x off %x addr %x\n", 43241480Smckusick cbp->b_blkno, count, boff, addr); 43341480Smckusick #endif 43441480Smckusick sdstrategy(cbp); 43541480Smckusick biowait(cbp); 43641480Smckusick if (cbp->b_flags & B_ERROR) { 43741480Smckusick bp->b_flags |= B_ERROR; 43841480Smckusick bp->b_error = cbp->b_error; 43941480Smckusick break; 44041480Smckusick } 44141480Smckusick if (bp->b_flags & B_READ) { 44241480Smckusick bcopy(&cbuf[boff], addr, count); 44341480Smckusick goto done; 44441480Smckusick } 44541480Smckusick bcopy(addr, &cbuf[boff], count); 44641480Smckusick #ifdef DEBUG 44741480Smckusick if (sddebug & SDB_PARTIAL) 44841480Smckusick printf(" writeback: bn %x cnt %x off %x addr %x\n", 44941480Smckusick cbp->b_blkno, count, boff, addr); 45041480Smckusick #endif 45141480Smckusick } else { 45241480Smckusick count = resid & ~(bsize - 1); 45341480Smckusick cbp->b_blkno = bn; 45441480Smckusick cbp->b_un.b_addr = addr; 45541480Smckusick cbp->b_bcount = count; 45641480Smckusick #ifdef DEBUG 45741480Smckusick if (sddebug & SDB_PARTIAL) 45841480Smckusick printf(" fulltrans: bn %x cnt %x addr %x\n", 45941480Smckusick cbp->b_blkno, count, addr); 46041480Smckusick #endif 46141480Smckusick } 46241480Smckusick cbp->b_flags = B_BUSY | B_PHYS | (bp->b_flags & B_READ); 46341480Smckusick sdstrategy(cbp); 46441480Smckusick biowait(cbp); 46541480Smckusick if (cbp->b_flags & B_ERROR) { 46641480Smckusick bp->b_flags |= B_ERROR; 46741480Smckusick bp->b_error = cbp->b_error; 46841480Smckusick break; 46941480Smckusick } 47041480Smckusick done: 47141480Smckusick bn += btodb(count); 47241480Smckusick resid -= count; 47341480Smckusick addr += count; 47441480Smckusick #ifdef DEBUG 47541480Smckusick if (sddebug & SDB_PARTIAL) 47641480Smckusick printf(" done: bn %x resid %x addr %x\n", 47741480Smckusick bn, resid, addr); 47841480Smckusick #endif 47941480Smckusick } 48041480Smckusick free(cbuf, M_DEVBUF); 48141480Smckusick free(cbp, M_DEVBUF); 48241480Smckusick } 48341480Smckusick 48441480Smckusick void 48541480Smckusick sdstrategy(bp) 48641480Smckusick register struct buf *bp; 48741480Smckusick { 48841480Smckusick register int unit = sdunit(bp->b_dev); 48941480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 49045750Smckusick register struct size *pinfo = &sc->sc_info.part[sdpart(bp->b_dev)]; 49141480Smckusick register struct buf *dp = &sdtab[unit]; 49245750Smckusick register daddr_t bn; 49345750Smckusick register int sz, s; 49441480Smckusick 49541480Smckusick if (sc->sc_format_pid) { 49649132Skarels if (sc->sc_format_pid != curproc->p_pid) { /* XXX */ 49741480Smckusick bp->b_error = EPERM; 49845750Smckusick bp->b_flags |= B_ERROR; 49945750Smckusick goto done; 50041480Smckusick } 50141480Smckusick bp->b_cylin = 0; 50241480Smckusick } else { 50341480Smckusick bn = bp->b_blkno; 50445750Smckusick sz = howmany(bp->b_bcount, DEV_BSIZE); 50545750Smckusick if (bn < 0 || bn + sz > pinfo->nblocks) { 50645750Smckusick sz = pinfo->nblocks - bn; 50745750Smckusick if (sz == 0) { 50841480Smckusick bp->b_resid = bp->b_bcount; 50941480Smckusick goto done; 51041480Smckusick } 51145750Smckusick if (sz < 0) { 51245750Smckusick bp->b_error = EINVAL; 51345750Smckusick bp->b_flags |= B_ERROR; 51445750Smckusick goto done; 51545750Smckusick } 51645750Smckusick bp->b_bcount = dbtob(sz); 51741480Smckusick } 51841480Smckusick /* 51941480Smckusick * Non-aligned or partial-block transfers handled specially. 52041480Smckusick */ 52141480Smckusick s = sc->sc_blksize - 1; 52241480Smckusick if ((dbtob(bn) & s) || (bp->b_bcount & s)) { 52341480Smckusick sdlblkstrat(bp, sc->sc_blksize); 52441480Smckusick goto done; 52541480Smckusick } 52645750Smckusick bp->b_cylin = (bn + pinfo->strtblk) >> sc->sc_bshift; 52741480Smckusick } 52841480Smckusick s = splbio(); 52941480Smckusick disksort(dp, bp); 53041480Smckusick if (dp->b_active == 0) { 53141480Smckusick dp->b_active = 1; 53241480Smckusick sdustart(unit); 53341480Smckusick } 53441480Smckusick splx(s); 53541480Smckusick return; 53641480Smckusick done: 53745750Smckusick biodone(bp); 53841480Smckusick } 53941480Smckusick 54041480Smckusick void 54141480Smckusick sdustart(unit) 54241480Smckusick register int unit; 54341480Smckusick { 54441480Smckusick if (scsireq(&sd_softc[unit].sc_dq)) 54541480Smckusick sdstart(unit); 54641480Smckusick } 54741480Smckusick 548*50039Skarels /* 549*50039Skarels * Return: 550*50039Skarels * 0 if not really an error 551*50039Skarels * <0 if we should do a retry 552*50039Skarels * >0 if a fatal error 553*50039Skarels */ 55445750Smckusick static int 55541480Smckusick sderror(unit, sc, hp, stat) 55641480Smckusick int unit, stat; 55741480Smckusick register struct sd_softc *sc; 55841480Smckusick register struct hp_device *hp; 55941480Smckusick { 560*50039Skarels int cond = 1; 56145750Smckusick 56241480Smckusick sdsense[unit].status = stat; 56341480Smckusick if (stat & STS_CHECKCOND) { 56441480Smckusick struct scsi_xsense *sp; 56541480Smckusick 56641480Smckusick scsi_request_sense(hp->hp_ctlr, hp->hp_slave, 56741480Smckusick sc->sc_punit, sdsense[unit].sense, 56841480Smckusick sizeof(sdsense[unit].sense)); 56941480Smckusick sp = (struct scsi_xsense *)sdsense[unit].sense; 57041480Smckusick printf("sd%d: scsi sense class %d, code %d", unit, 57141480Smckusick sp->class, sp->code); 57241480Smckusick if (sp->class == 7) { 57341480Smckusick printf(", key %d", sp->key); 57441480Smckusick if (sp->valid) 57541480Smckusick printf(", blk %d", *(int *)&sp->info1); 576*50039Skarels switch (sp->key) { 577*50039Skarels /* no sense, try again */ 578*50039Skarels case 0: 579*50039Skarels cond = -1; 580*50039Skarels break; 581*50039Skarels /* recovered error, not a problem */ 582*50039Skarels case 1: 583*50039Skarels cond = 0; 584*50039Skarels break; 585*50039Skarels } 58641480Smckusick } 58741480Smckusick printf("\n"); 58841480Smckusick } 589*50039Skarels return(cond); 59041480Smckusick } 59141480Smckusick 59241480Smckusick static void 59341480Smckusick sdfinish(unit, sc, bp) 59441480Smckusick int unit; 59541480Smckusick register struct sd_softc *sc; 59641480Smckusick register struct buf *bp; 59741480Smckusick { 59841480Smckusick sdtab[unit].b_errcnt = 0; 59941480Smckusick sdtab[unit].b_actf = bp->b_actf; 60041480Smckusick bp->b_resid = 0; 60145750Smckusick biodone(bp); 60241480Smckusick scsifree(&sc->sc_dq); 60341480Smckusick if (sdtab[unit].b_actf) 60441480Smckusick sdustart(unit); 60541480Smckusick else 60641480Smckusick sdtab[unit].b_active = 0; 60741480Smckusick } 60841480Smckusick 60941480Smckusick void 61041480Smckusick sdstart(unit) 61141480Smckusick register int unit; 61241480Smckusick { 61341480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 61441480Smckusick register struct hp_device *hp = sc->sc_hd; 61541480Smckusick 61641480Smckusick /* 61741480Smckusick * we have the SCSI bus -- in format mode, we may or may not need dma 61841480Smckusick * so check now. 61941480Smckusick */ 62041480Smckusick if (sc->sc_format_pid && legal_cmds[sdcmd[unit].cdb[0]] > 0) { 62141480Smckusick register struct buf *bp = sdtab[unit].b_actf; 62241480Smckusick register int sts; 62341480Smckusick 62441480Smckusick sts = scsi_immed_command(hp->hp_ctlr, hp->hp_slave, 62541480Smckusick sc->sc_punit, &sdcmd[unit], 62641480Smckusick bp->b_un.b_addr, bp->b_bcount, 62741480Smckusick bp->b_flags & B_READ); 62841480Smckusick sdsense[unit].status = sts; 62941480Smckusick if (sts & 0xfe) { 63045750Smckusick (void) sderror(unit, sc, hp, sts); 63141480Smckusick bp->b_flags |= B_ERROR; 63241480Smckusick bp->b_error = EIO; 63341480Smckusick } 63441480Smckusick sdfinish(unit, sc, bp); 63541480Smckusick 63641480Smckusick } else if (scsiustart(hp->hp_ctlr)) 63741480Smckusick sdgo(unit); 63841480Smckusick } 63941480Smckusick 64041480Smckusick void 64141480Smckusick sdgo(unit) 64241480Smckusick register int unit; 64341480Smckusick { 64441480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 64541480Smckusick register struct hp_device *hp = sc->sc_hd; 64641480Smckusick register struct buf *bp = sdtab[unit].b_actf; 64741480Smckusick register int pad; 64841480Smckusick register struct scsi_fmt_cdb *cmd; 64941480Smckusick 65041480Smckusick if (sc->sc_format_pid) { 65141480Smckusick cmd = &sdcmd[unit]; 65241480Smckusick pad = 0; 65341480Smckusick } else { 65441480Smckusick cmd = bp->b_flags & B_READ? &sd_read_cmd : &sd_write_cmd; 65541480Smckusick *(int *)(&cmd->cdb[2]) = bp->b_cylin; 65641480Smckusick pad = howmany(bp->b_bcount, sc->sc_blksize); 65741480Smckusick *(u_short *)(&cmd->cdb[7]) = pad; 65841480Smckusick pad = (bp->b_bcount & (sc->sc_blksize - 1)) != 0; 65941480Smckusick #ifdef DEBUG 66041480Smckusick if (pad) 66141480Smckusick printf("sd%d: partial block xfer -- %x bytes\n", 66241480Smckusick unit, bp->b_bcount); 66341480Smckusick #endif 66441480Smckusick sdstats[unit].sdtransfers++; 66541480Smckusick } 66641480Smckusick if (scsigo(hp->hp_ctlr, hp->hp_slave, sc->sc_punit, bp, cmd, pad) == 0) { 66741480Smckusick if (hp->hp_dk >= 0) { 66841480Smckusick dk_busy |= 1 << hp->hp_dk; 66941480Smckusick ++dk_seek[hp->hp_dk]; 67041480Smckusick ++dk_xfer[hp->hp_dk]; 67141480Smckusick dk_wds[hp->hp_dk] += bp->b_bcount >> 6; 67241480Smckusick } 67341480Smckusick return; 67441480Smckusick } 67541480Smckusick #ifdef DEBUG 67641480Smckusick if (sddebug & SDB_ERROR) 67741480Smckusick printf("sd%d: sdstart: %s adr %d blk %d len %d ecnt %d\n", 67841480Smckusick unit, bp->b_flags & B_READ? "read" : "write", 67941480Smckusick bp->b_un.b_addr, bp->b_cylin, bp->b_bcount, 68041480Smckusick sdtab[unit].b_errcnt); 68141480Smckusick #endif 68241480Smckusick bp->b_flags |= B_ERROR; 68341480Smckusick bp->b_error = EIO; 68441480Smckusick sdfinish(unit, sc, bp); 68541480Smckusick } 68641480Smckusick 68741480Smckusick void 68841480Smckusick sdintr(unit, stat) 68941480Smckusick register int unit; 69041480Smckusick int stat; 69141480Smckusick { 69241480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 69341480Smckusick register struct buf *bp = sdtab[unit].b_actf; 69441480Smckusick register struct hp_device *hp = sc->sc_hd; 695*50039Skarels int cond; 69641480Smckusick 69741480Smckusick if (bp == NULL) { 69841480Smckusick printf("sd%d: bp == NULL\n", unit); 69941480Smckusick return; 70041480Smckusick } 70141480Smckusick if (hp->hp_dk >= 0) 70241480Smckusick dk_busy &=~ (1 << hp->hp_dk); 70341480Smckusick if (stat) { 70441480Smckusick #ifdef DEBUG 70541480Smckusick if (sddebug & SDB_ERROR) 70641480Smckusick printf("sd%d: sdintr: bad scsi status 0x%x\n", 70741480Smckusick unit, stat); 70841480Smckusick #endif 709*50039Skarels cond = sderror(unit, sc, hp, stat); 710*50039Skarels if (cond) { 711*50039Skarels if (cond < 0 && sdtab[unit].b_errcnt++ < SDRETRY) { 71250038Skarels #ifdef DEBUG 713*50039Skarels if (sddebug & SDB_ERROR) 714*50039Skarels printf("sd%d: retry #%d\n", 715*50039Skarels unit, sdtab[unit].b_errcnt); 71650038Skarels #endif 717*50039Skarels sdstart(unit); 718*50039Skarels return; 719*50039Skarels } 720*50039Skarels bp->b_flags |= B_ERROR; 721*50039Skarels bp->b_error = EIO; 72245750Smckusick } 72341480Smckusick } 72441480Smckusick sdfinish(unit, sc, bp); 72541480Smckusick } 72641480Smckusick 72741480Smckusick int 72849132Skarels sdread(dev, uio, flags) 72941480Smckusick dev_t dev; 73041480Smckusick struct uio *uio; 73149132Skarels int flags; 73241480Smckusick { 73341480Smckusick register int unit = sdunit(dev); 73441480Smckusick register int pid; 73541480Smckusick 73649132Skarels if ((pid = sd_softc[unit].sc_format_pid) && 73749132Skarels pid != uio->uio_procp->p_pid) 73841480Smckusick return (EPERM); 73941480Smckusick 74049132Skarels return (physio(sdstrategy, NULL, dev, B_READ, minphys, uio)); 74141480Smckusick } 74241480Smckusick 74341480Smckusick int 74449132Skarels sdwrite(dev, uio, flags) 74541480Smckusick dev_t dev; 74641480Smckusick struct uio *uio; 74749132Skarels int flags; 74841480Smckusick { 74941480Smckusick register int unit = sdunit(dev); 75041480Smckusick register int pid; 75141480Smckusick 75249132Skarels if ((pid = sd_softc[unit].sc_format_pid) && 75349132Skarels pid != uio->uio_procp->p_pid) 75441480Smckusick return (EPERM); 75541480Smckusick 75649132Skarels return (physio(sdstrategy, NULL, dev, B_WRITE, minphys, uio)); 75741480Smckusick } 75841480Smckusick 75941480Smckusick int 76049132Skarels sdioctl(dev, cmd, data, flag, p) 76141480Smckusick dev_t dev; 76241480Smckusick int cmd; 76341480Smckusick caddr_t data; 76441480Smckusick int flag; 76549132Skarels struct proc *p; 76641480Smckusick { 76741480Smckusick register int unit = sdunit(dev); 76841480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 76941480Smckusick 77041480Smckusick switch (cmd) { 77141480Smckusick default: 77241480Smckusick return (EINVAL); 77341480Smckusick 77441480Smckusick case SDIOCSFORMAT: 77541480Smckusick /* take this device into or out of "format" mode */ 77649132Skarels if (suser(p->p_ucred, &p->p_acflag)) 77741480Smckusick return(EPERM); 77841480Smckusick 77941480Smckusick if (*(int *)data) { 78041480Smckusick if (sc->sc_format_pid) 78141480Smckusick return (EPERM); 78249132Skarels sc->sc_format_pid = p->p_pid; 78341480Smckusick } else 78441480Smckusick sc->sc_format_pid = 0; 78541480Smckusick return (0); 78641480Smckusick 78741480Smckusick case SDIOCGFORMAT: 78841480Smckusick /* find out who has the device in format mode */ 78941480Smckusick *(int *)data = sc->sc_format_pid; 79041480Smckusick return (0); 79141480Smckusick 79241480Smckusick case SDIOCSCSICOMMAND: 79341480Smckusick /* 79441480Smckusick * Save what user gave us as SCSI cdb to use with next 79541480Smckusick * read or write to the char device. 79641480Smckusick */ 79749132Skarels if (sc->sc_format_pid != p->p_pid) 79841480Smckusick return (EPERM); 79941480Smckusick if (legal_cmds[((struct scsi_fmt_cdb *)data)->cdb[0]] == 0) 80041480Smckusick return (EINVAL); 80141480Smckusick bcopy(data, (caddr_t)&sdcmd[unit], sizeof(sdcmd[0])); 80241480Smckusick return (0); 80341480Smckusick 80441480Smckusick case SDIOCSENSE: 80541480Smckusick /* 80641480Smckusick * return the SCSI sense data saved after the last 80741480Smckusick * operation that completed with "check condition" status. 80841480Smckusick */ 80941480Smckusick bcopy((caddr_t)&sdsense[unit], data, sizeof(sdsense[0])); 81041480Smckusick return (0); 81141480Smckusick 81241480Smckusick } 81341480Smckusick /*NOTREACHED*/ 81441480Smckusick } 81541480Smckusick 81641480Smckusick int 81741480Smckusick sdsize(dev) 81841480Smckusick dev_t dev; 81941480Smckusick { 82041480Smckusick register int unit = sdunit(dev); 82141480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 82241480Smckusick 82341480Smckusick if (unit >= NSD || (sc->sc_flags & SDF_ALIVE) == 0) 82441480Smckusick return(-1); 82541480Smckusick 82641480Smckusick return(sc->sc_info.part[sdpart(dev)].nblocks); 82741480Smckusick } 82841480Smckusick 82941480Smckusick /* 83041480Smckusick * Non-interrupt driven, non-dma dump routine. 83141480Smckusick */ 83241480Smckusick int 83341480Smckusick sddump(dev) 83441480Smckusick dev_t dev; 83541480Smckusick { 83641480Smckusick int part = sdpart(dev); 83741480Smckusick int unit = sdunit(dev); 83841480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 83941480Smckusick register struct hp_device *hp = sc->sc_hd; 84041480Smckusick register daddr_t baddr; 84141480Smckusick register int maddr; 84241480Smckusick register int pages, i; 84341480Smckusick int stat; 84441480Smckusick extern int lowram; 84541480Smckusick 84641480Smckusick /* 84741480Smckusick * Hmm... all vax drivers dump maxfree pages which is physmem minus 84841480Smckusick * the message buffer. Is there a reason for not dumping the 84941480Smckusick * message buffer? Savecore expects to read 'dumpsize' pages of 85041480Smckusick * dump, where dumpsys() sets dumpsize to physmem! 85141480Smckusick */ 85241480Smckusick pages = physmem; 85341480Smckusick 85441480Smckusick /* is drive ok? */ 85541480Smckusick if (unit >= NSD || (sc->sc_flags & SDF_ALIVE) == 0) 85641480Smckusick return (ENXIO); 85741480Smckusick /* dump parameters in range? */ 85841480Smckusick if (dumplo < 0 || dumplo >= sc->sc_info.part[part].nblocks) 85941480Smckusick return (EINVAL); 86041480Smckusick if (dumplo + ctod(pages) > sc->sc_info.part[part].nblocks) 86141480Smckusick pages = dtoc(sc->sc_info.part[part].nblocks - dumplo); 86241480Smckusick maddr = lowram; 86341480Smckusick baddr = dumplo + sc->sc_info.part[part].strtblk; 86441480Smckusick /* scsi bus idle? */ 86541480Smckusick if (!scsireq(&sc->sc_dq)) { 86641480Smckusick scsireset(hp->hp_ctlr); 86741480Smckusick sdreset(sc, sc->sc_hd); 86841480Smckusick printf("[ drive %d reset ] ", unit); 86941480Smckusick } 87041480Smckusick for (i = 0; i < pages; i++) { 87141480Smckusick #define NPGMB (1024*1024/NBPG) 87241480Smckusick /* print out how many Mbs we have dumped */ 87341480Smckusick if (i && (i % NPGMB) == 0) 87441480Smckusick printf("%d ", i / NPGMB); 87541480Smckusick #undef NPBMG 87645750Smckusick pmap_enter(pmap_kernel(), vmmap, maddr, VM_PROT_READ, TRUE); 87741480Smckusick stat = scsi_tt_write(hp->hp_ctlr, hp->hp_slave, sc->sc_punit, 87841480Smckusick vmmap, NBPG, baddr, sc->sc_bshift); 87941480Smckusick if (stat) { 88041480Smckusick printf("sddump: scsi write error 0x%x\n", stat); 88141480Smckusick return (EIO); 88241480Smckusick } 88341480Smckusick maddr += NBPG; 88441480Smckusick baddr += ctod(1); 88541480Smckusick } 88641480Smckusick return (0); 88741480Smckusick } 88841480Smckusick #endif 889