1 /* $NetBSD: sd.c,v 1.180 2001/11/15 09:48:17 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.180 2001/11/15 09:48:17 lukem 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 %ld 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 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 SCIOCIDENTIFY: 918 case OSCIOCIDENTIFY: 919 case SCIOCCOMMAND: 920 case SCIOCDEBUG: 921 if (part == RAW_PART) 922 break; 923 /* FALLTHROUGH */ 924 default: 925 if ((periph->periph_flags & PERIPH_OPEN) == 0) 926 return (ENODEV); 927 else 928 return (EIO); 929 } 930 } 931 932 switch (cmd) { 933 case DIOCGDINFO: 934 *(struct disklabel *)addr = *(sd->sc_dk.dk_label); 935 return (0); 936 937 #ifdef __HAVE_OLD_DISKLABEL 938 case ODIOCGDINFO: 939 newlabel = *(sd->sc_dk.dk_label); 940 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 941 return ENOTTY; 942 memcpy(addr, &newlabel, sizeof (struct olddisklabel)); 943 return (0); 944 #endif 945 946 case DIOCGPART: 947 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label; 948 ((struct partinfo *)addr)->part = 949 &sd->sc_dk.dk_label->d_partitions[part]; 950 return (0); 951 952 case DIOCWDINFO: 953 case DIOCSDINFO: 954 #ifdef __HAVE_OLD_DISKLABEL 955 case ODIOCWDINFO: 956 case ODIOCSDINFO: 957 #endif 958 { 959 struct disklabel *lp; 960 961 #ifdef __HAVE_OLD_DISKLABEL 962 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) { 963 memset(&newlabel, 0, sizeof newlabel); 964 memcpy(&newlabel, addr, sizeof (struct olddisklabel)); 965 lp = &newlabel; 966 } else 967 #endif 968 lp = (struct disklabel *)addr; 969 970 if ((flag & FWRITE) == 0) 971 return (EBADF); 972 973 if ((error = sdlock(sd)) != 0) 974 return (error); 975 sd->flags |= SDF_LABELLING; 976 977 error = setdisklabel(sd->sc_dk.dk_label, 978 lp, /*sd->sc_dk.dk_openmask : */0, 979 sd->sc_dk.dk_cpulabel); 980 if (error == 0) { 981 if (cmd == DIOCWDINFO 982 #ifdef __HAVE_OLD_DISKLABEL 983 || cmd == ODIOCWDINFO 984 #endif 985 ) 986 error = writedisklabel(SDLABELDEV(dev), 987 sdstrategy, sd->sc_dk.dk_label, 988 sd->sc_dk.dk_cpulabel); 989 } 990 991 sd->flags &= ~SDF_LABELLING; 992 sdunlock(sd); 993 return (error); 994 } 995 996 case DIOCKLABEL: 997 if (*(int *)addr) 998 periph->periph_flags |= PERIPH_KEEP_LABEL; 999 else 1000 periph->periph_flags &= ~PERIPH_KEEP_LABEL; 1001 return (0); 1002 1003 case DIOCWLABEL: 1004 if ((flag & FWRITE) == 0) 1005 return (EBADF); 1006 if (*(int *)addr) 1007 sd->flags |= SDF_WLABEL; 1008 else 1009 sd->flags &= ~SDF_WLABEL; 1010 return (0); 1011 1012 case DIOCLOCK: 1013 return (scsipi_prevent(periph, 1014 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0)); 1015 1016 case DIOCEJECT: 1017 if ((periph->periph_flags & PERIPH_REMOVABLE) == 0) 1018 return (ENOTTY); 1019 if (*(int *)addr == 0) { 1020 /* 1021 * Don't force eject: check that we are the only 1022 * partition open. If so, unlock it. 1023 */ 1024 if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 && 1025 sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask == 1026 sd->sc_dk.dk_openmask) { 1027 error = scsipi_prevent(periph, PR_ALLOW, 1028 XS_CTL_IGNORE_NOT_READY); 1029 if (error) 1030 return (error); 1031 } else { 1032 return (EBUSY); 1033 } 1034 } 1035 /* FALLTHROUGH */ 1036 case ODIOCEJECT: 1037 return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ? 1038 ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0)); 1039 1040 case DIOCGDEFLABEL: 1041 sdgetdefaultlabel(sd, (struct disklabel *)addr); 1042 return (0); 1043 1044 #ifdef __HAVE_OLD_DISKLABEL 1045 case ODIOCGDEFLABEL: 1046 sdgetdefaultlabel(sd, &newlabel); 1047 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 1048 return ENOTTY; 1049 memcpy(addr, &newlabel, sizeof (struct olddisklabel)); 1050 return (0); 1051 #endif 1052 1053 default: 1054 if (part != RAW_PART) 1055 return (ENOTTY); 1056 return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, p)); 1057 } 1058 1059 #ifdef DIAGNOSTIC 1060 panic("sdioctl: impossible"); 1061 #endif 1062 } 1063 1064 void 1065 sdgetdefaultlabel(sd, lp) 1066 struct sd_softc *sd; 1067 struct disklabel *lp; 1068 { 1069 1070 memset(lp, 0, sizeof(struct disklabel)); 1071 1072 lp->d_secsize = sd->params.blksize; 1073 lp->d_ntracks = sd->params.heads; 1074 lp->d_nsectors = sd->params.sectors; 1075 lp->d_ncylinders = sd->params.cyls; 1076 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 1077 1078 switch (scsipi_periph_bustype(sd->sc_periph)) { 1079 #if NSD_SCSIBUS > 0 1080 case SCSIPI_BUSTYPE_SCSI: 1081 lp->d_type = DTYPE_SCSI; 1082 break; 1083 #endif 1084 #if NSD_ATAPIBUS > 0 1085 case SCSIPI_BUSTYPE_ATAPI: 1086 lp->d_type = DTYPE_ATAPI; 1087 break; 1088 #endif 1089 } 1090 strncpy(lp->d_typename, sd->name, 16); 1091 strncpy(lp->d_packname, "fictitious", 16); 1092 lp->d_secperunit = sd->params.disksize; 1093 lp->d_rpm = sd->params.rot_rate; 1094 lp->d_interleave = 1; 1095 lp->d_flags = 0; 1096 1097 lp->d_partitions[RAW_PART].p_offset = 0; 1098 lp->d_partitions[RAW_PART].p_size = 1099 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 1100 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; 1101 lp->d_npartitions = RAW_PART + 1; 1102 1103 lp->d_magic = DISKMAGIC; 1104 lp->d_magic2 = DISKMAGIC; 1105 lp->d_checksum = dkcksum(lp); 1106 } 1107 1108 1109 /* 1110 * Load the label information on the named device 1111 */ 1112 void 1113 sdgetdisklabel(sd) 1114 struct sd_softc *sd; 1115 { 1116 struct disklabel *lp = sd->sc_dk.dk_label; 1117 char *errstring; 1118 1119 memset(sd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); 1120 1121 sdgetdefaultlabel(sd, lp); 1122 1123 if (lp->d_secpercyl == 0) { 1124 lp->d_secpercyl = 100; 1125 /* as long as it's not 0 - readdisklabel divides by it (?) */ 1126 } 1127 1128 /* 1129 * Call the generic disklabel extraction routine 1130 */ 1131 errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART), 1132 sdstrategy, lp, sd->sc_dk.dk_cpulabel); 1133 if (errstring) { 1134 printf("%s: %s\n", sd->sc_dev.dv_xname, errstring); 1135 return; 1136 } 1137 } 1138 1139 void 1140 sd_shutdown(arg) 1141 void *arg; 1142 { 1143 struct sd_softc *sd = arg; 1144 1145 /* 1146 * If the disk cache needs to be flushed, and the disk supports 1147 * it, flush it. We're cold at this point, so we poll for 1148 * completion. 1149 */ 1150 if ((sd->flags & SDF_DIRTY) != 0 && sd->sc_ops->sdo_flush != NULL) { 1151 if ((*sd->sc_ops->sdo_flush)(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) { 1152 printf("%s: cache synchronization failed\n", 1153 sd->sc_dev.dv_xname); 1154 sd->flags &= ~SDF_FLUSHING; 1155 } else 1156 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY); 1157 } 1158 } 1159 1160 /* 1161 * Tell the device to map out a defective block 1162 */ 1163 int 1164 sd_reassign_blocks(sd, blkno) 1165 struct sd_softc *sd; 1166 u_long blkno; 1167 { 1168 struct scsi_reassign_blocks scsipi_cmd; 1169 struct scsi_reassign_blocks_data rbdata; 1170 1171 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1172 memset(&rbdata, 0, sizeof(rbdata)); 1173 scsipi_cmd.opcode = SCSI_REASSIGN_BLOCKS; 1174 1175 _lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length); 1176 _lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr); 1177 1178 return (scsipi_command(sd->sc_periph, 1179 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1180 (u_char *)&rbdata, sizeof(rbdata), SDRETRIES, 5000, NULL, 1181 XS_CTL_DATA_OUT | XS_CTL_DATA_ONSTACK)); 1182 } 1183 1184 /* 1185 * Check Errors 1186 */ 1187 int 1188 sd_interpret_sense(xs) 1189 struct scsipi_xfer *xs; 1190 { 1191 struct scsipi_periph *periph = xs->xs_periph; 1192 struct scsipi_sense_data *sense = &xs->sense.scsi_sense; 1193 struct sd_softc *sd = (void *)periph->periph_dev; 1194 int s, error, retval = EJUSTRETURN; 1195 1196 /* 1197 * If the periph is already recovering, just do the normal 1198 * error processing. 1199 */ 1200 if (periph->periph_flags & PERIPH_RECOVERING) 1201 return (retval); 1202 1203 /* 1204 * If the device is not open yet, let the generic code handle it. 1205 */ 1206 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) 1207 return (retval); 1208 1209 /* 1210 * If it isn't a extended or extended/deferred error, let 1211 * the generic code handle it. 1212 */ 1213 if ((sense->error_code & SSD_ERRCODE) != 0x70 && 1214 (sense->error_code & SSD_ERRCODE) != 0x71) 1215 return (retval); 1216 1217 if ((sense->flags & SSD_KEY) == SKEY_NOT_READY && 1218 sense->add_sense_code == 0x4) { 1219 if (sense->add_sense_code_qual == 0x01) { 1220 /* 1221 * Unit In The Process Of Becoming Ready. 1222 */ 1223 printf("%s: waiting for pack to spin up...\n", 1224 sd->sc_dev.dv_xname); 1225 scsipi_periph_freeze(periph, 1); 1226 callout_reset(&periph->periph_callout, 1227 5 * hz, scsipi_periph_timed_thaw, periph); 1228 retval = ERESTART; 1229 } else if ((sense->add_sense_code_qual == 0x2) && 1230 (periph->periph_quirks & PQUIRK_NOSTARTUNIT) == 0) { 1231 printf("%s: pack is stopped, restarting...\n", 1232 sd->sc_dev.dv_xname); 1233 s = splbio(); 1234 periph->periph_flags |= PERIPH_RECOVERING; 1235 splx(s); 1236 error = scsipi_start(periph, SSS_START, 1237 XS_CTL_URGENT|XS_CTL_HEAD_TAG| 1238 XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH); 1239 if (error) { 1240 printf("%s: unable to restart pack\n", 1241 sd->sc_dev.dv_xname); 1242 retval = error; 1243 } else 1244 retval = ERESTART; 1245 s = splbio(); 1246 periph->periph_flags &= ~PERIPH_RECOVERING; 1247 splx(s); 1248 } 1249 } 1250 return (retval); 1251 } 1252 1253 1254 int 1255 sdsize(dev) 1256 dev_t dev; 1257 { 1258 struct sd_softc *sd; 1259 int part, unit, omask; 1260 int size; 1261 1262 unit = SDUNIT(dev); 1263 if (unit >= sd_cd.cd_ndevs) 1264 return (-1); 1265 sd = sd_cd.cd_devs[unit]; 1266 if (sd == NULL) 1267 return (-1); 1268 1269 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0) 1270 return (-1); 1271 1272 part = SDPART(dev); 1273 omask = sd->sc_dk.dk_openmask & (1 << part); 1274 1275 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0) 1276 return (-1); 1277 if ((sd->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) 1278 size = -1; 1279 else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP) 1280 size = -1; 1281 else 1282 size = sd->sc_dk.dk_label->d_partitions[part].p_size * 1283 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE); 1284 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0) 1285 return (-1); 1286 return (size); 1287 } 1288 1289 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */ 1290 static struct scsipi_xfer sx; 1291 static int sddoingadump; 1292 1293 /* 1294 * dump all of physical memory into the partition specified, starting 1295 * at offset 'dumplo' into the partition. 1296 */ 1297 int 1298 sddump(dev, blkno, va, size) 1299 dev_t dev; 1300 daddr_t blkno; 1301 caddr_t va; 1302 size_t size; 1303 { 1304 struct sd_softc *sd; /* disk unit to do the I/O */ 1305 struct disklabel *lp; /* disk's disklabel */ 1306 int unit, part; 1307 int sectorsize; /* size of a disk sector */ 1308 int nsects; /* number of sectors in partition */ 1309 int sectoff; /* sector offset of partition */ 1310 int totwrt; /* total number of sectors left to write */ 1311 int nwrt; /* current number of sectors to write */ 1312 struct scsipi_rw_big cmd; /* write command */ 1313 struct scsipi_xfer *xs; /* ... convenience */ 1314 struct scsipi_periph *periph; 1315 struct scsipi_channel *chan; 1316 1317 /* Check if recursive dump; if so, punt. */ 1318 if (sddoingadump) 1319 return (EFAULT); 1320 1321 /* Mark as active early. */ 1322 sddoingadump = 1; 1323 1324 unit = SDUNIT(dev); /* Decompose unit & partition. */ 1325 part = SDPART(dev); 1326 1327 /* Check for acceptable drive number. */ 1328 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL) 1329 return (ENXIO); 1330 1331 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0) 1332 return (ENODEV); 1333 1334 periph = sd->sc_periph; 1335 chan = periph->periph_channel; 1336 1337 /* Make sure it was initialized. */ 1338 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) 1339 return (ENXIO); 1340 1341 /* Convert to disk sectors. Request must be a multiple of size. */ 1342 lp = sd->sc_dk.dk_label; 1343 sectorsize = lp->d_secsize; 1344 if ((size % sectorsize) != 0) 1345 return (EFAULT); 1346 totwrt = size / sectorsize; 1347 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */ 1348 1349 nsects = lp->d_partitions[part].p_size; 1350 sectoff = lp->d_partitions[part].p_offset; 1351 1352 /* Check transfer bounds against partition size. */ 1353 if ((blkno < 0) || ((blkno + totwrt) > nsects)) 1354 return (EINVAL); 1355 1356 /* Offset block number to start of partition. */ 1357 blkno += sectoff; 1358 1359 xs = &sx; 1360 1361 while (totwrt > 0) { 1362 nwrt = totwrt; /* XXX */ 1363 #ifndef SD_DUMP_NOT_TRUSTED 1364 /* 1365 * Fill out the scsi command 1366 */ 1367 memset(&cmd, 0, sizeof(cmd)); 1368 cmd.opcode = WRITE_BIG; 1369 _lto4b(blkno, cmd.addr); 1370 _lto2b(nwrt, cmd.length); 1371 /* 1372 * Fill out the scsipi_xfer structure 1373 * Note: we cannot sleep as we may be an interrupt 1374 * don't use scsipi_command() as it may want to wait 1375 * for an xs. 1376 */ 1377 memset(xs, 0, sizeof(sx)); 1378 xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL | 1379 XS_CTL_DATA_OUT; 1380 xs->xs_status = 0; 1381 xs->xs_periph = periph; 1382 xs->xs_retries = SDRETRIES; 1383 xs->timeout = 10000; /* 10000 millisecs for a disk ! */ 1384 xs->cmd = (struct scsipi_generic *)&cmd; 1385 xs->cmdlen = sizeof(cmd); 1386 xs->resid = nwrt * sectorsize; 1387 xs->error = XS_NOERROR; 1388 xs->bp = 0; 1389 xs->data = va; 1390 xs->datalen = nwrt * sectorsize; 1391 1392 /* 1393 * Pass all this info to the scsi driver. 1394 */ 1395 scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs); 1396 if ((xs->xs_status & XS_STS_DONE) == 0 || 1397 xs->error != XS_NOERROR) 1398 return (EIO); 1399 #else /* SD_DUMP_NOT_TRUSTED */ 1400 /* Let's just talk about this first... */ 1401 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno); 1402 delay(500 * 1000); /* half a second */ 1403 #endif /* SD_DUMP_NOT_TRUSTED */ 1404 1405 /* update block count */ 1406 totwrt -= nwrt; 1407 blkno += nwrt; 1408 va += sectorsize * nwrt; 1409 } 1410 sddoingadump = 0; 1411 return (0); 1412 } 1413