1 /* $NetBSD: sd.c,v 1.345 2025/04/13 14:01:00 jakllsch 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 (at) 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 (at) dialix.oz.au) 47 * Sept 1992 48 */ 49 50 #include <sys/cdefs.h> 51 __KERNEL_RCSID(0, "$NetBSD: sd.c,v 1.345 2025/04/13 14:01:00 jakllsch Exp $"); 52 53 #ifdef _KERNEL_OPT 54 #include "opt_scsi.h" 55 #endif 56 57 #include <sys/param.h> 58 #include <sys/systm.h> 59 #include <sys/kernel.h> 60 #include <sys/file.h> 61 #include <sys/stat.h> 62 #include <sys/ioctl.h> 63 #include <sys/scsiio.h> 64 #include <sys/buf.h> 65 #include <sys/bufq.h> 66 #include <sys/uio.h> 67 #include <sys/malloc.h> 68 #include <sys/errno.h> 69 #include <sys/device.h> 70 #include <sys/disklabel.h> 71 #include <sys/disk.h> 72 #include <sys/proc.h> 73 #include <sys/conf.h> 74 #include <sys/vnode.h> 75 76 #include <dev/scsipi/scsi_spc.h> 77 #include <dev/scsipi/scsipi_all.h> 78 #include <dev/scsipi/scsi_all.h> 79 #include <dev/scsipi/scsipi_disk.h> 80 #include <dev/scsipi/scsi_disk.h> 81 #include <dev/scsipi/scsiconf.h> 82 #include <dev/scsipi/scsipi_base.h> 83 #include <dev/scsipi/sdvar.h> 84 85 #include <prop/proplib.h> 86 87 #define SDUNIT(dev) DISKUNIT(dev) 88 #define SDPART(dev) DISKPART(dev) 89 #define SDMINOR(unit, part) DISKMINOR(unit, part) 90 #define MAKESDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part) 91 92 #define SDLABELDEV(dev) (MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART)) 93 94 #define SD_DEFAULT_BLKSIZE 512 95 96 static void sdminphys(struct buf *); 97 static void sdstart(struct scsipi_periph *); 98 static void sdrestart(void *); 99 static void sddone(struct scsipi_xfer *, int); 100 static bool sd_suspend(device_t, const pmf_qual_t *); 101 static bool sd_shutdown(device_t, int); 102 static int sd_interpret_sense(struct scsipi_xfer *); 103 static int sd_diskstart(device_t, struct buf *); 104 static int sd_dumpblocks(device_t, void *, daddr_t, int); 105 static void sd_iosize(device_t, int *); 106 static int sd_lastclose(device_t); 107 static int sd_firstopen(device_t, dev_t, int, int); 108 static void sd_label(device_t, struct disklabel *); 109 static int sd_discard(device_t, off_t, off_t); 110 111 static int sd_mode_sense(struct sd_softc *, u_int8_t, void *, size_t, int, 112 int, int *); 113 static int sd_mode_select(struct sd_softc *, u_int8_t, void *, size_t, 114 int, int); 115 static int sd_validate_blksize(struct scsipi_periph *, int); 116 static u_int64_t sd_read_capacity(struct sd_softc *, int *, int flags); 117 static int sd_get_simplifiedparms(struct sd_softc *, struct disk_parms *, 118 int); 119 static int sd_get_capacity(struct sd_softc *, struct disk_parms *, int); 120 static int sd_get_parms(struct sd_softc *, struct disk_parms *, int); 121 static int sd_get_parms_page4(struct sd_softc *, struct disk_parms *, 122 int); 123 static int sd_get_parms_page5(struct sd_softc *, struct disk_parms *, 124 int); 125 126 static int sd_flush(struct sd_softc *, int); 127 static int sd_getcache(struct sd_softc *, int *); 128 static int sd_setcache(struct sd_softc *, int); 129 130 static int sdmatch(device_t, cfdata_t, void *); 131 static void sdattach(device_t, device_t, void *); 132 static int sddetach(device_t, int); 133 static void sd_set_geometry(struct sd_softc *); 134 135 CFATTACH_DECL3_NEW(sd, sizeof(struct sd_softc), sdmatch, sdattach, sddetach, 136 NULL, NULL, NULL, DVF_DETACH_SHUTDOWN); 137 138 extern struct cfdriver sd_cd; 139 140 static const struct scsipi_inquiry_pattern sd_patterns[] = { 141 {T_DIRECT, T_FIXED, 142 "", "", ""}, 143 {T_DIRECT, T_REMOV, 144 "", "", ""}, 145 {T_OPTICAL, T_FIXED, 146 "", "", ""}, 147 {T_OPTICAL, T_REMOV, 148 "", "", ""}, 149 {T_SIMPLE_DIRECT, T_FIXED, 150 "", "", ""}, 151 {T_SIMPLE_DIRECT, T_REMOV, 152 "", "", ""}, 153 }; 154 155 static dev_type_open(sdopen); 156 static dev_type_close(sdclose); 157 static dev_type_read(sdread); 158 static dev_type_write(sdwrite); 159 static dev_type_ioctl(sdioctl); 160 static dev_type_strategy(sdstrategy); 161 static dev_type_dump(sddump); 162 static dev_type_size(sdsize); 163 static dev_type_discard(sddiscard); 164 165 const struct bdevsw sd_bdevsw = { 166 .d_open = sdopen, 167 .d_close = sdclose, 168 .d_strategy = sdstrategy, 169 .d_ioctl = sdioctl, 170 .d_dump = sddump, 171 .d_psize = sdsize, 172 .d_discard = sddiscard, 173 .d_cfdriver = &sd_cd, 174 .d_devtounit = disklabel_dev_unit, 175 .d_flag = D_DISK | D_MPSAFE 176 }; 177 178 const struct cdevsw sd_cdevsw = { 179 .d_open = sdopen, 180 .d_close = sdclose, 181 .d_read = sdread, 182 .d_write = sdwrite, 183 .d_ioctl = sdioctl, 184 .d_stop = nostop, 185 .d_tty = notty, 186 .d_poll = nopoll, 187 .d_mmap = nommap, 188 .d_kqfilter = nokqfilter, 189 .d_discard = sddiscard, 190 .d_cfdriver = &sd_cd, 191 .d_devtounit = disklabel_dev_unit, 192 .d_flag = D_DISK | D_MPSAFE 193 }; 194 195 static const struct dkdriver sddkdriver = { 196 .d_open = sdopen, 197 .d_close = sdclose, 198 .d_strategy = sdstrategy, 199 .d_minphys = sdminphys, 200 .d_diskstart = sd_diskstart, 201 .d_dumpblocks = sd_dumpblocks, 202 .d_iosize = sd_iosize, 203 .d_firstopen = sd_firstopen, 204 .d_lastclose = sd_lastclose, 205 .d_label = sd_label, 206 .d_discard = sd_discard, 207 }; 208 209 static const struct scsipi_periphsw sd_switch = { 210 sd_interpret_sense, /* check our error handler first */ 211 sdstart, /* have a queue, served by this */ 212 NULL, /* have no async handler */ 213 sddone, /* deal with stats at interrupt time */ 214 }; 215 216 struct sd_mode_sense_data { 217 /* 218 * XXX 219 * We are not going to parse this as-is -- it just has to be large 220 * enough. 221 */ 222 union { 223 struct scsi_mode_parameter_header_6 small; 224 struct scsi_mode_parameter_header_10 big; 225 } header; 226 struct scsi_general_block_descriptor blk_desc; 227 union scsi_disk_pages pages; 228 }; 229 230 /* 231 * The routine called by the low level scsi routine when it discovers 232 * A device suitable for this driver 233 */ 234 static int 235 sdmatch(device_t parent, cfdata_t match, 236 void *aux) 237 { 238 struct scsipibus_attach_args *sa = aux; 239 int priority; 240 241 (void)scsipi_inqmatch(&sa->sa_inqbuf, 242 sd_patterns, sizeof(sd_patterns) / sizeof(sd_patterns[0]), 243 sizeof(sd_patterns[0]), &priority); 244 245 return (priority); 246 } 247 248 /* 249 * Attach routine common to atapi & scsi. 250 */ 251 static void 252 sdattach(device_t parent, device_t self, void *aux) 253 { 254 struct sd_softc *sd = device_private(self); 255 struct dk_softc *dksc = &sd->sc_dksc; 256 struct scsipibus_attach_args *sa = aux; 257 struct scsipi_periph *periph = sa->sa_periph; 258 int error, result, dtype; 259 struct disk_parms *dp = &sd->params; 260 char pbuf[9]; 261 262 SC_DEBUG(periph, SCSIPI_DB2, ("sdattach: ")); 263 264 sd->type = (sa->sa_inqbuf.type & SID_TYPE); 265 memcpy(sd->name, sa->sa_inqbuf.product, uimin(16, sizeof(sd->name))); 266 memcpy(sd->typename, sa->sa_inqbuf.product, 267 uimin(16, sizeof(sd->typename))); 268 269 if (sd->type == T_SIMPLE_DIRECT) { 270 periph->periph_quirks |= PQUIRK_ONLYBIG; 271 periph->periph_quirks |= PQUIRK_NOBIGMODESENSE; 272 } 273 274 switch (SCSIPI_BUSTYPE_TYPE(scsipi_periph_bustype(sa->sa_periph))) { 275 case SCSIPI_BUSTYPE_SCSI: 276 dtype = DKTYPE_SCSI; 277 if (periph->periph_version == 0) 278 sd->flags |= SDF_ANCIENT; 279 break; 280 case SCSIPI_BUSTYPE_ATAPI: 281 dtype = DKTYPE_ATAPI; 282 break; 283 default: 284 dtype = DKTYPE_UNKNOWN; 285 break; 286 } 287 288 /* Initialize dk and disk structure. */ 289 dk_init(dksc, self, dtype); 290 disk_init(&dksc->sc_dkdev, dksc->sc_xname, &sddkdriver); 291 292 /* Attach dk and disk subsystems */ 293 dk_attach(dksc); 294 disk_attach(&dksc->sc_dkdev); 295 296 bufq_alloc(&dksc->sc_bufq, BUFQ_DISK_DEFAULT_STRAT, 297 BUFQ_SORT_RAWBLOCK); 298 299 callout_init(&sd->sc_callout, 0); 300 301 /* 302 * Store information needed to contact our base driver 303 */ 304 sd->sc_periph = periph; 305 306 periph->periph_dev = dksc->sc_dev; 307 periph->periph_switch = &sd_switch; 308 309 /* 310 * Increase our openings to the maximum-per-periph 311 * supported by the adapter. This will either be 312 * clamped down or grown by the adapter if necessary. 313 */ 314 periph->periph_openings = 315 SCSIPI_CHAN_MAX_PERIPH(periph->periph_channel); 316 periph->periph_flags |= PERIPH_GROW_OPENINGS; 317 318 /* 319 * Use the subdriver to request information regarding the drive. 320 */ 321 aprint_naive("\n"); 322 aprint_normal("\n"); 323 324 if (periph->periph_quirks & PQUIRK_START) 325 (void)scsipi_start(periph, SSS_START, XS_CTL_SILENT); 326 327 error = scsipi_test_unit_ready(periph, 328 XS_CTL_DISCOVERY | XS_CTL_IGNORE_ILLEGAL_REQUEST | 329 XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_SILENT_NODEV); 330 if (error) 331 result = SDGP_RESULT_OFFLINE; 332 else 333 result = sd_get_parms(sd, &sd->params, XS_CTL_DISCOVERY); 334 335 aprint_normal_dev(dksc->sc_dev, ""); 336 switch (result) { 337 case SDGP_RESULT_OK: 338 format_bytes(pbuf, sizeof(pbuf), 339 (u_int64_t)dp->disksize * dp->blksize); 340 aprint_normal("%s, %ld cyl, %ld head, %ld sec, %ld bytes/sect" 341 " x %llu sectors", 342 pbuf, dp->cyls, dp->heads, dp->sectors, dp->blksize, 343 (unsigned long long)dp->disksize); 344 if (dp->lbppbe) { 345 aprint_normal(" (%lu bytes/physsect", dp->blksize << 346 dp->lbppbe); 347 if (dp->lalba) 348 aprint_normal("; first aligned sector %u", 349 dp->lalba); 350 aprint_normal(")"); 351 } 352 break; 353 354 case SDGP_RESULT_OFFLINE: 355 aprint_normal("drive offline"); 356 break; 357 358 case SDGP_RESULT_UNFORMATTED: 359 aprint_normal("unformatted media"); 360 break; 361 362 #ifdef DIAGNOSTIC 363 default: 364 panic("sdattach: unknown result from get_parms"); 365 break; 366 #endif 367 } 368 aprint_normal("\n"); 369 370 /* Discover wedges on this disk if it is online */ 371 if (result == SDGP_RESULT_OK) 372 dkwedge_discover(&dksc->sc_dkdev); 373 374 /* 375 * Establish a shutdown hook so that we can ensure that 376 * our data has actually made it onto the platter at 377 * shutdown time. Note that this relies on the fact 378 * that the shutdown hooks at the "leaves" of the device tree 379 * are run, first (thus guaranteeing that our hook runs before 380 * our ancestors'). 381 */ 382 if (!pmf_device_register1(self, sd_suspend, NULL, sd_shutdown)) 383 aprint_error_dev(self, "couldn't establish power handler\n"); 384 } 385 386 static int 387 sddetach(device_t self, int flags) 388 { 389 struct sd_softc *sd = device_private(self); 390 struct dk_softc *dksc = &sd->sc_dksc; 391 struct scsipi_periph *periph = sd->sc_periph; 392 struct scsipi_channel *chan = periph->periph_channel; 393 int bmaj, cmaj, i, mn, rc; 394 395 rc = disk_begindetach(&dksc->sc_dkdev, sd_lastclose, self, flags); 396 if (rc) 397 return rc; 398 399 /* locate the major number */ 400 bmaj = bdevsw_lookup_major(&sd_bdevsw); 401 cmaj = cdevsw_lookup_major(&sd_cdevsw); 402 403 /* Nuke the vnodes for any open instances */ 404 for (i = 0; i < MAXPARTITIONS; i++) { 405 mn = SDMINOR(device_unit(self), i); 406 vdevgone(bmaj, mn, mn, VBLK); 407 vdevgone(cmaj, mn, mn, VCHR); 408 } 409 410 /* kill any pending restart */ 411 callout_halt(&sd->sc_callout, NULL); 412 413 dk_drain(dksc); 414 415 /* Kill off any pending commands. */ 416 mutex_enter(chan_mtx(chan)); 417 scsipi_kill_pending(periph); 418 mutex_exit(chan_mtx(chan)); 419 420 bufq_free(dksc->sc_bufq); 421 422 /* Delete all of our wedges. */ 423 dkwedge_delall(&dksc->sc_dkdev); 424 425 /* Detach from the disk list. */ 426 disk_detach(&dksc->sc_dkdev); 427 disk_destroy(&dksc->sc_dkdev); 428 429 dk_detach(dksc); 430 431 callout_destroy(&sd->sc_callout); 432 433 pmf_device_deregister(self); 434 435 return (0); 436 } 437 438 /* 439 * Serialized by caller 440 */ 441 static int 442 sd_firstopen(device_t self, dev_t dev, int flag, int fmt) 443 { 444 struct sd_softc *sd = device_private(self); 445 struct scsipi_periph *periph = sd->sc_periph; 446 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; 447 int error, silent; 448 int part, removable; 449 450 part = SDPART(dev); 451 452 error = scsipi_adapter_addref(adapt); 453 if (error) 454 return error; 455 456 if ((part == RAW_PART && fmt == S_IFCHR) || (flag & FSILENT)) 457 silent = XS_CTL_SILENT; 458 else 459 silent = 0; 460 461 /* Check that it is still responding and ok. */ 462 error = scsipi_test_unit_ready(periph, 463 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE | 464 silent); 465 466 /* 467 * Start the pack spinning if necessary. Always allow the 468 * raw partition to be opened, for raw IOCTLs. Data transfers 469 * will check for SDEV_MEDIA_LOADED. 470 */ 471 if (error == EIO) { 472 error = scsipi_start(periph, SSS_START, silent); 473 if (error == EINVAL) 474 error = EIO; 475 } 476 if (error) 477 goto bad; 478 479 removable = (periph->periph_flags & PERIPH_REMOVABLE) != 0; 480 if (removable) { 481 /* Lock the pack in. */ 482 error = scsipi_prevent(periph, SPAMR_PREVENT_DT, 483 XS_CTL_IGNORE_ILLEGAL_REQUEST | 484 XS_CTL_IGNORE_MEDIA_CHANGE | 485 XS_CTL_SILENT); 486 if (error) 487 goto bad; 488 } 489 490 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 491 int param_error; 492 493 /* 494 * Load the physical device parameters. 495 * 496 * Note that if media is present but unformatted, 497 * we allow the open (so that it can be formatted!). 498 * The drive should refuse real I/O, if the media is 499 * unformatted. 500 */ 501 param_error = sd_get_parms(sd, &sd->params, 0); 502 if (param_error == SDGP_RESULT_OFFLINE) { 503 error = ENXIO; 504 goto bad2; 505 } 506 periph->periph_flags |= PERIPH_MEDIA_LOADED; 507 508 SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded ")); 509 } 510 511 periph->periph_flags |= PERIPH_OPEN; 512 return 0; 513 514 bad2: 515 if (removable) 516 scsipi_prevent(periph, SPAMR_ALLOW, 517 XS_CTL_IGNORE_ILLEGAL_REQUEST | 518 XS_CTL_IGNORE_MEDIA_CHANGE | 519 XS_CTL_SILENT); 520 521 bad: 522 scsipi_adapter_delref(adapt); 523 return error; 524 } 525 526 /* 527 * open the device. Make sure the partition info is a up-to-date as can be. 528 */ 529 static int 530 sdopen(dev_t dev, int flag, int fmt, struct lwp *l) 531 { 532 struct sd_softc *sd; 533 struct dk_softc *dksc; 534 struct scsipi_periph *periph; 535 int unit, part; 536 int error; 537 538 unit = SDUNIT(dev); 539 sd = device_lookup_private(&sd_cd, unit); 540 if (sd == NULL) 541 return (ENXIO); 542 dksc = &sd->sc_dksc; 543 544 if (!device_is_active(dksc->sc_dev)) 545 return (ENODEV); 546 547 periph = sd->sc_periph; 548 part = SDPART(dev); 549 550 SC_DEBUG(periph, SCSIPI_DB1, 551 ("sdopen: dev=0x%"PRIx64" (unit %d (of %d), partition %d)\n", 552 dev, unit, sd_cd.cd_ndevs, SDPART(dev))); 553 554 /* 555 * If any partition is open, but the disk has been invalidated, 556 * disallow further opens of non-raw partition 557 */ 558 if ((periph->periph_flags & (PERIPH_OPEN | PERIPH_MEDIA_LOADED)) == 559 PERIPH_OPEN) { 560 if (part != RAW_PART || fmt != S_IFCHR) 561 return EIO; 562 } 563 564 error = dk_open(dksc, dev, flag, fmt, l); 565 566 SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n")); 567 568 return error; 569 } 570 571 /* 572 * Serialized by caller 573 */ 574 static int 575 sd_lastclose(device_t self) 576 { 577 struct sd_softc *sd = device_private(self); 578 struct dk_softc *dksc = &sd->sc_dksc; 579 struct scsipi_periph *periph = sd->sc_periph; 580 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; 581 582 /* 583 * If the disk cache needs flushing, and the disk supports 584 * it, do it now. 585 */ 586 if ((sd->flags & SDF_DIRTY) != 0) { 587 if (sd_flush(sd, 0)) { 588 aprint_error_dev(dksc->sc_dev, 589 "cache synchronization failed\n"); 590 sd->flags &= ~SDF_FLUSHING; 591 } else 592 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 593 } 594 595 scsipi_wait_drain(periph); 596 597 if (periph->periph_flags & PERIPH_REMOVABLE) 598 scsipi_prevent(periph, SPAMR_ALLOW, 599 XS_CTL_IGNORE_ILLEGAL_REQUEST | 600 XS_CTL_IGNORE_NOT_READY | 601 XS_CTL_SILENT); 602 periph->periph_flags &= ~PERIPH_OPEN; 603 604 scsipi_wait_drain(periph); 605 606 scsipi_adapter_delref(adapt); 607 608 return 0; 609 } 610 611 /* 612 * close the device.. only called if we are the LAST occurrence of an open 613 * device. Convenient now but usually a pain. 614 */ 615 static int 616 sdclose(dev_t dev, int flag, int fmt, struct lwp *l) 617 { 618 struct sd_softc *sd; 619 struct dk_softc *dksc; 620 int unit; 621 622 unit = SDUNIT(dev); 623 sd = device_lookup_private(&sd_cd, unit); 624 dksc = &sd->sc_dksc; 625 626 return dk_close(dksc, dev, flag, fmt, l); 627 } 628 629 /* 630 * Actually translate the requested transfer into one the physical driver 631 * can understand. The transfer is described by a buf and will include 632 * only one physical transfer. 633 */ 634 static void 635 sdstrategy(struct buf *bp) 636 { 637 struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(bp->b_dev)); 638 struct dk_softc *dksc = &sd->sc_dksc; 639 struct scsipi_periph *periph = sd->sc_periph; 640 641 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdstrategy ")); 642 SC_DEBUG(sd->sc_periph, SCSIPI_DB1, 643 ("%d bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno)); 644 645 /* 646 * If the device has been made invalid, error out 647 */ 648 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 || 649 !device_is_active(dksc->sc_dev)) { 650 if (periph->periph_flags & PERIPH_OPEN) 651 bp->b_error = EIO; 652 else 653 bp->b_error = ENODEV; 654 655 bp->b_resid = bp->b_bcount; 656 biodone(bp); 657 return; 658 } 659 660 dk_strategy(dksc, bp); 661 } 662 663 /* 664 * Issue single I/O command 665 * 666 * Called from dk_start and implicitly from dk_strategy 667 */ 668 static int 669 sd_diskstart(device_t dev, struct buf *bp) 670 { 671 struct sd_softc *sd = device_private(dev); 672 struct scsipi_periph *periph = sd->sc_periph; 673 struct scsipi_channel *chan = periph->periph_channel; 674 struct scsipi_rw_16 cmd16; 675 struct scsipi_rw_10 cmd_big; 676 struct scsi_rw_6 cmd_small; 677 struct scsipi_generic *cmdp; 678 struct scsipi_xfer *xs; 679 int error, flags, nblks, cmdlen; 680 int cdb_flags; 681 bool havefua = !(periph->periph_quirks & PQUIRK_NOFUA); 682 683 mutex_enter(chan_mtx(chan)); 684 685 if (periph->periph_active >= periph->periph_openings) { 686 error = EAGAIN; 687 goto out; 688 } 689 690 /* 691 * there is excess capacity, but a special waits 692 * It'll need the adapter as soon as we clear out of the 693 * way and let it run (user level wait). 694 */ 695 if (periph->periph_flags & PERIPH_WAITING) { 696 periph->periph_flags &= ~PERIPH_WAITING; 697 cv_broadcast(periph_cv_periph(periph)); 698 error = EAGAIN; 699 goto out; 700 } 701 702 /* 703 * If the device has become invalid, abort all the 704 * reads and writes until all files have been closed and 705 * re-opened. 706 */ 707 if (__predict_false( 708 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)) { 709 error = EIO; 710 goto out; 711 } 712 713 /* 714 * Mark the disk dirty so that the cache will be 715 * flushed on close. 716 */ 717 if ((bp->b_flags & B_READ) == 0) 718 sd->flags |= SDF_DIRTY; 719 720 if (sd->params.blksize == DEV_BSIZE) 721 nblks = bp->b_bcount >> DEV_BSHIFT; 722 else 723 nblks = howmany(bp->b_bcount, sd->params.blksize); 724 725 /* 726 * Pass FUA and/or DPO if requested. Must be done before CDB 727 * selection, as 6-byte CDB doesn't support the flags. 728 */ 729 cdb_flags = 0; 730 if (havefua) { 731 if (bp->b_flags & B_MEDIA_FUA) 732 cdb_flags |= SRWB_FUA; 733 734 if (bp->b_flags & B_MEDIA_DPO) 735 cdb_flags |= SRWB_DPO; 736 } 737 738 /* 739 * Fill out the scsi command. Use the smallest CDB possible 740 * (6-byte, 10-byte, or 16-byte). If we need FUA or DPO, 741 * need to use 10-byte or bigger, as the 6-byte doesn't support 742 * the flags. 743 */ 744 if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) && 745 ((nblks & 0xff) == nblks) && 746 !(periph->periph_quirks & PQUIRK_ONLYBIG) && 747 !cdb_flags) { 748 /* 6-byte CDB */ 749 memset(&cmd_small, 0, sizeof(cmd_small)); 750 cmd_small.opcode = (bp->b_flags & B_READ) ? 751 SCSI_READ_6_COMMAND : SCSI_WRITE_6_COMMAND; 752 _lto3b(bp->b_rawblkno, cmd_small.addr); 753 cmd_small.length = nblks & 0xff; 754 cmdlen = sizeof(cmd_small); 755 cmdp = (struct scsipi_generic *)&cmd_small; 756 } else if ((bp->b_rawblkno & 0xffffffff) == bp->b_rawblkno) { 757 /* 10-byte CDB */ 758 memset(&cmd_big, 0, sizeof(cmd_big)); 759 cmd_big.opcode = (bp->b_flags & B_READ) ? 760 READ_10 : WRITE_10; 761 _lto4b(bp->b_rawblkno, cmd_big.addr); 762 _lto2b(nblks, cmd_big.length); 763 cmdlen = sizeof(cmd_big); 764 cmdp = (struct scsipi_generic *)&cmd_big; 765 } else { 766 /* 16-byte CDB */ 767 memset(&cmd16, 0, sizeof(cmd16)); 768 cmd16.opcode = (bp->b_flags & B_READ) ? 769 READ_16 : WRITE_16; 770 _lto8b(bp->b_rawblkno, cmd16.addr); 771 _lto4b(nblks, cmd16.length); 772 cmdlen = sizeof(cmd16); 773 cmdp = (struct scsipi_generic *)&cmd16; 774 } 775 776 if (cdb_flags) 777 cmdp->bytes[0] = cdb_flags; 778 779 /* 780 * Figure out what flags to use. 781 */ 782 flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_SIMPLE_TAG; 783 if (bp->b_flags & B_READ) 784 flags |= XS_CTL_DATA_IN; 785 else 786 flags |= XS_CTL_DATA_OUT; 787 788 /* 789 * Call the routine that chats with the adapter. 790 * Note: we cannot sleep as we may be an interrupt 791 */ 792 xs = scsipi_make_xs_locked(periph, cmdp, cmdlen, 793 (u_char *)bp->b_data, bp->b_bcount, 794 SDRETRIES, SD_IO_TIMEOUT, bp, flags); 795 if (__predict_false(xs == NULL)) { 796 /* 797 * out of memory. Keep this buffer in the queue, and 798 * retry later. 799 */ 800 callout_reset(&sd->sc_callout, hz / 2, sdrestart, sd); 801 error = EAGAIN; 802 goto out; 803 } 804 805 error = scsipi_execute_xs(xs); 806 /* with a scsipi_xfer preallocated, scsipi_command can't fail */ 807 KASSERT(error == 0); 808 809 out: 810 mutex_exit(chan_mtx(chan)); 811 812 return error; 813 } 814 815 /* 816 * Recover I/O request after memory shortage 817 * 818 * Called from callout 819 */ 820 static void 821 sdrestart(void *v) 822 { 823 struct sd_softc *sd = v; 824 struct dk_softc *dksc = &sd->sc_dksc; 825 826 dk_start(dksc, NULL); 827 } 828 829 /* 830 * Recover I/O request after memory shortage 831 * 832 * Called from scsipi midlayer when resources have been freed 833 * with channel lock held 834 */ 835 static void 836 sdstart(struct scsipi_periph *periph) 837 { 838 struct sd_softc *sd = device_private(periph->periph_dev); 839 struct dk_softc *dksc = &sd->sc_dksc; 840 struct scsipi_channel *chan = periph->periph_channel; 841 842 /* 843 * release channel lock as dk_start may need to acquire 844 * other locks 845 * 846 * sdstart is called from scsipi_put_xs and all its callers 847 * release the lock afterwards. So releasing it here 848 * doesn't matter. 849 */ 850 mutex_exit(chan_mtx(chan)); 851 852 dk_start(dksc, NULL); 853 854 mutex_enter(chan_mtx(chan)); 855 } 856 857 static void 858 sddone(struct scsipi_xfer *xs, int error) 859 { 860 struct sd_softc *sd = device_private(xs->xs_periph->periph_dev); 861 struct dk_softc *dksc = &sd->sc_dksc; 862 struct buf *bp = xs->bp; 863 864 if (sd->flags & SDF_FLUSHING) { 865 /* Flush completed, no longer dirty. */ 866 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 867 } 868 869 if (bp) { 870 bp->b_error = error; 871 bp->b_resid = xs->resid; 872 if (error) { 873 /* on a read/write error bp->b_resid is zero, so fix */ 874 bp->b_resid = bp->b_bcount; 875 } 876 877 dk_done(dksc, bp); 878 /* dk_start is called from scsipi_complete */ 879 } 880 } 881 882 static void 883 sdminphys(struct buf *bp) 884 { 885 struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(bp->b_dev)); 886 struct dk_softc *dksc = &sd->sc_dksc; 887 long xmax; 888 889 /* 890 * If the device is ancient, we want to make sure that 891 * the transfer fits into a 6-byte cdb. 892 * 893 * XXX Note that the SCSI-I spec says that 256-block transfers 894 * are allowed in a 6-byte read/write, and are specified 895 * by setting the "length" to 0. However, we're conservative 896 * here, allowing only 255-block transfers in case an 897 * ancient device gets confused by length == 0. A length of 0 898 * in a 10-byte read/write actually means 0 blocks. 899 */ 900 if ((sd->flags & SDF_ANCIENT) && 901 ((sd->sc_periph->periph_flags & 902 (PERIPH_REMOVABLE | PERIPH_MEDIA_LOADED)) != PERIPH_REMOVABLE)) { 903 xmax = dksc->sc_dkdev.dk_geom.dg_secsize * 0xff; 904 905 if (bp->b_bcount > xmax) 906 bp->b_bcount = xmax; 907 } 908 909 scsipi_adapter_minphys(sd->sc_periph->periph_channel, bp); 910 } 911 912 static void 913 sd_iosize(device_t dev, int *count) 914 { 915 struct buf B; 916 int bmaj; 917 918 bmaj = bdevsw_lookup_major(&sd_bdevsw); 919 B.b_dev = MAKESDDEV(bmaj,device_unit(dev),RAW_PART); 920 B.b_bcount = *count; 921 922 sdminphys(&B); 923 924 *count = B.b_bcount; 925 } 926 927 static int 928 sdread(dev_t dev, struct uio *uio, int ioflag) 929 { 930 931 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio)); 932 } 933 934 static int 935 sdwrite(dev_t dev, struct uio *uio, int ioflag) 936 { 937 938 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio)); 939 } 940 941 /* 942 * Perform special action on behalf of the user 943 * Knows about the internals of this device 944 */ 945 static int 946 sdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 947 { 948 struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(dev)); 949 struct dk_softc *dksc = &sd->sc_dksc; 950 struct scsipi_periph *periph = sd->sc_periph; 951 952 int part = SDPART(dev); 953 int error; 954 955 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdioctl 0x%lx ", cmd)); 956 957 /* 958 * If the device is not valid, some IOCTLs can still be 959 * handled on the raw partition. Check this here. 960 */ 961 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 && 962 part != RAW_PART) 963 return (EIO); 964 965 switch (cmd) { 966 case DIOCLOCK: 967 if (periph->periph_flags & PERIPH_REMOVABLE) 968 return (scsipi_prevent(periph, 969 (*(int *)addr) ? 970 SPAMR_PREVENT_DT : SPAMR_ALLOW, 0)); 971 else 972 return (ENOTTY); 973 974 case DIOCEJECT: 975 if ((periph->periph_flags & PERIPH_REMOVABLE) == 0) 976 return (ENOTTY); 977 if (*(int *)addr == 0) { 978 int pmask = __BIT(part); 979 /* 980 * Don't force eject: check that we are the only 981 * partition open. If so, unlock it. 982 */ 983 if (DK_BUSY(dksc, pmask) == 0) { 984 error = scsipi_prevent(periph, SPAMR_ALLOW, 985 XS_CTL_IGNORE_NOT_READY); 986 if (error) 987 return (error); 988 } else { 989 return (EBUSY); 990 } 991 } 992 /* FALLTHROUGH */ 993 case ODIOCEJECT: 994 return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ? 995 ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0)); 996 997 case DIOCGCACHE: 998 return (sd_getcache(sd, (int *) addr)); 999 1000 case DIOCSCACHE: 1001 if ((flag & FWRITE) == 0) 1002 return (EBADF); 1003 return (sd_setcache(sd, *(int *) addr)); 1004 1005 case DIOCCACHESYNC: 1006 /* 1007 * XXX Do we really need to care about having a writable 1008 * file descriptor here? 1009 */ 1010 if ((flag & FWRITE) == 0) 1011 return (EBADF); 1012 if (((sd->flags & SDF_DIRTY) != 0 || *(int *)addr != 0)) { 1013 error = sd_flush(sd, 0); 1014 if (error) { 1015 sd->flags &= ~SDF_FLUSHING; 1016 return (error); 1017 } 1018 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 1019 } 1020 return (0); 1021 1022 default: 1023 error = dk_ioctl(dksc, dev, cmd, addr, flag, l); 1024 if (error == ENOTTY) { 1025 error = scsipi_do_ioctl(periph, dev, cmd, addr, flag, 1026 l); 1027 } 1028 return (error); 1029 } 1030 1031 #ifdef DIAGNOSTIC 1032 panic("sdioctl: impossible"); 1033 #endif 1034 } 1035 1036 static void 1037 sd_label(device_t self, struct disklabel *lp) 1038 { 1039 struct sd_softc *sd = device_private(self); 1040 1041 strncpy(lp->d_typename, sd->name, 16); 1042 lp->d_rpm = sd->params.rot_rate; 1043 if (sd->sc_periph->periph_flags & PERIPH_REMOVABLE) 1044 lp->d_flags |= D_REMOVABLE; 1045 } 1046 1047 static int 1048 sd_unmap(struct sd_softc *sd, off_t pos, off_t len) 1049 { 1050 struct scsi_unmap_10 cmd; 1051 struct scsi_unmap_10_data data; 1052 int flags = 0; 1053 uint64_t bno; 1054 uint32_t size; 1055 1056 /* round the start up and the end down */ 1057 bno = (pos + sd->params.blksize - 1) / sd->params.blksize; 1058 size = ((pos + len) / sd->params.blksize) - bno; 1059 1060 if (size == 0) 1061 return 0; 1062 1063 memset(&data, 0, sizeof(data)); 1064 _lto2b(sizeof(data) - 2, data.unmap_data_length); 1065 _lto2b(sizeof(data) - 8, data.unmap_block_descriptor_data_length); 1066 _lto8b(bno, data.unmap_block_descriptor[0].addr); 1067 _lto4b(size, data.unmap_block_descriptor[0].len); 1068 1069 memset(&cmd, 0, sizeof(cmd)); 1070 cmd.opcode = UNMAP_10; 1071 cmd.byte2 = 0; 1072 _lto2b(sizeof(data), cmd.length); 1073 1074 scsipi_command(sd->sc_periph, 1075 (void *)&cmd, sizeof(cmd), (void *)&data, sizeof(data), 1076 SDRETRIES, 2000000, NULL, 1077 flags | XS_CTL_DATA_OUT); 1078 1079 return 0; 1080 } 1081 1082 static int 1083 sd_discard(device_t self, off_t pos, off_t len) 1084 { 1085 struct sd_softc *sd = device_private(self); 1086 if (sd->flags & SDF_LBPU) { 1087 return sd_unmap(sd, pos, len); 1088 } 1089 return ENODEV; 1090 } 1091 1092 static bool 1093 sd_shutdown(device_t self, int how) 1094 { 1095 struct sd_softc *sd = device_private(self); 1096 struct dk_softc *dksc = &sd->sc_dksc; 1097 1098 /* 1099 * If the disk cache needs to be flushed, and the disk supports 1100 * it, flush it. We're cold at this point, so we poll for 1101 * completion. 1102 */ 1103 if ((sd->flags & SDF_DIRTY) != 0) { 1104 if (sd_flush(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) { 1105 aprint_error_dev(dksc->sc_dev, 1106 "cache synchronization failed\n"); 1107 sd->flags &= ~SDF_FLUSHING; 1108 } else 1109 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 1110 } 1111 return true; 1112 } 1113 1114 static bool 1115 sd_suspend(device_t dv, const pmf_qual_t *qual) 1116 { 1117 return sd_shutdown(dv, boothowto); /* XXX no need to poll */ 1118 } 1119 1120 /* 1121 * Check Errors 1122 */ 1123 static int 1124 sd_interpret_sense(struct scsipi_xfer *xs) 1125 { 1126 struct scsipi_periph *periph = xs->xs_periph; 1127 struct scsipi_channel *chan = periph->periph_channel; 1128 struct scsi_sense_data *sense = &xs->sense.scsi_sense; 1129 struct sd_softc *sd = device_private(periph->periph_dev); 1130 struct dk_softc *dksc = &sd->sc_dksc; 1131 int error, retval = EJUSTRETURN; 1132 1133 /* 1134 * If the periph is already recovering, just do the normal 1135 * error processing. 1136 */ 1137 if (periph->periph_flags & PERIPH_RECOVERING) 1138 return (retval); 1139 1140 /* 1141 * Ignore errors from accessing illegal fields (e.g. trying to 1142 * lock the door of a digicam, which doesn't have a door that 1143 * can be locked) for the SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL command. 1144 */ 1145 if (xs->cmd->opcode == SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL && 1146 SSD_SENSE_KEY(sense->flags) == SKEY_ILLEGAL_REQUEST && 1147 sense->asc == 0x24 && 1148 sense->ascq == 0x00) { /* Illegal field in CDB */ 1149 if (!(xs->xs_control & XS_CTL_SILENT)) { 1150 scsipi_printaddr(periph); 1151 printf("no door lock\n"); 1152 } 1153 xs->xs_control |= XS_CTL_IGNORE_ILLEGAL_REQUEST; 1154 return (retval); 1155 } 1156 1157 /* 1158 * If it isn't a extended or extended/deferred error, let 1159 * the generic code handle it. 1160 */ 1161 if (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT && 1162 SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED) 1163 return (retval); 1164 1165 if (SSD_SENSE_KEY(sense->flags) == SKEY_NOT_READY && 1166 sense->asc == 0x4) { 1167 if (sense->ascq == 0x01) { 1168 /* 1169 * Unit In The Process Of Becoming Ready. 1170 */ 1171 printf("%s: waiting for pack to spin up...\n", 1172 dksc->sc_xname); 1173 if (!callout_pending(&periph->periph_callout)) 1174 scsipi_periph_freeze(periph, 1); 1175 callout_reset(&periph->periph_callout, 1176 5 * hz, scsipi_periph_timed_thaw, periph); 1177 retval = ERESTART; 1178 } else if (sense->ascq == 0x02) { 1179 printf("%s: pack is stopped, restarting...\n", 1180 dksc->sc_xname); 1181 mutex_enter(chan_mtx(chan)); 1182 periph->periph_flags |= PERIPH_RECOVERING; 1183 mutex_exit(chan_mtx(chan)); 1184 error = scsipi_start(periph, SSS_START, 1185 XS_CTL_URGENT|XS_CTL_HEAD_TAG| 1186 XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH); 1187 if (error) { 1188 aprint_error_dev(dksc->sc_dev, 1189 "unable to restart pack\n"); 1190 retval = error; 1191 } else 1192 retval = ERESTART; 1193 mutex_enter(chan_mtx(chan)); 1194 periph->periph_flags &= ~PERIPH_RECOVERING; 1195 mutex_exit(chan_mtx(chan)); 1196 } 1197 } 1198 if (SSD_SENSE_KEY(sense->flags) == SKEY_MEDIUM_ERROR && 1199 sense->asc == 0x31 && 1200 sense->ascq == 0x00) { /* maybe for any asq ? */ 1201 /* Medium Format Corrupted */ 1202 retval = EFTYPE; 1203 } 1204 return (retval); 1205 } 1206 1207 1208 static int 1209 sdsize(dev_t dev) 1210 { 1211 struct sd_softc *sd; 1212 struct dk_softc *dksc; 1213 int unit; 1214 1215 unit = SDUNIT(dev); 1216 sd = device_lookup_private(&sd_cd, unit); 1217 if (sd == NULL) 1218 return (-1); 1219 dksc = &sd->sc_dksc; 1220 1221 if (!device_is_active(dksc->sc_dev)) 1222 return (-1); 1223 1224 return dk_size(dksc, dev); 1225 } 1226 1227 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */ 1228 static struct scsipi_xfer sx; 1229 1230 /* 1231 * dump all of physical memory into the partition specified, starting 1232 * at offset 'dumplo' into the partition. 1233 */ 1234 static int 1235 sddump(dev_t dev, daddr_t blkno, void *va, size_t size) 1236 { 1237 struct sd_softc *sd; 1238 struct dk_softc *dksc; 1239 struct scsipi_periph *periph; 1240 int unit; 1241 1242 unit = SDUNIT(dev); 1243 if ((sd = device_lookup_private(&sd_cd, unit)) == NULL) 1244 return (ENXIO); 1245 dksc = &sd->sc_dksc; 1246 1247 if (!device_is_active(dksc->sc_dev)) 1248 return (ENODEV); 1249 1250 periph = sd->sc_periph; 1251 1252 /* Make sure it was initialized. */ 1253 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) 1254 return (ENXIO); 1255 1256 return dk_dump(dksc, dev, blkno, va, size, 0); 1257 } 1258 1259 static int 1260 sd_dumpblocks(device_t dev, void *va, daddr_t blkno, int nblk) 1261 { 1262 struct sd_softc *sd = device_private(dev); 1263 struct dk_softc *dksc = &sd->sc_dksc; 1264 struct disk_geom *dg = &dksc->sc_dkdev.dk_geom; 1265 struct scsipi_rw_10 cmd; /* write command */ 1266 struct scsipi_rw_16 cmd16; /* write command */ 1267 struct scsipi_generic *cmdp; 1268 struct scsipi_xfer *xs; /* ... convenience */ 1269 struct scsipi_periph *periph; 1270 struct scsipi_channel *chan; 1271 size_t sectorsize; 1272 int cmdlen; 1273 1274 periph = sd->sc_periph; 1275 chan = periph->periph_channel; 1276 1277 sectorsize = dg->dg_secsize; 1278 1279 xs = &sx; 1280 1281 #ifndef SD_DUMP_NOT_TRUSTED 1282 /* 1283 * Fill out the scsi command 1284 */ 1285 if ((blkno & 0xffffffff) == blkno) { 1286 memset(&cmd, 0, sizeof(cmd)); 1287 cmd.opcode = WRITE_10; 1288 _lto4b(blkno, cmd.addr); 1289 _lto2b(nblk, cmd.length); 1290 cmdlen = sizeof(cmd); 1291 cmdp = (struct scsipi_generic *)&cmd; 1292 } else { 1293 memset(&cmd16, 0, sizeof(cmd16)); 1294 cmd16.opcode = WRITE_16; 1295 _lto8b(blkno, cmd16.addr); 1296 _lto4b(nblk, cmd16.length); 1297 cmdlen = sizeof(cmd16); 1298 cmdp = (struct scsipi_generic *)&cmd16; 1299 } 1300 1301 /* 1302 * Fill out the scsipi_xfer structure 1303 * Note: we cannot sleep as we may be an interrupt 1304 * don't use scsipi_command() as it may want to wait 1305 * for an xs. 1306 */ 1307 memset(xs, 0, sizeof(sx)); 1308 xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL | 1309 XS_CTL_DATA_OUT; 1310 xs->xs_status = 0; 1311 xs->xs_periph = periph; 1312 xs->xs_retries = SDRETRIES; 1313 xs->timeout = 10000; /* 10000 millisecs for a disk ! */ 1314 xs->cmd = cmdp; 1315 xs->cmdlen = cmdlen; 1316 xs->resid = nblk * sectorsize; 1317 xs->error = XS_NOERROR; 1318 xs->bp = 0; 1319 xs->data = va; 1320 xs->datalen = nblk * sectorsize; 1321 callout_init(&xs->xs_callout, 0); 1322 1323 /* 1324 * Pass all this info to the scsi driver. 1325 */ 1326 scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs); 1327 if ((xs->xs_status & XS_STS_DONE) == 0 || 1328 xs->error != XS_NOERROR) 1329 return (EIO); 1330 #else /* SD_DUMP_NOT_TRUSTED */ 1331 /* Let's just talk about this first... */ 1332 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno); 1333 delay(500 * 1000); /* half a second */ 1334 #endif /* SD_DUMP_NOT_TRUSTED */ 1335 1336 return (0); 1337 } 1338 1339 static int 1340 sd_mode_sense(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size, 1341 int page, int flags, int *big) 1342 { 1343 1344 if ((sd->sc_periph->periph_quirks & PQUIRK_ONLYBIG) && 1345 !(sd->sc_periph->periph_quirks & PQUIRK_NOBIGMODESENSE)) { 1346 *big = 1; 1347 return scsipi_mode_sense_big(sd->sc_periph, byte2, page, sense, 1348 size + sizeof(struct scsi_mode_parameter_header_10), 1349 flags, SDRETRIES, 6000); 1350 } else { 1351 *big = 0; 1352 return scsipi_mode_sense(sd->sc_periph, byte2, page, sense, 1353 size + sizeof(struct scsi_mode_parameter_header_6), 1354 flags, SDRETRIES, 6000); 1355 } 1356 } 1357 1358 static int 1359 sd_mode_select(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size, 1360 int flags, int big) 1361 { 1362 1363 if (big) { 1364 struct scsi_mode_parameter_header_10 *header = sense; 1365 1366 _lto2b(0, header->data_length); 1367 return scsipi_mode_select_big(sd->sc_periph, byte2, sense, 1368 size + sizeof(struct scsi_mode_parameter_header_10), 1369 flags, SDRETRIES, 6000); 1370 } else { 1371 struct scsi_mode_parameter_header_6 *header = sense; 1372 1373 header->data_length = 0; 1374 return scsipi_mode_select(sd->sc_periph, byte2, sense, 1375 size + sizeof(struct scsi_mode_parameter_header_6), 1376 flags, SDRETRIES, 6000); 1377 } 1378 } 1379 1380 /* 1381 * sd_validate_blksize: 1382 * 1383 * Validate the block size. Print error if periph is specified, 1384 */ 1385 static int 1386 sd_validate_blksize(struct scsipi_periph *periph, int len) 1387 { 1388 1389 if (len >= 256 && powerof2(len) && len <= MAXPHYS) { 1390 return 1; 1391 } 1392 1393 if (periph) { 1394 scsipi_printaddr(periph); 1395 printf("%s sector size: 0x%x. Defaulting to %d bytes.\n", 1396 !powerof2(len) ? 1397 "preposterous" : "unsupported", 1398 len, SD_DEFAULT_BLKSIZE); 1399 } 1400 1401 return 0; 1402 } 1403 1404 /* 1405 * sd_read_capacity: 1406 * 1407 * Find out from the device what its capacity is. 1408 */ 1409 static u_int64_t 1410 sd_read_capacity(struct sd_softc *sd, int *blksize, int flags) 1411 { 1412 struct scsipi_periph *periph = sd->sc_periph; 1413 union { 1414 struct scsipi_read_capacity_10 cmd; 1415 struct scsipi_read_capacity_16 cmd16; 1416 } cmd; 1417 union { 1418 struct scsipi_read_capacity_10_data data; 1419 struct scsipi_read_capacity_16_data data16; 1420 } *datap; 1421 uint64_t rv; 1422 1423 sd->params.lbppbe = 0; 1424 sd->params.lalba = 0; 1425 1426 memset(&cmd, 0, sizeof(cmd)); 1427 cmd.cmd.opcode = READ_CAPACITY_10; 1428 1429 /* 1430 * Don't allocate data buffer on stack; 1431 * The lower driver layer might use the same stack and 1432 * if it uses region which is in the same cacheline, 1433 * cache flush ops against the data buffer won't work properly. 1434 */ 1435 datap = malloc(sizeof(*datap), M_TEMP, M_WAITOK); 1436 if (datap == NULL) 1437 return 0; 1438 1439 if (periph->periph_version >= 5) /* SPC-3 */ 1440 goto rc16; 1441 1442 /* 1443 * If the command works, interpret the result as a 4 byte 1444 * number of blocks 1445 */ 1446 rv = 0; 1447 memset(datap, 0, sizeof(datap->data)); 1448 if (scsipi_command(periph, (void *)&cmd.cmd, sizeof(cmd.cmd), 1449 (void *)datap, sizeof(datap->data), SCSIPIRETRIES, 20000, NULL, 1450 flags | XS_CTL_DATA_IN | XS_CTL_SILENT) != 0) 1451 goto out; 1452 1453 if (_4btol(datap->data.addr) != 0xffffffff) { 1454 *blksize = _4btol(datap->data.length); 1455 rv = _4btol(datap->data.addr) + 1; 1456 goto out; 1457 } 1458 1459 /* 1460 * Device is larger than can be reflected by READ CAPACITY (10). 1461 * Try READ CAPACITY (16). 1462 */ 1463 1464 rc16: 1465 memset(&cmd, 0, sizeof(cmd)); 1466 cmd.cmd16.opcode = READ_CAPACITY_16; 1467 cmd.cmd16.byte2 = SRC16_SERVICE_ACTION; 1468 _lto4b(sizeof(datap->data16), cmd.cmd16.len); 1469 1470 memset(datap, 0, sizeof(datap->data16)); 1471 if (scsipi_command(periph, (void *)&cmd.cmd16, sizeof(cmd.cmd16), 1472 (void *)datap, sizeof(datap->data16), SCSIPIRETRIES, 20000, NULL, 1473 flags | XS_CTL_DATA_IN | XS_CTL_SILENT) != 0) 1474 goto out; 1475 1476 *blksize = _4btol(datap->data16.length); 1477 rv = _8btol(datap->data16.addr) + 1; 1478 sd->params.lbppbe = datap->data16.byte14 & SRC16D_LBPPB_EXPONENT; 1479 sd->params.lalba = _2btol(datap->data16.lowest_aligned) & SRC16D_LALBA; 1480 if (_2btol(datap->data16.lowest_aligned) & SRC16D_LBPME) { 1481 sd->flags |= SDF_LBPME; 1482 } else { 1483 sd->flags &= ~SDF_LBPME; 1484 } 1485 1486 out: 1487 free(datap, M_TEMP); 1488 return rv; 1489 } 1490 1491 static int 1492 sd_get_simplifiedparms(struct sd_softc *sd, struct disk_parms *dp, int flags) 1493 { 1494 struct { 1495 struct scsi_mode_parameter_header_6 header; 1496 /* no block descriptor */ 1497 u_int8_t pg_code; /* page code (should be 6) */ 1498 u_int8_t pg_length; /* page length (should be 11) */ 1499 u_int8_t wcd; /* bit0: cache disable */ 1500 u_int8_t lbs[2]; /* logical block size */ 1501 u_int8_t size[5]; /* number of log. blocks */ 1502 u_int8_t pp; /* power/performance */ 1503 u_int8_t flags; 1504 u_int8_t resvd; 1505 } scsipi_sense; 1506 u_int64_t blocks; 1507 int error, blksize; 1508 1509 /* 1510 * sd_read_capacity (ie "read capacity") and mode sense page 6 1511 * give the same information. Do both for now, and check 1512 * for consistency. 1513 * XXX probably differs for removable media 1514 */ 1515 dp->blksize = SD_DEFAULT_BLKSIZE; 1516 if ((blocks = sd_read_capacity(sd, &blksize, flags)) == 0) 1517 return (SDGP_RESULT_OFFLINE); /* XXX? */ 1518 1519 error = scsipi_mode_sense(sd->sc_periph, SMS_DBD, 6, 1520 &scsipi_sense.header, sizeof(scsipi_sense), 1521 flags, SDRETRIES, 6000); 1522 1523 if (error != 0) 1524 return (SDGP_RESULT_OFFLINE); /* XXX? */ 1525 1526 dp->blksize = blksize; 1527 if (!sd_validate_blksize(NULL, dp->blksize)) 1528 dp->blksize = _2btol(scsipi_sense.lbs); 1529 if (!sd_validate_blksize(sd->sc_periph, dp->blksize)) 1530 dp->blksize = SD_DEFAULT_BLKSIZE; 1531 1532 /* 1533 * Create a pseudo-geometry. 1534 */ 1535 dp->heads = 64; 1536 dp->sectors = 32; 1537 dp->cyls = blocks / (dp->heads * dp->sectors); 1538 dp->disksize = _5btol(scsipi_sense.size); 1539 if (dp->disksize <= UINT32_MAX && dp->disksize != blocks) { 1540 printf("RBC size: mode sense=%llu, get cap=%llu\n", 1541 (unsigned long long)dp->disksize, 1542 (unsigned long long)blocks); 1543 dp->disksize = blocks; 1544 } 1545 dp->disksize512 = (dp->disksize * dp->blksize) / DEV_BSIZE; 1546 1547 return (SDGP_RESULT_OK); 1548 } 1549 1550 /* 1551 * Get the scsi driver to send a full inquiry to the * device and use the 1552 * results to fill out the disk parameter structure. 1553 */ 1554 static int 1555 sd_get_capacity(struct sd_softc *sd, struct disk_parms *dp, int flags) 1556 { 1557 u_int64_t blocks; 1558 int error, blksize; 1559 #if 0 1560 int i; 1561 u_int8_t *p; 1562 #endif 1563 1564 dp->disksize = blocks = sd_read_capacity(sd, &blksize, flags); 1565 if (blocks == 0) { 1566 struct scsipi_read_format_capacities cmd; 1567 struct { 1568 struct scsipi_capacity_list_header header; 1569 struct scsipi_capacity_descriptor desc; 1570 } __packed data; 1571 1572 memset(&cmd, 0, sizeof(cmd)); 1573 memset(&data, 0, sizeof(data)); 1574 cmd.opcode = READ_FORMAT_CAPACITIES; 1575 _lto2b(sizeof(data), cmd.length); 1576 1577 error = scsipi_command(sd->sc_periph, 1578 (void *)&cmd, sizeof(cmd), (void *)&data, sizeof(data), 1579 SDRETRIES, 20000, NULL, 1580 flags | XS_CTL_DATA_IN); 1581 if (error == EFTYPE) { 1582 /* Medium Format Corrupted, handle as not formatted */ 1583 return (SDGP_RESULT_UNFORMATTED); 1584 } 1585 if (error || data.header.length == 0) 1586 return (SDGP_RESULT_OFFLINE); 1587 1588 #if 0 1589 printf("rfc: length=%d\n", data.header.length); 1590 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"); 1591 #endif 1592 switch (data.desc.byte5 & SCSIPI_CAP_DESC_CODE_MASK) { 1593 case SCSIPI_CAP_DESC_CODE_RESERVED: 1594 case SCSIPI_CAP_DESC_CODE_FORMATTED: 1595 break; 1596 1597 case SCSIPI_CAP_DESC_CODE_UNFORMATTED: 1598 return (SDGP_RESULT_UNFORMATTED); 1599 1600 case SCSIPI_CAP_DESC_CODE_NONE: 1601 return (SDGP_RESULT_OFFLINE); 1602 } 1603 1604 dp->disksize = blocks = _4btol(data.desc.nblks); 1605 if (blocks == 0) 1606 return (SDGP_RESULT_OFFLINE); /* XXX? */ 1607 1608 blksize = _3btol(data.desc.blklen); 1609 1610 } else if (!sd_validate_blksize(NULL, blksize)) { 1611 struct sd_mode_sense_data scsipi_sense; 1612 int big, bsize; 1613 struct scsi_general_block_descriptor *bdesc; 1614 1615 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 1616 error = sd_mode_sense(sd, 0, &scsipi_sense, 1617 sizeof(scsipi_sense.blk_desc), 0, flags | XS_CTL_SILENT, 1618 &big); 1619 if (error) 1620 goto next; 1621 if (big) { 1622 bdesc = (void *)(&scsipi_sense.header.big + 1); 1623 bsize = _2btol(scsipi_sense.header.big.blk_desc_len); 1624 } else { 1625 bdesc = (void *)(&scsipi_sense.header.small + 1); 1626 bsize = scsipi_sense.header.small.blk_desc_len; 1627 } 1628 1629 #if 0 1630 printf("page 0 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n"); 1631 printf("page 0 bsize=%d\n", bsize); 1632 printf("page 0 ok\n"); 1633 #endif 1634 1635 if (bsize >= 8) { 1636 blksize = _3btol(bdesc->blklen); 1637 } 1638 } 1639 next: 1640 if (!sd_validate_blksize(sd->sc_periph, blksize)) 1641 blksize = SD_DEFAULT_BLKSIZE; 1642 1643 dp->blksize = blksize; 1644 dp->disksize512 = (blocks * dp->blksize) / DEV_BSIZE; 1645 1646 if ((sd->flags & SDF_LBPME) == 0) 1647 goto end; 1648 struct scsipi_inquiry cmd; 1649 struct scsi_vpd_logical_block_provisioning vpdbuf; 1650 memset(&cmd, 0, sizeof(cmd)); 1651 cmd.opcode = INQUIRY; 1652 cmd.length = sizeof(vpdbuf); 1653 cmd.byte2 |= SINQ_EVPD; 1654 cmd.pagecode = SINQ_VPD_LOGICAL_PROV; 1655 1656 sd->flags &= ~SDF_LBPU; 1657 if (scsipi_command(sd->sc_periph, (void *)&cmd, sizeof(cmd), 1658 (void *)&vpdbuf, sizeof(vpdbuf), 1659 SDRETRIES, 100000, NULL, 1660 flags | XS_CTL_DATA_IN | XS_CTL_IGNORE_ILLEGAL_REQUEST)) 1661 goto end; 1662 1663 if (vpdbuf.flags & VPD_LBP_LBPU) 1664 sd->flags |= SDF_LBPU; 1665 1666 end: 1667 return (0); 1668 } 1669 1670 static int 1671 sd_get_parms_page4(struct sd_softc *sd, struct disk_parms *dp, int flags) 1672 { 1673 struct sd_mode_sense_data scsipi_sense; 1674 int error; 1675 int big, byte2; 1676 size_t poffset; 1677 union scsi_disk_pages *pages; 1678 1679 byte2 = SMS_DBD; 1680 again: 1681 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 1682 error = sd_mode_sense(sd, byte2, &scsipi_sense, 1683 (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) + 1684 sizeof(scsipi_sense.pages.rigid_geometry), 4, 1685 flags | XS_CTL_SILENT, &big); 1686 if (error) { 1687 if (byte2 == SMS_DBD) { 1688 /* No result; try once more with DBD off */ 1689 byte2 = 0; 1690 goto again; 1691 } 1692 return (error); 1693 } 1694 1695 if (big) { 1696 poffset = sizeof scsipi_sense.header.big; 1697 poffset += _2btol(scsipi_sense.header.big.blk_desc_len); 1698 } else { 1699 poffset = sizeof scsipi_sense.header.small; 1700 poffset += scsipi_sense.header.small.blk_desc_len; 1701 } 1702 1703 if (poffset > sizeof(scsipi_sense) - sizeof(pages->rigid_geometry)) 1704 return ERESTART; 1705 1706 pages = (void *)((u_long)&scsipi_sense + poffset); 1707 #if 0 1708 { 1709 size_t i; 1710 u_int8_t *p; 1711 1712 printf("page 4 sense:"); 1713 for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; 1714 i--, p++) 1715 printf(" %02x", *p); 1716 printf("\n"); 1717 printf("page 4 pg_code=%d sense=%p/%p\n", 1718 pages->rigid_geometry.pg_code, &scsipi_sense, pages); 1719 } 1720 #endif 1721 1722 if ((pages->rigid_geometry.pg_code & PGCODE_MASK) != 4) 1723 return (ERESTART); 1724 1725 SC_DEBUG(sd->sc_periph, SCSIPI_DB3, 1726 ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n", 1727 _3btol(pages->rigid_geometry.ncyl), 1728 pages->rigid_geometry.nheads, 1729 _2btol(pages->rigid_geometry.st_cyl_wp), 1730 _2btol(pages->rigid_geometry.st_cyl_rwc), 1731 _2btol(pages->rigid_geometry.land_zone))); 1732 1733 /* 1734 * KLUDGE!! (for zone recorded disks) 1735 * give a number of sectors so that sec * trks * cyls 1736 * is <= disk_size 1737 * can lead to wasted space! THINK ABOUT THIS ! 1738 */ 1739 dp->heads = pages->rigid_geometry.nheads; 1740 dp->cyls = _3btol(pages->rigid_geometry.ncyl); 1741 if (dp->heads == 0 || dp->cyls == 0) 1742 return (ERESTART); 1743 dp->sectors = dp->disksize / (dp->heads * dp->cyls); /* XXX */ 1744 1745 dp->rot_rate = _2btol(pages->rigid_geometry.rpm); 1746 if (dp->rot_rate == 0) 1747 dp->rot_rate = 3600; 1748 1749 #if 0 1750 printf("page 4 ok\n"); 1751 #endif 1752 return (0); 1753 } 1754 1755 static int 1756 sd_get_parms_page5(struct sd_softc *sd, struct disk_parms *dp, int flags) 1757 { 1758 struct sd_mode_sense_data scsipi_sense; 1759 int error; 1760 int big, byte2; 1761 size_t poffset; 1762 union scsi_disk_pages *pages; 1763 1764 byte2 = SMS_DBD; 1765 again: 1766 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 1767 error = sd_mode_sense(sd, 0, &scsipi_sense, 1768 (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) + 1769 sizeof(scsipi_sense.pages.flex_geometry), 5, 1770 flags | XS_CTL_SILENT, &big); 1771 if (error) { 1772 if (byte2 == SMS_DBD) { 1773 /* No result; try once more with DBD off */ 1774 byte2 = 0; 1775 goto again; 1776 } 1777 return (error); 1778 } 1779 1780 if (big) { 1781 poffset = sizeof scsipi_sense.header.big; 1782 poffset += _2btol(scsipi_sense.header.big.blk_desc_len); 1783 } else { 1784 poffset = sizeof scsipi_sense.header.small; 1785 poffset += scsipi_sense.header.small.blk_desc_len; 1786 } 1787 1788 if (poffset > sizeof(scsipi_sense) - sizeof(pages->flex_geometry)) 1789 return ERESTART; 1790 1791 pages = (void *)((u_long)&scsipi_sense + poffset); 1792 #if 0 1793 { 1794 size_t i; 1795 u_int8_t *p; 1796 1797 printf("page 5 sense:"); 1798 for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; 1799 i--, p++) 1800 printf(" %02x", *p); 1801 printf("\n"); 1802 printf("page 5 pg_code=%d sense=%p/%p\n", 1803 pages->flex_geometry.pg_code, &scsipi_sense, pages); 1804 } 1805 #endif 1806 1807 if ((pages->flex_geometry.pg_code & PGCODE_MASK) != 5) 1808 return (ERESTART); 1809 1810 SC_DEBUG(sd->sc_periph, SCSIPI_DB3, 1811 ("%d cyls, %d heads, %d sec, %d bytes/sec\n", 1812 _3btol(pages->flex_geometry.ncyl), 1813 pages->flex_geometry.nheads, 1814 pages->flex_geometry.ph_sec_tr, 1815 _2btol(pages->flex_geometry.bytes_s))); 1816 1817 dp->heads = pages->flex_geometry.nheads; 1818 dp->cyls = _2btol(pages->flex_geometry.ncyl); 1819 dp->sectors = pages->flex_geometry.ph_sec_tr; 1820 if (dp->heads == 0 || dp->cyls == 0 || dp->sectors == 0) 1821 return (ERESTART); 1822 1823 dp->rot_rate = _2btol(pages->rigid_geometry.rpm); 1824 if (dp->rot_rate == 0) 1825 dp->rot_rate = 3600; 1826 1827 #if 0 1828 printf("page 5 ok\n"); 1829 #endif 1830 return (0); 1831 } 1832 1833 static int 1834 sd_get_parms(struct sd_softc *sd, struct disk_parms *dp, int flags) 1835 { 1836 struct dk_softc *dksc = &sd->sc_dksc; 1837 int error; 1838 1839 /* 1840 * If offline, the SDEV_MEDIA_LOADED flag will be 1841 * cleared by the caller if necessary. 1842 */ 1843 if (sd->type == T_SIMPLE_DIRECT) { 1844 error = sd_get_simplifiedparms(sd, dp, flags); 1845 if (!error) 1846 goto setprops; 1847 return (error); 1848 } 1849 1850 error = sd_get_capacity(sd, dp, flags); 1851 if (error) 1852 return (error); 1853 1854 if (sd->type == T_OPTICAL) 1855 goto page0; 1856 1857 if (sd->sc_periph->periph_flags & PERIPH_REMOVABLE) { 1858 if (!sd_get_parms_page5(sd, dp, flags) || 1859 !sd_get_parms_page4(sd, dp, flags)) 1860 goto setprops; 1861 } else { 1862 if (!sd_get_parms_page4(sd, dp, flags) || 1863 !sd_get_parms_page5(sd, dp, flags)) 1864 goto setprops; 1865 } 1866 1867 page0: 1868 printf("%s: fabricating a geometry\n", dksc->sc_xname); 1869 /* Try calling driver's method for figuring out geometry. */ 1870 if (!sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom || 1871 !(*sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom) 1872 (sd->sc_periph, dp, dp->disksize)) { 1873 /* 1874 * Use adaptec standard fictitious geometry 1875 * this depends on which controller (e.g. 1542C is 1876 * different. but we have to put SOMETHING here..) 1877 */ 1878 dp->heads = 64; 1879 dp->sectors = 32; 1880 dp->cyls = dp->disksize / (64 * 32); 1881 } 1882 dp->rot_rate = 3600; 1883 1884 setprops: 1885 sd_set_geometry(sd); 1886 1887 return (SDGP_RESULT_OK); 1888 } 1889 1890 static int 1891 sd_flush(struct sd_softc *sd, int flags) 1892 { 1893 struct scsipi_periph *periph = sd->sc_periph; 1894 struct scsi_synchronize_cache_10 cmd; 1895 1896 /* 1897 * If the device is SCSI-2, issue a SYNCHRONIZE CACHE. 1898 * We issue with address 0 length 0, which should be 1899 * interpreted by the device as "all remaining blocks 1900 * starting at address 0". We ignore ILLEGAL REQUEST 1901 * in the event that the command is not supported by 1902 * the device, and poll for completion so that we know 1903 * that the cache has actually been flushed. 1904 * 1905 * Unless, that is, the device can't handle the SYNCHRONIZE CACHE 1906 * command, as indicated by our quirks flags. 1907 * 1908 * XXX What about older devices? 1909 */ 1910 if (periph->periph_version < 2 || 1911 (periph->periph_quirks & PQUIRK_NOSYNCCACHE)) 1912 return (0); 1913 1914 sd->flags |= SDF_FLUSHING; 1915 memset(&cmd, 0, sizeof(cmd)); 1916 cmd.opcode = SCSI_SYNCHRONIZE_CACHE_10; 1917 1918 return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0, 1919 SDRETRIES, 100000, NULL, flags | XS_CTL_IGNORE_ILLEGAL_REQUEST)); 1920 } 1921 1922 static int 1923 sd_getcache(struct sd_softc *sd, int *bitsp) 1924 { 1925 struct scsipi_periph *periph = sd->sc_periph; 1926 struct sd_mode_sense_data scsipi_sense; 1927 int error, bits = 0; 1928 int big; 1929 union scsi_disk_pages *pages; 1930 uint8_t dev_spec; 1931 1932 /* only SCSI-2 and later supported */ 1933 if (periph->periph_version < 2) 1934 return (EOPNOTSUPP); 1935 1936 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 1937 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense, 1938 sizeof(scsipi_sense.pages.caching_params), 8, XS_CTL_SILENT, &big); 1939 if (error) 1940 return (error); 1941 1942 if (big) { 1943 pages = (void *)(&scsipi_sense.header.big + 1); 1944 dev_spec = scsipi_sense.header.big.dev_spec; 1945 } else { 1946 pages = (void *)(&scsipi_sense.header.small + 1); 1947 dev_spec = scsipi_sense.header.small.dev_spec; 1948 } 1949 1950 if ((pages->caching_params.flags & CACHING_RCD) == 0) 1951 bits |= DKCACHE_READ; 1952 if (pages->caching_params.flags & CACHING_WCE) 1953 bits |= DKCACHE_WRITE; 1954 if (pages->caching_params.pg_code & PGCODE_PS) 1955 bits |= DKCACHE_SAVE; 1956 1957 /* 1958 * Support for FUA/DPO, defined starting with SCSI-2. Use only 1959 * if device claims to support it, according to the MODE SENSE. 1960 */ 1961 if (!(periph->periph_quirks & PQUIRK_NOFUA) && 1962 ISSET(dev_spec, SMH_DSP_DPOFUA)) 1963 bits |= DKCACHE_FUA | DKCACHE_DPO; 1964 1965 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 1966 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense, 1967 sizeof(scsipi_sense.pages.caching_params), 1968 SMS_PCTRL_CHANGEABLE|8, XS_CTL_SILENT, &big); 1969 if (error == 0) { 1970 if (big) 1971 pages = (void *)(&scsipi_sense.header.big + 1); 1972 else 1973 pages = (void *)(&scsipi_sense.header.small + 1); 1974 1975 if (pages->caching_params.flags & CACHING_RCD) 1976 bits |= DKCACHE_RCHANGE; 1977 if (pages->caching_params.flags & CACHING_WCE) 1978 bits |= DKCACHE_WCHANGE; 1979 } 1980 1981 *bitsp = bits; 1982 1983 return (0); 1984 } 1985 1986 static int 1987 sd_setcache(struct sd_softc *sd, int bits) 1988 { 1989 struct scsipi_periph *periph = sd->sc_periph; 1990 struct sd_mode_sense_data scsipi_sense; 1991 int error; 1992 uint8_t oflags, byte2 = 0; 1993 int big; 1994 union scsi_disk_pages *pages; 1995 1996 if (periph->periph_version < 2) 1997 return (EOPNOTSUPP); 1998 1999 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 2000 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense, 2001 sizeof(scsipi_sense.pages.caching_params), 8, 0, &big); 2002 if (error) 2003 return (error); 2004 2005 if (big) 2006 pages = (void *)(&scsipi_sense.header.big + 1); 2007 else 2008 pages = (void *)(&scsipi_sense.header.small + 1); 2009 2010 oflags = pages->caching_params.flags; 2011 2012 if (bits & DKCACHE_READ) 2013 pages->caching_params.flags &= ~CACHING_RCD; 2014 else 2015 pages->caching_params.flags |= CACHING_RCD; 2016 2017 if (bits & DKCACHE_WRITE) 2018 pages->caching_params.flags |= CACHING_WCE; 2019 else 2020 pages->caching_params.flags &= ~CACHING_WCE; 2021 2022 if (oflags == pages->caching_params.flags) 2023 return (0); 2024 2025 pages->caching_params.pg_code &= PGCODE_MASK; 2026 2027 if (bits & DKCACHE_SAVE) 2028 byte2 |= SMS_SP; 2029 2030 return (sd_mode_select(sd, byte2|SMS_PF, &scsipi_sense, 2031 sizeof(struct scsi_mode_page_header) + 2032 pages->caching_params.pg_length, 0, big)); 2033 } 2034 2035 static void 2036 sd_set_geometry(struct sd_softc *sd) 2037 { 2038 struct dk_softc *dksc = &sd->sc_dksc; 2039 struct disk_geom *dg = &dksc->sc_dkdev.dk_geom; 2040 2041 memset(dg, 0, sizeof(*dg)); 2042 2043 dg->dg_secperunit = sd->params.disksize; 2044 dg->dg_secsize = sd->params.blksize; 2045 dg->dg_nsectors = sd->params.sectors; 2046 dg->dg_ntracks = sd->params.heads; 2047 dg->dg_ncylinders = sd->params.cyls; 2048 dg->dg_physsecsize = dg->dg_secsize << sd->params.lbppbe; 2049 dg->dg_alignedsec = sd->params.lalba; 2050 2051 disk_set_info(dksc->sc_dev, &dksc->sc_dkdev, sd->typename); 2052 } 2053 2054 static int 2055 sddiscard(dev_t dev, off_t pos, off_t len) 2056 { 2057 struct sd_softc *sd; 2058 int unit; 2059 2060 unit = SDUNIT(dev); 2061 sd = device_lookup_private(&sd_cd, unit); 2062 2063 return dk_discard(&sd->sc_dksc, dev, pos, len); 2064 } 2065 2066