ata_da.c revision 249940
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 249940 2013-04-26 16:22:54Z smh $"); 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> 46195534Sscottl#include <sys/cons.h> 47214279Sbrucec#include <sys/reboot.h> 48195534Sscottl#include <geom/geom_disk.h> 49195534Sscottl#endif /* _KERNEL */ 50195534Sscottl 51195534Sscottl#ifndef _KERNEL 52195534Sscottl#include <stdio.h> 53195534Sscottl#include <string.h> 54195534Sscottl#endif /* _KERNEL */ 55195534Sscottl 56195534Sscottl#include <cam/cam.h> 57195534Sscottl#include <cam/cam_ccb.h> 58195534Sscottl#include <cam/cam_periph.h> 59195534Sscottl#include <cam/cam_xpt_periph.h> 60195534Sscottl#include <cam/cam_sim.h> 61195534Sscottl 62195534Sscottl#include <cam/ata/ata_all.h> 63195534Sscottl 64208349Smarius#include <machine/md_var.h> /* geometry translation */ 65208349Smarius 66195534Sscottl#ifdef _KERNEL 67195534Sscottl 68195534Sscottl#define ATA_MAX_28BIT_LBA 268435455UL 69195534Sscottl 70195534Sscottltypedef enum { 71224497Smav ADA_STATE_RAHEAD, 72220412Smav ADA_STATE_WCACHE, 73198708Smav ADA_STATE_NORMAL 74195534Sscottl} ada_state; 75195534Sscottl 76195534Sscottltypedef enum { 77249199Smarius ADA_FLAG_PACK_INVALID = 0x0001, 78249199Smarius ADA_FLAG_CAN_48BIT = 0x0002, 79249199Smarius ADA_FLAG_CAN_FLUSHCACHE = 0x0004, 80249199Smarius ADA_FLAG_CAN_NCQ = 0x0008, 81249199Smarius ADA_FLAG_CAN_DMA = 0x0010, 82249199Smarius ADA_FLAG_NEED_OTAG = 0x0020, 83249199Smarius ADA_FLAG_WENT_IDLE = 0x0040, 84249199Smarius ADA_FLAG_CAN_TRIM = 0x0080, 85249199Smarius ADA_FLAG_OPEN = 0x0100, 86249199Smarius ADA_FLAG_SCTX_INIT = 0x0200, 87249199Smarius ADA_FLAG_CAN_CFA = 0x0400, 88249199Smarius ADA_FLAG_CAN_POWERMGT = 0x0800, 89249199Smarius ADA_FLAG_CAN_DMA48 = 0x1000 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 120249934Ssmh#define TRIM_MAX_RANGES (TRIM_MAX_BLOCKS * ATA_DSM_BLK_RANGES) 121222643Smav#define TRIM_MAX_BIOS (TRIM_MAX_RANGES * 4) 122201139Smavstruct trim_request { 123249934Ssmh uint8_t data[TRIM_MAX_RANGES * ATA_DSM_RANGE_SIZE]; 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; 133248922Ssmh int sort_io_queue; 134195534Sscottl int ordered_tag_count; 135195534Sscottl int outstanding_cmds; 136201139Smav int trim_max_ranges; 137201139Smav int trim_running; 138224497Smav int read_ahead; 139220454Smav int write_cache; 140220454Smav#ifdef ADA_TEST_FAILURE 141220454Smav int force_read_error; 142220454Smav int force_write_error; 143220454Smav int periodic_read_error; 144220454Smav int periodic_read_count; 145220454Smav#endif 146195534Sscottl struct disk_params params; 147195534Sscottl struct disk *disk; 148195534Sscottl struct task sysctl_task; 149195534Sscottl struct sysctl_ctx_list sysctl_ctx; 150195534Sscottl struct sysctl_oid *sysctl_tree; 151195534Sscottl struct callout sendordered_c; 152201139Smav struct trim_request trim_req; 153195534Sscottl}; 154195534Sscottl 155195534Sscottlstruct ada_quirk_entry { 156195534Sscottl struct scsi_inquiry_pattern inq_pat; 157195534Sscottl ada_quirks quirks; 158195534Sscottl}; 159195534Sscottl 160199178Smavstatic struct ada_quirk_entry ada_quirk_table[] = 161199178Smav{ 162199178Smav { 163222520Smav /* Hitachi Advanced Format (4k) drives */ 164222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" }, 165222520Smav /*quirks*/ADA_Q_4K 166222520Smav }, 167222520Smav { 168222520Smav /* Samsung Advanced Format (4k) drives */ 169228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD155UI*", "*" }, 170228819Smav /*quirks*/ADA_Q_4K 171228819Smav }, 172228819Smav { 173228819Smav /* Samsung Advanced Format (4k) drives */ 174222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" }, 175222520Smav /*quirks*/ADA_Q_4K 176222520Smav }, 177222520Smav { 178222520Smav /* Seagate Barracuda Green Advanced Format (4k) drives */ 179222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" }, 180222520Smav /*quirks*/ADA_Q_4K 181222520Smav }, 182222520Smav { 183228819Smav /* Seagate Barracuda Advanced Format (4k) drives */ 184228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???DM*", "*" }, 185228819Smav /*quirks*/ADA_Q_4K 186228819Smav }, 187228819Smav { 188228819Smav /* Seagate Barracuda Advanced Format (4k) drives */ 189228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DM*", "*" }, 190228819Smav /*quirks*/ADA_Q_4K 191228819Smav }, 192228819Smav { 193222520Smav /* Seagate Momentus Advanced Format (4k) drives */ 194222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" }, 195222520Smav /*quirks*/ADA_Q_4K 196222520Smav }, 197222520Smav { 198222520Smav /* Seagate Momentus Advanced Format (4k) drives */ 199222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" }, 200222520Smav /*quirks*/ADA_Q_4K 201222520Smav }, 202222520Smav { 203222520Smav /* Seagate Momentus Advanced Format (4k) drives */ 204228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640423AS*", "*" }, 205228819Smav /*quirks*/ADA_Q_4K 206228819Smav }, 207228819Smav { 208228819Smav /* Seagate Momentus Advanced Format (4k) drives */ 209228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640424AS*", "*" }, 210228819Smav /*quirks*/ADA_Q_4K 211228819Smav }, 212228819Smav { 213228819Smav /* Seagate Momentus Advanced Format (4k) drives */ 214222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" }, 215222520Smav /*quirks*/ADA_Q_4K 216222520Smav }, 217222520Smav { 218222520Smav /* Seagate Momentus Advanced Format (4k) drives */ 219222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" }, 220222520Smav /*quirks*/ADA_Q_4K 221222520Smav }, 222222520Smav { 223228819Smav /* Seagate Momentus Advanced Format (4k) drives */ 224228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750423AS*", "*" }, 225228819Smav /*quirks*/ADA_Q_4K 226228819Smav }, 227228819Smav { 228222520Smav /* Seagate Momentus Thin Advanced Format (4k) drives */ 229222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" }, 230222520Smav /*quirks*/ADA_Q_4K 231222520Smav }, 232222520Smav { 233222520Smav /* WDC Caviar Green Advanced Format (4k) drives */ 234222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" }, 235222520Smav /*quirks*/ADA_Q_4K 236222520Smav }, 237222520Smav { 238222520Smav /* WDC Caviar Green Advanced Format (4k) drives */ 239222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" }, 240222520Smav /*quirks*/ADA_Q_4K 241222520Smav }, 242222520Smav { 243222520Smav /* WDC Caviar Green Advanced Format (4k) drives */ 244222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" }, 245222520Smav /*quirks*/ADA_Q_4K 246222520Smav }, 247222520Smav { 248222520Smav /* WDC Caviar Green Advanced Format (4k) drives */ 249222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" }, 250222520Smav /*quirks*/ADA_Q_4K 251222520Smav }, 252222520Smav { 253222520Smav /* WDC Scorpio Black Advanced Format (4k) drives */ 254222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" }, 255222520Smav /*quirks*/ADA_Q_4K 256222520Smav }, 257222520Smav { 258222520Smav /* WDC Scorpio Black Advanced Format (4k) drives */ 259222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" }, 260222520Smav /*quirks*/ADA_Q_4K 261222520Smav }, 262222520Smav { 263222520Smav /* WDC Scorpio Blue Advanced Format (4k) drives */ 264222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" }, 265222520Smav /*quirks*/ADA_Q_4K 266222520Smav }, 267222520Smav { 268222520Smav /* WDC Scorpio Blue Advanced Format (4k) drives */ 269222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" }, 270222520Smav /*quirks*/ADA_Q_4K 271222520Smav }, 272222520Smav { 273241784Seadler /* 274241784Seadler * Corsair Force 2 SSDs 275241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 276241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 277241784Seadler * PR: 169974 278241784Seadler */ 279241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair CSSD-F*", "*" }, 280241784Seadler /*quirks*/ADA_Q_4K 281241784Seadler }, 282241784Seadler { 283241784Seadler /* 284241784Seadler * Corsair Force 3 SSDs 285241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 286241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 287241784Seadler * PR: 169974 288241784Seadler */ 289241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force 3*", "*" }, 290241784Seadler /*quirks*/ADA_Q_4K 291241784Seadler }, 292241784Seadler { 293241784Seadler /* 294241784Seadler * OCZ Agility 3 SSDs 295241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 296241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 297241784Seadler * PR: 169974 298241784Seadler */ 299241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY3*", "*" }, 300241784Seadler /*quirks*/ADA_Q_4K 301241784Seadler }, 302241784Seadler { 303241784Seadler /* 304241784Seadler * OCZ Vertex 2 SSDs (inc pro series) 305241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 306241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 307241784Seadler * PR: 169974 308241784Seadler */ 309241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ?VERTEX2*", "*" }, 310241784Seadler /*quirks*/ADA_Q_4K 311241784Seadler }, 312241784Seadler { 313241784Seadler /* 314241784Seadler * OCZ Vertex 3 SSDs 315241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 316241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 317241784Seadler * PR: 169974 318241784Seadler */ 319241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX3*", "*" }, 320241784Seadler /*quirks*/ADA_Q_4K 321241784Seadler }, 322241784Seadler { 323241784Seadler /* 324241784Seadler * SuperTalent TeraDrive CT SSDs 325241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 326241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 327241784Seadler * PR: 169974 328241784Seadler */ 329241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" }, 330241784Seadler /*quirks*/ADA_Q_4K 331241784Seadler }, 332241784Seadler { 333241784Seadler /* 334241784Seadler * Crucial RealSSD C300 SSDs 335241784Seadler * 4k optimised 336241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 337241784Seadler * PR: 169974 338241784Seadler */ 339241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "C300-CTFDDAC???MAG*", 340241784Seadler "*" }, /*quirks*/ADA_Q_4K 341241784Seadler }, 342241784Seadler { 343241784Seadler /* 344241784Seadler * XceedIOPS SATA SSDs 345241784Seadler * 4k optimised 346241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 347241784Seadler * PR: 169974 348241784Seadler */ 349241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" }, 350241784Seadler /*quirks*/ADA_Q_4K 351241784Seadler }, 352241784Seadler { 353241784Seadler /* 354241784Seadler * Intel 330 Series SSDs 355241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 356241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 357241784Seadler * PR: 169974 358241784Seadler */ 359241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2ct*", "*" }, 360241784Seadler /*quirks*/ADA_Q_4K 361241784Seadler }, 362241784Seadler { 363241784Seadler /* 364241784Seadler * OCZ Deneva R Series SSDs 365241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 366241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 367241784Seadler * PR: 169974 368241784Seadler */ 369241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "DENRSTE251M45*", "*" }, 370241784Seadler /*quirks*/ADA_Q_4K 371241784Seadler }, 372241784Seadler { 373241784Seadler /* 374241784Seadler * Kingston HyperX 3k SSDs 375241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 376241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 377241784Seadler * PR: 169974 378241784Seadler */ 379241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SH103S3*", "*" }, 380241784Seadler /*quirks*/ADA_Q_4K 381241784Seadler }, 382241784Seadler { 383199178Smav /* Default */ 384199178Smav { 385199178Smav T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, 386199178Smav /*vendor*/"*", /*product*/"*", /*revision*/"*" 387199178Smav }, 388199178Smav /*quirks*/0 389199178Smav }, 390199178Smav}; 391195534Sscottl 392195534Sscottlstatic disk_strategy_t adastrategy; 393195534Sscottlstatic dumper_t adadump; 394195534Sscottlstatic periph_init_t adainit; 395195534Sscottlstatic void adaasync(void *callback_arg, u_int32_t code, 396195534Sscottl struct cam_path *path, void *arg); 397195534Sscottlstatic void adasysctlinit(void *context, int pending); 398195534Sscottlstatic periph_ctor_t adaregister; 399195534Sscottlstatic periph_dtor_t adacleanup; 400195534Sscottlstatic periph_start_t adastart; 401195534Sscottlstatic periph_oninv_t adaoninvalidate; 402195534Sscottlstatic void adadone(struct cam_periph *periph, 403195534Sscottl union ccb *done_ccb); 404195534Sscottlstatic int adaerror(union ccb *ccb, u_int32_t cam_flags, 405195534Sscottl u_int32_t sense_flags); 406198897Smavstatic void adagetparams(struct cam_periph *periph, 407195534Sscottl struct ccb_getdev *cgd); 408195534Sscottlstatic timeout_t adasendorderedtag; 409195534Sscottlstatic void adashutdown(void *arg, int howto); 410220650Smavstatic void adasuspend(void *arg); 411220650Smavstatic void adaresume(void *arg); 412195534Sscottl 413221071Smav#ifndef ADA_DEFAULT_LEGACY_ALIASES 414221071Smav#define ADA_DEFAULT_LEGACY_ALIASES 1 415221071Smav#endif 416221071Smav 417195534Sscottl#ifndef ADA_DEFAULT_TIMEOUT 418195534Sscottl#define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */ 419195534Sscottl#endif 420195534Sscottl 421195534Sscottl#ifndef ADA_DEFAULT_RETRY 422195534Sscottl#define ADA_DEFAULT_RETRY 4 423195534Sscottl#endif 424195534Sscottl 425195534Sscottl#ifndef ADA_DEFAULT_SEND_ORDERED 426195534Sscottl#define ADA_DEFAULT_SEND_ORDERED 1 427195534Sscottl#endif 428195534Sscottl 429214279Sbrucec#ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN 430214279Sbrucec#define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1 431214279Sbrucec#endif 432214279Sbrucec 433220650Smav#ifndef ADA_DEFAULT_SPINDOWN_SUSPEND 434220650Smav#define ADA_DEFAULT_SPINDOWN_SUSPEND 1 435220650Smav#endif 436220650Smav 437224497Smav#ifndef ADA_DEFAULT_READ_AHEAD 438224497Smav#define ADA_DEFAULT_READ_AHEAD 1 439224497Smav#endif 440224497Smav 441220412Smav#ifndef ADA_DEFAULT_WRITE_CACHE 442220412Smav#define ADA_DEFAULT_WRITE_CACHE 1 443220412Smav#endif 444220412Smav 445224497Smav#define ADA_RA (softc->read_ahead >= 0 ? \ 446224497Smav softc->read_ahead : ada_read_ahead) 447224497Smav#define ADA_WC (softc->write_cache >= 0 ? \ 448224497Smav softc->write_cache : ada_write_cache) 449248922Ssmh#define ADA_SIO (softc->sort_io_queue >= 0 ? \ 450248922Ssmh softc->sort_io_queue : cam_sort_io_queues) 451224497Smav 452208349Smarius/* 453208349Smarius * Most platforms map firmware geometry to actual, but some don't. If 454208349Smarius * not overridden, default to nothing. 455208349Smarius */ 456208349Smarius#ifndef ata_disk_firmware_geom_adjust 457208349Smarius#define ata_disk_firmware_geom_adjust(disk) 458208349Smarius#endif 459195534Sscottl 460221071Smavstatic int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES; 461195534Sscottlstatic int ada_retry_count = ADA_DEFAULT_RETRY; 462195534Sscottlstatic int ada_default_timeout = ADA_DEFAULT_TIMEOUT; 463195534Sscottlstatic int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED; 464214279Sbrucecstatic int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN; 465220650Smavstatic int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND; 466224497Smavstatic int ada_read_ahead = ADA_DEFAULT_READ_AHEAD; 467220412Smavstatic int ada_write_cache = ADA_DEFAULT_WRITE_CACHE; 468195534Sscottl 469227309Sedstatic SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0, 470195534Sscottl "CAM Direct Access Disk driver"); 471221071SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RW, 472221071Smav &ada_legacy_aliases, 0, "Create legacy-like device aliases"); 473221071SmavTUNABLE_INT("kern.cam.ada.legacy_aliases", &ada_legacy_aliases); 474195534SscottlSYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW, 475195534Sscottl &ada_retry_count, 0, "Normal I/O retry count"); 476195534SscottlTUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count); 477195534SscottlSYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW, 478195534Sscottl &ada_default_timeout, 0, "Normal I/O timeout (in seconds)"); 479195534SscottlTUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout); 480238382SbruefferSYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RW, 481195534Sscottl &ada_send_ordered, 0, "Send Ordered Tags"); 482238382SbruefferTUNABLE_INT("kern.cam.ada.send_ordered", &ada_send_ordered); 483214279SbrucecSYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RW, 484214279Sbrucec &ada_spindown_shutdown, 0, "Spin down upon shutdown"); 485214279SbrucecTUNABLE_INT("kern.cam.ada.spindown_shutdown", &ada_spindown_shutdown); 486220650SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RW, 487220650Smav &ada_spindown_suspend, 0, "Spin down upon suspend"); 488220650SmavTUNABLE_INT("kern.cam.ada.spindown_suspend", &ada_spindown_suspend); 489224497SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RW, 490224497Smav &ada_read_ahead, 0, "Enable disk read-ahead"); 491224497SmavTUNABLE_INT("kern.cam.ada.read_ahead", &ada_read_ahead); 492220412SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RW, 493220412Smav &ada_write_cache, 0, "Enable disk write cache"); 494220412SmavTUNABLE_INT("kern.cam.ada.write_cache", &ada_write_cache); 495195534Sscottl 496195534Sscottl/* 497195534Sscottl * ADA_ORDEREDTAG_INTERVAL determines how often, relative 498195534Sscottl * to the default timeout, we check to see whether an ordered 499195534Sscottl * tagged transaction is appropriate to prevent simple tag 500195534Sscottl * starvation. Since we'd like to ensure that there is at least 501195534Sscottl * 1/2 of the timeout length left for a starved transaction to 502195534Sscottl * complete after we've sent an ordered tag, we must poll at least 503195534Sscottl * four times in every timeout period. This takes care of the worst 504195534Sscottl * case where a starved transaction starts during an interval that 505195534Sscottl * meets the requirement "don't send an ordered tag" test so it takes 506195534Sscottl * us two intervals to determine that a tag must be sent. 507195534Sscottl */ 508195534Sscottl#ifndef ADA_ORDEREDTAG_INTERVAL 509195534Sscottl#define ADA_ORDEREDTAG_INTERVAL 4 510195534Sscottl#endif 511195534Sscottl 512195534Sscottlstatic struct periph_driver adadriver = 513195534Sscottl{ 514195534Sscottl adainit, "ada", 515195534Sscottl TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0 516195534Sscottl}; 517195534Sscottl 518195534SscottlPERIPHDRIVER_DECLARE(ada, adadriver); 519195534Sscottl 520227293Sedstatic MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers"); 521195534Sscottl 522195534Sscottlstatic int 523195534Sscottladaopen(struct disk *dp) 524195534Sscottl{ 525195534Sscottl struct cam_periph *periph; 526195534Sscottl struct ada_softc *softc; 527195534Sscottl int error; 528195534Sscottl 529195534Sscottl periph = (struct cam_periph *)dp->d_drv1; 530195534Sscottl if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 531195534Sscottl return(ENXIO); 532195534Sscottl } 533195534Sscottl 534195534Sscottl cam_periph_lock(periph); 535195534Sscottl if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) { 536195534Sscottl cam_periph_unlock(periph); 537195534Sscottl cam_periph_release(periph); 538195534Sscottl return (error); 539195534Sscottl } 540195534Sscottl 541195534Sscottl softc = (struct ada_softc *)periph->softc; 542195534Sscottl softc->flags |= ADA_FLAG_OPEN; 543195534Sscottl 544236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 545236602Smav ("adaopen\n")); 546195534Sscottl 547195534Sscottl if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) { 548195534Sscottl /* Invalidate our pack information. */ 549195534Sscottl softc->flags &= ~ADA_FLAG_PACK_INVALID; 550195534Sscottl } 551195534Sscottl 552195534Sscottl cam_periph_unhold(periph); 553195534Sscottl cam_periph_unlock(periph); 554195534Sscottl return (0); 555195534Sscottl} 556195534Sscottl 557195534Sscottlstatic int 558195534Sscottladaclose(struct disk *dp) 559195534Sscottl{ 560195534Sscottl struct cam_periph *periph; 561195534Sscottl struct ada_softc *softc; 562195534Sscottl union ccb *ccb; 563195534Sscottl 564195534Sscottl periph = (struct cam_periph *)dp->d_drv1; 565195534Sscottl cam_periph_lock(periph); 566234414Smav if (cam_periph_hold(periph, PRIBIO) != 0) { 567195534Sscottl cam_periph_unlock(periph); 568195534Sscottl cam_periph_release(periph); 569234414Smav return (0); 570195534Sscottl } 571195534Sscottl 572195534Sscottl softc = (struct ada_softc *)periph->softc; 573236602Smav 574236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 575236602Smav ("adaclose\n")); 576236602Smav 577195534Sscottl /* We only sync the cache if the drive is capable of it. */ 578224283Smav if ((softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 && 579224283Smav (softc->flags & ADA_FLAG_PACK_INVALID) == 0) { 580195534Sscottl 581198382Smav ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 582195534Sscottl cam_fill_ataio(&ccb->ataio, 583195534Sscottl 1, 584195534Sscottl adadone, 585195534Sscottl CAM_DIR_NONE, 586195534Sscottl 0, 587195534Sscottl NULL, 588195534Sscottl 0, 589195534Sscottl ada_default_timeout*1000); 590195534Sscottl 591195534Sscottl if (softc->flags & ADA_FLAG_CAN_48BIT) 592195534Sscottl ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 593195534Sscottl else 594196659Smav ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 595236639Smav cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 596198328Smav /*sense_flags*/0, softc->disk->d_devstat); 597195534Sscottl 598195534Sscottl if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 599195534Sscottl xpt_print(periph->path, "Synchronize cache failed\n"); 600195534Sscottl xpt_release_ccb(ccb); 601195534Sscottl } 602195534Sscottl 603195534Sscottl softc->flags &= ~ADA_FLAG_OPEN; 604195534Sscottl cam_periph_unhold(periph); 605195534Sscottl cam_periph_unlock(periph); 606195534Sscottl cam_periph_release(periph); 607195534Sscottl return (0); 608195534Sscottl} 609195534Sscottl 610201139Smavstatic void 611201139Smavadaschedule(struct cam_periph *periph) 612201139Smav{ 613201139Smav struct ada_softc *softc = (struct ada_softc *)periph->softc; 614224531Smav uint32_t prio; 615201139Smav 616249205Smav if (softc->state != ADA_STATE_NORMAL) 617249205Smav return; 618249205Smav 619224531Smav /* Check if cam_periph_getccb() was called. */ 620224531Smav prio = periph->immediate_priority; 621224531Smav 622224531Smav /* Check if we have more work to do. */ 623201139Smav if (bioq_first(&softc->bio_queue) || 624201139Smav (!softc->trim_running && bioq_first(&softc->trim_queue))) { 625224531Smav prio = CAM_PRIORITY_NORMAL; 626201139Smav } 627224531Smav 628224531Smav /* Schedule CCB if any of above is true. */ 629224531Smav if (prio != CAM_PRIORITY_NONE) 630224531Smav xpt_schedule(periph, prio); 631201139Smav} 632201139Smav 633195534Sscottl/* 634195534Sscottl * Actually translate the requested transfer into one the physical driver 635195534Sscottl * can understand. The transfer is described by a buf and will include 636195534Sscottl * only one physical transfer. 637195534Sscottl */ 638195534Sscottlstatic void 639195534Sscottladastrategy(struct bio *bp) 640195534Sscottl{ 641195534Sscottl struct cam_periph *periph; 642195534Sscottl struct ada_softc *softc; 643195534Sscottl 644195534Sscottl periph = (struct cam_periph *)bp->bio_disk->d_drv1; 645195534Sscottl softc = (struct ada_softc *)periph->softc; 646195534Sscottl 647195534Sscottl cam_periph_lock(periph); 648195534Sscottl 649236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp)); 650236602Smav 651195534Sscottl /* 652195534Sscottl * If the device has been made invalid, error out 653195534Sscottl */ 654195534Sscottl if ((softc->flags & ADA_FLAG_PACK_INVALID)) { 655195534Sscottl cam_periph_unlock(periph); 656195534Sscottl biofinish(bp, NULL, ENXIO); 657195534Sscottl return; 658195534Sscottl } 659195534Sscottl 660195534Sscottl /* 661195534Sscottl * Place it in the queue of disk activities for this disk 662195534Sscottl */ 663201139Smav if (bp->bio_cmd == BIO_DELETE && 664248922Ssmh (softc->flags & ADA_FLAG_CAN_TRIM)) { 665248922Ssmh if (ADA_SIO) 666248922Ssmh bioq_disksort(&softc->trim_queue, bp); 667248922Ssmh else 668248922Ssmh bioq_insert_tail(&softc->trim_queue, bp); 669248922Ssmh } else { 670248922Ssmh if (ADA_SIO) 671248922Ssmh bioq_disksort(&softc->bio_queue, bp); 672248922Ssmh else 673248922Ssmh bioq_insert_tail(&softc->bio_queue, bp); 674248922Ssmh } 675195534Sscottl 676195534Sscottl /* 677195534Sscottl * Schedule ourselves for performing the work. 678195534Sscottl */ 679201139Smav adaschedule(periph); 680195534Sscottl cam_periph_unlock(periph); 681195534Sscottl 682195534Sscottl return; 683195534Sscottl} 684195534Sscottl 685195534Sscottlstatic int 686195534Sscottladadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) 687195534Sscottl{ 688195534Sscottl struct cam_periph *periph; 689195534Sscottl struct ada_softc *softc; 690195534Sscottl u_int secsize; 691195534Sscottl union ccb ccb; 692195534Sscottl struct disk *dp; 693195534Sscottl uint64_t lba; 694195534Sscottl uint16_t count; 695236814Smav int error = 0; 696195534Sscottl 697195534Sscottl dp = arg; 698195534Sscottl periph = dp->d_drv1; 699195534Sscottl softc = (struct ada_softc *)periph->softc; 700195534Sscottl cam_periph_lock(periph); 701195534Sscottl secsize = softc->params.secsize; 702195534Sscottl lba = offset / secsize; 703195534Sscottl count = length / secsize; 704195534Sscottl 705195534Sscottl if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) { 706195534Sscottl cam_periph_unlock(periph); 707195534Sscottl return (ENXIO); 708195534Sscottl } 709195534Sscottl 710195534Sscottl if (length > 0) { 711198382Smav xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 712195534Sscottl ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 713195534Sscottl cam_fill_ataio(&ccb.ataio, 714195534Sscottl 0, 715195534Sscottl adadone, 716195534Sscottl CAM_DIR_OUT, 717195534Sscottl 0, 718195534Sscottl (u_int8_t *) virtual, 719195534Sscottl length, 720195534Sscottl ada_default_timeout*1000); 721195534Sscottl if ((softc->flags & ADA_FLAG_CAN_48BIT) && 722195534Sscottl (lba + count >= ATA_MAX_28BIT_LBA || 723195534Sscottl count >= 256)) { 724195534Sscottl ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48, 725195534Sscottl 0, lba, count); 726195534Sscottl } else { 727196659Smav ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA, 728195534Sscottl 0, lba, count); 729195534Sscottl } 730195534Sscottl xpt_polled_action(&ccb); 731195534Sscottl 732236814Smav error = cam_periph_error(&ccb, 733236814Smav 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 734236814Smav if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 735236814Smav cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0, 736236814Smav /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 737236814Smav if (error != 0) 738195534Sscottl printf("Aborting dump due to I/O error.\n"); 739236814Smav 740195534Sscottl cam_periph_unlock(periph); 741236814Smav return (error); 742195534Sscottl } 743195534Sscottl 744195534Sscottl if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) { 745198382Smav xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 746195534Sscottl 747195534Sscottl ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 748195534Sscottl cam_fill_ataio(&ccb.ataio, 749236814Smav 0, 750195534Sscottl adadone, 751195534Sscottl CAM_DIR_NONE, 752195534Sscottl 0, 753195534Sscottl NULL, 754195534Sscottl 0, 755195534Sscottl ada_default_timeout*1000); 756195534Sscottl 757195534Sscottl if (softc->flags & ADA_FLAG_CAN_48BIT) 758195534Sscottl ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0); 759195534Sscottl else 760196659Smav ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0); 761195534Sscottl xpt_polled_action(&ccb); 762195534Sscottl 763236814Smav error = cam_periph_error(&ccb, 764236814Smav 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 765236814Smav if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 766236814Smav cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0, 767236814Smav /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 768236814Smav if (error != 0) 769195534Sscottl xpt_print(periph->path, "Synchronize cache failed\n"); 770195534Sscottl } 771195534Sscottl cam_periph_unlock(periph); 772236814Smav return (error); 773195534Sscottl} 774195534Sscottl 775195534Sscottlstatic void 776195534Sscottladainit(void) 777195534Sscottl{ 778195534Sscottl cam_status status; 779195534Sscottl 780195534Sscottl /* 781195534Sscottl * Install a global async callback. This callback will 782195534Sscottl * receive async callbacks like "new device found". 783195534Sscottl */ 784195534Sscottl status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL); 785195534Sscottl 786195534Sscottl if (status != CAM_REQ_CMP) { 787195534Sscottl printf("ada: Failed to attach master async callback " 788195534Sscottl "due to status 0x%x!\n", status); 789195534Sscottl } else if (ada_send_ordered) { 790195534Sscottl 791220650Smav /* Register our event handlers */ 792220650Smav if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend, 793220650Smav NULL, EVENTHANDLER_PRI_LAST)) == NULL) 794220650Smav printf("adainit: power event registration failed!\n"); 795220650Smav if ((EVENTHANDLER_REGISTER(power_resume, adaresume, 796220650Smav NULL, EVENTHANDLER_PRI_LAST)) == NULL) 797220650Smav printf("adainit: power event registration failed!\n"); 798220650Smav if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown, 799195534Sscottl NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 800195534Sscottl printf("adainit: shutdown event registration failed!\n"); 801195534Sscottl } 802195534Sscottl} 803195534Sscottl 804249347Sken/* 805249347Sken * Callback from GEOM, called when it has finished cleaning up its 806249347Sken * resources. 807249347Sken */ 808195534Sscottlstatic void 809249347Skenadadiskgonecb(struct disk *dp) 810249347Sken{ 811249347Sken struct cam_periph *periph; 812249347Sken 813249347Sken periph = (struct cam_periph *)dp->d_drv1; 814249347Sken 815249347Sken cam_periph_release(periph); 816249347Sken} 817249347Sken 818249347Skenstatic void 819195534Sscottladaoninvalidate(struct cam_periph *periph) 820195534Sscottl{ 821195534Sscottl struct ada_softc *softc; 822195534Sscottl 823195534Sscottl softc = (struct ada_softc *)periph->softc; 824195534Sscottl 825195534Sscottl /* 826195534Sscottl * De-register any async callbacks. 827195534Sscottl */ 828195534Sscottl xpt_register_async(0, adaasync, periph, periph->path); 829195534Sscottl 830195534Sscottl softc->flags |= ADA_FLAG_PACK_INVALID; 831195534Sscottl 832195534Sscottl /* 833195534Sscottl * Return all queued I/O with ENXIO. 834195534Sscottl * XXX Handle any transactions queued to the card 835195534Sscottl * with XPT_ABORT_CCB. 836195534Sscottl */ 837195534Sscottl bioq_flush(&softc->bio_queue, NULL, ENXIO); 838201139Smav bioq_flush(&softc->trim_queue, NULL, ENXIO); 839195534Sscottl 840195534Sscottl disk_gone(softc->disk); 841195534Sscottl xpt_print(periph->path, "lost device\n"); 842195534Sscottl} 843195534Sscottl 844195534Sscottlstatic void 845195534Sscottladacleanup(struct cam_periph *periph) 846195534Sscottl{ 847195534Sscottl struct ada_softc *softc; 848195534Sscottl 849195534Sscottl softc = (struct ada_softc *)periph->softc; 850195534Sscottl 851195534Sscottl xpt_print(periph->path, "removing device entry\n"); 852195534Sscottl cam_periph_unlock(periph); 853195534Sscottl 854195534Sscottl /* 855195534Sscottl * If we can't free the sysctl tree, oh well... 856195534Sscottl */ 857195534Sscottl if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0 858195534Sscottl && sysctl_ctx_free(&softc->sysctl_ctx) != 0) { 859195534Sscottl xpt_print(periph->path, "can't remove sysctl context\n"); 860195534Sscottl } 861195534Sscottl 862195534Sscottl disk_destroy(softc->disk); 863195534Sscottl callout_drain(&softc->sendordered_c); 864195534Sscottl free(softc, M_DEVBUF); 865195534Sscottl cam_periph_lock(periph); 866195534Sscottl} 867195534Sscottl 868195534Sscottlstatic void 869195534Sscottladaasync(void *callback_arg, u_int32_t code, 870195534Sscottl struct cam_path *path, void *arg) 871195534Sscottl{ 872236393Smav struct ccb_getdev cgd; 873195534Sscottl struct cam_periph *periph; 874220412Smav struct ada_softc *softc; 875195534Sscottl 876195534Sscottl periph = (struct cam_periph *)callback_arg; 877195534Sscottl switch (code) { 878195534Sscottl case AC_FOUND_DEVICE: 879195534Sscottl { 880195534Sscottl struct ccb_getdev *cgd; 881195534Sscottl cam_status status; 882195534Sscottl 883195534Sscottl cgd = (struct ccb_getdev *)arg; 884195534Sscottl if (cgd == NULL) 885195534Sscottl break; 886195534Sscottl 887195534Sscottl if (cgd->protocol != PROTO_ATA) 888195534Sscottl break; 889195534Sscottl 890195534Sscottl /* 891195534Sscottl * Allocate a peripheral instance for 892195534Sscottl * this device and start the probe 893195534Sscottl * process. 894195534Sscottl */ 895195534Sscottl status = cam_periph_alloc(adaregister, adaoninvalidate, 896195534Sscottl adacleanup, adastart, 897195534Sscottl "ada", CAM_PERIPH_BIO, 898195534Sscottl cgd->ccb_h.path, adaasync, 899195534Sscottl AC_FOUND_DEVICE, cgd); 900195534Sscottl 901195534Sscottl if (status != CAM_REQ_CMP 902195534Sscottl && status != CAM_REQ_INPROG) 903195534Sscottl printf("adaasync: Unable to attach to new device " 904195534Sscottl "due to status 0x%x\n", status); 905195534Sscottl break; 906195534Sscottl } 907236393Smav case AC_GETDEV_CHANGED: 908236393Smav { 909236393Smav softc = (struct ada_softc *)periph->softc; 910236393Smav xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 911236393Smav cgd.ccb_h.func_code = XPT_GDEV_TYPE; 912236393Smav xpt_action((union ccb *)&cgd); 913236393Smav 914236393Smav if ((cgd.ident_data.capabilities1 & ATA_SUPPORT_DMA) && 915236393Smav (cgd.inq_flags & SID_DMA)) 916236393Smav softc->flags |= ADA_FLAG_CAN_DMA; 917236393Smav else 918236393Smav softc->flags &= ~ADA_FLAG_CAN_DMA; 919249199Smarius if (cgd.ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) { 920249199Smarius softc->flags |= ADA_FLAG_CAN_48BIT; 921249199Smarius if (cgd.inq_flags & SID_DMA48) 922249199Smarius softc->flags |= ADA_FLAG_CAN_DMA48; 923249199Smarius else 924249199Smarius softc->flags &= ~ADA_FLAG_CAN_DMA48; 925249199Smarius } else 926249199Smarius softc->flags &= ~(ADA_FLAG_CAN_48BIT | 927249199Smarius ADA_FLAG_CAN_DMA48); 928236393Smav if ((cgd.ident_data.satacapabilities & ATA_SUPPORT_NCQ) && 929236393Smav (cgd.inq_flags & SID_DMA) && (cgd.inq_flags & SID_CmdQue)) 930236393Smav softc->flags |= ADA_FLAG_CAN_NCQ; 931236393Smav else 932236393Smav softc->flags &= ~ADA_FLAG_CAN_NCQ; 933236393Smav if ((cgd.ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) && 934236393Smav (cgd.inq_flags & SID_DMA)) 935236393Smav softc->flags |= ADA_FLAG_CAN_TRIM; 936236393Smav else 937236393Smav softc->flags &= ~ADA_FLAG_CAN_TRIM; 938236393Smav 939236393Smav cam_periph_async(periph, code, path, arg); 940236393Smav break; 941236393Smav } 942235897Smav case AC_ADVINFO_CHANGED: 943235897Smav { 944235897Smav uintptr_t buftype; 945235897Smav 946235897Smav buftype = (uintptr_t)arg; 947235897Smav if (buftype == CDAI_TYPE_PHYS_PATH) { 948235897Smav struct ada_softc *softc; 949235897Smav 950235897Smav softc = periph->softc; 951235897Smav disk_attr_changed(softc->disk, "GEOM::physpath", 952235897Smav M_NOWAIT); 953235897Smav } 954235897Smav break; 955235897Smav } 956220412Smav case AC_SENT_BDR: 957220412Smav case AC_BUS_RESET: 958220412Smav { 959220412Smav softc = (struct ada_softc *)periph->softc; 960220412Smav cam_periph_async(periph, code, path, arg); 961220412Smav if (softc->state != ADA_STATE_NORMAL) 962220412Smav break; 963220454Smav xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 964220412Smav cgd.ccb_h.func_code = XPT_GDEV_TYPE; 965220412Smav xpt_action((union ccb *)&cgd); 966224497Smav if (ADA_RA >= 0 && 967224497Smav cgd.ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) 968224497Smav softc->state = ADA_STATE_RAHEAD; 969224497Smav else if (ADA_WC >= 0 && 970224497Smav cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) 971224497Smav softc->state = ADA_STATE_WCACHE; 972224497Smav else 973224497Smav break; 974220412Smav cam_periph_acquire(periph); 975220412Smav xpt_schedule(periph, CAM_PRIORITY_DEV); 976220412Smav } 977195534Sscottl default: 978195534Sscottl cam_periph_async(periph, code, path, arg); 979195534Sscottl break; 980195534Sscottl } 981195534Sscottl} 982195534Sscottl 983195534Sscottlstatic void 984195534Sscottladasysctlinit(void *context, int pending) 985195534Sscottl{ 986195534Sscottl struct cam_periph *periph; 987195534Sscottl struct ada_softc *softc; 988195534Sscottl char tmpstr[80], tmpstr2[80]; 989195534Sscottl 990195534Sscottl periph = (struct cam_periph *)context; 991220454Smav 992220454Smav /* periph was held for us when this task was enqueued */ 993220454Smav if (periph->flags & CAM_PERIPH_INVALID) { 994220454Smav cam_periph_release(periph); 995195534Sscottl return; 996220454Smav } 997195534Sscottl 998195534Sscottl softc = (struct ada_softc *)periph->softc; 999195534Sscottl snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number); 1000195534Sscottl snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 1001195534Sscottl 1002195534Sscottl sysctl_ctx_init(&softc->sysctl_ctx); 1003195534Sscottl softc->flags |= ADA_FLAG_SCTX_INIT; 1004195534Sscottl softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1005195534Sscottl SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2, 1006195534Sscottl CTLFLAG_RD, 0, tmpstr); 1007195534Sscottl if (softc->sysctl_tree == NULL) { 1008195534Sscottl printf("adasysctlinit: unable to allocate sysctl tree\n"); 1009195534Sscottl cam_periph_release(periph); 1010195534Sscottl return; 1011195534Sscottl } 1012195534Sscottl 1013220454Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1014224497Smav OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE, 1015224497Smav &softc->read_ahead, 0, "Enable disk read ahead."); 1016224497Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1017220454Smav OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE, 1018220454Smav &softc->write_cache, 0, "Enable disk write cache."); 1019248922Ssmh SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1020248922Ssmh OID_AUTO, "sort_io_queue", CTLFLAG_RW | CTLFLAG_MPSAFE, 1021248922Ssmh &softc->sort_io_queue, 0, 1022248922Ssmh "Sort IO queue to try and optimise disk access patterns"); 1023220454Smav#ifdef ADA_TEST_FAILURE 1024220454Smav /* 1025220454Smav * Add a 'door bell' sysctl which allows one to set it from userland 1026220454Smav * and cause something bad to happen. For the moment, we only allow 1027220454Smav * whacking the next read or write. 1028220454Smav */ 1029220454Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1030220454Smav OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1031220454Smav &softc->force_read_error, 0, 1032220454Smav "Force a read error for the next N reads."); 1033220454Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1034220454Smav OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1035220454Smav &softc->force_write_error, 0, 1036220454Smav "Force a write error for the next N writes."); 1037220454Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1038220454Smav OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1039220454Smav &softc->periodic_read_error, 0, 1040220454Smav "Force a read error every N reads (don't set too low)."); 1041220454Smav#endif 1042195534Sscottl cam_periph_release(periph); 1043195534Sscottl} 1044195534Sscottl 1045223089Sgibbsstatic int 1046223089Sgibbsadagetattr(struct bio *bp) 1047223089Sgibbs{ 1048241485Smav int ret; 1049223089Sgibbs struct cam_periph *periph; 1050223089Sgibbs 1051223089Sgibbs periph = (struct cam_periph *)bp->bio_disk->d_drv1; 1052241485Smav cam_periph_lock(periph); 1053223089Sgibbs ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute, 1054223089Sgibbs periph->path); 1055241485Smav cam_periph_unlock(periph); 1056223089Sgibbs if (ret == 0) 1057223089Sgibbs bp->bio_completed = bp->bio_length; 1058223089Sgibbs return ret; 1059223089Sgibbs} 1060223089Sgibbs 1061195534Sscottlstatic cam_status 1062195534Sscottladaregister(struct cam_periph *periph, void *arg) 1063195534Sscottl{ 1064195534Sscottl struct ada_softc *softc; 1065195534Sscottl struct ccb_pathinq cpi; 1066195534Sscottl struct ccb_getdev *cgd; 1067221071Smav char announce_buf[80], buf1[32]; 1068195534Sscottl struct disk_params *dp; 1069195534Sscottl caddr_t match; 1070195534Sscottl u_int maxio; 1071222520Smav int legacy_id, quirks; 1072195534Sscottl 1073195534Sscottl cgd = (struct ccb_getdev *)arg; 1074195534Sscottl if (cgd == NULL) { 1075195534Sscottl printf("adaregister: no getdev CCB, can't register device\n"); 1076195534Sscottl return(CAM_REQ_CMP_ERR); 1077195534Sscottl } 1078195534Sscottl 1079195534Sscottl softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF, 1080195534Sscottl M_NOWAIT|M_ZERO); 1081195534Sscottl 1082195534Sscottl if (softc == NULL) { 1083195534Sscottl printf("adaregister: Unable to probe new device. " 1084198328Smav "Unable to allocate softc\n"); 1085195534Sscottl return(CAM_REQ_CMP_ERR); 1086195534Sscottl } 1087195534Sscottl 1088195534Sscottl bioq_init(&softc->bio_queue); 1089201139Smav bioq_init(&softc->trim_queue); 1090195534Sscottl 1091236393Smav if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) && 1092220886Smav (cgd->inq_flags & SID_DMA)) 1093198328Smav softc->flags |= ADA_FLAG_CAN_DMA; 1094249199Smarius if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) { 1095195534Sscottl softc->flags |= ADA_FLAG_CAN_48BIT; 1096249199Smarius if (cgd->inq_flags & SID_DMA48) 1097249199Smarius softc->flags |= ADA_FLAG_CAN_DMA48; 1098249199Smarius } 1099195534Sscottl if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE) 1100195534Sscottl softc->flags |= ADA_FLAG_CAN_FLUSHCACHE; 1101214279Sbrucec if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT) 1102214279Sbrucec softc->flags |= ADA_FLAG_CAN_POWERMGT; 1103236393Smav if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) && 1104220886Smav (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue)) 1105195534Sscottl softc->flags |= ADA_FLAG_CAN_NCQ; 1106236393Smav if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) && 1107236393Smav (cgd->inq_flags & SID_DMA)) { 1108201139Smav softc->flags |= ADA_FLAG_CAN_TRIM; 1109201139Smav softc->trim_max_ranges = TRIM_MAX_RANGES; 1110201139Smav if (cgd->ident_data.max_dsm_blocks != 0) { 1111201139Smav softc->trim_max_ranges = 1112249934Ssmh min(cgd->ident_data.max_dsm_blocks * 1113249934Ssmh ATA_DSM_BLK_RANGES, softc->trim_max_ranges); 1114201139Smav } 1115201139Smav } 1116201139Smav if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA) 1117201139Smav softc->flags |= ADA_FLAG_CAN_CFA; 1118195534Sscottl 1119195534Sscottl periph->softc = softc; 1120195534Sscottl 1121195534Sscottl /* 1122195534Sscottl * See if this device has any quirks. 1123195534Sscottl */ 1124199178Smav match = cam_quirkmatch((caddr_t)&cgd->ident_data, 1125199178Smav (caddr_t)ada_quirk_table, 1126199178Smav sizeof(ada_quirk_table)/sizeof(*ada_quirk_table), 1127199178Smav sizeof(*ada_quirk_table), ata_identify_match); 1128195534Sscottl if (match != NULL) 1129195534Sscottl softc->quirks = ((struct ada_quirk_entry *)match)->quirks; 1130195534Sscottl else 1131195534Sscottl softc->quirks = ADA_Q_NONE; 1132195534Sscottl 1133195534Sscottl bzero(&cpi, sizeof(cpi)); 1134203108Smav xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE); 1135195534Sscottl cpi.ccb_h.func_code = XPT_PATH_INQ; 1136195534Sscottl xpt_action((union ccb *)&cpi); 1137195534Sscottl 1138195534Sscottl TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph); 1139195534Sscottl 1140195534Sscottl /* 1141195534Sscottl * Register this media as a disk 1142195534Sscottl */ 1143220618Smav (void)cam_periph_hold(periph, PRIBIO); 1144249106Smav cam_periph_unlock(periph); 1145222520Smav snprintf(announce_buf, sizeof(announce_buf), 1146222520Smav "kern.cam.ada.%d.quirks", periph->unit_number); 1147222520Smav quirks = softc->quirks; 1148222520Smav TUNABLE_INT_FETCH(announce_buf, &quirks); 1149222520Smav softc->quirks = quirks; 1150224497Smav softc->read_ahead = -1; 1151224497Smav snprintf(announce_buf, sizeof(announce_buf), 1152224497Smav "kern.cam.ada.%d.read_ahead", periph->unit_number); 1153224497Smav TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead); 1154220618Smav softc->write_cache = -1; 1155220618Smav snprintf(announce_buf, sizeof(announce_buf), 1156220618Smav "kern.cam.ada.%d.write_cache", periph->unit_number); 1157220618Smav TUNABLE_INT_FETCH(announce_buf, &softc->write_cache); 1158248922Ssmh softc->sort_io_queue = -1; 1159198897Smav adagetparams(periph, cgd); 1160195534Sscottl softc->disk = disk_alloc(); 1161220644Smav softc->disk->d_devstat = devstat_new_entry(periph->periph_name, 1162220644Smav periph->unit_number, softc->params.secsize, 1163220644Smav DEVSTAT_ALL_SUPPORTED, 1164220644Smav DEVSTAT_TYPE_DIRECT | 1165220644Smav XPORT_DEVSTAT_TYPE(cpi.transport), 1166220644Smav DEVSTAT_PRIORITY_DISK); 1167195534Sscottl softc->disk->d_open = adaopen; 1168195534Sscottl softc->disk->d_close = adaclose; 1169195534Sscottl softc->disk->d_strategy = adastrategy; 1170223089Sgibbs softc->disk->d_getattr = adagetattr; 1171195534Sscottl softc->disk->d_dump = adadump; 1172249347Sken softc->disk->d_gone = adadiskgonecb; 1173195534Sscottl softc->disk->d_name = "ada"; 1174195534Sscottl softc->disk->d_drv1 = periph; 1175195534Sscottl maxio = cpi.maxio; /* Honor max I/O size of SIM */ 1176195534Sscottl if (maxio == 0) 1177195534Sscottl maxio = DFLTPHYS; /* traditional default */ 1178195534Sscottl else if (maxio > MAXPHYS) 1179195534Sscottl maxio = MAXPHYS; /* for safety */ 1180201139Smav if (softc->flags & ADA_FLAG_CAN_48BIT) 1181198897Smav maxio = min(maxio, 65536 * softc->params.secsize); 1182195534Sscottl else /* 28bit ATA command limit */ 1183198897Smav maxio = min(maxio, 256 * softc->params.secsize); 1184195534Sscottl softc->disk->d_maxsize = maxio; 1185195534Sscottl softc->disk->d_unit = periph->unit_number; 1186195534Sscottl softc->disk->d_flags = 0; 1187195534Sscottl if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) 1188195534Sscottl softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE; 1189249934Ssmh if (softc->flags & ADA_FLAG_CAN_TRIM) { 1190201139Smav softc->disk->d_flags |= DISKFLAG_CANDELETE; 1191249940Ssmh softc->disk->d_delmaxsize = softc->params.secsize * 1192249940Ssmh ATA_DSM_RANGE_MAX * 1193249940Ssmh softc->trim_max_ranges; 1194249934Ssmh } else if ((softc->flags & ADA_FLAG_CAN_CFA) && 1195249934Ssmh !(softc->flags & ADA_FLAG_CAN_48BIT)) { 1196249934Ssmh softc->disk->d_flags |= DISKFLAG_CANDELETE; 1197249940Ssmh softc->disk->d_delmaxsize = 256 * softc->params.secsize; 1198249940Ssmh } else 1199249940Ssmh softc->disk->d_delmaxsize = maxio; 1200248519Skib if ((cpi.hba_misc & PIM_UNMAPPED) != 0) 1201248519Skib softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO; 1202219056Snwhitehorn strlcpy(softc->disk->d_descr, cgd->ident_data.model, 1203219056Snwhitehorn MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model))); 1204241305Savg strlcpy(softc->disk->d_ident, cgd->ident_data.serial, 1205241305Savg MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial))); 1206210471Smav softc->disk->d_hba_vendor = cpi.hba_vendor; 1207210471Smav softc->disk->d_hba_device = cpi.hba_device; 1208210471Smav softc->disk->d_hba_subvendor = cpi.hba_subvendor; 1209210471Smav softc->disk->d_hba_subdevice = cpi.hba_subdevice; 1210195534Sscottl 1211195534Sscottl softc->disk->d_sectorsize = softc->params.secsize; 1212198897Smav softc->disk->d_mediasize = (off_t)softc->params.sectors * 1213198897Smav softc->params.secsize; 1214200969Smav if (ata_physical_sector_size(&cgd->ident_data) != 1215200969Smav softc->params.secsize) { 1216200969Smav softc->disk->d_stripesize = 1217200969Smav ata_physical_sector_size(&cgd->ident_data); 1218200969Smav softc->disk->d_stripeoffset = (softc->disk->d_stripesize - 1219200969Smav ata_logical_sector_offset(&cgd->ident_data)) % 1220200969Smav softc->disk->d_stripesize; 1221222520Smav } else if (softc->quirks & ADA_Q_4K) { 1222222520Smav softc->disk->d_stripesize = 4096; 1223222520Smav softc->disk->d_stripeoffset = 0; 1224200969Smav } 1225195534Sscottl softc->disk->d_fwsectors = softc->params.secs_per_track; 1226195534Sscottl softc->disk->d_fwheads = softc->params.heads; 1227208349Smarius ata_disk_firmware_geom_adjust(softc->disk); 1228195534Sscottl 1229221071Smav if (ada_legacy_aliases) { 1230221071Smav#ifdef ATA_STATIC_ID 1231221071Smav legacy_id = xpt_path_legacy_ata_id(periph->path); 1232221071Smav#else 1233221071Smav legacy_id = softc->disk->d_unit; 1234221071Smav#endif 1235221071Smav if (legacy_id >= 0) { 1236221071Smav snprintf(announce_buf, sizeof(announce_buf), 1237221071Smav "kern.devalias.%s%d", 1238221071Smav softc->disk->d_name, softc->disk->d_unit); 1239221071Smav snprintf(buf1, sizeof(buf1), 1240221071Smav "ad%d", legacy_id); 1241221071Smav setenv(announce_buf, buf1); 1242221071Smav } 1243221071Smav } else 1244221071Smav legacy_id = -1; 1245249347Sken /* 1246249347Sken * Acquire a reference to the periph before we register with GEOM. 1247249347Sken * We'll release this reference once GEOM calls us back (via 1248249347Sken * adadiskgonecb()) telling us that our provider has been freed. 1249249347Sken */ 1250249347Sken if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 1251249347Sken xpt_print(periph->path, "%s: lost periph during " 1252249347Sken "registration!\n", __func__); 1253249347Sken cam_periph_lock(periph); 1254249347Sken return (CAM_REQ_CMP_ERR); 1255249347Sken } 1256195534Sscottl disk_create(softc->disk, DISK_VERSION); 1257249106Smav cam_periph_lock(periph); 1258220618Smav cam_periph_unhold(periph); 1259195534Sscottl 1260195534Sscottl dp = &softc->params; 1261195534Sscottl snprintf(announce_buf, sizeof(announce_buf), 1262195534Sscottl "%juMB (%ju %u byte sectors: %dH %dS/T %dC)", 1263195534Sscottl (uintmax_t)(((uintmax_t)dp->secsize * 1264195534Sscottl dp->sectors) / (1024*1024)), 1265195534Sscottl (uintmax_t)dp->sectors, 1266195534Sscottl dp->secsize, dp->heads, 1267195534Sscottl dp->secs_per_track, dp->cylinders); 1268195534Sscottl xpt_announce_periph(periph, announce_buf); 1269221071Smav if (legacy_id >= 0) 1270221071Smav printf("%s%d: Previously was known as ad%d\n", 1271221071Smav periph->periph_name, periph->unit_number, legacy_id); 1272220454Smav 1273195534Sscottl /* 1274220454Smav * Create our sysctl variables, now that we know 1275220454Smav * we have successfully attached. 1276220454Smav */ 1277220454Smav cam_periph_acquire(periph); 1278220454Smav taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task); 1279220454Smav 1280220454Smav /* 1281195534Sscottl * Add async callbacks for bus reset and 1282195534Sscottl * bus device reset calls. I don't bother 1283195534Sscottl * checking if this fails as, in most cases, 1284195534Sscottl * the system will function just fine without 1285195534Sscottl * them and the only alternative would be to 1286195534Sscottl * not attach the device on failure. 1287195534Sscottl */ 1288235897Smav xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE | 1289236393Smav AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED, 1290236393Smav adaasync, periph, periph->path); 1291195534Sscottl 1292195534Sscottl /* 1293195534Sscottl * Schedule a periodic event to occasionally send an 1294195534Sscottl * ordered tag to a device. 1295195534Sscottl */ 1296195534Sscottl callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0); 1297195534Sscottl callout_reset(&softc->sendordered_c, 1298230921Smav (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 1299195534Sscottl adasendorderedtag, softc); 1300195534Sscottl 1301224497Smav if (ADA_RA >= 0 && 1302224497Smav cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) { 1303224497Smav softc->state = ADA_STATE_RAHEAD; 1304224497Smav cam_periph_acquire(periph); 1305224497Smav xpt_schedule(periph, CAM_PRIORITY_DEV); 1306224497Smav } else if (ADA_WC >= 0 && 1307220412Smav cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) { 1308220412Smav softc->state = ADA_STATE_WCACHE; 1309220412Smav cam_periph_acquire(periph); 1310220412Smav xpt_schedule(periph, CAM_PRIORITY_DEV); 1311220412Smav } else 1312220412Smav softc->state = ADA_STATE_NORMAL; 1313220412Smav 1314195534Sscottl return(CAM_REQ_CMP); 1315195534Sscottl} 1316195534Sscottl 1317195534Sscottlstatic void 1318195534Sscottladastart(struct cam_periph *periph, union ccb *start_ccb) 1319195534Sscottl{ 1320198328Smav struct ada_softc *softc = (struct ada_softc *)periph->softc; 1321198328Smav struct ccb_ataio *ataio = &start_ccb->ataio; 1322195534Sscottl 1323236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n")); 1324236602Smav 1325195534Sscottl switch (softc->state) { 1326195534Sscottl case ADA_STATE_NORMAL: 1327195534Sscottl { 1328195534Sscottl struct bio *bp; 1329201139Smav u_int8_t tag_code; 1330195534Sscottl 1331201139Smav /* Execute immediate CCB if waiting. */ 1332195534Sscottl if (periph->immediate_priority <= periph->pinfo.priority) { 1333236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1334195534Sscottl ("queuing for immediate ccb\n")); 1335195534Sscottl start_ccb->ccb_h.ccb_state = ADA_CCB_WAITING; 1336195534Sscottl SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 1337195534Sscottl periph_links.sle); 1338195534Sscottl periph->immediate_priority = CAM_PRIORITY_NONE; 1339195534Sscottl wakeup(&periph->ccb_list); 1340201139Smav /* Have more work to do, so ensure we stay scheduled */ 1341201139Smav adaschedule(periph); 1342201139Smav break; 1343201139Smav } 1344201139Smav /* Run TRIM if not running yet. */ 1345201139Smav if (!softc->trim_running && 1346201139Smav (bp = bioq_first(&softc->trim_queue)) != 0) { 1347201139Smav struct trim_request *req = &softc->trim_req; 1348201139Smav struct bio *bp1; 1349222628Smav uint64_t lastlba = (uint64_t)-1; 1350222628Smav int bps = 0, c, lastcount = 0, off, ranges = 0; 1351201139Smav 1352201139Smav softc->trim_running = 1; 1353201139Smav bzero(req, sizeof(*req)); 1354201139Smav bp1 = bp; 1355201139Smav do { 1356201139Smav uint64_t lba = bp1->bio_pblkno; 1357201139Smav int count = bp1->bio_bcount / 1358201139Smav softc->params.secsize; 1359201139Smav 1360201139Smav bioq_remove(&softc->trim_queue, bp1); 1361222628Smav 1362222628Smav /* Try to extend the previous range. */ 1363222628Smav if (lba == lastlba) { 1364249934Ssmh c = min(count, ATA_DSM_RANGE_MAX - lastcount); 1365222628Smav lastcount += c; 1366249934Ssmh off = (ranges - 1) * ATA_DSM_RANGE_SIZE; 1367222628Smav req->data[off + 6] = lastcount & 0xff; 1368222628Smav req->data[off + 7] = 1369222628Smav (lastcount >> 8) & 0xff; 1370222628Smav count -= c; 1371222628Smav lba += c; 1372222628Smav } 1373222628Smav 1374201139Smav while (count > 0) { 1375249934Ssmh c = min(count, ATA_DSM_RANGE_MAX); 1376249934Ssmh off = ranges * ATA_DSM_RANGE_SIZE; 1377201139Smav req->data[off + 0] = lba & 0xff; 1378201139Smav req->data[off + 1] = (lba >> 8) & 0xff; 1379201139Smav req->data[off + 2] = (lba >> 16) & 0xff; 1380201139Smav req->data[off + 3] = (lba >> 24) & 0xff; 1381201139Smav req->data[off + 4] = (lba >> 32) & 0xff; 1382201139Smav req->data[off + 5] = (lba >> 40) & 0xff; 1383201139Smav req->data[off + 6] = c & 0xff; 1384201139Smav req->data[off + 7] = (c >> 8) & 0xff; 1385201139Smav lba += c; 1386201139Smav count -= c; 1387222628Smav lastcount = c; 1388201139Smav ranges++; 1389249934Ssmh /* 1390249934Ssmh * Its the caller's responsibility to ensure the 1391249934Ssmh * request will fit so we don't need to check for 1392249934Ssmh * overrun here 1393249934Ssmh */ 1394201139Smav } 1395222628Smav lastlba = lba; 1396201139Smav req->bps[bps++] = bp1; 1397201139Smav bp1 = bioq_first(&softc->trim_queue); 1398222628Smav if (bps >= TRIM_MAX_BIOS || 1399222628Smav bp1 == NULL || 1400201139Smav bp1->bio_bcount / softc->params.secsize > 1401249934Ssmh (softc->trim_max_ranges - ranges) * 1402249934Ssmh ATA_DSM_RANGE_MAX) 1403201139Smav break; 1404201139Smav } while (1); 1405201139Smav cam_fill_ataio(ataio, 1406201139Smav ada_retry_count, 1407201139Smav adadone, 1408201139Smav CAM_DIR_OUT, 1409201139Smav 0, 1410201139Smav req->data, 1411249934Ssmh ((ranges + ATA_DSM_BLK_RANGES - 1) / 1412249934Ssmh ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE, 1413201139Smav ada_default_timeout * 1000); 1414201139Smav ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT, 1415249934Ssmh ATA_DSM_TRIM, 0, (ranges + ATA_DSM_BLK_RANGES - 1416249934Ssmh 1) / ATA_DSM_BLK_RANGES); 1417201139Smav start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM; 1418201139Smav goto out; 1419201139Smav } 1420201139Smav /* Run regular command. */ 1421201139Smav bp = bioq_first(&softc->bio_queue); 1422201139Smav if (bp == NULL) { 1423195534Sscottl xpt_release_ccb(start_ccb); 1424201139Smav break; 1425201139Smav } 1426201139Smav bioq_remove(&softc->bio_queue, bp); 1427201139Smav 1428212160Sgibbs if ((bp->bio_flags & BIO_ORDERED) != 0 1429212160Sgibbs || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) { 1430201139Smav softc->flags &= ~ADA_FLAG_NEED_OTAG; 1431201139Smav softc->ordered_tag_count++; 1432201139Smav tag_code = 0; 1433195534Sscottl } else { 1434201139Smav tag_code = 1; 1435201139Smav } 1436201139Smav switch (bp->bio_cmd) { 1437201139Smav case BIO_READ: 1438201139Smav case BIO_WRITE: 1439201139Smav { 1440201139Smav uint64_t lba = bp->bio_pblkno; 1441201139Smav uint16_t count = bp->bio_bcount / softc->params.secsize; 1442220454Smav#ifdef ADA_TEST_FAILURE 1443220454Smav int fail = 0; 1444195534Sscottl 1445220454Smav /* 1446220454Smav * Support the failure ioctls. If the command is a 1447220454Smav * read, and there are pending forced read errors, or 1448220454Smav * if a write and pending write errors, then fail this 1449220454Smav * operation with EIO. This is useful for testing 1450220454Smav * purposes. Also, support having every Nth read fail. 1451220454Smav * 1452220454Smav * This is a rather blunt tool. 1453220454Smav */ 1454220454Smav if (bp->bio_cmd == BIO_READ) { 1455220454Smav if (softc->force_read_error) { 1456220454Smav softc->force_read_error--; 1457220454Smav fail = 1; 1458220454Smav } 1459220454Smav if (softc->periodic_read_error > 0) { 1460220454Smav if (++softc->periodic_read_count >= 1461220454Smav softc->periodic_read_error) { 1462220454Smav softc->periodic_read_count = 0; 1463220454Smav fail = 1; 1464220454Smav } 1465220454Smav } 1466220454Smav } else { 1467220454Smav if (softc->force_write_error) { 1468220454Smav softc->force_write_error--; 1469220454Smav fail = 1; 1470220454Smav } 1471220454Smav } 1472220454Smav if (fail) { 1473220454Smav bp->bio_error = EIO; 1474220454Smav bp->bio_flags |= BIO_ERROR; 1475220454Smav biodone(bp); 1476220454Smav xpt_release_ccb(start_ccb); 1477220454Smav adaschedule(periph); 1478220454Smav return; 1479220454Smav } 1480220454Smav#endif 1481248519Skib KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 || 1482248519Skib round_page(bp->bio_bcount + bp->bio_ma_offset) / 1483248519Skib PAGE_SIZE == bp->bio_ma_n, 1484248519Skib ("Short bio %p", bp)); 1485201139Smav cam_fill_ataio(ataio, 1486201139Smav ada_retry_count, 1487201139Smav adadone, 1488248519Skib (bp->bio_cmd == BIO_READ ? CAM_DIR_IN : 1489248519Skib CAM_DIR_OUT) | ((bp->bio_flags & BIO_UNMAPPED) 1490248519Skib != 0 ? CAM_DATA_BIO : 0), 1491201139Smav tag_code, 1492248519Skib ((bp->bio_flags & BIO_UNMAPPED) != 0) ? (void *)bp : 1493248519Skib bp->bio_data, 1494201139Smav bp->bio_bcount, 1495201139Smav ada_default_timeout*1000); 1496195534Sscottl 1497201139Smav if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) { 1498201139Smav if (bp->bio_cmd == BIO_READ) { 1499201139Smav ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED, 1500201139Smav lba, count); 1501201139Smav } else { 1502201139Smav ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED, 1503201139Smav lba, count); 1504201139Smav } 1505201139Smav } else if ((softc->flags & ADA_FLAG_CAN_48BIT) && 1506201139Smav (lba + count >= ATA_MAX_28BIT_LBA || 1507201139Smav count > 256)) { 1508249199Smarius if (softc->flags & ADA_FLAG_CAN_DMA48) { 1509195534Sscottl if (bp->bio_cmd == BIO_READ) { 1510201139Smav ata_48bit_cmd(ataio, ATA_READ_DMA48, 1511201139Smav 0, lba, count); 1512195534Sscottl } else { 1513201139Smav ata_48bit_cmd(ataio, ATA_WRITE_DMA48, 1514201139Smav 0, lba, count); 1515195534Sscottl } 1516201139Smav } else { 1517201139Smav if (bp->bio_cmd == BIO_READ) { 1518201139Smav ata_48bit_cmd(ataio, ATA_READ_MUL48, 1519201139Smav 0, lba, count); 1520195534Sscottl } else { 1521201139Smav ata_48bit_cmd(ataio, ATA_WRITE_MUL48, 1522201139Smav 0, lba, count); 1523195534Sscottl } 1524201139Smav } 1525201139Smav } else { 1526201139Smav if (count == 256) 1527201139Smav count = 0; 1528201139Smav if (softc->flags & ADA_FLAG_CAN_DMA) { 1529201139Smav if (bp->bio_cmd == BIO_READ) { 1530201139Smav ata_28bit_cmd(ataio, ATA_READ_DMA, 1531201139Smav 0, lba, count); 1532201139Smav } else { 1533201139Smav ata_28bit_cmd(ataio, ATA_WRITE_DMA, 1534201139Smav 0, lba, count); 1535201139Smav } 1536195534Sscottl } else { 1537201139Smav if (bp->bio_cmd == BIO_READ) { 1538201139Smav ata_28bit_cmd(ataio, ATA_READ_MUL, 1539201139Smav 0, lba, count); 1540195534Sscottl } else { 1541201139Smav ata_28bit_cmd(ataio, ATA_WRITE_MUL, 1542201139Smav 0, lba, count); 1543195534Sscottl } 1544195534Sscottl } 1545195534Sscottl } 1546201139Smav break; 1547201139Smav } 1548201139Smav case BIO_DELETE: 1549201139Smav { 1550201139Smav uint64_t lba = bp->bio_pblkno; 1551201139Smav uint16_t count = bp->bio_bcount / softc->params.secsize; 1552195534Sscottl 1553201139Smav cam_fill_ataio(ataio, 1554201139Smav ada_retry_count, 1555201139Smav adadone, 1556201139Smav CAM_DIR_NONE, 1557201139Smav 0, 1558201139Smav NULL, 1559201139Smav 0, 1560201139Smav ada_default_timeout*1000); 1561201139Smav 1562201139Smav if (count >= 256) 1563201139Smav count = 0; 1564201139Smav ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count); 1565201139Smav break; 1566195534Sscottl } 1567201139Smav case BIO_FLUSH: 1568201139Smav cam_fill_ataio(ataio, 1569201139Smav 1, 1570201139Smav adadone, 1571201139Smav CAM_DIR_NONE, 1572201139Smav 0, 1573201139Smav NULL, 1574201139Smav 0, 1575201139Smav ada_default_timeout*1000); 1576201139Smav 1577201139Smav if (softc->flags & ADA_FLAG_CAN_48BIT) 1578201139Smav ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1579201139Smav else 1580201139Smav ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0); 1581201139Smav break; 1582195534Sscottl } 1583201139Smav start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO; 1584201139Smavout: 1585201139Smav start_ccb->ccb_h.ccb_bp = bp; 1586201139Smav softc->outstanding_cmds++; 1587201139Smav xpt_action(start_ccb); 1588201139Smav 1589201139Smav /* May have more work to do, so ensure we stay scheduled */ 1590201139Smav adaschedule(periph); 1591195534Sscottl break; 1592195534Sscottl } 1593224497Smav case ADA_STATE_RAHEAD: 1594220412Smav case ADA_STATE_WCACHE: 1595220412Smav { 1596224497Smav if (softc->flags & ADA_FLAG_PACK_INVALID) { 1597224497Smav softc->state = ADA_STATE_NORMAL; 1598224497Smav xpt_release_ccb(start_ccb); 1599224497Smav adaschedule(periph); 1600224497Smav cam_periph_release_locked(periph); 1601224497Smav return; 1602224497Smav } 1603224497Smav 1604220412Smav cam_fill_ataio(ataio, 1605220412Smav 1, 1606220412Smav adadone, 1607220412Smav CAM_DIR_NONE, 1608220412Smav 0, 1609220412Smav NULL, 1610220412Smav 0, 1611220412Smav ada_default_timeout*1000); 1612220412Smav 1613224497Smav if (softc->state == ADA_STATE_RAHEAD) { 1614224497Smav ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ? 1615224497Smav ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0); 1616224497Smav start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD; 1617224497Smav } else { 1618224497Smav ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ? 1619224497Smav ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0); 1620224497Smav start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE; 1621224497Smav } 1622249466Smav start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 1623220412Smav xpt_action(start_ccb); 1624220412Smav break; 1625195534Sscottl } 1626220412Smav } 1627195534Sscottl} 1628195534Sscottl 1629195534Sscottlstatic void 1630195534Sscottladadone(struct cam_periph *periph, union ccb *done_ccb) 1631195534Sscottl{ 1632195534Sscottl struct ada_softc *softc; 1633195534Sscottl struct ccb_ataio *ataio; 1634224497Smav struct ccb_getdev *cgd; 1635249466Smav struct cam_path *path; 1636195534Sscottl 1637195534Sscottl softc = (struct ada_softc *)periph->softc; 1638195534Sscottl ataio = &done_ccb->ataio; 1639249466Smav path = done_ccb->ccb_h.path; 1640236602Smav 1641249466Smav CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n")); 1642236602Smav 1643195534Sscottl switch (ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) { 1644195534Sscottl case ADA_CCB_BUFFER_IO: 1645201139Smav case ADA_CCB_TRIM: 1646195534Sscottl { 1647195534Sscottl struct bio *bp; 1648195534Sscottl 1649195534Sscottl bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 1650195534Sscottl if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1651195534Sscottl int error; 1652195534Sscottl 1653198328Smav error = adaerror(done_ccb, 0, 0); 1654195534Sscottl if (error == ERESTART) { 1655198328Smav /* A retry was scheduled, so just return. */ 1656195534Sscottl return; 1657195534Sscottl } 1658195534Sscottl if (error != 0) { 1659224283Smav if (error == ENXIO && 1660224283Smav (softc->flags & ADA_FLAG_PACK_INVALID) == 0) { 1661195534Sscottl /* 1662195534Sscottl * Catastrophic error. Mark our pack as 1663195534Sscottl * invalid. 1664195534Sscottl */ 1665195534Sscottl /* 1666195534Sscottl * XXX See if this is really a media 1667195534Sscottl * XXX change first? 1668195534Sscottl */ 1669249466Smav xpt_print(path, "Invalidating pack\n"); 1670195534Sscottl softc->flags |= ADA_FLAG_PACK_INVALID; 1671195534Sscottl } 1672195534Sscottl bp->bio_error = error; 1673195534Sscottl bp->bio_resid = bp->bio_bcount; 1674195534Sscottl bp->bio_flags |= BIO_ERROR; 1675195534Sscottl } else { 1676195534Sscottl bp->bio_resid = ataio->resid; 1677195534Sscottl bp->bio_error = 0; 1678195534Sscottl if (bp->bio_resid != 0) 1679195534Sscottl bp->bio_flags |= BIO_ERROR; 1680195534Sscottl } 1681195534Sscottl if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1682249466Smav cam_release_devq(path, 1683195534Sscottl /*relsim_flags*/0, 1684195534Sscottl /*reduction*/0, 1685195534Sscottl /*timeout*/0, 1686195534Sscottl /*getcount_only*/0); 1687195534Sscottl } else { 1688195534Sscottl if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1689195534Sscottl panic("REQ_CMP with QFRZN"); 1690195534Sscottl bp->bio_resid = ataio->resid; 1691195534Sscottl if (ataio->resid > 0) 1692195534Sscottl bp->bio_flags |= BIO_ERROR; 1693195534Sscottl } 1694195534Sscottl softc->outstanding_cmds--; 1695195534Sscottl if (softc->outstanding_cmds == 0) 1696195534Sscottl softc->flags |= ADA_FLAG_WENT_IDLE; 1697201139Smav if ((ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) == 1698201139Smav ADA_CCB_TRIM) { 1699201139Smav struct trim_request *req = 1700201139Smav (struct trim_request *)ataio->data_ptr; 1701201139Smav int i; 1702195534Sscottl 1703222628Smav for (i = 1; i < TRIM_MAX_BIOS && req->bps[i]; i++) { 1704201139Smav struct bio *bp1 = req->bps[i]; 1705201139Smav 1706201139Smav bp1->bio_resid = bp->bio_resid; 1707201139Smav bp1->bio_error = bp->bio_error; 1708201139Smav if (bp->bio_flags & BIO_ERROR) 1709201139Smav bp1->bio_flags |= BIO_ERROR; 1710201139Smav biodone(bp1); 1711201139Smav } 1712201139Smav softc->trim_running = 0; 1713201139Smav biodone(bp); 1714201139Smav adaschedule(periph); 1715201139Smav } else 1716201139Smav biodone(bp); 1717195534Sscottl break; 1718195534Sscottl } 1719224497Smav case ADA_CCB_RAHEAD: 1720224497Smav { 1721224497Smav if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1722224497Smav if (adaerror(done_ccb, 0, 0) == ERESTART) { 1723249466Smavout: 1724249466Smav /* Drop freeze taken due to CAM_DEV_QFREEZE */ 1725249466Smav cam_release_devq(path, 0, 0, 0, FALSE); 1726224497Smav return; 1727224497Smav } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1728249466Smav cam_release_devq(path, 1729224497Smav /*relsim_flags*/0, 1730224497Smav /*reduction*/0, 1731224497Smav /*timeout*/0, 1732224497Smav /*getcount_only*/0); 1733224497Smav } 1734224497Smav } 1735224497Smav 1736224497Smav /* 1737224497Smav * Since our peripheral may be invalidated by an error 1738224497Smav * above or an external event, we must release our CCB 1739224497Smav * before releasing the reference on the peripheral. 1740224497Smav * The peripheral will only go away once the last reference 1741224497Smav * is removed, and we need it around for the CCB release 1742224497Smav * operation. 1743224497Smav */ 1744224497Smav cgd = (struct ccb_getdev *)done_ccb; 1745249466Smav xpt_setup_ccb(&cgd->ccb_h, path, CAM_PRIORITY_NORMAL); 1746224497Smav cgd->ccb_h.func_code = XPT_GDEV_TYPE; 1747224497Smav xpt_action((union ccb *)cgd); 1748224497Smav if (ADA_WC >= 0 && 1749224497Smav cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) { 1750224497Smav softc->state = ADA_STATE_WCACHE; 1751224497Smav xpt_release_ccb(done_ccb); 1752224497Smav xpt_schedule(periph, CAM_PRIORITY_DEV); 1753249466Smav goto out; 1754224497Smav } 1755224497Smav softc->state = ADA_STATE_NORMAL; 1756224497Smav xpt_release_ccb(done_ccb); 1757249466Smav /* Drop freeze taken due to CAM_DEV_QFREEZE */ 1758249466Smav cam_release_devq(path, 0, 0, 0, FALSE); 1759224497Smav adaschedule(periph); 1760224497Smav cam_periph_release_locked(periph); 1761224497Smav return; 1762224497Smav } 1763220412Smav case ADA_CCB_WCACHE: 1764220412Smav { 1765220412Smav if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1766220412Smav if (adaerror(done_ccb, 0, 0) == ERESTART) { 1767249466Smav goto out; 1768220412Smav } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1769249466Smav cam_release_devq(path, 1770220412Smav /*relsim_flags*/0, 1771220412Smav /*reduction*/0, 1772220412Smav /*timeout*/0, 1773220412Smav /*getcount_only*/0); 1774220412Smav } 1775220412Smav } 1776220412Smav 1777220412Smav softc->state = ADA_STATE_NORMAL; 1778220412Smav /* 1779220412Smav * Since our peripheral may be invalidated by an error 1780220412Smav * above or an external event, we must release our CCB 1781220412Smav * before releasing the reference on the peripheral. 1782220412Smav * The peripheral will only go away once the last reference 1783220412Smav * is removed, and we need it around for the CCB release 1784220412Smav * operation. 1785220412Smav */ 1786220412Smav xpt_release_ccb(done_ccb); 1787249466Smav /* Drop freeze taken due to CAM_DEV_QFREEZE */ 1788249466Smav cam_release_devq(path, 0, 0, 0, FALSE); 1789220412Smav adaschedule(periph); 1790220412Smav cam_periph_release_locked(periph); 1791220412Smav return; 1792220412Smav } 1793195534Sscottl case ADA_CCB_WAITING: 1794195534Sscottl { 1795195534Sscottl /* Caller will release the CCB */ 1796195534Sscottl wakeup(&done_ccb->ccb_h.cbfcnp); 1797195534Sscottl return; 1798195534Sscottl } 1799195534Sscottl case ADA_CCB_DUMP: 1800195534Sscottl /* No-op. We're polling */ 1801195534Sscottl return; 1802195534Sscottl default: 1803195534Sscottl break; 1804195534Sscottl } 1805195534Sscottl xpt_release_ccb(done_ccb); 1806195534Sscottl} 1807195534Sscottl 1808195534Sscottlstatic int 1809195534Sscottladaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 1810195534Sscottl{ 1811195534Sscottl 1812203385Smav return(cam_periph_error(ccb, cam_flags, sense_flags, NULL)); 1813195534Sscottl} 1814195534Sscottl 1815195534Sscottlstatic void 1816198897Smavadagetparams(struct cam_periph *periph, struct ccb_getdev *cgd) 1817195534Sscottl{ 1818195534Sscottl struct ada_softc *softc = (struct ada_softc *)periph->softc; 1819195534Sscottl struct disk_params *dp = &softc->params; 1820195534Sscottl u_int64_t lbasize48; 1821195534Sscottl u_int32_t lbasize; 1822195534Sscottl 1823198897Smav dp->secsize = ata_logical_sector_size(&cgd->ident_data); 1824195534Sscottl if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) && 1825195534Sscottl cgd->ident_data.current_heads && cgd->ident_data.current_sectors) { 1826195534Sscottl dp->heads = cgd->ident_data.current_heads; 1827195534Sscottl dp->secs_per_track = cgd->ident_data.current_sectors; 1828195534Sscottl dp->cylinders = cgd->ident_data.cylinders; 1829195534Sscottl dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 | 1830195534Sscottl ((u_int32_t)cgd->ident_data.current_size_2 << 16); 1831195534Sscottl } else { 1832195534Sscottl dp->heads = cgd->ident_data.heads; 1833195534Sscottl dp->secs_per_track = cgd->ident_data.sectors; 1834195534Sscottl dp->cylinders = cgd->ident_data.cylinders; 1835195534Sscottl dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track; 1836195534Sscottl } 1837195534Sscottl lbasize = (u_int32_t)cgd->ident_data.lba_size_1 | 1838195534Sscottl ((u_int32_t)cgd->ident_data.lba_size_2 << 16); 1839195534Sscottl 1840195534Sscottl /* use the 28bit LBA size if valid or bigger than the CHS mapping */ 1841195534Sscottl if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize) 1842195534Sscottl dp->sectors = lbasize; 1843195534Sscottl 1844195534Sscottl /* use the 48bit LBA size if valid */ 1845195534Sscottl lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) | 1846195534Sscottl ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) | 1847195534Sscottl ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) | 1848195534Sscottl ((u_int64_t)cgd->ident_data.lba_size48_4 << 48); 1849195534Sscottl if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) && 1850195534Sscottl lbasize48 > ATA_MAX_28BIT_LBA) 1851195534Sscottl dp->sectors = lbasize48; 1852195534Sscottl} 1853195534Sscottl 1854195534Sscottlstatic void 1855195534Sscottladasendorderedtag(void *arg) 1856195534Sscottl{ 1857195534Sscottl struct ada_softc *softc = arg; 1858195534Sscottl 1859195534Sscottl if (ada_send_ordered) { 1860195534Sscottl if ((softc->ordered_tag_count == 0) 1861195534Sscottl && ((softc->flags & ADA_FLAG_WENT_IDLE) == 0)) { 1862195534Sscottl softc->flags |= ADA_FLAG_NEED_OTAG; 1863195534Sscottl } 1864195534Sscottl if (softc->outstanding_cmds > 0) 1865195534Sscottl softc->flags &= ~ADA_FLAG_WENT_IDLE; 1866195534Sscottl 1867195534Sscottl softc->ordered_tag_count = 0; 1868195534Sscottl } 1869195534Sscottl /* Queue us up again */ 1870195534Sscottl callout_reset(&softc->sendordered_c, 1871230921Smav (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 1872195534Sscottl adasendorderedtag, softc); 1873195534Sscottl} 1874195534Sscottl 1875195534Sscottl/* 1876195534Sscottl * Step through all ADA peripheral drivers, and if the device is still open, 1877195534Sscottl * sync the disk cache to physical media. 1878195534Sscottl */ 1879195534Sscottlstatic void 1880220650Smavadaflush(void) 1881195534Sscottl{ 1882195534Sscottl struct cam_periph *periph; 1883195534Sscottl struct ada_softc *softc; 1884248872Smav union ccb *ccb; 1885236814Smav int error; 1886195534Sscottl 1887248868Smav CAM_PERIPH_FOREACH(periph, &adadriver) { 1888200180Smav /* If we paniced with lock held - not recurse here. */ 1889200180Smav if (cam_periph_owned(periph)) 1890200180Smav continue; 1891195534Sscottl cam_periph_lock(periph); 1892195534Sscottl softc = (struct ada_softc *)periph->softc; 1893195534Sscottl /* 1894195534Sscottl * We only sync the cache if the drive is still open, and 1895195534Sscottl * if the drive is capable of it.. 1896195534Sscottl */ 1897195534Sscottl if (((softc->flags & ADA_FLAG_OPEN) == 0) || 1898195534Sscottl (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) { 1899195534Sscottl cam_periph_unlock(periph); 1900195534Sscottl continue; 1901195534Sscottl } 1902195534Sscottl 1903248872Smav ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1904248872Smav cam_fill_ataio(&ccb->ataio, 1905236814Smav 0, 1906195534Sscottl adadone, 1907195534Sscottl CAM_DIR_NONE, 1908195534Sscottl 0, 1909195534Sscottl NULL, 1910195534Sscottl 0, 1911195534Sscottl ada_default_timeout*1000); 1912195534Sscottl if (softc->flags & ADA_FLAG_CAN_48BIT) 1913248872Smav ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1914195534Sscottl else 1915248872Smav ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 1916195534Sscottl 1917248872Smav error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 1918248872Smav /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 1919248872Smav softc->disk->d_devstat); 1920236814Smav if (error != 0) 1921195534Sscottl xpt_print(periph->path, "Synchronize cache failed\n"); 1922249048Smav xpt_release_ccb(ccb); 1923195534Sscottl cam_periph_unlock(periph); 1924195534Sscottl } 1925220650Smav} 1926214279Sbrucec 1927220650Smavstatic void 1928220650Smavadaspindown(uint8_t cmd, int flags) 1929220650Smav{ 1930220650Smav struct cam_periph *periph; 1931220650Smav struct ada_softc *softc; 1932248872Smav union ccb *ccb; 1933236814Smav int error; 1934214279Sbrucec 1935248868Smav CAM_PERIPH_FOREACH(periph, &adadriver) { 1936214279Sbrucec /* If we paniced with lock held - not recurse here. */ 1937214279Sbrucec if (cam_periph_owned(periph)) 1938214279Sbrucec continue; 1939214279Sbrucec cam_periph_lock(periph); 1940214279Sbrucec softc = (struct ada_softc *)periph->softc; 1941214279Sbrucec /* 1942214279Sbrucec * We only spin-down the drive if it is capable of it.. 1943214279Sbrucec */ 1944214279Sbrucec if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 1945214279Sbrucec cam_periph_unlock(periph); 1946214279Sbrucec continue; 1947214279Sbrucec } 1948214279Sbrucec 1949214279Sbrucec if (bootverbose) 1950214279Sbrucec xpt_print(periph->path, "spin-down\n"); 1951214279Sbrucec 1952248872Smav ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1953248872Smav cam_fill_ataio(&ccb->ataio, 1954236814Smav 0, 1955214279Sbrucec adadone, 1956220650Smav CAM_DIR_NONE | flags, 1957214279Sbrucec 0, 1958214279Sbrucec NULL, 1959214279Sbrucec 0, 1960214279Sbrucec ada_default_timeout*1000); 1961248872Smav ata_28bit_cmd(&ccb->ataio, cmd, 0, 0, 0); 1962214279Sbrucec 1963248872Smav error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 1964248872Smav /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 1965248872Smav softc->disk->d_devstat); 1966236814Smav if (error != 0) 1967214279Sbrucec xpt_print(periph->path, "Spin-down disk failed\n"); 1968249048Smav xpt_release_ccb(ccb); 1969214279Sbrucec cam_periph_unlock(periph); 1970214279Sbrucec } 1971195534Sscottl} 1972195534Sscottl 1973220650Smavstatic void 1974220650Smavadashutdown(void *arg, int howto) 1975220650Smav{ 1976220650Smav 1977220650Smav adaflush(); 1978220650Smav if (ada_spindown_shutdown != 0 && 1979220650Smav (howto & (RB_HALT | RB_POWEROFF)) != 0) 1980220650Smav adaspindown(ATA_STANDBY_IMMEDIATE, 0); 1981220650Smav} 1982220650Smav 1983220650Smavstatic void 1984220650Smavadasuspend(void *arg) 1985220650Smav{ 1986220650Smav 1987220650Smav adaflush(); 1988220650Smav if (ada_spindown_suspend != 0) 1989220650Smav adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE); 1990220650Smav} 1991220650Smav 1992220650Smavstatic void 1993220650Smavadaresume(void *arg) 1994220650Smav{ 1995220650Smav struct cam_periph *periph; 1996220650Smav struct ada_softc *softc; 1997220650Smav 1998220650Smav if (ada_spindown_suspend == 0) 1999220650Smav return; 2000220650Smav 2001248868Smav CAM_PERIPH_FOREACH(periph, &adadriver) { 2002220650Smav cam_periph_lock(periph); 2003220650Smav softc = (struct ada_softc *)periph->softc; 2004220650Smav /* 2005220650Smav * We only spin-down the drive if it is capable of it.. 2006220650Smav */ 2007220650Smav if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 2008220650Smav cam_periph_unlock(periph); 2009220650Smav continue; 2010220650Smav } 2011220650Smav 2012220650Smav if (bootverbose) 2013220650Smav xpt_print(periph->path, "resume\n"); 2014220650Smav 2015220650Smav /* 2016220650Smav * Drop freeze taken due to CAM_DEV_QFREEZE flag set on 2017220650Smav * sleep request. 2018220650Smav */ 2019220650Smav cam_release_devq(periph->path, 2020220650Smav /*relsim_flags*/0, 2021220650Smav /*openings*/0, 2022220650Smav /*timeout*/0, 2023220650Smav /*getcount_only*/0); 2024220650Smav 2025220650Smav cam_periph_unlock(periph); 2026220650Smav } 2027220650Smav} 2028220650Smav 2029195534Sscottl#endif /* _KERNEL */ 2030