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/endian.h> 47 #include <sys/cons.h> 48 #include <sys/proc.h> 49 #include <sys/reboot.h> 50 #include <sys/sbuf.h> 51 #include <geom/geom_disk.h> 52 #endif /* _KERNEL */ 53 54 #ifndef _KERNEL 55 #include <stdio.h> 56 #include <string.h> 57 #endif /* _KERNEL */ 58 59 #include <cam/cam.h> 60 #include <cam/cam_ccb.h> 61 #include <cam/cam_periph.h> 62 #include <cam/cam_xpt_periph.h> 63 #include <cam/scsi/scsi_all.h> 64 #include <cam/scsi/scsi_da.h> 65 #include <cam/cam_sim.h> 66 #include <cam/cam_iosched.h> 67 68 #include <cam/ata/ata_all.h> 69 70 #include <machine/md_var.h> /* geometry translation */ 71 72 #ifdef _KERNEL 73 74 #define ATA_MAX_28BIT_LBA 268435455UL 75 76 extern int iosched_debug; 77 78 typedef enum { 79 ADA_STATE_RAHEAD, 80 ADA_STATE_WCACHE, 81 ADA_STATE_LOGDIR, 82 ADA_STATE_IDDIR, 83 ADA_STATE_SUP_CAP, 84 ADA_STATE_ZONE, 85 ADA_STATE_NORMAL 86 } ada_state; 87 88 typedef enum { 89 ADA_FLAG_CAN_48BIT = 0x00000002, 90 ADA_FLAG_CAN_FLUSHCACHE = 0x00000004, 91 ADA_FLAG_CAN_NCQ = 0x00000008, 92 ADA_FLAG_CAN_DMA = 0x00000010, 93 ADA_FLAG_NEED_OTAG = 0x00000020, 94 ADA_FLAG_WAS_OTAG = 0x00000040, 95 ADA_FLAG_CAN_TRIM = 0x00000080, 96 ADA_FLAG_OPEN = 0x00000100, 97 ADA_FLAG_SCTX_INIT = 0x00000200, 98 ADA_FLAG_CAN_CFA = 0x00000400, 99 ADA_FLAG_CAN_POWERMGT = 0x00000800, 100 ADA_FLAG_CAN_DMA48 = 0x00001000, 101 ADA_FLAG_CAN_LOG = 0x00002000, 102 ADA_FLAG_CAN_IDLOG = 0x00004000, 103 ADA_FLAG_CAN_SUPCAP = 0x00008000, 104 ADA_FLAG_CAN_ZONE = 0x00010000, 105 ADA_FLAG_CAN_WCACHE = 0x00020000, 106 ADA_FLAG_CAN_RAHEAD = 0x00040000, 107 ADA_FLAG_PROBED = 0x00080000, 108 ADA_FLAG_ANNOUNCED = 0x00100000, 109 ADA_FLAG_DIRTY = 0x00200000, 110 ADA_FLAG_CAN_NCQ_TRIM = 0x00400000, /* CAN_TRIM also set */ 111 ADA_FLAG_PIM_ATA_EXT = 0x00800000 112 } ada_flags; 113 114 typedef enum { 115 ADA_Q_NONE = 0x00, 116 ADA_Q_4K = 0x01, 117 ADA_Q_NCQ_TRIM_BROKEN = 0x02, 118 } ada_quirks; 119 120 #define ADA_Q_BIT_STRING \ 121 "\020" \ 122 "\0014K" \ 123 "\002NCQ_TRIM_BROKEN" 124 125 typedef enum { 126 ADA_CCB_RAHEAD = 0x01, 127 ADA_CCB_WCACHE = 0x02, 128 ADA_CCB_BUFFER_IO = 0x03, 129 ADA_CCB_DUMP = 0x05, 130 ADA_CCB_TRIM = 0x06, 131 ADA_CCB_LOGDIR = 0x07, 132 ADA_CCB_IDDIR = 0x08, 133 ADA_CCB_SUP_CAP = 0x09, 134 ADA_CCB_ZONE = 0x0a, 135 ADA_CCB_TYPE_MASK = 0x0F, 136 } ada_ccb_state; 137 138 typedef enum { 139 ADA_ZONE_NONE = 0x00, 140 ADA_ZONE_DRIVE_MANAGED = 0x01, 141 ADA_ZONE_HOST_AWARE = 0x02, 142 ADA_ZONE_HOST_MANAGED = 0x03 143 } ada_zone_mode; 144 145 typedef enum { 146 ADA_ZONE_FLAG_RZ_SUP = 0x0001, 147 ADA_ZONE_FLAG_OPEN_SUP = 0x0002, 148 ADA_ZONE_FLAG_CLOSE_SUP = 0x0004, 149 ADA_ZONE_FLAG_FINISH_SUP = 0x0008, 150 ADA_ZONE_FLAG_RWP_SUP = 0x0010, 151 ADA_ZONE_FLAG_SUP_MASK = (ADA_ZONE_FLAG_RZ_SUP | 152 ADA_ZONE_FLAG_OPEN_SUP | 153 ADA_ZONE_FLAG_CLOSE_SUP | 154 ADA_ZONE_FLAG_FINISH_SUP | 155 ADA_ZONE_FLAG_RWP_SUP), 156 ADA_ZONE_FLAG_URSWRZ = 0x0020, 157 ADA_ZONE_FLAG_OPT_SEQ_SET = 0x0040, 158 ADA_ZONE_FLAG_OPT_NONSEQ_SET = 0x0080, 159 ADA_ZONE_FLAG_MAX_SEQ_SET = 0x0100, 160 ADA_ZONE_FLAG_SET_MASK = (ADA_ZONE_FLAG_OPT_SEQ_SET | 161 ADA_ZONE_FLAG_OPT_NONSEQ_SET | 162 ADA_ZONE_FLAG_MAX_SEQ_SET) 163 } ada_zone_flags; 164 165 static struct ada_zone_desc { 166 ada_zone_flags value; 167 const char *desc; 168 } ada_zone_desc_table[] = { 169 {ADA_ZONE_FLAG_RZ_SUP, "Report Zones" }, 170 {ADA_ZONE_FLAG_OPEN_SUP, "Open" }, 171 {ADA_ZONE_FLAG_CLOSE_SUP, "Close" }, 172 {ADA_ZONE_FLAG_FINISH_SUP, "Finish" }, 173 {ADA_ZONE_FLAG_RWP_SUP, "Reset Write Pointer" }, 174 }; 175 176 177 /* Offsets into our private area for storing information */ 178 #define ccb_state ppriv_field0 179 #define ccb_bp ppriv_ptr1 180 181 typedef enum { 182 ADA_DELETE_NONE, 183 ADA_DELETE_DISABLE, 184 ADA_DELETE_CFA_ERASE, 185 ADA_DELETE_DSM_TRIM, 186 ADA_DELETE_NCQ_DSM_TRIM, 187 ADA_DELETE_MIN = ADA_DELETE_CFA_ERASE, 188 ADA_DELETE_MAX = ADA_DELETE_NCQ_DSM_TRIM, 189 } ada_delete_methods; 190 191 static const char *ada_delete_method_names[] = 192 { "NONE", "DISABLE", "CFA_ERASE", "DSM_TRIM", "NCQ_DSM_TRIM" }; 193 #if 0 194 static const char *ada_delete_method_desc[] = 195 { "NONE", "DISABLED", "CFA Erase", "DSM Trim", "DSM Trim via NCQ" }; 196 #endif 197 198 struct disk_params { 199 u_int8_t heads; 200 u_int8_t secs_per_track; 201 u_int32_t cylinders; 202 u_int32_t secsize; /* Number of bytes/logical sector */ 203 u_int64_t sectors; /* Total number sectors */ 204 }; 205 206 #define TRIM_MAX_BLOCKS 8 207 #define TRIM_MAX_RANGES (TRIM_MAX_BLOCKS * ATA_DSM_BLK_RANGES) 208 struct trim_request { 209 uint8_t data[TRIM_MAX_RANGES * ATA_DSM_RANGE_SIZE]; 210 TAILQ_HEAD(, bio) bps; 211 }; 212 213 struct ada_softc { 214 struct cam_iosched_softc *cam_iosched; 215 int outstanding_cmds; /* Number of active commands */ 216 int refcount; /* Active xpt_action() calls */ 217 ada_state state; 218 ada_flags flags; 219 ada_zone_mode zone_mode; 220 ada_zone_flags zone_flags; 221 struct ata_gp_log_dir ata_logdir; 222 int valid_logdir_len; 223 struct ata_identify_log_pages ata_iddir; 224 int valid_iddir_len; 225 uint64_t optimal_seq_zones; 226 uint64_t optimal_nonseq_zones; 227 uint64_t max_seq_zones; 228 ada_quirks quirks; 229 ada_delete_methods delete_method; 230 int trim_max_ranges; 231 int read_ahead; 232 int write_cache; 233 int unmappedio; 234 int rotating; 235 #ifdef ADA_TEST_FAILURE 236 int force_read_error; 237 int force_write_error; 238 int periodic_read_error; 239 int periodic_read_count; 240 #endif 241 struct disk_params params; 242 struct disk *disk; 243 struct task sysctl_task; 244 struct sysctl_ctx_list sysctl_ctx; 245 struct sysctl_oid *sysctl_tree; 246 struct callout sendordered_c; 247 struct trim_request trim_req; 248 #ifdef CAM_IO_STATS 249 struct sysctl_ctx_list sysctl_stats_ctx; 250 struct sysctl_oid *sysctl_stats_tree; 251 u_int timeouts; 252 u_int errors; 253 u_int invalidations; 254 #endif 255 }; 256 257 struct ada_quirk_entry { 258 struct scsi_inquiry_pattern inq_pat; 259 ada_quirks quirks; 260 }; 261 262 static struct ada_quirk_entry ada_quirk_table[] = 263 { 264 { 265 /* Hitachi Advanced Format (4k) drives */ 266 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" }, 267 /*quirks*/ADA_Q_4K 268 }, 269 { 270 /* Samsung Advanced Format (4k) drives */ 271 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD155UI*", "*" }, 272 /*quirks*/ADA_Q_4K 273 }, 274 { 275 /* Samsung Advanced Format (4k) drives */ 276 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" }, 277 /*quirks*/ADA_Q_4K 278 }, 279 { 280 /* Seagate Barracuda Green Advanced Format (4k) drives */ 281 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" }, 282 /*quirks*/ADA_Q_4K 283 }, 284 { 285 /* Seagate Barracuda Advanced Format (4k) drives */ 286 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???DM*", "*" }, 287 /*quirks*/ADA_Q_4K 288 }, 289 { 290 /* Seagate Barracuda Advanced Format (4k) drives */ 291 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DM*", "*" }, 292 /*quirks*/ADA_Q_4K 293 }, 294 { 295 /* Seagate Momentus Advanced Format (4k) drives */ 296 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" }, 297 /*quirks*/ADA_Q_4K 298 }, 299 { 300 /* Seagate Momentus Advanced Format (4k) drives */ 301 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" }, 302 /*quirks*/ADA_Q_4K 303 }, 304 { 305 /* Seagate Momentus Advanced Format (4k) drives */ 306 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640423AS*", "*" }, 307 /*quirks*/ADA_Q_4K 308 }, 309 { 310 /* Seagate Momentus Advanced Format (4k) drives */ 311 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640424AS*", "*" }, 312 /*quirks*/ADA_Q_4K 313 }, 314 { 315 /* Seagate Momentus Advanced Format (4k) drives */ 316 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" }, 317 /*quirks*/ADA_Q_4K 318 }, 319 { 320 /* Seagate Momentus Advanced Format (4k) drives */ 321 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" }, 322 /*quirks*/ADA_Q_4K 323 }, 324 { 325 /* Seagate Momentus Advanced Format (4k) drives */ 326 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750423AS*", "*" }, 327 /*quirks*/ADA_Q_4K 328 }, 329 { 330 /* Seagate Momentus Thin Advanced Format (4k) drives */ 331 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" }, 332 /*quirks*/ADA_Q_4K 333 }, 334 { 335 /* WDC Caviar Red Advanced Format (4k) drives */ 336 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????CX*", "*" }, 337 /*quirks*/ADA_Q_4K 338 }, 339 { 340 /* WDC Caviar Green Advanced Format (4k) drives */ 341 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" }, 342 /*quirks*/ADA_Q_4K 343 }, 344 { 345 /* WDC Caviar Green/Red Advanced Format (4k) drives */ 346 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" }, 347 /*quirks*/ADA_Q_4K 348 }, 349 { 350 /* WDC Caviar Red Advanced Format (4k) drives */ 351 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????CX*", "*" }, 352 /*quirks*/ADA_Q_4K 353 }, 354 { 355 /* WDC Caviar Black Advanced Format (4k) drives */ 356 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????EX*", "*" }, 357 /*quirks*/ADA_Q_4K 358 }, 359 { 360 /* WDC Caviar Green Advanced Format (4k) drives */ 361 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" }, 362 /*quirks*/ADA_Q_4K 363 }, 364 { 365 /* WDC Caviar Green Advanced Format (4k) drives */ 366 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" }, 367 /*quirks*/ADA_Q_4K 368 }, 369 { 370 /* WDC Scorpio Black Advanced Format (4k) drives */ 371 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" }, 372 /*quirks*/ADA_Q_4K 373 }, 374 { 375 /* WDC Scorpio Black Advanced Format (4k) drives */ 376 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" }, 377 /*quirks*/ADA_Q_4K 378 }, 379 { 380 /* WDC Scorpio Blue Advanced Format (4k) drives */ 381 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" }, 382 /*quirks*/ADA_Q_4K 383 }, 384 { 385 /* WDC Scorpio Blue Advanced Format (4k) drives */ 386 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" }, 387 /*quirks*/ADA_Q_4K 388 }, 389 /* SSDs */ 390 { 391 /* 392 * Corsair Force 2 SSDs 393 * 4k optimised & trim only works in 4k requests + 4k aligned 394 */ 395 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair CSSD-F*", "*" }, 396 /*quirks*/ADA_Q_4K 397 }, 398 { 399 /* 400 * Corsair Force 3 SSDs 401 * 4k optimised & trim only works in 4k requests + 4k aligned 402 */ 403 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force 3*", "*" }, 404 /*quirks*/ADA_Q_4K 405 }, 406 { 407 /* 408 * Corsair Neutron GTX SSDs 409 * 4k optimised & trim only works in 4k requests + 4k aligned 410 */ 411 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" }, 412 /*quirks*/ADA_Q_4K 413 }, 414 { 415 /* 416 * Corsair Force GT & GS SSDs 417 * 4k optimised & trim only works in 4k requests + 4k aligned 418 */ 419 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force G*", "*" }, 420 /*quirks*/ADA_Q_4K 421 }, 422 { 423 /* 424 * Crucial M4 SSDs 425 * 4k optimised & trim only works in 4k requests + 4k aligned 426 */ 427 { T_DIRECT, SIP_MEDIA_FIXED, "*", "M4-CT???M4SSD2*", "*" }, 428 /*quirks*/ADA_Q_4K 429 }, 430 { 431 /* 432 * Crucial M500 SSDs MU07 firmware 433 * NCQ Trim works 434 */ 435 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*M500*", "MU07" }, 436 /*quirks*/0 437 }, 438 { 439 /* 440 * Crucial M500 SSDs all other firmware 441 * NCQ Trim doesn't work 442 */ 443 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*M500*", "*" }, 444 /*quirks*/ADA_Q_NCQ_TRIM_BROKEN 445 }, 446 { 447 /* 448 * Crucial M550 SSDs 449 * NCQ Trim doesn't work, but only on MU01 firmware 450 */ 451 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*M550*", "MU01" }, 452 /*quirks*/ADA_Q_NCQ_TRIM_BROKEN 453 }, 454 { 455 /* 456 * Crucial MX100 SSDs 457 * NCQ Trim doesn't work, but only on MU01 firmware 458 */ 459 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*MX100*", "MU01" }, 460 /*quirks*/ADA_Q_NCQ_TRIM_BROKEN 461 }, 462 { 463 /* 464 * Crucial RealSSD C300 SSDs 465 * 4k optimised 466 */ 467 { T_DIRECT, SIP_MEDIA_FIXED, "*", "C300-CTFDDAC???MAG*", 468 "*" }, /*quirks*/ADA_Q_4K 469 }, 470 { 471 /* 472 * FCCT M500 SSDs 473 * NCQ Trim doesn't work 474 */ 475 { T_DIRECT, SIP_MEDIA_FIXED, "*", "FCCT*M500*", "*" }, 476 /*quirks*/ADA_Q_NCQ_TRIM_BROKEN 477 }, 478 { 479 /* 480 * Intel 320 Series SSDs 481 * 4k optimised & trim only works in 4k requests + 4k aligned 482 */ 483 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2CW*", "*" }, 484 /*quirks*/ADA_Q_4K 485 }, 486 { 487 /* 488 * Intel 330 Series SSDs 489 * 4k optimised & trim only works in 4k requests + 4k aligned 490 */ 491 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2CT*", "*" }, 492 /*quirks*/ADA_Q_4K 493 }, 494 { 495 /* 496 * Intel 510 Series SSDs 497 * 4k optimised & trim only works in 4k requests + 4k aligned 498 */ 499 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2MH*", "*" }, 500 /*quirks*/ADA_Q_4K 501 }, 502 { 503 /* 504 * Intel 520 Series SSDs 505 * 4k optimised & trim only works in 4k requests + 4k aligned 506 */ 507 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2BW*", "*" }, 508 /*quirks*/ADA_Q_4K 509 }, 510 { 511 /* 512 * Intel X25-M Series SSDs 513 * 4k optimised & trim only works in 4k requests + 4k aligned 514 */ 515 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2M*", "*" }, 516 /*quirks*/ADA_Q_4K 517 }, 518 { 519 /* 520 * Kingston E100 Series SSDs 521 * 4k optimised & trim only works in 4k requests + 4k aligned 522 */ 523 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SE100S3*", "*" }, 524 /*quirks*/ADA_Q_4K 525 }, 526 { 527 /* 528 * Kingston HyperX 3k SSDs 529 * 4k optimised & trim only works in 4k requests + 4k aligned 530 */ 531 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SH103S3*", "*" }, 532 /*quirks*/ADA_Q_4K 533 }, 534 { 535 /* 536 * Marvell SSDs (entry taken from OpenSolaris) 537 * 4k optimised & trim only works in 4k requests + 4k aligned 538 */ 539 { T_DIRECT, SIP_MEDIA_FIXED, "*", "MARVELL SD88SA02*", "*" }, 540 /*quirks*/ADA_Q_4K 541 }, 542 { 543 /* 544 * Micron M500 SSDs firmware MU07 545 * NCQ Trim works? 546 */ 547 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron M500*", "MU07" }, 548 /*quirks*/0 549 }, 550 { 551 /* 552 * Micron M500 SSDs all other firmware 553 * NCQ Trim doesn't work 554 */ 555 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron M500*", "*" }, 556 /*quirks*/ADA_Q_NCQ_TRIM_BROKEN 557 }, 558 { 559 /* 560 * Micron M5[15]0 SSDs 561 * NCQ Trim doesn't work, but only MU01 firmware 562 */ 563 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron M5[15]0*", "MU01" }, 564 /*quirks*/ADA_Q_NCQ_TRIM_BROKEN 565 }, 566 { 567 /* 568 * OCZ Agility 2 SSDs 569 * 4k optimised & trim only works in 4k requests + 4k aligned 570 */ 571 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" }, 572 /*quirks*/ADA_Q_4K 573 }, 574 { 575 /* 576 * OCZ Agility 3 SSDs 577 * 4k optimised & trim only works in 4k requests + 4k aligned 578 */ 579 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY3*", "*" }, 580 /*quirks*/ADA_Q_4K 581 }, 582 { 583 /* 584 * OCZ Deneva R Series SSDs 585 * 4k optimised & trim only works in 4k requests + 4k aligned 586 */ 587 { T_DIRECT, SIP_MEDIA_FIXED, "*", "DENRSTE251M45*", "*" }, 588 /*quirks*/ADA_Q_4K 589 }, 590 { 591 /* 592 * OCZ Vertex 2 SSDs (inc pro series) 593 * 4k optimised & trim only works in 4k requests + 4k aligned 594 */ 595 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ?VERTEX2*", "*" }, 596 /*quirks*/ADA_Q_4K 597 }, 598 { 599 /* 600 * OCZ Vertex 3 SSDs 601 * 4k optimised & trim only works in 4k requests + 4k aligned 602 */ 603 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX3*", "*" }, 604 /*quirks*/ADA_Q_4K 605 }, 606 { 607 /* 608 * OCZ Vertex 4 SSDs 609 * 4k optimised & trim only works in 4k requests + 4k aligned 610 */ 611 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX4*", "*" }, 612 /*quirks*/ADA_Q_4K 613 }, 614 { 615 /* 616 * Samsung 830 Series SSDs 617 * 4k optimised, NCQ TRIM Broken (normal TRIM is fine) 618 */ 619 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG SSD 830 Series*", "*" }, 620 /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN 621 }, 622 { 623 /* 624 * Samsung 840 SSDs 625 * 4k optimised, NCQ TRIM Broken (normal TRIM is fine) 626 */ 627 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 840*", "*" }, 628 /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN 629 }, 630 { 631 /* 632 * Samsung 850 SSDs 633 * 4k optimised, NCQ TRIM broken (normal TRIM fine) 634 */ 635 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 850*", "*" }, 636 /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN 637 }, 638 { 639 /* 640 * Samsung SM863 Series SSDs (MZ7KM*) 641 * 4k optimised, NCQ believed to be working 642 */ 643 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7KM*", "*" }, 644 /*quirks*/ADA_Q_4K 645 }, 646 { 647 /* 648 * Samsung 843T Series SSDs (MZ7WD*) 649 * Samsung PM851 Series SSDs (MZ7TE*) 650 * Samsung PM853T Series SSDs (MZ7GE*) 651 * 4k optimised, NCQ believed to be broken since these are 652 * appear to be built with the same controllers as the 840/850. 653 */ 654 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7*", "*" }, 655 /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN 656 }, 657 { 658 /* 659 * Samsung PM851 Series SSDs Dell OEM 660 * device model "SAMSUNG SSD PM851 mSATA 256GB" 661 * 4k optimised, NCQ broken 662 */ 663 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG SSD PM851*", "*" }, 664 /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN 665 }, 666 { 667 /* 668 * SuperTalent TeraDrive CT SSDs 669 * 4k optimised & trim only works in 4k requests + 4k aligned 670 */ 671 { T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" }, 672 /*quirks*/ADA_Q_4K 673 }, 674 { 675 /* 676 * XceedIOPS SATA SSDs 677 * 4k optimised 678 */ 679 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" }, 680 /*quirks*/ADA_Q_4K 681 }, 682 { 683 /* Default */ 684 { 685 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, 686 /*vendor*/"*", /*product*/"*", /*revision*/"*" 687 }, 688 /*quirks*/0 689 }, 690 }; 691 692 static disk_strategy_t adastrategy; 693 static dumper_t adadump; 694 static periph_init_t adainit; 695 static void adadiskgonecb(struct disk *dp); 696 static periph_oninv_t adaoninvalidate; 697 static periph_dtor_t adacleanup; 698 static void adaasync(void *callback_arg, u_int32_t code, 699 struct cam_path *path, void *arg); 700 static int adazonemodesysctl(SYSCTL_HANDLER_ARGS); 701 static int adazonesupsysctl(SYSCTL_HANDLER_ARGS); 702 static void adasysctlinit(void *context, int pending); 703 static int adagetattr(struct bio *bp); 704 static void adasetflags(struct ada_softc *softc, 705 struct ccb_getdev *cgd); 706 static periph_ctor_t adaregister; 707 static void ada_dsmtrim(struct ada_softc *softc, struct bio *bp, 708 struct ccb_ataio *ataio); 709 static void ada_cfaerase(struct ada_softc *softc, struct bio *bp, 710 struct ccb_ataio *ataio); 711 static int ada_zone_bio_to_ata(int disk_zone_cmd); 712 static int ada_zone_cmd(struct cam_periph *periph, union ccb *ccb, 713 struct bio *bp, int *queue_ccb); 714 static periph_start_t adastart; 715 static void adaprobedone(struct cam_periph *periph, union ccb *ccb); 716 static void adazonedone(struct cam_periph *periph, union ccb *ccb); 717 static void adadone(struct cam_periph *periph, 718 union ccb *done_ccb); 719 static int adaerror(union ccb *ccb, u_int32_t cam_flags, 720 u_int32_t sense_flags); 721 static void adagetparams(struct cam_periph *periph, 722 struct ccb_getdev *cgd); 723 static timeout_t adasendorderedtag; 724 static void adashutdown(void *arg, int howto); 725 static void adasuspend(void *arg); 726 static void adaresume(void *arg); 727 728 #ifndef ADA_DEFAULT_LEGACY_ALIASES 729 #define ADA_DEFAULT_LEGACY_ALIASES 1 730 #endif 731 732 #ifndef ADA_DEFAULT_TIMEOUT 733 #define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */ 734 #endif 735 736 #ifndef ADA_DEFAULT_RETRY 737 #define ADA_DEFAULT_RETRY 4 738 #endif 739 740 #ifndef ADA_DEFAULT_SEND_ORDERED 741 #define ADA_DEFAULT_SEND_ORDERED 1 742 #endif 743 744 #ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN 745 #define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1 746 #endif 747 748 #ifndef ADA_DEFAULT_SPINDOWN_SUSPEND 749 #define ADA_DEFAULT_SPINDOWN_SUSPEND 1 750 #endif 751 752 #ifndef ADA_DEFAULT_READ_AHEAD 753 #define ADA_DEFAULT_READ_AHEAD 1 754 #endif 755 756 #ifndef ADA_DEFAULT_WRITE_CACHE 757 #define ADA_DEFAULT_WRITE_CACHE 1 758 #endif 759 760 #define ADA_RA (softc->read_ahead >= 0 ? \ 761 softc->read_ahead : ada_read_ahead) 762 #define ADA_WC (softc->write_cache >= 0 ? \ 763 softc->write_cache : ada_write_cache) 764 765 /* 766 * Most platforms map firmware geometry to actual, but some don't. If 767 * not overridden, default to nothing. 768 */ 769 #ifndef ata_disk_firmware_geom_adjust 770 #define ata_disk_firmware_geom_adjust(disk) 771 #endif 772 773 static int ada_retry_count = ADA_DEFAULT_RETRY; 774 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT; 775 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED; 776 static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN; 777 static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND; 778 static int ada_read_ahead = ADA_DEFAULT_READ_AHEAD; 779 static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE; 780 781 static SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0, 782 "CAM Direct Access Disk driver"); 783 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RWTUN, 784 &ada_retry_count, 0, "Normal I/O retry count"); 785 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RWTUN, 786 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)"); 787 SYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RWTUN, 788 &ada_send_ordered, 0, "Send Ordered Tags"); 789 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RWTUN, 790 &ada_spindown_shutdown, 0, "Spin down upon shutdown"); 791 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RWTUN, 792 &ada_spindown_suspend, 0, "Spin down upon suspend"); 793 SYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RWTUN, 794 &ada_read_ahead, 0, "Enable disk read-ahead"); 795 SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RWTUN, 796 &ada_write_cache, 0, "Enable disk write cache"); 797 798 /* 799 * ADA_ORDEREDTAG_INTERVAL determines how often, relative 800 * to the default timeout, we check to see whether an ordered 801 * tagged transaction is appropriate to prevent simple tag 802 * starvation. Since we'd like to ensure that there is at least 803 * 1/2 of the timeout length left for a starved transaction to 804 * complete after we've sent an ordered tag, we must poll at least 805 * four times in every timeout period. This takes care of the worst 806 * case where a starved transaction starts during an interval that 807 * meets the requirement "don't send an ordered tag" test so it takes 808 * us two intervals to determine that a tag must be sent. 809 */ 810 #ifndef ADA_ORDEREDTAG_INTERVAL 811 #define ADA_ORDEREDTAG_INTERVAL 4 812 #endif 813 814 static struct periph_driver adadriver = 815 { 816 adainit, "ada", 817 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0 818 }; 819 820 static int adadeletemethodsysctl(SYSCTL_HANDLER_ARGS); 821 822 PERIPHDRIVER_DECLARE(ada, adadriver); 823 824 static MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers"); 825 826 static int 827 adaopen(struct disk *dp) 828 { 829 struct cam_periph *periph; 830 struct ada_softc *softc; 831 int error; 832 833 periph = (struct cam_periph *)dp->d_drv1; 834 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 835 return(ENXIO); 836 } 837 838 cam_periph_lock(periph); 839 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) { 840 cam_periph_unlock(periph); 841 cam_periph_release(periph); 842 return (error); 843 } 844 845 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 846 ("adaopen\n")); 847 848 softc = (struct ada_softc *)periph->softc; 849 softc->flags |= ADA_FLAG_OPEN; 850 851 cam_periph_unhold(periph); 852 cam_periph_unlock(periph); 853 return (0); 854 } 855 856 static int 857 adaclose(struct disk *dp) 858 { 859 struct cam_periph *periph; 860 struct ada_softc *softc; 861 union ccb *ccb; 862 int error; 863 864 periph = (struct cam_periph *)dp->d_drv1; 865 softc = (struct ada_softc *)periph->softc; 866 cam_periph_lock(periph); 867 868 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 869 ("adaclose\n")); 870 871 /* We only sync the cache if the drive is capable of it. */ 872 if ((softc->flags & ADA_FLAG_DIRTY) != 0 && 873 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 && 874 (periph->flags & CAM_PERIPH_INVALID) == 0 && 875 cam_periph_hold(periph, PRIBIO) == 0) { 876 877 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 878 cam_fill_ataio(&ccb->ataio, 879 1, 880 adadone, 881 CAM_DIR_NONE, 882 0, 883 NULL, 884 0, 885 ada_default_timeout*1000); 886 887 if (softc->flags & ADA_FLAG_CAN_48BIT) 888 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 889 else 890 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 891 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 892 /*sense_flags*/0, softc->disk->d_devstat); 893 894 if (error != 0) 895 xpt_print(periph->path, "Synchronize cache failed\n"); 896 else 897 softc->flags &= ~ADA_FLAG_DIRTY; 898 xpt_release_ccb(ccb); 899 cam_periph_unhold(periph); 900 } 901 902 softc->flags &= ~ADA_FLAG_OPEN; 903 904 while (softc->refcount != 0) 905 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "adaclose", 1); 906 cam_periph_unlock(periph); 907 cam_periph_release(periph); 908 return (0); 909 } 910 911 static void 912 adaschedule(struct cam_periph *periph) 913 { 914 struct ada_softc *softc = (struct ada_softc *)periph->softc; 915 916 if (softc->state != ADA_STATE_NORMAL) 917 return; 918 919 cam_iosched_schedule(softc->cam_iosched, periph); 920 } 921 922 /* 923 * Actually translate the requested transfer into one the physical driver 924 * can understand. The transfer is described by a buf and will include 925 * only one physical transfer. 926 */ 927 static void 928 adastrategy(struct bio *bp) 929 { 930 struct cam_periph *periph; 931 struct ada_softc *softc; 932 933 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 934 softc = (struct ada_softc *)periph->softc; 935 936 cam_periph_lock(periph); 937 938 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp)); 939 940 /* 941 * If the device has been made invalid, error out 942 */ 943 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 944 cam_periph_unlock(periph); 945 biofinish(bp, NULL, ENXIO); 946 return; 947 } 948 949 /* 950 * Zone commands must be ordered, because they can depend on the 951 * effects of previously issued commands, and they may affect 952 * commands after them. 953 */ 954 if (bp->bio_cmd == BIO_ZONE) 955 bp->bio_flags |= BIO_ORDERED; 956 957 /* 958 * Place it in the queue of disk activities for this disk 959 */ 960 cam_iosched_queue_work(softc->cam_iosched, bp); 961 962 /* 963 * Schedule ourselves for performing the work. 964 */ 965 adaschedule(periph); 966 cam_periph_unlock(periph); 967 968 return; 969 } 970 971 static int 972 adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) 973 { 974 struct cam_periph *periph; 975 struct ada_softc *softc; 976 u_int secsize; 977 union ccb ccb; 978 struct disk *dp; 979 uint64_t lba; 980 uint16_t count; 981 int error = 0; 982 983 dp = arg; 984 periph = dp->d_drv1; 985 softc = (struct ada_softc *)periph->softc; 986 cam_periph_lock(periph); 987 secsize = softc->params.secsize; 988 lba = offset / secsize; 989 count = length / secsize; 990 991 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 992 cam_periph_unlock(periph); 993 return (ENXIO); 994 } 995 996 if (length > 0) { 997 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 998 ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 999 cam_fill_ataio(&ccb.ataio, 1000 0, 1001 adadone, 1002 CAM_DIR_OUT, 1003 0, 1004 (u_int8_t *) virtual, 1005 length, 1006 ada_default_timeout*1000); 1007 if ((softc->flags & ADA_FLAG_CAN_48BIT) && 1008 (lba + count >= ATA_MAX_28BIT_LBA || 1009 count >= 256)) { 1010 ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48, 1011 0, lba, count); 1012 } else { 1013 ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA, 1014 0, lba, count); 1015 } 1016 xpt_polled_action(&ccb); 1017 1018 error = cam_periph_error(&ccb, 1019 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 1020 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 1021 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0, 1022 /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 1023 if (error != 0) 1024 printf("Aborting dump due to I/O error.\n"); 1025 1026 cam_periph_unlock(periph); 1027 return (error); 1028 } 1029 1030 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) { 1031 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1032 1033 /* 1034 * Tell the drive to flush its internal cache. if we 1035 * can't flush in 5s we have big problems. No need to 1036 * wait the default 60s to detect problems. 1037 */ 1038 ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 1039 cam_fill_ataio(&ccb.ataio, 1040 0, 1041 adadone, 1042 CAM_DIR_NONE, 1043 0, 1044 NULL, 1045 0, 1046 5*1000); 1047 1048 if (softc->flags & ADA_FLAG_CAN_48BIT) 1049 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1050 else 1051 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0); 1052 xpt_polled_action(&ccb); 1053 1054 error = cam_periph_error(&ccb, 1055 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 1056 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 1057 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0, 1058 /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 1059 if (error != 0) 1060 xpt_print(periph->path, "Synchronize cache failed\n"); 1061 } 1062 cam_periph_unlock(periph); 1063 return (error); 1064 } 1065 1066 static void 1067 adainit(void) 1068 { 1069 cam_status status; 1070 1071 /* 1072 * Install a global async callback. This callback will 1073 * receive async callbacks like "new device found". 1074 */ 1075 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL); 1076 1077 if (status != CAM_REQ_CMP) { 1078 printf("ada: Failed to attach master async callback " 1079 "due to status 0x%x!\n", status); 1080 } else if (ada_send_ordered) { 1081 1082 /* Register our event handlers */ 1083 if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend, 1084 NULL, EVENTHANDLER_PRI_LAST)) == NULL) 1085 printf("adainit: power event registration failed!\n"); 1086 if ((EVENTHANDLER_REGISTER(power_resume, adaresume, 1087 NULL, EVENTHANDLER_PRI_LAST)) == NULL) 1088 printf("adainit: power event registration failed!\n"); 1089 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown, 1090 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 1091 printf("adainit: shutdown event registration failed!\n"); 1092 } 1093 } 1094 1095 /* 1096 * Callback from GEOM, called when it has finished cleaning up its 1097 * resources. 1098 */ 1099 static void 1100 adadiskgonecb(struct disk *dp) 1101 { 1102 struct cam_periph *periph; 1103 1104 periph = (struct cam_periph *)dp->d_drv1; 1105 1106 cam_periph_release(periph); 1107 } 1108 1109 static void 1110 adaoninvalidate(struct cam_periph *periph) 1111 { 1112 struct ada_softc *softc; 1113 1114 softc = (struct ada_softc *)periph->softc; 1115 1116 /* 1117 * De-register any async callbacks. 1118 */ 1119 xpt_register_async(0, adaasync, periph, periph->path); 1120 #ifdef CAM_IO_STATS 1121 softc->invalidations++; 1122 #endif 1123 1124 /* 1125 * Return all queued I/O with ENXIO. 1126 * XXX Handle any transactions queued to the card 1127 * with XPT_ABORT_CCB. 1128 */ 1129 cam_iosched_flush(softc->cam_iosched, NULL, ENXIO); 1130 1131 disk_gone(softc->disk); 1132 } 1133 1134 static void 1135 adacleanup(struct cam_periph *periph) 1136 { 1137 struct ada_softc *softc; 1138 1139 softc = (struct ada_softc *)periph->softc; 1140 1141 cam_periph_unlock(periph); 1142 1143 cam_iosched_fini(softc->cam_iosched); 1144 1145 /* 1146 * If we can't free the sysctl tree, oh well... 1147 */ 1148 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0) { 1149 #ifdef CAM_IO_STATS 1150 if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0) 1151 xpt_print(periph->path, 1152 "can't remove sysctl stats context\n"); 1153 #endif 1154 if (sysctl_ctx_free(&softc->sysctl_ctx) != 0) 1155 xpt_print(periph->path, 1156 "can't remove sysctl context\n"); 1157 } 1158 1159 disk_destroy(softc->disk); 1160 callout_drain(&softc->sendordered_c); 1161 free(softc, M_DEVBUF); 1162 cam_periph_lock(periph); 1163 } 1164 1165 static void 1166 adasetdeletemethod(struct ada_softc *softc) 1167 { 1168 1169 if (softc->flags & ADA_FLAG_CAN_NCQ_TRIM) 1170 softc->delete_method = ADA_DELETE_NCQ_DSM_TRIM; 1171 else if (softc->flags & ADA_FLAG_CAN_TRIM) 1172 softc->delete_method = ADA_DELETE_DSM_TRIM; 1173 else if ((softc->flags & ADA_FLAG_CAN_CFA) && !(softc->flags & ADA_FLAG_CAN_48BIT)) 1174 softc->delete_method = ADA_DELETE_CFA_ERASE; 1175 else 1176 softc->delete_method = ADA_DELETE_NONE; 1177 } 1178 1179 static void 1180 adaasync(void *callback_arg, u_int32_t code, 1181 struct cam_path *path, void *arg) 1182 { 1183 struct ccb_getdev cgd; 1184 struct cam_periph *periph; 1185 struct ada_softc *softc; 1186 1187 periph = (struct cam_periph *)callback_arg; 1188 switch (code) { 1189 case AC_FOUND_DEVICE: 1190 { 1191 struct ccb_getdev *cgd; 1192 cam_status status; 1193 1194 cgd = (struct ccb_getdev *)arg; 1195 if (cgd == NULL) 1196 break; 1197 1198 if (cgd->protocol != PROTO_ATA) 1199 break; 1200 1201 /* 1202 * Allocate a peripheral instance for 1203 * this device and start the probe 1204 * process. 1205 */ 1206 status = cam_periph_alloc(adaregister, adaoninvalidate, 1207 adacleanup, adastart, 1208 "ada", CAM_PERIPH_BIO, 1209 path, adaasync, 1210 AC_FOUND_DEVICE, cgd); 1211 1212 if (status != CAM_REQ_CMP 1213 && status != CAM_REQ_INPROG) 1214 printf("adaasync: Unable to attach to new device " 1215 "due to status 0x%x\n", status); 1216 break; 1217 } 1218 case AC_GETDEV_CHANGED: 1219 { 1220 softc = (struct ada_softc *)periph->softc; 1221 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1222 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1223 xpt_action((union ccb *)&cgd); 1224 1225 /* 1226 * Set/clear support flags based on the new Identify data. 1227 */ 1228 adasetflags(softc, &cgd); 1229 1230 cam_periph_async(periph, code, path, arg); 1231 break; 1232 } 1233 case AC_ADVINFO_CHANGED: 1234 { 1235 uintptr_t buftype; 1236 1237 buftype = (uintptr_t)arg; 1238 if (buftype == CDAI_TYPE_PHYS_PATH) { 1239 struct ada_softc *softc; 1240 1241 softc = periph->softc; 1242 disk_attr_changed(softc->disk, "GEOM::physpath", 1243 M_NOWAIT); 1244 } 1245 break; 1246 } 1247 case AC_SENT_BDR: 1248 case AC_BUS_RESET: 1249 { 1250 softc = (struct ada_softc *)periph->softc; 1251 cam_periph_async(periph, code, path, arg); 1252 if (softc->state != ADA_STATE_NORMAL) 1253 break; 1254 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1255 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1256 xpt_action((union ccb *)&cgd); 1257 if (ADA_RA >= 0 && softc->flags & ADA_FLAG_CAN_RAHEAD) 1258 softc->state = ADA_STATE_RAHEAD; 1259 else if (ADA_WC >= 0 && softc->flags & ADA_FLAG_CAN_RAHEAD) 1260 softc->state = ADA_STATE_WCACHE; 1261 else if (softc->flags & ADA_FLAG_CAN_LOG) 1262 softc->state = ADA_STATE_LOGDIR; 1263 else 1264 break; 1265 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 1266 softc->state = ADA_STATE_NORMAL; 1267 else 1268 xpt_schedule(periph, CAM_PRIORITY_DEV); 1269 } 1270 default: 1271 cam_periph_async(periph, code, path, arg); 1272 break; 1273 } 1274 } 1275 1276 static int 1277 adazonemodesysctl(SYSCTL_HANDLER_ARGS) 1278 { 1279 char tmpbuf[40]; 1280 struct ada_softc *softc; 1281 int error; 1282 1283 softc = (struct ada_softc *)arg1; 1284 1285 switch (softc->zone_mode) { 1286 case ADA_ZONE_DRIVE_MANAGED: 1287 snprintf(tmpbuf, sizeof(tmpbuf), "Drive Managed"); 1288 break; 1289 case ADA_ZONE_HOST_AWARE: 1290 snprintf(tmpbuf, sizeof(tmpbuf), "Host Aware"); 1291 break; 1292 case ADA_ZONE_HOST_MANAGED: 1293 snprintf(tmpbuf, sizeof(tmpbuf), "Host Managed"); 1294 break; 1295 case ADA_ZONE_NONE: 1296 default: 1297 snprintf(tmpbuf, sizeof(tmpbuf), "Not Zoned"); 1298 break; 1299 } 1300 1301 error = sysctl_handle_string(oidp, tmpbuf, sizeof(tmpbuf), req); 1302 1303 return (error); 1304 } 1305 1306 static int 1307 adazonesupsysctl(SYSCTL_HANDLER_ARGS) 1308 { 1309 char tmpbuf[180]; 1310 struct ada_softc *softc; 1311 struct sbuf sb; 1312 int error, first; 1313 unsigned int i; 1314 1315 softc = (struct ada_softc *)arg1; 1316 1317 error = 0; 1318 first = 1; 1319 sbuf_new(&sb, tmpbuf, sizeof(tmpbuf), 0); 1320 1321 for (i = 0; i < sizeof(ada_zone_desc_table) / 1322 sizeof(ada_zone_desc_table[0]); i++) { 1323 if (softc->zone_flags & ada_zone_desc_table[i].value) { 1324 if (first == 0) 1325 sbuf_printf(&sb, ", "); 1326 else 1327 first = 0; 1328 sbuf_cat(&sb, ada_zone_desc_table[i].desc); 1329 } 1330 } 1331 1332 if (first == 1) 1333 sbuf_printf(&sb, "None"); 1334 1335 sbuf_finish(&sb); 1336 1337 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 1338 1339 return (error); 1340 } 1341 1342 1343 static void 1344 adasysctlinit(void *context, int pending) 1345 { 1346 struct cam_periph *periph; 1347 struct ada_softc *softc; 1348 char tmpstr[80], tmpstr2[80]; 1349 1350 periph = (struct cam_periph *)context; 1351 1352 /* periph was held for us when this task was enqueued */ 1353 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 1354 cam_periph_release(periph); 1355 return; 1356 } 1357 1358 softc = (struct ada_softc *)periph->softc; 1359 snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d",periph->unit_number); 1360 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 1361 1362 sysctl_ctx_init(&softc->sysctl_ctx); 1363 softc->flags |= ADA_FLAG_SCTX_INIT; 1364 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1365 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2, 1366 CTLFLAG_RD, 0, tmpstr); 1367 if (softc->sysctl_tree == NULL) { 1368 printf("adasysctlinit: unable to allocate sysctl tree\n"); 1369 cam_periph_release(periph); 1370 return; 1371 } 1372 1373 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1374 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW, 1375 softc, 0, adadeletemethodsysctl, "A", 1376 "BIO_DELETE execution method"); 1377 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1378 OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE, 1379 &softc->read_ahead, 0, "Enable disk read ahead."); 1380 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1381 OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE, 1382 &softc->write_cache, 0, "Enable disk write cache."); 1383 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1384 OID_AUTO, "unmapped_io", CTLFLAG_RD | CTLFLAG_MPSAFE, 1385 &softc->unmappedio, 0, "Unmapped I/O leaf"); 1386 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1387 OID_AUTO, "rotating", CTLFLAG_RD | CTLFLAG_MPSAFE, 1388 &softc->rotating, 0, "Rotating media"); 1389 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1390 OID_AUTO, "zone_mode", CTLTYPE_STRING | CTLFLAG_RD, 1391 softc, 0, adazonemodesysctl, "A", 1392 "Zone Mode"); 1393 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1394 OID_AUTO, "zone_support", CTLTYPE_STRING | CTLFLAG_RD, 1395 softc, 0, adazonesupsysctl, "A", 1396 "Zone Support"); 1397 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx, 1398 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1399 "optimal_seq_zones", CTLFLAG_RD, &softc->optimal_seq_zones, 1400 "Optimal Number of Open Sequential Write Preferred Zones"); 1401 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx, 1402 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1403 "optimal_nonseq_zones", CTLFLAG_RD, 1404 &softc->optimal_nonseq_zones, 1405 "Optimal Number of Non-Sequentially Written Sequential Write " 1406 "Preferred Zones"); 1407 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx, 1408 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1409 "max_seq_zones", CTLFLAG_RD, &softc->max_seq_zones, 1410 "Maximum Number of Open Sequential Write Required Zones"); 1411 1412 #ifdef ADA_TEST_FAILURE 1413 /* 1414 * Add a 'door bell' sysctl which allows one to set it from userland 1415 * and cause something bad to happen. For the moment, we only allow 1416 * whacking the next read or write. 1417 */ 1418 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1419 OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1420 &softc->force_read_error, 0, 1421 "Force a read error for the next N reads."); 1422 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1423 OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1424 &softc->force_write_error, 0, 1425 "Force a write error for the next N writes."); 1426 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1427 OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1428 &softc->periodic_read_error, 0, 1429 "Force a read error every N reads (don't set too low)."); 1430 #endif 1431 1432 #ifdef CAM_IO_STATS 1433 softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx, 1434 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats", 1435 CTLFLAG_RD, 0, "Statistics"); 1436 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx, 1437 SYSCTL_CHILDREN(softc->sysctl_stats_tree), 1438 OID_AUTO, "timeouts", CTLFLAG_RD | CTLFLAG_MPSAFE, 1439 &softc->timeouts, 0, 1440 "Device timeouts reported by the SIM"); 1441 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx, 1442 SYSCTL_CHILDREN(softc->sysctl_stats_tree), 1443 OID_AUTO, "errors", CTLFLAG_RD | CTLFLAG_MPSAFE, 1444 &softc->errors, 0, 1445 "Transport errors reported by the SIM."); 1446 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx, 1447 SYSCTL_CHILDREN(softc->sysctl_stats_tree), 1448 OID_AUTO, "pack_invalidations", CTLFLAG_RD | CTLFLAG_MPSAFE, 1449 &softc->invalidations, 0, 1450 "Device pack invalidations."); 1451 #endif 1452 1453 cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx, 1454 softc->sysctl_tree); 1455 1456 cam_periph_release(periph); 1457 } 1458 1459 static int 1460 adagetattr(struct bio *bp) 1461 { 1462 int ret; 1463 struct cam_periph *periph; 1464 1465 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 1466 cam_periph_lock(periph); 1467 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute, 1468 periph->path); 1469 cam_periph_unlock(periph); 1470 if (ret == 0) 1471 bp->bio_completed = bp->bio_length; 1472 return ret; 1473 } 1474 1475 static int 1476 adadeletemethodsysctl(SYSCTL_HANDLER_ARGS) 1477 { 1478 char buf[16]; 1479 const char *p; 1480 struct ada_softc *softc; 1481 int i, error, value, methods; 1482 1483 softc = (struct ada_softc *)arg1; 1484 1485 value = softc->delete_method; 1486 if (value < 0 || value > ADA_DELETE_MAX) 1487 p = "UNKNOWN"; 1488 else 1489 p = ada_delete_method_names[value]; 1490 strncpy(buf, p, sizeof(buf)); 1491 error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 1492 if (error != 0 || req->newptr == NULL) 1493 return (error); 1494 methods = 1 << ADA_DELETE_DISABLE; 1495 if ((softc->flags & ADA_FLAG_CAN_CFA) && 1496 !(softc->flags & ADA_FLAG_CAN_48BIT)) 1497 methods |= 1 << ADA_DELETE_CFA_ERASE; 1498 if (softc->flags & ADA_FLAG_CAN_TRIM) 1499 methods |= 1 << ADA_DELETE_DSM_TRIM; 1500 if (softc->flags & ADA_FLAG_CAN_NCQ_TRIM) 1501 methods |= 1 << ADA_DELETE_NCQ_DSM_TRIM; 1502 for (i = 0; i <= ADA_DELETE_MAX; i++) { 1503 if (!(methods & (1 << i)) || 1504 strcmp(buf, ada_delete_method_names[i]) != 0) 1505 continue; 1506 softc->delete_method = i; 1507 return (0); 1508 } 1509 return (EINVAL); 1510 } 1511 1512 static void 1513 adasetflags(struct ada_softc *softc, struct ccb_getdev *cgd) 1514 { 1515 if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) && 1516 (cgd->inq_flags & SID_DMA)) 1517 softc->flags |= ADA_FLAG_CAN_DMA; 1518 else 1519 softc->flags &= ~ADA_FLAG_CAN_DMA; 1520 1521 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) { 1522 softc->flags |= ADA_FLAG_CAN_48BIT; 1523 if (cgd->inq_flags & SID_DMA48) 1524 softc->flags |= ADA_FLAG_CAN_DMA48; 1525 else 1526 softc->flags &= ~ADA_FLAG_CAN_DMA48; 1527 } else 1528 softc->flags &= ~(ADA_FLAG_CAN_48BIT | ADA_FLAG_CAN_DMA48); 1529 1530 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE) 1531 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE; 1532 else 1533 softc->flags &= ~ADA_FLAG_CAN_FLUSHCACHE; 1534 1535 if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT) 1536 softc->flags |= ADA_FLAG_CAN_POWERMGT; 1537 else 1538 softc->flags &= ~ADA_FLAG_CAN_POWERMGT; 1539 1540 if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) && 1541 (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue)) 1542 softc->flags |= ADA_FLAG_CAN_NCQ; 1543 else 1544 softc->flags &= ~ADA_FLAG_CAN_NCQ; 1545 1546 if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) && 1547 (cgd->inq_flags & SID_DMA)) { 1548 softc->flags |= ADA_FLAG_CAN_TRIM; 1549 softc->trim_max_ranges = TRIM_MAX_RANGES; 1550 if (cgd->ident_data.max_dsm_blocks != 0) { 1551 softc->trim_max_ranges = 1552 min(cgd->ident_data.max_dsm_blocks * 1553 ATA_DSM_BLK_RANGES, softc->trim_max_ranges); 1554 } 1555 /* 1556 * If we can do RCVSND_FPDMA_QUEUED commands, we may be able 1557 * to do NCQ trims, if we support trims at all. We also need 1558 * support from the SIM to do things properly. Perhaps we 1559 * should look at log 13 dword 0 bit 0 and dword 1 bit 0 are 1560 * set too... 1561 */ 1562 if ((softc->quirks & ADA_Q_NCQ_TRIM_BROKEN) == 0 && 1563 (softc->flags & ADA_FLAG_PIM_ATA_EXT) != 0 && 1564 (cgd->ident_data.satacapabilities2 & 1565 ATA_SUPPORT_RCVSND_FPDMA_QUEUED) != 0 && 1566 (softc->flags & ADA_FLAG_CAN_TRIM) != 0) 1567 softc->flags |= ADA_FLAG_CAN_NCQ_TRIM; 1568 else 1569 softc->flags &= ~ADA_FLAG_CAN_NCQ_TRIM; 1570 } else 1571 softc->flags &= ~(ADA_FLAG_CAN_TRIM | ADA_FLAG_CAN_NCQ_TRIM); 1572 1573 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA) 1574 softc->flags |= ADA_FLAG_CAN_CFA; 1575 else 1576 softc->flags &= ~ADA_FLAG_CAN_CFA; 1577 1578 /* 1579 * Now that we've set the appropriate flags, setup the delete 1580 * method. 1581 */ 1582 adasetdeletemethod(softc); 1583 1584 if (cgd->ident_data.support.extension & ATA_SUPPORT_GENLOG) 1585 softc->flags |= ADA_FLAG_CAN_LOG; 1586 else 1587 softc->flags &= ~ADA_FLAG_CAN_LOG; 1588 1589 if ((cgd->ident_data.support3 & ATA_SUPPORT_ZONE_MASK) == 1590 ATA_SUPPORT_ZONE_HOST_AWARE) 1591 softc->zone_mode = ADA_ZONE_HOST_AWARE; 1592 else if ((cgd->ident_data.support3 & ATA_SUPPORT_ZONE_MASK) == 1593 ATA_SUPPORT_ZONE_DEV_MANAGED) 1594 softc->zone_mode = ADA_ZONE_DRIVE_MANAGED; 1595 else 1596 softc->zone_mode = ADA_ZONE_NONE; 1597 1598 if (cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) 1599 softc->flags |= ADA_FLAG_CAN_RAHEAD; 1600 else 1601 softc->flags &= ~ADA_FLAG_CAN_RAHEAD; 1602 1603 if (cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) 1604 softc->flags |= ADA_FLAG_CAN_WCACHE; 1605 else 1606 softc->flags &= ~ADA_FLAG_CAN_WCACHE; 1607 } 1608 1609 static cam_status 1610 adaregister(struct cam_periph *periph, void *arg) 1611 { 1612 struct ada_softc *softc; 1613 struct ccb_pathinq cpi; 1614 struct ccb_getdev *cgd; 1615 char announce_buf[80]; 1616 struct disk_params *dp; 1617 caddr_t match; 1618 u_int maxio; 1619 int quirks; 1620 1621 cgd = (struct ccb_getdev *)arg; 1622 if (cgd == NULL) { 1623 printf("adaregister: no getdev CCB, can't register device\n"); 1624 return(CAM_REQ_CMP_ERR); 1625 } 1626 1627 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF, 1628 M_NOWAIT|M_ZERO); 1629 1630 if (softc == NULL) { 1631 printf("adaregister: Unable to probe new device. " 1632 "Unable to allocate softc\n"); 1633 return(CAM_REQ_CMP_ERR); 1634 } 1635 1636 if (cam_iosched_init(&softc->cam_iosched, periph) != 0) { 1637 printf("adaregister: Unable to probe new device. " 1638 "Unable to allocate iosched memory\n"); 1639 return(CAM_REQ_CMP_ERR); 1640 } 1641 1642 periph->softc = softc; 1643 1644 /* 1645 * See if this device has any quirks. 1646 */ 1647 match = cam_quirkmatch((caddr_t)&cgd->ident_data, 1648 (caddr_t)ada_quirk_table, 1649 nitems(ada_quirk_table), 1650 sizeof(*ada_quirk_table), ata_identify_match); 1651 if (match != NULL) 1652 softc->quirks = ((struct ada_quirk_entry *)match)->quirks; 1653 else 1654 softc->quirks = ADA_Q_NONE; 1655 1656 bzero(&cpi, sizeof(cpi)); 1657 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE); 1658 cpi.ccb_h.func_code = XPT_PATH_INQ; 1659 xpt_action((union ccb *)&cpi); 1660 1661 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph); 1662 1663 /* 1664 * Register this media as a disk 1665 */ 1666 (void)cam_periph_hold(periph, PRIBIO); 1667 cam_periph_unlock(periph); 1668 snprintf(announce_buf, sizeof(announce_buf), 1669 "kern.cam.ada.%d.quirks", periph->unit_number); 1670 quirks = softc->quirks; 1671 TUNABLE_INT_FETCH(announce_buf, &quirks); 1672 softc->quirks = quirks; 1673 softc->read_ahead = -1; 1674 snprintf(announce_buf, sizeof(announce_buf), 1675 "kern.cam.ada.%d.read_ahead", periph->unit_number); 1676 TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead); 1677 softc->write_cache = -1; 1678 snprintf(announce_buf, sizeof(announce_buf), 1679 "kern.cam.ada.%d.write_cache", periph->unit_number); 1680 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache); 1681 1682 /* 1683 * Set support flags based on the Identify data and quirks. 1684 */ 1685 adasetflags(softc, cgd); 1686 1687 /* Disable queue sorting for non-rotational media by default. */ 1688 if (cgd->ident_data.media_rotation_rate == ATA_RATE_NON_ROTATING) { 1689 softc->rotating = 0; 1690 } else { 1691 softc->rotating = 1; 1692 } 1693 cam_iosched_set_sort_queue(softc->cam_iosched, softc->rotating ? -1 : 0); 1694 adagetparams(periph, cgd); 1695 softc->disk = disk_alloc(); 1696 softc->disk->d_rotation_rate = cgd->ident_data.media_rotation_rate; 1697 softc->disk->d_devstat = devstat_new_entry(periph->periph_name, 1698 periph->unit_number, softc->params.secsize, 1699 DEVSTAT_ALL_SUPPORTED, 1700 DEVSTAT_TYPE_DIRECT | 1701 XPORT_DEVSTAT_TYPE(cpi.transport), 1702 DEVSTAT_PRIORITY_DISK); 1703 softc->disk->d_open = adaopen; 1704 softc->disk->d_close = adaclose; 1705 softc->disk->d_strategy = adastrategy; 1706 softc->disk->d_getattr = adagetattr; 1707 softc->disk->d_dump = adadump; 1708 softc->disk->d_gone = adadiskgonecb; 1709 softc->disk->d_name = "ada"; 1710 softc->disk->d_drv1 = periph; 1711 maxio = cpi.maxio; /* Honor max I/O size of SIM */ 1712 if (maxio == 0) 1713 maxio = DFLTPHYS; /* traditional default */ 1714 else if (maxio > MAXPHYS) 1715 maxio = MAXPHYS; /* for safety */ 1716 if (softc->flags & ADA_FLAG_CAN_48BIT) 1717 maxio = min(maxio, 65536 * softc->params.secsize); 1718 else /* 28bit ATA command limit */ 1719 maxio = min(maxio, 256 * softc->params.secsize); 1720 softc->disk->d_maxsize = maxio; 1721 softc->disk->d_unit = periph->unit_number; 1722 softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION | DISKFLAG_CANZONE; 1723 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) 1724 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE; 1725 if (softc->flags & ADA_FLAG_CAN_TRIM) { 1726 softc->disk->d_flags |= DISKFLAG_CANDELETE; 1727 softc->disk->d_delmaxsize = softc->params.secsize * 1728 ATA_DSM_RANGE_MAX * 1729 softc->trim_max_ranges; 1730 } else if ((softc->flags & ADA_FLAG_CAN_CFA) && 1731 !(softc->flags & ADA_FLAG_CAN_48BIT)) { 1732 softc->disk->d_flags |= DISKFLAG_CANDELETE; 1733 softc->disk->d_delmaxsize = 256 * softc->params.secsize; 1734 } else 1735 softc->disk->d_delmaxsize = maxio; 1736 if ((cpi.hba_misc & PIM_UNMAPPED) != 0) { 1737 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO; 1738 softc->unmappedio = 1; 1739 } 1740 strlcpy(softc->disk->d_descr, cgd->ident_data.model, 1741 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model))); 1742 strlcpy(softc->disk->d_ident, cgd->ident_data.serial, 1743 MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial))); 1744 softc->disk->d_hba_vendor = cpi.hba_vendor; 1745 softc->disk->d_hba_device = cpi.hba_device; 1746 softc->disk->d_hba_subvendor = cpi.hba_subvendor; 1747 softc->disk->d_hba_subdevice = cpi.hba_subdevice; 1748 1749 softc->disk->d_sectorsize = softc->params.secsize; 1750 softc->disk->d_mediasize = (off_t)softc->params.sectors * 1751 softc->params.secsize; 1752 if (ata_physical_sector_size(&cgd->ident_data) != 1753 softc->params.secsize) { 1754 softc->disk->d_stripesize = 1755 ata_physical_sector_size(&cgd->ident_data); 1756 softc->disk->d_stripeoffset = (softc->disk->d_stripesize - 1757 ata_logical_sector_offset(&cgd->ident_data)) % 1758 softc->disk->d_stripesize; 1759 } else if (softc->quirks & ADA_Q_4K) { 1760 softc->disk->d_stripesize = 4096; 1761 softc->disk->d_stripeoffset = 0; 1762 } 1763 softc->disk->d_fwsectors = softc->params.secs_per_track; 1764 softc->disk->d_fwheads = softc->params.heads; 1765 ata_disk_firmware_geom_adjust(softc->disk); 1766 1767 /* 1768 * Acquire a reference to the periph before we register with GEOM. 1769 * We'll release this reference once GEOM calls us back (via 1770 * adadiskgonecb()) telling us that our provider has been freed. 1771 */ 1772 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 1773 xpt_print(periph->path, "%s: lost periph during " 1774 "registration!\n", __func__); 1775 cam_periph_lock(periph); 1776 return (CAM_REQ_CMP_ERR); 1777 } 1778 disk_create(softc->disk, DISK_VERSION); 1779 cam_periph_lock(periph); 1780 1781 dp = &softc->params; 1782 snprintf(announce_buf, sizeof(announce_buf), 1783 "%juMB (%ju %u byte sectors)", 1784 ((uintmax_t)dp->secsize * dp->sectors) / (1024 * 1024), 1785 (uintmax_t)dp->sectors, dp->secsize); 1786 xpt_announce_periph(periph, announce_buf); 1787 xpt_announce_quirks(periph, softc->quirks, ADA_Q_BIT_STRING); 1788 1789 /* 1790 * Create our sysctl variables, now that we know 1791 * we have successfully attached. 1792 */ 1793 if (cam_periph_acquire(periph) == CAM_REQ_CMP) 1794 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task); 1795 1796 /* 1797 * Add async callbacks for bus reset and 1798 * bus device reset calls. I don't bother 1799 * checking if this fails as, in most cases, 1800 * the system will function just fine without 1801 * them and the only alternative would be to 1802 * not attach the device on failure. 1803 */ 1804 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE | 1805 AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED, 1806 adaasync, periph, periph->path); 1807 1808 /* 1809 * Schedule a periodic event to occasionally send an 1810 * ordered tag to a device. 1811 */ 1812 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0); 1813 callout_reset(&softc->sendordered_c, 1814 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 1815 adasendorderedtag, softc); 1816 1817 if (ADA_RA >= 0 && softc->flags & ADA_FLAG_CAN_RAHEAD) { 1818 softc->state = ADA_STATE_RAHEAD; 1819 } else if (ADA_WC >= 0 && softc->flags & ADA_FLAG_CAN_WCACHE) { 1820 softc->state = ADA_STATE_WCACHE; 1821 } else if (softc->flags & ADA_FLAG_CAN_LOG) { 1822 softc->state = ADA_STATE_LOGDIR; 1823 } else { 1824 /* 1825 * Nothing to probe, so we can just transition to the 1826 * normal state. 1827 */ 1828 adaprobedone(periph, NULL); 1829 return(CAM_REQ_CMP); 1830 } 1831 1832 xpt_schedule(periph, CAM_PRIORITY_DEV); 1833 1834 return(CAM_REQ_CMP); 1835 } 1836 1837 static int 1838 ada_dsmtrim_req_create(struct ada_softc *softc, struct bio *bp, struct trim_request *req) 1839 { 1840 uint64_t lastlba = (uint64_t)-1; 1841 int c, lastcount = 0, off, ranges = 0; 1842 1843 bzero(req, sizeof(*req)); 1844 TAILQ_INIT(&req->bps); 1845 do { 1846 uint64_t lba = bp->bio_pblkno; 1847 int count = bp->bio_bcount / softc->params.secsize; 1848 1849 /* Try to extend the previous range. */ 1850 if (lba == lastlba) { 1851 c = min(count, ATA_DSM_RANGE_MAX - lastcount); 1852 lastcount += c; 1853 off = (ranges - 1) * ATA_DSM_RANGE_SIZE; 1854 req->data[off + 6] = lastcount & 0xff; 1855 req->data[off + 7] = 1856 (lastcount >> 8) & 0xff; 1857 count -= c; 1858 lba += c; 1859 } 1860 1861 while (count > 0) { 1862 c = min(count, ATA_DSM_RANGE_MAX); 1863 off = ranges * ATA_DSM_RANGE_SIZE; 1864 req->data[off + 0] = lba & 0xff; 1865 req->data[off + 1] = (lba >> 8) & 0xff; 1866 req->data[off + 2] = (lba >> 16) & 0xff; 1867 req->data[off + 3] = (lba >> 24) & 0xff; 1868 req->data[off + 4] = (lba >> 32) & 0xff; 1869 req->data[off + 5] = (lba >> 40) & 0xff; 1870 req->data[off + 6] = c & 0xff; 1871 req->data[off + 7] = (c >> 8) & 0xff; 1872 lba += c; 1873 count -= c; 1874 lastcount = c; 1875 ranges++; 1876 /* 1877 * Its the caller's responsibility to ensure the 1878 * request will fit so we don't need to check for 1879 * overrun here 1880 */ 1881 } 1882 lastlba = lba; 1883 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue); 1884 1885 bp = cam_iosched_next_trim(softc->cam_iosched); 1886 if (bp == NULL) 1887 break; 1888 if (bp->bio_bcount / softc->params.secsize > 1889 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) { 1890 cam_iosched_put_back_trim(softc->cam_iosched, bp); 1891 break; 1892 } 1893 } while (1); 1894 1895 return (ranges); 1896 } 1897 1898 static void 1899 ada_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 1900 { 1901 struct trim_request *req = &softc->trim_req; 1902 int ranges; 1903 1904 ranges = ada_dsmtrim_req_create(softc, bp, req); 1905 cam_fill_ataio(ataio, 1906 ada_retry_count, 1907 adadone, 1908 CAM_DIR_OUT, 1909 0, 1910 req->data, 1911 howmany(ranges, ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE, 1912 ada_default_timeout * 1000); 1913 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT, 1914 ATA_DSM_TRIM, 0, howmany(ranges, ATA_DSM_BLK_RANGES)); 1915 } 1916 1917 static void 1918 ada_ncq_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 1919 { 1920 struct trim_request *req = &softc->trim_req; 1921 int ranges; 1922 1923 ranges = ada_dsmtrim_req_create(softc, bp, req); 1924 cam_fill_ataio(ataio, 1925 ada_retry_count, 1926 adadone, 1927 CAM_DIR_OUT, 1928 0, 1929 req->data, 1930 howmany(ranges, ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE, 1931 ada_default_timeout * 1000); 1932 ata_ncq_cmd(ataio, 1933 ATA_SEND_FPDMA_QUEUED, 1934 0, 1935 howmany(ranges, ATA_DSM_BLK_RANGES)); 1936 ataio->cmd.sector_count_exp = ATA_SFPDMA_DSM; 1937 ataio->ata_flags |= ATA_FLAG_AUX; 1938 ataio->aux = 1; 1939 } 1940 1941 static void 1942 ada_cfaerase(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 1943 { 1944 struct trim_request *req = &softc->trim_req; 1945 uint64_t lba = bp->bio_pblkno; 1946 uint16_t count = bp->bio_bcount / softc->params.secsize; 1947 1948 bzero(req, sizeof(*req)); 1949 TAILQ_INIT(&req->bps); 1950 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue); 1951 1952 cam_fill_ataio(ataio, 1953 ada_retry_count, 1954 adadone, 1955 CAM_DIR_NONE, 1956 0, 1957 NULL, 1958 0, 1959 ada_default_timeout*1000); 1960 1961 if (count >= 256) 1962 count = 0; 1963 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count); 1964 } 1965 1966 static int 1967 ada_zone_bio_to_ata(int disk_zone_cmd) 1968 { 1969 switch (disk_zone_cmd) { 1970 case DISK_ZONE_OPEN: 1971 return ATA_ZM_OPEN_ZONE; 1972 case DISK_ZONE_CLOSE: 1973 return ATA_ZM_CLOSE_ZONE; 1974 case DISK_ZONE_FINISH: 1975 return ATA_ZM_FINISH_ZONE; 1976 case DISK_ZONE_RWP: 1977 return ATA_ZM_RWP; 1978 } 1979 1980 return -1; 1981 } 1982 1983 static int 1984 ada_zone_cmd(struct cam_periph *periph, union ccb *ccb, struct bio *bp, 1985 int *queue_ccb) 1986 { 1987 struct ada_softc *softc; 1988 int error; 1989 1990 error = 0; 1991 1992 if (bp->bio_cmd != BIO_ZONE) { 1993 error = EINVAL; 1994 goto bailout; 1995 } 1996 1997 softc = periph->softc; 1998 1999 switch (bp->bio_zone.zone_cmd) { 2000 case DISK_ZONE_OPEN: 2001 case DISK_ZONE_CLOSE: 2002 case DISK_ZONE_FINISH: 2003 case DISK_ZONE_RWP: { 2004 int zone_flags; 2005 int zone_sa; 2006 uint64_t lba; 2007 2008 zone_sa = ada_zone_bio_to_ata(bp->bio_zone.zone_cmd); 2009 if (zone_sa == -1) { 2010 xpt_print(periph->path, "Cannot translate zone " 2011 "cmd %#x to ATA\n", bp->bio_zone.zone_cmd); 2012 error = EINVAL; 2013 goto bailout; 2014 } 2015 2016 zone_flags = 0; 2017 lba = bp->bio_zone.zone_params.rwp.id; 2018 2019 if (bp->bio_zone.zone_params.rwp.flags & 2020 DISK_ZONE_RWP_FLAG_ALL) 2021 zone_flags |= ZBC_OUT_ALL; 2022 2023 ata_zac_mgmt_out(&ccb->ataio, 2024 /*retries*/ ada_retry_count, 2025 /*cbfcnp*/ adadone, 2026 /*use_ncq*/ (softc->flags & 2027 ADA_FLAG_PIM_ATA_EXT) ? 1 : 0, 2028 /*zm_action*/ zone_sa, 2029 /*zone_id*/ lba, 2030 /*zone_flags*/ zone_flags, 2031 /*sector_count*/ 0, 2032 /*data_ptr*/ NULL, 2033 /*dxfer_len*/ 0, 2034 /*timeout*/ ada_default_timeout * 1000); 2035 *queue_ccb = 1; 2036 2037 break; 2038 } 2039 case DISK_ZONE_REPORT_ZONES: { 2040 uint8_t *rz_ptr; 2041 uint32_t num_entries, alloc_size; 2042 struct disk_zone_report *rep; 2043 2044 rep = &bp->bio_zone.zone_params.report; 2045 2046 num_entries = rep->entries_allocated; 2047 if (num_entries == 0) { 2048 xpt_print(periph->path, "No entries allocated for " 2049 "Report Zones request\n"); 2050 error = EINVAL; 2051 goto bailout; 2052 } 2053 alloc_size = sizeof(struct scsi_report_zones_hdr) + 2054 (sizeof(struct scsi_report_zones_desc) * num_entries); 2055 alloc_size = min(alloc_size, softc->disk->d_maxsize); 2056 rz_ptr = malloc(alloc_size, M_ATADA, M_NOWAIT | M_ZERO); 2057 if (rz_ptr == NULL) { 2058 xpt_print(periph->path, "Unable to allocate memory " 2059 "for Report Zones request\n"); 2060 error = ENOMEM; 2061 goto bailout; 2062 } 2063 2064 ata_zac_mgmt_in(&ccb->ataio, 2065 /*retries*/ ada_retry_count, 2066 /*cbcfnp*/ adadone, 2067 /*use_ncq*/ (softc->flags & 2068 ADA_FLAG_PIM_ATA_EXT) ? 1 : 0, 2069 /*zm_action*/ ATA_ZM_REPORT_ZONES, 2070 /*zone_id*/ rep->starting_id, 2071 /*zone_flags*/ rep->rep_options, 2072 /*data_ptr*/ rz_ptr, 2073 /*dxfer_len*/ alloc_size, 2074 /*timeout*/ ada_default_timeout * 1000); 2075 2076 /* 2077 * For BIO_ZONE, this isn't normally needed. However, it 2078 * is used by devstat_end_transaction_bio() to determine 2079 * how much data was transferred. 2080 */ 2081 /* 2082 * XXX KDM we have a problem. But I'm not sure how to fix 2083 * it. devstat uses bio_bcount - bio_resid to calculate 2084 * the amount of data transferred. The GEOM disk code 2085 * uses bio_length - bio_resid to calculate the amount of 2086 * data in bio_completed. We have different structure 2087 * sizes above and below the ada(4) driver. So, if we 2088 * use the sizes above, the amount transferred won't be 2089 * quite accurate for devstat. If we use different sizes 2090 * for bio_bcount and bio_length (above and below 2091 * respectively), then the residual needs to match one or 2092 * the other. Everything is calculated after the bio 2093 * leaves the driver, so changing the values around isn't 2094 * really an option. For now, just set the count to the 2095 * passed in length. This means that the calculations 2096 * above (e.g. bio_completed) will be correct, but the 2097 * amount of data reported to devstat will be slightly 2098 * under or overstated. 2099 */ 2100 bp->bio_bcount = bp->bio_length; 2101 2102 *queue_ccb = 1; 2103 2104 break; 2105 } 2106 case DISK_ZONE_GET_PARAMS: { 2107 struct disk_zone_disk_params *params; 2108 2109 params = &bp->bio_zone.zone_params.disk_params; 2110 bzero(params, sizeof(*params)); 2111 2112 switch (softc->zone_mode) { 2113 case ADA_ZONE_DRIVE_MANAGED: 2114 params->zone_mode = DISK_ZONE_MODE_DRIVE_MANAGED; 2115 break; 2116 case ADA_ZONE_HOST_AWARE: 2117 params->zone_mode = DISK_ZONE_MODE_HOST_AWARE; 2118 break; 2119 case ADA_ZONE_HOST_MANAGED: 2120 params->zone_mode = DISK_ZONE_MODE_HOST_MANAGED; 2121 break; 2122 default: 2123 case ADA_ZONE_NONE: 2124 params->zone_mode = DISK_ZONE_MODE_NONE; 2125 break; 2126 } 2127 2128 if (softc->zone_flags & ADA_ZONE_FLAG_URSWRZ) 2129 params->flags |= DISK_ZONE_DISK_URSWRZ; 2130 2131 if (softc->zone_flags & ADA_ZONE_FLAG_OPT_SEQ_SET) { 2132 params->optimal_seq_zones = softc->optimal_seq_zones; 2133 params->flags |= DISK_ZONE_OPT_SEQ_SET; 2134 } 2135 2136 if (softc->zone_flags & ADA_ZONE_FLAG_OPT_NONSEQ_SET) { 2137 params->optimal_nonseq_zones = 2138 softc->optimal_nonseq_zones; 2139 params->flags |= DISK_ZONE_OPT_NONSEQ_SET; 2140 } 2141 2142 if (softc->zone_flags & ADA_ZONE_FLAG_MAX_SEQ_SET) { 2143 params->max_seq_zones = softc->max_seq_zones; 2144 params->flags |= DISK_ZONE_MAX_SEQ_SET; 2145 } 2146 if (softc->zone_flags & ADA_ZONE_FLAG_RZ_SUP) 2147 params->flags |= DISK_ZONE_RZ_SUP; 2148 2149 if (softc->zone_flags & ADA_ZONE_FLAG_OPEN_SUP) 2150 params->flags |= DISK_ZONE_OPEN_SUP; 2151 2152 if (softc->zone_flags & ADA_ZONE_FLAG_CLOSE_SUP) 2153 params->flags |= DISK_ZONE_CLOSE_SUP; 2154 2155 if (softc->zone_flags & ADA_ZONE_FLAG_FINISH_SUP) 2156 params->flags |= DISK_ZONE_FINISH_SUP; 2157 2158 if (softc->zone_flags & ADA_ZONE_FLAG_RWP_SUP) 2159 params->flags |= DISK_ZONE_RWP_SUP; 2160 break; 2161 } 2162 default: 2163 break; 2164 } 2165 bailout: 2166 return (error); 2167 } 2168 2169 static void 2170 adastart(struct cam_periph *periph, union ccb *start_ccb) 2171 { 2172 struct ada_softc *softc = (struct ada_softc *)periph->softc; 2173 struct ccb_ataio *ataio = &start_ccb->ataio; 2174 2175 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n")); 2176 2177 switch (softc->state) { 2178 case ADA_STATE_NORMAL: 2179 { 2180 struct bio *bp; 2181 u_int8_t tag_code; 2182 2183 bp = cam_iosched_next_bio(softc->cam_iosched); 2184 if (bp == NULL) { 2185 xpt_release_ccb(start_ccb); 2186 break; 2187 } 2188 2189 if ((bp->bio_flags & BIO_ORDERED) != 0 || 2190 (bp->bio_cmd != BIO_DELETE && (softc->flags & ADA_FLAG_NEED_OTAG) != 0)) { 2191 softc->flags &= ~ADA_FLAG_NEED_OTAG; 2192 softc->flags |= ADA_FLAG_WAS_OTAG; 2193 tag_code = 0; 2194 } else { 2195 tag_code = 1; 2196 } 2197 switch (bp->bio_cmd) { 2198 case BIO_WRITE: 2199 case BIO_READ: 2200 { 2201 uint64_t lba = bp->bio_pblkno; 2202 uint16_t count = bp->bio_bcount / softc->params.secsize; 2203 void *data_ptr; 2204 int rw_op; 2205 2206 if (bp->bio_cmd == BIO_WRITE) { 2207 softc->flags |= ADA_FLAG_DIRTY; 2208 rw_op = CAM_DIR_OUT; 2209 } else { 2210 rw_op = CAM_DIR_IN; 2211 } 2212 2213 data_ptr = bp->bio_data; 2214 if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) { 2215 rw_op |= CAM_DATA_BIO; 2216 data_ptr = bp; 2217 } 2218 2219 #ifdef ADA_TEST_FAILURE 2220 int fail = 0; 2221 2222 /* 2223 * Support the failure ioctls. If the command is a 2224 * read, and there are pending forced read errors, or 2225 * if a write and pending write errors, then fail this 2226 * operation with EIO. This is useful for testing 2227 * purposes. Also, support having every Nth read fail. 2228 * 2229 * This is a rather blunt tool. 2230 */ 2231 if (bp->bio_cmd == BIO_READ) { 2232 if (softc->force_read_error) { 2233 softc->force_read_error--; 2234 fail = 1; 2235 } 2236 if (softc->periodic_read_error > 0) { 2237 if (++softc->periodic_read_count >= 2238 softc->periodic_read_error) { 2239 softc->periodic_read_count = 0; 2240 fail = 1; 2241 } 2242 } 2243 } else { 2244 if (softc->force_write_error) { 2245 softc->force_write_error--; 2246 fail = 1; 2247 } 2248 } 2249 if (fail) { 2250 biofinish(bp, NULL, EIO); 2251 xpt_release_ccb(start_ccb); 2252 adaschedule(periph); 2253 return; 2254 } 2255 #endif 2256 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 || 2257 round_page(bp->bio_bcount + bp->bio_ma_offset) / 2258 PAGE_SIZE == bp->bio_ma_n, 2259 ("Short bio %p", bp)); 2260 cam_fill_ataio(ataio, 2261 ada_retry_count, 2262 adadone, 2263 rw_op, 2264 0, 2265 data_ptr, 2266 bp->bio_bcount, 2267 ada_default_timeout*1000); 2268 2269 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) { 2270 if (bp->bio_cmd == BIO_READ) { 2271 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED, 2272 lba, count); 2273 } else { 2274 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED, 2275 lba, count); 2276 } 2277 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) && 2278 (lba + count >= ATA_MAX_28BIT_LBA || 2279 count > 256)) { 2280 if (softc->flags & ADA_FLAG_CAN_DMA48) { 2281 if (bp->bio_cmd == BIO_READ) { 2282 ata_48bit_cmd(ataio, ATA_READ_DMA48, 2283 0, lba, count); 2284 } else { 2285 ata_48bit_cmd(ataio, ATA_WRITE_DMA48, 2286 0, lba, count); 2287 } 2288 } else { 2289 if (bp->bio_cmd == BIO_READ) { 2290 ata_48bit_cmd(ataio, ATA_READ_MUL48, 2291 0, lba, count); 2292 } else { 2293 ata_48bit_cmd(ataio, ATA_WRITE_MUL48, 2294 0, lba, count); 2295 } 2296 } 2297 } else { 2298 if (count == 256) 2299 count = 0; 2300 if (softc->flags & ADA_FLAG_CAN_DMA) { 2301 if (bp->bio_cmd == BIO_READ) { 2302 ata_28bit_cmd(ataio, ATA_READ_DMA, 2303 0, lba, count); 2304 } else { 2305 ata_28bit_cmd(ataio, ATA_WRITE_DMA, 2306 0, lba, count); 2307 } 2308 } else { 2309 if (bp->bio_cmd == BIO_READ) { 2310 ata_28bit_cmd(ataio, ATA_READ_MUL, 2311 0, lba, count); 2312 } else { 2313 ata_28bit_cmd(ataio, ATA_WRITE_MUL, 2314 0, lba, count); 2315 } 2316 } 2317 } 2318 break; 2319 } 2320 case BIO_DELETE: 2321 switch (softc->delete_method) { 2322 case ADA_DELETE_NCQ_DSM_TRIM: 2323 ada_ncq_dsmtrim(softc, bp, ataio); 2324 break; 2325 case ADA_DELETE_DSM_TRIM: 2326 ada_dsmtrim(softc, bp, ataio); 2327 break; 2328 case ADA_DELETE_CFA_ERASE: 2329 ada_cfaerase(softc, bp, ataio); 2330 break; 2331 default: 2332 biofinish(bp, NULL, EOPNOTSUPP); 2333 xpt_release_ccb(start_ccb); 2334 adaschedule(periph); 2335 return; 2336 } 2337 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM; 2338 start_ccb->ccb_h.flags |= CAM_UNLOCKED; 2339 cam_iosched_submit_trim(softc->cam_iosched); 2340 goto out; 2341 case BIO_FLUSH: 2342 cam_fill_ataio(ataio, 2343 1, 2344 adadone, 2345 CAM_DIR_NONE, 2346 0, 2347 NULL, 2348 0, 2349 ada_default_timeout*1000); 2350 2351 if (softc->flags & ADA_FLAG_CAN_48BIT) 2352 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0); 2353 else 2354 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0); 2355 break; 2356 case BIO_ZONE: { 2357 int error, queue_ccb; 2358 2359 queue_ccb = 0; 2360 2361 error = ada_zone_cmd(periph, start_ccb, bp, &queue_ccb); 2362 if ((error != 0) 2363 || (queue_ccb == 0)) { 2364 biofinish(bp, NULL, error); 2365 xpt_release_ccb(start_ccb); 2366 return; 2367 } 2368 break; 2369 } 2370 } 2371 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO; 2372 start_ccb->ccb_h.flags |= CAM_UNLOCKED; 2373 out: 2374 start_ccb->ccb_h.ccb_bp = bp; 2375 softc->outstanding_cmds++; 2376 softc->refcount++; 2377 cam_periph_unlock(periph); 2378 xpt_action(start_ccb); 2379 cam_periph_lock(periph); 2380 softc->refcount--; 2381 2382 /* May have more work to do, so ensure we stay scheduled */ 2383 adaschedule(periph); 2384 break; 2385 } 2386 case ADA_STATE_RAHEAD: 2387 case ADA_STATE_WCACHE: 2388 { 2389 cam_fill_ataio(ataio, 2390 1, 2391 adadone, 2392 CAM_DIR_NONE, 2393 0, 2394 NULL, 2395 0, 2396 ada_default_timeout*1000); 2397 2398 if (softc->state == ADA_STATE_RAHEAD) { 2399 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ? 2400 ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0); 2401 start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD; 2402 } else { 2403 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ? 2404 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0); 2405 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE; 2406 } 2407 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 2408 xpt_action(start_ccb); 2409 break; 2410 } 2411 case ADA_STATE_LOGDIR: 2412 { 2413 struct ata_gp_log_dir *log_dir; 2414 2415 if ((softc->flags & ADA_FLAG_CAN_LOG) == 0) { 2416 adaprobedone(periph, start_ccb); 2417 break; 2418 } 2419 2420 log_dir = malloc(sizeof(*log_dir), M_ATADA, M_NOWAIT|M_ZERO); 2421 if (log_dir == NULL) { 2422 xpt_print(periph->path, "Couldn't malloc log_dir " 2423 "data\n"); 2424 softc->state = ADA_STATE_NORMAL; 2425 xpt_release_ccb(start_ccb); 2426 break; 2427 } 2428 2429 2430 ata_read_log(ataio, 2431 /*retries*/1, 2432 /*cbfcnp*/adadone, 2433 /*log_address*/ ATA_LOG_DIRECTORY, 2434 /*page_number*/ 0, 2435 /*block_count*/ 1, 2436 /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ? 2437 CAM_ATAIO_DMA : 0, 2438 /*data_ptr*/ (uint8_t *)log_dir, 2439 /*dxfer_len*/sizeof(*log_dir), 2440 /*timeout*/ada_default_timeout*1000); 2441 2442 start_ccb->ccb_h.ccb_state = ADA_CCB_LOGDIR; 2443 xpt_action(start_ccb); 2444 break; 2445 } 2446 case ADA_STATE_IDDIR: 2447 { 2448 struct ata_identify_log_pages *id_dir; 2449 2450 id_dir = malloc(sizeof(*id_dir), M_ATADA, M_NOWAIT | M_ZERO); 2451 if (id_dir == NULL) { 2452 xpt_print(periph->path, "Couldn't malloc id_dir " 2453 "data\n"); 2454 adaprobedone(periph, start_ccb); 2455 break; 2456 } 2457 2458 ata_read_log(ataio, 2459 /*retries*/1, 2460 /*cbfcnp*/adadone, 2461 /*log_address*/ ATA_IDENTIFY_DATA_LOG, 2462 /*page_number*/ ATA_IDL_PAGE_LIST, 2463 /*block_count*/ 1, 2464 /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ? 2465 CAM_ATAIO_DMA : 0, 2466 /*data_ptr*/ (uint8_t *)id_dir, 2467 /*dxfer_len*/ sizeof(*id_dir), 2468 /*timeout*/ada_default_timeout*1000); 2469 2470 start_ccb->ccb_h.ccb_state = ADA_CCB_IDDIR; 2471 xpt_action(start_ccb); 2472 break; 2473 } 2474 case ADA_STATE_SUP_CAP: 2475 { 2476 struct ata_identify_log_sup_cap *sup_cap; 2477 2478 sup_cap = malloc(sizeof(*sup_cap), M_ATADA, M_NOWAIT|M_ZERO); 2479 if (sup_cap == NULL) { 2480 xpt_print(periph->path, "Couldn't malloc sup_cap " 2481 "data\n"); 2482 adaprobedone(periph, start_ccb); 2483 break; 2484 } 2485 2486 ata_read_log(ataio, 2487 /*retries*/1, 2488 /*cbfcnp*/adadone, 2489 /*log_address*/ ATA_IDENTIFY_DATA_LOG, 2490 /*page_number*/ ATA_IDL_SUP_CAP, 2491 /*block_count*/ 1, 2492 /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ? 2493 CAM_ATAIO_DMA : 0, 2494 /*data_ptr*/ (uint8_t *)sup_cap, 2495 /*dxfer_len*/ sizeof(*sup_cap), 2496 /*timeout*/ada_default_timeout*1000); 2497 2498 start_ccb->ccb_h.ccb_state = ADA_CCB_SUP_CAP; 2499 xpt_action(start_ccb); 2500 break; 2501 } 2502 case ADA_STATE_ZONE: 2503 { 2504 struct ata_zoned_info_log *ata_zone; 2505 2506 ata_zone = malloc(sizeof(*ata_zone), M_ATADA, M_NOWAIT|M_ZERO); 2507 if (ata_zone == NULL) { 2508 xpt_print(periph->path, "Couldn't malloc ata_zone " 2509 "data\n"); 2510 adaprobedone(periph, start_ccb); 2511 break; 2512 } 2513 2514 ata_read_log(ataio, 2515 /*retries*/1, 2516 /*cbfcnp*/adadone, 2517 /*log_address*/ ATA_IDENTIFY_DATA_LOG, 2518 /*page_number*/ ATA_IDL_ZDI, 2519 /*block_count*/ 1, 2520 /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ? 2521 CAM_ATAIO_DMA : 0, 2522 /*data_ptr*/ (uint8_t *)ata_zone, 2523 /*dxfer_len*/ sizeof(*ata_zone), 2524 /*timeout*/ada_default_timeout*1000); 2525 2526 start_ccb->ccb_h.ccb_state = ADA_CCB_ZONE; 2527 xpt_action(start_ccb); 2528 break; 2529 } 2530 } 2531 } 2532 2533 static void 2534 adaprobedone(struct cam_periph *periph, union ccb *ccb) 2535 { 2536 struct ada_softc *softc; 2537 2538 softc = (struct ada_softc *)periph->softc; 2539 2540 if (ccb != NULL) 2541 xpt_release_ccb(ccb); 2542 2543 softc->state = ADA_STATE_NORMAL; 2544 softc->flags |= ADA_FLAG_PROBED; 2545 adaschedule(periph); 2546 if ((softc->flags & ADA_FLAG_ANNOUNCED) == 0) { 2547 softc->flags |= ADA_FLAG_ANNOUNCED; 2548 cam_periph_unhold(periph); 2549 } else { 2550 cam_periph_release_locked(periph); 2551 } 2552 } 2553 2554 static void 2555 adazonedone(struct cam_periph *periph, union ccb *ccb) 2556 { 2557 struct ada_softc *softc; 2558 struct bio *bp; 2559 2560 softc = periph->softc; 2561 bp = (struct bio *)ccb->ccb_h.ccb_bp; 2562 2563 switch (bp->bio_zone.zone_cmd) { 2564 case DISK_ZONE_OPEN: 2565 case DISK_ZONE_CLOSE: 2566 case DISK_ZONE_FINISH: 2567 case DISK_ZONE_RWP: 2568 break; 2569 case DISK_ZONE_REPORT_ZONES: { 2570 uint32_t avail_len; 2571 struct disk_zone_report *rep; 2572 struct scsi_report_zones_hdr *hdr; 2573 struct scsi_report_zones_desc *desc; 2574 struct disk_zone_rep_entry *entry; 2575 uint32_t num_alloced, hdr_len, num_avail; 2576 uint32_t num_to_fill, i; 2577 2578 rep = &bp->bio_zone.zone_params.report; 2579 avail_len = ccb->ataio.dxfer_len - ccb->ataio.resid; 2580 /* 2581 * Note that bio_resid isn't normally used for zone 2582 * commands, but it is used by devstat_end_transaction_bio() 2583 * to determine how much data was transferred. Because 2584 * the size of the SCSI/ATA data structures is different 2585 * than the size of the BIO interface structures, the 2586 * amount of data actually transferred from the drive will 2587 * be different than the amount of data transferred to 2588 * the user. 2589 */ 2590 num_alloced = rep->entries_allocated; 2591 hdr = (struct scsi_report_zones_hdr *)ccb->ataio.data_ptr; 2592 if (avail_len < sizeof(*hdr)) { 2593 /* 2594 * Is there a better error than EIO here? We asked 2595 * for at least the header, and we got less than 2596 * that. 2597 */ 2598 bp->bio_error = EIO; 2599 bp->bio_flags |= BIO_ERROR; 2600 bp->bio_resid = bp->bio_bcount; 2601 break; 2602 } 2603 2604 hdr_len = le32dec(hdr->length); 2605 if (hdr_len > 0) 2606 rep->entries_available = hdr_len / sizeof(*desc); 2607 else 2608 rep->entries_available = 0; 2609 /* 2610 * NOTE: using the same values for the BIO version of the 2611 * same field as the SCSI/ATA values. This means we could 2612 * get some additional values that aren't defined in bio.h 2613 * if more values of the same field are defined later. 2614 */ 2615 rep->header.same = hdr->byte4 & SRZ_SAME_MASK; 2616 rep->header.maximum_lba = le64dec(hdr->maximum_lba); 2617 /* 2618 * If the drive reports no entries that match the query, 2619 * we're done. 2620 */ 2621 if (hdr_len == 0) { 2622 rep->entries_filled = 0; 2623 bp->bio_resid = bp->bio_bcount; 2624 break; 2625 } 2626 2627 num_avail = min((avail_len - sizeof(*hdr)) / sizeof(*desc), 2628 hdr_len / sizeof(*desc)); 2629 /* 2630 * If the drive didn't return any data, then we're done. 2631 */ 2632 if (num_avail == 0) { 2633 rep->entries_filled = 0; 2634 bp->bio_resid = bp->bio_bcount; 2635 break; 2636 } 2637 2638 num_to_fill = min(num_avail, rep->entries_allocated); 2639 /* 2640 * If the user didn't allocate any entries for us to fill, 2641 * we're done. 2642 */ 2643 if (num_to_fill == 0) { 2644 rep->entries_filled = 0; 2645 bp->bio_resid = bp->bio_bcount; 2646 break; 2647 } 2648 2649 for (i = 0, desc = &hdr->desc_list[0], entry=&rep->entries[0]; 2650 i < num_to_fill; i++, desc++, entry++) { 2651 /* 2652 * NOTE: we're mapping the values here directly 2653 * from the SCSI/ATA bit definitions to the bio.h 2654 * definitions. There is also a warning in 2655 * disk_zone.h, but the impact is that if 2656 * additional values are added in the SCSI/ATA 2657 * specs these will be visible to consumers of 2658 * this interface. 2659 */ 2660 entry->zone_type = desc->zone_type & SRZ_TYPE_MASK; 2661 entry->zone_condition = 2662 (desc->zone_flags & SRZ_ZONE_COND_MASK) >> 2663 SRZ_ZONE_COND_SHIFT; 2664 entry->zone_flags |= desc->zone_flags & 2665 (SRZ_ZONE_NON_SEQ|SRZ_ZONE_RESET); 2666 entry->zone_length = le64dec(desc->zone_length); 2667 entry->zone_start_lba = le64dec(desc->zone_start_lba); 2668 entry->write_pointer_lba = 2669 le64dec(desc->write_pointer_lba); 2670 } 2671 rep->entries_filled = num_to_fill; 2672 /* 2673 * Note that this residual is accurate from the user's 2674 * standpoint, but the amount transferred isn't accurate 2675 * from the standpoint of what actually came back from the 2676 * drive. 2677 */ 2678 bp->bio_resid = bp->bio_bcount - (num_to_fill * sizeof(*entry)); 2679 break; 2680 } 2681 case DISK_ZONE_GET_PARAMS: 2682 default: 2683 /* 2684 * In theory we should not get a GET_PARAMS bio, since it 2685 * should be handled without queueing the command to the 2686 * drive. 2687 */ 2688 panic("%s: Invalid zone command %d", __func__, 2689 bp->bio_zone.zone_cmd); 2690 break; 2691 } 2692 2693 if (bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES) 2694 free(ccb->ataio.data_ptr, M_ATADA); 2695 } 2696 2697 2698 static void 2699 adadone(struct cam_periph *periph, union ccb *done_ccb) 2700 { 2701 struct ada_softc *softc; 2702 struct ccb_ataio *ataio; 2703 struct cam_path *path; 2704 uint32_t priority; 2705 int state; 2706 2707 softc = (struct ada_softc *)periph->softc; 2708 ataio = &done_ccb->ataio; 2709 path = done_ccb->ccb_h.path; 2710 priority = done_ccb->ccb_h.pinfo.priority; 2711 2712 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n")); 2713 2714 state = ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK; 2715 switch (state) { 2716 case ADA_CCB_BUFFER_IO: 2717 case ADA_CCB_TRIM: 2718 { 2719 struct bio *bp; 2720 int error; 2721 2722 cam_periph_lock(periph); 2723 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 2724 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2725 error = adaerror(done_ccb, 0, 0); 2726 if (error == ERESTART) { 2727 /* A retry was scheduled, so just return. */ 2728 cam_periph_unlock(periph); 2729 return; 2730 } 2731 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 2732 cam_release_devq(path, 2733 /*relsim_flags*/0, 2734 /*reduction*/0, 2735 /*timeout*/0, 2736 /*getcount_only*/0); 2737 /* 2738 * If we get an error on an NCQ DSM TRIM, fall back 2739 * to a non-NCQ DSM TRIM forever. Please note that if 2740 * CAN_NCQ_TRIM is set, CAN_TRIM is necessarily set too. 2741 * However, for this one trim, we treat it as advisory 2742 * and return success up the stack. 2743 */ 2744 if (state == ADA_CCB_TRIM && 2745 error != 0 && 2746 (softc->flags & ADA_FLAG_CAN_NCQ_TRIM) != 0) { 2747 softc->flags &= ~ADA_FLAG_CAN_NCQ_TRIM; 2748 error = 0; 2749 adasetdeletemethod(softc); 2750 } 2751 } else { 2752 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 2753 panic("REQ_CMP with QFRZN"); 2754 2755 error = 0; 2756 } 2757 bp->bio_error = error; 2758 if (error != 0) { 2759 bp->bio_resid = bp->bio_bcount; 2760 bp->bio_flags |= BIO_ERROR; 2761 } else { 2762 if (bp->bio_cmd == BIO_ZONE) 2763 adazonedone(periph, done_ccb); 2764 else if (state == ADA_CCB_TRIM) 2765 bp->bio_resid = 0; 2766 else 2767 bp->bio_resid = ataio->resid; 2768 2769 if ((bp->bio_resid > 0) 2770 && (bp->bio_cmd != BIO_ZONE)) 2771 bp->bio_flags |= BIO_ERROR; 2772 } 2773 softc->outstanding_cmds--; 2774 if (softc->outstanding_cmds == 0) 2775 softc->flags |= ADA_FLAG_WAS_OTAG; 2776 2777 cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb); 2778 xpt_release_ccb(done_ccb); 2779 if (state == ADA_CCB_TRIM) { 2780 TAILQ_HEAD(, bio) queue; 2781 struct bio *bp1; 2782 2783 TAILQ_INIT(&queue); 2784 TAILQ_CONCAT(&queue, &softc->trim_req.bps, bio_queue); 2785 /* 2786 * Normally, the xpt_release_ccb() above would make sure 2787 * that when we have more work to do, that work would 2788 * get kicked off. However, we specifically keep 2789 * trim_running set to 0 before the call above to allow 2790 * other I/O to progress when many BIO_DELETE requests 2791 * are pushed down. We set trim_running to 0 and call 2792 * daschedule again so that we don't stall if there are 2793 * no other I/Os pending apart from BIO_DELETEs. 2794 */ 2795 cam_iosched_trim_done(softc->cam_iosched); 2796 adaschedule(periph); 2797 cam_periph_unlock(periph); 2798 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) { 2799 TAILQ_REMOVE(&queue, bp1, bio_queue); 2800 bp1->bio_error = error; 2801 if (error != 0) { 2802 bp1->bio_flags |= BIO_ERROR; 2803 bp1->bio_resid = bp1->bio_bcount; 2804 } else 2805 bp1->bio_resid = 0; 2806 biodone(bp1); 2807 } 2808 } else { 2809 adaschedule(periph); 2810 cam_periph_unlock(periph); 2811 biodone(bp); 2812 } 2813 return; 2814 } 2815 case ADA_CCB_RAHEAD: 2816 { 2817 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2818 if (adaerror(done_ccb, 0, 0) == ERESTART) { 2819 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 2820 cam_release_devq(path, 0, 0, 0, FALSE); 2821 return; 2822 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 2823 cam_release_devq(path, 2824 /*relsim_flags*/0, 2825 /*reduction*/0, 2826 /*timeout*/0, 2827 /*getcount_only*/0); 2828 } 2829 } 2830 2831 /* 2832 * Since our peripheral may be invalidated by an error 2833 * above or an external event, we must release our CCB 2834 * before releasing the reference on the peripheral. 2835 * The peripheral will only go away once the last reference 2836 * is removed, and we need it around for the CCB release 2837 * operation. 2838 */ 2839 2840 xpt_release_ccb(done_ccb); 2841 softc->state = ADA_STATE_WCACHE; 2842 xpt_schedule(periph, priority); 2843 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 2844 cam_release_devq(path, 0, 0, 0, FALSE); 2845 return; 2846 } 2847 case ADA_CCB_WCACHE: 2848 { 2849 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2850 if (adaerror(done_ccb, 0, 0) == ERESTART) { 2851 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 2852 cam_release_devq(path, 0, 0, 0, FALSE); 2853 return; 2854 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 2855 cam_release_devq(path, 2856 /*relsim_flags*/0, 2857 /*reduction*/0, 2858 /*timeout*/0, 2859 /*getcount_only*/0); 2860 } 2861 } 2862 2863 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 2864 cam_release_devq(path, 0, 0, 0, FALSE); 2865 2866 if (softc->flags & ADA_FLAG_CAN_LOG) { 2867 xpt_release_ccb(done_ccb); 2868 softc->state = ADA_STATE_LOGDIR; 2869 xpt_schedule(periph, priority); 2870 } else { 2871 adaprobedone(periph, done_ccb); 2872 } 2873 return; 2874 } 2875 case ADA_CCB_LOGDIR: 2876 { 2877 int error; 2878 2879 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 2880 error = 0; 2881 softc->valid_logdir_len = 0; 2882 bzero(&softc->ata_logdir, sizeof(softc->ata_logdir)); 2883 softc->valid_logdir_len = 2884 ataio->dxfer_len - ataio->resid; 2885 if (softc->valid_logdir_len > 0) 2886 bcopy(ataio->data_ptr, &softc->ata_logdir, 2887 min(softc->valid_logdir_len, 2888 sizeof(softc->ata_logdir))); 2889 /* 2890 * Figure out whether the Identify Device log is 2891 * supported. The General Purpose log directory 2892 * has a header, and lists the number of pages 2893 * available for each GP log identified by the 2894 * offset into the list. 2895 */ 2896 if ((softc->valid_logdir_len >= 2897 ((ATA_IDENTIFY_DATA_LOG + 1) * sizeof(uint16_t))) 2898 && (le16dec(softc->ata_logdir.header) == 2899 ATA_GP_LOG_DIR_VERSION) 2900 && (le16dec(&softc->ata_logdir.num_pages[ 2901 (ATA_IDENTIFY_DATA_LOG * 2902 sizeof(uint16_t)) - sizeof(uint16_t)]) > 0)){ 2903 softc->flags |= ADA_FLAG_CAN_IDLOG; 2904 } else { 2905 softc->flags &= ~ADA_FLAG_CAN_IDLOG; 2906 } 2907 } else { 2908 error = adaerror(done_ccb, CAM_RETRY_SELTO, 2909 SF_RETRY_UA|SF_NO_PRINT); 2910 if (error == ERESTART) 2911 return; 2912 else if (error != 0) { 2913 /* 2914 * If we can't get the ATA log directory, 2915 * then ATA logs are effectively not 2916 * supported even if the bit is set in the 2917 * identify data. 2918 */ 2919 softc->flags &= ~(ADA_FLAG_CAN_LOG | 2920 ADA_FLAG_CAN_IDLOG); 2921 if ((done_ccb->ccb_h.status & 2922 CAM_DEV_QFRZN) != 0) { 2923 /* Don't wedge this device's queue */ 2924 cam_release_devq(done_ccb->ccb_h.path, 2925 /*relsim_flags*/0, 2926 /*reduction*/0, 2927 /*timeout*/0, 2928 /*getcount_only*/0); 2929 } 2930 } 2931 2932 2933 } 2934 2935 free(ataio->data_ptr, M_ATADA); 2936 2937 if ((error == 0) 2938 && (softc->flags & ADA_FLAG_CAN_IDLOG)) { 2939 softc->state = ADA_STATE_IDDIR; 2940 xpt_release_ccb(done_ccb); 2941 xpt_schedule(periph, priority); 2942 } else 2943 adaprobedone(periph, done_ccb); 2944 2945 return; 2946 } 2947 case ADA_CCB_IDDIR: { 2948 int error; 2949 2950 if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 2951 off_t entries_offset, max_entries; 2952 error = 0; 2953 2954 softc->valid_iddir_len = 0; 2955 bzero(&softc->ata_iddir, sizeof(softc->ata_iddir)); 2956 softc->flags &= ~(ADA_FLAG_CAN_SUPCAP | 2957 ADA_FLAG_CAN_ZONE); 2958 softc->valid_iddir_len = 2959 ataio->dxfer_len - ataio->resid; 2960 if (softc->valid_iddir_len > 0) 2961 bcopy(ataio->data_ptr, &softc->ata_iddir, 2962 min(softc->valid_iddir_len, 2963 sizeof(softc->ata_iddir))); 2964 2965 entries_offset = 2966 __offsetof(struct ata_identify_log_pages,entries); 2967 max_entries = softc->valid_iddir_len - entries_offset; 2968 if ((softc->valid_iddir_len > (entries_offset + 1)) 2969 && (le64dec(softc->ata_iddir.header) == 2970 ATA_IDLOG_REVISION) 2971 && (softc->ata_iddir.entry_count > 0)) { 2972 int num_entries, i; 2973 2974 num_entries = softc->ata_iddir.entry_count; 2975 num_entries = min(num_entries, 2976 softc->valid_iddir_len - entries_offset); 2977 for (i = 0; i < num_entries && 2978 i < max_entries; i++) { 2979 if (softc->ata_iddir.entries[i] == 2980 ATA_IDL_SUP_CAP) 2981 softc->flags |= 2982 ADA_FLAG_CAN_SUPCAP; 2983 else if (softc->ata_iddir.entries[i]== 2984 ATA_IDL_ZDI) 2985 softc->flags |= 2986 ADA_FLAG_CAN_ZONE; 2987 2988 if ((softc->flags & 2989 ADA_FLAG_CAN_SUPCAP) 2990 && (softc->flags & 2991 ADA_FLAG_CAN_ZONE)) 2992 break; 2993 } 2994 } 2995 } else { 2996 error = adaerror(done_ccb, CAM_RETRY_SELTO, 2997 SF_RETRY_UA|SF_NO_PRINT); 2998 if (error == ERESTART) 2999 return; 3000 else if (error != 0) { 3001 /* 3002 * If we can't get the ATA Identify Data log 3003 * directory, then it effectively isn't 3004 * supported even if the ATA Log directory 3005 * a non-zero number of pages present for 3006 * this log. 3007 */ 3008 softc->flags &= ~ADA_FLAG_CAN_IDLOG; 3009 if ((done_ccb->ccb_h.status & 3010 CAM_DEV_QFRZN) != 0) { 3011 /* Don't wedge this device's queue */ 3012 cam_release_devq(done_ccb->ccb_h.path, 3013 /*relsim_flags*/0, 3014 /*reduction*/0, 3015 /*timeout*/0, 3016 /*getcount_only*/0); 3017 } 3018 } 3019 } 3020 3021 free(ataio->data_ptr, M_ATADA); 3022 3023 if ((error == 0) 3024 && (softc->flags & ADA_FLAG_CAN_SUPCAP)) { 3025 softc->state = ADA_STATE_SUP_CAP; 3026 xpt_release_ccb(done_ccb); 3027 xpt_schedule(periph, priority); 3028 } else 3029 adaprobedone(periph, done_ccb); 3030 return; 3031 } 3032 case ADA_CCB_SUP_CAP: { 3033 int error; 3034 3035 if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 3036 uint32_t valid_len; 3037 size_t needed_size; 3038 struct ata_identify_log_sup_cap *sup_cap; 3039 error = 0; 3040 3041 sup_cap = (struct ata_identify_log_sup_cap *) 3042 ataio->data_ptr; 3043 valid_len = ataio->dxfer_len - ataio->resid; 3044 needed_size = 3045 __offsetof(struct ata_identify_log_sup_cap, 3046 sup_zac_cap) + 1 + sizeof(sup_cap->sup_zac_cap); 3047 if (valid_len >= needed_size) { 3048 uint64_t zoned, zac_cap; 3049 3050 zoned = le64dec(sup_cap->zoned_cap); 3051 if (zoned & ATA_ZONED_VALID) { 3052 /* 3053 * This should have already been 3054 * set, because this is also in the 3055 * ATA identify data. 3056 */ 3057 if ((zoned & ATA_ZONED_MASK) == 3058 ATA_SUPPORT_ZONE_HOST_AWARE) 3059 softc->zone_mode = 3060 ADA_ZONE_HOST_AWARE; 3061 else if ((zoned & ATA_ZONED_MASK) == 3062 ATA_SUPPORT_ZONE_DEV_MANAGED) 3063 softc->zone_mode = 3064 ADA_ZONE_DRIVE_MANAGED; 3065 } 3066 3067 zac_cap = le64dec(sup_cap->sup_zac_cap); 3068 if (zac_cap & ATA_SUP_ZAC_CAP_VALID) { 3069 if (zac_cap & ATA_REPORT_ZONES_SUP) 3070 softc->zone_flags |= 3071 ADA_ZONE_FLAG_RZ_SUP; 3072 if (zac_cap & ATA_ND_OPEN_ZONE_SUP) 3073 softc->zone_flags |= 3074 ADA_ZONE_FLAG_OPEN_SUP; 3075 if (zac_cap & ATA_ND_CLOSE_ZONE_SUP) 3076 softc->zone_flags |= 3077 ADA_ZONE_FLAG_CLOSE_SUP; 3078 if (zac_cap & ATA_ND_FINISH_ZONE_SUP) 3079 softc->zone_flags |= 3080 ADA_ZONE_FLAG_FINISH_SUP; 3081 if (zac_cap & ATA_ND_RWP_SUP) 3082 softc->zone_flags |= 3083 ADA_ZONE_FLAG_RWP_SUP; 3084 } else { 3085 /* 3086 * This field was introduced in 3087 * ACS-4, r08 on April 28th, 2015. 3088 * If the drive firmware was written 3089 * to an earlier spec, it won't have 3090 * the field. So, assume all 3091 * commands are supported. 3092 */ 3093 softc->zone_flags |= 3094 ADA_ZONE_FLAG_SUP_MASK; 3095 } 3096 3097 } 3098 } else { 3099 error = adaerror(done_ccb, CAM_RETRY_SELTO, 3100 SF_RETRY_UA|SF_NO_PRINT); 3101 if (error == ERESTART) 3102 return; 3103 else if (error != 0) { 3104 /* 3105 * If we can't get the ATA Identify Data 3106 * Supported Capabilities page, clear the 3107 * flag... 3108 */ 3109 softc->flags &= ~ADA_FLAG_CAN_SUPCAP; 3110 /* 3111 * And clear zone capabilities. 3112 */ 3113 softc->zone_flags &= ~ADA_ZONE_FLAG_SUP_MASK; 3114 if ((done_ccb->ccb_h.status & 3115 CAM_DEV_QFRZN) != 0) { 3116 /* Don't wedge this device's queue */ 3117 cam_release_devq(done_ccb->ccb_h.path, 3118 /*relsim_flags*/0, 3119 /*reduction*/0, 3120 /*timeout*/0, 3121 /*getcount_only*/0); 3122 } 3123 } 3124 } 3125 3126 free(ataio->data_ptr, M_ATADA); 3127 3128 if ((error == 0) 3129 && (softc->flags & ADA_FLAG_CAN_ZONE)) { 3130 softc->state = ADA_STATE_ZONE; 3131 xpt_release_ccb(done_ccb); 3132 xpt_schedule(periph, priority); 3133 } else 3134 adaprobedone(periph, done_ccb); 3135 return; 3136 } 3137 case ADA_CCB_ZONE: { 3138 int error; 3139 3140 if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 3141 struct ata_zoned_info_log *zi_log; 3142 uint32_t valid_len; 3143 size_t needed_size; 3144 3145 zi_log = (struct ata_zoned_info_log *)ataio->data_ptr; 3146 3147 valid_len = ataio->dxfer_len - ataio->resid; 3148 needed_size = __offsetof(struct ata_zoned_info_log, 3149 version_info) + 1 + sizeof(zi_log->version_info); 3150 if (valid_len >= needed_size) { 3151 uint64_t tmpvar; 3152 3153 tmpvar = le64dec(zi_log->zoned_cap); 3154 if (tmpvar & ATA_ZDI_CAP_VALID) { 3155 if (tmpvar & ATA_ZDI_CAP_URSWRZ) 3156 softc->zone_flags |= 3157 ADA_ZONE_FLAG_URSWRZ; 3158 else 3159 softc->zone_flags &= 3160 ~ADA_ZONE_FLAG_URSWRZ; 3161 } 3162 tmpvar = le64dec(zi_log->optimal_seq_zones); 3163 if (tmpvar & ATA_ZDI_OPT_SEQ_VALID) { 3164 softc->zone_flags |= 3165 ADA_ZONE_FLAG_OPT_SEQ_SET; 3166 softc->optimal_seq_zones = (tmpvar & 3167 ATA_ZDI_OPT_SEQ_MASK); 3168 } else { 3169 softc->zone_flags &= 3170 ~ADA_ZONE_FLAG_OPT_SEQ_SET; 3171 softc->optimal_seq_zones = 0; 3172 } 3173 3174 tmpvar =le64dec(zi_log->optimal_nonseq_zones); 3175 if (tmpvar & ATA_ZDI_OPT_NS_VALID) { 3176 softc->zone_flags |= 3177 ADA_ZONE_FLAG_OPT_NONSEQ_SET; 3178 softc->optimal_nonseq_zones = 3179 (tmpvar & ATA_ZDI_OPT_NS_MASK); 3180 } else { 3181 softc->zone_flags &= 3182 ~ADA_ZONE_FLAG_OPT_NONSEQ_SET; 3183 softc->optimal_nonseq_zones = 0; 3184 } 3185 3186 tmpvar = le64dec(zi_log->max_seq_req_zones); 3187 if (tmpvar & ATA_ZDI_MAX_SEQ_VALID) { 3188 softc->zone_flags |= 3189 ADA_ZONE_FLAG_MAX_SEQ_SET; 3190 softc->max_seq_zones = 3191 (tmpvar & ATA_ZDI_MAX_SEQ_MASK); 3192 } else { 3193 softc->zone_flags &= 3194 ~ADA_ZONE_FLAG_MAX_SEQ_SET; 3195 softc->max_seq_zones = 0; 3196 } 3197 } 3198 } else { 3199 error = adaerror(done_ccb, CAM_RETRY_SELTO, 3200 SF_RETRY_UA|SF_NO_PRINT); 3201 if (error == ERESTART) 3202 return; 3203 else if (error != 0) { 3204 softc->flags &= ~ADA_FLAG_CAN_ZONE; 3205 softc->flags &= ~ADA_ZONE_FLAG_SET_MASK; 3206 3207 if ((done_ccb->ccb_h.status & 3208 CAM_DEV_QFRZN) != 0) { 3209 /* Don't wedge this device's queue */ 3210 cam_release_devq(done_ccb->ccb_h.path, 3211 /*relsim_flags*/0, 3212 /*reduction*/0, 3213 /*timeout*/0, 3214 /*getcount_only*/0); 3215 } 3216 } 3217 3218 } 3219 free(ataio->data_ptr, M_ATADA); 3220 3221 adaprobedone(periph, done_ccb); 3222 return; 3223 } 3224 case ADA_CCB_DUMP: 3225 /* No-op. We're polling */ 3226 return; 3227 default: 3228 break; 3229 } 3230 xpt_release_ccb(done_ccb); 3231 } 3232 3233 static int 3234 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 3235 { 3236 #ifdef CAM_IO_STATS 3237 struct ada_softc *softc; 3238 struct cam_periph *periph; 3239 3240 periph = xpt_path_periph(ccb->ccb_h.path); 3241 softc = (struct ada_softc *)periph->softc; 3242 3243 switch (ccb->ccb_h.status & CAM_STATUS_MASK) { 3244 case CAM_CMD_TIMEOUT: 3245 softc->timeouts++; 3246 break; 3247 case CAM_REQ_ABORTED: 3248 case CAM_REQ_CMP_ERR: 3249 case CAM_REQ_TERMIO: 3250 case CAM_UNREC_HBA_ERROR: 3251 case CAM_DATA_RUN_ERR: 3252 case CAM_ATA_STATUS_ERROR: 3253 softc->errors++; 3254 break; 3255 default: 3256 break; 3257 } 3258 #endif 3259 3260 return(cam_periph_error(ccb, cam_flags, sense_flags, NULL)); 3261 } 3262 3263 static void 3264 adagetparams(struct cam_periph *periph, struct ccb_getdev *cgd) 3265 { 3266 struct ada_softc *softc = (struct ada_softc *)periph->softc; 3267 struct disk_params *dp = &softc->params; 3268 u_int64_t lbasize48; 3269 u_int32_t lbasize; 3270 3271 dp->secsize = ata_logical_sector_size(&cgd->ident_data); 3272 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) && 3273 cgd->ident_data.current_heads && cgd->ident_data.current_sectors) { 3274 dp->heads = cgd->ident_data.current_heads; 3275 dp->secs_per_track = cgd->ident_data.current_sectors; 3276 dp->cylinders = cgd->ident_data.cylinders; 3277 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 | 3278 ((u_int32_t)cgd->ident_data.current_size_2 << 16); 3279 } else { 3280 dp->heads = cgd->ident_data.heads; 3281 dp->secs_per_track = cgd->ident_data.sectors; 3282 dp->cylinders = cgd->ident_data.cylinders; 3283 dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track; 3284 } 3285 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 | 3286 ((u_int32_t)cgd->ident_data.lba_size_2 << 16); 3287 3288 /* use the 28bit LBA size if valid or bigger than the CHS mapping */ 3289 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize) 3290 dp->sectors = lbasize; 3291 3292 /* use the 48bit LBA size if valid */ 3293 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) | 3294 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) | 3295 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) | 3296 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48); 3297 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) && 3298 lbasize48 > ATA_MAX_28BIT_LBA) 3299 dp->sectors = lbasize48; 3300 } 3301 3302 static void 3303 adasendorderedtag(void *arg) 3304 { 3305 struct ada_softc *softc = arg; 3306 3307 if (ada_send_ordered) { 3308 if (softc->outstanding_cmds > 0) { 3309 if ((softc->flags & ADA_FLAG_WAS_OTAG) == 0) 3310 softc->flags |= ADA_FLAG_NEED_OTAG; 3311 softc->flags &= ~ADA_FLAG_WAS_OTAG; 3312 } 3313 } 3314 /* Queue us up again */ 3315 callout_reset(&softc->sendordered_c, 3316 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 3317 adasendorderedtag, softc); 3318 } 3319 3320 /* 3321 * Step through all ADA peripheral drivers, and if the device is still open, 3322 * sync the disk cache to physical media. 3323 */ 3324 static void 3325 adaflush(void) 3326 { 3327 struct cam_periph *periph; 3328 struct ada_softc *softc; 3329 union ccb *ccb; 3330 int error; 3331 3332 CAM_PERIPH_FOREACH(periph, &adadriver) { 3333 softc = (struct ada_softc *)periph->softc; 3334 if (SCHEDULER_STOPPED()) { 3335 /* If we paniced with the lock held, do not recurse. */ 3336 if (!cam_periph_owned(periph) && 3337 (softc->flags & ADA_FLAG_OPEN)) { 3338 adadump(softc->disk, NULL, 0, 0, 0); 3339 } 3340 continue; 3341 } 3342 cam_periph_lock(periph); 3343 /* 3344 * We only sync the cache if the drive is still open, and 3345 * if the drive is capable of it.. 3346 */ 3347 if (((softc->flags & ADA_FLAG_OPEN) == 0) || 3348 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) { 3349 cam_periph_unlock(periph); 3350 continue; 3351 } 3352 3353 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3354 cam_fill_ataio(&ccb->ataio, 3355 0, 3356 adadone, 3357 CAM_DIR_NONE, 3358 0, 3359 NULL, 3360 0, 3361 ada_default_timeout*1000); 3362 if (softc->flags & ADA_FLAG_CAN_48BIT) 3363 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 3364 else 3365 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 3366 3367 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 3368 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 3369 softc->disk->d_devstat); 3370 if (error != 0) 3371 xpt_print(periph->path, "Synchronize cache failed\n"); 3372 xpt_release_ccb(ccb); 3373 cam_periph_unlock(periph); 3374 } 3375 } 3376 3377 static void 3378 adaspindown(uint8_t cmd, int flags) 3379 { 3380 struct cam_periph *periph; 3381 struct ada_softc *softc; 3382 union ccb *ccb; 3383 int error; 3384 3385 CAM_PERIPH_FOREACH(periph, &adadriver) { 3386 /* If we paniced with lock held - not recurse here. */ 3387 if (cam_periph_owned(periph)) 3388 continue; 3389 cam_periph_lock(periph); 3390 softc = (struct ada_softc *)periph->softc; 3391 /* 3392 * We only spin-down the drive if it is capable of it.. 3393 */ 3394 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 3395 cam_periph_unlock(periph); 3396 continue; 3397 } 3398 3399 if (bootverbose) 3400 xpt_print(periph->path, "spin-down\n"); 3401 3402 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3403 cam_fill_ataio(&ccb->ataio, 3404 0, 3405 adadone, 3406 CAM_DIR_NONE | flags, 3407 0, 3408 NULL, 3409 0, 3410 ada_default_timeout*1000); 3411 ata_28bit_cmd(&ccb->ataio, cmd, 0, 0, 0); 3412 3413 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 3414 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 3415 softc->disk->d_devstat); 3416 if (error != 0) 3417 xpt_print(periph->path, "Spin-down disk failed\n"); 3418 xpt_release_ccb(ccb); 3419 cam_periph_unlock(periph); 3420 } 3421 } 3422 3423 static void 3424 adashutdown(void *arg, int howto) 3425 { 3426 3427 adaflush(); 3428 if (ada_spindown_shutdown != 0 && 3429 (howto & (RB_HALT | RB_POWEROFF)) != 0) 3430 adaspindown(ATA_STANDBY_IMMEDIATE, 0); 3431 } 3432 3433 static void 3434 adasuspend(void *arg) 3435 { 3436 3437 adaflush(); 3438 if (ada_spindown_suspend != 0) 3439 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE); 3440 } 3441 3442 static void 3443 adaresume(void *arg) 3444 { 3445 struct cam_periph *periph; 3446 struct ada_softc *softc; 3447 3448 if (ada_spindown_suspend == 0) 3449 return; 3450 3451 CAM_PERIPH_FOREACH(periph, &adadriver) { 3452 cam_periph_lock(periph); 3453 softc = (struct ada_softc *)periph->softc; 3454 /* 3455 * We only spin-down the drive if it is capable of it.. 3456 */ 3457 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 3458 cam_periph_unlock(periph); 3459 continue; 3460 } 3461 3462 if (bootverbose) 3463 xpt_print(periph->path, "resume\n"); 3464 3465 /* 3466 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on 3467 * sleep request. 3468 */ 3469 cam_release_devq(periph->path, 3470 /*relsim_flags*/0, 3471 /*openings*/0, 3472 /*timeout*/0, 3473 /*getcount_only*/0); 3474 3475 cam_periph_unlock(periph); 3476 } 3477 } 3478 3479 #endif /* _KERNEL */ 3480