Merge remote-tracking branch 'remotes/bkoppelmann/tags/pull-tricore-20150330' into...
[qemu.git] / tests / libqos / ahci.c
blobb0f39a5e328b478afda5d888c03a2719cd1ad19d
1 /*
2 * libqos AHCI functions
4 * Copyright (c) 2014 John Snow <jsnow@redhat.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include <glib.h>
27 #include "libqtest.h"
28 #include "libqos/ahci.h"
29 #include "libqos/pci-pc.h"
31 #include "qemu-common.h"
32 #include "qemu/host-utils.h"
34 #include "hw/pci/pci_ids.h"
35 #include "hw/pci/pci_regs.h"
37 typedef 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;
52 AHCICommandProp ahci_command_properties[] = {
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 = CMD_READ_MAX, .lba28 = true },
72 { .cmd = CMD_READ_MAX_EXT, .lba48 = true },
73 { .cmd = CMD_FLUSH_CACHE, .data = false }
76 /**
77 * Allocate space in the guest using information in the AHCIQState object.
79 uint64_t ahci_alloc(AHCIQState *ahci, size_t bytes)
81 g_assert(ahci);
82 g_assert(ahci->parent);
83 return qmalloc(ahci->parent, bytes);
86 void ahci_free(AHCIQState *ahci, uint64_t addr)
88 g_assert(ahci);
89 g_assert(ahci->parent);
90 qfree(ahci->parent, addr);
93 /**
94 * Locate, verify, and return a handle to the AHCI device.
96 QPCIDevice *get_ahci_device(uint32_t *fingerprint)
98 QPCIDevice *ahci;
99 uint32_t ahci_fingerprint;
100 QPCIBus *pcibus;
102 pcibus = qpci_init_pc();
104 /* Find the AHCI PCI device and verify it's the right one. */
105 ahci = qpci_device_find(pcibus, QPCI_DEVFN(0x1F, 0x02));
106 g_assert(ahci != NULL);
108 ahci_fingerprint = qpci_config_readl(ahci, PCI_VENDOR_ID);
110 switch (ahci_fingerprint) {
111 case AHCI_INTEL_ICH9:
112 break;
113 default:
114 /* Unknown device. */
115 g_assert_not_reached();
118 if (fingerprint) {
119 *fingerprint = ahci_fingerprint;
121 return ahci;
124 void free_ahci_device(QPCIDevice *dev)
126 QPCIBus *pcibus = dev ? dev->bus : NULL;
128 /* libqos doesn't have a function for this, so free it manually */
129 g_free(dev);
130 qpci_free_pc(pcibus);
133 /* Free all memory in-use by the AHCI device. */
134 void ahci_clean_mem(AHCIQState *ahci)
136 uint8_t port, slot;
138 for (port = 0; port < 32; ++port) {
139 if (ahci->port[port].fb) {
140 ahci_free(ahci, ahci->port[port].fb);
142 if (ahci->port[port].clb) {
143 for (slot = 0; slot < 32; slot++) {
144 ahci_destroy_command(ahci, port, slot);
146 ahci_free(ahci, ahci->port[port].clb);
151 /*** Logical Device Initialization ***/
154 * Start the PCI device and sanity-check default operation.
156 void ahci_pci_enable(AHCIQState *ahci)
158 uint8_t reg;
160 start_ahci_device(ahci);
162 switch (ahci->fingerprint) {
163 case AHCI_INTEL_ICH9:
164 /* ICH9 has a register at PCI 0x92 that
165 * acts as a master port enabler mask. */
166 reg = qpci_config_readb(ahci->dev, 0x92);
167 reg |= 0x3F;
168 qpci_config_writeb(ahci->dev, 0x92, reg);
169 /* 0...0111111b -- bit significant, ports 0-5 enabled. */
170 ASSERT_BIT_SET(qpci_config_readb(ahci->dev, 0x92), 0x3F);
171 break;
177 * Map BAR5/ABAR, and engage the PCI device.
179 void start_ahci_device(AHCIQState *ahci)
181 /* Map AHCI's ABAR (BAR5) */
182 ahci->hba_base = qpci_iomap(ahci->dev, 5, &ahci->barsize);
183 g_assert(ahci->hba_base);
185 /* turns on pci.cmd.iose, pci.cmd.mse and pci.cmd.bme */
186 qpci_device_enable(ahci->dev);
190 * Test and initialize the AHCI's HBA memory areas.
191 * Initialize and start any ports with devices attached.
192 * Bring the HBA into the idle state.
194 void ahci_hba_enable(AHCIQState *ahci)
196 /* Bits of interest in this section:
197 * GHC.AE Global Host Control / AHCI Enable
198 * PxCMD.ST Port Command: Start
199 * PxCMD.SUD "Spin Up Device"
200 * PxCMD.POD "Power On Device"
201 * PxCMD.FRE "FIS Receive Enable"
202 * PxCMD.FR "FIS Receive Running"
203 * PxCMD.CR "Command List Running"
205 uint32_t reg, ports_impl;
206 uint16_t i;
207 uint8_t num_cmd_slots;
209 g_assert(ahci != NULL);
211 /* Set GHC.AE to 1 */
212 ahci_set(ahci, AHCI_GHC, AHCI_GHC_AE);
213 reg = ahci_rreg(ahci, AHCI_GHC);
214 ASSERT_BIT_SET(reg, AHCI_GHC_AE);
216 /* Cache CAP and CAP2. */
217 ahci->cap = ahci_rreg(ahci, AHCI_CAP);
218 ahci->cap2 = ahci_rreg(ahci, AHCI_CAP2);
220 /* Read CAP.NCS, how many command slots do we have? */
221 num_cmd_slots = ((ahci->cap & AHCI_CAP_NCS) >> ctzl(AHCI_CAP_NCS)) + 1;
222 g_test_message("Number of Command Slots: %u", num_cmd_slots);
224 /* Determine which ports are implemented. */
225 ports_impl = ahci_rreg(ahci, AHCI_PI);
227 for (i = 0; ports_impl; ports_impl >>= 1, ++i) {
228 if (!(ports_impl & 0x01)) {
229 continue;
232 g_test_message("Initializing port %u", i);
234 reg = ahci_px_rreg(ahci, i, AHCI_PX_CMD);
235 if (BITCLR(reg, AHCI_PX_CMD_ST | AHCI_PX_CMD_CR |
236 AHCI_PX_CMD_FRE | AHCI_PX_CMD_FR)) {
237 g_test_message("port is idle");
238 } else {
239 g_test_message("port needs to be idled");
240 ahci_px_clr(ahci, i, AHCI_PX_CMD,
241 (AHCI_PX_CMD_ST | AHCI_PX_CMD_FRE));
242 /* The port has 500ms to disengage. */
243 usleep(500000);
244 reg = ahci_px_rreg(ahci, i, AHCI_PX_CMD);
245 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CR);
246 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FR);
247 g_test_message("port is now idle");
248 /* The spec does allow for possibly needing a PORT RESET
249 * or HBA reset if we fail to idle the port. */
252 /* Allocate Memory for the Command List Buffer & FIS Buffer */
253 /* PxCLB space ... 0x20 per command, as in 4.2.2 p 36 */
254 ahci->port[i].clb = ahci_alloc(ahci, num_cmd_slots * 0x20);
255 qmemset(ahci->port[i].clb, 0x00, 0x100);
256 g_test_message("CLB: 0x%08" PRIx64, ahci->port[i].clb);
257 ahci_px_wreg(ahci, i, AHCI_PX_CLB, ahci->port[i].clb);
258 g_assert_cmphex(ahci->port[i].clb, ==,
259 ahci_px_rreg(ahci, i, AHCI_PX_CLB));
261 /* PxFB space ... 0x100, as in 4.2.1 p 35 */
262 ahci->port[i].fb = ahci_alloc(ahci, 0x100);
263 qmemset(ahci->port[i].fb, 0x00, 0x100);
264 g_test_message("FB: 0x%08" PRIx64, ahci->port[i].fb);
265 ahci_px_wreg(ahci, i, AHCI_PX_FB, ahci->port[i].fb);
266 g_assert_cmphex(ahci->port[i].fb, ==,
267 ahci_px_rreg(ahci, i, AHCI_PX_FB));
269 /* Clear PxSERR, PxIS, then IS.IPS[x] by writing '1's. */
270 ahci_px_wreg(ahci, i, AHCI_PX_SERR, 0xFFFFFFFF);
271 ahci_px_wreg(ahci, i, AHCI_PX_IS, 0xFFFFFFFF);
272 ahci_wreg(ahci, AHCI_IS, (1 << i));
274 /* Verify Interrupts Cleared */
275 reg = ahci_px_rreg(ahci, i, AHCI_PX_SERR);
276 g_assert_cmphex(reg, ==, 0);
278 reg = ahci_px_rreg(ahci, i, AHCI_PX_IS);
279 g_assert_cmphex(reg, ==, 0);
281 reg = ahci_rreg(ahci, AHCI_IS);
282 ASSERT_BIT_CLEAR(reg, (1 << i));
284 /* Enable All Interrupts: */
285 ahci_px_wreg(ahci, i, AHCI_PX_IE, 0xFFFFFFFF);
286 reg = ahci_px_rreg(ahci, i, AHCI_PX_IE);
287 g_assert_cmphex(reg, ==, ~((uint32_t)AHCI_PX_IE_RESERVED));
289 /* Enable the FIS Receive Engine. */
290 ahci_px_set(ahci, i, AHCI_PX_CMD, AHCI_PX_CMD_FRE);
291 reg = ahci_px_rreg(ahci, i, AHCI_PX_CMD);
292 ASSERT_BIT_SET(reg, AHCI_PX_CMD_FR);
294 /* AHCI 1.3 spec: if !STS.BSY, !STS.DRQ and PxSSTS.DET indicates
295 * physical presence, a device is present and may be started. However,
296 * PxSERR.DIAG.X /may/ need to be cleared a priori. */
297 reg = ahci_px_rreg(ahci, i, AHCI_PX_SERR);
298 if (BITSET(reg, AHCI_PX_SERR_DIAG_X)) {
299 ahci_px_set(ahci, i, AHCI_PX_SERR, AHCI_PX_SERR_DIAG_X);
302 reg = ahci_px_rreg(ahci, i, AHCI_PX_TFD);
303 if (BITCLR(reg, AHCI_PX_TFD_STS_BSY | AHCI_PX_TFD_STS_DRQ)) {
304 reg = ahci_px_rreg(ahci, i, AHCI_PX_SSTS);
305 if ((reg & AHCI_PX_SSTS_DET) == SSTS_DET_ESTABLISHED) {
306 /* Device Found: set PxCMD.ST := 1 */
307 ahci_px_set(ahci, i, AHCI_PX_CMD, AHCI_PX_CMD_ST);
308 ASSERT_BIT_SET(ahci_px_rreg(ahci, i, AHCI_PX_CMD),
309 AHCI_PX_CMD_CR);
310 g_test_message("Started Device %u", i);
311 } else if ((reg & AHCI_PX_SSTS_DET)) {
312 /* Device present, but in some unknown state. */
313 g_assert_not_reached();
318 /* Enable GHC.IE */
319 ahci_set(ahci, AHCI_GHC, AHCI_GHC_IE);
320 reg = ahci_rreg(ahci, AHCI_GHC);
321 ASSERT_BIT_SET(reg, AHCI_GHC_IE);
323 /* TODO: The device should now be idling and waiting for commands.
324 * In the future, a small test-case to inspect the Register D2H FIS
325 * and clear the initial interrupts might be good. */
329 * Pick the first implemented and running port
331 unsigned ahci_port_select(AHCIQState *ahci)
333 uint32_t ports, reg;
334 unsigned i;
336 ports = ahci_rreg(ahci, AHCI_PI);
337 for (i = 0; i < 32; ports >>= 1, ++i) {
338 if (ports == 0) {
339 i = 32;
342 if (!(ports & 0x01)) {
343 continue;
346 reg = ahci_px_rreg(ahci, i, AHCI_PX_CMD);
347 if (BITSET(reg, AHCI_PX_CMD_ST)) {
348 break;
351 g_assert(i < 32);
352 return i;
356 * Clear a port's interrupts and status information prior to a test.
358 void ahci_port_clear(AHCIQState *ahci, uint8_t port)
360 uint32_t reg;
362 /* Clear out this port's interrupts (ignore the init register d2h fis) */
363 reg = ahci_px_rreg(ahci, port, AHCI_PX_IS);
364 ahci_px_wreg(ahci, port, AHCI_PX_IS, reg);
365 g_assert_cmphex(ahci_px_rreg(ahci, port, AHCI_PX_IS), ==, 0);
367 /* Wipe the FIS-Recieve Buffer */
368 qmemset(ahci->port[port].fb, 0x00, 0x100);
372 * Check a port for errors.
374 void ahci_port_check_error(AHCIQState *ahci, uint8_t port)
376 uint32_t reg;
378 /* The upper 9 bits of the IS register all indicate errors. */
379 reg = ahci_px_rreg(ahci, port, AHCI_PX_IS);
380 reg >>= 23;
381 g_assert_cmphex(reg, ==, 0);
383 /* The Sata Error Register should be empty. */
384 reg = ahci_px_rreg(ahci, port, AHCI_PX_SERR);
385 g_assert_cmphex(reg, ==, 0);
387 /* The TFD also has two error sections. */
388 reg = ahci_px_rreg(ahci, port, AHCI_PX_TFD);
389 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_ERR);
390 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_ERR);
393 void ahci_port_check_interrupts(AHCIQState *ahci, uint8_t port,
394 uint32_t intr_mask)
396 uint32_t reg;
398 /* Check for expected interrupts */
399 reg = ahci_px_rreg(ahci, port, AHCI_PX_IS);
400 ASSERT_BIT_SET(reg, intr_mask);
402 /* Clear expected interrupts and assert all interrupts now cleared. */
403 ahci_px_wreg(ahci, port, AHCI_PX_IS, intr_mask);
404 g_assert_cmphex(ahci_px_rreg(ahci, port, AHCI_PX_IS), ==, 0);
407 void ahci_port_check_nonbusy(AHCIQState *ahci, uint8_t port, uint8_t slot)
409 uint32_t reg;
411 /* Assert that the command slot is no longer busy (NCQ) */
412 reg = ahci_px_rreg(ahci, port, AHCI_PX_SACT);
413 ASSERT_BIT_CLEAR(reg, (1 << slot));
415 /* Non-NCQ */
416 reg = ahci_px_rreg(ahci, port, AHCI_PX_CI);
417 ASSERT_BIT_CLEAR(reg, (1 << slot));
419 /* And assert that we are generally not busy. */
420 reg = ahci_px_rreg(ahci, port, AHCI_PX_TFD);
421 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_BSY);
422 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_DRQ);
425 void ahci_port_check_d2h_sanity(AHCIQState *ahci, uint8_t port, uint8_t slot)
427 RegD2HFIS *d2h = g_malloc0(0x20);
428 uint32_t reg;
430 memread(ahci->port[port].fb + 0x40, d2h, 0x20);
431 g_assert_cmphex(d2h->fis_type, ==, 0x34);
433 reg = ahci_px_rreg(ahci, port, AHCI_PX_TFD);
434 g_assert_cmphex((reg & AHCI_PX_TFD_ERR) >> 8, ==, d2h->error);
435 g_assert_cmphex((reg & AHCI_PX_TFD_STS), ==, d2h->status);
437 g_free(d2h);
440 void ahci_port_check_pio_sanity(AHCIQState *ahci, uint8_t port,
441 uint8_t slot, size_t buffsize)
443 PIOSetupFIS *pio = g_malloc0(0x20);
445 /* We cannot check the Status or E_Status registers, becuase
446 * the status may have again changed between the PIO Setup FIS
447 * and the conclusion of the command with the D2H Register FIS. */
448 memread(ahci->port[port].fb + 0x20, pio, 0x20);
449 g_assert_cmphex(pio->fis_type, ==, 0x5f);
451 /* BUG: PIO Setup FIS as utilized by QEMU tries to fit the entire
452 * transfer size in a uint16_t field. The maximum transfer size can
453 * eclipse this; the field is meant to convey the size of data per
454 * each Data FIS, not the entire operation as a whole. For now,
455 * we will sanity check the broken case where applicable. */
456 if (buffsize <= UINT16_MAX) {
457 g_assert_cmphex(le16_to_cpu(pio->tx_count), ==, buffsize);
460 g_free(pio);
463 void ahci_port_check_cmd_sanity(AHCIQState *ahci, uint8_t port,
464 uint8_t slot, size_t buffsize)
466 AHCICommandHeader cmd;
468 ahci_get_command_header(ahci, port, slot, &cmd);
469 g_assert_cmphex(buffsize, ==, cmd.prdbc);
472 /* Get the command in #slot of port #port. */
473 void ahci_get_command_header(AHCIQState *ahci, uint8_t port,
474 uint8_t slot, AHCICommandHeader *cmd)
476 uint64_t ba = ahci->port[port].clb;
477 ba += slot * sizeof(AHCICommandHeader);
478 memread(ba, cmd, sizeof(AHCICommandHeader));
480 cmd->flags = le16_to_cpu(cmd->flags);
481 cmd->prdtl = le16_to_cpu(cmd->prdtl);
482 cmd->prdbc = le32_to_cpu(cmd->prdbc);
483 cmd->ctba = le64_to_cpu(cmd->ctba);
486 /* Set the command in #slot of port #port. */
487 void ahci_set_command_header(AHCIQState *ahci, uint8_t port,
488 uint8_t slot, AHCICommandHeader *cmd)
490 AHCICommandHeader tmp = { .flags = 0 };
491 uint64_t ba = ahci->port[port].clb;
492 ba += slot * sizeof(AHCICommandHeader);
494 tmp.flags = cpu_to_le16(cmd->flags);
495 tmp.prdtl = cpu_to_le16(cmd->prdtl);
496 tmp.prdbc = cpu_to_le32(cmd->prdbc);
497 tmp.ctba = cpu_to_le64(cmd->ctba);
499 memwrite(ba, &tmp, sizeof(AHCICommandHeader));
502 void ahci_destroy_command(AHCIQState *ahci, uint8_t port, uint8_t slot)
504 AHCICommandHeader cmd;
506 /* Obtain the Nth Command Header */
507 ahci_get_command_header(ahci, port, slot, &cmd);
508 if (cmd.ctba == 0) {
509 /* No address in it, so just return -- it's empty. */
510 goto tidy;
513 /* Free the Table */
514 ahci_free(ahci, cmd.ctba);
516 tidy:
517 /* NULL the header. */
518 memset(&cmd, 0x00, sizeof(cmd));
519 ahci_set_command_header(ahci, port, slot, &cmd);
520 ahci->port[port].ctba[slot] = 0;
521 ahci->port[port].prdtl[slot] = 0;
524 void ahci_write_fis(AHCIQState *ahci, RegH2DFIS *fis, uint64_t addr)
526 RegH2DFIS tmp = *fis;
528 /* The auxiliary FIS fields are defined per-command and are not
529 * currently implemented in libqos/ahci.o, but may or may not need
530 * to be flipped. */
532 /* All other FIS fields are 8 bit and do not need to be flipped. */
533 tmp.count = cpu_to_le16(tmp.count);
535 memwrite(addr, &tmp, sizeof(tmp));
538 unsigned ahci_pick_cmd(AHCIQState *ahci, uint8_t port)
540 unsigned i;
541 unsigned j;
542 uint32_t reg;
544 reg = ahci_px_rreg(ahci, port, AHCI_PX_CI);
546 /* Pick the least recently used command slot that's available */
547 for (i = 0; i < 32; ++i) {
548 j = ((ahci->port[port].next + i) % 32);
549 if (reg & (1 << j)) {
550 continue;
552 ahci_destroy_command(ahci, port, i);
553 ahci->port[port].next = (j + 1) % 32;
554 return j;
557 g_test_message("All command slots were busy.");
558 g_assert_not_reached();
561 inline unsigned size_to_prdtl(unsigned bytes, unsigned bytes_per_prd)
563 /* Each PRD can describe up to 4MiB */
564 g_assert_cmphex(bytes_per_prd, <=, 4096 * 1024);
565 g_assert_cmphex(bytes_per_prd & 0x01, ==, 0x00);
566 return (bytes + bytes_per_prd - 1) / bytes_per_prd;
569 /* Given a guest buffer address, perform an IO operation */
570 void ahci_guest_io(AHCIQState *ahci, uint8_t port, uint8_t ide_cmd,
571 uint64_t buffer, size_t bufsize)
573 AHCICommand *cmd;
575 cmd = ahci_command_create(ide_cmd);
576 ahci_command_set_buffer(cmd, buffer);
577 ahci_command_set_size(cmd, bufsize);
578 ahci_command_commit(ahci, cmd, port);
579 ahci_command_issue(ahci, cmd);
580 ahci_command_verify(ahci, cmd);
581 ahci_command_free(cmd);
584 struct AHCICommand {
585 /* Test Management Data */
586 uint8_t name;
587 uint8_t port;
588 uint8_t slot;
589 uint32_t interrupts;
590 uint64_t xbytes;
591 uint32_t prd_size;
592 uint64_t buffer;
593 AHCICommandProp *props;
594 /* Data to be transferred to the guest */
595 AHCICommandHeader header;
596 RegH2DFIS fis;
597 void *atapi_cmd;
600 static AHCICommandProp *ahci_command_find(uint8_t command_name)
602 int i;
604 for (i = 0; i < ARRAY_SIZE(ahci_command_properties); i++) {
605 if (ahci_command_properties[i].cmd == command_name) {
606 return &ahci_command_properties[i];
610 return NULL;
613 /* Given a HOST buffer, create a buffer address and perform an IO operation. */
614 void ahci_io(AHCIQState *ahci, uint8_t port, uint8_t ide_cmd,
615 void *buffer, size_t bufsize)
617 uint64_t ptr;
618 AHCICommandProp *props;
620 props = ahci_command_find(ide_cmd);
621 g_assert(props);
622 ptr = ahci_alloc(ahci, bufsize);
623 g_assert(ptr);
625 if (props->write) {
626 memwrite(ptr, buffer, bufsize);
629 ahci_guest_io(ahci, port, ide_cmd, ptr, bufsize);
631 if (props->read) {
632 memread(ptr, buffer, bufsize);
635 ahci_free(ahci, ptr);
639 * Initializes a basic command header in memory.
640 * We assume that this is for an ATA command using RegH2DFIS.
642 static void command_header_init(AHCICommand *cmd)
644 AHCICommandHeader *hdr = &cmd->header;
645 AHCICommandProp *props = cmd->props;
647 hdr->flags = 5; /* RegH2DFIS is 5 DW long. Must be < 32 */
648 hdr->flags |= CMDH_CLR_BSY; /* Clear the BSY bit when done */
649 if (props->write) {
650 hdr->flags |= CMDH_WRITE;
652 if (props->atapi) {
653 hdr->flags |= CMDH_ATAPI;
655 /* Other flags: PREFETCH, RESET, and BIST */
656 hdr->prdtl = size_to_prdtl(cmd->xbytes, cmd->prd_size);
657 hdr->prdbc = 0;
658 hdr->ctba = 0;
661 static void command_table_init(AHCICommand *cmd)
663 RegH2DFIS *fis = &(cmd->fis);
665 fis->fis_type = REG_H2D_FIS;
666 fis->flags = REG_H2D_FIS_CMD; /* "Command" bit */
667 fis->command = cmd->name;
668 cmd->fis.feature_low = 0x00;
669 cmd->fis.feature_high = 0x00;
670 if (cmd->props->lba28 || cmd->props->lba48) {
671 cmd->fis.device = ATA_DEVICE_LBA;
673 cmd->fis.count = (cmd->xbytes / AHCI_SECTOR_SIZE);
674 cmd->fis.icc = 0x00;
675 cmd->fis.control = 0x00;
676 memset(cmd->fis.aux, 0x00, ARRAY_SIZE(cmd->fis.aux));
679 AHCICommand *ahci_command_create(uint8_t command_name)
681 AHCICommandProp *props = ahci_command_find(command_name);
682 AHCICommand *cmd;
684 g_assert(props);
685 cmd = g_malloc0(sizeof(AHCICommand));
686 g_assert(!(props->dma && props->pio));
687 g_assert(!(props->lba28 && props->lba48));
688 g_assert(!(props->read && props->write));
689 g_assert(!props->size || props->data);
691 /* Defaults and book-keeping */
692 cmd->props = props;
693 cmd->name = command_name;
694 cmd->xbytes = props->size;
695 cmd->prd_size = 4096;
696 cmd->buffer = 0xabad1dea;
698 cmd->interrupts = AHCI_PX_IS_DHRS;
699 /* BUG: We expect the DPS interrupt for data commands */
700 /* cmd->interrupts |= props->data ? AHCI_PX_IS_DPS : 0; */
701 /* BUG: We expect the DMA Setup interrupt for DMA commands */
702 /* cmd->interrupts |= props->dma ? AHCI_PX_IS_DSS : 0; */
703 cmd->interrupts |= props->pio ? AHCI_PX_IS_PSS : 0;
705 command_header_init(cmd);
706 command_table_init(cmd);
708 return cmd;
711 void ahci_command_free(AHCICommand *cmd)
713 g_free(cmd);
716 void ahci_command_set_flags(AHCICommand *cmd, uint16_t cmdh_flags)
718 cmd->header.flags |= cmdh_flags;
721 void ahci_command_clr_flags(AHCICommand *cmd, uint16_t cmdh_flags)
723 cmd->header.flags &= ~cmdh_flags;
726 void ahci_command_set_offset(AHCICommand *cmd, uint64_t lba_sect)
728 RegH2DFIS *fis = &(cmd->fis);
729 if (cmd->props->lba28) {
730 g_assert_cmphex(lba_sect, <=, 0xFFFFFFF);
731 } else if (cmd->props->lba48) {
732 g_assert_cmphex(lba_sect, <=, 0xFFFFFFFFFFFF);
733 } else {
734 /* Can't set offset if we don't know the format. */
735 g_assert_not_reached();
738 /* LBA28 uses the low nibble of the device/control register for LBA24:27 */
739 fis->lba_lo[0] = (lba_sect & 0xFF);
740 fis->lba_lo[1] = (lba_sect >> 8) & 0xFF;
741 fis->lba_lo[2] = (lba_sect >> 16) & 0xFF;
742 if (cmd->props->lba28) {
743 fis->device = (fis->device & 0xF0) || (lba_sect >> 24) & 0x0F;
745 fis->lba_hi[0] = (lba_sect >> 24) & 0xFF;
746 fis->lba_hi[1] = (lba_sect >> 32) & 0xFF;
747 fis->lba_hi[2] = (lba_sect >> 40) & 0xFF;
750 void ahci_command_set_buffer(AHCICommand *cmd, uint64_t buffer)
752 cmd->buffer = buffer;
755 void ahci_command_set_sizes(AHCICommand *cmd, uint64_t xbytes,
756 unsigned prd_size)
758 /* Each PRD can describe up to 4MiB, and must not be odd. */
759 g_assert_cmphex(prd_size, <=, 4096 * 1024);
760 g_assert_cmphex(prd_size & 0x01, ==, 0x00);
761 cmd->prd_size = prd_size;
762 cmd->xbytes = xbytes;
763 cmd->fis.count = (cmd->xbytes / AHCI_SECTOR_SIZE);
764 cmd->header.prdtl = size_to_prdtl(cmd->xbytes, cmd->prd_size);
767 void ahci_command_set_size(AHCICommand *cmd, uint64_t xbytes)
769 ahci_command_set_sizes(cmd, xbytes, cmd->prd_size);
772 void ahci_command_set_prd_size(AHCICommand *cmd, unsigned prd_size)
774 ahci_command_set_sizes(cmd, cmd->xbytes, prd_size);
777 void ahci_command_adjust(AHCICommand *cmd, uint64_t offset, uint64_t buffer,
778 uint64_t xbytes, unsigned prd_size)
780 ahci_command_set_sizes(cmd, xbytes, prd_size);
781 ahci_command_set_buffer(cmd, buffer);
782 ahci_command_set_offset(cmd, offset);
785 void ahci_command_commit(AHCIQState *ahci, AHCICommand *cmd, uint8_t port)
787 uint16_t i, prdtl;
788 uint64_t table_size, table_ptr, remaining;
789 PRD prd;
791 /* This command is now tied to this port/command slot */
792 cmd->port = port;
793 cmd->slot = ahci_pick_cmd(ahci, port);
795 /* Create a buffer for the command table */
796 prdtl = size_to_prdtl(cmd->xbytes, cmd->prd_size);
797 table_size = CMD_TBL_SIZ(prdtl);
798 table_ptr = ahci_alloc(ahci, table_size);
799 g_assert(table_ptr);
800 /* AHCI 1.3: Must be aligned to 0x80 */
801 g_assert((table_ptr & 0x7F) == 0x00);
802 cmd->header.ctba = table_ptr;
804 /* Commit the command header and command FIS */
805 ahci_set_command_header(ahci, port, cmd->slot, &(cmd->header));
806 ahci_write_fis(ahci, &(cmd->fis), table_ptr);
808 /* Construct and write the PRDs to the command table */
809 g_assert_cmphex(prdtl, ==, cmd->header.prdtl);
810 remaining = cmd->xbytes;
811 for (i = 0; i < prdtl; ++i) {
812 prd.dba = cpu_to_le64(cmd->buffer + (cmd->prd_size * i));
813 prd.res = 0;
814 if (remaining > cmd->prd_size) {
815 /* Note that byte count is 0-based. */
816 prd.dbc = cpu_to_le32(cmd->prd_size - 1);
817 remaining -= cmd->prd_size;
818 } else {
819 /* Again, dbc is 0-based. */
820 prd.dbc = cpu_to_le32(remaining - 1);
821 remaining = 0;
823 prd.dbc |= cpu_to_le32(0x80000000); /* Request DPS Interrupt */
825 /* Commit the PRD entry to the Command Table */
826 memwrite(table_ptr + 0x80 + (i * sizeof(PRD)),
827 &prd, sizeof(PRD));
830 /* Bookmark the PRDTL and CTBA values */
831 ahci->port[port].ctba[cmd->slot] = table_ptr;
832 ahci->port[port].prdtl[cmd->slot] = prdtl;
835 void ahci_command_issue_async(AHCIQState *ahci, AHCICommand *cmd)
837 if (cmd->props->ncq) {
838 ahci_px_wreg(ahci, cmd->port, AHCI_PX_SACT, (1 << cmd->slot));
841 ahci_px_wreg(ahci, cmd->port, AHCI_PX_CI, (1 << cmd->slot));
844 void ahci_command_wait(AHCIQState *ahci, AHCICommand *cmd)
846 /* We can't rely on STS_BSY until the command has started processing.
847 * Therefore, we also use the Command Issue bit as indication of
848 * a command in-flight. */
849 while (BITSET(ahci_px_rreg(ahci, cmd->port, AHCI_PX_TFD),
850 AHCI_PX_TFD_STS_BSY) ||
851 BITSET(ahci_px_rreg(ahci, cmd->port, AHCI_PX_CI), (1 << cmd->slot))) {
852 usleep(50);
856 void ahci_command_issue(AHCIQState *ahci, AHCICommand *cmd)
858 ahci_command_issue_async(ahci, cmd);
859 ahci_command_wait(ahci, cmd);
862 void ahci_command_verify(AHCIQState *ahci, AHCICommand *cmd)
864 uint8_t slot = cmd->slot;
865 uint8_t port = cmd->port;
867 ahci_port_check_error(ahci, port);
868 ahci_port_check_interrupts(ahci, port, cmd->interrupts);
869 ahci_port_check_nonbusy(ahci, port, slot);
870 ahci_port_check_cmd_sanity(ahci, port, slot, cmd->xbytes);
871 ahci_port_check_d2h_sanity(ahci, port, slot);
872 if (cmd->props->pio) {
873 ahci_port_check_pio_sanity(ahci, port, slot, cmd->xbytes);
877 uint8_t ahci_command_slot(AHCICommand *cmd)
879 return cmd->slot;