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