netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library
@ 2024-09-05  8:06 Daniel Machon
  2024-09-05  8:06 ` [PATCH net-next 01/12] net: lan966x: select " Daniel Machon
                   ` (12 more replies)
  0 siblings, 13 replies; 16+ messages in thread
From: Daniel Machon @ 2024-09-05  8:06 UTC (permalink / raw)
  To: Horatiu Vultur, UNGLinuxDriver, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend
  Cc: netdev, linux-kernel, bpf

This patch series is the second of a 2-part series [1], that adds a new
common FDMA library for Microchip switch chips Sparx5 and lan966x. These
chips share the same FDMA engine, and as such will benefit from a common
library with a common implementation.  This also has the benefit of
removing a lot of open-coded bookkeeping and duplicate code for the two
drivers.

In this second series, the FDMA library will be taken into use by the
lan966x switch driver.

 ###################
 # Example of use: #
 ###################

- Initialize the rx and tx fdma structs with values for: number of
  DCB's, number of DB's, channel ID, DB size (data buffer size), and
  total size of the requested memory. Also provide two callbacks:
  nextptr_cb() and dataptr_cb() for getting the nextptr and dataptr.

- Allocate memory using fdma_alloc_phys() or fdma_alloc_coherent().

- Initialize the DCB's with fdma_dcb_init().

- Add new DCB's with fdma_dcb_add().

- Free memory with fdma_free_phys() or fdma_free_coherent().

 #####################
 # Patch  breakdown: #
 #####################

Patch #1:  select FDMA library for lan966x.

Patch #2:  includes the fdma_api.h header and removes old symbols.

Patch #3:  replaces old rx and tx variables with equivalent ones from the
           fdma struct. Only the variables that can be changed without
           breaking traffic is changed in this patch.

Patch #4:  uses the library for allocation of rx buffers. This requires
           quite a bit of refactoring in this single patch.

Patch #5:  uses the library for adding DCB's in the rx path.

Patch #6:  uses the library for freeing rx buffers.

Patch #7:  uses the library for allocation of tx buffers. This requires
           quite a bit of refactoring in this single patch.

Patch #8:  uses the library for adding DCB's in the tx path.

Patch #9:  uses the library helpers in the tx path.

Patch #10: ditch last_in_use variable and use library instead.

Patch #11: uses library helpers throughout.

Patch #12: refactor lan966x_fdma_reload() function.

[1] https://lore.kernel.org/netdev/20240902-fdma-sparx5-v1-0-1e7d5e5a9f34@microchip.com/

Signed-off-by: Daniel Machon <daniel.machon@microchip.com>
---
Daniel Machon (12):
      net: lan966x: select FDMA library
      net: lan966x: use FDMA library symbols
      net: lan966x: replace a few variables with new equivalent ones
      net: lan966x: use the FDMA library for allocation of rx buffers
      net: lan966x: use FDMA library for adding DCB's in the rx path
      net: lan966x: use library helper for freeing rx buffers
      net: lan966x: use the FDMA library for allocation of tx buffers
      net: lan966x: use FDMA library for adding DCB's in the tx path
      net: lan966x: use library helper for freeing tx buffers
      net: lan966x: ditch tx->last_in_use variable
      net: lan966x: use a few FDMA helpers throughout
      net: lan966x: refactor buffer reload function

 drivers/net/ethernet/microchip/lan966x/Kconfig     |   1 +
 drivers/net/ethernet/microchip/lan966x/Makefile    |   1 +
 .../net/ethernet/microchip/lan966x/lan966x_fdma.c  | 409 ++++++++-------------
 .../net/ethernet/microchip/lan966x/lan966x_main.h  |  58 +--
 4 files changed, 164 insertions(+), 305 deletions(-)
---
base-commit: ff09bc366fc45861b35dfeac97baadee16f65aec
change-id: 20240904-fdma-lan966x-04281c53952b

Best regards,
-- 
Daniel Machon <daniel.machon@microchip.com>


