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