1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2019 Marvell International Ltd.
5 #include <rte_ethdev_pci.h>
7 #include <rte_malloc.h>
9 #include "otx2_ethdev.h"
12 otx2_eth_set_rx_function(struct rte_eth_dev *eth_dev)
14 RTE_SET_USED(eth_dev);
18 otx2_eth_set_tx_function(struct rte_eth_dev *eth_dev)
20 RTE_SET_USED(eth_dev);
23 static inline uint64_t
24 nix_get_rx_offload_capa(struct otx2_eth_dev *dev)
26 uint64_t capa = NIX_RX_OFFLOAD_CAPA;
28 if (otx2_dev_is_vf(dev))
29 capa &= ~DEV_RX_OFFLOAD_TIMESTAMP;
34 static inline uint64_t
35 nix_get_tx_offload_capa(struct otx2_eth_dev *dev)
39 return NIX_TX_OFFLOAD_CAPA;
43 nix_lf_free(struct otx2_eth_dev *dev)
45 struct otx2_mbox *mbox = dev->mbox;
46 struct nix_lf_free_req *req;
47 struct ndc_sync_op *ndc_req;
50 /* Sync NDC-NIX for LF */
51 ndc_req = otx2_mbox_alloc_msg_ndc_sync_op(mbox);
52 ndc_req->nix_lf_tx_sync = 1;
53 ndc_req->nix_lf_rx_sync = 1;
54 rc = otx2_mbox_process(mbox);
56 otx2_err("Error on NDC-NIX-[TX, RX] LF sync, rc %d", rc);
58 req = otx2_mbox_alloc_msg_nix_lf_free(mbox);
59 /* Let AF driver free all this nix lf's
60 * NPC entries allocated using NPC MBOX.
64 return otx2_mbox_process(mbox);
68 nix_lf_attach(struct otx2_eth_dev *dev)
70 struct otx2_mbox *mbox = dev->mbox;
71 struct rsrc_attach_req *req;
74 req = otx2_mbox_alloc_msg_attach_resources(mbox);
78 return otx2_mbox_process(mbox);
82 nix_lf_get_msix_offset(struct otx2_eth_dev *dev)
84 struct otx2_mbox *mbox = dev->mbox;
85 struct msix_offset_rsp *msix_rsp;
88 /* Get NPA and NIX MSIX vector offsets */
89 otx2_mbox_alloc_msg_msix_offset(mbox);
91 rc = otx2_mbox_process_msg(mbox, (void *)&msix_rsp);
93 dev->nix_msixoff = msix_rsp->nix_msixoff;
99 otx2_eth_dev_lf_detach(struct otx2_mbox *mbox)
101 struct rsrc_detach_req *req;
103 req = otx2_mbox_alloc_msg_detach_resources(mbox);
105 /* Detach all except npa lf */
113 return otx2_mbox_process(mbox);
117 otx2_eth_dev_init(struct rte_eth_dev *eth_dev)
119 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
120 struct rte_pci_device *pci_dev;
123 /* For secondary processes, the primary has done all the work */
124 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
125 /* Setup callbacks for secondary process */
126 otx2_eth_set_tx_function(eth_dev);
127 otx2_eth_set_rx_function(eth_dev);
131 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
133 rte_eth_copy_pci_info(eth_dev, pci_dev);
134 eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
136 /* Zero out everything after OTX2_DEV to allow proper dev_reset() */
137 memset(&dev->otx2_eth_dev_data_start, 0, sizeof(*dev) -
138 offsetof(struct otx2_eth_dev, otx2_eth_dev_data_start));
140 if (!dev->mbox_active) {
141 /* Initialize the base otx2_dev object
142 * only if already present
144 rc = otx2_dev_init(pci_dev, dev);
146 otx2_err("Failed to initialize otx2_dev rc=%d", rc);
151 /* Grab the NPA LF if required */
152 rc = otx2_npa_lf_init(pci_dev, dev);
154 goto otx2_dev_uninit;
157 dev->drv_inited = true;
158 dev->base = dev->bar2 + (RVU_BLOCK_ADDR_NIX0 << 20);
159 dev->lmt_addr = dev->bar2 + (RVU_BLOCK_ADDR_LMT << 20);
162 rc = nix_lf_attach(dev);
164 goto otx2_npa_uninit;
166 /* Get NIX MSIX offset */
167 rc = nix_lf_get_msix_offset(dev);
169 goto otx2_npa_uninit;
171 /* Get maximum number of supported MAC entries */
172 max_entries = otx2_cgx_mac_max_entries_get(dev);
173 if (max_entries < 0) {
174 otx2_err("Failed to get max entries for mac addr");
179 /* For VFs, returned max_entries will be 0. But to keep default MAC
180 * address, one entry must be allocated. So setting up to 1.
182 if (max_entries == 0)
185 eth_dev->data->mac_addrs = rte_zmalloc("mac_addr", max_entries *
186 RTE_ETHER_ADDR_LEN, 0);
187 if (eth_dev->data->mac_addrs == NULL) {
188 otx2_err("Failed to allocate memory for mac addr");
193 dev->max_mac_entries = max_entries;
195 rc = otx2_nix_mac_addr_get(eth_dev, dev->mac_addr);
199 /* Update the mac address */
200 memcpy(eth_dev->data->mac_addrs, dev->mac_addr, RTE_ETHER_ADDR_LEN);
202 /* Also sync same MAC address to CGX table */
203 otx2_cgx_mac_addr_set(eth_dev, ð_dev->data->mac_addrs[0]);
205 dev->tx_offload_capa = nix_get_tx_offload_capa(dev);
206 dev->rx_offload_capa = nix_get_rx_offload_capa(dev);
208 if (otx2_dev_is_A0(dev)) {
209 dev->hwcap |= OTX2_FIXUP_F_MIN_4K_Q;
210 dev->hwcap |= OTX2_FIXUP_F_LIMIT_CQ_FULL;
213 otx2_nix_dbg("Port=%d pf=%d vf=%d ver=%s msix_off=%d hwcap=0x%" PRIx64
214 " rxoffload_capa=0x%" PRIx64 " txoffload_capa=0x%" PRIx64,
215 eth_dev->data->port_id, dev->pf, dev->vf,
216 OTX2_ETH_DEV_PMD_VERSION, dev->nix_msixoff, dev->hwcap,
217 dev->rx_offload_capa, dev->tx_offload_capa);
221 rte_free(eth_dev->data->mac_addrs);
223 otx2_eth_dev_lf_detach(dev->mbox);
227 otx2_dev_fini(pci_dev, dev);
229 otx2_err("Failed to init nix eth_dev rc=%d", rc);
234 otx2_eth_dev_uninit(struct rte_eth_dev *eth_dev, bool mbox_close)
236 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
237 struct rte_pci_device *pci_dev;
240 /* Nothing to be done for secondary processes */
241 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
244 rc = nix_lf_free(dev);
246 otx2_err("Failed to free nix lf, rc=%d", rc);
248 rc = otx2_npa_lf_fini();
250 otx2_err("Failed to cleanup npa lf, rc=%d", rc);
252 rte_free(eth_dev->data->mac_addrs);
253 eth_dev->data->mac_addrs = NULL;
254 dev->drv_inited = false;
256 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
258 rc = otx2_eth_dev_lf_detach(dev->mbox);
260 otx2_err("Failed to detach resources, rc=%d", rc);
262 /* Check if mbox close is needed */
266 if (otx2_npa_lf_active(dev) || otx2_dev_active_vfs(dev)) {
267 /* Will be freed later by PMD */
268 eth_dev->data->dev_private = NULL;
272 otx2_dev_fini(pci_dev, dev);
277 nix_remove(struct rte_pci_device *pci_dev)
279 struct rte_eth_dev *eth_dev;
280 struct otx2_idev_cfg *idev;
281 struct otx2_dev *otx2_dev;
284 eth_dev = rte_eth_dev_allocated(pci_dev->device.name);
286 /* Cleanup eth dev */
287 rc = otx2_eth_dev_uninit(eth_dev, true);
291 rte_eth_dev_pci_release(eth_dev);
294 /* Nothing to be done for secondary processes */
295 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
298 /* Check for common resources */
299 idev = otx2_intra_dev_get_cfg();
300 if (!idev || !idev->npa_lf || idev->npa_lf->pci_dev != pci_dev)
303 otx2_dev = container_of(idev->npa_lf, struct otx2_dev, npalf);
305 if (otx2_npa_lf_active(otx2_dev) || otx2_dev_active_vfs(otx2_dev))
308 /* Safe to cleanup mbox as no more users */
309 otx2_dev_fini(pci_dev, otx2_dev);
314 otx2_info("%s: common resource in use by other devices", pci_dev->name);
319 nix_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
323 RTE_SET_USED(pci_drv);
325 rc = rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct otx2_eth_dev),
328 /* On error on secondary, recheck if port exists in primary or
329 * in mid of detach state.
331 if (rte_eal_process_type() != RTE_PROC_PRIMARY && rc)
332 if (!rte_eth_dev_allocated(pci_dev->device.name))
337 static const struct rte_pci_id pci_nix_map[] = {
339 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_PF)
342 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_VF)
345 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
346 PCI_DEVID_OCTEONTX2_RVU_AF_VF)
353 static struct rte_pci_driver pci_nix = {
354 .id_table = pci_nix_map,
355 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_IOVA_AS_VA |
356 RTE_PCI_DRV_INTR_LSC,
358 .remove = nix_remove,
361 RTE_PMD_REGISTER_PCI(net_octeontx2, pci_nix);
362 RTE_PMD_REGISTER_PCI_TABLE(net_octeontx2, pci_nix_map);
363 RTE_PMD_REGISTER_KMOD_DEP(net_octeontx2, "vfio-pci");