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