1*5c4a5fe1SAndy Fiddaman /*-
2*5c4a5fe1SAndy Fiddaman * SPDX-License-Identifier: BSD-2-Clause
3*5c4a5fe1SAndy Fiddaman *
4*5c4a5fe1SAndy Fiddaman * Copyright (c) 2013 Zhixiang Yu <zcore@freebsd.org>
5*5c4a5fe1SAndy Fiddaman * Copyright (c) 2015-2016 Alexander Motin <mav@FreeBSD.org>
6*5c4a5fe1SAndy Fiddaman * All rights reserved.
7*5c4a5fe1SAndy Fiddaman *
8*5c4a5fe1SAndy Fiddaman * Redistribution and use in source and binary forms, with or without
9*5c4a5fe1SAndy Fiddaman * modification, are permitted provided that the following conditions
10*5c4a5fe1SAndy Fiddaman * are met:
11*5c4a5fe1SAndy Fiddaman * 1. Redistributions of source code must retain the above copyright
12*5c4a5fe1SAndy Fiddaman * notice, this list of conditions and the following disclaimer.
13*5c4a5fe1SAndy Fiddaman * 2. Redistributions in binary form must reproduce the above copyright
14*5c4a5fe1SAndy Fiddaman * notice, this list of conditions and the following disclaimer in the
15*5c4a5fe1SAndy Fiddaman * documentation and/or other materials provided with the distribution.
16*5c4a5fe1SAndy Fiddaman *
17*5c4a5fe1SAndy Fiddaman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
18*5c4a5fe1SAndy Fiddaman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*5c4a5fe1SAndy Fiddaman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*5c4a5fe1SAndy Fiddaman * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*5c4a5fe1SAndy Fiddaman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*5c4a5fe1SAndy Fiddaman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*5c4a5fe1SAndy Fiddaman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*5c4a5fe1SAndy Fiddaman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*5c4a5fe1SAndy Fiddaman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*5c4a5fe1SAndy Fiddaman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*5c4a5fe1SAndy Fiddaman * SUCH DAMAGE.
28*5c4a5fe1SAndy Fiddaman */
29*5c4a5fe1SAndy Fiddaman
30*5c4a5fe1SAndy Fiddaman
31*5c4a5fe1SAndy Fiddaman #include <sys/param.h>
32*5c4a5fe1SAndy Fiddaman #include <sys/linker_set.h>
33*5c4a5fe1SAndy Fiddaman #include <sys/stat.h>
34*5c4a5fe1SAndy Fiddaman #include <sys/uio.h>
35*5c4a5fe1SAndy Fiddaman #include <sys/ioctl.h>
36*5c4a5fe1SAndy Fiddaman #include <sys/disk.h>
37*5c4a5fe1SAndy Fiddaman #include <sys/ata.h>
38*5c4a5fe1SAndy Fiddaman #include <sys/endian.h>
39*5c4a5fe1SAndy Fiddaman
40*5c4a5fe1SAndy Fiddaman #include <errno.h>
41*5c4a5fe1SAndy Fiddaman #include <fcntl.h>
42*5c4a5fe1SAndy Fiddaman #include <stdio.h>
43*5c4a5fe1SAndy Fiddaman #include <stdlib.h>
44*5c4a5fe1SAndy Fiddaman #include <stdint.h>
45*5c4a5fe1SAndy Fiddaman #include <string.h>
46*5c4a5fe1SAndy Fiddaman #include <strings.h>
47*5c4a5fe1SAndy Fiddaman #include <unistd.h>
48*5c4a5fe1SAndy Fiddaman #include <assert.h>
49*5c4a5fe1SAndy Fiddaman #include <pthread.h>
50*5c4a5fe1SAndy Fiddaman #include <pthread_np.h>
51*5c4a5fe1SAndy Fiddaman #include <inttypes.h>
52*5c4a5fe1SAndy Fiddaman #include <md5.h>
53*5c4a5fe1SAndy Fiddaman
54*5c4a5fe1SAndy Fiddaman #include "bhyverun.h"
55*5c4a5fe1SAndy Fiddaman #include "config.h"
56*5c4a5fe1SAndy Fiddaman #include "debug.h"
57*5c4a5fe1SAndy Fiddaman #include "pci_emul.h"
58*5c4a5fe1SAndy Fiddaman #include "ahci.h"
59*5c4a5fe1SAndy Fiddaman #include "block_if.h"
60*5c4a5fe1SAndy Fiddaman
61*5c4a5fe1SAndy Fiddaman #define DEF_PORTS 6 /* Intel ICH8 AHCI supports 6 ports */
62*5c4a5fe1SAndy Fiddaman #define MAX_PORTS 32 /* AHCI supports 32 ports */
63*5c4a5fe1SAndy Fiddaman
64*5c4a5fe1SAndy Fiddaman #define PxSIG_ATA 0x00000101 /* ATA drive */
65*5c4a5fe1SAndy Fiddaman #define PxSIG_ATAPI 0xeb140101 /* ATAPI drive */
66*5c4a5fe1SAndy Fiddaman
67*5c4a5fe1SAndy Fiddaman enum sata_fis_type {
68*5c4a5fe1SAndy Fiddaman FIS_TYPE_REGH2D = 0x27, /* Register FIS - host to device */
69*5c4a5fe1SAndy Fiddaman FIS_TYPE_REGD2H = 0x34, /* Register FIS - device to host */
70*5c4a5fe1SAndy Fiddaman FIS_TYPE_DMAACT = 0x39, /* DMA activate FIS - device to host */
71*5c4a5fe1SAndy Fiddaman FIS_TYPE_DMASETUP = 0x41, /* DMA setup FIS - bidirectional */
72*5c4a5fe1SAndy Fiddaman FIS_TYPE_DATA = 0x46, /* Data FIS - bidirectional */
73*5c4a5fe1SAndy Fiddaman FIS_TYPE_BIST = 0x58, /* BIST activate FIS - bidirectional */
74*5c4a5fe1SAndy Fiddaman FIS_TYPE_PIOSETUP = 0x5F, /* PIO setup FIS - device to host */
75*5c4a5fe1SAndy Fiddaman FIS_TYPE_SETDEVBITS = 0xA1, /* Set dev bits FIS - device to host */
76*5c4a5fe1SAndy Fiddaman };
77*5c4a5fe1SAndy Fiddaman
78*5c4a5fe1SAndy Fiddaman /*
79*5c4a5fe1SAndy Fiddaman * SCSI opcodes
80*5c4a5fe1SAndy Fiddaman */
81*5c4a5fe1SAndy Fiddaman #define TEST_UNIT_READY 0x00
82*5c4a5fe1SAndy Fiddaman #define REQUEST_SENSE 0x03
83*5c4a5fe1SAndy Fiddaman #define INQUIRY 0x12
84*5c4a5fe1SAndy Fiddaman #define START_STOP_UNIT 0x1B
85*5c4a5fe1SAndy Fiddaman #define PREVENT_ALLOW 0x1E
86*5c4a5fe1SAndy Fiddaman #define READ_CAPACITY 0x25
87*5c4a5fe1SAndy Fiddaman #define READ_10 0x28
88*5c4a5fe1SAndy Fiddaman #define POSITION_TO_ELEMENT 0x2B
89*5c4a5fe1SAndy Fiddaman #define READ_TOC 0x43
90*5c4a5fe1SAndy Fiddaman #define GET_EVENT_STATUS_NOTIFICATION 0x4A
91*5c4a5fe1SAndy Fiddaman #define MODE_SENSE_10 0x5A
92*5c4a5fe1SAndy Fiddaman #define REPORT_LUNS 0xA0
93*5c4a5fe1SAndy Fiddaman #define READ_12 0xA8
94*5c4a5fe1SAndy Fiddaman #define READ_CD 0xBE
95*5c4a5fe1SAndy Fiddaman
96*5c4a5fe1SAndy Fiddaman /*
97*5c4a5fe1SAndy Fiddaman * SCSI mode page codes
98*5c4a5fe1SAndy Fiddaman */
99*5c4a5fe1SAndy Fiddaman #define MODEPAGE_RW_ERROR_RECOVERY 0x01
100*5c4a5fe1SAndy Fiddaman #define MODEPAGE_CD_CAPABILITIES 0x2A
101*5c4a5fe1SAndy Fiddaman
102*5c4a5fe1SAndy Fiddaman /*
103*5c4a5fe1SAndy Fiddaman * ATA commands
104*5c4a5fe1SAndy Fiddaman */
105*5c4a5fe1SAndy Fiddaman #define ATA_SF_ENAB_SATA_SF 0x10
106*5c4a5fe1SAndy Fiddaman #define ATA_SATA_SF_AN 0x05
107*5c4a5fe1SAndy Fiddaman #define ATA_SF_DIS_SATA_SF 0x90
108*5c4a5fe1SAndy Fiddaman
109*5c4a5fe1SAndy Fiddaman /*
110*5c4a5fe1SAndy Fiddaman * Debug printf
111*5c4a5fe1SAndy Fiddaman */
112*5c4a5fe1SAndy Fiddaman #ifdef AHCI_DEBUG
113*5c4a5fe1SAndy Fiddaman static FILE *dbg;
114*5c4a5fe1SAndy Fiddaman #define DPRINTF(format, arg...) do{fprintf(dbg, format, ##arg);fflush(dbg);}while(0)
115*5c4a5fe1SAndy Fiddaman #else
116*5c4a5fe1SAndy Fiddaman #define DPRINTF(format, arg...)
117*5c4a5fe1SAndy Fiddaman #endif
118*5c4a5fe1SAndy Fiddaman
119*5c4a5fe1SAndy Fiddaman #define AHCI_PORT_IDENT 20 + 1
120*5c4a5fe1SAndy Fiddaman
121*5c4a5fe1SAndy Fiddaman struct ahci_ioreq {
122*5c4a5fe1SAndy Fiddaman struct blockif_req io_req;
123*5c4a5fe1SAndy Fiddaman struct ahci_port *io_pr;
124*5c4a5fe1SAndy Fiddaman STAILQ_ENTRY(ahci_ioreq) io_flist;
125*5c4a5fe1SAndy Fiddaman TAILQ_ENTRY(ahci_ioreq) io_blist;
126*5c4a5fe1SAndy Fiddaman uint8_t *cfis;
127*5c4a5fe1SAndy Fiddaman uint32_t len;
128*5c4a5fe1SAndy Fiddaman uint32_t done;
129*5c4a5fe1SAndy Fiddaman int slot;
130*5c4a5fe1SAndy Fiddaman int more;
131*5c4a5fe1SAndy Fiddaman };
132*5c4a5fe1SAndy Fiddaman
133*5c4a5fe1SAndy Fiddaman struct ahci_port {
134*5c4a5fe1SAndy Fiddaman struct blockif_ctxt *bctx;
135*5c4a5fe1SAndy Fiddaman struct pci_ahci_softc *pr_sc;
136*5c4a5fe1SAndy Fiddaman struct ata_params ata_ident;
137*5c4a5fe1SAndy Fiddaman uint8_t *cmd_lst;
138*5c4a5fe1SAndy Fiddaman uint8_t *rfis;
139*5c4a5fe1SAndy Fiddaman int port;
140*5c4a5fe1SAndy Fiddaman int atapi;
141*5c4a5fe1SAndy Fiddaman int reset;
142*5c4a5fe1SAndy Fiddaman int waitforclear;
143*5c4a5fe1SAndy Fiddaman int mult_sectors;
144*5c4a5fe1SAndy Fiddaman uint8_t xfermode;
145*5c4a5fe1SAndy Fiddaman uint8_t err_cfis[20];
146*5c4a5fe1SAndy Fiddaman uint8_t sense_key;
147*5c4a5fe1SAndy Fiddaman uint8_t asc;
148*5c4a5fe1SAndy Fiddaman u_int ccs;
149*5c4a5fe1SAndy Fiddaman uint32_t pending;
150*5c4a5fe1SAndy Fiddaman
151*5c4a5fe1SAndy Fiddaman uint32_t clb;
152*5c4a5fe1SAndy Fiddaman uint32_t clbu;
153*5c4a5fe1SAndy Fiddaman uint32_t fb;
154*5c4a5fe1SAndy Fiddaman uint32_t fbu;
155*5c4a5fe1SAndy Fiddaman uint32_t is;
156*5c4a5fe1SAndy Fiddaman uint32_t ie;
157*5c4a5fe1SAndy Fiddaman uint32_t cmd;
158*5c4a5fe1SAndy Fiddaman uint32_t unused0;
159*5c4a5fe1SAndy Fiddaman uint32_t tfd;
160*5c4a5fe1SAndy Fiddaman uint32_t sig;
161*5c4a5fe1SAndy Fiddaman uint32_t ssts;
162*5c4a5fe1SAndy Fiddaman uint32_t sctl;
163*5c4a5fe1SAndy Fiddaman uint32_t serr;
164*5c4a5fe1SAndy Fiddaman uint32_t sact;
165*5c4a5fe1SAndy Fiddaman uint32_t ci;
166*5c4a5fe1SAndy Fiddaman uint32_t sntf;
167*5c4a5fe1SAndy Fiddaman uint32_t fbs;
168*5c4a5fe1SAndy Fiddaman
169*5c4a5fe1SAndy Fiddaman /*
170*5c4a5fe1SAndy Fiddaman * i/o request info
171*5c4a5fe1SAndy Fiddaman */
172*5c4a5fe1SAndy Fiddaman struct ahci_ioreq *ioreq;
173*5c4a5fe1SAndy Fiddaman int ioqsz;
174*5c4a5fe1SAndy Fiddaman STAILQ_HEAD(ahci_fhead, ahci_ioreq) iofhd;
175*5c4a5fe1SAndy Fiddaman TAILQ_HEAD(ahci_bhead, ahci_ioreq) iobhd;
176*5c4a5fe1SAndy Fiddaman };
177*5c4a5fe1SAndy Fiddaman
178*5c4a5fe1SAndy Fiddaman struct ahci_cmd_hdr {
179*5c4a5fe1SAndy Fiddaman uint16_t flags;
180*5c4a5fe1SAndy Fiddaman uint16_t prdtl;
181*5c4a5fe1SAndy Fiddaman uint32_t prdbc;
182*5c4a5fe1SAndy Fiddaman uint64_t ctba;
183*5c4a5fe1SAndy Fiddaman uint32_t reserved[4];
184*5c4a5fe1SAndy Fiddaman };
185*5c4a5fe1SAndy Fiddaman
186*5c4a5fe1SAndy Fiddaman struct ahci_prdt_entry {
187*5c4a5fe1SAndy Fiddaman uint64_t dba;
188*5c4a5fe1SAndy Fiddaman uint32_t reserved;
189*5c4a5fe1SAndy Fiddaman #define DBCMASK 0x3fffff
190*5c4a5fe1SAndy Fiddaman uint32_t dbc;
191*5c4a5fe1SAndy Fiddaman };
192*5c4a5fe1SAndy Fiddaman
193*5c4a5fe1SAndy Fiddaman struct pci_ahci_softc {
194*5c4a5fe1SAndy Fiddaman struct pci_devinst *asc_pi;
195*5c4a5fe1SAndy Fiddaman pthread_mutex_t mtx;
196*5c4a5fe1SAndy Fiddaman int ports;
197*5c4a5fe1SAndy Fiddaman uint32_t cap;
198*5c4a5fe1SAndy Fiddaman uint32_t ghc;
199*5c4a5fe1SAndy Fiddaman uint32_t is;
200*5c4a5fe1SAndy Fiddaman uint32_t pi;
201*5c4a5fe1SAndy Fiddaman uint32_t vs;
202*5c4a5fe1SAndy Fiddaman uint32_t ccc_ctl;
203*5c4a5fe1SAndy Fiddaman uint32_t ccc_pts;
204*5c4a5fe1SAndy Fiddaman uint32_t em_loc;
205*5c4a5fe1SAndy Fiddaman uint32_t em_ctl;
206*5c4a5fe1SAndy Fiddaman uint32_t cap2;
207*5c4a5fe1SAndy Fiddaman uint32_t bohc;
208*5c4a5fe1SAndy Fiddaman uint32_t lintr;
209*5c4a5fe1SAndy Fiddaman struct ahci_port port[MAX_PORTS];
210*5c4a5fe1SAndy Fiddaman };
211*5c4a5fe1SAndy Fiddaman #define ahci_ctx(sc) ((sc)->asc_pi->pi_vmctx)
212*5c4a5fe1SAndy Fiddaman
213*5c4a5fe1SAndy Fiddaman static void ahci_handle_port(struct ahci_port *p);
214*5c4a5fe1SAndy Fiddaman
lba_to_msf(uint8_t * buf,int lba)215*5c4a5fe1SAndy Fiddaman static inline void lba_to_msf(uint8_t *buf, int lba)
216*5c4a5fe1SAndy Fiddaman {
217*5c4a5fe1SAndy Fiddaman lba += 150;
218*5c4a5fe1SAndy Fiddaman buf[0] = (lba / 75) / 60;
219*5c4a5fe1SAndy Fiddaman buf[1] = (lba / 75) % 60;
220*5c4a5fe1SAndy Fiddaman buf[2] = lba % 75;
221*5c4a5fe1SAndy Fiddaman }
222*5c4a5fe1SAndy Fiddaman
223*5c4a5fe1SAndy Fiddaman /*
224*5c4a5fe1SAndy Fiddaman * Generate HBA interrupts on global IS register write.
225*5c4a5fe1SAndy Fiddaman */
226*5c4a5fe1SAndy Fiddaman static void
ahci_generate_intr(struct pci_ahci_softc * sc,uint32_t mask)227*5c4a5fe1SAndy Fiddaman ahci_generate_intr(struct pci_ahci_softc *sc, uint32_t mask)
228*5c4a5fe1SAndy Fiddaman {
229*5c4a5fe1SAndy Fiddaman struct pci_devinst *pi = sc->asc_pi;
230*5c4a5fe1SAndy Fiddaman struct ahci_port *p;
231*5c4a5fe1SAndy Fiddaman int i, nmsg;
232*5c4a5fe1SAndy Fiddaman uint32_t mmask;
233*5c4a5fe1SAndy Fiddaman
234*5c4a5fe1SAndy Fiddaman /* Update global IS from PxIS/PxIE. */
235*5c4a5fe1SAndy Fiddaman for (i = 0; i < sc->ports; i++) {
236*5c4a5fe1SAndy Fiddaman p = &sc->port[i];
237*5c4a5fe1SAndy Fiddaman if (p->is & p->ie)
238*5c4a5fe1SAndy Fiddaman sc->is |= (1 << i);
239*5c4a5fe1SAndy Fiddaman }
240*5c4a5fe1SAndy Fiddaman DPRINTF("%s(%08x) %08x", __func__, mask, sc->is);
241*5c4a5fe1SAndy Fiddaman
242*5c4a5fe1SAndy Fiddaman /* If there is nothing enabled -- clear legacy interrupt and exit. */
243*5c4a5fe1SAndy Fiddaman if (sc->is == 0 || (sc->ghc & AHCI_GHC_IE) == 0) {
244*5c4a5fe1SAndy Fiddaman if (sc->lintr) {
245*5c4a5fe1SAndy Fiddaman pci_lintr_deassert(pi);
246*5c4a5fe1SAndy Fiddaman sc->lintr = 0;
247*5c4a5fe1SAndy Fiddaman }
248*5c4a5fe1SAndy Fiddaman return;
249*5c4a5fe1SAndy Fiddaman }
250*5c4a5fe1SAndy Fiddaman
251*5c4a5fe1SAndy Fiddaman /* If there is anything and no MSI -- assert legacy interrupt. */
252*5c4a5fe1SAndy Fiddaman nmsg = pci_msi_maxmsgnum(pi);
253*5c4a5fe1SAndy Fiddaman if (nmsg == 0) {
254*5c4a5fe1SAndy Fiddaman if (!sc->lintr) {
255*5c4a5fe1SAndy Fiddaman sc->lintr = 1;
256*5c4a5fe1SAndy Fiddaman pci_lintr_assert(pi);
257*5c4a5fe1SAndy Fiddaman }
258*5c4a5fe1SAndy Fiddaman return;
259*5c4a5fe1SAndy Fiddaman }
260*5c4a5fe1SAndy Fiddaman
261*5c4a5fe1SAndy Fiddaman /* Assert respective MSIs for ports that were touched. */
262*5c4a5fe1SAndy Fiddaman for (i = 0; i < nmsg; i++) {
263*5c4a5fe1SAndy Fiddaman if (sc->ports <= nmsg || i < nmsg - 1)
264*5c4a5fe1SAndy Fiddaman mmask = 1 << i;
265*5c4a5fe1SAndy Fiddaman else
266*5c4a5fe1SAndy Fiddaman mmask = 0xffffffff << i;
267*5c4a5fe1SAndy Fiddaman if (sc->is & mask && mmask & mask)
268*5c4a5fe1SAndy Fiddaman pci_generate_msi(pi, i);
269*5c4a5fe1SAndy Fiddaman }
270*5c4a5fe1SAndy Fiddaman }
271*5c4a5fe1SAndy Fiddaman
272*5c4a5fe1SAndy Fiddaman /*
273*5c4a5fe1SAndy Fiddaman * Generate HBA interrupt on specific port event.
274*5c4a5fe1SAndy Fiddaman */
275*5c4a5fe1SAndy Fiddaman static void
ahci_port_intr(struct ahci_port * p)276*5c4a5fe1SAndy Fiddaman ahci_port_intr(struct ahci_port *p)
277*5c4a5fe1SAndy Fiddaman {
278*5c4a5fe1SAndy Fiddaman struct pci_ahci_softc *sc = p->pr_sc;
279*5c4a5fe1SAndy Fiddaman struct pci_devinst *pi = sc->asc_pi;
280*5c4a5fe1SAndy Fiddaman int nmsg;
281*5c4a5fe1SAndy Fiddaman
282*5c4a5fe1SAndy Fiddaman DPRINTF("%s(%d) %08x/%08x %08x", __func__,
283*5c4a5fe1SAndy Fiddaman p->port, p->is, p->ie, sc->is);
284*5c4a5fe1SAndy Fiddaman
285*5c4a5fe1SAndy Fiddaman /* If there is nothing enabled -- we are done. */
286*5c4a5fe1SAndy Fiddaman if ((p->is & p->ie) == 0)
287*5c4a5fe1SAndy Fiddaman return;
288*5c4a5fe1SAndy Fiddaman
289*5c4a5fe1SAndy Fiddaman /* In case of non-shared MSI always generate interrupt. */
290*5c4a5fe1SAndy Fiddaman nmsg = pci_msi_maxmsgnum(pi);
291*5c4a5fe1SAndy Fiddaman if (sc->ports <= nmsg || p->port < nmsg - 1) {
292*5c4a5fe1SAndy Fiddaman sc->is |= (1 << p->port);
293*5c4a5fe1SAndy Fiddaman if ((sc->ghc & AHCI_GHC_IE) == 0)
294*5c4a5fe1SAndy Fiddaman return;
295*5c4a5fe1SAndy Fiddaman pci_generate_msi(pi, p->port);
296*5c4a5fe1SAndy Fiddaman return;
297*5c4a5fe1SAndy Fiddaman }
298*5c4a5fe1SAndy Fiddaman
299*5c4a5fe1SAndy Fiddaman /* If IS for this port is already set -- do nothing. */
300*5c4a5fe1SAndy Fiddaman if (sc->is & (1 << p->port))
301*5c4a5fe1SAndy Fiddaman return;
302*5c4a5fe1SAndy Fiddaman
303*5c4a5fe1SAndy Fiddaman sc->is |= (1 << p->port);
304*5c4a5fe1SAndy Fiddaman
305*5c4a5fe1SAndy Fiddaman /* If interrupts are enabled -- generate one. */
306*5c4a5fe1SAndy Fiddaman if ((sc->ghc & AHCI_GHC_IE) == 0)
307*5c4a5fe1SAndy Fiddaman return;
308*5c4a5fe1SAndy Fiddaman if (nmsg > 0) {
309*5c4a5fe1SAndy Fiddaman pci_generate_msi(pi, nmsg - 1);
310*5c4a5fe1SAndy Fiddaman } else if (!sc->lintr) {
311*5c4a5fe1SAndy Fiddaman sc->lintr = 1;
312*5c4a5fe1SAndy Fiddaman pci_lintr_assert(pi);
313*5c4a5fe1SAndy Fiddaman }
314*5c4a5fe1SAndy Fiddaman }
315*5c4a5fe1SAndy Fiddaman
316*5c4a5fe1SAndy Fiddaman static void
ahci_write_fis(struct ahci_port * p,enum sata_fis_type ft,uint8_t * fis)317*5c4a5fe1SAndy Fiddaman ahci_write_fis(struct ahci_port *p, enum sata_fis_type ft, uint8_t *fis)
318*5c4a5fe1SAndy Fiddaman {
319*5c4a5fe1SAndy Fiddaman int offset, len, irq;
320*5c4a5fe1SAndy Fiddaman
321*5c4a5fe1SAndy Fiddaman if (p->rfis == NULL || !(p->cmd & AHCI_P_CMD_FRE))
322*5c4a5fe1SAndy Fiddaman return;
323*5c4a5fe1SAndy Fiddaman
324*5c4a5fe1SAndy Fiddaman switch (ft) {
325*5c4a5fe1SAndy Fiddaman case FIS_TYPE_REGD2H:
326*5c4a5fe1SAndy Fiddaman offset = 0x40;
327*5c4a5fe1SAndy Fiddaman len = 20;
328*5c4a5fe1SAndy Fiddaman irq = (fis[1] & (1 << 6)) ? AHCI_P_IX_DHR : 0;
329*5c4a5fe1SAndy Fiddaman break;
330*5c4a5fe1SAndy Fiddaman case FIS_TYPE_SETDEVBITS:
331*5c4a5fe1SAndy Fiddaman offset = 0x58;
332*5c4a5fe1SAndy Fiddaman len = 8;
333*5c4a5fe1SAndy Fiddaman irq = (fis[1] & (1 << 6)) ? AHCI_P_IX_SDB : 0;
334*5c4a5fe1SAndy Fiddaman break;
335*5c4a5fe1SAndy Fiddaman case FIS_TYPE_PIOSETUP:
336*5c4a5fe1SAndy Fiddaman offset = 0x20;
337*5c4a5fe1SAndy Fiddaman len = 20;
338*5c4a5fe1SAndy Fiddaman irq = (fis[1] & (1 << 6)) ? AHCI_P_IX_PS : 0;
339*5c4a5fe1SAndy Fiddaman break;
340*5c4a5fe1SAndy Fiddaman default:
341*5c4a5fe1SAndy Fiddaman EPRINTLN("unsupported fis type %d", ft);
342*5c4a5fe1SAndy Fiddaman return;
343*5c4a5fe1SAndy Fiddaman }
344*5c4a5fe1SAndy Fiddaman if (fis[2] & ATA_S_ERROR) {
345*5c4a5fe1SAndy Fiddaman p->waitforclear = 1;
346*5c4a5fe1SAndy Fiddaman irq |= AHCI_P_IX_TFE;
347*5c4a5fe1SAndy Fiddaman }
348*5c4a5fe1SAndy Fiddaman memcpy(p->rfis + offset, fis, len);
349*5c4a5fe1SAndy Fiddaman if (irq) {
350*5c4a5fe1SAndy Fiddaman if (~p->is & irq) {
351*5c4a5fe1SAndy Fiddaman p->is |= irq;
352*5c4a5fe1SAndy Fiddaman ahci_port_intr(p);
353*5c4a5fe1SAndy Fiddaman }
354*5c4a5fe1SAndy Fiddaman }
355*5c4a5fe1SAndy Fiddaman }
356*5c4a5fe1SAndy Fiddaman
357*5c4a5fe1SAndy Fiddaman static void
ahci_write_fis_piosetup(struct ahci_port * p)358*5c4a5fe1SAndy Fiddaman ahci_write_fis_piosetup(struct ahci_port *p)
359*5c4a5fe1SAndy Fiddaman {
360*5c4a5fe1SAndy Fiddaman uint8_t fis[20];
361*5c4a5fe1SAndy Fiddaman
362*5c4a5fe1SAndy Fiddaman memset(fis, 0, sizeof(fis));
363*5c4a5fe1SAndy Fiddaman fis[0] = FIS_TYPE_PIOSETUP;
364*5c4a5fe1SAndy Fiddaman ahci_write_fis(p, FIS_TYPE_PIOSETUP, fis);
365*5c4a5fe1SAndy Fiddaman }
366*5c4a5fe1SAndy Fiddaman
367*5c4a5fe1SAndy Fiddaman static void
ahci_write_fis_sdb(struct ahci_port * p,int slot,uint8_t * cfis,uint32_t tfd)368*5c4a5fe1SAndy Fiddaman ahci_write_fis_sdb(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t tfd)
369*5c4a5fe1SAndy Fiddaman {
370*5c4a5fe1SAndy Fiddaman uint8_t fis[8];
371*5c4a5fe1SAndy Fiddaman uint8_t error;
372*5c4a5fe1SAndy Fiddaman
373*5c4a5fe1SAndy Fiddaman error = (tfd >> 8) & 0xff;
374*5c4a5fe1SAndy Fiddaman tfd &= 0x77;
375*5c4a5fe1SAndy Fiddaman memset(fis, 0, sizeof(fis));
376*5c4a5fe1SAndy Fiddaman fis[0] = FIS_TYPE_SETDEVBITS;
377*5c4a5fe1SAndy Fiddaman fis[1] = (1 << 6);
378*5c4a5fe1SAndy Fiddaman fis[2] = tfd;
379*5c4a5fe1SAndy Fiddaman fis[3] = error;
380*5c4a5fe1SAndy Fiddaman if (fis[2] & ATA_S_ERROR) {
381*5c4a5fe1SAndy Fiddaman p->err_cfis[0] = slot;
382*5c4a5fe1SAndy Fiddaman p->err_cfis[2] = tfd;
383*5c4a5fe1SAndy Fiddaman p->err_cfis[3] = error;
384*5c4a5fe1SAndy Fiddaman memcpy(&p->err_cfis[4], cfis + 4, 16);
385*5c4a5fe1SAndy Fiddaman } else {
386*5c4a5fe1SAndy Fiddaman *(uint32_t *)(fis + 4) = (1 << slot);
387*5c4a5fe1SAndy Fiddaman p->sact &= ~(1 << slot);
388*5c4a5fe1SAndy Fiddaman }
389*5c4a5fe1SAndy Fiddaman p->tfd &= ~0x77;
390*5c4a5fe1SAndy Fiddaman p->tfd |= tfd;
391*5c4a5fe1SAndy Fiddaman ahci_write_fis(p, FIS_TYPE_SETDEVBITS, fis);
392*5c4a5fe1SAndy Fiddaman }
393*5c4a5fe1SAndy Fiddaman
394*5c4a5fe1SAndy Fiddaman static void
ahci_write_fis_d2h(struct ahci_port * p,int slot,uint8_t * cfis,uint32_t tfd)395*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t tfd)
396*5c4a5fe1SAndy Fiddaman {
397*5c4a5fe1SAndy Fiddaman uint8_t fis[20];
398*5c4a5fe1SAndy Fiddaman uint8_t error;
399*5c4a5fe1SAndy Fiddaman
400*5c4a5fe1SAndy Fiddaman error = (tfd >> 8) & 0xff;
401*5c4a5fe1SAndy Fiddaman memset(fis, 0, sizeof(fis));
402*5c4a5fe1SAndy Fiddaman fis[0] = FIS_TYPE_REGD2H;
403*5c4a5fe1SAndy Fiddaman fis[1] = (1 << 6);
404*5c4a5fe1SAndy Fiddaman fis[2] = tfd & 0xff;
405*5c4a5fe1SAndy Fiddaman fis[3] = error;
406*5c4a5fe1SAndy Fiddaman fis[4] = cfis[4];
407*5c4a5fe1SAndy Fiddaman fis[5] = cfis[5];
408*5c4a5fe1SAndy Fiddaman fis[6] = cfis[6];
409*5c4a5fe1SAndy Fiddaman fis[7] = cfis[7];
410*5c4a5fe1SAndy Fiddaman fis[8] = cfis[8];
411*5c4a5fe1SAndy Fiddaman fis[9] = cfis[9];
412*5c4a5fe1SAndy Fiddaman fis[10] = cfis[10];
413*5c4a5fe1SAndy Fiddaman fis[11] = cfis[11];
414*5c4a5fe1SAndy Fiddaman fis[12] = cfis[12];
415*5c4a5fe1SAndy Fiddaman fis[13] = cfis[13];
416*5c4a5fe1SAndy Fiddaman if (fis[2] & ATA_S_ERROR) {
417*5c4a5fe1SAndy Fiddaman p->err_cfis[0] = 0x80;
418*5c4a5fe1SAndy Fiddaman p->err_cfis[2] = tfd & 0xff;
419*5c4a5fe1SAndy Fiddaman p->err_cfis[3] = error;
420*5c4a5fe1SAndy Fiddaman memcpy(&p->err_cfis[4], cfis + 4, 16);
421*5c4a5fe1SAndy Fiddaman } else
422*5c4a5fe1SAndy Fiddaman p->ci &= ~(1 << slot);
423*5c4a5fe1SAndy Fiddaman p->tfd = tfd;
424*5c4a5fe1SAndy Fiddaman ahci_write_fis(p, FIS_TYPE_REGD2H, fis);
425*5c4a5fe1SAndy Fiddaman }
426*5c4a5fe1SAndy Fiddaman
427*5c4a5fe1SAndy Fiddaman static void
ahci_write_fis_d2h_ncq(struct ahci_port * p,int slot)428*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h_ncq(struct ahci_port *p, int slot)
429*5c4a5fe1SAndy Fiddaman {
430*5c4a5fe1SAndy Fiddaman uint8_t fis[20];
431*5c4a5fe1SAndy Fiddaman
432*5c4a5fe1SAndy Fiddaman p->tfd = ATA_S_READY | ATA_S_DSC;
433*5c4a5fe1SAndy Fiddaman memset(fis, 0, sizeof(fis));
434*5c4a5fe1SAndy Fiddaman fis[0] = FIS_TYPE_REGD2H;
435*5c4a5fe1SAndy Fiddaman fis[1] = 0; /* No interrupt */
436*5c4a5fe1SAndy Fiddaman fis[2] = p->tfd; /* Status */
437*5c4a5fe1SAndy Fiddaman fis[3] = 0; /* No error */
438*5c4a5fe1SAndy Fiddaman p->ci &= ~(1 << slot);
439*5c4a5fe1SAndy Fiddaman ahci_write_fis(p, FIS_TYPE_REGD2H, fis);
440*5c4a5fe1SAndy Fiddaman }
441*5c4a5fe1SAndy Fiddaman
442*5c4a5fe1SAndy Fiddaman static void
ahci_write_reset_fis_d2h(struct ahci_port * p)443*5c4a5fe1SAndy Fiddaman ahci_write_reset_fis_d2h(struct ahci_port *p)
444*5c4a5fe1SAndy Fiddaman {
445*5c4a5fe1SAndy Fiddaman uint8_t fis[20];
446*5c4a5fe1SAndy Fiddaman
447*5c4a5fe1SAndy Fiddaman memset(fis, 0, sizeof(fis));
448*5c4a5fe1SAndy Fiddaman fis[0] = FIS_TYPE_REGD2H;
449*5c4a5fe1SAndy Fiddaman fis[3] = 1;
450*5c4a5fe1SAndy Fiddaman fis[4] = 1;
451*5c4a5fe1SAndy Fiddaman if (p->atapi) {
452*5c4a5fe1SAndy Fiddaman fis[5] = 0x14;
453*5c4a5fe1SAndy Fiddaman fis[6] = 0xeb;
454*5c4a5fe1SAndy Fiddaman }
455*5c4a5fe1SAndy Fiddaman fis[12] = 1;
456*5c4a5fe1SAndy Fiddaman ahci_write_fis(p, FIS_TYPE_REGD2H, fis);
457*5c4a5fe1SAndy Fiddaman }
458*5c4a5fe1SAndy Fiddaman
459*5c4a5fe1SAndy Fiddaman static void
ahci_check_stopped(struct ahci_port * p)460*5c4a5fe1SAndy Fiddaman ahci_check_stopped(struct ahci_port *p)
461*5c4a5fe1SAndy Fiddaman {
462*5c4a5fe1SAndy Fiddaman /*
463*5c4a5fe1SAndy Fiddaman * If we are no longer processing the command list and nothing
464*5c4a5fe1SAndy Fiddaman * is in-flight, clear the running bit, the current command
465*5c4a5fe1SAndy Fiddaman * slot, the command issue and active bits.
466*5c4a5fe1SAndy Fiddaman */
467*5c4a5fe1SAndy Fiddaman if (!(p->cmd & AHCI_P_CMD_ST)) {
468*5c4a5fe1SAndy Fiddaman if (p->pending == 0) {
469*5c4a5fe1SAndy Fiddaman p->ccs = 0;
470*5c4a5fe1SAndy Fiddaman p->cmd &= ~(AHCI_P_CMD_CR | AHCI_P_CMD_CCS_MASK);
471*5c4a5fe1SAndy Fiddaman p->ci = 0;
472*5c4a5fe1SAndy Fiddaman p->sact = 0;
473*5c4a5fe1SAndy Fiddaman p->waitforclear = 0;
474*5c4a5fe1SAndy Fiddaman }
475*5c4a5fe1SAndy Fiddaman }
476*5c4a5fe1SAndy Fiddaman }
477*5c4a5fe1SAndy Fiddaman
478*5c4a5fe1SAndy Fiddaman static void
ahci_port_stop(struct ahci_port * p)479*5c4a5fe1SAndy Fiddaman ahci_port_stop(struct ahci_port *p)
480*5c4a5fe1SAndy Fiddaman {
481*5c4a5fe1SAndy Fiddaman struct ahci_ioreq *aior;
482*5c4a5fe1SAndy Fiddaman uint8_t *cfis;
483*5c4a5fe1SAndy Fiddaman int slot;
484*5c4a5fe1SAndy Fiddaman int error;
485*5c4a5fe1SAndy Fiddaman
486*5c4a5fe1SAndy Fiddaman assert(pthread_mutex_isowned_np(&p->pr_sc->mtx));
487*5c4a5fe1SAndy Fiddaman
488*5c4a5fe1SAndy Fiddaman TAILQ_FOREACH(aior, &p->iobhd, io_blist) {
489*5c4a5fe1SAndy Fiddaman /*
490*5c4a5fe1SAndy Fiddaman * Try to cancel the outstanding blockif request.
491*5c4a5fe1SAndy Fiddaman */
492*5c4a5fe1SAndy Fiddaman error = blockif_cancel(p->bctx, &aior->io_req);
493*5c4a5fe1SAndy Fiddaman if (error != 0)
494*5c4a5fe1SAndy Fiddaman continue;
495*5c4a5fe1SAndy Fiddaman
496*5c4a5fe1SAndy Fiddaman slot = aior->slot;
497*5c4a5fe1SAndy Fiddaman cfis = aior->cfis;
498*5c4a5fe1SAndy Fiddaman if (cfis[2] == ATA_WRITE_FPDMA_QUEUED ||
499*5c4a5fe1SAndy Fiddaman cfis[2] == ATA_READ_FPDMA_QUEUED ||
500*5c4a5fe1SAndy Fiddaman cfis[2] == ATA_SEND_FPDMA_QUEUED)
501*5c4a5fe1SAndy Fiddaman p->sact &= ~(1 << slot); /* NCQ */
502*5c4a5fe1SAndy Fiddaman else
503*5c4a5fe1SAndy Fiddaman p->ci &= ~(1 << slot);
504*5c4a5fe1SAndy Fiddaman
505*5c4a5fe1SAndy Fiddaman /*
506*5c4a5fe1SAndy Fiddaman * This command is now done.
507*5c4a5fe1SAndy Fiddaman */
508*5c4a5fe1SAndy Fiddaman p->pending &= ~(1 << slot);
509*5c4a5fe1SAndy Fiddaman
510*5c4a5fe1SAndy Fiddaman /*
511*5c4a5fe1SAndy Fiddaman * Delete the blockif request from the busy list
512*5c4a5fe1SAndy Fiddaman */
513*5c4a5fe1SAndy Fiddaman TAILQ_REMOVE(&p->iobhd, aior, io_blist);
514*5c4a5fe1SAndy Fiddaman
515*5c4a5fe1SAndy Fiddaman /*
516*5c4a5fe1SAndy Fiddaman * Move the blockif request back to the free list
517*5c4a5fe1SAndy Fiddaman */
518*5c4a5fe1SAndy Fiddaman STAILQ_INSERT_TAIL(&p->iofhd, aior, io_flist);
519*5c4a5fe1SAndy Fiddaman }
520*5c4a5fe1SAndy Fiddaman
521*5c4a5fe1SAndy Fiddaman ahci_check_stopped(p);
522*5c4a5fe1SAndy Fiddaman }
523*5c4a5fe1SAndy Fiddaman
524*5c4a5fe1SAndy Fiddaman static void
ahci_port_reset(struct ahci_port * pr)525*5c4a5fe1SAndy Fiddaman ahci_port_reset(struct ahci_port *pr)
526*5c4a5fe1SAndy Fiddaman {
527*5c4a5fe1SAndy Fiddaman pr->serr = 0;
528*5c4a5fe1SAndy Fiddaman pr->sact = 0;
529*5c4a5fe1SAndy Fiddaman pr->xfermode = ATA_UDMA6;
530*5c4a5fe1SAndy Fiddaman pr->mult_sectors = 128;
531*5c4a5fe1SAndy Fiddaman
532*5c4a5fe1SAndy Fiddaman if (!pr->bctx) {
533*5c4a5fe1SAndy Fiddaman pr->ssts = ATA_SS_DET_NO_DEVICE;
534*5c4a5fe1SAndy Fiddaman pr->sig = 0xFFFFFFFF;
535*5c4a5fe1SAndy Fiddaman pr->tfd = 0x7F;
536*5c4a5fe1SAndy Fiddaman return;
537*5c4a5fe1SAndy Fiddaman }
538*5c4a5fe1SAndy Fiddaman pr->ssts = ATA_SS_DET_PHY_ONLINE | ATA_SS_IPM_ACTIVE;
539*5c4a5fe1SAndy Fiddaman if (pr->sctl & ATA_SC_SPD_MASK)
540*5c4a5fe1SAndy Fiddaman pr->ssts |= (pr->sctl & ATA_SC_SPD_MASK);
541*5c4a5fe1SAndy Fiddaman else
542*5c4a5fe1SAndy Fiddaman pr->ssts |= ATA_SS_SPD_GEN3;
543*5c4a5fe1SAndy Fiddaman pr->tfd = (1 << 8) | ATA_S_DSC | ATA_S_DMA;
544*5c4a5fe1SAndy Fiddaman if (!pr->atapi) {
545*5c4a5fe1SAndy Fiddaman pr->sig = PxSIG_ATA;
546*5c4a5fe1SAndy Fiddaman pr->tfd |= ATA_S_READY;
547*5c4a5fe1SAndy Fiddaman } else
548*5c4a5fe1SAndy Fiddaman pr->sig = PxSIG_ATAPI;
549*5c4a5fe1SAndy Fiddaman ahci_write_reset_fis_d2h(pr);
550*5c4a5fe1SAndy Fiddaman }
551*5c4a5fe1SAndy Fiddaman
552*5c4a5fe1SAndy Fiddaman static void
ahci_reset(struct pci_ahci_softc * sc)553*5c4a5fe1SAndy Fiddaman ahci_reset(struct pci_ahci_softc *sc)
554*5c4a5fe1SAndy Fiddaman {
555*5c4a5fe1SAndy Fiddaman int i;
556*5c4a5fe1SAndy Fiddaman
557*5c4a5fe1SAndy Fiddaman sc->ghc = AHCI_GHC_AE;
558*5c4a5fe1SAndy Fiddaman sc->is = 0;
559*5c4a5fe1SAndy Fiddaman
560*5c4a5fe1SAndy Fiddaman if (sc->lintr) {
561*5c4a5fe1SAndy Fiddaman pci_lintr_deassert(sc->asc_pi);
562*5c4a5fe1SAndy Fiddaman sc->lintr = 0;
563*5c4a5fe1SAndy Fiddaman }
564*5c4a5fe1SAndy Fiddaman
565*5c4a5fe1SAndy Fiddaman for (i = 0; i < sc->ports; i++) {
566*5c4a5fe1SAndy Fiddaman sc->port[i].ie = 0;
567*5c4a5fe1SAndy Fiddaman sc->port[i].is = 0;
568*5c4a5fe1SAndy Fiddaman sc->port[i].cmd = (AHCI_P_CMD_SUD | AHCI_P_CMD_POD);
569*5c4a5fe1SAndy Fiddaman if (sc->port[i].bctx)
570*5c4a5fe1SAndy Fiddaman sc->port[i].cmd |= AHCI_P_CMD_CPS;
571*5c4a5fe1SAndy Fiddaman sc->port[i].sctl = 0;
572*5c4a5fe1SAndy Fiddaman ahci_port_reset(&sc->port[i]);
573*5c4a5fe1SAndy Fiddaman }
574*5c4a5fe1SAndy Fiddaman }
575*5c4a5fe1SAndy Fiddaman
576*5c4a5fe1SAndy Fiddaman static void
ata_string(uint8_t * dest,const char * src,int len)577*5c4a5fe1SAndy Fiddaman ata_string(uint8_t *dest, const char *src, int len)
578*5c4a5fe1SAndy Fiddaman {
579*5c4a5fe1SAndy Fiddaman int i;
580*5c4a5fe1SAndy Fiddaman
581*5c4a5fe1SAndy Fiddaman for (i = 0; i < len; i++) {
582*5c4a5fe1SAndy Fiddaman if (*src)
583*5c4a5fe1SAndy Fiddaman dest[i ^ 1] = *src++;
584*5c4a5fe1SAndy Fiddaman else
585*5c4a5fe1SAndy Fiddaman dest[i ^ 1] = ' ';
586*5c4a5fe1SAndy Fiddaman }
587*5c4a5fe1SAndy Fiddaman }
588*5c4a5fe1SAndy Fiddaman
589*5c4a5fe1SAndy Fiddaman static void
atapi_string(uint8_t * dest,const char * src,int len)590*5c4a5fe1SAndy Fiddaman atapi_string(uint8_t *dest, const char *src, int len)
591*5c4a5fe1SAndy Fiddaman {
592*5c4a5fe1SAndy Fiddaman int i;
593*5c4a5fe1SAndy Fiddaman
594*5c4a5fe1SAndy Fiddaman for (i = 0; i < len; i++) {
595*5c4a5fe1SAndy Fiddaman if (*src)
596*5c4a5fe1SAndy Fiddaman dest[i] = *src++;
597*5c4a5fe1SAndy Fiddaman else
598*5c4a5fe1SAndy Fiddaman dest[i] = ' ';
599*5c4a5fe1SAndy Fiddaman }
600*5c4a5fe1SAndy Fiddaman }
601*5c4a5fe1SAndy Fiddaman
602*5c4a5fe1SAndy Fiddaman /*
603*5c4a5fe1SAndy Fiddaman * Build up the iovec based on the PRDT, 'done' and 'len'.
604*5c4a5fe1SAndy Fiddaman */
605*5c4a5fe1SAndy Fiddaman static void
ahci_build_iov(struct ahci_port * p,struct ahci_ioreq * aior,struct ahci_prdt_entry * prdt,uint16_t prdtl)606*5c4a5fe1SAndy Fiddaman ahci_build_iov(struct ahci_port *p, struct ahci_ioreq *aior,
607*5c4a5fe1SAndy Fiddaman struct ahci_prdt_entry *prdt, uint16_t prdtl)
608*5c4a5fe1SAndy Fiddaman {
609*5c4a5fe1SAndy Fiddaman struct blockif_req *breq = &aior->io_req;
610*5c4a5fe1SAndy Fiddaman uint32_t dbcsz, extra, left, skip, todo;
611*5c4a5fe1SAndy Fiddaman int i, j;
612*5c4a5fe1SAndy Fiddaman
613*5c4a5fe1SAndy Fiddaman assert(aior->len >= aior->done);
614*5c4a5fe1SAndy Fiddaman
615*5c4a5fe1SAndy Fiddaman /* Copy part of PRDT between 'done' and 'len' bytes into the iov. */
616*5c4a5fe1SAndy Fiddaman skip = aior->done;
617*5c4a5fe1SAndy Fiddaman left = aior->len - aior->done;
618*5c4a5fe1SAndy Fiddaman todo = 0;
619*5c4a5fe1SAndy Fiddaman for (i = 0, j = 0; i < prdtl && j < BLOCKIF_IOV_MAX && left > 0;
620*5c4a5fe1SAndy Fiddaman i++, prdt++) {
621*5c4a5fe1SAndy Fiddaman dbcsz = (prdt->dbc & DBCMASK) + 1;
622*5c4a5fe1SAndy Fiddaman /* Skip already done part of the PRDT */
623*5c4a5fe1SAndy Fiddaman if (dbcsz <= skip) {
624*5c4a5fe1SAndy Fiddaman skip -= dbcsz;
625*5c4a5fe1SAndy Fiddaman continue;
626*5c4a5fe1SAndy Fiddaman }
627*5c4a5fe1SAndy Fiddaman dbcsz -= skip;
628*5c4a5fe1SAndy Fiddaman if (dbcsz > left)
629*5c4a5fe1SAndy Fiddaman dbcsz = left;
630*5c4a5fe1SAndy Fiddaman breq->br_iov[j].iov_base = paddr_guest2host(ahci_ctx(p->pr_sc),
631*5c4a5fe1SAndy Fiddaman prdt->dba + skip, dbcsz);
632*5c4a5fe1SAndy Fiddaman breq->br_iov[j].iov_len = dbcsz;
633*5c4a5fe1SAndy Fiddaman todo += dbcsz;
634*5c4a5fe1SAndy Fiddaman left -= dbcsz;
635*5c4a5fe1SAndy Fiddaman skip = 0;
636*5c4a5fe1SAndy Fiddaman j++;
637*5c4a5fe1SAndy Fiddaman }
638*5c4a5fe1SAndy Fiddaman
639*5c4a5fe1SAndy Fiddaman /* If we got limited by IOV length, round I/O down to sector size. */
640*5c4a5fe1SAndy Fiddaman if (j == BLOCKIF_IOV_MAX) {
641*5c4a5fe1SAndy Fiddaman extra = todo % blockif_sectsz(p->bctx);
642*5c4a5fe1SAndy Fiddaman todo -= extra;
643*5c4a5fe1SAndy Fiddaman assert(todo > 0);
644*5c4a5fe1SAndy Fiddaman while (extra > 0) {
645*5c4a5fe1SAndy Fiddaman if (breq->br_iov[j - 1].iov_len > extra) {
646*5c4a5fe1SAndy Fiddaman breq->br_iov[j - 1].iov_len -= extra;
647*5c4a5fe1SAndy Fiddaman break;
648*5c4a5fe1SAndy Fiddaman }
649*5c4a5fe1SAndy Fiddaman extra -= breq->br_iov[j - 1].iov_len;
650*5c4a5fe1SAndy Fiddaman j--;
651*5c4a5fe1SAndy Fiddaman }
652*5c4a5fe1SAndy Fiddaman }
653*5c4a5fe1SAndy Fiddaman
654*5c4a5fe1SAndy Fiddaman breq->br_iovcnt = j;
655*5c4a5fe1SAndy Fiddaman breq->br_resid = todo;
656*5c4a5fe1SAndy Fiddaman aior->done += todo;
657*5c4a5fe1SAndy Fiddaman aior->more = (aior->done < aior->len && i < prdtl);
658*5c4a5fe1SAndy Fiddaman }
659*5c4a5fe1SAndy Fiddaman
660*5c4a5fe1SAndy Fiddaman static void
ahci_handle_rw(struct ahci_port * p,int slot,uint8_t * cfis,uint32_t done)661*5c4a5fe1SAndy Fiddaman ahci_handle_rw(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t done)
662*5c4a5fe1SAndy Fiddaman {
663*5c4a5fe1SAndy Fiddaman struct ahci_ioreq *aior;
664*5c4a5fe1SAndy Fiddaman struct blockif_req *breq;
665*5c4a5fe1SAndy Fiddaman struct ahci_prdt_entry *prdt;
666*5c4a5fe1SAndy Fiddaman struct ahci_cmd_hdr *hdr;
667*5c4a5fe1SAndy Fiddaman uint64_t lba;
668*5c4a5fe1SAndy Fiddaman uint32_t len;
669*5c4a5fe1SAndy Fiddaman int err, first, ncq, readop;
670*5c4a5fe1SAndy Fiddaman
671*5c4a5fe1SAndy Fiddaman prdt = (struct ahci_prdt_entry *)(cfis + 0x80);
672*5c4a5fe1SAndy Fiddaman hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
673*5c4a5fe1SAndy Fiddaman ncq = 0;
674*5c4a5fe1SAndy Fiddaman readop = 1;
675*5c4a5fe1SAndy Fiddaman first = (done == 0);
676*5c4a5fe1SAndy Fiddaman
677*5c4a5fe1SAndy Fiddaman if (cfis[2] == ATA_WRITE || cfis[2] == ATA_WRITE48 ||
678*5c4a5fe1SAndy Fiddaman cfis[2] == ATA_WRITE_MUL || cfis[2] == ATA_WRITE_MUL48 ||
679*5c4a5fe1SAndy Fiddaman cfis[2] == ATA_WRITE_DMA || cfis[2] == ATA_WRITE_DMA48 ||
680*5c4a5fe1SAndy Fiddaman cfis[2] == ATA_WRITE_FPDMA_QUEUED)
681*5c4a5fe1SAndy Fiddaman readop = 0;
682*5c4a5fe1SAndy Fiddaman
683*5c4a5fe1SAndy Fiddaman if (cfis[2] == ATA_WRITE_FPDMA_QUEUED ||
684*5c4a5fe1SAndy Fiddaman cfis[2] == ATA_READ_FPDMA_QUEUED) {
685*5c4a5fe1SAndy Fiddaman lba = ((uint64_t)cfis[10] << 40) |
686*5c4a5fe1SAndy Fiddaman ((uint64_t)cfis[9] << 32) |
687*5c4a5fe1SAndy Fiddaman ((uint64_t)cfis[8] << 24) |
688*5c4a5fe1SAndy Fiddaman ((uint64_t)cfis[6] << 16) |
689*5c4a5fe1SAndy Fiddaman ((uint64_t)cfis[5] << 8) |
690*5c4a5fe1SAndy Fiddaman cfis[4];
691*5c4a5fe1SAndy Fiddaman len = cfis[11] << 8 | cfis[3];
692*5c4a5fe1SAndy Fiddaman if (!len)
693*5c4a5fe1SAndy Fiddaman len = 65536;
694*5c4a5fe1SAndy Fiddaman ncq = 1;
695*5c4a5fe1SAndy Fiddaman } else if (cfis[2] == ATA_READ48 || cfis[2] == ATA_WRITE48 ||
696*5c4a5fe1SAndy Fiddaman cfis[2] == ATA_READ_MUL48 || cfis[2] == ATA_WRITE_MUL48 ||
697*5c4a5fe1SAndy Fiddaman cfis[2] == ATA_READ_DMA48 || cfis[2] == ATA_WRITE_DMA48) {
698*5c4a5fe1SAndy Fiddaman lba = ((uint64_t)cfis[10] << 40) |
699*5c4a5fe1SAndy Fiddaman ((uint64_t)cfis[9] << 32) |
700*5c4a5fe1SAndy Fiddaman ((uint64_t)cfis[8] << 24) |
701*5c4a5fe1SAndy Fiddaman ((uint64_t)cfis[6] << 16) |
702*5c4a5fe1SAndy Fiddaman ((uint64_t)cfis[5] << 8) |
703*5c4a5fe1SAndy Fiddaman cfis[4];
704*5c4a5fe1SAndy Fiddaman len = cfis[13] << 8 | cfis[12];
705*5c4a5fe1SAndy Fiddaman if (!len)
706*5c4a5fe1SAndy Fiddaman len = 65536;
707*5c4a5fe1SAndy Fiddaman } else {
708*5c4a5fe1SAndy Fiddaman lba = ((cfis[7] & 0xf) << 24) | (cfis[6] << 16) |
709*5c4a5fe1SAndy Fiddaman (cfis[5] << 8) | cfis[4];
710*5c4a5fe1SAndy Fiddaman len = cfis[12];
711*5c4a5fe1SAndy Fiddaman if (!len)
712*5c4a5fe1SAndy Fiddaman len = 256;
713*5c4a5fe1SAndy Fiddaman }
714*5c4a5fe1SAndy Fiddaman lba *= blockif_sectsz(p->bctx);
715*5c4a5fe1SAndy Fiddaman len *= blockif_sectsz(p->bctx);
716*5c4a5fe1SAndy Fiddaman
717*5c4a5fe1SAndy Fiddaman /* Pull request off free list */
718*5c4a5fe1SAndy Fiddaman aior = STAILQ_FIRST(&p->iofhd);
719*5c4a5fe1SAndy Fiddaman assert(aior != NULL);
720*5c4a5fe1SAndy Fiddaman STAILQ_REMOVE_HEAD(&p->iofhd, io_flist);
721*5c4a5fe1SAndy Fiddaman
722*5c4a5fe1SAndy Fiddaman aior->cfis = cfis;
723*5c4a5fe1SAndy Fiddaman aior->slot = slot;
724*5c4a5fe1SAndy Fiddaman aior->len = len;
725*5c4a5fe1SAndy Fiddaman aior->done = done;
726*5c4a5fe1SAndy Fiddaman breq = &aior->io_req;
727*5c4a5fe1SAndy Fiddaman breq->br_offset = lba + done;
728*5c4a5fe1SAndy Fiddaman ahci_build_iov(p, aior, prdt, hdr->prdtl);
729*5c4a5fe1SAndy Fiddaman
730*5c4a5fe1SAndy Fiddaman /* Mark this command in-flight. */
731*5c4a5fe1SAndy Fiddaman p->pending |= 1 << slot;
732*5c4a5fe1SAndy Fiddaman
733*5c4a5fe1SAndy Fiddaman /* Stuff request onto busy list. */
734*5c4a5fe1SAndy Fiddaman TAILQ_INSERT_HEAD(&p->iobhd, aior, io_blist);
735*5c4a5fe1SAndy Fiddaman
736*5c4a5fe1SAndy Fiddaman if (ncq && first)
737*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h_ncq(p, slot);
738*5c4a5fe1SAndy Fiddaman
739*5c4a5fe1SAndy Fiddaman if (readop)
740*5c4a5fe1SAndy Fiddaman err = blockif_read(p->bctx, breq);
741*5c4a5fe1SAndy Fiddaman else
742*5c4a5fe1SAndy Fiddaman err = blockif_write(p->bctx, breq);
743*5c4a5fe1SAndy Fiddaman assert(err == 0);
744*5c4a5fe1SAndy Fiddaman }
745*5c4a5fe1SAndy Fiddaman
746*5c4a5fe1SAndy Fiddaman static void
ahci_handle_flush(struct ahci_port * p,int slot,uint8_t * cfis)747*5c4a5fe1SAndy Fiddaman ahci_handle_flush(struct ahci_port *p, int slot, uint8_t *cfis)
748*5c4a5fe1SAndy Fiddaman {
749*5c4a5fe1SAndy Fiddaman struct ahci_ioreq *aior;
750*5c4a5fe1SAndy Fiddaman struct blockif_req *breq;
751*5c4a5fe1SAndy Fiddaman int err;
752*5c4a5fe1SAndy Fiddaman
753*5c4a5fe1SAndy Fiddaman /*
754*5c4a5fe1SAndy Fiddaman * Pull request off free list
755*5c4a5fe1SAndy Fiddaman */
756*5c4a5fe1SAndy Fiddaman aior = STAILQ_FIRST(&p->iofhd);
757*5c4a5fe1SAndy Fiddaman assert(aior != NULL);
758*5c4a5fe1SAndy Fiddaman STAILQ_REMOVE_HEAD(&p->iofhd, io_flist);
759*5c4a5fe1SAndy Fiddaman aior->cfis = cfis;
760*5c4a5fe1SAndy Fiddaman aior->slot = slot;
761*5c4a5fe1SAndy Fiddaman aior->len = 0;
762*5c4a5fe1SAndy Fiddaman aior->done = 0;
763*5c4a5fe1SAndy Fiddaman aior->more = 0;
764*5c4a5fe1SAndy Fiddaman breq = &aior->io_req;
765*5c4a5fe1SAndy Fiddaman
766*5c4a5fe1SAndy Fiddaman /*
767*5c4a5fe1SAndy Fiddaman * Mark this command in-flight.
768*5c4a5fe1SAndy Fiddaman */
769*5c4a5fe1SAndy Fiddaman p->pending |= 1 << slot;
770*5c4a5fe1SAndy Fiddaman
771*5c4a5fe1SAndy Fiddaman /*
772*5c4a5fe1SAndy Fiddaman * Stuff request onto busy list
773*5c4a5fe1SAndy Fiddaman */
774*5c4a5fe1SAndy Fiddaman TAILQ_INSERT_HEAD(&p->iobhd, aior, io_blist);
775*5c4a5fe1SAndy Fiddaman
776*5c4a5fe1SAndy Fiddaman err = blockif_flush(p->bctx, breq);
777*5c4a5fe1SAndy Fiddaman assert(err == 0);
778*5c4a5fe1SAndy Fiddaman }
779*5c4a5fe1SAndy Fiddaman
780*5c4a5fe1SAndy Fiddaman static inline void
read_prdt(struct ahci_port * p,int slot,uint8_t * cfis,void * buf,unsigned int size)781*5c4a5fe1SAndy Fiddaman read_prdt(struct ahci_port *p, int slot, uint8_t *cfis, void *buf,
782*5c4a5fe1SAndy Fiddaman unsigned int size)
783*5c4a5fe1SAndy Fiddaman {
784*5c4a5fe1SAndy Fiddaman struct ahci_cmd_hdr *hdr;
785*5c4a5fe1SAndy Fiddaman struct ahci_prdt_entry *prdt;
786*5c4a5fe1SAndy Fiddaman uint8_t *to;
787*5c4a5fe1SAndy Fiddaman unsigned int len;
788*5c4a5fe1SAndy Fiddaman int i;
789*5c4a5fe1SAndy Fiddaman
790*5c4a5fe1SAndy Fiddaman hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
791*5c4a5fe1SAndy Fiddaman len = size;
792*5c4a5fe1SAndy Fiddaman to = buf;
793*5c4a5fe1SAndy Fiddaman prdt = (struct ahci_prdt_entry *)(cfis + 0x80);
794*5c4a5fe1SAndy Fiddaman for (i = 0; i < hdr->prdtl && len; i++) {
795*5c4a5fe1SAndy Fiddaman uint8_t *ptr;
796*5c4a5fe1SAndy Fiddaman uint32_t dbcsz;
797*5c4a5fe1SAndy Fiddaman unsigned int sublen;
798*5c4a5fe1SAndy Fiddaman
799*5c4a5fe1SAndy Fiddaman dbcsz = (prdt->dbc & DBCMASK) + 1;
800*5c4a5fe1SAndy Fiddaman ptr = paddr_guest2host(ahci_ctx(p->pr_sc), prdt->dba, dbcsz);
801*5c4a5fe1SAndy Fiddaman sublen = MIN(len, dbcsz);
802*5c4a5fe1SAndy Fiddaman memcpy(to, ptr, sublen);
803*5c4a5fe1SAndy Fiddaman len -= sublen;
804*5c4a5fe1SAndy Fiddaman to += sublen;
805*5c4a5fe1SAndy Fiddaman prdt++;
806*5c4a5fe1SAndy Fiddaman }
807*5c4a5fe1SAndy Fiddaman }
808*5c4a5fe1SAndy Fiddaman
809*5c4a5fe1SAndy Fiddaman static void
ahci_handle_dsm_trim(struct ahci_port * p,int slot,uint8_t * cfis,uint32_t done)810*5c4a5fe1SAndy Fiddaman ahci_handle_dsm_trim(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t done)
811*5c4a5fe1SAndy Fiddaman {
812*5c4a5fe1SAndy Fiddaman struct ahci_ioreq *aior;
813*5c4a5fe1SAndy Fiddaman struct blockif_req *breq;
814*5c4a5fe1SAndy Fiddaman uint8_t *entry;
815*5c4a5fe1SAndy Fiddaman uint64_t elba;
816*5c4a5fe1SAndy Fiddaman uint32_t len, elen;
817*5c4a5fe1SAndy Fiddaman int err, first, ncq;
818*5c4a5fe1SAndy Fiddaman uint8_t buf[512];
819*5c4a5fe1SAndy Fiddaman
820*5c4a5fe1SAndy Fiddaman first = (done == 0);
821*5c4a5fe1SAndy Fiddaman if (cfis[2] == ATA_DATA_SET_MANAGEMENT) {
822*5c4a5fe1SAndy Fiddaman len = (uint16_t)cfis[13] << 8 | cfis[12];
823*5c4a5fe1SAndy Fiddaman len *= 512;
824*5c4a5fe1SAndy Fiddaman ncq = 0;
825*5c4a5fe1SAndy Fiddaman } else { /* ATA_SEND_FPDMA_QUEUED */
826*5c4a5fe1SAndy Fiddaman len = (uint16_t)cfis[11] << 8 | cfis[3];
827*5c4a5fe1SAndy Fiddaman len *= 512;
828*5c4a5fe1SAndy Fiddaman ncq = 1;
829*5c4a5fe1SAndy Fiddaman }
830*5c4a5fe1SAndy Fiddaman read_prdt(p, slot, cfis, buf, sizeof(buf));
831*5c4a5fe1SAndy Fiddaman
832*5c4a5fe1SAndy Fiddaman next:
833*5c4a5fe1SAndy Fiddaman entry = &buf[done];
834*5c4a5fe1SAndy Fiddaman elba = ((uint64_t)entry[5] << 40) |
835*5c4a5fe1SAndy Fiddaman ((uint64_t)entry[4] << 32) |
836*5c4a5fe1SAndy Fiddaman ((uint64_t)entry[3] << 24) |
837*5c4a5fe1SAndy Fiddaman ((uint64_t)entry[2] << 16) |
838*5c4a5fe1SAndy Fiddaman ((uint64_t)entry[1] << 8) |
839*5c4a5fe1SAndy Fiddaman entry[0];
840*5c4a5fe1SAndy Fiddaman elen = (uint16_t)entry[7] << 8 | entry[6];
841*5c4a5fe1SAndy Fiddaman done += 8;
842*5c4a5fe1SAndy Fiddaman if (elen == 0) {
843*5c4a5fe1SAndy Fiddaman if (done >= len) {
844*5c4a5fe1SAndy Fiddaman if (ncq) {
845*5c4a5fe1SAndy Fiddaman if (first)
846*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h_ncq(p, slot);
847*5c4a5fe1SAndy Fiddaman ahci_write_fis_sdb(p, slot, cfis,
848*5c4a5fe1SAndy Fiddaman ATA_S_READY | ATA_S_DSC);
849*5c4a5fe1SAndy Fiddaman } else {
850*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis,
851*5c4a5fe1SAndy Fiddaman ATA_S_READY | ATA_S_DSC);
852*5c4a5fe1SAndy Fiddaman }
853*5c4a5fe1SAndy Fiddaman p->pending &= ~(1 << slot);
854*5c4a5fe1SAndy Fiddaman ahci_check_stopped(p);
855*5c4a5fe1SAndy Fiddaman if (!first)
856*5c4a5fe1SAndy Fiddaman ahci_handle_port(p);
857*5c4a5fe1SAndy Fiddaman return;
858*5c4a5fe1SAndy Fiddaman }
859*5c4a5fe1SAndy Fiddaman goto next;
860*5c4a5fe1SAndy Fiddaman }
861*5c4a5fe1SAndy Fiddaman
862*5c4a5fe1SAndy Fiddaman /*
863*5c4a5fe1SAndy Fiddaman * Pull request off free list
864*5c4a5fe1SAndy Fiddaman */
865*5c4a5fe1SAndy Fiddaman aior = STAILQ_FIRST(&p->iofhd);
866*5c4a5fe1SAndy Fiddaman assert(aior != NULL);
867*5c4a5fe1SAndy Fiddaman STAILQ_REMOVE_HEAD(&p->iofhd, io_flist);
868*5c4a5fe1SAndy Fiddaman aior->cfis = cfis;
869*5c4a5fe1SAndy Fiddaman aior->slot = slot;
870*5c4a5fe1SAndy Fiddaman aior->len = len;
871*5c4a5fe1SAndy Fiddaman aior->done = done;
872*5c4a5fe1SAndy Fiddaman aior->more = (len != done);
873*5c4a5fe1SAndy Fiddaman
874*5c4a5fe1SAndy Fiddaman breq = &aior->io_req;
875*5c4a5fe1SAndy Fiddaman breq->br_offset = elba * blockif_sectsz(p->bctx);
876*5c4a5fe1SAndy Fiddaman breq->br_resid = elen * blockif_sectsz(p->bctx);
877*5c4a5fe1SAndy Fiddaman
878*5c4a5fe1SAndy Fiddaman /*
879*5c4a5fe1SAndy Fiddaman * Mark this command in-flight.
880*5c4a5fe1SAndy Fiddaman */
881*5c4a5fe1SAndy Fiddaman p->pending |= 1 << slot;
882*5c4a5fe1SAndy Fiddaman
883*5c4a5fe1SAndy Fiddaman /*
884*5c4a5fe1SAndy Fiddaman * Stuff request onto busy list
885*5c4a5fe1SAndy Fiddaman */
886*5c4a5fe1SAndy Fiddaman TAILQ_INSERT_HEAD(&p->iobhd, aior, io_blist);
887*5c4a5fe1SAndy Fiddaman
888*5c4a5fe1SAndy Fiddaman if (ncq && first)
889*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h_ncq(p, slot);
890*5c4a5fe1SAndy Fiddaman
891*5c4a5fe1SAndy Fiddaman err = blockif_delete(p->bctx, breq);
892*5c4a5fe1SAndy Fiddaman assert(err == 0);
893*5c4a5fe1SAndy Fiddaman }
894*5c4a5fe1SAndy Fiddaman
895*5c4a5fe1SAndy Fiddaman static inline void
write_prdt(struct ahci_port * p,int slot,uint8_t * cfis,void * buf,unsigned int size)896*5c4a5fe1SAndy Fiddaman write_prdt(struct ahci_port *p, int slot, uint8_t *cfis, void *buf,
897*5c4a5fe1SAndy Fiddaman unsigned int size)
898*5c4a5fe1SAndy Fiddaman {
899*5c4a5fe1SAndy Fiddaman struct ahci_cmd_hdr *hdr;
900*5c4a5fe1SAndy Fiddaman struct ahci_prdt_entry *prdt;
901*5c4a5fe1SAndy Fiddaman uint8_t *from;
902*5c4a5fe1SAndy Fiddaman unsigned int len;
903*5c4a5fe1SAndy Fiddaman int i;
904*5c4a5fe1SAndy Fiddaman
905*5c4a5fe1SAndy Fiddaman hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
906*5c4a5fe1SAndy Fiddaman len = size;
907*5c4a5fe1SAndy Fiddaman from = buf;
908*5c4a5fe1SAndy Fiddaman prdt = (struct ahci_prdt_entry *)(cfis + 0x80);
909*5c4a5fe1SAndy Fiddaman for (i = 0; i < hdr->prdtl && len; i++) {
910*5c4a5fe1SAndy Fiddaman uint8_t *ptr;
911*5c4a5fe1SAndy Fiddaman uint32_t dbcsz;
912*5c4a5fe1SAndy Fiddaman int sublen;
913*5c4a5fe1SAndy Fiddaman
914*5c4a5fe1SAndy Fiddaman dbcsz = (prdt->dbc & DBCMASK) + 1;
915*5c4a5fe1SAndy Fiddaman ptr = paddr_guest2host(ahci_ctx(p->pr_sc), prdt->dba, dbcsz);
916*5c4a5fe1SAndy Fiddaman sublen = MIN(len, dbcsz);
917*5c4a5fe1SAndy Fiddaman memcpy(ptr, from, sublen);
918*5c4a5fe1SAndy Fiddaman len -= sublen;
919*5c4a5fe1SAndy Fiddaman from += sublen;
920*5c4a5fe1SAndy Fiddaman prdt++;
921*5c4a5fe1SAndy Fiddaman }
922*5c4a5fe1SAndy Fiddaman hdr->prdbc = size - len;
923*5c4a5fe1SAndy Fiddaman }
924*5c4a5fe1SAndy Fiddaman
925*5c4a5fe1SAndy Fiddaman static void
ahci_checksum(uint8_t * buf,int size)926*5c4a5fe1SAndy Fiddaman ahci_checksum(uint8_t *buf, int size)
927*5c4a5fe1SAndy Fiddaman {
928*5c4a5fe1SAndy Fiddaman int i;
929*5c4a5fe1SAndy Fiddaman uint8_t sum = 0;
930*5c4a5fe1SAndy Fiddaman
931*5c4a5fe1SAndy Fiddaman for (i = 0; i < size - 1; i++)
932*5c4a5fe1SAndy Fiddaman sum += buf[i];
933*5c4a5fe1SAndy Fiddaman buf[size - 1] = 0x100 - sum;
934*5c4a5fe1SAndy Fiddaman }
935*5c4a5fe1SAndy Fiddaman
936*5c4a5fe1SAndy Fiddaman static void
ahci_handle_read_log(struct ahci_port * p,int slot,uint8_t * cfis)937*5c4a5fe1SAndy Fiddaman ahci_handle_read_log(struct ahci_port *p, int slot, uint8_t *cfis)
938*5c4a5fe1SAndy Fiddaman {
939*5c4a5fe1SAndy Fiddaman struct ahci_cmd_hdr *hdr;
940*5c4a5fe1SAndy Fiddaman uint32_t buf[128];
941*5c4a5fe1SAndy Fiddaman uint8_t *buf8 = (uint8_t *)buf;
942*5c4a5fe1SAndy Fiddaman uint16_t *buf16 = (uint16_t *)buf;
943*5c4a5fe1SAndy Fiddaman
944*5c4a5fe1SAndy Fiddaman hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
945*5c4a5fe1SAndy Fiddaman if (p->atapi || hdr->prdtl == 0 || cfis[5] != 0 ||
946*5c4a5fe1SAndy Fiddaman cfis[9] != 0 || cfis[12] != 1 || cfis[13] != 0) {
947*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis,
948*5c4a5fe1SAndy Fiddaman (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
949*5c4a5fe1SAndy Fiddaman return;
950*5c4a5fe1SAndy Fiddaman }
951*5c4a5fe1SAndy Fiddaman
952*5c4a5fe1SAndy Fiddaman memset(buf, 0, sizeof(buf));
953*5c4a5fe1SAndy Fiddaman if (cfis[4] == 0x00) { /* Log directory */
954*5c4a5fe1SAndy Fiddaman buf16[0x00] = 1; /* Version -- 1 */
955*5c4a5fe1SAndy Fiddaman buf16[0x10] = 1; /* NCQ Command Error Log -- 1 page */
956*5c4a5fe1SAndy Fiddaman buf16[0x13] = 1; /* SATA NCQ Send and Receive Log -- 1 page */
957*5c4a5fe1SAndy Fiddaman } else if (cfis[4] == 0x10) { /* NCQ Command Error Log */
958*5c4a5fe1SAndy Fiddaman memcpy(buf8, p->err_cfis, sizeof(p->err_cfis));
959*5c4a5fe1SAndy Fiddaman ahci_checksum(buf8, sizeof(buf));
960*5c4a5fe1SAndy Fiddaman } else if (cfis[4] == 0x13) { /* SATA NCQ Send and Receive Log */
961*5c4a5fe1SAndy Fiddaman if (blockif_candelete(p->bctx) && !blockif_is_ro(p->bctx)) {
962*5c4a5fe1SAndy Fiddaman buf[0x00] = 1; /* SFQ DSM supported */
963*5c4a5fe1SAndy Fiddaman buf[0x01] = 1; /* SFQ DSM TRIM supported */
964*5c4a5fe1SAndy Fiddaman }
965*5c4a5fe1SAndy Fiddaman } else {
966*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis,
967*5c4a5fe1SAndy Fiddaman (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
968*5c4a5fe1SAndy Fiddaman return;
969*5c4a5fe1SAndy Fiddaman }
970*5c4a5fe1SAndy Fiddaman
971*5c4a5fe1SAndy Fiddaman if (cfis[2] == ATA_READ_LOG_EXT)
972*5c4a5fe1SAndy Fiddaman ahci_write_fis_piosetup(p);
973*5c4a5fe1SAndy Fiddaman write_prdt(p, slot, cfis, (void *)buf, sizeof(buf));
974*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, ATA_S_DSC | ATA_S_READY);
975*5c4a5fe1SAndy Fiddaman }
976*5c4a5fe1SAndy Fiddaman
977*5c4a5fe1SAndy Fiddaman static void
handle_identify(struct ahci_port * p,int slot,uint8_t * cfis)978*5c4a5fe1SAndy Fiddaman handle_identify(struct ahci_port *p, int slot, uint8_t *cfis)
979*5c4a5fe1SAndy Fiddaman {
980*5c4a5fe1SAndy Fiddaman struct ahci_cmd_hdr *hdr;
981*5c4a5fe1SAndy Fiddaman
982*5c4a5fe1SAndy Fiddaman hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
983*5c4a5fe1SAndy Fiddaman if (p->atapi || hdr->prdtl == 0) {
984*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis,
985*5c4a5fe1SAndy Fiddaman (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
986*5c4a5fe1SAndy Fiddaman } else {
987*5c4a5fe1SAndy Fiddaman ahci_write_fis_piosetup(p);
988*5c4a5fe1SAndy Fiddaman write_prdt(p, slot, cfis, (void*)&p->ata_ident, sizeof(struct ata_params));
989*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, ATA_S_DSC | ATA_S_READY);
990*5c4a5fe1SAndy Fiddaman }
991*5c4a5fe1SAndy Fiddaman }
992*5c4a5fe1SAndy Fiddaman
993*5c4a5fe1SAndy Fiddaman static void
ata_identify_init(struct ahci_port * p,int atapi)994*5c4a5fe1SAndy Fiddaman ata_identify_init(struct ahci_port* p, int atapi)
995*5c4a5fe1SAndy Fiddaman {
996*5c4a5fe1SAndy Fiddaman struct ata_params* ata_ident = &p->ata_ident;
997*5c4a5fe1SAndy Fiddaman
998*5c4a5fe1SAndy Fiddaman if (atapi) {
999*5c4a5fe1SAndy Fiddaman ata_ident->config = ATA_PROTO_ATAPI | ATA_ATAPI_TYPE_CDROM |
1000*5c4a5fe1SAndy Fiddaman ATA_ATAPI_REMOVABLE | ATA_DRQ_FAST;
1001*5c4a5fe1SAndy Fiddaman ata_ident->capabilities1 = ATA_SUPPORT_LBA |
1002*5c4a5fe1SAndy Fiddaman ATA_SUPPORT_DMA;
1003*5c4a5fe1SAndy Fiddaman ata_ident->capabilities2 = (1 << 14 | 1);
1004*5c4a5fe1SAndy Fiddaman ata_ident->atavalid = ATA_FLAG_64_70 | ATA_FLAG_88;
1005*5c4a5fe1SAndy Fiddaman ata_ident->obsolete62 = 0x3f;
1006*5c4a5fe1SAndy Fiddaman ata_ident->mwdmamodes = 7;
1007*5c4a5fe1SAndy Fiddaman if (p->xfermode & ATA_WDMA0)
1008*5c4a5fe1SAndy Fiddaman ata_ident->mwdmamodes |= (1 << ((p->xfermode & 7) + 8));
1009*5c4a5fe1SAndy Fiddaman ata_ident->apiomodes = 3;
1010*5c4a5fe1SAndy Fiddaman ata_ident->mwdmamin = 0x0078;
1011*5c4a5fe1SAndy Fiddaman ata_ident->mwdmarec = 0x0078;
1012*5c4a5fe1SAndy Fiddaman ata_ident->pioblind = 0x0078;
1013*5c4a5fe1SAndy Fiddaman ata_ident->pioiordy = 0x0078;
1014*5c4a5fe1SAndy Fiddaman ata_ident->satacapabilities = (ATA_SATA_GEN1 | ATA_SATA_GEN2 | ATA_SATA_GEN3);
1015*5c4a5fe1SAndy Fiddaman ata_ident->satacapabilities2 = ((p->ssts & ATA_SS_SPD_MASK) >> 3);
1016*5c4a5fe1SAndy Fiddaman ata_ident->satasupport = ATA_SUPPORT_NCQ_STREAM;
1017*5c4a5fe1SAndy Fiddaman ata_ident->version_major = 0x3f0;
1018*5c4a5fe1SAndy Fiddaman ata_ident->support.command1 = (ATA_SUPPORT_POWERMGT | ATA_SUPPORT_PACKET |
1019*5c4a5fe1SAndy Fiddaman ATA_SUPPORT_RESET | ATA_SUPPORT_NOP);
1020*5c4a5fe1SAndy Fiddaman ata_ident->support.command2 = (1 << 14);
1021*5c4a5fe1SAndy Fiddaman ata_ident->support.extension = (1 << 14);
1022*5c4a5fe1SAndy Fiddaman ata_ident->enabled.command1 = (ATA_SUPPORT_POWERMGT | ATA_SUPPORT_PACKET |
1023*5c4a5fe1SAndy Fiddaman ATA_SUPPORT_RESET | ATA_SUPPORT_NOP);
1024*5c4a5fe1SAndy Fiddaman ata_ident->enabled.extension = (1 << 14);
1025*5c4a5fe1SAndy Fiddaman ata_ident->udmamodes = 0x7f;
1026*5c4a5fe1SAndy Fiddaman if (p->xfermode & ATA_UDMA0)
1027*5c4a5fe1SAndy Fiddaman ata_ident->udmamodes |= (1 << ((p->xfermode & 7) + 8));
1028*5c4a5fe1SAndy Fiddaman ata_ident->transport_major = 0x1020;
1029*5c4a5fe1SAndy Fiddaman ata_ident->integrity = 0x00a5;
1030*5c4a5fe1SAndy Fiddaman } else {
1031*5c4a5fe1SAndy Fiddaman uint64_t sectors;
1032*5c4a5fe1SAndy Fiddaman int sectsz, psectsz, psectoff, candelete, ro;
1033*5c4a5fe1SAndy Fiddaman uint16_t cyl;
1034*5c4a5fe1SAndy Fiddaman uint8_t sech, heads;
1035*5c4a5fe1SAndy Fiddaman
1036*5c4a5fe1SAndy Fiddaman ro = blockif_is_ro(p->bctx);
1037*5c4a5fe1SAndy Fiddaman candelete = blockif_candelete(p->bctx);
1038*5c4a5fe1SAndy Fiddaman sectsz = blockif_sectsz(p->bctx);
1039*5c4a5fe1SAndy Fiddaman sectors = blockif_size(p->bctx) / sectsz;
1040*5c4a5fe1SAndy Fiddaman blockif_chs(p->bctx, &cyl, &heads, &sech);
1041*5c4a5fe1SAndy Fiddaman blockif_psectsz(p->bctx, &psectsz, &psectoff);
1042*5c4a5fe1SAndy Fiddaman ata_ident->config = ATA_DRQ_FAST;
1043*5c4a5fe1SAndy Fiddaman ata_ident->cylinders = cyl;
1044*5c4a5fe1SAndy Fiddaman ata_ident->heads = heads;
1045*5c4a5fe1SAndy Fiddaman ata_ident->sectors = sech;
1046*5c4a5fe1SAndy Fiddaman
1047*5c4a5fe1SAndy Fiddaman ata_ident->sectors_intr = (0x8000 | 128);
1048*5c4a5fe1SAndy Fiddaman ata_ident->tcg = 0;
1049*5c4a5fe1SAndy Fiddaman
1050*5c4a5fe1SAndy Fiddaman ata_ident->capabilities1 = ATA_SUPPORT_DMA |
1051*5c4a5fe1SAndy Fiddaman ATA_SUPPORT_LBA | ATA_SUPPORT_IORDY;
1052*5c4a5fe1SAndy Fiddaman ata_ident->capabilities2 = (1 << 14);
1053*5c4a5fe1SAndy Fiddaman ata_ident->atavalid = ATA_FLAG_64_70 | ATA_FLAG_88;
1054*5c4a5fe1SAndy Fiddaman if (p->mult_sectors)
1055*5c4a5fe1SAndy Fiddaman ata_ident->multi = (ATA_MULTI_VALID | p->mult_sectors);
1056*5c4a5fe1SAndy Fiddaman if (sectors <= 0x0fffffff) {
1057*5c4a5fe1SAndy Fiddaman ata_ident->lba_size_1 = sectors;
1058*5c4a5fe1SAndy Fiddaman ata_ident->lba_size_2 = (sectors >> 16);
1059*5c4a5fe1SAndy Fiddaman } else {
1060*5c4a5fe1SAndy Fiddaman ata_ident->lba_size_1 = 0xffff;
1061*5c4a5fe1SAndy Fiddaman ata_ident->lba_size_2 = 0x0fff;
1062*5c4a5fe1SAndy Fiddaman }
1063*5c4a5fe1SAndy Fiddaman ata_ident->mwdmamodes = 0x7;
1064*5c4a5fe1SAndy Fiddaman if (p->xfermode & ATA_WDMA0)
1065*5c4a5fe1SAndy Fiddaman ata_ident->mwdmamodes |= (1 << ((p->xfermode & 7) + 8));
1066*5c4a5fe1SAndy Fiddaman ata_ident->apiomodes = 0x3;
1067*5c4a5fe1SAndy Fiddaman ata_ident->mwdmamin = 0x0078;
1068*5c4a5fe1SAndy Fiddaman ata_ident->mwdmarec = 0x0078;
1069*5c4a5fe1SAndy Fiddaman ata_ident->pioblind = 0x0078;
1070*5c4a5fe1SAndy Fiddaman ata_ident->pioiordy = 0x0078;
1071*5c4a5fe1SAndy Fiddaman ata_ident->support3 = 0;
1072*5c4a5fe1SAndy Fiddaman ata_ident->queue = 31;
1073*5c4a5fe1SAndy Fiddaman ata_ident->satacapabilities = (ATA_SATA_GEN1 | ATA_SATA_GEN2 | ATA_SATA_GEN3 |
1074*5c4a5fe1SAndy Fiddaman ATA_SUPPORT_NCQ);
1075*5c4a5fe1SAndy Fiddaman ata_ident->satacapabilities2 = (ATA_SUPPORT_RCVSND_FPDMA_QUEUED |
1076*5c4a5fe1SAndy Fiddaman (p->ssts & ATA_SS_SPD_MASK) >> 3);
1077*5c4a5fe1SAndy Fiddaman ata_ident->version_major = 0x3f0;
1078*5c4a5fe1SAndy Fiddaman ata_ident->version_minor = 0x28;
1079*5c4a5fe1SAndy Fiddaman ata_ident->support.command1 = (ATA_SUPPORT_POWERMGT | ATA_SUPPORT_WRITECACHE |
1080*5c4a5fe1SAndy Fiddaman ATA_SUPPORT_LOOKAHEAD | ATA_SUPPORT_NOP);
1081*5c4a5fe1SAndy Fiddaman ata_ident->support.command2 = (ATA_SUPPORT_ADDRESS48 | ATA_SUPPORT_FLUSHCACHE |
1082*5c4a5fe1SAndy Fiddaman ATA_SUPPORT_FLUSHCACHE48 | 1 << 14);
1083*5c4a5fe1SAndy Fiddaman ata_ident->support.extension = (1 << 14);
1084*5c4a5fe1SAndy Fiddaman ata_ident->enabled.command1 = (ATA_SUPPORT_POWERMGT | ATA_SUPPORT_WRITECACHE |
1085*5c4a5fe1SAndy Fiddaman ATA_SUPPORT_LOOKAHEAD | ATA_SUPPORT_NOP);
1086*5c4a5fe1SAndy Fiddaman ata_ident->enabled.command2 = (ATA_SUPPORT_ADDRESS48 | ATA_SUPPORT_FLUSHCACHE |
1087*5c4a5fe1SAndy Fiddaman ATA_SUPPORT_FLUSHCACHE48 | 1 << 15);
1088*5c4a5fe1SAndy Fiddaman ata_ident->enabled.extension = (1 << 14);
1089*5c4a5fe1SAndy Fiddaman ata_ident->udmamodes = 0x7f;
1090*5c4a5fe1SAndy Fiddaman if (p->xfermode & ATA_UDMA0)
1091*5c4a5fe1SAndy Fiddaman ata_ident->udmamodes |= (1 << ((p->xfermode & 7) + 8));
1092*5c4a5fe1SAndy Fiddaman ata_ident->lba_size48_1 = sectors;
1093*5c4a5fe1SAndy Fiddaman ata_ident->lba_size48_2 = (sectors >> 16);
1094*5c4a5fe1SAndy Fiddaman ata_ident->lba_size48_3 = (sectors >> 32);
1095*5c4a5fe1SAndy Fiddaman ata_ident->lba_size48_4 = (sectors >> 48);
1096*5c4a5fe1SAndy Fiddaman
1097*5c4a5fe1SAndy Fiddaman if (candelete && !ro) {
1098*5c4a5fe1SAndy Fiddaman ata_ident->support3 |= ATA_SUPPORT_RZAT | ATA_SUPPORT_DRAT;
1099*5c4a5fe1SAndy Fiddaman ata_ident->max_dsm_blocks = 1;
1100*5c4a5fe1SAndy Fiddaman ata_ident->support_dsm = ATA_SUPPORT_DSM_TRIM;
1101*5c4a5fe1SAndy Fiddaman }
1102*5c4a5fe1SAndy Fiddaman ata_ident->pss = ATA_PSS_VALID_VALUE;
1103*5c4a5fe1SAndy Fiddaman ata_ident->lsalign = 0x4000;
1104*5c4a5fe1SAndy Fiddaman if (psectsz > sectsz) {
1105*5c4a5fe1SAndy Fiddaman ata_ident->pss |= ATA_PSS_MULTLS;
1106*5c4a5fe1SAndy Fiddaman ata_ident->pss |= ffsl(psectsz / sectsz) - 1;
1107*5c4a5fe1SAndy Fiddaman ata_ident->lsalign |= (psectoff / sectsz);
1108*5c4a5fe1SAndy Fiddaman }
1109*5c4a5fe1SAndy Fiddaman if (sectsz > 512) {
1110*5c4a5fe1SAndy Fiddaman ata_ident->pss |= ATA_PSS_LSSABOVE512;
1111*5c4a5fe1SAndy Fiddaman ata_ident->lss_1 = sectsz / 2;
1112*5c4a5fe1SAndy Fiddaman ata_ident->lss_2 = ((sectsz / 2) >> 16);
1113*5c4a5fe1SAndy Fiddaman }
1114*5c4a5fe1SAndy Fiddaman ata_ident->support2 = (ATA_SUPPORT_RWLOGDMAEXT | 1 << 14);
1115*5c4a5fe1SAndy Fiddaman ata_ident->enabled2 = (ATA_SUPPORT_RWLOGDMAEXT | 1 << 14);
1116*5c4a5fe1SAndy Fiddaman ata_ident->transport_major = 0x1020;
1117*5c4a5fe1SAndy Fiddaman ata_ident->integrity = 0x00a5;
1118*5c4a5fe1SAndy Fiddaman }
1119*5c4a5fe1SAndy Fiddaman ahci_checksum((uint8_t*)ata_ident, sizeof(struct ata_params));
1120*5c4a5fe1SAndy Fiddaman }
1121*5c4a5fe1SAndy Fiddaman
1122*5c4a5fe1SAndy Fiddaman static void
handle_atapi_identify(struct ahci_port * p,int slot,uint8_t * cfis)1123*5c4a5fe1SAndy Fiddaman handle_atapi_identify(struct ahci_port *p, int slot, uint8_t *cfis)
1124*5c4a5fe1SAndy Fiddaman {
1125*5c4a5fe1SAndy Fiddaman if (!p->atapi) {
1126*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis,
1127*5c4a5fe1SAndy Fiddaman (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
1128*5c4a5fe1SAndy Fiddaman } else {
1129*5c4a5fe1SAndy Fiddaman ahci_write_fis_piosetup(p);
1130*5c4a5fe1SAndy Fiddaman write_prdt(p, slot, cfis, (void *)&p->ata_ident, sizeof(struct ata_params));
1131*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, ATA_S_DSC | ATA_S_READY);
1132*5c4a5fe1SAndy Fiddaman }
1133*5c4a5fe1SAndy Fiddaman }
1134*5c4a5fe1SAndy Fiddaman
1135*5c4a5fe1SAndy Fiddaman static void
atapi_inquiry(struct ahci_port * p,int slot,uint8_t * cfis)1136*5c4a5fe1SAndy Fiddaman atapi_inquiry(struct ahci_port *p, int slot, uint8_t *cfis)
1137*5c4a5fe1SAndy Fiddaman {
1138*5c4a5fe1SAndy Fiddaman uint8_t buf[36];
1139*5c4a5fe1SAndy Fiddaman uint8_t *acmd;
1140*5c4a5fe1SAndy Fiddaman unsigned int len;
1141*5c4a5fe1SAndy Fiddaman uint32_t tfd;
1142*5c4a5fe1SAndy Fiddaman
1143*5c4a5fe1SAndy Fiddaman acmd = cfis + 0x40;
1144*5c4a5fe1SAndy Fiddaman
1145*5c4a5fe1SAndy Fiddaman if (acmd[1] & 1) { /* VPD */
1146*5c4a5fe1SAndy Fiddaman if (acmd[2] == 0) { /* Supported VPD pages */
1147*5c4a5fe1SAndy Fiddaman buf[0] = 0x05;
1148*5c4a5fe1SAndy Fiddaman buf[1] = 0;
1149*5c4a5fe1SAndy Fiddaman buf[2] = 0;
1150*5c4a5fe1SAndy Fiddaman buf[3] = 1;
1151*5c4a5fe1SAndy Fiddaman buf[4] = 0;
1152*5c4a5fe1SAndy Fiddaman len = 4 + buf[3];
1153*5c4a5fe1SAndy Fiddaman } else {
1154*5c4a5fe1SAndy Fiddaman p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1155*5c4a5fe1SAndy Fiddaman p->asc = 0x24;
1156*5c4a5fe1SAndy Fiddaman tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1157*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1158*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, tfd);
1159*5c4a5fe1SAndy Fiddaman return;
1160*5c4a5fe1SAndy Fiddaman }
1161*5c4a5fe1SAndy Fiddaman } else {
1162*5c4a5fe1SAndy Fiddaman buf[0] = 0x05;
1163*5c4a5fe1SAndy Fiddaman buf[1] = 0x80;
1164*5c4a5fe1SAndy Fiddaman buf[2] = 0x00;
1165*5c4a5fe1SAndy Fiddaman buf[3] = 0x21;
1166*5c4a5fe1SAndy Fiddaman buf[4] = 31;
1167*5c4a5fe1SAndy Fiddaman buf[5] = 0;
1168*5c4a5fe1SAndy Fiddaman buf[6] = 0;
1169*5c4a5fe1SAndy Fiddaman buf[7] = 0;
1170*5c4a5fe1SAndy Fiddaman atapi_string(buf + 8, "BHYVE", 8);
1171*5c4a5fe1SAndy Fiddaman atapi_string(buf + 16, "BHYVE DVD-ROM", 16);
1172*5c4a5fe1SAndy Fiddaman atapi_string(buf + 32, "001", 4);
1173*5c4a5fe1SAndy Fiddaman len = sizeof(buf);
1174*5c4a5fe1SAndy Fiddaman }
1175*5c4a5fe1SAndy Fiddaman
1176*5c4a5fe1SAndy Fiddaman if (len > acmd[4])
1177*5c4a5fe1SAndy Fiddaman len = acmd[4];
1178*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1179*5c4a5fe1SAndy Fiddaman write_prdt(p, slot, cfis, buf, len);
1180*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1181*5c4a5fe1SAndy Fiddaman }
1182*5c4a5fe1SAndy Fiddaman
1183*5c4a5fe1SAndy Fiddaman static void
atapi_read_capacity(struct ahci_port * p,int slot,uint8_t * cfis)1184*5c4a5fe1SAndy Fiddaman atapi_read_capacity(struct ahci_port *p, int slot, uint8_t *cfis)
1185*5c4a5fe1SAndy Fiddaman {
1186*5c4a5fe1SAndy Fiddaman uint8_t buf[8];
1187*5c4a5fe1SAndy Fiddaman uint64_t sectors;
1188*5c4a5fe1SAndy Fiddaman
1189*5c4a5fe1SAndy Fiddaman sectors = blockif_size(p->bctx) / 2048;
1190*5c4a5fe1SAndy Fiddaman be32enc(buf, sectors - 1);
1191*5c4a5fe1SAndy Fiddaman be32enc(buf + 4, 2048);
1192*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1193*5c4a5fe1SAndy Fiddaman write_prdt(p, slot, cfis, buf, sizeof(buf));
1194*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1195*5c4a5fe1SAndy Fiddaman }
1196*5c4a5fe1SAndy Fiddaman
1197*5c4a5fe1SAndy Fiddaman static void
atapi_read_toc(struct ahci_port * p,int slot,uint8_t * cfis)1198*5c4a5fe1SAndy Fiddaman atapi_read_toc(struct ahci_port *p, int slot, uint8_t *cfis)
1199*5c4a5fe1SAndy Fiddaman {
1200*5c4a5fe1SAndy Fiddaman uint8_t *acmd;
1201*5c4a5fe1SAndy Fiddaman uint8_t format;
1202*5c4a5fe1SAndy Fiddaman unsigned int len;
1203*5c4a5fe1SAndy Fiddaman
1204*5c4a5fe1SAndy Fiddaman acmd = cfis + 0x40;
1205*5c4a5fe1SAndy Fiddaman
1206*5c4a5fe1SAndy Fiddaman len = be16dec(acmd + 7);
1207*5c4a5fe1SAndy Fiddaman format = acmd[9] >> 6;
1208*5c4a5fe1SAndy Fiddaman switch (format) {
1209*5c4a5fe1SAndy Fiddaman case 0:
1210*5c4a5fe1SAndy Fiddaman {
1211*5c4a5fe1SAndy Fiddaman size_t size;
1212*5c4a5fe1SAndy Fiddaman int msf;
1213*5c4a5fe1SAndy Fiddaman uint64_t sectors;
1214*5c4a5fe1SAndy Fiddaman uint8_t start_track, buf[20], *bp;
1215*5c4a5fe1SAndy Fiddaman
1216*5c4a5fe1SAndy Fiddaman msf = (acmd[1] >> 1) & 1;
1217*5c4a5fe1SAndy Fiddaman start_track = acmd[6];
1218*5c4a5fe1SAndy Fiddaman if (start_track > 1 && start_track != 0xaa) {
1219*5c4a5fe1SAndy Fiddaman uint32_t tfd;
1220*5c4a5fe1SAndy Fiddaman p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1221*5c4a5fe1SAndy Fiddaman p->asc = 0x24;
1222*5c4a5fe1SAndy Fiddaman tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1223*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1224*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, tfd);
1225*5c4a5fe1SAndy Fiddaman return;
1226*5c4a5fe1SAndy Fiddaman }
1227*5c4a5fe1SAndy Fiddaman bp = buf + 2;
1228*5c4a5fe1SAndy Fiddaman *bp++ = 1;
1229*5c4a5fe1SAndy Fiddaman *bp++ = 1;
1230*5c4a5fe1SAndy Fiddaman if (start_track <= 1) {
1231*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1232*5c4a5fe1SAndy Fiddaman *bp++ = 0x14;
1233*5c4a5fe1SAndy Fiddaman *bp++ = 1;
1234*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1235*5c4a5fe1SAndy Fiddaman if (msf) {
1236*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1237*5c4a5fe1SAndy Fiddaman lba_to_msf(bp, 0);
1238*5c4a5fe1SAndy Fiddaman bp += 3;
1239*5c4a5fe1SAndy Fiddaman } else {
1240*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1241*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1242*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1243*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1244*5c4a5fe1SAndy Fiddaman }
1245*5c4a5fe1SAndy Fiddaman }
1246*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1247*5c4a5fe1SAndy Fiddaman *bp++ = 0x14;
1248*5c4a5fe1SAndy Fiddaman *bp++ = 0xaa;
1249*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1250*5c4a5fe1SAndy Fiddaman sectors = blockif_size(p->bctx) / blockif_sectsz(p->bctx);
1251*5c4a5fe1SAndy Fiddaman sectors >>= 2;
1252*5c4a5fe1SAndy Fiddaman if (msf) {
1253*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1254*5c4a5fe1SAndy Fiddaman lba_to_msf(bp, sectors);
1255*5c4a5fe1SAndy Fiddaman bp += 3;
1256*5c4a5fe1SAndy Fiddaman } else {
1257*5c4a5fe1SAndy Fiddaman be32enc(bp, sectors);
1258*5c4a5fe1SAndy Fiddaman bp += 4;
1259*5c4a5fe1SAndy Fiddaman }
1260*5c4a5fe1SAndy Fiddaman size = bp - buf;
1261*5c4a5fe1SAndy Fiddaman be16enc(buf, size - 2);
1262*5c4a5fe1SAndy Fiddaman if (len > size)
1263*5c4a5fe1SAndy Fiddaman len = size;
1264*5c4a5fe1SAndy Fiddaman write_prdt(p, slot, cfis, buf, len);
1265*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1266*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1267*5c4a5fe1SAndy Fiddaman break;
1268*5c4a5fe1SAndy Fiddaman }
1269*5c4a5fe1SAndy Fiddaman case 1:
1270*5c4a5fe1SAndy Fiddaman {
1271*5c4a5fe1SAndy Fiddaman uint8_t buf[12];
1272*5c4a5fe1SAndy Fiddaman
1273*5c4a5fe1SAndy Fiddaman memset(buf, 0, sizeof(buf));
1274*5c4a5fe1SAndy Fiddaman buf[1] = 0xa;
1275*5c4a5fe1SAndy Fiddaman buf[2] = 0x1;
1276*5c4a5fe1SAndy Fiddaman buf[3] = 0x1;
1277*5c4a5fe1SAndy Fiddaman if (len > sizeof(buf))
1278*5c4a5fe1SAndy Fiddaman len = sizeof(buf);
1279*5c4a5fe1SAndy Fiddaman write_prdt(p, slot, cfis, buf, len);
1280*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1281*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1282*5c4a5fe1SAndy Fiddaman break;
1283*5c4a5fe1SAndy Fiddaman }
1284*5c4a5fe1SAndy Fiddaman case 2:
1285*5c4a5fe1SAndy Fiddaman {
1286*5c4a5fe1SAndy Fiddaman size_t size;
1287*5c4a5fe1SAndy Fiddaman int msf;
1288*5c4a5fe1SAndy Fiddaman uint64_t sectors;
1289*5c4a5fe1SAndy Fiddaman uint8_t *bp, buf[50];
1290*5c4a5fe1SAndy Fiddaman
1291*5c4a5fe1SAndy Fiddaman msf = (acmd[1] >> 1) & 1;
1292*5c4a5fe1SAndy Fiddaman bp = buf + 2;
1293*5c4a5fe1SAndy Fiddaman *bp++ = 1;
1294*5c4a5fe1SAndy Fiddaman *bp++ = 1;
1295*5c4a5fe1SAndy Fiddaman
1296*5c4a5fe1SAndy Fiddaman *bp++ = 1;
1297*5c4a5fe1SAndy Fiddaman *bp++ = 0x14;
1298*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1299*5c4a5fe1SAndy Fiddaman *bp++ = 0xa0;
1300*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1301*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1302*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1303*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1304*5c4a5fe1SAndy Fiddaman *bp++ = 1;
1305*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1306*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1307*5c4a5fe1SAndy Fiddaman
1308*5c4a5fe1SAndy Fiddaman *bp++ = 1;
1309*5c4a5fe1SAndy Fiddaman *bp++ = 0x14;
1310*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1311*5c4a5fe1SAndy Fiddaman *bp++ = 0xa1;
1312*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1313*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1314*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1315*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1316*5c4a5fe1SAndy Fiddaman *bp++ = 1;
1317*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1318*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1319*5c4a5fe1SAndy Fiddaman
1320*5c4a5fe1SAndy Fiddaman *bp++ = 1;
1321*5c4a5fe1SAndy Fiddaman *bp++ = 0x14;
1322*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1323*5c4a5fe1SAndy Fiddaman *bp++ = 0xa2;
1324*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1325*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1326*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1327*5c4a5fe1SAndy Fiddaman sectors = blockif_size(p->bctx) / blockif_sectsz(p->bctx);
1328*5c4a5fe1SAndy Fiddaman sectors >>= 2;
1329*5c4a5fe1SAndy Fiddaman if (msf) {
1330*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1331*5c4a5fe1SAndy Fiddaman lba_to_msf(bp, sectors);
1332*5c4a5fe1SAndy Fiddaman bp += 3;
1333*5c4a5fe1SAndy Fiddaman } else {
1334*5c4a5fe1SAndy Fiddaman be32enc(bp, sectors);
1335*5c4a5fe1SAndy Fiddaman bp += 4;
1336*5c4a5fe1SAndy Fiddaman }
1337*5c4a5fe1SAndy Fiddaman
1338*5c4a5fe1SAndy Fiddaman *bp++ = 1;
1339*5c4a5fe1SAndy Fiddaman *bp++ = 0x14;
1340*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1341*5c4a5fe1SAndy Fiddaman *bp++ = 1;
1342*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1343*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1344*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1345*5c4a5fe1SAndy Fiddaman if (msf) {
1346*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1347*5c4a5fe1SAndy Fiddaman lba_to_msf(bp, 0);
1348*5c4a5fe1SAndy Fiddaman bp += 3;
1349*5c4a5fe1SAndy Fiddaman } else {
1350*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1351*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1352*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1353*5c4a5fe1SAndy Fiddaman *bp++ = 0;
1354*5c4a5fe1SAndy Fiddaman }
1355*5c4a5fe1SAndy Fiddaman
1356*5c4a5fe1SAndy Fiddaman size = bp - buf;
1357*5c4a5fe1SAndy Fiddaman be16enc(buf, size - 2);
1358*5c4a5fe1SAndy Fiddaman if (len > size)
1359*5c4a5fe1SAndy Fiddaman len = size;
1360*5c4a5fe1SAndy Fiddaman write_prdt(p, slot, cfis, buf, len);
1361*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1362*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1363*5c4a5fe1SAndy Fiddaman break;
1364*5c4a5fe1SAndy Fiddaman }
1365*5c4a5fe1SAndy Fiddaman default:
1366*5c4a5fe1SAndy Fiddaman {
1367*5c4a5fe1SAndy Fiddaman uint32_t tfd;
1368*5c4a5fe1SAndy Fiddaman
1369*5c4a5fe1SAndy Fiddaman p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1370*5c4a5fe1SAndy Fiddaman p->asc = 0x24;
1371*5c4a5fe1SAndy Fiddaman tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1372*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1373*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, tfd);
1374*5c4a5fe1SAndy Fiddaman break;
1375*5c4a5fe1SAndy Fiddaman }
1376*5c4a5fe1SAndy Fiddaman }
1377*5c4a5fe1SAndy Fiddaman }
1378*5c4a5fe1SAndy Fiddaman
1379*5c4a5fe1SAndy Fiddaman static void
atapi_report_luns(struct ahci_port * p,int slot,uint8_t * cfis)1380*5c4a5fe1SAndy Fiddaman atapi_report_luns(struct ahci_port *p, int slot, uint8_t *cfis)
1381*5c4a5fe1SAndy Fiddaman {
1382*5c4a5fe1SAndy Fiddaman uint8_t buf[16];
1383*5c4a5fe1SAndy Fiddaman
1384*5c4a5fe1SAndy Fiddaman memset(buf, 0, sizeof(buf));
1385*5c4a5fe1SAndy Fiddaman buf[3] = 8;
1386*5c4a5fe1SAndy Fiddaman
1387*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1388*5c4a5fe1SAndy Fiddaman write_prdt(p, slot, cfis, buf, sizeof(buf));
1389*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1390*5c4a5fe1SAndy Fiddaman }
1391*5c4a5fe1SAndy Fiddaman
1392*5c4a5fe1SAndy Fiddaman static void
atapi_read(struct ahci_port * p,int slot,uint8_t * cfis,uint32_t done)1393*5c4a5fe1SAndy Fiddaman atapi_read(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t done)
1394*5c4a5fe1SAndy Fiddaman {
1395*5c4a5fe1SAndy Fiddaman struct ahci_ioreq *aior;
1396*5c4a5fe1SAndy Fiddaman struct ahci_cmd_hdr *hdr;
1397*5c4a5fe1SAndy Fiddaman struct ahci_prdt_entry *prdt;
1398*5c4a5fe1SAndy Fiddaman struct blockif_req *breq;
1399*5c4a5fe1SAndy Fiddaman uint8_t *acmd;
1400*5c4a5fe1SAndy Fiddaman uint64_t lba;
1401*5c4a5fe1SAndy Fiddaman uint32_t len;
1402*5c4a5fe1SAndy Fiddaman int err;
1403*5c4a5fe1SAndy Fiddaman
1404*5c4a5fe1SAndy Fiddaman acmd = cfis + 0x40;
1405*5c4a5fe1SAndy Fiddaman hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
1406*5c4a5fe1SAndy Fiddaman prdt = (struct ahci_prdt_entry *)(cfis + 0x80);
1407*5c4a5fe1SAndy Fiddaman
1408*5c4a5fe1SAndy Fiddaman lba = be32dec(acmd + 2);
1409*5c4a5fe1SAndy Fiddaman if (acmd[0] == READ_10)
1410*5c4a5fe1SAndy Fiddaman len = be16dec(acmd + 7);
1411*5c4a5fe1SAndy Fiddaman else
1412*5c4a5fe1SAndy Fiddaman len = be32dec(acmd + 6);
1413*5c4a5fe1SAndy Fiddaman if (len == 0) {
1414*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1415*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1416*5c4a5fe1SAndy Fiddaman }
1417*5c4a5fe1SAndy Fiddaman lba *= 2048;
1418*5c4a5fe1SAndy Fiddaman len *= 2048;
1419*5c4a5fe1SAndy Fiddaman
1420*5c4a5fe1SAndy Fiddaman /*
1421*5c4a5fe1SAndy Fiddaman * Pull request off free list
1422*5c4a5fe1SAndy Fiddaman */
1423*5c4a5fe1SAndy Fiddaman aior = STAILQ_FIRST(&p->iofhd);
1424*5c4a5fe1SAndy Fiddaman assert(aior != NULL);
1425*5c4a5fe1SAndy Fiddaman STAILQ_REMOVE_HEAD(&p->iofhd, io_flist);
1426*5c4a5fe1SAndy Fiddaman aior->cfis = cfis;
1427*5c4a5fe1SAndy Fiddaman aior->slot = slot;
1428*5c4a5fe1SAndy Fiddaman aior->len = len;
1429*5c4a5fe1SAndy Fiddaman aior->done = done;
1430*5c4a5fe1SAndy Fiddaman breq = &aior->io_req;
1431*5c4a5fe1SAndy Fiddaman breq->br_offset = lba + done;
1432*5c4a5fe1SAndy Fiddaman ahci_build_iov(p, aior, prdt, hdr->prdtl);
1433*5c4a5fe1SAndy Fiddaman
1434*5c4a5fe1SAndy Fiddaman /* Mark this command in-flight. */
1435*5c4a5fe1SAndy Fiddaman p->pending |= 1 << slot;
1436*5c4a5fe1SAndy Fiddaman
1437*5c4a5fe1SAndy Fiddaman /* Stuff request onto busy list. */
1438*5c4a5fe1SAndy Fiddaman TAILQ_INSERT_HEAD(&p->iobhd, aior, io_blist);
1439*5c4a5fe1SAndy Fiddaman
1440*5c4a5fe1SAndy Fiddaman err = blockif_read(p->bctx, breq);
1441*5c4a5fe1SAndy Fiddaman assert(err == 0);
1442*5c4a5fe1SAndy Fiddaman }
1443*5c4a5fe1SAndy Fiddaman
1444*5c4a5fe1SAndy Fiddaman static void
atapi_request_sense(struct ahci_port * p,int slot,uint8_t * cfis)1445*5c4a5fe1SAndy Fiddaman atapi_request_sense(struct ahci_port *p, int slot, uint8_t *cfis)
1446*5c4a5fe1SAndy Fiddaman {
1447*5c4a5fe1SAndy Fiddaman uint8_t buf[64];
1448*5c4a5fe1SAndy Fiddaman uint8_t *acmd;
1449*5c4a5fe1SAndy Fiddaman unsigned int len;
1450*5c4a5fe1SAndy Fiddaman
1451*5c4a5fe1SAndy Fiddaman acmd = cfis + 0x40;
1452*5c4a5fe1SAndy Fiddaman len = acmd[4];
1453*5c4a5fe1SAndy Fiddaman if (len > sizeof(buf))
1454*5c4a5fe1SAndy Fiddaman len = sizeof(buf);
1455*5c4a5fe1SAndy Fiddaman memset(buf, 0, len);
1456*5c4a5fe1SAndy Fiddaman buf[0] = 0x70 | (1 << 7);
1457*5c4a5fe1SAndy Fiddaman buf[2] = p->sense_key;
1458*5c4a5fe1SAndy Fiddaman buf[7] = 10;
1459*5c4a5fe1SAndy Fiddaman buf[12] = p->asc;
1460*5c4a5fe1SAndy Fiddaman write_prdt(p, slot, cfis, buf, len);
1461*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1462*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1463*5c4a5fe1SAndy Fiddaman }
1464*5c4a5fe1SAndy Fiddaman
1465*5c4a5fe1SAndy Fiddaman static void
atapi_start_stop_unit(struct ahci_port * p,int slot,uint8_t * cfis)1466*5c4a5fe1SAndy Fiddaman atapi_start_stop_unit(struct ahci_port *p, int slot, uint8_t *cfis)
1467*5c4a5fe1SAndy Fiddaman {
1468*5c4a5fe1SAndy Fiddaman uint8_t *acmd = cfis + 0x40;
1469*5c4a5fe1SAndy Fiddaman uint32_t tfd;
1470*5c4a5fe1SAndy Fiddaman
1471*5c4a5fe1SAndy Fiddaman switch (acmd[4] & 3) {
1472*5c4a5fe1SAndy Fiddaman case 0:
1473*5c4a5fe1SAndy Fiddaman case 1:
1474*5c4a5fe1SAndy Fiddaman case 3:
1475*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1476*5c4a5fe1SAndy Fiddaman tfd = ATA_S_READY | ATA_S_DSC;
1477*5c4a5fe1SAndy Fiddaman break;
1478*5c4a5fe1SAndy Fiddaman case 2:
1479*5c4a5fe1SAndy Fiddaman /* TODO eject media */
1480*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1481*5c4a5fe1SAndy Fiddaman p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1482*5c4a5fe1SAndy Fiddaman p->asc = 0x53;
1483*5c4a5fe1SAndy Fiddaman tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1484*5c4a5fe1SAndy Fiddaman break;
1485*5c4a5fe1SAndy Fiddaman }
1486*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, tfd);
1487*5c4a5fe1SAndy Fiddaman }
1488*5c4a5fe1SAndy Fiddaman
1489*5c4a5fe1SAndy Fiddaman static void
atapi_mode_sense(struct ahci_port * p,int slot,uint8_t * cfis)1490*5c4a5fe1SAndy Fiddaman atapi_mode_sense(struct ahci_port *p, int slot, uint8_t *cfis)
1491*5c4a5fe1SAndy Fiddaman {
1492*5c4a5fe1SAndy Fiddaman uint8_t *acmd;
1493*5c4a5fe1SAndy Fiddaman uint32_t tfd = 0;
1494*5c4a5fe1SAndy Fiddaman uint8_t pc, code;
1495*5c4a5fe1SAndy Fiddaman unsigned int len;
1496*5c4a5fe1SAndy Fiddaman
1497*5c4a5fe1SAndy Fiddaman acmd = cfis + 0x40;
1498*5c4a5fe1SAndy Fiddaman len = be16dec(acmd + 7);
1499*5c4a5fe1SAndy Fiddaman pc = acmd[2] >> 6;
1500*5c4a5fe1SAndy Fiddaman code = acmd[2] & 0x3f;
1501*5c4a5fe1SAndy Fiddaman
1502*5c4a5fe1SAndy Fiddaman switch (pc) {
1503*5c4a5fe1SAndy Fiddaman case 0:
1504*5c4a5fe1SAndy Fiddaman switch (code) {
1505*5c4a5fe1SAndy Fiddaman case MODEPAGE_RW_ERROR_RECOVERY:
1506*5c4a5fe1SAndy Fiddaman {
1507*5c4a5fe1SAndy Fiddaman uint8_t buf[16];
1508*5c4a5fe1SAndy Fiddaman
1509*5c4a5fe1SAndy Fiddaman if (len > sizeof(buf))
1510*5c4a5fe1SAndy Fiddaman len = sizeof(buf);
1511*5c4a5fe1SAndy Fiddaman
1512*5c4a5fe1SAndy Fiddaman memset(buf, 0, sizeof(buf));
1513*5c4a5fe1SAndy Fiddaman be16enc(buf, 16 - 2);
1514*5c4a5fe1SAndy Fiddaman buf[2] = 0x70;
1515*5c4a5fe1SAndy Fiddaman buf[8] = 0x01;
1516*5c4a5fe1SAndy Fiddaman buf[9] = 16 - 10;
1517*5c4a5fe1SAndy Fiddaman buf[11] = 0x05;
1518*5c4a5fe1SAndy Fiddaman write_prdt(p, slot, cfis, buf, len);
1519*5c4a5fe1SAndy Fiddaman tfd = ATA_S_READY | ATA_S_DSC;
1520*5c4a5fe1SAndy Fiddaman break;
1521*5c4a5fe1SAndy Fiddaman }
1522*5c4a5fe1SAndy Fiddaman case MODEPAGE_CD_CAPABILITIES:
1523*5c4a5fe1SAndy Fiddaman {
1524*5c4a5fe1SAndy Fiddaman uint8_t buf[30];
1525*5c4a5fe1SAndy Fiddaman
1526*5c4a5fe1SAndy Fiddaman if (len > sizeof(buf))
1527*5c4a5fe1SAndy Fiddaman len = sizeof(buf);
1528*5c4a5fe1SAndy Fiddaman
1529*5c4a5fe1SAndy Fiddaman memset(buf, 0, sizeof(buf));
1530*5c4a5fe1SAndy Fiddaman be16enc(buf, 30 - 2);
1531*5c4a5fe1SAndy Fiddaman buf[2] = 0x70;
1532*5c4a5fe1SAndy Fiddaman buf[8] = 0x2A;
1533*5c4a5fe1SAndy Fiddaman buf[9] = 30 - 10;
1534*5c4a5fe1SAndy Fiddaman buf[10] = 0x08;
1535*5c4a5fe1SAndy Fiddaman buf[12] = 0x71;
1536*5c4a5fe1SAndy Fiddaman be16enc(&buf[18], 2);
1537*5c4a5fe1SAndy Fiddaman be16enc(&buf[20], 512);
1538*5c4a5fe1SAndy Fiddaman write_prdt(p, slot, cfis, buf, len);
1539*5c4a5fe1SAndy Fiddaman tfd = ATA_S_READY | ATA_S_DSC;
1540*5c4a5fe1SAndy Fiddaman break;
1541*5c4a5fe1SAndy Fiddaman }
1542*5c4a5fe1SAndy Fiddaman default:
1543*5c4a5fe1SAndy Fiddaman goto error;
1544*5c4a5fe1SAndy Fiddaman break;
1545*5c4a5fe1SAndy Fiddaman }
1546*5c4a5fe1SAndy Fiddaman break;
1547*5c4a5fe1SAndy Fiddaman case 3:
1548*5c4a5fe1SAndy Fiddaman p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1549*5c4a5fe1SAndy Fiddaman p->asc = 0x39;
1550*5c4a5fe1SAndy Fiddaman tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1551*5c4a5fe1SAndy Fiddaman break;
1552*5c4a5fe1SAndy Fiddaman error:
1553*5c4a5fe1SAndy Fiddaman case 1:
1554*5c4a5fe1SAndy Fiddaman case 2:
1555*5c4a5fe1SAndy Fiddaman p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1556*5c4a5fe1SAndy Fiddaman p->asc = 0x24;
1557*5c4a5fe1SAndy Fiddaman tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1558*5c4a5fe1SAndy Fiddaman break;
1559*5c4a5fe1SAndy Fiddaman }
1560*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1561*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, tfd);
1562*5c4a5fe1SAndy Fiddaman }
1563*5c4a5fe1SAndy Fiddaman
1564*5c4a5fe1SAndy Fiddaman static void
atapi_get_event_status_notification(struct ahci_port * p,int slot,uint8_t * cfis)1565*5c4a5fe1SAndy Fiddaman atapi_get_event_status_notification(struct ahci_port *p, int slot,
1566*5c4a5fe1SAndy Fiddaman uint8_t *cfis)
1567*5c4a5fe1SAndy Fiddaman {
1568*5c4a5fe1SAndy Fiddaman uint8_t *acmd;
1569*5c4a5fe1SAndy Fiddaman uint32_t tfd;
1570*5c4a5fe1SAndy Fiddaman
1571*5c4a5fe1SAndy Fiddaman acmd = cfis + 0x40;
1572*5c4a5fe1SAndy Fiddaman
1573*5c4a5fe1SAndy Fiddaman /* we don't support asynchronous operation */
1574*5c4a5fe1SAndy Fiddaman if (!(acmd[1] & 1)) {
1575*5c4a5fe1SAndy Fiddaman p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1576*5c4a5fe1SAndy Fiddaman p->asc = 0x24;
1577*5c4a5fe1SAndy Fiddaman tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
1578*5c4a5fe1SAndy Fiddaman } else {
1579*5c4a5fe1SAndy Fiddaman uint8_t buf[8];
1580*5c4a5fe1SAndy Fiddaman unsigned int len;
1581*5c4a5fe1SAndy Fiddaman
1582*5c4a5fe1SAndy Fiddaman len = be16dec(acmd + 7);
1583*5c4a5fe1SAndy Fiddaman if (len > sizeof(buf))
1584*5c4a5fe1SAndy Fiddaman len = sizeof(buf);
1585*5c4a5fe1SAndy Fiddaman
1586*5c4a5fe1SAndy Fiddaman memset(buf, 0, sizeof(buf));
1587*5c4a5fe1SAndy Fiddaman be16enc(buf, 8 - 2);
1588*5c4a5fe1SAndy Fiddaman buf[2] = 0x04;
1589*5c4a5fe1SAndy Fiddaman buf[3] = 0x10;
1590*5c4a5fe1SAndy Fiddaman buf[5] = 0x02;
1591*5c4a5fe1SAndy Fiddaman write_prdt(p, slot, cfis, buf, len);
1592*5c4a5fe1SAndy Fiddaman tfd = ATA_S_READY | ATA_S_DSC;
1593*5c4a5fe1SAndy Fiddaman }
1594*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1595*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, tfd);
1596*5c4a5fe1SAndy Fiddaman }
1597*5c4a5fe1SAndy Fiddaman
1598*5c4a5fe1SAndy Fiddaman static void
handle_packet_cmd(struct ahci_port * p,int slot,uint8_t * cfis)1599*5c4a5fe1SAndy Fiddaman handle_packet_cmd(struct ahci_port *p, int slot, uint8_t *cfis)
1600*5c4a5fe1SAndy Fiddaman {
1601*5c4a5fe1SAndy Fiddaman uint8_t *acmd;
1602*5c4a5fe1SAndy Fiddaman
1603*5c4a5fe1SAndy Fiddaman acmd = cfis + 0x40;
1604*5c4a5fe1SAndy Fiddaman
1605*5c4a5fe1SAndy Fiddaman #ifdef AHCI_DEBUG
1606*5c4a5fe1SAndy Fiddaman {
1607*5c4a5fe1SAndy Fiddaman int i;
1608*5c4a5fe1SAndy Fiddaman DPRINTF("ACMD:");
1609*5c4a5fe1SAndy Fiddaman for (i = 0; i < 16; i++)
1610*5c4a5fe1SAndy Fiddaman DPRINTF("%02x ", acmd[i]);
1611*5c4a5fe1SAndy Fiddaman DPRINTF("");
1612*5c4a5fe1SAndy Fiddaman }
1613*5c4a5fe1SAndy Fiddaman #endif
1614*5c4a5fe1SAndy Fiddaman
1615*5c4a5fe1SAndy Fiddaman switch (acmd[0]) {
1616*5c4a5fe1SAndy Fiddaman case TEST_UNIT_READY:
1617*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1618*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1619*5c4a5fe1SAndy Fiddaman break;
1620*5c4a5fe1SAndy Fiddaman case INQUIRY:
1621*5c4a5fe1SAndy Fiddaman atapi_inquiry(p, slot, cfis);
1622*5c4a5fe1SAndy Fiddaman break;
1623*5c4a5fe1SAndy Fiddaman case READ_CAPACITY:
1624*5c4a5fe1SAndy Fiddaman atapi_read_capacity(p, slot, cfis);
1625*5c4a5fe1SAndy Fiddaman break;
1626*5c4a5fe1SAndy Fiddaman case PREVENT_ALLOW:
1627*5c4a5fe1SAndy Fiddaman /* TODO */
1628*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1629*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1630*5c4a5fe1SAndy Fiddaman break;
1631*5c4a5fe1SAndy Fiddaman case READ_TOC:
1632*5c4a5fe1SAndy Fiddaman atapi_read_toc(p, slot, cfis);
1633*5c4a5fe1SAndy Fiddaman break;
1634*5c4a5fe1SAndy Fiddaman case REPORT_LUNS:
1635*5c4a5fe1SAndy Fiddaman atapi_report_luns(p, slot, cfis);
1636*5c4a5fe1SAndy Fiddaman break;
1637*5c4a5fe1SAndy Fiddaman case READ_10:
1638*5c4a5fe1SAndy Fiddaman case READ_12:
1639*5c4a5fe1SAndy Fiddaman atapi_read(p, slot, cfis, 0);
1640*5c4a5fe1SAndy Fiddaman break;
1641*5c4a5fe1SAndy Fiddaman case REQUEST_SENSE:
1642*5c4a5fe1SAndy Fiddaman atapi_request_sense(p, slot, cfis);
1643*5c4a5fe1SAndy Fiddaman break;
1644*5c4a5fe1SAndy Fiddaman case START_STOP_UNIT:
1645*5c4a5fe1SAndy Fiddaman atapi_start_stop_unit(p, slot, cfis);
1646*5c4a5fe1SAndy Fiddaman break;
1647*5c4a5fe1SAndy Fiddaman case MODE_SENSE_10:
1648*5c4a5fe1SAndy Fiddaman atapi_mode_sense(p, slot, cfis);
1649*5c4a5fe1SAndy Fiddaman break;
1650*5c4a5fe1SAndy Fiddaman case GET_EVENT_STATUS_NOTIFICATION:
1651*5c4a5fe1SAndy Fiddaman atapi_get_event_status_notification(p, slot, cfis);
1652*5c4a5fe1SAndy Fiddaman break;
1653*5c4a5fe1SAndy Fiddaman default:
1654*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
1655*5c4a5fe1SAndy Fiddaman p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
1656*5c4a5fe1SAndy Fiddaman p->asc = 0x20;
1657*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, (p->sense_key << 12) |
1658*5c4a5fe1SAndy Fiddaman ATA_S_READY | ATA_S_ERROR);
1659*5c4a5fe1SAndy Fiddaman break;
1660*5c4a5fe1SAndy Fiddaman }
1661*5c4a5fe1SAndy Fiddaman }
1662*5c4a5fe1SAndy Fiddaman
1663*5c4a5fe1SAndy Fiddaman static void
ahci_handle_cmd(struct ahci_port * p,int slot,uint8_t * cfis)1664*5c4a5fe1SAndy Fiddaman ahci_handle_cmd(struct ahci_port *p, int slot, uint8_t *cfis)
1665*5c4a5fe1SAndy Fiddaman {
1666*5c4a5fe1SAndy Fiddaman
1667*5c4a5fe1SAndy Fiddaman p->tfd |= ATA_S_BUSY;
1668*5c4a5fe1SAndy Fiddaman switch (cfis[2]) {
1669*5c4a5fe1SAndy Fiddaman case ATA_ATA_IDENTIFY:
1670*5c4a5fe1SAndy Fiddaman handle_identify(p, slot, cfis);
1671*5c4a5fe1SAndy Fiddaman break;
1672*5c4a5fe1SAndy Fiddaman case ATA_SETFEATURES:
1673*5c4a5fe1SAndy Fiddaman {
1674*5c4a5fe1SAndy Fiddaman switch (cfis[3]) {
1675*5c4a5fe1SAndy Fiddaman case ATA_SF_ENAB_SATA_SF:
1676*5c4a5fe1SAndy Fiddaman switch (cfis[12]) {
1677*5c4a5fe1SAndy Fiddaman case ATA_SATA_SF_AN:
1678*5c4a5fe1SAndy Fiddaman p->tfd = ATA_S_DSC | ATA_S_READY;
1679*5c4a5fe1SAndy Fiddaman break;
1680*5c4a5fe1SAndy Fiddaman default:
1681*5c4a5fe1SAndy Fiddaman p->tfd = ATA_S_ERROR | ATA_S_READY;
1682*5c4a5fe1SAndy Fiddaman p->tfd |= (ATA_ERROR_ABORT << 8);
1683*5c4a5fe1SAndy Fiddaman break;
1684*5c4a5fe1SAndy Fiddaman }
1685*5c4a5fe1SAndy Fiddaman break;
1686*5c4a5fe1SAndy Fiddaman case ATA_SF_ENAB_WCACHE:
1687*5c4a5fe1SAndy Fiddaman case ATA_SF_DIS_WCACHE:
1688*5c4a5fe1SAndy Fiddaman case ATA_SF_ENAB_RCACHE:
1689*5c4a5fe1SAndy Fiddaman case ATA_SF_DIS_RCACHE:
1690*5c4a5fe1SAndy Fiddaman p->tfd = ATA_S_DSC | ATA_S_READY;
1691*5c4a5fe1SAndy Fiddaman break;
1692*5c4a5fe1SAndy Fiddaman case ATA_SF_SETXFER:
1693*5c4a5fe1SAndy Fiddaman {
1694*5c4a5fe1SAndy Fiddaman switch (cfis[12] & 0xf8) {
1695*5c4a5fe1SAndy Fiddaman case ATA_PIO:
1696*5c4a5fe1SAndy Fiddaman case ATA_PIO0:
1697*5c4a5fe1SAndy Fiddaman break;
1698*5c4a5fe1SAndy Fiddaman case ATA_WDMA0:
1699*5c4a5fe1SAndy Fiddaman case ATA_UDMA0:
1700*5c4a5fe1SAndy Fiddaman p->xfermode = (cfis[12] & 0x7);
1701*5c4a5fe1SAndy Fiddaman break;
1702*5c4a5fe1SAndy Fiddaman }
1703*5c4a5fe1SAndy Fiddaman p->tfd = ATA_S_DSC | ATA_S_READY;
1704*5c4a5fe1SAndy Fiddaman break;
1705*5c4a5fe1SAndy Fiddaman }
1706*5c4a5fe1SAndy Fiddaman default:
1707*5c4a5fe1SAndy Fiddaman p->tfd = ATA_S_ERROR | ATA_S_READY;
1708*5c4a5fe1SAndy Fiddaman p->tfd |= (ATA_ERROR_ABORT << 8);
1709*5c4a5fe1SAndy Fiddaman break;
1710*5c4a5fe1SAndy Fiddaman }
1711*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, p->tfd);
1712*5c4a5fe1SAndy Fiddaman break;
1713*5c4a5fe1SAndy Fiddaman }
1714*5c4a5fe1SAndy Fiddaman case ATA_SET_MULTI:
1715*5c4a5fe1SAndy Fiddaman if (cfis[12] != 0 &&
1716*5c4a5fe1SAndy Fiddaman (cfis[12] > 128 || (cfis[12] & (cfis[12] - 1)))) {
1717*5c4a5fe1SAndy Fiddaman p->tfd = ATA_S_ERROR | ATA_S_READY;
1718*5c4a5fe1SAndy Fiddaman p->tfd |= (ATA_ERROR_ABORT << 8);
1719*5c4a5fe1SAndy Fiddaman } else {
1720*5c4a5fe1SAndy Fiddaman p->mult_sectors = cfis[12];
1721*5c4a5fe1SAndy Fiddaman p->tfd = ATA_S_DSC | ATA_S_READY;
1722*5c4a5fe1SAndy Fiddaman }
1723*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, p->tfd);
1724*5c4a5fe1SAndy Fiddaman break;
1725*5c4a5fe1SAndy Fiddaman case ATA_READ:
1726*5c4a5fe1SAndy Fiddaman case ATA_WRITE:
1727*5c4a5fe1SAndy Fiddaman case ATA_READ48:
1728*5c4a5fe1SAndy Fiddaman case ATA_WRITE48:
1729*5c4a5fe1SAndy Fiddaman case ATA_READ_MUL:
1730*5c4a5fe1SAndy Fiddaman case ATA_WRITE_MUL:
1731*5c4a5fe1SAndy Fiddaman case ATA_READ_MUL48:
1732*5c4a5fe1SAndy Fiddaman case ATA_WRITE_MUL48:
1733*5c4a5fe1SAndy Fiddaman case ATA_READ_DMA:
1734*5c4a5fe1SAndy Fiddaman case ATA_WRITE_DMA:
1735*5c4a5fe1SAndy Fiddaman case ATA_READ_DMA48:
1736*5c4a5fe1SAndy Fiddaman case ATA_WRITE_DMA48:
1737*5c4a5fe1SAndy Fiddaman case ATA_READ_FPDMA_QUEUED:
1738*5c4a5fe1SAndy Fiddaman case ATA_WRITE_FPDMA_QUEUED:
1739*5c4a5fe1SAndy Fiddaman ahci_handle_rw(p, slot, cfis, 0);
1740*5c4a5fe1SAndy Fiddaman break;
1741*5c4a5fe1SAndy Fiddaman case ATA_FLUSHCACHE:
1742*5c4a5fe1SAndy Fiddaman case ATA_FLUSHCACHE48:
1743*5c4a5fe1SAndy Fiddaman ahci_handle_flush(p, slot, cfis);
1744*5c4a5fe1SAndy Fiddaman break;
1745*5c4a5fe1SAndy Fiddaman case ATA_DATA_SET_MANAGEMENT:
1746*5c4a5fe1SAndy Fiddaman if (cfis[11] == 0 && cfis[3] == ATA_DSM_TRIM &&
1747*5c4a5fe1SAndy Fiddaman cfis[13] == 0 && cfis[12] == 1) {
1748*5c4a5fe1SAndy Fiddaman ahci_handle_dsm_trim(p, slot, cfis, 0);
1749*5c4a5fe1SAndy Fiddaman break;
1750*5c4a5fe1SAndy Fiddaman }
1751*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis,
1752*5c4a5fe1SAndy Fiddaman (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
1753*5c4a5fe1SAndy Fiddaman break;
1754*5c4a5fe1SAndy Fiddaman case ATA_SEND_FPDMA_QUEUED:
1755*5c4a5fe1SAndy Fiddaman if ((cfis[13] & 0x1f) == ATA_SFPDMA_DSM &&
1756*5c4a5fe1SAndy Fiddaman cfis[17] == 0 && cfis[16] == ATA_DSM_TRIM &&
1757*5c4a5fe1SAndy Fiddaman cfis[11] == 0 && cfis[3] == 1) {
1758*5c4a5fe1SAndy Fiddaman ahci_handle_dsm_trim(p, slot, cfis, 0);
1759*5c4a5fe1SAndy Fiddaman break;
1760*5c4a5fe1SAndy Fiddaman }
1761*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis,
1762*5c4a5fe1SAndy Fiddaman (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
1763*5c4a5fe1SAndy Fiddaman break;
1764*5c4a5fe1SAndy Fiddaman case ATA_READ_LOG_EXT:
1765*5c4a5fe1SAndy Fiddaman case ATA_READ_LOG_DMA_EXT:
1766*5c4a5fe1SAndy Fiddaman ahci_handle_read_log(p, slot, cfis);
1767*5c4a5fe1SAndy Fiddaman break;
1768*5c4a5fe1SAndy Fiddaman case ATA_SECURITY_FREEZE_LOCK:
1769*5c4a5fe1SAndy Fiddaman case ATA_SMART_CMD:
1770*5c4a5fe1SAndy Fiddaman case ATA_NOP:
1771*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis,
1772*5c4a5fe1SAndy Fiddaman (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
1773*5c4a5fe1SAndy Fiddaman break;
1774*5c4a5fe1SAndy Fiddaman case ATA_CHECK_POWER_MODE:
1775*5c4a5fe1SAndy Fiddaman cfis[12] = 0xff; /* always on */
1776*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1777*5c4a5fe1SAndy Fiddaman break;
1778*5c4a5fe1SAndy Fiddaman case ATA_STANDBY_CMD:
1779*5c4a5fe1SAndy Fiddaman case ATA_STANDBY_IMMEDIATE:
1780*5c4a5fe1SAndy Fiddaman case ATA_IDLE_CMD:
1781*5c4a5fe1SAndy Fiddaman case ATA_IDLE_IMMEDIATE:
1782*5c4a5fe1SAndy Fiddaman case ATA_SLEEP:
1783*5c4a5fe1SAndy Fiddaman case ATA_READ_VERIFY:
1784*5c4a5fe1SAndy Fiddaman case ATA_READ_VERIFY48:
1785*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC);
1786*5c4a5fe1SAndy Fiddaman break;
1787*5c4a5fe1SAndy Fiddaman case ATA_ATAPI_IDENTIFY:
1788*5c4a5fe1SAndy Fiddaman handle_atapi_identify(p, slot, cfis);
1789*5c4a5fe1SAndy Fiddaman break;
1790*5c4a5fe1SAndy Fiddaman case ATA_PACKET_CMD:
1791*5c4a5fe1SAndy Fiddaman if (!p->atapi) {
1792*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis,
1793*5c4a5fe1SAndy Fiddaman (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
1794*5c4a5fe1SAndy Fiddaman } else
1795*5c4a5fe1SAndy Fiddaman handle_packet_cmd(p, slot, cfis);
1796*5c4a5fe1SAndy Fiddaman break;
1797*5c4a5fe1SAndy Fiddaman default:
1798*5c4a5fe1SAndy Fiddaman EPRINTLN("Unsupported cmd:%02x", cfis[2]);
1799*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis,
1800*5c4a5fe1SAndy Fiddaman (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR);
1801*5c4a5fe1SAndy Fiddaman break;
1802*5c4a5fe1SAndy Fiddaman }
1803*5c4a5fe1SAndy Fiddaman }
1804*5c4a5fe1SAndy Fiddaman
1805*5c4a5fe1SAndy Fiddaman static void
ahci_handle_slot(struct ahci_port * p,int slot)1806*5c4a5fe1SAndy Fiddaman ahci_handle_slot(struct ahci_port *p, int slot)
1807*5c4a5fe1SAndy Fiddaman {
1808*5c4a5fe1SAndy Fiddaman struct ahci_cmd_hdr *hdr;
1809*5c4a5fe1SAndy Fiddaman #ifdef AHCI_DEBUG
1810*5c4a5fe1SAndy Fiddaman struct ahci_prdt_entry *prdt;
1811*5c4a5fe1SAndy Fiddaman #endif
1812*5c4a5fe1SAndy Fiddaman struct pci_ahci_softc *sc;
1813*5c4a5fe1SAndy Fiddaman uint8_t *cfis;
1814*5c4a5fe1SAndy Fiddaman #ifdef AHCI_DEBUG
1815*5c4a5fe1SAndy Fiddaman int cfl, i;
1816*5c4a5fe1SAndy Fiddaman #endif
1817*5c4a5fe1SAndy Fiddaman
1818*5c4a5fe1SAndy Fiddaman sc = p->pr_sc;
1819*5c4a5fe1SAndy Fiddaman hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
1820*5c4a5fe1SAndy Fiddaman #ifdef AHCI_DEBUG
1821*5c4a5fe1SAndy Fiddaman cfl = (hdr->flags & 0x1f) * 4;
1822*5c4a5fe1SAndy Fiddaman #endif
1823*5c4a5fe1SAndy Fiddaman cfis = paddr_guest2host(ahci_ctx(sc), hdr->ctba,
1824*5c4a5fe1SAndy Fiddaman 0x80 + hdr->prdtl * sizeof(struct ahci_prdt_entry));
1825*5c4a5fe1SAndy Fiddaman #ifdef AHCI_DEBUG
1826*5c4a5fe1SAndy Fiddaman prdt = (struct ahci_prdt_entry *)(cfis + 0x80);
1827*5c4a5fe1SAndy Fiddaman
1828*5c4a5fe1SAndy Fiddaman DPRINTF("cfis:");
1829*5c4a5fe1SAndy Fiddaman for (i = 0; i < cfl; i++) {
1830*5c4a5fe1SAndy Fiddaman if (i % 10 == 0)
1831*5c4a5fe1SAndy Fiddaman DPRINTF("");
1832*5c4a5fe1SAndy Fiddaman DPRINTF("%02x ", cfis[i]);
1833*5c4a5fe1SAndy Fiddaman }
1834*5c4a5fe1SAndy Fiddaman DPRINTF("");
1835*5c4a5fe1SAndy Fiddaman
1836*5c4a5fe1SAndy Fiddaman for (i = 0; i < hdr->prdtl; i++) {
1837*5c4a5fe1SAndy Fiddaman DPRINTF("%d@%08"PRIx64"", prdt->dbc & 0x3fffff, prdt->dba);
1838*5c4a5fe1SAndy Fiddaman prdt++;
1839*5c4a5fe1SAndy Fiddaman }
1840*5c4a5fe1SAndy Fiddaman #endif
1841*5c4a5fe1SAndy Fiddaman
1842*5c4a5fe1SAndy Fiddaman if (cfis[0] != FIS_TYPE_REGH2D) {
1843*5c4a5fe1SAndy Fiddaman EPRINTLN("Not a H2D FIS:%02x", cfis[0]);
1844*5c4a5fe1SAndy Fiddaman return;
1845*5c4a5fe1SAndy Fiddaman }
1846*5c4a5fe1SAndy Fiddaman
1847*5c4a5fe1SAndy Fiddaman if (cfis[1] & 0x80) {
1848*5c4a5fe1SAndy Fiddaman ahci_handle_cmd(p, slot, cfis);
1849*5c4a5fe1SAndy Fiddaman } else {
1850*5c4a5fe1SAndy Fiddaman if (cfis[15] & (1 << 2))
1851*5c4a5fe1SAndy Fiddaman p->reset = 1;
1852*5c4a5fe1SAndy Fiddaman else if (p->reset) {
1853*5c4a5fe1SAndy Fiddaman p->reset = 0;
1854*5c4a5fe1SAndy Fiddaman ahci_port_reset(p);
1855*5c4a5fe1SAndy Fiddaman }
1856*5c4a5fe1SAndy Fiddaman p->ci &= ~(1 << slot);
1857*5c4a5fe1SAndy Fiddaman }
1858*5c4a5fe1SAndy Fiddaman }
1859*5c4a5fe1SAndy Fiddaman
1860*5c4a5fe1SAndy Fiddaman static void
ahci_handle_port(struct ahci_port * p)1861*5c4a5fe1SAndy Fiddaman ahci_handle_port(struct ahci_port *p)
1862*5c4a5fe1SAndy Fiddaman {
1863*5c4a5fe1SAndy Fiddaman
1864*5c4a5fe1SAndy Fiddaman if (!(p->cmd & AHCI_P_CMD_ST))
1865*5c4a5fe1SAndy Fiddaman return;
1866*5c4a5fe1SAndy Fiddaman
1867*5c4a5fe1SAndy Fiddaman /*
1868*5c4a5fe1SAndy Fiddaman * Search for any new commands to issue ignoring those that
1869*5c4a5fe1SAndy Fiddaman * are already in-flight. Stop if device is busy or in error.
1870*5c4a5fe1SAndy Fiddaman */
1871*5c4a5fe1SAndy Fiddaman for (; (p->ci & ~p->pending) != 0; p->ccs = ((p->ccs + 1) & 31)) {
1872*5c4a5fe1SAndy Fiddaman if ((p->tfd & (ATA_S_BUSY | ATA_S_DRQ)) != 0)
1873*5c4a5fe1SAndy Fiddaman break;
1874*5c4a5fe1SAndy Fiddaman if (p->waitforclear)
1875*5c4a5fe1SAndy Fiddaman break;
1876*5c4a5fe1SAndy Fiddaman if ((p->ci & ~p->pending & (1 << p->ccs)) != 0) {
1877*5c4a5fe1SAndy Fiddaman p->cmd &= ~AHCI_P_CMD_CCS_MASK;
1878*5c4a5fe1SAndy Fiddaman p->cmd |= p->ccs << AHCI_P_CMD_CCS_SHIFT;
1879*5c4a5fe1SAndy Fiddaman ahci_handle_slot(p, p->ccs);
1880*5c4a5fe1SAndy Fiddaman }
1881*5c4a5fe1SAndy Fiddaman }
1882*5c4a5fe1SAndy Fiddaman }
1883*5c4a5fe1SAndy Fiddaman
1884*5c4a5fe1SAndy Fiddaman /*
1885*5c4a5fe1SAndy Fiddaman * blockif callback routine - this runs in the context of the blockif
1886*5c4a5fe1SAndy Fiddaman * i/o thread, so the mutex needs to be acquired.
1887*5c4a5fe1SAndy Fiddaman */
1888*5c4a5fe1SAndy Fiddaman static void
ata_ioreq_cb(struct blockif_req * br,int err)1889*5c4a5fe1SAndy Fiddaman ata_ioreq_cb(struct blockif_req *br, int err)
1890*5c4a5fe1SAndy Fiddaman {
1891*5c4a5fe1SAndy Fiddaman struct ahci_cmd_hdr *hdr;
1892*5c4a5fe1SAndy Fiddaman struct ahci_ioreq *aior;
1893*5c4a5fe1SAndy Fiddaman struct ahci_port *p;
1894*5c4a5fe1SAndy Fiddaman struct pci_ahci_softc *sc;
1895*5c4a5fe1SAndy Fiddaman uint32_t tfd;
1896*5c4a5fe1SAndy Fiddaman uint8_t *cfis;
1897*5c4a5fe1SAndy Fiddaman int slot, ncq, dsm;
1898*5c4a5fe1SAndy Fiddaman
1899*5c4a5fe1SAndy Fiddaman DPRINTF("%s %d", __func__, err);
1900*5c4a5fe1SAndy Fiddaman
1901*5c4a5fe1SAndy Fiddaman ncq = dsm = 0;
1902*5c4a5fe1SAndy Fiddaman aior = br->br_param;
1903*5c4a5fe1SAndy Fiddaman p = aior->io_pr;
1904*5c4a5fe1SAndy Fiddaman cfis = aior->cfis;
1905*5c4a5fe1SAndy Fiddaman slot = aior->slot;
1906*5c4a5fe1SAndy Fiddaman sc = p->pr_sc;
1907*5c4a5fe1SAndy Fiddaman hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE);
1908*5c4a5fe1SAndy Fiddaman
1909*5c4a5fe1SAndy Fiddaman if (cfis[2] == ATA_WRITE_FPDMA_QUEUED ||
1910*5c4a5fe1SAndy Fiddaman cfis[2] == ATA_READ_FPDMA_QUEUED ||
1911*5c4a5fe1SAndy Fiddaman cfis[2] == ATA_SEND_FPDMA_QUEUED)
1912*5c4a5fe1SAndy Fiddaman ncq = 1;
1913*5c4a5fe1SAndy Fiddaman if (cfis[2] == ATA_DATA_SET_MANAGEMENT ||
1914*5c4a5fe1SAndy Fiddaman (cfis[2] == ATA_SEND_FPDMA_QUEUED &&
1915*5c4a5fe1SAndy Fiddaman (cfis[13] & 0x1f) == ATA_SFPDMA_DSM))
1916*5c4a5fe1SAndy Fiddaman dsm = 1;
1917*5c4a5fe1SAndy Fiddaman
1918*5c4a5fe1SAndy Fiddaman pthread_mutex_lock(&sc->mtx);
1919*5c4a5fe1SAndy Fiddaman
1920*5c4a5fe1SAndy Fiddaman /*
1921*5c4a5fe1SAndy Fiddaman * Delete the blockif request from the busy list
1922*5c4a5fe1SAndy Fiddaman */
1923*5c4a5fe1SAndy Fiddaman TAILQ_REMOVE(&p->iobhd, aior, io_blist);
1924*5c4a5fe1SAndy Fiddaman
1925*5c4a5fe1SAndy Fiddaman /*
1926*5c4a5fe1SAndy Fiddaman * Move the blockif request back to the free list
1927*5c4a5fe1SAndy Fiddaman */
1928*5c4a5fe1SAndy Fiddaman STAILQ_INSERT_TAIL(&p->iofhd, aior, io_flist);
1929*5c4a5fe1SAndy Fiddaman
1930*5c4a5fe1SAndy Fiddaman if (!err)
1931*5c4a5fe1SAndy Fiddaman hdr->prdbc = aior->done;
1932*5c4a5fe1SAndy Fiddaman
1933*5c4a5fe1SAndy Fiddaman if (!err && aior->more) {
1934*5c4a5fe1SAndy Fiddaman if (dsm)
1935*5c4a5fe1SAndy Fiddaman ahci_handle_dsm_trim(p, slot, cfis, aior->done);
1936*5c4a5fe1SAndy Fiddaman else
1937*5c4a5fe1SAndy Fiddaman ahci_handle_rw(p, slot, cfis, aior->done);
1938*5c4a5fe1SAndy Fiddaman goto out;
1939*5c4a5fe1SAndy Fiddaman }
1940*5c4a5fe1SAndy Fiddaman
1941*5c4a5fe1SAndy Fiddaman if (!err)
1942*5c4a5fe1SAndy Fiddaman tfd = ATA_S_READY | ATA_S_DSC;
1943*5c4a5fe1SAndy Fiddaman else
1944*5c4a5fe1SAndy Fiddaman tfd = (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR;
1945*5c4a5fe1SAndy Fiddaman if (ncq)
1946*5c4a5fe1SAndy Fiddaman ahci_write_fis_sdb(p, slot, cfis, tfd);
1947*5c4a5fe1SAndy Fiddaman else
1948*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, tfd);
1949*5c4a5fe1SAndy Fiddaman
1950*5c4a5fe1SAndy Fiddaman /*
1951*5c4a5fe1SAndy Fiddaman * This command is now complete.
1952*5c4a5fe1SAndy Fiddaman */
1953*5c4a5fe1SAndy Fiddaman p->pending &= ~(1 << slot);
1954*5c4a5fe1SAndy Fiddaman
1955*5c4a5fe1SAndy Fiddaman ahci_check_stopped(p);
1956*5c4a5fe1SAndy Fiddaman ahci_handle_port(p);
1957*5c4a5fe1SAndy Fiddaman out:
1958*5c4a5fe1SAndy Fiddaman pthread_mutex_unlock(&sc->mtx);
1959*5c4a5fe1SAndy Fiddaman DPRINTF("%s exit", __func__);
1960*5c4a5fe1SAndy Fiddaman }
1961*5c4a5fe1SAndy Fiddaman
1962*5c4a5fe1SAndy Fiddaman static void
atapi_ioreq_cb(struct blockif_req * br,int err)1963*5c4a5fe1SAndy Fiddaman atapi_ioreq_cb(struct blockif_req *br, int err)
1964*5c4a5fe1SAndy Fiddaman {
1965*5c4a5fe1SAndy Fiddaman struct ahci_cmd_hdr *hdr;
1966*5c4a5fe1SAndy Fiddaman struct ahci_ioreq *aior;
1967*5c4a5fe1SAndy Fiddaman struct ahci_port *p;
1968*5c4a5fe1SAndy Fiddaman struct pci_ahci_softc *sc;
1969*5c4a5fe1SAndy Fiddaman uint8_t *cfis;
1970*5c4a5fe1SAndy Fiddaman uint32_t tfd;
1971*5c4a5fe1SAndy Fiddaman int slot;
1972*5c4a5fe1SAndy Fiddaman
1973*5c4a5fe1SAndy Fiddaman DPRINTF("%s %d", __func__, err);
1974*5c4a5fe1SAndy Fiddaman
1975*5c4a5fe1SAndy Fiddaman aior = br->br_param;
1976*5c4a5fe1SAndy Fiddaman p = aior->io_pr;
1977*5c4a5fe1SAndy Fiddaman cfis = aior->cfis;
1978*5c4a5fe1SAndy Fiddaman slot = aior->slot;
1979*5c4a5fe1SAndy Fiddaman sc = p->pr_sc;
1980*5c4a5fe1SAndy Fiddaman hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + aior->slot * AHCI_CL_SIZE);
1981*5c4a5fe1SAndy Fiddaman
1982*5c4a5fe1SAndy Fiddaman pthread_mutex_lock(&sc->mtx);
1983*5c4a5fe1SAndy Fiddaman
1984*5c4a5fe1SAndy Fiddaman /*
1985*5c4a5fe1SAndy Fiddaman * Delete the blockif request from the busy list
1986*5c4a5fe1SAndy Fiddaman */
1987*5c4a5fe1SAndy Fiddaman TAILQ_REMOVE(&p->iobhd, aior, io_blist);
1988*5c4a5fe1SAndy Fiddaman
1989*5c4a5fe1SAndy Fiddaman /*
1990*5c4a5fe1SAndy Fiddaman * Move the blockif request back to the free list
1991*5c4a5fe1SAndy Fiddaman */
1992*5c4a5fe1SAndy Fiddaman STAILQ_INSERT_TAIL(&p->iofhd, aior, io_flist);
1993*5c4a5fe1SAndy Fiddaman
1994*5c4a5fe1SAndy Fiddaman if (!err)
1995*5c4a5fe1SAndy Fiddaman hdr->prdbc = aior->done;
1996*5c4a5fe1SAndy Fiddaman
1997*5c4a5fe1SAndy Fiddaman if (!err && aior->more) {
1998*5c4a5fe1SAndy Fiddaman atapi_read(p, slot, cfis, aior->done);
1999*5c4a5fe1SAndy Fiddaman goto out;
2000*5c4a5fe1SAndy Fiddaman }
2001*5c4a5fe1SAndy Fiddaman
2002*5c4a5fe1SAndy Fiddaman if (!err) {
2003*5c4a5fe1SAndy Fiddaman tfd = ATA_S_READY | ATA_S_DSC;
2004*5c4a5fe1SAndy Fiddaman } else {
2005*5c4a5fe1SAndy Fiddaman p->sense_key = ATA_SENSE_ILLEGAL_REQUEST;
2006*5c4a5fe1SAndy Fiddaman p->asc = 0x21;
2007*5c4a5fe1SAndy Fiddaman tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR;
2008*5c4a5fe1SAndy Fiddaman }
2009*5c4a5fe1SAndy Fiddaman cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN;
2010*5c4a5fe1SAndy Fiddaman ahci_write_fis_d2h(p, slot, cfis, tfd);
2011*5c4a5fe1SAndy Fiddaman
2012*5c4a5fe1SAndy Fiddaman /*
2013*5c4a5fe1SAndy Fiddaman * This command is now complete.
2014*5c4a5fe1SAndy Fiddaman */
2015*5c4a5fe1SAndy Fiddaman p->pending &= ~(1 << slot);
2016*5c4a5fe1SAndy Fiddaman
2017*5c4a5fe1SAndy Fiddaman ahci_check_stopped(p);
2018*5c4a5fe1SAndy Fiddaman ahci_handle_port(p);
2019*5c4a5fe1SAndy Fiddaman out:
2020*5c4a5fe1SAndy Fiddaman pthread_mutex_unlock(&sc->mtx);
2021*5c4a5fe1SAndy Fiddaman DPRINTF("%s exit", __func__);
2022*5c4a5fe1SAndy Fiddaman }
2023*5c4a5fe1SAndy Fiddaman
2024*5c4a5fe1SAndy Fiddaman static void
pci_ahci_ioreq_init(struct ahci_port * pr)2025*5c4a5fe1SAndy Fiddaman pci_ahci_ioreq_init(struct ahci_port *pr)
2026*5c4a5fe1SAndy Fiddaman {
2027*5c4a5fe1SAndy Fiddaman struct ahci_ioreq *vr;
2028*5c4a5fe1SAndy Fiddaman int i;
2029*5c4a5fe1SAndy Fiddaman
2030*5c4a5fe1SAndy Fiddaman pr->ioqsz = blockif_queuesz(pr->bctx);
2031*5c4a5fe1SAndy Fiddaman pr->ioreq = calloc(pr->ioqsz, sizeof(struct ahci_ioreq));
2032*5c4a5fe1SAndy Fiddaman STAILQ_INIT(&pr->iofhd);
2033*5c4a5fe1SAndy Fiddaman
2034*5c4a5fe1SAndy Fiddaman /*
2035*5c4a5fe1SAndy Fiddaman * Add all i/o request entries to the free queue
2036*5c4a5fe1SAndy Fiddaman */
2037*5c4a5fe1SAndy Fiddaman for (i = 0; i < pr->ioqsz; i++) {
2038*5c4a5fe1SAndy Fiddaman vr = &pr->ioreq[i];
2039*5c4a5fe1SAndy Fiddaman vr->io_pr = pr;
2040*5c4a5fe1SAndy Fiddaman if (!pr->atapi)
2041*5c4a5fe1SAndy Fiddaman vr->io_req.br_callback = ata_ioreq_cb;
2042*5c4a5fe1SAndy Fiddaman else
2043*5c4a5fe1SAndy Fiddaman vr->io_req.br_callback = atapi_ioreq_cb;
2044*5c4a5fe1SAndy Fiddaman vr->io_req.br_param = vr;
2045*5c4a5fe1SAndy Fiddaman STAILQ_INSERT_TAIL(&pr->iofhd, vr, io_flist);
2046*5c4a5fe1SAndy Fiddaman }
2047*5c4a5fe1SAndy Fiddaman
2048*5c4a5fe1SAndy Fiddaman TAILQ_INIT(&pr->iobhd);
2049*5c4a5fe1SAndy Fiddaman }
2050*5c4a5fe1SAndy Fiddaman
2051*5c4a5fe1SAndy Fiddaman static void
pci_ahci_port_write(struct pci_ahci_softc * sc,uint64_t offset,uint64_t value)2052*5c4a5fe1SAndy Fiddaman pci_ahci_port_write(struct pci_ahci_softc *sc, uint64_t offset, uint64_t value)
2053*5c4a5fe1SAndy Fiddaman {
2054*5c4a5fe1SAndy Fiddaman int port = (offset - AHCI_OFFSET) / AHCI_STEP;
2055*5c4a5fe1SAndy Fiddaman offset = (offset - AHCI_OFFSET) % AHCI_STEP;
2056*5c4a5fe1SAndy Fiddaman struct ahci_port *p = &sc->port[port];
2057*5c4a5fe1SAndy Fiddaman
2058*5c4a5fe1SAndy Fiddaman DPRINTF("pci_ahci_port %d: write offset 0x%"PRIx64" value 0x%"PRIx64"",
2059*5c4a5fe1SAndy Fiddaman port, offset, value);
2060*5c4a5fe1SAndy Fiddaman
2061*5c4a5fe1SAndy Fiddaman switch (offset) {
2062*5c4a5fe1SAndy Fiddaman case AHCI_P_CLB:
2063*5c4a5fe1SAndy Fiddaman p->clb = value;
2064*5c4a5fe1SAndy Fiddaman break;
2065*5c4a5fe1SAndy Fiddaman case AHCI_P_CLBU:
2066*5c4a5fe1SAndy Fiddaman p->clbu = value;
2067*5c4a5fe1SAndy Fiddaman break;
2068*5c4a5fe1SAndy Fiddaman case AHCI_P_FB:
2069*5c4a5fe1SAndy Fiddaman p->fb = value;
2070*5c4a5fe1SAndy Fiddaman break;
2071*5c4a5fe1SAndy Fiddaman case AHCI_P_FBU:
2072*5c4a5fe1SAndy Fiddaman p->fbu = value;
2073*5c4a5fe1SAndy Fiddaman break;
2074*5c4a5fe1SAndy Fiddaman case AHCI_P_IS:
2075*5c4a5fe1SAndy Fiddaman p->is &= ~value;
2076*5c4a5fe1SAndy Fiddaman ahci_port_intr(p);
2077*5c4a5fe1SAndy Fiddaman break;
2078*5c4a5fe1SAndy Fiddaman case AHCI_P_IE:
2079*5c4a5fe1SAndy Fiddaman p->ie = value & 0xFDC000FF;
2080*5c4a5fe1SAndy Fiddaman ahci_port_intr(p);
2081*5c4a5fe1SAndy Fiddaman break;
2082*5c4a5fe1SAndy Fiddaman case AHCI_P_CMD:
2083*5c4a5fe1SAndy Fiddaman {
2084*5c4a5fe1SAndy Fiddaman p->cmd &= ~(AHCI_P_CMD_ST | AHCI_P_CMD_SUD | AHCI_P_CMD_POD |
2085*5c4a5fe1SAndy Fiddaman AHCI_P_CMD_CLO | AHCI_P_CMD_FRE | AHCI_P_CMD_APSTE |
2086*5c4a5fe1SAndy Fiddaman AHCI_P_CMD_ATAPI | AHCI_P_CMD_DLAE | AHCI_P_CMD_ALPE |
2087*5c4a5fe1SAndy Fiddaman AHCI_P_CMD_ASP | AHCI_P_CMD_ICC_MASK);
2088*5c4a5fe1SAndy Fiddaman p->cmd |= (AHCI_P_CMD_ST | AHCI_P_CMD_SUD | AHCI_P_CMD_POD |
2089*5c4a5fe1SAndy Fiddaman AHCI_P_CMD_CLO | AHCI_P_CMD_FRE | AHCI_P_CMD_APSTE |
2090*5c4a5fe1SAndy Fiddaman AHCI_P_CMD_ATAPI | AHCI_P_CMD_DLAE | AHCI_P_CMD_ALPE |
2091*5c4a5fe1SAndy Fiddaman AHCI_P_CMD_ASP | AHCI_P_CMD_ICC_MASK) & value;
2092*5c4a5fe1SAndy Fiddaman
2093*5c4a5fe1SAndy Fiddaman if (!(value & AHCI_P_CMD_ST)) {
2094*5c4a5fe1SAndy Fiddaman ahci_port_stop(p);
2095*5c4a5fe1SAndy Fiddaman } else {
2096*5c4a5fe1SAndy Fiddaman uint64_t clb;
2097*5c4a5fe1SAndy Fiddaman
2098*5c4a5fe1SAndy Fiddaman p->cmd |= AHCI_P_CMD_CR;
2099*5c4a5fe1SAndy Fiddaman clb = (uint64_t)p->clbu << 32 | p->clb;
2100*5c4a5fe1SAndy Fiddaman p->cmd_lst = paddr_guest2host(ahci_ctx(sc), clb,
2101*5c4a5fe1SAndy Fiddaman AHCI_CL_SIZE * AHCI_MAX_SLOTS);
2102*5c4a5fe1SAndy Fiddaman }
2103*5c4a5fe1SAndy Fiddaman
2104*5c4a5fe1SAndy Fiddaman if (value & AHCI_P_CMD_FRE) {
2105*5c4a5fe1SAndy Fiddaman uint64_t fb;
2106*5c4a5fe1SAndy Fiddaman
2107*5c4a5fe1SAndy Fiddaman p->cmd |= AHCI_P_CMD_FR;
2108*5c4a5fe1SAndy Fiddaman fb = (uint64_t)p->fbu << 32 | p->fb;
2109*5c4a5fe1SAndy Fiddaman /* we don't support FBSCP, so rfis size is 256Bytes */
2110*5c4a5fe1SAndy Fiddaman p->rfis = paddr_guest2host(ahci_ctx(sc), fb, 256);
2111*5c4a5fe1SAndy Fiddaman } else {
2112*5c4a5fe1SAndy Fiddaman p->cmd &= ~AHCI_P_CMD_FR;
2113*5c4a5fe1SAndy Fiddaman }
2114*5c4a5fe1SAndy Fiddaman
2115*5c4a5fe1SAndy Fiddaman if (value & AHCI_P_CMD_CLO) {
2116*5c4a5fe1SAndy Fiddaman p->tfd &= ~(ATA_S_BUSY | ATA_S_DRQ);
2117*5c4a5fe1SAndy Fiddaman p->cmd &= ~AHCI_P_CMD_CLO;
2118*5c4a5fe1SAndy Fiddaman }
2119*5c4a5fe1SAndy Fiddaman
2120*5c4a5fe1SAndy Fiddaman if (value & AHCI_P_CMD_ICC_MASK) {
2121*5c4a5fe1SAndy Fiddaman p->cmd &= ~AHCI_P_CMD_ICC_MASK;
2122*5c4a5fe1SAndy Fiddaman }
2123*5c4a5fe1SAndy Fiddaman
2124*5c4a5fe1SAndy Fiddaman ahci_handle_port(p);
2125*5c4a5fe1SAndy Fiddaman break;
2126*5c4a5fe1SAndy Fiddaman }
2127*5c4a5fe1SAndy Fiddaman case AHCI_P_TFD:
2128*5c4a5fe1SAndy Fiddaman case AHCI_P_SIG:
2129*5c4a5fe1SAndy Fiddaman case AHCI_P_SSTS:
2130*5c4a5fe1SAndy Fiddaman EPRINTLN("pci_ahci_port: read only registers 0x%"PRIx64"", offset);
2131*5c4a5fe1SAndy Fiddaman break;
2132*5c4a5fe1SAndy Fiddaman case AHCI_P_SCTL:
2133*5c4a5fe1SAndy Fiddaman p->sctl = value;
2134*5c4a5fe1SAndy Fiddaman if (!(p->cmd & AHCI_P_CMD_ST)) {
2135*5c4a5fe1SAndy Fiddaman if (value & ATA_SC_DET_RESET)
2136*5c4a5fe1SAndy Fiddaman ahci_port_reset(p);
2137*5c4a5fe1SAndy Fiddaman }
2138*5c4a5fe1SAndy Fiddaman break;
2139*5c4a5fe1SAndy Fiddaman case AHCI_P_SERR:
2140*5c4a5fe1SAndy Fiddaman p->serr &= ~value;
2141*5c4a5fe1SAndy Fiddaman break;
2142*5c4a5fe1SAndy Fiddaman case AHCI_P_SACT:
2143*5c4a5fe1SAndy Fiddaman p->sact |= value;
2144*5c4a5fe1SAndy Fiddaman break;
2145*5c4a5fe1SAndy Fiddaman case AHCI_P_CI:
2146*5c4a5fe1SAndy Fiddaman p->ci |= value;
2147*5c4a5fe1SAndy Fiddaman ahci_handle_port(p);
2148*5c4a5fe1SAndy Fiddaman break;
2149*5c4a5fe1SAndy Fiddaman case AHCI_P_SNTF:
2150*5c4a5fe1SAndy Fiddaman case AHCI_P_FBS:
2151*5c4a5fe1SAndy Fiddaman default:
2152*5c4a5fe1SAndy Fiddaman break;
2153*5c4a5fe1SAndy Fiddaman }
2154*5c4a5fe1SAndy Fiddaman }
2155*5c4a5fe1SAndy Fiddaman
2156*5c4a5fe1SAndy Fiddaman static void
pci_ahci_host_write(struct pci_ahci_softc * sc,uint64_t offset,uint64_t value)2157*5c4a5fe1SAndy Fiddaman pci_ahci_host_write(struct pci_ahci_softc *sc, uint64_t offset, uint64_t value)
2158*5c4a5fe1SAndy Fiddaman {
2159*5c4a5fe1SAndy Fiddaman DPRINTF("pci_ahci_host: write offset 0x%"PRIx64" value 0x%"PRIx64"",
2160*5c4a5fe1SAndy Fiddaman offset, value);
2161*5c4a5fe1SAndy Fiddaman
2162*5c4a5fe1SAndy Fiddaman switch (offset) {
2163*5c4a5fe1SAndy Fiddaman case AHCI_CAP:
2164*5c4a5fe1SAndy Fiddaman case AHCI_PI:
2165*5c4a5fe1SAndy Fiddaman case AHCI_VS:
2166*5c4a5fe1SAndy Fiddaman case AHCI_CAP2:
2167*5c4a5fe1SAndy Fiddaman DPRINTF("pci_ahci_host: read only registers 0x%"PRIx64"", offset);
2168*5c4a5fe1SAndy Fiddaman break;
2169*5c4a5fe1SAndy Fiddaman case AHCI_GHC:
2170*5c4a5fe1SAndy Fiddaman if (value & AHCI_GHC_HR) {
2171*5c4a5fe1SAndy Fiddaman ahci_reset(sc);
2172*5c4a5fe1SAndy Fiddaman break;
2173*5c4a5fe1SAndy Fiddaman }
2174*5c4a5fe1SAndy Fiddaman if (value & AHCI_GHC_IE)
2175*5c4a5fe1SAndy Fiddaman sc->ghc |= AHCI_GHC_IE;
2176*5c4a5fe1SAndy Fiddaman else
2177*5c4a5fe1SAndy Fiddaman sc->ghc &= ~AHCI_GHC_IE;
2178*5c4a5fe1SAndy Fiddaman ahci_generate_intr(sc, 0xffffffff);
2179*5c4a5fe1SAndy Fiddaman break;
2180*5c4a5fe1SAndy Fiddaman case AHCI_IS:
2181*5c4a5fe1SAndy Fiddaman sc->is &= ~value;
2182*5c4a5fe1SAndy Fiddaman ahci_generate_intr(sc, value);
2183*5c4a5fe1SAndy Fiddaman break;
2184*5c4a5fe1SAndy Fiddaman default:
2185*5c4a5fe1SAndy Fiddaman break;
2186*5c4a5fe1SAndy Fiddaman }
2187*5c4a5fe1SAndy Fiddaman }
2188*5c4a5fe1SAndy Fiddaman
2189*5c4a5fe1SAndy Fiddaman static void
pci_ahci_write(struct pci_devinst * pi,int baridx,uint64_t offset,int size,uint64_t value)2190*5c4a5fe1SAndy Fiddaman pci_ahci_write(struct pci_devinst *pi, int baridx, uint64_t offset, int size,
2191*5c4a5fe1SAndy Fiddaman uint64_t value)
2192*5c4a5fe1SAndy Fiddaman {
2193*5c4a5fe1SAndy Fiddaman struct pci_ahci_softc *sc = pi->pi_arg;
2194*5c4a5fe1SAndy Fiddaman
2195*5c4a5fe1SAndy Fiddaman assert(baridx == 5);
2196*5c4a5fe1SAndy Fiddaman assert((offset % 4) == 0 && size == 4);
2197*5c4a5fe1SAndy Fiddaman
2198*5c4a5fe1SAndy Fiddaman pthread_mutex_lock(&sc->mtx);
2199*5c4a5fe1SAndy Fiddaman
2200*5c4a5fe1SAndy Fiddaman if (offset < AHCI_OFFSET)
2201*5c4a5fe1SAndy Fiddaman pci_ahci_host_write(sc, offset, value);
2202*5c4a5fe1SAndy Fiddaman else if (offset < (uint64_t)AHCI_OFFSET + sc->ports * AHCI_STEP)
2203*5c4a5fe1SAndy Fiddaman pci_ahci_port_write(sc, offset, value);
2204*5c4a5fe1SAndy Fiddaman else
2205*5c4a5fe1SAndy Fiddaman EPRINTLN("pci_ahci: unknown i/o write offset 0x%"PRIx64"", offset);
2206*5c4a5fe1SAndy Fiddaman
2207*5c4a5fe1SAndy Fiddaman pthread_mutex_unlock(&sc->mtx);
2208*5c4a5fe1SAndy Fiddaman }
2209*5c4a5fe1SAndy Fiddaman
2210*5c4a5fe1SAndy Fiddaman static uint64_t
pci_ahci_host_read(struct pci_ahci_softc * sc,uint64_t offset)2211*5c4a5fe1SAndy Fiddaman pci_ahci_host_read(struct pci_ahci_softc *sc, uint64_t offset)
2212*5c4a5fe1SAndy Fiddaman {
2213*5c4a5fe1SAndy Fiddaman uint32_t value;
2214*5c4a5fe1SAndy Fiddaman
2215*5c4a5fe1SAndy Fiddaman switch (offset) {
2216*5c4a5fe1SAndy Fiddaman case AHCI_CAP:
2217*5c4a5fe1SAndy Fiddaman case AHCI_GHC:
2218*5c4a5fe1SAndy Fiddaman case AHCI_IS:
2219*5c4a5fe1SAndy Fiddaman case AHCI_PI:
2220*5c4a5fe1SAndy Fiddaman case AHCI_VS:
2221*5c4a5fe1SAndy Fiddaman case AHCI_CCCC:
2222*5c4a5fe1SAndy Fiddaman case AHCI_CCCP:
2223*5c4a5fe1SAndy Fiddaman case AHCI_EM_LOC:
2224*5c4a5fe1SAndy Fiddaman case AHCI_EM_CTL:
2225*5c4a5fe1SAndy Fiddaman case AHCI_CAP2:
2226*5c4a5fe1SAndy Fiddaman {
2227*5c4a5fe1SAndy Fiddaman uint32_t *p = &sc->cap;
2228*5c4a5fe1SAndy Fiddaman p += (offset - AHCI_CAP) / sizeof(uint32_t);
2229*5c4a5fe1SAndy Fiddaman value = *p;
2230*5c4a5fe1SAndy Fiddaman break;
2231*5c4a5fe1SAndy Fiddaman }
2232*5c4a5fe1SAndy Fiddaman default:
2233*5c4a5fe1SAndy Fiddaman value = 0;
2234*5c4a5fe1SAndy Fiddaman break;
2235*5c4a5fe1SAndy Fiddaman }
2236*5c4a5fe1SAndy Fiddaman DPRINTF("pci_ahci_host: read offset 0x%"PRIx64" value 0x%x",
2237*5c4a5fe1SAndy Fiddaman offset, value);
2238*5c4a5fe1SAndy Fiddaman
2239*5c4a5fe1SAndy Fiddaman return (value);
2240*5c4a5fe1SAndy Fiddaman }
2241*5c4a5fe1SAndy Fiddaman
2242*5c4a5fe1SAndy Fiddaman static uint64_t
pci_ahci_port_read(struct pci_ahci_softc * sc,uint64_t offset)2243*5c4a5fe1SAndy Fiddaman pci_ahci_port_read(struct pci_ahci_softc *sc, uint64_t offset)
2244*5c4a5fe1SAndy Fiddaman {
2245*5c4a5fe1SAndy Fiddaman uint32_t value;
2246*5c4a5fe1SAndy Fiddaman int port = (offset - AHCI_OFFSET) / AHCI_STEP;
2247*5c4a5fe1SAndy Fiddaman offset = (offset - AHCI_OFFSET) % AHCI_STEP;
2248*5c4a5fe1SAndy Fiddaman
2249*5c4a5fe1SAndy Fiddaman switch (offset) {
2250*5c4a5fe1SAndy Fiddaman case AHCI_P_CLB:
2251*5c4a5fe1SAndy Fiddaman case AHCI_P_CLBU:
2252*5c4a5fe1SAndy Fiddaman case AHCI_P_FB:
2253*5c4a5fe1SAndy Fiddaman case AHCI_P_FBU:
2254*5c4a5fe1SAndy Fiddaman case AHCI_P_IS:
2255*5c4a5fe1SAndy Fiddaman case AHCI_P_IE:
2256*5c4a5fe1SAndy Fiddaman case AHCI_P_CMD:
2257*5c4a5fe1SAndy Fiddaman case AHCI_P_TFD:
2258*5c4a5fe1SAndy Fiddaman case AHCI_P_SIG:
2259*5c4a5fe1SAndy Fiddaman case AHCI_P_SSTS:
2260*5c4a5fe1SAndy Fiddaman case AHCI_P_SCTL:
2261*5c4a5fe1SAndy Fiddaman case AHCI_P_SERR:
2262*5c4a5fe1SAndy Fiddaman case AHCI_P_SACT:
2263*5c4a5fe1SAndy Fiddaman case AHCI_P_CI:
2264*5c4a5fe1SAndy Fiddaman case AHCI_P_SNTF:
2265*5c4a5fe1SAndy Fiddaman case AHCI_P_FBS:
2266*5c4a5fe1SAndy Fiddaman {
2267*5c4a5fe1SAndy Fiddaman uint32_t *p= &sc->port[port].clb;
2268*5c4a5fe1SAndy Fiddaman p += (offset - AHCI_P_CLB) / sizeof(uint32_t);
2269*5c4a5fe1SAndy Fiddaman value = *p;
2270*5c4a5fe1SAndy Fiddaman break;
2271*5c4a5fe1SAndy Fiddaman }
2272*5c4a5fe1SAndy Fiddaman default:
2273*5c4a5fe1SAndy Fiddaman value = 0;
2274*5c4a5fe1SAndy Fiddaman break;
2275*5c4a5fe1SAndy Fiddaman }
2276*5c4a5fe1SAndy Fiddaman
2277*5c4a5fe1SAndy Fiddaman DPRINTF("pci_ahci_port %d: read offset 0x%"PRIx64" value 0x%x",
2278*5c4a5fe1SAndy Fiddaman port, offset, value);
2279*5c4a5fe1SAndy Fiddaman
2280*5c4a5fe1SAndy Fiddaman return value;
2281*5c4a5fe1SAndy Fiddaman }
2282*5c4a5fe1SAndy Fiddaman
2283*5c4a5fe1SAndy Fiddaman static uint64_t
pci_ahci_read(struct pci_devinst * pi,int baridx,uint64_t regoff,int size)2284*5c4a5fe1SAndy Fiddaman pci_ahci_read(struct pci_devinst *pi, int baridx, uint64_t regoff, int size)
2285*5c4a5fe1SAndy Fiddaman {
2286*5c4a5fe1SAndy Fiddaman struct pci_ahci_softc *sc = pi->pi_arg;
2287*5c4a5fe1SAndy Fiddaman uint64_t offset;
2288*5c4a5fe1SAndy Fiddaman uint32_t value;
2289*5c4a5fe1SAndy Fiddaman
2290*5c4a5fe1SAndy Fiddaman assert(baridx == 5);
2291*5c4a5fe1SAndy Fiddaman assert(size == 1 || size == 2 || size == 4);
2292*5c4a5fe1SAndy Fiddaman assert((regoff & (size - 1)) == 0);
2293*5c4a5fe1SAndy Fiddaman
2294*5c4a5fe1SAndy Fiddaman pthread_mutex_lock(&sc->mtx);
2295*5c4a5fe1SAndy Fiddaman
2296*5c4a5fe1SAndy Fiddaman offset = regoff & ~0x3; /* round down to a multiple of 4 bytes */
2297*5c4a5fe1SAndy Fiddaman if (offset < AHCI_OFFSET)
2298*5c4a5fe1SAndy Fiddaman value = pci_ahci_host_read(sc, offset);
2299*5c4a5fe1SAndy Fiddaman else if (offset < (uint64_t)AHCI_OFFSET + sc->ports * AHCI_STEP)
2300*5c4a5fe1SAndy Fiddaman value = pci_ahci_port_read(sc, offset);
2301*5c4a5fe1SAndy Fiddaman else {
2302*5c4a5fe1SAndy Fiddaman value = 0;
2303*5c4a5fe1SAndy Fiddaman EPRINTLN("pci_ahci: unknown i/o read offset 0x%"PRIx64"",
2304*5c4a5fe1SAndy Fiddaman regoff);
2305*5c4a5fe1SAndy Fiddaman }
2306*5c4a5fe1SAndy Fiddaman value >>= 8 * (regoff & 0x3);
2307*5c4a5fe1SAndy Fiddaman
2308*5c4a5fe1SAndy Fiddaman pthread_mutex_unlock(&sc->mtx);
2309*5c4a5fe1SAndy Fiddaman
2310*5c4a5fe1SAndy Fiddaman return (value);
2311*5c4a5fe1SAndy Fiddaman }
2312*5c4a5fe1SAndy Fiddaman
2313*5c4a5fe1SAndy Fiddaman /*
2314*5c4a5fe1SAndy Fiddaman * Each AHCI controller has a "port" node which contains nodes for
2315*5c4a5fe1SAndy Fiddaman * each port named after the decimal number of the port (no leading
2316*5c4a5fe1SAndy Fiddaman * zeroes). Port nodes contain a "type" ("hd" or "cd"), as well as
2317*5c4a5fe1SAndy Fiddaman * options for blockif. For example:
2318*5c4a5fe1SAndy Fiddaman *
2319*5c4a5fe1SAndy Fiddaman * pci.0.1.0
2320*5c4a5fe1SAndy Fiddaman * .device="ahci"
2321*5c4a5fe1SAndy Fiddaman * .port
2322*5c4a5fe1SAndy Fiddaman * .0
2323*5c4a5fe1SAndy Fiddaman * .type="hd"
2324*5c4a5fe1SAndy Fiddaman * .path="/path/to/image"
2325*5c4a5fe1SAndy Fiddaman */
2326*5c4a5fe1SAndy Fiddaman static int
pci_ahci_legacy_config_port(nvlist_t * nvl,int port,const char * type,const char * opts)2327*5c4a5fe1SAndy Fiddaman pci_ahci_legacy_config_port(nvlist_t *nvl, int port, const char *type,
2328*5c4a5fe1SAndy Fiddaman const char *opts)
2329*5c4a5fe1SAndy Fiddaman {
2330*5c4a5fe1SAndy Fiddaman char node_name[sizeof("XX")];
2331*5c4a5fe1SAndy Fiddaman nvlist_t *port_nvl;
2332*5c4a5fe1SAndy Fiddaman
2333*5c4a5fe1SAndy Fiddaman snprintf(node_name, sizeof(node_name), "%d", port);
2334*5c4a5fe1SAndy Fiddaman port_nvl = create_relative_config_node(nvl, node_name);
2335*5c4a5fe1SAndy Fiddaman set_config_value_node(port_nvl, "type", type);
2336*5c4a5fe1SAndy Fiddaman return (blockif_legacy_config(port_nvl, opts));
2337*5c4a5fe1SAndy Fiddaman }
2338*5c4a5fe1SAndy Fiddaman
2339*5c4a5fe1SAndy Fiddaman static int
pci_ahci_legacy_config(nvlist_t * nvl,const char * opts)2340*5c4a5fe1SAndy Fiddaman pci_ahci_legacy_config(nvlist_t *nvl, const char *opts)
2341*5c4a5fe1SAndy Fiddaman {
2342*5c4a5fe1SAndy Fiddaman nvlist_t *ports_nvl;
2343*5c4a5fe1SAndy Fiddaman const char *type;
2344*5c4a5fe1SAndy Fiddaman char *next, *next2, *str, *tofree;
2345*5c4a5fe1SAndy Fiddaman int p, ret;
2346*5c4a5fe1SAndy Fiddaman
2347*5c4a5fe1SAndy Fiddaman if (opts == NULL)
2348*5c4a5fe1SAndy Fiddaman return (0);
2349*5c4a5fe1SAndy Fiddaman
2350*5c4a5fe1SAndy Fiddaman ports_nvl = create_relative_config_node(nvl, "port");
2351*5c4a5fe1SAndy Fiddaman ret = 1;
2352*5c4a5fe1SAndy Fiddaman tofree = str = strdup(opts);
2353*5c4a5fe1SAndy Fiddaman for (p = 0; p < MAX_PORTS && str != NULL; p++, str = next) {
2354*5c4a5fe1SAndy Fiddaman /* Identify and cut off type of present port. */
2355*5c4a5fe1SAndy Fiddaman if (strncmp(str, "hd:", 3) == 0) {
2356*5c4a5fe1SAndy Fiddaman type = "hd";
2357*5c4a5fe1SAndy Fiddaman str += 3;
2358*5c4a5fe1SAndy Fiddaman } else if (strncmp(str, "cd:", 3) == 0) {
2359*5c4a5fe1SAndy Fiddaman type = "cd";
2360*5c4a5fe1SAndy Fiddaman str += 3;
2361*5c4a5fe1SAndy Fiddaman } else
2362*5c4a5fe1SAndy Fiddaman type = NULL;
2363*5c4a5fe1SAndy Fiddaman
2364*5c4a5fe1SAndy Fiddaman /* Find and cut off the next port options. */
2365*5c4a5fe1SAndy Fiddaman next = strstr(str, ",hd:");
2366*5c4a5fe1SAndy Fiddaman next2 = strstr(str, ",cd:");
2367*5c4a5fe1SAndy Fiddaman if (next == NULL || (next2 != NULL && next2 < next))
2368*5c4a5fe1SAndy Fiddaman next = next2;
2369*5c4a5fe1SAndy Fiddaman if (next != NULL) {
2370*5c4a5fe1SAndy Fiddaman next[0] = 0;
2371*5c4a5fe1SAndy Fiddaman next++;
2372*5c4a5fe1SAndy Fiddaman }
2373*5c4a5fe1SAndy Fiddaman
2374*5c4a5fe1SAndy Fiddaman if (str[0] == 0)
2375*5c4a5fe1SAndy Fiddaman continue;
2376*5c4a5fe1SAndy Fiddaman
2377*5c4a5fe1SAndy Fiddaman if (type == NULL) {
2378*5c4a5fe1SAndy Fiddaman EPRINTLN("Missing or invalid type for port %d: \"%s\"",
2379*5c4a5fe1SAndy Fiddaman p, str);
2380*5c4a5fe1SAndy Fiddaman goto out;
2381*5c4a5fe1SAndy Fiddaman }
2382*5c4a5fe1SAndy Fiddaman
2383*5c4a5fe1SAndy Fiddaman if (pci_ahci_legacy_config_port(ports_nvl, p, type, str) != 0)
2384*5c4a5fe1SAndy Fiddaman goto out;
2385*5c4a5fe1SAndy Fiddaman }
2386*5c4a5fe1SAndy Fiddaman ret = 0;
2387*5c4a5fe1SAndy Fiddaman out:
2388*5c4a5fe1SAndy Fiddaman free(tofree);
2389*5c4a5fe1SAndy Fiddaman return (ret);
2390*5c4a5fe1SAndy Fiddaman }
2391*5c4a5fe1SAndy Fiddaman
2392*5c4a5fe1SAndy Fiddaman static int
pci_ahci_cd_legacy_config(nvlist_t * nvl,const char * opts)2393*5c4a5fe1SAndy Fiddaman pci_ahci_cd_legacy_config(nvlist_t *nvl, const char *opts)
2394*5c4a5fe1SAndy Fiddaman {
2395*5c4a5fe1SAndy Fiddaman nvlist_t *ports_nvl;
2396*5c4a5fe1SAndy Fiddaman
2397*5c4a5fe1SAndy Fiddaman ports_nvl = create_relative_config_node(nvl, "port");
2398*5c4a5fe1SAndy Fiddaman return (pci_ahci_legacy_config_port(ports_nvl, 0, "cd", opts));
2399*5c4a5fe1SAndy Fiddaman }
2400*5c4a5fe1SAndy Fiddaman
2401*5c4a5fe1SAndy Fiddaman static int
pci_ahci_hd_legacy_config(nvlist_t * nvl,const char * opts)2402*5c4a5fe1SAndy Fiddaman pci_ahci_hd_legacy_config(nvlist_t *nvl, const char *opts)
2403*5c4a5fe1SAndy Fiddaman {
2404*5c4a5fe1SAndy Fiddaman nvlist_t *ports_nvl;
2405*5c4a5fe1SAndy Fiddaman
2406*5c4a5fe1SAndy Fiddaman ports_nvl = create_relative_config_node(nvl, "port");
2407*5c4a5fe1SAndy Fiddaman return (pci_ahci_legacy_config_port(ports_nvl, 0, "hd", opts));
2408*5c4a5fe1SAndy Fiddaman }
2409*5c4a5fe1SAndy Fiddaman
2410*5c4a5fe1SAndy Fiddaman static int
pci_ahci_init(struct pci_devinst * pi,nvlist_t * nvl)2411*5c4a5fe1SAndy Fiddaman pci_ahci_init(struct pci_devinst *pi, nvlist_t *nvl)
2412*5c4a5fe1SAndy Fiddaman {
2413*5c4a5fe1SAndy Fiddaman char bident[sizeof("XXX:XXX:XXX")];
2414*5c4a5fe1SAndy Fiddaman char node_name[sizeof("XX")];
2415*5c4a5fe1SAndy Fiddaman struct blockif_ctxt *bctxt;
2416*5c4a5fe1SAndy Fiddaman struct pci_ahci_softc *sc;
2417*5c4a5fe1SAndy Fiddaman int atapi, ret, slots, p;
2418*5c4a5fe1SAndy Fiddaman MD5_CTX mdctx;
2419*5c4a5fe1SAndy Fiddaman u_char digest[16];
2420*5c4a5fe1SAndy Fiddaman const char *path, *type, *value;
2421*5c4a5fe1SAndy Fiddaman nvlist_t *ports_nvl, *port_nvl;
2422*5c4a5fe1SAndy Fiddaman
2423*5c4a5fe1SAndy Fiddaman ret = 0;
2424*5c4a5fe1SAndy Fiddaman
2425*5c4a5fe1SAndy Fiddaman #ifdef AHCI_DEBUG
2426*5c4a5fe1SAndy Fiddaman dbg = fopen("/tmp/log", "w+");
2427*5c4a5fe1SAndy Fiddaman #endif
2428*5c4a5fe1SAndy Fiddaman
2429*5c4a5fe1SAndy Fiddaman sc = calloc(1, sizeof(struct pci_ahci_softc));
2430*5c4a5fe1SAndy Fiddaman pi->pi_arg = sc;
2431*5c4a5fe1SAndy Fiddaman sc->asc_pi = pi;
2432*5c4a5fe1SAndy Fiddaman pthread_mutex_init(&sc->mtx, NULL);
2433*5c4a5fe1SAndy Fiddaman sc->ports = 0;
2434*5c4a5fe1SAndy Fiddaman sc->pi = 0;
2435*5c4a5fe1SAndy Fiddaman slots = 32;
2436*5c4a5fe1SAndy Fiddaman
2437*5c4a5fe1SAndy Fiddaman ports_nvl = find_relative_config_node(nvl, "port");
2438*5c4a5fe1SAndy Fiddaman for (p = 0; ports_nvl != NULL && p < MAX_PORTS; p++) {
2439*5c4a5fe1SAndy Fiddaman struct ata_params *ata_ident = &sc->port[p].ata_ident;
2440*5c4a5fe1SAndy Fiddaman char ident[AHCI_PORT_IDENT];
2441*5c4a5fe1SAndy Fiddaman
2442*5c4a5fe1SAndy Fiddaman snprintf(node_name, sizeof(node_name), "%d", p);
2443*5c4a5fe1SAndy Fiddaman port_nvl = find_relative_config_node(ports_nvl, node_name);
2444*5c4a5fe1SAndy Fiddaman if (port_nvl == NULL)
2445*5c4a5fe1SAndy Fiddaman continue;
2446*5c4a5fe1SAndy Fiddaman
2447*5c4a5fe1SAndy Fiddaman type = get_config_value_node(port_nvl, "type");
2448*5c4a5fe1SAndy Fiddaman if (type == NULL)
2449*5c4a5fe1SAndy Fiddaman continue;
2450*5c4a5fe1SAndy Fiddaman
2451*5c4a5fe1SAndy Fiddaman if (strcmp(type, "hd") == 0)
2452*5c4a5fe1SAndy Fiddaman atapi = 0;
2453*5c4a5fe1SAndy Fiddaman else
2454*5c4a5fe1SAndy Fiddaman atapi = 1;
2455*5c4a5fe1SAndy Fiddaman
2456*5c4a5fe1SAndy Fiddaman /*
2457*5c4a5fe1SAndy Fiddaman * Attempt to open the backing image. Use the PCI slot/func
2458*5c4a5fe1SAndy Fiddaman * and the port number for the identifier string.
2459*5c4a5fe1SAndy Fiddaman */
2460*5c4a5fe1SAndy Fiddaman snprintf(bident, sizeof(bident), "%u:%u:%u", pi->pi_slot,
2461*5c4a5fe1SAndy Fiddaman pi->pi_func, p);
2462*5c4a5fe1SAndy Fiddaman
2463*5c4a5fe1SAndy Fiddaman bctxt = blockif_open(port_nvl, bident);
2464*5c4a5fe1SAndy Fiddaman if (bctxt == NULL) {
2465*5c4a5fe1SAndy Fiddaman sc->ports = p;
2466*5c4a5fe1SAndy Fiddaman ret = 1;
2467*5c4a5fe1SAndy Fiddaman goto open_fail;
2468*5c4a5fe1SAndy Fiddaman }
2469*5c4a5fe1SAndy Fiddaman
2470*5c4a5fe1SAndy Fiddaman ret = blockif_add_boot_device(pi, bctxt);
2471*5c4a5fe1SAndy Fiddaman if (ret) {
2472*5c4a5fe1SAndy Fiddaman sc->ports = p;
2473*5c4a5fe1SAndy Fiddaman goto open_fail;
2474*5c4a5fe1SAndy Fiddaman }
2475*5c4a5fe1SAndy Fiddaman
2476*5c4a5fe1SAndy Fiddaman sc->port[p].bctx = bctxt;
2477*5c4a5fe1SAndy Fiddaman sc->port[p].pr_sc = sc;
2478*5c4a5fe1SAndy Fiddaman sc->port[p].port = p;
2479*5c4a5fe1SAndy Fiddaman sc->port[p].atapi = atapi;
2480*5c4a5fe1SAndy Fiddaman
2481*5c4a5fe1SAndy Fiddaman /*
2482*5c4a5fe1SAndy Fiddaman * Create an identifier for the backing file.
2483*5c4a5fe1SAndy Fiddaman * Use parts of the md5 sum of the filename
2484*5c4a5fe1SAndy Fiddaman */
2485*5c4a5fe1SAndy Fiddaman path = get_config_value_node(port_nvl, "path");
2486*5c4a5fe1SAndy Fiddaman MD5Init(&mdctx);
2487*5c4a5fe1SAndy Fiddaman MD5Update(&mdctx, path, strlen(path));
2488*5c4a5fe1SAndy Fiddaman MD5Final(digest, &mdctx);
2489*5c4a5fe1SAndy Fiddaman snprintf(ident, AHCI_PORT_IDENT,
2490*5c4a5fe1SAndy Fiddaman "BHYVE-%02X%02X-%02X%02X-%02X%02X",
2491*5c4a5fe1SAndy Fiddaman digest[0], digest[1], digest[2], digest[3], digest[4],
2492*5c4a5fe1SAndy Fiddaman digest[5]);
2493*5c4a5fe1SAndy Fiddaman
2494*5c4a5fe1SAndy Fiddaman memset(ata_ident, 0, sizeof(struct ata_params));
2495*5c4a5fe1SAndy Fiddaman ata_string((uint8_t*)&ata_ident->serial, ident, 20);
2496*5c4a5fe1SAndy Fiddaman ata_string((uint8_t*)&ata_ident->revision, "001", 8);
2497*5c4a5fe1SAndy Fiddaman if (atapi)
2498*5c4a5fe1SAndy Fiddaman ata_string((uint8_t*)&ata_ident->model, "BHYVE SATA DVD ROM", 40);
2499*5c4a5fe1SAndy Fiddaman else
2500*5c4a5fe1SAndy Fiddaman ata_string((uint8_t*)&ata_ident->model, "BHYVE SATA DISK", 40);
2501*5c4a5fe1SAndy Fiddaman value = get_config_value_node(port_nvl, "nmrr");
2502*5c4a5fe1SAndy Fiddaman if (value != NULL)
2503*5c4a5fe1SAndy Fiddaman ata_ident->media_rotation_rate = atoi(value);
2504*5c4a5fe1SAndy Fiddaman value = get_config_value_node(port_nvl, "ser");
2505*5c4a5fe1SAndy Fiddaman if (value != NULL)
2506*5c4a5fe1SAndy Fiddaman ata_string((uint8_t*)(&ata_ident->serial), value, 20);
2507*5c4a5fe1SAndy Fiddaman value = get_config_value_node(port_nvl, "rev");
2508*5c4a5fe1SAndy Fiddaman if (value != NULL)
2509*5c4a5fe1SAndy Fiddaman ata_string((uint8_t*)(&ata_ident->revision), value, 8);
2510*5c4a5fe1SAndy Fiddaman value = get_config_value_node(port_nvl, "model");
2511*5c4a5fe1SAndy Fiddaman if (value != NULL)
2512*5c4a5fe1SAndy Fiddaman ata_string((uint8_t*)(&ata_ident->model), value, 40);
2513*5c4a5fe1SAndy Fiddaman ata_identify_init(&sc->port[p], atapi);
2514*5c4a5fe1SAndy Fiddaman
2515*5c4a5fe1SAndy Fiddaman #ifndef __FreeBSD__
2516*5c4a5fe1SAndy Fiddaman /*
2517*5c4a5fe1SAndy Fiddaman * Attempt to enable the write cache for this device, as the
2518*5c4a5fe1SAndy Fiddaman * guest will issue FLUSH commands when it requires durability.
2519*5c4a5fe1SAndy Fiddaman *
2520*5c4a5fe1SAndy Fiddaman * Failure here is fine, since an always-sync device will not
2521*5c4a5fe1SAndy Fiddaman * have an impact on correctness.
2522*5c4a5fe1SAndy Fiddaman */
2523*5c4a5fe1SAndy Fiddaman (void) blockif_set_wce(bctxt, 1);
2524*5c4a5fe1SAndy Fiddaman #endif
2525*5c4a5fe1SAndy Fiddaman
2526*5c4a5fe1SAndy Fiddaman /*
2527*5c4a5fe1SAndy Fiddaman * Allocate blockif request structures and add them
2528*5c4a5fe1SAndy Fiddaman * to the free list
2529*5c4a5fe1SAndy Fiddaman */
2530*5c4a5fe1SAndy Fiddaman pci_ahci_ioreq_init(&sc->port[p]);
2531*5c4a5fe1SAndy Fiddaman
2532*5c4a5fe1SAndy Fiddaman sc->pi |= (1 << p);
2533*5c4a5fe1SAndy Fiddaman if (sc->port[p].ioqsz < slots)
2534*5c4a5fe1SAndy Fiddaman slots = sc->port[p].ioqsz;
2535*5c4a5fe1SAndy Fiddaman }
2536*5c4a5fe1SAndy Fiddaman sc->ports = p;
2537*5c4a5fe1SAndy Fiddaman
2538*5c4a5fe1SAndy Fiddaman /* Intel ICH8 AHCI */
2539*5c4a5fe1SAndy Fiddaman --slots;
2540*5c4a5fe1SAndy Fiddaman if (sc->ports < DEF_PORTS)
2541*5c4a5fe1SAndy Fiddaman sc->ports = DEF_PORTS;
2542*5c4a5fe1SAndy Fiddaman sc->cap = AHCI_CAP_64BIT | AHCI_CAP_SNCQ | AHCI_CAP_SSNTF |
2543*5c4a5fe1SAndy Fiddaman AHCI_CAP_SMPS | AHCI_CAP_SSS | AHCI_CAP_SALP |
2544*5c4a5fe1SAndy Fiddaman AHCI_CAP_SAL | AHCI_CAP_SCLO | (0x3 << AHCI_CAP_ISS_SHIFT)|
2545*5c4a5fe1SAndy Fiddaman AHCI_CAP_PMD | AHCI_CAP_SSC | AHCI_CAP_PSC |
2546*5c4a5fe1SAndy Fiddaman (slots << AHCI_CAP_NCS_SHIFT) | AHCI_CAP_SXS | (sc->ports - 1);
2547*5c4a5fe1SAndy Fiddaman
2548*5c4a5fe1SAndy Fiddaman sc->vs = 0x10300;
2549*5c4a5fe1SAndy Fiddaman sc->cap2 = AHCI_CAP2_APST;
2550*5c4a5fe1SAndy Fiddaman ahci_reset(sc);
2551*5c4a5fe1SAndy Fiddaman
2552*5c4a5fe1SAndy Fiddaman pci_set_cfgdata16(pi, PCIR_DEVICE, 0x2821);
2553*5c4a5fe1SAndy Fiddaman pci_set_cfgdata16(pi, PCIR_VENDOR, 0x8086);
2554*5c4a5fe1SAndy Fiddaman pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_STORAGE);
2555*5c4a5fe1SAndy Fiddaman pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_STORAGE_SATA);
2556*5c4a5fe1SAndy Fiddaman pci_set_cfgdata8(pi, PCIR_PROGIF, PCIP_STORAGE_SATA_AHCI_1_0);
2557*5c4a5fe1SAndy Fiddaman p = MIN(sc->ports, 16);
2558*5c4a5fe1SAndy Fiddaman p = flsl(p) - ((p & (p - 1)) ? 0 : 1);
2559*5c4a5fe1SAndy Fiddaman pci_emul_add_msicap(pi, 1 << p);
2560*5c4a5fe1SAndy Fiddaman pci_emul_alloc_bar(pi, 5, PCIBAR_MEM32,
2561*5c4a5fe1SAndy Fiddaman AHCI_OFFSET + sc->ports * AHCI_STEP);
2562*5c4a5fe1SAndy Fiddaman
2563*5c4a5fe1SAndy Fiddaman pci_lintr_request(pi);
2564*5c4a5fe1SAndy Fiddaman
2565*5c4a5fe1SAndy Fiddaman open_fail:
2566*5c4a5fe1SAndy Fiddaman if (ret) {
2567*5c4a5fe1SAndy Fiddaman for (p = 0; p < sc->ports; p++) {
2568*5c4a5fe1SAndy Fiddaman if (sc->port[p].bctx != NULL)
2569*5c4a5fe1SAndy Fiddaman blockif_close(sc->port[p].bctx);
2570*5c4a5fe1SAndy Fiddaman }
2571*5c4a5fe1SAndy Fiddaman free(sc);
2572*5c4a5fe1SAndy Fiddaman }
2573*5c4a5fe1SAndy Fiddaman
2574*5c4a5fe1SAndy Fiddaman return (ret);
2575*5c4a5fe1SAndy Fiddaman }
2576*5c4a5fe1SAndy Fiddaman
2577*5c4a5fe1SAndy Fiddaman /*
2578*5c4a5fe1SAndy Fiddaman * Use separate emulation names to distinguish drive and atapi devices
2579*5c4a5fe1SAndy Fiddaman */
2580*5c4a5fe1SAndy Fiddaman static const struct pci_devemu pci_de_ahci = {
2581*5c4a5fe1SAndy Fiddaman .pe_emu = "ahci",
2582*5c4a5fe1SAndy Fiddaman .pe_init = pci_ahci_init,
2583*5c4a5fe1SAndy Fiddaman .pe_legacy_config = pci_ahci_legacy_config,
2584*5c4a5fe1SAndy Fiddaman .pe_barwrite = pci_ahci_write,
2585*5c4a5fe1SAndy Fiddaman .pe_barread = pci_ahci_read,
2586*5c4a5fe1SAndy Fiddaman };
2587*5c4a5fe1SAndy Fiddaman PCI_EMUL_SET(pci_de_ahci);
2588*5c4a5fe1SAndy Fiddaman
2589*5c4a5fe1SAndy Fiddaman static const struct pci_devemu pci_de_ahci_hd = {
2590*5c4a5fe1SAndy Fiddaman .pe_emu = "ahci-hd",
2591*5c4a5fe1SAndy Fiddaman .pe_legacy_config = pci_ahci_hd_legacy_config,
2592*5c4a5fe1SAndy Fiddaman .pe_alias = "ahci",
2593*5c4a5fe1SAndy Fiddaman };
2594*5c4a5fe1SAndy Fiddaman PCI_EMUL_SET(pci_de_ahci_hd);
2595*5c4a5fe1SAndy Fiddaman
2596*5c4a5fe1SAndy Fiddaman static const struct pci_devemu pci_de_ahci_cd = {
2597*5c4a5fe1SAndy Fiddaman .pe_emu = "ahci-cd",
2598*5c4a5fe1SAndy Fiddaman .pe_legacy_config = pci_ahci_cd_legacy_config,
2599*5c4a5fe1SAndy Fiddaman .pe_alias = "ahci",
2600*5c4a5fe1SAndy Fiddaman };
2601*5c4a5fe1SAndy Fiddaman PCI_EMUL_SET(pci_de_ahci_cd);
2602