dd4d74ca05bd710c9acd09faabac5dfe13b0e7ec
[dpdk.git] / drivers / mempool / cnxk / cnxk_mempool.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #include <rte_atomic.h>
6 #include <rte_bus_pci.h>
7 #include <rte_common.h>
8 #include <rte_devargs.h>
9 #include <rte_eal.h>
10 #include <rte_io.h>
11 #include <rte_kvargs.h>
12 #include <rte_malloc.h>
13 #include <rte_mbuf_pool_ops.h>
14 #include <rte_pci.h>
15
16 #include "roc_api.h"
17
18 #define CNXK_NPA_DEV_NAME        RTE_STR(cnxk_npa_dev_)
19 #define CNXK_NPA_DEV_NAME_LEN    (sizeof(CNXK_NPA_DEV_NAME) + PCI_PRI_STR_SIZE)
20 #define CNXK_NPA_MAX_POOLS_PARAM "max_pools"
21
22 static inline uint32_t
23 npa_aura_size_to_u32(uint8_t val)
24 {
25         if (val == NPA_AURA_SZ_0)
26                 return 128;
27         if (val >= NPA_AURA_SZ_MAX)
28                 return BIT_ULL(20);
29
30         return 1 << (val + 6);
31 }
32
33 static int
34 parse_max_pools(const char *key, const char *value, void *extra_args)
35 {
36         RTE_SET_USED(key);
37         uint32_t val;
38
39         val = atoi(value);
40         if (val < npa_aura_size_to_u32(NPA_AURA_SZ_128))
41                 val = 128;
42         if (val > npa_aura_size_to_u32(NPA_AURA_SZ_1M))
43                 val = BIT_ULL(20);
44
45         *(uint8_t *)extra_args = rte_log2_u32(val) - 6;
46         return 0;
47 }
48
49 static inline uint8_t
50 parse_aura_size(struct rte_devargs *devargs)
51 {
52         uint8_t aura_sz = NPA_AURA_SZ_128;
53         struct rte_kvargs *kvlist;
54
55         if (devargs == NULL)
56                 goto exit;
57         kvlist = rte_kvargs_parse(devargs->args, NULL);
58         if (kvlist == NULL)
59                 goto exit;
60
61         rte_kvargs_process(kvlist, CNXK_NPA_MAX_POOLS_PARAM, &parse_max_pools,
62                            &aura_sz);
63         rte_kvargs_free(kvlist);
64 exit:
65         return aura_sz;
66 }
67
68 static inline char *
69 npa_dev_to_name(struct rte_pci_device *pci_dev, char *name)
70 {
71         snprintf(name, CNXK_NPA_DEV_NAME_LEN, CNXK_NPA_DEV_NAME PCI_PRI_FMT,
72                  pci_dev->addr.domain, pci_dev->addr.bus, pci_dev->addr.devid,
73                  pci_dev->addr.function);
74
75         return name;
76 }
77
78 static int
79 npa_init(struct rte_pci_device *pci_dev)
80 {
81         char name[CNXK_NPA_DEV_NAME_LEN];
82         const struct rte_memzone *mz;
83         struct roc_npa *dev;
84         int rc = -ENOMEM;
85
86         mz = rte_memzone_reserve_aligned(npa_dev_to_name(pci_dev, name),
87                                          sizeof(*dev), SOCKET_ID_ANY, 0,
88                                          RTE_CACHE_LINE_SIZE);
89         if (mz == NULL)
90                 goto error;
91
92         dev = mz->addr;
93         dev->pci_dev = pci_dev;
94
95         roc_idev_npa_maxpools_set(parse_aura_size(pci_dev->device.devargs));
96         rc = roc_npa_dev_init(dev);
97         if (rc)
98                 goto mz_free;
99
100         return 0;
101
102 mz_free:
103         rte_memzone_free(mz);
104 error:
105         plt_err("failed to initialize npa device rc=%d", rc);
106         return rc;
107 }
108
109 static int
110 npa_fini(struct rte_pci_device *pci_dev)
111 {
112         char name[CNXK_NPA_DEV_NAME_LEN];
113         const struct rte_memzone *mz;
114         int rc;
115
116         mz = rte_memzone_lookup(npa_dev_to_name(pci_dev, name));
117         if (mz == NULL)
118                 return -EINVAL;
119
120         rc = roc_npa_dev_fini(mz->addr);
121         if (rc) {
122                 if (rc != -EAGAIN)
123                         plt_err("Failed to remove npa dev, rc=%d", rc);
124                 return rc;
125         }
126         rte_memzone_free(mz);
127
128         return 0;
129 }
130
131 static int
132 npa_remove(struct rte_pci_device *pci_dev)
133 {
134         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
135                 return 0;
136
137         return npa_fini(pci_dev);
138 }
139
140 static int
141 npa_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
142 {
143         int rc;
144
145         RTE_SET_USED(pci_drv);
146
147         rc = roc_plt_init();
148         if (rc < 0)
149                 return rc;
150
151         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
152                 return 0;
153
154         return npa_init(pci_dev);
155 }
156
157 static const struct rte_pci_id npa_pci_map[] = {
158         {
159                 .class_id = RTE_CLASS_ANY_ID,
160                 .vendor_id = PCI_VENDOR_ID_CAVIUM,
161                 .device_id = PCI_DEVID_CNXK_RVU_NPA_PF,
162                 .subsystem_vendor_id = PCI_VENDOR_ID_CAVIUM,
163                 .subsystem_device_id = PCI_SUBSYSTEM_DEVID_CN10KA,
164         },
165         {
166                 .class_id = RTE_CLASS_ANY_ID,
167                 .vendor_id = PCI_VENDOR_ID_CAVIUM,
168                 .device_id = PCI_DEVID_CNXK_RVU_NPA_PF,
169                 .subsystem_vendor_id = PCI_VENDOR_ID_CAVIUM,
170                 .subsystem_device_id = PCI_SUBSYSTEM_DEVID_CN10KAS,
171         },
172         {
173                 .class_id = RTE_CLASS_ANY_ID,
174                 .vendor_id = PCI_VENDOR_ID_CAVIUM,
175                 .device_id = PCI_DEVID_CNXK_RVU_NPA_VF,
176                 .subsystem_vendor_id = PCI_VENDOR_ID_CAVIUM,
177                 .subsystem_device_id = PCI_SUBSYSTEM_DEVID_CN10KA,
178         },
179         {
180                 .class_id = RTE_CLASS_ANY_ID,
181                 .vendor_id = PCI_VENDOR_ID_CAVIUM,
182                 .device_id = PCI_DEVID_CNXK_RVU_NPA_VF,
183                 .subsystem_vendor_id = PCI_VENDOR_ID_CAVIUM,
184                 .subsystem_device_id = PCI_SUBSYSTEM_DEVID_CN10KAS,
185         },
186         {
187                 .vendor_id = 0,
188         },
189 };
190
191 static struct rte_pci_driver npa_pci = {
192         .id_table = npa_pci_map,
193         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_NEED_IOVA_AS_VA,
194         .probe = npa_probe,
195         .remove = npa_remove,
196 };
197
198 RTE_PMD_REGISTER_PCI(mempool_cnxk, npa_pci);
199 RTE_PMD_REGISTER_PCI_TABLE(mempool_cnxk, npa_pci_map);
200 RTE_PMD_REGISTER_KMOD_DEP(mempool_cnxk, "vfio-pci");
201 RTE_PMD_REGISTER_PARAM_STRING(mempool_cnxk,
202                               CNXK_NPA_MAX_POOLS_PARAM "=<128-1048576>");