^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH net-next 01/12] net: lan966x: select FDMA library
  2024-09-05  8:06 [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library Daniel Machon
@ 2024-09-05  8:06 ` Daniel Machon
  2024-09-05  8:06 ` [PATCH net-next 02/12] net: lan966x: use FDMA library symbols Daniel Machon
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 16+ messages in thread
From: Daniel Machon @ 2024-09-05  8:06 UTC (permalink / raw)
  To: Horatiu Vultur, UNGLinuxDriver, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend
  Cc: netdev, linux-kernel, bpf

Select the newly introduced FDMA library.

Signed-off-by: Daniel Machon <daniel.machon@microchip.com>
Reviewed-by: Horatiu Vultur <horatiu.vultur@microchip.com>
---
 drivers/net/ethernet/microchip/lan966x/Kconfig | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/net/ethernet/microchip/lan966x/Kconfig b/drivers/net/ethernet/microchip/lan966x/Kconfig
index f9ebffc04eb8..f663b6e12466 100644
--- a/drivers/net/ethernet/microchip/lan966x/Kconfig
+++ b/drivers/net/ethernet/microchip/lan966x/Kconfig
@@ -8,6 +8,7 @@ config LAN966X_SWITCH
 	select PHYLINK
 	select PAGE_POOL
 	select VCAP
+	select FDMA
 	help
 	  This driver supports the Lan966x network switch device.
 

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH net-next 02/12] net: lan966x: use FDMA library symbols
  2024-09-05  8:06 [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library Daniel Machon
  2024-09-05  8:06 ` [PATCH net-next 01/12] net: lan966x: select " Daniel Machon
@ 2024-09-05  8:06 ` Daniel Machon
  2024-09-05  8:06 ` [PATCH net-next 03/12] net: lan966x: replace a few variables with new equivalent ones Daniel Machon
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 16+ messages in thread
From: Daniel Machon @ 2024-09-05  8:06 UTC (permalink / raw)
  To: Horatiu Vultur, UNGLinuxDriver, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend
  Cc: netdev, linux-kernel, bpf

Include and use the new FDMA header, which now provides the required
masks and bit offsets for operating on the DCB's and DB's.

Signed-off-by: Daniel Machon <daniel.machon@microchip.com>
Reviewed-by: Horatiu Vultur <horatiu.vultur@microchip.com>
---
 drivers/net/ethernet/microchip/lan966x/Makefile       |  1 +
 drivers/net/ethernet/microchip/lan966x/lan966x_main.h | 10 +---------
 2 files changed, 2 insertions(+), 9 deletions(-)

diff --git a/drivers/net/ethernet/microchip/lan966x/Makefile b/drivers/net/ethernet/microchip/lan966x/Makefile
index 3b6ac331691d..4cdbe263502c 100644
--- a/drivers/net/ethernet/microchip/lan966x/Makefile
+++ b/drivers/net/ethernet/microchip/lan966x/Makefile
@@ -20,3 +20,4 @@ lan966x-switch-$(CONFIG_DEBUG_FS) += lan966x_vcap_debugfs.o
 
 # Provide include files
 ccflags-y += -I$(srctree)/drivers/net/ethernet/microchip/vcap
+ccflags-y += -I$(srctree)/drivers/net/ethernet/microchip/fdma
diff --git a/drivers/net/ethernet/microchip/lan966x/lan966x_main.h b/drivers/net/ethernet/microchip/lan966x/lan966x_main.h
index f8bebbcf77b2..4d2aa775fbfd 100644
--- a/drivers/net/ethernet/microchip/lan966x/lan966x_main.h
+++ b/drivers/net/ethernet/microchip/lan966x/lan966x_main.h
@@ -16,6 +16,7 @@
 #include <net/switchdev.h>
 #include <net/xdp.h>
 
+#include <fdma_api.h>
 #include <vcap_api.h>
 #include <vcap_api_client.h>
 
@@ -76,15 +77,6 @@
 
 #define FDMA_RX_DCB_MAX_DBS		1
 #define FDMA_TX_DCB_MAX_DBS		1
-#define FDMA_DCB_INFO_DATAL(x)		((x) & GENMASK(15, 0))
-
-#define FDMA_DCB_STATUS_BLOCKL(x)	((x) & GENMASK(15, 0))
-#define FDMA_DCB_STATUS_SOF		BIT(16)
-#define FDMA_DCB_STATUS_EOF		BIT(17)
-#define FDMA_DCB_STATUS_INTR		BIT(18)
-#define FDMA_DCB_STATUS_DONE		BIT(19)
-#define FDMA_DCB_STATUS_BLOCKO(x)	(((x) << 20) & GENMASK(31, 20))
-#define FDMA_DCB_INVALID_DATA		0x1
 
 #define FDMA_XTR_CHANNEL		6
 #define FDMA_INJ_CHANNEL		0

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH net-next 03/12] net: lan966x: replace a few variables with new equivalent ones
  2024-09-05  8:06 [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library Daniel Machon
  2024-09-05  8:06 ` [PATCH net-next 01/12] net: lan966x: select " Daniel Machon
  2024-09-05  8:06 ` [PATCH net-next 02/12] net: lan966x: use FDMA library symbols Daniel Machon
@ 2024-09-05  8:06 ` Daniel Machon
  2024-09-05  8:06 ` [PATCH net-next 04/12] net: lan966x: use the FDMA library for allocation of rx buffers Daniel Machon
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 16+ messages in thread
From: Daniel Machon @ 2024-09-05  8:06 UTC (permalink / raw)
  To: Horatiu Vultur, UNGLinuxDriver, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend
  Cc: netdev, linux-kernel, bpf

Replace the old rx and tx variables: channel_id, FDMA_DCB_MAX,
FDMA_RX_DCB_MAX_DBS, FDMA_TX_DCB_MAX_DBS, dcb_index and db_index with
the equivalents from the FDMA rx and tx structs. These variables are not
entangled in any buffer allocation and can therefore be replaced in
advance.

Signed-off-by: Daniel Machon <daniel.machon@microchip.com>
Reviewed-by: Horatiu Vultur <horatiu.vultur@microchip.com>
---
 .../net/ethernet/microchip/lan966x/lan966x_fdma.c  | 131 ++++++++++++---------
 .../net/ethernet/microchip/lan966x/lan966x_main.h  |  19 +--
 2 files changed, 81 insertions(+), 69 deletions(-)

diff --git a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
index 3960534ac2ad..b64f04ff99a8 100644
--- a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
+++ b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
@@ -27,10 +27,11 @@ static struct page *lan966x_fdma_rx_alloc_page(struct lan966x_rx *rx,
 
 static void lan966x_fdma_rx_free_pages(struct lan966x_rx *rx)
 {
+	struct fdma *fdma = &rx->fdma;
 	int i, j;
 
-	for (i = 0; i < FDMA_DCB_MAX; ++i) {
-		for (j = 0; j < FDMA_RX_DCB_MAX_DBS; ++j)
+	for (i = 0; i < fdma->n_dcbs; ++i) {
+		for (j = 0; j < fdma->n_dbs; ++j)
 			page_pool_put_full_page(rx->page_pool,
 						rx->page[i][j], false);
 	}
@@ -38,9 +39,10 @@ static void lan966x_fdma_rx_free_pages(struct lan966x_rx *rx)
 
 static void lan966x_fdma_rx_free_page(struct lan966x_rx *rx)
 {
+	struct fdma *fdma = &rx->fdma;
 	struct page *page;
 
-	page = rx->page[rx->dcb_index][rx->db_index];
+	page = rx->page[fdma->dcb_index][fdma->db_index];
 	if (unlikely(!page))
 		return;
 
@@ -51,10 +53,11 @@ static void lan966x_fdma_rx_add_dcb(struct lan966x_rx *rx,
 				    struct lan966x_rx_dcb *dcb,
 				    u64 nextptr)
 {
+	struct fdma *fdma = &rx->fdma;
 	struct lan966x_db *db;
 	int i;
 
-	for (i = 0; i < FDMA_RX_DCB_MAX_DBS; ++i) {
+	for (i = 0; i < fdma->n_dbs; ++i) {
 		db = &dcb->db[i];
 		db->status = FDMA_DCB_STATUS_INTR;
 	}
@@ -72,7 +75,7 @@ static int lan966x_fdma_rx_alloc_page_pool(struct lan966x_rx *rx)
 	struct page_pool_params pp_params = {
 		.order = rx->page_order,
 		.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
-		.pool_size = FDMA_DCB_MAX,
+		.pool_size = rx->fdma.n_dcbs,
 		.nid = NUMA_NO_NODE,
 		.dev = lan966x->dev,
 		.dma_dir = DMA_FROM_DEVICE,
@@ -104,6 +107,7 @@ static int lan966x_fdma_rx_alloc_page_pool(struct lan966x_rx *rx)
 static int lan966x_fdma_rx_alloc(struct lan966x_rx *rx)
 {
 	struct lan966x *lan966x = rx->lan966x;
+	struct fdma *fdma = &rx->fdma;
 	struct lan966x_rx_dcb *dcb;
 	struct lan966x_db *db;
 	struct page *page;
@@ -114,7 +118,7 @@ static int lan966x_fdma_rx_alloc(struct lan966x_rx *rx)
 		return PTR_ERR(rx->page_pool);
 
 	/* calculate how many pages are needed to allocate the dcbs */
-	size = sizeof(struct lan966x_rx_dcb) * FDMA_DCB_MAX;
+	size = sizeof(struct lan966x_rx_dcb) * fdma->n_dcbs;
 	size = ALIGN(size, PAGE_SIZE);
 
 	rx->dcbs = dma_alloc_coherent(lan966x->dev, size, &rx->dma, GFP_KERNEL);
@@ -122,16 +126,16 @@ static int lan966x_fdma_rx_alloc(struct lan966x_rx *rx)
 		return -ENOMEM;
 
 	rx->last_entry = rx->dcbs;
-	rx->db_index = 0;
-	rx->dcb_index = 0;
+	fdma->db_index = 0;
+	fdma->dcb_index = 0;
 
 	/* Now for each dcb allocate the dbs */
-	for (i = 0; i < FDMA_DCB_MAX; ++i) {
+	for (i = 0; i < fdma->n_dcbs; ++i) {
 		dcb = &rx->dcbs[i];
 		dcb->info = 0;
 
 		/* For each db allocate a page and map it to the DB dataptr. */
-		for (j = 0; j < FDMA_RX_DCB_MAX_DBS; ++j) {
+		for (j = 0; j < fdma->n_dbs; ++j) {
 			db = &dcb->db[j];
 			page = lan966x_fdma_rx_alloc_page(rx, db);
 			if (!page)
@@ -149,17 +153,20 @@ static int lan966x_fdma_rx_alloc(struct lan966x_rx *rx)
 
 static void lan966x_fdma_rx_advance_dcb(struct lan966x_rx *rx)
 {
-	rx->dcb_index++;
-	rx->dcb_index &= FDMA_DCB_MAX - 1;
+	struct fdma *fdma = &rx->fdma;
+
+	fdma->dcb_index++;
+	fdma->dcb_index &= fdma->n_dcbs - 1;
 }
 
 static void lan966x_fdma_rx_free(struct lan966x_rx *rx)
 {
 	struct lan966x *lan966x = rx->lan966x;
+	struct fdma *fdma = &rx->fdma;
 	u32 size;
 
 	/* Now it is possible to do the cleanup of dcb */
-	size = sizeof(struct lan966x_tx_dcb) * FDMA_DCB_MAX;
+	size = sizeof(struct lan966x_tx_dcb) * fdma->n_dcbs;
 	size = ALIGN(size, PAGE_SIZE);
 	dma_free_coherent(lan966x->dev, size, rx->dcbs, rx->dma);
 }
@@ -167,21 +174,22 @@ static void lan966x_fdma_rx_free(struct lan966x_rx *rx)
 static void lan966x_fdma_rx_start(struct lan966x_rx *rx)
 {
 	struct lan966x *lan966x = rx->lan966x;
+	struct fdma *fdma = &rx->fdma;
 	u32 mask;
 
 	/* When activating a channel, first is required to write the first DCB
 	 * address and then to activate it
 	 */
 	lan_wr(lower_32_bits((u64)rx->dma), lan966x,
-	       FDMA_DCB_LLP(rx->channel_id));
+	       FDMA_DCB_LLP(fdma->channel_id));
 	lan_wr(upper_32_bits((u64)rx->dma), lan966x,
-	       FDMA_DCB_LLP1(rx->channel_id));
+	       FDMA_DCB_LLP1(fdma->channel_id));
 
-	lan_wr(FDMA_CH_CFG_CH_DCB_DB_CNT_SET(FDMA_RX_DCB_MAX_DBS) |
+	lan_wr(FDMA_CH_CFG_CH_DCB_DB_CNT_SET(fdma->n_dbs) |
 	       FDMA_CH_CFG_CH_INTR_DB_EOF_ONLY_SET(1) |
 	       FDMA_CH_CFG_CH_INJ_PORT_SET(0) |
 	       FDMA_CH_CFG_CH_MEM_SET(1),
-	       lan966x, FDMA_CH_CFG(rx->channel_id));
+	       lan966x, FDMA_CH_CFG(fdma->channel_id));
 
 	/* Start fdma */
 	lan_rmw(FDMA_PORT_CTRL_XTR_STOP_SET(0),
@@ -191,13 +199,13 @@ static void lan966x_fdma_rx_start(struct lan966x_rx *rx)
 	/* Enable interrupts */
 	mask = lan_rd(lan966x, FDMA_INTR_DB_ENA);
 	mask = FDMA_INTR_DB_ENA_INTR_DB_ENA_GET(mask);
-	mask |= BIT(rx->channel_id);
+	mask |= BIT(fdma->channel_id);
 	lan_rmw(FDMA_INTR_DB_ENA_INTR_DB_ENA_SET(mask),
 		FDMA_INTR_DB_ENA_INTR_DB_ENA,
 		lan966x, FDMA_INTR_DB_ENA);
 
 	/* Activate the channel */
-	lan_rmw(FDMA_CH_ACTIVATE_CH_ACTIVATE_SET(BIT(rx->channel_id)),
+	lan_rmw(FDMA_CH_ACTIVATE_CH_ACTIVATE_SET(BIT(fdma->channel_id)),
 		FDMA_CH_ACTIVATE_CH_ACTIVATE,
 		lan966x, FDMA_CH_ACTIVATE);
 }
@@ -205,18 +213,19 @@ static void lan966x_fdma_rx_start(struct lan966x_rx *rx)
 static void lan966x_fdma_rx_disable(struct lan966x_rx *rx)
 {
 	struct lan966x *lan966x = rx->lan966x;
+	struct fdma *fdma = &rx->fdma;
 	u32 val;
 
 	/* Disable the channel */
-	lan_rmw(FDMA_CH_DISABLE_CH_DISABLE_SET(BIT(rx->channel_id)),
+	lan_rmw(FDMA_CH_DISABLE_CH_DISABLE_SET(BIT(fdma->channel_id)),
 		FDMA_CH_DISABLE_CH_DISABLE,
 		lan966x, FDMA_CH_DISABLE);
 
 	readx_poll_timeout_atomic(lan966x_fdma_channel_active, lan966x,
-				  val, !(val & BIT(rx->channel_id)),
+				  val, !(val & BIT(fdma->channel_id)),
 				  READL_SLEEP_US, READL_TIMEOUT_US);
 
-	lan_rmw(FDMA_CH_DB_DISCARD_DB_DISCARD_SET(BIT(rx->channel_id)),
+	lan_rmw(FDMA_CH_DB_DISCARD_DB_DISCARD_SET(BIT(fdma->channel_id)),
 		FDMA_CH_DB_DISCARD_DB_DISCARD,
 		lan966x, FDMA_CH_DB_DISCARD);
 }
@@ -225,7 +234,7 @@ static void lan966x_fdma_rx_reload(struct lan966x_rx *rx)
 {
 	struct lan966x *lan966x = rx->lan966x;
 
-	lan_rmw(FDMA_CH_RELOAD_CH_RELOAD_SET(BIT(rx->channel_id)),
+	lan_rmw(FDMA_CH_RELOAD_CH_RELOAD_SET(BIT(rx->fdma.channel_id)),
 		FDMA_CH_RELOAD_CH_RELOAD,
 		lan966x, FDMA_CH_RELOAD);
 }
@@ -240,28 +249,29 @@ static void lan966x_fdma_tx_add_dcb(struct lan966x_tx *tx,
 static int lan966x_fdma_tx_alloc(struct lan966x_tx *tx)
 {
 	struct lan966x *lan966x = tx->lan966x;
+	struct fdma *fdma = &tx->fdma;
 	struct lan966x_tx_dcb *dcb;
 	struct lan966x_db *db;
 	int size;
 	int i, j;
 
-	tx->dcbs_buf = kcalloc(FDMA_DCB_MAX, sizeof(struct lan966x_tx_dcb_buf),
+	tx->dcbs_buf = kcalloc(fdma->n_dcbs, sizeof(struct lan966x_tx_dcb_buf),
 			       GFP_KERNEL);
 	if (!tx->dcbs_buf)
 		return -ENOMEM;
 
 	/* calculate how many pages are needed to allocate the dcbs */
-	size = sizeof(struct lan966x_tx_dcb) * FDMA_DCB_MAX;
+	size = sizeof(struct lan966x_tx_dcb) * fdma->n_dcbs;
 	size = ALIGN(size, PAGE_SIZE);
 	tx->dcbs = dma_alloc_coherent(lan966x->dev, size, &tx->dma, GFP_KERNEL);
 	if (!tx->dcbs)
 		goto out;
 
 	/* Now for each dcb allocate the db */
-	for (i = 0; i < FDMA_DCB_MAX; ++i) {
+	for (i = 0; i < fdma->n_dcbs; ++i) {
 		dcb = &tx->dcbs[i];
 
-		for (j = 0; j < FDMA_TX_DCB_MAX_DBS; ++j) {
+		for (j = 0; j < fdma->n_dbs; ++j) {
 			db = &dcb->db[j];
 			db->dataptr = 0;
 			db->status = 0;
@@ -280,11 +290,12 @@ static int lan966x_fdma_tx_alloc(struct lan966x_tx *tx)
 static void lan966x_fdma_tx_free(struct lan966x_tx *tx)
 {
 	struct lan966x *lan966x = tx->lan966x;
+	struct fdma *fdma = &tx->fdma;
 	int size;
 
 	kfree(tx->dcbs_buf);
 
-	size = sizeof(struct lan966x_tx_dcb) * FDMA_DCB_MAX;
+	size = sizeof(struct lan966x_tx_dcb) * fdma->n_dcbs;
 	size = ALIGN(size, PAGE_SIZE);
 	dma_free_coherent(lan966x->dev, size, tx->dcbs, tx->dma);
 }
@@ -292,21 +303,22 @@ static void lan966x_fdma_tx_free(struct lan966x_tx *tx)
 static void lan966x_fdma_tx_activate(struct lan966x_tx *tx)
 {
 	struct lan966x *lan966x = tx->lan966x;
+	struct fdma *fdma = &tx->fdma;
 	u32 mask;
 
 	/* When activating a channel, first is required to write the first DCB
 	 * address and then to activate it
 	 */
 	lan_wr(lower_32_bits((u64)tx->dma), lan966x,
-	       FDMA_DCB_LLP(tx->channel_id));
+	       FDMA_DCB_LLP(fdma->channel_id));
 	lan_wr(upper_32_bits((u64)tx->dma), lan966x,
-	       FDMA_DCB_LLP1(tx->channel_id));
+	       FDMA_DCB_LLP1(fdma->channel_id));
 
-	lan_wr(FDMA_CH_CFG_CH_DCB_DB_CNT_SET(FDMA_TX_DCB_MAX_DBS) |
+	lan_wr(FDMA_CH_CFG_CH_DCB_DB_CNT_SET(fdma->n_dbs) |
 	       FDMA_CH_CFG_CH_INTR_DB_EOF_ONLY_SET(1) |
 	       FDMA_CH_CFG_CH_INJ_PORT_SET(0) |
 	       FDMA_CH_CFG_CH_MEM_SET(1),
-	       lan966x, FDMA_CH_CFG(tx->channel_id));
+	       lan966x, FDMA_CH_CFG(fdma->channel_id));
 
 	/* Start fdma */
 	lan_rmw(FDMA_PORT_CTRL_INJ_STOP_SET(0),
@@ -316,13 +328,13 @@ static void lan966x_fdma_tx_activate(struct lan966x_tx *tx)
 	/* Enable interrupts */
 	mask = lan_rd(lan966x, FDMA_INTR_DB_ENA);
 	mask = FDMA_INTR_DB_ENA_INTR_DB_ENA_GET(mask);
-	mask |= BIT(tx->channel_id);
+	mask |= BIT(fdma->channel_id);
 	lan_rmw(FDMA_INTR_DB_ENA_INTR_DB_ENA_SET(mask),
 		FDMA_INTR_DB_ENA_INTR_DB_ENA,
 		lan966x, FDMA_INTR_DB_ENA);
 
 	/* Activate the channel */
-	lan_rmw(FDMA_CH_ACTIVATE_CH_ACTIVATE_SET(BIT(tx->channel_id)),
+	lan_rmw(FDMA_CH_ACTIVATE_CH_ACTIVATE_SET(BIT(fdma->channel_id)),
 		FDMA_CH_ACTIVATE_CH_ACTIVATE,
 		lan966x, FDMA_CH_ACTIVATE);
 }
@@ -330,18 +342,19 @@ static void lan966x_fdma_tx_activate(struct lan966x_tx *tx)
 static void lan966x_fdma_tx_disable(struct lan966x_tx *tx)
 {
 	struct lan966x *lan966x = tx->lan966x;
+	struct fdma *fdma = &tx->fdma;
 	u32 val;
 
 	/* Disable the channel */
-	lan_rmw(FDMA_CH_DISABLE_CH_DISABLE_SET(BIT(tx->channel_id)),
+	lan_rmw(FDMA_CH_DISABLE_CH_DISABLE_SET(BIT(fdma->channel_id)),
 		FDMA_CH_DISABLE_CH_DISABLE,
 		lan966x, FDMA_CH_DISABLE);
 
 	readx_poll_timeout_atomic(lan966x_fdma_channel_active, lan966x,
-				  val, !(val & BIT(tx->channel_id)),
+				  val, !(val & BIT(fdma->channel_id)),
 				  READL_SLEEP_US, READL_TIMEOUT_US);
 
-	lan_rmw(FDMA_CH_DB_DISCARD_DB_DISCARD_SET(BIT(tx->channel_id)),
+	lan_rmw(FDMA_CH_DB_DISCARD_DB_DISCARD_SET(BIT(fdma->channel_id)),
 		FDMA_CH_DB_DISCARD_DB_DISCARD,
 		lan966x, FDMA_CH_DB_DISCARD);
 
@@ -354,7 +367,7 @@ static void lan966x_fdma_tx_reload(struct lan966x_tx *tx)
 	struct lan966x *lan966x = tx->lan966x;
 
 	/* Write the registers to reload the channel */
-	lan_rmw(FDMA_CH_RELOAD_CH_RELOAD_SET(BIT(tx->channel_id)),
+	lan_rmw(FDMA_CH_RELOAD_CH_RELOAD_SET(BIT(tx->fdma.channel_id)),
 		FDMA_CH_RELOAD_CH_RELOAD,
 		lan966x, FDMA_CH_RELOAD);
 }
@@ -402,7 +415,7 @@ static void lan966x_fdma_tx_clear_buf(struct lan966x *lan966x, int weight)
 	xdp_frame_bulk_init(&bq);
 
 	spin_lock_irqsave(&lan966x->tx_lock, flags);
-	for (i = 0; i < FDMA_DCB_MAX; ++i) {
+	for (i = 0; i < tx->fdma.n_dcbs; ++i) {
 		dcb_buf = &tx->dcbs_buf[i];
 
 		if (!dcb_buf->used)
@@ -451,10 +464,11 @@ static void lan966x_fdma_tx_clear_buf(struct lan966x *lan966x, int weight)
 
 static bool lan966x_fdma_rx_more_frames(struct lan966x_rx *rx)
 {
+	struct fdma *fdma = &rx->fdma;
 	struct lan966x_db *db;
 
 	/* Check if there is any data */
-	db = &rx->dcbs[rx->dcb_index].db[rx->db_index];
+	db = &rx->dcbs[fdma->dcb_index].db[fdma->db_index];
 	if (unlikely(!(db->status & FDMA_DCB_STATUS_DONE)))
 		return false;
 
@@ -464,12 +478,13 @@ static bool lan966x_fdma_rx_more_frames(struct lan966x_rx *rx)
 static int lan966x_fdma_rx_check_frame(struct lan966x_rx *rx, u64 *src_port)
 {
 	struct lan966x *lan966x = rx->lan966x;
+	struct fdma *fdma = &rx->fdma;
 	struct lan966x_port *port;
 	struct lan966x_db *db;
 	struct page *page;
 
-	db = &rx->dcbs[rx->dcb_index].db[rx->db_index];
-	page = rx->page[rx->dcb_index][rx->db_index];
+	db = &rx->dcbs[fdma->dcb_index].db[fdma->db_index];
+	page = rx->page[fdma->dcb_index][fdma->db_index];
 	if (unlikely(!page))
 		return FDMA_ERROR;
 
@@ -494,14 +509,15 @@ static struct sk_buff *lan966x_fdma_rx_get_frame(struct lan966x_rx *rx,
 						 u64 src_port)
 {
 	struct lan966x *lan966x = rx->lan966x;
+	struct fdma *fdma = &rx->fdma;
 	struct lan966x_db *db;
 	struct sk_buff *skb;
 	struct page *page;
 	u64 timestamp;
 
 	/* Get the received frame and unmap it */
-	db = &rx->dcbs[rx->dcb_index].db[rx->db_index];
-	page = rx->page[rx->dcb_index][rx->db_index];
+	db = &rx->dcbs[fdma->dcb_index].db[fdma->db_index];
+	page = rx->page[fdma->dcb_index][fdma->db_index];
 
 	skb = build_skb(page_address(page), PAGE_SIZE << rx->page_order);
 	if (unlikely(!skb))
@@ -546,16 +562,18 @@ static int lan966x_fdma_napi_poll(struct napi_struct *napi, int weight)
 {
 	struct lan966x *lan966x = container_of(napi, struct lan966x, napi);
 	struct lan966x_rx *rx = &lan966x->rx;
-	int dcb_reload = rx->dcb_index;
 	struct lan966x_rx_dcb *old_dcb;
+	struct fdma *fdma = &rx->fdma;
+	int dcb_reload, counter = 0;
 	struct lan966x_db *db;
 	bool redirect = false;
 	struct sk_buff *skb;
 	struct page *page;
-	int counter = 0;
 	u64 src_port;
 	u64 nextptr;
 
+	dcb_reload = fdma->dcb_index;
+
 	lan966x_fdma_tx_clear_buf(lan966x, weight);
 
 	/* Get all received skb */
@@ -594,16 +612,16 @@ static int lan966x_fdma_napi_poll(struct napi_struct *napi, int weight)
 
 allocate_new:
 	/* Allocate new pages and map them */
-	while (dcb_reload != rx->dcb_index) {
-		db = &rx->dcbs[dcb_reload].db[rx->db_index];
+	while (dcb_reload != fdma->dcb_index) {
+		db = &rx->dcbs[dcb_reload].db[fdma->db_index];
 		page = lan966x_fdma_rx_alloc_page(rx, db);
 		if (unlikely(!page))
 			break;
-		rx->page[dcb_reload][rx->db_index] = page;
+		rx->page[dcb_reload][fdma->db_index] = page;
 
 		old_dcb = &rx->dcbs[dcb_reload];
 		dcb_reload++;
-		dcb_reload &= FDMA_DCB_MAX - 1;
+		dcb_reload &= fdma->n_dcbs - 1;
 
 		nextptr = rx->dma + ((unsigned long)old_dcb -
 				     (unsigned long)rx->dcbs);
@@ -650,9 +668,10 @@ irqreturn_t lan966x_fdma_irq_handler(int irq, void *args)
 static int lan966x_fdma_get_next_dcb(struct lan966x_tx *tx)
 {
 	struct lan966x_tx_dcb_buf *dcb_buf;
+	struct fdma *fdma = &tx->fdma;
 	int i;
 
-	for (i = 0; i < FDMA_DCB_MAX; ++i) {
+	for (i = 0; i < fdma->n_dcbs; ++i) {
 		dcb_buf = &tx->dcbs_buf[i];
 		if (!dcb_buf->used && i != tx->last_in_use)
 			return i;
@@ -931,7 +950,7 @@ static int lan966x_fdma_reload(struct lan966x *lan966x, int new_mtu)
 		goto restore;
 	lan966x_fdma_rx_start(&lan966x->rx);
 
-	size = sizeof(struct lan966x_rx_dcb) * FDMA_DCB_MAX;
+	size = sizeof(struct lan966x_rx_dcb) * lan966x->rx.fdma.n_dcbs;
 	size = ALIGN(size, PAGE_SIZE);
 	dma_free_coherent(lan966x->dev, size, rx_dcbs, rx_dma);
 
@@ -1034,10 +1053,14 @@ int lan966x_fdma_init(struct lan966x *lan966x)
 		return 0;
 
 	lan966x->rx.lan966x = lan966x;
-	lan966x->rx.channel_id = FDMA_XTR_CHANNEL;
+	lan966x->rx.fdma.channel_id = FDMA_XTR_CHANNEL;
+	lan966x->rx.fdma.n_dcbs = FDMA_DCB_MAX;
+	lan966x->rx.fdma.n_dbs = FDMA_RX_DCB_MAX_DBS;
 	lan966x->rx.max_mtu = lan966x_fdma_get_max_frame(lan966x);
 	lan966x->tx.lan966x = lan966x;
-	lan966x->tx.channel_id = FDMA_INJ_CHANNEL;
+	lan966x->tx.fdma.channel_id = FDMA_INJ_CHANNEL;
+	lan966x->tx.fdma.n_dcbs = FDMA_DCB_MAX;
+	lan966x->tx.fdma.n_dbs = FDMA_TX_DCB_MAX_DBS;
 	lan966x->tx.last_in_use = -1;
 
 	err = lan966x_fdma_rx_alloc(&lan966x->rx);
diff --git a/drivers/net/ethernet/microchip/lan966x/lan966x_main.h b/drivers/net/ethernet/microchip/lan966x/lan966x_main.h
index 4d2aa775fbfd..fb9d8e00fe69 100644
--- a/drivers/net/ethernet/microchip/lan966x/lan966x_main.h
+++ b/drivers/net/ethernet/microchip/lan966x/lan966x_main.h
@@ -211,6 +211,8 @@ struct lan966x_tx_dcb {
 struct lan966x_rx {
 	struct lan966x *lan966x;
 
+	struct fdma fdma;
+
 	/* Pointer to the array of hardware dcbs. */
 	struct lan966x_rx_dcb *dcbs;
 
@@ -220,17 +222,6 @@ struct lan966x_rx {
 	/* For each DB, there is a page */
 	struct page *page[FDMA_DCB_MAX][FDMA_RX_DCB_MAX_DBS];
 
-	/* Represents the db_index, it can have a value between 0 and
-	 * FDMA_RX_DCB_MAX_DBS, once it reaches the value of FDMA_RX_DCB_MAX_DBS
-	 * it means that the DCB can be reused.
-	 */
-	int db_index;
-
-	/* Represents the index in the dcbs. It has a value between 0 and
-	 * FDMA_DCB_MAX
-	 */
-	int dcb_index;
-
 	/* Represents the dma address to the dcbs array */
 	dma_addr_t dma;
 
@@ -244,8 +235,6 @@ struct lan966x_rx {
 	 */
 	u32 max_mtu;
 
-	u8 channel_id;
-
 	struct page_pool *page_pool;
 };
 
@@ -267,6 +256,8 @@ struct lan966x_tx_dcb_buf {
 struct lan966x_tx {
 	struct lan966x *lan966x;
 
+	struct fdma fdma;
+
 	/* Pointer to the dcb list */
 	struct lan966x_tx_dcb *dcbs;
 	u16 last_in_use;
@@ -277,8 +268,6 @@ struct lan966x_tx {
 	/* Array of dcbs that are given to the HW */
 	struct lan966x_tx_dcb_buf *dcbs_buf;
 
-	u8 channel_id;
-
 	bool activated;
 };
 

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH net-next 04/12] net: lan966x: use the FDMA library for allocation of rx buffers
  2024-09-05  8:06 [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library Daniel Machon
                   ` (2 preceding siblings ...)
  2024-09-05  8:06 ` [PATCH net-next 03/12] net: lan966x: replace a few variables with new equivalent ones Daniel Machon
@ 2024-09-05  8:06 ` Daniel Machon
  2024-09-10  9:15   ` Paolo Abeni
  2024-09-05  8:06 ` [PATCH net-next 05/12] net: lan966x: use FDMA library for adding DCB's in the rx path Daniel Machon
                   ` (8 subsequent siblings)
  12 siblings, 1 reply; 16+ messages in thread
From: Daniel Machon @ 2024-09-05  8:06 UTC (permalink / raw)
  To: Horatiu Vultur, UNGLinuxDriver, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend
  Cc: netdev, linux-kernel, bpf

Use the two functions: fdma_alloc_phys() and fdma_dcb_init() for rx
buffer allocation and use the new buffers throughout.

In order to replace the old buffers with the new ones, we have to do the
following refactoring:

    - use fdma_alloc_phys() and fdma_dcb_init()

    - replace the variables: rx->dma, rx->dcbs and rx->last_entry
      with the equivalents from the FDMA struct.

    - make use of fdma->db_size for rx buffer size.

    - add lan966x_fdma_rx_dataptr_cb callback for obtaining the dataptr.

    - Initialize FDMA struct values.

Signed-off-by: Daniel Machon <daniel.machon@microchip.com>
Reviewed-by: Horatiu Vultur <horatiu.vultur@microchip.com>
---
 .../net/ethernet/microchip/lan966x/lan966x_fdma.c  | 116 ++++++++++-----------
 .../net/ethernet/microchip/lan966x/lan966x_main.h  |  15 ---
 2 files changed, 55 insertions(+), 76 deletions(-)

diff --git a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
index b64f04ff99a8..99d09c97737e 100644
--- a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
+++ b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
@@ -6,13 +6,30 @@
 
 #include "lan966x_main.h"
 
+static int lan966x_fdma_rx_dataptr_cb(struct fdma *fdma, int dcb, int db,
+				      u64 *dataptr)
+{
+	struct lan966x *lan966x = (struct lan966x *)fdma->priv;
+	struct lan966x_rx *rx = &lan966x->rx;
+	struct page *page;
+
+	page = page_pool_dev_alloc_pages(rx->page_pool);
+	if (unlikely(!page))
+		return -ENOMEM;
+
+	rx->page[dcb][db] = page;
+	*dataptr = page_pool_get_dma_addr(page) + XDP_PACKET_HEADROOM;
+
+	return 0;
+}
+
 static int lan966x_fdma_channel_active(struct lan966x *lan966x)
 {
 	return lan_rd(lan966x, FDMA_CH_ACTIVE);
 }
 
 static struct page *lan966x_fdma_rx_alloc_page(struct lan966x_rx *rx,
-					       struct lan966x_db *db)
+					       struct fdma_db *db)
 {
 	struct page *page;
 
@@ -50,11 +67,11 @@ static void lan966x_fdma_rx_free_page(struct lan966x_rx *rx)
 }
 
 static void lan966x_fdma_rx_add_dcb(struct lan966x_rx *rx,
-				    struct lan966x_rx_dcb *dcb,
+				    struct fdma_dcb *dcb,
 				    u64 nextptr)
 {
 	struct fdma *fdma = &rx->fdma;
-	struct lan966x_db *db;
+	struct fdma_db *db;
 	int i;
 
 	for (i = 0; i < fdma->n_dbs; ++i) {
@@ -65,8 +82,8 @@ static void lan966x_fdma_rx_add_dcb(struct lan966x_rx *rx,
 	dcb->nextptr = FDMA_DCB_INVALID_DATA;
 	dcb->info = FDMA_DCB_INFO_DATAL(PAGE_SIZE << rx->page_order);
 
-	rx->last_entry->nextptr = nextptr;
-	rx->last_entry = dcb;
+	fdma->last_dcb->nextptr = nextptr;
+	fdma->last_dcb = dcb;
 }
 
 static int lan966x_fdma_rx_alloc_page_pool(struct lan966x_rx *rx)
@@ -108,45 +125,17 @@ static int lan966x_fdma_rx_alloc(struct lan966x_rx *rx)
 {
 	struct lan966x *lan966x = rx->lan966x;
 	struct fdma *fdma = &rx->fdma;
-	struct lan966x_rx_dcb *dcb;
-	struct lan966x_db *db;
-	struct page *page;
-	int i, j;
-	int size;
+	int err;
 
 	if (lan966x_fdma_rx_alloc_page_pool(rx))
 		return PTR_ERR(rx->page_pool);
 
-	/* calculate how many pages are needed to allocate the dcbs */
-	size = sizeof(struct lan966x_rx_dcb) * fdma->n_dcbs;
-	size = ALIGN(size, PAGE_SIZE);
-
-	rx->dcbs = dma_alloc_coherent(lan966x->dev, size, &rx->dma, GFP_KERNEL);
-	if (!rx->dcbs)
-		return -ENOMEM;
-
-	rx->last_entry = rx->dcbs;
-	fdma->db_index = 0;
-	fdma->dcb_index = 0;
-
-	/* Now for each dcb allocate the dbs */
-	for (i = 0; i < fdma->n_dcbs; ++i) {
-		dcb = &rx->dcbs[i];
-		dcb->info = 0;
-
-		/* For each db allocate a page and map it to the DB dataptr. */
-		for (j = 0; j < fdma->n_dbs; ++j) {
-			db = &dcb->db[j];
-			page = lan966x_fdma_rx_alloc_page(rx, db);
-			if (!page)
-				return -ENOMEM;
-
-			db->status = 0;
-			rx->page[i][j] = page;
-		}
+	err = fdma_alloc_coherent(lan966x->dev, fdma);
+	if (err)
+		return err;
 
-		lan966x_fdma_rx_add_dcb(rx, dcb, rx->dma + sizeof(*dcb) * i);
-	}
+	fdma_dcbs_init(fdma, FDMA_DCB_INFO_DATAL(fdma->db_size),
+		       FDMA_DCB_STATUS_INTR);
 
 	return 0;
 }
@@ -168,7 +157,7 @@ static void lan966x_fdma_rx_free(struct lan966x_rx *rx)
 	/* Now it is possible to do the cleanup of dcb */
 	size = sizeof(struct lan966x_tx_dcb) * fdma->n_dcbs;
 	size = ALIGN(size, PAGE_SIZE);
-	dma_free_coherent(lan966x->dev, size, rx->dcbs, rx->dma);
+	dma_free_coherent(lan966x->dev, size, fdma->dcbs, fdma->dma);
 }
 
 static void lan966x_fdma_rx_start(struct lan966x_rx *rx)
@@ -180,9 +169,9 @@ static void lan966x_fdma_rx_start(struct lan966x_rx *rx)
 	/* When activating a channel, first is required to write the first DCB
 	 * address and then to activate it
 	 */
-	lan_wr(lower_32_bits((u64)rx->dma), lan966x,
+	lan_wr(lower_32_bits((u64)fdma->dma), lan966x,
 	       FDMA_DCB_LLP(fdma->channel_id));
-	lan_wr(upper_32_bits((u64)rx->dma), lan966x,
+	lan_wr(upper_32_bits((u64)fdma->dma), lan966x,
 	       FDMA_DCB_LLP1(fdma->channel_id));
 
 	lan_wr(FDMA_CH_CFG_CH_DCB_DB_CNT_SET(fdma->n_dbs) |
@@ -297,7 +286,7 @@ static void lan966x_fdma_tx_free(struct lan966x_tx *tx)
 
 	size = sizeof(struct lan966x_tx_dcb) * fdma->n_dcbs;
 	size = ALIGN(size, PAGE_SIZE);
-	dma_free_coherent(lan966x->dev, size, tx->dcbs, tx->dma);
+	dma_free_coherent(lan966x->dev, size, fdma->dcbs, fdma->dma);
 }
 
 static void lan966x_fdma_tx_activate(struct lan966x_tx *tx)
@@ -465,10 +454,10 @@ static void lan966x_fdma_tx_clear_buf(struct lan966x *lan966x, int weight)
 static bool lan966x_fdma_rx_more_frames(struct lan966x_rx *rx)
 {
 	struct fdma *fdma = &rx->fdma;
-	struct lan966x_db *db;
+	struct fdma_db *db;
 
 	/* Check if there is any data */
-	db = &rx->dcbs[fdma->dcb_index].db[fdma->db_index];
+	db = &fdma->dcbs[fdma->dcb_index].db[fdma->db_index];
 	if (unlikely(!(db->status & FDMA_DCB_STATUS_DONE)))
 		return false;
 
@@ -480,10 +469,10 @@ static int lan966x_fdma_rx_check_frame(struct lan966x_rx *rx, u64 *src_port)
 	struct lan966x *lan966x = rx->lan966x;
 	struct fdma *fdma = &rx->fdma;
 	struct lan966x_port *port;
-	struct lan966x_db *db;
+	struct fdma_db *db;
 	struct page *page;
 
-	db = &rx->dcbs[fdma->dcb_index].db[fdma->db_index];
+	db = &fdma->dcbs[fdma->dcb_index].db[fdma->db_index];
 	page = rx->page[fdma->dcb_index][fdma->db_index];
 	if (unlikely(!page))
 		return FDMA_ERROR;
@@ -510,16 +499,16 @@ static struct sk_buff *lan966x_fdma_rx_get_frame(struct lan966x_rx *rx,
 {
 	struct lan966x *lan966x = rx->lan966x;
 	struct fdma *fdma = &rx->fdma;
-	struct lan966x_db *db;
 	struct sk_buff *skb;
+	struct fdma_db *db;
 	struct page *page;
 	u64 timestamp;
 
 	/* Get the received frame and unmap it */
-	db = &rx->dcbs[fdma->dcb_index].db[fdma->db_index];
+	db = &fdma->dcbs[fdma->dcb_index].db[fdma->db_index];
 	page = rx->page[fdma->dcb_index][fdma->db_index];
 
-	skb = build_skb(page_address(page), PAGE_SIZE << rx->page_order);
+	skb = build_skb(page_address(page), fdma->db_size);
 	if (unlikely(!skb))
 		goto free_page;
 
@@ -562,12 +551,12 @@ static int lan966x_fdma_napi_poll(struct napi_struct *napi, int weight)
 {
 	struct lan966x *lan966x = container_of(napi, struct lan966x, napi);
 	struct lan966x_rx *rx = &lan966x->rx;
-	struct lan966x_rx_dcb *old_dcb;
 	struct fdma *fdma = &rx->fdma;
 	int dcb_reload, counter = 0;
-	struct lan966x_db *db;
+	struct fdma_dcb *old_dcb;
 	bool redirect = false;
 	struct sk_buff *skb;
+	struct fdma_db *db;
 	struct page *page;
 	u64 src_port;
 	u64 nextptr;
@@ -613,18 +602,18 @@ static int lan966x_fdma_napi_poll(struct napi_struct *napi, int weight)
 allocate_new:
 	/* Allocate new pages and map them */
 	while (dcb_reload != fdma->dcb_index) {
-		db = &rx->dcbs[dcb_reload].db[fdma->db_index];
+		db = &fdma->dcbs[dcb_reload].db[fdma->db_index];
 		page = lan966x_fdma_rx_alloc_page(rx, db);
 		if (unlikely(!page))
 			break;
 		rx->page[dcb_reload][fdma->db_index] = page;
 
-		old_dcb = &rx->dcbs[dcb_reload];
+		old_dcb = &fdma->dcbs[dcb_reload];
 		dcb_reload++;
 		dcb_reload &= fdma->n_dcbs - 1;
 
-		nextptr = rx->dma + ((unsigned long)old_dcb -
-				     (unsigned long)rx->dcbs);
+		nextptr = fdma->dma + ((unsigned long)old_dcb -
+				     (unsigned long)fdma->dcbs);
 		lan966x_fdma_rx_add_dcb(rx, old_dcb, nextptr);
 		lan966x_fdma_rx_reload(rx);
 	}
@@ -933,8 +922,8 @@ static int lan966x_fdma_reload(struct lan966x *lan966x, int new_mtu)
 	int err;
 
 	/* Store these for later to free them */
-	rx_dma = lan966x->rx.dma;
-	rx_dcbs = lan966x->rx.dcbs;
+	rx_dma = lan966x->rx.fdma.dma;
+	rx_dcbs = lan966x->rx.fdma.dcbs;
 	page_pool = lan966x->rx.page_pool;
 
 	napi_synchronize(&lan966x->napi);
@@ -950,7 +939,7 @@ static int lan966x_fdma_reload(struct lan966x *lan966x, int new_mtu)
 		goto restore;
 	lan966x_fdma_rx_start(&lan966x->rx);
 
-	size = sizeof(struct lan966x_rx_dcb) * lan966x->rx.fdma.n_dcbs;
+	size = sizeof(struct fdma_dcb) * lan966x->rx.fdma.n_dcbs;
 	size = ALIGN(size, PAGE_SIZE);
 	dma_free_coherent(lan966x->dev, size, rx_dcbs, rx_dma);
 
@@ -962,8 +951,8 @@ static int lan966x_fdma_reload(struct lan966x *lan966x, int new_mtu)
 	return err;
 restore:
 	lan966x->rx.page_pool = page_pool;
-	lan966x->rx.dma = rx_dma;
-	lan966x->rx.dcbs = rx_dcbs;
+	lan966x->rx.fdma.dma = rx_dma;
+	lan966x->rx.fdma.dcbs = rx_dcbs;
 	lan966x_fdma_rx_start(&lan966x->rx);
 
 	return err;
@@ -1056,6 +1045,11 @@ int lan966x_fdma_init(struct lan966x *lan966x)
 	lan966x->rx.fdma.channel_id = FDMA_XTR_CHANNEL;
 	lan966x->rx.fdma.n_dcbs = FDMA_DCB_MAX;
 	lan966x->rx.fdma.n_dbs = FDMA_RX_DCB_MAX_DBS;
+	lan966x->rx.fdma.priv = lan966x;
+	lan966x->rx.fdma.size = fdma_get_size(&lan966x->rx.fdma);
+	lan966x->rx.fdma.db_size = PAGE_SIZE << lan966x->rx.page_order;
+	lan966x->rx.fdma.ops.nextptr_cb = &fdma_nextptr_cb;
+	lan966x->rx.fdma.ops.dataptr_cb = &lan966x_fdma_rx_dataptr_cb;
 	lan966x->rx.max_mtu = lan966x_fdma_get_max_frame(lan966x);
 	lan966x->tx.lan966x = lan966x;
 	lan966x->tx.fdma.channel_id = FDMA_INJ_CHANNEL;
diff --git a/drivers/net/ethernet/microchip/lan966x/lan966x_main.h b/drivers/net/ethernet/microchip/lan966x/lan966x_main.h
index fb9d8e00fe69..8edb5ea484ee 100644
--- a/drivers/net/ethernet/microchip/lan966x/lan966x_main.h
+++ b/drivers/net/ethernet/microchip/lan966x/lan966x_main.h
@@ -196,12 +196,6 @@ struct lan966x_db {
 	u64 status;
 };
 
-struct lan966x_rx_dcb {
-	u64 nextptr;
-	u64 info;
-	struct lan966x_db db[FDMA_RX_DCB_MAX_DBS];
-};
-
 struct lan966x_tx_dcb {
 	u64 nextptr;
 	u64 info;
@@ -213,18 +207,9 @@ struct lan966x_rx {
 
 	struct fdma fdma;
 
-	/* Pointer to the array of hardware dcbs. */
-	struct lan966x_rx_dcb *dcbs;
-
-	/* Pointer to the last address in the dcbs. */
-	struct lan966x_rx_dcb *last_entry;
-
 	/* For each DB, there is a page */
 	struct page *page[FDMA_DCB_MAX][FDMA_RX_DCB_MAX_DBS];
 
-	/* Represents the dma address to the dcbs array */
-	dma_addr_t dma;
-
 	/* Represents the page order that is used to allocate the pages for the
 	 * RX buffers. This value is calculated based on max MTU of the devices.
 	 */

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH net-next 05/12] net: lan966x: use FDMA library for adding DCB's in the rx path
  2024-09-05  8:06 [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library Daniel Machon
                   ` (3 preceding siblings ...)
  2024-09-05  8:06 ` [PATCH net-next 04/12] net: lan966x: use the FDMA library for allocation of rx buffers Daniel Machon
@ 2024-09-05  8:06 ` Daniel Machon
  2024-09-05  8:06 ` [PATCH net-next 06/12] net: lan966x: use library helper for freeing rx buffers Daniel Machon
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 16+ messages in thread
From: Daniel Machon @ 2024-09-05  8:06 UTC (permalink / raw)
  To: Horatiu Vultur, UNGLinuxDriver, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend
  Cc: netdev, linux-kernel, bpf

Use the fdma_dcb_add() function to add DCB's in the rx path. This gets
rid of the open-coding of nextptr and dataptr handling and the functions
for adding DCB's.

Signed-off-by: Daniel Machon <daniel.machon@microchip.com>
Reviewed-by: Horatiu Vultur <horatiu.vultur@microchip.com>
---
 .../net/ethernet/microchip/lan966x/lan966x_fdma.c  | 54 ++--------------------
 1 file changed, 5 insertions(+), 49 deletions(-)

diff --git a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
index 99d09c97737e..b85b15ca2052 100644
--- a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
+++ b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
@@ -28,20 +28,6 @@ static int lan966x_fdma_channel_active(struct lan966x *lan966x)
 	return lan_rd(lan966x, FDMA_CH_ACTIVE);
 }
 
-static struct page *lan966x_fdma_rx_alloc_page(struct lan966x_rx *rx,
-					       struct fdma_db *db)
-{
-	struct page *page;
-
-	page = page_pool_dev_alloc_pages(rx->page_pool);
-	if (unlikely(!page))
-		return NULL;
-
-	db->dataptr = page_pool_get_dma_addr(page) + XDP_PACKET_HEADROOM;
-
-	return page;
-}
-
 static void lan966x_fdma_rx_free_pages(struct lan966x_rx *rx)
 {
 	struct fdma *fdma = &rx->fdma;
@@ -66,26 +52,6 @@ static void lan966x_fdma_rx_free_page(struct lan966x_rx *rx)
 	page_pool_recycle_direct(rx->page_pool, page);
 }
 
-static void lan966x_fdma_rx_add_dcb(struct lan966x_rx *rx,
-				    struct fdma_dcb *dcb,
-				    u64 nextptr)
-{
-	struct fdma *fdma = &rx->fdma;
-	struct fdma_db *db;
-	int i;
-
-	for (i = 0; i < fdma->n_dbs; ++i) {
-		db = &dcb->db[i];
-		db->status = FDMA_DCB_STATUS_INTR;
-	}
-
-	dcb->nextptr = FDMA_DCB_INVALID_DATA;
-	dcb->info = FDMA_DCB_INFO_DATAL(PAGE_SIZE << rx->page_order);
-
-	fdma->last_dcb->nextptr = nextptr;
-	fdma->last_dcb = dcb;
-}
-
 static int lan966x_fdma_rx_alloc_page_pool(struct lan966x_rx *rx)
 {
 	struct lan966x *lan966x = rx->lan966x;
@@ -551,15 +517,11 @@ static int lan966x_fdma_napi_poll(struct napi_struct *napi, int weight)
 {
 	struct lan966x *lan966x = container_of(napi, struct lan966x, napi);
 	struct lan966x_rx *rx = &lan966x->rx;
+	int old_dcb, dcb_reload, counter = 0;
 	struct fdma *fdma = &rx->fdma;
-	int dcb_reload, counter = 0;
-	struct fdma_dcb *old_dcb;
 	bool redirect = false;
 	struct sk_buff *skb;
-	struct fdma_db *db;
-	struct page *page;
 	u64 src_port;
-	u64 nextptr;
 
 	dcb_reload = fdma->dcb_index;
 
@@ -602,19 +564,13 @@ static int lan966x_fdma_napi_poll(struct napi_struct *napi, int weight)
 allocate_new:
 	/* Allocate new pages and map them */
 	while (dcb_reload != fdma->dcb_index) {
-		db = &fdma->dcbs[dcb_reload].db[fdma->db_index];
-		page = lan966x_fdma_rx_alloc_page(rx, db);
-		if (unlikely(!page))
-			break;
-		rx->page[dcb_reload][fdma->db_index] = page;
-
-		old_dcb = &fdma->dcbs[dcb_reload];
+		old_dcb = dcb_reload;
 		dcb_reload++;
 		dcb_reload &= fdma->n_dcbs - 1;
 
-		nextptr = fdma->dma + ((unsigned long)old_dcb -
-				     (unsigned long)fdma->dcbs);
-		lan966x_fdma_rx_add_dcb(rx, old_dcb, nextptr);
+		fdma_dcb_add(fdma, old_dcb, FDMA_DCB_INFO_DATAL(fdma->db_size),
+			     FDMA_DCB_STATUS_INTR);
+
 		lan966x_fdma_rx_reload(rx);
 	}
 

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH net-next 06/12] net: lan966x: use library helper for freeing rx buffers
  2024-09-05  8:06 [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library Daniel Machon
                   ` (4 preceding siblings ...)
  2024-09-05  8:06 ` [PATCH net-next 05/12] net: lan966x: use FDMA library for adding DCB's in the rx path Daniel Machon
@ 2024-09-05  8:06 ` Daniel Machon
  2024-09-05  8:06 ` [PATCH net-next 07/12] net: lan966x: use the FDMA library for allocation of tx buffers Daniel Machon
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 16+ messages in thread
From: Daniel Machon @ 2024-09-05  8:06 UTC (permalink / raw)
  To: Horatiu Vultur, UNGLinuxDriver, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend
  Cc: netdev, linux-kernel, bpf

The library has the helper fdma_free_phys() for freeing physical FDMA
memory. Use it in the exit path.

Signed-off-by: Daniel Machon <daniel.machon@microchip.com>
Reviewed-by: Horatiu Vultur <horatiu.vultur@microchip.com>
---
 drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c | 16 ++--------------
 1 file changed, 2 insertions(+), 14 deletions(-)

diff --git a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
index b85b15ca2052..627806a10674 100644
--- a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
+++ b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
@@ -114,18 +114,6 @@ static void lan966x_fdma_rx_advance_dcb(struct lan966x_rx *rx)
 	fdma->dcb_index &= fdma->n_dcbs - 1;
 }
 
-static void lan966x_fdma_rx_free(struct lan966x_rx *rx)
-{
-	struct lan966x *lan966x = rx->lan966x;
-	struct fdma *fdma = &rx->fdma;
-	u32 size;
-
-	/* Now it is possible to do the cleanup of dcb */
-	size = sizeof(struct lan966x_tx_dcb) * fdma->n_dcbs;
-	size = ALIGN(size, PAGE_SIZE);
-	dma_free_coherent(lan966x->dev, size, fdma->dcbs, fdma->dma);
-}
-
 static void lan966x_fdma_rx_start(struct lan966x_rx *rx)
 {
 	struct lan966x *lan966x = rx->lan966x;
@@ -1019,7 +1007,7 @@ int lan966x_fdma_init(struct lan966x *lan966x)
 
 	err = lan966x_fdma_tx_alloc(&lan966x->tx);
 	if (err) {
-		lan966x_fdma_rx_free(&lan966x->rx);
+		fdma_free_coherent(lan966x->dev, &lan966x->rx.fdma);
 		return err;
 	}
 
@@ -1040,7 +1028,7 @@ void lan966x_fdma_deinit(struct lan966x *lan966x)
 	napi_disable(&lan966x->napi);
 
 	lan966x_fdma_rx_free_pages(&lan966x->rx);
-	lan966x_fdma_rx_free(&lan966x->rx);
+	fdma_free_coherent(lan966x->dev, &lan966x->rx.fdma);
 	page_pool_destroy(lan966x->rx.page_pool);
 	lan966x_fdma_tx_free(&lan966x->tx);
 }

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH net-next 07/12] net: lan966x: use the FDMA library for allocation of tx buffers
  2024-09-05  8:06 [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library Daniel Machon
                   ` (5 preceding siblings ...)
  2024-09-05  8:06 ` [PATCH net-next 06/12] net: lan966x: use library helper for freeing rx buffers Daniel Machon
@ 2024-09-05  8:06 ` Daniel Machon
  2024-09-05  8:06 ` [PATCH net-next 08/12] net: lan966x: use FDMA library for adding DCB's in the tx path Daniel Machon
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 16+ messages in thread
From: Daniel Machon @ 2024-09-05  8:06 UTC (permalink / raw)
  To: Horatiu Vultur, UNGLinuxDriver, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend
  Cc: netdev, linux-kernel, bpf

Use the two functions: fdma_alloc_phys() and fdma_dcb_init() for rx
buffer allocation and use the new buffers throughout.

In order to replace the old buffers with the new ones, we have to do the
following refactoring:

    - use fdma_alloc_phys() and fdma_dcb_init()

    - replace the variables: tx->dma, tx->dcbs and tx->curr_entry
      with the equivalents from the FDMA struct.

    - add lan966x_fdma_tx_dataptr_cb callback for obtaining the dataptr.

    - Initialize FDMA struct values.

Signed-off-by: Daniel Machon <daniel.machon@microchip.com>
Reviewed-by: Horatiu Vultur <horatiu.vultur@microchip.com>
---
 .../net/ethernet/microchip/lan966x/lan966x_fdma.c  | 75 ++++++++++------------
 .../net/ethernet/microchip/lan966x/lan966x_main.h  | 16 -----
 2 files changed, 34 insertions(+), 57 deletions(-)

diff --git a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
index 627806a10674..3afc6c4c68a4 100644
--- a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
+++ b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
@@ -23,6 +23,16 @@ static int lan966x_fdma_rx_dataptr_cb(struct fdma *fdma, int dcb, int db,
 	return 0;
 }
 
+static int lan966x_fdma_tx_dataptr_cb(struct fdma *fdma, int dcb, int db,
+				      u64 *dataptr)
+{
+	struct lan966x *lan966x = (struct lan966x *)fdma->priv;
+
+	*dataptr = lan966x->tx.dcbs_buf[dcb].dma_addr;
+
+	return 0;
+}
+
 static int lan966x_fdma_channel_active(struct lan966x *lan966x)
 {
 	return lan_rd(lan966x, FDMA_CH_ACTIVE);
@@ -182,46 +192,22 @@ static void lan966x_fdma_rx_reload(struct lan966x_rx *rx)
 		lan966x, FDMA_CH_RELOAD);
 }
 
-static void lan966x_fdma_tx_add_dcb(struct lan966x_tx *tx,
-				    struct lan966x_tx_dcb *dcb)
-{
-	dcb->nextptr = FDMA_DCB_INVALID_DATA;
-	dcb->info = 0;
-}
-
 static int lan966x_fdma_tx_alloc(struct lan966x_tx *tx)
 {
 	struct lan966x *lan966x = tx->lan966x;
 	struct fdma *fdma = &tx->fdma;
-	struct lan966x_tx_dcb *dcb;
-	struct lan966x_db *db;
-	int size;
-	int i, j;
+	int err;
 
 	tx->dcbs_buf = kcalloc(fdma->n_dcbs, sizeof(struct lan966x_tx_dcb_buf),
 			       GFP_KERNEL);
 	if (!tx->dcbs_buf)
 		return -ENOMEM;
 
-	/* calculate how many pages are needed to allocate the dcbs */
-	size = sizeof(struct lan966x_tx_dcb) * fdma->n_dcbs;
-	size = ALIGN(size, PAGE_SIZE);
-	tx->dcbs = dma_alloc_coherent(lan966x->dev, size, &tx->dma, GFP_KERNEL);
-	if (!tx->dcbs)
+	err = fdma_alloc_coherent(lan966x->dev, fdma);
+	if (err)
 		goto out;
 
-	/* Now for each dcb allocate the db */
-	for (i = 0; i < fdma->n_dcbs; ++i) {
-		dcb = &tx->dcbs[i];
-
-		for (j = 0; j < fdma->n_dbs; ++j) {
-			db = &dcb->db[j];
-			db->dataptr = 0;
-			db->status = 0;
-		}
-
-		lan966x_fdma_tx_add_dcb(tx, dcb);
-	}
+	fdma_dcbs_init(fdma, 0, 0);
 
 	return 0;
 
@@ -238,7 +224,7 @@ static void lan966x_fdma_tx_free(struct lan966x_tx *tx)
 
 	kfree(tx->dcbs_buf);
 
-	size = sizeof(struct lan966x_tx_dcb) * fdma->n_dcbs;
+	size = sizeof(struct fdma_dcb) * fdma->n_dcbs;
 	size = ALIGN(size, PAGE_SIZE);
 	dma_free_coherent(lan966x->dev, size, fdma->dcbs, fdma->dma);
 }
@@ -252,9 +238,9 @@ static void lan966x_fdma_tx_activate(struct lan966x_tx *tx)
 	/* When activating a channel, first is required to write the first DCB
 	 * address and then to activate it
 	 */
-	lan_wr(lower_32_bits((u64)tx->dma), lan966x,
+	lan_wr(lower_32_bits((u64)fdma->dma), lan966x,
 	       FDMA_DCB_LLP(fdma->channel_id));
-	lan_wr(upper_32_bits((u64)tx->dma), lan966x,
+	lan_wr(upper_32_bits((u64)fdma->dma), lan966x,
 	       FDMA_DCB_LLP1(fdma->channel_id));
 
 	lan_wr(FDMA_CH_CFG_CH_DCB_DB_CNT_SET(fdma->n_dbs) |
@@ -349,22 +335,23 @@ static void lan966x_fdma_tx_clear_buf(struct lan966x *lan966x, int weight)
 	struct lan966x_tx *tx = &lan966x->tx;
 	struct lan966x_rx *rx = &lan966x->rx;
 	struct lan966x_tx_dcb_buf *dcb_buf;
+	struct fdma *fdma = &tx->fdma;
 	struct xdp_frame_bulk bq;
-	struct lan966x_db *db;
 	unsigned long flags;
 	bool clear = false;
+	struct fdma_db *db;
 	int i;
 
 	xdp_frame_bulk_init(&bq);
 
 	spin_lock_irqsave(&lan966x->tx_lock, flags);
-	for (i = 0; i < tx->fdma.n_dcbs; ++i) {
+	for (i = 0; i < fdma->n_dcbs; ++i) {
 		dcb_buf = &tx->dcbs_buf[i];
 
 		if (!dcb_buf->used)
 			continue;
 
-		db = &tx->dcbs[i].db[0];
+		db = &fdma->dcbs[i].db[0];
 		if (!(db->status & FDMA_DCB_STATUS_DONE))
 			continue;
 
@@ -617,10 +604,10 @@ static void lan966x_fdma_tx_setup_dcb(struct lan966x_tx *tx,
 				      int next_to_use, int len,
 				      dma_addr_t dma_addr)
 {
-	struct lan966x_tx_dcb *next_dcb;
-	struct lan966x_db *next_db;
+	struct fdma_dcb *next_dcb;
+	struct fdma_db *next_db;
 
-	next_dcb = &tx->dcbs[next_to_use];
+	next_dcb = &tx->fdma.dcbs[next_to_use];
 	next_dcb->nextptr = FDMA_DCB_INVALID_DATA;
 
 	next_db = &next_dcb->db[0];
@@ -635,13 +622,14 @@ static void lan966x_fdma_tx_setup_dcb(struct lan966x_tx *tx,
 static void lan966x_fdma_tx_start(struct lan966x_tx *tx, int next_to_use)
 {
 	struct lan966x *lan966x = tx->lan966x;
-	struct lan966x_tx_dcb *dcb;
+	struct fdma *fdma = &tx->fdma;
+	struct fdma_dcb *dcb;
 
 	if (likely(lan966x->tx.activated)) {
 		/* Connect current dcb to the next db */
-		dcb = &tx->dcbs[tx->last_in_use];
-		dcb->nextptr = tx->dma + (next_to_use *
-					  sizeof(struct lan966x_tx_dcb));
+		dcb = &fdma->dcbs[tx->last_in_use];
+		dcb->nextptr = fdma->dma + (next_to_use *
+					  sizeof(struct fdma_dcb));
 
 		lan966x_fdma_tx_reload(tx);
 	} else {
@@ -999,6 +987,11 @@ int lan966x_fdma_init(struct lan966x *lan966x)
 	lan966x->tx.fdma.channel_id = FDMA_INJ_CHANNEL;
 	lan966x->tx.fdma.n_dcbs = FDMA_DCB_MAX;
 	lan966x->tx.fdma.n_dbs = FDMA_TX_DCB_MAX_DBS;
+	lan966x->tx.fdma.priv = lan966x;
+	lan966x->tx.fdma.size = fdma_get_size(&lan966x->tx.fdma);
+	lan966x->tx.fdma.db_size = PAGE_SIZE << lan966x->rx.page_order;
+	lan966x->tx.fdma.ops.nextptr_cb = &fdma_nextptr_cb;
+	lan966x->tx.fdma.ops.dataptr_cb = &lan966x_fdma_tx_dataptr_cb;
 	lan966x->tx.last_in_use = -1;
 
 	err = lan966x_fdma_rx_alloc(&lan966x->rx);
diff --git a/drivers/net/ethernet/microchip/lan966x/lan966x_main.h b/drivers/net/ethernet/microchip/lan966x/lan966x_main.h
index 8edb5ea484ee..99efc596c9e6 100644
--- a/drivers/net/ethernet/microchip/lan966x/lan966x_main.h
+++ b/drivers/net/ethernet/microchip/lan966x/lan966x_main.h
@@ -191,17 +191,6 @@ enum vcap_is1_port_sel_rt {
 
 struct lan966x_port;
 
-struct lan966x_db {
-	u64 dataptr;
-	u64 status;
-};
-
-struct lan966x_tx_dcb {
-	u64 nextptr;
-	u64 info;
-	struct lan966x_db db[FDMA_TX_DCB_MAX_DBS];
-};
-
 struct lan966x_rx {
 	struct lan966x *lan966x;
 
@@ -243,13 +232,8 @@ struct lan966x_tx {
 
 	struct fdma fdma;
 
-	/* Pointer to the dcb list */
-	struct lan966x_tx_dcb *dcbs;
 	u16 last_in_use;
 
-	/* Represents the DMA address to the first entry of the dcb entries. */
-	dma_addr_t dma;
-
 	/* Array of dcbs that are given to the HW */
 	struct lan966x_tx_dcb_buf *dcbs_buf;
 

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH net-next 08/12] net: lan966x: use FDMA library for adding DCB's in the tx path
  2024-09-05  8:06 [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library Daniel Machon
                   ` (6 preceding siblings ...)
  2024-09-05  8:06 ` [PATCH net-next 07/12] net: lan966x: use the FDMA library for allocation of tx buffers Daniel Machon
@ 2024-09-05  8:06 ` Daniel Machon
  2024-09-05  8:06 ` [PATCH net-next 09/12] net: lan966x: use library helper for freeing tx buffers Daniel Machon
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 16+ messages in thread
From: Daniel Machon @ 2024-09-05  8:06 UTC (permalink / raw)
  To: Horatiu Vultur, UNGLinuxDriver, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend
  Cc: netdev, linux-kernel, bpf

Use the fdma_dcb_add() function to add DCB's in the tx path. This gets
rid of the open-coding of nextptr and dataptr handling and leaves it to
the library.

Signed-off-by: Daniel Machon <daniel.machon@microchip.com>
Reviewed-by: Horatiu Vultur <horatiu.vultur@microchip.com>
---
 .../net/ethernet/microchip/lan966x/lan966x_fdma.c  | 62 +++++++++++-----------
 1 file changed, 30 insertions(+), 32 deletions(-)

diff --git a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
index 3afc6c4c68a4..1beafadce87a 100644
--- a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
+++ b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
@@ -33,6 +33,16 @@ static int lan966x_fdma_tx_dataptr_cb(struct fdma *fdma, int dcb, int db,
 	return 0;
 }
 
+static int lan966x_fdma_xdp_tx_dataptr_cb(struct fdma *fdma, int dcb, int db,
+					  u64 *dataptr)
+{
+	struct lan966x *lan966x = (struct lan966x *)fdma->priv;
+
+	*dataptr = lan966x->tx.dcbs_buf[dcb].dma_addr + XDP_PACKET_HEADROOM;
+
+	return 0;
+}
+
 static int lan966x_fdma_channel_active(struct lan966x *lan966x)
 {
 	return lan_rd(lan966x, FDMA_CH_ACTIVE);
@@ -600,25 +610,6 @@ static int lan966x_fdma_get_next_dcb(struct lan966x_tx *tx)
 	return -1;
 }
 
-static void lan966x_fdma_tx_setup_dcb(struct lan966x_tx *tx,
-				      int next_to_use, int len,
-				      dma_addr_t dma_addr)
-{
-	struct fdma_dcb *next_dcb;
-	struct fdma_db *next_db;
-
-	next_dcb = &tx->fdma.dcbs[next_to_use];
-	next_dcb->nextptr = FDMA_DCB_INVALID_DATA;
-
-	next_db = &next_dcb->db[0];
-	next_db->dataptr = dma_addr;
-	next_db->status = FDMA_DCB_STATUS_SOF |
-			  FDMA_DCB_STATUS_EOF |
-			  FDMA_DCB_STATUS_INTR |
-			  FDMA_DCB_STATUS_BLOCKO(0) |
-			  FDMA_DCB_STATUS_BLOCKL(len);
-}
-
 static void lan966x_fdma_tx_start(struct lan966x_tx *tx, int next_to_use)
 {
 	struct lan966x *lan966x = tx->lan966x;
@@ -692,11 +683,6 @@ int lan966x_fdma_xmit_xdpf(struct lan966x_port *port, void *ptr, u32 len)
 
 		next_dcb_buf->data.xdpf = xdpf;
 		next_dcb_buf->len = xdpf->len + IFH_LEN_BYTES;
-
-		/* Setup next dcb */
-		lan966x_fdma_tx_setup_dcb(tx, next_to_use,
-					  xdpf->len + IFH_LEN_BYTES,
-					  dma_addr);
 	} else {
 		page = ptr;
 
@@ -713,11 +699,6 @@ int lan966x_fdma_xmit_xdpf(struct lan966x_port *port, void *ptr, u32 len)
 
 		next_dcb_buf->data.page = page;
 		next_dcb_buf->len = len + IFH_LEN_BYTES;
-
-		/* Setup next dcb */
-		lan966x_fdma_tx_setup_dcb(tx, next_to_use,
-					  len + IFH_LEN_BYTES,
-					  dma_addr + XDP_PACKET_HEADROOM);
 	}
 
 	/* Fill up the buffer */
@@ -728,6 +709,17 @@ int lan966x_fdma_xmit_xdpf(struct lan966x_port *port, void *ptr, u32 len)
 	next_dcb_buf->ptp = false;
 	next_dcb_buf->dev = port->dev;
 
+	__fdma_dcb_add(&tx->fdma,
+		       next_to_use,
+		       0,
+		       FDMA_DCB_STATUS_INTR |
+		       FDMA_DCB_STATUS_SOF |
+		       FDMA_DCB_STATUS_EOF |
+		       FDMA_DCB_STATUS_BLOCKO(0) |
+		       FDMA_DCB_STATUS_BLOCKL(next_dcb_buf->len),
+		       &fdma_nextptr_cb,
+		       &lan966x_fdma_xdp_tx_dataptr_cb);
+
 	/* Start the transmission */
 	lan966x_fdma_tx_start(tx, next_to_use);
 
@@ -787,9 +779,6 @@ int lan966x_fdma_xmit(struct sk_buff *skb, __be32 *ifh, struct net_device *dev)
 		goto release;
 	}
 
-	/* Setup next dcb */
-	lan966x_fdma_tx_setup_dcb(tx, next_to_use, skb->len, dma_addr);
-
 	/* Fill up the buffer */
 	next_dcb_buf = &tx->dcbs_buf[next_to_use];
 	next_dcb_buf->use_skb = true;
@@ -801,6 +790,15 @@ int lan966x_fdma_xmit(struct sk_buff *skb, __be32 *ifh, struct net_device *dev)
 	next_dcb_buf->ptp = false;
 	next_dcb_buf->dev = dev;
 
+	fdma_dcb_add(&tx->fdma,
+		     next_to_use,
+		     0,
+		     FDMA_DCB_STATUS_INTR |
+		     FDMA_DCB_STATUS_SOF |
+		     FDMA_DCB_STATUS_EOF |
+		     FDMA_DCB_STATUS_BLOCKO(0) |
+		     FDMA_DCB_STATUS_BLOCKL(skb->len));
+
 	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
 	    LAN966X_SKB_CB(skb)->rew_op == IFH_REW_OP_TWO_STEP_PTP)
 		next_dcb_buf->ptp = true;

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH net-next 09/12] net: lan966x: use library helper for freeing tx buffers
  2024-09-05  8:06 [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library Daniel Machon
                   ` (7 preceding siblings ...)
  2024-09-05  8:06 ` [PATCH net-next 08/12] net: lan966x: use FDMA library for adding DCB's in the tx path Daniel Machon
@ 2024-09-05  8:06 ` Daniel Machon
  2024-09-05  8:06 ` [PATCH net-next 10/12] net: lan966x: ditch tx->last_in_use variable Daniel Machon
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 16+ messages in thread
From: Daniel Machon @ 2024-09-05  8:06 UTC (permalink / raw)
  To: Horatiu Vultur, UNGLinuxDriver, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend
  Cc: netdev, linux-kernel, bpf

The library has the helper fdma_free_phys() for freeing physical FDMA
memory. Use it in the exit path.

Signed-off-by: Daniel Machon <daniel.machon@microchip.com>
Reviewed-by: Horatiu Vultur <horatiu.vultur@microchip.com>
---
 drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
index 1beafadce87a..6f7e3c27c1a7 100644
--- a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
+++ b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
@@ -229,14 +229,9 @@ static int lan966x_fdma_tx_alloc(struct lan966x_tx *tx)
 static void lan966x_fdma_tx_free(struct lan966x_tx *tx)
 {
 	struct lan966x *lan966x = tx->lan966x;
-	struct fdma *fdma = &tx->fdma;
-	int size;
 
 	kfree(tx->dcbs_buf);
-
-	size = sizeof(struct fdma_dcb) * fdma->n_dcbs;
-	size = ALIGN(size, PAGE_SIZE);
-	dma_free_coherent(lan966x->dev, size, fdma->dcbs, fdma->dma);
+	fdma_free_coherent(lan966x->dev, &tx->fdma);
 }
 
 static void lan966x_fdma_tx_activate(struct lan966x_tx *tx)

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH net-next 10/12] net: lan966x: ditch tx->last_in_use variable
  2024-09-05  8:06 [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library Daniel Machon
                   ` (8 preceding siblings ...)
  2024-09-05  8:06 ` [PATCH net-next 09/12] net: lan966x: use library helper for freeing tx buffers Daniel Machon
@ 2024-09-05  8:06 ` Daniel Machon
  2024-09-05  8:06 ` [PATCH net-next 11/12] net: lan966x: use a few FDMA helpers throughout Daniel Machon
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 16+ messages in thread
From: Daniel Machon @ 2024-09-05  8:06 UTC (permalink / raw)
  To: Horatiu Vultur, UNGLinuxDriver, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend
  Cc: netdev, linux-kernel, bpf

This variable is used in the tx path to determine the last used DCB. The
library has the variable last_dcb for the exact same purpose. Ditch the
last_in_use variable throughout.

Signed-off-by: Daniel Machon <daniel.machon@microchip.com>
Reviewed-by: Horatiu Vultur <horatiu.vultur@microchip.com>
---
 .../net/ethernet/microchip/lan966x/lan966x_fdma.c    | 20 ++++----------------
 .../net/ethernet/microchip/lan966x/lan966x_main.h    |  2 --
 2 files changed, 4 insertions(+), 18 deletions(-)

diff --git a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
index 6f7e3c27c1a7..b5a97c5a2e1b 100644
--- a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
+++ b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
@@ -293,7 +293,6 @@ static void lan966x_fdma_tx_disable(struct lan966x_tx *tx)
 		lan966x, FDMA_CH_DB_DISCARD);
 
 	tx->activated = false;
-	tx->last_in_use = -1;
 }
 
 static void lan966x_fdma_tx_reload(struct lan966x_tx *tx)
@@ -598,34 +597,24 @@ static int lan966x_fdma_get_next_dcb(struct lan966x_tx *tx)
 
 	for (i = 0; i < fdma->n_dcbs; ++i) {
 		dcb_buf = &tx->dcbs_buf[i];
-		if (!dcb_buf->used && i != tx->last_in_use)
+		if (!dcb_buf->used && &fdma->dcbs[i] != fdma->last_dcb)
 			return i;
 	}
 
 	return -1;
 }
 
-static void lan966x_fdma_tx_start(struct lan966x_tx *tx, int next_to_use)
+static void lan966x_fdma_tx_start(struct lan966x_tx *tx)
 {
 	struct lan966x *lan966x = tx->lan966x;
-	struct fdma *fdma = &tx->fdma;
-	struct fdma_dcb *dcb;
 
 	if (likely(lan966x->tx.activated)) {
-		/* Connect current dcb to the next db */
-		dcb = &fdma->dcbs[tx->last_in_use];
-		dcb->nextptr = fdma->dma + (next_to_use *
-					  sizeof(struct fdma_dcb));
-
 		lan966x_fdma_tx_reload(tx);
 	} else {
 		/* Because it is first time, then just activate */
 		lan966x->tx.activated = true;
 		lan966x_fdma_tx_activate(tx);
 	}
-
-	/* Move to next dcb because this last in use */
-	tx->last_in_use = next_to_use;
 }
 
 int lan966x_fdma_xmit_xdpf(struct lan966x_port *port, void *ptr, u32 len)
@@ -716,7 +705,7 @@ int lan966x_fdma_xmit_xdpf(struct lan966x_port *port, void *ptr, u32 len)
 		       &lan966x_fdma_xdp_tx_dataptr_cb);
 
 	/* Start the transmission */
-	lan966x_fdma_tx_start(tx, next_to_use);
+	lan966x_fdma_tx_start(tx);
 
 out:
 	spin_unlock(&lan966x->tx_lock);
@@ -799,7 +788,7 @@ int lan966x_fdma_xmit(struct sk_buff *skb, __be32 *ifh, struct net_device *dev)
 		next_dcb_buf->ptp = true;
 
 	/* Start the transmission */
-	lan966x_fdma_tx_start(tx, next_to_use);
+	lan966x_fdma_tx_start(tx);
 
 	return NETDEV_TX_OK;
 
@@ -985,7 +974,6 @@ int lan966x_fdma_init(struct lan966x *lan966x)
 	lan966x->tx.fdma.db_size = PAGE_SIZE << lan966x->rx.page_order;
 	lan966x->tx.fdma.ops.nextptr_cb = &fdma_nextptr_cb;
 	lan966x->tx.fdma.ops.dataptr_cb = &lan966x_fdma_tx_dataptr_cb;
-	lan966x->tx.last_in_use = -1;
 
 	err = lan966x_fdma_rx_alloc(&lan966x->rx);
 	if (err)
diff --git a/drivers/net/ethernet/microchip/lan966x/lan966x_main.h b/drivers/net/ethernet/microchip/lan966x/lan966x_main.h
index 99efc596c9e6..25cb2f61986f 100644
--- a/drivers/net/ethernet/microchip/lan966x/lan966x_main.h
+++ b/drivers/net/ethernet/microchip/lan966x/lan966x_main.h
@@ -232,8 +232,6 @@ struct lan966x_tx {
 
 	struct fdma fdma;
 
-	u16 last_in_use;
-
 	/* Array of dcbs that are given to the HW */
 	struct lan966x_tx_dcb_buf *dcbs_buf;
 

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH net-next 11/12] net: lan966x: use a few FDMA helpers throughout
  2024-09-05  8:06 [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library Daniel Machon
                   ` (9 preceding siblings ...)
  2024-09-05  8:06 ` [PATCH net-next 10/12] net: lan966x: ditch tx->last_in_use variable Daniel Machon
@ 2024-09-05  8:06 ` Daniel Machon
  2024-09-05  8:06 ` [PATCH net-next 12/12] net: lan966x: refactor buffer reload function Daniel Machon
  2024-09-10  9:20 ` [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library patchwork-bot+netdevbpf
  12 siblings, 0 replies; 16+ messages in thread
From: Daniel Machon @ 2024-09-05  8:06 UTC (permalink / raw)
  To: Horatiu Vultur, UNGLinuxDriver, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend
  Cc: netdev, linux-kernel, bpf

The library provides helpers for a number of DCB and DB operations. Use
these throughout the code and remove the old ones.

Signed-off-by: Daniel Machon <daniel.machon@microchip.com>
Reviewed-by: Horatiu Vultur <horatiu.vultur@microchip.com>
---
 .../net/ethernet/microchip/lan966x/lan966x_fdma.c  | 42 ++++++----------------
 1 file changed, 11 insertions(+), 31 deletions(-)

diff --git a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
index b5a97c5a2e1b..4c8f83e4c5de 100644
--- a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
+++ b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
@@ -126,14 +126,6 @@ static int lan966x_fdma_rx_alloc(struct lan966x_rx *rx)
 	return 0;
 }
 
-static void lan966x_fdma_rx_advance_dcb(struct lan966x_rx *rx)
-{
-	struct fdma *fdma = &rx->fdma;
-
-	fdma->dcb_index++;
-	fdma->dcb_index &= fdma->n_dcbs - 1;
-}
-
 static void lan966x_fdma_rx_start(struct lan966x_rx *rx)
 {
 	struct lan966x *lan966x = rx->lan966x;
@@ -355,8 +347,8 @@ static void lan966x_fdma_tx_clear_buf(struct lan966x *lan966x, int weight)
 		if (!dcb_buf->used)
 			continue;
 
-		db = &fdma->dcbs[i].db[0];
-		if (!(db->status & FDMA_DCB_STATUS_DONE))
+		db = fdma_db_get(fdma, i, 0);
+		if (!fdma_db_is_done(db))
 			continue;
 
 		dcb_buf->dev->stats.tx_packets++;
@@ -396,19 +388,6 @@ static void lan966x_fdma_tx_clear_buf(struct lan966x *lan966x, int weight)
 	spin_unlock_irqrestore(&lan966x->tx_lock, flags);
 }
 
-static bool lan966x_fdma_rx_more_frames(struct lan966x_rx *rx)
-{
-	struct fdma *fdma = &rx->fdma;
-	struct fdma_db *db;
-
-	/* Check if there is any data */
-	db = &fdma->dcbs[fdma->dcb_index].db[fdma->db_index];
-	if (unlikely(!(db->status & FDMA_DCB_STATUS_DONE)))
-		return false;
-
-	return true;
-}
-
 static int lan966x_fdma_rx_check_frame(struct lan966x_rx *rx, u64 *src_port)
 {
 	struct lan966x *lan966x = rx->lan966x;
@@ -417,7 +396,7 @@ static int lan966x_fdma_rx_check_frame(struct lan966x_rx *rx, u64 *src_port)
 	struct fdma_db *db;
 	struct page *page;
 
-	db = &fdma->dcbs[fdma->dcb_index].db[fdma->db_index];
+	db = fdma_db_next_get(fdma);
 	page = rx->page[fdma->dcb_index][fdma->db_index];
 	if (unlikely(!page))
 		return FDMA_ERROR;
@@ -450,7 +429,7 @@ static struct sk_buff *lan966x_fdma_rx_get_frame(struct lan966x_rx *rx,
 	u64 timestamp;
 
 	/* Get the received frame and unmap it */
-	db = &fdma->dcbs[fdma->dcb_index].db[fdma->db_index];
+	db = fdma_db_next_get(fdma);
 	page = rx->page[fdma->dcb_index][fdma->db_index];
 
 	skb = build_skb(page_address(page), fdma->db_size);
@@ -508,7 +487,7 @@ static int lan966x_fdma_napi_poll(struct napi_struct *napi, int weight)
 
 	/* Get all received skb */
 	while (counter < weight) {
-		if (!lan966x_fdma_rx_more_frames(rx))
+		if (!fdma_has_frames(fdma))
 			break;
 
 		counter++;
@@ -518,22 +497,22 @@ static int lan966x_fdma_napi_poll(struct napi_struct *napi, int weight)
 			break;
 		case FDMA_ERROR:
 			lan966x_fdma_rx_free_page(rx);
-			lan966x_fdma_rx_advance_dcb(rx);
+			fdma_dcb_advance(fdma);
 			goto allocate_new;
 		case FDMA_REDIRECT:
 			redirect = true;
 			fallthrough;
 		case FDMA_TX:
-			lan966x_fdma_rx_advance_dcb(rx);
+			fdma_dcb_advance(fdma);
 			continue;
 		case FDMA_DROP:
 			lan966x_fdma_rx_free_page(rx);
-			lan966x_fdma_rx_advance_dcb(rx);
+			fdma_dcb_advance(fdma);
 			continue;
 		}
 
 		skb = lan966x_fdma_rx_get_frame(rx, src_port);
-		lan966x_fdma_rx_advance_dcb(rx);
+		fdma_dcb_advance(fdma);
 		if (!skb)
 			goto allocate_new;
 
@@ -597,7 +576,8 @@ static int lan966x_fdma_get_next_dcb(struct lan966x_tx *tx)
 
 	for (i = 0; i < fdma->n_dcbs; ++i) {
 		dcb_buf = &tx->dcbs_buf[i];
-		if (!dcb_buf->used && &fdma->dcbs[i] != fdma->last_dcb)
+		if (!dcb_buf->used &&
+		    !fdma_is_last(&tx->fdma, &tx->fdma.dcbs[i]))
 			return i;
 	}
 

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH net-next 12/12] net: lan966x: refactor buffer reload function
  2024-09-05  8:06 [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library Daniel Machon
                   ` (10 preceding siblings ...)
  2024-09-05  8:06 ` [PATCH net-next 11/12] net: lan966x: use a few FDMA helpers throughout Daniel Machon
@ 2024-09-05  8:06 ` Daniel Machon
  2024-09-10  9:20 ` [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library patchwork-bot+netdevbpf
  12 siblings, 0 replies; 16+ messages in thread
From: Daniel Machon @ 2024-09-05  8:06 UTC (permalink / raw)
  To: Horatiu Vultur, UNGLinuxDriver, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend
  Cc: netdev, linux-kernel, bpf

Now that we store everything in the fdma structs, refactor
lan966x_fdma_reload() to store and restore the entire struct.

Signed-off-by: Daniel Machon <daniel.machon@microchip.com>
Reviewed-by: Horatiu Vultur <horatiu.vultur@microchip.com>
---
 drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
index 4c8f83e4c5de..502670718104 100644
--- a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
+++ b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
@@ -810,14 +810,11 @@ static int lan966x_qsys_sw_status(struct lan966x *lan966x)
 static int lan966x_fdma_reload(struct lan966x *lan966x, int new_mtu)
 {
 	struct page_pool *page_pool;
-	dma_addr_t rx_dma;
-	void *rx_dcbs;
-	u32 size;
+	struct fdma fdma_rx_old;
 	int err;
 
 	/* Store these for later to free them */
-	rx_dma = lan966x->rx.fdma.dma;
-	rx_dcbs = lan966x->rx.fdma.dcbs;
+	memcpy(&fdma_rx_old, &lan966x->rx.fdma, sizeof(struct fdma));
 	page_pool = lan966x->rx.page_pool;
 
 	napi_synchronize(&lan966x->napi);
@@ -833,9 +830,7 @@ static int lan966x_fdma_reload(struct lan966x *lan966x, int new_mtu)
 		goto restore;
 	lan966x_fdma_rx_start(&lan966x->rx);
 
-	size = sizeof(struct fdma_dcb) * lan966x->rx.fdma.n_dcbs;
-	size = ALIGN(size, PAGE_SIZE);
-	dma_free_coherent(lan966x->dev, size, rx_dcbs, rx_dma);
+	fdma_free_coherent(lan966x->dev, &fdma_rx_old);
 
 	page_pool_destroy(page_pool);
 
@@ -845,8 +840,7 @@ static int lan966x_fdma_reload(struct lan966x *lan966x, int new_mtu)
 	return err;
 restore:
 	lan966x->rx.page_pool = page_pool;
-	lan966x->rx.fdma.dma = rx_dma;
-	lan966x->rx.fdma.dcbs = rx_dcbs;
+	memcpy(&lan966x->rx.fdma, &fdma_rx_old, sizeof(struct fdma));
 	lan966x_fdma_rx_start(&lan966x->rx);
 
 	return err;

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH net-next 04/12] net: lan966x: use the FDMA library for allocation of rx buffers
  2024-09-05  8:06 ` [PATCH net-next 04/12] net: lan966x: use the FDMA library for allocation of rx buffers Daniel Machon
@ 2024-09-10  9:15   ` Paolo Abeni
  2024-09-10  9:42     ` Daniel Machon
  0 siblings, 1 reply; 16+ messages in thread
From: Paolo Abeni @ 2024-09-10  9:15 UTC (permalink / raw)
  To: Daniel Machon, Horatiu Vultur, UNGLinuxDriver, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend
  Cc: netdev, linux-kernel, bpf

On 9/5/24 10:06, Daniel Machon wrote:
> Use the two functions: fdma_alloc_phys() and fdma_dcb_init() for rx
> buffer allocation and use the new buffers throughout.
> 
> In order to replace the old buffers with the new ones, we have to do the
> following refactoring:
> 
>      - use fdma_alloc_phys() and fdma_dcb_init()
> 
>      - replace the variables: rx->dma, rx->dcbs and rx->last_entry
>        with the equivalents from the FDMA struct.
> 
>      - make use of fdma->db_size for rx buffer size.
> 
>      - add lan966x_fdma_rx_dataptr_cb callback for obtaining the dataptr.
> 
>      - Initialize FDMA struct values.
> 
> Signed-off-by: Daniel Machon <daniel.machon@microchip.com>
> Reviewed-by: Horatiu Vultur <horatiu.vultur@microchip.com>
> ---
>   .../net/ethernet/microchip/lan966x/lan966x_fdma.c  | 116 ++++++++++-----------
>   .../net/ethernet/microchip/lan966x/lan966x_main.h  |  15 ---
>   2 files changed, 55 insertions(+), 76 deletions(-)
> 
> diff --git a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
> index b64f04ff99a8..99d09c97737e 100644
> --- a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
> +++ b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
> @@ -6,13 +6,30 @@
>   
>   #include "lan966x_main.h"
>   
> +static int lan966x_fdma_rx_dataptr_cb(struct fdma *fdma, int dcb, int db,
> +				      u64 *dataptr)
> +{
> +	struct lan966x *lan966x = (struct lan966x *)fdma->priv;
> +	struct lan966x_rx *rx = &lan966x->rx;
> +	struct page *page;
> +
> +	page = page_pool_dev_alloc_pages(rx->page_pool);
> +	if (unlikely(!page))
> +		return -ENOMEM;
> +
> +	rx->page[dcb][db] = page;
> +	*dataptr = page_pool_get_dma_addr(page) + XDP_PACKET_HEADROOM;
> +
> +	return 0;
> +}

Very nice cleanup indeed!

Out of ENOMEM I can't recall if the following was already discussed, but 
looking at this cb, I'm wondering if a possible follow-up could replace 
the dataptr_cb() and nextptr_cb() with lib functions i.e. operating on 
page pool or doing netdev allocations according to some fdma lib flags.

Cheers,

Paolo


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library
  2024-09-05  8:06 [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library Daniel Machon
                   ` (11 preceding siblings ...)
  2024-09-05  8:06 ` [PATCH net-next 12/12] net: lan966x: refactor buffer reload function Daniel Machon
@ 2024-09-10  9:20 ` patchwork-bot+netdevbpf
  12 siblings, 0 replies; 16+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-09-10  9:20 UTC (permalink / raw)
  To: Daniel Machon
  Cc: horatiu.vultur, UNGLinuxDriver, davem, edumazet, kuba, pabeni,
	ast, daniel, hawk, john.fastabend, netdev, linux-kernel, bpf

Hello:

This series was applied to netdev/net-next.git (main)
by Paolo Abeni <pabeni@redhat.com>:

On Thu, 5 Sep 2024 10:06:28 +0200 you wrote:
> This patch series is the second of a 2-part series [1], that adds a new
> common FDMA library for Microchip switch chips Sparx5 and lan966x. These
> chips share the same FDMA engine, and as such will benefit from a common
> library with a common implementation.  This also has the benefit of
> removing a lot of open-coded bookkeeping and duplicate code for the two
> drivers.
> 
> [...]

Here is the summary with links:
  - [net-next,01/12] net: lan966x: select FDMA library
    https://git.kernel.org/netdev/net-next/c/63acda75801f
  - [net-next,02/12] net: lan966x: use FDMA library symbols
    https://git.kernel.org/netdev/net-next/c/1dfe4ca8cb4a
  - [net-next,03/12] net: lan966x: replace a few variables with new equivalent ones
    https://git.kernel.org/netdev/net-next/c/8274d40eafa3
  - [net-next,04/12] net: lan966x: use the FDMA library for allocation of rx buffers
    https://git.kernel.org/netdev/net-next/c/01a70754327b
  - [net-next,05/12] net: lan966x: use FDMA library for adding DCB's in the rx path
    https://git.kernel.org/netdev/net-next/c/2b5a09e67b72
  - [net-next,06/12] net: lan966x: use library helper for freeing rx buffers
    https://git.kernel.org/netdev/net-next/c/f51293b3ea89
  - [net-next,07/12] net: lan966x: use the FDMA library for allocation of tx buffers
    https://git.kernel.org/netdev/net-next/c/df2ddc1458c3
  - [net-next,08/12] net: lan966x: use FDMA library for adding DCB's in the tx path
    https://git.kernel.org/netdev/net-next/c/29cc3a66a81d
  - [net-next,09/12] net: lan966x: use library helper for freeing tx buffers
    https://git.kernel.org/netdev/net-next/c/8cdd0bd02283
  - [net-next,10/12] net: lan966x: ditch tx->last_in_use variable
    https://git.kernel.org/netdev/net-next/c/c06fef96c7d5
  - [net-next,11/12] net: lan966x: use a few FDMA helpers throughout
    https://git.kernel.org/netdev/net-next/c/9fbc5719f6aa
  - [net-next,12/12] net: lan966x: refactor buffer reload function
    https://git.kernel.org/netdev/net-next/c/89ba464fcf54

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH net-next 04/12] net: lan966x: use the FDMA library for allocation of rx buffers
  2024-09-10  9:15   ` Paolo Abeni
@ 2024-09-10  9:42     ` Daniel Machon
  0 siblings, 0 replies; 16+ messages in thread
From: Daniel Machon @ 2024-09-10  9:42 UTC (permalink / raw)
  To: Paolo Abeni
  Cc: Horatiu Vultur, UNGLinuxDriver, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend, netdev, linux-kernel, bpf

> Use the two functions: fdma_alloc_phys() and fdma_dcb_init() for rx
> > buffer allocation and use the new buffers throughout.
> > 
> > In order to replace the old buffers with the new ones, we have to do the
> > following refactoring:
> > 
> >      - use fdma_alloc_phys() and fdma_dcb_init()
> > 
> >      - replace the variables: rx->dma, rx->dcbs and rx->last_entry
> >        with the equivalents from the FDMA struct.
> > 
> >      - make use of fdma->db_size for rx buffer size.
> > 
> >      - add lan966x_fdma_rx_dataptr_cb callback for obtaining the dataptr.
> > 
> >      - Initialize FDMA struct values.
> > 
> > Signed-off-by: Daniel Machon <daniel.machon@microchip.com>
> > Reviewed-by: Horatiu Vultur <horatiu.vultur@microchip.com>
> > ---
> >   .../net/ethernet/microchip/lan966x/lan966x_fdma.c  | 116 ++++++++++-----------
> >   .../net/ethernet/microchip/lan966x/lan966x_main.h  |  15 ---
> >   2 files changed, 55 insertions(+), 76 deletions(-)
> > 
> > diff --git a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
> > index b64f04ff99a8..99d09c97737e 100644
> > --- a/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
> > +++ b/drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c
> > @@ -6,13 +6,30 @@
> > 
> >   #include "lan966x_main.h"
> > 
> > +static int lan966x_fdma_rx_dataptr_cb(struct fdma *fdma, int dcb, int db,
> > +                                   u64 *dataptr)
> > +{
> > +     struct lan966x *lan966x = (struct lan966x *)fdma->priv;
> > +     struct lan966x_rx *rx = &lan966x->rx;
> > +     struct page *page;
> > +
> > +     page = page_pool_dev_alloc_pages(rx->page_pool);
> > +     if (unlikely(!page))
> > +             return -ENOMEM;
> > +
> > +     rx->page[dcb][db] = page;
> > +     *dataptr = page_pool_get_dma_addr(page) + XDP_PACKET_HEADROOM;
> > +
> > +     return 0;
> > +}
> 
> Very nice cleanup indeed!
> 
> Out of ENOMEM I can't recall if the following was already discussed, but
> looking at this cb, I'm wondering if a possible follow-up could replace
> the dataptr_cb() and nextptr_cb() with lib functions i.e. operating on
> page pool or doing netdev allocations according to some fdma lib flags.
> 
> Cheers,
> 
> Paolo
>

Hi Paolo,

Something like this could definitely be added down the road. I initially
left this out to reduce library complexity.

Thanks for reviewing!

/Daniel

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2024-09-10  9:43 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-05  8:06 [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library Daniel Machon
2024-09-05  8:06 ` [PATCH net-next 01/12] net: lan966x: select " Daniel Machon
2024-09-05  8:06 ` [PATCH net-next 02/12] net: lan966x: use FDMA library symbols Daniel Machon
2024-09-05  8:06 ` [PATCH net-next 03/12] net: lan966x: replace a few variables with new equivalent ones Daniel Machon
2024-09-05  8:06 ` [PATCH net-next 04/12] net: lan966x: use the FDMA library for allocation of rx buffers Daniel Machon
2024-09-10  9:15   ` Paolo Abeni
2024-09-10  9:42     ` Daniel Machon
2024-09-05  8:06 ` [PATCH net-next 05/12] net: lan966x: use FDMA library for adding DCB's in the rx path Daniel Machon
2024-09-05  8:06 ` [PATCH net-next 06/12] net: lan966x: use library helper for freeing rx buffers Daniel Machon
2024-09-05  8:06 ` [PATCH net-next 07/12] net: lan966x: use the FDMA library for allocation of tx buffers Daniel Machon
2024-09-05  8:06 ` [PATCH net-next 08/12] net: lan966x: use FDMA library for adding DCB's in the tx path Daniel Machon
2024-09-05  8:06 ` [PATCH net-next 09/12] net: lan966x: use library helper for freeing tx buffers Daniel Machon
2024-09-05  8:06 ` [PATCH net-next 10/12] net: lan966x: ditch tx->last_in_use variable Daniel Machon
2024-09-05  8:06 ` [PATCH net-next 11/12] net: lan966x: use a few FDMA helpers throughout Daniel Machon
2024-09-05  8:06 ` [PATCH net-next 12/12] net: lan966x: refactor buffer reload function Daniel Machon
2024-09-10  9:20 ` [PATCH net-next 00/12] net: lan966x: use the newly introduced FDMA library patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).