1 /* $NetBSD: sd.c,v 1.135 1998/08/17 19:30:38 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 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 "opt_scsi.h" 57 #include "rnd.h" 58 59 #include <sys/types.h> 60 #include <sys/param.h> 61 #include <sys/systm.h> 62 #include <sys/kernel.h> 63 #include <sys/file.h> 64 #include <sys/stat.h> 65 #include <sys/ioctl.h> 66 #include <sys/buf.h> 67 #include <sys/uio.h> 68 #include <sys/malloc.h> 69 #include <sys/errno.h> 70 #include <sys/device.h> 71 #include <sys/disklabel.h> 72 #include <sys/disk.h> 73 #include <sys/proc.h> 74 #include <sys/conf.h> 75 #if NRND > 0 76 #include <sys/rnd.h> 77 #endif 78 79 #include <dev/scsipi/scsipi_all.h> 80 #include <dev/scsipi/scsi_all.h> 81 #include <dev/scsipi/scsipi_disk.h> 82 #include <dev/scsipi/scsi_disk.h> 83 #include <dev/scsipi/scsiconf.h> 84 #include <dev/scsipi/sdvar.h> 85 86 #include "sd.h" /* NSD_SCSIBUS and NSD_ATAPIBUS come from here */ 87 88 #ifndef SDOUTSTANDING 89 #define SDOUTSTANDING 4 90 #endif 91 92 #define SDUNIT(dev) DISKUNIT(dev) 93 #define SDPART(dev) DISKPART(dev) 94 #define MAKESDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part) 95 96 #define SDLABELDEV(dev) (MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART)) 97 98 int sdlock __P((struct sd_softc *)); 99 void sdunlock __P((struct sd_softc *)); 100 void sdminphys __P((struct buf *)); 101 void sdgetdefaultlabel __P((struct sd_softc *, struct disklabel *)); 102 void sdgetdisklabel __P((struct sd_softc *)); 103 void sdstart __P((void *)); 104 void sddone __P((struct scsipi_xfer *)); 105 void sd_shutdown __P((void *)); 106 int sd_reassign_blocks __P((struct sd_softc *, u_long)); 107 int sd_interpret_sense __P((struct scsipi_xfer *)); 108 109 extern struct cfdriver sd_cd; 110 111 struct dkdriver sddkdriver = { sdstrategy }; 112 113 struct scsipi_device sd_switch = { 114 sd_interpret_sense, /* check our error handler first */ 115 sdstart, /* have a queue, served by this */ 116 NULL, /* have no async handler */ 117 sddone, /* deal with stats at interrupt time */ 118 }; 119 120 /* 121 * The routine called by the low level scsi routine when it discovers 122 * a device suitable for this driver. 123 */ 124 void 125 sdattach(parent, sd, sc_link, ops) 126 struct device *parent; 127 struct sd_softc *sd; 128 struct scsipi_link *sc_link; 129 const struct sd_ops *ops; 130 { 131 int error, result; 132 struct disk_parms *dp = &sd->params; 133 134 SC_DEBUG(sc_link, SDEV_DB2, ("sdattach: ")); 135 136 /* 137 * Store information needed to contact our base driver 138 */ 139 sd->sc_link = sc_link; 140 sd->sc_ops = ops; 141 sc_link->device = &sd_switch; 142 sc_link->device_softc = sd; 143 if (sc_link->openings > SDOUTSTANDING) 144 sc_link->openings = SDOUTSTANDING; 145 146 /* 147 * Initialize and attach the disk structure. 148 */ 149 sd->sc_dk.dk_driver = &sddkdriver; 150 sd->sc_dk.dk_name = sd->sc_dev.dv_xname; 151 disk_attach(&sd->sc_dk); 152 153 #if !defined(i386) 154 dk_establish(&sd->sc_dk, &sd->sc_dev); /* XXX */ 155 #endif 156 157 /* 158 * Use the subdriver to request information regarding 159 * the drive. We cannot use interrupts yet, so the 160 * request must specify this. 161 */ 162 printf("\n"); 163 164 #if NSD_ATAPIBUS > 0 165 /* XXX BEGIN HACK ALERT!!! */ 166 if (sc_link->type == BUS_ATAPI) { 167 /* 168 * The ATAPI sense data handling is _TOTALLY_ broken. 169 * In particular, it _never_ does a REQUEST_SENSE to get 170 * sense data! This causes UNIT ATTENTION to be not 171 * cleared properly for some ATAPI devices (e.g. Zip), 172 * which messes up the probe. 173 * 174 * This is the easiest way to get around the problem. 175 * 176 * YUCK! 177 */ 178 (void)scsipi_test_unit_ready(sc_link, SCSI_AUTOCONF | 179 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE | 180 SCSI_IGNORE_NOT_READY); 181 } 182 /* XXX END HACK ALERT!!! */ 183 #endif /* NSD_ATAPIBUS > 0 */ 184 185 if ((sd->sc_link->quirks & SDEV_NOSTARTUNIT) == 0) { 186 error = scsipi_start(sd->sc_link, SSS_START, 187 SCSI_AUTOCONF | SCSI_IGNORE_ILLEGAL_REQUEST | 188 SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT); 189 } else 190 error = 0; 191 192 if (error) 193 result = SDGP_RESULT_OFFLINE; 194 else 195 result = (*sd->sc_ops->sdo_get_parms)(sd, &sd->params, 196 SCSI_AUTOCONF); 197 printf("%s: ", sd->sc_dev.dv_xname); 198 switch (result) { 199 case SDGP_RESULT_OK: 200 printf("%ldMB, %ld cyl, %ld head, %ld sec, %ld bytes/sect x %ld sectors", 201 dp->disksize / (1048576 / dp->blksize), dp->cyls, 202 dp->heads, dp->sectors, dp->blksize, dp->disksize); 203 break; 204 205 case SDGP_RESULT_OFFLINE: 206 printf("drive offline"); 207 break; 208 209 case SDGP_RESULT_UNFORMATTED: 210 printf("unformatted media"); 211 break; 212 213 #ifdef DIAGNOSTIC 214 default: 215 panic("sdattach: unknown result from get_parms"); 216 break; 217 #endif 218 } 219 printf("\n"); 220 221 /* 222 * Establish a shutdown hook so that we can ensure that 223 * our data has actually made it onto the platter at 224 * shutdown time. Note that this relies on the fact 225 * that the shutdown hook code puts us at the head of 226 * the list (thus guaranteeing that our hook runs before 227 * our ancestors'). 228 */ 229 if ((sd->sc_sdhook = 230 shutdownhook_establish(sd_shutdown, sd)) == NULL) 231 printf("%s: WARNING: unable to establish shutdown hook\n", 232 sd->sc_dev.dv_xname); 233 234 #if NRND > 0 235 /* 236 * attach the device into the random source list 237 */ 238 rnd_attach_source(&sd->rnd_source, sd->sc_dev.dv_xname, RND_TYPE_DISK); 239 #endif 240 } 241 242 /* 243 * Wait interruptibly for an exclusive lock. 244 * 245 * XXX 246 * Several drivers do this; it should be abstracted and made MP-safe. 247 */ 248 int 249 sdlock(sd) 250 struct sd_softc *sd; 251 { 252 int error; 253 254 while ((sd->flags & SDF_LOCKED) != 0) { 255 sd->flags |= SDF_WANTED; 256 if ((error = tsleep(sd, PRIBIO | PCATCH, "sdlck", 0)) != 0) 257 return (error); 258 } 259 sd->flags |= SDF_LOCKED; 260 return (0); 261 } 262 263 /* 264 * Unlock and wake up any waiters. 265 */ 266 void 267 sdunlock(sd) 268 struct sd_softc *sd; 269 { 270 271 sd->flags &= ~SDF_LOCKED; 272 if ((sd->flags & SDF_WANTED) != 0) { 273 sd->flags &= ~SDF_WANTED; 274 wakeup(sd); 275 } 276 } 277 278 /* 279 * open the device. Make sure the partition info is a up-to-date as can be. 280 */ 281 int 282 sdopen(dev, flag, fmt, p) 283 dev_t dev; 284 int flag, fmt; 285 struct proc *p; 286 { 287 struct sd_softc *sd; 288 struct scsipi_link *sc_link; 289 int unit, part; 290 int error; 291 292 unit = SDUNIT(dev); 293 if (unit >= sd_cd.cd_ndevs) 294 return (ENXIO); 295 sd = sd_cd.cd_devs[unit]; 296 if (sd == NULL) 297 return (ENXIO); 298 299 sc_link = sd->sc_link; 300 301 SC_DEBUG(sc_link, SDEV_DB1, 302 ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit, 303 sd_cd.cd_ndevs, SDPART(dev))); 304 305 if ((error = sdlock(sd)) != 0) 306 return (error); 307 308 if (sd->sc_dk.dk_openmask != 0) { 309 /* 310 * If any partition is open, but the disk has been invalidated, 311 * disallow further opens. 312 */ 313 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 314 error = EIO; 315 goto bad3; 316 } 317 } else { 318 /* Check that it is still responding and ok. */ 319 error = scsipi_test_unit_ready(sc_link, 320 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE | 321 SCSI_IGNORE_NOT_READY); 322 if (error) 323 goto bad3; 324 325 /* Start the pack spinning if necessary. */ 326 if ((sc_link->quirks & SDEV_NOSTARTUNIT) == 0) { 327 error = scsipi_start(sc_link, SSS_START, 328 SCSI_IGNORE_ILLEGAL_REQUEST | 329 SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT); 330 if (error) 331 goto bad3; 332 } 333 334 sc_link->flags |= SDEV_OPEN; 335 336 /* Lock the pack in. */ 337 error = scsipi_prevent(sc_link, PR_PREVENT, 338 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE); 339 if (error) 340 goto bad; 341 342 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 343 sc_link->flags |= SDEV_MEDIA_LOADED; 344 345 /* 346 * Load the physical device parameters. 347 * 348 * Note that if media is present but unformatted, 349 * we allow the open (so that it can be formatted!). 350 * The drive should refuse real I/O, if the media is 351 * unformatted. 352 */ 353 if ((*sd->sc_ops->sdo_get_parms)(sd, &sd->params, 354 0) == SDGP_RESULT_OFFLINE) { 355 error = ENXIO; 356 goto bad2; 357 } 358 SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded ")); 359 360 /* Load the partition info if not already loaded. */ 361 sdgetdisklabel(sd); 362 SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel loaded ")); 363 } 364 } 365 366 part = SDPART(dev); 367 368 /* Check that the partition exists. */ 369 if (part != RAW_PART && 370 (part >= sd->sc_dk.dk_label->d_npartitions || 371 sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) { 372 error = ENXIO; 373 goto bad; 374 } 375 376 /* Insure only one open at a time. */ 377 switch (fmt) { 378 case S_IFCHR: 379 sd->sc_dk.dk_copenmask |= (1 << part); 380 break; 381 case S_IFBLK: 382 sd->sc_dk.dk_bopenmask |= (1 << part); 383 break; 384 } 385 sd->sc_dk.dk_openmask = 386 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask; 387 388 SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n")); 389 sdunlock(sd); 390 return (0); 391 392 bad2: 393 sc_link->flags &= ~SDEV_MEDIA_LOADED; 394 395 bad: 396 if (sd->sc_dk.dk_openmask == 0) { 397 scsipi_prevent(sc_link, PR_ALLOW, 398 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE); 399 sc_link->flags &= ~SDEV_OPEN; 400 } 401 402 bad3: 403 sdunlock(sd); 404 return (error); 405 } 406 407 /* 408 * close the device.. only called if we are the LAST occurence of an open 409 * device. Convenient now but usually a pain. 410 */ 411 int 412 sdclose(dev, flag, fmt, p) 413 dev_t dev; 414 int flag, fmt; 415 struct proc *p; 416 { 417 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)]; 418 int part = SDPART(dev); 419 int error; 420 421 if ((error = sdlock(sd)) != 0) 422 return (error); 423 424 switch (fmt) { 425 case S_IFCHR: 426 sd->sc_dk.dk_copenmask &= ~(1 << part); 427 break; 428 case S_IFBLK: 429 sd->sc_dk.dk_bopenmask &= ~(1 << part); 430 break; 431 } 432 sd->sc_dk.dk_openmask = 433 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask; 434 435 if (sd->sc_dk.dk_openmask == 0) { 436 /* 437 * If the disk cache needs flushing, and the disk supports 438 * it, do it now. 439 */ 440 if ((sd->flags & SDF_DIRTY) != 0 && 441 sd->sc_ops->sdo_flush != NULL) 442 (*sd->sc_ops->sdo_flush)(sd, 0); 443 444 /* XXXX Must wait for I/O to complete! */ 445 446 scsipi_prevent(sd->sc_link, PR_ALLOW, 447 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY); 448 sd->sc_link->flags &= ~(SDEV_OPEN|SDEV_MEDIA_LOADED); 449 } 450 451 sdunlock(sd); 452 return (0); 453 } 454 455 /* 456 * Actually translate the requested transfer into one the physical driver 457 * can understand. The transfer is described by a buf and will include 458 * only one physical transfer. 459 */ 460 void 461 sdstrategy(bp) 462 struct buf *bp; 463 { 464 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)]; 465 int s; 466 467 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdstrategy ")); 468 SC_DEBUG(sd->sc_link, SDEV_DB1, 469 ("%ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno)); 470 /* 471 * The transfer must be a whole number of blocks. 472 */ 473 if ((bp->b_bcount % sd->sc_dk.dk_label->d_secsize) != 0) { 474 bp->b_error = EINVAL; 475 goto bad; 476 } 477 /* 478 * If the device has been made invalid, error out 479 */ 480 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 481 bp->b_error = EIO; 482 goto bad; 483 } 484 /* 485 * If it's a null transfer, return immediatly 486 */ 487 if (bp->b_bcount == 0) 488 goto done; 489 490 /* 491 * Do bounds checking, adjust transfer. if error, process. 492 * If end of partition, just return. 493 */ 494 if (SDPART(bp->b_dev) != RAW_PART && 495 bounds_check_with_label(bp, sd->sc_dk.dk_label, 496 (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0) 497 goto done; 498 499 s = splbio(); 500 501 /* 502 * Place it in the queue of disk activities for this disk 503 */ 504 disksort(&sd->buf_queue, bp); 505 506 /* 507 * Tell the device to get going on the transfer if it's 508 * not doing anything, otherwise just wait for completion 509 */ 510 sdstart(sd); 511 512 splx(s); 513 return; 514 515 bad: 516 bp->b_flags |= B_ERROR; 517 done: 518 /* 519 * Correctly set the buf to indicate a completed xfer 520 */ 521 bp->b_resid = bp->b_bcount; 522 biodone(bp); 523 } 524 525 /* 526 * sdstart looks to see if there is a buf waiting for the device 527 * and that the device is not already busy. If both are true, 528 * It dequeues the buf and creates a scsi command to perform the 529 * transfer in the buf. The transfer request will call scsipi_done 530 * on completion, which will in turn call this routine again 531 * so that the next queued transfer is performed. 532 * The bufs are queued by the strategy routine (sdstrategy) 533 * 534 * This routine is also called after other non-queued requests 535 * have been made of the scsi driver, to ensure that the queue 536 * continues to be drained. 537 * 538 * must be called at the correct (highish) spl level 539 * sdstart() is called at splbio from sdstrategy and scsipi_done 540 */ 541 void 542 sdstart(v) 543 register void *v; 544 { 545 register struct sd_softc *sd = v; 546 register struct scsipi_link *sc_link = sd->sc_link; 547 struct disklabel *lp = sd->sc_dk.dk_label; 548 struct buf *bp = 0; 549 struct buf *dp; 550 struct scsipi_rw_big cmd_big; 551 #if NSD_SCSIBUS > 0 552 struct scsi_rw cmd_small; 553 #endif 554 struct scsipi_generic *cmdp; 555 int blkno, nblks, cmdlen, error; 556 struct partition *p; 557 558 SC_DEBUG(sc_link, SDEV_DB2, ("sdstart ")); 559 /* 560 * Check if the device has room for another command 561 */ 562 while (sc_link->openings > 0) { 563 /* 564 * there is excess capacity, but a special waits 565 * It'll need the adapter as soon as we clear out of the 566 * way and let it run (user level wait). 567 */ 568 if (sc_link->flags & SDEV_WAITING) { 569 sc_link->flags &= ~SDEV_WAITING; 570 wakeup((caddr_t)sc_link); 571 return; 572 } 573 574 /* 575 * See if there is a buf with work for us to do.. 576 */ 577 dp = &sd->buf_queue; 578 if ((bp = dp->b_actf) == NULL) /* yes, an assign */ 579 return; 580 dp->b_actf = bp->b_actf; 581 582 /* 583 * If the device has become invalid, abort all the 584 * reads and writes until all files have been closed and 585 * re-opened 586 */ 587 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 588 bp->b_error = EIO; 589 bp->b_flags |= B_ERROR; 590 bp->b_resid = bp->b_bcount; 591 biodone(bp); 592 continue; 593 } 594 595 /* 596 * We have a buf, now we should make a command 597 * 598 * First, translate the block to absolute and put it in terms 599 * of the logical blocksize of the device. 600 */ 601 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE); 602 if (SDPART(bp->b_dev) != RAW_PART) { 603 p = &lp->d_partitions[SDPART(bp->b_dev)]; 604 blkno += p->p_offset; 605 } 606 nblks = howmany(bp->b_bcount, lp->d_secsize); 607 608 #if NSD_SCSIBUS > 0 609 /* 610 * Fill out the scsi command. If the transfer will 611 * fit in a "small" cdb, use it. 612 */ 613 if (((blkno & 0x1fffff) == blkno) && 614 ((nblks & 0xff) == nblks) && sc_link->type == BUS_SCSI) { 615 /* 616 * We can fit in a small cdb. 617 */ 618 bzero(&cmd_small, sizeof(cmd_small)); 619 cmd_small.opcode = (bp->b_flags & B_READ) ? 620 SCSI_READ_COMMAND : SCSI_WRITE_COMMAND; 621 _lto3b(blkno, cmd_small.addr); 622 cmd_small.length = nblks & 0xff; 623 cmdlen = sizeof(cmd_small); 624 cmdp = (struct scsipi_generic *)&cmd_small; 625 } else 626 #endif 627 { 628 /* 629 * Need a large cdb. 630 */ 631 bzero(&cmd_big, sizeof(cmd_big)); 632 cmd_big.opcode = (bp->b_flags & B_READ) ? 633 READ_BIG : WRITE_BIG; 634 _lto4b(blkno, cmd_big.addr); 635 _lto2b(nblks, cmd_big.length); 636 cmdlen = sizeof(cmd_big); 637 cmdp = (struct scsipi_generic *)&cmd_big; 638 } 639 640 /* Instrumentation. */ 641 disk_busy(&sd->sc_dk); 642 643 /* 644 * Mark the disk dirty so that the cache will be 645 * flushed on close. 646 */ 647 if ((bp->b_flags & B_READ) == 0) 648 sd->flags |= SDF_DIRTY; 649 650 /* 651 * Call the routine that chats with the adapter. 652 * Note: we cannot sleep as we may be an interrupt 653 */ 654 error = scsipi_command(sc_link, cmdp, cmdlen, 655 (u_char *)bp->b_data, bp->b_bcount, 656 SDRETRIES, 60000, bp, SCSI_NOSLEEP | 657 ((bp->b_flags & B_READ) ? SCSI_DATA_IN : SCSI_DATA_OUT)); 658 if (error) { 659 disk_unbusy(&sd->sc_dk, 0); 660 printf("%s: not queued, error %d\n", 661 sd->sc_dev.dv_xname, error); 662 } 663 } 664 } 665 666 void 667 sddone(xs) 668 struct scsipi_xfer *xs; 669 { 670 struct sd_softc *sd = xs->sc_link->device_softc; 671 672 if (sd->flags & SDF_FLUSHING) { 673 /* Flush completed, no longer dirty. */ 674 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 675 } 676 677 if (xs->bp != NULL) { 678 disk_unbusy(&sd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid); 679 #if NRND > 0 680 rnd_add_uint32(&sd->rnd_source, xs->bp->b_blkno); 681 #endif 682 } 683 } 684 685 void 686 sdminphys(bp) 687 struct buf *bp; 688 { 689 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)]; 690 long max; 691 692 /* 693 * If the device is ancient, we want to make sure that 694 * the transfer fits into a 6-byte cdb. 695 * 696 * XXX Note that the SCSI-I spec says that 256-block transfers 697 * are allowed in a 6-byte read/write, and are specified 698 * by settng the "length" to 0. However, we're conservative 699 * here, allowing only 255-block transfers in case an 700 * ancient device gets confused by length == 0. A length of 0 701 * in a 10-byte read/write actually means 0 blocks. 702 */ 703 if (sd->flags & SDF_ANCIENT) { 704 max = sd->sc_dk.dk_label->d_secsize * 0xff; 705 706 if (bp->b_bcount > max) 707 bp->b_bcount = max; 708 } 709 710 (*sd->sc_link->adapter->scsipi_minphys)(bp); 711 } 712 713 int 714 sdread(dev, uio, ioflag) 715 dev_t dev; 716 struct uio *uio; 717 int ioflag; 718 { 719 720 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio)); 721 } 722 723 int 724 sdwrite(dev, uio, ioflag) 725 dev_t dev; 726 struct uio *uio; 727 int ioflag; 728 { 729 730 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio)); 731 } 732 733 /* 734 * Perform special action on behalf of the user 735 * Knows about the internals of this device 736 */ 737 int 738 sdioctl(dev, cmd, addr, flag, p) 739 dev_t dev; 740 u_long cmd; 741 caddr_t addr; 742 int flag; 743 struct proc *p; 744 { 745 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)]; 746 int error; 747 748 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdioctl 0x%lx ", cmd)); 749 750 /* 751 * If the device is not valid.. abandon ship 752 */ 753 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) 754 return (EIO); 755 756 switch (cmd) { 757 case DIOCGDINFO: 758 *(struct disklabel *)addr = *(sd->sc_dk.dk_label); 759 return (0); 760 761 case DIOCGPART: 762 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label; 763 ((struct partinfo *)addr)->part = 764 &sd->sc_dk.dk_label->d_partitions[SDPART(dev)]; 765 return (0); 766 767 case DIOCWDINFO: 768 case DIOCSDINFO: 769 if ((flag & FWRITE) == 0) 770 return (EBADF); 771 772 if ((error = sdlock(sd)) != 0) 773 return (error); 774 sd->flags |= SDF_LABELLING; 775 776 error = setdisklabel(sd->sc_dk.dk_label, 777 (struct disklabel *)addr, /*sd->sc_dk.dk_openmask : */0, 778 sd->sc_dk.dk_cpulabel); 779 if (error == 0) { 780 if (cmd == DIOCWDINFO) 781 error = writedisklabel(SDLABELDEV(dev), 782 sdstrategy, sd->sc_dk.dk_label, 783 sd->sc_dk.dk_cpulabel); 784 } 785 786 sd->flags &= ~SDF_LABELLING; 787 sdunlock(sd); 788 return (error); 789 790 case DIOCWLABEL: 791 if ((flag & FWRITE) == 0) 792 return (EBADF); 793 if (*(int *)addr) 794 sd->flags |= SDF_WLABEL; 795 else 796 sd->flags &= ~SDF_WLABEL; 797 return (0); 798 799 case DIOCLOCK: 800 return (scsipi_prevent(sd->sc_link, 801 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0)); 802 803 case DIOCEJECT: 804 return ((sd->sc_link->flags & SDEV_REMOVABLE) == 0 ? ENOTTY : 805 scsipi_start(sd->sc_link, SSS_STOP|SSS_LOEJ, 0)); 806 807 case DIOCGDEFLABEL: 808 sdgetdefaultlabel(sd, (struct disklabel *)addr); 809 return (0); 810 811 default: 812 if (SDPART(dev) != RAW_PART) 813 return (ENOTTY); 814 return (scsipi_do_ioctl(sd->sc_link, dev, cmd, addr, flag, p)); 815 } 816 817 #ifdef DIAGNOSTIC 818 panic("sdioctl: impossible"); 819 #endif 820 } 821 822 void 823 sdgetdefaultlabel(sd, lp) 824 struct sd_softc *sd; 825 struct disklabel *lp; 826 { 827 828 bzero(lp, sizeof(struct disklabel)); 829 830 lp->d_secsize = sd->params.blksize; 831 lp->d_ntracks = sd->params.heads; 832 lp->d_nsectors = sd->params.sectors; 833 lp->d_ncylinders = sd->params.cyls; 834 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 835 836 switch (sd->sc_link->type) { 837 #if NSD_SCSIBUS > 0 838 case BUS_SCSI: 839 lp->d_type = DTYPE_SCSI; 840 break; 841 #endif 842 #if NSD_ATAPIBUS > 0 843 case BUS_ATAPI: 844 lp->d_type = DTYPE_ATAPI; 845 break; 846 #endif 847 } 848 strncpy(lp->d_typename, sd->name, 16); 849 strncpy(lp->d_packname, "fictitious", 16); 850 lp->d_secperunit = sd->params.disksize; 851 lp->d_rpm = sd->params.rot_rate; 852 lp->d_interleave = 1; 853 lp->d_flags = 0; 854 855 lp->d_partitions[RAW_PART].p_offset = 0; 856 lp->d_partitions[RAW_PART].p_size = 857 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 858 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; 859 lp->d_npartitions = RAW_PART + 1; 860 861 lp->d_magic = DISKMAGIC; 862 lp->d_magic2 = DISKMAGIC; 863 lp->d_checksum = dkcksum(lp); 864 } 865 866 867 /* 868 * Load the label information on the named device 869 */ 870 void 871 sdgetdisklabel(sd) 872 struct sd_softc *sd; 873 { 874 struct disklabel *lp = sd->sc_dk.dk_label; 875 char *errstring; 876 877 bzero(sd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel)); 878 879 sdgetdefaultlabel(sd, lp); 880 881 if (lp->d_secpercyl == 0) { 882 lp->d_secpercyl = 100; 883 /* as long as it's not 0 - readdisklabel divides by it (?) */ 884 } 885 886 /* 887 * Call the generic disklabel extraction routine 888 */ 889 errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART), 890 sdstrategy, lp, sd->sc_dk.dk_cpulabel); 891 if (errstring) { 892 printf("%s: %s\n", sd->sc_dev.dv_xname, errstring); 893 return; 894 } 895 } 896 897 void 898 sd_shutdown(arg) 899 void *arg; 900 { 901 struct sd_softc *sd = arg; 902 903 /* 904 * If the disk cache needs to be flushed, and the disk supports 905 * it, flush it. We're cold at this point, so we poll for 906 * completion. 907 */ 908 if ((sd->flags & SDF_DIRTY) != 0 && sd->sc_ops->sdo_flush != NULL) 909 (*sd->sc_ops->sdo_flush)(sd, SCSI_AUTOCONF); 910 } 911 912 /* 913 * Tell the device to map out a defective block 914 */ 915 int 916 sd_reassign_blocks(sd, blkno) 917 struct sd_softc *sd; 918 u_long blkno; 919 { 920 struct scsi_reassign_blocks scsipi_cmd; 921 struct scsi_reassign_blocks_data rbdata; 922 923 bzero(&scsipi_cmd, sizeof(scsipi_cmd)); 924 bzero(&rbdata, sizeof(rbdata)); 925 scsipi_cmd.opcode = SCSI_REASSIGN_BLOCKS; 926 927 _lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length); 928 _lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr); 929 930 return (scsipi_command(sd->sc_link, 931 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 932 (u_char *)&rbdata, sizeof(rbdata), SDRETRIES, 5000, NULL, 933 SCSI_DATA_OUT)); 934 } 935 936 /* 937 * Check Errors 938 */ 939 int 940 sd_interpret_sense(xs) 941 struct scsipi_xfer *xs; 942 { 943 struct scsipi_link *sc_link = xs->sc_link; 944 struct scsipi_sense_data *sense = &xs->sense.scsi_sense; 945 struct sd_softc *sd = sc_link->device_softc; 946 int retval = SCSIRET_CONTINUE; 947 948 /* 949 * If the device is not open yet, let the generic code handle it. 950 */ 951 if ((sc_link->flags & SDEV_OPEN) == 0) { 952 return (retval); 953 } 954 955 /* 956 * If it isn't a extended or extended/deferred error, let 957 * the generic code handle it. 958 */ 959 if ((sense->error_code & SSD_ERRCODE) != 0x70 && 960 (sense->error_code & SSD_ERRCODE) != 0x71) { /* DEFFERRED */ 961 return (retval); 962 } 963 964 if ((sense->flags & SSD_KEY) == SKEY_NOT_READY && 965 sense->add_sense_code == 0x4) { 966 if (sense->add_sense_code_qual == 0x01) { 967 printf("%s: ..is spinning up...waiting\n", 968 sd->sc_dev.dv_xname); 969 /* 970 * I really need a sdrestart function I can call here. 971 */ 972 delay(1000000 * 5); /* 5 seconds */ 973 retval = SCSIRET_RETRY; 974 } else if ((sense->add_sense_code_qual == 0x2) && 975 (sd->sc_link->quirks & SDEV_NOSTARTUNIT) == 0) { 976 if (sd->sc_link->flags & SDEV_REMOVABLE) { 977 printf("%s: removable disk stopped- not " 978 "restarting\n", sd->sc_dev.dv_xname); 979 retval = EIO; 980 } else { 981 printf("%s: respinning up disk\n", 982 sd->sc_dev.dv_xname); 983 retval = scsipi_start(sd->sc_link, SSS_START, 984 SCSI_URGENT | SCSI_NOSLEEP); 985 if (retval != 0) { 986 printf("%s: respin of disk failed-%d\n", 987 sd->sc_dev.dv_xname, retval); 988 retval = EIO; 989 } else { 990 retval = SCSIRET_RETRY; 991 } 992 } 993 } 994 } 995 return (retval); 996 } 997 998 999 int 1000 sdsize(dev) 1001 dev_t dev; 1002 { 1003 struct sd_softc *sd; 1004 int part, unit, omask; 1005 int size; 1006 1007 unit = SDUNIT(dev); 1008 if (unit >= sd_cd.cd_ndevs) 1009 return (-1); 1010 sd = sd_cd.cd_devs[unit]; 1011 if (sd == NULL) 1012 return (-1); 1013 1014 part = SDPART(dev); 1015 omask = sd->sc_dk.dk_openmask & (1 << part); 1016 1017 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0) 1018 return (-1); 1019 if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP) 1020 size = -1; 1021 else 1022 size = sd->sc_dk.dk_label->d_partitions[part].p_size * 1023 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE); 1024 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0) 1025 return (-1); 1026 return (size); 1027 } 1028 1029 #ifndef __BDEVSW_DUMP_OLD_TYPE 1030 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */ 1031 static struct scsipi_xfer sx; 1032 static int sddoingadump; 1033 1034 /* 1035 * dump all of physical memory into the partition specified, starting 1036 * at offset 'dumplo' into the partition. 1037 */ 1038 int 1039 sddump(dev, blkno, va, size) 1040 dev_t dev; 1041 daddr_t blkno; 1042 caddr_t va; 1043 size_t size; 1044 { 1045 struct sd_softc *sd; /* disk unit to do the I/O */ 1046 struct disklabel *lp; /* disk's disklabel */ 1047 int unit, part; 1048 int sectorsize; /* size of a disk sector */ 1049 int nsects; /* number of sectors in partition */ 1050 int sectoff; /* sector offset of partition */ 1051 int totwrt; /* total number of sectors left to write */ 1052 int nwrt; /* current number of sectors to write */ 1053 struct scsipi_rw_big cmd; /* write command */ 1054 struct scsipi_xfer *xs; /* ... convenience */ 1055 int retval; 1056 1057 /* Check if recursive dump; if so, punt. */ 1058 if (sddoingadump) 1059 return (EFAULT); 1060 1061 /* Mark as active early. */ 1062 sddoingadump = 1; 1063 1064 unit = SDUNIT(dev); /* Decompose unit & partition. */ 1065 part = SDPART(dev); 1066 1067 /* Check for acceptable drive number. */ 1068 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL) 1069 return (ENXIO); 1070 1071 /* 1072 * XXX Can't do this check, since the media might have been 1073 * XXX marked `invalid' by successful unmounting of all 1074 * XXX filesystems. 1075 */ 1076 #if 0 1077 /* Make sure it was initialized. */ 1078 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) != SDEV_MEDIA_LOADED) 1079 return (ENXIO); 1080 #endif 1081 1082 /* Convert to disk sectors. Request must be a multiple of size. */ 1083 lp = sd->sc_dk.dk_label; 1084 sectorsize = lp->d_secsize; 1085 if ((size % sectorsize) != 0) 1086 return (EFAULT); 1087 totwrt = size / sectorsize; 1088 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */ 1089 1090 nsects = lp->d_partitions[part].p_size; 1091 sectoff = lp->d_partitions[part].p_offset; 1092 1093 /* Check transfer bounds against partition size. */ 1094 if ((blkno < 0) || ((blkno + totwrt) > nsects)) 1095 return (EINVAL); 1096 1097 /* Offset block number to start of partition. */ 1098 blkno += sectoff; 1099 1100 xs = &sx; 1101 1102 while (totwrt > 0) { 1103 nwrt = totwrt; /* XXX */ 1104 #ifndef SD_DUMP_NOT_TRUSTED 1105 /* 1106 * Fill out the scsi command 1107 */ 1108 bzero(&cmd, sizeof(cmd)); 1109 cmd.opcode = WRITE_BIG; 1110 _lto4b(blkno, cmd.addr); 1111 _lto2b(nwrt, cmd.length); 1112 /* 1113 * Fill out the scsipi_xfer structure 1114 * Note: we cannot sleep as we may be an interrupt 1115 * don't use scsipi_command() as it may want to wait 1116 * for an xs. 1117 */ 1118 bzero(xs, sizeof(sx)); 1119 xs->flags |= SCSI_AUTOCONF | INUSE | SCSI_DATA_OUT; 1120 xs->sc_link = sd->sc_link; 1121 xs->retries = SDRETRIES; 1122 xs->timeout = 10000; /* 10000 millisecs for a disk ! */ 1123 xs->cmd = (struct scsipi_generic *)&cmd; 1124 xs->cmdlen = sizeof(cmd); 1125 xs->resid = nwrt * sectorsize; 1126 xs->error = XS_NOERROR; 1127 xs->bp = 0; 1128 xs->data = va; 1129 xs->datalen = nwrt * sectorsize; 1130 1131 /* 1132 * Pass all this info to the scsi driver. 1133 */ 1134 retval = scsipi_command_direct(xs); 1135 if (retval != COMPLETE) 1136 return (ENXIO); 1137 #else /* SD_DUMP_NOT_TRUSTED */ 1138 /* Let's just talk about this first... */ 1139 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno); 1140 delay(500 * 1000); /* half a second */ 1141 #endif /* SD_DUMP_NOT_TRUSTED */ 1142 1143 /* update block count */ 1144 totwrt -= nwrt; 1145 blkno += nwrt; 1146 va += sectorsize * nwrt; 1147 } 1148 sddoingadump = 0; 1149 return (0); 1150 } 1151 #else /* __BDEVSW_DUMP_NEW_TYPE */ 1152 int 1153 sddump(dev, blkno, va, size) 1154 dev_t dev; 1155 daddr_t blkno; 1156 caddr_t va; 1157 size_t size; 1158 { 1159 1160 /* Not implemented. */ 1161 return (ENXIO); 1162 } 1163 #endif /* __BDEVSW_DUMP_NEW_TYPE */ 1164