From 737a5851dfe7f8bb9e293d4caa924f3d54697502 Mon Sep 17 00:00:00 2001
From: daniel-k <github@daniel-krebs.net>
Date: Tue, 28 Nov 2017 11:26:41 +0100
Subject: [PATCH] lib/card: start FPGA card prior to parsing

Initializing IPs may want to probe the actual hardware for feature
detection (e.g. DMA), so the card has to be started in order to access
any memory on the card.
---
 fpga/lib/card.c   | 4 +++-
 fpga/tests/main.c | 6 ++++++
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/fpga/lib/card.c b/fpga/lib/card.c
index 87d327466..f7606d0a0 100644
--- a/fpga/lib/card.c
+++ b/fpga/lib/card.c
@@ -167,7 +167,7 @@ int fpga_card_start(struct fpga_card *c)
 
 	struct pci_device *pdev;
 
-	assert(c->state == STATE_CHECKED);
+	assert(c->state == STATE_INITIALIZED);
 
 	/* Search for FPGA card */
 	pdev = pci_lookup_device(c->pci, &c->filter);
@@ -258,6 +258,8 @@ void fpga_card_dump(struct fpga_card *c)
 
 int fpga_card_check(struct fpga_card *c)
 {
+	assert(c->state == STATE_PARSED);
+
 	/* Check FPGA configuration */
 	c->reset = fpga_vlnv_lookup(&c->ips, &(struct fpga_vlnv) { "xilinx.com", "ip", "axi_gpio", NULL });
 	if (!c->reset)
diff --git a/fpga/tests/main.c b/fpga/tests/main.c
index 5c9a62d9e..41549943f 100644
--- a/fpga/tests/main.c
+++ b/fpga/tests/main.c
@@ -76,9 +76,15 @@ static void init()
 	ret = fpga_card_init(card, &pci, &vc);
 	cr_assert_eq(ret, 0, "FPGA card initialization failed");
 
+	ret = fpga_card_start(card);
+	cr_assert_eq(ret, 0, "FPGA card cannot be started");
+
 	ret = fpga_card_parse(card, json_card, FPGA_CARD);
 	cr_assert_eq(ret, 0, "Failed to parse FPGA config");
 
+	ret = fpga_card_check(card);
+	cr_assert_eq(ret, 0, "FPGA card check failed");
+
 	json_decref(json);
 
 	if (criterion_options.logging_threshold < CRITERION_IMPORTANT)