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*52614Smckusick * @(#)sd.c 7.12 (Berkeley) 02/19/92 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" 3551118Smarc #include "vm/vm_prot.h" 3645788Sbostic #include "vm/pmap.h" 3745750Smckusick 3841480Smckusick extern int scsi_test_unit_rdy(); 3941480Smckusick extern int scsi_request_sense(); 4041480Smckusick extern int scsi_inquiry(); 4141480Smckusick extern int scsi_read_capacity(); 4241480Smckusick extern int scsi_tt_write(); 4341480Smckusick extern int scsireq(); 4441480Smckusick extern int scsiustart(); 4541480Smckusick extern int scsigo(); 4641480Smckusick extern void scsifree(); 4741480Smckusick extern void scsireset(); 4849304Shibler extern void scsi_delay(); 4941480Smckusick 5041480Smckusick extern void disksort(); 5141480Smckusick extern void biodone(); 5241480Smckusick extern int physio(); 5341480Smckusick extern void TBIS(); 5441480Smckusick 5541480Smckusick int sdinit(); 5641480Smckusick void sdstrategy(), sdstart(), sdustart(), sdgo(), sdintr(); 5741480Smckusick 5841480Smckusick struct driver sddriver = { 5941480Smckusick sdinit, "sd", (int (*)())sdstart, (int (*)())sdgo, (int (*)())sdintr, 6041480Smckusick }; 6141480Smckusick 6241480Smckusick struct size { 6341480Smckusick u_long strtblk; 6441480Smckusick u_long endblk; 6541480Smckusick int nblocks; 6641480Smckusick }; 6741480Smckusick 6841480Smckusick struct sdinfo { 6941480Smckusick struct size part[8]; 7041480Smckusick }; 7141480Smckusick 7241480Smckusick /* 7341480Smckusick * since the SCSI standard tends to hide the disk structure, we define 7441480Smckusick * partitions in terms of DEV_BSIZE blocks. The default partition table 7541480Smckusick * (for an unlabeled disk) reserves 512K for a boot area, has an 8 meg 7641480Smckusick * root and 32 meg of swap. The rest of the space on the drive goes in 7741480Smckusick * the G partition. As usual, the C partition covers the entire disk 7841480Smckusick * (including the boot area). 7941480Smckusick */ 8041480Smckusick struct sdinfo sddefaultpart = { 8141480Smckusick 1024, 17408, 16384 , /* A */ 8241480Smckusick 17408, 82944, 65536 , /* B */ 8341480Smckusick 0, 0, 0 , /* C */ 8441480Smckusick 17408, 115712, 98304 , /* D */ 8541480Smckusick 115712, 218112, 102400 , /* E */ 8641480Smckusick 218112, 0, 0 , /* F */ 8741480Smckusick 82944, 0, 0 , /* G */ 8841480Smckusick 115712, 0, 0 , /* H */ 8941480Smckusick }; 9041480Smckusick 9141480Smckusick struct sd_softc { 9241480Smckusick struct hp_device *sc_hd; 9341480Smckusick struct devqueue sc_dq; 9441480Smckusick int sc_format_pid; /* process using "format" mode */ 9541480Smckusick short sc_flags; 9641480Smckusick short sc_type; /* drive type */ 9741480Smckusick short sc_punit; /* physical unit (scsi lun) */ 9841480Smckusick u_short sc_bshift; /* convert device blocks to DEV_BSIZE blks */ 9941480Smckusick u_int sc_blks; /* number of blocks on device */ 10041480Smckusick int sc_blksize; /* device block size in bytes */ 10141480Smckusick u_int sc_wpms; /* average xfer rate in 16 bit wds/sec. */ 10241480Smckusick struct sdinfo sc_info; /* drive partition table & label info */ 10341480Smckusick } sd_softc[NSD]; 10441480Smckusick 10541480Smckusick /* sc_flags values */ 10641480Smckusick #define SDF_ALIVE 0x1 10741480Smckusick 10841480Smckusick #ifdef DEBUG 10941480Smckusick int sddebug = 1; 11041480Smckusick #define SDB_ERROR 0x01 11141480Smckusick #define SDB_PARTIAL 0x02 11241480Smckusick #endif 11341480Smckusick 11441480Smckusick struct sdstats { 11541480Smckusick long sdresets; 11641480Smckusick long sdtransfers; 11741480Smckusick long sdpartials; 11841480Smckusick } sdstats[NSD]; 11941480Smckusick 12041480Smckusick struct buf sdtab[NSD]; 12141480Smckusick struct scsi_fmt_cdb sdcmd[NSD]; 12241480Smckusick struct scsi_fmt_sense sdsense[NSD]; 12341480Smckusick 12441480Smckusick static struct scsi_fmt_cdb sd_read_cmd = { 10, CMD_READ_EXT }; 12541480Smckusick static struct scsi_fmt_cdb sd_write_cmd = { 10, CMD_WRITE_EXT }; 12641480Smckusick 12749132Skarels #define sdunit(x) (minor(x) >> 3) 12841480Smckusick #define sdpart(x) (minor(x) & 0x7) 12941480Smckusick #define sdpunit(x) ((x) & 7) 13041480Smckusick #define b_cylin b_resid 13145750Smckusick 13241480Smckusick #define SDRETRY 2 13341480Smckusick 13441480Smckusick /* 13541480Smckusick * Table of scsi commands users are allowed to access via "format" 13641480Smckusick * mode. 0 means not legal. 1 means "immediate" (doesn't need dma). 13741480Smckusick * -1 means needs dma and/or wait for intr. 13841480Smckusick */ 13941480Smckusick static char legal_cmds[256] = { 14041480Smckusick /***** 0 1 2 3 4 5 6 7 8 9 A B C D E F */ 14141480Smckusick /*00*/ 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14241480Smckusick /*10*/ 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 14341480Smckusick /*20*/ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14441480Smckusick /*30*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14541480Smckusick /*40*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14641480Smckusick /*50*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14741480Smckusick /*60*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14841480Smckusick /*70*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14941480Smckusick /*80*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15041480Smckusick /*90*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15141480Smckusick /*a0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15241480Smckusick /*b0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15341480Smckusick /*c0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15441480Smckusick /*d0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15541480Smckusick /*e0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15641480Smckusick /*f0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15741480Smckusick }; 15841480Smckusick 15941480Smckusick static struct scsi_inquiry inqbuf; 16041480Smckusick static struct scsi_fmt_cdb inq = { 16141480Smckusick 6, 16241480Smckusick CMD_INQUIRY, 0, 0, 0, sizeof(inqbuf), 0 16341480Smckusick }; 16441480Smckusick 16541480Smckusick static u_char capbuf[8]; 16641480Smckusick struct scsi_fmt_cdb cap = { 16741480Smckusick 10, 16841480Smckusick CMD_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 16941480Smckusick }; 17041480Smckusick 17141480Smckusick static int 17241480Smckusick sdident(sc, hd) 17341480Smckusick struct sd_softc *sc; 17441480Smckusick struct hp_device *hd; 17541480Smckusick { 17641480Smckusick int unit; 17741480Smckusick register int ctlr, slave; 17841480Smckusick register int i; 17941480Smckusick register int tries = 10; 18049304Shibler char idstr[32]; 18145750Smckusick int ismo = 0; 18241480Smckusick 18341480Smckusick ctlr = hd->hp_ctlr; 18441480Smckusick slave = hd->hp_slave; 18541480Smckusick unit = sc->sc_punit; 18649304Shibler scsi_delay(-1); 18741480Smckusick 18841480Smckusick /* 18941480Smckusick * See if unit exists and is a disk then read block size & nblocks. 19041480Smckusick */ 19141480Smckusick while ((i = scsi_test_unit_rdy(ctlr, slave, unit)) != 0) { 19245750Smckusick if (i == -1 || --tries < 0) { 19345750Smckusick if (ismo) 19445750Smckusick break; 19541480Smckusick /* doesn't exist or not a CCS device */ 19649304Shibler goto failed; 19745750Smckusick } 19841480Smckusick if (i == STS_CHECKCOND) { 19941480Smckusick u_char sensebuf[128]; 20041480Smckusick struct scsi_xsense *sp = (struct scsi_xsense *)sensebuf; 20141480Smckusick 20241480Smckusick scsi_request_sense(ctlr, slave, unit, sensebuf, 20341480Smckusick sizeof(sensebuf)); 20445750Smckusick if (sp->class == 7) 20545750Smckusick switch (sp->key) { 20645750Smckusick /* not ready -- might be MO with no media */ 20745750Smckusick case 2: 20845750Smckusick if (sp->len == 12 && 20945750Smckusick sensebuf[12] == 10) /* XXX */ 21045750Smckusick ismo = 1; 21145750Smckusick break; 21241480Smckusick /* drive doing an RTZ -- give it a while */ 21345750Smckusick case 6: 21445750Smckusick DELAY(1000000); 21545750Smckusick break; 21645750Smckusick default: 21745750Smckusick break; 21845750Smckusick } 21941480Smckusick } 22041480Smckusick DELAY(1000); 22141480Smckusick } 22245750Smckusick /* 22345750Smckusick * Find out about device 22445750Smckusick */ 22545750Smckusick if (scsi_immed_command(ctlr, slave, unit, &inq, 22645750Smckusick (u_char *)&inqbuf, sizeof(inqbuf), B_READ)) 22749304Shibler goto failed; 22841480Smckusick switch (inqbuf.type) { 22941480Smckusick case 0: /* disk */ 23041480Smckusick case 4: /* WORM */ 23141480Smckusick case 5: /* CD-ROM */ 23241480Smckusick case 7: /* Magneto-optical */ 23341480Smckusick break; 23441480Smckusick default: /* not a disk */ 23549304Shibler goto failed; 23641480Smckusick } 23745750Smckusick /* 23849304Shibler * Get a usable id string 23945750Smckusick */ 24049304Shibler if (inqbuf.version != 1) { 24149304Shibler bcopy("UNKNOWN", &idstr[0], 8); 24249304Shibler bcopy("DRIVE TYPE", &idstr[8], 11); 24349304Shibler } else { 24449304Shibler bcopy((caddr_t)&inqbuf.vendor_id, (caddr_t)idstr, 28); 24549304Shibler for (i = 27; i > 23; --i) 24649304Shibler if (idstr[i] != ' ') 24749304Shibler break; 24849304Shibler idstr[i+1] = 0; 24949304Shibler for (i = 23; i > 7; --i) 25049304Shibler if (idstr[i] != ' ') 25149304Shibler break; 25249304Shibler idstr[i+1] = 0; 25349304Shibler for (i = 7; i >= 0; --i) 25449304Shibler if (idstr[i] != ' ') 25549304Shibler break; 25649304Shibler idstr[i+1] = 0; 25745750Smckusick } 25845750Smckusick i = scsi_immed_command(ctlr, slave, unit, &cap, 25945750Smckusick (u_char *)&capbuf, sizeof(capbuf), B_READ); 26045750Smckusick if (i) { 26149304Shibler if (i != STS_CHECKCOND || 26249304Shibler bcmp(&idstr[0], "HP", 3) || 26349304Shibler bcmp(&idstr[8], "S6300.650A", 11)) 26449304Shibler goto failed; 26545750Smckusick /* XXX unformatted or non-existant MO media; fake it */ 26649304Shibler sc->sc_blks = 318664; 26749304Shibler sc->sc_blksize = 1024; 26845750Smckusick } else { 26945750Smckusick sc->sc_blks = *(u_int *)&capbuf[0]; 27045750Smckusick sc->sc_blksize = *(int *)&capbuf[4]; 27145750Smckusick } 27245750Smckusick /* return value of read capacity is last valid block number */ 27345750Smckusick sc->sc_blks++; 27445750Smckusick 27541480Smckusick if (inqbuf.version != 1) 27641480Smckusick printf("sd%d: type 0x%x, qual 0x%x, ver %d", hd->hp_unit, 27741480Smckusick inqbuf.type, inqbuf.qual, inqbuf.version); 27849304Shibler else 27941480Smckusick printf("sd%d: %s %s rev %s", hd->hp_unit, idstr, &idstr[8], 28041480Smckusick &idstr[24]); 28141480Smckusick printf(", %d %d byte blocks\n", sc->sc_blks, sc->sc_blksize); 28241480Smckusick if (sc->sc_blksize != DEV_BSIZE) { 28341480Smckusick if (sc->sc_blksize < DEV_BSIZE) { 28441480Smckusick printf("sd%d: need %d byte blocks - drive ignored\n", 28541480Smckusick unit, DEV_BSIZE); 28649304Shibler goto failed; 28741480Smckusick } 28841480Smckusick for (i = sc->sc_blksize; i > DEV_BSIZE; i >>= 1) 28941480Smckusick ++sc->sc_bshift; 29041480Smckusick sc->sc_blks <<= sc->sc_bshift; 29141480Smckusick } 29241480Smckusick sc->sc_wpms = 32 * (60 * DEV_BSIZE / 2); /* XXX */ 29349304Shibler scsi_delay(0); 29441480Smckusick return(inqbuf.type); 29549304Shibler failed: 29649304Shibler scsi_delay(0); 29749304Shibler return(-1); 29841480Smckusick } 29941480Smckusick 30041480Smckusick int 30141480Smckusick sdinit(hd) 30241480Smckusick register struct hp_device *hd; 30341480Smckusick { 30441480Smckusick register struct sd_softc *sc = &sd_softc[hd->hp_unit]; 30541480Smckusick 30641480Smckusick sc->sc_hd = hd; 30741480Smckusick sc->sc_punit = sdpunit(hd->hp_flags); 30841480Smckusick sc->sc_type = sdident(sc, hd); 30941480Smckusick if (sc->sc_type < 0) 31041480Smckusick return(0); 31141480Smckusick sc->sc_dq.dq_ctlr = hd->hp_ctlr; 31241480Smckusick sc->sc_dq.dq_unit = hd->hp_unit; 31341480Smckusick sc->sc_dq.dq_slave = hd->hp_slave; 31441480Smckusick sc->sc_dq.dq_driver = &sddriver; 31541480Smckusick 31641480Smckusick /* 31741480Smckusick * If we don't have a disk label, build a default partition 31841480Smckusick * table with 'standard' size root & swap and everything else 31941480Smckusick * in the G partition. 32041480Smckusick */ 32141480Smckusick sc->sc_info = sddefaultpart; 32241480Smckusick /* C gets everything */ 32341480Smckusick sc->sc_info.part[2].nblocks = sc->sc_blks; 32441480Smckusick sc->sc_info.part[2].endblk = sc->sc_blks; 32541480Smckusick /* G gets from end of B to end of disk */ 32641480Smckusick sc->sc_info.part[6].nblocks = sc->sc_blks - sc->sc_info.part[1].endblk; 32741480Smckusick sc->sc_info.part[6].endblk = sc->sc_blks; 32841480Smckusick /* 32941480Smckusick * We also define the D, E and F paritions as an alternative to 33041480Smckusick * B and G. D is 48Mb, starts after A and is intended for swapping. 33141480Smckusick * E is 50Mb, starts after D and is intended for /usr. F starts 33241480Smckusick * after E and is what ever is left. 33341480Smckusick */ 33441480Smckusick if (sc->sc_blks >= sc->sc_info.part[4].endblk) { 33541480Smckusick sc->sc_info.part[5].nblocks = 33641480Smckusick sc->sc_blks - sc->sc_info.part[4].endblk; 33741480Smckusick sc->sc_info.part[5].endblk = sc->sc_blks; 33841480Smckusick } else { 33941480Smckusick sc->sc_info.part[5].strtblk = 0; 34041480Smckusick sc->sc_info.part[3] = sc->sc_info.part[5]; 34141480Smckusick sc->sc_info.part[4] = sc->sc_info.part[5]; 34241480Smckusick } 34341480Smckusick /* 34441480Smckusick * H is a single partition alternative to E and F. 34541480Smckusick */ 34641480Smckusick if (sc->sc_blks >= sc->sc_info.part[3].endblk) { 34741480Smckusick sc->sc_info.part[7].nblocks = 34841480Smckusick sc->sc_blks - sc->sc_info.part[3].endblk; 34941480Smckusick sc->sc_info.part[7].endblk = sc->sc_blks; 35041480Smckusick } else { 35141480Smckusick sc->sc_info.part[7].strtblk = 0; 35241480Smckusick } 35341480Smckusick 35441480Smckusick sc->sc_flags = SDF_ALIVE; 35541480Smckusick return(1); 35641480Smckusick } 35741480Smckusick 35841480Smckusick void 35941480Smckusick sdreset(sc, hd) 36041480Smckusick register struct sd_softc *sc; 36141480Smckusick register struct hp_device *hd; 36241480Smckusick { 36341480Smckusick sdstats[hd->hp_unit].sdresets++; 36441480Smckusick } 36541480Smckusick 36641480Smckusick int 36749132Skarels sdopen(dev, flags, mode, p) 36841480Smckusick dev_t dev; 36949132Skarels int flags, mode; 37049132Skarels struct proc *p; 37141480Smckusick { 37241480Smckusick register int unit = sdunit(dev); 37341480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 37441480Smckusick 37541480Smckusick if (unit >= NSD) 37641480Smckusick return(ENXIO); 37749132Skarels if ((sc->sc_flags & SDF_ALIVE) == 0 && suser(p->p_ucred, &p->p_acflag)) 37841480Smckusick return(ENXIO); 37941480Smckusick 38041480Smckusick if (sc->sc_hd->hp_dk >= 0) 38141480Smckusick dk_wpms[sc->sc_hd->hp_dk] = sc->sc_wpms; 38241480Smckusick return(0); 38341480Smckusick } 38441480Smckusick 38541480Smckusick /* 38641480Smckusick * This routine is called for partial block transfers and non-aligned 38741480Smckusick * transfers (the latter only being possible on devices with a block size 38841480Smckusick * larger than DEV_BSIZE). The operation is performed in three steps 38941480Smckusick * using a locally allocated buffer: 39041480Smckusick * 1. transfer any initial partial block 39141480Smckusick * 2. transfer full blocks 39241480Smckusick * 3. transfer any final partial block 39341480Smckusick */ 39441480Smckusick static void 39541480Smckusick sdlblkstrat(bp, bsize) 39641480Smckusick register struct buf *bp; 39741480Smckusick register int bsize; 39841480Smckusick { 39941480Smckusick register struct buf *cbp = (struct buf *)malloc(sizeof(struct buf), 40041480Smckusick M_DEVBUF, M_WAITOK); 40141480Smckusick caddr_t cbuf = (caddr_t)malloc(bsize, M_DEVBUF, M_WAITOK); 40241480Smckusick register int bn, resid; 40341480Smckusick register caddr_t addr; 40441480Smckusick 40541480Smckusick bzero((caddr_t)cbp, sizeof(*cbp)); 40649132Skarels cbp->b_proc = curproc; /* XXX */ 40741480Smckusick cbp->b_dev = bp->b_dev; 40841480Smckusick bn = bp->b_blkno; 40941480Smckusick resid = bp->b_bcount; 41041480Smckusick addr = bp->b_un.b_addr; 41141480Smckusick #ifdef DEBUG 41241480Smckusick if (sddebug & SDB_PARTIAL) 41341480Smckusick printf("sdlblkstrat: bp %x flags %x bn %x resid %x addr %x\n", 41441480Smckusick bp, bp->b_flags, bn, resid, addr); 41541480Smckusick #endif 41641480Smckusick 41741480Smckusick while (resid > 0) { 41841480Smckusick register int boff = dbtob(bn) & (bsize - 1); 41941480Smckusick register int count; 42041480Smckusick 42141480Smckusick if (boff || resid < bsize) { 42241480Smckusick sdstats[sdunit(bp->b_dev)].sdpartials++; 42341480Smckusick count = MIN(resid, bsize - boff); 42441480Smckusick cbp->b_flags = B_BUSY | B_PHYS | B_READ; 42541480Smckusick cbp->b_blkno = bn - btodb(boff); 42641480Smckusick cbp->b_un.b_addr = cbuf; 42741480Smckusick cbp->b_bcount = bsize; 42841480Smckusick #ifdef DEBUG 42941480Smckusick if (sddebug & SDB_PARTIAL) 43041480Smckusick printf(" readahead: bn %x cnt %x off %x addr %x\n", 43141480Smckusick cbp->b_blkno, count, boff, addr); 43241480Smckusick #endif 43341480Smckusick sdstrategy(cbp); 43441480Smckusick biowait(cbp); 43541480Smckusick if (cbp->b_flags & B_ERROR) { 43641480Smckusick bp->b_flags |= B_ERROR; 43741480Smckusick bp->b_error = cbp->b_error; 43841480Smckusick break; 43941480Smckusick } 44041480Smckusick if (bp->b_flags & B_READ) { 44141480Smckusick bcopy(&cbuf[boff], addr, count); 44241480Smckusick goto done; 44341480Smckusick } 44441480Smckusick bcopy(addr, &cbuf[boff], count); 44541480Smckusick #ifdef DEBUG 44641480Smckusick if (sddebug & SDB_PARTIAL) 44741480Smckusick printf(" writeback: bn %x cnt %x off %x addr %x\n", 44841480Smckusick cbp->b_blkno, count, boff, addr); 44941480Smckusick #endif 45041480Smckusick } else { 45141480Smckusick count = resid & ~(bsize - 1); 45241480Smckusick cbp->b_blkno = bn; 45341480Smckusick cbp->b_un.b_addr = addr; 45441480Smckusick cbp->b_bcount = count; 45541480Smckusick #ifdef DEBUG 45641480Smckusick if (sddebug & SDB_PARTIAL) 45741480Smckusick printf(" fulltrans: bn %x cnt %x addr %x\n", 45841480Smckusick cbp->b_blkno, count, addr); 45941480Smckusick #endif 46041480Smckusick } 46141480Smckusick cbp->b_flags = B_BUSY | B_PHYS | (bp->b_flags & B_READ); 46241480Smckusick sdstrategy(cbp); 46341480Smckusick biowait(cbp); 46441480Smckusick if (cbp->b_flags & B_ERROR) { 46541480Smckusick bp->b_flags |= B_ERROR; 46641480Smckusick bp->b_error = cbp->b_error; 46741480Smckusick break; 46841480Smckusick } 46941480Smckusick done: 47041480Smckusick bn += btodb(count); 47141480Smckusick resid -= count; 47241480Smckusick addr += count; 47341480Smckusick #ifdef DEBUG 47441480Smckusick if (sddebug & SDB_PARTIAL) 47541480Smckusick printf(" done: bn %x resid %x addr %x\n", 47641480Smckusick bn, resid, addr); 47741480Smckusick #endif 47841480Smckusick } 47941480Smckusick free(cbuf, M_DEVBUF); 48041480Smckusick free(cbp, M_DEVBUF); 48141480Smckusick } 48241480Smckusick 48341480Smckusick void 48441480Smckusick sdstrategy(bp) 48541480Smckusick register struct buf *bp; 48641480Smckusick { 48741480Smckusick register int unit = sdunit(bp->b_dev); 48841480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 48945750Smckusick register struct size *pinfo = &sc->sc_info.part[sdpart(bp->b_dev)]; 49041480Smckusick register struct buf *dp = &sdtab[unit]; 49145750Smckusick register daddr_t bn; 49245750Smckusick register int sz, s; 49341480Smckusick 49441480Smckusick if (sc->sc_format_pid) { 49549132Skarels if (sc->sc_format_pid != curproc->p_pid) { /* XXX */ 49641480Smckusick bp->b_error = EPERM; 49745750Smckusick bp->b_flags |= B_ERROR; 49845750Smckusick goto done; 49941480Smckusick } 50041480Smckusick bp->b_cylin = 0; 50141480Smckusick } else { 50241480Smckusick bn = bp->b_blkno; 50345750Smckusick sz = howmany(bp->b_bcount, DEV_BSIZE); 50445750Smckusick if (bn < 0 || bn + sz > pinfo->nblocks) { 50545750Smckusick sz = pinfo->nblocks - bn; 50645750Smckusick if (sz == 0) { 50741480Smckusick bp->b_resid = bp->b_bcount; 50841480Smckusick goto done; 50941480Smckusick } 51045750Smckusick if (sz < 0) { 51145750Smckusick bp->b_error = EINVAL; 51245750Smckusick bp->b_flags |= B_ERROR; 51345750Smckusick goto done; 51445750Smckusick } 51545750Smckusick bp->b_bcount = dbtob(sz); 51641480Smckusick } 51741480Smckusick /* 51841480Smckusick * Non-aligned or partial-block transfers handled specially. 51941480Smckusick */ 52041480Smckusick s = sc->sc_blksize - 1; 52141480Smckusick if ((dbtob(bn) & s) || (bp->b_bcount & s)) { 52241480Smckusick sdlblkstrat(bp, sc->sc_blksize); 52341480Smckusick goto done; 52441480Smckusick } 52545750Smckusick bp->b_cylin = (bn + pinfo->strtblk) >> sc->sc_bshift; 52641480Smckusick } 52741480Smckusick s = splbio(); 52841480Smckusick disksort(dp, bp); 52941480Smckusick if (dp->b_active == 0) { 53041480Smckusick dp->b_active = 1; 53141480Smckusick sdustart(unit); 53241480Smckusick } 53341480Smckusick splx(s); 53441480Smckusick return; 53541480Smckusick done: 53645750Smckusick biodone(bp); 53741480Smckusick } 53841480Smckusick 53941480Smckusick void 54041480Smckusick sdustart(unit) 54141480Smckusick register int unit; 54241480Smckusick { 54341480Smckusick if (scsireq(&sd_softc[unit].sc_dq)) 54441480Smckusick sdstart(unit); 54541480Smckusick } 54641480Smckusick 54750039Skarels /* 54850039Skarels * Return: 54950039Skarels * 0 if not really an error 55050039Skarels * <0 if we should do a retry 55150039Skarels * >0 if a fatal error 55250039Skarels */ 55345750Smckusick static int 55441480Smckusick sderror(unit, sc, hp, stat) 55541480Smckusick int unit, stat; 55641480Smckusick register struct sd_softc *sc; 55741480Smckusick register struct hp_device *hp; 55841480Smckusick { 55950039Skarels int cond = 1; 56045750Smckusick 56141480Smckusick sdsense[unit].status = stat; 56241480Smckusick if (stat & STS_CHECKCOND) { 56341480Smckusick struct scsi_xsense *sp; 56441480Smckusick 56541480Smckusick scsi_request_sense(hp->hp_ctlr, hp->hp_slave, 56641480Smckusick sc->sc_punit, sdsense[unit].sense, 56741480Smckusick sizeof(sdsense[unit].sense)); 56841480Smckusick sp = (struct scsi_xsense *)sdsense[unit].sense; 56941480Smckusick printf("sd%d: scsi sense class %d, code %d", unit, 57041480Smckusick sp->class, sp->code); 57141480Smckusick if (sp->class == 7) { 57241480Smckusick printf(", key %d", sp->key); 57341480Smckusick if (sp->valid) 57441480Smckusick printf(", blk %d", *(int *)&sp->info1); 57550039Skarels switch (sp->key) { 57650039Skarels /* no sense, try again */ 57750039Skarels case 0: 57850039Skarels cond = -1; 57950039Skarels break; 58050039Skarels /* recovered error, not a problem */ 58150039Skarels case 1: 58250039Skarels cond = 0; 58350039Skarels break; 58450039Skarels } 58541480Smckusick } 58641480Smckusick printf("\n"); 58741480Smckusick } 58850039Skarels return(cond); 58941480Smckusick } 59041480Smckusick 59141480Smckusick static void 59241480Smckusick sdfinish(unit, sc, bp) 59341480Smckusick int unit; 59441480Smckusick register struct sd_softc *sc; 59541480Smckusick register struct buf *bp; 59641480Smckusick { 59741480Smckusick sdtab[unit].b_errcnt = 0; 59841480Smckusick sdtab[unit].b_actf = bp->b_actf; 59941480Smckusick bp->b_resid = 0; 60045750Smckusick biodone(bp); 60141480Smckusick scsifree(&sc->sc_dq); 60241480Smckusick if (sdtab[unit].b_actf) 60341480Smckusick sdustart(unit); 60441480Smckusick else 60541480Smckusick sdtab[unit].b_active = 0; 60641480Smckusick } 60741480Smckusick 60841480Smckusick void 60941480Smckusick sdstart(unit) 61041480Smckusick register int unit; 61141480Smckusick { 61241480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 61341480Smckusick register struct hp_device *hp = sc->sc_hd; 61441480Smckusick 61541480Smckusick /* 61641480Smckusick * we have the SCSI bus -- in format mode, we may or may not need dma 61741480Smckusick * so check now. 61841480Smckusick */ 61941480Smckusick if (sc->sc_format_pid && legal_cmds[sdcmd[unit].cdb[0]] > 0) { 62041480Smckusick register struct buf *bp = sdtab[unit].b_actf; 62141480Smckusick register int sts; 62241480Smckusick 62341480Smckusick sts = scsi_immed_command(hp->hp_ctlr, hp->hp_slave, 62441480Smckusick sc->sc_punit, &sdcmd[unit], 62541480Smckusick bp->b_un.b_addr, bp->b_bcount, 62641480Smckusick bp->b_flags & B_READ); 62741480Smckusick sdsense[unit].status = sts; 62841480Smckusick if (sts & 0xfe) { 62945750Smckusick (void) sderror(unit, sc, hp, sts); 63041480Smckusick bp->b_flags |= B_ERROR; 63141480Smckusick bp->b_error = EIO; 63241480Smckusick } 63341480Smckusick sdfinish(unit, sc, bp); 63441480Smckusick 63541480Smckusick } else if (scsiustart(hp->hp_ctlr)) 63641480Smckusick sdgo(unit); 63741480Smckusick } 63841480Smckusick 63941480Smckusick void 64041480Smckusick sdgo(unit) 64141480Smckusick register int unit; 64241480Smckusick { 64341480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 64441480Smckusick register struct hp_device *hp = sc->sc_hd; 64541480Smckusick register struct buf *bp = sdtab[unit].b_actf; 64641480Smckusick register int pad; 64741480Smckusick register struct scsi_fmt_cdb *cmd; 64841480Smckusick 64941480Smckusick if (sc->sc_format_pid) { 65041480Smckusick cmd = &sdcmd[unit]; 65141480Smckusick pad = 0; 65241480Smckusick } else { 65341480Smckusick cmd = bp->b_flags & B_READ? &sd_read_cmd : &sd_write_cmd; 65441480Smckusick *(int *)(&cmd->cdb[2]) = bp->b_cylin; 65541480Smckusick pad = howmany(bp->b_bcount, sc->sc_blksize); 65641480Smckusick *(u_short *)(&cmd->cdb[7]) = pad; 65741480Smckusick pad = (bp->b_bcount & (sc->sc_blksize - 1)) != 0; 65841480Smckusick #ifdef DEBUG 65941480Smckusick if (pad) 66041480Smckusick printf("sd%d: partial block xfer -- %x bytes\n", 66141480Smckusick unit, bp->b_bcount); 66241480Smckusick #endif 66341480Smckusick sdstats[unit].sdtransfers++; 66441480Smckusick } 66541480Smckusick if (scsigo(hp->hp_ctlr, hp->hp_slave, sc->sc_punit, bp, cmd, pad) == 0) { 66641480Smckusick if (hp->hp_dk >= 0) { 66741480Smckusick dk_busy |= 1 << hp->hp_dk; 66841480Smckusick ++dk_seek[hp->hp_dk]; 66941480Smckusick ++dk_xfer[hp->hp_dk]; 67041480Smckusick dk_wds[hp->hp_dk] += bp->b_bcount >> 6; 67141480Smckusick } 67241480Smckusick return; 67341480Smckusick } 67441480Smckusick #ifdef DEBUG 67541480Smckusick if (sddebug & SDB_ERROR) 67641480Smckusick printf("sd%d: sdstart: %s adr %d blk %d len %d ecnt %d\n", 67741480Smckusick unit, bp->b_flags & B_READ? "read" : "write", 67841480Smckusick bp->b_un.b_addr, bp->b_cylin, bp->b_bcount, 67941480Smckusick sdtab[unit].b_errcnt); 68041480Smckusick #endif 68141480Smckusick bp->b_flags |= B_ERROR; 68241480Smckusick bp->b_error = EIO; 68341480Smckusick sdfinish(unit, sc, bp); 68441480Smckusick } 68541480Smckusick 68641480Smckusick void 68741480Smckusick sdintr(unit, stat) 68841480Smckusick register int unit; 68941480Smckusick int stat; 69041480Smckusick { 69141480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 69241480Smckusick register struct buf *bp = sdtab[unit].b_actf; 69341480Smckusick register struct hp_device *hp = sc->sc_hd; 69450039Skarels int cond; 69541480Smckusick 69641480Smckusick if (bp == NULL) { 69741480Smckusick printf("sd%d: bp == NULL\n", unit); 69841480Smckusick return; 69941480Smckusick } 70041480Smckusick if (hp->hp_dk >= 0) 70141480Smckusick dk_busy &=~ (1 << hp->hp_dk); 70241480Smckusick if (stat) { 70341480Smckusick #ifdef DEBUG 70441480Smckusick if (sddebug & SDB_ERROR) 70541480Smckusick printf("sd%d: sdintr: bad scsi status 0x%x\n", 70641480Smckusick unit, stat); 70741480Smckusick #endif 70850039Skarels cond = sderror(unit, sc, hp, stat); 70950039Skarels if (cond) { 71050039Skarels if (cond < 0 && sdtab[unit].b_errcnt++ < SDRETRY) { 71150038Skarels #ifdef DEBUG 71250039Skarels if (sddebug & SDB_ERROR) 71350039Skarels printf("sd%d: retry #%d\n", 71450039Skarels unit, sdtab[unit].b_errcnt); 71550038Skarels #endif 71650039Skarels sdstart(unit); 71750039Skarels return; 71850039Skarels } 71950039Skarels bp->b_flags |= B_ERROR; 72050039Skarels bp->b_error = EIO; 72145750Smckusick } 72241480Smckusick } 72341480Smckusick sdfinish(unit, sc, bp); 72441480Smckusick } 72541480Smckusick 72641480Smckusick int 72749132Skarels sdread(dev, uio, flags) 72841480Smckusick dev_t dev; 72941480Smckusick struct uio *uio; 73049132Skarels int flags; 73141480Smckusick { 73241480Smckusick register int unit = sdunit(dev); 73341480Smckusick register int pid; 73441480Smckusick 73549132Skarels if ((pid = sd_softc[unit].sc_format_pid) && 73649132Skarels pid != uio->uio_procp->p_pid) 73741480Smckusick return (EPERM); 73841480Smckusick 73949132Skarels return (physio(sdstrategy, NULL, dev, B_READ, minphys, uio)); 74041480Smckusick } 74141480Smckusick 74241480Smckusick int 74349132Skarels sdwrite(dev, uio, flags) 74441480Smckusick dev_t dev; 74541480Smckusick struct uio *uio; 74649132Skarels int flags; 74741480Smckusick { 74841480Smckusick register int unit = sdunit(dev); 74941480Smckusick register int pid; 75041480Smckusick 75149132Skarels if ((pid = sd_softc[unit].sc_format_pid) && 75249132Skarels pid != uio->uio_procp->p_pid) 75341480Smckusick return (EPERM); 75441480Smckusick 75549132Skarels return (physio(sdstrategy, NULL, dev, B_WRITE, minphys, uio)); 75641480Smckusick } 75741480Smckusick 75841480Smckusick int 75949132Skarels sdioctl(dev, cmd, data, flag, p) 76041480Smckusick dev_t dev; 76141480Smckusick int cmd; 76241480Smckusick caddr_t data; 76341480Smckusick int flag; 76449132Skarels struct proc *p; 76541480Smckusick { 76641480Smckusick register int unit = sdunit(dev); 76741480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 76841480Smckusick 76941480Smckusick switch (cmd) { 77041480Smckusick default: 77141480Smckusick return (EINVAL); 77241480Smckusick 77341480Smckusick case SDIOCSFORMAT: 77441480Smckusick /* take this device into or out of "format" mode */ 77549132Skarels if (suser(p->p_ucred, &p->p_acflag)) 77641480Smckusick return(EPERM); 77741480Smckusick 77841480Smckusick if (*(int *)data) { 77941480Smckusick if (sc->sc_format_pid) 78041480Smckusick return (EPERM); 78149132Skarels sc->sc_format_pid = p->p_pid; 78241480Smckusick } else 78341480Smckusick sc->sc_format_pid = 0; 78441480Smckusick return (0); 78541480Smckusick 78641480Smckusick case SDIOCGFORMAT: 78741480Smckusick /* find out who has the device in format mode */ 78841480Smckusick *(int *)data = sc->sc_format_pid; 78941480Smckusick return (0); 79041480Smckusick 79141480Smckusick case SDIOCSCSICOMMAND: 79241480Smckusick /* 79341480Smckusick * Save what user gave us as SCSI cdb to use with next 79441480Smckusick * read or write to the char device. 79541480Smckusick */ 79649132Skarels if (sc->sc_format_pid != p->p_pid) 79741480Smckusick return (EPERM); 79841480Smckusick if (legal_cmds[((struct scsi_fmt_cdb *)data)->cdb[0]] == 0) 79941480Smckusick return (EINVAL); 80041480Smckusick bcopy(data, (caddr_t)&sdcmd[unit], sizeof(sdcmd[0])); 80141480Smckusick return (0); 80241480Smckusick 80341480Smckusick case SDIOCSENSE: 80441480Smckusick /* 80541480Smckusick * return the SCSI sense data saved after the last 80641480Smckusick * operation that completed with "check condition" status. 80741480Smckusick */ 80841480Smckusick bcopy((caddr_t)&sdsense[unit], data, sizeof(sdsense[0])); 80941480Smckusick return (0); 81041480Smckusick 81141480Smckusick } 81241480Smckusick /*NOTREACHED*/ 81341480Smckusick } 81441480Smckusick 81541480Smckusick int 81641480Smckusick sdsize(dev) 81741480Smckusick dev_t dev; 81841480Smckusick { 81941480Smckusick register int unit = sdunit(dev); 82041480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 82141480Smckusick 82241480Smckusick if (unit >= NSD || (sc->sc_flags & SDF_ALIVE) == 0) 82341480Smckusick return(-1); 82441480Smckusick 82541480Smckusick return(sc->sc_info.part[sdpart(dev)].nblocks); 82641480Smckusick } 82741480Smckusick 82841480Smckusick /* 82941480Smckusick * Non-interrupt driven, non-dma dump routine. 83041480Smckusick */ 83141480Smckusick int 83241480Smckusick sddump(dev) 83341480Smckusick dev_t dev; 83441480Smckusick { 83541480Smckusick int part = sdpart(dev); 83641480Smckusick int unit = sdunit(dev); 83741480Smckusick register struct sd_softc *sc = &sd_softc[unit]; 83841480Smckusick register struct hp_device *hp = sc->sc_hd; 83941480Smckusick register daddr_t baddr; 84041480Smckusick register int maddr; 84141480Smckusick register int pages, i; 84241480Smckusick int stat; 84341480Smckusick extern int lowram; 84441480Smckusick 84541480Smckusick /* 84641480Smckusick * Hmm... all vax drivers dump maxfree pages which is physmem minus 84741480Smckusick * the message buffer. Is there a reason for not dumping the 84841480Smckusick * message buffer? Savecore expects to read 'dumpsize' pages of 84941480Smckusick * dump, where dumpsys() sets dumpsize to physmem! 85041480Smckusick */ 85141480Smckusick pages = physmem; 85241480Smckusick 85341480Smckusick /* is drive ok? */ 85441480Smckusick if (unit >= NSD || (sc->sc_flags & SDF_ALIVE) == 0) 85541480Smckusick return (ENXIO); 85641480Smckusick /* dump parameters in range? */ 85741480Smckusick if (dumplo < 0 || dumplo >= sc->sc_info.part[part].nblocks) 85841480Smckusick return (EINVAL); 85941480Smckusick if (dumplo + ctod(pages) > sc->sc_info.part[part].nblocks) 86041480Smckusick pages = dtoc(sc->sc_info.part[part].nblocks - dumplo); 86141480Smckusick maddr = lowram; 86241480Smckusick baddr = dumplo + sc->sc_info.part[part].strtblk; 86341480Smckusick /* scsi bus idle? */ 86441480Smckusick if (!scsireq(&sc->sc_dq)) { 86541480Smckusick scsireset(hp->hp_ctlr); 86641480Smckusick sdreset(sc, sc->sc_hd); 86741480Smckusick printf("[ drive %d reset ] ", unit); 86841480Smckusick } 86941480Smckusick for (i = 0; i < pages; i++) { 87041480Smckusick #define NPGMB (1024*1024/NBPG) 87141480Smckusick /* print out how many Mbs we have dumped */ 87241480Smckusick if (i && (i % NPGMB) == 0) 87341480Smckusick printf("%d ", i / NPGMB); 87441480Smckusick #undef NPBMG 875*52614Smckusick pmap_enter(kernel_pmap, (vm_offset_t)vmmap, maddr, 87651576Smckusick 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