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