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