xref: /dpdk/drivers/net/octeontx/base/octeontx_pkivf.c (revision 91c0c9789c44bd478bfad6fc1b134741eab98987)
1aaf4363eSJerin Jacob /* SPDX-License-Identifier: BSD-3-Clause
2aaf4363eSJerin Jacob  * Copyright(c) 2017 Cavium, Inc
3445371e8SJerin Jacob  */
4aaf4363eSJerin Jacob 
5445371e8SJerin Jacob #include <string.h>
6445371e8SJerin Jacob 
7445371e8SJerin Jacob #include <rte_eal.h>
8c752998bSGaetan Rivet #include <rte_bus_pci.h>
9445371e8SJerin Jacob 
10a6d6f0afSPavan Nikhilesh #include "../octeontx_logs.h"
11a6d6f0afSPavan Nikhilesh #include "octeontx_io.h"
12b95c3413SJerin Jacob #include "octeontx_pkivf.h"
13b95c3413SJerin Jacob 
14a6d6f0afSPavan Nikhilesh 
15a6d6f0afSPavan Nikhilesh struct octeontx_pkivf {
16a6d6f0afSPavan Nikhilesh 	uint8_t		*bar0;
17a6d6f0afSPavan Nikhilesh 	uint8_t		status;
18a6d6f0afSPavan Nikhilesh 	uint16_t	domain;
19a6d6f0afSPavan Nikhilesh 	uint16_t	vfid;
20a6d6f0afSPavan Nikhilesh };
21a6d6f0afSPavan Nikhilesh 
22a6d6f0afSPavan Nikhilesh struct octeontx_pki_vf_ctl_s {
23a6d6f0afSPavan Nikhilesh 	struct octeontx_pkivf pki[PKI_VF_MAX];
24a6d6f0afSPavan Nikhilesh };
25a6d6f0afSPavan Nikhilesh 
26a6d6f0afSPavan Nikhilesh static struct octeontx_pki_vf_ctl_s pki_vf_ctl;
27a6d6f0afSPavan Nikhilesh 
28b95c3413SJerin Jacob int
29b95c3413SJerin Jacob octeontx_pki_port_open(int port)
30b95c3413SJerin Jacob {
31a6d6f0afSPavan Nikhilesh 	uint16_t global_domain = octeontx_get_global_domain();
32b95c3413SJerin Jacob 	struct octeontx_mbox_hdr hdr;
33*91c0c978SPavan Nikhilesh 	pki_port_type_t port_type;
34a6d6f0afSPavan Nikhilesh 	int i, res;
35a6d6f0afSPavan Nikhilesh 
36a6d6f0afSPavan Nikhilesh 	/* Check if atleast one PKI vf is in application domain. */
37a6d6f0afSPavan Nikhilesh 	for (i = 0; i < PKI_VF_MAX; i++) {
38a6d6f0afSPavan Nikhilesh 		if (pki_vf_ctl.pki[i].domain != global_domain)
39a6d6f0afSPavan Nikhilesh 			continue;
40a6d6f0afSPavan Nikhilesh 		break;
41a6d6f0afSPavan Nikhilesh 	}
42a6d6f0afSPavan Nikhilesh 
43a6d6f0afSPavan Nikhilesh 	if (i == PKI_VF_MAX)
44a6d6f0afSPavan Nikhilesh 		return -ENODEV;
45b95c3413SJerin Jacob 
46*91c0c978SPavan Nikhilesh 	port_type.port_type = OCTTX_PORT_TYPE_NET;
47b95c3413SJerin Jacob 	hdr.coproc = OCTEONTX_PKI_COPROC;
48b95c3413SJerin Jacob 	hdr.msg = MBOX_PKI_PORT_OPEN;
49b95c3413SJerin Jacob 	hdr.vfid = port;
50b95c3413SJerin Jacob 
51*91c0c978SPavan Nikhilesh 	res = octeontx_mbox_send(&hdr, &port_type, sizeof(pki_port_type_t),
52a6d6f0afSPavan Nikhilesh 				 NULL, 0);
53b95c3413SJerin Jacob 	if (res < 0)
54b95c3413SJerin Jacob 		return -EACCES;
55b95c3413SJerin Jacob 	return res;
56b95c3413SJerin Jacob }
57b95c3413SJerin Jacob 
58b95c3413SJerin Jacob int
596d28968eSJerin Jacob octeontx_pki_port_hash_config(int port, pki_hash_cfg_t *hash_cfg)
606d28968eSJerin Jacob {
616d28968eSJerin Jacob 	struct octeontx_mbox_hdr hdr;
626d28968eSJerin Jacob 	int res;
636d28968eSJerin Jacob 
64*91c0c978SPavan Nikhilesh 	pki_hash_cfg_t h_cfg = *(pki_hash_cfg_t *)hash_cfg;
65*91c0c978SPavan Nikhilesh 	int len = sizeof(pki_hash_cfg_t);
666d28968eSJerin Jacob 
676d28968eSJerin Jacob 	hdr.coproc = OCTEONTX_PKI_COPROC;
686d28968eSJerin Jacob 	hdr.msg = MBOX_PKI_PORT_HASH_CONFIG;
696d28968eSJerin Jacob 	hdr.vfid = port;
706d28968eSJerin Jacob 
71d8dd3165SPavan Nikhilesh 	res = octeontx_mbox_send(&hdr, &h_cfg, len, NULL, 0);
726d28968eSJerin Jacob 	if (res < 0)
736d28968eSJerin Jacob 		return -EACCES;
746d28968eSJerin Jacob 
756d28968eSJerin Jacob 	return res;
766d28968eSJerin Jacob }
776d28968eSJerin Jacob 
786d28968eSJerin Jacob int
796d28968eSJerin Jacob octeontx_pki_port_pktbuf_config(int port, pki_pktbuf_cfg_t *buf_cfg)
806d28968eSJerin Jacob {
816d28968eSJerin Jacob 	struct octeontx_mbox_hdr hdr;
826d28968eSJerin Jacob 	int res;
836d28968eSJerin Jacob 
84*91c0c978SPavan Nikhilesh 	pki_pktbuf_cfg_t b_cfg = *(pki_pktbuf_cfg_t *)buf_cfg;
85*91c0c978SPavan Nikhilesh 	int len = sizeof(pki_pktbuf_cfg_t);
866d28968eSJerin Jacob 
876d28968eSJerin Jacob 	hdr.coproc = OCTEONTX_PKI_COPROC;
886d28968eSJerin Jacob 	hdr.msg = MBOX_PKI_PORT_PKTBUF_CONFIG;
896d28968eSJerin Jacob 	hdr.vfid = port;
906d28968eSJerin Jacob 
91d8dd3165SPavan Nikhilesh 	res = octeontx_mbox_send(&hdr, &b_cfg, len, NULL, 0);
926d28968eSJerin Jacob 	if (res < 0)
936d28968eSJerin Jacob 		return -EACCES;
946d28968eSJerin Jacob 	return res;
956d28968eSJerin Jacob }
966d28968eSJerin Jacob 
976d28968eSJerin Jacob int
986d28968eSJerin Jacob octeontx_pki_port_create_qos(int port, pki_qos_cfg_t *qos_cfg)
996d28968eSJerin Jacob {
1006d28968eSJerin Jacob 	struct octeontx_mbox_hdr hdr;
1016d28968eSJerin Jacob 	int res;
1026d28968eSJerin Jacob 
103*91c0c978SPavan Nikhilesh 	pki_qos_cfg_t q_cfg = *(pki_qos_cfg_t *)qos_cfg;
104*91c0c978SPavan Nikhilesh 	int len = sizeof(pki_qos_cfg_t);
1056d28968eSJerin Jacob 
1066d28968eSJerin Jacob 	hdr.coproc = OCTEONTX_PKI_COPROC;
1076d28968eSJerin Jacob 	hdr.msg = MBOX_PKI_PORT_CREATE_QOS;
1086d28968eSJerin Jacob 	hdr.vfid = port;
1096d28968eSJerin Jacob 
110d8dd3165SPavan Nikhilesh 	res = octeontx_mbox_send(&hdr, &q_cfg, len, NULL, 0);
1116d28968eSJerin Jacob 	if (res < 0)
1126d28968eSJerin Jacob 		return -EACCES;
1136d28968eSJerin Jacob 
1146d28968eSJerin Jacob 	return res;
1156d28968eSJerin Jacob }
1166d28968eSJerin Jacob 
117b95c3413SJerin Jacob 
1186d28968eSJerin Jacob int
1196d28968eSJerin Jacob octeontx_pki_port_errchk_config(int port, pki_errchk_cfg_t *cfg)
1206d28968eSJerin Jacob {
1216d28968eSJerin Jacob 	struct octeontx_mbox_hdr hdr;
1226d28968eSJerin Jacob 	int res;
1236d28968eSJerin Jacob 
124*91c0c978SPavan Nikhilesh 	pki_errchk_cfg_t e_cfg;
125*91c0c978SPavan Nikhilesh 	e_cfg = *((pki_errchk_cfg_t *)(cfg));
126*91c0c978SPavan Nikhilesh 	int len = sizeof(pki_errchk_cfg_t);
1276d28968eSJerin Jacob 
1286d28968eSJerin Jacob 	hdr.coproc = OCTEONTX_PKI_COPROC;
1296d28968eSJerin Jacob 	hdr.msg = MBOX_PKI_PORT_ERRCHK_CONFIG;
1306d28968eSJerin Jacob 	hdr.vfid = port;
1316d28968eSJerin Jacob 
132d8dd3165SPavan Nikhilesh 	res = octeontx_mbox_send(&hdr, &e_cfg, len, NULL, 0);
1336d28968eSJerin Jacob 	if (res < 0)
1346d28968eSJerin Jacob 		return -EACCES;
1356d28968eSJerin Jacob 
1366d28968eSJerin Jacob 	return res;
1376d28968eSJerin Jacob }
1386d28968eSJerin Jacob 
139445371e8SJerin Jacob #define PCI_VENDOR_ID_CAVIUM               0x177D
140445371e8SJerin Jacob #define PCI_DEVICE_ID_OCTEONTX_PKI_VF      0xA0DD
141445371e8SJerin Jacob 
142445371e8SJerin Jacob /* PKIVF pcie device */
143445371e8SJerin Jacob static int
144445371e8SJerin Jacob pkivf_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
145445371e8SJerin Jacob {
146a6d6f0afSPavan Nikhilesh 	struct octeontx_pkivf *res;
147a6d6f0afSPavan Nikhilesh 	static uint8_t vf_cnt;
148a6d6f0afSPavan Nikhilesh 	uint16_t domain;
149a6d6f0afSPavan Nikhilesh 	uint16_t vfid;
150a6d6f0afSPavan Nikhilesh 	uint8_t *bar0;
151a6d6f0afSPavan Nikhilesh 	uint64_t val;
152445371e8SJerin Jacob 
153a6d6f0afSPavan Nikhilesh 	RTE_SET_USED(pci_drv);
154445371e8SJerin Jacob 	/* For secondary processes, the primary has done all the work */
155445371e8SJerin Jacob 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
156445371e8SJerin Jacob 		return 0;
157445371e8SJerin Jacob 
158a6d6f0afSPavan Nikhilesh 	if (pci_dev->mem_resource[0].addr == NULL) {
159a6d6f0afSPavan Nikhilesh 		octeontx_log_err("PKI Empty bar[0] %p",
160a6d6f0afSPavan Nikhilesh 				 pci_dev->mem_resource[0].addr);
161a6d6f0afSPavan Nikhilesh 		return -ENODEV;
162a6d6f0afSPavan Nikhilesh 	}
163a6d6f0afSPavan Nikhilesh 
164a6d6f0afSPavan Nikhilesh 	bar0 = pci_dev->mem_resource[0].addr;
165a6d6f0afSPavan Nikhilesh 	val = octeontx_read64(bar0);
166a6d6f0afSPavan Nikhilesh 	domain = val & 0xffff;
167a6d6f0afSPavan Nikhilesh 	vfid = (val >> 16) & 0xffff;
168a6d6f0afSPavan Nikhilesh 
169a6d6f0afSPavan Nikhilesh 	if (unlikely(vfid >= PKI_VF_MAX)) {
170a6d6f0afSPavan Nikhilesh 		octeontx_log_err("pki: Invalid vfid %d", vfid);
171a6d6f0afSPavan Nikhilesh 		return -EINVAL;
172a6d6f0afSPavan Nikhilesh 	}
173a6d6f0afSPavan Nikhilesh 
174a6d6f0afSPavan Nikhilesh 	res = &pki_vf_ctl.pki[vf_cnt++];
175a6d6f0afSPavan Nikhilesh 	res->vfid = vfid;
176a6d6f0afSPavan Nikhilesh 	res->domain = domain;
177a6d6f0afSPavan Nikhilesh 	res->bar0 = bar0;
178a6d6f0afSPavan Nikhilesh 
179a6d6f0afSPavan Nikhilesh 	octeontx_log_dbg("PKI Domain=%d vfid=%d", res->domain, res->vfid);
180445371e8SJerin Jacob 	return 0;
181445371e8SJerin Jacob }
182445371e8SJerin Jacob 
183445371e8SJerin Jacob static const struct rte_pci_id pci_pkivf_map[] = {
184445371e8SJerin Jacob 	{
185445371e8SJerin Jacob 		RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
186445371e8SJerin Jacob 				PCI_DEVICE_ID_OCTEONTX_PKI_VF)
187445371e8SJerin Jacob 	},
188445371e8SJerin Jacob 	{
189445371e8SJerin Jacob 		.vendor_id = 0,
190445371e8SJerin Jacob 	},
191445371e8SJerin Jacob };
192445371e8SJerin Jacob 
193445371e8SJerin Jacob static struct rte_pci_driver pci_pkivf = {
194445371e8SJerin Jacob 	.id_table = pci_pkivf_map,
195445371e8SJerin Jacob 	.drv_flags = RTE_PCI_DRV_NEED_MAPPING,
196445371e8SJerin Jacob 	.probe = pkivf_probe,
197445371e8SJerin Jacob };
198445371e8SJerin Jacob 
199445371e8SJerin Jacob RTE_PMD_REGISTER_PCI(octeontx_pkivf, pci_pkivf);
200