ahci_generic.c revision 248698
1/*- 2 * Copyright (c) 2009-2012 Alexander Motin <mav@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification, immediately at the beginning of the file. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/dev/ahci/ahci.c 248698 2013-03-25 08:50:51Z mav $"); 29 30#include <sys/param.h> 31#include <sys/module.h> 32#include <sys/systm.h> 33#include <sys/kernel.h> 34#include <sys/bus.h> 35#include <sys/conf.h> 36#include <sys/endian.h> 37#include <sys/malloc.h> 38#include <sys/lock.h> 39#include <sys/mutex.h> 40#include <machine/stdarg.h> 41#include <machine/resource.h> 42#include <machine/bus.h> 43#include <sys/rman.h> 44#include <dev/pci/pcivar.h> 45#include <dev/pci/pcireg.h> 46#include "ahci.h" 47 48#include <cam/cam.h> 49#include <cam/cam_ccb.h> 50#include <cam/cam_sim.h> 51#include <cam/cam_xpt_sim.h> 52#include <cam/cam_debug.h> 53 54/* local prototypes */ 55static int ahci_setup_interrupt(device_t dev); 56static void ahci_intr(void *data); 57static void ahci_intr_one(void *data); 58static int ahci_suspend(device_t dev); 59static int ahci_resume(device_t dev); 60static int ahci_ch_init(device_t dev); 61static int ahci_ch_deinit(device_t dev); 62static int ahci_ch_suspend(device_t dev); 63static int ahci_ch_resume(device_t dev); 64static void ahci_ch_pm(void *arg); 65static void ahci_ch_intr_locked(void *data); 66static void ahci_ch_intr(void *data); 67static int ahci_ctlr_reset(device_t dev); 68static int ahci_ctlr_setup(device_t dev); 69static void ahci_begin_transaction(device_t dev, union ccb *ccb); 70static void ahci_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error); 71static void ahci_execute_transaction(struct ahci_slot *slot); 72static void ahci_timeout(struct ahci_slot *slot); 73static void ahci_end_transaction(struct ahci_slot *slot, enum ahci_err_type et); 74static int ahci_setup_fis(device_t dev, struct ahci_cmd_tab *ctp, union ccb *ccb, int tag); 75static void ahci_dmainit(device_t dev); 76static void ahci_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error); 77static void ahci_dmafini(device_t dev); 78static void ahci_slotsalloc(device_t dev); 79static void ahci_slotsfree(device_t dev); 80static void ahci_reset(device_t dev); 81static void ahci_start(device_t dev, int fbs); 82static void ahci_stop(device_t dev); 83static void ahci_clo(device_t dev); 84static void ahci_start_fr(device_t dev); 85static void ahci_stop_fr(device_t dev); 86 87static int ahci_sata_connect(struct ahci_channel *ch); 88static int ahci_sata_phy_reset(device_t dev); 89static int ahci_wait_ready(device_t dev, int t, int t0); 90 91static void ahci_issue_recovery(device_t dev); 92static void ahci_process_read_log(device_t dev, union ccb *ccb); 93static void ahci_process_request_sense(device_t dev, union ccb *ccb); 94 95static void ahciaction(struct cam_sim *sim, union ccb *ccb); 96static void ahcipoll(struct cam_sim *sim); 97 98static MALLOC_DEFINE(M_AHCI, "AHCI driver", "AHCI driver data buffers"); 99 100static struct { 101 uint32_t id; 102 uint8_t rev; 103 const char *name; 104 int quirks; 105#define AHCI_Q_NOFORCE 1 106#define AHCI_Q_NOPMP 2 107#define AHCI_Q_NONCQ 4 108#define AHCI_Q_1CH 8 109#define AHCI_Q_2CH 16 110#define AHCI_Q_4CH 32 111#define AHCI_Q_EDGEIS 64 112#define AHCI_Q_SATA2 128 113#define AHCI_Q_NOBSYRES 256 114#define AHCI_Q_NOAA 512 115#define AHCI_Q_NOCOUNT 1024 116#define AHCI_Q_ALTSIG 2048 117#define AHCI_Q_NOMSI 4096 118} ahci_ids[] = { 119 {0x43801002, 0x00, "ATI IXP600", AHCI_Q_NOMSI}, 120 {0x43901002, 0x00, "ATI IXP700", 0}, 121 {0x43911002, 0x00, "ATI IXP700", 0}, 122 {0x43921002, 0x00, "ATI IXP700", 0}, 123 {0x43931002, 0x00, "ATI IXP700", 0}, 124 {0x43941002, 0x00, "ATI IXP800", 0}, 125 {0x43951002, 0x00, "ATI IXP800", 0}, 126 {0x78001022, 0x00, "AMD Hudson-2", 0}, 127 {0x78011022, 0x00, "AMD Hudson-2", 0}, 128 {0x78021022, 0x00, "AMD Hudson-2", 0}, 129 {0x78031022, 0x00, "AMD Hudson-2", 0}, 130 {0x78041022, 0x00, "AMD Hudson-2", 0}, 131 {0x06121b21, 0x00, "ASMedia ASM1061", 0}, 132 {0x26528086, 0x00, "Intel ICH6", AHCI_Q_NOFORCE}, 133 {0x26538086, 0x00, "Intel ICH6M", AHCI_Q_NOFORCE}, 134 {0x26818086, 0x00, "Intel ESB2", 0}, 135 {0x26828086, 0x00, "Intel ESB2", 0}, 136 {0x26838086, 0x00, "Intel ESB2", 0}, 137 {0x27c18086, 0x00, "Intel ICH7", 0}, 138 {0x27c38086, 0x00, "Intel ICH7", 0}, 139 {0x27c58086, 0x00, "Intel ICH7M", 0}, 140 {0x27c68086, 0x00, "Intel ICH7M", 0}, 141 {0x28218086, 0x00, "Intel ICH8", 0}, 142 {0x28228086, 0x00, "Intel ICH8", 0}, 143 {0x28248086, 0x00, "Intel ICH8", 0}, 144 {0x28298086, 0x00, "Intel ICH8M", 0}, 145 {0x282a8086, 0x00, "Intel ICH8M", 0}, 146 {0x29228086, 0x00, "Intel ICH9", 0}, 147 {0x29238086, 0x00, "Intel ICH9", 0}, 148 {0x29248086, 0x00, "Intel ICH9", 0}, 149 {0x29258086, 0x00, "Intel ICH9", 0}, 150 {0x29278086, 0x00, "Intel ICH9", 0}, 151 {0x29298086, 0x00, "Intel ICH9M", 0}, 152 {0x292a8086, 0x00, "Intel ICH9M", 0}, 153 {0x292b8086, 0x00, "Intel ICH9M", 0}, 154 {0x292c8086, 0x00, "Intel ICH9M", 0}, 155 {0x292f8086, 0x00, "Intel ICH9M", 0}, 156 {0x294d8086, 0x00, "Intel ICH9", 0}, 157 {0x294e8086, 0x00, "Intel ICH9M", 0}, 158 {0x3a058086, 0x00, "Intel ICH10", 0}, 159 {0x3a228086, 0x00, "Intel ICH10", 0}, 160 {0x3a258086, 0x00, "Intel ICH10", 0}, 161 {0x3b228086, 0x00, "Intel 5 Series/3400 Series", 0}, 162 {0x3b238086, 0x00, "Intel 5 Series/3400 Series", 0}, 163 {0x3b258086, 0x00, "Intel 5 Series/3400 Series", 0}, 164 {0x3b298086, 0x00, "Intel 5 Series/3400 Series", 0}, 165 {0x3b2c8086, 0x00, "Intel 5 Series/3400 Series", 0}, 166 {0x3b2f8086, 0x00, "Intel 5 Series/3400 Series", 0}, 167 {0x1c028086, 0x00, "Intel Cougar Point", 0}, 168 {0x1c038086, 0x00, "Intel Cougar Point", 0}, 169 {0x1c048086, 0x00, "Intel Cougar Point", 0}, 170 {0x1c058086, 0x00, "Intel Cougar Point", 0}, 171 {0x1d028086, 0x00, "Intel Patsburg", 0}, 172 {0x1d048086, 0x00, "Intel Patsburg", 0}, 173 {0x1d068086, 0x00, "Intel Patsburg", 0}, 174 {0x28268086, 0x00, "Intel Patsburg (RAID)", 0}, 175 {0x1e028086, 0x00, "Intel Panther Point", 0}, 176 {0x1e038086, 0x00, "Intel Panther Point", 0}, 177 {0x1e048086, 0x00, "Intel Panther Point", 0}, 178 {0x1e058086, 0x00, "Intel Panther Point", 0}, 179 {0x1e068086, 0x00, "Intel Panther Point", 0}, 180 {0x1e078086, 0x00, "Intel Panther Point", 0}, 181 {0x1e0e8086, 0x00, "Intel Panther Point", 0}, 182 {0x1e0f8086, 0x00, "Intel Panther Point", 0}, 183 {0x8c028086, 0x00, "Intel Lynx Point", 0}, 184 {0x8c038086, 0x00, "Intel Lynx Point", 0}, 185 {0x8c048086, 0x00, "Intel Lynx Point", 0}, 186 {0x8c058086, 0x00, "Intel Lynx Point", 0}, 187 {0x8c068086, 0x00, "Intel Lynx Point", 0}, 188 {0x8c078086, 0x00, "Intel Lynx Point", 0}, 189 {0x8c0e8086, 0x00, "Intel Lynx Point", 0}, 190 {0x8c0f8086, 0x00, "Intel Lynx Point", 0}, 191 {0x23238086, 0x00, "Intel DH89xxCC", 0}, 192 {0x2360197b, 0x00, "JMicron JMB360", 0}, 193 {0x2361197b, 0x00, "JMicron JMB361", AHCI_Q_NOFORCE}, 194 {0x2362197b, 0x00, "JMicron JMB362", 0}, 195 {0x2363197b, 0x00, "JMicron JMB363", AHCI_Q_NOFORCE}, 196 {0x2365197b, 0x00, "JMicron JMB365", AHCI_Q_NOFORCE}, 197 {0x2366197b, 0x00, "JMicron JMB366", AHCI_Q_NOFORCE}, 198 {0x2368197b, 0x00, "JMicron JMB368", AHCI_Q_NOFORCE}, 199 {0x611111ab, 0x00, "Marvell 88SE6111", AHCI_Q_NOFORCE | AHCI_Q_1CH | 200 AHCI_Q_EDGEIS}, 201 {0x612111ab, 0x00, "Marvell 88SE6121", AHCI_Q_NOFORCE | AHCI_Q_2CH | 202 AHCI_Q_EDGEIS | AHCI_Q_NONCQ | AHCI_Q_NOCOUNT}, 203 {0x614111ab, 0x00, "Marvell 88SE6141", AHCI_Q_NOFORCE | AHCI_Q_4CH | 204 AHCI_Q_EDGEIS | AHCI_Q_NONCQ | AHCI_Q_NOCOUNT}, 205 {0x614511ab, 0x00, "Marvell 88SE6145", AHCI_Q_NOFORCE | AHCI_Q_4CH | 206 AHCI_Q_EDGEIS | AHCI_Q_NONCQ | AHCI_Q_NOCOUNT}, 207 {0x91201b4b, 0x00, "Marvell 88SE912x", AHCI_Q_EDGEIS|AHCI_Q_NOBSYRES}, 208 {0x91231b4b, 0x11, "Marvell 88SE912x", AHCI_Q_NOBSYRES|AHCI_Q_ALTSIG}, 209 {0x91231b4b, 0x00, "Marvell 88SE912x", AHCI_Q_EDGEIS|AHCI_Q_SATA2|AHCI_Q_NOBSYRES}, 210 {0x91251b4b, 0x00, "Marvell 88SE9125", AHCI_Q_NOBSYRES}, 211 {0x91281b4b, 0x00, "Marvell 88SE9128", AHCI_Q_NOBSYRES|AHCI_Q_ALTSIG}, 212 {0x91301b4b, 0x00, "Marvell 88SE9130", AHCI_Q_NOBSYRES|AHCI_Q_ALTSIG}, 213 {0x91721b4b, 0x00, "Marvell 88SE9172", AHCI_Q_NOBSYRES}, 214 {0x91821b4b, 0x00, "Marvell 88SE9182", AHCI_Q_NOBSYRES}, 215 {0x92201b4b, 0x00, "Marvell 88SE9220", AHCI_Q_NOBSYRES|AHCI_Q_ALTSIG}, 216 {0x92301b4b, 0x00, "Marvell 88SE9230", AHCI_Q_NOBSYRES|AHCI_Q_ALTSIG}, 217 {0x92351b4b, 0x00, "Marvell 88SE9235", AHCI_Q_NOBSYRES}, 218 {0x06201103, 0x00, "HighPoint RocketRAID 620", AHCI_Q_NOBSYRES}, 219 {0x06201b4b, 0x00, "HighPoint RocketRAID 620", AHCI_Q_NOBSYRES}, 220 {0x06221103, 0x00, "HighPoint RocketRAID 622", AHCI_Q_NOBSYRES}, 221 {0x06221b4b, 0x00, "HighPoint RocketRAID 622", AHCI_Q_NOBSYRES}, 222 {0x06401103, 0x00, "HighPoint RocketRAID 640", AHCI_Q_NOBSYRES}, 223 {0x06401b4b, 0x00, "HighPoint RocketRAID 640", AHCI_Q_NOBSYRES}, 224 {0x06441103, 0x00, "HighPoint RocketRAID 644", AHCI_Q_NOBSYRES}, 225 {0x06441b4b, 0x00, "HighPoint RocketRAID 644", AHCI_Q_NOBSYRES}, 226 {0x044c10de, 0x00, "NVIDIA MCP65", AHCI_Q_NOAA}, 227 {0x044d10de, 0x00, "NVIDIA MCP65", AHCI_Q_NOAA}, 228 {0x044e10de, 0x00, "NVIDIA MCP65", AHCI_Q_NOAA}, 229 {0x044f10de, 0x00, "NVIDIA MCP65", AHCI_Q_NOAA}, 230 {0x045c10de, 0x00, "NVIDIA MCP65", AHCI_Q_NOAA}, 231 {0x045d10de, 0x00, "NVIDIA MCP65", AHCI_Q_NOAA}, 232 {0x045e10de, 0x00, "NVIDIA MCP65", AHCI_Q_NOAA}, 233 {0x045f10de, 0x00, "NVIDIA MCP65", AHCI_Q_NOAA}, 234 {0x055010de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA}, 235 {0x055110de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA}, 236 {0x055210de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA}, 237 {0x055310de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA}, 238 {0x055410de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA}, 239 {0x055510de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA}, 240 {0x055610de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA}, 241 {0x055710de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA}, 242 {0x055810de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA}, 243 {0x055910de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA}, 244 {0x055A10de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA}, 245 {0x055B10de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA}, 246 {0x058410de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA}, 247 {0x07f010de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA}, 248 {0x07f110de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA}, 249 {0x07f210de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA}, 250 {0x07f310de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA}, 251 {0x07f410de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA}, 252 {0x07f510de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA}, 253 {0x07f610de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA}, 254 {0x07f710de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA}, 255 {0x07f810de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA}, 256 {0x07f910de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA}, 257 {0x07fa10de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA}, 258 {0x07fb10de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA}, 259 {0x0ad010de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA}, 260 {0x0ad110de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA}, 261 {0x0ad210de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA}, 262 {0x0ad310de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA}, 263 {0x0ad410de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA}, 264 {0x0ad510de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA}, 265 {0x0ad610de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA}, 266 {0x0ad710de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA}, 267 {0x0ad810de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA}, 268 {0x0ad910de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA}, 269 {0x0ada10de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA}, 270 {0x0adb10de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA}, 271 {0x0ab410de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA}, 272 {0x0ab510de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA}, 273 {0x0ab610de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA}, 274 {0x0ab710de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA}, 275 {0x0ab810de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA}, 276 {0x0ab910de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA}, 277 {0x0aba10de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA}, 278 {0x0abb10de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA}, 279 {0x0abc10de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA}, 280 {0x0abd10de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA}, 281 {0x0abe10de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA}, 282 {0x0abf10de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA}, 283 {0x0d8410de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA}, 284 {0x0d8510de, 0x00, "NVIDIA MCP89", AHCI_Q_NOFORCE|AHCI_Q_NOAA}, 285 {0x0d8610de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA}, 286 {0x0d8710de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA}, 287 {0x0d8810de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA}, 288 {0x0d8910de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA}, 289 {0x0d8a10de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA}, 290 {0x0d8b10de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA}, 291 {0x0d8c10de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA}, 292 {0x0d8d10de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA}, 293 {0x0d8e10de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA}, 294 {0x0d8f10de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA}, 295 {0x33491106, 0x00, "VIA VT8251", AHCI_Q_NOPMP|AHCI_Q_NONCQ}, 296 {0x62871106, 0x00, "VIA VT8251", AHCI_Q_NOPMP|AHCI_Q_NONCQ}, 297 {0x11841039, 0x00, "SiS 966", 0}, 298 {0x11851039, 0x00, "SiS 968", 0}, 299 {0x01861039, 0x00, "SiS 968", 0}, 300 {0x00000000, 0x00, NULL, 0} 301}; 302 303#define recovery_type spriv_field0 304#define RECOVERY_NONE 0 305#define RECOVERY_READ_LOG 1 306#define RECOVERY_REQUEST_SENSE 2 307#define recovery_slot spriv_field1 308 309static int force_ahci = 1; 310TUNABLE_INT("hw.ahci.force", &force_ahci); 311 312static int 313ahci_probe(device_t dev) 314{ 315 char buf[64]; 316 int i, valid = 0; 317 uint32_t devid = pci_get_devid(dev); 318 uint8_t revid = pci_get_revid(dev); 319 320 /* Is this a possible AHCI candidate? */ 321 if (pci_get_class(dev) == PCIC_STORAGE && 322 pci_get_subclass(dev) == PCIS_STORAGE_SATA && 323 pci_get_progif(dev) == PCIP_STORAGE_SATA_AHCI_1_0) 324 valid = 1; 325 /* Is this a known AHCI chip? */ 326 for (i = 0; ahci_ids[i].id != 0; i++) { 327 if (ahci_ids[i].id == devid && 328 ahci_ids[i].rev <= revid && 329 (valid || (force_ahci == 1 && 330 !(ahci_ids[i].quirks & AHCI_Q_NOFORCE)))) { 331 /* Do not attach JMicrons with single PCI function. */ 332 if (pci_get_vendor(dev) == 0x197b && 333 (pci_read_config(dev, 0xdf, 1) & 0x40) == 0) 334 return (ENXIO); 335 snprintf(buf, sizeof(buf), "%s AHCI SATA controller", 336 ahci_ids[i].name); 337 device_set_desc_copy(dev, buf); 338 return (BUS_PROBE_VENDOR); 339 } 340 } 341 if (!valid) 342 return (ENXIO); 343 device_set_desc_copy(dev, "AHCI SATA controller"); 344 return (BUS_PROBE_VENDOR); 345} 346 347static int 348ahci_ata_probe(device_t dev) 349{ 350 char buf[64]; 351 int i; 352 uint32_t devid = pci_get_devid(dev); 353 uint8_t revid = pci_get_revid(dev); 354 355 if ((intptr_t)device_get_ivars(dev) >= 0) 356 return (ENXIO); 357 /* Is this a known AHCI chip? */ 358 for (i = 0; ahci_ids[i].id != 0; i++) { 359 if (ahci_ids[i].id == devid && 360 ahci_ids[i].rev <= revid) { 361 snprintf(buf, sizeof(buf), "%s AHCI SATA controller", 362 ahci_ids[i].name); 363 device_set_desc_copy(dev, buf); 364 return (BUS_PROBE_VENDOR); 365 } 366 } 367 device_set_desc_copy(dev, "AHCI SATA controller"); 368 return (BUS_PROBE_VENDOR); 369} 370 371static int 372ahci_attach(device_t dev) 373{ 374 struct ahci_controller *ctlr = device_get_softc(dev); 375 device_t child; 376 int error, unit, speed, i; 377 uint32_t devid = pci_get_devid(dev); 378 uint8_t revid = pci_get_revid(dev); 379 u_int32_t version; 380 381 ctlr->dev = dev; 382 i = 0; 383 while (ahci_ids[i].id != 0 && 384 (ahci_ids[i].id != devid || 385 ahci_ids[i].rev > revid)) 386 i++; 387 ctlr->quirks = ahci_ids[i].quirks; 388 resource_int_value(device_get_name(dev), 389 device_get_unit(dev), "ccc", &ctlr->ccc); 390 /* if we have a memory BAR(5) we are likely on an AHCI part */ 391 ctlr->r_rid = PCIR_BAR(5); 392 if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 393 &ctlr->r_rid, RF_ACTIVE))) 394 return ENXIO; 395 /* Setup our own memory management for channels. */ 396 ctlr->sc_iomem.rm_start = rman_get_start(ctlr->r_mem); 397 ctlr->sc_iomem.rm_end = rman_get_end(ctlr->r_mem); 398 ctlr->sc_iomem.rm_type = RMAN_ARRAY; 399 ctlr->sc_iomem.rm_descr = "I/O memory addresses"; 400 if ((error = rman_init(&ctlr->sc_iomem)) != 0) { 401 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 402 return (error); 403 } 404 if ((error = rman_manage_region(&ctlr->sc_iomem, 405 rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) { 406 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 407 rman_fini(&ctlr->sc_iomem); 408 return (error); 409 } 410 pci_enable_busmaster(dev); 411 /* Reset controller */ 412 if ((error = ahci_ctlr_reset(dev)) != 0) { 413 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 414 rman_fini(&ctlr->sc_iomem); 415 return (error); 416 }; 417 /* Get the HW capabilities */ 418 version = ATA_INL(ctlr->r_mem, AHCI_VS); 419 ctlr->caps = ATA_INL(ctlr->r_mem, AHCI_CAP); 420 if (version >= 0x00010200) 421 ctlr->caps2 = ATA_INL(ctlr->r_mem, AHCI_CAP2); 422 if (ctlr->caps & AHCI_CAP_EMS) 423 ctlr->capsem = ATA_INL(ctlr->r_mem, AHCI_EM_CTL); 424 ctlr->ichannels = ATA_INL(ctlr->r_mem, AHCI_PI); 425 426 /* Identify and set separate quirks for HBA and RAID f/w Marvells. */ 427 if ((ctlr->quirks & AHCI_Q_NOBSYRES) && 428 (ctlr->quirks & AHCI_Q_ALTSIG) && 429 (ctlr->caps & AHCI_CAP_SPM) == 0) 430 ctlr->quirks &= ~AHCI_Q_NOBSYRES; 431 432 if (ctlr->quirks & AHCI_Q_1CH) { 433 ctlr->caps &= ~AHCI_CAP_NPMASK; 434 ctlr->ichannels &= 0x01; 435 } 436 if (ctlr->quirks & AHCI_Q_2CH) { 437 ctlr->caps &= ~AHCI_CAP_NPMASK; 438 ctlr->caps |= 1; 439 ctlr->ichannels &= 0x03; 440 } 441 if (ctlr->quirks & AHCI_Q_4CH) { 442 ctlr->caps &= ~AHCI_CAP_NPMASK; 443 ctlr->caps |= 3; 444 ctlr->ichannels &= 0x0f; 445 } 446 ctlr->channels = MAX(flsl(ctlr->ichannels), 447 (ctlr->caps & AHCI_CAP_NPMASK) + 1); 448 if (ctlr->quirks & AHCI_Q_NOPMP) 449 ctlr->caps &= ~AHCI_CAP_SPM; 450 if (ctlr->quirks & AHCI_Q_NONCQ) 451 ctlr->caps &= ~AHCI_CAP_SNCQ; 452 if ((ctlr->caps & AHCI_CAP_CCCS) == 0) 453 ctlr->ccc = 0; 454 ctlr->emloc = ATA_INL(ctlr->r_mem, AHCI_EM_LOC); 455 ahci_ctlr_setup(dev); 456 /* Setup interrupts. */ 457 if (ahci_setup_interrupt(dev)) { 458 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 459 rman_fini(&ctlr->sc_iomem); 460 return ENXIO; 461 } 462 /* Announce HW capabilities. */ 463 speed = (ctlr->caps & AHCI_CAP_ISS) >> AHCI_CAP_ISS_SHIFT; 464 device_printf(dev, 465 "AHCI v%x.%02x with %d %sGbps ports, Port Multiplier %s%s\n", 466 ((version >> 20) & 0xf0) + ((version >> 16) & 0x0f), 467 ((version >> 4) & 0xf0) + (version & 0x0f), 468 (ctlr->caps & AHCI_CAP_NPMASK) + 1, 469 ((speed == 1) ? "1.5":((speed == 2) ? "3": 470 ((speed == 3) ? "6":"?"))), 471 (ctlr->caps & AHCI_CAP_SPM) ? 472 "supported" : "not supported", 473 (ctlr->caps & AHCI_CAP_FBSS) ? 474 " with FBS" : ""); 475 if (bootverbose) { 476 device_printf(dev, "Caps:%s%s%s%s%s%s%s%s %sGbps", 477 (ctlr->caps & AHCI_CAP_64BIT) ? " 64bit":"", 478 (ctlr->caps & AHCI_CAP_SNCQ) ? " NCQ":"", 479 (ctlr->caps & AHCI_CAP_SSNTF) ? " SNTF":"", 480 (ctlr->caps & AHCI_CAP_SMPS) ? " MPS":"", 481 (ctlr->caps & AHCI_CAP_SSS) ? " SS":"", 482 (ctlr->caps & AHCI_CAP_SALP) ? " ALP":"", 483 (ctlr->caps & AHCI_CAP_SAL) ? " AL":"", 484 (ctlr->caps & AHCI_CAP_SCLO) ? " CLO":"", 485 ((speed == 1) ? "1.5":((speed == 2) ? "3": 486 ((speed == 3) ? "6":"?")))); 487 printf("%s%s%s%s%s%s %dcmd%s%s%s %dports\n", 488 (ctlr->caps & AHCI_CAP_SAM) ? " AM":"", 489 (ctlr->caps & AHCI_CAP_SPM) ? " PM":"", 490 (ctlr->caps & AHCI_CAP_FBSS) ? " FBS":"", 491 (ctlr->caps & AHCI_CAP_PMD) ? " PMD":"", 492 (ctlr->caps & AHCI_CAP_SSC) ? " SSC":"", 493 (ctlr->caps & AHCI_CAP_PSC) ? " PSC":"", 494 ((ctlr->caps & AHCI_CAP_NCS) >> AHCI_CAP_NCS_SHIFT) + 1, 495 (ctlr->caps & AHCI_CAP_CCCS) ? " CCC":"", 496 (ctlr->caps & AHCI_CAP_EMS) ? " EM":"", 497 (ctlr->caps & AHCI_CAP_SXS) ? " eSATA":"", 498 (ctlr->caps & AHCI_CAP_NPMASK) + 1); 499 } 500 if (bootverbose && version >= 0x00010200) { 501 device_printf(dev, "Caps2:%s%s%s\n", 502 (ctlr->caps2 & AHCI_CAP2_APST) ? " APST":"", 503 (ctlr->caps2 & AHCI_CAP2_NVMP) ? " NVMP":"", 504 (ctlr->caps2 & AHCI_CAP2_BOH) ? " BOH":""); 505 } 506 /* Attach all channels on this controller */ 507 for (unit = 0; unit < ctlr->channels; unit++) { 508 child = device_add_child(dev, "ahcich", -1); 509 if (child == NULL) { 510 device_printf(dev, "failed to add channel device\n"); 511 continue; 512 } 513 device_set_ivars(child, (void *)(intptr_t)unit); 514 if ((ctlr->ichannels & (1 << unit)) == 0) 515 device_disable(child); 516 } 517 if (ctlr->caps & AHCI_CAP_EMS) { 518 child = device_add_child(dev, "ahciem", -1); 519 if (child == NULL) 520 device_printf(dev, "failed to add enclosure device\n"); 521 else 522 device_set_ivars(child, (void *)(intptr_t)-1); 523 } 524 bus_generic_attach(dev); 525 return 0; 526} 527 528static int 529ahci_detach(device_t dev) 530{ 531 struct ahci_controller *ctlr = device_get_softc(dev); 532 int i; 533 534 /* Detach & delete all children */ 535 device_delete_children(dev); 536 537 /* Free interrupts. */ 538 for (i = 0; i < ctlr->numirqs; i++) { 539 if (ctlr->irqs[i].r_irq) { 540 bus_teardown_intr(dev, ctlr->irqs[i].r_irq, 541 ctlr->irqs[i].handle); 542 bus_release_resource(dev, SYS_RES_IRQ, 543 ctlr->irqs[i].r_irq_rid, ctlr->irqs[i].r_irq); 544 } 545 } 546 pci_release_msi(dev); 547 /* Free memory. */ 548 rman_fini(&ctlr->sc_iomem); 549 if (ctlr->r_mem) 550 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 551 return (0); 552} 553 554static int 555ahci_ctlr_reset(device_t dev) 556{ 557 struct ahci_controller *ctlr = device_get_softc(dev); 558 int timeout; 559 560 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == 0x28298086 && 561 (pci_read_config(dev, 0x92, 1) & 0xfe) == 0x04) 562 pci_write_config(dev, 0x92, 0x01, 1); 563 /* Enable AHCI mode */ 564 ATA_OUTL(ctlr->r_mem, AHCI_GHC, AHCI_GHC_AE); 565 /* Reset AHCI controller */ 566 ATA_OUTL(ctlr->r_mem, AHCI_GHC, AHCI_GHC_AE|AHCI_GHC_HR); 567 for (timeout = 1000; timeout > 0; timeout--) { 568 DELAY(1000); 569 if ((ATA_INL(ctlr->r_mem, AHCI_GHC) & AHCI_GHC_HR) == 0) 570 break; 571 } 572 if (timeout == 0) { 573 device_printf(dev, "AHCI controller reset failure\n"); 574 return ENXIO; 575 } 576 /* Reenable AHCI mode */ 577 ATA_OUTL(ctlr->r_mem, AHCI_GHC, AHCI_GHC_AE); 578 return (0); 579} 580 581static int 582ahci_ctlr_setup(device_t dev) 583{ 584 struct ahci_controller *ctlr = device_get_softc(dev); 585 /* Clear interrupts */ 586 ATA_OUTL(ctlr->r_mem, AHCI_IS, ATA_INL(ctlr->r_mem, AHCI_IS)); 587 /* Configure CCC */ 588 if (ctlr->ccc) { 589 ATA_OUTL(ctlr->r_mem, AHCI_CCCP, ATA_INL(ctlr->r_mem, AHCI_PI)); 590 ATA_OUTL(ctlr->r_mem, AHCI_CCCC, 591 (ctlr->ccc << AHCI_CCCC_TV_SHIFT) | 592 (4 << AHCI_CCCC_CC_SHIFT) | 593 AHCI_CCCC_EN); 594 ctlr->cccv = (ATA_INL(ctlr->r_mem, AHCI_CCCC) & 595 AHCI_CCCC_INT_MASK) >> AHCI_CCCC_INT_SHIFT; 596 if (bootverbose) { 597 device_printf(dev, 598 "CCC with %dms/4cmd enabled on vector %d\n", 599 ctlr->ccc, ctlr->cccv); 600 } 601 } 602 /* Enable AHCI interrupts */ 603 ATA_OUTL(ctlr->r_mem, AHCI_GHC, 604 ATA_INL(ctlr->r_mem, AHCI_GHC) | AHCI_GHC_IE); 605 return (0); 606} 607 608static int 609ahci_suspend(device_t dev) 610{ 611 struct ahci_controller *ctlr = device_get_softc(dev); 612 613 bus_generic_suspend(dev); 614 /* Disable interupts, so the state change(s) doesn't trigger */ 615 ATA_OUTL(ctlr->r_mem, AHCI_GHC, 616 ATA_INL(ctlr->r_mem, AHCI_GHC) & (~AHCI_GHC_IE)); 617 return 0; 618} 619 620static int 621ahci_resume(device_t dev) 622{ 623 int res; 624 625 if ((res = ahci_ctlr_reset(dev)) != 0) 626 return (res); 627 ahci_ctlr_setup(dev); 628 return (bus_generic_resume(dev)); 629} 630 631static int 632ahci_setup_interrupt(device_t dev) 633{ 634 struct ahci_controller *ctlr = device_get_softc(dev); 635 int i, msi = 1; 636 637 /* Process hints. */ 638 if (ctlr->quirks & AHCI_Q_NOMSI) 639 msi = 0; 640 resource_int_value(device_get_name(dev), 641 device_get_unit(dev), "msi", &msi); 642 if (msi < 0) 643 msi = 0; 644 else if (msi == 1) 645 msi = min(1, pci_msi_count(dev)); 646 else if (msi > 1) 647 msi = pci_msi_count(dev); 648 /* Allocate MSI if needed/present. */ 649 if (msi && pci_alloc_msi(dev, &msi) == 0) { 650 ctlr->numirqs = msi; 651 } else { 652 msi = 0; 653 ctlr->numirqs = 1; 654 } 655 /* Check for single MSI vector fallback. */ 656 if (ctlr->numirqs > 1 && 657 (ATA_INL(ctlr->r_mem, AHCI_GHC) & AHCI_GHC_MRSM) != 0) { 658 device_printf(dev, "Falling back to one MSI\n"); 659 ctlr->numirqs = 1; 660 } 661 /* Allocate all IRQs. */ 662 for (i = 0; i < ctlr->numirqs; i++) { 663 ctlr->irqs[i].ctlr = ctlr; 664 ctlr->irqs[i].r_irq_rid = i + (msi ? 1 : 0); 665 if (ctlr->numirqs == 1 || i >= ctlr->channels || 666 (ctlr->ccc && i == ctlr->cccv)) 667 ctlr->irqs[i].mode = AHCI_IRQ_MODE_ALL; 668 else if (i == ctlr->numirqs - 1) 669 ctlr->irqs[i].mode = AHCI_IRQ_MODE_AFTER; 670 else 671 ctlr->irqs[i].mode = AHCI_IRQ_MODE_ONE; 672 if (!(ctlr->irqs[i].r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 673 &ctlr->irqs[i].r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) { 674 device_printf(dev, "unable to map interrupt\n"); 675 return ENXIO; 676 } 677 if ((bus_setup_intr(dev, ctlr->irqs[i].r_irq, ATA_INTR_FLAGS, NULL, 678 (ctlr->irqs[i].mode == AHCI_IRQ_MODE_ONE) ? ahci_intr_one : ahci_intr, 679 &ctlr->irqs[i], &ctlr->irqs[i].handle))) { 680 /* SOS XXX release r_irq */ 681 device_printf(dev, "unable to setup interrupt\n"); 682 return ENXIO; 683 } 684 if (ctlr->numirqs > 1) { 685 bus_describe_intr(dev, ctlr->irqs[i].r_irq, 686 ctlr->irqs[i].handle, 687 ctlr->irqs[i].mode == AHCI_IRQ_MODE_ONE ? 688 "ch%d" : "%d", i); 689 } 690 } 691 return (0); 692} 693 694/* 695 * Common case interrupt handler. 696 */ 697static void 698ahci_intr(void *data) 699{ 700 struct ahci_controller_irq *irq = data; 701 struct ahci_controller *ctlr = irq->ctlr; 702 u_int32_t is, ise = 0; 703 void *arg; 704 int unit; 705 706 if (irq->mode == AHCI_IRQ_MODE_ALL) { 707 unit = 0; 708 if (ctlr->ccc) 709 is = ctlr->ichannels; 710 else 711 is = ATA_INL(ctlr->r_mem, AHCI_IS); 712 } else { /* AHCI_IRQ_MODE_AFTER */ 713 unit = irq->r_irq_rid - 1; 714 is = ATA_INL(ctlr->r_mem, AHCI_IS); 715 } 716 /* CCC interrupt is edge triggered. */ 717 if (ctlr->ccc) 718 ise = 1 << ctlr->cccv; 719 /* Some controllers have edge triggered IS. */ 720 if (ctlr->quirks & AHCI_Q_EDGEIS) 721 ise |= is; 722 if (ise != 0) 723 ATA_OUTL(ctlr->r_mem, AHCI_IS, ise); 724 for (; unit < ctlr->channels; unit++) { 725 if ((is & (1 << unit)) != 0 && 726 (arg = ctlr->interrupt[unit].argument)) { 727 ctlr->interrupt[unit].function(arg); 728 } 729 } 730 /* AHCI declares level triggered IS. */ 731 if (!(ctlr->quirks & AHCI_Q_EDGEIS)) 732 ATA_OUTL(ctlr->r_mem, AHCI_IS, is); 733} 734 735/* 736 * Simplified interrupt handler for multivector MSI mode. 737 */ 738static void 739ahci_intr_one(void *data) 740{ 741 struct ahci_controller_irq *irq = data; 742 struct ahci_controller *ctlr = irq->ctlr; 743 void *arg; 744 int unit; 745 746 unit = irq->r_irq_rid - 1; 747 /* Some controllers have edge triggered IS. */ 748 if (ctlr->quirks & AHCI_Q_EDGEIS) 749 ATA_OUTL(ctlr->r_mem, AHCI_IS, 1 << unit); 750 if ((arg = ctlr->interrupt[unit].argument)) 751 ctlr->interrupt[unit].function(arg); 752 /* AHCI declares level triggered IS. */ 753 if (!(ctlr->quirks & AHCI_Q_EDGEIS)) 754 ATA_OUTL(ctlr->r_mem, AHCI_IS, 1 << unit); 755} 756 757static struct resource * 758ahci_alloc_resource(device_t dev, device_t child, int type, int *rid, 759 u_long start, u_long end, u_long count, u_int flags) 760{ 761 struct ahci_controller *ctlr = device_get_softc(dev); 762 struct resource *res; 763 long st; 764 int offset, size, unit; 765 766 unit = (intptr_t)device_get_ivars(child); 767 res = NULL; 768 switch (type) { 769 case SYS_RES_MEMORY: 770 if (unit >= 0) { 771 offset = AHCI_OFFSET + (unit << 7); 772 size = 128; 773 } else if (*rid == 0) { 774 offset = AHCI_EM_CTL; 775 size = 4; 776 } else { 777 offset = (ctlr->emloc & 0xffff0000) >> 14; 778 size = (ctlr->emloc & 0x0000ffff) << 2; 779 if (*rid != 1) { 780 if (*rid == 2 && (ctlr->capsem & 781 (AHCI_EM_XMT | AHCI_EM_SMB)) == 0) 782 offset += size; 783 else 784 break; 785 } 786 } 787 st = rman_get_start(ctlr->r_mem); 788 res = rman_reserve_resource(&ctlr->sc_iomem, st + offset, 789 st + offset + size - 1, size, RF_ACTIVE, child); 790 if (res) { 791 bus_space_handle_t bsh; 792 bus_space_tag_t bst; 793 bsh = rman_get_bushandle(ctlr->r_mem); 794 bst = rman_get_bustag(ctlr->r_mem); 795 bus_space_subregion(bst, bsh, offset, 128, &bsh); 796 rman_set_bushandle(res, bsh); 797 rman_set_bustag(res, bst); 798 } 799 break; 800 case SYS_RES_IRQ: 801 if (*rid == ATA_IRQ_RID) 802 res = ctlr->irqs[0].r_irq; 803 break; 804 } 805 return (res); 806} 807 808static int 809ahci_release_resource(device_t dev, device_t child, int type, int rid, 810 struct resource *r) 811{ 812 813 switch (type) { 814 case SYS_RES_MEMORY: 815 rman_release_resource(r); 816 return (0); 817 case SYS_RES_IRQ: 818 if (rid != ATA_IRQ_RID) 819 return ENOENT; 820 return (0); 821 } 822 return (EINVAL); 823} 824 825static int 826ahci_setup_intr(device_t dev, device_t child, struct resource *irq, 827 int flags, driver_filter_t *filter, driver_intr_t *function, 828 void *argument, void **cookiep) 829{ 830 struct ahci_controller *ctlr = device_get_softc(dev); 831 int unit = (intptr_t)device_get_ivars(child); 832 833 if (filter != NULL) { 834 printf("ahci.c: we cannot use a filter here\n"); 835 return (EINVAL); 836 } 837 ctlr->interrupt[unit].function = function; 838 ctlr->interrupt[unit].argument = argument; 839 return (0); 840} 841 842static int 843ahci_teardown_intr(device_t dev, device_t child, struct resource *irq, 844 void *cookie) 845{ 846 struct ahci_controller *ctlr = device_get_softc(dev); 847 int unit = (intptr_t)device_get_ivars(child); 848 849 ctlr->interrupt[unit].function = NULL; 850 ctlr->interrupt[unit].argument = NULL; 851 return (0); 852} 853 854static int 855ahci_print_child(device_t dev, device_t child) 856{ 857 int retval, channel; 858 859 retval = bus_print_child_header(dev, child); 860 channel = (int)(intptr_t)device_get_ivars(child); 861 if (channel >= 0) 862 retval += printf(" at channel %d", channel); 863 retval += bus_print_child_footer(dev, child); 864 return (retval); 865} 866 867static int 868ahci_child_location_str(device_t dev, device_t child, char *buf, 869 size_t buflen) 870{ 871 int channel; 872 873 channel = (int)(intptr_t)device_get_ivars(child); 874 if (channel >= 0) 875 snprintf(buf, buflen, "channel=%d", channel); 876 return (0); 877} 878 879devclass_t ahci_devclass; 880static device_method_t ahci_methods[] = { 881 DEVMETHOD(device_probe, ahci_probe), 882 DEVMETHOD(device_attach, ahci_attach), 883 DEVMETHOD(device_detach, ahci_detach), 884 DEVMETHOD(device_suspend, ahci_suspend), 885 DEVMETHOD(device_resume, ahci_resume), 886 DEVMETHOD(bus_print_child, ahci_print_child), 887 DEVMETHOD(bus_alloc_resource, ahci_alloc_resource), 888 DEVMETHOD(bus_release_resource, ahci_release_resource), 889 DEVMETHOD(bus_setup_intr, ahci_setup_intr), 890 DEVMETHOD(bus_teardown_intr,ahci_teardown_intr), 891 DEVMETHOD(bus_child_location_str, ahci_child_location_str), 892 { 0, 0 } 893}; 894static driver_t ahci_driver = { 895 "ahci", 896 ahci_methods, 897 sizeof(struct ahci_controller) 898}; 899DRIVER_MODULE(ahci, pci, ahci_driver, ahci_devclass, 0, 0); 900static device_method_t ahci_ata_methods[] = { 901 DEVMETHOD(device_probe, ahci_ata_probe), 902 DEVMETHOD(device_attach, ahci_attach), 903 DEVMETHOD(device_detach, ahci_detach), 904 DEVMETHOD(device_suspend, ahci_suspend), 905 DEVMETHOD(device_resume, ahci_resume), 906 DEVMETHOD(bus_print_child, ahci_print_child), 907 DEVMETHOD(bus_alloc_resource, ahci_alloc_resource), 908 DEVMETHOD(bus_release_resource, ahci_release_resource), 909 DEVMETHOD(bus_setup_intr, ahci_setup_intr), 910 DEVMETHOD(bus_teardown_intr,ahci_teardown_intr), 911 DEVMETHOD(bus_child_location_str, ahci_child_location_str), 912 { 0, 0 } 913}; 914static driver_t ahci_ata_driver = { 915 "ahci", 916 ahci_ata_methods, 917 sizeof(struct ahci_controller) 918}; 919DRIVER_MODULE(ahci, atapci, ahci_ata_driver, ahci_devclass, 0, 0); 920MODULE_VERSION(ahci, 1); 921MODULE_DEPEND(ahci, cam, 1, 1, 1); 922 923static int 924ahci_ch_probe(device_t dev) 925{ 926 927 device_set_desc_copy(dev, "AHCI channel"); 928 return (0); 929} 930 931static int 932ahci_ch_attach(device_t dev) 933{ 934 struct ahci_controller *ctlr = device_get_softc(device_get_parent(dev)); 935 struct ahci_channel *ch = device_get_softc(dev); 936 struct cam_devq *devq; 937 int rid, error, i, sata_rev = 0; 938 u_int32_t version; 939 940 ch->dev = dev; 941 ch->unit = (intptr_t)device_get_ivars(dev); 942 ch->caps = ctlr->caps; 943 ch->caps2 = ctlr->caps2; 944 ch->quirks = ctlr->quirks; 945 ch->numslots = ((ch->caps & AHCI_CAP_NCS) >> AHCI_CAP_NCS_SHIFT) + 1; 946 mtx_init(&ch->mtx, "AHCI channel lock", NULL, MTX_DEF); 947 resource_int_value(device_get_name(dev), 948 device_get_unit(dev), "pm_level", &ch->pm_level); 949 if (ch->pm_level > 3) 950 callout_init_mtx(&ch->pm_timer, &ch->mtx, 0); 951 callout_init_mtx(&ch->reset_timer, &ch->mtx, 0); 952 /* Limit speed for my onboard JMicron external port. 953 * It is not eSATA really. */ 954 if (pci_get_devid(ctlr->dev) == 0x2363197b && 955 pci_get_subvendor(ctlr->dev) == 0x1043 && 956 pci_get_subdevice(ctlr->dev) == 0x81e4 && 957 ch->unit == 0) 958 sata_rev = 1; 959 if (ch->quirks & AHCI_Q_SATA2) 960 sata_rev = 2; 961 resource_int_value(device_get_name(dev), 962 device_get_unit(dev), "sata_rev", &sata_rev); 963 for (i = 0; i < 16; i++) { 964 ch->user[i].revision = sata_rev; 965 ch->user[i].mode = 0; 966 ch->user[i].bytecount = 8192; 967 ch->user[i].tags = ch->numslots; 968 ch->user[i].caps = 0; 969 ch->curr[i] = ch->user[i]; 970 if (ch->pm_level) { 971 ch->user[i].caps = CTS_SATA_CAPS_H_PMREQ | 972 CTS_SATA_CAPS_H_APST | 973 CTS_SATA_CAPS_D_PMREQ | CTS_SATA_CAPS_D_APST; 974 } 975 ch->user[i].caps |= CTS_SATA_CAPS_H_DMAAA | 976 CTS_SATA_CAPS_H_AN; 977 } 978 rid = 0; 979 if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 980 &rid, RF_ACTIVE))) 981 return (ENXIO); 982 ahci_dmainit(dev); 983 ahci_slotsalloc(dev); 984 ahci_ch_init(dev); 985 mtx_lock(&ch->mtx); 986 rid = ATA_IRQ_RID; 987 if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 988 &rid, RF_SHAREABLE | RF_ACTIVE))) { 989 device_printf(dev, "Unable to map interrupt\n"); 990 error = ENXIO; 991 goto err0; 992 } 993 if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL, 994 ahci_ch_intr_locked, dev, &ch->ih))) { 995 device_printf(dev, "Unable to setup interrupt\n"); 996 error = ENXIO; 997 goto err1; 998 } 999 ch->chcaps = ATA_INL(ch->r_mem, AHCI_P_CMD); 1000 version = ATA_INL(ctlr->r_mem, AHCI_VS); 1001 if (version < 0x00010200 && (ctlr->caps & AHCI_CAP_FBSS)) 1002 ch->chcaps |= AHCI_P_CMD_FBSCP; 1003 if (bootverbose) { 1004 device_printf(dev, "Caps:%s%s%s%s%s\n", 1005 (ch->chcaps & AHCI_P_CMD_HPCP) ? " HPCP":"", 1006 (ch->chcaps & AHCI_P_CMD_MPSP) ? " MPSP":"", 1007 (ch->chcaps & AHCI_P_CMD_CPD) ? " CPD":"", 1008 (ch->chcaps & AHCI_P_CMD_ESP) ? " ESP":"", 1009 (ch->chcaps & AHCI_P_CMD_FBSCP) ? " FBSCP":""); 1010 } 1011 /* Create the device queue for our SIM. */ 1012 devq = cam_simq_alloc(ch->numslots); 1013 if (devq == NULL) { 1014 device_printf(dev, "Unable to allocate simq\n"); 1015 error = ENOMEM; 1016 goto err1; 1017 } 1018 /* Construct SIM entry */ 1019 ch->sim = cam_sim_alloc(ahciaction, ahcipoll, "ahcich", ch, 1020 device_get_unit(dev), &ch->mtx, 1021 min(2, ch->numslots), 1022 (ch->caps & AHCI_CAP_SNCQ) ? ch->numslots : 0, 1023 devq); 1024 if (ch->sim == NULL) { 1025 cam_simq_free(devq); 1026 device_printf(dev, "unable to allocate sim\n"); 1027 error = ENOMEM; 1028 goto err1; 1029 } 1030 if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) { 1031 device_printf(dev, "unable to register xpt bus\n"); 1032 error = ENXIO; 1033 goto err2; 1034 } 1035 if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim), 1036 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 1037 device_printf(dev, "unable to create path\n"); 1038 error = ENXIO; 1039 goto err3; 1040 } 1041 if (ch->pm_level > 3) { 1042 callout_reset(&ch->pm_timer, 1043 (ch->pm_level == 4) ? hz / 1000 : hz / 8, 1044 ahci_ch_pm, dev); 1045 } 1046 mtx_unlock(&ch->mtx); 1047 return (0); 1048 1049err3: 1050 xpt_bus_deregister(cam_sim_path(ch->sim)); 1051err2: 1052 cam_sim_free(ch->sim, /*free_devq*/TRUE); 1053err1: 1054 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 1055err0: 1056 bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 1057 mtx_unlock(&ch->mtx); 1058 mtx_destroy(&ch->mtx); 1059 return (error); 1060} 1061 1062static int 1063ahci_ch_detach(device_t dev) 1064{ 1065 struct ahci_channel *ch = device_get_softc(dev); 1066 1067 mtx_lock(&ch->mtx); 1068 xpt_async(AC_LOST_DEVICE, ch->path, NULL); 1069 /* Forget about reset. */ 1070 if (ch->resetting) { 1071 ch->resetting = 0; 1072 xpt_release_simq(ch->sim, TRUE); 1073 } 1074 xpt_free_path(ch->path); 1075 xpt_bus_deregister(cam_sim_path(ch->sim)); 1076 cam_sim_free(ch->sim, /*free_devq*/TRUE); 1077 mtx_unlock(&ch->mtx); 1078 1079 if (ch->pm_level > 3) 1080 callout_drain(&ch->pm_timer); 1081 callout_drain(&ch->reset_timer); 1082 bus_teardown_intr(dev, ch->r_irq, ch->ih); 1083 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 1084 1085 ahci_ch_deinit(dev); 1086 ahci_slotsfree(dev); 1087 ahci_dmafini(dev); 1088 1089 bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem); 1090 mtx_destroy(&ch->mtx); 1091 return (0); 1092} 1093 1094static int 1095ahci_ch_init(device_t dev) 1096{ 1097 struct ahci_channel *ch = device_get_softc(dev); 1098 uint64_t work; 1099 1100 /* Disable port interrupts */ 1101 ATA_OUTL(ch->r_mem, AHCI_P_IE, 0); 1102 /* Setup work areas */ 1103 work = ch->dma.work_bus + AHCI_CL_OFFSET; 1104 ATA_OUTL(ch->r_mem, AHCI_P_CLB, work & 0xffffffff); 1105 ATA_OUTL(ch->r_mem, AHCI_P_CLBU, work >> 32); 1106 work = ch->dma.rfis_bus; 1107 ATA_OUTL(ch->r_mem, AHCI_P_FB, work & 0xffffffff); 1108 ATA_OUTL(ch->r_mem, AHCI_P_FBU, work >> 32); 1109 /* Activate the channel and power/spin up device */ 1110 ATA_OUTL(ch->r_mem, AHCI_P_CMD, 1111 (AHCI_P_CMD_ACTIVE | AHCI_P_CMD_POD | AHCI_P_CMD_SUD | 1112 ((ch->pm_level == 2 || ch->pm_level == 3) ? AHCI_P_CMD_ALPE : 0) | 1113 ((ch->pm_level > 2) ? AHCI_P_CMD_ASP : 0 ))); 1114 ahci_start_fr(dev); 1115 ahci_start(dev, 1); 1116 return (0); 1117} 1118 1119static int 1120ahci_ch_deinit(device_t dev) 1121{ 1122 struct ahci_channel *ch = device_get_softc(dev); 1123 1124 /* Disable port interrupts. */ 1125 ATA_OUTL(ch->r_mem, AHCI_P_IE, 0); 1126 /* Reset command register. */ 1127 ahci_stop(dev); 1128 ahci_stop_fr(dev); 1129 ATA_OUTL(ch->r_mem, AHCI_P_CMD, 0); 1130 /* Allow everything, including partial and slumber modes. */ 1131 ATA_OUTL(ch->r_mem, AHCI_P_SCTL, 0); 1132 /* Request slumber mode transition and give some time to get there. */ 1133 ATA_OUTL(ch->r_mem, AHCI_P_CMD, AHCI_P_CMD_SLUMBER); 1134 DELAY(100); 1135 /* Disable PHY. */ 1136 ATA_OUTL(ch->r_mem, AHCI_P_SCTL, ATA_SC_DET_DISABLE); 1137 return (0); 1138} 1139 1140static int 1141ahci_ch_suspend(device_t dev) 1142{ 1143 struct ahci_channel *ch = device_get_softc(dev); 1144 1145 mtx_lock(&ch->mtx); 1146 xpt_freeze_simq(ch->sim, 1); 1147 /* Forget about reset. */ 1148 if (ch->resetting) { 1149 ch->resetting = 0; 1150 callout_stop(&ch->reset_timer); 1151 xpt_release_simq(ch->sim, TRUE); 1152 } 1153 while (ch->oslots) 1154 msleep(ch, &ch->mtx, PRIBIO, "ahcisusp", hz/100); 1155 ahci_ch_deinit(dev); 1156 mtx_unlock(&ch->mtx); 1157 return (0); 1158} 1159 1160static int 1161ahci_ch_resume(device_t dev) 1162{ 1163 struct ahci_channel *ch = device_get_softc(dev); 1164 1165 mtx_lock(&ch->mtx); 1166 ahci_ch_init(dev); 1167 ahci_reset(dev); 1168 xpt_release_simq(ch->sim, TRUE); 1169 mtx_unlock(&ch->mtx); 1170 return (0); 1171} 1172 1173devclass_t ahcich_devclass; 1174static device_method_t ahcich_methods[] = { 1175 DEVMETHOD(device_probe, ahci_ch_probe), 1176 DEVMETHOD(device_attach, ahci_ch_attach), 1177 DEVMETHOD(device_detach, ahci_ch_detach), 1178 DEVMETHOD(device_suspend, ahci_ch_suspend), 1179 DEVMETHOD(device_resume, ahci_ch_resume), 1180 { 0, 0 } 1181}; 1182static driver_t ahcich_driver = { 1183 "ahcich", 1184 ahcich_methods, 1185 sizeof(struct ahci_channel) 1186}; 1187DRIVER_MODULE(ahcich, ahci, ahcich_driver, ahcich_devclass, 0, 0); 1188 1189struct ahci_dc_cb_args { 1190 bus_addr_t maddr; 1191 int error; 1192}; 1193 1194static void 1195ahci_dmainit(device_t dev) 1196{ 1197 struct ahci_channel *ch = device_get_softc(dev); 1198 struct ahci_dc_cb_args dcba; 1199 size_t rfsize; 1200 1201 if (ch->caps & AHCI_CAP_64BIT) 1202 ch->dma.max_address = BUS_SPACE_MAXADDR; 1203 else 1204 ch->dma.max_address = BUS_SPACE_MAXADDR_32BIT; 1205 /* Command area. */ 1206 if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0, 1207 ch->dma.max_address, BUS_SPACE_MAXADDR, 1208 NULL, NULL, AHCI_WORK_SIZE, 1, AHCI_WORK_SIZE, 1209 0, NULL, NULL, &ch->dma.work_tag)) 1210 goto error; 1211 if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 1212 BUS_DMA_ZERO, &ch->dma.work_map)) 1213 goto error; 1214 if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work, 1215 AHCI_WORK_SIZE, ahci_dmasetupc_cb, &dcba, 0) || dcba.error) { 1216 bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map); 1217 goto error; 1218 } 1219 ch->dma.work_bus = dcba.maddr; 1220 /* FIS receive area. */ 1221 if (ch->chcaps & AHCI_P_CMD_FBSCP) 1222 rfsize = 4096; 1223 else 1224 rfsize = 256; 1225 if (bus_dma_tag_create(bus_get_dma_tag(dev), rfsize, 0, 1226 ch->dma.max_address, BUS_SPACE_MAXADDR, 1227 NULL, NULL, rfsize, 1, rfsize, 1228 0, NULL, NULL, &ch->dma.rfis_tag)) 1229 goto error; 1230 if (bus_dmamem_alloc(ch->dma.rfis_tag, (void **)&ch->dma.rfis, 0, 1231 &ch->dma.rfis_map)) 1232 goto error; 1233 if (bus_dmamap_load(ch->dma.rfis_tag, ch->dma.rfis_map, ch->dma.rfis, 1234 rfsize, ahci_dmasetupc_cb, &dcba, 0) || dcba.error) { 1235 bus_dmamem_free(ch->dma.rfis_tag, ch->dma.rfis, ch->dma.rfis_map); 1236 goto error; 1237 } 1238 ch->dma.rfis_bus = dcba.maddr; 1239 /* Data area. */ 1240 if (bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0, 1241 ch->dma.max_address, BUS_SPACE_MAXADDR, 1242 NULL, NULL, 1243 AHCI_SG_ENTRIES * PAGE_SIZE * ch->numslots, 1244 AHCI_SG_ENTRIES, AHCI_PRD_MAX, 1245 0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) { 1246 goto error; 1247 } 1248 return; 1249 1250error: 1251 device_printf(dev, "WARNING - DMA initialization failed\n"); 1252 ahci_dmafini(dev); 1253} 1254 1255static void 1256ahci_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 1257{ 1258 struct ahci_dc_cb_args *dcba = (struct ahci_dc_cb_args *)xsc; 1259 1260 if (!(dcba->error = error)) 1261 dcba->maddr = segs[0].ds_addr; 1262} 1263 1264static void 1265ahci_dmafini(device_t dev) 1266{ 1267 struct ahci_channel *ch = device_get_softc(dev); 1268 1269 if (ch->dma.data_tag) { 1270 bus_dma_tag_destroy(ch->dma.data_tag); 1271 ch->dma.data_tag = NULL; 1272 } 1273 if (ch->dma.rfis_bus) { 1274 bus_dmamap_unload(ch->dma.rfis_tag, ch->dma.rfis_map); 1275 bus_dmamem_free(ch->dma.rfis_tag, ch->dma.rfis, ch->dma.rfis_map); 1276 ch->dma.rfis_bus = 0; 1277 ch->dma.rfis_map = NULL; 1278 ch->dma.rfis = NULL; 1279 } 1280 if (ch->dma.work_bus) { 1281 bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map); 1282 bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map); 1283 ch->dma.work_bus = 0; 1284 ch->dma.work_map = NULL; 1285 ch->dma.work = NULL; 1286 } 1287 if (ch->dma.work_tag) { 1288 bus_dma_tag_destroy(ch->dma.work_tag); 1289 ch->dma.work_tag = NULL; 1290 } 1291} 1292 1293static void 1294ahci_slotsalloc(device_t dev) 1295{ 1296 struct ahci_channel *ch = device_get_softc(dev); 1297 int i; 1298 1299 /* Alloc and setup command/dma slots */ 1300 bzero(ch->slot, sizeof(ch->slot)); 1301 for (i = 0; i < ch->numslots; i++) { 1302 struct ahci_slot *slot = &ch->slot[i]; 1303 1304 slot->dev = dev; 1305 slot->slot = i; 1306 slot->state = AHCI_SLOT_EMPTY; 1307 slot->ccb = NULL; 1308 callout_init_mtx(&slot->timeout, &ch->mtx, 0); 1309 1310 if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map)) 1311 device_printf(ch->dev, "FAILURE - create data_map\n"); 1312 } 1313} 1314 1315static void 1316ahci_slotsfree(device_t dev) 1317{ 1318 struct ahci_channel *ch = device_get_softc(dev); 1319 int i; 1320 1321 /* Free all dma slots */ 1322 for (i = 0; i < ch->numslots; i++) { 1323 struct ahci_slot *slot = &ch->slot[i]; 1324 1325 callout_drain(&slot->timeout); 1326 if (slot->dma.data_map) { 1327 bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map); 1328 slot->dma.data_map = NULL; 1329 } 1330 } 1331} 1332 1333static int 1334ahci_phy_check_events(device_t dev, u_int32_t serr) 1335{ 1336 struct ahci_channel *ch = device_get_softc(dev); 1337 1338 if (((ch->pm_level == 0) && (serr & ATA_SE_PHY_CHANGED)) || 1339 ((ch->pm_level != 0 || ch->listening) && (serr & ATA_SE_EXCHANGED))) { 1340 u_int32_t status = ATA_INL(ch->r_mem, AHCI_P_SSTS); 1341 union ccb *ccb; 1342 1343 if (bootverbose) { 1344 if ((status & ATA_SS_DET_MASK) != ATA_SS_DET_NO_DEVICE) 1345 device_printf(dev, "CONNECT requested\n"); 1346 else 1347 device_printf(dev, "DISCONNECT requested\n"); 1348 } 1349 ahci_reset(dev); 1350 if ((ccb = xpt_alloc_ccb_nowait()) == NULL) 1351 return (0); 1352 if (xpt_create_path(&ccb->ccb_h.path, NULL, 1353 cam_sim_path(ch->sim), 1354 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 1355 xpt_free_ccb(ccb); 1356 return (0); 1357 } 1358 xpt_rescan(ccb); 1359 return (1); 1360 } 1361 return (0); 1362} 1363 1364static void 1365ahci_cpd_check_events(device_t dev) 1366{ 1367 struct ahci_channel *ch = device_get_softc(dev); 1368 u_int32_t status; 1369 union ccb *ccb; 1370 1371 if (ch->pm_level == 0) 1372 return; 1373 1374 status = ATA_INL(ch->r_mem, AHCI_P_CMD); 1375 if ((status & AHCI_P_CMD_CPD) == 0) 1376 return; 1377 1378 if (bootverbose) { 1379 if (status & AHCI_P_CMD_CPS) { 1380 device_printf(dev, "COLD CONNECT requested\n"); 1381 } else 1382 device_printf(dev, "COLD DISCONNECT requested\n"); 1383 } 1384 ahci_reset(dev); 1385 if ((ccb = xpt_alloc_ccb_nowait()) == NULL) 1386 return; 1387 if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(ch->sim), 1388 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 1389 xpt_free_ccb(ccb); 1390 return; 1391 } 1392 xpt_rescan(ccb); 1393} 1394 1395static void 1396ahci_notify_events(device_t dev, u_int32_t status) 1397{ 1398 struct ahci_channel *ch = device_get_softc(dev); 1399 struct cam_path *dpath; 1400 int i; 1401 1402 if (ch->caps & AHCI_CAP_SSNTF) 1403 ATA_OUTL(ch->r_mem, AHCI_P_SNTF, status); 1404 if (bootverbose) 1405 device_printf(dev, "SNTF 0x%04x\n", status); 1406 for (i = 0; i < 16; i++) { 1407 if ((status & (1 << i)) == 0) 1408 continue; 1409 if (xpt_create_path(&dpath, NULL, 1410 xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) { 1411 xpt_async(AC_SCSI_AEN, dpath, NULL); 1412 xpt_free_path(dpath); 1413 } 1414 } 1415} 1416 1417static void 1418ahci_ch_intr_locked(void *data) 1419{ 1420 device_t dev = (device_t)data; 1421 struct ahci_channel *ch = device_get_softc(dev); 1422 1423 mtx_lock(&ch->mtx); 1424 xpt_batch_start(ch->sim); 1425 ahci_ch_intr(data); 1426 xpt_batch_done(ch->sim); 1427 mtx_unlock(&ch->mtx); 1428} 1429 1430static void 1431ahci_ch_pm(void *arg) 1432{ 1433 device_t dev = (device_t)arg; 1434 struct ahci_channel *ch = device_get_softc(dev); 1435 uint32_t work; 1436 1437 if (ch->numrslots != 0) 1438 return; 1439 work = ATA_INL(ch->r_mem, AHCI_P_CMD); 1440 if (ch->pm_level == 4) 1441 work |= AHCI_P_CMD_PARTIAL; 1442 else 1443 work |= AHCI_P_CMD_SLUMBER; 1444 ATA_OUTL(ch->r_mem, AHCI_P_CMD, work); 1445} 1446 1447static void 1448ahci_ch_intr(void *data) 1449{ 1450 device_t dev = (device_t)data; 1451 struct ahci_channel *ch = device_get_softc(dev); 1452 uint32_t istatus, cstatus, serr = 0, sntf = 0, ok, err; 1453 enum ahci_err_type et; 1454 int i, ccs, port, reset = 0; 1455 1456 /* Read and clear interrupt statuses. */ 1457 istatus = ATA_INL(ch->r_mem, AHCI_P_IS); 1458 if (istatus == 0) 1459 return; 1460 ATA_OUTL(ch->r_mem, AHCI_P_IS, istatus); 1461 /* Read command statuses. */ 1462 if (ch->numtslots != 0) 1463 cstatus = ATA_INL(ch->r_mem, AHCI_P_SACT); 1464 else 1465 cstatus = 0; 1466 if (ch->numrslots != ch->numtslots) 1467 cstatus |= ATA_INL(ch->r_mem, AHCI_P_CI); 1468 /* Read SNTF in one of possible ways. */ 1469 if (istatus & AHCI_P_IX_SDB) { 1470 if (ch->caps & AHCI_CAP_SSNTF) 1471 sntf = ATA_INL(ch->r_mem, AHCI_P_SNTF); 1472 else if (ch->fbs_enabled) { 1473 u_int8_t *fis = ch->dma.rfis + 0x58; 1474 1475 for (i = 0; i < 16; i++) { 1476 if (fis[1] & 0x80) { 1477 fis[1] &= 0x7f; 1478 sntf |= 1 << i; 1479 } 1480 fis += 256; 1481 } 1482 } else { 1483 u_int8_t *fis = ch->dma.rfis + 0x58; 1484 1485 if (fis[1] & 0x80) 1486 sntf = (1 << (fis[1] & 0x0f)); 1487 } 1488 } 1489 /* Process PHY events */ 1490 if (istatus & (AHCI_P_IX_PC | AHCI_P_IX_PRC | AHCI_P_IX_OF | 1491 AHCI_P_IX_IF | AHCI_P_IX_HBD | AHCI_P_IX_HBF | AHCI_P_IX_TFE)) { 1492 serr = ATA_INL(ch->r_mem, AHCI_P_SERR); 1493 if (serr) { 1494 ATA_OUTL(ch->r_mem, AHCI_P_SERR, serr); 1495 reset = ahci_phy_check_events(dev, serr); 1496 } 1497 } 1498 /* Process cold presence detection events */ 1499 if ((istatus & AHCI_P_IX_CPD) && !reset) 1500 ahci_cpd_check_events(dev); 1501 /* Process command errors */ 1502 if (istatus & (AHCI_P_IX_OF | AHCI_P_IX_IF | 1503 AHCI_P_IX_HBD | AHCI_P_IX_HBF | AHCI_P_IX_TFE)) { 1504 ccs = (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CCS_MASK) 1505 >> AHCI_P_CMD_CCS_SHIFT; 1506//device_printf(dev, "%s ERROR is %08x cs %08x ss %08x rs %08x tfd %02x serr %08x fbs %08x ccs %d\n", 1507// __func__, istatus, cstatus, sstatus, ch->rslots, ATA_INL(ch->r_mem, AHCI_P_TFD), 1508// serr, ATA_INL(ch->r_mem, AHCI_P_FBS), ccs); 1509 port = -1; 1510 if (ch->fbs_enabled) { 1511 uint32_t fbs = ATA_INL(ch->r_mem, AHCI_P_FBS); 1512 if (fbs & AHCI_P_FBS_SDE) { 1513 port = (fbs & AHCI_P_FBS_DWE) 1514 >> AHCI_P_FBS_DWE_SHIFT; 1515 } else { 1516 for (i = 0; i < 16; i++) { 1517 if (ch->numrslotspd[i] == 0) 1518 continue; 1519 if (port == -1) 1520 port = i; 1521 else if (port != i) { 1522 port = -2; 1523 break; 1524 } 1525 } 1526 } 1527 } 1528 err = ch->rslots & cstatus; 1529 } else { 1530 ccs = 0; 1531 err = 0; 1532 port = -1; 1533 } 1534 /* Complete all successfull commands. */ 1535 ok = ch->rslots & ~cstatus; 1536 for (i = 0; i < ch->numslots; i++) { 1537 if ((ok >> i) & 1) 1538 ahci_end_transaction(&ch->slot[i], AHCI_ERR_NONE); 1539 } 1540 /* On error, complete the rest of commands with error statuses. */ 1541 if (err) { 1542 if (ch->frozen) { 1543 union ccb *fccb = ch->frozen; 1544 ch->frozen = NULL; 1545 fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 1546 if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 1547 xpt_freeze_devq(fccb->ccb_h.path, 1); 1548 fccb->ccb_h.status |= CAM_DEV_QFRZN; 1549 } 1550 xpt_done(fccb); 1551 } 1552 for (i = 0; i < ch->numslots; i++) { 1553 /* XXX: reqests in loading state. */ 1554 if (((err >> i) & 1) == 0) 1555 continue; 1556 if (port >= 0 && 1557 ch->slot[i].ccb->ccb_h.target_id != port) 1558 continue; 1559 if (istatus & AHCI_P_IX_TFE) { 1560 if (port != -2) { 1561 /* Task File Error */ 1562 if (ch->numtslotspd[ 1563 ch->slot[i].ccb->ccb_h.target_id] == 0) { 1564 /* Untagged operation. */ 1565 if (i == ccs) 1566 et = AHCI_ERR_TFE; 1567 else 1568 et = AHCI_ERR_INNOCENT; 1569 } else { 1570 /* Tagged operation. */ 1571 et = AHCI_ERR_NCQ; 1572 } 1573 } else { 1574 et = AHCI_ERR_TFE; 1575 ch->fatalerr = 1; 1576 } 1577 } else if (istatus & AHCI_P_IX_IF) { 1578 if (ch->numtslots == 0 && i != ccs && port != -2) 1579 et = AHCI_ERR_INNOCENT; 1580 else 1581 et = AHCI_ERR_SATA; 1582 } else 1583 et = AHCI_ERR_INVALID; 1584 ahci_end_transaction(&ch->slot[i], et); 1585 } 1586 /* 1587 * We can't reinit port if there are some other 1588 * commands active, use resume to complete them. 1589 */ 1590 if (ch->rslots != 0 && !ch->recoverycmd) 1591 ATA_OUTL(ch->r_mem, AHCI_P_FBS, AHCI_P_FBS_EN | AHCI_P_FBS_DEC); 1592 } 1593 /* Process NOTIFY events */ 1594 if (sntf) 1595 ahci_notify_events(dev, sntf); 1596} 1597 1598/* Must be called with channel locked. */ 1599static int 1600ahci_check_collision(device_t dev, union ccb *ccb) 1601{ 1602 struct ahci_channel *ch = device_get_softc(dev); 1603 int t = ccb->ccb_h.target_id; 1604 1605 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1606 (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 1607 /* Tagged command while we have no supported tag free. */ 1608 if (((~ch->oslots) & (0xffffffff >> (32 - 1609 ch->curr[t].tags))) == 0) 1610 return (1); 1611 /* If we have FBS */ 1612 if (ch->fbs_enabled) { 1613 /* Tagged command while untagged are active. */ 1614 if (ch->numrslotspd[t] != 0 && ch->numtslotspd[t] == 0) 1615 return (1); 1616 } else { 1617 /* Tagged command while untagged are active. */ 1618 if (ch->numrslots != 0 && ch->numtslots == 0) 1619 return (1); 1620 /* Tagged command while tagged to other target is active. */ 1621 if (ch->numtslots != 0 && 1622 ch->taggedtarget != ccb->ccb_h.target_id) 1623 return (1); 1624 } 1625 } else { 1626 /* If we have FBS */ 1627 if (ch->fbs_enabled) { 1628 /* Untagged command while tagged are active. */ 1629 if (ch->numrslotspd[t] != 0 && ch->numtslotspd[t] != 0) 1630 return (1); 1631 } else { 1632 /* Untagged command while tagged are active. */ 1633 if (ch->numrslots != 0 && ch->numtslots != 0) 1634 return (1); 1635 } 1636 } 1637 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1638 (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) { 1639 /* Atomic command while anything active. */ 1640 if (ch->numrslots != 0) 1641 return (1); 1642 } 1643 /* We have some atomic command running. */ 1644 if (ch->aslots != 0) 1645 return (1); 1646 return (0); 1647} 1648 1649/* Must be called with channel locked. */ 1650static void 1651ahci_begin_transaction(device_t dev, union ccb *ccb) 1652{ 1653 struct ahci_channel *ch = device_get_softc(dev); 1654 struct ahci_slot *slot; 1655 int tag, tags; 1656 1657 /* Choose empty slot. */ 1658 tags = ch->numslots; 1659 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1660 (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) 1661 tags = ch->curr[ccb->ccb_h.target_id].tags; 1662 tag = ch->lastslot; 1663 while (1) { 1664 if (tag >= tags) 1665 tag = 0; 1666 if (ch->slot[tag].state == AHCI_SLOT_EMPTY) 1667 break; 1668 tag++; 1669 }; 1670 ch->lastslot = tag; 1671 /* Occupy chosen slot. */ 1672 slot = &ch->slot[tag]; 1673 slot->ccb = ccb; 1674 /* Stop PM timer. */ 1675 if (ch->numrslots == 0 && ch->pm_level > 3) 1676 callout_stop(&ch->pm_timer); 1677 /* Update channel stats. */ 1678 ch->oslots |= (1 << slot->slot); 1679 ch->numrslots++; 1680 ch->numrslotspd[ccb->ccb_h.target_id]++; 1681 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1682 (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 1683 ch->numtslots++; 1684 ch->numtslotspd[ccb->ccb_h.target_id]++; 1685 ch->taggedtarget = ccb->ccb_h.target_id; 1686 } 1687 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1688 (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) 1689 ch->aslots |= (1 << slot->slot); 1690 slot->dma.nsegs = 0; 1691 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 1692 slot->state = AHCI_SLOT_LOADING; 1693 bus_dmamap_load_ccb(ch->dma.data_tag, slot->dma.data_map, ccb, 1694 ahci_dmasetprd, slot, 0); 1695 } else 1696 ahci_execute_transaction(slot); 1697} 1698 1699/* Locked by busdma engine. */ 1700static void 1701ahci_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 1702{ 1703 struct ahci_slot *slot = arg; 1704 struct ahci_channel *ch = device_get_softc(slot->dev); 1705 struct ahci_cmd_tab *ctp; 1706 struct ahci_dma_prd *prd; 1707 int i; 1708 1709 if (error) { 1710 device_printf(slot->dev, "DMA load error\n"); 1711 ahci_end_transaction(slot, AHCI_ERR_INVALID); 1712 return; 1713 } 1714 KASSERT(nsegs <= AHCI_SG_ENTRIES, ("too many DMA segment entries\n")); 1715 /* Get a piece of the workspace for this request */ 1716 ctp = (struct ahci_cmd_tab *) 1717 (ch->dma.work + AHCI_CT_OFFSET + (AHCI_CT_SIZE * slot->slot)); 1718 /* Fill S/G table */ 1719 prd = &ctp->prd_tab[0]; 1720 for (i = 0; i < nsegs; i++) { 1721 prd[i].dba = htole64(segs[i].ds_addr); 1722 prd[i].dbc = htole32((segs[i].ds_len - 1) & AHCI_PRD_MASK); 1723 } 1724 slot->dma.nsegs = nsegs; 1725 bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 1726 ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ? 1727 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE)); 1728 ahci_execute_transaction(slot); 1729} 1730 1731/* Must be called with channel locked. */ 1732static void 1733ahci_execute_transaction(struct ahci_slot *slot) 1734{ 1735 device_t dev = slot->dev; 1736 struct ahci_channel *ch = device_get_softc(dev); 1737 struct ahci_cmd_tab *ctp; 1738 struct ahci_cmd_list *clp; 1739 union ccb *ccb = slot->ccb; 1740 int port = ccb->ccb_h.target_id & 0x0f; 1741 int fis_size, i, softreset; 1742 uint8_t *fis = ch->dma.rfis + 0x40; 1743 uint8_t val; 1744 1745 /* Get a piece of the workspace for this request */ 1746 ctp = (struct ahci_cmd_tab *) 1747 (ch->dma.work + AHCI_CT_OFFSET + (AHCI_CT_SIZE * slot->slot)); 1748 /* Setup the FIS for this request */ 1749 if (!(fis_size = ahci_setup_fis(dev, ctp, ccb, slot->slot))) { 1750 device_printf(ch->dev, "Setting up SATA FIS failed\n"); 1751 ahci_end_transaction(slot, AHCI_ERR_INVALID); 1752 return; 1753 } 1754 /* Setup the command list entry */ 1755 clp = (struct ahci_cmd_list *) 1756 (ch->dma.work + AHCI_CL_OFFSET + (AHCI_CL_SIZE * slot->slot)); 1757 clp->cmd_flags = htole16( 1758 (ccb->ccb_h.flags & CAM_DIR_OUT ? AHCI_CMD_WRITE : 0) | 1759 (ccb->ccb_h.func_code == XPT_SCSI_IO ? 1760 (AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH) : 0) | 1761 (fis_size / sizeof(u_int32_t)) | 1762 (port << 12)); 1763 clp->prd_length = htole16(slot->dma.nsegs); 1764 /* Special handling for Soft Reset command. */ 1765 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1766 (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL)) { 1767 if (ccb->ataio.cmd.control & ATA_A_RESET) { 1768 softreset = 1; 1769 /* Kick controller into sane state */ 1770 ahci_stop(dev); 1771 ahci_clo(dev); 1772 ahci_start(dev, 0); 1773 clp->cmd_flags |= AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY; 1774 } else { 1775 softreset = 2; 1776 /* Prepare FIS receive area for check. */ 1777 for (i = 0; i < 20; i++) 1778 fis[i] = 0xff; 1779 } 1780 } else 1781 softreset = 0; 1782 clp->bytecount = 0; 1783 clp->cmd_table_phys = htole64(ch->dma.work_bus + AHCI_CT_OFFSET + 1784 (AHCI_CT_SIZE * slot->slot)); 1785 bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map, 1786 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1787 bus_dmamap_sync(ch->dma.rfis_tag, ch->dma.rfis_map, 1788 BUS_DMASYNC_PREREAD); 1789 /* Set ACTIVE bit for NCQ commands. */ 1790 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 1791 (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 1792 ATA_OUTL(ch->r_mem, AHCI_P_SACT, 1 << slot->slot); 1793 } 1794 /* If FBS is enabled, set PMP port. */ 1795 if (ch->fbs_enabled) { 1796 ATA_OUTL(ch->r_mem, AHCI_P_FBS, AHCI_P_FBS_EN | 1797 (port << AHCI_P_FBS_DEV_SHIFT)); 1798 } 1799 /* Issue command to the controller. */ 1800 slot->state = AHCI_SLOT_RUNNING; 1801 ch->rslots |= (1 << slot->slot); 1802 ATA_OUTL(ch->r_mem, AHCI_P_CI, (1 << slot->slot)); 1803 /* Device reset commands doesn't interrupt. Poll them. */ 1804 if (ccb->ccb_h.func_code == XPT_ATA_IO && 1805 (ccb->ataio.cmd.command == ATA_DEVICE_RESET || softreset)) { 1806 int count, timeout = ccb->ccb_h.timeout * 100; 1807 enum ahci_err_type et = AHCI_ERR_NONE; 1808 1809 for (count = 0; count < timeout; count++) { 1810 DELAY(10); 1811 if (!(ATA_INL(ch->r_mem, AHCI_P_CI) & (1 << slot->slot))) 1812 break; 1813 if ((ATA_INL(ch->r_mem, AHCI_P_TFD) & ATA_S_ERROR) && 1814 softreset != 1) { 1815#if 0 1816 device_printf(ch->dev, 1817 "Poll error on slot %d, TFD: %04x\n", 1818 slot->slot, ATA_INL(ch->r_mem, AHCI_P_TFD)); 1819#endif 1820 et = AHCI_ERR_TFE; 1821 break; 1822 } 1823 /* Workaround for ATI SB600/SB700 chipsets. */ 1824 if (ccb->ccb_h.target_id == 15 && 1825 pci_get_vendor(device_get_parent(dev)) == 0x1002 && 1826 (ATA_INL(ch->r_mem, AHCI_P_IS) & AHCI_P_IX_IPM)) { 1827 et = AHCI_ERR_TIMEOUT; 1828 break; 1829 } 1830 } 1831 1832 /* Marvell controllers do not wait for readyness. */ 1833 if ((ch->quirks & AHCI_Q_NOBSYRES) && softreset == 2 && 1834 et == AHCI_ERR_NONE) { 1835 while ((val = fis[2]) & ATA_S_BUSY) { 1836 DELAY(10); 1837 if (count++ >= timeout) 1838 break; 1839 } 1840 } 1841 1842 if (timeout && (count >= timeout)) { 1843 device_printf(dev, "Poll timeout on slot %d port %d\n", 1844 slot->slot, port); 1845 device_printf(dev, "is %08x cs %08x ss %08x " 1846 "rs %08x tfd %02x serr %08x cmd %08x\n", 1847 ATA_INL(ch->r_mem, AHCI_P_IS), 1848 ATA_INL(ch->r_mem, AHCI_P_CI), 1849 ATA_INL(ch->r_mem, AHCI_P_SACT), ch->rslots, 1850 ATA_INL(ch->r_mem, AHCI_P_TFD), 1851 ATA_INL(ch->r_mem, AHCI_P_SERR), 1852 ATA_INL(ch->r_mem, AHCI_P_CMD)); 1853 et = AHCI_ERR_TIMEOUT; 1854 } 1855 1856 /* Kick controller into sane state and enable FBS. */ 1857 if (softreset == 2) 1858 ch->eslots |= (1 << slot->slot); 1859 ahci_end_transaction(slot, et); 1860 return; 1861 } 1862 /* Start command execution timeout */ 1863 callout_reset(&slot->timeout, (int)ccb->ccb_h.timeout * hz / 2000, 1864 (timeout_t*)ahci_timeout, slot); 1865 return; 1866} 1867 1868/* Must be called with channel locked. */ 1869static void 1870ahci_process_timeout(device_t dev) 1871{ 1872 struct ahci_channel *ch = device_get_softc(dev); 1873 int i; 1874 1875 mtx_assert(&ch->mtx, MA_OWNED); 1876 /* Handle the rest of commands. */ 1877 for (i = 0; i < ch->numslots; i++) { 1878 /* Do we have a running request on slot? */ 1879 if (ch->slot[i].state < AHCI_SLOT_RUNNING) 1880 continue; 1881 ahci_end_transaction(&ch->slot[i], AHCI_ERR_TIMEOUT); 1882 } 1883} 1884 1885/* Must be called with channel locked. */ 1886static void 1887ahci_rearm_timeout(device_t dev) 1888{ 1889 struct ahci_channel *ch = device_get_softc(dev); 1890 int i; 1891 1892 mtx_assert(&ch->mtx, MA_OWNED); 1893 for (i = 0; i < ch->numslots; i++) { 1894 struct ahci_slot *slot = &ch->slot[i]; 1895 1896 /* Do we have a running request on slot? */ 1897 if (slot->state < AHCI_SLOT_RUNNING) 1898 continue; 1899 if ((ch->toslots & (1 << i)) == 0) 1900 continue; 1901 callout_reset(&slot->timeout, 1902 (int)slot->ccb->ccb_h.timeout * hz / 2000, 1903 (timeout_t*)ahci_timeout, slot); 1904 } 1905} 1906 1907/* Locked by callout mechanism. */ 1908static void 1909ahci_timeout(struct ahci_slot *slot) 1910{ 1911 device_t dev = slot->dev; 1912 struct ahci_channel *ch = device_get_softc(dev); 1913 uint32_t sstatus; 1914 int ccs; 1915 int i; 1916 1917 /* Check for stale timeout. */ 1918 if (slot->state < AHCI_SLOT_RUNNING) 1919 return; 1920 1921 /* Check if slot was not being executed last time we checked. */ 1922 if (slot->state < AHCI_SLOT_EXECUTING) { 1923 /* Check if slot started executing. */ 1924 sstatus = ATA_INL(ch->r_mem, AHCI_P_SACT); 1925 ccs = (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CCS_MASK) 1926 >> AHCI_P_CMD_CCS_SHIFT; 1927 if ((sstatus & (1 << slot->slot)) != 0 || ccs == slot->slot || 1928 ch->fbs_enabled || ch->wrongccs) 1929 slot->state = AHCI_SLOT_EXECUTING; 1930 else if ((ch->rslots & (1 << ccs)) == 0) { 1931 ch->wrongccs = 1; 1932 slot->state = AHCI_SLOT_EXECUTING; 1933 } 1934 1935 callout_reset(&slot->timeout, 1936 (int)slot->ccb->ccb_h.timeout * hz / 2000, 1937 (timeout_t*)ahci_timeout, slot); 1938 return; 1939 } 1940 1941 device_printf(dev, "Timeout on slot %d port %d\n", 1942 slot->slot, slot->ccb->ccb_h.target_id & 0x0f); 1943 device_printf(dev, "is %08x cs %08x ss %08x rs %08x tfd %02x " 1944 "serr %08x cmd %08x\n", 1945 ATA_INL(ch->r_mem, AHCI_P_IS), ATA_INL(ch->r_mem, AHCI_P_CI), 1946 ATA_INL(ch->r_mem, AHCI_P_SACT), ch->rslots, 1947 ATA_INL(ch->r_mem, AHCI_P_TFD), ATA_INL(ch->r_mem, AHCI_P_SERR), 1948 ATA_INL(ch->r_mem, AHCI_P_CMD)); 1949 1950 /* Handle frozen command. */ 1951 if (ch->frozen) { 1952 union ccb *fccb = ch->frozen; 1953 ch->frozen = NULL; 1954 fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 1955 if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 1956 xpt_freeze_devq(fccb->ccb_h.path, 1); 1957 fccb->ccb_h.status |= CAM_DEV_QFRZN; 1958 } 1959 xpt_done(fccb); 1960 } 1961 if (!ch->fbs_enabled && !ch->wrongccs) { 1962 /* Without FBS we know real timeout source. */ 1963 ch->fatalerr = 1; 1964 /* Handle command with timeout. */ 1965 ahci_end_transaction(&ch->slot[slot->slot], AHCI_ERR_TIMEOUT); 1966 /* Handle the rest of commands. */ 1967 for (i = 0; i < ch->numslots; i++) { 1968 /* Do we have a running request on slot? */ 1969 if (ch->slot[i].state < AHCI_SLOT_RUNNING) 1970 continue; 1971 ahci_end_transaction(&ch->slot[i], AHCI_ERR_INNOCENT); 1972 } 1973 } else { 1974 /* With FBS we wait for other commands timeout and pray. */ 1975 if (ch->toslots == 0) 1976 xpt_freeze_simq(ch->sim, 1); 1977 ch->toslots |= (1 << slot->slot); 1978 if ((ch->rslots & ~ch->toslots) == 0) 1979 ahci_process_timeout(dev); 1980 else 1981 device_printf(dev, " ... waiting for slots %08x\n", 1982 ch->rslots & ~ch->toslots); 1983 } 1984} 1985 1986/* Must be called with channel locked. */ 1987static void 1988ahci_end_transaction(struct ahci_slot *slot, enum ahci_err_type et) 1989{ 1990 device_t dev = slot->dev; 1991 struct ahci_channel *ch = device_get_softc(dev); 1992 union ccb *ccb = slot->ccb; 1993 struct ahci_cmd_list *clp; 1994 int lastto; 1995 uint32_t sig; 1996 1997 bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map, 1998 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 1999 clp = (struct ahci_cmd_list *) 2000 (ch->dma.work + AHCI_CL_OFFSET + (AHCI_CL_SIZE * slot->slot)); 2001 /* Read result registers to the result struct 2002 * May be incorrect if several commands finished same time, 2003 * so read only when sure or have to. 2004 */ 2005 if (ccb->ccb_h.func_code == XPT_ATA_IO) { 2006 struct ata_res *res = &ccb->ataio.res; 2007 2008 if ((et == AHCI_ERR_TFE) || 2009 (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) { 2010 u_int8_t *fis = ch->dma.rfis + 0x40; 2011 2012 bus_dmamap_sync(ch->dma.rfis_tag, ch->dma.rfis_map, 2013 BUS_DMASYNC_POSTREAD); 2014 if (ch->fbs_enabled) { 2015 fis += ccb->ccb_h.target_id * 256; 2016 res->status = fis[2]; 2017 res->error = fis[3]; 2018 } else { 2019 uint16_t tfd = ATA_INL(ch->r_mem, AHCI_P_TFD); 2020 2021 res->status = tfd; 2022 res->error = tfd >> 8; 2023 } 2024 res->lba_low = fis[4]; 2025 res->lba_mid = fis[5]; 2026 res->lba_high = fis[6]; 2027 res->device = fis[7]; 2028 res->lba_low_exp = fis[8]; 2029 res->lba_mid_exp = fis[9]; 2030 res->lba_high_exp = fis[10]; 2031 res->sector_count = fis[12]; 2032 res->sector_count_exp = fis[13]; 2033 2034 /* 2035 * Some weird controllers do not return signature in 2036 * FIS receive area. Read it from PxSIG register. 2037 */ 2038 if ((ch->quirks & AHCI_Q_ALTSIG) && 2039 (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) && 2040 (ccb->ataio.cmd.control & ATA_A_RESET) == 0) { 2041 sig = ATA_INL(ch->r_mem, AHCI_P_SIG); 2042 res->lba_high = sig >> 24; 2043 res->lba_mid = sig >> 16; 2044 res->lba_low = sig >> 8; 2045 res->sector_count = sig; 2046 } 2047 } else 2048 bzero(res, sizeof(*res)); 2049 if ((ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) == 0 && 2050 (ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE && 2051 (ch->quirks & AHCI_Q_NOCOUNT) == 0) { 2052 ccb->ataio.resid = 2053 ccb->ataio.dxfer_len - le32toh(clp->bytecount); 2054 } 2055 } else { 2056 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE && 2057 (ch->quirks & AHCI_Q_NOCOUNT) == 0) { 2058 ccb->csio.resid = 2059 ccb->csio.dxfer_len - le32toh(clp->bytecount); 2060 } 2061 } 2062 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 2063 bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map, 2064 (ccb->ccb_h.flags & CAM_DIR_IN) ? 2065 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 2066 bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map); 2067 } 2068 if (et != AHCI_ERR_NONE) 2069 ch->eslots |= (1 << slot->slot); 2070 /* In case of error, freeze device for proper recovery. */ 2071 if ((et != AHCI_ERR_NONE) && (!ch->recoverycmd) && 2072 !(ccb->ccb_h.status & CAM_DEV_QFRZN)) { 2073 xpt_freeze_devq(ccb->ccb_h.path, 1); 2074 ccb->ccb_h.status |= CAM_DEV_QFRZN; 2075 } 2076 /* Set proper result status. */ 2077 ccb->ccb_h.status &= ~CAM_STATUS_MASK; 2078 switch (et) { 2079 case AHCI_ERR_NONE: 2080 ccb->ccb_h.status |= CAM_REQ_CMP; 2081 if (ccb->ccb_h.func_code == XPT_SCSI_IO) 2082 ccb->csio.scsi_status = SCSI_STATUS_OK; 2083 break; 2084 case AHCI_ERR_INVALID: 2085 ch->fatalerr = 1; 2086 ccb->ccb_h.status |= CAM_REQ_INVALID; 2087 break; 2088 case AHCI_ERR_INNOCENT: 2089 ccb->ccb_h.status |= CAM_REQUEUE_REQ; 2090 break; 2091 case AHCI_ERR_TFE: 2092 case AHCI_ERR_NCQ: 2093 if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 2094 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR; 2095 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 2096 } else { 2097 ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR; 2098 } 2099 break; 2100 case AHCI_ERR_SATA: 2101 ch->fatalerr = 1; 2102 if (!ch->recoverycmd) { 2103 xpt_freeze_simq(ch->sim, 1); 2104 ccb->ccb_h.status &= ~CAM_STATUS_MASK; 2105 ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 2106 } 2107 ccb->ccb_h.status |= CAM_UNCOR_PARITY; 2108 break; 2109 case AHCI_ERR_TIMEOUT: 2110 if (!ch->recoverycmd) { 2111 xpt_freeze_simq(ch->sim, 1); 2112 ccb->ccb_h.status &= ~CAM_STATUS_MASK; 2113 ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 2114 } 2115 ccb->ccb_h.status |= CAM_CMD_TIMEOUT; 2116 break; 2117 default: 2118 ch->fatalerr = 1; 2119 ccb->ccb_h.status |= CAM_REQ_CMP_ERR; 2120 } 2121 /* Free slot. */ 2122 ch->oslots &= ~(1 << slot->slot); 2123 ch->rslots &= ~(1 << slot->slot); 2124 ch->aslots &= ~(1 << slot->slot); 2125 slot->state = AHCI_SLOT_EMPTY; 2126 slot->ccb = NULL; 2127 /* Update channel stats. */ 2128 ch->numrslots--; 2129 ch->numrslotspd[ccb->ccb_h.target_id]--; 2130 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 2131 (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) { 2132 ch->numtslots--; 2133 ch->numtslotspd[ccb->ccb_h.target_id]--; 2134 } 2135 /* Cancel timeout state if request completed normally. */ 2136 if (et != AHCI_ERR_TIMEOUT) { 2137 lastto = (ch->toslots == (1 << slot->slot)); 2138 ch->toslots &= ~(1 << slot->slot); 2139 if (lastto) 2140 xpt_release_simq(ch->sim, TRUE); 2141 } 2142 /* If it was first request of reset sequence and there is no error, 2143 * proceed to second request. */ 2144 if ((ccb->ccb_h.func_code == XPT_ATA_IO) && 2145 (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) && 2146 (ccb->ataio.cmd.control & ATA_A_RESET) && 2147 et == AHCI_ERR_NONE) { 2148 ccb->ataio.cmd.control &= ~ATA_A_RESET; 2149 ahci_begin_transaction(dev, ccb); 2150 return; 2151 } 2152 /* If it was our READ LOG command - process it. */ 2153 if (ccb->ccb_h.recovery_type == RECOVERY_READ_LOG) { 2154 ahci_process_read_log(dev, ccb); 2155 /* If it was our REQUEST SENSE command - process it. */ 2156 } else if (ccb->ccb_h.recovery_type == RECOVERY_REQUEST_SENSE) { 2157 ahci_process_request_sense(dev, ccb); 2158 /* If it was NCQ or ATAPI command error, put result on hold. */ 2159 } else if (et == AHCI_ERR_NCQ || 2160 ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR && 2161 (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)) { 2162 ch->hold[slot->slot] = ccb; 2163 ch->numhslots++; 2164 } else 2165 xpt_done(ccb); 2166 /* If we have no other active commands, ... */ 2167 if (ch->rslots == 0) { 2168 /* if there was fatal error - reset port. */ 2169 if (ch->toslots != 0 || ch->fatalerr) { 2170 ahci_reset(dev); 2171 } else { 2172 /* if we have slots in error, we can reinit port. */ 2173 if (ch->eslots != 0) { 2174 ahci_stop(dev); 2175 ahci_clo(dev); 2176 ahci_start(dev, 1); 2177 } 2178 /* if there commands on hold, we can do READ LOG. */ 2179 if (!ch->recoverycmd && ch->numhslots) 2180 ahci_issue_recovery(dev); 2181 } 2182 /* If all the rest of commands are in timeout - give them chance. */ 2183 } else if ((ch->rslots & ~ch->toslots) == 0 && 2184 et != AHCI_ERR_TIMEOUT) 2185 ahci_rearm_timeout(dev); 2186 /* Unfreeze frozen command. */ 2187 if (ch->frozen && !ahci_check_collision(dev, ch->frozen)) { 2188 union ccb *fccb = ch->frozen; 2189 ch->frozen = NULL; 2190 ahci_begin_transaction(dev, fccb); 2191 xpt_release_simq(ch->sim, TRUE); 2192 } 2193 /* Start PM timer. */ 2194 if (ch->numrslots == 0 && ch->pm_level > 3 && 2195 (ch->curr[ch->pm_present ? 15 : 0].caps & CTS_SATA_CAPS_D_PMREQ)) { 2196 callout_schedule(&ch->pm_timer, 2197 (ch->pm_level == 4) ? hz / 1000 : hz / 8); 2198 } 2199} 2200 2201static void 2202ahci_issue_recovery(device_t dev) 2203{ 2204 struct ahci_channel *ch = device_get_softc(dev); 2205 union ccb *ccb; 2206 struct ccb_ataio *ataio; 2207 struct ccb_scsiio *csio; 2208 int i; 2209 2210 /* Find some held command. */ 2211 for (i = 0; i < ch->numslots; i++) { 2212 if (ch->hold[i]) 2213 break; 2214 } 2215 ccb = xpt_alloc_ccb_nowait(); 2216 if (ccb == NULL) { 2217 device_printf(dev, "Unable to allocate recovery command\n"); 2218completeall: 2219 /* We can't do anything -- complete held commands. */ 2220 for (i = 0; i < ch->numslots; i++) { 2221 if (ch->hold[i] == NULL) 2222 continue; 2223 ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 2224 ch->hold[i]->ccb_h.status |= CAM_RESRC_UNAVAIL; 2225 xpt_done(ch->hold[i]); 2226 ch->hold[i] = NULL; 2227 ch->numhslots--; 2228 } 2229 ahci_reset(dev); 2230 return; 2231 } 2232 ccb->ccb_h = ch->hold[i]->ccb_h; /* Reuse old header. */ 2233 if (ccb->ccb_h.func_code == XPT_ATA_IO) { 2234 /* READ LOG */ 2235 ccb->ccb_h.recovery_type = RECOVERY_READ_LOG; 2236 ccb->ccb_h.func_code = XPT_ATA_IO; 2237 ccb->ccb_h.flags = CAM_DIR_IN; 2238 ccb->ccb_h.timeout = 1000; /* 1s should be enough. */ 2239 ataio = &ccb->ataio; 2240 ataio->data_ptr = malloc(512, M_AHCI, M_NOWAIT); 2241 if (ataio->data_ptr == NULL) { 2242 xpt_free_ccb(ccb); 2243 device_printf(dev, 2244 "Unable to allocate memory for READ LOG command\n"); 2245 goto completeall; 2246 } 2247 ataio->dxfer_len = 512; 2248 bzero(&ataio->cmd, sizeof(ataio->cmd)); 2249 ataio->cmd.flags = CAM_ATAIO_48BIT; 2250 ataio->cmd.command = 0x2F; /* READ LOG EXT */ 2251 ataio->cmd.sector_count = 1; 2252 ataio->cmd.sector_count_exp = 0; 2253 ataio->cmd.lba_low = 0x10; 2254 ataio->cmd.lba_mid = 0; 2255 ataio->cmd.lba_mid_exp = 0; 2256 } else { 2257 /* REQUEST SENSE */ 2258 ccb->ccb_h.recovery_type = RECOVERY_REQUEST_SENSE; 2259 ccb->ccb_h.recovery_slot = i; 2260 ccb->ccb_h.func_code = XPT_SCSI_IO; 2261 ccb->ccb_h.flags = CAM_DIR_IN; 2262 ccb->ccb_h.status = 0; 2263 ccb->ccb_h.timeout = 1000; /* 1s should be enough. */ 2264 csio = &ccb->csio; 2265 csio->data_ptr = (void *)&ch->hold[i]->csio.sense_data; 2266 csio->dxfer_len = ch->hold[i]->csio.sense_len; 2267 csio->cdb_len = 6; 2268 bzero(&csio->cdb_io, sizeof(csio->cdb_io)); 2269 csio->cdb_io.cdb_bytes[0] = 0x03; 2270 csio->cdb_io.cdb_bytes[4] = csio->dxfer_len; 2271 } 2272 /* Freeze SIM while doing recovery. */ 2273 ch->recoverycmd = 1; 2274 xpt_freeze_simq(ch->sim, 1); 2275 ahci_begin_transaction(dev, ccb); 2276} 2277 2278static void 2279ahci_process_read_log(device_t dev, union ccb *ccb) 2280{ 2281 struct ahci_channel *ch = device_get_softc(dev); 2282 uint8_t *data; 2283 struct ata_res *res; 2284 int i; 2285 2286 ch->recoverycmd = 0; 2287 2288 data = ccb->ataio.data_ptr; 2289 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && 2290 (data[0] & 0x80) == 0) { 2291 for (i = 0; i < ch->numslots; i++) { 2292 if (!ch->hold[i]) 2293 continue; 2294 if (ch->hold[i]->ccb_h.func_code != XPT_ATA_IO) 2295 continue; 2296 if ((data[0] & 0x1F) == i) { 2297 res = &ch->hold[i]->ataio.res; 2298 res->status = data[2]; 2299 res->error = data[3]; 2300 res->lba_low = data[4]; 2301 res->lba_mid = data[5]; 2302 res->lba_high = data[6]; 2303 res->device = data[7]; 2304 res->lba_low_exp = data[8]; 2305 res->lba_mid_exp = data[9]; 2306 res->lba_high_exp = data[10]; 2307 res->sector_count = data[12]; 2308 res->sector_count_exp = data[13]; 2309 } else { 2310 ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 2311 ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ; 2312 } 2313 xpt_done(ch->hold[i]); 2314 ch->hold[i] = NULL; 2315 ch->numhslots--; 2316 } 2317 } else { 2318 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 2319 device_printf(dev, "Error while READ LOG EXT\n"); 2320 else if ((data[0] & 0x80) == 0) { 2321 device_printf(dev, "Non-queued command error in READ LOG EXT\n"); 2322 } 2323 for (i = 0; i < ch->numslots; i++) { 2324 if (!ch->hold[i]) 2325 continue; 2326 if (ch->hold[i]->ccb_h.func_code != XPT_ATA_IO) 2327 continue; 2328 xpt_done(ch->hold[i]); 2329 ch->hold[i] = NULL; 2330 ch->numhslots--; 2331 } 2332 } 2333 free(ccb->ataio.data_ptr, M_AHCI); 2334 xpt_free_ccb(ccb); 2335 xpt_release_simq(ch->sim, TRUE); 2336} 2337 2338static void 2339ahci_process_request_sense(device_t dev, union ccb *ccb) 2340{ 2341 struct ahci_channel *ch = device_get_softc(dev); 2342 int i; 2343 2344 ch->recoverycmd = 0; 2345 2346 i = ccb->ccb_h.recovery_slot; 2347 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 2348 ch->hold[i]->ccb_h.status |= CAM_AUTOSNS_VALID; 2349 } else { 2350 ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK; 2351 ch->hold[i]->ccb_h.status |= CAM_AUTOSENSE_FAIL; 2352 } 2353 xpt_done(ch->hold[i]); 2354 ch->hold[i] = NULL; 2355 ch->numhslots--; 2356 xpt_free_ccb(ccb); 2357 xpt_release_simq(ch->sim, TRUE); 2358} 2359 2360static void 2361ahci_start(device_t dev, int fbs) 2362{ 2363 struct ahci_channel *ch = device_get_softc(dev); 2364 u_int32_t cmd; 2365 2366 /* Clear SATA error register */ 2367 ATA_OUTL(ch->r_mem, AHCI_P_SERR, 0xFFFFFFFF); 2368 /* Clear any interrupts pending on this channel */ 2369 ATA_OUTL(ch->r_mem, AHCI_P_IS, 0xFFFFFFFF); 2370 /* Configure FIS-based switching if supported. */ 2371 if (ch->chcaps & AHCI_P_CMD_FBSCP) { 2372 ch->fbs_enabled = (fbs && ch->pm_present) ? 1 : 0; 2373 ATA_OUTL(ch->r_mem, AHCI_P_FBS, 2374 ch->fbs_enabled ? AHCI_P_FBS_EN : 0); 2375 } 2376 /* Start operations on this channel */ 2377 cmd = ATA_INL(ch->r_mem, AHCI_P_CMD); 2378 cmd &= ~AHCI_P_CMD_PMA; 2379 ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd | AHCI_P_CMD_ST | 2380 (ch->pm_present ? AHCI_P_CMD_PMA : 0)); 2381} 2382 2383static void 2384ahci_stop(device_t dev) 2385{ 2386 struct ahci_channel *ch = device_get_softc(dev); 2387 u_int32_t cmd; 2388 int timeout; 2389 2390 /* Kill all activity on this channel */ 2391 cmd = ATA_INL(ch->r_mem, AHCI_P_CMD); 2392 ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd & ~AHCI_P_CMD_ST); 2393 /* Wait for activity stop. */ 2394 timeout = 0; 2395 do { 2396 DELAY(10); 2397 if (timeout++ > 50000) { 2398 device_printf(dev, "stopping AHCI engine failed\n"); 2399 break; 2400 } 2401 } while (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CR); 2402 ch->eslots = 0; 2403} 2404 2405static void 2406ahci_clo(device_t dev) 2407{ 2408 struct ahci_channel *ch = device_get_softc(dev); 2409 u_int32_t cmd; 2410 int timeout; 2411 2412 /* Issue Command List Override if supported */ 2413 if (ch->caps & AHCI_CAP_SCLO) { 2414 cmd = ATA_INL(ch->r_mem, AHCI_P_CMD); 2415 cmd |= AHCI_P_CMD_CLO; 2416 ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd); 2417 timeout = 0; 2418 do { 2419 DELAY(10); 2420 if (timeout++ > 50000) { 2421 device_printf(dev, "executing CLO failed\n"); 2422 break; 2423 } 2424 } while (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_CLO); 2425 } 2426} 2427 2428static void 2429ahci_stop_fr(device_t dev) 2430{ 2431 struct ahci_channel *ch = device_get_softc(dev); 2432 u_int32_t cmd; 2433 int timeout; 2434 2435 /* Kill all FIS reception on this channel */ 2436 cmd = ATA_INL(ch->r_mem, AHCI_P_CMD); 2437 ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd & ~AHCI_P_CMD_FRE); 2438 /* Wait for FIS reception stop. */ 2439 timeout = 0; 2440 do { 2441 DELAY(10); 2442 if (timeout++ > 50000) { 2443 device_printf(dev, "stopping AHCI FR engine failed\n"); 2444 break; 2445 } 2446 } while (ATA_INL(ch->r_mem, AHCI_P_CMD) & AHCI_P_CMD_FR); 2447} 2448 2449static void 2450ahci_start_fr(device_t dev) 2451{ 2452 struct ahci_channel *ch = device_get_softc(dev); 2453 u_int32_t cmd; 2454 2455 /* Start FIS reception on this channel */ 2456 cmd = ATA_INL(ch->r_mem, AHCI_P_CMD); 2457 ATA_OUTL(ch->r_mem, AHCI_P_CMD, cmd | AHCI_P_CMD_FRE); 2458} 2459 2460static int 2461ahci_wait_ready(device_t dev, int t, int t0) 2462{ 2463 struct ahci_channel *ch = device_get_softc(dev); 2464 int timeout = 0; 2465 uint32_t val; 2466 2467 while ((val = ATA_INL(ch->r_mem, AHCI_P_TFD)) & 2468 (ATA_S_BUSY | ATA_S_DRQ)) { 2469 if (timeout > t) { 2470 if (t != 0) { 2471 device_printf(dev, 2472 "AHCI reset: device not ready after %dms " 2473 "(tfd = %08x)\n", 2474 MAX(t, 0) + t0, val); 2475 } 2476 return (EBUSY); 2477 } 2478 DELAY(1000); 2479 timeout++; 2480 } 2481 if (bootverbose) 2482 device_printf(dev, "AHCI reset: device ready after %dms\n", 2483 timeout + t0); 2484 return (0); 2485} 2486 2487static void 2488ahci_reset_to(void *arg) 2489{ 2490 device_t dev = arg; 2491 struct ahci_channel *ch = device_get_softc(dev); 2492 2493 if (ch->resetting == 0) 2494 return; 2495 ch->resetting--; 2496 if (ahci_wait_ready(dev, ch->resetting == 0 ? -1 : 0, 2497 (310 - ch->resetting) * 100) == 0) { 2498 ch->resetting = 0; 2499 ahci_start(dev, 1); 2500 xpt_release_simq(ch->sim, TRUE); 2501 return; 2502 } 2503 if (ch->resetting == 0) { 2504 ahci_clo(dev); 2505 ahci_start(dev, 1); 2506 xpt_release_simq(ch->sim, TRUE); 2507 return; 2508 } 2509 callout_schedule(&ch->reset_timer, hz / 10); 2510} 2511 2512static void 2513ahci_reset(device_t dev) 2514{ 2515 struct ahci_channel *ch = device_get_softc(dev); 2516 struct ahci_controller *ctlr = device_get_softc(device_get_parent(dev)); 2517 int i; 2518 2519 xpt_freeze_simq(ch->sim, 1); 2520 if (bootverbose) 2521 device_printf(dev, "AHCI reset...\n"); 2522 /* Forget about previous reset. */ 2523 if (ch->resetting) { 2524 ch->resetting = 0; 2525 callout_stop(&ch->reset_timer); 2526 xpt_release_simq(ch->sim, TRUE); 2527 } 2528 /* Requeue freezed command. */ 2529 if (ch->frozen) { 2530 union ccb *fccb = ch->frozen; 2531 ch->frozen = NULL; 2532 fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ; 2533 if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) { 2534 xpt_freeze_devq(fccb->ccb_h.path, 1); 2535 fccb->ccb_h.status |= CAM_DEV_QFRZN; 2536 } 2537 xpt_done(fccb); 2538 } 2539 /* Kill the engine and requeue all running commands. */ 2540 ahci_stop(dev); 2541 for (i = 0; i < ch->numslots; i++) { 2542 /* Do we have a running request on slot? */ 2543 if (ch->slot[i].state < AHCI_SLOT_RUNNING) 2544 continue; 2545 /* XXX; Commands in loading state. */ 2546 ahci_end_transaction(&ch->slot[i], AHCI_ERR_INNOCENT); 2547 } 2548 for (i = 0; i < ch->numslots; i++) { 2549 if (!ch->hold[i]) 2550 continue; 2551 xpt_done(ch->hold[i]); 2552 ch->hold[i] = NULL; 2553 ch->numhslots--; 2554 } 2555 if (ch->toslots != 0) 2556 xpt_release_simq(ch->sim, TRUE); 2557 ch->eslots = 0; 2558 ch->toslots = 0; 2559 ch->wrongccs = 0; 2560 ch->fatalerr = 0; 2561 /* Tell the XPT about the event */ 2562 xpt_async(AC_BUS_RESET, ch->path, NULL); 2563 /* Disable port interrupts */ 2564 ATA_OUTL(ch->r_mem, AHCI_P_IE, 0); 2565 /* Reset and reconnect PHY, */ 2566 if (!ahci_sata_phy_reset(dev)) { 2567 if (bootverbose) 2568 device_printf(dev, 2569 "AHCI reset: device not found\n"); 2570 ch->devices = 0; 2571 /* Enable wanted port interrupts */ 2572 ATA_OUTL(ch->r_mem, AHCI_P_IE, 2573 (((ch->pm_level != 0) ? AHCI_P_IX_CPD | AHCI_P_IX_MP : 0) | 2574 AHCI_P_IX_PRC | AHCI_P_IX_PC)); 2575 xpt_release_simq(ch->sim, TRUE); 2576 return; 2577 } 2578 if (bootverbose) 2579 device_printf(dev, "AHCI reset: device found\n"); 2580 /* Wait for clearing busy status. */ 2581 if (ahci_wait_ready(dev, dumping ? 31000 : 0, 0)) { 2582 if (dumping) 2583 ahci_clo(dev); 2584 else 2585 ch->resetting = 310; 2586 } 2587 ch->devices = 1; 2588 /* Enable wanted port interrupts */ 2589 ATA_OUTL(ch->r_mem, AHCI_P_IE, 2590 (((ch->pm_level != 0) ? AHCI_P_IX_CPD | AHCI_P_IX_MP : 0) | 2591 AHCI_P_IX_TFE | AHCI_P_IX_HBF | 2592 AHCI_P_IX_HBD | AHCI_P_IX_IF | AHCI_P_IX_OF | 2593 ((ch->pm_level == 0) ? AHCI_P_IX_PRC : 0) | AHCI_P_IX_PC | 2594 AHCI_P_IX_DP | AHCI_P_IX_UF | (ctlr->ccc ? 0 : AHCI_P_IX_SDB) | 2595 AHCI_P_IX_DS | AHCI_P_IX_PS | (ctlr->ccc ? 0 : AHCI_P_IX_DHR))); 2596 if (ch->resetting) 2597 callout_reset(&ch->reset_timer, hz / 10, ahci_reset_to, dev); 2598 else { 2599 ahci_start(dev, 1); 2600 xpt_release_simq(ch->sim, TRUE); 2601 } 2602} 2603 2604static int 2605ahci_setup_fis(device_t dev, struct ahci_cmd_tab *ctp, union ccb *ccb, int tag) 2606{ 2607 struct ahci_channel *ch = device_get_softc(dev); 2608 u_int8_t *fis = &ctp->cfis[0]; 2609 2610 bzero(ctp->cfis, 16); 2611 fis[0] = 0x27; /* host to device */ 2612 fis[1] = (ccb->ccb_h.target_id & 0x0f); 2613 if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 2614 fis[1] |= 0x80; 2615 fis[2] = ATA_PACKET_CMD; 2616 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE && 2617 ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA) 2618 fis[3] = ATA_F_DMA; 2619 else { 2620 fis[5] = ccb->csio.dxfer_len; 2621 fis[6] = ccb->csio.dxfer_len >> 8; 2622 } 2623 fis[7] = ATA_D_LBA; 2624 fis[15] = ATA_A_4BIT; 2625 bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ? 2626 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes, 2627 ctp->acmd, ccb->csio.cdb_len); 2628 bzero(ctp->acmd + ccb->csio.cdb_len, 32 - ccb->csio.cdb_len); 2629 } else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) { 2630 fis[1] |= 0x80; 2631 fis[2] = ccb->ataio.cmd.command; 2632 fis[3] = ccb->ataio.cmd.features; 2633 fis[4] = ccb->ataio.cmd.lba_low; 2634 fis[5] = ccb->ataio.cmd.lba_mid; 2635 fis[6] = ccb->ataio.cmd.lba_high; 2636 fis[7] = ccb->ataio.cmd.device; 2637 fis[8] = ccb->ataio.cmd.lba_low_exp; 2638 fis[9] = ccb->ataio.cmd.lba_mid_exp; 2639 fis[10] = ccb->ataio.cmd.lba_high_exp; 2640 fis[11] = ccb->ataio.cmd.features_exp; 2641 if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) { 2642 fis[12] = tag << 3; 2643 fis[13] = 0; 2644 } else { 2645 fis[12] = ccb->ataio.cmd.sector_count; 2646 fis[13] = ccb->ataio.cmd.sector_count_exp; 2647 } 2648 fis[15] = ATA_A_4BIT; 2649 } else { 2650 fis[15] = ccb->ataio.cmd.control; 2651 } 2652 return (20); 2653} 2654 2655static int 2656ahci_sata_connect(struct ahci_channel *ch) 2657{ 2658 u_int32_t status; 2659 int timeout, found = 0; 2660 2661 /* Wait up to 100ms for "connect well" */ 2662 for (timeout = 0; timeout < 1000 ; timeout++) { 2663 status = ATA_INL(ch->r_mem, AHCI_P_SSTS); 2664 if ((status & ATA_SS_DET_MASK) != ATA_SS_DET_NO_DEVICE) 2665 found = 1; 2666 if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) && 2667 ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) && 2668 ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) 2669 break; 2670 if ((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_OFFLINE) { 2671 if (bootverbose) { 2672 device_printf(ch->dev, "SATA offline status=%08x\n", 2673 status); 2674 } 2675 return (0); 2676 } 2677 if (found == 0 && timeout >= 100) 2678 break; 2679 DELAY(100); 2680 } 2681 if (timeout >= 1000 || !found) { 2682 if (bootverbose) { 2683 device_printf(ch->dev, 2684 "SATA connect timeout time=%dus status=%08x\n", 2685 timeout * 100, status); 2686 } 2687 return (0); 2688 } 2689 if (bootverbose) { 2690 device_printf(ch->dev, "SATA connect time=%dus status=%08x\n", 2691 timeout * 100, status); 2692 } 2693 /* Clear SATA error register */ 2694 ATA_OUTL(ch->r_mem, AHCI_P_SERR, 0xffffffff); 2695 return (1); 2696} 2697 2698static int 2699ahci_sata_phy_reset(device_t dev) 2700{ 2701 struct ahci_channel *ch = device_get_softc(dev); 2702 int sata_rev; 2703 uint32_t val; 2704 2705 if (ch->listening) { 2706 val = ATA_INL(ch->r_mem, AHCI_P_CMD); 2707 val |= AHCI_P_CMD_SUD; 2708 ATA_OUTL(ch->r_mem, AHCI_P_CMD, val); 2709 ch->listening = 0; 2710 } 2711 sata_rev = ch->user[ch->pm_present ? 15 : 0].revision; 2712 if (sata_rev == 1) 2713 val = ATA_SC_SPD_SPEED_GEN1; 2714 else if (sata_rev == 2) 2715 val = ATA_SC_SPD_SPEED_GEN2; 2716 else if (sata_rev == 3) 2717 val = ATA_SC_SPD_SPEED_GEN3; 2718 else 2719 val = 0; 2720 ATA_OUTL(ch->r_mem, AHCI_P_SCTL, 2721 ATA_SC_DET_RESET | val | 2722 ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER); 2723 DELAY(1000); 2724 ATA_OUTL(ch->r_mem, AHCI_P_SCTL, 2725 ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 : 2726 (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER))); 2727 if (!ahci_sata_connect(ch)) { 2728 if (ch->caps & AHCI_CAP_SSS) { 2729 val = ATA_INL(ch->r_mem, AHCI_P_CMD); 2730 val &= ~AHCI_P_CMD_SUD; 2731 ATA_OUTL(ch->r_mem, AHCI_P_CMD, val); 2732 ch->listening = 1; 2733 } else if (ch->pm_level > 0) 2734 ATA_OUTL(ch->r_mem, AHCI_P_SCTL, ATA_SC_DET_DISABLE); 2735 return (0); 2736 } 2737 return (1); 2738} 2739 2740static int 2741ahci_check_ids(device_t dev, union ccb *ccb) 2742{ 2743 struct ahci_channel *ch = device_get_softc(dev); 2744 2745 if (ccb->ccb_h.target_id > ((ch->caps & AHCI_CAP_SPM) ? 15 : 0)) { 2746 ccb->ccb_h.status = CAM_TID_INVALID; 2747 xpt_done(ccb); 2748 return (-1); 2749 } 2750 if (ccb->ccb_h.target_lun != 0) { 2751 ccb->ccb_h.status = CAM_LUN_INVALID; 2752 xpt_done(ccb); 2753 return (-1); 2754 } 2755 return (0); 2756} 2757 2758static void 2759ahciaction(struct cam_sim *sim, union ccb *ccb) 2760{ 2761 device_t dev, parent; 2762 struct ahci_channel *ch; 2763 2764 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahciaction func_code=%x\n", 2765 ccb->ccb_h.func_code)); 2766 2767 ch = (struct ahci_channel *)cam_sim_softc(sim); 2768 dev = ch->dev; 2769 switch (ccb->ccb_h.func_code) { 2770 /* Common cases first */ 2771 case XPT_ATA_IO: /* Execute the requested I/O operation */ 2772 case XPT_SCSI_IO: 2773 if (ahci_check_ids(dev, ccb)) 2774 return; 2775 if (ch->devices == 0 || 2776 (ch->pm_present == 0 && 2777 ccb->ccb_h.target_id > 0 && ccb->ccb_h.target_id < 15)) { 2778 ccb->ccb_h.status = CAM_SEL_TIMEOUT; 2779 break; 2780 } 2781 ccb->ccb_h.recovery_type = RECOVERY_NONE; 2782 /* Check for command collision. */ 2783 if (ahci_check_collision(dev, ccb)) { 2784 /* Freeze command. */ 2785 ch->frozen = ccb; 2786 /* We have only one frozen slot, so freeze simq also. */ 2787 xpt_freeze_simq(ch->sim, 1); 2788 return; 2789 } 2790 ahci_begin_transaction(dev, ccb); 2791 return; 2792 case XPT_EN_LUN: /* Enable LUN as a target */ 2793 case XPT_TARGET_IO: /* Execute target I/O request */ 2794 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */ 2795 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/ 2796 case XPT_ABORT: /* Abort the specified CCB */ 2797 /* XXX Implement */ 2798 ccb->ccb_h.status = CAM_REQ_INVALID; 2799 break; 2800 case XPT_SET_TRAN_SETTINGS: 2801 { 2802 struct ccb_trans_settings *cts = &ccb->cts; 2803 struct ahci_device *d; 2804 2805 if (ahci_check_ids(dev, ccb)) 2806 return; 2807 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 2808 d = &ch->curr[ccb->ccb_h.target_id]; 2809 else 2810 d = &ch->user[ccb->ccb_h.target_id]; 2811 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION) 2812 d->revision = cts->xport_specific.sata.revision; 2813 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE) 2814 d->mode = cts->xport_specific.sata.mode; 2815 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT) 2816 d->bytecount = min(8192, cts->xport_specific.sata.bytecount); 2817 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS) 2818 d->tags = min(ch->numslots, cts->xport_specific.sata.tags); 2819 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) 2820 ch->pm_present = cts->xport_specific.sata.pm_present; 2821 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_ATAPI) 2822 d->atapi = cts->xport_specific.sata.atapi; 2823 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_CAPS) 2824 d->caps = cts->xport_specific.sata.caps; 2825 ccb->ccb_h.status = CAM_REQ_CMP; 2826 break; 2827 } 2828 case XPT_GET_TRAN_SETTINGS: 2829 /* Get default/user set transfer settings for the target */ 2830 { 2831 struct ccb_trans_settings *cts = &ccb->cts; 2832 struct ahci_device *d; 2833 uint32_t status; 2834 2835 if (ahci_check_ids(dev, ccb)) 2836 return; 2837 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 2838 d = &ch->curr[ccb->ccb_h.target_id]; 2839 else 2840 d = &ch->user[ccb->ccb_h.target_id]; 2841 cts->protocol = PROTO_UNSPECIFIED; 2842 cts->protocol_version = PROTO_VERSION_UNSPECIFIED; 2843 cts->transport = XPORT_SATA; 2844 cts->transport_version = XPORT_VERSION_UNSPECIFIED; 2845 cts->proto_specific.valid = 0; 2846 cts->xport_specific.sata.valid = 0; 2847 if (cts->type == CTS_TYPE_CURRENT_SETTINGS && 2848 (ccb->ccb_h.target_id == 15 || 2849 (ccb->ccb_h.target_id == 0 && !ch->pm_present))) { 2850 status = ATA_INL(ch->r_mem, AHCI_P_SSTS) & ATA_SS_SPD_MASK; 2851 if (status & 0x0f0) { 2852 cts->xport_specific.sata.revision = 2853 (status & 0x0f0) >> 4; 2854 cts->xport_specific.sata.valid |= 2855 CTS_SATA_VALID_REVISION; 2856 } 2857 cts->xport_specific.sata.caps = d->caps & CTS_SATA_CAPS_D; 2858 if (ch->pm_level) { 2859 if (ch->caps & (AHCI_CAP_PSC | AHCI_CAP_SSC)) 2860 cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_PMREQ; 2861 if (ch->caps2 & AHCI_CAP2_APST) 2862 cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_APST; 2863 } 2864 if ((ch->caps & AHCI_CAP_SNCQ) && 2865 (ch->quirks & AHCI_Q_NOAA) == 0) 2866 cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_DMAAA; 2867 cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_AN; 2868 cts->xport_specific.sata.caps &= 2869 ch->user[ccb->ccb_h.target_id].caps; 2870 cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS; 2871 } else { 2872 cts->xport_specific.sata.revision = d->revision; 2873 cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION; 2874 cts->xport_specific.sata.caps = d->caps; 2875 cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS; 2876 } 2877 cts->xport_specific.sata.mode = d->mode; 2878 cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE; 2879 cts->xport_specific.sata.bytecount = d->bytecount; 2880 cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT; 2881 cts->xport_specific.sata.pm_present = ch->pm_present; 2882 cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM; 2883 cts->xport_specific.sata.tags = d->tags; 2884 cts->xport_specific.sata.valid |= CTS_SATA_VALID_TAGS; 2885 cts->xport_specific.sata.atapi = d->atapi; 2886 cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI; 2887 ccb->ccb_h.status = CAM_REQ_CMP; 2888 break; 2889 } 2890 case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 2891 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */ 2892 ahci_reset(dev); 2893 ccb->ccb_h.status = CAM_REQ_CMP; 2894 break; 2895 case XPT_TERM_IO: /* Terminate the I/O process */ 2896 /* XXX Implement */ 2897 ccb->ccb_h.status = CAM_REQ_INVALID; 2898 break; 2899 case XPT_PATH_INQ: /* Path routing inquiry */ 2900 { 2901 struct ccb_pathinq *cpi = &ccb->cpi; 2902 2903 parent = device_get_parent(dev); 2904 cpi->version_num = 1; /* XXX??? */ 2905 cpi->hba_inquiry = PI_SDTR_ABLE; 2906 if (ch->caps & AHCI_CAP_SNCQ) 2907 cpi->hba_inquiry |= PI_TAG_ABLE; 2908 if (ch->caps & AHCI_CAP_SPM) 2909 cpi->hba_inquiry |= PI_SATAPM; 2910 cpi->target_sprt = 0; 2911 cpi->hba_misc = PIM_SEQSCAN | PIM_UNMAPPED; 2912 cpi->hba_eng_cnt = 0; 2913 if (ch->caps & AHCI_CAP_SPM) 2914 cpi->max_target = 15; 2915 else 2916 cpi->max_target = 0; 2917 cpi->max_lun = 0; 2918 cpi->initiator_id = 0; 2919 cpi->bus_id = cam_sim_bus(sim); 2920 cpi->base_transfer_speed = 150000; 2921 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2922 strncpy(cpi->hba_vid, "AHCI", HBA_IDLEN); 2923 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 2924 cpi->unit_number = cam_sim_unit(sim); 2925 cpi->transport = XPORT_SATA; 2926 cpi->transport_version = XPORT_VERSION_UNSPECIFIED; 2927 cpi->protocol = PROTO_ATA; 2928 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED; 2929 cpi->maxio = MAXPHYS; 2930 /* ATI SB600 can't handle 256 sectors with FPDMA (NCQ). */ 2931 if (pci_get_devid(parent) == 0x43801002) 2932 cpi->maxio = min(cpi->maxio, 128 * 512); 2933 cpi->hba_vendor = pci_get_vendor(parent); 2934 cpi->hba_device = pci_get_device(parent); 2935 cpi->hba_subvendor = pci_get_subvendor(parent); 2936 cpi->hba_subdevice = pci_get_subdevice(parent); 2937 cpi->ccb_h.status = CAM_REQ_CMP; 2938 break; 2939 } 2940 default: 2941 ccb->ccb_h.status = CAM_REQ_INVALID; 2942 break; 2943 } 2944 xpt_done(ccb); 2945} 2946 2947static void 2948ahcipoll(struct cam_sim *sim) 2949{ 2950 struct ahci_channel *ch = (struct ahci_channel *)cam_sim_softc(sim); 2951 2952 ahci_ch_intr(ch->dev); 2953 if (ch->resetting != 0 && 2954 (--ch->resetpolldiv <= 0 || !callout_pending(&ch->reset_timer))) { 2955 ch->resetpolldiv = 1000; 2956 ahci_reset_to(ch->dev); 2957 } 2958} 2959