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 #include "opt_ata.h" 32 33 #include <sys/param.h> 34 35 #ifdef _KERNEL 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/bio.h> 39 #include <sys/sysctl.h> 40 #include <sys/taskqueue.h> 41 #include <sys/lock.h> 42 #include <sys/mutex.h> 43 #include <sys/conf.h> 44 #include <sys/devicestat.h> 45 #include <sys/eventhandler.h> 46 #include <sys/malloc.h> 47 #include <sys/cons.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_WCACHE, 73 ADA_STATE_NORMAL 74 } ada_state; 75 76 typedef enum { 77 ADA_FLAG_PACK_INVALID = 0x001, 78 ADA_FLAG_CAN_48BIT = 0x002, 79 ADA_FLAG_CAN_FLUSHCACHE = 0x004, 80 ADA_FLAG_CAN_NCQ = 0x008, 81 ADA_FLAG_CAN_DMA = 0x010, 82 ADA_FLAG_NEED_OTAG = 0x020, 83 ADA_FLAG_WENT_IDLE = 0x040, 84 ADA_FLAG_CAN_TRIM = 0x080, 85 ADA_FLAG_OPEN = 0x100, 86 ADA_FLAG_SCTX_INIT = 0x200, 87 ADA_FLAG_CAN_CFA = 0x400, 88 ADA_FLAG_CAN_POWERMGT = 0x800 89 } ada_flags; 90 91 typedef enum { 92 ADA_Q_NONE = 0x00, 93 ADA_Q_4K = 0x01, 94 } ada_quirks; 95 96 typedef enum { 97 ADA_CCB_WCACHE = 0x01, 98 ADA_CCB_BUFFER_IO = 0x03, 99 ADA_CCB_WAITING = 0x04, 100 ADA_CCB_DUMP = 0x05, 101 ADA_CCB_TRIM = 0x06, 102 ADA_CCB_TYPE_MASK = 0x0F, 103 } ada_ccb_state; 104 105 /* Offsets into our private area for storing information */ 106 #define ccb_state ppriv_field0 107 #define ccb_bp ppriv_ptr1 108 109 struct disk_params { 110 u_int8_t heads; 111 u_int8_t secs_per_track; 112 u_int32_t cylinders; 113 u_int32_t secsize; /* Number of bytes/logical sector */ 114 u_int64_t sectors; /* Total number sectors */ 115 }; 116 117 #define TRIM_MAX_BLOCKS 8 118 #define TRIM_MAX_RANGES (TRIM_MAX_BLOCKS * 64) 119 #define TRIM_MAX_BIOS (TRIM_MAX_RANGES * 4) 120 struct trim_request { 121 uint8_t data[TRIM_MAX_RANGES * 8]; 122 struct bio *bps[TRIM_MAX_BIOS]; 123 }; 124 125 struct ada_softc { 126 struct bio_queue_head bio_queue; 127 struct bio_queue_head trim_queue; 128 ada_state state; 129 ada_flags flags; 130 ada_quirks quirks; 131 int ordered_tag_count; 132 int outstanding_cmds; 133 int trim_max_ranges; 134 int trim_running; 135 int write_cache; 136 #ifdef ADA_TEST_FAILURE 137 int force_read_error; 138 int force_write_error; 139 int periodic_read_error; 140 int periodic_read_count; 141 #endif 142 struct disk_params params; 143 struct disk *disk; 144 struct task sysctl_task; 145 struct sysctl_ctx_list sysctl_ctx; 146 struct sysctl_oid *sysctl_tree; 147 struct callout sendordered_c; 148 struct trim_request trim_req; 149 }; 150 151 struct ada_quirk_entry { 152 struct scsi_inquiry_pattern inq_pat; 153 ada_quirks quirks; 154 }; 155 156 static struct ada_quirk_entry ada_quirk_table[] = 157 { 158 { 159 /* Hitachi Advanced Format (4k) drives */ 160 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" }, 161 /*quirks*/ADA_Q_4K 162 }, 163 { 164 /* Samsung Advanced Format (4k) drives */ 165 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" }, 166 /*quirks*/ADA_Q_4K 167 }, 168 { 169 /* Seagate Barracuda Green Advanced Format (4k) drives */ 170 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" }, 171 /*quirks*/ADA_Q_4K 172 }, 173 { 174 /* Seagate Momentus Advanced Format (4k) drives */ 175 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" }, 176 /*quirks*/ADA_Q_4K 177 }, 178 { 179 /* Seagate Momentus Advanced Format (4k) drives */ 180 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" }, 181 /*quirks*/ADA_Q_4K 182 }, 183 { 184 /* Seagate Momentus Advanced Format (4k) drives */ 185 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" }, 186 /*quirks*/ADA_Q_4K 187 }, 188 { 189 /* Seagate Momentus Advanced Format (4k) drives */ 190 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" }, 191 /*quirks*/ADA_Q_4K 192 }, 193 { 194 /* Seagate Momentus Thin Advanced Format (4k) drives */ 195 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" }, 196 /*quirks*/ADA_Q_4K 197 }, 198 { 199 /* WDC Caviar Green Advanced Format (4k) drives */ 200 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" }, 201 /*quirks*/ADA_Q_4K 202 }, 203 { 204 /* WDC Caviar Green Advanced Format (4k) drives */ 205 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" }, 206 /*quirks*/ADA_Q_4K 207 }, 208 { 209 /* WDC Caviar Green Advanced Format (4k) drives */ 210 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" }, 211 /*quirks*/ADA_Q_4K 212 }, 213 { 214 /* WDC Caviar Green Advanced Format (4k) drives */ 215 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" }, 216 /*quirks*/ADA_Q_4K 217 }, 218 { 219 /* WDC Scorpio Black Advanced Format (4k) drives */ 220 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" }, 221 /*quirks*/ADA_Q_4K 222 }, 223 { 224 /* WDC Scorpio Black Advanced Format (4k) drives */ 225 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" }, 226 /*quirks*/ADA_Q_4K 227 }, 228 { 229 /* WDC Scorpio Blue Advanced Format (4k) drives */ 230 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" }, 231 /*quirks*/ADA_Q_4K 232 }, 233 { 234 /* WDC Scorpio Blue Advanced Format (4k) drives */ 235 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" }, 236 /*quirks*/ADA_Q_4K 237 }, 238 { 239 /* Default */ 240 { 241 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, 242 /*vendor*/"*", /*product*/"*", /*revision*/"*" 243 }, 244 /*quirks*/0 245 }, 246 }; 247 248 static disk_strategy_t adastrategy; 249 static dumper_t adadump; 250 static periph_init_t adainit; 251 static void adaasync(void *callback_arg, u_int32_t code, 252 struct cam_path *path, void *arg); 253 static void adasysctlinit(void *context, int pending); 254 static periph_ctor_t adaregister; 255 static periph_dtor_t adacleanup; 256 static periph_start_t adastart; 257 static periph_oninv_t adaoninvalidate; 258 static void adadone(struct cam_periph *periph, 259 union ccb *done_ccb); 260 static int adaerror(union ccb *ccb, u_int32_t cam_flags, 261 u_int32_t sense_flags); 262 static void adagetparams(struct cam_periph *periph, 263 struct ccb_getdev *cgd); 264 static timeout_t adasendorderedtag; 265 static void adashutdown(void *arg, int howto); 266 static void adasuspend(void *arg); 267 static void adaresume(void *arg); 268 269 #ifndef ADA_DEFAULT_LEGACY_ALIASES 270 #ifdef ATA_CAM 271 #define ADA_DEFAULT_LEGACY_ALIASES 1 272 #else 273 #define ADA_DEFAULT_LEGACY_ALIASES 0 274 #endif 275 #endif 276 277 #ifndef ADA_DEFAULT_TIMEOUT 278 #define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */ 279 #endif 280 281 #ifndef ADA_DEFAULT_RETRY 282 #define ADA_DEFAULT_RETRY 4 283 #endif 284 285 #ifndef ADA_DEFAULT_SEND_ORDERED 286 #define ADA_DEFAULT_SEND_ORDERED 1 287 #endif 288 289 #ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN 290 #define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1 291 #endif 292 293 #ifndef ADA_DEFAULT_SPINDOWN_SUSPEND 294 #define ADA_DEFAULT_SPINDOWN_SUSPEND 1 295 #endif 296 297 #ifndef ADA_DEFAULT_WRITE_CACHE 298 #define ADA_DEFAULT_WRITE_CACHE 1 299 #endif 300 301 /* 302 * Most platforms map firmware geometry to actual, but some don't. If 303 * not overridden, default to nothing. 304 */ 305 #ifndef ata_disk_firmware_geom_adjust 306 #define ata_disk_firmware_geom_adjust(disk) 307 #endif 308 309 static int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES; 310 static int ada_retry_count = ADA_DEFAULT_RETRY; 311 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT; 312 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED; 313 static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN; 314 static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND; 315 static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE; 316 317 SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0, 318 "CAM Direct Access Disk driver"); 319 SYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RW, 320 &ada_legacy_aliases, 0, "Create legacy-like device aliases"); 321 TUNABLE_INT("kern.cam.ada.legacy_aliases", &ada_legacy_aliases); 322 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW, 323 &ada_retry_count, 0, "Normal I/O retry count"); 324 TUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count); 325 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW, 326 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)"); 327 TUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout); 328 SYSCTL_INT(_kern_cam_ada, OID_AUTO, ada_send_ordered, CTLFLAG_RW, 329 &ada_send_ordered, 0, "Send Ordered Tags"); 330 TUNABLE_INT("kern.cam.ada.ada_send_ordered", &ada_send_ordered); 331 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RW, 332 &ada_spindown_shutdown, 0, "Spin down upon shutdown"); 333 TUNABLE_INT("kern.cam.ada.spindown_shutdown", &ada_spindown_shutdown); 334 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RW, 335 &ada_spindown_suspend, 0, "Spin down upon suspend"); 336 TUNABLE_INT("kern.cam.ada.spindown_suspend", &ada_spindown_suspend); 337 SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RW, 338 &ada_write_cache, 0, "Enable disk write cache"); 339 TUNABLE_INT("kern.cam.ada.write_cache", &ada_write_cache); 340 341 /* 342 * ADA_ORDEREDTAG_INTERVAL determines how often, relative 343 * to the default timeout, we check to see whether an ordered 344 * tagged transaction is appropriate to prevent simple tag 345 * starvation. Since we'd like to ensure that there is at least 346 * 1/2 of the timeout length left for a starved transaction to 347 * complete after we've sent an ordered tag, we must poll at least 348 * four times in every timeout period. This takes care of the worst 349 * case where a starved transaction starts during an interval that 350 * meets the requirement "don't send an ordered tag" test so it takes 351 * us two intervals to determine that a tag must be sent. 352 */ 353 #ifndef ADA_ORDEREDTAG_INTERVAL 354 #define ADA_ORDEREDTAG_INTERVAL 4 355 #endif 356 357 static struct periph_driver adadriver = 358 { 359 adainit, "ada", 360 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0 361 }; 362 363 PERIPHDRIVER_DECLARE(ada, adadriver); 364 365 MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers"); 366 367 static int 368 adaopen(struct disk *dp) 369 { 370 struct cam_periph *periph; 371 struct ada_softc *softc; 372 int error; 373 374 periph = (struct cam_periph *)dp->d_drv1; 375 if (periph == NULL) { 376 return (ENXIO); 377 } 378 379 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 380 return(ENXIO); 381 } 382 383 cam_periph_lock(periph); 384 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) { 385 cam_periph_unlock(periph); 386 cam_periph_release(periph); 387 return (error); 388 } 389 390 softc = (struct ada_softc *)periph->softc; 391 softc->flags |= ADA_FLAG_OPEN; 392 393 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 394 ("adaopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit, 395 periph->unit_number)); 396 397 if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) { 398 /* Invalidate our pack information. */ 399 softc->flags &= ~ADA_FLAG_PACK_INVALID; 400 } 401 402 cam_periph_unhold(periph); 403 cam_periph_unlock(periph); 404 return (0); 405 } 406 407 static int 408 adaclose(struct disk *dp) 409 { 410 struct cam_periph *periph; 411 struct ada_softc *softc; 412 union ccb *ccb; 413 int error; 414 415 periph = (struct cam_periph *)dp->d_drv1; 416 if (periph == NULL) 417 return (ENXIO); 418 419 cam_periph_lock(periph); 420 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) { 421 cam_periph_unlock(periph); 422 cam_periph_release(periph); 423 return (error); 424 } 425 426 softc = (struct ada_softc *)periph->softc; 427 /* We only sync the cache if the drive is capable of it. */ 428 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) { 429 430 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 431 cam_fill_ataio(&ccb->ataio, 432 1, 433 adadone, 434 CAM_DIR_NONE, 435 0, 436 NULL, 437 0, 438 ada_default_timeout*1000); 439 440 if (softc->flags & ADA_FLAG_CAN_48BIT) 441 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 442 else 443 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 444 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0, 445 /*sense_flags*/0, softc->disk->d_devstat); 446 447 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 448 xpt_print(periph->path, "Synchronize cache failed\n"); 449 450 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 451 cam_release_devq(ccb->ccb_h.path, 452 /*relsim_flags*/0, 453 /*reduction*/0, 454 /*timeout*/0, 455 /*getcount_only*/0); 456 xpt_release_ccb(ccb); 457 } 458 459 softc->flags &= ~ADA_FLAG_OPEN; 460 cam_periph_unhold(periph); 461 cam_periph_unlock(periph); 462 cam_periph_release(periph); 463 return (0); 464 } 465 466 static void 467 adaschedule(struct cam_periph *periph) 468 { 469 struct ada_softc *softc = (struct ada_softc *)periph->softc; 470 471 if (bioq_first(&softc->bio_queue) || 472 (!softc->trim_running && bioq_first(&softc->trim_queue))) { 473 /* Have more work to do, so ensure we stay scheduled */ 474 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 475 } 476 } 477 478 /* 479 * Actually translate the requested transfer into one the physical driver 480 * can understand. The transfer is described by a buf and will include 481 * only one physical transfer. 482 */ 483 static void 484 adastrategy(struct bio *bp) 485 { 486 struct cam_periph *periph; 487 struct ada_softc *softc; 488 489 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 490 if (periph == NULL) { 491 biofinish(bp, NULL, ENXIO); 492 return; 493 } 494 softc = (struct ada_softc *)periph->softc; 495 496 cam_periph_lock(periph); 497 498 /* 499 * If the device has been made invalid, error out 500 */ 501 if ((softc->flags & ADA_FLAG_PACK_INVALID)) { 502 cam_periph_unlock(periph); 503 biofinish(bp, NULL, ENXIO); 504 return; 505 } 506 507 /* 508 * Place it in the queue of disk activities for this disk 509 */ 510 if (bp->bio_cmd == BIO_DELETE && 511 (softc->flags & ADA_FLAG_CAN_TRIM)) 512 bioq_disksort(&softc->trim_queue, bp); 513 else 514 bioq_disksort(&softc->bio_queue, bp); 515 516 /* 517 * Schedule ourselves for performing the work. 518 */ 519 adaschedule(periph); 520 cam_periph_unlock(periph); 521 522 return; 523 } 524 525 static int 526 adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) 527 { 528 struct cam_periph *periph; 529 struct ada_softc *softc; 530 u_int secsize; 531 union ccb ccb; 532 struct disk *dp; 533 uint64_t lba; 534 uint16_t count; 535 536 dp = arg; 537 periph = dp->d_drv1; 538 if (periph == NULL) 539 return (ENXIO); 540 softc = (struct ada_softc *)periph->softc; 541 cam_periph_lock(periph); 542 secsize = softc->params.secsize; 543 lba = offset / secsize; 544 count = length / secsize; 545 546 if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) { 547 cam_periph_unlock(periph); 548 return (ENXIO); 549 } 550 551 if (length > 0) { 552 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 553 ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 554 cam_fill_ataio(&ccb.ataio, 555 0, 556 adadone, 557 CAM_DIR_OUT, 558 0, 559 (u_int8_t *) virtual, 560 length, 561 ada_default_timeout*1000); 562 if ((softc->flags & ADA_FLAG_CAN_48BIT) && 563 (lba + count >= ATA_MAX_28BIT_LBA || 564 count >= 256)) { 565 ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48, 566 0, lba, count); 567 } else { 568 ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA, 569 0, lba, count); 570 } 571 xpt_polled_action(&ccb); 572 573 if ((ccb.ataio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 574 printf("Aborting dump due to I/O error.\n"); 575 cam_periph_unlock(periph); 576 return(EIO); 577 } 578 cam_periph_unlock(periph); 579 return(0); 580 } 581 582 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) { 583 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 584 585 ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 586 cam_fill_ataio(&ccb.ataio, 587 1, 588 adadone, 589 CAM_DIR_NONE, 590 0, 591 NULL, 592 0, 593 ada_default_timeout*1000); 594 595 if (softc->flags & ADA_FLAG_CAN_48BIT) 596 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0); 597 else 598 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0); 599 xpt_polled_action(&ccb); 600 601 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 602 xpt_print(periph->path, "Synchronize cache failed\n"); 603 604 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 605 cam_release_devq(ccb.ccb_h.path, 606 /*relsim_flags*/0, 607 /*reduction*/0, 608 /*timeout*/0, 609 /*getcount_only*/0); 610 } 611 cam_periph_unlock(periph); 612 return (0); 613 } 614 615 static void 616 adainit(void) 617 { 618 cam_status status; 619 620 /* 621 * Install a global async callback. This callback will 622 * receive async callbacks like "new device found". 623 */ 624 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL); 625 626 if (status != CAM_REQ_CMP) { 627 printf("ada: Failed to attach master async callback " 628 "due to status 0x%x!\n", status); 629 } else if (ada_send_ordered) { 630 631 /* Register our event handlers */ 632 if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend, 633 NULL, EVENTHANDLER_PRI_LAST)) == NULL) 634 printf("adainit: power event registration failed!\n"); 635 if ((EVENTHANDLER_REGISTER(power_resume, adaresume, 636 NULL, EVENTHANDLER_PRI_LAST)) == NULL) 637 printf("adainit: power event registration failed!\n"); 638 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown, 639 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 640 printf("adainit: shutdown event registration failed!\n"); 641 } 642 } 643 644 static void 645 adaoninvalidate(struct cam_periph *periph) 646 { 647 struct ada_softc *softc; 648 649 softc = (struct ada_softc *)periph->softc; 650 651 /* 652 * De-register any async callbacks. 653 */ 654 xpt_register_async(0, adaasync, periph, periph->path); 655 656 softc->flags |= ADA_FLAG_PACK_INVALID; 657 658 /* 659 * Return all queued I/O with ENXIO. 660 * XXX Handle any transactions queued to the card 661 * with XPT_ABORT_CCB. 662 */ 663 bioq_flush(&softc->bio_queue, NULL, ENXIO); 664 bioq_flush(&softc->trim_queue, NULL, ENXIO); 665 666 disk_gone(softc->disk); 667 xpt_print(periph->path, "lost device\n"); 668 } 669 670 static void 671 adacleanup(struct cam_periph *periph) 672 { 673 struct ada_softc *softc; 674 675 softc = (struct ada_softc *)periph->softc; 676 677 xpt_print(periph->path, "removing device entry\n"); 678 cam_periph_unlock(periph); 679 680 /* 681 * If we can't free the sysctl tree, oh well... 682 */ 683 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0 684 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) { 685 xpt_print(periph->path, "can't remove sysctl context\n"); 686 } 687 688 disk_destroy(softc->disk); 689 callout_drain(&softc->sendordered_c); 690 free(softc, M_DEVBUF); 691 cam_periph_lock(periph); 692 } 693 694 static void 695 adaasync(void *callback_arg, u_int32_t code, 696 struct cam_path *path, void *arg) 697 { 698 struct cam_periph *periph; 699 struct ada_softc *softc; 700 701 periph = (struct cam_periph *)callback_arg; 702 switch (code) { 703 case AC_FOUND_DEVICE: 704 { 705 struct ccb_getdev *cgd; 706 cam_status status; 707 708 cgd = (struct ccb_getdev *)arg; 709 if (cgd == NULL) 710 break; 711 712 if (cgd->protocol != PROTO_ATA) 713 break; 714 715 /* 716 * Allocate a peripheral instance for 717 * this device and start the probe 718 * process. 719 */ 720 status = cam_periph_alloc(adaregister, adaoninvalidate, 721 adacleanup, adastart, 722 "ada", CAM_PERIPH_BIO, 723 cgd->ccb_h.path, adaasync, 724 AC_FOUND_DEVICE, cgd); 725 726 if (status != CAM_REQ_CMP 727 && status != CAM_REQ_INPROG) 728 printf("adaasync: Unable to attach to new device " 729 "due to status 0x%x\n", status); 730 break; 731 } 732 case AC_SENT_BDR: 733 case AC_BUS_RESET: 734 { 735 struct ccb_getdev cgd; 736 737 softc = (struct ada_softc *)periph->softc; 738 cam_periph_async(periph, code, path, arg); 739 if (ada_write_cache < 0 && softc->write_cache < 0) 740 break; 741 if (softc->state != ADA_STATE_NORMAL) 742 break; 743 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 744 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 745 xpt_action((union ccb *)&cgd); 746 if ((cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) == 0) 747 break; 748 softc->state = ADA_STATE_WCACHE; 749 cam_periph_acquire(periph); 750 cam_freeze_devq_arg(periph->path, 751 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1); 752 xpt_schedule(periph, CAM_PRIORITY_DEV); 753 } 754 default: 755 cam_periph_async(periph, code, path, arg); 756 break; 757 } 758 } 759 760 static void 761 adasysctlinit(void *context, int pending) 762 { 763 struct cam_periph *periph; 764 struct ada_softc *softc; 765 char tmpstr[80], tmpstr2[80]; 766 767 periph = (struct cam_periph *)context; 768 769 /* periph was held for us when this task was enqueued */ 770 if (periph->flags & CAM_PERIPH_INVALID) { 771 cam_periph_release(periph); 772 return; 773 } 774 775 softc = (struct ada_softc *)periph->softc; 776 snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number); 777 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 778 779 sysctl_ctx_init(&softc->sysctl_ctx); 780 softc->flags |= ADA_FLAG_SCTX_INIT; 781 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 782 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2, 783 CTLFLAG_RD, 0, tmpstr); 784 if (softc->sysctl_tree == NULL) { 785 printf("adasysctlinit: unable to allocate sysctl tree\n"); 786 cam_periph_release(periph); 787 return; 788 } 789 790 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 791 OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE, 792 &softc->write_cache, 0, "Enable disk write cache."); 793 #ifdef ADA_TEST_FAILURE 794 /* 795 * Add a 'door bell' sysctl which allows one to set it from userland 796 * and cause something bad to happen. For the moment, we only allow 797 * whacking the next read or write. 798 */ 799 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 800 OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 801 &softc->force_read_error, 0, 802 "Force a read error for the next N reads."); 803 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 804 OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 805 &softc->force_write_error, 0, 806 "Force a write error for the next N writes."); 807 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 808 OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 809 &softc->periodic_read_error, 0, 810 "Force a read error every N reads (don't set too low)."); 811 #endif 812 cam_periph_release(periph); 813 } 814 815 static int 816 adagetattr(struct bio *bp) 817 { 818 int ret = -1; 819 struct cam_periph *periph; 820 821 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL) 822 return ENXIO; 823 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 824 if (periph->path == NULL) 825 return ENXIO; 826 827 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute, 828 periph->path); 829 if (ret == 0) 830 bp->bio_completed = bp->bio_length; 831 return ret; 832 } 833 834 static cam_status 835 adaregister(struct cam_periph *periph, void *arg) 836 { 837 struct ada_softc *softc; 838 struct ccb_pathinq cpi; 839 struct ccb_getdev *cgd; 840 char announce_buf[80], buf1[32]; 841 struct disk_params *dp; 842 caddr_t match; 843 u_int maxio; 844 int legacy_id, quirks; 845 846 cgd = (struct ccb_getdev *)arg; 847 if (periph == NULL) { 848 printf("adaregister: periph was NULL!!\n"); 849 return(CAM_REQ_CMP_ERR); 850 } 851 852 if (cgd == NULL) { 853 printf("adaregister: no getdev CCB, can't register device\n"); 854 return(CAM_REQ_CMP_ERR); 855 } 856 857 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF, 858 M_NOWAIT|M_ZERO); 859 860 if (softc == NULL) { 861 printf("adaregister: Unable to probe new device. " 862 "Unable to allocate softc\n"); 863 return(CAM_REQ_CMP_ERR); 864 } 865 866 bioq_init(&softc->bio_queue); 867 bioq_init(&softc->trim_queue); 868 869 if (cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA && 870 (cgd->inq_flags & SID_DMA)) 871 softc->flags |= ADA_FLAG_CAN_DMA; 872 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) 873 softc->flags |= ADA_FLAG_CAN_48BIT; 874 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE) 875 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE; 876 if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT) 877 softc->flags |= ADA_FLAG_CAN_POWERMGT; 878 if (cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ && 879 (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue)) 880 softc->flags |= ADA_FLAG_CAN_NCQ; 881 if (cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) { 882 softc->flags |= ADA_FLAG_CAN_TRIM; 883 softc->trim_max_ranges = TRIM_MAX_RANGES; 884 if (cgd->ident_data.max_dsm_blocks != 0) { 885 softc->trim_max_ranges = 886 min(cgd->ident_data.max_dsm_blocks * 64, 887 softc->trim_max_ranges); 888 } 889 } 890 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA) 891 softc->flags |= ADA_FLAG_CAN_CFA; 892 893 periph->softc = softc; 894 895 /* 896 * See if this device has any quirks. 897 */ 898 match = cam_quirkmatch((caddr_t)&cgd->ident_data, 899 (caddr_t)ada_quirk_table, 900 sizeof(ada_quirk_table)/sizeof(*ada_quirk_table), 901 sizeof(*ada_quirk_table), ata_identify_match); 902 if (match != NULL) 903 softc->quirks = ((struct ada_quirk_entry *)match)->quirks; 904 else 905 softc->quirks = ADA_Q_NONE; 906 907 bzero(&cpi, sizeof(cpi)); 908 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE); 909 cpi.ccb_h.func_code = XPT_PATH_INQ; 910 xpt_action((union ccb *)&cpi); 911 912 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph); 913 914 /* 915 * Register this media as a disk 916 */ 917 (void)cam_periph_hold(periph, PRIBIO); 918 mtx_unlock(periph->sim->mtx); 919 snprintf(announce_buf, sizeof(announce_buf), 920 "kern.cam.ada.%d.quirks", periph->unit_number); 921 quirks = softc->quirks; 922 TUNABLE_INT_FETCH(announce_buf, &quirks); 923 softc->quirks = quirks; 924 softc->write_cache = -1; 925 snprintf(announce_buf, sizeof(announce_buf), 926 "kern.cam.ada.%d.write_cache", periph->unit_number); 927 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache); 928 adagetparams(periph, cgd); 929 softc->disk = disk_alloc(); 930 softc->disk->d_devstat = devstat_new_entry(periph->periph_name, 931 periph->unit_number, softc->params.secsize, 932 DEVSTAT_ALL_SUPPORTED, 933 DEVSTAT_TYPE_DIRECT | 934 XPORT_DEVSTAT_TYPE(cpi.transport), 935 DEVSTAT_PRIORITY_DISK); 936 softc->disk->d_open = adaopen; 937 softc->disk->d_close = adaclose; 938 softc->disk->d_strategy = adastrategy; 939 softc->disk->d_getattr = adagetattr; 940 softc->disk->d_dump = adadump; 941 softc->disk->d_name = "ada"; 942 softc->disk->d_drv1 = periph; 943 maxio = cpi.maxio; /* Honor max I/O size of SIM */ 944 if (maxio == 0) 945 maxio = DFLTPHYS; /* traditional default */ 946 else if (maxio > MAXPHYS) 947 maxio = MAXPHYS; /* for safety */ 948 if (softc->flags & ADA_FLAG_CAN_48BIT) 949 maxio = min(maxio, 65536 * softc->params.secsize); 950 else /* 28bit ATA command limit */ 951 maxio = min(maxio, 256 * softc->params.secsize); 952 softc->disk->d_maxsize = maxio; 953 softc->disk->d_unit = periph->unit_number; 954 softc->disk->d_flags = 0; 955 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) 956 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE; 957 if ((softc->flags & ADA_FLAG_CAN_TRIM) || 958 ((softc->flags & ADA_FLAG_CAN_CFA) && 959 !(softc->flags & ADA_FLAG_CAN_48BIT))) 960 softc->disk->d_flags |= DISKFLAG_CANDELETE; 961 strlcpy(softc->disk->d_descr, cgd->ident_data.model, 962 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model))); 963 softc->disk->d_hba_vendor = cpi.hba_vendor; 964 softc->disk->d_hba_device = cpi.hba_device; 965 softc->disk->d_hba_subvendor = cpi.hba_subvendor; 966 softc->disk->d_hba_subdevice = cpi.hba_subdevice; 967 968 softc->disk->d_sectorsize = softc->params.secsize; 969 softc->disk->d_mediasize = (off_t)softc->params.sectors * 970 softc->params.secsize; 971 if (ata_physical_sector_size(&cgd->ident_data) != 972 softc->params.secsize) { 973 softc->disk->d_stripesize = 974 ata_physical_sector_size(&cgd->ident_data); 975 softc->disk->d_stripeoffset = (softc->disk->d_stripesize - 976 ata_logical_sector_offset(&cgd->ident_data)) % 977 softc->disk->d_stripesize; 978 } else if (softc->quirks & ADA_Q_4K) { 979 softc->disk->d_stripesize = 4096; 980 softc->disk->d_stripeoffset = 0; 981 } 982 softc->disk->d_fwsectors = softc->params.secs_per_track; 983 softc->disk->d_fwheads = softc->params.heads; 984 ata_disk_firmware_geom_adjust(softc->disk); 985 986 if (ada_legacy_aliases) { 987 #ifdef ATA_STATIC_ID 988 legacy_id = xpt_path_legacy_ata_id(periph->path); 989 #else 990 legacy_id = softc->disk->d_unit; 991 #endif 992 if (legacy_id >= 0) { 993 snprintf(announce_buf, sizeof(announce_buf), 994 "kern.devalias.%s%d", 995 softc->disk->d_name, softc->disk->d_unit); 996 snprintf(buf1, sizeof(buf1), 997 "ad%d", legacy_id); 998 setenv(announce_buf, buf1); 999 } 1000 } else 1001 legacy_id = -1; 1002 disk_create(softc->disk, DISK_VERSION); 1003 mtx_lock(periph->sim->mtx); 1004 cam_periph_unhold(periph); 1005 1006 dp = &softc->params; 1007 snprintf(announce_buf, sizeof(announce_buf), 1008 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)", 1009 (uintmax_t)(((uintmax_t)dp->secsize * 1010 dp->sectors) / (1024*1024)), 1011 (uintmax_t)dp->sectors, 1012 dp->secsize, dp->heads, 1013 dp->secs_per_track, dp->cylinders); 1014 xpt_announce_periph(periph, announce_buf); 1015 if (legacy_id >= 0) 1016 printf("%s%d: Previously was known as ad%d\n", 1017 periph->periph_name, periph->unit_number, legacy_id); 1018 1019 /* 1020 * Create our sysctl variables, now that we know 1021 * we have successfully attached. 1022 */ 1023 cam_periph_acquire(periph); 1024 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task); 1025 1026 /* 1027 * Add async callbacks for bus reset and 1028 * bus device reset calls. I don't bother 1029 * checking if this fails as, in most cases, 1030 * the system will function just fine without 1031 * them and the only alternative would be to 1032 * not attach the device on failure. 1033 */ 1034 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE, 1035 adaasync, periph, periph->path); 1036 1037 /* 1038 * Schedule a periodic event to occasionally send an 1039 * ordered tag to a device. 1040 */ 1041 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0); 1042 callout_reset(&softc->sendordered_c, 1043 (ADA_DEFAULT_TIMEOUT * hz) / ADA_ORDEREDTAG_INTERVAL, 1044 adasendorderedtag, softc); 1045 1046 if ((ada_write_cache >= 0 || softc->write_cache >= 0) && 1047 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) { 1048 softc->state = ADA_STATE_WCACHE; 1049 cam_periph_acquire(periph); 1050 cam_freeze_devq_arg(periph->path, 1051 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1); 1052 xpt_schedule(periph, CAM_PRIORITY_DEV); 1053 } else 1054 softc->state = ADA_STATE_NORMAL; 1055 1056 return(CAM_REQ_CMP); 1057 } 1058 1059 static void 1060 adastart(struct cam_periph *periph, union ccb *start_ccb) 1061 { 1062 struct ada_softc *softc = (struct ada_softc *)periph->softc; 1063 struct ccb_ataio *ataio = &start_ccb->ataio; 1064 1065 switch (softc->state) { 1066 case ADA_STATE_NORMAL: 1067 { 1068 struct bio *bp; 1069 u_int8_t tag_code; 1070 1071 /* Execute immediate CCB if waiting. */ 1072 if (periph->immediate_priority <= periph->pinfo.priority) { 1073 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE, 1074 ("queuing for immediate ccb\n")); 1075 start_ccb->ccb_h.ccb_state = ADA_CCB_WAITING; 1076 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 1077 periph_links.sle); 1078 periph->immediate_priority = CAM_PRIORITY_NONE; 1079 wakeup(&periph->ccb_list); 1080 /* Have more work to do, so ensure we stay scheduled */ 1081 adaschedule(periph); 1082 break; 1083 } 1084 /* Run TRIM if not running yet. */ 1085 if (!softc->trim_running && 1086 (bp = bioq_first(&softc->trim_queue)) != 0) { 1087 struct trim_request *req = &softc->trim_req; 1088 struct bio *bp1; 1089 uint64_t lastlba = (uint64_t)-1; 1090 int bps = 0, c, lastcount = 0, off, ranges = 0; 1091 1092 softc->trim_running = 1; 1093 bzero(req, sizeof(*req)); 1094 bp1 = bp; 1095 do { 1096 uint64_t lba = bp1->bio_pblkno; 1097 int count = bp1->bio_bcount / 1098 softc->params.secsize; 1099 1100 bioq_remove(&softc->trim_queue, bp1); 1101 1102 /* Try to extend the previous range. */ 1103 if (lba == lastlba) { 1104 c = min(count, 0xffff - lastcount); 1105 lastcount += c; 1106 off = (ranges - 1) * 8; 1107 req->data[off + 6] = lastcount & 0xff; 1108 req->data[off + 7] = 1109 (lastcount >> 8) & 0xff; 1110 count -= c; 1111 lba += c; 1112 } 1113 1114 while (count > 0) { 1115 c = min(count, 0xffff); 1116 off = ranges * 8; 1117 req->data[off + 0] = lba & 0xff; 1118 req->data[off + 1] = (lba >> 8) & 0xff; 1119 req->data[off + 2] = (lba >> 16) & 0xff; 1120 req->data[off + 3] = (lba >> 24) & 0xff; 1121 req->data[off + 4] = (lba >> 32) & 0xff; 1122 req->data[off + 5] = (lba >> 40) & 0xff; 1123 req->data[off + 6] = c & 0xff; 1124 req->data[off + 7] = (c >> 8) & 0xff; 1125 lba += c; 1126 count -= c; 1127 lastcount = c; 1128 ranges++; 1129 } 1130 lastlba = lba; 1131 req->bps[bps++] = bp1; 1132 bp1 = bioq_first(&softc->trim_queue); 1133 if (bps >= TRIM_MAX_BIOS || 1134 bp1 == NULL || 1135 bp1->bio_bcount / softc->params.secsize > 1136 (softc->trim_max_ranges - ranges) * 0xffff) 1137 break; 1138 } while (1); 1139 cam_fill_ataio(ataio, 1140 ada_retry_count, 1141 adadone, 1142 CAM_DIR_OUT, 1143 0, 1144 req->data, 1145 ((ranges + 63) / 64) * 512, 1146 ada_default_timeout * 1000); 1147 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT, 1148 ATA_DSM_TRIM, 0, (ranges + 63) / 64); 1149 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM; 1150 goto out; 1151 } 1152 /* Run regular command. */ 1153 bp = bioq_first(&softc->bio_queue); 1154 if (bp == NULL) { 1155 xpt_release_ccb(start_ccb); 1156 break; 1157 } 1158 bioq_remove(&softc->bio_queue, bp); 1159 1160 if ((bp->bio_flags & BIO_ORDERED) != 0 1161 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) { 1162 softc->flags &= ~ADA_FLAG_NEED_OTAG; 1163 softc->ordered_tag_count++; 1164 tag_code = 0; 1165 } else { 1166 tag_code = 1; 1167 } 1168 switch (bp->bio_cmd) { 1169 case BIO_READ: 1170 case BIO_WRITE: 1171 { 1172 uint64_t lba = bp->bio_pblkno; 1173 uint16_t count = bp->bio_bcount / softc->params.secsize; 1174 #ifdef ADA_TEST_FAILURE 1175 int fail = 0; 1176 1177 /* 1178 * Support the failure ioctls. If the command is a 1179 * read, and there are pending forced read errors, or 1180 * if a write and pending write errors, then fail this 1181 * operation with EIO. This is useful for testing 1182 * purposes. Also, support having every Nth read fail. 1183 * 1184 * This is a rather blunt tool. 1185 */ 1186 if (bp->bio_cmd == BIO_READ) { 1187 if (softc->force_read_error) { 1188 softc->force_read_error--; 1189 fail = 1; 1190 } 1191 if (softc->periodic_read_error > 0) { 1192 if (++softc->periodic_read_count >= 1193 softc->periodic_read_error) { 1194 softc->periodic_read_count = 0; 1195 fail = 1; 1196 } 1197 } 1198 } else { 1199 if (softc->force_write_error) { 1200 softc->force_write_error--; 1201 fail = 1; 1202 } 1203 } 1204 if (fail) { 1205 bp->bio_error = EIO; 1206 bp->bio_flags |= BIO_ERROR; 1207 biodone(bp); 1208 xpt_release_ccb(start_ccb); 1209 adaschedule(periph); 1210 return; 1211 } 1212 #endif 1213 cam_fill_ataio(ataio, 1214 ada_retry_count, 1215 adadone, 1216 bp->bio_cmd == BIO_READ ? 1217 CAM_DIR_IN : CAM_DIR_OUT, 1218 tag_code, 1219 bp->bio_data, 1220 bp->bio_bcount, 1221 ada_default_timeout*1000); 1222 1223 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) { 1224 if (bp->bio_cmd == BIO_READ) { 1225 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED, 1226 lba, count); 1227 } else { 1228 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED, 1229 lba, count); 1230 } 1231 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) && 1232 (lba + count >= ATA_MAX_28BIT_LBA || 1233 count > 256)) { 1234 if (softc->flags & ADA_FLAG_CAN_DMA) { 1235 if (bp->bio_cmd == BIO_READ) { 1236 ata_48bit_cmd(ataio, ATA_READ_DMA48, 1237 0, lba, count); 1238 } else { 1239 ata_48bit_cmd(ataio, ATA_WRITE_DMA48, 1240 0, lba, count); 1241 } 1242 } else { 1243 if (bp->bio_cmd == BIO_READ) { 1244 ata_48bit_cmd(ataio, ATA_READ_MUL48, 1245 0, lba, count); 1246 } else { 1247 ata_48bit_cmd(ataio, ATA_WRITE_MUL48, 1248 0, lba, count); 1249 } 1250 } 1251 } else { 1252 if (count == 256) 1253 count = 0; 1254 if (softc->flags & ADA_FLAG_CAN_DMA) { 1255 if (bp->bio_cmd == BIO_READ) { 1256 ata_28bit_cmd(ataio, ATA_READ_DMA, 1257 0, lba, count); 1258 } else { 1259 ata_28bit_cmd(ataio, ATA_WRITE_DMA, 1260 0, lba, count); 1261 } 1262 } else { 1263 if (bp->bio_cmd == BIO_READ) { 1264 ata_28bit_cmd(ataio, ATA_READ_MUL, 1265 0, lba, count); 1266 } else { 1267 ata_28bit_cmd(ataio, ATA_WRITE_MUL, 1268 0, lba, count); 1269 } 1270 } 1271 } 1272 break; 1273 } 1274 case BIO_DELETE: 1275 { 1276 uint64_t lba = bp->bio_pblkno; 1277 uint16_t count = bp->bio_bcount / softc->params.secsize; 1278 1279 cam_fill_ataio(ataio, 1280 ada_retry_count, 1281 adadone, 1282 CAM_DIR_NONE, 1283 0, 1284 NULL, 1285 0, 1286 ada_default_timeout*1000); 1287 1288 if (count >= 256) 1289 count = 0; 1290 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count); 1291 break; 1292 } 1293 case BIO_FLUSH: 1294 cam_fill_ataio(ataio, 1295 1, 1296 adadone, 1297 CAM_DIR_NONE, 1298 0, 1299 NULL, 1300 0, 1301 ada_default_timeout*1000); 1302 1303 if (softc->flags & ADA_FLAG_CAN_48BIT) 1304 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1305 else 1306 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0); 1307 break; 1308 } 1309 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO; 1310 out: 1311 start_ccb->ccb_h.ccb_bp = bp; 1312 softc->outstanding_cmds++; 1313 xpt_action(start_ccb); 1314 1315 /* May have more work to do, so ensure we stay scheduled */ 1316 adaschedule(periph); 1317 break; 1318 } 1319 case ADA_STATE_WCACHE: 1320 { 1321 cam_fill_ataio(ataio, 1322 1, 1323 adadone, 1324 CAM_DIR_NONE, 1325 0, 1326 NULL, 1327 0, 1328 ada_default_timeout*1000); 1329 1330 ata_28bit_cmd(ataio, ATA_SETFEATURES, (softc->write_cache > 0 || 1331 (softc->write_cache < 0 && ada_write_cache)) ? 1332 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0); 1333 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE; 1334 xpt_action(start_ccb); 1335 break; 1336 } 1337 } 1338 } 1339 1340 static void 1341 adadone(struct cam_periph *periph, union ccb *done_ccb) 1342 { 1343 struct ada_softc *softc; 1344 struct ccb_ataio *ataio; 1345 1346 softc = (struct ada_softc *)periph->softc; 1347 ataio = &done_ccb->ataio; 1348 switch (ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) { 1349 case ADA_CCB_BUFFER_IO: 1350 case ADA_CCB_TRIM: 1351 { 1352 struct bio *bp; 1353 1354 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 1355 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1356 int error; 1357 1358 error = adaerror(done_ccb, 0, 0); 1359 if (error == ERESTART) { 1360 /* A retry was scheduled, so just return. */ 1361 return; 1362 } 1363 if (error != 0) { 1364 if (error == ENXIO) { 1365 /* 1366 * Catastrophic error. Mark our pack as 1367 * invalid. 1368 */ 1369 /* 1370 * XXX See if this is really a media 1371 * XXX change first? 1372 */ 1373 xpt_print(periph->path, 1374 "Invalidating pack\n"); 1375 softc->flags |= ADA_FLAG_PACK_INVALID; 1376 } 1377 bp->bio_error = error; 1378 bp->bio_resid = bp->bio_bcount; 1379 bp->bio_flags |= BIO_ERROR; 1380 } else { 1381 bp->bio_resid = ataio->resid; 1382 bp->bio_error = 0; 1383 if (bp->bio_resid != 0) 1384 bp->bio_flags |= BIO_ERROR; 1385 } 1386 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1387 cam_release_devq(done_ccb->ccb_h.path, 1388 /*relsim_flags*/0, 1389 /*reduction*/0, 1390 /*timeout*/0, 1391 /*getcount_only*/0); 1392 } else { 1393 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1394 panic("REQ_CMP with QFRZN"); 1395 bp->bio_resid = ataio->resid; 1396 if (ataio->resid > 0) 1397 bp->bio_flags |= BIO_ERROR; 1398 } 1399 softc->outstanding_cmds--; 1400 if (softc->outstanding_cmds == 0) 1401 softc->flags |= ADA_FLAG_WENT_IDLE; 1402 if ((ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) == 1403 ADA_CCB_TRIM) { 1404 struct trim_request *req = 1405 (struct trim_request *)ataio->data_ptr; 1406 int i; 1407 1408 for (i = 1; i < TRIM_MAX_BIOS && req->bps[i]; i++) { 1409 struct bio *bp1 = req->bps[i]; 1410 1411 bp1->bio_resid = bp->bio_resid; 1412 bp1->bio_error = bp->bio_error; 1413 if (bp->bio_flags & BIO_ERROR) 1414 bp1->bio_flags |= BIO_ERROR; 1415 biodone(bp1); 1416 } 1417 softc->trim_running = 0; 1418 biodone(bp); 1419 adaschedule(periph); 1420 } else 1421 biodone(bp); 1422 break; 1423 } 1424 case ADA_CCB_WCACHE: 1425 { 1426 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1427 if (adaerror(done_ccb, 0, 0) == ERESTART) { 1428 return; 1429 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1430 cam_release_devq(done_ccb->ccb_h.path, 1431 /*relsim_flags*/0, 1432 /*reduction*/0, 1433 /*timeout*/0, 1434 /*getcount_only*/0); 1435 } 1436 } 1437 1438 softc->state = ADA_STATE_NORMAL; 1439 /* 1440 * Since our peripheral may be invalidated by an error 1441 * above or an external event, we must release our CCB 1442 * before releasing the reference on the peripheral. 1443 * The peripheral will only go away once the last reference 1444 * is removed, and we need it around for the CCB release 1445 * operation. 1446 */ 1447 xpt_release_ccb(done_ccb); 1448 cam_release_devq(periph->path, 1449 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE); 1450 adaschedule(periph); 1451 cam_periph_release_locked(periph); 1452 return; 1453 } 1454 case ADA_CCB_WAITING: 1455 { 1456 /* Caller will release the CCB */ 1457 wakeup(&done_ccb->ccb_h.cbfcnp); 1458 return; 1459 } 1460 case ADA_CCB_DUMP: 1461 /* No-op. We're polling */ 1462 return; 1463 default: 1464 break; 1465 } 1466 xpt_release_ccb(done_ccb); 1467 } 1468 1469 static int 1470 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 1471 { 1472 1473 return(cam_periph_error(ccb, cam_flags, sense_flags, NULL)); 1474 } 1475 1476 static void 1477 adagetparams(struct cam_periph *periph, struct ccb_getdev *cgd) 1478 { 1479 struct ada_softc *softc = (struct ada_softc *)periph->softc; 1480 struct disk_params *dp = &softc->params; 1481 u_int64_t lbasize48; 1482 u_int32_t lbasize; 1483 1484 dp->secsize = ata_logical_sector_size(&cgd->ident_data); 1485 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) && 1486 cgd->ident_data.current_heads && cgd->ident_data.current_sectors) { 1487 dp->heads = cgd->ident_data.current_heads; 1488 dp->secs_per_track = cgd->ident_data.current_sectors; 1489 dp->cylinders = cgd->ident_data.cylinders; 1490 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 | 1491 ((u_int32_t)cgd->ident_data.current_size_2 << 16); 1492 } else { 1493 dp->heads = cgd->ident_data.heads; 1494 dp->secs_per_track = cgd->ident_data.sectors; 1495 dp->cylinders = cgd->ident_data.cylinders; 1496 dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track; 1497 } 1498 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 | 1499 ((u_int32_t)cgd->ident_data.lba_size_2 << 16); 1500 1501 /* use the 28bit LBA size if valid or bigger than the CHS mapping */ 1502 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize) 1503 dp->sectors = lbasize; 1504 1505 /* use the 48bit LBA size if valid */ 1506 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) | 1507 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) | 1508 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) | 1509 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48); 1510 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) && 1511 lbasize48 > ATA_MAX_28BIT_LBA) 1512 dp->sectors = lbasize48; 1513 } 1514 1515 static void 1516 adasendorderedtag(void *arg) 1517 { 1518 struct ada_softc *softc = arg; 1519 1520 if (ada_send_ordered) { 1521 if ((softc->ordered_tag_count == 0) 1522 && ((softc->flags & ADA_FLAG_WENT_IDLE) == 0)) { 1523 softc->flags |= ADA_FLAG_NEED_OTAG; 1524 } 1525 if (softc->outstanding_cmds > 0) 1526 softc->flags &= ~ADA_FLAG_WENT_IDLE; 1527 1528 softc->ordered_tag_count = 0; 1529 } 1530 /* Queue us up again */ 1531 callout_reset(&softc->sendordered_c, 1532 (ADA_DEFAULT_TIMEOUT * hz) / ADA_ORDEREDTAG_INTERVAL, 1533 adasendorderedtag, softc); 1534 } 1535 1536 /* 1537 * Step through all ADA peripheral drivers, and if the device is still open, 1538 * sync the disk cache to physical media. 1539 */ 1540 static void 1541 adaflush(void) 1542 { 1543 struct cam_periph *periph; 1544 struct ada_softc *softc; 1545 1546 TAILQ_FOREACH(periph, &adadriver.units, unit_links) { 1547 union ccb ccb; 1548 1549 /* If we paniced with lock held - not recurse here. */ 1550 if (cam_periph_owned(periph)) 1551 continue; 1552 cam_periph_lock(periph); 1553 softc = (struct ada_softc *)periph->softc; 1554 /* 1555 * We only sync the cache if the drive is still open, and 1556 * if the drive is capable of it.. 1557 */ 1558 if (((softc->flags & ADA_FLAG_OPEN) == 0) || 1559 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) { 1560 cam_periph_unlock(periph); 1561 continue; 1562 } 1563 1564 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1565 1566 ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 1567 cam_fill_ataio(&ccb.ataio, 1568 1, 1569 adadone, 1570 CAM_DIR_NONE, 1571 0, 1572 NULL, 1573 0, 1574 ada_default_timeout*1000); 1575 1576 if (softc->flags & ADA_FLAG_CAN_48BIT) 1577 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1578 else 1579 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0); 1580 xpt_polled_action(&ccb); 1581 1582 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 1583 xpt_print(periph->path, "Synchronize cache failed\n"); 1584 1585 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 1586 cam_release_devq(ccb.ccb_h.path, 1587 /*relsim_flags*/0, 1588 /*reduction*/0, 1589 /*timeout*/0, 1590 /*getcount_only*/0); 1591 cam_periph_unlock(periph); 1592 } 1593 } 1594 1595 static void 1596 adaspindown(uint8_t cmd, int flags) 1597 { 1598 struct cam_periph *periph; 1599 struct ada_softc *softc; 1600 1601 TAILQ_FOREACH(periph, &adadriver.units, unit_links) { 1602 union ccb ccb; 1603 1604 /* If we paniced with lock held - not recurse here. */ 1605 if (cam_periph_owned(periph)) 1606 continue; 1607 cam_periph_lock(periph); 1608 softc = (struct ada_softc *)periph->softc; 1609 /* 1610 * We only spin-down the drive if it is capable of it.. 1611 */ 1612 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 1613 cam_periph_unlock(periph); 1614 continue; 1615 } 1616 1617 if (bootverbose) 1618 xpt_print(periph->path, "spin-down\n"); 1619 1620 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1621 1622 ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 1623 cam_fill_ataio(&ccb.ataio, 1624 1, 1625 adadone, 1626 CAM_DIR_NONE | flags, 1627 0, 1628 NULL, 1629 0, 1630 ada_default_timeout*1000); 1631 1632 ata_28bit_cmd(&ccb.ataio, cmd, 0, 0, 0); 1633 xpt_polled_action(&ccb); 1634 1635 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 1636 xpt_print(periph->path, "Spin-down disk failed\n"); 1637 1638 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 1639 cam_release_devq(ccb.ccb_h.path, 1640 /*relsim_flags*/0, 1641 /*reduction*/0, 1642 /*timeout*/0, 1643 /*getcount_only*/0); 1644 cam_periph_unlock(periph); 1645 } 1646 } 1647 1648 static void 1649 adashutdown(void *arg, int howto) 1650 { 1651 1652 adaflush(); 1653 if (ada_spindown_shutdown != 0 && 1654 (howto & (RB_HALT | RB_POWEROFF)) != 0) 1655 adaspindown(ATA_STANDBY_IMMEDIATE, 0); 1656 } 1657 1658 static void 1659 adasuspend(void *arg) 1660 { 1661 1662 adaflush(); 1663 if (ada_spindown_suspend != 0) 1664 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE); 1665 } 1666 1667 static void 1668 adaresume(void *arg) 1669 { 1670 struct cam_periph *periph; 1671 struct ada_softc *softc; 1672 1673 if (ada_spindown_suspend == 0) 1674 return; 1675 1676 TAILQ_FOREACH(periph, &adadriver.units, unit_links) { 1677 cam_periph_lock(periph); 1678 softc = (struct ada_softc *)periph->softc; 1679 /* 1680 * We only spin-down the drive if it is capable of it.. 1681 */ 1682 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 1683 cam_periph_unlock(periph); 1684 continue; 1685 } 1686 1687 if (bootverbose) 1688 xpt_print(periph->path, "resume\n"); 1689 1690 /* 1691 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on 1692 * sleep request. 1693 */ 1694 cam_release_devq(periph->path, 1695 /*relsim_flags*/0, 1696 /*openings*/0, 1697 /*timeout*/0, 1698 /*getcount_only*/0); 1699 1700 cam_periph_unlock(periph); 1701 } 1702 } 1703 1704 #endif /* _KERNEL */ 1705