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 static int ada_retry_count = ADA_DEFAULT_RETRY; 898 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT; 899 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED; 900 static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN; 901 static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND; 902 static int ada_read_ahead = ADA_DEFAULT_READ_AHEAD; 903 static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE; 904 static int ada_enable_biospeedup = 1; 905 906 static SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 907 "CAM Direct Access Disk driver"); 908 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RWTUN, 909 &ada_retry_count, 0, "Normal I/O retry count"); 910 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RWTUN, 911 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)"); 912 SYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RWTUN, 913 &ada_send_ordered, 0, "Send Ordered Tags"); 914 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RWTUN, 915 &ada_spindown_shutdown, 0, "Spin down upon shutdown"); 916 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RWTUN, 917 &ada_spindown_suspend, 0, "Spin down upon suspend"); 918 SYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RWTUN, 919 &ada_read_ahead, 0, "Enable disk read-ahead"); 920 SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RWTUN, 921 &ada_write_cache, 0, "Enable disk write cache"); 922 SYSCTL_INT(_kern_cam_ada, OID_AUTO, enable_biospeedup, CTLFLAG_RDTUN, 923 &ada_enable_biospeedup, 0, "Enable BIO_SPEEDUP processing"); 924 925 /* 926 * ADA_ORDEREDTAG_INTERVAL determines how often, relative 927 * to the default timeout, we check to see whether an ordered 928 * tagged transaction is appropriate to prevent simple tag 929 * starvation. Since we'd like to ensure that there is at least 930 * 1/2 of the timeout length left for a starved transaction to 931 * complete after we've sent an ordered tag, we must poll at least 932 * four times in every timeout period. This takes care of the worst 933 * case where a starved transaction starts during an interval that 934 * meets the requirement "don't send an ordered tag" test so it takes 935 * us two intervals to determine that a tag must be sent. 936 */ 937 #ifndef ADA_ORDEREDTAG_INTERVAL 938 #define ADA_ORDEREDTAG_INTERVAL 4 939 #endif 940 941 static struct periph_driver adadriver = 942 { 943 adainit, "ada", 944 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0 945 }; 946 947 static int adadeletemethodsysctl(SYSCTL_HANDLER_ARGS); 948 949 PERIPHDRIVER_DECLARE(ada, adadriver); 950 951 static MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers"); 952 953 static int 954 adaopen(struct disk *dp) 955 { 956 struct cam_periph *periph; 957 struct ada_softc *softc; 958 int error; 959 960 periph = (struct cam_periph *)dp->d_drv1; 961 if (cam_periph_acquire(periph) != 0) { 962 return(ENXIO); 963 } 964 965 cam_periph_lock(periph); 966 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) { 967 cam_periph_unlock(periph); 968 cam_periph_release(periph); 969 return (error); 970 } 971 972 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 973 ("adaopen\n")); 974 975 softc = (struct ada_softc *)periph->softc; 976 softc->flags |= ADA_FLAG_OPEN; 977 978 cam_periph_unhold(periph); 979 cam_periph_unlock(periph); 980 return (0); 981 } 982 983 static int 984 adaclose(struct disk *dp) 985 { 986 struct cam_periph *periph; 987 struct ada_softc *softc; 988 union ccb *ccb; 989 int error; 990 991 periph = (struct cam_periph *)dp->d_drv1; 992 softc = (struct ada_softc *)periph->softc; 993 cam_periph_lock(periph); 994 995 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 996 ("adaclose\n")); 997 998 /* We only sync the cache if the drive is capable of it. */ 999 if ((softc->flags & ADA_FLAG_DIRTY) != 0 && 1000 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 && 1001 (periph->flags & CAM_PERIPH_INVALID) == 0 && 1002 cam_periph_hold(periph, PRIBIO) == 0) { 1003 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1004 cam_fill_ataio(&ccb->ataio, 1005 1, 1006 NULL, 1007 CAM_DIR_NONE, 1008 0, 1009 NULL, 1010 0, 1011 ada_default_timeout*1000); 1012 1013 if (softc->flags & ADA_FLAG_CAN_48BIT) 1014 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1015 else 1016 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 1017 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 1018 /*sense_flags*/0, softc->disk->d_devstat); 1019 1020 if (error != 0) 1021 xpt_print(periph->path, "Synchronize cache failed\n"); 1022 softc->flags &= ~ADA_FLAG_DIRTY; 1023 xpt_release_ccb(ccb); 1024 cam_periph_unhold(periph); 1025 } 1026 1027 softc->flags &= ~ADA_FLAG_OPEN; 1028 1029 while (softc->refcount != 0) 1030 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "adaclose", 1); 1031 cam_periph_unlock(periph); 1032 cam_periph_release(periph); 1033 return (0); 1034 } 1035 1036 static void 1037 adaschedule(struct cam_periph *periph) 1038 { 1039 struct ada_softc *softc = (struct ada_softc *)periph->softc; 1040 1041 if (softc->state != ADA_STATE_NORMAL) 1042 return; 1043 1044 cam_iosched_schedule(softc->cam_iosched, periph); 1045 } 1046 1047 /* 1048 * Actually translate the requested transfer into one the physical driver 1049 * can understand. The transfer is described by a buf and will include 1050 * only one physical transfer. 1051 */ 1052 static void 1053 adastrategy(struct bio *bp) 1054 { 1055 struct cam_periph *periph; 1056 struct ada_softc *softc; 1057 1058 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 1059 softc = (struct ada_softc *)periph->softc; 1060 1061 cam_periph_lock(periph); 1062 1063 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp)); 1064 1065 /* 1066 * If the device has been made invalid, error out 1067 */ 1068 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 1069 cam_periph_unlock(periph); 1070 biofinish(bp, NULL, ENXIO); 1071 return; 1072 } 1073 1074 /* 1075 * Zone commands must be ordered, because they can depend on the 1076 * effects of previously issued commands, and they may affect 1077 * commands after them. 1078 */ 1079 if (bp->bio_cmd == BIO_ZONE) 1080 bp->bio_flags |= BIO_ORDERED; 1081 1082 /* 1083 * Place it in the queue of disk activities for this disk 1084 */ 1085 cam_iosched_queue_work(softc->cam_iosched, bp); 1086 1087 /* 1088 * Schedule ourselves for performing the work. 1089 */ 1090 adaschedule(periph); 1091 cam_periph_unlock(periph); 1092 1093 return; 1094 } 1095 1096 static int 1097 adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) 1098 { 1099 struct cam_periph *periph; 1100 struct ada_softc *softc; 1101 u_int secsize; 1102 struct ccb_ataio ataio; 1103 struct disk *dp; 1104 uint64_t lba; 1105 uint16_t count; 1106 int error = 0; 1107 1108 dp = arg; 1109 periph = dp->d_drv1; 1110 softc = (struct ada_softc *)periph->softc; 1111 secsize = softc->params.secsize; 1112 lba = offset / secsize; 1113 count = length / secsize; 1114 if ((periph->flags & CAM_PERIPH_INVALID) != 0) 1115 return (ENXIO); 1116 1117 memset(&ataio, 0, sizeof(ataio)); 1118 if (length > 0) { 1119 xpt_setup_ccb(&ataio.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1120 ataio.ccb_h.ccb_state = ADA_CCB_DUMP; 1121 cam_fill_ataio(&ataio, 1122 0, 1123 NULL, 1124 CAM_DIR_OUT, 1125 0, 1126 (u_int8_t *) virtual, 1127 length, 1128 ada_default_timeout*1000); 1129 if ((softc->flags & ADA_FLAG_CAN_48BIT) && 1130 (lba + count >= ATA_MAX_28BIT_LBA || 1131 count >= 256)) { 1132 ata_48bit_cmd(&ataio, ATA_WRITE_DMA48, 1133 0, lba, count); 1134 } else { 1135 ata_28bit_cmd(&ataio, ATA_WRITE_DMA, 1136 0, lba, count); 1137 } 1138 error = cam_periph_runccb((union ccb *)&ataio, adaerror, 1139 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 1140 if (error != 0) 1141 printf("Aborting dump due to I/O error.\n"); 1142 1143 return (error); 1144 } 1145 1146 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) { 1147 xpt_setup_ccb(&ataio.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1148 1149 /* 1150 * Tell the drive to flush its internal cache. if we 1151 * can't flush in 5s we have big problems. No need to 1152 * wait the default 60s to detect problems. 1153 */ 1154 ataio.ccb_h.ccb_state = ADA_CCB_DUMP; 1155 cam_fill_ataio(&ataio, 1156 0, 1157 NULL, 1158 CAM_DIR_NONE, 1159 0, 1160 NULL, 1161 0, 1162 5*1000); 1163 1164 if (softc->flags & ADA_FLAG_CAN_48BIT) 1165 ata_48bit_cmd(&ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1166 else 1167 ata_28bit_cmd(&ataio, ATA_FLUSHCACHE, 0, 0, 0); 1168 error = cam_periph_runccb((union ccb *)&ataio, adaerror, 1169 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 1170 if (error != 0) 1171 xpt_print(periph->path, "Synchronize cache failed\n"); 1172 } 1173 return (error); 1174 } 1175 1176 static void 1177 adainit(void) 1178 { 1179 cam_status status; 1180 1181 /* 1182 * Install a global async callback. This callback will 1183 * receive async callbacks like "new device found". 1184 */ 1185 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL); 1186 1187 if (status != CAM_REQ_CMP) { 1188 printf("ada: Failed to attach master async callback " 1189 "due to status 0x%x!\n", status); 1190 } else if (ada_send_ordered) { 1191 /* Register our event handlers */ 1192 if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend, 1193 NULL, EVENTHANDLER_PRI_LAST)) == NULL) 1194 printf("adainit: power event registration failed!\n"); 1195 if ((EVENTHANDLER_REGISTER(power_resume, adaresume, 1196 NULL, EVENTHANDLER_PRI_LAST)) == NULL) 1197 printf("adainit: power event registration failed!\n"); 1198 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown, 1199 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 1200 printf("adainit: shutdown event registration failed!\n"); 1201 } 1202 } 1203 1204 /* 1205 * Callback from GEOM, called when it has finished cleaning up its 1206 * resources. 1207 */ 1208 static void 1209 adadiskgonecb(struct disk *dp) 1210 { 1211 struct cam_periph *periph; 1212 1213 periph = (struct cam_periph *)dp->d_drv1; 1214 1215 cam_periph_release(periph); 1216 } 1217 1218 static void 1219 adaoninvalidate(struct cam_periph *periph) 1220 { 1221 struct ada_softc *softc; 1222 1223 softc = (struct ada_softc *)periph->softc; 1224 1225 /* 1226 * De-register any async callbacks. 1227 */ 1228 xpt_register_async(0, adaasync, periph, periph->path); 1229 #ifdef CAM_IO_STATS 1230 softc->invalidations++; 1231 #endif 1232 1233 /* 1234 * Return all queued I/O with ENXIO. 1235 * XXX Handle any transactions queued to the card 1236 * with XPT_ABORT_CCB. 1237 */ 1238 cam_iosched_flush(softc->cam_iosched, NULL, ENXIO); 1239 1240 disk_gone(softc->disk); 1241 } 1242 1243 static void 1244 adacleanup(struct cam_periph *periph) 1245 { 1246 struct ada_softc *softc; 1247 1248 softc = (struct ada_softc *)periph->softc; 1249 1250 cam_periph_unlock(periph); 1251 1252 cam_iosched_fini(softc->cam_iosched); 1253 1254 /* 1255 * If we can't free the sysctl tree, oh well... 1256 */ 1257 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0) { 1258 #ifdef CAM_IO_STATS 1259 if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0) 1260 xpt_print(periph->path, 1261 "can't remove sysctl stats context\n"); 1262 #endif 1263 if (sysctl_ctx_free(&softc->sysctl_ctx) != 0) 1264 xpt_print(periph->path, 1265 "can't remove sysctl context\n"); 1266 } 1267 1268 disk_destroy(softc->disk); 1269 callout_drain(&softc->sendordered_c); 1270 free(softc, M_DEVBUF); 1271 cam_periph_lock(periph); 1272 } 1273 1274 static void 1275 adasetdeletemethod(struct ada_softc *softc) 1276 { 1277 1278 if (softc->flags & ADA_FLAG_CAN_NCQ_TRIM) 1279 softc->delete_method = ADA_DELETE_NCQ_DSM_TRIM; 1280 else if (softc->flags & ADA_FLAG_CAN_TRIM) 1281 softc->delete_method = ADA_DELETE_DSM_TRIM; 1282 else if ((softc->flags & ADA_FLAG_CAN_CFA) && !(softc->flags & ADA_FLAG_CAN_48BIT)) 1283 softc->delete_method = ADA_DELETE_CFA_ERASE; 1284 else 1285 softc->delete_method = ADA_DELETE_NONE; 1286 } 1287 1288 static void 1289 adaasync(void *callback_arg, u_int32_t code, 1290 struct cam_path *path, void *arg) 1291 { 1292 struct ccb_getdev cgd; 1293 struct cam_periph *periph; 1294 struct ada_softc *softc; 1295 1296 periph = (struct cam_periph *)callback_arg; 1297 switch (code) { 1298 case AC_FOUND_DEVICE: 1299 { 1300 struct ccb_getdev *cgd; 1301 cam_status status; 1302 1303 cgd = (struct ccb_getdev *)arg; 1304 if (cgd == NULL) 1305 break; 1306 1307 if (cgd->protocol != PROTO_ATA) 1308 break; 1309 1310 /* 1311 * Allocate a peripheral instance for 1312 * this device and start the probe 1313 * process. 1314 */ 1315 status = cam_periph_alloc(adaregister, adaoninvalidate, 1316 adacleanup, adastart, 1317 "ada", CAM_PERIPH_BIO, 1318 path, adaasync, 1319 AC_FOUND_DEVICE, cgd); 1320 1321 if (status != CAM_REQ_CMP 1322 && status != CAM_REQ_INPROG) 1323 printf("adaasync: Unable to attach to new device " 1324 "due to status 0x%x\n", status); 1325 break; 1326 } 1327 case AC_GETDEV_CHANGED: 1328 { 1329 softc = (struct ada_softc *)periph->softc; 1330 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1331 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1332 xpt_action((union ccb *)&cgd); 1333 1334 /* 1335 * Update our information based on the new Identify data. 1336 */ 1337 adasetflags(softc, &cgd); 1338 adasetgeom(softc, &cgd); 1339 disk_resize(softc->disk, M_NOWAIT); 1340 1341 cam_periph_async(periph, code, path, arg); 1342 break; 1343 } 1344 case AC_ADVINFO_CHANGED: 1345 { 1346 uintptr_t buftype; 1347 1348 buftype = (uintptr_t)arg; 1349 if (buftype == CDAI_TYPE_PHYS_PATH) { 1350 struct ada_softc *softc; 1351 1352 softc = periph->softc; 1353 disk_attr_changed(softc->disk, "GEOM::physpath", 1354 M_NOWAIT); 1355 } 1356 break; 1357 } 1358 case AC_SENT_BDR: 1359 case AC_BUS_RESET: 1360 { 1361 softc = (struct ada_softc *)periph->softc; 1362 cam_periph_async(periph, code, path, arg); 1363 if (softc->state != ADA_STATE_NORMAL) 1364 break; 1365 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1366 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1367 xpt_action((union ccb *)&cgd); 1368 if (ADA_RA >= 0 && softc->flags & ADA_FLAG_CAN_RAHEAD) 1369 softc->state = ADA_STATE_RAHEAD; 1370 else if (ADA_WC >= 0 && softc->flags & ADA_FLAG_CAN_WCACHE) 1371 softc->state = ADA_STATE_WCACHE; 1372 else if ((softc->flags & ADA_FLAG_CAN_LOG) 1373 && (softc->zone_mode != ADA_ZONE_NONE)) 1374 softc->state = ADA_STATE_LOGDIR; 1375 else 1376 break; 1377 if (cam_periph_acquire(periph) != 0) 1378 softc->state = ADA_STATE_NORMAL; 1379 else 1380 xpt_schedule(periph, CAM_PRIORITY_DEV); 1381 } 1382 default: 1383 cam_periph_async(periph, code, path, arg); 1384 break; 1385 } 1386 } 1387 1388 static int 1389 adazonemodesysctl(SYSCTL_HANDLER_ARGS) 1390 { 1391 char tmpbuf[40]; 1392 struct ada_softc *softc; 1393 int error; 1394 1395 softc = (struct ada_softc *)arg1; 1396 1397 switch (softc->zone_mode) { 1398 case ADA_ZONE_DRIVE_MANAGED: 1399 snprintf(tmpbuf, sizeof(tmpbuf), "Drive Managed"); 1400 break; 1401 case ADA_ZONE_HOST_AWARE: 1402 snprintf(tmpbuf, sizeof(tmpbuf), "Host Aware"); 1403 break; 1404 case ADA_ZONE_HOST_MANAGED: 1405 snprintf(tmpbuf, sizeof(tmpbuf), "Host Managed"); 1406 break; 1407 case ADA_ZONE_NONE: 1408 default: 1409 snprintf(tmpbuf, sizeof(tmpbuf), "Not Zoned"); 1410 break; 1411 } 1412 1413 error = sysctl_handle_string(oidp, tmpbuf, sizeof(tmpbuf), req); 1414 1415 return (error); 1416 } 1417 1418 static int 1419 adazonesupsysctl(SYSCTL_HANDLER_ARGS) 1420 { 1421 char tmpbuf[180]; 1422 struct ada_softc *softc; 1423 struct sbuf sb; 1424 int error, first; 1425 unsigned int i; 1426 1427 softc = (struct ada_softc *)arg1; 1428 1429 error = 0; 1430 first = 1; 1431 sbuf_new(&sb, tmpbuf, sizeof(tmpbuf), 0); 1432 1433 for (i = 0; i < sizeof(ada_zone_desc_table) / 1434 sizeof(ada_zone_desc_table[0]); i++) { 1435 if (softc->zone_flags & ada_zone_desc_table[i].value) { 1436 if (first == 0) 1437 sbuf_printf(&sb, ", "); 1438 else 1439 first = 0; 1440 sbuf_cat(&sb, ada_zone_desc_table[i].desc); 1441 } 1442 } 1443 1444 if (first == 1) 1445 sbuf_printf(&sb, "None"); 1446 1447 sbuf_finish(&sb); 1448 1449 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 1450 1451 return (error); 1452 } 1453 1454 static void 1455 adasysctlinit(void *context, int pending) 1456 { 1457 struct cam_periph *periph; 1458 struct ada_softc *softc; 1459 char tmpstr[32], tmpstr2[16]; 1460 1461 periph = (struct cam_periph *)context; 1462 1463 /* periph was held for us when this task was enqueued */ 1464 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 1465 cam_periph_release(periph); 1466 return; 1467 } 1468 1469 softc = (struct ada_softc *)periph->softc; 1470 snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d",periph->unit_number); 1471 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 1472 1473 sysctl_ctx_init(&softc->sysctl_ctx); 1474 softc->flags |= ADA_FLAG_SCTX_INIT; 1475 softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx, 1476 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2, 1477 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, tmpstr, "device_index"); 1478 if (softc->sysctl_tree == NULL) { 1479 printf("adasysctlinit: unable to allocate sysctl tree\n"); 1480 cam_periph_release(periph); 1481 return; 1482 } 1483 1484 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1485 OID_AUTO, "delete_method", 1486 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1487 softc, 0, adadeletemethodsysctl, "A", 1488 "BIO_DELETE execution method"); 1489 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx, 1490 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1491 "trim_count", CTLFLAG_RD, &softc->trim_count, 1492 "Total number of dsm commands sent"); 1493 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx, 1494 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1495 "trim_ranges", CTLFLAG_RD, &softc->trim_ranges, 1496 "Total number of ranges in dsm commands"); 1497 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx, 1498 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1499 "trim_lbas", CTLFLAG_RD, &softc->trim_lbas, 1500 "Total lbas in the dsm commands sent"); 1501 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1502 OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE, 1503 &softc->read_ahead, 0, "Enable disk read ahead."); 1504 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1505 OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE, 1506 &softc->write_cache, 0, "Enable disk write cache."); 1507 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1508 OID_AUTO, "zone_mode", 1509 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 1510 softc, 0, adazonemodesysctl, "A", 1511 "Zone Mode"); 1512 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1513 OID_AUTO, "zone_support", 1514 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 1515 softc, 0, adazonesupsysctl, "A", 1516 "Zone Support"); 1517 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx, 1518 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1519 "optimal_seq_zones", CTLFLAG_RD, &softc->optimal_seq_zones, 1520 "Optimal Number of Open Sequential Write Preferred Zones"); 1521 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx, 1522 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1523 "optimal_nonseq_zones", CTLFLAG_RD, 1524 &softc->optimal_nonseq_zones, 1525 "Optimal Number of Non-Sequentially Written Sequential Write " 1526 "Preferred Zones"); 1527 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx, 1528 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1529 "max_seq_zones", CTLFLAG_RD, &softc->max_seq_zones, 1530 "Maximum Number of Open Sequential Write Required Zones"); 1531 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1532 OID_AUTO, "flags", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 1533 softc, 0, adaflagssysctl, "A", 1534 "Flags for drive"); 1535 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1536 OID_AUTO, "unmapped_io", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 1537 &softc->flags, (u_int)ADA_FLAG_UNMAPPEDIO, adabitsysctl, "I", 1538 "Unmapped I/O support *DEPRECATED* gone in FreeBSD 14"); 1539 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1540 OID_AUTO, "rotating", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 1541 &softc->flags, (u_int)ADA_FLAG_ROTATING, adabitsysctl, "I", 1542 "Rotating media *DEPRECATED* gone in FreeBSD 14"); 1543 1544 #ifdef CAM_TEST_FAILURE 1545 /* 1546 * Add a 'door bell' sysctl which allows one to set it from userland 1547 * and cause something bad to happen. For the moment, we only allow 1548 * whacking the next read or write. 1549 */ 1550 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1551 OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1552 &softc->force_read_error, 0, 1553 "Force a read error for the next N reads."); 1554 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1555 OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1556 &softc->force_write_error, 0, 1557 "Force a write error for the next N writes."); 1558 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1559 OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1560 &softc->periodic_read_error, 0, 1561 "Force a read error every N reads (don't set too low)."); 1562 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1563 OID_AUTO, "invalidate", CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE, 1564 periph, 0, cam_periph_invalidate_sysctl, "I", 1565 "Write 1 to invalidate the drive immediately"); 1566 #endif 1567 1568 #ifdef CAM_IO_STATS 1569 softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx, 1570 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats", 1571 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Statistics"); 1572 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx, 1573 SYSCTL_CHILDREN(softc->sysctl_stats_tree), 1574 OID_AUTO, "timeouts", CTLFLAG_RD | CTLFLAG_MPSAFE, 1575 &softc->timeouts, 0, 1576 "Device timeouts reported by the SIM"); 1577 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx, 1578 SYSCTL_CHILDREN(softc->sysctl_stats_tree), 1579 OID_AUTO, "errors", CTLFLAG_RD | CTLFLAG_MPSAFE, 1580 &softc->errors, 0, 1581 "Transport errors reported by the SIM."); 1582 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx, 1583 SYSCTL_CHILDREN(softc->sysctl_stats_tree), 1584 OID_AUTO, "pack_invalidations", CTLFLAG_RD | CTLFLAG_MPSAFE, 1585 &softc->invalidations, 0, 1586 "Device pack invalidations."); 1587 #endif 1588 1589 cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx, 1590 softc->sysctl_tree); 1591 1592 cam_periph_release(periph); 1593 } 1594 1595 static int 1596 adagetattr(struct bio *bp) 1597 { 1598 int ret; 1599 struct cam_periph *periph; 1600 1601 if (g_handleattr_int(bp, "GEOM::canspeedup", ada_enable_biospeedup)) 1602 return (EJUSTRETURN); 1603 1604 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 1605 cam_periph_lock(periph); 1606 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute, 1607 periph->path); 1608 cam_periph_unlock(periph); 1609 if (ret == 0) 1610 bp->bio_completed = bp->bio_length; 1611 return ret; 1612 } 1613 1614 static int 1615 adadeletemethodsysctl(SYSCTL_HANDLER_ARGS) 1616 { 1617 char buf[16]; 1618 const char *p; 1619 struct ada_softc *softc; 1620 int i, error, value, methods; 1621 1622 softc = (struct ada_softc *)arg1; 1623 1624 value = softc->delete_method; 1625 if (value < 0 || value > ADA_DELETE_MAX) 1626 p = "UNKNOWN"; 1627 else 1628 p = ada_delete_method_names[value]; 1629 strncpy(buf, p, sizeof(buf)); 1630 error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 1631 if (error != 0 || req->newptr == NULL) 1632 return (error); 1633 methods = 1 << ADA_DELETE_DISABLE; 1634 if ((softc->flags & ADA_FLAG_CAN_CFA) && 1635 !(softc->flags & ADA_FLAG_CAN_48BIT)) 1636 methods |= 1 << ADA_DELETE_CFA_ERASE; 1637 if (softc->flags & ADA_FLAG_CAN_TRIM) 1638 methods |= 1 << ADA_DELETE_DSM_TRIM; 1639 if (softc->flags & ADA_FLAG_CAN_NCQ_TRIM) 1640 methods |= 1 << ADA_DELETE_NCQ_DSM_TRIM; 1641 for (i = 0; i <= ADA_DELETE_MAX; i++) { 1642 if (!(methods & (1 << i)) || 1643 strcmp(buf, ada_delete_method_names[i]) != 0) 1644 continue; 1645 softc->delete_method = i; 1646 return (0); 1647 } 1648 return (EINVAL); 1649 } 1650 1651 static int 1652 adabitsysctl(SYSCTL_HANDLER_ARGS) 1653 { 1654 u_int *flags = arg1; 1655 u_int test = arg2; 1656 int tmpout, error; 1657 1658 tmpout = !!(*flags & test); 1659 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout)); 1660 if (error || !req->newptr) 1661 return (error); 1662 1663 return (EPERM); 1664 } 1665 1666 static int 1667 adaflagssysctl(SYSCTL_HANDLER_ARGS) 1668 { 1669 struct sbuf sbuf; 1670 struct ada_softc *softc = arg1; 1671 int error; 1672 1673 sbuf_new_for_sysctl(&sbuf, NULL, 0, req); 1674 if (softc->flags != 0) 1675 sbuf_printf(&sbuf, "0x%b", (unsigned)softc->flags, ADA_FLAG_STRING); 1676 else 1677 sbuf_printf(&sbuf, "0"); 1678 error = sbuf_finish(&sbuf); 1679 sbuf_delete(&sbuf); 1680 1681 return (error); 1682 } 1683 1684 static void 1685 adasetflags(struct ada_softc *softc, struct ccb_getdev *cgd) 1686 { 1687 if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) && 1688 (cgd->inq_flags & SID_DMA)) 1689 softc->flags |= ADA_FLAG_CAN_DMA; 1690 else 1691 softc->flags &= ~ADA_FLAG_CAN_DMA; 1692 1693 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) { 1694 softc->flags |= ADA_FLAG_CAN_48BIT; 1695 if (cgd->inq_flags & SID_DMA48) 1696 softc->flags |= ADA_FLAG_CAN_DMA48; 1697 else 1698 softc->flags &= ~ADA_FLAG_CAN_DMA48; 1699 } else 1700 softc->flags &= ~(ADA_FLAG_CAN_48BIT | ADA_FLAG_CAN_DMA48); 1701 1702 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE) 1703 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE; 1704 else 1705 softc->flags &= ~ADA_FLAG_CAN_FLUSHCACHE; 1706 1707 if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT) 1708 softc->flags |= ADA_FLAG_CAN_POWERMGT; 1709 else 1710 softc->flags &= ~ADA_FLAG_CAN_POWERMGT; 1711 1712 if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) && 1713 (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue)) 1714 softc->flags |= ADA_FLAG_CAN_NCQ; 1715 else 1716 softc->flags &= ~ADA_FLAG_CAN_NCQ; 1717 1718 if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) && 1719 (cgd->inq_flags & SID_DMA) && 1720 (softc->quirks & ADA_Q_NO_TRIM) == 0) { 1721 softc->flags |= ADA_FLAG_CAN_TRIM; 1722 softc->trim_max_ranges = TRIM_MAX_RANGES; 1723 if (cgd->ident_data.max_dsm_blocks != 0) { 1724 softc->trim_max_ranges = 1725 min(cgd->ident_data.max_dsm_blocks * 1726 ATA_DSM_BLK_RANGES, softc->trim_max_ranges); 1727 } 1728 /* 1729 * If we can do RCVSND_FPDMA_QUEUED commands, we may be able 1730 * to do NCQ trims, if we support trims at all. We also need 1731 * support from the SIM to do things properly. Perhaps we 1732 * should look at log 13 dword 0 bit 0 and dword 1 bit 0 are 1733 * set too... 1734 */ 1735 if ((softc->quirks & ADA_Q_NCQ_TRIM_BROKEN) == 0 && 1736 (softc->flags & ADA_FLAG_PIM_ATA_EXT) != 0 && 1737 (cgd->ident_data.satacapabilities2 & 1738 ATA_SUPPORT_RCVSND_FPDMA_QUEUED) != 0 && 1739 (softc->flags & ADA_FLAG_CAN_TRIM) != 0) 1740 softc->flags |= ADA_FLAG_CAN_NCQ_TRIM; 1741 else 1742 softc->flags &= ~ADA_FLAG_CAN_NCQ_TRIM; 1743 } else 1744 softc->flags &= ~(ADA_FLAG_CAN_TRIM | ADA_FLAG_CAN_NCQ_TRIM); 1745 1746 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA) 1747 softc->flags |= ADA_FLAG_CAN_CFA; 1748 else 1749 softc->flags &= ~ADA_FLAG_CAN_CFA; 1750 1751 /* 1752 * Now that we've set the appropriate flags, setup the delete 1753 * method. 1754 */ 1755 adasetdeletemethod(softc); 1756 1757 if ((cgd->ident_data.support.extension & ATA_SUPPORT_GENLOG) 1758 && ((softc->quirks & ADA_Q_LOG_BROKEN) == 0)) 1759 softc->flags |= ADA_FLAG_CAN_LOG; 1760 else 1761 softc->flags &= ~ADA_FLAG_CAN_LOG; 1762 1763 if ((cgd->ident_data.support3 & ATA_SUPPORT_ZONE_MASK) == 1764 ATA_SUPPORT_ZONE_HOST_AWARE) 1765 softc->zone_mode = ADA_ZONE_HOST_AWARE; 1766 else if (((cgd->ident_data.support3 & ATA_SUPPORT_ZONE_MASK) == 1767 ATA_SUPPORT_ZONE_DEV_MANAGED) 1768 || (softc->quirks & ADA_Q_SMR_DM)) 1769 softc->zone_mode = ADA_ZONE_DRIVE_MANAGED; 1770 else 1771 softc->zone_mode = ADA_ZONE_NONE; 1772 1773 if (cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) 1774 softc->flags |= ADA_FLAG_CAN_RAHEAD; 1775 else 1776 softc->flags &= ~ADA_FLAG_CAN_RAHEAD; 1777 1778 if (cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) 1779 softc->flags |= ADA_FLAG_CAN_WCACHE; 1780 else 1781 softc->flags &= ~ADA_FLAG_CAN_WCACHE; 1782 } 1783 1784 static cam_status 1785 adaregister(struct cam_periph *periph, void *arg) 1786 { 1787 struct ada_softc *softc; 1788 struct ccb_getdev *cgd; 1789 struct disk_params *dp; 1790 struct sbuf sb; 1791 char *announce_buf; 1792 caddr_t match; 1793 int quirks; 1794 1795 cgd = (struct ccb_getdev *)arg; 1796 if (cgd == NULL) { 1797 printf("adaregister: no getdev CCB, can't register device\n"); 1798 return(CAM_REQ_CMP_ERR); 1799 } 1800 1801 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF, 1802 M_NOWAIT|M_ZERO); 1803 1804 if (softc == NULL) { 1805 printf("adaregister: Unable to probe new device. " 1806 "Unable to allocate softc\n"); 1807 return(CAM_REQ_CMP_ERR); 1808 } 1809 1810 announce_buf = softc->announce_temp; 1811 bzero(announce_buf, ADA_ANNOUNCETMP_SZ); 1812 1813 if (cam_iosched_init(&softc->cam_iosched, periph) != 0) { 1814 printf("adaregister: Unable to probe new device. " 1815 "Unable to allocate iosched memory\n"); 1816 free(softc, M_DEVBUF); 1817 return(CAM_REQ_CMP_ERR); 1818 } 1819 1820 periph->softc = softc; 1821 xpt_path_inq(&softc->cpi, periph->path); 1822 1823 /* 1824 * See if this device has any quirks. 1825 */ 1826 match = cam_quirkmatch((caddr_t)&cgd->ident_data, 1827 (caddr_t)ada_quirk_table, 1828 nitems(ada_quirk_table), 1829 sizeof(*ada_quirk_table), ata_identify_match); 1830 if (match != NULL) 1831 softc->quirks = ((struct ada_quirk_entry *)match)->quirks; 1832 else 1833 softc->quirks = ADA_Q_NONE; 1834 1835 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph); 1836 1837 /* 1838 * Register this media as a disk 1839 */ 1840 (void)cam_periph_hold(periph, PRIBIO); 1841 cam_periph_unlock(periph); 1842 snprintf(announce_buf, ADA_ANNOUNCETMP_SZ, 1843 "kern.cam.ada.%d.quirks", periph->unit_number); 1844 quirks = softc->quirks; 1845 TUNABLE_INT_FETCH(announce_buf, &quirks); 1846 softc->quirks = quirks; 1847 softc->read_ahead = -1; 1848 snprintf(announce_buf, ADA_ANNOUNCETMP_SZ, 1849 "kern.cam.ada.%d.read_ahead", periph->unit_number); 1850 TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead); 1851 softc->write_cache = -1; 1852 snprintf(announce_buf, ADA_ANNOUNCETMP_SZ, 1853 "kern.cam.ada.%d.write_cache", periph->unit_number); 1854 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache); 1855 1856 /* 1857 * Set support flags based on the Identify data and quirks. 1858 */ 1859 adasetflags(softc, cgd); 1860 if (softc->cpi.hba_misc & PIM_ATA_EXT) 1861 softc->flags |= ADA_FLAG_PIM_ATA_EXT; 1862 1863 /* Disable queue sorting for non-rotational media by default. */ 1864 if (cgd->ident_data.media_rotation_rate == ATA_RATE_NON_ROTATING) { 1865 softc->flags &= ~ADA_FLAG_ROTATING; 1866 } else { 1867 softc->flags |= ADA_FLAG_ROTATING; 1868 } 1869 cam_iosched_set_sort_queue(softc->cam_iosched, 1870 (softc->flags & ADA_FLAG_ROTATING) ? -1 : 0); 1871 softc->disk = disk_alloc(); 1872 adasetgeom(softc, cgd); 1873 softc->disk->d_devstat = devstat_new_entry(periph->periph_name, 1874 periph->unit_number, softc->params.secsize, 1875 DEVSTAT_ALL_SUPPORTED, 1876 DEVSTAT_TYPE_DIRECT | 1877 XPORT_DEVSTAT_TYPE(softc->cpi.transport), 1878 DEVSTAT_PRIORITY_DISK); 1879 softc->disk->d_open = adaopen; 1880 softc->disk->d_close = adaclose; 1881 softc->disk->d_strategy = adastrategy; 1882 softc->disk->d_getattr = adagetattr; 1883 softc->disk->d_dump = adadump; 1884 softc->disk->d_gone = adadiskgonecb; 1885 softc->disk->d_name = "ada"; 1886 softc->disk->d_drv1 = periph; 1887 softc->disk->d_unit = periph->unit_number; 1888 1889 /* 1890 * Acquire a reference to the periph before we register with GEOM. 1891 * We'll release this reference once GEOM calls us back (via 1892 * adadiskgonecb()) telling us that our provider has been freed. 1893 */ 1894 if (cam_periph_acquire(periph) != 0) { 1895 xpt_print(periph->path, "%s: lost periph during " 1896 "registration!\n", __func__); 1897 cam_periph_lock(periph); 1898 return (CAM_REQ_CMP_ERR); 1899 } 1900 disk_create(softc->disk, DISK_VERSION); 1901 cam_periph_lock(periph); 1902 1903 dp = &softc->params; 1904 snprintf(announce_buf, ADA_ANNOUNCETMP_SZ, 1905 "%juMB (%ju %u byte sectors)", 1906 ((uintmax_t)dp->secsize * dp->sectors) / (1024 * 1024), 1907 (uintmax_t)dp->sectors, dp->secsize); 1908 1909 sbuf_new(&sb, softc->announce_buffer, ADA_ANNOUNCE_SZ, SBUF_FIXEDLEN); 1910 xpt_announce_periph_sbuf(periph, &sb, announce_buf); 1911 xpt_announce_quirks_sbuf(periph, &sb, softc->quirks, ADA_Q_BIT_STRING); 1912 sbuf_finish(&sb); 1913 sbuf_putbuf(&sb); 1914 1915 /* 1916 * Create our sysctl variables, now that we know 1917 * we have successfully attached. 1918 */ 1919 if (cam_periph_acquire(periph) == 0) 1920 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task); 1921 1922 /* 1923 * Add async callbacks for bus reset and 1924 * bus device reset calls. I don't bother 1925 * checking if this fails as, in most cases, 1926 * the system will function just fine without 1927 * them and the only alternative would be to 1928 * not attach the device on failure. 1929 */ 1930 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE | 1931 AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED, 1932 adaasync, periph, periph->path); 1933 1934 /* 1935 * Schedule a periodic event to occasionally send an 1936 * ordered tag to a device. 1937 */ 1938 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0); 1939 callout_reset(&softc->sendordered_c, 1940 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 1941 adasendorderedtag, softc); 1942 1943 if (ADA_RA >= 0 && softc->flags & ADA_FLAG_CAN_RAHEAD) { 1944 softc->state = ADA_STATE_RAHEAD; 1945 } else if (ADA_WC >= 0 && softc->flags & ADA_FLAG_CAN_WCACHE) { 1946 softc->state = ADA_STATE_WCACHE; 1947 } else if ((softc->flags & ADA_FLAG_CAN_LOG) 1948 && (softc->zone_mode != ADA_ZONE_NONE)) { 1949 softc->state = ADA_STATE_LOGDIR; 1950 } else { 1951 /* 1952 * Nothing to probe, so we can just transition to the 1953 * normal state. 1954 */ 1955 adaprobedone(periph, NULL); 1956 return(CAM_REQ_CMP); 1957 } 1958 1959 xpt_schedule(periph, CAM_PRIORITY_DEV); 1960 1961 return(CAM_REQ_CMP); 1962 } 1963 1964 static int 1965 ada_dsmtrim_req_create(struct ada_softc *softc, struct bio *bp, struct trim_request *req) 1966 { 1967 uint64_t lastlba = (uint64_t)-1, lbas = 0; 1968 int c, lastcount = 0, off, ranges = 0; 1969 1970 bzero(req, sizeof(*req)); 1971 TAILQ_INIT(&req->bps); 1972 do { 1973 uint64_t lba = bp->bio_pblkno; 1974 int count = bp->bio_bcount / softc->params.secsize; 1975 1976 /* Try to extend the previous range. */ 1977 if (lba == lastlba) { 1978 c = min(count, ATA_DSM_RANGE_MAX - lastcount); 1979 lastcount += c; 1980 off = (ranges - 1) * ATA_DSM_RANGE_SIZE; 1981 req->data[off + 6] = lastcount & 0xff; 1982 req->data[off + 7] = 1983 (lastcount >> 8) & 0xff; 1984 count -= c; 1985 lba += c; 1986 lbas += c; 1987 } 1988 1989 while (count > 0) { 1990 c = min(count, ATA_DSM_RANGE_MAX); 1991 off = ranges * ATA_DSM_RANGE_SIZE; 1992 req->data[off + 0] = lba & 0xff; 1993 req->data[off + 1] = (lba >> 8) & 0xff; 1994 req->data[off + 2] = (lba >> 16) & 0xff; 1995 req->data[off + 3] = (lba >> 24) & 0xff; 1996 req->data[off + 4] = (lba >> 32) & 0xff; 1997 req->data[off + 5] = (lba >> 40) & 0xff; 1998 req->data[off + 6] = c & 0xff; 1999 req->data[off + 7] = (c >> 8) & 0xff; 2000 lba += c; 2001 lbas += c; 2002 count -= c; 2003 lastcount = c; 2004 ranges++; 2005 /* 2006 * Its the caller's responsibility to ensure the 2007 * request will fit so we don't need to check for 2008 * overrun here 2009 */ 2010 } 2011 lastlba = lba; 2012 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue); 2013 2014 bp = cam_iosched_next_trim(softc->cam_iosched); 2015 if (bp == NULL) 2016 break; 2017 if (bp->bio_bcount / softc->params.secsize > 2018 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) { 2019 cam_iosched_put_back_trim(softc->cam_iosched, bp); 2020 break; 2021 } 2022 } while (1); 2023 softc->trim_count++; 2024 softc->trim_ranges += ranges; 2025 softc->trim_lbas += lbas; 2026 2027 return (ranges); 2028 } 2029 2030 static void 2031 ada_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 2032 { 2033 struct trim_request *req = &softc->trim_req; 2034 int ranges; 2035 2036 ranges = ada_dsmtrim_req_create(softc, bp, req); 2037 cam_fill_ataio(ataio, 2038 ada_retry_count, 2039 adadone, 2040 CAM_DIR_OUT, 2041 0, 2042 req->data, 2043 howmany(ranges, ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE, 2044 ada_default_timeout * 1000); 2045 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT, 2046 ATA_DSM_TRIM, 0, howmany(ranges, ATA_DSM_BLK_RANGES)); 2047 } 2048 2049 static void 2050 ada_ncq_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 2051 { 2052 struct trim_request *req = &softc->trim_req; 2053 int ranges; 2054 2055 ranges = ada_dsmtrim_req_create(softc, bp, req); 2056 cam_fill_ataio(ataio, 2057 ada_retry_count, 2058 adadone, 2059 CAM_DIR_OUT, 2060 0, 2061 req->data, 2062 howmany(ranges, ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE, 2063 ada_default_timeout * 1000); 2064 ata_ncq_cmd(ataio, 2065 ATA_SEND_FPDMA_QUEUED, 2066 0, 2067 howmany(ranges, ATA_DSM_BLK_RANGES)); 2068 ataio->cmd.sector_count_exp = ATA_SFPDMA_DSM; 2069 ataio->ata_flags |= ATA_FLAG_AUX; 2070 ataio->aux = 1; 2071 } 2072 2073 static void 2074 ada_cfaerase(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 2075 { 2076 struct trim_request *req = &softc->trim_req; 2077 uint64_t lba = bp->bio_pblkno; 2078 uint16_t count = bp->bio_bcount / softc->params.secsize; 2079 2080 bzero(req, sizeof(*req)); 2081 TAILQ_INIT(&req->bps); 2082 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue); 2083 2084 cam_fill_ataio(ataio, 2085 ada_retry_count, 2086 adadone, 2087 CAM_DIR_NONE, 2088 0, 2089 NULL, 2090 0, 2091 ada_default_timeout*1000); 2092 2093 if (count >= 256) 2094 count = 0; 2095 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count); 2096 } 2097 2098 static int 2099 ada_zone_bio_to_ata(int disk_zone_cmd) 2100 { 2101 switch (disk_zone_cmd) { 2102 case DISK_ZONE_OPEN: 2103 return ATA_ZM_OPEN_ZONE; 2104 case DISK_ZONE_CLOSE: 2105 return ATA_ZM_CLOSE_ZONE; 2106 case DISK_ZONE_FINISH: 2107 return ATA_ZM_FINISH_ZONE; 2108 case DISK_ZONE_RWP: 2109 return ATA_ZM_RWP; 2110 } 2111 2112 return -1; 2113 } 2114 2115 static int 2116 ada_zone_cmd(struct cam_periph *periph, union ccb *ccb, struct bio *bp, 2117 int *queue_ccb) 2118 { 2119 struct ada_softc *softc; 2120 int error; 2121 2122 error = 0; 2123 2124 if (bp->bio_cmd != BIO_ZONE) { 2125 error = EINVAL; 2126 goto bailout; 2127 } 2128 2129 softc = periph->softc; 2130 2131 switch (bp->bio_zone.zone_cmd) { 2132 case DISK_ZONE_OPEN: 2133 case DISK_ZONE_CLOSE: 2134 case DISK_ZONE_FINISH: 2135 case DISK_ZONE_RWP: { 2136 int zone_flags; 2137 int zone_sa; 2138 uint64_t lba; 2139 2140 zone_sa = ada_zone_bio_to_ata(bp->bio_zone.zone_cmd); 2141 if (zone_sa == -1) { 2142 xpt_print(periph->path, "Cannot translate zone " 2143 "cmd %#x to ATA\n", bp->bio_zone.zone_cmd); 2144 error = EINVAL; 2145 goto bailout; 2146 } 2147 2148 zone_flags = 0; 2149 lba = bp->bio_zone.zone_params.rwp.id; 2150 2151 if (bp->bio_zone.zone_params.rwp.flags & 2152 DISK_ZONE_RWP_FLAG_ALL) 2153 zone_flags |= ZBC_OUT_ALL; 2154 2155 ata_zac_mgmt_out(&ccb->ataio, 2156 /*retries*/ ada_retry_count, 2157 /*cbfcnp*/ adadone, 2158 /*use_ncq*/ (softc->flags & 2159 ADA_FLAG_PIM_ATA_EXT) ? 1 : 0, 2160 /*zm_action*/ zone_sa, 2161 /*zone_id*/ lba, 2162 /*zone_flags*/ zone_flags, 2163 /*sector_count*/ 0, 2164 /*data_ptr*/ NULL, 2165 /*dxfer_len*/ 0, 2166 /*timeout*/ ada_default_timeout * 1000); 2167 *queue_ccb = 1; 2168 2169 break; 2170 } 2171 case DISK_ZONE_REPORT_ZONES: { 2172 uint8_t *rz_ptr; 2173 uint32_t num_entries, alloc_size; 2174 struct disk_zone_report *rep; 2175 2176 rep = &bp->bio_zone.zone_params.report; 2177 2178 num_entries = rep->entries_allocated; 2179 if (num_entries == 0) { 2180 xpt_print(periph->path, "No entries allocated for " 2181 "Report Zones request\n"); 2182 error = EINVAL; 2183 goto bailout; 2184 } 2185 alloc_size = sizeof(struct scsi_report_zones_hdr) + 2186 (sizeof(struct scsi_report_zones_desc) * num_entries); 2187 alloc_size = min(alloc_size, softc->disk->d_maxsize); 2188 rz_ptr = malloc(alloc_size, M_ATADA, M_NOWAIT | M_ZERO); 2189 if (rz_ptr == NULL) { 2190 xpt_print(periph->path, "Unable to allocate memory " 2191 "for Report Zones request\n"); 2192 error = ENOMEM; 2193 goto bailout; 2194 } 2195 2196 ata_zac_mgmt_in(&ccb->ataio, 2197 /*retries*/ ada_retry_count, 2198 /*cbcfnp*/ adadone, 2199 /*use_ncq*/ (softc->flags & 2200 ADA_FLAG_PIM_ATA_EXT) ? 1 : 0, 2201 /*zm_action*/ ATA_ZM_REPORT_ZONES, 2202 /*zone_id*/ rep->starting_id, 2203 /*zone_flags*/ rep->rep_options, 2204 /*data_ptr*/ rz_ptr, 2205 /*dxfer_len*/ alloc_size, 2206 /*timeout*/ ada_default_timeout * 1000); 2207 2208 /* 2209 * For BIO_ZONE, this isn't normally needed. However, it 2210 * is used by devstat_end_transaction_bio() to determine 2211 * how much data was transferred. 2212 */ 2213 /* 2214 * XXX KDM we have a problem. But I'm not sure how to fix 2215 * it. devstat uses bio_bcount - bio_resid to calculate 2216 * the amount of data transferred. The GEOM disk code 2217 * uses bio_length - bio_resid to calculate the amount of 2218 * data in bio_completed. We have different structure 2219 * sizes above and below the ada(4) driver. So, if we 2220 * use the sizes above, the amount transferred won't be 2221 * quite accurate for devstat. If we use different sizes 2222 * for bio_bcount and bio_length (above and below 2223 * respectively), then the residual needs to match one or 2224 * the other. Everything is calculated after the bio 2225 * leaves the driver, so changing the values around isn't 2226 * really an option. For now, just set the count to the 2227 * passed in length. This means that the calculations 2228 * above (e.g. bio_completed) will be correct, but the 2229 * amount of data reported to devstat will be slightly 2230 * under or overstated. 2231 */ 2232 bp->bio_bcount = bp->bio_length; 2233 2234 *queue_ccb = 1; 2235 2236 break; 2237 } 2238 case DISK_ZONE_GET_PARAMS: { 2239 struct disk_zone_disk_params *params; 2240 2241 params = &bp->bio_zone.zone_params.disk_params; 2242 bzero(params, sizeof(*params)); 2243 2244 switch (softc->zone_mode) { 2245 case ADA_ZONE_DRIVE_MANAGED: 2246 params->zone_mode = DISK_ZONE_MODE_DRIVE_MANAGED; 2247 break; 2248 case ADA_ZONE_HOST_AWARE: 2249 params->zone_mode = DISK_ZONE_MODE_HOST_AWARE; 2250 break; 2251 case ADA_ZONE_HOST_MANAGED: 2252 params->zone_mode = DISK_ZONE_MODE_HOST_MANAGED; 2253 break; 2254 default: 2255 case ADA_ZONE_NONE: 2256 params->zone_mode = DISK_ZONE_MODE_NONE; 2257 break; 2258 } 2259 2260 if (softc->zone_flags & ADA_ZONE_FLAG_URSWRZ) 2261 params->flags |= DISK_ZONE_DISK_URSWRZ; 2262 2263 if (softc->zone_flags & ADA_ZONE_FLAG_OPT_SEQ_SET) { 2264 params->optimal_seq_zones = softc->optimal_seq_zones; 2265 params->flags |= DISK_ZONE_OPT_SEQ_SET; 2266 } 2267 2268 if (softc->zone_flags & ADA_ZONE_FLAG_OPT_NONSEQ_SET) { 2269 params->optimal_nonseq_zones = 2270 softc->optimal_nonseq_zones; 2271 params->flags |= DISK_ZONE_OPT_NONSEQ_SET; 2272 } 2273 2274 if (softc->zone_flags & ADA_ZONE_FLAG_MAX_SEQ_SET) { 2275 params->max_seq_zones = softc->max_seq_zones; 2276 params->flags |= DISK_ZONE_MAX_SEQ_SET; 2277 } 2278 if (softc->zone_flags & ADA_ZONE_FLAG_RZ_SUP) 2279 params->flags |= DISK_ZONE_RZ_SUP; 2280 2281 if (softc->zone_flags & ADA_ZONE_FLAG_OPEN_SUP) 2282 params->flags |= DISK_ZONE_OPEN_SUP; 2283 2284 if (softc->zone_flags & ADA_ZONE_FLAG_CLOSE_SUP) 2285 params->flags |= DISK_ZONE_CLOSE_SUP; 2286 2287 if (softc->zone_flags & ADA_ZONE_FLAG_FINISH_SUP) 2288 params->flags |= DISK_ZONE_FINISH_SUP; 2289 2290 if (softc->zone_flags & ADA_ZONE_FLAG_RWP_SUP) 2291 params->flags |= DISK_ZONE_RWP_SUP; 2292 break; 2293 } 2294 default: 2295 break; 2296 } 2297 bailout: 2298 return (error); 2299 } 2300 2301 static void 2302 adastart(struct cam_periph *periph, union ccb *start_ccb) 2303 { 2304 struct ada_softc *softc = (struct ada_softc *)periph->softc; 2305 struct ccb_ataio *ataio = &start_ccb->ataio; 2306 2307 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n")); 2308 2309 switch (softc->state) { 2310 case ADA_STATE_NORMAL: 2311 { 2312 struct bio *bp; 2313 u_int8_t tag_code; 2314 2315 bp = cam_iosched_next_bio(softc->cam_iosched); 2316 if (bp == NULL) { 2317 xpt_release_ccb(start_ccb); 2318 break; 2319 } 2320 2321 if ((bp->bio_flags & BIO_ORDERED) != 0 || 2322 (bp->bio_cmd != BIO_DELETE && (softc->flags & ADA_FLAG_NEED_OTAG) != 0)) { 2323 softc->flags &= ~ADA_FLAG_NEED_OTAG; 2324 softc->flags |= ADA_FLAG_WAS_OTAG; 2325 tag_code = 0; 2326 } else { 2327 tag_code = 1; 2328 } 2329 switch (bp->bio_cmd) { 2330 case BIO_WRITE: 2331 case BIO_READ: 2332 { 2333 uint64_t lba = bp->bio_pblkno; 2334 uint16_t count = bp->bio_bcount / softc->params.secsize; 2335 void *data_ptr; 2336 int rw_op; 2337 2338 if (bp->bio_cmd == BIO_WRITE) { 2339 softc->flags |= ADA_FLAG_DIRTY; 2340 rw_op = CAM_DIR_OUT; 2341 } else { 2342 rw_op = CAM_DIR_IN; 2343 } 2344 2345 data_ptr = bp->bio_data; 2346 if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) { 2347 rw_op |= CAM_DATA_BIO; 2348 data_ptr = bp; 2349 } 2350 2351 #ifdef CAM_TEST_FAILURE 2352 int fail = 0; 2353 2354 /* 2355 * Support the failure ioctls. If the command is a 2356 * read, and there are pending forced read errors, or 2357 * if a write and pending write errors, then fail this 2358 * operation with EIO. This is useful for testing 2359 * purposes. Also, support having every Nth read fail. 2360 * 2361 * This is a rather blunt tool. 2362 */ 2363 if (bp->bio_cmd == BIO_READ) { 2364 if (softc->force_read_error) { 2365 softc->force_read_error--; 2366 fail = 1; 2367 } 2368 if (softc->periodic_read_error > 0) { 2369 if (++softc->periodic_read_count >= 2370 softc->periodic_read_error) { 2371 softc->periodic_read_count = 0; 2372 fail = 1; 2373 } 2374 } 2375 } else { 2376 if (softc->force_write_error) { 2377 softc->force_write_error--; 2378 fail = 1; 2379 } 2380 } 2381 if (fail) { 2382 biofinish(bp, NULL, EIO); 2383 xpt_release_ccb(start_ccb); 2384 adaschedule(periph); 2385 return; 2386 } 2387 #endif 2388 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 || 2389 round_page(bp->bio_bcount + bp->bio_ma_offset) / 2390 PAGE_SIZE == bp->bio_ma_n, 2391 ("Short bio %p", bp)); 2392 cam_fill_ataio(ataio, 2393 ada_retry_count, 2394 adadone, 2395 rw_op, 2396 0, 2397 data_ptr, 2398 bp->bio_bcount, 2399 ada_default_timeout*1000); 2400 2401 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) { 2402 if (bp->bio_cmd == BIO_READ) { 2403 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED, 2404 lba, count); 2405 } else { 2406 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED, 2407 lba, count); 2408 } 2409 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) && 2410 (lba + count >= ATA_MAX_28BIT_LBA || 2411 count > 256)) { 2412 if (softc->flags & ADA_FLAG_CAN_DMA48) { 2413 if (bp->bio_cmd == BIO_READ) { 2414 ata_48bit_cmd(ataio, ATA_READ_DMA48, 2415 0, lba, count); 2416 } else { 2417 ata_48bit_cmd(ataio, ATA_WRITE_DMA48, 2418 0, lba, count); 2419 } 2420 } else { 2421 if (bp->bio_cmd == BIO_READ) { 2422 ata_48bit_cmd(ataio, ATA_READ_MUL48, 2423 0, lba, count); 2424 } else { 2425 ata_48bit_cmd(ataio, ATA_WRITE_MUL48, 2426 0, lba, count); 2427 } 2428 } 2429 } else { 2430 if (count == 256) 2431 count = 0; 2432 if (softc->flags & ADA_FLAG_CAN_DMA) { 2433 if (bp->bio_cmd == BIO_READ) { 2434 ata_28bit_cmd(ataio, ATA_READ_DMA, 2435 0, lba, count); 2436 } else { 2437 ata_28bit_cmd(ataio, ATA_WRITE_DMA, 2438 0, lba, count); 2439 } 2440 } else { 2441 if (bp->bio_cmd == BIO_READ) { 2442 ata_28bit_cmd(ataio, ATA_READ_MUL, 2443 0, lba, count); 2444 } else { 2445 ata_28bit_cmd(ataio, ATA_WRITE_MUL, 2446 0, lba, count); 2447 } 2448 } 2449 } 2450 break; 2451 } 2452 case BIO_DELETE: 2453 switch (softc->delete_method) { 2454 case ADA_DELETE_NCQ_DSM_TRIM: 2455 ada_ncq_dsmtrim(softc, bp, ataio); 2456 break; 2457 case ADA_DELETE_DSM_TRIM: 2458 ada_dsmtrim(softc, bp, ataio); 2459 break; 2460 case ADA_DELETE_CFA_ERASE: 2461 ada_cfaerase(softc, bp, ataio); 2462 break; 2463 default: 2464 biofinish(bp, NULL, EOPNOTSUPP); 2465 xpt_release_ccb(start_ccb); 2466 adaschedule(periph); 2467 return; 2468 } 2469 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM; 2470 start_ccb->ccb_h.flags |= CAM_UNLOCKED; 2471 cam_iosched_submit_trim(softc->cam_iosched); 2472 goto out; 2473 case BIO_FLUSH: 2474 cam_fill_ataio(ataio, 2475 1, 2476 adadone, 2477 CAM_DIR_NONE, 2478 0, 2479 NULL, 2480 0, 2481 ada_default_timeout*1000); 2482 2483 if (softc->flags & ADA_FLAG_CAN_48BIT) 2484 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0); 2485 else 2486 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0); 2487 break; 2488 case BIO_ZONE: { 2489 int error, queue_ccb; 2490 2491 queue_ccb = 0; 2492 2493 error = ada_zone_cmd(periph, start_ccb, bp, &queue_ccb); 2494 if ((error != 0) 2495 || (queue_ccb == 0)) { 2496 biofinish(bp, NULL, error); 2497 xpt_release_ccb(start_ccb); 2498 return; 2499 } 2500 break; 2501 } 2502 default: 2503 biofinish(bp, NULL, EOPNOTSUPP); 2504 xpt_release_ccb(start_ccb); 2505 return; 2506 } 2507 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO; 2508 start_ccb->ccb_h.flags |= CAM_UNLOCKED; 2509 out: 2510 start_ccb->ccb_h.ccb_bp = bp; 2511 softc->outstanding_cmds++; 2512 softc->refcount++; 2513 cam_periph_unlock(periph); 2514 xpt_action(start_ccb); 2515 cam_periph_lock(periph); 2516 2517 /* May have more work to do, so ensure we stay scheduled */ 2518 adaschedule(periph); 2519 break; 2520 } 2521 case ADA_STATE_RAHEAD: 2522 case ADA_STATE_WCACHE: 2523 { 2524 cam_fill_ataio(ataio, 2525 1, 2526 adadone, 2527 CAM_DIR_NONE, 2528 0, 2529 NULL, 2530 0, 2531 ada_default_timeout*1000); 2532 2533 if (softc->state == ADA_STATE_RAHEAD) { 2534 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ? 2535 ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0); 2536 start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD; 2537 } else { 2538 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ? 2539 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0); 2540 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE; 2541 } 2542 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 2543 xpt_action(start_ccb); 2544 break; 2545 } 2546 case ADA_STATE_LOGDIR: 2547 { 2548 struct ata_gp_log_dir *log_dir; 2549 2550 if ((softc->flags & ADA_FLAG_CAN_LOG) == 0) { 2551 adaprobedone(periph, start_ccb); 2552 break; 2553 } 2554 2555 log_dir = malloc(sizeof(*log_dir), M_ATADA, M_NOWAIT|M_ZERO); 2556 if (log_dir == NULL) { 2557 xpt_print(periph->path, "Couldn't malloc log_dir " 2558 "data\n"); 2559 softc->state = ADA_STATE_NORMAL; 2560 xpt_release_ccb(start_ccb); 2561 break; 2562 } 2563 2564 ata_read_log(ataio, 2565 /*retries*/1, 2566 /*cbfcnp*/adadone, 2567 /*log_address*/ ATA_LOG_DIRECTORY, 2568 /*page_number*/ 0, 2569 /*block_count*/ 1, 2570 /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ? 2571 CAM_ATAIO_DMA : 0, 2572 /*data_ptr*/ (uint8_t *)log_dir, 2573 /*dxfer_len*/sizeof(*log_dir), 2574 /*timeout*/ada_default_timeout*1000); 2575 2576 start_ccb->ccb_h.ccb_state = ADA_CCB_LOGDIR; 2577 xpt_action(start_ccb); 2578 break; 2579 } 2580 case ADA_STATE_IDDIR: 2581 { 2582 struct ata_identify_log_pages *id_dir; 2583 2584 id_dir = malloc(sizeof(*id_dir), M_ATADA, M_NOWAIT | M_ZERO); 2585 if (id_dir == NULL) { 2586 xpt_print(periph->path, "Couldn't malloc id_dir " 2587 "data\n"); 2588 adaprobedone(periph, start_ccb); 2589 break; 2590 } 2591 2592 ata_read_log(ataio, 2593 /*retries*/1, 2594 /*cbfcnp*/adadone, 2595 /*log_address*/ ATA_IDENTIFY_DATA_LOG, 2596 /*page_number*/ ATA_IDL_PAGE_LIST, 2597 /*block_count*/ 1, 2598 /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ? 2599 CAM_ATAIO_DMA : 0, 2600 /*data_ptr*/ (uint8_t *)id_dir, 2601 /*dxfer_len*/ sizeof(*id_dir), 2602 /*timeout*/ada_default_timeout*1000); 2603 2604 start_ccb->ccb_h.ccb_state = ADA_CCB_IDDIR; 2605 xpt_action(start_ccb); 2606 break; 2607 } 2608 case ADA_STATE_SUP_CAP: 2609 { 2610 struct ata_identify_log_sup_cap *sup_cap; 2611 2612 sup_cap = malloc(sizeof(*sup_cap), M_ATADA, M_NOWAIT|M_ZERO); 2613 if (sup_cap == NULL) { 2614 xpt_print(periph->path, "Couldn't malloc sup_cap " 2615 "data\n"); 2616 adaprobedone(periph, start_ccb); 2617 break; 2618 } 2619 2620 ata_read_log(ataio, 2621 /*retries*/1, 2622 /*cbfcnp*/adadone, 2623 /*log_address*/ ATA_IDENTIFY_DATA_LOG, 2624 /*page_number*/ ATA_IDL_SUP_CAP, 2625 /*block_count*/ 1, 2626 /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ? 2627 CAM_ATAIO_DMA : 0, 2628 /*data_ptr*/ (uint8_t *)sup_cap, 2629 /*dxfer_len*/ sizeof(*sup_cap), 2630 /*timeout*/ada_default_timeout*1000); 2631 2632 start_ccb->ccb_h.ccb_state = ADA_CCB_SUP_CAP; 2633 xpt_action(start_ccb); 2634 break; 2635 } 2636 case ADA_STATE_ZONE: 2637 { 2638 struct ata_zoned_info_log *ata_zone; 2639 2640 ata_zone = malloc(sizeof(*ata_zone), M_ATADA, M_NOWAIT|M_ZERO); 2641 if (ata_zone == NULL) { 2642 xpt_print(periph->path, "Couldn't malloc ata_zone " 2643 "data\n"); 2644 adaprobedone(periph, start_ccb); 2645 break; 2646 } 2647 2648 ata_read_log(ataio, 2649 /*retries*/1, 2650 /*cbfcnp*/adadone, 2651 /*log_address*/ ATA_IDENTIFY_DATA_LOG, 2652 /*page_number*/ ATA_IDL_ZDI, 2653 /*block_count*/ 1, 2654 /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ? 2655 CAM_ATAIO_DMA : 0, 2656 /*data_ptr*/ (uint8_t *)ata_zone, 2657 /*dxfer_len*/ sizeof(*ata_zone), 2658 /*timeout*/ada_default_timeout*1000); 2659 2660 start_ccb->ccb_h.ccb_state = ADA_CCB_ZONE; 2661 xpt_action(start_ccb); 2662 break; 2663 } 2664 } 2665 } 2666 2667 static void 2668 adaprobedone(struct cam_periph *periph, union ccb *ccb) 2669 { 2670 struct ada_softc *softc; 2671 2672 softc = (struct ada_softc *)periph->softc; 2673 2674 if (ccb != NULL) 2675 xpt_release_ccb(ccb); 2676 2677 softc->state = ADA_STATE_NORMAL; 2678 softc->flags |= ADA_FLAG_PROBED; 2679 adaschedule(periph); 2680 if ((softc->flags & ADA_FLAG_ANNOUNCED) == 0) { 2681 softc->flags |= ADA_FLAG_ANNOUNCED; 2682 cam_periph_unhold(periph); 2683 } else { 2684 cam_periph_release_locked(periph); 2685 } 2686 } 2687 2688 static void 2689 adazonedone(struct cam_periph *periph, union ccb *ccb) 2690 { 2691 struct bio *bp; 2692 2693 bp = (struct bio *)ccb->ccb_h.ccb_bp; 2694 2695 switch (bp->bio_zone.zone_cmd) { 2696 case DISK_ZONE_OPEN: 2697 case DISK_ZONE_CLOSE: 2698 case DISK_ZONE_FINISH: 2699 case DISK_ZONE_RWP: 2700 break; 2701 case DISK_ZONE_REPORT_ZONES: { 2702 uint32_t avail_len; 2703 struct disk_zone_report *rep; 2704 struct scsi_report_zones_hdr *hdr; 2705 struct scsi_report_zones_desc *desc; 2706 struct disk_zone_rep_entry *entry; 2707 uint32_t hdr_len, num_avail; 2708 uint32_t num_to_fill, i; 2709 2710 rep = &bp->bio_zone.zone_params.report; 2711 avail_len = ccb->ataio.dxfer_len - ccb->ataio.resid; 2712 /* 2713 * Note that bio_resid isn't normally used for zone 2714 * commands, but it is used by devstat_end_transaction_bio() 2715 * to determine how much data was transferred. Because 2716 * the size of the SCSI/ATA data structures is different 2717 * than the size of the BIO interface structures, the 2718 * amount of data actually transferred from the drive will 2719 * be different than the amount of data transferred to 2720 * the user. 2721 */ 2722 hdr = (struct scsi_report_zones_hdr *)ccb->ataio.data_ptr; 2723 if (avail_len < sizeof(*hdr)) { 2724 /* 2725 * Is there a better error than EIO here? We asked 2726 * for at least the header, and we got less than 2727 * that. 2728 */ 2729 bp->bio_error = EIO; 2730 bp->bio_flags |= BIO_ERROR; 2731 bp->bio_resid = bp->bio_bcount; 2732 break; 2733 } 2734 2735 hdr_len = le32dec(hdr->length); 2736 if (hdr_len > 0) 2737 rep->entries_available = hdr_len / sizeof(*desc); 2738 else 2739 rep->entries_available = 0; 2740 /* 2741 * NOTE: using the same values for the BIO version of the 2742 * same field as the SCSI/ATA values. This means we could 2743 * get some additional values that aren't defined in bio.h 2744 * if more values of the same field are defined later. 2745 */ 2746 rep->header.same = hdr->byte4 & SRZ_SAME_MASK; 2747 rep->header.maximum_lba = le64dec(hdr->maximum_lba); 2748 /* 2749 * If the drive reports no entries that match the query, 2750 * we're done. 2751 */ 2752 if (hdr_len == 0) { 2753 rep->entries_filled = 0; 2754 bp->bio_resid = bp->bio_bcount; 2755 break; 2756 } 2757 2758 num_avail = min((avail_len - sizeof(*hdr)) / sizeof(*desc), 2759 hdr_len / sizeof(*desc)); 2760 /* 2761 * If the drive didn't return any data, then we're done. 2762 */ 2763 if (num_avail == 0) { 2764 rep->entries_filled = 0; 2765 bp->bio_resid = bp->bio_bcount; 2766 break; 2767 } 2768 2769 num_to_fill = min(num_avail, rep->entries_allocated); 2770 /* 2771 * If the user didn't allocate any entries for us to fill, 2772 * we're done. 2773 */ 2774 if (num_to_fill == 0) { 2775 rep->entries_filled = 0; 2776 bp->bio_resid = bp->bio_bcount; 2777 break; 2778 } 2779 2780 for (i = 0, desc = &hdr->desc_list[0], entry=&rep->entries[0]; 2781 i < num_to_fill; i++, desc++, entry++) { 2782 /* 2783 * NOTE: we're mapping the values here directly 2784 * from the SCSI/ATA bit definitions to the bio.h 2785 * definitions. There is also a warning in 2786 * disk_zone.h, but the impact is that if 2787 * additional values are added in the SCSI/ATA 2788 * specs these will be visible to consumers of 2789 * this interface. 2790 */ 2791 entry->zone_type = desc->zone_type & SRZ_TYPE_MASK; 2792 entry->zone_condition = 2793 (desc->zone_flags & SRZ_ZONE_COND_MASK) >> 2794 SRZ_ZONE_COND_SHIFT; 2795 entry->zone_flags |= desc->zone_flags & 2796 (SRZ_ZONE_NON_SEQ|SRZ_ZONE_RESET); 2797 entry->zone_length = le64dec(desc->zone_length); 2798 entry->zone_start_lba = le64dec(desc->zone_start_lba); 2799 entry->write_pointer_lba = 2800 le64dec(desc->write_pointer_lba); 2801 } 2802 rep->entries_filled = num_to_fill; 2803 /* 2804 * Note that this residual is accurate from the user's 2805 * standpoint, but the amount transferred isn't accurate 2806 * from the standpoint of what actually came back from the 2807 * drive. 2808 */ 2809 bp->bio_resid = bp->bio_bcount - (num_to_fill * sizeof(*entry)); 2810 break; 2811 } 2812 case DISK_ZONE_GET_PARAMS: 2813 default: 2814 /* 2815 * In theory we should not get a GET_PARAMS bio, since it 2816 * should be handled without queueing the command to the 2817 * drive. 2818 */ 2819 panic("%s: Invalid zone command %d", __func__, 2820 bp->bio_zone.zone_cmd); 2821 break; 2822 } 2823 2824 if (bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES) 2825 free(ccb->ataio.data_ptr, M_ATADA); 2826 } 2827 2828 static void 2829 adadone(struct cam_periph *periph, union ccb *done_ccb) 2830 { 2831 struct ada_softc *softc; 2832 struct ccb_ataio *ataio; 2833 struct cam_path *path; 2834 uint32_t priority; 2835 int state; 2836 2837 softc = (struct ada_softc *)periph->softc; 2838 ataio = &done_ccb->ataio; 2839 path = done_ccb->ccb_h.path; 2840 priority = done_ccb->ccb_h.pinfo.priority; 2841 2842 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n")); 2843 2844 state = ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK; 2845 switch (state) { 2846 case ADA_CCB_BUFFER_IO: 2847 case ADA_CCB_TRIM: 2848 { 2849 struct bio *bp; 2850 int error; 2851 2852 cam_periph_lock(periph); 2853 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 2854 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2855 error = adaerror(done_ccb, 0, 0); 2856 if (error == ERESTART) { 2857 /* A retry was scheduled, so just return. */ 2858 cam_periph_unlock(periph); 2859 return; 2860 } 2861 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 2862 cam_release_devq(path, 2863 /*relsim_flags*/0, 2864 /*reduction*/0, 2865 /*timeout*/0, 2866 /*getcount_only*/0); 2867 /* 2868 * If we get an error on an NCQ DSM TRIM, fall back 2869 * to a non-NCQ DSM TRIM forever. Please note that if 2870 * CAN_NCQ_TRIM is set, CAN_TRIM is necessarily set too. 2871 * However, for this one trim, we treat it as advisory 2872 * and return success up the stack. 2873 */ 2874 if (state == ADA_CCB_TRIM && 2875 error != 0 && 2876 (softc->flags & ADA_FLAG_CAN_NCQ_TRIM) != 0) { 2877 softc->flags &= ~ADA_FLAG_CAN_NCQ_TRIM; 2878 error = 0; 2879 adasetdeletemethod(softc); 2880 } 2881 } else { 2882 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 2883 panic("REQ_CMP with QFRZN"); 2884 2885 error = 0; 2886 } 2887 bp->bio_error = error; 2888 if (error != 0) { 2889 bp->bio_resid = bp->bio_bcount; 2890 bp->bio_flags |= BIO_ERROR; 2891 } else { 2892 if (bp->bio_cmd == BIO_ZONE) 2893 adazonedone(periph, done_ccb); 2894 else if (state == ADA_CCB_TRIM) 2895 bp->bio_resid = 0; 2896 else 2897 bp->bio_resid = ataio->resid; 2898 2899 if ((bp->bio_resid > 0) 2900 && (bp->bio_cmd != BIO_ZONE)) 2901 bp->bio_flags |= BIO_ERROR; 2902 } 2903 softc->outstanding_cmds--; 2904 if (softc->outstanding_cmds == 0) 2905 softc->flags |= ADA_FLAG_WAS_OTAG; 2906 2907 /* 2908 * We need to call cam_iosched before we call biodone so that we 2909 * don't measure any activity that happens in the completion 2910 * routine, which in the case of sendfile can be quite 2911 * extensive. Release the periph refcount taken in adastart() 2912 * for each CCB. 2913 */ 2914 cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb); 2915 xpt_release_ccb(done_ccb); 2916 KASSERT(softc->refcount >= 1, ("adadone softc %p refcount %d", softc, softc->refcount)); 2917 softc->refcount--; 2918 if (state == ADA_CCB_TRIM) { 2919 TAILQ_HEAD(, bio) queue; 2920 struct bio *bp1; 2921 2922 TAILQ_INIT(&queue); 2923 TAILQ_CONCAT(&queue, &softc->trim_req.bps, bio_queue); 2924 /* 2925 * Normally, the xpt_release_ccb() above would make sure 2926 * that when we have more work to do, that work would 2927 * get kicked off. However, we specifically keep 2928 * trim_running set to 0 before the call above to allow 2929 * other I/O to progress when many BIO_DELETE requests 2930 * are pushed down. We set trim_running to 0 and call 2931 * daschedule again so that we don't stall if there are 2932 * no other I/Os pending apart from BIO_DELETEs. 2933 */ 2934 cam_iosched_trim_done(softc->cam_iosched); 2935 adaschedule(periph); 2936 cam_periph_unlock(periph); 2937 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) { 2938 TAILQ_REMOVE(&queue, bp1, bio_queue); 2939 bp1->bio_error = error; 2940 if (error != 0) { 2941 bp1->bio_flags |= BIO_ERROR; 2942 bp1->bio_resid = bp1->bio_bcount; 2943 } else 2944 bp1->bio_resid = 0; 2945 biodone(bp1); 2946 } 2947 } else { 2948 adaschedule(periph); 2949 cam_periph_unlock(periph); 2950 biodone(bp); 2951 } 2952 return; 2953 } 2954 case ADA_CCB_RAHEAD: 2955 { 2956 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2957 if (adaerror(done_ccb, 0, 0) == ERESTART) { 2958 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 2959 cam_release_devq(path, 0, 0, 0, FALSE); 2960 return; 2961 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 2962 cam_release_devq(path, 2963 /*relsim_flags*/0, 2964 /*reduction*/0, 2965 /*timeout*/0, 2966 /*getcount_only*/0); 2967 } 2968 } 2969 2970 /* 2971 * Since our peripheral may be invalidated by an error 2972 * above or an external event, we must release our CCB 2973 * before releasing the reference on the peripheral. 2974 * The peripheral will only go away once the last reference 2975 * is removed, and we need it around for the CCB release 2976 * operation. 2977 */ 2978 2979 xpt_release_ccb(done_ccb); 2980 softc->state = ADA_STATE_WCACHE; 2981 xpt_schedule(periph, priority); 2982 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 2983 cam_release_devq(path, 0, 0, 0, FALSE); 2984 return; 2985 } 2986 case ADA_CCB_WCACHE: 2987 { 2988 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2989 if (adaerror(done_ccb, 0, 0) == ERESTART) { 2990 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 2991 cam_release_devq(path, 0, 0, 0, FALSE); 2992 return; 2993 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 2994 cam_release_devq(path, 2995 /*relsim_flags*/0, 2996 /*reduction*/0, 2997 /*timeout*/0, 2998 /*getcount_only*/0); 2999 } 3000 } 3001 3002 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 3003 cam_release_devq(path, 0, 0, 0, FALSE); 3004 3005 if ((softc->flags & ADA_FLAG_CAN_LOG) 3006 && (softc->zone_mode != ADA_ZONE_NONE)) { 3007 xpt_release_ccb(done_ccb); 3008 softc->state = ADA_STATE_LOGDIR; 3009 xpt_schedule(periph, priority); 3010 } else { 3011 adaprobedone(periph, done_ccb); 3012 } 3013 return; 3014 } 3015 case ADA_CCB_LOGDIR: 3016 { 3017 int error; 3018 3019 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 3020 error = 0; 3021 softc->valid_logdir_len = 0; 3022 bzero(&softc->ata_logdir, sizeof(softc->ata_logdir)); 3023 softc->valid_logdir_len = 3024 ataio->dxfer_len - ataio->resid; 3025 if (softc->valid_logdir_len > 0) 3026 bcopy(ataio->data_ptr, &softc->ata_logdir, 3027 min(softc->valid_logdir_len, 3028 sizeof(softc->ata_logdir))); 3029 /* 3030 * Figure out whether the Identify Device log is 3031 * supported. The General Purpose log directory 3032 * has a header, and lists the number of pages 3033 * available for each GP log identified by the 3034 * offset into the list. 3035 */ 3036 if ((softc->valid_logdir_len >= 3037 ((ATA_IDENTIFY_DATA_LOG + 1) * sizeof(uint16_t))) 3038 && (le16dec(softc->ata_logdir.header) == 3039 ATA_GP_LOG_DIR_VERSION) 3040 && (le16dec(&softc->ata_logdir.num_pages[ 3041 (ATA_IDENTIFY_DATA_LOG * 3042 sizeof(uint16_t)) - sizeof(uint16_t)]) > 0)){ 3043 softc->flags |= ADA_FLAG_CAN_IDLOG; 3044 } else { 3045 softc->flags &= ~ADA_FLAG_CAN_IDLOG; 3046 } 3047 } else { 3048 error = adaerror(done_ccb, CAM_RETRY_SELTO, 3049 SF_RETRY_UA|SF_NO_PRINT); 3050 if (error == ERESTART) 3051 return; 3052 else if (error != 0) { 3053 /* 3054 * If we can't get the ATA log directory, 3055 * then ATA logs are effectively not 3056 * supported even if the bit is set in the 3057 * identify data. 3058 */ 3059 softc->flags &= ~(ADA_FLAG_CAN_LOG | 3060 ADA_FLAG_CAN_IDLOG); 3061 if ((done_ccb->ccb_h.status & 3062 CAM_DEV_QFRZN) != 0) { 3063 /* Don't wedge this device's queue */ 3064 cam_release_devq(done_ccb->ccb_h.path, 3065 /*relsim_flags*/0, 3066 /*reduction*/0, 3067 /*timeout*/0, 3068 /*getcount_only*/0); 3069 } 3070 } 3071 } 3072 3073 free(ataio->data_ptr, M_ATADA); 3074 3075 if ((error == 0) 3076 && (softc->flags & ADA_FLAG_CAN_IDLOG)) { 3077 softc->state = ADA_STATE_IDDIR; 3078 xpt_release_ccb(done_ccb); 3079 xpt_schedule(periph, priority); 3080 } else 3081 adaprobedone(periph, done_ccb); 3082 3083 return; 3084 } 3085 case ADA_CCB_IDDIR: { 3086 int error; 3087 3088 if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 3089 off_t entries_offset, max_entries; 3090 error = 0; 3091 3092 softc->valid_iddir_len = 0; 3093 bzero(&softc->ata_iddir, sizeof(softc->ata_iddir)); 3094 softc->flags &= ~(ADA_FLAG_CAN_SUPCAP | 3095 ADA_FLAG_CAN_ZONE); 3096 softc->valid_iddir_len = 3097 ataio->dxfer_len - ataio->resid; 3098 if (softc->valid_iddir_len > 0) 3099 bcopy(ataio->data_ptr, &softc->ata_iddir, 3100 min(softc->valid_iddir_len, 3101 sizeof(softc->ata_iddir))); 3102 3103 entries_offset = 3104 __offsetof(struct ata_identify_log_pages,entries); 3105 max_entries = softc->valid_iddir_len - entries_offset; 3106 if ((softc->valid_iddir_len > (entries_offset + 1)) 3107 && (le64dec(softc->ata_iddir.header) == 3108 ATA_IDLOG_REVISION) 3109 && (softc->ata_iddir.entry_count > 0)) { 3110 int num_entries, i; 3111 3112 num_entries = softc->ata_iddir.entry_count; 3113 num_entries = min(num_entries, 3114 softc->valid_iddir_len - entries_offset); 3115 for (i = 0; i < num_entries && 3116 i < max_entries; i++) { 3117 if (softc->ata_iddir.entries[i] == 3118 ATA_IDL_SUP_CAP) 3119 softc->flags |= 3120 ADA_FLAG_CAN_SUPCAP; 3121 else if (softc->ata_iddir.entries[i]== 3122 ATA_IDL_ZDI) 3123 softc->flags |= 3124 ADA_FLAG_CAN_ZONE; 3125 3126 if ((softc->flags & 3127 ADA_FLAG_CAN_SUPCAP) 3128 && (softc->flags & 3129 ADA_FLAG_CAN_ZONE)) 3130 break; 3131 } 3132 } 3133 } else { 3134 error = adaerror(done_ccb, CAM_RETRY_SELTO, 3135 SF_RETRY_UA|SF_NO_PRINT); 3136 if (error == ERESTART) 3137 return; 3138 else if (error != 0) { 3139 /* 3140 * If we can't get the ATA Identify Data log 3141 * directory, then it effectively isn't 3142 * supported even if the ATA Log directory 3143 * a non-zero number of pages present for 3144 * this log. 3145 */ 3146 softc->flags &= ~ADA_FLAG_CAN_IDLOG; 3147 if ((done_ccb->ccb_h.status & 3148 CAM_DEV_QFRZN) != 0) { 3149 /* Don't wedge this device's queue */ 3150 cam_release_devq(done_ccb->ccb_h.path, 3151 /*relsim_flags*/0, 3152 /*reduction*/0, 3153 /*timeout*/0, 3154 /*getcount_only*/0); 3155 } 3156 } 3157 } 3158 3159 free(ataio->data_ptr, M_ATADA); 3160 3161 if ((error == 0) 3162 && (softc->flags & ADA_FLAG_CAN_SUPCAP)) { 3163 softc->state = ADA_STATE_SUP_CAP; 3164 xpt_release_ccb(done_ccb); 3165 xpt_schedule(periph, priority); 3166 } else 3167 adaprobedone(periph, done_ccb); 3168 return; 3169 } 3170 case ADA_CCB_SUP_CAP: { 3171 int error; 3172 3173 if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 3174 uint32_t valid_len; 3175 size_t needed_size; 3176 struct ata_identify_log_sup_cap *sup_cap; 3177 error = 0; 3178 3179 sup_cap = (struct ata_identify_log_sup_cap *) 3180 ataio->data_ptr; 3181 valid_len = ataio->dxfer_len - ataio->resid; 3182 needed_size = 3183 __offsetof(struct ata_identify_log_sup_cap, 3184 sup_zac_cap) + 1 + sizeof(sup_cap->sup_zac_cap); 3185 if (valid_len >= needed_size) { 3186 uint64_t zoned, zac_cap; 3187 3188 zoned = le64dec(sup_cap->zoned_cap); 3189 if (zoned & ATA_ZONED_VALID) { 3190 /* 3191 * This should have already been 3192 * set, because this is also in the 3193 * ATA identify data. 3194 */ 3195 if ((zoned & ATA_ZONED_MASK) == 3196 ATA_SUPPORT_ZONE_HOST_AWARE) 3197 softc->zone_mode = 3198 ADA_ZONE_HOST_AWARE; 3199 else if ((zoned & ATA_ZONED_MASK) == 3200 ATA_SUPPORT_ZONE_DEV_MANAGED) 3201 softc->zone_mode = 3202 ADA_ZONE_DRIVE_MANAGED; 3203 } 3204 3205 zac_cap = le64dec(sup_cap->sup_zac_cap); 3206 if (zac_cap & ATA_SUP_ZAC_CAP_VALID) { 3207 if (zac_cap & ATA_REPORT_ZONES_SUP) 3208 softc->zone_flags |= 3209 ADA_ZONE_FLAG_RZ_SUP; 3210 if (zac_cap & ATA_ND_OPEN_ZONE_SUP) 3211 softc->zone_flags |= 3212 ADA_ZONE_FLAG_OPEN_SUP; 3213 if (zac_cap & ATA_ND_CLOSE_ZONE_SUP) 3214 softc->zone_flags |= 3215 ADA_ZONE_FLAG_CLOSE_SUP; 3216 if (zac_cap & ATA_ND_FINISH_ZONE_SUP) 3217 softc->zone_flags |= 3218 ADA_ZONE_FLAG_FINISH_SUP; 3219 if (zac_cap & ATA_ND_RWP_SUP) 3220 softc->zone_flags |= 3221 ADA_ZONE_FLAG_RWP_SUP; 3222 } else { 3223 /* 3224 * This field was introduced in 3225 * ACS-4, r08 on April 28th, 2015. 3226 * If the drive firmware was written 3227 * to an earlier spec, it won't have 3228 * the field. So, assume all 3229 * commands are supported. 3230 */ 3231 softc->zone_flags |= 3232 ADA_ZONE_FLAG_SUP_MASK; 3233 } 3234 } 3235 } else { 3236 error = adaerror(done_ccb, CAM_RETRY_SELTO, 3237 SF_RETRY_UA|SF_NO_PRINT); 3238 if (error == ERESTART) 3239 return; 3240 else if (error != 0) { 3241 /* 3242 * If we can't get the ATA Identify Data 3243 * Supported Capabilities page, clear the 3244 * flag... 3245 */ 3246 softc->flags &= ~ADA_FLAG_CAN_SUPCAP; 3247 /* 3248 * And clear zone capabilities. 3249 */ 3250 softc->zone_flags &= ~ADA_ZONE_FLAG_SUP_MASK; 3251 if ((done_ccb->ccb_h.status & 3252 CAM_DEV_QFRZN) != 0) { 3253 /* Don't wedge this device's queue */ 3254 cam_release_devq(done_ccb->ccb_h.path, 3255 /*relsim_flags*/0, 3256 /*reduction*/0, 3257 /*timeout*/0, 3258 /*getcount_only*/0); 3259 } 3260 } 3261 } 3262 3263 free(ataio->data_ptr, M_ATADA); 3264 3265 if ((error == 0) 3266 && (softc->flags & ADA_FLAG_CAN_ZONE)) { 3267 softc->state = ADA_STATE_ZONE; 3268 xpt_release_ccb(done_ccb); 3269 xpt_schedule(periph, priority); 3270 } else 3271 adaprobedone(periph, done_ccb); 3272 return; 3273 } 3274 case ADA_CCB_ZONE: { 3275 int error; 3276 3277 if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 3278 struct ata_zoned_info_log *zi_log; 3279 uint32_t valid_len; 3280 size_t needed_size; 3281 3282 zi_log = (struct ata_zoned_info_log *)ataio->data_ptr; 3283 3284 valid_len = ataio->dxfer_len - ataio->resid; 3285 needed_size = __offsetof(struct ata_zoned_info_log, 3286 version_info) + 1 + sizeof(zi_log->version_info); 3287 if (valid_len >= needed_size) { 3288 uint64_t tmpvar; 3289 3290 tmpvar = le64dec(zi_log->zoned_cap); 3291 if (tmpvar & ATA_ZDI_CAP_VALID) { 3292 if (tmpvar & ATA_ZDI_CAP_URSWRZ) 3293 softc->zone_flags |= 3294 ADA_ZONE_FLAG_URSWRZ; 3295 else 3296 softc->zone_flags &= 3297 ~ADA_ZONE_FLAG_URSWRZ; 3298 } 3299 tmpvar = le64dec(zi_log->optimal_seq_zones); 3300 if (tmpvar & ATA_ZDI_OPT_SEQ_VALID) { 3301 softc->zone_flags |= 3302 ADA_ZONE_FLAG_OPT_SEQ_SET; 3303 softc->optimal_seq_zones = (tmpvar & 3304 ATA_ZDI_OPT_SEQ_MASK); 3305 } else { 3306 softc->zone_flags &= 3307 ~ADA_ZONE_FLAG_OPT_SEQ_SET; 3308 softc->optimal_seq_zones = 0; 3309 } 3310 3311 tmpvar =le64dec(zi_log->optimal_nonseq_zones); 3312 if (tmpvar & ATA_ZDI_OPT_NS_VALID) { 3313 softc->zone_flags |= 3314 ADA_ZONE_FLAG_OPT_NONSEQ_SET; 3315 softc->optimal_nonseq_zones = 3316 (tmpvar & ATA_ZDI_OPT_NS_MASK); 3317 } else { 3318 softc->zone_flags &= 3319 ~ADA_ZONE_FLAG_OPT_NONSEQ_SET; 3320 softc->optimal_nonseq_zones = 0; 3321 } 3322 3323 tmpvar = le64dec(zi_log->max_seq_req_zones); 3324 if (tmpvar & ATA_ZDI_MAX_SEQ_VALID) { 3325 softc->zone_flags |= 3326 ADA_ZONE_FLAG_MAX_SEQ_SET; 3327 softc->max_seq_zones = 3328 (tmpvar & ATA_ZDI_MAX_SEQ_MASK); 3329 } else { 3330 softc->zone_flags &= 3331 ~ADA_ZONE_FLAG_MAX_SEQ_SET; 3332 softc->max_seq_zones = 0; 3333 } 3334 } 3335 } else { 3336 error = adaerror(done_ccb, CAM_RETRY_SELTO, 3337 SF_RETRY_UA|SF_NO_PRINT); 3338 if (error == ERESTART) 3339 return; 3340 else if (error != 0) { 3341 softc->flags &= ~ADA_FLAG_CAN_ZONE; 3342 softc->flags &= ~ADA_ZONE_FLAG_SET_MASK; 3343 3344 if ((done_ccb->ccb_h.status & 3345 CAM_DEV_QFRZN) != 0) { 3346 /* Don't wedge this device's queue */ 3347 cam_release_devq(done_ccb->ccb_h.path, 3348 /*relsim_flags*/0, 3349 /*reduction*/0, 3350 /*timeout*/0, 3351 /*getcount_only*/0); 3352 } 3353 } 3354 } 3355 free(ataio->data_ptr, M_ATADA); 3356 3357 adaprobedone(periph, done_ccb); 3358 return; 3359 } 3360 case ADA_CCB_DUMP: 3361 /* No-op. We're polling */ 3362 return; 3363 default: 3364 break; 3365 } 3366 xpt_release_ccb(done_ccb); 3367 } 3368 3369 static int 3370 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 3371 { 3372 #ifdef CAM_IO_STATS 3373 struct ada_softc *softc; 3374 struct cam_periph *periph; 3375 3376 periph = xpt_path_periph(ccb->ccb_h.path); 3377 softc = (struct ada_softc *)periph->softc; 3378 3379 switch (ccb->ccb_h.status & CAM_STATUS_MASK) { 3380 case CAM_CMD_TIMEOUT: 3381 softc->timeouts++; 3382 break; 3383 case CAM_REQ_ABORTED: 3384 case CAM_REQ_CMP_ERR: 3385 case CAM_REQ_TERMIO: 3386 case CAM_UNREC_HBA_ERROR: 3387 case CAM_DATA_RUN_ERR: 3388 case CAM_ATA_STATUS_ERROR: 3389 softc->errors++; 3390 break; 3391 default: 3392 break; 3393 } 3394 #endif 3395 3396 return(cam_periph_error(ccb, cam_flags, sense_flags)); 3397 } 3398 3399 static void 3400 adasetgeom(struct ada_softc *softc, struct ccb_getdev *cgd) 3401 { 3402 struct disk_params *dp = &softc->params; 3403 u_int64_t lbasize48; 3404 u_int32_t lbasize; 3405 u_int maxio, d_flags; 3406 3407 dp->secsize = ata_logical_sector_size(&cgd->ident_data); 3408 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) && 3409 cgd->ident_data.current_heads != 0 && 3410 cgd->ident_data.current_sectors != 0) { 3411 dp->heads = cgd->ident_data.current_heads; 3412 dp->secs_per_track = cgd->ident_data.current_sectors; 3413 dp->cylinders = cgd->ident_data.cylinders; 3414 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 | 3415 ((u_int32_t)cgd->ident_data.current_size_2 << 16); 3416 } else { 3417 dp->heads = cgd->ident_data.heads; 3418 dp->secs_per_track = cgd->ident_data.sectors; 3419 dp->cylinders = cgd->ident_data.cylinders; 3420 dp->sectors = cgd->ident_data.cylinders * 3421 (u_int32_t)(dp->heads * dp->secs_per_track); 3422 } 3423 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 | 3424 ((u_int32_t)cgd->ident_data.lba_size_2 << 16); 3425 3426 /* use the 28bit LBA size if valid or bigger than the CHS mapping */ 3427 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize) 3428 dp->sectors = lbasize; 3429 3430 /* use the 48bit LBA size if valid */ 3431 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) | 3432 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) | 3433 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) | 3434 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48); 3435 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) && 3436 lbasize48 > ATA_MAX_28BIT_LBA) 3437 dp->sectors = lbasize48; 3438 3439 maxio = softc->cpi.maxio; /* Honor max I/O size of SIM */ 3440 if (maxio == 0) 3441 maxio = DFLTPHYS; /* traditional default */ 3442 else if (maxio > maxphys) 3443 maxio = maxphys; /* for safety */ 3444 if (softc->flags & ADA_FLAG_CAN_48BIT) 3445 maxio = min(maxio, 65536 * softc->params.secsize); 3446 else /* 28bit ATA command limit */ 3447 maxio = min(maxio, 256 * softc->params.secsize); 3448 if (softc->quirks & ADA_Q_128KB) 3449 maxio = min(maxio, 128 * 1024); 3450 softc->disk->d_maxsize = maxio; 3451 d_flags = DISKFLAG_DIRECT_COMPLETION | DISKFLAG_CANZONE; 3452 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) 3453 d_flags |= DISKFLAG_CANFLUSHCACHE; 3454 if (softc->flags & ADA_FLAG_CAN_TRIM) { 3455 d_flags |= DISKFLAG_CANDELETE; 3456 softc->disk->d_delmaxsize = softc->params.secsize * 3457 ATA_DSM_RANGE_MAX * softc->trim_max_ranges; 3458 } else if ((softc->flags & ADA_FLAG_CAN_CFA) && 3459 !(softc->flags & ADA_FLAG_CAN_48BIT)) { 3460 d_flags |= DISKFLAG_CANDELETE; 3461 softc->disk->d_delmaxsize = 256 * softc->params.secsize; 3462 } else 3463 softc->disk->d_delmaxsize = maxio; 3464 if ((softc->cpi.hba_misc & PIM_UNMAPPED) != 0) { 3465 d_flags |= DISKFLAG_UNMAPPED_BIO; 3466 softc->flags |= ADA_FLAG_UNMAPPEDIO; 3467 } 3468 softc->disk->d_flags = d_flags; 3469 strlcpy(softc->disk->d_descr, cgd->ident_data.model, 3470 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model))); 3471 strlcpy(softc->disk->d_ident, cgd->ident_data.serial, 3472 MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial))); 3473 3474 softc->disk->d_sectorsize = softc->params.secsize; 3475 softc->disk->d_mediasize = (off_t)softc->params.sectors * 3476 softc->params.secsize; 3477 if (ata_physical_sector_size(&cgd->ident_data) != 3478 softc->params.secsize) { 3479 softc->disk->d_stripesize = 3480 ata_physical_sector_size(&cgd->ident_data); 3481 softc->disk->d_stripeoffset = (softc->disk->d_stripesize - 3482 ata_logical_sector_offset(&cgd->ident_data)) % 3483 softc->disk->d_stripesize; 3484 } else if (softc->quirks & ADA_Q_4K) { 3485 softc->disk->d_stripesize = 4096; 3486 softc->disk->d_stripeoffset = 0; 3487 } 3488 softc->disk->d_fwsectors = softc->params.secs_per_track; 3489 softc->disk->d_fwheads = softc->params.heads; 3490 softc->disk->d_rotation_rate = cgd->ident_data.media_rotation_rate; 3491 snprintf(softc->disk->d_attachment, sizeof(softc->disk->d_attachment), 3492 "%s%d", softc->cpi.dev_name, softc->cpi.unit_number); 3493 } 3494 3495 static void 3496 adasendorderedtag(void *arg) 3497 { 3498 struct ada_softc *softc = arg; 3499 3500 if (ada_send_ordered) { 3501 if (softc->outstanding_cmds > 0) { 3502 if ((softc->flags & ADA_FLAG_WAS_OTAG) == 0) 3503 softc->flags |= ADA_FLAG_NEED_OTAG; 3504 softc->flags &= ~ADA_FLAG_WAS_OTAG; 3505 } 3506 } 3507 /* Queue us up again */ 3508 callout_reset(&softc->sendordered_c, 3509 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 3510 adasendorderedtag, softc); 3511 } 3512 3513 /* 3514 * Step through all ADA peripheral drivers, and if the device is still open, 3515 * sync the disk cache to physical media. 3516 */ 3517 static void 3518 adaflush(void) 3519 { 3520 struct cam_periph *periph; 3521 struct ada_softc *softc; 3522 union ccb *ccb; 3523 int error; 3524 3525 CAM_PERIPH_FOREACH(periph, &adadriver) { 3526 softc = (struct ada_softc *)periph->softc; 3527 if (SCHEDULER_STOPPED()) { 3528 /* If we paniced with the lock held, do not recurse. */ 3529 if (!cam_periph_owned(periph) && 3530 (softc->flags & ADA_FLAG_OPEN)) { 3531 adadump(softc->disk, NULL, 0, 0, 0); 3532 } 3533 continue; 3534 } 3535 cam_periph_lock(periph); 3536 /* 3537 * We only sync the cache if the drive is still open, and 3538 * if the drive is capable of it.. 3539 */ 3540 if (((softc->flags & ADA_FLAG_OPEN) == 0) || 3541 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) { 3542 cam_periph_unlock(periph); 3543 continue; 3544 } 3545 3546 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3547 cam_fill_ataio(&ccb->ataio, 3548 0, 3549 NULL, 3550 CAM_DIR_NONE, 3551 0, 3552 NULL, 3553 0, 3554 ada_default_timeout*1000); 3555 if (softc->flags & ADA_FLAG_CAN_48BIT) 3556 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 3557 else 3558 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 3559 3560 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 3561 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 3562 softc->disk->d_devstat); 3563 if (error != 0) 3564 xpt_print(periph->path, "Synchronize cache failed\n"); 3565 xpt_release_ccb(ccb); 3566 cam_periph_unlock(periph); 3567 } 3568 } 3569 3570 static void 3571 adaspindown(uint8_t cmd, int flags) 3572 { 3573 struct cam_periph *periph; 3574 struct ada_softc *softc; 3575 struct ccb_ataio local_ccb; 3576 int error; 3577 3578 CAM_PERIPH_FOREACH(periph, &adadriver) { 3579 /* If we paniced with lock held - not recurse here. */ 3580 if (cam_periph_owned(periph)) 3581 continue; 3582 cam_periph_lock(periph); 3583 softc = (struct ada_softc *)periph->softc; 3584 /* 3585 * We only spin-down the drive if it is capable of it.. 3586 */ 3587 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 3588 cam_periph_unlock(periph); 3589 continue; 3590 } 3591 3592 if (bootverbose) 3593 xpt_print(periph->path, "spin-down\n"); 3594 3595 memset(&local_ccb, 0, sizeof(local_ccb)); 3596 xpt_setup_ccb(&local_ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 3597 local_ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 3598 3599 cam_fill_ataio(&local_ccb, 3600 0, 3601 NULL, 3602 CAM_DIR_NONE | flags, 3603 0, 3604 NULL, 3605 0, 3606 ada_default_timeout*1000); 3607 ata_28bit_cmd(&local_ccb, cmd, 0, 0, 0); 3608 error = cam_periph_runccb((union ccb *)&local_ccb, adaerror, 3609 /*cam_flags*/0, /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 3610 softc->disk->d_devstat); 3611 if (error != 0) 3612 xpt_print(periph->path, "Spin-down disk failed\n"); 3613 cam_periph_unlock(periph); 3614 } 3615 } 3616 3617 static void 3618 adashutdown(void *arg, int howto) 3619 { 3620 int how; 3621 3622 adaflush(); 3623 3624 /* 3625 * STANDBY IMMEDIATE saves any volatile data to the drive. It also spins 3626 * down hard drives. IDLE IMMEDIATE also saves the volatile data without 3627 * a spindown. We send the former when we expect to lose power soon. For 3628 * a warm boot, we send the latter to avoid a thundering herd of spinups 3629 * just after the kernel loads while probing. We have to do something to 3630 * flush the data because the BIOS in many systems resets the HBA 3631 * causing a COMINIT/COMRESET negotiation, which some drives interpret 3632 * as license to toss the volatile data, and others count as unclean 3633 * shutdown when in the Active PM state in SMART attributes. 3634 * 3635 * adaspindown will ensure that we don't send this to a drive that 3636 * doesn't support it. 3637 */ 3638 if (ada_spindown_shutdown != 0) { 3639 how = (howto & (RB_HALT | RB_POWEROFF | RB_POWERCYCLE)) ? 3640 ATA_STANDBY_IMMEDIATE : ATA_IDLE_IMMEDIATE; 3641 adaspindown(how, 0); 3642 } 3643 } 3644 3645 static void 3646 adasuspend(void *arg) 3647 { 3648 3649 adaflush(); 3650 /* 3651 * SLEEP also fushes any volatile data, like STANDBY IMEDIATE, 3652 * so we don't need to send it as well. 3653 */ 3654 if (ada_spindown_suspend != 0) 3655 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE); 3656 } 3657 3658 static void 3659 adaresume(void *arg) 3660 { 3661 struct cam_periph *periph; 3662 struct ada_softc *softc; 3663 3664 if (ada_spindown_suspend == 0) 3665 return; 3666 3667 CAM_PERIPH_FOREACH(periph, &adadriver) { 3668 cam_periph_lock(periph); 3669 softc = (struct ada_softc *)periph->softc; 3670 /* 3671 * We only spin-down the drive if it is capable of it.. 3672 */ 3673 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 3674 cam_periph_unlock(periph); 3675 continue; 3676 } 3677 3678 if (bootverbose) 3679 xpt_print(periph->path, "resume\n"); 3680 3681 /* 3682 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on 3683 * sleep request. 3684 */ 3685 cam_release_devq(periph->path, 3686 /*relsim_flags*/0, 3687 /*openings*/0, 3688 /*timeout*/0, 3689 /*getcount_only*/0); 3690 3691 cam_periph_unlock(periph); 3692 } 3693 } 3694 3695 #endif /* _KERNEL */ 3696