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