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