New upstream version 17.08
[deb_dpdk.git] / drivers / mempool / octeontx / octeontx_ssovf.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium, Inc. 2017.
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, Inc 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
33 #include <rte_atomic.h>
34 #include <rte_common.h>
35 #include <rte_eal.h>
36 #include <rte_io.h>
37 #include <rte_pci.h>
38 #include <rte_bus_pci.h>
39
40 #include "octeontx_mbox.h"
41 #include "octeontx_pool_logs.h"
42
43 #define PCI_VENDOR_ID_CAVIUM              0x177D
44 #define PCI_DEVICE_ID_OCTEONTX_SSOGRP_VF  0xA04B
45 #define PCI_DEVICE_ID_OCTEONTX_SSOWS_VF   0xA04D
46
47 #define SSO_MAX_VHGRP                     (64)
48 #define SSO_MAX_VHWS                      (32)
49
50 #define SSO_VHGRP_AQ_THR                  (0x1E0ULL)
51
52 struct ssovf_res {
53         uint16_t domain;
54         uint16_t vfid;
55         void *bar0;
56         void *bar2;
57 };
58
59 struct ssowvf_res {
60         uint16_t domain;
61         uint16_t vfid;
62         void *bar0;
63         void *bar2;
64         void *bar4;
65 };
66
67 struct ssowvf_identify {
68         uint16_t domain;
69         uint16_t vfid;
70 };
71
72 struct ssodev {
73         uint8_t total_ssovfs;
74         uint8_t total_ssowvfs;
75         struct ssovf_res grp[SSO_MAX_VHGRP];
76         struct ssowvf_res hws[SSO_MAX_VHWS];
77 };
78
79 static struct ssodev sdev;
80
81 /* Interface functions */
82 int
83 octeontx_ssovf_info(struct octeontx_ssovf_info *info)
84 {
85         uint8_t i;
86         uint16_t domain;
87
88         if (rte_eal_process_type() != RTE_PROC_PRIMARY || info == NULL)
89                 return -EINVAL;
90
91         if (sdev.total_ssovfs == 0 || sdev.total_ssowvfs == 0)
92                 return -ENODEV;
93
94         domain = sdev.grp[0].domain;
95         for (i = 0; i < sdev.total_ssovfs; i++) {
96                 /* Check vfid's are contiguous and belong to same domain */
97                 if (sdev.grp[i].vfid != i ||
98                         sdev.grp[i].bar0 == NULL ||
99                         sdev.grp[i].domain != domain) {
100                         mbox_log_err("GRP error, vfid=%d/%d domain=%d/%d %p",
101                                 i, sdev.grp[i].vfid,
102                                 domain, sdev.grp[i].domain,
103                                 sdev.grp[i].bar0);
104                         return -EINVAL;
105                 }
106         }
107
108         for (i = 0; i < sdev.total_ssowvfs; i++) {
109                 /* Check vfid's are contiguous and belong to same domain */
110                 if (sdev.hws[i].vfid != i ||
111                         sdev.hws[i].bar0 == NULL ||
112                         sdev.hws[i].domain != domain) {
113                         mbox_log_err("HWS error, vfid=%d/%d domain=%d/%d %p",
114                                 i, sdev.hws[i].vfid,
115                                 domain, sdev.hws[i].domain,
116                                 sdev.hws[i].bar0);
117                         return -EINVAL;
118                 }
119         }
120
121         info->domain = domain;
122         info->total_ssovfs = sdev.total_ssovfs;
123         info->total_ssowvfs = sdev.total_ssowvfs;
124         return 0;
125 }
126
127 void*
128 octeontx_ssovf_bar(enum octeontx_ssovf_type type, uint8_t id, uint8_t bar)
129 {
130         if (rte_eal_process_type() != RTE_PROC_PRIMARY ||
131                         type > OCTEONTX_SSO_HWS)
132                 return NULL;
133
134         if (type == OCTEONTX_SSO_GROUP) {
135                 if (id >= sdev.total_ssovfs)
136                         return NULL;
137         } else {
138                 if (id >= sdev.total_ssowvfs)
139                         return NULL;
140         }
141
142         if (type == OCTEONTX_SSO_GROUP) {
143                 switch (bar) {
144                 case 0:
145                         return sdev.grp[id].bar0;
146                 case 2:
147                         return sdev.grp[id].bar2;
148                 default:
149                         return NULL;
150                 }
151         } else {
152                 switch (bar) {
153                 case 0:
154                         return sdev.hws[id].bar0;
155                 case 2:
156                         return sdev.hws[id].bar2;
157                 case 4:
158                         return sdev.hws[id].bar4;
159                 default:
160                         return NULL;
161                 }
162         }
163 }
164
165 /* SSOWVF pcie device aka event port probe */
166
167 static int
168 ssowvf_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
169 {
170         uint16_t vfid;
171         struct ssowvf_res *res;
172         struct ssowvf_identify *id;
173
174         RTE_SET_USED(pci_drv);
175
176         /* For secondary processes, the primary has done all the work */
177         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
178                 return 0;
179
180         if (pci_dev->mem_resource[0].addr == NULL ||
181                         pci_dev->mem_resource[2].addr == NULL ||
182                         pci_dev->mem_resource[4].addr == NULL) {
183                 mbox_log_err("Empty bars %p %p %p",
184                                 pci_dev->mem_resource[0].addr,
185                                 pci_dev->mem_resource[2].addr,
186                                 pci_dev->mem_resource[4].addr);
187                 return -ENODEV;
188         }
189
190         if (pci_dev->mem_resource[4].len != SSOW_BAR4_LEN) {
191                 mbox_log_err("Bar4 len mismatch %d != %d",
192                         SSOW_BAR4_LEN, (int)pci_dev->mem_resource[4].len);
193                 return -EINVAL;
194         }
195
196         id = pci_dev->mem_resource[4].addr;
197         vfid = id->vfid;
198         if (vfid >= SSO_MAX_VHWS) {
199                 mbox_log_err("Invalid vfid(%d/%d)", vfid, SSO_MAX_VHWS);
200                 return -EINVAL;
201         }
202
203         res = &sdev.hws[vfid];
204         res->vfid = vfid;
205         res->bar0 = pci_dev->mem_resource[0].addr;
206         res->bar2 = pci_dev->mem_resource[2].addr;
207         res->bar4 = pci_dev->mem_resource[4].addr;
208         res->domain = id->domain;
209
210         sdev.total_ssowvfs++;
211         rte_wmb();
212         mbox_log_dbg("Domain=%d hws=%d total_ssowvfs=%d", res->domain,
213                         res->vfid, sdev.total_ssowvfs);
214         return 0;
215 }
216
217 static const struct rte_pci_id pci_ssowvf_map[] = {
218         {
219                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
220                                 PCI_DEVICE_ID_OCTEONTX_SSOWS_VF)
221         },
222         {
223                 .vendor_id = 0,
224         },
225 };
226
227 static struct rte_pci_driver pci_ssowvf = {
228         .id_table = pci_ssowvf_map,
229         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
230         .probe = ssowvf_probe,
231 };
232
233 RTE_PMD_REGISTER_PCI(octeontx_ssowvf, pci_ssowvf);
234
235 /* SSOVF pcie device aka event queue probe */
236
237 static int
238 ssovf_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
239 {
240         uint64_t val;
241         uint16_t vfid;
242         uint8_t *idreg;
243         struct ssovf_res *res;
244
245         RTE_SET_USED(pci_drv);
246
247         /* For secondary processes, the primary has done all the work */
248         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
249                 return 0;
250
251         if (pci_dev->mem_resource[0].addr == NULL ||
252                         pci_dev->mem_resource[2].addr == NULL) {
253                 mbox_log_err("Empty bars %p %p",
254                         pci_dev->mem_resource[0].addr,
255                         pci_dev->mem_resource[2].addr);
256                 return -ENODEV;
257         }
258         idreg = pci_dev->mem_resource[0].addr;
259         idreg += SSO_VHGRP_AQ_THR;
260         val = rte_read64(idreg);
261
262         /* Write back the default value of aq_thr */
263         rte_write64((1ULL << 33) - 1, idreg);
264         vfid = (val >> 16) & 0xffff;
265         if (vfid >= SSO_MAX_VHGRP) {
266                 mbox_log_err("Invalid vfid (%d/%d)", vfid, SSO_MAX_VHGRP);
267                 return -EINVAL;
268         }
269
270         res = &sdev.grp[vfid];
271         res->vfid = vfid;
272         res->bar0 = pci_dev->mem_resource[0].addr;
273         res->bar2 = pci_dev->mem_resource[2].addr;
274         res->domain = val & 0xffff;
275
276         sdev.total_ssovfs++;
277         rte_wmb();
278         mbox_log_dbg("Domain=%d group=%d total_ssovfs=%d", res->domain,
279                         res->vfid, sdev.total_ssovfs);
280         return 0;
281 }
282
283 static const struct rte_pci_id pci_ssovf_map[] = {
284         {
285                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
286                                 PCI_DEVICE_ID_OCTEONTX_SSOGRP_VF)
287         },
288         {
289                 .vendor_id = 0,
290         },
291 };
292
293 static struct rte_pci_driver pci_ssovf = {
294         .id_table = pci_ssovf_map,
295         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
296         .probe = ssovf_probe,
297 };
298
299 RTE_PMD_REGISTER_PCI(octeontx_ssovf, pci_ssovf);