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