xref: /illumos-gate/usr/src/cmd/bhyve/pci_ahci.c (revision 32640292339b07090f10ce34d455f98711077343)
1bf21cd93STycho Nightingale /*-
2*32640292SAndy Fiddaman  * SPDX-License-Identifier: BSD-2-Clause
34c87aefeSPatrick Mooney  *
4bf21cd93STycho Nightingale  * Copyright (c) 2013  Zhixiang Yu <zcore@freebsd.org>
54c87aefeSPatrick Mooney  * Copyright (c) 2015-2016 Alexander Motin <mav@FreeBSD.org>
6bf21cd93STycho Nightingale  * All rights reserved.
7bf21cd93STycho Nightingale  *
8bf21cd93STycho Nightingale  * Redistribution and use in source and binary forms, with or without
9bf21cd93STycho Nightingale  * modification, are permitted provided that the following conditions
10bf21cd93STycho Nightingale  * are met:
11bf21cd93STycho Nightingale  * 1. Redistributions of source code must retain the above copyright
12bf21cd93STycho Nightingale  *    notice, this list of conditions and the following disclaimer.
13bf21cd93STycho Nightingale  * 2. Redistributions in binary form must reproduce the above copyright
14bf21cd93STycho Nightingale  *    notice, this list of conditions and the following disclaimer in the
15bf21cd93STycho Nightingale  *    documentation and/or other materials provided with the distribution.
16bf21cd93STycho Nightingale  *
17bf21cd93STycho Nightingale  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
18bf21cd93STycho Nightingale  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19bf21cd93STycho Nightingale  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20bf21cd93STycho Nightingale  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21bf21cd93STycho Nightingale  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22bf21cd93STycho Nightingale  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23bf21cd93STycho Nightingale  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24bf21cd93STycho Nightingale  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25bf21cd93STycho Nightingale  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26bf21cd93STycho Nightingale  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27bf21cd93STycho Nightingale  * SUCH DAMAGE.
28bf21cd93STycho Nightingale  */
29bf21cd93STycho Nightingale 
30bf21cd93STycho Nightingale #include <sys/cdefs.h>
31bf21cd93STycho Nightingale 
32bf21cd93STycho Nightingale #include <sys/param.h>
33bf21cd93STycho Nightingale #include <sys/linker_set.h>
34bf21cd93STycho Nightingale #include <sys/stat.h>
35bf21cd93STycho Nightingale #include <sys/uio.h>
36bf21cd93STycho Nightingale #include <sys/ioctl.h>
37bf21cd93STycho Nightingale #include <sys/disk.h>
38bf21cd93STycho Nightingale #include <sys/ata.h>
39bf21cd93STycho Nightingale #include <sys/endian.h>
40bf21cd93STycho Nightingale 
41bf21cd93STycho Nightingale #include <errno.h>
42bf21cd93STycho Nightingale #include <fcntl.h>
43bf21cd93STycho Nightingale #include <stdio.h>
44bf21cd93STycho Nightingale #include <stdlib.h>
45bf21cd93STycho Nightingale #include <stdint.h>
46bf21cd93STycho Nightingale #include <string.h>
47bf21cd93STycho Nightingale #include <strings.h>
48bf21cd93STycho Nightingale #include <unistd.h>
49bf21cd93STycho Nightingale #include <assert.h>
50bf21cd93STycho Nightingale #include <pthread.h>
51bf21cd93STycho Nightingale #include <pthread_np.h>
52bf21cd93STycho Nightingale #include <inttypes.h>
534c87aefeSPatrick Mooney #include <md5.h>
54bf21cd93STycho Nightingale 
55bf21cd93STycho Nightingale #include "bhyverun.h"
562b948146SAndy Fiddaman #include "config.h"
572b948146SAndy Fiddaman #include "debug.h"
58bf21cd93STycho Nightingale #include "pci_emul.h"
59bf21cd93STycho Nightingale #include "ahci.h"
60bf21cd93STycho Nightingale #include "block_if.h"
61bf21cd93STycho Nightingale 
624c87aefeSPatrick Mooney #define	DEF_PORTS	6	/* Intel ICH8 AHCI supports 6 ports */
634c87aefeSPatrick Mooney #define	MAX_PORTS	32	/* AHCI supports 32 ports */
64bf21cd93STycho Nightingale 
65bf21cd93STycho Nightingale #define	PxSIG_ATA	0x00000101 /* ATA drive */
66bf21cd93STycho Nightingale #define	PxSIG_ATAPI	0xeb140101 /* ATAPI drive */
67bf21cd93STycho Nightingale 
68bf21cd93STycho Nightingale enum sata_fis_type {
69bf21cd93STycho Nightingale 	FIS_TYPE_REGH2D		= 0x27,	/* Register FIS - host to device */
70bf21cd93STycho Nightingale 	FIS_TYPE_REGD2H		= 0x34,	/* Register FIS - device to host */
71bf21cd93STycho Nightingale 	FIS_TYPE_DMAACT		= 0x39,	/* DMA activate FIS - device to host */
72bf21cd93STycho Nightingale 	FIS_TYPE_DMASETUP	= 0x41,	/* DMA setup FIS - bidirectional */
73bf21cd93STycho Nightingale 	FIS_TYPE_DATA		= 0x46,	/* Data FIS - bidirectional */
74bf21cd93STycho Nightingale 	FIS_TYPE_BIST		= 0x58,	/* BIST activate FIS - bidirectional */
75bf21cd93STycho Nightingale 	FIS_TYPE_PIOSETUP	= 0x5F,	/* PIO setup FIS - device to host */
76bf21cd93STycho Nightingale 	FIS_TYPE_SETDEVBITS	= 0xA1,	/* Set dev bits FIS - device to host */
77bf21cd93STycho Nightingale };
78bf21cd93STycho Nightingale 
79bf21cd93STycho Nightingale /*
80bf21cd93STycho Nightingale  * SCSI opcodes
81bf21cd93STycho Nightingale  */
82bf21cd93STycho Nightingale #define	TEST_UNIT_READY		0x00
83bf21cd93STycho Nightingale #define	REQUEST_SENSE		0x03
84bf21cd93STycho Nightingale #define	INQUIRY			0x12
85bf21cd93STycho Nightingale #define	START_STOP_UNIT		0x1B
86bf21cd93STycho Nightingale #define	PREVENT_ALLOW		0x1E
87bf21cd93STycho Nightingale #define	READ_CAPACITY		0x25
88bf21cd93STycho Nightingale #define	READ_10			0x28
89bf21cd93STycho Nightingale #define	POSITION_TO_ELEMENT	0x2B
90bf21cd93STycho Nightingale #define	READ_TOC		0x43
91bf21cd93STycho Nightingale #define	GET_EVENT_STATUS_NOTIFICATION 0x4A
92bf21cd93STycho Nightingale #define	MODE_SENSE_10		0x5A
934c87aefeSPatrick Mooney #define	REPORT_LUNS		0xA0
94bf21cd93STycho Nightingale #define	READ_12			0xA8
95bf21cd93STycho Nightingale #define	READ_CD			0xBE
96bf21cd93STycho Nightingale 
97bf21cd93STycho Nightingale /*
98bf21cd93STycho Nightingale  * SCSI mode page codes
99bf21cd93STycho Nightingale  */
100bf21cd93STycho Nightingale #define	MODEPAGE_RW_ERROR_RECOVERY	0x01
101bf21cd93STycho Nightingale #define	MODEPAGE_CD_CAPABILITIES	0x2A
102bf21cd93STycho Nightingale 
103bf21cd93STycho Nightingale /*
104bf21cd93STycho Nightingale  * ATA commands
105bf21cd93STycho Nightingale  */
106bf21cd93STycho Nightingale #define	ATA_SF_ENAB_SATA_SF		0x10
107bf21cd93STycho Nightingale #define	ATA_SATA_SF_AN			0x05
108bf21cd93STycho Nightingale #define	ATA_SF_DIS_SATA_SF		0x90
109bf21cd93STycho Nightingale 
110bf21cd93STycho Nightingale /*
111bf21cd93STycho Nightingale  * Debug printf
112bf21cd93STycho Nightingale  */
113bf21cd93STycho Nightingale #ifdef AHCI_DEBUG
114bf21cd93STycho Nightingale static FILE *dbg;
115bf21cd93STycho Nightingale #define DPRINTF(format, arg...)	do{fprintf(dbg, format, ##arg);fflush(dbg);}while(0)
116bf21cd93STycho Nightingale #else
117bf21cd93STycho Nightingale #define DPRINTF(format, arg...)
118bf21cd93STycho Nightingale #endif
119bf21cd93STycho Nightingale #define WPRINTF(format, arg...) printf(format, ##arg)
120bf21cd93STycho Nightingale 
1214c87aefeSPatrick Mooney #define AHCI_PORT_IDENT 20 + 1
1224c87aefeSPatrick Mooney 
123bf21cd93STycho Nightingale struct ahci_ioreq {
124bf21cd93STycho Nightingale 	struct blockif_req io_req;
125bf21cd93STycho Nightingale 	struct ahci_port *io_pr;
126bf21cd93STycho Nightingale 	STAILQ_ENTRY(ahci_ioreq) io_flist;
127bf21cd93STycho Nightingale 	TAILQ_ENTRY(ahci_ioreq) io_blist;
128bf21cd93STycho Nightingale 	uint8_t *cfis;
129bf21cd93STycho Nightingale 	uint32_t len;
130bf21cd93STycho Nightingale 	uint32_t done;
131bf21cd93STycho Nightingale 	int slot;
1324c87aefeSPatrick Mooney 	int more;
133bf21cd93STycho Nightingale };
134bf21cd93STycho Nightingale 
135bf21cd93STycho Nightingale struct ahci_port {
136bf21cd93STycho Nightingale 	struct blockif_ctxt *bctx;
137bf21cd93STycho Nightingale 	struct pci_ahci_softc *pr_sc;
1386960cd89SAndy Fiddaman 	struct ata_params ata_ident;
139bf21cd93STycho Nightingale 	uint8_t *cmd_lst;
140bf21cd93STycho Nightingale 	uint8_t *rfis;
1414c87aefeSPatrick Mooney 	int port;
142bf21cd93STycho Nightingale 	int atapi;
143bf21cd93STycho Nightingale 	int reset;
1444c87aefeSPatrick Mooney 	int waitforclear;
145bf21cd93STycho Nightingale 	int mult_sectors;
146bf21cd93STycho Nightingale 	uint8_t xfermode;
1474c87aefeSPatrick Mooney 	uint8_t err_cfis[20];
148bf21cd93STycho Nightingale 	uint8_t sense_key;
149bf21cd93STycho Nightingale 	uint8_t asc;
1504c87aefeSPatrick Mooney 	u_int ccs;
151bf21cd93STycho Nightingale 	uint32_t pending;
152bf21cd93STycho Nightingale 
153bf21cd93STycho Nightingale 	uint32_t clb;
154bf21cd93STycho Nightingale 	uint32_t clbu;
155bf21cd93STycho Nightingale 	uint32_t fb;
156bf21cd93STycho Nightingale 	uint32_t fbu;
157bf21cd93STycho Nightingale 	uint32_t is;
158bf21cd93STycho Nightingale 	uint32_t ie;
159bf21cd93STycho Nightingale 	uint32_t cmd;
160bf21cd93STycho Nightingale 	uint32_t unused0;
161bf21cd93STycho Nightingale 	uint32_t tfd;
162bf21cd93STycho Nightingale 	uint32_t sig;
163bf21cd93STycho Nightingale 	uint32_t ssts;
164bf21cd93STycho Nightingale 	uint32_t sctl;
165bf21cd93STycho Nightingale 	uint32_t serr;
166bf21cd93STycho Nightingale 	uint32_t sact;
167bf21cd93STycho Nightingale 	uint32_t ci;
168bf21cd93STycho Nightingale 	uint32_t sntf;
169bf21cd93STycho Nightingale 	uint32_t fbs;
170bf21cd93STycho Nightingale 
171bf21cd93STycho Nightingale 	/*
172bf21cd93STycho Nightingale 	 * i/o request info
173bf21cd93STycho Nightingale 	 */
174bf21cd93STycho Nightingale 	struct ahci_ioreq *ioreq;
175bf21cd93STycho Nightingale 	int ioqsz;
176bf21cd93STycho Nightingale 	STAILQ_HEAD(ahci_fhead, ahci_ioreq) iofhd;
177bf21cd93STycho Nightingale 	TAILQ_HEAD(ahci_bhead, ahci_ioreq) iobhd;
178bf21cd93STycho Nightingale };
179bf21cd93STycho Nightingale 
180bf21cd93STycho Nightingale struct ahci_cmd_hdr {
181bf21cd93STycho Nightingale 	uint16_t flags;
182bf21cd93STycho Nightingale 	uint16_t prdtl;
183bf21cd93STycho Nightingale 	uint32_t prdbc;
184bf21cd93STycho Nightingale 	uint64_t ctba;
185bf21cd93STycho Nightingale 	uint32_t reserved[4];
186bf21cd93STycho Nightingale };
187bf21cd93STycho Nightingale 
188bf21cd93STycho Nightingale struct ahci_prdt_entry {
189bf21cd93STycho Nightingale 	uint64_t dba;
190bf21cd93STycho Nightingale 	uint32_t reserved;
191bf21cd93STycho Nightingale #define	DBCMASK		0x3fffff
192bf21cd93STycho Nightingale 	uint32_t dbc;
193bf21cd93STycho Nightingale };
194bf21cd93STycho Nightingale 
195bf21cd93STycho Nightingale struct pci_ahci_softc {
196bf21cd93STycho Nightingale 	struct pci_devinst *asc_pi;
197bf21cd93STycho Nightingale 	pthread_mutex_t	mtx;
198bf21cd93STycho Nightingale 	int ports;
199bf21cd93STycho Nightingale 	uint32_t cap;
200bf21cd93STycho Nightingale 	uint32_t ghc;
201bf21cd93STycho Nightingale 	uint32_t is;
202bf21cd93STycho Nightingale 	uint32_t pi;
203bf21cd93STycho Nightingale 	uint32_t vs;
204bf21cd93STycho Nightingale 	uint32_t ccc_ctl;
205bf21cd93STycho Nightingale 	uint32_t ccc_pts;
206bf21cd93STycho Nightingale 	uint32_t em_loc;
207bf21cd93STycho Nightingale 	uint32_t em_ctl;
208bf21cd93STycho Nightingale 	uint32_t cap2;
209bf21cd93STycho Nightingale 	uint32_t bohc;
210bf21cd93STycho Nightingale 	uint32_t lintr;
211bf21cd93STycho Nightingale 	struct ahci_port port[MAX_PORTS];
212bf21cd93STycho Nightingale };
213bf21cd93STycho Nightingale #define	ahci_ctx(sc)	((sc)->asc_pi->pi_vmctx)
214bf21cd93STycho Nightingale 
2154c87aefeSPatrick Mooney static void ahci_handle_port(struct ahci_port *p);
2164c87aefeSPatrick Mooney 
lba_to_msf(uint8_t * buf,int lba)217bf21cd93STycho Nightingale static inline void lba_to_msf(uint8_t *buf, int lba)
218bf21cd93STycho Nightingale {
219bf21cd93STycho Nightingale 	lba += 150;
220bf21cd93STycho Nightingale 	buf[0] = (lba / 75) / 60;
221bf21cd93STycho Nightingale 	buf[1] = (lba / 75) % 60;
222bf21cd93STycho Nightingale 	buf[2] = lba % 75;
223bf21cd93STycho Nightingale }
224bf21cd93STycho Nightingale 
225bf21cd93STycho Nightingale /*
2264c87aefeSPatrick Mooney  * Generate HBA interrupts on global IS register write.
227bf21cd93STycho Nightingale  */
228bf21cd93STycho Nightingale static void
ahci_generate_intr(struct pci_ahci_softc * sc,uint32_t mask)2294c87aefeSPatrick Mooney ahci_generate_intr(struct pci_ahci_softc *sc, uint32_t mask)
230bf21cd93STycho Nightingale {
2314c87aefeSPatrick Mooney 	struct pci_devinst *pi = sc->asc_pi;
2324c87aefeSPatrick Mooney 	struct ahci_port *p;
2334c87aefeSPatrick Mooney 	int i, nmsg;
2344c87aefeSPatrick Mooney 	uint32_t mmask;
235bf21cd93STycho Nightingale 
2364c87aefeSPatrick Mooney 	/* Update global IS from PxIS/PxIE. */
237bf21cd93STycho Nightingale 	for (i = 0; i < sc->ports; i++) {
2384c87aefeSPatrick Mooney 		p = &sc->port[i];
2394c87aefeSPatrick Mooney 		if (p->is & p->ie)
240bf21cd93STycho Nightingale 			sc->is |= (1 << i);
241bf21cd93STycho Nightingale 	}
242154972afSPatrick Mooney 	DPRINTF("%s(%08x) %08x", __func__, mask, sc->is);
243bf21cd93STycho Nightingale 
2444c87aefeSPatrick Mooney 	/* If there is nothing enabled -- clear legacy interrupt and exit. */
2454c87aefeSPatrick Mooney 	if (sc->is == 0 || (sc->ghc & AHCI_GHC_IE) == 0) {
2464c87aefeSPatrick Mooney 		if (sc->lintr) {
2474c87aefeSPatrick Mooney 			pci_lintr_deassert(pi);
2484c87aefeSPatrick Mooney 			sc->lintr = 0;
2494c87aefeSPatrick Mooney 		}
2504c87aefeSPatrick Mooney 		return;
2514c87aefeSPatrick Mooney 	}
252bf21cd93STycho Nightingale 
2534c87aefeSPatrick Mooney 	/* If there is anything and no MSI -- assert legacy interrupt. */
2544c87aefeSPatrick Mooney 	nmsg = pci_msi_maxmsgnum(pi);
2554c87aefeSPatrick Mooney 	if (nmsg == 0) {
2564c87aefeSPatrick Mooney 		if (!sc->lintr) {
257bf21cd93STycho Nightingale 			sc->lintr = 1;
258bf21cd93STycho Nightingale 			pci_lintr_assert(pi);
259bf21cd93STycho Nightingale 		}
2604c87aefeSPatrick Mooney 		return;
2614c87aefeSPatrick Mooney 	}
2624c87aefeSPatrick Mooney 
2634c87aefeSPatrick Mooney 	/* Assert respective MSIs for ports that were touched. */
2644c87aefeSPatrick Mooney 	for (i = 0; i < nmsg; i++) {
2654c87aefeSPatrick Mooney 		if (sc->ports <= nmsg || i < nmsg - 1)
2664c87aefeSPatrick Mooney 			mmask = 1 << i;
2674c87aefeSPatrick Mooney 		else
2684c87aefeSPatrick Mooney 			mmask = 0xffffffff << i;
2694c87aefeSPatrick Mooney 		if (sc->is & mask && mmask & mask)
2704c87aefeSPatrick Mooney 			pci_generate_msi(pi, i);
2714c87aefeSPatrick Mooney 	}
2724c87aefeSPatrick Mooney }
2734c87aefeSPatrick Mooney 
274bf21cd93STycho Nightingale /*
2754c87aefeSPatrick Mooney  * Generate HBA interrupt on specific port event.
276bf21cd93STycho Nightingale  */
2774c87aefeSPatrick Mooney static void
ahci_port_intr(struct ahci_port * p)2784c87aefeSPatrick Mooney ahci_port_intr(struct ahci_port *p)
2794c87aefeSPatrick Mooney {
2804c87aefeSPatrick Mooney 	struct pci_ahci_softc *sc = p->pr_sc;
2814c87aefeSPatrick Mooney 	struct pci_devinst *pi = sc->asc_pi;
2824c87aefeSPatrick Mooney 	int nmsg;
2834c87aefeSPatrick Mooney 
284154972afSPatrick Mooney 	DPRINTF("%s(%d) %08x/%08x %08x", __func__,
2854c87aefeSPatrick Mooney 	    p->port, p->is, p->ie, sc->is);
2864c87aefeSPatrick Mooney 
2874c87aefeSPatrick Mooney 	/* If there is nothing enabled -- we are done. */
2884c87aefeSPatrick Mooney 	if ((p->is & p->ie) == 0)
2894c87aefeSPatrick Mooney 		return;
2904c87aefeSPatrick Mooney 
2914c87aefeSPatrick Mooney 	/* In case of non-shared MSI always generate interrupt. */
2924c87aefeSPatrick Mooney 	nmsg = pci_msi_maxmsgnum(pi);
2934c87aefeSPatrick Mooney 	if (sc->ports <= nmsg || p->port < nmsg - 1) {
2944c87aefeSPatrick Mooney 		sc->is |= (1 << p->port);
2954c87aefeSPatrick Mooney 		if ((sc->ghc & AHCI_GHC_IE) == 0)
2964c87aefeSPatrick Mooney 			return;
2974c87aefeSPatrick Mooney 		pci_generate_msi(pi, p->port);
2984c87aefeSPatrick Mooney 		return;
2994c87aefeSPatrick Mooney 	}
3004c87aefeSPatrick Mooney 
3014c87aefeSPatrick Mooney 	/* If IS for this port is already set -- do nothing. */
3024c87aefeSPatrick Mooney 	if (sc->is & (1 << p->port))
3034c87aefeSPatrick Mooney 		return;
3044c87aefeSPatrick Mooney 
3054c87aefeSPatrick Mooney 	sc->is |= (1 << p->port);
3064c87aefeSPatrick Mooney 
3074c87aefeSPatrick Mooney 	/* If interrupts are enabled -- generate one. */
3084c87aefeSPatrick Mooney 	if ((sc->ghc & AHCI_GHC_IE) == 0)
3094c87aefeSPatrick Mooney 		return;
3104c87aefeSPatrick Mooney 	if (nmsg > 0) {
3114c87aefeSPatrick Mooney 		pci_generate_msi(pi, nmsg - 1);
3124c87aefeSPatrick Mooney 	} else if (!sc->lintr) {
3134c87aefeSPatrick Mooney 		sc->lintr = 1;
3144c87aefeSPatrick Mooney 		pci_lintr_assert(pi);
315bf21cd93STycho Nightingale 	}
316bf21cd93STycho Nightingale }
317bf21cd93STycho Nightingale 
318bf21cd93STycho Nightingale static void
ahci_write_fis(struct ahci_port * p,enum sata_fis_type ft,uint8_t * fis)319bf21cd93STycho Nightingale ahci_write_fis(struct ahci_port *p, enum sata_fis_type ft, uint8_t *fis)
320bf21cd93STycho Nightingale {
321bf21cd93STycho Nightingale 	int offset, len, irq;
322bf21cd93STycho Nightingale 
323bf21cd93STycho Nightingale 	if (p->rfis == NULL || !(p->cmd & AHCI_P_CMD_FRE))
324bf21cd93STycho Nightingale 		return;
325bf21cd93STycho Nightingale 
326bf21cd93STycho Nightingale 	switch (ft) {
327bf21cd93STycho Nightingale 	case FIS_TYPE_REGD2H:
328bf21cd93STycho Nightingale 		offset = 0x40;
329bf21cd93STycho Nightingale 		len = 20;
3304c87aefeSPatrick Mooney 		irq = (fis[1] & (1 << 6)) ? AHCI_P_IX_DHR : 0;
331bf21cd93STycho Nightingale 		break;
332bf21cd93STycho Nightingale 	case FIS_TYPE_SETDEVBITS:
333bf21cd93STycho Nightingale 		offset = 0x58;
334bf21cd93STycho Nightingale 		len = 8;
3354c87aefeSPatrick Mooney 		irq = (fis[1] & (1 << 6)) ? AHCI_P_IX_SDB : 0;
336bf21cd93STycho Nightingale 		break;
337bf21cd93STycho Nightingale 	case FIS_TYPE_PIOSETUP:
338bf21cd93STycho Nightingale 		offset = 0x20;
339bf21cd93STycho Nightingale 		len = 20;
3404c87aefeSPatrick Mooney 		irq = (fis[1] & (1 << 6)) ? AHCI_P_IX_PS : 0;
341bf21cd93STycho Nightingale 		break;
342bf21cd93STycho Nightingale 	default:
343154972afSPatrick Mooney 		WPRINTF("unsupported fis type %d", ft);
344bf21cd93STycho Nightingale 		return;
345bf21cd93STycho Nightingale 	}
3464c87aefeSPatrick Mooney 	if (fis[2] & ATA_S_ERROR) {
3474c87aefeSPatrick Mooney 		p->waitforclear = 1;
3484c87aefeSPatrick Mooney 		irq |= AHCI_P_IX_TFE;
3494c87aefeSPatrick Mooney 	}
350bf21cd93STycho Nightingale 	memcpy(p->rfis + offset, fis, len);
351bf21cd93STycho Nightingale 	if (irq) {
3524c87aefeSPatrick Mooney 		if (~p->is & irq) {
353bf21cd93STycho Nightingale 			p->is |= irq;
3544c87aefeSPatrick Mooney 			ahci_port_intr(p);
3554c87aefeSPatrick Mooney 		}
356bf21cd93STycho Nightingale 	}
357bf21cd93STycho Nightingale }
358bf21cd93STycho Nightingale 
359bf21cd93STycho Nightingale static void
ahci_write_fis_piosetup(struct ahci_port * p)360bf21cd93STycho Nightingale ahci_write_fis_piosetup(struct ahci_port *p)
361bf21cd93STycho Nightingale {
362bf21cd93STycho Nightingale 	uint8_t fis[20];
363bf21cd93STycho Nightingale 
364bf21cd93STycho Nightingale 	memset(fis, 0, sizeof(fis));
365bf21cd93STycho Nightingale 	fis[0] = FIS_TYPE_PIOSETUP;
366bf21cd93STycho Nightingale 	ahci_write_fis(p, FIS_TYPE_PIOSETUP, fis);
367bf21cd93STycho Nightingale }
368bf21cd93STycho Nightingale 
369bf21cd93STycho Nightingale static void
ahci_write_fis_sdb(struct ahci_port * p,int slot,uint8_t * cfis,uint32_t tfd)3704c87aefeSPatrick Mooney ahci_write_fis_sdb(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t tfd)
371bf21cd93STycho Nightingale {
372bf21cd93STycho Nightingale 	uint8_t fis[8];
373bf21cd93STycho Nightingale 	uint8_t error;
374bf21cd93STycho Nightingale 
375bf21cd93STycho Nightingale 	error = (tfd >> 8) & 0xff;
3764c87aefeSPatrick Mooney 	tfd &= 0x77;
377bf21cd93STycho Nightingale 	memset(fis, 0, sizeof(fis));
3784c87aefeSPatrick Mooney 	fis[0] = FIS_TYPE_SETDEVBITS;
3794c87aefeSPatrick Mooney 	fis[1] = (1 << 6);
3804c87aefeSPatrick Mooney 	fis[2] = tfd;
3814c87aefeSPatrick Mooney 	fis[3] = error;
3824c87aefeSPatrick Mooney 	if (fis[2] & ATA_S_ERROR) {
3834c87aefeSPatrick Mooney 		p->err_cfis[0] = slot;
3844c87aefeSPatrick Mooney 		p->err_cfis[2] = tfd;
3854c87aefeSPatrick Mooney 		p->err_cfis[3] = error;
3864c87aefeSPatrick Mooney 		memcpy(&p->err_cfis[4], cfis + 4, 16);
3874c87aefeSPatrick Mooney 	} else {
388bf21cd93STycho Nightingale 		*(uint32_t *)(fis + 4) = (1 << slot);
3894c87aefeSPatrick Mooney 		p->sact &= ~(1 << slot);
3904c87aefeSPatrick Mooney 	}
3914c87aefeSPatrick Mooney 	p->tfd &= ~0x77;
3924c87aefeSPatrick Mooney 	p->tfd |= tfd;
393bf21cd93STycho Nightingale 	ahci_write_fis(p, FIS_TYPE_SETDEVBITS, fis);
394bf21cd93STycho Nightingale }
395bf21cd93STycho Nightingale 
396bf21cd93STycho Nightingale static void
ahci_write_fis_d2h(struct ahci_port * p,int slot,uint8_t * cfis,uint32_t tfd)397bf21cd93STycho Nightingale ahci_write_fis_d2h(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t tfd)
398bf21cd93STycho Nightingale {
399bf21cd93STycho Nightingale 	uint8_t fis[20];
400bf21cd93STycho Nightingale 	uint8_t error;
401bf21cd93STycho Nightingale 
402bf21cd93STycho Nightingale 	error = (tfd >> 8) & 0xff;
403bf21cd93STycho Nightingale 	memset(fis, 0, sizeof(fis));
404bf21cd93STycho Nightingale 	fis[0] = FIS_TYPE_REGD2H;
405bf21cd93STycho Nightingale 	fis[1] = (1 << 6);
406bf21cd93STycho Nightingale 	fis[2] = tfd & 0xff;
407bf21cd93STycho Nightingale 	fis[3] = error;
408bf21cd93STycho Nightingale 	fis[4] = cfis[4];
409bf21cd93STycho Nightingale 	fis[5] = cfis[5];
410bf21cd93STycho Nightingale 	fis[6] = cfis[6];
411bf21cd93STycho Nightingale 	fis[7] = cfis[7];
412bf21cd93STycho Nightingale 	fis[8] = cfis[8];
413bf21cd93STycho Nightingale 	fis[9] = cfis[9];
414bf21cd93STycho Nightingale 	fis[10] = cfis[10];
415bf21cd93STycho Nightingale 	fis[11] = cfis[11];
416bf21cd93STycho Nightingale 	fis[12] = cfis[12];
417bf21cd93STycho Nightingale 	fis[13] = cfis[13];
4184c87aefeSPatrick Mooney 	if (fis[2] & ATA_S_ERROR) {
4194c87aefeSPatrick Mooney 		p->err_cfis[0] = 0x80;
4204c87aefeSPatrick Mooney 		p->err_cfis[2] = tfd & 0xff;
4214c87aefeSPatrick Mooney 		p->err_cfis[3] = error;
4224c87aefeSPatrick Mooney 		memcpy(&p->err_cfis[4], cfis + 4, 16);
4234c87aefeSPatrick Mooney 	} else
424bf21cd93STycho Nightingale 		p->ci &= ~(1 << slot);
425bf21cd93STycho Nightingale 	p->tfd = tfd;
426bf21cd93STycho Nightingale 	ahci_write_fis(p, FIS_TYPE_REGD2H, fis);
427bf21cd93STycho Nightingale }
428bf21cd93STycho Nightingale 
429bf21cd93STycho Nightingale static void
ahci_write_fis_d2h_ncq(struct ahci_port * p,int slot)4304c87aefeSPatrick Mooney ahci_write_fis_d2h_ncq(struct ahci_port *p, int slot)
4314c87aefeSPatrick Mooney {
4324c87aefeSPatrick Mooney 	uint8_t fis[20];
4334c87aefeSPatrick Mooney 
4344c87aefeSPatrick Mooney 	p->tfd = ATA_S_READY | ATA_S_DSC;
4354c87aefeSPatrick Mooney 	memset(fis, 0, sizeof(fis));
4364c87aefeSPatrick Mooney 	fis[0] = FIS_TYPE_REGD2H;
4374c87aefeSPatrick Mooney 	fis[1] = 0;			/* No interrupt */
4384c87aefeSPatrick Mooney 	fis[2] = p->tfd;		/* Status */
4394c87aefeSPatrick Mooney 	fis[3] = 0;			/* No error */
4404c87aefeSPatrick Mooney 	p->ci &= ~(1 << slot);
4414c87aefeSPatrick Mooney 	ahci_write_fis(p, FIS_TYPE_REGD2H, fis);
4424c87aefeSPatrick Mooney }
4434c87aefeSPatrick Mooney 
4444c87aefeSPatrick Mooney static void
ahci_write_reset_fis_d2h(struct ahci_port * p)445bf21cd93STycho Nightingale ahci_write_reset_fis_d2h(struct ahci_port *p)
446bf21cd93STycho Nightingale {
447bf21cd93STycho Nightingale 	uint8_t fis[20];
448bf21cd93STycho Nightingale 
449bf21cd93STycho Nightingale 	memset(fis, 0, sizeof(fis));
450bf21cd93STycho Nightingale 	fis[0] = FIS_TYPE_REGD2H;
451bf21cd93STycho Nightingale 	fis[3] = 1;
452bf21cd93STycho Nightingale 	fis[4] = 1;
453bf21cd93STycho Nightingale 	if (p->atapi) {
454bf21cd93STycho Nightingale 		fis[5] = 0x14;
455bf21cd93STycho Nightingale 		fis[6] = 0xeb;
456bf21cd93STycho Nightingale 	}
457bf21cd93STycho Nightingale 	fis[12] = 1;
458bf21cd93STycho Nightingale 	ahci_write_fis(p, FIS_TYPE_REGD2H, fis);
459bf21cd93STycho Nightingale }
460bf21cd93STycho Nightingale 
461bf21cd93STycho Nightingale static void
ahci_check_stopped(struct ahci_port * p)462bf21cd93STycho Nightingale ahci_check_stopped(struct ahci_port *p)
463bf21cd93STycho Nightingale {
464bf21cd93STycho Nightingale 	/*
465bf21cd93STycho Nightingale 	 * If we are no longer processing the command list and nothing
466bf21cd93STycho Nightingale 	 * is in-flight, clear the running bit, the current command
467bf21cd93STycho Nightingale 	 * slot, the command issue and active bits.
468bf21cd93STycho Nightingale 	 */
469bf21cd93STycho Nightingale 	if (!(p->cmd & AHCI_P_CMD_ST)) {
470bf21cd93STycho Nightingale 		if (p->pending == 0) {
4714c87aefeSPatrick Mooney 			p->ccs = 0;
472bf21cd93STycho Nightingale 			p->cmd &= ~(AHCI_P_CMD_CR | AHCI_P_CMD_CCS_MASK);
473bf21cd93STycho Nightingale 			p->ci = 0;
474bf21cd93STycho Nightingale 			p->sact = 0;
4754c87aefeSPatrick Mooney 			p->waitforclear = 0;
476bf21cd93STycho Nightingale 		}
477bf21cd93STycho Nightingale 	}
478bf21cd93STycho Nightingale }
479bf21cd93STycho Nightingale 
480bf21cd93STycho Nightingale static void
ahci_port_stop(struct ahci_port * p)481bf21cd93STycho Nightingale ahci_port_stop(struct ahci_port *p)
482bf21cd93STycho Nightingale {
483bf21cd93STycho Nightingale 	struct ahci_ioreq *aior;
484bf21cd93STycho Nightingale 	uint8_t *cfis;
485bf21cd93STycho Nightingale 	int slot;
486bf21cd93STycho Nightingale 	int error;
487bf21cd93STycho Nightingale 
488bf21cd93STycho Nightingale 	assert(pthread_mutex_isowned_np(&p->pr_sc->mtx));
489bf21cd93STycho Nightingale 
490bf21cd93STycho Nightingale 	TAILQ_FOREACH(aior, &p->iobhd, io_blist) {
491bf21cd93STycho Nightingale 		/*
492bf21cd93STycho Nightingale 		 * Try to cancel the outstanding blockif request.
493bf21cd93STycho Nightingale 		 */
494bf21cd93STycho Nightingale 		error = blockif_cancel(p->bctx, &aior->io_req);
495bf21cd93STycho Nightingale 		if (error != 0)
496bf21cd93STycho Nightingale 			continue;
497bf21cd93STycho Nightingale 
498bf21cd93STycho Nightingale 		slot = aior->slot;
499bf21cd93STycho Nightingale 		cfis = aior->cfis;
500bf21cd93STycho Nightingale 		if (cfis[2] == ATA_WRITE_FPDMA_QUEUED ||
5014c87aefeSPatrick Mooney 		    cfis[2] == ATA_READ_FPDMA_QUEUED ||
5024c87aefeSPatrick Mooney 		    cfis[2] == ATA_SEND_FPDMA_QUEUED)
5034c87aefeSPatrick Mooney 			p->sact &= ~(1 << slot);	/* NCQ */
504bf21cd93STycho Nightingale 		else
505bf21cd93STycho Nightingale 			p->ci &= ~(1 << slot);
506bf21cd93STycho Nightingale 
507bf21cd93STycho Nightingale 		/*
508bf21cd93STycho Nightingale 		 * This command is now done.
509bf21cd93STycho Nightingale 		 */
510bf21cd93STycho Nightingale 		p->pending &= ~(1 << slot);
511bf21cd93STycho Nightingale 
512bf21cd93STycho Nightingale 		/*
513bf21cd93STycho Nightingale 		 * Delete the blockif request from the busy list
514bf21cd93STycho Nightingale 		 */
515bf21cd93STycho Nightingale 		TAILQ_REMOVE(&p->iobhd, aior, io_blist);
516bf21cd93STycho Nightingale 
517bf21cd93STycho Nightingale 		/*
518bf21cd93STycho Nightingale 		 * Move the blockif request back to the free list
519bf21cd93STycho Nightingale 		 */
520bf21cd93STycho Nightingale 		STAILQ_INSERT_TAIL(&p->iofhd, aior, io_flist);
521bf21cd93STycho Nightingale 	}
522bf21cd93STycho Nightingale 
523bf21cd93STycho Nightingale 	ahci_check_stopped(p);
524bf21cd93STycho Nightingale }
525bf21cd93STycho Nightingale 
526bf21cd93STycho Nightingale static void
ahci_port_reset(struct ahci_port * pr)527bf21cd93STycho Nightingale ahci_port_reset(struct ahci_port *pr)
528bf21cd93STycho Nightingale {
529bf21cd93STycho Nightingale 	pr->serr = 0;
530bf21cd93STycho Nightingale 	pr->sact = 0;
531bf21cd93STycho Nightingale 	pr->xfermode = ATA_UDMA6;
532bf21cd93STycho Nightingale 	pr->mult_sectors = 128;
533bf21cd93STycho Nightingale 
534bf21cd93STycho Nightingale 	if (!pr->bctx) {
535bf21cd93STycho Nightingale 		pr->ssts = ATA_SS_DET_NO_DEVICE;
536bf21cd93STycho Nightingale 		pr->sig = 0xFFFFFFFF;
537bf21cd93STycho Nightingale 		pr->tfd = 0x7F;
538bf21cd93STycho Nightingale 		return;
539bf21cd93STycho Nightingale 	}
5404c87aefeSPatrick Mooney 	pr->ssts = ATA_SS_DET_PHY_ONLINE | ATA_SS_IPM_ACTIVE;
5414c87aefeSPatrick Mooney 	if (pr->sctl & ATA_SC_SPD_MASK)
5424c87aefeSPatrick Mooney 		pr->ssts |= (pr->sctl & ATA_SC_SPD_MASK);
5434c87aefeSPatrick Mooney 	else
5444c87aefeSPatrick Mooney 		pr->ssts |= ATA_SS_SPD_GEN3;
545bf21cd93STycho Nightingale 	pr->tfd = (1 << 8) | ATA_S_DSC | ATA_S_DMA;
546bf21cd93STycho Nightingale 	if (!pr->atapi) {
547bf21cd93STycho Nightingale 		pr->sig = PxSIG_ATA;
548bf21cd93STycho Nightingale 		pr->tfd |= ATA_S_READY;
549bf21cd93STycho Nightingale 	} else
550bf21cd93STycho Nightingale 		pr->sig = PxSIG_ATAPI;
551bf21cd93STycho Nightingale 	ahci_write_reset_fis_d2h(pr);
552bf21cd93STycho Nightingale }
553bf21cd93STycho Nightingale 
554bf21cd93STycho Nightingale static void
ahci_reset(struct pci_ahci_softc * sc)555bf21cd93STycho Nightingale ahci_reset(struct pci_ahci_softc *sc)
556bf21cd93STycho Nightingale {
557bf21cd93STycho Nightingale 	int i;
558bf21cd93STycho Nightingale 
559bf21cd93STycho Nightingale 	sc->ghc = AHCI_GHC_AE;
560bf21cd93STycho Nightingale 	sc->is = 0;
561bf21cd93STycho Nightingale 
562bf21cd93STycho Nightingale 	if (sc->lintr) {
563bf21cd93STycho Nightingale 		pci_lintr_deassert(sc->asc_pi);
564bf21cd93STycho Nightingale 		sc->lintr = 0;
565bf21cd93STycho Nightingale 	}
566bf21cd93STycho Nightingale 
567bf21cd93STycho Nightingale 	for (i = 0; i < sc->ports; i++) {
568bf21cd93STycho Nightingale 		sc->port[i].ie = 0;
569bf21cd93STycho Nightingale 		sc->port[i].is = 0;
5704c87aefeSPatrick Mooney 		sc->port[i].cmd = (AHCI_P_CMD_SUD | AHCI_P_CMD_POD);
5714c87aefeSPatrick Mooney 		if (sc->port[i].bctx)
5724c87aefeSPatrick Mooney 			sc->port[i].cmd |= AHCI_P_CMD_CPS;
5734c87aefeSPatrick Mooney 		sc->port[i].sctl = 0;
574bf21cd93STycho Nightingale 		ahci_port_reset(&sc->port[i]);
575bf21cd93STycho Nightingale 	}
576bf21cd93STycho Nightingale }
577bf21cd93STycho Nightingale 
578bf21cd93STycho Nightingale static void
ata_string(uint8_t * dest,const char * src,int len)579bf21cd93STycho Nightingale ata_string(uint8_t *dest, const char *src, int len)
580bf21cd93STycho Nightingale {
581bf21cd93STycho Nightingale 	int i;
582bf21cd93STycho Nightingale 
583bf21cd93STycho Nightingale 	for (i = 0; i < len; i++) {
584bf21cd93STycho Nightingale 		if (*src)
585bf21cd93STycho Nightingale 			dest[i ^ 1] = *src++;
586bf21cd93STycho Nightingale 		else
587bf21cd93STycho Nightingale 			dest[i ^ 1] = ' ';
588bf21cd93STycho Nightingale 	}
589bf21cd93STycho Nightingale }
590bf21cd93STycho Nightingale 
591bf21cd93STycho Nightingale static void
atapi_string(uint8_t * dest,const char * src,int len)592bf21cd93STycho Nightingale atapi_string(uint8_t *dest, const char *src, int len)
593bf21cd93STycho Nightingale {
594bf21cd93STycho Nightingale 	int i;
595bf21cd93STycho Nightingale 
596bf21cd93STycho Nightingale 	for (i = 0; i < len; i++) {
597bf21cd93STycho Nightingale 		if (*src)
598bf21cd93STycho Nightingale 			dest[i] = *src++;
599bf21cd93STycho Nightingale 		else
600bf21cd93STycho Nightingale 			dest[i] = ' ';
601bf21cd93STycho Nightingale 	}
602bf21cd93STycho Nightingale }
603bf21cd93STycho Nightingale 
6044c87aefeSPatrick Mooney /*
6054c87aefeSPatrick Mooney  * Build up the iovec based on the PRDT, 'done' and 'len'.
6064c87aefeSPatrick Mooney  */
607bf21cd93STycho Nightingale static void
ahci_build_iov(struct ahci_port * p,struct ahci_ioreq * aior,struct ahci_prdt_entry * prdt,uint16_t prdtl)6084c87aefeSPatrick Mooney ahci_build_iov(struct ahci_port *p, struct ahci_ioreq *aior,
6094c87aefeSPatrick Mooney     struct ahci_prdt_entry *prdt, uint16_t prdtl)
6104c87aefeSPatrick Mooney {
6114c87aefeSPatrick Mooney 	struct blockif_req *breq = &aior->io_req;
61259d65d31SAndy Fiddaman 	uint32_t dbcsz, extra, left, skip, todo;
61359d65d31SAndy Fiddaman 	int i, j;
61459d65d31SAndy Fiddaman 
61559d65d31SAndy Fiddaman 	assert(aior->len >= aior->done);
6164c87aefeSPatrick Mooney 
6174c87aefeSPatrick Mooney 	/* Copy part of PRDT between 'done' and 'len' bytes into the iov. */
6184c87aefeSPatrick Mooney 	skip = aior->done;
6194c87aefeSPatrick Mooney 	left = aior->len - aior->done;
6204c87aefeSPatrick Mooney 	todo = 0;
6214c87aefeSPatrick Mooney 	for (i = 0, j = 0; i < prdtl && j < BLOCKIF_IOV_MAX && left > 0;
6224c87aefeSPatrick Mooney 	    i++, prdt++) {
6234c87aefeSPatrick Mooney 		dbcsz = (prdt->dbc & DBCMASK) + 1;
6244c87aefeSPatrick Mooney 		/* Skip already done part of the PRDT */
6254c87aefeSPatrick Mooney 		if (dbcsz <= skip) {
6264c87aefeSPatrick Mooney 			skip -= dbcsz;
6274c87aefeSPatrick Mooney 			continue;
6284c87aefeSPatrick Mooney 		}
6294c87aefeSPatrick Mooney 		dbcsz -= skip;
6304c87aefeSPatrick Mooney 		if (dbcsz > left)
6314c87aefeSPatrick Mooney 			dbcsz = left;
6324c87aefeSPatrick Mooney 		breq->br_iov[j].iov_base = paddr_guest2host(ahci_ctx(p->pr_sc),
6334c87aefeSPatrick Mooney 		    prdt->dba + skip, dbcsz);
6344c87aefeSPatrick Mooney 		breq->br_iov[j].iov_len = dbcsz;
6354c87aefeSPatrick Mooney 		todo += dbcsz;
6364c87aefeSPatrick Mooney 		left -= dbcsz;
6374c87aefeSPatrick Mooney 		skip = 0;
6384c87aefeSPatrick Mooney 		j++;
6394c87aefeSPatrick Mooney 	}
6404c87aefeSPatrick Mooney 
6414c87aefeSPatrick Mooney 	/* If we got limited by IOV length, round I/O down to sector size. */
6424c87aefeSPatrick Mooney 	if (j == BLOCKIF_IOV_MAX) {
6434c87aefeSPatrick Mooney 		extra = todo % blockif_sectsz(p->bctx);
6444c87aefeSPatrick Mooney 		todo -= extra;
6454c87aefeSPatrick Mooney 		assert(todo > 0);
6464c87aefeSPatrick Mooney 		while (extra > 0) {
6474c87aefeSPatrick Mooney 			if (breq->br_iov[j - 1].iov_len > extra) {
6484c87aefeSPatrick Mooney 				breq->br_iov[j - 1].iov_len -= extra;
6494c87aefeSPatrick Mooney 				break;
6504c87aefeSPatrick Mooney 			}
6514c87aefeSPatrick Mooney 			extra -= breq->br_iov[j - 1].iov_len;
6524c87aefeSPatrick Mooney 			j--;
6534c87aefeSPatrick Mooney 		}
6544c87aefeSPatrick Mooney 	}
6554c87aefeSPatrick Mooney 
6564c87aefeSPatrick Mooney 	breq->br_iovcnt = j;
6574c87aefeSPatrick Mooney 	breq->br_resid = todo;
6584c87aefeSPatrick Mooney 	aior->done += todo;
6594c87aefeSPatrick Mooney 	aior->more = (aior->done < aior->len && i < prdtl);
6604c87aefeSPatrick Mooney }
6614c87aefeSPatrick Mooney 
6624c87aefeSPatrick Mooney static void
ahci_handle_rw(struct ahci_port * p,int slot,uint8_t * cfis,uint32_t done)6634c87aefeSPatrick Mooney ahci_handle_rw(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t done)
664bf21cd93STycho Nightingale {
665bf21cd93STycho Nightingale 	struct ahci_ioreq *aior;
666bf21cd93STycho Nightingale 	struct blockif_req *breq;
667bf21cd93STycho Nightingale 	struct ahci_prdt_entry *prdt;
668bf21cd93STycho Nightingale 	struct ahci_cmd_hdr *hdr;
669bf21cd93STycho Nightingale 	uint64_t lba;
670bf21cd93STycho Nightingale 	uint32_t len;
6714c87aefeSPatrick Mooney 	int err, first, ncq, readop;
672bf21cd93STycho Nightingale 
673bf21cd93STycho Nightingale 	prdt = (struct ahci_prdt_entry *)(cfis + 0x80);
674bf21cd93STycho Nightingale 	hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
675bf21cd93STycho Nightingale 	ncq = 0;
676bf21cd93STycho Nightingale 	readop = 1;
6774c87aefeSPatrick Mooney 	first = (done == 0);
678bf21cd93STycho Nightingale 
6794c87aefeSPatrick Mooney 	if (cfis[2] == ATA_WRITE || cfis[2] == ATA_WRITE48 ||
6804c87aefeSPatrick Mooney 	    cfis[2] == ATA_WRITE_MUL || cfis[2] == ATA_WRITE_MUL48 ||
6814c87aefeSPatrick Mooney 	    cfis[2] == ATA_WRITE_DMA || cfis[2] == ATA_WRITE_DMA48 ||
682bf21cd93STycho Nightingale 	    cfis[2] == ATA_WRITE_FPDMA_QUEUED)
683bf21cd93STycho Nightingale 		readop = 0;
684bf21cd93STycho Nightingale 
685bf21cd93STycho Nightingale 	if (cfis[2] == ATA_WRITE_FPDMA_QUEUED ||
686bf21cd93STycho Nightingale 	    cfis[2] == ATA_READ_FPDMA_QUEUED) {
687bf21cd93STycho Nightingale 		lba = ((uint64_t)cfis[10] << 40) |
688bf21cd93STycho Nightingale 			((uint64_t)cfis[9] << 32) |
689bf21cd93STycho Nightingale 			((uint64_t)cfis[8] << 24) |
690bf21cd93STycho Nightingale 			((uint64_t)cfis[6] << 16) |
691bf21cd93STycho Nightingale 			((uint64_t)cfis[5] << 8) |
692bf21cd93STycho Nightingale 			cfis[4];
693bf21cd93STycho Nightingale 		len = cfis[11] << 8 | cfis[3];
694bf21cd93STycho Nightingale 		if (!len)
695bf21cd93STycho Nightingale 			len = 65536;
696bf21cd93STycho Nightingale 		ncq = 1;
6974c87aefeSPatrick Mooney 	} else if (cfis[2] == ATA_READ48 || cfis[2] == ATA_WRITE48 ||
6984c87aefeSPatrick Mooney 	    cfis[2] == ATA_READ_MUL48 || cfis[2] == ATA_WRITE_MUL48 ||
6994c87aefeSPatrick Mooney 	    cfis[2] == ATA_READ_DMA48 || cfis[2] == ATA_WRITE_DMA48) {
700bf21cd93STycho Nightingale 		lba = ((uint64_t)cfis[10] << 40) |
701bf21cd93STycho Nightingale 			((uint64_t)cfis[9] << 32) |
702bf21cd93STycho Nightingale 			((uint64_t)cfis[8] << 24) |
703bf21cd93STycho Nightingale 			((uint64_t)cfis[6] << 16) |
704bf21cd93STycho Nightingale 			((uint64_t)cfis[5] << 8) |
705bf21cd93STycho Nightingale 			cfis[4];
706bf21cd93STycho Nightingale 		len = cfis[13] << 8 | cfis[12];
707bf21cd93STycho Nightingale 		if (!len)
708bf21cd93STycho Nightingale 			len = 65536;
709bf21cd93STycho Nightingale 	} else {
710bf21cd93STycho Nightingale 		lba = ((cfis[7] & 0xf) << 24) | (cfis[6] << 16) |
711bf21cd93STycho Nightingale 			(cfis[5] << 8) | cfis[4];
712bf21cd93STycho Nightingale 		len = cfis[12];
713bf21cd93STycho Nightingale 		if (!len)
714bf21cd93STycho Nightingale 			len = 256;
715bf21cd93STycho Nightingale 	}
716bf21cd93STycho Nightingale 	lba *= blockif_sectsz(p->bctx);
717bf21cd93STycho Nightingale 	len *= blockif_sectsz(p->bctx);
718bf21cd93STycho Nightingale 
7194c87aefeSPatrick Mooney 	/* Pull request off free list */
720bf21cd93STycho Nightingale 	aior = STAILQ_FIRST(&p->iofhd);
721bf21cd93STycho Nightingale 	assert(aior != NULL);
722bf21cd93STycho Nightingale 	STAILQ_REMOVE_HEAD(&p->iofhd, io_flist);
7234c87aefeSPatrick Mooney 
724bf21cd93STycho Nightingale 	aior->cfis = cfis;
725bf21cd93STycho Nightingale 	aior->slot = slot;
726bf21cd93STycho Nightingale 	aior->len = len;
727bf21cd93STycho Nightingale 	aior->done = done;
728bf21cd93STycho Nightingale 	breq = &aior->io_req;
729bf21cd93STycho Nightingale 	breq->br_offset = lba + done;
7304c87aefeSPatrick Mooney 	ahci_build_iov(p, aior, prdt, hdr->prdtl);
731bf21cd93STycho Nightingale 
7324c87aefeSPatrick Mooney 	/* Mark this command in-flight. */
733bf21cd93STycho Nightingale 	p->pending |= 1 << slot;
734bf21cd93STycho Nightingale 
7354c87aefeSPatrick Mooney 	/* Stuff request onto busy list. */
736bf21cd93STycho Nightingale 	TAILQ_INSERT_HEAD(&p->iobhd, aior, io_blist);
737bf21cd93STycho Nightingale 
7384c87aefeSPatrick Mooney 	if (ncq && first)
7394c87aefeSPatrick Mooney 		ahci_write_fis_d2h_ncq(p, slot);
740bf21cd93STycho Nightingale 
741bf21cd93STycho Nightingale 	if (readop)
742bf21cd93STycho Nightingale 		err = blockif_read(p->bctx, breq);
743bf21cd93STycho Nightingale 	else
744bf21cd93STycho Nightingale 		err = blockif_write(p->bctx, breq);
745bf21cd93STycho Nightingale 	assert(err == 0);
746bf21cd93STycho Nightingale }
747bf21cd93STycho Nightingale 
748bf21cd93STycho Nightingale static void
ahci_handle_flush(struct ahci_port * p,int slot,uint8_t * cfis)749bf21cd93STycho Nightingale ahci_handle_flush(struct ahci_port *p, int slot, uint8_t *cfis)
750bf21cd93STycho Nightingale {
751bf21cd93STycho Nightingale 	struct ahci_ioreq *aior;
752bf21cd93STycho Nightingale 	struct blockif_req *breq;
753bf21cd93STycho Nightingale 	int err;
754bf21cd93STycho Nightingale 
755bf21cd93STycho Nightingale 	/*
756bf21cd93STycho Nightingale 	 * Pull request off free list
757bf21cd93STycho Nightingale 	 */
758bf21cd93STycho Nightingale 	aior = STAILQ_FIRST(&p->iofhd);
759bf21cd93STycho Nightingale 	assert(aior != NULL);
760bf21cd93STycho Nightingale 	STAILQ_REMOVE_HEAD(&p->iofhd, io_flist);
761bf21cd93STycho Nightingale 	aior->cfis = cfis;
762bf21cd93STycho Nightingale 	aior->slot = slot;
763bf21cd93STycho Nightingale 	aior->len = 0;
764bf21cd93STycho Nightingale 	aior->done = 0;
7654c87aefeSPatrick Mooney 	aior->more = 0;
766bf21cd93STycho Nightingale 	breq = &aior->io_req;
767bf21cd93STycho Nightingale 
768bf21cd93STycho Nightingale 	/*
769bf21cd93STycho Nightingale 	 * Mark this command in-flight.
770bf21cd93STycho Nightingale 	 */
771bf21cd93STycho Nightingale 	p->pending |= 1 << slot;
772bf21cd93STycho Nightingale 
773bf21cd93STycho Nightingale 	/*
774bf21cd93STycho Nightingale 	 * Stuff request onto busy list
775bf21cd93STycho Nightingale 	 */
776bf21cd93STycho Nightingale 	TAILQ_INSERT_HEAD(&p->iobhd, aior, io_blist);
777bf21cd93STycho Nightingale 
778bf21cd93STycho Nightingale 	err = blockif_flush(p->bctx, breq);
779bf21cd93STycho Nightingale 	assert(err == 0);
780bf21cd93STycho Nightingale }
781bf21cd93STycho Nightingale 
782bf21cd93STycho Nightingale static inline void
read_prdt(struct ahci_port * p,int slot,uint8_t * cfis,void * buf,unsigned int size)78359d65d31SAndy Fiddaman read_prdt(struct ahci_port *p, int slot, uint8_t *cfis, void *buf,
78459d65d31SAndy Fiddaman     unsigned int size)
7854c87aefeSPatrick Mooney {
7864c87aefeSPatrick Mooney 	struct ahci_cmd_hdr *hdr;
7874c87aefeSPatrick Mooney 	struct ahci_prdt_entry *prdt;
78859d65d31SAndy Fiddaman 	uint8_t *to;
78959d65d31SAndy Fiddaman 	unsigned int len;
79059d65d31SAndy Fiddaman 	int i;
7914c87aefeSPatrick Mooney 
7924c87aefeSPatrick Mooney 	hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
7934c87aefeSPatrick Mooney 	len = size;
7944c87aefeSPatrick Mooney 	to = buf;
7954c87aefeSPatrick Mooney 	prdt = (struct ahci_prdt_entry *)(cfis + 0x80);
7964c87aefeSPatrick Mooney 	for (i = 0; i < hdr->prdtl && len; i++) {
7974c87aefeSPatrick Mooney 		uint8_t *ptr;
7984c87aefeSPatrick Mooney 		uint32_t dbcsz;
79959d65d31SAndy Fiddaman 		unsigned int sublen;
8004c87aefeSPatrick Mooney 
8014c87aefeSPatrick Mooney 		dbcsz = (prdt->dbc & DBCMASK) + 1;
8024c87aefeSPatrick Mooney 		ptr = paddr_guest2host(ahci_ctx(p->pr_sc), prdt->dba, dbcsz);
8034c87aefeSPatrick Mooney 		sublen = MIN(len, dbcsz);
8044c87aefeSPatrick Mooney 		memcpy(to, ptr, sublen);
8054c87aefeSPatrick Mooney 		len -= sublen;
8064c87aefeSPatrick Mooney 		to += sublen;
8074c87aefeSPatrick Mooney 		prdt++;
8084c87aefeSPatrick Mooney 	}
8094c87aefeSPatrick Mooney }
8104c87aefeSPatrick Mooney 
8114c87aefeSPatrick Mooney static void
ahci_handle_dsm_trim(struct ahci_port * p,int slot,uint8_t * cfis,uint32_t done)8124c87aefeSPatrick Mooney ahci_handle_dsm_trim(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t done)
8134c87aefeSPatrick Mooney {
8144c87aefeSPatrick Mooney 	struct ahci_ioreq *aior;
8154c87aefeSPatrick Mooney 	struct blockif_req *breq;
8164c87aefeSPatrick Mooney 	uint8_t *entry;
8174c87aefeSPatrick Mooney 	uint64_t elba;
8184c87aefeSPatrick Mooney 	uint32_t len, elen;
8194c87aefeSPatrick Mooney 	int err, first, ncq;
8204c87aefeSPatrick Mooney 	uint8_t buf[512];
8214c87aefeSPatrick Mooney 
8224c87aefeSPatrick Mooney 	first = (done == 0);
8234c87aefeSPatrick Mooney 	if (cfis[2] == ATA_DATA_SET_MANAGEMENT) {
8244c87aefeSPatrick Mooney 		len = (uint16_t)cfis[13] << 8 | cfis[12];
8254c87aefeSPatrick Mooney 		len *= 512;
8264c87aefeSPatrick Mooney 		ncq = 0;
8274c87aefeSPatrick Mooney 	} else { /* ATA_SEND_FPDMA_QUEUED */
8284c87aefeSPatrick Mooney 		len = (uint16_t)cfis[11] << 8 | cfis[3];
8294c87aefeSPatrick Mooney 		len *= 512;
8304c87aefeSPatrick Mooney 		ncq = 1;
8314c87aefeSPatrick Mooney 	}
8324c87aefeSPatrick Mooney 	read_prdt(p, slot, cfis, buf, sizeof(buf));
8334c87aefeSPatrick Mooney 
8344c87aefeSPatrick Mooney next:
8354c87aefeSPatrick Mooney 	entry = &buf[done];
8364c87aefeSPatrick Mooney 	elba = ((uint64_t)entry[5] << 40) |
8374c87aefeSPatrick Mooney 		((uint64_t)entry[4] << 32) |
8384c87aefeSPatrick Mooney 		((uint64_t)entry[3] << 24) |
8394c87aefeSPatrick Mooney 		((uint64_t)entry[2] << 16) |
8404c87aefeSPatrick Mooney 		((uint64_t)entry[1] << 8) |
8414c87aefeSPatrick Mooney 		entry[0];
8424c87aefeSPatrick Mooney 	elen = (uint16_t)entry[7] << 8 | entry[6];
8434c87aefeSPatrick Mooney 	done += 8;
8444c87aefeSPatrick Mooney 	if (elen == 0) {
8454c87aefeSPatrick Mooney 		if (done >= len) {
8464c87aefeSPatrick Mooney 			if (ncq) {
8474c87aefeSPatrick Mooney 				if (first)
8484c87aefeSPatrick Mooney 					ahci_write_fis_d2h_ncq(p, slot);
8494c87aefeSPatrick Mooney 				ahci_write_fis_sdb(p, slot, cfis,
8504c87aefeSPatrick Mooney 				    ATA_S_READY | ATA_S_DSC);
8514c87aefeSPatrick Mooney 			} else {
8524c87aefeSPatrick Mooney 				ahci_write_fis_d2h(p, slot, cfis,
8534c87aefeSPatrick Mooney 				    ATA_S_READY | ATA_S_DSC);
8544c87aefeSPatrick Mooney 			}
8554c87aefeSPatrick Mooney 			p->pending &= ~(1 << slot);
8564c87aefeSPatrick Mooney 			ahci_check_stopped(p);
8574c87aefeSPatrick Mooney 			if (!first)
8584c87aefeSPatrick Mooney 				ahci_handle_port(p);
8594c87aefeSPatrick Mooney 			return;
8604c87aefeSPatrick Mooney 		}
8614c87aefeSPatrick Mooney 		goto next;
8624c87aefeSPatrick Mooney 	}
8634c87aefeSPatrick Mooney 
8644c87aefeSPatrick Mooney 	/*
8654c87aefeSPatrick Mooney 	 * Pull request off free list
8664c87aefeSPatrick Mooney 	 */
8674c87aefeSPatrick Mooney 	aior = STAILQ_FIRST(&p->iofhd);
8684c87aefeSPatrick Mooney 	assert(aior != NULL);
8694c87aefeSPatrick Mooney 	STAILQ_REMOVE_HEAD(&p->iofhd, io_flist);
8704c87aefeSPatrick Mooney 	aior->cfis = cfis;
8714c87aefeSPatrick Mooney 	aior->slot = slot;
8724c87aefeSPatrick Mooney 	aior->len = len;
8734c87aefeSPatrick Mooney 	aior->done = done;
8744c87aefeSPatrick Mooney 	aior->more = (len != done);
8754c87aefeSPatrick Mooney 
8764c87aefeSPatrick Mooney 	breq = &aior->io_req;
8774c87aefeSPatrick Mooney 	breq->br_offset = elba * blockif_sectsz(p->bctx);
8784c87aefeSPatrick Mooney 	breq->br_resid = elen * blockif_sectsz(p->bctx);
8794c87aefeSPatrick Mooney 
8804c87aefeSPatrick Mooney 	/*
8814c87aefeSPatrick Mooney 	 * Mark this command in-flight.
8824c87aefeSPatrick Mooney 	 */
8834c87aefeSPatrick Mooney 	p->pending |= 1 << slot;
8844c87aefeSPatrick Mooney 
8854c87aefeSPatrick Mooney 	/*
8864c87aefeSPatrick Mooney 	 * Stuff request onto busy list
8874c87aefeSPatrick Mooney 	 */
8884c87aefeSPatrick Mooney 	TAILQ_INSERT_HEAD(&p->iobhd, aior, io_blist);
8894c87aefeSPatrick Mooney 
8904c87aefeSPatrick Mooney 	if (ncq && first)
8914c87aefeSPatrick Mooney 		ahci_write_fis_d2h_ncq(p, slot);
8924c87aefeSPatrick Mooney 
8934c87aefeSPatrick Mooney 	err = blockif_delete(p->bctx, breq);
8944c87aefeSPatrick Mooney 	assert(err == 0);
8954c87aefeSPatrick Mooney }
8964c87aefeSPatrick Mooney 
8974c87aefeSPatrick Mooney static inline void
write_prdt(struct ahci_port * p,int slot,uint8_t * cfis,void * buf,unsigned int size)89859d65d31SAndy Fiddaman write_prdt(struct ahci_port *p, int slot, uint8_t *cfis, void *buf,
89959d65d31SAndy Fiddaman     unsigned int size)
900bf21cd93STycho Nightingale {
901bf21cd93STycho Nightingale 	struct ahci_cmd_hdr *hdr;
902bf21cd93STycho Nightingale 	struct ahci_prdt_entry *prdt;
90359d65d31SAndy Fiddaman 	uint8_t *from;
90459d65d31SAndy Fiddaman 	unsigned int len;
90559d65d31SAndy Fiddaman 	int i;
906bf21cd93STycho Nightingale 
907bf21cd93STycho Nightingale 	hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
908bf21cd93STycho Nightingale 	len = size;
909bf21cd93STycho Nightingale 	from = buf;
910bf21cd93STycho Nightingale 	prdt = (struct ahci_prdt_entry *)(cfis + 0x80);
911bf21cd93STycho Nightingale 	for (i = 0; i < hdr->prdtl && len; i++) {
912bf21cd93STycho Nightingale 		uint8_t *ptr;
913bf21cd93STycho Nightingale 		uint32_t dbcsz;
914bf21cd93STycho Nightingale 		int sublen;
915bf21cd93STycho Nightingale 
916bf21cd93STycho Nightingale 		dbcsz = (prdt->dbc & DBCMASK) + 1;
917bf21cd93STycho Nightingale 		ptr = paddr_guest2host(ahci_ctx(p->pr_sc), prdt->dba, dbcsz);
9184c87aefeSPatrick Mooney 		sublen = MIN(len, dbcsz);
919bf21cd93STycho Nightingale 		memcpy(ptr, from, sublen);
920bf21cd93STycho Nightingale 		len -= sublen;
921bf21cd93STycho Nightingale 		from += sublen;
922bf21cd93STycho Nightingale 		prdt++;
923bf21cd93STycho Nightingale 	}
924bf21cd93STycho Nightingale 	hdr->prdbc = size - len;
925bf21cd93STycho Nightingale }
926bf21cd93STycho Nightingale 
927bf21cd93STycho Nightingale static void
ahci_checksum(uint8_t * buf,int size)9284c87aefeSPatrick Mooney ahci_checksum(uint8_t *buf, int size)
9294c87aefeSPatrick Mooney {
9304c87aefeSPatrick Mooney 	int i;
9314c87aefeSPatrick Mooney 	uint8_t sum = 0;
9324c87aefeSPatrick Mooney 
9334c87aefeSPatrick Mooney 	for (i = 0; i < size - 1; i++)
9344c87aefeSPatrick Mooney 		sum += buf[i];
9354c87aefeSPatrick Mooney 	buf[size - 1] = 0x100 - sum;
9364c87aefeSPatrick Mooney }
9374c87aefeSPatrick Mooney 
9384c87aefeSPatrick Mooney static void
ahci_handle_read_log(struct ahci_port * p,int slot,uint8_t * cfis)9394c87aefeSPatrick Mooney ahci_handle_read_log(struct ahci_port *p, int slot, uint8_t *cfis)
9404c87aefeSPatrick Mooney {
9414c87aefeSPatrick Mooney 	struct ahci_cmd_hdr *hdr;
9424c87aefeSPatrick Mooney 	uint32_t buf[128];
9434c87aefeSPatrick Mooney 	uint8_t *buf8 = (uint8_t *)buf;
9444c87aefeSPatrick Mooney 	uint16_t *buf16 = (uint16_t *)buf;
9454c87aefeSPatrick Mooney 
9464c87aefeSPatrick Mooney 	hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
9474c87aefeSPatrick Mooney 	if (p->atapi || hdr->prdtl == 0 || cfis[5] != 0 ||
9484c87aefeSPatrick Mooney 	    cfis[9] != 0 || cfis[12] != 1 || cfis[13] != 0) {
9494c87aefeSPatrick Mooney 		ahci_write_fis_d2h(p, slot, cfis,
9504c87aefeSPatrick Mooney 		    (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
9514c87aefeSPatrick Mooney 		return;
9524c87aefeSPatrick Mooney 	}
9534c87aefeSPatrick Mooney 
9544c87aefeSPatrick Mooney 	memset(buf, 0, sizeof(buf));
9554c87aefeSPatrick Mooney 	if (cfis[4] == 0x00) {	/* Log directory */
9564c87aefeSPatrick Mooney 		buf16[0x00] = 1; /* Version -- 1 */
9574c87aefeSPatrick Mooney 		buf16[0x10] = 1; /* NCQ Command Error Log -- 1 page */
9584c87aefeSPatrick Mooney 		buf16[0x13] = 1; /* SATA NCQ Send and Receive Log -- 1 page */
9594c87aefeSPatrick Mooney 	} else if (cfis[4] == 0x10) {	/* NCQ Command Error Log */
9604c87aefeSPatrick Mooney 		memcpy(buf8, p->err_cfis, sizeof(p->err_cfis));
9614c87aefeSPatrick Mooney 		ahci_checksum(buf8, sizeof(buf));
9624c87aefeSPatrick Mooney 	} else if (cfis[4] == 0x13) {	/* SATA NCQ Send and Receive Log */
9634c87aefeSPatrick Mooney 		if (blockif_candelete(p->bctx) && !blockif_is_ro(p->bctx)) {
9644c87aefeSPatrick Mooney 			buf[0x00] = 1;	/* SFQ DSM supported */
9654c87aefeSPatrick Mooney 			buf[0x01] = 1;	/* SFQ DSM TRIM supported */
9664c87aefeSPatrick Mooney 		}
9674c87aefeSPatrick Mooney 	} else {
9684c87aefeSPatrick Mooney 		ahci_write_fis_d2h(p, slot, cfis,
9694c87aefeSPatrick Mooney 		    (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
9704c87aefeSPatrick Mooney 		return;
9714c87aefeSPatrick Mooney 	}
9724c87aefeSPatrick Mooney 
9734c87aefeSPatrick Mooney 	if (cfis[2] == ATA_READ_LOG_EXT)
9744c87aefeSPatrick Mooney 		ahci_write_fis_piosetup(p);
9754c87aefeSPatrick Mooney 	write_prdt(p, slot, cfis, (void *)buf, sizeof(buf));
9764c87aefeSPatrick Mooney 	ahci_write_fis_d2h(p, slot, cfis, ATA_S_DSC | ATA_S_READY);
9774c87aefeSPatrick Mooney }
9784c87aefeSPatrick Mooney 
9794c87aefeSPatrick Mooney static void
handle_identify(struct ahci_port * p,int slot,uint8_t * cfis)980bf21cd93STycho Nightingale handle_identify(struct ahci_port *p, int slot, uint8_t *cfis)
981bf21cd93STycho Nightingale {
982bf21cd93STycho Nightingale 	struct ahci_cmd_hdr *hdr;
983bf21cd93STycho Nightingale 
984bf21cd93STycho Nightingale 	hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
985bf21cd93STycho Nightingale 	if (p->atapi || hdr->prdtl == 0) {
9864c87aefeSPatrick Mooney 		ahci_write_fis_d2h(p, slot, cfis,
9874c87aefeSPatrick Mooney 		    (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
988bf21cd93STycho Nightingale 	} else {
9896960cd89SAndy Fiddaman 		ahci_write_fis_piosetup(p);
9906960cd89SAndy Fiddaman 		write_prdt(p, slot, cfis, (void*)&p->ata_ident, sizeof(struct ata_params));
9916960cd89SAndy Fiddaman 		ahci_write_fis_d2h(p, slot, cfis, ATA_S_DSC | ATA_S_READY);
9926960cd89SAndy Fiddaman 	}
9936960cd89SAndy Fiddaman }
9946960cd89SAndy Fiddaman 
9956960cd89SAndy Fiddaman static void
ata_identify_init(struct ahci_port * p,int atapi)9966960cd89SAndy Fiddaman ata_identify_init(struct ahci_port* p, int atapi)
9976960cd89SAndy Fiddaman {
9986960cd89SAndy Fiddaman 	struct ata_params* ata_ident = &p->ata_ident;
9996960cd89SAndy Fiddaman 
10006960cd89SAndy Fiddaman 	if (atapi) {
10016960cd89SAndy Fiddaman 		ata_ident->config = ATA_PROTO_ATAPI | ATA_ATAPI_TYPE_CDROM |
10026960cd89SAndy Fiddaman 		    ATA_ATAPI_REMOVABLE | ATA_DRQ_FAST;
10036960cd89SAndy Fiddaman 		ata_ident->capabilities1 = ATA_SUPPORT_LBA |
10046960cd89SAndy Fiddaman 			ATA_SUPPORT_DMA;
10056960cd89SAndy Fiddaman 		ata_ident->capabilities2 = (1 << 14 | 1);
10066960cd89SAndy Fiddaman 		ata_ident->atavalid = ATA_FLAG_64_70 | ATA_FLAG_88;
10076960cd89SAndy Fiddaman 		ata_ident->obsolete62 = 0x3f;
10086960cd89SAndy Fiddaman 		ata_ident->mwdmamodes = 7;
10096960cd89SAndy Fiddaman 		if (p->xfermode & ATA_WDMA0)
10106960cd89SAndy Fiddaman 			ata_ident->mwdmamodes |= (1 << ((p->xfermode & 7) + 8));
10116960cd89SAndy Fiddaman 		ata_ident->apiomodes = 3;
10126960cd89SAndy Fiddaman 		ata_ident->mwdmamin = 0x0078;
10136960cd89SAndy Fiddaman 		ata_ident->mwdmarec = 0x0078;
10146960cd89SAndy Fiddaman 		ata_ident->pioblind = 0x0078;
10156960cd89SAndy Fiddaman 		ata_ident->pioiordy = 0x0078;
10166960cd89SAndy Fiddaman 		ata_ident->satacapabilities = (ATA_SATA_GEN1 | ATA_SATA_GEN2 | ATA_SATA_GEN3);
10176960cd89SAndy Fiddaman 		ata_ident->satacapabilities2 = ((p->ssts & ATA_SS_SPD_MASK) >> 3);
10186960cd89SAndy Fiddaman 		ata_ident->satasupport = ATA_SUPPORT_NCQ_STREAM;
10196960cd89SAndy Fiddaman 		ata_ident->version_major = 0x3f0;
10206960cd89SAndy Fiddaman 		ata_ident->support.command1 = (ATA_SUPPORT_POWERMGT | ATA_SUPPORT_PACKET |
10216960cd89SAndy Fiddaman 			ATA_SUPPORT_RESET | ATA_SUPPORT_NOP);
10226960cd89SAndy Fiddaman 		ata_ident->support.command2 = (1 << 14);
10236960cd89SAndy Fiddaman 		ata_ident->support.extension = (1 << 14);
10246960cd89SAndy Fiddaman 		ata_ident->enabled.command1 = (ATA_SUPPORT_POWERMGT | ATA_SUPPORT_PACKET |
10256960cd89SAndy Fiddaman 			ATA_SUPPORT_RESET | ATA_SUPPORT_NOP);
10266960cd89SAndy Fiddaman 		ata_ident->enabled.extension = (1 << 14);
10276960cd89SAndy Fiddaman 		ata_ident->udmamodes = 0x7f;
10286960cd89SAndy Fiddaman 		if (p->xfermode & ATA_UDMA0)
10296960cd89SAndy Fiddaman 			ata_ident->udmamodes |= (1 << ((p->xfermode & 7) + 8));
10306960cd89SAndy Fiddaman 		ata_ident->transport_major = 0x1020;
10316960cd89SAndy Fiddaman 		ata_ident->integrity = 0x00a5;
10326960cd89SAndy Fiddaman 	} else {
1033bf21cd93STycho Nightingale 		uint64_t sectors;
10344c87aefeSPatrick Mooney 		int sectsz, psectsz, psectoff, candelete, ro;
1035bf21cd93STycho Nightingale 		uint16_t cyl;
1036bf21cd93STycho Nightingale 		uint8_t sech, heads;
1037bf21cd93STycho Nightingale 
10384c87aefeSPatrick Mooney 		ro = blockif_is_ro(p->bctx);
10394c87aefeSPatrick Mooney 		candelete = blockif_candelete(p->bctx);
10404c87aefeSPatrick Mooney 		sectsz = blockif_sectsz(p->bctx);
10414c87aefeSPatrick Mooney 		sectors = blockif_size(p->bctx) / sectsz;
1042bf21cd93STycho Nightingale 		blockif_chs(p->bctx, &cyl, &heads, &sech);
10434c87aefeSPatrick Mooney 		blockif_psectsz(p->bctx, &psectsz, &psectoff);
10446960cd89SAndy Fiddaman 		ata_ident->config = ATA_DRQ_FAST;
10456960cd89SAndy Fiddaman 		ata_ident->cylinders = cyl;
10466960cd89SAndy Fiddaman 		ata_ident->heads = heads;
10476960cd89SAndy Fiddaman 		ata_ident->sectors = sech;
10486960cd89SAndy Fiddaman 
10496960cd89SAndy Fiddaman 		ata_ident->sectors_intr = (0x8000 | 128);
10506960cd89SAndy Fiddaman 		ata_ident->tcg = 0;
10516960cd89SAndy Fiddaman 
10526960cd89SAndy Fiddaman 		ata_ident->capabilities1 = ATA_SUPPORT_DMA |
10536960cd89SAndy Fiddaman 			ATA_SUPPORT_LBA | ATA_SUPPORT_IORDY;
10546960cd89SAndy Fiddaman 		ata_ident->capabilities2 = (1 << 14);
10556960cd89SAndy Fiddaman 		ata_ident->atavalid = ATA_FLAG_64_70 | ATA_FLAG_88;
1056bf21cd93STycho Nightingale 		if (p->mult_sectors)
10576960cd89SAndy Fiddaman 			ata_ident->multi = (ATA_MULTI_VALID | p->mult_sectors);
10584c87aefeSPatrick Mooney 		if (sectors <= 0x0fffffff) {
10596960cd89SAndy Fiddaman 			ata_ident->lba_size_1 = sectors;
10606960cd89SAndy Fiddaman 			ata_ident->lba_size_2 = (sectors >> 16);
10614c87aefeSPatrick Mooney 		} else {
10626960cd89SAndy Fiddaman 			ata_ident->lba_size_1 = 0xffff;
10636960cd89SAndy Fiddaman 			ata_ident->lba_size_2 = 0x0fff;
10644c87aefeSPatrick Mooney 		}
10656960cd89SAndy Fiddaman 		ata_ident->mwdmamodes = 0x7;
1066bf21cd93STycho Nightingale 		if (p->xfermode & ATA_WDMA0)
10676960cd89SAndy Fiddaman 			ata_ident->mwdmamodes |= (1 << ((p->xfermode & 7) + 8));
10686960cd89SAndy Fiddaman 		ata_ident->apiomodes = 0x3;
10696960cd89SAndy Fiddaman 		ata_ident->mwdmamin = 0x0078;
10706960cd89SAndy Fiddaman 		ata_ident->mwdmarec = 0x0078;
10716960cd89SAndy Fiddaman 		ata_ident->pioblind = 0x0078;
10726960cd89SAndy Fiddaman 		ata_ident->pioiordy = 0x0078;
10736960cd89SAndy Fiddaman 		ata_ident->support3 = 0;
10746960cd89SAndy Fiddaman 		ata_ident->queue = 31;
10756960cd89SAndy Fiddaman 		ata_ident->satacapabilities = (ATA_SATA_GEN1 | ATA_SATA_GEN2 | ATA_SATA_GEN3 |
10764c87aefeSPatrick Mooney 			ATA_SUPPORT_NCQ);
10776960cd89SAndy Fiddaman 		ata_ident->satacapabilities2 = (ATA_SUPPORT_RCVSND_FPDMA_QUEUED |
10784c87aefeSPatrick Mooney 			(p->ssts & ATA_SS_SPD_MASK) >> 3);
10796960cd89SAndy Fiddaman 		ata_ident->version_major = 0x3f0;
10806960cd89SAndy Fiddaman 		ata_ident->version_minor = 0x28;
10816960cd89SAndy Fiddaman 		ata_ident->support.command1 = (ATA_SUPPORT_POWERMGT | ATA_SUPPORT_WRITECACHE |
10824c87aefeSPatrick Mooney 			ATA_SUPPORT_LOOKAHEAD | ATA_SUPPORT_NOP);
10836960cd89SAndy Fiddaman 		ata_ident->support.command2 = (ATA_SUPPORT_ADDRESS48 | ATA_SUPPORT_FLUSHCACHE |
10844c87aefeSPatrick Mooney 			ATA_SUPPORT_FLUSHCACHE48 | 1 << 14);
10856960cd89SAndy Fiddaman 		ata_ident->support.extension = (1 << 14);
10866960cd89SAndy Fiddaman 		ata_ident->enabled.command1 = (ATA_SUPPORT_POWERMGT | ATA_SUPPORT_WRITECACHE |
10874c87aefeSPatrick Mooney 			ATA_SUPPORT_LOOKAHEAD | ATA_SUPPORT_NOP);
10886960cd89SAndy Fiddaman 		ata_ident->enabled.command2 = (ATA_SUPPORT_ADDRESS48 | ATA_SUPPORT_FLUSHCACHE |
10894c87aefeSPatrick Mooney 			ATA_SUPPORT_FLUSHCACHE48 | 1 << 15);
10906960cd89SAndy Fiddaman 		ata_ident->enabled.extension = (1 << 14);
10916960cd89SAndy Fiddaman 		ata_ident->udmamodes = 0x7f;
1092bf21cd93STycho Nightingale 		if (p->xfermode & ATA_UDMA0)
10936960cd89SAndy Fiddaman 			ata_ident->udmamodes |= (1 << ((p->xfermode & 7) + 8));
10946960cd89SAndy Fiddaman 		ata_ident->lba_size48_1 = sectors;
10956960cd89SAndy Fiddaman 		ata_ident->lba_size48_2 = (sectors >> 16);
10966960cd89SAndy Fiddaman 		ata_ident->lba_size48_3 = (sectors >> 32);
10976960cd89SAndy Fiddaman 		ata_ident->lba_size48_4 = (sectors >> 48);
10986960cd89SAndy Fiddaman 
10994c87aefeSPatrick Mooney 		if (candelete && !ro) {
11006960cd89SAndy Fiddaman 			ata_ident->support3 |= ATA_SUPPORT_RZAT | ATA_SUPPORT_DRAT;
11016960cd89SAndy Fiddaman 			ata_ident->max_dsm_blocks = 1;
11026960cd89SAndy Fiddaman 			ata_ident->support_dsm = ATA_SUPPORT_DSM_TRIM;
11034c87aefeSPatrick Mooney 		}
11046960cd89SAndy Fiddaman 		ata_ident->pss = ATA_PSS_VALID_VALUE;
11056960cd89SAndy Fiddaman 		ata_ident->lsalign = 0x4000;
11064c87aefeSPatrick Mooney 		if (psectsz > sectsz) {
11076960cd89SAndy Fiddaman 			ata_ident->pss |= ATA_PSS_MULTLS;
11086960cd89SAndy Fiddaman 			ata_ident->pss |= ffsl(psectsz / sectsz) - 1;
11096960cd89SAndy Fiddaman 			ata_ident->lsalign |= (psectoff / sectsz);
11104c87aefeSPatrick Mooney 		}
11114c87aefeSPatrick Mooney 		if (sectsz > 512) {
11126960cd89SAndy Fiddaman 			ata_ident->pss |= ATA_PSS_LSSABOVE512;
11136960cd89SAndy Fiddaman 			ata_ident->lss_1 = sectsz / 2;
11146960cd89SAndy Fiddaman 			ata_ident->lss_2 = ((sectsz / 2) >> 16);
11154c87aefeSPatrick Mooney 		}
11166960cd89SAndy Fiddaman 		ata_ident->support2 = (ATA_SUPPORT_RWLOGDMAEXT | 1 << 14);
11176960cd89SAndy Fiddaman 		ata_ident->enabled2 = (ATA_SUPPORT_RWLOGDMAEXT | 1 << 14);
11186960cd89SAndy Fiddaman 		ata_ident->transport_major = 0x1020;
11196960cd89SAndy Fiddaman 		ata_ident->integrity = 0x00a5;
1120bf21cd93STycho Nightingale 	}
11216960cd89SAndy Fiddaman 	ahci_checksum((uint8_t*)ata_ident, sizeof(struct ata_params));
1122bf21cd93STycho Nightingale }
1123bf21cd93STycho Nightingale 
1124bf21cd93STycho Nightingale static void
handle_atapi_identify(struct ahci_port * p,int slot,uint8_t * cfis)1125bf21cd93STycho Nightingale handle_atapi_identify(struct ahci_port *p, int slot, uint8_t *cfis)
1126bf21cd93STycho Nightingale {
1127bf21cd93STycho Nightingale 	if (!p->atapi) {
11284c87aefeSPatrick Mooney 		ahci_write_fis_d2h(p, slot, cfis,
11294c87aefeSPatrick Mooney 		    (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
1130bf21cd93STycho Nightingale 	} else {
1131bf21cd93STycho Nightingale 		ahci_write_fis_piosetup(p);
11326960cd89SAndy Fiddaman 		write_prdt(p, slot, cfis, (void *)&p->ata_ident, sizeof(struct ata_params));
11334c87aefeSPatrick Mooney 		ahci_write_fis_d2h(p, slot, cfis, ATA_S_DSC | ATA_S_READY);
1134bf21cd93STycho Nightingale 	}
1135bf21cd93STycho Nightingale }
1136bf21cd93STycho Nightingale 
1137bf21cd93STycho Nightingale static void
atapi_inquiry(struct ahci_port * p,int slot,uint8_t * cfis)1138bf21cd93STycho Nightingale atapi_inquiry(struct ahci_port *p, int slot, uint8_t *cfis)
1139bf21cd93STycho Nightingale {
1140bf21cd93STycho Nightingale 	uint8_t buf[36];
1141bf21cd93STycho Nightingale 	uint8_t *acmd;
114259d65d31SAndy Fiddaman 	unsigned int len;
11434c87aefeSPatrick Mooney 	uint32_t tfd;
1144bf21cd93STycho Nightingale 
1145bf21cd93STycho Nightingale 	acmd = cfis + 0x40;
1146bf21cd93STycho Nightingale 
11474c87aefeSPatrick Mooney 	if (acmd[1] & 1) {		/* VPD */
11484c87aefeSPatrick Mooney 		if (acmd[2] == 0) {	/* Supported VPD pages */
11494c87aefeSPatrick Mooney 			buf[0] = 0x05;
11504c87aefeSPatrick Mooney 			buf[1] = 0;
11514c87aefeSPatrick Mooney 			buf[2] = 0;
11524c87aefeSPatrick Mooney 			buf[3] = 1;
11534c87aefeSPatrick Mooney 			buf[4] = 0;
11544c87aefeSPatrick Mooney 			len = 4 + buf[3];
11554c87aefeSPatrick Mooney 		} else {
11564c87aefeSPatrick Mooney 			p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
11574c87aefeSPatrick Mooney 			p->asc = 0x24;
11584c87aefeSPatrick Mooney 			tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
11594c87aefeSPatrick Mooney 			cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
11604c87aefeSPatrick Mooney 			ahci_write_fis_d2h(p, slot, cfis, tfd);
11614c87aefeSPatrick Mooney 			return;
11624c87aefeSPatrick Mooney 		}
11634c87aefeSPatrick Mooney 	} else {
1164bf21cd93STycho Nightingale 		buf[0] = 0x05;
1165bf21cd93STycho Nightingale 		buf[1] = 0x80;
1166bf21cd93STycho Nightingale 		buf[2] = 0x00;
1167bf21cd93STycho Nightingale 		buf[3] = 0x21;
1168bf21cd93STycho Nightingale 		buf[4] = 31;
1169bf21cd93STycho Nightingale 		buf[5] = 0;
1170bf21cd93STycho Nightingale 		buf[6] = 0;
1171bf21cd93STycho Nightingale 		buf[7] = 0;
1172bf21cd93STycho Nightingale 		atapi_string(buf + 8, "BHYVE", 8);
1173bf21cd93STycho Nightingale 		atapi_string(buf + 16, "BHYVE DVD-ROM", 16);
1174bf21cd93STycho Nightingale 		atapi_string(buf + 32, "001", 4);
1175bf21cd93STycho Nightingale 		len = sizeof(buf);
11764c87aefeSPatrick Mooney 	}
11774c87aefeSPatrick Mooney 
1178bf21cd93STycho Nightingale 	if (len > acmd[4])
1179bf21cd93STycho Nightingale 		len = acmd[4];
1180bf21cd93STycho Nightingale 	cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1181bf21cd93STycho Nightingale 	write_prdt(p, slot, cfis, buf, len);
1182bf21cd93STycho Nightingale 	ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1183bf21cd93STycho Nightingale }
1184bf21cd93STycho Nightingale 
1185bf21cd93STycho Nightingale static void
atapi_read_capacity(struct ahci_port * p,int slot,uint8_t * cfis)1186bf21cd93STycho Nightingale atapi_read_capacity(struct ahci_port *p, int slot, uint8_t *cfis)
1187bf21cd93STycho Nightingale {
1188bf21cd93STycho Nightingale 	uint8_t buf[8];
1189bf21cd93STycho Nightingale 	uint64_t sectors;
1190bf21cd93STycho Nightingale 
1191bf21cd93STycho Nightingale 	sectors = blockif_size(p->bctx) / 2048;
1192bf21cd93STycho Nightingale 	be32enc(buf, sectors - 1);
1193bf21cd93STycho Nightingale 	be32enc(buf + 4, 2048);
1194bf21cd93STycho Nightingale 	cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1195bf21cd93STycho Nightingale 	write_prdt(p, slot, cfis, buf, sizeof(buf));
1196bf21cd93STycho Nightingale 	ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1197bf21cd93STycho Nightingale }
1198bf21cd93STycho Nightingale 
1199bf21cd93STycho Nightingale static void
atapi_read_toc(struct ahci_port * p,int slot,uint8_t * cfis)1200bf21cd93STycho Nightingale atapi_read_toc(struct ahci_port *p, int slot, uint8_t *cfis)
1201bf21cd93STycho Nightingale {
1202bf21cd93STycho Nightingale 	uint8_t *acmd;
1203bf21cd93STycho Nightingale 	uint8_t format;
120459d65d31SAndy Fiddaman 	unsigned int len;
1205bf21cd93STycho Nightingale 
1206bf21cd93STycho Nightingale 	acmd = cfis + 0x40;
1207bf21cd93STycho Nightingale 
1208bf21cd93STycho Nightingale 	len = be16dec(acmd + 7);
1209bf21cd93STycho Nightingale 	format = acmd[9] >> 6;
1210bf21cd93STycho Nightingale 	switch (format) {
1211bf21cd93STycho Nightingale 	case 0:
1212bf21cd93STycho Nightingale 	{
121359d65d31SAndy Fiddaman 		size_t size;
121459d65d31SAndy Fiddaman 		int msf;
1215bf21cd93STycho Nightingale 		uint64_t sectors;
1216bf21cd93STycho Nightingale 		uint8_t start_track, buf[20], *bp;
1217bf21cd93STycho Nightingale 
1218bf21cd93STycho Nightingale 		msf = (acmd[1] >> 1) & 1;
1219bf21cd93STycho Nightingale 		start_track = acmd[6];
1220bf21cd93STycho Nightingale 		if (start_track > 1 && start_track != 0xaa) {
1221bf21cd93STycho Nightingale 			uint32_t tfd;
1222bf21cd93STycho Nightingale 			p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1223bf21cd93STycho Nightingale 			p->asc = 0x24;
1224bf21cd93STycho Nightingale 			tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1225bf21cd93STycho Nightingale 			cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1226bf21cd93STycho Nightingale 			ahci_write_fis_d2h(p, slot, cfis, tfd);
1227bf21cd93STycho Nightingale 			return;
1228bf21cd93STycho Nightingale 		}
1229bf21cd93STycho Nightingale 		bp = buf + 2;
1230bf21cd93STycho Nightingale 		*bp++ = 1;
1231bf21cd93STycho Nightingale 		*bp++ = 1;
1232bf21cd93STycho Nightingale 		if (start_track <= 1) {
1233bf21cd93STycho Nightingale 			*bp++ = 0;
1234bf21cd93STycho Nightingale 			*bp++ = 0x14;
1235bf21cd93STycho Nightingale 			*bp++ = 1;
1236bf21cd93STycho Nightingale 			*bp++ = 0;
1237bf21cd93STycho Nightingale 			if (msf) {
1238bf21cd93STycho Nightingale 				*bp++ = 0;
1239bf21cd93STycho Nightingale 				lba_to_msf(bp, 0);
1240bf21cd93STycho Nightingale 				bp += 3;
1241bf21cd93STycho Nightingale 			} else {
1242bf21cd93STycho Nightingale 				*bp++ = 0;
1243bf21cd93STycho Nightingale 				*bp++ = 0;
1244bf21cd93STycho Nightingale 				*bp++ = 0;
1245bf21cd93STycho Nightingale 				*bp++ = 0;
1246bf21cd93STycho Nightingale 			}
1247bf21cd93STycho Nightingale 		}
1248bf21cd93STycho Nightingale 		*bp++ = 0;
1249bf21cd93STycho Nightingale 		*bp++ = 0x14;
1250bf21cd93STycho Nightingale 		*bp++ = 0xaa;
1251bf21cd93STycho Nightingale 		*bp++ = 0;
1252bf21cd93STycho Nightingale 		sectors = blockif_size(p->bctx) / blockif_sectsz(p->bctx);
1253bf21cd93STycho Nightingale 		sectors >>= 2;
1254bf21cd93STycho Nightingale 		if (msf) {
1255bf21cd93STycho Nightingale 			*bp++ = 0;
1256bf21cd93STycho Nightingale 			lba_to_msf(bp, sectors);
1257bf21cd93STycho Nightingale 			bp += 3;
1258bf21cd93STycho Nightingale 		} else {
1259bf21cd93STycho Nightingale 			be32enc(bp, sectors);
1260bf21cd93STycho Nightingale 			bp += 4;
1261bf21cd93STycho Nightingale 		}
1262bf21cd93STycho Nightingale 		size = bp - buf;
1263bf21cd93STycho Nightingale 		be16enc(buf, size - 2);
1264bf21cd93STycho Nightingale 		if (len > size)
1265bf21cd93STycho Nightingale 			len = size;
1266bf21cd93STycho Nightingale 		write_prdt(p, slot, cfis, buf, len);
1267bf21cd93STycho Nightingale 		cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1268bf21cd93STycho Nightingale 		ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1269bf21cd93STycho Nightingale 		break;
1270bf21cd93STycho Nightingale 	}
1271bf21cd93STycho Nightingale 	case 1:
1272bf21cd93STycho Nightingale 	{
1273bf21cd93STycho Nightingale 		uint8_t buf[12];
1274bf21cd93STycho Nightingale 
1275bf21cd93STycho Nightingale 		memset(buf, 0, sizeof(buf));
1276bf21cd93STycho Nightingale 		buf[1] = 0xa;
1277bf21cd93STycho Nightingale 		buf[2] = 0x1;
1278bf21cd93STycho Nightingale 		buf[3] = 0x1;
1279bf21cd93STycho Nightingale 		if (len > sizeof(buf))
1280bf21cd93STycho Nightingale 			len = sizeof(buf);
1281bf21cd93STycho Nightingale 		write_prdt(p, slot, cfis, buf, len);
1282bf21cd93STycho Nightingale 		cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1283bf21cd93STycho Nightingale 		ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1284bf21cd93STycho Nightingale 		break;
1285bf21cd93STycho Nightingale 	}
1286bf21cd93STycho Nightingale 	case 2:
1287bf21cd93STycho Nightingale 	{
128859d65d31SAndy Fiddaman 		size_t size;
128959d65d31SAndy Fiddaman 		int msf;
1290bf21cd93STycho Nightingale 		uint64_t sectors;
12914c87aefeSPatrick Mooney 		uint8_t *bp, buf[50];
1292bf21cd93STycho Nightingale 
1293bf21cd93STycho Nightingale 		msf = (acmd[1] >> 1) & 1;
1294bf21cd93STycho Nightingale 		bp = buf + 2;
1295bf21cd93STycho Nightingale 		*bp++ = 1;
1296bf21cd93STycho Nightingale 		*bp++ = 1;
1297bf21cd93STycho Nightingale 
1298bf21cd93STycho Nightingale 		*bp++ = 1;
1299bf21cd93STycho Nightingale 		*bp++ = 0x14;
1300bf21cd93STycho Nightingale 		*bp++ = 0;
1301bf21cd93STycho Nightingale 		*bp++ = 0xa0;
1302bf21cd93STycho Nightingale 		*bp++ = 0;
1303bf21cd93STycho Nightingale 		*bp++ = 0;
1304bf21cd93STycho Nightingale 		*bp++ = 0;
1305bf21cd93STycho Nightingale 		*bp++ = 0;
1306bf21cd93STycho Nightingale 		*bp++ = 1;
1307bf21cd93STycho Nightingale 		*bp++ = 0;
1308bf21cd93STycho Nightingale 		*bp++ = 0;
1309bf21cd93STycho Nightingale 
1310bf21cd93STycho Nightingale 		*bp++ = 1;
1311bf21cd93STycho Nightingale 		*bp++ = 0x14;
1312bf21cd93STycho Nightingale 		*bp++ = 0;
1313bf21cd93STycho Nightingale 		*bp++ = 0xa1;
1314bf21cd93STycho Nightingale 		*bp++ = 0;
1315bf21cd93STycho Nightingale 		*bp++ = 0;
1316bf21cd93STycho Nightingale 		*bp++ = 0;
1317bf21cd93STycho Nightingale 		*bp++ = 0;
1318bf21cd93STycho Nightingale 		*bp++ = 1;
1319bf21cd93STycho Nightingale 		*bp++ = 0;
1320bf21cd93STycho Nightingale 		*bp++ = 0;
1321bf21cd93STycho Nightingale 
1322bf21cd93STycho Nightingale 		*bp++ = 1;
1323bf21cd93STycho Nightingale 		*bp++ = 0x14;
1324bf21cd93STycho Nightingale 		*bp++ = 0;
1325bf21cd93STycho Nightingale 		*bp++ = 0xa2;
1326bf21cd93STycho Nightingale 		*bp++ = 0;
1327bf21cd93STycho Nightingale 		*bp++ = 0;
1328bf21cd93STycho Nightingale 		*bp++ = 0;
1329bf21cd93STycho Nightingale 		sectors = blockif_size(p->bctx) / blockif_sectsz(p->bctx);
1330bf21cd93STycho Nightingale 		sectors >>= 2;
1331bf21cd93STycho Nightingale 		if (msf) {
1332bf21cd93STycho Nightingale 			*bp++ = 0;
1333bf21cd93STycho Nightingale 			lba_to_msf(bp, sectors);
1334bf21cd93STycho Nightingale 			bp += 3;
1335bf21cd93STycho Nightingale 		} else {
1336bf21cd93STycho Nightingale 			be32enc(bp, sectors);
1337bf21cd93STycho Nightingale 			bp += 4;
1338bf21cd93STycho Nightingale 		}
1339bf21cd93STycho Nightingale 
1340bf21cd93STycho Nightingale 		*bp++ = 1;
1341bf21cd93STycho Nightingale 		*bp++ = 0x14;
1342bf21cd93STycho Nightingale 		*bp++ = 0;
1343bf21cd93STycho Nightingale 		*bp++ = 1;
1344bf21cd93STycho Nightingale 		*bp++ = 0;
1345bf21cd93STycho Nightingale 		*bp++ = 0;
1346bf21cd93STycho Nightingale 		*bp++ = 0;
1347bf21cd93STycho Nightingale 		if (msf) {
1348bf21cd93STycho Nightingale 			*bp++ = 0;
1349bf21cd93STycho Nightingale 			lba_to_msf(bp, 0);
1350bf21cd93STycho Nightingale 			bp += 3;
1351bf21cd93STycho Nightingale 		} else {
1352bf21cd93STycho Nightingale 			*bp++ = 0;
1353bf21cd93STycho Nightingale 			*bp++ = 0;
1354bf21cd93STycho Nightingale 			*bp++ = 0;
1355bf21cd93STycho Nightingale 			*bp++ = 0;
1356bf21cd93STycho Nightingale 		}
1357bf21cd93STycho Nightingale 
1358bf21cd93STycho Nightingale 		size = bp - buf;
1359bf21cd93STycho Nightingale 		be16enc(buf, size - 2);
1360bf21cd93STycho Nightingale 		if (len > size)
1361bf21cd93STycho Nightingale 			len = size;
1362bf21cd93STycho Nightingale 		write_prdt(p, slot, cfis, buf, len);
1363bf21cd93STycho Nightingale 		cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1364bf21cd93STycho Nightingale 		ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1365bf21cd93STycho Nightingale 		break;
1366bf21cd93STycho Nightingale 	}
1367bf21cd93STycho Nightingale 	default:
1368bf21cd93STycho Nightingale 	{
1369bf21cd93STycho Nightingale 		uint32_t tfd;
1370bf21cd93STycho Nightingale 
1371bf21cd93STycho Nightingale 		p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1372bf21cd93STycho Nightingale 		p->asc = 0x24;
1373bf21cd93STycho Nightingale 		tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1374bf21cd93STycho Nightingale 		cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1375bf21cd93STycho Nightingale 		ahci_write_fis_d2h(p, slot, cfis, tfd);
1376bf21cd93STycho Nightingale 		break;
1377bf21cd93STycho Nightingale 	}
1378bf21cd93STycho Nightingale 	}
1379bf21cd93STycho Nightingale }
1380bf21cd93STycho Nightingale 
1381bf21cd93STycho Nightingale static void
atapi_report_luns(struct ahci_port * p,int slot,uint8_t * cfis)13824c87aefeSPatrick Mooney atapi_report_luns(struct ahci_port *p, int slot, uint8_t *cfis)
13834c87aefeSPatrick Mooney {
13844c87aefeSPatrick Mooney 	uint8_t buf[16];
13854c87aefeSPatrick Mooney 
13864c87aefeSPatrick Mooney 	memset(buf, 0, sizeof(buf));
13874c87aefeSPatrick Mooney 	buf[3] = 8;
13884c87aefeSPatrick Mooney 
13894c87aefeSPatrick Mooney 	cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
13904c87aefeSPatrick Mooney 	write_prdt(p, slot, cfis, buf, sizeof(buf));
13914c87aefeSPatrick Mooney 	ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
13924c87aefeSPatrick Mooney }
13934c87aefeSPatrick Mooney 
13944c87aefeSPatrick Mooney static void
atapi_read(struct ahci_port * p,int slot,uint8_t * cfis,uint32_t done)13954c87aefeSPatrick Mooney atapi_read(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t done)
1396bf21cd93STycho Nightingale {
1397bf21cd93STycho Nightingale 	struct ahci_ioreq *aior;
1398bf21cd93STycho Nightingale 	struct ahci_cmd_hdr *hdr;
1399bf21cd93STycho Nightingale 	struct ahci_prdt_entry *prdt;
1400bf21cd93STycho Nightingale 	struct blockif_req *breq;
1401bf21cd93STycho Nightingale 	uint8_t *acmd;
1402bf21cd93STycho Nightingale 	uint64_t lba;
1403bf21cd93STycho Nightingale 	uint32_t len;
14044c87aefeSPatrick Mooney 	int err;
1405bf21cd93STycho Nightingale 
1406bf21cd93STycho Nightingale 	acmd = cfis + 0x40;
1407bf21cd93STycho Nightingale 	hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
1408bf21cd93STycho Nightingale 	prdt = (struct ahci_prdt_entry *)(cfis + 0x80);
1409bf21cd93STycho Nightingale 
1410bf21cd93STycho Nightingale 	lba = be32dec(acmd + 2);
1411bf21cd93STycho Nightingale 	if (acmd[0] == READ_10)
1412bf21cd93STycho Nightingale 		len = be16dec(acmd + 7);
1413bf21cd93STycho Nightingale 	else
1414bf21cd93STycho Nightingale 		len = be32dec(acmd + 6);
1415bf21cd93STycho Nightingale 	if (len == 0) {
1416bf21cd93STycho Nightingale 		cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1417bf21cd93STycho Nightingale 		ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1418bf21cd93STycho Nightingale 	}
1419bf21cd93STycho Nightingale 	lba *= 2048;
1420bf21cd93STycho Nightingale 	len *= 2048;
1421bf21cd93STycho Nightingale 
1422bf21cd93STycho Nightingale 	/*
1423bf21cd93STycho Nightingale 	 * Pull request off free list
1424bf21cd93STycho Nightingale 	 */
1425bf21cd93STycho Nightingale 	aior = STAILQ_FIRST(&p->iofhd);
1426bf21cd93STycho Nightingale 	assert(aior != NULL);
1427bf21cd93STycho Nightingale 	STAILQ_REMOVE_HEAD(&p->iofhd, io_flist);
1428bf21cd93STycho Nightingale 	aior->cfis = cfis;
1429bf21cd93STycho Nightingale 	aior->slot = slot;
1430bf21cd93STycho Nightingale 	aior->len = len;
1431bf21cd93STycho Nightingale 	aior->done = done;
1432bf21cd93STycho Nightingale 	breq = &aior->io_req;
1433bf21cd93STycho Nightingale 	breq->br_offset = lba + done;
14344c87aefeSPatrick Mooney 	ahci_build_iov(p, aior, prdt, hdr->prdtl);
1435bf21cd93STycho Nightingale 
14364c87aefeSPatrick Mooney 	/* Mark this command in-flight. */
1437bf21cd93STycho Nightingale 	p->pending |= 1 << slot;
1438bf21cd93STycho Nightingale 
14394c87aefeSPatrick Mooney 	/* Stuff request onto busy list. */
1440bf21cd93STycho Nightingale 	TAILQ_INSERT_HEAD(&p->iobhd, aior, io_blist);
1441bf21cd93STycho Nightingale 
1442bf21cd93STycho Nightingale 	err = blockif_read(p->bctx, breq);
1443bf21cd93STycho Nightingale 	assert(err == 0);
1444bf21cd93STycho Nightingale }
1445bf21cd93STycho Nightingale 
1446bf21cd93STycho Nightingale static void
atapi_request_sense(struct ahci_port * p,int slot,uint8_t * cfis)1447bf21cd93STycho Nightingale atapi_request_sense(struct ahci_port *p, int slot, uint8_t *cfis)
1448bf21cd93STycho Nightingale {
1449bf21cd93STycho Nightingale 	uint8_t buf[64];
1450bf21cd93STycho Nightingale 	uint8_t *acmd;
145159d65d31SAndy Fiddaman 	unsigned int len;
1452bf21cd93STycho Nightingale 
1453bf21cd93STycho Nightingale 	acmd = cfis + 0x40;
1454bf21cd93STycho Nightingale 	len = acmd[4];
1455bf21cd93STycho Nightingale 	if (len > sizeof(buf))
1456bf21cd93STycho Nightingale 		len = sizeof(buf);
1457bf21cd93STycho Nightingale 	memset(buf, 0, len);
1458bf21cd93STycho Nightingale 	buf[0] = 0x70 | (1 << 7);
1459bf21cd93STycho Nightingale 	buf[2] = p->sense_key;
1460bf21cd93STycho Nightingale 	buf[7] = 10;
1461bf21cd93STycho Nightingale 	buf[12] = p->asc;
1462bf21cd93STycho Nightingale 	write_prdt(p, slot, cfis, buf, len);
1463bf21cd93STycho Nightingale 	cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1464bf21cd93STycho Nightingale 	ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1465bf21cd93STycho Nightingale }
1466bf21cd93STycho Nightingale 
1467bf21cd93STycho Nightingale static void
atapi_start_stop_unit(struct ahci_port * p,int slot,uint8_t * cfis)1468bf21cd93STycho Nightingale atapi_start_stop_unit(struct ahci_port *p, int slot, uint8_t *cfis)
1469bf21cd93STycho Nightingale {
1470bf21cd93STycho Nightingale 	uint8_t *acmd = cfis + 0x40;
1471bf21cd93STycho Nightingale 	uint32_t tfd;
1472bf21cd93STycho Nightingale 
1473bf21cd93STycho Nightingale 	switch (acmd[4] & 3) {
1474bf21cd93STycho Nightingale 	case 0:
1475bf21cd93STycho Nightingale 	case 1:
1476bf21cd93STycho Nightingale 	case 3:
1477bf21cd93STycho Nightingale 		cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1478bf21cd93STycho Nightingale 		tfd = ATA_S_READY | ATA_S_DSC;
1479bf21cd93STycho Nightingale 		break;
1480bf21cd93STycho Nightingale 	case 2:
1481bf21cd93STycho Nightingale 		/* TODO eject media */
1482bf21cd93STycho Nightingale 		cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1483bf21cd93STycho Nightingale 		p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1484bf21cd93STycho Nightingale 		p->asc = 0x53;
1485bf21cd93STycho Nightingale 		tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1486bf21cd93STycho Nightingale 		break;
1487bf21cd93STycho Nightingale 	}
1488bf21cd93STycho Nightingale 	ahci_write_fis_d2h(p, slot, cfis, tfd);
1489bf21cd93STycho Nightingale }
1490bf21cd93STycho Nightingale 
1491bf21cd93STycho Nightingale static void
atapi_mode_sense(struct ahci_port * p,int slot,uint8_t * cfis)1492bf21cd93STycho Nightingale atapi_mode_sense(struct ahci_port *p, int slot, uint8_t *cfis)
1493bf21cd93STycho Nightingale {
1494bf21cd93STycho Nightingale 	uint8_t *acmd;
14954c87aefeSPatrick Mooney 	uint32_t tfd = 0;
1496bf21cd93STycho Nightingale 	uint8_t pc, code;
149759d65d31SAndy Fiddaman 	unsigned int len;
1498bf21cd93STycho Nightingale 
1499bf21cd93STycho Nightingale 	acmd = cfis + 0x40;
1500bf21cd93STycho Nightingale 	len = be16dec(acmd + 7);
1501bf21cd93STycho Nightingale 	pc = acmd[2] >> 6;
1502bf21cd93STycho Nightingale 	code = acmd[2] & 0x3f;
1503bf21cd93STycho Nightingale 
1504bf21cd93STycho Nightingale 	switch (pc) {
1505bf21cd93STycho Nightingale 	case 0:
1506bf21cd93STycho Nightingale 		switch (code) {
1507bf21cd93STycho Nightingale 		case MODEPAGE_RW_ERROR_RECOVERY:
1508bf21cd93STycho Nightingale 		{
1509bf21cd93STycho Nightingale 			uint8_t buf[16];
1510bf21cd93STycho Nightingale 
1511bf21cd93STycho Nightingale 			if (len > sizeof(buf))
1512bf21cd93STycho Nightingale 				len = sizeof(buf);
1513bf21cd93STycho Nightingale 
1514bf21cd93STycho Nightingale 			memset(buf, 0, sizeof(buf));
1515bf21cd93STycho Nightingale 			be16enc(buf, 16 - 2);
1516bf21cd93STycho Nightingale 			buf[2] = 0x70;
1517bf21cd93STycho Nightingale 			buf[8] = 0x01;
1518bf21cd93STycho Nightingale 			buf[9] = 16 - 10;
1519bf21cd93STycho Nightingale 			buf[11] = 0x05;
1520bf21cd93STycho Nightingale 			write_prdt(p, slot, cfis, buf, len);
1521bf21cd93STycho Nightingale 			tfd = ATA_S_READY | ATA_S_DSC;
1522bf21cd93STycho Nightingale 			break;
1523bf21cd93STycho Nightingale 		}
1524bf21cd93STycho Nightingale 		case MODEPAGE_CD_CAPABILITIES:
1525bf21cd93STycho Nightingale 		{
1526bf21cd93STycho Nightingale 			uint8_t buf[30];
1527bf21cd93STycho Nightingale 
1528bf21cd93STycho Nightingale 			if (len > sizeof(buf))
1529bf21cd93STycho Nightingale 				len = sizeof(buf);
1530bf21cd93STycho Nightingale 
1531bf21cd93STycho Nightingale 			memset(buf, 0, sizeof(buf));
1532bf21cd93STycho Nightingale 			be16enc(buf, 30 - 2);
1533bf21cd93STycho Nightingale 			buf[2] = 0x70;
1534bf21cd93STycho Nightingale 			buf[8] = 0x2A;
1535bf21cd93STycho Nightingale 			buf[9] = 30 - 10;
1536bf21cd93STycho Nightingale 			buf[10] = 0x08;
1537bf21cd93STycho Nightingale 			buf[12] = 0x71;
1538bf21cd93STycho Nightingale 			be16enc(&buf[18], 2);
1539bf21cd93STycho Nightingale 			be16enc(&buf[20], 512);
1540bf21cd93STycho Nightingale 			write_prdt(p, slot, cfis, buf, len);
1541bf21cd93STycho Nightingale 			tfd = ATA_S_READY | ATA_S_DSC;
1542bf21cd93STycho Nightingale 			break;
1543bf21cd93STycho Nightingale 		}
1544bf21cd93STycho Nightingale 		default:
1545bf21cd93STycho Nightingale 			goto error;
1546bf21cd93STycho Nightingale 			break;
1547bf21cd93STycho Nightingale 		}
1548bf21cd93STycho Nightingale 		break;
1549bf21cd93STycho Nightingale 	case 3:
1550bf21cd93STycho Nightingale 		p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1551bf21cd93STycho Nightingale 		p->asc = 0x39;
1552bf21cd93STycho Nightingale 		tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1553bf21cd93STycho Nightingale 		break;
1554bf21cd93STycho Nightingale error:
1555bf21cd93STycho Nightingale 	case 1:
1556bf21cd93STycho Nightingale 	case 2:
1557bf21cd93STycho Nightingale 		p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1558bf21cd93STycho Nightingale 		p->asc = 0x24;
1559bf21cd93STycho Nightingale 		tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1560bf21cd93STycho Nightingale 		break;
1561bf21cd93STycho Nightingale 	}
1562bf21cd93STycho Nightingale 	cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1563bf21cd93STycho Nightingale 	ahci_write_fis_d2h(p, slot, cfis, tfd);
1564bf21cd93STycho Nightingale }
1565bf21cd93STycho Nightingale 
1566bf21cd93STycho Nightingale static void
atapi_get_event_status_notification(struct ahci_port * p,int slot,uint8_t * cfis)1567bf21cd93STycho Nightingale atapi_get_event_status_notification(struct ahci_port *p, int slot,
1568bf21cd93STycho Nightingale     uint8_t *cfis)
1569bf21cd93STycho Nightingale {
1570bf21cd93STycho Nightingale 	uint8_t *acmd;
1571bf21cd93STycho Nightingale 	uint32_t tfd;
1572bf21cd93STycho Nightingale 
1573bf21cd93STycho Nightingale 	acmd = cfis + 0x40;
1574bf21cd93STycho Nightingale 
1575bf21cd93STycho Nightingale 	/* we don't support asynchronous operation */
1576bf21cd93STycho Nightingale 	if (!(acmd[1] & 1)) {
1577bf21cd93STycho Nightingale 		p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1578bf21cd93STycho Nightingale 		p->asc = 0x24;
1579bf21cd93STycho Nightingale 		tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1580bf21cd93STycho Nightingale 	} else {
1581bf21cd93STycho Nightingale 		uint8_t buf[8];
158259d65d31SAndy Fiddaman 		unsigned int len;
1583bf21cd93STycho Nightingale 
1584bf21cd93STycho Nightingale 		len = be16dec(acmd + 7);
1585bf21cd93STycho Nightingale 		if (len > sizeof(buf))
1586bf21cd93STycho Nightingale 			len = sizeof(buf);
1587bf21cd93STycho Nightingale 
1588bf21cd93STycho Nightingale 		memset(buf, 0, sizeof(buf));
1589bf21cd93STycho Nightingale 		be16enc(buf, 8 - 2);
1590bf21cd93STycho Nightingale 		buf[2] = 0x04;
1591bf21cd93STycho Nightingale 		buf[3] = 0x10;
1592bf21cd93STycho Nightingale 		buf[5] = 0x02;
1593bf21cd93STycho Nightingale 		write_prdt(p, slot, cfis, buf, len);
1594bf21cd93STycho Nightingale 		tfd = ATA_S_READY | ATA_S_DSC;
1595bf21cd93STycho Nightingale 	}
1596bf21cd93STycho Nightingale 	cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1597bf21cd93STycho Nightingale 	ahci_write_fis_d2h(p, slot, cfis, tfd);
1598bf21cd93STycho Nightingale }
1599bf21cd93STycho Nightingale 
1600bf21cd93STycho Nightingale static void
handle_packet_cmd(struct ahci_port * p,int slot,uint8_t * cfis)1601bf21cd93STycho Nightingale handle_packet_cmd(struct ahci_port *p, int slot, uint8_t *cfis)
1602bf21cd93STycho Nightingale {
1603bf21cd93STycho Nightingale 	uint8_t *acmd;
1604bf21cd93STycho Nightingale 
1605bf21cd93STycho Nightingale 	acmd = cfis + 0x40;
1606bf21cd93STycho Nightingale 
1607bf21cd93STycho Nightingale #ifdef AHCI_DEBUG
1608bf21cd93STycho Nightingale 	{
1609bf21cd93STycho Nightingale 		int i;
1610bf21cd93STycho Nightingale 		DPRINTF("ACMD:");
1611bf21cd93STycho Nightingale 		for (i = 0; i < 16; i++)
1612bf21cd93STycho Nightingale 			DPRINTF("%02x ", acmd[i]);
1613154972afSPatrick Mooney 		DPRINTF("");
1614bf21cd93STycho Nightingale 	}
1615bf21cd93STycho Nightingale #endif
1616bf21cd93STycho Nightingale 
1617bf21cd93STycho Nightingale 	switch (acmd[0]) {
1618bf21cd93STycho Nightingale 	case TEST_UNIT_READY:
1619bf21cd93STycho Nightingale 		cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1620bf21cd93STycho Nightingale 		ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1621bf21cd93STycho Nightingale 		break;
1622bf21cd93STycho Nightingale 	case INQUIRY:
1623bf21cd93STycho Nightingale 		atapi_inquiry(p, slot, cfis);
1624bf21cd93STycho Nightingale 		break;
1625bf21cd93STycho Nightingale 	case READ_CAPACITY:
1626bf21cd93STycho Nightingale 		atapi_read_capacity(p, slot, cfis);
1627bf21cd93STycho Nightingale 		break;
1628bf21cd93STycho Nightingale 	case PREVENT_ALLOW:
1629bf21cd93STycho Nightingale 		/* TODO */
1630bf21cd93STycho Nightingale 		cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1631bf21cd93STycho Nightingale 		ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1632bf21cd93STycho Nightingale 		break;
1633bf21cd93STycho Nightingale 	case READ_TOC:
1634bf21cd93STycho Nightingale 		atapi_read_toc(p, slot, cfis);
1635bf21cd93STycho Nightingale 		break;
16364c87aefeSPatrick Mooney 	case REPORT_LUNS:
16374c87aefeSPatrick Mooney 		atapi_report_luns(p, slot, cfis);
16384c87aefeSPatrick Mooney 		break;
1639bf21cd93STycho Nightingale 	case READ_10:
1640bf21cd93STycho Nightingale 	case READ_12:
16414c87aefeSPatrick Mooney 		atapi_read(p, slot, cfis, 0);
1642bf21cd93STycho Nightingale 		break;
1643bf21cd93STycho Nightingale 	case REQUEST_SENSE:
1644bf21cd93STycho Nightingale 		atapi_request_sense(p, slot, cfis);
1645bf21cd93STycho Nightingale 		break;
1646bf21cd93STycho Nightingale 	case START_STOP_UNIT:
1647bf21cd93STycho Nightingale 		atapi_start_stop_unit(p, slot, cfis);
1648bf21cd93STycho Nightingale 		break;
1649bf21cd93STycho Nightingale 	case MODE_SENSE_10:
1650bf21cd93STycho Nightingale 		atapi_mode_sense(p, slot, cfis);
1651bf21cd93STycho Nightingale 		break;
1652bf21cd93STycho Nightingale 	case GET_EVENT_STATUS_NOTIFICATION:
1653bf21cd93STycho Nightingale 		atapi_get_event_status_notification(p, slot, cfis);
1654bf21cd93STycho Nightingale 		break;
1655bf21cd93STycho Nightingale 	default:
1656bf21cd93STycho Nightingale 		cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1657bf21cd93STycho Nightingale 		p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1658bf21cd93STycho Nightingale 		p->asc = 0x20;
1659bf21cd93STycho Nightingale 		ahci_write_fis_d2h(p, slot, cfis, (p->sense_key << 12) |
1660bf21cd93STycho Nightingale 				ATA_S_READY | ATA_S_ERROR);
1661bf21cd93STycho Nightingale 		break;
1662bf21cd93STycho Nightingale 	}
1663bf21cd93STycho Nightingale }
1664bf21cd93STycho Nightingale 
1665bf21cd93STycho Nightingale static void
ahci_handle_cmd(struct ahci_port * p,int slot,uint8_t * cfis)1666bf21cd93STycho Nightingale ahci_handle_cmd(struct ahci_port *p, int slot, uint8_t *cfis)
1667bf21cd93STycho Nightingale {
1668bf21cd93STycho Nightingale 
16694c87aefeSPatrick Mooney 	p->tfd |= ATA_S_BUSY;
1670bf21cd93STycho Nightingale 	switch (cfis[2]) {
1671bf21cd93STycho Nightingale 	case ATA_ATA_IDENTIFY:
1672bf21cd93STycho Nightingale 		handle_identify(p, slot, cfis);
1673bf21cd93STycho Nightingale 		break;
1674bf21cd93STycho Nightingale 	case ATA_SETFEATURES:
1675bf21cd93STycho Nightingale 	{
1676bf21cd93STycho Nightingale 		switch (cfis[3]) {
1677bf21cd93STycho Nightingale 		case ATA_SF_ENAB_SATA_SF:
1678bf21cd93STycho Nightingale 			switch (cfis[12]) {
1679bf21cd93STycho Nightingale 			case ATA_SATA_SF_AN:
1680bf21cd93STycho Nightingale 				p->tfd = ATA_S_DSC | ATA_S_READY;
1681bf21cd93STycho Nightingale 				break;
1682bf21cd93STycho Nightingale 			default:
1683bf21cd93STycho Nightingale 				p->tfd = ATA_S_ERROR | ATA_S_READY;
1684bf21cd93STycho Nightingale 				p->tfd |= (ATA_ERROR_ABORT << 8);
1685bf21cd93STycho Nightingale 				break;
1686bf21cd93STycho Nightingale 			}
1687bf21cd93STycho Nightingale 			break;
1688bf21cd93STycho Nightingale 		case ATA_SF_ENAB_WCACHE:
1689bf21cd93STycho Nightingale 		case ATA_SF_DIS_WCACHE:
1690bf21cd93STycho Nightingale 		case ATA_SF_ENAB_RCACHE:
1691bf21cd93STycho Nightingale 		case ATA_SF_DIS_RCACHE:
1692bf21cd93STycho Nightingale 			p->tfd = ATA_S_DSC | ATA_S_READY;
1693bf21cd93STycho Nightingale 			break;
1694bf21cd93STycho Nightingale 		case ATA_SF_SETXFER:
1695bf21cd93STycho Nightingale 		{
1696bf21cd93STycho Nightingale 			switch (cfis[12] & 0xf8) {
1697bf21cd93STycho Nightingale 			case ATA_PIO:
1698bf21cd93STycho Nightingale 			case ATA_PIO0:
1699bf21cd93STycho Nightingale 				break;
1700bf21cd93STycho Nightingale 			case ATA_WDMA0:
1701bf21cd93STycho Nightingale 			case ATA_UDMA0:
1702bf21cd93STycho Nightingale 				p->xfermode = (cfis[12] & 0x7);
1703bf21cd93STycho Nightingale 				break;
1704bf21cd93STycho Nightingale 			}
1705bf21cd93STycho Nightingale 			p->tfd = ATA_S_DSC | ATA_S_READY;
1706bf21cd93STycho Nightingale 			break;
1707bf21cd93STycho Nightingale 		}
1708bf21cd93STycho Nightingale 		default:
1709bf21cd93STycho Nightingale 			p->tfd = ATA_S_ERROR | ATA_S_READY;
1710bf21cd93STycho Nightingale 			p->tfd |= (ATA_ERROR_ABORT << 8);
1711bf21cd93STycho Nightingale 			break;
1712bf21cd93STycho Nightingale 		}
1713bf21cd93STycho Nightingale 		ahci_write_fis_d2h(p, slot, cfis, p->tfd);
1714bf21cd93STycho Nightingale 		break;
1715bf21cd93STycho Nightingale 	}
1716bf21cd93STycho Nightingale 	case ATA_SET_MULTI:
1717bf21cd93STycho Nightingale 		if (cfis[12] != 0 &&
1718bf21cd93STycho Nightingale 			(cfis[12] > 128 || (cfis[12] & (cfis[12] - 1)))) {
1719bf21cd93STycho Nightingale 			p->tfd = ATA_S_ERROR | ATA_S_READY;
1720bf21cd93STycho Nightingale 			p->tfd |= (ATA_ERROR_ABORT << 8);
1721bf21cd93STycho Nightingale 		} else {
1722bf21cd93STycho Nightingale 			p->mult_sectors = cfis[12];
1723bf21cd93STycho Nightingale 			p->tfd = ATA_S_DSC | ATA_S_READY;
1724bf21cd93STycho Nightingale 		}
17254c87aefeSPatrick Mooney 		ahci_write_fis_d2h(p, slot, cfis, p->tfd);
1726bf21cd93STycho Nightingale 		break;
17274c87aefeSPatrick Mooney 	case ATA_READ:
17284c87aefeSPatrick Mooney 	case ATA_WRITE:
17294c87aefeSPatrick Mooney 	case ATA_READ48:
17304c87aefeSPatrick Mooney 	case ATA_WRITE48:
17314c87aefeSPatrick Mooney 	case ATA_READ_MUL:
17324c87aefeSPatrick Mooney 	case ATA_WRITE_MUL:
17334c87aefeSPatrick Mooney 	case ATA_READ_MUL48:
17344c87aefeSPatrick Mooney 	case ATA_WRITE_MUL48:
1735bf21cd93STycho Nightingale 	case ATA_READ_DMA:
1736bf21cd93STycho Nightingale 	case ATA_WRITE_DMA:
1737bf21cd93STycho Nightingale 	case ATA_READ_DMA48:
1738bf21cd93STycho Nightingale 	case ATA_WRITE_DMA48:
1739bf21cd93STycho Nightingale 	case ATA_READ_FPDMA_QUEUED:
1740bf21cd93STycho Nightingale 	case ATA_WRITE_FPDMA_QUEUED:
17414c87aefeSPatrick Mooney 		ahci_handle_rw(p, slot, cfis, 0);
1742bf21cd93STycho Nightingale 		break;
1743bf21cd93STycho Nightingale 	case ATA_FLUSHCACHE:
1744bf21cd93STycho Nightingale 	case ATA_FLUSHCACHE48:
1745bf21cd93STycho Nightingale 		ahci_handle_flush(p, slot, cfis);
1746bf21cd93STycho Nightingale 		break;
17474c87aefeSPatrick Mooney 	case ATA_DATA_SET_MANAGEMENT:
17484c87aefeSPatrick Mooney 		if (cfis[11] == 0 && cfis[3] == ATA_DSM_TRIM &&
17494c87aefeSPatrick Mooney 		    cfis[13] == 0 && cfis[12] == 1) {
17504c87aefeSPatrick Mooney 			ahci_handle_dsm_trim(p, slot, cfis, 0);
1751bf21cd93STycho Nightingale 			break;
17524c87aefeSPatrick Mooney 		}
17534c87aefeSPatrick Mooney 		ahci_write_fis_d2h(p, slot, cfis,
17544c87aefeSPatrick Mooney 		    (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
17554c87aefeSPatrick Mooney 		break;
17564c87aefeSPatrick Mooney 	case ATA_SEND_FPDMA_QUEUED:
17574c87aefeSPatrick Mooney 		if ((cfis[13] & 0x1f) == ATA_SFPDMA_DSM &&
17584c87aefeSPatrick Mooney 		    cfis[17] == 0 && cfis[16] == ATA_DSM_TRIM &&
17594c87aefeSPatrick Mooney 		    cfis[11] == 0 && cfis[3] == 1) {
17604c87aefeSPatrick Mooney 			ahci_handle_dsm_trim(p, slot, cfis, 0);
17614c87aefeSPatrick Mooney 			break;
17624c87aefeSPatrick Mooney 		}
17634c87aefeSPatrick Mooney 		ahci_write_fis_d2h(p, slot, cfis,
17644c87aefeSPatrick Mooney 		    (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
17654c87aefeSPatrick Mooney 		break;
17664c87aefeSPatrick Mooney 	case ATA_READ_LOG_EXT:
17674c87aefeSPatrick Mooney 	case ATA_READ_LOG_DMA_EXT:
17684c87aefeSPatrick Mooney 		ahci_handle_read_log(p, slot, cfis);
17694c87aefeSPatrick Mooney 		break;
17704c87aefeSPatrick Mooney 	case ATA_SECURITY_FREEZE_LOCK:
17714c87aefeSPatrick Mooney 	case ATA_SMART_CMD:
1772bf21cd93STycho Nightingale 	case ATA_NOP:
17734c87aefeSPatrick Mooney 		ahci_write_fis_d2h(p, slot, cfis,
17744c87aefeSPatrick Mooney 		    (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
17754c87aefeSPatrick Mooney 		break;
17764c87aefeSPatrick Mooney 	case ATA_CHECK_POWER_MODE:
17774c87aefeSPatrick Mooney 		cfis[12] = 0xff;	/* always on */
17784c87aefeSPatrick Mooney 		ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
17794c87aefeSPatrick Mooney 		break;
17804c87aefeSPatrick Mooney 	case ATA_STANDBY_CMD:
1781bf21cd93STycho Nightingale 	case ATA_STANDBY_IMMEDIATE:
17824c87aefeSPatrick Mooney 	case ATA_IDLE_CMD:
1783bf21cd93STycho Nightingale 	case ATA_IDLE_IMMEDIATE:
1784bf21cd93STycho Nightingale 	case ATA_SLEEP:
17854c87aefeSPatrick Mooney 	case ATA_READ_VERIFY:
17864c87aefeSPatrick Mooney 	case ATA_READ_VERIFY48:
1787bf21cd93STycho Nightingale 		ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1788bf21cd93STycho Nightingale 		break;
1789bf21cd93STycho Nightingale 	case ATA_ATAPI_IDENTIFY:
1790bf21cd93STycho Nightingale 		handle_atapi_identify(p, slot, cfis);
1791bf21cd93STycho Nightingale 		break;
1792bf21cd93STycho Nightingale 	case ATA_PACKET_CMD:
1793bf21cd93STycho Nightingale 		if (!p->atapi) {
17944c87aefeSPatrick Mooney 			ahci_write_fis_d2h(p, slot, cfis,
17954c87aefeSPatrick Mooney 			    (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
1796bf21cd93STycho Nightingale 		} else
1797bf21cd93STycho Nightingale 			handle_packet_cmd(p, slot, cfis);
1798bf21cd93STycho Nightingale 		break;
1799bf21cd93STycho Nightingale 	default:
1800154972afSPatrick Mooney 		WPRINTF("Unsupported cmd:%02x", cfis[2]);
18014c87aefeSPatrick Mooney 		ahci_write_fis_d2h(p, slot, cfis,
18024c87aefeSPatrick Mooney 		    (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
1803bf21cd93STycho Nightingale 		break;
1804bf21cd93STycho Nightingale 	}
1805bf21cd93STycho Nightingale }
1806bf21cd93STycho Nightingale 
1807bf21cd93STycho Nightingale static void
ahci_handle_slot(struct ahci_port * p,int slot)1808bf21cd93STycho Nightingale ahci_handle_slot(struct ahci_port *p, int slot)
1809bf21cd93STycho Nightingale {
1810bf21cd93STycho Nightingale 	struct ahci_cmd_hdr *hdr;
18114c87aefeSPatrick Mooney #ifdef AHCI_DEBUG
1812bf21cd93STycho Nightingale 	struct ahci_prdt_entry *prdt;
18134c87aefeSPatrick Mooney #endif
1814bf21cd93STycho Nightingale 	struct pci_ahci_softc *sc;
1815bf21cd93STycho Nightingale 	uint8_t *cfis;
18164c87aefeSPatrick Mooney #ifdef AHCI_DEBUG
18174c87aefeSPatrick Mooney 	int cfl, i;
18184c87aefeSPatrick Mooney #endif
1819bf21cd93STycho Nightingale 
1820bf21cd93STycho Nightingale 	sc = p->pr_sc;
1821bf21cd93STycho Nightingale 	hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
18224c87aefeSPatrick Mooney #ifdef AHCI_DEBUG
1823bf21cd93STycho Nightingale 	cfl = (hdr->flags & 0x1f) * 4;
18244c87aefeSPatrick Mooney #endif
1825bf21cd93STycho Nightingale 	cfis = paddr_guest2host(ahci_ctx(sc), hdr->ctba,
1826bf21cd93STycho Nightingale 			0x80 + hdr->prdtl * sizeof(struct ahci_prdt_entry));
18274c87aefeSPatrick Mooney #ifdef AHCI_DEBUG
1828bf21cd93STycho Nightingale 	prdt = (struct ahci_prdt_entry *)(cfis + 0x80);
1829bf21cd93STycho Nightingale 
1830154972afSPatrick Mooney 	DPRINTF("cfis:");
1831bf21cd93STycho Nightingale 	for (i = 0; i < cfl; i++) {
1832bf21cd93STycho Nightingale 		if (i % 10 == 0)
1833154972afSPatrick Mooney 			DPRINTF("");
1834bf21cd93STycho Nightingale 		DPRINTF("%02x ", cfis[i]);
1835bf21cd93STycho Nightingale 	}
1836154972afSPatrick Mooney 	DPRINTF("");
1837bf21cd93STycho Nightingale 
1838bf21cd93STycho Nightingale 	for (i = 0; i < hdr->prdtl; i++) {
1839154972afSPatrick Mooney 		DPRINTF("%d@%08"PRIx64"", prdt->dbc & 0x3fffff, prdt->dba);
1840bf21cd93STycho Nightingale 		prdt++;
1841bf21cd93STycho Nightingale 	}
1842bf21cd93STycho Nightingale #endif
1843bf21cd93STycho Nightingale 
1844bf21cd93STycho Nightingale 	if (cfis[0] != FIS_TYPE_REGH2D) {
1845154972afSPatrick Mooney 		WPRINTF("Not a H2D FIS:%02x", cfis[0]);
1846bf21cd93STycho Nightingale 		return;
1847bf21cd93STycho Nightingale 	}
1848bf21cd93STycho Nightingale 
1849bf21cd93STycho Nightingale 	if (cfis[1] & 0x80) {
1850bf21cd93STycho Nightingale 		ahci_handle_cmd(p, slot, cfis);
1851bf21cd93STycho Nightingale 	} else {
1852bf21cd93STycho Nightingale 		if (cfis[15] & (1 << 2))
1853bf21cd93STycho Nightingale 			p->reset = 1;
1854bf21cd93STycho Nightingale 		else if (p->reset) {
1855bf21cd93STycho Nightingale 			p->reset = 0;
1856bf21cd93STycho Nightingale 			ahci_port_reset(p);
1857bf21cd93STycho Nightingale 		}
1858bf21cd93STycho Nightingale 		p->ci &= ~(1 << slot);
1859bf21cd93STycho Nightingale 	}
1860bf21cd93STycho Nightingale }
1861bf21cd93STycho Nightingale 
1862bf21cd93STycho Nightingale static void
ahci_handle_port(struct ahci_port * p)1863bf21cd93STycho Nightingale ahci_handle_port(struct ahci_port *p)
1864bf21cd93STycho Nightingale {
1865bf21cd93STycho Nightingale 
1866bf21cd93STycho Nightingale 	if (!(p->cmd & AHCI_P_CMD_ST))
1867bf21cd93STycho Nightingale 		return;
1868bf21cd93STycho Nightingale 
1869bf21cd93STycho Nightingale 	/*
1870bf21cd93STycho Nightingale 	 * Search for any new commands to issue ignoring those that
18714c87aefeSPatrick Mooney 	 * are already in-flight.  Stop if device is busy or in error.
1872bf21cd93STycho Nightingale 	 */
18734c87aefeSPatrick Mooney 	for (; (p->ci & ~p->pending) != 0; p->ccs = ((p->ccs + 1) & 31)) {
18744c87aefeSPatrick Mooney 		if ((p->tfd & (ATA_S_BUSY | ATA_S_DRQ)) != 0)
18754c87aefeSPatrick Mooney 			break;
18764c87aefeSPatrick Mooney 		if (p->waitforclear)
18774c87aefeSPatrick Mooney 			break;
18784c87aefeSPatrick Mooney 		if ((p->ci & ~p->pending & (1 << p->ccs)) != 0) {
1879bf21cd93STycho Nightingale 			p->cmd &= ~AHCI_P_CMD_CCS_MASK;
18804c87aefeSPatrick Mooney 			p->cmd |= p->ccs << AHCI_P_CMD_CCS_SHIFT;
18814c87aefeSPatrick Mooney 			ahci_handle_slot(p, p->ccs);
1882bf21cd93STycho Nightingale 		}
1883bf21cd93STycho Nightingale 	}
1884bf21cd93STycho Nightingale }
1885bf21cd93STycho Nightingale 
1886bf21cd93STycho Nightingale /*
1887bf21cd93STycho Nightingale  * blockif callback routine - this runs in the context of the blockif
1888bf21cd93STycho Nightingale  * i/o thread, so the mutex needs to be acquired.
1889bf21cd93STycho Nightingale  */
1890bf21cd93STycho Nightingale static void
ata_ioreq_cb(struct blockif_req * br,int err)1891bf21cd93STycho Nightingale ata_ioreq_cb(struct blockif_req *br, int err)
1892bf21cd93STycho Nightingale {
1893bf21cd93STycho Nightingale 	struct ahci_cmd_hdr *hdr;
1894bf21cd93STycho Nightingale 	struct ahci_ioreq *aior;
1895bf21cd93STycho Nightingale 	struct ahci_port *p;
1896bf21cd93STycho Nightingale 	struct pci_ahci_softc *sc;
1897bf21cd93STycho Nightingale 	uint32_t tfd;
1898bf21cd93STycho Nightingale 	uint8_t *cfis;
18994c87aefeSPatrick Mooney 	int slot, ncq, dsm;
1900bf21cd93STycho Nightingale 
1901154972afSPatrick Mooney 	DPRINTF("%s %d", __func__, err);
1902bf21cd93STycho Nightingale 
19034c87aefeSPatrick Mooney 	ncq = dsm = 0;
1904bf21cd93STycho Nightingale 	aior = br->br_param;
1905bf21cd93STycho Nightingale 	p = aior->io_pr;
1906bf21cd93STycho Nightingale 	cfis = aior->cfis;
1907bf21cd93STycho Nightingale 	slot = aior->slot;
1908bf21cd93STycho Nightingale 	sc = p->pr_sc;
1909bf21cd93STycho Nightingale 	hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
1910bf21cd93STycho Nightingale 
1911bf21cd93STycho Nightingale 	if (cfis[2] == ATA_WRITE_FPDMA_QUEUED ||
19124c87aefeSPatrick Mooney 	    cfis[2] == ATA_READ_FPDMA_QUEUED ||
19134c87aefeSPatrick Mooney 	    cfis[2] == ATA_SEND_FPDMA_QUEUED)
1914bf21cd93STycho Nightingale 		ncq = 1;
19154c87aefeSPatrick Mooney 	if (cfis[2] == ATA_DATA_SET_MANAGEMENT ||
19164c87aefeSPatrick Mooney 	    (cfis[2] == ATA_SEND_FPDMA_QUEUED &&
19174c87aefeSPatrick Mooney 	     (cfis[13] & 0x1f) == ATA_SFPDMA_DSM))
19184c87aefeSPatrick Mooney 		dsm = 1;
1919bf21cd93STycho Nightingale 
1920bf21cd93STycho Nightingale 	pthread_mutex_lock(&sc->mtx);
1921bf21cd93STycho Nightingale 
1922bf21cd93STycho Nightingale 	/*
1923bf21cd93STycho Nightingale 	 * Delete the blockif request from the busy list
1924bf21cd93STycho Nightingale 	 */
1925bf21cd93STycho Nightingale 	TAILQ_REMOVE(&p->iobhd, aior, io_blist);
1926bf21cd93STycho Nightingale 
1927bf21cd93STycho Nightingale 	/*
1928bf21cd93STycho Nightingale 	 * Move the blockif request back to the free list
1929bf21cd93STycho Nightingale 	 */
1930bf21cd93STycho Nightingale 	STAILQ_INSERT_TAIL(&p->iofhd, aior, io_flist);
1931bf21cd93STycho Nightingale 
19324c87aefeSPatrick Mooney 	if (!err)
19334c87aefeSPatrick Mooney 		hdr->prdbc = aior->done;
19344c87aefeSPatrick Mooney 
19354c87aefeSPatrick Mooney 	if (!err && aior->more) {
19364c87aefeSPatrick Mooney 		if (dsm)
19374c87aefeSPatrick Mooney 			ahci_handle_dsm_trim(p, slot, cfis, aior->done);
19384c87aefeSPatrick Mooney 		else
19394c87aefeSPatrick Mooney 			ahci_handle_rw(p, slot, cfis, aior->done);
1940bf21cd93STycho Nightingale 		goto out;
1941bf21cd93STycho Nightingale 	}
1942bf21cd93STycho Nightingale 
19434c87aefeSPatrick Mooney 	if (!err)
1944bf21cd93STycho Nightingale 		tfd = ATA_S_READY | ATA_S_DSC;
1945bf21cd93STycho Nightingale 	else
1946bf21cd93STycho Nightingale 		tfd = (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR;
1947bf21cd93STycho Nightingale 	if (ncq)
19484c87aefeSPatrick Mooney 		ahci_write_fis_sdb(p, slot, cfis, tfd);
19494c87aefeSPatrick Mooney 	else
1950bf21cd93STycho Nightingale 		ahci_write_fis_d2h(p, slot, cfis, tfd);
1951bf21cd93STycho Nightingale 
1952bf21cd93STycho Nightingale 	/*
1953bf21cd93STycho Nightingale 	 * This command is now complete.
1954bf21cd93STycho Nightingale 	 */
1955bf21cd93STycho Nightingale 	p->pending &= ~(1 << slot);
1956bf21cd93STycho Nightingale 
1957bf21cd93STycho Nightingale 	ahci_check_stopped(p);
19584c87aefeSPatrick Mooney 	ahci_handle_port(p);
1959bf21cd93STycho Nightingale out:
1960bf21cd93STycho Nightingale 	pthread_mutex_unlock(&sc->mtx);
1961154972afSPatrick Mooney 	DPRINTF("%s exit", __func__);
1962bf21cd93STycho Nightingale }
1963bf21cd93STycho Nightingale 
1964bf21cd93STycho Nightingale static void
atapi_ioreq_cb(struct blockif_req * br,int err)1965bf21cd93STycho Nightingale atapi_ioreq_cb(struct blockif_req *br, int err)
1966bf21cd93STycho Nightingale {
1967bf21cd93STycho Nightingale 	struct ahci_cmd_hdr *hdr;
1968bf21cd93STycho Nightingale 	struct ahci_ioreq *aior;
1969bf21cd93STycho Nightingale 	struct ahci_port *p;
1970bf21cd93STycho Nightingale 	struct pci_ahci_softc *sc;
1971bf21cd93STycho Nightingale 	uint8_t *cfis;
1972bf21cd93STycho Nightingale 	uint32_t tfd;
19734c87aefeSPatrick Mooney 	int slot;
1974bf21cd93STycho Nightingale 
1975154972afSPatrick Mooney 	DPRINTF("%s %d", __func__, err);
1976bf21cd93STycho Nightingale 
1977bf21cd93STycho Nightingale 	aior = br->br_param;
1978bf21cd93STycho Nightingale 	p = aior->io_pr;
1979bf21cd93STycho Nightingale 	cfis = aior->cfis;
1980bf21cd93STycho Nightingale 	slot = aior->slot;
1981bf21cd93STycho Nightingale 	sc = p->pr_sc;
1982bf21cd93STycho Nightingale 	hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + aior->slot * AHCI_CL_SIZE);
1983bf21cd93STycho Nightingale 
1984bf21cd93STycho Nightingale 	pthread_mutex_lock(&sc->mtx);
1985bf21cd93STycho Nightingale 
1986bf21cd93STycho Nightingale 	/*
1987bf21cd93STycho Nightingale 	 * Delete the blockif request from the busy list
1988bf21cd93STycho Nightingale 	 */
1989bf21cd93STycho Nightingale 	TAILQ_REMOVE(&p->iobhd, aior, io_blist);
1990bf21cd93STycho Nightingale 
1991bf21cd93STycho Nightingale 	/*
1992bf21cd93STycho Nightingale 	 * Move the blockif request back to the free list
1993bf21cd93STycho Nightingale 	 */
1994bf21cd93STycho Nightingale 	STAILQ_INSERT_TAIL(&p->iofhd, aior, io_flist);
1995bf21cd93STycho Nightingale 
19964c87aefeSPatrick Mooney 	if (!err)
19974c87aefeSPatrick Mooney 		hdr->prdbc = aior->done;
19984c87aefeSPatrick Mooney 
19994c87aefeSPatrick Mooney 	if (!err && aior->more) {
20004c87aefeSPatrick Mooney 		atapi_read(p, slot, cfis, aior->done);
2001bf21cd93STycho Nightingale 		goto out;
2002bf21cd93STycho Nightingale 	}
2003bf21cd93STycho Nightingale 
20044c87aefeSPatrick Mooney 	if (!err) {
2005bf21cd93STycho Nightingale 		tfd = ATA_S_READY | ATA_S_DSC;
2006bf21cd93STycho Nightingale 	} else {
2007bf21cd93STycho Nightingale 		p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
2008bf21cd93STycho Nightingale 		p->asc = 0x21;
2009bf21cd93STycho Nightingale 		tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
2010bf21cd93STycho Nightingale 	}
2011bf21cd93STycho Nightingale 	cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
2012bf21cd93STycho Nightingale 	ahci_write_fis_d2h(p, slot, cfis, tfd);
2013bf21cd93STycho Nightingale 
2014bf21cd93STycho Nightingale 	/*
2015bf21cd93STycho Nightingale 	 * This command is now complete.
2016bf21cd93STycho Nightingale 	 */
2017bf21cd93STycho Nightingale 	p->pending &= ~(1 << slot);
2018bf21cd93STycho Nightingale 
2019bf21cd93STycho Nightingale 	ahci_check_stopped(p);
20204c87aefeSPatrick Mooney 	ahci_handle_port(p);
2021bf21cd93STycho Nightingale out:
2022bf21cd93STycho Nightingale 	pthread_mutex_unlock(&sc->mtx);
2023154972afSPatrick Mooney 	DPRINTF("%s exit", __func__);
2024bf21cd93STycho Nightingale }
2025bf21cd93STycho Nightingale 
2026bf21cd93STycho Nightingale static void
pci_ahci_ioreq_init(struct ahci_port * pr)2027bf21cd93STycho Nightingale pci_ahci_ioreq_init(struct ahci_port *pr)
2028bf21cd93STycho Nightingale {
2029bf21cd93STycho Nightingale 	struct ahci_ioreq *vr;
2030bf21cd93STycho Nightingale 	int i;
2031bf21cd93STycho Nightingale 
2032bf21cd93STycho Nightingale 	pr->ioqsz = blockif_queuesz(pr->bctx);
2033bf21cd93STycho Nightingale 	pr->ioreq = calloc(pr->ioqsz, sizeof(struct ahci_ioreq));
2034bf21cd93STycho Nightingale 	STAILQ_INIT(&pr->iofhd);
2035bf21cd93STycho Nightingale 
2036bf21cd93STycho Nightingale 	/*
2037bf21cd93STycho Nightingale 	 * Add all i/o request entries to the free queue
2038bf21cd93STycho Nightingale 	 */
2039bf21cd93STycho Nightingale 	for (i = 0; i < pr->ioqsz; i++) {
2040bf21cd93STycho Nightingale 		vr = &pr->ioreq[i];
2041bf21cd93STycho Nightingale 		vr->io_pr = pr;
2042bf21cd93STycho Nightingale 		if (!pr->atapi)
2043bf21cd93STycho Nightingale 			vr->io_req.br_callback = ata_ioreq_cb;
2044bf21cd93STycho Nightingale 		else
2045bf21cd93STycho Nightingale 			vr->io_req.br_callback = atapi_ioreq_cb;
2046bf21cd93STycho Nightingale 		vr->io_req.br_param = vr;
2047bf21cd93STycho Nightingale 		STAILQ_INSERT_TAIL(&pr->iofhd, vr, io_flist);
2048bf21cd93STycho Nightingale 	}
2049bf21cd93STycho Nightingale 
2050bf21cd93STycho Nightingale 	TAILQ_INIT(&pr->iobhd);
2051bf21cd93STycho Nightingale }
2052bf21cd93STycho Nightingale 
2053bf21cd93STycho Nightingale static void
pci_ahci_port_write(struct pci_ahci_softc * sc,uint64_t offset,uint64_t value)2054bf21cd93STycho Nightingale pci_ahci_port_write(struct pci_ahci_softc *sc, uint64_t offset, uint64_t value)
2055bf21cd93STycho Nightingale {
2056bf21cd93STycho Nightingale 	int port = (offset - AHCI_OFFSET) / AHCI_STEP;
2057bf21cd93STycho Nightingale 	offset = (offset - AHCI_OFFSET) % AHCI_STEP;
2058bf21cd93STycho Nightingale 	struct ahci_port *p = &sc->port[port];
2059bf21cd93STycho Nightingale 
2060154972afSPatrick Mooney 	DPRINTF("pci_ahci_port %d: write offset 0x%"PRIx64" value 0x%"PRIx64"",
2061bf21cd93STycho Nightingale 		port, offset, value);
2062bf21cd93STycho Nightingale 
2063bf21cd93STycho Nightingale 	switch (offset) {
2064bf21cd93STycho Nightingale 	case AHCI_P_CLB:
2065bf21cd93STycho Nightingale 		p->clb = value;
2066bf21cd93STycho Nightingale 		break;
2067bf21cd93STycho Nightingale 	case AHCI_P_CLBU:
2068bf21cd93STycho Nightingale 		p->clbu = value;
2069bf21cd93STycho Nightingale 		break;
2070bf21cd93STycho Nightingale 	case AHCI_P_FB:
2071bf21cd93STycho Nightingale 		p->fb = value;
2072bf21cd93STycho Nightingale 		break;
2073bf21cd93STycho Nightingale 	case AHCI_P_FBU:
2074bf21cd93STycho Nightingale 		p->fbu = value;
2075bf21cd93STycho Nightingale 		break;
2076bf21cd93STycho Nightingale 	case AHCI_P_IS:
2077bf21cd93STycho Nightingale 		p->is &= ~value;
20784c87aefeSPatrick Mooney 		ahci_port_intr(p);
2079bf21cd93STycho Nightingale 		break;
2080bf21cd93STycho Nightingale 	case AHCI_P_IE:
2081bf21cd93STycho Nightingale 		p->ie = value & 0xFDC000FF;
20824c87aefeSPatrick Mooney 		ahci_port_intr(p);
2083bf21cd93STycho Nightingale 		break;
2084bf21cd93STycho Nightingale 	case AHCI_P_CMD:
2085bf21cd93STycho Nightingale 	{
20864c87aefeSPatrick Mooney 		p->cmd &= ~(AHCI_P_CMD_ST | AHCI_P_CMD_SUD | AHCI_P_CMD_POD |
20874c87aefeSPatrick Mooney 		    AHCI_P_CMD_CLO | AHCI_P_CMD_FRE | AHCI_P_CMD_APSTE |
20884c87aefeSPatrick Mooney 		    AHCI_P_CMD_ATAPI | AHCI_P_CMD_DLAE | AHCI_P_CMD_ALPE |
20894c87aefeSPatrick Mooney 		    AHCI_P_CMD_ASP | AHCI_P_CMD_ICC_MASK);
20904c87aefeSPatrick Mooney 		p->cmd |= (AHCI_P_CMD_ST | AHCI_P_CMD_SUD | AHCI_P_CMD_POD |
20914c87aefeSPatrick Mooney 		    AHCI_P_CMD_CLO | AHCI_P_CMD_FRE | AHCI_P_CMD_APSTE |
20924c87aefeSPatrick Mooney 		    AHCI_P_CMD_ATAPI | AHCI_P_CMD_DLAE | AHCI_P_CMD_ALPE |
20934c87aefeSPatrick Mooney 		    AHCI_P_CMD_ASP | AHCI_P_CMD_ICC_MASK) & value;
2094bf21cd93STycho Nightingale 
2095bf21cd93STycho Nightingale 		if (!(value & AHCI_P_CMD_ST)) {
2096bf21cd93STycho Nightingale 			ahci_port_stop(p);
2097bf21cd93STycho Nightingale 		} else {
2098bf21cd93STycho Nightingale 			uint64_t clb;
2099bf21cd93STycho Nightingale 
2100bf21cd93STycho Nightingale 			p->cmd |= AHCI_P_CMD_CR;
2101bf21cd93STycho Nightingale 			clb = (uint64_t)p->clbu << 32 | p->clb;
2102bf21cd93STycho Nightingale 			p->cmd_lst = paddr_guest2host(ahci_ctx(sc), clb,
2103bf21cd93STycho Nightingale 					AHCI_CL_SIZE * AHCI_MAX_SLOTS);
2104bf21cd93STycho Nightingale 		}
2105bf21cd93STycho Nightingale 
2106bf21cd93STycho Nightingale 		if (value & AHCI_P_CMD_FRE) {
2107bf21cd93STycho Nightingale 			uint64_t fb;
2108bf21cd93STycho Nightingale 
2109bf21cd93STycho Nightingale 			p->cmd |= AHCI_P_CMD_FR;
2110bf21cd93STycho Nightingale 			fb = (uint64_t)p->fbu << 32 | p->fb;
2111bf21cd93STycho Nightingale 			/* we don't support FBSCP, so rfis size is 256Bytes */
2112bf21cd93STycho Nightingale 			p->rfis = paddr_guest2host(ahci_ctx(sc), fb, 256);
2113bf21cd93STycho Nightingale 		} else {
2114bf21cd93STycho Nightingale 			p->cmd &= ~AHCI_P_CMD_FR;
2115bf21cd93STycho Nightingale 		}
2116bf21cd93STycho Nightingale 
2117bf21cd93STycho Nightingale 		if (value & AHCI_P_CMD_CLO) {
21184c87aefeSPatrick Mooney 			p->tfd &= ~(ATA_S_BUSY | ATA_S_DRQ);
2119bf21cd93STycho Nightingale 			p->cmd &= ~AHCI_P_CMD_CLO;
2120bf21cd93STycho Nightingale 		}
2121bf21cd93STycho Nightingale 
21224c87aefeSPatrick Mooney 		if (value & AHCI_P_CMD_ICC_MASK) {
21234c87aefeSPatrick Mooney 			p->cmd &= ~AHCI_P_CMD_ICC_MASK;
21244c87aefeSPatrick Mooney 		}
21254c87aefeSPatrick Mooney 
2126bf21cd93STycho Nightingale 		ahci_handle_port(p);
2127bf21cd93STycho Nightingale 		break;
2128bf21cd93STycho Nightingale 	}
2129bf21cd93STycho Nightingale 	case AHCI_P_TFD:
2130bf21cd93STycho Nightingale 	case AHCI_P_SIG:
2131bf21cd93STycho Nightingale 	case AHCI_P_SSTS:
2132154972afSPatrick Mooney 		WPRINTF("pci_ahci_port: read only registers 0x%"PRIx64"", offset);
2133bf21cd93STycho Nightingale 		break;
2134bf21cd93STycho Nightingale 	case AHCI_P_SCTL:
21354c87aefeSPatrick Mooney 		p->sctl = value;
2136bf21cd93STycho Nightingale 		if (!(p->cmd & AHCI_P_CMD_ST)) {
2137bf21cd93STycho Nightingale 			if (value & ATA_SC_DET_RESET)
2138bf21cd93STycho Nightingale 				ahci_port_reset(p);
2139bf21cd93STycho Nightingale 		}
2140bf21cd93STycho Nightingale 		break;
2141bf21cd93STycho Nightingale 	case AHCI_P_SERR:
2142bf21cd93STycho Nightingale 		p->serr &= ~value;
2143bf21cd93STycho Nightingale 		break;
2144bf21cd93STycho Nightingale 	case AHCI_P_SACT:
2145bf21cd93STycho Nightingale 		p->sact |= value;
2146bf21cd93STycho Nightingale 		break;
2147bf21cd93STycho Nightingale 	case AHCI_P_CI:
2148bf21cd93STycho Nightingale 		p->ci |= value;
2149bf21cd93STycho Nightingale 		ahci_handle_port(p);
2150bf21cd93STycho Nightingale 		break;
2151bf21cd93STycho Nightingale 	case AHCI_P_SNTF:
2152bf21cd93STycho Nightingale 	case AHCI_P_FBS:
2153bf21cd93STycho Nightingale 	default:
2154bf21cd93STycho Nightingale 		break;
2155bf21cd93STycho Nightingale 	}
2156bf21cd93STycho Nightingale }
2157bf21cd93STycho Nightingale 
2158bf21cd93STycho Nightingale static void
pci_ahci_host_write(struct pci_ahci_softc * sc,uint64_t offset,uint64_t value)2159bf21cd93STycho Nightingale pci_ahci_host_write(struct pci_ahci_softc *sc, uint64_t offset, uint64_t value)
2160bf21cd93STycho Nightingale {
2161154972afSPatrick Mooney 	DPRINTF("pci_ahci_host: write offset 0x%"PRIx64" value 0x%"PRIx64"",
2162bf21cd93STycho Nightingale 		offset, value);
2163bf21cd93STycho Nightingale 
2164bf21cd93STycho Nightingale 	switch (offset) {
2165bf21cd93STycho Nightingale 	case AHCI_CAP:
2166bf21cd93STycho Nightingale 	case AHCI_PI:
2167bf21cd93STycho Nightingale 	case AHCI_VS:
2168bf21cd93STycho Nightingale 	case AHCI_CAP2:
2169154972afSPatrick Mooney 		DPRINTF("pci_ahci_host: read only registers 0x%"PRIx64"", offset);
2170bf21cd93STycho Nightingale 		break;
2171bf21cd93STycho Nightingale 	case AHCI_GHC:
21724c87aefeSPatrick Mooney 		if (value & AHCI_GHC_HR) {
2173bf21cd93STycho Nightingale 			ahci_reset(sc);
21744c87aefeSPatrick Mooney 			break;
2175bf21cd93STycho Nightingale 		}
21764c87aefeSPatrick Mooney 		if (value & AHCI_GHC_IE)
21774c87aefeSPatrick Mooney 			sc->ghc |= AHCI_GHC_IE;
21784c87aefeSPatrick Mooney 		else
21794c87aefeSPatrick Mooney 			sc->ghc &= ~AHCI_GHC_IE;
21804c87aefeSPatrick Mooney 		ahci_generate_intr(sc, 0xffffffff);
2181bf21cd93STycho Nightingale 		break;
2182bf21cd93STycho Nightingale 	case AHCI_IS:
2183bf21cd93STycho Nightingale 		sc->is &= ~value;
21844c87aefeSPatrick Mooney 		ahci_generate_intr(sc, value);
2185bf21cd93STycho Nightingale 		break;
2186bf21cd93STycho Nightingale 	default:
2187bf21cd93STycho Nightingale 		break;
2188bf21cd93STycho Nightingale 	}
2189bf21cd93STycho Nightingale }
2190bf21cd93STycho Nightingale 
2191bf21cd93STycho Nightingale static void
pci_ahci_write(struct pci_devinst * pi,int baridx,uint64_t offset,int size,uint64_t value)2192*32640292SAndy Fiddaman pci_ahci_write(struct pci_devinst *pi, int baridx, uint64_t offset, int size,
219359d65d31SAndy Fiddaman     uint64_t value)
2194bf21cd93STycho Nightingale {
2195bf21cd93STycho Nightingale 	struct pci_ahci_softc *sc = pi->pi_arg;
2196bf21cd93STycho Nightingale 
2197bf21cd93STycho Nightingale 	assert(baridx == 5);
21984c87aefeSPatrick Mooney 	assert((offset % 4) == 0 && size == 4);
2199bf21cd93STycho Nightingale 
2200bf21cd93STycho Nightingale 	pthread_mutex_lock(&sc->mtx);
2201bf21cd93STycho Nightingale 
2202bf21cd93STycho Nightingale 	if (offset < AHCI_OFFSET)
2203bf21cd93STycho Nightingale 		pci_ahci_host_write(sc, offset, value);
220459d65d31SAndy Fiddaman 	else if (offset < (uint64_t)AHCI_OFFSET + sc->ports * AHCI_STEP)
2205bf21cd93STycho Nightingale 		pci_ahci_port_write(sc, offset, value);
2206bf21cd93STycho Nightingale 	else
2207154972afSPatrick Mooney 		WPRINTF("pci_ahci: unknown i/o write offset 0x%"PRIx64"", offset);
2208bf21cd93STycho Nightingale 
2209bf21cd93STycho Nightingale 	pthread_mutex_unlock(&sc->mtx);
2210bf21cd93STycho Nightingale }
2211bf21cd93STycho Nightingale 
2212bf21cd93STycho Nightingale static uint64_t
pci_ahci_host_read(struct pci_ahci_softc * sc,uint64_t offset)2213bf21cd93STycho Nightingale pci_ahci_host_read(struct pci_ahci_softc *sc, uint64_t offset)
2214bf21cd93STycho Nightingale {
2215bf21cd93STycho Nightingale 	uint32_t value;
2216bf21cd93STycho Nightingale 
2217bf21cd93STycho Nightingale 	switch (offset) {
2218bf21cd93STycho Nightingale 	case AHCI_CAP:
2219bf21cd93STycho Nightingale 	case AHCI_GHC:
2220bf21cd93STycho Nightingale 	case AHCI_IS:
2221bf21cd93STycho Nightingale 	case AHCI_PI:
2222bf21cd93STycho Nightingale 	case AHCI_VS:
2223bf21cd93STycho Nightingale 	case AHCI_CCCC:
2224bf21cd93STycho Nightingale 	case AHCI_CCCP:
2225bf21cd93STycho Nightingale 	case AHCI_EM_LOC:
2226bf21cd93STycho Nightingale 	case AHCI_EM_CTL:
2227bf21cd93STycho Nightingale 	case AHCI_CAP2:
2228bf21cd93STycho Nightingale 	{
2229bf21cd93STycho Nightingale 		uint32_t *p = &sc->cap;
2230bf21cd93STycho Nightingale 		p += (offset - AHCI_CAP) / sizeof(uint32_t);
2231bf21cd93STycho Nightingale 		value = *p;
2232bf21cd93STycho Nightingale 		break;
2233bf21cd93STycho Nightingale 	}
2234bf21cd93STycho Nightingale 	default:
2235bf21cd93STycho Nightingale 		value = 0;
2236bf21cd93STycho Nightingale 		break;
2237bf21cd93STycho Nightingale 	}
2238154972afSPatrick Mooney 	DPRINTF("pci_ahci_host: read offset 0x%"PRIx64" value 0x%x",
2239bf21cd93STycho Nightingale 		offset, value);
2240bf21cd93STycho Nightingale 
2241bf21cd93STycho Nightingale 	return (value);
2242bf21cd93STycho Nightingale }
2243bf21cd93STycho Nightingale 
2244bf21cd93STycho Nightingale static uint64_t
pci_ahci_port_read(struct pci_ahci_softc * sc,uint64_t offset)2245bf21cd93STycho Nightingale pci_ahci_port_read(struct pci_ahci_softc *sc, uint64_t offset)
2246bf21cd93STycho Nightingale {
2247bf21cd93STycho Nightingale 	uint32_t value;
2248bf21cd93STycho Nightingale 	int port = (offset - AHCI_OFFSET) / AHCI_STEP;
2249bf21cd93STycho Nightingale 	offset = (offset - AHCI_OFFSET) % AHCI_STEP;
2250bf21cd93STycho Nightingale 
2251bf21cd93STycho Nightingale 	switch (offset) {
2252bf21cd93STycho Nightingale 	case AHCI_P_CLB:
2253bf21cd93STycho Nightingale 	case AHCI_P_CLBU:
2254bf21cd93STycho Nightingale 	case AHCI_P_FB:
2255bf21cd93STycho Nightingale 	case AHCI_P_FBU:
2256bf21cd93STycho Nightingale 	case AHCI_P_IS:
2257bf21cd93STycho Nightingale 	case AHCI_P_IE:
2258bf21cd93STycho Nightingale 	case AHCI_P_CMD:
2259bf21cd93STycho Nightingale 	case AHCI_P_TFD:
2260bf21cd93STycho Nightingale 	case AHCI_P_SIG:
2261bf21cd93STycho Nightingale 	case AHCI_P_SSTS:
2262bf21cd93STycho Nightingale 	case AHCI_P_SCTL:
2263bf21cd93STycho Nightingale 	case AHCI_P_SERR:
2264bf21cd93STycho Nightingale 	case AHCI_P_SACT:
2265bf21cd93STycho Nightingale 	case AHCI_P_CI:
2266bf21cd93STycho Nightingale 	case AHCI_P_SNTF:
2267bf21cd93STycho Nightingale 	case AHCI_P_FBS:
2268bf21cd93STycho Nightingale 	{
2269bf21cd93STycho Nightingale 		uint32_t *p= &sc->port[port].clb;
2270bf21cd93STycho Nightingale 		p += (offset - AHCI_P_CLB) / sizeof(uint32_t);
2271bf21cd93STycho Nightingale 		value = *p;
2272bf21cd93STycho Nightingale 		break;
2273bf21cd93STycho Nightingale 	}
2274bf21cd93STycho Nightingale 	default:
2275bf21cd93STycho Nightingale 		value = 0;
2276bf21cd93STycho Nightingale 		break;
2277bf21cd93STycho Nightingale 	}
2278bf21cd93STycho Nightingale 
2279154972afSPatrick Mooney 	DPRINTF("pci_ahci_port %d: read offset 0x%"PRIx64" value 0x%x",
2280bf21cd93STycho Nightingale 		port, offset, value);
2281bf21cd93STycho Nightingale 
2282bf21cd93STycho Nightingale 	return value;
2283bf21cd93STycho Nightingale }
2284bf21cd93STycho Nightingale 
2285bf21cd93STycho Nightingale static uint64_t
pci_ahci_read(struct pci_devinst * pi,int baridx,uint64_t regoff,int size)2286*32640292SAndy Fiddaman pci_ahci_read(struct pci_devinst *pi, int baridx, uint64_t regoff, int size)
2287bf21cd93STycho Nightingale {
2288bf21cd93STycho Nightingale 	struct pci_ahci_softc *sc = pi->pi_arg;
22894c87aefeSPatrick Mooney 	uint64_t offset;
2290bf21cd93STycho Nightingale 	uint32_t value;
2291bf21cd93STycho Nightingale 
2292bf21cd93STycho Nightingale 	assert(baridx == 5);
22934c87aefeSPatrick Mooney 	assert(size == 1 || size == 2 || size == 4);
22944c87aefeSPatrick Mooney 	assert((regoff & (size - 1)) == 0);
2295bf21cd93STycho Nightingale 
2296bf21cd93STycho Nightingale 	pthread_mutex_lock(&sc->mtx);
2297bf21cd93STycho Nightingale 
22984c87aefeSPatrick Mooney 	offset = regoff & ~0x3;	    /* round down to a multiple of 4 bytes */
2299bf21cd93STycho Nightingale 	if (offset < AHCI_OFFSET)
2300bf21cd93STycho Nightingale 		value = pci_ahci_host_read(sc, offset);
230159d65d31SAndy Fiddaman 	else if (offset < (uint64_t)AHCI_OFFSET + sc->ports * AHCI_STEP)
2302bf21cd93STycho Nightingale 		value = pci_ahci_port_read(sc, offset);
2303bf21cd93STycho Nightingale 	else {
2304bf21cd93STycho Nightingale 		value = 0;
2305154972afSPatrick Mooney 		WPRINTF("pci_ahci: unknown i/o read offset 0x%"PRIx64"",
23064c87aefeSPatrick Mooney 		    regoff);
2307bf21cd93STycho Nightingale 	}
23084c87aefeSPatrick Mooney 	value >>= 8 * (regoff & 0x3);
2309bf21cd93STycho Nightingale 
2310bf21cd93STycho Nightingale 	pthread_mutex_unlock(&sc->mtx);
2311bf21cd93STycho Nightingale 
2312bf21cd93STycho Nightingale 	return (value);
2313bf21cd93STycho Nightingale }
2314bf21cd93STycho Nightingale 
23152b948146SAndy Fiddaman /*
23162b948146SAndy Fiddaman  * Each AHCI controller has a "port" node which contains nodes for
23172b948146SAndy Fiddaman  * each port named after the decimal number of the port (no leading
23182b948146SAndy Fiddaman  * zeroes).  Port nodes contain a "type" ("hd" or "cd"), as well as
23192b948146SAndy Fiddaman  * options for blockif.  For example:
23202b948146SAndy Fiddaman  *
23212b948146SAndy Fiddaman  * pci.0.1.0
23222b948146SAndy Fiddaman  *          .device="ahci"
23232b948146SAndy Fiddaman  *          .port
23242b948146SAndy Fiddaman  *               .0
23252b948146SAndy Fiddaman  *                 .type="hd"
23262b948146SAndy Fiddaman  *                 .path="/path/to/image"
23272b948146SAndy Fiddaman  */
2328bf21cd93STycho Nightingale static int
pci_ahci_legacy_config_port(nvlist_t * nvl,int port,const char * type,const char * opts)23292b948146SAndy Fiddaman pci_ahci_legacy_config_port(nvlist_t *nvl, int port, const char *type,
23302b948146SAndy Fiddaman     const char *opts)
23312b948146SAndy Fiddaman {
23322b948146SAndy Fiddaman 	char node_name[sizeof("XX")];
23332b948146SAndy Fiddaman 	nvlist_t *port_nvl;
23342b948146SAndy Fiddaman 
23352b948146SAndy Fiddaman 	snprintf(node_name, sizeof(node_name), "%d", port);
23362b948146SAndy Fiddaman 	port_nvl = create_relative_config_node(nvl, node_name);
23372b948146SAndy Fiddaman 	set_config_value_node(port_nvl, "type", type);
23382b948146SAndy Fiddaman 	return (blockif_legacy_config(port_nvl, opts));
23392b948146SAndy Fiddaman }
23402b948146SAndy Fiddaman 
23412b948146SAndy Fiddaman static int
pci_ahci_legacy_config(nvlist_t * nvl,const char * opts)23422b948146SAndy Fiddaman pci_ahci_legacy_config(nvlist_t *nvl, const char *opts)
23432b948146SAndy Fiddaman {
23442b948146SAndy Fiddaman 	nvlist_t *ports_nvl;
23452b948146SAndy Fiddaman 	const char *type;
23462b948146SAndy Fiddaman 	char *next, *next2, *str, *tofree;
23472b948146SAndy Fiddaman 	int p, ret;
23482b948146SAndy Fiddaman 
23492b948146SAndy Fiddaman 	if (opts == NULL)
23502b948146SAndy Fiddaman 		return (0);
23512b948146SAndy Fiddaman 
23522b948146SAndy Fiddaman 	ports_nvl = create_relative_config_node(nvl, "port");
23532b948146SAndy Fiddaman 	ret = 1;
23542b948146SAndy Fiddaman 	tofree = str = strdup(opts);
23552b948146SAndy Fiddaman 	for (p = 0; p < MAX_PORTS && str != NULL; p++, str = next) {
23562b948146SAndy Fiddaman 		/* Identify and cut off type of present port. */
23572b948146SAndy Fiddaman 		if (strncmp(str, "hd:", 3) == 0) {
23582b948146SAndy Fiddaman 			type = "hd";
23592b948146SAndy Fiddaman 			str += 3;
23602b948146SAndy Fiddaman 		} else if (strncmp(str, "cd:", 3) == 0) {
23612b948146SAndy Fiddaman 			type = "cd";
23622b948146SAndy Fiddaman 			str += 3;
23632b948146SAndy Fiddaman 		} else
23642b948146SAndy Fiddaman 			type = NULL;
23652b948146SAndy Fiddaman 
23662b948146SAndy Fiddaman 		/* Find and cut off the next port options. */
23672b948146SAndy Fiddaman 		next = strstr(str, ",hd:");
23682b948146SAndy Fiddaman 		next2 = strstr(str, ",cd:");
23692b948146SAndy Fiddaman 		if (next == NULL || (next2 != NULL && next2 < next))
23702b948146SAndy Fiddaman 			next = next2;
23712b948146SAndy Fiddaman 		if (next != NULL) {
23722b948146SAndy Fiddaman 			next[0] = 0;
23732b948146SAndy Fiddaman 			next++;
23742b948146SAndy Fiddaman 		}
23752b948146SAndy Fiddaman 
23762b948146SAndy Fiddaman 		if (str[0] == 0)
23772b948146SAndy Fiddaman 			continue;
23782b948146SAndy Fiddaman 
23792b948146SAndy Fiddaman 		if (type == NULL) {
23802b948146SAndy Fiddaman 			EPRINTLN("Missing or invalid type for port %d: \"%s\"",
23812b948146SAndy Fiddaman 			    p, str);
23822b948146SAndy Fiddaman 			goto out;
23832b948146SAndy Fiddaman 		}
23842b948146SAndy Fiddaman 
23852b948146SAndy Fiddaman 		if (pci_ahci_legacy_config_port(ports_nvl, p, type, str) != 0)
23862b948146SAndy Fiddaman 			goto out;
23872b948146SAndy Fiddaman 	}
23882b948146SAndy Fiddaman 	ret = 0;
23892b948146SAndy Fiddaman out:
23902b948146SAndy Fiddaman 	free(tofree);
23912b948146SAndy Fiddaman 	return (ret);
23922b948146SAndy Fiddaman }
23932b948146SAndy Fiddaman 
23942b948146SAndy Fiddaman static int
pci_ahci_cd_legacy_config(nvlist_t * nvl,const char * opts)23952b948146SAndy Fiddaman pci_ahci_cd_legacy_config(nvlist_t *nvl, const char *opts)
23962b948146SAndy Fiddaman {
23972b948146SAndy Fiddaman 	nvlist_t *ports_nvl;
23982b948146SAndy Fiddaman 
23992b948146SAndy Fiddaman 	ports_nvl = create_relative_config_node(nvl, "port");
24002b948146SAndy Fiddaman 	return (pci_ahci_legacy_config_port(ports_nvl, 0, "cd", opts));
24012b948146SAndy Fiddaman }
24022b948146SAndy Fiddaman 
24032b948146SAndy Fiddaman static int
pci_ahci_hd_legacy_config(nvlist_t * nvl,const char * opts)24042b948146SAndy Fiddaman pci_ahci_hd_legacy_config(nvlist_t *nvl, const char *opts)
24052b948146SAndy Fiddaman {
24062b948146SAndy Fiddaman 	nvlist_t *ports_nvl;
24072b948146SAndy Fiddaman 
24082b948146SAndy Fiddaman 	ports_nvl = create_relative_config_node(nvl, "port");
24092b948146SAndy Fiddaman 	return (pci_ahci_legacy_config_port(ports_nvl, 0, "hd", opts));
24102b948146SAndy Fiddaman }
24112b948146SAndy Fiddaman 
24122b948146SAndy Fiddaman static int
pci_ahci_init(struct pci_devinst * pi,nvlist_t * nvl)2413*32640292SAndy Fiddaman pci_ahci_init(struct pci_devinst *pi, nvlist_t *nvl)
2414bf21cd93STycho Nightingale {
241559d65d31SAndy Fiddaman 	char bident[sizeof("XXX:XXX:XXX")];
24162b948146SAndy Fiddaman 	char node_name[sizeof("XX")];
2417bf21cd93STycho Nightingale 	struct blockif_ctxt *bctxt;
2418bf21cd93STycho Nightingale 	struct pci_ahci_softc *sc;
24192b948146SAndy Fiddaman 	int atapi, ret, slots, p;
24204c87aefeSPatrick Mooney 	MD5_CTX mdctx;
24214c87aefeSPatrick Mooney 	u_char digest[16];
24222b948146SAndy Fiddaman 	const char *path, *type, *value;
24232b948146SAndy Fiddaman 	nvlist_t *ports_nvl, *port_nvl;
2424bf21cd93STycho Nightingale 
2425bf21cd93STycho Nightingale 	ret = 0;
2426bf21cd93STycho Nightingale 
2427bf21cd93STycho Nightingale #ifdef AHCI_DEBUG
2428bf21cd93STycho Nightingale 	dbg = fopen("/tmp/log", "w+");
2429bf21cd93STycho Nightingale #endif
2430bf21cd93STycho Nightingale 
2431bf21cd93STycho Nightingale 	sc = calloc(1, sizeof(struct pci_ahci_softc));
2432bf21cd93STycho Nightingale 	pi->pi_arg = sc;
2433bf21cd93STycho Nightingale 	sc->asc_pi = pi;
24344c87aefeSPatrick Mooney 	pthread_mutex_init(&sc->mtx, NULL);
24354c87aefeSPatrick Mooney 	sc->ports = 0;
24364c87aefeSPatrick Mooney 	sc->pi = 0;
24374c87aefeSPatrick Mooney 	slots = 32;
24384c87aefeSPatrick Mooney 
24392b948146SAndy Fiddaman 	ports_nvl = find_relative_config_node(nvl, "port");
2440d7b72f7bSAndy Fiddaman 	for (p = 0; ports_nvl != NULL && p < MAX_PORTS; p++) {
24416960cd89SAndy Fiddaman 		struct ata_params *ata_ident = &sc->port[p].ata_ident;
24422b948146SAndy Fiddaman 		char ident[AHCI_PORT_IDENT];
24436960cd89SAndy Fiddaman 
24442b948146SAndy Fiddaman 		snprintf(node_name, sizeof(node_name), "%d", p);
24452b948146SAndy Fiddaman 		port_nvl = find_relative_config_node(ports_nvl, node_name);
24462b948146SAndy Fiddaman 		if (port_nvl == NULL)
24474c87aefeSPatrick Mooney 			continue;
2448bf21cd93STycho Nightingale 
24492b948146SAndy Fiddaman 		type = get_config_value_node(port_nvl, "type");
24502b948146SAndy Fiddaman 		if (type == NULL)
24512b948146SAndy Fiddaman 			continue;
24526960cd89SAndy Fiddaman 
24532b948146SAndy Fiddaman 		if (strcmp(type, "hd") == 0)
24542b948146SAndy Fiddaman 			atapi = 0;
24552b948146SAndy Fiddaman 		else
24562b948146SAndy Fiddaman 			atapi = 1;
24576960cd89SAndy Fiddaman 
2458bf21cd93STycho Nightingale 		/*
24594c87aefeSPatrick Mooney 		 * Attempt to open the backing image. Use the PCI slot/func
24604c87aefeSPatrick Mooney 		 * and the port number for the identifier string.
2461bf21cd93STycho Nightingale 		 */
246259d65d31SAndy Fiddaman 		snprintf(bident, sizeof(bident), "%u:%u:%u", pi->pi_slot,
24634c87aefeSPatrick Mooney 		    pi->pi_func, p);
24646960cd89SAndy Fiddaman 
24652b948146SAndy Fiddaman 		bctxt = blockif_open(port_nvl, bident);
2466bf21cd93STycho Nightingale 		if (bctxt == NULL) {
24674c87aefeSPatrick Mooney 			sc->ports = p;
2468bf21cd93STycho Nightingale 			ret = 1;
2469bf21cd93STycho Nightingale 			goto open_fail;
2470bf21cd93STycho Nightingale 		}
2471*32640292SAndy Fiddaman 
2472*32640292SAndy Fiddaman 		ret = blockif_add_boot_device(pi, bctxt);
2473*32640292SAndy Fiddaman 		if (ret) {
2474*32640292SAndy Fiddaman 			sc->ports = p;
2475*32640292SAndy Fiddaman 			goto open_fail;
2476*32640292SAndy Fiddaman 		}
2477*32640292SAndy Fiddaman 
24784c87aefeSPatrick Mooney 		sc->port[p].bctx = bctxt;
24794c87aefeSPatrick Mooney 		sc->port[p].pr_sc = sc;
24804c87aefeSPatrick Mooney 		sc->port[p].port = p;
24814c87aefeSPatrick Mooney 		sc->port[p].atapi = atapi;
24824c87aefeSPatrick Mooney 
24832b948146SAndy Fiddaman 		/*
24842b948146SAndy Fiddaman 		 * Create an identifier for the backing file.
24852b948146SAndy Fiddaman 		 * Use parts of the md5 sum of the filename
24862b948146SAndy Fiddaman 		 */
24872b948146SAndy Fiddaman 		path = get_config_value_node(port_nvl, "path");
24882b948146SAndy Fiddaman 		MD5Init(&mdctx);
24892b948146SAndy Fiddaman 		MD5Update(&mdctx, path, strlen(path));
24902b948146SAndy Fiddaman 		MD5Final(digest, &mdctx);
24912b948146SAndy Fiddaman 		snprintf(ident, AHCI_PORT_IDENT,
24922b948146SAndy Fiddaman 			"BHYVE-%02X%02X-%02X%02X-%02X%02X",
24932b948146SAndy Fiddaman 			digest[0], digest[1], digest[2], digest[3], digest[4],
24942b948146SAndy Fiddaman 			digest[5]);
24952b948146SAndy Fiddaman 
24962b948146SAndy Fiddaman 		memset(ata_ident, 0, sizeof(struct ata_params));
24972b948146SAndy Fiddaman 		ata_string((uint8_t*)&ata_ident->serial, ident, 20);
24982b948146SAndy Fiddaman 		ata_string((uint8_t*)&ata_ident->revision, "001", 8);
24992b948146SAndy Fiddaman 		if (atapi)
25002b948146SAndy Fiddaman 			ata_string((uint8_t*)&ata_ident->model, "BHYVE SATA DVD ROM", 40);
25012b948146SAndy Fiddaman 		else
25022b948146SAndy Fiddaman 			ata_string((uint8_t*)&ata_ident->model, "BHYVE SATA DISK", 40);
25032b948146SAndy Fiddaman 		value = get_config_value_node(port_nvl, "nmrr");
25042b948146SAndy Fiddaman 		if (value != NULL)
25052b948146SAndy Fiddaman 			ata_ident->media_rotation_rate = atoi(value);
25062b948146SAndy Fiddaman 		value = get_config_value_node(port_nvl, "ser");
25072b948146SAndy Fiddaman 		if (value != NULL)
25082b948146SAndy Fiddaman 			ata_string((uint8_t*)(&ata_ident->serial), value, 20);
25092b948146SAndy Fiddaman 		value = get_config_value_node(port_nvl, "rev");
25102b948146SAndy Fiddaman 		if (value != NULL)
25112b948146SAndy Fiddaman 			ata_string((uint8_t*)(&ata_ident->revision), value, 8);
25122b948146SAndy Fiddaman 		value = get_config_value_node(port_nvl, "model");
25132b948146SAndy Fiddaman 		if (value != NULL)
25142b948146SAndy Fiddaman 			ata_string((uint8_t*)(&ata_ident->model), value, 40);
25152b948146SAndy Fiddaman 		ata_identify_init(&sc->port[p], atapi);
25162b948146SAndy Fiddaman 
25174c87aefeSPatrick Mooney #ifndef __FreeBSD__
25184c87aefeSPatrick Mooney 		/*
25194c87aefeSPatrick Mooney 		 * Attempt to enable the write cache for this device, as the
25204c87aefeSPatrick Mooney 		 * guest will issue FLUSH commands when it requires durability.
25214c87aefeSPatrick Mooney 		 *
25224c87aefeSPatrick Mooney 		 * Failure here is fine, since an always-sync device will not
25234c87aefeSPatrick Mooney 		 * have an impact on correctness.
25244c87aefeSPatrick Mooney 		 */
25254c87aefeSPatrick Mooney 		(void) blockif_set_wce(bctxt, 1);
25264c87aefeSPatrick Mooney #endif
25274c87aefeSPatrick Mooney 
2528bf21cd93STycho Nightingale 		/*
2529bf21cd93STycho Nightingale 		 * Allocate blockif request structures and add them
2530bf21cd93STycho Nightingale 		 * to the free list
2531bf21cd93STycho Nightingale 		 */
25324c87aefeSPatrick Mooney 		pci_ahci_ioreq_init(&sc->port[p]);
2533bf21cd93STycho Nightingale 
25344c87aefeSPatrick Mooney 		sc->pi |= (1 << p);
25354c87aefeSPatrick Mooney 		if (sc->port[p].ioqsz < slots)
25364c87aefeSPatrick Mooney 			slots = sc->port[p].ioqsz;
25374c87aefeSPatrick Mooney 	}
25384c87aefeSPatrick Mooney 	sc->ports = p;
2539bf21cd93STycho Nightingale 
2540bf21cd93STycho Nightingale 	/* Intel ICH8 AHCI */
2541bf21cd93STycho Nightingale 	--slots;
25424c87aefeSPatrick Mooney 	if (sc->ports < DEF_PORTS)
25434c87aefeSPatrick Mooney 		sc->ports = DEF_PORTS;
2544bf21cd93STycho Nightingale 	sc->cap = AHCI_CAP_64BIT | AHCI_CAP_SNCQ | AHCI_CAP_SSNTF |
2545bf21cd93STycho Nightingale 	    AHCI_CAP_SMPS | AHCI_CAP_SSS | AHCI_CAP_SALP |
2546bf21cd93STycho Nightingale 	    AHCI_CAP_SAL | AHCI_CAP_SCLO | (0x3 << AHCI_CAP_ISS_SHIFT)|
2547bf21cd93STycho Nightingale 	    AHCI_CAP_PMD | AHCI_CAP_SSC | AHCI_CAP_PSC |
2548bf21cd93STycho Nightingale 	    (slots << AHCI_CAP_NCS_SHIFT) | AHCI_CAP_SXS | (sc->ports - 1);
2549bf21cd93STycho Nightingale 
2550bf21cd93STycho Nightingale 	sc->vs = 0x10300;
2551bf21cd93STycho Nightingale 	sc->cap2 = AHCI_CAP2_APST;
2552bf21cd93STycho Nightingale 	ahci_reset(sc);
2553bf21cd93STycho Nightingale 
2554bf21cd93STycho Nightingale 	pci_set_cfgdata16(pi, PCIR_DEVICE, 0x2821);
2555bf21cd93STycho Nightingale 	pci_set_cfgdata16(pi, PCIR_VENDOR, 0x8086);
2556bf21cd93STycho Nightingale 	pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_STORAGE);
2557bf21cd93STycho Nightingale 	pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_STORAGE_SATA);
2558bf21cd93STycho Nightingale 	pci_set_cfgdata8(pi, PCIR_PROGIF, PCIP_STORAGE_SATA_AHCI_1_0);
25594c87aefeSPatrick Mooney 	p = MIN(sc->ports, 16);
25604c87aefeSPatrick Mooney 	p = flsl(p) - ((p & (p - 1)) ? 0 : 1);
25614c87aefeSPatrick Mooney 	pci_emul_add_msicap(pi, 1 << p);
2562bf21cd93STycho Nightingale 	pci_emul_alloc_bar(pi, 5, PCIBAR_MEM32,
2563bf21cd93STycho Nightingale 	    AHCI_OFFSET + sc->ports * AHCI_STEP);
2564bf21cd93STycho Nightingale 
2565bf21cd93STycho Nightingale 	pci_lintr_request(pi);
2566bf21cd93STycho Nightingale 
2567bf21cd93STycho Nightingale open_fail:
2568bf21cd93STycho Nightingale 	if (ret) {
25694c87aefeSPatrick Mooney 		for (p = 0; p < sc->ports; p++) {
25704c87aefeSPatrick Mooney 			if (sc->port[p].bctx != NULL)
25714c87aefeSPatrick Mooney 				blockif_close(sc->port[p].bctx);
25724c87aefeSPatrick Mooney 		}
2573bf21cd93STycho Nightingale 		free(sc);
2574bf21cd93STycho Nightingale 	}
2575bf21cd93STycho Nightingale 
2576bf21cd93STycho Nightingale 	return (ret);
2577bf21cd93STycho Nightingale }
2578bf21cd93STycho Nightingale 
2579bf21cd93STycho Nightingale /*
2580bf21cd93STycho Nightingale  * Use separate emulation names to distinguish drive and atapi devices
2581bf21cd93STycho Nightingale  */
25824f3f3e9aSAndy Fiddaman static const struct pci_devemu pci_de_ahci = {
25834c87aefeSPatrick Mooney 	.pe_emu =	"ahci",
25842b948146SAndy Fiddaman 	.pe_init =	pci_ahci_init,
25852b948146SAndy Fiddaman 	.pe_legacy_config = pci_ahci_legacy_config,
25864c87aefeSPatrick Mooney 	.pe_barwrite =	pci_ahci_write,
25872b948146SAndy Fiddaman 	.pe_barread =	pci_ahci_read,
25884c87aefeSPatrick Mooney };
25894c87aefeSPatrick Mooney PCI_EMUL_SET(pci_de_ahci);
25904c87aefeSPatrick Mooney 
25914f3f3e9aSAndy Fiddaman static const struct pci_devemu pci_de_ahci_hd = {
2592bf21cd93STycho Nightingale 	.pe_emu =	"ahci-hd",
25932b948146SAndy Fiddaman 	.pe_legacy_config = pci_ahci_hd_legacy_config,
25942b948146SAndy Fiddaman 	.pe_alias =	"ahci",
2595bf21cd93STycho Nightingale };
2596bf21cd93STycho Nightingale PCI_EMUL_SET(pci_de_ahci_hd);
2597bf21cd93STycho Nightingale 
25984f3f3e9aSAndy Fiddaman static const struct pci_devemu pci_de_ahci_cd = {
2599bf21cd93STycho Nightingale 	.pe_emu =	"ahci-cd",
26002b948146SAndy Fiddaman 	.pe_legacy_config = pci_ahci_cd_legacy_config,
26012b948146SAndy Fiddaman 	.pe_alias =	"ahci",
2602bf21cd93STycho Nightingale };
2603bf21cd93STycho Nightingale PCI_EMUL_SET(pci_de_ahci_cd);
2604