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.h> 48 #include <geom/geom_disk.h> 49 #endif /* _KERNEL */ 50 51 #ifndef _KERNEL 52 #include <stdio.h> 53 #include <string.h> 54 #endif /* _KERNEL */ 55 56 #include <cam/cam.h> 57 #include <cam/cam_ccb.h> 58 #include <cam/cam_periph.h> 59 #include <cam/cam_xpt_periph.h> 60 #include <cam/cam_sim.h> 61 62 #include <cam/scsi/scsi_message.h> 63 64 #ifndef _KERNEL 65 #include <cam/scsi/scsi_da.h> 66 #endif /* !_KERNEL */ 67 68 #ifdef _KERNEL 69 typedef enum { 70 DA_STATE_PROBE, 71 DA_STATE_PROBE2, 72 DA_STATE_NORMAL 73 } da_state; 74 75 typedef enum { 76 DA_FLAG_PACK_INVALID = 0x001, 77 DA_FLAG_NEW_PACK = 0x002, 78 DA_FLAG_PACK_LOCKED = 0x004, 79 DA_FLAG_PACK_REMOVABLE = 0x008, 80 DA_FLAG_SAW_MEDIA = 0x010, 81 DA_FLAG_NEED_OTAG = 0x020, 82 DA_FLAG_WENT_IDLE = 0x040, 83 DA_FLAG_RETRY_UA = 0x080, 84 DA_FLAG_OPEN = 0x100, 85 DA_FLAG_SCTX_INIT = 0x200, 86 DA_FLAG_CAN_RC16 = 0x400, 87 DA_FLAG_PROBED = 0x800 88 } da_flags; 89 90 typedef enum { 91 DA_Q_NONE = 0x00, 92 DA_Q_NO_SYNC_CACHE = 0x01, 93 DA_Q_NO_6_BYTE = 0x02, 94 DA_Q_NO_PREVENT = 0x04, 95 DA_Q_4K = 0x08 96 } da_quirks; 97 98 typedef enum { 99 DA_CCB_PROBE = 0x01, 100 DA_CCB_PROBE2 = 0x02, 101 DA_CCB_BUFFER_IO = 0x03, 102 DA_CCB_WAITING = 0x04, 103 DA_CCB_DUMP = 0x05, 104 DA_CCB_DELETE = 0x06, 105 DA_CCB_TUR = 0x07, 106 DA_CCB_TYPE_MASK = 0x0F, 107 DA_CCB_RETRY_UA = 0x10 108 } da_ccb_state; 109 110 typedef enum { 111 DA_DELETE_NONE, 112 DA_DELETE_DISABLE, 113 DA_DELETE_ZERO, 114 DA_DELETE_WS10, 115 DA_DELETE_WS16, 116 DA_DELETE_UNMAP, 117 DA_DELETE_MAX = DA_DELETE_UNMAP 118 } da_delete_methods; 119 120 static const char *da_delete_method_names[] = 121 { "NONE", "DISABLE", "ZERO", "WS10", "WS16", "UNMAP" }; 122 123 /* Offsets into our private area for storing information */ 124 #define ccb_state ppriv_field0 125 #define ccb_bp ppriv_ptr1 126 127 struct disk_params { 128 u_int8_t heads; 129 u_int32_t cylinders; 130 u_int8_t secs_per_track; 131 u_int32_t secsize; /* Number of bytes/sector */ 132 u_int64_t sectors; /* total number sectors */ 133 u_int stripesize; 134 u_int stripeoffset; 135 }; 136 137 #define UNMAP_MAX_RANGES 512 138 139 struct da_softc { 140 struct bio_queue_head bio_queue; 141 struct bio_queue_head delete_queue; 142 struct bio_queue_head delete_run_queue; 143 SLIST_ENTRY(da_softc) links; 144 LIST_HEAD(, ccb_hdr) pending_ccbs; 145 da_state state; 146 da_flags flags; 147 da_quirks quirks; 148 int minimum_cmd_size; 149 int error_inject; 150 int ordered_tag_count; 151 int outstanding_cmds; 152 int unmap_max_ranges; 153 int unmap_max_lba; 154 int delete_running; 155 int tur; 156 da_delete_methods delete_method; 157 struct disk_params params; 158 struct disk *disk; 159 union ccb saved_ccb; 160 struct task sysctl_task; 161 struct sysctl_ctx_list sysctl_ctx; 162 struct sysctl_oid *sysctl_tree; 163 struct callout sendordered_c; 164 uint64_t wwpn; 165 uint8_t unmap_buf[UNMAP_MAX_RANGES * 16 + 8]; 166 struct scsi_read_capacity_data_long rcaplong; 167 struct callout mediapoll_c; 168 }; 169 170 struct da_quirk_entry { 171 struct scsi_inquiry_pattern inq_pat; 172 da_quirks quirks; 173 }; 174 175 static const char quantum[] = "QUANTUM"; 176 static const char microp[] = "MICROP"; 177 178 static struct da_quirk_entry da_quirk_table[] = 179 { 180 /* SPI, FC devices */ 181 { 182 /* 183 * Fujitsu M2513A MO drives. 184 * Tested devices: M2513A2 firmware versions 1200 & 1300. 185 * (dip switch selects whether T_DIRECT or T_OPTICAL device) 186 * Reported by: W.Scholten <whs@xs4all.nl> 187 */ 188 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"}, 189 /*quirks*/ DA_Q_NO_SYNC_CACHE 190 }, 191 { 192 /* See above. */ 193 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"}, 194 /*quirks*/ DA_Q_NO_SYNC_CACHE 195 }, 196 { 197 /* 198 * This particular Fujitsu drive doesn't like the 199 * synchronize cache command. 200 * Reported by: Tom Jackson <toj@gorilla.net> 201 */ 202 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"}, 203 /*quirks*/ DA_Q_NO_SYNC_CACHE 204 }, 205 { 206 /* 207 * This drive doesn't like the synchronize cache command 208 * either. Reported by: Matthew Jacob <mjacob@feral.com> 209 * in NetBSD PR kern/6027, August 24, 1998. 210 */ 211 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"}, 212 /*quirks*/ DA_Q_NO_SYNC_CACHE 213 }, 214 { 215 /* 216 * This drive doesn't like the synchronize cache command 217 * either. Reported by: Hellmuth Michaelis (hm@kts.org) 218 * (PR 8882). 219 */ 220 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"}, 221 /*quirks*/ DA_Q_NO_SYNC_CACHE 222 }, 223 { 224 /* 225 * Doesn't like the synchronize cache command. 226 * Reported by: Blaz Zupan <blaz@gold.amis.net> 227 */ 228 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"}, 229 /*quirks*/ DA_Q_NO_SYNC_CACHE 230 }, 231 { 232 /* 233 * Doesn't like the synchronize cache command. 234 * Reported by: Blaz Zupan <blaz@gold.amis.net> 235 */ 236 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"}, 237 /*quirks*/ DA_Q_NO_SYNC_CACHE 238 }, 239 { 240 /* 241 * Doesn't like the synchronize cache command. 242 */ 243 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"}, 244 /*quirks*/ DA_Q_NO_SYNC_CACHE 245 }, 246 { 247 /* 248 * Doesn't like the synchronize cache command. 249 * Reported by: walter@pelissero.de 250 */ 251 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"}, 252 /*quirks*/ DA_Q_NO_SYNC_CACHE 253 }, 254 { 255 /* 256 * Doesn't work correctly with 6 byte reads/writes. 257 * Returns illegal request, and points to byte 9 of the 258 * 6-byte CDB. 259 * Reported by: Adam McDougall <bsdx@spawnet.com> 260 */ 261 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"}, 262 /*quirks*/ DA_Q_NO_6_BYTE 263 }, 264 { 265 /* See above. */ 266 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"}, 267 /*quirks*/ DA_Q_NO_6_BYTE 268 }, 269 { 270 /* 271 * Doesn't like the synchronize cache command. 272 * Reported by: walter@pelissero.de 273 */ 274 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"}, 275 /*quirks*/ DA_Q_NO_SYNC_CACHE 276 }, 277 { 278 /* 279 * The CISS RAID controllers do not support SYNC_CACHE 280 */ 281 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"}, 282 /*quirks*/ DA_Q_NO_SYNC_CACHE 283 }, 284 /* USB mass storage devices supported by umass(4) */ 285 { 286 /* 287 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player 288 * PR: kern/51675 289 */ 290 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"}, 291 /*quirks*/ DA_Q_NO_SYNC_CACHE 292 }, 293 { 294 /* 295 * Power Quotient Int. (PQI) USB flash key 296 * PR: kern/53067 297 */ 298 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*", 299 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 300 }, 301 { 302 /* 303 * Creative Nomad MUVO mp3 player (USB) 304 * PR: kern/53094 305 */ 306 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"}, 307 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT 308 }, 309 { 310 /* 311 * Jungsoft NEXDISK USB flash key 312 * PR: kern/54737 313 */ 314 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"}, 315 /*quirks*/ DA_Q_NO_SYNC_CACHE 316 }, 317 { 318 /* 319 * FreeDik USB Mini Data Drive 320 * PR: kern/54786 321 */ 322 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive", 323 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 324 }, 325 { 326 /* 327 * Sigmatel USB Flash MP3 Player 328 * PR: kern/57046 329 */ 330 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"}, 331 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT 332 }, 333 { 334 /* 335 * Neuros USB Digital Audio Computer 336 * PR: kern/63645 337 */ 338 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.", 339 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 340 }, 341 { 342 /* 343 * SEAGRAND NP-900 MP3 Player 344 * PR: kern/64563 345 */ 346 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"}, 347 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT 348 }, 349 { 350 /* 351 * iRiver iFP MP3 player (with UMS Firmware) 352 * PR: kern/54881, i386/63941, kern/66124 353 */ 354 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"}, 355 /*quirks*/ DA_Q_NO_SYNC_CACHE 356 }, 357 { 358 /* 359 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01 360 * PR: kern/70158 361 */ 362 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"}, 363 /*quirks*/ DA_Q_NO_SYNC_CACHE 364 }, 365 { 366 /* 367 * ZICPlay USB MP3 Player with FM 368 * PR: kern/75057 369 */ 370 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"}, 371 /*quirks*/ DA_Q_NO_SYNC_CACHE 372 }, 373 { 374 /* 375 * TEAC USB floppy mechanisms 376 */ 377 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"}, 378 /*quirks*/ DA_Q_NO_SYNC_CACHE 379 }, 380 { 381 /* 382 * Kingston DataTraveler II+ USB Pen-Drive. 383 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org> 384 */ 385 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+", 386 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 387 }, 388 { 389 /* 390 * USB DISK Pro PMAP 391 * Reported by: jhs 392 * PR: usb/96381 393 */ 394 {T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"}, 395 /*quirks*/ DA_Q_NO_SYNC_CACHE 396 }, 397 { 398 /* 399 * Motorola E398 Mobile Phone (TransFlash memory card). 400 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl> 401 * PR: usb/89889 402 */ 403 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone", 404 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 405 }, 406 { 407 /* 408 * Qware BeatZkey! Pro 409 * PR: usb/79164 410 */ 411 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE", 412 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 413 }, 414 { 415 /* 416 * Time DPA20B 1GB MP3 Player 417 * PR: usb/81846 418 */ 419 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*", 420 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 421 }, 422 { 423 /* 424 * Samsung USB key 128Mb 425 * PR: usb/90081 426 */ 427 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb", 428 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 429 }, 430 { 431 /* 432 * Kingston DataTraveler 2.0 USB Flash memory. 433 * PR: usb/89196 434 */ 435 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0", 436 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 437 }, 438 { 439 /* 440 * Creative MUVO Slim mp3 player (USB) 441 * PR: usb/86131 442 */ 443 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim", 444 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT 445 }, 446 { 447 /* 448 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3) 449 * PR: usb/80487 450 */ 451 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK", 452 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 453 }, 454 { 455 /* 456 * SanDisk Micro Cruzer 128MB 457 * PR: usb/75970 458 */ 459 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer", 460 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 461 }, 462 { 463 /* 464 * TOSHIBA TransMemory USB sticks 465 * PR: kern/94660 466 */ 467 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory", 468 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 469 }, 470 { 471 /* 472 * PNY USB Flash keys 473 * PR: usb/75578, usb/72344, usb/65436 474 */ 475 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*", 476 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 477 }, 478 { 479 /* 480 * Genesys 6-in-1 Card Reader 481 * PR: usb/94647 482 */ 483 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*", 484 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 485 }, 486 { 487 /* 488 * Rekam Digital CAMERA 489 * PR: usb/98713 490 */ 491 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*", 492 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 493 }, 494 { 495 /* 496 * iRiver H10 MP3 player 497 * PR: usb/102547 498 */ 499 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*", 500 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 501 }, 502 { 503 /* 504 * iRiver U10 MP3 player 505 * PR: usb/92306 506 */ 507 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*", 508 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 509 }, 510 { 511 /* 512 * X-Micro Flash Disk 513 * PR: usb/96901 514 */ 515 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk", 516 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 517 }, 518 { 519 /* 520 * EasyMP3 EM732X USB 2.0 Flash MP3 Player 521 * PR: usb/96546 522 */ 523 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*", 524 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 525 }, 526 { 527 /* 528 * Denver MP3 player 529 * PR: usb/107101 530 */ 531 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER", 532 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 533 }, 534 { 535 /* 536 * Philips USB Key Audio KEY013 537 * PR: usb/68412 538 */ 539 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"}, 540 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT 541 }, 542 { 543 /* 544 * JNC MP3 Player 545 * PR: usb/94439 546 */ 547 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*", 548 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 549 }, 550 { 551 /* 552 * SAMSUNG MP0402H 553 * PR: usb/108427 554 */ 555 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"}, 556 /*quirks*/ DA_Q_NO_SYNC_CACHE 557 }, 558 { 559 /* 560 * I/O Magic USB flash - Giga Bank 561 * PR: usb/108810 562 */ 563 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"}, 564 /*quirks*/ DA_Q_NO_SYNC_CACHE 565 }, 566 { 567 /* 568 * JoyFly 128mb USB Flash Drive 569 * PR: 96133 570 */ 571 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*", 572 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 573 }, 574 { 575 /* 576 * ChipsBnk usb stick 577 * PR: 103702 578 */ 579 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*", 580 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 581 }, 582 { 583 /* 584 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A 585 * PR: 129858 586 */ 587 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*", 588 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 589 }, 590 { 591 /* 592 * Samsung YP-U3 mp3-player 593 * PR: 125398 594 */ 595 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3", 596 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 597 }, 598 { 599 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*", 600 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 601 }, 602 { 603 /* 604 * Sony Cyber-Shot DSC cameras 605 * PR: usb/137035 606 */ 607 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"}, 608 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT 609 }, 610 { 611 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler G3", 612 "1.00"}, /*quirks*/ DA_Q_NO_PREVENT 613 }, 614 /* ATA/SATA devices over SAS/USB/... */ 615 { 616 /* Hitachi Advanced Format (4k) drives */ 617 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" }, 618 /*quirks*/DA_Q_4K 619 }, 620 { 621 /* Samsung Advanced Format (4k) drives */ 622 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" }, 623 /*quirks*/DA_Q_4K 624 }, 625 { 626 /* Samsung Advanced Format (4k) drives */ 627 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" }, 628 /*quirks*/DA_Q_4K 629 }, 630 { 631 /* Samsung Advanced Format (4k) drives */ 632 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" }, 633 /*quirks*/DA_Q_4K 634 }, 635 { 636 /* Samsung Advanced Format (4k) drives */ 637 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" }, 638 /*quirks*/DA_Q_4K 639 }, 640 { 641 /* Seagate Barracuda Green Advanced Format (4k) drives */ 642 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" }, 643 /*quirks*/DA_Q_4K 644 }, 645 { 646 /* Seagate Barracuda Green Advanced Format (4k) drives */ 647 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" }, 648 /*quirks*/DA_Q_4K 649 }, 650 { 651 /* Seagate Barracuda Green Advanced Format (4k) drives */ 652 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" }, 653 /*quirks*/DA_Q_4K 654 }, 655 { 656 /* Seagate Barracuda Green Advanced Format (4k) drives */ 657 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" }, 658 /*quirks*/DA_Q_4K 659 }, 660 { 661 /* Seagate Barracuda Green Advanced Format (4k) drives */ 662 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" }, 663 /*quirks*/DA_Q_4K 664 }, 665 { 666 /* Seagate Barracuda Green Advanced Format (4k) drives */ 667 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" }, 668 /*quirks*/DA_Q_4K 669 }, 670 { 671 /* Seagate Momentus Advanced Format (4k) drives */ 672 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" }, 673 /*quirks*/DA_Q_4K 674 }, 675 { 676 /* Seagate Momentus Advanced Format (4k) drives */ 677 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" }, 678 /*quirks*/DA_Q_4K 679 }, 680 { 681 /* Seagate Momentus Advanced Format (4k) drives */ 682 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" }, 683 /*quirks*/DA_Q_4K 684 }, 685 { 686 /* Seagate Momentus Advanced Format (4k) drives */ 687 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" }, 688 /*quirks*/DA_Q_4K 689 }, 690 { 691 /* Seagate Momentus Advanced Format (4k) drives */ 692 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" }, 693 /*quirks*/DA_Q_4K 694 }, 695 { 696 /* Seagate Momentus Advanced Format (4k) drives */ 697 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" }, 698 /*quirks*/DA_Q_4K 699 }, 700 { 701 /* Seagate Momentus Advanced Format (4k) drives */ 702 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" }, 703 /*quirks*/DA_Q_4K 704 }, 705 { 706 /* Seagate Momentus Advanced Format (4k) drives */ 707 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" }, 708 /*quirks*/DA_Q_4K 709 }, 710 { 711 /* Seagate Momentus Advanced Format (4k) drives */ 712 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" }, 713 /*quirks*/DA_Q_4K 714 }, 715 { 716 /* Seagate Momentus Advanced Format (4k) drives */ 717 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" }, 718 /*quirks*/DA_Q_4K 719 }, 720 { 721 /* Seagate Momentus Advanced Format (4k) drives */ 722 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" }, 723 /*quirks*/DA_Q_4K 724 }, 725 { 726 /* Seagate Momentus Advanced Format (4k) drives */ 727 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" }, 728 /*quirks*/DA_Q_4K 729 }, 730 { 731 /* Seagate Momentus Advanced Format (4k) drives */ 732 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" }, 733 /*quirks*/DA_Q_4K 734 }, 735 { 736 /* Seagate Momentus Advanced Format (4k) drives */ 737 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" }, 738 /*quirks*/DA_Q_4K 739 }, 740 { 741 /* Seagate Momentus Thin Advanced Format (4k) drives */ 742 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" }, 743 /*quirks*/DA_Q_4K 744 }, 745 { 746 /* Seagate Momentus Thin Advanced Format (4k) drives */ 747 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" }, 748 /*quirks*/DA_Q_4K 749 }, 750 { 751 /* WDC Caviar Green Advanced Format (4k) drives */ 752 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" }, 753 /*quirks*/DA_Q_4K 754 }, 755 { 756 /* WDC Caviar Green Advanced Format (4k) drives */ 757 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" }, 758 /*quirks*/DA_Q_4K 759 }, 760 { 761 /* WDC Caviar Green Advanced Format (4k) drives */ 762 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" }, 763 /*quirks*/DA_Q_4K 764 }, 765 { 766 /* WDC Caviar Green Advanced Format (4k) drives */ 767 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" }, 768 /*quirks*/DA_Q_4K 769 }, 770 { 771 /* WDC Caviar Green Advanced Format (4k) drives */ 772 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" }, 773 /*quirks*/DA_Q_4K 774 }, 775 { 776 /* WDC Caviar Green Advanced Format (4k) drives */ 777 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" }, 778 /*quirks*/DA_Q_4K 779 }, 780 { 781 /* WDC Caviar Green Advanced Format (4k) drives */ 782 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" }, 783 /*quirks*/DA_Q_4K 784 }, 785 { 786 /* WDC Caviar Green Advanced Format (4k) drives */ 787 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" }, 788 /*quirks*/DA_Q_4K 789 }, 790 { 791 /* WDC Scorpio Black Advanced Format (4k) drives */ 792 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" }, 793 /*quirks*/DA_Q_4K 794 }, 795 { 796 /* WDC Scorpio Black Advanced Format (4k) drives */ 797 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" }, 798 /*quirks*/DA_Q_4K 799 }, 800 { 801 /* WDC Scorpio Black Advanced Format (4k) drives */ 802 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" }, 803 /*quirks*/DA_Q_4K 804 }, 805 { 806 /* WDC Scorpio Black Advanced Format (4k) drives */ 807 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" }, 808 /*quirks*/DA_Q_4K 809 }, 810 { 811 /* WDC Scorpio Blue Advanced Format (4k) drives */ 812 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" }, 813 /*quirks*/DA_Q_4K 814 }, 815 { 816 /* WDC Scorpio Blue Advanced Format (4k) drives */ 817 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" }, 818 /*quirks*/DA_Q_4K 819 }, 820 { 821 /* WDC Scorpio Blue Advanced Format (4k) drives */ 822 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" }, 823 /*quirks*/DA_Q_4K 824 }, 825 { 826 /* WDC Scorpio Blue Advanced Format (4k) drives */ 827 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" }, 828 /*quirks*/DA_Q_4K 829 }, 830 { 831 /* 832 * Olympus FE-210 camera 833 */ 834 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*", 835 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 836 }, 837 { 838 /* 839 * LG UP3S MP3 player 840 */ 841 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S", 842 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 843 }, 844 { 845 /* 846 * Laser MP3-2GA13 MP3 player 847 */ 848 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk", 849 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE 850 }, 851 { 852 /* 853 * LaCie external 250GB Hard drive des by Porsche 854 * Submitted by: Ben Stuyts <ben@altesco.nl> 855 * PR: 121474 856 */ 857 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"}, 858 /*quirks*/ DA_Q_NO_SYNC_CACHE 859 }, 860 }; 861 862 static disk_strategy_t dastrategy; 863 static dumper_t dadump; 864 static periph_init_t dainit; 865 static void daasync(void *callback_arg, u_int32_t code, 866 struct cam_path *path, void *arg); 867 static void dasysctlinit(void *context, int pending); 868 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS); 869 static int dadeletemethodsysctl(SYSCTL_HANDLER_ARGS); 870 static int dadeletemethodset(struct da_softc *softc, 871 da_delete_methods delete_method); 872 static periph_ctor_t daregister; 873 static periph_dtor_t dacleanup; 874 static periph_start_t dastart; 875 static periph_oninv_t daoninvalidate; 876 static void dadone(struct cam_periph *periph, 877 union ccb *done_ccb); 878 static int daerror(union ccb *ccb, u_int32_t cam_flags, 879 u_int32_t sense_flags); 880 static void daprevent(struct cam_periph *periph, int action); 881 static void dareprobe(struct cam_periph *periph); 882 static void dasetgeom(struct cam_periph *periph, uint32_t block_len, 883 uint64_t maxsector, 884 struct scsi_read_capacity_data_long *rcaplong, 885 size_t rcap_size); 886 static timeout_t dasendorderedtag; 887 static void dashutdown(void *arg, int howto); 888 static timeout_t damediapoll; 889 890 #ifndef DA_DEFAULT_POLL_PERIOD 891 #define DA_DEFAULT_POLL_PERIOD 3 892 #endif 893 894 #ifndef DA_DEFAULT_TIMEOUT 895 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */ 896 #endif 897 898 #ifndef DA_DEFAULT_RETRY 899 #define DA_DEFAULT_RETRY 4 900 #endif 901 902 #ifndef DA_DEFAULT_SEND_ORDERED 903 #define DA_DEFAULT_SEND_ORDERED 1 904 #endif 905 906 907 static int da_poll_period = DA_DEFAULT_POLL_PERIOD; 908 static int da_retry_count = DA_DEFAULT_RETRY; 909 static int da_default_timeout = DA_DEFAULT_TIMEOUT; 910 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED; 911 912 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0, 913 "CAM Direct Access Disk driver"); 914 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RW, 915 &da_poll_period, 0, "Media polling period in seconds"); 916 TUNABLE_INT("kern.cam.da.poll_period", &da_poll_period); 917 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW, 918 &da_retry_count, 0, "Normal I/O retry count"); 919 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count); 920 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW, 921 &da_default_timeout, 0, "Normal I/O timeout (in seconds)"); 922 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout); 923 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RW, 924 &da_send_ordered, 0, "Send Ordered Tags"); 925 TUNABLE_INT("kern.cam.da.send_ordered", &da_send_ordered); 926 927 /* 928 * DA_ORDEREDTAG_INTERVAL determines how often, relative 929 * to the default timeout, we check to see whether an ordered 930 * tagged transaction is appropriate to prevent simple tag 931 * starvation. Since we'd like to ensure that there is at least 932 * 1/2 of the timeout length left for a starved transaction to 933 * complete after we've sent an ordered tag, we must poll at least 934 * four times in every timeout period. This takes care of the worst 935 * case where a starved transaction starts during an interval that 936 * meets the requirement "don't send an ordered tag" test so it takes 937 * us two intervals to determine that a tag must be sent. 938 */ 939 #ifndef DA_ORDEREDTAG_INTERVAL 940 #define DA_ORDEREDTAG_INTERVAL 4 941 #endif 942 943 static struct periph_driver dadriver = 944 { 945 dainit, "da", 946 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0 947 }; 948 949 PERIPHDRIVER_DECLARE(da, dadriver); 950 951 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers"); 952 953 static int 954 daopen(struct disk *dp) 955 { 956 struct cam_periph *periph; 957 struct da_softc *softc; 958 int unit; 959 int error; 960 961 periph = (struct cam_periph *)dp->d_drv1; 962 if (periph == NULL) { 963 return (ENXIO); 964 } 965 966 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 967 return (ENXIO); 968 } 969 970 cam_periph_lock(periph); 971 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) { 972 cam_periph_unlock(periph); 973 cam_periph_release(periph); 974 return (error); 975 } 976 977 unit = periph->unit_number; 978 softc = (struct da_softc *)periph->softc; 979 softc->flags |= DA_FLAG_OPEN; 980 981 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 982 ("daopen\n")); 983 984 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) { 985 /* Invalidate our pack information. */ 986 softc->flags &= ~DA_FLAG_PACK_INVALID; 987 } 988 989 dareprobe(periph); 990 991 /* Wait for the disk size update. */ 992 error = msleep(&softc->disk->d_mediasize, periph->sim->mtx, PRIBIO, 993 "dareprobe", 0); 994 if (error != 0) 995 xpt_print(periph->path, "unable to retrieve capacity data"); 996 997 if (periph->flags & CAM_PERIPH_INVALID || 998 softc->disk->d_sectorsize == 0 || 999 softc->disk->d_mediasize == 0) 1000 error = ENXIO; 1001 1002 if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 && 1003 (softc->quirks & DA_Q_NO_PREVENT) == 0) 1004 daprevent(periph, PR_PREVENT); 1005 1006 if (error == 0) 1007 softc->flags |= DA_FLAG_SAW_MEDIA; 1008 1009 cam_periph_unhold(periph); 1010 cam_periph_unlock(periph); 1011 1012 if (error != 0) { 1013 softc->flags &= ~DA_FLAG_OPEN; 1014 cam_periph_release(periph); 1015 } 1016 1017 return (error); 1018 } 1019 1020 static int 1021 daclose(struct disk *dp) 1022 { 1023 struct cam_periph *periph; 1024 struct da_softc *softc; 1025 1026 periph = (struct cam_periph *)dp->d_drv1; 1027 if (periph == NULL) 1028 return (0); 1029 1030 cam_periph_lock(periph); 1031 if (cam_periph_hold(periph, PRIBIO) != 0) { 1032 cam_periph_unlock(periph); 1033 cam_periph_release(periph); 1034 return (0); 1035 } 1036 1037 softc = (struct da_softc *)periph->softc; 1038 1039 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 1040 ("daclose\n")); 1041 1042 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 1043 && (softc->flags & DA_FLAG_PACK_INVALID) == 0) { 1044 union ccb *ccb; 1045 1046 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1047 1048 scsi_synchronize_cache(&ccb->csio, 1049 /*retries*/1, 1050 /*cbfcnp*/dadone, 1051 MSG_SIMPLE_Q_TAG, 1052 /*begin_lba*/0,/* Cover the whole disk */ 1053 /*lb_count*/0, 1054 SSD_FULL_SIZE, 1055 5 * 60 * 1000); 1056 1057 cam_periph_runccb(ccb, daerror, /*cam_flags*/0, 1058 /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR, 1059 softc->disk->d_devstat); 1060 xpt_release_ccb(ccb); 1061 1062 } 1063 1064 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) { 1065 if ((softc->quirks & DA_Q_NO_PREVENT) == 0) 1066 daprevent(periph, PR_ALLOW); 1067 /* 1068 * If we've got removeable media, mark the blocksize as 1069 * unavailable, since it could change when new media is 1070 * inserted. 1071 */ 1072 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE; 1073 } 1074 1075 softc->flags &= ~DA_FLAG_OPEN; 1076 cam_periph_unhold(periph); 1077 cam_periph_unlock(periph); 1078 cam_periph_release(periph); 1079 return (0); 1080 } 1081 1082 static void 1083 daschedule(struct cam_periph *periph) 1084 { 1085 struct da_softc *softc = (struct da_softc *)periph->softc; 1086 uint32_t prio; 1087 1088 if (softc->state != DA_STATE_NORMAL) 1089 return; 1090 1091 /* Check if cam_periph_getccb() was called. */ 1092 prio = periph->immediate_priority; 1093 1094 /* Check if we have more work to do. */ 1095 if (bioq_first(&softc->bio_queue) || 1096 (!softc->delete_running && bioq_first(&softc->delete_queue)) || 1097 softc->tur) { 1098 prio = CAM_PRIORITY_NORMAL; 1099 } 1100 1101 /* Schedule CCB if any of above is true. */ 1102 if (prio != CAM_PRIORITY_NONE) 1103 xpt_schedule(periph, prio); 1104 } 1105 1106 /* 1107 * Actually translate the requested transfer into one the physical driver 1108 * can understand. The transfer is described by a buf and will include 1109 * only one physical transfer. 1110 */ 1111 static void 1112 dastrategy(struct bio *bp) 1113 { 1114 struct cam_periph *periph; 1115 struct da_softc *softc; 1116 1117 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 1118 if (periph == NULL) { 1119 biofinish(bp, NULL, ENXIO); 1120 return; 1121 } 1122 softc = (struct da_softc *)periph->softc; 1123 1124 cam_periph_lock(periph); 1125 1126 /* 1127 * If the device has been made invalid, error out 1128 */ 1129 if ((softc->flags & DA_FLAG_PACK_INVALID)) { 1130 cam_periph_unlock(periph); 1131 biofinish(bp, NULL, ENXIO); 1132 return; 1133 } 1134 1135 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp)); 1136 1137 /* 1138 * Place it in the queue of disk activities for this disk 1139 */ 1140 if (bp->bio_cmd == BIO_DELETE) { 1141 if (bp->bio_bcount == 0) 1142 biodone(bp); 1143 else 1144 bioq_disksort(&softc->delete_queue, bp); 1145 } else 1146 bioq_disksort(&softc->bio_queue, bp); 1147 1148 /* 1149 * Schedule ourselves for performing the work. 1150 */ 1151 daschedule(periph); 1152 cam_periph_unlock(periph); 1153 1154 return; 1155 } 1156 1157 static int 1158 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) 1159 { 1160 struct cam_periph *periph; 1161 struct da_softc *softc; 1162 u_int secsize; 1163 struct ccb_scsiio csio; 1164 struct disk *dp; 1165 int error = 0; 1166 1167 dp = arg; 1168 periph = dp->d_drv1; 1169 if (periph == NULL) 1170 return (ENXIO); 1171 softc = (struct da_softc *)periph->softc; 1172 cam_periph_lock(periph); 1173 secsize = softc->params.secsize; 1174 1175 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) { 1176 cam_periph_unlock(periph); 1177 return (ENXIO); 1178 } 1179 1180 if (length > 0) { 1181 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1182 csio.ccb_h.ccb_state = DA_CCB_DUMP; 1183 scsi_read_write(&csio, 1184 /*retries*/0, 1185 dadone, 1186 MSG_ORDERED_Q_TAG, 1187 /*read*/SCSI_RW_WRITE, 1188 /*byte2*/0, 1189 /*minimum_cmd_size*/ softc->minimum_cmd_size, 1190 offset / secsize, 1191 length / secsize, 1192 /*data_ptr*/(u_int8_t *) virtual, 1193 /*dxfer_len*/length, 1194 /*sense_len*/SSD_FULL_SIZE, 1195 da_default_timeout * 1000); 1196 xpt_polled_action((union ccb *)&csio); 1197 1198 error = cam_periph_error((union ccb *)&csio, 1199 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 1200 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0) 1201 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0, 1202 /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 1203 if (error != 0) 1204 printf("Aborting dump due to I/O error.\n"); 1205 cam_periph_unlock(periph); 1206 return (error); 1207 } 1208 1209 /* 1210 * Sync the disk cache contents to the physical media. 1211 */ 1212 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) { 1213 1214 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1215 csio.ccb_h.ccb_state = DA_CCB_DUMP; 1216 scsi_synchronize_cache(&csio, 1217 /*retries*/0, 1218 /*cbfcnp*/dadone, 1219 MSG_SIMPLE_Q_TAG, 1220 /*begin_lba*/0,/* Cover the whole disk */ 1221 /*lb_count*/0, 1222 SSD_FULL_SIZE, 1223 5 * 60 * 1000); 1224 xpt_polled_action((union ccb *)&csio); 1225 1226 error = cam_periph_error((union ccb *)&csio, 1227 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL); 1228 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0) 1229 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0, 1230 /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 1231 if (error != 0) 1232 xpt_print(periph->path, "Synchronize cache failed\n"); 1233 } 1234 cam_periph_unlock(periph); 1235 return (error); 1236 } 1237 1238 static int 1239 dagetattr(struct bio *bp) 1240 { 1241 int ret; 1242 struct cam_periph *periph; 1243 1244 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 1245 if (periph == NULL) 1246 return (ENXIO); 1247 1248 cam_periph_lock(periph); 1249 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute, 1250 periph->path); 1251 cam_periph_unlock(periph); 1252 if (ret == 0) 1253 bp->bio_completed = bp->bio_length; 1254 return ret; 1255 } 1256 1257 static void 1258 dainit(void) 1259 { 1260 cam_status status; 1261 1262 /* 1263 * Install a global async callback. This callback will 1264 * receive async callbacks like "new device found". 1265 */ 1266 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL); 1267 1268 if (status != CAM_REQ_CMP) { 1269 printf("da: Failed to attach master async callback " 1270 "due to status 0x%x!\n", status); 1271 } else if (da_send_ordered) { 1272 1273 /* Register our shutdown event handler */ 1274 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown, 1275 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 1276 printf("dainit: shutdown event registration failed!\n"); 1277 } 1278 } 1279 1280 /* 1281 * Callback from GEOM, called when it has finished cleaning up its 1282 * resources. 1283 */ 1284 static void 1285 dadiskgonecb(struct disk *dp) 1286 { 1287 struct cam_periph *periph; 1288 1289 periph = (struct cam_periph *)dp->d_drv1; 1290 1291 cam_periph_release(periph); 1292 } 1293 1294 static void 1295 daoninvalidate(struct cam_periph *periph) 1296 { 1297 struct da_softc *softc; 1298 1299 softc = (struct da_softc *)periph->softc; 1300 1301 /* 1302 * De-register any async callbacks. 1303 */ 1304 xpt_register_async(0, daasync, periph, periph->path); 1305 1306 softc->flags |= DA_FLAG_PACK_INVALID; 1307 1308 /* 1309 * Return all queued I/O with ENXIO. 1310 * XXX Handle any transactions queued to the card 1311 * with XPT_ABORT_CCB. 1312 */ 1313 bioq_flush(&softc->bio_queue, NULL, ENXIO); 1314 bioq_flush(&softc->delete_queue, NULL, ENXIO); 1315 1316 /* 1317 * Tell GEOM that we've gone away, we'll get a callback when it is 1318 * done cleaning up its resources. 1319 */ 1320 disk_gone(softc->disk); 1321 1322 xpt_print(periph->path, "lost device - %d outstanding, %d refs\n", 1323 softc->outstanding_cmds, periph->refcount); 1324 } 1325 1326 static void 1327 dacleanup(struct cam_periph *periph) 1328 { 1329 struct da_softc *softc; 1330 1331 softc = (struct da_softc *)periph->softc; 1332 1333 xpt_print(periph->path, "removing device entry\n"); 1334 cam_periph_unlock(periph); 1335 1336 /* 1337 * If we can't free the sysctl tree, oh well... 1338 */ 1339 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0 1340 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) { 1341 xpt_print(periph->path, "can't remove sysctl context\n"); 1342 } 1343 1344 callout_drain(&softc->mediapoll_c); 1345 disk_destroy(softc->disk); 1346 callout_drain(&softc->sendordered_c); 1347 free(softc, M_DEVBUF); 1348 cam_periph_lock(periph); 1349 } 1350 1351 static void 1352 daasync(void *callback_arg, u_int32_t code, 1353 struct cam_path *path, void *arg) 1354 { 1355 struct cam_periph *periph; 1356 struct da_softc *softc; 1357 1358 periph = (struct cam_periph *)callback_arg; 1359 switch (code) { 1360 case AC_FOUND_DEVICE: 1361 { 1362 struct ccb_getdev *cgd; 1363 cam_status status; 1364 1365 cgd = (struct ccb_getdev *)arg; 1366 if (cgd == NULL) 1367 break; 1368 1369 if (cgd->protocol != PROTO_SCSI) 1370 break; 1371 1372 if (SID_TYPE(&cgd->inq_data) != T_DIRECT 1373 && SID_TYPE(&cgd->inq_data) != T_RBC 1374 && SID_TYPE(&cgd->inq_data) != T_OPTICAL) 1375 break; 1376 1377 /* 1378 * Allocate a peripheral instance for 1379 * this device and start the probe 1380 * process. 1381 */ 1382 status = cam_periph_alloc(daregister, daoninvalidate, 1383 dacleanup, dastart, 1384 "da", CAM_PERIPH_BIO, 1385 cgd->ccb_h.path, daasync, 1386 AC_FOUND_DEVICE, cgd); 1387 1388 if (status != CAM_REQ_CMP 1389 && status != CAM_REQ_INPROG) 1390 printf("daasync: Unable to attach to new device " 1391 "due to status 0x%x\n", status); 1392 return; 1393 } 1394 case AC_ADVINFO_CHANGED: 1395 { 1396 uintptr_t buftype; 1397 1398 buftype = (uintptr_t)arg; 1399 if (buftype == CDAI_TYPE_PHYS_PATH) { 1400 struct da_softc *softc; 1401 1402 softc = periph->softc; 1403 disk_attr_changed(softc->disk, "GEOM::physpath", 1404 M_NOWAIT); 1405 } 1406 break; 1407 } 1408 case AC_UNIT_ATTENTION: 1409 { 1410 union ccb *ccb; 1411 int error_code, sense_key, asc, ascq; 1412 1413 softc = (struct da_softc *)periph->softc; 1414 ccb = (union ccb *)arg; 1415 1416 /* 1417 * Handle all UNIT ATTENTIONs except our own, 1418 * as they will be handled by daerror(). 1419 */ 1420 if (xpt_path_periph(ccb->ccb_h.path) != periph && 1421 scsi_extract_sense_ccb(ccb, 1422 &error_code, &sense_key, &asc, &ascq)) { 1423 if (asc == 0x2A && ascq == 0x09) { 1424 xpt_print(ccb->ccb_h.path, 1425 "capacity data has changed\n"); 1426 dareprobe(periph); 1427 } else if (asc == 0x28 && ascq == 0x00) 1428 disk_media_changed(softc->disk, M_NOWAIT); 1429 } 1430 cam_periph_async(periph, code, path, arg); 1431 break; 1432 } 1433 case AC_SCSI_AEN: 1434 softc = (struct da_softc *)periph->softc; 1435 if (!softc->tur) { 1436 if (cam_periph_acquire(periph) == CAM_REQ_CMP) { 1437 softc->tur = 1; 1438 daschedule(periph); 1439 } 1440 } 1441 /* FALLTHROUGH */ 1442 case AC_SENT_BDR: 1443 case AC_BUS_RESET: 1444 { 1445 struct ccb_hdr *ccbh; 1446 1447 softc = (struct da_softc *)periph->softc; 1448 /* 1449 * Don't fail on the expected unit attention 1450 * that will occur. 1451 */ 1452 softc->flags |= DA_FLAG_RETRY_UA; 1453 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le) 1454 ccbh->ccb_state |= DA_CCB_RETRY_UA; 1455 break; 1456 } 1457 default: 1458 break; 1459 } 1460 cam_periph_async(periph, code, path, arg); 1461 } 1462 1463 static void 1464 dasysctlinit(void *context, int pending) 1465 { 1466 struct cam_periph *periph; 1467 struct da_softc *softc; 1468 char tmpstr[80], tmpstr2[80]; 1469 struct ccb_trans_settings cts; 1470 1471 periph = (struct cam_periph *)context; 1472 /* 1473 * periph was held for us when this task was enqueued 1474 */ 1475 if (periph->flags & CAM_PERIPH_INVALID) { 1476 cam_periph_release(periph); 1477 return; 1478 } 1479 1480 softc = (struct da_softc *)periph->softc; 1481 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number); 1482 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 1483 1484 sysctl_ctx_init(&softc->sysctl_ctx); 1485 softc->flags |= DA_FLAG_SCTX_INIT; 1486 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1487 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2, 1488 CTLFLAG_RD, 0, tmpstr); 1489 if (softc->sysctl_tree == NULL) { 1490 printf("dasysctlinit: unable to allocate sysctl tree\n"); 1491 cam_periph_release(periph); 1492 return; 1493 } 1494 1495 /* 1496 * Now register the sysctl handler, so the user can change the value on 1497 * the fly. 1498 */ 1499 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1500 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW, 1501 softc, 0, dadeletemethodsysctl, "A", 1502 "BIO_DELETE execution method"); 1503 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1504 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW, 1505 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I", 1506 "Minimum CDB size"); 1507 1508 SYSCTL_ADD_INT(&softc->sysctl_ctx, 1509 SYSCTL_CHILDREN(softc->sysctl_tree), 1510 OID_AUTO, 1511 "error_inject", 1512 CTLFLAG_RW, 1513 &softc->error_inject, 1514 0, 1515 "error_inject leaf"); 1516 1517 1518 /* 1519 * Add some addressing info. 1520 */ 1521 memset(&cts, 0, sizeof (cts)); 1522 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE); 1523 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 1524 cts.type = CTS_TYPE_CURRENT_SETTINGS; 1525 cam_periph_lock(periph); 1526 xpt_action((union ccb *)&cts); 1527 cam_periph_unlock(periph); 1528 if (cts.ccb_h.status != CAM_REQ_CMP) { 1529 cam_periph_release(periph); 1530 return; 1531 } 1532 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) { 1533 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc; 1534 if (fc->valid & CTS_FC_VALID_WWPN) { 1535 softc->wwpn = fc->wwpn; 1536 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx, 1537 SYSCTL_CHILDREN(softc->sysctl_tree), 1538 OID_AUTO, "wwpn", CTLFLAG_RD, 1539 &softc->wwpn, "World Wide Port Name"); 1540 } 1541 } 1542 cam_periph_release(periph); 1543 } 1544 1545 static int 1546 dacmdsizesysctl(SYSCTL_HANDLER_ARGS) 1547 { 1548 int error, value; 1549 1550 value = *(int *)arg1; 1551 1552 error = sysctl_handle_int(oidp, &value, 0, req); 1553 1554 if ((error != 0) 1555 || (req->newptr == NULL)) 1556 return (error); 1557 1558 /* 1559 * Acceptable values here are 6, 10, 12 or 16. 1560 */ 1561 if (value < 6) 1562 value = 6; 1563 else if ((value > 6) 1564 && (value <= 10)) 1565 value = 10; 1566 else if ((value > 10) 1567 && (value <= 12)) 1568 value = 12; 1569 else if (value > 12) 1570 value = 16; 1571 1572 *(int *)arg1 = value; 1573 1574 return (0); 1575 } 1576 1577 static int 1578 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method) 1579 { 1580 1581 if (delete_method < 0 || delete_method > DA_DELETE_MAX) 1582 return (EINVAL); 1583 1584 softc->delete_method = delete_method; 1585 1586 if (softc->delete_method > DA_DELETE_DISABLE) 1587 softc->disk->d_flags |= DISKFLAG_CANDELETE; 1588 else 1589 softc->disk->d_flags &= ~DISKFLAG_CANDELETE; 1590 1591 return (0); 1592 } 1593 1594 static int 1595 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS) 1596 { 1597 char buf[16]; 1598 const char *p; 1599 struct da_softc *softc; 1600 int i, error, value; 1601 1602 softc = (struct da_softc *)arg1; 1603 1604 value = softc->delete_method; 1605 if (value < 0 || value > DA_DELETE_MAX) 1606 p = "UNKNOWN"; 1607 else 1608 p = da_delete_method_names[value]; 1609 strncpy(buf, p, sizeof(buf)); 1610 error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 1611 if (error != 0 || req->newptr == NULL) 1612 return (error); 1613 for (i = 0; i <= DA_DELETE_MAX; i++) { 1614 if (strcmp(buf, da_delete_method_names[i]) != 0) 1615 continue; 1616 return dadeletemethodset(softc, i); 1617 } 1618 return (EINVAL); 1619 } 1620 1621 static cam_status 1622 daregister(struct cam_periph *periph, void *arg) 1623 { 1624 struct da_softc *softc; 1625 struct ccb_pathinq cpi; 1626 struct ccb_getdev *cgd; 1627 char tmpstr[80]; 1628 caddr_t match; 1629 1630 cgd = (struct ccb_getdev *)arg; 1631 if (cgd == NULL) { 1632 printf("daregister: no getdev CCB, can't register device\n"); 1633 return(CAM_REQ_CMP_ERR); 1634 } 1635 1636 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF, 1637 M_NOWAIT|M_ZERO); 1638 1639 if (softc == NULL) { 1640 printf("daregister: Unable to probe new device. " 1641 "Unable to allocate softc\n"); 1642 return(CAM_REQ_CMP_ERR); 1643 } 1644 1645 LIST_INIT(&softc->pending_ccbs); 1646 softc->state = DA_STATE_PROBE; 1647 bioq_init(&softc->bio_queue); 1648 bioq_init(&softc->delete_queue); 1649 bioq_init(&softc->delete_run_queue); 1650 if (SID_IS_REMOVABLE(&cgd->inq_data)) 1651 softc->flags |= DA_FLAG_PACK_REMOVABLE; 1652 softc->unmap_max_ranges = UNMAP_MAX_RANGES; 1653 softc->unmap_max_lba = 1024*1024*2; 1654 1655 periph->softc = softc; 1656 1657 /* 1658 * See if this device has any quirks. 1659 */ 1660 match = cam_quirkmatch((caddr_t)&cgd->inq_data, 1661 (caddr_t)da_quirk_table, 1662 sizeof(da_quirk_table)/sizeof(*da_quirk_table), 1663 sizeof(*da_quirk_table), scsi_inquiry_match); 1664 1665 if (match != NULL) 1666 softc->quirks = ((struct da_quirk_entry *)match)->quirks; 1667 else 1668 softc->quirks = DA_Q_NONE; 1669 1670 /* Check if the SIM does not want 6 byte commands */ 1671 bzero(&cpi, sizeof(cpi)); 1672 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1673 cpi.ccb_h.func_code = XPT_PATH_INQ; 1674 xpt_action((union ccb *)&cpi); 1675 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE)) 1676 softc->quirks |= DA_Q_NO_6_BYTE; 1677 1678 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph); 1679 1680 /* 1681 * Take an exclusive refcount on the periph while dastart is called 1682 * to finish the probe. The reference will be dropped in dadone at 1683 * the end of probe. 1684 */ 1685 (void)cam_periph_hold(periph, PRIBIO); 1686 1687 /* 1688 * Schedule a periodic event to occasionally send an 1689 * ordered tag to a device. 1690 */ 1691 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0); 1692 callout_reset(&softc->sendordered_c, 1693 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL, 1694 dasendorderedtag, softc); 1695 1696 mtx_unlock(periph->sim->mtx); 1697 /* 1698 * RBC devices don't have to support READ(6), only READ(10). 1699 */ 1700 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC) 1701 softc->minimum_cmd_size = 10; 1702 else 1703 softc->minimum_cmd_size = 6; 1704 1705 /* 1706 * Load the user's default, if any. 1707 */ 1708 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size", 1709 periph->unit_number); 1710 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size); 1711 1712 /* 1713 * 6, 10, 12 and 16 are the currently permissible values. 1714 */ 1715 if (softc->minimum_cmd_size < 6) 1716 softc->minimum_cmd_size = 6; 1717 else if ((softc->minimum_cmd_size > 6) 1718 && (softc->minimum_cmd_size <= 10)) 1719 softc->minimum_cmd_size = 10; 1720 else if ((softc->minimum_cmd_size > 10) 1721 && (softc->minimum_cmd_size <= 12)) 1722 softc->minimum_cmd_size = 12; 1723 else if (softc->minimum_cmd_size > 12) 1724 softc->minimum_cmd_size = 16; 1725 1726 /* Predict whether device may support READ CAPACITY(16). */ 1727 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3) { 1728 softc->flags |= DA_FLAG_CAN_RC16; 1729 softc->state = DA_STATE_PROBE2; 1730 } 1731 1732 /* 1733 * Register this media as a disk. 1734 */ 1735 softc->disk = disk_alloc(); 1736 softc->disk->d_devstat = devstat_new_entry(periph->periph_name, 1737 periph->unit_number, 0, 1738 DEVSTAT_BS_UNAVAILABLE, 1739 SID_TYPE(&cgd->inq_data) | 1740 XPORT_DEVSTAT_TYPE(cpi.transport), 1741 DEVSTAT_PRIORITY_DISK); 1742 softc->disk->d_open = daopen; 1743 softc->disk->d_close = daclose; 1744 softc->disk->d_strategy = dastrategy; 1745 softc->disk->d_dump = dadump; 1746 softc->disk->d_getattr = dagetattr; 1747 softc->disk->d_gone = dadiskgonecb; 1748 softc->disk->d_name = "da"; 1749 softc->disk->d_drv1 = periph; 1750 if (cpi.maxio == 0) 1751 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */ 1752 else if (cpi.maxio > MAXPHYS) 1753 softc->disk->d_maxsize = MAXPHYS; /* for safety */ 1754 else 1755 softc->disk->d_maxsize = cpi.maxio; 1756 softc->disk->d_unit = periph->unit_number; 1757 softc->disk->d_flags = 0; 1758 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) 1759 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE; 1760 if ((cpi.hba_misc & PIM_UNMAPPED) != 0) 1761 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO; 1762 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor, 1763 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr)); 1764 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr)); 1765 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)], 1766 cgd->inq_data.product, sizeof(cgd->inq_data.product), 1767 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr)); 1768 softc->disk->d_hba_vendor = cpi.hba_vendor; 1769 softc->disk->d_hba_device = cpi.hba_device; 1770 softc->disk->d_hba_subvendor = cpi.hba_subvendor; 1771 softc->disk->d_hba_subdevice = cpi.hba_subdevice; 1772 1773 /* 1774 * Acquire a reference to the periph before we register with GEOM. 1775 * We'll release this reference once GEOM calls us back (via 1776 * dadiskgonecb()) telling us that our provider has been freed. 1777 */ 1778 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 1779 xpt_print(periph->path, "%s: lost periph during " 1780 "registration!\n", __func__); 1781 mtx_lock(periph->sim->mtx); 1782 return (CAM_REQ_CMP_ERR); 1783 } 1784 1785 disk_create(softc->disk, DISK_VERSION); 1786 mtx_lock(periph->sim->mtx); 1787 1788 /* 1789 * Add async callbacks for events of interest. 1790 * I don't bother checking if this fails as, 1791 * in most cases, the system will function just 1792 * fine without them and the only alternative 1793 * would be to not attach the device on failure. 1794 */ 1795 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE | 1796 AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION, 1797 daasync, periph, periph->path); 1798 1799 /* 1800 * Emit an attribute changed notification just in case 1801 * physical path information arrived before our async 1802 * event handler was registered, but after anyone attaching 1803 * to our disk device polled it. 1804 */ 1805 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT); 1806 1807 /* 1808 * Schedule a periodic media polling events. 1809 */ 1810 callout_init_mtx(&softc->mediapoll_c, periph->sim->mtx, 0); 1811 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) && 1812 (cgd->inq_flags & SID_AEN) == 0 && 1813 da_poll_period != 0) 1814 callout_reset(&softc->mediapoll_c, da_poll_period * hz, 1815 damediapoll, periph); 1816 1817 xpt_schedule(periph, CAM_PRIORITY_DEV); 1818 1819 return(CAM_REQ_CMP); 1820 } 1821 1822 static void 1823 dastart(struct cam_periph *periph, union ccb *start_ccb) 1824 { 1825 struct da_softc *softc; 1826 1827 softc = (struct da_softc *)periph->softc; 1828 1829 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n")); 1830 1831 switch (softc->state) { 1832 case DA_STATE_NORMAL: 1833 { 1834 struct bio *bp, *bp1; 1835 uint8_t tag_code; 1836 1837 /* Execute immediate CCB if waiting. */ 1838 if (periph->immediate_priority <= periph->pinfo.priority) { 1839 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1840 ("queuing for immediate ccb\n")); 1841 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING; 1842 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 1843 periph_links.sle); 1844 periph->immediate_priority = CAM_PRIORITY_NONE; 1845 wakeup(&periph->ccb_list); 1846 /* May have more work to do, so ensure we stay scheduled */ 1847 daschedule(periph); 1848 break; 1849 } 1850 1851 /* Run BIO_DELETE if not running yet. */ 1852 if (!softc->delete_running && 1853 (bp = bioq_first(&softc->delete_queue)) != NULL) { 1854 uint64_t lba; 1855 u_int count; 1856 1857 if (softc->delete_method == DA_DELETE_UNMAP) { 1858 uint8_t *buf = softc->unmap_buf; 1859 uint64_t lastlba = (uint64_t)-1; 1860 uint32_t lastcount = 0; 1861 int blocks = 0, off, ranges = 0; 1862 1863 softc->delete_running = 1; 1864 bzero(softc->unmap_buf, sizeof(softc->unmap_buf)); 1865 bp1 = bp; 1866 do { 1867 bioq_remove(&softc->delete_queue, bp1); 1868 if (bp1 != bp) 1869 bioq_insert_tail(&softc->delete_run_queue, bp1); 1870 lba = bp1->bio_pblkno; 1871 count = bp1->bio_bcount / softc->params.secsize; 1872 1873 /* Try to extend the previous range. */ 1874 if (lba == lastlba) { 1875 lastcount += count; 1876 off = (ranges - 1) * 16 + 8; 1877 scsi_ulto4b(lastcount, &buf[off + 8]); 1878 } else if (count > 0) { 1879 off = ranges * 16 + 8; 1880 scsi_u64to8b(lba, &buf[off + 0]); 1881 scsi_ulto4b(count, &buf[off + 8]); 1882 lastcount = count; 1883 ranges++; 1884 } 1885 blocks += count; 1886 lastlba = lba + count; 1887 bp1 = bioq_first(&softc->delete_queue); 1888 if (bp1 == NULL || 1889 ranges >= softc->unmap_max_ranges || 1890 blocks + bp1->bio_bcount / 1891 softc->params.secsize > softc->unmap_max_lba) 1892 break; 1893 } while (1); 1894 scsi_ulto2b(ranges * 16 + 6, &buf[0]); 1895 scsi_ulto2b(ranges * 16, &buf[2]); 1896 1897 scsi_unmap(&start_ccb->csio, 1898 /*retries*/da_retry_count, 1899 /*cbfcnp*/dadone, 1900 /*tag_action*/MSG_SIMPLE_Q_TAG, 1901 /*byte2*/0, 1902 /*data_ptr*/ buf, 1903 /*dxfer_len*/ ranges * 16 + 8, 1904 /*sense_len*/SSD_FULL_SIZE, 1905 da_default_timeout * 1000); 1906 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE; 1907 goto out; 1908 } else if (softc->delete_method == DA_DELETE_ZERO || 1909 softc->delete_method == DA_DELETE_WS10 || 1910 softc->delete_method == DA_DELETE_WS16) { 1911 softc->delete_running = 1; 1912 lba = bp->bio_pblkno; 1913 count = 0; 1914 bp1 = bp; 1915 do { 1916 bioq_remove(&softc->delete_queue, bp1); 1917 if (bp1 != bp) 1918 bioq_insert_tail(&softc->delete_run_queue, bp1); 1919 count += bp1->bio_bcount / softc->params.secsize; 1920 bp1 = bioq_first(&softc->delete_queue); 1921 if (bp1 == NULL || 1922 lba + count != bp1->bio_pblkno || 1923 count + bp1->bio_bcount / 1924 softc->params.secsize > 0xffff) 1925 break; 1926 } while (1); 1927 1928 scsi_write_same(&start_ccb->csio, 1929 /*retries*/da_retry_count, 1930 /*cbfcnp*/dadone, 1931 /*tag_action*/MSG_SIMPLE_Q_TAG, 1932 /*byte2*/softc->delete_method == 1933 DA_DELETE_ZERO ? 0 : SWS_UNMAP, 1934 softc->delete_method == 1935 DA_DELETE_WS16 ? 16 : 10, 1936 /*lba*/lba, 1937 /*block_count*/count, 1938 /*data_ptr*/ __DECONST(void *, 1939 zero_region), 1940 /*dxfer_len*/ softc->params.secsize, 1941 /*sense_len*/SSD_FULL_SIZE, 1942 da_default_timeout * 1000); 1943 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE; 1944 goto out; 1945 } else { 1946 bioq_flush(&softc->delete_queue, NULL, 0); 1947 /* FALLTHROUGH */ 1948 } 1949 } 1950 1951 /* Run regular command. */ 1952 bp = bioq_takefirst(&softc->bio_queue); 1953 if (bp == NULL) { 1954 if (softc->tur) { 1955 softc->tur = 0; 1956 scsi_test_unit_ready(&start_ccb->csio, 1957 /*retries*/ da_retry_count, 1958 dadone, 1959 MSG_SIMPLE_Q_TAG, 1960 SSD_FULL_SIZE, 1961 da_default_timeout * 1000); 1962 start_ccb->ccb_h.ccb_bp = NULL; 1963 start_ccb->ccb_h.ccb_state = DA_CCB_TUR; 1964 xpt_action(start_ccb); 1965 } else 1966 xpt_release_ccb(start_ccb); 1967 break; 1968 } 1969 if (softc->tur) { 1970 softc->tur = 0; 1971 cam_periph_release_locked(periph); 1972 } 1973 1974 if ((bp->bio_flags & BIO_ORDERED) != 0 || 1975 (softc->flags & DA_FLAG_NEED_OTAG) != 0) { 1976 softc->flags &= ~DA_FLAG_NEED_OTAG; 1977 softc->ordered_tag_count++; 1978 tag_code = MSG_ORDERED_Q_TAG; 1979 } else { 1980 tag_code = MSG_SIMPLE_Q_TAG; 1981 } 1982 1983 switch (bp->bio_cmd) { 1984 case BIO_READ: 1985 case BIO_WRITE: 1986 scsi_read_write(&start_ccb->csio, 1987 /*retries*/da_retry_count, 1988 /*cbfcnp*/dadone, 1989 /*tag_action*/tag_code, 1990 /*read_op*/(bp->bio_cmd == BIO_READ ? 1991 SCSI_RW_READ : SCSI_RW_WRITE) | 1992 ((bp->bio_flags & BIO_UNMAPPED) != 0 ? 1993 SCSI_RW_BIO : 0), 1994 /*byte2*/0, 1995 softc->minimum_cmd_size, 1996 /*lba*/bp->bio_pblkno, 1997 /*block_count*/bp->bio_bcount / 1998 softc->params.secsize, 1999 /*data_ptr*/ (bp->bio_flags & 2000 BIO_UNMAPPED) != 0 ? (void *)bp : 2001 bp->bio_data, 2002 /*dxfer_len*/ bp->bio_bcount, 2003 /*sense_len*/SSD_FULL_SIZE, 2004 da_default_timeout * 1000); 2005 break; 2006 case BIO_FLUSH: 2007 /* 2008 * BIO_FLUSH doesn't currently communicate 2009 * range data, so we synchronize the cache 2010 * over the whole disk. We also force 2011 * ordered tag semantics the flush applies 2012 * to all previously queued I/O. 2013 */ 2014 scsi_synchronize_cache(&start_ccb->csio, 2015 /*retries*/1, 2016 /*cbfcnp*/dadone, 2017 MSG_ORDERED_Q_TAG, 2018 /*begin_lba*/0, 2019 /*lb_count*/0, 2020 SSD_FULL_SIZE, 2021 da_default_timeout*1000); 2022 break; 2023 } 2024 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO; 2025 2026 out: 2027 /* 2028 * Block out any asyncronous callbacks 2029 * while we touch the pending ccb list. 2030 */ 2031 LIST_INSERT_HEAD(&softc->pending_ccbs, 2032 &start_ccb->ccb_h, periph_links.le); 2033 softc->outstanding_cmds++; 2034 2035 /* We expect a unit attention from this device */ 2036 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) { 2037 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA; 2038 softc->flags &= ~DA_FLAG_RETRY_UA; 2039 } 2040 2041 start_ccb->ccb_h.ccb_bp = bp; 2042 xpt_action(start_ccb); 2043 2044 /* May have more work to do, so ensure we stay scheduled */ 2045 daschedule(periph); 2046 break; 2047 } 2048 case DA_STATE_PROBE: 2049 { 2050 struct scsi_read_capacity_data *rcap; 2051 2052 rcap = (struct scsi_read_capacity_data *) 2053 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO); 2054 if (rcap == NULL) { 2055 printf("dastart: Couldn't malloc read_capacity data\n"); 2056 /* da_free_periph??? */ 2057 break; 2058 } 2059 scsi_read_capacity(&start_ccb->csio, 2060 /*retries*/da_retry_count, 2061 dadone, 2062 MSG_SIMPLE_Q_TAG, 2063 rcap, 2064 SSD_FULL_SIZE, 2065 /*timeout*/5000); 2066 start_ccb->ccb_h.ccb_bp = NULL; 2067 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE; 2068 xpt_action(start_ccb); 2069 break; 2070 } 2071 case DA_STATE_PROBE2: 2072 { 2073 struct scsi_read_capacity_data_long *rcaplong; 2074 2075 rcaplong = (struct scsi_read_capacity_data_long *) 2076 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO); 2077 if (rcaplong == NULL) { 2078 printf("dastart: Couldn't malloc read_capacity data\n"); 2079 /* da_free_periph??? */ 2080 break; 2081 } 2082 scsi_read_capacity_16(&start_ccb->csio, 2083 /*retries*/ da_retry_count, 2084 /*cbfcnp*/ dadone, 2085 /*tag_action*/ MSG_SIMPLE_Q_TAG, 2086 /*lba*/ 0, 2087 /*reladr*/ 0, 2088 /*pmi*/ 0, 2089 /*rcap_buf*/ (uint8_t *)rcaplong, 2090 /*rcap_buf_len*/ sizeof(*rcaplong), 2091 /*sense_len*/ SSD_FULL_SIZE, 2092 /*timeout*/ da_default_timeout * 1000); 2093 start_ccb->ccb_h.ccb_bp = NULL; 2094 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2; 2095 xpt_action(start_ccb); 2096 break; 2097 } 2098 } 2099 } 2100 2101 static int 2102 cmd6workaround(union ccb *ccb) 2103 { 2104 struct scsi_rw_6 cmd6; 2105 struct scsi_rw_10 *cmd10; 2106 struct da_softc *softc; 2107 u_int8_t *cdb; 2108 struct bio *bp; 2109 int frozen; 2110 2111 cdb = ccb->csio.cdb_io.cdb_bytes; 2112 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc; 2113 2114 if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) { 2115 if (softc->delete_method == DA_DELETE_UNMAP) { 2116 xpt_print(ccb->ccb_h.path, "UNMAP is not supported, " 2117 "switching to WRITE SAME(16) with UNMAP.\n"); 2118 dadeletemethodset(softc, DA_DELETE_WS16); 2119 } else if (softc->delete_method == DA_DELETE_WS16) { 2120 xpt_print(ccb->ccb_h.path, 2121 "WRITE SAME(16) with UNMAP is not supported, " 2122 "disabling BIO_DELETE.\n"); 2123 dadeletemethodset(softc, DA_DELETE_DISABLE); 2124 } else if (softc->delete_method == DA_DELETE_WS10) { 2125 xpt_print(ccb->ccb_h.path, 2126 "WRITE SAME(10) with UNMAP is not supported, " 2127 "disabling BIO_DELETE.\n"); 2128 dadeletemethodset(softc, DA_DELETE_DISABLE); 2129 } else if (softc->delete_method == DA_DELETE_ZERO) { 2130 xpt_print(ccb->ccb_h.path, 2131 "WRITE SAME(10) is not supported, " 2132 "disabling BIO_DELETE.\n"); 2133 dadeletemethodset(softc, DA_DELETE_DISABLE); 2134 } else 2135 dadeletemethodset(softc, DA_DELETE_DISABLE); 2136 while ((bp = bioq_takefirst(&softc->delete_run_queue)) 2137 != NULL) 2138 bioq_disksort(&softc->delete_queue, bp); 2139 bioq_insert_tail(&softc->delete_queue, 2140 (struct bio *)ccb->ccb_h.ccb_bp); 2141 ccb->ccb_h.ccb_bp = NULL; 2142 return (0); 2143 } 2144 2145 /* Translation only possible if CDB is an array and cmd is R/W6 */ 2146 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 || 2147 (*cdb != READ_6 && *cdb != WRITE_6)) 2148 return 0; 2149 2150 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, " 2151 "increasing minimum_cmd_size to 10.\n"); 2152 softc->minimum_cmd_size = 10; 2153 2154 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6)); 2155 cmd10 = (struct scsi_rw_10 *)cdb; 2156 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10; 2157 cmd10->byte2 = 0; 2158 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr); 2159 cmd10->reserved = 0; 2160 scsi_ulto2b(cmd6.length, cmd10->length); 2161 cmd10->control = cmd6.control; 2162 ccb->csio.cdb_len = sizeof(*cmd10); 2163 2164 /* Requeue request, unfreezing queue if necessary */ 2165 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 2166 ccb->ccb_h.status = CAM_REQUEUE_REQ; 2167 xpt_action(ccb); 2168 if (frozen) { 2169 cam_release_devq(ccb->ccb_h.path, 2170 /*relsim_flags*/0, 2171 /*reduction*/0, 2172 /*timeout*/0, 2173 /*getcount_only*/0); 2174 } 2175 return (ERESTART); 2176 } 2177 2178 static void 2179 dadone(struct cam_periph *periph, union ccb *done_ccb) 2180 { 2181 struct da_softc *softc; 2182 struct ccb_scsiio *csio; 2183 u_int32_t priority; 2184 da_ccb_state state; 2185 2186 softc = (struct da_softc *)periph->softc; 2187 priority = done_ccb->ccb_h.pinfo.priority; 2188 2189 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n")); 2190 2191 csio = &done_ccb->csio; 2192 state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK; 2193 switch (state) { 2194 case DA_CCB_BUFFER_IO: 2195 case DA_CCB_DELETE: 2196 { 2197 struct bio *bp, *bp1; 2198 2199 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 2200 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2201 int error; 2202 int sf; 2203 2204 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0) 2205 sf = SF_RETRY_UA; 2206 else 2207 sf = 0; 2208 2209 error = daerror(done_ccb, CAM_RETRY_SELTO, sf); 2210 if (error == ERESTART) { 2211 /* 2212 * A retry was scheuled, so 2213 * just return. 2214 */ 2215 return; 2216 } 2217 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 2218 if (error != 0) { 2219 int queued_error; 2220 2221 /* 2222 * return all queued I/O with EIO, so that 2223 * the client can retry these I/Os in the 2224 * proper order should it attempt to recover. 2225 */ 2226 queued_error = EIO; 2227 2228 if (error == ENXIO 2229 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) { 2230 /* 2231 * Catastrophic error. Mark our pack as 2232 * invalid. 2233 */ 2234 /* 2235 * XXX See if this is really a media 2236 * XXX change first? 2237 */ 2238 xpt_print(periph->path, 2239 "Invalidating pack\n"); 2240 softc->flags |= DA_FLAG_PACK_INVALID; 2241 queued_error = ENXIO; 2242 } 2243 bioq_flush(&softc->bio_queue, NULL, 2244 queued_error); 2245 if (bp != NULL) { 2246 bp->bio_error = error; 2247 bp->bio_resid = bp->bio_bcount; 2248 bp->bio_flags |= BIO_ERROR; 2249 } 2250 } else if (bp != NULL) { 2251 bp->bio_resid = csio->resid; 2252 bp->bio_error = 0; 2253 if (bp->bio_resid != 0) 2254 bp->bio_flags |= BIO_ERROR; 2255 } 2256 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 2257 cam_release_devq(done_ccb->ccb_h.path, 2258 /*relsim_flags*/0, 2259 /*reduction*/0, 2260 /*timeout*/0, 2261 /*getcount_only*/0); 2262 } else if (bp != NULL) { 2263 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 2264 panic("REQ_CMP with QFRZN"); 2265 bp->bio_resid = csio->resid; 2266 if (csio->resid > 0) 2267 bp->bio_flags |= BIO_ERROR; 2268 if (softc->error_inject != 0) { 2269 bp->bio_error = softc->error_inject; 2270 bp->bio_resid = bp->bio_bcount; 2271 bp->bio_flags |= BIO_ERROR; 2272 softc->error_inject = 0; 2273 } 2274 2275 } 2276 2277 /* 2278 * Block out any asyncronous callbacks 2279 * while we touch the pending ccb list. 2280 */ 2281 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 2282 softc->outstanding_cmds--; 2283 if (softc->outstanding_cmds == 0) 2284 softc->flags |= DA_FLAG_WENT_IDLE; 2285 2286 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) { 2287 xpt_print(periph->path, "oustanding %d\n", 2288 softc->outstanding_cmds); 2289 } 2290 2291 if (state == DA_CCB_DELETE) { 2292 while ((bp1 = bioq_takefirst(&softc->delete_run_queue)) 2293 != NULL) { 2294 bp1->bio_resid = bp->bio_resid; 2295 bp1->bio_error = bp->bio_error; 2296 if (bp->bio_flags & BIO_ERROR) 2297 bp1->bio_flags |= BIO_ERROR; 2298 biodone(bp1); 2299 } 2300 softc->delete_running = 0; 2301 if (bp != NULL) 2302 biodone(bp); 2303 daschedule(periph); 2304 } else if (bp != NULL) 2305 biodone(bp); 2306 break; 2307 } 2308 case DA_CCB_PROBE: 2309 case DA_CCB_PROBE2: 2310 { 2311 struct scsi_read_capacity_data *rdcap; 2312 struct scsi_read_capacity_data_long *rcaplong; 2313 char announce_buf[80]; 2314 2315 rdcap = NULL; 2316 rcaplong = NULL; 2317 if (state == DA_CCB_PROBE) 2318 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr; 2319 else 2320 rcaplong = (struct scsi_read_capacity_data_long *) 2321 csio->data_ptr; 2322 2323 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 2324 struct disk_params *dp; 2325 uint32_t block_size; 2326 uint64_t maxsector; 2327 u_int lbppbe; /* LB per physical block exponent. */ 2328 u_int lalba; /* Lowest aligned LBA. */ 2329 2330 if (state == DA_CCB_PROBE) { 2331 block_size = scsi_4btoul(rdcap->length); 2332 maxsector = scsi_4btoul(rdcap->addr); 2333 lbppbe = 0; 2334 lalba = 0; 2335 2336 /* 2337 * According to SBC-2, if the standard 10 2338 * byte READ CAPACITY command returns 2^32, 2339 * we should issue the 16 byte version of 2340 * the command, since the device in question 2341 * has more sectors than can be represented 2342 * with the short version of the command. 2343 */ 2344 if (maxsector == 0xffffffff) { 2345 softc->state = DA_STATE_PROBE2; 2346 free(rdcap, M_SCSIDA); 2347 xpt_release_ccb(done_ccb); 2348 xpt_schedule(periph, priority); 2349 return; 2350 } 2351 } else { 2352 block_size = scsi_4btoul(rcaplong->length); 2353 maxsector = scsi_8btou64(rcaplong->addr); 2354 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE; 2355 lalba = scsi_2btoul(rcaplong->lalba_lbp); 2356 } 2357 2358 /* 2359 * Because GEOM code just will panic us if we 2360 * give them an 'illegal' value we'll avoid that 2361 * here. 2362 */ 2363 if (block_size == 0 && maxsector == 0) { 2364 snprintf(announce_buf, sizeof(announce_buf), 2365 "0MB (no media?)"); 2366 } else if (block_size >= MAXPHYS || block_size == 0) { 2367 xpt_print(periph->path, 2368 "unsupportable block size %ju\n", 2369 (uintmax_t) block_size); 2370 announce_buf[0] = '\0'; 2371 cam_periph_invalidate(periph); 2372 } else { 2373 /* 2374 * We pass rcaplong into dasetgeom(), 2375 * because it will only use it if it is 2376 * non-NULL. 2377 */ 2378 dasetgeom(periph, block_size, maxsector, 2379 rcaplong, sizeof(*rcaplong)); 2380 if ((lalba & SRC16_LBPME_A) 2381 && softc->delete_method == DA_DELETE_NONE) 2382 dadeletemethodset(softc, DA_DELETE_UNMAP); 2383 dp = &softc->params; 2384 snprintf(announce_buf, sizeof(announce_buf), 2385 "%juMB (%ju %u byte sectors: %dH %dS/T " 2386 "%dC)", (uintmax_t) 2387 (((uintmax_t)dp->secsize * 2388 dp->sectors) / (1024*1024)), 2389 (uintmax_t)dp->sectors, 2390 dp->secsize, dp->heads, 2391 dp->secs_per_track, dp->cylinders); 2392 } 2393 } else { 2394 int error; 2395 2396 announce_buf[0] = '\0'; 2397 2398 /* 2399 * Retry any UNIT ATTENTION type errors. They 2400 * are expected at boot. 2401 */ 2402 error = daerror(done_ccb, CAM_RETRY_SELTO, 2403 SF_RETRY_UA|SF_NO_PRINT); 2404 if (error == ERESTART) { 2405 /* 2406 * A retry was scheuled, so 2407 * just return. 2408 */ 2409 return; 2410 } else if (error != 0) { 2411 int asc, ascq; 2412 int sense_key, error_code; 2413 int have_sense; 2414 cam_status status; 2415 struct ccb_getdev cgd; 2416 2417 /* Don't wedge this device's queue */ 2418 status = done_ccb->ccb_h.status; 2419 if ((status & CAM_DEV_QFRZN) != 0) 2420 cam_release_devq(done_ccb->ccb_h.path, 2421 /*relsim_flags*/0, 2422 /*reduction*/0, 2423 /*timeout*/0, 2424 /*getcount_only*/0); 2425 2426 2427 xpt_setup_ccb(&cgd.ccb_h, 2428 done_ccb->ccb_h.path, 2429 CAM_PRIORITY_NORMAL); 2430 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 2431 xpt_action((union ccb *)&cgd); 2432 2433 if (scsi_extract_sense_ccb(done_ccb, 2434 &error_code, &sense_key, &asc, &ascq)) 2435 have_sense = TRUE; 2436 else 2437 have_sense = FALSE; 2438 2439 /* 2440 * If we tried READ CAPACITY(16) and failed, 2441 * fallback to READ CAPACITY(10). 2442 */ 2443 if ((state == DA_CCB_PROBE2) && 2444 (softc->flags & DA_FLAG_CAN_RC16) && 2445 (((csio->ccb_h.status & CAM_STATUS_MASK) == 2446 CAM_REQ_INVALID) || 2447 ((have_sense) && 2448 (error_code == SSD_CURRENT_ERROR) && 2449 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) { 2450 softc->flags &= ~DA_FLAG_CAN_RC16; 2451 softc->state = DA_STATE_PROBE; 2452 free(rdcap, M_SCSIDA); 2453 xpt_release_ccb(done_ccb); 2454 xpt_schedule(periph, priority); 2455 return; 2456 } else 2457 /* 2458 * Attach to anything that claims to be a 2459 * direct access or optical disk device, 2460 * as long as it doesn't return a "Logical 2461 * unit not supported" (0x25) error. 2462 */ 2463 if ((have_sense) && (asc != 0x25) 2464 && (error_code == SSD_CURRENT_ERROR)) { 2465 const char *sense_key_desc; 2466 const char *asc_desc; 2467 2468 scsi_sense_desc(sense_key, asc, ascq, 2469 &cgd.inq_data, 2470 &sense_key_desc, 2471 &asc_desc); 2472 snprintf(announce_buf, 2473 sizeof(announce_buf), 2474 "Attempt to query device " 2475 "size failed: %s, %s", 2476 sense_key_desc, 2477 asc_desc); 2478 } else { 2479 if (have_sense) 2480 scsi_sense_print( 2481 &done_ccb->csio); 2482 else { 2483 xpt_print(periph->path, 2484 "got CAM status %#x\n", 2485 done_ccb->ccb_h.status); 2486 } 2487 2488 xpt_print(periph->path, "fatal error, " 2489 "failed to attach to device\n"); 2490 2491 /* 2492 * Free up resources. 2493 */ 2494 cam_periph_invalidate(periph); 2495 } 2496 } 2497 } 2498 free(csio->data_ptr, M_SCSIDA); 2499 if (announce_buf[0] != '\0' && ((softc->flags & DA_FLAG_PROBED) == 0)) { 2500 /* 2501 * Create our sysctl variables, now that we know 2502 * we have successfully attached. 2503 */ 2504 /* increase the refcount */ 2505 if (cam_periph_acquire(periph) == CAM_REQ_CMP) { 2506 taskqueue_enqueue(taskqueue_thread, 2507 &softc->sysctl_task); 2508 xpt_announce_periph(periph, announce_buf); 2509 } else { 2510 xpt_print(periph->path, "fatal error, " 2511 "could not acquire reference count\n"); 2512 } 2513 } 2514 /* 2515 * Since our peripheral may be invalidated by an error 2516 * above or an external event, we must release our CCB 2517 * before releasing the probe lock on the peripheral. 2518 * The peripheral will only go away once the last lock 2519 * is removed, and we need it around for the CCB release 2520 * operation. 2521 */ 2522 xpt_release_ccb(done_ccb); 2523 softc->state = DA_STATE_NORMAL; 2524 daschedule(periph); 2525 wakeup(&softc->disk->d_mediasize); 2526 if ((softc->flags & DA_FLAG_PROBED) == 0) { 2527 softc->flags |= DA_FLAG_PROBED; 2528 cam_periph_unhold(periph); 2529 } else 2530 cam_periph_release_locked(periph); 2531 return; 2532 } 2533 case DA_CCB_WAITING: 2534 { 2535 /* Caller will release the CCB */ 2536 wakeup(&done_ccb->ccb_h.cbfcnp); 2537 return; 2538 } 2539 case DA_CCB_DUMP: 2540 /* No-op. We're polling */ 2541 return; 2542 case DA_CCB_TUR: 2543 { 2544 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2545 2546 if (daerror(done_ccb, CAM_RETRY_SELTO, 2547 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) == 2548 ERESTART) 2549 return; 2550 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 2551 cam_release_devq(done_ccb->ccb_h.path, 2552 /*relsim_flags*/0, 2553 /*reduction*/0, 2554 /*timeout*/0, 2555 /*getcount_only*/0); 2556 } 2557 xpt_release_ccb(done_ccb); 2558 cam_periph_release_locked(periph); 2559 return; 2560 } 2561 default: 2562 break; 2563 } 2564 xpt_release_ccb(done_ccb); 2565 } 2566 2567 static void 2568 dareprobe(struct cam_periph *periph) 2569 { 2570 struct da_softc *softc; 2571 cam_status status; 2572 2573 softc = (struct da_softc *)periph->softc; 2574 2575 /* Probe in progress; don't interfere. */ 2576 if ((softc->flags & DA_FLAG_PROBED) == 0) 2577 return; 2578 2579 status = cam_periph_acquire(periph); 2580 KASSERT(status == CAM_REQ_CMP, 2581 ("dareprobe: cam_periph_acquire failed")); 2582 2583 if (softc->flags & DA_FLAG_CAN_RC16) 2584 softc->state = DA_STATE_PROBE2; 2585 else 2586 softc->state = DA_STATE_PROBE; 2587 2588 xpt_schedule(periph, CAM_PRIORITY_DEV); 2589 } 2590 2591 static int 2592 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 2593 { 2594 struct da_softc *softc; 2595 struct cam_periph *periph; 2596 int error, error_code, sense_key, asc, ascq; 2597 2598 periph = xpt_path_periph(ccb->ccb_h.path); 2599 softc = (struct da_softc *)periph->softc; 2600 2601 /* 2602 * Automatically detect devices that do not support 2603 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs. 2604 */ 2605 error = 0; 2606 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) { 2607 error = cmd6workaround(ccb); 2608 } else if (scsi_extract_sense_ccb(ccb, 2609 &error_code, &sense_key, &asc, &ascq)) { 2610 if (sense_key == SSD_KEY_ILLEGAL_REQUEST) 2611 error = cmd6workaround(ccb); 2612 /* 2613 * If the target replied with CAPACITY DATA HAS CHANGED UA, 2614 * query the capacity and notify upper layers. 2615 */ 2616 else if (sense_key == SSD_KEY_UNIT_ATTENTION && 2617 asc == 0x2A && ascq == 0x09) { 2618 xpt_print(periph->path, "capacity data has changed\n"); 2619 dareprobe(periph); 2620 sense_flags |= SF_NO_PRINT; 2621 } else if (sense_key == SSD_KEY_UNIT_ATTENTION && 2622 asc == 0x28 && ascq == 0x00) 2623 disk_media_changed(softc->disk, M_NOWAIT); 2624 else if (sense_key == SSD_KEY_NOT_READY && 2625 asc == 0x3a && (softc->flags & DA_FLAG_SAW_MEDIA)) { 2626 softc->flags &= ~DA_FLAG_SAW_MEDIA; 2627 disk_media_gone(softc->disk, M_NOWAIT); 2628 } 2629 } 2630 if (error == ERESTART) 2631 return (ERESTART); 2632 2633 /* 2634 * XXX 2635 * Until we have a better way of doing pack validation, 2636 * don't treat UAs as errors. 2637 */ 2638 sense_flags |= SF_RETRY_UA; 2639 return(cam_periph_error(ccb, cam_flags, sense_flags, 2640 &softc->saved_ccb)); 2641 } 2642 2643 static void 2644 damediapoll(void *arg) 2645 { 2646 struct cam_periph *periph = arg; 2647 struct da_softc *softc = periph->softc; 2648 2649 if (!softc->tur && softc->outstanding_cmds == 0) { 2650 if (cam_periph_acquire(periph) == CAM_REQ_CMP) { 2651 softc->tur = 1; 2652 daschedule(periph); 2653 } 2654 } 2655 /* Queue us up again */ 2656 if (da_poll_period != 0) 2657 callout_schedule(&softc->mediapoll_c, da_poll_period * hz); 2658 } 2659 2660 static void 2661 daprevent(struct cam_periph *periph, int action) 2662 { 2663 struct da_softc *softc; 2664 union ccb *ccb; 2665 int error; 2666 2667 softc = (struct da_softc *)periph->softc; 2668 2669 if (((action == PR_ALLOW) 2670 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0) 2671 || ((action == PR_PREVENT) 2672 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) { 2673 return; 2674 } 2675 2676 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 2677 2678 scsi_prevent(&ccb->csio, 2679 /*retries*/1, 2680 /*cbcfp*/dadone, 2681 MSG_SIMPLE_Q_TAG, 2682 action, 2683 SSD_FULL_SIZE, 2684 5000); 2685 2686 error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO, 2687 SF_RETRY_UA | SF_QUIET_IR, softc->disk->d_devstat); 2688 2689 if (error == 0) { 2690 if (action == PR_ALLOW) 2691 softc->flags &= ~DA_FLAG_PACK_LOCKED; 2692 else 2693 softc->flags |= DA_FLAG_PACK_LOCKED; 2694 } 2695 2696 xpt_release_ccb(ccb); 2697 } 2698 2699 static void 2700 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector, 2701 struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len) 2702 { 2703 struct ccb_calc_geometry ccg; 2704 struct da_softc *softc; 2705 struct disk_params *dp; 2706 u_int lbppbe, lalba; 2707 int error; 2708 2709 softc = (struct da_softc *)periph->softc; 2710 2711 dp = &softc->params; 2712 dp->secsize = block_len; 2713 dp->sectors = maxsector + 1; 2714 if (rcaplong != NULL) { 2715 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE; 2716 lalba = scsi_2btoul(rcaplong->lalba_lbp); 2717 lalba &= SRC16_LALBA_A; 2718 } else { 2719 lbppbe = 0; 2720 lalba = 0; 2721 } 2722 2723 if (lbppbe > 0) { 2724 dp->stripesize = block_len << lbppbe; 2725 dp->stripeoffset = (dp->stripesize - block_len * lalba) % 2726 dp->stripesize; 2727 } else if (softc->quirks & DA_Q_4K) { 2728 dp->stripesize = 4096; 2729 dp->stripeoffset = 0; 2730 } else { 2731 dp->stripesize = 0; 2732 dp->stripeoffset = 0; 2733 } 2734 /* 2735 * Have the controller provide us with a geometry 2736 * for this disk. The only time the geometry 2737 * matters is when we boot and the controller 2738 * is the only one knowledgeable enough to come 2739 * up with something that will make this a bootable 2740 * device. 2741 */ 2742 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 2743 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY; 2744 ccg.block_size = dp->secsize; 2745 ccg.volume_size = dp->sectors; 2746 ccg.heads = 0; 2747 ccg.secs_per_track = 0; 2748 ccg.cylinders = 0; 2749 xpt_action((union ccb*)&ccg); 2750 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2751 /* 2752 * We don't know what went wrong here- but just pick 2753 * a geometry so we don't have nasty things like divide 2754 * by zero. 2755 */ 2756 dp->heads = 255; 2757 dp->secs_per_track = 255; 2758 dp->cylinders = dp->sectors / (255 * 255); 2759 if (dp->cylinders == 0) { 2760 dp->cylinders = 1; 2761 } 2762 } else { 2763 dp->heads = ccg.heads; 2764 dp->secs_per_track = ccg.secs_per_track; 2765 dp->cylinders = ccg.cylinders; 2766 } 2767 2768 /* 2769 * If the user supplied a read capacity buffer, and if it is 2770 * different than the previous buffer, update the data in the EDT. 2771 * If it's the same, we don't bother. This avoids sending an 2772 * update every time someone opens this device. 2773 */ 2774 if ((rcaplong != NULL) 2775 && (bcmp(rcaplong, &softc->rcaplong, 2776 min(sizeof(softc->rcaplong), rcap_len)) != 0)) { 2777 struct ccb_dev_advinfo cdai; 2778 2779 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 2780 cdai.ccb_h.func_code = XPT_DEV_ADVINFO; 2781 cdai.buftype = CDAI_TYPE_RCAPLONG; 2782 cdai.flags |= CDAI_FLAG_STORE; 2783 cdai.bufsiz = rcap_len; 2784 cdai.buf = (uint8_t *)rcaplong; 2785 xpt_action((union ccb *)&cdai); 2786 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0) 2787 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE); 2788 if (cdai.ccb_h.status != CAM_REQ_CMP) { 2789 xpt_print(periph->path, "%s: failed to set read " 2790 "capacity advinfo\n", __func__); 2791 /* Use cam_error_print() to decode the status */ 2792 cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS, 2793 CAM_EPF_ALL); 2794 } else { 2795 bcopy(rcaplong, &softc->rcaplong, 2796 min(sizeof(softc->rcaplong), rcap_len)); 2797 } 2798 } 2799 2800 softc->disk->d_sectorsize = softc->params.secsize; 2801 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors; 2802 softc->disk->d_stripesize = softc->params.stripesize; 2803 softc->disk->d_stripeoffset = softc->params.stripeoffset; 2804 /* XXX: these are not actually "firmware" values, so they may be wrong */ 2805 softc->disk->d_fwsectors = softc->params.secs_per_track; 2806 softc->disk->d_fwheads = softc->params.heads; 2807 softc->disk->d_devstat->block_size = softc->params.secsize; 2808 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE; 2809 if (softc->delete_method > DA_DELETE_DISABLE) 2810 softc->disk->d_flags |= DISKFLAG_CANDELETE; 2811 else 2812 softc->disk->d_flags &= ~DISKFLAG_CANDELETE; 2813 2814 error = disk_resize(softc->disk, M_NOWAIT); 2815 if (error != 0) 2816 xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error); 2817 } 2818 2819 static void 2820 dasendorderedtag(void *arg) 2821 { 2822 struct da_softc *softc = arg; 2823 2824 if (da_send_ordered) { 2825 if ((softc->ordered_tag_count == 0) 2826 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) { 2827 softc->flags |= DA_FLAG_NEED_OTAG; 2828 } 2829 if (softc->outstanding_cmds > 0) 2830 softc->flags &= ~DA_FLAG_WENT_IDLE; 2831 2832 softc->ordered_tag_count = 0; 2833 } 2834 /* Queue us up again */ 2835 callout_reset(&softc->sendordered_c, 2836 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL, 2837 dasendorderedtag, softc); 2838 } 2839 2840 /* 2841 * Step through all DA peripheral drivers, and if the device is still open, 2842 * sync the disk cache to physical media. 2843 */ 2844 static void 2845 dashutdown(void * arg, int howto) 2846 { 2847 struct cam_periph *periph; 2848 struct da_softc *softc; 2849 int error; 2850 2851 TAILQ_FOREACH(periph, &dadriver.units, unit_links) { 2852 union ccb ccb; 2853 2854 cam_periph_lock(periph); 2855 softc = (struct da_softc *)periph->softc; 2856 2857 /* 2858 * We only sync the cache if the drive is still open, and 2859 * if the drive is capable of it.. 2860 */ 2861 if (((softc->flags & DA_FLAG_OPEN) == 0) 2862 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) { 2863 cam_periph_unlock(periph); 2864 continue; 2865 } 2866 2867 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 2868 2869 ccb.ccb_h.ccb_state = DA_CCB_DUMP; 2870 scsi_synchronize_cache(&ccb.csio, 2871 /*retries*/0, 2872 /*cbfcnp*/dadone, 2873 MSG_SIMPLE_Q_TAG, 2874 /*begin_lba*/0, /* whole disk */ 2875 /*lb_count*/0, 2876 SSD_FULL_SIZE, 2877 60 * 60 * 1000); 2878 2879 xpt_polled_action(&ccb); 2880 2881 error = cam_periph_error(&ccb, 2882 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL); 2883 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 2884 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0, 2885 /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 2886 if (error != 0) 2887 xpt_print(periph->path, "Synchronize cache failed\n"); 2888 cam_periph_unlock(periph); 2889 } 2890 } 2891 2892 #else /* !_KERNEL */ 2893 2894 /* 2895 * XXX This is only left out of the kernel build to silence warnings. If, 2896 * for some reason this function is used in the kernel, the ifdefs should 2897 * be moved so it is included both in the kernel and userland. 2898 */ 2899 void 2900 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries, 2901 void (*cbfcnp)(struct cam_periph *, union ccb *), 2902 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave, 2903 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, 2904 u_int32_t timeout) 2905 { 2906 struct scsi_format_unit *scsi_cmd; 2907 2908 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes; 2909 scsi_cmd->opcode = FORMAT_UNIT; 2910 scsi_cmd->byte2 = byte2; 2911 scsi_ulto2b(ileave, scsi_cmd->interleave); 2912 2913 cam_fill_csio(csio, 2914 retries, 2915 cbfcnp, 2916 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE, 2917 tag_action, 2918 data_ptr, 2919 dxfer_len, 2920 sense_len, 2921 sizeof(*scsi_cmd), 2922 timeout); 2923 } 2924 2925 #endif /* _KERNEL */ 2926