1 /* $NetBSD: sd.c,v 1.255 2006/11/25 12:06:31 scw 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.255 2006/11/25 12:06:31 scw 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 int 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, 198 void *aux) 199 { 200 struct scsipibus_attach_args *sa = aux; 201 int priority; 202 203 (void)scsipi_inqmatch(&sa->sa_inqbuf, 204 sd_patterns, sizeof(sd_patterns) / sizeof(sd_patterns[0]), 205 sizeof(sd_patterns[0]), &priority); 206 207 return (priority); 208 } 209 210 /* 211 * Attach routine common to atapi & scsi. 212 */ 213 static void 214 sdattach(struct device *parent, struct device *self, void *aux) 215 { 216 struct sd_softc *sd = device_private(self); 217 struct scsipibus_attach_args *sa = aux; 218 struct scsipi_periph *periph = sa->sa_periph; 219 int error, result; 220 struct disk_parms *dp = &sd->params; 221 char pbuf[9]; 222 223 SC_DEBUG(periph, SCSIPI_DB2, ("sdattach: ")); 224 225 sd->type = (sa->sa_inqbuf.type & SID_TYPE); 226 if (sd->type == T_SIMPLE_DIRECT) 227 periph->periph_quirks |= PQUIRK_ONLYBIG | PQUIRK_NOBIGMODESENSE; 228 229 if (scsipi_periph_bustype(sa->sa_periph) == SCSIPI_BUSTYPE_SCSI && 230 periph->periph_version == 0) 231 sd->flags |= SDF_ANCIENT; 232 233 bufq_alloc(&sd->buf_queue, 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 = device_private(self); 350 int s, bmaj, cmaj, i, mn; 351 352 /* locate the major number */ 353 bmaj = bdevsw_lookup_major(&sd_bdevsw); 354 cmaj = cdevsw_lookup_major(&sd_cdevsw); 355 356 /* Nuke the vnodes for any open instances */ 357 for (i = 0; i < MAXPARTITIONS; i++) { 358 mn = SDMINOR(device_unit(self), i); 359 vdevgone(bmaj, mn, mn, VBLK); 360 vdevgone(cmaj, mn, mn, VCHR); 361 } 362 363 /* kill any pending restart */ 364 callout_stop(&sd->sc_callout); 365 366 /* Delete all of our wedges. */ 367 dkwedge_delall(&sd->sc_dk); 368 369 s = splbio(); 370 371 /* Kill off any queued buffers. */ 372 bufq_drain(sd->buf_queue); 373 374 bufq_free(sd->buf_queue); 375 376 /* Kill off any pending commands. */ 377 scsipi_kill_pending(sd->sc_periph); 378 379 splx(s); 380 381 /* Detach from the disk list. */ 382 disk_detach(&sd->sc_dk); 383 384 /* Get rid of the shutdown hook. */ 385 shutdownhook_disestablish(sd->sc_sdhook); 386 387 #if NRND > 0 388 /* Unhook the entropy source. */ 389 rnd_detach_source(&sd->rnd_source); 390 #endif 391 392 return (0); 393 } 394 395 /* 396 * open the device. Make sure the partition info is a up-to-date as can be. 397 */ 398 static int 399 sdopen(dev_t dev, int flag, int fmt, struct lwp *l) 400 { 401 struct sd_softc *sd; 402 struct scsipi_periph *periph; 403 struct scsipi_adapter *adapt; 404 int unit, part; 405 int error; 406 407 unit = SDUNIT(dev); 408 if (unit >= sd_cd.cd_ndevs) 409 return (ENXIO); 410 sd = sd_cd.cd_devs[unit]; 411 if (sd == NULL) 412 return (ENXIO); 413 414 if (!device_is_active(&sd->sc_dev)) 415 return (ENODEV); 416 417 part = SDPART(dev); 418 419 if ((error = lockmgr(&sd->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0) 420 return (error); 421 422 /* 423 * If there are wedges, and this is not RAW_PART, then we 424 * need to fail. 425 */ 426 if (sd->sc_dk.dk_nwedges != 0 && part != RAW_PART) { 427 error = EBUSY; 428 goto bad1; 429 } 430 431 periph = sd->sc_periph; 432 adapt = periph->periph_channel->chan_adapter; 433 434 SC_DEBUG(periph, SCSIPI_DB1, 435 ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit, 436 sd_cd.cd_ndevs, part)); 437 438 /* 439 * If this is the first open of this device, add a reference 440 * to the adapter. 441 */ 442 if (sd->sc_dk.dk_openmask == 0 && 443 (error = scsipi_adapter_addref(adapt)) != 0) 444 goto bad1; 445 446 if ((periph->periph_flags & PERIPH_OPEN) != 0) { 447 /* 448 * If any partition is open, but the disk has been invalidated, 449 * disallow further opens of non-raw partition 450 */ 451 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 && 452 (part != RAW_PART || fmt != S_IFCHR)) { 453 error = EIO; 454 goto bad2; 455 } 456 } else { 457 int silent; 458 459 if (part == RAW_PART && fmt == S_IFCHR) 460 silent = XS_CTL_SILENT; 461 else 462 silent = 0; 463 464 /* Check that it is still responding and ok. */ 465 error = scsipi_test_unit_ready(periph, 466 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE | 467 silent); 468 469 /* 470 * Start the pack spinning if necessary. Always allow the 471 * raw parition to be opened, for raw IOCTLs. Data transfers 472 * will check for SDEV_MEDIA_LOADED. 473 */ 474 if (error == EIO) { 475 int error2; 476 477 error2 = scsipi_start(periph, SSS_START, silent); 478 switch (error2) { 479 case 0: 480 error = 0; 481 break; 482 case EIO: 483 case EINVAL: 484 break; 485 default: 486 error = error2; 487 break; 488 } 489 } 490 if (error) { 491 if (silent) 492 goto out; 493 goto bad2; 494 } 495 496 periph->periph_flags |= PERIPH_OPEN; 497 498 if (periph->periph_flags & PERIPH_REMOVABLE) { 499 /* Lock the pack in. */ 500 error = scsipi_prevent(periph, SPAMR_PREVENT_DT, 501 XS_CTL_IGNORE_ILLEGAL_REQUEST | 502 XS_CTL_IGNORE_MEDIA_CHANGE | 503 XS_CTL_SILENT); 504 if (error) 505 goto bad3; 506 } 507 508 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 509 int param_error; 510 periph->periph_flags |= PERIPH_MEDIA_LOADED; 511 512 /* 513 * Load the physical device parameters. 514 * 515 * Note that if media is present but unformatted, 516 * we allow the open (so that it can be formatted!). 517 * The drive should refuse real I/O, if the media is 518 * unformatted. 519 */ 520 if ((param_error = sd_get_parms(sd, &sd->params, 0)) 521 == SDGP_RESULT_OFFLINE) { 522 error = ENXIO; 523 periph->periph_flags &= ~PERIPH_MEDIA_LOADED; 524 goto bad3; 525 } 526 SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded ")); 527 528 /* Load the partition info if not already loaded. */ 529 if (param_error == 0) { 530 if ((sdgetdisklabel(sd) != 0) && (part != RAW_PART)) { 531 error = EIO; 532 goto bad3; 533 } 534 SC_DEBUG(periph, SCSIPI_DB3, 535 ("Disklabel loaded ")); 536 } 537 } 538 } 539 540 /* Check that the partition exists. */ 541 if (part != RAW_PART && 542 (part >= sd->sc_dk.dk_label->d_npartitions || 543 sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) { 544 error = ENXIO; 545 goto bad3; 546 } 547 548 out: /* Insure only one open at a time. */ 549 switch (fmt) { 550 case S_IFCHR: 551 sd->sc_dk.dk_copenmask |= (1 << part); 552 break; 553 case S_IFBLK: 554 sd->sc_dk.dk_bopenmask |= (1 << part); 555 break; 556 } 557 sd->sc_dk.dk_openmask = 558 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask; 559 560 SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n")); 561 (void) lockmgr(&sd->sc_dk.dk_openlock, LK_RELEASE, NULL); 562 return (0); 563 564 bad3: 565 if (sd->sc_dk.dk_openmask == 0) { 566 if (periph->periph_flags & PERIPH_REMOVABLE) 567 scsipi_prevent(periph, SPAMR_ALLOW, 568 XS_CTL_IGNORE_ILLEGAL_REQUEST | 569 XS_CTL_IGNORE_MEDIA_CHANGE | 570 XS_CTL_SILENT); 571 periph->periph_flags &= ~PERIPH_OPEN; 572 } 573 574 bad2: 575 if (sd->sc_dk.dk_openmask == 0) 576 scsipi_adapter_delref(adapt); 577 578 bad1: 579 (void) lockmgr(&sd->sc_dk.dk_openlock, LK_RELEASE, NULL); 580 return (error); 581 } 582 583 /* 584 * close the device.. only called if we are the LAST occurence of an open 585 * device. Convenient now but usually a pain. 586 */ 587 static int 588 sdclose(dev_t dev, int flag, int fmt, struct lwp *l) 589 { 590 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)]; 591 struct scsipi_periph *periph = sd->sc_periph; 592 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; 593 int part = SDPART(dev); 594 int error; 595 596 if ((error = lockmgr(&sd->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0) 597 return (error); 598 599 switch (fmt) { 600 case S_IFCHR: 601 sd->sc_dk.dk_copenmask &= ~(1 << part); 602 break; 603 case S_IFBLK: 604 sd->sc_dk.dk_bopenmask &= ~(1 << part); 605 break; 606 } 607 sd->sc_dk.dk_openmask = 608 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask; 609 610 if (sd->sc_dk.dk_openmask == 0) { 611 /* 612 * If the disk cache needs flushing, and the disk supports 613 * it, do it now. 614 */ 615 if ((sd->flags & SDF_DIRTY) != 0) { 616 if (sd_flush(sd, 0)) { 617 printf("%s: cache synchronization failed\n", 618 sd->sc_dev.dv_xname); 619 sd->flags &= ~SDF_FLUSHING; 620 } else 621 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 622 } 623 624 scsipi_wait_drain(periph); 625 626 if (periph->periph_flags & PERIPH_REMOVABLE) 627 scsipi_prevent(periph, SPAMR_ALLOW, 628 XS_CTL_IGNORE_ILLEGAL_REQUEST | 629 XS_CTL_IGNORE_NOT_READY | 630 XS_CTL_SILENT); 631 periph->periph_flags &= ~PERIPH_OPEN; 632 633 scsipi_wait_drain(periph); 634 635 scsipi_adapter_delref(adapt); 636 } 637 638 (void) lockmgr(&sd->sc_dk.dk_openlock, LK_RELEASE, NULL); 639 return (0); 640 } 641 642 /* 643 * Actually translate the requested transfer into one the physical driver 644 * can understand. The transfer is described by a buf and will include 645 * only one physical transfer. 646 */ 647 static void 648 sdstrategy(struct buf *bp) 649 { 650 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)]; 651 struct scsipi_periph *periph = sd->sc_periph; 652 struct disklabel *lp; 653 daddr_t blkno; 654 int s; 655 boolean_t sector_aligned; 656 657 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdstrategy ")); 658 SC_DEBUG(sd->sc_periph, SCSIPI_DB1, 659 ("%d bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno)); 660 /* 661 * If the device has been made invalid, error out 662 */ 663 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 || 664 !device_is_active(&sd->sc_dev)) { 665 if (periph->periph_flags & PERIPH_OPEN) 666 bp->b_error = EIO; 667 else 668 bp->b_error = ENODEV; 669 goto bad; 670 } 671 672 lp = sd->sc_dk.dk_label; 673 674 /* 675 * The transfer must be a whole number of blocks, offset must not be 676 * negative. 677 */ 678 if (lp->d_secsize == DEV_BSIZE) { 679 sector_aligned = (bp->b_bcount & (DEV_BSIZE - 1)) == 0; 680 } else { 681 sector_aligned = (bp->b_bcount % lp->d_secsize) == 0; 682 } 683 if (!sector_aligned || bp->b_blkno < 0) { 684 bp->b_error = EINVAL; 685 goto bad; 686 } 687 /* 688 * If it's a null transfer, return immediatly 689 */ 690 if (bp->b_bcount == 0) 691 goto done; 692 693 /* 694 * Do bounds checking, adjust transfer. if error, process. 695 * If end of partition, just return. 696 */ 697 if (SDPART(bp->b_dev) == RAW_PART) { 698 if (bounds_check_with_mediasize(bp, DEV_BSIZE, 699 sd->params.disksize512) <= 0) 700 goto done; 701 } else { 702 if (bounds_check_with_label(&sd->sc_dk, bp, 703 (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0) 704 goto done; 705 } 706 707 /* 708 * Now convert the block number to absolute and put it in 709 * terms of the device's logical block size. 710 */ 711 if (lp->d_secsize == DEV_BSIZE) 712 blkno = bp->b_blkno; 713 else if (lp->d_secsize > DEV_BSIZE) 714 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE); 715 else 716 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize); 717 718 if (SDPART(bp->b_dev) != RAW_PART) 719 blkno += lp->d_partitions[SDPART(bp->b_dev)].p_offset; 720 721 bp->b_rawblkno = blkno; 722 723 s = splbio(); 724 725 /* 726 * Place it in the queue of disk activities for this disk. 727 * 728 * XXX Only do disksort() if the current operating mode does not 729 * XXX include tagged queueing. 730 */ 731 BUFQ_PUT(sd->buf_queue, bp); 732 733 /* 734 * Tell the device to get going on the transfer if it's 735 * not doing anything, otherwise just wait for completion 736 */ 737 sdstart(sd->sc_periph); 738 739 splx(s); 740 return; 741 742 bad: 743 bp->b_flags |= B_ERROR; 744 done: 745 /* 746 * Correctly set the buf to indicate a completed xfer 747 */ 748 bp->b_resid = bp->b_bcount; 749 biodone(bp); 750 } 751 752 /* 753 * sdstart looks to see if there is a buf waiting for the device 754 * and that the device is not already busy. If both are true, 755 * It dequeues the buf and creates a scsi command to perform the 756 * transfer in the buf. The transfer request will call scsipi_done 757 * on completion, which will in turn call this routine again 758 * so that the next queued transfer is performed. 759 * The bufs are queued by the strategy routine (sdstrategy) 760 * 761 * This routine is also called after other non-queued requests 762 * have been made of the scsi driver, to ensure that the queue 763 * continues to be drained. 764 * 765 * must be called at the correct (highish) spl level 766 * sdstart() is called at splbio from sdstrategy, sdrestart and scsipi_done 767 */ 768 static void 769 sdstart(struct scsipi_periph *periph) 770 { 771 struct sd_softc *sd = (void *)periph->periph_dev; 772 struct disklabel *lp = sd->sc_dk.dk_label; 773 struct buf *bp = 0; 774 struct scsipi_rw_16 cmd16; 775 struct scsipi_rw_10 cmd_big; 776 struct scsi_rw_6 cmd_small; 777 struct scsipi_generic *cmdp; 778 struct scsipi_xfer *xs; 779 int nblks, cmdlen, error, flags; 780 781 SC_DEBUG(periph, SCSIPI_DB2, ("sdstart ")); 782 /* 783 * Check if the device has room for another command 784 */ 785 while (periph->periph_active < periph->periph_openings) { 786 /* 787 * there is excess capacity, but a special waits 788 * It'll need the adapter as soon as we clear out of the 789 * way and let it run (user level wait). 790 */ 791 if (periph->periph_flags & PERIPH_WAITING) { 792 periph->periph_flags &= ~PERIPH_WAITING; 793 wakeup((caddr_t)periph); 794 return; 795 } 796 797 /* 798 * If the device has become invalid, abort all the 799 * reads and writes until all files have been closed and 800 * re-opened 801 */ 802 if (__predict_false( 803 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)) { 804 if ((bp = BUFQ_GET(sd->buf_queue)) != NULL) { 805 bp->b_error = EIO; 806 bp->b_flags |= B_ERROR; 807 bp->b_resid = bp->b_bcount; 808 biodone(bp); 809 continue; 810 } else { 811 return; 812 } 813 } 814 815 /* 816 * See if there is a buf with work for us to do.. 817 */ 818 if ((bp = BUFQ_PEEK(sd->buf_queue)) == NULL) 819 return; 820 821 /* 822 * We have a buf, now we should make a command. 823 */ 824 825 if (lp->d_secsize == DEV_BSIZE) 826 nblks = bp->b_bcount >> DEV_BSHIFT; 827 else 828 nblks = howmany(bp->b_bcount, lp->d_secsize); 829 830 /* 831 * Fill out the scsi command. Use the smallest CDB possible 832 * (6-byte, 10-byte, or 16-byte). 833 */ 834 if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) && 835 ((nblks & 0xff) == nblks) && 836 !(periph->periph_quirks & PQUIRK_ONLYBIG)) { 837 /* 6-byte CDB */ 838 memset(&cmd_small, 0, sizeof(cmd_small)); 839 cmd_small.opcode = (bp->b_flags & B_READ) ? 840 SCSI_READ_6_COMMAND : SCSI_WRITE_6_COMMAND; 841 _lto3b(bp->b_rawblkno, cmd_small.addr); 842 cmd_small.length = nblks & 0xff; 843 cmdlen = sizeof(cmd_small); 844 cmdp = (struct scsipi_generic *)&cmd_small; 845 } else if ((bp->b_rawblkno & 0xffffffff) == bp->b_rawblkno) { 846 /* 10-byte CDB */ 847 memset(&cmd_big, 0, sizeof(cmd_big)); 848 cmd_big.opcode = (bp->b_flags & B_READ) ? 849 READ_10 : WRITE_10; 850 _lto4b(bp->b_rawblkno, cmd_big.addr); 851 _lto2b(nblks, cmd_big.length); 852 cmdlen = sizeof(cmd_big); 853 cmdp = (struct scsipi_generic *)&cmd_big; 854 } else { 855 /* 16-byte CDB */ 856 memset(&cmd16, 0, sizeof(cmd16)); 857 cmd16.opcode = (bp->b_flags & B_READ) ? 858 READ_16 : WRITE_16; 859 _lto8b(bp->b_rawblkno, cmd16.addr); 860 _lto4b(nblks, cmd16.length); 861 cmdlen = sizeof(cmd16); 862 cmdp = (struct scsipi_generic *)&cmd16; 863 } 864 865 /* Instrumentation. */ 866 disk_busy(&sd->sc_dk); 867 868 /* 869 * Mark the disk dirty so that the cache will be 870 * flushed on close. 871 */ 872 if ((bp->b_flags & B_READ) == 0) 873 sd->flags |= SDF_DIRTY; 874 875 /* 876 * Figure out what flags to use. 877 */ 878 flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_SIMPLE_TAG; 879 if (bp->b_flags & B_READ) 880 flags |= XS_CTL_DATA_IN; 881 else 882 flags |= XS_CTL_DATA_OUT; 883 884 /* 885 * Call the routine that chats with the adapter. 886 * Note: we cannot sleep as we may be an interrupt 887 */ 888 xs = scsipi_make_xs(periph, cmdp, cmdlen, 889 (u_char *)bp->b_data, bp->b_bcount, 890 SDRETRIES, SD_IO_TIMEOUT, bp, flags); 891 if (__predict_false(xs == NULL)) { 892 /* 893 * out of memory. Keep this buffer in the queue, and 894 * retry later. 895 */ 896 callout_reset(&sd->sc_callout, hz / 2, sdrestart, 897 periph); 898 return; 899 } 900 /* 901 * need to dequeue the buffer before queuing the command, 902 * because cdstart may be called recursively from the 903 * HBA driver 904 */ 905 #ifdef DIAGNOSTIC 906 if (BUFQ_GET(sd->buf_queue) != bp) 907 panic("sdstart(): dequeued wrong buf"); 908 #else 909 BUFQ_GET(sd->buf_queue); 910 #endif 911 error = scsipi_execute_xs(xs); 912 /* with a scsipi_xfer preallocated, scsipi_command can't fail */ 913 KASSERT(error == 0); 914 } 915 } 916 917 static void 918 sdrestart(void *v) 919 { 920 int s = splbio(); 921 sdstart((struct scsipi_periph *)v); 922 splx(s); 923 } 924 925 static void 926 sddone(struct scsipi_xfer *xs, int error) 927 { 928 struct sd_softc *sd = (void *)xs->xs_periph->periph_dev; 929 struct buf *bp = xs->bp; 930 931 if (sd->flags & SDF_FLUSHING) { 932 /* Flush completed, no longer dirty. */ 933 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 934 } 935 936 if (bp) { 937 bp->b_error = error; 938 bp->b_resid = xs->resid; 939 if (error) { 940 /* on a read/write error bp->b_resid is zero, so fix */ 941 bp->b_resid =bp->b_bcount; 942 bp->b_flags |= B_ERROR; 943 } 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 xmax; 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 xmax = sd->sc_dk.dk_label->d_secsize * 0xff; 976 977 if (bp->b_bcount > xmax) 978 bp->b_bcount = xmax; 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 lwp *l) 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 if (periph->periph_flags & PERIPH_REMOVABLE) 1139 return (scsipi_prevent(periph, 1140 (*(int *)addr) ? 1141 SPAMR_PREVENT_DT : SPAMR_ALLOW, 0)); 1142 else 1143 return (ENOTTY); 1144 1145 case DIOCEJECT: 1146 if ((periph->periph_flags & PERIPH_REMOVABLE) == 0) 1147 return (ENOTTY); 1148 if (*(int *)addr == 0) { 1149 /* 1150 * Don't force eject: check that we are the only 1151 * partition open. If so, unlock it. 1152 */ 1153 if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 && 1154 sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask == 1155 sd->sc_dk.dk_openmask) { 1156 error = scsipi_prevent(periph, SPAMR_ALLOW, 1157 XS_CTL_IGNORE_NOT_READY); 1158 if (error) 1159 return (error); 1160 } else { 1161 return (EBUSY); 1162 } 1163 } 1164 /* FALLTHROUGH */ 1165 case ODIOCEJECT: 1166 return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ? 1167 ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0)); 1168 1169 case DIOCGDEFLABEL: 1170 sdgetdefaultlabel(sd, (struct disklabel *)addr); 1171 return (0); 1172 1173 #ifdef __HAVE_OLD_DISKLABEL 1174 case ODIOCGDEFLABEL: 1175 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK); 1176 if (newlabel == NULL) 1177 return EIO; 1178 sdgetdefaultlabel(sd, newlabel); 1179 if (newlabel->d_npartitions <= OLDMAXPARTITIONS) 1180 memcpy(addr, newlabel, sizeof (struct olddisklabel)); 1181 else 1182 error = ENOTTY; 1183 free(newlabel, M_TEMP); 1184 return error; 1185 #endif 1186 1187 case DIOCGCACHE: 1188 return (sd_getcache(sd, (int *) addr)); 1189 1190 case DIOCSCACHE: 1191 if ((flag & FWRITE) == 0) 1192 return (EBADF); 1193 return (sd_setcache(sd, *(int *) addr)); 1194 1195 case DIOCCACHESYNC: 1196 /* 1197 * XXX Do we really need to care about having a writable 1198 * file descriptor here? 1199 */ 1200 if ((flag & FWRITE) == 0) 1201 return (EBADF); 1202 if (((sd->flags & SDF_DIRTY) != 0 || *(int *)addr != 0)) { 1203 error = sd_flush(sd, 0); 1204 if (error) 1205 sd->flags &= ~SDF_FLUSHING; 1206 else 1207 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 1208 } else 1209 error = 0; 1210 return (error); 1211 1212 case DIOCAWEDGE: 1213 { 1214 struct dkwedge_info *dkw = (void *) addr; 1215 1216 if ((flag & FWRITE) == 0) 1217 return (EBADF); 1218 1219 /* If the ioctl happens here, the parent is us. */ 1220 strcpy(dkw->dkw_parent, sd->sc_dev.dv_xname); 1221 return (dkwedge_add(dkw)); 1222 } 1223 1224 case DIOCDWEDGE: 1225 { 1226 struct dkwedge_info *dkw = (void *) addr; 1227 1228 if ((flag & FWRITE) == 0) 1229 return (EBADF); 1230 1231 /* If the ioctl happens here, the parent is us. */ 1232 strcpy(dkw->dkw_parent, sd->sc_dev.dv_xname); 1233 return (dkwedge_del(dkw)); 1234 } 1235 1236 case DIOCLWEDGES: 1237 { 1238 struct dkwedge_list *dkwl = (void *) addr; 1239 1240 return (dkwedge_list(&sd->sc_dk, dkwl, l)); 1241 } 1242 1243 default: 1244 if (part != RAW_PART) 1245 return (ENOTTY); 1246 return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, l)); 1247 } 1248 1249 #ifdef DIAGNOSTIC 1250 panic("sdioctl: impossible"); 1251 #endif 1252 } 1253 1254 static void 1255 sdgetdefaultlabel(struct sd_softc *sd, struct disklabel *lp) 1256 { 1257 1258 memset(lp, 0, sizeof(struct disklabel)); 1259 1260 lp->d_secsize = sd->params.blksize; 1261 lp->d_ntracks = sd->params.heads; 1262 lp->d_nsectors = sd->params.sectors; 1263 lp->d_ncylinders = sd->params.cyls; 1264 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 1265 1266 switch (scsipi_periph_bustype(sd->sc_periph)) { 1267 case SCSIPI_BUSTYPE_SCSI: 1268 lp->d_type = DTYPE_SCSI; 1269 break; 1270 case SCSIPI_BUSTYPE_ATAPI: 1271 lp->d_type = DTYPE_ATAPI; 1272 break; 1273 } 1274 /* 1275 * XXX 1276 * We could probe the mode pages to figure out what kind of disc it is. 1277 * Is this worthwhile? 1278 */ 1279 strncpy(lp->d_typename, "mydisk", 16); 1280 strncpy(lp->d_packname, "fictitious", 16); 1281 lp->d_secperunit = sd->params.disksize; 1282 lp->d_rpm = sd->params.rot_rate; 1283 lp->d_interleave = 1; 1284 lp->d_flags = sd->sc_periph->periph_flags & PERIPH_REMOVABLE ? 1285 D_REMOVABLE : 0; 1286 1287 lp->d_partitions[RAW_PART].p_offset = 0; 1288 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit; 1289 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; 1290 lp->d_npartitions = RAW_PART + 1; 1291 1292 lp->d_magic = DISKMAGIC; 1293 lp->d_magic2 = DISKMAGIC; 1294 lp->d_checksum = dkcksum(lp); 1295 } 1296 1297 1298 /* 1299 * Load the label information on the named device 1300 */ 1301 static int 1302 sdgetdisklabel(struct sd_softc *sd) 1303 { 1304 struct disklabel *lp = sd->sc_dk.dk_label; 1305 const char *errstring; 1306 1307 memset(sd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); 1308 1309 sdgetdefaultlabel(sd, lp); 1310 1311 if (lp->d_secpercyl == 0) { 1312 lp->d_secpercyl = 100; 1313 /* as long as it's not 0 - readdisklabel divides by it (?) */ 1314 } 1315 1316 /* 1317 * Call the generic disklabel extraction routine 1318 */ 1319 errstring = readdisklabel(MAKESDDEV(0, device_unit(&sd->sc_dev), 1320 RAW_PART), sdstrategy, lp, sd->sc_dk.dk_cpulabel); 1321 if (errstring) { 1322 printf("%s: %s\n", sd->sc_dev.dv_xname, errstring); 1323 return EIO; 1324 } 1325 return 0; 1326 } 1327 1328 static void 1329 sd_shutdown(void *arg) 1330 { 1331 struct sd_softc *sd = arg; 1332 1333 /* 1334 * If the disk cache needs to be flushed, and the disk supports 1335 * it, flush it. We're cold at this point, so we poll for 1336 * completion. 1337 */ 1338 if ((sd->flags & SDF_DIRTY) != 0) { 1339 if (sd_flush(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) { 1340 printf("%s: cache synchronization failed\n", 1341 sd->sc_dev.dv_xname); 1342 sd->flags &= ~SDF_FLUSHING; 1343 } else 1344 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 1345 } 1346 } 1347 1348 /* 1349 * Check Errors 1350 */ 1351 static int 1352 sd_interpret_sense(struct scsipi_xfer *xs) 1353 { 1354 struct scsipi_periph *periph = xs->xs_periph; 1355 struct scsi_sense_data *sense = &xs->sense.scsi_sense; 1356 struct sd_softc *sd = (void *)periph->periph_dev; 1357 int s, error, retval = EJUSTRETURN; 1358 1359 /* 1360 * If the periph is already recovering, just do the normal 1361 * error processing. 1362 */ 1363 if (periph->periph_flags & PERIPH_RECOVERING) 1364 return (retval); 1365 1366 /* 1367 * Ignore errors from accessing illegal fields (e.g. trying to 1368 * lock the door of a digicam, which doesn't have a door that 1369 * can be locked) for the SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL command. 1370 */ 1371 if (xs->cmd->opcode == SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL && 1372 SSD_SENSE_KEY(sense->flags) == SKEY_ILLEGAL_REQUEST && 1373 sense->asc == 0x24 && 1374 sense->ascq == 0x00) { /* Illegal field in CDB */ 1375 if (!(xs->xs_control & XS_CTL_SILENT)) { 1376 scsipi_printaddr(periph); 1377 printf("no door lock\n"); 1378 } 1379 xs->xs_control |= XS_CTL_IGNORE_ILLEGAL_REQUEST; 1380 return (retval); 1381 } 1382 1383 1384 1385 /* 1386 * If the device is not open yet, let the generic code handle it. 1387 */ 1388 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) 1389 return (retval); 1390 1391 /* 1392 * If it isn't a extended or extended/deferred error, let 1393 * the generic code handle it. 1394 */ 1395 if (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT && 1396 SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED) 1397 return (retval); 1398 1399 if (SSD_SENSE_KEY(sense->flags) == SKEY_NOT_READY && 1400 sense->asc == 0x4) { 1401 if (sense->ascq == 0x01) { 1402 /* 1403 * Unit In The Process Of Becoming Ready. 1404 */ 1405 printf("%s: waiting for pack to spin up...\n", 1406 sd->sc_dev.dv_xname); 1407 if (!callout_pending(&periph->periph_callout)) 1408 scsipi_periph_freeze(periph, 1); 1409 callout_reset(&periph->periph_callout, 1410 5 * hz, scsipi_periph_timed_thaw, periph); 1411 retval = ERESTART; 1412 } else if (sense->ascq == 0x02) { 1413 printf("%s: pack is stopped, restarting...\n", 1414 sd->sc_dev.dv_xname); 1415 s = splbio(); 1416 periph->periph_flags |= PERIPH_RECOVERING; 1417 splx(s); 1418 error = scsipi_start(periph, SSS_START, 1419 XS_CTL_URGENT|XS_CTL_HEAD_TAG| 1420 XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH); 1421 if (error) { 1422 printf("%s: unable to restart pack\n", 1423 sd->sc_dev.dv_xname); 1424 retval = error; 1425 } else 1426 retval = ERESTART; 1427 s = splbio(); 1428 periph->periph_flags &= ~PERIPH_RECOVERING; 1429 splx(s); 1430 } 1431 } 1432 if (SSD_SENSE_KEY(sense->flags) == SKEY_MEDIUM_ERROR && 1433 sense->asc == 0x31 && 1434 sense->ascq == 0x00) { /* maybe for any asq ? */ 1435 /* Medium Format Corrupted */ 1436 retval = EFTYPE; 1437 } 1438 return (retval); 1439 } 1440 1441 1442 static int 1443 sdsize(dev_t dev) 1444 { 1445 struct sd_softc *sd; 1446 int part, unit, omask; 1447 int size; 1448 1449 unit = SDUNIT(dev); 1450 if (unit >= sd_cd.cd_ndevs) 1451 return (-1); 1452 sd = sd_cd.cd_devs[unit]; 1453 if (sd == NULL) 1454 return (-1); 1455 1456 if (!device_is_active(&sd->sc_dev)) 1457 return (-1); 1458 1459 part = SDPART(dev); 1460 omask = sd->sc_dk.dk_openmask & (1 << part); 1461 1462 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0) 1463 return (-1); 1464 if ((sd->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) 1465 size = -1; 1466 else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP) 1467 size = -1; 1468 else 1469 size = sd->sc_dk.dk_label->d_partitions[part].p_size * 1470 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE); 1471 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0) 1472 return (-1); 1473 return (size); 1474 } 1475 1476 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */ 1477 static struct scsipi_xfer sx; 1478 static int sddoingadump; 1479 1480 /* 1481 * dump all of physical memory into the partition specified, starting 1482 * at offset 'dumplo' into the partition. 1483 */ 1484 static int 1485 sddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size) 1486 { 1487 struct sd_softc *sd; /* disk unit to do the I/O */ 1488 struct disklabel *lp; /* disk's disklabel */ 1489 int unit, part; 1490 int sectorsize; /* size of a disk sector */ 1491 int nsects; /* number of sectors in partition */ 1492 int sectoff; /* sector offset of partition */ 1493 int totwrt; /* total number of sectors left to write */ 1494 int nwrt; /* current number of sectors to write */ 1495 struct scsipi_rw_10 cmd; /* write command */ 1496 struct scsipi_xfer *xs; /* ... convenience */ 1497 struct scsipi_periph *periph; 1498 struct scsipi_channel *chan; 1499 1500 /* Check if recursive dump; if so, punt. */ 1501 if (sddoingadump) 1502 return (EFAULT); 1503 1504 /* Mark as active early. */ 1505 sddoingadump = 1; 1506 1507 unit = SDUNIT(dev); /* Decompose unit & partition. */ 1508 part = SDPART(dev); 1509 1510 /* Check for acceptable drive number. */ 1511 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL) 1512 return (ENXIO); 1513 1514 if (!device_is_active(&sd->sc_dev)) 1515 return (ENODEV); 1516 1517 periph = sd->sc_periph; 1518 chan = periph->periph_channel; 1519 1520 /* Make sure it was initialized. */ 1521 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) 1522 return (ENXIO); 1523 1524 /* Convert to disk sectors. Request must be a multiple of size. */ 1525 lp = sd->sc_dk.dk_label; 1526 sectorsize = lp->d_secsize; 1527 if ((size % sectorsize) != 0) 1528 return (EFAULT); 1529 totwrt = size / sectorsize; 1530 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */ 1531 1532 nsects = lp->d_partitions[part].p_size; 1533 sectoff = lp->d_partitions[part].p_offset; 1534 1535 /* Check transfer bounds against partition size. */ 1536 if ((blkno < 0) || ((blkno + totwrt) > nsects)) 1537 return (EINVAL); 1538 1539 /* Offset block number to start of partition. */ 1540 blkno += sectoff; 1541 1542 xs = &sx; 1543 1544 while (totwrt > 0) { 1545 nwrt = totwrt; /* XXX */ 1546 #ifndef SD_DUMP_NOT_TRUSTED 1547 /* 1548 * Fill out the scsi command 1549 */ 1550 memset(&cmd, 0, sizeof(cmd)); 1551 cmd.opcode = WRITE_10; 1552 _lto4b(blkno, cmd.addr); 1553 _lto2b(nwrt, cmd.length); 1554 /* 1555 * Fill out the scsipi_xfer structure 1556 * Note: we cannot sleep as we may be an interrupt 1557 * don't use scsipi_command() as it may want to wait 1558 * for an xs. 1559 */ 1560 memset(xs, 0, sizeof(sx)); 1561 xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL | 1562 XS_CTL_DATA_OUT; 1563 xs->xs_status = 0; 1564 xs->xs_periph = periph; 1565 xs->xs_retries = SDRETRIES; 1566 xs->timeout = 10000; /* 10000 millisecs for a disk ! */ 1567 xs->cmd = (struct scsipi_generic *)&cmd; 1568 xs->cmdlen = sizeof(cmd); 1569 xs->resid = nwrt * sectorsize; 1570 xs->error = XS_NOERROR; 1571 xs->bp = 0; 1572 xs->data = va; 1573 xs->datalen = nwrt * sectorsize; 1574 1575 /* 1576 * Pass all this info to the scsi driver. 1577 */ 1578 scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs); 1579 if ((xs->xs_status & XS_STS_DONE) == 0 || 1580 xs->error != XS_NOERROR) 1581 return (EIO); 1582 #else /* SD_DUMP_NOT_TRUSTED */ 1583 /* Let's just talk about this first... */ 1584 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno); 1585 delay(500 * 1000); /* half a second */ 1586 #endif /* SD_DUMP_NOT_TRUSTED */ 1587 1588 /* update block count */ 1589 totwrt -= nwrt; 1590 blkno += nwrt; 1591 va += sectorsize * nwrt; 1592 } 1593 sddoingadump = 0; 1594 return (0); 1595 } 1596 1597 static int 1598 sd_mode_sense(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size, 1599 int page, int flags, int *big) 1600 { 1601 1602 if ((sd->sc_periph->periph_quirks & PQUIRK_ONLYBIG) && 1603 !(sd->sc_periph->periph_quirks & PQUIRK_NOBIGMODESENSE)) { 1604 *big = 1; 1605 return scsipi_mode_sense_big(sd->sc_periph, byte2, page, sense, 1606 size + sizeof(struct scsi_mode_parameter_header_10), 1607 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000); 1608 } else { 1609 *big = 0; 1610 return scsipi_mode_sense(sd->sc_periph, byte2, page, sense, 1611 size + sizeof(struct scsi_mode_parameter_header_6), 1612 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000); 1613 } 1614 } 1615 1616 static int 1617 sd_mode_select(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size, 1618 int flags, int big) 1619 { 1620 1621 if (big) { 1622 struct scsi_mode_parameter_header_10 *header = sense; 1623 1624 _lto2b(0, header->data_length); 1625 return scsipi_mode_select_big(sd->sc_periph, byte2, sense, 1626 size + sizeof(struct scsi_mode_parameter_header_10), 1627 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000); 1628 } else { 1629 struct scsi_mode_parameter_header_6 *header = sense; 1630 1631 header->data_length = 0; 1632 return scsipi_mode_select(sd->sc_periph, byte2, sense, 1633 size + sizeof(struct scsi_mode_parameter_header_6), 1634 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000); 1635 } 1636 } 1637 1638 static int 1639 sd_get_simplifiedparms(struct sd_softc *sd, struct disk_parms *dp, int flags) 1640 { 1641 struct { 1642 struct scsi_mode_parameter_header_6 header; 1643 /* no block descriptor */ 1644 u_int8_t pg_code; /* page code (should be 6) */ 1645 u_int8_t pg_length; /* page length (should be 11) */ 1646 u_int8_t wcd; /* bit0: cache disable */ 1647 u_int8_t lbs[2]; /* logical block size */ 1648 u_int8_t size[5]; /* number of log. blocks */ 1649 u_int8_t pp; /* power/performance */ 1650 u_int8_t flags; 1651 u_int8_t resvd; 1652 } scsipi_sense; 1653 u_int64_t sectors; 1654 int error, blksize; 1655 1656 /* 1657 * scsipi_size (ie "read capacity") and mode sense page 6 1658 * give the same information. Do both for now, and check 1659 * for consistency. 1660 * XXX probably differs for removable media 1661 */ 1662 dp->blksize = 512; 1663 if ((sectors = scsipi_size(sd->sc_periph, &blksize, 512, flags)) == 0) 1664 return (SDGP_RESULT_OFFLINE); /* XXX? */ 1665 1666 error = scsipi_mode_sense(sd->sc_periph, SMS_DBD, 6, 1667 &scsipi_sense.header, sizeof(scsipi_sense), 1668 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000); 1669 1670 if (error != 0) 1671 return (SDGP_RESULT_OFFLINE); /* XXX? */ 1672 1673 dp->blksize = _2btol(scsipi_sense.lbs); 1674 if (dp->blksize == 0) 1675 dp->blksize = blksize; 1676 1677 /* 1678 * Create a pseudo-geometry. 1679 */ 1680 dp->heads = 64; 1681 dp->sectors = 32; 1682 dp->cyls = sectors / (dp->heads * dp->sectors); 1683 dp->disksize = _5btol(scsipi_sense.size); 1684 if (dp->disksize <= UINT32_MAX && dp->disksize != sectors) { 1685 printf("RBC size: mode sense=%llu, get cap=%llu\n", 1686 (unsigned long long)dp->disksize, 1687 (unsigned long long)sectors); 1688 dp->disksize = sectors; 1689 } 1690 dp->disksize512 = (dp->disksize * dp->blksize) / DEV_BSIZE; 1691 1692 return (SDGP_RESULT_OK); 1693 } 1694 1695 /* 1696 * Get the scsi driver to send a full inquiry to the * device and use the 1697 * results to fill out the disk parameter structure. 1698 */ 1699 static int 1700 sd_get_capacity(struct sd_softc *sd, struct disk_parms *dp, int flags) 1701 { 1702 u_int64_t sectors; 1703 int error, blksize; 1704 #if 0 1705 int i; 1706 u_int8_t *p; 1707 #endif 1708 1709 dp->disksize = sectors = scsipi_size(sd->sc_periph, &blksize, 512, 1710 flags); 1711 if (sectors == 0) { 1712 struct scsipi_read_format_capacities cmd; 1713 struct { 1714 struct scsipi_capacity_list_header header; 1715 struct scsipi_capacity_descriptor desc; 1716 } __attribute__((packed)) data; 1717 1718 memset(&cmd, 0, sizeof(cmd)); 1719 memset(&data, 0, sizeof(data)); 1720 cmd.opcode = READ_FORMAT_CAPACITIES; 1721 _lto2b(sizeof(data), cmd.length); 1722 1723 error = scsipi_command(sd->sc_periph, 1724 (void *)&cmd, sizeof(cmd), (void *)&data, sizeof(data), 1725 SDRETRIES, 20000, NULL, 1726 flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK); 1727 if (error == EFTYPE) { 1728 /* Medium Format Corrupted, handle as not formatted */ 1729 return (SDGP_RESULT_UNFORMATTED); 1730 } 1731 if (error || data.header.length == 0) 1732 return (SDGP_RESULT_OFFLINE); 1733 1734 #if 0 1735 printf("rfc: length=%d\n", data.header.length); 1736 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"); 1737 #endif 1738 switch (data.desc.byte5 & SCSIPI_CAP_DESC_CODE_MASK) { 1739 case SCSIPI_CAP_DESC_CODE_RESERVED: 1740 case SCSIPI_CAP_DESC_CODE_FORMATTED: 1741 break; 1742 1743 case SCSIPI_CAP_DESC_CODE_UNFORMATTED: 1744 return (SDGP_RESULT_UNFORMATTED); 1745 1746 case SCSIPI_CAP_DESC_CODE_NONE: 1747 return (SDGP_RESULT_OFFLINE); 1748 } 1749 1750 dp->disksize = sectors = _4btol(data.desc.nblks); 1751 if (sectors == 0) 1752 return (SDGP_RESULT_OFFLINE); /* XXX? */ 1753 1754 dp->blksize = _3btol(data.desc.blklen); 1755 if (dp->blksize == 0) 1756 dp->blksize = 512; 1757 } else { 1758 struct sd_mode_sense_data scsipi_sense; 1759 int big, bsize; 1760 struct scsi_general_block_descriptor *bdesc; 1761 1762 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 1763 error = sd_mode_sense(sd, 0, &scsipi_sense, 1764 sizeof(scsipi_sense.blk_desc), 0, flags | XS_CTL_SILENT, &big); 1765 dp->blksize = blksize; 1766 if (!error) { 1767 if (big) { 1768 bdesc = (void *)(&scsipi_sense.header.big + 1); 1769 bsize = _2btol(scsipi_sense.header.big.blk_desc_len); 1770 } else { 1771 bdesc = (void *)(&scsipi_sense.header.small + 1); 1772 bsize = scsipi_sense.header.small.blk_desc_len; 1773 } 1774 1775 #if 0 1776 printf("page 0 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n"); 1777 printf("page 0 bsize=%d\n", bsize); 1778 printf("page 0 ok\n"); 1779 #endif 1780 1781 if (bsize >= 8) { 1782 dp->blksize = _3btol(bdesc->blklen); 1783 if (dp->blksize == 0) 1784 dp->blksize = blksize; 1785 } 1786 } 1787 } 1788 1789 dp->disksize512 = (sectors * dp->blksize) / DEV_BSIZE; 1790 return (0); 1791 } 1792 1793 static int 1794 sd_get_parms_page4(struct sd_softc *sd, struct disk_parms *dp, int flags) 1795 { 1796 struct sd_mode_sense_data scsipi_sense; 1797 int error; 1798 int big, poffset, byte2; 1799 union scsi_disk_pages *pages; 1800 #if 0 1801 int i; 1802 u_int8_t *p; 1803 #endif 1804 1805 byte2 = SMS_DBD; 1806 again: 1807 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 1808 error = sd_mode_sense(sd, byte2, &scsipi_sense, 1809 (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) + 1810 sizeof(scsipi_sense.pages.rigid_geometry), 4, 1811 flags | XS_CTL_SILENT, &big); 1812 if (error) { 1813 if (byte2 == SMS_DBD) { 1814 /* No result; try once more with DBD off */ 1815 byte2 = 0; 1816 goto again; 1817 } 1818 return (error); 1819 } 1820 1821 if (big) { 1822 poffset = sizeof scsipi_sense.header.big; 1823 poffset += _2btol(scsipi_sense.header.big.blk_desc_len); 1824 } else { 1825 poffset = sizeof scsipi_sense.header.small; 1826 poffset += scsipi_sense.header.small.blk_desc_len; 1827 } 1828 1829 pages = (void *)((u_long)&scsipi_sense + poffset); 1830 #if 0 1831 printf("page 4 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n"); 1832 printf("page 4 pg_code=%d sense=%p/%p\n", pages->rigid_geometry.pg_code, &scsipi_sense, pages); 1833 #endif 1834 1835 if ((pages->rigid_geometry.pg_code & PGCODE_MASK) != 4) 1836 return (ERESTART); 1837 1838 SC_DEBUG(sd->sc_periph, SCSIPI_DB3, 1839 ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n", 1840 _3btol(pages->rigid_geometry.ncyl), 1841 pages->rigid_geometry.nheads, 1842 _2btol(pages->rigid_geometry.st_cyl_wp), 1843 _2btol(pages->rigid_geometry.st_cyl_rwc), 1844 _2btol(pages->rigid_geometry.land_zone))); 1845 1846 /* 1847 * KLUDGE!! (for zone recorded disks) 1848 * give a number of sectors so that sec * trks * cyls 1849 * is <= disk_size 1850 * can lead to wasted space! THINK ABOUT THIS ! 1851 */ 1852 dp->heads = pages->rigid_geometry.nheads; 1853 dp->cyls = _3btol(pages->rigid_geometry.ncyl); 1854 if (dp->heads == 0 || dp->cyls == 0) 1855 return (ERESTART); 1856 dp->sectors = dp->disksize / (dp->heads * dp->cyls); /* XXX */ 1857 1858 dp->rot_rate = _2btol(pages->rigid_geometry.rpm); 1859 if (dp->rot_rate == 0) 1860 dp->rot_rate = 3600; 1861 1862 #if 0 1863 printf("page 4 ok\n"); 1864 #endif 1865 return (0); 1866 } 1867 1868 static int 1869 sd_get_parms_page5(struct sd_softc *sd, struct disk_parms *dp, int flags) 1870 { 1871 struct sd_mode_sense_data scsipi_sense; 1872 int error; 1873 int big, poffset, byte2; 1874 union scsi_disk_pages *pages; 1875 #if 0 1876 int i; 1877 u_int8_t *p; 1878 #endif 1879 1880 byte2 = SMS_DBD; 1881 again: 1882 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 1883 error = sd_mode_sense(sd, 0, &scsipi_sense, 1884 (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) + 1885 sizeof(scsipi_sense.pages.flex_geometry), 5, 1886 flags | XS_CTL_SILENT, &big); 1887 if (error) { 1888 if (byte2 == SMS_DBD) { 1889 /* No result; try once more with DBD off */ 1890 byte2 = 0; 1891 goto again; 1892 } 1893 return (error); 1894 } 1895 1896 if (big) { 1897 poffset = sizeof scsipi_sense.header.big; 1898 poffset += _2btol(scsipi_sense.header.big.blk_desc_len); 1899 } else { 1900 poffset = sizeof scsipi_sense.header.small; 1901 poffset += scsipi_sense.header.small.blk_desc_len; 1902 } 1903 1904 pages = (void *)((u_long)&scsipi_sense + poffset); 1905 #if 0 1906 printf("page 5 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n"); 1907 printf("page 5 pg_code=%d sense=%p/%p\n", pages->flex_geometry.pg_code, &scsipi_sense, pages); 1908 #endif 1909 1910 if ((pages->flex_geometry.pg_code & PGCODE_MASK) != 5) 1911 return (ERESTART); 1912 1913 SC_DEBUG(sd->sc_periph, SCSIPI_DB3, 1914 ("%d cyls, %d heads, %d sec, %d bytes/sec\n", 1915 _3btol(pages->flex_geometry.ncyl), 1916 pages->flex_geometry.nheads, 1917 pages->flex_geometry.ph_sec_tr, 1918 _2btol(pages->flex_geometry.bytes_s))); 1919 1920 dp->heads = pages->flex_geometry.nheads; 1921 dp->cyls = _2btol(pages->flex_geometry.ncyl); 1922 dp->sectors = pages->flex_geometry.ph_sec_tr; 1923 if (dp->heads == 0 || dp->cyls == 0 || dp->sectors == 0) 1924 return (ERESTART); 1925 1926 dp->rot_rate = _2btol(pages->rigid_geometry.rpm); 1927 if (dp->rot_rate == 0) 1928 dp->rot_rate = 3600; 1929 1930 #if 0 1931 printf("page 5 ok\n"); 1932 #endif 1933 return (0); 1934 } 1935 1936 static int 1937 sd_get_parms(struct sd_softc *sd, struct disk_parms *dp, int flags) 1938 { 1939 int error; 1940 1941 /* 1942 * If offline, the SDEV_MEDIA_LOADED flag will be 1943 * cleared by the caller if necessary. 1944 */ 1945 if (sd->type == T_SIMPLE_DIRECT) { 1946 error = sd_get_simplifiedparms(sd, dp, flags); 1947 if (!error) 1948 disk_blocksize(&sd->sc_dk, dp->blksize); 1949 return (error); 1950 } 1951 1952 error = sd_get_capacity(sd, dp, flags); 1953 if (error) 1954 return (error); 1955 1956 disk_blocksize(&sd->sc_dk, dp->blksize); 1957 1958 if (sd->type == T_OPTICAL) 1959 goto page0; 1960 1961 if (sd->sc_periph->periph_flags & PERIPH_REMOVABLE) { 1962 if (!sd_get_parms_page5(sd, dp, flags) || 1963 !sd_get_parms_page4(sd, dp, flags)) 1964 return (SDGP_RESULT_OK); 1965 } else { 1966 if (!sd_get_parms_page4(sd, dp, flags) || 1967 !sd_get_parms_page5(sd, dp, flags)) 1968 return (SDGP_RESULT_OK); 1969 } 1970 1971 page0: 1972 printf("%s: fabricating a geometry\n", sd->sc_dev.dv_xname); 1973 /* Try calling driver's method for figuring out geometry. */ 1974 if (!sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom || 1975 !(*sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom) 1976 (sd->sc_periph, dp, dp->disksize)) { 1977 /* 1978 * Use adaptec standard fictitious geometry 1979 * this depends on which controller (e.g. 1542C is 1980 * different. but we have to put SOMETHING here..) 1981 */ 1982 dp->heads = 64; 1983 dp->sectors = 32; 1984 dp->cyls = dp->disksize / (64 * 32); 1985 } 1986 dp->rot_rate = 3600; 1987 return (SDGP_RESULT_OK); 1988 } 1989 1990 static int 1991 sd_flush(struct sd_softc *sd, int flags) 1992 { 1993 struct scsipi_periph *periph = sd->sc_periph; 1994 struct scsi_synchronize_cache_10 cmd; 1995 1996 /* 1997 * If the device is SCSI-2, issue a SYNCHRONIZE CACHE. 1998 * We issue with address 0 length 0, which should be 1999 * interpreted by the device as "all remaining blocks 2000 * starting at address 0". We ignore ILLEGAL REQUEST 2001 * in the event that the command is not supported by 2002 * the device, and poll for completion so that we know 2003 * that the cache has actually been flushed. 2004 * 2005 * Unless, that is, the device can't handle the SYNCHRONIZE CACHE 2006 * command, as indicated by our quirks flags. 2007 * 2008 * XXX What about older devices? 2009 */ 2010 if (periph->periph_version < 2 || 2011 (periph->periph_quirks & PQUIRK_NOSYNCCACHE)) 2012 return (0); 2013 2014 sd->flags |= SDF_FLUSHING; 2015 memset(&cmd, 0, sizeof(cmd)); 2016 cmd.opcode = SCSI_SYNCHRONIZE_CACHE_10; 2017 2018 return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0, 2019 SDRETRIES, 100000, NULL, flags | XS_CTL_IGNORE_ILLEGAL_REQUEST)); 2020 } 2021 2022 static int 2023 sd_getcache(struct sd_softc *sd, int *bitsp) 2024 { 2025 struct scsipi_periph *periph = sd->sc_periph; 2026 struct sd_mode_sense_data scsipi_sense; 2027 int error, bits = 0; 2028 int big; 2029 union scsi_disk_pages *pages; 2030 2031 if (periph->periph_version < 2) 2032 return (EOPNOTSUPP); 2033 2034 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 2035 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense, 2036 sizeof(scsipi_sense.pages.caching_params), 8, 0, &big); 2037 if (error) 2038 return (error); 2039 2040 if (big) 2041 pages = (void *)(&scsipi_sense.header.big + 1); 2042 else 2043 pages = (void *)(&scsipi_sense.header.small + 1); 2044 2045 if ((pages->caching_params.flags & CACHING_RCD) == 0) 2046 bits |= DKCACHE_READ; 2047 if (pages->caching_params.flags & CACHING_WCE) 2048 bits |= DKCACHE_WRITE; 2049 if (pages->caching_params.pg_code & PGCODE_PS) 2050 bits |= DKCACHE_SAVE; 2051 2052 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 2053 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense, 2054 sizeof(scsipi_sense.pages.caching_params), 2055 SMS_PCTRL_CHANGEABLE|8, 0, &big); 2056 if (error == 0) { 2057 if (big) 2058 pages = (void *)(&scsipi_sense.header.big + 1); 2059 else 2060 pages = (void *)(&scsipi_sense.header.small + 1); 2061 2062 if (pages->caching_params.flags & CACHING_RCD) 2063 bits |= DKCACHE_RCHANGE; 2064 if (pages->caching_params.flags & CACHING_WCE) 2065 bits |= DKCACHE_WCHANGE; 2066 } 2067 2068 *bitsp = bits; 2069 2070 return (0); 2071 } 2072 2073 static int 2074 sd_setcache(struct sd_softc *sd, int bits) 2075 { 2076 struct scsipi_periph *periph = sd->sc_periph; 2077 struct sd_mode_sense_data scsipi_sense; 2078 int error; 2079 uint8_t oflags, byte2 = 0; 2080 int big; 2081 union scsi_disk_pages *pages; 2082 2083 if (periph->periph_version < 2) 2084 return (EOPNOTSUPP); 2085 2086 memset(&scsipi_sense, 0, sizeof(scsipi_sense)); 2087 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense, 2088 sizeof(scsipi_sense.pages.caching_params), 8, 0, &big); 2089 if (error) 2090 return (error); 2091 2092 if (big) 2093 pages = (void *)(&scsipi_sense.header.big + 1); 2094 else 2095 pages = (void *)(&scsipi_sense.header.small + 1); 2096 2097 oflags = pages->caching_params.flags; 2098 2099 if (bits & DKCACHE_READ) 2100 pages->caching_params.flags &= ~CACHING_RCD; 2101 else 2102 pages->caching_params.flags |= CACHING_RCD; 2103 2104 if (bits & DKCACHE_WRITE) 2105 pages->caching_params.flags |= CACHING_WCE; 2106 else 2107 pages->caching_params.flags &= ~CACHING_WCE; 2108 2109 if (oflags == pages->caching_params.flags) 2110 return (0); 2111 2112 pages->caching_params.pg_code &= PGCODE_MASK; 2113 2114 if (bits & DKCACHE_SAVE) 2115 byte2 |= SMS_SP; 2116 2117 return (sd_mode_select(sd, byte2|SMS_PF, &scsipi_sense, 2118 sizeof(struct scsi_mode_page_header) + 2119 pages->caching_params.pg_length, 0, big)); 2120 } 2121