2
0

ahci-test.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  1. /*
  2. * AHCI test cases
  3. *
  4. * Copyright (c) 2014 John Snow <jsnow@redhat.com>
  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. #include "qemu/osdep.h"
  25. #include <getopt.h>
  26. #include "libqtest.h"
  27. #include "libqos/libqos-pc.h"
  28. #include "libqos/ahci.h"
  29. #include "libqos/pci-pc.h"
  30. #include "qemu-common.h"
  31. #include "qapi/qmp/qdict.h"
  32. #include "qemu/host-utils.h"
  33. #include "hw/pci/pci_ids.h"
  34. #include "hw/pci/pci_regs.h"
  35. /* TODO actually test the results and get rid of this */
  36. #define qmp_discard_response(s, ...) qobject_unref(qtest_qmp(s, __VA_ARGS__))
  37. /* Test images sizes in MB */
  38. #define TEST_IMAGE_SIZE_MB_LARGE (200 * 1024)
  39. #define TEST_IMAGE_SIZE_MB_SMALL 64
  40. /*** Globals ***/
  41. static char tmp_path[] = "/tmp/qtest.XXXXXX";
  42. static char debug_path[] = "/tmp/qtest-blkdebug.XXXXXX";
  43. static char mig_socket[] = "/tmp/qtest-migration.XXXXXX";
  44. static bool ahci_pedantic;
  45. static const char *imgfmt;
  46. static unsigned test_image_size_mb;
  47. /*** Function Declarations ***/
  48. static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port);
  49. static void ahci_test_pci_spec(AHCIQState *ahci);
  50. static void ahci_test_pci_caps(AHCIQState *ahci, uint16_t header,
  51. uint8_t offset);
  52. static void ahci_test_satacap(AHCIQState *ahci, uint8_t offset);
  53. static void ahci_test_msicap(AHCIQState *ahci, uint8_t offset);
  54. static void ahci_test_pmcap(AHCIQState *ahci, uint8_t offset);
  55. /*** Utilities ***/
  56. static uint64_t mb_to_sectors(uint64_t image_size_mb)
  57. {
  58. return (image_size_mb * 1024 * 1024) / AHCI_SECTOR_SIZE;
  59. }
  60. static void string_bswap16(uint16_t *s, size_t bytes)
  61. {
  62. g_assert_cmphex((bytes & 1), ==, 0);
  63. bytes /= 2;
  64. while (bytes--) {
  65. *s = bswap16(*s);
  66. s++;
  67. }
  68. }
  69. /**
  70. * Verify that the transfer did not corrupt our state at all.
  71. */
  72. static void verify_state(AHCIQState *ahci, uint64_t hba_old)
  73. {
  74. int i, j;
  75. uint32_t ahci_fingerprint;
  76. uint64_t hba_base;
  77. AHCICommandHeader cmd;
  78. ahci_fingerprint = qpci_config_readl(ahci->dev, PCI_VENDOR_ID);
  79. g_assert_cmphex(ahci_fingerprint, ==, ahci->fingerprint);
  80. /* If we haven't initialized, this is as much as can be validated. */
  81. if (!ahci->enabled) {
  82. return;
  83. }
  84. hba_base = (uint64_t)qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
  85. g_assert_cmphex(hba_base, ==, hba_old);
  86. g_assert_cmphex(ahci_rreg(ahci, AHCI_CAP), ==, ahci->cap);
  87. g_assert_cmphex(ahci_rreg(ahci, AHCI_CAP2), ==, ahci->cap2);
  88. for (i = 0; i < 32; i++) {
  89. g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_FB), ==,
  90. ahci->port[i].fb);
  91. g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_CLB), ==,
  92. ahci->port[i].clb);
  93. for (j = 0; j < 32; j++) {
  94. ahci_get_command_header(ahci, i, j, &cmd);
  95. g_assert_cmphex(cmd.prdtl, ==, ahci->port[i].prdtl[j]);
  96. g_assert_cmphex(cmd.ctba, ==, ahci->port[i].ctba[j]);
  97. }
  98. }
  99. }
  100. static void ahci_migrate(AHCIQState *from, AHCIQState *to, const char *uri)
  101. {
  102. QOSState *tmp = to->parent;
  103. QPCIDevice *dev = to->dev;
  104. char *uri_local = NULL;
  105. uint64_t hba_old;
  106. if (uri == NULL) {
  107. uri_local = g_strdup_printf("%s%s", "unix:", mig_socket);
  108. uri = uri_local;
  109. }
  110. hba_old = (uint64_t)qpci_config_readl(from->dev, PCI_BASE_ADDRESS_5);
  111. /* context will be 'to' after completion. */
  112. migrate(from->parent, to->parent, uri);
  113. /* We'd like for the AHCIState objects to still point
  114. * to information specific to its specific parent
  115. * instance, but otherwise just inherit the new data. */
  116. memcpy(to, from, sizeof(AHCIQState));
  117. to->parent = tmp;
  118. to->dev = dev;
  119. tmp = from->parent;
  120. dev = from->dev;
  121. memset(from, 0x00, sizeof(AHCIQState));
  122. from->parent = tmp;
  123. from->dev = dev;
  124. verify_state(to, hba_old);
  125. g_free(uri_local);
  126. }
  127. /*** Test Setup & Teardown ***/
  128. /**
  129. * Start a Q35 machine and bookmark a handle to the AHCI device.
  130. */
  131. static AHCIQState *ahci_vboot(const char *cli, va_list ap)
  132. {
  133. AHCIQState *s;
  134. s = g_new0(AHCIQState, 1);
  135. s->parent = qtest_pc_vboot(cli, ap);
  136. alloc_set_flags(&s->parent->alloc, ALLOC_LEAK_ASSERT);
  137. /* Verify that we have an AHCI device present. */
  138. s->dev = get_ahci_device(s->parent->qts, &s->fingerprint);
  139. return s;
  140. }
  141. /**
  142. * Start a Q35 machine and bookmark a handle to the AHCI device.
  143. */
  144. static AHCIQState *ahci_boot(const char *cli, ...)
  145. {
  146. AHCIQState *s;
  147. va_list ap;
  148. if (cli) {
  149. va_start(ap, cli);
  150. s = ahci_vboot(cli, ap);
  151. va_end(ap);
  152. } else {
  153. cli = "-drive if=none,id=drive0,file=%s,cache=writeback,format=%s"
  154. " -M q35 "
  155. "-device ide-hd,drive=drive0 "
  156. "-global ide-hd.serial=%s "
  157. "-global ide-hd.ver=%s";
  158. s = ahci_boot(cli, tmp_path, imgfmt, "testdisk", "version");
  159. }
  160. return s;
  161. }
  162. /**
  163. * Clean up the PCI device, then terminate the QEMU instance.
  164. */
  165. static void ahci_shutdown(AHCIQState *ahci)
  166. {
  167. QOSState *qs = ahci->parent;
  168. ahci_clean_mem(ahci);
  169. free_ahci_device(ahci->dev);
  170. g_free(ahci);
  171. qtest_shutdown(qs);
  172. }
  173. /**
  174. * Boot and fully enable the HBA device.
  175. * @see ahci_boot, ahci_pci_enable and ahci_hba_enable.
  176. */
  177. static AHCIQState *ahci_boot_and_enable(const char *cli, ...)
  178. {
  179. AHCIQState *ahci;
  180. va_list ap;
  181. uint16_t buff[256];
  182. uint8_t port;
  183. uint8_t hello;
  184. if (cli) {
  185. va_start(ap, cli);
  186. ahci = ahci_vboot(cli, ap);
  187. va_end(ap);
  188. } else {
  189. ahci = ahci_boot(NULL);
  190. }
  191. ahci_pci_enable(ahci);
  192. ahci_hba_enable(ahci);
  193. /* Initialize test device */
  194. port = ahci_port_select(ahci);
  195. ahci_port_clear(ahci, port);
  196. if (is_atapi(ahci, port)) {
  197. hello = CMD_PACKET_ID;
  198. } else {
  199. hello = CMD_IDENTIFY;
  200. }
  201. ahci_io(ahci, port, hello, &buff, sizeof(buff), 0);
  202. return ahci;
  203. }
  204. /*** Specification Adherence Tests ***/
  205. /**
  206. * Implementation for test_pci_spec. Ensures PCI configuration space is sane.
  207. */
  208. static void ahci_test_pci_spec(AHCIQState *ahci)
  209. {
  210. uint8_t datab;
  211. uint16_t data;
  212. uint32_t datal;
  213. /* Most of these bits should start cleared until we turn them on. */
  214. data = qpci_config_readw(ahci->dev, PCI_COMMAND);
  215. ASSERT_BIT_CLEAR(data, PCI_COMMAND_MEMORY);
  216. ASSERT_BIT_CLEAR(data, PCI_COMMAND_MASTER);
  217. ASSERT_BIT_CLEAR(data, PCI_COMMAND_SPECIAL); /* Reserved */
  218. ASSERT_BIT_CLEAR(data, PCI_COMMAND_VGA_PALETTE); /* Reserved */
  219. ASSERT_BIT_CLEAR(data, PCI_COMMAND_PARITY);
  220. ASSERT_BIT_CLEAR(data, PCI_COMMAND_WAIT); /* Reserved */
  221. ASSERT_BIT_CLEAR(data, PCI_COMMAND_SERR);
  222. ASSERT_BIT_CLEAR(data, PCI_COMMAND_FAST_BACK);
  223. ASSERT_BIT_CLEAR(data, PCI_COMMAND_INTX_DISABLE);
  224. ASSERT_BIT_CLEAR(data, 0xF800); /* Reserved */
  225. data = qpci_config_readw(ahci->dev, PCI_STATUS);
  226. ASSERT_BIT_CLEAR(data, 0x01 | 0x02 | 0x04); /* Reserved */
  227. ASSERT_BIT_CLEAR(data, PCI_STATUS_INTERRUPT);
  228. ASSERT_BIT_SET(data, PCI_STATUS_CAP_LIST); /* must be set */
  229. ASSERT_BIT_CLEAR(data, PCI_STATUS_UDF); /* Reserved */
  230. ASSERT_BIT_CLEAR(data, PCI_STATUS_PARITY);
  231. ASSERT_BIT_CLEAR(data, PCI_STATUS_SIG_TARGET_ABORT);
  232. ASSERT_BIT_CLEAR(data, PCI_STATUS_REC_TARGET_ABORT);
  233. ASSERT_BIT_CLEAR(data, PCI_STATUS_REC_MASTER_ABORT);
  234. ASSERT_BIT_CLEAR(data, PCI_STATUS_SIG_SYSTEM_ERROR);
  235. ASSERT_BIT_CLEAR(data, PCI_STATUS_DETECTED_PARITY);
  236. /* RID occupies the low byte, CCs occupy the high three. */
  237. datal = qpci_config_readl(ahci->dev, PCI_CLASS_REVISION);
  238. if (ahci_pedantic) {
  239. /* AHCI 1.3 specifies that at-boot, the RID should reset to 0x00,
  240. * Though in practice this is likely seldom true. */
  241. ASSERT_BIT_CLEAR(datal, 0xFF);
  242. }
  243. /* BCC *must* equal 0x01. */
  244. g_assert_cmphex(PCI_BCC(datal), ==, 0x01);
  245. if (PCI_SCC(datal) == 0x01) {
  246. /* IDE */
  247. ASSERT_BIT_SET(0x80000000, datal);
  248. ASSERT_BIT_CLEAR(0x60000000, datal);
  249. } else if (PCI_SCC(datal) == 0x04) {
  250. /* RAID */
  251. g_assert_cmphex(PCI_PI(datal), ==, 0);
  252. } else if (PCI_SCC(datal) == 0x06) {
  253. /* AHCI */
  254. g_assert_cmphex(PCI_PI(datal), ==, 0x01);
  255. } else {
  256. g_assert_not_reached();
  257. }
  258. datab = qpci_config_readb(ahci->dev, PCI_CACHE_LINE_SIZE);
  259. g_assert_cmphex(datab, ==, 0);
  260. datab = qpci_config_readb(ahci->dev, PCI_LATENCY_TIMER);
  261. g_assert_cmphex(datab, ==, 0);
  262. /* Only the bottom 7 bits must be off. */
  263. datab = qpci_config_readb(ahci->dev, PCI_HEADER_TYPE);
  264. ASSERT_BIT_CLEAR(datab, 0x7F);
  265. /* BIST is optional, but the low 7 bits must always start off regardless. */
  266. datab = qpci_config_readb(ahci->dev, PCI_BIST);
  267. ASSERT_BIT_CLEAR(datab, 0x7F);
  268. /* BARS 0-4 do not have a boot spec, but ABAR/BAR5 must be clean. */
  269. datal = qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
  270. g_assert_cmphex(datal, ==, 0);
  271. qpci_config_writel(ahci->dev, PCI_BASE_ADDRESS_5, 0xFFFFFFFF);
  272. datal = qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
  273. /* ABAR must be 32-bit, memory mapped, non-prefetchable and
  274. * must be >= 512 bytes. To that end, bits 0-8 must be off. */
  275. ASSERT_BIT_CLEAR(datal, 0xFF);
  276. /* Capability list MUST be present, */
  277. datal = qpci_config_readl(ahci->dev, PCI_CAPABILITY_LIST);
  278. /* But these bits are reserved. */
  279. ASSERT_BIT_CLEAR(datal, ~0xFF);
  280. g_assert_cmphex(datal, !=, 0);
  281. /* Check specification adherence for capability extenstions. */
  282. data = qpci_config_readw(ahci->dev, datal);
  283. switch (ahci->fingerprint) {
  284. case AHCI_INTEL_ICH9:
  285. /* Intel ICH9 Family Datasheet 14.1.19 p.550 */
  286. g_assert_cmphex((data & 0xFF), ==, PCI_CAP_ID_MSI);
  287. break;
  288. default:
  289. /* AHCI 1.3, Section 2.1.14 -- CAP must point to PMCAP. */
  290. g_assert_cmphex((data & 0xFF), ==, PCI_CAP_ID_PM);
  291. }
  292. ahci_test_pci_caps(ahci, data, (uint8_t)datal);
  293. /* Reserved. */
  294. datal = qpci_config_readl(ahci->dev, PCI_CAPABILITY_LIST + 4);
  295. g_assert_cmphex(datal, ==, 0);
  296. /* IPIN might vary, but ILINE must be off. */
  297. datab = qpci_config_readb(ahci->dev, PCI_INTERRUPT_LINE);
  298. g_assert_cmphex(datab, ==, 0);
  299. }
  300. /**
  301. * Test PCI capabilities for AHCI specification adherence.
  302. */
  303. static void ahci_test_pci_caps(AHCIQState *ahci, uint16_t header,
  304. uint8_t offset)
  305. {
  306. uint8_t cid = header & 0xFF;
  307. uint8_t next = header >> 8;
  308. g_test_message("CID: %02x; next: %02x", cid, next);
  309. switch (cid) {
  310. case PCI_CAP_ID_PM:
  311. ahci_test_pmcap(ahci, offset);
  312. break;
  313. case PCI_CAP_ID_MSI:
  314. ahci_test_msicap(ahci, offset);
  315. break;
  316. case PCI_CAP_ID_SATA:
  317. ahci_test_satacap(ahci, offset);
  318. break;
  319. default:
  320. g_test_message("Unknown CAP 0x%02x", cid);
  321. }
  322. if (next) {
  323. ahci_test_pci_caps(ahci, qpci_config_readw(ahci->dev, next), next);
  324. }
  325. }
  326. /**
  327. * Test SATA PCI capabilitity for AHCI specification adherence.
  328. */
  329. static void ahci_test_satacap(AHCIQState *ahci, uint8_t offset)
  330. {
  331. uint16_t dataw;
  332. uint32_t datal;
  333. g_test_message("Verifying SATACAP");
  334. /* Assert that the SATACAP version is 1.0, And reserved bits are empty. */
  335. dataw = qpci_config_readw(ahci->dev, offset + 2);
  336. g_assert_cmphex(dataw, ==, 0x10);
  337. /* Grab the SATACR1 register. */
  338. datal = qpci_config_readw(ahci->dev, offset + 4);
  339. switch (datal & 0x0F) {
  340. case 0x04: /* BAR0 */
  341. case 0x05: /* BAR1 */
  342. case 0x06:
  343. case 0x07:
  344. case 0x08:
  345. case 0x09: /* BAR5 */
  346. case 0x0F: /* Immediately following SATACR1 in PCI config space. */
  347. break;
  348. default:
  349. /* Invalid BARLOC for the Index Data Pair. */
  350. g_assert_not_reached();
  351. }
  352. /* Reserved. */
  353. g_assert_cmphex((datal >> 24), ==, 0x00);
  354. }
  355. /**
  356. * Test MSI PCI capability for AHCI specification adherence.
  357. */
  358. static void ahci_test_msicap(AHCIQState *ahci, uint8_t offset)
  359. {
  360. uint16_t dataw;
  361. uint32_t datal;
  362. g_test_message("Verifying MSICAP");
  363. dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_FLAGS);
  364. ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_ENABLE);
  365. ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_QSIZE);
  366. ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_RESERVED);
  367. datal = qpci_config_readl(ahci->dev, offset + PCI_MSI_ADDRESS_LO);
  368. g_assert_cmphex(datal, ==, 0);
  369. if (dataw & PCI_MSI_FLAGS_64BIT) {
  370. g_test_message("MSICAP is 64bit");
  371. datal = qpci_config_readl(ahci->dev, offset + PCI_MSI_ADDRESS_HI);
  372. g_assert_cmphex(datal, ==, 0);
  373. dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_DATA_64);
  374. g_assert_cmphex(dataw, ==, 0);
  375. } else {
  376. g_test_message("MSICAP is 32bit");
  377. dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_DATA_32);
  378. g_assert_cmphex(dataw, ==, 0);
  379. }
  380. }
  381. /**
  382. * Test Power Management PCI capability for AHCI specification adherence.
  383. */
  384. static void ahci_test_pmcap(AHCIQState *ahci, uint8_t offset)
  385. {
  386. uint16_t dataw;
  387. g_test_message("Verifying PMCAP");
  388. dataw = qpci_config_readw(ahci->dev, offset + PCI_PM_PMC);
  389. ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_PME_CLOCK);
  390. ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_RESERVED);
  391. ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_D1);
  392. ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_D2);
  393. dataw = qpci_config_readw(ahci->dev, offset + PCI_PM_CTRL);
  394. ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_STATE_MASK);
  395. ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_RESERVED);
  396. ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_DATA_SEL_MASK);
  397. ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_DATA_SCALE_MASK);
  398. }
  399. static void ahci_test_hba_spec(AHCIQState *ahci)
  400. {
  401. unsigned i;
  402. uint32_t reg;
  403. uint32_t ports;
  404. uint8_t nports_impl;
  405. uint8_t maxports;
  406. g_assert(ahci != NULL);
  407. /*
  408. * Note that the AHCI spec does expect the BIOS to set up a few things:
  409. * CAP.SSS - Support for staggered spin-up (t/f)
  410. * CAP.SMPS - Support for mechanical presence switches (t/f)
  411. * PI - Ports Implemented (1-32)
  412. * PxCMD.HPCP - Hot Plug Capable Port
  413. * PxCMD.MPSP - Mechanical Presence Switch Present
  414. * PxCMD.CPD - Cold Presence Detection support
  415. *
  416. * Additional items are touched if CAP.SSS is on, see AHCI 10.1.1 p.97:
  417. * Foreach Port Implemented:
  418. * -PxCMD.ST, PxCMD.CR, PxCMD.FRE, PxCMD.FR, PxSCTL.DET are 0
  419. * -PxCLB/U and PxFB/U are set to valid regions in memory
  420. * -PxSUD is set to 1.
  421. * -PxSSTS.DET is polled for presence; if detected, we continue:
  422. * -PxSERR is cleared with 1's.
  423. * -If PxTFD.STS.BSY, PxTFD.STS.DRQ, and PxTFD.STS.ERR are all zero,
  424. * the device is ready.
  425. */
  426. /* 1 CAP - Capabilities Register */
  427. ahci->cap = ahci_rreg(ahci, AHCI_CAP);
  428. ASSERT_BIT_CLEAR(ahci->cap, AHCI_CAP_RESERVED);
  429. /* 2 GHC - Global Host Control */
  430. reg = ahci_rreg(ahci, AHCI_GHC);
  431. ASSERT_BIT_CLEAR(reg, AHCI_GHC_HR);
  432. ASSERT_BIT_CLEAR(reg, AHCI_GHC_IE);
  433. ASSERT_BIT_CLEAR(reg, AHCI_GHC_MRSM);
  434. if (BITSET(ahci->cap, AHCI_CAP_SAM)) {
  435. g_test_message("Supports AHCI-Only Mode: GHC_AE is Read-Only.");
  436. ASSERT_BIT_SET(reg, AHCI_GHC_AE);
  437. } else {
  438. g_test_message("Supports AHCI/Legacy mix.");
  439. ASSERT_BIT_CLEAR(reg, AHCI_GHC_AE);
  440. }
  441. /* 3 IS - Interrupt Status */
  442. reg = ahci_rreg(ahci, AHCI_IS);
  443. g_assert_cmphex(reg, ==, 0);
  444. /* 4 PI - Ports Implemented */
  445. ports = ahci_rreg(ahci, AHCI_PI);
  446. /* Ports Implemented must be non-zero. */
  447. g_assert_cmphex(ports, !=, 0);
  448. /* Ports Implemented must be <= Number of Ports. */
  449. nports_impl = ctpopl(ports);
  450. g_assert_cmpuint(((AHCI_CAP_NP & ahci->cap) + 1), >=, nports_impl);
  451. /* Ports must be within the proper range. Given a mapping of SIZE,
  452. * 256 bytes are used for global HBA control, and the rest is used
  453. * for ports data, at 0x80 bytes each. */
  454. g_assert_cmphex(ahci->barsize, >, 0);
  455. maxports = (ahci->barsize - HBA_DATA_REGION_SIZE) / HBA_PORT_DATA_SIZE;
  456. /* e.g, 30 ports for 4K of memory. (4096 - 256) / 128 = 30 */
  457. g_assert_cmphex((reg >> maxports), ==, 0);
  458. /* 5 AHCI Version */
  459. reg = ahci_rreg(ahci, AHCI_VS);
  460. switch (reg) {
  461. case AHCI_VERSION_0_95:
  462. case AHCI_VERSION_1_0:
  463. case AHCI_VERSION_1_1:
  464. case AHCI_VERSION_1_2:
  465. case AHCI_VERSION_1_3:
  466. break;
  467. default:
  468. g_assert_not_reached();
  469. }
  470. /* 6 Command Completion Coalescing Control: depends on CAP.CCCS. */
  471. reg = ahci_rreg(ahci, AHCI_CCCCTL);
  472. if (BITSET(ahci->cap, AHCI_CAP_CCCS)) {
  473. ASSERT_BIT_CLEAR(reg, AHCI_CCCCTL_EN);
  474. ASSERT_BIT_CLEAR(reg, AHCI_CCCCTL_RESERVED);
  475. ASSERT_BIT_SET(reg, AHCI_CCCCTL_CC);
  476. ASSERT_BIT_SET(reg, AHCI_CCCCTL_TV);
  477. } else {
  478. g_assert_cmphex(reg, ==, 0);
  479. }
  480. /* 7 CCC_PORTS */
  481. reg = ahci_rreg(ahci, AHCI_CCCPORTS);
  482. /* Must be zeroes initially regardless of CAP.CCCS */
  483. g_assert_cmphex(reg, ==, 0);
  484. /* 8 EM_LOC */
  485. reg = ahci_rreg(ahci, AHCI_EMLOC);
  486. if (BITCLR(ahci->cap, AHCI_CAP_EMS)) {
  487. g_assert_cmphex(reg, ==, 0);
  488. }
  489. /* 9 EM_CTL */
  490. reg = ahci_rreg(ahci, AHCI_EMCTL);
  491. if (BITSET(ahci->cap, AHCI_CAP_EMS)) {
  492. ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_STSMR);
  493. ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_CTLTM);
  494. ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_CTLRST);
  495. ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_RESERVED);
  496. } else {
  497. g_assert_cmphex(reg, ==, 0);
  498. }
  499. /* 10 CAP2 -- Capabilities Extended */
  500. ahci->cap2 = ahci_rreg(ahci, AHCI_CAP2);
  501. ASSERT_BIT_CLEAR(ahci->cap2, AHCI_CAP2_RESERVED);
  502. /* 11 BOHC -- Bios/OS Handoff Control */
  503. reg = ahci_rreg(ahci, AHCI_BOHC);
  504. g_assert_cmphex(reg, ==, 0);
  505. /* 12 -- 23: Reserved */
  506. g_test_message("Verifying HBA reserved area is empty.");
  507. for (i = AHCI_RESERVED; i < AHCI_NVMHCI; ++i) {
  508. reg = ahci_rreg(ahci, i);
  509. g_assert_cmphex(reg, ==, 0);
  510. }
  511. /* 24 -- 39: NVMHCI */
  512. if (BITCLR(ahci->cap2, AHCI_CAP2_NVMP)) {
  513. g_test_message("Verifying HBA/NVMHCI area is empty.");
  514. for (i = AHCI_NVMHCI; i < AHCI_VENDOR; ++i) {
  515. reg = ahci_rreg(ahci, i);
  516. g_assert_cmphex(reg, ==, 0);
  517. }
  518. }
  519. /* 40 -- 63: Vendor */
  520. g_test_message("Verifying HBA/Vendor area is empty.");
  521. for (i = AHCI_VENDOR; i < AHCI_PORTS; ++i) {
  522. reg = ahci_rreg(ahci, i);
  523. g_assert_cmphex(reg, ==, 0);
  524. }
  525. /* 64 -- XX: Port Space */
  526. for (i = 0; ports || (i < maxports); ports >>= 1, ++i) {
  527. if (BITSET(ports, 0x1)) {
  528. g_test_message("Testing port %u for spec", i);
  529. ahci_test_port_spec(ahci, i);
  530. } else {
  531. uint16_t j;
  532. uint16_t low = AHCI_PORTS + (32 * i);
  533. uint16_t high = AHCI_PORTS + (32 * (i + 1));
  534. g_test_message("Asserting unimplemented port %u "
  535. "(reg [%u-%u]) is empty.",
  536. i, low, high - 1);
  537. for (j = low; j < high; ++j) {
  538. reg = ahci_rreg(ahci, j);
  539. g_assert_cmphex(reg, ==, 0);
  540. }
  541. }
  542. }
  543. }
  544. /**
  545. * Test the memory space for one port for specification adherence.
  546. */
  547. static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port)
  548. {
  549. uint32_t reg;
  550. unsigned i;
  551. /* (0) CLB */
  552. reg = ahci_px_rreg(ahci, port, AHCI_PX_CLB);
  553. ASSERT_BIT_CLEAR(reg, AHCI_PX_CLB_RESERVED);
  554. /* (1) CLBU */
  555. if (BITCLR(ahci->cap, AHCI_CAP_S64A)) {
  556. reg = ahci_px_rreg(ahci, port, AHCI_PX_CLBU);
  557. g_assert_cmphex(reg, ==, 0);
  558. }
  559. /* (2) FB */
  560. reg = ahci_px_rreg(ahci, port, AHCI_PX_FB);
  561. ASSERT_BIT_CLEAR(reg, AHCI_PX_FB_RESERVED);
  562. /* (3) FBU */
  563. if (BITCLR(ahci->cap, AHCI_CAP_S64A)) {
  564. reg = ahci_px_rreg(ahci, port, AHCI_PX_FBU);
  565. g_assert_cmphex(reg, ==, 0);
  566. }
  567. /* (4) IS */
  568. reg = ahci_px_rreg(ahci, port, AHCI_PX_IS);
  569. g_assert_cmphex(reg, ==, 0);
  570. /* (5) IE */
  571. reg = ahci_px_rreg(ahci, port, AHCI_PX_IE);
  572. g_assert_cmphex(reg, ==, 0);
  573. /* (6) CMD */
  574. reg = ahci_px_rreg(ahci, port, AHCI_PX_CMD);
  575. ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FRE);
  576. ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_RESERVED);
  577. ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CCS);
  578. ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FR);
  579. ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CR);
  580. ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_PMA); /* And RW only if CAP.SPM */
  581. ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_APSTE); /* RW only if CAP2.APST */
  582. ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ATAPI);
  583. ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_DLAE);
  584. ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ALPE); /* RW only if CAP.SALP */
  585. ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ASP); /* RW only if CAP.SALP */
  586. ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ICC);
  587. /* If CPDetect support does not exist, CPState must be off. */
  588. if (BITCLR(reg, AHCI_PX_CMD_CPD)) {
  589. ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CPS);
  590. }
  591. /* If MPSPresence is not set, MPSState must be off. */
  592. if (BITCLR(reg, AHCI_PX_CMD_MPSP)) {
  593. ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSS);
  594. }
  595. /* If we do not support MPS, MPSS and MPSP must be off. */
  596. if (BITCLR(ahci->cap, AHCI_CAP_SMPS)) {
  597. ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSS);
  598. ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSP);
  599. }
  600. /* If, via CPD or MPSP we detect a drive, HPCP must be on. */
  601. if (BITANY(reg, AHCI_PX_CMD_CPD | AHCI_PX_CMD_MPSP)) {
  602. ASSERT_BIT_SET(reg, AHCI_PX_CMD_HPCP);
  603. }
  604. /* HPCP and ESP cannot both be active. */
  605. g_assert(!BITSET(reg, AHCI_PX_CMD_HPCP | AHCI_PX_CMD_ESP));
  606. /* If CAP.FBSS is not set, FBSCP must not be set. */
  607. if (BITCLR(ahci->cap, AHCI_CAP_FBSS)) {
  608. ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FBSCP);
  609. }
  610. /* (7) RESERVED */
  611. reg = ahci_px_rreg(ahci, port, AHCI_PX_RES1);
  612. g_assert_cmphex(reg, ==, 0);
  613. /* (8) TFD */
  614. reg = ahci_px_rreg(ahci, port, AHCI_PX_TFD);
  615. /* At boot, prior to an FIS being received, the TFD register should be 0x7F,
  616. * which breaks down as follows, as seen in AHCI 1.3 sec 3.3.8, p. 27. */
  617. ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_ERR);
  618. ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_CS1);
  619. ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_DRQ);
  620. ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_CS2);
  621. ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_BSY);
  622. ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_ERR);
  623. ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_RESERVED);
  624. /* (9) SIG */
  625. /* Though AHCI specifies the boot value should be 0xFFFFFFFF,
  626. * Even when GHC.ST is zero, the AHCI HBA may receive the initial
  627. * D2H register FIS and update the signature asynchronously,
  628. * so we cannot expect a value here. AHCI 1.3, sec 3.3.9, pp 27-28 */
  629. /* (10) SSTS / SCR0: SStatus */
  630. reg = ahci_px_rreg(ahci, port, AHCI_PX_SSTS);
  631. ASSERT_BIT_CLEAR(reg, AHCI_PX_SSTS_RESERVED);
  632. /* Even though the register should be 0 at boot, it is asynchronous and
  633. * prone to change, so we cannot test any well known value. */
  634. /* (11) SCTL / SCR2: SControl */
  635. reg = ahci_px_rreg(ahci, port, AHCI_PX_SCTL);
  636. g_assert_cmphex(reg, ==, 0);
  637. /* (12) SERR / SCR1: SError */
  638. reg = ahci_px_rreg(ahci, port, AHCI_PX_SERR);
  639. g_assert_cmphex(reg, ==, 0);
  640. /* (13) SACT / SCR3: SActive */
  641. reg = ahci_px_rreg(ahci, port, AHCI_PX_SACT);
  642. g_assert_cmphex(reg, ==, 0);
  643. /* (14) CI */
  644. reg = ahci_px_rreg(ahci, port, AHCI_PX_CI);
  645. g_assert_cmphex(reg, ==, 0);
  646. /* (15) SNTF */
  647. reg = ahci_px_rreg(ahci, port, AHCI_PX_SNTF);
  648. g_assert_cmphex(reg, ==, 0);
  649. /* (16) FBS */
  650. reg = ahci_px_rreg(ahci, port, AHCI_PX_FBS);
  651. ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_EN);
  652. ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DEC);
  653. ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_SDE);
  654. ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DEV);
  655. ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DWE);
  656. ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_RESERVED);
  657. if (BITSET(ahci->cap, AHCI_CAP_FBSS)) {
  658. /* if Port-Multiplier FIS-based switching avail, ADO must >= 2 */
  659. g_assert((reg & AHCI_PX_FBS_ADO) >> ctzl(AHCI_PX_FBS_ADO) >= 2);
  660. }
  661. /* [17 -- 27] RESERVED */
  662. for (i = AHCI_PX_RES2; i < AHCI_PX_VS; ++i) {
  663. reg = ahci_px_rreg(ahci, port, i);
  664. g_assert_cmphex(reg, ==, 0);
  665. }
  666. /* [28 -- 31] Vendor-Specific */
  667. for (i = AHCI_PX_VS; i < 32; ++i) {
  668. reg = ahci_px_rreg(ahci, port, i);
  669. if (reg) {
  670. g_test_message("INFO: Vendor register %u non-empty", i);
  671. }
  672. }
  673. }
  674. /**
  675. * Utilizing an initialized AHCI HBA, issue an IDENTIFY command to the first
  676. * device we see, then read and check the response.
  677. */
  678. static void ahci_test_identify(AHCIQState *ahci)
  679. {
  680. uint16_t buff[256];
  681. unsigned px;
  682. int rc;
  683. uint16_t sect_size;
  684. const size_t buffsize = 512;
  685. g_assert(ahci != NULL);
  686. /**
  687. * This serves as a bit of a tutorial on AHCI device programming:
  688. *
  689. * (1) Create a data buffer for the IDENTIFY response to be sent to
  690. * (2) Create a Command Table buffer, where we will store the
  691. * command and PRDT (Physical Region Descriptor Table)
  692. * (3) Construct an FIS host-to-device command structure, and write it to
  693. * the top of the Command Table buffer.
  694. * (4) Create one or more Physical Region Descriptors (PRDs) that describe
  695. * a location in memory where data may be stored/retrieved.
  696. * (5) Write these PRDTs to the bottom (offset 0x80) of the Command Table.
  697. * (6) Each AHCI port has up to 32 command slots. Each slot contains a
  698. * header that points to a Command Table buffer. Pick an unused slot
  699. * and update it to point to the Command Table we have built.
  700. * (7) Now: Command #n points to our Command Table, and our Command Table
  701. * contains the FIS (that describes our command) and the PRDTL, which
  702. * describes our buffer.
  703. * (8) We inform the HBA via PxCI (Command Issue) that the command in slot
  704. * #n is ready for processing.
  705. */
  706. /* Pick the first implemented and running port */
  707. px = ahci_port_select(ahci);
  708. g_test_message("Selected port %u for test", px);
  709. /* Clear out the FIS Receive area and any pending interrupts. */
  710. ahci_port_clear(ahci, px);
  711. /* "Read" 512 bytes using CMD_IDENTIFY into the host buffer. */
  712. ahci_io(ahci, px, CMD_IDENTIFY, &buff, buffsize, 0);
  713. /* Check serial number/version in the buffer */
  714. /* NB: IDENTIFY strings are packed in 16bit little endian chunks.
  715. * Since we copy byte-for-byte in ahci-test, on both LE and BE, we need to
  716. * unchunk this data. By contrast, ide-test copies 2 bytes at a time, and
  717. * as a consequence, only needs to unchunk the data on LE machines. */
  718. string_bswap16(&buff[10], 20);
  719. rc = memcmp(&buff[10], "testdisk ", 20);
  720. g_assert_cmphex(rc, ==, 0);
  721. string_bswap16(&buff[23], 8);
  722. rc = memcmp(&buff[23], "version ", 8);
  723. g_assert_cmphex(rc, ==, 0);
  724. sect_size = le16_to_cpu(*((uint16_t *)(&buff[5])));
  725. g_assert_cmphex(sect_size, ==, AHCI_SECTOR_SIZE);
  726. }
  727. static void ahci_test_io_rw_simple(AHCIQState *ahci, unsigned bufsize,
  728. uint64_t sector, uint8_t read_cmd,
  729. uint8_t write_cmd)
  730. {
  731. uint64_t ptr;
  732. uint8_t port;
  733. unsigned char *tx = g_malloc(bufsize);
  734. unsigned char *rx = g_malloc0(bufsize);
  735. g_assert(ahci != NULL);
  736. /* Pick the first running port and clear it. */
  737. port = ahci_port_select(ahci);
  738. ahci_port_clear(ahci, port);
  739. /*** Create pattern and transfer to guest ***/
  740. /* Data buffer in the guest */
  741. ptr = ahci_alloc(ahci, bufsize);
  742. g_assert(ptr);
  743. /* Write some indicative pattern to our buffer. */
  744. generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
  745. qtest_bufwrite(ahci->parent->qts, ptr, tx, bufsize);
  746. /* Write this buffer to disk, then read it back to the DMA buffer. */
  747. ahci_guest_io(ahci, port, write_cmd, ptr, bufsize, sector);
  748. qtest_memset(ahci->parent->qts, ptr, 0x00, bufsize);
  749. ahci_guest_io(ahci, port, read_cmd, ptr, bufsize, sector);
  750. /*** Read back the Data ***/
  751. qtest_bufread(ahci->parent->qts, ptr, rx, bufsize);
  752. g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
  753. ahci_free(ahci, ptr);
  754. g_free(tx);
  755. g_free(rx);
  756. }
  757. static uint8_t ahci_test_nondata(AHCIQState *ahci, uint8_t ide_cmd)
  758. {
  759. uint8_t port;
  760. /* Sanitize */
  761. port = ahci_port_select(ahci);
  762. ahci_port_clear(ahci, port);
  763. ahci_io(ahci, port, ide_cmd, NULL, 0, 0);
  764. return port;
  765. }
  766. static void ahci_test_flush(AHCIQState *ahci)
  767. {
  768. ahci_test_nondata(ahci, CMD_FLUSH_CACHE);
  769. }
  770. static void ahci_test_max(AHCIQState *ahci)
  771. {
  772. RegD2HFIS *d2h = g_malloc0(0x20);
  773. uint64_t nsect;
  774. uint8_t port;
  775. uint8_t cmd;
  776. uint64_t config_sect = mb_to_sectors(test_image_size_mb) - 1;
  777. if (config_sect > 0xFFFFFF) {
  778. cmd = CMD_READ_MAX_EXT;
  779. } else {
  780. cmd = CMD_READ_MAX;
  781. }
  782. port = ahci_test_nondata(ahci, cmd);
  783. qtest_memread(ahci->parent->qts, ahci->port[port].fb + 0x40, d2h, 0x20);
  784. nsect = (uint64_t)d2h->lba_hi[2] << 40 |
  785. (uint64_t)d2h->lba_hi[1] << 32 |
  786. (uint64_t)d2h->lba_hi[0] << 24 |
  787. (uint64_t)d2h->lba_lo[2] << 16 |
  788. (uint64_t)d2h->lba_lo[1] << 8 |
  789. (uint64_t)d2h->lba_lo[0];
  790. g_assert_cmphex(nsect, ==, config_sect);
  791. g_free(d2h);
  792. }
  793. /******************************************************************************/
  794. /* Test Interfaces */
  795. /******************************************************************************/
  796. /**
  797. * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
  798. */
  799. static void test_sanity(void)
  800. {
  801. AHCIQState *ahci;
  802. ahci = ahci_boot(NULL);
  803. ahci_shutdown(ahci);
  804. }
  805. /**
  806. * Ensure that the PCI configuration space for the AHCI device is in-line with
  807. * the AHCI 1.3 specification for initial values.
  808. */
  809. static void test_pci_spec(void)
  810. {
  811. AHCIQState *ahci;
  812. ahci = ahci_boot(NULL);
  813. ahci_test_pci_spec(ahci);
  814. ahci_shutdown(ahci);
  815. }
  816. /**
  817. * Engage the PCI AHCI device and sanity check the response.
  818. * Perform additional PCI config space bringup for the HBA.
  819. */
  820. static void test_pci_enable(void)
  821. {
  822. AHCIQState *ahci;
  823. ahci = ahci_boot(NULL);
  824. ahci_pci_enable(ahci);
  825. ahci_shutdown(ahci);
  826. }
  827. /**
  828. * Investigate the memory mapped regions of the HBA,
  829. * and test them for AHCI specification adherence.
  830. */
  831. static void test_hba_spec(void)
  832. {
  833. AHCIQState *ahci;
  834. ahci = ahci_boot(NULL);
  835. ahci_pci_enable(ahci);
  836. ahci_test_hba_spec(ahci);
  837. ahci_shutdown(ahci);
  838. }
  839. /**
  840. * Engage the HBA functionality of the AHCI PCI device,
  841. * and bring it into a functional idle state.
  842. */
  843. static void test_hba_enable(void)
  844. {
  845. AHCIQState *ahci;
  846. ahci = ahci_boot(NULL);
  847. ahci_pci_enable(ahci);
  848. ahci_hba_enable(ahci);
  849. ahci_shutdown(ahci);
  850. }
  851. /**
  852. * Bring up the device and issue an IDENTIFY command.
  853. * Inspect the state of the HBA device and the data returned.
  854. */
  855. static void test_identify(void)
  856. {
  857. AHCIQState *ahci;
  858. ahci = ahci_boot_and_enable(NULL);
  859. ahci_test_identify(ahci);
  860. ahci_shutdown(ahci);
  861. }
  862. /**
  863. * Fragmented DMA test: Perform a standard 4K DMA read/write
  864. * test, but make sure the physical regions are fragmented to
  865. * be very small, each just 32 bytes, to see how AHCI performs
  866. * with chunks defined to be much less than a sector.
  867. */
  868. static void test_dma_fragmented(void)
  869. {
  870. AHCIQState *ahci;
  871. AHCICommand *cmd;
  872. uint8_t px;
  873. size_t bufsize = 4096;
  874. unsigned char *tx = g_malloc(bufsize);
  875. unsigned char *rx = g_malloc0(bufsize);
  876. uint64_t ptr;
  877. ahci = ahci_boot_and_enable(NULL);
  878. px = ahci_port_select(ahci);
  879. ahci_port_clear(ahci, px);
  880. /* create pattern */
  881. generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
  882. /* Create a DMA buffer in guest memory, and write our pattern to it. */
  883. ptr = guest_alloc(&ahci->parent->alloc, bufsize);
  884. g_assert(ptr);
  885. qtest_bufwrite(ahci->parent->qts, ptr, tx, bufsize);
  886. cmd = ahci_command_create(CMD_WRITE_DMA);
  887. ahci_command_adjust(cmd, 0, ptr, bufsize, 32);
  888. ahci_command_commit(ahci, cmd, px);
  889. ahci_command_issue(ahci, cmd);
  890. ahci_command_verify(ahci, cmd);
  891. ahci_command_free(cmd);
  892. cmd = ahci_command_create(CMD_READ_DMA);
  893. ahci_command_adjust(cmd, 0, ptr, bufsize, 32);
  894. ahci_command_commit(ahci, cmd, px);
  895. ahci_command_issue(ahci, cmd);
  896. ahci_command_verify(ahci, cmd);
  897. ahci_command_free(cmd);
  898. /* Read back the guest's receive buffer into local memory */
  899. qtest_bufread(ahci->parent->qts, ptr, rx, bufsize);
  900. guest_free(&ahci->parent->alloc, ptr);
  901. g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
  902. ahci_shutdown(ahci);
  903. g_free(rx);
  904. g_free(tx);
  905. }
  906. /*
  907. * Write sector 1 with random data to make AHCI storage dirty
  908. * Needed for flush tests so that flushes actually go though the block layer
  909. */
  910. static void make_dirty(AHCIQState* ahci, uint8_t port)
  911. {
  912. uint64_t ptr;
  913. unsigned bufsize = 512;
  914. ptr = ahci_alloc(ahci, bufsize);
  915. g_assert(ptr);
  916. ahci_guest_io(ahci, port, CMD_WRITE_DMA, ptr, bufsize, 1);
  917. ahci_free(ahci, ptr);
  918. }
  919. static void test_flush(void)
  920. {
  921. AHCIQState *ahci;
  922. uint8_t port;
  923. ahci = ahci_boot_and_enable(NULL);
  924. port = ahci_port_select(ahci);
  925. ahci_port_clear(ahci, port);
  926. make_dirty(ahci, port);
  927. ahci_test_flush(ahci);
  928. ahci_shutdown(ahci);
  929. }
  930. static void test_flush_retry(void)
  931. {
  932. AHCIQState *ahci;
  933. AHCICommand *cmd;
  934. uint8_t port;
  935. prepare_blkdebug_script(debug_path, "flush_to_disk");
  936. ahci = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
  937. "format=%s,cache=writeback,"
  938. "rerror=stop,werror=stop "
  939. "-M q35 "
  940. "-device ide-hd,drive=drive0 ",
  941. debug_path,
  942. tmp_path, imgfmt);
  943. port = ahci_port_select(ahci);
  944. ahci_port_clear(ahci, port);
  945. /* Issue write so that flush actually goes to disk */
  946. make_dirty(ahci, port);
  947. /* Issue Flush Command and wait for error */
  948. cmd = ahci_guest_io_halt(ahci, port, CMD_FLUSH_CACHE, 0, 0, 0);
  949. ahci_guest_io_resume(ahci, cmd);
  950. ahci_shutdown(ahci);
  951. }
  952. /**
  953. * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
  954. */
  955. static void test_migrate_sanity(void)
  956. {
  957. AHCIQState *src, *dst;
  958. char *uri = g_strdup_printf("unix:%s", mig_socket);
  959. src = ahci_boot("-m 384 -M q35 "
  960. "-drive if=ide,file=%s,format=%s ", tmp_path, imgfmt);
  961. dst = ahci_boot("-m 384 -M q35 "
  962. "-drive if=ide,file=%s,format=%s "
  963. "-incoming %s", tmp_path, imgfmt, uri);
  964. ahci_migrate(src, dst, uri);
  965. ahci_shutdown(src);
  966. ahci_shutdown(dst);
  967. g_free(uri);
  968. }
  969. /**
  970. * Simple migration test: Write a pattern, migrate, then read.
  971. */
  972. static void ahci_migrate_simple(uint8_t cmd_read, uint8_t cmd_write)
  973. {
  974. AHCIQState *src, *dst;
  975. uint8_t px;
  976. size_t bufsize = 4096;
  977. unsigned char *tx = g_malloc(bufsize);
  978. unsigned char *rx = g_malloc0(bufsize);
  979. char *uri = g_strdup_printf("unix:%s", mig_socket);
  980. src = ahci_boot_and_enable("-m 384 -M q35 "
  981. "-drive if=ide,format=%s,file=%s ",
  982. imgfmt, tmp_path);
  983. dst = ahci_boot("-m 384 -M q35 "
  984. "-drive if=ide,format=%s,file=%s "
  985. "-incoming %s", imgfmt, tmp_path, uri);
  986. /* initialize */
  987. px = ahci_port_select(src);
  988. ahci_port_clear(src, px);
  989. /* create pattern */
  990. generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
  991. /* Write, migrate, then read. */
  992. ahci_io(src, px, cmd_write, tx, bufsize, 0);
  993. ahci_migrate(src, dst, uri);
  994. ahci_io(dst, px, cmd_read, rx, bufsize, 0);
  995. /* Verify pattern */
  996. g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
  997. ahci_shutdown(src);
  998. ahci_shutdown(dst);
  999. g_free(rx);
  1000. g_free(tx);
  1001. g_free(uri);
  1002. }
  1003. static void test_migrate_dma(void)
  1004. {
  1005. ahci_migrate_simple(CMD_READ_DMA, CMD_WRITE_DMA);
  1006. }
  1007. static void test_migrate_ncq(void)
  1008. {
  1009. ahci_migrate_simple(READ_FPDMA_QUEUED, WRITE_FPDMA_QUEUED);
  1010. }
  1011. /**
  1012. * Halted IO Error Test
  1013. *
  1014. * Simulate an error on first write, Try to write a pattern,
  1015. * Confirm the VM has stopped, resume the VM, verify command
  1016. * has completed, then read back the data and verify.
  1017. */
  1018. static void ahci_halted_io_test(uint8_t cmd_read, uint8_t cmd_write)
  1019. {
  1020. AHCIQState *ahci;
  1021. uint8_t port;
  1022. size_t bufsize = 4096;
  1023. unsigned char *tx = g_malloc(bufsize);
  1024. unsigned char *rx = g_malloc0(bufsize);
  1025. uint64_t ptr;
  1026. AHCICommand *cmd;
  1027. prepare_blkdebug_script(debug_path, "write_aio");
  1028. ahci = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
  1029. "format=%s,cache=writeback,"
  1030. "rerror=stop,werror=stop "
  1031. "-M q35 "
  1032. "-device ide-hd,drive=drive0 ",
  1033. debug_path,
  1034. tmp_path, imgfmt);
  1035. /* Initialize and prepare */
  1036. port = ahci_port_select(ahci);
  1037. ahci_port_clear(ahci, port);
  1038. /* create DMA source buffer and write pattern */
  1039. generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
  1040. ptr = ahci_alloc(ahci, bufsize);
  1041. g_assert(ptr);
  1042. qtest_memwrite(ahci->parent->qts, ptr, tx, bufsize);
  1043. /* Attempt to write (and fail) */
  1044. cmd = ahci_guest_io_halt(ahci, port, cmd_write,
  1045. ptr, bufsize, 0);
  1046. /* Attempt to resume the command */
  1047. ahci_guest_io_resume(ahci, cmd);
  1048. ahci_free(ahci, ptr);
  1049. /* Read back and verify */
  1050. ahci_io(ahci, port, cmd_read, rx, bufsize, 0);
  1051. g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
  1052. /* Cleanup and go home */
  1053. ahci_shutdown(ahci);
  1054. g_free(rx);
  1055. g_free(tx);
  1056. }
  1057. static void test_halted_dma(void)
  1058. {
  1059. ahci_halted_io_test(CMD_READ_DMA, CMD_WRITE_DMA);
  1060. }
  1061. static void test_halted_ncq(void)
  1062. {
  1063. ahci_halted_io_test(READ_FPDMA_QUEUED, WRITE_FPDMA_QUEUED);
  1064. }
  1065. /**
  1066. * IO Error Migration Test
  1067. *
  1068. * Simulate an error on first write, Try to write a pattern,
  1069. * Confirm the VM has stopped, migrate, resume the VM,
  1070. * verify command has completed, then read back the data and verify.
  1071. */
  1072. static void ahci_migrate_halted_io(uint8_t cmd_read, uint8_t cmd_write)
  1073. {
  1074. AHCIQState *src, *dst;
  1075. uint8_t port;
  1076. size_t bufsize = 4096;
  1077. unsigned char *tx = g_malloc(bufsize);
  1078. unsigned char *rx = g_malloc0(bufsize);
  1079. uint64_t ptr;
  1080. AHCICommand *cmd;
  1081. char *uri = g_strdup_printf("unix:%s", mig_socket);
  1082. prepare_blkdebug_script(debug_path, "write_aio");
  1083. src = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
  1084. "format=%s,cache=writeback,"
  1085. "rerror=stop,werror=stop "
  1086. "-M q35 "
  1087. "-device ide-hd,drive=drive0 ",
  1088. debug_path,
  1089. tmp_path, imgfmt);
  1090. dst = ahci_boot("-drive file=%s,if=none,id=drive0,"
  1091. "format=%s,cache=writeback,"
  1092. "rerror=stop,werror=stop "
  1093. "-M q35 "
  1094. "-device ide-hd,drive=drive0 "
  1095. "-incoming %s",
  1096. tmp_path, imgfmt, uri);
  1097. /* Initialize and prepare */
  1098. port = ahci_port_select(src);
  1099. ahci_port_clear(src, port);
  1100. generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
  1101. /* create DMA source buffer and write pattern */
  1102. ptr = ahci_alloc(src, bufsize);
  1103. g_assert(ptr);
  1104. qtest_memwrite(src->parent->qts, ptr, tx, bufsize);
  1105. /* Write, trigger the VM to stop, migrate, then resume. */
  1106. cmd = ahci_guest_io_halt(src, port, cmd_write,
  1107. ptr, bufsize, 0);
  1108. ahci_migrate(src, dst, uri);
  1109. ahci_guest_io_resume(dst, cmd);
  1110. ahci_free(dst, ptr);
  1111. /* Read back */
  1112. ahci_io(dst, port, cmd_read, rx, bufsize, 0);
  1113. /* Verify TX and RX are identical */
  1114. g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
  1115. /* Cleanup and go home. */
  1116. ahci_shutdown(src);
  1117. ahci_shutdown(dst);
  1118. g_free(rx);
  1119. g_free(tx);
  1120. g_free(uri);
  1121. }
  1122. static void test_migrate_halted_dma(void)
  1123. {
  1124. ahci_migrate_halted_io(CMD_READ_DMA, CMD_WRITE_DMA);
  1125. }
  1126. static void test_migrate_halted_ncq(void)
  1127. {
  1128. ahci_migrate_halted_io(READ_FPDMA_QUEUED, WRITE_FPDMA_QUEUED);
  1129. }
  1130. /**
  1131. * Migration test: Try to flush, migrate, then resume.
  1132. */
  1133. static void test_flush_migrate(void)
  1134. {
  1135. AHCIQState *src, *dst;
  1136. AHCICommand *cmd;
  1137. uint8_t px;
  1138. char *uri = g_strdup_printf("unix:%s", mig_socket);
  1139. prepare_blkdebug_script(debug_path, "flush_to_disk");
  1140. src = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
  1141. "cache=writeback,rerror=stop,werror=stop,"
  1142. "format=%s "
  1143. "-M q35 "
  1144. "-device ide-hd,drive=drive0 ",
  1145. debug_path, tmp_path, imgfmt);
  1146. dst = ahci_boot("-drive file=%s,if=none,id=drive0,"
  1147. "cache=writeback,rerror=stop,werror=stop,"
  1148. "format=%s "
  1149. "-M q35 "
  1150. "-device ide-hd,drive=drive0 "
  1151. "-incoming %s", tmp_path, imgfmt, uri);
  1152. px = ahci_port_select(src);
  1153. ahci_port_clear(src, px);
  1154. /* Dirty device so that flush reaches disk */
  1155. make_dirty(src, px);
  1156. /* Issue Flush Command */
  1157. cmd = ahci_command_create(CMD_FLUSH_CACHE);
  1158. ahci_command_commit(src, cmd, px);
  1159. ahci_command_issue_async(src, cmd);
  1160. qtest_qmp_eventwait(src->parent->qts, "STOP");
  1161. /* Migrate over */
  1162. ahci_migrate(src, dst, uri);
  1163. /* Complete the command */
  1164. qtest_qmp_send(dst->parent->qts, "{'execute':'cont' }");
  1165. qtest_qmp_eventwait(dst->parent->qts, "RESUME");
  1166. ahci_command_wait(dst, cmd);
  1167. ahci_command_verify(dst, cmd);
  1168. ahci_command_free(cmd);
  1169. ahci_shutdown(src);
  1170. ahci_shutdown(dst);
  1171. g_free(uri);
  1172. }
  1173. static void test_max(void)
  1174. {
  1175. AHCIQState *ahci;
  1176. ahci = ahci_boot_and_enable(NULL);
  1177. ahci_test_max(ahci);
  1178. ahci_shutdown(ahci);
  1179. }
  1180. static void test_reset(void)
  1181. {
  1182. AHCIQState *ahci;
  1183. int i;
  1184. ahci = ahci_boot(NULL);
  1185. ahci_test_pci_spec(ahci);
  1186. ahci_pci_enable(ahci);
  1187. for (i = 0; i < 2; i++) {
  1188. ahci_test_hba_spec(ahci);
  1189. ahci_hba_enable(ahci);
  1190. ahci_test_identify(ahci);
  1191. ahci_test_io_rw_simple(ahci, 4096, 0,
  1192. CMD_READ_DMA_EXT,
  1193. CMD_WRITE_DMA_EXT);
  1194. ahci_set(ahci, AHCI_GHC, AHCI_GHC_HR);
  1195. ahci_clean_mem(ahci);
  1196. }
  1197. ahci_shutdown(ahci);
  1198. }
  1199. static void test_ncq_simple(void)
  1200. {
  1201. AHCIQState *ahci;
  1202. ahci = ahci_boot_and_enable(NULL);
  1203. ahci_test_io_rw_simple(ahci, 4096, 0,
  1204. READ_FPDMA_QUEUED,
  1205. WRITE_FPDMA_QUEUED);
  1206. ahci_shutdown(ahci);
  1207. }
  1208. static int prepare_iso(size_t size, unsigned char **buf, char **name)
  1209. {
  1210. char cdrom_path[] = "/tmp/qtest.iso.XXXXXX";
  1211. unsigned char *patt;
  1212. ssize_t ret;
  1213. int fd = mkstemp(cdrom_path);
  1214. g_assert(buf);
  1215. g_assert(name);
  1216. patt = g_malloc(size);
  1217. /* Generate a pattern and build a CDROM image to read from */
  1218. generate_pattern(patt, size, ATAPI_SECTOR_SIZE);
  1219. ret = write(fd, patt, size);
  1220. g_assert(ret == size);
  1221. *name = g_strdup(cdrom_path);
  1222. *buf = patt;
  1223. return fd;
  1224. }
  1225. static void remove_iso(int fd, char *name)
  1226. {
  1227. unlink(name);
  1228. g_free(name);
  1229. close(fd);
  1230. }
  1231. static int ahci_cb_cmp_buff(AHCIQState *ahci, AHCICommand *cmd,
  1232. const AHCIOpts *opts)
  1233. {
  1234. unsigned char *tx = opts->opaque;
  1235. unsigned char *rx;
  1236. if (!opts->size) {
  1237. return 0;
  1238. }
  1239. rx = g_malloc0(opts->size);
  1240. qtest_bufread(ahci->parent->qts, opts->buffer, rx, opts->size);
  1241. g_assert_cmphex(memcmp(tx, rx, opts->size), ==, 0);
  1242. g_free(rx);
  1243. return 0;
  1244. }
  1245. static void ahci_test_cdrom(int nsectors, bool dma, uint8_t cmd,
  1246. bool override_bcl, uint16_t bcl)
  1247. {
  1248. AHCIQState *ahci;
  1249. unsigned char *tx;
  1250. char *iso;
  1251. int fd;
  1252. AHCIOpts opts = {
  1253. .size = (ATAPI_SECTOR_SIZE * nsectors),
  1254. .atapi = true,
  1255. .atapi_dma = dma,
  1256. .post_cb = ahci_cb_cmp_buff,
  1257. .set_bcl = override_bcl,
  1258. .bcl = bcl,
  1259. };
  1260. uint64_t iso_size = ATAPI_SECTOR_SIZE * (nsectors + 1);
  1261. /* Prepare ISO and fill 'tx' buffer */
  1262. fd = prepare_iso(iso_size, &tx, &iso);
  1263. opts.opaque = tx;
  1264. /* Standard startup wonkery, but use ide-cd and our special iso file */
  1265. ahci = ahci_boot_and_enable("-drive if=none,id=drive0,file=%s,format=raw "
  1266. "-M q35 "
  1267. "-device ide-cd,drive=drive0 ", iso);
  1268. /* Build & Send AHCI command */
  1269. ahci_exec(ahci, ahci_port_select(ahci), cmd, &opts);
  1270. /* Cleanup */
  1271. g_free(tx);
  1272. ahci_shutdown(ahci);
  1273. remove_iso(fd, iso);
  1274. }
  1275. static void ahci_test_cdrom_read10(int nsectors, bool dma)
  1276. {
  1277. ahci_test_cdrom(nsectors, dma, CMD_ATAPI_READ_10, false, 0);
  1278. }
  1279. static void test_cdrom_dma(void)
  1280. {
  1281. ahci_test_cdrom_read10(1, true);
  1282. }
  1283. static void test_cdrom_dma_multi(void)
  1284. {
  1285. ahci_test_cdrom_read10(3, true);
  1286. }
  1287. static void test_cdrom_pio(void)
  1288. {
  1289. ahci_test_cdrom_read10(1, false);
  1290. }
  1291. static void test_cdrom_pio_multi(void)
  1292. {
  1293. ahci_test_cdrom_read10(3, false);
  1294. }
  1295. /* Regression test: Test that a READ_CD command with a BCL of 0 but a size of 0
  1296. * completes as a NOP instead of erroring out. */
  1297. static void test_atapi_bcl(void)
  1298. {
  1299. ahci_test_cdrom(0, false, CMD_ATAPI_READ_CD, true, 0);
  1300. }
  1301. static void atapi_wait_tray(AHCIQState *ahci, bool open)
  1302. {
  1303. QDict *rsp = qtest_qmp_eventwait_ref(ahci->parent->qts,
  1304. "DEVICE_TRAY_MOVED");
  1305. QDict *data = qdict_get_qdict(rsp, "data");
  1306. if (open) {
  1307. g_assert(qdict_get_bool(data, "tray-open"));
  1308. } else {
  1309. g_assert(!qdict_get_bool(data, "tray-open"));
  1310. }
  1311. qobject_unref(rsp);
  1312. }
  1313. static void test_atapi_tray(void)
  1314. {
  1315. AHCIQState *ahci;
  1316. unsigned char *tx;
  1317. char *iso;
  1318. int fd;
  1319. uint8_t port, sense, asc;
  1320. uint64_t iso_size = ATAPI_SECTOR_SIZE;
  1321. QDict *rsp;
  1322. fd = prepare_iso(iso_size, &tx, &iso);
  1323. ahci = ahci_boot_and_enable("-blockdev node-name=drive0,driver=file,filename=%s "
  1324. "-M q35 "
  1325. "-device ide-cd,id=cd0,drive=drive0 ", iso);
  1326. port = ahci_port_select(ahci);
  1327. ahci_atapi_eject(ahci, port);
  1328. atapi_wait_tray(ahci, true);
  1329. ahci_atapi_load(ahci, port);
  1330. atapi_wait_tray(ahci, false);
  1331. /* Remove media */
  1332. qtest_qmp_send(ahci->parent->qts, "{'execute': 'blockdev-open-tray', "
  1333. "'arguments': {'id': 'cd0'}}");
  1334. atapi_wait_tray(ahci, true);
  1335. rsp = qtest_qmp_receive(ahci->parent->qts);
  1336. qobject_unref(rsp);
  1337. qmp_discard_response(ahci->parent->qts,
  1338. "{'execute': 'blockdev-remove-medium', "
  1339. "'arguments': {'id': 'cd0'}}");
  1340. /* Test the tray without a medium */
  1341. ahci_atapi_load(ahci, port);
  1342. atapi_wait_tray(ahci, false);
  1343. ahci_atapi_eject(ahci, port);
  1344. atapi_wait_tray(ahci, true);
  1345. /* Re-insert media */
  1346. qmp_discard_response(ahci->parent->qts,
  1347. "{'execute': 'blockdev-add', "
  1348. "'arguments': {'node-name': 'node0', "
  1349. "'driver': 'raw', "
  1350. "'file': { 'driver': 'file', "
  1351. "'filename': %s }}}", iso);
  1352. qmp_discard_response(ahci->parent->qts,
  1353. "{'execute': 'blockdev-insert-medium',"
  1354. "'arguments': { 'id': 'cd0', "
  1355. "'node-name': 'node0' }}");
  1356. /* Again, the event shows up first */
  1357. qtest_qmp_send(ahci->parent->qts, "{'execute': 'blockdev-close-tray', "
  1358. "'arguments': {'id': 'cd0'}}");
  1359. atapi_wait_tray(ahci, false);
  1360. rsp = qtest_qmp_receive(ahci->parent->qts);
  1361. qobject_unref(rsp);
  1362. /* Now, to convince ATAPI we understand the media has changed... */
  1363. ahci_atapi_test_ready(ahci, port, false, SENSE_NOT_READY);
  1364. ahci_atapi_get_sense(ahci, port, &sense, &asc);
  1365. g_assert_cmpuint(sense, ==, SENSE_NOT_READY);
  1366. g_assert_cmpuint(asc, ==, ASC_MEDIUM_NOT_PRESENT);
  1367. ahci_atapi_test_ready(ahci, port, false, SENSE_UNIT_ATTENTION);
  1368. ahci_atapi_get_sense(ahci, port, &sense, &asc);
  1369. g_assert_cmpuint(sense, ==, SENSE_UNIT_ATTENTION);
  1370. g_assert_cmpuint(asc, ==, ASC_MEDIUM_MAY_HAVE_CHANGED);
  1371. ahci_atapi_test_ready(ahci, port, true, SENSE_NO_SENSE);
  1372. ahci_atapi_get_sense(ahci, port, &sense, &asc);
  1373. g_assert_cmpuint(sense, ==, SENSE_NO_SENSE);
  1374. /* Final tray test. */
  1375. ahci_atapi_eject(ahci, port);
  1376. atapi_wait_tray(ahci, true);
  1377. ahci_atapi_load(ahci, port);
  1378. atapi_wait_tray(ahci, false);
  1379. /* Cleanup */
  1380. g_free(tx);
  1381. ahci_shutdown(ahci);
  1382. remove_iso(fd, iso);
  1383. }
  1384. /******************************************************************************/
  1385. /* AHCI I/O Test Matrix Definitions */
  1386. enum BuffLen {
  1387. LEN_BEGIN = 0,
  1388. LEN_SIMPLE = LEN_BEGIN,
  1389. LEN_DOUBLE,
  1390. LEN_LONG,
  1391. LEN_SHORT,
  1392. NUM_LENGTHS
  1393. };
  1394. static const char *buff_len_str[NUM_LENGTHS] = { "simple", "double",
  1395. "long", "short" };
  1396. enum AddrMode {
  1397. ADDR_MODE_BEGIN = 0,
  1398. ADDR_MODE_LBA28 = ADDR_MODE_BEGIN,
  1399. ADDR_MODE_LBA48,
  1400. NUM_ADDR_MODES
  1401. };
  1402. static const char *addr_mode_str[NUM_ADDR_MODES] = { "lba28", "lba48" };
  1403. enum IOMode {
  1404. MODE_BEGIN = 0,
  1405. MODE_PIO = MODE_BEGIN,
  1406. MODE_DMA,
  1407. NUM_MODES
  1408. };
  1409. static const char *io_mode_str[NUM_MODES] = { "pio", "dma" };
  1410. enum IOOps {
  1411. IO_BEGIN = 0,
  1412. IO_READ = IO_BEGIN,
  1413. IO_WRITE,
  1414. NUM_IO_OPS
  1415. };
  1416. enum OffsetType {
  1417. OFFSET_BEGIN = 0,
  1418. OFFSET_ZERO = OFFSET_BEGIN,
  1419. OFFSET_LOW,
  1420. OFFSET_HIGH,
  1421. NUM_OFFSETS
  1422. };
  1423. static const char *offset_str[NUM_OFFSETS] = { "zero", "low", "high" };
  1424. typedef struct AHCIIOTestOptions {
  1425. enum BuffLen length;
  1426. enum AddrMode address_type;
  1427. enum IOMode io_type;
  1428. enum OffsetType offset;
  1429. } AHCIIOTestOptions;
  1430. static uint64_t offset_sector(enum OffsetType ofst,
  1431. enum AddrMode addr_type,
  1432. uint64_t buffsize)
  1433. {
  1434. uint64_t ceil;
  1435. uint64_t nsectors;
  1436. switch (ofst) {
  1437. case OFFSET_ZERO:
  1438. return 0;
  1439. case OFFSET_LOW:
  1440. return 1;
  1441. case OFFSET_HIGH:
  1442. ceil = (addr_type == ADDR_MODE_LBA28) ? 0xfffffff : 0xffffffffffff;
  1443. ceil = MIN(ceil, mb_to_sectors(test_image_size_mb) - 1);
  1444. nsectors = buffsize / AHCI_SECTOR_SIZE;
  1445. return ceil - nsectors + 1;
  1446. default:
  1447. g_assert_not_reached();
  1448. }
  1449. }
  1450. /**
  1451. * Table of possible I/O ATA commands given a set of enumerations.
  1452. */
  1453. static const uint8_t io_cmds[NUM_MODES][NUM_ADDR_MODES][NUM_IO_OPS] = {
  1454. [MODE_PIO] = {
  1455. [ADDR_MODE_LBA28] = {
  1456. [IO_READ] = CMD_READ_PIO,
  1457. [IO_WRITE] = CMD_WRITE_PIO },
  1458. [ADDR_MODE_LBA48] = {
  1459. [IO_READ] = CMD_READ_PIO_EXT,
  1460. [IO_WRITE] = CMD_WRITE_PIO_EXT }
  1461. },
  1462. [MODE_DMA] = {
  1463. [ADDR_MODE_LBA28] = {
  1464. [IO_READ] = CMD_READ_DMA,
  1465. [IO_WRITE] = CMD_WRITE_DMA },
  1466. [ADDR_MODE_LBA48] = {
  1467. [IO_READ] = CMD_READ_DMA_EXT,
  1468. [IO_WRITE] = CMD_WRITE_DMA_EXT }
  1469. }
  1470. };
  1471. /**
  1472. * Test a Read/Write pattern using various commands, addressing modes,
  1473. * transfer modes, and buffer sizes.
  1474. */
  1475. static void test_io_rw_interface(enum AddrMode lba48, enum IOMode dma,
  1476. unsigned bufsize, uint64_t sector)
  1477. {
  1478. AHCIQState *ahci;
  1479. ahci = ahci_boot_and_enable(NULL);
  1480. ahci_test_io_rw_simple(ahci, bufsize, sector,
  1481. io_cmds[dma][lba48][IO_READ],
  1482. io_cmds[dma][lba48][IO_WRITE]);
  1483. ahci_shutdown(ahci);
  1484. }
  1485. /**
  1486. * Demultiplex the test data and invoke the actual test routine.
  1487. */
  1488. static void test_io_interface(gconstpointer opaque)
  1489. {
  1490. AHCIIOTestOptions *opts = (AHCIIOTestOptions *)opaque;
  1491. unsigned bufsize;
  1492. uint64_t sector;
  1493. switch (opts->length) {
  1494. case LEN_SIMPLE:
  1495. bufsize = 4096;
  1496. break;
  1497. case LEN_DOUBLE:
  1498. bufsize = 8192;
  1499. break;
  1500. case LEN_LONG:
  1501. bufsize = 4096 * 64;
  1502. break;
  1503. case LEN_SHORT:
  1504. bufsize = 512;
  1505. break;
  1506. default:
  1507. g_assert_not_reached();
  1508. }
  1509. sector = offset_sector(opts->offset, opts->address_type, bufsize);
  1510. test_io_rw_interface(opts->address_type, opts->io_type, bufsize, sector);
  1511. g_free(opts);
  1512. return;
  1513. }
  1514. static void create_ahci_io_test(enum IOMode type, enum AddrMode addr,
  1515. enum BuffLen len, enum OffsetType offset)
  1516. {
  1517. char *name;
  1518. AHCIIOTestOptions *opts;
  1519. opts = g_new(AHCIIOTestOptions, 1);
  1520. opts->length = len;
  1521. opts->address_type = addr;
  1522. opts->io_type = type;
  1523. opts->offset = offset;
  1524. name = g_strdup_printf("ahci/io/%s/%s/%s/%s",
  1525. io_mode_str[type],
  1526. addr_mode_str[addr],
  1527. buff_len_str[len],
  1528. offset_str[offset]);
  1529. if ((addr == ADDR_MODE_LBA48) && (offset == OFFSET_HIGH) &&
  1530. (mb_to_sectors(test_image_size_mb) <= 0xFFFFFFF)) {
  1531. g_test_message("%s: skipped; test image too small", name);
  1532. g_free(opts);
  1533. g_free(name);
  1534. return;
  1535. }
  1536. qtest_add_data_func(name, opts, test_io_interface);
  1537. g_free(name);
  1538. }
  1539. /******************************************************************************/
  1540. int main(int argc, char **argv)
  1541. {
  1542. const char *arch;
  1543. int ret;
  1544. int fd;
  1545. int c;
  1546. int i, j, k, m;
  1547. static struct option long_options[] = {
  1548. {"pedantic", no_argument, 0, 'p' },
  1549. {0, 0, 0, 0},
  1550. };
  1551. /* Should be first to utilize g_test functionality, So we can see errors. */
  1552. g_test_init(&argc, &argv, NULL);
  1553. while (1) {
  1554. c = getopt_long(argc, argv, "", long_options, NULL);
  1555. if (c == -1) {
  1556. break;
  1557. }
  1558. switch (c) {
  1559. case -1:
  1560. break;
  1561. case 'p':
  1562. ahci_pedantic = 1;
  1563. break;
  1564. default:
  1565. fprintf(stderr, "Unrecognized ahci_test option.\n");
  1566. g_assert_not_reached();
  1567. }
  1568. }
  1569. /* Check architecture */
  1570. arch = qtest_get_arch();
  1571. if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
  1572. g_test_message("Skipping test for non-x86");
  1573. return 0;
  1574. }
  1575. /* Create a temporary image */
  1576. fd = mkstemp(tmp_path);
  1577. g_assert(fd >= 0);
  1578. if (have_qemu_img()) {
  1579. imgfmt = "qcow2";
  1580. test_image_size_mb = TEST_IMAGE_SIZE_MB_LARGE;
  1581. mkqcow2(tmp_path, TEST_IMAGE_SIZE_MB_LARGE);
  1582. } else {
  1583. g_test_message("QTEST_QEMU_IMG not set or qemu-img missing; "
  1584. "skipping LBA48 high-sector tests");
  1585. imgfmt = "raw";
  1586. test_image_size_mb = TEST_IMAGE_SIZE_MB_SMALL;
  1587. ret = ftruncate(fd, test_image_size_mb * 1024 * 1024);
  1588. g_assert(ret == 0);
  1589. }
  1590. close(fd);
  1591. /* Create temporary blkdebug instructions */
  1592. fd = mkstemp(debug_path);
  1593. g_assert(fd >= 0);
  1594. close(fd);
  1595. /* Reserve a hollow file to use as a socket for migration tests */
  1596. fd = mkstemp(mig_socket);
  1597. g_assert(fd >= 0);
  1598. close(fd);
  1599. /* Run the tests */
  1600. qtest_add_func("/ahci/sanity", test_sanity);
  1601. qtest_add_func("/ahci/pci_spec", test_pci_spec);
  1602. qtest_add_func("/ahci/pci_enable", test_pci_enable);
  1603. qtest_add_func("/ahci/hba_spec", test_hba_spec);
  1604. qtest_add_func("/ahci/hba_enable", test_hba_enable);
  1605. qtest_add_func("/ahci/identify", test_identify);
  1606. for (i = MODE_BEGIN; i < NUM_MODES; i++) {
  1607. for (j = ADDR_MODE_BEGIN; j < NUM_ADDR_MODES; j++) {
  1608. for (k = LEN_BEGIN; k < NUM_LENGTHS; k++) {
  1609. for (m = OFFSET_BEGIN; m < NUM_OFFSETS; m++) {
  1610. create_ahci_io_test(i, j, k, m);
  1611. }
  1612. }
  1613. }
  1614. }
  1615. qtest_add_func("/ahci/io/dma/lba28/fragmented", test_dma_fragmented);
  1616. qtest_add_func("/ahci/flush/simple", test_flush);
  1617. qtest_add_func("/ahci/flush/retry", test_flush_retry);
  1618. qtest_add_func("/ahci/flush/migrate", test_flush_migrate);
  1619. qtest_add_func("/ahci/migrate/sanity", test_migrate_sanity);
  1620. qtest_add_func("/ahci/migrate/dma/simple", test_migrate_dma);
  1621. qtest_add_func("/ahci/io/dma/lba28/retry", test_halted_dma);
  1622. qtest_add_func("/ahci/migrate/dma/halted", test_migrate_halted_dma);
  1623. qtest_add_func("/ahci/max", test_max);
  1624. qtest_add_func("/ahci/reset", test_reset);
  1625. qtest_add_func("/ahci/io/ncq/simple", test_ncq_simple);
  1626. qtest_add_func("/ahci/migrate/ncq/simple", test_migrate_ncq);
  1627. qtest_add_func("/ahci/io/ncq/retry", test_halted_ncq);
  1628. qtest_add_func("/ahci/migrate/ncq/halted", test_migrate_halted_ncq);
  1629. qtest_add_func("/ahci/cdrom/dma/single", test_cdrom_dma);
  1630. qtest_add_func("/ahci/cdrom/dma/multi", test_cdrom_dma_multi);
  1631. qtest_add_func("/ahci/cdrom/pio/single", test_cdrom_pio);
  1632. qtest_add_func("/ahci/cdrom/pio/multi", test_cdrom_pio_multi);
  1633. qtest_add_func("/ahci/cdrom/pio/bcl", test_atapi_bcl);
  1634. qtest_add_func("/ahci/cdrom/eject", test_atapi_tray);
  1635. ret = g_test_run();
  1636. /* Cleanup */
  1637. unlink(tmp_path);
  1638. unlink(debug_path);
  1639. unlink(mig_socket);
  1640. return ret;
  1641. }