ata_da.c revision 273814
1114117Simp/*- 250476Speter * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org> 31592Srgrimes * All rights reserved. 4156813Sru * 5156813Sru * Redistribution and use in source and binary forms, with or without 653909Speter * modification, are permitted provided that the following conditions 753909Speter * are met: 853909Speter * 1. Redistributions of source code must retain the above copyright 953909Speter * notice, this list of conditions and the following disclaimer, 1053909Speter * without modification, immediately at the beginning of the file. 1153909Speter * 2. Redistributions in binary form must reproduce the above copyright 12146286Sobrien * notice, this list of conditions and the following disclaimer in the 13124587Sru * documentation and/or other materials provided with the distribution. 1453909Speter * 15137675Sbz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1653909Speter * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17108574Sjmallett * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1853909Speter * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1953909Speter * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20143026Strhodes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2153909Speter * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22104385Smike * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2353909Speter * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2453909Speter * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2553909Speter */ 26143026Strhodes 27124587Sru#include <sys/cdefs.h> 2870922Sdougb__FBSDID("$FreeBSD: stable/10/sys/cam/ata/ata_da.c 273814 2014-10-29 09:27:24Z smh $"); 29124587Sru 3053909Speter#include "opt_ada.h" 31101808Sdwmalone 32117675Smarkm#include <sys/param.h> 3353909Speter 34171173Smlaier#ifdef _KERNEL 35137675Sbz#include <sys/systm.h> 361592Srgrimes#include <sys/kernel.h> 37156813Sru#include <sys/bio.h> 38137675Sbz#include <sys/sysctl.h> 39137675Sbz#include <sys/taskqueue.h> 40137675Sbz#include <sys/lock.h> 41137675Sbz#include <sys/mutex.h> 42156813Sru#include <sys/conf.h> 43171173Smlaier#include <sys/devicestat.h> 44126756Smlaier#include <sys/eventhandler.h> 45126756Smlaier#include <sys/malloc.h> 46173220Syar#include <sys/cons.h> 47124587Sru#include <sys/proc.h> 4853909Speter#include <sys/reboot.h> 4953909Speter#include <geom/geom_disk.h> 50156813Sru#endif /* _KERNEL */ 51143107Sru 52143026Strhodes#ifndef _KERNEL 53143026Strhodes#include <stdio.h> 54143026Strhodes#include <string.h> 55156813Sru#endif /* _KERNEL */ 56124587Sru 57124587Sru#include <cam/cam.h> 5838101Speter#include <cam/cam_ccb.h> 5938101Speter#include <cam/cam_periph.h> 601592Srgrimes#include <cam/cam_xpt_periph.h> 61#include <cam/cam_sim.h> 62 63#include <cam/ata/ata_all.h> 64 65#include <machine/md_var.h> /* geometry translation */ 66 67#ifdef _KERNEL 68 69#define ATA_MAX_28BIT_LBA 268435455UL 70 71typedef enum { 72 ADA_STATE_RAHEAD, 73 ADA_STATE_WCACHE, 74 ADA_STATE_NORMAL 75} ada_state; 76 77typedef enum { 78 ADA_FLAG_CAN_48BIT = 0x0002, 79 ADA_FLAG_CAN_FLUSHCACHE = 0x0004, 80 ADA_FLAG_CAN_NCQ = 0x0008, 81 ADA_FLAG_CAN_DMA = 0x0010, 82 ADA_FLAG_NEED_OTAG = 0x0020, 83 ADA_FLAG_WAS_OTAG = 0x0040, 84 ADA_FLAG_CAN_TRIM = 0x0080, 85 ADA_FLAG_OPEN = 0x0100, 86 ADA_FLAG_SCTX_INIT = 0x0200, 87 ADA_FLAG_CAN_CFA = 0x0400, 88 ADA_FLAG_CAN_POWERMGT = 0x0800, 89 ADA_FLAG_CAN_DMA48 = 0x1000, 90 ADA_FLAG_DIRTY = 0x2000 91} ada_flags; 92 93typedef enum { 94 ADA_Q_NONE = 0x00, 95 ADA_Q_4K = 0x01, 96} ada_quirks; 97 98#define ADA_Q_BIT_STRING \ 99 "\020" \ 100 "\0014K" 101 102typedef enum { 103 ADA_CCB_RAHEAD = 0x01, 104 ADA_CCB_WCACHE = 0x02, 105 ADA_CCB_BUFFER_IO = 0x03, 106 ADA_CCB_DUMP = 0x05, 107 ADA_CCB_TRIM = 0x06, 108 ADA_CCB_TYPE_MASK = 0x0F, 109} ada_ccb_state; 110 111/* Offsets into our private area for storing information */ 112#define ccb_state ppriv_field0 113#define ccb_bp ppriv_ptr1 114 115struct disk_params { 116 u_int8_t heads; 117 u_int8_t secs_per_track; 118 u_int32_t cylinders; 119 u_int32_t secsize; /* Number of bytes/logical sector */ 120 u_int64_t sectors; /* Total number sectors */ 121}; 122 123#define TRIM_MAX_BLOCKS 8 124#define TRIM_MAX_RANGES (TRIM_MAX_BLOCKS * ATA_DSM_BLK_RANGES) 125struct trim_request { 126 uint8_t data[TRIM_MAX_RANGES * ATA_DSM_RANGE_SIZE]; 127 TAILQ_HEAD(, bio) bps; 128}; 129 130struct ada_softc { 131 struct bio_queue_head bio_queue; 132 struct bio_queue_head trim_queue; 133 int outstanding_cmds; /* Number of active commands */ 134 int refcount; /* Active xpt_action() calls */ 135 ada_state state; 136 ada_flags flags; 137 ada_quirks quirks; 138 int sort_io_queue; 139 int trim_max_ranges; 140 int trim_running; 141 int read_ahead; 142 int write_cache; 143#ifdef ADA_TEST_FAILURE 144 int force_read_error; 145 int force_write_error; 146 int periodic_read_error; 147 int periodic_read_count; 148#endif 149 struct disk_params params; 150 struct disk *disk; 151 struct task sysctl_task; 152 struct sysctl_ctx_list sysctl_ctx; 153 struct sysctl_oid *sysctl_tree; 154 struct callout sendordered_c; 155 struct trim_request trim_req; 156}; 157 158struct ada_quirk_entry { 159 struct scsi_inquiry_pattern inq_pat; 160 ada_quirks quirks; 161}; 162 163static struct ada_quirk_entry ada_quirk_table[] = 164{ 165 { 166 /* Hitachi Advanced Format (4k) drives */ 167 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" }, 168 /*quirks*/ADA_Q_4K 169 }, 170 { 171 /* Samsung Advanced Format (4k) drives */ 172 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD155UI*", "*" }, 173 /*quirks*/ADA_Q_4K 174 }, 175 { 176 /* Samsung Advanced Format (4k) drives */ 177 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" }, 178 /*quirks*/ADA_Q_4K 179 }, 180 { 181 /* Seagate Barracuda Green Advanced Format (4k) drives */ 182 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" }, 183 /*quirks*/ADA_Q_4K 184 }, 185 { 186 /* Seagate Barracuda Advanced Format (4k) drives */ 187 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???DM*", "*" }, 188 /*quirks*/ADA_Q_4K 189 }, 190 { 191 /* Seagate Barracuda Advanced Format (4k) drives */ 192 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DM*", "*" }, 193 /*quirks*/ADA_Q_4K 194 }, 195 { 196 /* Seagate Momentus Advanced Format (4k) drives */ 197 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" }, 198 /*quirks*/ADA_Q_4K 199 }, 200 { 201 /* Seagate Momentus Advanced Format (4k) drives */ 202 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" }, 203 /*quirks*/ADA_Q_4K 204 }, 205 { 206 /* Seagate Momentus Advanced Format (4k) drives */ 207 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640423AS*", "*" }, 208 /*quirks*/ADA_Q_4K 209 }, 210 { 211 /* Seagate Momentus Advanced Format (4k) drives */ 212 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640424AS*", "*" }, 213 /*quirks*/ADA_Q_4K 214 }, 215 { 216 /* Seagate Momentus Advanced Format (4k) drives */ 217 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" }, 218 /*quirks*/ADA_Q_4K 219 }, 220 { 221 /* Seagate Momentus Advanced Format (4k) drives */ 222 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" }, 223 /*quirks*/ADA_Q_4K 224 }, 225 { 226 /* Seagate Momentus Advanced Format (4k) drives */ 227 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750423AS*", "*" }, 228 /*quirks*/ADA_Q_4K 229 }, 230 { 231 /* Seagate Momentus Thin Advanced Format (4k) drives */ 232 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" }, 233 /*quirks*/ADA_Q_4K 234 }, 235 { 236 /* WDC Caviar Green Advanced Format (4k) drives */ 237 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" }, 238 /*quirks*/ADA_Q_4K 239 }, 240 { 241 /* WDC Caviar Green Advanced Format (4k) drives */ 242 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" }, 243 /*quirks*/ADA_Q_4K 244 }, 245 { 246 /* WDC Caviar Green Advanced Format (4k) drives */ 247 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" }, 248 /*quirks*/ADA_Q_4K 249 }, 250 { 251 /* WDC Caviar Green Advanced Format (4k) drives */ 252 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" }, 253 /*quirks*/ADA_Q_4K 254 }, 255 { 256 /* WDC Scorpio Black Advanced Format (4k) drives */ 257 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" }, 258 /*quirks*/ADA_Q_4K 259 }, 260 { 261 /* WDC Scorpio Black Advanced Format (4k) drives */ 262 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" }, 263 /*quirks*/ADA_Q_4K 264 }, 265 { 266 /* WDC Scorpio Blue Advanced Format (4k) drives */ 267 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" }, 268 /*quirks*/ADA_Q_4K 269 }, 270 { 271 /* WDC Scorpio Blue Advanced Format (4k) drives */ 272 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" }, 273 /*quirks*/ADA_Q_4K 274 }, 275 /* SSDs */ 276 { 277 /* 278 * Corsair Force 2 SSDs 279 * 4k optimised & trim only works in 4k requests + 4k aligned 280 */ 281 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair CSSD-F*", "*" }, 282 /*quirks*/ADA_Q_4K 283 }, 284 { 285 /* 286 * Corsair Force 3 SSDs 287 * 4k optimised & trim only works in 4k requests + 4k aligned 288 */ 289 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force 3*", "*" }, 290 /*quirks*/ADA_Q_4K 291 }, 292 { 293 /* 294 * Corsair Neutron GTX SSDs 295 * 4k optimised & trim only works in 4k requests + 4k aligned 296 */ 297 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" }, 298 /*quirks*/ADA_Q_4K 299 }, 300 { 301 /* 302 * Corsair Force GT & GS SSDs 303 * 4k optimised & trim only works in 4k requests + 4k aligned 304 */ 305 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force G*", "*" }, 306 /*quirks*/ADA_Q_4K 307 }, 308 { 309 /* 310 * Crucial M4 SSDs 311 * 4k optimised & trim only works in 4k requests + 4k aligned 312 */ 313 { T_DIRECT, SIP_MEDIA_FIXED, "*", "M4-CT???M4SSD2*", "*" }, 314 /*quirks*/ADA_Q_4K 315 }, 316 { 317 /* 318 * Crucial RealSSD C300 SSDs 319 * 4k optimised 320 */ 321 { T_DIRECT, SIP_MEDIA_FIXED, "*", "C300-CTFDDAC???MAG*", 322 "*" }, /*quirks*/ADA_Q_4K 323 }, 324 { 325 /* 326 * Intel 320 Series SSDs 327 * 4k optimised & trim only works in 4k requests + 4k aligned 328 */ 329 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2CW*", "*" }, 330 /*quirks*/ADA_Q_4K 331 }, 332 { 333 /* 334 * Intel 330 Series SSDs 335 * 4k optimised & trim only works in 4k requests + 4k aligned 336 */ 337 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2CT*", "*" }, 338 /*quirks*/ADA_Q_4K 339 }, 340 { 341 /* 342 * Intel 510 Series SSDs 343 * 4k optimised & trim only works in 4k requests + 4k aligned 344 */ 345 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2MH*", "*" }, 346 /*quirks*/ADA_Q_4K 347 }, 348 { 349 /* 350 * Intel 520 Series SSDs 351 * 4k optimised & trim only works in 4k requests + 4k aligned 352 */ 353 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2BW*", "*" }, 354 /*quirks*/ADA_Q_4K 355 }, 356 { 357 /* 358 * Intel X25-M Series SSDs 359 * 4k optimised & trim only works in 4k requests + 4k aligned 360 */ 361 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2M*", "*" }, 362 /*quirks*/ADA_Q_4K 363 }, 364 { 365 /* 366 * Kingston E100 Series SSDs 367 * 4k optimised & trim only works in 4k requests + 4k aligned 368 */ 369 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SE100S3*", "*" }, 370 /*quirks*/ADA_Q_4K 371 }, 372 { 373 /* 374 * Kingston HyperX 3k SSDs 375 * 4k optimised & trim only works in 4k requests + 4k aligned 376 */ 377 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SH103S3*", "*" }, 378 /*quirks*/ADA_Q_4K 379 }, 380 { 381 /* 382 * Marvell SSDs (entry taken from OpenSolaris) 383 * 4k optimised & trim only works in 4k requests + 4k aligned 384 */ 385 { T_DIRECT, SIP_MEDIA_FIXED, "*", "MARVELL SD88SA02*", "*" }, 386 /*quirks*/ADA_Q_4K 387 }, 388 { 389 /* 390 * OCZ Agility 2 SSDs 391 * 4k optimised & trim only works in 4k requests + 4k aligned 392 */ 393 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" }, 394 /*quirks*/ADA_Q_4K 395 }, 396 { 397 /* 398 * OCZ Agility 3 SSDs 399 * 4k optimised & trim only works in 4k requests + 4k aligned 400 */ 401 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY3*", "*" }, 402 /*quirks*/ADA_Q_4K 403 }, 404 { 405 /* 406 * OCZ Deneva R Series SSDs 407 * 4k optimised & trim only works in 4k requests + 4k aligned 408 */ 409 { T_DIRECT, SIP_MEDIA_FIXED, "*", "DENRSTE251M45*", "*" }, 410 /*quirks*/ADA_Q_4K 411 }, 412 { 413 /* 414 * OCZ Vertex 2 SSDs (inc pro series) 415 * 4k optimised & trim only works in 4k requests + 4k aligned 416 */ 417 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ?VERTEX2*", "*" }, 418 /*quirks*/ADA_Q_4K 419 }, 420 { 421 /* 422 * OCZ Vertex 3 SSDs 423 * 4k optimised & trim only works in 4k requests + 4k aligned 424 */ 425 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX3*", "*" }, 426 /*quirks*/ADA_Q_4K 427 }, 428 { 429 /* 430 * OCZ Vertex 4 SSDs 431 * 4k optimised & trim only works in 4k requests + 4k aligned 432 */ 433 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX4*", "*" }, 434 /*quirks*/ADA_Q_4K 435 }, 436 { 437 /* 438 * Samsung 830 Series SSDs 439 * 4k optimised 440 */ 441 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG SSD 830 Series*", "*" }, 442 /*quirks*/ADA_Q_4K 443 }, 444 { 445 /* 446 * Samsung 840 SSDs 447 * 4k optimised 448 */ 449 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 840*", "*" }, 450 /*quirks*/ADA_Q_4K 451 }, 452 { 453 /* 454 * SuperTalent TeraDrive CT SSDs 455 * 4k optimised & trim only works in 4k requests + 4k aligned 456 */ 457 { T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" }, 458 /*quirks*/ADA_Q_4K 459 }, 460 { 461 /* 462 * XceedIOPS SATA SSDs 463 * 4k optimised 464 */ 465 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" }, 466 /*quirks*/ADA_Q_4K 467 }, 468 { 469 /* Default */ 470 { 471 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, 472 /*vendor*/"*", /*product*/"*", /*revision*/"*" 473 }, 474 /*quirks*/0 475 }, 476}; 477 478static disk_strategy_t adastrategy; 479static dumper_t adadump; 480static periph_init_t adainit; 481static void adaasync(void *callback_arg, u_int32_t code, 482 struct cam_path *path, void *arg); 483static void adasysctlinit(void *context, int pending); 484static periph_ctor_t adaregister; 485static periph_dtor_t adacleanup; 486static periph_start_t adastart; 487static periph_oninv_t adaoninvalidate; 488static void adadone(struct cam_periph *periph, 489 union ccb *done_ccb); 490static int adaerror(union ccb *ccb, u_int32_t cam_flags, 491 u_int32_t sense_flags); 492static void adagetparams(struct cam_periph *periph, 493 struct ccb_getdev *cgd); 494static timeout_t adasendorderedtag; 495static void adashutdown(void *arg, int howto); 496static void adasuspend(void *arg); 497static void adaresume(void *arg); 498 499#ifndef ADA_DEFAULT_LEGACY_ALIASES 500#define ADA_DEFAULT_LEGACY_ALIASES 1 501#endif 502 503#ifndef ADA_DEFAULT_TIMEOUT 504#define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */ 505#endif 506 507#ifndef ADA_DEFAULT_RETRY 508#define ADA_DEFAULT_RETRY 4 509#endif 510 511#ifndef ADA_DEFAULT_SEND_ORDERED 512#define ADA_DEFAULT_SEND_ORDERED 1 513#endif 514 515#ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN 516#define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1 517#endif 518 519#ifndef ADA_DEFAULT_SPINDOWN_SUSPEND 520#define ADA_DEFAULT_SPINDOWN_SUSPEND 1 521#endif 522 523#ifndef ADA_DEFAULT_READ_AHEAD 524#define ADA_DEFAULT_READ_AHEAD 1 525#endif 526 527#ifndef ADA_DEFAULT_WRITE_CACHE 528#define ADA_DEFAULT_WRITE_CACHE 1 529#endif 530 531#define ADA_RA (softc->read_ahead >= 0 ? \ 532 softc->read_ahead : ada_read_ahead) 533#define ADA_WC (softc->write_cache >= 0 ? \ 534 softc->write_cache : ada_write_cache) 535#define ADA_SIO (softc->sort_io_queue >= 0 ? \ 536 softc->sort_io_queue : cam_sort_io_queues) 537 538/* 539 * Most platforms map firmware geometry to actual, but some don't. If 540 * not overridden, default to nothing. 541 */ 542#ifndef ata_disk_firmware_geom_adjust 543#define ata_disk_firmware_geom_adjust(disk) 544#endif 545 546static int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES; 547static int ada_retry_count = ADA_DEFAULT_RETRY; 548static int ada_default_timeout = ADA_DEFAULT_TIMEOUT; 549static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED; 550static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN; 551static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND; 552static int ada_read_ahead = ADA_DEFAULT_READ_AHEAD; 553static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE; 554 555static SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0, 556 "CAM Direct Access Disk driver"); 557SYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RW, 558 &ada_legacy_aliases, 0, "Create legacy-like device aliases"); 559TUNABLE_INT("kern.cam.ada.legacy_aliases", &ada_legacy_aliases); 560SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW, 561 &ada_retry_count, 0, "Normal I/O retry count"); 562TUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count); 563SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW, 564 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)"); 565TUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout); 566SYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RW, 567 &ada_send_ordered, 0, "Send Ordered Tags"); 568TUNABLE_INT("kern.cam.ada.send_ordered", &ada_send_ordered); 569SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RW, 570 &ada_spindown_shutdown, 0, "Spin down upon shutdown"); 571TUNABLE_INT("kern.cam.ada.spindown_shutdown", &ada_spindown_shutdown); 572SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RW, 573 &ada_spindown_suspend, 0, "Spin down upon suspend"); 574TUNABLE_INT("kern.cam.ada.spindown_suspend", &ada_spindown_suspend); 575SYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RW, 576 &ada_read_ahead, 0, "Enable disk read-ahead"); 577TUNABLE_INT("kern.cam.ada.read_ahead", &ada_read_ahead); 578SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RW, 579 &ada_write_cache, 0, "Enable disk write cache"); 580TUNABLE_INT("kern.cam.ada.write_cache", &ada_write_cache); 581 582/* 583 * ADA_ORDEREDTAG_INTERVAL determines how often, relative 584 * to the default timeout, we check to see whether an ordered 585 * tagged transaction is appropriate to prevent simple tag 586 * starvation. Since we'd like to ensure that there is at least 587 * 1/2 of the timeout length left for a starved transaction to 588 * complete after we've sent an ordered tag, we must poll at least 589 * four times in every timeout period. This takes care of the worst 590 * case where a starved transaction starts during an interval that 591 * meets the requirement "don't send an ordered tag" test so it takes 592 * us two intervals to determine that a tag must be sent. 593 */ 594#ifndef ADA_ORDEREDTAG_INTERVAL 595#define ADA_ORDEREDTAG_INTERVAL 4 596#endif 597 598static struct periph_driver adadriver = 599{ 600 adainit, "ada", 601 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0 602}; 603 604PERIPHDRIVER_DECLARE(ada, adadriver); 605 606static MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers"); 607 608static int 609adaopen(struct disk *dp) 610{ 611 struct cam_periph *periph; 612 struct ada_softc *softc; 613 int error; 614 615 periph = (struct cam_periph *)dp->d_drv1; 616 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 617 return(ENXIO); 618 } 619 620 cam_periph_lock(periph); 621 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) { 622 cam_periph_unlock(periph); 623 cam_periph_release(periph); 624 return (error); 625 } 626 627 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 628 ("adaopen\n")); 629 630 softc = (struct ada_softc *)periph->softc; 631 softc->flags |= ADA_FLAG_OPEN; 632 633 cam_periph_unhold(periph); 634 cam_periph_unlock(periph); 635 return (0); 636} 637 638static int 639adaclose(struct disk *dp) 640{ 641 struct cam_periph *periph; 642 struct ada_softc *softc; 643 union ccb *ccb; 644 int error; 645 646 periph = (struct cam_periph *)dp->d_drv1; 647 softc = (struct ada_softc *)periph->softc; 648 cam_periph_lock(periph); 649 650 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 651 ("adaclose\n")); 652 653 /* We only sync the cache if the drive is capable of it. */ 654 if ((softc->flags & ADA_FLAG_DIRTY) != 0 && 655 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 && 656 (periph->flags & CAM_PERIPH_INVALID) == 0 && 657 cam_periph_hold(periph, PRIBIO) == 0) { 658 659 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 660 cam_fill_ataio(&ccb->ataio, 661 1, 662 adadone, 663 CAM_DIR_NONE, 664 0, 665 NULL, 666 0, 667 ada_default_timeout*1000); 668 669 if (softc->flags & ADA_FLAG_CAN_48BIT) 670 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 671 else 672 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 673 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 674 /*sense_flags*/0, softc->disk->d_devstat); 675 676 if (error != 0) 677 xpt_print(periph->path, "Synchronize cache failed\n"); 678 else 679 softc->flags &= ~ADA_FLAG_DIRTY; 680 xpt_release_ccb(ccb); 681 cam_periph_unhold(periph); 682 } 683 684 softc->flags &= ~ADA_FLAG_OPEN; 685 686 while (softc->refcount != 0) 687 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "adaclose", 1); 688 cam_periph_unlock(periph); 689 cam_periph_release(periph); 690 return (0); 691} 692 693static void 694adaschedule(struct cam_periph *periph) 695{ 696 struct ada_softc *softc = (struct ada_softc *)periph->softc; 697 698 if (softc->state != ADA_STATE_NORMAL) 699 return; 700 701 /* Check if we have more work to do. */ 702 if (bioq_first(&softc->bio_queue) || 703 (!softc->trim_running && bioq_first(&softc->trim_queue))) { 704 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 705 } 706} 707 708/* 709 * Actually translate the requested transfer into one the physical driver 710 * can understand. The transfer is described by a buf and will include 711 * only one physical transfer. 712 */ 713static void 714adastrategy(struct bio *bp) 715{ 716 struct cam_periph *periph; 717 struct ada_softc *softc; 718 719 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 720 softc = (struct ada_softc *)periph->softc; 721 722 cam_periph_lock(periph); 723 724 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp)); 725 726 /* 727 * If the device has been made invalid, error out 728 */ 729 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 730 cam_periph_unlock(periph); 731 biofinish(bp, NULL, ENXIO); 732 return; 733 } 734 735 /* 736 * Place it in the queue of disk activities for this disk 737 */ 738 if (bp->bio_cmd == BIO_DELETE) { 739 KASSERT((softc->flags & ADA_FLAG_CAN_TRIM) || 740 ((softc->flags & ADA_FLAG_CAN_CFA) && 741 !(softc->flags & ADA_FLAG_CAN_48BIT)), 742 ("BIO_DELETE but no supported TRIM method.")); 743 bioq_disksort(&softc->trim_queue, bp); 744 } else { 745 if (ADA_SIO) 746 bioq_disksort(&softc->bio_queue, bp); 747 else 748 bioq_insert_tail(&softc->bio_queue, bp); 749 } 750 751 /* 752 * Schedule ourselves for performing the work. 753 */ 754 adaschedule(periph); 755 cam_periph_unlock(periph); 756 757 return; 758} 759 760static int 761adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) 762{ 763 struct cam_periph *periph; 764 struct ada_softc *softc; 765 u_int secsize; 766 union ccb ccb; 767 struct disk *dp; 768 uint64_t lba; 769 uint16_t count; 770 int error = 0; 771 772 dp = arg; 773 periph = dp->d_drv1; 774 softc = (struct ada_softc *)periph->softc; 775 cam_periph_lock(periph); 776 secsize = softc->params.secsize; 777 lba = offset / secsize; 778 count = length / secsize; 779 780 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 781 cam_periph_unlock(periph); 782 return (ENXIO); 783 } 784 785 if (length > 0) { 786 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 787 ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 788 cam_fill_ataio(&ccb.ataio, 789 0, 790 adadone, 791 CAM_DIR_OUT, 792 0, 793 (u_int8_t *) virtual, 794 length, 795 ada_default_timeout*1000); 796 if ((softc->flags & ADA_FLAG_CAN_48BIT) && 797 (lba + count >= ATA_MAX_28BIT_LBA || 798 count >= 256)) { 799 ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48, 800 0, lba, count); 801 } else { 802 ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA, 803 0, lba, count); 804 } 805 xpt_polled_action(&ccb); 806 807 error = cam_periph_error(&ccb, 808 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 809 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 810 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0, 811 /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 812 if (error != 0) 813 printf("Aborting dump due to I/O error.\n"); 814 815 cam_periph_unlock(periph); 816 return (error); 817 } 818 819 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) { 820 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 821 822 ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 823 cam_fill_ataio(&ccb.ataio, 824 0, 825 adadone, 826 CAM_DIR_NONE, 827 0, 828 NULL, 829 0, 830 ada_default_timeout*1000); 831 832 if (softc->flags & ADA_FLAG_CAN_48BIT) 833 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0); 834 else 835 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0); 836 xpt_polled_action(&ccb); 837 838 error = cam_periph_error(&ccb, 839 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 840 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 841 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0, 842 /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 843 if (error != 0) 844 xpt_print(periph->path, "Synchronize cache failed\n"); 845 } 846 cam_periph_unlock(periph); 847 return (error); 848} 849 850static void 851adainit(void) 852{ 853 cam_status status; 854 855 /* 856 * Install a global async callback. This callback will 857 * receive async callbacks like "new device found". 858 */ 859 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL); 860 861 if (status != CAM_REQ_CMP) { 862 printf("ada: Failed to attach master async callback " 863 "due to status 0x%x!\n", status); 864 } else if (ada_send_ordered) { 865 866 /* Register our event handlers */ 867 if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend, 868 NULL, EVENTHANDLER_PRI_LAST)) == NULL) 869 printf("adainit: power event registration failed!\n"); 870 if ((EVENTHANDLER_REGISTER(power_resume, adaresume, 871 NULL, EVENTHANDLER_PRI_LAST)) == NULL) 872 printf("adainit: power event registration failed!\n"); 873 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown, 874 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 875 printf("adainit: shutdown event registration failed!\n"); 876 } 877} 878 879/* 880 * Callback from GEOM, called when it has finished cleaning up its 881 * resources. 882 */ 883static void 884adadiskgonecb(struct disk *dp) 885{ 886 struct cam_periph *periph; 887 888 periph = (struct cam_periph *)dp->d_drv1; 889 890 cam_periph_release(periph); 891} 892 893static void 894adaoninvalidate(struct cam_periph *periph) 895{ 896 struct ada_softc *softc; 897 898 softc = (struct ada_softc *)periph->softc; 899 900 /* 901 * De-register any async callbacks. 902 */ 903 xpt_register_async(0, adaasync, periph, periph->path); 904 905 /* 906 * Return all queued I/O with ENXIO. 907 * XXX Handle any transactions queued to the card 908 * with XPT_ABORT_CCB. 909 */ 910 bioq_flush(&softc->bio_queue, NULL, ENXIO); 911 bioq_flush(&softc->trim_queue, NULL, ENXIO); 912 913 disk_gone(softc->disk); 914} 915 916static void 917adacleanup(struct cam_periph *periph) 918{ 919 struct ada_softc *softc; 920 921 softc = (struct ada_softc *)periph->softc; 922 923 cam_periph_unlock(periph); 924 925 /* 926 * If we can't free the sysctl tree, oh well... 927 */ 928 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0 929 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) { 930 xpt_print(periph->path, "can't remove sysctl context\n"); 931 } 932 933 disk_destroy(softc->disk); 934 callout_drain(&softc->sendordered_c); 935 free(softc, M_DEVBUF); 936 cam_periph_lock(periph); 937} 938 939static void 940adaasync(void *callback_arg, u_int32_t code, 941 struct cam_path *path, void *arg) 942{ 943 struct ccb_getdev cgd; 944 struct cam_periph *periph; 945 struct ada_softc *softc; 946 947 periph = (struct cam_periph *)callback_arg; 948 switch (code) { 949 case AC_FOUND_DEVICE: 950 { 951 struct ccb_getdev *cgd; 952 cam_status status; 953 954 cgd = (struct ccb_getdev *)arg; 955 if (cgd == NULL) 956 break; 957 958 if (cgd->protocol != PROTO_ATA) 959 break; 960 961 /* 962 * Allocate a peripheral instance for 963 * this device and start the probe 964 * process. 965 */ 966 status = cam_periph_alloc(adaregister, adaoninvalidate, 967 adacleanup, adastart, 968 "ada", CAM_PERIPH_BIO, 969 path, adaasync, 970 AC_FOUND_DEVICE, cgd); 971 972 if (status != CAM_REQ_CMP 973 && status != CAM_REQ_INPROG) 974 printf("adaasync: Unable to attach to new device " 975 "due to status 0x%x\n", status); 976 break; 977 } 978 case AC_GETDEV_CHANGED: 979 { 980 softc = (struct ada_softc *)periph->softc; 981 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 982 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 983 xpt_action((union ccb *)&cgd); 984 985 if ((cgd.ident_data.capabilities1 & ATA_SUPPORT_DMA) && 986 (cgd.inq_flags & SID_DMA)) 987 softc->flags |= ADA_FLAG_CAN_DMA; 988 else 989 softc->flags &= ~ADA_FLAG_CAN_DMA; 990 if (cgd.ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) { 991 softc->flags |= ADA_FLAG_CAN_48BIT; 992 if (cgd.inq_flags & SID_DMA48) 993 softc->flags |= ADA_FLAG_CAN_DMA48; 994 else 995 softc->flags &= ~ADA_FLAG_CAN_DMA48; 996 } else 997 softc->flags &= ~(ADA_FLAG_CAN_48BIT | 998 ADA_FLAG_CAN_DMA48); 999 if ((cgd.ident_data.satacapabilities & ATA_SUPPORT_NCQ) && 1000 (cgd.inq_flags & SID_DMA) && (cgd.inq_flags & SID_CmdQue)) 1001 softc->flags |= ADA_FLAG_CAN_NCQ; 1002 else 1003 softc->flags &= ~ADA_FLAG_CAN_NCQ; 1004 if ((cgd.ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) && 1005 (cgd.inq_flags & SID_DMA)) 1006 softc->flags |= ADA_FLAG_CAN_TRIM; 1007 else 1008 softc->flags &= ~ADA_FLAG_CAN_TRIM; 1009 1010 cam_periph_async(periph, code, path, arg); 1011 break; 1012 } 1013 case AC_ADVINFO_CHANGED: 1014 { 1015 uintptr_t buftype; 1016 1017 buftype = (uintptr_t)arg; 1018 if (buftype == CDAI_TYPE_PHYS_PATH) { 1019 struct ada_softc *softc; 1020 1021 softc = periph->softc; 1022 disk_attr_changed(softc->disk, "GEOM::physpath", 1023 M_NOWAIT); 1024 } 1025 break; 1026 } 1027 case AC_SENT_BDR: 1028 case AC_BUS_RESET: 1029 { 1030 softc = (struct ada_softc *)periph->softc; 1031 cam_periph_async(periph, code, path, arg); 1032 if (softc->state != ADA_STATE_NORMAL) 1033 break; 1034 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1035 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1036 xpt_action((union ccb *)&cgd); 1037 if (ADA_RA >= 0 && 1038 cgd.ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) 1039 softc->state = ADA_STATE_RAHEAD; 1040 else if (ADA_WC >= 0 && 1041 cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) 1042 softc->state = ADA_STATE_WCACHE; 1043 else 1044 break; 1045 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 1046 softc->state = ADA_STATE_NORMAL; 1047 else 1048 xpt_schedule(periph, CAM_PRIORITY_DEV); 1049 } 1050 default: 1051 cam_periph_async(periph, code, path, arg); 1052 break; 1053 } 1054} 1055 1056static void 1057adasysctlinit(void *context, int pending) 1058{ 1059 struct cam_periph *periph; 1060 struct ada_softc *softc; 1061 char tmpstr[80], tmpstr2[80]; 1062 1063 periph = (struct cam_periph *)context; 1064 1065 /* periph was held for us when this task was enqueued */ 1066 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 1067 cam_periph_release(periph); 1068 return; 1069 } 1070 1071 softc = (struct ada_softc *)periph->softc; 1072 snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number); 1073 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 1074 1075 sysctl_ctx_init(&softc->sysctl_ctx); 1076 softc->flags |= ADA_FLAG_SCTX_INIT; 1077 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1078 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2, 1079 CTLFLAG_RD, 0, tmpstr); 1080 if (softc->sysctl_tree == NULL) { 1081 printf("adasysctlinit: unable to allocate sysctl tree\n"); 1082 cam_periph_release(periph); 1083 return; 1084 } 1085 1086 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1087 OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE, 1088 &softc->read_ahead, 0, "Enable disk read ahead."); 1089 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1090 OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE, 1091 &softc->write_cache, 0, "Enable disk write cache."); 1092 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1093 OID_AUTO, "sort_io_queue", CTLFLAG_RW | CTLFLAG_MPSAFE, 1094 &softc->sort_io_queue, 0, 1095 "Sort IO queue to try and optimise disk access patterns"); 1096#ifdef ADA_TEST_FAILURE 1097 /* 1098 * Add a 'door bell' sysctl which allows one to set it from userland 1099 * and cause something bad to happen. For the moment, we only allow 1100 * whacking the next read or write. 1101 */ 1102 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1103 OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1104 &softc->force_read_error, 0, 1105 "Force a read error for the next N reads."); 1106 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1107 OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1108 &softc->force_write_error, 0, 1109 "Force a write error for the next N writes."); 1110 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1111 OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1112 &softc->periodic_read_error, 0, 1113 "Force a read error every N reads (don't set too low)."); 1114#endif 1115 cam_periph_release(periph); 1116} 1117 1118static int 1119adagetattr(struct bio *bp) 1120{ 1121 int ret; 1122 struct cam_periph *periph; 1123 1124 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 1125 cam_periph_lock(periph); 1126 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute, 1127 periph->path); 1128 cam_periph_unlock(periph); 1129 if (ret == 0) 1130 bp->bio_completed = bp->bio_length; 1131 return ret; 1132} 1133 1134static cam_status 1135adaregister(struct cam_periph *periph, void *arg) 1136{ 1137 struct ada_softc *softc; 1138 struct ccb_pathinq cpi; 1139 struct ccb_getdev *cgd; 1140 char announce_buf[80], buf1[32]; 1141 struct disk_params *dp; 1142 caddr_t match; 1143 u_int maxio; 1144 int legacy_id, quirks; 1145 1146 cgd = (struct ccb_getdev *)arg; 1147 if (cgd == NULL) { 1148 printf("adaregister: no getdev CCB, can't register device\n"); 1149 return(CAM_REQ_CMP_ERR); 1150 } 1151 1152 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF, 1153 M_NOWAIT|M_ZERO); 1154 1155 if (softc == NULL) { 1156 printf("adaregister: Unable to probe new device. " 1157 "Unable to allocate softc\n"); 1158 return(CAM_REQ_CMP_ERR); 1159 } 1160 1161 bioq_init(&softc->bio_queue); 1162 bioq_init(&softc->trim_queue); 1163 1164 if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) && 1165 (cgd->inq_flags & SID_DMA)) 1166 softc->flags |= ADA_FLAG_CAN_DMA; 1167 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) { 1168 softc->flags |= ADA_FLAG_CAN_48BIT; 1169 if (cgd->inq_flags & SID_DMA48) 1170 softc->flags |= ADA_FLAG_CAN_DMA48; 1171 } 1172 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE) 1173 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE; 1174 if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT) 1175 softc->flags |= ADA_FLAG_CAN_POWERMGT; 1176 if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) && 1177 (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue)) 1178 softc->flags |= ADA_FLAG_CAN_NCQ; 1179 if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) && 1180 (cgd->inq_flags & SID_DMA)) { 1181 softc->flags |= ADA_FLAG_CAN_TRIM; 1182 softc->trim_max_ranges = TRIM_MAX_RANGES; 1183 if (cgd->ident_data.max_dsm_blocks != 0) { 1184 softc->trim_max_ranges = 1185 min(cgd->ident_data.max_dsm_blocks * 1186 ATA_DSM_BLK_RANGES, softc->trim_max_ranges); 1187 } 1188 } 1189 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA) 1190 softc->flags |= ADA_FLAG_CAN_CFA; 1191 1192 periph->softc = softc; 1193 1194 /* 1195 * See if this device has any quirks. 1196 */ 1197 match = cam_quirkmatch((caddr_t)&cgd->ident_data, 1198 (caddr_t)ada_quirk_table, 1199 sizeof(ada_quirk_table)/sizeof(*ada_quirk_table), 1200 sizeof(*ada_quirk_table), ata_identify_match); 1201 if (match != NULL) 1202 softc->quirks = ((struct ada_quirk_entry *)match)->quirks; 1203 else 1204 softc->quirks = ADA_Q_NONE; 1205 1206 bzero(&cpi, sizeof(cpi)); 1207 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE); 1208 cpi.ccb_h.func_code = XPT_PATH_INQ; 1209 xpt_action((union ccb *)&cpi); 1210 1211 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph); 1212 1213 /* 1214 * Register this media as a disk 1215 */ 1216 (void)cam_periph_hold(periph, PRIBIO); 1217 cam_periph_unlock(periph); 1218 snprintf(announce_buf, sizeof(announce_buf), 1219 "kern.cam.ada.%d.quirks", periph->unit_number); 1220 quirks = softc->quirks; 1221 TUNABLE_INT_FETCH(announce_buf, &quirks); 1222 softc->quirks = quirks; 1223 softc->read_ahead = -1; 1224 snprintf(announce_buf, sizeof(announce_buf), 1225 "kern.cam.ada.%d.read_ahead", periph->unit_number); 1226 TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead); 1227 softc->write_cache = -1; 1228 snprintf(announce_buf, sizeof(announce_buf), 1229 "kern.cam.ada.%d.write_cache", periph->unit_number); 1230 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache); 1231 /* Disable queue sorting for non-rotational media by default. */ 1232 if (cgd->ident_data.media_rotation_rate == ATA_RATE_NON_ROTATING) 1233 softc->sort_io_queue = 0; 1234 else 1235 softc->sort_io_queue = -1; 1236 adagetparams(periph, cgd); 1237 softc->disk = disk_alloc(); 1238 softc->disk->d_rotation_rate = cgd->ident_data.media_rotation_rate; 1239 softc->disk->d_devstat = devstat_new_entry(periph->periph_name, 1240 periph->unit_number, softc->params.secsize, 1241 DEVSTAT_ALL_SUPPORTED, 1242 DEVSTAT_TYPE_DIRECT | 1243 XPORT_DEVSTAT_TYPE(cpi.transport), 1244 DEVSTAT_PRIORITY_DISK); 1245 softc->disk->d_open = adaopen; 1246 softc->disk->d_close = adaclose; 1247 softc->disk->d_strategy = adastrategy; 1248 softc->disk->d_getattr = adagetattr; 1249 softc->disk->d_dump = adadump; 1250 softc->disk->d_gone = adadiskgonecb; 1251 softc->disk->d_name = "ada"; 1252 softc->disk->d_drv1 = periph; 1253 maxio = cpi.maxio; /* Honor max I/O size of SIM */ 1254 if (maxio == 0) 1255 maxio = DFLTPHYS; /* traditional default */ 1256 else if (maxio > MAXPHYS) 1257 maxio = MAXPHYS; /* for safety */ 1258 if (softc->flags & ADA_FLAG_CAN_48BIT) 1259 maxio = min(maxio, 65536 * softc->params.secsize); 1260 else /* 28bit ATA command limit */ 1261 maxio = min(maxio, 256 * softc->params.secsize); 1262 softc->disk->d_maxsize = maxio; 1263 softc->disk->d_unit = periph->unit_number; 1264 softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION; 1265 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) 1266 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE; 1267 if (softc->flags & ADA_FLAG_CAN_TRIM) { 1268 softc->disk->d_flags |= DISKFLAG_CANDELETE; 1269 softc->disk->d_delmaxsize = softc->params.secsize * 1270 ATA_DSM_RANGE_MAX * 1271 softc->trim_max_ranges; 1272 } else if ((softc->flags & ADA_FLAG_CAN_CFA) && 1273 !(softc->flags & ADA_FLAG_CAN_48BIT)) { 1274 softc->disk->d_flags |= DISKFLAG_CANDELETE; 1275 softc->disk->d_delmaxsize = 256 * softc->params.secsize; 1276 } else 1277 softc->disk->d_delmaxsize = maxio; 1278 if ((cpi.hba_misc & PIM_UNMAPPED) != 0) 1279 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO; 1280 strlcpy(softc->disk->d_descr, cgd->ident_data.model, 1281 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model))); 1282 strlcpy(softc->disk->d_ident, cgd->ident_data.serial, 1283 MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial))); 1284 softc->disk->d_hba_vendor = cpi.hba_vendor; 1285 softc->disk->d_hba_device = cpi.hba_device; 1286 softc->disk->d_hba_subvendor = cpi.hba_subvendor; 1287 softc->disk->d_hba_subdevice = cpi.hba_subdevice; 1288 1289 softc->disk->d_sectorsize = softc->params.secsize; 1290 softc->disk->d_mediasize = (off_t)softc->params.sectors * 1291 softc->params.secsize; 1292 if (ata_physical_sector_size(&cgd->ident_data) != 1293 softc->params.secsize) { 1294 softc->disk->d_stripesize = 1295 ata_physical_sector_size(&cgd->ident_data); 1296 softc->disk->d_stripeoffset = (softc->disk->d_stripesize - 1297 ata_logical_sector_offset(&cgd->ident_data)) % 1298 softc->disk->d_stripesize; 1299 } else if (softc->quirks & ADA_Q_4K) { 1300 softc->disk->d_stripesize = 4096; 1301 softc->disk->d_stripeoffset = 0; 1302 } 1303 softc->disk->d_fwsectors = softc->params.secs_per_track; 1304 softc->disk->d_fwheads = softc->params.heads; 1305 ata_disk_firmware_geom_adjust(softc->disk); 1306 1307 if (ada_legacy_aliases) { 1308#ifdef ATA_STATIC_ID 1309 legacy_id = xpt_path_legacy_ata_id(periph->path); 1310#else 1311 legacy_id = softc->disk->d_unit; 1312#endif 1313 if (legacy_id >= 0) { 1314 snprintf(announce_buf, sizeof(announce_buf), 1315 "kern.devalias.%s%d", 1316 softc->disk->d_name, softc->disk->d_unit); 1317 snprintf(buf1, sizeof(buf1), 1318 "ad%d", legacy_id); 1319 setenv(announce_buf, buf1); 1320 } 1321 } else 1322 legacy_id = -1; 1323 /* 1324 * Acquire a reference to the periph before we register with GEOM. 1325 * We'll release this reference once GEOM calls us back (via 1326 * adadiskgonecb()) telling us that our provider has been freed. 1327 */ 1328 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 1329 xpt_print(periph->path, "%s: lost periph during " 1330 "registration!\n", __func__); 1331 cam_periph_lock(periph); 1332 return (CAM_REQ_CMP_ERR); 1333 } 1334 disk_create(softc->disk, DISK_VERSION); 1335 cam_periph_lock(periph); 1336 cam_periph_unhold(periph); 1337 1338 dp = &softc->params; 1339 snprintf(announce_buf, sizeof(announce_buf), 1340 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)", 1341 (uintmax_t)(((uintmax_t)dp->secsize * 1342 dp->sectors) / (1024*1024)), 1343 (uintmax_t)dp->sectors, 1344 dp->secsize, dp->heads, 1345 dp->secs_per_track, dp->cylinders); 1346 xpt_announce_periph(periph, announce_buf); 1347 xpt_announce_quirks(periph, softc->quirks, ADA_Q_BIT_STRING); 1348 if (legacy_id >= 0) 1349 printf("%s%d: Previously was known as ad%d\n", 1350 periph->periph_name, periph->unit_number, legacy_id); 1351 1352 /* 1353 * Create our sysctl variables, now that we know 1354 * we have successfully attached. 1355 */ 1356 if (cam_periph_acquire(periph) == CAM_REQ_CMP) 1357 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task); 1358 1359 /* 1360 * Add async callbacks for bus reset and 1361 * bus device reset calls. I don't bother 1362 * checking if this fails as, in most cases, 1363 * the system will function just fine without 1364 * them and the only alternative would be to 1365 * not attach the device on failure. 1366 */ 1367 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE | 1368 AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED, 1369 adaasync, periph, periph->path); 1370 1371 /* 1372 * Schedule a periodic event to occasionally send an 1373 * ordered tag to a device. 1374 */ 1375 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0); 1376 callout_reset(&softc->sendordered_c, 1377 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 1378 adasendorderedtag, softc); 1379 1380 if (ADA_RA >= 0 && 1381 cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) { 1382 softc->state = ADA_STATE_RAHEAD; 1383 } else if (ADA_WC >= 0 && 1384 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) { 1385 softc->state = ADA_STATE_WCACHE; 1386 } else { 1387 softc->state = ADA_STATE_NORMAL; 1388 return(CAM_REQ_CMP); 1389 } 1390 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 1391 softc->state = ADA_STATE_NORMAL; 1392 else 1393 xpt_schedule(periph, CAM_PRIORITY_DEV); 1394 return(CAM_REQ_CMP); 1395} 1396 1397static void 1398ada_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 1399{ 1400 struct trim_request *req = &softc->trim_req; 1401 uint64_t lastlba = (uint64_t)-1; 1402 int c, lastcount = 0, off, ranges = 0; 1403 1404 bzero(req, sizeof(*req)); 1405 TAILQ_INIT(&req->bps); 1406 do { 1407 uint64_t lba = bp->bio_pblkno; 1408 int count = bp->bio_bcount / softc->params.secsize; 1409 1410 bioq_remove(&softc->trim_queue, bp); 1411 1412 /* Try to extend the previous range. */ 1413 if (lba == lastlba) { 1414 c = min(count, ATA_DSM_RANGE_MAX - lastcount); 1415 lastcount += c; 1416 off = (ranges - 1) * ATA_DSM_RANGE_SIZE; 1417 req->data[off + 6] = lastcount & 0xff; 1418 req->data[off + 7] = 1419 (lastcount >> 8) & 0xff; 1420 count -= c; 1421 lba += c; 1422 } 1423 1424 while (count > 0) { 1425 c = min(count, ATA_DSM_RANGE_MAX); 1426 off = ranges * ATA_DSM_RANGE_SIZE; 1427 req->data[off + 0] = lba & 0xff; 1428 req->data[off + 1] = (lba >> 8) & 0xff; 1429 req->data[off + 2] = (lba >> 16) & 0xff; 1430 req->data[off + 3] = (lba >> 24) & 0xff; 1431 req->data[off + 4] = (lba >> 32) & 0xff; 1432 req->data[off + 5] = (lba >> 40) & 0xff; 1433 req->data[off + 6] = c & 0xff; 1434 req->data[off + 7] = (c >> 8) & 0xff; 1435 lba += c; 1436 count -= c; 1437 lastcount = c; 1438 ranges++; 1439 /* 1440 * Its the caller's responsibility to ensure the 1441 * request will fit so we don't need to check for 1442 * overrun here 1443 */ 1444 } 1445 lastlba = lba; 1446 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue); 1447 bp = bioq_first(&softc->trim_queue); 1448 if (bp == NULL || 1449 bp->bio_bcount / softc->params.secsize > 1450 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) 1451 break; 1452 } while (1); 1453 cam_fill_ataio(ataio, 1454 ada_retry_count, 1455 adadone, 1456 CAM_DIR_OUT, 1457 0, 1458 req->data, 1459 ((ranges + ATA_DSM_BLK_RANGES - 1) / 1460 ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE, 1461 ada_default_timeout * 1000); 1462 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT, 1463 ATA_DSM_TRIM, 0, (ranges + ATA_DSM_BLK_RANGES - 1464 1) / ATA_DSM_BLK_RANGES); 1465} 1466 1467static void 1468ada_cfaerase(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 1469{ 1470 struct trim_request *req = &softc->trim_req; 1471 uint64_t lba = bp->bio_pblkno; 1472 uint16_t count = bp->bio_bcount / softc->params.secsize; 1473 1474 bzero(req, sizeof(*req)); 1475 TAILQ_INIT(&req->bps); 1476 bioq_remove(&softc->trim_queue, bp); 1477 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue); 1478 1479 cam_fill_ataio(ataio, 1480 ada_retry_count, 1481 adadone, 1482 CAM_DIR_NONE, 1483 0, 1484 NULL, 1485 0, 1486 ada_default_timeout*1000); 1487 1488 if (count >= 256) 1489 count = 0; 1490 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count); 1491} 1492 1493static void 1494adastart(struct cam_periph *periph, union ccb *start_ccb) 1495{ 1496 struct ada_softc *softc = (struct ada_softc *)periph->softc; 1497 struct ccb_ataio *ataio = &start_ccb->ataio; 1498 1499 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n")); 1500 1501 switch (softc->state) { 1502 case ADA_STATE_NORMAL: 1503 { 1504 struct bio *bp; 1505 u_int8_t tag_code; 1506 1507 /* Run TRIM if not running yet. */ 1508 if (!softc->trim_running && 1509 (bp = bioq_first(&softc->trim_queue)) != 0) { 1510 if (softc->flags & ADA_FLAG_CAN_TRIM) { 1511 ada_dsmtrim(softc, bp, ataio); 1512 } else if ((softc->flags & ADA_FLAG_CAN_CFA) && 1513 !(softc->flags & ADA_FLAG_CAN_48BIT)) { 1514 ada_cfaerase(softc, bp, ataio); 1515 } else { 1516 panic("adastart: BIO_DELETE without method, not possible."); 1517 } 1518 softc->trim_running = 1; 1519 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM; 1520 start_ccb->ccb_h.flags |= CAM_UNLOCKED; 1521 goto out; 1522 } 1523 /* Run regular command. */ 1524 bp = bioq_first(&softc->bio_queue); 1525 if (bp == NULL) { 1526 xpt_release_ccb(start_ccb); 1527 break; 1528 } 1529 bioq_remove(&softc->bio_queue, bp); 1530 1531 if ((bp->bio_flags & BIO_ORDERED) != 0 1532 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) { 1533 softc->flags &= ~ADA_FLAG_NEED_OTAG; 1534 softc->flags |= ADA_FLAG_WAS_OTAG; 1535 tag_code = 0; 1536 } else { 1537 tag_code = 1; 1538 } 1539 switch (bp->bio_cmd) { 1540 case BIO_WRITE: 1541 softc->flags |= ADA_FLAG_DIRTY; 1542 /* FALLTHROUGH */ 1543 case BIO_READ: 1544 { 1545 uint64_t lba = bp->bio_pblkno; 1546 uint16_t count = bp->bio_bcount / softc->params.secsize; 1547#ifdef ADA_TEST_FAILURE 1548 int fail = 0; 1549 1550 /* 1551 * Support the failure ioctls. If the command is a 1552 * read, and there are pending forced read errors, or 1553 * if a write and pending write errors, then fail this 1554 * operation with EIO. This is useful for testing 1555 * purposes. Also, support having every Nth read fail. 1556 * 1557 * This is a rather blunt tool. 1558 */ 1559 if (bp->bio_cmd == BIO_READ) { 1560 if (softc->force_read_error) { 1561 softc->force_read_error--; 1562 fail = 1; 1563 } 1564 if (softc->periodic_read_error > 0) { 1565 if (++softc->periodic_read_count >= 1566 softc->periodic_read_error) { 1567 softc->periodic_read_count = 0; 1568 fail = 1; 1569 } 1570 } 1571 } else { 1572 if (softc->force_write_error) { 1573 softc->force_write_error--; 1574 fail = 1; 1575 } 1576 } 1577 if (fail) { 1578 bp->bio_error = EIO; 1579 bp->bio_flags |= BIO_ERROR; 1580 biodone(bp); 1581 xpt_release_ccb(start_ccb); 1582 adaschedule(periph); 1583 return; 1584 } 1585#endif 1586 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 || 1587 round_page(bp->bio_bcount + bp->bio_ma_offset) / 1588 PAGE_SIZE == bp->bio_ma_n, 1589 ("Short bio %p", bp)); 1590 cam_fill_ataio(ataio, 1591 ada_retry_count, 1592 adadone, 1593 (bp->bio_cmd == BIO_READ ? CAM_DIR_IN : 1594 CAM_DIR_OUT) | ((bp->bio_flags & BIO_UNMAPPED) 1595 != 0 ? CAM_DATA_BIO : 0), 1596 tag_code, 1597 ((bp->bio_flags & BIO_UNMAPPED) != 0) ? (void *)bp : 1598 bp->bio_data, 1599 bp->bio_bcount, 1600 ada_default_timeout*1000); 1601 1602 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) { 1603 if (bp->bio_cmd == BIO_READ) { 1604 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED, 1605 lba, count); 1606 } else { 1607 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED, 1608 lba, count); 1609 } 1610 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) && 1611 (lba + count >= ATA_MAX_28BIT_LBA || 1612 count > 256)) { 1613 if (softc->flags & ADA_FLAG_CAN_DMA48) { 1614 if (bp->bio_cmd == BIO_READ) { 1615 ata_48bit_cmd(ataio, ATA_READ_DMA48, 1616 0, lba, count); 1617 } else { 1618 ata_48bit_cmd(ataio, ATA_WRITE_DMA48, 1619 0, lba, count); 1620 } 1621 } else { 1622 if (bp->bio_cmd == BIO_READ) { 1623 ata_48bit_cmd(ataio, ATA_READ_MUL48, 1624 0, lba, count); 1625 } else { 1626 ata_48bit_cmd(ataio, ATA_WRITE_MUL48, 1627 0, lba, count); 1628 } 1629 } 1630 } else { 1631 if (count == 256) 1632 count = 0; 1633 if (softc->flags & ADA_FLAG_CAN_DMA) { 1634 if (bp->bio_cmd == BIO_READ) { 1635 ata_28bit_cmd(ataio, ATA_READ_DMA, 1636 0, lba, count); 1637 } else { 1638 ata_28bit_cmd(ataio, ATA_WRITE_DMA, 1639 0, lba, count); 1640 } 1641 } else { 1642 if (bp->bio_cmd == BIO_READ) { 1643 ata_28bit_cmd(ataio, ATA_READ_MUL, 1644 0, lba, count); 1645 } else { 1646 ata_28bit_cmd(ataio, ATA_WRITE_MUL, 1647 0, lba, count); 1648 } 1649 } 1650 } 1651 break; 1652 } 1653 case BIO_FLUSH: 1654 cam_fill_ataio(ataio, 1655 1, 1656 adadone, 1657 CAM_DIR_NONE, 1658 0, 1659 NULL, 1660 0, 1661 ada_default_timeout*1000); 1662 1663 if (softc->flags & ADA_FLAG_CAN_48BIT) 1664 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1665 else 1666 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0); 1667 break; 1668 } 1669 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO; 1670 start_ccb->ccb_h.flags |= CAM_UNLOCKED; 1671out: 1672 start_ccb->ccb_h.ccb_bp = bp; 1673 softc->outstanding_cmds++; 1674 softc->refcount++; 1675 cam_periph_unlock(periph); 1676 xpt_action(start_ccb); 1677 cam_periph_lock(periph); 1678 softc->refcount--; 1679 1680 /* May have more work to do, so ensure we stay scheduled */ 1681 adaschedule(periph); 1682 break; 1683 } 1684 case ADA_STATE_RAHEAD: 1685 case ADA_STATE_WCACHE: 1686 { 1687 cam_fill_ataio(ataio, 1688 1, 1689 adadone, 1690 CAM_DIR_NONE, 1691 0, 1692 NULL, 1693 0, 1694 ada_default_timeout*1000); 1695 1696 if (softc->state == ADA_STATE_RAHEAD) { 1697 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ? 1698 ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0); 1699 start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD; 1700 } else { 1701 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ? 1702 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0); 1703 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE; 1704 } 1705 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 1706 xpt_action(start_ccb); 1707 break; 1708 } 1709 } 1710} 1711 1712static void 1713adadone(struct cam_periph *periph, union ccb *done_ccb) 1714{ 1715 struct ada_softc *softc; 1716 struct ccb_ataio *ataio; 1717 struct ccb_getdev *cgd; 1718 struct cam_path *path; 1719 int state; 1720 1721 softc = (struct ada_softc *)periph->softc; 1722 ataio = &done_ccb->ataio; 1723 path = done_ccb->ccb_h.path; 1724 1725 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n")); 1726 1727 state = ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK; 1728 switch (state) { 1729 case ADA_CCB_BUFFER_IO: 1730 case ADA_CCB_TRIM: 1731 { 1732 struct bio *bp; 1733 int error; 1734 1735 cam_periph_lock(periph); 1736 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1737 error = adaerror(done_ccb, 0, 0); 1738 if (error == ERESTART) { 1739 /* A retry was scheduled, so just return. */ 1740 cam_periph_unlock(periph); 1741 return; 1742 } 1743 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1744 cam_release_devq(path, 1745 /*relsim_flags*/0, 1746 /*reduction*/0, 1747 /*timeout*/0, 1748 /*getcount_only*/0); 1749 } else { 1750 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1751 panic("REQ_CMP with QFRZN"); 1752 error = 0; 1753 } 1754 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 1755 bp->bio_error = error; 1756 if (error != 0) { 1757 bp->bio_resid = bp->bio_bcount; 1758 bp->bio_flags |= BIO_ERROR; 1759 } else { 1760 if (state == ADA_CCB_TRIM) 1761 bp->bio_resid = 0; 1762 else 1763 bp->bio_resid = ataio->resid; 1764 if (bp->bio_resid > 0) 1765 bp->bio_flags |= BIO_ERROR; 1766 } 1767 softc->outstanding_cmds--; 1768 if (softc->outstanding_cmds == 0) 1769 softc->flags |= ADA_FLAG_WAS_OTAG; 1770 xpt_release_ccb(done_ccb); 1771 if (state == ADA_CCB_TRIM) { 1772 TAILQ_HEAD(, bio) queue; 1773 struct bio *bp1; 1774 1775 TAILQ_INIT(&queue); 1776 TAILQ_CONCAT(&queue, &softc->trim_req.bps, bio_queue); 1777 softc->trim_running = 0; 1778 adaschedule(periph); 1779 cam_periph_unlock(periph); 1780 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) { 1781 TAILQ_REMOVE(&queue, bp1, bio_queue); 1782 bp1->bio_error = error; 1783 if (error != 0) { 1784 bp1->bio_flags |= BIO_ERROR; 1785 bp1->bio_resid = bp1->bio_bcount; 1786 } else 1787 bp1->bio_resid = 0; 1788 biodone(bp1); 1789 } 1790 } else { 1791 cam_periph_unlock(periph); 1792 biodone(bp); 1793 } 1794 return; 1795 } 1796 case ADA_CCB_RAHEAD: 1797 { 1798 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1799 if (adaerror(done_ccb, 0, 0) == ERESTART) { 1800out: 1801 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 1802 cam_release_devq(path, 0, 0, 0, FALSE); 1803 return; 1804 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1805 cam_release_devq(path, 1806 /*relsim_flags*/0, 1807 /*reduction*/0, 1808 /*timeout*/0, 1809 /*getcount_only*/0); 1810 } 1811 } 1812 1813 /* 1814 * Since our peripheral may be invalidated by an error 1815 * above or an external event, we must release our CCB 1816 * before releasing the reference on the peripheral. 1817 * The peripheral will only go away once the last reference 1818 * is removed, and we need it around for the CCB release 1819 * operation. 1820 */ 1821 cgd = (struct ccb_getdev *)done_ccb; 1822 xpt_setup_ccb(&cgd->ccb_h, path, CAM_PRIORITY_NORMAL); 1823 cgd->ccb_h.func_code = XPT_GDEV_TYPE; 1824 xpt_action((union ccb *)cgd); 1825 if (ADA_WC >= 0 && 1826 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) { 1827 softc->state = ADA_STATE_WCACHE; 1828 xpt_release_ccb(done_ccb); 1829 xpt_schedule(periph, CAM_PRIORITY_DEV); 1830 goto out; 1831 } 1832 softc->state = ADA_STATE_NORMAL; 1833 xpt_release_ccb(done_ccb); 1834 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 1835 cam_release_devq(path, 0, 0, 0, FALSE); 1836 adaschedule(periph); 1837 cam_periph_release_locked(periph); 1838 return; 1839 } 1840 case ADA_CCB_WCACHE: 1841 { 1842 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1843 if (adaerror(done_ccb, 0, 0) == ERESTART) { 1844 goto out; 1845 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1846 cam_release_devq(path, 1847 /*relsim_flags*/0, 1848 /*reduction*/0, 1849 /*timeout*/0, 1850 /*getcount_only*/0); 1851 } 1852 } 1853 1854 softc->state = ADA_STATE_NORMAL; 1855 /* 1856 * Since our peripheral may be invalidated by an error 1857 * above or an external event, we must release our CCB 1858 * before releasing the reference on the peripheral. 1859 * The peripheral will only go away once the last reference 1860 * is removed, and we need it around for the CCB release 1861 * operation. 1862 */ 1863 xpt_release_ccb(done_ccb); 1864 /* Drop freeze taken due to CAM_DEV_QFREEZE */ 1865 cam_release_devq(path, 0, 0, 0, FALSE); 1866 adaschedule(periph); 1867 cam_periph_release_locked(periph); 1868 return; 1869 } 1870 case ADA_CCB_DUMP: 1871 /* No-op. We're polling */ 1872 return; 1873 default: 1874 break; 1875 } 1876 xpt_release_ccb(done_ccb); 1877} 1878 1879static int 1880adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 1881{ 1882 1883 return(cam_periph_error(ccb, cam_flags, sense_flags, NULL)); 1884} 1885 1886static void 1887adagetparams(struct cam_periph *periph, struct ccb_getdev *cgd) 1888{ 1889 struct ada_softc *softc = (struct ada_softc *)periph->softc; 1890 struct disk_params *dp = &softc->params; 1891 u_int64_t lbasize48; 1892 u_int32_t lbasize; 1893 1894 dp->secsize = ata_logical_sector_size(&cgd->ident_data); 1895 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) && 1896 cgd->ident_data.current_heads && cgd->ident_data.current_sectors) { 1897 dp->heads = cgd->ident_data.current_heads; 1898 dp->secs_per_track = cgd->ident_data.current_sectors; 1899 dp->cylinders = cgd->ident_data.cylinders; 1900 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 | 1901 ((u_int32_t)cgd->ident_data.current_size_2 << 16); 1902 } else { 1903 dp->heads = cgd->ident_data.heads; 1904 dp->secs_per_track = cgd->ident_data.sectors; 1905 dp->cylinders = cgd->ident_data.cylinders; 1906 dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track; 1907 } 1908 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 | 1909 ((u_int32_t)cgd->ident_data.lba_size_2 << 16); 1910 1911 /* use the 28bit LBA size if valid or bigger than the CHS mapping */ 1912 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize) 1913 dp->sectors = lbasize; 1914 1915 /* use the 48bit LBA size if valid */ 1916 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) | 1917 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) | 1918 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) | 1919 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48); 1920 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) && 1921 lbasize48 > ATA_MAX_28BIT_LBA) 1922 dp->sectors = lbasize48; 1923} 1924 1925static void 1926adasendorderedtag(void *arg) 1927{ 1928 struct ada_softc *softc = arg; 1929 1930 if (ada_send_ordered) { 1931 if (softc->outstanding_cmds > 0) { 1932 if ((softc->flags & ADA_FLAG_WAS_OTAG) == 0) 1933 softc->flags |= ADA_FLAG_NEED_OTAG; 1934 softc->flags &= ~ADA_FLAG_WAS_OTAG; 1935 } 1936 } 1937 /* Queue us up again */ 1938 callout_reset(&softc->sendordered_c, 1939 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 1940 adasendorderedtag, softc); 1941} 1942 1943/* 1944 * Step through all ADA peripheral drivers, and if the device is still open, 1945 * sync the disk cache to physical media. 1946 */ 1947static void 1948adaflush(void) 1949{ 1950 struct cam_periph *periph; 1951 struct ada_softc *softc; 1952 union ccb *ccb; 1953 int error; 1954 1955 CAM_PERIPH_FOREACH(periph, &adadriver) { 1956 softc = (struct ada_softc *)periph->softc; 1957 if (SCHEDULER_STOPPED()) { 1958 /* If we paniced with the lock held, do not recurse. */ 1959 if (!cam_periph_owned(periph) && 1960 (softc->flags & ADA_FLAG_OPEN)) { 1961 adadump(softc->disk, NULL, 0, 0, 0); 1962 } 1963 continue; 1964 } 1965 cam_periph_lock(periph); 1966 /* 1967 * We only sync the cache if the drive is still open, and 1968 * if the drive is capable of it.. 1969 */ 1970 if (((softc->flags & ADA_FLAG_OPEN) == 0) || 1971 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) { 1972 cam_periph_unlock(periph); 1973 continue; 1974 } 1975 1976 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1977 cam_fill_ataio(&ccb->ataio, 1978 0, 1979 adadone, 1980 CAM_DIR_NONE, 1981 0, 1982 NULL, 1983 0, 1984 ada_default_timeout*1000); 1985 if (softc->flags & ADA_FLAG_CAN_48BIT) 1986 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1987 else 1988 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 1989 1990 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 1991 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 1992 softc->disk->d_devstat); 1993 if (error != 0) 1994 xpt_print(periph->path, "Synchronize cache failed\n"); 1995 xpt_release_ccb(ccb); 1996 cam_periph_unlock(periph); 1997 } 1998} 1999 2000static void 2001adaspindown(uint8_t cmd, int flags) 2002{ 2003 struct cam_periph *periph; 2004 struct ada_softc *softc; 2005 union ccb *ccb; 2006 int error; 2007 2008 CAM_PERIPH_FOREACH(periph, &adadriver) { 2009 /* If we paniced with lock held - not recurse here. */ 2010 if (cam_periph_owned(periph)) 2011 continue; 2012 cam_periph_lock(periph); 2013 softc = (struct ada_softc *)periph->softc; 2014 /* 2015 * We only spin-down the drive if it is capable of it.. 2016 */ 2017 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 2018 cam_periph_unlock(periph); 2019 continue; 2020 } 2021 2022 if (bootverbose) 2023 xpt_print(periph->path, "spin-down\n"); 2024 2025 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 2026 cam_fill_ataio(&ccb->ataio, 2027 0, 2028 adadone, 2029 CAM_DIR_NONE | flags, 2030 0, 2031 NULL, 2032 0, 2033 ada_default_timeout*1000); 2034 ata_28bit_cmd(&ccb->ataio, cmd, 0, 0, 0); 2035 2036 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 2037 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 2038 softc->disk->d_devstat); 2039 if (error != 0) 2040 xpt_print(periph->path, "Spin-down disk failed\n"); 2041 xpt_release_ccb(ccb); 2042 cam_periph_unlock(periph); 2043 } 2044} 2045 2046static void 2047adashutdown(void *arg, int howto) 2048{ 2049 2050 adaflush(); 2051 if (ada_spindown_shutdown != 0 && 2052 (howto & (RB_HALT | RB_POWEROFF)) != 0) 2053 adaspindown(ATA_STANDBY_IMMEDIATE, 0); 2054} 2055 2056static void 2057adasuspend(void *arg) 2058{ 2059 2060 adaflush(); 2061 if (ada_spindown_suspend != 0) 2062 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE); 2063} 2064 2065static void 2066adaresume(void *arg) 2067{ 2068 struct cam_periph *periph; 2069 struct ada_softc *softc; 2070 2071 if (ada_spindown_suspend == 0) 2072 return; 2073 2074 CAM_PERIPH_FOREACH(periph, &adadriver) { 2075 cam_periph_lock(periph); 2076 softc = (struct ada_softc *)periph->softc; 2077 /* 2078 * We only spin-down the drive if it is capable of it.. 2079 */ 2080 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 2081 cam_periph_unlock(periph); 2082 continue; 2083 } 2084 2085 if (bootverbose) 2086 xpt_print(periph->path, "resume\n"); 2087 2088 /* 2089 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on 2090 * sleep request. 2091 */ 2092 cam_release_devq(periph->path, 2093 /*relsim_flags*/0, 2094 /*openings*/0, 2095 /*timeout*/0, 2096 /*getcount_only*/0); 2097 2098 cam_periph_unlock(periph); 2099 } 2100} 2101 2102#endif /* _KERNEL */ 2103