net/virtio: remove unused offloads macro
[dpdk.git] / drivers / raw / ifpga_rawdev / ifpga_rawdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 #include <string.h>
6 #include <dirent.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9 #include <sys/types.h>
10 #include <fcntl.h>
11 #include <rte_log.h>
12 #include <rte_bus.h>
13 #include <rte_eal_memconfig.h>
14 #include <rte_malloc.h>
15 #include <rte_devargs.h>
16 #include <rte_memcpy.h>
17 #include <rte_pci.h>
18 #include <rte_bus_pci.h>
19 #include <rte_kvargs.h>
20 #include <rte_alarm.h>
21
22 #include <rte_errno.h>
23 #include <rte_per_lcore.h>
24 #include <rte_memory.h>
25 #include <rte_memzone.h>
26 #include <rte_eal.h>
27 #include <rte_common.h>
28 #include <rte_bus_vdev.h>
29
30 #include "base/opae_hw_api.h"
31 #include "rte_rawdev.h"
32 #include "rte_rawdev_pmd.h"
33 #include "rte_bus_ifpga.h"
34 #include "ifpga_common.h"
35 #include "ifpga_logs.h"
36 #include "ifpga_rawdev.h"
37
38 int ifpga_rawdev_logtype;
39
40 #define PCI_VENDOR_ID_INTEL          0x8086
41 /* PCI Device ID */
42 #define PCIE_DEVICE_ID_PF_INT_5_X    0xBCBD
43 #define PCIE_DEVICE_ID_PF_INT_6_X    0xBCC0
44 #define PCIE_DEVICE_ID_PF_DSC_1_X    0x09C4
45 /* VF Device */
46 #define PCIE_DEVICE_ID_VF_INT_5_X    0xBCBF
47 #define PCIE_DEVICE_ID_VF_INT_6_X    0xBCC1
48 #define PCIE_DEVICE_ID_VF_DSC_1_X    0x09C5
49 #define RTE_MAX_RAW_DEVICE           10
50
51 static const struct rte_pci_id pci_ifpga_map[] = {
52         { RTE_PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIE_DEVICE_ID_PF_INT_5_X) },
53         { RTE_PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIE_DEVICE_ID_VF_INT_5_X) },
54         { RTE_PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIE_DEVICE_ID_PF_INT_6_X) },
55         { RTE_PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIE_DEVICE_ID_VF_INT_6_X) },
56         { RTE_PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIE_DEVICE_ID_PF_DSC_1_X) },
57         { RTE_PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIE_DEVICE_ID_VF_DSC_1_X) },
58         { .vendor_id = 0, /* sentinel */ },
59 };
60
61 static int
62 ifpga_fill_afu_dev(struct opae_accelerator *acc,
63                 struct rte_afu_device *afu_dev)
64 {
65         struct rte_mem_resource *res = afu_dev->mem_resource;
66         struct opae_acc_region_info region_info;
67         struct opae_acc_info info;
68         unsigned long i;
69         int ret;
70
71         ret = opae_acc_get_info(acc, &info);
72         if (ret)
73                 return ret;
74
75         if (info.num_regions > PCI_MAX_RESOURCE)
76                 return -EFAULT;
77
78         afu_dev->num_region = info.num_regions;
79
80         for (i = 0; i < info.num_regions; i++) {
81                 region_info.index = i;
82                 ret = opae_acc_get_region_info(acc, &region_info);
83                 if (ret)
84                         return ret;
85
86                 if ((region_info.flags & ACC_REGION_MMIO) &&
87                     (region_info.flags & ACC_REGION_READ) &&
88                     (region_info.flags & ACC_REGION_WRITE)) {
89                         res[i].phys_addr = region_info.phys_addr;
90                         res[i].len = region_info.len;
91                         res[i].addr = region_info.addr;
92                 } else
93                         return -EFAULT;
94         }
95
96         return 0;
97 }
98
99 static void
100 ifpga_rawdev_info_get(struct rte_rawdev *dev,
101                                      rte_rawdev_obj_t dev_info)
102 {
103         struct opae_adapter *adapter;
104         struct opae_accelerator *acc;
105         struct rte_afu_device *afu_dev;
106
107         IFPGA_RAWDEV_PMD_FUNC_TRACE();
108
109         if (!dev_info) {
110                 IFPGA_RAWDEV_PMD_ERR("Invalid request");
111                 return;
112         }
113
114         adapter = ifpga_rawdev_get_priv(dev);
115         if (!adapter)
116                 return;
117
118         afu_dev = dev_info;
119         afu_dev->rawdev = dev;
120
121         /* find opae_accelerator and fill info into afu_device */
122         opae_adapter_for_each_acc(adapter, acc) {
123                 if (acc->index != afu_dev->id.port)
124                         continue;
125
126                 if (ifpga_fill_afu_dev(acc, afu_dev)) {
127                         IFPGA_RAWDEV_PMD_ERR("cannot get info\n");
128                         return;
129                 }
130         }
131 }
132
133 static int
134 ifpga_rawdev_configure(const struct rte_rawdev *dev,
135                 rte_rawdev_obj_t config)
136 {
137         IFPGA_RAWDEV_PMD_FUNC_TRACE();
138
139         RTE_FUNC_PTR_OR_ERR_RET(dev, -EINVAL);
140
141         return config ? 0 : 1;
142 }
143
144 static int
145 ifpga_rawdev_start(struct rte_rawdev *dev)
146 {
147         int ret = 0;
148         struct opae_adapter *adapter;
149
150         IFPGA_RAWDEV_PMD_FUNC_TRACE();
151
152         RTE_FUNC_PTR_OR_ERR_RET(dev, -EINVAL);
153
154         adapter = ifpga_rawdev_get_priv(dev);
155         if (!adapter)
156                 return -ENODEV;
157
158         return ret;
159 }
160
161 static void
162 ifpga_rawdev_stop(struct rte_rawdev *dev)
163 {
164         dev->started = 0;
165 }
166
167 static int
168 ifpga_rawdev_close(struct rte_rawdev *dev)
169 {
170         return dev ? 0:1;
171 }
172
173 static int
174 ifpga_rawdev_reset(struct rte_rawdev *dev)
175 {
176         return dev ? 0:1;
177 }
178
179 static int
180 fpga_pr(struct rte_rawdev *raw_dev, u32 port_id, u64 *buffer, u32 size,
181                         u64 *status)
182 {
183
184         struct opae_adapter *adapter;
185         struct opae_manager *mgr;
186         struct opae_accelerator *acc;
187         struct opae_bridge *br;
188         int ret;
189
190         adapter = ifpga_rawdev_get_priv(raw_dev);
191         if (!adapter)
192                 return -ENODEV;
193
194         mgr = opae_adapter_get_mgr(adapter);
195         if (!mgr)
196                 return -ENODEV;
197
198         acc = opae_adapter_get_acc(adapter, port_id);
199         if (!acc)
200                 return -ENODEV;
201
202         br = opae_acc_get_br(acc);
203         if (!br)
204                 return -ENODEV;
205
206         ret = opae_manager_flash(mgr, port_id, buffer, size, status);
207         if (ret) {
208                 IFPGA_RAWDEV_PMD_ERR("%s pr error %d\n", __func__, ret);
209                 return ret;
210         }
211
212         ret = opae_bridge_reset(br);
213         if (ret) {
214                 IFPGA_RAWDEV_PMD_ERR("%s reset port:%d error %d\n",
215                                 __func__, port_id, ret);
216                 return ret;
217         }
218
219         return ret;
220 }
221
222 static int
223 rte_fpga_do_pr(struct rte_rawdev *rawdev, int port_id,
224                 const char *file_name)
225 {
226         struct stat file_stat;
227         int file_fd;
228         int ret = 0;
229         ssize_t buffer_size;
230         void *buffer;
231         u64 pr_error;
232
233         if (!file_name)
234                 return -EINVAL;
235
236         file_fd = open(file_name, O_RDONLY);
237         if (file_fd < 0) {
238                 IFPGA_RAWDEV_PMD_ERR("%s: open file error: %s\n",
239                                 __func__, file_name);
240                 IFPGA_RAWDEV_PMD_ERR("Message : %s\n", strerror(errno));
241                 return -EINVAL;
242         }
243         ret = stat(file_name, &file_stat);
244         if (ret) {
245                 IFPGA_RAWDEV_PMD_ERR("stat on bitstream file failed: %s\n",
246                                 file_name);
247                 return -EINVAL;
248         }
249         buffer_size = file_stat.st_size;
250         IFPGA_RAWDEV_PMD_INFO("bitstream file size: %zu\n", buffer_size);
251         buffer = rte_malloc(NULL, buffer_size, 0);
252         if (!buffer) {
253                 ret = -ENOMEM;
254                 goto close_fd;
255         }
256
257         /*read the raw data*/
258         if (buffer_size != read(file_fd, (void *)buffer, buffer_size)) {
259                 ret = -EINVAL;
260                 goto free_buffer;
261         }
262
263         /*do PR now*/
264         ret = fpga_pr(rawdev, port_id, buffer, buffer_size, &pr_error);
265         IFPGA_RAWDEV_PMD_INFO("downloading to device port %d....%s.\n", port_id,
266                 ret ? "failed" : "success");
267         if (ret) {
268                 ret = -EINVAL;
269                 goto free_buffer;
270         }
271
272 free_buffer:
273         if (buffer)
274                 rte_free(buffer);
275 close_fd:
276         close(file_fd);
277         file_fd = 0;
278         return ret;
279 }
280
281 static int
282 ifpga_rawdev_pr(struct rte_rawdev *dev,
283         rte_rawdev_obj_t pr_conf)
284 {
285         struct opae_adapter *adapter;
286         struct rte_afu_pr_conf *afu_pr_conf;
287         int ret;
288         struct uuid uuid;
289         struct opae_accelerator *acc;
290
291         IFPGA_RAWDEV_PMD_FUNC_TRACE();
292
293         adapter = ifpga_rawdev_get_priv(dev);
294         if (!adapter)
295                 return -ENODEV;
296
297         if (!pr_conf)
298                 return -EINVAL;
299
300         afu_pr_conf = pr_conf;
301
302         if (afu_pr_conf->pr_enable) {
303                 ret = rte_fpga_do_pr(dev,
304                                 afu_pr_conf->afu_id.port,
305                                 afu_pr_conf->bs_path);
306                 if (ret) {
307                         IFPGA_RAWDEV_PMD_ERR("do pr error %d\n", ret);
308                         return ret;
309                 }
310         }
311
312         acc = opae_adapter_get_acc(adapter, afu_pr_conf->afu_id.port);
313         if (!acc)
314                 return -ENODEV;
315
316         ret = opae_acc_get_uuid(acc, &uuid);
317         if (ret)
318                 return ret;
319
320         memcpy(&afu_pr_conf->afu_id.uuid.uuid_low, uuid.b, sizeof(u64));
321         memcpy(&afu_pr_conf->afu_id.uuid.uuid_high, uuid.b + 8, sizeof(u64));
322
323         IFPGA_RAWDEV_PMD_INFO("%s: uuid_l=0x%lx, uuid_h=0x%lx\n", __func__,
324                 (unsigned long)afu_pr_conf->afu_id.uuid.uuid_low,
325                 (unsigned long)afu_pr_conf->afu_id.uuid.uuid_high);
326
327         return 0;
328 }
329
330 static const struct rte_rawdev_ops ifpga_rawdev_ops = {
331         .dev_info_get = ifpga_rawdev_info_get,
332         .dev_configure = ifpga_rawdev_configure,
333         .dev_start = ifpga_rawdev_start,
334         .dev_stop = ifpga_rawdev_stop,
335         .dev_close = ifpga_rawdev_close,
336         .dev_reset = ifpga_rawdev_reset,
337
338         .queue_def_conf = NULL,
339         .queue_setup = NULL,
340         .queue_release = NULL,
341
342         .attr_get = NULL,
343         .attr_set = NULL,
344
345         .enqueue_bufs = NULL,
346         .dequeue_bufs = NULL,
347
348         .dump = NULL,
349
350         .xstats_get = NULL,
351         .xstats_get_names = NULL,
352         .xstats_get_by_name = NULL,
353         .xstats_reset = NULL,
354
355         .firmware_status_get = NULL,
356         .firmware_version_get = NULL,
357         .firmware_load = ifpga_rawdev_pr,
358         .firmware_unload = NULL,
359
360         .dev_selftest = NULL,
361 };
362
363 static int
364 ifpga_rawdev_create(struct rte_pci_device *pci_dev,
365                         int socket_id)
366 {
367         int ret = 0;
368         struct rte_rawdev *rawdev = NULL;
369         struct opae_adapter *adapter = NULL;
370         struct opae_manager *mgr = NULL;
371         struct opae_adapter_data_pci *data = NULL;
372         char name[RTE_RAWDEV_NAME_MAX_LEN];
373         int i;
374
375         if (!pci_dev) {
376                 IFPGA_RAWDEV_PMD_ERR("Invalid pci_dev of the device!");
377                 ret = -EINVAL;
378                 goto cleanup;
379         }
380
381         memset(name, 0, sizeof(name));
382         snprintf(name, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%x:%02x.%x",
383                 pci_dev->addr.bus, pci_dev->addr.devid, pci_dev->addr.function);
384
385         IFPGA_RAWDEV_PMD_INFO("Init %s on NUMA node %d", name, rte_socket_id());
386
387         /* Allocate device structure */
388         rawdev = rte_rawdev_pmd_allocate(name, sizeof(struct opae_adapter),
389                                          socket_id);
390         if (rawdev == NULL) {
391                 IFPGA_RAWDEV_PMD_ERR("Unable to allocate rawdevice");
392                 ret = -EINVAL;
393                 goto cleanup;
394         }
395
396         /* alloc OPAE_FPGA_PCI data to register to OPAE hardware level API */
397         data = opae_adapter_data_alloc(OPAE_FPGA_PCI);
398         if (!data) {
399                 ret = -ENOMEM;
400                 goto cleanup;
401         }
402
403         /* init opae_adapter_data_pci for device specific information */
404         for (i = 0; i < PCI_MAX_RESOURCE; i++) {
405                 data->region[i].phys_addr = pci_dev->mem_resource[i].phys_addr;
406                 data->region[i].len = pci_dev->mem_resource[i].len;
407                 data->region[i].addr = pci_dev->mem_resource[i].addr;
408         }
409         data->device_id = pci_dev->id.device_id;
410         data->vendor_id = pci_dev->id.vendor_id;
411
412         /* create a opae_adapter based on above device data */
413         adapter = opae_adapter_alloc(pci_dev->device.name, data);
414         if (!adapter) {
415                 ret = -ENOMEM;
416                 goto free_adapter_data;
417         }
418
419         rawdev->dev_ops = &ifpga_rawdev_ops;
420         rawdev->device = &pci_dev->device;
421         rawdev->driver_name = pci_dev->device.driver->name;
422
423         rawdev->dev_private = adapter;
424
425         /* must enumerate the adapter before use it */
426         ret = opae_adapter_enumerate(adapter);
427         if (ret)
428                 goto free_adapter;
429
430         /* get opae_manager to rawdev */
431         mgr = opae_adapter_get_mgr(adapter);
432         if (mgr) {
433                 /* PF function */
434                 IFPGA_RAWDEV_PMD_INFO("this is a PF function");
435         }
436
437         return ret;
438
439 free_adapter:
440         if (adapter)
441                 opae_adapter_free(adapter);
442 free_adapter_data:
443         if (data)
444                 opae_adapter_data_free(data);
445 cleanup:
446         if (rawdev)
447                 rte_rawdev_pmd_release(rawdev);
448
449         return ret;
450 }
451
452 static int
453 ifpga_rawdev_destroy(struct rte_pci_device *pci_dev)
454 {
455         int ret;
456         struct rte_rawdev *rawdev;
457         char name[RTE_RAWDEV_NAME_MAX_LEN];
458         struct opae_adapter *adapter;
459
460         if (!pci_dev) {
461                 IFPGA_RAWDEV_PMD_ERR("Invalid pci_dev of the device!");
462                 ret = -EINVAL;
463                 return ret;
464         }
465
466         memset(name, 0, sizeof(name));
467         snprintf(name, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%x:%02x.%x",
468                 pci_dev->addr.bus, pci_dev->addr.devid, pci_dev->addr.function);
469
470         IFPGA_RAWDEV_PMD_INFO("Closing %s on NUMA node %d",
471                 name, rte_socket_id());
472
473         rawdev = rte_rawdev_pmd_get_named_dev(name);
474         if (!rawdev) {
475                 IFPGA_RAWDEV_PMD_ERR("Invalid device name (%s)", name);
476                 return -EINVAL;
477         }
478
479         adapter = ifpga_rawdev_get_priv(rawdev);
480         if (!adapter)
481                 return -ENODEV;
482
483         opae_adapter_data_free(adapter->data);
484         opae_adapter_free(adapter);
485
486         /* rte_rawdev_close is called by pmd_release */
487         ret = rte_rawdev_pmd_release(rawdev);
488         if (ret)
489                 IFPGA_RAWDEV_PMD_DEBUG("Device cleanup failed");
490
491         return ret;
492 }
493
494 static int
495 ifpga_rawdev_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
496         struct rte_pci_device *pci_dev)
497 {
498
499         IFPGA_RAWDEV_PMD_FUNC_TRACE();
500         return ifpga_rawdev_create(pci_dev, rte_socket_id());
501 }
502
503 static int
504 ifpga_rawdev_pci_remove(struct rte_pci_device *pci_dev)
505 {
506         return ifpga_rawdev_destroy(pci_dev);
507 }
508
509 static struct rte_pci_driver rte_ifpga_rawdev_pmd = {
510         .id_table  = pci_ifpga_map,
511         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
512         .probe     = ifpga_rawdev_pci_probe,
513         .remove    = ifpga_rawdev_pci_remove,
514 };
515
516 RTE_PMD_REGISTER_PCI(ifpga_rawdev_pci_driver, rte_ifpga_rawdev_pmd);
517 RTE_PMD_REGISTER_PCI_TABLE(ifpga_rawdev_pci_driver, rte_ifpga_rawdev_pmd);
518 RTE_PMD_REGISTER_KMOD_DEP(ifpga_rawdev_pci_driver, "* igb_uio | uio_pci_generic | vfio-pci");
519
520 RTE_INIT(ifpga_rawdev_init_log)
521 {
522         ifpga_rawdev_logtype = rte_log_register("driver.raw.init");
523         if (ifpga_rawdev_logtype >= 0)
524                 rte_log_set_level(ifpga_rawdev_logtype, RTE_LOG_NOTICE);
525 }
526
527 static const char * const valid_args[] = {
528 #define IFPGA_ARG_NAME         "ifpga"
529         IFPGA_ARG_NAME,
530 #define IFPGA_ARG_PORT         "port"
531         IFPGA_ARG_PORT,
532 #define IFPGA_AFU_BTS          "afu_bts"
533         IFPGA_AFU_BTS,
534         NULL
535 };
536
537 static int
538 ifpga_cfg_probe(struct rte_vdev_device *dev)
539 {
540         struct rte_devargs *devargs;
541         struct rte_kvargs *kvlist = NULL;
542         int port;
543         char *name = NULL;
544         char dev_name[RTE_RAWDEV_NAME_MAX_LEN];
545
546         devargs = dev->device.devargs;
547
548         kvlist = rte_kvargs_parse(devargs->args, valid_args);
549         if (!kvlist) {
550                 IFPGA_RAWDEV_PMD_LOG(ERR, "error when parsing param");
551                 goto end;
552         }
553
554         if (rte_kvargs_count(kvlist, IFPGA_ARG_NAME) == 1) {
555                 if (rte_kvargs_process(kvlist, IFPGA_ARG_NAME,
556                                        &rte_ifpga_get_string_arg, &name) < 0) {
557                         IFPGA_RAWDEV_PMD_ERR("error to parse %s",
558                                      IFPGA_ARG_NAME);
559                         goto end;
560                 }
561         } else {
562                 IFPGA_RAWDEV_PMD_ERR("arg %s is mandatory for ifpga bus",
563                           IFPGA_ARG_NAME);
564                 goto end;
565         }
566
567         if (rte_kvargs_count(kvlist, IFPGA_ARG_PORT) == 1) {
568                 if (rte_kvargs_process(kvlist,
569                         IFPGA_ARG_PORT,
570                         &rte_ifpga_get_integer32_arg,
571                         &port) < 0) {
572                         IFPGA_RAWDEV_PMD_ERR("error to parse %s",
573                                 IFPGA_ARG_PORT);
574                         goto end;
575                 }
576         } else {
577                 IFPGA_RAWDEV_PMD_ERR("arg %s is mandatory for ifpga bus",
578                           IFPGA_ARG_PORT);
579                 goto end;
580         }
581
582         memset(dev_name, 0, sizeof(dev_name));
583         snprintf(dev_name, RTE_RAWDEV_NAME_MAX_LEN, "%d|%s",
584         port, name);
585
586         rte_eal_hotplug_add(RTE_STR(IFPGA_BUS_NAME),
587                         dev_name, devargs->args);
588 end:
589         if (kvlist)
590                 rte_kvargs_free(kvlist);
591         if (name)
592                 free(name);
593
594         return 0;
595 }
596
597 static int
598 ifpga_cfg_remove(struct rte_vdev_device *vdev)
599 {
600         IFPGA_RAWDEV_PMD_INFO("Remove ifpga_cfg %p",
601                 vdev);
602
603         return 0;
604 }
605
606 static struct rte_vdev_driver ifpga_cfg_driver = {
607         .probe = ifpga_cfg_probe,
608         .remove = ifpga_cfg_remove,
609 };
610
611 RTE_PMD_REGISTER_VDEV(ifpga_rawdev_cfg, ifpga_cfg_driver);
612 RTE_PMD_REGISTER_ALIAS(ifpga_rawdev_cfg, ifpga_cfg);
613 RTE_PMD_REGISTER_PARAM_STRING(ifpga_rawdev_cfg,
614         "ifpga=<string> "
615         "port=<int> "
616         "afu_bts=<path>");
617