Skip to content

Commit

Permalink
Merge branch 'sparx5-fdma-part-one'
Browse files Browse the repository at this point in the history
Daniel Machon says:

====================
net: microchip: add FDMA library and use it for Sparx5

This patch series is the first of a 2-part series, 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 open-coded bookkeeping and duplicate code for the two
drivers.

Additionally, upstreaming efforts for a third chip, lan969x, will begin
in the near future. This chip will use the new library too.

In this first series, the FDMA library is introduced and used by the
Sparx5 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:  introduces library and selects it for Sparx5.

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 torvalds#6:  uses the library for freeing rx buffers.

Patch torvalds#7:  uses the library helpers in the rx path.

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

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

Patch torvalds#10: uses the library helpers in the tx path.

Patch torvalds#11: ditches the existing linked list for storing buffer addresses,
           and instead uses offsets into contiguous memory.

Patch torvalds#12: modifies existing rx and tx functions to be direction
           independent.
====================

Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
davem330 committed Sep 4, 2024
2 parents 3d4d0fa + 5115231 commit ff09bc3
Show file tree
Hide file tree
Showing 10 changed files with 545 additions and 286 deletions.
1 change: 1 addition & 0 deletions drivers/net/ethernet/microchip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ config LAN743X
source "drivers/net/ethernet/microchip/lan966x/Kconfig"
source "drivers/net/ethernet/microchip/sparx5/Kconfig"
source "drivers/net/ethernet/microchip/vcap/Kconfig"
source "drivers/net/ethernet/microchip/fdma/Kconfig"

endif # NET_VENDOR_MICROCHIP
1 change: 1 addition & 0 deletions drivers/net/ethernet/microchip/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ lan743x-objs := lan743x_main.o lan743x_ethtool.o lan743x_ptp.o
obj-$(CONFIG_LAN966X_SWITCH) += lan966x/
obj-$(CONFIG_SPARX5_SWITCH) += sparx5/
obj-$(CONFIG_VCAP) += vcap/
obj-$(CONFIG_FDMA) += fdma/
18 changes: 18 additions & 0 deletions drivers/net/ethernet/microchip/fdma/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# SPDX-License-Identifier: GPL-2.0-only
#
# Microchip FDMA API configuration
#

if NET_VENDOR_MICROCHIP

config FDMA
bool "FDMA API"
help
Provides the basic FDMA functionality for multiple Microchip
switchcores.

Say Y here if you want to build the FDMA API that provides a common
set of functions and data structures for interacting with the Frame
DMA engine in multiple microchip switchcores.

endif # NET_VENDOR_MICROCHIP
7 changes: 7 additions & 0 deletions drivers/net/ethernet/microchip/fdma/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-License-Identifier: GPL-2.0-only
#
# Makefile for Microchip FDMA
#

obj-$(CONFIG_FDMA) += fdma.o
fdma-y += fdma_api.o
146 changes: 146 additions & 0 deletions drivers/net/ethernet/microchip/fdma/fdma_api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// SPDX-License-Identifier: GPL-2.0+

#include "fdma_api.h"

#include <linux/bits.h>
#include <linux/etherdevice.h>
#include <linux/types.h>

/* Add a DB to a DCB, providing a callback for getting the DB dataptr. */
static int __fdma_db_add(struct fdma *fdma, int dcb_idx, int db_idx, u64 status,
int (*cb)(struct fdma *fdma, int dcb_idx,
int db_idx, u64 *dataptr))
{
struct fdma_db *db = fdma_db_get(fdma, dcb_idx, db_idx);

db->status = status;

return cb(fdma, dcb_idx, db_idx, &db->dataptr);
}

/* Add a DB to a DCB, using the callback set in the fdma_ops struct. */
int fdma_db_add(struct fdma *fdma, int dcb_idx, int db_idx, u64 status)
{
return __fdma_db_add(fdma,
dcb_idx,
db_idx,
status,
fdma->ops.dataptr_cb);
}

