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