ahci_pci.c revision 199717
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 199717 2009-11-23 18:07:28Z 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))) { 257199717Smav /* Do not attach JMicrons with single PCI function. */ 258199717Smav if (pci_get_vendor(dev) == 0x197b && 259199717Smav (pci_read_config(dev, 0xdf, 1) & 0x40) == 0) 260199717Smav return (ENXIO); 261199322Smav snprintf(buf, sizeof(buf), "%s AHCI SATA controller", 262199322Smav ahci_ids[i].name); 263199322Smav device_set_desc_copy(dev, buf); 264199322Smav return (BUS_PROBE_VENDOR); 265199322Smav } 266199322Smav } 267199322Smav if (!valid) 268199322Smav return (ENXIO); 269199322Smav device_set_desc_copy(dev, "AHCI SATA controller"); 270199322Smav return (BUS_PROBE_VENDOR); 271199322Smav} 272199322Smav 273199322Smavstatic int 274199322Smavahci_ata_probe(device_t dev) 275199322Smav{ 276199322Smav char buf[64]; 277199176Smav int i; 278199176Smav uint32_t devid = pci_get_devid(dev); 279195534Sscottl 280199322Smav if ((intptr_t)device_get_ivars(dev) >= 0) 281199322Smav return (ENXIO); 282199176Smav /* Is this a known AHCI chip? */ 283199176Smav for (i = 0; ahci_ids[i].id != 0; i++) { 284199176Smav if (ahci_ids[i].id == devid) { 285199176Smav snprintf(buf, sizeof(buf), "%s AHCI SATA controller", 286199176Smav ahci_ids[i].name); 287199176Smav device_set_desc_copy(dev, buf); 288199176Smav return (BUS_PROBE_VENDOR); 289199176Smav } 290199176Smav } 291199176Smav device_set_desc_copy(dev, "AHCI SATA controller"); 292195534Sscottl return (BUS_PROBE_VENDOR); 293195534Sscottl} 294195534Sscottl 295195534Sscottlstatic int 296195534Sscottlahci_attach(device_t dev) 297195534Sscottl{ 298195534Sscottl struct ahci_controller *ctlr = device_get_softc(dev); 299195534Sscottl device_t child; 300199322Smav int error, unit, speed, i; 301199322Smav uint32_t devid = pci_get_devid(dev); 302196656Smav u_int32_t version; 303195534Sscottl 304195534Sscottl ctlr->dev = dev; 305199322Smav i = 0; 306199322Smav while (ahci_ids[i].id != 0 && ahci_ids[i].id != devid) 307199322Smav i++; 308199322Smav ctlr->quirks = ahci_ids[i].quirks; 309196656Smav resource_int_value(device_get_name(dev), 310196656Smav device_get_unit(dev), "ccc", &ctlr->ccc); 311195534Sscottl /* if we have a memory BAR(5) we are likely on an AHCI part */ 312195534Sscottl ctlr->r_rid = PCIR_BAR(5); 313195534Sscottl if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 314195534Sscottl &ctlr->r_rid, RF_ACTIVE))) 315195534Sscottl return ENXIO; 316195534Sscottl /* Setup our own memory management for channels. */ 317195534Sscottl ctlr->sc_iomem.rm_type = RMAN_ARRAY; 318195534Sscottl ctlr->sc_iomem.rm_descr = "I/O memory addresses"; 319195534Sscottl if ((error = rman_init(&ctlr->sc_iomem)) != 0) { 320195534Sscottl bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 321195534Sscottl return (error); 322195534Sscottl } 323195534Sscottl if ((error = rman_manage_region(&ctlr->sc_iomem, 324195534Sscottl rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) { 325195534Sscottl bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 326195534Sscottl rman_fini(&ctlr->sc_iomem); 327195534Sscottl return (error); 328195534Sscottl } 329195534Sscottl /* Reset controller */ 330195534Sscottl if ((error = ahci_ctlr_reset(dev)) != 0) { 331195534Sscottl bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 332195534Sscottl rman_fini(&ctlr->sc_iomem); 333195534Sscottl return (error); 334195534Sscottl }; 335199322Smav /* Get the HW capabilities */ 336199322Smav version = ATA_INL(ctlr->r_mem, AHCI_VS); 337199322Smav ctlr->caps = ATA_INL(ctlr->r_mem, AHCI_CAP); 338199322Smav if (version >= 0x00010020) 339199322Smav ctlr->caps2 = ATA_INL(ctlr->r_mem, AHCI_CAP2); 340195534Sscottl ctlr->ichannels = ATA_INL(ctlr->r_mem, AHCI_PI); 341199322Smav if (ctlr->quirks & AHCI_Q_1CH) { 342199322Smav ctlr->caps &= ~AHCI_CAP_NPMASK; 343199322Smav ctlr->ichannels &= 0x01; 344199322Smav } 345199322Smav if (ctlr->quirks & AHCI_Q_2CH) { 346199322Smav ctlr->caps &= ~AHCI_CAP_NPMASK; 347199322Smav ctlr->caps |= 1; 348199322Smav ctlr->ichannels &= 0x03; 349199322Smav } 350199322Smav if (ctlr->quirks & AHCI_Q_4CH) { 351199322Smav ctlr->caps &= ~AHCI_CAP_NPMASK; 352199322Smav ctlr->caps |= 3; 353199322Smav ctlr->ichannels &= 0x0f; 354199322Smav } 355195534Sscottl ctlr->channels = MAX(flsl(ctlr->ichannels), 356199322Smav (ctlr->caps & AHCI_CAP_NPMASK) + 1); 357199322Smav if (ctlr->quirks & AHCI_Q_NOPMP) 358199322Smav ctlr->caps &= ~AHCI_CAP_SPM; 359199322Smav if (ctlr->quirks & AHCI_Q_NONCQ) 360199322Smav ctlr->caps &= ~AHCI_CAP_SNCQ; 361195534Sscottl /* Setup interrupts. */ 362195534Sscottl if (ahci_setup_interrupt(dev)) { 363195534Sscottl bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 364195534Sscottl rman_fini(&ctlr->sc_iomem); 365195534Sscottl return ENXIO; 366195534Sscottl } 367195534Sscottl /* Announce HW capabilities. */ 368196656Smav speed = (ctlr->caps & AHCI_CAP_ISS) >> AHCI_CAP_ISS_SHIFT; 369195534Sscottl device_printf(dev, 370195534Sscottl "AHCI v%x.%02x with %d %sGbps ports, Port Multiplier %s\n", 371195534Sscottl ((version >> 20) & 0xf0) + ((version >> 16) & 0x0f), 372195534Sscottl ((version >> 4) & 0xf0) + (version & 0x0f), 373196656Smav (ctlr->caps & AHCI_CAP_NPMASK) + 1, 374195534Sscottl ((speed == 1) ? "1.5":((speed == 2) ? "3": 375195534Sscottl ((speed == 3) ? "6":"?"))), 376196656Smav (ctlr->caps & AHCI_CAP_SPM) ? 377195534Sscottl "supported" : "not supported"); 378195534Sscottl if (bootverbose) { 379195534Sscottl device_printf(dev, "Caps:%s%s%s%s%s%s%s%s %sGbps", 380196656Smav (ctlr->caps & AHCI_CAP_64BIT) ? " 64bit":"", 381196656Smav (ctlr->caps & AHCI_CAP_SNCQ) ? " NCQ":"", 382196656Smav (ctlr->caps & AHCI_CAP_SSNTF) ? " SNTF":"", 383196656Smav (ctlr->caps & AHCI_CAP_SMPS) ? " MPS":"", 384196656Smav (ctlr->caps & AHCI_CAP_SSS) ? " SS":"", 385196656Smav (ctlr->caps & AHCI_CAP_SALP) ? " ALP":"", 386196656Smav (ctlr->caps & AHCI_CAP_SAL) ? " AL":"", 387196656Smav (ctlr->caps & AHCI_CAP_SCLO) ? " CLO":"", 388195534Sscottl ((speed == 1) ? "1.5":((speed == 2) ? "3": 389195534Sscottl ((speed == 3) ? "6":"?")))); 390195534Sscottl printf("%s%s%s%s%s%s %dcmd%s%s%s %dports\n", 391196656Smav (ctlr->caps & AHCI_CAP_SAM) ? " AM":"", 392196656Smav (ctlr->caps & AHCI_CAP_SPM) ? " PM":"", 393196656Smav (ctlr->caps & AHCI_CAP_FBSS) ? " FBS":"", 394196656Smav (ctlr->caps & AHCI_CAP_PMD) ? " PMD":"", 395196656Smav (ctlr->caps & AHCI_CAP_SSC) ? " SSC":"", 396196656Smav (ctlr->caps & AHCI_CAP_PSC) ? " PSC":"", 397196656Smav ((ctlr->caps & AHCI_CAP_NCS) >> AHCI_CAP_NCS_SHIFT) + 1, 398196656Smav (ctlr->caps & AHCI_CAP_CCCS) ? " CCC":"", 399196656Smav (ctlr->caps & AHCI_CAP_EMS) ? " EM":"", 400196656Smav (ctlr->caps & AHCI_CAP_SXS) ? " eSATA":"", 401196656Smav (ctlr->caps & AHCI_CAP_NPMASK) + 1); 402195534Sscottl } 403196656Smav if (bootverbose && version >= 0x00010020) { 404196656Smav device_printf(dev, "Caps2:%s%s%s\n", 405196656Smav (ctlr->caps2 & AHCI_CAP2_APST) ? " APST":"", 406196656Smav (ctlr->caps2 & AHCI_CAP2_NVMP) ? " NVMP":"", 407196656Smav (ctlr->caps2 & AHCI_CAP2_BOH) ? " BOH":""); 408196656Smav } 409195534Sscottl /* Attach all channels on this controller */ 410195534Sscottl for (unit = 0; unit < ctlr->channels; unit++) { 411195534Sscottl if ((ctlr->ichannels & (1 << unit)) == 0) 412195534Sscottl continue; 413195534Sscottl child = device_add_child(dev, "ahcich", -1); 414195534Sscottl if (child == NULL) 415195534Sscottl device_printf(dev, "failed to add channel device\n"); 416195534Sscottl else 417195534Sscottl device_set_ivars(child, (void *)(intptr_t)unit); 418195534Sscottl } 419195534Sscottl bus_generic_attach(dev); 420195534Sscottl return 0; 421195534Sscottl} 422195534Sscottl 423195534Sscottlstatic int 424195534Sscottlahci_detach(device_t dev) 425195534Sscottl{ 426195534Sscottl struct ahci_controller *ctlr = device_get_softc(dev); 427195534Sscottl device_t *children; 428195534Sscottl int nchildren, i; 429195534Sscottl 430195534Sscottl /* Detach & delete all children */ 431195534Sscottl if (!device_get_children(dev, &children, &nchildren)) { 432195534Sscottl for (i = 0; i < nchildren; i++) 433195534Sscottl device_delete_child(dev, children[i]); 434195534Sscottl free(children, M_TEMP); 435195534Sscottl } 436195534Sscottl /* Free interrupts. */ 437195534Sscottl for (i = 0; i < ctlr->numirqs; i++) { 438195534Sscottl if (ctlr->irqs[i].r_irq) { 439195534Sscottl bus_teardown_intr(dev, ctlr->irqs[i].r_irq, 440195534Sscottl ctlr->irqs[i].handle); 441195534Sscottl bus_release_resource(dev, SYS_RES_IRQ, 442195534Sscottl ctlr->irqs[i].r_irq_rid, ctlr->irqs[i].r_irq); 443195534Sscottl } 444195534Sscottl } 445195534Sscottl pci_release_msi(dev); 446195534Sscottl /* Free memory. */ 447195534Sscottl rman_fini(&ctlr->sc_iomem); 448195534Sscottl if (ctlr->r_mem) 449195534Sscottl bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 450195534Sscottl return (0); 451195534Sscottl} 452195534Sscottl 453195534Sscottlstatic int 454195534Sscottlahci_ctlr_reset(device_t dev) 455195534Sscottl{ 456195534Sscottl struct ahci_controller *ctlr = device_get_softc(dev); 457195534Sscottl int timeout; 458195534Sscottl 459195534Sscottl if (pci_read_config(dev, 0x00, 4) == 0x28298086 && 460195534Sscottl (pci_read_config(dev, 0x92, 1) & 0xfe) == 0x04) 461195534Sscottl pci_write_config(dev, 0x92, 0x01, 1); 462195534Sscottl /* Enable AHCI mode */ 463195534Sscottl ATA_OUTL(ctlr->r_mem, AHCI_GHC, AHCI_GHC_AE); 464195534Sscottl /* Reset AHCI controller */ 465195534Sscottl ATA_OUTL(ctlr->r_mem, AHCI_GHC, AHCI_GHC_AE|AHCI_GHC_HR); 466195534Sscottl for (timeout = 1000; timeout > 0; timeout--) { 467195534Sscottl DELAY(1000); 468195534Sscottl if ((ATA_INL(ctlr->r_mem, AHCI_GHC) & AHCI_GHC_HR) == 0) 469195534Sscottl break; 470195534Sscottl } 471195534Sscottl if (timeout == 0) { 472195534Sscottl device_printf(dev, "AHCI controller reset failure\n"); 473195534Sscottl return ENXIO; 474195534Sscottl } 475195534Sscottl /* Reenable AHCI mode */ 476195534Sscottl ATA_OUTL(ctlr->r_mem, AHCI_GHC, AHCI_GHC_AE); 477195534Sscottl /* Clear interrupts */ 478195534Sscottl ATA_OUTL(ctlr->r_mem, AHCI_IS, ATA_INL(ctlr->r_mem, AHCI_IS)); 479196656Smav /* Configure CCC */ 480196656Smav if (ctlr->ccc) { 481196656Smav ATA_OUTL(ctlr->r_mem, AHCI_CCCP, ATA_INL(ctlr->r_mem, AHCI_PI)); 482196656Smav ATA_OUTL(ctlr->r_mem, AHCI_CCCC, 483196656Smav (ctlr->ccc << AHCI_CCCC_TV_SHIFT) | 484196656Smav (4 << AHCI_CCCC_CC_SHIFT) | 485196656Smav AHCI_CCCC_EN); 486196656Smav ctlr->cccv = (ATA_INL(ctlr->r_mem, AHCI_CCCC) & 487196656Smav AHCI_CCCC_INT_MASK) >> AHCI_CCCC_INT_SHIFT; 488196656Smav if (bootverbose) { 489196656Smav device_printf(dev, 490196656Smav "CCC with %dms/4cmd enabled on vector %d\n", 491196656Smav ctlr->ccc, ctlr->cccv); 492196656Smav } 493196656Smav } 494195534Sscottl /* Enable AHCI interrupts */ 495195534Sscottl ATA_OUTL(ctlr->r_mem, AHCI_GHC, 496195534Sscottl ATA_INL(ctlr->r_mem, AHCI_GHC) | AHCI_GHC_IE); 497195534Sscottl return (0); 498195534Sscottl} 499195534Sscottl 500195534Sscottlstatic int 501195534Sscottlahci_suspend(device_t dev) 502195534Sscottl{ 503195534Sscottl struct ahci_controller *ctlr = device_get_softc(dev); 504195534Sscottl 505195534Sscottl bus_generic_suspend(dev); 506195534Sscottl /* Disable interupts, so the state change(s) doesn't trigger */ 507195534Sscottl ATA_OUTL(ctlr->r_mem, AHCI_GHC, 508195534Sscottl ATA_INL(ctlr->r_mem, AHCI_GHC) & (~AHCI_GHC_IE)); 509195534Sscottl return 0; 510195534Sscottl} 511195534Sscottl 512195534Sscottlstatic int 513195534Sscottlahci_resume(device_t dev) 514195534Sscottl{ 515195534Sscottl int res; 516195534Sscottl 517195534Sscottl if ((res = ahci_ctlr_reset(dev)) != 0) 518195534Sscottl return (res); 519195534Sscottl return (bus_generic_resume(dev)); 520195534Sscottl} 521195534Sscottl 522195534Sscottlstatic int 523195534Sscottlahci_setup_interrupt(device_t dev) 524195534Sscottl{ 525195534Sscottl struct ahci_controller *ctlr = device_get_softc(dev); 526195534Sscottl int i, msi = 1; 527195534Sscottl 528195534Sscottl /* Process hints. */ 529195534Sscottl resource_int_value(device_get_name(dev), 530195534Sscottl device_get_unit(dev), "msi", &msi); 531195534Sscottl if (msi < 0) 532195534Sscottl msi = 0; 533195534Sscottl else if (msi == 1) 534195534Sscottl msi = min(1, pci_msi_count(dev)); 535195534Sscottl else if (msi > 1) 536195534Sscottl msi = pci_msi_count(dev); 537195534Sscottl /* Allocate MSI if needed/present. */ 538195534Sscottl if (msi && pci_alloc_msi(dev, &msi) == 0) { 539195534Sscottl ctlr->numirqs = msi; 540195534Sscottl } else { 541195534Sscottl msi = 0; 542195534Sscottl ctlr->numirqs = 1; 543195534Sscottl } 544195534Sscottl /* Check for single MSI vector fallback. */ 545195534Sscottl if (ctlr->numirqs > 1 && 546195534Sscottl (ATA_INL(ctlr->r_mem, AHCI_GHC) & AHCI_GHC_MRSM) != 0) { 547195534Sscottl device_printf(dev, "Falling back to one MSI\n"); 548195534Sscottl ctlr->numirqs = 1; 549195534Sscottl } 550195534Sscottl /* Allocate all IRQs. */ 551195534Sscottl for (i = 0; i < ctlr->numirqs; i++) { 552195534Sscottl ctlr->irqs[i].ctlr = ctlr; 553195534Sscottl ctlr->irqs[i].r_irq_rid = i + (msi ? 1 : 0); 554196656Smav if (ctlr->numirqs == 1 || i >= ctlr->channels || 555196656Smav (ctlr->ccc && i == ctlr->cccv)) 556195534Sscottl ctlr->irqs[i].mode = AHCI_IRQ_MODE_ALL; 557195534Sscottl else if (i == ctlr->numirqs - 1) 558195534Sscottl ctlr->irqs[i].mode = AHCI_IRQ_MODE_AFTER; 559195534Sscottl else 560195534Sscottl ctlr->irqs[i].mode = AHCI_IRQ_MODE_ONE; 561195534Sscottl if (!(ctlr->irqs[i].r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 562195534Sscottl &ctlr->irqs[i].r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) { 563195534Sscottl device_printf(dev, "unable to map interrupt\n"); 564195534Sscottl return ENXIO; 565195534Sscottl } 566195534Sscottl if ((bus_setup_intr(dev, ctlr->irqs[i].r_irq, ATA_INTR_FLAGS, NULL, 567195534Sscottl (ctlr->irqs[i].mode == AHCI_IRQ_MODE_ONE) ? ahci_intr_one : ahci_intr, 568195534Sscottl &ctlr->irqs[i], &ctlr->irqs[i].handle))) { 569195534Sscottl /* SOS XXX release r_irq */ 570195534Sscottl device_printf(dev, "unable to setup interrupt\n"); 571195534Sscottl return ENXIO; 572195534Sscottl } 573195534Sscottl } 574195534Sscottl return (0); 575195534Sscottl} 576195534Sscottl 577195534Sscottl/* 578195534Sscottl * Common case interrupt handler. 579195534Sscottl */ 580195534Sscottlstatic void 581195534Sscottlahci_intr(void *data) 582195534Sscottl{ 583195534Sscottl struct ahci_controller_irq *irq = data; 584195534Sscottl struct ahci_controller *ctlr = irq->ctlr; 585195534Sscottl u_int32_t is; 586195534Sscottl void *arg; 587195534Sscottl int unit; 588195534Sscottl 589196656Smav if (irq->mode == AHCI_IRQ_MODE_ALL) { 590195534Sscottl unit = 0; 591196656Smav if (ctlr->ccc) 592196656Smav is = ctlr->ichannels; 593196656Smav else 594196656Smav is = ATA_INL(ctlr->r_mem, AHCI_IS); 595196656Smav } else { /* AHCI_IRQ_MODE_AFTER */ 596195534Sscottl unit = irq->r_irq_rid - 1; 597196656Smav is = ATA_INL(ctlr->r_mem, AHCI_IS); 598196656Smav } 599195534Sscottl for (; unit < ctlr->channels; unit++) { 600195534Sscottl if ((is & (1 << unit)) != 0 && 601195534Sscottl (arg = ctlr->interrupt[unit].argument)) { 602199322Smav if (ctlr->quirks & AHCI_Q_EDGEIS) { 603199322Smav /* Some controller have edge triggered IS. */ 604199322Smav ATA_OUTL(ctlr->r_mem, AHCI_IS, 1 << unit); 605199322Smav ctlr->interrupt[unit].function(arg); 606199322Smav } else { 607199322Smav /* but AHCI declares level triggered IS. */ 608199322Smav ctlr->interrupt[unit].function(arg); 609199322Smav ATA_OUTL(ctlr->r_mem, AHCI_IS, 1 << unit); 610199322Smav } 611195534Sscottl } 612195534Sscottl } 613195534Sscottl} 614195534Sscottl 615195534Sscottl/* 616195534Sscottl * Simplified interrupt handler for multivector MSI mode. 617195534Sscottl */ 618195534Sscottlstatic void 619195534Sscottlahci_intr_one(void *data) 620195534Sscottl{ 621195534Sscottl struct ahci_controller_irq *irq = data; 622195534Sscottl struct ahci_controller *ctlr = irq->ctlr; 623195534Sscottl void *arg; 624195534Sscottl int unit; 625195534Sscottl 626195534Sscottl unit = irq->r_irq_rid - 1; 627195534Sscottl if ((arg = ctlr->interrupt[unit].argument)) 628195534Sscottl ctlr->interrupt[unit].function(arg); 629195534Sscottl} 630195534Sscottl 631195534Sscottlstatic struct resource * 632195534Sscottlahci_alloc_resource(device_t dev, device_t child, int type, int *rid, 633195534Sscottl u_long start, u_long end, u_long count, u_int flags) 634195534Sscottl{ 635195534Sscottl struct ahci_controller *ctlr = device_get_softc(dev); 636195534Sscottl int unit = ((struct ahci_channel *)device_get_softc(child))->unit; 637195534Sscottl struct resource *res = NULL; 638195534Sscottl int offset = AHCI_OFFSET + (unit << 7); 639195534Sscottl long st; 640195534Sscottl 641195534Sscottl switch (type) { 642195534Sscottl case SYS_RES_MEMORY: 643195534Sscottl st = rman_get_start(ctlr->r_mem); 644195534Sscottl res = rman_reserve_resource(&ctlr->sc_iomem, st + offset, 645195534Sscottl st + offset + 127, 128, RF_ACTIVE, child); 646195534Sscottl if (res) { 647195534Sscottl bus_space_handle_t bsh; 648195534Sscottl bus_space_tag_t bst; 649195534Sscottl bsh = rman_get_bushandle(ctlr->r_mem); 650195534Sscottl bst = rman_get_bustag(ctlr->r_mem); 651195534Sscottl bus_space_subregion(bst, bsh, offset, 128, &bsh); 652195534Sscottl rman_set_bushandle(res, bsh); 653195534Sscottl rman_set_bustag(res, bst); 654195534Sscottl } 655195534Sscottl break; 656195534Sscottl case SYS_RES_IRQ: 657195534Sscottl if (*rid == ATA_IRQ_RID) 658195534Sscottl res = ctlr->irqs[0].r_irq; 659195534Sscottl break; 660195534Sscottl } 661195534Sscottl return (res); 662195534Sscottl} 663195534Sscottl 664195534Sscottlstatic int 665195534Sscottlahci_release_resource(device_t dev, device_t child, int type, int rid, 666195534Sscottl struct resource *r) 667195534Sscottl{ 668195534Sscottl 669195534Sscottl switch (type) { 670195534Sscottl case SYS_RES_MEMORY: 671195534Sscottl rman_release_resource(r); 672195534Sscottl return (0); 673195534Sscottl case SYS_RES_IRQ: 674195534Sscottl if (rid != ATA_IRQ_RID) 675195534Sscottl return ENOENT; 676195534Sscottl return (0); 677195534Sscottl } 678195534Sscottl return (EINVAL); 679195534Sscottl} 680195534Sscottl 681195534Sscottlstatic int 682195534Sscottlahci_setup_intr(device_t dev, device_t child, struct resource *irq, 683195534Sscottl int flags, driver_filter_t *filter, driver_intr_t *function, 684195534Sscottl void *argument, void **cookiep) 685195534Sscottl{ 686195534Sscottl struct ahci_controller *ctlr = device_get_softc(dev); 687195534Sscottl int unit = (intptr_t)device_get_ivars(child); 688195534Sscottl 689195534Sscottl if (filter != NULL) { 690195534Sscottl printf("ahci.c: we cannot use a filter here\n"); 691195534Sscottl return (EINVAL); 692195534Sscottl } 693195534Sscottl ctlr->interrupt[unit].function = function; 694195534Sscottl ctlr->interrupt[unit].argument = argument; 695195534Sscottl return (0); 696195534Sscottl} 697195534Sscottl 698195534Sscottlstatic int 699195534Sscottlahci_teardown_intr(device_t dev, device_t child, struct resource *irq, 700195534Sscottl void *cookie) 701195534Sscottl{ 702195534Sscottl struct ahci_controller *ctlr = device_get_softc(dev); 703195534Sscottl int unit = (intptr_t)device_get_ivars(child); 704195534Sscottl 705195534Sscottl ctlr->interrupt[unit].function = NULL; 706195534Sscottl ctlr->interrupt[unit].argument = NULL; 707195534Sscottl return (0); 708195534Sscottl} 709195534Sscottl 710195534Sscottlstatic int 711195534Sscottlahci_print_child(device_t dev, device_t child) 712195534Sscottl{ 713195534Sscottl int retval; 714195534Sscottl 715195534Sscottl retval = bus_print_child_header(dev, child); 716195534Sscottl retval += printf(" at channel %d", 717195534Sscottl (int)(intptr_t)device_get_ivars(child)); 718195534Sscottl retval += bus_print_child_footer(dev, child); 719195534Sscottl 720195534Sscottl return (retval); 721195534Sscottl} 722195534Sscottl 723195534Sscottldevclass_t ahci_devclass; 724195534Sscottlstatic device_method_t ahci_methods[] = { 725195534Sscottl DEVMETHOD(device_probe, ahci_probe), 726195534Sscottl DEVMETHOD(device_attach, ahci_attach), 727195534Sscottl DEVMETHOD(device_detach, ahci_detach), 728195534Sscottl DEVMETHOD(device_suspend, ahci_suspend), 729195534Sscottl DEVMETHOD(device_resume, ahci_resume), 730195534Sscottl DEVMETHOD(bus_print_child, ahci_print_child), 731195534Sscottl DEVMETHOD(bus_alloc_resource, ahci_alloc_resource), 732195534Sscottl DEVMETHOD(bus_release_resource, ahci_release_resource), 733195534Sscottl DEVMETHOD(bus_setup_intr, ahci_setup_intr), 734195534Sscottl DEVMETHOD(bus_teardown_intr,ahci_teardown_intr), 735195534Sscottl { 0, 0 } 736195534Sscottl}; 737195534Sscottlstatic driver_t ahci_driver = { 738195534Sscottl "ahci", 739195534Sscottl ahci_methods, 740195534Sscottl sizeof(struct ahci_controller) 741195534Sscottl}; 742195534SscottlDRIVER_MODULE(ahci, pci, ahci_driver, ahci_devclass, 0, 0); 743199322Smavstatic device_method_t ahci_ata_methods[] = { 744199322Smav DEVMETHOD(device_probe, ahci_ata_probe), 745199322Smav DEVMETHOD(device_attach, ahci_attach), 746199322Smav DEVMETHOD(device_detach, ahci_detach), 747199322Smav DEVMETHOD(device_suspend, ahci_suspend), 748199322Smav DEVMETHOD(device_resume, ahci_resume), 749199322Smav DEVMETHOD(bus_print_child, ahci_print_child), 750199322Smav DEVMETHOD(bus_alloc_resource, ahci_alloc_resource), 751199322Smav DEVMETHOD(bus_release_resource, ahci_release_resource), 752199322Smav DEVMETHOD(bus_setup_intr, ahci_setup_intr), 753199322Smav DEVMETHOD(bus_teardown_intr,ahci_teardown_intr), 754199322Smav { 0, 0 } 755199322Smav}; 756199322Smavstatic driver_t ahci_ata_driver = { 757199322Smav "ahci", 758199322Smav ahci_ata_methods, 759199322Smav sizeof(struct ahci_controller) 760199322Smav}; 761199322SmavDRIVER_MODULE(ahci, atapci, ahci_ata_driver, ahci_devclass, 0, 0); 762195534SscottlMODULE_VERSION(ahci, 1); 763195534SscottlMODULE_DEPEND(ahci, cam, 1, 1, 1); 764195534Sscottl 765195534Sscottlstatic int 766195534Sscottlahci_ch_probe(device_t dev) 767195534Sscottl{ 768195534Sscottl 769195534Sscottl device_set_desc_copy(dev, "AHCI channel"); 770195534Sscottl return (0); 771195534Sscottl} 772195534Sscottl 773195534Sscottlstatic int 774195534Sscottlahci_ch_attach(device_t dev) 775195534Sscottl{ 776195534Sscottl struct ahci_controller *ctlr = device_get_softc(device_get_parent(dev)); 777195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 778195534Sscottl struct cam_devq *devq; 779195534Sscottl int rid, error; 780195534Sscottl 781195534Sscottl ch->dev = dev; 782195534Sscottl ch->unit = (intptr_t)device_get_ivars(dev); 783196656Smav ch->caps = ctlr->caps; 784196656Smav ch->caps2 = ctlr->caps2; 785199322Smav ch->quirks = ctlr->quirks; 786195534Sscottl ch->numslots = ((ch->caps & AHCI_CAP_NCS) >> AHCI_CAP_NCS_SHIFT) + 1, 787196656Smav mtx_init(&ch->mtx, "AHCI channel lock", NULL, MTX_DEF); 788195534Sscottl resource_int_value(device_get_name(dev), 789195534Sscottl device_get_unit(dev), "pm_level", &ch->pm_level); 790196656Smav if (ch->pm_level > 3) 791196656Smav callout_init_mtx(&ch->pm_timer, &ch->mtx, 0); 792195534Sscottl /* Limit speed for my onboard JMicron external port. 793195534Sscottl * It is not eSATA really. */ 794195534Sscottl if (pci_get_devid(ctlr->dev) == 0x2363197b && 795195534Sscottl pci_get_subvendor(ctlr->dev) == 0x1043 && 796195534Sscottl pci_get_subdevice(ctlr->dev) == 0x81e4 && 797195534Sscottl ch->unit == 0) 798195534Sscottl ch->sata_rev = 1; 799195534Sscottl resource_int_value(device_get_name(dev), 800195534Sscottl device_get_unit(dev), "sata_rev", &ch->sata_rev); 801195534Sscottl rid = ch->unit; 802195534Sscottl if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 803195534Sscottl &rid, RF_ACTIVE))) 804195534Sscottl return (ENXIO); 805195534Sscottl ahci_dmainit(dev); 806195534Sscottl ahci_slotsalloc(dev); 807195534Sscottl ahci_ch_resume(dev); 808195534Sscottl mtx_lock(&ch->mtx); 809195534Sscottl rid = ATA_IRQ_RID; 810195534Sscottl if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 811195534Sscottl &rid, RF_SHAREABLE | RF_ACTIVE))) { 812195534Sscottl bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 813195534Sscottl device_printf(dev, "Unable to map interrupt\n"); 814195534Sscottl return (ENXIO); 815195534Sscottl } 816195534Sscottl if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL, 817195534Sscottl ahci_ch_intr_locked, dev, &ch->ih))) { 818195534Sscottl device_printf(dev, "Unable to setup interrupt\n"); 819195534Sscottl error = ENXIO; 820195534Sscottl goto err1; 821195534Sscottl } 822195534Sscottl /* Create the device queue for our SIM. */ 823195534Sscottl devq = cam_simq_alloc(ch->numslots); 824195534Sscottl if (devq == NULL) { 825195534Sscottl device_printf(dev, "Unable to allocate simq\n"); 826195534Sscottl error = ENOMEM; 827195534Sscottl goto err1; 828195534Sscottl } 829195534Sscottl /* Construct SIM entry */ 830195534Sscottl ch->sim = cam_sim_alloc(ahciaction, ahcipoll, "ahcich", ch, 831199178Smav device_get_unit(dev), &ch->mtx, 832199278Smav min(2, ch->numslots), 833199278Smav (ch->caps & AHCI_CAP_SNCQ) ? ch->numslots : 0, 834199278Smav devq); 835195534Sscottl if (ch->sim == NULL) { 836195534Sscottl device_printf(dev, "unable to allocate sim\n"); 837195534Sscottl error = ENOMEM; 838195534Sscottl goto err2; 839195534Sscottl } 840195534Sscottl if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) { 841195534Sscottl device_printf(dev, "unable to register xpt bus\n"); 842195534Sscottl error = ENXIO; 843195534Sscottl goto err2; 844195534Sscottl } 845195534Sscottl if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim), 846195534Sscottl CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 847195534Sscottl device_printf(dev, "unable to create path\n"); 848195534Sscottl error = ENXIO; 849195534Sscottl goto err3; 850195534Sscottl } 851196656Smav if (ch->pm_level > 3) { 852196656Smav callout_reset(&ch->pm_timer, 853196656Smav (ch->pm_level == 4) ? hz / 1000 : hz / 8, 854196656Smav ahci_ch_pm, dev); 855196656Smav } 856195534Sscottl mtx_unlock(&ch->mtx); 857195534Sscottl return (0); 858195534Sscottl 859195534Sscottlerr3: 860195534Sscottl xpt_bus_deregister(cam_sim_path(ch->sim)); 861195534Sscottlerr2: 862195534Sscottl cam_sim_free(ch->sim, /*free_devq*/TRUE); 863195534Sscottlerr1: 864195534Sscottl bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 865195534Sscottl bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 866195534Sscottl mtx_unlock(&ch->mtx); 867195534Sscottl return (error); 868195534Sscottl} 869195534Sscottl 870195534Sscottlstatic int 871195534Sscottlahci_ch_detach(device_t dev) 872195534Sscottl{ 873195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 874195534Sscottl 875195534Sscottl mtx_lock(&ch->mtx); 876195534Sscottl xpt_async(AC_LOST_DEVICE, ch->path, NULL); 877195534Sscottl xpt_free_path(ch->path); 878195534Sscottl xpt_bus_deregister(cam_sim_path(ch->sim)); 879195534Sscottl cam_sim_free(ch->sim, /*free_devq*/TRUE); 880195534Sscottl mtx_unlock(&ch->mtx); 881195534Sscottl 882196656Smav if (ch->pm_level > 3) 883196656Smav callout_drain(&ch->pm_timer); 884195534Sscottl bus_teardown_intr(dev, ch->r_irq, ch->ih); 885195534Sscottl bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 886195534Sscottl 887195534Sscottl ahci_ch_suspend(dev); 888195534Sscottl ahci_slotsfree(dev); 889195534Sscottl ahci_dmafini(dev); 890195534Sscottl 891195534Sscottl bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 892195534Sscottl mtx_destroy(&ch->mtx); 893195534Sscottl return (0); 894195534Sscottl} 895195534Sscottl 896195534Sscottlstatic int 897195534Sscottlahci_ch_suspend(device_t dev) 898195534Sscottl{ 899195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 900195534Sscottl 901195534Sscottl /* Disable port interrupts. */ 902195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_IE, 0); 903195534Sscottl /* Reset command register. */ 904195534Sscottl ahci_stop(dev); 905195534Sscottl ahci_stop_fr(dev); 906195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CMD, 0); 907195534Sscottl /* Allow everything, including partial and slumber modes. */ 908195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_SCTL, 0); 909195534Sscottl /* Request slumber mode transition and give some time to get there. */ 910195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CMD, AHCI_P_CMD_SLUMBER); 911195534Sscottl DELAY(100); 912195534Sscottl /* Disable PHY. */ 913195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_SCTL, ATA_SC_DET_DISABLE); 914195534Sscottl return (0); 915195534Sscottl} 916195534Sscottl 917195534Sscottlstatic int 918195534Sscottlahci_ch_resume(device_t dev) 919195534Sscottl{ 920195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 921195534Sscottl uint64_t work; 922195534Sscottl 923195534Sscottl /* Disable port interrupts */ 924195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_IE, 0); 925195534Sscottl /* Setup work areas */ 926195534Sscottl work = ch->dma.work_bus + AHCI_CL_OFFSET; 927195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CLB, work & 0xffffffff); 928195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CLBU, work >> 32); 929195534Sscottl work = ch->dma.rfis_bus; 930195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_FB, work & 0xffffffff); 931195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_FBU, work >> 32); 932195534Sscottl /* Activate the channel and power/spin up device */ 933195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CMD, 934195534Sscottl (AHCI_P_CMD_ACTIVE | AHCI_P_CMD_POD | AHCI_P_CMD_SUD | 935196656Smav ((ch->pm_level == 2 || ch->pm_level == 3) ? AHCI_P_CMD_ALPE : 0) | 936195534Sscottl ((ch->pm_level > 2) ? AHCI_P_CMD_ASP : 0 ))); 937195534Sscottl ahci_start_fr(dev); 938195534Sscottl ahci_start(dev); 939195534Sscottl return (0); 940195534Sscottl} 941195534Sscottl 942195534Sscottldevclass_t ahcich_devclass; 943195534Sscottlstatic device_method_t ahcich_methods[] = { 944195534Sscottl DEVMETHOD(device_probe, ahci_ch_probe), 945195534Sscottl DEVMETHOD(device_attach, ahci_ch_attach), 946195534Sscottl DEVMETHOD(device_detach, ahci_ch_detach), 947195534Sscottl DEVMETHOD(device_suspend, ahci_ch_suspend), 948195534Sscottl DEVMETHOD(device_resume, ahci_ch_resume), 949195534Sscottl { 0, 0 } 950195534Sscottl}; 951195534Sscottlstatic driver_t ahcich_driver = { 952195534Sscottl "ahcich", 953195534Sscottl ahcich_methods, 954195534Sscottl sizeof(struct ahci_channel) 955195534Sscottl}; 956199322SmavDRIVER_MODULE(ahcich, ahci, ahcich_driver, ahcich_devclass, 0, 0); 957195534Sscottl 958195534Sscottlstruct ahci_dc_cb_args { 959195534Sscottl bus_addr_t maddr; 960195534Sscottl int error; 961195534Sscottl}; 962195534Sscottl 963195534Sscottlstatic void 964195534Sscottlahci_dmainit(device_t dev) 965195534Sscottl{ 966195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 967195534Sscottl struct ahci_dc_cb_args dcba; 968195534Sscottl 969195534Sscottl if (ch->caps & AHCI_CAP_64BIT) 970195534Sscottl ch->dma.max_address = BUS_SPACE_MAXADDR; 971195534Sscottl else 972195534Sscottl ch->dma.max_address = BUS_SPACE_MAXADDR_32BIT; 973195534Sscottl /* Command area. */ 974195534Sscottl if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0, 975195534Sscottl ch->dma.max_address, BUS_SPACE_MAXADDR, 976195534Sscottl NULL, NULL, AHCI_WORK_SIZE, 1, AHCI_WORK_SIZE, 977195534Sscottl 0, NULL, NULL, &ch->dma.work_tag)) 978195534Sscottl goto error; 979195534Sscottl if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0, 980195534Sscottl &ch->dma.work_map)) 981195534Sscottl goto error; 982195534Sscottl if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work, 983195534Sscottl AHCI_WORK_SIZE, ahci_dmasetupc_cb, &dcba, 0) || dcba.error) { 984195534Sscottl bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map); 985195534Sscottl goto error; 986195534Sscottl } 987195534Sscottl ch->dma.work_bus = dcba.maddr; 988195534Sscottl /* FIS receive area. */ 989195534Sscottl if (bus_dma_tag_create(bus_get_dma_tag(dev), 4096, 0, 990195534Sscottl ch->dma.max_address, BUS_SPACE_MAXADDR, 991195534Sscottl NULL, NULL, 4096, 1, 4096, 992195534Sscottl 0, NULL, NULL, &ch->dma.rfis_tag)) 993195534Sscottl goto error; 994195534Sscottl if (bus_dmamem_alloc(ch->dma.rfis_tag, (void **)&ch->dma.rfis, 0, 995195534Sscottl &ch->dma.rfis_map)) 996195534Sscottl goto error; 997195534Sscottl if (bus_dmamap_load(ch->dma.rfis_tag, ch->dma.rfis_map, ch->dma.rfis, 998195534Sscottl 4096, ahci_dmasetupc_cb, &dcba, 0) || dcba.error) { 999195534Sscottl bus_dmamem_free(ch->dma.rfis_tag, ch->dma.rfis, ch->dma.rfis_map); 1000195534Sscottl goto error; 1001195534Sscottl } 1002195534Sscottl ch->dma.rfis_bus = dcba.maddr; 1003195534Sscottl /* Data area. */ 1004195534Sscottl if (bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0, 1005195534Sscottl ch->dma.max_address, BUS_SPACE_MAXADDR, 1006195534Sscottl NULL, NULL, 1007195534Sscottl AHCI_SG_ENTRIES * PAGE_SIZE * ch->numslots, 1008195534Sscottl AHCI_SG_ENTRIES, AHCI_PRD_MAX, 1009195534Sscottl 0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) { 1010195534Sscottl goto error; 1011195534Sscottl } 1012195534Sscottl return; 1013195534Sscottl 1014195534Sscottlerror: 1015195534Sscottl device_printf(dev, "WARNING - DMA initialization failed\n"); 1016195534Sscottl ahci_dmafini(dev); 1017195534Sscottl} 1018195534Sscottl 1019195534Sscottlstatic void 1020195534Sscottlahci_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 1021195534Sscottl{ 1022195534Sscottl struct ahci_dc_cb_args *dcba = (struct ahci_dc_cb_args *)xsc; 1023195534Sscottl 1024195534Sscottl if (!(dcba->error = error)) 1025195534Sscottl dcba->maddr = segs[0].ds_addr; 1026195534Sscottl} 1027195534Sscottl 1028195534Sscottlstatic void 1029195534Sscottlahci_dmafini(device_t dev) 1030195534Sscottl{ 1031195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1032195534Sscottl 1033195534Sscottl if (ch->dma.data_tag) { 1034195534Sscottl bus_dma_tag_destroy(ch->dma.data_tag); 1035195534Sscottl ch->dma.data_tag = NULL; 1036195534Sscottl } 1037195534Sscottl if (ch->dma.rfis_bus) { 1038195534Sscottl bus_dmamap_unload(ch->dma.rfis_tag, ch->dma.rfis_map); 1039195534Sscottl bus_dmamem_free(ch->dma.rfis_tag, ch->dma.rfis, ch->dma.rfis_map); 1040195534Sscottl ch->dma.rfis_bus = 0; 1041195534Sscottl ch->dma.rfis_map = NULL; 1042195534Sscottl ch->dma.rfis = NULL; 1043195534Sscottl } 1044195534Sscottl if (ch->dma.work_bus) { 1045195534Sscottl bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map); 1046195534Sscottl bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map); 1047195534Sscottl ch->dma.work_bus = 0; 1048195534Sscottl ch->dma.work_map = NULL; 1049195534Sscottl ch->dma.work = NULL; 1050195534Sscottl } 1051195534Sscottl if (ch->dma.work_tag) { 1052195534Sscottl bus_dma_tag_destroy(ch->dma.work_tag); 1053195534Sscottl ch->dma.work_tag = NULL; 1054195534Sscottl } 1055195534Sscottl} 1056195534Sscottl 1057195534Sscottlstatic void 1058195534Sscottlahci_slotsalloc(device_t dev) 1059195534Sscottl{ 1060195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1061195534Sscottl int i; 1062195534Sscottl 1063195534Sscottl /* Alloc and setup command/dma slots */ 1064195534Sscottl bzero(ch->slot, sizeof(ch->slot)); 1065195534Sscottl for (i = 0; i < ch->numslots; i++) { 1066195534Sscottl struct ahci_slot *slot = &ch->slot[i]; 1067195534Sscottl 1068195534Sscottl slot->dev = dev; 1069195534Sscottl slot->slot = i; 1070195534Sscottl slot->state = AHCI_SLOT_EMPTY; 1071195534Sscottl slot->ccb = NULL; 1072195534Sscottl callout_init_mtx(&slot->timeout, &ch->mtx, 0); 1073195534Sscottl 1074195534Sscottl if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map)) 1075195534Sscottl device_printf(ch->dev, "FAILURE - create data_map\n"); 1076195534Sscottl } 1077195534Sscottl} 1078195534Sscottl 1079195534Sscottlstatic void 1080195534Sscottlahci_slotsfree(device_t dev) 1081195534Sscottl{ 1082195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1083195534Sscottl int i; 1084195534Sscottl 1085195534Sscottl /* Free all dma slots */ 1086195534Sscottl for (i = 0; i < ch->numslots; i++) { 1087195534Sscottl struct ahci_slot *slot = &ch->slot[i]; 1088195534Sscottl 1089196656Smav callout_drain(&slot->timeout); 1090195534Sscottl if (slot->dma.data_map) { 1091195534Sscottl bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map); 1092195534Sscottl slot->dma.data_map = NULL; 1093195534Sscottl } 1094195534Sscottl } 1095195534Sscottl} 1096195534Sscottl 1097195534Sscottlstatic void 1098198319Smavahci_phy_check_events(device_t dev, u_int32_t serr) 1099195534Sscottl{ 1100195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1101195534Sscottl 1102198319Smav if ((serr & ATA_SE_PHY_CHANGED) && (ch->pm_level == 0)) { 1103195534Sscottl u_int32_t status = ATA_INL(ch->r_mem, AHCI_P_SSTS); 1104195534Sscottl if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) && 1105195534Sscottl ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) && 1106195534Sscottl ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) { 1107195534Sscottl if (bootverbose) 1108195534Sscottl device_printf(dev, "CONNECT requested\n"); 1109195534Sscottl ahci_reset(dev); 1110195534Sscottl } else { 1111195534Sscottl if (bootverbose) 1112195534Sscottl device_printf(dev, "DISCONNECT requested\n"); 1113195534Sscottl ch->devices = 0; 1114195534Sscottl } 1115195534Sscottl } 1116195534Sscottl} 1117195534Sscottl 1118195534Sscottlstatic void 1119196907Smavahci_notify_events(device_t dev, u_int32_t status) 1120196656Smav{ 1121196656Smav struct ahci_channel *ch = device_get_softc(dev); 1122196656Smav struct cam_path *dpath; 1123196656Smav int i; 1124196656Smav 1125196656Smav ATA_OUTL(ch->r_mem, AHCI_P_SNTF, status); 1126196656Smav if (bootverbose) 1127196656Smav device_printf(dev, "SNTF 0x%04x\n", status); 1128196656Smav for (i = 0; i < 16; i++) { 1129196656Smav if ((status & (1 << i)) == 0) 1130196656Smav continue; 1131196656Smav if (xpt_create_path(&dpath, NULL, 1132196656Smav xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) { 1133196656Smav xpt_async(AC_SCSI_AEN, dpath, NULL); 1134196656Smav xpt_free_path(dpath); 1135196656Smav } 1136196656Smav } 1137196656Smav} 1138196656Smav 1139196656Smavstatic void 1140195534Sscottlahci_ch_intr_locked(void *data) 1141195534Sscottl{ 1142195534Sscottl device_t dev = (device_t)data; 1143195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1144195534Sscottl 1145195534Sscottl mtx_lock(&ch->mtx); 1146195534Sscottl ahci_ch_intr(data); 1147195534Sscottl mtx_unlock(&ch->mtx); 1148195534Sscottl} 1149195534Sscottl 1150195534Sscottlstatic void 1151196656Smavahci_ch_pm(void *arg) 1152196656Smav{ 1153196656Smav device_t dev = (device_t)arg; 1154196656Smav struct ahci_channel *ch = device_get_softc(dev); 1155196656Smav uint32_t work; 1156196656Smav 1157196656Smav if (ch->numrslots != 0) 1158196656Smav return; 1159196656Smav work = ATA_INL(ch->r_mem, AHCI_P_CMD); 1160196656Smav if (ch->pm_level == 4) 1161196656Smav work |= AHCI_P_CMD_PARTIAL; 1162196656Smav else 1163196656Smav work |= AHCI_P_CMD_SLUMBER; 1164196656Smav ATA_OUTL(ch->r_mem, AHCI_P_CMD, work); 1165196656Smav} 1166196656Smav 1167196656Smavstatic void 1168195534Sscottlahci_ch_intr(void *data) 1169195534Sscottl{ 1170195534Sscottl device_t dev = (device_t)data; 1171195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1172198319Smav uint32_t istatus, sstatus, cstatus, serr = 0, sntf = 0, ok, err; 1173195534Sscottl enum ahci_err_type et; 1174195534Sscottl int i, ccs, ncq_err = 0; 1175195534Sscottl 1176195534Sscottl /* Read and clear interrupt statuses. */ 1177195534Sscottl istatus = ATA_INL(ch->r_mem, AHCI_P_IS); 1178196656Smav if (istatus == 0) 1179196656Smav return; 1180195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_IS, istatus); 1181195534Sscottl /* Read command statuses. */ 1182196907Smav sstatus = ATA_INL(ch->r_mem, AHCI_P_SACT); 1183195534Sscottl cstatus = ATA_INL(ch->r_mem, AHCI_P_CI); 1184196907Smav if ((istatus & AHCI_P_IX_SDB) && (ch->caps & AHCI_CAP_SSNTF)) 1185196907Smav sntf = ATA_INL(ch->r_mem, AHCI_P_SNTF); 1186195534Sscottl /* Process PHY events */ 1187198319Smav if (istatus & (AHCI_P_IX_PC | AHCI_P_IX_PRC | AHCI_P_IX_OF | 1188198319Smav AHCI_P_IX_IF | AHCI_P_IX_HBD | AHCI_P_IX_HBF | AHCI_P_IX_TFE)) { 1189198319Smav serr = ATA_INL(ch->r_mem, AHCI_P_SERR); 1190198319Smav if (serr) { 1191198319Smav ATA_OUTL(ch->r_mem, AHCI_P_SERR, serr); 1192198319Smav ahci_phy_check_events(dev, serr); 1193198319Smav } 1194198319Smav } 1195195534Sscottl /* Process command errors */ 1196198319Smav if (istatus & (AHCI_P_IX_OF | AHCI_P_IX_IF | 1197198319Smav AHCI_P_IX_HBD | AHCI_P_IX_HBF | AHCI_P_IX_TFE)) { 1198195534Sscottl//device_printf(dev, "%s ERROR is %08x cs %08x ss %08x rs %08x tfd %02x serr %08x\n", 1199195534Sscottl// __func__, istatus, cstatus, sstatus, ch->rslots, ATA_INL(ch->r_mem, AHCI_P_TFD), 1200198319Smav// serr); 1201195534Sscottl ccs = (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CCS_MASK) 1202195534Sscottl >> AHCI_P_CMD_CCS_SHIFT; 1203196656Smav err = ch->rslots & (cstatus | sstatus); 1204195534Sscottl /* Kick controller into sane state */ 1205195534Sscottl ahci_stop(dev); 1206195534Sscottl ahci_start(dev); 1207195534Sscottl } else { 1208195534Sscottl ccs = 0; 1209195534Sscottl err = 0; 1210195534Sscottl } 1211195534Sscottl /* Complete all successfull commands. */ 1212196656Smav ok = ch->rslots & ~(cstatus | sstatus); 1213195534Sscottl for (i = 0; i < ch->numslots; i++) { 1214195534Sscottl if ((ok >> i) & 1) 1215195534Sscottl ahci_end_transaction(&ch->slot[i], AHCI_ERR_NONE); 1216195534Sscottl } 1217195534Sscottl /* On error, complete the rest of commands with error statuses. */ 1218195534Sscottl if (err) { 1219195534Sscottl if (ch->frozen) { 1220195534Sscottl union ccb *fccb = ch->frozen; 1221195534Sscottl ch->frozen = NULL; 1222195534Sscottl fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 1223198319Smav if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 1224198319Smav xpt_freeze_devq(fccb->ccb_h.path, 1); 1225198319Smav fccb->ccb_h.status |= CAM_DEV_QFRZN; 1226198319Smav } 1227195534Sscottl xpt_done(fccb); 1228195534Sscottl } 1229195534Sscottl for (i = 0; i < ch->numslots; i++) { 1230195534Sscottl /* XXX: reqests in loading state. */ 1231195534Sscottl if (((err >> i) & 1) == 0) 1232195534Sscottl continue; 1233198390Smav if (istatus & AHCI_P_IX_TFE) { 1234195534Sscottl /* Task File Error */ 1235195534Sscottl if (ch->numtslots == 0) { 1236195534Sscottl /* Untagged operation. */ 1237195534Sscottl if (i == ccs) 1238195534Sscottl et = AHCI_ERR_TFE; 1239195534Sscottl else 1240195534Sscottl et = AHCI_ERR_INNOCENT; 1241195534Sscottl } else { 1242195534Sscottl /* Tagged operation. */ 1243195534Sscottl et = AHCI_ERR_NCQ; 1244195534Sscottl ncq_err = 1; 1245195534Sscottl } 1246198390Smav } else if (istatus & AHCI_P_IX_IF) { 1247198390Smav if (ch->numtslots == 0 && i != ccs) 1248198390Smav et = AHCI_ERR_INNOCENT; 1249198390Smav else 1250198390Smav et = AHCI_ERR_SATA; 1251195534Sscottl } else 1252195534Sscottl et = AHCI_ERR_INVALID; 1253195534Sscottl ahci_end_transaction(&ch->slot[i], et); 1254195534Sscottl } 1255195534Sscottl if (ncq_err) 1256195534Sscottl ahci_issue_read_log(dev); 1257195534Sscottl } 1258196656Smav /* Process NOTIFY events */ 1259196907Smav if (sntf) 1260196907Smav ahci_notify_events(dev, sntf); 1261195534Sscottl} 1262195534Sscottl 1263195534Sscottl/* Must be called with channel locked. */ 1264195534Sscottlstatic int 1265195534Sscottlahci_check_collision(device_t dev, union ccb *ccb) 1266195534Sscottl{ 1267195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1268195534Sscottl 1269195534Sscottl if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1270195534Sscottl (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 1271195534Sscottl /* Tagged command while untagged are active. */ 1272195534Sscottl if (ch->numrslots != 0 && ch->numtslots == 0) 1273195534Sscottl return (1); 1274195534Sscottl /* Tagged command while tagged to other target is active. */ 1275195534Sscottl if (ch->numtslots != 0 && 1276195534Sscottl ch->taggedtarget != ccb->ccb_h.target_id) 1277195534Sscottl return (1); 1278195534Sscottl } else { 1279195534Sscottl /* Untagged command while tagged are active. */ 1280195534Sscottl if (ch->numrslots != 0 && ch->numtslots != 0) 1281195534Sscottl return (1); 1282195534Sscottl } 1283195534Sscottl if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1284195534Sscottl (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) { 1285195534Sscottl /* Atomic command while anything active. */ 1286195534Sscottl if (ch->numrslots != 0) 1287195534Sscottl return (1); 1288195534Sscottl } 1289195534Sscottl /* We have some atomic command running. */ 1290195534Sscottl if (ch->aslots != 0) 1291195534Sscottl return (1); 1292195534Sscottl return (0); 1293195534Sscottl} 1294195534Sscottl 1295195534Sscottl/* Must be called with channel locked. */ 1296195534Sscottlstatic void 1297195534Sscottlahci_begin_transaction(device_t dev, union ccb *ccb) 1298195534Sscottl{ 1299195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1300195534Sscottl struct ahci_slot *slot; 1301195534Sscottl int tag; 1302195534Sscottl 1303195534Sscottl /* Choose empty slot. */ 1304195534Sscottl tag = ch->lastslot; 1305196656Smav while (ch->slot[tag].state != AHCI_SLOT_EMPTY) { 1306196656Smav if (++tag >= ch->numslots) 1307195534Sscottl tag = 0; 1308196660Smav KASSERT(tag != ch->lastslot, ("ahci: ALL SLOTS BUSY!")); 1309196656Smav } 1310195534Sscottl ch->lastslot = tag; 1311195534Sscottl /* Occupy chosen slot. */ 1312195534Sscottl slot = &ch->slot[tag]; 1313195534Sscottl slot->ccb = ccb; 1314196656Smav /* Stop PM timer. */ 1315196656Smav if (ch->numrslots == 0 && ch->pm_level > 3) 1316196656Smav callout_stop(&ch->pm_timer); 1317195534Sscottl /* Update channel stats. */ 1318195534Sscottl ch->numrslots++; 1319195534Sscottl if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1320195534Sscottl (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 1321195534Sscottl ch->numtslots++; 1322195534Sscottl ch->taggedtarget = ccb->ccb_h.target_id; 1323195534Sscottl } 1324195534Sscottl if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1325195534Sscottl (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) 1326195534Sscottl ch->aslots |= (1 << slot->slot); 1327195534Sscottl slot->dma.nsegs = 0; 1328195534Sscottl /* If request moves data, setup and load SG list */ 1329195534Sscottl if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 1330195534Sscottl void *buf; 1331195534Sscottl bus_size_t size; 1332195534Sscottl 1333195534Sscottl slot->state = AHCI_SLOT_LOADING; 1334195534Sscottl if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1335195534Sscottl buf = ccb->ataio.data_ptr; 1336195534Sscottl size = ccb->ataio.dxfer_len; 1337195534Sscottl } else { 1338195534Sscottl buf = ccb->csio.data_ptr; 1339195534Sscottl size = ccb->csio.dxfer_len; 1340195534Sscottl } 1341195534Sscottl bus_dmamap_load(ch->dma.data_tag, slot->dma.data_map, 1342195534Sscottl buf, size, ahci_dmasetprd, slot, 0); 1343195534Sscottl } else 1344195534Sscottl ahci_execute_transaction(slot); 1345195534Sscottl} 1346195534Sscottl 1347195534Sscottl/* Locked by busdma engine. */ 1348195534Sscottlstatic void 1349195534Sscottlahci_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 1350195534Sscottl{ 1351195534Sscottl struct ahci_slot *slot = arg; 1352195534Sscottl struct ahci_channel *ch = device_get_softc(slot->dev); 1353195534Sscottl struct ahci_cmd_tab *ctp; 1354195534Sscottl struct ahci_dma_prd *prd; 1355195534Sscottl int i; 1356195534Sscottl 1357195534Sscottl if (error) { 1358195534Sscottl device_printf(slot->dev, "DMA load error\n"); 1359195534Sscottl ahci_end_transaction(slot, AHCI_ERR_INVALID); 1360195534Sscottl return; 1361195534Sscottl } 1362195534Sscottl KASSERT(nsegs <= AHCI_SG_ENTRIES, ("too many DMA segment entries\n")); 1363195534Sscottl /* Get a piece of the workspace for this request */ 1364195534Sscottl ctp = (struct ahci_cmd_tab *) 1365195534Sscottl (ch->dma.work + AHCI_CT_OFFSET + (AHCI_CT_SIZE * slot->slot)); 1366195534Sscottl /* Fill S/G table */ 1367195534Sscottl prd = &ctp->prd_tab[0]; 1368195534Sscottl for (i = 0; i < nsegs; i++) { 1369195534Sscottl prd[i].dba = htole64(segs[i].ds_addr); 1370195534Sscottl prd[i].dbc = htole32((segs[i].ds_len - 1) & AHCI_PRD_MASK); 1371195534Sscottl } 1372195534Sscottl slot->dma.nsegs = nsegs; 1373195534Sscottl bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 1374195534Sscottl ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ? 1375195534Sscottl BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE)); 1376195534Sscottl ahci_execute_transaction(slot); 1377195534Sscottl} 1378195534Sscottl 1379195534Sscottl/* Must be called with channel locked. */ 1380195534Sscottlstatic void 1381195534Sscottlahci_execute_transaction(struct ahci_slot *slot) 1382195534Sscottl{ 1383195534Sscottl device_t dev = slot->dev; 1384195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1385195534Sscottl struct ahci_cmd_tab *ctp; 1386195534Sscottl struct ahci_cmd_list *clp; 1387195534Sscottl union ccb *ccb = slot->ccb; 1388195534Sscottl int port = ccb->ccb_h.target_id & 0x0f; 1389195534Sscottl int fis_size; 1390195534Sscottl 1391195534Sscottl /* Get a piece of the workspace for this request */ 1392195534Sscottl ctp = (struct ahci_cmd_tab *) 1393195534Sscottl (ch->dma.work + AHCI_CT_OFFSET + (AHCI_CT_SIZE * slot->slot)); 1394195534Sscottl /* Setup the FIS for this request */ 1395195534Sscottl if (!(fis_size = ahci_setup_fis(ctp, ccb, slot->slot))) { 1396195534Sscottl device_printf(ch->dev, "Setting up SATA FIS failed\n"); 1397195534Sscottl ahci_end_transaction(slot, AHCI_ERR_INVALID); 1398195534Sscottl return; 1399195534Sscottl } 1400195534Sscottl /* Setup the command list entry */ 1401195534Sscottl clp = (struct ahci_cmd_list *) 1402195534Sscottl (ch->dma.work + AHCI_CL_OFFSET + (AHCI_CL_SIZE * slot->slot)); 1403195534Sscottl clp->prd_length = slot->dma.nsegs; 1404195534Sscottl clp->cmd_flags = (ccb->ccb_h.flags & CAM_DIR_OUT ? AHCI_CMD_WRITE : 0) | 1405195534Sscottl (ccb->ccb_h.func_code == XPT_SCSI_IO ? 1406195534Sscottl (AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH) : 0) | 1407195534Sscottl (fis_size / sizeof(u_int32_t)) | 1408195534Sscottl (port << 12); 1409195534Sscottl /* Special handling for Soft Reset command. */ 1410195534Sscottl if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1411195534Sscottl (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) && 1412195534Sscottl (ccb->ataio.cmd.control & ATA_A_RESET)) { 1413195534Sscottl /* Kick controller into sane state */ 1414195534Sscottl ahci_stop(dev); 1415195534Sscottl ahci_clo(dev); 1416195534Sscottl ahci_start(dev); 1417195534Sscottl clp->cmd_flags |= AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY; 1418195534Sscottl } 1419195534Sscottl clp->bytecount = 0; 1420195534Sscottl clp->cmd_table_phys = htole64(ch->dma.work_bus + AHCI_CT_OFFSET + 1421195534Sscottl (AHCI_CT_SIZE * slot->slot)); 1422195534Sscottl bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map, 1423195534Sscottl BUS_DMASYNC_PREWRITE); 1424195534Sscottl bus_dmamap_sync(ch->dma.rfis_tag, ch->dma.rfis_map, 1425195534Sscottl BUS_DMASYNC_PREREAD); 1426195534Sscottl /* Set ACTIVE bit for NCQ commands. */ 1427195534Sscottl if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1428195534Sscottl (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 1429195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_SACT, 1 << slot->slot); 1430195534Sscottl } 1431195534Sscottl /* Issue command to the controller. */ 1432195534Sscottl slot->state = AHCI_SLOT_RUNNING; 1433195534Sscottl ch->rslots |= (1 << slot->slot); 1434195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CI, (1 << slot->slot)); 1435195534Sscottl /* Device reset commands doesn't interrupt. Poll them. */ 1436195534Sscottl if (ccb->ccb_h.func_code == XPT_ATA_IO && 1437195534Sscottl (ccb->ataio.cmd.command == ATA_DEVICE_RESET || 1438195534Sscottl (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL))) { 1439195534Sscottl int count, timeout = ccb->ccb_h.timeout; 1440195534Sscottl enum ahci_err_type et = AHCI_ERR_NONE; 1441195534Sscottl 1442195534Sscottl for (count = 0; count < timeout; count++) { 1443195534Sscottl DELAY(1000); 1444195534Sscottl if (!(ATA_INL(ch->r_mem, AHCI_P_CI) & (1 << slot->slot))) 1445195534Sscottl break; 1446195534Sscottl if (ATA_INL(ch->r_mem, AHCI_P_TFD) & ATA_S_ERROR) { 1447195534Sscottl device_printf(ch->dev, 1448195534Sscottl "Poll error on slot %d, TFD: %04x\n", 1449195534Sscottl slot->slot, ATA_INL(ch->r_mem, AHCI_P_TFD)); 1450195534Sscottl et = AHCI_ERR_TFE; 1451195534Sscottl break; 1452195534Sscottl } 1453198851Smav /* Workaround for ATI SB600/SB700 chipsets. */ 1454198851Smav if (ccb->ccb_h.target_id == 15 && 1455198851Smav pci_get_vendor(device_get_parent(dev)) == 0x1002 && 1456198851Smav (ATA_INL(ch->r_mem, AHCI_P_IS) & AHCI_P_IX_IPM)) { 1457198851Smav et = AHCI_ERR_TIMEOUT; 1458198851Smav break; 1459198851Smav } 1460195534Sscottl } 1461195534Sscottl if (timeout && (count >= timeout)) { 1462195534Sscottl device_printf(ch->dev, 1463195534Sscottl "Poll timeout on slot %d\n", slot->slot); 1464195534Sscottl et = AHCI_ERR_TIMEOUT; 1465195534Sscottl } 1466195534Sscottl if (et != AHCI_ERR_NONE) { 1467195534Sscottl /* Kick controller into sane state */ 1468195534Sscottl ahci_stop(ch->dev); 1469195534Sscottl ahci_start(ch->dev); 1470195534Sscottl } 1471195534Sscottl ahci_end_transaction(slot, et); 1472195534Sscottl return; 1473195534Sscottl } 1474195534Sscottl /* Start command execution timeout */ 1475198319Smav callout_reset(&slot->timeout, (int)ccb->ccb_h.timeout * hz / 2000, 1476195534Sscottl (timeout_t*)ahci_timeout, slot); 1477195534Sscottl return; 1478195534Sscottl} 1479195534Sscottl 1480195534Sscottl/* Locked by callout mechanism. */ 1481195534Sscottlstatic void 1482195534Sscottlahci_timeout(struct ahci_slot *slot) 1483195534Sscottl{ 1484195534Sscottl device_t dev = slot->dev; 1485195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1486198319Smav uint32_t sstatus; 1487198319Smav int ccs; 1488195534Sscottl int i; 1489195534Sscottl 1490196656Smav /* Check for stale timeout. */ 1491198319Smav if (slot->state < AHCI_SLOT_RUNNING) 1492196656Smav return; 1493196656Smav 1494198319Smav /* Check if slot was not being executed last time we checked. */ 1495198319Smav if (slot->state < AHCI_SLOT_EXECUTING) { 1496198319Smav /* Check if slot started executing. */ 1497198319Smav sstatus = ATA_INL(ch->r_mem, AHCI_P_SACT); 1498198319Smav ccs = (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CCS_MASK) 1499198319Smav >> AHCI_P_CMD_CCS_SHIFT; 1500198319Smav if ((sstatus & (1 << slot->slot)) != 0 || ccs == slot->slot) 1501198319Smav slot->state = AHCI_SLOT_EXECUTING; 1502198319Smav 1503198319Smav callout_reset(&slot->timeout, 1504198319Smav (int)slot->ccb->ccb_h.timeout * hz / 2000, 1505198319Smav (timeout_t*)ahci_timeout, slot); 1506198319Smav return; 1507198319Smav } 1508198319Smav 1509195534Sscottl device_printf(dev, "Timeout on slot %d\n", slot->slot); 1510198319Smav device_printf(dev, "is %08x cs %08x ss %08x rs %08x tfd %02x serr %08x\n", 1511198319Smav ATA_INL(ch->r_mem, AHCI_P_IS), ATA_INL(ch->r_mem, AHCI_P_CI), 1512198319Smav ATA_INL(ch->r_mem, AHCI_P_SACT), ch->rslots, 1513198319Smav ATA_INL(ch->r_mem, AHCI_P_TFD), ATA_INL(ch->r_mem, AHCI_P_SERR)); 1514195534Sscottl 1515198851Smav ch->fatalerr = 1; 1516197838Smav /* Handle frozen command. */ 1517195534Sscottl if (ch->frozen) { 1518195534Sscottl union ccb *fccb = ch->frozen; 1519195534Sscottl ch->frozen = NULL; 1520195534Sscottl fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 1521198319Smav if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 1522198319Smav xpt_freeze_devq(fccb->ccb_h.path, 1); 1523198319Smav fccb->ccb_h.status |= CAM_DEV_QFRZN; 1524198319Smav } 1525195534Sscottl xpt_done(fccb); 1526195534Sscottl } 1527197838Smav /* Handle command with timeout. */ 1528197838Smav ahci_end_transaction(&ch->slot[slot->slot], AHCI_ERR_TIMEOUT); 1529197838Smav /* Handle the rest of commands. */ 1530195534Sscottl for (i = 0; i < ch->numslots; i++) { 1531195534Sscottl /* Do we have a running request on slot? */ 1532195534Sscottl if (ch->slot[i].state < AHCI_SLOT_RUNNING) 1533195534Sscottl continue; 1534195534Sscottl ahci_end_transaction(&ch->slot[i], AHCI_ERR_INNOCENT); 1535195534Sscottl } 1536195534Sscottl} 1537195534Sscottl 1538195534Sscottl/* Must be called with channel locked. */ 1539195534Sscottlstatic void 1540195534Sscottlahci_end_transaction(struct ahci_slot *slot, enum ahci_err_type et) 1541195534Sscottl{ 1542195534Sscottl device_t dev = slot->dev; 1543195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1544195534Sscottl union ccb *ccb = slot->ccb; 1545195534Sscottl 1546195534Sscottl bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map, 1547195534Sscottl BUS_DMASYNC_POSTWRITE); 1548195534Sscottl /* Read result registers to the result struct 1549195534Sscottl * May be incorrect if several commands finished same time, 1550195534Sscottl * so read only when sure or have to. 1551195534Sscottl */ 1552195534Sscottl if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1553195534Sscottl struct ata_res *res = &ccb->ataio.res; 1554195534Sscottl 1555195534Sscottl if ((et == AHCI_ERR_TFE) || 1556195534Sscottl (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) { 1557195534Sscottl u_int8_t *fis = ch->dma.rfis + 0x40; 1558195534Sscottl uint16_t tfd = ATA_INL(ch->r_mem, AHCI_P_TFD); 1559195534Sscottl 1560195534Sscottl bus_dmamap_sync(ch->dma.rfis_tag, ch->dma.rfis_map, 1561195534Sscottl BUS_DMASYNC_POSTREAD); 1562195534Sscottl res->status = tfd; 1563195534Sscottl res->error = tfd >> 8; 1564195534Sscottl res->lba_low = fis[4]; 1565195534Sscottl res->lba_mid = fis[5]; 1566195534Sscottl res->lba_high = fis[6]; 1567195534Sscottl res->device = fis[7]; 1568195534Sscottl res->lba_low_exp = fis[8]; 1569195534Sscottl res->lba_mid_exp = fis[9]; 1570195534Sscottl res->lba_high_exp = fis[10]; 1571195534Sscottl res->sector_count = fis[12]; 1572195534Sscottl res->sector_count_exp = fis[13]; 1573195534Sscottl } else 1574195534Sscottl bzero(res, sizeof(*res)); 1575195534Sscottl } 1576195534Sscottl if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 1577195534Sscottl bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 1578195534Sscottl (ccb->ccb_h.flags & CAM_DIR_IN) ? 1579195534Sscottl BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 1580195534Sscottl bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map); 1581195534Sscottl } 1582198319Smav /* In case of error, freeze device for proper recovery. */ 1583198319Smav if ((et != AHCI_ERR_NONE) && (!ch->readlog) && 1584198319Smav !(ccb->ccb_h.status & CAM_DEV_QFRZN)) { 1585198319Smav xpt_freeze_devq(ccb->ccb_h.path, 1); 1586198319Smav ccb->ccb_h.status |= CAM_DEV_QFRZN; 1587198319Smav } 1588195534Sscottl /* Set proper result status. */ 1589195534Sscottl ccb->ccb_h.status &= ~CAM_STATUS_MASK; 1590195534Sscottl switch (et) { 1591195534Sscottl case AHCI_ERR_NONE: 1592195534Sscottl ccb->ccb_h.status |= CAM_REQ_CMP; 1593195534Sscottl if (ccb->ccb_h.func_code == XPT_SCSI_IO) 1594195534Sscottl ccb->csio.scsi_status = SCSI_STATUS_OK; 1595195534Sscottl break; 1596195534Sscottl case AHCI_ERR_INVALID: 1597198851Smav ch->fatalerr = 1; 1598195534Sscottl ccb->ccb_h.status |= CAM_REQ_INVALID; 1599195534Sscottl break; 1600195534Sscottl case AHCI_ERR_INNOCENT: 1601195534Sscottl ccb->ccb_h.status |= CAM_REQUEUE_REQ; 1602195534Sscottl break; 1603195534Sscottl case AHCI_ERR_TFE: 1604198319Smav case AHCI_ERR_NCQ: 1605195534Sscottl if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1606195534Sscottl ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR; 1607195534Sscottl ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 1608195534Sscottl } else { 1609195534Sscottl ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR; 1610195534Sscottl } 1611195534Sscottl break; 1612195534Sscottl case AHCI_ERR_SATA: 1613198851Smav ch->fatalerr = 1; 1614198319Smav if (!ch->readlog) { 1615198319Smav xpt_freeze_simq(ch->sim, 1); 1616198319Smav ccb->ccb_h.status &= ~CAM_STATUS_MASK; 1617198319Smav ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 1618198319Smav } 1619198319Smav ccb->ccb_h.status |= CAM_UNCOR_PARITY; 1620195534Sscottl break; 1621195534Sscottl case AHCI_ERR_TIMEOUT: 1622198851Smav /* Do no treat soft-reset timeout as fatal here. */ 1623198851Smav if (ccb->ccb_h.func_code != XPT_ATA_IO || 1624198851Smav !(ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL)) 1625198851Smav ch->fatalerr = 1; 1626198319Smav if (!ch->readlog) { 1627198319Smav xpt_freeze_simq(ch->sim, 1); 1628198319Smav ccb->ccb_h.status &= ~CAM_STATUS_MASK; 1629198319Smav ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 1630198319Smav } 1631195534Sscottl ccb->ccb_h.status |= CAM_CMD_TIMEOUT; 1632195534Sscottl break; 1633195534Sscottl default: 1634198851Smav ch->fatalerr = 1; 1635195534Sscottl ccb->ccb_h.status |= CAM_REQ_CMP_ERR; 1636195534Sscottl } 1637195534Sscottl /* Free slot. */ 1638195534Sscottl ch->rslots &= ~(1 << slot->slot); 1639195534Sscottl ch->aslots &= ~(1 << slot->slot); 1640195534Sscottl slot->state = AHCI_SLOT_EMPTY; 1641195534Sscottl slot->ccb = NULL; 1642195534Sscottl /* Update channel stats. */ 1643195534Sscottl ch->numrslots--; 1644195534Sscottl if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1645195534Sscottl (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 1646195534Sscottl ch->numtslots--; 1647195534Sscottl } 1648195534Sscottl /* If it was first request of reset sequence and there is no error, 1649195534Sscottl * proceed to second request. */ 1650195534Sscottl if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1651195534Sscottl (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) && 1652195534Sscottl (ccb->ataio.cmd.control & ATA_A_RESET) && 1653195534Sscottl et == AHCI_ERR_NONE) { 1654195534Sscottl ccb->ataio.cmd.control &= ~ATA_A_RESET; 1655195534Sscottl ahci_begin_transaction(dev, ccb); 1656195534Sscottl return; 1657195534Sscottl } 1658198851Smav /* If it was our READ LOG command - process it. */ 1659198851Smav if (ch->readlog) { 1660198851Smav ahci_process_read_log(dev, ccb); 1661195534Sscottl /* If it was NCQ command error, put result on hold. */ 1662198851Smav } else if (et == AHCI_ERR_NCQ) { 1663195534Sscottl ch->hold[slot->slot] = ccb; 1664198851Smav } else 1665195534Sscottl xpt_done(ccb); 1666195534Sscottl /* Unfreeze frozen command. */ 1667195534Sscottl if (ch->frozen && ch->numrslots == 0) { 1668195534Sscottl union ccb *fccb = ch->frozen; 1669195534Sscottl ch->frozen = NULL; 1670195534Sscottl ahci_begin_transaction(dev, fccb); 1671195534Sscottl xpt_release_simq(ch->sim, TRUE); 1672195534Sscottl } 1673198851Smav /* If we have no other active commands, ... */ 1674198851Smav if (ch->rslots == 0) { 1675198851Smav /* if there was fatal error - reset port. */ 1676198851Smav if (ch->fatalerr) { 1677198851Smav ahci_reset(dev); 1678198851Smav } 1679198851Smav } 1680196656Smav /* Start PM timer. */ 1681196656Smav if (ch->numrslots == 0 && ch->pm_level > 3) { 1682196656Smav callout_schedule(&ch->pm_timer, 1683196656Smav (ch->pm_level == 4) ? hz / 1000 : hz / 8); 1684196656Smav } 1685195534Sscottl} 1686195534Sscottl 1687195534Sscottlstatic void 1688195534Sscottlahci_issue_read_log(device_t dev) 1689195534Sscottl{ 1690195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1691195534Sscottl union ccb *ccb; 1692195534Sscottl struct ccb_ataio *ataio; 1693195534Sscottl int i; 1694195534Sscottl 1695195534Sscottl ch->readlog = 1; 1696195534Sscottl /* Find some holden command. */ 1697195534Sscottl for (i = 0; i < ch->numslots; i++) { 1698195534Sscottl if (ch->hold[i]) 1699195534Sscottl break; 1700195534Sscottl } 1701195534Sscottl ccb = xpt_alloc_ccb_nowait(); 1702195534Sscottl if (ccb == NULL) { 1703195534Sscottl device_printf(dev, "Unable allocate READ LOG command"); 1704195534Sscottl return; /* XXX */ 1705195534Sscottl } 1706195534Sscottl ccb->ccb_h = ch->hold[i]->ccb_h; /* Reuse old header. */ 1707195534Sscottl ccb->ccb_h.func_code = XPT_ATA_IO; 1708195534Sscottl ccb->ccb_h.flags = CAM_DIR_IN; 1709195534Sscottl ccb->ccb_h.timeout = 1000; /* 1s should be enough. */ 1710195534Sscottl ataio = &ccb->ataio; 1711195534Sscottl ataio->data_ptr = malloc(512, M_AHCI, M_NOWAIT); 1712195534Sscottl if (ataio->data_ptr == NULL) { 1713195534Sscottl device_printf(dev, "Unable allocate memory for READ LOG command"); 1714195534Sscottl return; /* XXX */ 1715195534Sscottl } 1716195534Sscottl ataio->dxfer_len = 512; 1717195534Sscottl bzero(&ataio->cmd, sizeof(ataio->cmd)); 1718195534Sscottl ataio->cmd.flags = CAM_ATAIO_48BIT; 1719195534Sscottl ataio->cmd.command = 0x2F; /* READ LOG EXT */ 1720195534Sscottl ataio->cmd.sector_count = 1; 1721195534Sscottl ataio->cmd.sector_count_exp = 0; 1722195534Sscottl ataio->cmd.lba_low = 0x10; 1723195534Sscottl ataio->cmd.lba_mid = 0; 1724195534Sscottl ataio->cmd.lba_mid_exp = 0; 1725198319Smav /* Freeze SIM while doing READ LOG EXT. */ 1726198319Smav xpt_freeze_simq(ch->sim, 1); 1727195534Sscottl ahci_begin_transaction(dev, ccb); 1728195534Sscottl} 1729195534Sscottl 1730195534Sscottlstatic void 1731195534Sscottlahci_process_read_log(device_t dev, union ccb *ccb) 1732195534Sscottl{ 1733195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1734195534Sscottl uint8_t *data; 1735195534Sscottl struct ata_res *res; 1736195534Sscottl int i; 1737195534Sscottl 1738195534Sscottl ch->readlog = 0; 1739195534Sscottl 1740195534Sscottl data = ccb->ataio.data_ptr; 1741195534Sscottl if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && 1742195534Sscottl (data[0] & 0x80) == 0) { 1743195534Sscottl for (i = 0; i < ch->numslots; i++) { 1744195534Sscottl if (!ch->hold[i]) 1745195534Sscottl continue; 1746195534Sscottl if ((data[0] & 0x1F) == i) { 1747195534Sscottl res = &ch->hold[i]->ataio.res; 1748195534Sscottl res->status = data[2]; 1749195534Sscottl res->error = data[3]; 1750195534Sscottl res->lba_low = data[4]; 1751195534Sscottl res->lba_mid = data[5]; 1752195534Sscottl res->lba_high = data[6]; 1753195534Sscottl res->device = data[7]; 1754195534Sscottl res->lba_low_exp = data[8]; 1755195534Sscottl res->lba_mid_exp = data[9]; 1756195534Sscottl res->lba_high_exp = data[10]; 1757195534Sscottl res->sector_count = data[12]; 1758195534Sscottl res->sector_count_exp = data[13]; 1759195534Sscottl } else { 1760195534Sscottl ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 1761195534Sscottl ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ; 1762195534Sscottl } 1763195534Sscottl xpt_done(ch->hold[i]); 1764195534Sscottl ch->hold[i] = NULL; 1765195534Sscottl } 1766195534Sscottl } else { 1767195534Sscottl if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 1768195534Sscottl device_printf(dev, "Error while READ LOG EXT\n"); 1769195534Sscottl else if ((data[0] & 0x80) == 0) { 1770195534Sscottl device_printf(dev, "Non-queued command error in READ LOG EXT\n"); 1771195534Sscottl } 1772195534Sscottl for (i = 0; i < ch->numslots; i++) { 1773195534Sscottl if (!ch->hold[i]) 1774195534Sscottl continue; 1775195534Sscottl xpt_done(ch->hold[i]); 1776195534Sscottl ch->hold[i] = NULL; 1777195534Sscottl } 1778195534Sscottl } 1779195534Sscottl free(ccb->ataio.data_ptr, M_AHCI); 1780195534Sscottl xpt_free_ccb(ccb); 1781198319Smav xpt_release_simq(ch->sim, TRUE); 1782195534Sscottl} 1783195534Sscottl 1784195534Sscottlstatic void 1785195534Sscottlahci_start(device_t dev) 1786195534Sscottl{ 1787195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1788195534Sscottl u_int32_t cmd; 1789195534Sscottl 1790195534Sscottl /* Clear SATA error register */ 1791195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_SERR, 0xFFFFFFFF); 1792195534Sscottl /* Clear any interrupts pending on this channel */ 1793195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_IS, 0xFFFFFFFF); 1794195534Sscottl /* Start operations on this channel */ 1795195534Sscottl cmd = ATA_INL(ch->r_mem, AHCI_P_CMD); 1796195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd | AHCI_P_CMD_ST | 1797195534Sscottl (ch->pm_present ? AHCI_P_CMD_PMA : 0)); 1798195534Sscottl} 1799195534Sscottl 1800195534Sscottlstatic void 1801195534Sscottlahci_stop(device_t dev) 1802195534Sscottl{ 1803195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1804195534Sscottl u_int32_t cmd; 1805195534Sscottl int timeout; 1806195534Sscottl 1807195534Sscottl /* Kill all activity on this channel */ 1808195534Sscottl cmd = ATA_INL(ch->r_mem, AHCI_P_CMD); 1809195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd & ~AHCI_P_CMD_ST); 1810195534Sscottl /* Wait for activity stop. */ 1811195534Sscottl timeout = 0; 1812195534Sscottl do { 1813195534Sscottl DELAY(1000); 1814195534Sscottl if (timeout++ > 1000) { 1815195534Sscottl device_printf(dev, "stopping AHCI engine failed\n"); 1816195534Sscottl break; 1817195534Sscottl } 1818195534Sscottl } while (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CR); 1819195534Sscottl} 1820195534Sscottl 1821195534Sscottlstatic void 1822195534Sscottlahci_clo(device_t dev) 1823195534Sscottl{ 1824195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1825195534Sscottl u_int32_t cmd; 1826195534Sscottl int timeout; 1827195534Sscottl 1828195534Sscottl /* Issue Command List Override if supported */ 1829195534Sscottl if (ch->caps & AHCI_CAP_SCLO) { 1830195534Sscottl cmd = ATA_INL(ch->r_mem, AHCI_P_CMD); 1831195534Sscottl cmd |= AHCI_P_CMD_CLO; 1832195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd); 1833195534Sscottl timeout = 0; 1834195534Sscottl do { 1835195534Sscottl DELAY(1000); 1836195534Sscottl if (timeout++ > 1000) { 1837195534Sscottl device_printf(dev, "executing CLO failed\n"); 1838195534Sscottl break; 1839195534Sscottl } 1840195534Sscottl } while (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CLO); 1841195534Sscottl } 1842195534Sscottl} 1843195534Sscottl 1844195534Sscottlstatic void 1845195534Sscottlahci_stop_fr(device_t dev) 1846195534Sscottl{ 1847195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1848195534Sscottl u_int32_t cmd; 1849195534Sscottl int timeout; 1850195534Sscottl 1851195534Sscottl /* Kill all FIS reception on this channel */ 1852195534Sscottl cmd = ATA_INL(ch->r_mem, AHCI_P_CMD); 1853195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd & ~AHCI_P_CMD_FRE); 1854195534Sscottl /* Wait for FIS reception stop. */ 1855195534Sscottl timeout = 0; 1856195534Sscottl do { 1857195534Sscottl DELAY(1000); 1858195534Sscottl if (timeout++ > 1000) { 1859195534Sscottl device_printf(dev, "stopping AHCI FR engine failed\n"); 1860195534Sscottl break; 1861195534Sscottl } 1862195534Sscottl } while (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_FR); 1863195534Sscottl} 1864195534Sscottl 1865195534Sscottlstatic void 1866195534Sscottlahci_start_fr(device_t dev) 1867195534Sscottl{ 1868195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1869195534Sscottl u_int32_t cmd; 1870195534Sscottl 1871195534Sscottl /* Start FIS reception on this channel */ 1872195534Sscottl cmd = ATA_INL(ch->r_mem, AHCI_P_CMD); 1873195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd | AHCI_P_CMD_FRE); 1874195534Sscottl} 1875195534Sscottl 1876195534Sscottlstatic int 1877195534Sscottlahci_wait_ready(device_t dev, int t) 1878195534Sscottl{ 1879195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1880195534Sscottl int timeout = 0; 1881195534Sscottl uint32_t val; 1882195534Sscottl 1883195534Sscottl while ((val = ATA_INL(ch->r_mem, AHCI_P_TFD)) & 1884195534Sscottl (ATA_S_BUSY | ATA_S_DRQ)) { 1885195534Sscottl DELAY(1000); 1886195534Sscottl if (timeout++ > t) { 1887195534Sscottl device_printf(dev, "port is not ready (timeout %dms) " 1888195534Sscottl "tfd = %08x\n", t, val); 1889195534Sscottl return (EBUSY); 1890195534Sscottl } 1891195534Sscottl } 1892195534Sscottl if (bootverbose) 1893195534Sscottl device_printf(dev, "ready wait time=%dms\n", timeout); 1894195534Sscottl return (0); 1895195534Sscottl} 1896195534Sscottl 1897195534Sscottlstatic void 1898195534Sscottlahci_reset(device_t dev) 1899195534Sscottl{ 1900195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 1901196656Smav struct ahci_controller *ctlr = device_get_softc(device_get_parent(dev)); 1902195534Sscottl int i; 1903195534Sscottl 1904195534Sscottl if (bootverbose) 1905195534Sscottl device_printf(dev, "AHCI reset...\n"); 1906195534Sscottl /* Requeue freezed command. */ 1907195534Sscottl if (ch->frozen) { 1908195534Sscottl union ccb *fccb = ch->frozen; 1909195534Sscottl ch->frozen = NULL; 1910195534Sscottl fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 1911198319Smav if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 1912198319Smav xpt_freeze_devq(fccb->ccb_h.path, 1); 1913198319Smav fccb->ccb_h.status |= CAM_DEV_QFRZN; 1914198319Smav } 1915195534Sscottl xpt_done(fccb); 1916195534Sscottl } 1917195534Sscottl /* Kill the engine and requeue all running commands. */ 1918195534Sscottl ahci_stop(dev); 1919195534Sscottl for (i = 0; i < ch->numslots; i++) { 1920195534Sscottl /* Do we have a running request on slot? */ 1921195534Sscottl if (ch->slot[i].state < AHCI_SLOT_RUNNING) 1922195534Sscottl continue; 1923195534Sscottl /* XXX; Commands in loading state. */ 1924195534Sscottl ahci_end_transaction(&ch->slot[i], AHCI_ERR_INNOCENT); 1925195534Sscottl } 1926198851Smav for (i = 0; i < ch->numslots; i++) { 1927198851Smav if (!ch->hold[i]) 1928198851Smav continue; 1929198851Smav xpt_done(ch->hold[i]); 1930198851Smav ch->hold[i] = NULL; 1931198851Smav } 1932198851Smav ch->fatalerr = 0; 1933198319Smav /* Tell the XPT about the event */ 1934198319Smav xpt_async(AC_BUS_RESET, ch->path, NULL); 1935195534Sscottl /* Disable port interrupts */ 1936195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_IE, 0); 1937195534Sscottl /* Reset and reconnect PHY, */ 1938195534Sscottl if (!ahci_sata_phy_reset(dev, 0)) { 1939195534Sscottl if (bootverbose) 1940195534Sscottl device_printf(dev, 1941195534Sscottl "AHCI reset done: phy reset found no device\n"); 1942195534Sscottl ch->devices = 0; 1943195534Sscottl /* Enable wanted port interrupts */ 1944195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_IE, 1945195534Sscottl (AHCI_P_IX_CPD | AHCI_P_IX_PRC | AHCI_P_IX_PC)); 1946195534Sscottl return; 1947195534Sscottl } 1948195534Sscottl /* Wait for clearing busy status. */ 1949195534Sscottl if (ahci_wait_ready(dev, 10000)) { 1950195534Sscottl device_printf(dev, "device ready timeout\n"); 1951195534Sscottl ahci_clo(dev); 1952195534Sscottl } 1953195534Sscottl ahci_start(dev); 1954195534Sscottl ch->devices = 1; 1955195534Sscottl /* Enable wanted port interrupts */ 1956195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_IE, 1957195534Sscottl (AHCI_P_IX_CPD | AHCI_P_IX_TFE | AHCI_P_IX_HBF | 1958195534Sscottl AHCI_P_IX_HBD | AHCI_P_IX_IF | AHCI_P_IX_OF | 1959195534Sscottl ((ch->pm_level == 0) ? AHCI_P_IX_PRC | AHCI_P_IX_PC : 0) | 1960196656Smav AHCI_P_IX_DP | AHCI_P_IX_UF | (ctlr->ccc ? 0 : AHCI_P_IX_SDB) | 1961196656Smav AHCI_P_IX_DS | AHCI_P_IX_PS | (ctlr->ccc ? 0 : AHCI_P_IX_DHR))); 1962195534Sscottl if (bootverbose) 1963196656Smav device_printf(dev, "AHCI reset done: device found\n"); 1964195534Sscottl} 1965195534Sscottl 1966195534Sscottlstatic int 1967195534Sscottlahci_setup_fis(struct ahci_cmd_tab *ctp, union ccb *ccb, int tag) 1968195534Sscottl{ 1969195534Sscottl u_int8_t *fis = &ctp->cfis[0]; 1970195534Sscottl 1971195534Sscottl bzero(ctp->cfis, 64); 1972195534Sscottl fis[0] = 0x27; /* host to device */ 1973195534Sscottl fis[1] = (ccb->ccb_h.target_id & 0x0f); 1974195534Sscottl if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1975195534Sscottl fis[1] |= 0x80; 1976195534Sscottl fis[2] = ATA_PACKET_CMD; 1977195534Sscottl if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) 1978195534Sscottl fis[3] = ATA_F_DMA; 1979195534Sscottl else { 1980195534Sscottl fis[5] = ccb->csio.dxfer_len; 1981195534Sscottl fis[6] = ccb->csio.dxfer_len >> 8; 1982195534Sscottl } 1983195534Sscottl fis[7] = ATA_D_LBA; 1984195534Sscottl fis[15] = ATA_A_4BIT; 1985195534Sscottl bzero(ctp->acmd, 32); 1986195534Sscottl bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ? 1987195534Sscottl ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes, 1988195534Sscottl ctp->acmd, ccb->csio.cdb_len); 1989195534Sscottl } else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) { 1990195534Sscottl fis[1] |= 0x80; 1991195534Sscottl fis[2] = ccb->ataio.cmd.command; 1992195534Sscottl fis[3] = ccb->ataio.cmd.features; 1993195534Sscottl fis[4] = ccb->ataio.cmd.lba_low; 1994195534Sscottl fis[5] = ccb->ataio.cmd.lba_mid; 1995195534Sscottl fis[6] = ccb->ataio.cmd.lba_high; 1996195534Sscottl fis[7] = ccb->ataio.cmd.device; 1997195534Sscottl fis[8] = ccb->ataio.cmd.lba_low_exp; 1998195534Sscottl fis[9] = ccb->ataio.cmd.lba_mid_exp; 1999195534Sscottl fis[10] = ccb->ataio.cmd.lba_high_exp; 2000195534Sscottl fis[11] = ccb->ataio.cmd.features_exp; 2001195534Sscottl if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 2002195534Sscottl fis[12] = tag << 3; 2003195534Sscottl fis[13] = 0; 2004195534Sscottl } else { 2005195534Sscottl fis[12] = ccb->ataio.cmd.sector_count; 2006195534Sscottl fis[13] = ccb->ataio.cmd.sector_count_exp; 2007195534Sscottl } 2008195534Sscottl fis[15] = ATA_A_4BIT; 2009195534Sscottl } else { 2010195534Sscottl fis[15] = ccb->ataio.cmd.control; 2011195534Sscottl } 2012195534Sscottl return (20); 2013195534Sscottl} 2014195534Sscottl 2015195534Sscottlstatic int 2016195534Sscottlahci_sata_connect(struct ahci_channel *ch) 2017195534Sscottl{ 2018195534Sscottl u_int32_t status; 2019195534Sscottl int timeout; 2020195534Sscottl 2021195534Sscottl /* Wait up to 100ms for "connect well" */ 2022195534Sscottl for (timeout = 0; timeout < 100 ; timeout++) { 2023195534Sscottl status = ATA_INL(ch->r_mem, AHCI_P_SSTS); 2024195534Sscottl if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) && 2025195534Sscottl ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) && 2026195534Sscottl ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) 2027195534Sscottl break; 2028196656Smav if ((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_OFFLINE) { 2029196656Smav if (bootverbose) { 2030196656Smav device_printf(ch->dev, "SATA offline status=%08x\n", 2031196656Smav status); 2032196656Smav } 2033196656Smav return (0); 2034196656Smav } 2035195534Sscottl DELAY(1000); 2036195534Sscottl } 2037195534Sscottl if (timeout >= 100) { 2038195534Sscottl if (bootverbose) { 2039195534Sscottl device_printf(ch->dev, "SATA connect timeout status=%08x\n", 2040195534Sscottl status); 2041195534Sscottl } 2042195534Sscottl return (0); 2043195534Sscottl } 2044195534Sscottl if (bootverbose) { 2045195534Sscottl device_printf(ch->dev, "SATA connect time=%dms status=%08x\n", 2046195534Sscottl timeout, status); 2047195534Sscottl } 2048195534Sscottl /* Clear SATA error register */ 2049195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_SERR, 0xffffffff); 2050195534Sscottl return (1); 2051195534Sscottl} 2052195534Sscottl 2053195534Sscottlstatic int 2054195534Sscottlahci_sata_phy_reset(device_t dev, int quick) 2055195534Sscottl{ 2056195534Sscottl struct ahci_channel *ch = device_get_softc(dev); 2057195534Sscottl uint32_t val; 2058195534Sscottl 2059195534Sscottl if (quick) { 2060195534Sscottl val = ATA_INL(ch->r_mem, AHCI_P_SCTL); 2061195534Sscottl if ((val & ATA_SC_DET_MASK) == ATA_SC_DET_IDLE) 2062195534Sscottl return (ahci_sata_connect(ch)); 2063195534Sscottl } 2064195534Sscottl 2065195534Sscottl if (bootverbose) 2066195534Sscottl device_printf(dev, "hardware reset ...\n"); 2067195534Sscottl if (ch->sata_rev == 1) 2068195534Sscottl val = ATA_SC_SPD_SPEED_GEN1; 2069195534Sscottl else if (ch->sata_rev == 2) 2070195534Sscottl val = ATA_SC_SPD_SPEED_GEN2; 2071195534Sscottl else if (ch->sata_rev == 3) 2072195534Sscottl val = ATA_SC_SPD_SPEED_GEN3; 2073195534Sscottl else 2074195534Sscottl val = 0; 2075195534Sscottl ATA_OUTL(ch->r_mem, AHCI_P_SCTL, 2076196656Smav ATA_SC_DET_RESET | val | 2077196656Smav ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER); 2078196656Smav DELAY(5000); 2079196656Smav ATA_OUTL(ch->r_mem, AHCI_P_SCTL, 2080195534Sscottl ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 : 2081195534Sscottl (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER))); 2082196656Smav DELAY(5000); 2083195534Sscottl return (ahci_sata_connect(ch)); 2084195534Sscottl} 2085195534Sscottl 2086195534Sscottlstatic void 2087195534Sscottlahciaction(struct cam_sim *sim, union ccb *ccb) 2088195534Sscottl{ 2089195534Sscottl device_t dev; 2090195534Sscottl struct ahci_channel *ch; 2091195534Sscottl 2092195534Sscottl CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahciaction func_code=%x\n", 2093195534Sscottl ccb->ccb_h.func_code)); 2094195534Sscottl 2095195534Sscottl ch = (struct ahci_channel *)cam_sim_softc(sim); 2096195534Sscottl dev = ch->dev; 2097195534Sscottl switch (ccb->ccb_h.func_code) { 2098195534Sscottl /* Common cases first */ 2099195534Sscottl case XPT_ATA_IO: /* Execute the requested I/O operation */ 2100195534Sscottl case XPT_SCSI_IO: 2101195534Sscottl if (ch->devices == 0) { 2102195534Sscottl ccb->ccb_h.status = CAM_SEL_TIMEOUT; 2103195534Sscottl xpt_done(ccb); 2104195534Sscottl break; 2105195534Sscottl } 2106195534Sscottl /* Check for command collision. */ 2107195534Sscottl if (ahci_check_collision(dev, ccb)) { 2108195534Sscottl /* Freeze command. */ 2109195534Sscottl ch->frozen = ccb; 2110195534Sscottl /* We have only one frozen slot, so freeze simq also. */ 2111195534Sscottl xpt_freeze_simq(ch->sim, 1); 2112195534Sscottl return; 2113195534Sscottl } 2114195534Sscottl ahci_begin_transaction(dev, ccb); 2115195534Sscottl break; 2116195534Sscottl case XPT_EN_LUN: /* Enable LUN as a target */ 2117195534Sscottl case XPT_TARGET_IO: /* Execute target I/O request */ 2118195534Sscottl case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */ 2119195534Sscottl case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/ 2120195534Sscottl case XPT_ABORT: /* Abort the specified CCB */ 2121195534Sscottl /* XXX Implement */ 2122195534Sscottl ccb->ccb_h.status = CAM_REQ_INVALID; 2123195534Sscottl xpt_done(ccb); 2124195534Sscottl break; 2125195534Sscottl case XPT_SET_TRAN_SETTINGS: 2126195534Sscottl { 2127195534Sscottl struct ccb_trans_settings *cts = &ccb->cts; 2128195534Sscottl 2129195534Sscottl if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) { 2130195534Sscottl ch->pm_present = cts->xport_specific.sata.pm_present; 2131195534Sscottl } 2132195534Sscottl ccb->ccb_h.status = CAM_REQ_CMP; 2133195534Sscottl xpt_done(ccb); 2134195534Sscottl break; 2135195534Sscottl } 2136195534Sscottl case XPT_GET_TRAN_SETTINGS: 2137195534Sscottl /* Get default/user set transfer settings for the target */ 2138195534Sscottl { 2139195534Sscottl struct ccb_trans_settings *cts = &ccb->cts; 2140195534Sscottl uint32_t status; 2141195534Sscottl 2142195534Sscottl cts->protocol = PROTO_ATA; 2143196656Smav cts->protocol_version = PROTO_VERSION_UNSPECIFIED; 2144195534Sscottl cts->transport = XPORT_SATA; 2145196656Smav cts->transport_version = XPORT_VERSION_UNSPECIFIED; 2146195534Sscottl cts->proto_specific.valid = 0; 2147195534Sscottl cts->xport_specific.sata.valid = 0; 2148195534Sscottl if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 2149195534Sscottl status = ATA_INL(ch->r_mem, AHCI_P_SSTS) & ATA_SS_SPD_MASK; 2150195534Sscottl else 2151195534Sscottl status = ATA_INL(ch->r_mem, AHCI_P_SCTL) & ATA_SC_SPD_MASK; 2152195534Sscottl if (status & ATA_SS_SPD_GEN3) { 2153195534Sscottl cts->xport_specific.sata.bitrate = 600000; 2154195534Sscottl cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED; 2155195534Sscottl } else if (status & ATA_SS_SPD_GEN2) { 2156195534Sscottl cts->xport_specific.sata.bitrate = 300000; 2157195534Sscottl cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED; 2158195534Sscottl } else if (status & ATA_SS_SPD_GEN1) { 2159195534Sscottl cts->xport_specific.sata.bitrate = 150000; 2160195534Sscottl cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED; 2161195534Sscottl } 2162195534Sscottl if (cts->type == CTS_TYPE_CURRENT_SETTINGS) { 2163195534Sscottl cts->xport_specific.sata.pm_present = 2164195534Sscottl (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_PMA) ? 2165195534Sscottl 1 : 0; 2166195534Sscottl } else { 2167195534Sscottl cts->xport_specific.sata.pm_present = ch->pm_present; 2168195534Sscottl } 2169195534Sscottl cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM; 2170195534Sscottl ccb->ccb_h.status = CAM_REQ_CMP; 2171195534Sscottl xpt_done(ccb); 2172195534Sscottl break; 2173195534Sscottl } 2174195534Sscottl#if 0 2175195534Sscottl case XPT_CALC_GEOMETRY: 2176195534Sscottl { 2177195534Sscottl struct ccb_calc_geometry *ccg; 2178195534Sscottl uint32_t size_mb; 2179195534Sscottl uint32_t secs_per_cylinder; 2180195534Sscottl 2181195534Sscottl ccg = &ccb->ccg; 2182195534Sscottl size_mb = ccg->volume_size 2183195534Sscottl / ((1024L * 1024L) / ccg->block_size); 2184195534Sscottl if (size_mb >= 1024 && (aha->extended_trans != 0)) { 2185195534Sscottl if (size_mb >= 2048) { 2186195534Sscottl ccg->heads = 255; 2187195534Sscottl ccg->secs_per_track = 63; 2188195534Sscottl } else { 2189195534Sscottl ccg->heads = 128; 2190195534Sscottl ccg->secs_per_track = 32; 2191195534Sscottl } 2192195534Sscottl } else { 2193195534Sscottl ccg->heads = 64; 2194195534Sscottl ccg->secs_per_track = 32; 2195195534Sscottl } 2196195534Sscottl secs_per_cylinder = ccg->heads * ccg->secs_per_track; 2197195534Sscottl ccg->cylinders = ccg->volume_size / secs_per_cylinder; 2198195534Sscottl ccb->ccb_h.status = CAM_REQ_CMP; 2199195534Sscottl xpt_done(ccb); 2200195534Sscottl break; 2201195534Sscottl } 2202195534Sscottl#endif 2203195534Sscottl case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 2204195534Sscottl case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */ 2205195534Sscottl ahci_reset(dev); 2206195534Sscottl ccb->ccb_h.status = CAM_REQ_CMP; 2207195534Sscottl xpt_done(ccb); 2208195534Sscottl break; 2209195534Sscottl case XPT_TERM_IO: /* Terminate the I/O process */ 2210195534Sscottl /* XXX Implement */ 2211195534Sscottl ccb->ccb_h.status = CAM_REQ_INVALID; 2212195534Sscottl xpt_done(ccb); 2213195534Sscottl break; 2214195534Sscottl case XPT_PATH_INQ: /* Path routing inquiry */ 2215195534Sscottl { 2216195534Sscottl struct ccb_pathinq *cpi = &ccb->cpi; 2217195534Sscottl 2218195534Sscottl cpi->version_num = 1; /* XXX??? */ 2219199278Smav cpi->hba_inquiry = PI_SDTR_ABLE; 2220199278Smav if (ch->caps & AHCI_CAP_SNCQ) 2221199278Smav cpi->hba_inquiry |= PI_TAG_ABLE; 2222195534Sscottl if (ch->caps & AHCI_CAP_SPM) 2223195534Sscottl cpi->hba_inquiry |= PI_SATAPM; 2224195534Sscottl cpi->target_sprt = 0; 2225195534Sscottl cpi->hba_misc = PIM_SEQSCAN; 2226195534Sscottl cpi->hba_eng_cnt = 0; 2227195534Sscottl if (ch->caps & AHCI_CAP_SPM) 2228198322Smav cpi->max_target = 15; 2229195534Sscottl else 2230195534Sscottl cpi->max_target = 0; 2231195534Sscottl cpi->max_lun = 0; 2232195534Sscottl cpi->initiator_id = 0; 2233195534Sscottl cpi->bus_id = cam_sim_bus(sim); 2234195534Sscottl cpi->base_transfer_speed = 150000; 2235195534Sscottl strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2236195534Sscottl strncpy(cpi->hba_vid, "AHCI", HBA_IDLEN); 2237195534Sscottl strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 2238195534Sscottl cpi->unit_number = cam_sim_unit(sim); 2239195534Sscottl cpi->transport = XPORT_SATA; 2240196656Smav cpi->transport_version = XPORT_VERSION_UNSPECIFIED; 2241195534Sscottl cpi->protocol = PROTO_ATA; 2242196656Smav cpi->protocol_version = PROTO_VERSION_UNSPECIFIED; 2243195534Sscottl cpi->maxio = MAXPHYS; 2244196777Smav /* ATI SB600 can't handle 256 sectors with FPDMA (NCQ). */ 2245196777Smav if (pci_get_devid(device_get_parent(dev)) == 0x43801002) 2246196796Smav cpi->maxio = min(cpi->maxio, 128 * 512); 2247195534Sscottl cpi->ccb_h.status = CAM_REQ_CMP; 2248195534Sscottl xpt_done(ccb); 2249195534Sscottl break; 2250195534Sscottl } 2251195534Sscottl default: 2252195534Sscottl ccb->ccb_h.status = CAM_REQ_INVALID; 2253195534Sscottl xpt_done(ccb); 2254195534Sscottl break; 2255195534Sscottl } 2256195534Sscottl} 2257195534Sscottl 2258195534Sscottlstatic void 2259195534Sscottlahcipoll(struct cam_sim *sim) 2260195534Sscottl{ 2261195534Sscottl struct ahci_channel *ch = (struct ahci_channel *)cam_sim_softc(sim); 2262195534Sscottl 2263195534Sscottl ahci_ch_intr(ch->dev); 2264195534Sscottl} 2265