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 static int 552 daopen(struct disk *dp) 553 { 554 struct cam_periph *periph; 555 struct da_softc *softc; 556 int unit; 557 int error; 558 559 periph = (struct cam_periph *)dp->d_drv1; 560 if (periph == NULL) { 561 return (ENXIO); 562 } 563 564 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 565 return(ENXIO); 566 } 567 568 cam_periph_lock(periph); 569 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) { 570 cam_periph_unlock(periph); 571 cam_periph_release(periph); 572 return (error); 573 } 574 575 unit = periph->unit_number; 576 softc = (struct da_softc *)periph->softc; 577 softc->flags |= DA_FLAG_OPEN; 578 579 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 580 ("daopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit, 581 unit)); 582 583 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) { 584 /* Invalidate our pack information. */ 585 softc->flags &= ~DA_FLAG_PACK_INVALID; 586 } 587 588 error = dagetcapacity(periph); 589 590 if (error == 0) { 591 592 softc->disk->d_sectorsize = softc->params.secsize; 593 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors; 594 /* XXX: these are not actually "firmware" values, so they may be wrong */ 595 softc->disk->d_fwsectors = softc->params.secs_per_track; 596 softc->disk->d_fwheads = softc->params.heads; 597 softc->disk->d_devstat->block_size = softc->params.secsize; 598 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE; 599 } 600 601 if (error == 0) { 602 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 && 603 (softc->quirks & DA_Q_NO_PREVENT) == 0) 604 daprevent(periph, PR_PREVENT); 605 } else { 606 softc->flags &= ~DA_FLAG_OPEN; 607 cam_periph_release(periph); 608 } 609 cam_periph_unhold(periph); 610 cam_periph_unlock(periph); 611 return (error); 612 } 613 614 static int 615 daclose(struct disk *dp) 616 { 617 struct cam_periph *periph; 618 struct da_softc *softc; 619 int error; 620 621 periph = (struct cam_periph *)dp->d_drv1; 622 if (periph == NULL) 623 return (ENXIO); 624 625 cam_periph_lock(periph); 626 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) { 627 cam_periph_unlock(periph); 628 cam_periph_release(periph); 629 return (error); 630 } 631 632 softc = (struct da_softc *)periph->softc; 633 634 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) { 635 union ccb *ccb; 636 637 ccb = cam_periph_getccb(periph, /*priority*/1); 638 639 scsi_synchronize_cache(&ccb->csio, 640 /*retries*/1, 641 /*cbfcnp*/dadone, 642 MSG_SIMPLE_Q_TAG, 643 /*begin_lba*/0,/* Cover the whole disk */ 644 /*lb_count*/0, 645 SSD_FULL_SIZE, 646 5 * 60 * 1000); 647 648 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0, 649 /*sense_flags*/SF_RETRY_UA, 650 softc->disk->d_devstat); 651 652 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 653 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 654 CAM_SCSI_STATUS_ERROR) { 655 int asc, ascq; 656 int sense_key, error_code; 657 658 scsi_extract_sense(&ccb->csio.sense_data, 659 &error_code, 660 &sense_key, 661 &asc, &ascq); 662 if (sense_key != SSD_KEY_ILLEGAL_REQUEST) 663 scsi_sense_print(&ccb->csio); 664 } else { 665 xpt_print(periph->path, "Synchronize cache " 666 "failed, status == 0x%x, scsi status == " 667 "0x%x\n", ccb->csio.ccb_h.status, 668 ccb->csio.scsi_status); 669 } 670 } 671 672 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 673 cam_release_devq(ccb->ccb_h.path, 674 /*relsim_flags*/0, 675 /*reduction*/0, 676 /*timeout*/0, 677 /*getcount_only*/0); 678 679 xpt_release_ccb(ccb); 680 681 } 682 683 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) { 684 if ((softc->quirks & DA_Q_NO_PREVENT) == 0) 685 daprevent(periph, PR_ALLOW); 686 /* 687 * If we've got removeable media, mark the blocksize as 688 * unavailable, since it could change when new media is 689 * inserted. 690 */ 691 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE; 692 } 693 694 softc->flags &= ~DA_FLAG_OPEN; 695 cam_periph_unhold(periph); 696 cam_periph_unlock(periph); 697 cam_periph_release(periph); 698 return (0); 699 } 700 701 /* 702 * Actually translate the requested transfer into one the physical driver 703 * can understand. The transfer is described by a buf and will include 704 * only one physical transfer. 705 */ 706 static void 707 dastrategy(struct bio *bp) 708 { 709 struct cam_periph *periph; 710 struct da_softc *softc; 711 712 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 713 if (periph == NULL) { 714 biofinish(bp, NULL, ENXIO); 715 return; 716 } 717 softc = (struct da_softc *)periph->softc; 718 719 cam_periph_lock(periph); 720 721 #if 0 722 /* 723 * check it's not too big a transfer for our adapter 724 */ 725 scsi_minphys(bp,&sd_switch); 726 #endif 727 728 /* 729 * Mask interrupts so that the pack cannot be invalidated until 730 * after we are in the queue. Otherwise, we might not properly 731 * clean up one of the buffers. 732 */ 733 734 /* 735 * If the device has been made invalid, error out 736 */ 737 if ((softc->flags & DA_FLAG_PACK_INVALID)) { 738 cam_periph_unlock(periph); 739 biofinish(bp, NULL, ENXIO); 740 return; 741 } 742 743 /* 744 * Place it in the queue of disk activities for this disk 745 */ 746 bioq_disksort(&softc->bio_queue, bp); 747 748 /* 749 * Schedule ourselves for performing the work. 750 */ 751 xpt_schedule(periph, /* XXX priority */1); 752 cam_periph_unlock(periph); 753 754 return; 755 } 756 757 static int 758 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) 759 { 760 struct cam_periph *periph; 761 struct da_softc *softc; 762 u_int secsize; 763 struct ccb_scsiio csio; 764 struct disk *dp; 765 766 dp = arg; 767 periph = dp->d_drv1; 768 if (periph == NULL) 769 return (ENXIO); 770 softc = (struct da_softc *)periph->softc; 771 cam_periph_lock(periph); 772 secsize = softc->params.secsize; 773 774 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) { 775 cam_periph_unlock(periph); 776 return (ENXIO); 777 } 778 779 if (length > 0) { 780 periph->flags |= CAM_PERIPH_POLLED; 781 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1); 782 csio.ccb_h.ccb_state = DA_CCB_DUMP; 783 scsi_read_write(&csio, 784 /*retries*/1, 785 dadone, 786 MSG_ORDERED_Q_TAG, 787 /*read*/FALSE, 788 /*byte2*/0, 789 /*minimum_cmd_size*/ softc->minimum_cmd_size, 790 offset / secsize, 791 length / secsize, 792 /*data_ptr*/(u_int8_t *) virtual, 793 /*dxfer_len*/length, 794 /*sense_len*/SSD_FULL_SIZE, 795 DA_DEFAULT_TIMEOUT * 1000); 796 xpt_polled_action((union ccb *)&csio); 797 798 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 799 printf("Aborting dump due to I/O error.\n"); 800 if ((csio.ccb_h.status & CAM_STATUS_MASK) == 801 CAM_SCSI_STATUS_ERROR) 802 scsi_sense_print(&csio); 803 else 804 printf("status == 0x%x, scsi status == 0x%x\n", 805 csio.ccb_h.status, csio.scsi_status); 806 periph->flags |= CAM_PERIPH_POLLED; 807 return(EIO); 808 } 809 cam_periph_unlock(periph); 810 return(0); 811 } 812 813 /* 814 * Sync the disk cache contents to the physical media. 815 */ 816 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) { 817 818 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1); 819 csio.ccb_h.ccb_state = DA_CCB_DUMP; 820 scsi_synchronize_cache(&csio, 821 /*retries*/1, 822 /*cbfcnp*/dadone, 823 MSG_SIMPLE_Q_TAG, 824 /*begin_lba*/0,/* Cover the whole disk */ 825 /*lb_count*/0, 826 SSD_FULL_SIZE, 827 5 * 60 * 1000); 828 xpt_polled_action((union ccb *)&csio); 829 830 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 831 if ((csio.ccb_h.status & CAM_STATUS_MASK) == 832 CAM_SCSI_STATUS_ERROR) { 833 int asc, ascq; 834 int sense_key, error_code; 835 836 scsi_extract_sense(&csio.sense_data, 837 &error_code, 838 &sense_key, 839 &asc, &ascq); 840 if (sense_key != SSD_KEY_ILLEGAL_REQUEST) 841 scsi_sense_print(&csio); 842 } else { 843 xpt_print(periph->path, "Synchronize cache " 844 "failed, status == 0x%x, scsi status == " 845 "0x%x\n", csio.ccb_h.status, 846 csio.scsi_status); 847 } 848 } 849 } 850 periph->flags &= ~CAM_PERIPH_POLLED; 851 cam_periph_unlock(periph); 852 return (0); 853 } 854 855 static void 856 dainit(void) 857 { 858 cam_status status; 859 struct cam_path *path; 860 861 /* 862 * Install a global async callback. This callback will 863 * receive async callbacks like "new device found". 864 */ 865 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID, 866 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 867 868 if (status == CAM_REQ_CMP) { 869 struct ccb_setasync csa; 870 871 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5); 872 csa.ccb_h.func_code = XPT_SASYNC_CB; 873 csa.event_enable = AC_FOUND_DEVICE; 874 csa.callback = daasync; 875 csa.callback_arg = NULL; 876 xpt_action((union ccb *)&csa); 877 status = csa.ccb_h.status; 878 xpt_free_path(path); 879 } 880 881 if (status != CAM_REQ_CMP) { 882 printf("da: Failed to attach master async callback " 883 "due to status 0x%x!\n", status); 884 } else if (da_send_ordered) { 885 886 /* Register our shutdown event handler */ 887 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown, 888 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 889 printf("dainit: shutdown event registration failed!\n"); 890 } 891 } 892 893 static void 894 daoninvalidate(struct cam_periph *periph) 895 { 896 struct da_softc *softc; 897 struct ccb_setasync csa; 898 899 softc = (struct da_softc *)periph->softc; 900 901 /* 902 * De-register any async callbacks. 903 */ 904 xpt_setup_ccb(&csa.ccb_h, periph->path, 905 /* priority */ 5); 906 csa.ccb_h.func_code = XPT_SASYNC_CB; 907 csa.event_enable = 0; 908 csa.callback = daasync; 909 csa.callback_arg = periph; 910 xpt_action((union ccb *)&csa); 911 912 softc->flags |= DA_FLAG_PACK_INVALID; 913 914 /* 915 * Return all queued I/O with ENXIO. 916 * XXX Handle any transactions queued to the card 917 * with XPT_ABORT_CCB. 918 */ 919 bioq_flush(&softc->bio_queue, NULL, ENXIO); 920 921 disk_gone(softc->disk); 922 xpt_print(periph->path, "lost device\n"); 923 } 924 925 static void 926 dacleanup(struct cam_periph *periph) 927 { 928 struct da_softc *softc; 929 930 softc = (struct da_softc *)periph->softc; 931 932 xpt_print(periph->path, "removing device entry\n"); 933 /* 934 * If we can't free the sysctl tree, oh well... 935 */ 936 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0 937 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) { 938 xpt_print(periph->path, "can't remove sysctl context\n"); 939 } 940 941 cam_periph_unlock(periph); 942 disk_destroy(softc->disk); 943 callout_drain(&softc->sendordered_c); 944 cam_periph_lock(periph); 945 free(softc, M_DEVBUF); 946 } 947 948 static void 949 daasync(void *callback_arg, u_int32_t code, 950 struct cam_path *path, void *arg) 951 { 952 struct cam_periph *periph; 953 954 periph = (struct cam_periph *)callback_arg; 955 switch (code) { 956 case AC_FOUND_DEVICE: 957 { 958 struct ccb_getdev *cgd; 959 struct cam_sim *sim; 960 cam_status status; 961 962 cgd = (struct ccb_getdev *)arg; 963 if (cgd == NULL) 964 break; 965 966 if (SID_TYPE(&cgd->inq_data) != T_DIRECT 967 && SID_TYPE(&cgd->inq_data) != T_RBC 968 && SID_TYPE(&cgd->inq_data) != T_OPTICAL) 969 break; 970 971 /* 972 * Allocate a peripheral instance for 973 * this device and start the probe 974 * process. 975 */ 976 sim = xpt_path_sim(cgd->ccb_h.path); 977 status = cam_periph_alloc(daregister, daoninvalidate, 978 dacleanup, dastart, 979 "da", CAM_PERIPH_BIO, 980 cgd->ccb_h.path, daasync, 981 AC_FOUND_DEVICE, cgd); 982 983 if (status != CAM_REQ_CMP 984 && status != CAM_REQ_INPROG) 985 printf("daasync: Unable to attach to new device " 986 "due to status 0x%x\n", status); 987 break; 988 } 989 case AC_SENT_BDR: 990 case AC_BUS_RESET: 991 { 992 struct da_softc *softc; 993 struct ccb_hdr *ccbh; 994 995 softc = (struct da_softc *)periph->softc; 996 /* 997 * Don't fail on the expected unit attention 998 * that will occur. 999 */ 1000 softc->flags |= DA_FLAG_RETRY_UA; 1001 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le) 1002 ccbh->ccb_state |= DA_CCB_RETRY_UA; 1003 /* FALLTHROUGH*/ 1004 } 1005 default: 1006 cam_periph_async(periph, code, path, arg); 1007 break; 1008 } 1009 } 1010 1011 static void 1012 dasysctlinit(void *context, int pending) 1013 { 1014 struct cam_periph *periph; 1015 struct da_softc *softc; 1016 char tmpstr[80], tmpstr2[80]; 1017 1018 periph = (struct cam_periph *)context; 1019 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 1020 return; 1021 1022 softc = (struct da_softc *)periph->softc; 1023 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number); 1024 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 1025 1026 mtx_lock(&Giant); 1027 sysctl_ctx_init(&softc->sysctl_ctx); 1028 softc->flags |= DA_FLAG_SCTX_INIT; 1029 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1030 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2, 1031 CTLFLAG_RD, 0, tmpstr); 1032 if (softc->sysctl_tree == NULL) { 1033 printf("dasysctlinit: unable to allocate sysctl tree\n"); 1034 mtx_unlock(&Giant); 1035 cam_periph_release(periph); 1036 return; 1037 } 1038 1039 /* 1040 * Now register the sysctl handler, so the user can the value on 1041 * the fly. 1042 */ 1043 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1044 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW, 1045 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I", 1046 "Minimum CDB size"); 1047 1048 mtx_unlock(&Giant); 1049 cam_periph_release(periph); 1050 } 1051 1052 static int 1053 dacmdsizesysctl(SYSCTL_HANDLER_ARGS) 1054 { 1055 int error, value; 1056 1057 value = *(int *)arg1; 1058 1059 error = sysctl_handle_int(oidp, &value, 0, req); 1060 1061 if ((error != 0) 1062 || (req->newptr == NULL)) 1063 return (error); 1064 1065 /* 1066 * Acceptable values here are 6, 10, 12 or 16. 1067 */ 1068 if (value < 6) 1069 value = 6; 1070 else if ((value > 6) 1071 && (value <= 10)) 1072 value = 10; 1073 else if ((value > 10) 1074 && (value <= 12)) 1075 value = 12; 1076 else if (value > 12) 1077 value = 16; 1078 1079 *(int *)arg1 = value; 1080 1081 return (0); 1082 } 1083 1084 static cam_status 1085 daregister(struct cam_periph *periph, void *arg) 1086 { 1087 struct da_softc *softc; 1088 struct ccb_setasync csa; 1089 struct ccb_pathinq cpi; 1090 struct ccb_getdev *cgd; 1091 char tmpstr[80]; 1092 caddr_t match; 1093 1094 cgd = (struct ccb_getdev *)arg; 1095 if (periph == NULL) { 1096 printf("daregister: periph was NULL!!\n"); 1097 return(CAM_REQ_CMP_ERR); 1098 } 1099 1100 if (cgd == NULL) { 1101 printf("daregister: no getdev CCB, can't register device\n"); 1102 return(CAM_REQ_CMP_ERR); 1103 } 1104 1105 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF, 1106 M_NOWAIT|M_ZERO); 1107 1108 if (softc == NULL) { 1109 printf("daregister: Unable to probe new device. " 1110 "Unable to allocate softc\n"); 1111 return(CAM_REQ_CMP_ERR); 1112 } 1113 1114 LIST_INIT(&softc->pending_ccbs); 1115 softc->state = DA_STATE_PROBE; 1116 bioq_init(&softc->bio_queue); 1117 if (SID_IS_REMOVABLE(&cgd->inq_data)) 1118 softc->flags |= DA_FLAG_PACK_REMOVABLE; 1119 if ((cgd->inq_data.flags & SID_CmdQue) != 0) 1120 softc->flags |= DA_FLAG_TAGGED_QUEUING; 1121 1122 periph->softc = softc; 1123 1124 /* 1125 * See if this device has any quirks. 1126 */ 1127 match = cam_quirkmatch((caddr_t)&cgd->inq_data, 1128 (caddr_t)da_quirk_table, 1129 sizeof(da_quirk_table)/sizeof(*da_quirk_table), 1130 sizeof(*da_quirk_table), scsi_inquiry_match); 1131 1132 if (match != NULL) 1133 softc->quirks = ((struct da_quirk_entry *)match)->quirks; 1134 else 1135 softc->quirks = DA_Q_NONE; 1136 1137 /* Check if the SIM does not want 6 byte commands */ 1138 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1); 1139 cpi.ccb_h.func_code = XPT_PATH_INQ; 1140 xpt_action((union ccb *)&cpi); 1141 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE)) 1142 softc->quirks |= DA_Q_NO_6_BYTE; 1143 1144 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph); 1145 1146 /* 1147 * RBC devices don't have to support READ(6), only READ(10). 1148 */ 1149 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC) 1150 softc->minimum_cmd_size = 10; 1151 else 1152 softc->minimum_cmd_size = 6; 1153 1154 /* 1155 * Load the user's default, if any. 1156 */ 1157 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size", 1158 periph->unit_number); 1159 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size); 1160 1161 /* 1162 * 6, 10, 12 and 16 are the currently permissible values. 1163 */ 1164 if (softc->minimum_cmd_size < 6) 1165 softc->minimum_cmd_size = 6; 1166 else if ((softc->minimum_cmd_size > 6) 1167 && (softc->minimum_cmd_size <= 10)) 1168 softc->minimum_cmd_size = 10; 1169 else if ((softc->minimum_cmd_size > 10) 1170 && (softc->minimum_cmd_size <= 12)) 1171 softc->minimum_cmd_size = 12; 1172 else if (softc->minimum_cmd_size > 12) 1173 softc->minimum_cmd_size = 16; 1174 1175 /* 1176 * Register this media as a disk 1177 */ 1178 1179 mtx_unlock(periph->sim->mtx); 1180 softc->disk = disk_alloc(); 1181 softc->disk->d_open = daopen; 1182 softc->disk->d_close = daclose; 1183 softc->disk->d_strategy = dastrategy; 1184 softc->disk->d_dump = dadump; 1185 softc->disk->d_name = "da"; 1186 softc->disk->d_drv1 = periph; 1187 softc->disk->d_maxsize = DFLTPHYS; /* XXX: probably not arbitrary */ 1188 softc->disk->d_unit = periph->unit_number; 1189 softc->disk->d_flags = 0; 1190 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) 1191 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE; 1192 disk_create(softc->disk, DISK_VERSION); 1193 mtx_lock(periph->sim->mtx); 1194 1195 /* 1196 * Add async callbacks for bus reset and 1197 * bus device reset calls. I don't bother 1198 * checking if this fails as, in most cases, 1199 * the system will function just fine without 1200 * them and the only alternative would be to 1201 * not attach the device on failure. 1202 */ 1203 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5); 1204 csa.ccb_h.func_code = XPT_SASYNC_CB; 1205 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE; 1206 csa.callback = daasync; 1207 csa.callback_arg = periph; 1208 xpt_action((union ccb *)&csa); 1209 1210 /* 1211 * Take an exclusive refcount on the periph while dastart is called 1212 * to finish the probe. The reference will be dropped in dadone at 1213 * the end of probe. 1214 */ 1215 (void)cam_periph_hold(periph, PRIBIO); 1216 xpt_schedule(periph, /*priority*/5); 1217 1218 /* 1219 * Schedule a periodic event to occasionally send an 1220 * ordered tag to a device. 1221 */ 1222 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0); 1223 callout_reset(&softc->sendordered_c, 1224 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL, 1225 dasendorderedtag, softc); 1226 1227 return(CAM_REQ_CMP); 1228 } 1229 1230 static void 1231 dastart(struct cam_periph *periph, union ccb *start_ccb) 1232 { 1233 struct da_softc *softc; 1234 1235 softc = (struct da_softc *)periph->softc; 1236 1237 switch (softc->state) { 1238 case DA_STATE_NORMAL: 1239 { 1240 /* Pull a buffer from the queue and get going on it */ 1241 struct bio *bp; 1242 1243 /* 1244 * See if there is a buf with work for us to do.. 1245 */ 1246 bp = bioq_first(&softc->bio_queue); 1247 if (periph->immediate_priority <= periph->pinfo.priority) { 1248 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE, 1249 ("queuing for immediate ccb\n")); 1250 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING; 1251 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 1252 periph_links.sle); 1253 periph->immediate_priority = CAM_PRIORITY_NONE; 1254 wakeup(&periph->ccb_list); 1255 } else if (bp == NULL) { 1256 xpt_release_ccb(start_ccb); 1257 } else { 1258 u_int8_t tag_code; 1259 1260 bioq_remove(&softc->bio_queue, bp); 1261 1262 if ((softc->flags & DA_FLAG_NEED_OTAG) != 0) { 1263 softc->flags &= ~DA_FLAG_NEED_OTAG; 1264 softc->ordered_tag_count++; 1265 tag_code = MSG_ORDERED_Q_TAG; 1266 } else { 1267 tag_code = MSG_SIMPLE_Q_TAG; 1268 } 1269 switch (bp->bio_cmd) { 1270 case BIO_READ: 1271 case BIO_WRITE: 1272 scsi_read_write(&start_ccb->csio, 1273 /*retries*/da_retry_count, 1274 /*cbfcnp*/dadone, 1275 /*tag_action*/tag_code, 1276 /*read_op*/bp->bio_cmd == BIO_READ, 1277 /*byte2*/0, 1278 softc->minimum_cmd_size, 1279 /*lba*/bp->bio_pblkno, 1280 /*block_count*/bp->bio_bcount / 1281 softc->params.secsize, 1282 /*data_ptr*/ bp->bio_data, 1283 /*dxfer_len*/ bp->bio_bcount, 1284 /*sense_len*/SSD_FULL_SIZE, 1285 /*timeout*/da_default_timeout*1000); 1286 break; 1287 case BIO_FLUSH: 1288 scsi_synchronize_cache(&start_ccb->csio, 1289 /*retries*/1, 1290 /*cbfcnp*/dadone, 1291 MSG_SIMPLE_Q_TAG, 1292 /*begin_lba*/0,/* Cover the whole disk */ 1293 /*lb_count*/0, 1294 SSD_FULL_SIZE, 1295 /*timeout*/da_default_timeout*1000); 1296 break; 1297 } 1298 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO; 1299 1300 /* 1301 * Block out any asyncronous callbacks 1302 * while we touch the pending ccb list. 1303 */ 1304 LIST_INSERT_HEAD(&softc->pending_ccbs, 1305 &start_ccb->ccb_h, periph_links.le); 1306 softc->outstanding_cmds++; 1307 1308 /* We expect a unit attention from this device */ 1309 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) { 1310 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA; 1311 softc->flags &= ~DA_FLAG_RETRY_UA; 1312 } 1313 1314 start_ccb->ccb_h.ccb_bp = bp; 1315 bp = bioq_first(&softc->bio_queue); 1316 1317 xpt_action(start_ccb); 1318 } 1319 1320 if (bp != NULL) { 1321 /* Have more work to do, so ensure we stay scheduled */ 1322 xpt_schedule(periph, /* XXX priority */1); 1323 } 1324 break; 1325 } 1326 case DA_STATE_PROBE: 1327 { 1328 struct ccb_scsiio *csio; 1329 struct scsi_read_capacity_data *rcap; 1330 1331 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap), 1332 M_TEMP, 1333 M_NOWAIT); 1334 if (rcap == NULL) { 1335 printf("dastart: Couldn't malloc read_capacity data\n"); 1336 /* da_free_periph??? */ 1337 break; 1338 } 1339 csio = &start_ccb->csio; 1340 scsi_read_capacity(csio, 1341 /*retries*/4, 1342 dadone, 1343 MSG_SIMPLE_Q_TAG, 1344 rcap, 1345 SSD_FULL_SIZE, 1346 /*timeout*/5000); 1347 start_ccb->ccb_h.ccb_bp = NULL; 1348 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE; 1349 xpt_action(start_ccb); 1350 break; 1351 } 1352 case DA_STATE_PROBE2: 1353 { 1354 struct ccb_scsiio *csio; 1355 struct scsi_read_capacity_data_long *rcaplong; 1356 1357 rcaplong = (struct scsi_read_capacity_data_long *) 1358 malloc(sizeof(*rcaplong), M_TEMP, M_NOWAIT); 1359 if (rcaplong == NULL) { 1360 printf("dastart: Couldn't malloc read_capacity data\n"); 1361 /* da_free_periph??? */ 1362 break; 1363 } 1364 csio = &start_ccb->csio; 1365 scsi_read_capacity_16(csio, 1366 /*retries*/ 4, 1367 /*cbfcnp*/ dadone, 1368 /*tag_action*/ MSG_SIMPLE_Q_TAG, 1369 /*lba*/ 0, 1370 /*reladr*/ 0, 1371 /*pmi*/ 0, 1372 rcaplong, 1373 /*sense_len*/ SSD_FULL_SIZE, 1374 /*timeout*/ 60000); 1375 start_ccb->ccb_h.ccb_bp = NULL; 1376 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2; 1377 xpt_action(start_ccb); 1378 break; 1379 } 1380 } 1381 } 1382 1383 static int 1384 cmd6workaround(union ccb *ccb) 1385 { 1386 struct scsi_rw_6 cmd6; 1387 struct scsi_rw_10 *cmd10; 1388 struct da_softc *softc; 1389 u_int8_t *cdb; 1390 int frozen; 1391 1392 cdb = ccb->csio.cdb_io.cdb_bytes; 1393 1394 /* Translation only possible if CDB is an array and cmd is R/W6 */ 1395 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 || 1396 (*cdb != READ_6 && *cdb != WRITE_6)) 1397 return 0; 1398 1399 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, " 1400 "increasing minimum_cmd_size to 10.\n"); 1401 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc; 1402 softc->minimum_cmd_size = 10; 1403 1404 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6)); 1405 cmd10 = (struct scsi_rw_10 *)cdb; 1406 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10; 1407 cmd10->byte2 = 0; 1408 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr); 1409 cmd10->reserved = 0; 1410 scsi_ulto2b(cmd6.length, cmd10->length); 1411 cmd10->control = cmd6.control; 1412 ccb->csio.cdb_len = sizeof(*cmd10); 1413 1414 /* Requeue request, unfreezing queue if necessary */ 1415 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 1416 ccb->ccb_h.status = CAM_REQUEUE_REQ; 1417 xpt_action(ccb); 1418 if (frozen) { 1419 cam_release_devq(ccb->ccb_h.path, 1420 /*relsim_flags*/0, 1421 /*reduction*/0, 1422 /*timeout*/0, 1423 /*getcount_only*/0); 1424 } 1425 return (ERESTART); 1426 } 1427 1428 static void 1429 dadone(struct cam_periph *periph, union ccb *done_ccb) 1430 { 1431 struct da_softc *softc; 1432 struct ccb_scsiio *csio; 1433 1434 softc = (struct da_softc *)periph->softc; 1435 csio = &done_ccb->csio; 1436 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) { 1437 case DA_CCB_BUFFER_IO: 1438 { 1439 struct bio *bp; 1440 1441 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 1442 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1443 int error; 1444 int sf; 1445 1446 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0) 1447 sf = SF_RETRY_UA; 1448 else 1449 sf = 0; 1450 1451 error = daerror(done_ccb, CAM_RETRY_SELTO, sf); 1452 if (error == ERESTART) { 1453 /* 1454 * A retry was scheuled, so 1455 * just return. 1456 */ 1457 return; 1458 } 1459 if (error != 0) { 1460 1461 if (error == ENXIO) { 1462 /* 1463 * Catastrophic error. Mark our pack as 1464 * invalid. 1465 */ 1466 /* 1467 * XXX See if this is really a media 1468 * XXX change first? 1469 */ 1470 xpt_print(periph->path, 1471 "Invalidating pack\n"); 1472 softc->flags |= DA_FLAG_PACK_INVALID; 1473 } 1474 1475 /* 1476 * return all queued I/O with EIO, so that 1477 * the client can retry these I/Os in the 1478 * proper order should it attempt to recover. 1479 */ 1480 bioq_flush(&softc->bio_queue, NULL, EIO); 1481 bp->bio_error = error; 1482 bp->bio_resid = bp->bio_bcount; 1483 bp->bio_flags |= BIO_ERROR; 1484 } else { 1485 bp->bio_resid = csio->resid; 1486 bp->bio_error = 0; 1487 if (bp->bio_resid != 0) 1488 bp->bio_flags |= BIO_ERROR; 1489 } 1490 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1491 cam_release_devq(done_ccb->ccb_h.path, 1492 /*relsim_flags*/0, 1493 /*reduction*/0, 1494 /*timeout*/0, 1495 /*getcount_only*/0); 1496 } else { 1497 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1498 panic("REQ_CMP with QFRZN"); 1499 bp->bio_resid = csio->resid; 1500 if (csio->resid > 0) 1501 bp->bio_flags |= BIO_ERROR; 1502 } 1503 1504 /* 1505 * Block out any asyncronous callbacks 1506 * while we touch the pending ccb list. 1507 */ 1508 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 1509 softc->outstanding_cmds--; 1510 if (softc->outstanding_cmds == 0) 1511 softc->flags |= DA_FLAG_WENT_IDLE; 1512 1513 biodone(bp); 1514 break; 1515 } 1516 case DA_CCB_PROBE: 1517 case DA_CCB_PROBE2: 1518 { 1519 struct scsi_read_capacity_data *rdcap; 1520 struct scsi_read_capacity_data_long *rcaplong; 1521 char announce_buf[80]; 1522 1523 rdcap = NULL; 1524 rcaplong = NULL; 1525 if (softc->state == DA_STATE_PROBE) 1526 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr; 1527 else 1528 rcaplong = (struct scsi_read_capacity_data_long *) 1529 csio->data_ptr; 1530 1531 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1532 struct disk_params *dp; 1533 uint32_t block_size; 1534 uint64_t maxsector; 1535 1536 if (softc->state == DA_STATE_PROBE) { 1537 block_size = scsi_4btoul(rdcap->length); 1538 maxsector = scsi_4btoul(rdcap->addr); 1539 1540 /* 1541 * According to SBC-2, if the standard 10 1542 * byte READ CAPACITY command returns 2^32, 1543 * we should issue the 16 byte version of 1544 * the command, since the device in question 1545 * has more sectors than can be represented 1546 * with the short version of the command. 1547 */ 1548 if (maxsector == 0xffffffff) { 1549 softc->state = DA_STATE_PROBE2; 1550 free(rdcap, M_TEMP); 1551 xpt_release_ccb(done_ccb); 1552 xpt_schedule(periph, /*priority*/5); 1553 return; 1554 } 1555 } else { 1556 block_size = scsi_4btoul(rcaplong->length); 1557 maxsector = scsi_8btou64(rcaplong->addr); 1558 } 1559 1560 /* 1561 * Because GEOM code just will panic us if we 1562 * give them an 'illegal' value we'll avoid that 1563 * here. 1564 */ 1565 if (block_size >= MAXPHYS || block_size == 0) { 1566 xpt_print(periph->path, 1567 "unsupportable block size %ju\n", 1568 (uintmax_t) block_size); 1569 announce_buf[0] = '\0'; 1570 cam_periph_invalidate(periph); 1571 } else { 1572 dasetgeom(periph, block_size, maxsector); 1573 dp = &softc->params; 1574 snprintf(announce_buf, sizeof(announce_buf), 1575 "%juMB (%ju %u byte sectors: %dH %dS/T " 1576 "%dC)", (uintmax_t) 1577 (((uintmax_t)dp->secsize * 1578 dp->sectors) / (1024*1024)), 1579 (uintmax_t)dp->sectors, 1580 dp->secsize, dp->heads, 1581 dp->secs_per_track, dp->cylinders); 1582 } 1583 } else { 1584 int error; 1585 1586 announce_buf[0] = '\0'; 1587 1588 /* 1589 * Retry any UNIT ATTENTION type errors. They 1590 * are expected at boot. 1591 */ 1592 error = daerror(done_ccb, CAM_RETRY_SELTO, 1593 SF_RETRY_UA|SF_NO_PRINT); 1594 if (error == ERESTART) { 1595 /* 1596 * A retry was scheuled, so 1597 * just return. 1598 */ 1599 return; 1600 } else if (error != 0) { 1601 struct scsi_sense_data *sense; 1602 int asc, ascq; 1603 int sense_key, error_code; 1604 int have_sense; 1605 cam_status status; 1606 struct ccb_getdev cgd; 1607 1608 /* Don't wedge this device's queue */ 1609 status = done_ccb->ccb_h.status; 1610 if ((status & CAM_DEV_QFRZN) != 0) 1611 cam_release_devq(done_ccb->ccb_h.path, 1612 /*relsim_flags*/0, 1613 /*reduction*/0, 1614 /*timeout*/0, 1615 /*getcount_only*/0); 1616 1617 1618 xpt_setup_ccb(&cgd.ccb_h, 1619 done_ccb->ccb_h.path, 1620 /* priority */ 1); 1621 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1622 xpt_action((union ccb *)&cgd); 1623 1624 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0) 1625 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0) 1626 || ((status & CAM_AUTOSNS_VALID) == 0)) 1627 have_sense = FALSE; 1628 else 1629 have_sense = TRUE; 1630 1631 if (have_sense) { 1632 sense = &csio->sense_data; 1633 scsi_extract_sense(sense, &error_code, 1634 &sense_key, 1635 &asc, &ascq); 1636 } 1637 /* 1638 * Attach to anything that claims to be a 1639 * direct access or optical disk device, 1640 * as long as it doesn't return a "Logical 1641 * unit not supported" (0x25) error. 1642 */ 1643 if ((have_sense) && (asc != 0x25) 1644 && (error_code == SSD_CURRENT_ERROR)) { 1645 const char *sense_key_desc; 1646 const char *asc_desc; 1647 1648 scsi_sense_desc(sense_key, asc, ascq, 1649 &cgd.inq_data, 1650 &sense_key_desc, 1651 &asc_desc); 1652 snprintf(announce_buf, 1653 sizeof(announce_buf), 1654 "Attempt to query device " 1655 "size failed: %s, %s", 1656 sense_key_desc, 1657 asc_desc); 1658 } else { 1659 if (have_sense) 1660 scsi_sense_print( 1661 &done_ccb->csio); 1662 else { 1663 xpt_print(periph->path, 1664 "got CAM status %#x\n", 1665 done_ccb->ccb_h.status); 1666 } 1667 1668 xpt_print(periph->path, "fatal error, " 1669 "failed to attach to device\n"); 1670 1671 /* 1672 * Free up resources. 1673 */ 1674 cam_periph_invalidate(periph); 1675 } 1676 } 1677 } 1678 free(csio->data_ptr, M_TEMP); 1679 if (announce_buf[0] != '\0') { 1680 xpt_announce_periph(periph, announce_buf); 1681 /* 1682 * Create our sysctl variables, now that we know 1683 * we have successfully attached. 1684 */ 1685 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task); 1686 } 1687 softc->state = DA_STATE_NORMAL; 1688 /* 1689 * Since our peripheral may be invalidated by an error 1690 * above or an external event, we must release our CCB 1691 * before releasing the probe lock on the peripheral. 1692 * The peripheral will only go away once the last lock 1693 * is removed, and we need it around for the CCB release 1694 * operation. 1695 */ 1696 xpt_release_ccb(done_ccb); 1697 cam_periph_unhold(periph); 1698 return; 1699 } 1700 case DA_CCB_WAITING: 1701 { 1702 /* Caller will release the CCB */ 1703 wakeup(&done_ccb->ccb_h.cbfcnp); 1704 return; 1705 } 1706 case DA_CCB_DUMP: 1707 /* No-op. We're polling */ 1708 return; 1709 default: 1710 break; 1711 } 1712 xpt_release_ccb(done_ccb); 1713 } 1714 1715 static int 1716 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 1717 { 1718 struct da_softc *softc; 1719 struct cam_periph *periph; 1720 int error; 1721 1722 periph = xpt_path_periph(ccb->ccb_h.path); 1723 softc = (struct da_softc *)periph->softc; 1724 1725 /* 1726 * Automatically detect devices that do not support 1727 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs. 1728 */ 1729 error = 0; 1730 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) { 1731 error = cmd6workaround(ccb); 1732 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == 1733 CAM_SCSI_STATUS_ERROR) 1734 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) 1735 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) 1736 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0) 1737 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) { 1738 int sense_key, error_code, asc, ascq; 1739 1740 scsi_extract_sense(&ccb->csio.sense_data, 1741 &error_code, &sense_key, &asc, &ascq); 1742 if (sense_key == SSD_KEY_ILLEGAL_REQUEST) 1743 error = cmd6workaround(ccb); 1744 } 1745 if (error == ERESTART) 1746 return (ERESTART); 1747 1748 /* 1749 * XXX 1750 * Until we have a better way of doing pack validation, 1751 * don't treat UAs as errors. 1752 */ 1753 sense_flags |= SF_RETRY_UA; 1754 return(cam_periph_error(ccb, cam_flags, sense_flags, 1755 &softc->saved_ccb)); 1756 } 1757 1758 static void 1759 daprevent(struct cam_periph *periph, int action) 1760 { 1761 struct da_softc *softc; 1762 union ccb *ccb; 1763 int error; 1764 1765 softc = (struct da_softc *)periph->softc; 1766 1767 if (((action == PR_ALLOW) 1768 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0) 1769 || ((action == PR_PREVENT) 1770 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) { 1771 return; 1772 } 1773 1774 ccb = cam_periph_getccb(periph, /*priority*/1); 1775 1776 scsi_prevent(&ccb->csio, 1777 /*retries*/1, 1778 /*cbcfp*/dadone, 1779 MSG_SIMPLE_Q_TAG, 1780 action, 1781 SSD_FULL_SIZE, 1782 5000); 1783 1784 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO, 1785 SF_RETRY_UA, softc->disk->d_devstat); 1786 1787 if (error == 0) { 1788 if (action == PR_ALLOW) 1789 softc->flags &= ~DA_FLAG_PACK_LOCKED; 1790 else 1791 softc->flags |= DA_FLAG_PACK_LOCKED; 1792 } 1793 1794 xpt_release_ccb(ccb); 1795 } 1796 1797 static int 1798 dagetcapacity(struct cam_periph *periph) 1799 { 1800 struct da_softc *softc; 1801 union ccb *ccb; 1802 struct scsi_read_capacity_data *rcap; 1803 struct scsi_read_capacity_data_long *rcaplong; 1804 uint32_t block_len; 1805 uint64_t maxsector; 1806 int error; 1807 u_int32_t sense_flags; 1808 1809 softc = (struct da_softc *)periph->softc; 1810 block_len = 0; 1811 maxsector = 0; 1812 error = 0; 1813 sense_flags = SF_RETRY_UA; 1814 if (softc->flags & DA_FLAG_PACK_REMOVABLE) 1815 sense_flags |= SF_NO_PRINT; 1816 1817 /* Do a read capacity */ 1818 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong), 1819 M_TEMP, 1820 M_NOWAIT); 1821 if (rcap == NULL) 1822 return (ENOMEM); 1823 1824 ccb = cam_periph_getccb(periph, /*priority*/1); 1825 scsi_read_capacity(&ccb->csio, 1826 /*retries*/4, 1827 /*cbfncp*/dadone, 1828 MSG_SIMPLE_Q_TAG, 1829 rcap, 1830 SSD_FULL_SIZE, 1831 /*timeout*/60000); 1832 ccb->ccb_h.ccb_bp = NULL; 1833 1834 error = cam_periph_runccb(ccb, daerror, 1835 /*cam_flags*/CAM_RETRY_SELTO, 1836 sense_flags, 1837 softc->disk->d_devstat); 1838 1839 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1840 cam_release_devq(ccb->ccb_h.path, 1841 /*relsim_flags*/0, 1842 /*reduction*/0, 1843 /*timeout*/0, 1844 /*getcount_only*/0); 1845 1846 if (error == 0) { 1847 block_len = scsi_4btoul(rcap->length); 1848 maxsector = scsi_4btoul(rcap->addr); 1849 1850 if (maxsector != 0xffffffff) 1851 goto done; 1852 } else 1853 goto done; 1854 1855 rcaplong = (struct scsi_read_capacity_data_long *)rcap; 1856 1857 scsi_read_capacity_16(&ccb->csio, 1858 /*retries*/ 4, 1859 /*cbfcnp*/ dadone, 1860 /*tag_action*/ MSG_SIMPLE_Q_TAG, 1861 /*lba*/ 0, 1862 /*reladr*/ 0, 1863 /*pmi*/ 0, 1864 rcaplong, 1865 /*sense_len*/ SSD_FULL_SIZE, 1866 /*timeout*/ 60000); 1867 ccb->ccb_h.ccb_bp = NULL; 1868 1869 error = cam_periph_runccb(ccb, daerror, 1870 /*cam_flags*/CAM_RETRY_SELTO, 1871 sense_flags, 1872 softc->disk->d_devstat); 1873 1874 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1875 cam_release_devq(ccb->ccb_h.path, 1876 /*relsim_flags*/0, 1877 /*reduction*/0, 1878 /*timeout*/0, 1879 /*getcount_only*/0); 1880 1881 if (error == 0) { 1882 block_len = scsi_4btoul(rcaplong->length); 1883 maxsector = scsi_8btou64(rcaplong->addr); 1884 } 1885 1886 done: 1887 1888 if (error == 0) 1889 dasetgeom(periph, block_len, maxsector); 1890 1891 xpt_release_ccb(ccb); 1892 1893 free(rcap, M_TEMP); 1894 1895 return (error); 1896 } 1897 1898 static void 1899 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector) 1900 { 1901 struct ccb_calc_geometry ccg; 1902 struct da_softc *softc; 1903 struct disk_params *dp; 1904 1905 softc = (struct da_softc *)periph->softc; 1906 1907 dp = &softc->params; 1908 dp->secsize = block_len; 1909 dp->sectors = maxsector + 1; 1910 /* 1911 * Have the controller provide us with a geometry 1912 * for this disk. The only time the geometry 1913 * matters is when we boot and the controller 1914 * is the only one knowledgeable enough to come 1915 * up with something that will make this a bootable 1916 * device. 1917 */ 1918 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1); 1919 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY; 1920 ccg.block_size = dp->secsize; 1921 ccg.volume_size = dp->sectors; 1922 ccg.heads = 0; 1923 ccg.secs_per_track = 0; 1924 ccg.cylinders = 0; 1925 xpt_action((union ccb*)&ccg); 1926 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1927 /* 1928 * We don't know what went wrong here- but just pick 1929 * a geometry so we don't have nasty things like divide 1930 * by zero. 1931 */ 1932 dp->heads = 255; 1933 dp->secs_per_track = 255; 1934 dp->cylinders = dp->sectors / (255 * 255); 1935 if (dp->cylinders == 0) { 1936 dp->cylinders = 1; 1937 } 1938 } else { 1939 dp->heads = ccg.heads; 1940 dp->secs_per_track = ccg.secs_per_track; 1941 dp->cylinders = ccg.cylinders; 1942 } 1943 } 1944 1945 static void 1946 dasendorderedtag(void *arg) 1947 { 1948 struct da_softc *softc = arg; 1949 1950 if (da_send_ordered) { 1951 if ((softc->ordered_tag_count == 0) 1952 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) { 1953 softc->flags |= DA_FLAG_NEED_OTAG; 1954 } 1955 if (softc->outstanding_cmds > 0) 1956 softc->flags &= ~DA_FLAG_WENT_IDLE; 1957 1958 softc->ordered_tag_count = 0; 1959 } 1960 /* Queue us up again */ 1961 callout_reset(&softc->sendordered_c, 1962 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL, 1963 dasendorderedtag, softc); 1964 } 1965 1966 /* 1967 * Step through all DA peripheral drivers, and if the device is still open, 1968 * sync the disk cache to physical media. 1969 */ 1970 static void 1971 dashutdown(void * arg, int howto) 1972 { 1973 struct cam_periph *periph; 1974 struct da_softc *softc; 1975 1976 TAILQ_FOREACH(periph, &dadriver.units, unit_links) { 1977 union ccb ccb; 1978 1979 cam_periph_lock(periph); 1980 softc = (struct da_softc *)periph->softc; 1981 1982 /* 1983 * We only sync the cache if the drive is still open, and 1984 * if the drive is capable of it.. 1985 */ 1986 if (((softc->flags & DA_FLAG_OPEN) == 0) 1987 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) { 1988 cam_periph_unlock(periph); 1989 continue; 1990 } 1991 1992 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1); 1993 1994 ccb.ccb_h.ccb_state = DA_CCB_DUMP; 1995 scsi_synchronize_cache(&ccb.csio, 1996 /*retries*/1, 1997 /*cbfcnp*/dadone, 1998 MSG_SIMPLE_Q_TAG, 1999 /*begin_lba*/0, /* whole disk */ 2000 /*lb_count*/0, 2001 SSD_FULL_SIZE, 2002 60 * 60 * 1000); 2003 2004 xpt_polled_action(&ccb); 2005 2006 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2007 if (((ccb.ccb_h.status & CAM_STATUS_MASK) == 2008 CAM_SCSI_STATUS_ERROR) 2009 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){ 2010 int error_code, sense_key, asc, ascq; 2011 2012 scsi_extract_sense(&ccb.csio.sense_data, 2013 &error_code, &sense_key, 2014 &asc, &ascq); 2015 2016 if (sense_key != SSD_KEY_ILLEGAL_REQUEST) 2017 scsi_sense_print(&ccb.csio); 2018 } else { 2019 xpt_print(periph->path, "Synchronize " 2020 "cache failed, status == 0x%x, scsi status " 2021 "== 0x%x\n", ccb.ccb_h.status, 2022 ccb.csio.scsi_status); 2023 } 2024 } 2025 2026 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 2027 cam_release_devq(ccb.ccb_h.path, 2028 /*relsim_flags*/0, 2029 /*reduction*/0, 2030 /*timeout*/0, 2031 /*getcount_only*/0); 2032 cam_periph_unlock(periph); 2033 } 2034 } 2035 2036 #else /* !_KERNEL */ 2037 2038 /* 2039 * XXX This is only left out of the kernel build to silence warnings. If, 2040 * for some reason this function is used in the kernel, the ifdefs should 2041 * be moved so it is included both in the kernel and userland. 2042 */ 2043 void 2044 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries, 2045 void (*cbfcnp)(struct cam_periph *, union ccb *), 2046 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave, 2047 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, 2048 u_int32_t timeout) 2049 { 2050 struct scsi_format_unit *scsi_cmd; 2051 2052 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes; 2053 scsi_cmd->opcode = FORMAT_UNIT; 2054 scsi_cmd->byte2 = byte2; 2055 scsi_ulto2b(ileave, scsi_cmd->interleave); 2056 2057 cam_fill_csio(csio, 2058 retries, 2059 cbfcnp, 2060 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE, 2061 tag_action, 2062 data_ptr, 2063 dxfer_len, 2064 sense_len, 2065 sizeof(*scsi_cmd), 2066 timeout); 2067 } 2068 2069 #endif /* _KERNEL */ 2070