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*49304Shibler * @(#)sd.c 7.6 (Berkeley) 05/07/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 20*49304Shibler 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(); 49*49304Shibler 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; 181*49304Shibler char idstr[32]; 18245750Smckusick int ismo = 0; 18341480Smckusick 18441480Smckusick ctlr = hd->hp_ctlr; 18541480Smckusick slave = hd->hp_slave; 18641480Smckusick unit = sc->sc_punit; 187*49304Shibler 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 */ 197*49304Shibler 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)) 228*49304Shibler 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 */ 236*49304Shibler goto failed; 23741480Smckusick } 23845750Smckusick /* 239*49304Shibler * Get a usable id string 24045750Smckusick */ 241*49304Shibler if (inqbuf.version != 1) { 242*49304Shibler bcopy("UNKNOWN", &idstr[0], 8); 243*49304Shibler bcopy("DRIVE TYPE", &idstr[8], 11); 244*49304Shibler } else { 245*49304Shibler bcopy((caddr_t)&inqbuf.vendor_id, (caddr_t)idstr, 28); 246*49304Shibler for (i = 27; i > 23; --i) 247*49304Shibler if (idstr[i] != ' ') 248*49304Shibler break; 249*49304Shibler idstr[i+1] = 0; 250*49304Shibler for (i = 23; i > 7; --i) 251*49304Shibler if (idstr[i] != ' ') 252*49304Shibler break; 253*49304Shibler idstr[i+1] = 0; 254*49304Shibler for (i = 7; i >= 0; --i) 255*49304Shibler if (idstr[i] != ' ') 256*49304Shibler break; 257*49304Shibler 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) { 262*49304Shibler if (i != STS_CHECKCOND || 263*49304Shibler bcmp(&idstr[0], "HP", 3) || 264*49304Shibler bcmp(&idstr[8], "S6300.650A", 11)) 265*49304Shibler goto failed; 26645750Smckusick /* XXX unformatted or non-existant MO media; fake it */ 267*49304Shibler sc->sc_blks = 318664; 268*49304Shibler 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); 279*49304Shibler 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); 287*49304Shibler 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 */ 294*49304Shibler scsi_delay(0); 29541480Smckusick return(inqbuf.type); 296*49304Shibler failed: 297*49304Shibler scsi_delay(0); 298*49304Shibler 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 54845750Smckusick static int 54941480Smckusick sderror(unit, sc, hp, stat) 55041480Smckusick int unit, stat; 55141480Smckusick register struct sd_softc *sc; 55241480Smckusick register struct hp_device *hp; 55341480Smckusick { 55445750Smckusick int retry = 0; 55545750Smckusick 55641480Smckusick sdsense[unit].status = stat; 55741480Smckusick if (stat & STS_CHECKCOND) { 55841480Smckusick struct scsi_xsense *sp; 55941480Smckusick 56041480Smckusick scsi_request_sense(hp->hp_ctlr, hp->hp_slave, 56141480Smckusick sc->sc_punit, sdsense[unit].sense, 56241480Smckusick sizeof(sdsense[unit].sense)); 56341480Smckusick sp = (struct scsi_xsense *)sdsense[unit].sense; 56441480Smckusick printf("sd%d: scsi sense class %d, code %d", unit, 56541480Smckusick sp->class, sp->code); 56641480Smckusick if (sp->class == 7) { 56741480Smckusick printf(", key %d", sp->key); 56841480Smckusick if (sp->valid) 56941480Smckusick printf(", blk %d", *(int *)&sp->info1); 57045750Smckusick /* no sense or recovered error, try again */ 57145750Smckusick if (sp->key == 0 || sp->key == 1) 57245750Smckusick retry = 1; 57341480Smckusick } 57441480Smckusick printf("\n"); 57541480Smckusick } 57645750Smckusick return(retry); 57741480Smckusick } 57841480Smckusick 57941480Smckusick static void 58041480Smckusick sdfinish(unit, sc, bp) 58141480Smckusick int unit; 58241480Smckusick register struct sd_softc *sc; 58341480Smckusick register struct buf *bp; 58441480Smckusick { 58541480Smckusick sdtab[unit].b_errcnt = 0; 58641480Smckusick sdtab[unit].b_actf = bp->b_actf; 58741480Smckusick bp->b_resid = 0; 58845750Smckusick biodone(bp); 58941480Smckusick scsifree(&sc->sc_dq); 59041480Smckusick if (sdtab[unit].b_actf) 59141480Smckusick sdustart(unit); 59241480Smckusick else 59341480Smckusick sdtab[unit].b_active = 0; 59441480Smckusick } 59541480Smckusick 59641480Smckusick void 59741480Smckusick sdstart(unit) 59841480Smckusick register int unit; 59941480Smckusick { 60041480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 60141480Smckusick register struct hp_device *hp = sc->sc_hd; 60241480Smckusick 60341480Smckusick /* 60441480Smckusick * we have the SCSI bus -- in format mode, we may or may not need dma 60541480Smckusick * so check now. 60641480Smckusick */ 60741480Smckusick if (sc->sc_format_pid && legal_cmds[sdcmd[unit].cdb[0]] > 0) { 60841480Smckusick register struct buf *bp = sdtab[unit].b_actf; 60941480Smckusick register int sts; 61041480Smckusick 61141480Smckusick sts = scsi_immed_command(hp->hp_ctlr, hp->hp_slave, 61241480Smckusick sc->sc_punit, &sdcmd[unit], 61341480Smckusick bp->b_un.b_addr, bp->b_bcount, 61441480Smckusick bp->b_flags & B_READ); 61541480Smckusick sdsense[unit].status = sts; 61641480Smckusick if (sts & 0xfe) { 61745750Smckusick (void) sderror(unit, sc, hp, sts); 61841480Smckusick bp->b_flags |= B_ERROR; 61941480Smckusick bp->b_error = EIO; 62041480Smckusick } 62141480Smckusick sdfinish(unit, sc, bp); 62241480Smckusick 62341480Smckusick } else if (scsiustart(hp->hp_ctlr)) 62441480Smckusick sdgo(unit); 62541480Smckusick } 62641480Smckusick 62741480Smckusick void 62841480Smckusick sdgo(unit) 62941480Smckusick register int unit; 63041480Smckusick { 63141480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 63241480Smckusick register struct hp_device *hp = sc->sc_hd; 63341480Smckusick register struct buf *bp = sdtab[unit].b_actf; 63441480Smckusick register int pad; 63541480Smckusick register struct scsi_fmt_cdb *cmd; 63641480Smckusick 63741480Smckusick if (sc->sc_format_pid) { 63841480Smckusick cmd = &sdcmd[unit]; 63941480Smckusick pad = 0; 64041480Smckusick } else { 64141480Smckusick cmd = bp->b_flags & B_READ? &sd_read_cmd : &sd_write_cmd; 64241480Smckusick *(int *)(&cmd->cdb[2]) = bp->b_cylin; 64341480Smckusick pad = howmany(bp->b_bcount, sc->sc_blksize); 64441480Smckusick *(u_short *)(&cmd->cdb[7]) = pad; 64541480Smckusick pad = (bp->b_bcount & (sc->sc_blksize - 1)) != 0; 64641480Smckusick #ifdef DEBUG 64741480Smckusick if (pad) 64841480Smckusick printf("sd%d: partial block xfer -- %x bytes\n", 64941480Smckusick unit, bp->b_bcount); 65041480Smckusick #endif 65141480Smckusick sdstats[unit].sdtransfers++; 65241480Smckusick } 65341480Smckusick if (scsigo(hp->hp_ctlr, hp->hp_slave, sc->sc_punit, bp, cmd, pad) == 0) { 65441480Smckusick if (hp->hp_dk >= 0) { 65541480Smckusick dk_busy |= 1 << hp->hp_dk; 65641480Smckusick ++dk_seek[hp->hp_dk]; 65741480Smckusick ++dk_xfer[hp->hp_dk]; 65841480Smckusick dk_wds[hp->hp_dk] += bp->b_bcount >> 6; 65941480Smckusick } 66041480Smckusick return; 66141480Smckusick } 66241480Smckusick #ifdef DEBUG 66341480Smckusick if (sddebug & SDB_ERROR) 66441480Smckusick printf("sd%d: sdstart: %s adr %d blk %d len %d ecnt %d\n", 66541480Smckusick unit, bp->b_flags & B_READ? "read" : "write", 66641480Smckusick bp->b_un.b_addr, bp->b_cylin, bp->b_bcount, 66741480Smckusick sdtab[unit].b_errcnt); 66841480Smckusick #endif 66941480Smckusick bp->b_flags |= B_ERROR; 67041480Smckusick bp->b_error = EIO; 67141480Smckusick sdfinish(unit, sc, bp); 67241480Smckusick } 67341480Smckusick 67441480Smckusick void 67541480Smckusick sdintr(unit, stat) 67641480Smckusick register int unit; 67741480Smckusick int stat; 67841480Smckusick { 67941480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 68041480Smckusick register struct buf *bp = sdtab[unit].b_actf; 68141480Smckusick register struct hp_device *hp = sc->sc_hd; 68245750Smckusick int retry; 68341480Smckusick 68441480Smckusick if (bp == NULL) { 68541480Smckusick printf("sd%d: bp == NULL\n", unit); 68641480Smckusick return; 68741480Smckusick } 68841480Smckusick if (hp->hp_dk >= 0) 68941480Smckusick dk_busy &=~ (1 << hp->hp_dk); 69041480Smckusick if (stat) { 69141480Smckusick #ifdef DEBUG 69241480Smckusick if (sddebug & SDB_ERROR) 69341480Smckusick printf("sd%d: sdintr: bad scsi status 0x%x\n", 69441480Smckusick unit, stat); 69541480Smckusick #endif 69645750Smckusick retry = sderror(unit, sc, hp, stat); 69745750Smckusick if (retry && sdtab[unit].b_errcnt++ < SDRETRY) { 69845750Smckusick printf("sd%d: retry #%d\n", 69945750Smckusick unit, sdtab[unit].b_errcnt); 70045750Smckusick sdstart(unit); 70145750Smckusick return; 70245750Smckusick } 70341480Smckusick bp->b_flags |= B_ERROR; 70441480Smckusick bp->b_error = EIO; 70541480Smckusick } 70641480Smckusick sdfinish(unit, sc, bp); 70741480Smckusick } 70841480Smckusick 70941480Smckusick int 71049132Skarels sdread(dev, uio, flags) 71141480Smckusick dev_t dev; 71241480Smckusick struct uio *uio; 71349132Skarels int flags; 71441480Smckusick { 71541480Smckusick register int unit = sdunit(dev); 71641480Smckusick register int pid; 71741480Smckusick 71849132Skarels if ((pid = sd_softc[unit].sc_format_pid) && 71949132Skarels pid != uio->uio_procp->p_pid) 72041480Smckusick return (EPERM); 72141480Smckusick 72249132Skarels return (physio(sdstrategy, NULL, dev, B_READ, minphys, uio)); 72341480Smckusick } 72441480Smckusick 72541480Smckusick int 72649132Skarels sdwrite(dev, uio, flags) 72741480Smckusick dev_t dev; 72841480Smckusick struct uio *uio; 72949132Skarels int flags; 73041480Smckusick { 73141480Smckusick register int unit = sdunit(dev); 73241480Smckusick register int pid; 73341480Smckusick 73449132Skarels if ((pid = sd_softc[unit].sc_format_pid) && 73549132Skarels pid != uio->uio_procp->p_pid) 73641480Smckusick return (EPERM); 73741480Smckusick 73849132Skarels return (physio(sdstrategy, NULL, dev, B_WRITE, minphys, uio)); 73941480Smckusick } 74041480Smckusick 74141480Smckusick int 74249132Skarels sdioctl(dev, cmd, data, flag, p) 74341480Smckusick dev_t dev; 74441480Smckusick int cmd; 74541480Smckusick caddr_t data; 74641480Smckusick int flag; 74749132Skarels struct proc *p; 74841480Smckusick { 74941480Smckusick register int unit = sdunit(dev); 75041480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 75141480Smckusick 75241480Smckusick switch (cmd) { 75341480Smckusick default: 75441480Smckusick return (EINVAL); 75541480Smckusick 75641480Smckusick case SDIOCSFORMAT: 75741480Smckusick /* take this device into or out of "format" mode */ 75849132Skarels if (suser(p->p_ucred, &p->p_acflag)) 75941480Smckusick return(EPERM); 76041480Smckusick 76141480Smckusick if (*(int *)data) { 76241480Smckusick if (sc->sc_format_pid) 76341480Smckusick return (EPERM); 76449132Skarels sc->sc_format_pid = p->p_pid; 76541480Smckusick } else 76641480Smckusick sc->sc_format_pid = 0; 76741480Smckusick return (0); 76841480Smckusick 76941480Smckusick case SDIOCGFORMAT: 77041480Smckusick /* find out who has the device in format mode */ 77141480Smckusick *(int *)data = sc->sc_format_pid; 77241480Smckusick return (0); 77341480Smckusick 77441480Smckusick case SDIOCSCSICOMMAND: 77541480Smckusick /* 77641480Smckusick * Save what user gave us as SCSI cdb to use with next 77741480Smckusick * read or write to the char device. 77841480Smckusick */ 77949132Skarels if (sc->sc_format_pid != p->p_pid) 78041480Smckusick return (EPERM); 78141480Smckusick if (legal_cmds[((struct scsi_fmt_cdb *)data)->cdb[0]] == 0) 78241480Smckusick return (EINVAL); 78341480Smckusick bcopy(data, (caddr_t)&sdcmd[unit], sizeof(sdcmd[0])); 78441480Smckusick return (0); 78541480Smckusick 78641480Smckusick case SDIOCSENSE: 78741480Smckusick /* 78841480Smckusick * return the SCSI sense data saved after the last 78941480Smckusick * operation that completed with "check condition" status. 79041480Smckusick */ 79141480Smckusick bcopy((caddr_t)&sdsense[unit], data, sizeof(sdsense[0])); 79241480Smckusick return (0); 79341480Smckusick 79441480Smckusick } 79541480Smckusick /*NOTREACHED*/ 79641480Smckusick } 79741480Smckusick 79841480Smckusick int 79941480Smckusick sdsize(dev) 80041480Smckusick dev_t dev; 80141480Smckusick { 80241480Smckusick register int unit = sdunit(dev); 80341480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 80441480Smckusick 80541480Smckusick if (unit >= NSD || (sc->sc_flags & SDF_ALIVE) == 0) 80641480Smckusick return(-1); 80741480Smckusick 80841480Smckusick return(sc->sc_info.part[sdpart(dev)].nblocks); 80941480Smckusick } 81041480Smckusick 81141480Smckusick /* 81241480Smckusick * Non-interrupt driven, non-dma dump routine. 81341480Smckusick */ 81441480Smckusick int 81541480Smckusick sddump(dev) 81641480Smckusick dev_t dev; 81741480Smckusick { 81841480Smckusick int part = sdpart(dev); 81941480Smckusick int unit = sdunit(dev); 82041480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 82141480Smckusick register struct hp_device *hp = sc->sc_hd; 82241480Smckusick register daddr_t baddr; 82341480Smckusick register int maddr; 82441480Smckusick register int pages, i; 82541480Smckusick int stat; 82641480Smckusick extern int lowram; 82741480Smckusick 82841480Smckusick /* 82941480Smckusick * Hmm... all vax drivers dump maxfree pages which is physmem minus 83041480Smckusick * the message buffer. Is there a reason for not dumping the 83141480Smckusick * message buffer? Savecore expects to read 'dumpsize' pages of 83241480Smckusick * dump, where dumpsys() sets dumpsize to physmem! 83341480Smckusick */ 83441480Smckusick pages = physmem; 83541480Smckusick 83641480Smckusick /* is drive ok? */ 83741480Smckusick if (unit >= NSD || (sc->sc_flags & SDF_ALIVE) == 0) 83841480Smckusick return (ENXIO); 83941480Smckusick /* dump parameters in range? */ 84041480Smckusick if (dumplo < 0 || dumplo >= sc->sc_info.part[part].nblocks) 84141480Smckusick return (EINVAL); 84241480Smckusick if (dumplo + ctod(pages) > sc->sc_info.part[part].nblocks) 84341480Smckusick pages = dtoc(sc->sc_info.part[part].nblocks - dumplo); 84441480Smckusick maddr = lowram; 84541480Smckusick baddr = dumplo + sc->sc_info.part[part].strtblk; 84641480Smckusick /* scsi bus idle? */ 84741480Smckusick if (!scsireq(&sc->sc_dq)) { 84841480Smckusick scsireset(hp->hp_ctlr); 84941480Smckusick sdreset(sc, sc->sc_hd); 85041480Smckusick printf("[ drive %d reset ] ", unit); 85141480Smckusick } 85241480Smckusick for (i = 0; i < pages; i++) { 85341480Smckusick #define NPGMB (1024*1024/NBPG) 85441480Smckusick /* print out how many Mbs we have dumped */ 85541480Smckusick if (i && (i % NPGMB) == 0) 85641480Smckusick printf("%d ", i / NPGMB); 85741480Smckusick #undef NPBMG 85845750Smckusick pmap_enter(pmap_kernel(), vmmap, maddr, VM_PROT_READ, TRUE); 85941480Smckusick stat = scsi_tt_write(hp->hp_ctlr, hp->hp_slave, sc->sc_punit, 86041480Smckusick vmmap, NBPG, baddr, sc->sc_bshift); 86141480Smckusick if (stat) { 86241480Smckusick printf("sddump: scsi write error 0x%x\n", stat); 86341480Smckusick return (EIO); 86441480Smckusick } 86541480Smckusick maddr += NBPG; 86641480Smckusick baddr += ctod(1); 86741480Smckusick } 86841480Smckusick return (0); 86941480Smckusick } 87041480Smckusick #endif 871