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