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