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