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