Commit 01a70754 authored by Daniel Machon's avatar Daniel Machon Committed by Paolo Abeni

net: lan966x: use the FDMA library for allocation of rx buffers

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: default avatarDaniel Machon <daniel.machon@microchip.com>
Reviewed-by: default avatarHoratiu Vultur <horatiu.vultur@microchip.com>
Signed-off-by: default avatarPaolo Abeni <pabeni@redhat.com>
parent 8274d40e
...@@ -6,13 +6,30 @@ ...@@ -6,13 +6,30 @@
#include "lan966x_main.h" #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) static int lan966x_fdma_channel_active(struct lan966x *lan966x)
{ {
return lan_rd(lan966x, FDMA_CH_ACTIVE); return lan_rd(lan966x, FDMA_CH_ACTIVE);
} }
static struct page *lan966x_fdma_rx_alloc_page(struct lan966x_rx *rx, static struct page *lan966x_fdma_rx_alloc_page(struct lan966x_rx *rx,
struct lan966x_db *db) struct fdma_db *db)
{ {
struct page *page; struct page *page;
...@@ -50,11 +67,11 @@ static void lan966x_fdma_rx_free_page(struct lan966x_rx *rx) ...@@ -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, static void lan966x_fdma_rx_add_dcb(struct lan966x_rx *rx,
struct lan966x_rx_dcb *dcb, struct fdma_dcb *dcb,
u64 nextptr) u64 nextptr)
{ {
struct fdma *fdma = &rx->fdma; struct fdma *fdma = &rx->fdma;
struct lan966x_db *db; struct fdma_db *db;
int i; int i;
for (i = 0; i < fdma->n_dbs; ++i) { for (i = 0; i < fdma->n_dbs; ++i) {
...@@ -65,8 +82,8 @@ static void lan966x_fdma_rx_add_dcb(struct lan966x_rx *rx, ...@@ -65,8 +82,8 @@ static void lan966x_fdma_rx_add_dcb(struct lan966x_rx *rx,
dcb->nextptr = FDMA_DCB_INVALID_DATA; dcb->nextptr = FDMA_DCB_INVALID_DATA;
dcb->info = FDMA_DCB_INFO_DATAL(PAGE_SIZE << rx->page_order); dcb->info = FDMA_DCB_INFO_DATAL(PAGE_SIZE << rx->page_order);
rx->last_entry->nextptr = nextptr; fdma->last_dcb->nextptr = nextptr;
rx->last_entry = dcb; fdma->last_dcb = dcb;
} }
static int lan966x_fdma_rx_alloc_page_pool(struct lan966x_rx *rx) 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) ...@@ -108,45 +125,17 @@ static int lan966x_fdma_rx_alloc(struct lan966x_rx *rx)
{ {
struct lan966x *lan966x = rx->lan966x; struct lan966x *lan966x = rx->lan966x;
struct fdma *fdma = &rx->fdma; struct fdma *fdma = &rx->fdma;
struct lan966x_rx_dcb *dcb; int err;
struct lan966x_db *db;
struct page *page;
int i, j;
int size;
if (lan966x_fdma_rx_alloc_page_pool(rx)) if (lan966x_fdma_rx_alloc_page_pool(rx))
return PTR_ERR(rx->page_pool); return PTR_ERR(rx->page_pool);
/* calculate how many pages are needed to allocate the dcbs */ err = fdma_alloc_coherent(lan966x->dev, fdma);
size = sizeof(struct lan966x_rx_dcb) * fdma->n_dcbs; if (err)
size = ALIGN(size, PAGE_SIZE); return err;
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;
}
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; return 0;
} }
...@@ -168,7 +157,7 @@ static void lan966x_fdma_rx_free(struct lan966x_rx *rx) ...@@ -168,7 +157,7 @@ static void lan966x_fdma_rx_free(struct lan966x_rx *rx)
/* Now it is possible to do the cleanup of dcb */ /* Now it is possible to do the cleanup of dcb */
size = sizeof(struct lan966x_tx_dcb) * fdma->n_dcbs; size = sizeof(struct lan966x_tx_dcb) * fdma->n_dcbs;
size = ALIGN(size, PAGE_SIZE); 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) static void lan966x_fdma_rx_start(struct lan966x_rx *rx)
...@@ -180,9 +169,9 @@ 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 /* When activating a channel, first is required to write the first DCB
* address and then to activate it * 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)); 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)); FDMA_DCB_LLP1(fdma->channel_id));
lan_wr(FDMA_CH_CFG_CH_DCB_DB_CNT_SET(fdma->n_dbs) | 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) ...@@ -297,7 +286,7 @@ static void lan966x_fdma_tx_free(struct lan966x_tx *tx)
size = sizeof(struct lan966x_tx_dcb) * fdma->n_dcbs; size = sizeof(struct lan966x_tx_dcb) * fdma->n_dcbs;
size = ALIGN(size, PAGE_SIZE); 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) 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) ...@@ -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) static bool lan966x_fdma_rx_more_frames(struct lan966x_rx *rx)
{ {
struct fdma *fdma = &rx->fdma; struct fdma *fdma = &rx->fdma;
struct lan966x_db *db; struct fdma_db *db;
/* Check if there is any data */ /* 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))) if (unlikely(!(db->status & FDMA_DCB_STATUS_DONE)))
return false; return false;
...@@ -480,10 +469,10 @@ static int lan966x_fdma_rx_check_frame(struct lan966x_rx *rx, u64 *src_port) ...@@ -480,10 +469,10 @@ static int lan966x_fdma_rx_check_frame(struct lan966x_rx *rx, u64 *src_port)
struct lan966x *lan966x = rx->lan966x; struct lan966x *lan966x = rx->lan966x;
struct fdma *fdma = &rx->fdma; struct fdma *fdma = &rx->fdma;
struct lan966x_port *port; struct lan966x_port *port;
struct lan966x_db *db; struct fdma_db *db;
struct page *page; 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]; page = rx->page[fdma->dcb_index][fdma->db_index];
if (unlikely(!page)) if (unlikely(!page))
return FDMA_ERROR; return FDMA_ERROR;
...@@ -510,16 +499,16 @@ static struct sk_buff *lan966x_fdma_rx_get_frame(struct lan966x_rx *rx, ...@@ -510,16 +499,16 @@ static struct sk_buff *lan966x_fdma_rx_get_frame(struct lan966x_rx *rx,
{ {
struct lan966x *lan966x = rx->lan966x; struct lan966x *lan966x = rx->lan966x;
struct fdma *fdma = &rx->fdma; struct fdma *fdma = &rx->fdma;
struct lan966x_db *db;
struct sk_buff *skb; struct sk_buff *skb;
struct fdma_db *db;
struct page *page; struct page *page;
u64 timestamp; u64 timestamp;
/* Get the received frame and unmap it */ /* 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]; 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)) if (unlikely(!skb))
goto free_page; goto free_page;
...@@ -562,12 +551,12 @@ static int lan966x_fdma_napi_poll(struct napi_struct *napi, int weight) ...@@ -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 *lan966x = container_of(napi, struct lan966x, napi);
struct lan966x_rx *rx = &lan966x->rx; struct lan966x_rx *rx = &lan966x->rx;
struct lan966x_rx_dcb *old_dcb;
struct fdma *fdma = &rx->fdma; struct fdma *fdma = &rx->fdma;
int dcb_reload, counter = 0; int dcb_reload, counter = 0;
struct lan966x_db *db; struct fdma_dcb *old_dcb;
bool redirect = false; bool redirect = false;
struct sk_buff *skb; struct sk_buff *skb;
struct fdma_db *db;
struct page *page; struct page *page;
u64 src_port; u64 src_port;
u64 nextptr; u64 nextptr;
...@@ -613,18 +602,18 @@ static int lan966x_fdma_napi_poll(struct napi_struct *napi, int weight) ...@@ -613,18 +602,18 @@ static int lan966x_fdma_napi_poll(struct napi_struct *napi, int weight)
allocate_new: allocate_new:
/* Allocate new pages and map them */ /* Allocate new pages and map them */
while (dcb_reload != fdma->dcb_index) { 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); page = lan966x_fdma_rx_alloc_page(rx, db);
if (unlikely(!page)) if (unlikely(!page))
break; break;
rx->page[dcb_reload][fdma->db_index] = page; rx->page[dcb_reload][fdma->db_index] = page;
old_dcb = &rx->dcbs[dcb_reload]; old_dcb = &fdma->dcbs[dcb_reload];
dcb_reload++; dcb_reload++;
dcb_reload &= fdma->n_dcbs - 1; dcb_reload &= fdma->n_dcbs - 1;
nextptr = rx->dma + ((unsigned long)old_dcb - nextptr = fdma->dma + ((unsigned long)old_dcb -
(unsigned long)rx->dcbs); (unsigned long)fdma->dcbs);
lan966x_fdma_rx_add_dcb(rx, old_dcb, nextptr); lan966x_fdma_rx_add_dcb(rx, old_dcb, nextptr);
lan966x_fdma_rx_reload(rx); lan966x_fdma_rx_reload(rx);
} }
...@@ -933,8 +922,8 @@ static int lan966x_fdma_reload(struct lan966x *lan966x, int new_mtu) ...@@ -933,8 +922,8 @@ static int lan966x_fdma_reload(struct lan966x *lan966x, int new_mtu)
int err; int err;
/* Store these for later to free them */ /* Store these for later to free them */
rx_dma = lan966x->rx.dma; rx_dma = lan966x->rx.fdma.dma;
rx_dcbs = lan966x->rx.dcbs; rx_dcbs = lan966x->rx.fdma.dcbs;
page_pool = lan966x->rx.page_pool; page_pool = lan966x->rx.page_pool;
napi_synchronize(&lan966x->napi); napi_synchronize(&lan966x->napi);
...@@ -950,7 +939,7 @@ static int lan966x_fdma_reload(struct lan966x *lan966x, int new_mtu) ...@@ -950,7 +939,7 @@ static int lan966x_fdma_reload(struct lan966x *lan966x, int new_mtu)
goto restore; goto restore;
lan966x_fdma_rx_start(&lan966x->rx); 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); size = ALIGN(size, PAGE_SIZE);
dma_free_coherent(lan966x->dev, size, rx_dcbs, rx_dma); 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) ...@@ -962,8 +951,8 @@ static int lan966x_fdma_reload(struct lan966x *lan966x, int new_mtu)
return err; return err;
restore: restore:
lan966x->rx.page_pool = page_pool; lan966x->rx.page_pool = page_pool;
lan966x->rx.dma = rx_dma; lan966x->rx.fdma.dma = rx_dma;
lan966x->rx.dcbs = rx_dcbs; lan966x->rx.fdma.dcbs = rx_dcbs;
lan966x_fdma_rx_start(&lan966x->rx); lan966x_fdma_rx_start(&lan966x->rx);
return err; return err;
...@@ -1056,6 +1045,11 @@ int lan966x_fdma_init(struct lan966x *lan966x) ...@@ -1056,6 +1045,11 @@ int lan966x_fdma_init(struct lan966x *lan966x)
lan966x->rx.fdma.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_dcbs = FDMA_DCB_MAX;
lan966x->rx.fdma.n_dbs = FDMA_RX_DCB_MAX_DBS; 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->rx.max_mtu = lan966x_fdma_get_max_frame(lan966x);
lan966x->tx.lan966x = lan966x; lan966x->tx.lan966x = lan966x;
lan966x->tx.fdma.channel_id = FDMA_INJ_CHANNEL; lan966x->tx.fdma.channel_id = FDMA_INJ_CHANNEL;
......
...@@ -196,12 +196,6 @@ struct lan966x_db { ...@@ -196,12 +196,6 @@ struct lan966x_db {
u64 status; u64 status;
}; };
struct lan966x_rx_dcb {
u64 nextptr;
u64 info;
struct lan966x_db db[FDMA_RX_DCB_MAX_DBS];
};
struct lan966x_tx_dcb { struct lan966x_tx_dcb {
u64 nextptr; u64 nextptr;
u64 info; u64 info;
...@@ -213,18 +207,9 @@ struct lan966x_rx { ...@@ -213,18 +207,9 @@ struct lan966x_rx {
struct fdma fdma; 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 */ /* For each DB, there is a page */
struct page *page[FDMA_DCB_MAX][FDMA_RX_DCB_MAX_DBS]; 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 /* 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. * RX buffers. This value is calculated based on max MTU of the devices.
*/ */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment