1 /* $NetBSD: sd.c,v 1.287 2009/06/05 21:52:32 haad Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Originally written by Julian Elischer (julian@dialix.oz.au) 34 * for TRW Financial Systems for use under the MACH(2.5) operating system. 35 * 36 * TRW Financial Systems, in accordance with their agreement with Carnegie 37 * Mellon University, makes this software available to CMU to distribute 38 * or use in any manner that they see fit as long as this message is kept with 39 * the software. For this reason TFS also grants any other persons or 40 * organisations permission to use or modify this software. 41 * 42 * TFS supplies this software to be publicly redistributed 43 * on the understanding that TFS is not responsible for the correct 44 * functioning of this software in any circumstances. 45 * 46 * Ported to run under 386BSD by Julian Elischer (julian@dialix.oz.au) Sept 1992 47 */ 48 49 #include <sys/cdefs.h> 50 __KERNEL_RCSID(0, "$NetBSD: sd.c,v 1.287 2009/06/05 21:52:32 haad Exp $"); 51 52 #include "opt_scsi.h" 53 #include "rnd.h" 54 55 #include <sys/param.h> 56 #include <sys/systm.h> 57 #include <sys/kernel.h> 58 #include <sys/file.h> 59 #include <sys/stat.h> 60 #include <sys/ioctl.h> 61 #include <sys/scsiio.h> 62 #include <sys/buf.h> 63 #include <sys/bufq.h> 64 #include <sys/uio.h> 65 #include <sys/malloc.h> 66 #include <sys/errno.h> 67 #include <sys/device.h> 68 #include <sys/disklabel.h> 69 #include <sys/disk.h> 70 #include <sys/proc.h> 71 #include <sys/conf.h> 72 #include <sys/vnode.h> 73 #if NRND > 0 74 #include <sys/rnd.h> 75 #endif 76 77 #include <dev/scsipi/scsi_spc.h> 78 #include <dev/scsipi/scsipi_all.h> 79 #include <dev/scsipi/scsi_all.h> 80 #include <dev/scsipi/scsipi_disk.h> 81 #include <dev/scsipi/scsi_disk.h> 82 #include <dev/scsipi/scsiconf.h> 83 #include <dev/scsipi/scsipi_base.h> 84 #include <dev/scsipi/sdvar.h> 85 86 #include <prop/proplib.h> 87 88 #define SDUNIT(dev) DISKUNIT(dev) 89 #define SDPART(dev) DISKPART(dev) 90 #define SDMINOR(unit, part) DISKMINOR(unit, part) 91 #define MAKESDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part) 92 93 #define SDLABELDEV(dev) (MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART)) 94 95 #define SD_DEFAULT_BLKSIZE 512 96 97 static void sdminphys(struct buf *); 98 static void sdgetdefaultlabel(struct sd_softc *, struct disklabel *); 99 static int sdgetdisklabel(struct sd_softc *); 100 static void sdstart(struct scsipi_periph *); 101 static void sdrestart(void *); 102 static void sddone(struct scsipi_xfer *, int); 103 static bool sd_suspend(device_t PMF_FN_PROTO); 104 static bool sd_shutdown(device_t, int); 105 static int sd_interpret_sense(struct scsipi_xfer *); 106 static int sdlastclose(device_t); 107 108 static int sd_mode_sense(struct sd_softc *, u_int8_t, void *, size_t, int, 109 int, int *); 110 static int sd_mode_select(struct sd_softc *, u_int8_t, void *, size_t, int, 111 int); 112 static int sd_validate_blksize(struct scsipi_periph *, int); 113 static u_int64_t sd_read_capacity(struct scsipi_periph *, int *, int flags); 114 static int sd_get_simplifiedparms(struct sd_softc *, struct disk_parms *, 115 int); 116 static int sd_get_capacity(struct sd_softc *, struct disk_parms *, int); 117 static int sd_get_parms(struct sd_softc *, struct disk_parms *, int); 118 static int sd_get_parms_page4(struct sd_softc *, struct disk_parms *, 119 int); 120 static int sd_get_parms_page5(struct sd_softc *, struct disk_parms *, 121 int); 122 123 static int sd_flush(struct sd_softc *, int); 124 static int sd_getcache(struct sd_softc *, int *); 125 static int sd_setcache(struct sd_softc *, int); 126 127 static int sdmatch(device_t, cfdata_t, void *); 128 static void sdattach(device_t, device_t, void *); 129 static int sddetach(device_t, int); 130 static void sd_set_properties(struct sd_softc *); 131 132 CFATTACH_DECL3_NEW(sd, sizeof(struct sd_softc), sdmatch, sdattach, sddetach, 133 NULL, NULL, NULL, DVF_DETACH_SHUTDOWN); 134 135 extern struct cfdriver sd_cd; 136 137 static const struct scsipi_inquiry_pattern sd_patterns[] = { 138 {T_DIRECT, T_FIXED, 139 "", "", ""}, 140 {T_DIRECT, T_REMOV, 141 "", "", ""}, 142 {T_OPTICAL, T_FIXED, 143 "", "", ""}, 144 {T_OPTICAL, T_REMOV, 145 "", "", ""}, 146 {T_SIMPLE_DIRECT, T_FIXED, 147 "", "", ""}, 148 {T_SIMPLE_DIRECT, T_REMOV, 149 "", "", ""}, 150 }; 151 152 static dev_type_open(sdopen); 153 static dev_type_close(sdclose); 154 static dev_type_read(sdread); 155 static dev_type_write(sdwrite); 156 static dev_type_ioctl(sdioctl); 157 static dev_type_strategy(sdstrategy); 158 static dev_type_dump(sddump); 159 static dev_type_size(sdsize); 160 161 const struct bdevsw sd_bdevsw = { 162 sdopen, sdclose, sdstrategy, sdioctl, sddump, sdsize, D_DISK 163 }; 164 165 const struct cdevsw sd_cdevsw = { 166 sdopen, sdclose, sdread, sdwrite, sdioctl, 167 nostop, notty, nopoll, nommap, nokqfilter, D_DISK 168 }; 169 170 static struct dkdriver sddkdriver = { sdstrategy, sdminphys }; 171 172 static const struct scsipi_periphsw sd_switch = { 173 sd_interpret_sense, /* check our error handler first */ 174 sdstart, /* have a queue, served by this */ 175 NULL, /* have no async handler */ 176 sddone, /* deal with stats at interrupt time */ 177 }; 178 179 struct sd_mode_sense_data { 180 /* 181 * XXX 182 * We are not going to parse this as-is -- it just has to be large 183 * enough. 184 */ 185 union { 186 struct scsi_mode_parameter_header_6 small; 187 struct scsi_mode_parameter_header_10 big; 188 } header; 189 struct scsi_general_block_descriptor blk_desc; 190 union scsi_disk_pages pages; 191 }; 192 193 /* 194 * The routine called by the low level scsi routine when it discovers 195 * A device suitable for this driver 196 */ 197 static int 198 sdmatch(device_t parent, cfdata_t match, 199 void *aux) 200 { 201 struct scsipibus_attach_args *sa = aux; 202 int priority; 203 204 (void)scsipi_inqmatch(&sa->sa_inqbuf, 205 sd_patterns, sizeof(sd_patterns) / sizeof(sd_patterns[0]), 206 sizeof(sd_patterns[0]), &priority); 207 208 return (priority); 209 } 210 211 /* 212 * Attach routine common to atapi & scsi. 213 */ 214 static void 215 sdattach(device_t parent, device_t self, void *aux) 216 { 217 struct sd_softc *sd = device_private(self); 218 struct scsipibus_attach_args *sa = aux; 219 struct scsipi_periph *periph = sa->sa_periph; 220 int error, result; 221 struct disk_parms *dp = &sd->params; 222 char pbuf[9]; 223 224 SC_DEBUG(periph, SCSIPI_DB2, ("sdattach: ")); 225 226 sd->sc_dev = self; 227 sd->type = (sa->sa_inqbuf.type & SID_TYPE); 228 strncpy(sd->name, sa->sa_inqbuf.product, sizeof(sd->name)); 229 if (sd->type == T_SIMPLE_DIRECT) 230 periph->periph_quirks |= PQUIRK_ONLYBIG | PQUIRK_NOBIGMODESENSE; 231 232 if (scsipi_periph_bustype(sa->sa_periph) == SCSIPI_BUSTYPE_SCSI && 233 periph->periph_version == 0) 234 sd->flags |= SDF_ANCIENT; 235 236 bufq_alloc(&sd->buf_queue, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK); 237 238 callout_init(&sd->sc_callout, 0); 239 240 /* 241 * Store information needed to contact our base driver 242 */ 243 sd->sc_periph = periph; 244 245 periph->periph_dev = sd->sc_dev; 246 periph->periph_switch = &sd_switch; 247 248 /* 249 * Increase our openings to the maximum-per-periph 250 * supported by the adapter. This will either be 251 * clamped down or grown by the adapter if necessary. 252 */ 253 periph->periph_openings = 254 SCSIPI_CHAN_MAX_PERIPH(periph->periph_channel); 255 periph->periph_flags |= PERIPH_GROW_OPENINGS; 256 257 /* 258 * Initialize and attach the disk structure. 259 */ 260 disk_init(&sd->sc_dk, device_xname(sd->sc_dev), &sddkdriver); 261 disk_attach(&sd->sc_dk); 262 263 /* 264 * Use the subdriver to request information regarding the drive. 265 */ 266 aprint_naive("\n"); 267 aprint_normal("\n"); 268 269 error = scsipi_test_unit_ready(periph, 270 XS_CTL_DISCOVERY | XS_CTL_IGNORE_ILLEGAL_REQUEST | 271 XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_SILENT_NODEV); 272 273 if (error) 274 result = SDGP_RESULT_OFFLINE; 275 else 276 result = sd_get_parms(sd, &sd->params, XS_CTL_DISCOVERY); 277 aprint_normal_dev(sd->sc_dev, ""); 278 switch (result) { 279 case SDGP_RESULT_OK: 280 format_bytes(pbuf, sizeof(pbuf), 281 (u_int64_t)dp->disksize * dp->blksize); 282 aprint_normal( 283 "%s, %ld cyl, %ld head, %ld sec, %ld bytes/sect x %llu sectors", 284 pbuf, dp->cyls, dp->heads, dp->sectors, dp->blksize, 285 (unsigned long long)dp->disksize); 286 break; 287 288 case SDGP_RESULT_OFFLINE: 289 aprint_normal("drive offline"); 290 break; 291 292 case SDGP_RESULT_UNFORMATTED: 293 aprint_normal("unformatted media"); 294 break; 295 296 #ifdef DIAGNOSTIC 297 default: 298 panic("sdattach: unknown result from get_parms"); 299 break; 300 #endif 301 } 302 aprint_normal("\n"); 303 304 /* 305 * Establish a shutdown hook so that we can ensure that 306 * our data has actually made it onto the platter at 307 * shutdown time. Note that this relies on the fact 308 * that the shutdown hooks at the "leaves" of the device tree 309 * are run, first (thus guaranteeing that our hook runs before 310 * our ancestors'). 311 */ 312 if (!pmf_device_register1(self, sd_suspend, NULL, sd_shutdown)) 313 aprint_error_dev(self, "couldn't establish power handler\n"); 314 315 #if NRND > 0 316 /* 317 * attach the device into the random source list 318 */ 319 rnd_attach_source(&sd->rnd_source, device_xname(sd->sc_dev), 320 RND_TYPE_DISK, 0); 321 #endif 322 323 /* Discover wedges on this disk. */ 324 dkwedge_discover(&sd->sc_dk); 325 326 sd_set_properties(sd); 327 } 328 329 static int 330 sddetach(device_t self, int flags) 331 { 332 struct sd_softc *sd = device_private(self); 333 int s, bmaj, cmaj, i, mn, rc; 334 335 if ((rc = disk_begindetach(&sd->sc_dk, sdlastclose, self, flags)) != 0) 336 return rc; 337 338 /* locate the major number */ 339 bmaj = bdevsw_lookup_major(&sd_bdevsw); 340 cmaj = cdevsw_lookup_major(&sd_cdevsw); 341 342 /* Nuke the vnodes for any open instances */ 343 for (i = 0; i < MAXPARTITIONS; i++) { 344 mn = SDMINOR(device_unit(self), i); 345 vdevgone(bmaj, mn, mn, VBLK); 346 vdevgone(cmaj, mn, mn, VCHR); 347 } 348 349 /* kill any pending restart */ 350 callout_stop(&sd->sc_callout); 351 352 /* Delete all of our wedges. */ 353 dkwedge_delall(&sd->sc_dk); 354 355 s = splbio(); 356 357 /* Kill off any queued buffers. */ 358 bufq_drain(sd->buf_queue); 359 360 bufq_free(sd->buf_queue); 361 362 /* Kill off any pending commands. */ 363 scsipi_kill_pending(sd->sc_periph); 364 365 splx(s); 366 367 /* Detach from the disk list. */ 368 disk_detach(&sd->sc_dk); 369 disk_destroy(&sd->sc_dk); 370 371 callout_destroy(&sd->sc_callout); 372 373 pmf_device_deregister(self); 374 375 #if NRND > 0 376 /* Unhook the entropy source. */ 377 rnd_detach_source(&sd->rnd_source); 378 #endif 379 380 return (0); 381 } 382 383 /* 384 * open the device. Make sure the partition info is a up-to-date as can be. 385 */ 386 static int 387 sdopen(dev_t dev, int flag, int fmt, struct lwp *l) 388 { 389 struct sd_softc *sd; 390 struct scsipi_periph *periph; 391 struct scsipi_adapter *adapt; 392 int unit, part; 393 int error; 394 395 unit = SDUNIT(dev); 396 sd = device_lookup_private(&sd_cd, unit); 397 if (sd == NULL) 398 return (ENXIO); 399 400 if (!device_is_active(sd->sc_dev)) 401 return (ENODEV); 402 403 part = SDPART(dev); 404 405 mutex_enter(&sd->sc_dk.dk_openlock); 406 407 /* 408 * If there are wedges, and this is not RAW_PART, then we 409 * need to fail. 410 */ 411 if (sd->sc_dk.dk_nwedges != 0 && part != RAW_PART) { 412 error = EBUSY; 413 goto bad1; 414 } 415 416 periph = sd->sc_periph; 417 adapt = periph->periph_channel->chan_adapter; 418 419 SC_DEBUG(periph, SCSIPI_DB1, 420 ("sdopen: dev=0x%"PRIx64" (unit %d (of %d), partition %d)\n", dev, unit, 421 sd_cd.cd_ndevs, part)); 422 423 /* 424 * If this is the first open of this device, add a reference 425 * to the adapter. 426 */ 427 if (sd->sc_dk.dk_openmask == 0 && 428 (error = scsipi_adapter_addref(adapt)) != 0) 429 goto bad1; 430 431 if ((periph->periph_flags & PERIPH_OPEN) != 0) { 432 /* 433 * If any partition is open, but the disk has been invalidated, 434 * disallow further opens of non-raw partition 435 */ 436 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 && 437 (part != RAW_PART || fmt != S_IFCHR)) { 438 error = EIO; 439 goto bad2; 440 } 441 } else { 442 int silent; 443 444 if (part == RAW_PART && fmt == S_IFCHR) 445 silent = XS_CTL_SILENT; 446 else 447 silent = 0; 448 449 /* Check that it is still responding and ok. */ 450 error = scsipi_test_unit_ready(periph, 451 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE | 452 silent); 453 454 /* 455 * Start the pack spinning if necessary. Always allow the 456 * raw parition to be opened, for raw IOCTLs. Data transfers 457 * will check for SDEV_MEDIA_LOADED. 458 */ 459 if (error == EIO) { 460 int error2; 461 462 error2 = scsipi_start(periph, SSS_START, silent); 463 switch (error2) { 464 case 0: 465 error = 0; 466 break; 467 case EIO: 468 case EINVAL: 469 break; 470 default: 471 error = error2; 472 break; 473 } 474 } 475 if (error) { 476 if (silent) 477 goto out; 478 goto bad2; 479 } 480 481 periph->periph_flags |= PERIPH_OPEN; 482 483 if (periph->periph_flags & PERIPH_REMOVABLE) { 484 /* Lock the pack in. */ 485 error = scsipi_prevent(periph, SPAMR_PREVENT_DT, 486 XS_CTL_IGNORE_ILLEGAL_REQUEST | 487 XS_CTL_IGNORE_MEDIA_CHANGE | 488 XS_CTL_SILENT); 489 if (error) 490 goto bad3; 491 } 492 493 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 494 int param_error; 495 periph->periph_flags |= PERIPH_MEDIA_LOADED; 496 497 /* 498 * Load the physical device parameters. 499 * 500 * Note that if media is present but unformatted, 501 * we allow the open (so that it can be formatted!). 502 * The drive should refuse real I/O, if the media is 503 * unformatted. 504 */ 505 if ((param_error = sd_get_parms(sd, &sd->params, 0)) 506 == SDGP_RESULT_OFFLINE) { 507 error = ENXIO; 508 periph->periph_flags &= ~PERIPH_MEDIA_LOADED; 509 goto bad3; 510 } 511 SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded ")); 512 513 /* Load the partition info if not already loaded. */ 514 if (param_error == 0) { 515 if ((sdgetdisklabel(sd) != 0) && (part != RAW_PART)) { 516 error = EIO; 517 goto bad3; 518 } 519 SC_DEBUG(periph, SCSIPI_DB3, 520 ("Disklabel loaded ")); 521 } 522 } 523 } 524 525 /* Check that the partition exists. */ 526 if (part != RAW_PART && 527 (part >= sd->sc_dk.dk_label->d_npartitions || 528 sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) { 529 error = ENXIO; 530 goto bad3; 531 } 532 533 out: /* Insure only one open at a time. */ 534 switch (fmt) { 535 case S_IFCHR: 536 sd->sc_dk.dk_copenmask |= (1 << part); 537 break; 538 case S_IFBLK: 539 sd->sc_dk.dk_bopenmask |= (1 << part); 540 break; 541 } 542 sd->sc_dk.dk_openmask = 543 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask; 544 545 SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n")); 546 mutex_exit(&sd->sc_dk.dk_openlock); 547 return (0); 548 549 bad3: 550 if (sd->sc_dk.dk_openmask == 0) { 551 if (periph->periph_flags & PERIPH_REMOVABLE) 552 scsipi_prevent(periph, SPAMR_ALLOW, 553 XS_CTL_IGNORE_ILLEGAL_REQUEST | 554 XS_CTL_IGNORE_MEDIA_CHANGE | 555 XS_CTL_SILENT); 556 periph->periph_flags &= ~PERIPH_OPEN; 557 } 558 559 bad2: 560 if (sd->sc_dk.dk_openmask == 0) 561 scsipi_adapter_delref(adapt); 562 563 bad1: 564 mutex_exit(&sd->sc_dk.dk_openlock); 565 return (error); 566 } 567 568 /* 569 * Caller must hold sd->sc_dk.dk_openlock. 570 */ 571 static int 572 sdlastclose(device_t self) 573 { 574 struct sd_softc *sd = device_private(self); 575 struct scsipi_periph *periph = sd->sc_periph; 576 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; 577 578 /* 579 * If the disk cache needs flushing, and the disk supports 580 * it, do it now. 581 */ 582 if ((sd->flags & SDF_DIRTY) != 0) { 583 if (sd_flush(sd, 0)) { 584 aprint_error_dev(sd->sc_dev, 585 "cache synchronization failed\n"); 586 sd->flags &= ~SDF_FLUSHING; 587 } else 588 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 589 } 590 591 scsipi_wait_drain(periph); 592 593 if (periph->periph_flags & PERIPH_REMOVABLE) 594 scsipi_prevent(periph, SPAMR_ALLOW, 595 XS_CTL_IGNORE_ILLEGAL_REQUEST | 596 XS_CTL_IGNORE_NOT_READY | 597 XS_CTL_SILENT); 598 periph->periph_flags &= ~PERIPH_OPEN; 599 600 scsipi_wait_drain(periph); 601 602 scsipi_adapter_delref(adapt); 603 604 return 0; 605 } 606 607 /* 608 * close the device.. only called if we are the LAST occurence of an open 609 * device. Convenient now but usually a pain. 610 */ 611 static int 612 sdclose(dev_t dev, int flag, int fmt, struct lwp *l) 613 { 614 struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(dev)); 615 int part = SDPART(dev); 616 617 mutex_enter(&sd->sc_dk.dk_openlock); 618 switch (fmt) { 619 case S_IFCHR: 620 sd->sc_dk.dk_copenmask &= ~(1 << part); 621 break; 622 case S_IFBLK: 623 sd->sc_dk.dk_bopenmask &= ~(1 << part); 624 break; 625 } 626 sd->sc_dk.dk_openmask = 627 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask; 628 629 if (sd->sc_dk.dk_openmask == 0) 630 sdlastclose(sd->sc_dev); 631 632 mutex_exit(&sd->sc_dk.dk_openlock); 633 return (0); 634 } 635 636 /* 637 * Actually translate the requested transfer into one the physical driver 638 * can understand. The transfer is described by a buf and will include 639 * only one physical transfer. 640 */ 641 static void 642 sdstrategy(struct buf *bp) 643 { 644 struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(bp->b_dev)); 645 struct scsipi_periph *periph = sd->sc_periph; 646 struct disklabel *lp; 647 daddr_t blkno; 648 int s; 649 bool sector_aligned; 650 651 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdstrategy ")); 652 SC_DEBUG(sd->sc_periph, SCSIPI_DB1, 653 ("%d bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno)); 654 /* 655 * If the device has been made invalid, error out 656 */ 657 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 || 658 !device_is_active(sd->sc_dev)) { 659 if (periph->periph_flags & PERIPH_OPEN) 660 bp->b_error = EIO; 661 else 662 bp->b_error = ENODEV; 663 goto done; 664 } 665 666 lp = sd->sc_dk.dk_label; 667 668 /* 669 * The transfer must be a whole number of blocks, offset must not be 670 * negative. 671 */ 672 if (lp->d_secsize == DEV_BSIZE) { 673 sector_aligned = (bp->b_bcount & (DEV_BSIZE - 1)) == 0; 674 } else { 675 sector_aligned = (bp->b_bcount % lp->d_secsize) == 0; 676 } 677 if (!sector_aligned || bp->b_blkno < 0) { 678 bp->b_error = EINVAL; 679 goto done; 680 } 681 /* 682 * If it's a null transfer, return immediatly 683 */ 684 if (bp->b_bcount == 0) 685 goto done; 686 687 /* 688 * Do bounds checking, adjust transfer. if error, process. 689 * If end of partition, just return. 690 */ 691 if (SDPART(bp->b_dev) == RAW_PART) { 692 if (bounds_check_with_mediasize(bp, DEV_BSIZE, 693 sd->params.disksize512) <= 0) 694 goto done; 695 } else { 696 if (bounds_check_with_label(&sd->sc_dk, bp, 697 (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0) 698 goto done; 699 } 700 701 /* 702 * Now convert the block number to absolute and put it in 703 * terms of the device's logical block size. 704 */ 705 if (lp->d_secsize == DEV_BSIZE) 706 blkno = bp->b_blkno; 707 else if (lp->d_secsize > DEV_BSIZE) 708 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE); 709 else 710 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize); 711 712 if (SDPART(bp->b_dev) != RAW_PART) 713 blkno += lp->d_partitions[SDPART(bp->b_dev)].p_offset; 714 715 bp->b_rawblkno = blkno; 716 717 s = splbio(); 718 719 /* 720 * Place it in the queue of disk activities for this disk. 721 * 722 * XXX Only do disksort() if the current operating mode does not 723 * XXX include tagged queueing. 724 */ 725 bufq_put(sd->buf_queue, bp); 726 727 /* 728 * Tell the device to get going on the transfer if it's 729 * not doing anything, otherwise just wait for completion 730 */ 731 sdstart(sd->sc_periph); 732 733 splx(s); 734 return; 735 736 done: 737 /* 738 * Correctly set the buf to indicate a completed xfer 739 */ 740 bp->b_resid = bp->b_bcount; 741 biodone(bp); 742 } 743 744 /* 745 * sdstart looks to see if there is a buf waiting for the device 746 * and that the device is not already busy. If both are true, 747 * It dequeues the buf and creates a scsi command to perform the 748 * transfer in the buf. The transfer request will call scsipi_done 749 * on completion, which will in turn call this routine again 750 * so that the next queued transfer is performed. 751 * The bufs are queued by the strategy routine (sdstrategy) 752 * 753 * This routine is also called after other non-queued requests 754 * have been made of the scsi driver, to ensure that the queue 755 * continues to be drained. 756 * 757 * must be called at the correct (highish) spl level 758 * sdstart() is called at splbio from sdstrategy, sdrestart and scsipi_done 759 */ 760 static void 761 sdstart(struct scsipi_periph *periph) 762 { 763 struct sd_softc *sd = device_private(periph->periph_dev); 764 struct disklabel *lp = sd->sc_dk.dk_label; 765 struct buf *bp = 0; 766 struct scsipi_rw_16 cmd16; 767 struct scsipi_rw_10 cmd_big; 768 struct scsi_rw_6 cmd_small; 769 struct scsipi_generic *cmdp; 770 struct scsipi_xfer *xs; 771 int nblks, cmdlen, error, flags; 772 773 SC_DEBUG(periph, SCSIPI_DB2, ("sdstart ")); 774 /* 775 * Check if the device has room for another command 776 */ 777 while (periph->periph_active < periph->periph_openings) { 778 /* 779 * there is excess capacity, but a special waits 780 * It'll need the adapter as soon as we clear out of the 781 * way and let it run (user level wait). 782 */ 783 if (periph->periph_flags & PERIPH_WAITING) { 784 periph->periph_flags &= ~PERIPH_WAITING; 785 wakeup((void *)periph); 786 return; 787 } 788 789 /* 790 * If the device has become invalid, abort all the 791 * reads and writes until all files have been closed and 792 * re-opened 793 */ 794 if (__predict_false( 795 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)) { 796 if ((bp = bufq_get(sd->buf_queue)) != NULL) { 797 bp->b_error = EIO; 798 bp->b_resid = bp->b_bcount; 799 biodone(bp); 800 continue; 801 } else { 802 return; 803 } 804 } 805 806 /* 807 * See if there is a buf with work for us to do.. 808 */ 809 if ((bp = bufq_peek(sd->buf_queue)) == NULL) 810 return; 811 812 /* 813 * We have a buf, now we should make a command. 814 */ 815 816 if (lp->d_secsize == DEV_BSIZE) 817 nblks = bp->b_bcount >> DEV_BSHIFT; 818 else 819 nblks = howmany(bp->b_bcount, lp->d_secsize); 820 821 /* 822 * Fill out the scsi command. Use the smallest CDB possible 823 * (6-byte, 10-byte, or 16-byte). 824 */ 825 if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) && 826 ((nblks & 0xff) == nblks) && 827 !(periph->periph_quirks & PQUIRK_ONLYBIG)) { 828 /* 6-byte CDB */ 829 memset(&cmd_small, 0, sizeof(cmd_small)); 830 cmd_small.opcode = (bp->b_flags & B_READ) ? 831 SCSI_READ_6_COMMAND : SCSI_WRITE_6_COMMAND; 832 _lto3b(bp->b_rawblkno, cmd_small.addr); 833 cmd_small.length = nblks & 0xff; 834 cmdlen = sizeof(cmd_small); 835 cmdp = (struct scsipi_generic *)&cmd_small; 836 } else if ((bp->b_rawblkno & 0xffffffff) == bp->b_rawblkno) { 837 /* 10-byte CDB */ 838 memset(&cmd_big, 0, sizeof(cmd_big)); 839 cmd_big.opcode = (bp->b_flags & B_READ) ? 840 READ_10 : WRITE_10; 841 _lto4b(bp->b_rawblkno, cmd_big.addr); 842 _lto2b(nblks, cmd_big.length); 843 cmdlen = sizeof(cmd_big); 844 cmdp = (struct scsipi_generic *)&cmd_big; 845 } else { 846 /* 16-byte CDB */ 847 memset(&cmd16, 0, sizeof(cmd16)); 848 cmd16.opcode = (bp->b_flags & B_READ) ? 849 READ_16 : WRITE_16; 850 _lto8b(bp->b_rawblkno, cmd16.addr); 851 _lto4b(nblks, cmd16.length); 852 cmdlen = sizeof(cmd16); 853 cmdp = (struct scsipi_generic *)&cmd16; 854 } 855 856 /* Instrumentation. */ 857 disk_busy(&sd->sc_dk); 858 859 /* 860 * Mark the disk dirty so that the cache will be 861 * flushed on close. 862 */ 863 if ((bp->b_flags & B_READ) == 0) 864 sd->flags |= SDF_DIRTY; 865 866 /* 867 * Figure out what flags to use. 868 */ 869 flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_SIMPLE_TAG; 870 if (bp->b_flags & B_READ) 871 flags |= XS_CTL_DATA_IN; 872 else 873 flags |= XS_CTL_DATA_OUT; 874 875 /* 876 * Call the routine that chats with the adapter. 877 * Note: we cannot sleep as we may be an interrupt 878 */ 879 xs = scsipi_make_xs(periph, cmdp, cmdlen, 880 (u_char *)bp->b_data, bp->b_bcount, 881 SDRETRIES, SD_IO_TIMEOUT, bp, flags); 882 if (__predict_false(xs == NULL)) { 883 /* 884 * out of memory. Keep this buffer in the queue, and 885 * retry later. 886 */ 887 callout_reset(&sd->sc_callout, hz / 2, sdrestart, 888 periph); 889 return; 890 } 891 /* 892 * need to dequeue the buffer before queuing the command, 893 * because cdstart may be called recursively from the 894 * HBA driver 895 */ 896 #ifdef DIAGNOSTIC 897 if (bufq_get(sd->buf_queue) != bp) 898 panic("sdstart(): dequeued wrong buf"); 899 #else 900 bufq_get(sd->buf_queue); 901 #endif 902 error = scsipi_execute_xs(xs); 903 /* with a scsipi_xfer preallocated, scsipi_command can't fail */ 904 KASSERT(error == 0); 905 } 906 } 907 908 static void 909 sdrestart(void *v) 910 { 911 int s = splbio(); 912 sdstart((struct scsipi_periph *)v); 913 splx(s); 914 } 915 916 static void 917 sddone(struct scsipi_xfer *xs, int error) 918 { 919 struct sd_softc *sd = device_private(xs->xs_periph->periph_dev); 920 struct buf *bp = xs->bp; 921 922 if (sd->flags & SDF_FLUSHING) { 923 /* Flush completed, no longer dirty. */ 924 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 925 } 926 927 if (bp) { 928 bp->b_error = error; 929 bp->b_resid = xs->resid; 930 if (error) { 931 /* on a read/write error bp->b_resid is zero, so fix */ 932 bp->b_resid = bp->b_bcount; 933 } 934 935 disk_unbusy(&sd->sc_dk, bp->b_bcount - bp->b_resid, 936 (bp->b_flags & B_READ)); 937 #if NRND > 0 938 rnd_add_uint32(&sd->rnd_source, bp->b_rawblkno); 939 #endif 940 941 biodone(bp); 942 } 943 } 944 945 static void 946 sdminphys(struct buf *bp) 947 { 948 struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(bp->b_dev)); 949 long xmax; 950 951 /* 952 * If the device is ancient, we want to make sure that 953 * the transfer fits into a 6-byte cdb. 954 * 955 * XXX Note that the SCSI-I spec says that 256-block transfers 956 * are allowed in a 6-byte read/write, and are specified 957 * by settng the "length" to 0. However, we're conservative 958 * here, allowing only 255-block transfers in case an 959 * ancient device gets confused by length == 0. A length of 0 960 * in a 10-byte read/write actually means 0 blocks. 961 */ 962 if ((sd->flags & SDF_ANCIENT) && 963 ((sd->sc_periph->periph_flags & 964 (PERIPH_REMOVABLE | PERIPH_MEDIA_LOADED)) != PERIPH_REMOVABLE)) { 965 xmax = sd->sc_dk.dk_label->d_secsize * 0xff; 966 967 if (bp->b_bcount > xmax) 968 bp->b_bcount = xmax; 969 } 970 971 scsipi_adapter_minphys(sd->sc_periph->periph_channel, bp); 972 } 973 974 static int 975 sdread(dev_t dev, struct uio *uio, int ioflag) 976 { 977 978 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio)); 979 } 980 981 static int 982 sdwrite(dev_t dev, struct uio *uio, int ioflag) 983 { 984 985 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio)); 986 } 987 988 /* 989 * Perform special action on behalf of the user 990 * Knows about the internals of this device 991 */ 992 static int 993 sdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 994 { 995 struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(dev)); 996 struct scsipi_periph *periph = sd->sc_periph; 997 int part = SDPART(dev); 998 int error = 0; 999 #ifdef __HAVE_OLD_DISKLABEL 1000 struct disklabel *newlabel = NULL; 1001 #endif 1002 1003 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdioctl 0x%lx ", cmd)); 1004 1005 /* 1006 * If the device is not valid, some IOCTLs can still be 1007 * handled on the raw partition. Check this here. 1008 */ 1009 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 1010 switch (cmd) { 1011 case DIOCKLABEL: 1012 case DIOCWLABEL: 1013 case DIOCLOCK: 1014 case DIOCEJECT: 1015 case ODIOCEJECT: 1016 case DIOCGCACHE: 1017 case DIOCSCACHE: 1018 case SCIOCIDENTIFY: 1019 case OSCIOCIDENTIFY: 1020 case SCIOCCOMMAND: 1021 case SCIOCDEBUG: 1022 if (part == RAW_PART) 1023 break; 1024 /* FALLTHROUGH */ 1025 default: 1026 if ((periph->periph_flags & PERIPH_OPEN) == 0) 1027 return (ENODEV); 1028 else 1029 return (EIO); 1030 } 1031 } 1032 1033 error = disk_ioctl(&sd->sc_dk, cmd, addr, flag, l); 1034 if (error != EPASSTHROUGH) 1035 return (error); 1036 1037 switch (cmd) { 1038 case DIOCGDINFO: 1039 *(struct disklabel *)addr = *(sd->sc_dk.dk_label); 1040 return (0); 1041 1042 #ifdef __HAVE_OLD_DISKLABEL 1043 case ODIOCGDINFO: 1044 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK); 1045 if (newlabel == NULL) 1046 return EIO; 1047 memcpy(newlabel, sd->sc_dk.dk_label, sizeof (*newlabel)); 1048 if (newlabel->d_npartitions <= OLDMAXPARTITIONS) 1049 memcpy(addr, newlabel, sizeof (struct olddisklabel)); 1050 else 1051 error = ENOTTY; 1052 free(newlabel, M_TEMP); 1053 return error; 1054 #endif 1055 1056 case DIOCGPART: 1057 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label; 1058 ((struct partinfo *)addr)->part = 1059 &sd->sc_dk.dk_label->d_partitions[part]; 1060 return (0); 1061 1062 case DIOCWDINFO: 1063 case DIOCSDINFO: 1064 #ifdef __HAVE_OLD_DISKLABEL 1065 case ODIOCWDINFO: 1066 case ODIOCSDINFO: 1067 #endif 1068 { 1069 struct disklabel *lp; 1070 1071 if ((flag & FWRITE) == 0) 1072 return (EBADF); 1073 1074 #ifdef __HAVE_OLD_DISKLABEL 1075 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) { 1076 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK); 1077 if (newlabel == NULL) 1078 return EIO; 1079 memset(newlabel, 0, sizeof newlabel); 1080 memcpy(newlabel, addr, sizeof (struct olddisklabel)); 1081 lp = newlabel; 1082 } else 1083 #endif 1084 lp = (struct disklabel *)addr; 1085 1086 mutex_enter(&sd->sc_dk.dk_openlock); 1087 sd->flags |= SDF_LABELLING; 1088 1089 error = setdisklabel(sd->sc_dk.dk_label, 1090 lp, /*sd->sc_dk.dk_openmask : */0, 1091 sd->sc_dk.dk_cpulabel); 1092 if (error == 0) { 1093 if (cmd == DIOCWDINFO 1094 #ifdef __HAVE_OLD_DISKLABEL 1095 || cmd == ODIOCWDINFO 1096 #endif 1097 ) 1098 error = writedisklabel(SDLABELDEV(dev), 1099 sdstrategy, sd->sc_dk.dk_label, 1100 sd->sc_dk.dk_cpulabel); 1101 } 1102 1103 sd->flags &= ~SDF_LABELLING; 1104 mutex_exit(&sd->sc_dk.dk_openlock); 1105 #ifdef __HAVE_OLD_DISKLABEL 1106 if (newlabel != NULL) 1107 free(newlabel, M_TEMP); 1108 #endif 1109 return (error); 1110 } 1111 1112 case DIOCKLABEL: 1113 if (*(int *)addr) 1114 periph->periph_flags |= PERIPH_KEEP_LABEL; 1115 else 1116 periph->periph_flags &= ~PERIPH_KEEP_LABEL; 1117 return (0); 1118 1119 case DIOCWLABEL: 1120 if ((flag & FWRITE) == 0) 1121 return (EBADF); 1122 if (*(int *)addr) 1123 sd->flags |= SDF_WLABEL; 1124 else 1125 sd->flags &= ~SDF_WLABEL; 1126 return (0); 1127 1128 case DIOCLOCK: 1129 if (periph->periph_flags & PERIPH_REMOVABLE) 1130 return (scsipi_prevent(periph, 1131 (*(int *)addr) ? 1132 SPAMR_PREVENT_DT : SPAMR_ALLOW, 0)); 1133 else 1134 return (ENOTTY); 1135 1136 case DIOCEJECT: 1137 if ((periph->periph_flags & PERIPH_REMOVABLE) == 0) 1138 return (ENOTTY); 1139 if (*(int *)addr == 0) { 1140 /* 1141 * Don't force eject: check that we are the only 1142 * partition open. If so, unlock it. 1143 */ 1144 if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 && 1145 sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask == 1146 sd->sc_dk.dk_openmask) { 1147 error = scsipi_prevent(periph, SPAMR_ALLOW, 1148 XS_CTL_IGNORE_NOT_READY); 1149 if (error) 1150 return (error); 1151 } else { 1152 return (EBUSY); 1153 } 1154 } 1155 /* FALLTHROUGH */ 1156 case ODIOCEJECT: 1157 return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ? 1158 ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0)); 1159 1160 case DIOCGDEFLABEL: 1161 sdgetdefaultlabel(sd, (struct disklabel *)addr); 1162 return (0); 1163 1164 #ifdef __HAVE_OLD_DISKLABEL 1165 case ODIOCGDEFLABEL: 1166 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK); 1167 if (newlabel == NULL) 1168 return EIO; 1169 sdgetdefaultlabel(sd, newlabel); 1170 if (newlabel->d_npartitions <= OLDMAXPARTITIONS) 1171 memcpy(addr, newlabel, sizeof (struct olddisklabel)); 1172 else 1173 error = ENOTTY; 1174 free(newlabel, M_TEMP); 1175 return error; 1176 #endif 1177 1178 case DIOCGCACHE: 1179 return (sd_getcache(sd, (int *) addr)); 1180 1181 case DIOCSCACHE: 1182 if ((flag & FWRITE) == 0) 1183 return (EBADF); 1184 return (sd_setcache(sd, *(int *) addr)); 1185 1186 case DIOCCACHESYNC: 1187 /* 1188 * XXX Do we really need to care about having a writable 1189 * file descriptor here? 1190 */ 1191 if ((flag & FWRITE) == 0) 1192 return (EBADF); 1193 if (((sd->flags & SDF_DIRTY) != 0 || *(int *)addr != 0)) { 1194 error = sd_flush(sd, 0); 1195 if (error) 1196 sd->flags &= ~SDF_FLUSHING; 1197 else 1198 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 1199 } else 1200 error = 0; 1201 return (error); 1202 1203 case DIOCAWEDGE: 1204 { 1205 struct dkwedge_info *dkw = (void *) addr; 1206 1207 if ((flag & FWRITE) == 0) 1208 return (EBADF); 1209 1210 /* If the ioctl happens here, the parent is us. */ 1211 strlcpy(dkw->dkw_parent, device_xname(sd->sc_dev), 1212 sizeof(dkw->dkw_parent)); 1213 return (dkwedge_add(dkw)); 1214 } 1215 1216 case DIOCDWEDGE: 1217 { 1218 struct dkwedge_info *dkw = (void *) addr; 1219 1220 if ((flag & FWRITE) == 0) 1221 return (EBADF); 1222 1223 /* If the ioctl happens here, the parent is us. */ 1224 strlcpy(dkw->dkw_parent, device_xname(sd->sc_dev), 1225 sizeof(dkw->dkw_parent)); 1226 return (dkwedge_del(dkw)); 1227 } 1228 1229 case DIOCLWEDGES: 1230 { 1231 struct dkwedge_list *dkwl = (void *) addr; 1232 1233 return (dkwedge_list(&sd->sc_dk, dkwl, l)); 1234 } 1235 1236 default: 1237 if (part != RAW_PART) 1238 return (ENOTTY); 1239 return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, l)); 1240 } 1241 1242 #ifdef DIAGNOSTIC 1243 panic("sdioctl: impossible"); 1244 #endif 1245 } 1246 1247 static void 1248 sdgetdefaultlabel(struct sd_softc *sd, struct disklabel *lp) 1249 { 1250 1251 memset(lp, 0, sizeof(struct disklabel)); 1252 1253 lp->d_secsize = sd->params.blksize; 1254 lp->d_ntracks = sd->params.heads; 1255 lp->d_nsectors = sd->params.sectors; 1256 lp->d_ncylinders = sd->params.cyls; 1257 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 1258 1259 switch (scsipi_periph_bustype(sd->sc_periph)) { 1260 case SCSIPI_BUSTYPE_SCSI: 1261 lp->d_type = DTYPE_SCSI; 1262 break; 1263 case SCSIPI_BUSTYPE_ATAPI: 1264 lp->d_type = DTYPE_ATAPI; 1265 break; 1266 } 1267 /* 1268 * XXX 1269 * We could probe the mode pages to figure out what kind of disc it is. 1270 * Is this worthwhile? 1271 */ 1272 strncpy(lp->d_typename, sd->name, 16); 1273 strncpy(lp->d_packname, "fictitious", 16); 1274 lp->d_secperunit = sd->params.disksize; 1275 lp->d_rpm = sd->params.rot_rate; 1276 lp->d_interleave = 1; 1277 lp->d_flags = sd->sc_periph->periph_flags & PERIPH_REMOVABLE ? 1278 D_REMOVABLE : 0; 1279 1280 lp->d_partitions[RAW_PART].p_offset = 0; 1281 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit; 1282 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; 1283 lp->d_npartitions = RAW_PART + 1; 1284 1285 lp->d_magic = DISKMAGIC; 1286 lp->d_magic2 = DISKMAGIC; 1287 lp->d_checksum = dkcksum(lp); 1288 } 1289 1290 1291 /* 1292 * Load the label information on the named device 1293 */ 1294 static int 1295 sdgetdisklabel(struct sd_softc *sd) 1296 { 1297 struct disklabel *lp = sd->sc_dk.dk_label; 1298 const char *errstring; 1299 1300 memset(sd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); 1301 1302 sdgetdefaultlabel(sd, lp); 1303 1304 if (lp->d_secpercyl == 0) { 1305 lp->d_secpercyl = 100; 1306 /* as long as it's not 0 - readdisklabel divides by it (?) */ 1307 } 1308 1309 /* 1310 * Call the generic disklabel extraction routine 1311 */ 1312 errstring = readdisklabel(MAKESDDEV(0, device_unit(sd->sc_dev), 1313 RAW_PART), sdstrategy, lp, sd->sc_dk.dk_cpulabel); 1314 if (errstring) { 1315 aprint_error_dev(sd->sc_dev, "%s\n", errstring); 1316 return EIO; 1317 } 1318 return 0; 1319 } 1320 1321 static bool 1322 sd_shutdown(device_t self, int how) 1323 { 1324 struct sd_softc *sd = device_private(self); 1325 1326 /* 1327 * If the disk cache needs to be flushed, and the disk supports 1328 * it, flush it. We're cold at this point, so we poll for 1329 * completion. 1330 */ 1331 if ((sd->flags & SDF_DIRTY) != 0) { 1332 if (sd_flush(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) { 1333 aprint_error_dev(sd->sc_dev, 1334 "cache synchronization failed\n"); 1335 sd->flags &= ~SDF_FLUSHING; 1336 } else 1337 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 1338 } 1339 return true; 1340 } 1341 1342 static bool 1343 sd_suspend(device_t dv PMF_FN_ARGS) 1344 { 1345 return sd_shutdown(dv, boothowto); /* XXX no need to poll */ 1346 } 1347 1348 /* 1349 * Check Errors 1350 */ 1351 static int 1352 sd_interpret_sense(struct scsipi_xfer *xs) 1353 { 1354 struct scsipi_periph *periph = xs->xs_periph; 1355 struct scsi_sense_data *sense = &xs->sense.scsi_sense; 1356 struct sd_softc *sd = device_private(periph->periph_dev); 1357 int s, error, retval = EJUSTRETURN; 1358 1359 /* 1360 * If the periph is already recovering, just do the normal 1361 * error processing. 1362 */ 1363 if (periph->periph_flags & PERIPH_RECOVERING) 1364 return (retval); 1365 1366 /* 1367 * Ignore errors from accessing illegal fields (e.g. trying to 1368 * lock the door of a digicam, which doesn't have a door that 1369 * can be locked) for the SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL command. 1370 */ 1371 if (xs->cmd->opcode == SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL && 1372 SSD_SENSE_KEY(sense->flags) == SKEY_ILLEGAL_REQUEST && 1373 sense->asc == 0x24 && 1374 sense->ascq == 0x00) { /* Illegal field in CDB */ 1375 if (!(xs->xs_control & XS_CTL_SILENT)) { 1376 scsipi_printaddr(periph); 1377 printf("no door lock\n"); 1378 } 1379 xs->xs_control |= XS_CTL_IGNORE_ILLEGAL_REQUEST; 1380 return (retval); 1381 } 1382 1383 1384 1385 /* 1386 * If the device is not open yet, let the generic code handle it. 1387 */ 1388 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) 1389 return (retval); 1390 1391 /* 1392 * If it isn't a extended or extended/deferred error, let 1393 * the generic code handle it. 1394 */ 1395 if (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT && 1396 SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED) 1397 return (retval); 1398 1399 if (SSD_SENSE_KEY(sense->flags) == SKEY_NOT_READY && 1400 sense->asc == 0x4) { 1401 if (sense->ascq == 0x01) { 1402 /* 1403 * Unit In The Process Of Becoming Ready. 1404 */ 1405 printf("%s: waiting for pack to spin up...\n", 1406 device_xname(sd->sc_dev)); 1407 if (!callout_pending(&periph->periph_callout)) 1408 scsipi_periph_freeze(periph, 1); 1409 callout_reset(&periph->periph_callout, 1410 5 * hz, scsipi_periph_timed_thaw, periph); 1411 retval = ERESTART; 1412 } else if (sense->ascq == 0x02) { 1413 printf("%s: pack is stopped, restarting...\n", 1414 device_xname(sd->sc_dev)); 1415 s = splbio(); 1416 periph->periph_flags |= PERIPH_RECOVERING; 1417 splx(s); 1418 error = scsipi_start(periph, SSS_START, 1419 XS_CTL_URGENT|XS_CTL_HEAD_TAG| 1420 XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH); 1421 if (error) { 1422 aprint_error_dev(sd->sc_dev, 1423 "unable to restart pack\n"); 1424 retval = error; 1425 } else 1426 retval = ERESTART; 1427 s = splbio(); 1428 periph->periph_flags &= ~PERIPH_RECOVERING; 1429 splx(s); 1430 } 1431 } 1432 if (SSD_SENSE_KEY(sense->flags) == SKEY_MEDIUM_ERROR && 1433 sense->asc == 0x31 && 1434 sense->ascq == 0x00) { /* maybe for any asq ? */ 1435 /* Medium Format Corrupted */ 1436 retval = EFTYPE; 1437 } 1438 return (retval); 1439 } 1440 1441 1442 static int 1443 sdsize(dev_t dev) 1444 { 1445 struct sd_softc *sd; 1446 int part, unit, omask; 1447 int size; 1448 1449 unit = SDUNIT(dev); 1450 sd = device_lookup_private(&sd_cd, unit); 1451 if (sd == NULL) 1452 return (-1); 1453 1454 if (!device_is_active(sd->sc_dev)) 1455 return (-1); 1456 1457 part = SDPART(dev); 1458 omask = sd->sc_dk.dk_openmask & (1 << part); 1459 1460 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0) 1461 return (-1); 1462 if ((sd->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) 1463 size = -1; 1464 else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP) 1465 size = -1; 1466 else 1467 size = sd->sc_dk.dk_label->d_partitions[part].p_size * 1468 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE); 1469 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0) 1470 return (-1); 1471 return (size); 1472 } 1473 1474 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */ 1475 static struct scsipi_xfer sx; 1476 static int sddoingadump; 1477 1478 /* 1479 * dump all of physical memory into the partition specified, starting 1480 * at offset 'dumplo' into the partition. 1481 */ 1482 static int 1483 sddump(dev_t dev, daddr_t blkno, void *va, size_t size) 1484 { 1485 struct sd_softc *sd; /* disk unit to do the I/O */ 1486 struct disklabel *lp; /* disk's disklabel */ 1487 int unit, part; 1488 int sectorsize; /* size of a disk sector */ 1489 int nsects; /* number of sectors in partition */ 1490 int sectoff; /* sector offset of partition */ 1491 int totwrt; /* total number of sectors left to write */ 1492 int nwrt; /* current number of sectors to write */ 1493 struct scsipi_rw_10 cmd; /* write command */ 1494 struct scsipi_xfer *xs; /* ... convenience */ 1495 struct scsipi_periph *periph; 1496 struct scsipi_channel *chan; 1497 1498 /* Check if recursive dump; if so, punt. */ 1499 if (sddoingadump) 1500 return (EFAULT); 1501 1502 /* Mark as active early. */ 1503 sddoingadump = 1; 1504 1505 unit = SDUNIT(dev); /* Decompose unit & partition. */ 1506 part = SDPART(dev); 1507 1508 /* Check for acceptable drive number. */ 1509 sd = device_lookup_private(&sd_cd, unit); 1510 if (sd == NULL) 1511 return (ENXIO); 1512 1513 if (!device_is_active(sd->sc_dev)) 1514 return (ENODEV); 1515 1516 periph = sd->sc_periph; 1517 chan = periph->periph_channel; 1518 1519 /* Make sure it was initialized. */ 1520 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) 1521 return (ENXIO); 1522 1523 /* Convert to disk sectors. Request must be a multiple of size. */ 1524 lp = sd->sc_dk.dk_label; 1525 sectorsize = lp->d_secsize; 1526 if ((size % sectorsize) != 0) 1527 return (EFAULT); 1528 totwrt = size / sectorsize; 1529 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */ 1530 1531 nsects = lp->d_partitions[part].p_size; 1532 sectoff = lp->d_partitions[part].p_offset; 1533 1534 /* Check transfer bounds against partition size. */ 1535 if ((blkno < 0) || ((blkno + totwrt) > nsects)) 1536 return (EINVAL); 1537 1538 /* Offset block number to start of partition. */ 1539 blkno += sectoff; 1540 1541 xs = &sx; 1542 1543 while (totwrt > 0) { 1544 nwrt = totwrt; /* XXX */ 1545 #ifndef SD_DUMP_NOT_TRUSTED 1546 /* 1547 * Fill out the scsi command 1548 */ 1549 memset(&cmd, 0, sizeof(cmd)); 1550 cmd.opcode = WRITE_10; 1551 _lto4b(blkno, cmd.addr); 1552 _lto2b(nwrt, cmd.length); 1553 /* 1554 * Fill out the scsipi_xfer structure 1555 * Note: we cannot sleep as we may be an interrupt 1556 * don't use scsipi_command() as it may want to wait 1557 * for an xs. 1558 */ 1559 memset(xs, 0, sizeof(sx)); 1560 xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL | 1561 XS_CTL_DATA_OUT; 1562 xs->xs_status = 0; 1563 xs->xs_periph = periph; 1564 xs->xs_retries = SDRETRIES; 1565 xs->timeout = 10000; /* 10000 millisecs for a disk ! */ 1566 xs->cmd = (struct scsipi_generic *)&cmd; 1567 xs->cmdlen = sizeof(cmd); 1568 xs->resid = nwrt * sectorsize; 1569 xs->error = XS_NOERROR; 1570 xs->bp = 0; 1571 xs->data = va; 1572 xs->datalen = nwrt * sectorsize; 1573 callout_init(&xs->xs_callout, 0); 1574 1575 /* 1576 * Pass all this info to the scsi driver. 1577 */ 1578 scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs); 1579 if ((xs->xs_status & XS_STS_DONE) == 0 || 1580 xs->error != XS_NOERROR) 1581 return (EIO); 1582 #else /* SD_DUMP_NOT_TRUSTED */ 1583 /* Let's just talk about this first... */ 1584 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno); 1585 delay(500 * 1000); /* half a second */ 1586 #endif /* SD_DUMP_NOT_TRUSTED */ 1587 1588 /* update block count */ 1589 totwrt -= nwrt; 1590 blkno += nwrt; 1591 va = (char *)va + sectorsize * nwrt; 1592 } 1593 sddoingadump = 0; 1594 return (0); 1595 } 1596 1597 static int 1598 sd_mode_sense(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size, 1599 int page, int flags, int *big) 1600 { 1601 1602 if ((sd->sc_periph->periph_quirks & PQUIRK_ONLYBIG) && 1603 !(sd->sc_periph->periph_quirks & PQUIRK_NOBIGMODESENSE)) { 1604 *big = 1; 1605 return scsipi_mode_sense_big(sd->sc_periph, byte2, page, sense, 1606 size + sizeof(struct scsi_mode_parameter_header_10), 1607 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000); 1608 } else { 1609 *big = 0; 1610 return scsipi_mode_sense(sd->sc_periph, byte2, page, sense, 1611 size + sizeof(struct scsi_mode_parameter_header_6), 1612 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000); 1613 } 1614 } 1615 1616 static int 1617 sd_mode_select(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size, 1618 int flags, int big) 1619 { 1620 1621 if (big) { 1622 struct scsi_mode_parameter_header_10 *header = sense; 1623 1624 _lto2b(0, header->data_length); 1625 return scsipi_mode_select_big(sd->sc_periph, byte2, sense, 1626 size + sizeof(struct scsi_mode_parameter_header_10), 1627 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000); 1628 } else { 1629 struct scsi_mode_parameter_header_6 *header = sense; 1630 1631 header->data_length = 0; 1632 return scsipi_mode_select(sd->sc_periph, byte2, sense, 1633 size + sizeof(struct scsi_mode_parameter_header_6), 1634 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000); 1635 } 1636 } 1637 1638 /* 1639 * sd_validate_blksize: 1640 * 1641 * Validate the block size. Print error if periph is specified, 1642 */ 1643 static int 1644 sd_validate_blksize(struct scsipi_periph *periph, int len) 1645 { 1646 1647 switch (len) { 1648 case 256: 1649 case 512: 1650 case 1024: 1651 case 2048: 1652 case 4096: 1653 return 1; 1654 } 1655 1656 if (periph) { 1657 scsipi_printaddr(periph); 1658 printf("%s sector size: 0x%x. Defaulting to %d bytes.\n", 1659 (len ^ (1 << (ffs(len) - 1))) ? 1660 "preposterous" : "unsupported", 1661 len, SD_DEFAULT_BLKSIZE); 1662 } 1663 1664 return 0; 1665 } 1666 1667 /* 1668 * sd_read_capacity: 1669 * 1670 * Find out from the device what its capacity is. 1671 */ 1672 static u_int64_t 1673 sd_read_capacity(struct scsipi_periph *periph, int *blksize, int flags) 1674 { 1675 union { 1676 struct scsipi_read_capacity_10 cmd; 1677 struct scsipi_read_capacity_16 cmd16; 1678 } cmd; 1679 union { 1680 struct scsipi_read_capacity_10_data data; 1681 struct scsipi_read_capacity_16_data data16; 1682 } *datap; 1683 uint64_t rv; 1684 1685 memset(&cmd, 0, sizeof(cmd)); 1686 cmd.cmd.opcode = READ_CAPACITY_10; 1687 1688 /* 1689 * Don't allocate data buffer on stack; 1690 * The lower driver layer might use the same stack and 1691 * if it uses region which is in the same cacheline, 1692 * cache flush ops against the data buffer won't work properly. 1693 */ 1694 datap = malloc(sizeof(*datap), M_TEMP, M_WAITOK); 1695 if (datap == NULL) 1696 return 0; 1697 1698 /* 1699 * If the command works, interpret the result as a 4 byte 1700 * number of blocks 1701 */ 1702 rv = 0; 1703 memset(datap, 0, sizeof(datap->data)); 1704 if (scsipi_command(periph, (void *)&cmd.cmd, sizeof(cmd.cmd), 1705 (void *)datap, sizeof(datap->data), SCSIPIRETRIES, 20000, NULL, 1706 flags | XS_CTL_DATA_IN | XS_CTL_SILENT) != 0) 1707 goto out; 1708 1709 if (_4btol(datap->data.addr) != 0xffffffff) { 1710 *blksize = _4btol(datap->data.length); 1711 rv = _4btol(datap->data.addr) + 1; 1712 goto out; 1713 } 1714 1715 /* 1716 * Device is larger than can be reflected by READ CAPACITY (10). 1717 * Try READ CAPACITY (16). 1718 */ 1719 1720 memset(&cmd, 0, sizeof(cmd)); 1721 cmd.cmd16.opcode = READ_CAPACITY_16; 1722 cmd.cmd16.byte2 = SRC16_SERVICE_ACTION; 1723 _lto4b(sizeof(datap->data16), cmd.cmd16.len); 1724 1725 memset(datap, 0, sizeof(datap->data16)); 1726 if (scsipi_command(periph, (void *)&cmd.cmd16, sizeof(cmd.cmd16), 1727 (void *)datap, sizeof(datap->data16), SCSIPIRETRIES, 20000, NULL, 1728 flags | XS_CTL_DATA_IN | XS_CTL_SILENT) != 0) 1729 goto out; 1730 1731 *blksize = _4btol(datap->data16.length); 1732 rv = _8btol(datap->data16.addr) + 1; 1733 1734 out: 1735 free(datap, M_TEMP); 1736 return rv; 1737 } 1738 1739 static int 1740 sd_get_simplifiedparms(struct sd_softc *sd, struct disk_parms *dp, int flags) 1741 { 1742 struct { 1743 struct scsi_mode_parameter_header_6 header; 1744 /* no block descriptor */ 1745 u_int8_t pg_code; /* page code (should be 6) */ 1746 u_int8_t pg_length; /* page length (should be 11) */ 1747 u_int8_t wcd; /* bit0: cache disable */ 1748 u_int8_t lbs[2]; /* logical block size */ 1749 u_int8_t size[5]; /* number of log. blocks */ 1750 u_int8_t pp; /* power/performance */ 1751 u_int8_t flags; 1752 u_int8_t resvd; 1753 } scsipi_sense; 1754 u_int64_t blocks; 1755 int error, blksize; 1756 1757 /* 1758 * sd_read_capacity (ie "read capacity") and mode sense page 6 1759 * give the same information. Do both for now, and check 1760 * for consistency. 1761 * XXX probably differs for removable media 1762 */ 1763 dp->blksize = SD_DEFAULT_BLKSIZE; 1764 if ((blocks = sd_read_capacity(sd->sc_periph, &blksize, flags)) == 0) 1765 return (SDGP_RESULT_OFFLINE); /* XXX? */ 1766 1767 error = scsipi_mode_sense(sd->sc_periph, SMS_DBD, 6, 1768 &scsipi_sense.header, sizeof(scsipi_sense), 1769 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000); 1770 1771 if (error != 0) 1772 return (SDGP_RESULT_OFFLINE); /* XXX? */ 1773 1774 dp->blksize = blksize; 1775 if (!sd_validate_blksize(NULL, dp->blksize)) 1776 dp->blksize = _2btol(scsipi_sense.lbs); 1777 if (!sd_validate_blksize(sd->sc_periph, dp->blksize)) 1778 dp->blksize = SD_DEFAULT_BLKSIZE; 1779 1780 /* 1781 * Create a pseudo-geometry. 1782 */ 1783 dp->heads = 64; 1784 dp->sectors = 32; 1785 dp->cyls = blocks / (dp->heads * dp->sectors); 1786 dp->disksize = _5btol(scsipi_sense.size); 1787 if (dp->disksize <= UINT32_MAX && dp->disksize != blocks) { 1788 printf("RBC size: mode sense=%llu, get cap=%llu\n", 1789 (unsigned long long)dp->disksize, 1790 (unsigned long long)blocks); 1791 dp->disksize = blocks; 1792 } 1793 dp->disksize512 = (dp->disksize * dp->blksize) / DEV_BSIZE; 1794 1795 return (SDGP_RESULT_OK); 1796 } 1797 1798 /* 1799 * Get the scsi driver to send a full inquiry to the * device and use the 1800 * results to fill out the disk parameter structure. 1801 */ 1802 static int 1803 sd_get_capacity(struct sd_softc *sd, struct disk_parms *dp, int flags) 1804 { 1805 u_int64_t blocks; 1806 int error, blksize; 1807 #if 0 1808 int i; 1809 u_int8_t *p; 1810 #endif 1811 1812 dp->disksize = blocks = sd_read_capacity(sd->sc_periph, &blksize, 1813 flags); 1814 if (blocks == 0) { 1815 struct scsipi_read_format_capacities cmd; 1816 struct { 1817 struct scsipi_capacity_list_header header; 1818 struct scsipi_capacity_descriptor desc; 1819 } __packed data; 1820 1821 memset(&cmd, 0, sizeof(cmd)); 1822 memset(&data, 0, sizeof(data)); 1823 cmd.opcode = READ_FORMAT_CAPACITIES; 1824 _lto2b(sizeof(data), cmd.length); 1825 1826 error = scsipi_command(sd->sc_periph, 1827 (void *)&cmd, sizeof(cmd), (void *)&data, sizeof(data), 1828 SDRETRIES, 20000, NULL, 1829 flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK); 1830 if (error == EFTYPE) { 1831 /* Medium Format Corrupted, handle as not formatted */ 1832 return (SDGP_RESULT_UNFORMATTED); 1833 } 1834 if (error || data.header.length == 0) 1835 return (SDGP_RESULT_OFFLINE); 1836 1837 #if 0 1838 printf("rfc: length=%d\n", data.header.length); 1839 printf("rfc result:"); for (i = sizeof(struct scsipi_capacity_list_header) + data.header.length, p = (void *)&data; i; i--, p++) printf(" %02x", *p); printf("\n"); 1840 #endif 1841 switch (data.desc.byte5 & SCSIPI_CAP_DESC_CODE_MASK) { 1842 case SCSIPI_CAP_DESC_CODE_RESERVED: 1843 case SCSIPI_CAP_DESC_CODE_FORMATTED: 1844 break; 1845 1846 case SCSIPI_CAP_DESC_CODE_UNFORMATTED: 1847 return (SDGP_RESULT_UNFORMATTED); 1848 1849 case SCSIPI_CAP_DESC_CODE_NONE: 1850 return (SDGP_RESULT_OFFLINE); 1851 } 1852 1853 dp->disksize = blocks = _4btol(data.desc.nblks); 1854 if (blocks == 0) 1855 return (SDGP_RESULT_OFFLINE); /* XXX? */ 1856 1857 blksize = _3btol(data.desc.blklen); 1858 1859 } else if (!sd_validate_blksize(NULL, blksize)) { 1860 struct sd_mode_sense_data scsipi_sense; 1861 int big, bsize; 1862 struct scsi_general_block_descriptor *bdesc; 1863 1864 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 1865 error = sd_mode_sense(sd, 0, &scsipi_sense, 1866 sizeof(scsipi_sense.blk_desc), 0, flags | XS_CTL_SILENT, &big); 1867 if (!error) { 1868 if (big) { 1869 bdesc = (void *)(&scsipi_sense.header.big + 1); 1870 bsize = _2btol(scsipi_sense.header.big.blk_desc_len); 1871 } else { 1872 bdesc = (void *)(&scsipi_sense.header.small + 1); 1873 bsize = scsipi_sense.header.small.blk_desc_len; 1874 } 1875 1876 #if 0 1877 printf("page 0 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n"); 1878 printf("page 0 bsize=%d\n", bsize); 1879 printf("page 0 ok\n"); 1880 #endif 1881 1882 if (bsize >= 8) { 1883 blksize = _3btol(bdesc->blklen); 1884 } 1885 } 1886 } 1887 1888 if (!sd_validate_blksize(sd->sc_periph, blksize)) 1889 blksize = SD_DEFAULT_BLKSIZE; 1890 1891 dp->blksize = blksize; 1892 dp->disksize512 = (blocks * dp->blksize) / DEV_BSIZE; 1893 return (0); 1894 } 1895 1896 static int 1897 sd_get_parms_page4(struct sd_softc *sd, struct disk_parms *dp, int flags) 1898 { 1899 struct sd_mode_sense_data scsipi_sense; 1900 int error; 1901 int big, byte2; 1902 size_t poffset; 1903 union scsi_disk_pages *pages; 1904 1905 byte2 = SMS_DBD; 1906 again: 1907 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 1908 error = sd_mode_sense(sd, byte2, &scsipi_sense, 1909 (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) + 1910 sizeof(scsipi_sense.pages.rigid_geometry), 4, 1911 flags | XS_CTL_SILENT, &big); 1912 if (error) { 1913 if (byte2 == SMS_DBD) { 1914 /* No result; try once more with DBD off */ 1915 byte2 = 0; 1916 goto again; 1917 } 1918 return (error); 1919 } 1920 1921 if (big) { 1922 poffset = sizeof scsipi_sense.header.big; 1923 poffset += _2btol(scsipi_sense.header.big.blk_desc_len); 1924 } else { 1925 poffset = sizeof scsipi_sense.header.small; 1926 poffset += scsipi_sense.header.small.blk_desc_len; 1927 } 1928 1929 if (poffset > sizeof(scsipi_sense) - sizeof(pages->rigid_geometry)) 1930 return ERESTART; 1931 1932 pages = (void *)((u_long)&scsipi_sense + poffset); 1933 #if 0 1934 { 1935 size_t i; 1936 u_int8_t *p; 1937 1938 printf("page 4 sense:"); 1939 for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; 1940 i--, p++) 1941 printf(" %02x", *p); 1942 printf("\n"); 1943 printf("page 4 pg_code=%d sense=%p/%p\n", 1944 pages->rigid_geometry.pg_code, &scsipi_sense, pages); 1945 } 1946 #endif 1947 1948 if ((pages->rigid_geometry.pg_code & PGCODE_MASK) != 4) 1949 return (ERESTART); 1950 1951 SC_DEBUG(sd->sc_periph, SCSIPI_DB3, 1952 ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n", 1953 _3btol(pages->rigid_geometry.ncyl), 1954 pages->rigid_geometry.nheads, 1955 _2btol(pages->rigid_geometry.st_cyl_wp), 1956 _2btol(pages->rigid_geometry.st_cyl_rwc), 1957 _2btol(pages->rigid_geometry.land_zone))); 1958 1959 /* 1960 * KLUDGE!! (for zone recorded disks) 1961 * give a number of sectors so that sec * trks * cyls 1962 * is <= disk_size 1963 * can lead to wasted space! THINK ABOUT THIS ! 1964 */ 1965 dp->heads = pages->rigid_geometry.nheads; 1966 dp->cyls = _3btol(pages->rigid_geometry.ncyl); 1967 if (dp->heads == 0 || dp->cyls == 0) 1968 return (ERESTART); 1969 dp->sectors = dp->disksize / (dp->heads * dp->cyls); /* XXX */ 1970 1971 dp->rot_rate = _2btol(pages->rigid_geometry.rpm); 1972 if (dp->rot_rate == 0) 1973 dp->rot_rate = 3600; 1974 1975 #if 0 1976 printf("page 4 ok\n"); 1977 #endif 1978 return (0); 1979 } 1980 1981 static int 1982 sd_get_parms_page5(struct sd_softc *sd, struct disk_parms *dp, int flags) 1983 { 1984 struct sd_mode_sense_data scsipi_sense; 1985 int error; 1986 int big, byte2; 1987 size_t poffset; 1988 union scsi_disk_pages *pages; 1989 1990 byte2 = SMS_DBD; 1991 again: 1992 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 1993 error = sd_mode_sense(sd, 0, &scsipi_sense, 1994 (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) + 1995 sizeof(scsipi_sense.pages.flex_geometry), 5, 1996 flags | XS_CTL_SILENT, &big); 1997 if (error) { 1998 if (byte2 == SMS_DBD) { 1999 /* No result; try once more with DBD off */ 2000 byte2 = 0; 2001 goto again; 2002 } 2003 return (error); 2004 } 2005 2006 if (big) { 2007 poffset = sizeof scsipi_sense.header.big; 2008 poffset += _2btol(scsipi_sense.header.big.blk_desc_len); 2009 } else { 2010 poffset = sizeof scsipi_sense.header.small; 2011 poffset += scsipi_sense.header.small.blk_desc_len; 2012 } 2013 2014 if (poffset > sizeof(scsipi_sense) - sizeof(pages->flex_geometry)) 2015 return ERESTART; 2016 2017 pages = (void *)((u_long)&scsipi_sense + poffset); 2018 #if 0 2019 { 2020 size_t i; 2021 u_int8_t *p; 2022 2023 printf("page 5 sense:"); 2024 for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; 2025 i--, p++) 2026 printf(" %02x", *p); 2027 printf("\n"); 2028 printf("page 5 pg_code=%d sense=%p/%p\n", 2029 pages->flex_geometry.pg_code, &scsipi_sense, pages); 2030 } 2031 #endif 2032 2033 if ((pages->flex_geometry.pg_code & PGCODE_MASK) != 5) 2034 return (ERESTART); 2035 2036 SC_DEBUG(sd->sc_periph, SCSIPI_DB3, 2037 ("%d cyls, %d heads, %d sec, %d bytes/sec\n", 2038 _3btol(pages->flex_geometry.ncyl), 2039 pages->flex_geometry.nheads, 2040 pages->flex_geometry.ph_sec_tr, 2041 _2btol(pages->flex_geometry.bytes_s))); 2042 2043 dp->heads = pages->flex_geometry.nheads; 2044 dp->cyls = _2btol(pages->flex_geometry.ncyl); 2045 dp->sectors = pages->flex_geometry.ph_sec_tr; 2046 if (dp->heads == 0 || dp->cyls == 0 || dp->sectors == 0) 2047 return (ERESTART); 2048 2049 dp->rot_rate = _2btol(pages->rigid_geometry.rpm); 2050 if (dp->rot_rate == 0) 2051 dp->rot_rate = 3600; 2052 2053 #if 0 2054 printf("page 5 ok\n"); 2055 #endif 2056 return (0); 2057 } 2058 2059 static int 2060 sd_get_parms(struct sd_softc *sd, struct disk_parms *dp, int flags) 2061 { 2062 int error; 2063 2064 /* 2065 * If offline, the SDEV_MEDIA_LOADED flag will be 2066 * cleared by the caller if necessary. 2067 */ 2068 if (sd->type == T_SIMPLE_DIRECT) { 2069 error = sd_get_simplifiedparms(sd, dp, flags); 2070 if (!error) 2071 disk_blocksize(&sd->sc_dk, dp->blksize); 2072 return (error); 2073 } 2074 2075 error = sd_get_capacity(sd, dp, flags); 2076 if (error) 2077 return (error); 2078 2079 disk_blocksize(&sd->sc_dk, dp->blksize); 2080 2081 if (sd->type == T_OPTICAL) 2082 goto page0; 2083 2084 if (sd->sc_periph->periph_flags & PERIPH_REMOVABLE) { 2085 if (!sd_get_parms_page5(sd, dp, flags) || 2086 !sd_get_parms_page4(sd, dp, flags)) 2087 return (SDGP_RESULT_OK); 2088 } else { 2089 if (!sd_get_parms_page4(sd, dp, flags) || 2090 !sd_get_parms_page5(sd, dp, flags)) 2091 return (SDGP_RESULT_OK); 2092 } 2093 2094 page0: 2095 printf("%s: fabricating a geometry\n", device_xname(sd->sc_dev)); 2096 /* Try calling driver's method for figuring out geometry. */ 2097 if (!sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom || 2098 !(*sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom) 2099 (sd->sc_periph, dp, dp->disksize)) { 2100 /* 2101 * Use adaptec standard fictitious geometry 2102 * this depends on which controller (e.g. 1542C is 2103 * different. but we have to put SOMETHING here..) 2104 */ 2105 dp->heads = 64; 2106 dp->sectors = 32; 2107 dp->cyls = dp->disksize / (64 * 32); 2108 } 2109 dp->rot_rate = 3600; 2110 return (SDGP_RESULT_OK); 2111 } 2112 2113 static int 2114 sd_flush(struct sd_softc *sd, int flags) 2115 { 2116 struct scsipi_periph *periph = sd->sc_periph; 2117 struct scsi_synchronize_cache_10 cmd; 2118 2119 /* 2120 * If the device is SCSI-2, issue a SYNCHRONIZE CACHE. 2121 * We issue with address 0 length 0, which should be 2122 * interpreted by the device as "all remaining blocks 2123 * starting at address 0". We ignore ILLEGAL REQUEST 2124 * in the event that the command is not supported by 2125 * the device, and poll for completion so that we know 2126 * that the cache has actually been flushed. 2127 * 2128 * Unless, that is, the device can't handle the SYNCHRONIZE CACHE 2129 * command, as indicated by our quirks flags. 2130 * 2131 * XXX What about older devices? 2132 */ 2133 if (periph->periph_version < 2 || 2134 (periph->periph_quirks & PQUIRK_NOSYNCCACHE)) 2135 return (0); 2136 2137 sd->flags |= SDF_FLUSHING; 2138 memset(&cmd, 0, sizeof(cmd)); 2139 cmd.opcode = SCSI_SYNCHRONIZE_CACHE_10; 2140 2141 return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0, 2142 SDRETRIES, 100000, NULL, flags | XS_CTL_IGNORE_ILLEGAL_REQUEST)); 2143 } 2144 2145 static int 2146 sd_getcache(struct sd_softc *sd, int *bitsp) 2147 { 2148 struct scsipi_periph *periph = sd->sc_periph; 2149 struct sd_mode_sense_data scsipi_sense; 2150 int error, bits = 0; 2151 int big; 2152 union scsi_disk_pages *pages; 2153 2154 if (periph->periph_version < 2) 2155 return (EOPNOTSUPP); 2156 2157 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 2158 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense, 2159 sizeof(scsipi_sense.pages.caching_params), 8, 0, &big); 2160 if (error) 2161 return (error); 2162 2163 if (big) 2164 pages = (void *)(&scsipi_sense.header.big + 1); 2165 else 2166 pages = (void *)(&scsipi_sense.header.small + 1); 2167 2168 if ((pages->caching_params.flags & CACHING_RCD) == 0) 2169 bits |= DKCACHE_READ; 2170 if (pages->caching_params.flags & CACHING_WCE) 2171 bits |= DKCACHE_WRITE; 2172 if (pages->caching_params.pg_code & PGCODE_PS) 2173 bits |= DKCACHE_SAVE; 2174 2175 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 2176 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense, 2177 sizeof(scsipi_sense.pages.caching_params), 2178 SMS_PCTRL_CHANGEABLE|8, 0, &big); 2179 if (error == 0) { 2180 if (big) 2181 pages = (void *)(&scsipi_sense.header.big + 1); 2182 else 2183 pages = (void *)(&scsipi_sense.header.small + 1); 2184 2185 if (pages->caching_params.flags & CACHING_RCD) 2186 bits |= DKCACHE_RCHANGE; 2187 if (pages->caching_params.flags & CACHING_WCE) 2188 bits |= DKCACHE_WCHANGE; 2189 } 2190 2191 *bitsp = bits; 2192 2193 return (0); 2194 } 2195 2196 static int 2197 sd_setcache(struct sd_softc *sd, int bits) 2198 { 2199 struct scsipi_periph *periph = sd->sc_periph; 2200 struct sd_mode_sense_data scsipi_sense; 2201 int error; 2202 uint8_t oflags, byte2 = 0; 2203 int big; 2204 union scsi_disk_pages *pages; 2205 2206 if (periph->periph_version < 2) 2207 return (EOPNOTSUPP); 2208 2209 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 2210 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense, 2211 sizeof(scsipi_sense.pages.caching_params), 8, 0, &big); 2212 if (error) 2213 return (error); 2214 2215 if (big) 2216 pages = (void *)(&scsipi_sense.header.big + 1); 2217 else 2218 pages = (void *)(&scsipi_sense.header.small + 1); 2219 2220 oflags = pages->caching_params.flags; 2221 2222 if (bits & DKCACHE_READ) 2223 pages->caching_params.flags &= ~CACHING_RCD; 2224 else 2225 pages->caching_params.flags |= CACHING_RCD; 2226 2227 if (bits & DKCACHE_WRITE) 2228 pages->caching_params.flags |= CACHING_WCE; 2229 else 2230 pages->caching_params.flags &= ~CACHING_WCE; 2231 2232 if (oflags == pages->caching_params.flags) 2233 return (0); 2234 2235 pages->caching_params.pg_code &= PGCODE_MASK; 2236 2237 if (bits & DKCACHE_SAVE) 2238 byte2 |= SMS_SP; 2239 2240 return (sd_mode_select(sd, byte2|SMS_PF, &scsipi_sense, 2241 sizeof(struct scsi_mode_page_header) + 2242 pages->caching_params.pg_length, 0, big)); 2243 } 2244 2245 static void 2246 sd_set_properties(struct sd_softc *sd) 2247 { 2248 prop_dictionary_t disk_info, odisk_info, geom; 2249 2250 disk_info = prop_dictionary_create(); 2251 2252 geom = prop_dictionary_create(); 2253 2254 prop_dictionary_set_uint64(geom, "sectors-per-unit", 2255 sd->params.disksize); 2256 2257 prop_dictionary_set_uint32(geom, "sector-size", 2258 sd->params.blksize); 2259 2260 prop_dictionary_set_uint16(geom, "sectors-per-track", 2261 sd->params.sectors); 2262 2263 prop_dictionary_set_uint16(geom, "tracks-per-cylinder", 2264 sd->params.heads); 2265 2266 prop_dictionary_set_uint64(geom, "cylinders-per-unit", 2267 sd->params.cyls); 2268 2269 prop_dictionary_set(disk_info, "geometry", geom); 2270 prop_object_release(geom); 2271 2272 prop_dictionary_set(device_properties(sd->sc_dev), 2273 "disk-info", disk_info); 2274 2275 /* 2276 * Don't release disk_info here; we keep a reference to it. 2277 * disk_detach() will release it when we go away. 2278 */ 2279 2280 odisk_info = sd->sc_dk.dk_info; 2281 sd->sc_dk.dk_info = disk_info; 2282 if (odisk_info) 2283 prop_object_release(odisk_info); 2284 } 2285