1 /* 2 * Copyright (c) 1992 Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Van Jacobson of Lawrence Berkeley Laboratory and Ralph Campbell. 7 * 8 * %sccs.include.redist.c% 9 * 10 * @(#)rz.c 7.9 (Berkeley) 05/09/93 11 */ 12 13 /* 14 * SCSI CCS (Command Command Set) disk driver. 15 * NOTE: The name was changed from "sd" to "rz" for DEC naming compatibility. 16 * I guess I can't avoid confusion someplace. 17 */ 18 #include "rz.h" 19 #if NRZ > 0 20 21 #include <sys/param.h> 22 #include <sys/systm.h> 23 #include <sys/buf.h> 24 #include <sys/errno.h> 25 #include <sys/fcntl.h> 26 #include <sys/ioctl.h> 27 #include <sys/dkstat.h> 28 #include <sys/disklabel.h> 29 #include <sys/malloc.h> 30 #include <sys/proc.h> 31 #include <sys/uio.h> 32 #include <sys/stat.h> 33 #include <sys/syslog.h> 34 35 #include <ufs/ffs/fs.h> 36 37 #include <pmax/dev/device.h> 38 #include <pmax/dev/scsi.h> 39 40 extern int splbio(); 41 extern void splx(); 42 extern int physio(); 43 extern char *readdisklabel(); 44 45 int rzprobe(); 46 void rzstrategy(), rzstart(), rzdone(); 47 48 struct driver rzdriver = { 49 "rz", rzprobe, rzstart, rzdone, 50 }; 51 52 struct size { 53 u_long strtblk; 54 u_long nblocks; 55 }; 56 57 /* 58 * Since the SCSI standard tends to hide the disk structure, we define 59 * partitions in terms of DEV_BSIZE blocks. The default partition table 60 * (for an unlabeled disk) reserves 8K for a boot area, has an 8 meg 61 * root and 32 meg of swap. The rest of the space on the drive goes in 62 * the G partition. As usual, the C partition covers the entire disk 63 * (including the boot area). 64 */ 65 static struct size rzdefaultpart[MAXPARTITIONS] = { 66 0, 16384, /* A */ 67 16384, 65536, /* B */ 68 0, 0, /* C */ 69 17408, 0, /* D */ 70 115712, 0, /* E */ 71 218112, 0, /* F */ 72 81920, 0, /* G */ 73 115712, 0, /* H */ 74 }; 75 76 #define RAWPART 2 /* 'c' partition */ /* XXX */ 77 78 struct rzstats { 79 long rzresets; 80 long rztransfers; 81 long rzpartials; 82 }; 83 84 struct rz_softc { 85 struct scsi_device *sc_sd; /* physical unit info */ 86 pid_t sc_format_pid; /* process using "format" mode */ 87 u_long sc_openpart; /* partitions open */ 88 u_long sc_bopenpart; /* block partitions open */ 89 u_long sc_copenpart; /* character partitions open */ 90 short sc_flags; /* see below */ 91 short sc_type; /* drive type from INQUIRY cmd */ 92 u_int sc_blks; /* number of blocks on device */ 93 int sc_blksize; /* device block size in bytes */ 94 int sc_bshift; /* convert device blocks to DEV_BSIZE */ 95 u_int sc_wpms; /* average xfer rate in 16bit wds/sec */ 96 struct disklabel sc_label; /* disk label for this disk */ 97 struct rzstats sc_stats; /* statisic counts */ 98 struct buf sc_tab; /* queue of pending operations */ 99 struct buf sc_buf; /* buf for doing I/O */ 100 struct buf sc_errbuf; /* buf for doing REQUEST_SENSE */ 101 struct ScsiCmd sc_cmd; /* command for controller */ 102 ScsiGroup1Cmd sc_rwcmd; /* SCSI cmd if not in "format" mode */ 103 struct scsi_fmt_cdb sc_cdb; /* SCSI cmd if in "format" mode */ 104 struct scsi_fmt_sense sc_sense; /* sense data from last cmd */ 105 } rz_softc[NRZ]; 106 107 /* sc_flags values */ 108 #define RZF_ALIVE 0x01 /* drive found and ready */ 109 #define RZF_SENSEINPROGRESS 0x02 /* REQUEST_SENSE command in progress */ 110 #define RZF_HAVELABEL 0x04 /* valid label found on disk */ 111 #define RZF_WLABEL 0x08 /* label is writeable */ 112 113 #ifdef DEBUG 114 int rzdebug = 3; 115 #define RZB_ERROR 0x01 116 #define RZB_PARTIAL 0x02 117 #define RZB_PRLABEL 0x04 118 #endif 119 120 #define rzunit(x) (minor(x) >> 3) 121 #define rzpart(x) (minor(x) & 0x7) 122 #define b_cylin b_resid 123 124 /* 125 * Table of scsi commands users are allowed to access via "format" mode. 126 * 0 means not legal. 127 * 1 means legal. 128 */ 129 static char legal_cmds[256] = { 130 /***** 0 1 2 3 4 5 6 7 8 9 A B C D E F */ 131 /*00*/ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 132 /*10*/ 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 133 /*20*/ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 134 /*30*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 135 /*40*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 136 /*50*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 137 /*60*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 138 /*70*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 139 /*80*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 140 /*90*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 141 /*a0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 142 /*b0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 143 /*c0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 144 /*d0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 145 /*e0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 146 /*f0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 147 }; 148 149 /* 150 * Test to see if device is present. 151 * Return true if found and initialized ok. 152 */ 153 rzprobe(sd) 154 register struct scsi_device *sd; 155 { 156 register struct rz_softc *sc = &rz_softc[sd->sd_unit]; 157 register int tries, i; 158 ScsiInquiryData inqbuf; 159 u_char capbuf[8]; 160 ScsiClass7Sense *sp; 161 162 /* init some parameters that don't change */ 163 sc->sc_sd = sd; 164 sc->sc_cmd.sd = sd; 165 sc->sc_cmd.unit = sd->sd_unit; 166 sc->sc_rwcmd.unitNumber = sd->sd_slave; 167 168 /* try to find out what type of device this is */ 169 sc->sc_format_pid = 1; /* force use of sc_cdb */ 170 sc->sc_cdb.len = sizeof(ScsiGroup0Cmd); 171 scsiGroup0Cmd(SCSI_INQUIRY, sd->sd_slave, 0, sizeof(inqbuf), 172 (ScsiGroup0Cmd *)sc->sc_cdb.cdb); 173 sc->sc_buf.b_flags = B_BUSY | B_PHYS | B_READ; 174 sc->sc_buf.b_bcount = sizeof(inqbuf); 175 sc->sc_buf.b_un.b_addr = (caddr_t)&inqbuf; 176 sc->sc_buf.b_actf = (struct buf *)0; 177 sc->sc_tab.b_actf = &sc->sc_buf; 178 rzstart(sd->sd_unit); 179 if (biowait(&sc->sc_buf) || 180 (i = sizeof(inqbuf) - sc->sc_buf.b_resid) < 5) 181 goto bad; 182 switch (inqbuf.type) { 183 case SCSI_DISK_TYPE: /* disk */ 184 case SCSI_WORM_TYPE: /* WORM */ 185 case SCSI_ROM_TYPE: /* CD-ROM */ 186 case SCSI_OPTICAL_MEM_TYPE: /* Magneto-optical */ 187 break; 188 189 default: /* not a disk */ 190 goto bad; 191 } 192 sc->sc_type = inqbuf.type; 193 194 /* see if device is ready */ 195 for (tries = 10; ; ) { 196 sc->sc_cdb.len = sizeof(ScsiGroup0Cmd); 197 scsiGroup0Cmd(SCSI_TEST_UNIT_READY, sd->sd_slave, 0, 0, 198 (ScsiGroup0Cmd *)sc->sc_cdb.cdb); 199 sc->sc_buf.b_flags = B_BUSY | B_PHYS | B_READ; 200 sc->sc_buf.b_bcount = 0; 201 sc->sc_buf.b_un.b_addr = (caddr_t)0; 202 sc->sc_buf.b_actf = (struct buf *)0; 203 sc->sc_tab.b_actf = &sc->sc_buf; 204 205 sc->sc_cmd.cmd = sc->sc_cdb.cdb; 206 sc->sc_cmd.cmdlen = sc->sc_cdb.len; 207 sc->sc_cmd.buf = (caddr_t)0; 208 sc->sc_cmd.buflen = 0; 209 /* setup synchronous data transfers if the device supports it */ 210 if (tries == 10 && (inqbuf.flags & SCSI_SYNC)) 211 sc->sc_cmd.flags = SCSICMD_USE_SYNC; 212 else 213 sc->sc_cmd.flags = 0; 214 215 (*sc->sc_sd->sd_cdriver->d_start)(&sc->sc_cmd); 216 if (!biowait(&sc->sc_buf)) 217 break; 218 if (--tries < 0) 219 goto bad; 220 if (!(sc->sc_sense.status & SCSI_STATUS_CHECKCOND)) 221 goto again; 222 sp = (ScsiClass7Sense *)sc->sc_sense.sense; 223 if (sp->error7 != 0x70) 224 goto again; 225 if (sp->key == SCSI_CLASS7_UNIT_ATTN && tries != 9) { 226 /* drive recalibrating, give it a while */ 227 DELAY(1000000); 228 continue; 229 } 230 if (sp->key == SCSI_CLASS7_NOT_READY) { 231 ScsiStartStopCmd *cp; 232 233 /* try to spin-up disk with start/stop command */ 234 sc->sc_cdb.len = sizeof(ScsiGroup0Cmd); 235 cp = (ScsiStartStopCmd *)sc->sc_cdb.cdb; 236 cp->command = SCSI_START_STOP; 237 cp->unitNumber = sd->sd_slave; 238 cp->immed = 0; 239 cp->loadEject = 0; 240 cp->start = 1; 241 cp->pad1 = 0; 242 cp->pad2 = 0; 243 cp->pad3 = 0; 244 cp->pad4 = 0; 245 cp->control = 0; 246 sc->sc_buf.b_flags = B_BUSY | B_PHYS | B_READ; 247 sc->sc_buf.b_bcount = 0; 248 sc->sc_buf.b_un.b_addr = (caddr_t)0; 249 sc->sc_buf.b_actf = (struct buf *)0; 250 sc->sc_tab.b_actf = &sc->sc_buf; 251 rzstart(sd->sd_unit); 252 if (biowait(&sc->sc_buf)) 253 goto bad; 254 continue; 255 } 256 again: 257 DELAY(1000); 258 } 259 260 /* find out how big a disk this is */ 261 sc->sc_cdb.len = sizeof(ScsiGroup1Cmd); 262 scsiGroup1Cmd(SCSI_READ_CAPACITY, sd->sd_slave, 0, 0, 263 (ScsiGroup1Cmd *)sc->sc_cdb.cdb); 264 sc->sc_buf.b_flags = B_BUSY | B_PHYS | B_READ; 265 sc->sc_buf.b_bcount = sizeof(capbuf); 266 sc->sc_buf.b_un.b_addr = (caddr_t)capbuf; 267 sc->sc_buf.b_actf = (struct buf *)0; 268 sc->sc_tab.b_actf = &sc->sc_buf; 269 rzstart(sd->sd_unit); 270 if (biowait(&sc->sc_buf) || sc->sc_buf.b_resid != 0) 271 goto bad; 272 sc->sc_blks = ((capbuf[0] << 24) | (capbuf[1] << 16) | 273 (capbuf[2] << 8) | capbuf[3]) + 1; 274 sc->sc_blksize = (capbuf[4] << 24) | (capbuf[5] << 16) | 275 (capbuf[6] << 8) | capbuf[7]; 276 277 printf("rz%d at %s%d drive %d slave %d", sd->sd_unit, 278 sd->sd_cdriver->d_name, sd->sd_ctlr, sd->sd_drive, 279 sd->sd_slave); 280 if (inqbuf.version > 1 || i < 36) 281 printf(" type 0x%x, qual 0x%x, ver %d", 282 inqbuf.type, inqbuf.qualifier, inqbuf.version); 283 else { 284 char vid[9], pid[17], revl[5]; 285 286 bcopy((caddr_t)inqbuf.vendorID, (caddr_t)vid, 8); 287 bcopy((caddr_t)inqbuf.productID, (caddr_t)pid, 16); 288 bcopy((caddr_t)inqbuf.revLevel, (caddr_t)revl, 4); 289 for (i = 8; --i > 0; ) 290 if (vid[i] != ' ') 291 break; 292 vid[i+1] = 0; 293 for (i = 16; --i > 0; ) 294 if (pid[i] != ' ') 295 break; 296 pid[i+1] = 0; 297 for (i = 4; --i > 0; ) 298 if (revl[i] != ' ') 299 break; 300 revl[i+1] = 0; 301 printf(" %s %s rev %s", vid, pid, revl); 302 } 303 printf(", %d %d byte blocks\n", sc->sc_blks, sc->sc_blksize); 304 if (sc->sc_blksize != DEV_BSIZE) { 305 if (sc->sc_blksize < DEV_BSIZE) { 306 printf("rz%d: need %d byte blocks - drive ignored\n", 307 sd->sd_unit, DEV_BSIZE); 308 goto bad; 309 } 310 for (i = sc->sc_blksize; i > DEV_BSIZE; i >>= 1) 311 ++sc->sc_bshift; 312 sc->sc_blks <<= sc->sc_bshift; 313 } 314 sc->sc_wpms = 32 * (60 * DEV_BSIZE / 2); /* XXX */ 315 sc->sc_format_pid = 0; 316 sc->sc_flags = RZF_ALIVE; 317 sc->sc_buf.b_flags = 0; 318 return (1); 319 320 bad: 321 /* doesn't exist or not a CCS device */ 322 sc->sc_format_pid = 0; 323 sc->sc_buf.b_flags = 0; 324 return (0); 325 } 326 327 /* 328 * This routine is called for partial block transfers and non-aligned 329 * transfers (the latter only being possible on devices with a block size 330 * larger than DEV_BSIZE). The operation is performed in three steps 331 * using a locally allocated buffer: 332 * 1. transfer any initial partial block 333 * 2. transfer full blocks 334 * 3. transfer any final partial block 335 */ 336 static void 337 rzlblkstrat(bp, bsize) 338 register struct buf *bp; 339 register int bsize; 340 { 341 register struct buf *cbp; 342 caddr_t cbuf; 343 register int bn, resid; 344 register caddr_t addr; 345 346 cbp = (struct buf *)malloc(sizeof(struct buf), M_DEVBUF, M_WAITOK); 347 cbuf = (caddr_t)malloc(bsize, M_DEVBUF, M_WAITOK); 348 bzero((caddr_t)cbp, sizeof(*cbp)); 349 cbp->b_proc = curproc; 350 cbp->b_dev = bp->b_dev; 351 bn = bp->b_blkno; 352 resid = bp->b_bcount; 353 addr = bp->b_un.b_addr; 354 #ifdef DEBUG 355 if (rzdebug & RZB_PARTIAL) 356 printf("rzlblkstrat: bp %x flags %x bn %x resid %x addr %x\n", 357 bp, bp->b_flags, bn, resid, addr); 358 #endif 359 360 while (resid > 0) { 361 register int boff = dbtob(bn) & (bsize - 1); 362 register int count; 363 364 if (boff || resid < bsize) { 365 rz_softc[rzunit(bp->b_dev)].sc_stats.rzpartials++; 366 count = min(resid, bsize - boff); 367 cbp->b_flags = B_BUSY | B_PHYS | B_READ; 368 cbp->b_blkno = bn - btodb(boff); 369 cbp->b_un.b_addr = cbuf; 370 cbp->b_bcount = bsize; 371 #ifdef DEBUG 372 if (rzdebug & RZB_PARTIAL) 373 printf(" readahead: bn %x cnt %x off %x addr %x\n", 374 cbp->b_blkno, count, boff, addr); 375 #endif 376 rzstrategy(cbp); 377 biowait(cbp); 378 if (cbp->b_flags & B_ERROR) { 379 bp->b_flags |= B_ERROR; 380 bp->b_error = cbp->b_error; 381 break; 382 } 383 if (bp->b_flags & B_READ) { 384 bcopy(&cbuf[boff], addr, count); 385 goto done; 386 } 387 bcopy(addr, &cbuf[boff], count); 388 #ifdef DEBUG 389 if (rzdebug & RZB_PARTIAL) 390 printf(" writeback: bn %x cnt %x off %x addr %x\n", 391 cbp->b_blkno, count, boff, addr); 392 #endif 393 } else { 394 count = resid & ~(bsize - 1); 395 cbp->b_blkno = bn; 396 cbp->b_un.b_addr = addr; 397 cbp->b_bcount = count; 398 #ifdef DEBUG 399 if (rzdebug & RZB_PARTIAL) 400 printf(" fulltrans: bn %x cnt %x addr %x\n", 401 cbp->b_blkno, count, addr); 402 #endif 403 } 404 cbp->b_flags = B_BUSY | B_PHYS | (bp->b_flags & B_READ); 405 rzstrategy(cbp); 406 biowait(cbp); 407 if (cbp->b_flags & B_ERROR) { 408 bp->b_flags |= B_ERROR; 409 bp->b_error = cbp->b_error; 410 break; 411 } 412 done: 413 bn += btodb(count); 414 resid -= count; 415 addr += count; 416 #ifdef DEBUG 417 if (rzdebug & RZB_PARTIAL) 418 printf(" done: bn %x resid %x addr %x\n", 419 bn, resid, addr); 420 #endif 421 } 422 free(cbuf, M_DEVBUF); 423 free(cbp, M_DEVBUF); 424 } 425 426 void 427 rzstrategy(bp) 428 register struct buf *bp; 429 { 430 register int unit = rzunit(bp->b_dev); 431 register int part = rzpart(bp->b_dev); 432 register struct rz_softc *sc = &rz_softc[unit]; 433 register struct partition *pp = &sc->sc_label.d_partitions[part]; 434 register daddr_t bn; 435 register long sz, s; 436 437 if (sc->sc_format_pid) { 438 if (sc->sc_format_pid != curproc->p_pid) { 439 bp->b_error = EPERM; 440 goto bad; 441 } 442 bp->b_cylin = 0; 443 } else { 444 bn = bp->b_blkno; 445 sz = howmany(bp->b_bcount, DEV_BSIZE); 446 if ((unsigned)bn + sz > pp->p_size) { 447 sz = pp->p_size - bn; 448 /* if exactly at end of disk, return an EOF */ 449 if (sz == 0) { 450 bp->b_resid = bp->b_bcount; 451 goto done; 452 } 453 /* if none of it fits, error */ 454 if (sz < 0) { 455 bp->b_error = EINVAL; 456 goto bad; 457 } 458 /* otherwise, truncate */ 459 bp->b_bcount = dbtob(sz); 460 } 461 /* check for write to write protected label */ 462 if (bn + pp->p_offset <= LABELSECTOR && 463 #if LABELSECTOR != 0 464 bn + pp->p_offset + sz > LABELSECTOR && 465 #endif 466 !(bp->b_flags & B_READ) && !(sc->sc_flags & RZF_WLABEL)) { 467 bp->b_error = EROFS; 468 goto bad; 469 } 470 /* 471 * Non-aligned or partial-block transfers handled specially. 472 */ 473 s = sc->sc_blksize - 1; 474 if ((dbtob(bn) & s) || (bp->b_bcount & s)) { 475 rzlblkstrat(bp, sc->sc_blksize); 476 goto done; 477 } 478 bp->b_cylin = (bn + pp->p_offset) >> sc->sc_bshift; 479 } 480 /* don't let disksort() see sc_errbuf */ 481 while (sc->sc_flags & RZF_SENSEINPROGRESS) 482 printf("SENSE\n"); /* XXX */ 483 s = splbio(); 484 disksort(&sc->sc_tab, bp); 485 if (sc->sc_tab.b_active == 0) { 486 sc->sc_tab.b_active = 1; 487 rzstart(unit); 488 } 489 splx(s); 490 return; 491 bad: 492 bp->b_flags |= B_ERROR; 493 done: 494 biodone(bp); 495 } 496 497 void 498 rzstart(unit) 499 int unit; 500 { 501 register struct rz_softc *sc = &rz_softc[unit]; 502 register struct buf *bp = sc->sc_tab.b_actf; 503 register int n; 504 505 sc->sc_cmd.buf = bp->b_un.b_addr; 506 sc->sc_cmd.buflen = bp->b_bcount; 507 508 if (sc->sc_format_pid || (sc->sc_flags & RZF_SENSEINPROGRESS)) { 509 sc->sc_cmd.flags = !(bp->b_flags & B_READ) ? 510 SCSICMD_DATA_TO_DEVICE : 0; 511 sc->sc_cmd.cmd = sc->sc_cdb.cdb; 512 sc->sc_cmd.cmdlen = sc->sc_cdb.len; 513 } else { 514 if (bp->b_flags & B_READ) { 515 sc->sc_cmd.flags = 0; 516 sc->sc_rwcmd.command = SCSI_READ_EXT; 517 } else { 518 sc->sc_cmd.flags = SCSICMD_DATA_TO_DEVICE; 519 sc->sc_rwcmd.command = SCSI_WRITE_EXT; 520 } 521 sc->sc_cmd.cmd = (u_char *)&sc->sc_rwcmd; 522 sc->sc_cmd.cmdlen = sizeof(sc->sc_rwcmd); 523 n = bp->b_cylin; 524 sc->sc_rwcmd.highAddr = n >> 24; 525 sc->sc_rwcmd.midHighAddr = n >> 16; 526 sc->sc_rwcmd.midLowAddr = n >> 8; 527 sc->sc_rwcmd.lowAddr = n; 528 n = howmany(bp->b_bcount, sc->sc_blksize); 529 sc->sc_rwcmd.highBlockCount = n >> 8; 530 sc->sc_rwcmd.lowBlockCount = n; 531 #ifdef DEBUG 532 if ((bp->b_bcount & (sc->sc_blksize - 1)) != 0) 533 printf("rz%d: partial block xfer -- %x bytes\n", 534 unit, bp->b_bcount); 535 #endif 536 sc->sc_stats.rztransfers++; 537 if ((n = sc->sc_sd->sd_dk) >= 0) { 538 dk_busy |= 1 << n; 539 ++dk_seek[n]; 540 ++dk_xfer[n]; 541 dk_wds[n] += bp->b_bcount >> 6; 542 } 543 } 544 545 /* tell controller to start this command */ 546 (*sc->sc_sd->sd_cdriver->d_start)(&sc->sc_cmd); 547 } 548 549 /* 550 * This is called by the controller driver when the command is done. 551 */ 552 void 553 rzdone(unit, error, resid, status) 554 register int unit; 555 int error; /* error number from errno.h */ 556 int resid; /* amount not transfered */ 557 int status; /* SCSI status byte */ 558 { 559 register struct rz_softc *sc = &rz_softc[unit]; 560 register struct buf *bp = sc->sc_tab.b_actf; 561 register struct scsi_device *sd = sc->sc_sd; 562 extern int cold; 563 564 if (bp == NULL) { 565 printf("rz%d: bp == NULL\n", unit); 566 return; 567 } 568 if (sd->sd_dk >= 0) 569 dk_busy &= ~(1 << sd->sd_dk); 570 if (sc->sc_flags & RZF_SENSEINPROGRESS) { 571 sc->sc_flags &= ~RZF_SENSEINPROGRESS; 572 sc->sc_tab.b_actf = bp = bp->b_actf; /* remove sc_errbuf */ 573 574 if (error || (status & SCSI_STATUS_CHECKCOND)) { 575 #ifdef DEBUG 576 if (rzdebug & RZB_ERROR) 577 printf("rz%d: error reading sense data: error %d scsi status 0x%x\n", 578 unit, error, status); 579 #endif 580 /* 581 * We got an error during the REQUEST_SENSE, 582 * fill in no sense for data. 583 */ 584 sc->sc_sense.sense[0] = 0x70; 585 sc->sc_sense.sense[2] = SCSI_CLASS7_NO_SENSE; 586 } else if (!cold) { 587 printf("rz%d: ", unit); 588 scsiPrintSense((ScsiClass7Sense *)sc->sc_sense.sense, 589 sizeof(sc->sc_sense.sense) - resid); 590 } 591 } else if (error || (status & SCSI_STATUS_CHECKCOND)) { 592 #ifdef DEBUG 593 if (!cold && (rzdebug & RZB_ERROR)) 594 printf("rz%d: error %d scsi status 0x%x\n", 595 unit, error, status); 596 #endif 597 /* save error info */ 598 sc->sc_sense.status = status; 599 bp->b_flags |= B_ERROR; 600 bp->b_error = error; 601 bp->b_resid = resid; 602 603 if (status & SCSI_STATUS_CHECKCOND) { 604 /* 605 * Start a REQUEST_SENSE command. 606 * Since we are called at interrupt time, we can't 607 * wait for the command to finish; that's why we use 608 * the sc_flags field. 609 */ 610 sc->sc_flags |= RZF_SENSEINPROGRESS; 611 sc->sc_cdb.len = sizeof(ScsiGroup0Cmd); 612 scsiGroup0Cmd(SCSI_REQUEST_SENSE, sd->sd_slave, 0, 613 sizeof(sc->sc_sense.sense), 614 (ScsiGroup0Cmd *)sc->sc_cdb.cdb); 615 sc->sc_errbuf.b_flags = B_BUSY | B_PHYS | B_READ; 616 sc->sc_errbuf.b_bcount = sizeof(sc->sc_sense.sense); 617 sc->sc_errbuf.b_un.b_addr = (caddr_t)sc->sc_sense.sense; 618 sc->sc_errbuf.b_actf = bp; 619 sc->sc_tab.b_actf = &sc->sc_errbuf; 620 rzstart(unit); 621 return; 622 } 623 } else { 624 sc->sc_sense.status = status; 625 bp->b_resid = resid; 626 } 627 628 sc->sc_tab.b_actf = bp->b_actf; 629 biodone(bp); 630 if (sc->sc_tab.b_actf) 631 rzstart(unit); 632 else { 633 sc->sc_tab.b_active = 0; 634 /* finish close protocol */ 635 if (sc->sc_openpart == 0) 636 wakeup((caddr_t)&sc->sc_tab); 637 } 638 } 639 640 int 641 rzopen(dev, flags, mode, p) 642 dev_t dev; 643 int flags, mode; 644 struct proc *p; 645 { 646 register int unit = rzunit(dev); 647 register struct rz_softc *sc = &rz_softc[unit]; 648 register struct disklabel *lp; 649 register int i; 650 char *err_msg; 651 int part; 652 u_long mask; 653 654 if (unit >= NRZ || !(sc->sc_flags & RZF_ALIVE)) 655 return (ENXIO); 656 657 /* try to read disk label and partition table information */ 658 part = rzpart(dev); 659 lp = &sc->sc_label; 660 if (!(sc->sc_flags & RZF_HAVELABEL)) { 661 sc->sc_flags |= RZF_HAVELABEL; 662 lp->d_secsize = DEV_BSIZE; 663 lp->d_secpercyl = 1 << sc->sc_bshift; 664 lp->d_npartitions = MAXPARTITIONS; 665 lp->d_partitions[part].p_offset = 0; 666 lp->d_partitions[part].p_size = sc->sc_blks; 667 if (err_msg = readdisklabel(dev, rzstrategy, lp)) { 668 printf("rz%d: %s\n", unit, err_msg); 669 sc->sc_label.d_magic = DISKMAGIC; 670 sc->sc_label.d_magic2 = DISKMAGIC; 671 sc->sc_label.d_type = DTYPE_SCSI; 672 sc->sc_label.d_subtype = 0; 673 sc->sc_label.d_typename[0] = '\0'; 674 sc->sc_label.d_secsize = DEV_BSIZE; 675 sc->sc_label.d_secperunit = sc->sc_blks; 676 sc->sc_label.d_npartitions = MAXPARTITIONS; 677 sc->sc_label.d_bbsize = BBSIZE; 678 sc->sc_label.d_sbsize = SBSIZE; 679 for (i = 0; i < MAXPARTITIONS; i++) { 680 sc->sc_label.d_partitions[i].p_size = 681 rzdefaultpart[i].nblocks; 682 sc->sc_label.d_partitions[i].p_offset = 683 rzdefaultpart[i].strtblk; 684 } 685 sc->sc_label.d_partitions[RAWPART].p_size = 686 sc->sc_blks; 687 } 688 } 689 690 if (part >= lp->d_npartitions || lp->d_partitions[part].p_size == 0) 691 return (ENXIO); 692 /* 693 * Warn if a partition is opened that overlaps another 694 * already open, unless either is the `raw' partition 695 * (whole disk). 696 */ 697 mask = 1 << part; 698 if ((sc->sc_openpart & mask) == 0 && part != RAWPART) { 699 register struct partition *pp; 700 u_long start, end; 701 702 pp = &lp->d_partitions[part]; 703 start = pp->p_offset; 704 end = pp->p_offset + pp->p_size; 705 for (pp = lp->d_partitions, i = 0; 706 i < lp->d_npartitions; pp++, i++) { 707 if (pp->p_offset + pp->p_size <= start || 708 pp->p_offset >= end || i == RAWPART) 709 continue; 710 if (sc->sc_openpart & (1 << i)) 711 log(LOG_WARNING, 712 "rz%d%c: overlaps open partition (%c)\n", 713 unit, part + 'a', i + 'a'); 714 } 715 } 716 switch (mode) { 717 case S_IFCHR: 718 sc->sc_copenpart |= mask; 719 break; 720 case S_IFBLK: 721 sc->sc_bopenpart |= mask; 722 break; 723 } 724 sc->sc_openpart |= mask; 725 if (sc->sc_sd->sd_dk >= 0) 726 dk_wpms[sc->sc_sd->sd_dk] = sc->sc_wpms; 727 return (0); 728 } 729 730 rzclose(dev, flags, mode) 731 dev_t dev; 732 int flags, mode; 733 { 734 register struct rz_softc *sc = &rz_softc[rzunit(dev)]; 735 u_long mask = (1 << rzpart(dev)); 736 int s; 737 738 switch (mode) { 739 case S_IFCHR: 740 sc->sc_copenpart &= ~mask; 741 break; 742 case S_IFBLK: 743 sc->sc_bopenpart &= ~mask; 744 break; 745 } 746 sc->sc_openpart = sc->sc_copenpart | sc->sc_bopenpart; 747 748 /* 749 * Should wait for I/O to complete on this partition even if 750 * others are open, but wait for work on blkflush(). 751 */ 752 if (sc->sc_openpart == 0) { 753 s = splbio(); 754 while (sc->sc_tab.b_actf) 755 sleep((caddr_t)&sc->sc_tab, PZERO - 1); 756 splx(s); 757 sc->sc_flags &= ~RZF_WLABEL; 758 } 759 return (0); 760 } 761 762 int 763 rzread(dev, uio) 764 dev_t dev; 765 struct uio *uio; 766 { 767 register struct rz_softc *sc = &rz_softc[rzunit(dev)]; 768 769 if (sc->sc_format_pid && sc->sc_format_pid != curproc->p_pid) 770 return (EPERM); 771 772 return (physio(rzstrategy, (struct buf *)0, dev, 773 B_READ, minphys, uio)); 774 } 775 776 int 777 rzwrite(dev, uio) 778 dev_t dev; 779 struct uio *uio; 780 { 781 register struct rz_softc *sc = &rz_softc[rzunit(dev)]; 782 783 if (sc->sc_format_pid && sc->sc_format_pid != curproc->p_pid) 784 return (EPERM); 785 786 return (physio(rzstrategy, (struct buf *)0, dev, 787 B_WRITE, minphys, uio)); 788 } 789 790 int 791 rzioctl(dev, cmd, data, flag, p) 792 dev_t dev; 793 int cmd; 794 caddr_t data; 795 int flag; 796 struct proc *p; 797 { 798 register struct rz_softc *sc = &rz_softc[rzunit(dev)]; 799 int error; 800 int flags; 801 802 switch (cmd) { 803 default: 804 return (EINVAL); 805 806 case SDIOCSFORMAT: 807 /* take this device into or out of "format" mode */ 808 if (suser(p->p_ucred, &p->p_acflag)) 809 return (EPERM); 810 811 if (*(int *)data) { 812 if (sc->sc_format_pid) 813 return (EPERM); 814 sc->sc_format_pid = p->p_pid; 815 } else 816 sc->sc_format_pid = 0; 817 return (0); 818 819 case SDIOCGFORMAT: 820 /* find out who has the device in format mode */ 821 *(int *)data = sc->sc_format_pid; 822 return (0); 823 824 case SDIOCSCSICOMMAND: 825 /* 826 * Save what user gave us as SCSI cdb to use with next 827 * read or write to the char device. 828 */ 829 if (sc->sc_format_pid != p->p_pid) 830 return (EPERM); 831 if (legal_cmds[((struct scsi_fmt_cdb *)data)->cdb[0]] == 0) 832 return (EINVAL); 833 bcopy(data, (caddr_t)&sc->sc_cdb, sizeof(sc->sc_cdb)); 834 return (0); 835 836 case SDIOCSENSE: 837 /* 838 * return the SCSI sense data saved after the last 839 * operation that completed with "check condition" status. 840 */ 841 bcopy((caddr_t)&sc->sc_sense, data, sizeof(sc->sc_sense)); 842 return (0); 843 844 case DIOCGDINFO: 845 /* get the current disk label */ 846 *(struct disklabel *)data = sc->sc_label; 847 return (0); 848 849 case DIOCSDINFO: 850 /* set the current disk label */ 851 if (!(flag & FWRITE)) 852 return (EBADF); 853 error = setdisklabel(&sc->sc_label, 854 (struct disklabel *)data, 855 (sc->sc_flags & RZF_WLABEL) ? 0 : sc->sc_openpart); 856 return (error); 857 858 case DIOCGPART: 859 /* return the disk partition data */ 860 ((struct partinfo *)data)->disklab = &sc->sc_label; 861 ((struct partinfo *)data)->part = 862 &sc->sc_label.d_partitions[rzpart(dev)]; 863 return (0); 864 865 case DIOCWLABEL: 866 if (!(flag & FWRITE)) 867 return (EBADF); 868 if (*(int *)data) 869 sc->sc_flags |= RZF_WLABEL; 870 else 871 sc->sc_flags &= ~RZF_WLABEL; 872 return (0); 873 874 case DIOCWDINFO: 875 /* write the disk label to disk */ 876 if (!(flag & FWRITE)) 877 return (EBADF); 878 error = setdisklabel(&sc->sc_label, 879 (struct disklabel *)data, 880 (sc->sc_flags & RZF_WLABEL) ? 0 : sc->sc_openpart); 881 if (error) 882 return (error); 883 884 /* simulate opening partition 0 so write succeeds */ 885 flags = sc->sc_flags; 886 sc->sc_flags = RZF_ALIVE | RZF_WLABEL; 887 error = writedisklabel(dev, rzstrategy, &sc->sc_label); 888 sc->sc_flags = flags; 889 return (error); 890 } 891 /*NOTREACHED*/ 892 } 893 894 int 895 rzsize(dev) 896 dev_t dev; 897 { 898 register int unit = rzunit(dev); 899 register int part = rzpart(dev); 900 register struct rz_softc *sc = &rz_softc[unit]; 901 902 if (unit >= NRZ || !(sc->sc_flags & RZF_ALIVE) || 903 part >= sc->sc_label.d_npartitions) 904 return (-1); 905 906 return (sc->sc_label.d_partitions[part].p_size); 907 } 908 909 /* 910 * Non-interrupt driven, non-dma dump routine. 911 */ 912 int 913 rzdump(dev) 914 dev_t dev; 915 { 916 #ifdef notdef 917 int part = rzpart(dev); 918 int unit = rzunit(dev); 919 register struct rz_softc *sc = &rz_softc[unit]; 920 register struct scsi_device *sd = sc->sc_hd; 921 register daddr_t baddr; 922 register int maddr; 923 register int pages, i; 924 int stat; 925 extern int lowram; 926 927 /* 928 * Hmm... all vax drivers dump maxfree pages which is physmem minus 929 * the message buffer. Is there a reason for not dumping the 930 * message buffer? Savecore expects to read 'dumpsize' pages of 931 * dump, where dumpsys() sets dumpsize to physmem! 932 */ 933 pages = physmem; 934 935 /* is drive ok? */ 936 if (unit >= NRZ || (sc->sc_flags & RZF_ALIVE) == 0) 937 return (ENXIO); 938 /* dump parameters in range? */ 939 if (dumplo < 0 || dumplo >= sc->sc_info.part[part].nblocks) 940 return (EINVAL); 941 if (dumplo + ctod(pages) > sc->sc_info.part[part].nblocks) 942 pages = dtoc(sc->sc_info.part[part].nblocks - dumplo); 943 maddr = lowram; 944 baddr = dumplo + sc->sc_info.part[part].strtblk; 945 /* scsi bus idle? */ 946 if (!scsireq(&sc->sc_dq)) { 947 scsireset(sd->sd_ctlr); 948 sc->sc_stats.rzresets++; 949 printf("[ drive %d reset ] ", unit); 950 } 951 for (i = 0; i < pages; i++) { 952 #define NPGMB (1024*1024/NBPG) 953 /* print out how many Mbs we have dumped */ 954 if (i && (i % NPGMB) == 0) 955 printf("%d ", i / NPGMB); 956 #undef NPBMG 957 mapin(mmap, (u_int)vmmap, btop(maddr), PG_URKR|PG_CI|PG_V); 958 stat = scsi_tt_write(sd->sd_ctlr, sd->sd_drive, sd->sd_slave, 959 vmmap, NBPG, baddr, sc->sc_bshift); 960 if (stat) { 961 printf("rzdump: scsi write error 0x%x\n", stat); 962 return (EIO); 963 } 964 maddr += NBPG; 965 baddr += ctod(1); 966 } 967 return (0); 968 #else /* notdef */ 969 return (ENXIO); 970 #endif /* notdef */ 971 } 972 #endif 973