ata_da.c revision 331044
1195534Sscottl/*- 2195534Sscottl * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org> 3195534Sscottl * All rights reserved. 4195534Sscottl * 5195534Sscottl * Redistribution and use in source and binary forms, with or without 6195534Sscottl * modification, are permitted provided that the following conditions 7195534Sscottl * are met: 8195534Sscottl * 1. Redistributions of source code must retain the above copyright 9195534Sscottl * notice, this list of conditions and the following disclaimer, 10195534Sscottl * without modification, immediately at the beginning of the file. 11195534Sscottl * 2. Redistributions in binary form must reproduce the above copyright 12195534Sscottl * notice, this list of conditions and the following disclaimer in the 13195534Sscottl * documentation and/or other materials provided with the distribution. 14195534Sscottl * 15195534Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16195534Sscottl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17195534Sscottl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18195534Sscottl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19195534Sscottl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20195534Sscottl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21195534Sscottl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22195534Sscottl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23195534Sscottl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24195534Sscottl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25195534Sscottl */ 26195534Sscottl 27195534Sscottl#include <sys/cdefs.h> 28195534Sscottl__FBSDID("$FreeBSD: stable/11/sys/cam/ata/ata_da.c 331044 2018-03-16 02:44:18Z eadler $"); 29195534Sscottl 30220454Smav#include "opt_ada.h" 31220454Smav 32195534Sscottl#include <sys/param.h> 33195534Sscottl 34195534Sscottl#ifdef _KERNEL 35195534Sscottl#include <sys/systm.h> 36195534Sscottl#include <sys/kernel.h> 37195534Sscottl#include <sys/bio.h> 38195534Sscottl#include <sys/sysctl.h> 39195534Sscottl#include <sys/taskqueue.h> 40195534Sscottl#include <sys/lock.h> 41195534Sscottl#include <sys/mutex.h> 42195534Sscottl#include <sys/conf.h> 43195534Sscottl#include <sys/devicestat.h> 44195534Sscottl#include <sys/eventhandler.h> 45195534Sscottl#include <sys/malloc.h> 46300207Sken#include <sys/endian.h> 47195534Sscottl#include <sys/cons.h> 48251792Smav#include <sys/proc.h> 49214279Sbrucec#include <sys/reboot.h> 50300207Sken#include <sys/sbuf.h> 51195534Sscottl#include <geom/geom_disk.h> 52195534Sscottl#endif /* _KERNEL */ 53195534Sscottl 54195534Sscottl#ifndef _KERNEL 55195534Sscottl#include <stdio.h> 56195534Sscottl#include <string.h> 57195534Sscottl#endif /* _KERNEL */ 58195534Sscottl 59195534Sscottl#include <cam/cam.h> 60195534Sscottl#include <cam/cam_ccb.h> 61195534Sscottl#include <cam/cam_periph.h> 62195534Sscottl#include <cam/cam_xpt_periph.h> 63300207Sken#include <cam/scsi/scsi_all.h> 64300207Sken#include <cam/scsi/scsi_da.h> 65195534Sscottl#include <cam/cam_sim.h> 66298002Simp#include <cam/cam_iosched.h> 67195534Sscottl 68195534Sscottl#include <cam/ata/ata_all.h> 69195534Sscottl 70208349Smarius#include <machine/md_var.h> /* geometry translation */ 71208349Smarius 72195534Sscottl#ifdef _KERNEL 73195534Sscottl 74195534Sscottl#define ATA_MAX_28BIT_LBA 268435455UL 75195534Sscottl 76298002Simpextern int iosched_debug; 77298002Simp 78195534Sscottltypedef enum { 79224497Smav ADA_STATE_RAHEAD, 80220412Smav ADA_STATE_WCACHE, 81300207Sken ADA_STATE_LOGDIR, 82300207Sken ADA_STATE_IDDIR, 83300207Sken ADA_STATE_SUP_CAP, 84300207Sken ADA_STATE_ZONE, 85198708Smav ADA_STATE_NORMAL 86195534Sscottl} ada_state; 87195534Sscottl 88195534Sscottltypedef enum { 89300207Sken ADA_FLAG_CAN_48BIT = 0x00000002, 90300207Sken ADA_FLAG_CAN_FLUSHCACHE = 0x00000004, 91300207Sken ADA_FLAG_CAN_NCQ = 0x00000008, 92300207Sken ADA_FLAG_CAN_DMA = 0x00000010, 93300207Sken ADA_FLAG_NEED_OTAG = 0x00000020, 94300207Sken ADA_FLAG_WAS_OTAG = 0x00000040, 95300207Sken ADA_FLAG_CAN_TRIM = 0x00000080, 96300207Sken ADA_FLAG_OPEN = 0x00000100, 97300207Sken ADA_FLAG_SCTX_INIT = 0x00000200, 98300207Sken ADA_FLAG_CAN_CFA = 0x00000400, 99300207Sken ADA_FLAG_CAN_POWERMGT = 0x00000800, 100300207Sken ADA_FLAG_CAN_DMA48 = 0x00001000, 101300207Sken ADA_FLAG_CAN_LOG = 0x00002000, 102300207Sken ADA_FLAG_CAN_IDLOG = 0x00004000, 103300207Sken ADA_FLAG_CAN_SUPCAP = 0x00008000, 104300207Sken ADA_FLAG_CAN_ZONE = 0x00010000, 105300207Sken ADA_FLAG_CAN_WCACHE = 0x00020000, 106300207Sken ADA_FLAG_CAN_RAHEAD = 0x00040000, 107300207Sken ADA_FLAG_PROBED = 0x00080000, 108300207Sken ADA_FLAG_ANNOUNCED = 0x00100000, 109300207Sken ADA_FLAG_DIRTY = 0x00200000, 110300207Sken ADA_FLAG_CAN_NCQ_TRIM = 0x00400000, /* CAN_TRIM also set */ 111300207Sken ADA_FLAG_PIM_ATA_EXT = 0x00800000 112195534Sscottl} ada_flags; 113195534Sscottl 114195534Sscottltypedef enum { 115222520Smav ADA_Q_NONE = 0x00, 116222520Smav ADA_Q_4K = 0x01, 117298002Simp ADA_Q_NCQ_TRIM_BROKEN = 0x02, 118300640Sken ADA_Q_LOG_BROKEN = 0x04, 119300640Sken ADA_Q_SMR_DM = 0x08 120195534Sscottl} ada_quirks; 121195534Sscottl 122250792Ssmh#define ADA_Q_BIT_STRING \ 123250792Ssmh "\020" \ 124298002Simp "\0014K" \ 125300640Sken "\002NCQ_TRIM_BROKEN" \ 126300640Sken "\003LOG_BROKEN" \ 127300640Sken "\004SMR_DM" 128250792Ssmh 129195534Sscottltypedef enum { 130224497Smav ADA_CCB_RAHEAD = 0x01, 131224497Smav ADA_CCB_WCACHE = 0x02, 132195534Sscottl ADA_CCB_BUFFER_IO = 0x03, 133195534Sscottl ADA_CCB_DUMP = 0x05, 134201139Smav ADA_CCB_TRIM = 0x06, 135300207Sken ADA_CCB_LOGDIR = 0x07, 136300207Sken ADA_CCB_IDDIR = 0x08, 137300207Sken ADA_CCB_SUP_CAP = 0x09, 138300207Sken ADA_CCB_ZONE = 0x0a, 139195534Sscottl ADA_CCB_TYPE_MASK = 0x0F, 140195534Sscottl} ada_ccb_state; 141195534Sscottl 142300207Skentypedef enum { 143300207Sken ADA_ZONE_NONE = 0x00, 144300207Sken ADA_ZONE_DRIVE_MANAGED = 0x01, 145300207Sken ADA_ZONE_HOST_AWARE = 0x02, 146300207Sken ADA_ZONE_HOST_MANAGED = 0x03 147300207Sken} ada_zone_mode; 148300207Sken 149300207Skentypedef enum { 150300207Sken ADA_ZONE_FLAG_RZ_SUP = 0x0001, 151300207Sken ADA_ZONE_FLAG_OPEN_SUP = 0x0002, 152300207Sken ADA_ZONE_FLAG_CLOSE_SUP = 0x0004, 153300207Sken ADA_ZONE_FLAG_FINISH_SUP = 0x0008, 154300207Sken ADA_ZONE_FLAG_RWP_SUP = 0x0010, 155300207Sken ADA_ZONE_FLAG_SUP_MASK = (ADA_ZONE_FLAG_RZ_SUP | 156300207Sken ADA_ZONE_FLAG_OPEN_SUP | 157300207Sken ADA_ZONE_FLAG_CLOSE_SUP | 158300207Sken ADA_ZONE_FLAG_FINISH_SUP | 159300207Sken ADA_ZONE_FLAG_RWP_SUP), 160300207Sken ADA_ZONE_FLAG_URSWRZ = 0x0020, 161300207Sken ADA_ZONE_FLAG_OPT_SEQ_SET = 0x0040, 162300207Sken ADA_ZONE_FLAG_OPT_NONSEQ_SET = 0x0080, 163300207Sken ADA_ZONE_FLAG_MAX_SEQ_SET = 0x0100, 164300207Sken ADA_ZONE_FLAG_SET_MASK = (ADA_ZONE_FLAG_OPT_SEQ_SET | 165300207Sken ADA_ZONE_FLAG_OPT_NONSEQ_SET | 166300207Sken ADA_ZONE_FLAG_MAX_SEQ_SET) 167300207Sken} ada_zone_flags; 168300207Sken 169300207Skenstatic struct ada_zone_desc { 170300207Sken ada_zone_flags value; 171300207Sken const char *desc; 172300207Sken} ada_zone_desc_table[] = { 173300207Sken {ADA_ZONE_FLAG_RZ_SUP, "Report Zones" }, 174300207Sken {ADA_ZONE_FLAG_OPEN_SUP, "Open" }, 175300207Sken {ADA_ZONE_FLAG_CLOSE_SUP, "Close" }, 176300207Sken {ADA_ZONE_FLAG_FINISH_SUP, "Finish" }, 177300207Sken {ADA_ZONE_FLAG_RWP_SUP, "Reset Write Pointer" }, 178300207Sken}; 179300207Sken 180300207Sken 181195534Sscottl/* Offsets into our private area for storing information */ 182195534Sscottl#define ccb_state ppriv_field0 183195534Sscottl#define ccb_bp ppriv_ptr1 184195534Sscottl 185298002Simptypedef enum { 186298002Simp ADA_DELETE_NONE, 187298002Simp ADA_DELETE_DISABLE, 188298002Simp ADA_DELETE_CFA_ERASE, 189298002Simp ADA_DELETE_DSM_TRIM, 190298002Simp ADA_DELETE_NCQ_DSM_TRIM, 191298002Simp ADA_DELETE_MIN = ADA_DELETE_CFA_ERASE, 192298002Simp ADA_DELETE_MAX = ADA_DELETE_NCQ_DSM_TRIM, 193298002Simp} ada_delete_methods; 194298002Simp 195298002Simpstatic const char *ada_delete_method_names[] = 196298002Simp { "NONE", "DISABLE", "CFA_ERASE", "DSM_TRIM", "NCQ_DSM_TRIM" }; 197298002Simp#if 0 198298002Simpstatic const char *ada_delete_method_desc[] = 199298002Simp { "NONE", "DISABLED", "CFA Erase", "DSM Trim", "DSM Trim via NCQ" }; 200298002Simp#endif 201298002Simp 202195534Sscottlstruct disk_params { 203195534Sscottl u_int8_t heads; 204198897Smav u_int8_t secs_per_track; 205195534Sscottl u_int32_t cylinders; 206198897Smav u_int32_t secsize; /* Number of bytes/logical sector */ 207198897Smav u_int64_t sectors; /* Total number sectors */ 208195534Sscottl}; 209195534Sscottl 210222643Smav#define TRIM_MAX_BLOCKS 8 211249934Ssmh#define TRIM_MAX_RANGES (TRIM_MAX_BLOCKS * ATA_DSM_BLK_RANGES) 212201139Smavstruct trim_request { 213249934Ssmh uint8_t data[TRIM_MAX_RANGES * ATA_DSM_RANGE_SIZE]; 214256836Smav TAILQ_HEAD(, bio) bps; 215201139Smav}; 216201139Smav 217195534Sscottlstruct ada_softc { 218298002Simp struct cam_iosched_softc *cam_iosched; 219257054Smav int outstanding_cmds; /* Number of active commands */ 220257054Smav int refcount; /* Active xpt_action() calls */ 221195534Sscottl ada_state state; 222257054Smav ada_flags flags; 223300207Sken ada_zone_mode zone_mode; 224300207Sken ada_zone_flags zone_flags; 225300207Sken struct ata_gp_log_dir ata_logdir; 226300207Sken int valid_logdir_len; 227300207Sken struct ata_identify_log_pages ata_iddir; 228300207Sken int valid_iddir_len; 229300207Sken uint64_t optimal_seq_zones; 230300207Sken uint64_t optimal_nonseq_zones; 231300207Sken uint64_t max_seq_zones; 232195534Sscottl ada_quirks quirks; 233298002Simp ada_delete_methods delete_method; 234201139Smav int trim_max_ranges; 235224497Smav int read_ahead; 236220454Smav int write_cache; 237298002Simp int unmappedio; 238298002Simp int rotating; 239220454Smav#ifdef ADA_TEST_FAILURE 240220454Smav int force_read_error; 241220454Smav int force_write_error; 242220454Smav int periodic_read_error; 243220454Smav int periodic_read_count; 244220454Smav#endif 245195534Sscottl struct disk_params params; 246195534Sscottl struct disk *disk; 247195534Sscottl struct task sysctl_task; 248195534Sscottl struct sysctl_ctx_list sysctl_ctx; 249195534Sscottl struct sysctl_oid *sysctl_tree; 250195534Sscottl struct callout sendordered_c; 251201139Smav struct trim_request trim_req; 252298002Simp#ifdef CAM_IO_STATS 253298002Simp struct sysctl_ctx_list sysctl_stats_ctx; 254298002Simp struct sysctl_oid *sysctl_stats_tree; 255298002Simp u_int timeouts; 256298002Simp u_int errors; 257298002Simp u_int invalidations; 258298002Simp#endif 259195534Sscottl}; 260195534Sscottl 261195534Sscottlstruct ada_quirk_entry { 262195534Sscottl struct scsi_inquiry_pattern inq_pat; 263195534Sscottl ada_quirks quirks; 264195534Sscottl}; 265195534Sscottl 266199178Smavstatic struct ada_quirk_entry ada_quirk_table[] = 267199178Smav{ 268199178Smav { 269222520Smav /* Hitachi Advanced Format (4k) drives */ 270222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" }, 271222520Smav /*quirks*/ADA_Q_4K 272222520Smav }, 273222520Smav { 274222520Smav /* Samsung Advanced Format (4k) drives */ 275228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD155UI*", "*" }, 276228819Smav /*quirks*/ADA_Q_4K 277228819Smav }, 278228819Smav { 279228819Smav /* Samsung Advanced Format (4k) drives */ 280222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" }, 281222520Smav /*quirks*/ADA_Q_4K 282222520Smav }, 283222520Smav { 284222520Smav /* Seagate Barracuda Green Advanced Format (4k) drives */ 285222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" }, 286222520Smav /*quirks*/ADA_Q_4K 287222520Smav }, 288222520Smav { 289228819Smav /* Seagate Barracuda Advanced Format (4k) drives */ 290228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???DM*", "*" }, 291228819Smav /*quirks*/ADA_Q_4K 292228819Smav }, 293228819Smav { 294228819Smav /* Seagate Barracuda Advanced Format (4k) drives */ 295228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DM*", "*" }, 296228819Smav /*quirks*/ADA_Q_4K 297228819Smav }, 298228819Smav { 299222520Smav /* Seagate Momentus Advanced Format (4k) drives */ 300222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" }, 301222520Smav /*quirks*/ADA_Q_4K 302222520Smav }, 303222520Smav { 304222520Smav /* Seagate Momentus Advanced Format (4k) drives */ 305222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" }, 306222520Smav /*quirks*/ADA_Q_4K 307222520Smav }, 308222520Smav { 309222520Smav /* Seagate Momentus Advanced Format (4k) drives */ 310228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640423AS*", "*" }, 311228819Smav /*quirks*/ADA_Q_4K 312228819Smav }, 313228819Smav { 314228819Smav /* Seagate Momentus Advanced Format (4k) drives */ 315228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640424AS*", "*" }, 316228819Smav /*quirks*/ADA_Q_4K 317228819Smav }, 318228819Smav { 319228819Smav /* Seagate Momentus Advanced Format (4k) drives */ 320222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" }, 321222520Smav /*quirks*/ADA_Q_4K 322222520Smav }, 323222520Smav { 324222520Smav /* Seagate Momentus Advanced Format (4k) drives */ 325222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" }, 326222520Smav /*quirks*/ADA_Q_4K 327222520Smav }, 328222520Smav { 329228819Smav /* Seagate Momentus Advanced Format (4k) drives */ 330228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750423AS*", "*" }, 331228819Smav /*quirks*/ADA_Q_4K 332228819Smav }, 333228819Smav { 334222520Smav /* Seagate Momentus Thin Advanced Format (4k) drives */ 335222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" }, 336222520Smav /*quirks*/ADA_Q_4K 337222520Smav }, 338222520Smav { 339280845Seadler /* WDC Caviar Red Advanced Format (4k) drives */ 340280845Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????CX*", "*" }, 341280845Seadler /*quirks*/ADA_Q_4K 342280845Seadler }, 343280845Seadler { 344222520Smav /* WDC Caviar Green Advanced Format (4k) drives */ 345222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" }, 346222520Smav /*quirks*/ADA_Q_4K 347222520Smav }, 348222520Smav { 349280845Seadler /* WDC Caviar Green/Red Advanced Format (4k) drives */ 350222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" }, 351222520Smav /*quirks*/ADA_Q_4K 352222520Smav }, 353222520Smav { 354280845Seadler /* WDC Caviar Red Advanced Format (4k) drives */ 355280845Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????CX*", "*" }, 356280845Seadler /*quirks*/ADA_Q_4K 357280845Seadler }, 358280845Seadler { 359280845Seadler /* WDC Caviar Black Advanced Format (4k) drives */ 360280845Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????EX*", "*" }, 361280845Seadler /*quirks*/ADA_Q_4K 362280845Seadler }, 363280845Seadler { 364222520Smav /* WDC Caviar Green Advanced Format (4k) drives */ 365222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" }, 366222520Smav /*quirks*/ADA_Q_4K 367222520Smav }, 368222520Smav { 369222520Smav /* WDC Caviar Green Advanced Format (4k) drives */ 370222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" }, 371222520Smav /*quirks*/ADA_Q_4K 372222520Smav }, 373222520Smav { 374222520Smav /* WDC Scorpio Black Advanced Format (4k) drives */ 375222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" }, 376222520Smav /*quirks*/ADA_Q_4K 377222520Smav }, 378222520Smav { 379222520Smav /* WDC Scorpio Black Advanced Format (4k) drives */ 380222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" }, 381222520Smav /*quirks*/ADA_Q_4K 382222520Smav }, 383222520Smav { 384222520Smav /* WDC Scorpio Blue Advanced Format (4k) drives */ 385222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" }, 386222520Smav /*quirks*/ADA_Q_4K 387222520Smav }, 388222520Smav { 389222520Smav /* WDC Scorpio Blue Advanced Format (4k) drives */ 390222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" }, 391222520Smav /*quirks*/ADA_Q_4K 392222520Smav }, 393251061Ssmh /* SSDs */ 394222520Smav { 395241784Seadler /* 396241784Seadler * Corsair Force 2 SSDs 397241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 398241784Seadler */ 399241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair CSSD-F*", "*" }, 400241784Seadler /*quirks*/ADA_Q_4K 401241784Seadler }, 402241784Seadler { 403241784Seadler /* 404241784Seadler * Corsair Force 3 SSDs 405241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 406241784Seadler */ 407241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force 3*", "*" }, 408241784Seadler /*quirks*/ADA_Q_4K 409241784Seadler }, 410241784Seadler { 411241784Seadler /* 412256547Ssmh * Corsair Neutron GTX SSDs 413256547Ssmh * 4k optimised & trim only works in 4k requests + 4k aligned 414256547Ssmh */ 415256547Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" }, 416256547Ssmh /*quirks*/ADA_Q_4K 417256547Ssmh }, 418256547Ssmh { 419256547Ssmh /* 420269974Ssmh * Corsair Force GT & GS SSDs 421241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 422241784Seadler */ 423269974Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force G*", "*" }, 424241784Seadler /*quirks*/ADA_Q_4K 425241784Seadler }, 426241784Seadler { 427241784Seadler /* 428251061Ssmh * Crucial M4 SSDs 429241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 430241784Seadler */ 431251061Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "M4-CT???M4SSD2*", "*" }, 432241784Seadler /*quirks*/ADA_Q_4K 433241784Seadler }, 434241784Seadler { 435241784Seadler /* 436298027Simp * Crucial M500 SSDs MU07 firmware 437298027Simp * NCQ Trim works 438298002Simp */ 439298027Simp { T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*M500*", "MU07" }, 440298002Simp /*quirks*/0 441298002Simp }, 442298002Simp { 443298002Simp /* 444298002Simp * Crucial M500 SSDs all other firmware 445298002Simp * NCQ Trim doesn't work 446298002Simp */ 447298002Simp { T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*M500*", "*" }, 448298002Simp /*quirks*/ADA_Q_NCQ_TRIM_BROKEN 449298002Simp }, 450298002Simp { 451298002Simp /* 452298002Simp * Crucial M550 SSDs 453298002Simp * NCQ Trim doesn't work, but only on MU01 firmware 454298002Simp */ 455298002Simp { T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*M550*", "MU01" }, 456298002Simp /*quirks*/ADA_Q_NCQ_TRIM_BROKEN 457298002Simp }, 458298002Simp { 459298002Simp /* 460298002Simp * Crucial MX100 SSDs 461298002Simp * NCQ Trim doesn't work, but only on MU01 firmware 462298002Simp */ 463298002Simp { T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*MX100*", "MU01" }, 464298002Simp /*quirks*/ADA_Q_NCQ_TRIM_BROKEN 465298002Simp }, 466298002Simp { 467298002Simp /* 468251061Ssmh * Crucial RealSSD C300 SSDs 469251061Ssmh * 4k optimised 470251061Ssmh */ 471251061Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "C300-CTFDDAC???MAG*", 472251061Ssmh "*" }, /*quirks*/ADA_Q_4K 473251061Ssmh }, 474251061Ssmh { 475251061Ssmh /* 476298027Simp * FCCT M500 SSDs 477298027Simp * NCQ Trim doesn't work 478298027Simp */ 479298027Simp { T_DIRECT, SIP_MEDIA_FIXED, "*", "FCCT*M500*", "*" }, 480298027Simp /*quirks*/ADA_Q_NCQ_TRIM_BROKEN 481298027Simp }, 482298027Simp { 483298027Simp /* 484251061Ssmh * Intel 320 Series SSDs 485241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 486241784Seadler */ 487251061Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2CW*", "*" }, 488241784Seadler /*quirks*/ADA_Q_4K 489241784Seadler }, 490241784Seadler { 491241784Seadler /* 492251061Ssmh * Intel 330 Series SSDs 493241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 494241784Seadler */ 495251061Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2CT*", "*" }, 496241784Seadler /*quirks*/ADA_Q_4K 497241784Seadler }, 498241784Seadler { 499241784Seadler /* 500251061Ssmh * Intel 510 Series SSDs 501251061Ssmh * 4k optimised & trim only works in 4k requests + 4k aligned 502241784Seadler */ 503251061Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2MH*", "*" }, 504251061Ssmh /*quirks*/ADA_Q_4K 505241784Seadler }, 506241784Seadler { 507241784Seadler /* 508251061Ssmh * Intel 520 Series SSDs 509251061Ssmh * 4k optimised & trim only works in 4k requests + 4k aligned 510241784Seadler */ 511251061Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2BW*", "*" }, 512241784Seadler /*quirks*/ADA_Q_4K 513241784Seadler }, 514241784Seadler { 515241784Seadler /* 516331044Seadler * Intel S3610 Series SSDs 517331044Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 518331044Seadler */ 519331044Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2BX*", "*" }, 520331044Seadler /*quirks*/ADA_Q_4K 521331044Seadler }, 522331044Seadler { 523331044Seadler /* 524254329Ssmh * Intel X25-M Series SSDs 525254329Ssmh * 4k optimised & trim only works in 4k requests + 4k aligned 526254329Ssmh */ 527254329Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2M*", "*" }, 528254329Ssmh /*quirks*/ADA_Q_4K 529254329Ssmh }, 530254329Ssmh { 531254329Ssmh /* 532251061Ssmh * Kingston E100 Series SSDs 533250532Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 534250532Seadler */ 535251061Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SE100S3*", "*" }, 536250532Seadler /*quirks*/ADA_Q_4K 537250532Seadler }, 538250532Seadler { 539250532Seadler /* 540251061Ssmh * Kingston HyperX 3k SSDs 541241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 542241784Seadler */ 543251061Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SH103S3*", "*" }, 544241784Seadler /*quirks*/ADA_Q_4K 545241784Seadler }, 546241784Seadler { 547241784Seadler /* 548254329Ssmh * Marvell SSDs (entry taken from OpenSolaris) 549254329Ssmh * 4k optimised & trim only works in 4k requests + 4k aligned 550254329Ssmh */ 551254329Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "MARVELL SD88SA02*", "*" }, 552254329Ssmh /*quirks*/ADA_Q_4K 553254329Ssmh }, 554254329Ssmh { 555254329Ssmh /* 556298027Simp * Micron M500 SSDs firmware MU07 557298002Simp * NCQ Trim works? 558298002Simp */ 559298027Simp { T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron M500*", "MU07" }, 560298002Simp /*quirks*/0 561298002Simp }, 562298002Simp { 563298002Simp /* 564298002Simp * Micron M500 SSDs all other firmware 565298002Simp * NCQ Trim doesn't work 566298002Simp */ 567298002Simp { T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron M500*", "*" }, 568298002Simp /*quirks*/ADA_Q_NCQ_TRIM_BROKEN 569298002Simp }, 570298002Simp { 571298002Simp /* 572298002Simp * Micron M5[15]0 SSDs 573298002Simp * NCQ Trim doesn't work, but only MU01 firmware 574298002Simp */ 575298002Simp { T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron M5[15]0*", "MU01" }, 576298002Simp /*quirks*/ADA_Q_NCQ_TRIM_BROKEN 577298002Simp }, 578298002Simp { 579298002Simp /* 580331044Seadler * Micron 5100 SSDs 581331044Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 582331044Seadler */ 583331044Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron 5100 MTFDDAK*", "*" }, 584331044Seadler /*quirks*/ADA_Q_4K 585331044Seadler }, 586331044Seadler { 587331044Seadler /* 588254329Ssmh * OCZ Agility 2 SSDs 589254329Ssmh * 4k optimised & trim only works in 4k requests + 4k aligned 590254329Ssmh */ 591254329Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" }, 592254329Ssmh /*quirks*/ADA_Q_4K 593254329Ssmh }, 594254329Ssmh { 595254329Ssmh /* 596251061Ssmh * OCZ Agility 3 SSDs 597250532Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 598250532Seadler */ 599251061Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY3*", "*" }, 600250532Seadler /*quirks*/ADA_Q_4K 601250532Seadler }, 602250532Seadler { 603250532Seadler /* 604241784Seadler * OCZ Deneva R Series SSDs 605241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 606241784Seadler */ 607241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "DENRSTE251M45*", "*" }, 608241784Seadler /*quirks*/ADA_Q_4K 609241784Seadler }, 610241784Seadler { 611241784Seadler /* 612251061Ssmh * OCZ Vertex 2 SSDs (inc pro series) 613241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 614241784Seadler */ 615251061Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ?VERTEX2*", "*" }, 616241784Seadler /*quirks*/ADA_Q_4K 617241784Seadler }, 618241784Seadler { 619251061Ssmh /* 620251061Ssmh * OCZ Vertex 3 SSDs 621251061Ssmh * 4k optimised & trim only works in 4k requests + 4k aligned 622251061Ssmh */ 623251061Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX3*", "*" }, 624251061Ssmh /*quirks*/ADA_Q_4K 625251061Ssmh }, 626251061Ssmh { 627251061Ssmh /* 628253091Ssmh * OCZ Vertex 4 SSDs 629253091Ssmh * 4k optimised & trim only works in 4k requests + 4k aligned 630253091Ssmh */ 631253091Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX4*", "*" }, 632253091Ssmh /*quirks*/ADA_Q_4K 633253091Ssmh }, 634253091Ssmh { 635253091Ssmh /* 636331044Seadler * Samsung 750 SSDs 637331044Seadler * 4k optimised, NCQ TRIM seems to work 638331044Seadler */ 639331044Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 750*", "*" }, 640331044Seadler /*quirks*/ADA_Q_4K 641331044Seadler }, 642331044Seadler { 643331044Seadler /* 644251061Ssmh * Samsung 830 Series SSDs 645298002Simp * 4k optimised, NCQ TRIM Broken (normal TRIM is fine) 646251061Ssmh */ 647251061Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG SSD 830 Series*", "*" }, 648298002Simp /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN 649251061Ssmh }, 650251061Ssmh { 651251061Ssmh /* 652269974Ssmh * Samsung 840 SSDs 653298002Simp * 4k optimised, NCQ TRIM Broken (normal TRIM is fine) 654269974Ssmh */ 655269974Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 840*", "*" }, 656298002Simp /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN 657269974Ssmh }, 658269974Ssmh { 659269974Ssmh /* 660331044Seadler * Samsung 845 SSDs 661331044Seadler * 4k optimised, NCQ TRIM Broken (normal TRIM is fine) 662331044Seadler */ 663331044Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 845*", "*" }, 664331044Seadler /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN 665331044Seadler }, 666331044Seadler { 667331044Seadler /* 668273279Sgnn * Samsung 850 SSDs 669298002Simp * 4k optimised, NCQ TRIM broken (normal TRIM fine) 670273279Sgnn */ 671273279Sgnn { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 850*", "*" }, 672298002Simp /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN 673273279Sgnn }, 674270305Ssbruno { 675270305Ssbruno /* 676298035Simp * Samsung SM863 Series SSDs (MZ7KM*) 677298035Simp * 4k optimised, NCQ believed to be working 678298035Simp */ 679298035Simp { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7KM*", "*" }, 680298035Simp /*quirks*/ADA_Q_4K 681298035Simp }, 682298035Simp { 683298035Simp /* 684297370Sdumbbell * Samsung 843T Series SSDs (MZ7WD*) 685297370Sdumbbell * Samsung PM851 Series SSDs (MZ7TE*) 686297370Sdumbbell * Samsung PM853T Series SSDs (MZ7GE*) 687298035Simp * 4k optimised, NCQ believed to be broken since these are 688298035Simp * appear to be built with the same controllers as the 840/850. 689273183Ssbruno */ 690297370Sdumbbell { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7*", "*" }, 691298035Simp /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN 692273183Ssbruno }, 693273183Ssbruno { 694273183Ssbruno /* 695326119Sbapt * Same as for SAMSUNG MZ7* but enable the quirks for SSD 696326119Sbapt * starting with MZ7* too 697326119Sbapt */ 698326119Sbapt { T_DIRECT, SIP_MEDIA_FIXED, "*", "MZ7*", "*" }, 699326119Sbapt /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN 700326119Sbapt }, 701326119Sbapt { 702326119Sbapt /* 703298137Simp * Samsung PM851 Series SSDs Dell OEM 704298137Simp * device model "SAMSUNG SSD PM851 mSATA 256GB" 705298137Simp * 4k optimised, NCQ broken 706298137Simp */ 707298137Simp { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG SSD PM851*", "*" }, 708298137Simp /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN 709298137Simp }, 710298137Simp { 711298137Simp /* 712251061Ssmh * SuperTalent TeraDrive CT SSDs 713251061Ssmh * 4k optimised & trim only works in 4k requests + 4k aligned 714251061Ssmh */ 715251061Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" }, 716251061Ssmh /*quirks*/ADA_Q_4K 717251061Ssmh }, 718251061Ssmh { 719251061Ssmh /* 720251061Ssmh * XceedIOPS SATA SSDs 721251061Ssmh * 4k optimised 722251061Ssmh */ 723251061Ssmh { T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" }, 724251061Ssmh /*quirks*/ADA_Q_4K 725251061Ssmh }, 726251061Ssmh { 727300640Sken /* 728300640Sken * Samsung drive that doesn't support READ LOG EXT or 729300640Sken * READ LOG DMA EXT, despite reporting that it does in 730300640Sken * ATA identify data: 731300640Sken * SAMSUNG HD200HJ KF100-06 732300640Sken */ 733300640Sken { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD200*", "*" }, 734300640Sken /*quirks*/ADA_Q_LOG_BROKEN 735300640Sken }, 736300640Sken { 737300640Sken /* 738300640Sken * Samsung drive that doesn't support READ LOG EXT or 739300640Sken * READ LOG DMA EXT, despite reporting that it does in 740300640Sken * ATA identify data: 741300640Sken * SAMSUNG HD501LJ CR100-10 742300640Sken */ 743300640Sken { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD501*", "*" }, 744300640Sken /*quirks*/ADA_Q_LOG_BROKEN 745300640Sken }, 746300640Sken { 747300640Sken /* 748300640Sken * Seagate Lamarr 8TB Shingled Magnetic Recording (SMR) 749300640Sken * Drive Managed SATA hard drive. This drive doesn't report 750300640Sken * in firmware that it is a drive managed SMR drive. 751300640Sken */ 752326825Sasomers { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST8000AS000[23]*", "*" }, 753300640Sken /*quirks*/ADA_Q_SMR_DM 754300640Sken }, 755300640Sken { 756199178Smav /* Default */ 757199178Smav { 758199178Smav T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, 759199178Smav /*vendor*/"*", /*product*/"*", /*revision*/"*" 760199178Smav }, 761199178Smav /*quirks*/0 762199178Smav }, 763199178Smav}; 764195534Sscottl 765195534Sscottlstatic disk_strategy_t adastrategy; 766195534Sscottlstatic dumper_t adadump; 767195534Sscottlstatic periph_init_t adainit; 768300207Skenstatic void adadiskgonecb(struct disk *dp); 769300207Skenstatic periph_oninv_t adaoninvalidate; 770300207Skenstatic periph_dtor_t adacleanup; 771195534Sscottlstatic void adaasync(void *callback_arg, u_int32_t code, 772195534Sscottl struct cam_path *path, void *arg); 773300207Skenstatic int adazonemodesysctl(SYSCTL_HANDLER_ARGS); 774300207Skenstatic int adazonesupsysctl(SYSCTL_HANDLER_ARGS); 775195534Sscottlstatic void adasysctlinit(void *context, int pending); 776300207Skenstatic int adagetattr(struct bio *bp); 777300207Skenstatic void adasetflags(struct ada_softc *softc, 778300207Sken struct ccb_getdev *cgd); 779195534Sscottlstatic periph_ctor_t adaregister; 780300207Skenstatic void ada_dsmtrim(struct ada_softc *softc, struct bio *bp, 781300207Sken struct ccb_ataio *ataio); 782300207Skenstatic void ada_cfaerase(struct ada_softc *softc, struct bio *bp, 783300207Sken struct ccb_ataio *ataio); 784300207Skenstatic int ada_zone_bio_to_ata(int disk_zone_cmd); 785300207Skenstatic int ada_zone_cmd(struct cam_periph *periph, union ccb *ccb, 786300207Sken struct bio *bp, int *queue_ccb); 787195534Sscottlstatic periph_start_t adastart; 788300207Skenstatic void adaprobedone(struct cam_periph *periph, union ccb *ccb); 789300207Skenstatic void adazonedone(struct cam_periph *periph, union ccb *ccb); 790195534Sscottlstatic void adadone(struct cam_periph *periph, 791195534Sscottl union ccb *done_ccb); 792195534Sscottlstatic int adaerror(union ccb *ccb, u_int32_t cam_flags, 793195534Sscottl u_int32_t sense_flags); 794198897Smavstatic void adagetparams(struct cam_periph *periph, 795195534Sscottl struct ccb_getdev *cgd); 796195534Sscottlstatic timeout_t adasendorderedtag; 797195534Sscottlstatic void adashutdown(void *arg, int howto); 798220650Smavstatic void adasuspend(void *arg); 799220650Smavstatic void adaresume(void *arg); 800195534Sscottl 801221071Smav#ifndef ADA_DEFAULT_LEGACY_ALIASES 802221071Smav#define ADA_DEFAULT_LEGACY_ALIASES 1 803221071Smav#endif 804221071Smav 805195534Sscottl#ifndef ADA_DEFAULT_TIMEOUT 806195534Sscottl#define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */ 807195534Sscottl#endif 808195534Sscottl 809195534Sscottl#ifndef ADA_DEFAULT_RETRY 810195534Sscottl#define ADA_DEFAULT_RETRY 4 811195534Sscottl#endif 812195534Sscottl 813195534Sscottl#ifndef ADA_DEFAULT_SEND_ORDERED 814195534Sscottl#define ADA_DEFAULT_SEND_ORDERED 1 815195534Sscottl#endif 816195534Sscottl 817214279Sbrucec#ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN 818214279Sbrucec#define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1 819214279Sbrucec#endif 820214279Sbrucec 821220650Smav#ifndef ADA_DEFAULT_SPINDOWN_SUSPEND 822220650Smav#define ADA_DEFAULT_SPINDOWN_SUSPEND 1 823220650Smav#endif 824220650Smav 825224497Smav#ifndef ADA_DEFAULT_READ_AHEAD 826224497Smav#define ADA_DEFAULT_READ_AHEAD 1 827224497Smav#endif 828224497Smav 829220412Smav#ifndef ADA_DEFAULT_WRITE_CACHE 830220412Smav#define ADA_DEFAULT_WRITE_CACHE 1 831220412Smav#endif 832220412Smav 833224497Smav#define ADA_RA (softc->read_ahead >= 0 ? \ 834224497Smav softc->read_ahead : ada_read_ahead) 835224497Smav#define ADA_WC (softc->write_cache >= 0 ? \ 836224497Smav softc->write_cache : ada_write_cache) 837224497Smav 838208349Smarius/* 839208349Smarius * Most platforms map firmware geometry to actual, but some don't. If 840208349Smarius * not overridden, default to nothing. 841208349Smarius */ 842208349Smarius#ifndef ata_disk_firmware_geom_adjust 843208349Smarius#define ata_disk_firmware_geom_adjust(disk) 844208349Smarius#endif 845195534Sscottl 846195534Sscottlstatic int ada_retry_count = ADA_DEFAULT_RETRY; 847195534Sscottlstatic int ada_default_timeout = ADA_DEFAULT_TIMEOUT; 848195534Sscottlstatic int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED; 849214279Sbrucecstatic int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN; 850220650Smavstatic int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND; 851224497Smavstatic int ada_read_ahead = ADA_DEFAULT_READ_AHEAD; 852220412Smavstatic int ada_write_cache = ADA_DEFAULT_WRITE_CACHE; 853195534Sscottl 854227309Sedstatic SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0, 855195534Sscottl "CAM Direct Access Disk driver"); 856267992ShselaskySYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RWTUN, 857195534Sscottl &ada_retry_count, 0, "Normal I/O retry count"); 858267992ShselaskySYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RWTUN, 859195534Sscottl &ada_default_timeout, 0, "Normal I/O timeout (in seconds)"); 860267992ShselaskySYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RWTUN, 861195534Sscottl &ada_send_ordered, 0, "Send Ordered Tags"); 862267992ShselaskySYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RWTUN, 863214279Sbrucec &ada_spindown_shutdown, 0, "Spin down upon shutdown"); 864267992ShselaskySYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RWTUN, 865220650Smav &ada_spindown_suspend, 0, "Spin down upon suspend"); 866267992ShselaskySYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RWTUN, 867224497Smav &ada_read_ahead, 0, "Enable disk read-ahead"); 868267992ShselaskySYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RWTUN, 869220412Smav &ada_write_cache, 0, "Enable disk write cache"); 870195534Sscottl 871195534Sscottl/* 872195534Sscottl * ADA_ORDEREDTAG_INTERVAL determines how often, relative 873195534Sscottl * to the default timeout, we check to see whether an ordered 874195534Sscottl * tagged transaction is appropriate to prevent simple tag 875195534Sscottl * starvation. Since we'd like to ensure that there is at least 876195534Sscottl * 1/2 of the timeout length left for a starved transaction to 877195534Sscottl * complete after we've sent an ordered tag, we must poll at least 878195534Sscottl * four times in every timeout period. This takes care of the worst 879195534Sscottl * case where a starved transaction starts during an interval that 880195534Sscottl * meets the requirement "don't send an ordered tag" test so it takes 881195534Sscottl * us two intervals to determine that a tag must be sent. 882195534Sscottl */ 883195534Sscottl#ifndef ADA_ORDEREDTAG_INTERVAL 884195534Sscottl#define ADA_ORDEREDTAG_INTERVAL 4 885195534Sscottl#endif 886195534Sscottl 887195534Sscottlstatic struct periph_driver adadriver = 888195534Sscottl{ 889195534Sscottl adainit, "ada", 890195534Sscottl TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0 891195534Sscottl}; 892195534Sscottl 893298002Simpstatic int adadeletemethodsysctl(SYSCTL_HANDLER_ARGS); 894298002Simp 895195534SscottlPERIPHDRIVER_DECLARE(ada, adadriver); 896195534Sscottl 897300207Skenstatic MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers"); 898300207Sken 899195534Sscottlstatic int 900195534Sscottladaopen(struct disk *dp) 901195534Sscottl{ 902195534Sscottl struct cam_periph *periph; 903195534Sscottl struct ada_softc *softc; 904195534Sscottl int error; 905195534Sscottl 906195534Sscottl periph = (struct cam_periph *)dp->d_drv1; 907195534Sscottl if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 908195534Sscottl return(ENXIO); 909195534Sscottl } 910195534Sscottl 911195534Sscottl cam_periph_lock(periph); 912195534Sscottl if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) { 913195534Sscottl cam_periph_unlock(periph); 914195534Sscottl cam_periph_release(periph); 915195534Sscottl return (error); 916195534Sscottl } 917195534Sscottl 918236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 919236602Smav ("adaopen\n")); 920195534Sscottl 921249981Smav softc = (struct ada_softc *)periph->softc; 922249981Smav softc->flags |= ADA_FLAG_OPEN; 923195534Sscottl 924195534Sscottl cam_periph_unhold(periph); 925195534Sscottl cam_periph_unlock(periph); 926195534Sscottl return (0); 927195534Sscottl} 928195534Sscottl 929195534Sscottlstatic int 930195534Sscottladaclose(struct disk *dp) 931195534Sscottl{ 932195534Sscottl struct cam_periph *periph; 933195534Sscottl struct ada_softc *softc; 934195534Sscottl union ccb *ccb; 935253724Smav int error; 936195534Sscottl 937195534Sscottl periph = (struct cam_periph *)dp->d_drv1; 938256843Smav softc = (struct ada_softc *)periph->softc; 939195534Sscottl cam_periph_lock(periph); 940195534Sscottl 941236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 942236602Smav ("adaclose\n")); 943236602Smav 944195534Sscottl /* We only sync the cache if the drive is capable of it. */ 945253724Smav if ((softc->flags & ADA_FLAG_DIRTY) != 0 && 946253724Smav (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 && 947256843Smav (periph->flags & CAM_PERIPH_INVALID) == 0 && 948256843Smav cam_periph_hold(periph, PRIBIO) == 0) { 949195534Sscottl 950198382Smav ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 951195534Sscottl cam_fill_ataio(&ccb->ataio, 952195534Sscottl 1, 953195534Sscottl adadone, 954195534Sscottl CAM_DIR_NONE, 955195534Sscottl 0, 956195534Sscottl NULL, 957195534Sscottl 0, 958195534Sscottl ada_default_timeout*1000); 959195534Sscottl 960195534Sscottl if (softc->flags & ADA_FLAG_CAN_48BIT) 961195534Sscottl ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 962195534Sscottl else 963196659Smav ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 964253724Smav error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 965198328Smav /*sense_flags*/0, softc->disk->d_devstat); 966195534Sscottl 967253724Smav if (error != 0) 968195534Sscottl xpt_print(periph->path, "Synchronize cache failed\n"); 969308080Smav softc->flags &= ~ADA_FLAG_DIRTY; 970195534Sscottl xpt_release_ccb(ccb); 971256843Smav cam_periph_unhold(periph); 972195534Sscottl } 973195534Sscottl 974195534Sscottl softc->flags &= ~ADA_FLAG_OPEN; 975256843Smav 976256843Smav while (softc->refcount != 0) 977256843Smav cam_periph_sleep(periph, &softc->refcount, PRIBIO, "adaclose", 1); 978195534Sscottl cam_periph_unlock(periph); 979195534Sscottl cam_periph_release(periph); 980195534Sscottl return (0); 981195534Sscottl} 982195534Sscottl 983201139Smavstatic void 984201139Smavadaschedule(struct cam_periph *periph) 985201139Smav{ 986201139Smav struct ada_softc *softc = (struct ada_softc *)periph->softc; 987201139Smav 988249205Smav if (softc->state != ADA_STATE_NORMAL) 989249205Smav return; 990249205Smav 991298002Simp cam_iosched_schedule(softc->cam_iosched, periph); 992201139Smav} 993201139Smav 994195534Sscottl/* 995195534Sscottl * Actually translate the requested transfer into one the physical driver 996195534Sscottl * can understand. The transfer is described by a buf and will include 997195534Sscottl * only one physical transfer. 998195534Sscottl */ 999195534Sscottlstatic void 1000195534Sscottladastrategy(struct bio *bp) 1001195534Sscottl{ 1002195534Sscottl struct cam_periph *periph; 1003195534Sscottl struct ada_softc *softc; 1004195534Sscottl 1005195534Sscottl periph = (struct cam_periph *)bp->bio_disk->d_drv1; 1006195534Sscottl softc = (struct ada_softc *)periph->softc; 1007195534Sscottl 1008195534Sscottl cam_periph_lock(periph); 1009195534Sscottl 1010236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp)); 1011236602Smav 1012195534Sscottl /* 1013195534Sscottl * If the device has been made invalid, error out 1014195534Sscottl */ 1015249981Smav if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 1016195534Sscottl cam_periph_unlock(periph); 1017195534Sscottl biofinish(bp, NULL, ENXIO); 1018195534Sscottl return; 1019195534Sscottl } 1020300207Sken 1021300207Sken /* 1022300207Sken * Zone commands must be ordered, because they can depend on the 1023300207Sken * effects of previously issued commands, and they may affect 1024300207Sken * commands after them. 1025300207Sken */ 1026300207Sken if (bp->bio_cmd == BIO_ZONE) 1027300207Sken bp->bio_flags |= BIO_ORDERED; 1028195534Sscottl 1029195534Sscottl /* 1030195534Sscottl * Place it in the queue of disk activities for this disk 1031195534Sscottl */ 1032298002Simp cam_iosched_queue_work(softc->cam_iosched, bp); 1033195534Sscottl 1034195534Sscottl /* 1035195534Sscottl * Schedule ourselves for performing the work. 1036195534Sscottl */ 1037201139Smav adaschedule(periph); 1038195534Sscottl cam_periph_unlock(periph); 1039195534Sscottl 1040195534Sscottl return; 1041195534Sscottl} 1042195534Sscottl 1043195534Sscottlstatic int 1044195534Sscottladadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) 1045195534Sscottl{ 1046195534Sscottl struct cam_periph *periph; 1047195534Sscottl struct ada_softc *softc; 1048195534Sscottl u_int secsize; 1049195534Sscottl union ccb ccb; 1050195534Sscottl struct disk *dp; 1051195534Sscottl uint64_t lba; 1052195534Sscottl uint16_t count; 1053236814Smav int error = 0; 1054195534Sscottl 1055195534Sscottl dp = arg; 1056195534Sscottl periph = dp->d_drv1; 1057195534Sscottl softc = (struct ada_softc *)periph->softc; 1058195534Sscottl cam_periph_lock(periph); 1059195534Sscottl secsize = softc->params.secsize; 1060195534Sscottl lba = offset / secsize; 1061195534Sscottl count = length / secsize; 1062195534Sscottl 1063249981Smav if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 1064195534Sscottl cam_periph_unlock(periph); 1065195534Sscottl return (ENXIO); 1066195534Sscottl } 1067195534Sscottl 1068195534Sscottl if (length > 0) { 1069198382Smav xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1070195534Sscottl ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 1071195534Sscottl cam_fill_ataio(&ccb.ataio, 1072195534Sscottl 0, 1073195534Sscottl adadone, 1074195534Sscottl CAM_DIR_OUT, 1075195534Sscottl 0, 1076195534Sscottl (u_int8_t *) virtual, 1077195534Sscottl length, 1078195534Sscottl ada_default_timeout*1000); 1079195534Sscottl if ((softc->flags & ADA_FLAG_CAN_48BIT) && 1080195534Sscottl (lba + count >= ATA_MAX_28BIT_LBA || 1081195534Sscottl count >= 256)) { 1082195534Sscottl ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48, 1083195534Sscottl 0, lba, count); 1084195534Sscottl } else { 1085196659Smav ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA, 1086195534Sscottl 0, lba, count); 1087195534Sscottl } 1088195534Sscottl xpt_polled_action(&ccb); 1089195534Sscottl 1090236814Smav error = cam_periph_error(&ccb, 1091236814Smav 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 1092236814Smav if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 1093236814Smav cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0, 1094236814Smav /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 1095236814Smav if (error != 0) 1096195534Sscottl printf("Aborting dump due to I/O error.\n"); 1097236814Smav 1098195534Sscottl cam_periph_unlock(periph); 1099236814Smav return (error); 1100195534Sscottl } 1101195534Sscottl 1102195534Sscottl if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) { 1103198382Smav xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1104195534Sscottl 1105298011Simp /* 1106298023Sngie * Tell the drive to flush its internal cache. if we 1107298011Simp * can't flush in 5s we have big problems. No need to 1108298011Simp * wait the default 60s to detect problems. 1109298011Simp */ 1110195534Sscottl ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 1111195534Sscottl cam_fill_ataio(&ccb.ataio, 1112236814Smav 0, 1113195534Sscottl adadone, 1114195534Sscottl CAM_DIR_NONE, 1115195534Sscottl 0, 1116195534Sscottl NULL, 1117195534Sscottl 0, 1118298002Simp 5*1000); 1119195534Sscottl 1120195534Sscottl if (softc->flags & ADA_FLAG_CAN_48BIT) 1121195534Sscottl ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1122195534Sscottl else 1123196659Smav ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0); 1124195534Sscottl xpt_polled_action(&ccb); 1125195534Sscottl 1126236814Smav error = cam_periph_error(&ccb, 1127236814Smav 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 1128236814Smav if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 1129236814Smav cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0, 1130236814Smav /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 1131236814Smav if (error != 0) 1132195534Sscottl xpt_print(periph->path, "Synchronize cache failed\n"); 1133195534Sscottl } 1134195534Sscottl cam_periph_unlock(periph); 1135236814Smav return (error); 1136195534Sscottl} 1137195534Sscottl 1138195534Sscottlstatic void 1139195534Sscottladainit(void) 1140195534Sscottl{ 1141195534Sscottl cam_status status; 1142195534Sscottl 1143195534Sscottl /* 1144195534Sscottl * Install a global async callback. This callback will 1145195534Sscottl * receive async callbacks like "new device found". 1146195534Sscottl */ 1147195534Sscottl status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL); 1148195534Sscottl 1149195534Sscottl if (status != CAM_REQ_CMP) { 1150195534Sscottl printf("ada: Failed to attach master async callback " 1151195534Sscottl "due to status 0x%x!\n", status); 1152195534Sscottl } else if (ada_send_ordered) { 1153195534Sscottl 1154220650Smav /* Register our event handlers */ 1155220650Smav if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend, 1156220650Smav NULL, EVENTHANDLER_PRI_LAST)) == NULL) 1157220650Smav printf("adainit: power event registration failed!\n"); 1158220650Smav if ((EVENTHANDLER_REGISTER(power_resume, adaresume, 1159220650Smav NULL, EVENTHANDLER_PRI_LAST)) == NULL) 1160220650Smav printf("adainit: power event registration failed!\n"); 1161220650Smav if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown, 1162195534Sscottl NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 1163195534Sscottl printf("adainit: shutdown event registration failed!\n"); 1164195534Sscottl } 1165195534Sscottl} 1166195534Sscottl 1167249347Sken/* 1168249347Sken * Callback from GEOM, called when it has finished cleaning up its 1169249347Sken * resources. 1170249347Sken */ 1171195534Sscottlstatic void 1172249347Skenadadiskgonecb(struct disk *dp) 1173249347Sken{ 1174249347Sken struct cam_periph *periph; 1175249347Sken 1176249347Sken periph = (struct cam_periph *)dp->d_drv1; 1177249347Sken 1178249347Sken cam_periph_release(periph); 1179249347Sken} 1180249347Sken 1181249347Skenstatic void 1182195534Sscottladaoninvalidate(struct cam_periph *periph) 1183195534Sscottl{ 1184195534Sscottl struct ada_softc *softc; 1185195534Sscottl 1186195534Sscottl softc = (struct ada_softc *)periph->softc; 1187195534Sscottl 1188195534Sscottl /* 1189195534Sscottl * De-register any async callbacks. 1190195534Sscottl */ 1191195534Sscottl xpt_register_async(0, adaasync, periph, periph->path); 1192298002Simp#ifdef CAM_IO_STATS 1193298002Simp softc->invalidations++; 1194298002Simp#endif 1195195534Sscottl 1196195534Sscottl /* 1197195534Sscottl * Return all queued I/O with ENXIO. 1198195534Sscottl * XXX Handle any transactions queued to the card 1199195534Sscottl * with XPT_ABORT_CCB. 1200195534Sscottl */ 1201298002Simp cam_iosched_flush(softc->cam_iosched, NULL, ENXIO); 1202195534Sscottl 1203195534Sscottl disk_gone(softc->disk); 1204195534Sscottl} 1205195534Sscottl 1206195534Sscottlstatic void 1207195534Sscottladacleanup(struct cam_periph *periph) 1208195534Sscottl{ 1209195534Sscottl struct ada_softc *softc; 1210195534Sscottl 1211195534Sscottl softc = (struct ada_softc *)periph->softc; 1212195534Sscottl 1213195534Sscottl cam_periph_unlock(periph); 1214195534Sscottl 1215298002Simp cam_iosched_fini(softc->cam_iosched); 1216298002Simp 1217195534Sscottl /* 1218195534Sscottl * If we can't free the sysctl tree, oh well... 1219195534Sscottl */ 1220298002Simp if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0) { 1221298002Simp#ifdef CAM_IO_STATS 1222298002Simp if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0) 1223298002Simp xpt_print(periph->path, 1224298002Simp "can't remove sysctl stats context\n"); 1225298002Simp#endif 1226298002Simp if (sysctl_ctx_free(&softc->sysctl_ctx) != 0) 1227298002Simp xpt_print(periph->path, 1228298002Simp "can't remove sysctl context\n"); 1229195534Sscottl } 1230195534Sscottl 1231195534Sscottl disk_destroy(softc->disk); 1232195534Sscottl callout_drain(&softc->sendordered_c); 1233195534Sscottl free(softc, M_DEVBUF); 1234195534Sscottl cam_periph_lock(periph); 1235195534Sscottl} 1236195534Sscottl 1237195534Sscottlstatic void 1238298002Simpadasetdeletemethod(struct ada_softc *softc) 1239298002Simp{ 1240298002Simp 1241298002Simp if (softc->flags & ADA_FLAG_CAN_NCQ_TRIM) 1242298002Simp softc->delete_method = ADA_DELETE_NCQ_DSM_TRIM; 1243298002Simp else if (softc->flags & ADA_FLAG_CAN_TRIM) 1244298002Simp softc->delete_method = ADA_DELETE_DSM_TRIM; 1245298002Simp else if ((softc->flags & ADA_FLAG_CAN_CFA) && !(softc->flags & ADA_FLAG_CAN_48BIT)) 1246298002Simp softc->delete_method = ADA_DELETE_CFA_ERASE; 1247298002Simp else 1248298002Simp softc->delete_method = ADA_DELETE_NONE; 1249298002Simp} 1250298002Simp 1251298002Simpstatic void 1252195534Sscottladaasync(void *callback_arg, u_int32_t code, 1253195534Sscottl struct cam_path *path, void *arg) 1254195534Sscottl{ 1255236393Smav struct ccb_getdev cgd; 1256195534Sscottl struct cam_periph *periph; 1257220412Smav struct ada_softc *softc; 1258195534Sscottl 1259195534Sscottl periph = (struct cam_periph *)callback_arg; 1260195534Sscottl switch (code) { 1261195534Sscottl case AC_FOUND_DEVICE: 1262195534Sscottl { 1263195534Sscottl struct ccb_getdev *cgd; 1264195534Sscottl cam_status status; 1265195534Sscottl 1266195534Sscottl cgd = (struct ccb_getdev *)arg; 1267195534Sscottl if (cgd == NULL) 1268195534Sscottl break; 1269195534Sscottl 1270195534Sscottl if (cgd->protocol != PROTO_ATA) 1271195534Sscottl break; 1272195534Sscottl 1273195534Sscottl /* 1274195534Sscottl * Allocate a peripheral instance for 1275195534Sscottl * this device and start the probe 1276195534Sscottl * process. 1277195534Sscottl */ 1278195534Sscottl status = cam_periph_alloc(adaregister, adaoninvalidate, 1279195534Sscottl adacleanup, adastart, 1280195534Sscottl "ada", CAM_PERIPH_BIO, 1281256843Smav path, adaasync, 1282195534Sscottl AC_FOUND_DEVICE, cgd); 1283195534Sscottl 1284195534Sscottl if (status != CAM_REQ_CMP 1285195534Sscottl && status != CAM_REQ_INPROG) 1286195534Sscottl printf("adaasync: Unable to attach to new device " 1287195534Sscottl "due to status 0x%x\n", status); 1288195534Sscottl break; 1289195534Sscottl } 1290236393Smav case AC_GETDEV_CHANGED: 1291236393Smav { 1292236393Smav softc = (struct ada_softc *)periph->softc; 1293236393Smav xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1294236393Smav cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1295236393Smav xpt_action((union ccb *)&cgd); 1296236393Smav 1297300207Sken /* 1298300207Sken * Set/clear support flags based on the new Identify data. 1299300207Sken */ 1300300207Sken adasetflags(softc, &cgd); 1301298002Simp 1302236393Smav cam_periph_async(periph, code, path, arg); 1303236393Smav break; 1304236393Smav } 1305235897Smav case AC_ADVINFO_CHANGED: 1306235897Smav { 1307235897Smav uintptr_t buftype; 1308235897Smav 1309235897Smav buftype = (uintptr_t)arg; 1310235897Smav if (buftype == CDAI_TYPE_PHYS_PATH) { 1311235897Smav struct ada_softc *softc; 1312235897Smav 1313235897Smav softc = periph->softc; 1314235897Smav disk_attr_changed(softc->disk, "GEOM::physpath", 1315235897Smav M_NOWAIT); 1316235897Smav } 1317235897Smav break; 1318235897Smav } 1319220412Smav case AC_SENT_BDR: 1320220412Smav case AC_BUS_RESET: 1321220412Smav { 1322220412Smav softc = (struct ada_softc *)periph->softc; 1323220412Smav cam_periph_async(periph, code, path, arg); 1324220412Smav if (softc->state != ADA_STATE_NORMAL) 1325220412Smav break; 1326220454Smav xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1327220412Smav cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1328220412Smav xpt_action((union ccb *)&cgd); 1329300207Sken if (ADA_RA >= 0 && softc->flags & ADA_FLAG_CAN_RAHEAD) 1330224497Smav softc->state = ADA_STATE_RAHEAD; 1331321976Smav else if (ADA_WC >= 0 && softc->flags & ADA_FLAG_CAN_WCACHE) 1332224497Smav softc->state = ADA_STATE_WCACHE; 1333300640Sken else if ((softc->flags & ADA_FLAG_CAN_LOG) 1334300640Sken && (softc->zone_mode != ADA_ZONE_NONE)) 1335300207Sken softc->state = ADA_STATE_LOGDIR; 1336224497Smav else 1337224497Smav break; 1338256843Smav if (cam_periph_acquire(periph) != CAM_REQ_CMP) 1339256843Smav softc->state = ADA_STATE_NORMAL; 1340256843Smav else 1341256843Smav xpt_schedule(periph, CAM_PRIORITY_DEV); 1342220412Smav } 1343195534Sscottl default: 1344195534Sscottl cam_periph_async(periph, code, path, arg); 1345195534Sscottl break; 1346195534Sscottl } 1347195534Sscottl} 1348195534Sscottl 1349300207Skenstatic int 1350300207Skenadazonemodesysctl(SYSCTL_HANDLER_ARGS) 1351300207Sken{ 1352300207Sken char tmpbuf[40]; 1353300207Sken struct ada_softc *softc; 1354300207Sken int error; 1355300207Sken 1356300207Sken softc = (struct ada_softc *)arg1; 1357300207Sken 1358300207Sken switch (softc->zone_mode) { 1359300207Sken case ADA_ZONE_DRIVE_MANAGED: 1360300207Sken snprintf(tmpbuf, sizeof(tmpbuf), "Drive Managed"); 1361300207Sken break; 1362300207Sken case ADA_ZONE_HOST_AWARE: 1363300207Sken snprintf(tmpbuf, sizeof(tmpbuf), "Host Aware"); 1364300207Sken break; 1365300207Sken case ADA_ZONE_HOST_MANAGED: 1366300207Sken snprintf(tmpbuf, sizeof(tmpbuf), "Host Managed"); 1367300207Sken break; 1368300207Sken case ADA_ZONE_NONE: 1369300207Sken default: 1370300207Sken snprintf(tmpbuf, sizeof(tmpbuf), "Not Zoned"); 1371300207Sken break; 1372300207Sken } 1373300207Sken 1374300207Sken error = sysctl_handle_string(oidp, tmpbuf, sizeof(tmpbuf), req); 1375300207Sken 1376300207Sken return (error); 1377300207Sken} 1378300207Sken 1379300207Skenstatic int 1380300207Skenadazonesupsysctl(SYSCTL_HANDLER_ARGS) 1381300207Sken{ 1382300207Sken char tmpbuf[180]; 1383300207Sken struct ada_softc *softc; 1384300207Sken struct sbuf sb; 1385300207Sken int error, first; 1386300207Sken unsigned int i; 1387300207Sken 1388300207Sken softc = (struct ada_softc *)arg1; 1389300207Sken 1390300207Sken error = 0; 1391300207Sken first = 1; 1392300207Sken sbuf_new(&sb, tmpbuf, sizeof(tmpbuf), 0); 1393300207Sken 1394300207Sken for (i = 0; i < sizeof(ada_zone_desc_table) / 1395300207Sken sizeof(ada_zone_desc_table[0]); i++) { 1396300207Sken if (softc->zone_flags & ada_zone_desc_table[i].value) { 1397300207Sken if (first == 0) 1398300207Sken sbuf_printf(&sb, ", "); 1399300207Sken else 1400300207Sken first = 0; 1401300207Sken sbuf_cat(&sb, ada_zone_desc_table[i].desc); 1402300207Sken } 1403300207Sken } 1404300207Sken 1405300207Sken if (first == 1) 1406300207Sken sbuf_printf(&sb, "None"); 1407300207Sken 1408300207Sken sbuf_finish(&sb); 1409300207Sken 1410300207Sken error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 1411300207Sken 1412300207Sken return (error); 1413300207Sken} 1414300207Sken 1415300207Sken 1416195534Sscottlstatic void 1417195534Sscottladasysctlinit(void *context, int pending) 1418195534Sscottl{ 1419195534Sscottl struct cam_periph *periph; 1420195534Sscottl struct ada_softc *softc; 1421327228Smav char tmpstr[32], tmpstr2[16]; 1422195534Sscottl 1423195534Sscottl periph = (struct cam_periph *)context; 1424220454Smav 1425220454Smav /* periph was held for us when this task was enqueued */ 1426249981Smav if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 1427220454Smav cam_periph_release(periph); 1428195534Sscottl return; 1429220454Smav } 1430195534Sscottl 1431195534Sscottl softc = (struct ada_softc *)periph->softc; 1432300207Sken snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d",periph->unit_number); 1433195534Sscottl snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 1434195534Sscottl 1435195534Sscottl sysctl_ctx_init(&softc->sysctl_ctx); 1436195534Sscottl softc->flags |= ADA_FLAG_SCTX_INIT; 1437195534Sscottl softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1438195534Sscottl SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2, 1439195534Sscottl CTLFLAG_RD, 0, tmpstr); 1440195534Sscottl if (softc->sysctl_tree == NULL) { 1441195534Sscottl printf("adasysctlinit: unable to allocate sysctl tree\n"); 1442195534Sscottl cam_periph_release(periph); 1443195534Sscottl return; 1444195534Sscottl } 1445195534Sscottl 1446298002Simp SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1447298002Simp OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW, 1448298002Simp softc, 0, adadeletemethodsysctl, "A", 1449298002Simp "BIO_DELETE execution method"); 1450220454Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1451224497Smav OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE, 1452224497Smav &softc->read_ahead, 0, "Enable disk read ahead."); 1453224497Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1454220454Smav OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE, 1455220454Smav &softc->write_cache, 0, "Enable disk write cache."); 1456248922Ssmh SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1457298002Simp OID_AUTO, "unmapped_io", CTLFLAG_RD | CTLFLAG_MPSAFE, 1458298002Simp &softc->unmappedio, 0, "Unmapped I/O leaf"); 1459298002Simp SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1460298002Simp OID_AUTO, "rotating", CTLFLAG_RD | CTLFLAG_MPSAFE, 1461298002Simp &softc->rotating, 0, "Rotating media"); 1462300207Sken SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1463300207Sken OID_AUTO, "zone_mode", CTLTYPE_STRING | CTLFLAG_RD, 1464300207Sken softc, 0, adazonemodesysctl, "A", 1465300207Sken "Zone Mode"); 1466300207Sken SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1467300207Sken OID_AUTO, "zone_support", CTLTYPE_STRING | CTLFLAG_RD, 1468300207Sken softc, 0, adazonesupsysctl, "A", 1469300207Sken "Zone Support"); 1470300207Sken SYSCTL_ADD_UQUAD(&softc->sysctl_ctx, 1471300207Sken SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1472300207Sken "optimal_seq_zones", CTLFLAG_RD, &softc->optimal_seq_zones, 1473300207Sken "Optimal Number of Open Sequential Write Preferred Zones"); 1474300207Sken SYSCTL_ADD_UQUAD(&softc->sysctl_ctx, 1475300207Sken SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1476300207Sken "optimal_nonseq_zones", CTLFLAG_RD, 1477300207Sken &softc->optimal_nonseq_zones, 1478300207Sken "Optimal Number of Non-Sequentially Written Sequential Write " 1479300207Sken "Preferred Zones"); 1480300207Sken SYSCTL_ADD_UQUAD(&softc->sysctl_ctx, 1481300207Sken SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1482300207Sken "max_seq_zones", CTLFLAG_RD, &softc->max_seq_zones, 1483300207Sken "Maximum Number of Open Sequential Write Required Zones"); 1484300207Sken 1485220454Smav#ifdef ADA_TEST_FAILURE 1486220454Smav /* 1487220454Smav * Add a 'door bell' sysctl which allows one to set it from userland 1488220454Smav * and cause something bad to happen. For the moment, we only allow 1489220454Smav * whacking the next read or write. 1490220454Smav */ 1491220454Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1492220454Smav OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1493220454Smav &softc->force_read_error, 0, 1494220454Smav "Force a read error for the next N reads."); 1495220454Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1496220454Smav OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1497220454Smav &softc->force_write_error, 0, 1498220454Smav "Force a write error for the next N writes."); 1499220454Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1500220454Smav OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1501220454Smav &softc->periodic_read_error, 0, 1502220454Smav "Force a read error every N reads (don't set too low)."); 1503220454Smav#endif 1504298002Simp 1505298002Simp#ifdef CAM_IO_STATS 1506298002Simp softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx, 1507298002Simp SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats", 1508298002Simp CTLFLAG_RD, 0, "Statistics"); 1509298002Simp SYSCTL_ADD_INT(&softc->sysctl_stats_ctx, 1510298002Simp SYSCTL_CHILDREN(softc->sysctl_stats_tree), 1511298002Simp OID_AUTO, "timeouts", CTLFLAG_RD | CTLFLAG_MPSAFE, 1512298002Simp &softc->timeouts, 0, 1513298002Simp "Device timeouts reported by the SIM"); 1514298002Simp SYSCTL_ADD_INT(&softc->sysctl_stats_ctx, 1515298002Simp SYSCTL_CHILDREN(softc->sysctl_stats_tree), 1516298002Simp OID_AUTO, "errors", CTLFLAG_RD | CTLFLAG_MPSAFE, 1517298002Simp &softc->errors, 0, 1518298002Simp "Transport errors reported by the SIM."); 1519298002Simp SYSCTL_ADD_INT(&softc->sysctl_stats_ctx, 1520298002Simp SYSCTL_CHILDREN(softc->sysctl_stats_tree), 1521298002Simp OID_AUTO, "pack_invalidations", CTLFLAG_RD | CTLFLAG_MPSAFE, 1522298002Simp &softc->invalidations, 0, 1523298002Simp "Device pack invalidations."); 1524298002Simp#endif 1525298002Simp 1526298002Simp cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx, 1527298002Simp softc->sysctl_tree); 1528298002Simp 1529195534Sscottl cam_periph_release(periph); 1530195534Sscottl} 1531195534Sscottl 1532223089Sgibbsstatic int 1533223089Sgibbsadagetattr(struct bio *bp) 1534223089Sgibbs{ 1535241485Smav int ret; 1536223089Sgibbs struct cam_periph *periph; 1537223089Sgibbs 1538223089Sgibbs periph = (struct cam_periph *)bp->bio_disk->d_drv1; 1539241485Smav cam_periph_lock(periph); 1540223089Sgibbs ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute, 1541223089Sgibbs periph->path); 1542241485Smav cam_periph_unlock(periph); 1543223089Sgibbs if (ret == 0) 1544223089Sgibbs bp->bio_completed = bp->bio_length; 1545223089Sgibbs return ret; 1546223089Sgibbs} 1547223089Sgibbs 1548298002Simpstatic int 1549298002Simpadadeletemethodsysctl(SYSCTL_HANDLER_ARGS) 1550298002Simp{ 1551298002Simp char buf[16]; 1552298002Simp const char *p; 1553298002Simp struct ada_softc *softc; 1554298002Simp int i, error, value, methods; 1555298002Simp 1556298002Simp softc = (struct ada_softc *)arg1; 1557298002Simp 1558298002Simp value = softc->delete_method; 1559298002Simp if (value < 0 || value > ADA_DELETE_MAX) 1560298002Simp p = "UNKNOWN"; 1561298002Simp else 1562298002Simp p = ada_delete_method_names[value]; 1563298002Simp strncpy(buf, p, sizeof(buf)); 1564298002Simp error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 1565298002Simp if (error != 0 || req->newptr == NULL) 1566298002Simp return (error); 1567298002Simp methods = 1 << ADA_DELETE_DISABLE; 1568298002Simp if ((softc->flags & ADA_FLAG_CAN_CFA) && 1569298002Simp !(softc->flags & ADA_FLAG_CAN_48BIT)) 1570298002Simp methods |= 1 << ADA_DELETE_CFA_ERASE; 1571298002Simp if (softc->flags & ADA_FLAG_CAN_TRIM) 1572298002Simp methods |= 1 << ADA_DELETE_DSM_TRIM; 1573298002Simp if (softc->flags & ADA_FLAG_CAN_NCQ_TRIM) 1574298002Simp methods |= 1 << ADA_DELETE_NCQ_DSM_TRIM; 1575298002Simp for (i = 0; i <= ADA_DELETE_MAX; i++) { 1576298002Simp if (!(methods & (1 << i)) || 1577298002Simp strcmp(buf, ada_delete_method_names[i]) != 0) 1578298002Simp continue; 1579298002Simp softc->delete_method = i; 1580298002Simp return (0); 1581298002Simp } 1582298002Simp return (EINVAL); 1583298002Simp} 1584298002Simp 1585300207Skenstatic void 1586300207Skenadasetflags(struct ada_softc *softc, struct ccb_getdev *cgd) 1587300207Sken{ 1588300207Sken if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) && 1589300207Sken (cgd->inq_flags & SID_DMA)) 1590300207Sken softc->flags |= ADA_FLAG_CAN_DMA; 1591300207Sken else 1592300207Sken softc->flags &= ~ADA_FLAG_CAN_DMA; 1593300207Sken 1594300207Sken if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) { 1595300207Sken softc->flags |= ADA_FLAG_CAN_48BIT; 1596300207Sken if (cgd->inq_flags & SID_DMA48) 1597300207Sken softc->flags |= ADA_FLAG_CAN_DMA48; 1598300207Sken else 1599300207Sken softc->flags &= ~ADA_FLAG_CAN_DMA48; 1600300207Sken } else 1601300207Sken softc->flags &= ~(ADA_FLAG_CAN_48BIT | ADA_FLAG_CAN_DMA48); 1602300207Sken 1603300207Sken if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE) 1604300207Sken softc->flags |= ADA_FLAG_CAN_FLUSHCACHE; 1605300207Sken else 1606300207Sken softc->flags &= ~ADA_FLAG_CAN_FLUSHCACHE; 1607300207Sken 1608300207Sken if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT) 1609300207Sken softc->flags |= ADA_FLAG_CAN_POWERMGT; 1610300207Sken else 1611300207Sken softc->flags &= ~ADA_FLAG_CAN_POWERMGT; 1612300207Sken 1613300207Sken if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) && 1614300207Sken (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue)) 1615300207Sken softc->flags |= ADA_FLAG_CAN_NCQ; 1616300207Sken else 1617300207Sken softc->flags &= ~ADA_FLAG_CAN_NCQ; 1618300207Sken 1619300207Sken if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) && 1620300207Sken (cgd->inq_flags & SID_DMA)) { 1621300207Sken softc->flags |= ADA_FLAG_CAN_TRIM; 1622300207Sken softc->trim_max_ranges = TRIM_MAX_RANGES; 1623300207Sken if (cgd->ident_data.max_dsm_blocks != 0) { 1624300207Sken softc->trim_max_ranges = 1625300207Sken min(cgd->ident_data.max_dsm_blocks * 1626300207Sken ATA_DSM_BLK_RANGES, softc->trim_max_ranges); 1627300207Sken } 1628300207Sken /* 1629300207Sken * If we can do RCVSND_FPDMA_QUEUED commands, we may be able 1630300207Sken * to do NCQ trims, if we support trims at all. We also need 1631300207Sken * support from the SIM to do things properly. Perhaps we 1632300207Sken * should look at log 13 dword 0 bit 0 and dword 1 bit 0 are 1633300207Sken * set too... 1634300207Sken */ 1635300207Sken if ((softc->quirks & ADA_Q_NCQ_TRIM_BROKEN) == 0 && 1636300207Sken (softc->flags & ADA_FLAG_PIM_ATA_EXT) != 0 && 1637300207Sken (cgd->ident_data.satacapabilities2 & 1638300207Sken ATA_SUPPORT_RCVSND_FPDMA_QUEUED) != 0 && 1639300207Sken (softc->flags & ADA_FLAG_CAN_TRIM) != 0) 1640300207Sken softc->flags |= ADA_FLAG_CAN_NCQ_TRIM; 1641300207Sken else 1642300207Sken softc->flags &= ~ADA_FLAG_CAN_NCQ_TRIM; 1643300207Sken } else 1644300207Sken softc->flags &= ~(ADA_FLAG_CAN_TRIM | ADA_FLAG_CAN_NCQ_TRIM); 1645300207Sken 1646300207Sken if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA) 1647300207Sken softc->flags |= ADA_FLAG_CAN_CFA; 1648300207Sken else 1649300207Sken softc->flags &= ~ADA_FLAG_CAN_CFA; 1650300207Sken 1651300207Sken /* 1652300207Sken * Now that we've set the appropriate flags, setup the delete 1653300207Sken * method. 1654300207Sken */ 1655300207Sken adasetdeletemethod(softc); 1656300207Sken 1657300640Sken if ((cgd->ident_data.support.extension & ATA_SUPPORT_GENLOG) 1658300640Sken && ((softc->quirks & ADA_Q_LOG_BROKEN) == 0)) 1659300207Sken softc->flags |= ADA_FLAG_CAN_LOG; 1660300207Sken else 1661300207Sken softc->flags &= ~ADA_FLAG_CAN_LOG; 1662300207Sken 1663300207Sken if ((cgd->ident_data.support3 & ATA_SUPPORT_ZONE_MASK) == 1664300207Sken ATA_SUPPORT_ZONE_HOST_AWARE) 1665300207Sken softc->zone_mode = ADA_ZONE_HOST_AWARE; 1666300640Sken else if (((cgd->ident_data.support3 & ATA_SUPPORT_ZONE_MASK) == 1667300640Sken ATA_SUPPORT_ZONE_DEV_MANAGED) 1668300640Sken || (softc->quirks & ADA_Q_SMR_DM)) 1669300207Sken softc->zone_mode = ADA_ZONE_DRIVE_MANAGED; 1670300207Sken else 1671300207Sken softc->zone_mode = ADA_ZONE_NONE; 1672300207Sken 1673300207Sken if (cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) 1674300207Sken softc->flags |= ADA_FLAG_CAN_RAHEAD; 1675300207Sken else 1676300207Sken softc->flags &= ~ADA_FLAG_CAN_RAHEAD; 1677300207Sken 1678300207Sken if (cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) 1679300207Sken softc->flags |= ADA_FLAG_CAN_WCACHE; 1680300207Sken else 1681300207Sken softc->flags &= ~ADA_FLAG_CAN_WCACHE; 1682300207Sken} 1683300207Sken 1684195534Sscottlstatic cam_status 1685195534Sscottladaregister(struct cam_periph *periph, void *arg) 1686195534Sscottl{ 1687195534Sscottl struct ada_softc *softc; 1688195534Sscottl struct ccb_pathinq cpi; 1689195534Sscottl struct ccb_getdev *cgd; 1690289137Smav char announce_buf[80]; 1691195534Sscottl struct disk_params *dp; 1692195534Sscottl caddr_t match; 1693195534Sscottl u_int maxio; 1694289137Smav int quirks; 1695195534Sscottl 1696195534Sscottl cgd = (struct ccb_getdev *)arg; 1697195534Sscottl if (cgd == NULL) { 1698195534Sscottl printf("adaregister: no getdev CCB, can't register device\n"); 1699195534Sscottl return(CAM_REQ_CMP_ERR); 1700195534Sscottl } 1701195534Sscottl 1702195534Sscottl softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF, 1703195534Sscottl M_NOWAIT|M_ZERO); 1704195534Sscottl 1705195534Sscottl if (softc == NULL) { 1706195534Sscottl printf("adaregister: Unable to probe new device. " 1707198328Smav "Unable to allocate softc\n"); 1708195534Sscottl return(CAM_REQ_CMP_ERR); 1709195534Sscottl } 1710195534Sscottl 1711298002Simp if (cam_iosched_init(&softc->cam_iosched, periph) != 0) { 1712298002Simp printf("adaregister: Unable to probe new device. " 1713298002Simp "Unable to allocate iosched memory\n"); 1714301568Scem free(softc, M_DEVBUF); 1715298002Simp return(CAM_REQ_CMP_ERR); 1716298002Simp } 1717195534Sscottl 1718195534Sscottl periph->softc = softc; 1719195534Sscottl 1720195534Sscottl /* 1721195534Sscottl * See if this device has any quirks. 1722195534Sscottl */ 1723199178Smav match = cam_quirkmatch((caddr_t)&cgd->ident_data, 1724199178Smav (caddr_t)ada_quirk_table, 1725298431Spfg nitems(ada_quirk_table), 1726199178Smav sizeof(*ada_quirk_table), ata_identify_match); 1727195534Sscottl if (match != NULL) 1728195534Sscottl softc->quirks = ((struct ada_quirk_entry *)match)->quirks; 1729195534Sscottl else 1730195534Sscottl softc->quirks = ADA_Q_NONE; 1731195534Sscottl 1732195534Sscottl bzero(&cpi, sizeof(cpi)); 1733203108Smav xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE); 1734195534Sscottl cpi.ccb_h.func_code = XPT_PATH_INQ; 1735195534Sscottl xpt_action((union ccb *)&cpi); 1736195534Sscottl 1737195534Sscottl TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph); 1738195534Sscottl 1739195534Sscottl /* 1740195534Sscottl * Register this media as a disk 1741195534Sscottl */ 1742220618Smav (void)cam_periph_hold(periph, PRIBIO); 1743249106Smav cam_periph_unlock(periph); 1744222520Smav snprintf(announce_buf, sizeof(announce_buf), 1745222520Smav "kern.cam.ada.%d.quirks", periph->unit_number); 1746222520Smav quirks = softc->quirks; 1747222520Smav TUNABLE_INT_FETCH(announce_buf, &quirks); 1748222520Smav softc->quirks = quirks; 1749224497Smav softc->read_ahead = -1; 1750224497Smav snprintf(announce_buf, sizeof(announce_buf), 1751224497Smav "kern.cam.ada.%d.read_ahead", periph->unit_number); 1752224497Smav TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead); 1753220618Smav softc->write_cache = -1; 1754220618Smav snprintf(announce_buf, sizeof(announce_buf), 1755220618Smav "kern.cam.ada.%d.write_cache", periph->unit_number); 1756220618Smav TUNABLE_INT_FETCH(announce_buf, &softc->write_cache); 1757300532Sken 1758300532Sken /* 1759300532Sken * Set support flags based on the Identify data and quirks. 1760300532Sken */ 1761300532Sken adasetflags(softc, cgd); 1762300532Sken 1763250033Ssmh /* Disable queue sorting for non-rotational media by default. */ 1764298002Simp if (cgd->ident_data.media_rotation_rate == ATA_RATE_NON_ROTATING) { 1765298002Simp softc->rotating = 0; 1766298002Simp } else { 1767298002Simp softc->rotating = 1; 1768298002Simp } 1769298002Simp cam_iosched_set_sort_queue(softc->cam_iosched, softc->rotating ? -1 : 0); 1770198897Smav adagetparams(periph, cgd); 1771195534Sscottl softc->disk = disk_alloc(); 1772256956Ssmh softc->disk->d_rotation_rate = cgd->ident_data.media_rotation_rate; 1773220644Smav softc->disk->d_devstat = devstat_new_entry(periph->periph_name, 1774220644Smav periph->unit_number, softc->params.secsize, 1775220644Smav DEVSTAT_ALL_SUPPORTED, 1776220644Smav DEVSTAT_TYPE_DIRECT | 1777220644Smav XPORT_DEVSTAT_TYPE(cpi.transport), 1778220644Smav DEVSTAT_PRIORITY_DISK); 1779195534Sscottl softc->disk->d_open = adaopen; 1780195534Sscottl softc->disk->d_close = adaclose; 1781195534Sscottl softc->disk->d_strategy = adastrategy; 1782223089Sgibbs softc->disk->d_getattr = adagetattr; 1783195534Sscottl softc->disk->d_dump = adadump; 1784249347Sken softc->disk->d_gone = adadiskgonecb; 1785195534Sscottl softc->disk->d_name = "ada"; 1786195534Sscottl softc->disk->d_drv1 = periph; 1787195534Sscottl maxio = cpi.maxio; /* Honor max I/O size of SIM */ 1788195534Sscottl if (maxio == 0) 1789195534Sscottl maxio = DFLTPHYS; /* traditional default */ 1790195534Sscottl else if (maxio > MAXPHYS) 1791195534Sscottl maxio = MAXPHYS; /* for safety */ 1792201139Smav if (softc->flags & ADA_FLAG_CAN_48BIT) 1793198897Smav maxio = min(maxio, 65536 * softc->params.secsize); 1794195534Sscottl else /* 28bit ATA command limit */ 1795198897Smav maxio = min(maxio, 256 * softc->params.secsize); 1796195534Sscottl softc->disk->d_maxsize = maxio; 1797195534Sscottl softc->disk->d_unit = periph->unit_number; 1798300207Sken softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION | DISKFLAG_CANZONE; 1799195534Sscottl if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) 1800195534Sscottl softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE; 1801249934Ssmh if (softc->flags & ADA_FLAG_CAN_TRIM) { 1802201139Smav softc->disk->d_flags |= DISKFLAG_CANDELETE; 1803249940Ssmh softc->disk->d_delmaxsize = softc->params.secsize * 1804249940Ssmh ATA_DSM_RANGE_MAX * 1805249940Ssmh softc->trim_max_ranges; 1806249934Ssmh } else if ((softc->flags & ADA_FLAG_CAN_CFA) && 1807249934Ssmh !(softc->flags & ADA_FLAG_CAN_48BIT)) { 1808249934Ssmh softc->disk->d_flags |= DISKFLAG_CANDELETE; 1809249940Ssmh softc->disk->d_delmaxsize = 256 * softc->params.secsize; 1810249940Ssmh } else 1811249940Ssmh softc->disk->d_delmaxsize = maxio; 1812298002Simp if ((cpi.hba_misc & PIM_UNMAPPED) != 0) { 1813248519Skib softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO; 1814298002Simp softc->unmappedio = 1; 1815298002Simp } 1816303140Smav if (cpi.hba_misc & PIM_ATA_EXT) 1817303140Smav softc->flags |= ADA_FLAG_PIM_ATA_EXT; 1818219056Snwhitehorn strlcpy(softc->disk->d_descr, cgd->ident_data.model, 1819219056Snwhitehorn MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model))); 1820241305Savg strlcpy(softc->disk->d_ident, cgd->ident_data.serial, 1821241305Savg MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial))); 1822210471Smav softc->disk->d_hba_vendor = cpi.hba_vendor; 1823210471Smav softc->disk->d_hba_device = cpi.hba_device; 1824210471Smav softc->disk->d_hba_subvendor = cpi.hba_subvendor; 1825210471Smav softc->disk->d_hba_subdevice = cpi.hba_subdevice; 1826195534Sscottl 1827195534Sscottl softc->disk->d_sectorsize = softc->params.secsize; 1828198897Smav softc->disk->d_mediasize = (off_t)softc->params.sectors * 1829198897Smav softc->params.secsize; 1830200969Smav if (ata_physical_sector_size(&cgd->ident_data) != 1831200969Smav softc->params.secsize) { 1832200969Smav softc->disk->d_stripesize = 1833200969Smav ata_physical_sector_size(&cgd->ident_data); 1834200969Smav softc->disk->d_stripeoffset = (softc->disk->d_stripesize - 1835200969Smav ata_logical_sector_offset(&cgd->ident_data)) % 1836200969Smav softc->disk->d_stripesize; 1837222520Smav } else if (softc->quirks & ADA_Q_4K) { 1838222520Smav softc->disk->d_stripesize = 4096; 1839222520Smav softc->disk->d_stripeoffset = 0; 1840200969Smav } 1841195534Sscottl softc->disk->d_fwsectors = softc->params.secs_per_track; 1842195534Sscottl softc->disk->d_fwheads = softc->params.heads; 1843208349Smarius ata_disk_firmware_geom_adjust(softc->disk); 1844195534Sscottl 1845249347Sken /* 1846249347Sken * Acquire a reference to the periph before we register with GEOM. 1847249347Sken * We'll release this reference once GEOM calls us back (via 1848249347Sken * adadiskgonecb()) telling us that our provider has been freed. 1849249347Sken */ 1850249347Sken if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 1851249347Sken xpt_print(periph->path, "%s: lost periph during " 1852249347Sken "registration!\n", __func__); 1853249347Sken cam_periph_lock(periph); 1854249347Sken return (CAM_REQ_CMP_ERR); 1855249347Sken } 1856195534Sscottl disk_create(softc->disk, DISK_VERSION); 1857249106Smav cam_periph_lock(periph); 1858195534Sscottl 1859195534Sscottl dp = &softc->params; 1860195534Sscottl snprintf(announce_buf, sizeof(announce_buf), 1861289138Smav "%juMB (%ju %u byte sectors)", 1862289138Smav ((uintmax_t)dp->secsize * dp->sectors) / (1024 * 1024), 1863289138Smav (uintmax_t)dp->sectors, dp->secsize); 1864195534Sscottl xpt_announce_periph(periph, announce_buf); 1865250792Ssmh xpt_announce_quirks(periph, softc->quirks, ADA_Q_BIT_STRING); 1866220454Smav 1867195534Sscottl /* 1868220454Smav * Create our sysctl variables, now that we know 1869220454Smav * we have successfully attached. 1870220454Smav */ 1871256843Smav if (cam_periph_acquire(periph) == CAM_REQ_CMP) 1872256843Smav taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task); 1873220454Smav 1874220454Smav /* 1875195534Sscottl * Add async callbacks for bus reset and 1876195534Sscottl * bus device reset calls. I don't bother 1877195534Sscottl * checking if this fails as, in most cases, 1878195534Sscottl * the system will function just fine without 1879195534Sscottl * them and the only alternative would be to 1880195534Sscottl * not attach the device on failure. 1881195534Sscottl */ 1882235897Smav xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE | 1883236393Smav AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED, 1884236393Smav adaasync, periph, periph->path); 1885195534Sscottl 1886195534Sscottl /* 1887195534Sscottl * Schedule a periodic event to occasionally send an 1888195534Sscottl * ordered tag to a device. 1889195534Sscottl */ 1890256843Smav callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0); 1891195534Sscottl callout_reset(&softc->sendordered_c, 1892230921Smav (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 1893195534Sscottl adasendorderedtag, softc); 1894195534Sscottl 1895300207Sken if (ADA_RA >= 0 && softc->flags & ADA_FLAG_CAN_RAHEAD) { 1896224497Smav softc->state = ADA_STATE_RAHEAD; 1897300207Sken } else if (ADA_WC >= 0 && softc->flags & ADA_FLAG_CAN_WCACHE) { 1898220412Smav softc->state = ADA_STATE_WCACHE; 1899300640Sken } else if ((softc->flags & ADA_FLAG_CAN_LOG) 1900300640Sken && (softc->zone_mode != ADA_ZONE_NONE)) { 1901300207Sken softc->state = ADA_STATE_LOGDIR; 1902256843Smav } else { 1903300207Sken /* 1904300207Sken * Nothing to probe, so we can just transition to the 1905300207Sken * normal state. 1906300207Sken */ 1907300207Sken adaprobedone(periph, NULL); 1908256843Smav return(CAM_REQ_CMP); 1909256843Smav } 1910300207Sken 1911300207Sken xpt_schedule(periph, CAM_PRIORITY_DEV); 1912300207Sken 1913195534Sscottl return(CAM_REQ_CMP); 1914195534Sscottl} 1915195534Sscottl 1916298002Simpstatic int 1917298002Simpada_dsmtrim_req_create(struct ada_softc *softc, struct bio *bp, struct trim_request *req) 1918268205Simp{ 1919268205Simp uint64_t lastlba = (uint64_t)-1; 1920268205Simp int c, lastcount = 0, off, ranges = 0; 1921268205Simp 1922268205Simp bzero(req, sizeof(*req)); 1923268205Simp TAILQ_INIT(&req->bps); 1924268205Simp do { 1925268205Simp uint64_t lba = bp->bio_pblkno; 1926268205Simp int count = bp->bio_bcount / softc->params.secsize; 1927268205Simp 1928268205Simp /* Try to extend the previous range. */ 1929268205Simp if (lba == lastlba) { 1930268205Simp c = min(count, ATA_DSM_RANGE_MAX - lastcount); 1931268205Simp lastcount += c; 1932268205Simp off = (ranges - 1) * ATA_DSM_RANGE_SIZE; 1933268205Simp req->data[off + 6] = lastcount & 0xff; 1934268205Simp req->data[off + 7] = 1935268205Simp (lastcount >> 8) & 0xff; 1936268205Simp count -= c; 1937268205Simp lba += c; 1938268205Simp } 1939268205Simp 1940268205Simp while (count > 0) { 1941268205Simp c = min(count, ATA_DSM_RANGE_MAX); 1942268205Simp off = ranges * ATA_DSM_RANGE_SIZE; 1943268205Simp req->data[off + 0] = lba & 0xff; 1944268205Simp req->data[off + 1] = (lba >> 8) & 0xff; 1945268205Simp req->data[off + 2] = (lba >> 16) & 0xff; 1946268205Simp req->data[off + 3] = (lba >> 24) & 0xff; 1947268205Simp req->data[off + 4] = (lba >> 32) & 0xff; 1948268205Simp req->data[off + 5] = (lba >> 40) & 0xff; 1949268205Simp req->data[off + 6] = c & 0xff; 1950268205Simp req->data[off + 7] = (c >> 8) & 0xff; 1951268205Simp lba += c; 1952268205Simp count -= c; 1953268205Simp lastcount = c; 1954268205Simp ranges++; 1955268205Simp /* 1956268205Simp * Its the caller's responsibility to ensure the 1957268205Simp * request will fit so we don't need to check for 1958268205Simp * overrun here 1959268205Simp */ 1960268205Simp } 1961268205Simp lastlba = lba; 1962268205Simp TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue); 1963298002Simp 1964298002Simp bp = cam_iosched_next_trim(softc->cam_iosched); 1965298002Simp if (bp == NULL) 1966268205Simp break; 1967298002Simp if (bp->bio_bcount / softc->params.secsize > 1968298002Simp (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) { 1969298002Simp cam_iosched_put_back_trim(softc->cam_iosched, bp); 1970298002Simp break; 1971298002Simp } 1972268205Simp } while (1); 1973298002Simp 1974298002Simp return (ranges); 1975298002Simp} 1976298002Simp 1977298002Simpstatic void 1978298002Simpada_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 1979298002Simp{ 1980298002Simp struct trim_request *req = &softc->trim_req; 1981298002Simp int ranges; 1982298002Simp 1983298002Simp ranges = ada_dsmtrim_req_create(softc, bp, req); 1984268205Simp cam_fill_ataio(ataio, 1985268205Simp ada_retry_count, 1986268205Simp adadone, 1987268205Simp CAM_DIR_OUT, 1988268205Simp 0, 1989268205Simp req->data, 1990298649Spfg howmany(ranges, ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE, 1991268205Simp ada_default_timeout * 1000); 1992268205Simp ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT, 1993298649Spfg ATA_DSM_TRIM, 0, howmany(ranges, ATA_DSM_BLK_RANGES)); 1994268205Simp} 1995268205Simp 1996268205Simpstatic void 1997298002Simpada_ncq_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 1998298002Simp{ 1999298002Simp struct trim_request *req = &softc->trim_req; 2000298002Simp int ranges; 2001298002Simp 2002298002Simp ranges = ada_dsmtrim_req_create(softc, bp, req); 2003298002Simp cam_fill_ataio(ataio, 2004298002Simp ada_retry_count, 2005298002Simp adadone, 2006298002Simp CAM_DIR_OUT, 2007298002Simp 0, 2008298002Simp req->data, 2009298649Spfg howmany(ranges, ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE, 2010298002Simp ada_default_timeout * 1000); 2011298002Simp ata_ncq_cmd(ataio, 2012298002Simp ATA_SEND_FPDMA_QUEUED, 2013298002Simp 0, 2014298649Spfg howmany(ranges, ATA_DSM_BLK_RANGES)); 2015298002Simp ataio->cmd.sector_count_exp = ATA_SFPDMA_DSM; 2016298143Simp ataio->ata_flags |= ATA_FLAG_AUX; 2017298143Simp ataio->aux = 1; 2018298002Simp} 2019298002Simp 2020298002Simpstatic void 2021268205Simpada_cfaerase(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio) 2022268205Simp{ 2023273704Ssmh struct trim_request *req = &softc->trim_req; 2024268205Simp uint64_t lba = bp->bio_pblkno; 2025268205Simp uint16_t count = bp->bio_bcount / softc->params.secsize; 2026268205Simp 2027273704Ssmh bzero(req, sizeof(*req)); 2028273704Ssmh TAILQ_INIT(&req->bps); 2029273704Ssmh TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue); 2030273704Ssmh 2031268205Simp cam_fill_ataio(ataio, 2032268205Simp ada_retry_count, 2033268205Simp adadone, 2034268205Simp CAM_DIR_NONE, 2035268205Simp 0, 2036268205Simp NULL, 2037268205Simp 0, 2038268205Simp ada_default_timeout*1000); 2039268205Simp 2040268205Simp if (count >= 256) 2041268205Simp count = 0; 2042268205Simp ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count); 2043268205Simp} 2044268205Simp 2045300207Skenstatic int 2046300207Skenada_zone_bio_to_ata(int disk_zone_cmd) 2047300207Sken{ 2048300207Sken switch (disk_zone_cmd) { 2049300207Sken case DISK_ZONE_OPEN: 2050300207Sken return ATA_ZM_OPEN_ZONE; 2051300207Sken case DISK_ZONE_CLOSE: 2052300207Sken return ATA_ZM_CLOSE_ZONE; 2053300207Sken case DISK_ZONE_FINISH: 2054300207Sken return ATA_ZM_FINISH_ZONE; 2055300207Sken case DISK_ZONE_RWP: 2056300207Sken return ATA_ZM_RWP; 2057300207Sken } 2058300207Sken 2059300207Sken return -1; 2060300207Sken} 2061300207Sken 2062300207Skenstatic int 2063300207Skenada_zone_cmd(struct cam_periph *periph, union ccb *ccb, struct bio *bp, 2064300207Sken int *queue_ccb) 2065300207Sken{ 2066300207Sken struct ada_softc *softc; 2067300207Sken int error; 2068300207Sken 2069300207Sken error = 0; 2070300207Sken 2071300207Sken if (bp->bio_cmd != BIO_ZONE) { 2072300207Sken error = EINVAL; 2073300207Sken goto bailout; 2074300207Sken } 2075300207Sken 2076300207Sken softc = periph->softc; 2077300207Sken 2078300207Sken switch (bp->bio_zone.zone_cmd) { 2079300207Sken case DISK_ZONE_OPEN: 2080300207Sken case DISK_ZONE_CLOSE: 2081300207Sken case DISK_ZONE_FINISH: 2082300207Sken case DISK_ZONE_RWP: { 2083300207Sken int zone_flags; 2084300207Sken int zone_sa; 2085300207Sken uint64_t lba; 2086300207Sken 2087300207Sken zone_sa = ada_zone_bio_to_ata(bp->bio_zone.zone_cmd); 2088300207Sken if (zone_sa == -1) { 2089300207Sken xpt_print(periph->path, "Cannot translate zone " 2090300207Sken "cmd %#x to ATA\n", bp->bio_zone.zone_cmd); 2091300207Sken error = EINVAL; 2092300207Sken goto bailout; 2093300207Sken } 2094300207Sken 2095300207Sken zone_flags = 0; 2096300207Sken lba = bp->bio_zone.zone_params.rwp.id; 2097300207Sken 2098300207Sken if (bp->bio_zone.zone_params.rwp.flags & 2099300207Sken DISK_ZONE_RWP_FLAG_ALL) 2100300207Sken zone_flags |= ZBC_OUT_ALL; 2101300207Sken 2102300207Sken ata_zac_mgmt_out(&ccb->ataio, 2103300207Sken /*retries*/ ada_retry_count, 2104300207Sken /*cbfcnp*/ adadone, 2105300207Sken /*use_ncq*/ (softc->flags & 2106300207Sken ADA_FLAG_PIM_ATA_EXT) ? 1 : 0, 2107300207Sken /*zm_action*/ zone_sa, 2108300207Sken /*zone_id*/ lba, 2109300207Sken /*zone_flags*/ zone_flags, 2110300207Sken /*sector_count*/ 0, 2111300207Sken /*data_ptr*/ NULL, 2112300207Sken /*dxfer_len*/ 0, 2113300207Sken /*timeout*/ ada_default_timeout * 1000); 2114300207Sken *queue_ccb = 1; 2115300207Sken 2116300207Sken break; 2117300207Sken } 2118300207Sken case DISK_ZONE_REPORT_ZONES: { 2119300207Sken uint8_t *rz_ptr; 2120300207Sken uint32_t num_entries, alloc_size; 2121300207Sken struct disk_zone_report *rep; 2122300207Sken 2123300207Sken rep = &bp->bio_zone.zone_params.report; 2124300207Sken 2125300207Sken num_entries = rep->entries_allocated; 2126300207Sken if (num_entries == 0) { 2127300207Sken xpt_print(periph->path, "No entries allocated for " 2128300207Sken "Report Zones request\n"); 2129300207Sken error = EINVAL; 2130300207Sken goto bailout; 2131300207Sken } 2132300207Sken alloc_size = sizeof(struct scsi_report_zones_hdr) + 2133300207Sken (sizeof(struct scsi_report_zones_desc) * num_entries); 2134300207Sken alloc_size = min(alloc_size, softc->disk->d_maxsize); 2135300207Sken rz_ptr = malloc(alloc_size, M_ATADA, M_NOWAIT | M_ZERO); 2136300207Sken if (rz_ptr == NULL) { 2137300207Sken xpt_print(periph->path, "Unable to allocate memory " 2138300207Sken "for Report Zones request\n"); 2139300207Sken error = ENOMEM; 2140300207Sken goto bailout; 2141300207Sken } 2142300207Sken 2143300207Sken ata_zac_mgmt_in(&ccb->ataio, 2144300207Sken /*retries*/ ada_retry_count, 2145300207Sken /*cbcfnp*/ adadone, 2146300207Sken /*use_ncq*/ (softc->flags & 2147300207Sken ADA_FLAG_PIM_ATA_EXT) ? 1 : 0, 2148300207Sken /*zm_action*/ ATA_ZM_REPORT_ZONES, 2149300207Sken /*zone_id*/ rep->starting_id, 2150300207Sken /*zone_flags*/ rep->rep_options, 2151300207Sken /*data_ptr*/ rz_ptr, 2152300207Sken /*dxfer_len*/ alloc_size, 2153300207Sken /*timeout*/ ada_default_timeout * 1000); 2154300207Sken 2155300207Sken /* 2156300207Sken * For BIO_ZONE, this isn't normally needed. However, it 2157300207Sken * is used by devstat_end_transaction_bio() to determine 2158300207Sken * how much data was transferred. 2159300207Sken */ 2160300207Sken /* 2161300207Sken * XXX KDM we have a problem. But I'm not sure how to fix 2162300207Sken * it. devstat uses bio_bcount - bio_resid to calculate 2163300207Sken * the amount of data transferred. The GEOM disk code 2164300207Sken * uses bio_length - bio_resid to calculate the amount of 2165300207Sken * data in bio_completed. We have different structure 2166300207Sken * sizes above and below the ada(4) driver. So, if we 2167300207Sken * use the sizes above, the amount transferred won't be 2168300207Sken * quite accurate for devstat. If we use different sizes 2169300207Sken * for bio_bcount and bio_length (above and below 2170300207Sken * respectively), then the residual needs to match one or 2171300207Sken * the other. Everything is calculated after the bio 2172300207Sken * leaves the driver, so changing the values around isn't 2173300207Sken * really an option. For now, just set the count to the 2174300207Sken * passed in length. This means that the calculations 2175300207Sken * above (e.g. bio_completed) will be correct, but the 2176300207Sken * amount of data reported to devstat will be slightly 2177300207Sken * under or overstated. 2178300207Sken */ 2179300207Sken bp->bio_bcount = bp->bio_length; 2180300207Sken 2181300207Sken *queue_ccb = 1; 2182300207Sken 2183300207Sken break; 2184300207Sken } 2185300207Sken case DISK_ZONE_GET_PARAMS: { 2186300207Sken struct disk_zone_disk_params *params; 2187300207Sken 2188300207Sken params = &bp->bio_zone.zone_params.disk_params; 2189300207Sken bzero(params, sizeof(*params)); 2190300207Sken 2191300207Sken switch (softc->zone_mode) { 2192300207Sken case ADA_ZONE_DRIVE_MANAGED: 2193300207Sken params->zone_mode = DISK_ZONE_MODE_DRIVE_MANAGED; 2194300207Sken break; 2195300207Sken case ADA_ZONE_HOST_AWARE: 2196300207Sken params->zone_mode = DISK_ZONE_MODE_HOST_AWARE; 2197300207Sken break; 2198300207Sken case ADA_ZONE_HOST_MANAGED: 2199300207Sken params->zone_mode = DISK_ZONE_MODE_HOST_MANAGED; 2200300207Sken break; 2201300207Sken default: 2202300207Sken case ADA_ZONE_NONE: 2203300207Sken params->zone_mode = DISK_ZONE_MODE_NONE; 2204300207Sken break; 2205300207Sken } 2206300207Sken 2207300207Sken if (softc->zone_flags & ADA_ZONE_FLAG_URSWRZ) 2208300207Sken params->flags |= DISK_ZONE_DISK_URSWRZ; 2209300207Sken 2210300207Sken if (softc->zone_flags & ADA_ZONE_FLAG_OPT_SEQ_SET) { 2211300207Sken params->optimal_seq_zones = softc->optimal_seq_zones; 2212300207Sken params->flags |= DISK_ZONE_OPT_SEQ_SET; 2213300207Sken } 2214300207Sken 2215300207Sken if (softc->zone_flags & ADA_ZONE_FLAG_OPT_NONSEQ_SET) { 2216300207Sken params->optimal_nonseq_zones = 2217300207Sken softc->optimal_nonseq_zones; 2218300207Sken params->flags |= DISK_ZONE_OPT_NONSEQ_SET; 2219300207Sken } 2220300207Sken 2221300207Sken if (softc->zone_flags & ADA_ZONE_FLAG_MAX_SEQ_SET) { 2222300207Sken params->max_seq_zones = softc->max_seq_zones; 2223300207Sken params->flags |= DISK_ZONE_MAX_SEQ_SET; 2224300207Sken } 2225300207Sken if (softc->zone_flags & ADA_ZONE_FLAG_RZ_SUP) 2226300207Sken params->flags |= DISK_ZONE_RZ_SUP; 2227300207Sken 2228300207Sken if (softc->zone_flags & ADA_ZONE_FLAG_OPEN_SUP) 2229300207Sken params->flags |= DISK_ZONE_OPEN_SUP; 2230300207Sken 2231300207Sken if (softc->zone_flags & ADA_ZONE_FLAG_CLOSE_SUP) 2232300207Sken params->flags |= DISK_ZONE_CLOSE_SUP; 2233300207Sken 2234300207Sken if (softc->zone_flags & ADA_ZONE_FLAG_FINISH_SUP) 2235300207Sken params->flags |= DISK_ZONE_FINISH_SUP; 2236300207Sken 2237300207Sken if (softc->zone_flags & ADA_ZONE_FLAG_RWP_SUP) 2238300207Sken params->flags |= DISK_ZONE_RWP_SUP; 2239300207Sken break; 2240300207Sken } 2241300207Sken default: 2242300207Sken break; 2243300207Sken } 2244300207Skenbailout: 2245300207Sken return (error); 2246300207Sken} 2247300207Sken 2248268205Simpstatic void 2249195534Sscottladastart(struct cam_periph *periph, union ccb *start_ccb) 2250195534Sscottl{ 2251198328Smav struct ada_softc *softc = (struct ada_softc *)periph->softc; 2252198328Smav struct ccb_ataio *ataio = &start_ccb->ataio; 2253195534Sscottl 2254236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n")); 2255236602Smav 2256195534Sscottl switch (softc->state) { 2257195534Sscottl case ADA_STATE_NORMAL: 2258195534Sscottl { 2259195534Sscottl struct bio *bp; 2260201139Smav u_int8_t tag_code; 2261195534Sscottl 2262298002Simp bp = cam_iosched_next_bio(softc->cam_iosched); 2263201139Smav if (bp == NULL) { 2264195534Sscottl xpt_release_ccb(start_ccb); 2265201139Smav break; 2266201139Smav } 2267201139Smav 2268298002Simp if ((bp->bio_flags & BIO_ORDERED) != 0 || 2269298002Simp (bp->bio_cmd != BIO_DELETE && (softc->flags & ADA_FLAG_NEED_OTAG) != 0)) { 2270201139Smav softc->flags &= ~ADA_FLAG_NEED_OTAG; 2271257054Smav softc->flags |= ADA_FLAG_WAS_OTAG; 2272201139Smav tag_code = 0; 2273195534Sscottl } else { 2274201139Smav tag_code = 1; 2275201139Smav } 2276201139Smav switch (bp->bio_cmd) { 2277253724Smav case BIO_WRITE: 2278201139Smav case BIO_READ: 2279201139Smav { 2280201139Smav uint64_t lba = bp->bio_pblkno; 2281201139Smav uint16_t count = bp->bio_bcount / softc->params.secsize; 2282291716Sken void *data_ptr; 2283291716Sken int rw_op; 2284291716Sken 2285291716Sken if (bp->bio_cmd == BIO_WRITE) { 2286291716Sken softc->flags |= ADA_FLAG_DIRTY; 2287291716Sken rw_op = CAM_DIR_OUT; 2288291716Sken } else { 2289291716Sken rw_op = CAM_DIR_IN; 2290291716Sken } 2291291716Sken 2292291716Sken data_ptr = bp->bio_data; 2293291716Sken if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) { 2294291716Sken rw_op |= CAM_DATA_BIO; 2295291716Sken data_ptr = bp; 2296291716Sken } 2297291716Sken 2298220454Smav#ifdef ADA_TEST_FAILURE 2299220454Smav int fail = 0; 2300195534Sscottl 2301220454Smav /* 2302220454Smav * Support the failure ioctls. If the command is a 2303220454Smav * read, and there are pending forced read errors, or 2304220454Smav * if a write and pending write errors, then fail this 2305220454Smav * operation with EIO. This is useful for testing 2306220454Smav * purposes. Also, support having every Nth read fail. 2307220454Smav * 2308220454Smav * This is a rather blunt tool. 2309220454Smav */ 2310220454Smav if (bp->bio_cmd == BIO_READ) { 2311220454Smav if (softc->force_read_error) { 2312220454Smav softc->force_read_error--; 2313220454Smav fail = 1; 2314220454Smav } 2315220454Smav if (softc->periodic_read_error > 0) { 2316220454Smav if (++softc->periodic_read_count >= 2317220454Smav softc->periodic_read_error) { 2318220454Smav softc->periodic_read_count = 0; 2319220454Smav fail = 1; 2320220454Smav } 2321220454Smav } 2322220454Smav } else { 2323220454Smav if (softc->force_write_error) { 2324220454Smav softc->force_write_error--; 2325220454Smav fail = 1; 2326220454Smav } 2327220454Smav } 2328220454Smav if (fail) { 2329287025Smav biofinish(bp, NULL, EIO); 2330220454Smav xpt_release_ccb(start_ccb); 2331220454Smav adaschedule(periph); 2332220454Smav return; 2333220454Smav } 2334220454Smav#endif 2335248519Skib KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 || 2336248519Skib round_page(bp->bio_bcount + bp->bio_ma_offset) / 2337248519Skib PAGE_SIZE == bp->bio_ma_n, 2338248519Skib ("Short bio %p", bp)); 2339201139Smav cam_fill_ataio(ataio, 2340201139Smav ada_retry_count, 2341201139Smav adadone, 2342291716Sken rw_op, 2343298142Simp 0, 2344291716Sken data_ptr, 2345201139Smav bp->bio_bcount, 2346201139Smav ada_default_timeout*1000); 2347195534Sscottl 2348201139Smav if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) { 2349201139Smav if (bp->bio_cmd == BIO_READ) { 2350201139Smav ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED, 2351201139Smav lba, count); 2352201139Smav } else { 2353201139Smav ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED, 2354201139Smav lba, count); 2355201139Smav } 2356201139Smav } else if ((softc->flags & ADA_FLAG_CAN_48BIT) && 2357201139Smav (lba + count >= ATA_MAX_28BIT_LBA || 2358201139Smav count > 256)) { 2359249199Smarius if (softc->flags & ADA_FLAG_CAN_DMA48) { 2360195534Sscottl if (bp->bio_cmd == BIO_READ) { 2361201139Smav ata_48bit_cmd(ataio, ATA_READ_DMA48, 2362201139Smav 0, lba, count); 2363195534Sscottl } else { 2364201139Smav ata_48bit_cmd(ataio, ATA_WRITE_DMA48, 2365201139Smav 0, lba, count); 2366195534Sscottl } 2367201139Smav } else { 2368201139Smav if (bp->bio_cmd == BIO_READ) { 2369201139Smav ata_48bit_cmd(ataio, ATA_READ_MUL48, 2370201139Smav 0, lba, count); 2371195534Sscottl } else { 2372201139Smav ata_48bit_cmd(ataio, ATA_WRITE_MUL48, 2373201139Smav 0, lba, count); 2374195534Sscottl } 2375201139Smav } 2376201139Smav } else { 2377201139Smav if (count == 256) 2378201139Smav count = 0; 2379201139Smav if (softc->flags & ADA_FLAG_CAN_DMA) { 2380201139Smav if (bp->bio_cmd == BIO_READ) { 2381201139Smav ata_28bit_cmd(ataio, ATA_READ_DMA, 2382201139Smav 0, lba, count); 2383201139Smav } else { 2384201139Smav ata_28bit_cmd(ataio, ATA_WRITE_DMA, 2385201139Smav 0, lba, count); 2386201139Smav } 2387195534Sscottl } else { 2388201139Smav if (bp->bio_cmd == BIO_READ) { 2389201139Smav ata_28bit_cmd(ataio, ATA_READ_MUL, 2390201139Smav 0, lba, count); 2391195534Sscottl } else { 2392201139Smav ata_28bit_cmd(ataio, ATA_WRITE_MUL, 2393201139Smav 0, lba, count); 2394195534Sscottl } 2395195534Sscottl } 2396195534Sscottl } 2397201139Smav break; 2398201139Smav } 2399298002Simp case BIO_DELETE: 2400298002Simp switch (softc->delete_method) { 2401298002Simp case ADA_DELETE_NCQ_DSM_TRIM: 2402298002Simp ada_ncq_dsmtrim(softc, bp, ataio); 2403298002Simp break; 2404298002Simp case ADA_DELETE_DSM_TRIM: 2405298002Simp ada_dsmtrim(softc, bp, ataio); 2406298002Simp break; 2407298002Simp case ADA_DELETE_CFA_ERASE: 2408298002Simp ada_cfaerase(softc, bp, ataio); 2409298002Simp break; 2410298002Simp default: 2411298002Simp biofinish(bp, NULL, EOPNOTSUPP); 2412298002Simp xpt_release_ccb(start_ccb); 2413298002Simp adaschedule(periph); 2414298002Simp return; 2415298002Simp } 2416298002Simp start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM; 2417298002Simp start_ccb->ccb_h.flags |= CAM_UNLOCKED; 2418298002Simp cam_iosched_submit_trim(softc->cam_iosched); 2419298002Simp goto out; 2420201139Smav case BIO_FLUSH: 2421201139Smav cam_fill_ataio(ataio, 2422201139Smav 1, 2423201139Smav adadone, 2424201139Smav CAM_DIR_NONE, 2425201139Smav 0, 2426201139Smav NULL, 2427201139Smav 0, 2428201139Smav ada_default_timeout*1000); 2429201139Smav 2430201139Smav if (softc->flags & ADA_FLAG_CAN_48BIT) 2431201139Smav ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0); 2432201139Smav else 2433201139Smav ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0); 2434201139Smav break; 2435300207Sken case BIO_ZONE: { 2436300207Sken int error, queue_ccb; 2437300207Sken 2438300207Sken queue_ccb = 0; 2439300207Sken 2440300207Sken error = ada_zone_cmd(periph, start_ccb, bp, &queue_ccb); 2441300207Sken if ((error != 0) 2442300207Sken || (queue_ccb == 0)) { 2443300207Sken biofinish(bp, NULL, error); 2444300207Sken xpt_release_ccb(start_ccb); 2445300207Sken return; 2446300207Sken } 2447300207Sken break; 2448195534Sscottl } 2449300207Sken } 2450201139Smav start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO; 2451256843Smav start_ccb->ccb_h.flags |= CAM_UNLOCKED; 2452201139Smavout: 2453201139Smav start_ccb->ccb_h.ccb_bp = bp; 2454201139Smav softc->outstanding_cmds++; 2455256843Smav softc->refcount++; 2456256843Smav cam_periph_unlock(periph); 2457201139Smav xpt_action(start_ccb); 2458256843Smav cam_periph_lock(periph); 2459256843Smav softc->refcount--; 2460201139Smav 2461201139Smav /* May have more work to do, so ensure we stay scheduled */ 2462201139Smav adaschedule(periph); 2463195534Sscottl break; 2464195534Sscottl } 2465224497Smav case ADA_STATE_RAHEAD: 2466220412Smav case ADA_STATE_WCACHE: 2467220412Smav { 2468220412Smav cam_fill_ataio(ataio, 2469220412Smav 1, 2470220412Smav adadone, 2471220412Smav CAM_DIR_NONE, 2472220412Smav 0, 2473220412Smav NULL, 2474220412Smav 0, 2475220412Smav ada_default_timeout*1000); 2476220412Smav 2477224497Smav if (softc->state == ADA_STATE_RAHEAD) { 2478224497Smav ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ? 2479224497Smav ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0); 2480224497Smav start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD; 2481224497Smav } else { 2482224497Smav ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ? 2483224497Smav ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0); 2484224497Smav start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE; 2485224497Smav } 2486249466Smav start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 2487220412Smav xpt_action(start_ccb); 2488220412Smav break; 2489195534Sscottl } 2490300207Sken case ADA_STATE_LOGDIR: 2491300207Sken { 2492300207Sken struct ata_gp_log_dir *log_dir; 2493300207Sken 2494300207Sken if ((softc->flags & ADA_FLAG_CAN_LOG) == 0) { 2495300207Sken adaprobedone(periph, start_ccb); 2496300207Sken break; 2497300207Sken } 2498300207Sken 2499300207Sken log_dir = malloc(sizeof(*log_dir), M_ATADA, M_NOWAIT|M_ZERO); 2500300207Sken if (log_dir == NULL) { 2501300207Sken xpt_print(periph->path, "Couldn't malloc log_dir " 2502300207Sken "data\n"); 2503300207Sken softc->state = ADA_STATE_NORMAL; 2504300207Sken xpt_release_ccb(start_ccb); 2505300207Sken break; 2506300207Sken } 2507300207Sken 2508300207Sken 2509300207Sken ata_read_log(ataio, 2510300207Sken /*retries*/1, 2511300207Sken /*cbfcnp*/adadone, 2512300207Sken /*log_address*/ ATA_LOG_DIRECTORY, 2513300207Sken /*page_number*/ 0, 2514300207Sken /*block_count*/ 1, 2515300207Sken /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ? 2516300207Sken CAM_ATAIO_DMA : 0, 2517300207Sken /*data_ptr*/ (uint8_t *)log_dir, 2518300207Sken /*dxfer_len*/sizeof(*log_dir), 2519300207Sken /*timeout*/ada_default_timeout*1000); 2520300207Sken 2521300207Sken start_ccb->ccb_h.ccb_state = ADA_CCB_LOGDIR; 2522300207Sken xpt_action(start_ccb); 2523300207Sken break; 2524220412Smav } 2525300207Sken case ADA_STATE_IDDIR: 2526300207Sken { 2527300207Sken struct ata_identify_log_pages *id_dir; 2528300207Sken 2529300207Sken id_dir = malloc(sizeof(*id_dir), M_ATADA, M_NOWAIT | M_ZERO); 2530300207Sken if (id_dir == NULL) { 2531300207Sken xpt_print(periph->path, "Couldn't malloc id_dir " 2532300207Sken "data\n"); 2533300207Sken adaprobedone(periph, start_ccb); 2534300207Sken break; 2535300207Sken } 2536300207Sken 2537300207Sken ata_read_log(ataio, 2538300207Sken /*retries*/1, 2539300207Sken /*cbfcnp*/adadone, 2540300207Sken /*log_address*/ ATA_IDENTIFY_DATA_LOG, 2541300207Sken /*page_number*/ ATA_IDL_PAGE_LIST, 2542300207Sken /*block_count*/ 1, 2543300207Sken /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ? 2544300207Sken CAM_ATAIO_DMA : 0, 2545300207Sken /*data_ptr*/ (uint8_t *)id_dir, 2546300207Sken /*dxfer_len*/ sizeof(*id_dir), 2547300207Sken /*timeout*/ada_default_timeout*1000); 2548300207Sken 2549300207Sken start_ccb->ccb_h.ccb_state = ADA_CCB_IDDIR; 2550300207Sken xpt_action(start_ccb); 2551300207Sken break; 2552300207Sken } 2553300207Sken case ADA_STATE_SUP_CAP: 2554300207Sken { 2555300207Sken struct ata_identify_log_sup_cap *sup_cap; 2556300207Sken 2557300207Sken sup_cap = malloc(sizeof(*sup_cap), M_ATADA, M_NOWAIT|M_ZERO); 2558300207Sken if (sup_cap == NULL) { 2559300207Sken xpt_print(periph->path, "Couldn't malloc sup_cap " 2560300207Sken "data\n"); 2561300207Sken adaprobedone(periph, start_ccb); 2562300207Sken break; 2563300207Sken } 2564300207Sken 2565300207Sken ata_read_log(ataio, 2566300207Sken /*retries*/1, 2567300207Sken /*cbfcnp*/adadone, 2568300207Sken /*log_address*/ ATA_IDENTIFY_DATA_LOG, 2569300207Sken /*page_number*/ ATA_IDL_SUP_CAP, 2570300207Sken /*block_count*/ 1, 2571300207Sken /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ? 2572300207Sken CAM_ATAIO_DMA : 0, 2573300207Sken /*data_ptr*/ (uint8_t *)sup_cap, 2574300207Sken /*dxfer_len*/ sizeof(*sup_cap), 2575300207Sken /*timeout*/ada_default_timeout*1000); 2576300207Sken 2577300207Sken start_ccb->ccb_h.ccb_state = ADA_CCB_SUP_CAP; 2578300207Sken xpt_action(start_ccb); 2579300207Sken break; 2580300207Sken } 2581300207Sken case ADA_STATE_ZONE: 2582300207Sken { 2583300207Sken struct ata_zoned_info_log *ata_zone; 2584300207Sken 2585300207Sken ata_zone = malloc(sizeof(*ata_zone), M_ATADA, M_NOWAIT|M_ZERO); 2586300207Sken if (ata_zone == NULL) { 2587300207Sken xpt_print(periph->path, "Couldn't malloc ata_zone " 2588300207Sken "data\n"); 2589300207Sken adaprobedone(periph, start_ccb); 2590300207Sken break; 2591300207Sken } 2592300207Sken 2593300207Sken ata_read_log(ataio, 2594300207Sken /*retries*/1, 2595300207Sken /*cbfcnp*/adadone, 2596300207Sken /*log_address*/ ATA_IDENTIFY_DATA_LOG, 2597300207Sken /*page_number*/ ATA_IDL_ZDI, 2598300207Sken /*block_count*/ 1, 2599300207Sken /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ? 2600300207Sken CAM_ATAIO_DMA : 0, 2601300207Sken /*data_ptr*/ (uint8_t *)ata_zone, 2602300207Sken /*dxfer_len*/ sizeof(*ata_zone), 2603300207Sken /*timeout*/ada_default_timeout*1000); 2604300207Sken 2605300207Sken start_ccb->ccb_h.ccb_state = ADA_CCB_ZONE; 2606300207Sken xpt_action(start_ccb); 2607300207Sken break; 2608300207Sken } 2609300207Sken } 2610195534Sscottl} 2611195534Sscottl 2612195534Sscottlstatic void 2613300207Skenadaprobedone(struct cam_periph *periph, union ccb *ccb) 2614300207Sken{ 2615300207Sken struct ada_softc *softc; 2616300207Sken 2617300207Sken softc = (struct ada_softc *)periph->softc; 2618300207Sken 2619300207Sken if (ccb != NULL) 2620300207Sken xpt_release_ccb(ccb); 2621300207Sken 2622300207Sken softc->state = ADA_STATE_NORMAL; 2623300207Sken softc->flags |= ADA_FLAG_PROBED; 2624300207Sken adaschedule(periph); 2625300207Sken if ((softc->flags & ADA_FLAG_ANNOUNCED) == 0) { 2626300207Sken softc->flags |= ADA_FLAG_ANNOUNCED; 2627300207Sken cam_periph_unhold(periph); 2628300207Sken } else { 2629300207Sken cam_periph_release_locked(periph); 2630300207Sken } 2631300207Sken} 2632300207Sken 2633300207Skenstatic void 2634300207Skenadazonedone(struct cam_periph *periph, union ccb *ccb) 2635300207Sken{ 2636300207Sken struct ada_softc *softc; 2637300207Sken struct bio *bp; 2638300207Sken 2639300207Sken softc = periph->softc; 2640300207Sken bp = (struct bio *)ccb->ccb_h.ccb_bp; 2641300207Sken 2642300207Sken switch (bp->bio_zone.zone_cmd) { 2643300207Sken case DISK_ZONE_OPEN: 2644300207Sken case DISK_ZONE_CLOSE: 2645300207Sken case DISK_ZONE_FINISH: 2646300207Sken case DISK_ZONE_RWP: 2647300207Sken break; 2648300207Sken case DISK_ZONE_REPORT_ZONES: { 2649300207Sken uint32_t avail_len; 2650300207Sken struct disk_zone_report *rep; 2651300207Sken struct scsi_report_zones_hdr *hdr; 2652300207Sken struct scsi_report_zones_desc *desc; 2653300207Sken struct disk_zone_rep_entry *entry; 2654300207Sken uint32_t num_alloced, hdr_len, num_avail; 2655300207Sken uint32_t num_to_fill, i; 2656300207Sken 2657300207Sken rep = &bp->bio_zone.zone_params.report; 2658300207Sken avail_len = ccb->ataio.dxfer_len - ccb->ataio.resid; 2659300207Sken /* 2660300207Sken * Note that bio_resid isn't normally used for zone 2661300207Sken * commands, but it is used by devstat_end_transaction_bio() 2662300207Sken * to determine how much data was transferred. Because 2663300207Sken * the size of the SCSI/ATA data structures is different 2664300207Sken * than the size of the BIO interface structures, the 2665300207Sken * amount of data actually transferred from the drive will 2666300207Sken * be different than the amount of data transferred to 2667300207Sken * the user. 2668300207Sken */ 2669300207Sken num_alloced = rep->entries_allocated; 2670300207Sken hdr = (struct scsi_report_zones_hdr *)ccb->ataio.data_ptr; 2671300207Sken if (avail_len < sizeof(*hdr)) { 2672300207Sken /* 2673300207Sken * Is there a better error than EIO here? We asked 2674300207Sken * for at least the header, and we got less than 2675300207Sken * that. 2676300207Sken */ 2677300207Sken bp->bio_error = EIO; 2678300207Sken bp->bio_flags |= BIO_ERROR; 2679300207Sken bp->bio_resid = bp->bio_bcount; 2680300207Sken break; 2681300207Sken } 2682300207Sken 2683300207Sken hdr_len = le32dec(hdr->length); 2684300207Sken if (hdr_len > 0) 2685300207Sken rep->entries_available = hdr_len / sizeof(*desc); 2686300207Sken else 2687300207Sken rep->entries_available = 0; 2688300207Sken /* 2689300207Sken * NOTE: using the same values for the BIO version of the 2690300207Sken * same field as the SCSI/ATA values. This means we could 2691300207Sken * get some additional values that aren't defined in bio.h 2692300207Sken * if more values of the same field are defined later. 2693300207Sken */ 2694300207Sken rep->header.same = hdr->byte4 & SRZ_SAME_MASK; 2695300207Sken rep->header.maximum_lba = le64dec(hdr->maximum_lba); 2696300207Sken /* 2697300207Sken * If the drive reports no entries that match the query, 2698300207Sken * we're done. 2699300207Sken */ 2700300207Sken if (hdr_len == 0) { 2701300207Sken rep->entries_filled = 0; 2702300207Sken bp->bio_resid = bp->bio_bcount; 2703300207Sken break; 2704300207Sken } 2705300207Sken 2706300207Sken num_avail = min((avail_len - sizeof(*hdr)) / sizeof(*desc), 2707300207Sken hdr_len / sizeof(*desc)); 2708300207Sken /* 2709300207Sken * If the drive didn't return any data, then we're done. 2710300207Sken */ 2711300207Sken if (num_avail == 0) { 2712300207Sken rep->entries_filled = 0; 2713300207Sken bp->bio_resid = bp->bio_bcount; 2714300207Sken break; 2715300207Sken } 2716300207Sken 2717300207Sken num_to_fill = min(num_avail, rep->entries_allocated); 2718300207Sken /* 2719300207Sken * If the user didn't allocate any entries for us to fill, 2720300207Sken * we're done. 2721300207Sken */ 2722300207Sken if (num_to_fill == 0) { 2723300207Sken rep->entries_filled = 0; 2724300207Sken bp->bio_resid = bp->bio_bcount; 2725300207Sken break; 2726300207Sken } 2727300207Sken 2728300207Sken for (i = 0, desc = &hdr->desc_list[0], entry=&rep->entries[0]; 2729300207Sken i < num_to_fill; i++, desc++, entry++) { 2730300207Sken /* 2731300207Sken * NOTE: we're mapping the values here directly 2732300207Sken * from the SCSI/ATA bit definitions to the bio.h 2733300207Sken * definitions. There is also a warning in 2734300207Sken * disk_zone.h, but the impact is that if 2735300207Sken * additional values are added in the SCSI/ATA 2736300207Sken * specs these will be visible to consumers of 2737300207Sken * this interface. 2738300207Sken */ 2739300207Sken entry->zone_type = desc->zone_type & SRZ_TYPE_MASK; 2740300207Sken entry->zone_condition = 2741300207Sken (desc->zone_flags & SRZ_ZONE_COND_MASK) >> 2742300207Sken SRZ_ZONE_COND_SHIFT; 2743300207Sken entry->zone_flags |= desc->zone_flags & 2744300207Sken (SRZ_ZONE_NON_SEQ|SRZ_ZONE_RESET); 2745300207Sken entry->zone_length = le64dec(desc->zone_length); 2746300207Sken entry->zone_start_lba = le64dec(desc->zone_start_lba); 2747300207Sken entry->write_pointer_lba = 2748300207Sken le64dec(desc->write_pointer_lba); 2749300207Sken } 2750300207Sken rep->entries_filled = num_to_fill; 2751300207Sken /* 2752300207Sken * Note that this residual is accurate from the user's 2753300207Sken * standpoint, but the amount transferred isn't accurate 2754300207Sken * from the standpoint of what actually came back from the 2755300207Sken * drive. 2756300207Sken */ 2757300207Sken bp->bio_resid = bp->bio_bcount - (num_to_fill * sizeof(*entry)); 2758300207Sken break; 2759300207Sken } 2760300207Sken case DISK_ZONE_GET_PARAMS: 2761300207Sken default: 2762300207Sken /* 2763300207Sken * In theory we should not get a GET_PARAMS bio, since it 2764300207Sken * should be handled without queueing the command to the 2765300207Sken * drive. 2766300207Sken */ 2767300207Sken panic("%s: Invalid zone command %d", __func__, 2768300207Sken bp->bio_zone.zone_cmd); 2769300207Sken break; 2770300207Sken } 2771300207Sken 2772300207Sken if (bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES) 2773300207Sken free(ccb->ataio.data_ptr, M_ATADA); 2774300207Sken} 2775300207Sken 2776300207Sken 2777300207Skenstatic void 2778195534Sscottladadone(struct cam_periph *periph, union ccb *done_ccb) 2779195534Sscottl{ 2780195534Sscottl struct ada_softc *softc; 2781195534Sscottl struct ccb_ataio *ataio; 2782249466Smav struct cam_path *path; 2783300207Sken uint32_t priority; 2784253752Smav int state; 2785195534Sscottl 2786195534Sscottl softc = (struct ada_softc *)periph->softc; 2787195534Sscottl ataio = &done_ccb->ataio; 2788249466Smav path = done_ccb->ccb_h.path; 2789300207Sken priority = done_ccb->ccb_h.pinfo.priority; 2790236602Smav 2791249466Smav CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n")); 2792236602Smav 2793253752Smav state = ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK; 2794253752Smav switch (state) { 2795195534Sscottl case ADA_CCB_BUFFER_IO: 2796201139Smav case ADA_CCB_TRIM: 2797195534Sscottl { 2798195534Sscottl struct bio *bp; 2799253752Smav int error; 2800195534Sscottl 2801256843Smav cam_periph_lock(periph); 2802298002Simp bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 2803195534Sscottl if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2804198328Smav error = adaerror(done_ccb, 0, 0); 2805195534Sscottl if (error == ERESTART) { 2806198328Smav /* A retry was scheduled, so just return. */ 2807256843Smav cam_periph_unlock(periph); 2808195534Sscottl return; 2809195534Sscottl } 2810195534Sscottl if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 2811249466Smav cam_release_devq(path, 2812195534Sscottl /*relsim_flags*/0, 2813195534Sscottl /*reduction*/0, 2814195534Sscottl /*timeout*/0, 2815195534Sscottl /*getcount_only*/0); 2816298002Simp /* 2817298002Simp * If we get an error on an NCQ DSM TRIM, fall back 2818298002Simp * to a non-NCQ DSM TRIM forever. Please note that if 2819298002Simp * CAN_NCQ_TRIM is set, CAN_TRIM is necessarily set too. 2820298002Simp * However, for this one trim, we treat it as advisory 2821298002Simp * and return success up the stack. 2822298002Simp */ 2823298002Simp if (state == ADA_CCB_TRIM && 2824298002Simp error != 0 && 2825298002Simp (softc->flags & ADA_FLAG_CAN_NCQ_TRIM) != 0) { 2826298002Simp softc->flags &= ~ADA_FLAG_CAN_NCQ_TRIM; 2827298002Simp error = 0; 2828298002Simp adasetdeletemethod(softc); 2829298002Simp } 2830195534Sscottl } else { 2831195534Sscottl if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 2832195534Sscottl panic("REQ_CMP with QFRZN"); 2833300207Sken 2834253752Smav error = 0; 2835253752Smav } 2836253752Smav bp->bio_error = error; 2837253752Smav if (error != 0) { 2838253752Smav bp->bio_resid = bp->bio_bcount; 2839253752Smav bp->bio_flags |= BIO_ERROR; 2840253752Smav } else { 2841300207Sken if (bp->bio_cmd == BIO_ZONE) 2842300207Sken adazonedone(periph, done_ccb); 2843300207Sken else if (state == ADA_CCB_TRIM) 2844253752Smav bp->bio_resid = 0; 2845253752Smav else 2846253752Smav bp->bio_resid = ataio->resid; 2847300207Sken 2848300207Sken if ((bp->bio_resid > 0) 2849300207Sken && (bp->bio_cmd != BIO_ZONE)) 2850195534Sscottl bp->bio_flags |= BIO_ERROR; 2851195534Sscottl } 2852195534Sscottl softc->outstanding_cmds--; 2853195534Sscottl if (softc->outstanding_cmds == 0) 2854257054Smav softc->flags |= ADA_FLAG_WAS_OTAG; 2855298002Simp 2856298002Simp cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb); 2857256843Smav xpt_release_ccb(done_ccb); 2858253752Smav if (state == ADA_CCB_TRIM) { 2859256836Smav TAILQ_HEAD(, bio) queue; 2860256836Smav struct bio *bp1; 2861195534Sscottl 2862256836Smav TAILQ_INIT(&queue); 2863256836Smav TAILQ_CONCAT(&queue, &softc->trim_req.bps, bio_queue); 2864277101Simp /* 2865277101Simp * Normally, the xpt_release_ccb() above would make sure 2866277101Simp * that when we have more work to do, that work would 2867277101Simp * get kicked off. However, we specifically keep 2868277101Simp * trim_running set to 0 before the call above to allow 2869277101Simp * other I/O to progress when many BIO_DELETE requests 2870277101Simp * are pushed down. We set trim_running to 0 and call 2871277101Simp * daschedule again so that we don't stall if there are 2872277101Simp * no other I/Os pending apart from BIO_DELETEs. 2873277101Simp */ 2874298002Simp cam_iosched_trim_done(softc->cam_iosched); 2875256843Smav adaschedule(periph); 2876256843Smav cam_periph_unlock(periph); 2877256836Smav while ((bp1 = TAILQ_FIRST(&queue)) != NULL) { 2878256836Smav TAILQ_REMOVE(&queue, bp1, bio_queue); 2879256836Smav bp1->bio_error = error; 2880256836Smav if (error != 0) { 2881201139Smav bp1->bio_flags |= BIO_ERROR; 2882253752Smav bp1->bio_resid = bp1->bio_bcount; 2883253752Smav } else 2884253752Smav bp1->bio_resid = 0; 2885201139Smav biodone(bp1); 2886201139Smav } 2887256843Smav } else { 2888298002Simp adaschedule(periph); 2889256843Smav cam_periph_unlock(periph); 2890201139Smav biodone(bp); 2891256843Smav } 2892256843Smav return; 2893195534Sscottl } 2894224497Smav case ADA_CCB_RAHEAD: 2895224497Smav { 2896224497Smav if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2897224497Smav if (adaerror(done_ccb, 0, 0) == ERESTART) { 2898249466Smav /* Drop freeze taken due to CAM_DEV_QFREEZE */ 2899249466Smav cam_release_devq(path, 0, 0, 0, FALSE); 2900224497Smav return; 2901224497Smav } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 2902249466Smav cam_release_devq(path, 2903224497Smav /*relsim_flags*/0, 2904224497Smav /*reduction*/0, 2905224497Smav /*timeout*/0, 2906224497Smav /*getcount_only*/0); 2907224497Smav } 2908224497Smav } 2909224497Smav 2910224497Smav /* 2911224497Smav * Since our peripheral may be invalidated by an error 2912224497Smav * above or an external event, we must release our CCB 2913224497Smav * before releasing the reference on the peripheral. 2914224497Smav * The peripheral will only go away once the last reference 2915224497Smav * is removed, and we need it around for the CCB release 2916224497Smav * operation. 2917224497Smav */ 2918300207Sken 2919224497Smav xpt_release_ccb(done_ccb); 2920300207Sken softc->state = ADA_STATE_WCACHE; 2921300207Sken xpt_schedule(periph, priority); 2922249466Smav /* Drop freeze taken due to CAM_DEV_QFREEZE */ 2923249466Smav cam_release_devq(path, 0, 0, 0, FALSE); 2924224497Smav return; 2925224497Smav } 2926220412Smav case ADA_CCB_WCACHE: 2927220412Smav { 2928220412Smav if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2929220412Smav if (adaerror(done_ccb, 0, 0) == ERESTART) { 2930300207Sken /* Drop freeze taken due to CAM_DEV_QFREEZE */ 2931300207Sken cam_release_devq(path, 0, 0, 0, FALSE); 2932300207Sken return; 2933220412Smav } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 2934249466Smav cam_release_devq(path, 2935220412Smav /*relsim_flags*/0, 2936220412Smav /*reduction*/0, 2937220412Smav /*timeout*/0, 2938220412Smav /*getcount_only*/0); 2939220412Smav } 2940220412Smav } 2941220412Smav 2942249466Smav /* Drop freeze taken due to CAM_DEV_QFREEZE */ 2943249466Smav cam_release_devq(path, 0, 0, 0, FALSE); 2944300207Sken 2945300640Sken if ((softc->flags & ADA_FLAG_CAN_LOG) 2946300640Sken && (softc->zone_mode != ADA_ZONE_NONE)) { 2947300207Sken xpt_release_ccb(done_ccb); 2948300207Sken softc->state = ADA_STATE_LOGDIR; 2949300207Sken xpt_schedule(periph, priority); 2950300207Sken } else { 2951300207Sken adaprobedone(periph, done_ccb); 2952300207Sken } 2953220412Smav return; 2954220412Smav } 2955300207Sken case ADA_CCB_LOGDIR: 2956300207Sken { 2957300207Sken int error; 2958300207Sken 2959300207Sken if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 2960300207Sken error = 0; 2961300207Sken softc->valid_logdir_len = 0; 2962300207Sken bzero(&softc->ata_logdir, sizeof(softc->ata_logdir)); 2963300207Sken softc->valid_logdir_len = 2964300207Sken ataio->dxfer_len - ataio->resid; 2965300207Sken if (softc->valid_logdir_len > 0) 2966300207Sken bcopy(ataio->data_ptr, &softc->ata_logdir, 2967300207Sken min(softc->valid_logdir_len, 2968300207Sken sizeof(softc->ata_logdir))); 2969300207Sken /* 2970300207Sken * Figure out whether the Identify Device log is 2971300207Sken * supported. The General Purpose log directory 2972300207Sken * has a header, and lists the number of pages 2973300207Sken * available for each GP log identified by the 2974300207Sken * offset into the list. 2975300207Sken */ 2976300207Sken if ((softc->valid_logdir_len >= 2977300207Sken ((ATA_IDENTIFY_DATA_LOG + 1) * sizeof(uint16_t))) 2978300207Sken && (le16dec(softc->ata_logdir.header) == 2979300207Sken ATA_GP_LOG_DIR_VERSION) 2980300207Sken && (le16dec(&softc->ata_logdir.num_pages[ 2981300207Sken (ATA_IDENTIFY_DATA_LOG * 2982300207Sken sizeof(uint16_t)) - sizeof(uint16_t)]) > 0)){ 2983300207Sken softc->flags |= ADA_FLAG_CAN_IDLOG; 2984300207Sken } else { 2985300207Sken softc->flags &= ~ADA_FLAG_CAN_IDLOG; 2986300207Sken } 2987300207Sken } else { 2988300207Sken error = adaerror(done_ccb, CAM_RETRY_SELTO, 2989300207Sken SF_RETRY_UA|SF_NO_PRINT); 2990300207Sken if (error == ERESTART) 2991300207Sken return; 2992300207Sken else if (error != 0) { 2993300207Sken /* 2994300207Sken * If we can't get the ATA log directory, 2995300207Sken * then ATA logs are effectively not 2996300207Sken * supported even if the bit is set in the 2997300207Sken * identify data. 2998300207Sken */ 2999300207Sken softc->flags &= ~(ADA_FLAG_CAN_LOG | 3000300207Sken ADA_FLAG_CAN_IDLOG); 3001300207Sken if ((done_ccb->ccb_h.status & 3002300207Sken CAM_DEV_QFRZN) != 0) { 3003300207Sken /* Don't wedge this device's queue */ 3004300207Sken cam_release_devq(done_ccb->ccb_h.path, 3005300207Sken /*relsim_flags*/0, 3006300207Sken /*reduction*/0, 3007300207Sken /*timeout*/0, 3008300207Sken /*getcount_only*/0); 3009300207Sken } 3010300207Sken } 3011300207Sken 3012300207Sken 3013300207Sken } 3014300207Sken 3015300207Sken free(ataio->data_ptr, M_ATADA); 3016300207Sken 3017300207Sken if ((error == 0) 3018300207Sken && (softc->flags & ADA_FLAG_CAN_IDLOG)) { 3019300207Sken softc->state = ADA_STATE_IDDIR; 3020300207Sken xpt_release_ccb(done_ccb); 3021300207Sken xpt_schedule(periph, priority); 3022300207Sken } else 3023300207Sken adaprobedone(periph, done_ccb); 3024300207Sken 3025300207Sken return; 3026300207Sken } 3027300207Sken case ADA_CCB_IDDIR: { 3028300207Sken int error; 3029300207Sken 3030300207Sken if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 3031300207Sken off_t entries_offset, max_entries; 3032300207Sken error = 0; 3033300207Sken 3034300207Sken softc->valid_iddir_len = 0; 3035300207Sken bzero(&softc->ata_iddir, sizeof(softc->ata_iddir)); 3036300207Sken softc->flags &= ~(ADA_FLAG_CAN_SUPCAP | 3037300207Sken ADA_FLAG_CAN_ZONE); 3038300207Sken softc->valid_iddir_len = 3039300207Sken ataio->dxfer_len - ataio->resid; 3040300207Sken if (softc->valid_iddir_len > 0) 3041300207Sken bcopy(ataio->data_ptr, &softc->ata_iddir, 3042300207Sken min(softc->valid_iddir_len, 3043300207Sken sizeof(softc->ata_iddir))); 3044300207Sken 3045300207Sken entries_offset = 3046300207Sken __offsetof(struct ata_identify_log_pages,entries); 3047300207Sken max_entries = softc->valid_iddir_len - entries_offset; 3048300207Sken if ((softc->valid_iddir_len > (entries_offset + 1)) 3049300207Sken && (le64dec(softc->ata_iddir.header) == 3050300207Sken ATA_IDLOG_REVISION) 3051300207Sken && (softc->ata_iddir.entry_count > 0)) { 3052300207Sken int num_entries, i; 3053300207Sken 3054300207Sken num_entries = softc->ata_iddir.entry_count; 3055300207Sken num_entries = min(num_entries, 3056300207Sken softc->valid_iddir_len - entries_offset); 3057300207Sken for (i = 0; i < num_entries && 3058300207Sken i < max_entries; i++) { 3059300207Sken if (softc->ata_iddir.entries[i] == 3060300207Sken ATA_IDL_SUP_CAP) 3061300207Sken softc->flags |= 3062300207Sken ADA_FLAG_CAN_SUPCAP; 3063300207Sken else if (softc->ata_iddir.entries[i]== 3064300207Sken ATA_IDL_ZDI) 3065300207Sken softc->flags |= 3066300207Sken ADA_FLAG_CAN_ZONE; 3067300207Sken 3068300207Sken if ((softc->flags & 3069300207Sken ADA_FLAG_CAN_SUPCAP) 3070300207Sken && (softc->flags & 3071300207Sken ADA_FLAG_CAN_ZONE)) 3072300207Sken break; 3073300207Sken } 3074300207Sken } 3075300207Sken } else { 3076300207Sken error = adaerror(done_ccb, CAM_RETRY_SELTO, 3077300207Sken SF_RETRY_UA|SF_NO_PRINT); 3078300207Sken if (error == ERESTART) 3079300207Sken return; 3080300207Sken else if (error != 0) { 3081300207Sken /* 3082300207Sken * If we can't get the ATA Identify Data log 3083300207Sken * directory, then it effectively isn't 3084300207Sken * supported even if the ATA Log directory 3085300207Sken * a non-zero number of pages present for 3086300207Sken * this log. 3087300207Sken */ 3088300207Sken softc->flags &= ~ADA_FLAG_CAN_IDLOG; 3089300207Sken if ((done_ccb->ccb_h.status & 3090300207Sken CAM_DEV_QFRZN) != 0) { 3091300207Sken /* Don't wedge this device's queue */ 3092300207Sken cam_release_devq(done_ccb->ccb_h.path, 3093300207Sken /*relsim_flags*/0, 3094300207Sken /*reduction*/0, 3095300207Sken /*timeout*/0, 3096300207Sken /*getcount_only*/0); 3097300207Sken } 3098300207Sken } 3099300207Sken } 3100300207Sken 3101300207Sken free(ataio->data_ptr, M_ATADA); 3102300207Sken 3103300207Sken if ((error == 0) 3104300207Sken && (softc->flags & ADA_FLAG_CAN_SUPCAP)) { 3105300207Sken softc->state = ADA_STATE_SUP_CAP; 3106300207Sken xpt_release_ccb(done_ccb); 3107300207Sken xpt_schedule(periph, priority); 3108300207Sken } else 3109300207Sken adaprobedone(periph, done_ccb); 3110300207Sken return; 3111300207Sken } 3112300207Sken case ADA_CCB_SUP_CAP: { 3113300207Sken int error; 3114300207Sken 3115300207Sken if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 3116300207Sken uint32_t valid_len; 3117300207Sken size_t needed_size; 3118300207Sken struct ata_identify_log_sup_cap *sup_cap; 3119300207Sken error = 0; 3120300207Sken 3121300207Sken sup_cap = (struct ata_identify_log_sup_cap *) 3122300207Sken ataio->data_ptr; 3123300207Sken valid_len = ataio->dxfer_len - ataio->resid; 3124300207Sken needed_size = 3125300207Sken __offsetof(struct ata_identify_log_sup_cap, 3126300207Sken sup_zac_cap) + 1 + sizeof(sup_cap->sup_zac_cap); 3127300207Sken if (valid_len >= needed_size) { 3128300207Sken uint64_t zoned, zac_cap; 3129300207Sken 3130300207Sken zoned = le64dec(sup_cap->zoned_cap); 3131300207Sken if (zoned & ATA_ZONED_VALID) { 3132300207Sken /* 3133300207Sken * This should have already been 3134300207Sken * set, because this is also in the 3135300207Sken * ATA identify data. 3136300207Sken */ 3137300207Sken if ((zoned & ATA_ZONED_MASK) == 3138300207Sken ATA_SUPPORT_ZONE_HOST_AWARE) 3139300207Sken softc->zone_mode = 3140300207Sken ADA_ZONE_HOST_AWARE; 3141300207Sken else if ((zoned & ATA_ZONED_MASK) == 3142300207Sken ATA_SUPPORT_ZONE_DEV_MANAGED) 3143300207Sken softc->zone_mode = 3144300207Sken ADA_ZONE_DRIVE_MANAGED; 3145300207Sken } 3146300207Sken 3147300207Sken zac_cap = le64dec(sup_cap->sup_zac_cap); 3148300207Sken if (zac_cap & ATA_SUP_ZAC_CAP_VALID) { 3149300207Sken if (zac_cap & ATA_REPORT_ZONES_SUP) 3150300207Sken softc->zone_flags |= 3151300207Sken ADA_ZONE_FLAG_RZ_SUP; 3152300207Sken if (zac_cap & ATA_ND_OPEN_ZONE_SUP) 3153300207Sken softc->zone_flags |= 3154300207Sken ADA_ZONE_FLAG_OPEN_SUP; 3155300207Sken if (zac_cap & ATA_ND_CLOSE_ZONE_SUP) 3156300207Sken softc->zone_flags |= 3157300207Sken ADA_ZONE_FLAG_CLOSE_SUP; 3158300207Sken if (zac_cap & ATA_ND_FINISH_ZONE_SUP) 3159300207Sken softc->zone_flags |= 3160300207Sken ADA_ZONE_FLAG_FINISH_SUP; 3161300207Sken if (zac_cap & ATA_ND_RWP_SUP) 3162300207Sken softc->zone_flags |= 3163300207Sken ADA_ZONE_FLAG_RWP_SUP; 3164300207Sken } else { 3165300207Sken /* 3166300207Sken * This field was introduced in 3167300207Sken * ACS-4, r08 on April 28th, 2015. 3168300207Sken * If the drive firmware was written 3169300207Sken * to an earlier spec, it won't have 3170300207Sken * the field. So, assume all 3171300207Sken * commands are supported. 3172300207Sken */ 3173300207Sken softc->zone_flags |= 3174300207Sken ADA_ZONE_FLAG_SUP_MASK; 3175300207Sken } 3176300207Sken 3177300207Sken } 3178300207Sken } else { 3179300207Sken error = adaerror(done_ccb, CAM_RETRY_SELTO, 3180300207Sken SF_RETRY_UA|SF_NO_PRINT); 3181300207Sken if (error == ERESTART) 3182300207Sken return; 3183300207Sken else if (error != 0) { 3184300207Sken /* 3185300207Sken * If we can't get the ATA Identify Data 3186300207Sken * Supported Capabilities page, clear the 3187300207Sken * flag... 3188300207Sken */ 3189300207Sken softc->flags &= ~ADA_FLAG_CAN_SUPCAP; 3190300207Sken /* 3191300207Sken * And clear zone capabilities. 3192300207Sken */ 3193300207Sken softc->zone_flags &= ~ADA_ZONE_FLAG_SUP_MASK; 3194300207Sken if ((done_ccb->ccb_h.status & 3195300207Sken CAM_DEV_QFRZN) != 0) { 3196300207Sken /* Don't wedge this device's queue */ 3197300207Sken cam_release_devq(done_ccb->ccb_h.path, 3198300207Sken /*relsim_flags*/0, 3199300207Sken /*reduction*/0, 3200300207Sken /*timeout*/0, 3201300207Sken /*getcount_only*/0); 3202300207Sken } 3203300207Sken } 3204300207Sken } 3205300207Sken 3206300207Sken free(ataio->data_ptr, M_ATADA); 3207300207Sken 3208300207Sken if ((error == 0) 3209300207Sken && (softc->flags & ADA_FLAG_CAN_ZONE)) { 3210300207Sken softc->state = ADA_STATE_ZONE; 3211300207Sken xpt_release_ccb(done_ccb); 3212300207Sken xpt_schedule(periph, priority); 3213300207Sken } else 3214300207Sken adaprobedone(periph, done_ccb); 3215300207Sken return; 3216300207Sken } 3217300207Sken case ADA_CCB_ZONE: { 3218300207Sken int error; 3219300207Sken 3220300207Sken if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 3221300207Sken struct ata_zoned_info_log *zi_log; 3222300207Sken uint32_t valid_len; 3223300207Sken size_t needed_size; 3224300207Sken 3225300207Sken zi_log = (struct ata_zoned_info_log *)ataio->data_ptr; 3226300207Sken 3227300207Sken valid_len = ataio->dxfer_len - ataio->resid; 3228300207Sken needed_size = __offsetof(struct ata_zoned_info_log, 3229300207Sken version_info) + 1 + sizeof(zi_log->version_info); 3230300207Sken if (valid_len >= needed_size) { 3231300207Sken uint64_t tmpvar; 3232300207Sken 3233300207Sken tmpvar = le64dec(zi_log->zoned_cap); 3234300207Sken if (tmpvar & ATA_ZDI_CAP_VALID) { 3235300207Sken if (tmpvar & ATA_ZDI_CAP_URSWRZ) 3236300207Sken softc->zone_flags |= 3237300207Sken ADA_ZONE_FLAG_URSWRZ; 3238300207Sken else 3239300207Sken softc->zone_flags &= 3240300207Sken ~ADA_ZONE_FLAG_URSWRZ; 3241300207Sken } 3242300207Sken tmpvar = le64dec(zi_log->optimal_seq_zones); 3243300207Sken if (tmpvar & ATA_ZDI_OPT_SEQ_VALID) { 3244300207Sken softc->zone_flags |= 3245300207Sken ADA_ZONE_FLAG_OPT_SEQ_SET; 3246300207Sken softc->optimal_seq_zones = (tmpvar & 3247300207Sken ATA_ZDI_OPT_SEQ_MASK); 3248300207Sken } else { 3249300207Sken softc->zone_flags &= 3250300207Sken ~ADA_ZONE_FLAG_OPT_SEQ_SET; 3251300207Sken softc->optimal_seq_zones = 0; 3252300207Sken } 3253300207Sken 3254300207Sken tmpvar =le64dec(zi_log->optimal_nonseq_zones); 3255300207Sken if (tmpvar & ATA_ZDI_OPT_NS_VALID) { 3256300207Sken softc->zone_flags |= 3257300207Sken ADA_ZONE_FLAG_OPT_NONSEQ_SET; 3258300207Sken softc->optimal_nonseq_zones = 3259300207Sken (tmpvar & ATA_ZDI_OPT_NS_MASK); 3260300207Sken } else { 3261300207Sken softc->zone_flags &= 3262300207Sken ~ADA_ZONE_FLAG_OPT_NONSEQ_SET; 3263300207Sken softc->optimal_nonseq_zones = 0; 3264300207Sken } 3265300207Sken 3266300207Sken tmpvar = le64dec(zi_log->max_seq_req_zones); 3267300207Sken if (tmpvar & ATA_ZDI_MAX_SEQ_VALID) { 3268300207Sken softc->zone_flags |= 3269300207Sken ADA_ZONE_FLAG_MAX_SEQ_SET; 3270300207Sken softc->max_seq_zones = 3271300207Sken (tmpvar & ATA_ZDI_MAX_SEQ_MASK); 3272300207Sken } else { 3273300207Sken softc->zone_flags &= 3274300207Sken ~ADA_ZONE_FLAG_MAX_SEQ_SET; 3275300207Sken softc->max_seq_zones = 0; 3276300207Sken } 3277300207Sken } 3278300207Sken } else { 3279300207Sken error = adaerror(done_ccb, CAM_RETRY_SELTO, 3280300207Sken SF_RETRY_UA|SF_NO_PRINT); 3281300207Sken if (error == ERESTART) 3282300207Sken return; 3283300207Sken else if (error != 0) { 3284300207Sken softc->flags &= ~ADA_FLAG_CAN_ZONE; 3285300207Sken softc->flags &= ~ADA_ZONE_FLAG_SET_MASK; 3286300207Sken 3287300207Sken if ((done_ccb->ccb_h.status & 3288300207Sken CAM_DEV_QFRZN) != 0) { 3289300207Sken /* Don't wedge this device's queue */ 3290300207Sken cam_release_devq(done_ccb->ccb_h.path, 3291300207Sken /*relsim_flags*/0, 3292300207Sken /*reduction*/0, 3293300207Sken /*timeout*/0, 3294300207Sken /*getcount_only*/0); 3295300207Sken } 3296300207Sken } 3297300207Sken 3298300207Sken } 3299300207Sken free(ataio->data_ptr, M_ATADA); 3300300207Sken 3301300207Sken adaprobedone(periph, done_ccb); 3302300207Sken return; 3303300207Sken } 3304195534Sscottl case ADA_CCB_DUMP: 3305195534Sscottl /* No-op. We're polling */ 3306195534Sscottl return; 3307195534Sscottl default: 3308195534Sscottl break; 3309195534Sscottl } 3310195534Sscottl xpt_release_ccb(done_ccb); 3311195534Sscottl} 3312195534Sscottl 3313195534Sscottlstatic int 3314195534Sscottladaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 3315195534Sscottl{ 3316298037Simp#ifdef CAM_IO_STATS 3317298002Simp struct ada_softc *softc; 3318298002Simp struct cam_periph *periph; 3319195534Sscottl 3320298002Simp periph = xpt_path_periph(ccb->ccb_h.path); 3321298002Simp softc = (struct ada_softc *)periph->softc; 3322298002Simp 3323298002Simp switch (ccb->ccb_h.status & CAM_STATUS_MASK) { 3324298002Simp case CAM_CMD_TIMEOUT: 3325298002Simp softc->timeouts++; 3326298002Simp break; 3327298002Simp case CAM_REQ_ABORTED: 3328298002Simp case CAM_REQ_CMP_ERR: 3329298002Simp case CAM_REQ_TERMIO: 3330298002Simp case CAM_UNREC_HBA_ERROR: 3331298002Simp case CAM_DATA_RUN_ERR: 3332298002Simp case CAM_ATA_STATUS_ERROR: 3333298002Simp softc->errors++; 3334298002Simp break; 3335298002Simp default: 3336298002Simp break; 3337298002Simp } 3338298037Simp#endif 3339298002Simp 3340203385Smav return(cam_periph_error(ccb, cam_flags, sense_flags, NULL)); 3341195534Sscottl} 3342195534Sscottl 3343195534Sscottlstatic void 3344198897Smavadagetparams(struct cam_periph *periph, struct ccb_getdev *cgd) 3345195534Sscottl{ 3346195534Sscottl struct ada_softc *softc = (struct ada_softc *)periph->softc; 3347195534Sscottl struct disk_params *dp = &softc->params; 3348195534Sscottl u_int64_t lbasize48; 3349195534Sscottl u_int32_t lbasize; 3350195534Sscottl 3351198897Smav dp->secsize = ata_logical_sector_size(&cgd->ident_data); 3352195534Sscottl if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) && 3353195534Sscottl cgd->ident_data.current_heads && cgd->ident_data.current_sectors) { 3354195534Sscottl dp->heads = cgd->ident_data.current_heads; 3355195534Sscottl dp->secs_per_track = cgd->ident_data.current_sectors; 3356195534Sscottl dp->cylinders = cgd->ident_data.cylinders; 3357195534Sscottl dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 | 3358195534Sscottl ((u_int32_t)cgd->ident_data.current_size_2 << 16); 3359195534Sscottl } else { 3360195534Sscottl dp->heads = cgd->ident_data.heads; 3361195534Sscottl dp->secs_per_track = cgd->ident_data.sectors; 3362195534Sscottl dp->cylinders = cgd->ident_data.cylinders; 3363327849Sasomers dp->sectors = cgd->ident_data.cylinders * 3364327849Sasomers (u_int32_t)(dp->heads * dp->secs_per_track); 3365195534Sscottl } 3366195534Sscottl lbasize = (u_int32_t)cgd->ident_data.lba_size_1 | 3367195534Sscottl ((u_int32_t)cgd->ident_data.lba_size_2 << 16); 3368195534Sscottl 3369195534Sscottl /* use the 28bit LBA size if valid or bigger than the CHS mapping */ 3370195534Sscottl if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize) 3371195534Sscottl dp->sectors = lbasize; 3372195534Sscottl 3373195534Sscottl /* use the 48bit LBA size if valid */ 3374195534Sscottl lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) | 3375195534Sscottl ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) | 3376195534Sscottl ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) | 3377195534Sscottl ((u_int64_t)cgd->ident_data.lba_size48_4 << 48); 3378195534Sscottl if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) && 3379195534Sscottl lbasize48 > ATA_MAX_28BIT_LBA) 3380195534Sscottl dp->sectors = lbasize48; 3381195534Sscottl} 3382195534Sscottl 3383195534Sscottlstatic void 3384195534Sscottladasendorderedtag(void *arg) 3385195534Sscottl{ 3386195534Sscottl struct ada_softc *softc = arg; 3387195534Sscottl 3388195534Sscottl if (ada_send_ordered) { 3389257054Smav if (softc->outstanding_cmds > 0) { 3390257054Smav if ((softc->flags & ADA_FLAG_WAS_OTAG) == 0) 3391257054Smav softc->flags |= ADA_FLAG_NEED_OTAG; 3392257054Smav softc->flags &= ~ADA_FLAG_WAS_OTAG; 3393195534Sscottl } 3394195534Sscottl } 3395195534Sscottl /* Queue us up again */ 3396195534Sscottl callout_reset(&softc->sendordered_c, 3397230921Smav (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 3398195534Sscottl adasendorderedtag, softc); 3399195534Sscottl} 3400195534Sscottl 3401195534Sscottl/* 3402195534Sscottl * Step through all ADA peripheral drivers, and if the device is still open, 3403195534Sscottl * sync the disk cache to physical media. 3404195534Sscottl */ 3405195534Sscottlstatic void 3406220650Smavadaflush(void) 3407195534Sscottl{ 3408195534Sscottl struct cam_periph *periph; 3409195534Sscottl struct ada_softc *softc; 3410248872Smav union ccb *ccb; 3411236814Smav int error; 3412195534Sscottl 3413248868Smav CAM_PERIPH_FOREACH(periph, &adadriver) { 3414251792Smav softc = (struct ada_softc *)periph->softc; 3415251792Smav if (SCHEDULER_STOPPED()) { 3416251792Smav /* If we paniced with the lock held, do not recurse. */ 3417251792Smav if (!cam_periph_owned(periph) && 3418251792Smav (softc->flags & ADA_FLAG_OPEN)) { 3419251792Smav adadump(softc->disk, NULL, 0, 0, 0); 3420251792Smav } 3421200180Smav continue; 3422251792Smav } 3423195534Sscottl cam_periph_lock(periph); 3424195534Sscottl /* 3425195534Sscottl * We only sync the cache if the drive is still open, and 3426195534Sscottl * if the drive is capable of it.. 3427195534Sscottl */ 3428195534Sscottl if (((softc->flags & ADA_FLAG_OPEN) == 0) || 3429195534Sscottl (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) { 3430195534Sscottl cam_periph_unlock(periph); 3431195534Sscottl continue; 3432195534Sscottl } 3433195534Sscottl 3434248872Smav ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3435248872Smav cam_fill_ataio(&ccb->ataio, 3436236814Smav 0, 3437195534Sscottl adadone, 3438195534Sscottl CAM_DIR_NONE, 3439195534Sscottl 0, 3440195534Sscottl NULL, 3441195534Sscottl 0, 3442195534Sscottl ada_default_timeout*1000); 3443195534Sscottl if (softc->flags & ADA_FLAG_CAN_48BIT) 3444248872Smav ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 3445195534Sscottl else 3446248872Smav ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 3447195534Sscottl 3448248872Smav error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 3449248872Smav /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 3450248872Smav softc->disk->d_devstat); 3451236814Smav if (error != 0) 3452195534Sscottl xpt_print(periph->path, "Synchronize cache failed\n"); 3453249048Smav xpt_release_ccb(ccb); 3454195534Sscottl cam_periph_unlock(periph); 3455195534Sscottl } 3456220650Smav} 3457214279Sbrucec 3458220650Smavstatic void 3459220650Smavadaspindown(uint8_t cmd, int flags) 3460220650Smav{ 3461220650Smav struct cam_periph *periph; 3462220650Smav struct ada_softc *softc; 3463248872Smav union ccb *ccb; 3464236814Smav int error; 3465214279Sbrucec 3466248868Smav CAM_PERIPH_FOREACH(periph, &adadriver) { 3467214279Sbrucec /* If we paniced with lock held - not recurse here. */ 3468214279Sbrucec if (cam_periph_owned(periph)) 3469214279Sbrucec continue; 3470214279Sbrucec cam_periph_lock(periph); 3471214279Sbrucec softc = (struct ada_softc *)periph->softc; 3472214279Sbrucec /* 3473214279Sbrucec * We only spin-down the drive if it is capable of it.. 3474214279Sbrucec */ 3475214279Sbrucec if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 3476214279Sbrucec cam_periph_unlock(periph); 3477214279Sbrucec continue; 3478214279Sbrucec } 3479214279Sbrucec 3480214279Sbrucec if (bootverbose) 3481214279Sbrucec xpt_print(periph->path, "spin-down\n"); 3482214279Sbrucec 3483248872Smav ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3484248872Smav cam_fill_ataio(&ccb->ataio, 3485236814Smav 0, 3486214279Sbrucec adadone, 3487220650Smav CAM_DIR_NONE | flags, 3488214279Sbrucec 0, 3489214279Sbrucec NULL, 3490214279Sbrucec 0, 3491214279Sbrucec ada_default_timeout*1000); 3492248872Smav ata_28bit_cmd(&ccb->ataio, cmd, 0, 0, 0); 3493214279Sbrucec 3494248872Smav error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 3495248872Smav /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 3496248872Smav softc->disk->d_devstat); 3497236814Smav if (error != 0) 3498214279Sbrucec xpt_print(periph->path, "Spin-down disk failed\n"); 3499249048Smav xpt_release_ccb(ccb); 3500214279Sbrucec cam_periph_unlock(periph); 3501214279Sbrucec } 3502195534Sscottl} 3503195534Sscottl 3504220650Smavstatic void 3505220650Smavadashutdown(void *arg, int howto) 3506220650Smav{ 3507220650Smav 3508220650Smav adaflush(); 3509220650Smav if (ada_spindown_shutdown != 0 && 3510220650Smav (howto & (RB_HALT | RB_POWEROFF)) != 0) 3511220650Smav adaspindown(ATA_STANDBY_IMMEDIATE, 0); 3512220650Smav} 3513220650Smav 3514220650Smavstatic void 3515220650Smavadasuspend(void *arg) 3516220650Smav{ 3517220650Smav 3518220650Smav adaflush(); 3519220650Smav if (ada_spindown_suspend != 0) 3520220650Smav adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE); 3521220650Smav} 3522220650Smav 3523220650Smavstatic void 3524220650Smavadaresume(void *arg) 3525220650Smav{ 3526220650Smav struct cam_periph *periph; 3527220650Smav struct ada_softc *softc; 3528220650Smav 3529220650Smav if (ada_spindown_suspend == 0) 3530220650Smav return; 3531220650Smav 3532248868Smav CAM_PERIPH_FOREACH(periph, &adadriver) { 3533220650Smav cam_periph_lock(periph); 3534220650Smav softc = (struct ada_softc *)periph->softc; 3535220650Smav /* 3536220650Smav * We only spin-down the drive if it is capable of it.. 3537220650Smav */ 3538220650Smav if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 3539220650Smav cam_periph_unlock(periph); 3540220650Smav continue; 3541220650Smav } 3542220650Smav 3543220650Smav if (bootverbose) 3544220650Smav xpt_print(periph->path, "resume\n"); 3545220650Smav 3546220650Smav /* 3547220650Smav * Drop freeze taken due to CAM_DEV_QFREEZE flag set on 3548220650Smav * sleep request. 3549220650Smav */ 3550220650Smav cam_release_devq(periph->path, 3551220650Smav /*relsim_flags*/0, 3552220650Smav /*openings*/0, 3553220650Smav /*timeout*/0, 3554220650Smav /*getcount_only*/0); 3555220650Smav 3556220650Smav cam_periph_unlock(periph); 3557220650Smav } 3558220650Smav} 3559220650Smav 3560195534Sscottl#endif /* _KERNEL */ 3561