1 /* $NetBSD: sd.c,v 1.347 2025/10/11 13:58:57 mlelstv 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.347 2025/10/11 13:58:57 mlelstv 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 sd->flags &= ~SDF_LBPME; 1426 1427 /* 1428 * Don't allocate data buffer on stack; 1429 * The lower driver layer might use the same stack and 1430 * if it uses region which is in the same cacheline, 1431 * cache flush ops against the data buffer won't work properly. 1432 */ 1433 datap = malloc(sizeof(*datap), M_TEMP, M_WAITOK); 1434 if (datap == NULL) 1435 return 0; 1436 1437 rv = 0; 1438 1439 /* 1440 * When device announces SPC-3 conformance, 1441 * try READ CAPACITY (16) to collect information 1442 * about physical blocks and management capabilities. 1443 * 1444 * Fall back to READ CAPACITY (10). 1445 */ 1446 1447 memset(&cmd, 0, sizeof(cmd)); 1448 cmd.cmd16.opcode = READ_CAPACITY_16; 1449 cmd.cmd16.byte2 = SRC16_SERVICE_ACTION; 1450 _lto4b(sizeof(datap->data16), cmd.cmd16.len); 1451 1452 memset(datap, 0, sizeof(datap->data16)); 1453 if (periph->periph_version >= 5 && 1454 scsipi_command(periph, (void *)&cmd.cmd16, sizeof(cmd.cmd16), 1455 (void *)datap, sizeof(datap->data16), SCSIPIRETRIES, 20000, 1456 NULL, flags | XS_CTL_DATA_IN | XS_CTL_SILENT) == 0) { 1457 1458 *blksize = _4btol(datap->data16.length); 1459 rv = _8btol(datap->data16.addr) + 1; 1460 1461 sd->params.lbppbe = datap->data16.byte14 1462 & SRC16D_LBPPB_EXPONENT; 1463 sd->params.lalba = _2btol(datap->data16.lowest_aligned) 1464 & SRC16D_LALBA; 1465 if (_2btol(datap->data16.lowest_aligned) & SRC16D_LBPME) 1466 sd->flags |= SDF_LBPME; 1467 } else { 1468 memset(&cmd, 0, sizeof(cmd)); 1469 cmd.cmd.opcode = READ_CAPACITY_10; 1470 1471 memset(datap, 0, sizeof(datap->data)); 1472 if (scsipi_command(periph, (void *)&cmd.cmd, sizeof(cmd.cmd), 1473 (void *)datap, sizeof(datap->data), SCSIPIRETRIES, 20000, 1474 NULL, flags | XS_CTL_DATA_IN | XS_CTL_SILENT) == 0) { 1475 1476 *blksize = _4btol(datap->data.length); 1477 rv = _4btol(datap->data.addr) + 1; 1478 } 1479 } 1480 1481 free(datap, M_TEMP); 1482 return rv; 1483 } 1484 1485 static int 1486 sd_get_simplifiedparms(struct sd_softc *sd, struct disk_parms *dp, int flags) 1487 { 1488 struct { 1489 struct scsi_mode_parameter_header_6 header; 1490 /* no block descriptor */ 1491 u_int8_t pg_code; /* page code (should be 6) */ 1492 u_int8_t pg_length; /* page length (should be 11) */ 1493 u_int8_t wcd; /* bit0: cache disable */ 1494 u_int8_t lbs[2]; /* logical block size */ 1495 u_int8_t size[5]; /* number of log. blocks */ 1496 u_int8_t pp; /* power/performance */ 1497 u_int8_t flags; 1498 u_int8_t resvd; 1499 } scsipi_sense; 1500 u_int64_t blocks; 1501 int error, blksize; 1502 1503 /* 1504 * sd_read_capacity (ie "read capacity") and mode sense page 6 1505 * give the same information. Do both for now, and check 1506 * for consistency. 1507 * XXX probably differs for removable media 1508 */ 1509 dp->blksize = SD_DEFAULT_BLKSIZE; 1510 if ((blocks = sd_read_capacity(sd, &blksize, flags)) == 0) 1511 return (SDGP_RESULT_OFFLINE); /* XXX? */ 1512 1513 error = scsipi_mode_sense(sd->sc_periph, SMS_DBD, 6, 1514 &scsipi_sense.header, sizeof(scsipi_sense), 1515 flags, SDRETRIES, 6000); 1516 1517 if (error != 0) 1518 return (SDGP_RESULT_OFFLINE); /* XXX? */ 1519 1520 dp->blksize = blksize; 1521 if (!sd_validate_blksize(NULL, dp->blksize)) 1522 dp->blksize = _2btol(scsipi_sense.lbs); 1523 if (!sd_validate_blksize(sd->sc_periph, dp->blksize)) 1524 dp->blksize = SD_DEFAULT_BLKSIZE; 1525 1526 /* 1527 * Create a pseudo-geometry. 1528 */ 1529 dp->heads = 64; 1530 dp->sectors = 32; 1531 dp->cyls = blocks / (dp->heads * dp->sectors); 1532 dp->disksize = _5btol(scsipi_sense.size); 1533 if (dp->disksize <= UINT32_MAX && dp->disksize != blocks) { 1534 printf("RBC size: mode sense=%llu, get cap=%llu\n", 1535 (unsigned long long)dp->disksize, 1536 (unsigned long long)blocks); 1537 dp->disksize = blocks; 1538 } 1539 dp->disksize512 = (dp->disksize * dp->blksize) / DEV_BSIZE; 1540 1541 return (SDGP_RESULT_OK); 1542 } 1543 1544 /* 1545 * Get the scsi driver to send a full inquiry to the * device and use the 1546 * results to fill out the disk parameter structure. 1547 */ 1548 static int 1549 sd_get_capacity(struct sd_softc *sd, struct disk_parms *dp, int flags) 1550 { 1551 u_int64_t blocks; 1552 int error, blksize; 1553 #if 0 1554 int i; 1555 u_int8_t *p; 1556 #endif 1557 1558 dp->disksize = blocks = sd_read_capacity(sd, &blksize, flags); 1559 if (blocks == 0) { 1560 struct scsipi_read_format_capacities cmd; 1561 struct { 1562 struct scsipi_capacity_list_header header; 1563 struct scsipi_capacity_descriptor desc; 1564 } __packed data; 1565 1566 memset(&cmd, 0, sizeof(cmd)); 1567 memset(&data, 0, sizeof(data)); 1568 cmd.opcode = READ_FORMAT_CAPACITIES; 1569 _lto2b(sizeof(data), cmd.length); 1570 1571 error = scsipi_command(sd->sc_periph, 1572 (void *)&cmd, sizeof(cmd), (void *)&data, sizeof(data), 1573 SDRETRIES, 20000, NULL, 1574 flags | XS_CTL_DATA_IN | XS_CTL_SILENT); 1575 if (error == EFTYPE) { 1576 /* Medium Format Corrupted, handle as not formatted */ 1577 return (SDGP_RESULT_UNFORMATTED); 1578 } 1579 if (error || data.header.length == 0) 1580 return (SDGP_RESULT_OFFLINE); 1581 1582 #if 0 1583 printf("rfc: length=%d\n", data.header.length); 1584 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"); 1585 #endif 1586 switch (data.desc.byte5 & SCSIPI_CAP_DESC_CODE_MASK) { 1587 case SCSIPI_CAP_DESC_CODE_RESERVED: 1588 case SCSIPI_CAP_DESC_CODE_FORMATTED: 1589 break; 1590 1591 case SCSIPI_CAP_DESC_CODE_UNFORMATTED: 1592 return (SDGP_RESULT_UNFORMATTED); 1593 1594 case SCSIPI_CAP_DESC_CODE_NONE: 1595 return (SDGP_RESULT_OFFLINE); 1596 } 1597 1598 dp->disksize = blocks = _4btol(data.desc.nblks); 1599 if (blocks == 0) 1600 return (SDGP_RESULT_OFFLINE); /* XXX? */ 1601 1602 blksize = _3btol(data.desc.blklen); 1603 1604 } else if (!sd_validate_blksize(NULL, blksize)) { 1605 struct sd_mode_sense_data scsipi_sense; 1606 int big, bsize; 1607 struct scsi_general_block_descriptor *bdesc; 1608 1609 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 1610 error = sd_mode_sense(sd, 0, &scsipi_sense, 1611 sizeof(scsipi_sense.blk_desc), 0, flags | XS_CTL_SILENT, 1612 &big); 1613 if (error) 1614 goto next; 1615 if (big) { 1616 bdesc = (void *)(&scsipi_sense.header.big + 1); 1617 bsize = _2btol(scsipi_sense.header.big.blk_desc_len); 1618 } else { 1619 bdesc = (void *)(&scsipi_sense.header.small + 1); 1620 bsize = scsipi_sense.header.small.blk_desc_len; 1621 } 1622 1623 #if 0 1624 printf("page 0 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n"); 1625 printf("page 0 bsize=%d\n", bsize); 1626 printf("page 0 ok\n"); 1627 #endif 1628 1629 if (bsize >= 8) { 1630 blksize = _3btol(bdesc->blklen); 1631 } 1632 } 1633 next: 1634 if (!sd_validate_blksize(sd->sc_periph, blksize)) 1635 blksize = SD_DEFAULT_BLKSIZE; 1636 1637 dp->blksize = blksize; 1638 dp->disksize512 = (blocks * dp->blksize) / DEV_BSIZE; 1639 1640 if ((sd->flags & SDF_LBPME) == 0) 1641 goto end; 1642 struct scsipi_inquiry cmd; 1643 struct scsi_vpd_logical_block_provisioning vpdbuf; 1644 memset(&cmd, 0, sizeof(cmd)); 1645 cmd.opcode = INQUIRY; 1646 cmd.length = sizeof(vpdbuf); 1647 cmd.byte2 |= SINQ_EVPD; 1648 cmd.pagecode = SINQ_VPD_LOGICAL_PROV; 1649 1650 sd->flags &= ~SDF_LBPU; 1651 if (scsipi_command(sd->sc_periph, (void *)&cmd, sizeof(cmd), 1652 (void *)&vpdbuf, sizeof(vpdbuf), 1653 SDRETRIES, 100000, NULL, 1654 flags | XS_CTL_DATA_IN | XS_CTL_IGNORE_ILLEGAL_REQUEST)) 1655 goto end; 1656 1657 if (vpdbuf.flags & VPD_LBP_LBPU) 1658 sd->flags |= SDF_LBPU; 1659 1660 end: 1661 return (0); 1662 } 1663 1664 static int 1665 sd_get_parms_page4(struct sd_softc *sd, struct disk_parms *dp, int flags) 1666 { 1667 struct sd_mode_sense_data scsipi_sense; 1668 int error; 1669 int big, byte2; 1670 size_t poffset; 1671 union scsi_disk_pages *pages; 1672 1673 byte2 = SMS_DBD; 1674 again: 1675 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 1676 error = sd_mode_sense(sd, byte2, &scsipi_sense, 1677 (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) + 1678 sizeof(scsipi_sense.pages.rigid_geometry), 4, 1679 flags | XS_CTL_SILENT, &big); 1680 if (error) { 1681 if (byte2 == SMS_DBD) { 1682 /* No result; try once more with DBD off */ 1683 byte2 = 0; 1684 goto again; 1685 } 1686 return (error); 1687 } 1688 1689 if (big) { 1690 poffset = sizeof scsipi_sense.header.big; 1691 poffset += _2btol(scsipi_sense.header.big.blk_desc_len); 1692 } else { 1693 poffset = sizeof scsipi_sense.header.small; 1694 poffset += scsipi_sense.header.small.blk_desc_len; 1695 } 1696 1697 if (poffset > sizeof(scsipi_sense) - sizeof(pages->rigid_geometry)) 1698 return ERESTART; 1699 1700 pages = (void *)((u_long)&scsipi_sense + poffset); 1701 #if 0 1702 { 1703 size_t i; 1704 u_int8_t *p; 1705 1706 printf("page 4 sense:"); 1707 for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; 1708 i--, p++) 1709 printf(" %02x", *p); 1710 printf("\n"); 1711 printf("page 4 pg_code=%d sense=%p/%p\n", 1712 pages->rigid_geometry.pg_code, &scsipi_sense, pages); 1713 } 1714 #endif 1715 1716 if ((pages->rigid_geometry.pg_code & PGCODE_MASK) != 4) 1717 return (ERESTART); 1718 1719 SC_DEBUG(sd->sc_periph, SCSIPI_DB3, 1720 ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n", 1721 _3btol(pages->rigid_geometry.ncyl), 1722 pages->rigid_geometry.nheads, 1723 _2btol(pages->rigid_geometry.st_cyl_wp), 1724 _2btol(pages->rigid_geometry.st_cyl_rwc), 1725 _2btol(pages->rigid_geometry.land_zone))); 1726 1727 /* 1728 * KLUDGE!! (for zone recorded disks) 1729 * give a number of sectors so that sec * trks * cyls 1730 * is <= disk_size 1731 * can lead to wasted space! THINK ABOUT THIS ! 1732 */ 1733 dp->heads = pages->rigid_geometry.nheads; 1734 dp->cyls = _3btol(pages->rigid_geometry.ncyl); 1735 if (dp->heads == 0 || dp->cyls == 0) 1736 return (ERESTART); 1737 dp->sectors = dp->disksize / (dp->heads * dp->cyls); /* XXX */ 1738 1739 dp->rot_rate = _2btol(pages->rigid_geometry.rpm); 1740 if (dp->rot_rate == 0) 1741 dp->rot_rate = 3600; 1742 1743 #if 0 1744 printf("page 4 ok\n"); 1745 #endif 1746 return (0); 1747 } 1748 1749 static int 1750 sd_get_parms_page5(struct sd_softc *sd, struct disk_parms *dp, int flags) 1751 { 1752 struct sd_mode_sense_data scsipi_sense; 1753 int error; 1754 int big, byte2; 1755 size_t poffset; 1756 union scsi_disk_pages *pages; 1757 1758 byte2 = SMS_DBD; 1759 again: 1760 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 1761 error = sd_mode_sense(sd, 0, &scsipi_sense, 1762 (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) + 1763 sizeof(scsipi_sense.pages.flex_geometry), 5, 1764 flags | XS_CTL_SILENT, &big); 1765 if (error) { 1766 if (byte2 == SMS_DBD) { 1767 /* No result; try once more with DBD off */ 1768 byte2 = 0; 1769 goto again; 1770 } 1771 return (error); 1772 } 1773 1774 if (big) { 1775 poffset = sizeof scsipi_sense.header.big; 1776 poffset += _2btol(scsipi_sense.header.big.blk_desc_len); 1777 } else { 1778 poffset = sizeof scsipi_sense.header.small; 1779 poffset += scsipi_sense.header.small.blk_desc_len; 1780 } 1781 1782 if (poffset > sizeof(scsipi_sense) - sizeof(pages->flex_geometry)) 1783 return ERESTART; 1784 1785 pages = (void *)((u_long)&scsipi_sense + poffset); 1786 #if 0 1787 { 1788 size_t i; 1789 u_int8_t *p; 1790 1791 printf("page 5 sense:"); 1792 for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; 1793 i--, p++) 1794 printf(" %02x", *p); 1795 printf("\n"); 1796 printf("page 5 pg_code=%d sense=%p/%p\n", 1797 pages->flex_geometry.pg_code, &scsipi_sense, pages); 1798 } 1799 #endif 1800 1801 if ((pages->flex_geometry.pg_code & PGCODE_MASK) != 5) 1802 return (ERESTART); 1803 1804 SC_DEBUG(sd->sc_periph, SCSIPI_DB3, 1805 ("%d cyls, %d heads, %d sec, %d bytes/sec\n", 1806 _3btol(pages->flex_geometry.ncyl), 1807 pages->flex_geometry.nheads, 1808 pages->flex_geometry.ph_sec_tr, 1809 _2btol(pages->flex_geometry.bytes_s))); 1810 1811 dp->heads = pages->flex_geometry.nheads; 1812 dp->cyls = _2btol(pages->flex_geometry.ncyl); 1813 dp->sectors = pages->flex_geometry.ph_sec_tr; 1814 if (dp->heads == 0 || dp->cyls == 0 || dp->sectors == 0) 1815 return (ERESTART); 1816 1817 dp->rot_rate = _2btol(pages->rigid_geometry.rpm); 1818 if (dp->rot_rate == 0) 1819 dp->rot_rate = 3600; 1820 1821 #if 0 1822 printf("page 5 ok\n"); 1823 #endif 1824 return (0); 1825 } 1826 1827 static int 1828 sd_get_parms(struct sd_softc *sd, struct disk_parms *dp, int flags) 1829 { 1830 struct dk_softc *dksc = &sd->sc_dksc; 1831 int error; 1832 1833 /* 1834 * If offline, the SDEV_MEDIA_LOADED flag will be 1835 * cleared by the caller if necessary. 1836 */ 1837 if (sd->type == T_SIMPLE_DIRECT) { 1838 error = sd_get_simplifiedparms(sd, dp, flags); 1839 if (!error) 1840 goto setprops; 1841 return (error); 1842 } 1843 1844 error = sd_get_capacity(sd, dp, flags); 1845 if (error) 1846 return (error); 1847 1848 if (sd->type == T_OPTICAL) 1849 goto page0; 1850 1851 if (sd->sc_periph->periph_flags & PERIPH_REMOVABLE) { 1852 if (!sd_get_parms_page5(sd, dp, flags) || 1853 !sd_get_parms_page4(sd, dp, flags)) 1854 goto setprops; 1855 } else { 1856 if (!sd_get_parms_page4(sd, dp, flags) || 1857 !sd_get_parms_page5(sd, dp, flags)) 1858 goto setprops; 1859 } 1860 1861 page0: 1862 printf("%s: fabricating a geometry\n", dksc->sc_xname); 1863 /* Try calling driver's method for figuring out geometry. */ 1864 if (!sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom || 1865 !(*sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom) 1866 (sd->sc_periph, dp, dp->disksize)) { 1867 /* 1868 * Use adaptec standard fictitious geometry 1869 * this depends on which controller (e.g. 1542C is 1870 * different. but we have to put SOMETHING here..) 1871 */ 1872 dp->heads = 64; 1873 dp->sectors = 32; 1874 dp->cyls = dp->disksize / (64 * 32); 1875 } 1876 dp->rot_rate = 3600; 1877 1878 setprops: 1879 sd_set_geometry(sd); 1880 1881 return (SDGP_RESULT_OK); 1882 } 1883 1884 static int 1885 sd_flush(struct sd_softc *sd, int flags) 1886 { 1887 struct scsipi_periph *periph = sd->sc_periph; 1888 struct scsi_synchronize_cache_10 cmd; 1889 1890 /* 1891 * If the device is SCSI-2, issue a SYNCHRONIZE CACHE. 1892 * We issue with address 0 length 0, which should be 1893 * interpreted by the device as "all remaining blocks 1894 * starting at address 0". We ignore ILLEGAL REQUEST 1895 * in the event that the command is not supported by 1896 * the device, and poll for completion so that we know 1897 * that the cache has actually been flushed. 1898 * 1899 * Unless, that is, the device can't handle the SYNCHRONIZE CACHE 1900 * command, as indicated by our quirks flags. 1901 * 1902 * XXX What about older devices? 1903 */ 1904 if (periph->periph_version < 2 || 1905 (periph->periph_quirks & PQUIRK_NOSYNCCACHE)) 1906 return (0); 1907 1908 sd->flags |= SDF_FLUSHING; 1909 memset(&cmd, 0, sizeof(cmd)); 1910 cmd.opcode = SCSI_SYNCHRONIZE_CACHE_10; 1911 1912 return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0, 1913 SDRETRIES, 100000, NULL, flags | XS_CTL_IGNORE_ILLEGAL_REQUEST)); 1914 } 1915 1916 static int 1917 sd_getcache(struct sd_softc *sd, int *bitsp) 1918 { 1919 struct scsipi_periph *periph = sd->sc_periph; 1920 struct sd_mode_sense_data scsipi_sense; 1921 int error, bits = 0; 1922 int big; 1923 union scsi_disk_pages *pages; 1924 uint8_t dev_spec; 1925 1926 /* only SCSI-2 and later supported */ 1927 if (periph->periph_version < 2) 1928 return (EOPNOTSUPP); 1929 1930 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 1931 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense, 1932 sizeof(scsipi_sense.pages.caching_params), 8, XS_CTL_SILENT, &big); 1933 if (error) 1934 return (error); 1935 1936 if (big) { 1937 pages = (void *)(&scsipi_sense.header.big + 1); 1938 dev_spec = scsipi_sense.header.big.dev_spec; 1939 } else { 1940 pages = (void *)(&scsipi_sense.header.small + 1); 1941 dev_spec = scsipi_sense.header.small.dev_spec; 1942 } 1943 1944 if ((pages->caching_params.flags & CACHING_RCD) == 0) 1945 bits |= DKCACHE_READ; 1946 if (pages->caching_params.flags & CACHING_WCE) 1947 bits |= DKCACHE_WRITE; 1948 if (pages->caching_params.pg_code & PGCODE_PS) 1949 bits |= DKCACHE_SAVE; 1950 1951 /* 1952 * Support for FUA/DPO, defined starting with SCSI-2. Use only 1953 * if device claims to support it, according to the MODE SENSE. 1954 */ 1955 if (!(periph->periph_quirks & PQUIRK_NOFUA) && 1956 ISSET(dev_spec, SMH_DSP_DPOFUA)) 1957 bits |= DKCACHE_FUA | DKCACHE_DPO; 1958 1959 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 1960 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense, 1961 sizeof(scsipi_sense.pages.caching_params), 1962 SMS_PCTRL_CHANGEABLE|8, XS_CTL_SILENT, &big); 1963 if (error == 0) { 1964 if (big) 1965 pages = (void *)(&scsipi_sense.header.big + 1); 1966 else 1967 pages = (void *)(&scsipi_sense.header.small + 1); 1968 1969 if (pages->caching_params.flags & CACHING_RCD) 1970 bits |= DKCACHE_RCHANGE; 1971 if (pages->caching_params.flags & CACHING_WCE) 1972 bits |= DKCACHE_WCHANGE; 1973 } 1974 1975 *bitsp = bits; 1976 1977 return (0); 1978 } 1979 1980 static int 1981 sd_setcache(struct sd_softc *sd, int bits) 1982 { 1983 struct scsipi_periph *periph = sd->sc_periph; 1984 struct sd_mode_sense_data scsipi_sense; 1985 int error; 1986 uint8_t oflags, byte2 = 0; 1987 int big; 1988 union scsi_disk_pages *pages; 1989 1990 if (periph->periph_version < 2) 1991 return (EOPNOTSUPP); 1992 1993 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 1994 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense, 1995 sizeof(scsipi_sense.pages.caching_params), 8, 0, &big); 1996 if (error) 1997 return (error); 1998 1999 if (big) 2000 pages = (void *)(&scsipi_sense.header.big + 1); 2001 else 2002 pages = (void *)(&scsipi_sense.header.small + 1); 2003 2004 oflags = pages->caching_params.flags; 2005 2006 if (bits & DKCACHE_READ) 2007 pages->caching_params.flags &= ~CACHING_RCD; 2008 else 2009 pages->caching_params.flags |= CACHING_RCD; 2010 2011 if (bits & DKCACHE_WRITE) 2012 pages->caching_params.flags |= CACHING_WCE; 2013 else 2014 pages->caching_params.flags &= ~CACHING_WCE; 2015 2016 if (oflags == pages->caching_params.flags) 2017 return (0); 2018 2019 pages->caching_params.pg_code &= PGCODE_MASK; 2020 2021 if (bits & DKCACHE_SAVE) 2022 byte2 |= SMS_SP; 2023 2024 return (sd_mode_select(sd, byte2|SMS_PF, &scsipi_sense, 2025 sizeof(struct scsi_mode_page_header) + 2026 pages->caching_params.pg_length, 0, big)); 2027 } 2028 2029 static void 2030 sd_set_geometry(struct sd_softc *sd) 2031 { 2032 struct dk_softc *dksc = &sd->sc_dksc; 2033 struct disk_geom *dg = &dksc->sc_dkdev.dk_geom; 2034 2035 memset(dg, 0, sizeof(*dg)); 2036 2037 dg->dg_secperunit = sd->params.disksize; 2038 dg->dg_secsize = sd->params.blksize; 2039 dg->dg_nsectors = sd->params.sectors; 2040 dg->dg_ntracks = sd->params.heads; 2041 dg->dg_ncylinders = sd->params.cyls; 2042 dg->dg_physsecsize = dg->dg_secsize << sd->params.lbppbe; 2043 dg->dg_alignedsec = sd->params.lalba; 2044 2045 disk_set_info(dksc->sc_dev, &dksc->sc_dkdev, sd->typename); 2046 } 2047 2048 static int 2049 sddiscard(dev_t dev, off_t pos, off_t len) 2050 { 2051 struct sd_softc *sd; 2052 int unit; 2053 2054 unit = SDUNIT(dev); 2055 sd = device_lookup_private(&sd_cd, unit); 2056 2057 return dk_discard(&sd->sc_dksc, dev, pos, len); 2058 } 2059 2060