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 843T Series SSDs 470 * 4k optimised 471 */ 472 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7WD*", "*" }, 473 /*quirks*/ADA_Q_4K 474 }, 475 { 476 /* 477 * Samsung 850 SSDs 478 * 4k optimised 479 */ 480 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 850*", "*" }, 481 /*quirks*/ADA_Q_4K 482 }, 483 { 484 /* 485 * Samsung PM853T Series SSDs 486 * 4k optimised 487 */ 488 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7GE*", "*" }, 489 /*quirks*/ADA_Q_4K 490 }, 491 { 492 /* 493 * SuperTalent TeraDrive CT SSDs 494 * 4k optimised & trim only works in 4k requests + 4k aligned 495 */ 496 { T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" }, 497 /*quirks*/ADA_Q_4K 498 }, 499 { 500 /* 501 * XceedIOPS SATA SSDs 502 * 4k optimised 503 */ 504 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" }, 505 /*quirks*/ADA_Q_4K 506 }, 507 { 508 /* Default */ 509 { 510 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, 511 /*vendor*/"*", /*product*/"*", /*revision*/"*" 512 }, 513 /*quirks*/0 514 }, 515 }; 516 517 static disk_strategy_t adastrategy; 518 static dumper_t adadump; 519 static periph_init_t adainit; 520 static void adaasync(void *callback_arg, u_int32_t code, 521 struct cam_path *path, void *arg); 522 static void adasysctlinit(void *context, int pending); 523 static periph_ctor_t adaregister; 524 static periph_dtor_t adacleanup; 525 static periph_start_t adastart; 526 static periph_oninv_t adaoninvalidate; 527 static void adadone(struct cam_periph *periph, 528 union ccb *done_ccb); 529 static int adaerror(union ccb *ccb, u_int32_t cam_flags, 530 u_int32_t sense_flags); 531 static void adagetparams(struct cam_periph *periph, 532 struct ccb_getdev *cgd); 533 static timeout_t adasendorderedtag; 534 static void adashutdown(void *arg, int howto); 535 static void adasuspend(void *arg); 536 static void adaresume(void *arg); 537 538 #ifndef ADA_DEFAULT_LEGACY_ALIASES 539 #define ADA_DEFAULT_LEGACY_ALIASES 1 540 #endif 541 542 #ifndef ADA_DEFAULT_TIMEOUT 543 #define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */ 544 #endif 545 546 #ifndef ADA_DEFAULT_RETRY 547 #define ADA_DEFAULT_RETRY 4 548 #endif 549 550 #ifndef ADA_DEFAULT_SEND_ORDERED 551 #define ADA_DEFAULT_SEND_ORDERED 1 552 #endif 553 554 #ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN 555 #define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1 556 #endif 557 558 #ifndef ADA_DEFAULT_SPINDOWN_SUSPEND 559 #define ADA_DEFAULT_SPINDOWN_SUSPEND 1 560 #endif 561 562 #ifndef ADA_DEFAULT_READ_AHEAD 563 #define ADA_DEFAULT_READ_AHEAD 1 564 #endif 565 566 #ifndef ADA_DEFAULT_WRITE_CACHE 567 #define ADA_DEFAULT_WRITE_CACHE 1 568 #endif 569 570 #define ADA_RA (softc->read_ahead >= 0 ? \ 571 softc->read_ahead : ada_read_ahead) 572 #define ADA_WC (softc->write_cache >= 0 ? \ 573 softc->write_cache : ada_write_cache) 574 #define ADA_SIO (softc->sort_io_queue >= 0 ? \ 575 softc->sort_io_queue : cam_sort_io_queues) 576 577 /* 578 * Most platforms map firmware geometry to actual, but some don't. If 579 * not overridden, default to nothing. 580 */ 581 #ifndef ata_disk_firmware_geom_adjust 582 #define ata_disk_firmware_geom_adjust(disk) 583 #endif 584 585 static int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES; 586 static int ada_retry_count = ADA_DEFAULT_RETRY; 587 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT; 588 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED; 589 static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN; 590 static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND; 591 static int ada_read_ahead = ADA_DEFAULT_READ_AHEAD; 592 static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE; 593 594 static SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0, 595 "CAM Direct Access Disk driver"); 596 SYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RWTUN, 597 &ada_legacy_aliases, 0, "Create legacy-like device aliases"); 598 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RWTUN, 599 &ada_retry_count, 0, "Normal I/O retry count"); 600 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RWTUN, 601 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)"); 602 SYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RWTUN, 603 &ada_send_ordered, 0, "Send Ordered Tags"); 604 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RWTUN, 605 &ada_spindown_shutdown, 0, "Spin down upon shutdown"); 606 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RWTUN, 607 &ada_spindown_suspend, 0, "Spin down upon suspend"); 608 SYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RWTUN, 609 &ada_read_ahead, 0, "Enable disk read-ahead"); 610 SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RWTUN, 611 &ada_write_cache, 0, "Enable disk write cache"); 612 613 /* 614 * ADA_ORDEREDTAG_INTERVAL determines how often, relative 615 * to the default timeout, we check to see whether an ordered 616 * tagged transaction is appropriate to prevent simple tag 617 * starvation. Since we'd like to ensure that there is at least 618 * 1/2 of the timeout length left for a starved transaction to 619 * complete after we've sent an ordered tag, we must poll at least 620 * four times in every timeout period. This takes care of the worst 621 * case where a starved transaction starts during an interval that 622 * meets the requirement "don't send an ordered tag" test so it takes 623 * us two intervals to determine that a tag must be sent. 624 */ 625 #ifndef ADA_ORDEREDTAG_INTERVAL 626 #define ADA_ORDEREDTAG_INTERVAL 4 627 #endif 628 629 static struct periph_driver adadriver = 630 { 631 adainit, "ada", 632 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0 633 }; 634 635 PERIPHDRIVER_DECLARE(ada, adadriver); 636 637 static MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers"); 638 639 static int 640 adaopen(struct disk *dp) 641 { 642 struct cam_periph *periph; 643 struct ada_softc *softc; 644 int error; 645 646 periph = (struct cam_periph *)dp->d_drv1; 647 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 648 return(ENXIO); 649 } 650 651 cam_periph_lock(periph); 652 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) { 653 cam_periph_unlock(periph); 654 cam_periph_release(periph); 655 return (error); 656 } 657 658 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 659 ("adaopen\n")); 660 661 softc = (struct ada_softc *)periph->softc; 662 softc->flags |= ADA_FLAG_OPEN; 663 664 cam_periph_unhold(periph); 665 cam_periph_unlock(periph); 666 return (0); 667 } 668 669 static int 670 adaclose(struct disk *dp) 671 { 672 struct cam_periph *periph; 673 struct ada_softc *softc; 674 union ccb *ccb; 675 int error; 676 677 periph = (struct cam_periph *)dp->d_drv1; 678 softc = (struct ada_softc *)periph->softc; 679 cam_periph_lock(periph); 680 681 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 682 ("adaclose\n")); 683 684 /* We only sync the cache if the drive is capable of it. */ 685 if ((softc->flags & ADA_FLAG_DIRTY) != 0 && 686 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 && 687 (periph->flags & CAM_PERIPH_INVALID) == 0 && 688 cam_periph_hold(periph, PRIBIO) == 0) { 689 690 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 691 cam_fill_ataio(&ccb->ataio, 692 1, 693 adadone, 694 CAM_DIR_NONE, 695 0, 696 NULL, 697 0, 698 ada_default_timeout*1000); 699 700 if (softc->flags & ADA_FLAG_CAN_48BIT) 701 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 702 else 703 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 704 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 705 /*sense_flags*/0, softc->disk->d_devstat); 706 707 if (error != 0) 708 xpt_print(periph->path, "Synchronize cache failed\n"); 709 else 710 softc->flags &= ~ADA_FLAG_DIRTY; 711 xpt_release_ccb(ccb); 712 cam_periph_unhold(periph); 713 } 714 715 softc->flags &= ~ADA_FLAG_OPEN; 716 717 while (softc->refcount != 0) 718 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "adaclose", 1); 719 cam_periph_unlock(periph); 720 cam_periph_release(periph); 721 return (0); 722 } 723 724 static void 725 adaschedule(struct cam_periph *periph) 726 { 727 struct ada_softc *softc = (struct ada_softc *)periph->softc; 728 729 if (softc->state != ADA_STATE_NORMAL) 730 return; 731 732 /* Check if we have more work to do. */ 733 if (bioq_first(&softc->bio_queue) || 734 (!softc->trim_running && bioq_first(&softc->trim_queue))) { 735 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 736 } 737 } 738 739 /* 740 * Actually translate the requested transfer into one the physical driver 741 * can understand. The transfer is described by a buf and will include 742 * only one physical transfer. 743 */ 744 static void 745 adastrategy(struct bio *bp) 746 { 747 struct cam_periph *periph; 748 struct ada_softc *softc; 749 750 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 751 softc = (struct ada_softc *)periph->softc; 752 753 cam_periph_lock(periph); 754 755 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp)); 756 757 /* 758 * If the device has been made invalid, error out 759 */ 760 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 761 cam_periph_unlock(periph); 762 biofinish(bp, NULL, ENXIO); 763 return; 764 } 765 766 /* 767 * Place it in the queue of disk activities for this disk 768 */ 769 if (bp->bio_cmd == BIO_DELETE) { 770 bioq_disksort(&softc->trim_queue, bp); 771 } else { 772 if (ADA_SIO) 773 bioq_disksort(&softc->bio_queue, bp); 774 else 775 bioq_insert_tail(&softc->bio_queue, bp); 776 } 777 778 /* 779 * Schedule ourselves for performing the work. 780 */ 781 adaschedule(periph); 782 cam_periph_unlock(periph); 783 784 return; 785 } 786 787 static int 788 adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) 789 { 790 struct cam_periph *periph; 791 struct ada_softc *softc; 792 u_int secsize; 793 union ccb ccb; 794 struct disk *dp; 795 uint64_t lba; 796 uint16_t count; 797 int error = 0; 798 799 dp = arg; 800 periph = dp->d_drv1; 801 softc = (struct ada_softc *)periph->softc; 802 cam_periph_lock(periph); 803 secsize = softc->params.secsize; 804 lba = offset / secsize; 805 count = length / secsize; 806 807 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 808 cam_periph_unlock(periph); 809 return (ENXIO); 810 } 811 812 if (length > 0) { 813 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 814 ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 815 cam_fill_ataio(&ccb.ataio, 816 0, 817 adadone, 818 CAM_DIR_OUT, 819 0, 820 (u_int8_t *) virtual, 821 length, 822 ada_default_timeout*1000); 823 if ((softc->flags & ADA_FLAG_CAN_48BIT) && 824 (lba + count >= ATA_MAX_28BIT_LBA || 825 count >= 256)) { 826 ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48, 827 0, lba, count); 828 } else { 829 ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA, 830 0, lba, count); 831 } 832 xpt_polled_action(&ccb); 833 834 error = cam_periph_error(&ccb, 835 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 836 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 837 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0, 838 /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 839 if (error != 0) 840 printf("Aborting dump due to I/O error.\n"); 841 842 cam_periph_unlock(periph); 843 return (error); 844 } 845 846 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) { 847 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 848 849 ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 850 cam_fill_ataio(&ccb.ataio, 851 0, 852 adadone, 853 CAM_DIR_NONE, 854 0, 855 NULL, 856 0, 857 ada_default_timeout*1000); 858 859 if (softc->flags & ADA_FLAG_CAN_48BIT) 860 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0); 861 else 862 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0); 863 xpt_polled_action(&ccb); 864 865 error = cam_periph_error(&ccb, 866 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 867 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 868 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0, 869 /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 870 if (error != 0) 871 xpt_print(periph->path, "Synchronize cache failed\n"); 872 } 873 cam_periph_unlock(periph); 874 return (error); 875 } 876 877 static void 878 adainit(void) 879 { 880 cam_status status; 881 882 /* 883 * Install a global async callback. This callback will 884 * receive async callbacks like "new device found". 885 */ 886 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL); 887 888 if (status != CAM_REQ_CMP) { 889 printf("ada: Failed to attach master async callback " 890 "due to status 0x%x!\n", status); 891 } else if (ada_send_ordered) { 892 893 /* Register our event handlers */ 894 if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend, 895 NULL, EVENTHANDLER_PRI_LAST)) == NULL) 896 printf("adainit: power event registration failed!\n"); 897 if ((EVENTHANDLER_REGISTER(power_resume, adaresume, 898 NULL, EVENTHANDLER_PRI_LAST)) == NULL) 899 printf("adainit: power event registration failed!\n"); 900 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown, 901 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 902 printf("adainit: shutdown event registration failed!\n"); 903 } 904 } 905 906 /* 907 * Callback from GEOM, called when it has finished cleaning up its 908 * resources. 909 */ 910 static void 911 adadiskgonecb(struct disk *dp) 912 { 913 struct cam_periph *periph; 914 915 periph = (struct cam_periph *)dp->d_drv1; 916 917 cam_periph_release(periph); 918 } 919 920 static void 921 adaoninvalidate(struct cam_periph *periph) 922 { 923 struct ada_softc *softc; 924 925 softc = (struct ada_softc *)periph->softc; 926 927 /* 928 * De-register any async callbacks. 929 */ 930 xpt_register_async(0, adaasync, periph, periph->path); 931 932 /* 933 * Return all queued I/O with ENXIO. 934 * XXX Handle any transactions queued to the card 935 * with XPT_ABORT_CCB. 936 */ 937 bioq_flush(&softc->bio_queue, NULL, ENXIO); 938 bioq_flush(&softc->trim_queue, NULL, ENXIO); 939 940 disk_gone(softc->disk); 941 } 942 943 static void 944 adacleanup(struct cam_periph *periph) 945 { 946 struct ada_softc *softc; 947 948 softc = (struct ada_softc *)periph->softc; 949 950 cam_periph_unlock(periph); 951 952 /* 953 * If we can't free the sysctl tree, oh well... 954 */ 955 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0 956 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) { 957 xpt_print(periph->path, "can't remove sysctl context\n"); 958 } 959 960 disk_destroy(softc->disk); 961 callout_drain(&softc->sendordered_c); 962 free(softc, M_DEVBUF); 963 cam_periph_lock(periph); 964 } 965 966 static void 967 adaasync(void *callback_arg, u_int32_t code, 968 struct cam_path *path, void *arg) 969 { 970 struct ccb_getdev cgd; 971 struct cam_periph *periph; 972 struct ada_softc *softc; 973 974 periph = (struct cam_periph *)callback_arg; 975 switch (code) { 976 case AC_FOUND_DEVICE: 977 { 978 struct ccb_getdev *cgd; 979 cam_status status; 980 981 cgd = (struct ccb_getdev *)arg; 982 if (cgd == NULL) 983 break; 984 985 if (cgd->protocol != PROTO_ATA) 986 break; 987 988 /* 989 * Allocate a peripheral instance for 990 * this device and start the probe 991 * process. 992 */ 993 status = cam_periph_alloc(adaregister, adaoninvalidate, 994 adacleanup, adastart, 995 "ada", CAM_PERIPH_BIO, 996 path, adaasync, 997 AC_FOUND_DEVICE, cgd); 998 999 if (status != CAM_REQ_CMP 1000 && status != CAM_REQ_INPROG) 1001 printf("adaasync: Unable to attach to new device " 1002 "due to status 0x%x\n", status); 1003 break; 1004 } 1005 case AC_GETDEV_CHANGED: 1006 { 1007 softc = (struct ada_softc *)periph->softc; 1008 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1009 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1010 xpt_action((union ccb *)&cgd); 1011 1012 if ((cgd.ident_data.capabilities1 & ATA_SUPPORT_DMA) && 1013 (cgd.inq_flags & SID_DMA)) 1014 softc->flags |= ADA_FLAG_CAN_DMA; 1015 else 1016 softc->flags &= ~ADA_FLAG_CAN_DMA; 1017 if (cgd.ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) { 1018 softc->flags |= ADA_FLAG_CAN_48BIT; 1019 if (cgd.inq_flags & SID_DMA48) 1020 softc->flags |= ADA_FLAG_CAN_DMA48; 1021 else 1022 softc->flags &= ~ADA_FLAG_CAN_DMA48; 1023 } else 1024 softc->flags &= ~(ADA_FLAG_CAN_48BIT | 1025 ADA_FLAG_CAN_DMA48); 1026 if ((cgd.ident_data.satacapabilities & ATA_SUPPORT_NCQ) && 1027 (cgd.inq_flags & SID_DMA) && (cgd.inq_flags & SID_CmdQue)) 1028 softc->flags |= ADA_FLAG_CAN_NCQ; 1029 else 1030 softc->flags &= ~ADA_FLAG_CAN_NCQ; 1031 if ((cgd.ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) && 1032 (cgd.inq_flags & SID_DMA)) 1033 softc->flags |= ADA_FLAG_CAN_TRIM; 1034 else 1035 softc->flags &= ~ADA_FLAG_CAN_TRIM; 1036 1037 cam_periph_async(periph, code, path, arg); 1038 break; 1039 } 1040 case AC_ADVINFO_CHANGED: 1041 { 1042 uintptr_t buftype; 1043 1044 buftype = (uintptr_t)arg; 1045 if (buftype == CDAI_TYPE_PHYS_PATH) { 1046 struct ada_softc *softc; 1047 1048 softc = periph->softc; 1049 disk_attr_changed(softc->disk, "GEOM::physpath", 1050 M_NOWAIT); 1051 } 1052 break; 1053 } 1054 case AC_SENT_BDR: 1055 case AC_BUS_RESET: 1056 { 1057 softc = (struct ada_softc *)periph->softc; 1058 cam_periph_async(periph, code, path, arg); 1059 if (softc->state != ADA_STATE_NORMAL) 1060 break; 1061 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1062 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1063 xpt_action((union ccb *)&cgd); 1064 if (ADA_RA >= 0 && 1065 cgd.ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) 1066 softc->state = ADA_STATE_RAHEAD; 1067 else if (ADA_WC >= 0 && 1068 cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) 1069 softc->state = ADA_STATE_WCACHE; 1070 else 1071 break; 1072 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 1073 softc->state = ADA_STATE_NORMAL; 1074 else 1075 xpt_schedule(periph, CAM_PRIORITY_DEV); 1076 } 1077 default: 1078 cam_periph_async(periph, code, path, arg); 1079 break; 1080 } 1081 } 1082 1083 static void 1084 adasysctlinit(void *context, int pending) 1085 { 1086 struct cam_periph *periph; 1087 struct ada_softc *softc; 1088 char tmpstr[80], tmpstr2[80]; 1089 1090 periph = (struct cam_periph *)context; 1091 1092 /* periph was held for us when this task was enqueued */ 1093 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 1094 cam_periph_release(periph); 1095 return; 1096 } 1097 1098 softc = (struct ada_softc *)periph->softc; 1099 snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number); 1100 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 1101 1102 sysctl_ctx_init(&softc->sysctl_ctx); 1103 softc->flags |= ADA_FLAG_SCTX_INIT; 1104 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1105 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2, 1106 CTLFLAG_RD, 0, tmpstr); 1107 if (softc->sysctl_tree == NULL) { 1108 printf("adasysctlinit: unable to allocate sysctl tree\n"); 1109 cam_periph_release(periph); 1110 return; 1111 } 1112 1113 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1114 OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE, 1115 &softc->read_ahead, 0, "Enable disk read ahead."); 1116 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1117 OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE, 1118 &softc->write_cache, 0, "Enable disk write cache."); 1119 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1120 OID_AUTO, "sort_io_queue", CTLFLAG_RW | CTLFLAG_MPSAFE, 1121 &softc->sort_io_queue, 0, 1122 "Sort IO queue to try and optimise disk access patterns"); 1123 #ifdef ADA_TEST_FAILURE 1124 /* 1125 * Add a 'door bell' sysctl which allows one to set it from userland 1126 * and cause something bad to happen. For the moment, we only allow 1127 * whacking the next read or write. 1128 */ 1129 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1130 OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1131 &softc->force_read_error, 0, 1132 "Force a read error for the next N reads."); 1133 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1134 OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1135 &softc->force_write_error, 0, 1136 "Force a write error for the next N writes."); 1137 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1138 OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1139 &softc->periodic_read_error, 0, 1140 "Force a read error every N reads (don't set too low)."); 1141 #endif 1142 cam_periph_release(periph); 1143 } 1144 1145 static int 1146 adagetattr(struct bio *bp) 1147 { 1148 int ret; 1149 struct cam_periph *periph; 1150 1151 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 1152 cam_periph_lock(periph); 1153 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute, 1154 periph->path); 1155 cam_periph_unlock(periph); 1156 if (ret == 0) 1157 bp->bio_completed = bp->bio_length; 1158 return ret; 1159 } 1160 1161 static cam_status 1162 adaregister(struct cam_periph *periph, void *arg) 1163 { 1164 struct ada_softc *softc; 1165 struct ccb_pathinq cpi; 1166 struct ccb_getdev *cgd; 1167 char announce_buf[80], buf1[32]; 1168 struct disk_params *dp; 1169 caddr_t match; 1170 u_int maxio; 1171 int legacy_id, quirks; 1172 1173 cgd = (struct ccb_getdev *)arg; 1174 if (cgd == NULL) { 1175 printf("adaregister: no getdev CCB, can't register device\n"); 1176 return(CAM_REQ_CMP_ERR); 1177 } 1178 1179 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF, 1180 M_NOWAIT|M_ZERO); 1181 1182 if (softc == NULL) { 1183 printf("adaregister: Unable to probe new device. " 1184 "Unable to allocate softc\n"); 1185 return(CAM_REQ_CMP_ERR); 1186 } 1187 1188 bioq_init(&softc->bio_queue); 1189 bioq_init(&softc->trim_queue); 1190 1191 if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) && 1192 (cgd->inq_flags & SID_DMA)) 1193 softc->flags |= ADA_FLAG_CAN_DMA; 1194 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) { 1195 softc->flags |= ADA_FLAG_CAN_48BIT; 1196 if (cgd->inq_flags & SID_DMA48) 1197 softc->flags |= ADA_FLAG_CAN_DMA48; 1198 } 1199 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE) 1200 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE; 1201 if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT) 1202 softc->flags |= ADA_FLAG_CAN_POWERMGT; 1203 if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) && 1204 (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue)) 1205 softc->flags |= ADA_FLAG_CAN_NCQ; 1206 if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) && 1207 (cgd->inq_flags & SID_DMA)) { 1208 softc->flags |= ADA_FLAG_CAN_TRIM; 1209 softc->trim_max_ranges = TRIM_MAX_RANGES; 1210 if (cgd->ident_data.max_dsm_blocks != 0) { 1211 softc->trim_max_ranges = 1212 min(cgd->ident_data.max_dsm_blocks * 1213 ATA_DSM_BLK_RANGES, softc->trim_max_ranges); 1214 } 1215 } 1216 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA) 1217 softc->flags |= ADA_FLAG_CAN_CFA; 1218 1219 periph->softc = softc; 1220 1221 /* 1222 * See if this device has any quirks. 1223 */ 1224 match = cam_quirkmatch((caddr_t)&cgd->ident_data, 1225 (caddr_t)ada_quirk_table, 1226 sizeof(ada_quirk_table)/sizeof(*ada_quirk_table), 1227 sizeof(*ada_quirk_table), ata_identify_match); 1228 if (match != NULL) 1229 softc->quirks = ((struct ada_quirk_entry *)match)->quirks; 1230 else 1231 softc->quirks = ADA_Q_NONE; 1232 1233 bzero(&cpi, sizeof(cpi)); 1234 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE); 1235 cpi.ccb_h.func_code = XPT_PATH_INQ; 1236 xpt_action((union ccb *)&cpi); 1237 1238 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph); 1239 1240 /* 1241 * Register this media as a disk 1242 */ 1243 (void)cam_periph_hold(periph, PRIBIO); 1244 cam_periph_unlock(periph); 1245 snprintf(announce_buf, sizeof(announce_buf), 1246 "kern.cam.ada.%d.quirks", periph->unit_number); 1247 quirks = softc->quirks; 1248 TUNABLE_INT_FETCH(announce_buf, &quirks); 1249 softc->quirks = quirks; 1250 softc->read_ahead = -1; 1251 snprintf(announce_buf, sizeof(announce_buf), 1252 "kern.cam.ada.%d.read_ahead", periph->unit_number); 1253 TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead); 1254 softc->write_cache = -1; 1255 snprintf(announce_buf, sizeof(announce_buf), 1256 "kern.cam.ada.%d.write_cache", periph->unit_number); 1257 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache); 1258 /* Disable queue sorting for non-rotational media by default. */ 1259 if (cgd->ident_data.media_rotation_rate == ATA_RATE_NON_ROTATING) 1260 softc->sort_io_queue = 0; 1261 else 1262 softc->sort_io_queue = -1; 1263 adagetparams(periph, cgd); 1264 softc->disk = disk_alloc(); 1265 softc->disk->d_rotation_rate = cgd->ident_data.media_rotation_rate; 1266 softc->disk->d_devstat = devstat_new_entry(periph->periph_name, 1267 periph->unit_number, softc->params.secsize, 1268 DEVSTAT_ALL_SUPPORTED, 1269 DEVSTAT_TYPE_DIRECT | 1270 XPORT_DEVSTAT_TYPE(cpi.transport), 1271 DEVSTAT_PRIORITY_DISK); 1272 softc->disk->d_open = adaopen; 1273 softc->disk->d_close = adaclose; 1274 softc->disk->d_strategy = adastrategy; 1275 softc->disk->d_getattr = adagetattr; 1276 softc->disk->d_dump = adadump; 1277 softc->disk->d_gone = adadiskgonecb; 1278 softc->disk->d_name = "ada"; 1279 softc->disk->d_drv1 = periph; 1280 maxio = cpi.maxio; /* Honor max I/O size of SIM */ 1281 if (maxio == 0) 1282 maxio = DFLTPHYS; /* traditional default */ 1283 else if (maxio > MAXPHYS) 1284 maxio = MAXPHYS; /* for safety */ 1285 if (softc->flags & ADA_FLAG_CAN_48BIT) 1286 maxio = min(maxio, 65536 * softc->params.secsize); 1287 else /* 28bit ATA command limit */ 1288 maxio = min(maxio, 256 * softc->params.secsize); 1289 softc->disk->d_maxsize = maxio; 1290 softc->disk->d_unit = periph->unit_number; 1291 softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION; 1292 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) 1293 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE; 1294 if (softc->flags & ADA_FLAG_CAN_TRIM) { 1295 softc->disk->d_flags |= DISKFLAG_CANDELETE; 1296 softc->disk->d_delmaxsize = softc->params.secsize * 1297 ATA_DSM_RANGE_MAX * 1298 softc->trim_max_ranges; 1299 } else if ((softc->flags & ADA_FLAG_CAN_CFA) && 1300 !(softc->flags & ADA_FLAG_CAN_48BIT)) { 1301 softc->disk->d_flags |= DISKFLAG_CANDELETE; 1302 softc->disk->d_delmaxsize = 256 * softc->params.secsize; 1303 } else 1304 softc->disk->d_delmaxsize = maxio; 1305 if ((cpi.hba_misc & PIM_UNMAPPED) != 0) 1306 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO; 1307 strlcpy(softc->disk->d_descr, cgd->ident_data.model, 1308 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model))); 1309 strlcpy(softc->disk->d_ident, cgd->ident_data.serial, 1310 MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial))); 1311 softc->disk->d_hba_vendor = cpi.hba_vendor; 1312 softc->disk->d_hba_device = cpi.hba_device; 1313 softc->disk->d_hba_subvendor = cpi.hba_subvendor; 1314 softc->disk->d_hba_subdevice = cpi.hba_subdevice; 1315 1316 softc->disk->d_sectorsize = softc->params.secsize; 1317 softc->disk->d_mediasize = (off_t)softc->params.sectors * 1318 softc->params.secsize; 1319 if (ata_physical_sector_size(&cgd->ident_data) != 1320 softc->params.secsize) { 1321 softc->disk->d_stripesize = 1322 ata_physical_sector_size(&cgd->ident_data); 1323 softc->disk->d_stripeoffset = (softc->disk->d_stripesize - 1324 ata_logical_sector_offset(&cgd->ident_data)) % 1325 softc->disk->d_stripesize; 1326 } else if (softc->quirks & ADA_Q_4K) { 1327 softc->disk->d_stripesize = 4096; 1328 softc->disk->d_stripeoffset = 0; 1329 } 1330 softc->disk->d_fwsectors = softc->params.secs_per_track; 1331 softc->disk->d_fwheads = softc->params.heads; 1332 ata_disk_firmware_geom_adjust(softc->disk); 1333 1334 if (ada_legacy_aliases) { 1335 #ifdef ATA_STATIC_ID 1336 legacy_id = xpt_path_legacy_ata_id(periph->path); 1337 #else 1338 legacy_id = softc->disk->d_unit; 1339 #endif 1340 if (legacy_id >= 0) { 1341 snprintf(announce_buf, sizeof(announce_buf), 1342 "kern.devalias.%s%d", 1343 softc->disk->d_name, softc->disk->d_unit); 1344 snprintf(buf1, sizeof(buf1), 1345 "ad%d", legacy_id); 1346 kern_setenv(announce_buf, buf1); 1347 } 1348 } else 1349 legacy_id = -1; 1350 /* 1351 * Acquire a reference to the periph before we register with GEOM. 1352 * We'll release this reference once GEOM calls us back (via 1353 * adadiskgonecb()) telling us that our provider has been freed. 1354 */ 1355 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 1356 xpt_print(periph->path, "%s: lost periph during " 1357 "registration!\n", __func__); 1358 cam_periph_lock(periph); 1359 return (CAM_REQ_CMP_ERR); 1360 } 1361 disk_create(softc->disk, DISK_VERSION); 1362 cam_periph_lock(periph); 1363 cam_periph_unhold(periph); 1364 1365 dp = &softc->params; 1366 snprintf(announce_buf, sizeof(announce_buf), 1367 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)", 1368 (uintmax_t)(((uintmax_t)dp->secsize * 1369 dp->sectors) / (1024*1024)), 1370 (uintmax_t)dp->sectors, 1371 dp->secsize, dp->heads, 1372 dp->secs_per_track, dp->cylinders); 1373 xpt_announce_periph(periph, announce_buf); 1374 xpt_announce_quirks(periph, softc->quirks, ADA_Q_BIT_STRING); 1375 if (legacy_id >= 0) 1376 printf("%s%d: Previously was known as ad%d\n", 1377 periph->periph_name, periph->unit_number, legacy_id); 1378 1379 /* 1380 * Create our sysctl variables, now that we know 1381 * we have successfully attached. 1382 */ 1383 if (cam_periph_acquire(periph) == CAM_REQ_CMP) 1384 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task); 1385 1386 /* 1387 * Add async callbacks for bus reset and 1388 * bus device reset calls. I don't bother 1389 * checking if this fails as, in most cases, 1390 * the system will function just fine without 1391 * them and the only alternative would be to 1392 * not attach the device on failure. 1393 */ 1394 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE | 1395 AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED, 1396 adaasync, periph, periph->path); 1397 1398 /* 1399 * Schedule a periodic event to occasionally send an 1400 * ordered tag to a device. 1401 */ 1402 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0); 1403 callout_reset(&softc->sendordered_c, 1404 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 1405 adasendorderedtag, softc); 1406 1407 if (ADA_RA >= 0 && 1408 cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) { 1409 softc->state = ADA_STATE_RAHEAD; 1410 } else if (ADA_WC >= 0 && 1411 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) { 1412 softc->state = ADA_STATE_WCACHE; 1413 } else { 1414 softc->state = ADA_STATE_NORMAL; 1415 return(CAM_REQ_CMP); 1416 } 1417 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 1418 softc->state = ADA_STATE_NORMAL; 1419 else 1420 xpt_schedule(periph, CAM_PRIORITY_DEV); 1421 return(CAM_REQ_CMP); 1422 } 1423 1424 static void 1425 ada_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 1426 { 1427 struct trim_request *req = &softc->trim_req; 1428 uint64_t lastlba = (uint64_t)-1; 1429 int c, lastcount = 0, off, ranges = 0; 1430 1431 bzero(req, sizeof(*req)); 1432 TAILQ_INIT(&req->bps); 1433 do { 1434 uint64_t lba = bp->bio_pblkno; 1435 int count = bp->bio_bcount / softc->params.secsize; 1436 1437 bioq_remove(&softc->trim_queue, bp); 1438 1439 /* Try to extend the previous range. */ 1440 if (lba == lastlba) { 1441 c = min(count, ATA_DSM_RANGE_MAX - lastcount); 1442 lastcount += c; 1443 off = (ranges - 1) * ATA_DSM_RANGE_SIZE; 1444 req->data[off + 6] = lastcount & 0xff; 1445 req->data[off + 7] = 1446 (lastcount >> 8) & 0xff; 1447 count -= c; 1448 lba += c; 1449 } 1450 1451 while (count > 0) { 1452 c = min(count, ATA_DSM_RANGE_MAX); 1453 off = ranges * ATA_DSM_RANGE_SIZE; 1454 req->data[off + 0] = lba & 0xff; 1455 req->data[off + 1] = (lba >> 8) & 0xff; 1456 req->data[off + 2] = (lba >> 16) & 0xff; 1457 req->data[off + 3] = (lba >> 24) & 0xff; 1458 req->data[off + 4] = (lba >> 32) & 0xff; 1459 req->data[off + 5] = (lba >> 40) & 0xff; 1460 req->data[off + 6] = c & 0xff; 1461 req->data[off + 7] = (c >> 8) & 0xff; 1462 lba += c; 1463 count -= c; 1464 lastcount = c; 1465 ranges++; 1466 /* 1467 * Its the caller's responsibility to ensure the 1468 * request will fit so we don't need to check for 1469 * overrun here 1470 */ 1471 } 1472 lastlba = lba; 1473 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue); 1474 bp = bioq_first(&softc->trim_queue); 1475 if (bp == NULL || 1476 bp->bio_bcount / softc->params.secsize > 1477 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) 1478 break; 1479 } while (1); 1480 cam_fill_ataio(ataio, 1481 ada_retry_count, 1482 adadone, 1483 CAM_DIR_OUT, 1484 0, 1485 req->data, 1486 ((ranges + ATA_DSM_BLK_RANGES - 1) / 1487 ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE, 1488 ada_default_timeout * 1000); 1489 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT, 1490 ATA_DSM_TRIM, 0, (ranges + ATA_DSM_BLK_RANGES - 1491 1) / ATA_DSM_BLK_RANGES); 1492 } 1493 1494 static void 1495 ada_cfaerase(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 1496 { 1497 struct trim_request *req = &softc->trim_req; 1498 uint64_t lba = bp->bio_pblkno; 1499 uint16_t count = bp->bio_bcount / softc->params.secsize; 1500 1501 bzero(req, sizeof(*req)); 1502 TAILQ_INIT(&req->bps); 1503 bioq_remove(&softc->trim_queue, bp); 1504 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue); 1505 1506 cam_fill_ataio(ataio, 1507 ada_retry_count, 1508 adadone, 1509 CAM_DIR_NONE, 1510 0, 1511 NULL, 1512 0, 1513 ada_default_timeout*1000); 1514 1515 if (count >= 256) 1516 count = 0; 1517 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count); 1518 } 1519 1520 static void 1521 adastart(struct cam_periph *periph, union ccb *start_ccb) 1522 { 1523 struct ada_softc *softc = (struct ada_softc *)periph->softc; 1524 struct ccb_ataio *ataio = &start_ccb->ataio; 1525 1526 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n")); 1527 1528 switch (softc->state) { 1529 case ADA_STATE_NORMAL: 1530 { 1531 struct bio *bp; 1532 u_int8_t tag_code; 1533 1534 /* Run TRIM if not running yet. */ 1535 if (!softc->trim_running && 1536 (bp = bioq_first(&softc->trim_queue)) != 0) { 1537 if (softc->flags & ADA_FLAG_CAN_TRIM) { 1538 ada_dsmtrim(softc, bp, ataio); 1539 } else if ((softc->flags & ADA_FLAG_CAN_CFA) && 1540 !(softc->flags & ADA_FLAG_CAN_48BIT)) { 1541 ada_cfaerase(softc, bp, ataio); 1542 } else { 1543 /* This can happen if DMA was disabled. */ 1544 bioq_remove(&softc->trim_queue, bp); 1545 bp->bio_error = EOPNOTSUPP; 1546 bp->bio_flags |= BIO_ERROR; 1547 biodone(bp); 1548 xpt_release_ccb(start_ccb); 1549 adaschedule(periph); 1550 return; 1551 } 1552 softc->trim_running = 1; 1553 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM; 1554 start_ccb->ccb_h.flags |= CAM_UNLOCKED; 1555 goto out; 1556 } 1557 /* Run regular command. */ 1558 bp = bioq_first(&softc->bio_queue); 1559 if (bp == NULL) { 1560 xpt_release_ccb(start_ccb); 1561 break; 1562 } 1563 bioq_remove(&softc->bio_queue, bp); 1564 1565 if ((bp->bio_flags & BIO_ORDERED) != 0 1566 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) { 1567 softc->flags &= ~ADA_FLAG_NEED_OTAG; 1568 softc->flags |= ADA_FLAG_WAS_OTAG; 1569 tag_code = 0; 1570 } else { 1571 tag_code = 1; 1572 } 1573 switch (bp->bio_cmd) { 1574 case BIO_WRITE: 1575 softc->flags |= ADA_FLAG_DIRTY; 1576 /* FALLTHROUGH */ 1577 case BIO_READ: 1578 { 1579 uint64_t lba = bp->bio_pblkno; 1580 uint16_t count = bp->bio_bcount / softc->params.secsize; 1581 #ifdef ADA_TEST_FAILURE 1582 int fail = 0; 1583 1584 /* 1585 * Support the failure ioctls. If the command is a 1586 * read, and there are pending forced read errors, or 1587 * if a write and pending write errors, then fail this 1588 * operation with EIO. This is useful for testing 1589 * purposes. Also, support having every Nth read fail. 1590 * 1591 * This is a rather blunt tool. 1592 */ 1593 if (bp->bio_cmd == BIO_READ) { 1594 if (softc->force_read_error) { 1595 softc->force_read_error--; 1596 fail = 1; 1597 } 1598 if (softc->periodic_read_error > 0) { 1599 if (++softc->periodic_read_count >= 1600 softc->periodic_read_error) { 1601 softc->periodic_read_count = 0; 1602 fail = 1; 1603 } 1604 } 1605 } else { 1606 if (softc->force_write_error) { 1607 softc->force_write_error--; 1608 fail = 1; 1609 } 1610 } 1611 if (fail) { 1612 bp->bio_error = EIO; 1613 bp->bio_flags |= BIO_ERROR; 1614 biodone(bp); 1615 xpt_release_ccb(start_ccb); 1616 adaschedule(periph); 1617 return; 1618 } 1619 #endif 1620 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 || 1621 round_page(bp->bio_bcount + bp->bio_ma_offset) / 1622 PAGE_SIZE == bp->bio_ma_n, 1623 ("Short bio %p", bp)); 1624 cam_fill_ataio(ataio, 1625 ada_retry_count, 1626 adadone, 1627 (bp->bio_cmd == BIO_READ ? CAM_DIR_IN : 1628 CAM_DIR_OUT) | ((bp->bio_flags & BIO_UNMAPPED) 1629 != 0 ? CAM_DATA_BIO : 0), 1630 tag_code, 1631 ((bp->bio_flags & BIO_UNMAPPED) != 0) ? (void *)bp : 1632 bp->bio_data, 1633 bp->bio_bcount, 1634 ada_default_timeout*1000); 1635 1636 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) { 1637 if (bp->bio_cmd == BIO_READ) { 1638 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED, 1639 lba, count); 1640 } else { 1641 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED, 1642 lba, count); 1643 } 1644 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) && 1645 (lba + count >= ATA_MAX_28BIT_LBA || 1646 count > 256)) { 1647 if (softc->flags & ADA_FLAG_CAN_DMA48) { 1648 if (bp->bio_cmd == BIO_READ) { 1649 ata_48bit_cmd(ataio, ATA_READ_DMA48, 1650 0, lba, count); 1651 } else { 1652 ata_48bit_cmd(ataio, ATA_WRITE_DMA48, 1653 0, lba, count); 1654 } 1655 } else { 1656 if (bp->bio_cmd == BIO_READ) { 1657 ata_48bit_cmd(ataio, ATA_READ_MUL48, 1658 0, lba, count); 1659 } else { 1660 ata_48bit_cmd(ataio, ATA_WRITE_MUL48, 1661 0, lba, count); 1662 } 1663 } 1664 } else { 1665 if (count == 256) 1666 count = 0; 1667 if (softc->flags & ADA_FLAG_CAN_DMA) { 1668 if (bp->bio_cmd == BIO_READ) { 1669 ata_28bit_cmd(ataio, ATA_READ_DMA, 1670 0, lba, count); 1671 } else { 1672 ata_28bit_cmd(ataio, ATA_WRITE_DMA, 1673 0, lba, count); 1674 } 1675 } else { 1676 if (bp->bio_cmd == BIO_READ) { 1677 ata_28bit_cmd(ataio, ATA_READ_MUL, 1678 0, lba, count); 1679 } else { 1680 ata_28bit_cmd(ataio, ATA_WRITE_MUL, 1681 0, lba, count); 1682 } 1683 } 1684 } 1685 break; 1686 } 1687 case BIO_FLUSH: 1688 cam_fill_ataio(ataio, 1689 1, 1690 adadone, 1691 CAM_DIR_NONE, 1692 0, 1693 NULL, 1694 0, 1695 ada_default_timeout*1000); 1696 1697 if (softc->flags & ADA_FLAG_CAN_48BIT) 1698 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1699 else 1700 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0); 1701 break; 1702 } 1703 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO; 1704 start_ccb->ccb_h.flags |= CAM_UNLOCKED; 1705 out: 1706 start_ccb->ccb_h.ccb_bp = bp; 1707 softc->outstanding_cmds++; 1708 softc->refcount++; 1709 cam_periph_unlock(periph); 1710 xpt_action(start_ccb); 1711 cam_periph_lock(periph); 1712 softc->refcount--; 1713 1714 /* May have more work to do, so ensure we stay scheduled */ 1715 adaschedule(periph); 1716 break; 1717 } 1718 case ADA_STATE_RAHEAD: 1719 case ADA_STATE_WCACHE: 1720 { 1721 cam_fill_ataio(ataio, 1722 1, 1723 adadone, 1724 CAM_DIR_NONE, 1725 0, 1726 NULL, 1727 0, 1728 ada_default_timeout*1000); 1729 1730 if (softc->state == ADA_STATE_RAHEAD) { 1731 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ? 1732 ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0); 1733 start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD; 1734 } else { 1735 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ? 1736 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0); 1737 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE; 1738 } 1739 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 1740 xpt_action(start_ccb); 1741 break; 1742 } 1743 } 1744 } 1745 1746 static void 1747 adadone(struct cam_periph *periph, union ccb *done_ccb) 1748 { 1749 struct ada_softc *softc; 1750 struct ccb_ataio *ataio; 1751 struct ccb_getdev *cgd; 1752 struct cam_path *path; 1753 int state; 1754 1755 softc = (struct ada_softc *)periph->softc; 1756 ataio = &done_ccb->ataio; 1757 path = done_ccb->ccb_h.path; 1758 1759 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n")); 1760 1761 state = ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK; 1762 switch (state) { 1763 case ADA_CCB_BUFFER_IO: 1764 case ADA_CCB_TRIM: 1765 { 1766 struct bio *bp; 1767 int error; 1768 1769 cam_periph_lock(periph); 1770 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1771 error = adaerror(done_ccb, 0, 0); 1772 if (error == ERESTART) { 1773 /* A retry was scheduled, so just return. */ 1774 cam_periph_unlock(periph); 1775 return; 1776 } 1777 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1778 cam_release_devq(path, 1779 /*relsim_flags*/0, 1780 /*reduction*/0, 1781 /*timeout*/0, 1782 /*getcount_only*/0); 1783 } else { 1784 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1785 panic("REQ_CMP with QFRZN"); 1786 error = 0; 1787 } 1788 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 1789 bp->bio_error = error; 1790 if (error != 0) { 1791 bp->bio_resid = bp->bio_bcount; 1792 bp->bio_flags |= BIO_ERROR; 1793 } else { 1794 if (state == ADA_CCB_TRIM) 1795 bp->bio_resid = 0; 1796 else 1797 bp->bio_resid = ataio->resid; 1798 if (bp->bio_resid > 0) 1799 bp->bio_flags |= BIO_ERROR; 1800 } 1801 softc->outstanding_cmds--; 1802 if (softc->outstanding_cmds == 0) 1803 softc->flags |= ADA_FLAG_WAS_OTAG; 1804 xpt_release_ccb(done_ccb); 1805 if (state == ADA_CCB_TRIM) { 1806 TAILQ_HEAD(, bio) queue; 1807 struct bio *bp1; 1808 1809 TAILQ_INIT(&queue); 1810 TAILQ_CONCAT(&queue, &softc->trim_req.bps, bio_queue); 1811 /* 1812 * Normally, the xpt_release_ccb() above would make sure 1813 * that when we have more work to do, that work would 1814 * get kicked off. However, we specifically keep 1815 * trim_running set to 0 before the call above to allow 1816 * other I/O to progress when many BIO_DELETE requests 1817 * are pushed down. We set trim_running to 0 and call 1818 * daschedule again so that we don't stall if there are 1819 * no other I/Os pending apart from BIO_DELETEs. 1820 */ 1821 softc->trim_running = 0; 1822 adaschedule(periph); 1823 cam_periph_unlock(periph); 1824 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) { 1825 TAILQ_REMOVE(&queue, bp1, bio_queue); 1826 bp1->bio_error = error; 1827 if (error != 0) { 1828 bp1->bio_flags |= BIO_ERROR; 1829 bp1->bio_resid = bp1->bio_bcount; 1830 } else 1831 bp1->bio_resid = 0; 1832 biodone(bp1); 1833 } 1834 } else { 1835 cam_periph_unlock(periph); 1836 biodone(bp); 1837 } 1838 return; 1839 } 1840 case ADA_CCB_RAHEAD: 1841 { 1842 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1843 if (adaerror(done_ccb, 0, 0) == ERESTART) { 1844 out: 1845 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 1846 cam_release_devq(path, 0, 0, 0, FALSE); 1847 return; 1848 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1849 cam_release_devq(path, 1850 /*relsim_flags*/0, 1851 /*reduction*/0, 1852 /*timeout*/0, 1853 /*getcount_only*/0); 1854 } 1855 } 1856 1857 /* 1858 * Since our peripheral may be invalidated by an error 1859 * above or an external event, we must release our CCB 1860 * before releasing the reference on the peripheral. 1861 * The peripheral will only go away once the last reference 1862 * is removed, and we need it around for the CCB release 1863 * operation. 1864 */ 1865 cgd = (struct ccb_getdev *)done_ccb; 1866 xpt_setup_ccb(&cgd->ccb_h, path, CAM_PRIORITY_NORMAL); 1867 cgd->ccb_h.func_code = XPT_GDEV_TYPE; 1868 xpt_action((union ccb *)cgd); 1869 if (ADA_WC >= 0 && 1870 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) { 1871 softc->state = ADA_STATE_WCACHE; 1872 xpt_release_ccb(done_ccb); 1873 xpt_schedule(periph, CAM_PRIORITY_DEV); 1874 goto out; 1875 } 1876 softc->state = ADA_STATE_NORMAL; 1877 xpt_release_ccb(done_ccb); 1878 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 1879 cam_release_devq(path, 0, 0, 0, FALSE); 1880 adaschedule(periph); 1881 cam_periph_release_locked(periph); 1882 return; 1883 } 1884 case ADA_CCB_WCACHE: 1885 { 1886 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1887 if (adaerror(done_ccb, 0, 0) == ERESTART) { 1888 goto out; 1889 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1890 cam_release_devq(path, 1891 /*relsim_flags*/0, 1892 /*reduction*/0, 1893 /*timeout*/0, 1894 /*getcount_only*/0); 1895 } 1896 } 1897 1898 softc->state = ADA_STATE_NORMAL; 1899 /* 1900 * Since our peripheral may be invalidated by an error 1901 * above or an external event, we must release our CCB 1902 * before releasing the reference on the peripheral. 1903 * The peripheral will only go away once the last reference 1904 * is removed, and we need it around for the CCB release 1905 * operation. 1906 */ 1907 xpt_release_ccb(done_ccb); 1908 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 1909 cam_release_devq(path, 0, 0, 0, FALSE); 1910 adaschedule(periph); 1911 cam_periph_release_locked(periph); 1912 return; 1913 } 1914 case ADA_CCB_DUMP: 1915 /* No-op. We're polling */ 1916 return; 1917 default: 1918 break; 1919 } 1920 xpt_release_ccb(done_ccb); 1921 } 1922 1923 static int 1924 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 1925 { 1926 1927 return(cam_periph_error(ccb, cam_flags, sense_flags, NULL)); 1928 } 1929 1930 static void 1931 adagetparams(struct cam_periph *periph, struct ccb_getdev *cgd) 1932 { 1933 struct ada_softc *softc = (struct ada_softc *)periph->softc; 1934 struct disk_params *dp = &softc->params; 1935 u_int64_t lbasize48; 1936 u_int32_t lbasize; 1937 1938 dp->secsize = ata_logical_sector_size(&cgd->ident_data); 1939 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) && 1940 cgd->ident_data.current_heads && cgd->ident_data.current_sectors) { 1941 dp->heads = cgd->ident_data.current_heads; 1942 dp->secs_per_track = cgd->ident_data.current_sectors; 1943 dp->cylinders = cgd->ident_data.cylinders; 1944 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 | 1945 ((u_int32_t)cgd->ident_data.current_size_2 << 16); 1946 } else { 1947 dp->heads = cgd->ident_data.heads; 1948 dp->secs_per_track = cgd->ident_data.sectors; 1949 dp->cylinders = cgd->ident_data.cylinders; 1950 dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track; 1951 } 1952 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 | 1953 ((u_int32_t)cgd->ident_data.lba_size_2 << 16); 1954 1955 /* use the 28bit LBA size if valid or bigger than the CHS mapping */ 1956 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize) 1957 dp->sectors = lbasize; 1958 1959 /* use the 48bit LBA size if valid */ 1960 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) | 1961 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) | 1962 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) | 1963 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48); 1964 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) && 1965 lbasize48 > ATA_MAX_28BIT_LBA) 1966 dp->sectors = lbasize48; 1967 } 1968 1969 static void 1970 adasendorderedtag(void *arg) 1971 { 1972 struct ada_softc *softc = arg; 1973 1974 if (ada_send_ordered) { 1975 if (softc->outstanding_cmds > 0) { 1976 if ((softc->flags & ADA_FLAG_WAS_OTAG) == 0) 1977 softc->flags |= ADA_FLAG_NEED_OTAG; 1978 softc->flags &= ~ADA_FLAG_WAS_OTAG; 1979 } 1980 } 1981 /* Queue us up again */ 1982 callout_reset(&softc->sendordered_c, 1983 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 1984 adasendorderedtag, softc); 1985 } 1986 1987 /* 1988 * Step through all ADA peripheral drivers, and if the device is still open, 1989 * sync the disk cache to physical media. 1990 */ 1991 static void 1992 adaflush(void) 1993 { 1994 struct cam_periph *periph; 1995 struct ada_softc *softc; 1996 union ccb *ccb; 1997 int error; 1998 1999 CAM_PERIPH_FOREACH(periph, &adadriver) { 2000 softc = (struct ada_softc *)periph->softc; 2001 if (SCHEDULER_STOPPED()) { 2002 /* If we paniced with the lock held, do not recurse. */ 2003 if (!cam_periph_owned(periph) && 2004 (softc->flags & ADA_FLAG_OPEN)) { 2005 adadump(softc->disk, NULL, 0, 0, 0); 2006 } 2007 continue; 2008 } 2009 cam_periph_lock(periph); 2010 /* 2011 * We only sync the cache if the drive is still open, and 2012 * if the drive is capable of it.. 2013 */ 2014 if (((softc->flags & ADA_FLAG_OPEN) == 0) || 2015 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) { 2016 cam_periph_unlock(periph); 2017 continue; 2018 } 2019 2020 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 2021 cam_fill_ataio(&ccb->ataio, 2022 0, 2023 adadone, 2024 CAM_DIR_NONE, 2025 0, 2026 NULL, 2027 0, 2028 ada_default_timeout*1000); 2029 if (softc->flags & ADA_FLAG_CAN_48BIT) 2030 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 2031 else 2032 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 2033 2034 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 2035 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 2036 softc->disk->d_devstat); 2037 if (error != 0) 2038 xpt_print(periph->path, "Synchronize cache failed\n"); 2039 xpt_release_ccb(ccb); 2040 cam_periph_unlock(periph); 2041 } 2042 } 2043 2044 static void 2045 adaspindown(uint8_t cmd, int flags) 2046 { 2047 struct cam_periph *periph; 2048 struct ada_softc *softc; 2049 union ccb *ccb; 2050 int error; 2051 2052 CAM_PERIPH_FOREACH(periph, &adadriver) { 2053 /* If we paniced with lock held - not recurse here. */ 2054 if (cam_periph_owned(periph)) 2055 continue; 2056 cam_periph_lock(periph); 2057 softc = (struct ada_softc *)periph->softc; 2058 /* 2059 * We only spin-down the drive if it is capable of it.. 2060 */ 2061 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 2062 cam_periph_unlock(periph); 2063 continue; 2064 } 2065 2066 if (bootverbose) 2067 xpt_print(periph->path, "spin-down\n"); 2068 2069 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 2070 cam_fill_ataio(&ccb->ataio, 2071 0, 2072 adadone, 2073 CAM_DIR_NONE | flags, 2074 0, 2075 NULL, 2076 0, 2077 ada_default_timeout*1000); 2078 ata_28bit_cmd(&ccb->ataio, cmd, 0, 0, 0); 2079 2080 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 2081 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 2082 softc->disk->d_devstat); 2083 if (error != 0) 2084 xpt_print(periph->path, "Spin-down disk failed\n"); 2085 xpt_release_ccb(ccb); 2086 cam_periph_unlock(periph); 2087 } 2088 } 2089 2090 static void 2091 adashutdown(void *arg, int howto) 2092 { 2093 2094 adaflush(); 2095 if (ada_spindown_shutdown != 0 && 2096 (howto & (RB_HALT | RB_POWEROFF)) != 0) 2097 adaspindown(ATA_STANDBY_IMMEDIATE, 0); 2098 } 2099 2100 static void 2101 adasuspend(void *arg) 2102 { 2103 2104 adaflush(); 2105 if (ada_spindown_suspend != 0) 2106 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE); 2107 } 2108 2109 static void 2110 adaresume(void *arg) 2111 { 2112 struct cam_periph *periph; 2113 struct ada_softc *softc; 2114 2115 if (ada_spindown_suspend == 0) 2116 return; 2117 2118 CAM_PERIPH_FOREACH(periph, &adadriver) { 2119 cam_periph_lock(periph); 2120 softc = (struct ada_softc *)periph->softc; 2121 /* 2122 * We only spin-down the drive if it is capable of it.. 2123 */ 2124 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 2125 cam_periph_unlock(periph); 2126 continue; 2127 } 2128 2129 if (bootverbose) 2130 xpt_print(periph->path, "resume\n"); 2131 2132 /* 2133 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on 2134 * sleep request. 2135 */ 2136 cam_release_devq(periph->path, 2137 /*relsim_flags*/0, 2138 /*openings*/0, 2139 /*timeout*/0, 2140 /*getcount_only*/0); 2141 2142 cam_periph_unlock(periph); 2143 } 2144 } 2145 2146 #endif /* _KERNEL */ 2147