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 cam_status 816 adaregister(struct cam_periph *periph, void *arg) 817 { 818 struct ada_softc *softc; 819 struct ccb_pathinq cpi; 820 struct ccb_getdev *cgd; 821 char announce_buf[80], buf1[32]; 822 struct disk_params *dp; 823 caddr_t match; 824 u_int maxio; 825 int legacy_id, quirks; 826 827 cgd = (struct ccb_getdev *)arg; 828 if (periph == NULL) { 829 printf("adaregister: periph was NULL!!\n"); 830 return(CAM_REQ_CMP_ERR); 831 } 832 833 if (cgd == NULL) { 834 printf("adaregister: no getdev CCB, can't register device\n"); 835 return(CAM_REQ_CMP_ERR); 836 } 837 838 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF, 839 M_NOWAIT|M_ZERO); 840 841 if (softc == NULL) { 842 printf("adaregister: Unable to probe new device. " 843 "Unable to allocate softc\n"); 844 return(CAM_REQ_CMP_ERR); 845 } 846 847 bioq_init(&softc->bio_queue); 848 bioq_init(&softc->trim_queue); 849 850 if (cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA && 851 (cgd->inq_flags & SID_DMA)) 852 softc->flags |= ADA_FLAG_CAN_DMA; 853 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) 854 softc->flags |= ADA_FLAG_CAN_48BIT; 855 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE) 856 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE; 857 if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT) 858 softc->flags |= ADA_FLAG_CAN_POWERMGT; 859 if (cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ && 860 (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue)) 861 softc->flags |= ADA_FLAG_CAN_NCQ; 862 if (cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) { 863 softc->flags |= ADA_FLAG_CAN_TRIM; 864 softc->trim_max_ranges = TRIM_MAX_RANGES; 865 if (cgd->ident_data.max_dsm_blocks != 0) { 866 softc->trim_max_ranges = 867 min(cgd->ident_data.max_dsm_blocks * 64, 868 softc->trim_max_ranges); 869 } 870 } 871 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA) 872 softc->flags |= ADA_FLAG_CAN_CFA; 873 874 periph->softc = softc; 875 876 /* 877 * See if this device has any quirks. 878 */ 879 match = cam_quirkmatch((caddr_t)&cgd->ident_data, 880 (caddr_t)ada_quirk_table, 881 sizeof(ada_quirk_table)/sizeof(*ada_quirk_table), 882 sizeof(*ada_quirk_table), ata_identify_match); 883 if (match != NULL) 884 softc->quirks = ((struct ada_quirk_entry *)match)->quirks; 885 else 886 softc->quirks = ADA_Q_NONE; 887 888 bzero(&cpi, sizeof(cpi)); 889 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE); 890 cpi.ccb_h.func_code = XPT_PATH_INQ; 891 xpt_action((union ccb *)&cpi); 892 893 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph); 894 895 /* 896 * Register this media as a disk 897 */ 898 (void)cam_periph_hold(periph, PRIBIO); 899 mtx_unlock(periph->sim->mtx); 900 snprintf(announce_buf, sizeof(announce_buf), 901 "kern.cam.ada.%d.quirks", periph->unit_number); 902 quirks = softc->quirks; 903 TUNABLE_INT_FETCH(announce_buf, &quirks); 904 softc->quirks = quirks; 905 softc->write_cache = -1; 906 snprintf(announce_buf, sizeof(announce_buf), 907 "kern.cam.ada.%d.write_cache", periph->unit_number); 908 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache); 909 adagetparams(periph, cgd); 910 softc->disk = disk_alloc(); 911 softc->disk->d_devstat = devstat_new_entry(periph->periph_name, 912 periph->unit_number, softc->params.secsize, 913 DEVSTAT_ALL_SUPPORTED, 914 DEVSTAT_TYPE_DIRECT | 915 XPORT_DEVSTAT_TYPE(cpi.transport), 916 DEVSTAT_PRIORITY_DISK); 917 softc->disk->d_open = adaopen; 918 softc->disk->d_close = adaclose; 919 softc->disk->d_strategy = adastrategy; 920 softc->disk->d_dump = adadump; 921 softc->disk->d_name = "ada"; 922 softc->disk->d_drv1 = periph; 923 maxio = cpi.maxio; /* Honor max I/O size of SIM */ 924 if (maxio == 0) 925 maxio = DFLTPHYS; /* traditional default */ 926 else if (maxio > MAXPHYS) 927 maxio = MAXPHYS; /* for safety */ 928 if (softc->flags & ADA_FLAG_CAN_48BIT) 929 maxio = min(maxio, 65536 * softc->params.secsize); 930 else /* 28bit ATA command limit */ 931 maxio = min(maxio, 256 * softc->params.secsize); 932 softc->disk->d_maxsize = maxio; 933 softc->disk->d_unit = periph->unit_number; 934 softc->disk->d_flags = 0; 935 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) 936 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE; 937 if ((softc->flags & ADA_FLAG_CAN_TRIM) || 938 ((softc->flags & ADA_FLAG_CAN_CFA) && 939 !(softc->flags & ADA_FLAG_CAN_48BIT))) 940 softc->disk->d_flags |= DISKFLAG_CANDELETE; 941 strlcpy(softc->disk->d_ident, cgd->serial_num, 942 MIN(sizeof(softc->disk->d_ident), cgd->serial_num_len + 1)); 943 strlcpy(softc->disk->d_descr, cgd->ident_data.model, 944 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model))); 945 softc->disk->d_hba_vendor = cpi.hba_vendor; 946 softc->disk->d_hba_device = cpi.hba_device; 947 softc->disk->d_hba_subvendor = cpi.hba_subvendor; 948 softc->disk->d_hba_subdevice = cpi.hba_subdevice; 949 950 softc->disk->d_sectorsize = softc->params.secsize; 951 softc->disk->d_mediasize = (off_t)softc->params.sectors * 952 softc->params.secsize; 953 if (ata_physical_sector_size(&cgd->ident_data) != 954 softc->params.secsize) { 955 softc->disk->d_stripesize = 956 ata_physical_sector_size(&cgd->ident_data); 957 softc->disk->d_stripeoffset = (softc->disk->d_stripesize - 958 ata_logical_sector_offset(&cgd->ident_data)) % 959 softc->disk->d_stripesize; 960 } else if (softc->quirks & ADA_Q_4K) { 961 softc->disk->d_stripesize = 4096; 962 softc->disk->d_stripeoffset = 0; 963 } 964 softc->disk->d_fwsectors = softc->params.secs_per_track; 965 softc->disk->d_fwheads = softc->params.heads; 966 ata_disk_firmware_geom_adjust(softc->disk); 967 968 if (ada_legacy_aliases) { 969 #ifdef ATA_STATIC_ID 970 legacy_id = xpt_path_legacy_ata_id(periph->path); 971 #else 972 legacy_id = softc->disk->d_unit; 973 #endif 974 if (legacy_id >= 0) { 975 snprintf(announce_buf, sizeof(announce_buf), 976 "kern.devalias.%s%d", 977 softc->disk->d_name, softc->disk->d_unit); 978 snprintf(buf1, sizeof(buf1), 979 "ad%d", legacy_id); 980 setenv(announce_buf, buf1); 981 } 982 } else 983 legacy_id = -1; 984 disk_create(softc->disk, DISK_VERSION); 985 mtx_lock(periph->sim->mtx); 986 cam_periph_unhold(periph); 987 988 dp = &softc->params; 989 snprintf(announce_buf, sizeof(announce_buf), 990 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)", 991 (uintmax_t)(((uintmax_t)dp->secsize * 992 dp->sectors) / (1024*1024)), 993 (uintmax_t)dp->sectors, 994 dp->secsize, dp->heads, 995 dp->secs_per_track, dp->cylinders); 996 xpt_announce_periph(periph, announce_buf); 997 if (legacy_id >= 0) 998 printf("%s%d: Previously was known as ad%d\n", 999 periph->periph_name, periph->unit_number, legacy_id); 1000 1001 /* 1002 * Create our sysctl variables, now that we know 1003 * we have successfully attached. 1004 */ 1005 cam_periph_acquire(periph); 1006 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task); 1007 1008 /* 1009 * Add async callbacks for bus reset and 1010 * bus device reset calls. I don't bother 1011 * checking if this fails as, in most cases, 1012 * the system will function just fine without 1013 * them and the only alternative would be to 1014 * not attach the device on failure. 1015 */ 1016 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE, 1017 adaasync, periph, periph->path); 1018 1019 /* 1020 * Schedule a periodic event to occasionally send an 1021 * ordered tag to a device. 1022 */ 1023 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0); 1024 callout_reset(&softc->sendordered_c, 1025 (ADA_DEFAULT_TIMEOUT * hz) / ADA_ORDEREDTAG_INTERVAL, 1026 adasendorderedtag, softc); 1027 1028 if ((ada_write_cache >= 0 || softc->write_cache >= 0) && 1029 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) { 1030 softc->state = ADA_STATE_WCACHE; 1031 cam_periph_acquire(periph); 1032 cam_freeze_devq_arg(periph->path, 1033 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1); 1034 xpt_schedule(periph, CAM_PRIORITY_DEV); 1035 } else 1036 softc->state = ADA_STATE_NORMAL; 1037 1038 return(CAM_REQ_CMP); 1039 } 1040 1041 static void 1042 adastart(struct cam_periph *periph, union ccb *start_ccb) 1043 { 1044 struct ada_softc *softc = (struct ada_softc *)periph->softc; 1045 struct ccb_ataio *ataio = &start_ccb->ataio; 1046 1047 switch (softc->state) { 1048 case ADA_STATE_NORMAL: 1049 { 1050 struct bio *bp; 1051 u_int8_t tag_code; 1052 1053 /* Execute immediate CCB if waiting. */ 1054 if (periph->immediate_priority <= periph->pinfo.priority) { 1055 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE, 1056 ("queuing for immediate ccb\n")); 1057 start_ccb->ccb_h.ccb_state = ADA_CCB_WAITING; 1058 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 1059 periph_links.sle); 1060 periph->immediate_priority = CAM_PRIORITY_NONE; 1061 wakeup(&periph->ccb_list); 1062 /* Have more work to do, so ensure we stay scheduled */ 1063 adaschedule(periph); 1064 break; 1065 } 1066 /* Run TRIM if not running yet. */ 1067 if (!softc->trim_running && 1068 (bp = bioq_first(&softc->trim_queue)) != 0) { 1069 struct trim_request *req = &softc->trim_req; 1070 struct bio *bp1; 1071 uint64_t lastlba = (uint64_t)-1; 1072 int bps = 0, c, lastcount = 0, off, ranges = 0; 1073 1074 softc->trim_running = 1; 1075 bzero(req, sizeof(*req)); 1076 bp1 = bp; 1077 do { 1078 uint64_t lba = bp1->bio_pblkno; 1079 int count = bp1->bio_bcount / 1080 softc->params.secsize; 1081 1082 bioq_remove(&softc->trim_queue, bp1); 1083 1084 /* Try to extend the previous range. */ 1085 if (lba == lastlba) { 1086 c = min(count, 0xffff - lastcount); 1087 lastcount += c; 1088 off = (ranges - 1) * 8; 1089 req->data[off + 6] = lastcount & 0xff; 1090 req->data[off + 7] = 1091 (lastcount >> 8) & 0xff; 1092 count -= c; 1093 lba += c; 1094 } 1095 1096 while (count > 0) { 1097 c = min(count, 0xffff); 1098 off = ranges * 8; 1099 req->data[off + 0] = lba & 0xff; 1100 req->data[off + 1] = (lba >> 8) & 0xff; 1101 req->data[off + 2] = (lba >> 16) & 0xff; 1102 req->data[off + 3] = (lba >> 24) & 0xff; 1103 req->data[off + 4] = (lba >> 32) & 0xff; 1104 req->data[off + 5] = (lba >> 40) & 0xff; 1105 req->data[off + 6] = c & 0xff; 1106 req->data[off + 7] = (c >> 8) & 0xff; 1107 lba += c; 1108 count -= c; 1109 lastcount = c; 1110 ranges++; 1111 } 1112 lastlba = lba; 1113 req->bps[bps++] = bp1; 1114 bp1 = bioq_first(&softc->trim_queue); 1115 if (bps >= TRIM_MAX_BIOS || 1116 bp1 == NULL || 1117 bp1->bio_bcount / softc->params.secsize > 1118 (softc->trim_max_ranges - ranges) * 0xffff) 1119 break; 1120 } while (1); 1121 cam_fill_ataio(ataio, 1122 ada_retry_count, 1123 adadone, 1124 CAM_DIR_OUT, 1125 0, 1126 req->data, 1127 ((ranges + 63) / 64) * 512, 1128 ada_default_timeout * 1000); 1129 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT, 1130 ATA_DSM_TRIM, 0, (ranges + 63) / 64); 1131 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM; 1132 goto out; 1133 } 1134 /* Run regular command. */ 1135 bp = bioq_first(&softc->bio_queue); 1136 if (bp == NULL) { 1137 xpt_release_ccb(start_ccb); 1138 break; 1139 } 1140 bioq_remove(&softc->bio_queue, bp); 1141 1142 if ((bp->bio_flags & BIO_ORDERED) != 0 1143 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) { 1144 softc->flags &= ~ADA_FLAG_NEED_OTAG; 1145 softc->ordered_tag_count++; 1146 tag_code = 0; 1147 } else { 1148 tag_code = 1; 1149 } 1150 switch (bp->bio_cmd) { 1151 case BIO_READ: 1152 case BIO_WRITE: 1153 { 1154 uint64_t lba = bp->bio_pblkno; 1155 uint16_t count = bp->bio_bcount / softc->params.secsize; 1156 #ifdef ADA_TEST_FAILURE 1157 int fail = 0; 1158 1159 /* 1160 * Support the failure ioctls. If the command is a 1161 * read, and there are pending forced read errors, or 1162 * if a write and pending write errors, then fail this 1163 * operation with EIO. This is useful for testing 1164 * purposes. Also, support having every Nth read fail. 1165 * 1166 * This is a rather blunt tool. 1167 */ 1168 if (bp->bio_cmd == BIO_READ) { 1169 if (softc->force_read_error) { 1170 softc->force_read_error--; 1171 fail = 1; 1172 } 1173 if (softc->periodic_read_error > 0) { 1174 if (++softc->periodic_read_count >= 1175 softc->periodic_read_error) { 1176 softc->periodic_read_count = 0; 1177 fail = 1; 1178 } 1179 } 1180 } else { 1181 if (softc->force_write_error) { 1182 softc->force_write_error--; 1183 fail = 1; 1184 } 1185 } 1186 if (fail) { 1187 bp->bio_error = EIO; 1188 bp->bio_flags |= BIO_ERROR; 1189 biodone(bp); 1190 xpt_release_ccb(start_ccb); 1191 adaschedule(periph); 1192 return; 1193 } 1194 #endif 1195 cam_fill_ataio(ataio, 1196 ada_retry_count, 1197 adadone, 1198 bp->bio_cmd == BIO_READ ? 1199 CAM_DIR_IN : CAM_DIR_OUT, 1200 tag_code, 1201 bp->bio_data, 1202 bp->bio_bcount, 1203 ada_default_timeout*1000); 1204 1205 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) { 1206 if (bp->bio_cmd == BIO_READ) { 1207 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED, 1208 lba, count); 1209 } else { 1210 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED, 1211 lba, count); 1212 } 1213 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) && 1214 (lba + count >= ATA_MAX_28BIT_LBA || 1215 count > 256)) { 1216 if (softc->flags & ADA_FLAG_CAN_DMA) { 1217 if (bp->bio_cmd == BIO_READ) { 1218 ata_48bit_cmd(ataio, ATA_READ_DMA48, 1219 0, lba, count); 1220 } else { 1221 ata_48bit_cmd(ataio, ATA_WRITE_DMA48, 1222 0, lba, count); 1223 } 1224 } else { 1225 if (bp->bio_cmd == BIO_READ) { 1226 ata_48bit_cmd(ataio, ATA_READ_MUL48, 1227 0, lba, count); 1228 } else { 1229 ata_48bit_cmd(ataio, ATA_WRITE_MUL48, 1230 0, lba, count); 1231 } 1232 } 1233 } else { 1234 if (count == 256) 1235 count = 0; 1236 if (softc->flags & ADA_FLAG_CAN_DMA) { 1237 if (bp->bio_cmd == BIO_READ) { 1238 ata_28bit_cmd(ataio, ATA_READ_DMA, 1239 0, lba, count); 1240 } else { 1241 ata_28bit_cmd(ataio, ATA_WRITE_DMA, 1242 0, lba, count); 1243 } 1244 } else { 1245 if (bp->bio_cmd == BIO_READ) { 1246 ata_28bit_cmd(ataio, ATA_READ_MUL, 1247 0, lba, count); 1248 } else { 1249 ata_28bit_cmd(ataio, ATA_WRITE_MUL, 1250 0, lba, count); 1251 } 1252 } 1253 } 1254 break; 1255 } 1256 case BIO_DELETE: 1257 { 1258 uint64_t lba = bp->bio_pblkno; 1259 uint16_t count = bp->bio_bcount / softc->params.secsize; 1260 1261 cam_fill_ataio(ataio, 1262 ada_retry_count, 1263 adadone, 1264 CAM_DIR_NONE, 1265 0, 1266 NULL, 1267 0, 1268 ada_default_timeout*1000); 1269 1270 if (count >= 256) 1271 count = 0; 1272 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count); 1273 break; 1274 } 1275 case BIO_FLUSH: 1276 cam_fill_ataio(ataio, 1277 1, 1278 adadone, 1279 CAM_DIR_NONE, 1280 0, 1281 NULL, 1282 0, 1283 ada_default_timeout*1000); 1284 1285 if (softc->flags & ADA_FLAG_CAN_48BIT) 1286 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1287 else 1288 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0); 1289 break; 1290 } 1291 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO; 1292 out: 1293 start_ccb->ccb_h.ccb_bp = bp; 1294 softc->outstanding_cmds++; 1295 xpt_action(start_ccb); 1296 1297 /* May have more work to do, so ensure we stay scheduled */ 1298 adaschedule(periph); 1299 break; 1300 } 1301 case ADA_STATE_WCACHE: 1302 { 1303 cam_fill_ataio(ataio, 1304 1, 1305 adadone, 1306 CAM_DIR_NONE, 1307 0, 1308 NULL, 1309 0, 1310 ada_default_timeout*1000); 1311 1312 ata_28bit_cmd(ataio, ATA_SETFEATURES, (softc->write_cache > 0 || 1313 (softc->write_cache < 0 && ada_write_cache)) ? 1314 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0); 1315 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE; 1316 xpt_action(start_ccb); 1317 break; 1318 } 1319 } 1320 } 1321 1322 static void 1323 adadone(struct cam_periph *periph, union ccb *done_ccb) 1324 { 1325 struct ada_softc *softc; 1326 struct ccb_ataio *ataio; 1327 1328 softc = (struct ada_softc *)periph->softc; 1329 ataio = &done_ccb->ataio; 1330 switch (ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) { 1331 case ADA_CCB_BUFFER_IO: 1332 case ADA_CCB_TRIM: 1333 { 1334 struct bio *bp; 1335 1336 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 1337 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1338 int error; 1339 1340 error = adaerror(done_ccb, 0, 0); 1341 if (error == ERESTART) { 1342 /* A retry was scheduled, so just return. */ 1343 return; 1344 } 1345 if (error != 0) { 1346 if (error == ENXIO) { 1347 /* 1348 * Catastrophic error. Mark our pack as 1349 * invalid. 1350 */ 1351 /* 1352 * XXX See if this is really a media 1353 * XXX change first? 1354 */ 1355 xpt_print(periph->path, 1356 "Invalidating pack\n"); 1357 softc->flags |= ADA_FLAG_PACK_INVALID; 1358 } 1359 bp->bio_error = error; 1360 bp->bio_resid = bp->bio_bcount; 1361 bp->bio_flags |= BIO_ERROR; 1362 } else { 1363 bp->bio_resid = ataio->resid; 1364 bp->bio_error = 0; 1365 if (bp->bio_resid != 0) 1366 bp->bio_flags |= BIO_ERROR; 1367 } 1368 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1369 cam_release_devq(done_ccb->ccb_h.path, 1370 /*relsim_flags*/0, 1371 /*reduction*/0, 1372 /*timeout*/0, 1373 /*getcount_only*/0); 1374 } else { 1375 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1376 panic("REQ_CMP with QFRZN"); 1377 bp->bio_resid = ataio->resid; 1378 if (ataio->resid > 0) 1379 bp->bio_flags |= BIO_ERROR; 1380 } 1381 softc->outstanding_cmds--; 1382 if (softc->outstanding_cmds == 0) 1383 softc->flags |= ADA_FLAG_WENT_IDLE; 1384 if ((ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) == 1385 ADA_CCB_TRIM) { 1386 struct trim_request *req = 1387 (struct trim_request *)ataio->data_ptr; 1388 int i; 1389 1390 for (i = 1; i < TRIM_MAX_BIOS && req->bps[i]; i++) { 1391 struct bio *bp1 = req->bps[i]; 1392 1393 bp1->bio_resid = bp->bio_resid; 1394 bp1->bio_error = bp->bio_error; 1395 if (bp->bio_flags & BIO_ERROR) 1396 bp1->bio_flags |= BIO_ERROR; 1397 biodone(bp1); 1398 } 1399 softc->trim_running = 0; 1400 biodone(bp); 1401 adaschedule(periph); 1402 } else 1403 biodone(bp); 1404 break; 1405 } 1406 case ADA_CCB_WCACHE: 1407 { 1408 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1409 if (adaerror(done_ccb, 0, 0) == ERESTART) { 1410 return; 1411 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1412 cam_release_devq(done_ccb->ccb_h.path, 1413 /*relsim_flags*/0, 1414 /*reduction*/0, 1415 /*timeout*/0, 1416 /*getcount_only*/0); 1417 } 1418 } 1419 1420 softc->state = ADA_STATE_NORMAL; 1421 /* 1422 * Since our peripheral may be invalidated by an error 1423 * above or an external event, we must release our CCB 1424 * before releasing the reference on the peripheral. 1425 * The peripheral will only go away once the last reference 1426 * is removed, and we need it around for the CCB release 1427 * operation. 1428 */ 1429 xpt_release_ccb(done_ccb); 1430 cam_release_devq(periph->path, 1431 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE); 1432 adaschedule(periph); 1433 cam_periph_release_locked(periph); 1434 return; 1435 } 1436 case ADA_CCB_WAITING: 1437 { 1438 /* Caller will release the CCB */ 1439 wakeup(&done_ccb->ccb_h.cbfcnp); 1440 return; 1441 } 1442 case ADA_CCB_DUMP: 1443 /* No-op. We're polling */ 1444 return; 1445 default: 1446 break; 1447 } 1448 xpt_release_ccb(done_ccb); 1449 } 1450 1451 static int 1452 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 1453 { 1454 1455 return(cam_periph_error(ccb, cam_flags, sense_flags, NULL)); 1456 } 1457 1458 static void 1459 adagetparams(struct cam_periph *periph, struct ccb_getdev *cgd) 1460 { 1461 struct ada_softc *softc = (struct ada_softc *)periph->softc; 1462 struct disk_params *dp = &softc->params; 1463 u_int64_t lbasize48; 1464 u_int32_t lbasize; 1465 1466 dp->secsize = ata_logical_sector_size(&cgd->ident_data); 1467 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) && 1468 cgd->ident_data.current_heads && cgd->ident_data.current_sectors) { 1469 dp->heads = cgd->ident_data.current_heads; 1470 dp->secs_per_track = cgd->ident_data.current_sectors; 1471 dp->cylinders = cgd->ident_data.cylinders; 1472 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 | 1473 ((u_int32_t)cgd->ident_data.current_size_2 << 16); 1474 } else { 1475 dp->heads = cgd->ident_data.heads; 1476 dp->secs_per_track = cgd->ident_data.sectors; 1477 dp->cylinders = cgd->ident_data.cylinders; 1478 dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track; 1479 } 1480 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 | 1481 ((u_int32_t)cgd->ident_data.lba_size_2 << 16); 1482 1483 /* use the 28bit LBA size if valid or bigger than the CHS mapping */ 1484 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize) 1485 dp->sectors = lbasize; 1486 1487 /* use the 48bit LBA size if valid */ 1488 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) | 1489 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) | 1490 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) | 1491 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48); 1492 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) && 1493 lbasize48 > ATA_MAX_28BIT_LBA) 1494 dp->sectors = lbasize48; 1495 } 1496 1497 static void 1498 adasendorderedtag(void *arg) 1499 { 1500 struct ada_softc *softc = arg; 1501 1502 if (ada_send_ordered) { 1503 if ((softc->ordered_tag_count == 0) 1504 && ((softc->flags & ADA_FLAG_WENT_IDLE) == 0)) { 1505 softc->flags |= ADA_FLAG_NEED_OTAG; 1506 } 1507 if (softc->outstanding_cmds > 0) 1508 softc->flags &= ~ADA_FLAG_WENT_IDLE; 1509 1510 softc->ordered_tag_count = 0; 1511 } 1512 /* Queue us up again */ 1513 callout_reset(&softc->sendordered_c, 1514 (ADA_DEFAULT_TIMEOUT * hz) / ADA_ORDEREDTAG_INTERVAL, 1515 adasendorderedtag, softc); 1516 } 1517 1518 /* 1519 * Step through all ADA peripheral drivers, and if the device is still open, 1520 * sync the disk cache to physical media. 1521 */ 1522 static void 1523 adaflush(void) 1524 { 1525 struct cam_periph *periph; 1526 struct ada_softc *softc; 1527 1528 TAILQ_FOREACH(periph, &adadriver.units, unit_links) { 1529 union ccb ccb; 1530 1531 /* If we paniced with lock held - not recurse here. */ 1532 if (cam_periph_owned(periph)) 1533 continue; 1534 cam_periph_lock(periph); 1535 softc = (struct ada_softc *)periph->softc; 1536 /* 1537 * We only sync the cache if the drive is still open, and 1538 * if the drive is capable of it.. 1539 */ 1540 if (((softc->flags & ADA_FLAG_OPEN) == 0) || 1541 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) { 1542 cam_periph_unlock(periph); 1543 continue; 1544 } 1545 1546 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1547 1548 ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 1549 cam_fill_ataio(&ccb.ataio, 1550 1, 1551 adadone, 1552 CAM_DIR_NONE, 1553 0, 1554 NULL, 1555 0, 1556 ada_default_timeout*1000); 1557 1558 if (softc->flags & ADA_FLAG_CAN_48BIT) 1559 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1560 else 1561 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0); 1562 xpt_polled_action(&ccb); 1563 1564 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 1565 xpt_print(periph->path, "Synchronize cache failed\n"); 1566 1567 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 1568 cam_release_devq(ccb.ccb_h.path, 1569 /*relsim_flags*/0, 1570 /*reduction*/0, 1571 /*timeout*/0, 1572 /*getcount_only*/0); 1573 cam_periph_unlock(periph); 1574 } 1575 } 1576 1577 static void 1578 adaspindown(uint8_t cmd, int flags) 1579 { 1580 struct cam_periph *periph; 1581 struct ada_softc *softc; 1582 1583 TAILQ_FOREACH(periph, &adadriver.units, unit_links) { 1584 union ccb ccb; 1585 1586 /* If we paniced with lock held - not recurse here. */ 1587 if (cam_periph_owned(periph)) 1588 continue; 1589 cam_periph_lock(periph); 1590 softc = (struct ada_softc *)periph->softc; 1591 /* 1592 * We only spin-down the drive if it is capable of it.. 1593 */ 1594 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 1595 cam_periph_unlock(periph); 1596 continue; 1597 } 1598 1599 if (bootverbose) 1600 xpt_print(periph->path, "spin-down\n"); 1601 1602 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1603 1604 ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 1605 cam_fill_ataio(&ccb.ataio, 1606 1, 1607 adadone, 1608 CAM_DIR_NONE | flags, 1609 0, 1610 NULL, 1611 0, 1612 ada_default_timeout*1000); 1613 1614 ata_28bit_cmd(&ccb.ataio, cmd, 0, 0, 0); 1615 xpt_polled_action(&ccb); 1616 1617 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 1618 xpt_print(periph->path, "Spin-down disk failed\n"); 1619 1620 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 1621 cam_release_devq(ccb.ccb_h.path, 1622 /*relsim_flags*/0, 1623 /*reduction*/0, 1624 /*timeout*/0, 1625 /*getcount_only*/0); 1626 cam_periph_unlock(periph); 1627 } 1628 } 1629 1630 static void 1631 adashutdown(void *arg, int howto) 1632 { 1633 1634 adaflush(); 1635 if (ada_spindown_shutdown != 0 && 1636 (howto & (RB_HALT | RB_POWEROFF)) != 0) 1637 adaspindown(ATA_STANDBY_IMMEDIATE, 0); 1638 } 1639 1640 static void 1641 adasuspend(void *arg) 1642 { 1643 1644 adaflush(); 1645 if (ada_spindown_suspend != 0) 1646 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE); 1647 } 1648 1649 static void 1650 adaresume(void *arg) 1651 { 1652 struct cam_periph *periph; 1653 struct ada_softc *softc; 1654 1655 if (ada_spindown_suspend == 0) 1656 return; 1657 1658 TAILQ_FOREACH(periph, &adadriver.units, unit_links) { 1659 cam_periph_lock(periph); 1660 softc = (struct ada_softc *)periph->softc; 1661 /* 1662 * We only spin-down the drive if it is capable of it.. 1663 */ 1664 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 1665 cam_periph_unlock(periph); 1666 continue; 1667 } 1668 1669 if (bootverbose) 1670 xpt_print(periph->path, "resume\n"); 1671 1672 /* 1673 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on 1674 * sleep request. 1675 */ 1676 cam_release_devq(periph->path, 1677 /*relsim_flags*/0, 1678 /*openings*/0, 1679 /*timeout*/0, 1680 /*getcount_only*/0); 1681 1682 cam_periph_unlock(periph); 1683 } 1684 } 1685 1686 #endif /* _KERNEL */ 1687