]> Git Repo - qemu.git/blame - tests/libqos/ahci.c
libqos/ahci: add ahci command helpers
[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
25#include <glib.h>
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[] = {
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 }
74};
75
9a75b0a0
JS
76/**
77 * Allocate space in the guest using information in the AHCIQState object.
78 */
79uint64_t ahci_alloc(AHCIQState *ahci, size_t bytes)
80{
81 g_assert(ahci);
82 g_assert(ahci->parent);
83 return qmalloc(ahci->parent, bytes);
84}
85
86void ahci_free(AHCIQState *ahci, uint64_t addr)
87{
88 g_assert(ahci);
89 g_assert(ahci->parent);
90 qfree(ahci->parent, addr);
91}
92
93/**
94 * Locate, verify, and return a handle to the AHCI device.
95 */
96QPCIDevice *get_ahci_device(uint32_t *fingerprint)
97{
98 QPCIDevice *ahci;
99 uint32_t ahci_fingerprint;
100 QPCIBus *pcibus;
101
102 pcibus = qpci_init_pc();
103
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);
107
108 ahci_fingerprint = qpci_config_readl(ahci, PCI_VENDOR_ID);
109
110 switch (ahci_fingerprint) {
111 case AHCI_INTEL_ICH9:
112 break;
113 default:
114 /* Unknown device. */
115 g_assert_not_reached();
116 }
117
118 if (fingerprint) {
119 *fingerprint = ahci_fingerprint;
120 }
121 return ahci;
122}
123
124void free_ahci_device(QPCIDevice *dev)
125{
126 QPCIBus *pcibus = dev ? dev->bus : NULL;
127
128 /* libqos doesn't have a function for this, so free it manually */
129 g_free(dev);
130 qpci_free_pc(pcibus);
131}
132
259342d3
JS
133/* Free all memory in-use by the AHCI device. */
134void ahci_clean_mem(AHCIQState *ahci)
135{
136 uint8_t port, slot;
137
138 for (port = 0; port < 32; ++port) {
139 if (ahci->port[port].fb) {
140 ahci_free(ahci, ahci->port[port].fb);
141 }
142 if (ahci->port[port].clb) {
143 for (slot = 0; slot < 32; slot++) {
144 ahci_destroy_command(ahci, port, slot);
145 }
146 ahci_free(ahci, ahci->port[port].clb);
147 }
148 }
149}
150
9a75b0a0
JS
151/*** Logical Device Initialization ***/
152
153/**
154 * Start the PCI device and sanity-check default operation.
155 */
156void ahci_pci_enable(AHCIQState *ahci)
157{
158 uint8_t reg;
159
160 start_ahci_device(ahci);
161
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;
172 }
173
174}
175
176/**
177 * Map BAR5/ABAR, and engage the PCI device.
178 */
179void start_ahci_device(AHCIQState *ahci)
180{
181 /* Map AHCI's ABAR (BAR5) */
182 ahci->hba_base = qpci_iomap(ahci->dev, 5, &ahci->barsize);
183 g_assert(ahci->hba_base);
184
185 /* turns on pci.cmd.iose, pci.cmd.mse and pci.cmd.bme */
186 qpci_device_enable(ahci->dev);
187}
188
189/**
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.
193 */
194void ahci_hba_enable(AHCIQState *ahci)
195{
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"
204 */
205 uint32_t reg, ports_impl;
206 uint16_t i;
207 uint8_t num_cmd_slots;
208
209 g_assert(ahci != NULL);
210
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);
215
216 /* Cache CAP and CAP2. */
217 ahci->cap = ahci_rreg(ahci, AHCI_CAP);
218 ahci->cap2 = ahci_rreg(ahci, AHCI_CAP2);
219
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);
223
224 /* Determine which ports are implemented. */
225 ports_impl = ahci_rreg(ahci, AHCI_PI);
226
227 for (i = 0; ports_impl; ports_impl >>= 1, ++i) {
228 if (!(ports_impl & 0x01)) {
229 continue;
230 }
231
232 g_test_message("Initializing port %u", i);
233
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. */
250 }
251
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));
260
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));
268
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));
273
274 /* Verify Interrupts Cleared */
275 reg = ahci_px_rreg(ahci, i, AHCI_PX_SERR);
276 g_assert_cmphex(reg, ==, 0);
277
278 reg = ahci_px_rreg(ahci, i, AHCI_PX_IS);
279 g_assert_cmphex(reg, ==, 0);
280
281 reg = ahci_rreg(ahci, AHCI_IS);
282 ASSERT_BIT_CLEAR(reg, (1 << i));
283
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));
288
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);
293
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);
300 }
301
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();
314 }
315 }
316 }
317
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);
322
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. */
326}
e77448a3
JS
327
328/**
329 * Pick the first implemented and running port
330 */
331unsigned ahci_port_select(AHCIQState *ahci)
332{
333 uint32_t ports, reg;
334 unsigned i;
335
336 ports = ahci_rreg(ahci, AHCI_PI);
337 for (i = 0; i < 32; ports >>= 1, ++i) {
338 if (ports == 0) {
339 i = 32;
340 }
341
342 if (!(ports & 0x01)) {
343 continue;
344 }
345
346 reg = ahci_px_rreg(ahci, i, AHCI_PX_CMD);
347 if (BITSET(reg, AHCI_PX_CMD_ST)) {
348 break;
349 }
350 }
351 g_assert(i < 32);
352 return i;
353}
e83fd96b
JS
354
355/**
356 * Clear a port's interrupts and status information prior to a test.
357 */
358void ahci_port_clear(AHCIQState *ahci, uint8_t port)
359{
360 uint32_t reg;
361
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);
366
367 /* Wipe the FIS-Recieve Buffer */
368 qmemset(ahci->port[port].fb, 0x00, 0x100);
369}
6cae27a6 370
85c34e93
JS
371/**
372 * Check a port for errors.
373 */
374void ahci_port_check_error(AHCIQState *ahci, uint8_t port)
375{
376 uint32_t reg;
377
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);
382
383 /* The Sata Error Register should be empty. */
384 reg = ahci_px_rreg(ahci, port, AHCI_PX_SERR);
385 g_assert_cmphex(reg, ==, 0);
386
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);
391}
392
5bf99aa1
JS
393void ahci_port_check_interrupts(AHCIQState *ahci, uint8_t port,
394 uint32_t intr_mask)
395{
396 uint32_t reg;
397
398 /* Check for expected interrupts */
399 reg = ahci_px_rreg(ahci, port, AHCI_PX_IS);
400 ASSERT_BIT_SET(reg, intr_mask);
401
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);
405}
406
89a46723
JS
407void ahci_port_check_nonbusy(AHCIQState *ahci, uint8_t port, uint8_t slot)
408{
409 uint32_t reg;
410
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));
414
415 /* Non-NCQ */
416 reg = ahci_px_rreg(ahci, port, AHCI_PX_CI);
417 ASSERT_BIT_CLEAR(reg, (1 << slot));
418
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);
423}
424
d1ef8838
JS
425void ahci_port_check_d2h_sanity(AHCIQState *ahci, uint8_t port, uint8_t slot)
426{
427 RegD2HFIS *d2h = g_malloc0(0x20);
428 uint32_t reg;
429
430 memread(ahci->port[port].fb + 0x40, d2h, 0x20);
431 g_assert_cmphex(d2h->fis_type, ==, 0x34);
432
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);
436
437 g_free(d2h);
438}
439
440void ahci_port_check_pio_sanity(AHCIQState *ahci, uint8_t port,
441 uint8_t slot, size_t buffsize)
442{
443 PIOSetupFIS *pio = g_malloc0(0x20);
444
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);
450
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);
458 }
459
460 g_free(pio);
461}
462
463void ahci_port_check_cmd_sanity(AHCIQState *ahci, uint8_t port,
464 uint8_t slot, size_t buffsize)
465{
466 AHCICommandHeader cmd;
467
468 ahci_get_command_header(ahci, port, slot, &cmd);
469 g_assert_cmphex(buffsize, ==, cmd.prdbc);
470}
471
6cae27a6
JS
472/* Get the command in #slot of port #port. */
473void ahci_get_command_header(AHCIQState *ahci, uint8_t port,
474 uint8_t slot, AHCICommandHeader *cmd)
475{
476 uint64_t ba = ahci->port[port].clb;
477 ba += slot * sizeof(AHCICommandHeader);
478 memread(ba, cmd, sizeof(AHCICommandHeader));
479
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);
484}
485
486/* Set the command in #slot of port #port. */
487void ahci_set_command_header(AHCIQState *ahci, uint8_t port,
488 uint8_t slot, AHCICommandHeader *cmd)
489{
4a42f6d4 490 AHCICommandHeader tmp = { .flags = 0 };
6cae27a6
JS
491 uint64_t ba = ahci->port[port].clb;
492 ba += slot * sizeof(AHCICommandHeader);
493
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);
498
499 memwrite(ba, &tmp, sizeof(AHCICommandHeader));
500}
501
502void ahci_destroy_command(AHCIQState *ahci, uint8_t port, uint8_t slot)
503{
504 AHCICommandHeader cmd;
505
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;
511 }
512
513 /* Free the Table */
514 ahci_free(ahci, cmd.ctba);
515
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;
522}
523
52515766
JS
524void ahci_write_fis(AHCIQState *ahci, RegH2DFIS *fis, uint64_t addr)
525{
526 RegH2DFIS tmp = *fis;
527
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. */
531
532 /* All other FIS fields are 8 bit and do not need to be flipped. */
533 tmp.count = cpu_to_le16(tmp.count);
534
535 memwrite(addr, &tmp, sizeof(tmp));
536}
537
6cae27a6
JS
538unsigned ahci_pick_cmd(AHCIQState *ahci, uint8_t port)
539{
540 unsigned i;
541 unsigned j;
542 uint32_t reg;
543
544 reg = ahci_px_rreg(ahci, port, AHCI_PX_CI);
545
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;
551 }
552 ahci_destroy_command(ahci, port, i);
553 ahci->port[port].next = (j + 1) % 32;
554 return j;
555 }
556
557 g_test_message("All command slots were busy.");
558 g_assert_not_reached();
559}
64a5a272
JS
560
561inline unsigned size_to_prdtl(unsigned bytes, unsigned bytes_per_prd)
562{
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;
567}
568
11322195
JS
569/* Given a guest buffer address, perform an IO operation */
570void ahci_guest_io(AHCIQState *ahci, uint8_t port, uint8_t ide_cmd,
571 uint64_t buffer, size_t bufsize)
572{
573 AHCICommand *cmd;
574
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);
582}
583
64a5a272
JS
584struct 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;
598};
599
600static AHCICommandProp *ahci_command_find(uint8_t command_name)
601{
602 int i;
603
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];
607 }
608 }
609
610 return NULL;
611}
612
ae029620
JS
613/* Given a HOST buffer, create a buffer address and perform an IO operation. */
614void ahci_io(AHCIQState *ahci, uint8_t port, uint8_t ide_cmd,
615 void *buffer, size_t bufsize)
616{
617 uint64_t ptr;
618 AHCICommandProp *props;
619
620 props = ahci_command_find(ide_cmd);
621 g_assert(props);
622 ptr = ahci_alloc(ahci, bufsize);
623 g_assert(ptr);
624
625 if (props->write) {
626 memwrite(ptr, buffer, bufsize);
627 }
628
629 ahci_guest_io(ahci, port, ide_cmd, ptr, bufsize);
630
631 if (props->read) {
632 memread(ptr, buffer, bufsize);
633 }
634
635 ahci_free(ahci, ptr);
636}
637
64a5a272
JS
638/**
639 * Initializes a basic command header in memory.
640 * We assume that this is for an ATA command using RegH2DFIS.
641 */
642static void command_header_init(AHCICommand *cmd)
643{
644 AHCICommandHeader *hdr = &cmd->header;
645 AHCICommandProp *props = cmd->props;
646
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;
651 }
652 if (props->atapi) {
653 hdr->flags |= CMDH_ATAPI;
654 }
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;
659}
660
661static void command_table_init(AHCICommand *cmd)
662{
663 RegH2DFIS *fis = &(cmd->fis);
664
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;
672 }
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));
677}
678
679AHCICommand *ahci_command_create(uint8_t command_name)
680{
681 AHCICommandProp *props = ahci_command_find(command_name);
682 AHCICommand *cmd;
683
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);
690
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;
697
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;
704
705 command_header_init(cmd);
706 command_table_init(cmd);
707
708 return cmd;
709}
710
711void ahci_command_free(AHCICommand *cmd)
712{
713 g_free(cmd);
714}
715
f9f963e0
JS
716void ahci_command_set_flags(AHCICommand *cmd, uint16_t cmdh_flags)
717{
718 cmd->header.flags |= cmdh_flags;
719}
720
721void ahci_command_clr_flags(AHCICommand *cmd, uint16_t cmdh_flags)
722{
723 cmd->header.flags &= ~cmdh_flags;
724}
725
726void ahci_command_set_offset(AHCICommand *cmd, uint64_t lba_sect)
727{
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();
736 }
737
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;
744 }
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;
748}
749
64a5a272
JS
750void ahci_command_set_buffer(AHCICommand *cmd, uint64_t buffer)
751{
752 cmd->buffer = buffer;
753}
754
cbc97569
JS
755void ahci_command_set_sizes(AHCICommand *cmd, uint64_t xbytes,
756 unsigned prd_size)
757{
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);
765}
766
767void ahci_command_set_size(AHCICommand *cmd, uint64_t xbytes)
768{
769 ahci_command_set_sizes(cmd, xbytes, cmd->prd_size);
770}
771
772void ahci_command_set_prd_size(AHCICommand *cmd, unsigned prd_size)
773{
774 ahci_command_set_sizes(cmd, cmd->xbytes, prd_size);
775}
776
f9f963e0
JS
777void ahci_command_adjust(AHCICommand *cmd, uint64_t offset, uint64_t buffer,
778 uint64_t xbytes, unsigned prd_size)
779{
780 ahci_command_set_sizes(cmd, xbytes, prd_size);
781 ahci_command_set_buffer(cmd, buffer);
782 ahci_command_set_offset(cmd, offset);
783}
784
64a5a272
JS
785void ahci_command_commit(AHCIQState *ahci, AHCICommand *cmd, uint8_t port)
786{
787 uint16_t i, prdtl;
788 uint64_t table_size, table_ptr, remaining;
789 PRD prd;
790
791 /* This command is now tied to this port/command slot */
792 cmd->port = port;
793 cmd->slot = ahci_pick_cmd(ahci, port);
794
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;
803
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);
807
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;
822 }
823 prd.dbc |= cpu_to_le32(0x80000000); /* Request DPS Interrupt */
824
825 /* Commit the PRD entry to the Command Table */
826 memwrite(table_ptr + 0x80 + (i * sizeof(PRD)),
827 &prd, sizeof(PRD));
828 }
829
830 /* Bookmark the PRDTL and CTBA values */
831 ahci->port[port].ctba[cmd->slot] = table_ptr;
832 ahci->port[port].prdtl[cmd->slot] = prdtl;
833}
834
835void ahci_command_issue_async(AHCIQState *ahci, AHCICommand *cmd)
836{
837 if (cmd->props->ncq) {
838 ahci_px_wreg(ahci, cmd->port, AHCI_PX_SACT, (1 << cmd->slot));
839 }
840
841 ahci_px_wreg(ahci, cmd->port, AHCI_PX_CI, (1 << cmd->slot));
842}
843
844void ahci_command_wait(AHCIQState *ahci, AHCICommand *cmd)
845{
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);
853 }
854}
855
856void ahci_command_issue(AHCIQState *ahci, AHCICommand *cmd)
857{
858 ahci_command_issue_async(ahci, cmd);
859 ahci_command_wait(ahci, cmd);
860}
861
ea41deb6
JS
862void ahci_command_verify(AHCIQState *ahci, AHCICommand *cmd)
863{
864 uint8_t slot = cmd->slot;
865 uint8_t port = cmd->port;
866
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);
874 }
875}
876
64a5a272
JS
877uint8_t ahci_command_slot(AHCICommand *cmd)
878{
879 return cmd->slot;
880}
This page took 0.129643 seconds and 4 git commands to generate.