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