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 SLIST_ENTRY(da_softc) links; 120 LIST_HEAD(, ccb_hdr) pending_ccbs; 121 da_state state; 122 da_flags flags; 123 da_quirks quirks; 124 int minimum_cmd_size; 125 int ordered_tag_count; 126 int outstanding_cmds; 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 * Pentax USB Optio 230 camera 452 * PR: kern/46369 453 */ 454 {T_DIRECT, SIP_MEDIA_REMOVABLE, 455 "PENTAX", "DIGITAL_CAMERA", "*"}, 456 /*quirks*/ DA_Q_NO_6_BYTE 457 }, 458 { 459 /* 460 * SanDisk ImageMate II compact flash 461 * PR: kern/47877 462 */ 463 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk", "ImageMate*", "*"}, 464 /*quirks*/ DA_Q_NO_6_BYTE 465 } 466 }; 467 468 static disk_strategy_t dastrategy; 469 static dumper_t dadump; 470 static periph_init_t dainit; 471 static void daasync(void *callback_arg, u_int32_t code, 472 struct cam_path *path, void *arg); 473 static periph_ctor_t daregister; 474 static periph_dtor_t dacleanup; 475 static periph_start_t dastart; 476 static periph_oninv_t daoninvalidate; 477 static void dadone(struct cam_periph *periph, 478 union ccb *done_ccb); 479 static int daerror(union ccb *ccb, u_int32_t cam_flags, 480 u_int32_t sense_flags); 481 static void daprevent(struct cam_periph *periph, int action); 482 static void dasetgeom(struct cam_periph *periph, 483 struct scsi_read_capacity_data * rdcap); 484 static timeout_t dasendorderedtag; 485 static void dashutdown(void *arg, int howto); 486 487 #ifndef DA_DEFAULT_TIMEOUT 488 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */ 489 #endif 490 491 #ifndef DA_DEFAULT_RETRY 492 #define DA_DEFAULT_RETRY 4 493 #endif 494 495 static int da_retry_count = DA_DEFAULT_RETRY; 496 static int da_default_timeout = DA_DEFAULT_TIMEOUT; 497 498 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0, 499 "CAM Direct Access Disk driver"); 500 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW, 501 &da_retry_count, 0, "Normal I/O retry count"); 502 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count); 503 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW, 504 &da_default_timeout, 0, "Normal I/O timeout (in seconds)"); 505 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout); 506 507 /* 508 * DA_ORDEREDTAG_INTERVAL determines how often, relative 509 * to the default timeout, we check to see whether an ordered 510 * tagged transaction is appropriate to prevent simple tag 511 * starvation. Since we'd like to ensure that there is at least 512 * 1/2 of the timeout length left for a starved transaction to 513 * complete after we've sent an ordered tag, we must poll at least 514 * four times in every timeout period. This takes care of the worst 515 * case where a starved transaction starts during an interval that 516 * meets the requirement "don't send an ordered tag" test so it takes 517 * us two intervals to determine that a tag must be sent. 518 */ 519 #ifndef DA_ORDEREDTAG_INTERVAL 520 #define DA_ORDEREDTAG_INTERVAL 4 521 #endif 522 523 static struct periph_driver dadriver = 524 { 525 dainit, "da", 526 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0 527 }; 528 529 PERIPHDRIVER_DECLARE(da, dadriver); 530 531 static SLIST_HEAD(,da_softc) softc_list; 532 533 static int 534 daopen(struct disk *dp) 535 { 536 struct cam_periph *periph; 537 struct da_softc *softc; 538 struct scsi_read_capacity_data *rcap; 539 union ccb *ccb; 540 int unit; 541 int error; 542 int s; 543 544 s = splsoftcam(); 545 periph = (struct cam_periph *)dp->d_drv1; 546 unit = periph->unit_number; 547 if (periph == NULL) { 548 splx(s); 549 return (ENXIO); 550 } 551 552 softc = (struct da_softc *)periph->softc; 553 554 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 555 ("daopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit, 556 unit)); 557 558 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) 559 return (error); /* error code from tsleep */ 560 561 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 562 return(ENXIO); 563 softc->flags |= DA_FLAG_OPEN; 564 565 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) { 566 /* Invalidate our pack information. */ 567 softc->flags &= ~DA_FLAG_PACK_INVALID; 568 } 569 splx(s); 570 571 /* Do a read capacity */ 572 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap), 573 M_TEMP, 574 M_WAITOK); 575 576 ccb = cam_periph_getccb(periph, /*priority*/1); 577 scsi_read_capacity(&ccb->csio, 578 /*retries*/4, 579 /*cbfncp*/dadone, 580 MSG_SIMPLE_Q_TAG, 581 rcap, 582 SSD_FULL_SIZE, 583 /*timeout*/60000); 584 ccb->ccb_h.ccb_bp = NULL; 585 586 error = cam_periph_runccb(ccb, daerror, 587 /*cam_flags*/CAM_RETRY_SELTO, 588 /*sense_flags*/SF_RETRY_UA, 589 softc->disk.d_devstat); 590 591 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 592 cam_release_devq(ccb->ccb_h.path, 593 /*relsim_flags*/0, 594 /*reduction*/0, 595 /*timeout*/0, 596 /*getcount_only*/0); 597 xpt_release_ccb(ccb); 598 599 if (error == 0) 600 dasetgeom(periph, rcap); 601 602 free(rcap, M_TEMP); 603 604 if (error == 0) { 605 606 softc->disk.d_sectorsize = softc->params.secsize; 607 softc->disk.d_mediasize = softc->params.secsize * (off_t)softc->params.sectors; 608 /* XXX: these are not actually "firmware" values, so they may be wrong */ 609 softc->disk.d_fwsectors = softc->params.secs_per_track; 610 softc->disk.d_fwheads = softc->params.heads; 611 softc->disk.d_devstat->block_size = softc->params.secsize; 612 softc->disk.d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE; 613 } 614 615 if (error == 0) { 616 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) 617 daprevent(periph, PR_PREVENT); 618 } else { 619 softc->flags &= ~DA_FLAG_OPEN; 620 cam_periph_release(periph); 621 } 622 cam_periph_unlock(periph); 623 return (error); 624 } 625 626 static int 627 daclose(struct disk *dp) 628 { 629 struct cam_periph *periph; 630 struct da_softc *softc; 631 int error; 632 633 periph = (struct cam_periph *)dp->d_drv1; 634 if (periph == NULL) 635 return (ENXIO); 636 637 softc = (struct da_softc *)periph->softc; 638 639 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) { 640 return (error); /* error code from tsleep */ 641 } 642 643 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) { 644 union ccb *ccb; 645 646 ccb = cam_periph_getccb(periph, /*priority*/1); 647 648 scsi_synchronize_cache(&ccb->csio, 649 /*retries*/1, 650 /*cbfcnp*/dadone, 651 MSG_SIMPLE_Q_TAG, 652 /*begin_lba*/0,/* Cover the whole disk */ 653 /*lb_count*/0, 654 SSD_FULL_SIZE, 655 5 * 60 * 1000); 656 657 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0, 658 /*sense_flags*/SF_RETRY_UA, 659 softc->disk.d_devstat); 660 661 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 662 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 663 CAM_SCSI_STATUS_ERROR) { 664 int asc, ascq; 665 int sense_key, error_code; 666 667 scsi_extract_sense(&ccb->csio.sense_data, 668 &error_code, 669 &sense_key, 670 &asc, &ascq); 671 if (sense_key != SSD_KEY_ILLEGAL_REQUEST) 672 scsi_sense_print(&ccb->csio); 673 } else { 674 xpt_print_path(periph->path); 675 printf("Synchronize cache failed, status " 676 "== 0x%x, scsi status == 0x%x\n", 677 ccb->csio.ccb_h.status, 678 ccb->csio.scsi_status); 679 } 680 } 681 682 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 683 cam_release_devq(ccb->ccb_h.path, 684 /*relsim_flags*/0, 685 /*reduction*/0, 686 /*timeout*/0, 687 /*getcount_only*/0); 688 689 xpt_release_ccb(ccb); 690 691 } 692 693 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) { 694 daprevent(periph, PR_ALLOW); 695 /* 696 * If we've got removeable media, mark the blocksize as 697 * unavailable, since it could change when new media is 698 * inserted. 699 */ 700 softc->disk.d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE; 701 } 702 703 softc->flags &= ~DA_FLAG_OPEN; 704 cam_periph_unlock(periph); 705 cam_periph_release(periph); 706 return (0); 707 } 708 709 /* 710 * Actually translate the requested transfer into one the physical driver 711 * can understand. The transfer is described by a buf and will include 712 * only one physical transfer. 713 */ 714 static void 715 dastrategy(struct bio *bp) 716 { 717 struct cam_periph *periph; 718 struct da_softc *softc; 719 int s; 720 721 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 722 if (periph == NULL) { 723 biofinish(bp, NULL, ENXIO); 724 return; 725 } 726 softc = (struct da_softc *)periph->softc; 727 #if 0 728 /* 729 * check it's not too big a transfer for our adapter 730 */ 731 scsi_minphys(bp,&sd_switch); 732 #endif 733 734 /* 735 * Mask interrupts so that the pack cannot be invalidated until 736 * after we are in the queue. Otherwise, we might not properly 737 * clean up one of the buffers. 738 */ 739 s = splbio(); 740 741 /* 742 * If the device has been made invalid, error out 743 */ 744 if ((softc->flags & DA_FLAG_PACK_INVALID)) { 745 splx(s); 746 biofinish(bp, NULL, ENXIO); 747 return; 748 } 749 750 /* 751 * Place it in the queue of disk activities for this disk 752 */ 753 bioqdisksort(&softc->bio_queue, bp); 754 755 splx(s); 756 757 /* 758 * Schedule ourselves for performing the work. 759 */ 760 xpt_schedule(periph, /* XXX priority */1); 761 762 return; 763 } 764 765 /* For 2.2-stable support */ 766 #ifndef ENOIOCTL 767 #define ENOIOCTL -1 768 #endif 769 770 static int 771 daioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td) 772 { 773 struct cam_periph *periph; 774 struct da_softc *softc; 775 int error; 776 777 periph = (struct cam_periph *)dp->d_drv1; 778 if (periph == NULL) 779 return (ENXIO); 780 781 softc = (struct da_softc *)periph->softc; 782 783 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("daioctl\n")); 784 785 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) { 786 return (error); /* error code from tsleep */ 787 } 788 789 error = cam_periph_ioctl(periph, cmd, addr, daerror); 790 791 cam_periph_unlock(periph); 792 793 return (error); 794 } 795 796 static int 797 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) 798 { 799 struct cam_periph *periph; 800 struct da_softc *softc; 801 u_int secsize; 802 struct ccb_scsiio csio; 803 struct disk *dp; 804 805 dp = arg; 806 periph = dp->d_drv1; 807 if (periph == NULL) 808 return (ENXIO); 809 softc = (struct da_softc *)periph->softc; 810 secsize = softc->params.secsize; 811 812 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) 813 return (ENXIO); 814 815 if (length > 0) { 816 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1); 817 csio.ccb_h.ccb_state = DA_CCB_DUMP; 818 scsi_read_write(&csio, 819 /*retries*/1, 820 dadone, 821 MSG_ORDERED_Q_TAG, 822 /*read*/FALSE, 823 /*byte2*/0, 824 /*minimum_cmd_size*/ softc->minimum_cmd_size, 825 offset / secsize, 826 length / secsize, 827 /*data_ptr*/(u_int8_t *) virtual, 828 /*dxfer_len*/length, 829 /*sense_len*/SSD_FULL_SIZE, 830 DA_DEFAULT_TIMEOUT * 1000); 831 xpt_polled_action((union ccb *)&csio); 832 833 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 834 printf("Aborting dump due to I/O error.\n"); 835 if ((csio.ccb_h.status & CAM_STATUS_MASK) == 836 CAM_SCSI_STATUS_ERROR) 837 scsi_sense_print(&csio); 838 else 839 printf("status == 0x%x, scsi status == 0x%x\n", 840 csio.ccb_h.status, csio.scsi_status); 841 return(EIO); 842 } 843 return(0); 844 } 845 846 /* 847 * Sync the disk cache contents to the physical media. 848 */ 849 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) { 850 851 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1); 852 csio.ccb_h.ccb_state = DA_CCB_DUMP; 853 scsi_synchronize_cache(&csio, 854 /*retries*/1, 855 /*cbfcnp*/dadone, 856 MSG_SIMPLE_Q_TAG, 857 /*begin_lba*/0,/* Cover the whole disk */ 858 /*lb_count*/0, 859 SSD_FULL_SIZE, 860 5 * 60 * 1000); 861 xpt_polled_action((union ccb *)&csio); 862 863 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 864 if ((csio.ccb_h.status & CAM_STATUS_MASK) == 865 CAM_SCSI_STATUS_ERROR) { 866 int asc, ascq; 867 int sense_key, error_code; 868 869 scsi_extract_sense(&csio.sense_data, 870 &error_code, 871 &sense_key, 872 &asc, &ascq); 873 if (sense_key != SSD_KEY_ILLEGAL_REQUEST) 874 scsi_sense_print(&csio); 875 } else { 876 xpt_print_path(periph->path); 877 printf("Synchronize cache failed, status " 878 "== 0x%x, scsi status == 0x%x\n", 879 csio.ccb_h.status, csio.scsi_status); 880 } 881 } 882 } 883 return (0); 884 } 885 886 static void 887 dainit(void) 888 { 889 cam_status status; 890 struct cam_path *path; 891 892 SLIST_INIT(&softc_list); 893 894 /* 895 * Install a global async callback. This callback will 896 * receive async callbacks like "new device found". 897 */ 898 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID, 899 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 900 901 if (status == CAM_REQ_CMP) { 902 struct ccb_setasync csa; 903 904 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5); 905 csa.ccb_h.func_code = XPT_SASYNC_CB; 906 csa.event_enable = AC_FOUND_DEVICE; 907 csa.callback = daasync; 908 csa.callback_arg = NULL; 909 xpt_action((union ccb *)&csa); 910 status = csa.ccb_h.status; 911 xpt_free_path(path); 912 } 913 914 if (status != CAM_REQ_CMP) { 915 printf("da: Failed to attach master async callback " 916 "due to status 0x%x!\n", status); 917 } else { 918 919 /* 920 * Schedule a periodic event to occasionally send an 921 * ordered tag to a device. 922 */ 923 timeout(dasendorderedtag, NULL, 924 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL); 925 926 /* Register our shutdown event handler */ 927 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown, 928 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 929 printf("dainit: shutdown event registration failed!\n"); 930 } 931 } 932 933 static void 934 daoninvalidate(struct cam_periph *periph) 935 { 936 int s; 937 struct da_softc *softc; 938 struct bio *q_bp; 939 struct ccb_setasync csa; 940 941 softc = (struct da_softc *)periph->softc; 942 943 /* 944 * De-register any async callbacks. 945 */ 946 xpt_setup_ccb(&csa.ccb_h, periph->path, 947 /* priority */ 5); 948 csa.ccb_h.func_code = XPT_SASYNC_CB; 949 csa.event_enable = 0; 950 csa.callback = daasync; 951 csa.callback_arg = periph; 952 xpt_action((union ccb *)&csa); 953 954 softc->flags |= DA_FLAG_PACK_INVALID; 955 956 /* 957 * Although the oninvalidate() routines are always called at 958 * splsoftcam, we need to be at splbio() here to keep the buffer 959 * queue from being modified while we traverse it. 960 */ 961 s = splbio(); 962 963 /* 964 * Return all queued I/O with ENXIO. 965 * XXX Handle any transactions queued to the card 966 * with XPT_ABORT_CCB. 967 */ 968 while ((q_bp = bioq_first(&softc->bio_queue)) != NULL){ 969 bioq_remove(&softc->bio_queue, q_bp); 970 q_bp->bio_resid = q_bp->bio_bcount; 971 biofinish(q_bp, NULL, ENXIO); 972 } 973 splx(s); 974 975 SLIST_REMOVE(&softc_list, softc, da_softc, links); 976 977 xpt_print_path(periph->path); 978 printf("lost device\n"); 979 } 980 981 static void 982 dacleanup(struct cam_periph *periph) 983 { 984 struct da_softc *softc; 985 986 softc = (struct da_softc *)periph->softc; 987 988 xpt_print_path(periph->path); 989 printf("removing device entry\n"); 990 /* 991 * If we can't free the sysctl tree, oh well... 992 */ 993 if (sysctl_ctx_free(&softc->sysctl_ctx) != 0) { 994 xpt_print_path(periph->path); 995 printf("can't remove sysctl context\n"); 996 } 997 disk_destroy(&softc->disk); 998 free(softc, M_DEVBUF); 999 } 1000 1001 static void 1002 daasync(void *callback_arg, u_int32_t code, 1003 struct cam_path *path, void *arg) 1004 { 1005 struct cam_periph *periph; 1006 1007 periph = (struct cam_periph *)callback_arg; 1008 switch (code) { 1009 case AC_FOUND_DEVICE: 1010 { 1011 struct ccb_getdev *cgd; 1012 cam_status status; 1013 1014 cgd = (struct ccb_getdev *)arg; 1015 if (cgd == NULL) 1016 break; 1017 1018 if (SID_TYPE(&cgd->inq_data) != T_DIRECT 1019 && SID_TYPE(&cgd->inq_data) != T_RBC 1020 && SID_TYPE(&cgd->inq_data) != T_OPTICAL) 1021 break; 1022 1023 /* 1024 * Allocate a peripheral instance for 1025 * this device and start the probe 1026 * process. 1027 */ 1028 status = cam_periph_alloc(daregister, daoninvalidate, 1029 dacleanup, dastart, 1030 "da", CAM_PERIPH_BIO, 1031 cgd->ccb_h.path, daasync, 1032 AC_FOUND_DEVICE, cgd); 1033 1034 if (status != CAM_REQ_CMP 1035 && status != CAM_REQ_INPROG) 1036 printf("daasync: Unable to attach to new device " 1037 "due to status 0x%x\n", status); 1038 break; 1039 } 1040 case AC_SENT_BDR: 1041 case AC_BUS_RESET: 1042 { 1043 struct da_softc *softc; 1044 struct ccb_hdr *ccbh; 1045 int s; 1046 1047 softc = (struct da_softc *)periph->softc; 1048 s = splsoftcam(); 1049 /* 1050 * Don't fail on the expected unit attention 1051 * that will occur. 1052 */ 1053 softc->flags |= DA_FLAG_RETRY_UA; 1054 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le) 1055 ccbh->ccb_state |= DA_CCB_RETRY_UA; 1056 splx(s); 1057 /* FALLTHROUGH*/ 1058 } 1059 default: 1060 cam_periph_async(periph, code, path, arg); 1061 break; 1062 } 1063 } 1064 1065 static int 1066 dacmdsizesysctl(SYSCTL_HANDLER_ARGS) 1067 { 1068 int error, value; 1069 1070 value = *(int *)arg1; 1071 1072 error = sysctl_handle_int(oidp, &value, 0, req); 1073 1074 if ((error != 0) 1075 || (req->newptr == NULL)) 1076 return (error); 1077 1078 /* 1079 * Acceptable values here are 6, 10 or 12. It's possible we may 1080 * support a 16 byte minimum command size in the future, since 1081 * there are now READ(16) and WRITE(16) commands defined in the 1082 * SBC-2 spec. 1083 */ 1084 if (value < 6) 1085 value = 6; 1086 else if ((value > 6) 1087 && (value <= 10)) 1088 value = 10; 1089 else if (value > 10) 1090 value = 12; 1091 1092 *(int *)arg1 = value; 1093 1094 return (0); 1095 } 1096 1097 static cam_status 1098 daregister(struct cam_periph *periph, void *arg) 1099 { 1100 int s; 1101 struct da_softc *softc; 1102 struct ccb_setasync csa; 1103 struct ccb_getdev *cgd; 1104 char tmpstr[80], tmpstr2[80]; 1105 caddr_t match; 1106 1107 cgd = (struct ccb_getdev *)arg; 1108 if (periph == NULL) { 1109 printf("daregister: periph was NULL!!\n"); 1110 return(CAM_REQ_CMP_ERR); 1111 } 1112 1113 if (cgd == NULL) { 1114 printf("daregister: no getdev CCB, can't register device\n"); 1115 return(CAM_REQ_CMP_ERR); 1116 } 1117 1118 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF, 1119 M_NOWAIT|M_ZERO); 1120 1121 if (softc == NULL) { 1122 printf("daregister: Unable to probe new device. " 1123 "Unable to allocate softc\n"); 1124 return(CAM_REQ_CMP_ERR); 1125 } 1126 1127 LIST_INIT(&softc->pending_ccbs); 1128 softc->state = DA_STATE_PROBE; 1129 bioq_init(&softc->bio_queue); 1130 if (SID_IS_REMOVABLE(&cgd->inq_data)) 1131 softc->flags |= DA_FLAG_PACK_REMOVABLE; 1132 if ((cgd->inq_data.flags & SID_CmdQue) != 0) 1133 softc->flags |= DA_FLAG_TAGGED_QUEUING; 1134 1135 periph->softc = softc; 1136 1137 /* 1138 * See if this device has any quirks. 1139 */ 1140 match = cam_quirkmatch((caddr_t)&cgd->inq_data, 1141 (caddr_t)da_quirk_table, 1142 sizeof(da_quirk_table)/sizeof(*da_quirk_table), 1143 sizeof(*da_quirk_table), scsi_inquiry_match); 1144 1145 if (match != NULL) 1146 softc->quirks = ((struct da_quirk_entry *)match)->quirks; 1147 else 1148 softc->quirks = DA_Q_NONE; 1149 1150 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number); 1151 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 1152 sysctl_ctx_init(&softc->sysctl_ctx); 1153 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1154 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2, 1155 CTLFLAG_RD, 0, tmpstr); 1156 if (softc->sysctl_tree == NULL) { 1157 printf("daregister: unable to allocate sysctl tree\n"); 1158 free(softc, M_DEVBUF); 1159 return (CAM_REQ_CMP_ERR); 1160 } 1161 1162 /* 1163 * RBC devices don't have to support READ(6), only READ(10). 1164 */ 1165 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC) 1166 softc->minimum_cmd_size = 10; 1167 else 1168 softc->minimum_cmd_size = 6; 1169 1170 /* 1171 * Load the user's default, if any. 1172 */ 1173 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size", 1174 periph->unit_number); 1175 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size); 1176 1177 /* 1178 * 6, 10 and 12 are the currently permissible values. 1179 */ 1180 if (softc->minimum_cmd_size < 6) 1181 softc->minimum_cmd_size = 6; 1182 else if ((softc->minimum_cmd_size > 6) 1183 && (softc->minimum_cmd_size <= 10)) 1184 softc->minimum_cmd_size = 10; 1185 else if (softc->minimum_cmd_size > 12) 1186 softc->minimum_cmd_size = 12; 1187 1188 /* 1189 * Now register the sysctl handler, so the user can the value on 1190 * the fly. 1191 */ 1192 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1193 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW, 1194 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I", 1195 "Minimum CDB size"); 1196 1197 /* 1198 * Block our timeout handler while we 1199 * add this softc to the dev list. 1200 */ 1201 s = splsoftclock(); 1202 SLIST_INSERT_HEAD(&softc_list, softc, links); 1203 splx(s); 1204 1205 /* 1206 * Register this media as a disk 1207 */ 1208 1209 softc->disk.d_open = daopen; 1210 softc->disk.d_close = daclose; 1211 softc->disk.d_strategy = dastrategy; 1212 softc->disk.d_ioctl = daioctl; 1213 softc->disk.d_dump = dadump; 1214 softc->disk.d_name = "da"; 1215 softc->disk.d_drv1 = periph; 1216 softc->disk.d_maxsize = DFLTPHYS; /* XXX: probably not arbitrary */ 1217 disk_create(periph->unit_number, &softc->disk, 0, NULL, NULL); 1218 1219 /* 1220 * Add async callbacks for bus reset and 1221 * bus device reset calls. I don't bother 1222 * checking if this fails as, in most cases, 1223 * the system will function just fine without 1224 * them and the only alternative would be to 1225 * not attach the device on failure. 1226 */ 1227 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5); 1228 csa.ccb_h.func_code = XPT_SASYNC_CB; 1229 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE; 1230 csa.callback = daasync; 1231 csa.callback_arg = periph; 1232 xpt_action((union ccb *)&csa); 1233 /* 1234 * Lock this peripheral until we are setup. 1235 * This first call can't block 1236 */ 1237 (void)cam_periph_lock(periph, PRIBIO); 1238 xpt_schedule(periph, /*priority*/5); 1239 1240 return(CAM_REQ_CMP); 1241 } 1242 1243 static void 1244 dastart(struct cam_periph *periph, union ccb *start_ccb) 1245 { 1246 struct da_softc *softc; 1247 1248 softc = (struct da_softc *)periph->softc; 1249 1250 1251 switch (softc->state) { 1252 case DA_STATE_NORMAL: 1253 { 1254 /* Pull a buffer from the queue and get going on it */ 1255 struct bio *bp; 1256 int s; 1257 1258 /* 1259 * See if there is a buf with work for us to do.. 1260 */ 1261 s = splbio(); 1262 bp = bioq_first(&softc->bio_queue); 1263 if (periph->immediate_priority <= periph->pinfo.priority) { 1264 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE, 1265 ("queuing for immediate ccb\n")); 1266 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING; 1267 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 1268 periph_links.sle); 1269 periph->immediate_priority = CAM_PRIORITY_NONE; 1270 splx(s); 1271 wakeup(&periph->ccb_list); 1272 } else if (bp == NULL) { 1273 splx(s); 1274 xpt_release_ccb(start_ccb); 1275 } else { 1276 int oldspl; 1277 u_int8_t tag_code; 1278 1279 bioq_remove(&softc->bio_queue, bp); 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 softc->outstanding_cmds++; 1311 splx(oldspl); 1312 1313 /* We expect a unit attention from this device */ 1314 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) { 1315 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA; 1316 softc->flags &= ~DA_FLAG_RETRY_UA; 1317 } 1318 1319 start_ccb->ccb_h.ccb_bp = bp; 1320 bp = bioq_first(&softc->bio_queue); 1321 splx(s); 1322 1323 xpt_action(start_ccb); 1324 } 1325 1326 if (bp != NULL) { 1327 /* Have more work to do, so ensure we stay scheduled */ 1328 xpt_schedule(periph, /* XXX priority */1); 1329 } 1330 break; 1331 } 1332 case DA_STATE_PROBE: 1333 { 1334 struct ccb_scsiio *csio; 1335 struct scsi_read_capacity_data *rcap; 1336 1337 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap), 1338 M_TEMP, 1339 M_NOWAIT); 1340 if (rcap == NULL) { 1341 printf("dastart: Couldn't malloc read_capacity data\n"); 1342 /* da_free_periph??? */ 1343 break; 1344 } 1345 csio = &start_ccb->csio; 1346 scsi_read_capacity(csio, 1347 /*retries*/4, 1348 dadone, 1349 MSG_SIMPLE_Q_TAG, 1350 rcap, 1351 SSD_FULL_SIZE, 1352 /*timeout*/5000); 1353 start_ccb->ccb_h.ccb_bp = NULL; 1354 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE; 1355 xpt_action(start_ccb); 1356 break; 1357 } 1358 } 1359 } 1360 1361 static int 1362 cmd6workaround(union ccb *ccb) 1363 { 1364 struct scsi_rw_6 cmd6; 1365 struct scsi_rw_10 *cmd10; 1366 struct da_softc *softc; 1367 u_int8_t *cdb; 1368 int frozen; 1369 1370 cdb = ccb->csio.cdb_io.cdb_bytes; 1371 1372 /* Translation only possible if CDB is an array and cmd is R/W6 */ 1373 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 || 1374 (*cdb != READ_6 && *cdb != WRITE_6)) 1375 return 0; 1376 1377 xpt_print_path(ccb->ccb_h.path); 1378 printf("READ(6)/WRITE(6) not supported, " 1379 "increasing minimum_cmd_size to 10.\n"); 1380 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc; 1381 softc->minimum_cmd_size = 10; 1382 1383 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6)); 1384 cmd10 = (struct scsi_rw_10 *)cdb; 1385 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10; 1386 cmd10->byte2 = 0; 1387 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr); 1388 cmd10->reserved = 0; 1389 scsi_ulto2b(cmd6.length, cmd10->length); 1390 cmd10->control = cmd6.control; 1391 ccb->csio.cdb_len = sizeof(*cmd10); 1392 1393 /* Requeue request, unfreezing queue if necessary */ 1394 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 1395 ccb->ccb_h.status = CAM_REQUEUE_REQ; 1396 xpt_action(ccb); 1397 if (frozen) { 1398 cam_release_devq(ccb->ccb_h.path, 1399 /*relsim_flags*/0, 1400 /*reduction*/0, 1401 /*timeout*/0, 1402 /*getcount_only*/0); 1403 } 1404 return (ERESTART); 1405 } 1406 1407 static void 1408 dadone(struct cam_periph *periph, union ccb *done_ccb) 1409 { 1410 struct da_softc *softc; 1411 struct ccb_scsiio *csio; 1412 1413 softc = (struct da_softc *)periph->softc; 1414 csio = &done_ccb->csio; 1415 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) { 1416 case DA_CCB_BUFFER_IO: 1417 { 1418 struct bio *bp; 1419 int oldspl; 1420 1421 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 1422 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1423 int error; 1424 int s; 1425 int sf; 1426 1427 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0) 1428 sf = SF_RETRY_UA; 1429 else 1430 sf = 0; 1431 1432 error = daerror(done_ccb, CAM_RETRY_SELTO, sf); 1433 if (error == ERESTART) { 1434 /* 1435 * A retry was scheuled, so 1436 * just return. 1437 */ 1438 return; 1439 } 1440 if (error != 0) { 1441 struct bio *q_bp; 1442 1443 s = splbio(); 1444 1445 if (error == ENXIO) { 1446 /* 1447 * Catastrophic error. Mark our pack as 1448 * invalid. 1449 */ 1450 /* XXX See if this is really a media 1451 * change first. 1452 */ 1453 xpt_print_path(periph->path); 1454 printf("Invalidating pack\n"); 1455 softc->flags |= DA_FLAG_PACK_INVALID; 1456 } 1457 1458 /* 1459 * return all queued I/O with EIO, so that 1460 * the client can retry these I/Os in the 1461 * proper order should it attempt to recover. 1462 */ 1463 while ((q_bp = bioq_first(&softc->bio_queue)) 1464 != NULL) { 1465 bioq_remove(&softc->bio_queue, q_bp); 1466 q_bp->bio_resid = q_bp->bio_bcount; 1467 biofinish(q_bp, NULL, EIO); 1468 } 1469 splx(s); 1470 bp->bio_error = error; 1471 bp->bio_resid = bp->bio_bcount; 1472 bp->bio_flags |= BIO_ERROR; 1473 } else { 1474 bp->bio_resid = csio->resid; 1475 bp->bio_error = 0; 1476 if (bp->bio_resid != 0) { 1477 /* Short transfer ??? */ 1478 #if 0 1479 if (cmd6workaround(done_ccb) 1480 == ERESTART) 1481 return; 1482 #endif 1483 bp->bio_flags |= BIO_ERROR; 1484 } 1485 } 1486 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1487 cam_release_devq(done_ccb->ccb_h.path, 1488 /*relsim_flags*/0, 1489 /*reduction*/0, 1490 /*timeout*/0, 1491 /*getcount_only*/0); 1492 } else { 1493 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1494 panic("REQ_CMP with QFRZN"); 1495 bp->bio_resid = csio->resid; 1496 if (csio->resid > 0) { 1497 /* Short transfer ??? */ 1498 #if 0 /* XXX most of the broken umass devices need this ad-hoc work around */ 1499 if (cmd6workaround(done_ccb) == ERESTART) 1500 return; 1501 #endif 1502 bp->bio_flags |= BIO_ERROR; 1503 } 1504 } 1505 1506 /* 1507 * Block out any asyncronous callbacks 1508 * while we touch the pending ccb list. 1509 */ 1510 oldspl = splcam(); 1511 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 1512 softc->outstanding_cmds--; 1513 if (softc->outstanding_cmds == 0) 1514 softc->flags |= DA_FLAG_WENT_IDLE; 1515 splx(oldspl); 1516 1517 biodone(bp); 1518 break; 1519 } 1520 case DA_CCB_PROBE: 1521 { 1522 struct scsi_read_capacity_data *rdcap; 1523 char announce_buf[80]; 1524 1525 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr; 1526 1527 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1528 struct disk_params *dp; 1529 1530 dasetgeom(periph, rdcap); 1531 dp = &softc->params; 1532 snprintf(announce_buf, sizeof(announce_buf), 1533 "%luMB (%u %u byte sectors: %dH %dS/T %dC)", 1534 (unsigned long) (((u_int64_t)dp->secsize * 1535 dp->sectors) / (1024*1024)), dp->sectors, 1536 dp->secsize, dp->heads, dp->secs_per_track, 1537 dp->cylinders); 1538 } else { 1539 int error; 1540 1541 announce_buf[0] = '\0'; 1542 1543 /* 1544 * Retry any UNIT ATTENTION type errors. They 1545 * are expected at boot. 1546 */ 1547 error = daerror(done_ccb, CAM_RETRY_SELTO, 1548 SF_RETRY_UA|SF_NO_PRINT); 1549 if (error == ERESTART) { 1550 /* 1551 * A retry was scheuled, so 1552 * just return. 1553 */ 1554 return; 1555 } else if (error != 0) { 1556 struct scsi_sense_data *sense; 1557 int asc, ascq; 1558 int sense_key, error_code; 1559 int have_sense; 1560 cam_status status; 1561 struct ccb_getdev cgd; 1562 1563 /* Don't wedge this device's queue */ 1564 status = done_ccb->ccb_h.status; 1565 if ((status & CAM_DEV_QFRZN) != 0) 1566 cam_release_devq(done_ccb->ccb_h.path, 1567 /*relsim_flags*/0, 1568 /*reduction*/0, 1569 /*timeout*/0, 1570 /*getcount_only*/0); 1571 1572 1573 xpt_setup_ccb(&cgd.ccb_h, 1574 done_ccb->ccb_h.path, 1575 /* priority */ 1); 1576 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1577 xpt_action((union ccb *)&cgd); 1578 1579 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0) 1580 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0) 1581 || ((status & CAM_AUTOSNS_VALID) == 0)) 1582 have_sense = FALSE; 1583 else 1584 have_sense = TRUE; 1585 1586 if (have_sense) { 1587 sense = &csio->sense_data; 1588 scsi_extract_sense(sense, &error_code, 1589 &sense_key, 1590 &asc, &ascq); 1591 } 1592 /* 1593 * Attach to anything that claims to be a 1594 * direct access or optical disk device, 1595 * as long as it doesn't return a "Logical 1596 * unit not supported" (0x25) error. 1597 */ 1598 if ((have_sense) && (asc != 0x25) 1599 && (error_code == SSD_CURRENT_ERROR)) { 1600 const char *sense_key_desc; 1601 const char *asc_desc; 1602 1603 scsi_sense_desc(sense_key, asc, ascq, 1604 &cgd.inq_data, 1605 &sense_key_desc, 1606 &asc_desc); 1607 snprintf(announce_buf, 1608 sizeof(announce_buf), 1609 "Attempt to query device " 1610 "size failed: %s, %s", 1611 sense_key_desc, 1612 asc_desc); 1613 } else { 1614 if (have_sense) 1615 scsi_sense_print( 1616 &done_ccb->csio); 1617 else { 1618 xpt_print_path(periph->path); 1619 printf("got CAM status %#x\n", 1620 done_ccb->ccb_h.status); 1621 } 1622 1623 xpt_print_path(periph->path); 1624 printf("fatal error, failed" 1625 " to attach to device\n"); 1626 1627 /* 1628 * Free up resources. 1629 */ 1630 cam_periph_invalidate(periph); 1631 } 1632 } 1633 } 1634 free(rdcap, M_TEMP); 1635 if (announce_buf[0] != '\0') 1636 xpt_announce_periph(periph, announce_buf); 1637 softc->state = DA_STATE_NORMAL; 1638 /* 1639 * Since our peripheral may be invalidated by an error 1640 * above or an external event, we must release our CCB 1641 * before releasing the probe lock on the peripheral. 1642 * The peripheral will only go away once the last lock 1643 * is removed, and we need it around for the CCB release 1644 * operation. 1645 */ 1646 xpt_release_ccb(done_ccb); 1647 cam_periph_unlock(periph); 1648 return; 1649 } 1650 case DA_CCB_WAITING: 1651 { 1652 /* Caller will release the CCB */ 1653 wakeup(&done_ccb->ccb_h.cbfcnp); 1654 return; 1655 } 1656 case DA_CCB_DUMP: 1657 /* No-op. We're polling */ 1658 return; 1659 default: 1660 break; 1661 } 1662 xpt_release_ccb(done_ccb); 1663 } 1664 1665 static int 1666 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 1667 { 1668 struct da_softc *softc; 1669 struct cam_periph *periph; 1670 int error; 1671 1672 periph = xpt_path_periph(ccb->ccb_h.path); 1673 softc = (struct da_softc *)periph->softc; 1674 1675 /* 1676 * Automatically detect devices that do not support 1677 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs. 1678 */ 1679 error = 0; 1680 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) { 1681 error = cmd6workaround(ccb); 1682 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == 1683 CAM_SCSI_STATUS_ERROR) 1684 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) 1685 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) 1686 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0) 1687 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) { 1688 int sense_key, error_code, asc, ascq; 1689 1690 scsi_extract_sense(&ccb->csio.sense_data, 1691 &error_code, &sense_key, &asc, &ascq); 1692 if (sense_key == SSD_KEY_ILLEGAL_REQUEST) 1693 error = cmd6workaround(ccb); 1694 } 1695 if (error == ERESTART) 1696 return (ERESTART); 1697 1698 /* 1699 * XXX 1700 * Until we have a better way of doing pack validation, 1701 * don't treat UAs as errors. 1702 */ 1703 sense_flags |= SF_RETRY_UA; 1704 return(cam_periph_error(ccb, cam_flags, sense_flags, 1705 &softc->saved_ccb)); 1706 } 1707 1708 static void 1709 daprevent(struct cam_periph *periph, int action) 1710 { 1711 struct da_softc *softc; 1712 union ccb *ccb; 1713 int error; 1714 1715 softc = (struct da_softc *)periph->softc; 1716 1717 if (((action == PR_ALLOW) 1718 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0) 1719 || ((action == PR_PREVENT) 1720 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) { 1721 return; 1722 } 1723 1724 ccb = cam_periph_getccb(periph, /*priority*/1); 1725 1726 scsi_prevent(&ccb->csio, 1727 /*retries*/1, 1728 /*cbcfp*/dadone, 1729 MSG_SIMPLE_Q_TAG, 1730 action, 1731 SSD_FULL_SIZE, 1732 5000); 1733 1734 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO, 1735 SF_RETRY_UA, softc->disk.d_devstat); 1736 1737 if (error == 0) { 1738 if (action == PR_ALLOW) 1739 softc->flags &= ~DA_FLAG_PACK_LOCKED; 1740 else 1741 softc->flags |= DA_FLAG_PACK_LOCKED; 1742 } 1743 1744 xpt_release_ccb(ccb); 1745 } 1746 1747 static void 1748 dasetgeom(struct cam_periph *periph, struct scsi_read_capacity_data * rdcap) 1749 { 1750 struct ccb_calc_geometry ccg; 1751 struct da_softc *softc; 1752 struct disk_params *dp; 1753 1754 softc = (struct da_softc *)periph->softc; 1755 1756 dp = &softc->params; 1757 dp->secsize = scsi_4btoul(rdcap->length); 1758 dp->sectors = scsi_4btoul(rdcap->addr) + 1; 1759 /* 1760 * Have the controller provide us with a geometry 1761 * for this disk. The only time the geometry 1762 * matters is when we boot and the controller 1763 * is the only one knowledgeable enough to come 1764 * up with something that will make this a bootable 1765 * device. 1766 */ 1767 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1); 1768 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY; 1769 ccg.block_size = dp->secsize; 1770 ccg.volume_size = dp->sectors; 1771 ccg.heads = 0; 1772 ccg.secs_per_track = 0; 1773 ccg.cylinders = 0; 1774 xpt_action((union ccb*)&ccg); 1775 dp->heads = ccg.heads; 1776 dp->secs_per_track = ccg.secs_per_track; 1777 dp->cylinders = ccg.cylinders; 1778 } 1779 1780 static void 1781 dasendorderedtag(void *arg) 1782 { 1783 struct da_softc *softc; 1784 int s; 1785 1786 for (softc = SLIST_FIRST(&softc_list); 1787 softc != NULL; 1788 softc = SLIST_NEXT(softc, links)) { 1789 s = splsoftcam(); 1790 if ((softc->ordered_tag_count == 0) 1791 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) { 1792 softc->flags |= DA_FLAG_NEED_OTAG; 1793 } 1794 if (softc->outstanding_cmds > 0) 1795 softc->flags &= ~DA_FLAG_WENT_IDLE; 1796 1797 softc->ordered_tag_count = 0; 1798 splx(s); 1799 } 1800 /* Queue us up again */ 1801 timeout(dasendorderedtag, NULL, 1802 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL); 1803 } 1804 1805 /* 1806 * Step through all DA peripheral drivers, and if the device is still open, 1807 * sync the disk cache to physical media. 1808 */ 1809 static void 1810 dashutdown(void * arg, int howto) 1811 { 1812 struct cam_periph *periph; 1813 struct da_softc *softc; 1814 1815 TAILQ_FOREACH(periph, &dadriver.units, unit_links) { 1816 union ccb ccb; 1817 softc = (struct da_softc *)periph->softc; 1818 1819 /* 1820 * We only sync the cache if the drive is still open, and 1821 * if the drive is capable of it.. 1822 */ 1823 if (((softc->flags & DA_FLAG_OPEN) == 0) 1824 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) 1825 continue; 1826 1827 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1); 1828 1829 ccb.ccb_h.ccb_state = DA_CCB_DUMP; 1830 scsi_synchronize_cache(&ccb.csio, 1831 /*retries*/1, 1832 /*cbfcnp*/dadone, 1833 MSG_SIMPLE_Q_TAG, 1834 /*begin_lba*/0, /* whole disk */ 1835 /*lb_count*/0, 1836 SSD_FULL_SIZE, 1837 60 * 60 * 1000); 1838 1839 xpt_polled_action(&ccb); 1840 1841 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1842 if (((ccb.ccb_h.status & CAM_STATUS_MASK) == 1843 CAM_SCSI_STATUS_ERROR) 1844 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){ 1845 int error_code, sense_key, asc, ascq; 1846 1847 scsi_extract_sense(&ccb.csio.sense_data, 1848 &error_code, &sense_key, 1849 &asc, &ascq); 1850 1851 if (sense_key != SSD_KEY_ILLEGAL_REQUEST) 1852 scsi_sense_print(&ccb.csio); 1853 } else { 1854 xpt_print_path(periph->path); 1855 printf("Synchronize cache failed, status " 1856 "== 0x%x, scsi status == 0x%x\n", 1857 ccb.ccb_h.status, ccb.csio.scsi_status); 1858 } 1859 } 1860 1861 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 1862 cam_release_devq(ccb.ccb_h.path, 1863 /*relsim_flags*/0, 1864 /*reduction*/0, 1865 /*timeout*/0, 1866 /*getcount_only*/0); 1867 1868 } 1869 } 1870 1871 #else /* !_KERNEL */ 1872 1873 /* 1874 * XXX This is only left out of the kernel build to silence warnings. If, 1875 * for some reason this function is used in the kernel, the ifdefs should 1876 * be moved so it is included both in the kernel and userland. 1877 */ 1878 void 1879 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries, 1880 void (*cbfcnp)(struct cam_periph *, union ccb *), 1881 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave, 1882 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, 1883 u_int32_t timeout) 1884 { 1885 struct scsi_format_unit *scsi_cmd; 1886 1887 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes; 1888 scsi_cmd->opcode = FORMAT_UNIT; 1889 scsi_cmd->byte2 = byte2; 1890 scsi_ulto2b(ileave, scsi_cmd->interleave); 1891 1892 cam_fill_csio(csio, 1893 retries, 1894 cbfcnp, 1895 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE, 1896 tag_action, 1897 data_ptr, 1898 dxfer_len, 1899 sense_len, 1900 sizeof(*scsi_cmd), 1901 timeout); 1902 } 1903 1904 #endif /* _KERNEL */ 1905