net/octeontx/base: probe PKI and PKO PCIe VF devices
[dpdk.git] / drivers / net / octeontx / base / octeontx_pkovf.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium Inc. 2017. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Cavium networks nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 #include <stdbool.h>
33 #include <string.h>
34 #include <stdio.h>
35
36 #include <rte_eal.h>
37 #include <rte_cycles.h>
38 #include <rte_malloc.h>
39 #include <rte_memory.h>
40 #include <rte_pci.h>
41 #include <rte_spinlock.h>
42
43 #include "../octeontx_logs.h"
44 #include "octeontx_io.h"
45 #include "octeontx_pkovf.h"
46
47 struct octeontx_pko_iomem {
48         uint8_t         *va;
49         phys_addr_t     iova;
50         size_t          size;
51 };
52
53 #define PKO_IOMEM_NULL (struct octeontx_pko_iomem){0, 0, 0}
54
55 struct octeontx_pko_fc_ctl_s {
56         int64_t buf_cnt;
57         int64_t padding[(PKO_DQ_FC_STRIDE / 8) - 1];
58 };
59
60 struct octeontx_pkovf {
61         uint8_t         *bar0;
62         uint8_t         *bar2;
63         uint16_t        domain;
64         uint16_t        vfid;
65 };
66
67 struct octeontx_pko_vf_ctl_s {
68         rte_spinlock_t lock;
69
70         struct octeontx_pko_iomem fc_iomem;
71         struct octeontx_pko_fc_ctl_s *fc_ctl;
72         struct octeontx_pkovf pko[PKO_VF_MAX];
73         struct {
74                 uint64_t chanid;
75         } dq_map[PKO_VF_MAX * PKO_VF_NUM_DQ];
76 };
77
78 static struct octeontx_pko_vf_ctl_s pko_vf_ctl;
79
80 static void
81 octeontx_pkovf_setup(void)
82 {
83         static bool init_once;
84
85         if (!init_once) {
86                 unsigned int i;
87
88                 rte_spinlock_init(&pko_vf_ctl.lock);
89
90                 pko_vf_ctl.fc_iomem = PKO_IOMEM_NULL;
91                 pko_vf_ctl.fc_ctl = NULL;
92
93                 for (i = 0; i < PKO_VF_MAX; i++) {
94                         pko_vf_ctl.pko[i].bar0 = NULL;
95                         pko_vf_ctl.pko[i].bar2 = NULL;
96                         pko_vf_ctl.pko[i].domain = ~(uint16_t)0;
97                         pko_vf_ctl.pko[i].vfid = ~(uint16_t)0;
98                 }
99
100                 for (i = 0; i < (PKO_VF_MAX * PKO_VF_NUM_DQ); i++)
101                         pko_vf_ctl.dq_map[i].chanid = 0;
102
103                 init_once = true;
104         }
105 }
106
107 /* PKOVF pcie device*/
108 static int
109 pkovf_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
110 {
111         uint64_t val;
112         uint16_t vfid;
113         uint16_t domain;
114         uint8_t *bar0;
115         uint8_t *bar2;
116         struct octeontx_pkovf *res;
117
118         RTE_SET_USED(pci_drv);
119
120         /* For secondary processes, the primary has done all the work */
121         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
122                 return 0;
123
124         if (pci_dev->mem_resource[0].addr == NULL ||
125             pci_dev->mem_resource[2].addr == NULL) {
126                 octeontx_log_err("Empty bars %p %p",
127                         pci_dev->mem_resource[0].addr,
128                         pci_dev->mem_resource[2].addr);
129                 return -ENODEV;
130         }
131         bar0 = pci_dev->mem_resource[0].addr;
132         bar2 = pci_dev->mem_resource[2].addr;
133
134         octeontx_pkovf_setup();
135
136         /* get vfid and domain */
137         val = octeontx_read64(bar0 + PKO_VF_DQ_FC_CONFIG);
138         domain = (val >> 7) & 0xffff;
139         vfid = (val >> 23) & 0xffff;
140
141         if (unlikely(vfid >= PKO_VF_MAX)) {
142                 octeontx_log_err("pko: Invalid vfid %d", vfid);
143                 return -EINVAL;
144         }
145
146         res = &pko_vf_ctl.pko[vfid];
147         res->vfid = vfid;
148         res->domain = domain;
149         res->bar0 = bar0;
150         res->bar2 = bar2;
151
152         octeontx_log_dbg("Domain=%d group=%d", res->domain, res->vfid);
153         return 0;
154 }
155
156 #define PCI_VENDOR_ID_CAVIUM               0x177D
157 #define PCI_DEVICE_ID_OCTEONTX_PKO_VF      0xA049
158
159 static const struct rte_pci_id pci_pkovf_map[] = {
160         {
161                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
162                                 PCI_DEVICE_ID_OCTEONTX_PKO_VF)
163         },
164         {
165                 .vendor_id = 0,
166         },
167 };
168
169 static struct rte_pci_driver pci_pkovf = {
170         .id_table = pci_pkovf_map,
171         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
172         .probe = pkovf_probe,
173 };
174
175 RTE_PMD_REGISTER_PCI(octeontx_pkovf, pci_pkovf);