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