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