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 struct trim_request *req = &softc->trim_req; 1487 uint64_t lba = bp->bio_pblkno; 1488 uint16_t count = bp->bio_bcount / softc->params.secsize; 1489 1490 bzero(req, sizeof(*req)); 1491 TAILQ_INIT(&req->bps); 1492 bioq_remove(&softc->trim_queue, bp); 1493 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue); 1494 1495 cam_fill_ataio(ataio, 1496 ada_retry_count, 1497 adadone, 1498 CAM_DIR_NONE, 1499 0, 1500 NULL, 1501 0, 1502 ada_default_timeout*1000); 1503 1504 if (count >= 256) 1505 count = 0; 1506 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count); 1507 } 1508 1509 static void 1510 adastart(struct cam_periph *periph, union ccb *start_ccb) 1511 { 1512 struct ada_softc *softc = (struct ada_softc *)periph->softc; 1513 struct ccb_ataio *ataio = &start_ccb->ataio; 1514 1515 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n")); 1516 1517 switch (softc->state) { 1518 case ADA_STATE_NORMAL: 1519 { 1520 struct bio *bp; 1521 u_int8_t tag_code; 1522 1523 /* Run TRIM if not running yet. */ 1524 if (!softc->trim_running && 1525 (bp = bioq_first(&softc->trim_queue)) != 0) { 1526 if (softc->flags & ADA_FLAG_CAN_TRIM) { 1527 ada_dsmtrim(softc, bp, ataio); 1528 } else if ((softc->flags & ADA_FLAG_CAN_CFA) && 1529 !(softc->flags & ADA_FLAG_CAN_48BIT)) { 1530 ada_cfaerase(softc, bp, ataio); 1531 } else { 1532 panic("adastart: BIO_DELETE without method, not possible."); 1533 } 1534 softc->trim_running = 1; 1535 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM; 1536 start_ccb->ccb_h.flags |= CAM_UNLOCKED; 1537 goto out; 1538 } 1539 /* Run regular command. */ 1540 bp = bioq_first(&softc->bio_queue); 1541 if (bp == NULL) { 1542 xpt_release_ccb(start_ccb); 1543 break; 1544 } 1545 bioq_remove(&softc->bio_queue, bp); 1546 1547 if ((bp->bio_flags & BIO_ORDERED) != 0 1548 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) { 1549 softc->flags &= ~ADA_FLAG_NEED_OTAG; 1550 softc->flags |= ADA_FLAG_WAS_OTAG; 1551 tag_code = 0; 1552 } else { 1553 tag_code = 1; 1554 } 1555 switch (bp->bio_cmd) { 1556 case BIO_WRITE: 1557 softc->flags |= ADA_FLAG_DIRTY; 1558 /* FALLTHROUGH */ 1559 case BIO_READ: 1560 { 1561 uint64_t lba = bp->bio_pblkno; 1562 uint16_t count = bp->bio_bcount / softc->params.secsize; 1563 #ifdef ADA_TEST_FAILURE 1564 int fail = 0; 1565 1566 /* 1567 * Support the failure ioctls. If the command is a 1568 * read, and there are pending forced read errors, or 1569 * if a write and pending write errors, then fail this 1570 * operation with EIO. This is useful for testing 1571 * purposes. Also, support having every Nth read fail. 1572 * 1573 * This is a rather blunt tool. 1574 */ 1575 if (bp->bio_cmd == BIO_READ) { 1576 if (softc->force_read_error) { 1577 softc->force_read_error--; 1578 fail = 1; 1579 } 1580 if (softc->periodic_read_error > 0) { 1581 if (++softc->periodic_read_count >= 1582 softc->periodic_read_error) { 1583 softc->periodic_read_count = 0; 1584 fail = 1; 1585 } 1586 } 1587 } else { 1588 if (softc->force_write_error) { 1589 softc->force_write_error--; 1590 fail = 1; 1591 } 1592 } 1593 if (fail) { 1594 bp->bio_error = EIO; 1595 bp->bio_flags |= BIO_ERROR; 1596 biodone(bp); 1597 xpt_release_ccb(start_ccb); 1598 adaschedule(periph); 1599 return; 1600 } 1601 #endif 1602 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 || 1603 round_page(bp->bio_bcount + bp->bio_ma_offset) / 1604 PAGE_SIZE == bp->bio_ma_n, 1605 ("Short bio %p", bp)); 1606 cam_fill_ataio(ataio, 1607 ada_retry_count, 1608 adadone, 1609 (bp->bio_cmd == BIO_READ ? CAM_DIR_IN : 1610 CAM_DIR_OUT) | ((bp->bio_flags & BIO_UNMAPPED) 1611 != 0 ? CAM_DATA_BIO : 0), 1612 tag_code, 1613 ((bp->bio_flags & BIO_UNMAPPED) != 0) ? (void *)bp : 1614 bp->bio_data, 1615 bp->bio_bcount, 1616 ada_default_timeout*1000); 1617 1618 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) { 1619 if (bp->bio_cmd == BIO_READ) { 1620 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED, 1621 lba, count); 1622 } else { 1623 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED, 1624 lba, count); 1625 } 1626 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) && 1627 (lba + count >= ATA_MAX_28BIT_LBA || 1628 count > 256)) { 1629 if (softc->flags & ADA_FLAG_CAN_DMA48) { 1630 if (bp->bio_cmd == BIO_READ) { 1631 ata_48bit_cmd(ataio, ATA_READ_DMA48, 1632 0, lba, count); 1633 } else { 1634 ata_48bit_cmd(ataio, ATA_WRITE_DMA48, 1635 0, lba, count); 1636 } 1637 } else { 1638 if (bp->bio_cmd == BIO_READ) { 1639 ata_48bit_cmd(ataio, ATA_READ_MUL48, 1640 0, lba, count); 1641 } else { 1642 ata_48bit_cmd(ataio, ATA_WRITE_MUL48, 1643 0, lba, count); 1644 } 1645 } 1646 } else { 1647 if (count == 256) 1648 count = 0; 1649 if (softc->flags & ADA_FLAG_CAN_DMA) { 1650 if (bp->bio_cmd == BIO_READ) { 1651 ata_28bit_cmd(ataio, ATA_READ_DMA, 1652 0, lba, count); 1653 } else { 1654 ata_28bit_cmd(ataio, ATA_WRITE_DMA, 1655 0, lba, count); 1656 } 1657 } else { 1658 if (bp->bio_cmd == BIO_READ) { 1659 ata_28bit_cmd(ataio, ATA_READ_MUL, 1660 0, lba, count); 1661 } else { 1662 ata_28bit_cmd(ataio, ATA_WRITE_MUL, 1663 0, lba, count); 1664 } 1665 } 1666 } 1667 break; 1668 } 1669 case BIO_FLUSH: 1670 cam_fill_ataio(ataio, 1671 1, 1672 adadone, 1673 CAM_DIR_NONE, 1674 0, 1675 NULL, 1676 0, 1677 ada_default_timeout*1000); 1678 1679 if (softc->flags & ADA_FLAG_CAN_48BIT) 1680 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1681 else 1682 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0); 1683 break; 1684 } 1685 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO; 1686 start_ccb->ccb_h.flags |= CAM_UNLOCKED; 1687 out: 1688 start_ccb->ccb_h.ccb_bp = bp; 1689 softc->outstanding_cmds++; 1690 softc->refcount++; 1691 cam_periph_unlock(periph); 1692 xpt_action(start_ccb); 1693 cam_periph_lock(periph); 1694 softc->refcount--; 1695 1696 /* May have more work to do, so ensure we stay scheduled */ 1697 adaschedule(periph); 1698 break; 1699 } 1700 case ADA_STATE_RAHEAD: 1701 case ADA_STATE_WCACHE: 1702 { 1703 cam_fill_ataio(ataio, 1704 1, 1705 adadone, 1706 CAM_DIR_NONE, 1707 0, 1708 NULL, 1709 0, 1710 ada_default_timeout*1000); 1711 1712 if (softc->state == ADA_STATE_RAHEAD) { 1713 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ? 1714 ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0); 1715 start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD; 1716 } else { 1717 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ? 1718 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0); 1719 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE; 1720 } 1721 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 1722 xpt_action(start_ccb); 1723 break; 1724 } 1725 } 1726 } 1727 1728 static void 1729 adadone(struct cam_periph *periph, union ccb *done_ccb) 1730 { 1731 struct ada_softc *softc; 1732 struct ccb_ataio *ataio; 1733 struct ccb_getdev *cgd; 1734 struct cam_path *path; 1735 int state; 1736 1737 softc = (struct ada_softc *)periph->softc; 1738 ataio = &done_ccb->ataio; 1739 path = done_ccb->ccb_h.path; 1740 1741 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n")); 1742 1743 state = ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK; 1744 switch (state) { 1745 case ADA_CCB_BUFFER_IO: 1746 case ADA_CCB_TRIM: 1747 { 1748 struct bio *bp; 1749 int error; 1750 1751 cam_periph_lock(periph); 1752 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1753 error = adaerror(done_ccb, 0, 0); 1754 if (error == ERESTART) { 1755 /* A retry was scheduled, so just return. */ 1756 cam_periph_unlock(periph); 1757 return; 1758 } 1759 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1760 cam_release_devq(path, 1761 /*relsim_flags*/0, 1762 /*reduction*/0, 1763 /*timeout*/0, 1764 /*getcount_only*/0); 1765 } else { 1766 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1767 panic("REQ_CMP with QFRZN"); 1768 error = 0; 1769 } 1770 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 1771 bp->bio_error = error; 1772 if (error != 0) { 1773 bp->bio_resid = bp->bio_bcount; 1774 bp->bio_flags |= BIO_ERROR; 1775 } else { 1776 if (state == ADA_CCB_TRIM) 1777 bp->bio_resid = 0; 1778 else 1779 bp->bio_resid = ataio->resid; 1780 if (bp->bio_resid > 0) 1781 bp->bio_flags |= BIO_ERROR; 1782 } 1783 softc->outstanding_cmds--; 1784 if (softc->outstanding_cmds == 0) 1785 softc->flags |= ADA_FLAG_WAS_OTAG; 1786 xpt_release_ccb(done_ccb); 1787 if (state == ADA_CCB_TRIM) { 1788 TAILQ_HEAD(, bio) queue; 1789 struct bio *bp1; 1790 1791 TAILQ_INIT(&queue); 1792 TAILQ_CONCAT(&queue, &softc->trim_req.bps, bio_queue); 1793 softc->trim_running = 0; 1794 adaschedule(periph); 1795 cam_periph_unlock(periph); 1796 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) { 1797 TAILQ_REMOVE(&queue, bp1, bio_queue); 1798 bp1->bio_error = error; 1799 if (error != 0) { 1800 bp1->bio_flags |= BIO_ERROR; 1801 bp1->bio_resid = bp1->bio_bcount; 1802 } else 1803 bp1->bio_resid = 0; 1804 biodone(bp1); 1805 } 1806 } else { 1807 cam_periph_unlock(periph); 1808 biodone(bp); 1809 } 1810 return; 1811 } 1812 case ADA_CCB_RAHEAD: 1813 { 1814 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1815 if (adaerror(done_ccb, 0, 0) == ERESTART) { 1816 out: 1817 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 1818 cam_release_devq(path, 0, 0, 0, FALSE); 1819 return; 1820 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1821 cam_release_devq(path, 1822 /*relsim_flags*/0, 1823 /*reduction*/0, 1824 /*timeout*/0, 1825 /*getcount_only*/0); 1826 } 1827 } 1828 1829 /* 1830 * Since our peripheral may be invalidated by an error 1831 * above or an external event, we must release our CCB 1832 * before releasing the reference on the peripheral. 1833 * The peripheral will only go away once the last reference 1834 * is removed, and we need it around for the CCB release 1835 * operation. 1836 */ 1837 cgd = (struct ccb_getdev *)done_ccb; 1838 xpt_setup_ccb(&cgd->ccb_h, path, CAM_PRIORITY_NORMAL); 1839 cgd->ccb_h.func_code = XPT_GDEV_TYPE; 1840 xpt_action((union ccb *)cgd); 1841 if (ADA_WC >= 0 && 1842 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) { 1843 softc->state = ADA_STATE_WCACHE; 1844 xpt_release_ccb(done_ccb); 1845 xpt_schedule(periph, CAM_PRIORITY_DEV); 1846 goto out; 1847 } 1848 softc->state = ADA_STATE_NORMAL; 1849 xpt_release_ccb(done_ccb); 1850 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 1851 cam_release_devq(path, 0, 0, 0, FALSE); 1852 adaschedule(periph); 1853 cam_periph_release_locked(periph); 1854 return; 1855 } 1856 case ADA_CCB_WCACHE: 1857 { 1858 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1859 if (adaerror(done_ccb, 0, 0) == ERESTART) { 1860 goto out; 1861 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1862 cam_release_devq(path, 1863 /*relsim_flags*/0, 1864 /*reduction*/0, 1865 /*timeout*/0, 1866 /*getcount_only*/0); 1867 } 1868 } 1869 1870 softc->state = ADA_STATE_NORMAL; 1871 /* 1872 * Since our peripheral may be invalidated by an error 1873 * above or an external event, we must release our CCB 1874 * before releasing the reference on the peripheral. 1875 * The peripheral will only go away once the last reference 1876 * is removed, and we need it around for the CCB release 1877 * operation. 1878 */ 1879 xpt_release_ccb(done_ccb); 1880 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 1881 cam_release_devq(path, 0, 0, 0, FALSE); 1882 adaschedule(periph); 1883 cam_periph_release_locked(periph); 1884 return; 1885 } 1886 case ADA_CCB_DUMP: 1887 /* No-op. We're polling */ 1888 return; 1889 default: 1890 break; 1891 } 1892 xpt_release_ccb(done_ccb); 1893 } 1894 1895 static int 1896 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 1897 { 1898 1899 return(cam_periph_error(ccb, cam_flags, sense_flags, NULL)); 1900 } 1901 1902 static void 1903 adagetparams(struct cam_periph *periph, struct ccb_getdev *cgd) 1904 { 1905 struct ada_softc *softc = (struct ada_softc *)periph->softc; 1906 struct disk_params *dp = &softc->params; 1907 u_int64_t lbasize48; 1908 u_int32_t lbasize; 1909 1910 dp->secsize = ata_logical_sector_size(&cgd->ident_data); 1911 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) && 1912 cgd->ident_data.current_heads && cgd->ident_data.current_sectors) { 1913 dp->heads = cgd->ident_data.current_heads; 1914 dp->secs_per_track = cgd->ident_data.current_sectors; 1915 dp->cylinders = cgd->ident_data.cylinders; 1916 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 | 1917 ((u_int32_t)cgd->ident_data.current_size_2 << 16); 1918 } else { 1919 dp->heads = cgd->ident_data.heads; 1920 dp->secs_per_track = cgd->ident_data.sectors; 1921 dp->cylinders = cgd->ident_data.cylinders; 1922 dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track; 1923 } 1924 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 | 1925 ((u_int32_t)cgd->ident_data.lba_size_2 << 16); 1926 1927 /* use the 28bit LBA size if valid or bigger than the CHS mapping */ 1928 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize) 1929 dp->sectors = lbasize; 1930 1931 /* use the 48bit LBA size if valid */ 1932 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) | 1933 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) | 1934 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) | 1935 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48); 1936 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) && 1937 lbasize48 > ATA_MAX_28BIT_LBA) 1938 dp->sectors = lbasize48; 1939 } 1940 1941 static void 1942 adasendorderedtag(void *arg) 1943 { 1944 struct ada_softc *softc = arg; 1945 1946 if (ada_send_ordered) { 1947 if (softc->outstanding_cmds > 0) { 1948 if ((softc->flags & ADA_FLAG_WAS_OTAG) == 0) 1949 softc->flags |= ADA_FLAG_NEED_OTAG; 1950 softc->flags &= ~ADA_FLAG_WAS_OTAG; 1951 } 1952 } 1953 /* Queue us up again */ 1954 callout_reset(&softc->sendordered_c, 1955 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 1956 adasendorderedtag, softc); 1957 } 1958 1959 /* 1960 * Step through all ADA peripheral drivers, and if the device is still open, 1961 * sync the disk cache to physical media. 1962 */ 1963 static void 1964 adaflush(void) 1965 { 1966 struct cam_periph *periph; 1967 struct ada_softc *softc; 1968 union ccb *ccb; 1969 int error; 1970 1971 CAM_PERIPH_FOREACH(periph, &adadriver) { 1972 softc = (struct ada_softc *)periph->softc; 1973 if (SCHEDULER_STOPPED()) { 1974 /* If we paniced with the lock held, do not recurse. */ 1975 if (!cam_periph_owned(periph) && 1976 (softc->flags & ADA_FLAG_OPEN)) { 1977 adadump(softc->disk, NULL, 0, 0, 0); 1978 } 1979 continue; 1980 } 1981 cam_periph_lock(periph); 1982 /* 1983 * We only sync the cache if the drive is still open, and 1984 * if the drive is capable of it.. 1985 */ 1986 if (((softc->flags & ADA_FLAG_OPEN) == 0) || 1987 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) { 1988 cam_periph_unlock(periph); 1989 continue; 1990 } 1991 1992 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1993 cam_fill_ataio(&ccb->ataio, 1994 0, 1995 adadone, 1996 CAM_DIR_NONE, 1997 0, 1998 NULL, 1999 0, 2000 ada_default_timeout*1000); 2001 if (softc->flags & ADA_FLAG_CAN_48BIT) 2002 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 2003 else 2004 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 2005 2006 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 2007 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 2008 softc->disk->d_devstat); 2009 if (error != 0) 2010 xpt_print(periph->path, "Synchronize cache failed\n"); 2011 xpt_release_ccb(ccb); 2012 cam_periph_unlock(periph); 2013 } 2014 } 2015 2016 static void 2017 adaspindown(uint8_t cmd, int flags) 2018 { 2019 struct cam_periph *periph; 2020 struct ada_softc *softc; 2021 union ccb *ccb; 2022 int error; 2023 2024 CAM_PERIPH_FOREACH(periph, &adadriver) { 2025 /* If we paniced with lock held - not recurse here. */ 2026 if (cam_periph_owned(periph)) 2027 continue; 2028 cam_periph_lock(periph); 2029 softc = (struct ada_softc *)periph->softc; 2030 /* 2031 * We only spin-down the drive if it is capable of it.. 2032 */ 2033 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 2034 cam_periph_unlock(periph); 2035 continue; 2036 } 2037 2038 if (bootverbose) 2039 xpt_print(periph->path, "spin-down\n"); 2040 2041 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 2042 cam_fill_ataio(&ccb->ataio, 2043 0, 2044 adadone, 2045 CAM_DIR_NONE | flags, 2046 0, 2047 NULL, 2048 0, 2049 ada_default_timeout*1000); 2050 ata_28bit_cmd(&ccb->ataio, cmd, 0, 0, 0); 2051 2052 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 2053 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 2054 softc->disk->d_devstat); 2055 if (error != 0) 2056 xpt_print(periph->path, "Spin-down disk failed\n"); 2057 xpt_release_ccb(ccb); 2058 cam_periph_unlock(periph); 2059 } 2060 } 2061 2062 static void 2063 adashutdown(void *arg, int howto) 2064 { 2065 2066 adaflush(); 2067 if (ada_spindown_shutdown != 0 && 2068 (howto & (RB_HALT | RB_POWEROFF)) != 0) 2069 adaspindown(ATA_STANDBY_IMMEDIATE, 0); 2070 } 2071 2072 static void 2073 adasuspend(void *arg) 2074 { 2075 2076 adaflush(); 2077 if (ada_spindown_suspend != 0) 2078 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE); 2079 } 2080 2081 static void 2082 adaresume(void *arg) 2083 { 2084 struct cam_periph *periph; 2085 struct ada_softc *softc; 2086 2087 if (ada_spindown_suspend == 0) 2088 return; 2089 2090 CAM_PERIPH_FOREACH(periph, &adadriver) { 2091 cam_periph_lock(periph); 2092 softc = (struct ada_softc *)periph->softc; 2093 /* 2094 * We only spin-down the drive if it is capable of it.. 2095 */ 2096 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 2097 cam_periph_unlock(periph); 2098 continue; 2099 } 2100 2101 if (bootverbose) 2102 xpt_print(periph->path, "resume\n"); 2103 2104 /* 2105 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on 2106 * sleep request. 2107 */ 2108 cam_release_devq(periph->path, 2109 /*relsim_flags*/0, 2110 /*openings*/0, 2111 /*timeout*/0, 2112 /*getcount_only*/0); 2113 2114 cam_periph_unlock(periph); 2115 } 2116 } 2117 2118 #endif /* _KERNEL */ 2119