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