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