1 /* $OpenBSD: sd.c,v 1.62 2003/06/24 22:42:07 mickey Exp $ */ 2 /* $NetBSD: sd.c,v 1.111 1997/04/02 02:29:41 mycroft Exp $ */ 3 4 /*- 5 * Copyright (c) 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Charles M. Hannum. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Originally written by Julian Elischer (julian@dialix.oz.au) 42 * for TRW Financial Systems for use under the MACH(2.5) operating system. 43 * 44 * TRW Financial Systems, in accordance with their agreement with Carnegie 45 * Mellon University, makes this software available to CMU to distribute 46 * or use in any manner that they see fit as long as this message is kept with 47 * the software. For this reason TFS also grants any other persons or 48 * organisations permission to use or modify this software. 49 * 50 * TFS supplies this software to be publicly redistributed 51 * on the understanding that TFS is not responsible for the correct 52 * functioning of this software in any circumstances. 53 * 54 * Ported to run under 386BSD by Julian Elischer (julian@dialix.oz.au) Sept 1992 55 */ 56 57 #include <sys/types.h> 58 #include <sys/param.h> 59 #include <sys/systm.h> 60 #include <sys/kernel.h> 61 #include <sys/file.h> 62 #include <sys/stat.h> 63 #include <sys/ioctl.h> 64 #include <sys/mtio.h> 65 #include <sys/buf.h> 66 #include <sys/uio.h> 67 #include <sys/malloc.h> 68 #include <sys/errno.h> 69 #include <sys/device.h> 70 #include <sys/disklabel.h> 71 #include <sys/disk.h> 72 #include <sys/proc.h> 73 #include <sys/conf.h> 74 #include <sys/scsiio.h> 75 76 #include <scsi/scsi_all.h> 77 #include <scsi/scsi_disk.h> 78 #include <scsi/scsiconf.h> 79 #include <scsi/sdvar.h> 80 81 #include <ufs/ffs/fs.h> /* for BBSIZE and SBSIZE */ 82 83 #include <sys/vnode.h> 84 85 #define SDUNIT(dev) DISKUNIT(dev) 86 #define SDMINOR(unit, part) DISKMINOR(unit, part) 87 #define SDPART(dev) DISKPART(dev) 88 #define MAKESDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part) 89 90 #define SDLABELDEV(dev) (MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART)) 91 92 int sdmatch(struct device *, void *, void *); 93 void sdattach(struct device *, struct device *, void *); 94 int sdactivate(struct device *, enum devact); 95 int sddetach(struct device *, int); 96 void sdzeroref(struct device *); 97 98 void sdminphys(struct buf *); 99 void sdgetdisklabel(dev_t, struct sd_softc *, struct disklabel *, 100 struct cpu_disklabel *, int); 101 void sdstart(void *); 102 void sddone(struct scsi_xfer *); 103 void sd_shutdown(void *); 104 int sd_reassign_blocks(struct sd_softc *, u_long); 105 int sd_interpret_sense(struct scsi_xfer *); 106 107 void viscpy(u_char *, u_char *, int); 108 109 struct cfattach sd_ca = { 110 sizeof(struct sd_softc), sdmatch, sdattach, 111 sddetach, sdactivate, sdzeroref 112 }; 113 114 struct cfdriver sd_cd = { 115 NULL, "sd", DV_DISK 116 }; 117 118 struct dkdriver sddkdriver = { sdstrategy }; 119 120 struct scsi_device sd_switch = { 121 sd_interpret_sense, /* check out error handler first */ 122 sdstart, /* have a queue, served by this */ 123 NULL, /* have no async handler */ 124 sddone, /* deal with stats at interrupt time */ 125 }; 126 127 const struct scsi_inquiry_pattern sd_patterns[] = { 128 {T_DIRECT, T_FIXED, 129 "", "", ""}, 130 {T_DIRECT, T_REMOV, 131 "", "", ""}, 132 {T_RDIRECT, T_FIXED, 133 "", "", ""}, 134 {T_RDIRECT, T_REMOV, 135 "", "", ""}, 136 {T_OPTICAL, T_FIXED, 137 "", "", ""}, 138 {T_OPTICAL, T_REMOV, 139 "", "", ""}, 140 }; 141 142 extern struct sd_ops sd_scsibus_ops; 143 extern struct sd_ops sd_atapibus_ops; 144 145 #define sdlock(softc) disk_lock(&(softc)->sc_dk) 146 #define sdunlock(softc) disk_unlock(&(softc)->sc_dk) 147 #define sdlookup(unit) (struct sd_softc *)device_lookup(&sd_cd, (unit)) 148 149 int 150 sdmatch(parent, match, aux) 151 struct device *parent; 152 void *match, *aux; 153 { 154 struct scsibus_attach_args *sa = aux; 155 int priority; 156 157 (void)scsi_inqmatch(sa->sa_inqbuf, 158 sd_patterns, sizeof(sd_patterns)/sizeof(sd_patterns[0]), 159 sizeof(sd_patterns[0]), &priority); 160 return (priority); 161 } 162 163 /* 164 * The routine called by the low level scsi routine when it discovers 165 * a device suitable for this driver. 166 */ 167 void 168 sdattach(parent, self, aux) 169 struct device *parent, *self; 170 void *aux; 171 { 172 int error, result; 173 struct sd_softc *sd = (void *)self; 174 struct disk_parms *dp = &sd->params; 175 struct scsibus_attach_args *sa = aux; 176 struct scsi_link *sc_link = sa->sa_sc_link; 177 178 SC_DEBUG(sc_link, SDEV_DB2, ("sdattach: ")); 179 180 /* 181 * Store information needed to contact our base driver 182 */ 183 sd->sc_link = sc_link; 184 sd->type = (sa->sa_inqbuf->device & SID_TYPE); 185 sc_link->device = &sd_switch; 186 sc_link->device_softc = sd; 187 188 /* 189 * Initialize and attach the disk structure. 190 */ 191 sd->sc_dk.dk_driver = &sddkdriver; 192 sd->sc_dk.dk_name = sd->sc_dev.dv_xname; 193 disk_attach(&sd->sc_dk); 194 195 dk_establish(&sd->sc_dk, &sd->sc_dev); 196 197 if (sc_link->flags & SDEV_ATAPI && 198 (sc_link->flags & SDEV_REMOVABLE)) { 199 sd->sc_ops = &sd_atapibus_ops; 200 } else { 201 sd->sc_ops = &sd_scsibus_ops; 202 } 203 204 if (!(sc_link->inquiry_flags & SID_RelAdr)) 205 sc_link->quirks |= SDEV_ONLYBIG; 206 207 /* 208 * Note if this device is ancient. This is used in sdminphys(). 209 */ 210 if (!(sc_link->flags & SDEV_ATAPI) && 211 (sa->sa_inqbuf->version & SID_ANSII) == 0) 212 sd->flags |= SDF_ANCIENT; 213 214 /* 215 * Use the subdriver to request information regarding 216 * the drive. We cannot use interrupts yet, so the 217 * request must specify this. 218 */ 219 printf("\n"); 220 221 if ((sd->sc_link->quirks & SDEV_NOSTARTUNIT) == 0) { 222 error = scsi_start(sd->sc_link, SSS_START, 223 scsi_autoconf | SCSI_IGNORE_ILLEGAL_REQUEST | 224 SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT); 225 } else 226 error = 0; 227 228 /* Fill in name struct for spoofed label */ 229 viscpy(sd->name.vendor, sa->sa_inqbuf->vendor, 8); 230 viscpy(sd->name.product, sa->sa_inqbuf->product, 16); 231 viscpy(sd->name.revision, sa->sa_inqbuf->revision, 4); 232 233 if (error) 234 result = SDGP_RESULT_OFFLINE; 235 else 236 result = (*sd->sc_ops->sdo_get_parms)(sd, &sd->params, 237 scsi_autoconf | SCSI_IGNORE_MEDIA_CHANGE); 238 239 printf("%s: ", sd->sc_dev.dv_xname); 240 switch (result) { 241 case SDGP_RESULT_OK: 242 printf("%ldMB, %d cyl, %d head, %d sec, %d bytes/sec, %ld sec total", 243 dp->disksize / (1048576 / dp->blksize), dp->cyls, 244 dp->heads, dp->sectors, dp->blksize, dp->disksize); 245 break; 246 247 case SDGP_RESULT_OFFLINE: 248 printf("drive offline"); 249 break; 250 251 case SDGP_RESULT_UNFORMATTED: 252 printf("unformatted media"); 253 break; 254 255 #ifdef DIAGNOSTIC 256 default: 257 panic("sdattach: unknown result from get_parms"); 258 break; 259 #endif 260 } 261 printf("\n"); 262 263 /* 264 * Establish a shutdown hook so that we can ensure that 265 * our data has actually made it onto the platter at 266 * shutdown time. Note that this relies on the fact 267 * that the shutdown hook code puts us at the head of 268 * the list (thus guaranteeing that our hook runs before 269 * our ancestors'). 270 */ 271 if ((sd->sc_sdhook = 272 shutdownhook_establish(sd_shutdown, sd)) == NULL) 273 printf("%s: WARNING: unable to establish shutdown hook\n", 274 sd->sc_dev.dv_xname); 275 } 276 277 int 278 sdactivate(self, act) 279 struct device *self; 280 enum devact act; 281 { 282 int rv = 0; 283 284 switch (act) { 285 case DVACT_ACTIVATE: 286 break; 287 288 case DVACT_DEACTIVATE: 289 /* 290 * Nothing to do; we key off the device's DVF_ACTIVATE. 291 */ 292 break; 293 } 294 return (rv); 295 } 296 297 298 int 299 sddetach(self, flags) 300 struct device *self; 301 int flags; 302 { 303 struct sd_softc *sc = (struct sd_softc *)self; 304 struct buf *dp, *bp; 305 int s, bmaj, cmaj, mn; 306 307 /* Remove unprocessed buffers from queue */ 308 s = splbio(); 309 for (dp = &sc->buf_queue; (bp = dp->b_actf) != NULL; ) { 310 dp->b_actf = bp->b_actf; 311 312 bp->b_error = ENXIO; 313 bp->b_flags |= B_ERROR; 314 biodone(bp); 315 } 316 splx(s); 317 318 /* locate the major number */ 319 mn = SDMINOR(self->dv_unit, 0); 320 321 for (bmaj = 0; bmaj < nblkdev; bmaj++) 322 if (bdevsw[bmaj].d_open == sdopen) 323 vdevgone(bmaj, mn, mn + MAXPARTITIONS - 1, VBLK); 324 for (cmaj = 0; cmaj < nchrdev; cmaj++) 325 if (cdevsw[cmaj].d_open == sdopen) 326 vdevgone(cmaj, mn, mn + MAXPARTITIONS - 1, VCHR); 327 328 /* Get rid of the shutdown hook. */ 329 if (sc->sc_sdhook != NULL) 330 shutdownhook_disestablish(sc->sc_sdhook); 331 332 #if NRND > 0 333 /* Unhook the entropy source. */ 334 rnd_detach_source(&sc->rnd_source); 335 #endif 336 337 return (0); 338 } 339 340 void 341 sdzeroref(self) 342 struct device *self; 343 { 344 struct sd_softc *sd = (struct sd_softc *)self; 345 346 /* Detach disk. */ 347 disk_detach(&sd->sc_dk); 348 } 349 350 /* 351 * open the device. Make sure the partition info is a up-to-date as can be. 352 */ 353 int 354 sdopen(dev, flag, fmt, p) 355 dev_t dev; 356 int flag, fmt; 357 struct proc *p; 358 { 359 struct sd_softc *sd; 360 struct scsi_link *sc_link; 361 int unit, part; 362 int error; 363 364 unit = SDUNIT(dev); 365 sd = sdlookup(unit); 366 if (sd == NULL) 367 return ENXIO; 368 369 sc_link = sd->sc_link; 370 part = SDPART(dev); 371 372 SC_DEBUG(sc_link, SDEV_DB1, 373 ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit, 374 sd_cd.cd_ndevs, part)); 375 376 if ((error = sdlock(sd)) != 0) { 377 device_unref(&sd->sc_dev); 378 return error; 379 } 380 381 if (sd->sc_dk.dk_openmask != 0) { 382 /* 383 * If any partition is open, but the disk has been invalidated, 384 * disallow further opens. 385 */ 386 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 387 error = EIO; 388 goto bad3; 389 } 390 } else { 391 /* Check that it is still responding and ok. */ 392 error = scsi_test_unit_ready(sc_link, 393 TEST_READY_RETRIES_DEFAULT, 394 SCSI_IGNORE_ILLEGAL_REQUEST | 395 SCSI_IGNORE_MEDIA_CHANGE | 396 SCSI_IGNORE_NOT_READY); 397 if (error) 398 goto bad3; 399 400 /* Start the pack spinning if necessary. */ 401 if ((sc_link->quirks & SDEV_NOSTARTUNIT) == 0) { 402 error = scsi_start(sc_link, SSS_START, 403 SCSI_IGNORE_ILLEGAL_REQUEST | 404 SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT); 405 if (error) 406 goto bad3; 407 } 408 409 sc_link->flags |= SDEV_OPEN; 410 411 /* Lock the pack in. */ 412 error = scsi_prevent(sc_link, PR_PREVENT, 413 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE); 414 if (error) 415 goto bad; 416 417 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 418 sc_link->flags |= SDEV_MEDIA_LOADED; 419 420 /* Load the physical device parameters. */ 421 if ((*sd->sc_ops->sdo_get_parms)(sd, &sd->params, 422 0) == SDGP_RESULT_OFFLINE) { 423 error = ENXIO; 424 goto bad2; 425 } 426 SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded ")); 427 428 /* Load the partition info if not already loaded. */ 429 sdgetdisklabel(dev, sd, sd->sc_dk.dk_label, 430 sd->sc_dk.dk_cpulabel, 0); 431 SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel loaded ")); 432 } 433 } 434 435 /* Check that the partition exists. */ 436 if (part != RAW_PART && 437 (part >= sd->sc_dk.dk_label->d_npartitions || 438 sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) { 439 error = ENXIO; 440 goto bad; 441 } 442 443 /* Insure only one open at a time. */ 444 switch (fmt) { 445 case S_IFCHR: 446 sd->sc_dk.dk_copenmask |= (1 << part); 447 break; 448 case S_IFBLK: 449 sd->sc_dk.dk_bopenmask |= (1 << part); 450 break; 451 } 452 sd->sc_dk.dk_openmask = sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask; 453 454 SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n")); 455 sdunlock(sd); 456 device_unref(&sd->sc_dev); 457 return 0; 458 459 bad2: 460 sc_link->flags &= ~SDEV_MEDIA_LOADED; 461 462 bad: 463 if (sd->sc_dk.dk_openmask == 0) { 464 scsi_prevent(sc_link, PR_ALLOW, 465 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE); 466 sc_link->flags &= ~SDEV_OPEN; 467 } 468 469 bad3: 470 sdunlock(sd); 471 device_unref(&sd->sc_dev); 472 return error; 473 } 474 475 /* 476 * close the device.. only called if we are the LAST occurence of an open 477 * device. Convenient now but usually a pain. 478 */ 479 int 480 sdclose(dev, flag, fmt, p) 481 dev_t dev; 482 int flag, fmt; 483 struct proc *p; 484 { 485 struct sd_softc *sd; 486 int part = SDPART(dev); 487 int error; 488 489 sd = sdlookup(SDUNIT(dev)); 490 if (sd == NULL) 491 return ENXIO; 492 493 if ((error = sdlock(sd)) != 0) 494 return error; 495 496 switch (fmt) { 497 case S_IFCHR: 498 sd->sc_dk.dk_copenmask &= ~(1 << part); 499 break; 500 case S_IFBLK: 501 sd->sc_dk.dk_bopenmask &= ~(1 << part); 502 break; 503 } 504 sd->sc_dk.dk_openmask = sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask; 505 506 if (sd->sc_dk.dk_openmask == 0) { 507 if ((sd->flags & SDF_DIRTY) != 0 && 508 sd->sc_ops->sdo_flush != NULL) 509 (*sd->sc_ops->sdo_flush)(sd, 0); 510 511 scsi_prevent(sd->sc_link, PR_ALLOW, 512 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY); 513 sd->sc_link->flags &= ~(SDEV_OPEN|SDEV_MEDIA_LOADED); 514 515 if (sd->sc_link->flags & SDEV_EJECTING) { 516 scsi_start(sd->sc_link, SSS_STOP|SSS_LOEJ, 0); 517 518 sd->sc_link->flags &= ~SDEV_EJECTING; 519 } 520 } 521 522 sdunlock(sd); 523 device_unref(&sd->sc_dev); 524 return 0; 525 } 526 527 /* 528 * Actually translate the requested transfer into one the physical driver 529 * can understand. The transfer is described by a buf and will include 530 * only one physical transfer. 531 */ 532 void 533 sdstrategy(bp) 534 struct buf *bp; 535 { 536 struct sd_softc *sd; 537 int s; 538 539 sd = sdlookup(SDUNIT(bp->b_dev)); 540 if (sd == NULL) { 541 bp->b_error = ENXIO; 542 goto bad; 543 } 544 545 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdstrategy ")); 546 SC_DEBUG(sd->sc_link, SDEV_DB1, 547 ("%ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno)); 548 /* 549 * The transfer must be a whole number of blocks. 550 */ 551 if ((bp->b_bcount % sd->sc_dk.dk_label->d_secsize) != 0) { 552 bp->b_error = EINVAL; 553 goto bad; 554 } 555 /* 556 * If the device has been made invalid, error out 557 */ 558 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 559 if (sd->sc_link->flags & SDEV_OPEN) 560 bp->b_error = EIO; 561 else 562 bp->b_error = ENODEV; 563 goto bad; 564 } 565 /* 566 * If it's a null transfer, return immediatly 567 */ 568 if (bp->b_bcount == 0) 569 goto done; 570 571 /* 572 * Do bounds checking, adjust transfer. if error, process. 573 * If end of partition, just return. 574 */ 575 if (SDPART(bp->b_dev) != RAW_PART && 576 bounds_check_with_label(bp, sd->sc_dk.dk_label, 577 sd->sc_dk.dk_cpulabel, 578 (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0) 579 goto done; 580 581 s = splbio(); 582 583 /* 584 * Place it in the queue of disk activities for this disk 585 */ 586 disksort(&sd->buf_queue, bp); 587 588 /* 589 * Tell the device to get going on the transfer if it's 590 * not doing anything, otherwise just wait for completion 591 */ 592 sdstart(sd); 593 594 splx(s); 595 596 device_unref(&sd->sc_dev); 597 return; 598 599 bad: 600 bp->b_flags |= B_ERROR; 601 done: 602 /* 603 * Correctly set the buf to indicate a completed xfer 604 */ 605 bp->b_resid = bp->b_bcount; 606 s = splbio(); 607 biodone(bp); 608 splx(s); 609 if (sd != NULL) 610 device_unref(&sd->sc_dev); 611 } 612 613 /* 614 * sdstart looks to see if there is a buf waiting for the device 615 * and that the device is not already busy. If both are true, 616 * It dequeues the buf and creates a scsi command to perform the 617 * transfer in the buf. The transfer request will call scsi_done 618 * on completion, which will in turn call this routine again 619 * so that the next queued transfer is performed. 620 * The bufs are queued by the strategy routine (sdstrategy) 621 * 622 * This routine is also called after other non-queued requests 623 * have been made of the scsi driver, to ensure that the queue 624 * continues to be drained. 625 * 626 * must be called at the correct (highish) spl level 627 * sdstart() is called at splbio from sdstrategy and scsi_done 628 */ 629 void 630 sdstart(v) 631 register void *v; 632 { 633 register struct sd_softc *sd = v; 634 register struct scsi_link *sc_link = sd->sc_link; 635 struct buf *bp = 0; 636 struct buf *dp; 637 struct scsi_rw_big cmd_big; 638 struct scsi_rw cmd_small; 639 struct scsi_generic *cmdp; 640 int blkno, nblks, cmdlen, error; 641 struct partition *p; 642 643 SC_DEBUG(sc_link, SDEV_DB2, ("sdstart ")); 644 645 splassert(IPL_BIO); 646 647 /* 648 * Check if the device has room for another command 649 */ 650 while (sc_link->openings > 0) { 651 /* 652 * there is excess capacity, but a special waits 653 * It'll need the adapter as soon as we clear out of the 654 * way and let it run (user level wait). 655 */ 656 if (sc_link->flags & SDEV_WAITING) { 657 sc_link->flags &= ~SDEV_WAITING; 658 wakeup((caddr_t)sc_link); 659 return; 660 } 661 662 /* 663 * See if there is a buf with work for us to do.. 664 */ 665 dp = &sd->buf_queue; 666 if ((bp = dp->b_actf) == NULL) /* yes, an assign */ 667 return; 668 dp->b_actf = bp->b_actf; 669 670 /* 671 * If the device has become invalid, abort all the 672 * reads and writes until all files have been closed and 673 * re-opened 674 */ 675 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 676 bp->b_error = EIO; 677 bp->b_flags |= B_ERROR; 678 bp->b_resid = bp->b_bcount; 679 biodone(bp); 680 continue; 681 } 682 683 /* 684 * We have a buf, now we should make a command 685 * 686 * First, translate the block to absolute and put it in terms 687 * of the logical blocksize of the device. 688 */ 689 blkno = 690 bp->b_blkno / (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE); 691 if (SDPART(bp->b_dev) != RAW_PART) { 692 p = &sd->sc_dk.dk_label->d_partitions[SDPART(bp->b_dev)]; 693 blkno += p->p_offset; 694 } 695 nblks = howmany(bp->b_bcount, sd->sc_dk.dk_label->d_secsize); 696 697 /* 698 * Fill out the scsi command. If the transfer will 699 * fit in a "small" cdb, use it. 700 */ 701 if (!(sc_link->flags & SDEV_ATAPI) && 702 !(sc_link->quirks & SDEV_ONLYBIG) && 703 ((blkno & 0x1fffff) == blkno) && 704 ((nblks & 0xff) == nblks)) { 705 /* 706 * We can fit in a small cdb. 707 */ 708 bzero(&cmd_small, sizeof(cmd_small)); 709 cmd_small.opcode = (bp->b_flags & B_READ) ? 710 READ_COMMAND : WRITE_COMMAND; 711 _lto3b(blkno, cmd_small.addr); 712 cmd_small.length = nblks & 0xff; 713 cmdlen = sizeof(cmd_small); 714 cmdp = (struct scsi_generic *)&cmd_small; 715 } else { 716 /* 717 * Need a large cdb. 718 */ 719 bzero(&cmd_big, sizeof(cmd_big)); 720 cmd_big.opcode = (bp->b_flags & B_READ) ? 721 READ_BIG : WRITE_BIG; 722 _lto4b(blkno, cmd_big.addr); 723 _lto2b(nblks, cmd_big.length); 724 cmdlen = sizeof(cmd_big); 725 cmdp = (struct scsi_generic *)&cmd_big; 726 } 727 728 /* Instrumentation. */ 729 disk_busy(&sd->sc_dk); 730 731 /* 732 * Mark the disk dirty so that the cache will be 733 * flushed on close. 734 */ 735 if ((bp->b_flags & B_READ) == 0) 736 sd->flags |= SDF_DIRTY; 737 738 739 /* 740 * Call the routine that chats with the adapter. 741 * Note: we cannot sleep as we may be an interrupt 742 */ 743 error = scsi_scsi_cmd(sc_link, cmdp, cmdlen, 744 (u_char *)bp->b_data, bp->b_bcount, 745 SDRETRIES, 60000, bp, SCSI_NOSLEEP | 746 ((bp->b_flags & B_READ) ? SCSI_DATA_IN : SCSI_DATA_OUT)); 747 if (error) { 748 disk_unbusy(&sd->sc_dk, 0); 749 printf("%s: not queued, error %d\n", 750 sd->sc_dev.dv_xname, error); 751 } 752 } 753 } 754 755 void 756 sddone(xs) 757 struct scsi_xfer *xs; 758 { 759 struct sd_softc *sd = xs->sc_link->device_softc; 760 761 if (sd->flags & SDF_FLUSHING) { 762 /* Flush completed, no longer dirty. */ 763 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 764 } 765 766 if (xs->bp != NULL) 767 disk_unbusy(&sd->sc_dk, (xs->bp->b_bcount - xs->bp->b_resid)); 768 } 769 770 void 771 sdminphys(bp) 772 struct buf *bp; 773 { 774 struct sd_softc *sd; 775 long max; 776 777 sd = sdlookup(SDUNIT(bp->b_dev)); 778 if (sd == NULL) 779 return; /* XXX - right way to fail this? */ 780 781 /* 782 * If the device is ancient, we want to make sure that 783 * the transfer fits into a 6-byte cdb. 784 * 785 * XXX Note that the SCSI-I spec says that 256-block transfers 786 * are allowed in a 6-byte read/write, and are specified 787 * by settng the "length" to 0. However, we're conservative 788 * here, allowing only 255-block transfers in case an 789 * ancient device gets confused by length == 0. A length of 0 790 * in a 10-byte read/write actually means 0 blocks. 791 */ 792 if (sd->flags & SDF_ANCIENT) { 793 max = sd->sc_dk.dk_label->d_secsize * 0xff; 794 795 if (bp->b_bcount > max) 796 bp->b_bcount = max; 797 } 798 799 (*sd->sc_link->adapter->scsi_minphys)(bp); 800 801 device_unref(&sd->sc_dev); 802 } 803 804 int 805 sdread(dev, uio, ioflag) 806 dev_t dev; 807 struct uio *uio; 808 int ioflag; 809 { 810 811 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio)); 812 } 813 814 int 815 sdwrite(dev, uio, ioflag) 816 dev_t dev; 817 struct uio *uio; 818 int ioflag; 819 { 820 821 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio)); 822 } 823 824 /* 825 * Perform special action on behalf of the user 826 * Knows about the internals of this device 827 */ 828 int 829 sdioctl(dev, cmd, addr, flag, p) 830 dev_t dev; 831 u_long cmd; 832 caddr_t addr; 833 int flag; 834 struct proc *p; 835 { 836 struct sd_softc *sd; 837 int error = 0; 838 int part = SDPART(dev); 839 840 sd = sdlookup(SDUNIT(dev)); 841 if (sd == NULL) 842 return ENXIO; 843 844 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdioctl 0x%lx ", cmd)); 845 846 /* 847 * If the device is not valid.. abandon ship 848 */ 849 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 850 switch (cmd) { 851 case DIOCWLABEL: 852 case DIOCLOCK: 853 case DIOCEJECT: 854 case SCIOCIDENTIFY: 855 case OSCIOCIDENTIFY: 856 case SCIOCCOMMAND: 857 case SCIOCDEBUG: 858 if (part == RAW_PART) 859 break; 860 /* FALLTHROUGH */ 861 default: 862 if ((sd->sc_link->flags & SDEV_OPEN) == 0) { 863 error = ENODEV; 864 goto exit; 865 } else { 866 error = EIO; 867 goto exit; 868 } 869 } 870 } 871 872 switch (cmd) { 873 case DIOCRLDINFO: 874 sdgetdisklabel(dev, sd, sd->sc_dk.dk_label, 875 sd->sc_dk.dk_cpulabel, 0); 876 goto exit; 877 case DIOCGPDINFO: { 878 struct cpu_disklabel osdep; 879 880 sdgetdisklabel(dev, sd, (struct disklabel *)addr, 881 &osdep, 1); 882 goto exit; 883 } 884 885 case DIOCGDINFO: 886 *(struct disklabel *)addr = *(sd->sc_dk.dk_label); 887 goto exit; 888 889 case DIOCGPART: 890 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label; 891 ((struct partinfo *)addr)->part = 892 &sd->sc_dk.dk_label->d_partitions[SDPART(dev)]; 893 goto exit; 894 895 case DIOCWDINFO: 896 case DIOCSDINFO: 897 if ((flag & FWRITE) == 0) { 898 error = EBADF; 899 goto exit; 900 } 901 902 if ((error = sdlock(sd)) != 0) 903 goto exit; 904 sd->flags |= SDF_LABELLING; 905 906 error = setdisklabel(sd->sc_dk.dk_label, 907 (struct disklabel *)addr, /*sd->sc_dk.dk_openmask : */0, 908 sd->sc_dk.dk_cpulabel); 909 if (error == 0) { 910 if (cmd == DIOCWDINFO) 911 error = writedisklabel(SDLABELDEV(dev), 912 sdstrategy, sd->sc_dk.dk_label, 913 sd->sc_dk.dk_cpulabel); 914 } 915 916 sd->flags &= ~SDF_LABELLING; 917 sdunlock(sd); 918 goto exit; 919 920 case DIOCWLABEL: 921 if ((flag & FWRITE) == 0) { 922 error = EBADF; 923 goto exit; 924 } 925 if (*(int *)addr) 926 sd->flags |= SDF_WLABEL; 927 else 928 sd->flags &= ~SDF_WLABEL; 929 goto exit; 930 931 case DIOCLOCK: 932 error = scsi_prevent(sd->sc_link, 933 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0); 934 goto exit; 935 936 case MTIOCTOP: 937 if (((struct mtop *)addr)->mt_op != MTOFFL) { 938 error = EIO; 939 goto exit; 940 } 941 /* FALLTHROUGH */ 942 case DIOCEJECT: 943 if ((sd->sc_link->flags & SDEV_REMOVABLE) == 0) { 944 error = ENOTTY; 945 goto exit; 946 } 947 sd->sc_link->flags |= SDEV_EJECTING; 948 goto exit; 949 950 case SCIOCREASSIGN: 951 if ((flag & FWRITE) == 0) { 952 error = EBADF; 953 goto exit; 954 } 955 error = sd_reassign_blocks(sd, (*(int *)addr)); 956 goto exit; 957 958 default: 959 if (part != RAW_PART) { 960 error = ENOTTY; 961 goto exit; 962 } 963 error = scsi_do_ioctl(sd->sc_link, dev, cmd, addr, flag, p); 964 } 965 966 exit: 967 device_unref(&sd->sc_dev); 968 return (error); 969 } 970 971 /* 972 * Load the label information on the named device 973 */ 974 void 975 sdgetdisklabel(dev, sd, lp, clp, spoofonly) 976 dev_t dev; 977 struct sd_softc *sd; 978 struct disklabel *lp; 979 struct cpu_disklabel *clp; 980 int spoofonly; 981 { 982 size_t len; 983 char *errstring, packname[sizeof(lp->d_packname) + 1]; 984 985 bzero(lp, sizeof(struct disklabel)); 986 bzero(clp, sizeof(struct cpu_disklabel)); 987 988 lp->d_secsize = sd->params.blksize; 989 lp->d_ntracks = sd->params.heads; 990 lp->d_nsectors = sd->params.sectors; 991 lp->d_ncylinders = sd->params.cyls; 992 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 993 if (lp->d_secpercyl == 0) { 994 lp->d_secpercyl = 100; 995 /* as long as it's not 0 - readdisklabel divides by it */ 996 } 997 998 lp->d_type = DTYPE_SCSI; 999 if (sd->type == T_OPTICAL) 1000 strncpy(lp->d_typename, "SCSI optical", 1001 sizeof(lp->d_typename)); 1002 else 1003 strncpy(lp->d_typename, "SCSI disk", 1004 sizeof(lp->d_typename)); 1005 1006 /* 1007 * Try to fit '<vendor> <product>' into d_packname. If that doesn't fit 1008 * then leave out '<vendor> ' and use only as much of '<product>' as 1009 * does fit. 1010 */ 1011 len = snprintf(packname, sizeof(packname), "%s %s", 1012 sd->name.vendor, sd->name.product); 1013 if (len > sizeof(lp->d_packname)) { 1014 strlcpy(packname, sd->name.product, sizeof(packname)); 1015 len = strlen(packname); 1016 } 1017 /* 1018 * It is safe to use len as the count of characters to copy because 1019 * packname is sizeof(lp->d_packname)+1, the string in packname is 1020 * always null terminated and len does not count the terminating null. 1021 * d_packname is not a null terminated string. 1022 */ 1023 bcopy(packname, lp->d_packname, len); 1024 1025 lp->d_secperunit = sd->params.disksize; 1026 lp->d_rpm = sd->params.rot_rate; 1027 lp->d_interleave = 1; 1028 lp->d_flags = 0; 1029 1030 /* XXX - these values for BBSIZE and SBSIZE assume ffs */ 1031 lp->d_bbsize = BBSIZE; 1032 lp->d_sbsize = SBSIZE; 1033 1034 lp->d_partitions[RAW_PART].p_offset = 0; 1035 lp->d_partitions[RAW_PART].p_size = 1036 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 1037 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; 1038 lp->d_npartitions = RAW_PART + 1; 1039 1040 lp->d_magic = DISKMAGIC; 1041 lp->d_magic2 = DISKMAGIC; 1042 lp->d_checksum = dkcksum(lp); 1043 1044 /* 1045 * Call the generic disklabel extraction routine 1046 */ 1047 errstring = readdisklabel(SDLABELDEV(dev), sdstrategy, lp, clp, 1048 spoofonly); 1049 if (errstring) { 1050 /*printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);*/ 1051 return; 1052 } 1053 } 1054 1055 1056 void 1057 sd_shutdown(arg) 1058 void *arg; 1059 { 1060 struct sd_softc *sd = arg; 1061 1062 /* 1063 * If the disk cache needs to be flushed, and the disk supports 1064 * it, flush it. We're cold at this point, so we poll for 1065 * completion. 1066 */ 1067 if ((sd->flags & SDF_DIRTY) != 0 && sd->sc_ops->sdo_flush != NULL) 1068 (*sd->sc_ops->sdo_flush)(sd, SCSI_AUTOCONF); 1069 } 1070 1071 /* 1072 * Tell the device to map out a defective block 1073 */ 1074 int 1075 sd_reassign_blocks(sd, blkno) 1076 struct sd_softc *sd; 1077 u_long blkno; 1078 { 1079 struct scsi_reassign_blocks scsi_cmd; 1080 struct scsi_reassign_blocks_data rbdata; 1081 1082 bzero(&scsi_cmd, sizeof(scsi_cmd)); 1083 bzero(&rbdata, sizeof(rbdata)); 1084 scsi_cmd.opcode = REASSIGN_BLOCKS; 1085 1086 _lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length); 1087 _lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr); 1088 1089 return scsi_scsi_cmd(sd->sc_link, (struct scsi_generic *)&scsi_cmd, 1090 sizeof(scsi_cmd), (u_char *)&rbdata, sizeof(rbdata), SDRETRIES, 1091 5000, NULL, SCSI_DATA_OUT); 1092 } 1093 1094 /* 1095 * Check Errors 1096 */ 1097 int 1098 sd_interpret_sense(xs) 1099 struct scsi_xfer *xs; 1100 { 1101 struct scsi_link *sc_link = xs->sc_link; 1102 struct scsi_sense_data *sense = &xs->sense; 1103 struct sd_softc *sd = sc_link->device_softc; 1104 int retval = SCSIRET_CONTINUE; 1105 1106 /* 1107 * If the device is not open yet, let the generic code handle it. 1108 */ 1109 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 1110 return (retval); 1111 } 1112 1113 /* 1114 * If it isn't a extended or extended/deferred error, let 1115 * the generic code handle it. 1116 */ 1117 if ((sense->error_code & SSD_ERRCODE) != 0x70 && 1118 (sense->error_code & SSD_ERRCODE) != 0x71) { /* DEFFERRED */ 1119 return (retval); 1120 } 1121 1122 if ((sense->flags & SSD_KEY) == SKEY_NOT_READY && 1123 sense->add_sense_code == 0x4) { 1124 if (sense->add_sense_code_qual == 0x01) { 1125 printf("%s: ..is spinning up...waiting\n", 1126 sd->sc_dev.dv_xname); 1127 /* 1128 * I really need a sdrestart function I can call here. 1129 */ 1130 delay(1000000 * 5); /* 5 seconds */ 1131 retval = SCSIRET_RETRY; 1132 } else if ((sense->add_sense_code_qual == 0x2) && 1133 (sd->sc_link->quirks & SDEV_NOSTARTUNIT) == 0) { 1134 if (sd->sc_link->flags & SDEV_REMOVABLE) { 1135 printf( 1136 "%s: removable disk stopped - not restarting\n", 1137 sd->sc_dev.dv_xname); 1138 retval = EIO; 1139 } else { 1140 printf("%s: respinning up disk\n", 1141 sd->sc_dev.dv_xname); 1142 retval = scsi_start(sd->sc_link, SSS_START, 1143 SCSI_URGENT | SCSI_NOSLEEP); 1144 if (retval != 0) { 1145 printf( 1146 "%s: respin of disk failed - %d\n", 1147 sd->sc_dev.dv_xname, retval); 1148 retval = EIO; 1149 } else { 1150 retval = SCSIRET_RETRY; 1151 } 1152 } 1153 } 1154 } 1155 return (retval); 1156 } 1157 1158 int 1159 sdsize(dev) 1160 dev_t dev; 1161 { 1162 struct sd_softc *sd; 1163 int part, omask; 1164 int size; 1165 1166 sd = sdlookup(SDUNIT(dev)); 1167 if (sd == NULL) 1168 return -1; 1169 1170 part = SDPART(dev); 1171 omask = sd->sc_dk.dk_openmask & (1 << part); 1172 1173 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0) { 1174 size = -1; 1175 goto exit; 1176 } 1177 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) 1178 size = -1; 1179 else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP) 1180 size = -1; 1181 else 1182 size = sd->sc_dk.dk_label->d_partitions[part].p_size * 1183 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE); 1184 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0) 1185 size = -1; 1186 1187 exit: 1188 device_unref(&sd->sc_dev); 1189 return size; 1190 } 1191 1192 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */ 1193 static struct scsi_xfer sx; 1194 static int sddoingadump; 1195 1196 /* 1197 * dump all of physical memory into the partition specified, starting 1198 * at offset 'dumplo' into the partition. 1199 */ 1200 int 1201 sddump(dev, blkno, va, size) 1202 dev_t dev; 1203 daddr_t blkno; 1204 caddr_t va; 1205 size_t size; 1206 { 1207 struct sd_softc *sd; /* disk unit to do the I/O */ 1208 struct disklabel *lp; /* disk's disklabel */ 1209 int unit, part; 1210 int sectorsize; /* size of a disk sector */ 1211 int nsects; /* number of sectors in partition */ 1212 int sectoff; /* sector offset of partition */ 1213 int totwrt; /* total number of sectors left to write */ 1214 int nwrt; /* current number of sectors to write */ 1215 struct scsi_rw_big cmd; /* write command */ 1216 struct scsi_xfer *xs; /* ... convenience */ 1217 int retval; 1218 1219 /* Check if recursive dump; if so, punt. */ 1220 if (sddoingadump) 1221 return EFAULT; 1222 1223 /* Mark as active early. */ 1224 sddoingadump = 1; 1225 1226 unit = SDUNIT(dev); /* Decompose unit & partition. */ 1227 part = SDPART(dev); 1228 1229 /* Check for acceptable drive number. */ 1230 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL) 1231 return ENXIO; 1232 1233 /* 1234 * XXX Can't do this check, since the media might have been 1235 * XXX marked `invalid' by successful unmounting of all 1236 * XXX filesystems. 1237 */ 1238 #if 0 1239 /* Make sure it was initialized. */ 1240 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) != SDEV_MEDIA_LOADED) 1241 return ENXIO; 1242 #endif 1243 1244 /* Convert to disk sectors. Request must be a multiple of size. */ 1245 lp = sd->sc_dk.dk_label; 1246 sectorsize = lp->d_secsize; 1247 if ((size % sectorsize) != 0) 1248 return EFAULT; 1249 totwrt = size / sectorsize; 1250 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */ 1251 1252 nsects = lp->d_partitions[part].p_size; 1253 sectoff = lp->d_partitions[part].p_offset; 1254 1255 /* Check transfer bounds against partition size. */ 1256 if ((blkno < 0) || ((blkno + totwrt) > nsects)) 1257 return EINVAL; 1258 1259 /* Offset block number to start of partition. */ 1260 blkno += sectoff; 1261 1262 xs = &sx; 1263 1264 while (totwrt > 0) { 1265 nwrt = totwrt; /* XXX */ 1266 #ifndef SD_DUMP_NOT_TRUSTED 1267 /* 1268 * Fill out the scsi command 1269 */ 1270 bzero(&cmd, sizeof(cmd)); 1271 cmd.opcode = WRITE_BIG; 1272 _lto4b(blkno, cmd.addr); 1273 _lto2b(nwrt, cmd.length); 1274 /* 1275 * Fill out the scsi_xfer structure 1276 * Note: we cannot sleep as we may be an interrupt 1277 * don't use scsi_scsi_cmd() as it may want 1278 * to wait for an xs. 1279 */ 1280 bzero(xs, sizeof(sx)); 1281 xs->flags |= SCSI_AUTOCONF | SCSI_DATA_OUT; 1282 xs->sc_link = sd->sc_link; 1283 xs->retries = SDRETRIES; 1284 xs->timeout = 10000; /* 10000 millisecs for a disk ! */ 1285 xs->cmd = (struct scsi_generic *)&cmd; 1286 xs->cmdlen = sizeof(cmd); 1287 xs->resid = nwrt * sectorsize; 1288 xs->error = XS_NOERROR; 1289 xs->bp = 0; 1290 xs->data = va; 1291 xs->datalen = nwrt * sectorsize; 1292 1293 /* 1294 * Pass all this info to the scsi driver. 1295 */ 1296 retval = (*(sd->sc_link->adapter->scsi_cmd)) (xs); 1297 if (retval != COMPLETE) 1298 return ENXIO; 1299 #else /* SD_DUMP_NOT_TRUSTED */ 1300 /* Let's just talk about this first... */ 1301 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno); 1302 delay(500 * 1000); /* half a second */ 1303 #endif /* SD_DUMP_NOT_TRUSTED */ 1304 1305 /* update block count */ 1306 totwrt -= nwrt; 1307 blkno += nwrt; 1308 va += sectorsize * nwrt; 1309 } 1310 sddoingadump = 0; 1311 return 0; 1312 } 1313 1314 /* 1315 * Copy up to len chars from src to dst, ignoring non-printables. 1316 * Must be room for len+1 chars in dst so we can write the NUL. 1317 * Does not assume src is NUL-terminated. 1318 */ 1319 void 1320 viscpy(dst, src, len) 1321 u_char *dst; 1322 u_char *src; 1323 int len; 1324 { 1325 while (len > 0 && *src != '\0') { 1326 if (*src < 0x20 || *src >= 0x80) { 1327 src++; 1328 continue; 1329 } 1330 *dst++ = *src++; 1331 len--; 1332 } 1333 *dst = '\0'; 1334 } 1335