From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id AC9FEA034C for ; Fri, 25 Feb 2022 18:16:47 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id A6A9A410FD; Fri, 25 Feb 2022 18:16:47 +0100 (CET) Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) by mails.dpdk.org (Postfix) with ESMTP id 6AED841153 for ; Fri, 25 Feb 2022 18:16:45 +0100 (CET) Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 44ACC3F1BC for ; Fri, 25 Feb 2022 17:16:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1645809405; bh=gyZaw56aNSotRSRrtJkWKS+PLjvKN15MWWl1+yfkemY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Kf1hodsthlO7bCb7mstvvOjpf0uZKi9yoFC2tFjjzVINt7TP/+KQXgXiTR7vf7lUw oEQJSX2b/pXyr6MdbivEcEiBBe+QPRqIGUJu+8pkDLP9Nhzc12P3JSiwHPlYSVN3Rf tx/MtklEgudDK7cr6VCgYv84L2lYyJd3xbKC32sQ2xxinJj//3+yRQ1zF+0DSPSUXp VBGOzZDbw1HyndVPmtSD+azL0cPE2gaKkCQcC7ukhyKFw1cBBAALYYHNycoP+DMtrZ dDe+dzF+W256iLxWHpqOYnoEMoB8airc0LvYqR8vG1iT/b6pt9R8AjRo0+OLo8bL/3 BTCa/Pw9LBSwg== Received: by mail-wm1-f69.google.com with SMTP id i131-20020a1c3b89000000b0037bb9f6feeeso1833915wma.5 for ; Fri, 25 Feb 2022 09:16:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gyZaw56aNSotRSRrtJkWKS+PLjvKN15MWWl1+yfkemY=; b=d2VoBmCB621w8DJ7MqlzayxIiKwgxvsxuFYCFNoZ7ap8+0spZi49Id1kSzsJd3hzKh yFbL7VCKJMaUhKFAbhvipDr2uwv3faG5sWQqo8sQZLDGm2e6UYChTTisVkBaWK79KkcM YuS1hgPZXESPUAmxgxnvtxRWjaziaJOKAIqpoNTqlehXlyDRuH1cP04/epLsh02v663X 1IPChN2ezVaudy0gtEwsXhEW/V3uAIKNIBmOCHr7pd19Fa5z8SFqPrn2maoYytUrZpYL hFWG/gXpDdSJLQO6TExjbzy6HBTJeeBPlofNO0jl0rsZkFcpVdQ2dx3UyOzbeoP94LMP NShw== X-Gm-Message-State: AOAM533D3k98wBpNcpWGeYg9I0adVGO1SDC2Fs7D6WBOWeA+zwqFhvXO PxJfdqEcS/V9lFOxSL0VoD6FB8D8uuwU4xUYOeZmvd8Q/BMQiOWKOSng//hHgLlFPefqmlYh7Rs gufcu+ljR9gewA2W+9j2BPbf0 X-Received: by 2002:a05:6000:1547:b0:1ea:7d56:83e8 with SMTP id 7-20020a056000154700b001ea7d5683e8mr7099974wry.404.1645809404920; Fri, 25 Feb 2022 09:16:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJzudyroL0X32curBbK3i3ukKhpseK/6aWPHSg9XFQqH8Y6vhVnT93A4COjGD085k4JaJc8C4A== X-Received: by 2002:a05:6000:1547:b0:1ea:7d56:83e8 with SMTP id 7-20020a056000154700b001ea7d5683e8mr7099948wry.404.1645809404623; Fri, 25 Feb 2022 09:16:44 -0800 (PST) Received: from Keschdeichel.fritz.box (068-133-067-156.ip-addr.inexio.net. [156.67.133.68]) by smtp.gmail.com with ESMTPSA id f12-20020a05600c154c00b0037df2e5d8c9sm3533984wmg.27.2022.02.25.09.16.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Feb 2022 09:16:44 -0800 (PST) From: christian.ehrhardt@canonical.com To: Tianfei Zhang Cc: Rosen Xu , dpdk stable Subject: patch 'raw/ifpga/base: fix SPI transaction' has been queued to stable release 19.11.12 Date: Fri, 25 Feb 2022 18:15:12 +0100 Message-Id: <20220225171550.3499040-19-christian.ehrhardt@canonical.com> X-Mailer: git-send-email 2.35.0 In-Reply-To: <20220225171550.3499040-1-christian.ehrhardt@canonical.com> References: <20220225171550.3499040-1-christian.ehrhardt@canonical.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: stable@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: patches for DPDK stable branches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: stable-bounces@dpdk.org Hi, FYI, your patch has been queued to stable release 19.11.12 Note it hasn't been pushed to http://dpdk.org/browse/dpdk-stable yet. It will be pushed if I get no objections before 02/27/22. So please shout if anyone has objections. Also note that after the patch there's a diff of the upstream commit vs the patch applied to the branch. This will indicate if there was any rebasing needed to apply to the stable branch. If there were code changes for rebasing (ie: not only metadata diffs), please double check that the rebase was correctly done. Queued patches are on a temporary branch at: https://github.com/cpaelzer/dpdk-stable-queue This queued commit can be viewed at: https://github.com/cpaelzer/dpdk-stable-queue/commit/30c9deb946a2adcd873b1e69fb604f7fe7926e0a Thanks. Christian Ehrhardt --- >From 30c9deb946a2adcd873b1e69fb604f7fe7926e0a Mon Sep 17 00:00:00 2001 From: Tianfei Zhang Date: Tue, 18 Jan 2022 20:44:59 -0500 Subject: [PATCH] raw/ifpga/base: fix SPI transaction [ upstream commit eacdd03c79f5d8adfa7d0dad1e75657dbaf4f788 ] When EOP is detected, 2 more bytes should be received (may be a SPI_PACKET_ESC before last valid byte) then rx should be finished. Fixes: 96ebfcf8125c ("raw/ifpga/base: add SPI and MAX10 device driver") Signed-off-by: Tianfei Zhang Acked-by: Rosen Xu --- drivers/raw/ifpga/base/opae_spi.c | 12 + drivers/raw/ifpga/base/opae_spi.h | 4 + drivers/raw/ifpga/base/opae_spi_transaction.c | 215 ++++++++++-------- 3 files changed, 140 insertions(+), 91 deletions(-) diff --git a/drivers/raw/ifpga/base/opae_spi.c b/drivers/raw/ifpga/base/opae_spi.c index bfdc83e6cd..d2c955e6b3 100644 --- a/drivers/raw/ifpga/base/opae_spi.c +++ b/drivers/raw/ifpga/base/opae_spi.c @@ -239,6 +239,18 @@ int spi_command(struct altera_spi_device *dev, unsigned int chip_select, return 0; } +int spi_write(struct altera_spi_device *dev, unsigned int chip_select, + unsigned int wlen, void *wdata) +{ + return spi_command(dev, chip_select, wlen, wdata, 0, NULL); +} + +int spi_read(struct altera_spi_device *dev, unsigned int chip_select, + unsigned int rlen, void *rdata) +{ + return spi_command(dev, chip_select, 0, NULL, rlen, rdata); +} + struct altera_spi_device *altera_spi_alloc(void *base, int type) { struct altera_spi_device *spi_dev = diff --git a/drivers/raw/ifpga/base/opae_spi.h b/drivers/raw/ifpga/base/opae_spi.h index 73a2276739..e0f87a9088 100644 --- a/drivers/raw/ifpga/base/opae_spi.h +++ b/drivers/raw/ifpga/base/opae_spi.h @@ -112,6 +112,10 @@ struct spi_tran_header { u32 addr; }; +int spi_read(struct altera_spi_device *dev, unsigned int chip_select, + unsigned int rlen, void *rdata); +int spi_write(struct altera_spi_device *dev, unsigned int chip_select, + unsigned int wlen, void *wdata); int spi_command(struct altera_spi_device *dev, unsigned int chip_select, unsigned int wlen, void *wdata, unsigned int rlen, void *rdata); void spi_cs_deactivate(struct altera_spi_device *dev); diff --git a/drivers/raw/ifpga/base/opae_spi_transaction.c b/drivers/raw/ifpga/base/opae_spi_transaction.c index d13d2fbc83..99131a1f76 100644 --- a/drivers/raw/ifpga/base/opae_spi_transaction.c +++ b/drivers/raw/ifpga/base/opae_spi_transaction.c @@ -40,7 +40,7 @@ static void print_buffer(const char *string, void *buffer, int len) printf("%s print buffer, len=%d\n", string, len); for (i = 0; i < len; i++) - printf("%x ", *(p+i)); + printf("%02x ", *(p+i)); printf("\n"); } #else @@ -72,43 +72,6 @@ static void reorder_phy_data(u8 bits_per_word, } } -enum { - SPI_FOUND_SOP, - SPI_FOUND_EOP, - SPI_NOT_FOUND, -}; - -static int resp_find_sop_eop(unsigned char *resp, unsigned int len, - int flags) -{ - int ret = SPI_NOT_FOUND; - - unsigned char *b = resp; - - /* find SOP */ - if (flags != SPI_FOUND_SOP) { - while (b < resp + len && *b != SPI_PACKET_SOP) - b++; - - if (*b != SPI_PACKET_SOP) - goto done; - - ret = SPI_FOUND_SOP; - } - - /* find EOP */ - while (b < resp + len && *b != SPI_PACKET_EOP) - b++; - - if (*b != SPI_PACKET_EOP) - goto done; - - ret = SPI_FOUND_EOP; - -done: - return ret; -} - static void phy_tx_pad(unsigned char *phy_buf, unsigned int phy_buf_len, unsigned int *aligned_len) { @@ -137,6 +100,104 @@ static void phy_tx_pad(unsigned char *phy_buf, unsigned int phy_buf_len, *p++ = SPI_BYTE_IDLE; } +#define RX_ALL_IDLE_DATA (SPI_BYTE_IDLE << 24 | SPI_BYTE_IDLE << 16 | \ + SPI_BYTE_IDLE << 8 | SPI_BYTE_IDLE) + +static bool all_idle_data(u8 *rxbuf) +{ + return *(u32 *)rxbuf == RX_ALL_IDLE_DATA; +} + +static unsigned char *find_eop(u8 *rxbuf, u32 BPW) +{ + return memchr(rxbuf, SPI_PACKET_EOP, BPW); +} + +static int do_spi_txrx(struct spi_transaction_dev *dev, + unsigned char *tx_buffer, + unsigned int tx_len, unsigned char *rx_buffer, + unsigned int rx_len, + unsigned int *actual_rx) +{ + unsigned int rx_cnt = 0; + int ret = 0; + unsigned int BPW = 4; + bool eop_found = false; + unsigned char *eop; + unsigned char *ptr; + unsigned char *rxbuf = rx_buffer; + int add_byte = 0; + unsigned long ticks; + unsigned long timeout; + + /* send command */ + ret = spi_write(dev->dev, dev->chipselect, tx_len, tx_buffer); + if (ret) + return -EBUSY; + + timeout = rte_get_timer_cycles() + + msecs_to_timer_cycles(2000); + + /* read out data */ + while (rx_cnt < rx_len) { + ret = spi_read(dev->dev, dev->chipselect, BPW, rxbuf); + if (ret) + return -EBUSY; + + /* skip all of invalid data */ + if (!eop_found && all_idle_data(rxbuf)) { + ticks = rte_get_timer_cycles(); + if (!time_after(ticks, timeout)) { + continue; + } else { + dev_err(dev, "read spi data timeout\n"); + return -ETIMEDOUT; + } + } + + rx_cnt += BPW; + if (!eop_found) { + /* EOP is found, we read 2 more bytes and exit. */ + eop = find_eop(rxbuf, BPW); + if (eop) { + if ((BPW + rxbuf - eop) > 2) { + /* + * check if the last 2 bytes are already + * received in current word. + */ + break; + } else if ((BPW + rxbuf - eop) == 2) { + /* + * skip if last byte is not SPI_BYTE_ESC + * or SPI_PACKET_ESC. this is the valid + * end of a response too. + */ + ptr = eop + 1; + + if (*ptr != SPI_BYTE_ESC && + *ptr != SPI_PACKET_ESC) + break; + + add_byte = 1; + } else { + add_byte = 2; + } + + rx_len = min(rx_len, + IFPGA_ALIGN(rx_cnt + + add_byte, BPW)); + eop_found = true; + } + } + rxbuf += BPW; + } + + *actual_rx = rx_cnt; + print_buffer("found valid data:", rx_buffer, rx_cnt); + + return ret; +} + static int byte_to_core_convert(struct spi_transaction_dev *dev, unsigned int send_len, unsigned char *send_data, unsigned int resp_len, unsigned char *resp_data, @@ -148,15 +209,9 @@ static int byte_to_core_convert(struct spi_transaction_dev *dev, unsigned char *resp_packet = dev->buffer->bytes_resp; unsigned char *p; unsigned char current_byte; - unsigned char *tx_buffer; unsigned int tx_len = 0; - unsigned char *rx_buffer; - unsigned int rx_len = 0; - int retry = 0; - int spi_flags; - unsigned long timeout = msecs_to_timer_cycles(1000); - unsigned long ticks; unsigned int resp_max_len = 2 * resp_len; + unsigned int actual_rx; print_buffer("before bytes:", send_data, send_len); @@ -190,48 +245,15 @@ static int byte_to_core_convert(struct spi_transaction_dev *dev, print_buffer("after order to spi:", send_packet, tx_len); - /* call spi */ - tx_buffer = send_packet; - rx_buffer = resp_packet; - rx_len = resp_max_len; - spi_flags = SPI_NOT_FOUND; - -read_again: - ret = spi_command(dev->dev, dev->chipselect, tx_len, tx_buffer, - rx_len, rx_buffer); + ret = do_spi_txrx(dev, send_packet, tx_len, resp_packet, + resp_max_len, &actual_rx); if (ret) - return -EBUSY; - - print_buffer("read from spi:", rx_buffer, rx_len); - - /* look for SOP firstly*/ - ret = resp_find_sop_eop(rx_buffer, rx_len - 1, spi_flags); - if (ret != SPI_FOUND_EOP) { - tx_buffer = NULL; - tx_len = 0; - ticks = rte_get_timer_cycles(); - if (time_after(ticks, timeout) && - retry++ > SPI_MAX_RETRY) { - dev_err(NULL, "Have retry %d, found invalid packet data\n", - retry); - return -EBUSY; - } - - if (ret == SPI_FOUND_SOP) { - rx_buffer += rx_len; - resp_max_len += rx_len; - } - - spi_flags = ret; - goto read_again; - } - - print_buffer("found valid data:", resp_packet, resp_max_len); + return ret; /* analyze response packet */ i = 0; p = resp_data; - while (i < resp_max_len) { + while (i < actual_rx) { current_byte = resp_packet[i]; switch (current_byte) { case SPI_BYTE_IDLE: @@ -337,9 +359,13 @@ static int packet_to_byte_conver(struct spi_transaction_dev *dev, current_byte = resp_packet[i]; switch (current_byte) { - case SPI_PACKET_ESC: - case SPI_PACKET_CHANNEL: case SPI_PACKET_SOP: + dev_err(dev, "error on get SOP after SOP\n"); + return -EINVAL; + case SPI_PACKET_CHANNEL: + i += 2; + break; + case SPI_PACKET_ESC: i++; current_byte = resp_packet[i]; *p++ = xor_20(current_byte); @@ -348,23 +374,30 @@ static int packet_to_byte_conver(struct spi_transaction_dev *dev, case SPI_PACKET_EOP: i++; current_byte = resp_packet[i]; - if (current_byte == SPI_PACKET_ESC || - current_byte == SPI_PACKET_CHANNEL || - current_byte == SPI_PACKET_SOP) { + switch (current_byte) { + case SPI_PACKET_ESC: i++; current_byte = resp_packet[i]; *p++ = xor_20(current_byte); - } else + break; + case SPI_PACKET_CHANNEL: + case SPI_PACKET_SOP: + case SPI_PACKET_EOP: + dev_err(dev, "error get SOP/EOP after EOP\n"); + return -EINVAL; + default: *p++ = current_byte; - i = valid_resp_len; - break; + break; + } + goto done; + default: *p++ = current_byte; i++; } - } +done: *valid = p - resp_buf; print_buffer("after packet:", resp_buf, *valid); -- 2.35.0 --- Diff of the applied patch vs upstream commit (please double-check if non-empty: --- --- - 2022-02-25 16:58:45.032924906 +0100 +++ 0019-raw-ifpga-base-fix-SPI-transaction.patch 2022-02-25 16:58:44.220990396 +0100 @@ -1 +1 @@ -From eacdd03c79f5d8adfa7d0dad1e75657dbaf4f788 Mon Sep 17 00:00:00 2001 +From 30c9deb946a2adcd873b1e69fb604f7fe7926e0a Mon Sep 17 00:00:00 2001 @@ -5,0 +6,2 @@ +[ upstream commit eacdd03c79f5d8adfa7d0dad1e75657dbaf4f788 ] + @@ -11 +12,0 @@ -Cc: stable@dpdk.org @@ -22 +23 @@ -index 9efeecb791..ca3d41fb92 100644 +index bfdc83e6cd..d2c955e6b3 100644 @@ -45 +46 @@ -index af11656e4d..bcff67dd66 100644 +index 73a2276739..e0f87a9088 100644 @@ -48 +49 @@ -@@ -117,6 +117,10 @@ struct spi_tran_header { +@@ -112,6 +112,10 @@ struct spi_tran_header { @@ -60 +61 @@ -index 006cdb4c1a..cd50d40629 100644 +index d13d2fbc83..99131a1f76 100644