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