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