git: dbc92a6d9d2b - stable/13 - ena: rework tx req_id validation logic

From: Marcin Wojtas <mw_at_FreeBSD.org>
Date: Thu, 24 Feb 2022 12:55:46 UTC
The branch stable/13 has been updated by mw:

URL: https://cgit.FreeBSD.org/src/commit/?id=dbc92a6d9d2b436b50a9098aa6b3dfc8f61e1060

commit dbc92a6d9d2b436b50a9098aa6b3dfc8f61e1060
Author:     Artur Rojek <ar@semihalf.com>
AuthorDate: 2022-01-03 13:50:06 +0000
Commit:     Marcin Wojtas <mw@FreeBSD.org>
CommitDate: 2022-02-24 12:53:43 +0000

    ena: rework tx req_id validation logic
    
    Since `ena_com_tx_comp_req_id_get` already checks for `req_id` validity,
    the logic was exiting early, never giving `validate_tx_req_id` a chance
    to trigger device reset.
    Rewrite the logic so that device reset is called based on return value
    of `ena_com_tx_comp_req_id_get` instead.
    
    Submitted by: Artur Rojek <ar@semihalf.com>
    Obtained from: Semihalf
    MFC after: 2 weeks
    Sponsored by: Amazon, Inc.
    
    (cherry picked from commit b168d0c850f34e8178d815ba3a87de2657a02ca9)
---
 sys/dev/ena/ena_datapath.c | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/sys/dev/ena/ena_datapath.c b/sys/dev/ena/ena_datapath.c
index 3026671b6a19..c6798b3f7eb3 100644
--- a/sys/dev/ena/ena_datapath.c
+++ b/sys/dev/ena/ena_datapath.c
@@ -48,7 +48,8 @@ __FBSDID("$FreeBSD$");
 
 static int	ena_tx_cleanup(struct ena_ring *);
 static int	ena_rx_cleanup(struct ena_ring *);
-static inline int validate_tx_req_id(struct ena_ring *, uint16_t);
+static inline int ena_get_tx_req_id(struct ena_ring *tx_ring,
+    struct ena_com_io_cq *io_cq, uint16_t *req_id);
 static void	ena_rx_hash_mbuf(struct ena_ring *, struct ena_com_rx_ctx *,
     struct mbuf *);
 static struct mbuf* ena_rx_mbuf(struct ena_ring *, struct ena_com_rx_buf_info *,
@@ -200,23 +201,27 @@ ena_qflush(if_t ifp)
  *********************************************************************/
 
 static inline int
-validate_tx_req_id(struct ena_ring *tx_ring, uint16_t req_id)
+ena_get_tx_req_id(struct ena_ring *tx_ring, struct ena_com_io_cq *io_cq,
+    uint16_t *req_id)
 {
 	struct ena_adapter *adapter = tx_ring->adapter;
-	struct ena_tx_buffer *tx_info = NULL;
+	int rc;
 
-	if (likely(req_id < tx_ring->ring_size)) {
-		tx_info = &tx_ring->tx_buffer_info[req_id];
-		if (tx_info->mbuf != NULL)
-			return (0);
-		ena_log(adapter->pdev, ERR,
-		    "tx_info doesn't have valid mbuf\n");
+	rc = ena_com_tx_comp_req_id_get(io_cq, req_id);
+	if (rc == ENA_COM_TRY_AGAIN)
+		return (EAGAIN);
+
+	if (unlikely(rc != 0)) {
+		ena_log(adapter->pdev, ERR, "Invalid req_id: %hu\n", *req_id);
+		counter_u64_add(tx_ring->tx_stats.bad_req_id, 1);
+		goto err;
 	}
 
-	ena_log(adapter->pdev, ERR, "Invalid req_id: %hu\n", req_id);
-	counter_u64_add(tx_ring->tx_stats.bad_req_id, 1);
+	if (tx_ring->tx_buffer_info[*req_id].mbuf != NULL)
+		return (0);
 
-	/* Trigger device reset */
+	ena_log(adapter->pdev, ERR, "tx_info doesn't have valid mbuf\n");
+err:
 	ena_trigger_reset(adapter, ENA_REGS_RESET_INV_TX_REQ_ID);
 
 	return (EFAULT);
@@ -262,11 +267,7 @@ ena_tx_cleanup(struct ena_ring *tx_ring)
 		struct ena_tx_buffer *tx_info;
 		struct mbuf *mbuf;
 
-		rc = ena_com_tx_comp_req_id_get(io_cq, &req_id);
-		if (unlikely(rc != 0))
-			break;
-
-		rc = validate_tx_req_id(tx_ring, req_id);
+		rc = ena_get_tx_req_id(tx_ring, io_cq, &req_id);
 		if (unlikely(rc != 0))
 			break;