1 /* $NetBSD: sd.c,v 1.114 1997/07/02 03:51:25 phil Exp $ */ 2 3 /* 4 * Copyright (c) 1994, 1995, 1997 Charles M. Hannum. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Charles M. Hannum. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Originally written by Julian Elischer (julian@dialix.oz.au) 34 * for TRW Financial Systems for use under the MACH(2.5) operating system. 35 * 36 * TRW Financial Systems, in accordance with their agreement with Carnegie 37 * Mellon University, makes this software available to CMU to distribute 38 * or use in any manner that they see fit as long as this message is kept with 39 * the software. For this reason TFS also grants any other persons or 40 * organisations permission to use or modify this software. 41 * 42 * TFS supplies this software to be publicly redistributed 43 * on the understanding that TFS is not responsible for the correct 44 * functioning of this software in any circumstances. 45 * 46 * Ported to run under 386BSD by Julian Elischer (julian@dialix.oz.au) Sept 1992 47 */ 48 49 #include <sys/types.h> 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/kernel.h> 53 #include <sys/file.h> 54 #include <sys/stat.h> 55 #include <sys/ioctl.h> 56 #include <sys/buf.h> 57 #include <sys/uio.h> 58 #include <sys/malloc.h> 59 #include <sys/errno.h> 60 #include <sys/device.h> 61 #include <sys/disklabel.h> 62 #include <sys/disk.h> 63 #include <sys/proc.h> 64 #include <sys/conf.h> 65 66 #include <scsi/scsi_all.h> 67 #include <scsi/scsi_disk.h> 68 #include <scsi/scsiconf.h> 69 70 #define SDOUTSTANDING 4 71 #define SDRETRIES 4 72 73 #define SDUNIT(dev) DISKUNIT(dev) 74 #define SDPART(dev) DISKPART(dev) 75 #define MAKESDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part) 76 77 #define SDLABELDEV(dev) (MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART)) 78 79 struct sd_softc { 80 struct device sc_dev; 81 struct disk sc_dk; 82 83 int flags; 84 #define SDF_LOCKED 0x01 85 #define SDF_WANTED 0x02 86 #define SDF_WLABEL 0x04 /* label is writable */ 87 #define SDF_LABELLING 0x08 /* writing label */ 88 #define SDF_ANCIENT 0x10 /* disk is ancient; for minphys */ 89 struct scsi_link *sc_link; /* contains our targ, lun, etc. */ 90 struct disk_parms { 91 u_char heads; /* number of heads */ 92 u_short cyls; /* number of cylinders */ 93 u_char sectors; /* number of sectors/track */ 94 int blksize; /* number of bytes/sector */ 95 u_long disksize; /* total number sectors */ 96 } params; 97 struct buf buf_queue; 98 u_int8_t type; 99 }; 100 101 struct scsi_mode_sense_data { 102 struct scsi_mode_header header; 103 struct scsi_blk_desc blk_desc; 104 union disk_pages pages; 105 }; 106 107 #ifdef __BROKEN_INDIRECT_CONFIG 108 int sdmatch __P((struct device *, void *, void *)); 109 #else 110 int sdmatch __P((struct device *, struct cfdata *, void *)); 111 #endif 112 void sdattach __P((struct device *, struct device *, void *)); 113 int sdlock __P((struct sd_softc *)); 114 void sdunlock __P((struct sd_softc *)); 115 void sdminphys __P((struct buf *)); 116 void sdgetdisklabel __P((struct sd_softc *)); 117 void sdstart __P((void *)); 118 void sddone __P((struct scsi_xfer *)); 119 int sd_reassign_blocks __P((struct sd_softc *, u_long)); 120 int sd_get_optparms __P((struct sd_softc *, int, struct disk_parms *)); 121 int sd_get_parms __P((struct sd_softc *, int)); 122 static int sd_mode_sense __P((struct sd_softc *, struct scsi_mode_sense_data *, 123 int, int)); 124 125 struct cfattach sd_ca = { 126 sizeof(struct sd_softc), sdmatch, sdattach 127 }; 128 129 struct cfdriver sd_cd = { 130 NULL, "sd", DV_DISK 131 }; 132 133 struct dkdriver sddkdriver = { sdstrategy }; 134 135 struct scsi_device sd_switch = { 136 NULL, /* Use default error handler */ 137 sdstart, /* have a queue, served by this */ 138 NULL, /* have no async handler */ 139 sddone, /* deal with stats at interrupt time */ 140 }; 141 142 struct scsi_inquiry_pattern sd_patterns[] = { 143 {T_DIRECT, T_FIXED, 144 "", "", ""}, 145 {T_DIRECT, T_REMOV, 146 "", "", ""}, 147 {T_OPTICAL, T_FIXED, 148 "", "", ""}, 149 {T_OPTICAL, T_REMOV, 150 "", "", ""}, 151 }; 152 153 int 154 sdmatch(parent, match, aux) 155 struct device *parent; 156 #ifdef __BROKEN_INDIRECT_CONFIG 157 void *match; 158 #else 159 struct cfdata *match; 160 #endif 161 void *aux; 162 { 163 struct scsibus_attach_args *sa = aux; 164 int priority; 165 166 (void)scsi_inqmatch(sa->sa_inqbuf, 167 (caddr_t)sd_patterns, sizeof(sd_patterns)/sizeof(sd_patterns[0]), 168 sizeof(sd_patterns[0]), &priority); 169 return (priority); 170 } 171 172 /* 173 * The routine called by the low level scsi routine when it discovers 174 * a device suitable for this driver. 175 */ 176 void 177 sdattach(parent, self, aux) 178 struct device *parent, *self; 179 void *aux; 180 { 181 int error; 182 struct sd_softc *sd = (void *)self; 183 struct disk_parms *dp = &sd->params; 184 struct scsibus_attach_args *sa = aux; 185 struct scsi_link *sc_link = sa->sa_sc_link; 186 187 SC_DEBUG(sc_link, SDEV_DB2, ("sdattach: ")); 188 189 /* 190 * Store information needed to contact our base driver 191 */ 192 sd->sc_link = sc_link; 193 sd->type = (sa->sa_inqbuf->device & SID_TYPE); 194 sc_link->device = &sd_switch; 195 sc_link->device_softc = sd; 196 if (sc_link->openings > SDOUTSTANDING) 197 sc_link->openings = SDOUTSTANDING; 198 199 /* 200 * Initialize and attach the disk structure. 201 */ 202 sd->sc_dk.dk_driver = &sddkdriver; 203 sd->sc_dk.dk_name = sd->sc_dev.dv_xname; 204 disk_attach(&sd->sc_dk); 205 206 #if !defined(i386) 207 dk_establish(&sd->sc_dk, &sd->sc_dev); /* XXX */ 208 #endif 209 210 /* 211 * Note if this device is ancient. This is used in sdminphys(). 212 */ 213 if ((sa->sa_inqbuf->version & SID_ANSII) == 0) 214 sd->flags |= SDF_ANCIENT; 215 216 /* 217 * Use the subdriver to request information regarding 218 * the drive. We cannot use interrupts yet, so the 219 * request must specify this. 220 */ 221 printf("\n"); 222 printf("%s: ", sd->sc_dev.dv_xname); 223 224 if ((sd->sc_link->quirks & SDEV_NOSTARTUNIT) == 0) { 225 error = scsi_start(sd->sc_link, SSS_START, 226 SCSI_AUTOCONF | SCSI_IGNORE_ILLEGAL_REQUEST | 227 SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT); 228 } else 229 error = 0; 230 231 if (error || sd_get_parms(sd, SCSI_AUTOCONF) != 0) 232 printf("drive offline\n"); 233 else 234 printf("%ldMB, %d cyl, %d head, %d sec, %d bytes/sec x %ld sectors\n", 235 dp->disksize / (1048576 / dp->blksize), dp->cyls, 236 dp->heads, dp->sectors, dp->blksize, dp->disksize); 237 } 238 239 /* 240 * Wait interruptibly for an exclusive lock. 241 * 242 * XXX 243 * Several drivers do this; it should be abstracted and made MP-safe. 244 */ 245 int 246 sdlock(sd) 247 struct sd_softc *sd; 248 { 249 int error; 250 251 while ((sd->flags & SDF_LOCKED) != 0) { 252 sd->flags |= SDF_WANTED; 253 if ((error = tsleep(sd, PRIBIO | PCATCH, "sdlck", 0)) != 0) 254 return error; 255 } 256 sd->flags |= SDF_LOCKED; 257 return 0; 258 } 259 260 /* 261 * Unlock and wake up any waiters. 262 */ 263 void 264 sdunlock(sd) 265 struct sd_softc *sd; 266 { 267 268 sd->flags &= ~SDF_LOCKED; 269 if ((sd->flags & SDF_WANTED) != 0) { 270 sd->flags &= ~SDF_WANTED; 271 wakeup(sd); 272 } 273 } 274 275 /* 276 * open the device. Make sure the partition info is a up-to-date as can be. 277 */ 278 int 279 sdopen(dev, flag, fmt, p) 280 dev_t dev; 281 int flag, fmt; 282 struct proc *p; 283 { 284 struct sd_softc *sd; 285 struct scsi_link *sc_link; 286 int unit, part; 287 int error; 288 289 unit = SDUNIT(dev); 290 if (unit >= sd_cd.cd_ndevs) 291 return ENXIO; 292 sd = sd_cd.cd_devs[unit]; 293 if (sd == NULL) 294 return ENXIO; 295 296 sc_link = sd->sc_link; 297 298 SC_DEBUG(sc_link, SDEV_DB1, 299 ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit, 300 sd_cd.cd_ndevs, SDPART(dev))); 301 302 if ((error = sdlock(sd)) != 0) 303 return error; 304 305 if (sd->sc_dk.dk_openmask != 0) { 306 /* 307 * If any partition is open, but the disk has been invalidated, 308 * disallow further opens. 309 */ 310 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 311 error = EIO; 312 goto bad3; 313 } 314 } else { 315 /* Check that it is still responding and ok. */ 316 error = scsi_test_unit_ready(sc_link, 317 SCSI_IGNORE_ILLEGAL_REQUEST | 318 SCSI_IGNORE_MEDIA_CHANGE | 319 SCSI_IGNORE_NOT_READY); 320 if (error) 321 goto bad3; 322 323 /* Start the pack spinning if necessary. */ 324 if ((sc_link->quirks & SDEV_NOSTARTUNIT) == 0) { 325 error = scsi_start(sc_link, SSS_START, 326 SCSI_IGNORE_ILLEGAL_REQUEST | 327 SCSI_IGNORE_MEDIA_CHANGE | 328 SCSI_SILENT); 329 if (error) 330 goto bad3; 331 } 332 333 sc_link->flags |= SDEV_OPEN; 334 335 /* Lock the pack in. */ 336 error = scsi_prevent(sc_link, PR_PREVENT, 337 SCSI_IGNORE_ILLEGAL_REQUEST | 338 SCSI_IGNORE_MEDIA_CHANGE); 339 if (error) 340 goto bad; 341 342 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 343 sc_link->flags |= SDEV_MEDIA_LOADED; 344 345 /* Load the physical device parameters. */ 346 if (sd_get_parms(sd, 0) != 0) { 347 error = ENXIO; 348 goto bad2; 349 } 350 SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded ")); 351 352 /* Load the partition info if not already loaded. */ 353 sdgetdisklabel(sd); 354 SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel loaded ")); 355 } 356 } 357 358 part = SDPART(dev); 359 360 /* Check that the partition exists. */ 361 if (part != RAW_PART && 362 (part >= sd->sc_dk.dk_label->d_npartitions || 363 sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) { 364 error = ENXIO; 365 goto bad; 366 } 367 368 /* Insure only one open at a time. */ 369 switch (fmt) { 370 case S_IFCHR: 371 sd->sc_dk.dk_copenmask |= (1 << part); 372 break; 373 case S_IFBLK: 374 sd->sc_dk.dk_bopenmask |= (1 << part); 375 break; 376 } 377 sd->sc_dk.dk_openmask = sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask; 378 379 SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n")); 380 sdunlock(sd); 381 return 0; 382 383 bad2: 384 sc_link->flags &= ~SDEV_MEDIA_LOADED; 385 386 bad: 387 if (sd->sc_dk.dk_openmask == 0) { 388 scsi_prevent(sc_link, PR_ALLOW, 389 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE); 390 sc_link->flags &= ~SDEV_OPEN; 391 } 392 393 bad3: 394 sdunlock(sd); 395 return error; 396 } 397 398 /* 399 * close the device.. only called if we are the LAST occurence of an open 400 * device. Convenient now but usually a pain. 401 */ 402 int 403 sdclose(dev, flag, fmt, p) 404 dev_t dev; 405 int flag, fmt; 406 struct proc *p; 407 { 408 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)]; 409 int part = SDPART(dev); 410 int error; 411 412 if ((error = sdlock(sd)) != 0) 413 return error; 414 415 switch (fmt) { 416 case S_IFCHR: 417 sd->sc_dk.dk_copenmask &= ~(1 << part); 418 break; 419 case S_IFBLK: 420 sd->sc_dk.dk_bopenmask &= ~(1 << part); 421 break; 422 } 423 sd->sc_dk.dk_openmask = sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask; 424 425 if (sd->sc_dk.dk_openmask == 0) { 426 /* XXXX Must wait for I/O to complete! */ 427 428 scsi_prevent(sd->sc_link, PR_ALLOW, 429 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY); 430 sd->sc_link->flags &= ~(SDEV_OPEN|SDEV_MEDIA_LOADED); 431 } 432 433 sdunlock(sd); 434 return 0; 435 } 436 437 /* 438 * Actually translate the requested transfer into one the physical driver 439 * can understand. The transfer is described by a buf and will include 440 * only one physical transfer. 441 */ 442 void 443 sdstrategy(bp) 444 struct buf *bp; 445 { 446 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)]; 447 int s; 448 449 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdstrategy ")); 450 SC_DEBUG(sd->sc_link, SDEV_DB1, 451 ("%ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno)); 452 /* 453 * The transfer must be a whole number of blocks. 454 */ 455 if ((bp->b_bcount % sd->sc_dk.dk_label->d_secsize) != 0) { 456 bp->b_error = EINVAL; 457 goto bad; 458 } 459 /* 460 * If the device has been made invalid, error out 461 */ 462 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 463 bp->b_error = EIO; 464 goto bad; 465 } 466 /* 467 * If it's a null transfer, return immediatly 468 */ 469 if (bp->b_bcount == 0) 470 goto done; 471 472 /* 473 * Do bounds checking, adjust transfer. if error, process. 474 * If end of partition, just return. 475 */ 476 if (SDPART(bp->b_dev) != RAW_PART && 477 bounds_check_with_label(bp, sd->sc_dk.dk_label, 478 (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0) 479 goto done; 480 481 s = splbio(); 482 483 /* 484 * Place it in the queue of disk activities for this disk 485 */ 486 disksort(&sd->buf_queue, bp); 487 488 /* 489 * Tell the device to get going on the transfer if it's 490 * not doing anything, otherwise just wait for completion 491 */ 492 sdstart(sd); 493 494 splx(s); 495 return; 496 497 bad: 498 bp->b_flags |= B_ERROR; 499 done: 500 /* 501 * Correctly set the buf to indicate a completed xfer 502 */ 503 bp->b_resid = bp->b_bcount; 504 biodone(bp); 505 } 506 507 /* 508 * sdstart looks to see if there is a buf waiting for the device 509 * and that the device is not already busy. If both are true, 510 * It dequeues the buf and creates a scsi command to perform the 511 * transfer in the buf. The transfer request will call scsi_done 512 * on completion, which will in turn call this routine again 513 * so that the next queued transfer is performed. 514 * The bufs are queued by the strategy routine (sdstrategy) 515 * 516 * This routine is also called after other non-queued requests 517 * have been made of the scsi driver, to ensure that the queue 518 * continues to be drained. 519 * 520 * must be called at the correct (highish) spl level 521 * sdstart() is called at splbio from sdstrategy and scsi_done 522 */ 523 void 524 sdstart(v) 525 register void *v; 526 { 527 register struct sd_softc *sd = v; 528 register struct scsi_link *sc_link = sd->sc_link; 529 struct buf *bp = 0; 530 struct buf *dp; 531 struct scsi_rw_big cmd_big; 532 struct scsi_rw cmd_small; 533 struct scsi_generic *cmdp; 534 int blkno, nblks, cmdlen, error; 535 struct partition *p; 536 537 SC_DEBUG(sc_link, SDEV_DB2, ("sdstart ")); 538 /* 539 * Check if the device has room for another command 540 */ 541 while (sc_link->openings > 0) { 542 /* 543 * there is excess capacity, but a special waits 544 * It'll need the adapter as soon as we clear out of the 545 * way and let it run (user level wait). 546 */ 547 if (sc_link->flags & SDEV_WAITING) { 548 sc_link->flags &= ~SDEV_WAITING; 549 wakeup((caddr_t)sc_link); 550 return; 551 } 552 553 /* 554 * See if there is a buf with work for us to do.. 555 */ 556 dp = &sd->buf_queue; 557 if ((bp = dp->b_actf) == NULL) /* yes, an assign */ 558 return; 559 dp->b_actf = bp->b_actf; 560 561 /* 562 * If the device has become invalid, abort all the 563 * reads and writes until all files have been closed and 564 * re-opened 565 */ 566 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 567 bp->b_error = EIO; 568 bp->b_flags |= B_ERROR; 569 bp->b_resid = bp->b_bcount; 570 biodone(bp); 571 continue; 572 } 573 574 /* 575 * We have a buf, now we should make a command 576 * 577 * First, translate the block to absolute and put it in terms 578 * of the logical blocksize of the device. 579 */ 580 blkno = 581 bp->b_blkno / (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE); 582 if (SDPART(bp->b_dev) != RAW_PART) { 583 p = &sd->sc_dk.dk_label->d_partitions[SDPART(bp->b_dev)]; 584 blkno += p->p_offset; 585 } 586 nblks = howmany(bp->b_bcount, sd->sc_dk.dk_label->d_secsize); 587 588 /* 589 * Fill out the scsi command. If the transfer will 590 * fit in a "small" cdb, use it. 591 */ 592 if (((blkno & 0x1fffff) == blkno) && 593 ((nblks & 0xff) == nblks)) { 594 /* 595 * We can fit in a small cdb. 596 */ 597 bzero(&cmd_small, sizeof(cmd_small)); 598 cmd_small.opcode = (bp->b_flags & B_READ) ? 599 READ_COMMAND : WRITE_COMMAND; 600 _lto3b(blkno, cmd_small.addr); 601 cmd_small.length = nblks & 0xff; 602 cmdlen = sizeof(cmd_small); 603 cmdp = (struct scsi_generic *)&cmd_small; 604 } else { 605 /* 606 * Need a large cdb. 607 */ 608 bzero(&cmd_big, sizeof(cmd_big)); 609 cmd_big.opcode = (bp->b_flags & B_READ) ? 610 READ_BIG : WRITE_BIG; 611 _lto4b(blkno, cmd_big.addr); 612 _lto2b(nblks, cmd_big.length); 613 cmdlen = sizeof(cmd_big); 614 cmdp = (struct scsi_generic *)&cmd_big; 615 } 616 617 /* Instrumentation. */ 618 disk_busy(&sd->sc_dk); 619 620 /* 621 * Call the routine that chats with the adapter. 622 * Note: we cannot sleep as we may be an interrupt 623 */ 624 error = scsi_scsi_cmd(sc_link, cmdp, cmdlen, 625 (u_char *)bp->b_data, bp->b_bcount, 626 SDRETRIES, 60000, bp, SCSI_NOSLEEP | 627 ((bp->b_flags & B_READ) ? SCSI_DATA_IN : SCSI_DATA_OUT)); 628 if (error) 629 printf("%s: not queued, error %d\n", 630 sd->sc_dev.dv_xname, error); 631 } 632 } 633 634 void 635 sddone(xs) 636 struct scsi_xfer *xs; 637 { 638 struct sd_softc *sd = xs->sc_link->device_softc; 639 640 if (xs->bp != NULL) 641 disk_unbusy(&sd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid); 642 } 643 644 void 645 sdminphys(bp) 646 struct buf *bp; 647 { 648 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)]; 649 long max; 650 651 /* 652 * If the device is ancient, we want to make sure that 653 * the transfer fits into a 6-byte cdb. 654 * 655 * XXX Note that the SCSI-I spec says that 256-block transfers 656 * are allowed in a 6-byte read/write, and are specified 657 * by settng the "length" to 0. However, we're conservative 658 * here, allowing only 255-block transfers in case an 659 * ancient device gets confused by length == 0. A length of 0 660 * in a 10-byte read/write actually means 0 blocks. 661 */ 662 if (sd->flags & SDF_ANCIENT) { 663 max = sd->sc_dk.dk_label->d_secsize * 0xff; 664 665 if (bp->b_bcount > max) 666 bp->b_bcount = max; 667 } 668 669 (*sd->sc_link->adapter->scsi_minphys)(bp); 670 } 671 672 int 673 sdread(dev, uio, ioflag) 674 dev_t dev; 675 struct uio *uio; 676 int ioflag; 677 { 678 679 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio)); 680 } 681 682 int 683 sdwrite(dev, uio, ioflag) 684 dev_t dev; 685 struct uio *uio; 686 int ioflag; 687 { 688 689 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio)); 690 } 691 692 /* 693 * Perform special action on behalf of the user 694 * Knows about the internals of this device 695 */ 696 int 697 sdioctl(dev, cmd, addr, flag, p) 698 dev_t dev; 699 u_long cmd; 700 caddr_t addr; 701 int flag; 702 struct proc *p; 703 { 704 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)]; 705 int error; 706 707 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdioctl 0x%lx ", cmd)); 708 709 /* 710 * If the device is not valid.. abandon ship 711 */ 712 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) 713 return EIO; 714 715 switch (cmd) { 716 case DIOCGDINFO: 717 *(struct disklabel *)addr = *(sd->sc_dk.dk_label); 718 return 0; 719 720 case DIOCGPART: 721 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label; 722 ((struct partinfo *)addr)->part = 723 &sd->sc_dk.dk_label->d_partitions[SDPART(dev)]; 724 return 0; 725 726 case DIOCWDINFO: 727 case DIOCSDINFO: 728 if ((flag & FWRITE) == 0) 729 return EBADF; 730 731 if ((error = sdlock(sd)) != 0) 732 return error; 733 sd->flags |= SDF_LABELLING; 734 735 error = setdisklabel(sd->sc_dk.dk_label, 736 (struct disklabel *)addr, /*sd->sc_dk.dk_openmask : */0, 737 sd->sc_dk.dk_cpulabel); 738 if (error == 0) { 739 if (cmd == DIOCWDINFO) 740 error = writedisklabel(SDLABELDEV(dev), 741 sdstrategy, sd->sc_dk.dk_label, 742 sd->sc_dk.dk_cpulabel); 743 } 744 745 sd->flags &= ~SDF_LABELLING; 746 sdunlock(sd); 747 return error; 748 749 case DIOCWLABEL: 750 if ((flag & FWRITE) == 0) 751 return EBADF; 752 if (*(int *)addr) 753 sd->flags |= SDF_WLABEL; 754 else 755 sd->flags &= ~SDF_WLABEL; 756 return 0; 757 758 case DIOCLOCK: 759 return scsi_prevent(sd->sc_link, 760 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0); 761 762 case DIOCEJECT: 763 return ((sd->sc_link->flags & SDEV_REMOVABLE) == 0 ? ENOTTY : 764 scsi_start(sd->sc_link, SSS_STOP|SSS_LOEJ, 0)); 765 766 default: 767 if (SDPART(dev) != RAW_PART) 768 return ENOTTY; 769 return scsi_do_ioctl(sd->sc_link, dev, cmd, addr, flag, p); 770 } 771 772 #ifdef DIAGNOSTIC 773 panic("sdioctl: impossible"); 774 #endif 775 } 776 777 /* 778 * Load the label information on the named device 779 */ 780 void 781 sdgetdisklabel(sd) 782 struct sd_softc *sd; 783 { 784 struct disklabel *lp = sd->sc_dk.dk_label; 785 char *errstring; 786 787 bzero(lp, sizeof(struct disklabel)); 788 bzero(sd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel)); 789 790 lp->d_secsize = sd->params.blksize; 791 lp->d_ntracks = sd->params.heads; 792 lp->d_nsectors = sd->params.sectors; 793 lp->d_ncylinders = sd->params.cyls; 794 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 795 if (lp->d_secpercyl == 0) { 796 lp->d_secpercyl = 100; 797 /* as long as it's not 0 - readdisklabel divides by it (?) */ 798 } 799 800 if (sd->type == T_OPTICAL) 801 strncpy(lp->d_typename, "SCSI optical", 16); 802 else 803 strncpy(lp->d_typename, "SCSI disk", 16); 804 lp->d_type = DTYPE_SCSI; 805 strncpy(lp->d_packname, "fictitious", 16); 806 lp->d_secperunit = sd->params.disksize; 807 lp->d_rpm = 3600; 808 lp->d_interleave = 1; 809 lp->d_flags = 0; 810 811 lp->d_partitions[RAW_PART].p_offset = 0; 812 lp->d_partitions[RAW_PART].p_size = 813 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 814 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; 815 lp->d_npartitions = RAW_PART + 1; 816 817 lp->d_magic = DISKMAGIC; 818 lp->d_magic2 = DISKMAGIC; 819 lp->d_checksum = dkcksum(lp); 820 821 /* 822 * Call the generic disklabel extraction routine 823 */ 824 errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART), 825 sdstrategy, lp, sd->sc_dk.dk_cpulabel); 826 if (errstring) { 827 printf("%s: %s\n", sd->sc_dev.dv_xname, errstring); 828 return; 829 } 830 } 831 832 /* 833 * Tell the device to map out a defective block 834 */ 835 int 836 sd_reassign_blocks(sd, blkno) 837 struct sd_softc *sd; 838 u_long blkno; 839 { 840 struct scsi_reassign_blocks scsi_cmd; 841 struct scsi_reassign_blocks_data rbdata; 842 843 bzero(&scsi_cmd, sizeof(scsi_cmd)); 844 bzero(&rbdata, sizeof(rbdata)); 845 scsi_cmd.opcode = REASSIGN_BLOCKS; 846 847 _lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length); 848 _lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr); 849 850 return scsi_scsi_cmd(sd->sc_link, (struct scsi_generic *)&scsi_cmd, 851 sizeof(scsi_cmd), (u_char *)&rbdata, sizeof(rbdata), SDRETRIES, 852 5000, NULL, SCSI_DATA_OUT); 853 } 854 855 856 857 static int 858 sd_mode_sense(sd, scsi_sense, page, flags) 859 struct sd_softc *sd; 860 struct scsi_mode_sense_data *scsi_sense; 861 int page, flags; 862 { 863 struct scsi_mode_sense scsi_cmd; 864 865 /* 866 * Make sure the sense buffer is clean before we do 867 * the mode sense, so that checks for bogus values of 868 * 0 will work in case the mode sense fails. 869 */ 870 bzero(scsi_sense, sizeof(*scsi_sense)); 871 872 bzero(&scsi_cmd, sizeof(scsi_cmd)); 873 scsi_cmd.opcode = MODE_SENSE; 874 scsi_cmd.page = page; 875 scsi_cmd.length = 0x20; 876 /* 877 * If the command worked, use the results to fill out 878 * the parameter structure 879 */ 880 return scsi_scsi_cmd(sd->sc_link, (struct scsi_generic *)&scsi_cmd, 881 sizeof(scsi_cmd), (u_char *)scsi_sense, sizeof(*scsi_sense), 882 SDRETRIES, 6000, NULL, flags | SCSI_DATA_IN | SCSI_SILENT); 883 } 884 885 int 886 sd_get_optparms(sd, flags, dp) 887 struct sd_softc *sd; 888 int flags; 889 struct disk_parms *dp; 890 { 891 struct scsi_mode_sense scsi_cmd; 892 struct scsi_mode_sense_data { 893 struct scsi_mode_header header; 894 struct scsi_blk_desc blk_desc; 895 union disk_pages pages; 896 } scsi_sense; 897 u_long sectors; 898 int error; 899 900 dp->blksize = 512; 901 if ((sectors = scsi_size(sd->sc_link, flags)) == 0) 902 return 1; 903 904 /* XXX 905 * It is better to get the following params from the 906 * mode sense page 6 only (optical device parameter page). 907 * However, there are stupid optical devices which does NOT 908 * support the page 6. Ghaa.... 909 */ 910 bzero(&scsi_cmd, sizeof(scsi_cmd)); 911 scsi_cmd.opcode = MODE_SENSE; 912 scsi_cmd.page = 0x3f; /* all pages */ 913 scsi_cmd.length = sizeof(struct scsi_mode_header) + 914 sizeof(struct scsi_blk_desc); 915 916 if ((error = scsi_scsi_cmd(sd->sc_link, 917 (struct scsi_generic *)&scsi_cmd, sizeof(scsi_cmd), 918 (u_char *)&scsi_sense, sizeof(scsi_sense), SDRETRIES, 919 6000, NULL, flags | SCSI_DATA_IN)) != 0) 920 return error; 921 922 dp->blksize = _3btol(scsi_sense.blk_desc.blklen); 923 if (dp->blksize == 0) 924 dp->blksize = 512; 925 926 /* 927 * Create a pseudo-geometry. 928 */ 929 dp->heads = 64; 930 dp->sectors = 32; 931 dp->cyls = sectors / (dp->heads * dp->sectors); 932 dp->disksize = sectors; 933 934 return 0; 935 } 936 937 /* 938 * Get the scsi driver to send a full inquiry to the * device and use the 939 * results to fill out the disk parameter structure. 940 */ 941 int 942 sd_get_parms(sd, flags) 943 struct sd_softc *sd; 944 int flags; 945 { 946 struct disk_parms *dp = &sd->params; 947 struct scsi_mode_sense_data scsi_sense; 948 u_long sectors; 949 int page; 950 int error; 951 952 if (sd->type == T_OPTICAL) { 953 if ((error = sd_get_optparms(sd, flags, dp)) != 0) 954 sd->sc_link->flags &= ~SDEV_MEDIA_LOADED; 955 return error; 956 } 957 958 if ((error = sd_mode_sense(sd, &scsi_sense, page = 4, flags)) == 0) { 959 SC_DEBUG(sd->sc_link, SDEV_DB3, 960 ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n", 961 _3btol(scsi_sense.pages.rigid_geometry.ncyl), 962 scsi_sense.pages.rigid_geometry.nheads, 963 _2btol(scsi_sense.pages.rigid_geometry.st_cyl_wp), 964 _2btol(scsi_sense.pages.rigid_geometry.st_cyl_rwc), 965 _2btol(scsi_sense.pages.rigid_geometry.land_zone))); 966 967 /* 968 * KLUDGE!! (for zone recorded disks) 969 * give a number of sectors so that sec * trks * cyls 970 * is <= disk_size 971 * can lead to wasted space! THINK ABOUT THIS ! 972 */ 973 dp->heads = scsi_sense.pages.rigid_geometry.nheads; 974 dp->cyls = _3btol(scsi_sense.pages.rigid_geometry.ncyl); 975 dp->blksize = _3btol(scsi_sense.blk_desc.blklen); 976 977 if (dp->heads == 0 || dp->cyls == 0) 978 goto fake_it; 979 980 if (dp->blksize == 0) 981 dp->blksize = 512; 982 983 sectors = scsi_size(sd->sc_link, flags); 984 dp->disksize = sectors; 985 sectors /= (dp->heads * dp->cyls); 986 dp->sectors = sectors; /* XXX dubious on SCSI */ 987 988 return 0; 989 } 990 991 if ((error = sd_mode_sense(sd, &scsi_sense, page = 5, flags)) == 0) { 992 dp->heads = scsi_sense.pages.flex_geometry.nheads; 993 dp->cyls = _2btol(scsi_sense.pages.flex_geometry.ncyl); 994 dp->blksize = _3btol(scsi_sense.blk_desc.blklen); 995 dp->sectors = scsi_sense.pages.flex_geometry.ph_sec_tr; 996 dp->disksize = dp->heads * dp->cyls * dp->sectors; 997 if (dp->disksize == 0) 998 goto fake_it; 999 1000 if (dp->blksize == 0) 1001 dp->blksize = 512; 1002 1003 return 0; 1004 } 1005 1006 fake_it: 1007 if ((sd->sc_link->quirks & SDEV_NOMODESENSE) == 0) { 1008 if (error == 0) 1009 printf("%s: mode sense (%d) returned nonsense", 1010 sd->sc_dev.dv_xname, page); 1011 else 1012 printf("%s: could not mode sense (4/5)", 1013 sd->sc_dev.dv_xname); 1014 printf("; using fictitious geometry\n"); 1015 } 1016 /* 1017 * use adaptec standard fictitious geometry 1018 * this depends on which controller (e.g. 1542C is 1019 * different. but we have to put SOMETHING here..) 1020 */ 1021 sectors = scsi_size(sd->sc_link, flags); 1022 dp->heads = 64; 1023 dp->sectors = 32; 1024 dp->cyls = sectors / (64 * 32); 1025 dp->blksize = 512; 1026 dp->disksize = sectors; 1027 return 0; 1028 } 1029 1030 int 1031 sdsize(dev) 1032 dev_t dev; 1033 { 1034 struct sd_softc *sd; 1035 int part, unit, omask; 1036 int size; 1037 1038 unit = SDUNIT(dev); 1039 if (unit >= sd_cd.cd_ndevs) 1040 return (-1); 1041 sd = sd_cd.cd_devs[unit]; 1042 if (sd == NULL) 1043 return (-1); 1044 1045 part = SDPART(dev); 1046 omask = sd->sc_dk.dk_openmask & (1 << part); 1047 1048 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0) 1049 return (-1); 1050 if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP) 1051 size = -1; 1052 else 1053 size = sd->sc_dk.dk_label->d_partitions[part].p_size * 1054 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE); 1055 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0) 1056 return (-1); 1057 return (size); 1058 } 1059 1060 #ifndef __BDEVSW_DUMP_OLD_TYPE 1061 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */ 1062 static struct scsi_xfer sx; 1063 static int sddoingadump; 1064 1065 /* 1066 * dump all of physical memory into the partition specified, starting 1067 * at offset 'dumplo' into the partition. 1068 */ 1069 int 1070 sddump(dev, blkno, va, size) 1071 dev_t dev; 1072 daddr_t blkno; 1073 caddr_t va; 1074 size_t size; 1075 { 1076 struct sd_softc *sd; /* disk unit to do the I/O */ 1077 struct disklabel *lp; /* disk's disklabel */ 1078 int unit, part; 1079 int sectorsize; /* size of a disk sector */ 1080 int nsects; /* number of sectors in partition */ 1081 int sectoff; /* sector offset of partition */ 1082 int totwrt; /* total number of sectors left to write */ 1083 int nwrt; /* current number of sectors to write */ 1084 struct scsi_rw_big cmd; /* write command */ 1085 struct scsi_xfer *xs; /* ... convenience */ 1086 int retval; 1087 1088 /* Check if recursive dump; if so, punt. */ 1089 if (sddoingadump) 1090 return EFAULT; 1091 1092 /* Mark as active early. */ 1093 sddoingadump = 1; 1094 1095 unit = SDUNIT(dev); /* Decompose unit & partition. */ 1096 part = SDPART(dev); 1097 1098 /* Check for acceptable drive number. */ 1099 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL) 1100 return ENXIO; 1101 1102 /* 1103 * XXX Can't do this check, since the media might have been 1104 * XXX marked `invalid' by successful unmounting of all 1105 * XXX filesystems. 1106 */ 1107 #if 0 1108 /* Make sure it was initialized. */ 1109 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) != SDEV_MEDIA_LOADED) 1110 return ENXIO; 1111 #endif 1112 1113 /* Convert to disk sectors. Request must be a multiple of size. */ 1114 lp = sd->sc_dk.dk_label; 1115 sectorsize = lp->d_secsize; 1116 if ((size % sectorsize) != 0) 1117 return EFAULT; 1118 totwrt = size / sectorsize; 1119 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */ 1120 1121 nsects = lp->d_partitions[part].p_size; 1122 sectoff = lp->d_partitions[part].p_offset; 1123 1124 /* Check transfer bounds against partition size. */ 1125 if ((blkno < 0) || ((blkno + totwrt) > nsects)) 1126 return EINVAL; 1127 1128 /* Offset block number to start of partition. */ 1129 blkno += sectoff; 1130 1131 xs = &sx; 1132 1133 while (totwrt > 0) { 1134 nwrt = totwrt; /* XXX */ 1135 #ifndef SD_DUMP_NOT_TRUSTED 1136 /* 1137 * Fill out the scsi command 1138 */ 1139 bzero(&cmd, sizeof(cmd)); 1140 cmd.opcode = WRITE_BIG; 1141 _lto4b(blkno, cmd.addr); 1142 _lto2b(nwrt, cmd.length); 1143 /* 1144 * Fill out the scsi_xfer structure 1145 * Note: we cannot sleep as we may be an interrupt 1146 * don't use scsi_scsi_cmd() as it may want 1147 * to wait for an xs. 1148 */ 1149 bzero(xs, sizeof(sx)); 1150 xs->flags |= SCSI_AUTOCONF | INUSE | SCSI_DATA_OUT; 1151 xs->sc_link = sd->sc_link; 1152 xs->retries = SDRETRIES; 1153 xs->timeout = 10000; /* 10000 millisecs for a disk ! */ 1154 xs->cmd = (struct scsi_generic *)&cmd; 1155 xs->cmdlen = sizeof(cmd); 1156 xs->resid = nwrt * sectorsize; 1157 xs->error = XS_NOERROR; 1158 xs->bp = 0; 1159 xs->data = va; 1160 xs->datalen = nwrt * sectorsize; 1161 1162 /* 1163 * Pass all this info to the scsi driver. 1164 */ 1165 retval = (*(sd->sc_link->adapter->scsi_cmd)) (xs); 1166 if (retval != COMPLETE) 1167 return ENXIO; 1168 #else /* SD_DUMP_NOT_TRUSTED */ 1169 /* Let's just talk about this first... */ 1170 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno); 1171 delay(500 * 1000); /* half a second */ 1172 #endif /* SD_DUMP_NOT_TRUSTED */ 1173 1174 /* update block count */ 1175 totwrt -= nwrt; 1176 blkno += nwrt; 1177 va += sectorsize * nwrt; 1178 } 1179 sddoingadump = 0; 1180 return 0; 1181 } 1182 #else /* __BDEVSW_DUMP_NEW_TYPE */ 1183 int 1184 sddump(dev, blkno, va, size) 1185 dev_t dev; 1186 daddr_t blkno; 1187 caddr_t va; 1188 size_t size; 1189 { 1190 1191 /* Not implemented. */ 1192 return ENXIO; 1193 } 1194 #endif /* __BDEVSW_DUMP_NEW_TYPE */ 1195