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