ata_da.c revision 308081
1263508Sdim/*- 2193323Sed * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org> 3193323Sed * All rights reserved. 4193323Sed * 5193323Sed * Redistribution and use in source and binary forms, with or without 6193323Sed * modification, are permitted provided that the following conditions 7193323Sed * are met: 8193323Sed * 1. Redistributions of source code must retain the above copyright 9193323Sed * notice, this list of conditions and the following disclaimer, 10193323Sed * without modification, immediately at the beginning of the file. 11193323Sed * 2. Redistributions in binary form must reproduce the above copyright 12193323Sed * notice, this list of conditions and the following disclaimer in the 13193323Sed * documentation and/or other materials provided with the distribution. 14193323Sed * 15193323Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16193323Sed * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17193323Sed * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18193323Sed * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19193323Sed * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20193323Sed * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21193323Sed * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22193323Sed * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23193323Sed * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24193323Sed * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25224145Sdim */ 26193323Sed 27193323Sed#include <sys/cdefs.h> 28193323Sed__FBSDID("$FreeBSD: stable/10/sys/cam/ata/ata_da.c 308081 2016-10-29 08:48:01Z mav $"); 29193323Sed 30193323Sed#include "opt_ada.h" 31193323Sed 32193323Sed#include <sys/param.h> 33193323Sed 34193323Sed#ifdef _KERNEL 35193323Sed#include <sys/systm.h> 36193323Sed#include <sys/kernel.h> 37193323Sed#include <sys/bio.h> 38193323Sed#include <sys/sysctl.h> 39193323Sed#include <sys/taskqueue.h> 40193323Sed#include <sys/lock.h> 41193323Sed#include <sys/mutex.h> 42193323Sed#include <sys/conf.h> 43193323Sed#include <sys/devicestat.h> 44199481Srdivacky#include <sys/eventhandler.h> 45193323Sed#include <sys/malloc.h> 46193323Sed#include <sys/cons.h> 47193323Sed#include <sys/proc.h> 48193323Sed#include <sys/reboot.h> 49224145Sdim#include <geom/geom_disk.h> 50224145Sdim#endif /* _KERNEL */ 51224145Sdim 52199481Srdivacky#ifndef _KERNEL 53193323Sed#include <stdio.h> 54193323Sed#include <string.h> 55193323Sed#endif /* _KERNEL */ 56193323Sed 57199481Srdivacky#include <cam/cam.h> 58193323Sed#include <cam/cam_ccb.h> 59193323Sed#include <cam/cam_periph.h> 60193323Sed#include <cam/cam_xpt_periph.h> 61193323Sed#include <cam/cam_sim.h> 62199481Srdivacky 63193323Sed#include <cam/ata/ata_all.h> 64193323Sed 65193323Sed#include <machine/md_var.h> /* geometry translation */ 66193323Sed 67199481Srdivacky#ifdef _KERNEL 68193323Sed 69193323Sed#define ATA_MAX_28BIT_LBA 268435455UL 70193323Sed 71193323Sedtypedef enum { 72221345Sdim ADA_STATE_RAHEAD, 73221345Sdim ADA_STATE_WCACHE, 74221345Sdim ADA_STATE_NORMAL 75221345Sdim} ada_state; 76221345Sdim 77221345Sdimtypedef enum { 78221345Sdim ADA_FLAG_CAN_48BIT = 0x0002, 79221345Sdim ADA_FLAG_CAN_FLUSHCACHE = 0x0004, 80221345Sdim ADA_FLAG_CAN_NCQ = 0x0008, 81221345Sdim ADA_FLAG_CAN_DMA = 0x0010, 82193323Sed ADA_FLAG_NEED_OTAG = 0x0020, 83193323Sed ADA_FLAG_WAS_OTAG = 0x0040, 84193323Sed ADA_FLAG_CAN_TRIM = 0x0080, 85193323Sed ADA_FLAG_OPEN = 0x0100, 86193323Sed ADA_FLAG_SCTX_INIT = 0x0200, 87193323Sed ADA_FLAG_CAN_CFA = 0x0400, 88193323Sed ADA_FLAG_CAN_POWERMGT = 0x0800, 89249423Sdim ADA_FLAG_CAN_DMA48 = 0x1000, 90193323Sed ADA_FLAG_DIRTY = 0x2000 91193323Sed} ada_flags; 92193323Sed 93193323Sedtypedef enum { 94249423Sdim ADA_Q_NONE = 0x00, 95193323Sed ADA_Q_4K = 0x01, 96193323Sed} ada_quirks; 97193323Sed 98193323Sed#define ADA_Q_BIT_STRING \ 99249423Sdim "\020" \ 100193323Sed "\0014K" 101193323Sed 102193323Sedtypedef enum { 103193323Sed ADA_CCB_RAHEAD = 0x01, 104249423Sdim ADA_CCB_WCACHE = 0x02, 105193323Sed ADA_CCB_BUFFER_IO = 0x03, 106193323Sed ADA_CCB_DUMP = 0x05, 107193323Sed ADA_CCB_TRIM = 0x06, 108193323Sed ADA_CCB_TYPE_MASK = 0x0F, 109249423Sdim} ada_ccb_state; 110193323Sed 111224145Sdim/* Offsets into our private area for storing information */ 112224145Sdim#define ccb_state ppriv_field0 113224145Sdim#define ccb_bp ppriv_ptr1 114249423Sdim 115224145Sdimstruct disk_params { 116193323Sed u_int8_t heads; 117193323Sed u_int8_t secs_per_track; 118193323Sed u_int32_t cylinders; 119249423Sdim u_int32_t secsize; /* Number of bytes/logical sector */ 120193323Sed u_int64_t sectors; /* Total number sectors */ 121193323Sed}; 122193323Sed 123193323Sed#define TRIM_MAX_BLOCKS 8 124249423Sdim#define TRIM_MAX_RANGES (TRIM_MAX_BLOCKS * ATA_DSM_BLK_RANGES) 125193323Sedstruct trim_request { 126193323Sed uint8_t data[TRIM_MAX_RANGES * ATA_DSM_RANGE_SIZE]; 127193323Sed TAILQ_HEAD(, bio) bps; 128193323Sed}; 129249423Sdim 130193323Sedstruct ada_softc { 131193323Sed struct bio_queue_head bio_queue; 132193323Sed struct bio_queue_head trim_queue; 133193323Sed int outstanding_cmds; /* Number of active commands */ 134249423Sdim int refcount; /* Active xpt_action() calls */ 135193323Sed ada_state state; 136193323Sed ada_flags flags; 137193323Sed ada_quirks quirks; 138193323Sed int sort_io_queue; 139249423Sdim int trim_max_ranges; 140193323Sed int trim_running; 141193323Sed int read_ahead; 142193323Sed int write_cache; 143193323Sed#ifdef ADA_TEST_FAILURE 144249423Sdim int force_read_error; 145193323Sed int force_write_error; 146193323Sed int periodic_read_error; 147193323Sed int periodic_read_count; 148193323Sed#endif 149249423Sdim struct disk_params params; 150193323Sed struct disk *disk; 151193323Sed struct task sysctl_task; 152193323Sed struct sysctl_ctx_list sysctl_ctx; 153193323Sed struct sysctl_oid *sysctl_tree; 154249423Sdim struct callout sendordered_c; 155193323Sed struct trim_request trim_req; 156193323Sed}; 157193323Sed 158193323Sedstruct ada_quirk_entry { 159249423Sdim struct scsi_inquiry_pattern inq_pat; 160193323Sed ada_quirks quirks; 161249423Sdim}; 162249423Sdim 163249423Sdimstatic struct ada_quirk_entry ada_quirk_table[] = 164249423Sdim{ 165249423Sdim { 166193323Sed /* Hitachi Advanced Format (4k) drives */ 167193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" }, 168193323Sed /*quirks*/ADA_Q_4K 169249423Sdim }, 170193323Sed { 171193323Sed /* Samsung Advanced Format (4k) drives */ 172193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD155UI*", "*" }, 173193323Sed /*quirks*/ADA_Q_4K 174249423Sdim }, 175193323Sed { 176193323Sed /* Samsung Advanced Format (4k) drives */ 177193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" }, 178193323Sed /*quirks*/ADA_Q_4K 179249423Sdim }, 180193323Sed { 181193323Sed /* Seagate Barracuda Green Advanced Format (4k) drives */ 182193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" }, 183193323Sed /*quirks*/ADA_Q_4K 184249423Sdim }, 185193323Sed { 186193323Sed /* Seagate Barracuda Advanced Format (4k) drives */ 187193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???DM*", "*" }, 188193323Sed /*quirks*/ADA_Q_4K 189249423Sdim }, 190193323Sed { 191263508Sdim /* Seagate Barracuda Advanced Format (4k) drives */ 192263508Sdim { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DM*", "*" }, 193263508Sdim /*quirks*/ADA_Q_4K 194263508Sdim }, 195263508Sdim { 196193323Sed /* Seagate Momentus Advanced Format (4k) drives */ 197193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" }, 198193323Sed /*quirks*/ADA_Q_4K 199249423Sdim }, 200193323Sed { 201205218Srdivacky /* Seagate Momentus Advanced Format (4k) drives */ 202205218Srdivacky { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" }, 203205218Srdivacky /*quirks*/ADA_Q_4K 204249423Sdim }, 205205218Srdivacky { 206193323Sed /* Seagate Momentus Advanced Format (4k) drives */ 207193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640423AS*", "*" }, 208249423Sdim /*quirks*/ADA_Q_4K 209249423Sdim }, 210193323Sed { 211205218Srdivacky /* Seagate Momentus Advanced Format (4k) drives */ 212205218Srdivacky { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640424AS*", "*" }, 213193323Sed /*quirks*/ADA_Q_4K 214193323Sed }, 215249423Sdim { 216193323Sed /* Seagate Momentus Advanced Format (4k) drives */ 217193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" }, 218249423Sdim /*quirks*/ADA_Q_4K 219193323Sed }, 220194612Sed { 221194612Sed /* Seagate Momentus Advanced Format (4k) drives */ 222193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" }, 223193323Sed /*quirks*/ADA_Q_4K 224193323Sed }, 225206083Srdivacky { 226206083Srdivacky /* Seagate Momentus Advanced Format (4k) drives */ 227193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750423AS*", "*" }, 228193323Sed /*quirks*/ADA_Q_4K 229193323Sed }, 230193323Sed { 231193323Sed /* Seagate Momentus Thin Advanced Format (4k) drives */ 232193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" }, 233249423Sdim /*quirks*/ADA_Q_4K 234249423Sdim }, 235249423Sdim { 236193323Sed /* WDC Caviar Red Advanced Format (4k) drives */ 237193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????CX*", "*" }, 238193323Sed /*quirks*/ADA_Q_4K 239194612Sed }, 240194612Sed { 241193323Sed /* WDC Caviar Green Advanced Format (4k) drives */ 242193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" }, 243193323Sed /*quirks*/ADA_Q_4K 244206083Srdivacky }, 245206083Srdivacky { 246193323Sed /* WDC Caviar Green/Red Advanced Format (4k) drives */ 247193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" }, 248193323Sed /*quirks*/ADA_Q_4K 249193323Sed }, 250193323Sed { 251193323Sed /* WDC Caviar Red Advanced Format (4k) drives */ 252249423Sdim { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????CX*", "*" }, 253249423Sdim /*quirks*/ADA_Q_4K 254249423Sdim }, 255193323Sed { 256193323Sed /* WDC Caviar Black Advanced Format (4k) drives */ 257193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????EX*", "*" }, 258193323Sed /*quirks*/ADA_Q_4K 259193323Sed }, 260193323Sed { 261249423Sdim /* WDC Caviar Green Advanced Format (4k) drives */ 262193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" }, 263193323Sed /*quirks*/ADA_Q_4K 264193323Sed }, 265193323Sed { 266249423Sdim /* WDC Caviar Green Advanced Format (4k) drives */ 267193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" }, 268193323Sed /*quirks*/ADA_Q_4K 269193323Sed }, 270193323Sed { 271249423Sdim /* WDC Scorpio Black Advanced Format (4k) drives */ 272193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" }, 273193323Sed /*quirks*/ADA_Q_4K 274193323Sed }, 275193323Sed { 276249423Sdim /* WDC Scorpio Black Advanced Format (4k) drives */ 277193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" }, 278193323Sed /*quirks*/ADA_Q_4K 279193323Sed }, 280193323Sed { 281249423Sdim /* WDC Scorpio Blue Advanced Format (4k) drives */ 282193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" }, 283193323Sed /*quirks*/ADA_Q_4K 284193323Sed }, 285193323Sed { 286249423Sdim /* WDC Scorpio Blue Advanced Format (4k) drives */ 287193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" }, 288193323Sed /*quirks*/ADA_Q_4K 289193323Sed }, 290193323Sed /* SSDs */ 291193323Sed { 292249423Sdim /* 293193323Sed * Corsair Force 2 SSDs 294193323Sed * 4k optimised & trim only works in 4k requests + 4k aligned 295249423Sdim */ 296193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair CSSD-F*", "*" }, 297193323Sed /*quirks*/ADA_Q_4K 298249423Sdim }, 299193323Sed { 300193323Sed /* 301249423Sdim * Corsair Force 3 SSDs 302193323Sed * 4k optimised & trim only works in 4k requests + 4k aligned 303193323Sed */ 304249423Sdim { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force 3*", "*" }, 305193323Sed /*quirks*/ADA_Q_4K 306193323Sed }, 307249423Sdim { 308193323Sed /* 309193323Sed * Corsair Neutron GTX SSDs 310249423Sdim * 4k optimised & trim only works in 4k requests + 4k aligned 311193323Sed */ 312193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" }, 313249423Sdim /*quirks*/ADA_Q_4K 314193323Sed }, 315198090Srdivacky { 316198090Srdivacky /* 317198090Srdivacky * Corsair Force GT & GS SSDs 318198090Srdivacky * 4k optimised & trim only works in 4k requests + 4k aligned 319198090Srdivacky */ 320193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force G*", "*" }, 321193323Sed /*quirks*/ADA_Q_4K 322193323Sed }, 323210299Sed { 324210299Sed /* 325210299Sed * Crucial M4 SSDs 326210299Sed * 4k optimised & trim only works in 4k requests + 4k aligned 327210299Sed */ 328263508Sdim { T_DIRECT, SIP_MEDIA_FIXED, "*", "M4-CT???M4SSD2*", "*" }, 329210299Sed /*quirks*/ADA_Q_4K 330210299Sed }, 331210299Sed { 332210299Sed /* 333263508Sdim * Crucial RealSSD C300 SSDs 334210299Sed * 4k optimised 335210299Sed */ 336210299Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "C300-CTFDDAC???MAG*", 337210299Sed "*" }, /*quirks*/ADA_Q_4K 338263508Sdim }, 339210299Sed { 340210299Sed /* 341210299Sed * Intel 320 Series SSDs 342210299Sed * 4k optimised & trim only works in 4k requests + 4k aligned 343263508Sdim */ 344210299Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2CW*", "*" }, 345210299Sed /*quirks*/ADA_Q_4K 346210299Sed }, 347210299Sed { 348263508Sdim /* 349210299Sed * Intel 330 Series SSDs 350210299Sed * 4k optimised & trim only works in 4k requests + 4k aligned 351210299Sed */ 352210299Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2CT*", "*" }, 353263508Sdim /*quirks*/ADA_Q_4K 354210299Sed }, 355210299Sed { 356210299Sed /* 357210299Sed * Intel 510 Series SSDs 358263508Sdim * 4k optimised & trim only works in 4k requests + 4k aligned 359210299Sed */ 360210299Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2MH*", "*" }, 361210299Sed /*quirks*/ADA_Q_4K 362210299Sed }, 363263508Sdim { 364263508Sdim /* 365263508Sdim * Intel 520 Series SSDs 366263508Sdim * 4k optimised & trim only works in 4k requests + 4k aligned 367263508Sdim */ 368263508Sdim { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2BW*", "*" }, 369263508Sdim /*quirks*/ADA_Q_4K 370263508Sdim }, 371263508Sdim { 372263508Sdim /* 373263508Sdim * Intel X25-M Series SSDs 374263508Sdim * 4k optimised & trim only works in 4k requests + 4k aligned 375263508Sdim */ 376263508Sdim { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2M*", "*" }, 377263508Sdim /*quirks*/ADA_Q_4K 378263508Sdim }, 379263508Sdim { 380263508Sdim /* 381263508Sdim * Kingston E100 Series SSDs 382263508Sdim * 4k optimised & trim only works in 4k requests + 4k aligned 383263508Sdim */ 384210299Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SE100S3*", "*" }, 385263508Sdim /*quirks*/ADA_Q_4K 386263508Sdim }, 387263508Sdim { 388193323Sed /* 389193323Sed * Kingston HyperX 3k SSDs 390193323Sed * 4k optimised & trim only works in 4k requests + 4k aligned 391193323Sed */ 392193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SH103S3*", "*" }, 393198090Srdivacky /*quirks*/ADA_Q_4K 394193323Sed }, 395193323Sed { 396193323Sed /* 397198090Srdivacky * Marvell SSDs (entry taken from OpenSolaris) 398193323Sed * 4k optimised & trim only works in 4k requests + 4k aligned 399193323Sed */ 400193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "MARVELL SD88SA02*", "*" }, 401198090Srdivacky /*quirks*/ADA_Q_4K 402193323Sed }, 403193323Sed { 404193323Sed /* 405198090Srdivacky * OCZ Agility 2 SSDs 406193323Sed * 4k optimised & trim only works in 4k requests + 4k aligned 407193323Sed */ 408193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" }, 409198090Srdivacky /*quirks*/ADA_Q_4K 410193323Sed }, 411193323Sed { 412193323Sed /* 413198090Srdivacky * OCZ Agility 3 SSDs 414193323Sed * 4k optimised & trim only works in 4k requests + 4k aligned 415193323Sed */ 416193323Sed { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY3*", "*" }, 417193323Sed /*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 850 SSDs 470 * 4k optimised 471 */ 472 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 850*", "*" }, 473 /*quirks*/ADA_Q_4K 474 }, 475 { 476 /* 477 * Samsung 843T Series SSDs (MZ7WD*) 478 * Samsung PM851 Series SSDs (MZ7TE*) 479 * Samsung PM853T Series SSDs (MZ7GE*) 480 * Samsung SM863 Series SSDs (MZ7KM*) 481 * 4k optimised 482 */ 483 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7*", "*" }, 484 /*quirks*/ADA_Q_4K 485 }, 486 { 487 /* 488 * SuperTalent TeraDrive CT SSDs 489 * 4k optimised & trim only works in 4k requests + 4k aligned 490 */ 491 { T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" }, 492 /*quirks*/ADA_Q_4K 493 }, 494 { 495 /* 496 * XceedIOPS SATA SSDs 497 * 4k optimised 498 */ 499 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" }, 500 /*quirks*/ADA_Q_4K 501 }, 502 { 503 /* Default */ 504 { 505 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, 506 /*vendor*/"*", /*product*/"*", /*revision*/"*" 507 }, 508 /*quirks*/0 509 }, 510}; 511 512static disk_strategy_t adastrategy; 513static dumper_t adadump; 514static periph_init_t adainit; 515static void adaasync(void *callback_arg, u_int32_t code, 516 struct cam_path *path, void *arg); 517static void adasysctlinit(void *context, int pending); 518static periph_ctor_t adaregister; 519static periph_dtor_t adacleanup; 520static periph_start_t adastart; 521static periph_oninv_t adaoninvalidate; 522static void adadone(struct cam_periph *periph, 523 union ccb *done_ccb); 524static int adaerror(union ccb *ccb, u_int32_t cam_flags, 525 u_int32_t sense_flags); 526static void adagetparams(struct cam_periph *periph, 527 struct ccb_getdev *cgd); 528static timeout_t adasendorderedtag; 529static void adashutdown(void *arg, int howto); 530static void adasuspend(void *arg); 531static void adaresume(void *arg); 532 533#ifndef ADA_DEFAULT_LEGACY_ALIASES 534#define ADA_DEFAULT_LEGACY_ALIASES 1 535#endif 536 537#ifndef ADA_DEFAULT_TIMEOUT 538#define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */ 539#endif 540 541#ifndef ADA_DEFAULT_RETRY 542#define ADA_DEFAULT_RETRY 4 543#endif 544 545#ifndef ADA_DEFAULT_SEND_ORDERED 546#define ADA_DEFAULT_SEND_ORDERED 1 547#endif 548 549#ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN 550#define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1 551#endif 552 553#ifndef ADA_DEFAULT_SPINDOWN_SUSPEND 554#define ADA_DEFAULT_SPINDOWN_SUSPEND 1 555#endif 556 557#ifndef ADA_DEFAULT_READ_AHEAD 558#define ADA_DEFAULT_READ_AHEAD 1 559#endif 560 561#ifndef ADA_DEFAULT_WRITE_CACHE 562#define ADA_DEFAULT_WRITE_CACHE 1 563#endif 564 565#define ADA_RA (softc->read_ahead >= 0 ? \ 566 softc->read_ahead : ada_read_ahead) 567#define ADA_WC (softc->write_cache >= 0 ? \ 568 softc->write_cache : ada_write_cache) 569#define ADA_SIO (softc->sort_io_queue >= 0 ? \ 570 softc->sort_io_queue : cam_sort_io_queues) 571 572/* 573 * Most platforms map firmware geometry to actual, but some don't. If 574 * not overridden, default to nothing. 575 */ 576#ifndef ata_disk_firmware_geom_adjust 577#define ata_disk_firmware_geom_adjust(disk) 578#endif 579 580static int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES; 581static int ada_retry_count = ADA_DEFAULT_RETRY; 582static int ada_default_timeout = ADA_DEFAULT_TIMEOUT; 583static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED; 584static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN; 585static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND; 586static int ada_read_ahead = ADA_DEFAULT_READ_AHEAD; 587static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE; 588 589static SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0, 590 "CAM Direct Access Disk driver"); 591SYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RW, 592 &ada_legacy_aliases, 0, "Create legacy-like device aliases"); 593TUNABLE_INT("kern.cam.ada.legacy_aliases", &ada_legacy_aliases); 594SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW, 595 &ada_retry_count, 0, "Normal I/O retry count"); 596TUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count); 597SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW, 598 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)"); 599TUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout); 600SYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RW, 601 &ada_send_ordered, 0, "Send Ordered Tags"); 602TUNABLE_INT("kern.cam.ada.send_ordered", &ada_send_ordered); 603SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RW, 604 &ada_spindown_shutdown, 0, "Spin down upon shutdown"); 605TUNABLE_INT("kern.cam.ada.spindown_shutdown", &ada_spindown_shutdown); 606SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RW, 607 &ada_spindown_suspend, 0, "Spin down upon suspend"); 608TUNABLE_INT("kern.cam.ada.spindown_suspend", &ada_spindown_suspend); 609SYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RW, 610 &ada_read_ahead, 0, "Enable disk read-ahead"); 611TUNABLE_INT("kern.cam.ada.read_ahead", &ada_read_ahead); 612SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RW, 613 &ada_write_cache, 0, "Enable disk write cache"); 614TUNABLE_INT("kern.cam.ada.write_cache", &ada_write_cache); 615 616/* 617 * ADA_ORDEREDTAG_INTERVAL determines how often, relative 618 * to the default timeout, we check to see whether an ordered 619 * tagged transaction is appropriate to prevent simple tag 620 * starvation. Since we'd like to ensure that there is at least 621 * 1/2 of the timeout length left for a starved transaction to 622 * complete after we've sent an ordered tag, we must poll at least 623 * four times in every timeout period. This takes care of the worst 624 * case where a starved transaction starts during an interval that 625 * meets the requirement "don't send an ordered tag" test so it takes 626 * us two intervals to determine that a tag must be sent. 627 */ 628#ifndef ADA_ORDEREDTAG_INTERVAL 629#define ADA_ORDEREDTAG_INTERVAL 4 630#endif 631 632static struct periph_driver adadriver = 633{ 634 adainit, "ada", 635 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0 636}; 637 638PERIPHDRIVER_DECLARE(ada, adadriver); 639 640static int 641adaopen(struct disk *dp) 642{ 643 struct cam_periph *periph; 644 struct ada_softc *softc; 645 int error; 646 647 periph = (struct cam_periph *)dp->d_drv1; 648 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 649 return(ENXIO); 650 } 651 652 cam_periph_lock(periph); 653 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) { 654 cam_periph_unlock(periph); 655 cam_periph_release(periph); 656 return (error); 657 } 658 659 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 660 ("adaopen\n")); 661 662 softc = (struct ada_softc *)periph->softc; 663 softc->flags |= ADA_FLAG_OPEN; 664 665 cam_periph_unhold(periph); 666 cam_periph_unlock(periph); 667 return (0); 668} 669 670static int 671adaclose(struct disk *dp) 672{ 673 struct cam_periph *periph; 674 struct ada_softc *softc; 675 union ccb *ccb; 676 int error; 677 678 periph = (struct cam_periph *)dp->d_drv1; 679 softc = (struct ada_softc *)periph->softc; 680 cam_periph_lock(periph); 681 682 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 683 ("adaclose\n")); 684 685 /* We only sync the cache if the drive is capable of it. */ 686 if ((softc->flags & ADA_FLAG_DIRTY) != 0 && 687 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 && 688 (periph->flags & CAM_PERIPH_INVALID) == 0 && 689 cam_periph_hold(periph, PRIBIO) == 0) { 690 691 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 692 cam_fill_ataio(&ccb->ataio, 693 1, 694 adadone, 695 CAM_DIR_NONE, 696 0, 697 NULL, 698 0, 699 ada_default_timeout*1000); 700 701 if (softc->flags & ADA_FLAG_CAN_48BIT) 702 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 703 else 704 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 705 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 706 /*sense_flags*/0, softc->disk->d_devstat); 707 708 if (error != 0) 709 xpt_print(periph->path, "Synchronize cache failed\n"); 710 softc->flags &= ~ADA_FLAG_DIRTY; 711 xpt_release_ccb(ccb); 712 cam_periph_unhold(periph); 713 } 714 715 softc->flags &= ~ADA_FLAG_OPEN; 716 717 while (softc->refcount != 0) 718 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "adaclose", 1); 719 cam_periph_unlock(periph); 720 cam_periph_release(periph); 721 return (0); 722} 723 724static void 725adaschedule(struct cam_periph *periph) 726{ 727 struct ada_softc *softc = (struct ada_softc *)periph->softc; 728 729 if (softc->state != ADA_STATE_NORMAL) 730 return; 731 732 /* Check if we have more work to do. */ 733 if (bioq_first(&softc->bio_queue) || 734 (!softc->trim_running && bioq_first(&softc->trim_queue))) { 735 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 736 } 737} 738 739/* 740 * Actually translate the requested transfer into one the physical driver 741 * can understand. The transfer is described by a buf and will include 742 * only one physical transfer. 743 */ 744static void 745adastrategy(struct bio *bp) 746{ 747 struct cam_periph *periph; 748 struct ada_softc *softc; 749 750 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 751 softc = (struct ada_softc *)periph->softc; 752 753 cam_periph_lock(periph); 754 755 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp)); 756 757 /* 758 * If the device has been made invalid, error out 759 */ 760 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 761 cam_periph_unlock(periph); 762 biofinish(bp, NULL, ENXIO); 763 return; 764 } 765 766 /* 767 * Place it in the queue of disk activities for this disk 768 */ 769 if (bp->bio_cmd == BIO_DELETE) { 770 bioq_disksort(&softc->trim_queue, bp); 771 } else { 772 if (ADA_SIO) 773 bioq_disksort(&softc->bio_queue, bp); 774 else 775 bioq_insert_tail(&softc->bio_queue, bp); 776 } 777 778 /* 779 * Schedule ourselves for performing the work. 780 */ 781 adaschedule(periph); 782 cam_periph_unlock(periph); 783 784 return; 785} 786 787static int 788adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) 789{ 790 struct cam_periph *periph; 791 struct ada_softc *softc; 792 u_int secsize; 793 union ccb ccb; 794 struct disk *dp; 795 uint64_t lba; 796 uint16_t count; 797 int error = 0; 798 799 dp = arg; 800 periph = dp->d_drv1; 801 softc = (struct ada_softc *)periph->softc; 802 cam_periph_lock(periph); 803 secsize = softc->params.secsize; 804 lba = offset / secsize; 805 count = length / secsize; 806 807 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 808 cam_periph_unlock(periph); 809 return (ENXIO); 810 } 811 812 if (length > 0) { 813 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 814 ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 815 cam_fill_ataio(&ccb.ataio, 816 0, 817 adadone, 818 CAM_DIR_OUT, 819 0, 820 (u_int8_t *) virtual, 821 length, 822 ada_default_timeout*1000); 823 if ((softc->flags & ADA_FLAG_CAN_48BIT) && 824 (lba + count >= ATA_MAX_28BIT_LBA || 825 count >= 256)) { 826 ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48, 827 0, lba, count); 828 } else { 829 ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA, 830 0, lba, count); 831 } 832 xpt_polled_action(&ccb); 833 834 error = cam_periph_error(&ccb, 835 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 836 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 837 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0, 838 /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 839 if (error != 0) 840 printf("Aborting dump due to I/O error.\n"); 841 842 cam_periph_unlock(periph); 843 return (error); 844 } 845 846 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) { 847 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 848 849 ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 850 cam_fill_ataio(&ccb.ataio, 851 0, 852 adadone, 853 CAM_DIR_NONE, 854 0, 855 NULL, 856 0, 857 ada_default_timeout*1000); 858 859 if (softc->flags & ADA_FLAG_CAN_48BIT) 860 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0); 861 else 862 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0); 863 xpt_polled_action(&ccb); 864 865 error = cam_periph_error(&ccb, 866 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 867 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 868 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0, 869 /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 870 if (error != 0) 871 xpt_print(periph->path, "Synchronize cache failed\n"); 872 } 873 cam_periph_unlock(periph); 874 return (error); 875} 876 877static void 878adainit(void) 879{ 880 cam_status status; 881 882 /* 883 * Install a global async callback. This callback will 884 * receive async callbacks like "new device found". 885 */ 886 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL); 887 888 if (status != CAM_REQ_CMP) { 889 printf("ada: Failed to attach master async callback " 890 "due to status 0x%x!\n", status); 891 } else if (ada_send_ordered) { 892 893 /* Register our event handlers */ 894 if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend, 895 NULL, EVENTHANDLER_PRI_LAST)) == NULL) 896 printf("adainit: power event registration failed!\n"); 897 if ((EVENTHANDLER_REGISTER(power_resume, adaresume, 898 NULL, EVENTHANDLER_PRI_LAST)) == NULL) 899 printf("adainit: power event registration failed!\n"); 900 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown, 901 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 902 printf("adainit: shutdown event registration failed!\n"); 903 } 904} 905 906/* 907 * Callback from GEOM, called when it has finished cleaning up its 908 * resources. 909 */ 910static void 911adadiskgonecb(struct disk *dp) 912{ 913 struct cam_periph *periph; 914 915 periph = (struct cam_periph *)dp->d_drv1; 916 917 cam_periph_release(periph); 918} 919 920static void 921adaoninvalidate(struct cam_periph *periph) 922{ 923 struct ada_softc *softc; 924 925 softc = (struct ada_softc *)periph->softc; 926 927 /* 928 * De-register any async callbacks. 929 */ 930 xpt_register_async(0, adaasync, periph, periph->path); 931 932 /* 933 * Return all queued I/O with ENXIO. 934 * XXX Handle any transactions queued to the card 935 * with XPT_ABORT_CCB. 936 */ 937 bioq_flush(&softc->bio_queue, NULL, ENXIO); 938 bioq_flush(&softc->trim_queue, NULL, ENXIO); 939 940 disk_gone(softc->disk); 941} 942 943static void 944adacleanup(struct cam_periph *periph) 945{ 946 struct ada_softc *softc; 947 948 softc = (struct ada_softc *)periph->softc; 949 950 cam_periph_unlock(periph); 951 952 /* 953 * If we can't free the sysctl tree, oh well... 954 */ 955 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0 956 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) { 957 xpt_print(periph->path, "can't remove sysctl context\n"); 958 } 959 960 disk_destroy(softc->disk); 961 callout_drain(&softc->sendordered_c); 962 free(softc, M_DEVBUF); 963 cam_periph_lock(periph); 964} 965 966static void 967adaasync(void *callback_arg, u_int32_t code, 968 struct cam_path *path, void *arg) 969{ 970 struct ccb_getdev cgd; 971 struct cam_periph *periph; 972 struct ada_softc *softc; 973 974 periph = (struct cam_periph *)callback_arg; 975 switch (code) { 976 case AC_FOUND_DEVICE: 977 { 978 struct ccb_getdev *cgd; 979 cam_status status; 980 981 cgd = (struct ccb_getdev *)arg; 982 if (cgd == NULL) 983 break; 984 985 if (cgd->protocol != PROTO_ATA) 986 break; 987 988 /* 989 * Allocate a peripheral instance for 990 * this device and start the probe 991 * process. 992 */ 993 status = cam_periph_alloc(adaregister, adaoninvalidate, 994 adacleanup, adastart, 995 "ada", CAM_PERIPH_BIO, 996 path, adaasync, 997 AC_FOUND_DEVICE, cgd); 998 999 if (status != CAM_REQ_CMP 1000 && status != CAM_REQ_INPROG) 1001 printf("adaasync: Unable to attach to new device " 1002 "due to status 0x%x\n", status); 1003 break; 1004 } 1005 case AC_GETDEV_CHANGED: 1006 { 1007 softc = (struct ada_softc *)periph->softc; 1008 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1009 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1010 xpt_action((union ccb *)&cgd); 1011 1012 if ((cgd.ident_data.capabilities1 & ATA_SUPPORT_DMA) && 1013 (cgd.inq_flags & SID_DMA)) 1014 softc->flags |= ADA_FLAG_CAN_DMA; 1015 else 1016 softc->flags &= ~ADA_FLAG_CAN_DMA; 1017 if (cgd.ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) { 1018 softc->flags |= ADA_FLAG_CAN_48BIT; 1019 if (cgd.inq_flags & SID_DMA48) 1020 softc->flags |= ADA_FLAG_CAN_DMA48; 1021 else 1022 softc->flags &= ~ADA_FLAG_CAN_DMA48; 1023 } else 1024 softc->flags &= ~(ADA_FLAG_CAN_48BIT | 1025 ADA_FLAG_CAN_DMA48); 1026 if ((cgd.ident_data.satacapabilities & ATA_SUPPORT_NCQ) && 1027 (cgd.inq_flags & SID_DMA) && (cgd.inq_flags & SID_CmdQue)) 1028 softc->flags |= ADA_FLAG_CAN_NCQ; 1029 else 1030 softc->flags &= ~ADA_FLAG_CAN_NCQ; 1031 if ((cgd.ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) && 1032 (cgd.inq_flags & SID_DMA)) 1033 softc->flags |= ADA_FLAG_CAN_TRIM; 1034 else 1035 softc->flags &= ~ADA_FLAG_CAN_TRIM; 1036 1037 cam_periph_async(periph, code, path, arg); 1038 break; 1039 } 1040 case AC_ADVINFO_CHANGED: 1041 { 1042 uintptr_t buftype; 1043 1044 buftype = (uintptr_t)arg; 1045 if (buftype == CDAI_TYPE_PHYS_PATH) { 1046 struct ada_softc *softc; 1047 1048 softc = periph->softc; 1049 disk_attr_changed(softc->disk, "GEOM::physpath", 1050 M_NOWAIT); 1051 } 1052 break; 1053 } 1054 case AC_SENT_BDR: 1055 case AC_BUS_RESET: 1056 { 1057 softc = (struct ada_softc *)periph->softc; 1058 cam_periph_async(periph, code, path, arg); 1059 if (softc->state != ADA_STATE_NORMAL) 1060 break; 1061 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1062 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1063 xpt_action((union ccb *)&cgd); 1064 if (ADA_RA >= 0 && 1065 cgd.ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) 1066 softc->state = ADA_STATE_RAHEAD; 1067 else if (ADA_WC >= 0 && 1068 cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) 1069 softc->state = ADA_STATE_WCACHE; 1070 else 1071 break; 1072 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 1073 softc->state = ADA_STATE_NORMAL; 1074 else 1075 xpt_schedule(periph, CAM_PRIORITY_DEV); 1076 } 1077 default: 1078 cam_periph_async(periph, code, path, arg); 1079 break; 1080 } 1081} 1082 1083static void 1084adasysctlinit(void *context, int pending) 1085{ 1086 struct cam_periph *periph; 1087 struct ada_softc *softc; 1088 char tmpstr[80], tmpstr2[80]; 1089 1090 periph = (struct cam_periph *)context; 1091 1092 /* periph was held for us when this task was enqueued */ 1093 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 1094 cam_periph_release(periph); 1095 return; 1096 } 1097 1098 softc = (struct ada_softc *)periph->softc; 1099 snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number); 1100 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 1101 1102 sysctl_ctx_init(&softc->sysctl_ctx); 1103 softc->flags |= ADA_FLAG_SCTX_INIT; 1104 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1105 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2, 1106 CTLFLAG_RD, 0, tmpstr); 1107 if (softc->sysctl_tree == NULL) { 1108 printf("adasysctlinit: unable to allocate sysctl tree\n"); 1109 cam_periph_release(periph); 1110 return; 1111 } 1112 1113 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1114 OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE, 1115 &softc->read_ahead, 0, "Enable disk read ahead."); 1116 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1117 OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE, 1118 &softc->write_cache, 0, "Enable disk write cache."); 1119 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1120 OID_AUTO, "sort_io_queue", CTLFLAG_RW | CTLFLAG_MPSAFE, 1121 &softc->sort_io_queue, 0, 1122 "Sort IO queue to try and optimise disk access patterns"); 1123#ifdef ADA_TEST_FAILURE 1124 /* 1125 * Add a 'door bell' sysctl which allows one to set it from userland 1126 * and cause something bad to happen. For the moment, we only allow 1127 * whacking the next read or write. 1128 */ 1129 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1130 OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1131 &softc->force_read_error, 0, 1132 "Force a read error for the next N reads."); 1133 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1134 OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1135 &softc->force_write_error, 0, 1136 "Force a write error for the next N writes."); 1137 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1138 OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1139 &softc->periodic_read_error, 0, 1140 "Force a read error every N reads (don't set too low)."); 1141#endif 1142 cam_periph_release(periph); 1143} 1144 1145static int 1146adagetattr(struct bio *bp) 1147{ 1148 int ret; 1149 struct cam_periph *periph; 1150 1151 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 1152 cam_periph_lock(periph); 1153 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute, 1154 periph->path); 1155 cam_periph_unlock(periph); 1156 if (ret == 0) 1157 bp->bio_completed = bp->bio_length; 1158 return ret; 1159} 1160 1161static cam_status 1162adaregister(struct cam_periph *periph, void *arg) 1163{ 1164 struct ada_softc *softc; 1165 struct ccb_pathinq cpi; 1166 struct ccb_getdev *cgd; 1167 char announce_buf[80], buf1[32]; 1168 struct disk_params *dp; 1169 caddr_t match; 1170 u_int maxio; 1171 int legacy_id, quirks; 1172 1173 cgd = (struct ccb_getdev *)arg; 1174 if (cgd == NULL) { 1175 printf("adaregister: no getdev CCB, can't register device\n"); 1176 return(CAM_REQ_CMP_ERR); 1177 } 1178 1179 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF, 1180 M_NOWAIT|M_ZERO); 1181 1182 if (softc == NULL) { 1183 printf("adaregister: Unable to probe new device. " 1184 "Unable to allocate softc\n"); 1185 return(CAM_REQ_CMP_ERR); 1186 } 1187 1188 bioq_init(&softc->bio_queue); 1189 bioq_init(&softc->trim_queue); 1190 1191 if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) && 1192 (cgd->inq_flags & SID_DMA)) 1193 softc->flags |= ADA_FLAG_CAN_DMA; 1194 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) { 1195 softc->flags |= ADA_FLAG_CAN_48BIT; 1196 if (cgd->inq_flags & SID_DMA48) 1197 softc->flags |= ADA_FLAG_CAN_DMA48; 1198 } 1199 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE) 1200 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE; 1201 if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT) 1202 softc->flags |= ADA_FLAG_CAN_POWERMGT; 1203 if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) && 1204 (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue)) 1205 softc->flags |= ADA_FLAG_CAN_NCQ; 1206 if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) && 1207 (cgd->inq_flags & SID_DMA)) { 1208 softc->flags |= ADA_FLAG_CAN_TRIM; 1209 softc->trim_max_ranges = TRIM_MAX_RANGES; 1210 if (cgd->ident_data.max_dsm_blocks != 0) { 1211 softc->trim_max_ranges = 1212 min(cgd->ident_data.max_dsm_blocks * 1213 ATA_DSM_BLK_RANGES, softc->trim_max_ranges); 1214 } 1215 } 1216 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA) 1217 softc->flags |= ADA_FLAG_CAN_CFA; 1218 1219 periph->softc = softc; 1220 1221 /* 1222 * See if this device has any quirks. 1223 */ 1224 match = cam_quirkmatch((caddr_t)&cgd->ident_data, 1225 (caddr_t)ada_quirk_table, 1226 sizeof(ada_quirk_table)/sizeof(*ada_quirk_table), 1227 sizeof(*ada_quirk_table), ata_identify_match); 1228 if (match != NULL) 1229 softc->quirks = ((struct ada_quirk_entry *)match)->quirks; 1230 else 1231 softc->quirks = ADA_Q_NONE; 1232 1233 bzero(&cpi, sizeof(cpi)); 1234 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE); 1235 cpi.ccb_h.func_code = XPT_PATH_INQ; 1236 xpt_action((union ccb *)&cpi); 1237 1238 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph); 1239 1240 /* 1241 * Register this media as a disk 1242 */ 1243 (void)cam_periph_hold(periph, PRIBIO); 1244 cam_periph_unlock(periph); 1245 snprintf(announce_buf, sizeof(announce_buf), 1246 "kern.cam.ada.%d.quirks", periph->unit_number); 1247 quirks = softc->quirks; 1248 TUNABLE_INT_FETCH(announce_buf, &quirks); 1249 softc->quirks = quirks; 1250 softc->read_ahead = -1; 1251 snprintf(announce_buf, sizeof(announce_buf), 1252 "kern.cam.ada.%d.read_ahead", periph->unit_number); 1253 TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead); 1254 softc->write_cache = -1; 1255 snprintf(announce_buf, sizeof(announce_buf), 1256 "kern.cam.ada.%d.write_cache", periph->unit_number); 1257 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache); 1258 /* Disable queue sorting for non-rotational media by default. */ 1259 if (cgd->ident_data.media_rotation_rate == ATA_RATE_NON_ROTATING) 1260 softc->sort_io_queue = 0; 1261 else 1262 softc->sort_io_queue = -1; 1263 adagetparams(periph, cgd); 1264 softc->disk = disk_alloc(); 1265 softc->disk->d_rotation_rate = cgd->ident_data.media_rotation_rate; 1266 softc->disk->d_devstat = devstat_new_entry(periph->periph_name, 1267 periph->unit_number, softc->params.secsize, 1268 DEVSTAT_ALL_SUPPORTED, 1269 DEVSTAT_TYPE_DIRECT | 1270 XPORT_DEVSTAT_TYPE(cpi.transport), 1271 DEVSTAT_PRIORITY_DISK); 1272 softc->disk->d_open = adaopen; 1273 softc->disk->d_close = adaclose; 1274 softc->disk->d_strategy = adastrategy; 1275 softc->disk->d_getattr = adagetattr; 1276 softc->disk->d_dump = adadump; 1277 softc->disk->d_gone = adadiskgonecb; 1278 softc->disk->d_name = "ada"; 1279 softc->disk->d_drv1 = periph; 1280 maxio = cpi.maxio; /* Honor max I/O size of SIM */ 1281 if (maxio == 0) 1282 maxio = DFLTPHYS; /* traditional default */ 1283 else if (maxio > MAXPHYS) 1284 maxio = MAXPHYS; /* for safety */ 1285 if (softc->flags & ADA_FLAG_CAN_48BIT) 1286 maxio = min(maxio, 65536 * softc->params.secsize); 1287 else /* 28bit ATA command limit */ 1288 maxio = min(maxio, 256 * softc->params.secsize); 1289 softc->disk->d_maxsize = maxio; 1290 softc->disk->d_unit = periph->unit_number; 1291 softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION; 1292 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) 1293 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE; 1294 if (softc->flags & ADA_FLAG_CAN_TRIM) { 1295 softc->disk->d_flags |= DISKFLAG_CANDELETE; 1296 softc->disk->d_delmaxsize = softc->params.secsize * 1297 ATA_DSM_RANGE_MAX * 1298 softc->trim_max_ranges; 1299 } else if ((softc->flags & ADA_FLAG_CAN_CFA) && 1300 !(softc->flags & ADA_FLAG_CAN_48BIT)) { 1301 softc->disk->d_flags |= DISKFLAG_CANDELETE; 1302 softc->disk->d_delmaxsize = 256 * softc->params.secsize; 1303 } else 1304 softc->disk->d_delmaxsize = maxio; 1305 if ((cpi.hba_misc & PIM_UNMAPPED) != 0) 1306 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO; 1307 strlcpy(softc->disk->d_descr, cgd->ident_data.model, 1308 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model))); 1309 strlcpy(softc->disk->d_ident, cgd->ident_data.serial, 1310 MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial))); 1311 softc->disk->d_hba_vendor = cpi.hba_vendor; 1312 softc->disk->d_hba_device = cpi.hba_device; 1313 softc->disk->d_hba_subvendor = cpi.hba_subvendor; 1314 softc->disk->d_hba_subdevice = cpi.hba_subdevice; 1315 1316 softc->disk->d_sectorsize = softc->params.secsize; 1317 softc->disk->d_mediasize = (off_t)softc->params.sectors * 1318 softc->params.secsize; 1319 if (ata_physical_sector_size(&cgd->ident_data) != 1320 softc->params.secsize) { 1321 softc->disk->d_stripesize = 1322 ata_physical_sector_size(&cgd->ident_data); 1323 softc->disk->d_stripeoffset = (softc->disk->d_stripesize - 1324 ata_logical_sector_offset(&cgd->ident_data)) % 1325 softc->disk->d_stripesize; 1326 } else if (softc->quirks & ADA_Q_4K) { 1327 softc->disk->d_stripesize = 4096; 1328 softc->disk->d_stripeoffset = 0; 1329 } 1330 softc->disk->d_fwsectors = softc->params.secs_per_track; 1331 softc->disk->d_fwheads = softc->params.heads; 1332 ata_disk_firmware_geom_adjust(softc->disk); 1333 1334 if (ada_legacy_aliases) { 1335#ifdef ATA_STATIC_ID 1336 legacy_id = xpt_path_legacy_ata_id(periph->path); 1337#else 1338 legacy_id = softc->disk->d_unit; 1339#endif 1340 if (legacy_id >= 0) { 1341 snprintf(announce_buf, sizeof(announce_buf), 1342 "kern.devalias.%s%d", 1343 softc->disk->d_name, softc->disk->d_unit); 1344 snprintf(buf1, sizeof(buf1), 1345 "ad%d", legacy_id); 1346 setenv(announce_buf, buf1); 1347 } 1348 } else 1349 legacy_id = -1; 1350 /* 1351 * Acquire a reference to the periph before we register with GEOM. 1352 * We'll release this reference once GEOM calls us back (via 1353 * adadiskgonecb()) telling us that our provider has been freed. 1354 */ 1355 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 1356 xpt_print(periph->path, "%s: lost periph during " 1357 "registration!\n", __func__); 1358 cam_periph_lock(periph); 1359 return (CAM_REQ_CMP_ERR); 1360 } 1361 disk_create(softc->disk, DISK_VERSION); 1362 cam_periph_lock(periph); 1363 cam_periph_unhold(periph); 1364 1365 dp = &softc->params; 1366 snprintf(announce_buf, sizeof(announce_buf), 1367 "%juMB (%ju %u byte sectors)", 1368 ((uintmax_t)dp->secsize * dp->sectors) / (1024 * 1024), 1369 (uintmax_t)dp->sectors, dp->secsize); 1370 xpt_announce_periph(periph, announce_buf); 1371 xpt_announce_quirks(periph, softc->quirks, ADA_Q_BIT_STRING); 1372 if (legacy_id >= 0) 1373 printf("%s%d: Previously was known as ad%d\n", 1374 periph->periph_name, periph->unit_number, legacy_id); 1375 1376 /* 1377 * Create our sysctl variables, now that we know 1378 * we have successfully attached. 1379 */ 1380 if (cam_periph_acquire(periph) == CAM_REQ_CMP) 1381 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task); 1382 1383 /* 1384 * Add async callbacks for bus reset and 1385 * bus device reset calls. I don't bother 1386 * checking if this fails as, in most cases, 1387 * the system will function just fine without 1388 * them and the only alternative would be to 1389 * not attach the device on failure. 1390 */ 1391 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE | 1392 AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED, 1393 adaasync, periph, periph->path); 1394 1395 /* 1396 * Schedule a periodic event to occasionally send an 1397 * ordered tag to a device. 1398 */ 1399 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0); 1400 callout_reset(&softc->sendordered_c, 1401 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 1402 adasendorderedtag, softc); 1403 1404 if (ADA_RA >= 0 && 1405 cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) { 1406 softc->state = ADA_STATE_RAHEAD; 1407 } else if (ADA_WC >= 0 && 1408 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) { 1409 softc->state = ADA_STATE_WCACHE; 1410 } else { 1411 softc->state = ADA_STATE_NORMAL; 1412 return(CAM_REQ_CMP); 1413 } 1414 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 1415 softc->state = ADA_STATE_NORMAL; 1416 else 1417 xpt_schedule(periph, CAM_PRIORITY_DEV); 1418 return(CAM_REQ_CMP); 1419} 1420 1421static void 1422ada_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 1423{ 1424 struct trim_request *req = &softc->trim_req; 1425 uint64_t lastlba = (uint64_t)-1; 1426 int c, lastcount = 0, off, ranges = 0; 1427 1428 bzero(req, sizeof(*req)); 1429 TAILQ_INIT(&req->bps); 1430 do { 1431 uint64_t lba = bp->bio_pblkno; 1432 int count = bp->bio_bcount / softc->params.secsize; 1433 1434 bioq_remove(&softc->trim_queue, bp); 1435 1436 /* Try to extend the previous range. */ 1437 if (lba == lastlba) { 1438 c = min(count, ATA_DSM_RANGE_MAX - lastcount); 1439 lastcount += c; 1440 off = (ranges - 1) * ATA_DSM_RANGE_SIZE; 1441 req->data[off + 6] = lastcount & 0xff; 1442 req->data[off + 7] = 1443 (lastcount >> 8) & 0xff; 1444 count -= c; 1445 lba += c; 1446 } 1447 1448 while (count > 0) { 1449 c = min(count, ATA_DSM_RANGE_MAX); 1450 off = ranges * ATA_DSM_RANGE_SIZE; 1451 req->data[off + 0] = lba & 0xff; 1452 req->data[off + 1] = (lba >> 8) & 0xff; 1453 req->data[off + 2] = (lba >> 16) & 0xff; 1454 req->data[off + 3] = (lba >> 24) & 0xff; 1455 req->data[off + 4] = (lba >> 32) & 0xff; 1456 req->data[off + 5] = (lba >> 40) & 0xff; 1457 req->data[off + 6] = c & 0xff; 1458 req->data[off + 7] = (c >> 8) & 0xff; 1459 lba += c; 1460 count -= c; 1461 lastcount = c; 1462 ranges++; 1463 /* 1464 * Its the caller's responsibility to ensure the 1465 * request will fit so we don't need to check for 1466 * overrun here 1467 */ 1468 } 1469 lastlba = lba; 1470 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue); 1471 bp = bioq_first(&softc->trim_queue); 1472 if (bp == NULL || 1473 bp->bio_bcount / softc->params.secsize > 1474 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) 1475 break; 1476 } while (1); 1477 cam_fill_ataio(ataio, 1478 ada_retry_count, 1479 adadone, 1480 CAM_DIR_OUT, 1481 0, 1482 req->data, 1483 ((ranges + ATA_DSM_BLK_RANGES - 1) / 1484 ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE, 1485 ada_default_timeout * 1000); 1486 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT, 1487 ATA_DSM_TRIM, 0, (ranges + ATA_DSM_BLK_RANGES - 1488 1) / ATA_DSM_BLK_RANGES); 1489} 1490 1491static void 1492ada_cfaerase(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 1493{ 1494 struct trim_request *req = &softc->trim_req; 1495 uint64_t lba = bp->bio_pblkno; 1496 uint16_t count = bp->bio_bcount / softc->params.secsize; 1497 1498 bzero(req, sizeof(*req)); 1499 TAILQ_INIT(&req->bps); 1500 bioq_remove(&softc->trim_queue, bp); 1501 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue); 1502 1503 cam_fill_ataio(ataio, 1504 ada_retry_count, 1505 adadone, 1506 CAM_DIR_NONE, 1507 0, 1508 NULL, 1509 0, 1510 ada_default_timeout*1000); 1511 1512 if (count >= 256) 1513 count = 0; 1514 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count); 1515} 1516 1517static void 1518adastart(struct cam_periph *periph, union ccb *start_ccb) 1519{ 1520 struct ada_softc *softc = (struct ada_softc *)periph->softc; 1521 struct ccb_ataio *ataio = &start_ccb->ataio; 1522 1523 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n")); 1524 1525 switch (softc->state) { 1526 case ADA_STATE_NORMAL: 1527 { 1528 struct bio *bp; 1529 u_int8_t tag_code; 1530 1531 /* Run TRIM if not running yet. */ 1532 if (!softc->trim_running && 1533 (bp = bioq_first(&softc->trim_queue)) != 0) { 1534 if (softc->flags & ADA_FLAG_CAN_TRIM) { 1535 ada_dsmtrim(softc, bp, ataio); 1536 } else if ((softc->flags & ADA_FLAG_CAN_CFA) && 1537 !(softc->flags & ADA_FLAG_CAN_48BIT)) { 1538 ada_cfaerase(softc, bp, ataio); 1539 } else { 1540 /* This can happen if DMA was disabled. */ 1541 bioq_remove(&softc->trim_queue, bp); 1542 biofinish(bp, NULL, EOPNOTSUPP); 1543 xpt_release_ccb(start_ccb); 1544 adaschedule(periph); 1545 return; 1546 } 1547 softc->trim_running = 1; 1548 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM; 1549 start_ccb->ccb_h.flags |= CAM_UNLOCKED; 1550 goto out; 1551 } 1552 /* Run regular command. */ 1553 bp = bioq_first(&softc->bio_queue); 1554 if (bp == NULL) { 1555 xpt_release_ccb(start_ccb); 1556 break; 1557 } 1558 bioq_remove(&softc->bio_queue, bp); 1559 1560 if ((bp->bio_flags & BIO_ORDERED) != 0 1561 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) { 1562 softc->flags &= ~ADA_FLAG_NEED_OTAG; 1563 softc->flags |= ADA_FLAG_WAS_OTAG; 1564 tag_code = 0; 1565 } else { 1566 tag_code = 1; 1567 } 1568 switch (bp->bio_cmd) { 1569 case BIO_WRITE: 1570 case BIO_READ: 1571 { 1572 uint64_t lba = bp->bio_pblkno; 1573 uint16_t count = bp->bio_bcount / softc->params.secsize; 1574 void *data_ptr; 1575 int rw_op; 1576 1577 if (bp->bio_cmd == BIO_WRITE) { 1578 softc->flags |= ADA_FLAG_DIRTY; 1579 rw_op = CAM_DIR_OUT; 1580 } else { 1581 rw_op = CAM_DIR_IN; 1582 } 1583 1584 data_ptr = bp->bio_data; 1585 if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) { 1586 rw_op |= CAM_DATA_BIO; 1587 data_ptr = bp; 1588 } 1589 1590#ifdef ADA_TEST_FAILURE 1591 int fail = 0; 1592 1593 /* 1594 * Support the failure ioctls. If the command is a 1595 * read, and there are pending forced read errors, or 1596 * if a write and pending write errors, then fail this 1597 * operation with EIO. This is useful for testing 1598 * purposes. Also, support having every Nth read fail. 1599 * 1600 * This is a rather blunt tool. 1601 */ 1602 if (bp->bio_cmd == BIO_READ) { 1603 if (softc->force_read_error) { 1604 softc->force_read_error--; 1605 fail = 1; 1606 } 1607 if (softc->periodic_read_error > 0) { 1608 if (++softc->periodic_read_count >= 1609 softc->periodic_read_error) { 1610 softc->periodic_read_count = 0; 1611 fail = 1; 1612 } 1613 } 1614 } else { 1615 if (softc->force_write_error) { 1616 softc->force_write_error--; 1617 fail = 1; 1618 } 1619 } 1620 if (fail) { 1621 biofinish(bp, NULL, EIO); 1622 xpt_release_ccb(start_ccb); 1623 adaschedule(periph); 1624 return; 1625 } 1626#endif 1627 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 || 1628 round_page(bp->bio_bcount + bp->bio_ma_offset) / 1629 PAGE_SIZE == bp->bio_ma_n, 1630 ("Short bio %p", bp)); 1631 cam_fill_ataio(ataio, 1632 ada_retry_count, 1633 adadone, 1634 rw_op, 1635 tag_code, 1636 data_ptr, 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