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