ahci_pci.c revision 199322
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/dev/ahci/ahci.c 199322 2009-11-16 15:38:27Z mav $"); 29195534Sscottl 30195534Sscottl#include <sys/param.h> 31195534Sscottl#include <sys/module.h> 32195534Sscottl#include <sys/systm.h> 33195534Sscottl#include <sys/kernel.h> 34195534Sscottl#include <sys/ata.h> 35195534Sscottl#include <sys/bus.h> 36195534Sscottl#include <sys/endian.h> 37195534Sscottl#include <sys/malloc.h> 38195534Sscottl#include <sys/lock.h> 39195534Sscottl#include <sys/mutex.h> 40195534Sscottl#include <sys/sema.h> 41195534Sscottl#include <sys/taskqueue.h> 42195534Sscottl#include <vm/uma.h> 43195534Sscottl#include <machine/stdarg.h> 44195534Sscottl#include <machine/resource.h> 45195534Sscottl#include <machine/bus.h> 46195534Sscottl#include <sys/rman.h> 47195534Sscottl#include <dev/pci/pcivar.h> 48195534Sscottl#include <dev/pci/pcireg.h> 49195534Sscottl#include "ahci.h" 50195534Sscottl 51195534Sscottl#include <cam/cam.h> 52195534Sscottl#include <cam/cam_ccb.h> 53195534Sscottl#include <cam/cam_sim.h> 54195534Sscottl#include <cam/cam_xpt_sim.h> 55195534Sscottl#include <cam/cam_xpt_periph.h> 56195534Sscottl#include <cam/cam_debug.h> 57195534Sscottl 58195534Sscottl/* local prototypes */ 59195534Sscottlstatic int ahci_setup_interrupt(device_t dev); 60195534Sscottlstatic void ahci_intr(void *data); 61195534Sscottlstatic void ahci_intr_one(void *data); 62195534Sscottlstatic int ahci_suspend(device_t dev); 63195534Sscottlstatic int ahci_resume(device_t dev); 64195534Sscottlstatic int ahci_ch_suspend(device_t dev); 65195534Sscottlstatic int ahci_ch_resume(device_t dev); 66196656Smavstatic void ahci_ch_pm(void *arg); 67195534Sscottlstatic void ahci_ch_intr_locked(void *data); 68195534Sscottlstatic void ahci_ch_intr(void *data); 69195534Sscottlstatic int ahci_ctlr_reset(device_t dev); 70195534Sscottlstatic void ahci_begin_transaction(device_t dev, union ccb *ccb); 71195534Sscottlstatic void ahci_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error); 72195534Sscottlstatic void ahci_execute_transaction(struct ahci_slot *slot); 73195534Sscottlstatic void ahci_timeout(struct ahci_slot *slot); 74195534Sscottlstatic void ahci_end_transaction(struct ahci_slot *slot, enum ahci_err_type et); 75195534Sscottlstatic int ahci_setup_fis(struct ahci_cmd_tab *ctp, union ccb *ccb, int tag); 76195534Sscottlstatic void ahci_dmainit(device_t dev); 77195534Sscottlstatic void ahci_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error); 78195534Sscottlstatic void ahci_dmafini(device_t dev); 79195534Sscottlstatic void ahci_slotsalloc(device_t dev); 80195534Sscottlstatic void ahci_slotsfree(device_t dev); 81195534Sscottlstatic void ahci_reset(device_t dev); 82195534Sscottlstatic void ahci_start(device_t dev); 83195534Sscottlstatic void ahci_stop(device_t dev); 84195534Sscottlstatic void ahci_clo(device_t dev); 85195534Sscottlstatic void ahci_start_fr(device_t dev); 86195534Sscottlstatic void ahci_stop_fr(device_t dev); 87195534Sscottl 88195534Sscottlstatic int ahci_sata_connect(struct ahci_channel *ch); 89195534Sscottlstatic int ahci_sata_phy_reset(device_t dev, int quick); 90195534Sscottl 91195534Sscottlstatic void ahci_issue_read_log(device_t dev); 92195534Sscottlstatic void ahci_process_read_log(device_t dev, union ccb *ccb); 93195534Sscottl 94195534Sscottlstatic void ahciaction(struct cam_sim *sim, union ccb *ccb); 95195534Sscottlstatic void ahcipoll(struct cam_sim *sim); 96195534Sscottl 97195534SscottlMALLOC_DEFINE(M_AHCI, "AHCI driver", "AHCI driver data buffers"); 98195534Sscottl 99199176Smavstatic struct { 100199176Smav uint32_t id; 101199176Smav const char *name; 102199322Smav int quirks; 103199322Smav#define AHCI_Q_NOFORCE 1 104199322Smav#define AHCI_Q_NOPMP 2 105199322Smav#define AHCI_Q_NONCQ 4 106199322Smav#define AHCI_Q_1CH 8 107199322Smav#define AHCI_Q_2CH 16 108199322Smav#define AHCI_Q_4CH 32 109199322Smav#define AHCI_Q_EDGEIS 64 110199176Smav} ahci_ids[] = { 111199176Smav {0x43801002, "ATI IXP600", 0}, 112199176Smav {0x43901002, "ATI IXP700", 0}, 113199176Smav {0x43911002, "ATI IXP700", 0}, 114199176Smav {0x43921002, "ATI IXP700", 0}, 115199176Smav {0x43931002, "ATI IXP700", 0}, 116199176Smav {0x43941002, "ATI IXP800", 0}, 117199176Smav {0x43951002, "ATI IXP800", 0}, 118199176Smav {0x26528086, "Intel ICH6", 0}, 119199176Smav {0x26538086, "Intel ICH6M", 0}, 120199176Smav {0x26818086, "Intel ESB2", 0}, 121199176Smav {0x26828086, "Intel ESB2", 0}, 122199176Smav {0x26838086, "Intel ESB2", 0}, 123199176Smav {0x27c18086, "Intel ICH7", 0}, 124199176Smav {0x27c38086, "Intel ICH7", 0}, 125199176Smav {0x27c58086, "Intel ICH7M", 0}, 126199176Smav {0x27c68086, "Intel ICH7M", 0}, 127199176Smav {0x28218086, "Intel ICH8", 0}, 128199176Smav {0x28228086, "Intel ICH8", 0}, 129199176Smav {0x28248086, "Intel ICH8", 0}, 130199176Smav {0x28298086, "Intel ICH8M", 0}, 131199176Smav {0x282a8086, "Intel ICH8M", 0}, 132199176Smav {0x29228086, "Intel ICH9", 0}, 133199176Smav {0x29238086, "Intel ICH9", 0}, 134199176Smav {0x29248086, "Intel ICH9", 0}, 135199176Smav {0x29258086, "Intel ICH9", 0}, 136199176Smav {0x29278086, "Intel ICH9", 0}, 137199176Smav {0x29298086, "Intel ICH9M", 0}, 138199176Smav {0x292a8086, "Intel ICH9M", 0}, 139199176Smav {0x292b8086, "Intel ICH9M", 0}, 140199176Smav {0x292c8086, "Intel ICH9M", 0}, 141199176Smav {0x292f8086, "Intel ICH9M", 0}, 142199176Smav {0x294d8086, "Intel ICH9", 0}, 143199176Smav {0x294e8086, "Intel ICH9M", 0}, 144199176Smav {0x3a058086, "Intel ICH10", 0}, 145199176Smav {0x3a228086, "Intel ICH10", 0}, 146199176Smav {0x3a258086, "Intel ICH10", 0}, 147199176Smav {0x3b228086, "Intel PCH", 0}, 148199176Smav {0x3b238086, "Intel PCH", 0}, 149199176Smav {0x3b248086, "Intel PCH", 0}, 150199176Smav {0x3b258086, "Intel PCH", 0}, 151199176Smav {0x3b298086, "Intel PCH", 0}, 152199176Smav {0x3b2b8086, "Intel PCH", 0}, 153199176Smav {0x3b2c8086, "Intel PCH", 0}, 154199176Smav {0x3b2f8086, "Intel PCH", 0}, 155199322Smav {0x2361197b, "JMicron JMB361", AHCI_Q_NOFORCE}, 156199322Smav {0x2363197b, "JMicron JMB363", AHCI_Q_NOFORCE}, 157199322Smav {0x2365197b, "JMicron JMB365", AHCI_Q_NOFORCE}, 158199322Smav {0x2366197b, "JMicron JMB366", AHCI_Q_NOFORCE}, 159199322Smav {0x2368197b, "JMicron JMB368", AHCI_Q_NOFORCE}, 160199322Smav {0x611111ab, "Marvell 88SX6111", AHCI_Q_NOFORCE|AHCI_Q_1CH|AHCI_Q_EDGEIS}, 161199322Smav {0x612111ab, "Marvell 88SX6121", AHCI_Q_NOFORCE|AHCI_Q_2CH|AHCI_Q_EDGEIS}, 162199322Smav {0x614111ab, "Marvell 88SX6141", AHCI_Q_NOFORCE|AHCI_Q_4CH|AHCI_Q_EDGEIS}, 163199322Smav {0x614511ab, "Marvell 88SX6145", AHCI_Q_NOFORCE|AHCI_Q_4CH|AHCI_Q_EDGEIS}, 164199176Smav {0x044c10de, "NVIDIA MCP65", 0}, 165199176Smav {0x044d10de, "NVIDIA MCP65", 0}, 166199176Smav {0x044e10de, "NVIDIA MCP65", 0}, 167199176Smav {0x044f10de, "NVIDIA MCP65", 0}, 168199176Smav {0x045c10de, "NVIDIA MCP65", 0}, 169199176Smav {0x045d10de, "NVIDIA MCP65", 0}, 170199176Smav {0x045e10de, "NVIDIA MCP65", 0}, 171199176Smav {0x045f10de, "NVIDIA MCP65", 0}, 172199176Smav {0x055010de, "NVIDIA MCP67", 0}, 173199176Smav {0x055110de, "NVIDIA MCP67", 0}, 174199176Smav {0x055210de, "NVIDIA MCP67", 0}, 175199176Smav {0x055310de, "NVIDIA MCP67", 0}, 176199176Smav {0x055410de, "NVIDIA MCP67", 0}, 177199176Smav {0x055510de, "NVIDIA MCP67", 0}, 178199176Smav {0x055610de, "NVIDIA MCP67", 0}, 179199176Smav {0x055710de, "NVIDIA MCP67", 0}, 180199176Smav {0x055810de, "NVIDIA MCP67", 0}, 181199176Smav {0x055910de, "NVIDIA MCP67", 0}, 182199176Smav {0x055A10de, "NVIDIA MCP67", 0}, 183199176Smav {0x055B10de, "NVIDIA MCP67", 0}, 184199176Smav {0x058410de, "NVIDIA MCP67", 0}, 185199176Smav {0x07f010de, "NVIDIA MCP73", 0}, 186199176Smav {0x07f110de, "NVIDIA MCP73", 0}, 187199176Smav {0x07f210de, "NVIDIA MCP73", 0}, 188199176Smav {0x07f310de, "NVIDIA MCP73", 0}, 189199176Smav {0x07f410de, "NVIDIA MCP73", 0}, 190199176Smav {0x07f510de, "NVIDIA MCP73", 0}, 191199176Smav {0x07f610de, "NVIDIA MCP73", 0}, 192199176Smav {0x07f710de, "NVIDIA MCP73", 0}, 193199176Smav {0x07f810de, "NVIDIA MCP73", 0}, 194199176Smav {0x07f910de, "NVIDIA MCP73", 0}, 195199176Smav {0x07fa10de, "NVIDIA MCP73", 0}, 196199176Smav {0x07fb10de, "NVIDIA MCP73", 0}, 197199176Smav {0x0ad010de, "NVIDIA MCP77", 0}, 198199176Smav {0x0ad110de, "NVIDIA MCP77", 0}, 199199176Smav {0x0ad210de, "NVIDIA MCP77", 0}, 200199176Smav {0x0ad310de, "NVIDIA MCP77", 0}, 201199176Smav {0x0ad410de, "NVIDIA MCP77", 0}, 202199176Smav {0x0ad510de, "NVIDIA MCP77", 0}, 203199176Smav {0x0ad610de, "NVIDIA MCP77", 0}, 204199176Smav {0x0ad710de, "NVIDIA MCP77", 0}, 205199176Smav {0x0ad810de, "NVIDIA MCP77", 0}, 206199176Smav {0x0ad910de, "NVIDIA MCP77", 0}, 207199176Smav {0x0ada10de, "NVIDIA MCP77", 0}, 208199176Smav {0x0adb10de, "NVIDIA MCP77", 0}, 209199176Smav {0x0ab410de, "NVIDIA MCP79", 0}, 210199176Smav {0x0ab510de, "NVIDIA MCP79", 0}, 211199176Smav {0x0ab610de, "NVIDIA MCP79", 0}, 212199176Smav {0x0ab710de, "NVIDIA MCP79", 0}, 213199176Smav {0x0ab810de, "NVIDIA MCP79", 0}, 214199176Smav {0x0ab910de, "NVIDIA MCP79", 0}, 215199176Smav {0x0aba10de, "NVIDIA MCP79", 0}, 216199176Smav {0x0abb10de, "NVIDIA MCP79", 0}, 217199176Smav {0x0abc10de, "NVIDIA MCP79", 0}, 218199176Smav {0x0abd10de, "NVIDIA MCP79", 0}, 219199176Smav {0x0abe10de, "NVIDIA MCP79", 0}, 220199176Smav {0x0abf10de, "NVIDIA MCP79", 0}, 221199176Smav {0x0d8410de, "NVIDIA MCP89", 0}, 222199176Smav {0x0d8510de, "NVIDIA MCP89", 0}, 223199176Smav {0x0d8610de, "NVIDIA MCP89", 0}, 224199176Smav {0x0d8710de, "NVIDIA MCP89", 0}, 225199176Smav {0x0d8810de, "NVIDIA MCP89", 0}, 226199176Smav {0x0d8910de, "NVIDIA MCP89", 0}, 227199176Smav {0x0d8a10de, "NVIDIA MCP89", 0}, 228199176Smav {0x0d8b10de, "NVIDIA MCP89", 0}, 229199176Smav {0x0d8c10de, "NVIDIA MCP89", 0}, 230199176Smav {0x0d8d10de, "NVIDIA MCP89", 0}, 231199176Smav {0x0d8e10de, "NVIDIA MCP89", 0}, 232199176Smav {0x0d8f10de, "NVIDIA MCP89", 0}, 233199176Smav {0x33491106, "VIA VT8251", 0}, 234199176Smav {0x62871106, "VIA VT8251", 0}, 235199176Smav {0x11841039, "SiS 966", 0}, 236199176Smav {0x11851039, "SiS 968", 0}, 237199176Smav {0x01861039, "SiS 968", 0}, 238199176Smav {0, NULL, 0} 239199176Smav}; 240199176Smav 241195534Sscottlstatic int 242195534Sscottlahci_probe(device_t dev) 243195534Sscottl{ 244199176Smav char buf[64]; 245199322Smav int i, valid = 0; 246199322Smav uint32_t devid = pci_get_devid(dev); 247199322Smav 248199322Smav /* Is this a possible AHCI candidate? */ 249199322Smav if (pci_get_class(dev) == PCIC_STORAGE && 250199322Smav pci_get_subclass(dev) == PCIS_STORAGE_SATA && 251199322Smav pci_get_progif(dev) == PCIP_STORAGE_SATA_AHCI_1_0) 252199322Smav valid = 1; 253199322Smav /* Is this a known AHCI chip? */ 254199322Smav for (i = 0; ahci_ids[i].id != 0; i++) { 255199322Smav if (ahci_ids[i].id == devid && 256199322Smav (valid || !(ahci_ids[i].quirks & AHCI_Q_NOFORCE))) { 257199322Smav snprintf(buf, sizeof(buf), "%s AHCI SATA controller", 258199322Smav ahci_ids[i].name); 259199322Smav device_set_desc_copy(dev, buf); 260199322Smav return (BUS_PROBE_VENDOR); 261199322Smav } 262199322Smav } 263199322Smav if (!valid) 264199322Smav return (ENXIO); 265199322Smav device_set_desc_copy(dev, "AHCI SATA controller"); 266199322Smav return (BUS_PROBE_VENDOR); 267199322Smav} 268199322Smav 269199322Smavstatic int 270199322Smavahci_ata_probe(device_t dev) 271199322Smav{ 272199322Smav char buf[64]; 273199176Smav int i; 274199176Smav uint32_t devid = pci_get_devid(dev); 275195534Sscottl 276199322Smav if ((intptr_t)device_get_ivars(dev) >= 0) 277199322Smav return (ENXIO); 278199176Smav /* Is this a known AHCI chip? */ 279199176Smav for (i = 0; ahci_ids[i].id != 0; i++) { 280199176Smav if (ahci_ids[i].id == devid) { 281199176Smav snprintf(buf, sizeof(buf), "%s AHCI SATA controller", 282199176Smav ahci_ids[i].name); 283199176Smav device_set_desc_copy(dev, buf); 284199176Smav return (BUS_PROBE_VENDOR); 285199176Smav } 286199176Smav } 287199176Smav device_set_desc_copy(dev, "AHCI SATA controller"); 288195534Sscottl return (BUS_PROBE_VENDOR); 289195534Sscottl} 290195534Sscottl 291195534Sscottlstatic int 292195534Sscottlahci_attach(device_t dev) 293195534Sscottl{ 294195534Sscottl struct ahci_controller *ctlr = device_get_softc(dev); 295195534Sscottl device_t child; 296199322Smav int error, unit, speed, i; 297199322Smav uint32_t devid = pci_get_devid(dev); 298196656Smav u_int32_t version; 299195534Sscottl 300195534Sscottl ctlr->dev = dev; 301199322Smav i = 0; 302199322Smav while (ahci_ids[i].id != 0 && ahci_ids[i].id != devid) 303199322Smav i++; 304199322Smav ctlr->quirks = ahci_ids[i].quirks; 305196656Smav resource_int_value(device_get_name(dev), 306196656Smav device_get_unit(dev), "ccc", &ctlr->ccc); 307195534Sscottl /* if we have a memory BAR(5) we are likely on an AHCI part */ 308195534Sscottl ctlr->r_rid = PCIR_BAR(5); 309195534Sscottl if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 310195534Sscottl &ctlr->r_rid, RF_ACTIVE))) 311195534Sscottl return ENXIO; 312195534Sscottl /* Setup our own memory management for channels. */ 313195534Sscottl ctlr->sc_iomem.rm_type = RMAN_ARRAY; 314195534Sscottl ctlr->sc_iomem.rm_descr = "I/O memory addresses"; 315195534Sscottl if ((error = rman_init(&ctlr->sc_iomem)) != 0) { 316195534Sscottl bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 317195534Sscottl return (error); 318195534Sscottl } 319195534Sscottl if ((error = rman_manage_region(&ctlr->sc_iomem, 320195534Sscottl rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) { 321195534Sscottl bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 322195534Sscottl rman_fini(&ctlr->sc_iomem); 323195534Sscottl return (error); 324195534Sscottl } 325195534Sscottl /* Reset controller */ 326195534Sscottl if ((error = ahci_ctlr_reset(dev)) != 0) { 327195534Sscottl bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 328195534Sscottl rman_fini(&ctlr->sc_iomem); 329195534Sscottl return (error); 330195534Sscottl }; 331199322Smav /* Get the HW capabilities */ 332199322Smav version = ATA_INL(ctlr->r_mem, AHCI_VS); 333199322Smav ctlr->caps = ATA_INL(ctlr->r_mem, AHCI_CAP); 334199322Smav if (version >= 0x00010020) 335199322Smav ctlr->caps2 = ATA_INL(ctlr->r_mem, AHCI_CAP2); 336195534Sscottl ctlr->ichannels = ATA_INL(ctlr->r_mem, AHCI_PI); 337199322Smav if (ctlr->quirks & AHCI_Q_1CH) { 338199322Smav ctlr->caps &= ~AHCI_CAP_NPMASK; 339199322Smav ctlr->ichannels &= 0x01; 340199322Smav } 341199322Smav if (ctlr->quirks & AHCI_Q_2CH) { 342199322Smav ctlr->caps &= ~AHCI_CAP_NPMASK; 343199322Smav ctlr->caps |= 1; 344199322Smav ctlr->ichannels &= 0x03; 345199322Smav } 346199322Smav if (ctlr->quirks & AHCI_Q_4CH) { 347199322Smav ctlr->caps &= ~AHCI_CAP_NPMASK; 348199322Smav ctlr->caps |= 3; 349199322Smav ctlr->ichannels &= 0x0f; 350199322Smav } 351195534Sscottl ctlr->channels = MAX(flsl(ctlr->ichannels), 352199322Smav (ctlr->caps & AHCI_CAP_NPMASK) + 1); 353199322Smav if (ctlr->quirks & AHCI_Q_NOPMP) 354199322Smav ctlr->caps &= ~AHCI_CAP_SPM; 355199322Smav if (ctlr->quirks & AHCI_Q_NONCQ) 356199322Smav ctlr->caps &= ~AHCI_CAP_SNCQ; 357195534Sscottl /* Setup interrupts. */ 358195534Sscottl if (ahci_setup_interrupt(dev)) { 359195534Sscottl bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 360195534Sscottl rman_fini(&ctlr->sc_iomem); 361195534Sscottl return ENXIO; 362195534Sscottl } 363195534Sscottl /* Announce HW capabilities. */ 364196656Smav speed = (ctlr->caps & AHCI_CAP_ISS) >> AHCI_CAP_ISS_SHIFT; 365195534Sscottl device_printf(dev, 366195534Sscottl "AHCI v%x.%02x with %d %sGbps ports, Port Multiplier %s\n", 367195534Sscottl ((version >> 20) & 0xf0) + ((version >> 16) & 0x0f), 368195534Sscottl ((version >> 4) & 0xf0) + (version & 0x0f), 369196656Smav (ctlr->caps & AHCI_CAP_NPMASK) + 1, 370195534Sscottl ((speed == 1) ? "1.5":((speed == 2) ? "3": 371195534Sscottl ((speed == 3) ? "6":"?"))), 372196656Smav (ctlr->caps & AHCI_CAP_SPM) ? 373195534Sscottl "supported" : "not supported"); 374195534Sscottl if (bootverbose) { 375195534Sscottl device_printf(dev, "Caps:%s%s%s%s%s%s%s%s %sGbps", 376196656Smav (ctlr->caps & AHCI_CAP_64BIT) ? " 64bit":"", 377196656Smav (ctlr->caps & AHCI_CAP_SNCQ) ? " NCQ":"", 378196656Smav (ctlr->caps & AHCI_CAP_SSNTF) ? " SNTF":"", 379196656Smav (ctlr->caps & AHCI_CAP_SMPS) ? " MPS":"", 380196656Smav (ctlr->caps & AHCI_CAP_SSS) ? " SS":"", 381196656Smav (ctlr->caps & AHCI_CAP_SALP) ? " ALP":"", 382196656Smav (ctlr->caps & AHCI_CAP_SAL) ? " AL":"", 383196656Smav (ctlr->caps & AHCI_CAP_SCLO) ? " CLO":"", 384195534Sscottl ((speed == 1) ? "1.5":((speed == 2) ? "3": 385195534Sscottl ((speed == 3) ? "6":"?")))); 386195534Sscottl printf("%s%s%s%s%s%s %dcmd%s%s%s %dports\n", 387196656Smav (ctlr->caps & AHCI_CAP_SAM) ? " AM":"", 388196656Smav (ctlr->caps & AHCI_CAP_SPM) ? " PM":"", 389196656Smav (ctlr->caps & AHCI_CAP_FBSS) ? " FBS":"", 390196656Smav (ctlr->caps & AHCI_CAP_PMD) ? " PMD":"", 391196656Smav (ctlr->caps & AHCI_CAP_SSC) ? " SSC":"", 392196656Smav (ctlr->caps & AHCI_CAP_PSC) ? " PSC":"", 393196656Smav ((ctlr->caps & AHCI_CAP_NCS) >> AHCI_CAP_NCS_SHIFT) + 1, 394196656Smav (ctlr->caps & AHCI_CAP_CCCS) ? " CCC":"", 395196656Smav (ctlr->caps & AHCI_CAP_EMS) ? " EM":"", 396196656Smav (ctlr->caps & AHCI_CAP_SXS) ? " eSATA":"", 397196656Smav (ctlr->caps & AHCI_CAP_NPMASK) + 1); 398195534Sscottl } 399196656Smav if (bootverbose && version >= 0x00010020) { 400196656Smav device_printf(dev, "Caps2:%s%s%s\n", 401196656Smav (ctlr->caps2 & AHCI_CAP2_APST) ? " APST":"", 402196656Smav (ctlr->caps2 & AHCI_CAP2_NVMP) ? " NVMP":"", 403196656Smav (ctlr->caps2 & AHCI_CAP2_BOH) ? " BOH":""); 404196656Smav } 405195534Sscottl /* Attach all channels on this controller */ 406195534Sscottl for (unit = 0; unit < ctlr->channels; unit++) { 407195534Sscottl if ((ctlr->ichannels & (1 << unit)) == 0) 408195534Sscottl continue; 409195534Sscottl child = device_add_child(dev, "ahcich", -1); 410195534Sscottl if (child == NULL) 411195534Sscottl device_printf(dev, "failed to add channel device\n"); 412195534Sscottl else 413195534Sscottl device_set_ivars(child, (void *)(intptr_t)unit); 414195534Sscottl } 415195534Sscottl bus_generic_attach(dev); 416195534Sscottl return 0; 417195534Sscottl} 418195534Sscottl 419195534Sscottlstatic int 420195534Sscottlahci_detach(device_t dev) 421195534Sscottl{ 422195534Sscottl struct ahci_controller *ctlr = device_get_softc(dev); 423195534Sscottl device_t *children; 424195534Sscottl int nchildren, i; 425195534Sscottl 426195534Sscottl /* Detach & delete all children */ 427195534Sscottl if (!device_get_children(dev, &children, &nchildren)) { 428195534Sscottl for (i = 0; i < nchildren; i++) 429195534Sscottl device_delete_child(dev, children[i]); 430195534Sscottl free(children, M_TEMP); 431195534Sscottl } 432195534Sscottl /* Free interrupts. */ 433195534Sscottl for (i = 0; i < ctlr->numirqs; i++) { 434195534Sscottl if (ctlr->irqs[i].r_irq) { 435195534Sscottl bus_teardown_intr(dev, ctlr->irqs[i].r_irq, 436195534Sscottl ctlr->irqs[i].handle); 437195534Sscottl bus_release_resource(dev, SYS_RES_IRQ, 438195534Sscottl ctlr->irqs[i].r_irq_rid, ctlr->irqs[i].r_irq); 439195534Sscottl } 440195534Sscottl } 441195534Sscottl pci_release_msi(dev); 442195534Sscottl /* Free memory. */ 443195534Sscottl rman_fini(&ctlr->sc_iomem); 444195534Sscottl if (ctlr->r_mem) 445195534Sscottl bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 446195534Sscottl return (0); 447195534Sscottl} 448195534Sscottl 449195534Sscottlstatic int 450195534Sscottlahci_ctlr_reset(device_t dev) 451195534Sscottl{ 452195534Sscottl struct ahci_controller *ctlr = device_get_softc(dev); 453195534Sscottl int timeout; 454195534Sscottl 455195534Sscottl if (pci_read_config(dev, 0x00, 4) == 0x28298086 && 456195534Sscottl (pci_read_config(dev, 0x92, 1) & 0xfe) == 0x04) 457195534Sscottl pci_write_config(dev, 0x92, 0x01, 1); 458195534Sscottl /* Enable AHCI mode */ 459195534Sscottl ATA_OUTL(ctlr->r_mem, AHCI_GHC, AHCI_GHC_AE); 460195534Sscottl /* Reset AHCI controller */ 461195534Sscottl ATA_OUTL(ctlr->r_mem, AHCI_GHC, AHCI_GHC_AE|AHCI_GHC_HR); 462195534Sscottl for (timeout = 1000; timeout > 0; timeout--) { 463195534Sscottl DELAY(1000); 464195534Sscottl if ((ATA_INL(ctlr->r_mem, AHCI_GHC) & AHCI_GHC_HR) == 0) 465195534Sscottl break; 466195534Sscottl } 467195534Sscottl if (timeout == 0) { 468195534Sscottl device_printf(dev, "AHCI controller reset failure\n"); 469195534Sscottl return ENXIO; 470195534Sscottl } 471195534Sscottl /* Reenable AHCI mode */ 472195534Sscottl ATA_OUTL(ctlr->r_mem, AHCI_GHC, AHCI_GHC_AE); 473195534Sscottl /* Clear interrupts */ 474195534Sscottl ATA_OUTL(ctlr->r_mem, AHCI_IS, ATA_INL(ctlr->r_mem, AHCI_IS)); 475196656Smav /* Configure CCC */ 476196656Smav if (ctlr->ccc) { 477196656Smav ATA_OUTL(ctlr->r_mem, AHCI_CCCP, ATA_INL(ctlr->r_mem, AHCI_PI)); 478196656Smav ATA_OUTL(ctlr->r_mem, AHCI_CCCC, 479196656Smav (ctlr->ccc << AHCI_CCCC_TV_SHIFT) | 480196656Smav (4 << AHCI_CCCC_CC_SHIFT) | 481196656Smav AHCI_CCCC_EN); 482196656Smav ctlr->cccv = (ATA_INL(ctlr->r_mem, AHCI_CCCC) & 483196656Smav AHCI_CCCC_INT_MASK) >> AHCI_CCCC_INT_SHIFT; 484196656Smav if (bootverbose) { 485196656Smav device_printf(dev, 486196656Smav "CCC with %dms/4cmd enabled on vector %d\n", 487196656Smav ctlr->ccc, ctlr->cccv); 488196656Smav } 489196656Smav } 490195534Sscottl /* Enable AHCI interrupts */ 491195534Sscottl ATA_OUTL(ctlr->r_mem, AHCI_GHC, 492195534Sscottl ATA_INL(ctlr->r_mem, AHCI_GHC) | AHCI_GHC_IE); 493195534Sscottl return (0); 494195534Sscottl} 495195534Sscottl 496195534Sscottlstatic int 497195534Sscottlahci_suspend(device_t dev) 498195534Sscottl{ 499195534Sscottl struct ahci_controller *ctlr = device_get_softc(dev); 500195534Sscottl 501195534Sscottl bus_generic_suspend(dev); 502195534Sscottl /* Disable interupts, so the state change(s) doesn't trigger */ 503195534Sscottl ATA_OUTL(ctlr->r_mem, AHCI_GHC, 504195534Sscottl ATA_INL(ctlr->r_mem, AHCI_GHC) & (~AHCI_GHC_IE)); 505195534Sscottl return 0; 506195534Sscottl} 507195534Sscottl 508195534Sscottlstatic int 509195534Sscottlahci_resume(device_t dev) 510195534Sscottl{ 511195534Sscottl int res; 512195534Sscottl 513195534Sscottl if ((res = ahci_ctlr_reset(dev)) != 0) 514195534Sscottl return (res); 515195534Sscottl return (bus_generic_resume(dev)); 516195534Sscottl} 517195534Sscottl 518195534Sscottlstatic int 519195534Sscottlahci_setup_interrupt(device_t dev) 520195534Sscottl{ 521195534Sscottl struct ahci_controller *ctlr = device_get_softc(dev); 522195534Sscottl int i, msi = 1; 523195534Sscottl 524195534Sscottl /* Process hints. */ 525195534Sscottl resource_int_value(device_get_name(dev), 526195534Sscottl device_get_unit(dev), "msi", &msi); 527195534Sscottl if (msi < 0) 528195534Sscottl msi = 0; 529195534Sscottl else if (msi == 1) 530195534Sscottl msi = min(1, pci_msi_count(dev)); 531195534Sscottl else if (msi > 1) 532195534Sscottl msi = pci_msi_count(dev); 533195534Sscottl /* Allocate MSI if needed/present. */ 534195534Sscottl if (msi && pci_alloc_msi(dev, &msi) == 0) { 535195534Sscottl ctlr->numirqs = msi; 536195534Sscottl } else { 537195534Sscottl msi = 0; 538195534Sscottl ctlr->numirqs = 1; 539195534Sscottl } 540195534Sscottl /* Check for single MSI vector fallback. */ 541195534Sscottl if (ctlr->numirqs > 1 && 542195534Sscottl (ATA_INL(ctlr->r_mem, AHCI_GHC) & AHCI_GHC_MRSM) != 0) { 543195534Sscottl device_printf(dev, "Falling back to one MSI\n"); 544195534Sscottl ctlr->numirqs = 1; 545195534Sscottl } 546195534Sscottl /* Allocate all IRQs. */ 547195534Sscottl for (i = 0; i < ctlr->numirqs; i++) { 548195534Sscottl ctlr->irqs[i].ctlr = ctlr; 549195534Sscottl ctlr->irqs[i].r_irq_rid = i + (msi ? 1 : 0); 550196656Smav if (ctlr->numirqs == 1 || i >= ctlr->channels || 551196656Smav (ctlr->ccc && i == ctlr->cccv)) 552195534Sscottl ctlr->irqs[i].mode = AHCI_IRQ_MODE_ALL; 553195534Sscottl else if (i == ctlr->numirqs - 1) 554195534Sscottl ctlr->irqs[i].mode = AHCI_IRQ_MODE_AFTER; 555195534Sscottl else 556195534Sscottl ctlr->irqs[i].mode = AHCI_IRQ_MODE_ONE; 557195534Sscottl if (!(ctlr->irqs[i].r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 558195534Sscottl &ctlr->irqs[i].r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) { 559195534Sscottl device_printf(dev, "unable to map interrupt\n"); 560195534Sscottl return ENXIO; 561195534Sscottl } 562195534Sscottl if ((bus_setup_intr(dev, ctlr->irqs[i].r_irq, ATA_INTR_FLAGS, NULL, 563195534Sscottl (ctlr->irqs[i].mode == AHCI_IRQ_MODE_ONE) ? ahci_intr_one : ahci_intr, 564195534Sscottl &ctlr->irqs[i], &ctlr->irqs[i].handle))) { 565195534Sscottl /* SOS XXX release r_irq */ 566195534Sscottl device_printf(dev, "unable to setup interrupt\n"); 567195534Sscottl return ENXIO; 568195534Sscottl } 569195534Sscottl } 570195534Sscottl return (0); 571195534Sscottl} 572195534Sscottl 573195534Sscottl/* 574195534Sscottl * Common case interrupt handler. 575195534Sscottl */ 576195534Sscottlstatic void 577195534Sscottlahci_intr(void *data) 578195534Sscottl{ 579195534Sscottl struct ahci_controller_irq *irq = data; 580195534Sscottl struct ahci_controller *ctlr = irq->ctlr; 581195534Sscottl u_int32_t is; 582195534Sscottl void *arg; 583195534Sscottl int unit; 584195534Sscottl 585196656Smav if (irq->mode == AHCI_IRQ_MODE_ALL) { 586195534Sscottl unit = 0; 587196656Smav if (ctlr->ccc) 588196656Smav is = ctlr->ichannels; 589196656Smav else 590196656Smav is = ATA_INL(ctlr->r_mem, AHCI_IS); 591196656Smav } else { /* AHCI_IRQ_MODE_AFTER */ 592195534Sscottl unit = irq->r_irq_rid - 1; 593196656Smav is = ATA_INL(ctlr->r_mem, AHCI_IS); 594196656Smav } 595195534Sscottl for (; unit < ctlr->channels; unit++) { 596195534Sscottl if ((is & (1 << unit)) != 0 && 597195534Sscottl (arg = ctlr->interrupt[unit].argument)) { 598199322Smav if (ctlr->quirks & AHCI_Q_EDGEIS) { 599199322Smav /* Some controller have edge triggered IS. */ 600199322Smav ATA_OUTL(ctlr->r_mem, AHCI_IS, 1 << unit); 601199322Smav ctlr->interrupt[unit].function(arg); 602199322Smav } else { 603199322Smav /* but AHCI declares level triggered IS. */ 604199322Smav ctlr->interrupt[unit].function(arg); 605199322Smav ATA_OUTL(ctlr->r_mem, AHCI_IS, 1 << unit); 606199322Smav } 607195534Sscottl } 608195534Sscottl } 609195534Sscottl} 610195534Sscottl 611195534Sscottl/* 612195534Sscottl * Simplified interrupt handler for multivector MSI mode. 613195534Sscottl */ 614195534Sscottlstatic void 615195534Sscottlahci_intr_one(void *data) 616195534Sscottl{ 617195534Sscottl struct ahci_controller_irq *irq = data; 618195534Sscottl struct ahci_controller *ctlr = irq->ctlr; 619195534Sscottl void *arg; 620195534Sscottl int unit; 621195534Sscottl 622195534Sscottl unit = irq->r_irq_rid - 1; 623195534Sscottl if ((arg = ctlr->interrupt[unit].argument)) 624195534Sscottl ctlr->interrupt[unit].function(arg); 625195534Sscottl} 626195534Sscottl 627195534Sscottlstatic struct resource * 628195534Sscottlahci_alloc_resource(device_t dev, device_t child, int type, int *rid, 629195534Sscottl u_long start, u_long end, u_long count, u_int flags) 630195534Sscottl{ 631195534Sscottl struct ahci_controller *ctlr = device_get_softc(dev); 632195534Sscottl int unit = ((struct ahci_channel *)device_get_softc(child))->unit; 633195534Sscottl struct resource *res = NULL; 634195534Sscottl int offset = AHCI_OFFSET + (unit << 7); 635195534Sscottl long st; 636195534Sscottl 637195534Sscottl switch (type) { 638195534Sscottl case SYS_RES_MEMORY: 639195534Sscottl st = rman_get_start(ctlr->r_mem); 640195534Sscottl res = rman_reserve_resource(&ctlr->sc_iomem, st + offset, 641195534Sscottl st + offset + 127, 128, RF_ACTIVE, child); 642195534Sscottl if (res) { 643195534Sscottl bus_space_handle_t bsh; 644195534Sscottl bus_space_tag_t bst; 645195534Sscottl bsh = rman_get_bushandle(ctlr->r_mem); 646195534Sscottl bst = rman_get_bustag(ctlr->r_mem); 647195534Sscottl bus_space_subregion(bst, bsh, offset, 128, &bsh); 648195534Sscottl rman_set_bushandle(res, bsh); 649195534Sscottl rman_set_bustag(res, bst); 650195534Sscottl } 651195534Sscottl break; 652195534Sscottl case SYS_RES_IRQ: 653195534Sscottl if (*rid == ATA_IRQ_RID) 654195534Sscottl res = ctlr->irqs[0].r_irq; 655195534Sscottl break; 656195534Sscottl } 657195534Sscottl return (res); 658195534Sscottl} 659195534Sscottl 660195534Sscottlstatic int 661195534Sscottlahci_release_resource(device_t dev, device_t child, int type, int rid, 662195534Sscottl struct resource *r) 663195534Sscottl{ 664195534Sscottl 665195534Sscottl switch (type) { 666195534Sscottl case SYS_RES_MEMORY: 667195534Sscottl rman_release_resource(r); 668195534Sscottl return (0); 669195534Sscottl case SYS_RES_IRQ: 670195534Sscottl if (rid != ATA_IRQ_RID) 671195534Sscottl return ENOENT; 672195534Sscottl return (0); 673195534Sscottl } 674195534Sscottl return (EINVAL); 675195534Sscottl} 676195534Sscottl 677195534Sscottlstatic int 678195534Sscottlahci_setup_intr(device_t dev, device_t child, struct resource *irq, 679195534Sscottl int flags, driver_filter_t *filter, driver_intr_t *function, 680195534Sscottl void *argument, void **cookiep) 681195534Sscottl{ 682195534Sscottl struct ahci_controller *ctlr = device_get_softc(dev); 683195534Sscottl int unit = (intptr_t)device_get_ivars(child); 684195534Sscottl 685195534Sscottl if (filter != NULL) { 686195534Sscottl printf("ahci.c: we cannot use a filter here\n"); 687195534Sscottl return (EINVAL); 688195534Sscottl } 689195534Sscottl ctlr->interrupt[unit].function = function; 690195534Sscottl ctlr->interrupt[unit].argument = argument; 691195534Sscottl return (0); 692195534Sscottl} 693195534Sscottl 694195534Sscottlstatic int 695195534Sscottlahci_teardown_intr(device_t dev, device_t child, struct resource *irq, 696195534Sscottl void *cookie) 697195534Sscottl{ 698195534Sscottl struct ahci_controller *ctlr = device_get_softc(dev); 699195534Sscottl int unit = (intptr_t)device_get_ivars(child); 700195534Sscottl 701195534Sscottl ctlr->interrupt[unit].function = NULL; 702195534Sscottl ctlr->interrupt[unit].argument = NULL; 703195534Sscottl return (0); 704195534Sscottl} 705195534Sscottl 706195534Sscottlstatic int 707195534Sscottlahci_print_child(device_t dev, device_t child) 708195534Sscottl{ 709195534Sscottl int retval; 710195534Sscottl 711195534Sscottl retval = bus_print_child_header(dev, child); 712195534Sscottl retval += printf(" at channel %d", 713195534Sscottl (int)(intptr_t)device_get_ivars(child)); 714195534Sscottl retval += bus_print_child_footer(dev, child); 715195534Sscottl 716195534Sscottl return (retval); 717195534Sscottl} 718195534Sscottl 719195534Sscottldevclass_t ahci_devclass; 720195534Sscottlstatic device_method_t ahci_methods[] = { 721195534Sscottl DEVMETHOD(device_probe, ahci_probe), 722195534Sscottl DEVMETHOD(device_attach, ahci_attach), 723195534Sscottl DEVMETHOD(device_detach, ahci_detach), 724195534Sscottl DEVMETHOD(device_suspend, ahci_suspend), 725195534Sscottl DEVMETHOD(device_resume, ahci_resume), 726195534Sscottl DEVMETHOD(bus_print_child, ahci_print_child), 727195534Sscottl DEVMETHOD(bus_alloc_resource, ahci_alloc_resource), 728195534Sscottl DEVMETHOD(bus_release_resource, ahci_release_resource), 729195534Sscottl DEVMETHOD(bus_setup_intr, ahci_setup_intr), 730195534Sscottl DEVMETHOD(bus_teardown_intr,ahci_teardown_intr), 731195534Sscottl { 0, 0 } 732195534Sscottl}; 733195534Sscottlstatic driver_t ahci_driver = { 734195534Sscottl "ahci", 735195534Sscottl ahci_methods, 736195534Sscottl sizeof(struct ahci_controller) 737195534Sscottl}; 738195534SscottlDRIVER_MODULE(ahci, pci, ahci_driver, ahci_devclass, 0, 0); 739199322Smavstatic device_method_t ahci_ata_methods[] = { 740199322Smav DEVMETHOD(device_probe, ahci_ata_probe), 741199322Smav DEVMETHOD(device_attach, ahci_attach), 742199322Smav DEVMETHOD(device_detach, ahci_detach), 743199322Smav DEVMETHOD(device_suspend, ahci_suspend), 744199322Smav DEVMETHOD(device_resume, ahci_resume), 745199322Smav DEVMETHOD(bus_print_child, ahci_print_child), 746199322Smav DEVMETHOD(bus_alloc_resource, ahci_alloc_resource), 747199322Smav DEVMETHOD(bus_release_resource, ahci_release_resource), 748199322Smav DEVMETHOD(bus_setup_intr, ahci_setup_intr), 749199322Smav DEVMETHOD(bus_teardown_intr,ahci_teardown_intr), 750199322Smav { 0, 0 } 751199322Smav}; 752199322Smavstatic driver_t ahci_ata_driver = { 753199322Smav "ahci", 754199322Smav ahci_ata_methods, 755199322Smav sizeof(struct ahci_controller) 756199322Smav}; 757199322SmavDRIVER_MODULE(ahci, atapci, ahci_ata_driver, ahci_devclass, 0, 0); 758195534SscottlMODULE_VERSION(ahci, 1); 759195534SscottlMODULE_DEPEND(ahci, cam, 1, 1, 1); 760195534Sscottl 761195534Sscottlstatic int 762195534Sscottlahci_ch_probe(device_t dev) 763195534Sscottl{ 764195534Sscottl 765195534Sscottl device_set_desc_copy(dev, "AHCI channel"); 766195534Sscottl return (0); 767195534Sscottl} 768195534Sscottl 769195534Sscottlstatic int 770195534Sscottlahci_ch_attach(device_t dev) 771195534Sscottl{ 772195534Sscottl struct ahci_controller *ctlr = device_get_softc(device_get_parent(dev)); 773195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 774195534Sscottl struct cam_devq *devq; 775195534Sscottl int rid, error; 776195534Sscottl 777195534Sscottl ch->dev = dev; 778195534Sscottl ch->unit = (intptr_t)device_get_ivars(dev); 779196656Smav ch->caps = ctlr->caps; 780196656Smav ch->caps2 = ctlr->caps2; 781199322Smav ch->quirks = ctlr->quirks; 782195534Sscottl ch->numslots = ((ch->caps & AHCI_CAP_NCS) >> AHCI_CAP_NCS_SHIFT) + 1, 783196656Smav mtx_init(&ch->mtx, "AHCI channel lock", NULL, MTX_DEF); 784195534Sscottl resource_int_value(device_get_name(dev), 785195534Sscottl device_get_unit(dev), "pm_level", &ch->pm_level); 786196656Smav if (ch->pm_level > 3) 787196656Smav callout_init_mtx(&ch->pm_timer, &ch->mtx, 0); 788195534Sscottl /* Limit speed for my onboard JMicron external port. 789195534Sscottl * It is not eSATA really. */ 790195534Sscottl if (pci_get_devid(ctlr->dev) == 0x2363197b && 791195534Sscottl pci_get_subvendor(ctlr->dev) == 0x1043 && 792195534Sscottl pci_get_subdevice(ctlr->dev) == 0x81e4 && 793195534Sscottl ch->unit == 0) 794195534Sscottl ch->sata_rev = 1; 795195534Sscottl resource_int_value(device_get_name(dev), 796195534Sscottl device_get_unit(dev), "sata_rev", &ch->sata_rev); 797195534Sscottl rid = ch->unit; 798195534Sscottl if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 799195534Sscottl &rid, RF_ACTIVE))) 800195534Sscottl return (ENXIO); 801195534Sscottl ahci_dmainit(dev); 802195534Sscottl ahci_slotsalloc(dev); 803195534Sscottl ahci_ch_resume(dev); 804195534Sscottl mtx_lock(&ch->mtx); 805195534Sscottl rid = ATA_IRQ_RID; 806195534Sscottl if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 807195534Sscottl &rid, RF_SHAREABLE | RF_ACTIVE))) { 808195534Sscottl bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 809195534Sscottl device_printf(dev, "Unable to map interrupt\n"); 810195534Sscottl return (ENXIO); 811195534Sscottl } 812195534Sscottl if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL, 813195534Sscottl ahci_ch_intr_locked, dev, &ch->ih))) { 814195534Sscottl device_printf(dev, "Unable to setup interrupt\n"); 815195534Sscottl error = ENXIO; 816195534Sscottl goto err1; 817195534Sscottl } 818195534Sscottl /* Create the device queue for our SIM. */ 819195534Sscottl devq = cam_simq_alloc(ch->numslots); 820195534Sscottl if (devq == NULL) { 821195534Sscottl device_printf(dev, "Unable to allocate simq\n"); 822195534Sscottl error = ENOMEM; 823195534Sscottl goto err1; 824195534Sscottl } 825195534Sscottl /* Construct SIM entry */ 826195534Sscottl ch->sim = cam_sim_alloc(ahciaction, ahcipoll, "ahcich", ch, 827199178Smav device_get_unit(dev), &ch->mtx, 828199278Smav min(2, ch->numslots), 829199278Smav (ch->caps & AHCI_CAP_SNCQ) ? ch->numslots : 0, 830199278Smav devq); 831195534Sscottl if (ch->sim == NULL) { 832195534Sscottl device_printf(dev, "unable to allocate sim\n"); 833195534Sscottl error = ENOMEM; 834195534Sscottl goto err2; 835195534Sscottl } 836195534Sscottl if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) { 837195534Sscottl device_printf(dev, "unable to register xpt bus\n"); 838195534Sscottl error = ENXIO; 839195534Sscottl goto err2; 840195534Sscottl } 841195534Sscottl if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim), 842195534Sscottl CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 843195534Sscottl device_printf(dev, "unable to create path\n"); 844195534Sscottl error = ENXIO; 845195534Sscottl goto err3; 846195534Sscottl } 847196656Smav if (ch->pm_level > 3) { 848196656Smav callout_reset(&ch->pm_timer, 849196656Smav (ch->pm_level == 4) ? hz / 1000 : hz / 8, 850196656Smav ahci_ch_pm, dev); 851196656Smav } 852195534Sscottl mtx_unlock(&ch->mtx); 853195534Sscottl return (0); 854195534Sscottl 855195534Sscottlerr3: 856195534Sscottl xpt_bus_deregister(cam_sim_path(ch->sim)); 857195534Sscottlerr2: 858195534Sscottl cam_sim_free(ch->sim, /*free_devq*/TRUE); 859195534Sscottlerr1: 860195534Sscottl bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 861195534Sscottl bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 862195534Sscottl mtx_unlock(&ch->mtx); 863195534Sscottl return (error); 864195534Sscottl} 865195534Sscottl 866195534Sscottlstatic int 867195534Sscottlahci_ch_detach(device_t dev) 868195534Sscottl{ 869195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 870195534Sscottl 871195534Sscottl mtx_lock(&ch->mtx); 872195534Sscottl xpt_async(AC_LOST_DEVICE, ch->path, NULL); 873195534Sscottl xpt_free_path(ch->path); 874195534Sscottl xpt_bus_deregister(cam_sim_path(ch->sim)); 875195534Sscottl cam_sim_free(ch->sim, /*free_devq*/TRUE); 876195534Sscottl mtx_unlock(&ch->mtx); 877195534Sscottl 878196656Smav if (ch->pm_level > 3) 879196656Smav callout_drain(&ch->pm_timer); 880195534Sscottl bus_teardown_intr(dev, ch->r_irq, ch->ih); 881195534Sscottl bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 882195534Sscottl 883195534Sscottl ahci_ch_suspend(dev); 884195534Sscottl ahci_slotsfree(dev); 885195534Sscottl ahci_dmafini(dev); 886195534Sscottl 887195534Sscottl bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 888195534Sscottl mtx_destroy(&ch->mtx); 889195534Sscottl return (0); 890195534Sscottl} 891195534Sscottl 892195534Sscottlstatic int 893195534Sscottlahci_ch_suspend(device_t dev) 894195534Sscottl{ 895195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 896195534Sscottl 897195534Sscottl /* Disable port interrupts. */ 898195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_IE, 0); 899195534Sscottl /* Reset command register. */ 900195534Sscottl ahci_stop(dev); 901195534Sscottl ahci_stop_fr(dev); 902195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CMD, 0); 903195534Sscottl /* Allow everything, including partial and slumber modes. */ 904195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_SCTL, 0); 905195534Sscottl /* Request slumber mode transition and give some time to get there. */ 906195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CMD, AHCI_P_CMD_SLUMBER); 907195534Sscottl DELAY(100); 908195534Sscottl /* Disable PHY. */ 909195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_SCTL, ATA_SC_DET_DISABLE); 910195534Sscottl return (0); 911195534Sscottl} 912195534Sscottl 913195534Sscottlstatic int 914195534Sscottlahci_ch_resume(device_t dev) 915195534Sscottl{ 916195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 917195534Sscottl uint64_t work; 918195534Sscottl 919195534Sscottl /* Disable port interrupts */ 920195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_IE, 0); 921195534Sscottl /* Setup work areas */ 922195534Sscottl work = ch->dma.work_bus + AHCI_CL_OFFSET; 923195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CLB, work & 0xffffffff); 924195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CLBU, work >> 32); 925195534Sscottl work = ch->dma.rfis_bus; 926195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_FB, work & 0xffffffff); 927195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_FBU, work >> 32); 928195534Sscottl /* Activate the channel and power/spin up device */ 929195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CMD, 930195534Sscottl (AHCI_P_CMD_ACTIVE | AHCI_P_CMD_POD | AHCI_P_CMD_SUD | 931196656Smav ((ch->pm_level == 2 || ch->pm_level == 3) ? AHCI_P_CMD_ALPE : 0) | 932195534Sscottl ((ch->pm_level > 2) ? AHCI_P_CMD_ASP : 0 ))); 933195534Sscottl ahci_start_fr(dev); 934195534Sscottl ahci_start(dev); 935195534Sscottl return (0); 936195534Sscottl} 937195534Sscottl 938195534Sscottldevclass_t ahcich_devclass; 939195534Sscottlstatic device_method_t ahcich_methods[] = { 940195534Sscottl DEVMETHOD(device_probe, ahci_ch_probe), 941195534Sscottl DEVMETHOD(device_attach, ahci_ch_attach), 942195534Sscottl DEVMETHOD(device_detach, ahci_ch_detach), 943195534Sscottl DEVMETHOD(device_suspend, ahci_ch_suspend), 944195534Sscottl DEVMETHOD(device_resume, ahci_ch_resume), 945195534Sscottl { 0, 0 } 946195534Sscottl}; 947195534Sscottlstatic driver_t ahcich_driver = { 948195534Sscottl "ahcich", 949195534Sscottl ahcich_methods, 950195534Sscottl sizeof(struct ahci_channel) 951195534Sscottl}; 952199322SmavDRIVER_MODULE(ahcich, ahci, ahcich_driver, ahcich_devclass, 0, 0); 953195534Sscottl 954195534Sscottlstruct ahci_dc_cb_args { 955195534Sscottl bus_addr_t maddr; 956195534Sscottl int error; 957195534Sscottl}; 958195534Sscottl 959195534Sscottlstatic void 960195534Sscottlahci_dmainit(device_t dev) 961195534Sscottl{ 962195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 963195534Sscottl struct ahci_dc_cb_args dcba; 964195534Sscottl 965195534Sscottl if (ch->caps & AHCI_CAP_64BIT) 966195534Sscottl ch->dma.max_address = BUS_SPACE_MAXADDR; 967195534Sscottl else 968195534Sscottl ch->dma.max_address = BUS_SPACE_MAXADDR_32BIT; 969195534Sscottl /* Command area. */ 970195534Sscottl if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0, 971195534Sscottl ch->dma.max_address, BUS_SPACE_MAXADDR, 972195534Sscottl NULL, NULL, AHCI_WORK_SIZE, 1, AHCI_WORK_SIZE, 973195534Sscottl 0, NULL, NULL, &ch->dma.work_tag)) 974195534Sscottl goto error; 975195534Sscottl if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0, 976195534Sscottl &ch->dma.work_map)) 977195534Sscottl goto error; 978195534Sscottl if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work, 979195534Sscottl AHCI_WORK_SIZE, ahci_dmasetupc_cb, &dcba, 0) || dcba.error) { 980195534Sscottl bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map); 981195534Sscottl goto error; 982195534Sscottl } 983195534Sscottl ch->dma.work_bus = dcba.maddr; 984195534Sscottl /* FIS receive area. */ 985195534Sscottl if (bus_dma_tag_create(bus_get_dma_tag(dev), 4096, 0, 986195534Sscottl ch->dma.max_address, BUS_SPACE_MAXADDR, 987195534Sscottl NULL, NULL, 4096, 1, 4096, 988195534Sscottl 0, NULL, NULL, &ch->dma.rfis_tag)) 989195534Sscottl goto error; 990195534Sscottl if (bus_dmamem_alloc(ch->dma.rfis_tag, (void **)&ch->dma.rfis, 0, 991195534Sscottl &ch->dma.rfis_map)) 992195534Sscottl goto error; 993195534Sscottl if (bus_dmamap_load(ch->dma.rfis_tag, ch->dma.rfis_map, ch->dma.rfis, 994195534Sscottl 4096, ahci_dmasetupc_cb, &dcba, 0) || dcba.error) { 995195534Sscottl bus_dmamem_free(ch->dma.rfis_tag, ch->dma.rfis, ch->dma.rfis_map); 996195534Sscottl goto error; 997195534Sscottl } 998195534Sscottl ch->dma.rfis_bus = dcba.maddr; 999195534Sscottl /* Data area. */ 1000195534Sscottl if (bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0, 1001195534Sscottl ch->dma.max_address, BUS_SPACE_MAXADDR, 1002195534Sscottl NULL, NULL, 1003195534Sscottl AHCI_SG_ENTRIES * PAGE_SIZE * ch->numslots, 1004195534Sscottl AHCI_SG_ENTRIES, AHCI_PRD_MAX, 1005195534Sscottl 0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) { 1006195534Sscottl goto error; 1007195534Sscottl } 1008195534Sscottl return; 1009195534Sscottl 1010195534Sscottlerror: 1011195534Sscottl device_printf(dev, "WARNING - DMA initialization failed\n"); 1012195534Sscottl ahci_dmafini(dev); 1013195534Sscottl} 1014195534Sscottl 1015195534Sscottlstatic void 1016195534Sscottlahci_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 1017195534Sscottl{ 1018195534Sscottl struct ahci_dc_cb_args *dcba = (struct ahci_dc_cb_args *)xsc; 1019195534Sscottl 1020195534Sscottl if (!(dcba->error = error)) 1021195534Sscottl dcba->maddr = segs[0].ds_addr; 1022195534Sscottl} 1023195534Sscottl 1024195534Sscottlstatic void 1025195534Sscottlahci_dmafini(device_t dev) 1026195534Sscottl{ 1027195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1028195534Sscottl 1029195534Sscottl if (ch->dma.data_tag) { 1030195534Sscottl bus_dma_tag_destroy(ch->dma.data_tag); 1031195534Sscottl ch->dma.data_tag = NULL; 1032195534Sscottl } 1033195534Sscottl if (ch->dma.rfis_bus) { 1034195534Sscottl bus_dmamap_unload(ch->dma.rfis_tag, ch->dma.rfis_map); 1035195534Sscottl bus_dmamem_free(ch->dma.rfis_tag, ch->dma.rfis, ch->dma.rfis_map); 1036195534Sscottl ch->dma.rfis_bus = 0; 1037195534Sscottl ch->dma.rfis_map = NULL; 1038195534Sscottl ch->dma.rfis = NULL; 1039195534Sscottl } 1040195534Sscottl if (ch->dma.work_bus) { 1041195534Sscottl bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map); 1042195534Sscottl bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map); 1043195534Sscottl ch->dma.work_bus = 0; 1044195534Sscottl ch->dma.work_map = NULL; 1045195534Sscottl ch->dma.work = NULL; 1046195534Sscottl } 1047195534Sscottl if (ch->dma.work_tag) { 1048195534Sscottl bus_dma_tag_destroy(ch->dma.work_tag); 1049195534Sscottl ch->dma.work_tag = NULL; 1050195534Sscottl } 1051195534Sscottl} 1052195534Sscottl 1053195534Sscottlstatic void 1054195534Sscottlahci_slotsalloc(device_t dev) 1055195534Sscottl{ 1056195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1057195534Sscottl int i; 1058195534Sscottl 1059195534Sscottl /* Alloc and setup command/dma slots */ 1060195534Sscottl bzero(ch->slot, sizeof(ch->slot)); 1061195534Sscottl for (i = 0; i < ch->numslots; i++) { 1062195534Sscottl struct ahci_slot *slot = &ch->slot[i]; 1063195534Sscottl 1064195534Sscottl slot->dev = dev; 1065195534Sscottl slot->slot = i; 1066195534Sscottl slot->state = AHCI_SLOT_EMPTY; 1067195534Sscottl slot->ccb = NULL; 1068195534Sscottl callout_init_mtx(&slot->timeout, &ch->mtx, 0); 1069195534Sscottl 1070195534Sscottl if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map)) 1071195534Sscottl device_printf(ch->dev, "FAILURE - create data_map\n"); 1072195534Sscottl } 1073195534Sscottl} 1074195534Sscottl 1075195534Sscottlstatic void 1076195534Sscottlahci_slotsfree(device_t dev) 1077195534Sscottl{ 1078195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1079195534Sscottl int i; 1080195534Sscottl 1081195534Sscottl /* Free all dma slots */ 1082195534Sscottl for (i = 0; i < ch->numslots; i++) { 1083195534Sscottl struct ahci_slot *slot = &ch->slot[i]; 1084195534Sscottl 1085196656Smav callout_drain(&slot->timeout); 1086195534Sscottl if (slot->dma.data_map) { 1087195534Sscottl bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map); 1088195534Sscottl slot->dma.data_map = NULL; 1089195534Sscottl } 1090195534Sscottl } 1091195534Sscottl} 1092195534Sscottl 1093195534Sscottlstatic void 1094198319Smavahci_phy_check_events(device_t dev, u_int32_t serr) 1095195534Sscottl{ 1096195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1097195534Sscottl 1098198319Smav if ((serr & ATA_SE_PHY_CHANGED) && (ch->pm_level == 0)) { 1099195534Sscottl u_int32_t status = ATA_INL(ch->r_mem, AHCI_P_SSTS); 1100195534Sscottl if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) && 1101195534Sscottl ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) && 1102195534Sscottl ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) { 1103195534Sscottl if (bootverbose) 1104195534Sscottl device_printf(dev, "CONNECT requested\n"); 1105195534Sscottl ahci_reset(dev); 1106195534Sscottl } else { 1107195534Sscottl if (bootverbose) 1108195534Sscottl device_printf(dev, "DISCONNECT requested\n"); 1109195534Sscottl ch->devices = 0; 1110195534Sscottl } 1111195534Sscottl } 1112195534Sscottl} 1113195534Sscottl 1114195534Sscottlstatic void 1115196907Smavahci_notify_events(device_t dev, u_int32_t status) 1116196656Smav{ 1117196656Smav struct ahci_channel *ch = device_get_softc(dev); 1118196656Smav struct cam_path *dpath; 1119196656Smav int i; 1120196656Smav 1121196656Smav ATA_OUTL(ch->r_mem, AHCI_P_SNTF, status); 1122196656Smav if (bootverbose) 1123196656Smav device_printf(dev, "SNTF 0x%04x\n", status); 1124196656Smav for (i = 0; i < 16; i++) { 1125196656Smav if ((status & (1 << i)) == 0) 1126196656Smav continue; 1127196656Smav if (xpt_create_path(&dpath, NULL, 1128196656Smav xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) { 1129196656Smav xpt_async(AC_SCSI_AEN, dpath, NULL); 1130196656Smav xpt_free_path(dpath); 1131196656Smav } 1132196656Smav } 1133196656Smav} 1134196656Smav 1135196656Smavstatic void 1136195534Sscottlahci_ch_intr_locked(void *data) 1137195534Sscottl{ 1138195534Sscottl device_t dev = (device_t)data; 1139195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1140195534Sscottl 1141195534Sscottl mtx_lock(&ch->mtx); 1142195534Sscottl ahci_ch_intr(data); 1143195534Sscottl mtx_unlock(&ch->mtx); 1144195534Sscottl} 1145195534Sscottl 1146195534Sscottlstatic void 1147196656Smavahci_ch_pm(void *arg) 1148196656Smav{ 1149196656Smav device_t dev = (device_t)arg; 1150196656Smav struct ahci_channel *ch = device_get_softc(dev); 1151196656Smav uint32_t work; 1152196656Smav 1153196656Smav if (ch->numrslots != 0) 1154196656Smav return; 1155196656Smav work = ATA_INL(ch->r_mem, AHCI_P_CMD); 1156196656Smav if (ch->pm_level == 4) 1157196656Smav work |= AHCI_P_CMD_PARTIAL; 1158196656Smav else 1159196656Smav work |= AHCI_P_CMD_SLUMBER; 1160196656Smav ATA_OUTL(ch->r_mem, AHCI_P_CMD, work); 1161196656Smav} 1162196656Smav 1163196656Smavstatic void 1164195534Sscottlahci_ch_intr(void *data) 1165195534Sscottl{ 1166195534Sscottl device_t dev = (device_t)data; 1167195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1168198319Smav uint32_t istatus, sstatus, cstatus, serr = 0, sntf = 0, ok, err; 1169195534Sscottl enum ahci_err_type et; 1170195534Sscottl int i, ccs, ncq_err = 0; 1171195534Sscottl 1172195534Sscottl /* Read and clear interrupt statuses. */ 1173195534Sscottl istatus = ATA_INL(ch->r_mem, AHCI_P_IS); 1174196656Smav if (istatus == 0) 1175196656Smav return; 1176195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_IS, istatus); 1177195534Sscottl /* Read command statuses. */ 1178196907Smav sstatus = ATA_INL(ch->r_mem, AHCI_P_SACT); 1179195534Sscottl cstatus = ATA_INL(ch->r_mem, AHCI_P_CI); 1180196907Smav if ((istatus & AHCI_P_IX_SDB) && (ch->caps & AHCI_CAP_SSNTF)) 1181196907Smav sntf = ATA_INL(ch->r_mem, AHCI_P_SNTF); 1182195534Sscottl /* Process PHY events */ 1183198319Smav if (istatus & (AHCI_P_IX_PC | AHCI_P_IX_PRC | AHCI_P_IX_OF | 1184198319Smav AHCI_P_IX_IF | AHCI_P_IX_HBD | AHCI_P_IX_HBF | AHCI_P_IX_TFE)) { 1185198319Smav serr = ATA_INL(ch->r_mem, AHCI_P_SERR); 1186198319Smav if (serr) { 1187198319Smav ATA_OUTL(ch->r_mem, AHCI_P_SERR, serr); 1188198319Smav ahci_phy_check_events(dev, serr); 1189198319Smav } 1190198319Smav } 1191195534Sscottl /* Process command errors */ 1192198319Smav if (istatus & (AHCI_P_IX_OF | AHCI_P_IX_IF | 1193198319Smav AHCI_P_IX_HBD | AHCI_P_IX_HBF | AHCI_P_IX_TFE)) { 1194195534Sscottl//device_printf(dev, "%s ERROR is %08x cs %08x ss %08x rs %08x tfd %02x serr %08x\n", 1195195534Sscottl// __func__, istatus, cstatus, sstatus, ch->rslots, ATA_INL(ch->r_mem, AHCI_P_TFD), 1196198319Smav// serr); 1197195534Sscottl ccs = (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CCS_MASK) 1198195534Sscottl >> AHCI_P_CMD_CCS_SHIFT; 1199196656Smav err = ch->rslots & (cstatus | sstatus); 1200195534Sscottl /* Kick controller into sane state */ 1201195534Sscottl ahci_stop(dev); 1202195534Sscottl ahci_start(dev); 1203195534Sscottl } else { 1204195534Sscottl ccs = 0; 1205195534Sscottl err = 0; 1206195534Sscottl } 1207195534Sscottl /* Complete all successfull commands. */ 1208196656Smav ok = ch->rslots & ~(cstatus | sstatus); 1209195534Sscottl for (i = 0; i < ch->numslots; i++) { 1210195534Sscottl if ((ok >> i) & 1) 1211195534Sscottl ahci_end_transaction(&ch->slot[i], AHCI_ERR_NONE); 1212195534Sscottl } 1213195534Sscottl /* On error, complete the rest of commands with error statuses. */ 1214195534Sscottl if (err) { 1215195534Sscottl if (ch->frozen) { 1216195534Sscottl union ccb *fccb = ch->frozen; 1217195534Sscottl ch->frozen = NULL; 1218195534Sscottl fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 1219198319Smav if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 1220198319Smav xpt_freeze_devq(fccb->ccb_h.path, 1); 1221198319Smav fccb->ccb_h.status |= CAM_DEV_QFRZN; 1222198319Smav } 1223195534Sscottl xpt_done(fccb); 1224195534Sscottl } 1225195534Sscottl for (i = 0; i < ch->numslots; i++) { 1226195534Sscottl /* XXX: reqests in loading state. */ 1227195534Sscottl if (((err >> i) & 1) == 0) 1228195534Sscottl continue; 1229198390Smav if (istatus & AHCI_P_IX_TFE) { 1230195534Sscottl /* Task File Error */ 1231195534Sscottl if (ch->numtslots == 0) { 1232195534Sscottl /* Untagged operation. */ 1233195534Sscottl if (i == ccs) 1234195534Sscottl et = AHCI_ERR_TFE; 1235195534Sscottl else 1236195534Sscottl et = AHCI_ERR_INNOCENT; 1237195534Sscottl } else { 1238195534Sscottl /* Tagged operation. */ 1239195534Sscottl et = AHCI_ERR_NCQ; 1240195534Sscottl ncq_err = 1; 1241195534Sscottl } 1242198390Smav } else if (istatus & AHCI_P_IX_IF) { 1243198390Smav if (ch->numtslots == 0 && i != ccs) 1244198390Smav et = AHCI_ERR_INNOCENT; 1245198390Smav else 1246198390Smav et = AHCI_ERR_SATA; 1247195534Sscottl } else 1248195534Sscottl et = AHCI_ERR_INVALID; 1249195534Sscottl ahci_end_transaction(&ch->slot[i], et); 1250195534Sscottl } 1251195534Sscottl if (ncq_err) 1252195534Sscottl ahci_issue_read_log(dev); 1253195534Sscottl } 1254196656Smav /* Process NOTIFY events */ 1255196907Smav if (sntf) 1256196907Smav ahci_notify_events(dev, sntf); 1257195534Sscottl} 1258195534Sscottl 1259195534Sscottl/* Must be called with channel locked. */ 1260195534Sscottlstatic int 1261195534Sscottlahci_check_collision(device_t dev, union ccb *ccb) 1262195534Sscottl{ 1263195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1264195534Sscottl 1265195534Sscottl if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1266195534Sscottl (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 1267195534Sscottl /* Tagged command while untagged are active. */ 1268195534Sscottl if (ch->numrslots != 0 && ch->numtslots == 0) 1269195534Sscottl return (1); 1270195534Sscottl /* Tagged command while tagged to other target is active. */ 1271195534Sscottl if (ch->numtslots != 0 && 1272195534Sscottl ch->taggedtarget != ccb->ccb_h.target_id) 1273195534Sscottl return (1); 1274195534Sscottl } else { 1275195534Sscottl /* Untagged command while tagged are active. */ 1276195534Sscottl if (ch->numrslots != 0 && ch->numtslots != 0) 1277195534Sscottl return (1); 1278195534Sscottl } 1279195534Sscottl if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1280195534Sscottl (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) { 1281195534Sscottl /* Atomic command while anything active. */ 1282195534Sscottl if (ch->numrslots != 0) 1283195534Sscottl return (1); 1284195534Sscottl } 1285195534Sscottl /* We have some atomic command running. */ 1286195534Sscottl if (ch->aslots != 0) 1287195534Sscottl return (1); 1288195534Sscottl return (0); 1289195534Sscottl} 1290195534Sscottl 1291195534Sscottl/* Must be called with channel locked. */ 1292195534Sscottlstatic void 1293195534Sscottlahci_begin_transaction(device_t dev, union ccb *ccb) 1294195534Sscottl{ 1295195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1296195534Sscottl struct ahci_slot *slot; 1297195534Sscottl int tag; 1298195534Sscottl 1299195534Sscottl /* Choose empty slot. */ 1300195534Sscottl tag = ch->lastslot; 1301196656Smav while (ch->slot[tag].state != AHCI_SLOT_EMPTY) { 1302196656Smav if (++tag >= ch->numslots) 1303195534Sscottl tag = 0; 1304196660Smav KASSERT(tag != ch->lastslot, ("ahci: ALL SLOTS BUSY!")); 1305196656Smav } 1306195534Sscottl ch->lastslot = tag; 1307195534Sscottl /* Occupy chosen slot. */ 1308195534Sscottl slot = &ch->slot[tag]; 1309195534Sscottl slot->ccb = ccb; 1310196656Smav /* Stop PM timer. */ 1311196656Smav if (ch->numrslots == 0 && ch->pm_level > 3) 1312196656Smav callout_stop(&ch->pm_timer); 1313195534Sscottl /* Update channel stats. */ 1314195534Sscottl ch->numrslots++; 1315195534Sscottl if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1316195534Sscottl (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 1317195534Sscottl ch->numtslots++; 1318195534Sscottl ch->taggedtarget = ccb->ccb_h.target_id; 1319195534Sscottl } 1320195534Sscottl if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1321195534Sscottl (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) 1322195534Sscottl ch->aslots |= (1 << slot->slot); 1323195534Sscottl slot->dma.nsegs = 0; 1324195534Sscottl /* If request moves data, setup and load SG list */ 1325195534Sscottl if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 1326195534Sscottl void *buf; 1327195534Sscottl bus_size_t size; 1328195534Sscottl 1329195534Sscottl slot->state = AHCI_SLOT_LOADING; 1330195534Sscottl if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1331195534Sscottl buf = ccb->ataio.data_ptr; 1332195534Sscottl size = ccb->ataio.dxfer_len; 1333195534Sscottl } else { 1334195534Sscottl buf = ccb->csio.data_ptr; 1335195534Sscottl size = ccb->csio.dxfer_len; 1336195534Sscottl } 1337195534Sscottl bus_dmamap_load(ch->dma.data_tag, slot->dma.data_map, 1338195534Sscottl buf, size, ahci_dmasetprd, slot, 0); 1339195534Sscottl } else 1340195534Sscottl ahci_execute_transaction(slot); 1341195534Sscottl} 1342195534Sscottl 1343195534Sscottl/* Locked by busdma engine. */ 1344195534Sscottlstatic void 1345195534Sscottlahci_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 1346195534Sscottl{ 1347195534Sscottl struct ahci_slot *slot = arg; 1348195534Sscottl struct ahci_channel *ch = device_get_softc(slot->dev); 1349195534Sscottl struct ahci_cmd_tab *ctp; 1350195534Sscottl struct ahci_dma_prd *prd; 1351195534Sscottl int i; 1352195534Sscottl 1353195534Sscottl if (error) { 1354195534Sscottl device_printf(slot->dev, "DMA load error\n"); 1355195534Sscottl ahci_end_transaction(slot, AHCI_ERR_INVALID); 1356195534Sscottl return; 1357195534Sscottl } 1358195534Sscottl KASSERT(nsegs <= AHCI_SG_ENTRIES, ("too many DMA segment entries\n")); 1359195534Sscottl /* Get a piece of the workspace for this request */ 1360195534Sscottl ctp = (struct ahci_cmd_tab *) 1361195534Sscottl (ch->dma.work + AHCI_CT_OFFSET + (AHCI_CT_SIZE * slot->slot)); 1362195534Sscottl /* Fill S/G table */ 1363195534Sscottl prd = &ctp->prd_tab[0]; 1364195534Sscottl for (i = 0; i < nsegs; i++) { 1365195534Sscottl prd[i].dba = htole64(segs[i].ds_addr); 1366195534Sscottl prd[i].dbc = htole32((segs[i].ds_len - 1) & AHCI_PRD_MASK); 1367195534Sscottl } 1368195534Sscottl slot->dma.nsegs = nsegs; 1369195534Sscottl bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 1370195534Sscottl ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ? 1371195534Sscottl BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE)); 1372195534Sscottl ahci_execute_transaction(slot); 1373195534Sscottl} 1374195534Sscottl 1375195534Sscottl/* Must be called with channel locked. */ 1376195534Sscottlstatic void 1377195534Sscottlahci_execute_transaction(struct ahci_slot *slot) 1378195534Sscottl{ 1379195534Sscottl device_t dev = slot->dev; 1380195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1381195534Sscottl struct ahci_cmd_tab *ctp; 1382195534Sscottl struct ahci_cmd_list *clp; 1383195534Sscottl union ccb *ccb = slot->ccb; 1384195534Sscottl int port = ccb->ccb_h.target_id & 0x0f; 1385195534Sscottl int fis_size; 1386195534Sscottl 1387195534Sscottl /* Get a piece of the workspace for this request */ 1388195534Sscottl ctp = (struct ahci_cmd_tab *) 1389195534Sscottl (ch->dma.work + AHCI_CT_OFFSET + (AHCI_CT_SIZE * slot->slot)); 1390195534Sscottl /* Setup the FIS for this request */ 1391195534Sscottl if (!(fis_size = ahci_setup_fis(ctp, ccb, slot->slot))) { 1392195534Sscottl device_printf(ch->dev, "Setting up SATA FIS failed\n"); 1393195534Sscottl ahci_end_transaction(slot, AHCI_ERR_INVALID); 1394195534Sscottl return; 1395195534Sscottl } 1396195534Sscottl /* Setup the command list entry */ 1397195534Sscottl clp = (struct ahci_cmd_list *) 1398195534Sscottl (ch->dma.work + AHCI_CL_OFFSET + (AHCI_CL_SIZE * slot->slot)); 1399195534Sscottl clp->prd_length = slot->dma.nsegs; 1400195534Sscottl clp->cmd_flags = (ccb->ccb_h.flags & CAM_DIR_OUT ? AHCI_CMD_WRITE : 0) | 1401195534Sscottl (ccb->ccb_h.func_code == XPT_SCSI_IO ? 1402195534Sscottl (AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH) : 0) | 1403195534Sscottl (fis_size / sizeof(u_int32_t)) | 1404195534Sscottl (port << 12); 1405195534Sscottl /* Special handling for Soft Reset command. */ 1406195534Sscottl if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1407195534Sscottl (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) && 1408195534Sscottl (ccb->ataio.cmd.control & ATA_A_RESET)) { 1409195534Sscottl /* Kick controller into sane state */ 1410195534Sscottl ahci_stop(dev); 1411195534Sscottl ahci_clo(dev); 1412195534Sscottl ahci_start(dev); 1413195534Sscottl clp->cmd_flags |= AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY; 1414195534Sscottl } 1415195534Sscottl clp->bytecount = 0; 1416195534Sscottl clp->cmd_table_phys = htole64(ch->dma.work_bus + AHCI_CT_OFFSET + 1417195534Sscottl (AHCI_CT_SIZE * slot->slot)); 1418195534Sscottl bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map, 1419195534Sscottl BUS_DMASYNC_PREWRITE); 1420195534Sscottl bus_dmamap_sync(ch->dma.rfis_tag, ch->dma.rfis_map, 1421195534Sscottl BUS_DMASYNC_PREREAD); 1422195534Sscottl /* Set ACTIVE bit for NCQ commands. */ 1423195534Sscottl if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1424195534Sscottl (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 1425195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_SACT, 1 << slot->slot); 1426195534Sscottl } 1427195534Sscottl /* Issue command to the controller. */ 1428195534Sscottl slot->state = AHCI_SLOT_RUNNING; 1429195534Sscottl ch->rslots |= (1 << slot->slot); 1430195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CI, (1 << slot->slot)); 1431195534Sscottl /* Device reset commands doesn't interrupt. Poll them. */ 1432195534Sscottl if (ccb->ccb_h.func_code == XPT_ATA_IO && 1433195534Sscottl (ccb->ataio.cmd.command == ATA_DEVICE_RESET || 1434195534Sscottl (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL))) { 1435195534Sscottl int count, timeout = ccb->ccb_h.timeout; 1436195534Sscottl enum ahci_err_type et = AHCI_ERR_NONE; 1437195534Sscottl 1438195534Sscottl for (count = 0; count < timeout; count++) { 1439195534Sscottl DELAY(1000); 1440195534Sscottl if (!(ATA_INL(ch->r_mem, AHCI_P_CI) & (1 << slot->slot))) 1441195534Sscottl break; 1442195534Sscottl if (ATA_INL(ch->r_mem, AHCI_P_TFD) & ATA_S_ERROR) { 1443195534Sscottl device_printf(ch->dev, 1444195534Sscottl "Poll error on slot %d, TFD: %04x\n", 1445195534Sscottl slot->slot, ATA_INL(ch->r_mem, AHCI_P_TFD)); 1446195534Sscottl et = AHCI_ERR_TFE; 1447195534Sscottl break; 1448195534Sscottl } 1449198851Smav /* Workaround for ATI SB600/SB700 chipsets. */ 1450198851Smav if (ccb->ccb_h.target_id == 15 && 1451198851Smav pci_get_vendor(device_get_parent(dev)) == 0x1002 && 1452198851Smav (ATA_INL(ch->r_mem, AHCI_P_IS) & AHCI_P_IX_IPM)) { 1453198851Smav et = AHCI_ERR_TIMEOUT; 1454198851Smav break; 1455198851Smav } 1456195534Sscottl } 1457195534Sscottl if (timeout && (count >= timeout)) { 1458195534Sscottl device_printf(ch->dev, 1459195534Sscottl "Poll timeout on slot %d\n", slot->slot); 1460195534Sscottl et = AHCI_ERR_TIMEOUT; 1461195534Sscottl } 1462195534Sscottl if (et != AHCI_ERR_NONE) { 1463195534Sscottl /* Kick controller into sane state */ 1464195534Sscottl ahci_stop(ch->dev); 1465195534Sscottl ahci_start(ch->dev); 1466195534Sscottl } 1467195534Sscottl ahci_end_transaction(slot, et); 1468195534Sscottl return; 1469195534Sscottl } 1470195534Sscottl /* Start command execution timeout */ 1471198319Smav callout_reset(&slot->timeout, (int)ccb->ccb_h.timeout * hz / 2000, 1472195534Sscottl (timeout_t*)ahci_timeout, slot); 1473195534Sscottl return; 1474195534Sscottl} 1475195534Sscottl 1476195534Sscottl/* Locked by callout mechanism. */ 1477195534Sscottlstatic void 1478195534Sscottlahci_timeout(struct ahci_slot *slot) 1479195534Sscottl{ 1480195534Sscottl device_t dev = slot->dev; 1481195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1482198319Smav uint32_t sstatus; 1483198319Smav int ccs; 1484195534Sscottl int i; 1485195534Sscottl 1486196656Smav /* Check for stale timeout. */ 1487198319Smav if (slot->state < AHCI_SLOT_RUNNING) 1488196656Smav return; 1489196656Smav 1490198319Smav /* Check if slot was not being executed last time we checked. */ 1491198319Smav if (slot->state < AHCI_SLOT_EXECUTING) { 1492198319Smav /* Check if slot started executing. */ 1493198319Smav sstatus = ATA_INL(ch->r_mem, AHCI_P_SACT); 1494198319Smav ccs = (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CCS_MASK) 1495198319Smav >> AHCI_P_CMD_CCS_SHIFT; 1496198319Smav if ((sstatus & (1 << slot->slot)) != 0 || ccs == slot->slot) 1497198319Smav slot->state = AHCI_SLOT_EXECUTING; 1498198319Smav 1499198319Smav callout_reset(&slot->timeout, 1500198319Smav (int)slot->ccb->ccb_h.timeout * hz / 2000, 1501198319Smav (timeout_t*)ahci_timeout, slot); 1502198319Smav return; 1503198319Smav } 1504198319Smav 1505195534Sscottl device_printf(dev, "Timeout on slot %d\n", slot->slot); 1506198319Smav device_printf(dev, "is %08x cs %08x ss %08x rs %08x tfd %02x serr %08x\n", 1507198319Smav ATA_INL(ch->r_mem, AHCI_P_IS), ATA_INL(ch->r_mem, AHCI_P_CI), 1508198319Smav ATA_INL(ch->r_mem, AHCI_P_SACT), ch->rslots, 1509198319Smav ATA_INL(ch->r_mem, AHCI_P_TFD), ATA_INL(ch->r_mem, AHCI_P_SERR)); 1510195534Sscottl 1511198851Smav ch->fatalerr = 1; 1512197838Smav /* Handle frozen command. */ 1513195534Sscottl if (ch->frozen) { 1514195534Sscottl union ccb *fccb = ch->frozen; 1515195534Sscottl ch->frozen = NULL; 1516195534Sscottl fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 1517198319Smav if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 1518198319Smav xpt_freeze_devq(fccb->ccb_h.path, 1); 1519198319Smav fccb->ccb_h.status |= CAM_DEV_QFRZN; 1520198319Smav } 1521195534Sscottl xpt_done(fccb); 1522195534Sscottl } 1523197838Smav /* Handle command with timeout. */ 1524197838Smav ahci_end_transaction(&ch->slot[slot->slot], AHCI_ERR_TIMEOUT); 1525197838Smav /* Handle the rest of commands. */ 1526195534Sscottl for (i = 0; i < ch->numslots; i++) { 1527195534Sscottl /* Do we have a running request on slot? */ 1528195534Sscottl if (ch->slot[i].state < AHCI_SLOT_RUNNING) 1529195534Sscottl continue; 1530195534Sscottl ahci_end_transaction(&ch->slot[i], AHCI_ERR_INNOCENT); 1531195534Sscottl } 1532195534Sscottl} 1533195534Sscottl 1534195534Sscottl/* Must be called with channel locked. */ 1535195534Sscottlstatic void 1536195534Sscottlahci_end_transaction(struct ahci_slot *slot, enum ahci_err_type et) 1537195534Sscottl{ 1538195534Sscottl device_t dev = slot->dev; 1539195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1540195534Sscottl union ccb *ccb = slot->ccb; 1541195534Sscottl 1542195534Sscottl bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map, 1543195534Sscottl BUS_DMASYNC_POSTWRITE); 1544195534Sscottl /* Read result registers to the result struct 1545195534Sscottl * May be incorrect if several commands finished same time, 1546195534Sscottl * so read only when sure or have to. 1547195534Sscottl */ 1548195534Sscottl if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1549195534Sscottl struct ata_res *res = &ccb->ataio.res; 1550195534Sscottl 1551195534Sscottl if ((et == AHCI_ERR_TFE) || 1552195534Sscottl (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) { 1553195534Sscottl u_int8_t *fis = ch->dma.rfis + 0x40; 1554195534Sscottl uint16_t tfd = ATA_INL(ch->r_mem, AHCI_P_TFD); 1555195534Sscottl 1556195534Sscottl bus_dmamap_sync(ch->dma.rfis_tag, ch->dma.rfis_map, 1557195534Sscottl BUS_DMASYNC_POSTREAD); 1558195534Sscottl res->status = tfd; 1559195534Sscottl res->error = tfd >> 8; 1560195534Sscottl res->lba_low = fis[4]; 1561195534Sscottl res->lba_mid = fis[5]; 1562195534Sscottl res->lba_high = fis[6]; 1563195534Sscottl res->device = fis[7]; 1564195534Sscottl res->lba_low_exp = fis[8]; 1565195534Sscottl res->lba_mid_exp = fis[9]; 1566195534Sscottl res->lba_high_exp = fis[10]; 1567195534Sscottl res->sector_count = fis[12]; 1568195534Sscottl res->sector_count_exp = fis[13]; 1569195534Sscottl } else 1570195534Sscottl bzero(res, sizeof(*res)); 1571195534Sscottl } 1572195534Sscottl if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 1573195534Sscottl bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 1574195534Sscottl (ccb->ccb_h.flags & CAM_DIR_IN) ? 1575195534Sscottl BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 1576195534Sscottl bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map); 1577195534Sscottl } 1578198319Smav /* In case of error, freeze device for proper recovery. */ 1579198319Smav if ((et != AHCI_ERR_NONE) && (!ch->readlog) && 1580198319Smav !(ccb->ccb_h.status & CAM_DEV_QFRZN)) { 1581198319Smav xpt_freeze_devq(ccb->ccb_h.path, 1); 1582198319Smav ccb->ccb_h.status |= CAM_DEV_QFRZN; 1583198319Smav } 1584195534Sscottl /* Set proper result status. */ 1585195534Sscottl ccb->ccb_h.status &= ~CAM_STATUS_MASK; 1586195534Sscottl switch (et) { 1587195534Sscottl case AHCI_ERR_NONE: 1588195534Sscottl ccb->ccb_h.status |= CAM_REQ_CMP; 1589195534Sscottl if (ccb->ccb_h.func_code == XPT_SCSI_IO) 1590195534Sscottl ccb->csio.scsi_status = SCSI_STATUS_OK; 1591195534Sscottl break; 1592195534Sscottl case AHCI_ERR_INVALID: 1593198851Smav ch->fatalerr = 1; 1594195534Sscottl ccb->ccb_h.status |= CAM_REQ_INVALID; 1595195534Sscottl break; 1596195534Sscottl case AHCI_ERR_INNOCENT: 1597195534Sscottl ccb->ccb_h.status |= CAM_REQUEUE_REQ; 1598195534Sscottl break; 1599195534Sscottl case AHCI_ERR_TFE: 1600198319Smav case AHCI_ERR_NCQ: 1601195534Sscottl if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1602195534Sscottl ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR; 1603195534Sscottl ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 1604195534Sscottl } else { 1605195534Sscottl ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR; 1606195534Sscottl } 1607195534Sscottl break; 1608195534Sscottl case AHCI_ERR_SATA: 1609198851Smav ch->fatalerr = 1; 1610198319Smav if (!ch->readlog) { 1611198319Smav xpt_freeze_simq(ch->sim, 1); 1612198319Smav ccb->ccb_h.status &= ~CAM_STATUS_MASK; 1613198319Smav ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 1614198319Smav } 1615198319Smav ccb->ccb_h.status |= CAM_UNCOR_PARITY; 1616195534Sscottl break; 1617195534Sscottl case AHCI_ERR_TIMEOUT: 1618198851Smav /* Do no treat soft-reset timeout as fatal here. */ 1619198851Smav if (ccb->ccb_h.func_code != XPT_ATA_IO || 1620198851Smav !(ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL)) 1621198851Smav ch->fatalerr = 1; 1622198319Smav if (!ch->readlog) { 1623198319Smav xpt_freeze_simq(ch->sim, 1); 1624198319Smav ccb->ccb_h.status &= ~CAM_STATUS_MASK; 1625198319Smav ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 1626198319Smav } 1627195534Sscottl ccb->ccb_h.status |= CAM_CMD_TIMEOUT; 1628195534Sscottl break; 1629195534Sscottl default: 1630198851Smav ch->fatalerr = 1; 1631195534Sscottl ccb->ccb_h.status |= CAM_REQ_CMP_ERR; 1632195534Sscottl } 1633195534Sscottl /* Free slot. */ 1634195534Sscottl ch->rslots &= ~(1 << slot->slot); 1635195534Sscottl ch->aslots &= ~(1 << slot->slot); 1636195534Sscottl slot->state = AHCI_SLOT_EMPTY; 1637195534Sscottl slot->ccb = NULL; 1638195534Sscottl /* Update channel stats. */ 1639195534Sscottl ch->numrslots--; 1640195534Sscottl if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1641195534Sscottl (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 1642195534Sscottl ch->numtslots--; 1643195534Sscottl } 1644195534Sscottl /* If it was first request of reset sequence and there is no error, 1645195534Sscottl * proceed to second request. */ 1646195534Sscottl if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1647195534Sscottl (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) && 1648195534Sscottl (ccb->ataio.cmd.control & ATA_A_RESET) && 1649195534Sscottl et == AHCI_ERR_NONE) { 1650195534Sscottl ccb->ataio.cmd.control &= ~ATA_A_RESET; 1651195534Sscottl ahci_begin_transaction(dev, ccb); 1652195534Sscottl return; 1653195534Sscottl } 1654198851Smav /* If it was our READ LOG command - process it. */ 1655198851Smav if (ch->readlog) { 1656198851Smav ahci_process_read_log(dev, ccb); 1657195534Sscottl /* If it was NCQ command error, put result on hold. */ 1658198851Smav } else if (et == AHCI_ERR_NCQ) { 1659195534Sscottl ch->hold[slot->slot] = ccb; 1660198851Smav } else 1661195534Sscottl xpt_done(ccb); 1662195534Sscottl /* Unfreeze frozen command. */ 1663195534Sscottl if (ch->frozen && ch->numrslots == 0) { 1664195534Sscottl union ccb *fccb = ch->frozen; 1665195534Sscottl ch->frozen = NULL; 1666195534Sscottl ahci_begin_transaction(dev, fccb); 1667195534Sscottl xpt_release_simq(ch->sim, TRUE); 1668195534Sscottl } 1669198851Smav /* If we have no other active commands, ... */ 1670198851Smav if (ch->rslots == 0) { 1671198851Smav /* if there was fatal error - reset port. */ 1672198851Smav if (ch->fatalerr) { 1673198851Smav ahci_reset(dev); 1674198851Smav } 1675198851Smav } 1676196656Smav /* Start PM timer. */ 1677196656Smav if (ch->numrslots == 0 && ch->pm_level > 3) { 1678196656Smav callout_schedule(&ch->pm_timer, 1679196656Smav (ch->pm_level == 4) ? hz / 1000 : hz / 8); 1680196656Smav } 1681195534Sscottl} 1682195534Sscottl 1683195534Sscottlstatic void 1684195534Sscottlahci_issue_read_log(device_t dev) 1685195534Sscottl{ 1686195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1687195534Sscottl union ccb *ccb; 1688195534Sscottl struct ccb_ataio *ataio; 1689195534Sscottl int i; 1690195534Sscottl 1691195534Sscottl ch->readlog = 1; 1692195534Sscottl /* Find some holden command. */ 1693195534Sscottl for (i = 0; i < ch->numslots; i++) { 1694195534Sscottl if (ch->hold[i]) 1695195534Sscottl break; 1696195534Sscottl } 1697195534Sscottl ccb = xpt_alloc_ccb_nowait(); 1698195534Sscottl if (ccb == NULL) { 1699195534Sscottl device_printf(dev, "Unable allocate READ LOG command"); 1700195534Sscottl return; /* XXX */ 1701195534Sscottl } 1702195534Sscottl ccb->ccb_h = ch->hold[i]->ccb_h; /* Reuse old header. */ 1703195534Sscottl ccb->ccb_h.func_code = XPT_ATA_IO; 1704195534Sscottl ccb->ccb_h.flags = CAM_DIR_IN; 1705195534Sscottl ccb->ccb_h.timeout = 1000; /* 1s should be enough. */ 1706195534Sscottl ataio = &ccb->ataio; 1707195534Sscottl ataio->data_ptr = malloc(512, M_AHCI, M_NOWAIT); 1708195534Sscottl if (ataio->data_ptr == NULL) { 1709195534Sscottl device_printf(dev, "Unable allocate memory for READ LOG command"); 1710195534Sscottl return; /* XXX */ 1711195534Sscottl } 1712195534Sscottl ataio->dxfer_len = 512; 1713195534Sscottl bzero(&ataio->cmd, sizeof(ataio->cmd)); 1714195534Sscottl ataio->cmd.flags = CAM_ATAIO_48BIT; 1715195534Sscottl ataio->cmd.command = 0x2F; /* READ LOG EXT */ 1716195534Sscottl ataio->cmd.sector_count = 1; 1717195534Sscottl ataio->cmd.sector_count_exp = 0; 1718195534Sscottl ataio->cmd.lba_low = 0x10; 1719195534Sscottl ataio->cmd.lba_mid = 0; 1720195534Sscottl ataio->cmd.lba_mid_exp = 0; 1721198319Smav /* Freeze SIM while doing READ LOG EXT. */ 1722198319Smav xpt_freeze_simq(ch->sim, 1); 1723195534Sscottl ahci_begin_transaction(dev, ccb); 1724195534Sscottl} 1725195534Sscottl 1726195534Sscottlstatic void 1727195534Sscottlahci_process_read_log(device_t dev, union ccb *ccb) 1728195534Sscottl{ 1729195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1730195534Sscottl uint8_t *data; 1731195534Sscottl struct ata_res *res; 1732195534Sscottl int i; 1733195534Sscottl 1734195534Sscottl ch->readlog = 0; 1735195534Sscottl 1736195534Sscottl data = ccb->ataio.data_ptr; 1737195534Sscottl if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && 1738195534Sscottl (data[0] & 0x80) == 0) { 1739195534Sscottl for (i = 0; i < ch->numslots; i++) { 1740195534Sscottl if (!ch->hold[i]) 1741195534Sscottl continue; 1742195534Sscottl if ((data[0] & 0x1F) == i) { 1743195534Sscottl res = &ch->hold[i]->ataio.res; 1744195534Sscottl res->status = data[2]; 1745195534Sscottl res->error = data[3]; 1746195534Sscottl res->lba_low = data[4]; 1747195534Sscottl res->lba_mid = data[5]; 1748195534Sscottl res->lba_high = data[6]; 1749195534Sscottl res->device = data[7]; 1750195534Sscottl res->lba_low_exp = data[8]; 1751195534Sscottl res->lba_mid_exp = data[9]; 1752195534Sscottl res->lba_high_exp = data[10]; 1753195534Sscottl res->sector_count = data[12]; 1754195534Sscottl res->sector_count_exp = data[13]; 1755195534Sscottl } else { 1756195534Sscottl ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 1757195534Sscottl ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ; 1758195534Sscottl } 1759195534Sscottl xpt_done(ch->hold[i]); 1760195534Sscottl ch->hold[i] = NULL; 1761195534Sscottl } 1762195534Sscottl } else { 1763195534Sscottl if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 1764195534Sscottl device_printf(dev, "Error while READ LOG EXT\n"); 1765195534Sscottl else if ((data[0] & 0x80) == 0) { 1766195534Sscottl device_printf(dev, "Non-queued command error in READ LOG EXT\n"); 1767195534Sscottl } 1768195534Sscottl for (i = 0; i < ch->numslots; i++) { 1769195534Sscottl if (!ch->hold[i]) 1770195534Sscottl continue; 1771195534Sscottl xpt_done(ch->hold[i]); 1772195534Sscottl ch->hold[i] = NULL; 1773195534Sscottl } 1774195534Sscottl } 1775195534Sscottl free(ccb->ataio.data_ptr, M_AHCI); 1776195534Sscottl xpt_free_ccb(ccb); 1777198319Smav xpt_release_simq(ch->sim, TRUE); 1778195534Sscottl} 1779195534Sscottl 1780195534Sscottlstatic void 1781195534Sscottlahci_start(device_t dev) 1782195534Sscottl{ 1783195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1784195534Sscottl u_int32_t cmd; 1785195534Sscottl 1786195534Sscottl /* Clear SATA error register */ 1787195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_SERR, 0xFFFFFFFF); 1788195534Sscottl /* Clear any interrupts pending on this channel */ 1789195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_IS, 0xFFFFFFFF); 1790195534Sscottl /* Start operations on this channel */ 1791195534Sscottl cmd = ATA_INL(ch->r_mem, AHCI_P_CMD); 1792195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd | AHCI_P_CMD_ST | 1793195534Sscottl (ch->pm_present ? AHCI_P_CMD_PMA : 0)); 1794195534Sscottl} 1795195534Sscottl 1796195534Sscottlstatic void 1797195534Sscottlahci_stop(device_t dev) 1798195534Sscottl{ 1799195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1800195534Sscottl u_int32_t cmd; 1801195534Sscottl int timeout; 1802195534Sscottl 1803195534Sscottl /* Kill all activity on this channel */ 1804195534Sscottl cmd = ATA_INL(ch->r_mem, AHCI_P_CMD); 1805195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd & ~AHCI_P_CMD_ST); 1806195534Sscottl /* Wait for activity stop. */ 1807195534Sscottl timeout = 0; 1808195534Sscottl do { 1809195534Sscottl DELAY(1000); 1810195534Sscottl if (timeout++ > 1000) { 1811195534Sscottl device_printf(dev, "stopping AHCI engine failed\n"); 1812195534Sscottl break; 1813195534Sscottl } 1814195534Sscottl } while (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CR); 1815195534Sscottl} 1816195534Sscottl 1817195534Sscottlstatic void 1818195534Sscottlahci_clo(device_t dev) 1819195534Sscottl{ 1820195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1821195534Sscottl u_int32_t cmd; 1822195534Sscottl int timeout; 1823195534Sscottl 1824195534Sscottl /* Issue Command List Override if supported */ 1825195534Sscottl if (ch->caps & AHCI_CAP_SCLO) { 1826195534Sscottl cmd = ATA_INL(ch->r_mem, AHCI_P_CMD); 1827195534Sscottl cmd |= AHCI_P_CMD_CLO; 1828195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd); 1829195534Sscottl timeout = 0; 1830195534Sscottl do { 1831195534Sscottl DELAY(1000); 1832195534Sscottl if (timeout++ > 1000) { 1833195534Sscottl device_printf(dev, "executing CLO failed\n"); 1834195534Sscottl break; 1835195534Sscottl } 1836195534Sscottl } while (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CLO); 1837195534Sscottl } 1838195534Sscottl} 1839195534Sscottl 1840195534Sscottlstatic void 1841195534Sscottlahci_stop_fr(device_t dev) 1842195534Sscottl{ 1843195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1844195534Sscottl u_int32_t cmd; 1845195534Sscottl int timeout; 1846195534Sscottl 1847195534Sscottl /* Kill all FIS reception on this channel */ 1848195534Sscottl cmd = ATA_INL(ch->r_mem, AHCI_P_CMD); 1849195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd & ~AHCI_P_CMD_FRE); 1850195534Sscottl /* Wait for FIS reception stop. */ 1851195534Sscottl timeout = 0; 1852195534Sscottl do { 1853195534Sscottl DELAY(1000); 1854195534Sscottl if (timeout++ > 1000) { 1855195534Sscottl device_printf(dev, "stopping AHCI FR engine failed\n"); 1856195534Sscottl break; 1857195534Sscottl } 1858195534Sscottl } while (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_FR); 1859195534Sscottl} 1860195534Sscottl 1861195534Sscottlstatic void 1862195534Sscottlahci_start_fr(device_t dev) 1863195534Sscottl{ 1864195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1865195534Sscottl u_int32_t cmd; 1866195534Sscottl 1867195534Sscottl /* Start FIS reception on this channel */ 1868195534Sscottl cmd = ATA_INL(ch->r_mem, AHCI_P_CMD); 1869195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd | AHCI_P_CMD_FRE); 1870195534Sscottl} 1871195534Sscottl 1872195534Sscottlstatic int 1873195534Sscottlahci_wait_ready(device_t dev, int t) 1874195534Sscottl{ 1875195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1876195534Sscottl int timeout = 0; 1877195534Sscottl uint32_t val; 1878195534Sscottl 1879195534Sscottl while ((val = ATA_INL(ch->r_mem, AHCI_P_TFD)) & 1880195534Sscottl (ATA_S_BUSY | ATA_S_DRQ)) { 1881195534Sscottl DELAY(1000); 1882195534Sscottl if (timeout++ > t) { 1883195534Sscottl device_printf(dev, "port is not ready (timeout %dms) " 1884195534Sscottl "tfd = %08x\n", t, val); 1885195534Sscottl return (EBUSY); 1886195534Sscottl } 1887195534Sscottl } 1888195534Sscottl if (bootverbose) 1889195534Sscottl device_printf(dev, "ready wait time=%dms\n", timeout); 1890195534Sscottl return (0); 1891195534Sscottl} 1892195534Sscottl 1893195534Sscottlstatic void 1894195534Sscottlahci_reset(device_t dev) 1895195534Sscottl{ 1896195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1897196656Smav struct ahci_controller *ctlr = device_get_softc(device_get_parent(dev)); 1898195534Sscottl int i; 1899195534Sscottl 1900195534Sscottl if (bootverbose) 1901195534Sscottl device_printf(dev, "AHCI reset...\n"); 1902195534Sscottl /* Requeue freezed command. */ 1903195534Sscottl if (ch->frozen) { 1904195534Sscottl union ccb *fccb = ch->frozen; 1905195534Sscottl ch->frozen = NULL; 1906195534Sscottl fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 1907198319Smav if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 1908198319Smav xpt_freeze_devq(fccb->ccb_h.path, 1); 1909198319Smav fccb->ccb_h.status |= CAM_DEV_QFRZN; 1910198319Smav } 1911195534Sscottl xpt_done(fccb); 1912195534Sscottl } 1913195534Sscottl /* Kill the engine and requeue all running commands. */ 1914195534Sscottl ahci_stop(dev); 1915195534Sscottl for (i = 0; i < ch->numslots; i++) { 1916195534Sscottl /* Do we have a running request on slot? */ 1917195534Sscottl if (ch->slot[i].state < AHCI_SLOT_RUNNING) 1918195534Sscottl continue; 1919195534Sscottl /* XXX; Commands in loading state. */ 1920195534Sscottl ahci_end_transaction(&ch->slot[i], AHCI_ERR_INNOCENT); 1921195534Sscottl } 1922198851Smav for (i = 0; i < ch->numslots; i++) { 1923198851Smav if (!ch->hold[i]) 1924198851Smav continue; 1925198851Smav xpt_done(ch->hold[i]); 1926198851Smav ch->hold[i] = NULL; 1927198851Smav } 1928198851Smav ch->fatalerr = 0; 1929198319Smav /* Tell the XPT about the event */ 1930198319Smav xpt_async(AC_BUS_RESET, ch->path, NULL); 1931195534Sscottl /* Disable port interrupts */ 1932195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_IE, 0); 1933195534Sscottl /* Reset and reconnect PHY, */ 1934195534Sscottl if (!ahci_sata_phy_reset(dev, 0)) { 1935195534Sscottl if (bootverbose) 1936195534Sscottl device_printf(dev, 1937195534Sscottl "AHCI reset done: phy reset found no device\n"); 1938195534Sscottl ch->devices = 0; 1939195534Sscottl /* Enable wanted port interrupts */ 1940195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_IE, 1941195534Sscottl (AHCI_P_IX_CPD | AHCI_P_IX_PRC | AHCI_P_IX_PC)); 1942195534Sscottl return; 1943195534Sscottl } 1944195534Sscottl /* Wait for clearing busy status. */ 1945195534Sscottl if (ahci_wait_ready(dev, 10000)) { 1946195534Sscottl device_printf(dev, "device ready timeout\n"); 1947195534Sscottl ahci_clo(dev); 1948195534Sscottl } 1949195534Sscottl ahci_start(dev); 1950195534Sscottl ch->devices = 1; 1951195534Sscottl /* Enable wanted port interrupts */ 1952195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_IE, 1953195534Sscottl (AHCI_P_IX_CPD | AHCI_P_IX_TFE | AHCI_P_IX_HBF | 1954195534Sscottl AHCI_P_IX_HBD | AHCI_P_IX_IF | AHCI_P_IX_OF | 1955195534Sscottl ((ch->pm_level == 0) ? AHCI_P_IX_PRC | AHCI_P_IX_PC : 0) | 1956196656Smav AHCI_P_IX_DP | AHCI_P_IX_UF | (ctlr->ccc ? 0 : AHCI_P_IX_SDB) | 1957196656Smav AHCI_P_IX_DS | AHCI_P_IX_PS | (ctlr->ccc ? 0 : AHCI_P_IX_DHR))); 1958195534Sscottl if (bootverbose) 1959196656Smav device_printf(dev, "AHCI reset done: device found\n"); 1960195534Sscottl} 1961195534Sscottl 1962195534Sscottlstatic int 1963195534Sscottlahci_setup_fis(struct ahci_cmd_tab *ctp, union ccb *ccb, int tag) 1964195534Sscottl{ 1965195534Sscottl u_int8_t *fis = &ctp->cfis[0]; 1966195534Sscottl 1967195534Sscottl bzero(ctp->cfis, 64); 1968195534Sscottl fis[0] = 0x27; /* host to device */ 1969195534Sscottl fis[1] = (ccb->ccb_h.target_id & 0x0f); 1970195534Sscottl if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1971195534Sscottl fis[1] |= 0x80; 1972195534Sscottl fis[2] = ATA_PACKET_CMD; 1973195534Sscottl if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) 1974195534Sscottl fis[3] = ATA_F_DMA; 1975195534Sscottl else { 1976195534Sscottl fis[5] = ccb->csio.dxfer_len; 1977195534Sscottl fis[6] = ccb->csio.dxfer_len >> 8; 1978195534Sscottl } 1979195534Sscottl fis[7] = ATA_D_LBA; 1980195534Sscottl fis[15] = ATA_A_4BIT; 1981195534Sscottl bzero(ctp->acmd, 32); 1982195534Sscottl bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ? 1983195534Sscottl ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes, 1984195534Sscottl ctp->acmd, ccb->csio.cdb_len); 1985195534Sscottl } else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) { 1986195534Sscottl fis[1] |= 0x80; 1987195534Sscottl fis[2] = ccb->ataio.cmd.command; 1988195534Sscottl fis[3] = ccb->ataio.cmd.features; 1989195534Sscottl fis[4] = ccb->ataio.cmd.lba_low; 1990195534Sscottl fis[5] = ccb->ataio.cmd.lba_mid; 1991195534Sscottl fis[6] = ccb->ataio.cmd.lba_high; 1992195534Sscottl fis[7] = ccb->ataio.cmd.device; 1993195534Sscottl fis[8] = ccb->ataio.cmd.lba_low_exp; 1994195534Sscottl fis[9] = ccb->ataio.cmd.lba_mid_exp; 1995195534Sscottl fis[10] = ccb->ataio.cmd.lba_high_exp; 1996195534Sscottl fis[11] = ccb->ataio.cmd.features_exp; 1997195534Sscottl if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 1998195534Sscottl fis[12] = tag << 3; 1999195534Sscottl fis[13] = 0; 2000195534Sscottl } else { 2001195534Sscottl fis[12] = ccb->ataio.cmd.sector_count; 2002195534Sscottl fis[13] = ccb->ataio.cmd.sector_count_exp; 2003195534Sscottl } 2004195534Sscottl fis[15] = ATA_A_4BIT; 2005195534Sscottl } else { 2006195534Sscottl fis[15] = ccb->ataio.cmd.control; 2007195534Sscottl } 2008195534Sscottl return (20); 2009195534Sscottl} 2010195534Sscottl 2011195534Sscottlstatic int 2012195534Sscottlahci_sata_connect(struct ahci_channel *ch) 2013195534Sscottl{ 2014195534Sscottl u_int32_t status; 2015195534Sscottl int timeout; 2016195534Sscottl 2017195534Sscottl /* Wait up to 100ms for "connect well" */ 2018195534Sscottl for (timeout = 0; timeout < 100 ; timeout++) { 2019195534Sscottl status = ATA_INL(ch->r_mem, AHCI_P_SSTS); 2020195534Sscottl if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) && 2021195534Sscottl ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) && 2022195534Sscottl ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) 2023195534Sscottl break; 2024196656Smav if ((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_OFFLINE) { 2025196656Smav if (bootverbose) { 2026196656Smav device_printf(ch->dev, "SATA offline status=%08x\n", 2027196656Smav status); 2028196656Smav } 2029196656Smav return (0); 2030196656Smav } 2031195534Sscottl DELAY(1000); 2032195534Sscottl } 2033195534Sscottl if (timeout >= 100) { 2034195534Sscottl if (bootverbose) { 2035195534Sscottl device_printf(ch->dev, "SATA connect timeout status=%08x\n", 2036195534Sscottl status); 2037195534Sscottl } 2038195534Sscottl return (0); 2039195534Sscottl } 2040195534Sscottl if (bootverbose) { 2041195534Sscottl device_printf(ch->dev, "SATA connect time=%dms status=%08x\n", 2042195534Sscottl timeout, status); 2043195534Sscottl } 2044195534Sscottl /* Clear SATA error register */ 2045195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_SERR, 0xffffffff); 2046195534Sscottl return (1); 2047195534Sscottl} 2048195534Sscottl 2049195534Sscottlstatic int 2050195534Sscottlahci_sata_phy_reset(device_t dev, int quick) 2051195534Sscottl{ 2052195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 2053195534Sscottl uint32_t val; 2054195534Sscottl 2055195534Sscottl if (quick) { 2056195534Sscottl val = ATA_INL(ch->r_mem, AHCI_P_SCTL); 2057195534Sscottl if ((val & ATA_SC_DET_MASK) == ATA_SC_DET_IDLE) 2058195534Sscottl return (ahci_sata_connect(ch)); 2059195534Sscottl } 2060195534Sscottl 2061195534Sscottl if (bootverbose) 2062195534Sscottl device_printf(dev, "hardware reset ...\n"); 2063195534Sscottl if (ch->sata_rev == 1) 2064195534Sscottl val = ATA_SC_SPD_SPEED_GEN1; 2065195534Sscottl else if (ch->sata_rev == 2) 2066195534Sscottl val = ATA_SC_SPD_SPEED_GEN2; 2067195534Sscottl else if (ch->sata_rev == 3) 2068195534Sscottl val = ATA_SC_SPD_SPEED_GEN3; 2069195534Sscottl else 2070195534Sscottl val = 0; 2071195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_SCTL, 2072196656Smav ATA_SC_DET_RESET | val | 2073196656Smav ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER); 2074196656Smav DELAY(5000); 2075196656Smav ATA_OUTL(ch->r_mem, AHCI_P_SCTL, 2076195534Sscottl ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 : 2077195534Sscottl (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER))); 2078196656Smav DELAY(5000); 2079195534Sscottl return (ahci_sata_connect(ch)); 2080195534Sscottl} 2081195534Sscottl 2082195534Sscottlstatic void 2083195534Sscottlahciaction(struct cam_sim *sim, union ccb *ccb) 2084195534Sscottl{ 2085195534Sscottl device_t dev; 2086195534Sscottl struct ahci_channel *ch; 2087195534Sscottl 2088195534Sscottl CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahciaction func_code=%x\n", 2089195534Sscottl ccb->ccb_h.func_code)); 2090195534Sscottl 2091195534Sscottl ch = (struct ahci_channel *)cam_sim_softc(sim); 2092195534Sscottl dev = ch->dev; 2093195534Sscottl switch (ccb->ccb_h.func_code) { 2094195534Sscottl /* Common cases first */ 2095195534Sscottl case XPT_ATA_IO: /* Execute the requested I/O operation */ 2096195534Sscottl case XPT_SCSI_IO: 2097195534Sscottl if (ch->devices == 0) { 2098195534Sscottl ccb->ccb_h.status = CAM_SEL_TIMEOUT; 2099195534Sscottl xpt_done(ccb); 2100195534Sscottl break; 2101195534Sscottl } 2102195534Sscottl /* Check for command collision. */ 2103195534Sscottl if (ahci_check_collision(dev, ccb)) { 2104195534Sscottl /* Freeze command. */ 2105195534Sscottl ch->frozen = ccb; 2106195534Sscottl /* We have only one frozen slot, so freeze simq also. */ 2107195534Sscottl xpt_freeze_simq(ch->sim, 1); 2108195534Sscottl return; 2109195534Sscottl } 2110195534Sscottl ahci_begin_transaction(dev, ccb); 2111195534Sscottl break; 2112195534Sscottl case XPT_EN_LUN: /* Enable LUN as a target */ 2113195534Sscottl case XPT_TARGET_IO: /* Execute target I/O request */ 2114195534Sscottl case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */ 2115195534Sscottl case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/ 2116195534Sscottl case XPT_ABORT: /* Abort the specified CCB */ 2117195534Sscottl /* XXX Implement */ 2118195534Sscottl ccb->ccb_h.status = CAM_REQ_INVALID; 2119195534Sscottl xpt_done(ccb); 2120195534Sscottl break; 2121195534Sscottl case XPT_SET_TRAN_SETTINGS: 2122195534Sscottl { 2123195534Sscottl struct ccb_trans_settings *cts = &ccb->cts; 2124195534Sscottl 2125195534Sscottl if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) { 2126195534Sscottl ch->pm_present = cts->xport_specific.sata.pm_present; 2127195534Sscottl } 2128195534Sscottl ccb->ccb_h.status = CAM_REQ_CMP; 2129195534Sscottl xpt_done(ccb); 2130195534Sscottl break; 2131195534Sscottl } 2132195534Sscottl case XPT_GET_TRAN_SETTINGS: 2133195534Sscottl /* Get default/user set transfer settings for the target */ 2134195534Sscottl { 2135195534Sscottl struct ccb_trans_settings *cts = &ccb->cts; 2136195534Sscottl uint32_t status; 2137195534Sscottl 2138195534Sscottl cts->protocol = PROTO_ATA; 2139196656Smav cts->protocol_version = PROTO_VERSION_UNSPECIFIED; 2140195534Sscottl cts->transport = XPORT_SATA; 2141196656Smav cts->transport_version = XPORT_VERSION_UNSPECIFIED; 2142195534Sscottl cts->proto_specific.valid = 0; 2143195534Sscottl cts->xport_specific.sata.valid = 0; 2144195534Sscottl if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 2145195534Sscottl status = ATA_INL(ch->r_mem, AHCI_P_SSTS) & ATA_SS_SPD_MASK; 2146195534Sscottl else 2147195534Sscottl status = ATA_INL(ch->r_mem, AHCI_P_SCTL) & ATA_SC_SPD_MASK; 2148195534Sscottl if (status & ATA_SS_SPD_GEN3) { 2149195534Sscottl cts->xport_specific.sata.bitrate = 600000; 2150195534Sscottl cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED; 2151195534Sscottl } else if (status & ATA_SS_SPD_GEN2) { 2152195534Sscottl cts->xport_specific.sata.bitrate = 300000; 2153195534Sscottl cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED; 2154195534Sscottl } else if (status & ATA_SS_SPD_GEN1) { 2155195534Sscottl cts->xport_specific.sata.bitrate = 150000; 2156195534Sscottl cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED; 2157195534Sscottl } 2158195534Sscottl if (cts->type == CTS_TYPE_CURRENT_SETTINGS) { 2159195534Sscottl cts->xport_specific.sata.pm_present = 2160195534Sscottl (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_PMA) ? 2161195534Sscottl 1 : 0; 2162195534Sscottl } else { 2163195534Sscottl cts->xport_specific.sata.pm_present = ch->pm_present; 2164195534Sscottl } 2165195534Sscottl cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM; 2166195534Sscottl ccb->ccb_h.status = CAM_REQ_CMP; 2167195534Sscottl xpt_done(ccb); 2168195534Sscottl break; 2169195534Sscottl } 2170195534Sscottl#if 0 2171195534Sscottl case XPT_CALC_GEOMETRY: 2172195534Sscottl { 2173195534Sscottl struct ccb_calc_geometry *ccg; 2174195534Sscottl uint32_t size_mb; 2175195534Sscottl uint32_t secs_per_cylinder; 2176195534Sscottl 2177195534Sscottl ccg = &ccb->ccg; 2178195534Sscottl size_mb = ccg->volume_size 2179195534Sscottl / ((1024L * 1024L) / ccg->block_size); 2180195534Sscottl if (size_mb >= 1024 && (aha->extended_trans != 0)) { 2181195534Sscottl if (size_mb >= 2048) { 2182195534Sscottl ccg->heads = 255; 2183195534Sscottl ccg->secs_per_track = 63; 2184195534Sscottl } else { 2185195534Sscottl ccg->heads = 128; 2186195534Sscottl ccg->secs_per_track = 32; 2187195534Sscottl } 2188195534Sscottl } else { 2189195534Sscottl ccg->heads = 64; 2190195534Sscottl ccg->secs_per_track = 32; 2191195534Sscottl } 2192195534Sscottl secs_per_cylinder = ccg->heads * ccg->secs_per_track; 2193195534Sscottl ccg->cylinders = ccg->volume_size / secs_per_cylinder; 2194195534Sscottl ccb->ccb_h.status = CAM_REQ_CMP; 2195195534Sscottl xpt_done(ccb); 2196195534Sscottl break; 2197195534Sscottl } 2198195534Sscottl#endif 2199195534Sscottl case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 2200195534Sscottl case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */ 2201195534Sscottl ahci_reset(dev); 2202195534Sscottl ccb->ccb_h.status = CAM_REQ_CMP; 2203195534Sscottl xpt_done(ccb); 2204195534Sscottl break; 2205195534Sscottl case XPT_TERM_IO: /* Terminate the I/O process */ 2206195534Sscottl /* XXX Implement */ 2207195534Sscottl ccb->ccb_h.status = CAM_REQ_INVALID; 2208195534Sscottl xpt_done(ccb); 2209195534Sscottl break; 2210195534Sscottl case XPT_PATH_INQ: /* Path routing inquiry */ 2211195534Sscottl { 2212195534Sscottl struct ccb_pathinq *cpi = &ccb->cpi; 2213195534Sscottl 2214195534Sscottl cpi->version_num = 1; /* XXX??? */ 2215199278Smav cpi->hba_inquiry = PI_SDTR_ABLE; 2216199278Smav if (ch->caps & AHCI_CAP_SNCQ) 2217199278Smav cpi->hba_inquiry |= PI_TAG_ABLE; 2218195534Sscottl if (ch->caps & AHCI_CAP_SPM) 2219195534Sscottl cpi->hba_inquiry |= PI_SATAPM; 2220195534Sscottl cpi->target_sprt = 0; 2221195534Sscottl cpi->hba_misc = PIM_SEQSCAN; 2222195534Sscottl cpi->hba_eng_cnt = 0; 2223195534Sscottl if (ch->caps & AHCI_CAP_SPM) 2224198322Smav cpi->max_target = 15; 2225195534Sscottl else 2226195534Sscottl cpi->max_target = 0; 2227195534Sscottl cpi->max_lun = 0; 2228195534Sscottl cpi->initiator_id = 0; 2229195534Sscottl cpi->bus_id = cam_sim_bus(sim); 2230195534Sscottl cpi->base_transfer_speed = 150000; 2231195534Sscottl strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2232195534Sscottl strncpy(cpi->hba_vid, "AHCI", HBA_IDLEN); 2233195534Sscottl strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 2234195534Sscottl cpi->unit_number = cam_sim_unit(sim); 2235195534Sscottl cpi->transport = XPORT_SATA; 2236196656Smav cpi->transport_version = XPORT_VERSION_UNSPECIFIED; 2237195534Sscottl cpi->protocol = PROTO_ATA; 2238196656Smav cpi->protocol_version = PROTO_VERSION_UNSPECIFIED; 2239195534Sscottl cpi->maxio = MAXPHYS; 2240196777Smav /* ATI SB600 can't handle 256 sectors with FPDMA (NCQ). */ 2241196777Smav if (pci_get_devid(device_get_parent(dev)) == 0x43801002) 2242196796Smav cpi->maxio = min(cpi->maxio, 128 * 512); 2243195534Sscottl cpi->ccb_h.status = CAM_REQ_CMP; 2244195534Sscottl xpt_done(ccb); 2245195534Sscottl break; 2246195534Sscottl } 2247195534Sscottl default: 2248195534Sscottl ccb->ccb_h.status = CAM_REQ_INVALID; 2249195534Sscottl xpt_done(ccb); 2250195534Sscottl break; 2251195534Sscottl } 2252195534Sscottl} 2253195534Sscottl 2254195534Sscottlstatic void 2255195534Sscottlahcipoll(struct cam_sim *sim) 2256195534Sscottl{ 2257195534Sscottl struct ahci_channel *ch = (struct ahci_channel *)cam_sim_softc(sim); 2258195534Sscottl 2259195534Sscottl ahci_ch_intr(ch->dev); 2260195534Sscottl} 2261