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