qtest/ahci: add dma test
v3 cover qtest/ahci: add dma test
Add a simple DMA r/w test to ahci-test.
Oh, and for the first 18 patches, refactor everything into helpers so that each ahci_test isn't a thousand lines long.
This patch depends upon the "ahci test preliminary refactoring" series upstream, which shuffled a lot of libqos and malloc facilities to support this series.
This patchset is a necessary step in checking in AHCI/DMA migration tests that I will later use as proof as suitability of enabling the ICH9 and AHCI migration flags.
V3:
(04) Changed "px" to "port" and "cx" to "slot" In this and all other patches. Kept R-Bys in other patches as this is a non-functional change.
(10) Modified aux to be an array of four bytes, and added a note explaining that they are for NCQ and not currently used in libqos.
(12) Minor formatting and updating aux initialization. Kept R-B.
V2:
(03) Added cpu_to_le16 into intermediary code.
(04) Fixed assignment to be or-equals. ahci_set_command_header no longer modifies its argument.
(09) Fixed assignment to be or-equals in the helper refactor.
(10) New patch. Writes an FIS to guest memory, handling endianness.
(11) Remove the property table sentinel
update find_command to use the array size instead.
(12) Remove cpu_to_le16 from ahci_command_commit,
leave endianness to ahci_write_fis.
(14) Remove cpu_to_le16 from ahci_set_sizes,
leave endianness to ahci_write_fis.
(19) qmemset the buffer to zeroes between IO calls,
just to make sure.
(--) Removed the ahci_macro_boot function patch
(...for now...)
(--) Retested entire stack of patches on F20/PPC.
~John
John Snow (19):
libqos/ahci: Add ahci_port_select helper libqos/ahci: Add ahci_port_clear helper qtest/ahci: rename 'Command' to 'CommandHeader' libqos/ahci: Add command header helpers libqos/ahci: Add ahci_port_check_error helper libqos/ahci: Add ahci_port_check_interrupts helper libqos/ahci: Add port_check_nonbusy helper libqos/ahci: Add cmd response sanity check helpers qtest/ahci: Demagic ahci tests. qtest/ahci: add ahci_write_fis libqos/ahci: Add ide cmd properties libqos/ahci: add ahci command functions libqos/ahci: add ahci command verify libqos/ahci: add ahci command size setters libqos/ahci: Add ahci_guest_io libqos/ahci: add ahci_io
libqos/ahci: Add ahci_clean_mem qtest/ahci: Assert sector size in identify test qtest/ahci: Adding simple dma read-write test
tests/ahci-test.c | 230 ++++++++------------ tests/libqos/ahci.c | 569 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/libqos/ahci.h | 168 ++++++++++++--- tests/libqos/malloc.c | 5 + tests/libqos/malloc.h | 1 + 5 files changed, 804 insertions(+), 169 deletions(-)
Signature · 1 line
1.9.3
PATCH v3 1/19 libqos/ahci: Add ahci_port_select helper
This helper identifies which port of the
AHCI HBA has a device we may run tests on.
--- tests/ahci-test.c | 19 ++----------------- tests/libqos/ahci.c | 27 +++++++++++++++++++++++++++ tests/libqos/ahci.h | 1 + 3 files changed, 30 insertions(+), 17 deletions(-)
tests/ahci-test.c +2 -17
@@ -662,7 +662,7 @@ static void ahci_test_identify(AHCIQState *ahci)RegH2DFIS fis;AHCICommand cmd;PRD prd; -uint32_t ports, reg, table, data_ptr; +uint32_t reg, table, data_ptr;uint16_t buff[256 ];unsigned i;int rc;
@@ -684,22 +684,7 @@ static void ahci_test_identify(AHCIQState *ahci)*/ /* Pick the first implemented and running port */ - ports =ahci_rreg (ahci,AHCI_PI ); -for (i =0 ; i <32 ; ports >>=1 , ++i) { -if (ports ==0 ) { - i =32 ; - } - -if (!(ports &0x01 )) { -continue ; - } - - reg =ahci_px_rreg (ahci, i,AHCI_PX_CMD ); -if (BITSET (reg,AHCI_PX_CMD_ST )) { -break ; - } - } - g_assert_cmphex(i, <, 32); + i =ahci_port_select (ahci);g_test_message ("Selected port %u for test" , i);/* Clear out this port's interrupts (ignore the init register d2h fis) */
tests/libqos/ahci.c +27 -0
@@ -267,3 +267,30 @@ void ahci_hba_enable(AHCIQState *ahci)* In the future, a small test-case to inspect the Register D2H FIS * and clear the initial interrupts might be good. */ } + +/** +* Pick the first implemented and running port +*/ +unsigned ahci_port_select (AHCIQState *ahci) +{ +uint32_t ports, reg; +unsigned i; + + ports =ahci_rreg (ahci,AHCI_PI ); +for (i =0 ; i <32 ; ports >>=1 , ++i) { +if (ports ==0 ) { + i =32 ; + } + +if (!(ports &0x01 )) { +continue ; + } + + reg =ahci_px_rreg (ahci, i,AHCI_PX_CMD ); +if (BITSET (reg,AHCI_PX_CMD_ST )) { +break ; + } + } +g_assert (i <32 ); +return i; +}
tests/libqos/ahci.h +1 -0
@@ -431,5 +431,6 @@ void free_ahci_device(QPCIDevice *dev);void ahci_pci_enable (AHCIQState *ahci);void start_ahci_device (AHCIQState *ahci);void ahci_hba_enable (AHCIQState *ahci); +unsigned ahci_port_select (AHCIQState *ahci);#endif
Signature · 1 line
1.9.3
PATCH v3 2/19 libqos/ahci: Add ahci_port_clear helper
In reply to John SnowAdd a helper that assists in clearing out potentially old error and FIS information from an AHCI port's data structures. This ensures we always start with a blank slate for interrupt and FIS receipt information.
--- tests/ahci-test.c | 9 ++------- tests/libqos/ahci.c | 16 ++++++++++++++++ tests/libqos/ahci.h | 1 + 3 files changed, 19 insertions(+), 7 deletions(-)
tests/ahci-test.c +2 -7
@@ -687,13 +687,8 @@ static void ahci_test_identify(AHCIQState *ahci) i =ahci_port_select (ahci);g_test_message ("Selected port %u for test" , i); -/* Clear out this port's interrupts (ignore the init register d2h fis) */ - reg =ahci_px_rreg (ahci, i,AHCI_PX_IS ); -ahci_px_wreg (ahci, i,AHCI_PX_IS , reg); - g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0); - -/* Wipe the FIS-Receive Buffer */ -qmemset (ahci->port[i].fb,0x00 ,0x100 ); +/* Clear out the FIS Receive area and any pending interrupts. */ +ahci_port_clear (ahci, i);/* Create a Command Table buffer. 0x80 is the smallest with a PRDTL of 0. */ /* We need at least one PRD, so round up to the nearest 0x80 multiple. */
tests/libqos/ahci.c +16 -0
@@ -294,3 +294,19 @@ unsigned ahci_port_select(AHCIQState *ahci)g_assert (i <32 );return i; } + +/** +* Clear a port's interrupts and status information prior to a test. +*/ +void ahci_port_clear (AHCIQState *ahci,uint8_t port) +{ +uint32_t reg; + +/* Clear out this port's interrupts (ignore the init register d2h fis) */ + reg =ahci_px_rreg (ahci, port,AHCI_PX_IS ); +ahci_px_wreg (ahci, port,AHCI_PX_IS , reg); + g_assert_cmphex(ahci_px_rreg(ahci, port, AHCI_PX_IS), ==, 0); + +/* Wipe the FIS-Recieve Buffer */ +qmemset (ahci->port[port].fb,0x00 ,0x100 ); +}
tests/libqos/ahci.h +1 -0
@@ -432,5 +432,6 @@ void ahci_pci_enable(AHCIQState *ahci);void start_ahci_device (AHCIQState *ahci);void ahci_hba_enable (AHCIQState *ahci);unsigned ahci_port_select (AHCIQState *ahci); +void ahci_port_clear (AHCIQState *ahci,uint8_t port);#endif
Signature · 1 line
1.9.3
PATCH v3 3/19 qtest/ahci: rename 'Command' to 'CommandHeader'
In reply to John SnowThe structure name is a bit of a misnomer; the structure currently named command is actually the commandheader. A future patch in this series will add an actual "Command" structure, so we'll rename it now before the rest of the functions in this series try to use it.
In addition, rename the "b1" and "b2" fields
to be a unified uint16_t named "flags."
--- tests/ahci-test.c | 8 ++++---- tests/libqos/ahci.h | 7 +++---- 2 files changed, 7 insertions(+), 8 deletions(-)
tests/ahci-test.c +4 -4
@@ -660,7 +660,7 @@ static void ahci_test_identify(AHCIQState *ahci)RegD2HFIS *d2h =g_malloc0 (0x20 );RegD2HFIS *pio =g_malloc0 (0x20 );RegH2DFIS fis; -AHCICommand cmd; +AHCICommandHeader cmd;PRD prd;uint32_t reg, table, data_ptr;uint16_t buff[256 ];
@@ -703,9 +703,9 @@ static void ahci_test_identify(AHCIQState *ahci)/* Copy the existing Command #0 structure from the CLB into local memory, * and build a new command #0. */ memread (ahci->port[i].clb, &cmd,sizeof (cmd)); - cmd.b1 =5 ;/* reg_h2d_fis is 5 double-words long */ - cmd.b2 =0x04 ;/* clear PxTFD.STS.BSY when done */ - cmd.prdtl =cpu_to_le16 (1 );/* One PRD table entry. */ + cmd.flags =cpu_to_le16 (5 );/* reg_h2d_fis is 5 double-words long */ + cmd.flags |=cpu_to_le16 (0x400 );/* clear PxTFD.STS.BSY when done */ + cmd.prdtl =cpu_to_le16 (1 );/* One PRD table entry. */ cmd.prdbc =0 ; cmd.ctba =cpu_to_le32 (table); cmd.ctbau =0 ;
tests/libqos/ahci.h +3 -4
@@ -329,15 +329,14 @@ typedef struct RegH2DFIS {* Command List entry structure. * The command list contains between 1-32 of these structures. */ -typedef struct AHCICommand { -uint8_t b1; -uint8_t b2; +typedef struct AHCICommandHeader { +uint16_t flags;/* Cmd-Fis-Len, PMP#, and flags. */ uint16_t prdtl;/* Phys Region Desc. Table Length */ uint32_t prdbc;/* Phys Region Desc. Byte Count */ uint32_t ctba;/* Command Table Descriptor Base Address */ uint32_t ctbau;/* '' Upper */ uint32_t res[4 ]; -} __attribute__((__packed__ ))AHCICommand ; +} __attribute__((__packed__ ))AHCICommandHeader ;/** * Physical Region Descriptor; pointed to by the Command List Header,
Signature · 1 line
1.9.3
PATCH v3 4/19 libqos/ahci: Add command header helpers
In reply to John SnowAdds command header helper functions:
-ahci_command_header_set
-ahci_command_header_get,
-ahci_command_destroy, and
-ahci_cmd_pick
These helpers help to quickly manage the command header information in the AHCI device.
ahci_command_header_set and get will store or retrieve an AHCI command header, respectively.
ahci_cmd_pick chooses the first available but least recently used command slot to allow us to cycle through the available command slots.
ahci_command_destroy obliterates all information contained within a given slot's command header, and frees its associated command table, but not its DMA buffer!
Lastly, the command table pointer fields (dba and dbau) are merged into a single 64bit value to make managing 64bit tests simpler.
--- tests/ahci-test.c | 43 ++++++++++++++++-------------- tests/libqos/ahci.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/libqos/ahci.h | 17 ++++++++---- 3 files changed, 110 insertions(+), 25 deletions(-)
tests/ahci-test.c +23 -20
@@ -662,10 +662,12 @@ static void ahci_test_identify(AHCIQState *ahci)RegH2DFIS fis;AHCICommandHeader cmd;PRD prd; -uint32_t reg, table, data_ptr; +uint32_t reg, data_ptr;uint16_t buff[256 ];unsigned i;int rc; +uint8_t cx; +uint64_t table;g_assert (ahci !=NULL );
@@ -700,19 +702,19 @@ static void ahci_test_identify(AHCIQState *ahci) data_ptr =ahci_alloc (ahci,512 );g_assert (data_ptr); -/* Copy the existing Command #0 structure from the CLB into local memory, -* and build a new command #0. */ -memread (ahci->port[i].clb, &cmd,sizeof (cmd)); - cmd.flags =cpu_to_le16 (5 );/* reg_h2d_fis is 5 double-words long */ - cmd.flags |=cpu_to_le16 (0x400 );/* clear PxTFD.STS.BSY when done */ - cmd.prdtl =cpu_to_le16 (1 );/* One PRD table entry. */ +/* pick a command slot (should be 0!) */ + cx =ahci_pick_cmd (ahci, i); + +/* Construct our Command Header (set_command_header handles endianness.) */ +memset (&cmd,0x00 ,sizeof (cmd)); + cmd.flags =5 ;/* reg_h2d_fis is 5 double-words long */ + cmd.flags |=0x400 ;/* clear PxTFD.STS.BSY when done */ + cmd.prdtl =1 ;/* One PRD table entry. */ cmd.prdbc =0 ; - cmd.ctba =cpu_to_le32 (table); - cmd.ctbau =0 ; + cmd.ctba = table;/* Construct our PRD, noting that DBC is 0-indexed. */ - prd.dba =cpu_to_le32 (data_ptr); - prd.dbau =0 ; + prd.dba =cpu_to_le64 (data_ptr); prd.res =0 ;/* 511+1 bytes, request DPS interrupt */ prd.dbc =cpu_to_le32 (511 |0x80000000 );
@@ -733,14 +735,15 @@ static void ahci_test_identify(AHCIQState *ahci)/* Commit the PRD entry to the Command Table */ memwrite (table +0x80 , &prd,sizeof (prd)); -/* Commit Command #0, pointing to the Table, to the Command List Buffer. */ -memwrite (ahci->port[i].clb, &cmd,sizeof (cmd)); +/* Commit Command #cx, pointing to the Table, to the Command List Buffer. */ +ahci_set_command_header (ahci, i, cx, &cmd); -/* Everything is in place, but we haven't given the go-ahead yet. */ +/* Everything is in place, but we haven't given the go-ahead yet, +* so we should find that there are no pending interrupts yet. */ g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0); -/* Issue Command #0 via PxCI */ -ahci_px_wreg (ahci, i,AHCI_PX_CI , (1 <<0 )); +/* Issue Command #cx via PxCI */ +ahci_px_wreg (ahci, i,AHCI_PX_CI , (1 << cx));while (BITSET (ahci_px_rreg (ahci, i,AHCI_PX_TFD ),AHCI_PX_TFD_STS_BSY )) {usleep (50 ); }
@@ -764,9 +767,9 @@ static void ahci_test_identify(AHCIQState *ahci)ASSERT_BIT_CLEAR (reg,AHCI_PX_TFD_STS_ERR );ASSERT_BIT_CLEAR (reg,AHCI_PX_TFD_ERR ); -/* Investigate CMD #0, assert that we read 512 bytes */ -memread (ahci->port[i].clb, &cmd,sizeof (cmd)); - g_assert_cmphex(512, ==, le32_to_cpu(cmd.prdbc)); +/* Investigate the CMD, assert that we read 512 bytes */ +ahci_get_command_header (ahci, i, cx, &cmd); + g_assert_cmphex(512, ==, cmd.prdbc);/* Investigate FIS responses */ memread (ahci->port[i].fb +0x20 , pio,0x20 );
@@ -783,7 +786,7 @@ static void ahci_test_identify(AHCIQState *ahci)/* The PIO Setup FIS contains a "bytes read" field, which is a * 16-bit value. The Physical Region Descriptor Byte Count is * 32-bit, but for small transfers using one PRD, it should match. */ - g_assert_cmphex(le16_to_cpu(pio->res4), ==, le32_to_cpu(cmd.prdbc)); + g_assert_cmphex(le16_to_cpu(pio->res4), ==, cmd.prdbc);/* Last, but not least: Investigate the IDENTIFY response data. */ memread (data_ptr, &buff,512 );
tests/libqos/ahci.c +75 -0
78 lines not on this page · raw message
tests/libqos/ahci.h +12 -5
@@ -248,6 +248,9 @@typedef struct AHCIPortQState {uint64_t fb;uint64_t clb; +uint64_t ctba[32 ]; +uint16_t prdtl[32 ]; +uint8_t next;/** Next Command Slot to Use **/ }AHCIPortQState ; typedef struct AHCIQState {
@@ -333,8 +336,7 @@ typedef struct AHCICommandHeader {uint16_t flags;/* Cmd-Fis-Len, PMP#, and flags. */ uint16_t prdtl;/* Phys Region Desc. Table Length */ uint32_t prdbc;/* Phys Region Desc. Byte Count */ -uint32_t ctba;/* Command Table Descriptor Base Address */ -uint32_t ctbau;/* '' Upper */ +uint64_t ctba;/* Command Table Descriptor Base Address */ uint32_t res[4 ]; } __attribute__((__packed__)) AHCICommandHeader;
@@ -343,11 +345,10 @@ typedef struct AHCICommandHeader {* struct ahci_command. */ typedef struct PRD { -uint32_t dba;/* Data Base Address */ -uint32_t dbau;/* Data Base Address Upper */ +uint64_t dba;/* Data Base Address */ uint32_t res;/* Reserved */ uint32_t dbc;/* Data Byte Count (0-indexed) & Interrupt Flag (bit 2^31) */ -}PRD ; +} __attribute__((__packed__ ))PRD ;/*** Macro Utilities ***/ #define BITANY (data, mask) (((data) & (mask)) != 0)
@@ -432,5 +433,11 @@ void start_ahci_device(AHCIQState *ahci);void ahci_hba_enable (AHCIQState *ahci);unsigned ahci_port_select (AHCIQState *ahci);void ahci_port_clear (AHCIQState *ahci,uint8_t port); +void ahci_get_command_header (AHCIQState *ahci,uint8_t port, +uint8_t slot,AHCICommandHeader *cmd); +void ahci_set_command_header (AHCIQState *ahci,uint8_t port, +uint8_t slot,AHCICommandHeader *cmd); +void ahci_destroy_command (AHCIQState *ahci,uint8_t port,uint8_t slot); +unsigned ahci_pick_cmd (AHCIQState *ahci,uint8_t port);#endif
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v3 04/19] libqos/ahci: Add command header helpers
Quoted 22 lines · John Snow
Adds command header helper functions:
(quoted diff) +0
-4-ahci_command_header_set -ahci_command_header_get, -ahci_command_destroy, and -ahci_cmd_pickThese helpers help to quickly manage the command header information in the AHCI device.
ahci_command_header_setand get will store or retrieve an AHCI command header, respectively.
ahci_cmd_pickchooses the first available but least recently used command slot to allow us to cycle through the available command slots.
ahci_command_destroyobliterates all information contained within a given slot's command header, and frees its associated command table, but not its DMA buffer!Lastly, the command table pointer fields (dba and dbau) are merged into a single 64bit value to make managing 64bit tests simpler.
Quoted 6 lines
Signed-off-by John Snow <[email protected]>--- tests/ahci-test.c | 43 ++++++++++++++++-------------- tests/libqos/ahci.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/libqos/ahci.h | 17 ++++++++---- 3 files changed, 110 insertions(+), 25 deletions(-)
PATCH v3 5/19 libqos/ahci: Add ahci_port_check_error helper
In reply to John Snowahci_port_check_error checks a given port's error registers and asserts that everything from the port-level view is still OK.
--- tests/ahci-test.c | 8 +------- tests/libqos/ahci.c | 22 ++++++++++++++++++++++ tests/libqos/ahci.h | 1 + 3 files changed, 24 insertions(+), 7 deletions(-)
tests/ahci-test.c +1 -7
@@ -747,6 +747,7 @@ static void ahci_test_identify(AHCIQState *ahci)while (BITSET (ahci_px_rreg (ahci, i,AHCI_PX_TFD ),AHCI_PX_TFD_STS_BSY )) {usleep (50 ); } +ahci_port_check_error (ahci, i);/* Check for expected interrupts */ reg =ahci_px_rreg (ahci, i,AHCI_PX_IS );
@@ -760,13 +761,6 @@ static void ahci_test_identify(AHCIQState *ahci) AHCI_PX_IS_DHRS | AHCI_PX_IS_PSS | AHCI_PX_IS_DPS); g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0); -/* Check for errors. */ - reg =ahci_px_rreg (ahci, i,AHCI_PX_SERR ); - g_assert_cmphex(reg, ==, 0); - reg =ahci_px_rreg (ahci, i,AHCI_PX_TFD ); -ASSERT_BIT_CLEAR (reg,AHCI_PX_TFD_STS_ERR ); -ASSERT_BIT_CLEAR (reg,AHCI_PX_TFD_ERR ); -/* Investigate the CMD, assert that we read 512 bytes */ ahci_get_command_header (ahci, i, cx, &cmd); g_assert_cmphex(512, ==, cmd.prdbc);
tests/libqos/ahci.c +22 -0
@@ -311,6 +311,28 @@ void ahci_port_clear(AHCIQState *ahci, uint8_t port)qmemset (ahci->port[port].fb,0x00 ,0x100 ); } +/** +* Check a port for errors. +*/ +void ahci_port_check_error (AHCIQState *ahci,uint8_t port) +{ +uint32_t reg; + +/* The upper 9 bits of the IS register all indicate errors. */ + reg =ahci_px_rreg (ahci, port,AHCI_PX_IS ); + reg >>=23 ; + g_assert_cmphex(reg, ==, 0); + +/* The Sata Error Register should be empty. */ + reg =ahci_px_rreg (ahci, port,AHCI_PX_SERR ); + g_assert_cmphex(reg, ==, 0); + +/* The TFD also has two error sections. */ + reg =ahci_px_rreg (ahci, port,AHCI_PX_TFD ); +ASSERT_BIT_CLEAR (reg,AHCI_PX_TFD_STS_ERR ); +ASSERT_BIT_CLEAR (reg,AHCI_PX_TFD_ERR ); +} +/* Get the command in #slot of port #port. */ void ahci_get_command_header (AHCIQState *ahci,uint8_t port,uint8_t slot,AHCICommandHeader *cmd)
tests/libqos/ahci.h +1 -0
@@ -433,6 +433,7 @@ void start_ahci_device(AHCIQState *ahci);void ahci_hba_enable (AHCIQState *ahci);unsigned ahci_port_select (AHCIQState *ahci);void ahci_port_clear (AHCIQState *ahci,uint8_t port); +void ahci_port_check_error (AHCIQState *ahci,uint8_t port);void ahci_get_command_header (AHCIQState *ahci,uint8_t port,uint8_t slot,AHCICommandHeader *cmd); void ahci_set_command_header(AHCIQState *ahci, uint8_t port,
Signature · 1 line
1.9.3
PATCH v3 6/19 libqos/ahci: Add ahci_port_check_interrupts helper
In reply to John SnowA helper that compares a given port's current interrupts and checks them against a supplied list of expected interrupt bits, and throws an error if they do not match.
The helper then resets the requested interrupts on this port, and asserts that the interrupt register is now empty.
--- tests/ahci-test.c | 13 ++----------- tests/libqos/ahci.c | 14 ++++++++++++++ tests/libqos/ahci.h | 2 ++ 3 files changed, 18 insertions(+), 11 deletions(-)
tests/ahci-test.c +2 -11
@@ -747,19 +747,10 @@ static void ahci_test_identify(AHCIQState *ahci)while (BITSET (ahci_px_rreg (ahci, i,AHCI_PX_TFD ),AHCI_PX_TFD_STS_BSY )) {usleep (50 ); } +/* Check registers for post-command consistency */ ahci_port_check_error (ahci, i); - -/* Check for expected interrupts */ - reg =ahci_px_rreg (ahci, i,AHCI_PX_IS ); -ASSERT_BIT_SET (reg,AHCI_PX_IS_DHRS ); -ASSERT_BIT_SET (reg,AHCI_PX_IS_PSS );/* BUG: we expect AHCI_PX_IS_DPS to be set. */ -ASSERT_BIT_CLEAR (reg,AHCI_PX_IS_DPS ); - -/* Clear expected interrupts and assert all interrupts now cleared. */ -ahci_px_wreg (ahci, i,AHCI_PX_IS , -AHCI_PX_IS_DHRS |AHCI_PX_IS_PSS |AHCI_PX_IS_DPS ); - g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0); +ahci_port_check_interrupts (ahci, i,AHCI_PX_IS_DHRS |AHCI_PX_IS_PSS );/* Investigate the CMD, assert that we read 512 bytes */ ahci_get_command_header (ahci, i, cx, &cmd);
tests/libqos/ahci.c +14 -0
@@ -333,6 +333,20 @@ void ahci_port_check_error(AHCIQState *ahci, uint8_t port)ASSERT_BIT_CLEAR (reg,AHCI_PX_TFD_ERR ); } +void ahci_port_check_interrupts (AHCIQState *ahci,uint8_t port, +uint32_t intr_mask) +{ +uint32_t reg; + +/* Check for expected interrupts */ + reg =ahci_px_rreg (ahci, port,AHCI_PX_IS ); +ASSERT_BIT_SET (reg, intr_mask); + +/* Clear expected interrupts and assert all interrupts now cleared. */ +ahci_px_wreg (ahci, port,AHCI_PX_IS , intr_mask); + g_assert_cmphex(ahci_px_rreg(ahci, port, AHCI_PX_IS), ==, 0); +} +/* Get the command in #slot of port #port. */ void ahci_get_command_header (AHCIQState *ahci,uint8_t port,uint8_t slot,AHCICommandHeader *cmd)
tests/libqos/ahci.h +2 -0
@@ -434,6 +434,8 @@ void ahci_hba_enable(AHCIQState *ahci);unsigned ahci_port_select (AHCIQState *ahci);void ahci_port_clear (AHCIQState *ahci,uint8_t port);void ahci_port_check_error (AHCIQState *ahci,uint8_t port); +void ahci_port_check_interrupts (AHCIQState *ahci,uint8_t port, +uint32_t intr_mask);void ahci_get_command_header (AHCIQState *ahci,uint8_t port,uint8_t slot,AHCICommandHeader *cmd); void ahci_set_command_header(AHCIQState *ahci, uint8_t port,
Signature · 1 line
1.9.3
PATCH v3 7/19 libqos/ahci: Add port_check_nonbusy helper
In reply to John SnowA simple helper that asserts a given port is not busy processing any commands via the TFD, Command Issue and SACT registers.
--- tests/ahci-test.c | 1 + tests/libqos/ahci.c | 18 ++++++++++++++++++ tests/libqos/ahci.h | 1 + 3 files changed, 20 insertions(+)
tests/ahci-test.c +1 -0
@@ -751,6 +751,7 @@ static void ahci_test_identify(AHCIQState *ahci)ahci_port_check_error (ahci, i);/* BUG: we expect AHCI_PX_IS_DPS to be set. */ ahci_port_check_interrupts (ahci, i,AHCI_PX_IS_DHRS |AHCI_PX_IS_PSS ); +ahci_port_check_nonbusy (ahci, i, cx);/* Investigate the CMD, assert that we read 512 bytes */ ahci_get_command_header (ahci, i, cx, &cmd);
tests/libqos/ahci.c +18 -0
@@ -347,6 +347,24 @@ void ahci_port_check_interrupts(AHCIQState *ahci, uint8_t port, g_assert_cmphex(ahci_px_rreg(ahci, port, AHCI_PX_IS), ==, 0); } +void ahci_port_check_nonbusy (AHCIQState *ahci,uint8_t port,uint8_t slot) +{ +uint32_t reg; + +/* Assert that the command slot is no longer busy (NCQ) */ + reg =ahci_px_rreg (ahci, port,AHCI_PX_SACT ); +ASSERT_BIT_CLEAR (reg, (1 << slot)); + +/* Non-NCQ */ + reg =ahci_px_rreg (ahci, port,AHCI_PX_CI ); +ASSERT_BIT_CLEAR (reg, (1 << slot)); + +/* And assert that we are generally not busy. */ + reg =ahci_px_rreg (ahci, port,AHCI_PX_TFD ); +ASSERT_BIT_CLEAR (reg,AHCI_PX_TFD_STS_BSY ); +ASSERT_BIT_CLEAR (reg,AHCI_PX_TFD_STS_DRQ ); +} +/* Get the command in #slot of port #port. */ void ahci_get_command_header (AHCIQState *ahci,uint8_t port,uint8_t slot,AHCICommandHeader *cmd)
tests/libqos/ahci.h +1 -0
@@ -436,6 +436,7 @@ void ahci_port_clear(AHCIQState *ahci, uint8_t port);void ahci_port_check_error (AHCIQState *ahci,uint8_t port);void ahci_port_check_interrupts (AHCIQState *ahci,uint8_t port,uint32_t intr_mask); +void ahci_port_check_nonbusy (AHCIQState *ahci,uint8_t port,uint8_t slot);void ahci_get_command_header (AHCIQState *ahci,uint8_t port,uint8_t slot,AHCICommandHeader *cmd); void ahci_set_command_header(AHCIQState *ahci, uint8_t port,
Signature · 1 line
1.9.3
PATCH v3 8/19 libqos/ahci: Add cmd response sanity check helpers
In reply to John SnowThis patch adds a few helpers to help sanity-check the response of the AHCI device after a command.
ahci_d2h_check_sanity inspects the D2H Register FIS, ahci_pio_check_sanity inspects the PIO Setup FIS, and ahci_cmd_check_sanity inspects the command header.
To support the PIO sanity check, a new structure is added for the PIO Setup FIS type. Existing FIS types (H2D and D2H) have had their members renamed slightly to condense reserved members into fewer fields; and LBA fields are now represented by arrays of 8 byte chunks instead of independent variables.
--- tests/ahci-test.c | 29 ++++------------------------ tests/libqos/ahci.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++ tests/libqos/ahci.h | 54 ++++++++++++++++++++++++++++++++++++----------------- 3 files changed, 88 insertions(+), 42 deletions(-)
tests/ahci-test.c +4 -25
@@ -657,12 +657,10 @@ static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port)*/ static void ahci_test_identify (AHCIQState *ahci) { -RegD2HFIS *d2h =g_malloc0 (0x20 ); -RegD2HFIS *pio =g_malloc0 (0x20 );RegH2DFIS fis;AHCICommandHeader cmd;PRD prd; -uint32_t reg, data_ptr; +uint32_t data_ptr;uint16_t buff[256 ];unsigned i;int rc;
@@ -752,27 +750,11 @@ static void ahci_test_identify(AHCIQState *ahci)/* BUG: we expect AHCI_PX_IS_DPS to be set. */ ahci_port_check_interrupts (ahci, i,AHCI_PX_IS_DHRS |AHCI_PX_IS_PSS );ahci_port_check_nonbusy (ahci, i, cx); -/* Investigate the CMD, assert that we read 512 bytes */ -ahci_get_command_header (ahci, i, cx, &cmd); - g_assert_cmphex(512, ==, cmd.prdbc); - +ahci_port_check_cmd_sanity (ahci, i, cx,512 );/* Investigate FIS responses */ -memread (ahci->port[i].fb +0x20 , pio,0x20 ); -memread (ahci->port[i].fb +0x40 , d2h,0x20 ); - g_assert_cmphex(pio->fis_type, ==, 0x5f); - g_assert_cmphex(d2h->fis_type, ==, 0x34); - g_assert_cmphex(pio->flags, ==, d2h->flags); - g_assert_cmphex(pio->status, ==, d2h->status); - g_assert_cmphex(pio->error, ==, d2h->error); - - reg =ahci_px_rreg (ahci, i,AHCI_PX_TFD ); - g_assert_cmphex((reg & AHCI_PX_TFD_ERR), ==, pio->error); - g_assert_cmphex((reg & AHCI_PX_TFD_STS), ==, pio->status); -/* The PIO Setup FIS contains a "bytes read" field, which is a -* 16-bit value. The Physical Region Descriptor Byte Count is -* 32-bit, but for small transfers using one PRD, it should match. */ - g_assert_cmphex(le16_to_cpu(pio->res4), ==, cmd.prdbc); +ahci_port_check_d2h_sanity (ahci, i, cx); +ahci_port_check_pio_sanity (ahci, i, cx,512 );/* Last, but not least: Investigate the IDENTIFY response data. */ memread (data_ptr, &buff,512 );
@@ -789,9 +771,6 @@ static void ahci_test_identify(AHCIQState *ahci)string_bswap16 (&buff[23 ],8 ); rc =memcmp (&buff[23 ],"version " ,8 ); g_assert_cmphex(rc, ==, 0); - -g_free (d2h); -g_free (pio); }/******************************************************************************/
tests/libqos/ahci.c +47 -0
53 lines not on this page · raw message
tests/libqos/ahci.h +37 -17
82 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH v3 9/19 qtest/ahci: Demagic ahci tests.
In reply to John SnowAdd human-readable command names and other miscellaneous #defines to help make the code more readable.
Some of these definitions are not yet used in this current series, but for convenience and sanity they have been lumped together here, as it's more trouble than it is worth in a test suite to hand-pick, one-by-one, which preprocessor definitions are useful per-each test.
These definitions include:
ATA Command Mnemonics
Current expected AHCI sector size
FIS magic bytesREG_H2D_FIS flags
Command Header flags
--- tests/ahci-test.c | 12 ++++++------ tests/libqos/ahci.h | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 6 deletions(-)
tests/ahci-test.c +6 -6
@@ -705,9 +705,9 @@ static void ahci_test_identify(AHCIQState *ahci)/* Construct our Command Header (set_command_header handles endianness.) */ memset (&cmd,0x00 ,sizeof (cmd)); - cmd.flags =5 ;/* reg_h2d_fis is 5 double-words long */ - cmd.flags |=0x400 ;/* clear PxTFD.STS.BSY when done */ - cmd.prdtl =1 ;/* One PRD table entry. */ + cmd.flags =5 ;/* reg_h2d_fis is 5 double-words long */ + cmd.flags |=CMDH_CLR_BSY ;/* clear PxTFD.STS.BSY when done */ + cmd.prdtl =1 ;/* One PRD table entry. */ cmd.prdbc =0 ; cmd.ctba = table;
@@ -719,10 +719,10 @@ static void ahci_test_identify(AHCIQState *ahci)/* Construct our Command FIS, Based on http://wiki.osdev.org/AHCI */ memset (&fis,0x00 ,sizeof (fis)); - fis.fis_type =0x27 ;/* Register Host-to-Device FIS */ - fis.command =0xEC ;/* IDENTIFY */ + fis.fis_type =REG_H2D_FIS ;/* Register Host-to-Device FIS */ + fis.command =CMD_IDENTIFY ; fis.device =0 ; - fis.flags =0x80 ;/* Indicate this is a command FIS */ + fis.flags =REG_H2D_FIS_CMD ;/* Indicate this is a command FIS */ /* We've committed nothing yet, no interrupts should be posted yet. */ g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0);
tests/libqos/ahci.h +53 -0
59 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH v3 10/19 qtest/ahci: add ahci_write_fis
In reply to John SnowSimilar to ahci_set_command_header, add a helper that takes an in-memory representation of a command FIS and writes it to guest memory, handling endianness as-needed.
--- tests/ahci-test.c | 2 +- tests/libqos/ahci.c | 14 ++++++++++++++ tests/libqos/ahci.h | 3 ++- 3 files changed, 17 insertions(+), 2 deletions(-)
tests/ahci-test.c +1 -1
@@ -728,7 +728,7 @@ static void ahci_test_identify(AHCIQState *ahci) g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0);/* Commit the Command FIS to the Command Table */ -memwrite (table, &fis,sizeof (fis)); +ahci_write_fis (ahci, &fis, table);/* Commit the PRD entry to the Command Table */ memwrite (table +0x80 , &prd,sizeof (prd));
tests/libqos/ahci.c +14 -0
@@ -464,6 +464,20 @@ void ahci_destroy_command(AHCIQState *ahci, uint8_t port, uint8_t slot) ahci->port[port].prdtl[slot] =0 ; } +void ahci_write_fis (AHCIQState *ahci,RegH2DFIS *fis,uint64_t addr) +{ +RegH2DFIS tmp = *fis; + +/* The auxiliary FIS fields are defined per-command and are not +* currently implemented in libqos/ahci.o, but may or may not need +* to be flipped. */ + +/* All other FIS fields are 8 bit and do not need to be flipped. */ + tmp.count =cpu_to_le16 (tmp.count); + +memwrite (addr, &tmp,sizeof (tmp)); +} +unsigned ahci_pick_cmd (AHCIQState *ahci,uint8_t port) {unsigned i;
tests/libqos/ahci.h +2 -1
@@ -393,7 +393,7 @@ typedef struct RegH2DFIS {uint8_t icc;uint8_t control;/* DW4 */ -uint32_t aux; +uint8_t aux[4 ]; } __attribute__((__packed__)) RegH2DFIS;/**
@@ -515,6 +515,7 @@ void ahci_get_command_header(AHCIQState *ahci, uint8_t port,void ahci_set_command_header (AHCIQState *ahci,uint8_t port,uint8_t slot,AHCICommandHeader *cmd);void ahci_destroy_command (AHCIQState *ahci,uint8_t port,uint8_t slot); +void ahci_write_fis (AHCIQState *ahci,RegH2DFIS *fis,uint64_t addr);unsigned ahci_pick_cmd (AHCIQState *ahci,uint8_t port);#endif
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v3 10/19] qtest/ahci: add ahci_write_fis
Quoted 10 lines · John Snow
Similar to
ahci_set_command_header, add a helper that takes an in-memory representation of a command FIS and writes it to guest memory, handling endianness as-needed.Signed-off-by John Snow <[email protected]>--- tests/ahci-test.c | 2 +- tests/libqos/ahci.c | 14 ++++++++++++++ tests/libqos/ahci.h | 3 ++- 3 files changed, 17 insertions(+), 2 deletions(-)
PATCH v3 11/19 libqos/ahci: Add ide cmd properties
In reply to John SnowAdd a structure that defines some properties of various IDE commands. These will be used to simplify the interface to the libqos AHCI calls, lessening the redundancy of specifying and respecifying properties of commands to various helper functions.
--- tests/libqos/ahci.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)
tests/libqos/ahci.c +39 -0
@@ -34,6 +34,45 @@#include "hw/pci/pci_ids.h" #include "hw/pci/pci_regs.h" +typedef struct AHCICommandProp { +uint8_t cmd;/* Command Code */ +bool data;/* Data transfer command? */ +bool pio; +bool dma; +bool lba28; +bool lba48; +bool read; +bool write; +bool atapi; +bool ncq; +uint64_t size;/* Static transfer size, for commands like IDENTIFY. */ +uint32_t interrupts;/* Expected interrupts for this command. */ +}AHCICommandProp ; + +AHCICommandProp ahci_command_properties[] = { + { .cmd =CMD_READ_PIO , .data = true, .pio = true, + .lba28 = true, .read = true }, + { .cmd =CMD_WRITE_PIO , .data = true, .pio = true, + .lba28 = true, .write = true }, + { .cmd =CMD_READ_PIO_EXT , .data = true, .pio = true, + .lba48 = true, .read = true }, + { .cmd =CMD_WRITE_PIO_EXT , .data = true, .pio = true, + .lba48 = true, .write = true }, + { .cmd =CMD_READ_DMA , .data = true, .dma = true, + .lba28 = true, .read = true }, + { .cmd =CMD_WRITE_DMA , .data = true, .dma = true, + .lba28 = true, .write = true }, + { .cmd =CMD_READ_DMA_EXT , .data = true, .dma = true, + .lba48 = true, .read = true }, + { .cmd =CMD_WRITE_DMA_EXT , .data = true, .dma = true, + .lba48 = true, .write = true }, + { .cmd =CMD_IDENTIFY , .data = true, .pio = true, + .size =512 , .read = true }, + { .cmd =CMD_READ_MAX , .lba28 = true }, + { .cmd =CMD_READ_MAX_EXT , .lba48 = true }, + { .cmd =CMD_FLUSH_CACHE , .data = false } +}; +/** * Allocate space in the guest using information in the AHCIQState object. */
Signature · 1 line
1.9.3
PATCH v3 12/19 libqos/ahci: add ahci command functions
In reply to John SnowThis patch adds the AHCICommand structure, and a set of functions to operate on the structure.
ahci_command_create - Initialize and create a new AHCICommand in memory ahci_command_free - Destroy this object. ahci_command_set_buffer - Set where the guest memory DMA buffer is. ahci_command_commit - Write this command to the AHCI HBA. ahci_command_issue - Issue the committed command synchronously. ahci_command_issue_async - Issue the committed command asynchronously. ahci_command_wait - Wait for an asynchronous command to finish. ahci_command_slot - Get the number of the command slot we committed to.
Helpers:size_to_prdtl - Calculate the required minimum PRDTL size from
a buffer size.ahci_command_find - Given an ATA command mnemonic, look it up in the
properties table to obtain info about the command.command_header_init - Initialize the command header with sane values. command_table_init - Initialize the command table with sane values.
--- tests/ahci-test.c | 73 +++++-------------- tests/libqos/ahci.c | 202 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/libqos/ahci.h | 18 +++++ 3 files changed, 237 insertions(+), 56 deletions(-)
tests/ahci-test.c +17 -56
@@ -657,30 +657,28 @@ static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port)*/ static void ahci_test_identify (AHCIQState *ahci) { -RegH2DFIS fis; -AHCICommandHeader cmd; -PRD prd;uint32_t data_ptr;uint16_t buff[256 ];unsigned i;int rc; +AHCICommand *cmd;uint8_t cx; -uint64_t table;g_assert (ahci !=NULL );/* We need to: -* (1) Create a Command Table Buffer and update the Command List Slot #0 -* to point to this buffer. -* (2) Construct an FIS host-to-device command structure, and write it to +* (1) Create a data buffer for the IDENTIFY response to be sent to, +* (2) Create a Command Table Buffer +* (3) Construct an FIS host-to-device command structure, and write it to * the top of the command table buffer. -* (3) Create a data buffer for the IDENTIFY response to be sent to * (4) Create a Physical Region Descriptor that points to the data buffer, * and write it to the bottom (offset 0x80) of the command table. -* (5) Now, PxCLB points to the command list, command 0 points to +* (5) Obtain a Command List slot, and update this header to point to +* the Command Table we built above. +* (6) Now, PxCLB points to the command list, command 0 points to * our table, and our table contains an FIS instruction and a * PRD that points to our rx buffer. -* (6) We inform the HBA via PxCI that there is a command ready in slot #0. +* (7) We inform the HBA via PxCI that there is a command ready in slot #0. */ /* Pick the first implemented and running port */
@@ -690,61 +688,24 @@ static void ahci_test_identify(AHCIQState *ahci)/* Clear out the FIS Receive area and any pending interrupts. */ ahci_port_clear (ahci, i); -/* Create a Command Table buffer. 0x80 is the smallest with a PRDTL of 0. */ -/* We need at least one PRD, so round up to the nearest 0x80 multiple. */ - table =ahci_alloc (ahci,CMD_TBL_SIZ (1 )); -g_assert (table); -ASSERT_BIT_CLEAR (table,0x7F ); - -/* Create a data buffer ... where we will dump the IDENTIFY data to. */ +/* Create a data buffer where we will dump the IDENTIFY data to. */ data_ptr =ahci_alloc (ahci,512 );g_assert (data_ptr); -/* pick a command slot (should be 0!) */ - cx =ahci_pick_cmd (ahci, i); - -/* Construct our Command Header (set_command_header handles endianness.) */ -memset (&cmd,0x00 ,sizeof (cmd)); - cmd.flags =5 ;/* reg_h2d_fis is 5 double-words long */ - cmd.flags |=CMDH_CLR_BSY ;/* clear PxTFD.STS.BSY when done */ - cmd.prdtl =1 ;/* One PRD table entry. */ - cmd.prdbc =0 ; - cmd.ctba = table; - -/* Construct our PRD, noting that DBC is 0-indexed. */ - prd.dba =cpu_to_le64 (data_ptr); - prd.res =0 ; -/* 511+1 bytes, request DPS interrupt */ - prd.dbc =cpu_to_le32 (511 |0x80000000 ); - -/* Construct our Command FIS, Based on http://wiki.osdev.org/AHCI */ -memset (&fis,0x00 ,sizeof (fis)); - fis.fis_type =REG_H2D_FIS ;/* Register Host-to-Device FIS */ - fis.command =CMD_IDENTIFY ; - fis.device =0 ; - fis.flags =REG_H2D_FIS_CMD ;/* Indicate this is a command FIS */ - -/* We've committed nothing yet, no interrupts should be posted yet. */ - g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0); - -/* Commit the Command FIS to the Command Table */ -ahci_write_fis (ahci, &fis, table); - -/* Commit the PRD entry to the Command Table */ -memwrite (table +0x80 , &prd,sizeof (prd)); - -/* Commit Command #cx, pointing to the Table, to the Command List Buffer. */ -ahci_set_command_header (ahci, i, cx, &cmd); +/* Construct the Command Table (FIS and PRDT) and Command Header */ + cmd =ahci_command_create (CMD_IDENTIFY ); +ahci_command_set_buffer (cmd, data_ptr); +/* Write the command header and PRDT to guest memory */ +ahci_command_commit (ahci, cmd, i);/* Everything is in place, but we haven't given the go-ahead yet, * so we should find that there are no pending interrupts yet. */ g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0);/* Issue Command #cx via PxCI */ -ahci_px_wreg (ahci, i,AHCI_PX_CI , (1 << cx)); -while (BITSET (ahci_px_rreg (ahci, i,AHCI_PX_TFD ),AHCI_PX_TFD_STS_BSY )) { -usleep (50 ); - } +ahci_command_issue (ahci, cmd); + cx =ahci_command_slot (cmd); +/* Check registers for post-command consistency */ ahci_port_check_error (ahci, i);/* BUG: we expect AHCI_PX_IS_DPS to be set. */
tests/libqos/ahci.c +202 -0
205 lines not on this page · raw message
tests/libqos/ahci.h +18 -0
@@ -418,6 +418,9 @@ typedef struct PRD {uint32_t dbc;/* Data Byte Count (0-indexed) & Interrupt Flag (bit 2^31) */ } __attribute__((__packed__)) PRD; +/* Opaque, defined within ahci.c */ +typedef struct AHCICommand AHCICommand ; +/*** Macro Utilities ***/ #define BITANY (data, mask) (((data) & (mask)) != 0)#define BITSET (data, mask) (((data) & (mask)) == (mask))
@@ -517,5 +520,20 @@ void ahci_set_command_header(AHCIQState *ahci, uint8_t port,void ahci_destroy_command (AHCIQState *ahci,uint8_t port,uint8_t slot);void ahci_write_fis (AHCIQState *ahci,RegH2DFIS *fis,uint64_t addr);unsigned ahci_pick_cmd (AHCIQState *ahci,uint8_t port); +unsigned size_to_prdtl (unsigned bytes,unsigned bytes_per_prd); + +/* Command Lifecycle */ +AHCICommand *ahci_command_create (uint8_t command_name); +void ahci_command_commit (AHCIQState *ahci,AHCICommand *cmd,uint8_t port); +void ahci_command_issue (AHCIQState *ahci,AHCICommand *cmd); +void ahci_command_issue_async (AHCIQState *ahci,AHCICommand *cmd); +void ahci_command_wait (AHCIQState *ahci,AHCICommand *cmd); +void ahci_command_free (AHCICommand *cmd); + +/* Command adjustments */ +void ahci_command_set_buffer (AHCICommand *cmd,uint64_t buffer); + +/* Command Misc */ +uint8_t ahci_command_slot (AHCICommand *cmd);#endif
Signature · 1 line
1.9.3
PATCH v3 13/19 libqos/ahci: add ahci command verify
In reply to John SnowHelps to verify that a command completed successfully.
--- tests/ahci-test.c | 16 ++-------------- tests/libqos/ahci.c | 15 +++++++++++++++ tests/libqos/ahci.h | 1 + 3 files changed, 18 insertions(+), 14 deletions(-)
tests/ahci-test.c +2 -14
@@ -662,7 +662,6 @@ static void ahci_test_identify(AHCIQState *ahci)unsigned i;int rc;AHCICommand *cmd; -uint8_t cx;g_assert (ahci !=NULL );
@@ -702,20 +701,9 @@ static void ahci_test_identify(AHCIQState *ahci)* so we should find that there are no pending interrupts yet. */ g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0); -/* Issue Command #cx via PxCI */ +/* Issue command and sanity check response. */ ahci_command_issue (ahci, cmd); - cx =ahci_command_slot (cmd); - -/* Check registers for post-command consistency */ -ahci_port_check_error (ahci, i); -/* BUG: we expect AHCI_PX_IS_DPS to be set. */ -ahci_port_check_interrupts (ahci, i,AHCI_PX_IS_DHRS |AHCI_PX_IS_PSS ); -ahci_port_check_nonbusy (ahci, i, cx); -/* Investigate the CMD, assert that we read 512 bytes */ -ahci_port_check_cmd_sanity (ahci, i, cx,512 ); -/* Investigate FIS responses */ -ahci_port_check_d2h_sanity (ahci, i, cx); -ahci_port_check_pio_sanity (ahci, i, cx,512 ); +ahci_command_verify (ahci, cmd);/* Last, but not least: Investigate the IDENTIFY response data. */ memread (data_ptr, &buff,512 );
tests/libqos/ahci.c +15 -0
@@ -737,6 +737,21 @@ void ahci_command_issue(AHCIQState *ahci, AHCICommand *cmd)ahci_command_wait (ahci, cmd); } +void ahci_command_verify (AHCIQState *ahci,AHCICommand *cmd) +{ +uint8_t slot = cmd->slot; +uint8_t port = cmd->port; + +ahci_port_check_error (ahci, port); +ahci_port_check_interrupts (ahci, port, cmd->interrupts); +ahci_port_check_nonbusy (ahci, port, slot); +ahci_port_check_cmd_sanity (ahci, port, slot, cmd->xbytes); +ahci_port_check_d2h_sanity (ahci, port, slot); +if (cmd->props->pio) { +ahci_port_check_pio_sanity (ahci, port, slot, cmd->xbytes); + } +} +uint8_t ahci_command_slot (AHCICommand *cmd) {return cmd->slot;
tests/libqos/ahci.h +1 -0
@@ -528,6 +528,7 @@ void ahci_command_commit(AHCIQState *ahci, AHCICommand *cmd, uint8_t port);void ahci_command_issue (AHCIQState *ahci,AHCICommand *cmd);void ahci_command_issue_async (AHCIQState *ahci,AHCICommand *cmd);void ahci_command_wait (AHCIQState *ahci,AHCICommand *cmd); +void ahci_command_verify (AHCIQState *ahci,AHCICommand *cmd);void ahci_command_free (AHCICommand *cmd);/* Command adjustments */
Signature · 1 line
1.9.3
PATCH v3 14/19 libqos/ahci: add ahci command size setters
In reply to John SnowAdds setters for size, prd_size and both via set_sizes.
--- tests/libqos/ahci.c | 22 ++++++++++++++++++++++ tests/libqos/ahci.h | 4 ++++ 2 files changed, 26 insertions(+)
tests/libqos/ahci.c +22 -0
@@ -660,6 +660,28 @@ void ahci_command_set_buffer(AHCICommand *cmd, uint64_t buffer) cmd->buffer = buffer; } +void ahci_command_set_sizes (AHCICommand *cmd,uint64_t xbytes, +unsigned prd_size) +{ +/* Each PRD can describe up to 4MiB, and must not be odd. */ + g_assert_cmphex(prd_size, <=, 4096 * 1024); + g_assert_cmphex(prd_size & 0x01, ==, 0x00); + cmd->prd_size = prd_size; + cmd->xbytes = xbytes; + cmd->fis.count = (cmd->xbytes /AHCI_SECTOR_SIZE ); + cmd->header.prdtl =size_to_prdtl (cmd->xbytes, cmd->prd_size); +} + +void ahci_command_set_size (AHCICommand *cmd,uint64_t xbytes) +{ +ahci_command_set_sizes (cmd, xbytes, cmd->prd_size); +} + +void ahci_command_set_prd_size (AHCICommand *cmd,unsigned prd_size) +{ +ahci_command_set_sizes (cmd, cmd->xbytes, prd_size); +} +void ahci_command_commit (AHCIQState *ahci,AHCICommand *cmd,uint8_t port) {uint16_t i, prdtl;
tests/libqos/ahci.h +4 -0
@@ -533,6 +533,10 @@ void ahci_command_free(AHCICommand *cmd);/* Command adjustments */ void ahci_command_set_buffer (AHCICommand *cmd,uint64_t buffer); +void ahci_command_set_size (AHCICommand *cmd,uint64_t xbytes); +void ahci_command_set_prd_size (AHCICommand *cmd,unsigned prd_size); +void ahci_command_set_sizes (AHCICommand *cmd,uint64_t xbytes, +unsigned prd_size);/* Command Misc */ uint8_t ahci_command_slot (AHCICommand *cmd);
Signature · 1 line
1.9.3
PATCH v3 15/19 libqos/ahci: Add ahci_guest_io
In reply to John Snowahci_guest_io is a shorthand function that will, in one shot, execute a data command on the guest to the specified guest buffer location, in the requested amount.
--- tests/libqos/ahci.c | 15 +++++++++++++++ tests/libqos/ahci.h | 2 ++ 2 files changed, 17 insertions(+)
tests/libqos/ahci.c +15 -0
@@ -548,6 +548,21 @@ inline unsigned size_to_prdtl(unsigned bytes, unsigned bytes_per_prd)return (bytes + bytes_per_prd -1 ) / bytes_per_prd; } +/* Given a guest buffer address, perform an IO operation */ +void ahci_guest_io (AHCIQState *ahci,uint8_t port,uint8_t ide_cmd, +uint64_t buffer,size_t bufsize) +{ +AHCICommand *cmd; + + cmd =ahci_command_create (ide_cmd); +ahci_command_set_buffer (cmd, buffer); +ahci_command_set_size (cmd, bufsize); +ahci_command_commit (ahci, cmd, port); +ahci_command_issue (ahci, cmd); +ahci_command_verify (ahci, cmd); +ahci_command_free (cmd); +} + typedef struct AHCICommand { /* Test Management Data */ uint8_t name;
tests/libqos/ahci.h +2 -0
@@ -521,6 +521,8 @@ void ahci_destroy_command(AHCIQState *ahci, uint8_t port, uint8_t slot);void ahci_write_fis (AHCIQState *ahci,RegH2DFIS *fis,uint64_t addr);unsigned ahci_pick_cmd (AHCIQState *ahci,uint8_t port);unsigned size_to_prdtl (unsigned bytes,unsigned bytes_per_prd); +void ahci_guest_io (AHCIQState *ahci,uint8_t port,uint8_t ide_cmd, +uint64_t gbuffer,size_t size);/* Command Lifecycle */ AHCICommand *ahci_command_create (uint8_t command_name);
Signature · 1 line
1.9.3
PATCH v3 16/19 libqos/ahci: add ahci_io
In reply to John Snowahci_io is a wrapper around ahci_guest_io that takes a pointer to host memory instead, and will create a guest memory buffer and copy the data to/from as needed and as appropriate for a read/write command, such that after a read, the guest data will be in a host buffer, and for a write, the data will be transmitted to guest memory prior to the block operation.
Now that we have all the syntactic sugar functions in place for AHCI, we can convert the identify test to be very, very short.
--- tests/ahci-test.c | 63 +++++++++++++++++++++-------------------------------- tests/libqos/ahci.c | 25 +++++++++++++++++++++ tests/libqos/ahci.h | 2 ++ 3 files changed, 52 insertions(+), 38 deletions(-)
tests/ahci-test.c +25 -38
@@ -657,56 +657,43 @@ static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port)*/ static void ahci_test_identify (AHCIQState *ahci) { -uint32_t data_ptr;uint16_t buff[256 ]; -unsigned i; +unsigned px;int rc; -AHCICommand *cmd; +const size_t buffsize =512 ;g_assert (ahci !=NULL ); -/* We need to: -* (1) Create a data buffer for the IDENTIFY response to be sent to, -* (2) Create a Command Table Buffer +/** +* This serves as a bit of a tutorial on AHCI device programming: +* +* (1) Create a data buffer for the IDENTIFY response to be sent to +* (2) Create a Command Table buffer, where we will store the +* command and PRDT (Physical Region Descriptor Table) * (3) Construct an FIS host-to-device command structure, and write it to -* the top of the command table buffer. -* (4) Create a Physical Region Descriptor that points to the data buffer, -* and write it to the bottom (offset 0x80) of the command table. -* (5) Obtain a Command List slot, and update this header to point to -* the Command Table we built above. -* (6) Now, PxCLB points to the command list, command 0 points to -* our table, and our table contains an FIS instruction and a -* PRD that points to our rx buffer. -* (7) We inform the HBA via PxCI that there is a command ready in slot #0. +* the top of the Command Table buffer. +* (4) Create one or more Physical Region Descriptors (PRDs) that describe +* a location in memory where data may be stored/retrieved. +* (5) Write these PRDTs to the bottom (offset 0x80) of the Command Table. +* (6) Each AHCI port has up to 32 command slots. Each slot contains a +* header that points to a Command Table buffer. Pick an unused slot +* and update it to point to the Command Table we have built. +* (7) Now: Command #n points to our Command Table, and our Command Table +* contains the FIS (that describes our command) and the PRDTL, which +* describes our buffer. +* (8) We inform the HBA via PxCI (Command Issue) that the command in slot +* #n is ready for processing. */ /* Pick the first implemented and running port */ - i =ahci_port_select (ahci); -g_test_message ("Selected port %u for test" , i); + px =ahci_port_select (ahci); +g_test_message ("Selected port %u for test" , px);/* Clear out the FIS Receive area and any pending interrupts. */ -ahci_port_clear (ahci, i); +ahci_port_clear (ahci, px); -/* Create a data buffer where we will dump the IDENTIFY data to. */ - data_ptr =ahci_alloc (ahci,512 ); -g_assert (data_ptr); - -/* Construct the Command Table (FIS and PRDT) and Command Header */ - cmd =ahci_command_create (CMD_IDENTIFY ); -ahci_command_set_buffer (cmd, data_ptr); -/* Write the command header and PRDT to guest memory */ -ahci_command_commit (ahci, cmd, i); - -/* Everything is in place, but we haven't given the go-ahead yet, -* so we should find that there are no pending interrupts yet. */ - g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0); - -/* Issue command and sanity check response. */ -ahci_command_issue (ahci, cmd); -ahci_command_verify (ahci, cmd); - -/* Last, but not least: Investigate the IDENTIFY response data. */ -memread (data_ptr, &buff,512 ); +/* "Read" 512 bytes using CMD_IDENTIFY into the host buffer. */ +ahci_io (ahci, px,CMD_IDENTIFY , &buff, buffsize);/* Check serial number/version in the buffer */ /* NB: IDENTIFY strings are packed in 16bit little endian chunks.
tests/libqos/ahci.c +25 -0
@@ -592,6 +592,31 @@ static AHCICommandProp *ahci_command_find(uint8_t command_name)return NULL ; } +/* Given a HOST buffer, create a buffer address and perform an IO operation. */ +void ahci_io (AHCIQState *ahci,uint8_t port,uint8_t ide_cmd, +void *buffer,size_t bufsize) +{ +uint64_t ptr; +AHCICommandProp *props; + + props =ahci_command_find (ide_cmd); +g_assert (props); + ptr =ahci_alloc (ahci, bufsize); +g_assert (ptr); + +if (props->write) { +memwrite (ptr, buffer, bufsize); + } + +ahci_guest_io (ahci, port, ide_cmd, ptr, bufsize); + +if (props->read) { +memread (ptr, buffer, bufsize); + } + +ahci_free (ahci, ptr); +} +/** * Initializes a basic command header in memory. * We assume that this is for an ATA command using RegH2DFIS.
tests/libqos/ahci.h +2 -0
@@ -523,6 +523,8 @@ unsigned ahci_pick_cmd(AHCIQState *ahci, uint8_t port);unsigned size_to_prdtl (unsigned bytes,unsigned bytes_per_prd);void ahci_guest_io (AHCIQState *ahci,uint8_t port,uint8_t ide_cmd,uint64_t gbuffer,size_t size); +void ahci_io (AHCIQState *ahci,uint8_t port,uint8_t ide_cmd, +void *buffer,size_t bufsize);/* Command Lifecycle */ AHCICommand *ahci_command_create (uint8_t command_name);
Signature · 1 line
1.9.3
PATCH v3 17/19 libqos/ahci: Add ahci_clean_mem
In reply to John SnowClean up guest memory being used in ahci_clean_mem, to be called during ahci_shutdown. With all guest memory leaks removed, add an option to the allocator to throw an assertion if a leak occurs.
This test adds some sanity to both the AHCI library and the allocator.
--- tests/ahci-test.c | 3 +++ tests/libqos/ahci.c | 18 ++++++++++++++++++ tests/libqos/ahci.h | 1 + tests/libqos/malloc.c | 5 +++++ tests/libqos/malloc.h | 1 + 5 files changed, 28 insertions(+)
tests/ahci-test.c +3 -0
@@ -86,6 +86,7 @@ static AHCIQState *ahci_boot(void)"-device ide-hd,drive=drive0 " "-global ide-hd.ver=%s" ; s->parent =qtest_pc_boot (cli, tmp_path,"testdisk" ,"version" ); +alloc_set_flags (s->parent->alloc,ALLOC_LEAK_ASSERT );/* Verify that we have an AHCI device present. */ s->dev =get_ahci_device (&s->fingerprint);
@@ -99,6 +100,8 @@ static AHCIQState *ahci_boot(void)static void ahci_shutdown (AHCIQState *ahci) {QOSState *qs = ahci->parent; + +ahci_clean_mem (ahci);free_ahci_device (ahci->dev);g_free (ahci);qtest_shutdown (qs);
tests/libqos/ahci.c +18 -0
@@ -130,6 +130,24 @@ void free_ahci_device(QPCIDevice *dev)qpci_free_pc (pcibus); } +/* Free all memory in-use by the AHCI device. */ +void ahci_clean_mem (AHCIQState *ahci) +{ +uint8_t port, slot; + +for (port =0 ; port <32 ; ++port) { +if (ahci->port[port].fb) { +ahci_free (ahci, ahci->port[port].fb); + } +if (ahci->port[port].clb) { +for (slot =0 ; slot <32 ; slot++) { +ahci_destroy_command (ahci, port, slot); + } +ahci_free (ahci, ahci->port[port].clb); + } + } +} +/*** Logical Device Initialization ***/ /**
tests/libqos/ahci.h +1 -0
@@ -499,6 +499,7 @@ uint64_t ahci_alloc(AHCIQState *ahci, size_t bytes);void ahci_free (AHCIQState *ahci,uint64_t addr);QPCIDevice *get_ahci_device (uint32_t *fingerprint);void free_ahci_device (QPCIDevice *dev); +void ahci_clean_mem (AHCIQState *ahci);void ahci_pci_enable (AHCIQState *ahci);void start_ahci_device (AHCIQState *ahci);void ahci_hba_enable (AHCIQState *ahci);
tests/libqos/malloc.c +5 -0
@@ -324,3 +324,8 @@ void alloc_set_page_size(QGuestAllocator *allocator, size_t page_size)g_assert (is_power_of_2 (page_size)); allocator->page_size = page_size; } + +void alloc_set_flags (QGuestAllocator *allocator,QAllocOpts opts) +{ + allocator->opts |= opts; +}
tests/libqos/malloc.h +1 -0
@@ -36,5 +36,6 @@ QGuestAllocator *alloc_init(uint64_t start, uint64_t end);QGuestAllocator *alloc_init_flags (QAllocOpts flags,uint64_t start,uint64_t end);void alloc_set_page_size (QGuestAllocator *allocator,size_t page_size); +void alloc_set_flags (QGuestAllocator *allocator,QAllocOpts opts);#endif
Signature · 1 line
1.9.3
PATCH v3 18/19 qtest/ahci: Assert sector size in identify test
In reply to John SnowA minor sanity check to assert that the sector size is 512. The current block layer code deeply assumes that the IDE sector size will be 512 bytes, so we carry forward that assumption here.
This is useful for the DMA tests, which currently assume that a sector will always be 512 bytes.
--- tests/ahci-test.c | 4 ++++ 1 file changed, 4 insertions(+)
tests/ahci-test.c +4 -0
@@ -663,6 +663,7 @@ static void ahci_test_identify(AHCIQState *ahci)uint16_t buff[256 ];unsigned px;int rc; +uint16_t sect_size;const size_t buffsize =512 ;g_assert (ahci !=NULL );
@@ -710,6 +711,9 @@ static void ahci_test_identify(AHCIQState *ahci)string_bswap16 (&buff[23 ],8 ); rc =memcmp (&buff[23 ],"version " ,8 ); g_assert_cmphex(rc, ==, 0); + + sect_size =le16_to_cpu (*((uint16_t *)(&buff[5 ]))); + g_assert_cmphex(sect_size, ==, 0x200); }/******************************************************************************/
Signature · 1 line
1.9.3
PATCH v3 19/19 qtest/ahci: Adding simple dma read-write test
In reply to John SnowAdds a test case for AHCI wherein we write a 4K block of a changing pattern to sector 0, then read back that 4K and compare the transmit and receive buffers.
--- tests/ahci-test.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+)
tests/ahci-test.c +55 -0
@@ -716,6 +716,46 @@ static void ahci_test_identify(AHCIQState *ahci) g_assert_cmphex(sect_size, ==, 0x200); } +static void ahci_test_dma_rw_simple (AHCIQState *ahci) +{ +uint64_t ptr; +uint8_t port; +unsigned i; +const unsigned bufsize =4096 ; +unsigned char *tx =g_malloc (bufsize); +unsigned char *rx =g_malloc0 (bufsize); + +g_assert (ahci !=NULL ); + +/* Pick the first running port and clear it. */ + port =ahci_port_select (ahci); +ahci_port_clear (ahci, port); + +/*** Create pattern and transfer to guest ***/ +/* Data buffer in the guest */ + ptr =ahci_alloc (ahci, bufsize); +g_assert (ptr); + +/* Write some indicative pattern to our 4K buffer. */ +for (i =0 ; i < bufsize; i++) { + tx[i] = (bufsize - i); + } +memwrite (ptr, tx, bufsize); + +/* Write this buffer to disk, then read it back to the DMA buffer. */ +ahci_guest_io (ahci, port,CMD_WRITE_DMA , ptr, bufsize); +qmemset (ptr,0x00 , bufsize); +ahci_guest_io (ahci, port,CMD_READ_DMA , ptr, bufsize); + +/*** Read back the Data ***/ +memread (ptr, rx, bufsize); + g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0); + +ahci_free (ahci, ptr); +g_free (tx); +g_free (rx); +} +/******************************************************************************/ /* Test Interfaces */ /******************************************************************************/
@@ -798,6 +838,20 @@ static void test_identify(void)ahci_shutdown (ahci); } +/** +* Perform a simple DMA R/W test, using a single PRD and non-NCQ commands. +*/ +static void test_dma_rw_simple (void ) +{ +AHCIQState *ahci; + + ahci =ahci_boot (); +ahci_pci_enable (ahci); +ahci_hba_enable (ahci); +ahci_test_dma_rw_simple (ahci); +ahci_shutdown (ahci); +} +/******************************************************************************/ int main (int argc,char **argv)
@@ -853,6 +907,7 @@ int main(int argc, char **argv)qtest_add_func ("/ahci/hba_spec" , test_hba_spec);qtest_add_func ("/ahci/hba_enable" , test_hba_enable);qtest_add_func ("/ahci/identify" , test_identify); +qtest_add_func ("/ahci/dma/simple" , test_dma_rw_simple); ret =g_test_run ();
Signature · 1 line
1.9.3
Stefan Hajnoczi
AppliedQuoted 61 lines · John Snow
Add a simple DMA r/w test to ahci-test.
Oh, and for the first 18 patches, refactor everything into helpers so that each
ahci_testisn't a thousand lines long.This patch depends upon the "ahci test preliminary refactoring" series upstream, which shuffled a lot of libqos and malloc facilities to support this series.
This patchset is a necessary step in checking in AHCI/DMA migration tests that I will later use as proof as suitability of enabling the ICH9 and AHCI migration flags.
V3:
(04) Changed "px" to "port" and "cx" to "slot" In this and all other patches. Kept R-Bys in other patches as this is a non-functional change.
(10) Modified aux to be an array of four bytes, and added a note explaining that they are for NCQ and not currently used in libqos.
(12) Minor formatting and updating aux initialization. Kept R-B.V2:
(03) Addedcpu_to_le16into intermediary code.
(04) Fixed assignment to be or-equals.ahci_set_command_headerno longer modifies its argument.
(09) Fixed assignment to be or-equals in the helper refactor.
(10) New patch. Writes an FIS to guest memory, handling endianness.
(11) Remove the property table sentinel
updatefind_commandto use the array size instead.
(12) Removecpu_to_le16fromahci_command_commit,
leave endianness toahci_write_fis.
(14) Removecpu_to_le16fromahci_set_sizes,
leave endianness toahci_write_fis.
(19) qmemset the buffer to zeroes between IO calls,
just to make sure.
(--) Removed theahci_macro_bootfunction patch(...for now...)(--) Retested entire stack of patches on F20/PPC.
~John
John Snow (19):
libqos/ahci: Addahci_port_selecthelper libqos/ahci: Addahci_port_clearhelper qtest/ahci: rename 'Command' to 'CommandHeader' libqos/ahci: Add command header helpers libqos/ahci: Addahci_port_check_errorhelper libqos/ahci: Addahci_port_check_interruptshelper libqos/ahci: Addport_check_nonbusyhelper libqos/ahci: Add cmd response sanity check helpers qtest/ahci: Demagic ahci tests. qtest/ahci: addahci_write_fislibqos/ahci: Add ide cmd properties libqos/ahci: add ahci command functions libqos/ahci: add ahci command verify libqos/ahci: add ahci command size setters libqos/ahci: Addahci_guest_iolibqos/ahci: addahci_io
libqos/ahci: Addahci_clean_memqtest/ahci: Assert sector size in identify test qtest/ahci: Adding simple dma read-write test
Quoted 6 lines
tests/ahci-test.c | 230 ++++++++------------ tests/libqos/ahci.c | 569 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/libqos/ahci.h | 168 ++++++++++++--- tests/libqos/malloc.c | 5 + tests/libqos/malloc.h | 1 + 5 files changed, 804 insertions(+), 169 deletions(-)
Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block
Stefan