1 /* $NetBSD: sd.c,v 1.279 2009/04/10 17:36:42 dyoung 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.279 2009/04/10 17:36:42 dyoung 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 107 static int sd_mode_sense(struct sd_softc *, u_int8_t, void *, size_t, int, 108 int, int *); 109 static int sd_mode_select(struct sd_softc *, u_int8_t, void *, size_t, int, 110 int); 111 static int sd_validate_blksize(struct scsipi_periph *, int); 112 static u_int64_t sd_read_capacity(struct scsipi_periph *, int *, int flags); 113 static int sd_get_simplifiedparms(struct sd_softc *, struct disk_parms *, 114 int); 115 static int sd_get_capacity(struct sd_softc *, struct disk_parms *, int); 116 static int sd_get_parms(struct sd_softc *, struct disk_parms *, int); 117 static int sd_get_parms_page4(struct sd_softc *, struct disk_parms *, 118 int); 119 static int sd_get_parms_page5(struct sd_softc *, struct disk_parms *, 120 int); 121 122 static int sd_flush(struct sd_softc *, int); 123 static int sd_getcache(struct sd_softc *, int *); 124 static int sd_setcache(struct sd_softc *, int); 125 126 static int sdmatch(struct device *, struct cfdata *, void *); 127 static void sdattach(struct device *, struct device *, void *); 128 static int sdactivate(struct device *, enum devact); 129 static int sddetach(struct device *, int); 130 static void sd_set_properties(struct sd_softc *); 131 132 CFATTACH_DECL3_NEW(sd, sizeof(struct sd_softc), sdmatch, sdattach, sddetach, 133 sdactivate, 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(struct device *parent, struct cfdata *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(struct device *parent, struct device *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 sdactivate(struct device *self, enum devact act) 331 { 332 int rv = 0; 333 334 switch (act) { 335 case DVACT_ACTIVATE: 336 rv = EOPNOTSUPP; 337 break; 338 339 case DVACT_DEACTIVATE: 340 /* 341 * Nothing to do; we key off the device's DVF_ACTIVE. 342 */ 343 break; 344 } 345 return (rv); 346 } 347 348 static int 349 sddetach(struct device *self, int flags) 350 { 351 struct sd_softc *sd = device_private(self); 352 int s, bmaj, cmaj, i, mn; 353 354 /* locate the major number */ 355 bmaj = bdevsw_lookup_major(&sd_bdevsw); 356 cmaj = cdevsw_lookup_major(&sd_cdevsw); 357 358 /* Nuke the vnodes for any open instances */ 359 for (i = 0; i < MAXPARTITIONS; i++) { 360 mn = SDMINOR(device_unit(self), i); 361 vdevgone(bmaj, mn, mn, VBLK); 362 vdevgone(cmaj, mn, mn, VCHR); 363 } 364 365 /* kill any pending restart */ 366 callout_stop(&sd->sc_callout); 367 368 /* Delete all of our wedges. */ 369 dkwedge_delall(&sd->sc_dk); 370 371 s = splbio(); 372 373 /* Kill off any queued buffers. */ 374 bufq_drain(sd->buf_queue); 375 376 bufq_free(sd->buf_queue); 377 378 /* Kill off any pending commands. */ 379 scsipi_kill_pending(sd->sc_periph); 380 381 splx(s); 382 383 /* Detach from the disk list. */ 384 disk_detach(&sd->sc_dk); 385 disk_destroy(&sd->sc_dk); 386 387 callout_destroy(&sd->sc_callout); 388 389 pmf_device_deregister(self); 390 391 #if NRND > 0 392 /* Unhook the entropy source. */ 393 rnd_detach_source(&sd->rnd_source); 394 #endif 395 396 return (0); 397 } 398 399 /* 400 * open the device. Make sure the partition info is a up-to-date as can be. 401 */ 402 static int 403 sdopen(dev_t dev, int flag, int fmt, struct lwp *l) 404 { 405 struct sd_softc *sd; 406 struct scsipi_periph *periph; 407 struct scsipi_adapter *adapt; 408 int unit, part; 409 int error; 410 411 unit = SDUNIT(dev); 412 sd = device_lookup_private(&sd_cd, unit); 413 if (sd == NULL) 414 return (ENXIO); 415 416 if (!device_is_active(sd->sc_dev)) 417 return (ENODEV); 418 419 part = SDPART(dev); 420 421 mutex_enter(&sd->sc_dk.dk_openlock); 422 423 /* 424 * If there are wedges, and this is not RAW_PART, then we 425 * need to fail. 426 */ 427 if (sd->sc_dk.dk_nwedges != 0 && part != RAW_PART) { 428 error = EBUSY; 429 goto bad1; 430 } 431 432 periph = sd->sc_periph; 433 adapt = periph->periph_channel->chan_adapter; 434 435 SC_DEBUG(periph, SCSIPI_DB1, 436 ("sdopen: dev=0x%"PRIx64" (unit %d (of %d), partition %d)\n", dev, unit, 437 sd_cd.cd_ndevs, part)); 438 439 /* 440 * If this is the first open of this device, add a reference 441 * to the adapter. 442 */ 443 if (sd->sc_dk.dk_openmask == 0 && 444 (error = scsipi_adapter_addref(adapt)) != 0) 445 goto bad1; 446 447 if ((periph->periph_flags & PERIPH_OPEN) != 0) { 448 /* 449 * If any partition is open, but the disk has been invalidated, 450 * disallow further opens of non-raw partition 451 */ 452 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 && 453 (part != RAW_PART || fmt != S_IFCHR)) { 454 error = EIO; 455 goto bad2; 456 } 457 } else { 458 int silent; 459 460 if (part == RAW_PART && fmt == S_IFCHR) 461 silent = XS_CTL_SILENT; 462 else 463 silent = 0; 464 465 /* Check that it is still responding and ok. */ 466 error = scsipi_test_unit_ready(periph, 467 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE | 468 silent); 469 470 /* 471 * Start the pack spinning if necessary. Always allow the 472 * raw parition to be opened, for raw IOCTLs. Data transfers 473 * will check for SDEV_MEDIA_LOADED. 474 */ 475 if (error == EIO) { 476 int error2; 477 478 error2 = scsipi_start(periph, SSS_START, silent); 479 switch (error2) { 480 case 0: 481 error = 0; 482 break; 483 case EIO: 484 case EINVAL: 485 break; 486 default: 487 error = error2; 488 break; 489 } 490 } 491 if (error) { 492 if (silent) 493 goto out; 494 goto bad2; 495 } 496 497 periph->periph_flags |= PERIPH_OPEN; 498 499 if (periph->periph_flags & PERIPH_REMOVABLE) { 500 /* Lock the pack in. */ 501 error = scsipi_prevent(periph, SPAMR_PREVENT_DT, 502 XS_CTL_IGNORE_ILLEGAL_REQUEST | 503 XS_CTL_IGNORE_MEDIA_CHANGE | 504 XS_CTL_SILENT); 505 if (error) 506 goto bad3; 507 } 508 509 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 510 int param_error; 511 periph->periph_flags |= PERIPH_MEDIA_LOADED; 512 513 /* 514 * Load the physical device parameters. 515 * 516 * Note that if media is present but unformatted, 517 * we allow the open (so that it can be formatted!). 518 * The drive should refuse real I/O, if the media is 519 * unformatted. 520 */ 521 if ((param_error = sd_get_parms(sd, &sd->params, 0)) 522 == SDGP_RESULT_OFFLINE) { 523 error = ENXIO; 524 periph->periph_flags &= ~PERIPH_MEDIA_LOADED; 525 goto bad3; 526 } 527 SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded ")); 528 529 /* Load the partition info if not already loaded. */ 530 if (param_error == 0) { 531 if ((sdgetdisklabel(sd) != 0) && (part != RAW_PART)) { 532 error = EIO; 533 goto bad3; 534 } 535 SC_DEBUG(periph, SCSIPI_DB3, 536 ("Disklabel loaded ")); 537 } 538 } 539 } 540 541 /* Check that the partition exists. */ 542 if (part != RAW_PART && 543 (part >= sd->sc_dk.dk_label->d_npartitions || 544 sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) { 545 error = ENXIO; 546 goto bad3; 547 } 548 549 out: /* Insure only one open at a time. */ 550 switch (fmt) { 551 case S_IFCHR: 552 sd->sc_dk.dk_copenmask |= (1 << part); 553 break; 554 case S_IFBLK: 555 sd->sc_dk.dk_bopenmask |= (1 << part); 556 break; 557 } 558 sd->sc_dk.dk_openmask = 559 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask; 560 561 SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n")); 562 mutex_exit(&sd->sc_dk.dk_openlock); 563 return (0); 564 565 bad3: 566 if (sd->sc_dk.dk_openmask == 0) { 567 if (periph->periph_flags & PERIPH_REMOVABLE) 568 scsipi_prevent(periph, SPAMR_ALLOW, 569 XS_CTL_IGNORE_ILLEGAL_REQUEST | 570 XS_CTL_IGNORE_MEDIA_CHANGE | 571 XS_CTL_SILENT); 572 periph->periph_flags &= ~PERIPH_OPEN; 573 } 574 575 bad2: 576 if (sd->sc_dk.dk_openmask == 0) 577 scsipi_adapter_delref(adapt); 578 579 bad1: 580 mutex_exit(&sd->sc_dk.dk_openlock); 581 return (error); 582 } 583 584 /* 585 * close the device.. only called if we are the LAST occurence of an open 586 * device. Convenient now but usually a pain. 587 */ 588 static int 589 sdclose(dev_t dev, int flag, int fmt, struct lwp *l) 590 { 591 struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(dev)); 592 struct scsipi_periph *periph = sd->sc_periph; 593 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; 594 int part = SDPART(dev); 595 596 mutex_enter(&sd->sc_dk.dk_openlock); 597 switch (fmt) { 598 case S_IFCHR: 599 sd->sc_dk.dk_copenmask &= ~(1 << part); 600 break; 601 case S_IFBLK: 602 sd->sc_dk.dk_bopenmask &= ~(1 << part); 603 break; 604 } 605 sd->sc_dk.dk_openmask = 606 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask; 607 608 if (sd->sc_dk.dk_openmask == 0) { 609 /* 610 * If the disk cache needs flushing, and the disk supports 611 * it, do it now. 612 */ 613 if ((sd->flags & SDF_DIRTY) != 0) { 614 if (sd_flush(sd, 0)) { 615 aprint_error_dev(sd->sc_dev, 616 "cache synchronization failed\n"); 617 sd->flags &= ~SDF_FLUSHING; 618 } else 619 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 620 } 621 622 scsipi_wait_drain(periph); 623 624 if (periph->periph_flags & PERIPH_REMOVABLE) 625 scsipi_prevent(periph, SPAMR_ALLOW, 626 XS_CTL_IGNORE_ILLEGAL_REQUEST | 627 XS_CTL_IGNORE_NOT_READY | 628 XS_CTL_SILENT); 629 periph->periph_flags &= ~PERIPH_OPEN; 630 631 scsipi_wait_drain(periph); 632 633 scsipi_adapter_delref(adapt); 634 } 635 636 mutex_exit(&sd->sc_dk.dk_openlock); 637 return (0); 638 } 639 640 /* 641 * Actually translate the requested transfer into one the physical driver 642 * can understand. The transfer is described by a buf and will include 643 * only one physical transfer. 644 */ 645 static void 646 sdstrategy(struct buf *bp) 647 { 648 struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(bp->b_dev)); 649 struct scsipi_periph *periph = sd->sc_periph; 650 struct disklabel *lp; 651 daddr_t blkno; 652 int s; 653 bool sector_aligned; 654 655 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdstrategy ")); 656 SC_DEBUG(sd->sc_periph, SCSIPI_DB1, 657 ("%d bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno)); 658 /* 659 * If the device has been made invalid, error out 660 */ 661 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 || 662 !device_is_active(sd->sc_dev)) { 663 if (periph->periph_flags & PERIPH_OPEN) 664 bp->b_error = EIO; 665 else 666 bp->b_error = ENODEV; 667 goto done; 668 } 669 670 lp = sd->sc_dk.dk_label; 671 672 /* 673 * The transfer must be a whole number of blocks, offset must not be 674 * negative. 675 */ 676 if (lp->d_secsize == DEV_BSIZE) { 677 sector_aligned = (bp->b_bcount & (DEV_BSIZE - 1)) == 0; 678 } else { 679 sector_aligned = (bp->b_bcount % lp->d_secsize) == 0; 680 } 681 if (!sector_aligned || bp->b_blkno < 0) { 682 bp->b_error = EINVAL; 683 goto done; 684 } 685 /* 686 * If it's a null transfer, return immediatly 687 */ 688 if (bp->b_bcount == 0) 689 goto done; 690 691 /* 692 * Do bounds checking, adjust transfer. if error, process. 693 * If end of partition, just return. 694 */ 695 if (SDPART(bp->b_dev) == RAW_PART) { 696 if (bounds_check_with_mediasize(bp, DEV_BSIZE, 697 sd->params.disksize512) <= 0) 698 goto done; 699 } else { 700 if (bounds_check_with_label(&sd->sc_dk, bp, 701 (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0) 702 goto done; 703 } 704 705 /* 706 * Now convert the block number to absolute and put it in 707 * terms of the device's logical block size. 708 */ 709 if (lp->d_secsize == DEV_BSIZE) 710 blkno = bp->b_blkno; 711 else if (lp->d_secsize > DEV_BSIZE) 712 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE); 713 else 714 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize); 715 716 if (SDPART(bp->b_dev) != RAW_PART) 717 blkno += lp->d_partitions[SDPART(bp->b_dev)].p_offset; 718 719 bp->b_rawblkno = blkno; 720 721 s = splbio(); 722 723 /* 724 * Place it in the queue of disk activities for this disk. 725 * 726 * XXX Only do disksort() if the current operating mode does not 727 * XXX include tagged queueing. 728 */ 729 bufq_put(sd->buf_queue, bp); 730 731 /* 732 * Tell the device to get going on the transfer if it's 733 * not doing anything, otherwise just wait for completion 734 */ 735 sdstart(sd->sc_periph); 736 737 splx(s); 738 return; 739 740 done: 741 /* 742 * Correctly set the buf to indicate a completed xfer 743 */ 744 bp->b_resid = bp->b_bcount; 745 biodone(bp); 746 } 747 748 /* 749 * sdstart looks to see if there is a buf waiting for the device 750 * and that the device is not already busy. If both are true, 751 * It dequeues the buf and creates a scsi command to perform the 752 * transfer in the buf. The transfer request will call scsipi_done 753 * on completion, which will in turn call this routine again 754 * so that the next queued transfer is performed. 755 * The bufs are queued by the strategy routine (sdstrategy) 756 * 757 * This routine is also called after other non-queued requests 758 * have been made of the scsi driver, to ensure that the queue 759 * continues to be drained. 760 * 761 * must be called at the correct (highish) spl level 762 * sdstart() is called at splbio from sdstrategy, sdrestart and scsipi_done 763 */ 764 static void 765 sdstart(struct scsipi_periph *periph) 766 { 767 struct sd_softc *sd = device_private(periph->periph_dev); 768 struct disklabel *lp = sd->sc_dk.dk_label; 769 struct buf *bp = 0; 770 struct scsipi_rw_16 cmd16; 771 struct scsipi_rw_10 cmd_big; 772 struct scsi_rw_6 cmd_small; 773 struct scsipi_generic *cmdp; 774 struct scsipi_xfer *xs; 775 int nblks, cmdlen, error, flags; 776 777 SC_DEBUG(periph, SCSIPI_DB2, ("sdstart ")); 778 /* 779 * Check if the device has room for another command 780 */ 781 while (periph->periph_active < periph->periph_openings) { 782 /* 783 * there is excess capacity, but a special waits 784 * It'll need the adapter as soon as we clear out of the 785 * way and let it run (user level wait). 786 */ 787 if (periph->periph_flags & PERIPH_WAITING) { 788 periph->periph_flags &= ~PERIPH_WAITING; 789 wakeup((void *)periph); 790 return; 791 } 792 793 /* 794 * If the device has become invalid, abort all the 795 * reads and writes until all files have been closed and 796 * re-opened 797 */ 798 if (__predict_false( 799 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)) { 800 if ((bp = bufq_get(sd->buf_queue)) != NULL) { 801 bp->b_error = EIO; 802 bp->b_resid = bp->b_bcount; 803 biodone(bp); 804 continue; 805 } else { 806 return; 807 } 808 } 809 810 /* 811 * See if there is a buf with work for us to do.. 812 */ 813 if ((bp = bufq_peek(sd->buf_queue)) == NULL) 814 return; 815 816 /* 817 * We have a buf, now we should make a command. 818 */ 819 820 if (lp->d_secsize == DEV_BSIZE) 821 nblks = bp->b_bcount >> DEV_BSHIFT; 822 else 823 nblks = howmany(bp->b_bcount, lp->d_secsize); 824 825 /* 826 * Fill out the scsi command. Use the smallest CDB possible 827 * (6-byte, 10-byte, or 16-byte). 828 */ 829 if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) && 830 ((nblks & 0xff) == nblks) && 831 !(periph->periph_quirks & PQUIRK_ONLYBIG)) { 832 /* 6-byte CDB */ 833 memset(&cmd_small, 0, sizeof(cmd_small)); 834 cmd_small.opcode = (bp->b_flags & B_READ) ? 835 SCSI_READ_6_COMMAND : SCSI_WRITE_6_COMMAND; 836 _lto3b(bp->b_rawblkno, cmd_small.addr); 837 cmd_small.length = nblks & 0xff; 838 cmdlen = sizeof(cmd_small); 839 cmdp = (struct scsipi_generic *)&cmd_small; 840 } else if ((bp->b_rawblkno & 0xffffffff) == bp->b_rawblkno) { 841 /* 10-byte CDB */ 842 memset(&cmd_big, 0, sizeof(cmd_big)); 843 cmd_big.opcode = (bp->b_flags & B_READ) ? 844 READ_10 : WRITE_10; 845 _lto4b(bp->b_rawblkno, cmd_big.addr); 846 _lto2b(nblks, cmd_big.length); 847 cmdlen = sizeof(cmd_big); 848 cmdp = (struct scsipi_generic *)&cmd_big; 849 } else { 850 /* 16-byte CDB */ 851 memset(&cmd16, 0, sizeof(cmd16)); 852 cmd16.opcode = (bp->b_flags & B_READ) ? 853 READ_16 : WRITE_16; 854 _lto8b(bp->b_rawblkno, cmd16.addr); 855 _lto4b(nblks, cmd16.length); 856 cmdlen = sizeof(cmd16); 857 cmdp = (struct scsipi_generic *)&cmd16; 858 } 859 860 /* Instrumentation. */ 861 disk_busy(&sd->sc_dk); 862 863 /* 864 * Mark the disk dirty so that the cache will be 865 * flushed on close. 866 */ 867 if ((bp->b_flags & B_READ) == 0) 868 sd->flags |= SDF_DIRTY; 869 870 /* 871 * Figure out what flags to use. 872 */ 873 flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_SIMPLE_TAG; 874 if (bp->b_flags & B_READ) 875 flags |= XS_CTL_DATA_IN; 876 else 877 flags |= XS_CTL_DATA_OUT; 878 879 /* 880 * Call the routine that chats with the adapter. 881 * Note: we cannot sleep as we may be an interrupt 882 */ 883 xs = scsipi_make_xs(periph, cmdp, cmdlen, 884 (u_char *)bp->b_data, bp->b_bcount, 885 SDRETRIES, SD_IO_TIMEOUT, bp, flags); 886 if (__predict_false(xs == NULL)) { 887 /* 888 * out of memory. Keep this buffer in the queue, and 889 * retry later. 890 */ 891 callout_reset(&sd->sc_callout, hz / 2, sdrestart, 892 periph); 893 return; 894 } 895 /* 896 * need to dequeue the buffer before queuing the command, 897 * because cdstart may be called recursively from the 898 * HBA driver 899 */ 900 #ifdef DIAGNOSTIC 901 if (bufq_get(sd->buf_queue) != bp) 902 panic("sdstart(): dequeued wrong buf"); 903 #else 904 bufq_get(sd->buf_queue); 905 #endif 906 error = scsipi_execute_xs(xs); 907 /* with a scsipi_xfer preallocated, scsipi_command can't fail */ 908 KASSERT(error == 0); 909 } 910 } 911 912 static void 913 sdrestart(void *v) 914 { 915 int s = splbio(); 916 sdstart((struct scsipi_periph *)v); 917 splx(s); 918 } 919 920 static void 921 sddone(struct scsipi_xfer *xs, int error) 922 { 923 struct sd_softc *sd = device_private(xs->xs_periph->periph_dev); 924 struct buf *bp = xs->bp; 925 926 if (sd->flags & SDF_FLUSHING) { 927 /* Flush completed, no longer dirty. */ 928 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 929 } 930 931 if (bp) { 932 bp->b_error = error; 933 bp->b_resid = xs->resid; 934 if (error) { 935 /* on a read/write error bp->b_resid is zero, so fix */ 936 bp->b_resid = bp->b_bcount; 937 } 938 939 disk_unbusy(&sd->sc_dk, bp->b_bcount - bp->b_resid, 940 (bp->b_flags & B_READ)); 941 #if NRND > 0 942 rnd_add_uint32(&sd->rnd_source, bp->b_rawblkno); 943 #endif 944 945 biodone(bp); 946 } 947 } 948 949 static void 950 sdminphys(struct buf *bp) 951 { 952 struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(bp->b_dev)); 953 long xmax; 954 955 /* 956 * If the device is ancient, we want to make sure that 957 * the transfer fits into a 6-byte cdb. 958 * 959 * XXX Note that the SCSI-I spec says that 256-block transfers 960 * are allowed in a 6-byte read/write, and are specified 961 * by settng the "length" to 0. However, we're conservative 962 * here, allowing only 255-block transfers in case an 963 * ancient device gets confused by length == 0. A length of 0 964 * in a 10-byte read/write actually means 0 blocks. 965 */ 966 if ((sd->flags & SDF_ANCIENT) && 967 ((sd->sc_periph->periph_flags & 968 (PERIPH_REMOVABLE | PERIPH_MEDIA_LOADED)) != PERIPH_REMOVABLE)) { 969 xmax = sd->sc_dk.dk_label->d_secsize * 0xff; 970 971 if (bp->b_bcount > xmax) 972 bp->b_bcount = xmax; 973 } 974 975 scsipi_adapter_minphys(sd->sc_periph->periph_channel, bp); 976 } 977 978 static int 979 sdread(dev_t dev, struct uio *uio, int ioflag) 980 { 981 982 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio)); 983 } 984 985 static int 986 sdwrite(dev_t dev, struct uio *uio, int ioflag) 987 { 988 989 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio)); 990 } 991 992 /* 993 * Perform special action on behalf of the user 994 * Knows about the internals of this device 995 */ 996 static int 997 sdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 998 { 999 struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(dev)); 1000 struct scsipi_periph *periph = sd->sc_periph; 1001 int part = SDPART(dev); 1002 int error = 0; 1003 #ifdef __HAVE_OLD_DISKLABEL 1004 struct disklabel *newlabel = NULL; 1005 #endif 1006 1007 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdioctl 0x%lx ", cmd)); 1008 1009 /* 1010 * If the device is not valid, some IOCTLs can still be 1011 * handled on the raw partition. Check this here. 1012 */ 1013 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 1014 switch (cmd) { 1015 case DIOCKLABEL: 1016 case DIOCWLABEL: 1017 case DIOCLOCK: 1018 case DIOCEJECT: 1019 case ODIOCEJECT: 1020 case DIOCGCACHE: 1021 case DIOCSCACHE: 1022 case SCIOCIDENTIFY: 1023 case OSCIOCIDENTIFY: 1024 case SCIOCCOMMAND: 1025 case SCIOCDEBUG: 1026 if (part == RAW_PART) 1027 break; 1028 /* FALLTHROUGH */ 1029 default: 1030 if ((periph->periph_flags & PERIPH_OPEN) == 0) 1031 return (ENODEV); 1032 else 1033 return (EIO); 1034 } 1035 } 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