1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2021 Marvell.
6 #include <rte_bus_pci.h>
7 #include <rte_rawdev.h>
8 #include <rte_rawdev_pmd.h>
12 #include "rte_pmd_bphy.h"
14 struct cnxk_bphy_cgx_queue {
16 /* queue holds up to one response */
20 struct cnxk_bphy_cgx {
21 struct roc_bphy_cgx *rcgx;
22 struct cnxk_bphy_cgx_queue queues[MAX_LMACS_PER_CGX];
23 unsigned int num_queues;
27 cnxk_bphy_cgx_format_name(char *name, unsigned int len,
28 struct rte_pci_device *pci_dev)
30 snprintf(name, len, "BPHY_CGX:%x:%02x.%x", pci_dev->addr.bus,
31 pci_dev->addr.devid, pci_dev->addr.function);
35 cnxk_bphy_cgx_queue_def_conf(struct rte_rawdev *dev, uint16_t queue_id,
36 rte_rawdev_obj_t queue_conf,
37 size_t queue_conf_size)
42 RTE_SET_USED(queue_id);
44 if (queue_conf_size != sizeof(*conf))
47 conf = (unsigned int *)queue_conf;
54 cnxk_bphy_cgx_process_buf(struct cnxk_bphy_cgx *cgx, unsigned int queue,
55 struct rte_rawdev_buf *buf)
57 struct cnxk_bphy_cgx_queue *qp = &cgx->queues[queue];
58 struct cnxk_bphy_cgx_msg_set_link_state *link_state;
59 struct cnxk_bphy_cgx_msg *msg = buf->buf_addr;
60 struct cnxk_bphy_cgx_msg_link_mode *link_mode;
61 struct cnxk_bphy_cgx_msg_link_info *link_info;
62 struct roc_bphy_cgx_link_info rlink_info;
63 struct roc_bphy_cgx_link_mode rlink_mode;
64 unsigned int lmac = qp->lmac;
69 case CNXK_BPHY_CGX_MSG_TYPE_GET_LINKINFO:
70 memset(&rlink_info, 0, sizeof(rlink_info));
71 ret = roc_bphy_cgx_get_linkinfo(cgx->rcgx, lmac, &rlink_info);
75 link_info = rte_zmalloc(NULL, sizeof(*link_info), 0);
79 link_info->link_up = rlink_info.link_up;
80 link_info->full_duplex = rlink_info.full_duplex;
82 (enum cnxk_bphy_cgx_eth_link_speed)rlink_info.speed;
83 link_info->autoneg = rlink_info.an;
85 (enum cnxk_bphy_cgx_eth_link_fec)rlink_info.fec;
87 (enum cnxk_bphy_cgx_eth_link_mode)rlink_info.mode;
90 case CNXK_BPHY_CGX_MSG_TYPE_INTLBK_DISABLE:
91 ret = roc_bphy_cgx_intlbk_disable(cgx->rcgx, lmac);
93 case CNXK_BPHY_CGX_MSG_TYPE_INTLBK_ENABLE:
94 ret = roc_bphy_cgx_intlbk_enable(cgx->rcgx, lmac);
96 case CNXK_BPHY_CGX_MSG_TYPE_PTP_RX_DISABLE:
97 ret = roc_bphy_cgx_ptp_rx_disable(cgx->rcgx, lmac);
99 case CNXK_BPHY_CGX_MSG_TYPE_PTP_RX_ENABLE:
100 ret = roc_bphy_cgx_ptp_rx_enable(cgx->rcgx, lmac);
102 case CNXK_BPHY_CGX_MSG_TYPE_SET_LINK_MODE:
103 link_mode = msg->data;
104 memset(&rlink_mode, 0, sizeof(rlink_mode));
105 rlink_mode.full_duplex = link_mode->full_duplex;
106 rlink_mode.an = link_mode->autoneg;
108 (enum roc_bphy_cgx_eth_link_speed)link_mode->speed;
110 (enum roc_bphy_cgx_eth_link_mode)link_mode->mode;
111 ret = roc_bphy_cgx_set_link_mode(cgx->rcgx, lmac, &rlink_mode);
113 case CNXK_BPHY_CGX_MSG_TYPE_SET_LINK_STATE:
114 link_state = msg->data;
115 ret = roc_bphy_cgx_set_link_state(cgx->rcgx, lmac,
118 case CNXK_BPHY_CGX_MSG_TYPE_START_RXTX:
119 ret = roc_bphy_cgx_start_rxtx(cgx->rcgx, lmac);
121 case CNXK_BPHY_CGX_MSG_TYPE_STOP_RXTX:
122 ret = roc_bphy_cgx_stop_rxtx(cgx->rcgx, lmac);
128 /* get rid of last response if any */
130 RTE_LOG(WARNING, PMD, "Previous response got overwritten\n");
139 cnxk_bphy_cgx_enqueue_bufs(struct rte_rawdev *dev,
140 struct rte_rawdev_buf **buffers, unsigned int count,
141 rte_rawdev_obj_t context)
143 struct cnxk_bphy_cgx *cgx = dev->dev_private;
144 unsigned int queue = (size_t)context;
147 if (queue >= cgx->num_queues)
153 ret = cnxk_bphy_cgx_process_buf(cgx, queue, buffers[0]);
161 cnxk_bphy_cgx_queue_count(struct rte_rawdev *dev)
163 struct cnxk_bphy_cgx *cgx = dev->dev_private;
165 return cgx->num_queues;
168 static const struct rte_rawdev_ops cnxk_bphy_cgx_rawdev_ops = {
169 .queue_def_conf = cnxk_bphy_cgx_queue_def_conf,
170 .enqueue_bufs = cnxk_bphy_cgx_enqueue_bufs,
171 .queue_count = cnxk_bphy_cgx_queue_count,
175 cnxk_bphy_cgx_init_queues(struct cnxk_bphy_cgx *cgx)
177 struct roc_bphy_cgx *rcgx = cgx->rcgx;
180 for (i = 0; i < RTE_DIM(cgx->queues); i++) {
181 if (!(rcgx->lmac_bmap & BIT_ULL(i)))
184 cgx->queues[cgx->num_queues++].lmac = i;
189 cnxk_bphy_cgx_fini_queues(struct cnxk_bphy_cgx *cgx)
193 for (i = 0; i < cgx->num_queues; i++) {
194 if (cgx->queues[i].rsp)
195 rte_free(cgx->queues[i].rsp);
202 cnxk_bphy_cgx_rawdev_probe(struct rte_pci_driver *pci_drv,
203 struct rte_pci_device *pci_dev)
205 char name[RTE_RAWDEV_NAME_MAX_LEN];
206 struct rte_rawdev *rawdev;
207 struct cnxk_bphy_cgx *cgx;
208 struct roc_bphy_cgx *rcgx;
211 RTE_SET_USED(pci_drv);
213 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
216 if (!pci_dev->mem_resource[0].addr)
219 ret = roc_plt_init();
223 cnxk_bphy_cgx_format_name(name, sizeof(name), pci_dev);
224 rawdev = rte_rawdev_pmd_allocate(name, sizeof(*cgx), rte_socket_id());
228 rawdev->dev_ops = &cnxk_bphy_cgx_rawdev_ops;
229 rawdev->device = &pci_dev->device;
230 rawdev->driver_name = pci_dev->driver->driver.name;
232 cgx = rawdev->dev_private;
233 cgx->rcgx = rte_zmalloc(NULL, sizeof(*rcgx), 0);
236 goto out_pmd_release;
240 rcgx->bar0_pa = pci_dev->mem_resource[0].phys_addr;
241 rcgx->bar0_va = pci_dev->mem_resource[0].addr;
242 ret = roc_bphy_cgx_dev_init(rcgx);
246 cnxk_bphy_cgx_init_queues(cgx);
252 rte_rawdev_pmd_release(rawdev);
258 cnxk_bphy_cgx_rawdev_remove(struct rte_pci_device *pci_dev)
260 char name[RTE_RAWDEV_NAME_MAX_LEN];
261 struct rte_rawdev *rawdev;
262 struct cnxk_bphy_cgx *cgx;
264 cnxk_bphy_cgx_format_name(name, sizeof(name), pci_dev);
265 rawdev = rte_rawdev_pmd_get_named_dev(name);
269 cgx = rawdev->dev_private;
270 cnxk_bphy_cgx_fini_queues(cgx);
271 roc_bphy_cgx_dev_fini(cgx->rcgx);
274 return rte_rawdev_pmd_release(rawdev);
277 static const struct rte_pci_id cnxk_bphy_cgx_map[] = {
278 {RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_CN9K_CGX)},
279 {RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_CN10K_RPM)},
283 static struct rte_pci_driver bphy_cgx_rawdev_pmd = {
284 .id_table = cnxk_bphy_cgx_map,
285 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
286 .probe = cnxk_bphy_cgx_rawdev_probe,
287 .remove = cnxk_bphy_cgx_rawdev_remove,
290 RTE_PMD_REGISTER_PCI(cnxk_bphy_cgx_rawdev_pci_driver, bphy_cgx_rawdev_pmd);
291 RTE_PMD_REGISTER_PCI_TABLE(cnxk_bphy_cgx_rawdev_pci_driver, cnxk_bphy_cgx_map);
292 RTE_PMD_REGISTER_KMOD_DEP(cnxk_bphy_cgx_rawdev_pci_driver, "vfio-pci");