ata_da.c revision 236639
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: head/sys/cam/ata/ata_da.c 236639 2012-06-05 20:39:12Z mav $"); 29195534Sscottl 30220454Smav#include "opt_ada.h" 31221071Smav#include "opt_ata.h" 32220454Smav 33195534Sscottl#include <sys/param.h> 34195534Sscottl 35195534Sscottl#ifdef _KERNEL 36195534Sscottl#include <sys/systm.h> 37195534Sscottl#include <sys/kernel.h> 38195534Sscottl#include <sys/bio.h> 39195534Sscottl#include <sys/sysctl.h> 40195534Sscottl#include <sys/taskqueue.h> 41195534Sscottl#include <sys/lock.h> 42195534Sscottl#include <sys/mutex.h> 43195534Sscottl#include <sys/conf.h> 44195534Sscottl#include <sys/devicestat.h> 45195534Sscottl#include <sys/eventhandler.h> 46195534Sscottl#include <sys/malloc.h> 47195534Sscottl#include <sys/cons.h> 48214279Sbrucec#include <sys/reboot.h> 49195534Sscottl#include <geom/geom_disk.h> 50195534Sscottl#endif /* _KERNEL */ 51195534Sscottl 52195534Sscottl#ifndef _KERNEL 53195534Sscottl#include <stdio.h> 54195534Sscottl#include <string.h> 55195534Sscottl#endif /* _KERNEL */ 56195534Sscottl 57195534Sscottl#include <cam/cam.h> 58195534Sscottl#include <cam/cam_ccb.h> 59195534Sscottl#include <cam/cam_periph.h> 60195534Sscottl#include <cam/cam_xpt_periph.h> 61195534Sscottl#include <cam/cam_sim.h> 62195534Sscottl 63195534Sscottl#include <cam/ata/ata_all.h> 64195534Sscottl 65208349Smarius#include <machine/md_var.h> /* geometry translation */ 66208349Smarius 67195534Sscottl#ifdef _KERNEL 68195534Sscottl 69195534Sscottl#define ATA_MAX_28BIT_LBA 268435455UL 70195534Sscottl 71195534Sscottltypedef enum { 72224497Smav ADA_STATE_RAHEAD, 73220412Smav ADA_STATE_WCACHE, 74198708Smav ADA_STATE_NORMAL 75195534Sscottl} ada_state; 76195534Sscottl 77195534Sscottltypedef enum { 78195534Sscottl ADA_FLAG_PACK_INVALID = 0x001, 79195534Sscottl ADA_FLAG_CAN_48BIT = 0x002, 80195534Sscottl ADA_FLAG_CAN_FLUSHCACHE = 0x004, 81198328Smav ADA_FLAG_CAN_NCQ = 0x008, 82198328Smav ADA_FLAG_CAN_DMA = 0x010, 83195534Sscottl ADA_FLAG_NEED_OTAG = 0x020, 84195534Sscottl ADA_FLAG_WENT_IDLE = 0x040, 85201139Smav ADA_FLAG_CAN_TRIM = 0x080, 86195534Sscottl ADA_FLAG_OPEN = 0x100, 87201139Smav ADA_FLAG_SCTX_INIT = 0x200, 88214279Sbrucec ADA_FLAG_CAN_CFA = 0x400, 89214279Sbrucec ADA_FLAG_CAN_POWERMGT = 0x800 90195534Sscottl} ada_flags; 91195534Sscottl 92195534Sscottltypedef enum { 93222520Smav ADA_Q_NONE = 0x00, 94222520Smav ADA_Q_4K = 0x01, 95195534Sscottl} ada_quirks; 96195534Sscottl 97195534Sscottltypedef enum { 98224497Smav ADA_CCB_RAHEAD = 0x01, 99224497Smav ADA_CCB_WCACHE = 0x02, 100195534Sscottl ADA_CCB_BUFFER_IO = 0x03, 101195534Sscottl ADA_CCB_WAITING = 0x04, 102195534Sscottl ADA_CCB_DUMP = 0x05, 103201139Smav ADA_CCB_TRIM = 0x06, 104195534Sscottl ADA_CCB_TYPE_MASK = 0x0F, 105195534Sscottl} ada_ccb_state; 106195534Sscottl 107195534Sscottl/* Offsets into our private area for storing information */ 108195534Sscottl#define ccb_state ppriv_field0 109195534Sscottl#define ccb_bp ppriv_ptr1 110195534Sscottl 111195534Sscottlstruct disk_params { 112195534Sscottl u_int8_t heads; 113198897Smav u_int8_t secs_per_track; 114195534Sscottl u_int32_t cylinders; 115198897Smav u_int32_t secsize; /* Number of bytes/logical sector */ 116198897Smav u_int64_t sectors; /* Total number sectors */ 117195534Sscottl}; 118195534Sscottl 119222643Smav#define TRIM_MAX_BLOCKS 8 120222628Smav#define TRIM_MAX_RANGES (TRIM_MAX_BLOCKS * 64) 121222643Smav#define TRIM_MAX_BIOS (TRIM_MAX_RANGES * 4) 122201139Smavstruct trim_request { 123201139Smav uint8_t data[TRIM_MAX_RANGES * 8]; 124222628Smav struct bio *bps[TRIM_MAX_BIOS]; 125201139Smav}; 126201139Smav 127195534Sscottlstruct ada_softc { 128195534Sscottl struct bio_queue_head bio_queue; 129201139Smav struct bio_queue_head trim_queue; 130195534Sscottl ada_state state; 131195534Sscottl ada_flags flags; 132195534Sscottl ada_quirks quirks; 133195534Sscottl int ordered_tag_count; 134195534Sscottl int outstanding_cmds; 135201139Smav int trim_max_ranges; 136201139Smav int trim_running; 137224497Smav int read_ahead; 138220454Smav int write_cache; 139220454Smav#ifdef ADA_TEST_FAILURE 140220454Smav int force_read_error; 141220454Smav int force_write_error; 142220454Smav int periodic_read_error; 143220454Smav int periodic_read_count; 144220454Smav#endif 145195534Sscottl struct disk_params params; 146195534Sscottl struct disk *disk; 147195534Sscottl struct task sysctl_task; 148195534Sscottl struct sysctl_ctx_list sysctl_ctx; 149195534Sscottl struct sysctl_oid *sysctl_tree; 150195534Sscottl struct callout sendordered_c; 151201139Smav struct trim_request trim_req; 152195534Sscottl}; 153195534Sscottl 154195534Sscottlstruct ada_quirk_entry { 155195534Sscottl struct scsi_inquiry_pattern inq_pat; 156195534Sscottl ada_quirks quirks; 157195534Sscottl}; 158195534Sscottl 159199178Smavstatic struct ada_quirk_entry ada_quirk_table[] = 160199178Smav{ 161199178Smav { 162222520Smav /* Hitachi Advanced Format (4k) drives */ 163222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" }, 164222520Smav /*quirks*/ADA_Q_4K 165222520Smav }, 166222520Smav { 167222520Smav /* Samsung Advanced Format (4k) drives */ 168228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD155UI*", "*" }, 169228819Smav /*quirks*/ADA_Q_4K 170228819Smav }, 171228819Smav { 172228819Smav /* Samsung Advanced Format (4k) drives */ 173222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" }, 174222520Smav /*quirks*/ADA_Q_4K 175222520Smav }, 176222520Smav { 177222520Smav /* Seagate Barracuda Green Advanced Format (4k) drives */ 178222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" }, 179222520Smav /*quirks*/ADA_Q_4K 180222520Smav }, 181222520Smav { 182228819Smav /* Seagate Barracuda Advanced Format (4k) drives */ 183228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???DM*", "*" }, 184228819Smav /*quirks*/ADA_Q_4K 185228819Smav }, 186228819Smav { 187228819Smav /* Seagate Barracuda Advanced Format (4k) drives */ 188228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DM*", "*" }, 189228819Smav /*quirks*/ADA_Q_4K 190228819Smav }, 191228819Smav { 192222520Smav /* Seagate Momentus Advanced Format (4k) drives */ 193222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" }, 194222520Smav /*quirks*/ADA_Q_4K 195222520Smav }, 196222520Smav { 197222520Smav /* Seagate Momentus Advanced Format (4k) drives */ 198222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" }, 199222520Smav /*quirks*/ADA_Q_4K 200222520Smav }, 201222520Smav { 202222520Smav /* Seagate Momentus Advanced Format (4k) drives */ 203228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640423AS*", "*" }, 204228819Smav /*quirks*/ADA_Q_4K 205228819Smav }, 206228819Smav { 207228819Smav /* Seagate Momentus Advanced Format (4k) drives */ 208228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640424AS*", "*" }, 209228819Smav /*quirks*/ADA_Q_4K 210228819Smav }, 211228819Smav { 212228819Smav /* Seagate Momentus Advanced Format (4k) drives */ 213222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" }, 214222520Smav /*quirks*/ADA_Q_4K 215222520Smav }, 216222520Smav { 217222520Smav /* Seagate Momentus Advanced Format (4k) drives */ 218222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" }, 219222520Smav /*quirks*/ADA_Q_4K 220222520Smav }, 221222520Smav { 222228819Smav /* Seagate Momentus Advanced Format (4k) drives */ 223228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750423AS*", "*" }, 224228819Smav /*quirks*/ADA_Q_4K 225228819Smav }, 226228819Smav { 227222520Smav /* Seagate Momentus Thin Advanced Format (4k) drives */ 228222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" }, 229222520Smav /*quirks*/ADA_Q_4K 230222520Smav }, 231222520Smav { 232222520Smav /* WDC Caviar Green Advanced Format (4k) drives */ 233222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" }, 234222520Smav /*quirks*/ADA_Q_4K 235222520Smav }, 236222520Smav { 237222520Smav /* WDC Caviar Green Advanced Format (4k) drives */ 238222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" }, 239222520Smav /*quirks*/ADA_Q_4K 240222520Smav }, 241222520Smav { 242222520Smav /* WDC Caviar Green Advanced Format (4k) drives */ 243222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" }, 244222520Smav /*quirks*/ADA_Q_4K 245222520Smav }, 246222520Smav { 247222520Smav /* WDC Caviar Green Advanced Format (4k) drives */ 248222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" }, 249222520Smav /*quirks*/ADA_Q_4K 250222520Smav }, 251222520Smav { 252222520Smav /* WDC Scorpio Black Advanced Format (4k) drives */ 253222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" }, 254222520Smav /*quirks*/ADA_Q_4K 255222520Smav }, 256222520Smav { 257222520Smav /* WDC Scorpio Black Advanced Format (4k) drives */ 258222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" }, 259222520Smav /*quirks*/ADA_Q_4K 260222520Smav }, 261222520Smav { 262222520Smav /* WDC Scorpio Blue Advanced Format (4k) drives */ 263222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" }, 264222520Smav /*quirks*/ADA_Q_4K 265222520Smav }, 266222520Smav { 267222520Smav /* WDC Scorpio Blue Advanced Format (4k) drives */ 268222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" }, 269222520Smav /*quirks*/ADA_Q_4K 270222520Smav }, 271222520Smav { 272199178Smav /* Default */ 273199178Smav { 274199178Smav T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, 275199178Smav /*vendor*/"*", /*product*/"*", /*revision*/"*" 276199178Smav }, 277199178Smav /*quirks*/0 278199178Smav }, 279199178Smav}; 280195534Sscottl 281195534Sscottlstatic disk_strategy_t adastrategy; 282195534Sscottlstatic dumper_t adadump; 283195534Sscottlstatic periph_init_t adainit; 284195534Sscottlstatic void adaasync(void *callback_arg, u_int32_t code, 285195534Sscottl struct cam_path *path, void *arg); 286195534Sscottlstatic void adasysctlinit(void *context, int pending); 287195534Sscottlstatic periph_ctor_t adaregister; 288195534Sscottlstatic periph_dtor_t adacleanup; 289195534Sscottlstatic periph_start_t adastart; 290195534Sscottlstatic periph_oninv_t adaoninvalidate; 291195534Sscottlstatic void adadone(struct cam_periph *periph, 292195534Sscottl union ccb *done_ccb); 293195534Sscottlstatic int adaerror(union ccb *ccb, u_int32_t cam_flags, 294195534Sscottl u_int32_t sense_flags); 295198897Smavstatic void adagetparams(struct cam_periph *periph, 296195534Sscottl struct ccb_getdev *cgd); 297195534Sscottlstatic timeout_t adasendorderedtag; 298195534Sscottlstatic void adashutdown(void *arg, int howto); 299220650Smavstatic void adasuspend(void *arg); 300220650Smavstatic void adaresume(void *arg); 301195534Sscottl 302221071Smav#ifndef ADA_DEFAULT_LEGACY_ALIASES 303221071Smav#ifdef ATA_CAM 304221071Smav#define ADA_DEFAULT_LEGACY_ALIASES 1 305221071Smav#else 306221071Smav#define ADA_DEFAULT_LEGACY_ALIASES 0 307221071Smav#endif 308221071Smav#endif 309221071Smav 310195534Sscottl#ifndef ADA_DEFAULT_TIMEOUT 311195534Sscottl#define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */ 312195534Sscottl#endif 313195534Sscottl 314195534Sscottl#ifndef ADA_DEFAULT_RETRY 315195534Sscottl#define ADA_DEFAULT_RETRY 4 316195534Sscottl#endif 317195534Sscottl 318195534Sscottl#ifndef ADA_DEFAULT_SEND_ORDERED 319195534Sscottl#define ADA_DEFAULT_SEND_ORDERED 1 320195534Sscottl#endif 321195534Sscottl 322214279Sbrucec#ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN 323214279Sbrucec#define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1 324214279Sbrucec#endif 325214279Sbrucec 326220650Smav#ifndef ADA_DEFAULT_SPINDOWN_SUSPEND 327220650Smav#define ADA_DEFAULT_SPINDOWN_SUSPEND 1 328220650Smav#endif 329220650Smav 330224497Smav#ifndef ADA_DEFAULT_READ_AHEAD 331224497Smav#define ADA_DEFAULT_READ_AHEAD 1 332224497Smav#endif 333224497Smav 334220412Smav#ifndef ADA_DEFAULT_WRITE_CACHE 335220412Smav#define ADA_DEFAULT_WRITE_CACHE 1 336220412Smav#endif 337220412Smav 338224497Smav#define ADA_RA (softc->read_ahead >= 0 ? \ 339224497Smav softc->read_ahead : ada_read_ahead) 340224497Smav#define ADA_WC (softc->write_cache >= 0 ? \ 341224497Smav softc->write_cache : ada_write_cache) 342224497Smav 343208349Smarius/* 344208349Smarius * Most platforms map firmware geometry to actual, but some don't. If 345208349Smarius * not overridden, default to nothing. 346208349Smarius */ 347208349Smarius#ifndef ata_disk_firmware_geom_adjust 348208349Smarius#define ata_disk_firmware_geom_adjust(disk) 349208349Smarius#endif 350195534Sscottl 351221071Smavstatic int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES; 352195534Sscottlstatic int ada_retry_count = ADA_DEFAULT_RETRY; 353195534Sscottlstatic int ada_default_timeout = ADA_DEFAULT_TIMEOUT; 354195534Sscottlstatic int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED; 355214279Sbrucecstatic int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN; 356220650Smavstatic int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND; 357224497Smavstatic int ada_read_ahead = ADA_DEFAULT_READ_AHEAD; 358220412Smavstatic int ada_write_cache = ADA_DEFAULT_WRITE_CACHE; 359195534Sscottl 360227309Sedstatic SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0, 361195534Sscottl "CAM Direct Access Disk driver"); 362221071SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RW, 363221071Smav &ada_legacy_aliases, 0, "Create legacy-like device aliases"); 364221071SmavTUNABLE_INT("kern.cam.ada.legacy_aliases", &ada_legacy_aliases); 365195534SscottlSYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW, 366195534Sscottl &ada_retry_count, 0, "Normal I/O retry count"); 367195534SscottlTUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count); 368195534SscottlSYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW, 369195534Sscottl &ada_default_timeout, 0, "Normal I/O timeout (in seconds)"); 370195534SscottlTUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout); 371195534SscottlSYSCTL_INT(_kern_cam_ada, OID_AUTO, ada_send_ordered, CTLFLAG_RW, 372195534Sscottl &ada_send_ordered, 0, "Send Ordered Tags"); 373195534SscottlTUNABLE_INT("kern.cam.ada.ada_send_ordered", &ada_send_ordered); 374214279SbrucecSYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RW, 375214279Sbrucec &ada_spindown_shutdown, 0, "Spin down upon shutdown"); 376214279SbrucecTUNABLE_INT("kern.cam.ada.spindown_shutdown", &ada_spindown_shutdown); 377220650SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RW, 378220650Smav &ada_spindown_suspend, 0, "Spin down upon suspend"); 379220650SmavTUNABLE_INT("kern.cam.ada.spindown_suspend", &ada_spindown_suspend); 380224497SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RW, 381224497Smav &ada_read_ahead, 0, "Enable disk read-ahead"); 382224497SmavTUNABLE_INT("kern.cam.ada.read_ahead", &ada_read_ahead); 383220412SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RW, 384220412Smav &ada_write_cache, 0, "Enable disk write cache"); 385220412SmavTUNABLE_INT("kern.cam.ada.write_cache", &ada_write_cache); 386195534Sscottl 387195534Sscottl/* 388195534Sscottl * ADA_ORDEREDTAG_INTERVAL determines how often, relative 389195534Sscottl * to the default timeout, we check to see whether an ordered 390195534Sscottl * tagged transaction is appropriate to prevent simple tag 391195534Sscottl * starvation. Since we'd like to ensure that there is at least 392195534Sscottl * 1/2 of the timeout length left for a starved transaction to 393195534Sscottl * complete after we've sent an ordered tag, we must poll at least 394195534Sscottl * four times in every timeout period. This takes care of the worst 395195534Sscottl * case where a starved transaction starts during an interval that 396195534Sscottl * meets the requirement "don't send an ordered tag" test so it takes 397195534Sscottl * us two intervals to determine that a tag must be sent. 398195534Sscottl */ 399195534Sscottl#ifndef ADA_ORDEREDTAG_INTERVAL 400195534Sscottl#define ADA_ORDEREDTAG_INTERVAL 4 401195534Sscottl#endif 402195534Sscottl 403195534Sscottlstatic struct periph_driver adadriver = 404195534Sscottl{ 405195534Sscottl adainit, "ada", 406195534Sscottl TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0 407195534Sscottl}; 408195534Sscottl 409195534SscottlPERIPHDRIVER_DECLARE(ada, adadriver); 410195534Sscottl 411227293Sedstatic MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers"); 412195534Sscottl 413195534Sscottlstatic int 414195534Sscottladaopen(struct disk *dp) 415195534Sscottl{ 416195534Sscottl struct cam_periph *periph; 417195534Sscottl struct ada_softc *softc; 418195534Sscottl int error; 419195534Sscottl 420195534Sscottl periph = (struct cam_periph *)dp->d_drv1; 421195534Sscottl if (periph == NULL) { 422195534Sscottl return (ENXIO); 423195534Sscottl } 424195534Sscottl 425195534Sscottl if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 426195534Sscottl return(ENXIO); 427195534Sscottl } 428195534Sscottl 429195534Sscottl cam_periph_lock(periph); 430195534Sscottl if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) { 431195534Sscottl cam_periph_unlock(periph); 432195534Sscottl cam_periph_release(periph); 433195534Sscottl return (error); 434195534Sscottl } 435195534Sscottl 436195534Sscottl softc = (struct ada_softc *)periph->softc; 437195534Sscottl softc->flags |= ADA_FLAG_OPEN; 438195534Sscottl 439236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 440236602Smav ("adaopen\n")); 441195534Sscottl 442195534Sscottl if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) { 443195534Sscottl /* Invalidate our pack information. */ 444195534Sscottl softc->flags &= ~ADA_FLAG_PACK_INVALID; 445195534Sscottl } 446195534Sscottl 447195534Sscottl cam_periph_unhold(periph); 448195534Sscottl cam_periph_unlock(periph); 449195534Sscottl return (0); 450195534Sscottl} 451195534Sscottl 452195534Sscottlstatic int 453195534Sscottladaclose(struct disk *dp) 454195534Sscottl{ 455195534Sscottl struct cam_periph *periph; 456195534Sscottl struct ada_softc *softc; 457195534Sscottl union ccb *ccb; 458195534Sscottl 459195534Sscottl periph = (struct cam_periph *)dp->d_drv1; 460195534Sscottl if (periph == NULL) 461195534Sscottl return (ENXIO); 462195534Sscottl 463195534Sscottl cam_periph_lock(periph); 464234414Smav if (cam_periph_hold(periph, PRIBIO) != 0) { 465195534Sscottl cam_periph_unlock(periph); 466195534Sscottl cam_periph_release(periph); 467234414Smav return (0); 468195534Sscottl } 469195534Sscottl 470195534Sscottl softc = (struct ada_softc *)periph->softc; 471236602Smav 472236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 473236602Smav ("adaclose\n")); 474236602Smav 475195534Sscottl /* We only sync the cache if the drive is capable of it. */ 476224283Smav if ((softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 && 477224283Smav (softc->flags & ADA_FLAG_PACK_INVALID) == 0) { 478195534Sscottl 479198382Smav ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 480195534Sscottl cam_fill_ataio(&ccb->ataio, 481195534Sscottl 1, 482195534Sscottl adadone, 483195534Sscottl CAM_DIR_NONE, 484195534Sscottl 0, 485195534Sscottl NULL, 486195534Sscottl 0, 487195534Sscottl ada_default_timeout*1000); 488195534Sscottl 489195534Sscottl if (softc->flags & ADA_FLAG_CAN_48BIT) 490195534Sscottl ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 491195534Sscottl else 492196659Smav ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 493236639Smav cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 494198328Smav /*sense_flags*/0, softc->disk->d_devstat); 495195534Sscottl 496195534Sscottl if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 497195534Sscottl xpt_print(periph->path, "Synchronize cache failed\n"); 498195534Sscottl xpt_release_ccb(ccb); 499195534Sscottl } 500195534Sscottl 501195534Sscottl softc->flags &= ~ADA_FLAG_OPEN; 502195534Sscottl cam_periph_unhold(periph); 503195534Sscottl cam_periph_unlock(periph); 504195534Sscottl cam_periph_release(periph); 505195534Sscottl return (0); 506195534Sscottl} 507195534Sscottl 508201139Smavstatic void 509201139Smavadaschedule(struct cam_periph *periph) 510201139Smav{ 511201139Smav struct ada_softc *softc = (struct ada_softc *)periph->softc; 512224531Smav uint32_t prio; 513201139Smav 514224531Smav /* Check if cam_periph_getccb() was called. */ 515224531Smav prio = periph->immediate_priority; 516224531Smav 517224531Smav /* Check if we have more work to do. */ 518201139Smav if (bioq_first(&softc->bio_queue) || 519201139Smav (!softc->trim_running && bioq_first(&softc->trim_queue))) { 520224531Smav prio = CAM_PRIORITY_NORMAL; 521201139Smav } 522224531Smav 523224531Smav /* Schedule CCB if any of above is true. */ 524224531Smav if (prio != CAM_PRIORITY_NONE) 525224531Smav xpt_schedule(periph, prio); 526201139Smav} 527201139Smav 528195534Sscottl/* 529195534Sscottl * Actually translate the requested transfer into one the physical driver 530195534Sscottl * can understand. The transfer is described by a buf and will include 531195534Sscottl * only one physical transfer. 532195534Sscottl */ 533195534Sscottlstatic void 534195534Sscottladastrategy(struct bio *bp) 535195534Sscottl{ 536195534Sscottl struct cam_periph *periph; 537195534Sscottl struct ada_softc *softc; 538195534Sscottl 539195534Sscottl periph = (struct cam_periph *)bp->bio_disk->d_drv1; 540195534Sscottl if (periph == NULL) { 541195534Sscottl biofinish(bp, NULL, ENXIO); 542195534Sscottl return; 543195534Sscottl } 544195534Sscottl softc = (struct ada_softc *)periph->softc; 545195534Sscottl 546195534Sscottl cam_periph_lock(periph); 547195534Sscottl 548236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp)); 549236602Smav 550195534Sscottl /* 551195534Sscottl * If the device has been made invalid, error out 552195534Sscottl */ 553195534Sscottl if ((softc->flags & ADA_FLAG_PACK_INVALID)) { 554195534Sscottl cam_periph_unlock(periph); 555195534Sscottl biofinish(bp, NULL, ENXIO); 556195534Sscottl return; 557195534Sscottl } 558195534Sscottl 559195534Sscottl /* 560195534Sscottl * Place it in the queue of disk activities for this disk 561195534Sscottl */ 562201139Smav if (bp->bio_cmd == BIO_DELETE && 563201139Smav (softc->flags & ADA_FLAG_CAN_TRIM)) 564201139Smav bioq_disksort(&softc->trim_queue, bp); 565201139Smav else 566201139Smav bioq_disksort(&softc->bio_queue, bp); 567195534Sscottl 568195534Sscottl /* 569195534Sscottl * Schedule ourselves for performing the work. 570195534Sscottl */ 571201139Smav adaschedule(periph); 572195534Sscottl cam_periph_unlock(periph); 573195534Sscottl 574195534Sscottl return; 575195534Sscottl} 576195534Sscottl 577195534Sscottlstatic int 578195534Sscottladadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) 579195534Sscottl{ 580195534Sscottl struct cam_periph *periph; 581195534Sscottl struct ada_softc *softc; 582195534Sscottl u_int secsize; 583195534Sscottl union ccb ccb; 584195534Sscottl struct disk *dp; 585195534Sscottl uint64_t lba; 586195534Sscottl uint16_t count; 587195534Sscottl 588195534Sscottl dp = arg; 589195534Sscottl periph = dp->d_drv1; 590195534Sscottl if (periph == NULL) 591195534Sscottl return (ENXIO); 592195534Sscottl softc = (struct ada_softc *)periph->softc; 593195534Sscottl cam_periph_lock(periph); 594195534Sscottl secsize = softc->params.secsize; 595195534Sscottl lba = offset / secsize; 596195534Sscottl count = length / secsize; 597195534Sscottl 598195534Sscottl if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) { 599195534Sscottl cam_periph_unlock(periph); 600195534Sscottl return (ENXIO); 601195534Sscottl } 602195534Sscottl 603195534Sscottl if (length > 0) { 604198382Smav xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 605195534Sscottl ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 606195534Sscottl cam_fill_ataio(&ccb.ataio, 607195534Sscottl 0, 608195534Sscottl adadone, 609195534Sscottl CAM_DIR_OUT, 610195534Sscottl 0, 611195534Sscottl (u_int8_t *) virtual, 612195534Sscottl length, 613195534Sscottl ada_default_timeout*1000); 614195534Sscottl if ((softc->flags & ADA_FLAG_CAN_48BIT) && 615195534Sscottl (lba + count >= ATA_MAX_28BIT_LBA || 616195534Sscottl count >= 256)) { 617195534Sscottl ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48, 618195534Sscottl 0, lba, count); 619195534Sscottl } else { 620196659Smav ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA, 621195534Sscottl 0, lba, count); 622195534Sscottl } 623195534Sscottl xpt_polled_action(&ccb); 624195534Sscottl 625195534Sscottl if ((ccb.ataio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 626195534Sscottl printf("Aborting dump due to I/O error.\n"); 627195534Sscottl cam_periph_unlock(periph); 628195534Sscottl return(EIO); 629195534Sscottl } 630195534Sscottl cam_periph_unlock(periph); 631195534Sscottl return(0); 632195534Sscottl } 633195534Sscottl 634195534Sscottl if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) { 635198382Smav xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 636195534Sscottl 637195534Sscottl ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 638195534Sscottl cam_fill_ataio(&ccb.ataio, 639195534Sscottl 1, 640195534Sscottl adadone, 641195534Sscottl CAM_DIR_NONE, 642195534Sscottl 0, 643195534Sscottl NULL, 644195534Sscottl 0, 645195534Sscottl ada_default_timeout*1000); 646195534Sscottl 647195534Sscottl if (softc->flags & ADA_FLAG_CAN_48BIT) 648195534Sscottl ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0); 649195534Sscottl else 650196659Smav ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0); 651195534Sscottl xpt_polled_action(&ccb); 652195534Sscottl 653195534Sscottl if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 654195534Sscottl xpt_print(periph->path, "Synchronize cache failed\n"); 655195534Sscottl 656195534Sscottl if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 657195534Sscottl cam_release_devq(ccb.ccb_h.path, 658195534Sscottl /*relsim_flags*/0, 659195534Sscottl /*reduction*/0, 660195534Sscottl /*timeout*/0, 661195534Sscottl /*getcount_only*/0); 662195534Sscottl } 663195534Sscottl cam_periph_unlock(periph); 664195534Sscottl return (0); 665195534Sscottl} 666195534Sscottl 667195534Sscottlstatic void 668195534Sscottladainit(void) 669195534Sscottl{ 670195534Sscottl cam_status status; 671195534Sscottl 672195534Sscottl /* 673195534Sscottl * Install a global async callback. This callback will 674195534Sscottl * receive async callbacks like "new device found". 675195534Sscottl */ 676195534Sscottl status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL); 677195534Sscottl 678195534Sscottl if (status != CAM_REQ_CMP) { 679195534Sscottl printf("ada: Failed to attach master async callback " 680195534Sscottl "due to status 0x%x!\n", status); 681195534Sscottl } else if (ada_send_ordered) { 682195534Sscottl 683220650Smav /* Register our event handlers */ 684220650Smav if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend, 685220650Smav NULL, EVENTHANDLER_PRI_LAST)) == NULL) 686220650Smav printf("adainit: power event registration failed!\n"); 687220650Smav if ((EVENTHANDLER_REGISTER(power_resume, adaresume, 688220650Smav NULL, EVENTHANDLER_PRI_LAST)) == NULL) 689220650Smav printf("adainit: power event registration failed!\n"); 690220650Smav if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown, 691195534Sscottl NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 692195534Sscottl printf("adainit: shutdown event registration failed!\n"); 693195534Sscottl } 694195534Sscottl} 695195534Sscottl 696195534Sscottlstatic void 697195534Sscottladaoninvalidate(struct cam_periph *periph) 698195534Sscottl{ 699195534Sscottl struct ada_softc *softc; 700195534Sscottl 701195534Sscottl softc = (struct ada_softc *)periph->softc; 702195534Sscottl 703195534Sscottl /* 704195534Sscottl * De-register any async callbacks. 705195534Sscottl */ 706195534Sscottl xpt_register_async(0, adaasync, periph, periph->path); 707195534Sscottl 708195534Sscottl softc->flags |= ADA_FLAG_PACK_INVALID; 709195534Sscottl 710195534Sscottl /* 711195534Sscottl * Return all queued I/O with ENXIO. 712195534Sscottl * XXX Handle any transactions queued to the card 713195534Sscottl * with XPT_ABORT_CCB. 714195534Sscottl */ 715195534Sscottl bioq_flush(&softc->bio_queue, NULL, ENXIO); 716201139Smav bioq_flush(&softc->trim_queue, NULL, ENXIO); 717195534Sscottl 718195534Sscottl disk_gone(softc->disk); 719195534Sscottl xpt_print(periph->path, "lost device\n"); 720195534Sscottl} 721195534Sscottl 722195534Sscottlstatic void 723195534Sscottladacleanup(struct cam_periph *periph) 724195534Sscottl{ 725195534Sscottl struct ada_softc *softc; 726195534Sscottl 727195534Sscottl softc = (struct ada_softc *)periph->softc; 728195534Sscottl 729195534Sscottl xpt_print(periph->path, "removing device entry\n"); 730195534Sscottl cam_periph_unlock(periph); 731195534Sscottl 732195534Sscottl /* 733195534Sscottl * If we can't free the sysctl tree, oh well... 734195534Sscottl */ 735195534Sscottl if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0 736195534Sscottl && sysctl_ctx_free(&softc->sysctl_ctx) != 0) { 737195534Sscottl xpt_print(periph->path, "can't remove sysctl context\n"); 738195534Sscottl } 739195534Sscottl 740195534Sscottl disk_destroy(softc->disk); 741195534Sscottl callout_drain(&softc->sendordered_c); 742195534Sscottl free(softc, M_DEVBUF); 743195534Sscottl cam_periph_lock(periph); 744195534Sscottl} 745195534Sscottl 746195534Sscottlstatic void 747195534Sscottladaasync(void *callback_arg, u_int32_t code, 748195534Sscottl struct cam_path *path, void *arg) 749195534Sscottl{ 750236393Smav struct ccb_getdev cgd; 751195534Sscottl struct cam_periph *periph; 752220412Smav struct ada_softc *softc; 753195534Sscottl 754195534Sscottl periph = (struct cam_periph *)callback_arg; 755195534Sscottl switch (code) { 756195534Sscottl case AC_FOUND_DEVICE: 757195534Sscottl { 758195534Sscottl struct ccb_getdev *cgd; 759195534Sscottl cam_status status; 760195534Sscottl 761195534Sscottl cgd = (struct ccb_getdev *)arg; 762195534Sscottl if (cgd == NULL) 763195534Sscottl break; 764195534Sscottl 765195534Sscottl if (cgd->protocol != PROTO_ATA) 766195534Sscottl break; 767195534Sscottl 768195534Sscottl /* 769195534Sscottl * Allocate a peripheral instance for 770195534Sscottl * this device and start the probe 771195534Sscottl * process. 772195534Sscottl */ 773195534Sscottl status = cam_periph_alloc(adaregister, adaoninvalidate, 774195534Sscottl adacleanup, adastart, 775195534Sscottl "ada", CAM_PERIPH_BIO, 776195534Sscottl cgd->ccb_h.path, adaasync, 777195534Sscottl AC_FOUND_DEVICE, cgd); 778195534Sscottl 779195534Sscottl if (status != CAM_REQ_CMP 780195534Sscottl && status != CAM_REQ_INPROG) 781195534Sscottl printf("adaasync: Unable to attach to new device " 782195534Sscottl "due to status 0x%x\n", status); 783195534Sscottl break; 784195534Sscottl } 785236393Smav case AC_GETDEV_CHANGED: 786236393Smav { 787236393Smav softc = (struct ada_softc *)periph->softc; 788236393Smav xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 789236393Smav cgd.ccb_h.func_code = XPT_GDEV_TYPE; 790236393Smav xpt_action((union ccb *)&cgd); 791236393Smav 792236393Smav if ((cgd.ident_data.capabilities1 & ATA_SUPPORT_DMA) && 793236393Smav (cgd.inq_flags & SID_DMA)) 794236393Smav softc->flags |= ADA_FLAG_CAN_DMA; 795236393Smav else 796236393Smav softc->flags &= ~ADA_FLAG_CAN_DMA; 797236393Smav if ((cgd.ident_data.satacapabilities & ATA_SUPPORT_NCQ) && 798236393Smav (cgd.inq_flags & SID_DMA) && (cgd.inq_flags & SID_CmdQue)) 799236393Smav softc->flags |= ADA_FLAG_CAN_NCQ; 800236393Smav else 801236393Smav softc->flags &= ~ADA_FLAG_CAN_NCQ; 802236393Smav if ((cgd.ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) && 803236393Smav (cgd.inq_flags & SID_DMA)) 804236393Smav softc->flags |= ADA_FLAG_CAN_TRIM; 805236393Smav else 806236393Smav softc->flags &= ~ADA_FLAG_CAN_TRIM; 807236393Smav 808236393Smav cam_periph_async(periph, code, path, arg); 809236393Smav break; 810236393Smav } 811235897Smav case AC_ADVINFO_CHANGED: 812235897Smav { 813235897Smav uintptr_t buftype; 814235897Smav 815235897Smav buftype = (uintptr_t)arg; 816235897Smav if (buftype == CDAI_TYPE_PHYS_PATH) { 817235897Smav struct ada_softc *softc; 818235897Smav 819235897Smav softc = periph->softc; 820235897Smav disk_attr_changed(softc->disk, "GEOM::physpath", 821235897Smav M_NOWAIT); 822235897Smav } 823235897Smav break; 824235897Smav } 825220412Smav case AC_SENT_BDR: 826220412Smav case AC_BUS_RESET: 827220412Smav { 828220412Smav softc = (struct ada_softc *)periph->softc; 829220412Smav cam_periph_async(periph, code, path, arg); 830220412Smav if (softc->state != ADA_STATE_NORMAL) 831220412Smav break; 832220454Smav xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 833220412Smav cgd.ccb_h.func_code = XPT_GDEV_TYPE; 834220412Smav xpt_action((union ccb *)&cgd); 835224497Smav if (ADA_RA >= 0 && 836224497Smav cgd.ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) 837224497Smav softc->state = ADA_STATE_RAHEAD; 838224497Smav else if (ADA_WC >= 0 && 839224497Smav cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) 840224497Smav softc->state = ADA_STATE_WCACHE; 841224497Smav else 842224497Smav break; 843220412Smav cam_periph_acquire(periph); 844220412Smav cam_freeze_devq_arg(periph->path, 845220412Smav RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1); 846220412Smav xpt_schedule(periph, CAM_PRIORITY_DEV); 847220412Smav } 848195534Sscottl default: 849195534Sscottl cam_periph_async(periph, code, path, arg); 850195534Sscottl break; 851195534Sscottl } 852195534Sscottl} 853195534Sscottl 854195534Sscottlstatic void 855195534Sscottladasysctlinit(void *context, int pending) 856195534Sscottl{ 857195534Sscottl struct cam_periph *periph; 858195534Sscottl struct ada_softc *softc; 859195534Sscottl char tmpstr[80], tmpstr2[80]; 860195534Sscottl 861195534Sscottl periph = (struct cam_periph *)context; 862220454Smav 863220454Smav /* periph was held for us when this task was enqueued */ 864220454Smav if (periph->flags & CAM_PERIPH_INVALID) { 865220454Smav cam_periph_release(periph); 866195534Sscottl return; 867220454Smav } 868195534Sscottl 869195534Sscottl softc = (struct ada_softc *)periph->softc; 870195534Sscottl snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number); 871195534Sscottl snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 872195534Sscottl 873195534Sscottl sysctl_ctx_init(&softc->sysctl_ctx); 874195534Sscottl softc->flags |= ADA_FLAG_SCTX_INIT; 875195534Sscottl softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 876195534Sscottl SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2, 877195534Sscottl CTLFLAG_RD, 0, tmpstr); 878195534Sscottl if (softc->sysctl_tree == NULL) { 879195534Sscottl printf("adasysctlinit: unable to allocate sysctl tree\n"); 880195534Sscottl cam_periph_release(periph); 881195534Sscottl return; 882195534Sscottl } 883195534Sscottl 884220454Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 885224497Smav OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE, 886224497Smav &softc->read_ahead, 0, "Enable disk read ahead."); 887224497Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 888220454Smav OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE, 889220454Smav &softc->write_cache, 0, "Enable disk write cache."); 890220454Smav#ifdef ADA_TEST_FAILURE 891220454Smav /* 892220454Smav * Add a 'door bell' sysctl which allows one to set it from userland 893220454Smav * and cause something bad to happen. For the moment, we only allow 894220454Smav * whacking the next read or write. 895220454Smav */ 896220454Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 897220454Smav OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 898220454Smav &softc->force_read_error, 0, 899220454Smav "Force a read error for the next N reads."); 900220454Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 901220454Smav OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 902220454Smav &softc->force_write_error, 0, 903220454Smav "Force a write error for the next N writes."); 904220454Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 905220454Smav OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 906220454Smav &softc->periodic_read_error, 0, 907220454Smav "Force a read error every N reads (don't set too low)."); 908220454Smav#endif 909195534Sscottl cam_periph_release(periph); 910195534Sscottl} 911195534Sscottl 912223089Sgibbsstatic int 913223089Sgibbsadagetattr(struct bio *bp) 914223089Sgibbs{ 915223089Sgibbs int ret = -1; 916223089Sgibbs struct cam_periph *periph; 917223089Sgibbs 918223277Skib if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL) 919223089Sgibbs return ENXIO; 920223089Sgibbs periph = (struct cam_periph *)bp->bio_disk->d_drv1; 921223089Sgibbs if (periph->path == NULL) 922223089Sgibbs return ENXIO; 923223089Sgibbs 924223089Sgibbs ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute, 925223089Sgibbs periph->path); 926223089Sgibbs if (ret == 0) 927223089Sgibbs bp->bio_completed = bp->bio_length; 928223089Sgibbs return ret; 929223089Sgibbs} 930223089Sgibbs 931195534Sscottlstatic cam_status 932195534Sscottladaregister(struct cam_periph *periph, void *arg) 933195534Sscottl{ 934195534Sscottl struct ada_softc *softc; 935195534Sscottl struct ccb_pathinq cpi; 936195534Sscottl struct ccb_getdev *cgd; 937221071Smav char announce_buf[80], buf1[32]; 938195534Sscottl struct disk_params *dp; 939195534Sscottl caddr_t match; 940195534Sscottl u_int maxio; 941222520Smav int legacy_id, quirks; 942195534Sscottl 943195534Sscottl cgd = (struct ccb_getdev *)arg; 944195534Sscottl if (periph == NULL) { 945195534Sscottl printf("adaregister: periph was NULL!!\n"); 946195534Sscottl return(CAM_REQ_CMP_ERR); 947195534Sscottl } 948195534Sscottl 949195534Sscottl if (cgd == NULL) { 950195534Sscottl printf("adaregister: no getdev CCB, can't register device\n"); 951195534Sscottl return(CAM_REQ_CMP_ERR); 952195534Sscottl } 953195534Sscottl 954195534Sscottl softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF, 955195534Sscottl M_NOWAIT|M_ZERO); 956195534Sscottl 957195534Sscottl if (softc == NULL) { 958195534Sscottl printf("adaregister: Unable to probe new device. " 959198328Smav "Unable to allocate softc\n"); 960195534Sscottl return(CAM_REQ_CMP_ERR); 961195534Sscottl } 962195534Sscottl 963195534Sscottl bioq_init(&softc->bio_queue); 964201139Smav bioq_init(&softc->trim_queue); 965195534Sscottl 966236393Smav if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) && 967220886Smav (cgd->inq_flags & SID_DMA)) 968198328Smav softc->flags |= ADA_FLAG_CAN_DMA; 969195534Sscottl if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) 970195534Sscottl softc->flags |= ADA_FLAG_CAN_48BIT; 971195534Sscottl if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE) 972195534Sscottl softc->flags |= ADA_FLAG_CAN_FLUSHCACHE; 973214279Sbrucec if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT) 974214279Sbrucec softc->flags |= ADA_FLAG_CAN_POWERMGT; 975236393Smav if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) && 976220886Smav (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue)) 977195534Sscottl softc->flags |= ADA_FLAG_CAN_NCQ; 978236393Smav if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) && 979236393Smav (cgd->inq_flags & SID_DMA)) { 980201139Smav softc->flags |= ADA_FLAG_CAN_TRIM; 981201139Smav softc->trim_max_ranges = TRIM_MAX_RANGES; 982201139Smav if (cgd->ident_data.max_dsm_blocks != 0) { 983201139Smav softc->trim_max_ranges = 984201139Smav min(cgd->ident_data.max_dsm_blocks * 64, 985201139Smav softc->trim_max_ranges); 986201139Smav } 987201139Smav } 988201139Smav if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA) 989201139Smav softc->flags |= ADA_FLAG_CAN_CFA; 990195534Sscottl 991195534Sscottl periph->softc = softc; 992195534Sscottl 993195534Sscottl /* 994195534Sscottl * See if this device has any quirks. 995195534Sscottl */ 996199178Smav match = cam_quirkmatch((caddr_t)&cgd->ident_data, 997199178Smav (caddr_t)ada_quirk_table, 998199178Smav sizeof(ada_quirk_table)/sizeof(*ada_quirk_table), 999199178Smav sizeof(*ada_quirk_table), ata_identify_match); 1000195534Sscottl if (match != NULL) 1001195534Sscottl softc->quirks = ((struct ada_quirk_entry *)match)->quirks; 1002195534Sscottl else 1003195534Sscottl softc->quirks = ADA_Q_NONE; 1004195534Sscottl 1005195534Sscottl bzero(&cpi, sizeof(cpi)); 1006203108Smav xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE); 1007195534Sscottl cpi.ccb_h.func_code = XPT_PATH_INQ; 1008195534Sscottl xpt_action((union ccb *)&cpi); 1009195534Sscottl 1010195534Sscottl TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph); 1011195534Sscottl 1012195534Sscottl /* 1013195534Sscottl * Register this media as a disk 1014195534Sscottl */ 1015220618Smav (void)cam_periph_hold(periph, PRIBIO); 1016195534Sscottl mtx_unlock(periph->sim->mtx); 1017222520Smav snprintf(announce_buf, sizeof(announce_buf), 1018222520Smav "kern.cam.ada.%d.quirks", periph->unit_number); 1019222520Smav quirks = softc->quirks; 1020222520Smav TUNABLE_INT_FETCH(announce_buf, &quirks); 1021222520Smav softc->quirks = quirks; 1022224497Smav softc->read_ahead = -1; 1023224497Smav snprintf(announce_buf, sizeof(announce_buf), 1024224497Smav "kern.cam.ada.%d.read_ahead", periph->unit_number); 1025224497Smav TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead); 1026220618Smav softc->write_cache = -1; 1027220618Smav snprintf(announce_buf, sizeof(announce_buf), 1028220618Smav "kern.cam.ada.%d.write_cache", periph->unit_number); 1029220618Smav TUNABLE_INT_FETCH(announce_buf, &softc->write_cache); 1030198897Smav adagetparams(periph, cgd); 1031195534Sscottl softc->disk = disk_alloc(); 1032220644Smav softc->disk->d_devstat = devstat_new_entry(periph->periph_name, 1033220644Smav periph->unit_number, softc->params.secsize, 1034220644Smav DEVSTAT_ALL_SUPPORTED, 1035220644Smav DEVSTAT_TYPE_DIRECT | 1036220644Smav XPORT_DEVSTAT_TYPE(cpi.transport), 1037220644Smav DEVSTAT_PRIORITY_DISK); 1038195534Sscottl softc->disk->d_open = adaopen; 1039195534Sscottl softc->disk->d_close = adaclose; 1040195534Sscottl softc->disk->d_strategy = adastrategy; 1041223089Sgibbs softc->disk->d_getattr = adagetattr; 1042195534Sscottl softc->disk->d_dump = adadump; 1043195534Sscottl softc->disk->d_name = "ada"; 1044195534Sscottl softc->disk->d_drv1 = periph; 1045195534Sscottl maxio = cpi.maxio; /* Honor max I/O size of SIM */ 1046195534Sscottl if (maxio == 0) 1047195534Sscottl maxio = DFLTPHYS; /* traditional default */ 1048195534Sscottl else if (maxio > MAXPHYS) 1049195534Sscottl maxio = MAXPHYS; /* for safety */ 1050201139Smav if (softc->flags & ADA_FLAG_CAN_48BIT) 1051198897Smav maxio = min(maxio, 65536 * softc->params.secsize); 1052195534Sscottl else /* 28bit ATA command limit */ 1053198897Smav maxio = min(maxio, 256 * softc->params.secsize); 1054195534Sscottl softc->disk->d_maxsize = maxio; 1055195534Sscottl softc->disk->d_unit = periph->unit_number; 1056195534Sscottl softc->disk->d_flags = 0; 1057195534Sscottl if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) 1058195534Sscottl softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE; 1059201139Smav if ((softc->flags & ADA_FLAG_CAN_TRIM) || 1060201139Smav ((softc->flags & ADA_FLAG_CAN_CFA) && 1061201139Smav !(softc->flags & ADA_FLAG_CAN_48BIT))) 1062201139Smav softc->disk->d_flags |= DISKFLAG_CANDELETE; 1063219056Snwhitehorn strlcpy(softc->disk->d_descr, cgd->ident_data.model, 1064219056Snwhitehorn MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model))); 1065210471Smav softc->disk->d_hba_vendor = cpi.hba_vendor; 1066210471Smav softc->disk->d_hba_device = cpi.hba_device; 1067210471Smav softc->disk->d_hba_subvendor = cpi.hba_subvendor; 1068210471Smav softc->disk->d_hba_subdevice = cpi.hba_subdevice; 1069195534Sscottl 1070195534Sscottl softc->disk->d_sectorsize = softc->params.secsize; 1071198897Smav softc->disk->d_mediasize = (off_t)softc->params.sectors * 1072198897Smav softc->params.secsize; 1073200969Smav if (ata_physical_sector_size(&cgd->ident_data) != 1074200969Smav softc->params.secsize) { 1075200969Smav softc->disk->d_stripesize = 1076200969Smav ata_physical_sector_size(&cgd->ident_data); 1077200969Smav softc->disk->d_stripeoffset = (softc->disk->d_stripesize - 1078200969Smav ata_logical_sector_offset(&cgd->ident_data)) % 1079200969Smav softc->disk->d_stripesize; 1080222520Smav } else if (softc->quirks & ADA_Q_4K) { 1081222520Smav softc->disk->d_stripesize = 4096; 1082222520Smav softc->disk->d_stripeoffset = 0; 1083200969Smav } 1084195534Sscottl softc->disk->d_fwsectors = softc->params.secs_per_track; 1085195534Sscottl softc->disk->d_fwheads = softc->params.heads; 1086208349Smarius ata_disk_firmware_geom_adjust(softc->disk); 1087195534Sscottl 1088221071Smav if (ada_legacy_aliases) { 1089221071Smav#ifdef ATA_STATIC_ID 1090221071Smav legacy_id = xpt_path_legacy_ata_id(periph->path); 1091221071Smav#else 1092221071Smav legacy_id = softc->disk->d_unit; 1093221071Smav#endif 1094221071Smav if (legacy_id >= 0) { 1095221071Smav snprintf(announce_buf, sizeof(announce_buf), 1096221071Smav "kern.devalias.%s%d", 1097221071Smav softc->disk->d_name, softc->disk->d_unit); 1098221071Smav snprintf(buf1, sizeof(buf1), 1099221071Smav "ad%d", legacy_id); 1100221071Smav setenv(announce_buf, buf1); 1101221071Smav } 1102221071Smav } else 1103221071Smav legacy_id = -1; 1104195534Sscottl disk_create(softc->disk, DISK_VERSION); 1105195534Sscottl mtx_lock(periph->sim->mtx); 1106220618Smav cam_periph_unhold(periph); 1107195534Sscottl 1108195534Sscottl dp = &softc->params; 1109195534Sscottl snprintf(announce_buf, sizeof(announce_buf), 1110195534Sscottl "%juMB (%ju %u byte sectors: %dH %dS/T %dC)", 1111195534Sscottl (uintmax_t)(((uintmax_t)dp->secsize * 1112195534Sscottl dp->sectors) / (1024*1024)), 1113195534Sscottl (uintmax_t)dp->sectors, 1114195534Sscottl dp->secsize, dp->heads, 1115195534Sscottl dp->secs_per_track, dp->cylinders); 1116195534Sscottl xpt_announce_periph(periph, announce_buf); 1117221071Smav if (legacy_id >= 0) 1118221071Smav printf("%s%d: Previously was known as ad%d\n", 1119221071Smav periph->periph_name, periph->unit_number, legacy_id); 1120220454Smav 1121195534Sscottl /* 1122220454Smav * Create our sysctl variables, now that we know 1123220454Smav * we have successfully attached. 1124220454Smav */ 1125220454Smav cam_periph_acquire(periph); 1126220454Smav taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task); 1127220454Smav 1128220454Smav /* 1129195534Sscottl * Add async callbacks for bus reset and 1130195534Sscottl * bus device reset calls. I don't bother 1131195534Sscottl * checking if this fails as, in most cases, 1132195534Sscottl * the system will function just fine without 1133195534Sscottl * them and the only alternative would be to 1134195534Sscottl * not attach the device on failure. 1135195534Sscottl */ 1136235897Smav xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE | 1137236393Smav AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED, 1138236393Smav adaasync, periph, periph->path); 1139195534Sscottl 1140195534Sscottl /* 1141195534Sscottl * Schedule a periodic event to occasionally send an 1142195534Sscottl * ordered tag to a device. 1143195534Sscottl */ 1144195534Sscottl callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0); 1145195534Sscottl callout_reset(&softc->sendordered_c, 1146230921Smav (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 1147195534Sscottl adasendorderedtag, softc); 1148195534Sscottl 1149224497Smav if (ADA_RA >= 0 && 1150224497Smav cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) { 1151224497Smav softc->state = ADA_STATE_RAHEAD; 1152224497Smav cam_periph_acquire(periph); 1153224497Smav cam_freeze_devq_arg(periph->path, 1154224497Smav RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1); 1155224497Smav xpt_schedule(periph, CAM_PRIORITY_DEV); 1156224497Smav } else if (ADA_WC >= 0 && 1157220412Smav cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) { 1158220412Smav softc->state = ADA_STATE_WCACHE; 1159220412Smav cam_periph_acquire(periph); 1160220412Smav cam_freeze_devq_arg(periph->path, 1161220412Smav RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1); 1162220412Smav xpt_schedule(periph, CAM_PRIORITY_DEV); 1163220412Smav } else 1164220412Smav softc->state = ADA_STATE_NORMAL; 1165220412Smav 1166195534Sscottl return(CAM_REQ_CMP); 1167195534Sscottl} 1168195534Sscottl 1169195534Sscottlstatic void 1170195534Sscottladastart(struct cam_periph *periph, union ccb *start_ccb) 1171195534Sscottl{ 1172198328Smav struct ada_softc *softc = (struct ada_softc *)periph->softc; 1173198328Smav struct ccb_ataio *ataio = &start_ccb->ataio; 1174195534Sscottl 1175236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n")); 1176236602Smav 1177195534Sscottl switch (softc->state) { 1178195534Sscottl case ADA_STATE_NORMAL: 1179195534Sscottl { 1180195534Sscottl struct bio *bp; 1181201139Smav u_int8_t tag_code; 1182195534Sscottl 1183201139Smav /* Execute immediate CCB if waiting. */ 1184195534Sscottl if (periph->immediate_priority <= periph->pinfo.priority) { 1185236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1186195534Sscottl ("queuing for immediate ccb\n")); 1187195534Sscottl start_ccb->ccb_h.ccb_state = ADA_CCB_WAITING; 1188195534Sscottl SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 1189195534Sscottl periph_links.sle); 1190195534Sscottl periph->immediate_priority = CAM_PRIORITY_NONE; 1191195534Sscottl wakeup(&periph->ccb_list); 1192201139Smav /* Have more work to do, so ensure we stay scheduled */ 1193201139Smav adaschedule(periph); 1194201139Smav break; 1195201139Smav } 1196201139Smav /* Run TRIM if not running yet. */ 1197201139Smav if (!softc->trim_running && 1198201139Smav (bp = bioq_first(&softc->trim_queue)) != 0) { 1199201139Smav struct trim_request *req = &softc->trim_req; 1200201139Smav struct bio *bp1; 1201222628Smav uint64_t lastlba = (uint64_t)-1; 1202222628Smav int bps = 0, c, lastcount = 0, off, ranges = 0; 1203201139Smav 1204201139Smav softc->trim_running = 1; 1205201139Smav bzero(req, sizeof(*req)); 1206201139Smav bp1 = bp; 1207201139Smav do { 1208201139Smav uint64_t lba = bp1->bio_pblkno; 1209201139Smav int count = bp1->bio_bcount / 1210201139Smav softc->params.secsize; 1211201139Smav 1212201139Smav bioq_remove(&softc->trim_queue, bp1); 1213222628Smav 1214222628Smav /* Try to extend the previous range. */ 1215222628Smav if (lba == lastlba) { 1216222628Smav c = min(count, 0xffff - lastcount); 1217222628Smav lastcount += c; 1218222628Smav off = (ranges - 1) * 8; 1219222628Smav req->data[off + 6] = lastcount & 0xff; 1220222628Smav req->data[off + 7] = 1221222628Smav (lastcount >> 8) & 0xff; 1222222628Smav count -= c; 1223222628Smav lba += c; 1224222628Smav } 1225222628Smav 1226201139Smav while (count > 0) { 1227222628Smav c = min(count, 0xffff); 1228222628Smav off = ranges * 8; 1229201139Smav req->data[off + 0] = lba & 0xff; 1230201139Smav req->data[off + 1] = (lba >> 8) & 0xff; 1231201139Smav req->data[off + 2] = (lba >> 16) & 0xff; 1232201139Smav req->data[off + 3] = (lba >> 24) & 0xff; 1233201139Smav req->data[off + 4] = (lba >> 32) & 0xff; 1234201139Smav req->data[off + 5] = (lba >> 40) & 0xff; 1235201139Smav req->data[off + 6] = c & 0xff; 1236201139Smav req->data[off + 7] = (c >> 8) & 0xff; 1237201139Smav lba += c; 1238201139Smav count -= c; 1239222628Smav lastcount = c; 1240201139Smav ranges++; 1241201139Smav } 1242222628Smav lastlba = lba; 1243201139Smav req->bps[bps++] = bp1; 1244201139Smav bp1 = bioq_first(&softc->trim_queue); 1245222628Smav if (bps >= TRIM_MAX_BIOS || 1246222628Smav bp1 == NULL || 1247201139Smav bp1->bio_bcount / softc->params.secsize > 1248201139Smav (softc->trim_max_ranges - ranges) * 0xffff) 1249201139Smav break; 1250201139Smav } while (1); 1251201139Smav cam_fill_ataio(ataio, 1252201139Smav ada_retry_count, 1253201139Smav adadone, 1254201139Smav CAM_DIR_OUT, 1255201139Smav 0, 1256201139Smav req->data, 1257201139Smav ((ranges + 63) / 64) * 512, 1258201139Smav ada_default_timeout * 1000); 1259201139Smav ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT, 1260201139Smav ATA_DSM_TRIM, 0, (ranges + 63) / 64); 1261201139Smav start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM; 1262201139Smav goto out; 1263201139Smav } 1264201139Smav /* Run regular command. */ 1265201139Smav bp = bioq_first(&softc->bio_queue); 1266201139Smav if (bp == NULL) { 1267195534Sscottl xpt_release_ccb(start_ccb); 1268201139Smav break; 1269201139Smav } 1270201139Smav bioq_remove(&softc->bio_queue, bp); 1271201139Smav 1272212160Sgibbs if ((bp->bio_flags & BIO_ORDERED) != 0 1273212160Sgibbs || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) { 1274201139Smav softc->flags &= ~ADA_FLAG_NEED_OTAG; 1275201139Smav softc->ordered_tag_count++; 1276201139Smav tag_code = 0; 1277195534Sscottl } else { 1278201139Smav tag_code = 1; 1279201139Smav } 1280201139Smav switch (bp->bio_cmd) { 1281201139Smav case BIO_READ: 1282201139Smav case BIO_WRITE: 1283201139Smav { 1284201139Smav uint64_t lba = bp->bio_pblkno; 1285201139Smav uint16_t count = bp->bio_bcount / softc->params.secsize; 1286220454Smav#ifdef ADA_TEST_FAILURE 1287220454Smav int fail = 0; 1288195534Sscottl 1289220454Smav /* 1290220454Smav * Support the failure ioctls. If the command is a 1291220454Smav * read, and there are pending forced read errors, or 1292220454Smav * if a write and pending write errors, then fail this 1293220454Smav * operation with EIO. This is useful for testing 1294220454Smav * purposes. Also, support having every Nth read fail. 1295220454Smav * 1296220454Smav * This is a rather blunt tool. 1297220454Smav */ 1298220454Smav if (bp->bio_cmd == BIO_READ) { 1299220454Smav if (softc->force_read_error) { 1300220454Smav softc->force_read_error--; 1301220454Smav fail = 1; 1302220454Smav } 1303220454Smav if (softc->periodic_read_error > 0) { 1304220454Smav if (++softc->periodic_read_count >= 1305220454Smav softc->periodic_read_error) { 1306220454Smav softc->periodic_read_count = 0; 1307220454Smav fail = 1; 1308220454Smav } 1309220454Smav } 1310220454Smav } else { 1311220454Smav if (softc->force_write_error) { 1312220454Smav softc->force_write_error--; 1313220454Smav fail = 1; 1314220454Smav } 1315220454Smav } 1316220454Smav if (fail) { 1317220454Smav bp->bio_error = EIO; 1318220454Smav bp->bio_flags |= BIO_ERROR; 1319220454Smav biodone(bp); 1320220454Smav xpt_release_ccb(start_ccb); 1321220454Smav adaschedule(periph); 1322220454Smav return; 1323220454Smav } 1324220454Smav#endif 1325201139Smav cam_fill_ataio(ataio, 1326201139Smav ada_retry_count, 1327201139Smav adadone, 1328201139Smav bp->bio_cmd == BIO_READ ? 1329201139Smav CAM_DIR_IN : CAM_DIR_OUT, 1330201139Smav tag_code, 1331201139Smav bp->bio_data, 1332201139Smav bp->bio_bcount, 1333201139Smav ada_default_timeout*1000); 1334195534Sscottl 1335201139Smav if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) { 1336201139Smav if (bp->bio_cmd == BIO_READ) { 1337201139Smav ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED, 1338201139Smav lba, count); 1339201139Smav } else { 1340201139Smav ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED, 1341201139Smav lba, count); 1342201139Smav } 1343201139Smav } else if ((softc->flags & ADA_FLAG_CAN_48BIT) && 1344201139Smav (lba + count >= ATA_MAX_28BIT_LBA || 1345201139Smav count > 256)) { 1346201139Smav if (softc->flags & ADA_FLAG_CAN_DMA) { 1347195534Sscottl if (bp->bio_cmd == BIO_READ) { 1348201139Smav ata_48bit_cmd(ataio, ATA_READ_DMA48, 1349201139Smav 0, lba, count); 1350195534Sscottl } else { 1351201139Smav ata_48bit_cmd(ataio, ATA_WRITE_DMA48, 1352201139Smav 0, lba, count); 1353195534Sscottl } 1354201139Smav } else { 1355201139Smav if (bp->bio_cmd == BIO_READ) { 1356201139Smav ata_48bit_cmd(ataio, ATA_READ_MUL48, 1357201139Smav 0, lba, count); 1358195534Sscottl } else { 1359201139Smav ata_48bit_cmd(ataio, ATA_WRITE_MUL48, 1360201139Smav 0, lba, count); 1361195534Sscottl } 1362201139Smav } 1363201139Smav } else { 1364201139Smav if (count == 256) 1365201139Smav count = 0; 1366201139Smav if (softc->flags & ADA_FLAG_CAN_DMA) { 1367201139Smav if (bp->bio_cmd == BIO_READ) { 1368201139Smav ata_28bit_cmd(ataio, ATA_READ_DMA, 1369201139Smav 0, lba, count); 1370201139Smav } else { 1371201139Smav ata_28bit_cmd(ataio, ATA_WRITE_DMA, 1372201139Smav 0, lba, count); 1373201139Smav } 1374195534Sscottl } else { 1375201139Smav if (bp->bio_cmd == BIO_READ) { 1376201139Smav ata_28bit_cmd(ataio, ATA_READ_MUL, 1377201139Smav 0, lba, count); 1378195534Sscottl } else { 1379201139Smav ata_28bit_cmd(ataio, ATA_WRITE_MUL, 1380201139Smav 0, lba, count); 1381195534Sscottl } 1382195534Sscottl } 1383195534Sscottl } 1384201139Smav break; 1385201139Smav } 1386201139Smav case BIO_DELETE: 1387201139Smav { 1388201139Smav uint64_t lba = bp->bio_pblkno; 1389201139Smav uint16_t count = bp->bio_bcount / softc->params.secsize; 1390195534Sscottl 1391201139Smav cam_fill_ataio(ataio, 1392201139Smav ada_retry_count, 1393201139Smav adadone, 1394201139Smav CAM_DIR_NONE, 1395201139Smav 0, 1396201139Smav NULL, 1397201139Smav 0, 1398201139Smav ada_default_timeout*1000); 1399201139Smav 1400201139Smav if (count >= 256) 1401201139Smav count = 0; 1402201139Smav ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count); 1403201139Smav break; 1404195534Sscottl } 1405201139Smav case BIO_FLUSH: 1406201139Smav cam_fill_ataio(ataio, 1407201139Smav 1, 1408201139Smav adadone, 1409201139Smav CAM_DIR_NONE, 1410201139Smav 0, 1411201139Smav NULL, 1412201139Smav 0, 1413201139Smav ada_default_timeout*1000); 1414201139Smav 1415201139Smav if (softc->flags & ADA_FLAG_CAN_48BIT) 1416201139Smav ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1417201139Smav else 1418201139Smav ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0); 1419201139Smav break; 1420195534Sscottl } 1421201139Smav start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO; 1422201139Smavout: 1423201139Smav start_ccb->ccb_h.ccb_bp = bp; 1424201139Smav softc->outstanding_cmds++; 1425201139Smav xpt_action(start_ccb); 1426201139Smav 1427201139Smav /* May have more work to do, so ensure we stay scheduled */ 1428201139Smav adaschedule(periph); 1429195534Sscottl break; 1430195534Sscottl } 1431224497Smav case ADA_STATE_RAHEAD: 1432220412Smav case ADA_STATE_WCACHE: 1433220412Smav { 1434224497Smav if (softc->flags & ADA_FLAG_PACK_INVALID) { 1435224497Smav softc->state = ADA_STATE_NORMAL; 1436224497Smav xpt_release_ccb(start_ccb); 1437224497Smav cam_release_devq(periph->path, 1438224497Smav RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE); 1439224497Smav adaschedule(periph); 1440224497Smav cam_periph_release_locked(periph); 1441224497Smav return; 1442224497Smav } 1443224497Smav 1444220412Smav cam_fill_ataio(ataio, 1445220412Smav 1, 1446220412Smav adadone, 1447220412Smav CAM_DIR_NONE, 1448220412Smav 0, 1449220412Smav NULL, 1450220412Smav 0, 1451220412Smav ada_default_timeout*1000); 1452220412Smav 1453224497Smav if (softc->state == ADA_STATE_RAHEAD) { 1454224497Smav ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ? 1455224497Smav ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0); 1456224497Smav start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD; 1457224497Smav } else { 1458224497Smav ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ? 1459224497Smav ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0); 1460224497Smav start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE; 1461224497Smav } 1462220412Smav xpt_action(start_ccb); 1463220412Smav break; 1464195534Sscottl } 1465220412Smav } 1466195534Sscottl} 1467195534Sscottl 1468195534Sscottlstatic void 1469195534Sscottladadone(struct cam_periph *periph, union ccb *done_ccb) 1470195534Sscottl{ 1471195534Sscottl struct ada_softc *softc; 1472195534Sscottl struct ccb_ataio *ataio; 1473224497Smav struct ccb_getdev *cgd; 1474195534Sscottl 1475195534Sscottl softc = (struct ada_softc *)periph->softc; 1476195534Sscottl ataio = &done_ccb->ataio; 1477236602Smav 1478236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adadone\n")); 1479236602Smav 1480195534Sscottl switch (ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) { 1481195534Sscottl case ADA_CCB_BUFFER_IO: 1482201139Smav case ADA_CCB_TRIM: 1483195534Sscottl { 1484195534Sscottl struct bio *bp; 1485195534Sscottl 1486195534Sscottl bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 1487195534Sscottl if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1488195534Sscottl int error; 1489195534Sscottl 1490198328Smav error = adaerror(done_ccb, 0, 0); 1491195534Sscottl if (error == ERESTART) { 1492198328Smav /* A retry was scheduled, so just return. */ 1493195534Sscottl return; 1494195534Sscottl } 1495195534Sscottl if (error != 0) { 1496224283Smav if (error == ENXIO && 1497224283Smav (softc->flags & ADA_FLAG_PACK_INVALID) == 0) { 1498195534Sscottl /* 1499195534Sscottl * Catastrophic error. Mark our pack as 1500195534Sscottl * invalid. 1501195534Sscottl */ 1502195534Sscottl /* 1503195534Sscottl * XXX See if this is really a media 1504195534Sscottl * XXX change first? 1505195534Sscottl */ 1506195534Sscottl xpt_print(periph->path, 1507195534Sscottl "Invalidating pack\n"); 1508195534Sscottl softc->flags |= ADA_FLAG_PACK_INVALID; 1509195534Sscottl } 1510195534Sscottl bp->bio_error = error; 1511195534Sscottl bp->bio_resid = bp->bio_bcount; 1512195534Sscottl bp->bio_flags |= BIO_ERROR; 1513195534Sscottl } else { 1514195534Sscottl bp->bio_resid = ataio->resid; 1515195534Sscottl bp->bio_error = 0; 1516195534Sscottl if (bp->bio_resid != 0) 1517195534Sscottl bp->bio_flags |= BIO_ERROR; 1518195534Sscottl } 1519195534Sscottl if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1520195534Sscottl cam_release_devq(done_ccb->ccb_h.path, 1521195534Sscottl /*relsim_flags*/0, 1522195534Sscottl /*reduction*/0, 1523195534Sscottl /*timeout*/0, 1524195534Sscottl /*getcount_only*/0); 1525195534Sscottl } else { 1526195534Sscottl if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1527195534Sscottl panic("REQ_CMP with QFRZN"); 1528195534Sscottl bp->bio_resid = ataio->resid; 1529195534Sscottl if (ataio->resid > 0) 1530195534Sscottl bp->bio_flags |= BIO_ERROR; 1531195534Sscottl } 1532195534Sscottl softc->outstanding_cmds--; 1533195534Sscottl if (softc->outstanding_cmds == 0) 1534195534Sscottl softc->flags |= ADA_FLAG_WENT_IDLE; 1535201139Smav if ((ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) == 1536201139Smav ADA_CCB_TRIM) { 1537201139Smav struct trim_request *req = 1538201139Smav (struct trim_request *)ataio->data_ptr; 1539201139Smav int i; 1540195534Sscottl 1541222628Smav for (i = 1; i < TRIM_MAX_BIOS && req->bps[i]; i++) { 1542201139Smav struct bio *bp1 = req->bps[i]; 1543201139Smav 1544201139Smav bp1->bio_resid = bp->bio_resid; 1545201139Smav bp1->bio_error = bp->bio_error; 1546201139Smav if (bp->bio_flags & BIO_ERROR) 1547201139Smav bp1->bio_flags |= BIO_ERROR; 1548201139Smav biodone(bp1); 1549201139Smav } 1550201139Smav softc->trim_running = 0; 1551201139Smav biodone(bp); 1552201139Smav adaschedule(periph); 1553201139Smav } else 1554201139Smav biodone(bp); 1555195534Sscottl break; 1556195534Sscottl } 1557224497Smav case ADA_CCB_RAHEAD: 1558224497Smav { 1559224497Smav if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1560224497Smav if (adaerror(done_ccb, 0, 0) == ERESTART) { 1561224497Smav return; 1562224497Smav } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1563224497Smav cam_release_devq(done_ccb->ccb_h.path, 1564224497Smav /*relsim_flags*/0, 1565224497Smav /*reduction*/0, 1566224497Smav /*timeout*/0, 1567224497Smav /*getcount_only*/0); 1568224497Smav } 1569224497Smav } 1570224497Smav 1571224497Smav /* 1572224497Smav * Since our peripheral may be invalidated by an error 1573224497Smav * above or an external event, we must release our CCB 1574224497Smav * before releasing the reference on the peripheral. 1575224497Smav * The peripheral will only go away once the last reference 1576224497Smav * is removed, and we need it around for the CCB release 1577224497Smav * operation. 1578224497Smav */ 1579224497Smav cgd = (struct ccb_getdev *)done_ccb; 1580224497Smav xpt_setup_ccb(&cgd->ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1581224497Smav cgd->ccb_h.func_code = XPT_GDEV_TYPE; 1582224497Smav xpt_action((union ccb *)cgd); 1583224497Smav if (ADA_WC >= 0 && 1584224497Smav cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) { 1585224497Smav softc->state = ADA_STATE_WCACHE; 1586224497Smav xpt_release_ccb(done_ccb); 1587224497Smav xpt_schedule(periph, CAM_PRIORITY_DEV); 1588224497Smav return; 1589224497Smav } 1590224497Smav softc->state = ADA_STATE_NORMAL; 1591224497Smav xpt_release_ccb(done_ccb); 1592224497Smav cam_release_devq(periph->path, 1593224497Smav RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE); 1594224497Smav adaschedule(periph); 1595224497Smav cam_periph_release_locked(periph); 1596224497Smav return; 1597224497Smav } 1598220412Smav case ADA_CCB_WCACHE: 1599220412Smav { 1600220412Smav if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1601220412Smav if (adaerror(done_ccb, 0, 0) == ERESTART) { 1602220412Smav return; 1603220412Smav } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1604220412Smav cam_release_devq(done_ccb->ccb_h.path, 1605220412Smav /*relsim_flags*/0, 1606220412Smav /*reduction*/0, 1607220412Smav /*timeout*/0, 1608220412Smav /*getcount_only*/0); 1609220412Smav } 1610220412Smav } 1611220412Smav 1612220412Smav softc->state = ADA_STATE_NORMAL; 1613220412Smav /* 1614220412Smav * Since our peripheral may be invalidated by an error 1615220412Smav * above or an external event, we must release our CCB 1616220412Smav * before releasing the reference on the peripheral. 1617220412Smav * The peripheral will only go away once the last reference 1618220412Smav * is removed, and we need it around for the CCB release 1619220412Smav * operation. 1620220412Smav */ 1621220412Smav xpt_release_ccb(done_ccb); 1622220412Smav cam_release_devq(periph->path, 1623220412Smav RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE); 1624220412Smav adaschedule(periph); 1625220412Smav cam_periph_release_locked(periph); 1626220412Smav return; 1627220412Smav } 1628195534Sscottl case ADA_CCB_WAITING: 1629195534Sscottl { 1630195534Sscottl /* Caller will release the CCB */ 1631195534Sscottl wakeup(&done_ccb->ccb_h.cbfcnp); 1632195534Sscottl return; 1633195534Sscottl } 1634195534Sscottl case ADA_CCB_DUMP: 1635195534Sscottl /* No-op. We're polling */ 1636195534Sscottl return; 1637195534Sscottl default: 1638195534Sscottl break; 1639195534Sscottl } 1640195534Sscottl xpt_release_ccb(done_ccb); 1641195534Sscottl} 1642195534Sscottl 1643195534Sscottlstatic int 1644195534Sscottladaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 1645195534Sscottl{ 1646195534Sscottl 1647203385Smav return(cam_periph_error(ccb, cam_flags, sense_flags, NULL)); 1648195534Sscottl} 1649195534Sscottl 1650195534Sscottlstatic void 1651198897Smavadagetparams(struct cam_periph *periph, struct ccb_getdev *cgd) 1652195534Sscottl{ 1653195534Sscottl struct ada_softc *softc = (struct ada_softc *)periph->softc; 1654195534Sscottl struct disk_params *dp = &softc->params; 1655195534Sscottl u_int64_t lbasize48; 1656195534Sscottl u_int32_t lbasize; 1657195534Sscottl 1658198897Smav dp->secsize = ata_logical_sector_size(&cgd->ident_data); 1659195534Sscottl if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) && 1660195534Sscottl cgd->ident_data.current_heads && cgd->ident_data.current_sectors) { 1661195534Sscottl dp->heads = cgd->ident_data.current_heads; 1662195534Sscottl dp->secs_per_track = cgd->ident_data.current_sectors; 1663195534Sscottl dp->cylinders = cgd->ident_data.cylinders; 1664195534Sscottl dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 | 1665195534Sscottl ((u_int32_t)cgd->ident_data.current_size_2 << 16); 1666195534Sscottl } else { 1667195534Sscottl dp->heads = cgd->ident_data.heads; 1668195534Sscottl dp->secs_per_track = cgd->ident_data.sectors; 1669195534Sscottl dp->cylinders = cgd->ident_data.cylinders; 1670195534Sscottl dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track; 1671195534Sscottl } 1672195534Sscottl lbasize = (u_int32_t)cgd->ident_data.lba_size_1 | 1673195534Sscottl ((u_int32_t)cgd->ident_data.lba_size_2 << 16); 1674195534Sscottl 1675195534Sscottl /* use the 28bit LBA size if valid or bigger than the CHS mapping */ 1676195534Sscottl if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize) 1677195534Sscottl dp->sectors = lbasize; 1678195534Sscottl 1679195534Sscottl /* use the 48bit LBA size if valid */ 1680195534Sscottl lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) | 1681195534Sscottl ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) | 1682195534Sscottl ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) | 1683195534Sscottl ((u_int64_t)cgd->ident_data.lba_size48_4 << 48); 1684195534Sscottl if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) && 1685195534Sscottl lbasize48 > ATA_MAX_28BIT_LBA) 1686195534Sscottl dp->sectors = lbasize48; 1687195534Sscottl} 1688195534Sscottl 1689195534Sscottlstatic void 1690195534Sscottladasendorderedtag(void *arg) 1691195534Sscottl{ 1692195534Sscottl struct ada_softc *softc = arg; 1693195534Sscottl 1694195534Sscottl if (ada_send_ordered) { 1695195534Sscottl if ((softc->ordered_tag_count == 0) 1696195534Sscottl && ((softc->flags & ADA_FLAG_WENT_IDLE) == 0)) { 1697195534Sscottl softc->flags |= ADA_FLAG_NEED_OTAG; 1698195534Sscottl } 1699195534Sscottl if (softc->outstanding_cmds > 0) 1700195534Sscottl softc->flags &= ~ADA_FLAG_WENT_IDLE; 1701195534Sscottl 1702195534Sscottl softc->ordered_tag_count = 0; 1703195534Sscottl } 1704195534Sscottl /* Queue us up again */ 1705195534Sscottl callout_reset(&softc->sendordered_c, 1706230921Smav (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 1707195534Sscottl adasendorderedtag, softc); 1708195534Sscottl} 1709195534Sscottl 1710195534Sscottl/* 1711195534Sscottl * Step through all ADA peripheral drivers, and if the device is still open, 1712195534Sscottl * sync the disk cache to physical media. 1713195534Sscottl */ 1714195534Sscottlstatic void 1715220650Smavadaflush(void) 1716195534Sscottl{ 1717195534Sscottl struct cam_periph *periph; 1718195534Sscottl struct ada_softc *softc; 1719195534Sscottl 1720195534Sscottl TAILQ_FOREACH(periph, &adadriver.units, unit_links) { 1721195534Sscottl union ccb ccb; 1722195534Sscottl 1723200180Smav /* If we paniced with lock held - not recurse here. */ 1724200180Smav if (cam_periph_owned(periph)) 1725200180Smav continue; 1726195534Sscottl cam_periph_lock(periph); 1727195534Sscottl softc = (struct ada_softc *)periph->softc; 1728195534Sscottl /* 1729195534Sscottl * We only sync the cache if the drive is still open, and 1730195534Sscottl * if the drive is capable of it.. 1731195534Sscottl */ 1732195534Sscottl if (((softc->flags & ADA_FLAG_OPEN) == 0) || 1733195534Sscottl (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) { 1734195534Sscottl cam_periph_unlock(periph); 1735195534Sscottl continue; 1736195534Sscottl } 1737195534Sscottl 1738198382Smav xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1739195534Sscottl 1740195534Sscottl ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 1741195534Sscottl cam_fill_ataio(&ccb.ataio, 1742195534Sscottl 1, 1743195534Sscottl adadone, 1744195534Sscottl CAM_DIR_NONE, 1745195534Sscottl 0, 1746195534Sscottl NULL, 1747195534Sscottl 0, 1748195534Sscottl ada_default_timeout*1000); 1749195534Sscottl 1750195534Sscottl if (softc->flags & ADA_FLAG_CAN_48BIT) 1751195534Sscottl ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1752195534Sscottl else 1753196659Smav ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0); 1754195534Sscottl xpt_polled_action(&ccb); 1755195534Sscottl 1756195534Sscottl if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 1757195534Sscottl xpt_print(periph->path, "Synchronize cache failed\n"); 1758195534Sscottl 1759195534Sscottl if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 1760195534Sscottl cam_release_devq(ccb.ccb_h.path, 1761195534Sscottl /*relsim_flags*/0, 1762195534Sscottl /*reduction*/0, 1763195534Sscottl /*timeout*/0, 1764195534Sscottl /*getcount_only*/0); 1765195534Sscottl cam_periph_unlock(periph); 1766195534Sscottl } 1767220650Smav} 1768214279Sbrucec 1769220650Smavstatic void 1770220650Smavadaspindown(uint8_t cmd, int flags) 1771220650Smav{ 1772220650Smav struct cam_periph *periph; 1773220650Smav struct ada_softc *softc; 1774214279Sbrucec 1775214279Sbrucec TAILQ_FOREACH(periph, &adadriver.units, unit_links) { 1776214279Sbrucec union ccb ccb; 1777214279Sbrucec 1778214279Sbrucec /* If we paniced with lock held - not recurse here. */ 1779214279Sbrucec if (cam_periph_owned(periph)) 1780214279Sbrucec continue; 1781214279Sbrucec cam_periph_lock(periph); 1782214279Sbrucec softc = (struct ada_softc *)periph->softc; 1783214279Sbrucec /* 1784214279Sbrucec * We only spin-down the drive if it is capable of it.. 1785214279Sbrucec */ 1786214279Sbrucec if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 1787214279Sbrucec cam_periph_unlock(periph); 1788214279Sbrucec continue; 1789214279Sbrucec } 1790214279Sbrucec 1791214279Sbrucec if (bootverbose) 1792214279Sbrucec xpt_print(periph->path, "spin-down\n"); 1793214279Sbrucec 1794214279Sbrucec xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1795214279Sbrucec 1796214279Sbrucec ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 1797214279Sbrucec cam_fill_ataio(&ccb.ataio, 1798214279Sbrucec 1, 1799214279Sbrucec adadone, 1800220650Smav CAM_DIR_NONE | flags, 1801214279Sbrucec 0, 1802214279Sbrucec NULL, 1803214279Sbrucec 0, 1804214279Sbrucec ada_default_timeout*1000); 1805214279Sbrucec 1806220650Smav ata_28bit_cmd(&ccb.ataio, cmd, 0, 0, 0); 1807214279Sbrucec xpt_polled_action(&ccb); 1808214279Sbrucec 1809214279Sbrucec if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 1810214279Sbrucec xpt_print(periph->path, "Spin-down disk failed\n"); 1811214279Sbrucec 1812214279Sbrucec if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 1813214279Sbrucec cam_release_devq(ccb.ccb_h.path, 1814214279Sbrucec /*relsim_flags*/0, 1815214279Sbrucec /*reduction*/0, 1816214279Sbrucec /*timeout*/0, 1817214279Sbrucec /*getcount_only*/0); 1818214279Sbrucec cam_periph_unlock(periph); 1819214279Sbrucec } 1820195534Sscottl} 1821195534Sscottl 1822220650Smavstatic void 1823220650Smavadashutdown(void *arg, int howto) 1824220650Smav{ 1825220650Smav 1826220650Smav adaflush(); 1827220650Smav if (ada_spindown_shutdown != 0 && 1828220650Smav (howto & (RB_HALT | RB_POWEROFF)) != 0) 1829220650Smav adaspindown(ATA_STANDBY_IMMEDIATE, 0); 1830220650Smav} 1831220650Smav 1832220650Smavstatic void 1833220650Smavadasuspend(void *arg) 1834220650Smav{ 1835220650Smav 1836220650Smav adaflush(); 1837220650Smav if (ada_spindown_suspend != 0) 1838220650Smav adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE); 1839220650Smav} 1840220650Smav 1841220650Smavstatic void 1842220650Smavadaresume(void *arg) 1843220650Smav{ 1844220650Smav struct cam_periph *periph; 1845220650Smav struct ada_softc *softc; 1846220650Smav 1847220650Smav if (ada_spindown_suspend == 0) 1848220650Smav return; 1849220650Smav 1850220650Smav TAILQ_FOREACH(periph, &adadriver.units, unit_links) { 1851220650Smav cam_periph_lock(periph); 1852220650Smav softc = (struct ada_softc *)periph->softc; 1853220650Smav /* 1854220650Smav * We only spin-down the drive if it is capable of it.. 1855220650Smav */ 1856220650Smav if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 1857220650Smav cam_periph_unlock(periph); 1858220650Smav continue; 1859220650Smav } 1860220650Smav 1861220650Smav if (bootverbose) 1862220650Smav xpt_print(periph->path, "resume\n"); 1863220650Smav 1864220650Smav /* 1865220650Smav * Drop freeze taken due to CAM_DEV_QFREEZE flag set on 1866220650Smav * sleep request. 1867220650Smav */ 1868220650Smav cam_release_devq(periph->path, 1869220650Smav /*relsim_flags*/0, 1870220650Smav /*openings*/0, 1871220650Smav /*timeout*/0, 1872220650Smav /*getcount_only*/0); 1873220650Smav 1874220650Smav cam_periph_unlock(periph); 1875220650Smav } 1876220650Smav} 1877220650Smav 1878195534Sscottl#endif /* _KERNEL */ 1879