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