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