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