|
38 | 38 |
|
39 | 39 | #include <libnvme.h> |
40 | 40 |
|
| 41 | +#ifdef HAVE_LIBKMOD |
| 42 | +#include <libkmod.h> |
| 43 | +#endif |
| 44 | + |
41 | 45 | #include "common.h" |
42 | 46 | #include "nvme.h" |
43 | 47 | #include "nvme-print.h" |
@@ -469,6 +473,50 @@ static int nvme_read_config_checked(struct libnvme_global_ctx *ctx, |
469 | 473 | return libnvme_read_config(ctx, filename); |
470 | 474 | } |
471 | 475 |
|
| 476 | +static void load_nvme_fabrics_module(void) |
| 477 | +{ |
| 478 | +#ifdef HAVE_LIBKMOD |
| 479 | + struct kmod_ctx *ctx; |
| 480 | + struct kmod_module *mod; |
| 481 | + int err, state; |
| 482 | + int timeout = 20; /* 2 seconds */ |
| 483 | + |
| 484 | + ctx = kmod_new(NULL, NULL); |
| 485 | + if (!ctx) |
| 486 | + return; |
| 487 | + |
| 488 | + err = kmod_module_new_from_name(ctx, "nvme-fabrics", &mod); |
| 489 | + if (err) |
| 490 | + goto unref; |
| 491 | + |
| 492 | + state = kmod_module_get_initstate(mod); |
| 493 | + if (state != KMOD_MODULE_LIVE && state != KMOD_MODULE_BUILTIN) { |
| 494 | + err = kmod_module_probe_insert_module(mod, |
| 495 | + KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL); |
| 496 | + if (err) |
| 497 | + goto mod_unref; |
| 498 | + |
| 499 | + while (timeout--) { |
| 500 | + state = kmod_module_get_initstate(mod); |
| 501 | + if (state == KMOD_MODULE_LIVE) |
| 502 | + goto mod_unref; |
| 503 | + |
| 504 | + /* 100 ms */ |
| 505 | + usleep(100 * 1000); |
| 506 | + } |
| 507 | + err = -ENOENT; |
| 508 | + } |
| 509 | + |
| 510 | +mod_unref: |
| 511 | + kmod_module_unref(mod); |
| 512 | +unref: |
| 513 | + kmod_unref(ctx); |
| 514 | + |
| 515 | + if (err) |
| 516 | + fprintf(stderr, "Couldn't load the nvme-fabrics module\n"); |
| 517 | +#endif |
| 518 | +} |
| 519 | + |
472 | 520 | #define NBFT_SYSFS_PATH "/sys/firmware/acpi/tables" |
473 | 521 |
|
474 | 522 | int fabrics_discovery(const char *desc, int argc, char **argv, bool connect) |
@@ -502,6 +550,8 @@ int fabrics_discovery(const char *desc, int argc, char **argv, bool connect) |
502 | 550 |
|
503 | 551 | libnvmf_default_config(&cfg); |
504 | 552 |
|
| 553 | + load_nvme_fabrics_module(); |
| 554 | + |
505 | 555 | ret = argconfig_parse(argc, argv, desc, opts); |
506 | 556 | if (ret) |
507 | 557 | return ret; |
@@ -603,6 +653,8 @@ int fabrics_connect(const char *desc, int argc, char **argv) |
603 | 653 |
|
604 | 654 | libnvmf_default_config(&cfg); |
605 | 655 |
|
| 656 | + load_nvme_fabrics_module(); |
| 657 | + |
606 | 658 | ret = argconfig_parse(argc, argv, desc, opts); |
607 | 659 | if (ret) |
608 | 660 | return ret; |
|
0 commit comments