xref: /spdk/module/event/subsystems/vmd/vmd.c (revision a6dbe3721eb3b5990707fc3e378c95e505dd8ab5)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2019 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #include "spdk/stdinc.h"
7 
8 #include "spdk/json.h"
9 #include "spdk/thread.h"
10 #include "spdk/likely.h"
11 #include "spdk/log.h"
12 
13 #include "spdk/vmd.h"
14 
15 #include "spdk_internal/init.h"
16 #include "event_vmd.h"
17 
18 static struct spdk_poller *g_hotplug_poller;
19 static bool g_enabled;
20 
21 void
vmd_subsystem_enable(void)22 vmd_subsystem_enable(void)
23 {
24 	g_enabled = true;
25 }
26 
27 bool
vmd_subsystem_is_enabled(void)28 vmd_subsystem_is_enabled(void)
29 {
30 	return g_enabled;
31 }
32 
33 static int
vmd_hotplug_monitor(void * ctx)34 vmd_hotplug_monitor(void *ctx)
35 {
36 	return spdk_vmd_hotplug_monitor();
37 }
38 
39 static void
vmd_subsystem_init(void)40 vmd_subsystem_init(void)
41 {
42 	int rc = 0;
43 
44 	if (!g_enabled) {
45 		goto out;
46 	}
47 
48 	rc = spdk_vmd_init();
49 	if (spdk_likely(rc != 0)) {
50 		SPDK_ERRLOG("Failed to initialize the VMD library\n");
51 		goto out;
52 	}
53 
54 	assert(g_hotplug_poller == NULL);
55 
56 	g_hotplug_poller = SPDK_POLLER_REGISTER(vmd_hotplug_monitor, NULL, 1000000ULL);
57 	if (g_hotplug_poller == NULL) {
58 		SPDK_ERRLOG("Failed to register hotplug monitor poller\n");
59 		rc = -ENOMEM;
60 		goto out;
61 	}
62 out:
63 	spdk_subsystem_init_next(rc);
64 }
65 
66 static void
vmd_subsystem_fini(void)67 vmd_subsystem_fini(void)
68 {
69 	spdk_poller_unregister(&g_hotplug_poller);
70 
71 	spdk_vmd_fini();
72 
73 	spdk_subsystem_fini_next();
74 }
75 
76 static void
vmd_write_config_json(struct spdk_json_write_ctx * w)77 vmd_write_config_json(struct spdk_json_write_ctx *w)
78 {
79 	spdk_json_write_array_begin(w);
80 
81 	if (g_enabled) {
82 		spdk_json_write_object_begin(w);
83 		spdk_json_write_named_string(w, "method", "vmd_enable");
84 		spdk_json_write_named_object_begin(w, "params");
85 		spdk_json_write_object_end(w);
86 		spdk_json_write_object_end(w);
87 	}
88 
89 	spdk_json_write_array_end(w);
90 }
91 
92 static struct spdk_subsystem g_spdk_subsystem_vmd = {
93 	.name = "vmd",
94 	.init = vmd_subsystem_init,
95 	.fini = vmd_subsystem_fini,
96 	.write_config_json = vmd_write_config_json,
97 };
98 
99 SPDK_SUBSYSTEM_REGISTER(g_spdk_subsystem_vmd);
100