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