xref: /spdk/lib/ftl/utils/ftl_property.c (revision 309682ca440b54ff500d4c51e4eebe21b01df887)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright 2023 Solidigm All Rights Reserved
3  */
4 
5 #include "spdk/queue.h"
6 #include "spdk/json.h"
7 #include "spdk/jsonrpc.h"
8 
9 #include "ftl_core.h"
10 #include "ftl_property.h"
11 
12 struct ftl_properties {
13 	LIST_HEAD(, ftl_property) list;
14 };
15 
16 /**
17  * @brief FTL property descriptor
18  */
19 struct ftl_property {
20 	/** Name of the property */
21 	const char *name;
22 
23 	/** Link to put the property to the list */
24 	LIST_ENTRY(ftl_property) entry;
25 };
26 
27 static struct ftl_property *
28 get_property_item(struct ftl_properties *properties, const char *name)
29 {
30 	struct ftl_property *entry;
31 
32 	LIST_FOREACH(entry, &properties->list, entry) {
33 		/* TODO think about strncmp */
34 		if (0 == strcmp(entry->name, name)) {
35 			return entry;
36 		}
37 	}
38 
39 	return NULL;
40 }
41 
42 void
43 ftl_property_register(struct spdk_ftl_dev *dev, const char *name)
44 {
45 	struct ftl_properties *properties = dev->properties;
46 
47 	if (get_property_item(properties, name)) {
48 		FTL_ERRLOG(dev, "FTL property registration ERROR, already exist, name %s\n", name);
49 		ftl_abort();
50 	} else {
51 		struct ftl_property *prop = calloc(1, sizeof(*prop));
52 		if (NULL == prop) {
53 			FTL_ERRLOG(dev, "FTL property registration ERROR, out of memory, name %s\n", name);
54 			ftl_abort();
55 		}
56 
57 		prop->name = name;
58 		LIST_INSERT_HEAD(&properties->list, prop, entry);
59 	}
60 }
61 
62 int
63 ftl_properties_init(struct spdk_ftl_dev *dev)
64 {
65 	dev->properties = calloc(1, sizeof(*dev->properties));
66 	if (!dev->properties) {
67 		return -ENOMEM;
68 	}
69 
70 	LIST_INIT(&dev->properties->list);
71 	return 0;
72 }
73 
74 void
75 ftl_properties_deinit(struct spdk_ftl_dev *dev)
76 {
77 	struct ftl_properties *properties = dev->properties;
78 	struct ftl_property *prop;
79 
80 	if (!properties) {
81 		return;
82 	}
83 
84 	while (!LIST_EMPTY(&properties->list)) {
85 		prop = LIST_FIRST(&properties->list);
86 		LIST_REMOVE(prop, entry);
87 		free(prop);
88 	}
89 
90 	free(dev->properties);
91 }
92 
93 void
94 ftl_property_dump(struct spdk_ftl_dev *dev, struct spdk_jsonrpc_request *request)
95 {
96 	struct ftl_properties *properties = dev->properties;
97 	struct ftl_property *prop;
98 	struct spdk_json_write_ctx *w;
99 
100 	w = spdk_jsonrpc_begin_result(request);
101 	spdk_json_write_object_begin(w);
102 	spdk_json_write_named_string(w, "name", dev->conf.name);
103 
104 	spdk_json_write_named_object_begin(w, "properties");
105 	LIST_FOREACH(prop, &properties->list, entry) {
106 		/* TODO dump the value as well */
107 		spdk_json_write_named_string(w, prop->name, "");
108 	}
109 	spdk_json_write_object_end(w);
110 
111 	spdk_json_write_object_end(w);
112 	spdk_jsonrpc_end_result(request, w);
113 }
114