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 struct sysctl_ctx_list sysctl_ctx; 131 struct sysctl_oid *sysctl_tree; 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 * Logitec USB/Firewire LHD-P30FU 146 */ 147 { 148 /* USB part */ 149 {T_DIRECT, SIP_MEDIA_FIXED, "HITACHI_", "DK23DA*", "*"}, 150 /*quirks*/ DA_Q_NO_6_BYTE 151 }, 152 { 153 /* Firewire part */ 154 {T_DIRECT, SIP_MEDIA_FIXED, "LSILogic", "SYM13FW*", "*"}, 155 /*quirks*/ DA_Q_NO_6_BYTE 156 }, 157 { 158 /* 159 * Fujitsu M2513A MO drives. 160 * Tested devices: M2513A2 firmware versions 1200 & 1300. 161 * (dip switch selects whether T_DIRECT or T_OPTICAL device) 162 * Reported by: W.Scholten <whs@xs4all.nl> 163 */ 164 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"}, 165 /*quirks*/ DA_Q_NO_SYNC_CACHE 166 }, 167 { 168 /* See above. */ 169 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"}, 170 /*quirks*/ DA_Q_NO_SYNC_CACHE 171 }, 172 { 173 /* 174 * This particular Fujitsu drive doesn't like the 175 * synchronize cache command. 176 * Reported by: Tom Jackson <toj@gorilla.net> 177 */ 178 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"}, 179 /*quirks*/ DA_Q_NO_SYNC_CACHE 180 181 }, 182 { 183 /* 184 * This drive doesn't like the synchronize cache command 185 * either. Reported by: Matthew Jacob <mjacob@feral.com> 186 * in NetBSD PR kern/6027, August 24, 1998. 187 */ 188 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"}, 189 /*quirks*/ DA_Q_NO_SYNC_CACHE 190 }, 191 { 192 /* 193 * This drive doesn't like the synchronize cache command 194 * either. Reported by: Hellmuth Michaelis (hm@kts.org) 195 * (PR 8882). 196 */ 197 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"}, 198 /*quirks*/ DA_Q_NO_SYNC_CACHE 199 }, 200 { 201 /* 202 * Doesn't like the synchronize cache command. 203 * Reported by: Blaz Zupan <blaz@gold.amis.net> 204 */ 205 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"}, 206 /*quirks*/ DA_Q_NO_SYNC_CACHE 207 }, 208 { 209 /* 210 * Doesn't like the synchronize cache command. 211 */ 212 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"}, 213 /*quirks*/ DA_Q_NO_SYNC_CACHE 214 }, 215 { 216 /* 217 * Doesn't like the synchronize cache command. 218 */ 219 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"}, 220 /*quirks*/ DA_Q_NO_SYNC_CACHE 221 }, 222 { 223 /* 224 * Doesn't work correctly with 6 byte reads/writes. 225 * Returns illegal request, and points to byte 9 of the 226 * 6-byte CDB. 227 * Reported by: Adam McDougall <bsdx@spawnet.com> 228 */ 229 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"}, 230 /*quirks*/ DA_Q_NO_6_BYTE 231 }, 232 { 233 /* 234 * See above. 235 */ 236 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"}, 237 /*quirks*/ DA_Q_NO_6_BYTE 238 }, 239 240 /* Below a list of quirks for USB devices supported by umass. */ 241 { 242 /* 243 * This USB floppy drive uses the UFI command set. This 244 * command set is a derivative of the ATAPI command set and 245 * does not support READ_6 commands only READ_10. It also does 246 * not support sync cache (0x35). 247 */ 248 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Y-E DATA", "USB-FDU", "*"}, 249 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 250 }, 251 { 252 /* Another USB floppy */ 253 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MATSHITA", "FDD CF-VFDU*","*"}, 254 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 255 }, 256 { 257 /* 258 * Sony Memory Stick adapter MSAC-US1 and 259 * Sony PCG-C1VJ Internal Memory Stick Slot (MSC-U01). 260 * Make all sony MS* products use this quirk. 261 */ 262 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "MS*", "*"}, 263 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 264 }, 265 { 266 /* 267 * Sony Memory Stick adapter for the CLIE series 268 * of PalmOS PDA's 269 */ 270 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "CLIE*", "*"}, 271 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 272 }, 273 { 274 /* 275 * Sony DSC cameras (DSC-S30, DSC-S50, DSC-S70) 276 */ 277 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"}, 278 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 279 }, 280 { 281 /* 282 * Maxtor 3000LE USB Drive 283 */ 284 {T_DIRECT, SIP_MEDIA_FIXED, "MAXTOR*", "K040H2*", "*"}, 285 /*quirks*/ DA_Q_NO_6_BYTE 286 }, 287 { 288 /* 289 * LaCie USB drive, among others 290 */ 291 {T_DIRECT, SIP_MEDIA_FIXED, "Maxtor*", "D080H4*", "*"}, 292 /*quirks*/ DA_Q_NO_6_BYTE 293 }, 294 { 295 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "MCF3064AP", "*"}, 296 /*quirks*/ DA_Q_NO_6_BYTE 297 }, 298 { 299 /* 300 * Microtech USB CameraMate 301 */ 302 {T_DIRECT, SIP_MEDIA_REMOVABLE, "eUSB Compact*", 303 "Compact Flash*", "*"}, 304 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 305 }, 306 { 307 /* 308 * The vendor, product and version strings coming from the 309 * controller are null terminated instead of being padded with 310 * spaces. The trailing wildcard character '*' is required. 311 */ 312 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SMSC*", "USB FDC*","*"}, 313 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 314 }, 315 { 316 /* 317 * Olympus digital cameras (C-3040ZOOM, C-2040ZOOM, C-1) 318 */ 319 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "C-*", "*"}, 320 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 321 }, 322 { 323 /* 324 * Olympus digital cameras (D-370) 325 */ 326 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "D-*", "*"}, 327 /*quirks*/ DA_Q_NO_6_BYTE 328 }, 329 { 330 /* 331 * Olympus digital cameras (E-100RS, E-10). 332 */ 333 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "E-*", "*"}, 334 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 335 }, 336 { 337 /* 338 * KingByte Pen Drives 339 */ 340 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NO BRAND", "PEN DRIVE", "*"}, 341 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 342 }, 343 { 344 /* 345 * FujiFilm Camera 346 */ 347 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJIFILMUSB-DRIVEUNIT", 348 "USB-DRIVEUNIT", "*"}, 349 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 350 }, 351 { 352 /* 353 * Nikon Coolpix E775/E995 Cameras 354 */ 355 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NIKON", "NIKON DSC E*", "*"}, 356 /*quirks*/ DA_Q_NO_6_BYTE 357 }, 358 { 359 /* 360 * Nikon Coolpix E885 Camera 361 */ 362 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Nikon", "Digital Camera", "*"}, 363 /*quirks*/ DA_Q_NO_6_BYTE 364 }, 365 { 366 /* 367 * SimpleTech FlashLink UCF-100 368 */ 369 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OEI-USB", "CompactFlash", "*"}, 370 /*quirks*/ DA_Q_NO_6_BYTE 371 }, 372 { 373 /* 374 * Minolta Dimage 2330 375 */ 376 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MINOLTA", "DIMAGE 2330*", "*"}, 377 /*quirks*/ DA_Q_NO_6_BYTE 378 }, 379 { 380 /* 381 * Minolta Dimage E203 382 */ 383 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MINOLTA", "DiMAGE E203", "*"}, 384 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 385 }, 386 { 387 /* 388 * DIVA USB Mp3 Player. 389 * PR: kern/33638 390 */ 391 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DIVA USB", "Media Reader","*"}, 392 /*quirks*/ DA_Q_NO_6_BYTE 393 }, 394 { 395 /* 396 * Daisy Technology PhotoClip USB Camera 397 */ 398 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Digital", "World DMC","*"}, 399 /*quirks*/ DA_Q_NO_6_BYTE 400 }, 401 { 402 /* 403 * Apacer HandyDrive 404 * PR: kern/43627 405 */ 406 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Apacer", "HandyDrive", "*"}, 407 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 408 }, 409 { 410 /* 411 * Daisy Technology PhotoClip on Zoran chip 412 * PR: kern/43580 413 */ 414 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ZORAN", "COACH", "*"}, 415 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 416 }, 417 { 418 /* 419 * HP 315 Digital Camera 420 * PR: kern/41010 421 */ 422 {T_DIRECT, SIP_MEDIA_REMOVABLE, "HP", "USB CAMERA", "*"}, 423 /*quirks*/ DA_Q_NO_6_BYTE 424 }, 425 { 426 /* 427 * Fujitsu-Siemens Memorybird pen drive 428 * PR: kern/34712 429 */ 430 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Fujitsu", "Memorybird", "*"}, 431 /*quirks*/ DA_Q_NO_6_BYTE 432 }, 433 { 434 /* 435 * Sony USB Key-Storage 436 * PR: kern/46386 437 */ 438 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Storage Media", "*"}, 439 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE 440 }, 441 { 442 /* 443 * Lexar Media Jumpdrive 444 * PR: kern/47006 445 */ 446 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LEXAR", "DIGITAL FILM", "*"}, 447 /*quirks*/ DA_Q_NO_6_BYTE 448 } 449 }; 450 451 static disk_strategy_t dastrategy; 452 static dumper_t dadump; 453 static periph_init_t dainit; 454 static void daasync(void *callback_arg, u_int32_t code, 455 struct cam_path *path, void *arg); 456 static periph_ctor_t daregister; 457 static periph_dtor_t dacleanup; 458 static periph_start_t dastart; 459 static periph_oninv_t daoninvalidate; 460 static void dadone(struct cam_periph *periph, 461 union ccb *done_ccb); 462 static int daerror(union ccb *ccb, u_int32_t cam_flags, 463 u_int32_t sense_flags); 464 static void daprevent(struct cam_periph *periph, int action); 465 static void dasetgeom(struct cam_periph *periph, 466 struct scsi_read_capacity_data * rdcap); 467 static timeout_t dasendorderedtag; 468 static void dashutdown(void *arg, int howto); 469 470 #ifndef DA_DEFAULT_TIMEOUT 471 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */ 472 #endif 473 474 #ifndef DA_DEFAULT_RETRY 475 #define DA_DEFAULT_RETRY 4 476 #endif 477 478 static int da_retry_count = DA_DEFAULT_RETRY; 479 static int da_default_timeout = DA_DEFAULT_TIMEOUT; 480 481 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0, 482 "CAM Direct Access Disk driver"); 483 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW, 484 &da_retry_count, 0, "Normal I/O retry count"); 485 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count); 486 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW, 487 &da_default_timeout, 0, "Normal I/O timeout (in seconds)"); 488 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout); 489 490 /* 491 * DA_ORDEREDTAG_INTERVAL determines how often, relative 492 * to the default timeout, we check to see whether an ordered 493 * tagged transaction is appropriate to prevent simple tag 494 * starvation. Since we'd like to ensure that there is at least 495 * 1/2 of the timeout length left for a starved transaction to 496 * complete after we've sent an ordered tag, we must poll at least 497 * four times in every timeout period. This takes care of the worst 498 * case where a starved transaction starts during an interval that 499 * meets the requirement "don't send an ordered tag" test so it takes 500 * us two intervals to determine that a tag must be sent. 501 */ 502 #ifndef DA_ORDEREDTAG_INTERVAL 503 #define DA_ORDEREDTAG_INTERVAL 4 504 #endif 505 506 static struct periph_driver dadriver = 507 { 508 dainit, "da", 509 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0 510 }; 511 512 PERIPHDRIVER_DECLARE(da, dadriver); 513 514 static SLIST_HEAD(,da_softc) softc_list; 515 516 static int 517 daopen(struct disk *dp) 518 { 519 struct cam_periph *periph; 520 struct da_softc *softc; 521 struct scsi_read_capacity_data *rcap; 522 union ccb *ccb; 523 int unit; 524 int error; 525 int s; 526 527 s = splsoftcam(); 528 periph = (struct cam_periph *)dp->d_drv1; 529 unit = periph->unit_number; 530 if (periph == NULL) { 531 splx(s); 532 return (ENXIO); 533 } 534 535 softc = (struct da_softc *)periph->softc; 536 537 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 538 ("daopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit, 539 unit)); 540 541 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) 542 return (error); /* error code from tsleep */ 543 544 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 545 return(ENXIO); 546 softc->flags |= DA_FLAG_OPEN; 547 548 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) { 549 /* Invalidate our pack information. */ 550 softc->flags &= ~DA_FLAG_PACK_INVALID; 551 } 552 splx(s); 553 554 /* Do a read capacity */ 555 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap), 556 M_TEMP, 557 M_WAITOK); 558 559 ccb = cam_periph_getccb(periph, /*priority*/1); 560 scsi_read_capacity(&ccb->csio, 561 /*retries*/4, 562 /*cbfncp*/dadone, 563 MSG_SIMPLE_Q_TAG, 564 rcap, 565 SSD_FULL_SIZE, 566 /*timeout*/60000); 567 ccb->ccb_h.ccb_bp = NULL; 568 569 error = cam_periph_runccb(ccb, daerror, 570 /*cam_flags*/CAM_RETRY_SELTO, 571 /*sense_flags*/SF_RETRY_UA, 572 &softc->device_stats); 573 574 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 575 cam_release_devq(ccb->ccb_h.path, 576 /*relsim_flags*/0, 577 /*reduction*/0, 578 /*timeout*/0, 579 /*getcount_only*/0); 580 xpt_release_ccb(ccb); 581 582 if (error == 0) 583 dasetgeom(periph, rcap); 584 585 free(rcap, M_TEMP); 586 587 if (error == 0) { 588 589 softc->disk.d_sectorsize = softc->params.secsize; 590 softc->disk.d_mediasize = softc->params.secsize * (off_t)softc->params.sectors; 591 /* XXX: these are not actually "firmware" values, so they may be wrong */ 592 softc->disk.d_fwsectors = softc->params.secs_per_track; 593 softc->disk.d_fwheads = softc->params.heads; 594 595 /* 596 * Check to see whether or not the blocksize is set yet. 597 * If it isn't, set it and then clear the blocksize 598 * unavailable flag for the device statistics. 599 */ 600 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){ 601 softc->device_stats.block_size = softc->params.secsize; 602 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE; 603 } 604 } 605 606 if (error == 0) { 607 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) 608 daprevent(periph, PR_PREVENT); 609 } else { 610 softc->flags &= ~DA_FLAG_OPEN; 611 cam_periph_release(periph); 612 } 613 cam_periph_unlock(periph); 614 return (error); 615 } 616 617 static int 618 daclose(struct disk *dp) 619 { 620 struct cam_periph *periph; 621 struct da_softc *softc; 622 int error; 623 624 periph = (struct cam_periph *)dp->d_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_disk->d_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(struct disk *dp, u_long cmd, void *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 *)dp->d_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(void *arg, 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 struct disk *dp; 795 796 dp = arg; 797 periph = dp->d_drv1; 798 if (periph == NULL) 799 return (ENXIO); 800 softc = (struct da_softc *)periph->softc; 801 secsize = softc->params.secsize; 802 803 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) 804 return (ENXIO); 805 806 if (length > 0) { 807 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1); 808 csio.ccb_h.ccb_state = DA_CCB_DUMP; 809 scsi_read_write(&csio, 810 /*retries*/1, 811 dadone, 812 MSG_ORDERED_Q_TAG, 813 /*read*/FALSE, 814 /*byte2*/0, 815 /*minimum_cmd_size*/ softc->minimum_cmd_size, 816 offset / secsize, 817 length / secsize, 818 /*data_ptr*/(u_int8_t *) virtual, 819 /*dxfer_len*/length, 820 /*sense_len*/SSD_FULL_SIZE, 821 DA_DEFAULT_TIMEOUT * 1000); 822 xpt_polled_action((union ccb *)&csio); 823 824 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 825 printf("Aborting dump due to I/O error.\n"); 826 if ((csio.ccb_h.status & CAM_STATUS_MASK) == 827 CAM_SCSI_STATUS_ERROR) 828 scsi_sense_print(&csio); 829 else 830 printf("status == 0x%x, scsi status == 0x%x\n", 831 csio.ccb_h.status, csio.scsi_status); 832 return(EIO); 833 } 834 return(0); 835 } 836 837 /* 838 * Sync the disk cache contents to the physical media. 839 */ 840 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) { 841 842 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1); 843 csio.ccb_h.ccb_state = DA_CCB_DUMP; 844 scsi_synchronize_cache(&csio, 845 /*retries*/1, 846 /*cbfcnp*/dadone, 847 MSG_SIMPLE_Q_TAG, 848 /*begin_lba*/0,/* Cover the whole disk */ 849 /*lb_count*/0, 850 SSD_FULL_SIZE, 851 5 * 60 * 1000); 852 xpt_polled_action((union ccb *)&csio); 853 854 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 855 if ((csio.ccb_h.status & CAM_STATUS_MASK) == 856 CAM_SCSI_STATUS_ERROR) { 857 int asc, ascq; 858 int sense_key, error_code; 859 860 scsi_extract_sense(&csio.sense_data, 861 &error_code, 862 &sense_key, 863 &asc, &ascq); 864 if (sense_key != SSD_KEY_ILLEGAL_REQUEST) 865 scsi_sense_print(&csio); 866 } else { 867 xpt_print_path(periph->path); 868 printf("Synchronize cache failed, status " 869 "== 0x%x, scsi status == 0x%x\n", 870 csio.ccb_h.status, csio.scsi_status); 871 } 872 } 873 } 874 return (0); 875 } 876 877 static void 878 dainit(void) 879 { 880 cam_status status; 881 struct cam_path *path; 882 883 SLIST_INIT(&softc_list); 884 885 /* 886 * Install a global async callback. This callback will 887 * receive async callbacks like "new device found". 888 */ 889 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID, 890 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 891 892 if (status == CAM_REQ_CMP) { 893 struct ccb_setasync csa; 894 895 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5); 896 csa.ccb_h.func_code = XPT_SASYNC_CB; 897 csa.event_enable = AC_FOUND_DEVICE; 898 csa.callback = daasync; 899 csa.callback_arg = NULL; 900 xpt_action((union ccb *)&csa); 901 status = csa.ccb_h.status; 902 xpt_free_path(path); 903 } 904 905 if (status != CAM_REQ_CMP) { 906 printf("da: Failed to attach master async callback " 907 "due to status 0x%x!\n", status); 908 } else { 909 910 /* 911 * Schedule a periodic event to occasionally send an 912 * ordered tag to a device. 913 */ 914 timeout(dasendorderedtag, NULL, 915 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL); 916 917 /* Register our shutdown event handler */ 918 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown, 919 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 920 printf("dainit: shutdown event registration failed!\n"); 921 } 922 } 923 924 static void 925 daoninvalidate(struct cam_periph *periph) 926 { 927 int s; 928 struct da_softc *softc; 929 struct bio *q_bp; 930 struct ccb_setasync csa; 931 932 softc = (struct da_softc *)periph->softc; 933 934 /* 935 * De-register any async callbacks. 936 */ 937 xpt_setup_ccb(&csa.ccb_h, periph->path, 938 /* priority */ 5); 939 csa.ccb_h.func_code = XPT_SASYNC_CB; 940 csa.event_enable = 0; 941 csa.callback = daasync; 942 csa.callback_arg = periph; 943 xpt_action((union ccb *)&csa); 944 945 softc->flags |= DA_FLAG_PACK_INVALID; 946 947 /* 948 * Although the oninvalidate() routines are always called at 949 * splsoftcam, we need to be at splbio() here to keep the buffer 950 * queue from being modified while we traverse it. 951 */ 952 s = splbio(); 953 954 /* 955 * Return all queued I/O with ENXIO. 956 * XXX Handle any transactions queued to the card 957 * with XPT_ABORT_CCB. 958 */ 959 while ((q_bp = bioq_first(&softc->bio_queue)) != NULL){ 960 bioq_remove(&softc->bio_queue, q_bp); 961 q_bp->bio_resid = q_bp->bio_bcount; 962 biofinish(q_bp, NULL, ENXIO); 963 } 964 splx(s); 965 966 SLIST_REMOVE(&softc_list, softc, da_softc, links); 967 968 xpt_print_path(periph->path); 969 printf("lost device\n"); 970 } 971 972 static void 973 dacleanup(struct cam_periph *periph) 974 { 975 struct da_softc *softc; 976 977 softc = (struct da_softc *)periph->softc; 978 979 devstat_remove_entry(&softc->device_stats); 980 xpt_print_path(periph->path); 981 printf("removing device entry\n"); 982 disk_destroy(&softc->disk); 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 int 1051 dacmdsizesysctl(SYSCTL_HANDLER_ARGS) 1052 { 1053 int error, value; 1054 1055 value = *(int *)arg1; 1056 1057 error = sysctl_handle_int(oidp, &value, 0, req); 1058 1059 if ((error != 0) 1060 || (req->newptr == NULL)) 1061 return (error); 1062 1063 /* 1064 * Acceptable values here are 6, 10 or 12. It's possible we may 1065 * support a 16 byte minimum command size in the future, since 1066 * there are now READ(16) and WRITE(16) commands defined in the 1067 * SBC-2 spec. 1068 */ 1069 if (value < 6) 1070 value = 6; 1071 else if ((value > 6) 1072 && (value <= 10)) 1073 value = 10; 1074 else if (value > 10) 1075 value = 12; 1076 1077 *(int *)arg1 = value; 1078 1079 return (0); 1080 } 1081 1082 static cam_status 1083 daregister(struct cam_periph *periph, void *arg) 1084 { 1085 int s; 1086 struct da_softc *softc; 1087 struct ccb_setasync csa; 1088 struct ccb_getdev *cgd; 1089 char tmpstr[80], tmpstr2[80]; 1090 caddr_t match; 1091 1092 cgd = (struct ccb_getdev *)arg; 1093 if (periph == NULL) { 1094 printf("daregister: periph was NULL!!\n"); 1095 return(CAM_REQ_CMP_ERR); 1096 } 1097 1098 if (cgd == NULL) { 1099 printf("daregister: no getdev CCB, can't register device\n"); 1100 return(CAM_REQ_CMP_ERR); 1101 } 1102 1103 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF, 1104 M_NOWAIT|M_ZERO); 1105 1106 if (softc == NULL) { 1107 printf("daregister: Unable to probe new device. " 1108 "Unable to allocate softc\n"); 1109 return(CAM_REQ_CMP_ERR); 1110 } 1111 1112 LIST_INIT(&softc->pending_ccbs); 1113 softc->state = DA_STATE_PROBE; 1114 bioq_init(&softc->bio_queue); 1115 if (SID_IS_REMOVABLE(&cgd->inq_data)) 1116 softc->flags |= DA_FLAG_PACK_REMOVABLE; 1117 if ((cgd->inq_data.flags & SID_CmdQue) != 0) 1118 softc->flags |= DA_FLAG_TAGGED_QUEUING; 1119 1120 periph->softc = softc; 1121 1122 /* 1123 * See if this device has any quirks. 1124 */ 1125 match = cam_quirkmatch((caddr_t)&cgd->inq_data, 1126 (caddr_t)da_quirk_table, 1127 sizeof(da_quirk_table)/sizeof(*da_quirk_table), 1128 sizeof(*da_quirk_table), scsi_inquiry_match); 1129 1130 if (match != NULL) 1131 softc->quirks = ((struct da_quirk_entry *)match)->quirks; 1132 else 1133 softc->quirks = DA_Q_NONE; 1134 1135 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number); 1136 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 1137 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1138 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2, 1139 CTLFLAG_RD, 0, tmpstr); 1140 if (softc->sysctl_tree == NULL) { 1141 printf("daregister: unable to allocate sysctl tree\n"); 1142 free(softc, M_DEVBUF); 1143 return (CAM_REQ_CMP_ERR); 1144 } 1145 1146 /* 1147 * RBC devices don't have to support READ(6), only READ(10). 1148 */ 1149 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC) 1150 softc->minimum_cmd_size = 10; 1151 else 1152 softc->minimum_cmd_size = 6; 1153 1154 /* 1155 * Load the user's default, if any. 1156 */ 1157 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size", 1158 periph->unit_number); 1159 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size); 1160 1161 /* 1162 * 6, 10 and 12 are the currently permissible values. 1163 */ 1164 if (softc->minimum_cmd_size < 6) 1165 softc->minimum_cmd_size = 6; 1166 else if ((softc->minimum_cmd_size > 6) 1167 && (softc->minimum_cmd_size <= 10)) 1168 softc->minimum_cmd_size = 10; 1169 else if (softc->minimum_cmd_size > 12) 1170 softc->minimum_cmd_size = 12; 1171 1172 /* 1173 * Now register the sysctl handler, so the user can the value on 1174 * the fly. 1175 */ 1176 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1177 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW, 1178 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I", 1179 "Minimum CDB size"); 1180 1181 /* 1182 * Block our timeout handler while we 1183 * add this softc to the dev list. 1184 */ 1185 s = splsoftclock(); 1186 SLIST_INSERT_HEAD(&softc_list, softc, links); 1187 splx(s); 1188 1189 /* 1190 * The DA driver supports a blocksize, but 1191 * we don't know the blocksize until we do 1192 * a read capacity. So, set a flag to 1193 * indicate that the blocksize is 1194 * unavailable right now. We'll clear the 1195 * flag as soon as we've done a read capacity. 1196 */ 1197 devstat_add_entry(&softc->device_stats, "da", 1198 periph->unit_number, 0, 1199 DEVSTAT_BS_UNAVAILABLE, 1200 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI, 1201 DEVSTAT_PRIORITY_DISK); 1202 1203 /* 1204 * Register this media as a disk 1205 */ 1206 1207 softc->disk.d_open = daopen; 1208 softc->disk.d_close = daclose; 1209 softc->disk.d_strategy = dastrategy; 1210 softc->disk.d_ioctl = daioctl; 1211 softc->disk.d_dump = dadump; 1212 softc->disk.d_name = "da"; 1213 softc->disk.d_drv1 = periph; 1214 softc->disk.d_maxsize = DFLTPHYS; /* XXX: probably not arbitrary */ 1215 disk_create(periph->unit_number, &softc->disk, 0, NULL, NULL); 1216 1217 /* 1218 * Add async callbacks for bus reset and 1219 * bus device reset calls. I don't bother 1220 * checking if this fails as, in most cases, 1221 * the system will function just fine without 1222 * them and the only alternative would be to 1223 * not attach the device on failure. 1224 */ 1225 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5); 1226 csa.ccb_h.func_code = XPT_SASYNC_CB; 1227 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE; 1228 csa.callback = daasync; 1229 csa.callback_arg = periph; 1230 xpt_action((union ccb *)&csa); 1231 /* 1232 * Lock this peripheral until we are setup. 1233 * This first call can't block 1234 */ 1235 (void)cam_periph_lock(periph, PRIBIO); 1236 xpt_schedule(periph, /*priority*/5); 1237 1238 return(CAM_REQ_CMP); 1239 } 1240 1241 static void 1242 dastart(struct cam_periph *periph, union ccb *start_ccb) 1243 { 1244 struct da_softc *softc; 1245 1246 softc = (struct da_softc *)periph->softc; 1247 1248 1249 switch (softc->state) { 1250 case DA_STATE_NORMAL: 1251 { 1252 /* Pull a buffer from the queue and get going on it */ 1253 struct bio *bp; 1254 int s; 1255 1256 /* 1257 * See if there is a buf with work for us to do.. 1258 */ 1259 s = splbio(); 1260 bp = bioq_first(&softc->bio_queue); 1261 if (periph->immediate_priority <= periph->pinfo.priority) { 1262 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE, 1263 ("queuing for immediate ccb\n")); 1264 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING; 1265 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 1266 periph_links.sle); 1267 periph->immediate_priority = CAM_PRIORITY_NONE; 1268 splx(s); 1269 wakeup(&periph->ccb_list); 1270 } else if (bp == NULL) { 1271 splx(s); 1272 xpt_release_ccb(start_ccb); 1273 } else { 1274 int oldspl; 1275 u_int8_t tag_code; 1276 1277 bioq_remove(&softc->bio_queue, bp); 1278 1279 devstat_start_transaction(&softc->device_stats); 1280 1281 if ((softc->flags & DA_FLAG_NEED_OTAG) != 0) { 1282 softc->flags &= ~DA_FLAG_NEED_OTAG; 1283 softc->ordered_tag_count++; 1284 tag_code = MSG_ORDERED_Q_TAG; 1285 } else { 1286 tag_code = MSG_SIMPLE_Q_TAG; 1287 } 1288 scsi_read_write(&start_ccb->csio, 1289 /*retries*/da_retry_count, 1290 dadone, 1291 tag_code, 1292 bp->bio_cmd == BIO_READ, 1293 /*byte2*/0, 1294 softc->minimum_cmd_size, 1295 bp->bio_pblkno, 1296 bp->bio_bcount / softc->params.secsize, 1297 bp->bio_data, 1298 bp->bio_bcount, 1299 /*sense_len*/SSD_FULL_SIZE, 1300 da_default_timeout * 1000); 1301 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO; 1302 1303 /* 1304 * Block out any asyncronous callbacks 1305 * while we touch the pending ccb list. 1306 */ 1307 oldspl = splcam(); 1308 LIST_INSERT_HEAD(&softc->pending_ccbs, 1309 &start_ccb->ccb_h, periph_links.le); 1310 splx(oldspl); 1311 1312 /* We expect a unit attention from this device */ 1313 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) { 1314 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA; 1315 softc->flags &= ~DA_FLAG_RETRY_UA; 1316 } 1317 1318 start_ccb->ccb_h.ccb_bp = bp; 1319 bp = bioq_first(&softc->bio_queue); 1320 splx(s); 1321 1322 xpt_action(start_ccb); 1323 } 1324 1325 if (bp != NULL) { 1326 /* Have more work to do, so ensure we stay scheduled */ 1327 xpt_schedule(periph, /* XXX priority */1); 1328 } 1329 break; 1330 } 1331 case DA_STATE_PROBE: 1332 { 1333 struct ccb_scsiio *csio; 1334 struct scsi_read_capacity_data *rcap; 1335 1336 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap), 1337 M_TEMP, 1338 M_NOWAIT); 1339 if (rcap == NULL) { 1340 printf("dastart: Couldn't malloc read_capacity data\n"); 1341 /* da_free_periph??? */ 1342 break; 1343 } 1344 csio = &start_ccb->csio; 1345 scsi_read_capacity(csio, 1346 /*retries*/4, 1347 dadone, 1348 MSG_SIMPLE_Q_TAG, 1349 rcap, 1350 SSD_FULL_SIZE, 1351 /*timeout*/5000); 1352 start_ccb->ccb_h.ccb_bp = NULL; 1353 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE; 1354 xpt_action(start_ccb); 1355 break; 1356 } 1357 } 1358 } 1359 1360 static int 1361 cmd6workaround(union ccb *ccb) 1362 { 1363 struct scsi_rw_6 cmd6; 1364 struct scsi_rw_10 *cmd10; 1365 struct da_softc *softc; 1366 u_int8_t *cdb; 1367 int frozen; 1368 1369 cdb = ccb->csio.cdb_io.cdb_bytes; 1370 1371 /* Translation only possible if CDB is an array and cmd is R/W6 */ 1372 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 || 1373 (*cdb != READ_6 && *cdb != WRITE_6)) 1374 return 0; 1375 1376 xpt_print_path(ccb->ccb_h.path); 1377 printf("READ(6)/WRITE(6) not supported, " 1378 "increasing minimum_cmd_size to 10.\n"); 1379 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc; 1380 softc->minimum_cmd_size = 10; 1381 1382 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6)); 1383 cmd10 = (struct scsi_rw_10 *)cdb; 1384 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10; 1385 cmd10->byte2 = 0; 1386 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr); 1387 cmd10->reserved = 0; 1388 scsi_ulto2b(cmd6.length, cmd10->length); 1389 cmd10->control = cmd6.control; 1390 ccb->csio.cdb_len = sizeof(*cmd10); 1391 1392 /* Requeue request, unfreezing queue if necessary */ 1393 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 1394 ccb->ccb_h.status = CAM_REQUEUE_REQ; 1395 xpt_action(ccb); 1396 if (frozen) { 1397 cam_release_devq(ccb->ccb_h.path, 1398 /*relsim_flags*/0, 1399 /*reduction*/0, 1400 /*timeout*/0, 1401 /*getcount_only*/0); 1402 } 1403 return (ERESTART); 1404 } 1405 1406 static void 1407 dadone(struct cam_periph *periph, union ccb *done_ccb) 1408 { 1409 struct da_softc *softc; 1410 struct ccb_scsiio *csio; 1411 1412 softc = (struct da_softc *)periph->softc; 1413 csio = &done_ccb->csio; 1414 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) { 1415 case DA_CCB_BUFFER_IO: 1416 { 1417 struct bio *bp; 1418 int oldspl; 1419 1420 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 1421 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1422 int error; 1423 int s; 1424 int sf; 1425 1426 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0) 1427 sf = SF_RETRY_UA; 1428 else 1429 sf = 0; 1430 1431 error = daerror(done_ccb, CAM_RETRY_SELTO, sf); 1432 if (error == ERESTART) { 1433 /* 1434 * A retry was scheuled, so 1435 * just return. 1436 */ 1437 return; 1438 } 1439 if (error != 0) { 1440 struct bio *q_bp; 1441 1442 s = splbio(); 1443 1444 if (error == ENXIO) { 1445 /* 1446 * Catastrophic error. Mark our pack as 1447 * invalid. 1448 */ 1449 /* XXX See if this is really a media 1450 * change first. 1451 */ 1452 xpt_print_path(periph->path); 1453 printf("Invalidating pack\n"); 1454 softc->flags |= DA_FLAG_PACK_INVALID; 1455 } 1456 1457 /* 1458 * return all queued I/O with EIO, so that 1459 * the client can retry these I/Os in the 1460 * proper order should it attempt to recover. 1461 */ 1462 while ((q_bp = bioq_first(&softc->bio_queue)) 1463 != NULL) { 1464 bioq_remove(&softc->bio_queue, q_bp); 1465 q_bp->bio_resid = q_bp->bio_bcount; 1466 biofinish(q_bp, NULL, EIO); 1467 } 1468 splx(s); 1469 bp->bio_error = error; 1470 bp->bio_resid = bp->bio_bcount; 1471 bp->bio_flags |= BIO_ERROR; 1472 } else { 1473 bp->bio_resid = csio->resid; 1474 bp->bio_error = 0; 1475 if (bp->bio_resid != 0) { 1476 /* Short transfer ??? */ 1477 #if 0 1478 if (cmd6workaround(done_ccb) 1479 == ERESTART) 1480 return; 1481 #endif 1482 bp->bio_flags |= BIO_ERROR; 1483 } 1484 } 1485 if ((done_ccb->ccb_h.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 } else { 1492 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1493 panic("REQ_CMP with QFRZN"); 1494 bp->bio_resid = csio->resid; 1495 if (csio->resid > 0) { 1496 /* Short transfer ??? */ 1497 #if 0 /* XXX most of the broken umass devices need this ad-hoc work around */ 1498 if (cmd6workaround(done_ccb) == ERESTART) 1499 return; 1500 #endif 1501 bp->bio_flags |= BIO_ERROR; 1502 } 1503 } 1504 1505 /* 1506 * Block out any asyncronous callbacks 1507 * while we touch the pending ccb list. 1508 */ 1509 oldspl = splcam(); 1510 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 1511 splx(oldspl); 1512 1513 if (softc->device_stats.busy_count == 0) 1514 softc->flags |= DA_FLAG_WENT_IDLE; 1515 1516 biofinish(bp, &softc->device_stats, 0); 1517 break; 1518 } 1519 case DA_CCB_PROBE: 1520 { 1521 struct scsi_read_capacity_data *rdcap; 1522 char announce_buf[80]; 1523 1524 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr; 1525 1526 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1527 struct disk_params *dp; 1528 1529 dasetgeom(periph, rdcap); 1530 dp = &softc->params; 1531 snprintf(announce_buf, sizeof(announce_buf), 1532 "%luMB (%u %u byte sectors: %dH %dS/T %dC)", 1533 (unsigned long) (((u_int64_t)dp->secsize * 1534 dp->sectors) / (1024*1024)), dp->sectors, 1535 dp->secsize, dp->heads, dp->secs_per_track, 1536 dp->cylinders); 1537 } else { 1538 int error; 1539 1540 announce_buf[0] = '\0'; 1541 1542 /* 1543 * Retry any UNIT ATTENTION type errors. They 1544 * are expected at boot. 1545 */ 1546 error = daerror(done_ccb, CAM_RETRY_SELTO, 1547 SF_RETRY_UA|SF_NO_PRINT); 1548 if (error == ERESTART) { 1549 /* 1550 * A retry was scheuled, so 1551 * just return. 1552 */ 1553 return; 1554 } else if (error != 0) { 1555 struct scsi_sense_data *sense; 1556 int asc, ascq; 1557 int sense_key, error_code; 1558 int have_sense; 1559 cam_status status; 1560 struct ccb_getdev cgd; 1561 1562 /* Don't wedge this device's queue */ 1563 status = done_ccb->ccb_h.status; 1564 if ((status & CAM_DEV_QFRZN) != 0) 1565 cam_release_devq(done_ccb->ccb_h.path, 1566 /*relsim_flags*/0, 1567 /*reduction*/0, 1568 /*timeout*/0, 1569 /*getcount_only*/0); 1570 1571 1572 xpt_setup_ccb(&cgd.ccb_h, 1573 done_ccb->ccb_h.path, 1574 /* priority */ 1); 1575 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1576 xpt_action((union ccb *)&cgd); 1577 1578 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0) 1579 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0) 1580 || ((status & CAM_AUTOSNS_VALID) == 0)) 1581 have_sense = FALSE; 1582 else 1583 have_sense = TRUE; 1584 1585 if (have_sense) { 1586 sense = &csio->sense_data; 1587 scsi_extract_sense(sense, &error_code, 1588 &sense_key, 1589 &asc, &ascq); 1590 } 1591 /* 1592 * Attach to anything that claims to be a 1593 * direct access or optical disk device, 1594 * as long as it doesn't return a "Logical 1595 * unit not supported" (0x25) error. 1596 */ 1597 if ((have_sense) && (asc != 0x25) 1598 && (error_code == SSD_CURRENT_ERROR)) { 1599 const char *sense_key_desc; 1600 const char *asc_desc; 1601 1602 scsi_sense_desc(sense_key, asc, ascq, 1603 &cgd.inq_data, 1604 &sense_key_desc, 1605 &asc_desc); 1606 snprintf(announce_buf, 1607 sizeof(announce_buf), 1608 "Attempt to query device " 1609 "size failed: %s, %s", 1610 sense_key_desc, 1611 asc_desc); 1612 } else { 1613 if (have_sense) 1614 scsi_sense_print( 1615 &done_ccb->csio); 1616 else { 1617 xpt_print_path(periph->path); 1618 printf("got CAM status %#x\n", 1619 done_ccb->ccb_h.status); 1620 } 1621 1622 xpt_print_path(periph->path); 1623 printf("fatal error, failed" 1624 " to attach to device\n"); 1625 1626 /* 1627 * Free up resources. 1628 */ 1629 cam_periph_invalidate(periph); 1630 } 1631 } 1632 } 1633 free(rdcap, M_TEMP); 1634 if (announce_buf[0] != '\0') 1635 xpt_announce_periph(periph, announce_buf); 1636 softc->state = DA_STATE_NORMAL; 1637 /* 1638 * Since our peripheral may be invalidated by an error 1639 * above or an external event, we must release our CCB 1640 * before releasing the probe lock on the peripheral. 1641 * The peripheral will only go away once the last lock 1642 * is removed, and we need it around for the CCB release 1643 * operation. 1644 */ 1645 xpt_release_ccb(done_ccb); 1646 cam_periph_unlock(periph); 1647 return; 1648 } 1649 case DA_CCB_WAITING: 1650 { 1651 /* Caller will release the CCB */ 1652 wakeup(&done_ccb->ccb_h.cbfcnp); 1653 return; 1654 } 1655 case DA_CCB_DUMP: 1656 /* No-op. We're polling */ 1657 return; 1658 default: 1659 break; 1660 } 1661 xpt_release_ccb(done_ccb); 1662 } 1663 1664 static int 1665 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 1666 { 1667 struct da_softc *softc; 1668 struct cam_periph *periph; 1669 int error; 1670 1671 periph = xpt_path_periph(ccb->ccb_h.path); 1672 softc = (struct da_softc *)periph->softc; 1673 1674 /* 1675 * Automatically detect devices that do not support 1676 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs. 1677 */ 1678 error = 0; 1679 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) { 1680 error = cmd6workaround(ccb); 1681 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == 1682 CAM_SCSI_STATUS_ERROR) 1683 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) 1684 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) 1685 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0) 1686 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) { 1687 int sense_key, error_code, asc, ascq; 1688 1689 scsi_extract_sense(&ccb->csio.sense_data, 1690 &error_code, &sense_key, &asc, &ascq); 1691 if (sense_key == SSD_KEY_ILLEGAL_REQUEST) 1692 error = cmd6workaround(ccb); 1693 } 1694 if (error == ERESTART) 1695 return (ERESTART); 1696 1697 /* 1698 * XXX 1699 * Until we have a better way of doing pack validation, 1700 * don't treat UAs as errors. 1701 */ 1702 sense_flags |= SF_RETRY_UA; 1703 return(cam_periph_error(ccb, cam_flags, sense_flags, 1704 &softc->saved_ccb)); 1705 } 1706 1707 static void 1708 daprevent(struct cam_periph *periph, int action) 1709 { 1710 struct da_softc *softc; 1711 union ccb *ccb; 1712 int error; 1713 1714 softc = (struct da_softc *)periph->softc; 1715 1716 if (((action == PR_ALLOW) 1717 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0) 1718 || ((action == PR_PREVENT) 1719 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) { 1720 return; 1721 } 1722 1723 ccb = cam_periph_getccb(periph, /*priority*/1); 1724 1725 scsi_prevent(&ccb->csio, 1726 /*retries*/1, 1727 /*cbcfp*/dadone, 1728 MSG_SIMPLE_Q_TAG, 1729 action, 1730 SSD_FULL_SIZE, 1731 5000); 1732 1733 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO, 1734 SF_RETRY_UA, &softc->device_stats); 1735 1736 if (error == 0) { 1737 if (action == PR_ALLOW) 1738 softc->flags &= ~DA_FLAG_PACK_LOCKED; 1739 else 1740 softc->flags |= DA_FLAG_PACK_LOCKED; 1741 } 1742 1743 xpt_release_ccb(ccb); 1744 } 1745 1746 static void 1747 dasetgeom(struct cam_periph *periph, struct scsi_read_capacity_data * rdcap) 1748 { 1749 struct ccb_calc_geometry ccg; 1750 struct da_softc *softc; 1751 struct disk_params *dp; 1752 1753 softc = (struct da_softc *)periph->softc; 1754 1755 dp = &softc->params; 1756 dp->secsize = scsi_4btoul(rdcap->length); 1757 dp->sectors = scsi_4btoul(rdcap->addr) + 1; 1758 /* 1759 * Have the controller provide us with a geometry 1760 * for this disk. The only time the geometry 1761 * matters is when we boot and the controller 1762 * is the only one knowledgeable enough to come 1763 * up with something that will make this a bootable 1764 * device. 1765 */ 1766 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1); 1767 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY; 1768 ccg.block_size = dp->secsize; 1769 ccg.volume_size = dp->sectors; 1770 ccg.heads = 0; 1771 ccg.secs_per_track = 0; 1772 ccg.cylinders = 0; 1773 xpt_action((union ccb*)&ccg); 1774 dp->heads = ccg.heads; 1775 dp->secs_per_track = ccg.secs_per_track; 1776 dp->cylinders = ccg.cylinders; 1777 } 1778 1779 static void 1780 dasendorderedtag(void *arg) 1781 { 1782 struct da_softc *softc; 1783 int s; 1784 1785 for (softc = SLIST_FIRST(&softc_list); 1786 softc != NULL; 1787 softc = SLIST_NEXT(softc, links)) { 1788 s = splsoftcam(); 1789 if ((softc->ordered_tag_count == 0) 1790 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) { 1791 softc->flags |= DA_FLAG_NEED_OTAG; 1792 } 1793 if (softc->device_stats.busy_count > 0) 1794 softc->flags &= ~DA_FLAG_WENT_IDLE; 1795 1796 softc->ordered_tag_count = 0; 1797 splx(s); 1798 } 1799 /* Queue us up again */ 1800 timeout(dasendorderedtag, NULL, 1801 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL); 1802 } 1803 1804 /* 1805 * Step through all DA peripheral drivers, and if the device is still open, 1806 * sync the disk cache to physical media. 1807 */ 1808 static void 1809 dashutdown(void * arg, int howto) 1810 { 1811 struct cam_periph *periph; 1812 struct da_softc *softc; 1813 1814 TAILQ_FOREACH(periph, &dadriver.units, unit_links) { 1815 union ccb ccb; 1816 softc = (struct da_softc *)periph->softc; 1817 1818 /* 1819 * We only sync the cache if the drive is still open, and 1820 * if the drive is capable of it.. 1821 */ 1822 if (((softc->flags & DA_FLAG_OPEN) == 0) 1823 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) 1824 continue; 1825 1826 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1); 1827 1828 ccb.ccb_h.ccb_state = DA_CCB_DUMP; 1829 scsi_synchronize_cache(&ccb.csio, 1830 /*retries*/1, 1831 /*cbfcnp*/dadone, 1832 MSG_SIMPLE_Q_TAG, 1833 /*begin_lba*/0, /* whole disk */ 1834 /*lb_count*/0, 1835 SSD_FULL_SIZE, 1836 60 * 60 * 1000); 1837 1838 xpt_polled_action(&ccb); 1839 1840 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1841 if (((ccb.ccb_h.status & CAM_STATUS_MASK) == 1842 CAM_SCSI_STATUS_ERROR) 1843 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){ 1844 int error_code, sense_key, asc, ascq; 1845 1846 scsi_extract_sense(&ccb.csio.sense_data, 1847 &error_code, &sense_key, 1848 &asc, &ascq); 1849 1850 if (sense_key != SSD_KEY_ILLEGAL_REQUEST) 1851 scsi_sense_print(&ccb.csio); 1852 } else { 1853 xpt_print_path(periph->path); 1854 printf("Synchronize cache failed, status " 1855 "== 0x%x, scsi status == 0x%x\n", 1856 ccb.ccb_h.status, ccb.csio.scsi_status); 1857 } 1858 } 1859 1860 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 1861 cam_release_devq(ccb.ccb_h.path, 1862 /*relsim_flags*/0, 1863 /*reduction*/0, 1864 /*timeout*/0, 1865 /*getcount_only*/0); 1866 1867 } 1868 } 1869 1870 #else /* !_KERNEL */ 1871 1872 /* 1873 * XXX This is only left out of the kernel build to silence warnings. If, 1874 * for some reason this function is used in the kernel, the ifdefs should 1875 * be moved so it is included both in the kernel and userland. 1876 */ 1877 void 1878 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries, 1879 void (*cbfcnp)(struct cam_periph *, union ccb *), 1880 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave, 1881 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, 1882 u_int32_t timeout) 1883 { 1884 struct scsi_format_unit *scsi_cmd; 1885 1886 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes; 1887 scsi_cmd->opcode = FORMAT_UNIT; 1888 scsi_cmd->byte2 = byte2; 1889 scsi_ulto2b(ileave, scsi_cmd->interleave); 1890 1891 cam_fill_csio(csio, 1892 retries, 1893 cbfcnp, 1894 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE, 1895 tag_action, 1896 data_ptr, 1897 dxfer_len, 1898 sense_len, 1899 sizeof(*scsi_cmd), 1900 timeout); 1901 } 1902 1903 #endif /* _KERNEL */ 1904