/* Add a DCB with callbacks for getting the DB dataptr and the DCB nextptr. */
int __fdma_dcb_add(struct fdma *fdma, int dcb_idx, u64 info, u64 status,
int (*dcb_cb)(struct fdma *fdma, int dcb_idx, u64 *nextptr),
int (*db_cb)(struct fdma *fdma, int dcb_idx, int db_idx,
u64 *dataptr))
{
struct fdma_dcb *dcb = fdma_dcb_get(fdma, dcb_idx);
int i, err;

for (i = 0; i < fdma->n_dbs; i++) {
err = __fdma_db_add(fdma, dcb_idx, i, status, db_cb);
if (unlikely(err))
return err;
}

err = dcb_cb(fdma, dcb_idx, &fdma->last_dcb->nextptr);
if (unlikely(err))
return err;

fdma->last_dcb = dcb;

dcb->nextptr = FDMA_DCB_INVALID_DATA;
dcb->info = info;

return 0;
}
EXPORT_SYMBOL_GPL(__fdma_dcb_add);

/* Add a DCB, using the preset callbacks in the fdma_ops struct. */
int fdma_dcb_add(struct fdma *fdma, int dcb_idx, u64 info, u64 status)
{
return __fdma_dcb_add(fdma,
dcb_idx,
info, status,
fdma->ops.nextptr_cb,
fdma->ops.dataptr_cb);
}
EXPORT_SYMBOL_GPL(fdma_dcb_add);

/* Initialize the DCB's and DB's. */
int fdma_dcbs_init(struct fdma *fdma, u64 info, u64 status)
{
int i, err;

fdma->last_dcb = fdma->dcbs;
fdma->db_index = 0;
fdma->dcb_index = 0;

for (i = 0; i < fdma->n_dcbs; i++) {
err = fdma_dcb_add(fdma, i, info, status);
if (err)
return err;
}

return 0;
}
EXPORT_SYMBOL_GPL(fdma_dcbs_init);

/* Allocate coherent DMA memory for FDMA. */
int fdma_alloc_coherent(struct device *dev, struct fdma *fdma)
{
fdma->dcbs = dma_alloc_coherent(dev,
fdma->size,
&fdma->dma,
GFP_KERNEL);
if (!fdma->dcbs)
return -ENOMEM;

return 0;
}
EXPORT_SYMBOL_GPL(fdma_alloc_coherent);

/* Allocate physical memory for FDMA. */
int fdma_alloc_phys(struct fdma *fdma)
{
fdma->dcbs = kzalloc(fdma->size, GFP_KERNEL);
if (!fdma->dcbs)
return -ENOMEM;

fdma->dma = virt_to_phys(fdma->dcbs);

return 0;
}
EXPORT_SYMBOL_GPL(fdma_alloc_phys);

/* Free coherent DMA memory. */
void fdma_free_coherent(struct device *dev, struct fdma *fdma)
{
dma_free_coherent(dev, fdma->size, fdma->dcbs, fdma->dma);
}
EXPORT_SYMBOL_GPL(fdma_free_coherent);

/* Free virtual memory. */
void fdma_free_phys(struct fdma *fdma)
{
kfree(fdma->dcbs);
}
EXPORT_SYMBOL_GPL(fdma_free_phys);

/* Get the size of the FDMA memory */
u32 fdma_get_size(struct fdma *fdma)
{
return ALIGN(sizeof(struct fdma_dcb) * fdma->n_dcbs, PAGE_SIZE);
}
EXPORT_SYMBOL_GPL(fdma_get_size);

/* Get the size of the FDMA memory. This function is only applicable if the
* dataptr addresses and DCB's are in contiguous memory.
*/
u32 fdma_get_size_contiguous(struct fdma *fdma)
{
return ALIGN(fdma->n_dcbs * sizeof(struct fdma_dcb) +
fdma->n_dcbs * fdma->n_dbs * fdma->db_size,
PAGE_SIZE);
}
EXPORT_SYMBOL_GPL(fdma_get_size_contiguous);
Loading

0 comments on commit ff09bc3

Please sign in to comment.