1 /* $OpenBSD: sd.c,v 1.49 2001/08/06 20:50:25 miod Exp $ */ 2 /* $NetBSD: sd.c,v 1.111 1997/04/02 02:29:41 mycroft Exp $ */ 3 4 /*- 5 * Copyright (c) 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Charles M. Hannum. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Originally written by Julian Elischer (julian@dialix.oz.au) 42 * for TRW Financial Systems for use under the MACH(2.5) operating system. 43 * 44 * TRW Financial Systems, in accordance with their agreement with Carnegie 45 * Mellon University, makes this software available to CMU to distribute 46 * or use in any manner that they see fit as long as this message is kept with 47 * the software. For this reason TFS also grants any other persons or 48 * organisations permission to use or modify this software. 49 * 50 * TFS supplies this software to be publicly redistributed 51 * on the understanding that TFS is not responsible for the correct 52 * functioning of this software in any circumstances. 53 * 54 * Ported to run under 386BSD by Julian Elischer (julian@dialix.oz.au) Sept 1992 55 */ 56 57 #include <sys/types.h> 58 #include <sys/param.h> 59 #include <sys/systm.h> 60 #include <sys/kernel.h> 61 #include <sys/file.h> 62 #include <sys/stat.h> 63 #include <sys/ioctl.h> 64 #include <sys/mtio.h> 65 #include <sys/buf.h> 66 #include <sys/uio.h> 67 #include <sys/malloc.h> 68 #include <sys/errno.h> 69 #include <sys/device.h> 70 #include <sys/disklabel.h> 71 #include <sys/disk.h> 72 #include <sys/proc.h> 73 #include <sys/conf.h> 74 #include <sys/scsiio.h> 75 76 #include <scsi/scsi_all.h> 77 #include <scsi/scsi_disk.h> 78 #include <scsi/scsiconf.h> 79 #include <scsi/sdvar.h> 80 81 #include <ufs/ffs/fs.h> /* for BBSIZE and SBSIZE */ 82 83 #include <sys/vnode.h> 84 85 #define SDUNIT(dev) DISKUNIT(dev) 86 #define SDMINOR(unit, part) DISKMINOR(unit, part) 87 #define SDPART(dev) DISKPART(dev) 88 #define MAKESDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part) 89 90 #define SDLABELDEV(dev) (MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART)) 91 92 int sdmatch __P((struct device *, void *, void *)); 93 void sdattach __P((struct device *, struct device *, void *)); 94 int sdactivate __P((struct device *, enum devact)); 95 int sddetach __P((struct device *, int)); 96 void sdzeroref __P((struct device *)); 97 98 void sdminphys __P((struct buf *)); 99 void sdgetdisklabel __P((dev_t, struct sd_softc *, struct disklabel *, 100 struct cpu_disklabel *, int)); 101 void sdstart __P((void *)); 102 void sddone __P((struct scsi_xfer *)); 103 void sd_shutdown __P((void *)); 104 int sd_reassign_blocks __P((struct sd_softc *, u_long)); 105 int sd_interpret_sense __P((struct scsi_xfer *)); 106 107 void viscpy __P((u_char *, u_char *, int)); 108 109 struct cfattach sd_ca = { 110 sizeof(struct sd_softc), sdmatch, sdattach, 111 sddetach, sdactivate, sdzeroref 112 }; 113 114 struct cfdriver sd_cd = { 115 NULL, "sd", DV_DISK 116 }; 117 118 struct dkdriver sddkdriver = { sdstrategy }; 119 120 struct scsi_device sd_switch = { 121 sd_interpret_sense, /* check out error handler first */ 122 sdstart, /* have a queue, served by this */ 123 NULL, /* have no async handler */ 124 sddone, /* deal with stats at interrupt time */ 125 }; 126 127 struct scsi_inquiry_pattern sd_patterns[] = { 128 {T_DIRECT, T_FIXED, 129 "", "", ""}, 130 {T_DIRECT, T_REMOV, 131 "", "", ""}, 132 {T_OPTICAL, T_FIXED, 133 "", "", ""}, 134 {T_OPTICAL, T_REMOV, 135 "", "", ""}, 136 }; 137 138 extern struct sd_ops sd_scsibus_ops; 139 extern struct sd_ops sd_atapibus_ops; 140 141 #define sdlock(softc) disk_lock(&(softc)->sc_dk) 142 #define sdunlock(softc) disk_unlock(&(softc)->sc_dk) 143 #define sdlookup(unit) (struct sd_softc *)device_lookup(&sd_cd, (unit)) 144 145 int 146 sdmatch(parent, match, aux) 147 struct device *parent; 148 void *match, *aux; 149 { 150 struct scsibus_attach_args *sa = aux; 151 int priority; 152 153 (void)scsi_inqmatch(sa->sa_inqbuf, 154 (caddr_t)sd_patterns, sizeof(sd_patterns)/sizeof(sd_patterns[0]), 155 sizeof(sd_patterns[0]), &priority); 156 return (priority); 157 } 158 159 /* 160 * The routine called by the low level scsi routine when it discovers 161 * a device suitable for this driver. 162 */ 163 void 164 sdattach(parent, self, aux) 165 struct device *parent, *self; 166 void *aux; 167 { 168 int error, result; 169 struct sd_softc *sd = (void *)self; 170 struct disk_parms *dp = &sd->params; 171 struct scsibus_attach_args *sa = aux; 172 struct scsi_link *sc_link = sa->sa_sc_link; 173 174 SC_DEBUG(sc_link, SDEV_DB2, ("sdattach: ")); 175 176 /* 177 * Store information needed to contact our base driver 178 */ 179 sd->sc_link = sc_link; 180 sd->type = (sa->sa_inqbuf->device & SID_TYPE); 181 sc_link->device = &sd_switch; 182 sc_link->device_softc = sd; 183 184 /* 185 * Initialize and attach the disk structure. 186 */ 187 sd->sc_dk.dk_driver = &sddkdriver; 188 sd->sc_dk.dk_name = sd->sc_dev.dv_xname; 189 disk_attach(&sd->sc_dk); 190 191 dk_establish(&sd->sc_dk, &sd->sc_dev); 192 193 if (sc_link->flags & SDEV_ATAPI && 194 (sc_link->flags & SDEV_REMOVABLE)) { 195 sd->sc_ops = &sd_atapibus_ops; 196 } else { 197 sd->sc_ops = &sd_scsibus_ops; 198 } 199 200 /* 201 * Note if this device is ancient. This is used in sdminphys(). 202 */ 203 if (!(sc_link->flags & SDEV_ATAPI) && 204 (sa->sa_inqbuf->version & SID_ANSII) == 0) 205 sd->flags |= SDF_ANCIENT; 206 207 /* 208 * Use the subdriver to request information regarding 209 * the drive. We cannot use interrupts yet, so the 210 * request must specify this. 211 */ 212 printf("\n"); 213 214 if ((sd->sc_link->quirks & SDEV_NOSTARTUNIT) == 0) { 215 error = scsi_start(sd->sc_link, SSS_START, 216 scsi_autoconf | SCSI_IGNORE_ILLEGAL_REQUEST | 217 SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT); 218 } else 219 error = 0; 220 221 /* Fill in name struct for spoofed label */ 222 viscpy(sd->name.vendor, sa->sa_inqbuf->vendor, 8); 223 viscpy(sd->name.product, sa->sa_inqbuf->product, 16); 224 viscpy(sd->name.revision, sa->sa_inqbuf->revision, 4); 225 226 if (error) 227 result = SDGP_RESULT_OFFLINE; 228 else 229 result = (*sd->sc_ops->sdo_get_parms)(sd, &sd->params, 230 scsi_autoconf); 231 232 printf("%s: ", sd->sc_dev.dv_xname); 233 switch (result) { 234 case SDGP_RESULT_OK: 235 printf("%ldMB, %d cyl, %d head, %d sec, %d bytes/sec, %ld sec total", 236 dp->disksize / (1048576 / dp->blksize), dp->cyls, 237 dp->heads, dp->sectors, dp->blksize, dp->disksize); 238 break; 239 240 case SDGP_RESULT_OFFLINE: 241 printf("drive offline"); 242 break; 243 244 case SDGP_RESULT_UNFORMATTED: 245 printf("unformatted media"); 246 break; 247 248 #ifdef DIAGNOSTIC 249 default: 250 panic("sdattach: unknown result from get_parms"); 251 break; 252 #endif 253 } 254 printf("\n"); 255 256 /* 257 * Establish a shutdown hook so that we can ensure that 258 * our data has actually made it onto the platter at 259 * shutdown time. Note that this relies on the fact 260 * that the shutdown hook code puts us at the head of 261 * the list (thus guaranteeing that our hook runs before 262 * our ancestors'). 263 */ 264 if ((sd->sc_sdhook = 265 shutdownhook_establish(sd_shutdown, sd)) == NULL) 266 printf("%s: WARNING: unable to establish shutdown hook\n", 267 sd->sc_dev.dv_xname); 268 } 269 270 int 271 sdactivate(self, act) 272 struct device *self; 273 enum devact act; 274 { 275 int rv = 0; 276 277 switch (act) { 278 case DVACT_ACTIVATE: 279 break; 280 281 case DVACT_DEACTIVATE: 282 /* 283 * Nothing to do; we key off the device's DVF_ACTIVATE. 284 */ 285 break; 286 } 287 return (rv); 288 } 289 290 291 int 292 sddetach(self, flags) 293 struct device *self; 294 int flags; 295 { 296 struct sd_softc *sc = (struct sd_softc *)self; 297 struct buf *dp, *bp; 298 int s, bmaj, cmaj, mn; 299 300 /* Remove unprocessed buffers from queue */ 301 s = splbio(); 302 for (dp = &sc->buf_queue; (bp = dp->b_actf) != NULL; ) { 303 dp->b_actf = bp->b_actf; 304 305 bp->b_error = ENXIO; 306 bp->b_flags |= B_ERROR; 307 biodone(bp); 308 } 309 splx(s); 310 311 /* locate the major number */ 312 mn = SDMINOR(self->dv_unit, 0); 313 314 for (bmaj = 0; bmaj < nblkdev; bmaj++) 315 if (bdevsw[bmaj].d_open == sdopen) 316 vdevgone(bmaj, mn, mn + MAXPARTITIONS - 1, VBLK); 317 for (cmaj = 0; cmaj < nchrdev; cmaj++) 318 if (cdevsw[cmaj].d_open == sdopen) 319 vdevgone(cmaj, mn, mn + MAXPARTITIONS - 1, VCHR); 320 321 /* Get rid of the shutdown hook. */ 322 if (sc->sc_sdhook != NULL) 323 shutdownhook_disestablish(sc->sc_sdhook); 324 325 #if NRND > 0 326 /* Unhook the entropy source. */ 327 rnd_detach_source(&sc->rnd_source); 328 #endif 329 330 return (0); 331 } 332 333 void 334 sdzeroref(self) 335 struct device *self; 336 { 337 struct sd_softc *sd = (struct sd_softc *)self; 338 339 /* Detach disk. */ 340 disk_detach(&sd->sc_dk); 341 } 342 343 /* 344 * open the device. Make sure the partition info is a up-to-date as can be. 345 */ 346 int 347 sdopen(dev, flag, fmt, p) 348 dev_t dev; 349 int flag, fmt; 350 struct proc *p; 351 { 352 struct sd_softc *sd; 353 struct scsi_link *sc_link; 354 int unit, part; 355 int error; 356 357 unit = SDUNIT(dev); 358 sd = sdlookup(unit); 359 if (sd == NULL) 360 return ENXIO; 361 362 sc_link = sd->sc_link; 363 364 SC_DEBUG(sc_link, SDEV_DB1, 365 ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit, 366 sd_cd.cd_ndevs, part)); 367 368 if ((error = sdlock(sd)) != 0) { 369 device_unref(&sd->sc_dev); 370 return error; 371 } 372 373 if (sd->sc_dk.dk_openmask != 0) { 374 /* 375 * If any partition is open, but the disk has been invalidated, 376 * disallow further opens. 377 */ 378 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 379 error = EIO; 380 goto bad3; 381 } 382 } else { 383 /* Check that it is still responding and ok. */ 384 error = scsi_test_unit_ready(sc_link, 385 SCSI_IGNORE_ILLEGAL_REQUEST | 386 SCSI_IGNORE_MEDIA_CHANGE | 387 SCSI_IGNORE_NOT_READY); 388 if (error) 389 goto bad3; 390 391 /* Start the pack spinning if necessary. */ 392 if ((sc_link->quirks & SDEV_NOSTARTUNIT) == 0) { 393 error = scsi_start(sc_link, SSS_START, 394 SCSI_IGNORE_ILLEGAL_REQUEST | 395 SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT); 396 if (error) 397 goto bad3; 398 } 399 400 sc_link->flags |= SDEV_OPEN; 401 402 /* Lock the pack in. */ 403 error = scsi_prevent(sc_link, PR_PREVENT, 404 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE); 405 if (error) 406 goto bad; 407 408 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 409 sc_link->flags |= SDEV_MEDIA_LOADED; 410 411 /* Load the physical device parameters. */ 412 if ((*sd->sc_ops->sdo_get_parms)(sd, &sd->params, 413 0) == SDGP_RESULT_OFFLINE) { 414 error = ENXIO; 415 goto bad2; 416 } 417 SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded ")); 418 419 /* Load the partition info if not already loaded. */ 420 sdgetdisklabel(dev, sd, sd->sc_dk.dk_label, 421 sd->sc_dk.dk_cpulabel, 0); 422 SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel loaded ")); 423 } 424 } 425 426 part = SDPART(dev); 427 428 /* Check that the partition exists. */ 429 if (part != RAW_PART && 430 (part >= sd->sc_dk.dk_label->d_npartitions || 431 sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) { 432 error = ENXIO; 433 goto bad; 434 } 435 436 /* Insure only one open at a time. */ 437 switch (fmt) { 438 case S_IFCHR: 439 sd->sc_dk.dk_copenmask |= (1 << part); 440 break; 441 case S_IFBLK: 442 sd->sc_dk.dk_bopenmask |= (1 << part); 443 break; 444 } 445 sd->sc_dk.dk_openmask = sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask; 446 447 SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n")); 448 sdunlock(sd); 449 device_unref(&sd->sc_dev); 450 return 0; 451 452 bad2: 453 sc_link->flags &= ~SDEV_MEDIA_LOADED; 454 455 bad: 456 if (sd->sc_dk.dk_openmask == 0) { 457 scsi_prevent(sc_link, PR_ALLOW, 458 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE); 459 sc_link->flags &= ~SDEV_OPEN; 460 } 461 462 bad3: 463 sdunlock(sd); 464 device_unref(&sd->sc_dev); 465 return error; 466 } 467 468 /* 469 * close the device.. only called if we are the LAST occurence of an open 470 * device. Convenient now but usually a pain. 471 */ 472 int 473 sdclose(dev, flag, fmt, p) 474 dev_t dev; 475 int flag, fmt; 476 struct proc *p; 477 { 478 struct sd_softc *sd; 479 int part = SDPART(dev); 480 int error; 481 482 sd = sdlookup(SDUNIT(dev)); 483 if (sd == NULL) 484 return ENXIO; 485 486 if ((error = sdlock(sd)) != 0) 487 return error; 488 489 switch (fmt) { 490 case S_IFCHR: 491 sd->sc_dk.dk_copenmask &= ~(1 << part); 492 break; 493 case S_IFBLK: 494 sd->sc_dk.dk_bopenmask &= ~(1 << part); 495 break; 496 } 497 sd->sc_dk.dk_openmask = sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask; 498 499 if (sd->sc_dk.dk_openmask == 0) { 500 if ((sd->flags & SDF_DIRTY) != 0 && 501 sd->sc_ops->sdo_flush != NULL) 502 (*sd->sc_ops->sdo_flush)(sd, 0); 503 504 scsi_prevent(sd->sc_link, PR_ALLOW, 505 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY); 506 sd->sc_link->flags &= ~(SDEV_OPEN|SDEV_MEDIA_LOADED); 507 508 if (sd->sc_link->flags & SDEV_EJECTING) { 509 scsi_start(sd->sc_link, SSS_STOP|SSS_LOEJ, 0); 510 511 sd->sc_link->flags &= ~SDEV_EJECTING; 512 } 513 } 514 515 sdunlock(sd); 516 device_unref(&sd->sc_dev); 517 return 0; 518 } 519 520 /* 521 * Actually translate the requested transfer into one the physical driver 522 * can understand. The transfer is described by a buf and will include 523 * only one physical transfer. 524 */ 525 void 526 sdstrategy(bp) 527 struct buf *bp; 528 { 529 struct sd_softc *sd; 530 int s; 531 532 sd = sdlookup(SDUNIT(bp->b_dev)); 533 if (sd == NULL) { 534 bp->b_error = ENXIO; 535 goto bad; 536 } 537 538 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdstrategy ")); 539 SC_DEBUG(sd->sc_link, SDEV_DB1, 540 ("%ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno)); 541 /* 542 * The transfer must be a whole number of blocks. 543 */ 544 if ((bp->b_bcount % sd->sc_dk.dk_label->d_secsize) != 0) { 545 bp->b_error = EINVAL; 546 goto bad; 547 } 548 /* 549 * If the device has been made invalid, error out 550 */ 551 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 552 if (sd->sc_link->flags & SDEV_OPEN) 553 bp->b_error = EIO; 554 else 555 bp->b_error = ENODEV; 556 goto bad; 557 } 558 /* 559 * If it's a null transfer, return immediatly 560 */ 561 if (bp->b_bcount == 0) 562 goto done; 563 564 /* 565 * Do bounds checking, adjust transfer. if error, process. 566 * If end of partition, just return. 567 */ 568 if (SDPART(bp->b_dev) != RAW_PART && 569 bounds_check_with_label(bp, sd->sc_dk.dk_label, 570 sd->sc_dk.dk_cpulabel, 571 (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0) 572 goto done; 573 574 s = splbio(); 575 576 /* 577 * Place it in the queue of disk activities for this disk 578 */ 579 disksort(&sd->buf_queue, bp); 580 581 /* 582 * Tell the device to get going on the transfer if it's 583 * not doing anything, otherwise just wait for completion 584 */ 585 sdstart(sd); 586 587 splx(s); 588 589 device_unref(&sd->sc_dev); 590 return; 591 592 bad: 593 bp->b_flags |= B_ERROR; 594 done: 595 /* 596 * Correctly set the buf to indicate a completed xfer 597 */ 598 bp->b_resid = bp->b_bcount; 599 biodone(bp); 600 601 if (sd != NULL) 602 device_unref(&sd->sc_dev); 603 } 604 605 /* 606 * sdstart looks to see if there is a buf waiting for the device 607 * and that the device is not already busy. If both are true, 608 * It dequeues the buf and creates a scsi command to perform the 609 * transfer in the buf. The transfer request will call scsi_done 610 * on completion, which will in turn call this routine again 611 * so that the next queued transfer is performed. 612 * The bufs are queued by the strategy routine (sdstrategy) 613 * 614 * This routine is also called after other non-queued requests 615 * have been made of the scsi driver, to ensure that the queue 616 * continues to be drained. 617 * 618 * must be called at the correct (highish) spl level 619 * sdstart() is called at splbio from sdstrategy and scsi_done 620 */ 621 void 622 sdstart(v) 623 register void *v; 624 { 625 register struct sd_softc *sd = v; 626 register struct scsi_link *sc_link = sd->sc_link; 627 struct buf *bp = 0; 628 struct buf *dp; 629 struct scsi_rw_big cmd_big; 630 struct scsi_rw cmd_small; 631 struct scsi_generic *cmdp; 632 int blkno, nblks, cmdlen, error; 633 struct partition *p; 634 635 SC_DEBUG(sc_link, SDEV_DB2, ("sdstart ")); 636 /* 637 * Check if the device has room for another command 638 */ 639 while (sc_link->openings > 0) { 640 /* 641 * there is excess capacity, but a special waits 642 * It'll need the adapter as soon as we clear out of the 643 * way and let it run (user level wait). 644 */ 645 if (sc_link->flags & SDEV_WAITING) { 646 sc_link->flags &= ~SDEV_WAITING; 647 wakeup((caddr_t)sc_link); 648 return; 649 } 650 651 /* 652 * See if there is a buf with work for us to do.. 653 */ 654 dp = &sd->buf_queue; 655 if ((bp = dp->b_actf) == NULL) /* yes, an assign */ 656 return; 657 dp->b_actf = bp->b_actf; 658 659 /* 660 * If the device has become invalid, abort all the 661 * reads and writes until all files have been closed and 662 * re-opened 663 */ 664 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 665 bp->b_error = EIO; 666 bp->b_flags |= B_ERROR; 667 bp->b_resid = bp->b_bcount; 668 biodone(bp); 669 continue; 670 } 671 672 /* 673 * We have a buf, now we should make a command 674 * 675 * First, translate the block to absolute and put it in terms 676 * of the logical blocksize of the device. 677 */ 678 blkno = 679 bp->b_blkno / (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE); 680 if (SDPART(bp->b_dev) != RAW_PART) { 681 p = &sd->sc_dk.dk_label->d_partitions[SDPART(bp->b_dev)]; 682 blkno += p->p_offset; 683 } 684 nblks = howmany(bp->b_bcount, sd->sc_dk.dk_label->d_secsize); 685 686 /* 687 * Fill out the scsi command. If the transfer will 688 * fit in a "small" cdb, use it. 689 */ 690 if (!(sc_link->flags & SDEV_ATAPI) && 691 ((blkno & 0x1fffff) == blkno) && 692 ((nblks & 0xff) == nblks)) { 693 /* 694 * We can fit in a small cdb. 695 */ 696 bzero(&cmd_small, sizeof(cmd_small)); 697 cmd_small.opcode = (bp->b_flags & B_READ) ? 698 READ_COMMAND : WRITE_COMMAND; 699 _lto3b(blkno, cmd_small.addr); 700 cmd_small.length = nblks & 0xff; 701 cmdlen = sizeof(cmd_small); 702 cmdp = (struct scsi_generic *)&cmd_small; 703 } else { 704 /* 705 * Need a large cdb. 706 */ 707 bzero(&cmd_big, sizeof(cmd_big)); 708 cmd_big.opcode = (bp->b_flags & B_READ) ? 709 READ_BIG : WRITE_BIG; 710 _lto4b(blkno, cmd_big.addr); 711 _lto2b(nblks, cmd_big.length); 712 cmdlen = sizeof(cmd_big); 713 cmdp = (struct scsi_generic *)&cmd_big; 714 } 715 716 /* Instrumentation. */ 717 disk_busy(&sd->sc_dk); 718 719 /* 720 * Mark the disk dirty so that the cache will be 721 * flushed on close. 722 */ 723 if ((bp->b_flags & B_READ) == 0) 724 sd->flags |= SDF_DIRTY; 725 726 727 /* 728 * Call the routine that chats with the adapter. 729 * Note: we cannot sleep as we may be an interrupt 730 */ 731 error = scsi_scsi_cmd(sc_link, cmdp, cmdlen, 732 (u_char *)bp->b_data, bp->b_bcount, 733 SDRETRIES, 60000, bp, SCSI_NOSLEEP | 734 ((bp->b_flags & B_READ) ? SCSI_DATA_IN : SCSI_DATA_OUT)); 735 if (error) { 736 disk_unbusy(&sd->sc_dk, 0); 737 printf("%s: not queued, error %d\n", 738 sd->sc_dev.dv_xname, error); 739 } 740 } 741 } 742 743 void 744 sddone(xs) 745 struct scsi_xfer *xs; 746 { 747 struct sd_softc *sd = xs->sc_link->device_softc; 748 749 if (sd->flags & SDF_FLUSHING) { 750 /* Flush completed, no longer dirty. */ 751 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 752 } 753 754 if (xs->bp != NULL) 755 disk_unbusy(&sd->sc_dk, (xs->bp->b_bcount - xs->bp->b_resid)); 756 } 757 758 void 759 sdminphys(bp) 760 struct buf *bp; 761 { 762 struct sd_softc *sd; 763 long max; 764 765 sd = sdlookup(SDUNIT(bp->b_dev)); 766 if (sd == NULL) 767 return; /* XXX - right way to fail this? */ 768 769 /* 770 * If the device is ancient, we want to make sure that 771 * the transfer fits into a 6-byte cdb. 772 * 773 * XXX Note that the SCSI-I spec says that 256-block transfers 774 * are allowed in a 6-byte read/write, and are specified 775 * by settng the "length" to 0. However, we're conservative 776 * here, allowing only 255-block transfers in case an 777 * ancient device gets confused by length == 0. A length of 0 778 * in a 10-byte read/write actually means 0 blocks. 779 */ 780 if (sd->flags & SDF_ANCIENT) { 781 max = sd->sc_dk.dk_label->d_secsize * 0xff; 782 783 if (bp->b_bcount > max) 784 bp->b_bcount = max; 785 } 786 787 (*sd->sc_link->adapter->scsi_minphys)(bp); 788 789 device_unref(&sd->sc_dev); 790 } 791 792 int 793 sdread(dev, uio, ioflag) 794 dev_t dev; 795 struct uio *uio; 796 int ioflag; 797 { 798 799 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio)); 800 } 801 802 int 803 sdwrite(dev, uio, ioflag) 804 dev_t dev; 805 struct uio *uio; 806 int ioflag; 807 { 808 809 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio)); 810 } 811 812 /* 813 * Perform special action on behalf of the user 814 * Knows about the internals of this device 815 */ 816 int 817 sdioctl(dev, cmd, addr, flag, p) 818 dev_t dev; 819 u_long cmd; 820 caddr_t addr; 821 int flag; 822 struct proc *p; 823 { 824 struct sd_softc *sd; 825 int error = 0; 826 int part = SDPART(dev); 827 828 sd = sdlookup(SDUNIT(dev)); 829 if (sd == NULL) 830 return ENXIO; 831 832 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdioctl 0x%lx ", cmd)); 833 834 /* 835 * If the device is not valid.. abandon ship 836 */ 837 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 838 switch (cmd) { 839 case DIOCWLABEL: 840 case DIOCLOCK: 841 case DIOCEJECT: 842 case SCIOCIDENTIFY: 843 case OSCIOCIDENTIFY: 844 case SCIOCCOMMAND: 845 case SCIOCDEBUG: 846 if (part == RAW_PART) 847 break; 848 /* FALLTHROUGH */ 849 default: 850 if ((sd->sc_link->flags & SDEV_OPEN) == 0) { 851 error = ENODEV; 852 goto exit; 853 } else { 854 error = EIO; 855 goto exit; 856 } 857 } 858 } 859 860 switch (cmd) { 861 case DIOCRLDINFO: 862 sdgetdisklabel(dev, sd, sd->sc_dk.dk_label, 863 sd->sc_dk.dk_cpulabel, 0); 864 goto exit; 865 case DIOCGPDINFO: { 866 struct cpu_disklabel osdep; 867 868 sdgetdisklabel(dev, sd, (struct disklabel *)addr, 869 &osdep, 1); 870 goto exit; 871 } 872 873 case DIOCGDINFO: 874 *(struct disklabel *)addr = *(sd->sc_dk.dk_label); 875 goto exit; 876 877 case DIOCGPART: 878 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label; 879 ((struct partinfo *)addr)->part = 880 &sd->sc_dk.dk_label->d_partitions[SDPART(dev)]; 881 goto exit; 882 883 case DIOCWDINFO: 884 case DIOCSDINFO: 885 if ((flag & FWRITE) == 0) { 886 error = EBADF; 887 goto exit; 888 } 889 890 if ((error = sdlock(sd)) != 0) 891 goto exit; 892 sd->flags |= SDF_LABELLING; 893 894 error = setdisklabel(sd->sc_dk.dk_label, 895 (struct disklabel *)addr, /*sd->sc_dk.dk_openmask : */0, 896 sd->sc_dk.dk_cpulabel); 897 if (error == 0) { 898 if (cmd == DIOCWDINFO) 899 error = writedisklabel(SDLABELDEV(dev), 900 sdstrategy, sd->sc_dk.dk_label, 901 sd->sc_dk.dk_cpulabel); 902 } 903 904 sd->flags &= ~SDF_LABELLING; 905 sdunlock(sd); 906 goto exit; 907 908 case DIOCWLABEL: 909 if ((flag & FWRITE) == 0) { 910 error = EBADF; 911 goto exit; 912 } 913 if (*(int *)addr) 914 sd->flags |= SDF_WLABEL; 915 else 916 sd->flags &= ~SDF_WLABEL; 917 goto exit; 918 919 case DIOCLOCK: 920 error = scsi_prevent(sd->sc_link, 921 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0); 922 goto exit; 923 924 case MTIOCTOP: 925 if (((struct mtop *)addr)->mt_op != MTOFFL) { 926 error = EIO; 927 goto exit; 928 } 929 /* FALLTHROUGH */ 930 case DIOCEJECT: 931 if ((sd->sc_link->flags & SDEV_REMOVABLE) == 0) { 932 error = ENOTTY; 933 goto exit; 934 } 935 sd->sc_link->flags |= SDEV_EJECTING; 936 goto exit; 937 938 case SCIOCREASSIGN: 939 if ((flag & FWRITE) == 0) { 940 error = EBADF; 941 goto exit; 942 } 943 error = sd_reassign_blocks(sd, (*(int *)addr)); 944 goto exit; 945 946 default: 947 if (part != RAW_PART) { 948 error = ENOTTY; 949 goto exit; 950 } 951 error = scsi_do_ioctl(sd->sc_link, dev, cmd, addr, flag, p); 952 } 953 954 exit: 955 device_unref(&sd->sc_dev); 956 return (error); 957 } 958 959 /* 960 * Load the label information on the named device 961 */ 962 void 963 sdgetdisklabel(dev, sd, lp, clp, spoofonly) 964 dev_t dev; 965 struct sd_softc *sd; 966 struct disklabel *lp; 967 struct cpu_disklabel *clp; 968 int spoofonly; 969 { 970 char *errstring; 971 972 bzero(lp, sizeof(struct disklabel)); 973 bzero(clp, sizeof(struct cpu_disklabel)); 974 975 lp->d_secsize = sd->params.blksize; 976 lp->d_ntracks = sd->params.heads; 977 lp->d_nsectors = sd->params.sectors; 978 lp->d_ncylinders = sd->params.cyls; 979 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 980 if (lp->d_secpercyl == 0) { 981 lp->d_secpercyl = 100; 982 /* as long as it's not 0 - readdisklabel divides by it */ 983 } 984 985 lp->d_type = DTYPE_SCSI; 986 if (sd->type == T_OPTICAL) 987 strncpy(lp->d_typename, "SCSI optical", 988 sizeof(lp->d_typename) - 1); 989 else 990 strncpy(lp->d_typename, "SCSI disk", 991 sizeof(lp->d_typename) - 1); 992 993 if (strlen(sd->name.vendor) + strlen(sd->name.product) + 1 < 994 sizeof(lp->d_packname)) 995 sprintf(lp->d_packname, "%s %s", sd->name.vendor, 996 sd->name.product); 997 else 998 strncpy(lp->d_packname, sd->name.product, 999 sizeof(lp->d_packname) - 1); 1000 1001 lp->d_secperunit = sd->params.disksize; 1002 lp->d_rpm = 3600; 1003 lp->d_interleave = 1; 1004 lp->d_flags = 0; 1005 1006 /* XXX - these values for BBSIZE and SBSIZE assume ffs */ 1007 lp->d_bbsize = BBSIZE; 1008 lp->d_sbsize = SBSIZE; 1009 1010 lp->d_partitions[RAW_PART].p_offset = 0; 1011 lp->d_partitions[RAW_PART].p_size = 1012 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 1013 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; 1014 lp->d_npartitions = RAW_PART + 1; 1015 1016 lp->d_magic = DISKMAGIC; 1017 lp->d_magic2 = DISKMAGIC; 1018 lp->d_checksum = dkcksum(lp); 1019 1020 /* 1021 * Call the generic disklabel extraction routine 1022 */ 1023 errstring = readdisklabel(SDLABELDEV(dev), sdstrategy, lp, clp, 1024 spoofonly); 1025 if (errstring) { 1026 /*printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);*/ 1027 return; 1028 } 1029 } 1030 1031 1032 void 1033 sd_shutdown(arg) 1034 void *arg; 1035 { 1036 struct sd_softc *sd = arg; 1037 1038 /* 1039 * If the disk cache needs to be flushed, and the disk supports 1040 * it, flush it. We're cold at this point, so we poll for 1041 * completion. 1042 */ 1043 if ((sd->flags & SDF_DIRTY) != 0 && sd->sc_ops->sdo_flush != NULL) 1044 (*sd->sc_ops->sdo_flush)(sd, SCSI_AUTOCONF); 1045 } 1046 1047 /* 1048 * Tell the device to map out a defective block 1049 */ 1050 int 1051 sd_reassign_blocks(sd, blkno) 1052 struct sd_softc *sd; 1053 u_long blkno; 1054 { 1055 struct scsi_reassign_blocks scsi_cmd; 1056 struct scsi_reassign_blocks_data rbdata; 1057 1058 bzero(&scsi_cmd, sizeof(scsi_cmd)); 1059 bzero(&rbdata, sizeof(rbdata)); 1060 scsi_cmd.opcode = REASSIGN_BLOCKS; 1061 1062 _lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length); 1063 _lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr); 1064 1065 return scsi_scsi_cmd(sd->sc_link, (struct scsi_generic *)&scsi_cmd, 1066 sizeof(scsi_cmd), (u_char *)&rbdata, sizeof(rbdata), SDRETRIES, 1067 5000, NULL, SCSI_DATA_OUT); 1068 } 1069 1070 /* 1071 * Check Errors 1072 */ 1073 int 1074 sd_interpret_sense(xs) 1075 struct scsi_xfer *xs; 1076 { 1077 struct scsi_link *sc_link = xs->sc_link; 1078 struct scsi_sense_data *sense = &xs->sense; 1079 struct sd_softc *sd = sc_link->device_softc; 1080 int retval = SCSIRET_CONTINUE; 1081 1082 /* 1083 * If the device is not open yet, let the generic code handle it. 1084 */ 1085 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 1086 return (retval); 1087 } 1088 1089 /* 1090 * If it isn't a extended or extended/deferred error, let 1091 * the generic code handle it. 1092 */ 1093 if ((sense->error_code & SSD_ERRCODE) != 0x70 && 1094 (sense->error_code & SSD_ERRCODE) != 0x71) { /* DEFFERRED */ 1095 return (retval); 1096 } 1097 1098 if ((sense->flags & SSD_KEY) == SKEY_NOT_READY && 1099 sense->add_sense_code == 0x4) { 1100 if (sense->add_sense_code_qual == 0x01) { 1101 printf("%s: ..is spinning up...waiting\n", 1102 sd->sc_dev.dv_xname); 1103 /* 1104 * I really need a sdrestart function I can call here. 1105 */ 1106 delay(1000000 * 5); /* 5 seconds */ 1107 retval = SCSIRET_RETRY; 1108 } else if ((sense->add_sense_code_qual == 0x2) && 1109 (sd->sc_link->quirks & SDEV_NOSTARTUNIT) == 0) { 1110 if (sd->sc_link->flags & SDEV_REMOVABLE) { 1111 printf( 1112 "%s: removable disk stopped - not restarting\n", 1113 sd->sc_dev.dv_xname); 1114 retval = EIO; 1115 } else { 1116 printf("%s: respinning up disk\n", 1117 sd->sc_dev.dv_xname); 1118 retval = scsi_start(sd->sc_link, SSS_START, 1119 SCSI_URGENT | SCSI_NOSLEEP); 1120 if (retval != 0) { 1121 printf( 1122 "%s: respin of disk failed - %d\n", 1123 sd->sc_dev.dv_xname, retval); 1124 retval = EIO; 1125 } else { 1126 retval = SCSIRET_RETRY; 1127 } 1128 } 1129 } 1130 } 1131 return (retval); 1132 } 1133 1134 int 1135 sdsize(dev) 1136 dev_t dev; 1137 { 1138 struct sd_softc *sd; 1139 int part, omask; 1140 int size; 1141 1142 sd = sdlookup(SDUNIT(dev)); 1143 if (sd == NULL) 1144 return -1; 1145 1146 part = SDPART(dev); 1147 omask = sd->sc_dk.dk_openmask & (1 << part); 1148 1149 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0) { 1150 size = -1; 1151 goto exit; 1152 } 1153 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) 1154 size = -1; 1155 else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP) 1156 size = -1; 1157 else 1158 size = sd->sc_dk.dk_label->d_partitions[part].p_size * 1159 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE); 1160 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0) 1161 size = -1; 1162 1163 exit: 1164 device_unref(&sd->sc_dev); 1165 return size; 1166 } 1167 1168 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */ 1169 static struct scsi_xfer sx; 1170 static int sddoingadump; 1171 1172 /* 1173 * dump all of physical memory into the partition specified, starting 1174 * at offset 'dumplo' into the partition. 1175 */ 1176 int 1177 sddump(dev, blkno, va, size) 1178 dev_t dev; 1179 daddr_t blkno; 1180 caddr_t va; 1181 size_t size; 1182 { 1183 struct sd_softc *sd; /* disk unit to do the I/O */ 1184 struct disklabel *lp; /* disk's disklabel */ 1185 int unit, part; 1186 int sectorsize; /* size of a disk sector */ 1187 int nsects; /* number of sectors in partition */ 1188 int sectoff; /* sector offset of partition */ 1189 int totwrt; /* total number of sectors left to write */ 1190 int nwrt; /* current number of sectors to write */ 1191 struct scsi_rw_big cmd; /* write command */ 1192 struct scsi_xfer *xs; /* ... convenience */ 1193 int retval; 1194 1195 /* Check if recursive dump; if so, punt. */ 1196 if (sddoingadump) 1197 return EFAULT; 1198 1199 /* Mark as active early. */ 1200 sddoingadump = 1; 1201 1202 unit = SDUNIT(dev); /* Decompose unit & partition. */ 1203 part = SDPART(dev); 1204 1205 /* Check for acceptable drive number. */ 1206 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL) 1207 return ENXIO; 1208 1209 /* 1210 * XXX Can't do this check, since the media might have been 1211 * XXX marked `invalid' by successful unmounting of all 1212 * XXX filesystems. 1213 */ 1214 #if 0 1215 /* Make sure it was initialized. */ 1216 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) != SDEV_MEDIA_LOADED) 1217 return ENXIO; 1218 #endif 1219 1220 /* Convert to disk sectors. Request must be a multiple of size. */ 1221 lp = sd->sc_dk.dk_label; 1222 sectorsize = lp->d_secsize; 1223 if ((size % sectorsize) != 0) 1224 return EFAULT; 1225 totwrt = size / sectorsize; 1226 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */ 1227 1228 nsects = lp->d_partitions[part].p_size; 1229 sectoff = lp->d_partitions[part].p_offset; 1230 1231 /* Check transfer bounds against partition size. */ 1232 if ((blkno < 0) || ((blkno + totwrt) > nsects)) 1233 return EINVAL; 1234 1235 /* Offset block number to start of partition. */ 1236 blkno += sectoff; 1237 1238 xs = &sx; 1239 1240 while (totwrt > 0) { 1241 nwrt = totwrt; /* XXX */ 1242 #ifndef SD_DUMP_NOT_TRUSTED 1243 /* 1244 * Fill out the scsi command 1245 */ 1246 bzero(&cmd, sizeof(cmd)); 1247 cmd.opcode = WRITE_BIG; 1248 _lto4b(blkno, cmd.addr); 1249 _lto2b(nwrt, cmd.length); 1250 /* 1251 * Fill out the scsi_xfer structure 1252 * Note: we cannot sleep as we may be an interrupt 1253 * don't use scsi_scsi_cmd() as it may want 1254 * to wait for an xs. 1255 */ 1256 bzero(xs, sizeof(sx)); 1257 xs->flags |= SCSI_AUTOCONF | INUSE | SCSI_DATA_OUT; 1258 xs->sc_link = sd->sc_link; 1259 xs->retries = SDRETRIES; 1260 xs->timeout = 10000; /* 10000 millisecs for a disk ! */ 1261 xs->cmd = (struct scsi_generic *)&cmd; 1262 xs->cmdlen = sizeof(cmd); 1263 xs->resid = nwrt * sectorsize; 1264 xs->error = XS_NOERROR; 1265 xs->bp = 0; 1266 xs->data = va; 1267 xs->datalen = nwrt * sectorsize; 1268 1269 /* 1270 * Pass all this info to the scsi driver. 1271 */ 1272 retval = (*(sd->sc_link->adapter->scsi_cmd)) (xs); 1273 if (retval != COMPLETE) 1274 return ENXIO; 1275 #else /* SD_DUMP_NOT_TRUSTED */ 1276 /* Let's just talk about this first... */ 1277 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno); 1278 delay(500 * 1000); /* half a second */ 1279 #endif /* SD_DUMP_NOT_TRUSTED */ 1280 1281 /* update block count */ 1282 totwrt -= nwrt; 1283 blkno += nwrt; 1284 va += sectorsize * nwrt; 1285 } 1286 sddoingadump = 0; 1287 return 0; 1288 } 1289 1290 /* 1291 * Copy up to len chars from src to dst, ignoring non-printables. 1292 * Must be room for len+1 chars in dst so we can write the NUL. 1293 * Does not assume src is NUL-terminated. 1294 */ 1295 void 1296 viscpy(dst, src, len) 1297 u_char *dst; 1298 u_char *src; 1299 int len; 1300 { 1301 while (len > 0 && *src != '\0') { 1302 if (*src < 0x20 || *src >= 0x80) { 1303 src++; 1304 continue; 1305 } 1306 *dst++ = *src++; 1307 len--; 1308 } 1309 *dst = '\0'; 1310 } 1311