1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2018 Intel Corporation 3 */ 4 5 #include <string.h> 6 #include <inttypes.h> 7 #include <stdint.h> 8 #include <stdlib.h> 9 #include <stdio.h> 10 #include <sys/queue.h> 11 #include <sys/mman.h> 12 #include <sys/types.h> 13 #include <unistd.h> 14 #include <fcntl.h> 15 16 #include <rte_errno.h> 17 #include <bus_driver.h> 18 #include <rte_per_lcore.h> 19 #include <rte_memory.h> 20 #include <rte_memzone.h> 21 #include <rte_eal.h> 22 #include <rte_common.h> 23 #include <rte_devargs.h> 24 #include <rte_kvargs.h> 25 #include <rte_alarm.h> 26 #include <rte_string_fns.h> 27 #include <rte_debug.h> 28 29 #include "rte_rawdev.h" 30 #include "rte_rawdev_pmd.h" 31 #include "bus_ifpga_driver.h" 32 #include "ifpga_logs.h" 33 #include "ifpga_common.h" 34 35 /* Forward declaration to access Intel FPGA bus 36 * on which iFPGA devices are connected 37 */ 38 static struct rte_bus rte_ifpga_bus; 39 40 static TAILQ_HEAD(, rte_afu_device) ifpga_afu_dev_list = 41 TAILQ_HEAD_INITIALIZER(ifpga_afu_dev_list); 42 static TAILQ_HEAD(, rte_afu_driver) ifpga_afu_drv_list = 43 TAILQ_HEAD_INITIALIZER(ifpga_afu_drv_list); 44 45 46 /* register a ifpga bus based driver */ 47 void rte_ifpga_driver_register(struct rte_afu_driver *driver) 48 { 49 RTE_VERIFY(driver); 50 51 TAILQ_INSERT_TAIL(&ifpga_afu_drv_list, driver, next); 52 } 53 54 /* un-register a fpga bus based driver */ 55 void rte_ifpga_driver_unregister(struct rte_afu_driver *driver) 56 { 57 TAILQ_REMOVE(&ifpga_afu_drv_list, driver, next); 58 } 59 60 static struct rte_afu_device * 61 ifpga_find_afu_dev(const struct rte_rawdev *rdev, 62 const struct rte_afu_id *afu_id) 63 { 64 struct rte_afu_device *afu_dev = NULL; 65 66 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) { 67 if (afu_dev->rawdev == rdev && 68 !ifpga_afu_id_cmp(&afu_dev->id, afu_id)) 69 return afu_dev; 70 } 71 return NULL; 72 } 73 74 struct rte_afu_device * 75 rte_ifpga_find_afu_by_name(const char *name) 76 { 77 struct rte_afu_device *afu_dev = NULL; 78 79 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) { 80 if (!strcmp(afu_dev->device.name, name)) 81 return afu_dev; 82 } 83 return NULL; 84 } 85 86 static const char * const valid_args[] = { 87 #define IFPGA_ARG_NAME "ifpga" 88 IFPGA_ARG_NAME, 89 #define IFPGA_ARG_PORT "port" 90 IFPGA_ARG_PORT, 91 #define IFPGA_AFU_BTS "afu_bts" 92 IFPGA_AFU_BTS, 93 NULL 94 }; 95 96 /* 97 * Scan the content of the FPGA bus, and the devices in the devices 98 * list 99 */ 100 static struct rte_afu_device * 101 ifpga_scan_one(struct rte_rawdev *rawdev, 102 struct rte_devargs *devargs) 103 { 104 struct rte_kvargs *kvlist = NULL; 105 struct rte_afu_device *afu_dev = NULL; 106 struct rte_afu_pr_conf afu_pr_conf; 107 int ret = 0; 108 char *path = NULL; 109 110 memset(&afu_pr_conf, 0, sizeof(struct rte_afu_pr_conf)); 111 112 kvlist = rte_kvargs_parse(devargs->args, valid_args); 113 if (!kvlist) { 114 IFPGA_BUS_ERR("error when parsing param"); 115 goto end; 116 } 117 118 if (rte_kvargs_count(kvlist, IFPGA_ARG_PORT) == 1) { 119 if (rte_kvargs_process(kvlist, IFPGA_ARG_PORT, 120 ifpga_get_integer32_arg, 121 &afu_pr_conf.afu_id.port) < 0) { 122 IFPGA_BUS_ERR("error to parse %s", IFPGA_ARG_PORT); 123 goto end; 124 } 125 } else { 126 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus", 127 IFPGA_ARG_PORT); 128 goto end; 129 } 130 131 if (rte_kvargs_count(kvlist, IFPGA_AFU_BTS) == 1) { 132 if (rte_kvargs_process(kvlist, IFPGA_AFU_BTS, 133 ifpga_get_string_arg, &path) < 0) { 134 IFPGA_BUS_ERR("Failed to parse %s", IFPGA_AFU_BTS); 135 goto end; 136 } 137 afu_pr_conf.pr_enable = 1; 138 strlcpy(afu_pr_conf.bs_path, path, 139 sizeof(afu_pr_conf.bs_path)); 140 } else { 141 afu_pr_conf.pr_enable = 0; 142 } 143 144 afu_pr_conf.afu_id.uuid.uuid_low = 0; 145 afu_pr_conf.afu_id.uuid.uuid_high = 0; 146 147 if (ifpga_find_afu_dev(rawdev, &afu_pr_conf.afu_id)) 148 goto end; 149 150 afu_dev = calloc(1, sizeof(*afu_dev)); 151 if (!afu_dev) 152 goto end; 153 154 afu_dev->device.bus = &rte_ifpga_bus; 155 afu_dev->device.devargs = devargs; 156 afu_dev->device.numa_node = SOCKET_ID_ANY; 157 afu_dev->device.name = devargs->name; 158 afu_dev->rawdev = rawdev; 159 afu_dev->id.uuid.uuid_low = 0; 160 afu_dev->id.uuid.uuid_high = 0; 161 afu_dev->id.port = afu_pr_conf.afu_id.port; 162 163 /* Allocate interrupt instance */ 164 afu_dev->intr_handle = 165 rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE); 166 if (afu_dev->intr_handle == NULL) { 167 IFPGA_BUS_ERR("Failed to allocate intr handle"); 168 goto end; 169 } 170 171 if (rawdev->dev_ops && rawdev->dev_ops->dev_info_get) 172 rawdev->dev_ops->dev_info_get(rawdev, afu_dev, sizeof(*afu_dev)); 173 174 if (rawdev->dev_ops && 175 rawdev->dev_ops->dev_start && 176 rawdev->dev_ops->dev_start(rawdev)) 177 goto end; 178 179 if (rawdev->dev_ops && 180 rawdev->dev_ops->firmware_load && 181 rawdev->dev_ops->firmware_load(rawdev, 182 &afu_pr_conf)){ 183 IFPGA_BUS_ERR("firmware load error %d", ret); 184 goto end; 185 } 186 afu_dev->id.uuid.uuid_low = afu_pr_conf.afu_id.uuid.uuid_low; 187 afu_dev->id.uuid.uuid_high = afu_pr_conf.afu_id.uuid.uuid_high; 188 189 rte_kvargs_free(kvlist); 190 free(path); 191 return afu_dev; 192 193 end: 194 rte_kvargs_free(kvlist); 195 free(path); 196 if (afu_dev) { 197 rte_intr_instance_free(afu_dev->intr_handle); 198 free(afu_dev); 199 } 200 201 return NULL; 202 } 203 204 /* 205 * Scan the content of the FPGA bus, and the devices in the devices 206 * list 207 */ 208 static int 209 ifpga_scan(void) 210 { 211 struct rte_devargs *devargs; 212 struct rte_kvargs *kvlist = NULL; 213 struct rte_rawdev *rawdev = NULL; 214 char *name = NULL; 215 char name1[RTE_RAWDEV_NAME_MAX_LEN]; 216 struct rte_afu_device *afu_dev = NULL; 217 218 /* for FPGA devices we scan the devargs_list populated via cmdline */ 219 RTE_EAL_DEVARGS_FOREACH(IFPGA_ARG_NAME, devargs) { 220 if (devargs->bus != &rte_ifpga_bus) 221 continue; 222 223 kvlist = rte_kvargs_parse(devargs->args, valid_args); 224 if (!kvlist) { 225 IFPGA_BUS_ERR("error when parsing param"); 226 goto end; 227 } 228 229 if (rte_kvargs_count(kvlist, IFPGA_ARG_NAME) == 1) { 230 if (rte_kvargs_process(kvlist, IFPGA_ARG_NAME, 231 ifpga_get_string_arg, &name) < 0) { 232 IFPGA_BUS_ERR("error to parse %s", 233 IFPGA_ARG_NAME); 234 goto end; 235 } 236 } else { 237 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus", 238 IFPGA_ARG_NAME); 239 goto end; 240 } 241 242 memset(name1, 0, sizeof(name1)); 243 snprintf(name1, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", name); 244 245 rawdev = rte_rawdev_pmd_get_named_dev(name1); 246 if (!rawdev) 247 goto end; 248 249 afu_dev = ifpga_scan_one(rawdev, devargs); 250 if (afu_dev != NULL) 251 TAILQ_INSERT_TAIL(&ifpga_afu_dev_list, afu_dev, next); 252 } 253 254 end: 255 rte_kvargs_free(kvlist); 256 free(name); 257 258 return 0; 259 } 260 261 /* 262 * Match the AFU Driver and AFU Device using the ID Table 263 */ 264 static int 265 rte_afu_match(const struct rte_afu_driver *afu_drv, 266 const struct rte_afu_device *afu_dev) 267 { 268 const struct rte_afu_uuid *id_table; 269 270 for (id_table = afu_drv->id_table; 271 ((id_table->uuid_low != 0) && (id_table->uuid_high != 0)); 272 id_table++) { 273 /* check if device's identifiers match the driver's ones */ 274 if ((id_table->uuid_low != afu_dev->id.uuid.uuid_low) || 275 id_table->uuid_high != 276 afu_dev->id.uuid.uuid_high) 277 continue; 278 279 return 1; 280 } 281 282 return 0; 283 } 284 285 static int 286 ifpga_probe_one_driver(struct rte_afu_driver *drv, 287 struct rte_afu_device *afu_dev) 288 { 289 int ret; 290 291 if (!rte_afu_match(drv, afu_dev)) 292 /* Match of device and driver failed */ 293 return 1; 294 295 /* reference driver structure */ 296 afu_dev->driver = drv; 297 298 /* call the driver probe() function */ 299 ret = drv->probe(afu_dev); 300 if (ret) 301 afu_dev->driver = NULL; 302 else 303 afu_dev->device.driver = &drv->driver; 304 305 return ret; 306 } 307 308 static int 309 ifpga_probe_all_drivers(struct rte_afu_device *afu_dev) 310 { 311 struct rte_afu_driver *drv = NULL; 312 int ret = 0; 313 314 if (afu_dev == NULL) 315 return -1; 316 317 /* Check if a driver is already loaded */ 318 if (rte_dev_is_probed(&afu_dev->device)) { 319 IFPGA_BUS_DEBUG("Device %s is already probed", 320 rte_ifpga_device_name(afu_dev)); 321 return -EEXIST; 322 } 323 324 TAILQ_FOREACH(drv, &ifpga_afu_drv_list, next) { 325 ret = ifpga_probe_one_driver(drv, afu_dev); 326 if (ret < 0) 327 /* negative value is an error */ 328 return ret; 329 if (ret > 0) 330 /* positive value means driver doesn't support it */ 331 continue; 332 return 0; 333 } 334 if ((ret > 0) && (afu_dev->driver == NULL)) 335 return 0; 336 else 337 return ret; 338 } 339 340 /* 341 * Scan the content of the Intel FPGA bus, and call the probe() function for 342 * all registered drivers that have a matching entry in its id_table 343 * for discovered devices. 344 */ 345 static int 346 ifpga_probe(void) 347 { 348 struct rte_afu_device *afu_dev = NULL; 349 int ret = 0; 350 351 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) { 352 ret = ifpga_probe_all_drivers(afu_dev); 353 if (ret == -EEXIST) 354 continue; 355 if (ret < 0) 356 IFPGA_BUS_ERR("failed to initialize %s device", 357 rte_ifpga_device_name(afu_dev)); 358 } 359 360 return ret; 361 } 362 363 /* 364 * Cleanup the content of the Intel FPGA bus, and call the remove() function 365 * for all registered devices. 366 */ 367 static int 368 ifpga_cleanup(void) 369 { 370 struct rte_afu_device *afu_dev, *tmp_dev; 371 int error = 0; 372 373 RTE_TAILQ_FOREACH_SAFE(afu_dev, &ifpga_afu_dev_list, next, tmp_dev) { 374 struct rte_afu_driver *drv = afu_dev->driver; 375 int ret = 0; 376 377 if (drv == NULL || drv->remove == NULL) 378 goto free; 379 380 ret = drv->remove(afu_dev); 381 if (ret < 0) { 382 rte_errno = errno; 383 error = -1; 384 } 385 afu_dev->driver = NULL; 386 afu_dev->device.driver = NULL; 387 388 free: 389 TAILQ_REMOVE(&ifpga_afu_dev_list, afu_dev, next); 390 rte_devargs_remove(afu_dev->device.devargs); 391 rte_intr_instance_free(afu_dev->intr_handle); 392 free(afu_dev); 393 } 394 395 return error; 396 } 397 398 static int 399 ifpga_plug(struct rte_device *dev) 400 { 401 return ifpga_probe_all_drivers(RTE_DEV_TO_AFU(dev)); 402 } 403 404 static int 405 ifpga_remove_driver(struct rte_afu_device *afu_dev) 406 { 407 const char *name; 408 409 name = rte_ifpga_device_name(afu_dev); 410 if (afu_dev->driver == NULL) { 411 IFPGA_BUS_DEBUG("no driver attach to device %s", name); 412 return 1; 413 } 414 415 return afu_dev->driver->remove(afu_dev); 416 } 417 418 static int 419 ifpga_unplug(struct rte_device *dev) 420 { 421 struct rte_afu_device *afu_dev = NULL; 422 int ret; 423 424 if (dev == NULL) 425 return -EINVAL; 426 427 afu_dev = RTE_DEV_TO_AFU(dev); 428 if (!afu_dev) 429 return -ENOENT; 430 431 ret = ifpga_remove_driver(afu_dev); 432 if (ret) 433 return ret; 434 435 TAILQ_REMOVE(&ifpga_afu_dev_list, afu_dev, next); 436 437 rte_devargs_remove(dev->devargs); 438 rte_intr_instance_free(afu_dev->intr_handle); 439 free(afu_dev); 440 return 0; 441 442 } 443 444 static struct rte_device * 445 ifpga_find_device(const struct rte_device *start, 446 rte_dev_cmp_t cmp, const void *data) 447 { 448 struct rte_afu_device *afu_dev; 449 450 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) { 451 if (start && &afu_dev->device == start) { 452 start = NULL; 453 continue; 454 } 455 if (cmp(&afu_dev->device, data) == 0) 456 return &afu_dev->device; 457 } 458 459 return NULL; 460 } 461 static int 462 ifpga_parse(const char *name, void *addr) 463 { 464 int *out = addr; 465 struct rte_rawdev *rawdev = NULL; 466 char rawdev_name[RTE_RAWDEV_NAME_MAX_LEN]; 467 char *c1 = NULL; 468 char *c2 = NULL; 469 int port = IFPGA_BUS_DEV_PORT_MAX; 470 char str_port[8]; 471 int str_port_len = 0; 472 int ret; 473 474 memset(str_port, 0, 8); 475 c1 = strchr(name, '|'); 476 if (c1 != NULL) { 477 str_port_len = c1 - name; 478 c2 = c1 + 1; 479 } 480 481 if (str_port_len < 8 && 482 str_port_len > 0) { 483 memcpy(str_port, name, str_port_len); 484 ret = sscanf(str_port, "%d", &port); 485 if (ret == -1) 486 return 0; 487 } 488 489 memset(rawdev_name, 0, sizeof(rawdev_name)); 490 snprintf(rawdev_name, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", c2); 491 rawdev = rte_rawdev_pmd_get_named_dev(rawdev_name); 492 493 if ((port < IFPGA_BUS_DEV_PORT_MAX) && 494 rawdev && 495 (addr != NULL)) 496 *out = port; 497 498 if ((port < IFPGA_BUS_DEV_PORT_MAX) && 499 rawdev) 500 return 0; 501 else 502 return 1; 503 } 504 505 static struct rte_bus rte_ifpga_bus = { 506 .scan = ifpga_scan, 507 .probe = ifpga_probe, 508 .cleanup = ifpga_cleanup, 509 .find_device = ifpga_find_device, 510 .plug = ifpga_plug, 511 .unplug = ifpga_unplug, 512 .parse = ifpga_parse, 513 }; 514 515 RTE_REGISTER_BUS(IFPGA_BUS_NAME, rte_ifpga_bus); 516 RTE_LOG_REGISTER_DEFAULT(ifpga_bus_logtype, NOTICE); 517