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