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