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