ata_da.c revision 250792
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 250792 2013-05-18 23:36:21Z 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_CAN_48BIT = 0x0002, 78249199Smarius ADA_FLAG_CAN_FLUSHCACHE = 0x0004, 79249199Smarius ADA_FLAG_CAN_NCQ = 0x0008, 80249199Smarius ADA_FLAG_CAN_DMA = 0x0010, 81249199Smarius ADA_FLAG_NEED_OTAG = 0x0020, 82249199Smarius ADA_FLAG_WENT_IDLE = 0x0040, 83249199Smarius ADA_FLAG_CAN_TRIM = 0x0080, 84249199Smarius ADA_FLAG_OPEN = 0x0100, 85249199Smarius ADA_FLAG_SCTX_INIT = 0x0200, 86249199Smarius ADA_FLAG_CAN_CFA = 0x0400, 87249199Smarius ADA_FLAG_CAN_POWERMGT = 0x0800, 88249199Smarius ADA_FLAG_CAN_DMA48 = 0x1000 89195534Sscottl} ada_flags; 90195534Sscottl 91195534Sscottltypedef enum { 92222520Smav ADA_Q_NONE = 0x00, 93222520Smav ADA_Q_4K = 0x01, 94195534Sscottl} ada_quirks; 95195534Sscottl 96250792Ssmh#define ADA_Q_BIT_STRING \ 97250792Ssmh "\020" \ 98250792Ssmh "\0014K" 99250792Ssmh 100195534Sscottltypedef enum { 101224497Smav ADA_CCB_RAHEAD = 0x01, 102224497Smav ADA_CCB_WCACHE = 0x02, 103195534Sscottl ADA_CCB_BUFFER_IO = 0x03, 104195534Sscottl ADA_CCB_WAITING = 0x04, 105195534Sscottl ADA_CCB_DUMP = 0x05, 106201139Smav ADA_CCB_TRIM = 0x06, 107195534Sscottl ADA_CCB_TYPE_MASK = 0x0F, 108195534Sscottl} ada_ccb_state; 109195534Sscottl 110195534Sscottl/* Offsets into our private area for storing information */ 111195534Sscottl#define ccb_state ppriv_field0 112195534Sscottl#define ccb_bp ppriv_ptr1 113195534Sscottl 114195534Sscottlstruct disk_params { 115195534Sscottl u_int8_t heads; 116198897Smav u_int8_t secs_per_track; 117195534Sscottl u_int32_t cylinders; 118198897Smav u_int32_t secsize; /* Number of bytes/logical sector */ 119198897Smav u_int64_t sectors; /* Total number sectors */ 120195534Sscottl}; 121195534Sscottl 122222643Smav#define TRIM_MAX_BLOCKS 8 123249934Ssmh#define TRIM_MAX_RANGES (TRIM_MAX_BLOCKS * ATA_DSM_BLK_RANGES) 124222643Smav#define TRIM_MAX_BIOS (TRIM_MAX_RANGES * 4) 125201139Smavstruct trim_request { 126249934Ssmh uint8_t data[TRIM_MAX_RANGES * ATA_DSM_RANGE_SIZE]; 127222628Smav struct bio *bps[TRIM_MAX_BIOS]; 128201139Smav}; 129201139Smav 130195534Sscottlstruct ada_softc { 131195534Sscottl struct bio_queue_head bio_queue; 132201139Smav struct bio_queue_head trim_queue; 133195534Sscottl ada_state state; 134195534Sscottl ada_flags flags; 135195534Sscottl ada_quirks quirks; 136248922Ssmh int sort_io_queue; 137195534Sscottl int ordered_tag_count; 138195534Sscottl int outstanding_cmds; 139201139Smav int trim_max_ranges; 140201139Smav int trim_running; 141224497Smav int read_ahead; 142220454Smav int write_cache; 143220454Smav#ifdef ADA_TEST_FAILURE 144220454Smav int force_read_error; 145220454Smav int force_write_error; 146220454Smav int periodic_read_error; 147220454Smav int periodic_read_count; 148220454Smav#endif 149195534Sscottl struct disk_params params; 150195534Sscottl struct disk *disk; 151195534Sscottl struct task sysctl_task; 152195534Sscottl struct sysctl_ctx_list sysctl_ctx; 153195534Sscottl struct sysctl_oid *sysctl_tree; 154195534Sscottl struct callout sendordered_c; 155201139Smav struct trim_request trim_req; 156195534Sscottl}; 157195534Sscottl 158195534Sscottlstruct ada_quirk_entry { 159195534Sscottl struct scsi_inquiry_pattern inq_pat; 160195534Sscottl ada_quirks quirks; 161195534Sscottl}; 162195534Sscottl 163199178Smavstatic struct ada_quirk_entry ada_quirk_table[] = 164199178Smav{ 165199178Smav { 166222520Smav /* Hitachi Advanced Format (4k) drives */ 167222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" }, 168222520Smav /*quirks*/ADA_Q_4K 169222520Smav }, 170222520Smav { 171222520Smav /* Samsung Advanced Format (4k) drives */ 172228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD155UI*", "*" }, 173228819Smav /*quirks*/ADA_Q_4K 174228819Smav }, 175228819Smav { 176228819Smav /* Samsung Advanced Format (4k) drives */ 177222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" }, 178222520Smav /*quirks*/ADA_Q_4K 179222520Smav }, 180222520Smav { 181222520Smav /* Seagate Barracuda Green Advanced Format (4k) drives */ 182222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" }, 183222520Smav /*quirks*/ADA_Q_4K 184222520Smav }, 185222520Smav { 186228819Smav /* Seagate Barracuda Advanced Format (4k) drives */ 187228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???DM*", "*" }, 188228819Smav /*quirks*/ADA_Q_4K 189228819Smav }, 190228819Smav { 191228819Smav /* Seagate Barracuda Advanced Format (4k) drives */ 192228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DM*", "*" }, 193228819Smav /*quirks*/ADA_Q_4K 194228819Smav }, 195228819Smav { 196222520Smav /* Seagate Momentus Advanced Format (4k) drives */ 197222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" }, 198222520Smav /*quirks*/ADA_Q_4K 199222520Smav }, 200222520Smav { 201222520Smav /* Seagate Momentus Advanced Format (4k) drives */ 202222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" }, 203222520Smav /*quirks*/ADA_Q_4K 204222520Smav }, 205222520Smav { 206222520Smav /* Seagate Momentus Advanced Format (4k) drives */ 207228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640423AS*", "*" }, 208228819Smav /*quirks*/ADA_Q_4K 209228819Smav }, 210228819Smav { 211228819Smav /* Seagate Momentus Advanced Format (4k) drives */ 212228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640424AS*", "*" }, 213228819Smav /*quirks*/ADA_Q_4K 214228819Smav }, 215228819Smav { 216228819Smav /* Seagate Momentus Advanced Format (4k) drives */ 217222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" }, 218222520Smav /*quirks*/ADA_Q_4K 219222520Smav }, 220222520Smav { 221222520Smav /* Seagate Momentus Advanced Format (4k) drives */ 222222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" }, 223222520Smav /*quirks*/ADA_Q_4K 224222520Smav }, 225222520Smav { 226228819Smav /* Seagate Momentus Advanced Format (4k) drives */ 227228819Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750423AS*", "*" }, 228228819Smav /*quirks*/ADA_Q_4K 229228819Smav }, 230228819Smav { 231222520Smav /* Seagate Momentus Thin Advanced Format (4k) drives */ 232222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" }, 233222520Smav /*quirks*/ADA_Q_4K 234222520Smav }, 235222520Smav { 236222520Smav /* WDC Caviar Green Advanced Format (4k) drives */ 237222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" }, 238222520Smav /*quirks*/ADA_Q_4K 239222520Smav }, 240222520Smav { 241222520Smav /* WDC Caviar Green Advanced Format (4k) drives */ 242222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" }, 243222520Smav /*quirks*/ADA_Q_4K 244222520Smav }, 245222520Smav { 246222520Smav /* WDC Caviar Green Advanced Format (4k) drives */ 247222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" }, 248222520Smav /*quirks*/ADA_Q_4K 249222520Smav }, 250222520Smav { 251222520Smav /* WDC Caviar Green Advanced Format (4k) drives */ 252222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" }, 253222520Smav /*quirks*/ADA_Q_4K 254222520Smav }, 255222520Smav { 256222520Smav /* WDC Scorpio Black Advanced Format (4k) drives */ 257222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" }, 258222520Smav /*quirks*/ADA_Q_4K 259222520Smav }, 260222520Smav { 261222520Smav /* WDC Scorpio Black Advanced Format (4k) drives */ 262222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" }, 263222520Smav /*quirks*/ADA_Q_4K 264222520Smav }, 265222520Smav { 266222520Smav /* WDC Scorpio Blue Advanced Format (4k) drives */ 267222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" }, 268222520Smav /*quirks*/ADA_Q_4K 269222520Smav }, 270222520Smav { 271222520Smav /* WDC Scorpio Blue Advanced Format (4k) drives */ 272222520Smav { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" }, 273222520Smav /*quirks*/ADA_Q_4K 274222520Smav }, 275222520Smav { 276241784Seadler /* 277241784Seadler * Corsair Force 2 SSDs 278241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 279241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 280241784Seadler * PR: 169974 281241784Seadler */ 282241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair CSSD-F*", "*" }, 283241784Seadler /*quirks*/ADA_Q_4K 284241784Seadler }, 285241784Seadler { 286241784Seadler /* 287241784Seadler * Corsair Force 3 SSDs 288241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 289241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 290241784Seadler * PR: 169974 291241784Seadler */ 292241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force 3*", "*" }, 293241784Seadler /*quirks*/ADA_Q_4K 294241784Seadler }, 295241784Seadler { 296241784Seadler /* 297241784Seadler * OCZ Agility 3 SSDs 298241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 299241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 300241784Seadler * PR: 169974 301241784Seadler */ 302241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY3*", "*" }, 303241784Seadler /*quirks*/ADA_Q_4K 304241784Seadler }, 305241784Seadler { 306241784Seadler /* 307241784Seadler * OCZ Vertex 2 SSDs (inc pro series) 308241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 309241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 310241784Seadler * PR: 169974 311241784Seadler */ 312241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ?VERTEX2*", "*" }, 313241784Seadler /*quirks*/ADA_Q_4K 314241784Seadler }, 315241784Seadler { 316241784Seadler /* 317241784Seadler * OCZ Vertex 3 SSDs 318241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 319241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 320241784Seadler * PR: 169974 321241784Seadler */ 322241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX3*", "*" }, 323241784Seadler /*quirks*/ADA_Q_4K 324241784Seadler }, 325241784Seadler { 326241784Seadler /* 327241784Seadler * SuperTalent TeraDrive CT SSDs 328241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 329241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 330241784Seadler * PR: 169974 331241784Seadler */ 332241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" }, 333241784Seadler /*quirks*/ADA_Q_4K 334241784Seadler }, 335241784Seadler { 336241784Seadler /* 337241784Seadler * Crucial RealSSD C300 SSDs 338241784Seadler * 4k optimised 339241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 340241784Seadler * PR: 169974 341241784Seadler */ 342241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "C300-CTFDDAC???MAG*", 343241784Seadler "*" }, /*quirks*/ADA_Q_4K 344241784Seadler }, 345241784Seadler { 346241784Seadler /* 347241784Seadler * XceedIOPS SATA SSDs 348241784Seadler * 4k optimised 349241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 350241784Seadler * PR: 169974 351241784Seadler */ 352241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" }, 353241784Seadler /*quirks*/ADA_Q_4K 354241784Seadler }, 355241784Seadler { 356241784Seadler /* 357250532Seadler * Intel 320 Series SSDs 358250532Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 359250532Seadler */ 360250532Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2CW*", "*" }, 361250532Seadler /*quirks*/ADA_Q_4K 362250532Seadler }, 363250532Seadler { 364250532Seadler /* 365241784Seadler * Intel 330 Series SSDs 366241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 367241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 368241784Seadler * PR: 169974 369241784Seadler */ 370241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2ct*", "*" }, 371241784Seadler /*quirks*/ADA_Q_4K 372241784Seadler }, 373241784Seadler { 374241784Seadler /* 375250532Seadler * Intel 510 Series SSDs 376250532Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 377250532Seadler */ 378250532Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2MH*", "*" }, 379250532Seadler /*quirks*/ADA_Q_4K 380250532Seadler }, 381250532Seadler { 382250532Seadler /* 383241784Seadler * OCZ Deneva R Series SSDs 384241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 385241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 386241784Seadler * PR: 169974 387241784Seadler */ 388241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "DENRSTE251M45*", "*" }, 389241784Seadler /*quirks*/ADA_Q_4K 390241784Seadler }, 391241784Seadler { 392241784Seadler /* 393241784Seadler * Kingston HyperX 3k SSDs 394241784Seadler * 4k optimised & trim only works in 4k requests + 4k aligned 395241784Seadler * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk> 396241784Seadler * PR: 169974 397241784Seadler */ 398241784Seadler { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SH103S3*", "*" }, 399241784Seadler /*quirks*/ADA_Q_4K 400241784Seadler }, 401241784Seadler { 402199178Smav /* Default */ 403199178Smav { 404199178Smav T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, 405199178Smav /*vendor*/"*", /*product*/"*", /*revision*/"*" 406199178Smav }, 407199178Smav /*quirks*/0 408199178Smav }, 409199178Smav}; 410195534Sscottl 411195534Sscottlstatic disk_strategy_t adastrategy; 412195534Sscottlstatic dumper_t adadump; 413195534Sscottlstatic periph_init_t adainit; 414195534Sscottlstatic void adaasync(void *callback_arg, u_int32_t code, 415195534Sscottl struct cam_path *path, void *arg); 416195534Sscottlstatic void adasysctlinit(void *context, int pending); 417195534Sscottlstatic periph_ctor_t adaregister; 418195534Sscottlstatic periph_dtor_t adacleanup; 419195534Sscottlstatic periph_start_t adastart; 420195534Sscottlstatic periph_oninv_t adaoninvalidate; 421195534Sscottlstatic void adadone(struct cam_periph *periph, 422195534Sscottl union ccb *done_ccb); 423195534Sscottlstatic int adaerror(union ccb *ccb, u_int32_t cam_flags, 424195534Sscottl u_int32_t sense_flags); 425198897Smavstatic void adagetparams(struct cam_periph *periph, 426195534Sscottl struct ccb_getdev *cgd); 427195534Sscottlstatic timeout_t adasendorderedtag; 428195534Sscottlstatic void adashutdown(void *arg, int howto); 429220650Smavstatic void adasuspend(void *arg); 430220650Smavstatic void adaresume(void *arg); 431195534Sscottl 432221071Smav#ifndef ADA_DEFAULT_LEGACY_ALIASES 433221071Smav#define ADA_DEFAULT_LEGACY_ALIASES 1 434221071Smav#endif 435221071Smav 436195534Sscottl#ifndef ADA_DEFAULT_TIMEOUT 437195534Sscottl#define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */ 438195534Sscottl#endif 439195534Sscottl 440195534Sscottl#ifndef ADA_DEFAULT_RETRY 441195534Sscottl#define ADA_DEFAULT_RETRY 4 442195534Sscottl#endif 443195534Sscottl 444195534Sscottl#ifndef ADA_DEFAULT_SEND_ORDERED 445195534Sscottl#define ADA_DEFAULT_SEND_ORDERED 1 446195534Sscottl#endif 447195534Sscottl 448214279Sbrucec#ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN 449214279Sbrucec#define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1 450214279Sbrucec#endif 451214279Sbrucec 452220650Smav#ifndef ADA_DEFAULT_SPINDOWN_SUSPEND 453220650Smav#define ADA_DEFAULT_SPINDOWN_SUSPEND 1 454220650Smav#endif 455220650Smav 456224497Smav#ifndef ADA_DEFAULT_READ_AHEAD 457224497Smav#define ADA_DEFAULT_READ_AHEAD 1 458224497Smav#endif 459224497Smav 460220412Smav#ifndef ADA_DEFAULT_WRITE_CACHE 461220412Smav#define ADA_DEFAULT_WRITE_CACHE 1 462220412Smav#endif 463220412Smav 464224497Smav#define ADA_RA (softc->read_ahead >= 0 ? \ 465224497Smav softc->read_ahead : ada_read_ahead) 466224497Smav#define ADA_WC (softc->write_cache >= 0 ? \ 467224497Smav softc->write_cache : ada_write_cache) 468248922Ssmh#define ADA_SIO (softc->sort_io_queue >= 0 ? \ 469248922Ssmh softc->sort_io_queue : cam_sort_io_queues) 470224497Smav 471208349Smarius/* 472208349Smarius * Most platforms map firmware geometry to actual, but some don't. If 473208349Smarius * not overridden, default to nothing. 474208349Smarius */ 475208349Smarius#ifndef ata_disk_firmware_geom_adjust 476208349Smarius#define ata_disk_firmware_geom_adjust(disk) 477208349Smarius#endif 478195534Sscottl 479221071Smavstatic int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES; 480195534Sscottlstatic int ada_retry_count = ADA_DEFAULT_RETRY; 481195534Sscottlstatic int ada_default_timeout = ADA_DEFAULT_TIMEOUT; 482195534Sscottlstatic int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED; 483214279Sbrucecstatic int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN; 484220650Smavstatic int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND; 485224497Smavstatic int ada_read_ahead = ADA_DEFAULT_READ_AHEAD; 486220412Smavstatic int ada_write_cache = ADA_DEFAULT_WRITE_CACHE; 487195534Sscottl 488227309Sedstatic SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0, 489195534Sscottl "CAM Direct Access Disk driver"); 490221071SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RW, 491221071Smav &ada_legacy_aliases, 0, "Create legacy-like device aliases"); 492221071SmavTUNABLE_INT("kern.cam.ada.legacy_aliases", &ada_legacy_aliases); 493195534SscottlSYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW, 494195534Sscottl &ada_retry_count, 0, "Normal I/O retry count"); 495195534SscottlTUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count); 496195534SscottlSYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW, 497195534Sscottl &ada_default_timeout, 0, "Normal I/O timeout (in seconds)"); 498195534SscottlTUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout); 499238382SbruefferSYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RW, 500195534Sscottl &ada_send_ordered, 0, "Send Ordered Tags"); 501238382SbruefferTUNABLE_INT("kern.cam.ada.send_ordered", &ada_send_ordered); 502214279SbrucecSYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RW, 503214279Sbrucec &ada_spindown_shutdown, 0, "Spin down upon shutdown"); 504214279SbrucecTUNABLE_INT("kern.cam.ada.spindown_shutdown", &ada_spindown_shutdown); 505220650SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RW, 506220650Smav &ada_spindown_suspend, 0, "Spin down upon suspend"); 507220650SmavTUNABLE_INT("kern.cam.ada.spindown_suspend", &ada_spindown_suspend); 508224497SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RW, 509224497Smav &ada_read_ahead, 0, "Enable disk read-ahead"); 510224497SmavTUNABLE_INT("kern.cam.ada.read_ahead", &ada_read_ahead); 511220412SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RW, 512220412Smav &ada_write_cache, 0, "Enable disk write cache"); 513220412SmavTUNABLE_INT("kern.cam.ada.write_cache", &ada_write_cache); 514195534Sscottl 515195534Sscottl/* 516195534Sscottl * ADA_ORDEREDTAG_INTERVAL determines how often, relative 517195534Sscottl * to the default timeout, we check to see whether an ordered 518195534Sscottl * tagged transaction is appropriate to prevent simple tag 519195534Sscottl * starvation. Since we'd like to ensure that there is at least 520195534Sscottl * 1/2 of the timeout length left for a starved transaction to 521195534Sscottl * complete after we've sent an ordered tag, we must poll at least 522195534Sscottl * four times in every timeout period. This takes care of the worst 523195534Sscottl * case where a starved transaction starts during an interval that 524195534Sscottl * meets the requirement "don't send an ordered tag" test so it takes 525195534Sscottl * us two intervals to determine that a tag must be sent. 526195534Sscottl */ 527195534Sscottl#ifndef ADA_ORDEREDTAG_INTERVAL 528195534Sscottl#define ADA_ORDEREDTAG_INTERVAL 4 529195534Sscottl#endif 530195534Sscottl 531195534Sscottlstatic struct periph_driver adadriver = 532195534Sscottl{ 533195534Sscottl adainit, "ada", 534195534Sscottl TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0 535195534Sscottl}; 536195534Sscottl 537195534SscottlPERIPHDRIVER_DECLARE(ada, adadriver); 538195534Sscottl 539227293Sedstatic MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers"); 540195534Sscottl 541195534Sscottlstatic int 542195534Sscottladaopen(struct disk *dp) 543195534Sscottl{ 544195534Sscottl struct cam_periph *periph; 545195534Sscottl struct ada_softc *softc; 546195534Sscottl int error; 547195534Sscottl 548195534Sscottl periph = (struct cam_periph *)dp->d_drv1; 549195534Sscottl if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 550195534Sscottl return(ENXIO); 551195534Sscottl } 552195534Sscottl 553195534Sscottl cam_periph_lock(periph); 554195534Sscottl if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) { 555195534Sscottl cam_periph_unlock(periph); 556195534Sscottl cam_periph_release(periph); 557195534Sscottl return (error); 558195534Sscottl } 559195534Sscottl 560236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 561236602Smav ("adaopen\n")); 562195534Sscottl 563249981Smav softc = (struct ada_softc *)periph->softc; 564249981Smav softc->flags |= ADA_FLAG_OPEN; 565195534Sscottl 566195534Sscottl cam_periph_unhold(periph); 567195534Sscottl cam_periph_unlock(periph); 568195534Sscottl return (0); 569195534Sscottl} 570195534Sscottl 571195534Sscottlstatic int 572195534Sscottladaclose(struct disk *dp) 573195534Sscottl{ 574195534Sscottl struct cam_periph *periph; 575195534Sscottl struct ada_softc *softc; 576195534Sscottl union ccb *ccb; 577195534Sscottl 578195534Sscottl periph = (struct cam_periph *)dp->d_drv1; 579195534Sscottl cam_periph_lock(periph); 580234414Smav if (cam_periph_hold(periph, PRIBIO) != 0) { 581195534Sscottl cam_periph_unlock(periph); 582195534Sscottl cam_periph_release(periph); 583234414Smav return (0); 584195534Sscottl } 585195534Sscottl 586195534Sscottl softc = (struct ada_softc *)periph->softc; 587236602Smav 588236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 589236602Smav ("adaclose\n")); 590236602Smav 591195534Sscottl /* We only sync the cache if the drive is capable of it. */ 592224283Smav if ((softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 && 593249981Smav (periph->flags & CAM_PERIPH_INVALID) == 0) { 594195534Sscottl 595198382Smav ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 596195534Sscottl cam_fill_ataio(&ccb->ataio, 597195534Sscottl 1, 598195534Sscottl adadone, 599195534Sscottl CAM_DIR_NONE, 600195534Sscottl 0, 601195534Sscottl NULL, 602195534Sscottl 0, 603195534Sscottl ada_default_timeout*1000); 604195534Sscottl 605195534Sscottl if (softc->flags & ADA_FLAG_CAN_48BIT) 606195534Sscottl ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 607195534Sscottl else 608196659Smav ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 609236639Smav cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 610198328Smav /*sense_flags*/0, softc->disk->d_devstat); 611195534Sscottl 612195534Sscottl if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 613195534Sscottl xpt_print(periph->path, "Synchronize cache failed\n"); 614195534Sscottl xpt_release_ccb(ccb); 615195534Sscottl } 616195534Sscottl 617195534Sscottl softc->flags &= ~ADA_FLAG_OPEN; 618195534Sscottl cam_periph_unhold(periph); 619195534Sscottl cam_periph_unlock(periph); 620195534Sscottl cam_periph_release(periph); 621195534Sscottl return (0); 622195534Sscottl} 623195534Sscottl 624201139Smavstatic void 625201139Smavadaschedule(struct cam_periph *periph) 626201139Smav{ 627201139Smav struct ada_softc *softc = (struct ada_softc *)periph->softc; 628224531Smav uint32_t prio; 629201139Smav 630249205Smav if (softc->state != ADA_STATE_NORMAL) 631249205Smav return; 632249205Smav 633224531Smav /* Check if cam_periph_getccb() was called. */ 634224531Smav prio = periph->immediate_priority; 635224531Smav 636224531Smav /* Check if we have more work to do. */ 637201139Smav if (bioq_first(&softc->bio_queue) || 638201139Smav (!softc->trim_running && bioq_first(&softc->trim_queue))) { 639224531Smav prio = CAM_PRIORITY_NORMAL; 640201139Smav } 641224531Smav 642224531Smav /* Schedule CCB if any of above is true. */ 643224531Smav if (prio != CAM_PRIORITY_NONE) 644224531Smav xpt_schedule(periph, prio); 645201139Smav} 646201139Smav 647195534Sscottl/* 648195534Sscottl * Actually translate the requested transfer into one the physical driver 649195534Sscottl * can understand. The transfer is described by a buf and will include 650195534Sscottl * only one physical transfer. 651195534Sscottl */ 652195534Sscottlstatic void 653195534Sscottladastrategy(struct bio *bp) 654195534Sscottl{ 655195534Sscottl struct cam_periph *periph; 656195534Sscottl struct ada_softc *softc; 657195534Sscottl 658195534Sscottl periph = (struct cam_periph *)bp->bio_disk->d_drv1; 659195534Sscottl softc = (struct ada_softc *)periph->softc; 660195534Sscottl 661195534Sscottl cam_periph_lock(periph); 662195534Sscottl 663236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp)); 664236602Smav 665195534Sscottl /* 666195534Sscottl * If the device has been made invalid, error out 667195534Sscottl */ 668249981Smav if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 669195534Sscottl cam_periph_unlock(periph); 670195534Sscottl biofinish(bp, NULL, ENXIO); 671195534Sscottl return; 672195534Sscottl } 673195534Sscottl 674195534Sscottl /* 675195534Sscottl * Place it in the queue of disk activities for this disk 676195534Sscottl */ 677201139Smav if (bp->bio_cmd == BIO_DELETE && 678248922Ssmh (softc->flags & ADA_FLAG_CAN_TRIM)) { 679248922Ssmh if (ADA_SIO) 680248922Ssmh bioq_disksort(&softc->trim_queue, bp); 681248922Ssmh else 682248922Ssmh bioq_insert_tail(&softc->trim_queue, bp); 683248922Ssmh } else { 684248922Ssmh if (ADA_SIO) 685248922Ssmh bioq_disksort(&softc->bio_queue, bp); 686248922Ssmh else 687248922Ssmh bioq_insert_tail(&softc->bio_queue, bp); 688248922Ssmh } 689195534Sscottl 690195534Sscottl /* 691195534Sscottl * Schedule ourselves for performing the work. 692195534Sscottl */ 693201139Smav adaschedule(periph); 694195534Sscottl cam_periph_unlock(periph); 695195534Sscottl 696195534Sscottl return; 697195534Sscottl} 698195534Sscottl 699195534Sscottlstatic int 700195534Sscottladadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) 701195534Sscottl{ 702195534Sscottl struct cam_periph *periph; 703195534Sscottl struct ada_softc *softc; 704195534Sscottl u_int secsize; 705195534Sscottl union ccb ccb; 706195534Sscottl struct disk *dp; 707195534Sscottl uint64_t lba; 708195534Sscottl uint16_t count; 709236814Smav int error = 0; 710195534Sscottl 711195534Sscottl dp = arg; 712195534Sscottl periph = dp->d_drv1; 713195534Sscottl softc = (struct ada_softc *)periph->softc; 714195534Sscottl cam_periph_lock(periph); 715195534Sscottl secsize = softc->params.secsize; 716195534Sscottl lba = offset / secsize; 717195534Sscottl count = length / secsize; 718195534Sscottl 719249981Smav if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 720195534Sscottl cam_periph_unlock(periph); 721195534Sscottl return (ENXIO); 722195534Sscottl } 723195534Sscottl 724195534Sscottl if (length > 0) { 725198382Smav xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 726195534Sscottl ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 727195534Sscottl cam_fill_ataio(&ccb.ataio, 728195534Sscottl 0, 729195534Sscottl adadone, 730195534Sscottl CAM_DIR_OUT, 731195534Sscottl 0, 732195534Sscottl (u_int8_t *) virtual, 733195534Sscottl length, 734195534Sscottl ada_default_timeout*1000); 735195534Sscottl if ((softc->flags & ADA_FLAG_CAN_48BIT) && 736195534Sscottl (lba + count >= ATA_MAX_28BIT_LBA || 737195534Sscottl count >= 256)) { 738195534Sscottl ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48, 739195534Sscottl 0, lba, count); 740195534Sscottl } else { 741196659Smav ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA, 742195534Sscottl 0, lba, count); 743195534Sscottl } 744195534Sscottl xpt_polled_action(&ccb); 745195534Sscottl 746236814Smav error = cam_periph_error(&ccb, 747236814Smav 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 748236814Smav if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 749236814Smav cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0, 750236814Smav /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 751236814Smav if (error != 0) 752195534Sscottl printf("Aborting dump due to I/O error.\n"); 753236814Smav 754195534Sscottl cam_periph_unlock(periph); 755236814Smav return (error); 756195534Sscottl } 757195534Sscottl 758195534Sscottl if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) { 759198382Smav xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 760195534Sscottl 761195534Sscottl ccb.ccb_h.ccb_state = ADA_CCB_DUMP; 762195534Sscottl cam_fill_ataio(&ccb.ataio, 763236814Smav 0, 764195534Sscottl adadone, 765195534Sscottl CAM_DIR_NONE, 766195534Sscottl 0, 767195534Sscottl NULL, 768195534Sscottl 0, 769195534Sscottl ada_default_timeout*1000); 770195534Sscottl 771195534Sscottl if (softc->flags & ADA_FLAG_CAN_48BIT) 772195534Sscottl ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0); 773195534Sscottl else 774196659Smav ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0); 775195534Sscottl xpt_polled_action(&ccb); 776195534Sscottl 777236814Smav error = cam_periph_error(&ccb, 778236814Smav 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 779236814Smav if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) 780236814Smav cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0, 781236814Smav /*reduction*/0, /*timeout*/0, /*getcount_only*/0); 782236814Smav if (error != 0) 783195534Sscottl xpt_print(periph->path, "Synchronize cache failed\n"); 784195534Sscottl } 785195534Sscottl cam_periph_unlock(periph); 786236814Smav return (error); 787195534Sscottl} 788195534Sscottl 789195534Sscottlstatic void 790195534Sscottladainit(void) 791195534Sscottl{ 792195534Sscottl cam_status status; 793195534Sscottl 794195534Sscottl /* 795195534Sscottl * Install a global async callback. This callback will 796195534Sscottl * receive async callbacks like "new device found". 797195534Sscottl */ 798195534Sscottl status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL); 799195534Sscottl 800195534Sscottl if (status != CAM_REQ_CMP) { 801195534Sscottl printf("ada: Failed to attach master async callback " 802195534Sscottl "due to status 0x%x!\n", status); 803195534Sscottl } else if (ada_send_ordered) { 804195534Sscottl 805220650Smav /* Register our event handlers */ 806220650Smav if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend, 807220650Smav NULL, EVENTHANDLER_PRI_LAST)) == NULL) 808220650Smav printf("adainit: power event registration failed!\n"); 809220650Smav if ((EVENTHANDLER_REGISTER(power_resume, adaresume, 810220650Smav NULL, EVENTHANDLER_PRI_LAST)) == NULL) 811220650Smav printf("adainit: power event registration failed!\n"); 812220650Smav if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown, 813195534Sscottl NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 814195534Sscottl printf("adainit: shutdown event registration failed!\n"); 815195534Sscottl } 816195534Sscottl} 817195534Sscottl 818249347Sken/* 819249347Sken * Callback from GEOM, called when it has finished cleaning up its 820249347Sken * resources. 821249347Sken */ 822195534Sscottlstatic void 823249347Skenadadiskgonecb(struct disk *dp) 824249347Sken{ 825249347Sken struct cam_periph *periph; 826249347Sken 827249347Sken periph = (struct cam_periph *)dp->d_drv1; 828249347Sken 829249347Sken cam_periph_release(periph); 830249347Sken} 831249347Sken 832249347Skenstatic void 833195534Sscottladaoninvalidate(struct cam_periph *periph) 834195534Sscottl{ 835195534Sscottl struct ada_softc *softc; 836195534Sscottl 837195534Sscottl softc = (struct ada_softc *)periph->softc; 838195534Sscottl 839195534Sscottl /* 840195534Sscottl * De-register any async callbacks. 841195534Sscottl */ 842195534Sscottl xpt_register_async(0, adaasync, periph, periph->path); 843195534Sscottl 844195534Sscottl /* 845195534Sscottl * Return all queued I/O with ENXIO. 846195534Sscottl * XXX Handle any transactions queued to the card 847195534Sscottl * with XPT_ABORT_CCB. 848195534Sscottl */ 849195534Sscottl bioq_flush(&softc->bio_queue, NULL, ENXIO); 850201139Smav bioq_flush(&softc->trim_queue, NULL, ENXIO); 851195534Sscottl 852195534Sscottl disk_gone(softc->disk); 853195534Sscottl xpt_print(periph->path, "lost device\n"); 854195534Sscottl} 855195534Sscottl 856195534Sscottlstatic void 857195534Sscottladacleanup(struct cam_periph *periph) 858195534Sscottl{ 859195534Sscottl struct ada_softc *softc; 860195534Sscottl 861195534Sscottl softc = (struct ada_softc *)periph->softc; 862195534Sscottl 863195534Sscottl xpt_print(periph->path, "removing device entry\n"); 864195534Sscottl cam_periph_unlock(periph); 865195534Sscottl 866195534Sscottl /* 867195534Sscottl * If we can't free the sysctl tree, oh well... 868195534Sscottl */ 869195534Sscottl if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0 870195534Sscottl && sysctl_ctx_free(&softc->sysctl_ctx) != 0) { 871195534Sscottl xpt_print(periph->path, "can't remove sysctl context\n"); 872195534Sscottl } 873195534Sscottl 874195534Sscottl disk_destroy(softc->disk); 875195534Sscottl callout_drain(&softc->sendordered_c); 876195534Sscottl free(softc, M_DEVBUF); 877195534Sscottl cam_periph_lock(periph); 878195534Sscottl} 879195534Sscottl 880195534Sscottlstatic void 881195534Sscottladaasync(void *callback_arg, u_int32_t code, 882195534Sscottl struct cam_path *path, void *arg) 883195534Sscottl{ 884236393Smav struct ccb_getdev cgd; 885195534Sscottl struct cam_periph *periph; 886220412Smav struct ada_softc *softc; 887195534Sscottl 888195534Sscottl periph = (struct cam_periph *)callback_arg; 889195534Sscottl switch (code) { 890195534Sscottl case AC_FOUND_DEVICE: 891195534Sscottl { 892195534Sscottl struct ccb_getdev *cgd; 893195534Sscottl cam_status status; 894195534Sscottl 895195534Sscottl cgd = (struct ccb_getdev *)arg; 896195534Sscottl if (cgd == NULL) 897195534Sscottl break; 898195534Sscottl 899195534Sscottl if (cgd->protocol != PROTO_ATA) 900195534Sscottl break; 901195534Sscottl 902195534Sscottl /* 903195534Sscottl * Allocate a peripheral instance for 904195534Sscottl * this device and start the probe 905195534Sscottl * process. 906195534Sscottl */ 907195534Sscottl status = cam_periph_alloc(adaregister, adaoninvalidate, 908195534Sscottl adacleanup, adastart, 909195534Sscottl "ada", CAM_PERIPH_BIO, 910195534Sscottl cgd->ccb_h.path, adaasync, 911195534Sscottl AC_FOUND_DEVICE, cgd); 912195534Sscottl 913195534Sscottl if (status != CAM_REQ_CMP 914195534Sscottl && status != CAM_REQ_INPROG) 915195534Sscottl printf("adaasync: Unable to attach to new device " 916195534Sscottl "due to status 0x%x\n", status); 917195534Sscottl break; 918195534Sscottl } 919236393Smav case AC_GETDEV_CHANGED: 920236393Smav { 921236393Smav softc = (struct ada_softc *)periph->softc; 922236393Smav xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 923236393Smav cgd.ccb_h.func_code = XPT_GDEV_TYPE; 924236393Smav xpt_action((union ccb *)&cgd); 925236393Smav 926236393Smav if ((cgd.ident_data.capabilities1 & ATA_SUPPORT_DMA) && 927236393Smav (cgd.inq_flags & SID_DMA)) 928236393Smav softc->flags |= ADA_FLAG_CAN_DMA; 929236393Smav else 930236393Smav softc->flags &= ~ADA_FLAG_CAN_DMA; 931249199Smarius if (cgd.ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) { 932249199Smarius softc->flags |= ADA_FLAG_CAN_48BIT; 933249199Smarius if (cgd.inq_flags & SID_DMA48) 934249199Smarius softc->flags |= ADA_FLAG_CAN_DMA48; 935249199Smarius else 936249199Smarius softc->flags &= ~ADA_FLAG_CAN_DMA48; 937249199Smarius } else 938249199Smarius softc->flags &= ~(ADA_FLAG_CAN_48BIT | 939249199Smarius ADA_FLAG_CAN_DMA48); 940236393Smav if ((cgd.ident_data.satacapabilities & ATA_SUPPORT_NCQ) && 941236393Smav (cgd.inq_flags & SID_DMA) && (cgd.inq_flags & SID_CmdQue)) 942236393Smav softc->flags |= ADA_FLAG_CAN_NCQ; 943236393Smav else 944236393Smav softc->flags &= ~ADA_FLAG_CAN_NCQ; 945236393Smav if ((cgd.ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) && 946236393Smav (cgd.inq_flags & SID_DMA)) 947236393Smav softc->flags |= ADA_FLAG_CAN_TRIM; 948236393Smav else 949236393Smav softc->flags &= ~ADA_FLAG_CAN_TRIM; 950236393Smav 951236393Smav cam_periph_async(periph, code, path, arg); 952236393Smav break; 953236393Smav } 954235897Smav case AC_ADVINFO_CHANGED: 955235897Smav { 956235897Smav uintptr_t buftype; 957235897Smav 958235897Smav buftype = (uintptr_t)arg; 959235897Smav if (buftype == CDAI_TYPE_PHYS_PATH) { 960235897Smav struct ada_softc *softc; 961235897Smav 962235897Smav softc = periph->softc; 963235897Smav disk_attr_changed(softc->disk, "GEOM::physpath", 964235897Smav M_NOWAIT); 965235897Smav } 966235897Smav break; 967235897Smav } 968220412Smav case AC_SENT_BDR: 969220412Smav case AC_BUS_RESET: 970220412Smav { 971220412Smav softc = (struct ada_softc *)periph->softc; 972220412Smav cam_periph_async(periph, code, path, arg); 973220412Smav if (softc->state != ADA_STATE_NORMAL) 974220412Smav break; 975220454Smav xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 976220412Smav cgd.ccb_h.func_code = XPT_GDEV_TYPE; 977220412Smav xpt_action((union ccb *)&cgd); 978224497Smav if (ADA_RA >= 0 && 979224497Smav cgd.ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) 980224497Smav softc->state = ADA_STATE_RAHEAD; 981224497Smav else if (ADA_WC >= 0 && 982224497Smav cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) 983224497Smav softc->state = ADA_STATE_WCACHE; 984224497Smav else 985224497Smav break; 986220412Smav cam_periph_acquire(periph); 987220412Smav xpt_schedule(periph, CAM_PRIORITY_DEV); 988220412Smav } 989195534Sscottl default: 990195534Sscottl cam_periph_async(periph, code, path, arg); 991195534Sscottl break; 992195534Sscottl } 993195534Sscottl} 994195534Sscottl 995195534Sscottlstatic void 996195534Sscottladasysctlinit(void *context, int pending) 997195534Sscottl{ 998195534Sscottl struct cam_periph *periph; 999195534Sscottl struct ada_softc *softc; 1000195534Sscottl char tmpstr[80], tmpstr2[80]; 1001195534Sscottl 1002195534Sscottl periph = (struct cam_periph *)context; 1003220454Smav 1004220454Smav /* periph was held for us when this task was enqueued */ 1005249981Smav if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 1006220454Smav cam_periph_release(periph); 1007195534Sscottl return; 1008220454Smav } 1009195534Sscottl 1010195534Sscottl softc = (struct ada_softc *)periph->softc; 1011195534Sscottl snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number); 1012195534Sscottl snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 1013195534Sscottl 1014195534Sscottl sysctl_ctx_init(&softc->sysctl_ctx); 1015195534Sscottl softc->flags |= ADA_FLAG_SCTX_INIT; 1016195534Sscottl softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1017195534Sscottl SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2, 1018195534Sscottl CTLFLAG_RD, 0, tmpstr); 1019195534Sscottl if (softc->sysctl_tree == NULL) { 1020195534Sscottl printf("adasysctlinit: unable to allocate sysctl tree\n"); 1021195534Sscottl cam_periph_release(periph); 1022195534Sscottl return; 1023195534Sscottl } 1024195534Sscottl 1025220454Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1026224497Smav OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE, 1027224497Smav &softc->read_ahead, 0, "Enable disk read ahead."); 1028224497Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1029220454Smav OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE, 1030220454Smav &softc->write_cache, 0, "Enable disk write cache."); 1031248922Ssmh SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1032248922Ssmh OID_AUTO, "sort_io_queue", CTLFLAG_RW | CTLFLAG_MPSAFE, 1033248922Ssmh &softc->sort_io_queue, 0, 1034248922Ssmh "Sort IO queue to try and optimise disk access patterns"); 1035220454Smav#ifdef ADA_TEST_FAILURE 1036220454Smav /* 1037220454Smav * Add a 'door bell' sysctl which allows one to set it from userland 1038220454Smav * and cause something bad to happen. For the moment, we only allow 1039220454Smav * whacking the next read or write. 1040220454Smav */ 1041220454Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1042220454Smav OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1043220454Smav &softc->force_read_error, 0, 1044220454Smav "Force a read error for the next N reads."); 1045220454Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1046220454Smav OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1047220454Smav &softc->force_write_error, 0, 1048220454Smav "Force a write error for the next N writes."); 1049220454Smav SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1050220454Smav OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE, 1051220454Smav &softc->periodic_read_error, 0, 1052220454Smav "Force a read error every N reads (don't set too low)."); 1053220454Smav#endif 1054195534Sscottl cam_periph_release(periph); 1055195534Sscottl} 1056195534Sscottl 1057223089Sgibbsstatic int 1058223089Sgibbsadagetattr(struct bio *bp) 1059223089Sgibbs{ 1060241485Smav int ret; 1061223089Sgibbs struct cam_periph *periph; 1062223089Sgibbs 1063223089Sgibbs periph = (struct cam_periph *)bp->bio_disk->d_drv1; 1064241485Smav cam_periph_lock(periph); 1065223089Sgibbs ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute, 1066223089Sgibbs periph->path); 1067241485Smav cam_periph_unlock(periph); 1068223089Sgibbs if (ret == 0) 1069223089Sgibbs bp->bio_completed = bp->bio_length; 1070223089Sgibbs return ret; 1071223089Sgibbs} 1072223089Sgibbs 1073195534Sscottlstatic cam_status 1074195534Sscottladaregister(struct cam_periph *periph, void *arg) 1075195534Sscottl{ 1076195534Sscottl struct ada_softc *softc; 1077195534Sscottl struct ccb_pathinq cpi; 1078195534Sscottl struct ccb_getdev *cgd; 1079221071Smav char announce_buf[80], buf1[32]; 1080195534Sscottl struct disk_params *dp; 1081195534Sscottl caddr_t match; 1082195534Sscottl u_int maxio; 1083222520Smav int legacy_id, quirks; 1084195534Sscottl 1085195534Sscottl cgd = (struct ccb_getdev *)arg; 1086195534Sscottl if (cgd == NULL) { 1087195534Sscottl printf("adaregister: no getdev CCB, can't register device\n"); 1088195534Sscottl return(CAM_REQ_CMP_ERR); 1089195534Sscottl } 1090195534Sscottl 1091195534Sscottl softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF, 1092195534Sscottl M_NOWAIT|M_ZERO); 1093195534Sscottl 1094195534Sscottl if (softc == NULL) { 1095195534Sscottl printf("adaregister: Unable to probe new device. " 1096198328Smav "Unable to allocate softc\n"); 1097195534Sscottl return(CAM_REQ_CMP_ERR); 1098195534Sscottl } 1099195534Sscottl 1100195534Sscottl bioq_init(&softc->bio_queue); 1101201139Smav bioq_init(&softc->trim_queue); 1102195534Sscottl 1103236393Smav if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) && 1104220886Smav (cgd->inq_flags & SID_DMA)) 1105198328Smav softc->flags |= ADA_FLAG_CAN_DMA; 1106249199Smarius if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) { 1107195534Sscottl softc->flags |= ADA_FLAG_CAN_48BIT; 1108249199Smarius if (cgd->inq_flags & SID_DMA48) 1109249199Smarius softc->flags |= ADA_FLAG_CAN_DMA48; 1110249199Smarius } 1111195534Sscottl if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE) 1112195534Sscottl softc->flags |= ADA_FLAG_CAN_FLUSHCACHE; 1113214279Sbrucec if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT) 1114214279Sbrucec softc->flags |= ADA_FLAG_CAN_POWERMGT; 1115236393Smav if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) && 1116220886Smav (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue)) 1117195534Sscottl softc->flags |= ADA_FLAG_CAN_NCQ; 1118236393Smav if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) && 1119236393Smav (cgd->inq_flags & SID_DMA)) { 1120201139Smav softc->flags |= ADA_FLAG_CAN_TRIM; 1121201139Smav softc->trim_max_ranges = TRIM_MAX_RANGES; 1122201139Smav if (cgd->ident_data.max_dsm_blocks != 0) { 1123201139Smav softc->trim_max_ranges = 1124249934Ssmh min(cgd->ident_data.max_dsm_blocks * 1125249934Ssmh ATA_DSM_BLK_RANGES, softc->trim_max_ranges); 1126201139Smav } 1127201139Smav } 1128201139Smav if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA) 1129201139Smav softc->flags |= ADA_FLAG_CAN_CFA; 1130195534Sscottl 1131195534Sscottl periph->softc = softc; 1132195534Sscottl 1133195534Sscottl /* 1134195534Sscottl * See if this device has any quirks. 1135195534Sscottl */ 1136199178Smav match = cam_quirkmatch((caddr_t)&cgd->ident_data, 1137199178Smav (caddr_t)ada_quirk_table, 1138199178Smav sizeof(ada_quirk_table)/sizeof(*ada_quirk_table), 1139199178Smav sizeof(*ada_quirk_table), ata_identify_match); 1140195534Sscottl if (match != NULL) 1141195534Sscottl softc->quirks = ((struct ada_quirk_entry *)match)->quirks; 1142195534Sscottl else 1143195534Sscottl softc->quirks = ADA_Q_NONE; 1144195534Sscottl 1145195534Sscottl bzero(&cpi, sizeof(cpi)); 1146203108Smav xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE); 1147195534Sscottl cpi.ccb_h.func_code = XPT_PATH_INQ; 1148195534Sscottl xpt_action((union ccb *)&cpi); 1149195534Sscottl 1150195534Sscottl TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph); 1151195534Sscottl 1152195534Sscottl /* 1153195534Sscottl * Register this media as a disk 1154195534Sscottl */ 1155220618Smav (void)cam_periph_hold(periph, PRIBIO); 1156249106Smav cam_periph_unlock(periph); 1157222520Smav snprintf(announce_buf, sizeof(announce_buf), 1158222520Smav "kern.cam.ada.%d.quirks", periph->unit_number); 1159222520Smav quirks = softc->quirks; 1160222520Smav TUNABLE_INT_FETCH(announce_buf, &quirks); 1161222520Smav softc->quirks = quirks; 1162224497Smav softc->read_ahead = -1; 1163224497Smav snprintf(announce_buf, sizeof(announce_buf), 1164224497Smav "kern.cam.ada.%d.read_ahead", periph->unit_number); 1165224497Smav TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead); 1166220618Smav softc->write_cache = -1; 1167220618Smav snprintf(announce_buf, sizeof(announce_buf), 1168220618Smav "kern.cam.ada.%d.write_cache", periph->unit_number); 1169220618Smav TUNABLE_INT_FETCH(announce_buf, &softc->write_cache); 1170250033Ssmh /* Disable queue sorting for non-rotational media by default. */ 1171249941Ssmh if (cgd->ident_data.media_rotation_rate == 1) 1172249941Ssmh softc->sort_io_queue = 0; 1173249941Ssmh else 1174249941Ssmh softc->sort_io_queue = -1; 1175198897Smav adagetparams(periph, cgd); 1176195534Sscottl softc->disk = disk_alloc(); 1177220644Smav softc->disk->d_devstat = devstat_new_entry(periph->periph_name, 1178220644Smav periph->unit_number, softc->params.secsize, 1179220644Smav DEVSTAT_ALL_SUPPORTED, 1180220644Smav DEVSTAT_TYPE_DIRECT | 1181220644Smav XPORT_DEVSTAT_TYPE(cpi.transport), 1182220644Smav DEVSTAT_PRIORITY_DISK); 1183195534Sscottl softc->disk->d_open = adaopen; 1184195534Sscottl softc->disk->d_close = adaclose; 1185195534Sscottl softc->disk->d_strategy = adastrategy; 1186223089Sgibbs softc->disk->d_getattr = adagetattr; 1187195534Sscottl softc->disk->d_dump = adadump; 1188249347Sken softc->disk->d_gone = adadiskgonecb; 1189195534Sscottl softc->disk->d_name = "ada"; 1190195534Sscottl softc->disk->d_drv1 = periph; 1191195534Sscottl maxio = cpi.maxio; /* Honor max I/O size of SIM */ 1192195534Sscottl if (maxio == 0) 1193195534Sscottl maxio = DFLTPHYS; /* traditional default */ 1194195534Sscottl else if (maxio > MAXPHYS) 1195195534Sscottl maxio = MAXPHYS; /* for safety */ 1196201139Smav if (softc->flags & ADA_FLAG_CAN_48BIT) 1197198897Smav maxio = min(maxio, 65536 * softc->params.secsize); 1198195534Sscottl else /* 28bit ATA command limit */ 1199198897Smav maxio = min(maxio, 256 * softc->params.secsize); 1200195534Sscottl softc->disk->d_maxsize = maxio; 1201195534Sscottl softc->disk->d_unit = periph->unit_number; 1202195534Sscottl softc->disk->d_flags = 0; 1203195534Sscottl if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) 1204195534Sscottl softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE; 1205249934Ssmh if (softc->flags & ADA_FLAG_CAN_TRIM) { 1206201139Smav softc->disk->d_flags |= DISKFLAG_CANDELETE; 1207249940Ssmh softc->disk->d_delmaxsize = softc->params.secsize * 1208249940Ssmh ATA_DSM_RANGE_MAX * 1209249940Ssmh softc->trim_max_ranges; 1210249934Ssmh } else if ((softc->flags & ADA_FLAG_CAN_CFA) && 1211249934Ssmh !(softc->flags & ADA_FLAG_CAN_48BIT)) { 1212249934Ssmh softc->disk->d_flags |= DISKFLAG_CANDELETE; 1213249940Ssmh softc->disk->d_delmaxsize = 256 * softc->params.secsize; 1214249940Ssmh } else 1215249940Ssmh softc->disk->d_delmaxsize = maxio; 1216248519Skib if ((cpi.hba_misc & PIM_UNMAPPED) != 0) 1217248519Skib softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO; 1218219056Snwhitehorn strlcpy(softc->disk->d_descr, cgd->ident_data.model, 1219219056Snwhitehorn MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model))); 1220241305Savg strlcpy(softc->disk->d_ident, cgd->ident_data.serial, 1221241305Savg MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial))); 1222210471Smav softc->disk->d_hba_vendor = cpi.hba_vendor; 1223210471Smav softc->disk->d_hba_device = cpi.hba_device; 1224210471Smav softc->disk->d_hba_subvendor = cpi.hba_subvendor; 1225210471Smav softc->disk->d_hba_subdevice = cpi.hba_subdevice; 1226195534Sscottl 1227195534Sscottl softc->disk->d_sectorsize = softc->params.secsize; 1228198897Smav softc->disk->d_mediasize = (off_t)softc->params.sectors * 1229198897Smav softc->params.secsize; 1230200969Smav if (ata_physical_sector_size(&cgd->ident_data) != 1231200969Smav softc->params.secsize) { 1232200969Smav softc->disk->d_stripesize = 1233200969Smav ata_physical_sector_size(&cgd->ident_data); 1234200969Smav softc->disk->d_stripeoffset = (softc->disk->d_stripesize - 1235200969Smav ata_logical_sector_offset(&cgd->ident_data)) % 1236200969Smav softc->disk->d_stripesize; 1237222520Smav } else if (softc->quirks & ADA_Q_4K) { 1238222520Smav softc->disk->d_stripesize = 4096; 1239222520Smav softc->disk->d_stripeoffset = 0; 1240200969Smav } 1241195534Sscottl softc->disk->d_fwsectors = softc->params.secs_per_track; 1242195534Sscottl softc->disk->d_fwheads = softc->params.heads; 1243208349Smarius ata_disk_firmware_geom_adjust(softc->disk); 1244195534Sscottl 1245221071Smav if (ada_legacy_aliases) { 1246221071Smav#ifdef ATA_STATIC_ID 1247221071Smav legacy_id = xpt_path_legacy_ata_id(periph->path); 1248221071Smav#else 1249221071Smav legacy_id = softc->disk->d_unit; 1250221071Smav#endif 1251221071Smav if (legacy_id >= 0) { 1252221071Smav snprintf(announce_buf, sizeof(announce_buf), 1253221071Smav "kern.devalias.%s%d", 1254221071Smav softc->disk->d_name, softc->disk->d_unit); 1255221071Smav snprintf(buf1, sizeof(buf1), 1256221071Smav "ad%d", legacy_id); 1257221071Smav setenv(announce_buf, buf1); 1258221071Smav } 1259221071Smav } else 1260221071Smav legacy_id = -1; 1261249347Sken /* 1262249347Sken * Acquire a reference to the periph before we register with GEOM. 1263249347Sken * We'll release this reference once GEOM calls us back (via 1264249347Sken * adadiskgonecb()) telling us that our provider has been freed. 1265249347Sken */ 1266249347Sken if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 1267249347Sken xpt_print(periph->path, "%s: lost periph during " 1268249347Sken "registration!\n", __func__); 1269249347Sken cam_periph_lock(periph); 1270249347Sken return (CAM_REQ_CMP_ERR); 1271249347Sken } 1272195534Sscottl disk_create(softc->disk, DISK_VERSION); 1273249106Smav cam_periph_lock(periph); 1274220618Smav cam_periph_unhold(periph); 1275195534Sscottl 1276195534Sscottl dp = &softc->params; 1277195534Sscottl snprintf(announce_buf, sizeof(announce_buf), 1278195534Sscottl "%juMB (%ju %u byte sectors: %dH %dS/T %dC)", 1279195534Sscottl (uintmax_t)(((uintmax_t)dp->secsize * 1280195534Sscottl dp->sectors) / (1024*1024)), 1281195534Sscottl (uintmax_t)dp->sectors, 1282195534Sscottl dp->secsize, dp->heads, 1283195534Sscottl dp->secs_per_track, dp->cylinders); 1284195534Sscottl xpt_announce_periph(periph, announce_buf); 1285250792Ssmh xpt_announce_quirks(periph, softc->quirks, ADA_Q_BIT_STRING); 1286221071Smav if (legacy_id >= 0) 1287221071Smav printf("%s%d: Previously was known as ad%d\n", 1288221071Smav periph->periph_name, periph->unit_number, legacy_id); 1289220454Smav 1290195534Sscottl /* 1291220454Smav * Create our sysctl variables, now that we know 1292220454Smav * we have successfully attached. 1293220454Smav */ 1294220454Smav cam_periph_acquire(periph); 1295220454Smav taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task); 1296220454Smav 1297220454Smav /* 1298195534Sscottl * Add async callbacks for bus reset and 1299195534Sscottl * bus device reset calls. I don't bother 1300195534Sscottl * checking if this fails as, in most cases, 1301195534Sscottl * the system will function just fine without 1302195534Sscottl * them and the only alternative would be to 1303195534Sscottl * not attach the device on failure. 1304195534Sscottl */ 1305235897Smav xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE | 1306236393Smav AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED, 1307236393Smav adaasync, periph, periph->path); 1308195534Sscottl 1309195534Sscottl /* 1310195534Sscottl * Schedule a periodic event to occasionally send an 1311195534Sscottl * ordered tag to a device. 1312195534Sscottl */ 1313195534Sscottl callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0); 1314195534Sscottl callout_reset(&softc->sendordered_c, 1315230921Smav (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 1316195534Sscottl adasendorderedtag, softc); 1317195534Sscottl 1318224497Smav if (ADA_RA >= 0 && 1319224497Smav cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) { 1320224497Smav softc->state = ADA_STATE_RAHEAD; 1321224497Smav cam_periph_acquire(periph); 1322224497Smav xpt_schedule(periph, CAM_PRIORITY_DEV); 1323224497Smav } else if (ADA_WC >= 0 && 1324220412Smav cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) { 1325220412Smav softc->state = ADA_STATE_WCACHE; 1326220412Smav cam_periph_acquire(periph); 1327220412Smav xpt_schedule(periph, CAM_PRIORITY_DEV); 1328220412Smav } else 1329220412Smav softc->state = ADA_STATE_NORMAL; 1330220412Smav 1331195534Sscottl return(CAM_REQ_CMP); 1332195534Sscottl} 1333195534Sscottl 1334195534Sscottlstatic void 1335195534Sscottladastart(struct cam_periph *periph, union ccb *start_ccb) 1336195534Sscottl{ 1337198328Smav struct ada_softc *softc = (struct ada_softc *)periph->softc; 1338198328Smav struct ccb_ataio *ataio = &start_ccb->ataio; 1339195534Sscottl 1340236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n")); 1341236602Smav 1342195534Sscottl switch (softc->state) { 1343195534Sscottl case ADA_STATE_NORMAL: 1344195534Sscottl { 1345195534Sscottl struct bio *bp; 1346201139Smav u_int8_t tag_code; 1347195534Sscottl 1348201139Smav /* Execute immediate CCB if waiting. */ 1349195534Sscottl if (periph->immediate_priority <= periph->pinfo.priority) { 1350236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1351195534Sscottl ("queuing for immediate ccb\n")); 1352195534Sscottl start_ccb->ccb_h.ccb_state = ADA_CCB_WAITING; 1353195534Sscottl SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 1354195534Sscottl periph_links.sle); 1355195534Sscottl periph->immediate_priority = CAM_PRIORITY_NONE; 1356195534Sscottl wakeup(&periph->ccb_list); 1357201139Smav /* Have more work to do, so ensure we stay scheduled */ 1358201139Smav adaschedule(periph); 1359201139Smav break; 1360201139Smav } 1361201139Smav /* Run TRIM if not running yet. */ 1362201139Smav if (!softc->trim_running && 1363201139Smav (bp = bioq_first(&softc->trim_queue)) != 0) { 1364201139Smav struct trim_request *req = &softc->trim_req; 1365201139Smav struct bio *bp1; 1366222628Smav uint64_t lastlba = (uint64_t)-1; 1367222628Smav int bps = 0, c, lastcount = 0, off, ranges = 0; 1368201139Smav 1369201139Smav softc->trim_running = 1; 1370201139Smav bzero(req, sizeof(*req)); 1371201139Smav bp1 = bp; 1372201139Smav do { 1373201139Smav uint64_t lba = bp1->bio_pblkno; 1374201139Smav int count = bp1->bio_bcount / 1375201139Smav softc->params.secsize; 1376201139Smav 1377201139Smav bioq_remove(&softc->trim_queue, bp1); 1378222628Smav 1379222628Smav /* Try to extend the previous range. */ 1380222628Smav if (lba == lastlba) { 1381249934Ssmh c = min(count, ATA_DSM_RANGE_MAX - lastcount); 1382222628Smav lastcount += c; 1383249934Ssmh off = (ranges - 1) * ATA_DSM_RANGE_SIZE; 1384222628Smav req->data[off + 6] = lastcount & 0xff; 1385222628Smav req->data[off + 7] = 1386222628Smav (lastcount >> 8) & 0xff; 1387222628Smav count -= c; 1388222628Smav lba += c; 1389222628Smav } 1390222628Smav 1391201139Smav while (count > 0) { 1392249934Ssmh c = min(count, ATA_DSM_RANGE_MAX); 1393249934Ssmh off = ranges * ATA_DSM_RANGE_SIZE; 1394201139Smav req->data[off + 0] = lba & 0xff; 1395201139Smav req->data[off + 1] = (lba >> 8) & 0xff; 1396201139Smav req->data[off + 2] = (lba >> 16) & 0xff; 1397201139Smav req->data[off + 3] = (lba >> 24) & 0xff; 1398201139Smav req->data[off + 4] = (lba >> 32) & 0xff; 1399201139Smav req->data[off + 5] = (lba >> 40) & 0xff; 1400201139Smav req->data[off + 6] = c & 0xff; 1401201139Smav req->data[off + 7] = (c >> 8) & 0xff; 1402201139Smav lba += c; 1403201139Smav count -= c; 1404222628Smav lastcount = c; 1405201139Smav ranges++; 1406249934Ssmh /* 1407249934Ssmh * Its the caller's responsibility to ensure the 1408249934Ssmh * request will fit so we don't need to check for 1409249934Ssmh * overrun here 1410249934Ssmh */ 1411201139Smav } 1412222628Smav lastlba = lba; 1413201139Smav req->bps[bps++] = bp1; 1414201139Smav bp1 = bioq_first(&softc->trim_queue); 1415222628Smav if (bps >= TRIM_MAX_BIOS || 1416222628Smav bp1 == NULL || 1417201139Smav bp1->bio_bcount / softc->params.secsize > 1418249934Ssmh (softc->trim_max_ranges - ranges) * 1419249934Ssmh ATA_DSM_RANGE_MAX) 1420201139Smav break; 1421201139Smav } while (1); 1422201139Smav cam_fill_ataio(ataio, 1423201139Smav ada_retry_count, 1424201139Smav adadone, 1425201139Smav CAM_DIR_OUT, 1426201139Smav 0, 1427201139Smav req->data, 1428249934Ssmh ((ranges + ATA_DSM_BLK_RANGES - 1) / 1429249934Ssmh ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE, 1430201139Smav ada_default_timeout * 1000); 1431201139Smav ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT, 1432249934Ssmh ATA_DSM_TRIM, 0, (ranges + ATA_DSM_BLK_RANGES - 1433249934Ssmh 1) / ATA_DSM_BLK_RANGES); 1434201139Smav start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM; 1435201139Smav goto out; 1436201139Smav } 1437201139Smav /* Run regular command. */ 1438201139Smav bp = bioq_first(&softc->bio_queue); 1439201139Smav if (bp == NULL) { 1440195534Sscottl xpt_release_ccb(start_ccb); 1441201139Smav break; 1442201139Smav } 1443201139Smav bioq_remove(&softc->bio_queue, bp); 1444201139Smav 1445212160Sgibbs if ((bp->bio_flags & BIO_ORDERED) != 0 1446212160Sgibbs || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) { 1447201139Smav softc->flags &= ~ADA_FLAG_NEED_OTAG; 1448201139Smav softc->ordered_tag_count++; 1449201139Smav tag_code = 0; 1450195534Sscottl } else { 1451201139Smav tag_code = 1; 1452201139Smav } 1453201139Smav switch (bp->bio_cmd) { 1454201139Smav case BIO_READ: 1455201139Smav case BIO_WRITE: 1456201139Smav { 1457201139Smav uint64_t lba = bp->bio_pblkno; 1458201139Smav uint16_t count = bp->bio_bcount / softc->params.secsize; 1459220454Smav#ifdef ADA_TEST_FAILURE 1460220454Smav int fail = 0; 1461195534Sscottl 1462220454Smav /* 1463220454Smav * Support the failure ioctls. If the command is a 1464220454Smav * read, and there are pending forced read errors, or 1465220454Smav * if a write and pending write errors, then fail this 1466220454Smav * operation with EIO. This is useful for testing 1467220454Smav * purposes. Also, support having every Nth read fail. 1468220454Smav * 1469220454Smav * This is a rather blunt tool. 1470220454Smav */ 1471220454Smav if (bp->bio_cmd == BIO_READ) { 1472220454Smav if (softc->force_read_error) { 1473220454Smav softc->force_read_error--; 1474220454Smav fail = 1; 1475220454Smav } 1476220454Smav if (softc->periodic_read_error > 0) { 1477220454Smav if (++softc->periodic_read_count >= 1478220454Smav softc->periodic_read_error) { 1479220454Smav softc->periodic_read_count = 0; 1480220454Smav fail = 1; 1481220454Smav } 1482220454Smav } 1483220454Smav } else { 1484220454Smav if (softc->force_write_error) { 1485220454Smav softc->force_write_error--; 1486220454Smav fail = 1; 1487220454Smav } 1488220454Smav } 1489220454Smav if (fail) { 1490220454Smav bp->bio_error = EIO; 1491220454Smav bp->bio_flags |= BIO_ERROR; 1492220454Smav biodone(bp); 1493220454Smav xpt_release_ccb(start_ccb); 1494220454Smav adaschedule(periph); 1495220454Smav return; 1496220454Smav } 1497220454Smav#endif 1498248519Skib KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 || 1499248519Skib round_page(bp->bio_bcount + bp->bio_ma_offset) / 1500248519Skib PAGE_SIZE == bp->bio_ma_n, 1501248519Skib ("Short bio %p", bp)); 1502201139Smav cam_fill_ataio(ataio, 1503201139Smav ada_retry_count, 1504201139Smav adadone, 1505248519Skib (bp->bio_cmd == BIO_READ ? CAM_DIR_IN : 1506248519Skib CAM_DIR_OUT) | ((bp->bio_flags & BIO_UNMAPPED) 1507248519Skib != 0 ? CAM_DATA_BIO : 0), 1508201139Smav tag_code, 1509248519Skib ((bp->bio_flags & BIO_UNMAPPED) != 0) ? (void *)bp : 1510248519Skib bp->bio_data, 1511201139Smav bp->bio_bcount, 1512201139Smav ada_default_timeout*1000); 1513195534Sscottl 1514201139Smav if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) { 1515201139Smav if (bp->bio_cmd == BIO_READ) { 1516201139Smav ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED, 1517201139Smav lba, count); 1518201139Smav } else { 1519201139Smav ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED, 1520201139Smav lba, count); 1521201139Smav } 1522201139Smav } else if ((softc->flags & ADA_FLAG_CAN_48BIT) && 1523201139Smav (lba + count >= ATA_MAX_28BIT_LBA || 1524201139Smav count > 256)) { 1525249199Smarius if (softc->flags & ADA_FLAG_CAN_DMA48) { 1526195534Sscottl if (bp->bio_cmd == BIO_READ) { 1527201139Smav ata_48bit_cmd(ataio, ATA_READ_DMA48, 1528201139Smav 0, lba, count); 1529195534Sscottl } else { 1530201139Smav ata_48bit_cmd(ataio, ATA_WRITE_DMA48, 1531201139Smav 0, lba, count); 1532195534Sscottl } 1533201139Smav } else { 1534201139Smav if (bp->bio_cmd == BIO_READ) { 1535201139Smav ata_48bit_cmd(ataio, ATA_READ_MUL48, 1536201139Smav 0, lba, count); 1537195534Sscottl } else { 1538201139Smav ata_48bit_cmd(ataio, ATA_WRITE_MUL48, 1539201139Smav 0, lba, count); 1540195534Sscottl } 1541201139Smav } 1542201139Smav } else { 1543201139Smav if (count == 256) 1544201139Smav count = 0; 1545201139Smav if (softc->flags & ADA_FLAG_CAN_DMA) { 1546201139Smav if (bp->bio_cmd == BIO_READ) { 1547201139Smav ata_28bit_cmd(ataio, ATA_READ_DMA, 1548201139Smav 0, lba, count); 1549201139Smav } else { 1550201139Smav ata_28bit_cmd(ataio, ATA_WRITE_DMA, 1551201139Smav 0, lba, count); 1552201139Smav } 1553195534Sscottl } else { 1554201139Smav if (bp->bio_cmd == BIO_READ) { 1555201139Smav ata_28bit_cmd(ataio, ATA_READ_MUL, 1556201139Smav 0, lba, count); 1557195534Sscottl } else { 1558201139Smav ata_28bit_cmd(ataio, ATA_WRITE_MUL, 1559201139Smav 0, lba, count); 1560195534Sscottl } 1561195534Sscottl } 1562195534Sscottl } 1563201139Smav break; 1564201139Smav } 1565201139Smav case BIO_DELETE: 1566201139Smav { 1567201139Smav uint64_t lba = bp->bio_pblkno; 1568201139Smav uint16_t count = bp->bio_bcount / softc->params.secsize; 1569195534Sscottl 1570201139Smav cam_fill_ataio(ataio, 1571201139Smav ada_retry_count, 1572201139Smav adadone, 1573201139Smav CAM_DIR_NONE, 1574201139Smav 0, 1575201139Smav NULL, 1576201139Smav 0, 1577201139Smav ada_default_timeout*1000); 1578201139Smav 1579201139Smav if (count >= 256) 1580201139Smav count = 0; 1581201139Smav ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count); 1582201139Smav break; 1583195534Sscottl } 1584201139Smav case BIO_FLUSH: 1585201139Smav cam_fill_ataio(ataio, 1586201139Smav 1, 1587201139Smav adadone, 1588201139Smav CAM_DIR_NONE, 1589201139Smav 0, 1590201139Smav NULL, 1591201139Smav 0, 1592201139Smav ada_default_timeout*1000); 1593201139Smav 1594201139Smav if (softc->flags & ADA_FLAG_CAN_48BIT) 1595201139Smav ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1596201139Smav else 1597201139Smav ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0); 1598201139Smav break; 1599195534Sscottl } 1600201139Smav start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO; 1601201139Smavout: 1602201139Smav start_ccb->ccb_h.ccb_bp = bp; 1603201139Smav softc->outstanding_cmds++; 1604201139Smav xpt_action(start_ccb); 1605201139Smav 1606201139Smav /* May have more work to do, so ensure we stay scheduled */ 1607201139Smav adaschedule(periph); 1608195534Sscottl break; 1609195534Sscottl } 1610224497Smav case ADA_STATE_RAHEAD: 1611220412Smav case ADA_STATE_WCACHE: 1612220412Smav { 1613249981Smav if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 1614224497Smav softc->state = ADA_STATE_NORMAL; 1615224497Smav xpt_release_ccb(start_ccb); 1616224497Smav cam_periph_release_locked(periph); 1617224497Smav return; 1618224497Smav } 1619224497Smav 1620220412Smav cam_fill_ataio(ataio, 1621220412Smav 1, 1622220412Smav adadone, 1623220412Smav CAM_DIR_NONE, 1624220412Smav 0, 1625220412Smav NULL, 1626220412Smav 0, 1627220412Smav ada_default_timeout*1000); 1628220412Smav 1629224497Smav if (softc->state == ADA_STATE_RAHEAD) { 1630224497Smav ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ? 1631224497Smav ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0); 1632224497Smav start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD; 1633224497Smav } else { 1634224497Smav ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ? 1635224497Smav ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0); 1636224497Smav start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE; 1637224497Smav } 1638249466Smav start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 1639220412Smav xpt_action(start_ccb); 1640220412Smav break; 1641195534Sscottl } 1642220412Smav } 1643195534Sscottl} 1644195534Sscottl 1645195534Sscottlstatic void 1646195534Sscottladadone(struct cam_periph *periph, union ccb *done_ccb) 1647195534Sscottl{ 1648195534Sscottl struct ada_softc *softc; 1649195534Sscottl struct ccb_ataio *ataio; 1650224497Smav struct ccb_getdev *cgd; 1651249466Smav struct cam_path *path; 1652195534Sscottl 1653195534Sscottl softc = (struct ada_softc *)periph->softc; 1654195534Sscottl ataio = &done_ccb->ataio; 1655249466Smav path = done_ccb->ccb_h.path; 1656236602Smav 1657249466Smav CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n")); 1658236602Smav 1659195534Sscottl switch (ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) { 1660195534Sscottl case ADA_CCB_BUFFER_IO: 1661201139Smav case ADA_CCB_TRIM: 1662195534Sscottl { 1663195534Sscottl struct bio *bp; 1664195534Sscottl 1665195534Sscottl bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 1666195534Sscottl if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1667195534Sscottl int error; 1668195534Sscottl 1669198328Smav error = adaerror(done_ccb, 0, 0); 1670195534Sscottl if (error == ERESTART) { 1671198328Smav /* A retry was scheduled, so just return. */ 1672195534Sscottl return; 1673195534Sscottl } 1674195534Sscottl if (error != 0) { 1675195534Sscottl bp->bio_error = error; 1676195534Sscottl bp->bio_resid = bp->bio_bcount; 1677195534Sscottl bp->bio_flags |= BIO_ERROR; 1678195534Sscottl } else { 1679195534Sscottl bp->bio_resid = ataio->resid; 1680195534Sscottl bp->bio_error = 0; 1681195534Sscottl if (bp->bio_resid != 0) 1682195534Sscottl bp->bio_flags |= BIO_ERROR; 1683195534Sscottl } 1684195534Sscottl if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1685249466Smav cam_release_devq(path, 1686195534Sscottl /*relsim_flags*/0, 1687195534Sscottl /*reduction*/0, 1688195534Sscottl /*timeout*/0, 1689195534Sscottl /*getcount_only*/0); 1690195534Sscottl } else { 1691195534Sscottl if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1692195534Sscottl panic("REQ_CMP with QFRZN"); 1693195534Sscottl bp->bio_resid = ataio->resid; 1694195534Sscottl if (ataio->resid > 0) 1695195534Sscottl bp->bio_flags |= BIO_ERROR; 1696195534Sscottl } 1697195534Sscottl softc->outstanding_cmds--; 1698195534Sscottl if (softc->outstanding_cmds == 0) 1699195534Sscottl softc->flags |= ADA_FLAG_WENT_IDLE; 1700201139Smav if ((ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) == 1701201139Smav ADA_CCB_TRIM) { 1702201139Smav struct trim_request *req = 1703201139Smav (struct trim_request *)ataio->data_ptr; 1704201139Smav int i; 1705195534Sscottl 1706222628Smav for (i = 1; i < TRIM_MAX_BIOS && req->bps[i]; i++) { 1707201139Smav struct bio *bp1 = req->bps[i]; 1708201139Smav 1709201139Smav bp1->bio_resid = bp->bio_resid; 1710201139Smav bp1->bio_error = bp->bio_error; 1711201139Smav if (bp->bio_flags & BIO_ERROR) 1712201139Smav bp1->bio_flags |= BIO_ERROR; 1713201139Smav biodone(bp1); 1714201139Smav } 1715201139Smav softc->trim_running = 0; 1716201139Smav biodone(bp); 1717201139Smav adaschedule(periph); 1718201139Smav } else 1719201139Smav biodone(bp); 1720195534Sscottl break; 1721195534Sscottl } 1722224497Smav case ADA_CCB_RAHEAD: 1723224497Smav { 1724224497Smav if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1725224497Smav if (adaerror(done_ccb, 0, 0) == ERESTART) { 1726249466Smavout: 1727249466Smav /* Drop freeze taken due to CAM_DEV_QFREEZE */ 1728249466Smav cam_release_devq(path, 0, 0, 0, FALSE); 1729224497Smav return; 1730224497Smav } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1731249466Smav cam_release_devq(path, 1732224497Smav /*relsim_flags*/0, 1733224497Smav /*reduction*/0, 1734224497Smav /*timeout*/0, 1735224497Smav /*getcount_only*/0); 1736224497Smav } 1737224497Smav } 1738224497Smav 1739224497Smav /* 1740224497Smav * Since our peripheral may be invalidated by an error 1741224497Smav * above or an external event, we must release our CCB 1742224497Smav * before releasing the reference on the peripheral. 1743224497Smav * The peripheral will only go away once the last reference 1744224497Smav * is removed, and we need it around for the CCB release 1745224497Smav * operation. 1746224497Smav */ 1747224497Smav cgd = (struct ccb_getdev *)done_ccb; 1748249466Smav xpt_setup_ccb(&cgd->ccb_h, path, CAM_PRIORITY_NORMAL); 1749224497Smav cgd->ccb_h.func_code = XPT_GDEV_TYPE; 1750224497Smav xpt_action((union ccb *)cgd); 1751224497Smav if (ADA_WC >= 0 && 1752224497Smav cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) { 1753224497Smav softc->state = ADA_STATE_WCACHE; 1754224497Smav xpt_release_ccb(done_ccb); 1755224497Smav xpt_schedule(periph, CAM_PRIORITY_DEV); 1756249466Smav goto out; 1757224497Smav } 1758224497Smav softc->state = ADA_STATE_NORMAL; 1759224497Smav xpt_release_ccb(done_ccb); 1760249466Smav /* Drop freeze taken due to CAM_DEV_QFREEZE */ 1761249466Smav cam_release_devq(path, 0, 0, 0, FALSE); 1762224497Smav adaschedule(periph); 1763224497Smav cam_periph_release_locked(periph); 1764224497Smav return; 1765224497Smav } 1766220412Smav case ADA_CCB_WCACHE: 1767220412Smav { 1768220412Smav if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1769220412Smav if (adaerror(done_ccb, 0, 0) == ERESTART) { 1770249466Smav goto out; 1771220412Smav } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1772249466Smav cam_release_devq(path, 1773220412Smav /*relsim_flags*/0, 1774220412Smav /*reduction*/0, 1775220412Smav /*timeout*/0, 1776220412Smav /*getcount_only*/0); 1777220412Smav } 1778220412Smav } 1779220412Smav 1780220412Smav softc->state = ADA_STATE_NORMAL; 1781220412Smav /* 1782220412Smav * Since our peripheral may be invalidated by an error 1783220412Smav * above or an external event, we must release our CCB 1784220412Smav * before releasing the reference on the peripheral. 1785220412Smav * The peripheral will only go away once the last reference 1786220412Smav * is removed, and we need it around for the CCB release 1787220412Smav * operation. 1788220412Smav */ 1789220412Smav xpt_release_ccb(done_ccb); 1790249466Smav /* Drop freeze taken due to CAM_DEV_QFREEZE */ 1791249466Smav cam_release_devq(path, 0, 0, 0, FALSE); 1792220412Smav adaschedule(periph); 1793220412Smav cam_periph_release_locked(periph); 1794220412Smav return; 1795220412Smav } 1796195534Sscottl case ADA_CCB_WAITING: 1797195534Sscottl { 1798195534Sscottl /* Caller will release the CCB */ 1799195534Sscottl wakeup(&done_ccb->ccb_h.cbfcnp); 1800195534Sscottl return; 1801195534Sscottl } 1802195534Sscottl case ADA_CCB_DUMP: 1803195534Sscottl /* No-op. We're polling */ 1804195534Sscottl return; 1805195534Sscottl default: 1806195534Sscottl break; 1807195534Sscottl } 1808195534Sscottl xpt_release_ccb(done_ccb); 1809195534Sscottl} 1810195534Sscottl 1811195534Sscottlstatic int 1812195534Sscottladaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 1813195534Sscottl{ 1814195534Sscottl 1815203385Smav return(cam_periph_error(ccb, cam_flags, sense_flags, NULL)); 1816195534Sscottl} 1817195534Sscottl 1818195534Sscottlstatic void 1819198897Smavadagetparams(struct cam_periph *periph, struct ccb_getdev *cgd) 1820195534Sscottl{ 1821195534Sscottl struct ada_softc *softc = (struct ada_softc *)periph->softc; 1822195534Sscottl struct disk_params *dp = &softc->params; 1823195534Sscottl u_int64_t lbasize48; 1824195534Sscottl u_int32_t lbasize; 1825195534Sscottl 1826198897Smav dp->secsize = ata_logical_sector_size(&cgd->ident_data); 1827195534Sscottl if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) && 1828195534Sscottl cgd->ident_data.current_heads && cgd->ident_data.current_sectors) { 1829195534Sscottl dp->heads = cgd->ident_data.current_heads; 1830195534Sscottl dp->secs_per_track = cgd->ident_data.current_sectors; 1831195534Sscottl dp->cylinders = cgd->ident_data.cylinders; 1832195534Sscottl dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 | 1833195534Sscottl ((u_int32_t)cgd->ident_data.current_size_2 << 16); 1834195534Sscottl } else { 1835195534Sscottl dp->heads = cgd->ident_data.heads; 1836195534Sscottl dp->secs_per_track = cgd->ident_data.sectors; 1837195534Sscottl dp->cylinders = cgd->ident_data.cylinders; 1838195534Sscottl dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track; 1839195534Sscottl } 1840195534Sscottl lbasize = (u_int32_t)cgd->ident_data.lba_size_1 | 1841195534Sscottl ((u_int32_t)cgd->ident_data.lba_size_2 << 16); 1842195534Sscottl 1843195534Sscottl /* use the 28bit LBA size if valid or bigger than the CHS mapping */ 1844195534Sscottl if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize) 1845195534Sscottl dp->sectors = lbasize; 1846195534Sscottl 1847195534Sscottl /* use the 48bit LBA size if valid */ 1848195534Sscottl lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) | 1849195534Sscottl ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) | 1850195534Sscottl ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) | 1851195534Sscottl ((u_int64_t)cgd->ident_data.lba_size48_4 << 48); 1852195534Sscottl if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) && 1853195534Sscottl lbasize48 > ATA_MAX_28BIT_LBA) 1854195534Sscottl dp->sectors = lbasize48; 1855195534Sscottl} 1856195534Sscottl 1857195534Sscottlstatic void 1858195534Sscottladasendorderedtag(void *arg) 1859195534Sscottl{ 1860195534Sscottl struct ada_softc *softc = arg; 1861195534Sscottl 1862195534Sscottl if (ada_send_ordered) { 1863195534Sscottl if ((softc->ordered_tag_count == 0) 1864195534Sscottl && ((softc->flags & ADA_FLAG_WENT_IDLE) == 0)) { 1865195534Sscottl softc->flags |= ADA_FLAG_NEED_OTAG; 1866195534Sscottl } 1867195534Sscottl if (softc->outstanding_cmds > 0) 1868195534Sscottl softc->flags &= ~ADA_FLAG_WENT_IDLE; 1869195534Sscottl 1870195534Sscottl softc->ordered_tag_count = 0; 1871195534Sscottl } 1872195534Sscottl /* Queue us up again */ 1873195534Sscottl callout_reset(&softc->sendordered_c, 1874230921Smav (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL, 1875195534Sscottl adasendorderedtag, softc); 1876195534Sscottl} 1877195534Sscottl 1878195534Sscottl/* 1879195534Sscottl * Step through all ADA peripheral drivers, and if the device is still open, 1880195534Sscottl * sync the disk cache to physical media. 1881195534Sscottl */ 1882195534Sscottlstatic void 1883220650Smavadaflush(void) 1884195534Sscottl{ 1885195534Sscottl struct cam_periph *periph; 1886195534Sscottl struct ada_softc *softc; 1887248872Smav union ccb *ccb; 1888236814Smav int error; 1889195534Sscottl 1890248868Smav CAM_PERIPH_FOREACH(periph, &adadriver) { 1891200180Smav /* If we paniced with lock held - not recurse here. */ 1892200180Smav if (cam_periph_owned(periph)) 1893200180Smav continue; 1894195534Sscottl cam_periph_lock(periph); 1895195534Sscottl softc = (struct ada_softc *)periph->softc; 1896195534Sscottl /* 1897195534Sscottl * We only sync the cache if the drive is still open, and 1898195534Sscottl * if the drive is capable of it.. 1899195534Sscottl */ 1900195534Sscottl if (((softc->flags & ADA_FLAG_OPEN) == 0) || 1901195534Sscottl (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) { 1902195534Sscottl cam_periph_unlock(periph); 1903195534Sscottl continue; 1904195534Sscottl } 1905195534Sscottl 1906248872Smav ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1907248872Smav cam_fill_ataio(&ccb->ataio, 1908236814Smav 0, 1909195534Sscottl adadone, 1910195534Sscottl CAM_DIR_NONE, 1911195534Sscottl 0, 1912195534Sscottl NULL, 1913195534Sscottl 0, 1914195534Sscottl ada_default_timeout*1000); 1915195534Sscottl if (softc->flags & ADA_FLAG_CAN_48BIT) 1916248872Smav ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0); 1917195534Sscottl else 1918248872Smav ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0); 1919195534Sscottl 1920248872Smav error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 1921248872Smav /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 1922248872Smav softc->disk->d_devstat); 1923236814Smav if (error != 0) 1924195534Sscottl xpt_print(periph->path, "Synchronize cache failed\n"); 1925249048Smav xpt_release_ccb(ccb); 1926195534Sscottl cam_periph_unlock(periph); 1927195534Sscottl } 1928220650Smav} 1929214279Sbrucec 1930220650Smavstatic void 1931220650Smavadaspindown(uint8_t cmd, int flags) 1932220650Smav{ 1933220650Smav struct cam_periph *periph; 1934220650Smav struct ada_softc *softc; 1935248872Smav union ccb *ccb; 1936236814Smav int error; 1937214279Sbrucec 1938248868Smav CAM_PERIPH_FOREACH(periph, &adadriver) { 1939214279Sbrucec /* If we paniced with lock held - not recurse here. */ 1940214279Sbrucec if (cam_periph_owned(periph)) 1941214279Sbrucec continue; 1942214279Sbrucec cam_periph_lock(periph); 1943214279Sbrucec softc = (struct ada_softc *)periph->softc; 1944214279Sbrucec /* 1945214279Sbrucec * We only spin-down the drive if it is capable of it.. 1946214279Sbrucec */ 1947214279Sbrucec if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 1948214279Sbrucec cam_periph_unlock(periph); 1949214279Sbrucec continue; 1950214279Sbrucec } 1951214279Sbrucec 1952214279Sbrucec if (bootverbose) 1953214279Sbrucec xpt_print(periph->path, "spin-down\n"); 1954214279Sbrucec 1955248872Smav ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1956248872Smav cam_fill_ataio(&ccb->ataio, 1957236814Smav 0, 1958214279Sbrucec adadone, 1959220650Smav CAM_DIR_NONE | flags, 1960214279Sbrucec 0, 1961214279Sbrucec NULL, 1962214279Sbrucec 0, 1963214279Sbrucec ada_default_timeout*1000); 1964248872Smav ata_28bit_cmd(&ccb->ataio, cmd, 0, 0, 0); 1965214279Sbrucec 1966248872Smav error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0, 1967248872Smav /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 1968248872Smav softc->disk->d_devstat); 1969236814Smav if (error != 0) 1970214279Sbrucec xpt_print(periph->path, "Spin-down disk failed\n"); 1971249048Smav xpt_release_ccb(ccb); 1972214279Sbrucec cam_periph_unlock(periph); 1973214279Sbrucec } 1974195534Sscottl} 1975195534Sscottl 1976220650Smavstatic void 1977220650Smavadashutdown(void *arg, int howto) 1978220650Smav{ 1979220650Smav 1980220650Smav adaflush(); 1981220650Smav if (ada_spindown_shutdown != 0 && 1982220650Smav (howto & (RB_HALT | RB_POWEROFF)) != 0) 1983220650Smav adaspindown(ATA_STANDBY_IMMEDIATE, 0); 1984220650Smav} 1985220650Smav 1986220650Smavstatic void 1987220650Smavadasuspend(void *arg) 1988220650Smav{ 1989220650Smav 1990220650Smav adaflush(); 1991220650Smav if (ada_spindown_suspend != 0) 1992220650Smav adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE); 1993220650Smav} 1994220650Smav 1995220650Smavstatic void 1996220650Smavadaresume(void *arg) 1997220650Smav{ 1998220650Smav struct cam_periph *periph; 1999220650Smav struct ada_softc *softc; 2000220650Smav 2001220650Smav if (ada_spindown_suspend == 0) 2002220650Smav return; 2003220650Smav 2004248868Smav CAM_PERIPH_FOREACH(periph, &adadriver) { 2005220650Smav cam_periph_lock(periph); 2006220650Smav softc = (struct ada_softc *)periph->softc; 2007220650Smav /* 2008220650Smav * We only spin-down the drive if it is capable of it.. 2009220650Smav */ 2010220650Smav if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) { 2011220650Smav cam_periph_unlock(periph); 2012220650Smav continue; 2013220650Smav } 2014220650Smav 2015220650Smav if (bootverbose) 2016220650Smav xpt_print(periph->path, "resume\n"); 2017220650Smav 2018220650Smav /* 2019220650Smav * Drop freeze taken due to CAM_DEV_QFREEZE flag set on 2020220650Smav * sleep request. 2021220650Smav */ 2022220650Smav cam_release_devq(periph->path, 2023220650Smav /*relsim_flags*/0, 2024220650Smav /*openings*/0, 2025220650Smav /*timeout*/0, 2026220650Smav /*getcount_only*/0); 2027220650Smav 2028220650Smav cam_periph_unlock(periph); 2029220650Smav } 2030220650Smav} 2031220650Smav 2032195534Sscottl#endif /* _KERNEL */ 2033