1 /* 2 * Copyright (c) 1990, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This software was developed by the Computer Systems Engineering group 6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 7 * contributed to Berkeley. 8 * 9 * All advertising materials mentioning features or use of this software 10 * must display the following acknowledgement: 11 * This product includes software developed by the University of 12 * California, Lawrence Berkeley Laboratory. 13 * 14 * %sccs.include.redist.c% 15 * 16 * @(#)sd.c 8.1 (Berkeley) 06/10/93 17 * 18 * from: $Header: sd.c,v 1.27 93/04/29 01:22:19 torek Exp $ 19 */ 20 21 /* 22 * SCSI CCS (Command Command Set) disk driver. 23 * 24 * MACHINE INDEPENDENT (do not put machine dependent goo in here!) 25 * 26 * (from sd.c,v 1.7 90/12/15 14:11:26 van Exp) 27 */ 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/proc.h> 32 #include <sys/buf.h> 33 #include <sys/errno.h> 34 #include <sys/device.h> 35 #include <sys/disklabel.h> 36 #include <sys/dkstat.h> 37 #include <sys/disk.h> 38 #include <sys/ioctl.h> 39 #include <sys/malloc.h> 40 41 #include <dev/scsi/scsi.h> 42 #include <dev/scsi/disk.h> 43 #include <dev/scsi/scsivar.h> 44 #include <dev/scsi/scsi_ioctl.h> 45 46 #include <machine/cpu.h> 47 48 #include <dev/scsi/sdtrace.h> 49 50 #ifdef sparc /* XXX */ 51 #define SUN_LABEL_HACK /* XXX */ 52 #endif /* XXX */ 53 54 #ifdef SUN_LABEL_HACK 55 #include <sparc/sunos/sun_disklabel.h> 56 #endif 57 58 /* 59 * Per-disk variables. 60 * 61 * sd_dk contains all the `disk' specific stuff (label/partitions, 62 * transfer rate, etc). We need only things that are special to 63 * scsi disks. Note that our blocks are in terms of DEV_BSIZE blocks. 64 */ 65 struct sd_softc { 66 struct dkdevice sc_dk; /* base disk device, must be first */ 67 struct unit sc_unit; /* scsi unit */ 68 pid_t sc_format_pid; /* process using "format" mode */ 69 u_char sc_type; /* drive type */ 70 u_char sc_bshift; /* convert device blocks to DEV_BSIZE blks */ 71 short sc_flags; /* see below */ 72 u_int sc_blks; /* number of blocks on device */ 73 int sc_blksize; /* device block size in bytes */ 74 75 /* should be in dkdevice?? */ 76 struct buf sc_tab; /* transfer queue */ 77 78 /* statistics */ 79 long sc_resets; /* number of times reset */ 80 long sc_transfers; /* count of total transfers */ 81 long sc_partials; /* count of `partial' transfers */ 82 83 /* for user formatting */ 84 struct scsi_cdb sc_cmd; 85 struct scsi_fmt_sense sc_sense; 86 }; 87 88 #define SDF_ALIVE 1 /* drive OK for regular kernel use */ 89 90 /* definition of the autoconfig driver */ 91 int sdmatch __P((struct device *, struct cfdata *, void *)); 92 void sdattach __P((struct device *, struct device *, void *)); 93 94 struct cfdriver sdcd = 95 { NULL, "sd", sdmatch, sdattach, DV_DISK, sizeof(struct sd_softc) }; 96 97 /* definition of the unit driver, for hba */ 98 void sdigo __P((struct device *, struct scsi_cdb *)); 99 void sdgo __P((struct device *, struct scsi_cdb *)); 100 void sdintr __P((struct device *, int, int)); 101 void sdreset __P((struct unit *)); 102 103 static struct unitdriver sdunitdriver = { /*sdgo, sdintr*/ sdreset }; 104 105 /* definition of the disk driver, for kernel */ 106 void sdstrategy __P((struct buf *)); 107 108 static struct dkdriver sddkdriver = { sdstrategy }; 109 110 #ifdef DEBUG 111 int sddebug = 1; 112 #define SDB_ERROR 0x01 113 #define SDB_PARTIAL 0x02 114 #endif 115 116 #define sdunit(x) (minor(x) >> 3) 117 #define sdpart(x) (minor(x) & 0x7) 118 119 #define b_cylin b_resid 120 121 #define SDRETRY 2 122 123 /* 124 * Table of scsi commands users are allowed to access via `format' 125 * mode. 0 means not legal. 1 means `immediate' (doesn't need dma). 126 * -1 means needs dma and/or wait for intr (i.e., `slow'). 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 int 149 sdmatch(parent, cf, aux) 150 struct device *parent; 151 register struct cfdata *cf; 152 void *aux; 153 { 154 register struct scsi_attach_args *sa = aux; 155 #ifdef DEBUG 156 char *whynot; 157 #endif 158 159 /* 160 * unit number must match, or be given as `any' 161 */ 162 if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != sa->sa_unit) 163 return (0); 164 /* 165 * drive must be a disk, and of a kind we recognize 166 */ 167 if ((sa->sa_inq_status & STS_MASK) != STS_GOOD) { 168 #ifdef DEBUG 169 whynot = "INQUIRY failed"; 170 #endif 171 goto notdisk; 172 } 173 174 switch (sa->sa_si.si_type & TYPE_TYPE_MASK) { 175 176 case TYPE_DAD: /* disk */ 177 case TYPE_WORM: /* WORM */ 178 case TYPE_ROM: /* CD-ROM */ 179 case TYPE_MO: /* Magneto-optical */ 180 case TYPE_JUKEBOX: /* medium changer */ 181 break; 182 183 default: 184 notdisk: 185 #ifdef DEBUG 186 whynot = "not a disk"; 187 printf("[not matching `sd' at unit %d: %s]\n", 188 sa->sa_unit, whynot); 189 #endif 190 return (0); 191 } 192 193 /* 194 * It is a disk of some kind; take it. We will figure out 195 * the rest in the attach routine. 196 */ 197 return (1); 198 } 199 200 /* 201 * Attach a disk (called after sdmatch returns true). 202 * Note that this routine is never reentered (so we can use statics). 203 */ 204 void 205 sdattach(parent, self, aux) 206 struct device *parent, *self; 207 void *aux; 208 { 209 register struct sd_softc *sc = (struct sd_softc *)self; 210 register struct scsi_attach_args *sa = aux; 211 register int i; 212 char vendor[10], drive[17], rev[5]; 213 static u_char capbuf[8]; 214 static struct scsi_cdb cap = { CMD_READ_CAPACITY }; 215 #ifdef SUN_LABEL_HACK 216 static struct scsi_cdb rd0 = { CMD_READ10, 0, 0, 0, 0, 0, 0, 0, 1, 0 }; 217 caddr_t sector; 218 #endif 219 220 /* 221 * Declare our existence. 222 */ 223 sc->sc_unit.u_driver = &sdunitdriver; 224 scsi_establish(&sc->sc_unit, &sc->sc_dk.dk_dev, sa->sa_unit); 225 226 /* 227 * Figure out what kind of disk this is. 228 * We only accepted it if the inquiry succeeded, so 229 * we can inspect those fields. 230 */ 231 i = (sa->sa_si.si_version >> VER_ANSI_SHIFT) & VER_ANSI_MASK; 232 if (i == 1 || i == 2) { 233 scsi_inq_ansi((struct scsi_inq_ansi *)&sa->sa_si, 234 vendor, drive, rev); 235 printf(": %s %s", vendor, drive); 236 /* XXX should we even ever bother printing this? */ 237 if (rev[0]) 238 printf(" %s", rev); 239 } else { 240 /* bleah */ 241 bcopy("<unknown>", vendor, 10); 242 bcopy("<unknown>", drive, 10); 243 printf(": type 0x%x, qual 0x%x, ver %d", 244 sa->sa_si.si_type, sa->sa_si.si_qual, 245 sa->sa_si.si_version); 246 } 247 248 CDB10(&cap)->cdb_lun_rel = sc->sc_unit.u_unit << 5; 249 i = (*sc->sc_unit.u_hbd->hd_icmd)(sc->sc_unit.u_hba, 250 sc->sc_unit.u_targ, &cap, (char *)capbuf, sizeof capbuf, B_READ); 251 i &= STS_MASK; 252 if (i == STS_GOOD) { 253 #define NUMBER(p) (((p)[0] << 24) | ((p)[1] << 16) | ((p)[2] << 8) | (p)[3]) 254 sc->sc_blks = NUMBER(&capbuf[0]); 255 sc->sc_blksize = NUMBER(&capbuf[4]); 256 } else if (i == STS_CHECKCOND && 257 (strcmp(vendor, "HP") == 0 && strcmp(drive, "S6300.650A") == 0)) { 258 /* XXX unformatted or nonexistent MO medium: fake it */ 259 sc->sc_blks = 318664; 260 sc->sc_blksize = 1024; 261 } else { 262 /* XXX shouldn't bail for removable media */ 263 printf(": unable to determine drive capacity [sts=%x]\n", i); 264 return; 265 } 266 /* return value from read capacity is last valid block, not nblocks */ 267 sc->sc_blks++; 268 printf(", %u %d byte blocks\n", sc->sc_blks, sc->sc_blksize); 269 if (sc->sc_blksize != DEV_BSIZE) { 270 for (i = sc->sc_blksize; i > DEV_BSIZE; i >>= 1) 271 ++sc->sc_bshift; 272 if (i != DEV_BSIZE) { 273 printf("%s: blksize not multiple of %d: cannot use\n", 274 sc->sc_dk.dk_dev.dv_xname, DEV_BSIZE); 275 return; 276 } 277 sc->sc_blks <<= sc->sc_bshift; 278 } 279 sc->sc_type = sa->sa_si.si_type; /* sufficient? */ 280 281 sc->sc_dk.dk_driver = &sddkdriver; 282 #ifdef notyet 283 dk_establish(&sc->sc_dk); 284 /* READ DISK LABEL HERE, UNLESS REMOVABLE MEDIUM... NEEDS THOUGHT */ 285 #else 286 sc->sc_dk.dk_label.d_secsize = 512; /* XXX */ 287 sc->sc_dk.dk_bps = (3600/60) * 32 * 512;/* XXX */ 288 289 #ifdef SUN_LABEL_HACK 290 sector = (caddr_t)malloc(sc->sc_blksize, M_DEVBUF, M_NOWAIT); 291 CDB10(&rd0)->cdb_lun_rel = sc->sc_unit.u_unit << 5; 292 i = (*sc->sc_unit.u_hbd->hd_icmd)(sc->sc_unit.u_hba, 293 sc->sc_unit.u_targ, &rd0, sector, sc->sc_blksize, B_READ); 294 if (i == STS_GOOD) { 295 printf("%s: <%s>\n", sc->sc_dk.dk_dev.dv_xname, 296 ((struct sun_disklabel *)sector)->sl_text); 297 if (sun_disklabel(sector, &sc->sc_dk.dk_label)) 298 sc->sc_flags |= SDF_ALIVE; 299 else 300 printf("%s: sun_disklabel fails\n", 301 sc->sc_dk.dk_dev.dv_xname); 302 } else 303 printf("%s: could not read sector 0 for disk label\n", 304 sc->sc_dk.dk_dev.dv_xname); 305 free(sector, M_DEVBUF); 306 #endif 307 #endif /* notyet */ 308 } 309 310 /* 311 * Reset a disk, after a SCSI bus reset. 312 * 313 * XXX untested and probably incomplete/incorrect 314 */ 315 void 316 sdreset(u) 317 register struct unit *u; 318 { 319 register struct sd_softc *sc = (struct sd_softc *)u->u_dev; 320 321 printf(" %s", sc->sc_dk.dk_dev.dv_xname); 322 sc->sc_resets++; 323 } 324 325 /* dev_t is short, must use prototype syntax */ 326 int 327 sdopen(dev_t dev, int flags, int ifmt, struct proc *p) 328 { 329 register int unit = sdunit(dev); 330 register struct sd_softc *sc; 331 332 if (unit >= sdcd.cd_ndevs || (sc = sdcd.cd_devs[unit]) == NULL) 333 return (ENXIO); 334 if ((sc->sc_flags & SDF_ALIVE) == 0 && suser(p->p_ucred, &p->p_acflag)) 335 return (ENXIO); 336 return (0); 337 } 338 339 int 340 sdclose(dev_t dev, int flags, int ifmt, struct proc *p) 341 { 342 register struct sd_softc *sc = sdcd.cd_devs[sdunit(dev)]; 343 344 sc->sc_format_pid = 0; 345 return (0); 346 } 347 348 /* 349 * This routine is called for partial block transfers and non-aligned 350 * transfers (the latter only being possible on devices with a block size 351 * larger than DEV_BSIZE). The operation is performed in three steps 352 * using a locally allocated buffer: 353 * 1. transfer any initial partial block 354 * 2. transfer full blocks 355 * 3. transfer any final partial block 356 */ 357 static void 358 sdlblkstrat(bp, bsize) 359 register struct buf *bp; 360 register int bsize; 361 { 362 register int bn, resid, boff, count; 363 register caddr_t addr, cbuf; 364 struct buf *tbp; 365 366 /* should probably use geteblk() here, but I fear consequences */ 367 cbuf = (caddr_t)malloc(bsize, M_DEVBUF, M_WAITOK); 368 tbp = (struct buf *)malloc(sizeof *tbp, M_DEVBUF, M_WAITOK); 369 bzero((caddr_t)tbp, sizeof *tbp); 370 tbp->b_proc = curproc; 371 tbp->b_dev = bp->b_dev; 372 bn = bp->b_blkno; 373 resid = bp->b_bcount; 374 addr = bp->b_un.b_addr; 375 #ifdef DEBUG 376 if (sddebug & SDB_PARTIAL) 377 printf("sdlblkstrat: bp %x flags %x bn %x resid %x addr %x\n", 378 bp, bp->b_flags, bn, resid, addr); 379 #endif 380 381 while (resid > 0) { 382 boff = dbtob(bn) & (bsize - 1); 383 if (boff || resid < bsize) { 384 struct sd_softc *sc = sdcd.cd_devs[sdunit(bp->b_dev)]; 385 sc->sc_partials++; 386 count = min(resid, bsize - boff); 387 tbp->b_flags = B_BUSY | B_READ; 388 tbp->b_blkno = bn - btodb(boff); 389 tbp->b_un.b_addr = cbuf; 390 tbp->b_bcount = bsize; 391 #ifdef DEBUG 392 if (sddebug & SDB_PARTIAL) 393 printf(" readahead: bn %x cnt %x off %x addr %x\n", 394 tbp->b_blkno, count, boff, addr); 395 #endif 396 sdstrategy(tbp); 397 biowait(tbp); 398 if (tbp->b_flags & B_ERROR) { 399 bp->b_flags |= B_ERROR; 400 bp->b_error = tbp->b_error; 401 break; 402 } 403 if (bp->b_flags & B_READ) { 404 bcopy(&cbuf[boff], addr, count); 405 goto done; 406 } 407 bcopy(addr, &cbuf[boff], count); 408 #ifdef DEBUG 409 if (sddebug & SDB_PARTIAL) 410 printf(" writeback: bn %x cnt %x off %x addr %x\n", 411 tbp->b_blkno, count, boff, addr); 412 #endif 413 } else { 414 count = resid & ~(bsize - 1); 415 tbp->b_blkno = bn; 416 tbp->b_un.b_addr = addr; 417 tbp->b_bcount = count; 418 #ifdef DEBUG 419 if (sddebug & SDB_PARTIAL) 420 printf(" fulltrans: bn %x cnt %x addr %x\n", 421 tbp->b_blkno, count, addr); 422 #endif 423 } 424 tbp->b_flags = B_BUSY | (bp->b_flags & B_READ); 425 sdstrategy(tbp); 426 biowait(tbp); 427 if (tbp->b_flags & B_ERROR) { 428 bp->b_flags |= B_ERROR; 429 bp->b_error = tbp->b_error; 430 break; 431 } 432 done: 433 bn += btodb(count); 434 resid -= count; 435 addr += count; 436 #ifdef DEBUG 437 if (sddebug & SDB_PARTIAL) 438 printf(" done: bn %x resid %x addr %x\n", 439 bn, resid, addr); 440 #endif 441 } 442 free(cbuf, M_DEVBUF); 443 free((caddr_t)tbp, M_DEVBUF); 444 biodone(bp); 445 } 446 447 /* 448 * Start a transfer on sc as described by bp 449 * (i.e., call hba or target start). 450 * If in format mode, we may not need dma. 451 */ 452 #define sdstart(sc, bp) { \ 453 SD_TRACE(T_START, sc, bp); \ 454 if ((sc)->sc_format_pid && legal_cmds[(sc)->sc_cmd.cdb_bytes[0]] > 0) \ 455 (*(sc)->sc_unit.u_start)((sc)->sc_unit.u_updev, \ 456 &(sc)->sc_unit.u_forw, (struct buf *)NULL, \ 457 sdigo, &(sc)->sc_dk.dk_dev); \ 458 else \ 459 (*(sc)->sc_unit.u_start)((sc)->sc_unit.u_updev, \ 460 &(sc)->sc_unit.u_forw, bp, sdgo, &(sc)->sc_dk.dk_dev); \ 461 } 462 463 void 464 sdstrategy(bp) 465 register struct buf *bp; 466 { 467 register struct sd_softc *sc = sdcd.cd_devs[sdunit(bp->b_dev)]; 468 register int s; 469 470 if (sc->sc_format_pid) { 471 /* XXXXXXXXX SHOULD NOT COMPARE curproc IN HERE!?! */ 472 /* 473 * In format mode, only allow the owner to mess 474 * with the drive. Skip all the partition checks. 475 */ 476 if (sc->sc_format_pid != curproc->p_pid) { 477 bp->b_error = EPERM; 478 bp->b_flags |= B_ERROR; 479 biodone(bp); 480 return; 481 } 482 bp->b_cylin = 0; 483 } else { 484 register daddr_t bn = bp->b_blkno; 485 register int sz = howmany(bp->b_bcount, DEV_BSIZE); 486 register struct partition *p; 487 488 /* 489 * Make sure transfer is within partition. 490 * If it starts at the end, return EOF; if 491 * it extends past the end, truncate it. 492 */ 493 p = &sc->sc_dk.dk_label.d_partitions[sdpart(bp->b_dev)]; 494 if ((unsigned)bn >= p->p_size) { 495 if ((unsigned)bn > p->p_size) { 496 bp->b_error = EINVAL; 497 bp->b_flags |= B_ERROR; 498 } else 499 bp->b_resid = bp->b_bcount; 500 biodone(bp); 501 return; 502 } 503 if (bn + sz > p->p_size) { 504 sz = p->p_size - bn; 505 bp->b_bcount = dbtob(sz); 506 } 507 /* 508 * Non-aligned or partial-block transfers handled specially. 509 * SHOULD THIS BE AT A HIGHER LEVEL? 510 */ 511 s = sc->sc_blksize - 1; 512 if ((dbtob(bn) & s) || (bp->b_bcount & s)) { 513 sdlblkstrat(bp, sc->sc_blksize); 514 return; 515 } 516 bp->b_cylin = (bn + p->p_offset) >> sc->sc_bshift; 517 } 518 519 /* 520 * Transfer valid, or format mode. Queue the request 521 * on the drive, and maybe try to start it. 522 */ 523 s = splbio(); 524 disksort(&sc->sc_tab, bp); 525 if (sc->sc_tab.b_active == 0) { 526 sc->sc_tab.b_active = 1; 527 sdstart(sc, bp); 528 } 529 splx(s); 530 } 531 532 int 533 sderror(sc, stat) 534 register struct sd_softc *sc; 535 register int stat; 536 { 537 register struct scsi_sense *sn; 538 int retry = 0; 539 540 sc->sc_sense.status = stat; 541 if ((stat & STS_MASK) == STS_CHECKCOND) { 542 sn = (struct scsi_sense *)sc->sc_sense.sense; 543 stat = scsi_request_sense(sc->sc_unit.u_hba, 544 sc->sc_unit.u_targ, sc->sc_unit.u_unit, 545 (caddr_t)sn, sizeof sc->sc_sense.sense); 546 sc->sc_sense.status = stat; /* ??? */ 547 if ((stat & STS_MASK) != STS_GOOD) { 548 printf("%s: sense failed, status %x\n", 549 sc->sc_dk.dk_dev.dv_xname, stat); 550 return (0); 551 } 552 printf("%s: scsi sense class %d, code %d", 553 sc->sc_dk.dk_dev.dv_xname, 554 SENSE_ECLASS(sn), SENSE_ECODE(sn)); 555 if (SENSE_ISXSENSE(sn) && XSENSE_ISSTD(sn)) { 556 int key; 557 558 /* 559 * Standard extended sense: can examine sense key 560 * and (if valid) info. 561 */ 562 key = XSENSE_KEY(sn); 563 printf(", key %d", key); 564 if (XSENSE_IVALID(sn)) 565 printf(", blk %d", XSENSE_INFO(sn)); 566 /* no sense or recovered error, try again */ 567 if (key == 0 || key == 1) 568 retry = 1; 569 } 570 printf("\n"); 571 } 572 return (retry); 573 } 574 575 /* 576 * sdigo is called from the hba driver when it has got the scsi bus 577 * for us, and we were doing a format op that did not need dma. 578 */ 579 void 580 sdigo(sc0, cdb) 581 struct device *sc0; 582 struct scsi_cdb *cdb; 583 { 584 register struct sd_softc *sc = (struct sd_softc *)sc0; 585 register struct buf *bp = sc->sc_tab.b_actf; 586 register int stat; 587 588 stat = (*sc->sc_unit.u_hbd->hd_icmd)(sc->sc_unit.u_hba, 589 sc->sc_unit.u_targ, &sc->sc_cmd, bp->b_un.b_addr, bp->b_bcount, 590 bp->b_flags & B_READ); 591 sc->sc_sense.status = stat; 592 if (stat & 0xfe) { /* XXX */ 593 (void) sderror(sc, stat); 594 bp->b_flags |= B_ERROR; 595 bp->b_error = EIO; 596 } 597 /* 598 * Done with SCSI bus, before we `ought' to be. Release it. 599 */ 600 (*sc->sc_unit.u_rel)(sc->sc_unit.u_updev); 601 bp->b_resid = 0; 602 sc->sc_tab.b_errcnt = 0; 603 sc->sc_tab.b_actf = bp->b_actf; 604 biodone(bp); 605 if ((bp = sc->sc_tab.b_actf) == NULL) 606 sc->sc_tab.b_active = 0; 607 else 608 sdstart(sc, bp); 609 } 610 611 /* 612 * sdgo is called from the hba driver or target code when it has 613 * allocated the scsi bus and DMA resources and target datapath for us. 614 */ 615 void 616 sdgo(sc0, cdb) 617 struct device *sc0; 618 register struct scsi_cdb *cdb; 619 { 620 register struct sd_softc *sc = (struct sd_softc *)sc0; 621 register struct buf *bp = sc->sc_tab.b_actf; 622 register int n; 623 register unsigned int u; 624 625 SD_TRACE(T_MKCDB, sc, bp); 626 if (sc->sc_format_pid) { 627 *cdb = sc->sc_cmd; 628 n = 0; 629 } else { 630 CDB10(cdb)->cdb_cmd = bp->b_flags & B_READ ? CMD_READ10 : 631 CMD_WRITE10; 632 CDB10(cdb)->cdb_lun_rel = sc->sc_unit.u_unit << 5; 633 u = bp->b_cylin; 634 CDB10(cdb)->cdb_lbah = u >> 24; 635 CDB10(cdb)->cdb_lbahm = u >> 16; 636 CDB10(cdb)->cdb_lbalm = u >> 8; 637 CDB10(cdb)->cdb_lbal = u; 638 CDB10(cdb)->cdb_xxx = 0; 639 n = sc->sc_blksize - 1; 640 u = (bp->b_bcount + n) >> (DEV_BSHIFT + sc->sc_bshift); 641 CDB10(cdb)->cdb_lenh = u >> 8; 642 CDB10(cdb)->cdb_lenl = u; 643 CDB10(cdb)->cdb_ctrl = 0; 644 n = (bp->b_bcount & n) != 0; 645 #ifdef DEBUG 646 if (n) 647 printf("%s: partial block xfer -- %x bytes\n", 648 sc->sc_dk.dk_dev.dv_xname, bp->b_bcount); 649 #endif 650 sc->sc_transfers++; 651 } 652 if ((*sc->sc_unit.u_go)(sc->sc_unit.u_updev, sc->sc_unit.u_targ, 653 sdintr, (void *)sc, bp, n) == 0) { 654 #ifdef notyet 655 sc->sc_dk.dk_busy = 1; 656 sc->sc_dk.dk_seek++; /* XXX */ 657 sc->sc_dk.dk_xfer++; 658 sc->sc_dk.dk_wds += bp->b_bcount >> 6; 659 #endif 660 return; 661 } 662 /* 663 * Some sort of nasty unrecoverable error: clobber the 664 * transfer. Call the bus release function first, though. 665 */ 666 (*sc->sc_unit.u_rel)(sc->sc_unit.u_updev); 667 #ifdef DEBUG 668 if (sddebug & SDB_ERROR) 669 printf("%s: sdgo: %s adr %d blk %d len %d ecnt %d\n", 670 sc->sc_dk.dk_dev.dv_xname, 671 bp->b_flags & B_READ? "read" : "write", 672 bp->b_un.b_addr, bp->b_cylin, bp->b_bcount, 673 sc->sc_tab.b_errcnt); 674 #endif 675 bp->b_flags |= B_ERROR; 676 bp->b_error = EIO; 677 bp->b_resid = 0; 678 sc->sc_tab.b_errcnt = 0; 679 sc->sc_tab.b_actf = bp->b_actf; 680 biodone(bp); 681 if ((bp = sc->sc_tab.b_actf) == NULL) 682 sc->sc_tab.b_active = 0; 683 else 684 sdstart(sc, bp); 685 } 686 687 /* 688 * A transfer finished (or, someday, disconnected). 689 * We are already off the target/hba queues. 690 * Restart this one for error recovery, or start the next, as appropriate. 691 */ 692 void 693 sdintr(sc0, stat, resid) 694 struct device *sc0; 695 int stat, resid; 696 { 697 register struct sd_softc *sc = (struct sd_softc *)sc0; 698 register struct buf *bp = sc->sc_tab.b_actf; 699 int retry; 700 701 if (bp == NULL) 702 panic("sdintr"); 703 SD_TRACE(T_INTR, sc, bp); 704 #ifdef notyet 705 sc->sc_dk.dk_busy = 0; 706 #endif 707 if ((stat & STS_MASK) != STS_GOOD) { 708 #ifdef DEBUG 709 if (sddebug & SDB_ERROR) 710 printf("%s: sdintr scsi status 0x%x resid %d\n", 711 sc->sc_dk.dk_dev.dv_xname, stat, resid); 712 #endif 713 retry = sderror(sc, stat); 714 if (retry && ++sc->sc_tab.b_errcnt <= SDRETRY) { 715 printf("%s: retry %d\n", 716 sc->sc_dk.dk_dev.dv_xname, sc->sc_tab.b_errcnt); 717 goto restart; 718 } 719 bp->b_flags |= B_ERROR; 720 bp->b_error = EIO; 721 } 722 bp->b_resid = resid; 723 sc->sc_tab.b_errcnt = 0; 724 sc->sc_tab.b_actf = bp->b_actf; 725 biodone(bp); 726 if ((bp = sc->sc_tab.b_actf) == NULL) 727 sc->sc_tab.b_active = 0; 728 else { 729 restart: 730 sdstart(sc, bp); 731 } 732 } 733 734 int 735 sdioctl(dev_t dev, int cmd, register caddr_t data, int flag, struct proc *p) 736 { 737 register struct sd_softc *sc = sdcd.cd_devs[sdunit(dev)]; 738 #ifdef COMPAT_SUNOS 739 int error; 740 741 error = sun_dkioctl(&sc->sc_dk, cmd, data, sdpart(dev)); 742 if (error >= 0) 743 return (error); 744 #endif 745 switch (cmd) { 746 747 case SDIOCSFORMAT: 748 /* take this device into or out of "format" mode */ 749 if (suser(p->p_ucred, &p->p_acflag)) 750 return (EPERM); 751 if (*(int *)data) { 752 if (sc->sc_format_pid) 753 return (EPERM); 754 sc->sc_format_pid = p->p_pid; 755 } else 756 sc->sc_format_pid = 0; 757 break; 758 759 case SDIOCGFORMAT: 760 /* find out who has the device in format mode */ 761 *(int *)data = sc->sc_format_pid; 762 break; 763 764 case SDIOCSCSICOMMAND: 765 #define cdb ((struct scsi_cdb *)data) 766 /* 767 * Save what user gave us as SCSI cdb to use with next 768 * read or write to the char device. Be sure to replace 769 * the lun field with the actual unit number. 770 */ 771 if (sc->sc_format_pid != p->p_pid) 772 return (EPERM); 773 if (legal_cmds[cdb->cdb_bytes[0]] == 0) 774 return (EINVAL); 775 sc->sc_cmd = *cdb; 776 sc->sc_cmd.cdb_bytes[1] = 777 (sc->sc_cmd.cdb_bytes[1] & ~(7 << 5)) | 778 (sc->sc_unit.u_unit << 5); 779 #undef cdb 780 break; 781 782 case SDIOCSENSE: 783 /* 784 * return the SCSI sense data saved after the last 785 * operation that completed with "check condition" status. 786 */ 787 sc->sc_sense = *(struct scsi_fmt_sense *)data; 788 break; 789 790 case DIOCGDINFO: 791 *(struct disklabel *)data = sc->sc_dk.dk_label; 792 break; 793 794 case DIOCGPART: 795 ((struct partinfo *)data)->disklab = &sc->sc_dk.dk_label; 796 ((struct partinfo *)data)->part = 797 &sc->sc_dk.dk_label.d_partitions[sdpart(dev)]; 798 break; 799 800 default: 801 return (ENOTTY); 802 } 803 return (0); 804 } 805 806 int 807 sdsize(dev_t dev) 808 { 809 register int unit = sdunit(dev); 810 register struct sd_softc *sc; 811 812 if (unit >= sdcd.cd_ndevs || (sc = sdcd.cd_devs[unit]) == NULL || 813 (sc->sc_flags & SDF_ALIVE) == 0) 814 return (-1); 815 return (sc->sc_dk.dk_label.d_partitions[sdpart(dev)].p_size); 816 } 817 818 /* 819 * Write `len' bytes from address `addr' to drive and partition in `dev', 820 * at block blkoff from the beginning of the partition. The address is 821 * either kernel virtual or physical (some machines may never use one or 822 * the other, but we need it in the protocol to stay machine-independent). 823 */ 824 int 825 sddump(dev_t dev, daddr_t blkoff, caddr_t addr, int len) 826 { 827 register struct sd_softc *sc; 828 register struct partition *p; 829 register daddr_t bn, n, nblks; 830 register struct hba_softc *hba; 831 register int stat, unit; 832 struct scsi_cdb cdb; 833 834 /* drive ok? */ 835 unit = sdunit(dev); 836 if (unit >= sdcd.cd_ndevs || (sc = sdcd.cd_devs[unit]) == NULL || 837 (sc->sc_flags & SDF_ALIVE) == 0) 838 return (ENXIO); 839 840 /* blocks in range? */ 841 p = &sc->sc_dk.dk_label.d_partitions[sdpart(dev)]; 842 n = (len + sc->sc_blksize - 1) >> DEV_BSHIFT; 843 if (blkoff < 0 || blkoff >= p->p_size || blkoff + n > p->p_size) 844 return (EINVAL); 845 bn = blkoff + p->p_offset; 846 bn >>= sc->sc_bshift; 847 848 /* scsi bus idle? */ 849 hba = sc->sc_unit.u_hba; 850 if (hba->hba_head) { 851 (*hba->hba_driver->hd_reset)(hba, 0); 852 printf("[reset %s] ", sc->sc_dk.dk_dev.dv_xname); 853 } 854 855 CDB10(&cdb)->cdb_cmd = CMD_WRITE10; 856 CDB10(&cdb)->cdb_lun_rel = sc->sc_unit.u_unit << 5; 857 CDB10(&cdb)->cdb_xxx = 0; 858 CDB10(&cdb)->cdb_ctrl = 0; 859 860 #define DUMP_MAX (32 * 1024) /* no more than 32k per write */ 861 for (;;) { 862 if ((n = len) > DUMP_MAX) 863 n = DUMP_MAX; 864 CDB10(&cdb)->cdb_lbah = bn >> 24; 865 CDB10(&cdb)->cdb_lbahm = bn >> 16; 866 CDB10(&cdb)->cdb_lbalm = bn >> 8; 867 CDB10(&cdb)->cdb_lbal = bn; 868 nblks = n >> (DEV_BSHIFT + sc->sc_bshift); 869 CDB10(&cdb)->cdb_lenh = nblks >> 8; 870 CDB10(&cdb)->cdb_lenl = nblks; 871 stat = hba->hba_driver->hd_dump(hba, sc->sc_unit.u_targ, 872 &cdb, addr, n); 873 if ((stat & STS_MASK) != STS_GOOD) { 874 printf("%s: scsi write error 0x%x\ndump ", 875 sc->sc_dk.dk_dev.dv_xname, stat); 876 return (EIO); 877 } 878 if ((len -= n) == 0) 879 return (0); 880 addr += n; 881 bn += nblks; 882 } 883 } 884