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