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