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: src/sys/cam/scsi/scsi_da.c,v 1.42.2.46 2003/10/21 22:18:19 thomas Exp $ 29 * $DragonFly: src/sys/bus/cam/scsi/scsi_da.c,v 1.59 2008/08/29 20:08:40 dillon Exp $ 30 */ 31 32 #include <sys/param.h> 33 #include <sys/bootmaj.h> 34 35 #ifdef _KERNEL 36 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/buf.h> 40 #include <sys/sysctl.h> 41 #include <sys/taskqueue.h> 42 #include <sys/lock.h> 43 #include <sys/conf.h> 44 #include <sys/devicestat.h> 45 #include <sys/disk.h> 46 #include <sys/dtype.h> 47 #include <sys/eventhandler.h> 48 #include <sys/malloc.h> 49 #include <sys/cons.h> 50 #include <sys/proc.h> 51 52 #include <sys/buf2.h> 53 #include <sys/thread2.h> 54 #include <sys/mplock2.h> 55 56 #endif /* _KERNEL */ 57 58 #ifdef _KERNEL 59 #include <vm/pmap.h> 60 #endif 61 62 #ifndef _KERNEL 63 #include <stdio.h> 64 #include <string.h> 65 #endif /* _KERNEL */ 66 67 #include <sys/camlib.h> 68 #include "../cam.h" 69 #include "../cam_ccb.h" 70 #include "../cam_extend.h" 71 #include "../cam_periph.h" 72 #include "../cam_xpt_periph.h" 73 #include "../cam_sim.h" 74 75 #include "scsi_message.h" 76 77 #ifndef _KERNEL 78 #include "scsi_da.h" 79 #endif /* !_KERNEL */ 80 81 #ifdef _KERNEL 82 typedef enum { 83 DA_STATE_PROBE, 84 DA_STATE_PROBE2, 85 DA_STATE_NORMAL 86 } da_state; 87 88 typedef enum { 89 DA_FLAG_PACK_INVALID = 0x001, 90 DA_FLAG_NEW_PACK = 0x002, 91 DA_FLAG_PACK_LOCKED = 0x004, 92 DA_FLAG_PACK_REMOVABLE = 0x008, 93 DA_FLAG_TAGGED_QUEUING = 0x010, 94 DA_FLAG_NEED_OTAG = 0x020, 95 DA_FLAG_WENT_IDLE = 0x040, 96 DA_FLAG_RETRY_UA = 0x080, 97 DA_FLAG_OPEN = 0x100, 98 DA_FLAG_SCTX_INIT = 0x200 99 } da_flags; 100 101 typedef enum { 102 DA_Q_NONE = 0x00, 103 DA_Q_NO_SYNC_CACHE = 0x01, 104 DA_Q_NO_6_BYTE = 0x02, 105 DA_Q_NO_PREVENT = 0x04 106 } da_quirks; 107 108 typedef enum { 109 DA_CCB_PROBE = 0x01, 110 DA_CCB_PROBE2 = 0x02, 111 DA_CCB_BUFFER_IO = 0x03, 112 DA_CCB_WAITING = 0x04, 113 DA_CCB_DUMP = 0x05, 114 DA_CCB_TYPE_MASK = 0x0F, 115 DA_CCB_RETRY_UA = 0x10 116 } da_ccb_state; 117 118 /* Offsets into our private area for storing information */ 119 #define ccb_state ppriv_field0 120 #define ccb_bio ppriv_ptr1 121 122 struct disk_params { 123 u_int8_t heads; 124 u_int32_t cylinders; 125 u_int8_t secs_per_track; 126 u_int32_t secsize; /* Number of bytes/sector */ 127 u_int64_t sectors; /* total number sectors */ 128 }; 129 130 struct da_softc { 131 struct bio_queue_head bio_queue; 132 struct devstat device_stats; 133 SLIST_ENTRY(da_softc) links; 134 LIST_HEAD(, ccb_hdr) pending_ccbs; 135 da_state state; 136 da_flags flags; 137 da_quirks quirks; 138 int minimum_cmd_size; 139 int ordered_tag_count; 140 int outstanding_cmds; 141 struct disk_params params; 142 struct disk disk; 143 union ccb saved_ccb; 144 struct task sysctl_task; 145 struct sysctl_ctx_list sysctl_ctx; 146 struct sysctl_oid *sysctl_tree; 147 struct callout sendordered_c; 148 }; 149 150 struct da_quirk_entry { 151 struct scsi_inquiry_pattern inq_pat; 152 da_quirks quirks; 153 }; 154 155 static const char quantum[] = "QUANTUM"; 156 static const char microp[] = "MICROP"; 157 158 static struct da_quirk_entry da_quirk_table[] = 159 { 160 /* SPI, FC devices */ 161 { 162 /* 163 * Fujitsu M2513A MO drives. 164 * Tested devices: M2513A2 firmware versions 1200 & 1300. 165 * (dip switch selects whether T_DIRECT or T_OPTICAL device) 166 * Reported by: W.Scholten <whs@xs4all.nl> 167 */ 168 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"}, 169 /*quirks*/ DA_Q_NO_SYNC_CACHE 170 }, 171 { 172 /* See above. */ 173 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"}, 174 /*quirks*/ DA_Q_NO_SYNC_CACHE 175 }, 176 { 177 /* 178 * This particular Fujitsu drive doesn't like the 179 * synchronize cache command. 180 * Reported by: Tom Jackson <toj@gorilla.net> 181 */ 182 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"}, 183 /*quirks*/ DA_Q_NO_SYNC_CACHE 184 }, 185 { 186 /* 187 * This drive doesn't like the synchronize cache command 188 * either. Reported by: Matthew Jacob <mjacob@feral.com> 189 * in NetBSD PR kern/6027, August 24, 1998. 190 */ 191 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"}, 192 /*quirks*/ DA_Q_NO_SYNC_CACHE 193 }, 194 { 195 /* 196 * This drive doesn't like the synchronize cache command 197 * either. Reported by: Hellmuth Michaelis (hm@kts.org) 198 * (PR 8882). 199 */ 200 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"}, 201 /*quirks*/ DA_Q_NO_SYNC_CACHE 202 }, 203 { 204 /* 205 * Doesn't like the synchronize cache command. 206 * Reported by: Blaz Zupan <blaz@gold.amis.net> 207 */ 208 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"}, 209 /*quirks*/ DA_Q_NO_SYNC_CACHE 210 }, 211 { 212 /* 213 * Doesn't like the synchronize cache command. 214 * Reported by: Blaz Zupan <blaz@gold.amis.net> 215 */ 216 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"}, 217 /*quirks*/ DA_Q_NO_SYNC_CACHE 218 }, 219 { 220 /* 221 * Doesn't like the synchronize cache command. 222 */ 223 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"}, 224 /*quirks*/ DA_Q_NO_SYNC_CACHE 225 }, 226 { 227 /* 228 * Doesn't like the synchronize cache command. 229 * Reported by: walter@pelissero.de 230 */ 231 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"}, 232 /*quirks*/ DA_Q_NO_SYNC_CACHE 233 }, 234 { 235 /* 236 * Doesn't work correctly with 6 byte reads/writes. 237 * Returns illegal request, and points to byte 9 of the 238 * 6-byte CDB. 239 * Reported by: Adam McDougall <bsdx@spawnet.com> 240 */ 241 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"}, 242 /*quirks*/ DA_Q_NO_6_BYTE 243 }, 244 { 245 /* See above. */ 246 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"}, 247 /*quirks*/ DA_Q_NO_6_BYTE 248 }, 249 { 250 /* 251 * Doesn't like the synchronize cache command. 252 * Reported by: walter@pelissero.de 253 */ 254 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"}, 255 /*quirks*/ DA_Q_NO_SYNC_CACHE 256 }, 257 { 258 /* 259 * The CISS RAID controllers do not support SYNC_CACHE 260 */ 261 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"}, 262 /*quirks*/ DA_Q_NO_SYNC_CACHE 263 }, 264 /* 265 * USB mass storage devices supported by umass(4) 266 * 267 * NOTE: USB attachments automatically set DA_Q_NO_SYNC_CACHE so 268 * it does not have to be specified here. 269 */ 270 { 271 /* 272 * Creative Nomad MUVO mp3 player (USB) 273 * PR: kern/53094 274 */ 275 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"}, 276 /*quirks*/ DA_Q_NO_PREVENT 277 }, 278 { 279 /* 280 * Sigmatel USB Flash MP3 Player 281 * PR: kern/57046 282 */ 283 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"}, 284 /*quirks*/ DA_Q_NO_PREVENT 285 }, 286 { 287 /* 288 * SEAGRAND NP-900 MP3 Player 289 * PR: kern/64563 290 */ 291 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"}, 292 /*quirks*/ DA_Q_NO_PREVENT 293 }, 294 { 295 /* 296 * Creative MUVO Slim mp3 player (USB) 297 * PR: usb/86131 298 */ 299 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim", 300 "*"}, /*quirks*/ DA_Q_NO_PREVENT 301 }, 302 { 303 /* 304 * Philips USB Key Audio KEY013 305 * PR: usb/68412 306 */ 307 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"}, 308 /*quirks*/ DA_Q_NO_PREVENT 309 }, 310 }; 311 312 static d_open_t daopen; 313 static d_close_t daclose; 314 static d_strategy_t dastrategy; 315 static d_dump_t dadump; 316 static periph_init_t dainit; 317 static void daasync(void *callback_arg, u_int32_t code, 318 struct cam_path *path, void *arg); 319 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS); 320 static periph_ctor_t daregister; 321 static periph_dtor_t dacleanup; 322 static periph_start_t dastart; 323 static periph_oninv_t daoninvalidate; 324 static void dadone(struct cam_periph *periph, 325 union ccb *done_ccb); 326 static int daerror(union ccb *ccb, u_int32_t cam_flags, 327 u_int32_t sense_flags); 328 static void daprevent(struct cam_periph *periph, int action); 329 static int dagetcapacity(struct cam_periph *periph); 330 static int dacheckmedia(struct cam_periph *periph); 331 static void dasetgeom(struct cam_periph *periph, uint32_t block_len, 332 uint64_t maxsector); 333 334 static timeout_t dasendorderedtag; 335 static void dashutdown(void *arg, int howto); 336 337 #ifndef DA_DEFAULT_TIMEOUT 338 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */ 339 #endif 340 341 #ifndef DA_DEFAULT_RETRY 342 #define DA_DEFAULT_RETRY 4 343 #endif 344 345 #ifndef DA_DEFAULT_SEND_ORDERED 346 #define DA_DEFAULT_SEND_ORDERED 1 347 #endif 348 349 static int da_retry_count = DA_DEFAULT_RETRY; 350 static int da_default_timeout = DA_DEFAULT_TIMEOUT; 351 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED; 352 static struct callout dasendorderedtag_ch; 353 354 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0, 355 "CAM Direct Access Disk driver"); 356 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW, 357 &da_retry_count, 0, "Normal I/O retry count"); 358 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count); 359 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW, 360 &da_default_timeout, 0, "Normal I/O timeout (in seconds)"); 361 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout); 362 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW, 363 &da_send_ordered, 0, "Send Ordered Tags"); 364 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered); 365 366 /* 367 * DA_ORDEREDTAG_INTERVAL determines how often, relative 368 * to the default timeout, we check to see whether an ordered 369 * tagged transaction is appropriate to prevent simple tag 370 * starvation. Since we'd like to ensure that there is at least 371 * 1/2 of the timeout length left for a starved transaction to 372 * complete after we've sent an ordered tag, we must poll at least 373 * four times in every timeout period. This takes care of the worst 374 * case where a starved transaction starts during an interval that 375 * meets the requirement "don't send an ordered tag" test so it takes 376 * us two intervals to determine that a tag must be sent. 377 */ 378 #ifndef DA_ORDEREDTAG_INTERVAL 379 #define DA_ORDEREDTAG_INTERVAL 4 380 #endif 381 382 static struct periph_driver dadriver = 383 { 384 dainit, "da", 385 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0 386 }; 387 388 PERIPHDRIVER_DECLARE(da, dadriver); 389 390 static struct dev_ops da_ops = { 391 { "da", DA_CDEV_MAJOR, D_DISK | D_MPSAFE }, 392 .d_open = daopen, 393 .d_close = daclose, 394 .d_read = physread, 395 .d_write = physwrite, 396 .d_strategy = dastrategy, 397 .d_dump = dadump 398 }; 399 400 static struct extend_array *daperiphs; 401 402 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers"); 403 404 static int 405 daopen(struct dev_open_args *ap) 406 { 407 cdev_t dev = ap->a_head.a_dev; 408 struct cam_periph *periph; 409 struct da_softc *softc; 410 struct disk_info info; 411 int unit; 412 int error; 413 414 unit = dkunit(dev); 415 periph = cam_extend_get(daperiphs, unit); 416 if (periph == NULL) { 417 return (ENXIO); 418 } 419 420 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 421 return(ENXIO); 422 } 423 424 cam_periph_lock(periph); 425 if ((error = cam_periph_hold(periph, PCATCH)) != 0) { 426 cam_periph_unlock(periph); 427 cam_periph_release(periph); 428 return (error); 429 } 430 431 unit = periph->unit_number; 432 softc = (struct da_softc *)periph->softc; 433 434 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 435 ("daopen: dev=%s (unit %d)\n", devtoname(dev), 436 unit)); 437 438 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) { 439 /* Invalidate our pack information. */ 440 disk_invalidate(&softc->disk); 441 softc->flags &= ~DA_FLAG_PACK_INVALID; 442 } 443 444 error = dacheckmedia(periph); 445 softc->flags |= DA_FLAG_OPEN; 446 447 if (error == 0) { 448 struct ccb_getdev cgd; 449 450 /* Build disk information structure */ 451 bzero(&info, sizeof(info)); 452 info.d_type = DTYPE_SCSI; 453 454 /* 455 * Grab the inquiry data to get the vendor and product names. 456 * Put them in the typename and packname for the label. 457 */ 458 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1); 459 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 460 xpt_action((union ccb *)&cgd); 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 (softc->quirks & DA_Q_NO_PREVENT) == 0) 476 daprevent(periph, PR_PREVENT); 477 } else { 478 softc->flags &= ~DA_FLAG_OPEN; 479 cam_periph_release(periph); 480 } 481 cam_periph_unhold(periph, 1); 482 return (error); 483 } 484 485 static int 486 daclose(struct dev_close_args *ap) 487 { 488 cdev_t dev = ap->a_head.a_dev; 489 struct cam_periph *periph; 490 struct da_softc *softc; 491 int unit; 492 int error; 493 494 unit = dkunit(dev); 495 periph = cam_extend_get(daperiphs, unit); 496 if (periph == NULL) 497 return (ENXIO); 498 499 cam_periph_lock(periph); 500 if ((error = cam_periph_hold(periph, 0)) != 0) { 501 cam_periph_unlock(periph); 502 cam_periph_release(periph); 503 return (error); 504 } 505 506 softc = (struct da_softc *)periph->softc; 507 508 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) { 509 union ccb *ccb; 510 511 ccb = cam_periph_getccb(periph, /*priority*/1); 512 513 scsi_synchronize_cache(&ccb->csio, 514 /*retries*/1, 515 /*cbfcnp*/dadone, 516 MSG_SIMPLE_Q_TAG, 517 /*begin_lba*/0,/* Cover the whole disk */ 518 /*lb_count*/0, 519 SSD_FULL_SIZE, 520 5 * 60 * 1000); 521 522 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0, 523 /*sense_flags*/SF_RETRY_UA, 524 &softc->device_stats); 525 526 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 527 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 528 CAM_SCSI_STATUS_ERROR) { 529 int asc, ascq; 530 int sense_key, error_code; 531 532 scsi_extract_sense(&ccb->csio.sense_data, 533 &error_code, 534 &sense_key, 535 &asc, &ascq); 536 if (sense_key != SSD_KEY_ILLEGAL_REQUEST) 537 scsi_sense_print(&ccb->csio); 538 } else { 539 xpt_print(periph->path, "Synchronize cache " 540 "failed, status == 0x%x, scsi status == " 541 "0x%x\n", ccb->csio.ccb_h.status, 542 ccb->csio.scsi_status); 543 } 544 } 545 546 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 547 cam_release_devq(ccb->ccb_h.path, 548 /*relsim_flags*/0, 549 /*reduction*/0, 550 /*timeout*/0, 551 /*getcount_only*/0); 552 553 xpt_release_ccb(ccb); 554 555 } 556 557 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) { 558 if ((softc->quirks & DA_Q_NO_PREVENT) == 0) 559 daprevent(periph, PR_ALLOW); 560 /* 561 * If we've got removeable media, mark the blocksize as 562 * unavailable, since it could change when new media is 563 * inserted. 564 */ 565 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE; 566 } 567 568 /* 569 * Don't compound any ref counting software bugs with more. 570 */ 571 if (softc->flags & DA_FLAG_OPEN) { 572 softc->flags &= ~DA_FLAG_OPEN; 573 cam_periph_release(periph); 574 } else { 575 xpt_print(periph->path, 576 "daclose() called on an already closed device!\n"); 577 } 578 cam_periph_unhold(periph, 1); 579 return (0); 580 } 581 582 /* 583 * Actually translate the requested transfer into one the physical driver 584 * can understand. The transfer is described by a buf and will include 585 * only one physical transfer. 586 */ 587 static int 588 dastrategy(struct dev_strategy_args *ap) 589 { 590 cdev_t dev = ap->a_head.a_dev; 591 struct bio *bio = ap->a_bio; 592 struct buf *bp = bio->bio_buf; 593 struct cam_periph *periph; 594 struct da_softc *softc; 595 u_int unit; 596 u_int part; 597 598 unit = dkunit(dev); 599 part = dkpart(dev); 600 periph = cam_extend_get(daperiphs, unit); 601 if (periph == NULL) { 602 bp->b_error = ENXIO; 603 goto bad; 604 } 605 softc = (struct da_softc *)periph->softc; 606 607 cam_periph_lock(periph); 608 609 #if 0 610 /* 611 * check it's not too big a transfer for our adapter 612 */ 613 scsi_minphys(bp, &sd_switch); 614 #endif 615 616 /* 617 * Mask interrupts so that the pack cannot be invalidated until 618 * after we are in the queue. Otherwise, we might not properly 619 * clean up one of the buffers. 620 */ 621 622 /* 623 * If the device has been made invalid, error out 624 */ 625 if ((softc->flags & DA_FLAG_PACK_INVALID)) { 626 cam_periph_unlock(periph); 627 bp->b_error = ENXIO; 628 goto bad; 629 } 630 631 /* 632 * Place it in the queue of disk activities for this disk 633 */ 634 bioqdisksort(&softc->bio_queue, bio); 635 636 /* 637 * Schedule ourselves for performing the work. 638 */ 639 xpt_schedule(periph, /* XXX priority */1); 640 cam_periph_unlock(periph); 641 642 return(0); 643 bad: 644 bp->b_flags |= B_ERROR; 645 646 /* 647 * Correctly set the buf to indicate a completed xfer 648 */ 649 bp->b_resid = bp->b_bcount; 650 biodone(bio); 651 return(0); 652 } 653 654 static int 655 dadump(struct dev_dump_args *ap) 656 { 657 cdev_t dev = ap->a_head.a_dev; 658 struct cam_periph *periph; 659 struct da_softc *softc; 660 u_int unit; 661 u_int32_t secsize; 662 struct ccb_scsiio csio; 663 664 unit = dkunit(dev); 665 periph = cam_extend_get(daperiphs, unit); 666 if (periph == NULL) 667 return (ENXIO); 668 669 softc = (struct da_softc *)periph->softc; 670 cam_periph_lock(periph); 671 secsize = softc->params.secsize; /* XXX: or ap->a_secsize? */ 672 673 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) { 674 cam_periph_unlock(periph); 675 return (ENXIO); 676 } 677 678 /* 679 * because length == 0 means we are supposed to flush cache, we only 680 * try to write something if length > 0. 681 */ 682 if (ap->a_length > 0) { 683 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1); 684 csio.ccb_h.flags |= CAM_POLLED; 685 csio.ccb_h.ccb_state = DA_CCB_DUMP; 686 scsi_read_write(&csio, 687 /*retries*/1, 688 dadone, 689 MSG_ORDERED_Q_TAG, 690 /*read*/FALSE, 691 /*byte2*/0, 692 /*minimum_cmd_size*/ softc->minimum_cmd_size, 693 ap->a_offset / secsize, 694 ap->a_length / secsize, 695 /*data_ptr*/(u_int8_t *) ap->a_virtual, 696 /*dxfer_len*/ap->a_length, 697 /*sense_len*/SSD_FULL_SIZE, 698 DA_DEFAULT_TIMEOUT * 1000); 699 xpt_polled_action((union ccb *)&csio); 700 701 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 702 kprintf("Aborting dump due to I/O error.\n"); 703 if ((csio.ccb_h.status & CAM_STATUS_MASK) == 704 CAM_SCSI_STATUS_ERROR) 705 scsi_sense_print(&csio); 706 else 707 kprintf("status == 0x%x, scsi status == 0x%x\n", 708 csio.ccb_h.status, csio.scsi_status); 709 return(EIO); 710 } 711 cam_periph_unlock(periph); 712 return 0; 713 } 714 715 /* 716 * Sync the disk cache contents to the physical media. 717 */ 718 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) { 719 720 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1); 721 csio.ccb_h.ccb_state = DA_CCB_DUMP; 722 scsi_synchronize_cache(&csio, 723 /*retries*/1, 724 /*cbfcnp*/dadone, 725 MSG_SIMPLE_Q_TAG, 726 /*begin_lba*/0,/* Cover the whole disk */ 727 /*lb_count*/0, 728 SSD_FULL_SIZE, 729 5 * 60 * 1000); 730 xpt_polled_action((union ccb *)&csio); 731 732 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 733 if ((csio.ccb_h.status & CAM_STATUS_MASK) == 734 CAM_SCSI_STATUS_ERROR) { 735 int asc, ascq; 736 int sense_key, error_code; 737 738 scsi_extract_sense(&csio.sense_data, 739 &error_code, 740 &sense_key, 741 &asc, &ascq); 742 if (sense_key != SSD_KEY_ILLEGAL_REQUEST) 743 scsi_sense_print(&csio); 744 } else { 745 xpt_print(periph->path, "Synchronize cache " 746 "failed, status == 0x%x, scsi status == " 747 "0x%x\n", csio.ccb_h.status, 748 csio.scsi_status); 749 } 750 } 751 } 752 cam_periph_unlock(periph); 753 return (0); 754 } 755 756 static void 757 dainit(void) 758 { 759 cam_status status; 760 761 /* 762 * Create our extend array for storing the devices we attach to. 763 */ 764 daperiphs = cam_extend_new(); 765 if (daperiphs == NULL) { 766 kprintf("da: Failed to alloc extend array!\n"); 767 return; 768 } 769 770 callout_init(&dasendorderedtag_ch); 771 772 /* 773 * Install a global async callback. This callback will 774 * receive async callbacks like "new device found". 775 */ 776 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL); 777 778 if (status != CAM_REQ_CMP) { 779 kprintf("da: Failed to attach master async callback " 780 "due to status 0x%x!\n", status); 781 } else if (da_send_ordered) { 782 783 /* Register our shutdown event handler */ 784 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown, 785 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 786 kprintf("dainit: shutdown event registration failed!\n"); 787 } 788 } 789 790 static void 791 daoninvalidate(struct cam_periph *periph) 792 { 793 struct da_softc *softc; 794 struct bio *q_bio; 795 struct buf *q_bp; 796 797 softc = (struct da_softc *)periph->softc; 798 799 /* 800 * De-register any async callbacks. 801 */ 802 xpt_register_async(0, daasync, periph, periph->path); 803 804 softc->flags |= DA_FLAG_PACK_INVALID; 805 806 /* 807 * Return all queued I/O with ENXIO. 808 * XXX Handle any transactions queued to the card 809 * with XPT_ABORT_CCB. 810 */ 811 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){ 812 bioq_remove(&softc->bio_queue, q_bio); 813 q_bp = q_bio->bio_buf; 814 q_bp->b_resid = q_bp->b_bcount; 815 q_bp->b_error = ENXIO; 816 q_bp->b_flags |= B_ERROR; 817 biodone(q_bio); 818 } 819 xpt_print(periph->path, "lost device\n"); 820 } 821 822 static void 823 dacleanup(struct cam_periph *periph) 824 { 825 struct da_softc *softc; 826 827 softc = (struct da_softc *)periph->softc; 828 829 devstat_remove_entry(&softc->device_stats); 830 cam_extend_release(daperiphs, periph->unit_number); 831 xpt_print(periph->path, "removing device entry\n"); 832 /* 833 * If we can't free the sysctl tree, oh well... 834 */ 835 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0 836 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) { 837 xpt_print(periph->path, "can't remove sysctl context\n"); 838 } 839 periph->softc = NULL; 840 if (softc->disk.d_rawdev) { 841 cam_periph_unlock(periph); 842 disk_destroy(&softc->disk); 843 cam_periph_lock(periph); 844 } 845 846 callout_stop(&softc->sendordered_c); 847 kfree(softc, M_DEVBUF); 848 } 849 850 static void 851 daasync(void *callback_arg, u_int32_t code, 852 struct cam_path *path, void *arg) 853 { 854 struct cam_periph *periph; 855 856 periph = (struct cam_periph *)callback_arg; 857 858 switch (code) { 859 case AC_FOUND_DEVICE: 860 { 861 struct ccb_getdev *cgd; 862 struct cam_sim *sim; 863 cam_status status; 864 865 cgd = (struct ccb_getdev *)arg; 866 if (cgd == NULL) 867 break; 868 869 if (SID_TYPE(&cgd->inq_data) != T_DIRECT 870 && SID_TYPE(&cgd->inq_data) != T_RBC 871 && SID_TYPE(&cgd->inq_data) != T_OPTICAL) 872 break; 873 874 /* 875 * Don't complain if a valid peripheral is already attached. 876 */ 877 periph = cam_periph_find(cgd->ccb_h.path, "da"); 878 if (periph && (periph->flags & CAM_PERIPH_INVALID) == 0) 879 break; 880 881 /* 882 * Allocate a peripheral instance for 883 * this device and start the probe 884 * process. 885 */ 886 sim = xpt_path_sim(cgd->ccb_h.path); 887 status = cam_periph_alloc(daregister, daoninvalidate, 888 dacleanup, dastart, 889 "da", CAM_PERIPH_BIO, 890 cgd->ccb_h.path, daasync, 891 AC_FOUND_DEVICE, cgd); 892 893 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) { 894 kprintf("daasync: Unable to attach to new device " 895 "due to status 0x%x\n", status); 896 } 897 break; 898 } 899 case AC_SENT_BDR: 900 case AC_BUS_RESET: 901 { 902 struct da_softc *softc; 903 struct ccb_hdr *ccbh; 904 905 softc = (struct da_softc *)periph->softc; 906 /* 907 * Don't fail on the expected unit attention 908 * that will occur. 909 */ 910 softc->flags |= DA_FLAG_RETRY_UA; 911 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le) 912 ccbh->ccb_state |= DA_CCB_RETRY_UA; 913 /* FALLTHROUGH*/ 914 } 915 default: 916 cam_periph_async(periph, code, path, arg); 917 break; 918 } 919 } 920 921 static void 922 dasysctlinit(void *context, int pending) 923 { 924 struct cam_periph *periph; 925 struct da_softc *softc; 926 char tmpstr[80], tmpstr2[80]; 927 928 get_mplock(); 929 periph = (struct cam_periph *)context; 930 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 931 rel_mplock(); 932 return; 933 } 934 935 softc = (struct da_softc *)periph->softc; 936 ksnprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number); 937 ksnprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 938 939 sysctl_ctx_init(&softc->sysctl_ctx); 940 softc->flags |= DA_FLAG_SCTX_INIT; 941 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 942 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2, 943 CTLFLAG_RD, 0, tmpstr); 944 if (softc->sysctl_tree == NULL) { 945 kprintf("dasysctlinit: unable to allocate sysctl tree\n"); 946 cam_periph_release(periph); 947 rel_mplock(); 948 return; 949 } 950 951 /* 952 * Now register the sysctl handler, so the user can the value on 953 * the fly. 954 */ 955 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 956 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW, 957 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I", 958 "Minimum CDB size"); 959 960 cam_periph_release(periph); 961 rel_mplock(); 962 } 963 964 static int 965 dacmdsizesysctl(SYSCTL_HANDLER_ARGS) 966 { 967 int error, value; 968 969 value = *(int *)arg1; 970 971 error = sysctl_handle_int(oidp, &value, 0, req); 972 973 if ((error != 0) 974 || (req->newptr == NULL)) 975 return (error); 976 977 /* 978 * Acceptable values here are 6, 10 or 12, or 16. 979 */ 980 if (value < 6) 981 value = 6; 982 else if ((value > 6) 983 && (value <= 10)) 984 value = 10; 985 else if ((value > 10) 986 && (value <= 12)) 987 value = 12; 988 else if (value > 12) 989 value = 16; 990 991 *(int *)arg1 = value; 992 993 return (0); 994 } 995 996 static cam_status 997 daregister(struct cam_periph *periph, void *arg) 998 { 999 struct da_softc *softc; 1000 struct ccb_pathinq cpi; 1001 struct ccb_getdev *cgd; 1002 char tmpstr[80]; 1003 caddr_t match; 1004 1005 cgd = (struct ccb_getdev *)arg; 1006 if (periph == NULL) { 1007 kprintf("daregister: periph was NULL!!\n"); 1008 return(CAM_REQ_CMP_ERR); 1009 } 1010 1011 if (cgd == NULL) { 1012 kprintf("daregister: no getdev CCB, can't register device\n"); 1013 return(CAM_REQ_CMP_ERR); 1014 } 1015 1016 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO); 1017 LIST_INIT(&softc->pending_ccbs); 1018 softc->state = DA_STATE_PROBE; 1019 bioq_init(&softc->bio_queue); 1020 if (SID_IS_REMOVABLE(&cgd->inq_data)) 1021 softc->flags |= DA_FLAG_PACK_REMOVABLE; 1022 if ((cgd->inq_data.flags & SID_CmdQue) != 0) 1023 softc->flags |= DA_FLAG_TAGGED_QUEUING; 1024 1025 periph->softc = softc; 1026 1027 cam_extend_set(daperiphs, periph->unit_number, periph); 1028 1029 /* 1030 * See if this device has any quirks. 1031 */ 1032 match = cam_quirkmatch((caddr_t)&cgd->inq_data, 1033 (caddr_t)da_quirk_table, 1034 sizeof(da_quirk_table)/sizeof(*da_quirk_table), 1035 sizeof(*da_quirk_table), scsi_inquiry_match); 1036 1037 if (match != NULL) 1038 softc->quirks = ((struct da_quirk_entry *)match)->quirks; 1039 else 1040 softc->quirks = DA_Q_NONE; 1041 1042 /* 1043 * Unconditionally disable the synchronize cache command for 1044 * usb attachments. It's just impossible to determine if the 1045 * device supports it or not and if it doesn't the port can 1046 * brick. 1047 */ 1048 if (strncmp(periph->sim->sim_name, "umass", 4) == 0) { 1049 softc->quirks |= DA_Q_NO_SYNC_CACHE; 1050 } 1051 1052 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph); 1053 1054 /* Check if the SIM does not want 6 byte commands */ 1055 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1); 1056 cpi.ccb_h.func_code = XPT_PATH_INQ; 1057 xpt_action((union ccb *)&cpi); 1058 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE)) 1059 softc->quirks |= DA_Q_NO_6_BYTE; 1060 1061 /* 1062 * RBC devices don't have to support READ(6), only READ(10). 1063 */ 1064 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC) 1065 softc->minimum_cmd_size = 10; 1066 else 1067 softc->minimum_cmd_size = 6; 1068 1069 /* 1070 * Load the user's default, if any. 1071 */ 1072 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size", 1073 periph->unit_number); 1074 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size); 1075 1076 /* 1077 * 6, 10, 12, and 16 are the currently permissible values. 1078 */ 1079 if (softc->minimum_cmd_size < 6) 1080 softc->minimum_cmd_size = 6; 1081 else if ((softc->minimum_cmd_size > 6) 1082 && (softc->minimum_cmd_size <= 10)) 1083 softc->minimum_cmd_size = 10; 1084 else if ((softc->minimum_cmd_size > 10) 1085 && (softc->minimum_cmd_size <= 12)) 1086 softc->minimum_cmd_size = 12; 1087 else if (softc->minimum_cmd_size > 12) 1088 softc->minimum_cmd_size = 16; 1089 1090 /* 1091 * The DA driver supports a blocksize, but 1092 * we don't know the blocksize until we do 1093 * a read capacity. So, set a flag to 1094 * indicate that the blocksize is 1095 * unavailable right now. We'll clear the 1096 * flag as soon as we've done a read capacity. 1097 */ 1098 devstat_add_entry(&softc->device_stats, "da", 1099 periph->unit_number, 0, 1100 DEVSTAT_BS_UNAVAILABLE, 1101 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI, 1102 DEVSTAT_PRIORITY_DISK); 1103 1104 /* 1105 * Register this media as a disk 1106 */ 1107 CAM_SIM_UNLOCK(periph->sim); 1108 disk_create(periph->unit_number, &softc->disk, &da_ops); 1109 softc->disk.d_rawdev->si_iosize_max = MAXPHYS; 1110 CAM_SIM_LOCK(periph->sim); 1111 1112 /* 1113 * Add async callbacks for bus reset and 1114 * bus device reset calls. I don't bother 1115 * checking if this fails as, in most cases, 1116 * the system will function just fine without 1117 * them and the only alternative would be to 1118 * not attach the device on failure. 1119 */ 1120 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE, 1121 daasync, periph, periph->path); 1122 1123 /* 1124 * Take an exclusive refcount on the periph while dastart is called 1125 * to finish the probe. The reference will be dropped in dadone at 1126 * the end of probe. 1127 */ 1128 cam_periph_hold(periph, 0); 1129 xpt_schedule(periph, /*priority*/5); 1130 1131 /* 1132 * Schedule a periodic event to occasionally send an 1133 * ordered tag to a device. 1134 */ 1135 callout_init(&softc->sendordered_c); 1136 callout_reset(&softc->sendordered_c, 1137 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL, 1138 dasendorderedtag, softc); 1139 1140 return(CAM_REQ_CMP); 1141 } 1142 1143 static void 1144 dastart(struct cam_periph *periph, union ccb *start_ccb) 1145 { 1146 struct da_softc *softc; 1147 1148 softc = (struct da_softc *)periph->softc; 1149 1150 switch (softc->state) { 1151 case DA_STATE_NORMAL: 1152 { 1153 /* Pull a buffer from the queue and get going on it */ 1154 struct bio *bio; 1155 struct buf *bp; 1156 u_int8_t tag_code; 1157 1158 /* 1159 * See if there is a buf with work for us to do.. 1160 */ 1161 bio = bioq_first(&softc->bio_queue); 1162 if (periph->immediate_priority <= periph->pinfo.priority) { 1163 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE, 1164 ("queuing for immediate ccb\n")); 1165 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING; 1166 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 1167 periph_links.sle); 1168 periph->immediate_priority = CAM_PRIORITY_NONE; 1169 wakeup(&periph->ccb_list); 1170 if (bio != NULL) { 1171 /* 1172 * Have more work to do, so ensure we stay 1173 * scheduled 1174 */ 1175 xpt_schedule(periph, /* XXX priority */1); 1176 } 1177 break; 1178 } 1179 if (bio == NULL) { 1180 xpt_release_ccb(start_ccb); 1181 break; 1182 } 1183 1184 /* 1185 * We can queue new work. 1186 */ 1187 bioq_remove(&softc->bio_queue, bio); 1188 bp = bio->bio_buf; 1189 1190 devstat_start_transaction(&softc->device_stats); 1191 1192 if ((bp->b_flags & B_ORDERED) != 0 || 1193 (softc->flags & DA_FLAG_NEED_OTAG) != 0) { 1194 softc->flags &= ~DA_FLAG_NEED_OTAG; 1195 softc->ordered_tag_count++; 1196 tag_code = MSG_ORDERED_Q_TAG; 1197 } else { 1198 tag_code = MSG_SIMPLE_Q_TAG; 1199 } 1200 1201 switch(bp->b_cmd) { 1202 case BUF_CMD_READ: 1203 case BUF_CMD_WRITE: 1204 /* 1205 * Block read/write op 1206 */ 1207 KKASSERT(bio->bio_offset % softc->params.secsize == 0); 1208 1209 scsi_read_write( 1210 &start_ccb->csio, 1211 da_retry_count, /* retries */ 1212 dadone, 1213 tag_code, 1214 (bp->b_cmd == BUF_CMD_READ), 1215 0, /* byte2 */ 1216 softc->minimum_cmd_size, 1217 bio->bio_offset / softc->params.secsize, 1218 bp->b_bcount / softc->params.secsize, 1219 bp->b_data, 1220 bp->b_bcount, 1221 SSD_FULL_SIZE, /* sense_len */ 1222 da_default_timeout * 1000 1223 ); 1224 break; 1225 case BUF_CMD_FLUSH: 1226 /* 1227 * Silently complete a flush request if the device 1228 * cannot handle it. 1229 */ 1230 if (softc->quirks & DA_Q_NO_SYNC_CACHE) { 1231 xpt_release_ccb(start_ccb); 1232 start_ccb = NULL; 1233 devstat_end_transaction_buf( 1234 &softc->device_stats, bp); 1235 biodone(bio); 1236 } else { 1237 scsi_synchronize_cache( 1238 &start_ccb->csio, 1239 1, /* retries */ 1240 dadone, /* cbfcnp */ 1241 MSG_SIMPLE_Q_TAG, 1242 0, /* lba */ 1243 0, /* count (whole disk) */ 1244 SSD_FULL_SIZE, 1245 da_default_timeout*1000 /* timeout */ 1246 ); 1247 } 1248 break; 1249 default: 1250 xpt_release_ccb(start_ccb); 1251 start_ccb = NULL; 1252 panic("dastart: unrecognized bio cmd %d", bp->b_cmd); 1253 break; /* NOT REACHED */ 1254 } 1255 1256 /* 1257 * Block out any asyncronous callbacks 1258 * while we touch the pending ccb list. 1259 */ 1260 if (start_ccb) { 1261 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO; 1262 LIST_INSERT_HEAD(&softc->pending_ccbs, 1263 &start_ccb->ccb_h, periph_links.le); 1264 softc->outstanding_cmds++; 1265 1266 /* We expect a unit attention from this device */ 1267 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) { 1268 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA; 1269 softc->flags &= ~DA_FLAG_RETRY_UA; 1270 } 1271 1272 start_ccb->ccb_h.ccb_bio = bio; 1273 xpt_action(start_ccb); 1274 } 1275 1276 /* 1277 * Be sure we stay scheduled if we have more work to do. 1278 */ 1279 bio = bioq_first(&softc->bio_queue); 1280 if (bio != NULL) 1281 xpt_schedule(periph, 1); 1282 break; 1283 } 1284 case DA_STATE_PROBE: 1285 { 1286 struct ccb_scsiio *csio; 1287 struct scsi_read_capacity_data *rcap; 1288 1289 rcap = kmalloc(sizeof(*rcap), M_SCSIDA, M_INTWAIT | M_ZERO); 1290 csio = &start_ccb->csio; 1291 scsi_read_capacity(csio, 1292 /*retries*/4, 1293 dadone, 1294 MSG_SIMPLE_Q_TAG, 1295 rcap, 1296 SSD_FULL_SIZE, 1297 /*timeout*/5000); 1298 start_ccb->ccb_h.ccb_bio = NULL; 1299 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE; 1300 xpt_action(start_ccb); 1301 break; 1302 } 1303 case DA_STATE_PROBE2: 1304 { 1305 struct ccb_scsiio *csio; 1306 struct scsi_read_capacity_data_16 *rcaplong; 1307 1308 rcaplong = kmalloc(sizeof(*rcaplong), M_SCSIDA, 1309 M_INTWAIT | M_ZERO); 1310 if (rcaplong == NULL) { 1311 kprintf("dastart: Couldn't allocate read_capacity\n"); 1312 /* da_free_periph??? */ 1313 break; 1314 } 1315 csio = &start_ccb->csio; 1316 scsi_read_capacity_16(csio, 1317 /*retries*/ 4, 1318 /*cbfcnp*/ dadone, 1319 /*tag_action*/ MSG_SIMPLE_Q_TAG, 1320 /*lba*/ 0, 1321 /*reladr*/ 0, 1322 /*pmi*/ 0, 1323 rcaplong, 1324 /*sense_len*/ SSD_FULL_SIZE, 1325 /*timeout*/ 60000); 1326 start_ccb->ccb_h.ccb_bio = NULL; 1327 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2; 1328 xpt_action(start_ccb); 1329 break; 1330 } 1331 } 1332 } 1333 1334 static int 1335 cmd6workaround(union ccb *ccb) 1336 { 1337 struct scsi_rw_6 cmd6; 1338 struct scsi_rw_10 *cmd10; 1339 struct da_softc *softc; 1340 u_int8_t *cdb; 1341 int frozen; 1342 1343 cdb = ccb->csio.cdb_io.cdb_bytes; 1344 1345 /* Translation only possible if CDB is an array and cmd is R/W6 */ 1346 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 || 1347 (*cdb != READ_6 && *cdb != WRITE_6)) 1348 return 0; 1349 1350 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, " 1351 "increasing minimum_cmd_size to 10.\n"); 1352 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc; 1353 softc->minimum_cmd_size = 10; 1354 1355 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6)); 1356 cmd10 = (struct scsi_rw_10 *)cdb; 1357 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10; 1358 cmd10->byte2 = 0; 1359 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr); 1360 cmd10->reserved = 0; 1361 scsi_ulto2b(cmd6.length, cmd10->length); 1362 cmd10->control = cmd6.control; 1363 ccb->csio.cdb_len = sizeof(*cmd10); 1364 1365 /* Requeue request, unfreezing queue if necessary */ 1366 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 1367 ccb->ccb_h.status = CAM_REQUEUE_REQ; 1368 xpt_action(ccb); 1369 if (frozen) { 1370 cam_release_devq(ccb->ccb_h.path, 1371 /*relsim_flags*/0, 1372 /*reduction*/0, 1373 /*timeout*/0, 1374 /*getcount_only*/0); 1375 } 1376 return (ERESTART); 1377 } 1378 1379 static void 1380 dadone(struct cam_periph *periph, union ccb *done_ccb) 1381 { 1382 struct da_softc *softc; 1383 struct ccb_scsiio *csio; 1384 struct disk_info info; 1385 1386 softc = (struct da_softc *)periph->softc; 1387 csio = &done_ccb->csio; 1388 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) { 1389 case DA_CCB_BUFFER_IO: 1390 { 1391 struct buf *bp; 1392 struct bio *bio; 1393 1394 bio = (struct bio *)done_ccb->ccb_h.ccb_bio; 1395 bp = bio->bio_buf; 1396 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1397 int error; 1398 int sf; 1399 1400 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0) 1401 sf = SF_RETRY_UA; 1402 else 1403 sf = 0; 1404 1405 error = daerror(done_ccb, CAM_RETRY_SELTO, sf); 1406 if (error == ERESTART) { 1407 /* 1408 * A retry was scheuled, so 1409 * just return. 1410 */ 1411 return; 1412 } 1413 if (error != 0) { 1414 struct bio *q_bio; 1415 struct buf *q_bp; 1416 1417 if (error == ENXIO) { 1418 /* 1419 * Catastrophic error. Mark our pack as 1420 * invalid. 1421 */ 1422 /* 1423 * XXX See if this is really a media 1424 * XXX change first? 1425 */ 1426 xpt_print(periph->path, 1427 "Invalidating pack\n"); 1428 softc->flags |= DA_FLAG_PACK_INVALID; 1429 } 1430 1431 /* 1432 * return all queued I/O with EIO, so that 1433 * the client can retry these I/Os in the 1434 * proper order should it attempt to recover. 1435 */ 1436 while ((q_bio = bioq_first(&softc->bio_queue)) 1437 != NULL) { 1438 bioq_remove(&softc->bio_queue, q_bio); 1439 q_bp = q_bio->bio_buf; 1440 q_bp->b_resid = q_bp->b_bcount; 1441 q_bp->b_error = EIO; 1442 q_bp->b_flags |= B_ERROR; 1443 biodone(q_bio); 1444 } 1445 bp->b_error = error; 1446 bp->b_resid = bp->b_bcount; 1447 bp->b_flags |= B_ERROR; 1448 } else { 1449 bp->b_resid = csio->resid; 1450 bp->b_error = 0; 1451 if (bp->b_resid != 0) 1452 bp->b_flags |= B_ERROR; 1453 } 1454 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1455 cam_release_devq(done_ccb->ccb_h.path, 1456 /*relsim_flags*/0, 1457 /*reduction*/0, 1458 /*timeout*/0, 1459 /*getcount_only*/0); 1460 } else { 1461 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1462 panic("REQ_CMP with QFRZN"); 1463 bp->b_resid = csio->resid; 1464 if (csio->resid > 0) 1465 bp->b_flags |= B_ERROR; 1466 } 1467 1468 /* 1469 * Block out any asyncronous callbacks 1470 * while we touch the pending ccb list. 1471 */ 1472 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 1473 softc->outstanding_cmds--; 1474 if (softc->outstanding_cmds == 0) 1475 softc->flags |= DA_FLAG_WENT_IDLE; 1476 1477 devstat_end_transaction_buf(&softc->device_stats, bp); 1478 biodone(bio); 1479 break; 1480 } 1481 case DA_CCB_PROBE: 1482 case DA_CCB_PROBE2: 1483 { 1484 struct scsi_read_capacity_data *rdcap; 1485 struct scsi_read_capacity_data_16 *rcaplong; 1486 char announce_buf[80]; 1487 1488 rdcap = NULL; 1489 rcaplong = NULL; 1490 if (softc->state == DA_STATE_PROBE) 1491 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr; 1492 else 1493 rcaplong = (struct scsi_read_capacity_data_16 *) 1494 csio->data_ptr; 1495 1496 bzero(&info, sizeof(info)); 1497 info.d_type = DTYPE_SCSI; 1498 info.d_serialno = xpt_path_serialno(periph->path); 1499 1500 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1501 struct disk_params *dp; 1502 uint32_t block_size; 1503 uint64_t maxsector; 1504 1505 if (softc->state == DA_STATE_PROBE) { 1506 block_size = scsi_4btoul(rdcap->length); 1507 maxsector = scsi_4btoul(rdcap->addr); 1508 1509 /* 1510 * According to SBC-2, if the standard 10 1511 * byte READ CAPACITY command returns 2^32, 1512 * we should issue the 16 byte version of 1513 * the command, since the device in question 1514 * has more sectors than can be represented 1515 * with the short version of the command. 1516 */ 1517 if (maxsector == 0xffffffff) { 1518 softc->state = DA_STATE_PROBE2; 1519 kfree(rdcap, M_SCSIDA); 1520 xpt_release_ccb(done_ccb); 1521 xpt_schedule(periph, /*priority*/5); 1522 return; 1523 } 1524 } else { 1525 block_size = scsi_4btoul(rcaplong->length); 1526 maxsector = scsi_8btou64(rcaplong->addr); 1527 } 1528 dasetgeom(periph, block_size, maxsector); 1529 dp = &softc->params; 1530 ksnprintf(announce_buf, sizeof(announce_buf), 1531 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)", 1532 (uintmax_t) (((uintmax_t)dp->secsize * 1533 dp->sectors) / (1024*1024)), 1534 (uintmax_t)dp->sectors, 1535 dp->secsize, dp->heads, dp->secs_per_track, 1536 dp->cylinders); 1537 CAM_SIM_UNLOCK(periph->sim); 1538 info.d_media_blksize = softc->params.secsize; 1539 info.d_media_blocks = softc->params.sectors; 1540 info.d_media_size = 0; 1541 info.d_secpertrack = softc->params.secs_per_track; 1542 info.d_nheads = softc->params.heads; 1543 info.d_ncylinders = softc->params.cylinders; 1544 info.d_secpercyl = softc->params.heads * 1545 softc->params.secs_per_track; 1546 info.d_serialno = xpt_path_serialno(periph->path); 1547 disk_setdiskinfo(&softc->disk, &info); 1548 CAM_SIM_LOCK(periph->sim); 1549 } else { 1550 int error; 1551 1552 announce_buf[0] = '\0'; 1553 1554 /* 1555 * Retry any UNIT ATTENTION type errors. They 1556 * are expected at boot. 1557 */ 1558 error = daerror(done_ccb, CAM_RETRY_SELTO, 1559 SF_RETRY_UA|SF_NO_PRINT); 1560 if (error == ERESTART) { 1561 /* 1562 * A retry was scheuled, so 1563 * just return. 1564 */ 1565 return; 1566 } else if (error != 0) { 1567 struct scsi_sense_data *sense; 1568 int asc, ascq; 1569 int sense_key, error_code; 1570 int have_sense; 1571 cam_status status; 1572 struct ccb_getdev cgd; 1573 1574 /* Don't wedge this device's queue */ 1575 status = done_ccb->ccb_h.status; 1576 if ((status & CAM_DEV_QFRZN) != 0) 1577 cam_release_devq(done_ccb->ccb_h.path, 1578 /*relsim_flags*/0, 1579 /*reduction*/0, 1580 /*timeout*/0, 1581 /*getcount_only*/0); 1582 1583 1584 xpt_setup_ccb(&cgd.ccb_h, 1585 done_ccb->ccb_h.path, 1586 /* priority */ 1); 1587 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1588 xpt_action((union ccb *)&cgd); 1589 1590 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0) 1591 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0) 1592 || ((status & CAM_AUTOSNS_VALID) == 0)) 1593 have_sense = FALSE; 1594 else 1595 have_sense = TRUE; 1596 1597 if (have_sense) { 1598 sense = &csio->sense_data; 1599 scsi_extract_sense(sense, &error_code, 1600 &sense_key, 1601 &asc, &ascq); 1602 } 1603 /* 1604 * Attach to anything that claims to be a 1605 * direct access or optical disk device, 1606 * as long as it doesn't return a "Logical 1607 * unit not supported" (0x25) error. 1608 */ 1609 if ((have_sense) && (asc != 0x25) 1610 && (error_code == SSD_CURRENT_ERROR)) { 1611 const char *sense_key_desc; 1612 const char *asc_desc; 1613 1614 scsi_sense_desc(sense_key, asc, ascq, 1615 &cgd.inq_data, 1616 &sense_key_desc, 1617 &asc_desc); 1618 ksnprintf(announce_buf, 1619 sizeof(announce_buf), 1620 "Attempt to query device " 1621 "size failed: %s, %s", 1622 sense_key_desc, 1623 asc_desc); 1624 info.d_media_blksize = 512; 1625 disk_setdiskinfo(&softc->disk, &info); 1626 } else { 1627 if (have_sense) 1628 scsi_sense_print( 1629 &done_ccb->csio); 1630 else { 1631 xpt_print(periph->path, 1632 "got CAM status %#x\n", 1633 done_ccb->ccb_h.status); 1634 } 1635 1636 xpt_print(periph->path, "fatal error, " 1637 "failed to attach to device\n"); 1638 1639 /* 1640 * Free up resources. 1641 */ 1642 cam_periph_invalidate(periph); 1643 } 1644 } 1645 } 1646 kfree(csio->data_ptr, M_SCSIDA); 1647 if (announce_buf[0] != '\0') { 1648 xpt_announce_periph(periph, announce_buf); 1649 /* 1650 * Create our sysctl variables, now that we know 1651 * we have successfully attached. 1652 */ 1653 taskqueue_enqueue(taskqueue_thread[mycpuid], 1654 &softc->sysctl_task); 1655 } 1656 softc->state = DA_STATE_NORMAL; 1657 /* 1658 * Since our peripheral may be invalidated by an error 1659 * above or an external event, we must release our CCB 1660 * before releasing the probe lock on the peripheral. 1661 * The peripheral will only go away once the last lock 1662 * is removed, and we need it around for the CCB release 1663 * operation. 1664 */ 1665 xpt_release_ccb(done_ccb); 1666 cam_periph_unhold(periph, 0); 1667 return; 1668 } 1669 case DA_CCB_WAITING: 1670 { 1671 /* Caller will release the CCB */ 1672 wakeup(&done_ccb->ccb_h.cbfcnp); 1673 return; 1674 } 1675 case DA_CCB_DUMP: 1676 /* No-op. We're polling */ 1677 return; 1678 default: 1679 break; 1680 } 1681 xpt_release_ccb(done_ccb); 1682 } 1683 1684 static int 1685 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 1686 { 1687 struct da_softc *softc; 1688 struct cam_periph *periph; 1689 int error; 1690 1691 periph = xpt_path_periph(ccb->ccb_h.path); 1692 softc = (struct da_softc *)periph->softc; 1693 1694 /* 1695 * Automatically detect devices that do not support 1696 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs. 1697 */ 1698 error = 0; 1699 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) { 1700 error = cmd6workaround(ccb); 1701 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == 1702 CAM_SCSI_STATUS_ERROR) 1703 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) 1704 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) 1705 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0) 1706 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) { 1707 int sense_key, error_code, asc, ascq; 1708 1709 scsi_extract_sense(&ccb->csio.sense_data, 1710 &error_code, &sense_key, &asc, &ascq); 1711 if (sense_key == SSD_KEY_ILLEGAL_REQUEST) 1712 error = cmd6workaround(ccb); 1713 } 1714 if (error == ERESTART) 1715 return (ERESTART); 1716 1717 /* 1718 * XXX 1719 * Until we have a better way of doing pack validation, 1720 * don't treat UAs as errors. 1721 */ 1722 sense_flags |= SF_RETRY_UA; 1723 return(cam_periph_error(ccb, cam_flags, sense_flags, 1724 &softc->saved_ccb)); 1725 } 1726 1727 static void 1728 daprevent(struct cam_periph *periph, int action) 1729 { 1730 struct da_softc *softc; 1731 union ccb *ccb; 1732 int error; 1733 1734 softc = (struct da_softc *)periph->softc; 1735 1736 if (((action == PR_ALLOW) 1737 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0) 1738 || ((action == PR_PREVENT) 1739 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) { 1740 return; 1741 } 1742 1743 ccb = cam_periph_getccb(periph, /*priority*/1); 1744 1745 scsi_prevent(&ccb->csio, 1746 /*retries*/1, 1747 /*cbcfp*/dadone, 1748 MSG_SIMPLE_Q_TAG, 1749 action, 1750 SSD_FULL_SIZE, 1751 5000); 1752 1753 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO, 1754 SF_RETRY_UA, &softc->device_stats); 1755 1756 if (error == 0) { 1757 if (action == PR_ALLOW) 1758 softc->flags &= ~DA_FLAG_PACK_LOCKED; 1759 else 1760 softc->flags |= DA_FLAG_PACK_LOCKED; 1761 } 1762 1763 xpt_release_ccb(ccb); 1764 } 1765 1766 /* 1767 * Check media on open, e.g. card reader devices which had no initial media. 1768 */ 1769 static int 1770 dacheckmedia(struct cam_periph *periph) 1771 { 1772 struct disk_params *dp; 1773 struct da_softc *softc; 1774 struct disk_info info; 1775 int error; 1776 1777 softc = (struct da_softc *)periph->softc; 1778 dp = &softc->params; 1779 1780 error = dagetcapacity(periph); 1781 1782 /* 1783 * Only reprobe on initial open and if the media is removable. 1784 * 1785 * NOTE: If we setdiskinfo() it will take the device probe 1786 * a bit of time to probe the slices and partitions, 1787 * and mess up booting. So avoid if nothing has changed. 1788 * XXX 1789 */ 1790 if (softc->flags & DA_FLAG_OPEN) 1791 return (error); 1792 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) == 0) 1793 return (error); 1794 1795 bzero(&info, sizeof(info)); 1796 info.d_type = DTYPE_SCSI; 1797 info.d_serialno = xpt_path_serialno(periph->path); 1798 1799 if (error == 0) { 1800 CAM_SIM_UNLOCK(periph->sim); 1801 info.d_media_blksize = softc->params.secsize; 1802 info.d_media_blocks = softc->params.sectors; 1803 info.d_media_size = 0; 1804 info.d_secpertrack = softc->params.secs_per_track; 1805 info.d_nheads = softc->params.heads; 1806 info.d_ncylinders = softc->params.cylinders; 1807 info.d_secpercyl = softc->params.heads * 1808 softc->params.secs_per_track; 1809 info.d_serialno = xpt_path_serialno(periph->path); 1810 if (info.d_media_blocks != softc->disk.d_info.d_media_blocks) { 1811 kprintf("%s%d: open removable media: " 1812 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)\n", 1813 periph->periph_name, periph->unit_number, 1814 (uintmax_t)(((uintmax_t)dp->secsize * 1815 dp->sectors) / (1024*1024)), 1816 (uintmax_t)dp->sectors, dp->secsize, 1817 dp->heads, dp->secs_per_track, dp->cylinders); 1818 disk_setdiskinfo(&softc->disk, &info); 1819 } 1820 CAM_SIM_LOCK(periph->sim); 1821 } else { 1822 kprintf("%s%d: open removable media: no media present\n", 1823 periph->periph_name, periph->unit_number); 1824 info.d_media_blksize = 512; 1825 disk_setdiskinfo(&softc->disk, &info); 1826 } 1827 return (error); 1828 } 1829 1830 static int 1831 dagetcapacity(struct cam_periph *periph) 1832 { 1833 struct da_softc *softc; 1834 union ccb *ccb; 1835 struct scsi_read_capacity_data *rcap; 1836 struct scsi_read_capacity_data_16 *rcaplong; 1837 uint32_t block_len; 1838 uint64_t maxsector; 1839 int error; 1840 1841 softc = (struct da_softc *)periph->softc; 1842 block_len = 0; 1843 maxsector = 0; 1844 error = 0; 1845 1846 /* Do a read capacity */ 1847 rcap = (struct scsi_read_capacity_data *)kmalloc(sizeof(*rcaplong), 1848 M_SCSIDA, M_INTWAIT); 1849 1850 ccb = cam_periph_getccb(periph, /*priority*/1); 1851 scsi_read_capacity(&ccb->csio, 1852 /*retries*/4, 1853 /*cbfncp*/dadone, 1854 MSG_SIMPLE_Q_TAG, 1855 rcap, 1856 SSD_FULL_SIZE, 1857 /*timeout*/60000); 1858 ccb->ccb_h.ccb_bio = NULL; 1859 1860 error = cam_periph_runccb(ccb, daerror, 1861 /*cam_flags*/CAM_RETRY_SELTO, 1862 /*sense_flags*/SF_RETRY_UA, 1863 &softc->device_stats); 1864 1865 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1866 cam_release_devq(ccb->ccb_h.path, 1867 /*relsim_flags*/0, 1868 /*reduction*/0, 1869 /*timeout*/0, 1870 /*getcount_only*/0); 1871 1872 if (error == 0) { 1873 block_len = scsi_4btoul(rcap->length); 1874 maxsector = scsi_4btoul(rcap->addr); 1875 1876 if (maxsector != 0xffffffff) 1877 goto done; 1878 } else 1879 goto done; 1880 1881 rcaplong = (struct scsi_read_capacity_data_16 *)rcap; 1882 1883 scsi_read_capacity_16(&ccb->csio, 1884 /*retries*/ 4, 1885 /*cbfcnp*/ dadone, 1886 /*tag_action*/ MSG_SIMPLE_Q_TAG, 1887 /*lba*/ 0, 1888 /*reladr*/ 0, 1889 /*pmi*/ 0, 1890 rcaplong, 1891 /*sense_len*/ SSD_FULL_SIZE, 1892 /*timeout*/ 60000); 1893 ccb->ccb_h.ccb_bio = NULL; 1894 1895 error = cam_periph_runccb(ccb, daerror, 1896 /*cam_flags*/CAM_RETRY_SELTO, 1897 /*sense_flags*/SF_RETRY_UA, 1898 &softc->device_stats); 1899 1900 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1901 cam_release_devq(ccb->ccb_h.path, 1902 /*relsim_flags*/0, 1903 /*reduction*/0, 1904 /*timeout*/0, 1905 /*getcount_only*/0); 1906 1907 if (error == 0) { 1908 block_len = scsi_4btoul(rcaplong->length); 1909 maxsector = scsi_8btou64(rcaplong->addr); 1910 } 1911 1912 done: 1913 1914 if (error == 0) 1915 dasetgeom(periph, block_len, maxsector); 1916 1917 xpt_release_ccb(ccb); 1918 1919 kfree(rcap, M_SCSIDA); 1920 1921 return (error); 1922 } 1923 1924 static void 1925 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector) 1926 { 1927 struct ccb_calc_geometry ccg; 1928 struct da_softc *softc; 1929 struct disk_params *dp; 1930 1931 softc = (struct da_softc *)periph->softc; 1932 1933 dp = &softc->params; 1934 dp->secsize = block_len; 1935 dp->sectors = maxsector + 1; 1936 /* 1937 * Have the controller provide us with a geometry 1938 * for this disk. The only time the geometry 1939 * matters is when we boot and the controller 1940 * is the only one knowledgeable enough to come 1941 * up with something that will make this a bootable 1942 * device. 1943 */ 1944 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1); 1945 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY; 1946 ccg.block_size = dp->secsize; 1947 ccg.volume_size = dp->sectors; 1948 ccg.heads = 0; 1949 ccg.secs_per_track = 0; 1950 ccg.cylinders = 0; 1951 xpt_action((union ccb*)&ccg); 1952 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1953 /* 1954 * We don't know what went wrong here- but just pick 1955 * a geometry so we don't have nasty things like divide 1956 * by zero. 1957 */ 1958 dp->heads = 255; 1959 dp->secs_per_track = 255; 1960 dp->cylinders = dp->sectors / (255 * 255); 1961 if (dp->cylinders == 0) { 1962 dp->cylinders = 1; 1963 } 1964 } else { 1965 dp->heads = ccg.heads; 1966 dp->secs_per_track = ccg.secs_per_track; 1967 dp->cylinders = ccg.cylinders; 1968 } 1969 } 1970 1971 static void 1972 dasendorderedtag(void *arg) 1973 { 1974 struct da_softc *softc = arg; 1975 1976 if (da_send_ordered) { 1977 if ((softc->ordered_tag_count == 0) 1978 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) { 1979 softc->flags |= DA_FLAG_NEED_OTAG; 1980 } 1981 if (softc->outstanding_cmds > 0) 1982 softc->flags &= ~DA_FLAG_WENT_IDLE; 1983 1984 softc->ordered_tag_count = 0; 1985 } 1986 /* Queue us up again */ 1987 callout_reset(&softc->sendordered_c, 1988 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL, 1989 dasendorderedtag, softc); 1990 } 1991 1992 /* 1993 * Step through all DA peripheral drivers, and if the device is still open, 1994 * sync the disk cache to physical media. 1995 */ 1996 static void 1997 dashutdown(void * arg, int howto) 1998 { 1999 struct cam_periph *periph; 2000 struct da_softc *softc; 2001 2002 TAILQ_FOREACH(periph, &dadriver.units, unit_links) { 2003 union ccb ccb; 2004 2005 cam_periph_lock(periph); 2006 softc = (struct da_softc *)periph->softc; 2007 2008 /* 2009 * We only sync the cache if the drive is still open, and 2010 * if the drive is capable of it.. 2011 */ 2012 if (((softc->flags & DA_FLAG_OPEN) == 0) 2013 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) { 2014 cam_periph_unlock(periph); 2015 continue; 2016 } 2017 2018 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1); 2019 2020 ccb.ccb_h.ccb_state = DA_CCB_DUMP; 2021 scsi_synchronize_cache(&ccb.csio, 2022 /*retries*/1, 2023 /*cbfcnp*/dadone, 2024 MSG_SIMPLE_Q_TAG, 2025 /*begin_lba*/0, /* whole disk */ 2026 /*lb_count*/0, 2027 SSD_FULL_SIZE, 2028 60 * 60 * 1000); 2029 2030 xpt_polled_action(&ccb); 2031 2032 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2033 if (((ccb.ccb_h.status & CAM_STATUS_MASK) == 2034 CAM_SCSI_STATUS_ERROR) 2035 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){ 2036 int error_code, sense_key, asc, ascq; 2037 2038 scsi_extract_sense(&ccb.csio.sense_data, 2039 &error_code, &sense_key, 2040 &asc, &ascq); 2041 2042 if (sense_key != SSD_KEY_ILLEGAL_REQUEST) 2043 scsi_sense_print(&ccb.csio); 2044 } else { 2045 xpt_print(periph->path, "Synchronize " 2046 "cache failed, status == 0x%x, scsi status " 2047 "== 0x%x\n", ccb.ccb_h.status, 2048 ccb.csio.scsi_status); 2049 } 2050 } 2051 2052 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 2053 cam_release_devq(ccb.ccb_h.path, 2054 /*relsim_flags*/0, 2055 /*reduction*/0, 2056 /*timeout*/0, 2057 /*getcount_only*/0); 2058 2059 cam_periph_unlock(periph); 2060 } 2061 } 2062 2063 #else /* !_KERNEL */ 2064 2065 /* 2066 * XXX This is only left out of the kernel build to silence warnings. If, 2067 * for some reason this function is used in the kernel, the ifdefs should 2068 * be moved so it is included both in the kernel and userland. 2069 */ 2070 void 2071 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries, 2072 void (*cbfcnp)(struct cam_periph *, union ccb *), 2073 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave, 2074 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, 2075 u_int32_t timeout) 2076 { 2077 struct scsi_format_unit *scsi_cmd; 2078 2079 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes; 2080 scsi_cmd->opcode = FORMAT_UNIT; 2081 scsi_cmd->byte2 = byte2; 2082 scsi_ulto2b(ileave, scsi_cmd->interleave); 2083 2084 cam_fill_csio(csio, 2085 retries, 2086 cbfcnp, 2087 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE, 2088 tag_action, 2089 data_ptr, 2090 dxfer_len, 2091 sense_len, 2092 sizeof(*scsi_cmd), 2093 timeout); 2094 } 2095 2096 #endif /* _KERNEL */ 2097