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