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