1 /*- 2 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification, immediately at the beginning of the file. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_ada.h" 31 32 #include <sys/param.h> 33 34 #ifdef _KERNEL 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/bio.h> 38 #include <sys/sysctl.h> 39 #include <sys/taskqueue.h> 40 #include <sys/lock.h> 41 #include <sys/mutex.h> 42 #include <sys/conf.h> 43 #include <sys/devicestat.h> 44 #include <sys/eventhandler.h> 45 #include <sys/malloc.h> 46 #include <sys/cons.h> 47 #include <sys/proc.h> 48 #include <sys/reboot.h> 49 #include <geom/geom_disk.h> 50 #endif /* _KERNEL */ 51 52 #ifndef _KERNEL 53 #include <stdio.h> 54 #include <string.h> 55 #endif /* _KERNEL */ 56 57 #include <cam/cam.h> 58 #include <cam/cam_ccb.h> 59 #include <cam/cam_periph.h> 60 #include <cam/cam_xpt_periph.h> 61 #include <cam/cam_sim.h> 62 63 #include <cam/ata/ata_all.h> 64 65 #include <machine/md_var.h> /* geometry translation */ 66 67 #ifdef _KERNEL 68 69 #define ATA_MAX_28BIT_LBA 268435455UL 70 71 typedef enum { 72 ADA_STATE_RAHEAD, 73 ADA_STATE_WCACHE, 74 ADA_STATE_NORMAL 75 } ada_state; 76 77 typedef enum { 78 ADA_FLAG_CAN_48BIT = 0x0002, 79 ADA_FLAG_CAN_FLUSHCACHE = 0x0004, 80 ADA_FLAG_CAN_NCQ = 0x0008, 81 ADA_FLAG_CAN_DMA = 0x0010, 82 ADA_FLAG_NEED_OTAG = 0x0020, 83 ADA_FLAG_WAS_OTAG = 0x0040, 84 ADA_FLAG_CAN_TRIM = 0x0080, 85 ADA_FLAG_OPEN = 0x0100, 86 ADA_FLAG_SCTX_INIT = 0x0200, 87 ADA_FLAG_CAN_CFA = 0x0400, 88 ADA_FLAG_CAN_POWERMGT = 0x0800, 89 ADA_FLAG_CAN_DMA48 = 0x1000, 90 ADA_FLAG_DIRTY = 0x2000 91 } ada_flags; 92 93 typedef enum { 94 ADA_Q_NONE = 0x00, 95 ADA_Q_4K = 0x01, 96 } ada_quirks; 97 98 #define ADA_Q_BIT_STRING \ 99 "\020" \ 100 "\0014K" 101 102 typedef enum { 103 ADA_CCB_RAHEAD = 0x01, 104 ADA_CCB_WCACHE = 0x02, 105 ADA_CCB_BUFFER_IO = 0x03, 106 ADA_CCB_DUMP = 0x05, 107 ADA_CCB_TRIM = 0x06, 108 ADA_CCB_TYPE_MASK = 0x0F, 109 } ada_ccb_state; 110 111 /* Offsets into our private area for storing information */ 112 #define ccb_state ppriv_field0 113 #define ccb_bp ppriv_ptr1 114 115 struct disk_params { 116 u_int8_t heads; 117 u_int8_t secs_per_track; 118 u_int32_t cylinders; 119 u_int32_t secsize; /* Number of bytes/logical sector */ 120 u_int64_t sectors; /* Total number sectors */ 121 }; 122 123 #define TRIM_MAX_BLOCKS 8 124 #define TRIM_MAX_RANGES (TRIM_MAX_BLOCKS * ATA_DSM_BLK_RANGES) 125 struct trim_request { 126 uint8_t data[TRIM_MAX_RANGES * ATA_DSM_RANGE_SIZE]; 127 TAILQ_HEAD(, bio) bps; 128 }; 129 130 struct ada_softc { 131 struct bio_queue_head bio_queue; 132 struct bio_queue_head trim_queue; 133 int outstanding_cmds; /* Number of active commands */ 134 int refcount; /* Active xpt_action() calls */ 135 ada_state state; 136 ada_flags flags; 137 ada_quirks quirks; 138 int sort_io_queue; 139 int trim_max_ranges; 140 int trim_running; 141 int read_ahead; 142 int write_cache; 143 #ifdef ADA_TEST_FAILURE 144 int force_read_error; 145 int force_write_error; 146 int periodic_read_error; 147 int periodic_read_count; 148 #endif 149 struct disk_params params; 150 struct disk *disk; 151 struct task sysctl_task; 152 struct sysctl_ctx_list sysctl_ctx; 153 struct sysctl_oid *sysctl_tree; 154 struct callout sendordered_c; 155 struct trim_request trim_req; 156 }; 157 158 struct ada_quirk_entry { 159 struct scsi_inquiry_pattern inq_pat; 160 ada_quirks quirks; 161 }; 162 163 static struct ada_quirk_entry ada_quirk_table[] = 164 { 165 { 166 /* Hitachi Advanced Format (4k) drives */ 167 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" }, 168 /*quirks*/ADA_Q_4K 169 }, 170 { 171 /* Samsung Advanced Format (4k) drives */ 172 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD155UI*", "*" }, 173 /*quirks*/ADA_Q_4K 174 }, 175 { 176 /* Samsung Advanced Format (4k) drives */ 177 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" }, 178 /*quirks*/ADA_Q_4K 179 }, 180 { 181 /* Seagate Barracuda Green Advanced Format (4k) drives */ 182 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" }, 183 /*quirks*/ADA_Q_4K 184 }, 185 { 186 /* Seagate Barracuda Advanced Format (4k) drives */ 187 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???DM*", "*" }, 188 /*quirks*/ADA_Q_4K 189 }, 190 { 191 /* Seagate Barracuda Advanced Format (4k) drives */ 192 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DM*", "*" }, 193 /*quirks*/ADA_Q_4K 194 }, 195 { 196 /* Seagate Momentus Advanced Format (4k) drives */ 197 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" }, 198 /*quirks*/ADA_Q_4K 199 }, 200 { 201 /* Seagate Momentus Advanced Format (4k) drives */ 202 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" }, 203 /*quirks*/ADA_Q_4K 204 }, 205 { 206 /* Seagate Momentus Advanced Format (4k) drives */ 207 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640423AS*", "*" }, 208 /*quirks*/ADA_Q_4K 209 }, 210 { 211 /* Seagate Momentus Advanced Format (4k) drives */ 212 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640424AS*", "*" }, 213 /*quirks*/ADA_Q_4K 214 }, 215 { 216 /* Seagate Momentus Advanced Format (4k) drives */ 217 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" }, 218 /*quirks*/ADA_Q_4K 219 }, 220 { 221 /* Seagate Momentus Advanced Format (4k) drives */ 222 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" }, 223 /*quirks*/ADA_Q_4K 224 }, 225 { 226 /* Seagate Momentus Advanced Format (4k) drives */ 227 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750423AS*", "*" }, 228 /*quirks*/ADA_Q_4K 229 }, 230 { 231 /* Seagate Momentus Thin Advanced Format (4k) drives */ 232 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" }, 233 /*quirks*/ADA_Q_4K 234 }, 235 { 236 /* WDC Caviar Green Advanced Format (4k) drives */ 237 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" }, 238 /*quirks*/ADA_Q_4K 239 }, 240 { 241 /* WDC Caviar Green Advanced Format (4k) drives */ 242 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" }, 243 /*quirks*/ADA_Q_4K 244 }, 245 { 246 /* WDC Caviar Green Advanced Format (4k) drives */ 247 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" }, 248 /*quirks*/ADA_Q_4K 249 }, 250 { 251 /* WDC Caviar Green Advanced Format (4k) drives */ 252 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" }, 253 /*quirks*/ADA_Q_4K 254 }, 255 { 256 /* WDC Scorpio Black Advanced Format (4k) drives */ 257 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" }, 258 /*quirks*/ADA_Q_4K 259 }, 260 { 261 /* WDC Scorpio Black Advanced Format (4k) drives */ 262 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" }, 263 /*quirks*/ADA_Q_4K 264 }, 265 { 266 /* WDC Scorpio Blue Advanced Format (4k) drives */ 267 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" }, 268 /*quirks*/ADA_Q_4K 269 }, 270 { 271 /* WDC Scorpio Blue Advanced Format (4k) drives */ 272 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" }, 273 /*quirks*/ADA_Q_4K 274 }, 275 /* SSDs */ 276 { 277 /* 278 * Corsair Force 2 SSDs 279 * 4k optimised & trim only works in 4k requests + 4k aligned 280 */ 281 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair CSSD-F*", "*" }, 282 /*quirks*/ADA_Q_4K 283 }, 284 { 285 /* 286 * Corsair Force 3 SSDs 287 * 4k optimised & trim only works in 4k requests + 4k aligned 288 */ 289 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force 3*", "*" }, 290 /*quirks*/ADA_Q_4K 291 }, 292 { 293 /* 294 * Corsair Neutron GTX SSDs 295 * 4k optimised & trim only works in 4k requests + 4k aligned 296 */ 297 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" }, 298 /*quirks*/ADA_Q_4K 299 }, 300 { 301 /* 302 * Corsair Force GT & GS SSDs 303 * 4k optimised & trim only works in 4k requests + 4k aligned 304 */ 305 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force G*", "*" }, 306 /*quirks*/ADA_Q_4K 307 }, 308 { 309 /* 310 * Crucial M4 SSDs 311 * 4k optimised & trim only works in 4k requests + 4k aligned 312 */ 313 { T_DIRECT, SIP_MEDIA_FIXED, "*", "M4-CT???M4SSD2*", "*" }, 314 /*quirks*/ADA_Q_4K 315 }, 316 { 317 /* 318 * Crucial RealSSD C300 SSDs 319 * 4k optimised 320 */ 321 { T_DIRECT, SIP_MEDIA_FIXED, "*", "C300-CTFDDAC???MAG*", 322 "*" }, /*quirks*/ADA_Q_4K 323 }, 324 { 325 /* 326 * Intel 320 Series SSDs 327 * 4k optimised & trim only works in 4k requests + 4k aligned 328 */ 329 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2CW*", "*" }, 330 /*quirks*/ADA_Q_4K 331 }, 332 { 333 /* 334 * Intel 330 Series SSDs 335 * 4k optimised & trim only works in 4k requests + 4k aligned 336 */ 337 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2CT*", "*" }, 338 /*quirks*/ADA_Q_4K 339 }, 340 { 341 /* 342 * Intel 510 Series SSDs 343 * 4k optimised & trim only works in 4k requests + 4k aligned 344 */ 345 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2MH*", "*" }, 346 /*quirks*/ADA_Q_4K 347 }, 348 { 349 /* 350 * Intel 520 Series SSDs 351 * 4k optimised & trim only works in 4k requests + 4k aligned 352 */ 353 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2BW*", "*" }, 354 /*quirks*/ADA_Q_4K 355 }, 356 { 357 /* 358 * Intel X25-M Series SSDs 359 * 4k optimised & trim only works in 4k requests + 4k aligned 360 */ 361 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2M*", "*" }, 362 /*quirks*/ADA_Q_4K 363 }, 364 { 365 /* 366 * Kingston E100 Series SSDs 367 * 4k optimised & trim only works in 4k requests + 4k aligned 368 */ 369 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SE100S3*", "*" }, 370 /*quirks*/ADA_Q_4K 371 }, 372 { 373 /* 374 * Kingston HyperX 3k SSDs 375 * 4k optimised & trim only works in 4k requests + 4k aligned 376 */ 377 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SH103S3*", "*" }, 378 /*quirks*/ADA_Q_4K 379 }, 380 { 381 /* 382 * Marvell SSDs (entry taken from OpenSolaris) 383 * 4k optimised & trim only works in 4k requests + 4k aligned 384 */ 385 { T_DIRECT, SIP_MEDIA_FIXED, "*", "MARVELL SD88SA02*", "*" }, 386 /*quirks*/ADA_Q_4K 387 }, 388 { 389 /* 390 * OCZ Agility 2 SSDs 391 * 4k optimised & trim only works in 4k requests + 4k aligned 392 */ 393 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" }, 394 /*quirks*/ADA_Q_4K 395 }, 396 { 397 /* 398 * OCZ Agility 3 SSDs 399 * 4k optimised & trim only works in 4k requests + 4k aligned 400 */ 401 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY3*", "*" }, 402 /*quirks*/ADA_Q_4K 403 }, 404 { 405 /* 406 * OCZ Deneva R Series SSDs 407 * 4k optimised & trim only works in 4k requests + 4k aligned 408 */ 409 { T_DIRECT, SIP_MEDIA_FIXED, "*", "DENRSTE251M45*", "*" }, 410 /*quirks*/ADA_Q_4K 411 }, 412 { 413 /* 414 * OCZ Vertex 2 SSDs (inc pro series) 415 * 4k optimised & trim only works in 4k requests + 4k aligned 416 */ 417 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ?VERTEX2*", "*" }, 418 /*quirks*/ADA_Q_4K 419 }, 420 { 421 /* 422 * OCZ Vertex 3 SSDs 423 * 4k optimised & trim only works in 4k requests + 4k aligned 424 */ 425 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX3*", "*" }, 426 /*quirks*/ADA_Q_4K 427 }, 428 { 429 /* 430 * OCZ Vertex 4 SSDs 431 * 4k optimised & trim only works in 4k requests + 4k aligned 432 */ 433 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX4*", "*" }, 434 /*quirks*/ADA_Q_4K 435 }, 436 { 437 /* 438 * Samsung 830 Series SSDs 439 * 4k optimised 440 */ 441 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG SSD 830 Series*", "*" }, 442 /*quirks*/ADA_Q_4K 443 }, 444 { 445 /* 446 * Samsung 840 SSDs 447 * 4k optimised 448 */ 449 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 840*", "*" }, 450 /*quirks*/ADA_Q_4K 451 }, 452 { 453 /* 454 * Samsung 843T Series SSDs 455 * 4k optimised 456 */ 457 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7WD*", "*" }, 458 /*quirks*/ADA_Q_4K 459 }, 460 { 461 /* 462 * Samsung 850 SSDs 463 * 4k optimised 464 */ 465 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 850*", "*" }, 466 /*quirks*/ADA_Q_4K 467 }, 468 { 469 /* 470 * Samsung PM853T Series SSDs 471 * 4k optimised 472 */ 473 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7GE*", "*" }, 474 /*quirks*/ADA_Q_4K 475 }, 476 { 477 /* 478 * SuperTalent TeraDrive CT SSDs 479 * 4k optimised & trim only works in 4k requests + 4k aligned 480 */ 481 { T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" }, 482 /*quirks*/ADA_Q_4K 483 }, 484 { 485 /* 486 * XceedIOPS SATA SSDs 487 * 4k optimised 488 */ 489 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" }, 490 /*quirks*/ADA_Q_4K 491 }, 492 { 493 /* Default */ 494 { 495 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, 496 /*vendor*/"*", /*product*/"*", /*revision*/"*" 497 }, 498 /*quirks*/0 499 }, 500 }; 501 502 static disk_strategy_t adastrategy; 503 static dumper_t adadump; 504 static periph_init_t adainit; 505 static void adaasync(void *callback_arg, u_int32_t code, 506 struct cam_path *path, void *arg); 507 static void adasysctlinit(void *context, int pending); 508 static periph_ctor_t adaregister; 509 static periph_dtor_t adacleanup; 510 static periph_start_t adastart; 511 static periph_oninv_t adaoninvalidate; 512 static void adadone(struct cam_periph *periph, 513 union ccb *done_ccb); 514 static int adaerror(union ccb *ccb, u_int32_t cam_flags, 515 u_int32_t sense_flags); 516 static void adagetparams(struct cam_periph *periph, 517 struct ccb_getdev *cgd); 518 static timeout_t adasendorderedtag; 519 static void adashutdown(void *arg, int howto); 520 static void adasuspend(void *arg); 521 static void adaresume(void *arg); 522 523 #ifndef ADA_DEFAULT_LEGACY_ALIASES 524 #define ADA_DEFAULT_LEGACY_ALIASES 1 525 #endif 526 527 #ifndef ADA_DEFAULT_TIMEOUT 528 #define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */ 529 #endif 530 531 #ifndef ADA_DEFAULT_RETRY 532 #define ADA_DEFAULT_RETRY 4 533 #endif 534 535 #ifndef ADA_DEFAULT_SEND_ORDERED 536 #define ADA_DEFAULT_SEND_ORDERED 1 537 #endif 538 539 #ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN 540 #define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1 541 #endif 542 543 #ifndef ADA_DEFAULT_SPINDOWN_SUSPEND 544 #define ADA_DEFAULT_SPINDOWN_SUSPEND 1 545 #endif 546 547 #ifndef ADA_DEFAULT_READ_AHEAD 548 #define ADA_DEFAULT_READ_AHEAD 1 549 #endif 550 551 #ifndef ADA_DEFAULT_WRITE_CACHE 552 #define ADA_DEFAULT_WRITE_CACHE 1 553 #endif 554 555 #define ADA_RA (softc->read_ahead >= 0 ? \ 556 softc->read_ahead : ada_read_ahead) 557 #define ADA_WC (softc->write_cache >= 0 ? \ 558 softc->write_cache : ada_write_cache) 559 #define ADA_SIO (softc->sort_io_queue >= 0 ? \ 560 softc->sort_io_queue : cam_sort_io_queues) 561 562 /* 563 * Most platforms map firmware geometry to actual, but some don't. If 564 * not overridden, default to nothing. 565 */ 566 #ifndef ata_disk_firmware_geom_adjust 567 #define ata_disk_firmware_geom_adjust(disk) 568 #endif 569 570 static int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES; 571 static int ada_retry_count = ADA_DEFAULT_RETRY; 572 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT; 573 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED; 574 static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN; 575 static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND; 576 static int ada_read_ahead = ADA_DEFAULT_READ_AHEAD; 577 static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE; 578 579 static SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0, 580 "CAM Direct Access Disk driver"); 581 SYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RWTUN, 582 &ada_legacy_aliases, 0, "Create legacy-like device aliases"); 583 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RWTUN, 584 &ada_retry_count, 0, "Normal I/O retry count"); 585 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RWTUN, 586 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)"); 587 SYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RWTUN, 588 &ada_send_ordered, 0, "Send Ordered Tags"); 589 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RWTUN, 590 &ada_spindown_shutdown, 0, "Spin down upon shutdown"); 591 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RWTUN, 592 &ada_spindown_suspend, 0, "Spin down upon suspend"); 593 SYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RWTUN, 594 &ada_read_ahead, 0, "Enable disk read-ahead"); 595 SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RWTUN, 596 &ada_write_cache, 0, "Enable disk write cache"); 597 598 /* 599 * ADA_ORDEREDTAG_INTERVAL determines how often, relative 600 * to the default timeout, we check to see whether an ordered 601 * tagged transaction is appropriate to prevent simple tag 602 * starvation. Since we'd like to ensure that there is at least 603 * 1/2 of the timeout length left for a starved transaction to 604 * complete after we've sent an ordered tag, we must poll at least 605 * four times in every timeout period. This takes care of the worst 606 * case where a starved transaction starts during an interval that 607 * meets the requirement "don't send an ordered tag" test so it takes 608 * us two intervals to determine that a tag must be sent. 609 */ 610 #ifndef ADA_ORDEREDTAG_INTERVAL 611 #define ADA_ORDEREDTAG_INTERVAL 4 612 #endif 613 614 static struct periph_driver adadriver = 615 { 616 adainit, "ada", 617 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0 618 }; 619 620 PERIPHDRIVER_DECLARE(ada, adadriver); 621 622 static MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers"); 623 624 static int 625 adaopen(struct disk *dp) 626 { 627 struct cam_periph *periph; 628 struct ada_softc *softc; 629 int error; 630 631 periph = (struct cam_periph *)dp->d_drv1; 632 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 633 return(ENXIO); 634 } 635 636 cam_periph_lock(periph); 637 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) { 638 cam_periph_unlock(periph); 639 cam_periph_release(periph); 640 return (error); 641 } 642 643 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 644 ("adaopen\n")); 645 646 softc = (struct ada_softc *)periph->softc; 647 softc->flags |= ADA_FLAG_OPEN; 648 649 cam_periph_unhold(periph); 650 cam_periph_unlock(periph); 651 return (0); 652 } 653 654 static int 655 adaclose(struct disk *dp) 656 { 657 struct cam_periph *periph; 658 struct ada_softc *softc; 659 union ccb *ccb; 660 int error; 661 662 periph = (struct cam_periph *)dp->d_drv1; 663 softc = (struct ada_softc *)periph->softc; 664 cam_periph_lock(periph); 665 666 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 667 ("adaclose\n")); 668 669 /* We only sync the cache if the drive is capable of it. */ 670 if ((softc->flags & ADA_FLAG_DIRTY) != 0 && 671 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 && 672 (periph->flags & CAM_PERIPH_INVALID) == 0 && 673 cam_periph_hold(periph, PRIBIO) == 0) { 674 675 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 676 cam_fill_ataio(&ccb->ataio, 677 1, 678 adadone, 679 CAM_DIR_NONE, 680 0, 681 NULL, 682 0, 683 ada_default_timeout*1000); 684 685 if (softc->flags & ADA_FLAG_CAN_48BIT) 686 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 687 else 688 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 689 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 690 /*sense_flags*/0, softc->disk->d_devstat); 691 692 if (error != 0) 693 xpt_print(periph->path, "Synchronize cache failed\n"); 694 else 695 softc->flags &= ~ADA_FLAG_DIRTY; 696 xpt_release_ccb(ccb); 697 cam_periph_unhold(periph); 698 } 699 700 softc->flags &= ~ADA_FLAG_OPEN; 701 702 while (softc->refcount != 0) 703 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "adaclose", 1); 704 cam_periph_unlock(periph); 705 cam_periph_release(periph); 706 return (0); 707 } 708 709 static void 710 adaschedule(struct cam_periph *periph) 711 { 712 struct ada_softc *softc = (struct ada_softc *)periph->softc; 713 714 if (softc->state != ADA_STATE_NORMAL) 715 return; 716 717 /* Check if we have more work to do. */ 718 if (bioq_first(&softc->bio_queue) || 719 (!softc->trim_running && bioq_first(&softc->trim_queue))) { 720 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 721 } 722 } 723 724 /* 725 * Actually translate the requested transfer into one the physical driver 726 * can understand. The transfer is described by a buf and will include 727 * only one physical transfer. 728 */ 729 static void 730 adastrategy(struct bio *bp) 731 { 732 struct cam_periph *periph; 733 struct ada_softc *softc; 734 735 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 736 softc = (struct ada_softc *)periph->softc; 737 738 cam_periph_lock(periph); 739 740 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp)); 741 742 /* 743 * If the device has been made invalid, error out 744 */ 745 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 746 cam_periph_unlock(periph); 747 biofinish(bp, NULL, ENXIO); 748 return; 749 } 750 751 /* 752 * Place it in the queue of disk activities for this disk 753 */ 754 if (bp->bio_cmd == BIO_DELETE) { 755 KASSERT((softc->flags & ADA_FLAG_CAN_TRIM) || 756 ((softc->flags & ADA_FLAG_CAN_CFA) && 757 !(softc->flags & ADA_FLAG_CAN_48BIT)), 758 ("BIO_DELETE but no supported TRIM method.")); 759 bioq_disksort(&softc->trim_queue, bp); 760 } else { 761 if (ADA_SIO) 762 bioq_disksort(&softc->bio_queue, bp); 763 else 764 bioq_insert_tail(&softc->bio_queue, bp); 765 } 766 767 /* 768 * Schedule ourselves for performing the work. 769 */ 770 adaschedule(periph); 771 cam_periph_unlock(periph); 772 773 return; 774 } 775 776 static int 777 adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) 778 { 779 struct cam_periph *periph; 780 struct ada_softc *softc; 781 u_int secsize; 782 union ccb ccb; 783 struct disk *dp; 784 uint64_t lba; 785 uint16_t count; 786 int error = 0; 787 788 dp = arg; 789 periph = dp->d_drv1; 790 softc = (struct ada_softc *)periph->softc; 791 cam_periph_lock(periph); 792 secsize = softc->params.secsize; 793 lba = offset / secsize; 794 count = length / secsize; 795 796 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 797 cam_periph_unlock(periph); 798 return (ENXIO); 799 } 800 801 if (length > 0) { 802 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 803 ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 804 cam_fill_ataio(&ccb.ataio, 805 0, 806 adadone, 807 CAM_DIR_OUT, 808 0, 809 (u_int8_t *) virtual, 810 length, 811 ada_default_timeout*1000); 812 if ((softc->flags & ADA_FLAG_CAN_48BIT) && 813 (lba + count >= ATA_MAX_28BIT_LBA || 814 count >= 256)) { 815 ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48, 816 0, lba, count); 817 } else { 818 ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA, 819 0, lba, count); 820 } 821 xpt_polled_action(&ccb); 822 823 error = cam_periph_error(&ccb, 824 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 825 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 826 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0, 827 /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 828 if (error != 0) 829 printf("Aborting dump due to I/O error.\n"); 830 831 cam_periph_unlock(periph); 832 return (error); 833 } 834 835 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) { 836 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 837 838 ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 839 cam_fill_ataio(&ccb.ataio, 840 0, 841 adadone, 842 CAM_DIR_NONE, 843 0, 844 NULL, 845 0, 846 ada_default_timeout*1000); 847 848 if (softc->flags & ADA_FLAG_CAN_48BIT) 849 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0); 850 else 851 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0); 852 xpt_polled_action(&ccb); 853 854 error = cam_periph_error(&ccb, 855 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 856 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 857 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0, 858 /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 859 if (error != 0) 860 xpt_print(periph->path, "Synchronize cache failed\n"); 861 } 862 cam_periph_unlock(periph); 863 return (error); 864 } 865 866 static void 867 adainit(void) 868 { 869 cam_status status; 870 871 /* 872 * Install a global async callback. This callback will 873 * receive async callbacks like "new device found". 874 */ 875 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL); 876 877 if (status != CAM_REQ_CMP) { 878 printf("ada: Failed to attach master async callback " 879 "due to status 0x%x!\n", status); 880 } else if (ada_send_ordered) { 881 882 /* Register our event handlers */ 883 if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend, 884 NULL, EVENTHANDLER_PRI_LAST)) == NULL) 885 printf("adainit: power event registration failed!\n"); 886 if ((EVENTHANDLER_REGISTER(power_resume, adaresume, 887 NULL, EVENTHANDLER_PRI_LAST)) == NULL) 888 printf("adainit: power event registration failed!\n"); 889 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown, 890 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 891 printf("adainit: shutdown event registration failed!\n"); 892 } 893 } 894 895 /* 896 * Callback from GEOM, called when it has finished cleaning up its 897 * resources. 898 */ 899 static void 900 adadiskgonecb(struct disk *dp) 901 { 902 struct cam_periph *periph; 903 904 periph = (struct cam_periph *)dp->d_drv1; 905 906 cam_periph_release(periph); 907 } 908 909 static void 910 adaoninvalidate(struct cam_periph *periph) 911 { 912 struct ada_softc *softc; 913 914 softc = (struct ada_softc *)periph->softc; 915 916 /* 917 * De-register any async callbacks. 918 */ 919 xpt_register_async(0, adaasync, periph, periph->path); 920 921 /* 922 * Return all queued I/O with ENXIO. 923 * XXX Handle any transactions queued to the card 924 * with XPT_ABORT_CCB. 925 */ 926 bioq_flush(&softc->bio_queue, NULL, ENXIO); 927 bioq_flush(&softc->trim_queue, NULL, ENXIO); 928 929 disk_gone(softc->disk); 930 } 931 932 static void 933 adacleanup(struct cam_periph *periph) 934 { 935 struct ada_softc *softc; 936 937 softc = (struct ada_softc *)periph->softc; 938 939 cam_periph_unlock(periph); 940 941 /* 942 * If we can't free the sysctl tree, oh well... 943 */ 944 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0 945 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) { 946 xpt_print(periph->path, "can't remove sysctl context\n"); 947 } 948 949 disk_destroy(softc->disk); 950 callout_drain(&softc->sendordered_c); 951 free(softc, M_DEVBUF); 952 cam_periph_lock(periph); 953 } 954 955 static void 956 adaasync(void *callback_arg, u_int32_t code, 957 struct cam_path *path, void *arg) 958 { 959 struct ccb_getdev cgd; 960 struct cam_periph *periph; 961 struct ada_softc *softc; 962 963 periph = (struct cam_periph *)callback_arg; 964 switch (code) { 965 case AC_FOUND_DEVICE: 966 { 967 struct ccb_getdev *cgd; 968 cam_status status; 969 970 cgd = (struct ccb_getdev *)arg; 971 if (cgd == NULL) 972 break; 973 974 if (cgd->protocol != PROTO_ATA) 975 break; 976 977 /* 978 * Allocate a peripheral instance for 979 * this device and start the probe 980 * process. 981 */ 982 status = cam_periph_alloc(adaregister, adaoninvalidate, 983 adacleanup, adastart, 984 "ada", CAM_PERIPH_BIO, 985 path, adaasync, 986 AC_FOUND_DEVICE, cgd); 987 988 if (status != CAM_REQ_CMP 989 && status != CAM_REQ_INPROG) 990 printf("adaasync: Unable to attach to new device " 991 "due to status 0x%x\n", status); 992 break; 993 } 994 case AC_GETDEV_CHANGED: 995 { 996 softc = (struct ada_softc *)periph->softc; 997 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 998 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 999 xpt_action((union ccb *)&cgd); 1000 1001 if ((cgd.ident_data.capabilities1 & ATA_SUPPORT_DMA) && 1002 (cgd.inq_flags & SID_DMA)) 1003 softc->flags |= ADA_FLAG_CAN_DMA; 1004 else 1005 softc->flags &= ~ADA_FLAG_CAN_DMA; 1006 if (cgd.ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) { 1007 softc->flags |= ADA_FLAG_CAN_48BIT; 1008 if (cgd.inq_flags & SID_DMA48) 1009 softc->flags |= ADA_FLAG_CAN_DMA48; 1010 else 1011 softc->flags &= ~ADA_FLAG_CAN_DMA48; 1012 } else 1013 softc->flags &= ~(ADA_FLAG_CAN_48BIT | 1014 ADA_FLAG_CAN_DMA48); 1015 if ((cgd.ident_data.satacapabilities & ATA_SUPPORT_NCQ) && 1016 (cgd.inq_flags & SID_DMA) && (cgd.inq_flags & SID_CmdQue)) 1017 softc->flags |= ADA_FLAG_CAN_NCQ; 1018 else 1019 softc->flags &= ~ADA_FLAG_CAN_NCQ; 1020 if ((cgd.ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) && 1021 (cgd.inq_flags & SID_DMA)) 1022 softc->flags |= ADA_FLAG_CAN_TRIM; 1023 else 1024 softc->flags &= ~ADA_FLAG_CAN_TRIM; 1025 1026 cam_periph_async(periph, code, path, arg); 1027 break; 1028 } 1029 case AC_ADVINFO_CHANGED: 1030 { 1031 uintptr_t buftype; 1032 1033 buftype = (uintptr_t)arg; 1034 if (buftype == CDAI_TYPE_PHYS_PATH) { 1035 struct ada_softc *softc; 1036 1037 softc = periph->softc; 1038 disk_attr_changed(softc->disk, "GEOM::physpath", 1039 M_NOWAIT); 1040 } 1041 break; 1042 } 1043 case AC_SENT_BDR: 1044 case AC_BUS_RESET: 1045 { 1046 softc = (struct ada_softc *)periph->softc; 1047 cam_periph_async(periph, code, path, arg); 1048 if (softc->state != ADA_STATE_NORMAL) 1049 break; 1050 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1051 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1052 xpt_action((union ccb *)&cgd); 1053 if (ADA_RA >= 0 && 1054 cgd.ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) 1055 softc->state = ADA_STATE_RAHEAD; 1056 else if (ADA_WC >= 0 && 1057 cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) 1058 softc->state = ADA_STATE_WCACHE; 1059 else 1060 break; 1061 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 1062 softc->state = ADA_STATE_NORMAL; 1063 else 1064 xpt_schedule(periph, CAM_PRIORITY_DEV); 1065 } 1066 default: 1067 cam_periph_async(periph, code, path, arg); 1068 break; 1069 } 1070 } 1071 1072 static void 1073 adasysctlinit(void *context, int pending) 1074 { 1075 struct cam_periph *periph; 1076 struct ada_softc *softc; 1077 char tmpstr[80], tmpstr2[80]; 1078 1079 periph = (struct cam_periph *)context; 1080 1081 /* periph was held for us when this task was enqueued */ 1082 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 1083 cam_periph_release(periph); 1084 return; 1085 } 1086 1087 softc = (struct ada_softc *)periph->softc; 1088 snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number); 1089 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 1090 1091 sysctl_ctx_init(&softc->sysctl_ctx); 1092 softc->flags |= ADA_FLAG_SCTX_INIT; 1093 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1094 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2, 1095 CTLFLAG_RD, 0, tmpstr); 1096 if (softc->sysctl_tree == NULL) { 1097 printf("adasysctlinit: unable to allocate sysctl tree\n"); 1098 cam_periph_release(periph); 1099 return; 1100 } 1101 1102 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1103 OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE, 1104 &softc->read_ahead, 0, "Enable disk read ahead."); 1105 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1106 OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE, 1107 &softc->write_cache, 0, "Enable disk write cache."); 1108 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1109 OID_AUTO, "sort_io_queue", CTLFLAG_RW | CTLFLAG_MPSAFE, 1110 &softc->sort_io_queue, 0, 1111 "Sort IO queue to try and optimise disk access patterns"); 1112 #ifdef ADA_TEST_FAILURE 1113 /* 1114 * Add a 'door bell' sysctl which allows one to set it from userland 1115 * and cause something bad to happen. For the moment, we only allow 1116 * whacking the next read or write. 1117 */ 1118 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1119 OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1120 &softc->force_read_error, 0, 1121 "Force a read error for the next N reads."); 1122 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1123 OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1124 &softc->force_write_error, 0, 1125 "Force a write error for the next N writes."); 1126 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1127 OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1128 &softc->periodic_read_error, 0, 1129 "Force a read error every N reads (don't set too low)."); 1130 #endif 1131 cam_periph_release(periph); 1132 } 1133 1134 static int 1135 adagetattr(struct bio *bp) 1136 { 1137 int ret; 1138 struct cam_periph *periph; 1139 1140 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 1141 cam_periph_lock(periph); 1142 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute, 1143 periph->path); 1144 cam_periph_unlock(periph); 1145 if (ret == 0) 1146 bp->bio_completed = bp->bio_length; 1147 return ret; 1148 } 1149 1150 static cam_status 1151 adaregister(struct cam_periph *periph, void *arg) 1152 { 1153 struct ada_softc *softc; 1154 struct ccb_pathinq cpi; 1155 struct ccb_getdev *cgd; 1156 char announce_buf[80], buf1[32]; 1157 struct disk_params *dp; 1158 caddr_t match; 1159 u_int maxio; 1160 int legacy_id, quirks; 1161 1162 cgd = (struct ccb_getdev *)arg; 1163 if (cgd == NULL) { 1164 printf("adaregister: no getdev CCB, can't register device\n"); 1165 return(CAM_REQ_CMP_ERR); 1166 } 1167 1168 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF, 1169 M_NOWAIT|M_ZERO); 1170 1171 if (softc == NULL) { 1172 printf("adaregister: Unable to probe new device. " 1173 "Unable to allocate softc\n"); 1174 return(CAM_REQ_CMP_ERR); 1175 } 1176 1177 bioq_init(&softc->bio_queue); 1178 bioq_init(&softc->trim_queue); 1179 1180 if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) && 1181 (cgd->inq_flags & SID_DMA)) 1182 softc->flags |= ADA_FLAG_CAN_DMA; 1183 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) { 1184 softc->flags |= ADA_FLAG_CAN_48BIT; 1185 if (cgd->inq_flags & SID_DMA48) 1186 softc->flags |= ADA_FLAG_CAN_DMA48; 1187 } 1188 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE) 1189 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE; 1190 if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT) 1191 softc->flags |= ADA_FLAG_CAN_POWERMGT; 1192 if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) && 1193 (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue)) 1194 softc->flags |= ADA_FLAG_CAN_NCQ; 1195 if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) && 1196 (cgd->inq_flags & SID_DMA)) { 1197 softc->flags |= ADA_FLAG_CAN_TRIM; 1198 softc->trim_max_ranges = TRIM_MAX_RANGES; 1199 if (cgd->ident_data.max_dsm_blocks != 0) { 1200 softc->trim_max_ranges = 1201 min(cgd->ident_data.max_dsm_blocks * 1202 ATA_DSM_BLK_RANGES, softc->trim_max_ranges); 1203 } 1204 } 1205 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA) 1206 softc->flags |= ADA_FLAG_CAN_CFA; 1207 1208 periph->softc = softc; 1209 1210 /* 1211 * See if this device has any quirks. 1212 */ 1213 match = cam_quirkmatch((caddr_t)&cgd->ident_data, 1214 (caddr_t)ada_quirk_table, 1215 sizeof(ada_quirk_table)/sizeof(*ada_quirk_table), 1216 sizeof(*ada_quirk_table), ata_identify_match); 1217 if (match != NULL) 1218 softc->quirks = ((struct ada_quirk_entry *)match)->quirks; 1219 else 1220 softc->quirks = ADA_Q_NONE; 1221 1222 bzero(&cpi, sizeof(cpi)); 1223 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE); 1224 cpi.ccb_h.func_code = XPT_PATH_INQ; 1225 xpt_action((union ccb *)&cpi); 1226 1227 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph); 1228 1229 /* 1230 * Register this media as a disk 1231 */ 1232 (void)cam_periph_hold(periph, PRIBIO); 1233 cam_periph_unlock(periph); 1234 snprintf(announce_buf, sizeof(announce_buf), 1235 "kern.cam.ada.%d.quirks", periph->unit_number); 1236 quirks = softc->quirks; 1237 TUNABLE_INT_FETCH(announce_buf, &quirks); 1238 softc->quirks = quirks; 1239 softc->read_ahead = -1; 1240 snprintf(announce_buf, sizeof(announce_buf), 1241 "kern.cam.ada.%d.read_ahead", periph->unit_number); 1242 TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead); 1243 softc->write_cache = -1; 1244 snprintf(announce_buf, sizeof(announce_buf), 1245 "kern.cam.ada.%d.write_cache", periph->unit_number); 1246 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache); 1247 /* Disable queue sorting for non-rotational media by default. */ 1248 if (cgd->ident_data.media_rotation_rate == ATA_RATE_NON_ROTATING) 1249 softc->sort_io_queue = 0; 1250 else 1251 softc->sort_io_queue = -1; 1252 adagetparams(periph, cgd); 1253 softc->disk = disk_alloc(); 1254 softc->disk->d_rotation_rate = cgd->ident_data.media_rotation_rate; 1255 softc->disk->d_devstat = devstat_new_entry(periph->periph_name, 1256 periph->unit_number, softc->params.secsize, 1257 DEVSTAT_ALL_SUPPORTED, 1258 DEVSTAT_TYPE_DIRECT | 1259 XPORT_DEVSTAT_TYPE(cpi.transport), 1260 DEVSTAT_PRIORITY_DISK); 1261 softc->disk->d_open = adaopen; 1262 softc->disk->d_close = adaclose; 1263 softc->disk->d_strategy = adastrategy; 1264 softc->disk->d_getattr = adagetattr; 1265 softc->disk->d_dump = adadump; 1266 softc->disk->d_gone = adadiskgonecb; 1267 softc->disk->d_name = "ada"; 1268 softc->disk->d_drv1 = periph; 1269 maxio = cpi.maxio; /* Honor max I/O size of SIM */ 1270 if (maxio == 0) 1271 maxio = DFLTPHYS; /* traditional default */ 1272 else if (maxio > MAXPHYS) 1273 maxio = MAXPHYS; /* for safety */ 1274 if (softc->flags & ADA_FLAG_CAN_48BIT) 1275 maxio = min(maxio, 65536 * softc->params.secsize); 1276 else /* 28bit ATA command limit */ 1277 maxio = min(maxio, 256 * softc->params.secsize); 1278 softc->disk->d_maxsize = maxio; 1279 softc->disk->d_unit = periph->unit_number; 1280 softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION; 1281 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) 1282 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE; 1283 if (softc->flags & ADA_FLAG_CAN_TRIM) { 1284 softc->disk->d_flags |= DISKFLAG_CANDELETE; 1285 softc->disk->d_delmaxsize = softc->params.secsize * 1286 ATA_DSM_RANGE_MAX * 1287 softc->trim_max_ranges; 1288 } else if ((softc->flags & ADA_FLAG_CAN_CFA) && 1289 !(softc->flags & ADA_FLAG_CAN_48BIT)) { 1290 softc->disk->d_flags |= DISKFLAG_CANDELETE; 1291 softc->disk->d_delmaxsize = 256 * softc->params.secsize; 1292 } else 1293 softc->disk->d_delmaxsize = maxio; 1294 if ((cpi.hba_misc & PIM_UNMAPPED) != 0) 1295 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO; 1296 strlcpy(softc->disk->d_descr, cgd->ident_data.model, 1297 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model))); 1298 strlcpy(softc->disk->d_ident, cgd->ident_data.serial, 1299 MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial))); 1300 softc->disk->d_hba_vendor = cpi.hba_vendor; 1301 softc->disk->d_hba_device = cpi.hba_device; 1302 softc->disk->d_hba_subvendor = cpi.hba_subvendor; 1303 softc->disk->d_hba_subdevice = cpi.hba_subdevice; 1304 1305 softc->disk->d_sectorsize = softc->params.secsize; 1306 softc->disk->d_mediasize = (off_t)softc->params.sectors * 1307 softc->params.secsize; 1308 if (ata_physical_sector_size(&cgd->ident_data) != 1309 softc->params.secsize) { 1310 softc->disk->d_stripesize = 1311 ata_physical_sector_size(&cgd->ident_data); 1312 softc->disk->d_stripeoffset = (softc->disk->d_stripesize - 1313 ata_logical_sector_offset(&cgd->ident_data)) % 1314 softc->disk->d_stripesize; 1315 } else if (softc->quirks & ADA_Q_4K) { 1316 softc->disk->d_stripesize = 4096; 1317 softc->disk->d_stripeoffset = 0; 1318 } 1319 softc->disk->d_fwsectors = softc->params.secs_per_track; 1320 softc->disk->d_fwheads = softc->params.heads; 1321 ata_disk_firmware_geom_adjust(softc->disk); 1322 1323 if (ada_legacy_aliases) { 1324 #ifdef ATA_STATIC_ID 1325 legacy_id = xpt_path_legacy_ata_id(periph->path); 1326 #else 1327 legacy_id = softc->disk->d_unit; 1328 #endif 1329 if (legacy_id >= 0) { 1330 snprintf(announce_buf, sizeof(announce_buf), 1331 "kern.devalias.%s%d", 1332 softc->disk->d_name, softc->disk->d_unit); 1333 snprintf(buf1, sizeof(buf1), 1334 "ad%d", legacy_id); 1335 kern_setenv(announce_buf, buf1); 1336 } 1337 } else 1338 legacy_id = -1; 1339 /* 1340 * Acquire a reference to the periph before we register with GEOM. 1341 * We'll release this reference once GEOM calls us back (via 1342 * adadiskgonecb()) telling us that our provider has been freed. 1343 */ 1344 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 1345 xpt_print(periph->path, "%s: lost periph during " 1346 "registration!\n", __func__); 1347 cam_periph_lock(periph); 1348 return (CAM_REQ_CMP_ERR); 1349 } 1350 disk_create(softc->disk, DISK_VERSION); 1351 cam_periph_lock(periph); 1352 cam_periph_unhold(periph); 1353 1354 dp = &softc->params; 1355 snprintf(announce_buf, sizeof(announce_buf), 1356 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)", 1357 (uintmax_t)(((uintmax_t)dp->secsize * 1358 dp->sectors) / (1024*1024)), 1359 (uintmax_t)dp->sectors, 1360 dp->secsize, dp->heads, 1361 dp->secs_per_track, dp->cylinders); 1362 xpt_announce_periph(periph, announce_buf); 1363 xpt_announce_quirks(periph, softc->quirks, ADA_Q_BIT_STRING); 1364 if (legacy_id >= 0) 1365 printf("%s%d: Previously was known as ad%d\n", 1366 periph->periph_name, periph->unit_number, legacy_id); 1367 1368 /* 1369 * Create our sysctl variables, now that we know 1370 * we have successfully attached. 1371 */ 1372 if (cam_periph_acquire(periph) == CAM_REQ_CMP) 1373 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task); 1374 1375 /* 1376 * Add async callbacks for bus reset and 1377 * bus device reset calls. I don't bother 1378 * checking if this fails as, in most cases, 1379 * the system will function just fine without 1380 * them and the only alternative would be to 1381 * not attach the device on failure. 1382 */ 1383 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE | 1384 AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED, 1385 adaasync, periph, periph->path); 1386 1387 /* 1388 * Schedule a periodic event to occasionally send an 1389 * ordered tag to a device. 1390 */ 1391 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0); 1392 callout_reset(&softc->sendordered_c, 1393 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 1394 adasendorderedtag, softc); 1395 1396 if (ADA_RA >= 0 && 1397 cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) { 1398 softc->state = ADA_STATE_RAHEAD; 1399 } else if (ADA_WC >= 0 && 1400 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) { 1401 softc->state = ADA_STATE_WCACHE; 1402 } else { 1403 softc->state = ADA_STATE_NORMAL; 1404 return(CAM_REQ_CMP); 1405 } 1406 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 1407 softc->state = ADA_STATE_NORMAL; 1408 else 1409 xpt_schedule(periph, CAM_PRIORITY_DEV); 1410 return(CAM_REQ_CMP); 1411 } 1412 1413 static void 1414 ada_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 1415 { 1416 struct trim_request *req = &softc->trim_req; 1417 uint64_t lastlba = (uint64_t)-1; 1418 int c, lastcount = 0, off, ranges = 0; 1419 1420 bzero(req, sizeof(*req)); 1421 TAILQ_INIT(&req->bps); 1422 do { 1423 uint64_t lba = bp->bio_pblkno; 1424 int count = bp->bio_bcount / softc->params.secsize; 1425 1426 bioq_remove(&softc->trim_queue, bp); 1427 1428 /* Try to extend the previous range. */ 1429 if (lba == lastlba) { 1430 c = min(count, ATA_DSM_RANGE_MAX - lastcount); 1431 lastcount += c; 1432 off = (ranges - 1) * ATA_DSM_RANGE_SIZE; 1433 req->data[off + 6] = lastcount & 0xff; 1434 req->data[off + 7] = 1435 (lastcount >> 8) & 0xff; 1436 count -= c; 1437 lba += c; 1438 } 1439 1440 while (count > 0) { 1441 c = min(count, ATA_DSM_RANGE_MAX); 1442 off = ranges * ATA_DSM_RANGE_SIZE; 1443 req->data[off + 0] = lba & 0xff; 1444 req->data[off + 1] = (lba >> 8) & 0xff; 1445 req->data[off + 2] = (lba >> 16) & 0xff; 1446 req->data[off + 3] = (lba >> 24) & 0xff; 1447 req->data[off + 4] = (lba >> 32) & 0xff; 1448 req->data[off + 5] = (lba >> 40) & 0xff; 1449 req->data[off + 6] = c & 0xff; 1450 req->data[off + 7] = (c >> 8) & 0xff; 1451 lba += c; 1452 count -= c; 1453 lastcount = c; 1454 ranges++; 1455 /* 1456 * Its the caller's responsibility to ensure the 1457 * request will fit so we don't need to check for 1458 * overrun here 1459 */ 1460 } 1461 lastlba = lba; 1462 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue); 1463 bp = bioq_first(&softc->trim_queue); 1464 if (bp == NULL || 1465 bp->bio_bcount / softc->params.secsize > 1466 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) 1467 break; 1468 } while (1); 1469 cam_fill_ataio(ataio, 1470 ada_retry_count, 1471 adadone, 1472 CAM_DIR_OUT, 1473 0, 1474 req->data, 1475 ((ranges + ATA_DSM_BLK_RANGES - 1) / 1476 ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE, 1477 ada_default_timeout * 1000); 1478 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT, 1479 ATA_DSM_TRIM, 0, (ranges + ATA_DSM_BLK_RANGES - 1480 1) / ATA_DSM_BLK_RANGES); 1481 } 1482 1483 static void 1484 ada_cfaerase(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 1485 { 1486 uint64_t lba = bp->bio_pblkno; 1487 uint16_t count = bp->bio_bcount / softc->params.secsize; 1488 1489 cam_fill_ataio(ataio, 1490 ada_retry_count, 1491 adadone, 1492 CAM_DIR_NONE, 1493 0, 1494 NULL, 1495 0, 1496 ada_default_timeout*1000); 1497 1498 if (count >= 256) 1499 count = 0; 1500 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count); 1501 } 1502 1503 static void 1504 adastart(struct cam_periph *periph, union ccb *start_ccb) 1505 { 1506 struct ada_softc *softc = (struct ada_softc *)periph->softc; 1507 struct ccb_ataio *ataio = &start_ccb->ataio; 1508 1509 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n")); 1510 1511 switch (softc->state) { 1512 case ADA_STATE_NORMAL: 1513 { 1514 struct bio *bp; 1515 u_int8_t tag_code; 1516 1517 /* Run TRIM if not running yet. */ 1518 if (!softc->trim_running && 1519 (bp = bioq_first(&softc->trim_queue)) != 0) { 1520 if (softc->flags & ADA_FLAG_CAN_TRIM) { 1521 ada_dsmtrim(softc, bp, ataio); 1522 } else if ((softc->flags & ADA_FLAG_CAN_CFA) && 1523 !(softc->flags & ADA_FLAG_CAN_48BIT)) { 1524 ada_cfaerase(softc, bp, ataio); 1525 } else { 1526 panic("adastart: BIO_DELETE without method, not possible."); 1527 } 1528 softc->trim_running = 1; 1529 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM; 1530 start_ccb->ccb_h.flags |= CAM_UNLOCKED; 1531 goto out; 1532 } 1533 /* Run regular command. */ 1534 bp = bioq_first(&softc->bio_queue); 1535 if (bp == NULL) { 1536 xpt_release_ccb(start_ccb); 1537 break; 1538 } 1539 bioq_remove(&softc->bio_queue, bp); 1540 1541 if ((bp->bio_flags & BIO_ORDERED) != 0 1542 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) { 1543 softc->flags &= ~ADA_FLAG_NEED_OTAG; 1544 softc->flags |= ADA_FLAG_WAS_OTAG; 1545 tag_code = 0; 1546 } else { 1547 tag_code = 1; 1548 } 1549 switch (bp->bio_cmd) { 1550 case BIO_WRITE: 1551 softc->flags |= ADA_FLAG_DIRTY; 1552 /* FALLTHROUGH */ 1553 case BIO_READ: 1554 { 1555 uint64_t lba = bp->bio_pblkno; 1556 uint16_t count = bp->bio_bcount / softc->params.secsize; 1557 #ifdef ADA_TEST_FAILURE 1558 int fail = 0; 1559 1560 /* 1561 * Support the failure ioctls. If the command is a 1562 * read, and there are pending forced read errors, or 1563 * if a write and pending write errors, then fail this 1564 * operation with EIO. This is useful for testing 1565 * purposes. Also, support having every Nth read fail. 1566 * 1567 * This is a rather blunt tool. 1568 */ 1569 if (bp->bio_cmd == BIO_READ) { 1570 if (softc->force_read_error) { 1571 softc->force_read_error--; 1572 fail = 1; 1573 } 1574 if (softc->periodic_read_error > 0) { 1575 if (++softc->periodic_read_count >= 1576 softc->periodic_read_error) { 1577 softc->periodic_read_count = 0; 1578 fail = 1; 1579 } 1580 } 1581 } else { 1582 if (softc->force_write_error) { 1583 softc->force_write_error--; 1584 fail = 1; 1585 } 1586 } 1587 if (fail) { 1588 bp->bio_error = EIO; 1589 bp->bio_flags |= BIO_ERROR; 1590 biodone(bp); 1591 xpt_release_ccb(start_ccb); 1592 adaschedule(periph); 1593 return; 1594 } 1595 #endif 1596 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 || 1597 round_page(bp->bio_bcount + bp->bio_ma_offset) / 1598 PAGE_SIZE == bp->bio_ma_n, 1599 ("Short bio %p", bp)); 1600 cam_fill_ataio(ataio, 1601 ada_retry_count, 1602 adadone, 1603 (bp->bio_cmd == BIO_READ ? CAM_DIR_IN : 1604 CAM_DIR_OUT) | ((bp->bio_flags & BIO_UNMAPPED) 1605 != 0 ? CAM_DATA_BIO : 0), 1606 tag_code, 1607 ((bp->bio_flags & BIO_UNMAPPED) != 0) ? (void *)bp : 1608 bp->bio_data, 1609 bp->bio_bcount, 1610 ada_default_timeout*1000); 1611 1612 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) { 1613 if (bp->bio_cmd == BIO_READ) { 1614 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED, 1615 lba, count); 1616 } else { 1617 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED, 1618 lba, count); 1619 } 1620 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) && 1621 (lba + count >= ATA_MAX_28BIT_LBA || 1622 count > 256)) { 1623 if (softc->flags & ADA_FLAG_CAN_DMA48) { 1624 if (bp->bio_cmd == BIO_READ) { 1625 ata_48bit_cmd(ataio, ATA_READ_DMA48, 1626 0, lba, count); 1627 } else { 1628 ata_48bit_cmd(ataio, ATA_WRITE_DMA48, 1629 0, lba, count); 1630 } 1631 } else { 1632 if (bp->bio_cmd == BIO_READ) { 1633 ata_48bit_cmd(ataio, ATA_READ_MUL48, 1634 0, lba, count); 1635 } else { 1636 ata_48bit_cmd(ataio, ATA_WRITE_MUL48, 1637 0, lba, count); 1638 } 1639 } 1640 } else { 1641 if (count == 256) 1642 count = 0; 1643 if (softc->flags & ADA_FLAG_CAN_DMA) { 1644 if (bp->bio_cmd == BIO_READ) { 1645 ata_28bit_cmd(ataio, ATA_READ_DMA, 1646 0, lba, count); 1647 } else { 1648 ata_28bit_cmd(ataio, ATA_WRITE_DMA, 1649 0, lba, count); 1650 } 1651 } else { 1652 if (bp->bio_cmd == BIO_READ) { 1653 ata_28bit_cmd(ataio, ATA_READ_MUL, 1654 0, lba, count); 1655 } else { 1656 ata_28bit_cmd(ataio, ATA_WRITE_MUL, 1657 0, lba, count); 1658 } 1659 } 1660 } 1661 break; 1662 } 1663 case BIO_FLUSH: 1664 cam_fill_ataio(ataio, 1665 1, 1666 adadone, 1667 CAM_DIR_NONE, 1668 0, 1669 NULL, 1670 0, 1671 ada_default_timeout*1000); 1672 1673 if (softc->flags & ADA_FLAG_CAN_48BIT) 1674 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1675 else 1676 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0); 1677 break; 1678 } 1679 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO; 1680 start_ccb->ccb_h.flags |= CAM_UNLOCKED; 1681 out: 1682 start_ccb->ccb_h.ccb_bp = bp; 1683 softc->outstanding_cmds++; 1684 softc->refcount++; 1685 cam_periph_unlock(periph); 1686 xpt_action(start_ccb); 1687 cam_periph_lock(periph); 1688 softc->refcount--; 1689 1690 /* May have more work to do, so ensure we stay scheduled */ 1691 adaschedule(periph); 1692 break; 1693 } 1694 case ADA_STATE_RAHEAD: 1695 case ADA_STATE_WCACHE: 1696 { 1697 cam_fill_ataio(ataio, 1698 1, 1699 adadone, 1700 CAM_DIR_NONE, 1701 0, 1702 NULL, 1703 0, 1704 ada_default_timeout*1000); 1705 1706 if (softc->state == ADA_STATE_RAHEAD) { 1707 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ? 1708 ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0); 1709 start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD; 1710 } else { 1711 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ? 1712 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0); 1713 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE; 1714 } 1715 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 1716 xpt_action(start_ccb); 1717 break; 1718 } 1719 } 1720 } 1721 1722 static void 1723 adadone(struct cam_periph *periph, union ccb *done_ccb) 1724 { 1725 struct ada_softc *softc; 1726 struct ccb_ataio *ataio; 1727 struct ccb_getdev *cgd; 1728 struct cam_path *path; 1729 int state; 1730 1731 softc = (struct ada_softc *)periph->softc; 1732 ataio = &done_ccb->ataio; 1733 path = done_ccb->ccb_h.path; 1734 1735 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n")); 1736 1737 state = ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK; 1738 switch (state) { 1739 case ADA_CCB_BUFFER_IO: 1740 case ADA_CCB_TRIM: 1741 { 1742 struct bio *bp; 1743 int error; 1744 1745 cam_periph_lock(periph); 1746 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1747 error = adaerror(done_ccb, 0, 0); 1748 if (error == ERESTART) { 1749 /* A retry was scheduled, so just return. */ 1750 cam_periph_unlock(periph); 1751 return; 1752 } 1753 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1754 cam_release_devq(path, 1755 /*relsim_flags*/0, 1756 /*reduction*/0, 1757 /*timeout*/0, 1758 /*getcount_only*/0); 1759 } else { 1760 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1761 panic("REQ_CMP with QFRZN"); 1762 error = 0; 1763 } 1764 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 1765 bp->bio_error = error; 1766 if (error != 0) { 1767 bp->bio_resid = bp->bio_bcount; 1768 bp->bio_flags |= BIO_ERROR; 1769 } else { 1770 if (state == ADA_CCB_TRIM) 1771 bp->bio_resid = 0; 1772 else 1773 bp->bio_resid = ataio->resid; 1774 if (bp->bio_resid > 0) 1775 bp->bio_flags |= BIO_ERROR; 1776 } 1777 softc->outstanding_cmds--; 1778 if (softc->outstanding_cmds == 0) 1779 softc->flags |= ADA_FLAG_WAS_OTAG; 1780 xpt_release_ccb(done_ccb); 1781 if (state == ADA_CCB_TRIM) { 1782 TAILQ_HEAD(, bio) queue; 1783 struct bio *bp1; 1784 1785 TAILQ_INIT(&queue); 1786 TAILQ_CONCAT(&queue, &softc->trim_req.bps, bio_queue); 1787 softc->trim_running = 0; 1788 adaschedule(periph); 1789 cam_periph_unlock(periph); 1790 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) { 1791 TAILQ_REMOVE(&queue, bp1, bio_queue); 1792 bp1->bio_error = error; 1793 if (error != 0) { 1794 bp1->bio_flags |= BIO_ERROR; 1795 bp1->bio_resid = bp1->bio_bcount; 1796 } else 1797 bp1->bio_resid = 0; 1798 biodone(bp1); 1799 } 1800 } else { 1801 cam_periph_unlock(periph); 1802 biodone(bp); 1803 } 1804 return; 1805 } 1806 case ADA_CCB_RAHEAD: 1807 { 1808 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1809 if (adaerror(done_ccb, 0, 0) == ERESTART) { 1810 out: 1811 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 1812 cam_release_devq(path, 0, 0, 0, FALSE); 1813 return; 1814 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1815 cam_release_devq(path, 1816 /*relsim_flags*/0, 1817 /*reduction*/0, 1818 /*timeout*/0, 1819 /*getcount_only*/0); 1820 } 1821 } 1822 1823 /* 1824 * Since our peripheral may be invalidated by an error 1825 * above or an external event, we must release our CCB 1826 * before releasing the reference on the peripheral. 1827 * The peripheral will only go away once the last reference 1828 * is removed, and we need it around for the CCB release 1829 * operation. 1830 */ 1831 cgd = (struct ccb_getdev *)done_ccb; 1832 xpt_setup_ccb(&cgd->ccb_h, path, CAM_PRIORITY_NORMAL); 1833 cgd->ccb_h.func_code = XPT_GDEV_TYPE; 1834 xpt_action((union ccb *)cgd); 1835 if (ADA_WC >= 0 && 1836 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) { 1837 softc->state = ADA_STATE_WCACHE; 1838 xpt_release_ccb(done_ccb); 1839 xpt_schedule(periph, CAM_PRIORITY_DEV); 1840 goto out; 1841 } 1842 softc->state = ADA_STATE_NORMAL; 1843 xpt_release_ccb(done_ccb); 1844 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 1845 cam_release_devq(path, 0, 0, 0, FALSE); 1846 adaschedule(periph); 1847 cam_periph_release_locked(periph); 1848 return; 1849 } 1850 case ADA_CCB_WCACHE: 1851 { 1852 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1853 if (adaerror(done_ccb, 0, 0) == ERESTART) { 1854 goto out; 1855 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1856 cam_release_devq(path, 1857 /*relsim_flags*/0, 1858 /*reduction*/0, 1859 /*timeout*/0, 1860 /*getcount_only*/0); 1861 } 1862 } 1863 1864 softc->state = ADA_STATE_NORMAL; 1865 /* 1866 * Since our peripheral may be invalidated by an error 1867 * above or an external event, we must release our CCB 1868 * before releasing the reference on the peripheral. 1869 * The peripheral will only go away once the last reference 1870 * is removed, and we need it around for the CCB release 1871 * operation. 1872 */ 1873 xpt_release_ccb(done_ccb); 1874 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 1875 cam_release_devq(path, 0, 0, 0, FALSE); 1876 adaschedule(periph); 1877 cam_periph_release_locked(periph); 1878 return; 1879 } 1880 case ADA_CCB_DUMP: 1881 /* No-op. We're polling */ 1882 return; 1883 default: 1884 break; 1885 } 1886 xpt_release_ccb(done_ccb); 1887 } 1888 1889 static int 1890 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 1891 { 1892 1893 return(cam_periph_error(ccb, cam_flags, sense_flags, NULL)); 1894 } 1895 1896 static void 1897 adagetparams(struct cam_periph *periph, struct ccb_getdev *cgd) 1898 { 1899 struct ada_softc *softc = (struct ada_softc *)periph->softc; 1900 struct disk_params *dp = &softc->params; 1901 u_int64_t lbasize48; 1902 u_int32_t lbasize; 1903 1904 dp->secsize = ata_logical_sector_size(&cgd->ident_data); 1905 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) && 1906 cgd->ident_data.current_heads && cgd->ident_data.current_sectors) { 1907 dp->heads = cgd->ident_data.current_heads; 1908 dp->secs_per_track = cgd->ident_data.current_sectors; 1909 dp->cylinders = cgd->ident_data.cylinders; 1910 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 | 1911 ((u_int32_t)cgd->ident_data.current_size_2 << 16); 1912 } else { 1913 dp->heads = cgd->ident_data.heads; 1914 dp->secs_per_track = cgd->ident_data.sectors; 1915 dp->cylinders = cgd->ident_data.cylinders; 1916 dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track; 1917 } 1918 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 | 1919 ((u_int32_t)cgd->ident_data.lba_size_2 << 16); 1920 1921 /* use the 28bit LBA size if valid or bigger than the CHS mapping */ 1922 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize) 1923 dp->sectors = lbasize; 1924 1925 /* use the 48bit LBA size if valid */ 1926 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) | 1927 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) | 1928 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) | 1929 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48); 1930 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) && 1931 lbasize48 > ATA_MAX_28BIT_LBA) 1932 dp->sectors = lbasize48; 1933 } 1934 1935 static void 1936 adasendorderedtag(void *arg) 1937 { 1938 struct ada_softc *softc = arg; 1939 1940 if (ada_send_ordered) { 1941 if (softc->outstanding_cmds > 0) { 1942 if ((softc->flags & ADA_FLAG_WAS_OTAG) == 0) 1943 softc->flags |= ADA_FLAG_NEED_OTAG; 1944 softc->flags &= ~ADA_FLAG_WAS_OTAG; 1945 } 1946 } 1947 /* Queue us up again */ 1948 callout_reset(&softc->sendordered_c, 1949 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 1950 adasendorderedtag, softc); 1951 } 1952 1953 /* 1954 * Step through all ADA peripheral drivers, and if the device is still open, 1955 * sync the disk cache to physical media. 1956 */ 1957 static void 1958 adaflush(void) 1959 { 1960 struct cam_periph *periph; 1961 struct ada_softc *softc; 1962 union ccb *ccb; 1963 int error; 1964 1965 CAM_PERIPH_FOREACH(periph, &adadriver) { 1966 softc = (struct ada_softc *)periph->softc; 1967 if (SCHEDULER_STOPPED()) { 1968 /* If we paniced with the lock held, do not recurse. */ 1969 if (!cam_periph_owned(periph) && 1970 (softc->flags & ADA_FLAG_OPEN)) { 1971 adadump(softc->disk, NULL, 0, 0, 0); 1972 } 1973 continue; 1974 } 1975 cam_periph_lock(periph); 1976 /* 1977 * We only sync the cache if the drive is still open, and 1978 * if the drive is capable of it.. 1979 */ 1980 if (((softc->flags & ADA_FLAG_OPEN) == 0) || 1981 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) { 1982 cam_periph_unlock(periph); 1983 continue; 1984 } 1985 1986 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1987 cam_fill_ataio(&ccb->ataio, 1988 0, 1989 adadone, 1990 CAM_DIR_NONE, 1991 0, 1992 NULL, 1993 0, 1994 ada_default_timeout*1000); 1995 if (softc->flags & ADA_FLAG_CAN_48BIT) 1996 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1997 else 1998 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 1999 2000 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 2001 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 2002 softc->disk->d_devstat); 2003 if (error != 0) 2004 xpt_print(periph->path, "Synchronize cache failed\n"); 2005 xpt_release_ccb(ccb); 2006 cam_periph_unlock(periph); 2007 } 2008 } 2009 2010 static void 2011 adaspindown(uint8_t cmd, int flags) 2012 { 2013 struct cam_periph *periph; 2014 struct ada_softc *softc; 2015 union ccb *ccb; 2016 int error; 2017 2018 CAM_PERIPH_FOREACH(periph, &adadriver) { 2019 /* If we paniced with lock held - not recurse here. */ 2020 if (cam_periph_owned(periph)) 2021 continue; 2022 cam_periph_lock(periph); 2023 softc = (struct ada_softc *)periph->softc; 2024 /* 2025 * We only spin-down the drive if it is capable of it.. 2026 */ 2027 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 2028 cam_periph_unlock(periph); 2029 continue; 2030 } 2031 2032 if (bootverbose) 2033 xpt_print(periph->path, "spin-down\n"); 2034 2035 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 2036 cam_fill_ataio(&ccb->ataio, 2037 0, 2038 adadone, 2039 CAM_DIR_NONE | flags, 2040 0, 2041 NULL, 2042 0, 2043 ada_default_timeout*1000); 2044 ata_28bit_cmd(&ccb->ataio, cmd, 0, 0, 0); 2045 2046 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 2047 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 2048 softc->disk->d_devstat); 2049 if (error != 0) 2050 xpt_print(periph->path, "Spin-down disk failed\n"); 2051 xpt_release_ccb(ccb); 2052 cam_periph_unlock(periph); 2053 } 2054 } 2055 2056 static void 2057 adashutdown(void *arg, int howto) 2058 { 2059 2060 adaflush(); 2061 if (ada_spindown_shutdown != 0 && 2062 (howto & (RB_HALT | RB_POWEROFF)) != 0) 2063 adaspindown(ATA_STANDBY_IMMEDIATE, 0); 2064 } 2065 2066 static void 2067 adasuspend(void *arg) 2068 { 2069 2070 adaflush(); 2071 if (ada_spindown_suspend != 0) 2072 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE); 2073 } 2074 2075 static void 2076 adaresume(void *arg) 2077 { 2078 struct cam_periph *periph; 2079 struct ada_softc *softc; 2080 2081 if (ada_spindown_suspend == 0) 2082 return; 2083 2084 CAM_PERIPH_FOREACH(periph, &adadriver) { 2085 cam_periph_lock(periph); 2086 softc = (struct ada_softc *)periph->softc; 2087 /* 2088 * We only spin-down the drive if it is capable of it.. 2089 */ 2090 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 2091 cam_periph_unlock(periph); 2092 continue; 2093 } 2094 2095 if (bootverbose) 2096 xpt_print(periph->path, "resume\n"); 2097 2098 /* 2099 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on 2100 * sleep request. 2101 */ 2102 cam_release_devq(periph->path, 2103 /*relsim_flags*/0, 2104 /*openings*/0, 2105 /*timeout*/0, 2106 /*getcount_only*/0); 2107 2108 cam_periph_unlock(periph); 2109 } 2110 } 2111 2112 #endif /* _KERNEL */ 2113