]> Git Repo - qemu.git/blame - tests/libqos/ahci.c
ahci-test: test atapi read_cd with bcl, nb_sectors = 0
[qemu.git] / tests / libqos / ahci.c
CommitLineData
9a75b0a0
JS
1/*
2 * libqos AHCI functions
3 *
4 * Copyright (c) 2014 John Snow <[email protected]>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
53239262 25#include "qemu/osdep.h"
9a75b0a0
JS
26
27#include "libqtest.h"
28#include "libqos/ahci.h"
29#include "libqos/pci-pc.h"
30
31#include "qemu-common.h"
32#include "qemu/host-utils.h"
33
34#include "hw/pci/pci_ids.h"
35#include "hw/pci/pci_regs.h"
36
716b6407
JS
37typedef struct AHCICommandProp {
38 uint8_t cmd; /* Command Code */
39 bool data; /* Data transfer command? */
40 bool pio;
41 bool dma;
42 bool lba28;
43 bool lba48;
44 bool read;
45 bool write;
46 bool atapi;
47 bool ncq;
48 uint64_t size; /* Static transfer size, for commands like IDENTIFY. */
49 uint32_t interrupts; /* Expected interrupts for this command. */
50} AHCICommandProp;
51
52AHCICommandProp ahci_command_properties[] = {
26ad0045
JS
53 { .cmd = CMD_READ_PIO, .data = true, .pio = true,
54 .lba28 = true, .read = true },
55 { .cmd = CMD_WRITE_PIO, .data = true, .pio = true,
56 .lba28 = true, .write = true },
57 { .cmd = CMD_READ_PIO_EXT, .data = true, .pio = true,
58 .lba48 = true, .read = true },
59 { .cmd = CMD_WRITE_PIO_EXT, .data = true, .pio = true,
60 .lba48 = true, .write = true },
61 { .cmd = CMD_READ_DMA, .data = true, .dma = true,
62 .lba28 = true, .read = true },
63 { .cmd = CMD_WRITE_DMA, .data = true, .dma = true,
64 .lba28 = true, .write = true },
65 { .cmd = CMD_READ_DMA_EXT, .data = true, .dma = true,
66 .lba48 = true, .read = true },
67 { .cmd = CMD_WRITE_DMA_EXT, .data = true, .dma = true,
68 .lba48 = true, .write = true },
69 { .cmd = CMD_IDENTIFY, .data = true, .pio = true,
70 .size = 512, .read = true },
71 { .cmd = READ_FPDMA_QUEUED, .data = true, .dma = true,
72 .lba48 = true, .read = true, .ncq = true },
73 { .cmd = WRITE_FPDMA_QUEUED, .data = true, .dma = true,
74 .lba48 = true, .write = true, .ncq = true },
75 { .cmd = CMD_READ_MAX, .lba28 = true },
76 { .cmd = CMD_READ_MAX_EXT, .lba48 = true },
54d268b2
JS
77 { .cmd = CMD_FLUSH_CACHE, .data = false },
78 { .cmd = CMD_PACKET, .data = true, .size = 16,
b88641e2 79 .atapi = true, .pio = true },
54d268b2
JS
80 { .cmd = CMD_PACKET_ID, .data = true, .pio = true,
81 .size = 512, .read = true }
716b6407
JS
82};
83
40d29928
JS
84struct AHCICommand {
85 /* Test Management Data */
86 uint8_t name;
87 uint8_t port;
88 uint8_t slot;
89 uint32_t interrupts;
90 uint64_t xbytes;
91 uint32_t prd_size;
92 uint64_t buffer;
93 AHCICommandProp *props;
94 /* Data to be transferred to the guest */
95 AHCICommandHeader header;
96 RegH2DFIS fis;
54d268b2 97 unsigned char *atapi_cmd;
40d29928
JS
98};
99
9a75b0a0
JS
100/**
101 * Allocate space in the guest using information in the AHCIQState object.
102 */
103uint64_t ahci_alloc(AHCIQState *ahci, size_t bytes)
104{
105 g_assert(ahci);
106 g_assert(ahci->parent);
107 return qmalloc(ahci->parent, bytes);
108}
109
110void ahci_free(AHCIQState *ahci, uint64_t addr)
111{
112 g_assert(ahci);
113 g_assert(ahci->parent);
114 qfree(ahci->parent, addr);
115}
116
d0b282a5
JS
117bool is_atapi(AHCIQState *ahci, uint8_t port)
118{
119 return ahci_px_rreg(ahci, port, AHCI_PX_SIG) == AHCI_SIGNATURE_CDROM;
120}
121
9a75b0a0
JS
122/**
123 * Locate, verify, and return a handle to the AHCI device.
124 */
125QPCIDevice *get_ahci_device(uint32_t *fingerprint)
126{
127 QPCIDevice *ahci;
128 uint32_t ahci_fingerprint;
129 QPCIBus *pcibus;
130
2ecd7e2f 131 pcibus = qpci_init_pc(NULL);
9a75b0a0
JS
132
133 /* Find the AHCI PCI device and verify it's the right one. */
134 ahci = qpci_device_find(pcibus, QPCI_DEVFN(0x1F, 0x02));
135 g_assert(ahci != NULL);
136
137 ahci_fingerprint = qpci_config_readl(ahci, PCI_VENDOR_ID);
138
139 switch (ahci_fingerprint) {
140 case AHCI_INTEL_ICH9:
141 break;
142 default:
143 /* Unknown device. */
144 g_assert_not_reached();
145 }
146
147 if (fingerprint) {
148 *fingerprint = ahci_fingerprint;
149 }
150 return ahci;
151}
152
153void free_ahci_device(QPCIDevice *dev)
154{
155 QPCIBus *pcibus = dev ? dev->bus : NULL;
156
157 /* libqos doesn't have a function for this, so free it manually */
158 g_free(dev);
159 qpci_free_pc(pcibus);
160}
161
259342d3
JS
162/* Free all memory in-use by the AHCI device. */
163void ahci_clean_mem(AHCIQState *ahci)
164{
165 uint8_t port, slot;
166
167 for (port = 0; port < 32; ++port) {
168 if (ahci->port[port].fb) {
169 ahci_free(ahci, ahci->port[port].fb);
95ea6636 170 ahci->port[port].fb = 0;
259342d3
JS
171 }
172 if (ahci->port[port].clb) {
173 for (slot = 0; slot < 32; slot++) {
174 ahci_destroy_command(ahci, port, slot);
175 }
176 ahci_free(ahci, ahci->port[port].clb);
95ea6636 177 ahci->port[port].clb = 0;
259342d3
JS
178 }
179 }
180}
181
9a75b0a0
JS
182/*** Logical Device Initialization ***/
183
184/**
185 * Start the PCI device and sanity-check default operation.
186 */
187void ahci_pci_enable(AHCIQState *ahci)
188{
189 uint8_t reg;
190
191 start_ahci_device(ahci);
192
193 switch (ahci->fingerprint) {
194 case AHCI_INTEL_ICH9:
195 /* ICH9 has a register at PCI 0x92 that
196 * acts as a master port enabler mask. */
197 reg = qpci_config_readb(ahci->dev, 0x92);
198 reg |= 0x3F;
199 qpci_config_writeb(ahci->dev, 0x92, reg);
200 /* 0...0111111b -- bit significant, ports 0-5 enabled. */
201 ASSERT_BIT_SET(qpci_config_readb(ahci->dev, 0x92), 0x3F);
202 break;
203 }
204
205}
206
207/**
208 * Map BAR5/ABAR, and engage the PCI device.
209 */
210void start_ahci_device(AHCIQState *ahci)
211{
212 /* Map AHCI's ABAR (BAR5) */
b4ba67d9 213 ahci->hba_bar = qpci_iomap(ahci->dev, 5, &ahci->barsize);
9a75b0a0
JS
214
215 /* turns on pci.cmd.iose, pci.cmd.mse and pci.cmd.bme */
216 qpci_device_enable(ahci->dev);
217}
218
219/**
220 * Test and initialize the AHCI's HBA memory areas.
221 * Initialize and start any ports with devices attached.
222 * Bring the HBA into the idle state.
223 */
224void ahci_hba_enable(AHCIQState *ahci)
225{
226 /* Bits of interest in this section:
227 * GHC.AE Global Host Control / AHCI Enable
228 * PxCMD.ST Port Command: Start
229 * PxCMD.SUD "Spin Up Device"
230 * PxCMD.POD "Power On Device"
231 * PxCMD.FRE "FIS Receive Enable"
232 * PxCMD.FR "FIS Receive Running"
233 * PxCMD.CR "Command List Running"
234 */
235 uint32_t reg, ports_impl;
236 uint16_t i;
237 uint8_t num_cmd_slots;
238
239 g_assert(ahci != NULL);
240
241 /* Set GHC.AE to 1 */
242 ahci_set(ahci, AHCI_GHC, AHCI_GHC_AE);
243 reg = ahci_rreg(ahci, AHCI_GHC);
244 ASSERT_BIT_SET(reg, AHCI_GHC_AE);
245
246 /* Cache CAP and CAP2. */
247 ahci->cap = ahci_rreg(ahci, AHCI_CAP);
248 ahci->cap2 = ahci_rreg(ahci, AHCI_CAP2);
249
250 /* Read CAP.NCS, how many command slots do we have? */
251 num_cmd_slots = ((ahci->cap & AHCI_CAP_NCS) >> ctzl(AHCI_CAP_NCS)) + 1;
252 g_test_message("Number of Command Slots: %u", num_cmd_slots);
253
254 /* Determine which ports are implemented. */
255 ports_impl = ahci_rreg(ahci, AHCI_PI);
256
257 for (i = 0; ports_impl; ports_impl >>= 1, ++i) {
258 if (!(ports_impl & 0x01)) {
259 continue;
260 }
261
262 g_test_message("Initializing port %u", i);
263
264 reg = ahci_px_rreg(ahci, i, AHCI_PX_CMD);
265 if (BITCLR(reg, AHCI_PX_CMD_ST | AHCI_PX_CMD_CR |
266 AHCI_PX_CMD_FRE | AHCI_PX_CMD_FR)) {
267 g_test_message("port is idle");
268 } else {
269 g_test_message("port needs to be idled");
270 ahci_px_clr(ahci, i, AHCI_PX_CMD,
271 (AHCI_PX_CMD_ST | AHCI_PX_CMD_FRE));
272 /* The port has 500ms to disengage. */
273 usleep(500000);
274 reg = ahci_px_rreg(ahci, i, AHCI_PX_CMD);
275 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CR);
276 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FR);
277 g_test_message("port is now idle");
278 /* The spec does allow for possibly needing a PORT RESET
279 * or HBA reset if we fail to idle the port. */
280 }
281
282 /* Allocate Memory for the Command List Buffer & FIS Buffer */
283 /* PxCLB space ... 0x20 per command, as in 4.2.2 p 36 */
284 ahci->port[i].clb = ahci_alloc(ahci, num_cmd_slots * 0x20);
95ea6636 285 qmemset(ahci->port[i].clb, 0x00, num_cmd_slots * 0x20);
9a75b0a0
JS
286 g_test_message("CLB: 0x%08" PRIx64, ahci->port[i].clb);
287 ahci_px_wreg(ahci, i, AHCI_PX_CLB, ahci->port[i].clb);
288 g_assert_cmphex(ahci->port[i].clb, ==,
289 ahci_px_rreg(ahci, i, AHCI_PX_CLB));
290
291 /* PxFB space ... 0x100, as in 4.2.1 p 35 */
292 ahci->port[i].fb = ahci_alloc(ahci, 0x100);
293 qmemset(ahci->port[i].fb, 0x00, 0x100);
294 g_test_message("FB: 0x%08" PRIx64, ahci->port[i].fb);
295 ahci_px_wreg(ahci, i, AHCI_PX_FB, ahci->port[i].fb);
296 g_assert_cmphex(ahci->port[i].fb, ==,
297 ahci_px_rreg(ahci, i, AHCI_PX_FB));
298
299 /* Clear PxSERR, PxIS, then IS.IPS[x] by writing '1's. */
300 ahci_px_wreg(ahci, i, AHCI_PX_SERR, 0xFFFFFFFF);
301 ahci_px_wreg(ahci, i, AHCI_PX_IS, 0xFFFFFFFF);
302 ahci_wreg(ahci, AHCI_IS, (1 << i));
303
304 /* Verify Interrupts Cleared */
305 reg = ahci_px_rreg(ahci, i, AHCI_PX_SERR);
306 g_assert_cmphex(reg, ==, 0);
307
308 reg = ahci_px_rreg(ahci, i, AHCI_PX_IS);
309 g_assert_cmphex(reg, ==, 0);
310
311 reg = ahci_rreg(ahci, AHCI_IS);
312 ASSERT_BIT_CLEAR(reg, (1 << i));
313
314 /* Enable All Interrupts: */
315 ahci_px_wreg(ahci, i, AHCI_PX_IE, 0xFFFFFFFF);
316 reg = ahci_px_rreg(ahci, i, AHCI_PX_IE);
317 g_assert_cmphex(reg, ==, ~((uint32_t)AHCI_PX_IE_RESERVED));
318
319 /* Enable the FIS Receive Engine. */
320 ahci_px_set(ahci, i, AHCI_PX_CMD, AHCI_PX_CMD_FRE);
321 reg = ahci_px_rreg(ahci, i, AHCI_PX_CMD);
322 ASSERT_BIT_SET(reg, AHCI_PX_CMD_FR);
323
324 /* AHCI 1.3 spec: if !STS.BSY, !STS.DRQ and PxSSTS.DET indicates
325 * physical presence, a device is present and may be started. However,
326 * PxSERR.DIAG.X /may/ need to be cleared a priori. */
327 reg = ahci_px_rreg(ahci, i, AHCI_PX_SERR);
328 if (BITSET(reg, AHCI_PX_SERR_DIAG_X)) {
329 ahci_px_set(ahci, i, AHCI_PX_SERR, AHCI_PX_SERR_DIAG_X);
330 }
331
332 reg = ahci_px_rreg(ahci, i, AHCI_PX_TFD);
333 if (BITCLR(reg, AHCI_PX_TFD_STS_BSY | AHCI_PX_TFD_STS_DRQ)) {
334 reg = ahci_px_rreg(ahci, i, AHCI_PX_SSTS);
335 if ((reg & AHCI_PX_SSTS_DET) == SSTS_DET_ESTABLISHED) {
336 /* Device Found: set PxCMD.ST := 1 */
337 ahci_px_set(ahci, i, AHCI_PX_CMD, AHCI_PX_CMD_ST);
338 ASSERT_BIT_SET(ahci_px_rreg(ahci, i, AHCI_PX_CMD),
339 AHCI_PX_CMD_CR);
340 g_test_message("Started Device %u", i);
341 } else if ((reg & AHCI_PX_SSTS_DET)) {
342 /* Device present, but in some unknown state. */
343 g_assert_not_reached();
344 }
345 }
346 }
347
348 /* Enable GHC.IE */
349 ahci_set(ahci, AHCI_GHC, AHCI_GHC_IE);
350 reg = ahci_rreg(ahci, AHCI_GHC);
351 ASSERT_BIT_SET(reg, AHCI_GHC_IE);
352
e7c8526b 353 ahci->enabled = true;
9a75b0a0
JS
354 /* TODO: The device should now be idling and waiting for commands.
355 * In the future, a small test-case to inspect the Register D2H FIS
356 * and clear the initial interrupts might be good. */
357}
e77448a3
JS
358
359/**
360 * Pick the first implemented and running port
361 */
362unsigned ahci_port_select(AHCIQState *ahci)
363{
364 uint32_t ports, reg;
365 unsigned i;
366
367 ports = ahci_rreg(ahci, AHCI_PI);
368 for (i = 0; i < 32; ports >>= 1, ++i) {
369 if (ports == 0) {
370 i = 32;
371 }
372
373 if (!(ports & 0x01)) {
374 continue;
375 }
376
377 reg = ahci_px_rreg(ahci, i, AHCI_PX_CMD);
378 if (BITSET(reg, AHCI_PX_CMD_ST)) {
379 break;
380 }
381 }
382 g_assert(i < 32);
383 return i;
384}
e83fd96b
JS
385
386/**
387 * Clear a port's interrupts and status information prior to a test.
388 */
389void ahci_port_clear(AHCIQState *ahci, uint8_t port)
390{
391 uint32_t reg;
392
393 /* Clear out this port's interrupts (ignore the init register d2h fis) */
394 reg = ahci_px_rreg(ahci, port, AHCI_PX_IS);
395 ahci_px_wreg(ahci, port, AHCI_PX_IS, reg);
396 g_assert_cmphex(ahci_px_rreg(ahci, port, AHCI_PX_IS), ==, 0);
397
631b22ea 398 /* Wipe the FIS-Receive Buffer */
e83fd96b
JS
399 qmemset(ahci->port[port].fb, 0x00, 0x100);
400}
6cae27a6 401
85c34e93
JS
402/**
403 * Check a port for errors.
404 */
405void ahci_port_check_error(AHCIQState *ahci, uint8_t port)
406{
407 uint32_t reg;
408
409 /* The upper 9 bits of the IS register all indicate errors. */
410 reg = ahci_px_rreg(ahci, port, AHCI_PX_IS);
411 reg >>= 23;
412 g_assert_cmphex(reg, ==, 0);
413
414 /* The Sata Error Register should be empty. */
415 reg = ahci_px_rreg(ahci, port, AHCI_PX_SERR);
416 g_assert_cmphex(reg, ==, 0);
417
418 /* The TFD also has two error sections. */
419 reg = ahci_px_rreg(ahci, port, AHCI_PX_TFD);
420 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_ERR);
421 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_ERR);
422}
423
5bf99aa1
JS
424void ahci_port_check_interrupts(AHCIQState *ahci, uint8_t port,
425 uint32_t intr_mask)
426{
427 uint32_t reg;
428
429 /* Check for expected interrupts */
430 reg = ahci_px_rreg(ahci, port, AHCI_PX_IS);
431 ASSERT_BIT_SET(reg, intr_mask);
432
433 /* Clear expected interrupts and assert all interrupts now cleared. */
434 ahci_px_wreg(ahci, port, AHCI_PX_IS, intr_mask);
435 g_assert_cmphex(ahci_px_rreg(ahci, port, AHCI_PX_IS), ==, 0);
436}
437
89a46723
JS
438void ahci_port_check_nonbusy(AHCIQState *ahci, uint8_t port, uint8_t slot)
439{
440 uint32_t reg;
441
442 /* Assert that the command slot is no longer busy (NCQ) */
443 reg = ahci_px_rreg(ahci, port, AHCI_PX_SACT);
444 ASSERT_BIT_CLEAR(reg, (1 << slot));
445
446 /* Non-NCQ */
447 reg = ahci_px_rreg(ahci, port, AHCI_PX_CI);
448 ASSERT_BIT_CLEAR(reg, (1 << slot));
449
450 /* And assert that we are generally not busy. */
451 reg = ahci_px_rreg(ahci, port, AHCI_PX_TFD);
452 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_BSY);
453 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_DRQ);
454}
455
d1ef8838
JS
456void ahci_port_check_d2h_sanity(AHCIQState *ahci, uint8_t port, uint8_t slot)
457{
458 RegD2HFIS *d2h = g_malloc0(0x20);
459 uint32_t reg;
460
461 memread(ahci->port[port].fb + 0x40, d2h, 0x20);
462 g_assert_cmphex(d2h->fis_type, ==, 0x34);
463
464 reg = ahci_px_rreg(ahci, port, AHCI_PX_TFD);
465 g_assert_cmphex((reg & AHCI_PX_TFD_ERR) >> 8, ==, d2h->error);
466 g_assert_cmphex((reg & AHCI_PX_TFD_STS), ==, d2h->status);
467
468 g_free(d2h);
469}
470
471void ahci_port_check_pio_sanity(AHCIQState *ahci, uint8_t port,
472 uint8_t slot, size_t buffsize)
473{
474 PIOSetupFIS *pio = g_malloc0(0x20);
475
631b22ea 476 /* We cannot check the Status or E_Status registers, because
d1ef8838
JS
477 * the status may have again changed between the PIO Setup FIS
478 * and the conclusion of the command with the D2H Register FIS. */
479 memread(ahci->port[port].fb + 0x20, pio, 0x20);
480 g_assert_cmphex(pio->fis_type, ==, 0x5f);
481
482 /* BUG: PIO Setup FIS as utilized by QEMU tries to fit the entire
483 * transfer size in a uint16_t field. The maximum transfer size can
484 * eclipse this; the field is meant to convey the size of data per
485 * each Data FIS, not the entire operation as a whole. For now,
486 * we will sanity check the broken case where applicable. */
487 if (buffsize <= UINT16_MAX) {
488 g_assert_cmphex(le16_to_cpu(pio->tx_count), ==, buffsize);
489 }
490
491 g_free(pio);
492}
493
40d29928 494void ahci_port_check_cmd_sanity(AHCIQState *ahci, AHCICommand *cmd)
d1ef8838 495{
40d29928 496 AHCICommandHeader cmdh;
d1ef8838 497
40d29928
JS
498 ahci_get_command_header(ahci, cmd->port, cmd->slot, &cmdh);
499 /* Physical Region Descriptor Byte Count is not required to work for NCQ. */
500 if (!cmd->props->ncq) {
501 g_assert_cmphex(cmd->xbytes, ==, cmdh.prdbc);
502 }
d1ef8838
JS
503}
504
6cae27a6
JS
505/* Get the command in #slot of port #port. */
506void ahci_get_command_header(AHCIQState *ahci, uint8_t port,
507 uint8_t slot, AHCICommandHeader *cmd)
508{
509 uint64_t ba = ahci->port[port].clb;
510 ba += slot * sizeof(AHCICommandHeader);
511 memread(ba, cmd, sizeof(AHCICommandHeader));
512
513 cmd->flags = le16_to_cpu(cmd->flags);
514 cmd->prdtl = le16_to_cpu(cmd->prdtl);
515 cmd->prdbc = le32_to_cpu(cmd->prdbc);
516 cmd->ctba = le64_to_cpu(cmd->ctba);
517}
518
519/* Set the command in #slot of port #port. */
520void ahci_set_command_header(AHCIQState *ahci, uint8_t port,
521 uint8_t slot, AHCICommandHeader *cmd)
522{
4a42f6d4 523 AHCICommandHeader tmp = { .flags = 0 };
6cae27a6
JS
524 uint64_t ba = ahci->port[port].clb;
525 ba += slot * sizeof(AHCICommandHeader);
526
527 tmp.flags = cpu_to_le16(cmd->flags);
528 tmp.prdtl = cpu_to_le16(cmd->prdtl);
529 tmp.prdbc = cpu_to_le32(cmd->prdbc);
530 tmp.ctba = cpu_to_le64(cmd->ctba);
531
532 memwrite(ba, &tmp, sizeof(AHCICommandHeader));
533}
534
535void ahci_destroy_command(AHCIQState *ahci, uint8_t port, uint8_t slot)
536{
537 AHCICommandHeader cmd;
538
539 /* Obtain the Nth Command Header */
540 ahci_get_command_header(ahci, port, slot, &cmd);
541 if (cmd.ctba == 0) {
542 /* No address in it, so just return -- it's empty. */
543 goto tidy;
544 }
545
546 /* Free the Table */
547 ahci_free(ahci, cmd.ctba);
548
549 tidy:
550 /* NULL the header. */
551 memset(&cmd, 0x00, sizeof(cmd));
552 ahci_set_command_header(ahci, port, slot, &cmd);
553 ahci->port[port].ctba[slot] = 0;
554 ahci->port[port].prdtl[slot] = 0;
555}
556
9ab9993f 557void ahci_write_fis(AHCIQState *ahci, AHCICommand *cmd)
52515766 558{
9ab9993f
JS
559 RegH2DFIS tmp = cmd->fis;
560 uint64_t addr = cmd->header.ctba;
52515766 561
9ab9993f
JS
562 /* NCQ commands use exclusively 8 bit fields and needs no adjustment.
563 * Only the count field needs to be adjusted for non-NCQ commands.
564 * The auxiliary FIS fields are defined per-command and are not currently
565 * implemented in libqos/ahci.o, but may or may not need to be flipped. */
566 if (!cmd->props->ncq) {
567 tmp.count = cpu_to_le16(tmp.count);
568 }
52515766
JS
569
570 memwrite(addr, &tmp, sizeof(tmp));
571}
572
6cae27a6
JS
573unsigned ahci_pick_cmd(AHCIQState *ahci, uint8_t port)
574{
575 unsigned i;
576 unsigned j;
577 uint32_t reg;
578
579 reg = ahci_px_rreg(ahci, port, AHCI_PX_CI);
580
581 /* Pick the least recently used command slot that's available */
582 for (i = 0; i < 32; ++i) {
583 j = ((ahci->port[port].next + i) % 32);
584 if (reg & (1 << j)) {
585 continue;
586 }
95ea6636 587 ahci_destroy_command(ahci, port, j);
6cae27a6
JS
588 ahci->port[port].next = (j + 1) % 32;
589 return j;
590 }
591
592 g_test_message("All command slots were busy.");
593 g_assert_not_reached();
594}
64a5a272
JS
595
596inline unsigned size_to_prdtl(unsigned bytes, unsigned bytes_per_prd)
597{
598 /* Each PRD can describe up to 4MiB */
599 g_assert_cmphex(bytes_per_prd, <=, 4096 * 1024);
600 g_assert_cmphex(bytes_per_prd & 0x01, ==, 0x00);
601 return (bytes + bytes_per_prd - 1) / bytes_per_prd;
602}
603
9350df7c
JS
604const AHCIOpts default_opts = { .size = 0 };
605
606/**
607 * ahci_exec: execute a given command on a specific
608 * AHCI port.
609 *
610 * @ahci: The device to send the command to
611 * @port: The port number of the SATA device we wish
612 * to have execute this command
613 * @op: The S/ATA command to execute, or if opts.atapi
614 * is true, the SCSI command code.
615 * @opts: Optional arguments to modify execution behavior.
616 */
617void ahci_exec(AHCIQState *ahci, uint8_t port,
618 uint8_t op, const AHCIOpts *opts_in)
619{
620 AHCICommand *cmd;
621 int rc;
622 AHCIOpts *opts;
623
624 opts = g_memdup((opts_in == NULL ? &default_opts : opts_in),
625 sizeof(AHCIOpts));
626
627 /* No guest buffer provided, create one. */
628 if (opts->size && !opts->buffer) {
629 opts->buffer = ahci_alloc(ahci, opts->size);
630 g_assert(opts->buffer);
631 qmemset(opts->buffer, 0x00, opts->size);
632 }
633
634 /* Command creation */
635 if (opts->atapi) {
ebde93bf
JS
636 uint16_t bcl = opts->set_bcl ? opts->bcl : ATAPI_SECTOR_SIZE;
637 cmd = ahci_atapi_command_create(op, bcl);
9350df7c
JS
638 if (opts->atapi_dma) {
639 ahci_command_enable_atapi_dma(cmd);
640 }
641 } else {
642 cmd = ahci_command_create(op);
643 }
644 ahci_command_adjust(cmd, opts->lba, opts->buffer,
645 opts->size, opts->prd_size);
646
647 if (opts->pre_cb) {
648 rc = opts->pre_cb(ahci, cmd, opts);
649 g_assert_cmpint(rc, ==, 0);
650 }
651
652 /* Write command to memory and issue it */
653 ahci_command_commit(ahci, cmd, port);
654 ahci_command_issue_async(ahci, cmd);
655 if (opts->error) {
656 qmp_eventwait("STOP");
657 }
658 if (opts->mid_cb) {
659 rc = opts->mid_cb(ahci, cmd, opts);
660 g_assert_cmpint(rc, ==, 0);
661 }
662 if (opts->error) {
663 qmp_async("{'execute':'cont' }");
664 qmp_eventwait("RESUME");
665 }
666
667 /* Wait for command to complete and verify sanity */
668 ahci_command_wait(ahci, cmd);
669 ahci_command_verify(ahci, cmd);
670 if (opts->post_cb) {
671 rc = opts->post_cb(ahci, cmd, opts);
672 g_assert_cmpint(rc, ==, 0);
673 }
674 ahci_command_free(cmd);
675 if (opts->buffer != opts_in->buffer) {
676 ahci_free(ahci, opts->buffer);
677 }
678 g_free(opts);
679}
680
008b6e12
JS
681/* Issue a command, expecting it to fail and STOP the VM */
682AHCICommand *ahci_guest_io_halt(AHCIQState *ahci, uint8_t port,
683 uint8_t ide_cmd, uint64_t buffer,
684 size_t bufsize, uint64_t sector)
685{
686 AHCICommand *cmd;
687
688 cmd = ahci_command_create(ide_cmd);
689 ahci_command_adjust(cmd, sector, buffer, bufsize, 0);
690 ahci_command_commit(ahci, cmd, port);
691 ahci_command_issue_async(ahci, cmd);
692 qmp_eventwait("STOP");
693
694 return cmd;
695}
696
697/* Resume a previously failed command and verify/finalize */
698void ahci_guest_io_resume(AHCIQState *ahci, AHCICommand *cmd)
699{
700 /* Complete the command */
701 qmp_async("{'execute':'cont' }");
702 qmp_eventwait("RESUME");
703 ahci_command_wait(ahci, cmd);
704 ahci_command_verify(ahci, cmd);
705 ahci_command_free(cmd);
706}
707
11322195
JS
708/* Given a guest buffer address, perform an IO operation */
709void ahci_guest_io(AHCIQState *ahci, uint8_t port, uint8_t ide_cmd,
727be1a7 710 uint64_t buffer, size_t bufsize, uint64_t sector)
11322195
JS
711{
712 AHCICommand *cmd;
11322195
JS
713 cmd = ahci_command_create(ide_cmd);
714 ahci_command_set_buffer(cmd, buffer);
715 ahci_command_set_size(cmd, bufsize);
727be1a7
JS
716 if (sector) {
717 ahci_command_set_offset(cmd, sector);
718 }
11322195
JS
719 ahci_command_commit(ahci, cmd, port);
720 ahci_command_issue(ahci, cmd);
721 ahci_command_verify(ahci, cmd);
722 ahci_command_free(cmd);
723}
724
64a5a272
JS
725static AHCICommandProp *ahci_command_find(uint8_t command_name)
726{
727 int i;
728
729 for (i = 0; i < ARRAY_SIZE(ahci_command_properties); i++) {
730 if (ahci_command_properties[i].cmd == command_name) {
731 return &ahci_command_properties[i];
732 }
733 }
734
735 return NULL;
736}
737
ae029620
JS
738/* Given a HOST buffer, create a buffer address and perform an IO operation. */
739void ahci_io(AHCIQState *ahci, uint8_t port, uint8_t ide_cmd,
727be1a7 740 void *buffer, size_t bufsize, uint64_t sector)
ae029620
JS
741{
742 uint64_t ptr;
743 AHCICommandProp *props;
744
745 props = ahci_command_find(ide_cmd);
746 g_assert(props);
747 ptr = ahci_alloc(ahci, bufsize);
b1b66c3b 748 g_assert(!bufsize || ptr);
88e21f94 749 qmemset(ptr, 0x00, bufsize);
ae029620 750
b1b66c3b 751 if (bufsize && props->write) {
91d0374a 752 bufwrite(ptr, buffer, bufsize);
ae029620
JS
753 }
754
727be1a7 755 ahci_guest_io(ahci, port, ide_cmd, ptr, bufsize, sector);
ae029620 756
b1b66c3b 757 if (bufsize && props->read) {
91d0374a 758 bufread(ptr, buffer, bufsize);
ae029620
JS
759 }
760
761 ahci_free(ahci, ptr);
762}
763
64a5a272
JS
764/**
765 * Initializes a basic command header in memory.
766 * We assume that this is for an ATA command using RegH2DFIS.
767 */
768static void command_header_init(AHCICommand *cmd)
769{
770 AHCICommandHeader *hdr = &cmd->header;
771 AHCICommandProp *props = cmd->props;
772
773 hdr->flags = 5; /* RegH2DFIS is 5 DW long. Must be < 32 */
774 hdr->flags |= CMDH_CLR_BSY; /* Clear the BSY bit when done */
775 if (props->write) {
776 hdr->flags |= CMDH_WRITE;
777 }
778 if (props->atapi) {
779 hdr->flags |= CMDH_ATAPI;
780 }
781 /* Other flags: PREFETCH, RESET, and BIST */
782 hdr->prdtl = size_to_prdtl(cmd->xbytes, cmd->prd_size);
783 hdr->prdbc = 0;
784 hdr->ctba = 0;
785}
786
787static void command_table_init(AHCICommand *cmd)
788{
789 RegH2DFIS *fis = &(cmd->fis);
cb453041 790 uint16_t sect_count = (cmd->xbytes / AHCI_SECTOR_SIZE);
64a5a272
JS
791
792 fis->fis_type = REG_H2D_FIS;
793 fis->flags = REG_H2D_FIS_CMD; /* "Command" bit */
794 fis->command = cmd->name;
cb453041
JS
795
796 if (cmd->props->ncq) {
797 NCQFIS *ncqfis = (NCQFIS *)fis;
798 /* NCQ is weird and re-uses FIS frames for unrelated data.
799 * See SATA 3.2, 13.6.4.1 READ FPDMA QUEUED for an example. */
800 ncqfis->sector_low = sect_count & 0xFF;
801 ncqfis->sector_hi = (sect_count >> 8) & 0xFF;
802 ncqfis->device = NCQ_DEVICE_MAGIC;
803 /* Force Unit Access is bit 7 in the device register */
804 ncqfis->tag = 0; /* bits 3-7 are the NCQ tag */
805 ncqfis->prio = 0; /* bits 6,7 are a prio tag */
806 /* RARC bit is bit 0 of TAG field */
807 } else {
808 fis->feature_low = 0x00;
809 fis->feature_high = 0x00;
810 if (cmd->props->lba28 || cmd->props->lba48) {
811 fis->device = ATA_DEVICE_LBA;
812 }
813 fis->count = (cmd->xbytes / AHCI_SECTOR_SIZE);
64a5a272 814 }
cb453041
JS
815 fis->icc = 0x00;
816 fis->control = 0x00;
817 memset(fis->aux, 0x00, ARRAY_SIZE(fis->aux));
64a5a272
JS
818}
819
54d268b2
JS
820void ahci_command_enable_atapi_dma(AHCICommand *cmd)
821{
822 RegH2DFIS *fis = &(cmd->fis);
823 g_assert(cmd->props->atapi);
824 fis->feature_low |= 0x01;
b88641e2
JS
825 cmd->interrupts &= ~AHCI_PX_IS_PSS;
826 cmd->props->dma = true;
827 cmd->props->pio = false;
828 /* BUG: We expect the DMA Setup interrupt for DMA commands */
829 /* cmd->interrupts |= AHCI_PX_IS_DSS; */
54d268b2
JS
830}
831
64a5a272
JS
832AHCICommand *ahci_command_create(uint8_t command_name)
833{
834 AHCICommandProp *props = ahci_command_find(command_name);
835 AHCICommand *cmd;
836
837 g_assert(props);
838 cmd = g_malloc0(sizeof(AHCICommand));
839 g_assert(!(props->dma && props->pio));
840 g_assert(!(props->lba28 && props->lba48));
841 g_assert(!(props->read && props->write));
842 g_assert(!props->size || props->data);
3d937150 843 g_assert(!props->ncq || props->lba48);
64a5a272
JS
844
845 /* Defaults and book-keeping */
b88641e2 846 cmd->props = g_memdup(props, sizeof(AHCICommandProp));
64a5a272
JS
847 cmd->name = command_name;
848 cmd->xbytes = props->size;
849 cmd->prd_size = 4096;
850 cmd->buffer = 0xabad1dea;
851
359790c2
JS
852 if (!cmd->props->ncq) {
853 cmd->interrupts = AHCI_PX_IS_DHRS;
854 }
64a5a272
JS
855 /* BUG: We expect the DPS interrupt for data commands */
856 /* cmd->interrupts |= props->data ? AHCI_PX_IS_DPS : 0; */
857 /* BUG: We expect the DMA Setup interrupt for DMA commands */
858 /* cmd->interrupts |= props->dma ? AHCI_PX_IS_DSS : 0; */
859 cmd->interrupts |= props->pio ? AHCI_PX_IS_PSS : 0;
359790c2 860 cmd->interrupts |= props->ncq ? AHCI_PX_IS_SDBS : 0;
64a5a272
JS
861
862 command_header_init(cmd);
863 command_table_init(cmd);
864
865 return cmd;
866}
867
ebde93bf 868AHCICommand *ahci_atapi_command_create(uint8_t scsi_cmd, uint16_t bcl)
54d268b2
JS
869{
870 AHCICommand *cmd = ahci_command_create(CMD_PACKET);
871 cmd->atapi_cmd = g_malloc0(16);
872 cmd->atapi_cmd[0] = scsi_cmd;
ebde93bf 873 stw_le_p(&cmd->fis.lba_lo[1], bcl);
54d268b2
JS
874 return cmd;
875}
876
64a5a272
JS
877void ahci_command_free(AHCICommand *cmd)
878{
54d268b2 879 g_free(cmd->atapi_cmd);
b88641e2 880 g_free(cmd->props);
64a5a272
JS
881 g_free(cmd);
882}
883
f9f963e0
JS
884void ahci_command_set_flags(AHCICommand *cmd, uint16_t cmdh_flags)
885{
886 cmd->header.flags |= cmdh_flags;
887}
888
889void ahci_command_clr_flags(AHCICommand *cmd, uint16_t cmdh_flags)
890{
891 cmd->header.flags &= ~cmdh_flags;
892}
893
54d268b2
JS
894static void ahci_atapi_command_set_offset(AHCICommand *cmd, uint64_t lba)
895{
896 unsigned char *cbd = cmd->atapi_cmd;
897 g_assert(cbd);
898
899 switch (cbd[0]) {
900 case CMD_ATAPI_READ_10:
ebde93bf 901 case CMD_ATAPI_READ_CD:
54d268b2
JS
902 g_assert_cmpuint(lba, <=, UINT32_MAX);
903 stl_be_p(&cbd[2], lba);
904 break;
905 default:
906 /* SCSI doesn't have uniform packet formats,
907 * so you have to add support for it manually. Sorry! */
ebde93bf
JS
908 fprintf(stderr, "The Libqos AHCI driver does not support the "
909 "set_offset operation for ATAPI command 0x%02x, "
910 "please add support.\n",
911 cbd[0]);
54d268b2
JS
912 g_assert_not_reached();
913 }
914}
915
f9f963e0
JS
916void ahci_command_set_offset(AHCICommand *cmd, uint64_t lba_sect)
917{
918 RegH2DFIS *fis = &(cmd->fis);
54d268b2
JS
919
920 if (cmd->props->atapi) {
921 ahci_atapi_command_set_offset(cmd, lba_sect);
922 return;
b682d3a7
JS
923 } else if (!cmd->props->data && !lba_sect) {
924 /* Not meaningful, ignore. */
925 return;
54d268b2 926 } else if (cmd->props->lba28) {
f9f963e0 927 g_assert_cmphex(lba_sect, <=, 0xFFFFFFF);
e38cc93a 928 } else if (cmd->props->lba48 || cmd->props->ncq) {
f9f963e0
JS
929 g_assert_cmphex(lba_sect, <=, 0xFFFFFFFFFFFF);
930 } else {
931 /* Can't set offset if we don't know the format. */
932 g_assert_not_reached();
933 }
934
935 /* LBA28 uses the low nibble of the device/control register for LBA24:27 */
936 fis->lba_lo[0] = (lba_sect & 0xFF);
937 fis->lba_lo[1] = (lba_sect >> 8) & 0xFF;
938 fis->lba_lo[2] = (lba_sect >> 16) & 0xFF;
939 if (cmd->props->lba28) {
455e861c 940 fis->device = (fis->device & 0xF0) | ((lba_sect >> 24) & 0x0F);
f9f963e0
JS
941 }
942 fis->lba_hi[0] = (lba_sect >> 24) & 0xFF;
943 fis->lba_hi[1] = (lba_sect >> 32) & 0xFF;
944 fis->lba_hi[2] = (lba_sect >> 40) & 0xFF;
945}
946
64a5a272
JS
947void ahci_command_set_buffer(AHCICommand *cmd, uint64_t buffer)
948{
949 cmd->buffer = buffer;
950}
951
54d268b2
JS
952static void ahci_atapi_set_size(AHCICommand *cmd, uint64_t xbytes)
953{
954 unsigned char *cbd = cmd->atapi_cmd;
955 uint64_t nsectors = xbytes / 2048;
ebde93bf 956 uint32_t tmp;
54d268b2
JS
957 g_assert(cbd);
958
959 switch (cbd[0]) {
960 case CMD_ATAPI_READ_10:
961 g_assert_cmpuint(nsectors, <=, UINT16_MAX);
962 stw_be_p(&cbd[7], nsectors);
963 break;
ebde93bf
JS
964 case CMD_ATAPI_READ_CD:
965 /* 24bit BE store */
966 g_assert_cmpuint(nsectors, <, 1ULL << 24);
967 tmp = nsectors;
968 cbd[6] = (tmp & 0xFF0000) >> 16;
969 cbd[7] = (tmp & 0xFF00) >> 8;
970 cbd[8] = (tmp & 0xFF);
971 break;
54d268b2
JS
972 default:
973 /* SCSI doesn't have uniform packet formats,
974 * so you have to add support for it manually. Sorry! */
ebde93bf
JS
975 fprintf(stderr, "The Libqos AHCI driver does not support the set_size "
976 "operation for ATAPI command 0x%02x, please add support.\n",
977 cbd[0]);
54d268b2
JS
978 g_assert_not_reached();
979 }
980}
981
cbc97569
JS
982void ahci_command_set_sizes(AHCICommand *cmd, uint64_t xbytes,
983 unsigned prd_size)
984{
cb453041
JS
985 uint16_t sect_count;
986
cbc97569
JS
987 /* Each PRD can describe up to 4MiB, and must not be odd. */
988 g_assert_cmphex(prd_size, <=, 4096 * 1024);
989 g_assert_cmphex(prd_size & 0x01, ==, 0x00);
455e861c
JS
990 if (prd_size) {
991 cmd->prd_size = prd_size;
992 }
cbc97569 993 cmd->xbytes = xbytes;
cb453041
JS
994 sect_count = (cmd->xbytes / AHCI_SECTOR_SIZE);
995
996 if (cmd->props->ncq) {
997 NCQFIS *nfis = (NCQFIS *)&(cmd->fis);
998 nfis->sector_low = sect_count & 0xFF;
999 nfis->sector_hi = (sect_count >> 8) & 0xFF;
54d268b2
JS
1000 } else if (cmd->props->atapi) {
1001 ahci_atapi_set_size(cmd, xbytes);
cb453041
JS
1002 } else {
1003 cmd->fis.count = sect_count;
1004 }
cbc97569
JS
1005 cmd->header.prdtl = size_to_prdtl(cmd->xbytes, cmd->prd_size);
1006}
1007
1008void ahci_command_set_size(AHCICommand *cmd, uint64_t xbytes)
1009{
1010 ahci_command_set_sizes(cmd, xbytes, cmd->prd_size);
1011}
1012
1013void ahci_command_set_prd_size(AHCICommand *cmd, unsigned prd_size)
1014{
1015 ahci_command_set_sizes(cmd, cmd->xbytes, prd_size);
1016}
1017
f9f963e0
JS
1018void ahci_command_adjust(AHCICommand *cmd, uint64_t offset, uint64_t buffer,
1019 uint64_t xbytes, unsigned prd_size)
1020{
1021 ahci_command_set_sizes(cmd, xbytes, prd_size);
1022 ahci_command_set_buffer(cmd, buffer);
1023 ahci_command_set_offset(cmd, offset);
1024}
1025
64a5a272
JS
1026void ahci_command_commit(AHCIQState *ahci, AHCICommand *cmd, uint8_t port)
1027{
1028 uint16_t i, prdtl;
1029 uint64_t table_size, table_ptr, remaining;
1030 PRD prd;
1031
1032 /* This command is now tied to this port/command slot */
1033 cmd->port = port;
1034 cmd->slot = ahci_pick_cmd(ahci, port);
1035
a8973ff5
JS
1036 if (cmd->props->ncq) {
1037 NCQFIS *nfis = (NCQFIS *)&cmd->fis;
1038 nfis->tag = (cmd->slot << 3) & 0xFC;
1039 }
1040
64a5a272
JS
1041 /* Create a buffer for the command table */
1042 prdtl = size_to_prdtl(cmd->xbytes, cmd->prd_size);
1043 table_size = CMD_TBL_SIZ(prdtl);
1044 table_ptr = ahci_alloc(ahci, table_size);
1045 g_assert(table_ptr);
1046 /* AHCI 1.3: Must be aligned to 0x80 */
1047 g_assert((table_ptr & 0x7F) == 0x00);
1048 cmd->header.ctba = table_ptr;
1049
54d268b2 1050 /* Commit the command header (part of the Command List Buffer) */
64a5a272 1051 ahci_set_command_header(ahci, port, cmd->slot, &(cmd->header));
54d268b2 1052 /* Now, write the command table (FIS, ACMD, and PRDT) -- FIS first, */
9ab9993f 1053 ahci_write_fis(ahci, cmd);
54d268b2
JS
1054 /* Then ATAPI CMD, if needed */
1055 if (cmd->props->atapi) {
1056 memwrite(table_ptr + 0x40, cmd->atapi_cmd, 16);
1057 }
64a5a272
JS
1058
1059 /* Construct and write the PRDs to the command table */
1060 g_assert_cmphex(prdtl, ==, cmd->header.prdtl);
1061 remaining = cmd->xbytes;
1062 for (i = 0; i < prdtl; ++i) {
1063 prd.dba = cpu_to_le64(cmd->buffer + (cmd->prd_size * i));
1064 prd.res = 0;
1065 if (remaining > cmd->prd_size) {
1066 /* Note that byte count is 0-based. */
1067 prd.dbc = cpu_to_le32(cmd->prd_size - 1);
1068 remaining -= cmd->prd_size;
1069 } else {
1070 /* Again, dbc is 0-based. */
1071 prd.dbc = cpu_to_le32(remaining - 1);
1072 remaining = 0;
1073 }
1074 prd.dbc |= cpu_to_le32(0x80000000); /* Request DPS Interrupt */
1075
1076 /* Commit the PRD entry to the Command Table */
1077 memwrite(table_ptr + 0x80 + (i * sizeof(PRD)),
1078 &prd, sizeof(PRD));
1079 }
1080
1081 /* Bookmark the PRDTL and CTBA values */
1082 ahci->port[port].ctba[cmd->slot] = table_ptr;
1083 ahci->port[port].prdtl[cmd->slot] = prdtl;
1084}
1085
1086void ahci_command_issue_async(AHCIQState *ahci, AHCICommand *cmd)
1087{
1088 if (cmd->props->ncq) {
1089 ahci_px_wreg(ahci, cmd->port, AHCI_PX_SACT, (1 << cmd->slot));
1090 }
1091
1092 ahci_px_wreg(ahci, cmd->port, AHCI_PX_CI, (1 << cmd->slot));
1093}
1094
1095void ahci_command_wait(AHCIQState *ahci, AHCICommand *cmd)
1096{
1097 /* We can't rely on STS_BSY until the command has started processing.
1098 * Therefore, we also use the Command Issue bit as indication of
1099 * a command in-flight. */
4de48469
JS
1100
1101#define RSET(REG, MASK) (BITSET(ahci_px_rreg(ahci, cmd->port, (REG)), (MASK)))
1102
1103 while (RSET(AHCI_PX_TFD, AHCI_PX_TFD_STS_BSY) ||
1104 RSET(AHCI_PX_CI, 1 << cmd->slot) ||
1105 (cmd->props->ncq && RSET(AHCI_PX_SACT, 1 << cmd->slot))) {
64a5a272
JS
1106 usleep(50);
1107 }
4de48469 1108
64a5a272
JS
1109}
1110
1111void ahci_command_issue(AHCIQState *ahci, AHCICommand *cmd)
1112{
1113 ahci_command_issue_async(ahci, cmd);
1114 ahci_command_wait(ahci, cmd);
1115}
1116
ea41deb6
JS
1117void ahci_command_verify(AHCIQState *ahci, AHCICommand *cmd)
1118{
1119 uint8_t slot = cmd->slot;
1120 uint8_t port = cmd->port;
1121
1122 ahci_port_check_error(ahci, port);
1123 ahci_port_check_interrupts(ahci, port, cmd->interrupts);
1124 ahci_port_check_nonbusy(ahci, port, slot);
40d29928 1125 ahci_port_check_cmd_sanity(ahci, cmd);
359790c2
JS
1126 if (cmd->interrupts & AHCI_PX_IS_DHRS) {
1127 ahci_port_check_d2h_sanity(ahci, port, slot);
1128 }
ea41deb6
JS
1129 if (cmd->props->pio) {
1130 ahci_port_check_pio_sanity(ahci, port, slot, cmd->xbytes);
1131 }
1132}
1133
64a5a272
JS
1134uint8_t ahci_command_slot(AHCICommand *cmd)
1135{
1136 return cmd->slot;
1137}
This page took 0.29611 seconds and 4 git commands to generate.