1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 __FBSDID("$FreeBSD$"); 31 32 #include "opt_ada.h" 33 34 #include <sys/param.h> 35 36 #ifdef _KERNEL 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/bio.h> 40 #include <sys/sysctl.h> 41 #include <sys/taskqueue.h> 42 #include <sys/lock.h> 43 #include <sys/mutex.h> 44 #include <sys/conf.h> 45 #include <sys/devicestat.h> 46 #include <sys/eventhandler.h> 47 #include <sys/malloc.h> 48 #include <sys/endian.h> 49 #include <sys/cons.h> 50 #include <sys/proc.h> 51 #include <sys/reboot.h> 52 #include <sys/sbuf.h> 53 #include <geom/geom.h> 54 #include <geom/geom_disk.h> 55 #endif /* _KERNEL */ 56 57 #ifndef _KERNEL 58 #include <stdio.h> 59 #include <string.h> 60 #endif /* _KERNEL */ 61 62 #include <cam/cam.h> 63 #include <cam/cam_ccb.h> 64 #include <cam/cam_periph.h> 65 #include <cam/cam_xpt_periph.h> 66 #include <cam/scsi/scsi_all.h> 67 #include <cam/scsi/scsi_da.h> 68 #include <cam/cam_sim.h> 69 #include <cam/cam_iosched.h> 70 71 #include <cam/ata/ata_all.h> 72 73 #ifdef _KERNEL 74 75 #define ATA_MAX_28BIT_LBA 268435455UL 76 77 extern int iosched_debug; 78 79 typedef enum { 80 ADA_STATE_RAHEAD, 81 ADA_STATE_WCACHE, 82 ADA_STATE_LOGDIR, 83 ADA_STATE_IDDIR, 84 ADA_STATE_SUP_CAP, 85 ADA_STATE_ZONE, 86 ADA_STATE_NORMAL 87 } ada_state; 88 89 typedef enum { 90 ADA_FLAG_CAN_48BIT = 0x00000002, 91 ADA_FLAG_CAN_FLUSHCACHE = 0x00000004, 92 ADA_FLAG_CAN_NCQ = 0x00000008, 93 ADA_FLAG_CAN_DMA = 0x00000010, 94 ADA_FLAG_NEED_OTAG = 0x00000020, 95 ADA_FLAG_WAS_OTAG = 0x00000040, 96 ADA_FLAG_CAN_TRIM = 0x00000080, 97 ADA_FLAG_OPEN = 0x00000100, 98 ADA_FLAG_SCTX_INIT = 0x00000200, 99 ADA_FLAG_CAN_CFA = 0x00000400, 100 ADA_FLAG_CAN_POWERMGT = 0x00000800, 101 ADA_FLAG_CAN_DMA48 = 0x00001000, 102 ADA_FLAG_CAN_LOG = 0x00002000, 103 ADA_FLAG_CAN_IDLOG = 0x00004000, 104 ADA_FLAG_CAN_SUPCAP = 0x00008000, 105 ADA_FLAG_CAN_ZONE = 0x00010000, 106 ADA_FLAG_CAN_WCACHE = 0x00020000, 107 ADA_FLAG_CAN_RAHEAD = 0x00040000, 108 ADA_FLAG_PROBED = 0x00080000, 109 ADA_FLAG_ANNOUNCED = 0x00100000, 110 ADA_FLAG_DIRTY = 0x00200000, 111 ADA_FLAG_CAN_NCQ_TRIM = 0x00400000, /* CAN_TRIM also set */ 112 ADA_FLAG_PIM_ATA_EXT = 0x00800000, 113 ADA_FLAG_UNMAPPEDIO = 0x01000000, 114 ADA_FLAG_ROTATING = 0x02000000 115 } ada_flags; 116 #define ADA_FLAG_STRING \ 117 "\020" \ 118 "\002CAN_48BIT" \ 119 "\003CAN_FLUSHCACHE" \ 120 "\004CAN_NCQ" \ 121 "\005CAN_DMA" \ 122 "\006NEED_OTAG" \ 123 "\007WAS_OTAG" \ 124 "\010CAN_TRIM" \ 125 "\011OPEN" \ 126 "\012SCTX_INIT" \ 127 "\013CAN_CFA" \ 128 "\014CAN_POWERMGT" \ 129 "\015CAN_DMA48" \ 130 "\016CAN_LOG" \ 131 "\017CAN_IDLOG" \ 132 "\020CAN_SUPCAP" \ 133 "\021CAN_ZONE" \ 134 "\022CAN_WCACHE" \ 135 "\023CAN_RAHEAD" \ 136 "\024PROBED" \ 137 "\025ANNOUNCED" \ 138 "\026DIRTY" \ 139 "\027CAN_NCQ_TRIM" \ 140 "\030PIM_ATA_EXT" \ 141 "\031UNMAPPEDIO" \ 142 "\032ROTATING" 143 144 typedef enum { 145 ADA_Q_NONE = 0x00, 146 ADA_Q_4K = 0x01, 147 ADA_Q_NCQ_TRIM_BROKEN = 0x02, 148 ADA_Q_LOG_BROKEN = 0x04, 149 ADA_Q_SMR_DM = 0x08, 150 ADA_Q_NO_TRIM = 0x10, 151 ADA_Q_128KB = 0x20 152 } ada_quirks; 153 154 #define ADA_Q_BIT_STRING \ 155 "\020" \ 156 "\0014K" \ 157 "\002NCQ_TRIM_BROKEN" \ 158 "\003LOG_BROKEN" \ 159 "\004SMR_DM" \ 160 "\005NO_TRIM" \ 161 "\006128KB" 162 163 typedef enum { 164 ADA_CCB_RAHEAD = 0x01, 165 ADA_CCB_WCACHE = 0x02, 166 ADA_CCB_BUFFER_IO = 0x03, 167 ADA_CCB_DUMP = 0x05, 168 ADA_CCB_TRIM = 0x06, 169 ADA_CCB_LOGDIR = 0x07, 170 ADA_CCB_IDDIR = 0x08, 171 ADA_CCB_SUP_CAP = 0x09, 172 ADA_CCB_ZONE = 0x0a, 173 ADA_CCB_TYPE_MASK = 0x0F, 174 } ada_ccb_state; 175 176 typedef enum { 177 ADA_ZONE_NONE = 0x00, 178 ADA_ZONE_DRIVE_MANAGED = 0x01, 179 ADA_ZONE_HOST_AWARE = 0x02, 180 ADA_ZONE_HOST_MANAGED = 0x03 181 } ada_zone_mode; 182 183 typedef enum { 184 ADA_ZONE_FLAG_RZ_SUP = 0x0001, 185 ADA_ZONE_FLAG_OPEN_SUP = 0x0002, 186 ADA_ZONE_FLAG_CLOSE_SUP = 0x0004, 187 ADA_ZONE_FLAG_FINISH_SUP = 0x0008, 188 ADA_ZONE_FLAG_RWP_SUP = 0x0010, 189 ADA_ZONE_FLAG_SUP_MASK = (ADA_ZONE_FLAG_RZ_SUP | 190 ADA_ZONE_FLAG_OPEN_SUP | 191 ADA_ZONE_FLAG_CLOSE_SUP | 192 ADA_ZONE_FLAG_FINISH_SUP | 193 ADA_ZONE_FLAG_RWP_SUP), 194 ADA_ZONE_FLAG_URSWRZ = 0x0020, 195 ADA_ZONE_FLAG_OPT_SEQ_SET = 0x0040, 196 ADA_ZONE_FLAG_OPT_NONSEQ_SET = 0x0080, 197 ADA_ZONE_FLAG_MAX_SEQ_SET = 0x0100, 198 ADA_ZONE_FLAG_SET_MASK = (ADA_ZONE_FLAG_OPT_SEQ_SET | 199 ADA_ZONE_FLAG_OPT_NONSEQ_SET | 200 ADA_ZONE_FLAG_MAX_SEQ_SET) 201 } ada_zone_flags; 202 203 static struct ada_zone_desc { 204 ada_zone_flags value; 205 const char *desc; 206 } ada_zone_desc_table[] = { 207 {ADA_ZONE_FLAG_RZ_SUP, "Report Zones" }, 208 {ADA_ZONE_FLAG_OPEN_SUP, "Open" }, 209 {ADA_ZONE_FLAG_CLOSE_SUP, "Close" }, 210 {ADA_ZONE_FLAG_FINISH_SUP, "Finish" }, 211 {ADA_ZONE_FLAG_RWP_SUP, "Reset Write Pointer" }, 212 }; 213 214 /* Offsets into our private area for storing information */ 215 #define ccb_state ppriv_field0 216 #define ccb_bp ppriv_ptr1 217 218 typedef enum { 219 ADA_DELETE_NONE, 220 ADA_DELETE_DISABLE, 221 ADA_DELETE_CFA_ERASE, 222 ADA_DELETE_DSM_TRIM, 223 ADA_DELETE_NCQ_DSM_TRIM, 224 ADA_DELETE_MIN = ADA_DELETE_CFA_ERASE, 225 ADA_DELETE_MAX = ADA_DELETE_NCQ_DSM_TRIM, 226 } ada_delete_methods; 227 228 static const char *ada_delete_method_names[] = 229 { "NONE", "DISABLE", "CFA_ERASE", "DSM_TRIM", "NCQ_DSM_TRIM" }; 230 #if 0 231 static const char *ada_delete_method_desc[] = 232 { "NONE", "DISABLED", "CFA Erase", "DSM Trim", "DSM Trim via NCQ" }; 233 #endif 234 235 struct disk_params { 236 u_int8_t heads; 237 u_int8_t secs_per_track; 238 u_int32_t cylinders; 239 u_int32_t secsize; /* Number of bytes/logical sector */ 240 u_int64_t sectors; /* Total number sectors */ 241 }; 242 243 #define TRIM_MAX_BLOCKS 8 244 #define TRIM_MAX_RANGES (TRIM_MAX_BLOCKS * ATA_DSM_BLK_RANGES) 245 struct trim_request { 246 uint8_t data[TRIM_MAX_RANGES * ATA_DSM_RANGE_SIZE]; 247 TAILQ_HEAD(, bio) bps; 248 }; 249 250 struct ada_softc { 251 struct cam_iosched_softc *cam_iosched; 252 int outstanding_cmds; /* Number of active commands */ 253 int refcount; /* Active xpt_action() calls */ 254 ada_state state; 255 ada_flags flags; 256 ada_zone_mode zone_mode; 257 ada_zone_flags zone_flags; 258 struct ata_gp_log_dir ata_logdir; 259 int valid_logdir_len; 260 struct ata_identify_log_pages ata_iddir; 261 int valid_iddir_len; 262 uint64_t optimal_seq_zones; 263 uint64_t optimal_nonseq_zones; 264 uint64_t max_seq_zones; 265 ada_quirks quirks; 266 ada_delete_methods delete_method; 267 int trim_max_ranges; 268 int read_ahead; 269 int write_cache; 270 #ifdef CAM_TEST_FAILURE 271 int force_read_error; 272 int force_write_error; 273 int periodic_read_error; 274 int periodic_read_count; 275 #endif 276 struct ccb_pathinq cpi; 277 struct disk_params params; 278 struct disk *disk; 279 struct task sysctl_task; 280 struct sysctl_ctx_list sysctl_ctx; 281 struct sysctl_oid *sysctl_tree; 282 struct callout sendordered_c; 283 struct trim_request trim_req; 284 uint64_t trim_count; 285 uint64_t trim_ranges; 286 uint64_t trim_lbas; 287 #ifdef CAM_IO_STATS 288 struct sysctl_ctx_list sysctl_stats_ctx; 289 struct sysctl_oid *sysctl_stats_tree; 290 u_int timeouts; 291 u_int errors; 292 u_int invalidations; 293 #endif 294 #define ADA_ANNOUNCETMP_SZ 80 295 char announce_temp[ADA_ANNOUNCETMP_SZ]; 296 #define ADA_ANNOUNCE_SZ 400 297 char announce_buffer[ADA_ANNOUNCE_SZ]; 298 }; 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, u_int32_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, u_int32_t cam_flags, 858 u_int32_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 /* 898 * Most platforms map firmware geometry to actual, but some don't. If 899 * not overridden, default to nothing. 900 */ 901 #ifndef ata_disk_firmware_geom_adjust 902 #define ata_disk_firmware_geom_adjust(disk) 903 #endif 904 905 static int ada_retry_count = ADA_DEFAULT_RETRY; 906 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT; 907 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED; 908 static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN; 909 static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND; 910 static int ada_read_ahead = ADA_DEFAULT_READ_AHEAD; 911 static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE; 912 static int ada_enable_biospeedup = 1; 913 914 static SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 915 "CAM Direct Access Disk driver"); 916 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RWTUN, 917 &ada_retry_count, 0, "Normal I/O retry count"); 918 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RWTUN, 919 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)"); 920 SYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RWTUN, 921 &ada_send_ordered, 0, "Send Ordered Tags"); 922 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RWTUN, 923 &ada_spindown_shutdown, 0, "Spin down upon shutdown"); 924 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RWTUN, 925 &ada_spindown_suspend, 0, "Spin down upon suspend"); 926 SYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RWTUN, 927 &ada_read_ahead, 0, "Enable disk read-ahead"); 928 SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RWTUN, 929 &ada_write_cache, 0, "Enable disk write cache"); 930 SYSCTL_INT(_kern_cam_ada, OID_AUTO, enable_biospeedup, CTLFLAG_RDTUN, 931 &ada_enable_biospeedup, 0, "Enable BIO_SPEEDUP processing"); 932 933 /* 934 * ADA_ORDEREDTAG_INTERVAL determines how often, relative 935 * to the default timeout, we check to see whether an ordered 936 * tagged transaction is appropriate to prevent simple tag 937 * starvation. Since we'd like to ensure that there is at least 938 * 1/2 of the timeout length left for a starved transaction to 939 * complete after we've sent an ordered tag, we must poll at least 940 * four times in every timeout period. This takes care of the worst 941 * case where a starved transaction starts during an interval that 942 * meets the requirement "don't send an ordered tag" test so it takes 943 * us two intervals to determine that a tag must be sent. 944 */ 945 #ifndef ADA_ORDEREDTAG_INTERVAL 946 #define ADA_ORDEREDTAG_INTERVAL 4 947 #endif 948 949 static struct periph_driver adadriver = 950 { 951 adainit, "ada", 952 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0 953 }; 954 955 static int adadeletemethodsysctl(SYSCTL_HANDLER_ARGS); 956 957 PERIPHDRIVER_DECLARE(ada, adadriver); 958 959 static MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers"); 960 961 static int 962 adaopen(struct disk *dp) 963 { 964 struct cam_periph *periph; 965 struct ada_softc *softc; 966 int error; 967 968 periph = (struct cam_periph *)dp->d_drv1; 969 if (cam_periph_acquire(periph) != 0) { 970 return(ENXIO); 971 } 972 973 cam_periph_lock(periph); 974 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) { 975 cam_periph_unlock(periph); 976 cam_periph_release(periph); 977 return (error); 978 } 979 980 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 981 ("adaopen\n")); 982 983 softc = (struct ada_softc *)periph->softc; 984 softc->flags |= ADA_FLAG_OPEN; 985 986 cam_periph_unhold(periph); 987 cam_periph_unlock(periph); 988 return (0); 989 } 990 991 static int 992 adaclose(struct disk *dp) 993 { 994 struct cam_periph *periph; 995 struct ada_softc *softc; 996 union ccb *ccb; 997 int error; 998 999 periph = (struct cam_periph *)dp->d_drv1; 1000 softc = (struct ada_softc *)periph->softc; 1001 cam_periph_lock(periph); 1002 1003 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 1004 ("adaclose\n")); 1005 1006 /* We only sync the cache if the drive is capable of it. */ 1007 if ((softc->flags & ADA_FLAG_DIRTY) != 0 && 1008 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 && 1009 (periph->flags & CAM_PERIPH_INVALID) == 0 && 1010 cam_periph_hold(periph, PRIBIO) == 0) { 1011 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1012 cam_fill_ataio(&ccb->ataio, 1013 1, 1014 NULL, 1015 CAM_DIR_NONE, 1016 0, 1017 NULL, 1018 0, 1019 ada_default_timeout*1000); 1020 1021 if (softc->flags & ADA_FLAG_CAN_48BIT) 1022 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1023 else 1024 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 1025 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 1026 /*sense_flags*/0, softc->disk->d_devstat); 1027 1028 if (error != 0) 1029 xpt_print(periph->path, "Synchronize cache failed\n"); 1030 softc->flags &= ~ADA_FLAG_DIRTY; 1031 xpt_release_ccb(ccb); 1032 cam_periph_unhold(periph); 1033 } 1034 1035 softc->flags &= ~ADA_FLAG_OPEN; 1036 1037 while (softc->refcount != 0) 1038 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "adaclose", 1); 1039 cam_periph_unlock(periph); 1040 cam_periph_release(periph); 1041 return (0); 1042 } 1043 1044 static void 1045 adaschedule(struct cam_periph *periph) 1046 { 1047 struct ada_softc *softc = (struct ada_softc *)periph->softc; 1048 1049 if (softc->state != ADA_STATE_NORMAL) 1050 return; 1051 1052 cam_iosched_schedule(softc->cam_iosched, periph); 1053 } 1054 1055 /* 1056 * Actually translate the requested transfer into one the physical driver 1057 * can understand. The transfer is described by a buf and will include 1058 * only one physical transfer. 1059 */ 1060 static void 1061 adastrategy(struct bio *bp) 1062 { 1063 struct cam_periph *periph; 1064 struct ada_softc *softc; 1065 1066 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 1067 softc = (struct ada_softc *)periph->softc; 1068 1069 cam_periph_lock(periph); 1070 1071 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp)); 1072 1073 /* 1074 * If the device has been made invalid, error out 1075 */ 1076 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 1077 cam_periph_unlock(periph); 1078 biofinish(bp, NULL, ENXIO); 1079 return; 1080 } 1081 1082 /* 1083 * Zone commands must be ordered, because they can depend on the 1084 * effects of previously issued commands, and they may affect 1085 * commands after them. 1086 */ 1087 if (bp->bio_cmd == BIO_ZONE) 1088 bp->bio_flags |= BIO_ORDERED; 1089 1090 /* 1091 * Place it in the queue of disk activities for this disk 1092 */ 1093 cam_iosched_queue_work(softc->cam_iosched, bp); 1094 1095 /* 1096 * Schedule ourselves for performing the work. 1097 */ 1098 adaschedule(periph); 1099 cam_periph_unlock(periph); 1100 1101 return; 1102 } 1103 1104 static int 1105 adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) 1106 { 1107 struct cam_periph *periph; 1108 struct ada_softc *softc; 1109 u_int secsize; 1110 struct ccb_ataio ataio; 1111 struct disk *dp; 1112 uint64_t lba; 1113 uint16_t count; 1114 int error = 0; 1115 1116 dp = arg; 1117 periph = dp->d_drv1; 1118 softc = (struct ada_softc *)periph->softc; 1119 secsize = softc->params.secsize; 1120 lba = offset / secsize; 1121 count = length / secsize; 1122 if ((periph->flags & CAM_PERIPH_INVALID) != 0) 1123 return (ENXIO); 1124 1125 memset(&ataio, 0, sizeof(ataio)); 1126 if (length > 0) { 1127 xpt_setup_ccb(&ataio.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1128 ataio.ccb_h.ccb_state = ADA_CCB_DUMP; 1129 cam_fill_ataio(&ataio, 1130 0, 1131 NULL, 1132 CAM_DIR_OUT, 1133 0, 1134 (u_int8_t *) virtual, 1135 length, 1136 ada_default_timeout*1000); 1137 if ((softc->flags & ADA_FLAG_CAN_48BIT) && 1138 (lba + count >= ATA_MAX_28BIT_LBA || 1139 count >= 256)) { 1140 ata_48bit_cmd(&ataio, ATA_WRITE_DMA48, 1141 0, lba, count); 1142 } else { 1143 ata_28bit_cmd(&ataio, ATA_WRITE_DMA, 1144 0, lba, count); 1145 } 1146 error = cam_periph_runccb((union ccb *)&ataio, adaerror, 1147 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 1148 if (error != 0) 1149 printf("Aborting dump due to I/O error.\n"); 1150 1151 return (error); 1152 } 1153 1154 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) { 1155 xpt_setup_ccb(&ataio.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1156 1157 /* 1158 * Tell the drive to flush its internal cache. if we 1159 * can't flush in 5s we have big problems. No need to 1160 * wait the default 60s to detect problems. 1161 */ 1162 ataio.ccb_h.ccb_state = ADA_CCB_DUMP; 1163 cam_fill_ataio(&ataio, 1164 0, 1165 NULL, 1166 CAM_DIR_NONE, 1167 0, 1168 NULL, 1169 0, 1170 5*1000); 1171 1172 if (softc->flags & ADA_FLAG_CAN_48BIT) 1173 ata_48bit_cmd(&ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1174 else 1175 ata_28bit_cmd(&ataio, ATA_FLUSHCACHE, 0, 0, 0); 1176 error = cam_periph_runccb((union ccb *)&ataio, adaerror, 1177 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 1178 if (error != 0) 1179 xpt_print(periph->path, "Synchronize cache failed\n"); 1180 } 1181 return (error); 1182 } 1183 1184 static void 1185 adainit(void) 1186 { 1187 cam_status status; 1188 1189 /* 1190 * Install a global async callback. This callback will 1191 * receive async callbacks like "new device found". 1192 */ 1193 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL); 1194 1195 if (status != CAM_REQ_CMP) { 1196 printf("ada: Failed to attach master async callback " 1197 "due to status 0x%x!\n", status); 1198 } else if (ada_send_ordered) { 1199 /* Register our event handlers */ 1200 if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend, 1201 NULL, EVENTHANDLER_PRI_LAST)) == NULL) 1202 printf("adainit: power event registration failed!\n"); 1203 if ((EVENTHANDLER_REGISTER(power_resume, adaresume, 1204 NULL, EVENTHANDLER_PRI_LAST)) == NULL) 1205 printf("adainit: power event registration failed!\n"); 1206 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown, 1207 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 1208 printf("adainit: shutdown event registration failed!\n"); 1209 } 1210 } 1211 1212 /* 1213 * Callback from GEOM, called when it has finished cleaning up its 1214 * resources. 1215 */ 1216 static void 1217 adadiskgonecb(struct disk *dp) 1218 { 1219 struct cam_periph *periph; 1220 1221 periph = (struct cam_periph *)dp->d_drv1; 1222 1223 cam_periph_release(periph); 1224 } 1225 1226 static void 1227 adaoninvalidate(struct cam_periph *periph) 1228 { 1229 struct ada_softc *softc; 1230 1231 softc = (struct ada_softc *)periph->softc; 1232 1233 /* 1234 * De-register any async callbacks. 1235 */ 1236 xpt_register_async(0, adaasync, periph, periph->path); 1237 #ifdef CAM_IO_STATS 1238 softc->invalidations++; 1239 #endif 1240 1241 /* 1242 * Return all queued I/O with ENXIO. 1243 * XXX Handle any transactions queued to the card 1244 * with XPT_ABORT_CCB. 1245 */ 1246 cam_iosched_flush(softc->cam_iosched, NULL, ENXIO); 1247 1248 disk_gone(softc->disk); 1249 } 1250 1251 static void 1252 adacleanup(struct cam_periph *periph) 1253 { 1254 struct ada_softc *softc; 1255 1256 softc = (struct ada_softc *)periph->softc; 1257 1258 cam_periph_unlock(periph); 1259 1260 cam_iosched_fini(softc->cam_iosched); 1261 1262 /* 1263 * If we can't free the sysctl tree, oh well... 1264 */ 1265 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0) { 1266 #ifdef CAM_IO_STATS 1267 if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0) 1268 xpt_print(periph->path, 1269 "can't remove sysctl stats context\n"); 1270 #endif 1271 if (sysctl_ctx_free(&softc->sysctl_ctx) != 0) 1272 xpt_print(periph->path, 1273 "can't remove sysctl context\n"); 1274 } 1275 1276 disk_destroy(softc->disk); 1277 callout_drain(&softc->sendordered_c); 1278 free(softc, M_DEVBUF); 1279 cam_periph_lock(periph); 1280 } 1281 1282 static void 1283 adasetdeletemethod(struct ada_softc *softc) 1284 { 1285 1286 if (softc->flags & ADA_FLAG_CAN_NCQ_TRIM) 1287 softc->delete_method = ADA_DELETE_NCQ_DSM_TRIM; 1288 else if (softc->flags & ADA_FLAG_CAN_TRIM) 1289 softc->delete_method = ADA_DELETE_DSM_TRIM; 1290 else if ((softc->flags & ADA_FLAG_CAN_CFA) && !(softc->flags & ADA_FLAG_CAN_48BIT)) 1291 softc->delete_method = ADA_DELETE_CFA_ERASE; 1292 else 1293 softc->delete_method = ADA_DELETE_NONE; 1294 } 1295 1296 static void 1297 adaasync(void *callback_arg, u_int32_t code, 1298 struct cam_path *path, void *arg) 1299 { 1300 struct ccb_getdev cgd; 1301 struct cam_periph *periph; 1302 struct ada_softc *softc; 1303 1304 periph = (struct cam_periph *)callback_arg; 1305 switch (code) { 1306 case AC_FOUND_DEVICE: 1307 { 1308 struct ccb_getdev *cgd; 1309 cam_status status; 1310 1311 cgd = (struct ccb_getdev *)arg; 1312 if (cgd == NULL) 1313 break; 1314 1315 if (cgd->protocol != PROTO_ATA) 1316 break; 1317 1318 /* 1319 * Allocate a peripheral instance for 1320 * this device and start the probe 1321 * process. 1322 */ 1323 status = cam_periph_alloc(adaregister, adaoninvalidate, 1324 adacleanup, adastart, 1325 "ada", CAM_PERIPH_BIO, 1326 path, adaasync, 1327 AC_FOUND_DEVICE, cgd); 1328 1329 if (status != CAM_REQ_CMP 1330 && status != CAM_REQ_INPROG) 1331 printf("adaasync: Unable to attach to new device " 1332 "due to status 0x%x\n", status); 1333 break; 1334 } 1335 case AC_GETDEV_CHANGED: 1336 { 1337 softc = (struct ada_softc *)periph->softc; 1338 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1339 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1340 xpt_action((union ccb *)&cgd); 1341 1342 /* 1343 * Update our information based on the new Identify data. 1344 */ 1345 adasetflags(softc, &cgd); 1346 adasetgeom(softc, &cgd); 1347 disk_resize(softc->disk, M_NOWAIT); 1348 1349 cam_periph_async(periph, code, path, arg); 1350 break; 1351 } 1352 case AC_ADVINFO_CHANGED: 1353 { 1354 uintptr_t buftype; 1355 1356 buftype = (uintptr_t)arg; 1357 if (buftype == CDAI_TYPE_PHYS_PATH) { 1358 struct ada_softc *softc; 1359 1360 softc = periph->softc; 1361 disk_attr_changed(softc->disk, "GEOM::physpath", 1362 M_NOWAIT); 1363 } 1364 break; 1365 } 1366 case AC_SENT_BDR: 1367 case AC_BUS_RESET: 1368 { 1369 softc = (struct ada_softc *)periph->softc; 1370 cam_periph_async(periph, code, path, arg); 1371 if (softc->state != ADA_STATE_NORMAL) 1372 break; 1373 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1374 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1375 xpt_action((union ccb *)&cgd); 1376 if (ADA_RA >= 0 && softc->flags & ADA_FLAG_CAN_RAHEAD) 1377 softc->state = ADA_STATE_RAHEAD; 1378 else if (ADA_WC >= 0 && softc->flags & ADA_FLAG_CAN_WCACHE) 1379 softc->state = ADA_STATE_WCACHE; 1380 else if ((softc->flags & ADA_FLAG_CAN_LOG) 1381 && (softc->zone_mode != ADA_ZONE_NONE)) 1382 softc->state = ADA_STATE_LOGDIR; 1383 else 1384 break; 1385 if (cam_periph_acquire(periph) != 0) 1386 softc->state = ADA_STATE_NORMAL; 1387 else 1388 xpt_schedule(periph, CAM_PRIORITY_DEV); 1389 } 1390 default: 1391 cam_periph_async(periph, code, path, arg); 1392 break; 1393 } 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_NEEDGIANT, 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_NEEDGIANT, 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_NEEDGIANT, 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 * Register this media as a disk 1847 */ 1848 (void)cam_periph_hold(periph, PRIBIO); 1849 cam_periph_unlock(periph); 1850 snprintf(announce_buf, ADA_ANNOUNCETMP_SZ, 1851 "kern.cam.ada.%d.quirks", periph->unit_number); 1852 quirks = softc->quirks; 1853 TUNABLE_INT_FETCH(announce_buf, &quirks); 1854 softc->quirks = quirks; 1855 softc->read_ahead = -1; 1856 snprintf(announce_buf, ADA_ANNOUNCETMP_SZ, 1857 "kern.cam.ada.%d.read_ahead", periph->unit_number); 1858 TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead); 1859 softc->write_cache = -1; 1860 snprintf(announce_buf, ADA_ANNOUNCETMP_SZ, 1861 "kern.cam.ada.%d.write_cache", periph->unit_number); 1862 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache); 1863 1864 /* 1865 * Set support flags based on the Identify data and quirks. 1866 */ 1867 adasetflags(softc, cgd); 1868 if (softc->cpi.hba_misc & PIM_ATA_EXT) 1869 softc->flags |= ADA_FLAG_PIM_ATA_EXT; 1870 1871 /* Disable queue sorting for non-rotational media by default. */ 1872 if (cgd->ident_data.media_rotation_rate == ATA_RATE_NON_ROTATING) { 1873 softc->flags &= ~ADA_FLAG_ROTATING; 1874 } else { 1875 softc->flags |= ADA_FLAG_ROTATING; 1876 } 1877 cam_iosched_set_sort_queue(softc->cam_iosched, 1878 (softc->flags & ADA_FLAG_ROTATING) ? -1 : 0); 1879 softc->disk = disk_alloc(); 1880 adasetgeom(softc, cgd); 1881 softc->disk->d_devstat = devstat_new_entry(periph->periph_name, 1882 periph->unit_number, softc->params.secsize, 1883 DEVSTAT_ALL_SUPPORTED, 1884 DEVSTAT_TYPE_DIRECT | 1885 XPORT_DEVSTAT_TYPE(softc->cpi.transport), 1886 DEVSTAT_PRIORITY_DISK); 1887 softc->disk->d_open = adaopen; 1888 softc->disk->d_close = adaclose; 1889 softc->disk->d_strategy = adastrategy; 1890 softc->disk->d_getattr = adagetattr; 1891 softc->disk->d_dump = adadump; 1892 softc->disk->d_gone = adadiskgonecb; 1893 softc->disk->d_name = "ada"; 1894 softc->disk->d_drv1 = periph; 1895 softc->disk->d_unit = periph->unit_number; 1896 1897 /* 1898 * Acquire a reference to the periph before we register with GEOM. 1899 * We'll release this reference once GEOM calls us back (via 1900 * adadiskgonecb()) telling us that our provider has been freed. 1901 */ 1902 if (cam_periph_acquire(periph) != 0) { 1903 xpt_print(periph->path, "%s: lost periph during " 1904 "registration!\n", __func__); 1905 cam_periph_lock(periph); 1906 return (CAM_REQ_CMP_ERR); 1907 } 1908 disk_create(softc->disk, DISK_VERSION); 1909 cam_periph_lock(periph); 1910 1911 dp = &softc->params; 1912 snprintf(announce_buf, ADA_ANNOUNCETMP_SZ, 1913 "%juMB (%ju %u byte sectors)", 1914 ((uintmax_t)dp->secsize * dp->sectors) / (1024 * 1024), 1915 (uintmax_t)dp->sectors, dp->secsize); 1916 1917 sbuf_new(&sb, softc->announce_buffer, ADA_ANNOUNCE_SZ, SBUF_FIXEDLEN); 1918 xpt_announce_periph_sbuf(periph, &sb, announce_buf); 1919 xpt_announce_quirks_sbuf(periph, &sb, softc->quirks, ADA_Q_BIT_STRING); 1920 sbuf_finish(&sb); 1921 sbuf_putbuf(&sb); 1922 1923 /* 1924 * Create our sysctl variables, now that we know 1925 * we have successfully attached. 1926 */ 1927 if (cam_periph_acquire(periph) == 0) 1928 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task); 1929 1930 /* 1931 * Add async callbacks for bus reset and 1932 * bus device reset calls. I don't bother 1933 * checking if this fails as, in most cases, 1934 * the system will function just fine without 1935 * them and the only alternative would be to 1936 * not attach the device on failure. 1937 */ 1938 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE | 1939 AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED, 1940 adaasync, periph, periph->path); 1941 1942 /* 1943 * Schedule a periodic event to occasionally send an 1944 * ordered tag to a device. 1945 */ 1946 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0); 1947 callout_reset(&softc->sendordered_c, 1948 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 1949 adasendorderedtag, softc); 1950 1951 if (ADA_RA >= 0 && softc->flags & ADA_FLAG_CAN_RAHEAD) { 1952 softc->state = ADA_STATE_RAHEAD; 1953 } else if (ADA_WC >= 0 && softc->flags & ADA_FLAG_CAN_WCACHE) { 1954 softc->state = ADA_STATE_WCACHE; 1955 } else if ((softc->flags & ADA_FLAG_CAN_LOG) 1956 && (softc->zone_mode != ADA_ZONE_NONE)) { 1957 softc->state = ADA_STATE_LOGDIR; 1958 } else { 1959 /* 1960 * Nothing to probe, so we can just transition to the 1961 * normal state. 1962 */ 1963 adaprobedone(periph, NULL); 1964 return(CAM_REQ_CMP); 1965 } 1966 1967 xpt_schedule(periph, CAM_PRIORITY_DEV); 1968 1969 return(CAM_REQ_CMP); 1970 } 1971 1972 static int 1973 ada_dsmtrim_req_create(struct ada_softc *softc, struct bio *bp, struct trim_request *req) 1974 { 1975 uint64_t lastlba = (uint64_t)-1, lbas = 0; 1976 int c, lastcount = 0, off, ranges = 0; 1977 1978 bzero(req, sizeof(*req)); 1979 TAILQ_INIT(&req->bps); 1980 do { 1981 uint64_t lba = bp->bio_pblkno; 1982 int count = bp->bio_bcount / softc->params.secsize; 1983 1984 /* Try to extend the previous range. */ 1985 if (lba == lastlba) { 1986 c = min(count, ATA_DSM_RANGE_MAX - lastcount); 1987 lastcount += c; 1988 off = (ranges - 1) * ATA_DSM_RANGE_SIZE; 1989 req->data[off + 6] = lastcount & 0xff; 1990 req->data[off + 7] = 1991 (lastcount >> 8) & 0xff; 1992 count -= c; 1993 lba += c; 1994 lbas += c; 1995 } 1996 1997 while (count > 0) { 1998 c = min(count, ATA_DSM_RANGE_MAX); 1999 off = ranges * ATA_DSM_RANGE_SIZE; 2000 req->data[off + 0] = lba & 0xff; 2001 req->data[off + 1] = (lba >> 8) & 0xff; 2002 req->data[off + 2] = (lba >> 16) & 0xff; 2003 req->data[off + 3] = (lba >> 24) & 0xff; 2004 req->data[off + 4] = (lba >> 32) & 0xff; 2005 req->data[off + 5] = (lba >> 40) & 0xff; 2006 req->data[off + 6] = c & 0xff; 2007 req->data[off + 7] = (c >> 8) & 0xff; 2008 lba += c; 2009 lbas += c; 2010 count -= c; 2011 lastcount = c; 2012 ranges++; 2013 /* 2014 * Its the caller's responsibility to ensure the 2015 * request will fit so we don't need to check for 2016 * overrun here 2017 */ 2018 } 2019 lastlba = lba; 2020 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue); 2021 2022 bp = cam_iosched_next_trim(softc->cam_iosched); 2023 if (bp == NULL) 2024 break; 2025 if (bp->bio_bcount / softc->params.secsize > 2026 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) { 2027 cam_iosched_put_back_trim(softc->cam_iosched, bp); 2028 break; 2029 } 2030 } while (1); 2031 softc->trim_count++; 2032 softc->trim_ranges += ranges; 2033 softc->trim_lbas += lbas; 2034 2035 return (ranges); 2036 } 2037 2038 static void 2039 ada_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 2040 { 2041 struct trim_request *req = &softc->trim_req; 2042 int ranges; 2043 2044 ranges = ada_dsmtrim_req_create(softc, bp, req); 2045 cam_fill_ataio(ataio, 2046 ada_retry_count, 2047 adadone, 2048 CAM_DIR_OUT, 2049 0, 2050 req->data, 2051 howmany(ranges, ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE, 2052 ada_default_timeout * 1000); 2053 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT, 2054 ATA_DSM_TRIM, 0, howmany(ranges, ATA_DSM_BLK_RANGES)); 2055 } 2056 2057 static void 2058 ada_ncq_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 2059 { 2060 struct trim_request *req = &softc->trim_req; 2061 int ranges; 2062 2063 ranges = ada_dsmtrim_req_create(softc, bp, req); 2064 cam_fill_ataio(ataio, 2065 ada_retry_count, 2066 adadone, 2067 CAM_DIR_OUT, 2068 0, 2069 req->data, 2070 howmany(ranges, ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE, 2071 ada_default_timeout * 1000); 2072 ata_ncq_cmd(ataio, 2073 ATA_SEND_FPDMA_QUEUED, 2074 0, 2075 howmany(ranges, ATA_DSM_BLK_RANGES)); 2076 ataio->cmd.sector_count_exp = ATA_SFPDMA_DSM; 2077 ataio->ata_flags |= ATA_FLAG_AUX; 2078 ataio->aux = 1; 2079 } 2080 2081 static void 2082 ada_cfaerase(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 2083 { 2084 struct trim_request *req = &softc->trim_req; 2085 uint64_t lba = bp->bio_pblkno; 2086 uint16_t count = bp->bio_bcount / softc->params.secsize; 2087 2088 bzero(req, sizeof(*req)); 2089 TAILQ_INIT(&req->bps); 2090 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue); 2091 2092 cam_fill_ataio(ataio, 2093 ada_retry_count, 2094 adadone, 2095 CAM_DIR_NONE, 2096 0, 2097 NULL, 2098 0, 2099 ada_default_timeout*1000); 2100 2101 if (count >= 256) 2102 count = 0; 2103 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count); 2104 } 2105 2106 static int 2107 ada_zone_bio_to_ata(int disk_zone_cmd) 2108 { 2109 switch (disk_zone_cmd) { 2110 case DISK_ZONE_OPEN: 2111 return ATA_ZM_OPEN_ZONE; 2112 case DISK_ZONE_CLOSE: 2113 return ATA_ZM_CLOSE_ZONE; 2114 case DISK_ZONE_FINISH: 2115 return ATA_ZM_FINISH_ZONE; 2116 case DISK_ZONE_RWP: 2117 return ATA_ZM_RWP; 2118 } 2119 2120 return -1; 2121 } 2122 2123 static int 2124 ada_zone_cmd(struct cam_periph *periph, union ccb *ccb, struct bio *bp, 2125 int *queue_ccb) 2126 { 2127 struct ada_softc *softc; 2128 int error; 2129 2130 error = 0; 2131 2132 if (bp->bio_cmd != BIO_ZONE) { 2133 error = EINVAL; 2134 goto bailout; 2135 } 2136 2137 softc = periph->softc; 2138 2139 switch (bp->bio_zone.zone_cmd) { 2140 case DISK_ZONE_OPEN: 2141 case DISK_ZONE_CLOSE: 2142 case DISK_ZONE_FINISH: 2143 case DISK_ZONE_RWP: { 2144 int zone_flags; 2145 int zone_sa; 2146 uint64_t lba; 2147 2148 zone_sa = ada_zone_bio_to_ata(bp->bio_zone.zone_cmd); 2149 if (zone_sa == -1) { 2150 xpt_print(periph->path, "Cannot translate zone " 2151 "cmd %#x to ATA\n", bp->bio_zone.zone_cmd); 2152 error = EINVAL; 2153 goto bailout; 2154 } 2155 2156 zone_flags = 0; 2157 lba = bp->bio_zone.zone_params.rwp.id; 2158 2159 if (bp->bio_zone.zone_params.rwp.flags & 2160 DISK_ZONE_RWP_FLAG_ALL) 2161 zone_flags |= ZBC_OUT_ALL; 2162 2163 ata_zac_mgmt_out(&ccb->ataio, 2164 /*retries*/ ada_retry_count, 2165 /*cbfcnp*/ adadone, 2166 /*use_ncq*/ (softc->flags & 2167 ADA_FLAG_PIM_ATA_EXT) ? 1 : 0, 2168 /*zm_action*/ zone_sa, 2169 /*zone_id*/ lba, 2170 /*zone_flags*/ zone_flags, 2171 /*sector_count*/ 0, 2172 /*data_ptr*/ NULL, 2173 /*dxfer_len*/ 0, 2174 /*timeout*/ ada_default_timeout * 1000); 2175 *queue_ccb = 1; 2176 2177 break; 2178 } 2179 case DISK_ZONE_REPORT_ZONES: { 2180 uint8_t *rz_ptr; 2181 uint32_t num_entries, alloc_size; 2182 struct disk_zone_report *rep; 2183 2184 rep = &bp->bio_zone.zone_params.report; 2185 2186 num_entries = rep->entries_allocated; 2187 if (num_entries == 0) { 2188 xpt_print(periph->path, "No entries allocated for " 2189 "Report Zones request\n"); 2190 error = EINVAL; 2191 goto bailout; 2192 } 2193 alloc_size = sizeof(struct scsi_report_zones_hdr) + 2194 (sizeof(struct scsi_report_zones_desc) * num_entries); 2195 alloc_size = min(alloc_size, softc->disk->d_maxsize); 2196 rz_ptr = malloc(alloc_size, M_ATADA, M_NOWAIT | M_ZERO); 2197 if (rz_ptr == NULL) { 2198 xpt_print(periph->path, "Unable to allocate memory " 2199 "for Report Zones request\n"); 2200 error = ENOMEM; 2201 goto bailout; 2202 } 2203 2204 ata_zac_mgmt_in(&ccb->ataio, 2205 /*retries*/ ada_retry_count, 2206 /*cbcfnp*/ adadone, 2207 /*use_ncq*/ (softc->flags & 2208 ADA_FLAG_PIM_ATA_EXT) ? 1 : 0, 2209 /*zm_action*/ ATA_ZM_REPORT_ZONES, 2210 /*zone_id*/ rep->starting_id, 2211 /*zone_flags*/ rep->rep_options, 2212 /*data_ptr*/ rz_ptr, 2213 /*dxfer_len*/ alloc_size, 2214 /*timeout*/ ada_default_timeout * 1000); 2215 2216 /* 2217 * For BIO_ZONE, this isn't normally needed. However, it 2218 * is used by devstat_end_transaction_bio() to determine 2219 * how much data was transferred. 2220 */ 2221 /* 2222 * XXX KDM we have a problem. But I'm not sure how to fix 2223 * it. devstat uses bio_bcount - bio_resid to calculate 2224 * the amount of data transferred. The GEOM disk code 2225 * uses bio_length - bio_resid to calculate the amount of 2226 * data in bio_completed. We have different structure 2227 * sizes above and below the ada(4) driver. So, if we 2228 * use the sizes above, the amount transferred won't be 2229 * quite accurate for devstat. If we use different sizes 2230 * for bio_bcount and bio_length (above and below 2231 * respectively), then the residual needs to match one or 2232 * the other. Everything is calculated after the bio 2233 * leaves the driver, so changing the values around isn't 2234 * really an option. For now, just set the count to the 2235 * passed in length. This means that the calculations 2236 * above (e.g. bio_completed) will be correct, but the 2237 * amount of data reported to devstat will be slightly 2238 * under or overstated. 2239 */ 2240 bp->bio_bcount = bp->bio_length; 2241 2242 *queue_ccb = 1; 2243 2244 break; 2245 } 2246 case DISK_ZONE_GET_PARAMS: { 2247 struct disk_zone_disk_params *params; 2248 2249 params = &bp->bio_zone.zone_params.disk_params; 2250 bzero(params, sizeof(*params)); 2251 2252 switch (softc->zone_mode) { 2253 case ADA_ZONE_DRIVE_MANAGED: 2254 params->zone_mode = DISK_ZONE_MODE_DRIVE_MANAGED; 2255 break; 2256 case ADA_ZONE_HOST_AWARE: 2257 params->zone_mode = DISK_ZONE_MODE_HOST_AWARE; 2258 break; 2259 case ADA_ZONE_HOST_MANAGED: 2260 params->zone_mode = DISK_ZONE_MODE_HOST_MANAGED; 2261 break; 2262 default: 2263 case ADA_ZONE_NONE: 2264 params->zone_mode = DISK_ZONE_MODE_NONE; 2265 break; 2266 } 2267 2268 if (softc->zone_flags & ADA_ZONE_FLAG_URSWRZ) 2269 params->flags |= DISK_ZONE_DISK_URSWRZ; 2270 2271 if (softc->zone_flags & ADA_ZONE_FLAG_OPT_SEQ_SET) { 2272 params->optimal_seq_zones = softc->optimal_seq_zones; 2273 params->flags |= DISK_ZONE_OPT_SEQ_SET; 2274 } 2275 2276 if (softc->zone_flags & ADA_ZONE_FLAG_OPT_NONSEQ_SET) { 2277 params->optimal_nonseq_zones = 2278 softc->optimal_nonseq_zones; 2279 params->flags |= DISK_ZONE_OPT_NONSEQ_SET; 2280 } 2281 2282 if (softc->zone_flags & ADA_ZONE_FLAG_MAX_SEQ_SET) { 2283 params->max_seq_zones = softc->max_seq_zones; 2284 params->flags |= DISK_ZONE_MAX_SEQ_SET; 2285 } 2286 if (softc->zone_flags & ADA_ZONE_FLAG_RZ_SUP) 2287 params->flags |= DISK_ZONE_RZ_SUP; 2288 2289 if (softc->zone_flags & ADA_ZONE_FLAG_OPEN_SUP) 2290 params->flags |= DISK_ZONE_OPEN_SUP; 2291 2292 if (softc->zone_flags & ADA_ZONE_FLAG_CLOSE_SUP) 2293 params->flags |= DISK_ZONE_CLOSE_SUP; 2294 2295 if (softc->zone_flags & ADA_ZONE_FLAG_FINISH_SUP) 2296 params->flags |= DISK_ZONE_FINISH_SUP; 2297 2298 if (softc->zone_flags & ADA_ZONE_FLAG_RWP_SUP) 2299 params->flags |= DISK_ZONE_RWP_SUP; 2300 break; 2301 } 2302 default: 2303 break; 2304 } 2305 bailout: 2306 return (error); 2307 } 2308 2309 static void 2310 adastart(struct cam_periph *periph, union ccb *start_ccb) 2311 { 2312 struct ada_softc *softc = (struct ada_softc *)periph->softc; 2313 struct ccb_ataio *ataio = &start_ccb->ataio; 2314 2315 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n")); 2316 2317 switch (softc->state) { 2318 case ADA_STATE_NORMAL: 2319 { 2320 struct bio *bp; 2321 u_int8_t tag_code; 2322 2323 bp = cam_iosched_next_bio(softc->cam_iosched); 2324 if (bp == NULL) { 2325 xpt_release_ccb(start_ccb); 2326 break; 2327 } 2328 2329 if ((bp->bio_flags & BIO_ORDERED) != 0 || 2330 (bp->bio_cmd != BIO_DELETE && (softc->flags & ADA_FLAG_NEED_OTAG) != 0)) { 2331 softc->flags &= ~ADA_FLAG_NEED_OTAG; 2332 softc->flags |= ADA_FLAG_WAS_OTAG; 2333 tag_code = 0; 2334 } else { 2335 tag_code = 1; 2336 } 2337 switch (bp->bio_cmd) { 2338 case BIO_WRITE: 2339 case BIO_READ: 2340 { 2341 uint64_t lba = bp->bio_pblkno; 2342 uint16_t count = bp->bio_bcount / softc->params.secsize; 2343 void *data_ptr; 2344 int rw_op; 2345 2346 if (bp->bio_cmd == BIO_WRITE) { 2347 softc->flags |= ADA_FLAG_DIRTY; 2348 rw_op = CAM_DIR_OUT; 2349 } else { 2350 rw_op = CAM_DIR_IN; 2351 } 2352 2353 data_ptr = bp->bio_data; 2354 if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) { 2355 rw_op |= CAM_DATA_BIO; 2356 data_ptr = bp; 2357 } 2358 2359 #ifdef CAM_TEST_FAILURE 2360 int fail = 0; 2361 2362 /* 2363 * Support the failure ioctls. If the command is a 2364 * read, and there are pending forced read errors, or 2365 * if a write and pending write errors, then fail this 2366 * operation with EIO. This is useful for testing 2367 * purposes. Also, support having every Nth read fail. 2368 * 2369 * This is a rather blunt tool. 2370 */ 2371 if (bp->bio_cmd == BIO_READ) { 2372 if (softc->force_read_error) { 2373 softc->force_read_error--; 2374 fail = 1; 2375 } 2376 if (softc->periodic_read_error > 0) { 2377 if (++softc->periodic_read_count >= 2378 softc->periodic_read_error) { 2379 softc->periodic_read_count = 0; 2380 fail = 1; 2381 } 2382 } 2383 } else { 2384 if (softc->force_write_error) { 2385 softc->force_write_error--; 2386 fail = 1; 2387 } 2388 } 2389 if (fail) { 2390 biofinish(bp, NULL, EIO); 2391 xpt_release_ccb(start_ccb); 2392 adaschedule(periph); 2393 return; 2394 } 2395 #endif 2396 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 || 2397 round_page(bp->bio_bcount + bp->bio_ma_offset) / 2398 PAGE_SIZE == bp->bio_ma_n, 2399 ("Short bio %p", bp)); 2400 cam_fill_ataio(ataio, 2401 ada_retry_count, 2402 adadone, 2403 rw_op, 2404 0, 2405 data_ptr, 2406 bp->bio_bcount, 2407 ada_default_timeout*1000); 2408 2409 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) { 2410 if (bp->bio_cmd == BIO_READ) { 2411 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED, 2412 lba, count); 2413 } else { 2414 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED, 2415 lba, count); 2416 } 2417 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) && 2418 (lba + count >= ATA_MAX_28BIT_LBA || 2419 count > 256)) { 2420 if (softc->flags & ADA_FLAG_CAN_DMA48) { 2421 if (bp->bio_cmd == BIO_READ) { 2422 ata_48bit_cmd(ataio, ATA_READ_DMA48, 2423 0, lba, count); 2424 } else { 2425 ata_48bit_cmd(ataio, ATA_WRITE_DMA48, 2426 0, lba, count); 2427 } 2428 } else { 2429 if (bp->bio_cmd == BIO_READ) { 2430 ata_48bit_cmd(ataio, ATA_READ_MUL48, 2431 0, lba, count); 2432 } else { 2433 ata_48bit_cmd(ataio, ATA_WRITE_MUL48, 2434 0, lba, count); 2435 } 2436 } 2437 } else { 2438 if (count == 256) 2439 count = 0; 2440 if (softc->flags & ADA_FLAG_CAN_DMA) { 2441 if (bp->bio_cmd == BIO_READ) { 2442 ata_28bit_cmd(ataio, ATA_READ_DMA, 2443 0, lba, count); 2444 } else { 2445 ata_28bit_cmd(ataio, ATA_WRITE_DMA, 2446 0, lba, count); 2447 } 2448 } else { 2449 if (bp->bio_cmd == BIO_READ) { 2450 ata_28bit_cmd(ataio, ATA_READ_MUL, 2451 0, lba, count); 2452 } else { 2453 ata_28bit_cmd(ataio, ATA_WRITE_MUL, 2454 0, lba, count); 2455 } 2456 } 2457 } 2458 break; 2459 } 2460 case BIO_DELETE: 2461 switch (softc->delete_method) { 2462 case ADA_DELETE_NCQ_DSM_TRIM: 2463 ada_ncq_dsmtrim(softc, bp, ataio); 2464 break; 2465 case ADA_DELETE_DSM_TRIM: 2466 ada_dsmtrim(softc, bp, ataio); 2467 break; 2468 case ADA_DELETE_CFA_ERASE: 2469 ada_cfaerase(softc, bp, ataio); 2470 break; 2471 default: 2472 biofinish(bp, NULL, EOPNOTSUPP); 2473 xpt_release_ccb(start_ccb); 2474 adaschedule(periph); 2475 return; 2476 } 2477 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM; 2478 start_ccb->ccb_h.flags |= CAM_UNLOCKED; 2479 cam_iosched_submit_trim(softc->cam_iosched); 2480 goto out; 2481 case BIO_FLUSH: 2482 cam_fill_ataio(ataio, 2483 1, 2484 adadone, 2485 CAM_DIR_NONE, 2486 0, 2487 NULL, 2488 0, 2489 ada_default_timeout*1000); 2490 2491 if (softc->flags & ADA_FLAG_CAN_48BIT) 2492 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0); 2493 else 2494 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0); 2495 break; 2496 case BIO_ZONE: { 2497 int error, queue_ccb; 2498 2499 queue_ccb = 0; 2500 2501 error = ada_zone_cmd(periph, start_ccb, bp, &queue_ccb); 2502 if ((error != 0) 2503 || (queue_ccb == 0)) { 2504 biofinish(bp, NULL, error); 2505 xpt_release_ccb(start_ccb); 2506 return; 2507 } 2508 break; 2509 } 2510 default: 2511 biofinish(bp, NULL, EOPNOTSUPP); 2512 xpt_release_ccb(start_ccb); 2513 return; 2514 } 2515 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO; 2516 start_ccb->ccb_h.flags |= CAM_UNLOCKED; 2517 out: 2518 start_ccb->ccb_h.ccb_bp = bp; 2519 softc->outstanding_cmds++; 2520 softc->refcount++; 2521 cam_periph_unlock(periph); 2522 xpt_action(start_ccb); 2523 cam_periph_lock(periph); 2524 2525 /* May have more work to do, so ensure we stay scheduled */ 2526 adaschedule(periph); 2527 break; 2528 } 2529 case ADA_STATE_RAHEAD: 2530 case ADA_STATE_WCACHE: 2531 { 2532 cam_fill_ataio(ataio, 2533 1, 2534 adadone, 2535 CAM_DIR_NONE, 2536 0, 2537 NULL, 2538 0, 2539 ada_default_timeout*1000); 2540 2541 if (softc->state == ADA_STATE_RAHEAD) { 2542 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ? 2543 ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0); 2544 start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD; 2545 } else { 2546 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ? 2547 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0); 2548 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE; 2549 } 2550 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 2551 xpt_action(start_ccb); 2552 break; 2553 } 2554 case ADA_STATE_LOGDIR: 2555 { 2556 struct ata_gp_log_dir *log_dir; 2557 2558 if ((softc->flags & ADA_FLAG_CAN_LOG) == 0) { 2559 adaprobedone(periph, start_ccb); 2560 break; 2561 } 2562 2563 log_dir = malloc(sizeof(*log_dir), M_ATADA, M_NOWAIT|M_ZERO); 2564 if (log_dir == NULL) { 2565 xpt_print(periph->path, "Couldn't malloc log_dir " 2566 "data\n"); 2567 softc->state = ADA_STATE_NORMAL; 2568 xpt_release_ccb(start_ccb); 2569 break; 2570 } 2571 2572 ata_read_log(ataio, 2573 /*retries*/1, 2574 /*cbfcnp*/adadone, 2575 /*log_address*/ ATA_LOG_DIRECTORY, 2576 /*page_number*/ 0, 2577 /*block_count*/ 1, 2578 /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ? 2579 CAM_ATAIO_DMA : 0, 2580 /*data_ptr*/ (uint8_t *)log_dir, 2581 /*dxfer_len*/sizeof(*log_dir), 2582 /*timeout*/ada_default_timeout*1000); 2583 2584 start_ccb->ccb_h.ccb_state = ADA_CCB_LOGDIR; 2585 xpt_action(start_ccb); 2586 break; 2587 } 2588 case ADA_STATE_IDDIR: 2589 { 2590 struct ata_identify_log_pages *id_dir; 2591 2592 id_dir = malloc(sizeof(*id_dir), M_ATADA, M_NOWAIT | M_ZERO); 2593 if (id_dir == NULL) { 2594 xpt_print(periph->path, "Couldn't malloc id_dir " 2595 "data\n"); 2596 adaprobedone(periph, start_ccb); 2597 break; 2598 } 2599 2600 ata_read_log(ataio, 2601 /*retries*/1, 2602 /*cbfcnp*/adadone, 2603 /*log_address*/ ATA_IDENTIFY_DATA_LOG, 2604 /*page_number*/ ATA_IDL_PAGE_LIST, 2605 /*block_count*/ 1, 2606 /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ? 2607 CAM_ATAIO_DMA : 0, 2608 /*data_ptr*/ (uint8_t *)id_dir, 2609 /*dxfer_len*/ sizeof(*id_dir), 2610 /*timeout*/ada_default_timeout*1000); 2611 2612 start_ccb->ccb_h.ccb_state = ADA_CCB_IDDIR; 2613 xpt_action(start_ccb); 2614 break; 2615 } 2616 case ADA_STATE_SUP_CAP: 2617 { 2618 struct ata_identify_log_sup_cap *sup_cap; 2619 2620 sup_cap = malloc(sizeof(*sup_cap), M_ATADA, M_NOWAIT|M_ZERO); 2621 if (sup_cap == NULL) { 2622 xpt_print(periph->path, "Couldn't malloc sup_cap " 2623 "data\n"); 2624 adaprobedone(periph, start_ccb); 2625 break; 2626 } 2627 2628 ata_read_log(ataio, 2629 /*retries*/1, 2630 /*cbfcnp*/adadone, 2631 /*log_address*/ ATA_IDENTIFY_DATA_LOG, 2632 /*page_number*/ ATA_IDL_SUP_CAP, 2633 /*block_count*/ 1, 2634 /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ? 2635 CAM_ATAIO_DMA : 0, 2636 /*data_ptr*/ (uint8_t *)sup_cap, 2637 /*dxfer_len*/ sizeof(*sup_cap), 2638 /*timeout*/ada_default_timeout*1000); 2639 2640 start_ccb->ccb_h.ccb_state = ADA_CCB_SUP_CAP; 2641 xpt_action(start_ccb); 2642 break; 2643 } 2644 case ADA_STATE_ZONE: 2645 { 2646 struct ata_zoned_info_log *ata_zone; 2647 2648 ata_zone = malloc(sizeof(*ata_zone), M_ATADA, M_NOWAIT|M_ZERO); 2649 if (ata_zone == NULL) { 2650 xpt_print(periph->path, "Couldn't malloc ata_zone " 2651 "data\n"); 2652 adaprobedone(periph, start_ccb); 2653 break; 2654 } 2655 2656 ata_read_log(ataio, 2657 /*retries*/1, 2658 /*cbfcnp*/adadone, 2659 /*log_address*/ ATA_IDENTIFY_DATA_LOG, 2660 /*page_number*/ ATA_IDL_ZDI, 2661 /*block_count*/ 1, 2662 /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ? 2663 CAM_ATAIO_DMA : 0, 2664 /*data_ptr*/ (uint8_t *)ata_zone, 2665 /*dxfer_len*/ sizeof(*ata_zone), 2666 /*timeout*/ada_default_timeout*1000); 2667 2668 start_ccb->ccb_h.ccb_state = ADA_CCB_ZONE; 2669 xpt_action(start_ccb); 2670 break; 2671 } 2672 } 2673 } 2674 2675 static void 2676 adaprobedone(struct cam_periph *periph, union ccb *ccb) 2677 { 2678 struct ada_softc *softc; 2679 2680 softc = (struct ada_softc *)periph->softc; 2681 2682 if (ccb != NULL) 2683 xpt_release_ccb(ccb); 2684 2685 softc->state = ADA_STATE_NORMAL; 2686 softc->flags |= ADA_FLAG_PROBED; 2687 adaschedule(periph); 2688 if ((softc->flags & ADA_FLAG_ANNOUNCED) == 0) { 2689 softc->flags |= ADA_FLAG_ANNOUNCED; 2690 cam_periph_unhold(periph); 2691 } else { 2692 cam_periph_release_locked(periph); 2693 } 2694 } 2695 2696 static void 2697 adazonedone(struct cam_periph *periph, union ccb *ccb) 2698 { 2699 struct bio *bp; 2700 2701 bp = (struct bio *)ccb->ccb_h.ccb_bp; 2702 2703 switch (bp->bio_zone.zone_cmd) { 2704 case DISK_ZONE_OPEN: 2705 case DISK_ZONE_CLOSE: 2706 case DISK_ZONE_FINISH: 2707 case DISK_ZONE_RWP: 2708 break; 2709 case DISK_ZONE_REPORT_ZONES: { 2710 uint32_t avail_len; 2711 struct disk_zone_report *rep; 2712 struct scsi_report_zones_hdr *hdr; 2713 struct scsi_report_zones_desc *desc; 2714 struct disk_zone_rep_entry *entry; 2715 uint32_t hdr_len, num_avail; 2716 uint32_t num_to_fill, i; 2717 2718 rep = &bp->bio_zone.zone_params.report; 2719 avail_len = ccb->ataio.dxfer_len - ccb->ataio.resid; 2720 /* 2721 * Note that bio_resid isn't normally used for zone 2722 * commands, but it is used by devstat_end_transaction_bio() 2723 * to determine how much data was transferred. Because 2724 * the size of the SCSI/ATA data structures is different 2725 * than the size of the BIO interface structures, the 2726 * amount of data actually transferred from the drive will 2727 * be different than the amount of data transferred to 2728 * the user. 2729 */ 2730 hdr = (struct scsi_report_zones_hdr *)ccb->ataio.data_ptr; 2731 if (avail_len < sizeof(*hdr)) { 2732 /* 2733 * Is there a better error than EIO here? We asked 2734 * for at least the header, and we got less than 2735 * that. 2736 */ 2737 bp->bio_error = EIO; 2738 bp->bio_flags |= BIO_ERROR; 2739 bp->bio_resid = bp->bio_bcount; 2740 break; 2741 } 2742 2743 hdr_len = le32dec(hdr->length); 2744 if (hdr_len > 0) 2745 rep->entries_available = hdr_len / sizeof(*desc); 2746 else 2747 rep->entries_available = 0; 2748 /* 2749 * NOTE: using the same values for the BIO version of the 2750 * same field as the SCSI/ATA values. This means we could 2751 * get some additional values that aren't defined in bio.h 2752 * if more values of the same field are defined later. 2753 */ 2754 rep->header.same = hdr->byte4 & SRZ_SAME_MASK; 2755 rep->header.maximum_lba = le64dec(hdr->maximum_lba); 2756 /* 2757 * If the drive reports no entries that match the query, 2758 * we're done. 2759 */ 2760 if (hdr_len == 0) { 2761 rep->entries_filled = 0; 2762 bp->bio_resid = bp->bio_bcount; 2763 break; 2764 } 2765 2766 num_avail = min((avail_len - sizeof(*hdr)) / sizeof(*desc), 2767 hdr_len / sizeof(*desc)); 2768 /* 2769 * If the drive didn't return any data, then we're done. 2770 */ 2771 if (num_avail == 0) { 2772 rep->entries_filled = 0; 2773 bp->bio_resid = bp->bio_bcount; 2774 break; 2775 } 2776 2777 num_to_fill = min(num_avail, rep->entries_allocated); 2778 /* 2779 * If the user didn't allocate any entries for us to fill, 2780 * we're done. 2781 */ 2782 if (num_to_fill == 0) { 2783 rep->entries_filled = 0; 2784 bp->bio_resid = bp->bio_bcount; 2785 break; 2786 } 2787 2788 for (i = 0, desc = &hdr->desc_list[0], entry=&rep->entries[0]; 2789 i < num_to_fill; i++, desc++, entry++) { 2790 /* 2791 * NOTE: we're mapping the values here directly 2792 * from the SCSI/ATA bit definitions to the bio.h 2793 * definitions. There is also a warning in 2794 * disk_zone.h, but the impact is that if 2795 * additional values are added in the SCSI/ATA 2796 * specs these will be visible to consumers of 2797 * this interface. 2798 */ 2799 entry->zone_type = desc->zone_type & SRZ_TYPE_MASK; 2800 entry->zone_condition = 2801 (desc->zone_flags & SRZ_ZONE_COND_MASK) >> 2802 SRZ_ZONE_COND_SHIFT; 2803 entry->zone_flags |= desc->zone_flags & 2804 (SRZ_ZONE_NON_SEQ|SRZ_ZONE_RESET); 2805 entry->zone_length = le64dec(desc->zone_length); 2806 entry->zone_start_lba = le64dec(desc->zone_start_lba); 2807 entry->write_pointer_lba = 2808 le64dec(desc->write_pointer_lba); 2809 } 2810 rep->entries_filled = num_to_fill; 2811 /* 2812 * Note that this residual is accurate from the user's 2813 * standpoint, but the amount transferred isn't accurate 2814 * from the standpoint of what actually came back from the 2815 * drive. 2816 */ 2817 bp->bio_resid = bp->bio_bcount - (num_to_fill * sizeof(*entry)); 2818 break; 2819 } 2820 case DISK_ZONE_GET_PARAMS: 2821 default: 2822 /* 2823 * In theory we should not get a GET_PARAMS bio, since it 2824 * should be handled without queueing the command to the 2825 * drive. 2826 */ 2827 panic("%s: Invalid zone command %d", __func__, 2828 bp->bio_zone.zone_cmd); 2829 break; 2830 } 2831 2832 if (bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES) 2833 free(ccb->ataio.data_ptr, M_ATADA); 2834 } 2835 2836 static void 2837 adadone(struct cam_periph *periph, union ccb *done_ccb) 2838 { 2839 struct ada_softc *softc; 2840 struct ccb_ataio *ataio; 2841 struct cam_path *path; 2842 uint32_t priority; 2843 int state; 2844 2845 softc = (struct ada_softc *)periph->softc; 2846 ataio = &done_ccb->ataio; 2847 path = done_ccb->ccb_h.path; 2848 priority = done_ccb->ccb_h.pinfo.priority; 2849 2850 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n")); 2851 2852 state = ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK; 2853 switch (state) { 2854 case ADA_CCB_BUFFER_IO: 2855 case ADA_CCB_TRIM: 2856 { 2857 struct bio *bp; 2858 int error; 2859 2860 cam_periph_lock(periph); 2861 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 2862 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2863 error = adaerror(done_ccb, 0, 0); 2864 if (error == ERESTART) { 2865 /* A retry was scheduled, so just return. */ 2866 cam_periph_unlock(periph); 2867 return; 2868 } 2869 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 2870 cam_release_devq(path, 2871 /*relsim_flags*/0, 2872 /*reduction*/0, 2873 /*timeout*/0, 2874 /*getcount_only*/0); 2875 /* 2876 * If we get an error on an NCQ DSM TRIM, fall back 2877 * to a non-NCQ DSM TRIM forever. Please note that if 2878 * CAN_NCQ_TRIM is set, CAN_TRIM is necessarily set too. 2879 * However, for this one trim, we treat it as advisory 2880 * and return success up the stack. 2881 */ 2882 if (state == ADA_CCB_TRIM && 2883 error != 0 && 2884 (softc->flags & ADA_FLAG_CAN_NCQ_TRIM) != 0) { 2885 softc->flags &= ~ADA_FLAG_CAN_NCQ_TRIM; 2886 error = 0; 2887 adasetdeletemethod(softc); 2888 } 2889 } else { 2890 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 2891 panic("REQ_CMP with QFRZN"); 2892 2893 error = 0; 2894 } 2895 bp->bio_error = error; 2896 if (error != 0) { 2897 bp->bio_resid = bp->bio_bcount; 2898 bp->bio_flags |= BIO_ERROR; 2899 } else { 2900 if (bp->bio_cmd == BIO_ZONE) 2901 adazonedone(periph, done_ccb); 2902 else if (state == ADA_CCB_TRIM) 2903 bp->bio_resid = 0; 2904 else 2905 bp->bio_resid = ataio->resid; 2906 2907 if ((bp->bio_resid > 0) 2908 && (bp->bio_cmd != BIO_ZONE)) 2909 bp->bio_flags |= BIO_ERROR; 2910 } 2911 softc->outstanding_cmds--; 2912 if (softc->outstanding_cmds == 0) 2913 softc->flags |= ADA_FLAG_WAS_OTAG; 2914 2915 /* 2916 * We need to call cam_iosched before we call biodone so that we 2917 * don't measure any activity that happens in the completion 2918 * routine, which in the case of sendfile can be quite 2919 * extensive. Release the periph refcount taken in adastart() 2920 * for each CCB. 2921 */ 2922 cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb); 2923 xpt_release_ccb(done_ccb); 2924 KASSERT(softc->refcount >= 1, ("adadone softc %p refcount %d", softc, softc->refcount)); 2925 softc->refcount--; 2926 if (state == ADA_CCB_TRIM) { 2927 TAILQ_HEAD(, bio) queue; 2928 struct bio *bp1; 2929 2930 TAILQ_INIT(&queue); 2931 TAILQ_CONCAT(&queue, &softc->trim_req.bps, bio_queue); 2932 /* 2933 * Normally, the xpt_release_ccb() above would make sure 2934 * that when we have more work to do, that work would 2935 * get kicked off. However, we specifically keep 2936 * trim_running set to 0 before the call above to allow 2937 * other I/O to progress when many BIO_DELETE requests 2938 * are pushed down. We set trim_running to 0 and call 2939 * daschedule again so that we don't stall if there are 2940 * no other I/Os pending apart from BIO_DELETEs. 2941 */ 2942 cam_iosched_trim_done(softc->cam_iosched); 2943 adaschedule(periph); 2944 cam_periph_unlock(periph); 2945 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) { 2946 TAILQ_REMOVE(&queue, bp1, bio_queue); 2947 bp1->bio_error = error; 2948 if (error != 0) { 2949 bp1->bio_flags |= BIO_ERROR; 2950 bp1->bio_resid = bp1->bio_bcount; 2951 } else 2952 bp1->bio_resid = 0; 2953 biodone(bp1); 2954 } 2955 } else { 2956 adaschedule(periph); 2957 cam_periph_unlock(periph); 2958 biodone(bp); 2959 } 2960 return; 2961 } 2962 case ADA_CCB_RAHEAD: 2963 { 2964 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2965 if (adaerror(done_ccb, 0, 0) == ERESTART) { 2966 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 2967 cam_release_devq(path, 0, 0, 0, FALSE); 2968 return; 2969 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 2970 cam_release_devq(path, 2971 /*relsim_flags*/0, 2972 /*reduction*/0, 2973 /*timeout*/0, 2974 /*getcount_only*/0); 2975 } 2976 } 2977 2978 /* 2979 * Since our peripheral may be invalidated by an error 2980 * above or an external event, we must release our CCB 2981 * before releasing the reference on the peripheral. 2982 * The peripheral will only go away once the last reference 2983 * is removed, and we need it around for the CCB release 2984 * operation. 2985 */ 2986 2987 xpt_release_ccb(done_ccb); 2988 softc->state = ADA_STATE_WCACHE; 2989 xpt_schedule(periph, priority); 2990 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 2991 cam_release_devq(path, 0, 0, 0, FALSE); 2992 return; 2993 } 2994 case ADA_CCB_WCACHE: 2995 { 2996 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2997 if (adaerror(done_ccb, 0, 0) == ERESTART) { 2998 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 2999 cam_release_devq(path, 0, 0, 0, FALSE); 3000 return; 3001 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 3002 cam_release_devq(path, 3003 /*relsim_flags*/0, 3004 /*reduction*/0, 3005 /*timeout*/0, 3006 /*getcount_only*/0); 3007 } 3008 } 3009 3010 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 3011 cam_release_devq(path, 0, 0, 0, FALSE); 3012 3013 if ((softc->flags & ADA_FLAG_CAN_LOG) 3014 && (softc->zone_mode != ADA_ZONE_NONE)) { 3015 xpt_release_ccb(done_ccb); 3016 softc->state = ADA_STATE_LOGDIR; 3017 xpt_schedule(periph, priority); 3018 } else { 3019 adaprobedone(periph, done_ccb); 3020 } 3021 return; 3022 } 3023 case ADA_CCB_LOGDIR: 3024 { 3025 int error; 3026 3027 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 3028 error = 0; 3029 softc->valid_logdir_len = 0; 3030 bzero(&softc->ata_logdir, sizeof(softc->ata_logdir)); 3031 softc->valid_logdir_len = 3032 ataio->dxfer_len - ataio->resid; 3033 if (softc->valid_logdir_len > 0) 3034 bcopy(ataio->data_ptr, &softc->ata_logdir, 3035 min(softc->valid_logdir_len, 3036 sizeof(softc->ata_logdir))); 3037 /* 3038 * Figure out whether the Identify Device log is 3039 * supported. The General Purpose log directory 3040 * has a header, and lists the number of pages 3041 * available for each GP log identified by the 3042 * offset into the list. 3043 */ 3044 if ((softc->valid_logdir_len >= 3045 ((ATA_IDENTIFY_DATA_LOG + 1) * sizeof(uint16_t))) 3046 && (le16dec(softc->ata_logdir.header) == 3047 ATA_GP_LOG_DIR_VERSION) 3048 && (le16dec(&softc->ata_logdir.num_pages[ 3049 (ATA_IDENTIFY_DATA_LOG * 3050 sizeof(uint16_t)) - sizeof(uint16_t)]) > 0)){ 3051 softc->flags |= ADA_FLAG_CAN_IDLOG; 3052 } else { 3053 softc->flags &= ~ADA_FLAG_CAN_IDLOG; 3054 } 3055 } else { 3056 error = adaerror(done_ccb, CAM_RETRY_SELTO, 3057 SF_RETRY_UA|SF_NO_PRINT); 3058 if (error == ERESTART) 3059 return; 3060 else if (error != 0) { 3061 /* 3062 * If we can't get the ATA log directory, 3063 * then ATA logs are effectively not 3064 * supported even if the bit is set in the 3065 * identify data. 3066 */ 3067 softc->flags &= ~(ADA_FLAG_CAN_LOG | 3068 ADA_FLAG_CAN_IDLOG); 3069 if ((done_ccb->ccb_h.status & 3070 CAM_DEV_QFRZN) != 0) { 3071 /* Don't wedge this device's queue */ 3072 cam_release_devq(done_ccb->ccb_h.path, 3073 /*relsim_flags*/0, 3074 /*reduction*/0, 3075 /*timeout*/0, 3076 /*getcount_only*/0); 3077 } 3078 } 3079 } 3080 3081 free(ataio->data_ptr, M_ATADA); 3082 3083 if ((error == 0) 3084 && (softc->flags & ADA_FLAG_CAN_IDLOG)) { 3085 softc->state = ADA_STATE_IDDIR; 3086 xpt_release_ccb(done_ccb); 3087 xpt_schedule(periph, priority); 3088 } else 3089 adaprobedone(periph, done_ccb); 3090 3091 return; 3092 } 3093 case ADA_CCB_IDDIR: { 3094 int error; 3095 3096 if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 3097 off_t entries_offset, max_entries; 3098 error = 0; 3099 3100 softc->valid_iddir_len = 0; 3101 bzero(&softc->ata_iddir, sizeof(softc->ata_iddir)); 3102 softc->flags &= ~(ADA_FLAG_CAN_SUPCAP | 3103 ADA_FLAG_CAN_ZONE); 3104 softc->valid_iddir_len = 3105 ataio->dxfer_len - ataio->resid; 3106 if (softc->valid_iddir_len > 0) 3107 bcopy(ataio->data_ptr, &softc->ata_iddir, 3108 min(softc->valid_iddir_len, 3109 sizeof(softc->ata_iddir))); 3110 3111 entries_offset = 3112 __offsetof(struct ata_identify_log_pages,entries); 3113 max_entries = softc->valid_iddir_len - entries_offset; 3114 if ((softc->valid_iddir_len > (entries_offset + 1)) 3115 && (le64dec(softc->ata_iddir.header) == 3116 ATA_IDLOG_REVISION) 3117 && (softc->ata_iddir.entry_count > 0)) { 3118 int num_entries, i; 3119 3120 num_entries = softc->ata_iddir.entry_count; 3121 num_entries = min(num_entries, 3122 softc->valid_iddir_len - entries_offset); 3123 for (i = 0; i < num_entries && 3124 i < max_entries; i++) { 3125 if (softc->ata_iddir.entries[i] == 3126 ATA_IDL_SUP_CAP) 3127 softc->flags |= 3128 ADA_FLAG_CAN_SUPCAP; 3129 else if (softc->ata_iddir.entries[i]== 3130 ATA_IDL_ZDI) 3131 softc->flags |= 3132 ADA_FLAG_CAN_ZONE; 3133 3134 if ((softc->flags & 3135 ADA_FLAG_CAN_SUPCAP) 3136 && (softc->flags & 3137 ADA_FLAG_CAN_ZONE)) 3138 break; 3139 } 3140 } 3141 } else { 3142 error = adaerror(done_ccb, CAM_RETRY_SELTO, 3143 SF_RETRY_UA|SF_NO_PRINT); 3144 if (error == ERESTART) 3145 return; 3146 else if (error != 0) { 3147 /* 3148 * If we can't get the ATA Identify Data log 3149 * directory, then it effectively isn't 3150 * supported even if the ATA Log directory 3151 * a non-zero number of pages present for 3152 * this log. 3153 */ 3154 softc->flags &= ~ADA_FLAG_CAN_IDLOG; 3155 if ((done_ccb->ccb_h.status & 3156 CAM_DEV_QFRZN) != 0) { 3157 /* Don't wedge this device's queue */ 3158 cam_release_devq(done_ccb->ccb_h.path, 3159 /*relsim_flags*/0, 3160 /*reduction*/0, 3161 /*timeout*/0, 3162 /*getcount_only*/0); 3163 } 3164 } 3165 } 3166 3167 free(ataio->data_ptr, M_ATADA); 3168 3169 if ((error == 0) 3170 && (softc->flags & ADA_FLAG_CAN_SUPCAP)) { 3171 softc->state = ADA_STATE_SUP_CAP; 3172 xpt_release_ccb(done_ccb); 3173 xpt_schedule(periph, priority); 3174 } else 3175 adaprobedone(periph, done_ccb); 3176 return; 3177 } 3178 case ADA_CCB_SUP_CAP: { 3179 int error; 3180 3181 if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 3182 uint32_t valid_len; 3183 size_t needed_size; 3184 struct ata_identify_log_sup_cap *sup_cap; 3185 error = 0; 3186 3187 sup_cap = (struct ata_identify_log_sup_cap *) 3188 ataio->data_ptr; 3189 valid_len = ataio->dxfer_len - ataio->resid; 3190 needed_size = 3191 __offsetof(struct ata_identify_log_sup_cap, 3192 sup_zac_cap) + 1 + sizeof(sup_cap->sup_zac_cap); 3193 if (valid_len >= needed_size) { 3194 uint64_t zoned, zac_cap; 3195 3196 zoned = le64dec(sup_cap->zoned_cap); 3197 if (zoned & ATA_ZONED_VALID) { 3198 /* 3199 * This should have already been 3200 * set, because this is also in the 3201 * ATA identify data. 3202 */ 3203 if ((zoned & ATA_ZONED_MASK) == 3204 ATA_SUPPORT_ZONE_HOST_AWARE) 3205 softc->zone_mode = 3206 ADA_ZONE_HOST_AWARE; 3207 else if ((zoned & ATA_ZONED_MASK) == 3208 ATA_SUPPORT_ZONE_DEV_MANAGED) 3209 softc->zone_mode = 3210 ADA_ZONE_DRIVE_MANAGED; 3211 } 3212 3213 zac_cap = le64dec(sup_cap->sup_zac_cap); 3214 if (zac_cap & ATA_SUP_ZAC_CAP_VALID) { 3215 if (zac_cap & ATA_REPORT_ZONES_SUP) 3216 softc->zone_flags |= 3217 ADA_ZONE_FLAG_RZ_SUP; 3218 if (zac_cap & ATA_ND_OPEN_ZONE_SUP) 3219 softc->zone_flags |= 3220 ADA_ZONE_FLAG_OPEN_SUP; 3221 if (zac_cap & ATA_ND_CLOSE_ZONE_SUP) 3222 softc->zone_flags |= 3223 ADA_ZONE_FLAG_CLOSE_SUP; 3224 if (zac_cap & ATA_ND_FINISH_ZONE_SUP) 3225 softc->zone_flags |= 3226 ADA_ZONE_FLAG_FINISH_SUP; 3227 if (zac_cap & ATA_ND_RWP_SUP) 3228 softc->zone_flags |= 3229 ADA_ZONE_FLAG_RWP_SUP; 3230 } else { 3231 /* 3232 * This field was introduced in 3233 * ACS-4, r08 on April 28th, 2015. 3234 * If the drive firmware was written 3235 * to an earlier spec, it won't have 3236 * the field. So, assume all 3237 * commands are supported. 3238 */ 3239 softc->zone_flags |= 3240 ADA_ZONE_FLAG_SUP_MASK; 3241 } 3242 } 3243 } else { 3244 error = adaerror(done_ccb, CAM_RETRY_SELTO, 3245 SF_RETRY_UA|SF_NO_PRINT); 3246 if (error == ERESTART) 3247 return; 3248 else if (error != 0) { 3249 /* 3250 * If we can't get the ATA Identify Data 3251 * Supported Capabilities page, clear the 3252 * flag... 3253 */ 3254 softc->flags &= ~ADA_FLAG_CAN_SUPCAP; 3255 /* 3256 * And clear zone capabilities. 3257 */ 3258 softc->zone_flags &= ~ADA_ZONE_FLAG_SUP_MASK; 3259 if ((done_ccb->ccb_h.status & 3260 CAM_DEV_QFRZN) != 0) { 3261 /* Don't wedge this device's queue */ 3262 cam_release_devq(done_ccb->ccb_h.path, 3263 /*relsim_flags*/0, 3264 /*reduction*/0, 3265 /*timeout*/0, 3266 /*getcount_only*/0); 3267 } 3268 } 3269 } 3270 3271 free(ataio->data_ptr, M_ATADA); 3272 3273 if ((error == 0) 3274 && (softc->flags & ADA_FLAG_CAN_ZONE)) { 3275 softc->state = ADA_STATE_ZONE; 3276 xpt_release_ccb(done_ccb); 3277 xpt_schedule(periph, priority); 3278 } else 3279 adaprobedone(periph, done_ccb); 3280 return; 3281 } 3282 case ADA_CCB_ZONE: { 3283 int error; 3284 3285 if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 3286 struct ata_zoned_info_log *zi_log; 3287 uint32_t valid_len; 3288 size_t needed_size; 3289 3290 zi_log = (struct ata_zoned_info_log *)ataio->data_ptr; 3291 3292 valid_len = ataio->dxfer_len - ataio->resid; 3293 needed_size = __offsetof(struct ata_zoned_info_log, 3294 version_info) + 1 + sizeof(zi_log->version_info); 3295 if (valid_len >= needed_size) { 3296 uint64_t tmpvar; 3297 3298 tmpvar = le64dec(zi_log->zoned_cap); 3299 if (tmpvar & ATA_ZDI_CAP_VALID) { 3300 if (tmpvar & ATA_ZDI_CAP_URSWRZ) 3301 softc->zone_flags |= 3302 ADA_ZONE_FLAG_URSWRZ; 3303 else 3304 softc->zone_flags &= 3305 ~ADA_ZONE_FLAG_URSWRZ; 3306 } 3307 tmpvar = le64dec(zi_log->optimal_seq_zones); 3308 if (tmpvar & ATA_ZDI_OPT_SEQ_VALID) { 3309 softc->zone_flags |= 3310 ADA_ZONE_FLAG_OPT_SEQ_SET; 3311 softc->optimal_seq_zones = (tmpvar & 3312 ATA_ZDI_OPT_SEQ_MASK); 3313 } else { 3314 softc->zone_flags &= 3315 ~ADA_ZONE_FLAG_OPT_SEQ_SET; 3316 softc->optimal_seq_zones = 0; 3317 } 3318 3319 tmpvar =le64dec(zi_log->optimal_nonseq_zones); 3320 if (tmpvar & ATA_ZDI_OPT_NS_VALID) { 3321 softc->zone_flags |= 3322 ADA_ZONE_FLAG_OPT_NONSEQ_SET; 3323 softc->optimal_nonseq_zones = 3324 (tmpvar & ATA_ZDI_OPT_NS_MASK); 3325 } else { 3326 softc->zone_flags &= 3327 ~ADA_ZONE_FLAG_OPT_NONSEQ_SET; 3328 softc->optimal_nonseq_zones = 0; 3329 } 3330 3331 tmpvar = le64dec(zi_log->max_seq_req_zones); 3332 if (tmpvar & ATA_ZDI_MAX_SEQ_VALID) { 3333 softc->zone_flags |= 3334 ADA_ZONE_FLAG_MAX_SEQ_SET; 3335 softc->max_seq_zones = 3336 (tmpvar & ATA_ZDI_MAX_SEQ_MASK); 3337 } else { 3338 softc->zone_flags &= 3339 ~ADA_ZONE_FLAG_MAX_SEQ_SET; 3340 softc->max_seq_zones = 0; 3341 } 3342 } 3343 } else { 3344 error = adaerror(done_ccb, CAM_RETRY_SELTO, 3345 SF_RETRY_UA|SF_NO_PRINT); 3346 if (error == ERESTART) 3347 return; 3348 else if (error != 0) { 3349 softc->flags &= ~ADA_FLAG_CAN_ZONE; 3350 softc->flags &= ~ADA_ZONE_FLAG_SET_MASK; 3351 3352 if ((done_ccb->ccb_h.status & 3353 CAM_DEV_QFRZN) != 0) { 3354 /* Don't wedge this device's queue */ 3355 cam_release_devq(done_ccb->ccb_h.path, 3356 /*relsim_flags*/0, 3357 /*reduction*/0, 3358 /*timeout*/0, 3359 /*getcount_only*/0); 3360 } 3361 } 3362 } 3363 free(ataio->data_ptr, M_ATADA); 3364 3365 adaprobedone(periph, done_ccb); 3366 return; 3367 } 3368 case ADA_CCB_DUMP: 3369 /* No-op. We're polling */ 3370 return; 3371 default: 3372 break; 3373 } 3374 xpt_release_ccb(done_ccb); 3375 } 3376 3377 static int 3378 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 3379 { 3380 #ifdef CAM_IO_STATS 3381 struct ada_softc *softc; 3382 struct cam_periph *periph; 3383 3384 periph = xpt_path_periph(ccb->ccb_h.path); 3385 softc = (struct ada_softc *)periph->softc; 3386 3387 switch (ccb->ccb_h.status & CAM_STATUS_MASK) { 3388 case CAM_CMD_TIMEOUT: 3389 softc->timeouts++; 3390 break; 3391 case CAM_REQ_ABORTED: 3392 case CAM_REQ_CMP_ERR: 3393 case CAM_REQ_TERMIO: 3394 case CAM_UNREC_HBA_ERROR: 3395 case CAM_DATA_RUN_ERR: 3396 case CAM_ATA_STATUS_ERROR: 3397 softc->errors++; 3398 break; 3399 default: 3400 break; 3401 } 3402 #endif 3403 3404 return(cam_periph_error(ccb, cam_flags, sense_flags)); 3405 } 3406 3407 static void 3408 adasetgeom(struct ada_softc *softc, struct ccb_getdev *cgd) 3409 { 3410 struct disk_params *dp = &softc->params; 3411 u_int64_t lbasize48; 3412 u_int32_t lbasize; 3413 u_int maxio, d_flags; 3414 3415 dp->secsize = ata_logical_sector_size(&cgd->ident_data); 3416 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) && 3417 cgd->ident_data.current_heads != 0 && 3418 cgd->ident_data.current_sectors != 0) { 3419 dp->heads = cgd->ident_data.current_heads; 3420 dp->secs_per_track = cgd->ident_data.current_sectors; 3421 dp->cylinders = cgd->ident_data.cylinders; 3422 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 | 3423 ((u_int32_t)cgd->ident_data.current_size_2 << 16); 3424 } else { 3425 dp->heads = cgd->ident_data.heads; 3426 dp->secs_per_track = cgd->ident_data.sectors; 3427 dp->cylinders = cgd->ident_data.cylinders; 3428 dp->sectors = cgd->ident_data.cylinders * 3429 (u_int32_t)(dp->heads * dp->secs_per_track); 3430 } 3431 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 | 3432 ((u_int32_t)cgd->ident_data.lba_size_2 << 16); 3433 3434 /* use the 28bit LBA size if valid or bigger than the CHS mapping */ 3435 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize) 3436 dp->sectors = lbasize; 3437 3438 /* use the 48bit LBA size if valid */ 3439 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) | 3440 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) | 3441 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) | 3442 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48); 3443 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) && 3444 lbasize48 > ATA_MAX_28BIT_LBA) 3445 dp->sectors = lbasize48; 3446 3447 maxio = softc->cpi.maxio; /* Honor max I/O size of SIM */ 3448 if (maxio == 0) 3449 maxio = DFLTPHYS; /* traditional default */ 3450 else if (maxio > MAXPHYS) 3451 maxio = MAXPHYS; /* for safety */ 3452 if (softc->flags & ADA_FLAG_CAN_48BIT) 3453 maxio = min(maxio, 65536 * softc->params.secsize); 3454 else /* 28bit ATA command limit */ 3455 maxio = min(maxio, 256 * softc->params.secsize); 3456 if (softc->quirks & ADA_Q_128KB) 3457 maxio = min(maxio, 128 * 1024); 3458 softc->disk->d_maxsize = maxio; 3459 d_flags = DISKFLAG_DIRECT_COMPLETION | DISKFLAG_CANZONE; 3460 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) 3461 d_flags |= DISKFLAG_CANFLUSHCACHE; 3462 if (softc->flags & ADA_FLAG_CAN_TRIM) { 3463 d_flags |= DISKFLAG_CANDELETE; 3464 softc->disk->d_delmaxsize = softc->params.secsize * 3465 ATA_DSM_RANGE_MAX * softc->trim_max_ranges; 3466 } else if ((softc->flags & ADA_FLAG_CAN_CFA) && 3467 !(softc->flags & ADA_FLAG_CAN_48BIT)) { 3468 d_flags |= DISKFLAG_CANDELETE; 3469 softc->disk->d_delmaxsize = 256 * softc->params.secsize; 3470 } else 3471 softc->disk->d_delmaxsize = maxio; 3472 if ((softc->cpi.hba_misc & PIM_UNMAPPED) != 0) { 3473 d_flags |= DISKFLAG_UNMAPPED_BIO; 3474 softc->flags |= ADA_FLAG_UNMAPPEDIO; 3475 } 3476 softc->disk->d_flags = d_flags; 3477 strlcpy(softc->disk->d_descr, cgd->ident_data.model, 3478 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model))); 3479 strlcpy(softc->disk->d_ident, cgd->ident_data.serial, 3480 MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial))); 3481 3482 softc->disk->d_sectorsize = softc->params.secsize; 3483 softc->disk->d_mediasize = (off_t)softc->params.sectors * 3484 softc->params.secsize; 3485 if (ata_physical_sector_size(&cgd->ident_data) != 3486 softc->params.secsize) { 3487 softc->disk->d_stripesize = 3488 ata_physical_sector_size(&cgd->ident_data); 3489 softc->disk->d_stripeoffset = (softc->disk->d_stripesize - 3490 ata_logical_sector_offset(&cgd->ident_data)) % 3491 softc->disk->d_stripesize; 3492 } else if (softc->quirks & ADA_Q_4K) { 3493 softc->disk->d_stripesize = 4096; 3494 softc->disk->d_stripeoffset = 0; 3495 } 3496 softc->disk->d_fwsectors = softc->params.secs_per_track; 3497 softc->disk->d_fwheads = softc->params.heads; 3498 ata_disk_firmware_geom_adjust(softc->disk); 3499 softc->disk->d_rotation_rate = cgd->ident_data.media_rotation_rate; 3500 snprintf(softc->disk->d_attachment, sizeof(softc->disk->d_attachment), 3501 "%s%d", softc->cpi.dev_name, softc->cpi.unit_number); 3502 } 3503 3504 static void 3505 adasendorderedtag(void *arg) 3506 { 3507 struct ada_softc *softc = arg; 3508 3509 if (ada_send_ordered) { 3510 if (softc->outstanding_cmds > 0) { 3511 if ((softc->flags & ADA_FLAG_WAS_OTAG) == 0) 3512 softc->flags |= ADA_FLAG_NEED_OTAG; 3513 softc->flags &= ~ADA_FLAG_WAS_OTAG; 3514 } 3515 } 3516 /* Queue us up again */ 3517 callout_reset(&softc->sendordered_c, 3518 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 3519 adasendorderedtag, softc); 3520 } 3521 3522 /* 3523 * Step through all ADA peripheral drivers, and if the device is still open, 3524 * sync the disk cache to physical media. 3525 */ 3526 static void 3527 adaflush(void) 3528 { 3529 struct cam_periph *periph; 3530 struct ada_softc *softc; 3531 union ccb *ccb; 3532 int error; 3533 3534 CAM_PERIPH_FOREACH(periph, &adadriver) { 3535 softc = (struct ada_softc *)periph->softc; 3536 if (SCHEDULER_STOPPED()) { 3537 /* If we paniced with the lock held, do not recurse. */ 3538 if (!cam_periph_owned(periph) && 3539 (softc->flags & ADA_FLAG_OPEN)) { 3540 adadump(softc->disk, NULL, 0, 0, 0); 3541 } 3542 continue; 3543 } 3544 cam_periph_lock(periph); 3545 /* 3546 * We only sync the cache if the drive is still open, and 3547 * if the drive is capable of it.. 3548 */ 3549 if (((softc->flags & ADA_FLAG_OPEN) == 0) || 3550 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) { 3551 cam_periph_unlock(periph); 3552 continue; 3553 } 3554 3555 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3556 cam_fill_ataio(&ccb->ataio, 3557 0, 3558 NULL, 3559 CAM_DIR_NONE, 3560 0, 3561 NULL, 3562 0, 3563 ada_default_timeout*1000); 3564 if (softc->flags & ADA_FLAG_CAN_48BIT) 3565 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 3566 else 3567 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 3568 3569 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 3570 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 3571 softc->disk->d_devstat); 3572 if (error != 0) 3573 xpt_print(periph->path, "Synchronize cache failed\n"); 3574 xpt_release_ccb(ccb); 3575 cam_periph_unlock(periph); 3576 } 3577 } 3578 3579 static void 3580 adaspindown(uint8_t cmd, int flags) 3581 { 3582 struct cam_periph *periph; 3583 struct ada_softc *softc; 3584 struct ccb_ataio local_ccb; 3585 int error; 3586 3587 CAM_PERIPH_FOREACH(periph, &adadriver) { 3588 /* If we paniced with lock held - not recurse here. */ 3589 if (cam_periph_owned(periph)) 3590 continue; 3591 cam_periph_lock(periph); 3592 softc = (struct ada_softc *)periph->softc; 3593 /* 3594 * We only spin-down the drive if it is capable of it.. 3595 */ 3596 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 3597 cam_periph_unlock(periph); 3598 continue; 3599 } 3600 3601 if (bootverbose) 3602 xpt_print(periph->path, "spin-down\n"); 3603 3604 memset(&local_ccb, 0, sizeof(local_ccb)); 3605 xpt_setup_ccb(&local_ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 3606 local_ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 3607 3608 cam_fill_ataio(&local_ccb, 3609 0, 3610 NULL, 3611 CAM_DIR_NONE | flags, 3612 0, 3613 NULL, 3614 0, 3615 ada_default_timeout*1000); 3616 ata_28bit_cmd(&local_ccb, cmd, 0, 0, 0); 3617 error = cam_periph_runccb((union ccb *)&local_ccb, adaerror, 3618 /*cam_flags*/0, /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 3619 softc->disk->d_devstat); 3620 if (error != 0) 3621 xpt_print(periph->path, "Spin-down disk failed\n"); 3622 cam_periph_unlock(periph); 3623 } 3624 } 3625 3626 static void 3627 adashutdown(void *arg, int howto) 3628 { 3629 int how; 3630 3631 adaflush(); 3632 3633 /* 3634 * STANDBY IMMEDIATE saves any volatile data to the drive. It also spins 3635 * down hard drives. IDLE IMMEDIATE also saves the volatile data without 3636 * a spindown. We send the former when we expect to lose power soon. For 3637 * a warm boot, we send the latter to avoid a thundering herd of spinups 3638 * just after the kernel loads while probing. We have to do something to 3639 * flush the data because the BIOS in many systems resets the HBA 3640 * causing a COMINIT/COMRESET negotiation, which some drives interpret 3641 * as license to toss the volatile data, and others count as unclean 3642 * shutdown when in the Active PM state in SMART attributes. 3643 * 3644 * adaspindown will ensure that we don't send this to a drive that 3645 * doesn't support it. 3646 */ 3647 if (ada_spindown_shutdown != 0) { 3648 how = (howto & (RB_HALT | RB_POWEROFF | RB_POWERCYCLE)) ? 3649 ATA_STANDBY_IMMEDIATE : ATA_IDLE_IMMEDIATE; 3650 adaspindown(how, 0); 3651 } 3652 } 3653 3654 static void 3655 adasuspend(void *arg) 3656 { 3657 3658 adaflush(); 3659 /* 3660 * SLEEP also fushes any volatile data, like STANDBY IMEDIATE, 3661 * so we don't need to send it as well. 3662 */ 3663 if (ada_spindown_suspend != 0) 3664 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE); 3665 } 3666 3667 static void 3668 adaresume(void *arg) 3669 { 3670 struct cam_periph *periph; 3671 struct ada_softc *softc; 3672 3673 if (ada_spindown_suspend == 0) 3674 return; 3675 3676 CAM_PERIPH_FOREACH(periph, &adadriver) { 3677 cam_periph_lock(periph); 3678 softc = (struct ada_softc *)periph->softc; 3679 /* 3680 * We only spin-down the drive if it is capable of it.. 3681 */ 3682 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 3683 cam_periph_unlock(periph); 3684 continue; 3685 } 3686 3687 if (bootverbose) 3688 xpt_print(periph->path, "resume\n"); 3689 3690 /* 3691 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on 3692 * sleep request. 3693 */ 3694 cam_release_devq(periph->path, 3695 /*relsim_flags*/0, 3696 /*openings*/0, 3697 /*timeout*/0, 3698 /*getcount_only*/0); 3699 3700 cam_periph_unlock(periph); 3701 } 3702 } 3703 3704 #endif /* _KERNEL */ 3705