1 /* $NetBSD: sd.c,v 1.188 2002/09/18 01:46:24 chs 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 <sys/cdefs.h> 57 __KERNEL_RCSID(0, "$NetBSD: sd.c,v 1.188 2002/09/18 01:46:24 chs Exp $"); 58 59 #include "opt_scsi.h" 60 #include "rnd.h" 61 62 #include <sys/param.h> 63 #include <sys/systm.h> 64 #include <sys/kernel.h> 65 #include <sys/file.h> 66 #include <sys/stat.h> 67 #include <sys/ioctl.h> 68 #include <sys/scsiio.h> 69 #include <sys/buf.h> 70 #include <sys/uio.h> 71 #include <sys/malloc.h> 72 #include <sys/errno.h> 73 #include <sys/device.h> 74 #include <sys/disklabel.h> 75 #include <sys/disk.h> 76 #include <sys/proc.h> 77 #include <sys/conf.h> 78 #include <sys/vnode.h> 79 #if NRND > 0 80 #include <sys/rnd.h> 81 #endif 82 83 #include <dev/scsipi/scsipi_all.h> 84 #include <dev/scsipi/scsi_all.h> 85 #include <dev/scsipi/scsipi_disk.h> 86 #include <dev/scsipi/scsi_disk.h> 87 #include <dev/scsipi/scsiconf.h> 88 #include <dev/scsipi/sdvar.h> 89 90 #include "sd.h" /* NSD_SCSIBUS and NSD_ATAPIBUS come from here */ 91 92 #define SDUNIT(dev) DISKUNIT(dev) 93 #define SDPART(dev) DISKPART(dev) 94 #define SDMINOR(unit, part) DISKMINOR(unit, part) 95 #define MAKESDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part) 96 97 #define SDLABELDEV(dev) (MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART)) 98 99 int sdlock __P((struct sd_softc *)); 100 void sdunlock __P((struct sd_softc *)); 101 void sdminphys __P((struct buf *)); 102 void sdgetdefaultlabel __P((struct sd_softc *, struct disklabel *)); 103 void sdgetdisklabel __P((struct sd_softc *)); 104 void sdstart __P((struct scsipi_periph *)); 105 void sddone __P((struct scsipi_xfer *)); 106 void sd_shutdown __P((void *)); 107 int sd_reassign_blocks __P((struct sd_softc *, u_long)); 108 int sd_interpret_sense __P((struct scsipi_xfer *)); 109 110 extern struct cfdriver sd_cd; 111 112 dev_type_open(sdopen); 113 dev_type_close(sdclose); 114 dev_type_read(sdread); 115 dev_type_write(sdwrite); 116 dev_type_ioctl(sdioctl); 117 dev_type_strategy(sdstrategy); 118 dev_type_dump(sddump); 119 dev_type_size(sdsize); 120 121 const struct bdevsw sd_bdevsw = { 122 sdopen, sdclose, sdstrategy, sdioctl, sddump, sdsize, D_DISK 123 }; 124 125 const struct cdevsw sd_cdevsw = { 126 sdopen, sdclose, sdread, sdwrite, sdioctl, 127 nostop, notty, nopoll, nommap, D_DISK 128 }; 129 130 struct dkdriver sddkdriver = { sdstrategy }; 131 132 const struct scsipi_periphsw sd_switch = { 133 sd_interpret_sense, /* check our error handler first */ 134 sdstart, /* have a queue, served by this */ 135 NULL, /* have no async handler */ 136 sddone, /* deal with stats at interrupt time */ 137 }; 138 139 /* 140 * Attach routine common to atapi & scsi. 141 */ 142 void 143 sdattach(parent, sd, periph, ops) 144 struct device *parent; 145 struct sd_softc *sd; 146 struct scsipi_periph *periph; 147 const struct sd_ops *ops; 148 { 149 int error, result; 150 struct disk_parms *dp = &sd->params; 151 char pbuf[9]; 152 153 SC_DEBUG(periph, SCSIPI_DB2, ("sdattach: ")); 154 155 bufq_alloc(&sd->buf_queue, BUFQ_DISKSORT|BUFQ_SORT_RAWBLOCK); 156 157 /* 158 * Store information needed to contact our base driver 159 */ 160 sd->sc_periph = periph; 161 sd->sc_ops = ops; 162 163 periph->periph_dev = &sd->sc_dev; 164 periph->periph_switch = &sd_switch; 165 166 /* 167 * Increase our openings to the maximum-per-periph 168 * supported by the adapter. This will either be 169 * clamped down or grown by the adapter if necessary. 170 */ 171 periph->periph_openings = 172 SCSIPI_CHAN_MAX_PERIPH(periph->periph_channel); 173 periph->periph_flags |= PERIPH_GROW_OPENINGS; 174 175 /* 176 * Initialize and attach the disk structure. 177 */ 178 sd->sc_dk.dk_driver = &sddkdriver; 179 sd->sc_dk.dk_name = sd->sc_dev.dv_xname; 180 disk_attach(&sd->sc_dk); 181 182 /* 183 * Use the subdriver to request information regarding the drive. 184 */ 185 printf("\n"); 186 187 error = scsipi_start(periph, SSS_START, 188 XS_CTL_DISCOVERY | XS_CTL_IGNORE_ILLEGAL_REQUEST | 189 XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_SILENT); 190 191 if (error) 192 result = SDGP_RESULT_OFFLINE; 193 else 194 result = (*sd->sc_ops->sdo_get_parms)(sd, &sd->params, 195 XS_CTL_DISCOVERY); 196 printf("%s: ", sd->sc_dev.dv_xname); 197 switch (result) { 198 case SDGP_RESULT_OK: 199 format_bytes(pbuf, sizeof(pbuf), 200 (u_int64_t)dp->disksize * dp->blksize); 201 printf( 202 "%s, %ld cyl, %ld head, %ld sec, %ld bytes/sect x %lu sectors", 203 pbuf, dp->cyls, dp->heads, dp->sectors, dp->blksize, 204 dp->disksize); 205 break; 206 207 case SDGP_RESULT_OFFLINE: 208 printf("drive offline"); 209 break; 210 211 case SDGP_RESULT_UNFORMATTED: 212 printf("unformatted media"); 213 break; 214 215 #ifdef DIAGNOSTIC 216 default: 217 panic("sdattach: unknown result from get_parms"); 218 break; 219 #endif 220 } 221 printf("\n"); 222 223 /* 224 * Establish a shutdown hook so that we can ensure that 225 * our data has actually made it onto the platter at 226 * shutdown time. Note that this relies on the fact 227 * that the shutdown hook code puts us at the head of 228 * the list (thus guaranteeing that our hook runs before 229 * our ancestors'). 230 */ 231 if ((sd->sc_sdhook = 232 shutdownhook_establish(sd_shutdown, sd)) == NULL) 233 printf("%s: WARNING: unable to establish shutdown hook\n", 234 sd->sc_dev.dv_xname); 235 236 #if NRND > 0 237 /* 238 * attach the device into the random source list 239 */ 240 rnd_attach_source(&sd->rnd_source, sd->sc_dev.dv_xname, 241 RND_TYPE_DISK, 0); 242 #endif 243 } 244 245 int 246 sdactivate(self, act) 247 struct device *self; 248 enum devact act; 249 { 250 int rv = 0; 251 252 switch (act) { 253 case DVACT_ACTIVATE: 254 rv = EOPNOTSUPP; 255 break; 256 257 case DVACT_DEACTIVATE: 258 /* 259 * Nothing to do; we key off the device's DVF_ACTIVE. 260 */ 261 break; 262 } 263 return (rv); 264 } 265 266 int 267 sddetach(self, flags) 268 struct device *self; 269 int flags; 270 { 271 struct sd_softc *sd = (struct sd_softc *) self; 272 struct buf *bp; 273 int s, bmaj, cmaj, i, mn; 274 275 /* locate the major number */ 276 bmaj = bdevsw_lookup_major(&sd_bdevsw); 277 cmaj = cdevsw_lookup_major(&sd_cdevsw); 278 279 s = splbio(); 280 281 /* Kill off any queued buffers. */ 282 while ((bp = BUFQ_GET(&sd->buf_queue)) != NULL) { 283 bp->b_error = EIO; 284 bp->b_flags |= B_ERROR; 285 bp->b_resid = bp->b_bcount; 286 biodone(bp); 287 } 288 289 bufq_free(&sd->buf_queue); 290 291 /* Kill off any pending commands. */ 292 scsipi_kill_pending(sd->sc_periph); 293 294 splx(s); 295 296 /* Nuke the vnodes for any open instances */ 297 for (i = 0; i < MAXPARTITIONS; i++) { 298 mn = SDMINOR(self->dv_unit, i); 299 vdevgone(bmaj, mn, mn, VBLK); 300 vdevgone(cmaj, mn, mn, VCHR); 301 } 302 303 /* Detach from the disk list. */ 304 disk_detach(&sd->sc_dk); 305 306 /* Get rid of the shutdown hook. */ 307 shutdownhook_disestablish(sd->sc_sdhook); 308 309 #if NRND > 0 310 /* Unhook the entropy source. */ 311 rnd_detach_source(&sd->rnd_source); 312 #endif 313 314 return (0); 315 } 316 317 /* 318 * Wait interruptibly for an exclusive lock. 319 * 320 * XXX 321 * Several drivers do this; it should be abstracted and made MP-safe. 322 */ 323 int 324 sdlock(sd) 325 struct sd_softc *sd; 326 { 327 int error; 328 329 while ((sd->flags & SDF_LOCKED) != 0) { 330 sd->flags |= SDF_WANTED; 331 if ((error = tsleep(sd, PRIBIO | PCATCH, "sdlck", 0)) != 0) 332 return (error); 333 } 334 sd->flags |= SDF_LOCKED; 335 return (0); 336 } 337 338 /* 339 * Unlock and wake up any waiters. 340 */ 341 void 342 sdunlock(sd) 343 struct sd_softc *sd; 344 { 345 346 sd->flags &= ~SDF_LOCKED; 347 if ((sd->flags & SDF_WANTED) != 0) { 348 sd->flags &= ~SDF_WANTED; 349 wakeup(sd); 350 } 351 } 352 353 /* 354 * open the device. Make sure the partition info is a up-to-date as can be. 355 */ 356 int 357 sdopen(dev, flag, fmt, p) 358 dev_t dev; 359 int flag, fmt; 360 struct proc *p; 361 { 362 struct sd_softc *sd; 363 struct scsipi_periph *periph; 364 struct scsipi_adapter *adapt; 365 int unit, part; 366 int error; 367 368 unit = SDUNIT(dev); 369 if (unit >= sd_cd.cd_ndevs) 370 return (ENXIO); 371 sd = sd_cd.cd_devs[unit]; 372 if (sd == NULL) 373 return (ENXIO); 374 375 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0) 376 return (ENODEV); 377 378 periph = sd->sc_periph; 379 adapt = periph->periph_channel->chan_adapter; 380 part = SDPART(dev); 381 382 SC_DEBUG(periph, SCSIPI_DB1, 383 ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit, 384 sd_cd.cd_ndevs, part)); 385 386 /* 387 * If this is the first open of this device, add a reference 388 * to the adapter. 389 */ 390 if (sd->sc_dk.dk_openmask == 0 && 391 (error = scsipi_adapter_addref(adapt)) != 0) 392 return (error); 393 394 if ((error = sdlock(sd)) != 0) 395 goto bad4; 396 397 if ((periph->periph_flags & PERIPH_OPEN) != 0) { 398 /* 399 * If any partition is open, but the disk has been invalidated, 400 * disallow further opens of non-raw partition 401 */ 402 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 && 403 (part != RAW_PART || fmt != S_IFCHR)) { 404 error = EIO; 405 goto bad3; 406 } 407 } else { 408 /* Check that it is still responding and ok. */ 409 error = scsipi_test_unit_ready(periph, 410 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE | 411 XS_CTL_IGNORE_NOT_READY); 412 if (error) 413 goto bad3; 414 415 /* 416 * Start the pack spinning if necessary. Always allow the 417 * raw parition to be opened, for raw IOCTLs. Data transfers 418 * will check for SDEV_MEDIA_LOADED. 419 */ 420 error = scsipi_start(periph, SSS_START, 421 XS_CTL_IGNORE_ILLEGAL_REQUEST | 422 XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_SILENT); 423 if (error) { 424 if (part != RAW_PART || fmt != S_IFCHR) 425 goto bad3; 426 else 427 goto out; 428 } 429 430 periph->periph_flags |= PERIPH_OPEN; 431 432 if (periph->periph_flags & PERIPH_REMOVABLE) { 433 /* Lock the pack in. */ 434 error = scsipi_prevent(periph, PR_PREVENT, 435 XS_CTL_IGNORE_ILLEGAL_REQUEST | 436 XS_CTL_IGNORE_MEDIA_CHANGE); 437 if (error) 438 goto bad; 439 } 440 441 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 442 periph->periph_flags |= PERIPH_MEDIA_LOADED; 443 444 /* 445 * Load the physical device parameters. 446 * 447 * Note that if media is present but unformatted, 448 * we allow the open (so that it can be formatted!). 449 * The drive should refuse real I/O, if the media is 450 * unformatted. 451 */ 452 if ((*sd->sc_ops->sdo_get_parms)(sd, &sd->params, 453 0) == SDGP_RESULT_OFFLINE) { 454 error = ENXIO; 455 goto bad2; 456 } 457 SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded ")); 458 459 /* Load the partition info if not already loaded. */ 460 sdgetdisklabel(sd); 461 SC_DEBUG(periph, SCSIPI_DB3, ("Disklabel loaded ")); 462 } 463 } 464 465 /* Check that the partition exists. */ 466 if (part != RAW_PART && 467 (part >= sd->sc_dk.dk_label->d_npartitions || 468 sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) { 469 error = ENXIO; 470 goto bad; 471 } 472 473 out: /* Insure only one open at a time. */ 474 switch (fmt) { 475 case S_IFCHR: 476 sd->sc_dk.dk_copenmask |= (1 << part); 477 break; 478 case S_IFBLK: 479 sd->sc_dk.dk_bopenmask |= (1 << part); 480 break; 481 } 482 sd->sc_dk.dk_openmask = 483 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask; 484 485 SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n")); 486 sdunlock(sd); 487 return (0); 488 489 bad2: 490 periph->periph_flags &= ~PERIPH_MEDIA_LOADED; 491 492 bad: 493 if (sd->sc_dk.dk_openmask == 0) { 494 scsipi_prevent(periph, PR_ALLOW, 495 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE); 496 periph->periph_flags &= ~PERIPH_OPEN; 497 } 498 499 bad3: 500 sdunlock(sd); 501 bad4: 502 if (sd->sc_dk.dk_openmask == 0) 503 scsipi_adapter_delref(adapt); 504 return (error); 505 } 506 507 /* 508 * close the device.. only called if we are the LAST occurence of an open 509 * device. Convenient now but usually a pain. 510 */ 511 int 512 sdclose(dev, flag, fmt, p) 513 dev_t dev; 514 int flag, fmt; 515 struct proc *p; 516 { 517 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)]; 518 struct scsipi_periph *periph = sd->sc_periph; 519 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; 520 int part = SDPART(dev); 521 int error; 522 523 if ((error = sdlock(sd)) != 0) 524 return (error); 525 526 switch (fmt) { 527 case S_IFCHR: 528 sd->sc_dk.dk_copenmask &= ~(1 << part); 529 break; 530 case S_IFBLK: 531 sd->sc_dk.dk_bopenmask &= ~(1 << part); 532 break; 533 } 534 sd->sc_dk.dk_openmask = 535 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask; 536 537 if (sd->sc_dk.dk_openmask == 0) { 538 /* 539 * If the disk cache needs flushing, and the disk supports 540 * it, do it now. 541 */ 542 if ((sd->flags & SDF_DIRTY) != 0 && 543 sd->sc_ops->sdo_flush != NULL) { 544 if ((*sd->sc_ops->sdo_flush)(sd, 0)) { 545 printf("%s: cache synchronization failed\n", 546 sd->sc_dev.dv_xname); 547 sd->flags &= ~SDF_FLUSHING; 548 } else 549 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 550 } 551 552 if (! (periph->periph_flags & PERIPH_KEEP_LABEL)) 553 periph->periph_flags &= ~PERIPH_MEDIA_LOADED; 554 555 scsipi_wait_drain(periph); 556 557 if (periph->periph_flags & PERIPH_REMOVABLE) { 558 scsipi_prevent(periph, PR_ALLOW, 559 XS_CTL_IGNORE_ILLEGAL_REQUEST | 560 XS_CTL_IGNORE_NOT_READY); 561 } 562 periph->periph_flags &= ~PERIPH_OPEN; 563 564 scsipi_wait_drain(periph); 565 566 scsipi_adapter_delref(adapt); 567 } 568 569 sdunlock(sd); 570 return (0); 571 } 572 573 /* 574 * Actually translate the requested transfer into one the physical driver 575 * can understand. The transfer is described by a buf and will include 576 * only one physical transfer. 577 */ 578 void 579 sdstrategy(bp) 580 struct buf *bp; 581 { 582 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)]; 583 struct scsipi_periph *periph = sd->sc_periph; 584 struct disklabel *lp; 585 daddr_t blkno; 586 int s; 587 boolean_t sector_aligned; 588 589 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdstrategy ")); 590 SC_DEBUG(sd->sc_periph, SCSIPI_DB1, 591 ("%ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno)); 592 /* 593 * If the device has been made invalid, error out 594 */ 595 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 || 596 (sd->sc_dev.dv_flags & DVF_ACTIVE) == 0) { 597 if (periph->periph_flags & PERIPH_OPEN) 598 bp->b_error = EIO; 599 else 600 bp->b_error = ENODEV; 601 goto bad; 602 } 603 604 lp = sd->sc_dk.dk_label; 605 606 /* 607 * The transfer must be a whole number of blocks, offset must not be 608 * negative. 609 */ 610 if (lp->d_secsize == DEV_BSIZE) { 611 sector_aligned = (bp->b_bcount & (DEV_BSIZE - 1)) == 0; 612 } else { 613 sector_aligned = (bp->b_bcount % lp->d_secsize) == 0; 614 } 615 if (!sector_aligned || bp->b_blkno < 0) { 616 bp->b_error = EINVAL; 617 goto bad; 618 } 619 /* 620 * If it's a null transfer, return immediatly 621 */ 622 if (bp->b_bcount == 0) 623 goto done; 624 625 /* 626 * Do bounds checking, adjust transfer. if error, process. 627 * If end of partition, just return. 628 */ 629 if (SDPART(bp->b_dev) != RAW_PART && 630 bounds_check_with_label(bp, lp, 631 (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0) 632 goto done; 633 634 /* 635 * Now convert the block number to absolute and put it in 636 * terms of the device's logical block size. 637 */ 638 if (lp->d_secsize == DEV_BSIZE) 639 blkno = bp->b_blkno; 640 else if (lp->d_secsize > DEV_BSIZE) 641 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE); 642 else 643 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize); 644 645 if (SDPART(bp->b_dev) != RAW_PART) 646 blkno += lp->d_partitions[SDPART(bp->b_dev)].p_offset; 647 648 bp->b_rawblkno = blkno; 649 650 s = splbio(); 651 652 /* 653 * Place it in the queue of disk activities for this disk. 654 * 655 * XXX Only do disksort() if the current operating mode does not 656 * XXX include tagged queueing. 657 */ 658 BUFQ_PUT(&sd->buf_queue, bp); 659 660 /* 661 * Tell the device to get going on the transfer if it's 662 * not doing anything, otherwise just wait for completion 663 */ 664 sdstart(sd->sc_periph); 665 666 splx(s); 667 return; 668 669 bad: 670 bp->b_flags |= B_ERROR; 671 done: 672 /* 673 * Correctly set the buf to indicate a completed xfer 674 */ 675 bp->b_resid = bp->b_bcount; 676 biodone(bp); 677 } 678 679 /* 680 * sdstart looks to see if there is a buf waiting for the device 681 * and that the device is not already busy. If both are true, 682 * It dequeues the buf and creates a scsi command to perform the 683 * transfer in the buf. The transfer request will call scsipi_done 684 * on completion, which will in turn call this routine again 685 * so that the next queued transfer is performed. 686 * The bufs are queued by the strategy routine (sdstrategy) 687 * 688 * This routine is also called after other non-queued requests 689 * have been made of the scsi driver, to ensure that the queue 690 * continues to be drained. 691 * 692 * must be called at the correct (highish) spl level 693 * sdstart() is called at splbio from sdstrategy and scsipi_done 694 */ 695 void 696 sdstart(periph) 697 struct scsipi_periph *periph; 698 { 699 struct sd_softc *sd = (void *)periph->periph_dev; 700 struct disklabel *lp = sd->sc_dk.dk_label; 701 struct buf *bp = 0; 702 struct scsipi_rw_big cmd_big; 703 #if NSD_SCSIBUS > 0 704 struct scsi_rw cmd_small; 705 #endif 706 struct scsipi_generic *cmdp; 707 int nblks, cmdlen, error, flags; 708 709 SC_DEBUG(periph, SCSIPI_DB2, ("sdstart ")); 710 /* 711 * Check if the device has room for another command 712 */ 713 while (periph->periph_active < periph->periph_openings) { 714 /* 715 * there is excess capacity, but a special waits 716 * It'll need the adapter as soon as we clear out of the 717 * way and let it run (user level wait). 718 */ 719 if (periph->periph_flags & PERIPH_WAITING) { 720 periph->periph_flags &= ~PERIPH_WAITING; 721 wakeup((caddr_t)periph); 722 return; 723 } 724 725 /* 726 * See if there is a buf with work for us to do.. 727 */ 728 if ((bp = BUFQ_GET(&sd->buf_queue)) == NULL) 729 return; 730 731 /* 732 * If the device has become invalid, abort all the 733 * reads and writes until all files have been closed and 734 * re-opened 735 */ 736 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 737 bp->b_error = EIO; 738 bp->b_flags |= B_ERROR; 739 bp->b_resid = bp->b_bcount; 740 biodone(bp); 741 continue; 742 } 743 744 /* 745 * We have a buf, now we should make a command. 746 */ 747 748 if (lp->d_secsize == DEV_BSIZE) 749 nblks = bp->b_bcount >> DEV_BSHIFT; 750 else 751 nblks = howmany(bp->b_bcount, lp->d_secsize); 752 753 #if NSD_SCSIBUS > 0 754 /* 755 * Fill out the scsi command. If the transfer will 756 * fit in a "small" cdb, use it. 757 */ 758 if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) && 759 ((nblks & 0xff) == nblks) && 760 !(periph->periph_quirks & PQUIRK_ONLYBIG) && 761 scsipi_periph_bustype(periph) == SCSIPI_BUSTYPE_SCSI) { 762 /* 763 * We can fit in a small cdb. 764 */ 765 memset(&cmd_small, 0, sizeof(cmd_small)); 766 cmd_small.opcode = (bp->b_flags & B_READ) ? 767 SCSI_READ_COMMAND : SCSI_WRITE_COMMAND; 768 _lto3b(bp->b_rawblkno, cmd_small.addr); 769 cmd_small.length = nblks & 0xff; 770 cmdlen = sizeof(cmd_small); 771 cmdp = (struct scsipi_generic *)&cmd_small; 772 } else 773 #endif /* NSD_SCSIBUS > 0 */ 774 { 775 /* 776 * Need a large cdb. 777 */ 778 memset(&cmd_big, 0, sizeof(cmd_big)); 779 cmd_big.opcode = (bp->b_flags & B_READ) ? 780 READ_BIG : WRITE_BIG; 781 _lto4b(bp->b_rawblkno, cmd_big.addr); 782 _lto2b(nblks, cmd_big.length); 783 cmdlen = sizeof(cmd_big); 784 cmdp = (struct scsipi_generic *)&cmd_big; 785 } 786 787 /* Instrumentation. */ 788 disk_busy(&sd->sc_dk); 789 790 /* 791 * Mark the disk dirty so that the cache will be 792 * flushed on close. 793 */ 794 if ((bp->b_flags & B_READ) == 0) 795 sd->flags |= SDF_DIRTY; 796 797 /* 798 * Figure out what flags to use. 799 */ 800 flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_SIMPLE_TAG; 801 if (bp->b_flags & B_READ) 802 flags |= XS_CTL_DATA_IN; 803 else 804 flags |= XS_CTL_DATA_OUT; 805 806 /* 807 * Call the routine that chats with the adapter. 808 * Note: we cannot sleep as we may be an interrupt 809 */ 810 error = scsipi_command(periph, cmdp, cmdlen, 811 (u_char *)bp->b_data, bp->b_bcount, 812 SDRETRIES, SD_IO_TIMEOUT, bp, flags); 813 if (error) { 814 disk_unbusy(&sd->sc_dk, 0); 815 printf("%s: not queued, error %d\n", 816 sd->sc_dev.dv_xname, error); 817 } 818 } 819 } 820 821 void 822 sddone(xs) 823 struct scsipi_xfer *xs; 824 { 825 struct sd_softc *sd = (void *)xs->xs_periph->periph_dev; 826 827 if (sd->flags & SDF_FLUSHING) { 828 /* Flush completed, no longer dirty. */ 829 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 830 } 831 832 if (xs->bp != NULL) { 833 disk_unbusy(&sd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid); 834 #if NRND > 0 835 rnd_add_uint32(&sd->rnd_source, xs->bp->b_rawblkno); 836 #endif 837 } 838 } 839 840 void 841 sdminphys(bp) 842 struct buf *bp; 843 { 844 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)]; 845 long max; 846 847 /* 848 * If the device is ancient, we want to make sure that 849 * the transfer fits into a 6-byte cdb. 850 * 851 * XXX Note that the SCSI-I spec says that 256-block transfers 852 * are allowed in a 6-byte read/write, and are specified 853 * by settng the "length" to 0. However, we're conservative 854 * here, allowing only 255-block transfers in case an 855 * ancient device gets confused by length == 0. A length of 0 856 * in a 10-byte read/write actually means 0 blocks. 857 */ 858 if ((sd->flags & SDF_ANCIENT) && 859 ((sd->sc_periph->periph_flags & 860 (PERIPH_REMOVABLE | PERIPH_MEDIA_LOADED)) != PERIPH_REMOVABLE)) { 861 max = sd->sc_dk.dk_label->d_secsize * 0xff; 862 863 if (bp->b_bcount > max) 864 bp->b_bcount = max; 865 } 866 867 (*sd->sc_periph->periph_channel->chan_adapter->adapt_minphys)(bp); 868 } 869 870 int 871 sdread(dev, uio, ioflag) 872 dev_t dev; 873 struct uio *uio; 874 int ioflag; 875 { 876 877 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio)); 878 } 879 880 int 881 sdwrite(dev, uio, ioflag) 882 dev_t dev; 883 struct uio *uio; 884 int ioflag; 885 { 886 887 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio)); 888 } 889 890 /* 891 * Perform special action on behalf of the user 892 * Knows about the internals of this device 893 */ 894 int 895 sdioctl(dev, cmd, addr, flag, p) 896 dev_t dev; 897 u_long cmd; 898 caddr_t addr; 899 int flag; 900 struct proc *p; 901 { 902 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)]; 903 struct scsipi_periph *periph = sd->sc_periph; 904 int part = SDPART(dev); 905 int error; 906 #ifdef __HAVE_OLD_DISKLABEL 907 struct disklabel newlabel; 908 #endif 909 910 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdioctl 0x%lx ", cmd)); 911 912 /* 913 * If the device is not valid, some IOCTLs can still be 914 * handled on the raw partition. Check this here. 915 */ 916 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 917 switch (cmd) { 918 case DIOCKLABEL: 919 case DIOCWLABEL: 920 case DIOCLOCK: 921 case DIOCEJECT: 922 case ODIOCEJECT: 923 case DIOCGCACHE: 924 case DIOCSCACHE: 925 case SCIOCIDENTIFY: 926 case OSCIOCIDENTIFY: 927 case SCIOCCOMMAND: 928 case SCIOCDEBUG: 929 if (part == RAW_PART) 930 break; 931 /* FALLTHROUGH */ 932 default: 933 if ((periph->periph_flags & PERIPH_OPEN) == 0) 934 return (ENODEV); 935 else 936 return (EIO); 937 } 938 } 939 940 switch (cmd) { 941 case DIOCGDINFO: 942 *(struct disklabel *)addr = *(sd->sc_dk.dk_label); 943 return (0); 944 945 #ifdef __HAVE_OLD_DISKLABEL 946 case ODIOCGDINFO: 947 newlabel = *(sd->sc_dk.dk_label); 948 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 949 return ENOTTY; 950 memcpy(addr, &newlabel, sizeof (struct olddisklabel)); 951 return (0); 952 #endif 953 954 case DIOCGPART: 955 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label; 956 ((struct partinfo *)addr)->part = 957 &sd->sc_dk.dk_label->d_partitions[part]; 958 return (0); 959 960 case DIOCWDINFO: 961 case DIOCSDINFO: 962 #ifdef __HAVE_OLD_DISKLABEL 963 case ODIOCWDINFO: 964 case ODIOCSDINFO: 965 #endif 966 { 967 struct disklabel *lp; 968 969 #ifdef __HAVE_OLD_DISKLABEL 970 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) { 971 memset(&newlabel, 0, sizeof newlabel); 972 memcpy(&newlabel, addr, sizeof (struct olddisklabel)); 973 lp = &newlabel; 974 } else 975 #endif 976 lp = (struct disklabel *)addr; 977 978 if ((flag & FWRITE) == 0) 979 return (EBADF); 980 981 if ((error = sdlock(sd)) != 0) 982 return (error); 983 sd->flags |= SDF_LABELLING; 984 985 error = setdisklabel(sd->sc_dk.dk_label, 986 lp, /*sd->sc_dk.dk_openmask : */0, 987 sd->sc_dk.dk_cpulabel); 988 if (error == 0) { 989 if (cmd == DIOCWDINFO 990 #ifdef __HAVE_OLD_DISKLABEL 991 || cmd == ODIOCWDINFO 992 #endif 993 ) 994 error = writedisklabel(SDLABELDEV(dev), 995 sdstrategy, sd->sc_dk.dk_label, 996 sd->sc_dk.dk_cpulabel); 997 } 998 999 sd->flags &= ~SDF_LABELLING; 1000 sdunlock(sd); 1001 return (error); 1002 } 1003 1004 case DIOCKLABEL: 1005 if (*(int *)addr) 1006 periph->periph_flags |= PERIPH_KEEP_LABEL; 1007 else 1008 periph->periph_flags &= ~PERIPH_KEEP_LABEL; 1009 return (0); 1010 1011 case DIOCWLABEL: 1012 if ((flag & FWRITE) == 0) 1013 return (EBADF); 1014 if (*(int *)addr) 1015 sd->flags |= SDF_WLABEL; 1016 else 1017 sd->flags &= ~SDF_WLABEL; 1018 return (0); 1019 1020 case DIOCLOCK: 1021 return (scsipi_prevent(periph, 1022 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0)); 1023 1024 case DIOCEJECT: 1025 if ((periph->periph_flags & PERIPH_REMOVABLE) == 0) 1026 return (ENOTTY); 1027 if (*(int *)addr == 0) { 1028 /* 1029 * Don't force eject: check that we are the only 1030 * partition open. If so, unlock it. 1031 */ 1032 if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 && 1033 sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask == 1034 sd->sc_dk.dk_openmask) { 1035 error = scsipi_prevent(periph, PR_ALLOW, 1036 XS_CTL_IGNORE_NOT_READY); 1037 if (error) 1038 return (error); 1039 } else { 1040 return (EBUSY); 1041 } 1042 } 1043 /* FALLTHROUGH */ 1044 case ODIOCEJECT: 1045 return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ? 1046 ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0)); 1047 1048 case DIOCGDEFLABEL: 1049 sdgetdefaultlabel(sd, (struct disklabel *)addr); 1050 return (0); 1051 1052 #ifdef __HAVE_OLD_DISKLABEL 1053 case ODIOCGDEFLABEL: 1054 sdgetdefaultlabel(sd, &newlabel); 1055 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 1056 return ENOTTY; 1057 memcpy(addr, &newlabel, sizeof (struct olddisklabel)); 1058 return (0); 1059 #endif 1060 1061 case DIOCGCACHE: 1062 if (sd->sc_ops->sdo_getcache != NULL) 1063 return ((*sd->sc_ops->sdo_getcache)(sd, (int *) addr)); 1064 1065 /* Not supported on this device. */ 1066 *(int *) addr = 0; 1067 return (0); 1068 1069 case DIOCSCACHE: 1070 if ((flag & FWRITE) == 0) 1071 return (EBADF); 1072 if (sd->sc_ops->sdo_setcache != NULL) 1073 return ((*sd->sc_ops->sdo_setcache)(sd, *(int *) addr)); 1074 1075 /* Not supported on this device. */ 1076 return (EOPNOTSUPP); 1077 1078 case DIOCCACHESYNC: 1079 /* 1080 * XXX Do we really need to care about having a writeable 1081 * file descriptor here? 1082 */ 1083 if ((flag & FWRITE) == 0) 1084 return (EBADF); 1085 if (((sd->flags & SDF_DIRTY) != 0 || *(int *)addr != 0) && 1086 sd->sc_ops->sdo_flush != NULL) { 1087 error = (*sd->sc_ops->sdo_flush)(sd, 0); 1088 if (error) 1089 sd->flags &= ~SDF_FLUSHING; 1090 else 1091 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 1092 } else 1093 error = 0; 1094 return (error); 1095 1096 default: 1097 if (part != RAW_PART) 1098 return (ENOTTY); 1099 return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, p)); 1100 } 1101 1102 #ifdef DIAGNOSTIC 1103 panic("sdioctl: impossible"); 1104 #endif 1105 } 1106 1107 void 1108 sdgetdefaultlabel(sd, lp) 1109 struct sd_softc *sd; 1110 struct disklabel *lp; 1111 { 1112 1113 memset(lp, 0, sizeof(struct disklabel)); 1114 1115 lp->d_secsize = sd->params.blksize; 1116 lp->d_ntracks = sd->params.heads; 1117 lp->d_nsectors = sd->params.sectors; 1118 lp->d_ncylinders = sd->params.cyls; 1119 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 1120 1121 switch (scsipi_periph_bustype(sd->sc_periph)) { 1122 #if NSD_SCSIBUS > 0 1123 case SCSIPI_BUSTYPE_SCSI: 1124 lp->d_type = DTYPE_SCSI; 1125 break; 1126 #endif 1127 #if NSD_ATAPIBUS > 0 1128 case SCSIPI_BUSTYPE_ATAPI: 1129 lp->d_type = DTYPE_ATAPI; 1130 break; 1131 #endif 1132 } 1133 strncpy(lp->d_typename, sd->name, 16); 1134 strncpy(lp->d_packname, "fictitious", 16); 1135 lp->d_secperunit = sd->params.disksize; 1136 lp->d_rpm = sd->params.rot_rate; 1137 lp->d_interleave = 1; 1138 lp->d_flags = 0; 1139 1140 lp->d_partitions[RAW_PART].p_offset = 0; 1141 lp->d_partitions[RAW_PART].p_size = 1142 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 1143 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; 1144 lp->d_npartitions = RAW_PART + 1; 1145 1146 lp->d_magic = DISKMAGIC; 1147 lp->d_magic2 = DISKMAGIC; 1148 lp->d_checksum = dkcksum(lp); 1149 } 1150 1151 1152 /* 1153 * Load the label information on the named device 1154 */ 1155 void 1156 sdgetdisklabel(sd) 1157 struct sd_softc *sd; 1158 { 1159 struct disklabel *lp = sd->sc_dk.dk_label; 1160 char *errstring; 1161 1162 memset(sd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); 1163 1164 sdgetdefaultlabel(sd, lp); 1165 1166 if (lp->d_secpercyl == 0) { 1167 lp->d_secpercyl = 100; 1168 /* as long as it's not 0 - readdisklabel divides by it (?) */ 1169 } 1170 1171 /* 1172 * Call the generic disklabel extraction routine 1173 */ 1174 errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART), 1175 sdstrategy, lp, sd->sc_dk.dk_cpulabel); 1176 if (errstring) { 1177 printf("%s: %s\n", sd->sc_dev.dv_xname, errstring); 1178 return; 1179 } 1180 } 1181 1182 void 1183 sd_shutdown(arg) 1184 void *arg; 1185 { 1186 struct sd_softc *sd = arg; 1187 1188 /* 1189 * If the disk cache needs to be flushed, and the disk supports 1190 * it, flush it. We're cold at this point, so we poll for 1191 * completion. 1192 */ 1193 if ((sd->flags & SDF_DIRTY) != 0 && sd->sc_ops->sdo_flush != NULL) { 1194 if ((*sd->sc_ops->sdo_flush)(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) { 1195 printf("%s: cache synchronization failed\n", 1196 sd->sc_dev.dv_xname); 1197 sd->flags &= ~SDF_FLUSHING; 1198 } else 1199 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 1200 } 1201 } 1202 1203 /* 1204 * Tell the device to map out a defective block 1205 */ 1206 int 1207 sd_reassign_blocks(sd, blkno) 1208 struct sd_softc *sd; 1209 u_long blkno; 1210 { 1211 struct scsi_reassign_blocks scsipi_cmd; 1212 struct scsi_reassign_blocks_data rbdata; 1213 1214 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1215 memset(&rbdata, 0, sizeof(rbdata)); 1216 scsipi_cmd.opcode = SCSI_REASSIGN_BLOCKS; 1217 1218 _lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length); 1219 _lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr); 1220 1221 return (scsipi_command(sd->sc_periph, 1222 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1223 (u_char *)&rbdata, sizeof(rbdata), SDRETRIES, 5000, NULL, 1224 XS_CTL_DATA_OUT | XS_CTL_DATA_ONSTACK)); 1225 } 1226 1227 /* 1228 * Check Errors 1229 */ 1230 int 1231 sd_interpret_sense(xs) 1232 struct scsipi_xfer *xs; 1233 { 1234 struct scsipi_periph *periph = xs->xs_periph; 1235 struct scsipi_sense_data *sense = &xs->sense.scsi_sense; 1236 struct sd_softc *sd = (void *)periph->periph_dev; 1237 int s, error, retval = EJUSTRETURN; 1238 1239 /* 1240 * If the periph is already recovering, just do the normal 1241 * error processing. 1242 */ 1243 if (periph->periph_flags & PERIPH_RECOVERING) 1244 return (retval); 1245 1246 /* 1247 * If the device is not open yet, let the generic code handle it. 1248 */ 1249 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) 1250 return (retval); 1251 1252 /* 1253 * If it isn't a extended or extended/deferred error, let 1254 * the generic code handle it. 1255 */ 1256 if ((sense->error_code & SSD_ERRCODE) != 0x70 && 1257 (sense->error_code & SSD_ERRCODE) != 0x71) 1258 return (retval); 1259 1260 if ((sense->flags & SSD_KEY) == SKEY_NOT_READY && 1261 sense->add_sense_code == 0x4) { 1262 if (sense->add_sense_code_qual == 0x01) { 1263 /* 1264 * Unit In The Process Of Becoming Ready. 1265 */ 1266 printf("%s: waiting for pack to spin up...\n", 1267 sd->sc_dev.dv_xname); 1268 if (!callout_active(&periph->periph_callout)) 1269 scsipi_periph_freeze(periph, 1); 1270 callout_reset(&periph->periph_callout, 1271 5 * hz, scsipi_periph_timed_thaw, periph); 1272 retval = ERESTART; 1273 } else if ((sense->add_sense_code_qual == 0x2) && 1274 (periph->periph_quirks & PQUIRK_NOSTARTUNIT) == 0) { 1275 printf("%s: pack is stopped, restarting...\n", 1276 sd->sc_dev.dv_xname); 1277 s = splbio(); 1278 periph->periph_flags |= PERIPH_RECOVERING; 1279 splx(s); 1280 error = scsipi_start(periph, SSS_START, 1281 XS_CTL_URGENT|XS_CTL_HEAD_TAG| 1282 XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH); 1283 if (error) { 1284 printf("%s: unable to restart pack\n", 1285 sd->sc_dev.dv_xname); 1286 retval = error; 1287 } else 1288 retval = ERESTART; 1289 s = splbio(); 1290 periph->periph_flags &= ~PERIPH_RECOVERING; 1291 splx(s); 1292 } 1293 } 1294 return (retval); 1295 } 1296 1297 1298 int 1299 sdsize(dev) 1300 dev_t dev; 1301 { 1302 struct sd_softc *sd; 1303 int part, unit, omask; 1304 int size; 1305 1306 unit = SDUNIT(dev); 1307 if (unit >= sd_cd.cd_ndevs) 1308 return (-1); 1309 sd = sd_cd.cd_devs[unit]; 1310 if (sd == NULL) 1311 return (-1); 1312 1313 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0) 1314 return (-1); 1315 1316 part = SDPART(dev); 1317 omask = sd->sc_dk.dk_openmask & (1 << part); 1318 1319 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0) 1320 return (-1); 1321 if ((sd->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) 1322 size = -1; 1323 else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP) 1324 size = -1; 1325 else 1326 size = sd->sc_dk.dk_label->d_partitions[part].p_size * 1327 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE); 1328 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0) 1329 return (-1); 1330 return (size); 1331 } 1332 1333 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */ 1334 static struct scsipi_xfer sx; 1335 static int sddoingadump; 1336 1337 /* 1338 * dump all of physical memory into the partition specified, starting 1339 * at offset 'dumplo' into the partition. 1340 */ 1341 int 1342 sddump(dev, blkno, va, size) 1343 dev_t dev; 1344 daddr_t blkno; 1345 caddr_t va; 1346 size_t size; 1347 { 1348 struct sd_softc *sd; /* disk unit to do the I/O */ 1349 struct disklabel *lp; /* disk's disklabel */ 1350 int unit, part; 1351 int sectorsize; /* size of a disk sector */ 1352 int nsects; /* number of sectors in partition */ 1353 int sectoff; /* sector offset of partition */ 1354 int totwrt; /* total number of sectors left to write */ 1355 int nwrt; /* current number of sectors to write */ 1356 struct scsipi_rw_big cmd; /* write command */ 1357 struct scsipi_xfer *xs; /* ... convenience */ 1358 struct scsipi_periph *periph; 1359 struct scsipi_channel *chan; 1360 1361 /* Check if recursive dump; if so, punt. */ 1362 if (sddoingadump) 1363 return (EFAULT); 1364 1365 /* Mark as active early. */ 1366 sddoingadump = 1; 1367 1368 unit = SDUNIT(dev); /* Decompose unit & partition. */ 1369 part = SDPART(dev); 1370 1371 /* Check for acceptable drive number. */ 1372 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL) 1373 return (ENXIO); 1374 1375 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0) 1376 return (ENODEV); 1377 1378 periph = sd->sc_periph; 1379 chan = periph->periph_channel; 1380 1381 /* Make sure it was initialized. */ 1382 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) 1383 return (ENXIO); 1384 1385 /* Convert to disk sectors. Request must be a multiple of size. */ 1386 lp = sd->sc_dk.dk_label; 1387 sectorsize = lp->d_secsize; 1388 if ((size % sectorsize) != 0) 1389 return (EFAULT); 1390 totwrt = size / sectorsize; 1391 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */ 1392 1393 nsects = lp->d_partitions[part].p_size; 1394 sectoff = lp->d_partitions[part].p_offset; 1395 1396 /* Check transfer bounds against partition size. */ 1397 if ((blkno < 0) || ((blkno + totwrt) > nsects)) 1398 return (EINVAL); 1399 1400 /* Offset block number to start of partition. */ 1401 blkno += sectoff; 1402 1403 xs = &sx; 1404 1405 while (totwrt > 0) { 1406 nwrt = totwrt; /* XXX */ 1407 #ifndef SD_DUMP_NOT_TRUSTED 1408 /* 1409 * Fill out the scsi command 1410 */ 1411 memset(&cmd, 0, sizeof(cmd)); 1412 cmd.opcode = WRITE_BIG; 1413 _lto4b(blkno, cmd.addr); 1414 _lto2b(nwrt, cmd.length); 1415 /* 1416 * Fill out the scsipi_xfer structure 1417 * Note: we cannot sleep as we may be an interrupt 1418 * don't use scsipi_command() as it may want to wait 1419 * for an xs. 1420 */ 1421 memset(xs, 0, sizeof(sx)); 1422 xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL | 1423 XS_CTL_DATA_OUT; 1424 xs->xs_status = 0; 1425 xs->xs_periph = periph; 1426 xs->xs_retries = SDRETRIES; 1427 xs->timeout = 10000; /* 10000 millisecs for a disk ! */ 1428 xs->cmd = (struct scsipi_generic *)&cmd; 1429 xs->cmdlen = sizeof(cmd); 1430 xs->resid = nwrt * sectorsize; 1431 xs->error = XS_NOERROR; 1432 xs->bp = 0; 1433 xs->data = va; 1434 xs->datalen = nwrt * sectorsize; 1435 1436 /* 1437 * Pass all this info to the scsi driver. 1438 */ 1439 scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs); 1440 if ((xs->xs_status & XS_STS_DONE) == 0 || 1441 xs->error != XS_NOERROR) 1442 return (EIO); 1443 #else /* SD_DUMP_NOT_TRUSTED */ 1444 /* Let's just talk about this first... */ 1445 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno); 1446 delay(500 * 1000); /* half a second */ 1447 #endif /* SD_DUMP_NOT_TRUSTED */ 1448 1449 /* update block count */ 1450 totwrt -= nwrt; 1451 blkno += nwrt; 1452 va += sectorsize * nwrt; 1453 } 1454 sddoingadump = 0; 1455 return (0); 1456 } 1457