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