[PATCH iwl-next 11/15] idpf: psp: add psp tx key state
From: Stan Iliev
Date: Tue Nov 17 2026 - 09:58:03 EST
TX keys are inserted into the SADB asynchronously: the SA index of a
key is only known when the CP replies. Add struct idpf_psp_tx_key, the
reference counted state of a TX key, stored in the driver data of its
PSP association in a later patch:
- idpf_psp_tx_key_alloc(), idpf_psp_tx_key_get() and
idpf_psp_tx_key_put(). The TX path reads the SA index of the key
under RCU: the key is freed after an RCU grace period.
- The SA index is IDPF_PSP_IDX_IN_PROG while the insertion is pending,
IDPF_PSP_IDX_ERROR if it failed and IDPF_PSP_IDX_DELETED once the key
was deleted, see idpf_psp_idx_valid().
- idpf_psp_saidx_ok() checks the SA indexes returned by the CP against
the SA index width negotiated for the adapter.
Signed-off-by: Stan Iliev <stani@xxxxxxxxxx>
---
drivers/net/ethernet/intel/idpf/idpf_psp.c | 51 +++++++++++++++++++
drivers/net/ethernet/intel/idpf/idpf_psp.h | 31 +++++++++++
.../net/ethernet/intel/idpf/idpf_psp_def.h | 30 +++++++++++
3 files changed, 112 insertions(+)
diff --git a/drivers/net/ethernet/intel/idpf/idpf_psp.c b/drivers/net/ethernet/intel/idpf/idpf_psp.c
index 844463754632..ef23b1699b09 100644
--- a/drivers/net/ethernet/intel/idpf/idpf_psp.c
+++ b/drivers/net/ethernet/intel/idpf/idpf_psp.c
@@ -128,6 +128,57 @@ void idpf_psp_free(struct idpf_adapter *adapter)
idpf_psp_unregister(adapter);
}
+/**
+ * idpf_psp_tx_key_alloc - Allocate the state of a PSP TX key
+ *
+ * Return: the TX key with a reference, NULL if it couldn't be allocated.
+ */
+struct idpf_psp_tx_key *idpf_psp_tx_key_alloc(void)
+{
+ struct idpf_psp_tx_key *key;
+
+ key = kzalloc_obj(*key);
+ if (!key)
+ return NULL;
+
+ kref_init(&key->ref);
+ spin_lock_init(&key->lock);
+ key->idx = IDPF_PSP_IDX_IN_PROG;
+
+ return key;
+}
+
+/**
+ * idpf_psp_tx_key_free - Free the state of a PSP TX key
+ * @ref: reference count of the TX key
+ */
+static void idpf_psp_tx_key_free(struct kref *ref)
+{
+ struct idpf_psp_tx_key *key = container_of(ref, struct idpf_psp_tx_key,
+ ref);
+
+ /* The TX path may still read the SA index of the key */
+ kfree_rcu(key, rcu);
+}
+
+/**
+ * idpf_psp_tx_key_get - Get a reference on a PSP TX key
+ * @key: the TX key
+ */
+void idpf_psp_tx_key_get(struct idpf_psp_tx_key *key)
+{
+ kref_get(&key->ref);
+}
+
+/**
+ * idpf_psp_tx_key_put - Release a reference on a PSP TX key
+ * @key: the TX key
+ */
+void idpf_psp_tx_key_put(struct idpf_psp_tx_key *key)
+{
+ kref_put(&key->ref, idpf_psp_tx_key_free);
+}
+
/**
* idpf_psp_set_config - Set the PSP device configuration
* @psd: PSP device
diff --git a/drivers/net/ethernet/intel/idpf/idpf_psp.h b/drivers/net/ethernet/intel/idpf/idpf_psp.h
index 34534076da47..44ac750e2c3b 100644
--- a/drivers/net/ethernet/intel/idpf/idpf_psp.h
+++ b/drivers/net/ethernet/intel/idpf/idpf_psp.h
@@ -16,6 +16,37 @@ void idpf_psp_recv_late_reply(struct idpf_adapter *adapter,
void idpf_psp_rotation_event(struct idpf_adapter *adapter,
const struct libie_ctlq_msg *ctlq_msg);
+struct idpf_psp_tx_key *idpf_psp_tx_key_alloc(void);
+void idpf_psp_tx_key_get(struct idpf_psp_tx_key *key);
+void idpf_psp_tx_key_put(struct idpf_psp_tx_key *key);
+
+/**
+ * idpf_psp_saidx_ok - Check an SA index returned by the CP
+ * @adapter: private data struct
+ * @saidx: the SA index
+ *
+ * Return: true if @saidx fits in the SA index width negotiated for the
+ * adapter, false otherwise.
+ */
+static inline bool idpf_psp_saidx_ok(const struct idpf_adapter *adapter,
+ u32 saidx)
+{
+ return saidx < BIT(adapter->psp.saidx_width);
+}
+
+/**
+ * idpf_psp_idx_valid - Check if the SA index of a TX key is usable
+ * @idx: SA index or state of a TX key, see struct idpf_psp_tx_key
+ *
+ * Only SA indexes checked with idpf_psp_saidx_ok() are stored in a TX key.
+ *
+ * Return: true if @idx is an SA index, false if it is a state.
+ */
+static inline bool idpf_psp_idx_valid(u32 idx)
+{
+ return idx <= IDPF_PSP_IDX_MAX;
+}
+
#else /* !CONFIG_INET_PSP */
static inline void idpf_psp_init(struct idpf_adapter *adapter) { }
diff --git a/drivers/net/ethernet/intel/idpf/idpf_psp_def.h b/drivers/net/ethernet/intel/idpf/idpf_psp_def.h
index fdd65bc5321a..9ba99e08853a 100644
--- a/drivers/net/ethernet/intel/idpf/idpf_psp_def.h
+++ b/drivers/net/ethernet/intel/idpf/idpf_psp_def.h
@@ -3,6 +3,8 @@
#ifndef _IDPF_PSP_DEF_H_
#define _IDPF_PSP_DEF_H_
+#include <linux/kref.h>
+#include <linux/spinlock.h>
#include <linux/net/intel/virtchnl2.h>
#include <net/psp.h>
@@ -12,6 +14,34 @@
#define IDPF_PSP_SAIDX_WIDTH_21 21
#define IDPF_PSP_SAIDX_WIDTH_26 26
+/* Largest SA index the driver supports. The SA indexes the CP returns are
+ * checked against the width negotiated for the adapter before they are used,
+ * see idpf_psp_saidx_ok().
+ */
+#define IDPF_PSP_IDX_MAX (BIT(IDPF_PSP_SAIDX_WIDTH_26) - 1)
+/* States of a TX key without a usable SA index, see struct idpf_psp_tx_key */
+#define IDPF_PSP_IDX_NONE (U32_MAX - 3) /* no SA index */
+#define IDPF_PSP_IDX_IN_PROG (U32_MAX - 2) /* insertion pending */
+#define IDPF_PSP_IDX_ERROR (U32_MAX - 1) /* insertion failed */
+#define IDPF_PSP_IDX_DELETED U32_MAX /* key deleted */
+
+/**
+ * struct idpf_psp_tx_key - State of a PSP TX key
+ * @ref: reference count, held by the PSP association, see
+ * idpf_psp_tx_key_add(), and by the lists and the insert request the
+ * key is on
+ * @lock: protects @idx and @hw_idx updates, and @tx_queue
+ * @idx: SA index the TX path uses, or IDPF_PSP_IDX_IN_PROG,
+ * IDPF_PSP_IDX_ERROR or IDPF_PSP_IDX_DELETED. Read without @lock.
+ * @rcu: for freeing the key after the RCU readers of the TX path
+ */
+struct idpf_psp_tx_key {
+ struct kref ref;
+ spinlock_t lock; /* protects the SA indexes and tx_queue */
+ u32 idx;
+ struct rcu_head rcu;
+};
+
/**
* struct idpf_psp_rx_key - PSP RX key allocated by the CP
* @spi: SPI of the key
--
2.56.0.679.gc527c65306-goog