1 /* 2 * Implementation of SCSI Direct Access Peripheral driver for CAM. 3 * 4 * Copyright (c) 1997 Justin T. Gibbs. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions, and the following disclaimer, 12 * without modification, immediately at the beginning of the file. 13 * 2. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #ifdef _KERNEL 32 #include "opt_hw_wdog.h" 33 #endif /* _KERNEL */ 34 35 #include <sys/param.h> 36 37 #ifdef _KERNEL 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/bio.h> 41 #include <sys/sysctl.h> 42 #endif /* _KERNEL */ 43 44 #include <sys/devicestat.h> 45 #include <sys/conf.h> 46 #include <sys/disk.h> 47 #include <sys/eventhandler.h> 48 #include <sys/malloc.h> 49 #include <sys/cons.h> 50 51 #include <machine/md_var.h> 52 53 #include <vm/vm.h> 54 #include <vm/pmap.h> 55 56 #ifndef _KERNEL 57 #include <stdio.h> 58 #include <string.h> 59 #endif /* _KERNEL */ 60 61 #include <cam/cam.h> 62 #include <cam/cam_ccb.h> 63 #include <cam/cam_extend.h> 64 #include <cam/cam_periph.h> 65 #include <cam/cam_xpt_periph.h> 66 67 #include <cam/scsi/scsi_message.h> 68 69 #ifndef _KERNEL 70 #include <cam/scsi/scsi_da.h> 71 #endif /* !_KERNEL */ 72 73 #ifdef _KERNEL 74 typedef enum { 75 DA_STATE_PROBE, 76 DA_STATE_NORMAL 77 } da_state; 78 79 typedef enum { 80 DA_FLAG_PACK_INVALID = 0x001, 81 DA_FLAG_NEW_PACK = 0x002, 82 DA_FLAG_PACK_LOCKED = 0x004, 83 DA_FLAG_PACK_REMOVABLE = 0x008, 84 DA_FLAG_TAGGED_QUEUING = 0x010, 85 DA_FLAG_NEED_OTAG = 0x020, 86 DA_FLAG_WENT_IDLE = 0x040, 87 DA_FLAG_RETRY_UA = 0x080, 88 DA_FLAG_OPEN = 0x100 89 } da_flags; 90 91 typedef enum { 92 DA_Q_NONE = 0x00, 93 DA_Q_NO_SYNC_CACHE = 0x01, 94 DA_Q_NO_6_BYTE = 0x02 95 } da_quirks; 96 97 typedef enum { 98 DA_CCB_PROBE = 0x01, 99 DA_CCB_BUFFER_IO = 0x02, 100 DA_CCB_WAITING = 0x03, 101 DA_CCB_DUMP = 0x04, 102 DA_CCB_TYPE_MASK = 0x0F, 103 DA_CCB_RETRY_UA = 0x10 104 } da_ccb_state; 105 106 /* Offsets into our private area for storing information */ 107 #define ccb_state ppriv_field0 108 #define ccb_bp ppriv_ptr1 109 110 struct disk_params { 111 u_int8_t heads; 112 u_int16_t cylinders; 113 u_int8_t secs_per_track; 114 u_int32_t secsize; /* Number of bytes/sector */ 115 u_int32_t sectors; /* total number sectors */ 116 }; 117 118 struct da_softc { 119 struct bio_queue_head bio_queue; 120 struct devstat device_stats; 121 SLIST_ENTRY(da_softc) links; 122 LIST_HEAD(, ccb_hdr) pending_ccbs; 123 da_state state; 124 da_flags flags; 125 da_quirks quirks; 126 int minimum_cmd_size; 127 int ordered_tag_count; 128 struct disk_params params; 129 struct disk disk; 130 union ccb saved_ccb; 131 dev_t dev; 132 }; 133 134 struct da_quirk_entry { 135 struct scsi_inquiry_pattern inq_pat; 136 da_quirks quirks; 137 }; 138 139 static const char quantum[] = "QUANTUM"; 140 static const char microp[] = "MICROP"; 141 142 static struct da_quirk_entry da_quirk_table[] = 143 { 144 { 145 /* 146 * This particular Fujitsu drive doesn't like the 147 * synchronize cache command. 148 * Reported by: Tom Jackson <toj@gorilla.net> 149 */ 150 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"}, 151 /*quirks*/ DA_Q_NO_SYNC_CACHE 152 153 }, 154 { 155 /* 156 * This drive doesn't like the synchronize cache command 157 * either. Reported by: Matthew Jacob <mjacob@feral.com> 158 * in NetBSD PR kern/6027, August 24, 1998. 159 */ 160 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"}, 161 /*quirks*/ DA_Q_NO_SYNC_CACHE 162 }, 163 { 164 /* 165 * This drive doesn't like the synchronize cache command 166 * either. Reported by: Hellmuth Michaelis (hm@kts.org) 167 * (PR 8882). 168 */ 169 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"}, 170 /*quirks*/ DA_Q_NO_SYNC_CACHE 171 }, 172 { 173 /* 174 * Doesn't like the synchronize cache command. 175 * Reported by: Blaz Zupan <blaz@gold.amis.net> 176 */ 177 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"}, 178 /*quirks*/ DA_Q_NO_SYNC_CACHE 179 }, 180 { 181 /* 182 * Doesn't like the synchronize cache command. 183 */ 184 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"}, 185 /*quirks*/ DA_Q_NO_SYNC_CACHE 186 }, 187 { 188 /* 189 * Doesn't like the synchronize cache command. 190 */ 191 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"}, 192 /*quirks*/ DA_Q_NO_SYNC_CACHE 193 }, 194 { 195 /* 196 * Doesn't work correctly with 6 byte reads/writes. 197 * Returns illegal request, and points to byte 9 of the 198 * 6-byte CDB. 199 * Reported by: Adam McDougall <bsdx@spawnet.com> 200 */ 201 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"}, 202 /*quirks*/ DA_Q_NO_6_BYTE 203 }, 204 { 205 /* 206 * See above. 207 */ 208 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"}, 209 /*quirks*/ DA_Q_NO_6_BYTE 210 }, 211 212 /* Below a list of quirks for USB devices supported by umass. */ 213 { 214 /* 215 * This USB floppy drive uses the UFI command set. This 216 * command set is a derivative of the ATAPI command set and 217 * does not support READ_6 commands only READ_10. It also does 218 * not support sync cache (0x35). 219 */ 220 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Y-E DATA", "USB-FDU", "*"}, 221 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 222 }, 223 { 224 /* Another USB floppy */ 225 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MATSHITA", "FDD CF-VFDU*","*"}, 226 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 227 }, 228 { 229 /* 230 * Sony Memory Stick adapter MSAC-US1 and 231 * Sony PCG-C1VJ Internal Memory Stick Slot (MSC-U01). 232 * Make all sony MS* products use this quirk. 233 */ 234 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "MS*", "*"}, 235 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 236 }, 237 { 238 /* 239 * Sony Memory Stick adapter for the CLIE series 240 * of PalmOS PDA's 241 */ 242 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "CLIE*", "*"}, 243 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 244 }, 245 { 246 /* 247 * Sony DSC cameras (DSC-S30, DSC-S50, DSC-S70) 248 */ 249 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"}, 250 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 251 }, 252 { 253 /* 254 * Maxtor 3000LE USB Drive 255 */ 256 {T_DIRECT, SIP_MEDIA_FIXED, "MAXTOR*", "K040H2*", "*"}, 257 /*quirks*/ DA_Q_NO_6_BYTE 258 }, 259 { 260 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "MCF3064AP", "*"}, 261 /*quirks*/ DA_Q_NO_6_BYTE 262 }, 263 { 264 /* 265 * Microtech USB CameraMate 266 */ 267 {T_DIRECT, SIP_MEDIA_REMOVABLE, "eUSB Compact*", "Compact Flash*", "*"}, 268 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 269 }, 270 { 271 /* 272 * The vendor, product and version strings coming from the 273 * controller are null terminated instead of being padded with 274 * spaces. The trailing wildcard character '*' is required. 275 */ 276 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SMSC*", "USB FDC*","*"}, 277 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 278 }, 279 { 280 /* 281 * Olympus digital cameras (C-3040ZOOM, C-2040ZOOM, C-1) 282 */ 283 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "C-*", "*"}, 284 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 285 }, 286 { 287 /* 288 * Olympus E-100RS digital camera. 289 * Reported by: Bernd Walter <ticso@cicely8.cicely.de> 290 * XXX See above; its likely all Olympus digital cameras 291 * have the same quirk, but I cannot confirm. - kbyanc 292 */ 293 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "E-100RS", "*"}, 294 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 295 }, 296 { 297 /* 298 * KingByte Pen Drives 299 */ 300 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NO BRAND", "PEN DRIVE", "*"}, 301 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 302 }, 303 { 304 /* 305 * FujiFilm Camera 306 */ 307 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJIFILMUSB-DRIVEUNIT", "USB-DRIVEUNIT", "*"}, 308 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 309 }, 310 { 311 /* 312 * Nikon Coolpix 995 313 */ 314 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NIKON", "NIKON DSC E995", "*"}, 315 /*quirks*/ DA_Q_NO_6_BYTE 316 }, 317 { 318 /* 319 * Minolta Dimage 2330 320 */ 321 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MINOLTA", "DIMAGE 2330*", "*"}, 322 /*quirks*/ DA_Q_NO_6_BYTE 323 } 324 }; 325 326 static d_open_t daopen; 327 static d_close_t daclose; 328 static d_strategy_t dastrategy; 329 static d_ioctl_t daioctl; 330 static d_dump_t dadump; 331 static periph_init_t dainit; 332 static void daasync(void *callback_arg, u_int32_t code, 333 struct cam_path *path, void *arg); 334 static periph_ctor_t daregister; 335 static periph_dtor_t dacleanup; 336 static periph_start_t dastart; 337 static periph_oninv_t daoninvalidate; 338 static void dadone(struct cam_periph *periph, 339 union ccb *done_ccb); 340 static int daerror(union ccb *ccb, u_int32_t cam_flags, 341 u_int32_t sense_flags); 342 static void daprevent(struct cam_periph *periph, int action); 343 static void dasetgeom(struct cam_periph *periph, 344 struct scsi_read_capacity_data * rdcap); 345 static timeout_t dasendorderedtag; 346 static void dashutdown(void *arg, int howto); 347 348 #ifndef DA_DEFAULT_TIMEOUT 349 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */ 350 #endif 351 352 #ifndef DA_DEFAULT_RETRY 353 #define DA_DEFAULT_RETRY 4 354 #endif 355 356 static int da_retry_count = DA_DEFAULT_RETRY; 357 static int da_default_timeout = DA_DEFAULT_TIMEOUT; 358 359 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem"); 360 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0, 361 "CAM Direct Access Disk driver"); 362 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW, 363 &da_retry_count, 0, "Normal I/O retry count"); 364 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW, 365 &da_default_timeout, 0, "Normal I/O timeout (in seconds)"); 366 367 /* 368 * DA_ORDEREDTAG_INTERVAL determines how often, relative 369 * to the default timeout, we check to see whether an ordered 370 * tagged transaction is appropriate to prevent simple tag 371 * starvation. Since we'd like to ensure that there is at least 372 * 1/2 of the timeout length left for a starved transaction to 373 * complete after we've sent an ordered tag, we must poll at least 374 * four times in every timeout period. This takes care of the worst 375 * case where a starved transaction starts during an interval that 376 * meets the requirement "don't send an ordered tag" test so it takes 377 * us two intervals to determine that a tag must be sent. 378 */ 379 #ifndef DA_ORDEREDTAG_INTERVAL 380 #define DA_ORDEREDTAG_INTERVAL 4 381 #endif 382 383 static struct periph_driver dadriver = 384 { 385 dainit, "da", 386 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0 387 }; 388 389 PERIPHDRIVER_DECLARE(da, dadriver); 390 391 #define DA_CDEV_MAJOR 13 392 393 /* For 2.2-stable support */ 394 #ifndef D_DISK 395 #define D_DISK 0 396 #endif 397 398 static struct cdevsw da_cdevsw = { 399 /* open */ daopen, 400 /* close */ daclose, 401 /* read */ physread, 402 /* write */ physwrite, 403 /* ioctl */ daioctl, 404 /* poll */ nopoll, 405 /* mmap */ nommap, 406 /* strategy */ dastrategy, 407 /* name */ "da", 408 /* maj */ DA_CDEV_MAJOR, 409 /* dump */ dadump, 410 /* psize */ nopsize, 411 /* flags */ D_DISK, 412 }; 413 414 static struct cdevsw dadisk_cdevsw; 415 416 static SLIST_HEAD(,da_softc) softc_list; 417 static struct extend_array *daperiphs; 418 419 static int 420 daopen(dev_t dev, int flags, int fmt, struct thread *td) 421 { 422 struct cam_periph *periph; 423 struct da_softc *softc; 424 struct disklabel *label; 425 struct scsi_read_capacity_data *rcap; 426 union ccb *ccb; 427 int unit; 428 int part; 429 int error; 430 int s; 431 432 unit = dkunit(dev); 433 part = dkpart(dev); 434 s = splsoftcam(); 435 periph = cam_extend_get(daperiphs, unit); 436 if (periph == NULL) { 437 splx(s); 438 return (ENXIO); 439 } 440 441 softc = (struct da_softc *)periph->softc; 442 443 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 444 ("daopen: dev=%s (unit %d , partition %d)\n", devtoname(dev), 445 unit, part)); 446 447 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) 448 return (error); /* error code from tsleep */ 449 450 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 451 return(ENXIO); 452 softc->flags |= DA_FLAG_OPEN; 453 454 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) { 455 /* Invalidate our pack information. */ 456 disk_invalidate(&softc->disk); 457 softc->flags &= ~DA_FLAG_PACK_INVALID; 458 } 459 splx(s); 460 461 /* Do a read capacity */ 462 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap), 463 M_TEMP, 464 M_WAITOK); 465 466 ccb = cam_periph_getccb(periph, /*priority*/1); 467 scsi_read_capacity(&ccb->csio, 468 /*retries*/4, 469 /*cbfncp*/dadone, 470 MSG_SIMPLE_Q_TAG, 471 rcap, 472 SSD_FULL_SIZE, 473 /*timeout*/60000); 474 ccb->ccb_h.ccb_bp = NULL; 475 476 error = cam_periph_runccb(ccb, daerror, 477 /*cam_flags*/CAM_RETRY_SELTO, 478 /*sense_flags*/SF_RETRY_UA, 479 &softc->device_stats); 480 481 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 482 cam_release_devq(ccb->ccb_h.path, 483 /*relsim_flags*/0, 484 /*reduction*/0, 485 /*timeout*/0, 486 /*getcount_only*/0); 487 xpt_release_ccb(ccb); 488 489 if (error == 0) 490 dasetgeom(periph, rcap); 491 492 free(rcap, M_TEMP); 493 494 if (error == 0) { 495 struct ccb_getdev cgd; 496 497 /* Build label for whole disk. */ 498 label = &softc->disk.d_label; 499 bzero(label, sizeof(*label)); 500 label->d_type = DTYPE_SCSI; 501 502 /* 503 * Grab the inquiry data to get the vendor and product names. 504 * Put them in the typename and packname for the label. 505 */ 506 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1); 507 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 508 xpt_action((union ccb *)&cgd); 509 510 strncpy(label->d_typename, cgd.inq_data.vendor, 511 min(SID_VENDOR_SIZE, sizeof(label->d_typename))); 512 strncpy(label->d_packname, cgd.inq_data.product, 513 min(SID_PRODUCT_SIZE, sizeof(label->d_packname))); 514 515 label->d_secsize = softc->params.secsize; 516 label->d_nsectors = softc->params.secs_per_track; 517 label->d_ntracks = softc->params.heads; 518 label->d_ncylinders = softc->params.cylinders; 519 label->d_secpercyl = softc->params.heads 520 * softc->params.secs_per_track; 521 label->d_secperunit = softc->params.sectors; 522 523 /* 524 * Check to see whether or not the blocksize is set yet. 525 * If it isn't, set it and then clear the blocksize 526 * unavailable flag for the device statistics. 527 */ 528 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){ 529 softc->device_stats.block_size = softc->params.secsize; 530 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE; 531 } 532 } 533 534 if (error == 0) { 535 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) 536 daprevent(periph, PR_PREVENT); 537 } 538 cam_periph_unlock(periph); 539 return (error); 540 } 541 542 static int 543 daclose(dev_t dev, int flag, int fmt, struct thread *td) 544 { 545 struct cam_periph *periph; 546 struct da_softc *softc; 547 int unit; 548 int error; 549 550 unit = dkunit(dev); 551 periph = cam_extend_get(daperiphs, unit); 552 if (periph == NULL) 553 return (ENXIO); 554 555 softc = (struct da_softc *)periph->softc; 556 557 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) { 558 return (error); /* error code from tsleep */ 559 } 560 561 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) { 562 union ccb *ccb; 563 564 ccb = cam_periph_getccb(periph, /*priority*/1); 565 566 scsi_synchronize_cache(&ccb->csio, 567 /*retries*/1, 568 /*cbfcnp*/dadone, 569 MSG_SIMPLE_Q_TAG, 570 /*begin_lba*/0,/* Cover the whole disk */ 571 /*lb_count*/0, 572 SSD_FULL_SIZE, 573 5 * 60 * 1000); 574 575 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0, 576 /*sense_flags*/SF_RETRY_UA, 577 &softc->device_stats); 578 579 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 580 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 581 CAM_SCSI_STATUS_ERROR) { 582 int asc, ascq; 583 int sense_key, error_code; 584 585 scsi_extract_sense(&ccb->csio.sense_data, 586 &error_code, 587 &sense_key, 588 &asc, &ascq); 589 if (sense_key != SSD_KEY_ILLEGAL_REQUEST) 590 scsi_sense_print(&ccb->csio); 591 } else { 592 xpt_print_path(periph->path); 593 printf("Synchronize cache failed, status " 594 "== 0x%x, scsi status == 0x%x\n", 595 ccb->csio.ccb_h.status, 596 ccb->csio.scsi_status); 597 } 598 } 599 600 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 601 cam_release_devq(ccb->ccb_h.path, 602 /*relsim_flags*/0, 603 /*reduction*/0, 604 /*timeout*/0, 605 /*getcount_only*/0); 606 607 xpt_release_ccb(ccb); 608 609 } 610 611 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) { 612 daprevent(periph, PR_ALLOW); 613 /* 614 * If we've got removeable media, mark the blocksize as 615 * unavailable, since it could change when new media is 616 * inserted. 617 */ 618 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE; 619 } 620 621 softc->flags &= ~DA_FLAG_OPEN; 622 cam_periph_unlock(periph); 623 cam_periph_release(periph); 624 return (0); 625 } 626 627 /* 628 * Actually translate the requested transfer into one the physical driver 629 * can understand. The transfer is described by a buf and will include 630 * only one physical transfer. 631 */ 632 static void 633 dastrategy(struct bio *bp) 634 { 635 struct cam_periph *periph; 636 struct da_softc *softc; 637 u_int unit; 638 u_int part; 639 int s; 640 641 unit = dkunit(bp->bio_dev); 642 part = dkpart(bp->bio_dev); 643 periph = cam_extend_get(daperiphs, unit); 644 if (periph == NULL) { 645 biofinish(bp, NULL, ENXIO); 646 return; 647 } 648 softc = (struct da_softc *)periph->softc; 649 #if 0 650 /* 651 * check it's not too big a transfer for our adapter 652 */ 653 scsi_minphys(bp,&sd_switch); 654 #endif 655 656 /* 657 * Mask interrupts so that the pack cannot be invalidated until 658 * after we are in the queue. Otherwise, we might not properly 659 * clean up one of the buffers. 660 */ 661 s = splbio(); 662 663 /* 664 * If the device has been made invalid, error out 665 */ 666 if ((softc->flags & DA_FLAG_PACK_INVALID)) { 667 splx(s); 668 biofinish(bp, NULL, ENXIO); 669 return; 670 } 671 672 /* 673 * Place it in the queue of disk activities for this disk 674 */ 675 bioqdisksort(&softc->bio_queue, bp); 676 677 splx(s); 678 679 /* 680 * Schedule ourselves for performing the work. 681 */ 682 xpt_schedule(periph, /* XXX priority */1); 683 684 return; 685 } 686 687 /* For 2.2-stable support */ 688 #ifndef ENOIOCTL 689 #define ENOIOCTL -1 690 #endif 691 692 static int 693 daioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 694 { 695 struct cam_periph *periph; 696 struct da_softc *softc; 697 int unit; 698 int error; 699 700 unit = dkunit(dev); 701 periph = cam_extend_get(daperiphs, unit); 702 if (periph == NULL) 703 return (ENXIO); 704 705 softc = (struct da_softc *)periph->softc; 706 707 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("daioctl\n")); 708 709 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) { 710 return (error); /* error code from tsleep */ 711 } 712 713 error = cam_periph_ioctl(periph, cmd, addr, daerror); 714 715 cam_periph_unlock(periph); 716 717 return (error); 718 } 719 720 static int 721 dadump(dev_t dev) 722 { 723 struct cam_periph *periph; 724 struct da_softc *softc; 725 u_int unit; 726 u_int part; 727 u_int secsize; 728 u_int num; /* number of sectors to write */ 729 u_int blknum; 730 long blkcnt; 731 vm_offset_t addr; 732 struct ccb_scsiio csio; 733 int dumppages = MAXDUMPPGS; 734 int error; 735 int i; 736 737 /* toss any characters present prior to dump */ 738 while (cncheckc() != -1) 739 ; 740 741 unit = dkunit(dev); 742 part = dkpart(dev); 743 periph = cam_extend_get(daperiphs, unit); 744 if (periph == NULL) { 745 return (ENXIO); 746 } 747 softc = (struct da_softc *)periph->softc; 748 749 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) 750 return (ENXIO); 751 752 error = disk_dumpcheck(dev, &num, &blknum, &secsize); 753 if (error) 754 return (error); 755 756 addr = 0; /* starting address */ 757 blkcnt = howmany(PAGE_SIZE, secsize); 758 759 while (num > 0) { 760 caddr_t va = NULL; 761 762 if ((num / blkcnt) < dumppages) 763 dumppages = num / blkcnt; 764 765 for (i = 0; i < dumppages; ++i) { 766 vm_offset_t a = addr + (i * PAGE_SIZE); 767 if (is_physical_memory(a)) 768 va = pmap_kenter_temporary(trunc_page(a), i); 769 else 770 va = pmap_kenter_temporary(trunc_page(0), i); 771 } 772 773 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1); 774 csio.ccb_h.ccb_state = DA_CCB_DUMP; 775 scsi_read_write(&csio, 776 /*retries*/1, 777 dadone, 778 MSG_ORDERED_Q_TAG, 779 /*read*/FALSE, 780 /*byte2*/0, 781 /*minimum_cmd_size*/ softc->minimum_cmd_size, 782 blknum, 783 blkcnt * dumppages, 784 /*data_ptr*/(u_int8_t *) va, 785 /*dxfer_len*/blkcnt * secsize * dumppages, 786 /*sense_len*/SSD_FULL_SIZE, 787 DA_DEFAULT_TIMEOUT * 1000); 788 xpt_polled_action((union ccb *)&csio); 789 790 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 791 printf("Aborting dump due to I/O error.\n"); 792 if ((csio.ccb_h.status & CAM_STATUS_MASK) == 793 CAM_SCSI_STATUS_ERROR) 794 scsi_sense_print(&csio); 795 else 796 printf("status == 0x%x, scsi status == 0x%x\n", 797 csio.ccb_h.status, csio.scsi_status); 798 return(EIO); 799 } 800 801 if (dumpstatus(addr, (off_t)num * softc->params.secsize) < 0) 802 return (EINTR); 803 804 /* update block count */ 805 num -= blkcnt * dumppages; 806 blknum += blkcnt * dumppages; 807 addr += PAGE_SIZE * dumppages; 808 } 809 810 /* 811 * Sync the disk cache contents to the physical media. 812 */ 813 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) { 814 815 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1); 816 csio.ccb_h.ccb_state = DA_CCB_DUMP; 817 scsi_synchronize_cache(&csio, 818 /*retries*/1, 819 /*cbfcnp*/dadone, 820 MSG_SIMPLE_Q_TAG, 821 /*begin_lba*/0,/* Cover the whole disk */ 822 /*lb_count*/0, 823 SSD_FULL_SIZE, 824 5 * 60 * 1000); 825 xpt_polled_action((union ccb *)&csio); 826 827 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 828 if ((csio.ccb_h.status & CAM_STATUS_MASK) == 829 CAM_SCSI_STATUS_ERROR) { 830 int asc, ascq; 831 int sense_key, error_code; 832 833 scsi_extract_sense(&csio.sense_data, 834 &error_code, 835 &sense_key, 836 &asc, &ascq); 837 if (sense_key != SSD_KEY_ILLEGAL_REQUEST) 838 scsi_sense_print(&csio); 839 } else { 840 xpt_print_path(periph->path); 841 printf("Synchronize cache failed, status " 842 "== 0x%x, scsi status == 0x%x\n", 843 csio.ccb_h.status, csio.scsi_status); 844 } 845 } 846 } 847 return (0); 848 } 849 850 static void 851 dainit(void) 852 { 853 cam_status status; 854 struct cam_path *path; 855 856 /* 857 * Create our extend array for storing the devices we attach to. 858 */ 859 daperiphs = cam_extend_new(); 860 SLIST_INIT(&softc_list); 861 if (daperiphs == NULL) { 862 printf("da: Failed to alloc extend array!\n"); 863 return; 864 } 865 866 /* 867 * Install a global async callback. This callback will 868 * receive async callbacks like "new device found". 869 */ 870 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID, 871 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 872 873 if (status == CAM_REQ_CMP) { 874 struct ccb_setasync csa; 875 876 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5); 877 csa.ccb_h.func_code = XPT_SASYNC_CB; 878 csa.event_enable = AC_FOUND_DEVICE; 879 csa.callback = daasync; 880 csa.callback_arg = NULL; 881 xpt_action((union ccb *)&csa); 882 status = csa.ccb_h.status; 883 xpt_free_path(path); 884 } 885 886 if (status != CAM_REQ_CMP) { 887 printf("da: Failed to attach master async callback " 888 "due to status 0x%x!\n", status); 889 } else { 890 891 /* 892 * Schedule a periodic event to occasionally send an 893 * ordered tag to a device. 894 */ 895 timeout(dasendorderedtag, NULL, 896 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL); 897 898 /* Register our shutdown event handler */ 899 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown, 900 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 901 printf("dainit: shutdown event registration failed!\n"); 902 } 903 } 904 905 static void 906 daoninvalidate(struct cam_periph *periph) 907 { 908 int s; 909 struct da_softc *softc; 910 struct bio *q_bp; 911 struct ccb_setasync csa; 912 913 softc = (struct da_softc *)periph->softc; 914 915 /* 916 * De-register any async callbacks. 917 */ 918 xpt_setup_ccb(&csa.ccb_h, periph->path, 919 /* priority */ 5); 920 csa.ccb_h.func_code = XPT_SASYNC_CB; 921 csa.event_enable = 0; 922 csa.callback = daasync; 923 csa.callback_arg = periph; 924 xpt_action((union ccb *)&csa); 925 926 softc->flags |= DA_FLAG_PACK_INVALID; 927 928 /* 929 * Although the oninvalidate() routines are always called at 930 * splsoftcam, we need to be at splbio() here to keep the buffer 931 * queue from being modified while we traverse it. 932 */ 933 s = splbio(); 934 935 /* 936 * Return all queued I/O with ENXIO. 937 * XXX Handle any transactions queued to the card 938 * with XPT_ABORT_CCB. 939 */ 940 while ((q_bp = bioq_first(&softc->bio_queue)) != NULL){ 941 bioq_remove(&softc->bio_queue, q_bp); 942 q_bp->bio_resid = q_bp->bio_bcount; 943 biofinish(q_bp, NULL, ENXIO); 944 } 945 splx(s); 946 947 SLIST_REMOVE(&softc_list, softc, da_softc, links); 948 949 xpt_print_path(periph->path); 950 printf("lost device\n"); 951 } 952 953 static void 954 dacleanup(struct cam_periph *periph) 955 { 956 struct da_softc *softc; 957 958 softc = (struct da_softc *)periph->softc; 959 960 devstat_remove_entry(&softc->device_stats); 961 cam_extend_release(daperiphs, periph->unit_number); 962 xpt_print_path(periph->path); 963 printf("removing device entry\n"); 964 if (softc->dev) { 965 disk_destroy(softc->dev); 966 } 967 free(softc, M_DEVBUF); 968 } 969 970 static void 971 daasync(void *callback_arg, u_int32_t code, 972 struct cam_path *path, void *arg) 973 { 974 struct cam_periph *periph; 975 976 periph = (struct cam_periph *)callback_arg; 977 switch (code) { 978 case AC_FOUND_DEVICE: 979 { 980 struct ccb_getdev *cgd; 981 cam_status status; 982 983 cgd = (struct ccb_getdev *)arg; 984 if (cgd == NULL) 985 break; 986 987 if (SID_TYPE(&cgd->inq_data) != T_DIRECT 988 && SID_TYPE(&cgd->inq_data) != T_OPTICAL) 989 break; 990 991 /* 992 * Allocate a peripheral instance for 993 * this device and start the probe 994 * process. 995 */ 996 status = cam_periph_alloc(daregister, daoninvalidate, 997 dacleanup, dastart, 998 "da", CAM_PERIPH_BIO, 999 cgd->ccb_h.path, daasync, 1000 AC_FOUND_DEVICE, cgd); 1001 1002 if (status != CAM_REQ_CMP 1003 && status != CAM_REQ_INPROG) 1004 printf("daasync: Unable to attach to new device " 1005 "due to status 0x%x\n", status); 1006 break; 1007 } 1008 case AC_SENT_BDR: 1009 case AC_BUS_RESET: 1010 { 1011 struct da_softc *softc; 1012 struct ccb_hdr *ccbh; 1013 int s; 1014 1015 softc = (struct da_softc *)periph->softc; 1016 s = splsoftcam(); 1017 /* 1018 * Don't fail on the expected unit attention 1019 * that will occur. 1020 */ 1021 softc->flags |= DA_FLAG_RETRY_UA; 1022 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le) 1023 ccbh->ccb_state |= DA_CCB_RETRY_UA; 1024 splx(s); 1025 /* FALLTHROUGH*/ 1026 } 1027 default: 1028 cam_periph_async(periph, code, path, arg); 1029 break; 1030 } 1031 } 1032 1033 static cam_status 1034 daregister(struct cam_periph *periph, void *arg) 1035 { 1036 int s; 1037 struct da_softc *softc; 1038 struct ccb_setasync csa; 1039 struct ccb_getdev *cgd; 1040 caddr_t match; 1041 1042 cgd = (struct ccb_getdev *)arg; 1043 if (periph == NULL) { 1044 printf("daregister: periph was NULL!!\n"); 1045 return(CAM_REQ_CMP_ERR); 1046 } 1047 1048 if (cgd == NULL) { 1049 printf("daregister: no getdev CCB, can't register device\n"); 1050 return(CAM_REQ_CMP_ERR); 1051 } 1052 1053 softc = (struct da_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT); 1054 1055 if (softc == NULL) { 1056 printf("daregister: Unable to probe new device. " 1057 "Unable to allocate softc\n"); 1058 return(CAM_REQ_CMP_ERR); 1059 } 1060 1061 bzero(softc, sizeof(*softc)); 1062 LIST_INIT(&softc->pending_ccbs); 1063 softc->state = DA_STATE_PROBE; 1064 bioq_init(&softc->bio_queue); 1065 if (SID_IS_REMOVABLE(&cgd->inq_data)) 1066 softc->flags |= DA_FLAG_PACK_REMOVABLE; 1067 if ((cgd->inq_data.flags & SID_CmdQue) != 0) 1068 softc->flags |= DA_FLAG_TAGGED_QUEUING; 1069 1070 periph->softc = softc; 1071 1072 cam_extend_set(daperiphs, periph->unit_number, periph); 1073 1074 /* 1075 * See if this device has any quirks. 1076 */ 1077 match = cam_quirkmatch((caddr_t)&cgd->inq_data, 1078 (caddr_t)da_quirk_table, 1079 sizeof(da_quirk_table)/sizeof(*da_quirk_table), 1080 sizeof(*da_quirk_table), scsi_inquiry_match); 1081 1082 if (match != NULL) 1083 softc->quirks = ((struct da_quirk_entry *)match)->quirks; 1084 else 1085 softc->quirks = DA_Q_NONE; 1086 1087 if (softc->quirks & DA_Q_NO_6_BYTE) 1088 softc->minimum_cmd_size = 10; 1089 else 1090 softc->minimum_cmd_size = 6; 1091 1092 /* 1093 * Block our timeout handler while we 1094 * add this softc to the dev list. 1095 */ 1096 s = splsoftclock(); 1097 SLIST_INSERT_HEAD(&softc_list, softc, links); 1098 splx(s); 1099 1100 /* 1101 * The DA driver supports a blocksize, but 1102 * we don't know the blocksize until we do 1103 * a read capacity. So, set a flag to 1104 * indicate that the blocksize is 1105 * unavailable right now. We'll clear the 1106 * flag as soon as we've done a read capacity. 1107 */ 1108 devstat_add_entry(&softc->device_stats, "da", 1109 periph->unit_number, 0, 1110 DEVSTAT_BS_UNAVAILABLE, 1111 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI, 1112 DEVSTAT_PRIORITY_DISK); 1113 1114 /* 1115 * Register this media as a disk 1116 */ 1117 softc->dev = disk_create(periph->unit_number, &softc->disk, 0, 1118 &da_cdevsw, &dadisk_cdevsw); 1119 1120 /* 1121 * Add async callbacks for bus reset and 1122 * bus device reset calls. I don't bother 1123 * checking if this fails as, in most cases, 1124 * the system will function just fine without 1125 * them and the only alternative would be to 1126 * not attach the device on failure. 1127 */ 1128 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5); 1129 csa.ccb_h.func_code = XPT_SASYNC_CB; 1130 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE; 1131 csa.callback = daasync; 1132 csa.callback_arg = periph; 1133 xpt_action((union ccb *)&csa); 1134 /* 1135 * Lock this peripheral until we are setup. 1136 * This first call can't block 1137 */ 1138 (void)cam_periph_lock(periph, PRIBIO); 1139 xpt_schedule(periph, /*priority*/5); 1140 1141 return(CAM_REQ_CMP); 1142 } 1143 1144 static void 1145 dastart(struct cam_periph *periph, union ccb *start_ccb) 1146 { 1147 struct da_softc *softc; 1148 1149 softc = (struct da_softc *)periph->softc; 1150 1151 1152 switch (softc->state) { 1153 case DA_STATE_NORMAL: 1154 { 1155 /* Pull a buffer from the queue and get going on it */ 1156 struct bio *bp; 1157 int s; 1158 1159 /* 1160 * See if there is a buf with work for us to do.. 1161 */ 1162 s = splbio(); 1163 bp = bioq_first(&softc->bio_queue); 1164 if (periph->immediate_priority <= periph->pinfo.priority) { 1165 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE, 1166 ("queuing for immediate ccb\n")); 1167 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING; 1168 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 1169 periph_links.sle); 1170 periph->immediate_priority = CAM_PRIORITY_NONE; 1171 splx(s); 1172 wakeup(&periph->ccb_list); 1173 } else if (bp == NULL) { 1174 splx(s); 1175 xpt_release_ccb(start_ccb); 1176 } else { 1177 int oldspl; 1178 u_int8_t tag_code; 1179 1180 bioq_remove(&softc->bio_queue, bp); 1181 1182 devstat_start_transaction(&softc->device_stats); 1183 1184 if ((bp->bio_flags & BIO_ORDERED) != 0 1185 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) { 1186 softc->flags &= ~DA_FLAG_NEED_OTAG; 1187 softc->ordered_tag_count++; 1188 tag_code = MSG_ORDERED_Q_TAG; 1189 } else { 1190 tag_code = MSG_SIMPLE_Q_TAG; 1191 } 1192 scsi_read_write(&start_ccb->csio, 1193 /*retries*/da_retry_count, 1194 dadone, 1195 tag_code, 1196 bp->bio_cmd == BIO_READ, 1197 /*byte2*/0, 1198 softc->minimum_cmd_size, 1199 bp->bio_pblkno, 1200 bp->bio_bcount / softc->params.secsize, 1201 bp->bio_data, 1202 bp->bio_bcount, 1203 /*sense_len*/SSD_FULL_SIZE, 1204 da_default_timeout * 1000); 1205 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO; 1206 1207 /* 1208 * Block out any asyncronous callbacks 1209 * while we touch the pending ccb list. 1210 */ 1211 oldspl = splcam(); 1212 LIST_INSERT_HEAD(&softc->pending_ccbs, 1213 &start_ccb->ccb_h, periph_links.le); 1214 splx(oldspl); 1215 1216 /* We expect a unit attention from this device */ 1217 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) { 1218 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA; 1219 softc->flags &= ~DA_FLAG_RETRY_UA; 1220 } 1221 1222 start_ccb->ccb_h.ccb_bp = bp; 1223 bp = bioq_first(&softc->bio_queue); 1224 splx(s); 1225 1226 xpt_action(start_ccb); 1227 } 1228 1229 if (bp != NULL) { 1230 /* Have more work to do, so ensure we stay scheduled */ 1231 xpt_schedule(periph, /* XXX priority */1); 1232 } 1233 break; 1234 } 1235 case DA_STATE_PROBE: 1236 { 1237 struct ccb_scsiio *csio; 1238 struct scsi_read_capacity_data *rcap; 1239 1240 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap), 1241 M_TEMP, 1242 M_NOWAIT); 1243 if (rcap == NULL) { 1244 printf("dastart: Couldn't malloc read_capacity data\n"); 1245 /* da_free_periph??? */ 1246 break; 1247 } 1248 csio = &start_ccb->csio; 1249 scsi_read_capacity(csio, 1250 /*retries*/4, 1251 dadone, 1252 MSG_SIMPLE_Q_TAG, 1253 rcap, 1254 SSD_FULL_SIZE, 1255 /*timeout*/5000); 1256 start_ccb->ccb_h.ccb_bp = NULL; 1257 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE; 1258 xpt_action(start_ccb); 1259 break; 1260 } 1261 } 1262 } 1263 1264 1265 static void 1266 dadone(struct cam_periph *periph, union ccb *done_ccb) 1267 { 1268 struct da_softc *softc; 1269 struct ccb_scsiio *csio; 1270 1271 softc = (struct da_softc *)periph->softc; 1272 csio = &done_ccb->csio; 1273 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) { 1274 case DA_CCB_BUFFER_IO: 1275 { 1276 struct bio *bp; 1277 int oldspl; 1278 1279 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 1280 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1281 int error; 1282 int s; 1283 int sf; 1284 1285 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0) 1286 sf = SF_RETRY_UA; 1287 else 1288 sf = 0; 1289 1290 error = daerror(done_ccb, CAM_RETRY_SELTO, sf); 1291 if (error == ERESTART) { 1292 /* 1293 * A retry was scheuled, so 1294 * just return. 1295 */ 1296 return; 1297 } 1298 if (error != 0) { 1299 struct bio *q_bp; 1300 1301 s = splbio(); 1302 1303 if (error == ENXIO) { 1304 /* 1305 * Catastrophic error. Mark our pack as 1306 * invalid. 1307 */ 1308 /* XXX See if this is really a media 1309 * change first. 1310 */ 1311 xpt_print_path(periph->path); 1312 printf("Invalidating pack\n"); 1313 softc->flags |= DA_FLAG_PACK_INVALID; 1314 } 1315 1316 /* 1317 * return all queued I/O with EIO, so that 1318 * the client can retry these I/Os in the 1319 * proper order should it attempt to recover. 1320 */ 1321 while ((q_bp = bioq_first(&softc->bio_queue)) 1322 != NULL) { 1323 bioq_remove(&softc->bio_queue, q_bp); 1324 q_bp->bio_resid = q_bp->bio_bcount; 1325 biofinish(q_bp, NULL, EIO); 1326 } 1327 splx(s); 1328 bp->bio_error = error; 1329 bp->bio_resid = bp->bio_bcount; 1330 bp->bio_flags |= BIO_ERROR; 1331 } else { 1332 bp->bio_resid = csio->resid; 1333 bp->bio_error = 0; 1334 if (bp->bio_resid != 0) { 1335 /* Short transfer ??? */ 1336 bp->bio_flags |= BIO_ERROR; 1337 } 1338 } 1339 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1340 cam_release_devq(done_ccb->ccb_h.path, 1341 /*relsim_flags*/0, 1342 /*reduction*/0, 1343 /*timeout*/0, 1344 /*getcount_only*/0); 1345 } else { 1346 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1347 panic("REQ_CMP with QFRZN"); 1348 bp->bio_resid = csio->resid; 1349 if (csio->resid > 0) 1350 bp->bio_flags |= BIO_ERROR; 1351 } 1352 1353 /* 1354 * Block out any asyncronous callbacks 1355 * while we touch the pending ccb list. 1356 */ 1357 oldspl = splcam(); 1358 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 1359 splx(oldspl); 1360 1361 if (softc->device_stats.busy_count == 0) 1362 softc->flags |= DA_FLAG_WENT_IDLE; 1363 1364 biofinish(bp, &softc->device_stats, 0); 1365 break; 1366 } 1367 case DA_CCB_PROBE: 1368 { 1369 struct scsi_read_capacity_data *rdcap; 1370 char announce_buf[80]; 1371 1372 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr; 1373 1374 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1375 struct disk_params *dp; 1376 1377 dasetgeom(periph, rdcap); 1378 dp = &softc->params; 1379 snprintf(announce_buf, sizeof(announce_buf), 1380 "%luMB (%u %u byte sectors: %dH %dS/T %dC)", 1381 (unsigned long) (((u_int64_t)dp->secsize * 1382 dp->sectors) / (1024*1024)), dp->sectors, 1383 dp->secsize, dp->heads, dp->secs_per_track, 1384 dp->cylinders); 1385 } else { 1386 int error; 1387 1388 announce_buf[0] = '\0'; 1389 1390 /* 1391 * Retry any UNIT ATTENTION type errors. They 1392 * are expected at boot. 1393 */ 1394 error = daerror(done_ccb, CAM_RETRY_SELTO, 1395 SF_RETRY_UA|SF_NO_PRINT); 1396 if (error == ERESTART) { 1397 /* 1398 * A retry was scheuled, so 1399 * just return. 1400 */ 1401 return; 1402 } else if (error != 0) { 1403 struct scsi_sense_data *sense; 1404 int asc, ascq; 1405 int sense_key, error_code; 1406 int have_sense; 1407 cam_status status; 1408 struct ccb_getdev cgd; 1409 1410 /* Don't wedge this device's queue */ 1411 status = done_ccb->ccb_h.status; 1412 if ((status & CAM_DEV_QFRZN) != 0) 1413 cam_release_devq(done_ccb->ccb_h.path, 1414 /*relsim_flags*/0, 1415 /*reduction*/0, 1416 /*timeout*/0, 1417 /*getcount_only*/0); 1418 1419 1420 xpt_setup_ccb(&cgd.ccb_h, 1421 done_ccb->ccb_h.path, 1422 /* priority */ 1); 1423 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1424 xpt_action((union ccb *)&cgd); 1425 1426 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0) 1427 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0) 1428 || ((status & CAM_AUTOSNS_VALID) == 0)) 1429 have_sense = FALSE; 1430 else 1431 have_sense = TRUE; 1432 1433 if (have_sense) { 1434 sense = &csio->sense_data; 1435 scsi_extract_sense(sense, &error_code, 1436 &sense_key, 1437 &asc, &ascq); 1438 } 1439 /* 1440 * Attach to anything that claims to be a 1441 * direct access or optical disk device, 1442 * as long as it doesn't return a "Logical 1443 * unit not supported" (0x25) error. 1444 */ 1445 if ((have_sense) && (asc != 0x25) 1446 && (error_code == SSD_CURRENT_ERROR)) { 1447 const char *sense_key_desc; 1448 const char *asc_desc; 1449 1450 scsi_sense_desc(sense_key, asc, ascq, 1451 &cgd.inq_data, 1452 &sense_key_desc, 1453 &asc_desc); 1454 snprintf(announce_buf, 1455 sizeof(announce_buf), 1456 "Attempt to query device " 1457 "size failed: %s, %s", 1458 sense_key_desc, 1459 asc_desc); 1460 } else { 1461 if (have_sense) 1462 scsi_sense_print( 1463 &done_ccb->csio); 1464 else { 1465 xpt_print_path(periph->path); 1466 printf("got CAM status %#x\n", 1467 done_ccb->ccb_h.status); 1468 } 1469 1470 xpt_print_path(periph->path); 1471 printf("fatal error, failed" 1472 " to attach to device\n"); 1473 1474 /* 1475 * Free up resources. 1476 */ 1477 cam_periph_invalidate(periph); 1478 } 1479 } 1480 } 1481 free(rdcap, M_TEMP); 1482 if (announce_buf[0] != '\0') 1483 xpt_announce_periph(periph, announce_buf); 1484 softc->state = DA_STATE_NORMAL; 1485 /* 1486 * Since our peripheral may be invalidated by an error 1487 * above or an external event, we must release our CCB 1488 * before releasing the probe lock on the peripheral. 1489 * The peripheral will only go away once the last lock 1490 * is removed, and we need it around for the CCB release 1491 * operation. 1492 */ 1493 xpt_release_ccb(done_ccb); 1494 cam_periph_unlock(periph); 1495 return; 1496 } 1497 case DA_CCB_WAITING: 1498 { 1499 /* Caller will release the CCB */ 1500 wakeup(&done_ccb->ccb_h.cbfcnp); 1501 return; 1502 } 1503 case DA_CCB_DUMP: 1504 /* No-op. We're polling */ 1505 return; 1506 default: 1507 break; 1508 } 1509 xpt_release_ccb(done_ccb); 1510 } 1511 1512 static int 1513 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 1514 { 1515 struct da_softc *softc; 1516 struct cam_periph *periph; 1517 1518 periph = xpt_path_periph(ccb->ccb_h.path); 1519 softc = (struct da_softc *)periph->softc; 1520 1521 /* 1522 * XXX 1523 * Until we have a better way of doing pack validation, 1524 * don't treat UAs as errors. 1525 */ 1526 sense_flags |= SF_RETRY_UA; 1527 return(cam_periph_error(ccb, cam_flags, sense_flags, 1528 &softc->saved_ccb)); 1529 } 1530 1531 static void 1532 daprevent(struct cam_periph *periph, int action) 1533 { 1534 struct da_softc *softc; 1535 union ccb *ccb; 1536 int error; 1537 1538 softc = (struct da_softc *)periph->softc; 1539 1540 if (((action == PR_ALLOW) 1541 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0) 1542 || ((action == PR_PREVENT) 1543 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) { 1544 return; 1545 } 1546 1547 ccb = cam_periph_getccb(periph, /*priority*/1); 1548 1549 scsi_prevent(&ccb->csio, 1550 /*retries*/1, 1551 /*cbcfp*/dadone, 1552 MSG_SIMPLE_Q_TAG, 1553 action, 1554 SSD_FULL_SIZE, 1555 5000); 1556 1557 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO, 1558 SF_RETRY_UA, &softc->device_stats); 1559 1560 if (error == 0) { 1561 if (action == PR_ALLOW) 1562 softc->flags &= ~DA_FLAG_PACK_LOCKED; 1563 else 1564 softc->flags |= DA_FLAG_PACK_LOCKED; 1565 } 1566 1567 xpt_release_ccb(ccb); 1568 } 1569 1570 static void 1571 dasetgeom(struct cam_periph *periph, struct scsi_read_capacity_data * rdcap) 1572 { 1573 struct ccb_calc_geometry ccg; 1574 struct da_softc *softc; 1575 struct disk_params *dp; 1576 1577 softc = (struct da_softc *)periph->softc; 1578 1579 dp = &softc->params; 1580 dp->secsize = scsi_4btoul(rdcap->length); 1581 dp->sectors = scsi_4btoul(rdcap->addr) + 1; 1582 /* 1583 * Have the controller provide us with a geometry 1584 * for this disk. The only time the geometry 1585 * matters is when we boot and the controller 1586 * is the only one knowledgeable enough to come 1587 * up with something that will make this a bootable 1588 * device. 1589 */ 1590 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1); 1591 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY; 1592 ccg.block_size = dp->secsize; 1593 ccg.volume_size = dp->sectors; 1594 ccg.heads = 0; 1595 ccg.secs_per_track = 0; 1596 ccg.cylinders = 0; 1597 xpt_action((union ccb*)&ccg); 1598 dp->heads = ccg.heads; 1599 dp->secs_per_track = ccg.secs_per_track; 1600 dp->cylinders = ccg.cylinders; 1601 } 1602 1603 static void 1604 dasendorderedtag(void *arg) 1605 { 1606 struct da_softc *softc; 1607 int s; 1608 1609 for (softc = SLIST_FIRST(&softc_list); 1610 softc != NULL; 1611 softc = SLIST_NEXT(softc, links)) { 1612 s = splsoftcam(); 1613 if ((softc->ordered_tag_count == 0) 1614 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) { 1615 softc->flags |= DA_FLAG_NEED_OTAG; 1616 } 1617 if (softc->device_stats.busy_count > 0) 1618 softc->flags &= ~DA_FLAG_WENT_IDLE; 1619 1620 softc->ordered_tag_count = 0; 1621 splx(s); 1622 } 1623 /* Queue us up again */ 1624 timeout(dasendorderedtag, NULL, 1625 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL); 1626 } 1627 1628 /* 1629 * Step through all DA peripheral drivers, and if the device is still open, 1630 * sync the disk cache to physical media. 1631 */ 1632 static void 1633 dashutdown(void * arg, int howto) 1634 { 1635 struct cam_periph *periph; 1636 struct da_softc *softc; 1637 1638 TAILQ_FOREACH(periph, &dadriver.units, unit_links) { 1639 union ccb ccb; 1640 softc = (struct da_softc *)periph->softc; 1641 1642 /* 1643 * We only sync the cache if the drive is still open, and 1644 * if the drive is capable of it.. 1645 */ 1646 if (((softc->flags & DA_FLAG_OPEN) == 0) 1647 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) 1648 continue; 1649 1650 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1); 1651 1652 ccb.ccb_h.ccb_state = DA_CCB_DUMP; 1653 scsi_synchronize_cache(&ccb.csio, 1654 /*retries*/1, 1655 /*cbfcnp*/dadone, 1656 MSG_SIMPLE_Q_TAG, 1657 /*begin_lba*/0, /* whole disk */ 1658 /*lb_count*/0, 1659 SSD_FULL_SIZE, 1660 5 * 60 * 1000); 1661 1662 xpt_polled_action(&ccb); 1663 1664 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1665 if (((ccb.ccb_h.status & CAM_STATUS_MASK) == 1666 CAM_SCSI_STATUS_ERROR) 1667 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){ 1668 int error_code, sense_key, asc, ascq; 1669 1670 scsi_extract_sense(&ccb.csio.sense_data, 1671 &error_code, &sense_key, 1672 &asc, &ascq); 1673 1674 if (sense_key != SSD_KEY_ILLEGAL_REQUEST) 1675 scsi_sense_print(&ccb.csio); 1676 } else { 1677 xpt_print_path(periph->path); 1678 printf("Synchronize cache failed, status " 1679 "== 0x%x, scsi status == 0x%x\n", 1680 ccb.ccb_h.status, ccb.csio.scsi_status); 1681 } 1682 } 1683 1684 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 1685 cam_release_devq(ccb.ccb_h.path, 1686 /*relsim_flags*/0, 1687 /*reduction*/0, 1688 /*timeout*/0, 1689 /*getcount_only*/0); 1690 1691 } 1692 } 1693 1694 #else /* !_KERNEL */ 1695 1696 /* 1697 * XXX This is only left out of the kernel build to silence warnings. If, 1698 * for some reason this function is used in the kernel, the ifdefs should 1699 * be moved so it is included both in the kernel and userland. 1700 */ 1701 void 1702 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries, 1703 void (*cbfcnp)(struct cam_periph *, union ccb *), 1704 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave, 1705 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, 1706 u_int32_t timeout) 1707 { 1708 struct scsi_format_unit *scsi_cmd; 1709 1710 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes; 1711 scsi_cmd->opcode = FORMAT_UNIT; 1712 scsi_cmd->byte2 = byte2; 1713 scsi_ulto2b(ileave, scsi_cmd->interleave); 1714 1715 cam_fill_csio(csio, 1716 retries, 1717 cbfcnp, 1718 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE, 1719 tag_action, 1720 data_ptr, 1721 dxfer_len, 1722 sense_len, 1723 sizeof(*scsi_cmd), 1724 timeout); 1725 } 1726 1727 #endif /* _KERNEL */ 1728