1 /* 2 * Copyright (c) 1990 The 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. 7 * 8 * %sccs.include.redist.c% 9 * 10 * @(#)sd.c 7.16 (Berkeley) 10/11/92 11 */ 12 13 /* 14 * SCSI CCS (Command Command Set) disk driver. 15 */ 16 #include "sd.h" 17 #if NSD > 0 18 19 #ifndef lint 20 static char rcsid[] = "$Header: /usr/src/sys/hp300/dev/RCS/sd.c,v 1.2 92/04/10 20:48:35 mike Exp $"; 21 #endif 22 23 #include <sys/param.h> 24 #include <sys/systm.h> 25 #include <sys/buf.h> 26 #include <sys/dkstat.h> 27 #include <sys/disklabel.h> 28 #include <sys/malloc.h> 29 #include <sys/proc.h> 30 #include <sys/ioctl.h> 31 32 #include <hp/dev/device.h> 33 #include <hp300/dev/scsireg.h> 34 35 #include <vm/vm_param.h> 36 #include <vm/lock.h> 37 #include <vm/vm_prot.h> 38 #include <vm/pmap.h> 39 40 extern int scsi_test_unit_rdy(); 41 extern int scsi_request_sense(); 42 extern int scsi_inquiry(); 43 extern int scsi_read_capacity(); 44 extern int scsi_tt_write(); 45 extern int scsireq(); 46 extern int scsiustart(); 47 extern int scsigo(); 48 extern void scsifree(); 49 extern void scsireset(); 50 extern void scsi_delay(); 51 52 extern void disksort(); 53 extern void biodone(); 54 extern int physio(); 55 extern void TBIS(); 56 57 int sdinit(); 58 void sdstrategy(), sdstart(), sdustart(), sdgo(), sdintr(); 59 60 struct driver sddriver = { 61 sdinit, "sd", (int (*)())sdstart, (int (*)())sdgo, (int (*)())sdintr, 62 }; 63 64 struct size { 65 u_long strtblk; 66 u_long endblk; 67 int nblocks; 68 }; 69 70 struct sdinfo { 71 struct size part[8]; 72 }; 73 74 /* 75 * since the SCSI standard tends to hide the disk structure, we define 76 * partitions in terms of DEV_BSIZE blocks. The default partition table 77 * (for an unlabeled disk) reserves 512K for a boot area, has an 8 meg 78 * root and 32 meg of swap. The rest of the space on the drive goes in 79 * the G partition. As usual, the C partition covers the entire disk 80 * (including the boot area). 81 */ 82 struct sdinfo sddefaultpart = { 83 1024, 17408, 16384 , /* A */ 84 17408, 82944, 65536 , /* B */ 85 0, 0, 0 , /* C */ 86 17408, 115712, 98304 , /* D */ 87 115712, 218112, 102400 , /* E */ 88 218112, 0, 0 , /* F */ 89 82944, 0, 0 , /* G */ 90 115712, 0, 0 , /* H */ 91 }; 92 93 struct sd_softc { 94 struct hp_device *sc_hd; 95 struct devqueue sc_dq; 96 int sc_format_pid; /* process using "format" mode */ 97 short sc_flags; 98 short sc_type; /* drive type */ 99 short sc_punit; /* physical unit (scsi lun) */ 100 u_short sc_bshift; /* convert device blocks to DEV_BSIZE blks */ 101 u_int sc_blks; /* number of blocks on device */ 102 int sc_blksize; /* device block size in bytes */ 103 u_int sc_wpms; /* average xfer rate in 16 bit wds/sec. */ 104 struct sdinfo sc_info; /* drive partition table & label info */ 105 } sd_softc[NSD]; 106 107 /* sc_flags values */ 108 #define SDF_ALIVE 0x1 109 #define SDF_WANTED 0x2 110 #define SDF_RMEDIA 0x4 111 112 #ifdef DEBUG 113 int sddebug = 1; 114 #define SDB_ERROR 0x01 115 #define SDB_PARTIAL 0x02 116 #endif 117 118 struct sdstats { 119 long sdresets; 120 long sdtransfers; 121 long sdpartials; 122 } sdstats[NSD]; 123 124 struct buf sdtab[NSD]; 125 struct scsi_fmt_cdb sdcmd[NSD]; 126 struct scsi_fmt_sense sdsense[NSD]; 127 128 static struct scsi_fmt_cdb sd_read_cmd = { 10, CMD_READ_EXT }; 129 static struct scsi_fmt_cdb sd_write_cmd = { 10, CMD_WRITE_EXT }; 130 131 #define sdunit(x) (minor(x) >> 3) 132 #define sdpart(x) (minor(x) & 0x7) 133 #define sdpunit(x) ((x) & 7) 134 #define b_cylin b_resid 135 136 #define SDRETRY 2 137 138 /* 139 * Table of scsi commands users are allowed to access via "format" 140 * mode. 0 means not legal. 1 means "immediate" (doesn't need dma). 141 * -1 means needs dma and/or wait for intr. 142 */ 143 static char legal_cmds[256] = { 144 /***** 0 1 2 3 4 5 6 7 8 9 A B C D E F */ 145 /*00*/ 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 146 /*10*/ 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 147 /*20*/ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 148 /*30*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 149 /*40*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 150 /*50*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 151 /*60*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 152 /*70*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 153 /*80*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 154 /*90*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155 /*a0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 156 /*b0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 157 /*c0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 158 /*d0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 159 /*e0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 160 /*f0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 161 }; 162 163 static struct scsi_inquiry inqbuf; 164 static struct scsi_fmt_cdb inq = { 165 6, 166 CMD_INQUIRY, 0, 0, 0, sizeof(inqbuf), 0 167 }; 168 169 static u_char capbuf[8]; 170 struct scsi_fmt_cdb cap = { 171 10, 172 CMD_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 173 }; 174 175 static int 176 sdident(sc, hd) 177 struct sd_softc *sc; 178 struct hp_device *hd; 179 { 180 int unit; 181 register int ctlr, slave; 182 register int i; 183 register int tries = 10; 184 char idstr[32]; 185 int ismo = 0; 186 187 ctlr = hd->hp_ctlr; 188 slave = hd->hp_slave; 189 unit = sc->sc_punit; 190 scsi_delay(-1); 191 192 /* 193 * See if unit exists and is a disk then read block size & nblocks. 194 */ 195 while ((i = scsi_test_unit_rdy(ctlr, slave, unit)) != 0) { 196 if (i == -1 || --tries < 0) { 197 if (ismo) 198 break; 199 /* doesn't exist or not a CCS device */ 200 goto failed; 201 } 202 if (i == STS_CHECKCOND) { 203 u_char sensebuf[128]; 204 struct scsi_xsense *sp = (struct scsi_xsense *)sensebuf; 205 206 scsi_request_sense(ctlr, slave, unit, sensebuf, 207 sizeof(sensebuf)); 208 if (sp->class == 7) 209 switch (sp->key) { 210 /* not ready -- might be MO with no media */ 211 case 2: 212 if (sp->len == 12 && 213 sensebuf[12] == 10) /* XXX */ 214 ismo = 1; 215 break; 216 /* drive doing an RTZ -- give it a while */ 217 case 6: 218 DELAY(1000000); 219 break; 220 default: 221 break; 222 } 223 } 224 DELAY(1000); 225 } 226 /* 227 * Find out about device 228 */ 229 if (scsi_immed_command(ctlr, slave, unit, &inq, 230 (u_char *)&inqbuf, sizeof(inqbuf), B_READ)) 231 goto failed; 232 switch (inqbuf.type) { 233 case 0: /* disk */ 234 case 4: /* WORM */ 235 case 5: /* CD-ROM */ 236 case 7: /* Magneto-optical */ 237 break; 238 default: /* not a disk */ 239 goto failed; 240 } 241 /* 242 * Get a usable id string 243 */ 244 if (inqbuf.version != 1) { 245 bcopy("UNKNOWN", &idstr[0], 8); 246 bcopy("DRIVE TYPE", &idstr[8], 11); 247 } else { 248 bcopy((caddr_t)&inqbuf.vendor_id, (caddr_t)idstr, 28); 249 for (i = 27; i > 23; --i) 250 if (idstr[i] != ' ') 251 break; 252 idstr[i+1] = 0; 253 for (i = 23; i > 7; --i) 254 if (idstr[i] != ' ') 255 break; 256 idstr[i+1] = 0; 257 for (i = 7; i >= 0; --i) 258 if (idstr[i] != ' ') 259 break; 260 idstr[i+1] = 0; 261 } 262 i = scsi_immed_command(ctlr, slave, unit, &cap, 263 (u_char *)&capbuf, sizeof(capbuf), B_READ); 264 if (i) { 265 if (i != STS_CHECKCOND || 266 bcmp(&idstr[0], "HP", 3) || 267 bcmp(&idstr[8], "S6300.650A", 11)) 268 goto failed; 269 /* XXX unformatted or non-existant MO media; fake it */ 270 sc->sc_blks = 318664; 271 sc->sc_blksize = 1024; 272 } else { 273 sc->sc_blks = *(u_int *)&capbuf[0]; 274 sc->sc_blksize = *(int *)&capbuf[4]; 275 } 276 /* return value of read capacity is last valid block number */ 277 sc->sc_blks++; 278 279 if (inqbuf.version != 1) 280 printf("sd%d: type 0x%x, qual 0x%x, ver %d", hd->hp_unit, 281 inqbuf.type, inqbuf.qual, inqbuf.version); 282 else 283 printf("sd%d: %s %s rev %s", hd->hp_unit, idstr, &idstr[8], 284 &idstr[24]); 285 printf(", %d %d byte blocks\n", sc->sc_blks, sc->sc_blksize); 286 if (inqbuf.qual & 0x80) 287 sc->sc_flags |= SDF_RMEDIA; 288 if (sc->sc_blksize != DEV_BSIZE) { 289 if (sc->sc_blksize < DEV_BSIZE) { 290 printf("sd%d: need %d byte blocks - drive ignored\n", 291 unit, DEV_BSIZE); 292 goto failed; 293 } 294 for (i = sc->sc_blksize; i > DEV_BSIZE; i >>= 1) 295 ++sc->sc_bshift; 296 sc->sc_blks <<= sc->sc_bshift; 297 } 298 sc->sc_wpms = 32 * (60 * DEV_BSIZE / 2); /* XXX */ 299 scsi_delay(0); 300 return(inqbuf.type); 301 failed: 302 scsi_delay(0); 303 return(-1); 304 } 305 306 int 307 sdinit(hd) 308 register struct hp_device *hd; 309 { 310 register struct sd_softc *sc = &sd_softc[hd->hp_unit]; 311 312 sc->sc_hd = hd; 313 sc->sc_flags = 0; 314 sc->sc_punit = sdpunit(hd->hp_flags); 315 sc->sc_type = sdident(sc, hd); 316 if (sc->sc_type < 0) 317 return(0); 318 sc->sc_dq.dq_ctlr = hd->hp_ctlr; 319 sc->sc_dq.dq_unit = hd->hp_unit; 320 sc->sc_dq.dq_slave = hd->hp_slave; 321 sc->sc_dq.dq_driver = &sddriver; 322 323 /* 324 * If we don't have a disk label, build a default partition 325 * table with 'standard' size root & swap and everything else 326 * in the G partition. 327 */ 328 sc->sc_info = sddefaultpart; 329 /* C gets everything */ 330 sc->sc_info.part[2].nblocks = sc->sc_blks; 331 sc->sc_info.part[2].endblk = sc->sc_blks; 332 /* G gets from end of B to end of disk */ 333 sc->sc_info.part[6].nblocks = sc->sc_blks - sc->sc_info.part[1].endblk; 334 sc->sc_info.part[6].endblk = sc->sc_blks; 335 /* 336 * We also define the D, E and F paritions as an alternative to 337 * B and G. D is 48Mb, starts after A and is intended for swapping. 338 * E is 50Mb, starts after D and is intended for /usr. F starts 339 * after E and is what ever is left. 340 */ 341 if (sc->sc_blks >= sc->sc_info.part[4].endblk) { 342 sc->sc_info.part[5].nblocks = 343 sc->sc_blks - sc->sc_info.part[4].endblk; 344 sc->sc_info.part[5].endblk = sc->sc_blks; 345 } else { 346 sc->sc_info.part[5].strtblk = 0; 347 sc->sc_info.part[3] = sc->sc_info.part[5]; 348 sc->sc_info.part[4] = sc->sc_info.part[5]; 349 } 350 /* 351 * H is a single partition alternative to E and F. 352 */ 353 if (sc->sc_blks >= sc->sc_info.part[3].endblk) { 354 sc->sc_info.part[7].nblocks = 355 sc->sc_blks - sc->sc_info.part[3].endblk; 356 sc->sc_info.part[7].endblk = sc->sc_blks; 357 } else { 358 sc->sc_info.part[7].strtblk = 0; 359 } 360 361 sc->sc_flags |= SDF_ALIVE; 362 return(1); 363 } 364 365 void 366 sdreset(sc, hd) 367 register struct sd_softc *sc; 368 register struct hp_device *hd; 369 { 370 sdstats[hd->hp_unit].sdresets++; 371 } 372 373 int 374 sdopen(dev, flags, mode, p) 375 dev_t dev; 376 int flags, mode; 377 struct proc *p; 378 { 379 register int unit = sdunit(dev); 380 register struct sd_softc *sc = &sd_softc[unit]; 381 382 if (unit >= NSD) 383 return(ENXIO); 384 if ((sc->sc_flags & SDF_ALIVE) == 0 && suser(p->p_ucred, &p->p_acflag)) 385 return(ENXIO); 386 387 if (sc->sc_hd->hp_dk >= 0) 388 dk_wpms[sc->sc_hd->hp_dk] = sc->sc_wpms; 389 return(0); 390 } 391 392 int 393 sdclose(dev, flag, mode, p) 394 dev_t dev; 395 int flag, mode; 396 struct proc *p; 397 { 398 int unit = sdunit(dev); 399 register struct sd_softc *sc = &sd_softc[unit]; 400 int s; 401 402 /* 403 * XXX we should really do this for all drives. 404 */ 405 if (sc->sc_flags & SDF_RMEDIA) { 406 s = splbio(); 407 while (sdtab[unit].b_active) { 408 sc->sc_flags |= SDF_WANTED; 409 sleep((caddr_t)&sdtab[unit], PRIBIO); 410 } 411 splx(s); 412 } 413 sc->sc_format_pid = 0; 414 } 415 416 /* 417 * This routine is called for partial block transfers and non-aligned 418 * transfers (the latter only being possible on devices with a block size 419 * larger than DEV_BSIZE). The operation is performed in three steps 420 * using a locally allocated buffer: 421 * 1. transfer any initial partial block 422 * 2. transfer full blocks 423 * 3. transfer any final partial block 424 */ 425 static void 426 sdlblkstrat(bp, bsize) 427 register struct buf *bp; 428 register int bsize; 429 { 430 register struct buf *cbp = (struct buf *)malloc(sizeof(struct buf), 431 M_DEVBUF, M_WAITOK); 432 caddr_t cbuf = (caddr_t)malloc(bsize, M_DEVBUF, M_WAITOK); 433 register int bn, resid; 434 register caddr_t addr; 435 436 bzero((caddr_t)cbp, sizeof(*cbp)); 437 cbp->b_proc = curproc; /* XXX */ 438 cbp->b_dev = bp->b_dev; 439 bn = bp->b_blkno; 440 resid = bp->b_bcount; 441 addr = bp->b_un.b_addr; 442 #ifdef DEBUG 443 if (sddebug & SDB_PARTIAL) 444 printf("sdlblkstrat: bp %x flags %x bn %x resid %x addr %x\n", 445 bp, bp->b_flags, bn, resid, addr); 446 #endif 447 448 while (resid > 0) { 449 register int boff = dbtob(bn) & (bsize - 1); 450 register int count; 451 452 if (boff || resid < bsize) { 453 sdstats[sdunit(bp->b_dev)].sdpartials++; 454 count = min(resid, bsize - boff); 455 cbp->b_flags = B_BUSY | B_PHYS | B_READ; 456 cbp->b_blkno = bn - btodb(boff); 457 cbp->b_un.b_addr = cbuf; 458 cbp->b_bcount = bsize; 459 #ifdef DEBUG 460 if (sddebug & SDB_PARTIAL) 461 printf(" readahead: bn %x cnt %x off %x addr %x\n", 462 cbp->b_blkno, count, boff, addr); 463 #endif 464 sdstrategy(cbp); 465 biowait(cbp); 466 if (cbp->b_flags & B_ERROR) { 467 bp->b_flags |= B_ERROR; 468 bp->b_error = cbp->b_error; 469 break; 470 } 471 if (bp->b_flags & B_READ) { 472 bcopy(&cbuf[boff], addr, count); 473 goto done; 474 } 475 bcopy(addr, &cbuf[boff], count); 476 #ifdef DEBUG 477 if (sddebug & SDB_PARTIAL) 478 printf(" writeback: bn %x cnt %x off %x addr %x\n", 479 cbp->b_blkno, count, boff, addr); 480 #endif 481 } else { 482 count = resid & ~(bsize - 1); 483 cbp->b_blkno = bn; 484 cbp->b_un.b_addr = addr; 485 cbp->b_bcount = count; 486 #ifdef DEBUG 487 if (sddebug & SDB_PARTIAL) 488 printf(" fulltrans: bn %x cnt %x addr %x\n", 489 cbp->b_blkno, count, addr); 490 #endif 491 } 492 cbp->b_flags = B_BUSY | B_PHYS | (bp->b_flags & B_READ); 493 sdstrategy(cbp); 494 biowait(cbp); 495 if (cbp->b_flags & B_ERROR) { 496 bp->b_flags |= B_ERROR; 497 bp->b_error = cbp->b_error; 498 break; 499 } 500 done: 501 bn += btodb(count); 502 resid -= count; 503 addr += count; 504 #ifdef DEBUG 505 if (sddebug & SDB_PARTIAL) 506 printf(" done: bn %x resid %x addr %x\n", 507 bn, resid, addr); 508 #endif 509 } 510 free(cbuf, M_DEVBUF); 511 free(cbp, M_DEVBUF); 512 } 513 514 void 515 sdstrategy(bp) 516 register struct buf *bp; 517 { 518 register int unit = sdunit(bp->b_dev); 519 register struct sd_softc *sc = &sd_softc[unit]; 520 register struct size *pinfo = &sc->sc_info.part[sdpart(bp->b_dev)]; 521 register struct buf *dp = &sdtab[unit]; 522 register daddr_t bn; 523 register int sz, s; 524 525 if (sc->sc_format_pid) { 526 if (sc->sc_format_pid != curproc->p_pid) { /* XXX */ 527 bp->b_error = EPERM; 528 bp->b_flags |= B_ERROR; 529 goto done; 530 } 531 bp->b_cylin = 0; 532 } else { 533 bn = bp->b_blkno; 534 sz = howmany(bp->b_bcount, DEV_BSIZE); 535 if (bn < 0 || bn + sz > pinfo->nblocks) { 536 sz = pinfo->nblocks - bn; 537 if (sz == 0) { 538 bp->b_resid = bp->b_bcount; 539 goto done; 540 } 541 if (sz < 0) { 542 bp->b_error = EINVAL; 543 bp->b_flags |= B_ERROR; 544 goto done; 545 } 546 bp->b_bcount = dbtob(sz); 547 } 548 /* 549 * Non-aligned or partial-block transfers handled specially. 550 */ 551 s = sc->sc_blksize - 1; 552 if ((dbtob(bn) & s) || (bp->b_bcount & s)) { 553 sdlblkstrat(bp, sc->sc_blksize); 554 goto done; 555 } 556 bp->b_cylin = (bn + pinfo->strtblk) >> sc->sc_bshift; 557 } 558 s = splbio(); 559 disksort(dp, bp); 560 if (dp->b_active == 0) { 561 dp->b_active = 1; 562 sdustart(unit); 563 } 564 splx(s); 565 return; 566 done: 567 biodone(bp); 568 } 569 570 void 571 sdustart(unit) 572 register int unit; 573 { 574 if (scsireq(&sd_softc[unit].sc_dq)) 575 sdstart(unit); 576 } 577 578 /* 579 * Return: 580 * 0 if not really an error 581 * <0 if we should do a retry 582 * >0 if a fatal error 583 */ 584 static int 585 sderror(unit, sc, hp, stat) 586 int unit, stat; 587 register struct sd_softc *sc; 588 register struct hp_device *hp; 589 { 590 int cond = 1; 591 592 sdsense[unit].status = stat; 593 if (stat & STS_CHECKCOND) { 594 struct scsi_xsense *sp; 595 596 scsi_request_sense(hp->hp_ctlr, hp->hp_slave, 597 sc->sc_punit, sdsense[unit].sense, 598 sizeof(sdsense[unit].sense)); 599 sp = (struct scsi_xsense *)sdsense[unit].sense; 600 printf("sd%d: scsi sense class %d, code %d", unit, 601 sp->class, sp->code); 602 if (sp->class == 7) { 603 printf(", key %d", sp->key); 604 if (sp->valid) 605 printf(", blk %d", *(int *)&sp->info1); 606 switch (sp->key) { 607 /* no sense, try again */ 608 case 0: 609 cond = -1; 610 break; 611 /* recovered error, not a problem */ 612 case 1: 613 cond = 0; 614 break; 615 } 616 } 617 printf("\n"); 618 } 619 return(cond); 620 } 621 622 static void 623 sdfinish(unit, sc, bp) 624 int unit; 625 register struct sd_softc *sc; 626 register struct buf *bp; 627 { 628 register struct buf *dp = &sdtab[unit]; 629 630 dp->b_errcnt = 0; 631 dp->b_actf = bp->b_actf; 632 bp->b_resid = 0; 633 biodone(bp); 634 scsifree(&sc->sc_dq); 635 if (dp->b_actf) 636 sdustart(unit); 637 else { 638 dp->b_active = 0; 639 if (sc->sc_flags & SDF_WANTED) { 640 sc->sc_flags &= ~SDF_WANTED; 641 wakeup((caddr_t)dp); 642 } 643 } 644 } 645 646 void 647 sdstart(unit) 648 register int unit; 649 { 650 register struct sd_softc *sc = &sd_softc[unit]; 651 register struct hp_device *hp = sc->sc_hd; 652 653 /* 654 * we have the SCSI bus -- in format mode, we may or may not need dma 655 * so check now. 656 */ 657 if (sc->sc_format_pid && legal_cmds[sdcmd[unit].cdb[0]] > 0) { 658 register struct buf *bp = sdtab[unit].b_actf; 659 register int sts; 660 661 sts = scsi_immed_command(hp->hp_ctlr, hp->hp_slave, 662 sc->sc_punit, &sdcmd[unit], 663 bp->b_un.b_addr, bp->b_bcount, 664 bp->b_flags & B_READ); 665 sdsense[unit].status = sts; 666 if (sts & 0xfe) { 667 (void) sderror(unit, sc, hp, sts); 668 bp->b_flags |= B_ERROR; 669 bp->b_error = EIO; 670 } 671 sdfinish(unit, sc, bp); 672 673 } else if (scsiustart(hp->hp_ctlr)) 674 sdgo(unit); 675 } 676 677 void 678 sdgo(unit) 679 register int unit; 680 { 681 register struct sd_softc *sc = &sd_softc[unit]; 682 register struct hp_device *hp = sc->sc_hd; 683 register struct buf *bp = sdtab[unit].b_actf; 684 register int pad; 685 register struct scsi_fmt_cdb *cmd; 686 687 if (sc->sc_format_pid) { 688 cmd = &sdcmd[unit]; 689 pad = 0; 690 } else { 691 cmd = bp->b_flags & B_READ? &sd_read_cmd : &sd_write_cmd; 692 *(int *)(&cmd->cdb[2]) = bp->b_cylin; 693 pad = howmany(bp->b_bcount, sc->sc_blksize); 694 *(u_short *)(&cmd->cdb[7]) = pad; 695 pad = (bp->b_bcount & (sc->sc_blksize - 1)) != 0; 696 #ifdef DEBUG 697 if (pad) 698 printf("sd%d: partial block xfer -- %x bytes\n", 699 unit, bp->b_bcount); 700 #endif 701 sdstats[unit].sdtransfers++; 702 } 703 if (scsigo(hp->hp_ctlr, hp->hp_slave, sc->sc_punit, bp, cmd, pad) == 0) { 704 if (hp->hp_dk >= 0) { 705 dk_busy |= 1 << hp->hp_dk; 706 ++dk_seek[hp->hp_dk]; 707 ++dk_xfer[hp->hp_dk]; 708 dk_wds[hp->hp_dk] += bp->b_bcount >> 6; 709 } 710 return; 711 } 712 #ifdef DEBUG 713 if (sddebug & SDB_ERROR) 714 printf("sd%d: sdstart: %s adr %d blk %d len %d ecnt %d\n", 715 unit, bp->b_flags & B_READ? "read" : "write", 716 bp->b_un.b_addr, bp->b_cylin, bp->b_bcount, 717 sdtab[unit].b_errcnt); 718 #endif 719 bp->b_flags |= B_ERROR; 720 bp->b_error = EIO; 721 sdfinish(unit, sc, bp); 722 } 723 724 void 725 sdintr(unit, stat) 726 register int unit; 727 int stat; 728 { 729 register struct sd_softc *sc = &sd_softc[unit]; 730 register struct buf *bp = sdtab[unit].b_actf; 731 register struct hp_device *hp = sc->sc_hd; 732 int cond; 733 734 if (bp == NULL) { 735 printf("sd%d: bp == NULL\n", unit); 736 return; 737 } 738 if (hp->hp_dk >= 0) 739 dk_busy &=~ (1 << hp->hp_dk); 740 if (stat) { 741 #ifdef DEBUG 742 if (sddebug & SDB_ERROR) 743 printf("sd%d: sdintr: bad scsi status 0x%x\n", 744 unit, stat); 745 #endif 746 cond = sderror(unit, sc, hp, stat); 747 if (cond) { 748 if (cond < 0 && sdtab[unit].b_errcnt++ < SDRETRY) { 749 #ifdef DEBUG 750 if (sddebug & SDB_ERROR) 751 printf("sd%d: retry #%d\n", 752 unit, sdtab[unit].b_errcnt); 753 #endif 754 sdstart(unit); 755 return; 756 } 757 bp->b_flags |= B_ERROR; 758 bp->b_error = EIO; 759 } 760 } 761 sdfinish(unit, sc, bp); 762 } 763 764 int 765 sdread(dev, uio, flags) 766 dev_t dev; 767 struct uio *uio; 768 int flags; 769 { 770 register int unit = sdunit(dev); 771 register int pid; 772 773 if ((pid = sd_softc[unit].sc_format_pid) && 774 pid != uio->uio_procp->p_pid) 775 return (EPERM); 776 777 return (physio(sdstrategy, NULL, dev, B_READ, minphys, uio)); 778 } 779 780 int 781 sdwrite(dev, uio, flags) 782 dev_t dev; 783 struct uio *uio; 784 int flags; 785 { 786 register int unit = sdunit(dev); 787 register int pid; 788 789 if ((pid = sd_softc[unit].sc_format_pid) && 790 pid != uio->uio_procp->p_pid) 791 return (EPERM); 792 793 return (physio(sdstrategy, NULL, dev, B_WRITE, minphys, uio)); 794 } 795 796 int 797 sdioctl(dev, cmd, data, flag, p) 798 dev_t dev; 799 int cmd; 800 caddr_t data; 801 int flag; 802 struct proc *p; 803 { 804 register int unit = sdunit(dev); 805 register struct sd_softc *sc = &sd_softc[unit]; 806 807 switch (cmd) { 808 default: 809 return (EINVAL); 810 811 case SDIOCSFORMAT: 812 /* take this device into or out of "format" mode */ 813 if (suser(p->p_ucred, &p->p_acflag)) 814 return(EPERM); 815 816 if (*(int *)data) { 817 if (sc->sc_format_pid) 818 return (EPERM); 819 sc->sc_format_pid = p->p_pid; 820 } else 821 sc->sc_format_pid = 0; 822 return (0); 823 824 case SDIOCGFORMAT: 825 /* find out who has the device in format mode */ 826 *(int *)data = sc->sc_format_pid; 827 return (0); 828 829 case SDIOCSCSICOMMAND: 830 /* 831 * Save what user gave us as SCSI cdb to use with next 832 * read or write to the char device. 833 */ 834 if (sc->sc_format_pid != p->p_pid) 835 return (EPERM); 836 if (legal_cmds[((struct scsi_fmt_cdb *)data)->cdb[0]] == 0) 837 return (EINVAL); 838 bcopy(data, (caddr_t)&sdcmd[unit], sizeof(sdcmd[0])); 839 return (0); 840 841 case SDIOCSENSE: 842 /* 843 * return the SCSI sense data saved after the last 844 * operation that completed with "check condition" status. 845 */ 846 bcopy((caddr_t)&sdsense[unit], data, sizeof(sdsense[0])); 847 return (0); 848 849 } 850 /*NOTREACHED*/ 851 } 852 853 int 854 sdsize(dev) 855 dev_t dev; 856 { 857 register int unit = sdunit(dev); 858 register struct sd_softc *sc = &sd_softc[unit]; 859 860 if (unit >= NSD || (sc->sc_flags & SDF_ALIVE) == 0) 861 return(-1); 862 863 return(sc->sc_info.part[sdpart(dev)].nblocks); 864 } 865 866 /* 867 * Non-interrupt driven, non-dma dump routine. 868 */ 869 int 870 sddump(dev) 871 dev_t dev; 872 { 873 int part = sdpart(dev); 874 int unit = sdunit(dev); 875 register struct sd_softc *sc = &sd_softc[unit]; 876 register struct hp_device *hp = sc->sc_hd; 877 register daddr_t baddr; 878 register int maddr; 879 register int pages, i; 880 int stat; 881 extern int lowram; 882 883 /* 884 * Hmm... all vax drivers dump maxfree pages which is physmem minus 885 * the message buffer. Is there a reason for not dumping the 886 * message buffer? Savecore expects to read 'dumpsize' pages of 887 * dump, where dumpsys() sets dumpsize to physmem! 888 */ 889 pages = physmem; 890 891 /* is drive ok? */ 892 if (unit >= NSD || (sc->sc_flags & SDF_ALIVE) == 0) 893 return (ENXIO); 894 /* dump parameters in range? */ 895 if (dumplo < 0 || dumplo >= sc->sc_info.part[part].nblocks) 896 return (EINVAL); 897 if (dumplo + ctod(pages) > sc->sc_info.part[part].nblocks) 898 pages = dtoc(sc->sc_info.part[part].nblocks - dumplo); 899 maddr = lowram; 900 baddr = dumplo + sc->sc_info.part[part].strtblk; 901 /* scsi bus idle? */ 902 if (!scsireq(&sc->sc_dq)) { 903 scsireset(hp->hp_ctlr); 904 sdreset(sc, sc->sc_hd); 905 printf("[ drive %d reset ] ", unit); 906 } 907 for (i = 0; i < pages; i++) { 908 #define NPGMB (1024*1024/NBPG) 909 /* print out how many Mbs we have dumped */ 910 if (i && (i % NPGMB) == 0) 911 printf("%d ", i / NPGMB); 912 #undef NPBMG 913 pmap_enter(kernel_pmap, (vm_offset_t)vmmap, maddr, 914 VM_PROT_READ, TRUE); 915 stat = scsi_tt_write(hp->hp_ctlr, hp->hp_slave, sc->sc_punit, 916 vmmap, NBPG, baddr, sc->sc_bshift); 917 if (stat) { 918 printf("sddump: scsi write error 0x%x\n", stat); 919 return (EIO); 920 } 921 maddr += NBPG; 922 baddr += ctod(1); 923 } 924 return (0); 925 } 926 #endif 927