net:enetc: command BD ring data memory alloc as one function alone
authorPo Liu <po.liu@nxp.com>
Wed, 9 Feb 2022 12:33:02 +0000 (20:33 +0800)
committerDavid S. Miller <davem@davemloft.net>
Wed, 9 Feb 2022 13:23:25 +0000 (13:23 +0000)
Separate the CBDR data memory alloc standalone. It is convenient for
other part loading, for example the ENETC QOS part.

Reported-and-suggested-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Signed-off-by: Po Liu <po.liu@nxp.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/freescale/enetc/enetc.h
drivers/net/ethernet/freescale/enetc/enetc_cbdr.c

index fb39e40..68d806d 100644 (file)
@@ -18,6 +18,8 @@
 #define ENETC_MAX_MTU          (ENETC_MAC_MAXFRM_SIZE - \
                                (ETH_FCS_LEN + ETH_HLEN + VLAN_HLEN))
 
+#define ENETC_CBD_DATA_MEM_ALIGN 64
+
 struct enetc_tx_swbd {
        union {
                struct sk_buff *skb;
@@ -415,6 +417,42 @@ int enetc_get_rss_table(struct enetc_si *si, u32 *table, int count);
 int enetc_set_rss_table(struct enetc_si *si, const u32 *table, int count);
 int enetc_send_cmd(struct enetc_si *si, struct enetc_cbd *cbd);
 
+static inline void *enetc_cbd_alloc_data_mem(struct enetc_si *si,
+                                            struct enetc_cbd *cbd,
+                                            int size, dma_addr_t *dma,
+                                            void **data_align)
+{
+       struct enetc_cbdr *ring = &si->cbd_ring;
+       dma_addr_t dma_align;
+       void *data;
+
+       data = dma_alloc_coherent(ring->dma_dev,
+                                 size + ENETC_CBD_DATA_MEM_ALIGN,
+                                 dma, GFP_KERNEL);
+       if (!data) {
+               dev_err(ring->dma_dev, "CBD alloc data memory failed!\n");
+               return NULL;
+       }
+
+       dma_align = ALIGN(*dma, ENETC_CBD_DATA_MEM_ALIGN);
+       *data_align = PTR_ALIGN(data, ENETC_CBD_DATA_MEM_ALIGN);
+
+       cbd->addr[0] = cpu_to_le32(lower_32_bits(dma_align));
+       cbd->addr[1] = cpu_to_le32(upper_32_bits(dma_align));
+       cbd->length = cpu_to_le16(size);
+
+       return data;
+}
+
+static inline void enetc_cbd_free_data_mem(struct enetc_si *si, int size,
+                                          void *data, dma_addr_t *dma)
+{
+       struct enetc_cbdr *ring = &si->cbd_ring;
+
+       dma_free_coherent(ring->dma_dev, size + ENETC_CBD_DATA_MEM_ALIGN,
+                         data, *dma);
+}
+
 #ifdef CONFIG_FSL_ENETC_QOS
 int enetc_setup_tc_taprio(struct net_device *ndev, void *type_data);
 void enetc_sched_speed_set(struct enetc_ndev_priv *priv, int speed);
index 073e56d..af68dc4 100644 (file)
@@ -166,70 +166,55 @@ int enetc_set_mac_flt_entry(struct enetc_si *si, int index,
        return enetc_send_cmd(si, &cbd);
 }
 
-#define RFSE_ALIGN     64
 /* Set entry in RFS table */
 int enetc_set_fs_entry(struct enetc_si *si, struct enetc_cmd_rfse *rfse,
                       int index)
 {
        struct enetc_cbdr *ring = &si->cbd_ring;
        struct enetc_cbd cbd = {.cmd = 0};
-       dma_addr_t dma, dma_align;
        void *tmp, *tmp_align;
+       dma_addr_t dma;
        int err;
 
        /* fill up the "set" descriptor */
        cbd.cmd = 0;
        cbd.cls = 4;
        cbd.index = cpu_to_le16(index);
-       cbd.length = cpu_to_le16(sizeof(*rfse));
        cbd.opt[3] = cpu_to_le32(0); /* SI */
 
-       tmp = dma_alloc_coherent(ring->dma_dev, sizeof(*rfse) + RFSE_ALIGN,
-                                &dma, GFP_KERNEL);
-       if (!tmp) {
-               dev_err(ring->dma_dev, "DMA mapping of RFS entry failed!\n");
+       tmp = enetc_cbd_alloc_data_mem(si, &cbd, sizeof(*rfse),
+                                      &dma, &tmp_align);
+       if (!tmp)
                return -ENOMEM;
-       }
 
-       dma_align = ALIGN(dma, RFSE_ALIGN);
-       tmp_align = PTR_ALIGN(tmp, RFSE_ALIGN);
        memcpy(tmp_align, rfse, sizeof(*rfse));
 
-       cbd.addr[0] = cpu_to_le32(lower_32_bits(dma_align));
-       cbd.addr[1] = cpu_to_le32(upper_32_bits(dma_align));
-
        err = enetc_send_cmd(si, &cbd);
        if (err)
                dev_err(ring->dma_dev, "FS entry add failed (%d)!", err);
 
-       dma_free_coherent(ring->dma_dev, sizeof(*rfse) + RFSE_ALIGN,
-                         tmp, dma);
+       enetc_cbd_free_data_mem(si, sizeof(*rfse), tmp, &dma);
 
        return err;
 }
 
-#define RSSE_ALIGN     64
 static int enetc_cmd_rss_table(struct enetc_si *si, u32 *table, int count,
                               bool read)
 {
        struct enetc_cbdr *ring = &si->cbd_ring;
        struct enetc_cbd cbd = {.cmd = 0};
-       dma_addr_t dma, dma_align;
        u8 *tmp, *tmp_align;
+       dma_addr_t dma;
        int err, i;
 
-       if (count < RSSE_ALIGN)
+       if (count < ENETC_CBD_DATA_MEM_ALIGN)
                /* HW only takes in a full 64 entry table */
                return -EINVAL;
 
-       tmp = dma_alloc_coherent(ring->dma_dev, count + RSSE_ALIGN,
-                                &dma, GFP_KERNEL);
-       if (!tmp) {
-               dev_err(ring->dma_dev, "DMA mapping of RSS table failed!\n");
+       tmp = enetc_cbd_alloc_data_mem(si, &cbd, count,
+                                      &dma, (void *)&tmp_align);
+       if (!tmp)
                return -ENOMEM;
-       }
-       dma_align = ALIGN(dma, RSSE_ALIGN);
-       tmp_align = PTR_ALIGN(tmp, RSSE_ALIGN);
 
        if (!read)
                for (i = 0; i < count; i++)
@@ -238,10 +223,6 @@ static int enetc_cmd_rss_table(struct enetc_si *si, u32 *table, int count,
        /* fill up the descriptor */
        cbd.cmd = read ? 2 : 1;
        cbd.cls = 3;
-       cbd.length = cpu_to_le16(count);
-
-       cbd.addr[0] = cpu_to_le32(lower_32_bits(dma_align));
-       cbd.addr[1] = cpu_to_le32(upper_32_bits(dma_align));
 
        err = enetc_send_cmd(si, &cbd);
        if (err)
@@ -251,7 +232,7 @@ static int enetc_cmd_rss_table(struct enetc_si *si, u32 *table, int count,
                for (i = 0; i < count; i++)
                        table[i] = tmp_align[i];
 
-       dma_free_coherent(ring->dma_dev, count + RSSE_ALIGN, tmp, dma);
+       enetc_cbd_free_data_mem(si, count, tmp, &dma);
 
        return err;
 }