1 /* $OpenBSD: sd.c,v 1.51 2001/10/08 01:50:48 drahn 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 !(sc_link->quirks & SDEV_NOCDB6) && 692 ((blkno & 0x1fffff) == blkno) && 693 ((nblks & 0xff) == nblks)) { 694 /* 695 * We can fit in a small cdb. 696 */ 697 bzero(&cmd_small, sizeof(cmd_small)); 698 cmd_small.opcode = (bp->b_flags & B_READ) ? 699 READ_COMMAND : WRITE_COMMAND; 700 _lto3b(blkno, cmd_small.addr); 701 cmd_small.length = nblks & 0xff; 702 cmdlen = sizeof(cmd_small); 703 cmdp = (struct scsi_generic *)&cmd_small; 704 } else { 705 /* 706 * Need a large cdb. 707 */ 708 bzero(&cmd_big, sizeof(cmd_big)); 709 cmd_big.opcode = (bp->b_flags & B_READ) ? 710 READ_BIG : WRITE_BIG; 711 _lto4b(blkno, cmd_big.addr); 712 _lto2b(nblks, cmd_big.length); 713 cmdlen = sizeof(cmd_big); 714 cmdp = (struct scsi_generic *)&cmd_big; 715 } 716 717 /* Instrumentation. */ 718 disk_busy(&sd->sc_dk); 719 720 /* 721 * Mark the disk dirty so that the cache will be 722 * flushed on close. 723 */ 724 if ((bp->b_flags & B_READ) == 0) 725 sd->flags |= SDF_DIRTY; 726 727 728 /* 729 * Call the routine that chats with the adapter. 730 * Note: we cannot sleep as we may be an interrupt 731 */ 732 error = scsi_scsi_cmd(sc_link, cmdp, cmdlen, 733 (u_char *)bp->b_data, bp->b_bcount, 734 SDRETRIES, 60000, bp, SCSI_NOSLEEP | 735 ((bp->b_flags & B_READ) ? SCSI_DATA_IN : SCSI_DATA_OUT)); 736 if (error) { 737 disk_unbusy(&sd->sc_dk, 0); 738 printf("%s: not queued, error %d\n", 739 sd->sc_dev.dv_xname, error); 740 } 741 } 742 } 743 744 void 745 sddone(xs) 746 struct scsi_xfer *xs; 747 { 748 struct sd_softc *sd = xs->sc_link->device_softc; 749 750 if (sd->flags & SDF_FLUSHING) { 751 /* Flush completed, no longer dirty. */ 752 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 753 } 754 755 if (xs->bp != NULL) 756 disk_unbusy(&sd->sc_dk, (xs->bp->b_bcount - xs->bp->b_resid)); 757 } 758 759 void 760 sdminphys(bp) 761 struct buf *bp; 762 { 763 struct sd_softc *sd; 764 long max; 765 766 sd = sdlookup(SDUNIT(bp->b_dev)); 767 if (sd == NULL) 768 return; /* XXX - right way to fail this? */ 769 770 /* 771 * If the device is ancient, we want to make sure that 772 * the transfer fits into a 6-byte cdb. 773 * 774 * XXX Note that the SCSI-I spec says that 256-block transfers 775 * are allowed in a 6-byte read/write, and are specified 776 * by settng the "length" to 0. However, we're conservative 777 * here, allowing only 255-block transfers in case an 778 * ancient device gets confused by length == 0. A length of 0 779 * in a 10-byte read/write actually means 0 blocks. 780 */ 781 if (sd->flags & SDF_ANCIENT) { 782 max = sd->sc_dk.dk_label->d_secsize * 0xff; 783 784 if (bp->b_bcount > max) 785 bp->b_bcount = max; 786 } 787 788 (*sd->sc_link->adapter->scsi_minphys)(bp); 789 790 device_unref(&sd->sc_dev); 791 } 792 793 int 794 sdread(dev, uio, ioflag) 795 dev_t dev; 796 struct uio *uio; 797 int ioflag; 798 { 799 800 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio)); 801 } 802 803 int 804 sdwrite(dev, uio, ioflag) 805 dev_t dev; 806 struct uio *uio; 807 int ioflag; 808 { 809 810 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio)); 811 } 812 813 /* 814 * Perform special action on behalf of the user 815 * Knows about the internals of this device 816 */ 817 int 818 sdioctl(dev, cmd, addr, flag, p) 819 dev_t dev; 820 u_long cmd; 821 caddr_t addr; 822 int flag; 823 struct proc *p; 824 { 825 struct sd_softc *sd; 826 int error = 0; 827 int part = SDPART(dev); 828 829 sd = sdlookup(SDUNIT(dev)); 830 if (sd == NULL) 831 return ENXIO; 832 833 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdioctl 0x%lx ", cmd)); 834 835 /* 836 * If the device is not valid.. abandon ship 837 */ 838 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 839 switch (cmd) { 840 case DIOCWLABEL: 841 case DIOCLOCK: 842 case DIOCEJECT: 843 case SCIOCIDENTIFY: 844 case OSCIOCIDENTIFY: 845 case SCIOCCOMMAND: 846 case SCIOCDEBUG: 847 if (part == RAW_PART) 848 break; 849 /* FALLTHROUGH */ 850 default: 851 if ((sd->sc_link->flags & SDEV_OPEN) == 0) { 852 error = ENODEV; 853 goto exit; 854 } else { 855 error = EIO; 856 goto exit; 857 } 858 } 859 } 860 861 switch (cmd) { 862 case DIOCRLDINFO: 863 sdgetdisklabel(dev, sd, sd->sc_dk.dk_label, 864 sd->sc_dk.dk_cpulabel, 0); 865 goto exit; 866 case DIOCGPDINFO: { 867 struct cpu_disklabel osdep; 868 869 sdgetdisklabel(dev, sd, (struct disklabel *)addr, 870 &osdep, 1); 871 goto exit; 872 } 873 874 case DIOCGDINFO: 875 *(struct disklabel *)addr = *(sd->sc_dk.dk_label); 876 goto exit; 877 878 case DIOCGPART: 879 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label; 880 ((struct partinfo *)addr)->part = 881 &sd->sc_dk.dk_label->d_partitions[SDPART(dev)]; 882 goto exit; 883 884 case DIOCWDINFO: 885 case DIOCSDINFO: 886 if ((flag & FWRITE) == 0) { 887 error = EBADF; 888 goto exit; 889 } 890 891 if ((error = sdlock(sd)) != 0) 892 goto exit; 893 sd->flags |= SDF_LABELLING; 894 895 error = setdisklabel(sd->sc_dk.dk_label, 896 (struct disklabel *)addr, /*sd->sc_dk.dk_openmask : */0, 897 sd->sc_dk.dk_cpulabel); 898 if (error == 0) { 899 if (cmd == DIOCWDINFO) 900 error = writedisklabel(SDLABELDEV(dev), 901 sdstrategy, sd->sc_dk.dk_label, 902 sd->sc_dk.dk_cpulabel); 903 } 904 905 sd->flags &= ~SDF_LABELLING; 906 sdunlock(sd); 907 goto exit; 908 909 case DIOCWLABEL: 910 if ((flag & FWRITE) == 0) { 911 error = EBADF; 912 goto exit; 913 } 914 if (*(int *)addr) 915 sd->flags |= SDF_WLABEL; 916 else 917 sd->flags &= ~SDF_WLABEL; 918 goto exit; 919 920 case DIOCLOCK: 921 error = scsi_prevent(sd->sc_link, 922 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0); 923 goto exit; 924 925 case MTIOCTOP: 926 if (((struct mtop *)addr)->mt_op != MTOFFL) { 927 error = EIO; 928 goto exit; 929 } 930 /* FALLTHROUGH */ 931 case DIOCEJECT: 932 if ((sd->sc_link->flags & SDEV_REMOVABLE) == 0) { 933 error = ENOTTY; 934 goto exit; 935 } 936 sd->sc_link->flags |= SDEV_EJECTING; 937 goto exit; 938 939 case SCIOCREASSIGN: 940 if ((flag & FWRITE) == 0) { 941 error = EBADF; 942 goto exit; 943 } 944 error = sd_reassign_blocks(sd, (*(int *)addr)); 945 goto exit; 946 947 default: 948 if (part != RAW_PART) { 949 error = ENOTTY; 950 goto exit; 951 } 952 error = scsi_do_ioctl(sd->sc_link, dev, cmd, addr, flag, p); 953 } 954 955 exit: 956 device_unref(&sd->sc_dev); 957 return (error); 958 } 959 960 /* 961 * Load the label information on the named device 962 */ 963 void 964 sdgetdisklabel(dev, sd, lp, clp, spoofonly) 965 dev_t dev; 966 struct sd_softc *sd; 967 struct disklabel *lp; 968 struct cpu_disklabel *clp; 969 int spoofonly; 970 { 971 char *errstring; 972 973 bzero(lp, sizeof(struct disklabel)); 974 bzero(clp, sizeof(struct cpu_disklabel)); 975 976 lp->d_secsize = sd->params.blksize; 977 lp->d_ntracks = sd->params.heads; 978 lp->d_nsectors = sd->params.sectors; 979 lp->d_ncylinders = sd->params.cyls; 980 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 981 if (lp->d_secpercyl == 0) { 982 lp->d_secpercyl = 100; 983 /* as long as it's not 0 - readdisklabel divides by it */ 984 } 985 986 lp->d_type = DTYPE_SCSI; 987 if (sd->type == T_OPTICAL) 988 strncpy(lp->d_typename, "SCSI optical", 989 sizeof(lp->d_typename) - 1); 990 else 991 strncpy(lp->d_typename, "SCSI disk", 992 sizeof(lp->d_typename) - 1); 993 994 if (strlen(sd->name.vendor) + strlen(sd->name.product) + 1 < 995 sizeof(lp->d_packname)) 996 sprintf(lp->d_packname, "%s %s", sd->name.vendor, 997 sd->name.product); 998 else 999 strncpy(lp->d_packname, sd->name.product, 1000 sizeof(lp->d_packname) - 1); 1001 1002 lp->d_secperunit = sd->params.disksize; 1003 lp->d_rpm = 3600; 1004 lp->d_interleave = 1; 1005 lp->d_flags = 0; 1006 1007 /* XXX - these values for BBSIZE and SBSIZE assume ffs */ 1008 lp->d_bbsize = BBSIZE; 1009 lp->d_sbsize = SBSIZE; 1010 1011 lp->d_partitions[RAW_PART].p_offset = 0; 1012 lp->d_partitions[RAW_PART].p_size = 1013 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 1014 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; 1015 lp->d_npartitions = RAW_PART + 1; 1016 1017 lp->d_magic = DISKMAGIC; 1018 lp->d_magic2 = DISKMAGIC; 1019 lp->d_checksum = dkcksum(lp); 1020 1021 /* 1022 * Call the generic disklabel extraction routine 1023 */ 1024 errstring = readdisklabel(SDLABELDEV(dev), sdstrategy, lp, clp, 1025 spoofonly); 1026 if (errstring) { 1027 /*printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);*/ 1028 return; 1029 } 1030 } 1031 1032 1033 void 1034 sd_shutdown(arg) 1035 void *arg; 1036 { 1037 struct sd_softc *sd = arg; 1038 1039 /* 1040 * If the disk cache needs to be flushed, and the disk supports 1041 * it, flush it. We're cold at this point, so we poll for 1042 * completion. 1043 */ 1044 if ((sd->flags & SDF_DIRTY) != 0 && sd->sc_ops->sdo_flush != NULL) 1045 (*sd->sc_ops->sdo_flush)(sd, SCSI_AUTOCONF); 1046 } 1047 1048 /* 1049 * Tell the device to map out a defective block 1050 */ 1051 int 1052 sd_reassign_blocks(sd, blkno) 1053 struct sd_softc *sd; 1054 u_long blkno; 1055 { 1056 struct scsi_reassign_blocks scsi_cmd; 1057 struct scsi_reassign_blocks_data rbdata; 1058 1059 bzero(&scsi_cmd, sizeof(scsi_cmd)); 1060 bzero(&rbdata, sizeof(rbdata)); 1061 scsi_cmd.opcode = REASSIGN_BLOCKS; 1062 1063 _lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length); 1064 _lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr); 1065 1066 return scsi_scsi_cmd(sd->sc_link, (struct scsi_generic *)&scsi_cmd, 1067 sizeof(scsi_cmd), (u_char *)&rbdata, sizeof(rbdata), SDRETRIES, 1068 5000, NULL, SCSI_DATA_OUT); 1069 } 1070 1071 /* 1072 * Check Errors 1073 */ 1074 int 1075 sd_interpret_sense(xs) 1076 struct scsi_xfer *xs; 1077 { 1078 struct scsi_link *sc_link = xs->sc_link; 1079 struct scsi_sense_data *sense = &xs->sense; 1080 struct sd_softc *sd = sc_link->device_softc; 1081 int retval = SCSIRET_CONTINUE; 1082 1083 /* 1084 * If the device is not open yet, let the generic code handle it. 1085 */ 1086 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 1087 return (retval); 1088 } 1089 1090 /* 1091 * If it isn't a extended or extended/deferred error, let 1092 * the generic code handle it. 1093 */ 1094 if ((sense->error_code & SSD_ERRCODE) != 0x70 && 1095 (sense->error_code & SSD_ERRCODE) != 0x71) { /* DEFFERRED */ 1096 return (retval); 1097 } 1098 1099 if ((sense->flags & SSD_KEY) == SKEY_NOT_READY && 1100 sense->add_sense_code == 0x4) { 1101 if (sense->add_sense_code_qual == 0x01) { 1102 printf("%s: ..is spinning up...waiting\n", 1103 sd->sc_dev.dv_xname); 1104 /* 1105 * I really need a sdrestart function I can call here. 1106 */ 1107 delay(1000000 * 5); /* 5 seconds */ 1108 retval = SCSIRET_RETRY; 1109 } else if ((sense->add_sense_code_qual == 0x2) && 1110 (sd->sc_link->quirks & SDEV_NOSTARTUNIT) == 0) { 1111 if (sd->sc_link->flags & SDEV_REMOVABLE) { 1112 printf( 1113 "%s: removable disk stopped - not restarting\n", 1114 sd->sc_dev.dv_xname); 1115 retval = EIO; 1116 } else { 1117 printf("%s: respinning up disk\n", 1118 sd->sc_dev.dv_xname); 1119 retval = scsi_start(sd->sc_link, SSS_START, 1120 SCSI_URGENT | SCSI_NOSLEEP); 1121 if (retval != 0) { 1122 printf( 1123 "%s: respin of disk failed - %d\n", 1124 sd->sc_dev.dv_xname, retval); 1125 retval = EIO; 1126 } else { 1127 retval = SCSIRET_RETRY; 1128 } 1129 } 1130 } 1131 } 1132 return (retval); 1133 } 1134 1135 int 1136 sdsize(dev) 1137 dev_t dev; 1138 { 1139 struct sd_softc *sd; 1140 int part, omask; 1141 int size; 1142 1143 sd = sdlookup(SDUNIT(dev)); 1144 if (sd == NULL) 1145 return -1; 1146 1147 part = SDPART(dev); 1148 omask = sd->sc_dk.dk_openmask & (1 << part); 1149 1150 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0) { 1151 size = -1; 1152 goto exit; 1153 } 1154 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) 1155 size = -1; 1156 else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP) 1157 size = -1; 1158 else 1159 size = sd->sc_dk.dk_label->d_partitions[part].p_size * 1160 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE); 1161 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0) 1162 size = -1; 1163 1164 exit: 1165 device_unref(&sd->sc_dev); 1166 return size; 1167 } 1168 1169 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */ 1170 static struct scsi_xfer sx; 1171 static int sddoingadump; 1172 1173 /* 1174 * dump all of physical memory into the partition specified, starting 1175 * at offset 'dumplo' into the partition. 1176 */ 1177 int 1178 sddump(dev, blkno, va, size) 1179 dev_t dev; 1180 daddr_t blkno; 1181 caddr_t va; 1182 size_t size; 1183 { 1184 struct sd_softc *sd; /* disk unit to do the I/O */ 1185 struct disklabel *lp; /* disk's disklabel */ 1186 int unit, part; 1187 int sectorsize; /* size of a disk sector */ 1188 int nsects; /* number of sectors in partition */ 1189 int sectoff; /* sector offset of partition */ 1190 int totwrt; /* total number of sectors left to write */ 1191 int nwrt; /* current number of sectors to write */ 1192 struct scsi_rw_big cmd; /* write command */ 1193 struct scsi_xfer *xs; /* ... convenience */ 1194 int retval; 1195 1196 /* Check if recursive dump; if so, punt. */ 1197 if (sddoingadump) 1198 return EFAULT; 1199 1200 /* Mark as active early. */ 1201 sddoingadump = 1; 1202 1203 unit = SDUNIT(dev); /* Decompose unit & partition. */ 1204 part = SDPART(dev); 1205 1206 /* Check for acceptable drive number. */ 1207 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL) 1208 return ENXIO; 1209 1210 /* 1211 * XXX Can't do this check, since the media might have been 1212 * XXX marked `invalid' by successful unmounting of all 1213 * XXX filesystems. 1214 */ 1215 #if 0 1216 /* Make sure it was initialized. */ 1217 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) != SDEV_MEDIA_LOADED) 1218 return ENXIO; 1219 #endif 1220 1221 /* Convert to disk sectors. Request must be a multiple of size. */ 1222 lp = sd->sc_dk.dk_label; 1223 sectorsize = lp->d_secsize; 1224 if ((size % sectorsize) != 0) 1225 return EFAULT; 1226 totwrt = size / sectorsize; 1227 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */ 1228 1229 nsects = lp->d_partitions[part].p_size; 1230 sectoff = lp->d_partitions[part].p_offset; 1231 1232 /* Check transfer bounds against partition size. */ 1233 if ((blkno < 0) || ((blkno + totwrt) > nsects)) 1234 return EINVAL; 1235 1236 /* Offset block number to start of partition. */ 1237 blkno += sectoff; 1238 1239 xs = &sx; 1240 1241 while (totwrt > 0) { 1242 nwrt = totwrt; /* XXX */ 1243 #ifndef SD_DUMP_NOT_TRUSTED 1244 /* 1245 * Fill out the scsi command 1246 */ 1247 bzero(&cmd, sizeof(cmd)); 1248 cmd.opcode = WRITE_BIG; 1249 _lto4b(blkno, cmd.addr); 1250 _lto2b(nwrt, cmd.length); 1251 /* 1252 * Fill out the scsi_xfer structure 1253 * Note: we cannot sleep as we may be an interrupt 1254 * don't use scsi_scsi_cmd() as it may want 1255 * to wait for an xs. 1256 */ 1257 bzero(xs, sizeof(sx)); 1258 xs->flags |= SCSI_AUTOCONF | SCSI_DATA_OUT; 1259 xs->sc_link = sd->sc_link; 1260 xs->retries = SDRETRIES; 1261 xs->timeout = 10000; /* 10000 millisecs for a disk ! */ 1262 xs->cmd = (struct scsi_generic *)&cmd; 1263 xs->cmdlen = sizeof(cmd); 1264 xs->resid = nwrt * sectorsize; 1265 xs->error = XS_NOERROR; 1266 xs->bp = 0; 1267 xs->data = va; 1268 xs->datalen = nwrt * sectorsize; 1269 1270 /* 1271 * Pass all this info to the scsi driver. 1272 */ 1273 retval = (*(sd->sc_link->adapter->scsi_cmd)) (xs); 1274 if (retval != COMPLETE) 1275 return ENXIO; 1276 #else /* SD_DUMP_NOT_TRUSTED */ 1277 /* Let's just talk about this first... */ 1278 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno); 1279 delay(500 * 1000); /* half a second */ 1280 #endif /* SD_DUMP_NOT_TRUSTED */ 1281 1282 /* update block count */ 1283 totwrt -= nwrt; 1284 blkno += nwrt; 1285 va += sectorsize * nwrt; 1286 } 1287 sddoingadump = 0; 1288 return 0; 1289 } 1290 1291 /* 1292 * Copy up to len chars from src to dst, ignoring non-printables. 1293 * Must be room for len+1 chars in dst so we can write the NUL. 1294 * Does not assume src is NUL-terminated. 1295 */ 1296 void 1297 viscpy(dst, src, len) 1298 u_char *dst; 1299 u_char *src; 1300 int len; 1301 { 1302 while (len > 0 && *src != '\0') { 1303 if (*src < 0x20 || *src >= 0x80) { 1304 src++; 1305 continue; 1306 } 1307 *dst++ = *src++; 1308 len--; 1309 } 1310 *dst = '\0'; 1311 } 1312