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