xref: /freebsd/sys/dev/nvme/nvme_sysctl.c (revision 206b73d0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2012-2016 Intel Corporation
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_nvme.h"
33 
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/sysctl.h>
37 
38 #include "nvme_private.h"
39 
40 #ifndef NVME_USE_NVD
41 #define NVME_USE_NVD 1
42 #endif
43 
44 int nvme_use_nvd = NVME_USE_NVD;
45 bool nvme_verbose_cmd_dump = false;
46 
47 SYSCTL_NODE(_hw, OID_AUTO, nvme, CTLFLAG_RD, 0, "NVMe sysctl tunables");
48 SYSCTL_INT(_hw_nvme, OID_AUTO, use_nvd, CTLFLAG_RDTUN,
49     &nvme_use_nvd, 1, "1 = Create NVD devices, 0 = Create NDA devices");
50 SYSCTL_BOOL(_hw_nvme, OID_AUTO, verbose_cmd_dump, CTLFLAG_RWTUN,
51     &nvme_verbose_cmd_dump, 0,
52     "enable verbose command printting when a command fails");
53 
54 /*
55  * CTLTYPE_S64 and sysctl_handle_64 were added in r217616.  Define these
56  *  explicitly here for older kernels that don't include the r217616
57  *  changeset.
58  */
59 #ifndef CTLTYPE_S64
60 #define CTLTYPE_S64		CTLTYPE_QUAD
61 #define sysctl_handle_64	sysctl_handle_quad
62 #endif
63 
64 static void
65 nvme_dump_queue(struct nvme_qpair *qpair)
66 {
67 	struct nvme_completion *cpl;
68 	struct nvme_command *cmd;
69 	int i;
70 
71 	printf("id:%04Xh phase:%d\n", qpair->id, qpair->phase);
72 
73 	printf("Completion queue:\n");
74 	for (i = 0; i < qpair->num_entries; i++) {
75 		cpl = &qpair->cpl[i];
76 		printf("%05d: ", i);
77 		nvme_dump_completion(cpl);
78 	}
79 
80 	printf("Submission queue:\n");
81 	for (i = 0; i < qpair->num_entries; i++) {
82 		cmd = &qpair->cmd[i];
83 		printf("%05d: ", i);
84 		nvme_dump_command(cmd);
85 	}
86 }
87 
88 
89 static int
90 nvme_sysctl_dump_debug(SYSCTL_HANDLER_ARGS)
91 {
92 	struct nvme_qpair 	*qpair = arg1;
93 	uint32_t		val = 0;
94 
95 	int error = sysctl_handle_int(oidp, &val, 0, req);
96 
97 	if (error)
98 		return (error);
99 
100 	if (val != 0)
101 		nvme_dump_queue(qpair);
102 
103 	return (0);
104 }
105 
106 static int
107 nvme_sysctl_int_coal_time(SYSCTL_HANDLER_ARGS)
108 {
109 	struct nvme_controller *ctrlr = arg1;
110 	uint32_t oldval = ctrlr->int_coal_time;
111 	int error = sysctl_handle_int(oidp, &ctrlr->int_coal_time, 0,
112 	    req);
113 
114 	if (error)
115 		return (error);
116 
117 	if (oldval != ctrlr->int_coal_time)
118 		nvme_ctrlr_cmd_set_interrupt_coalescing(ctrlr,
119 		    ctrlr->int_coal_time, ctrlr->int_coal_threshold, NULL,
120 		    NULL);
121 
122 	return (0);
123 }
124 
125 static int
126 nvme_sysctl_int_coal_threshold(SYSCTL_HANDLER_ARGS)
127 {
128 	struct nvme_controller *ctrlr = arg1;
129 	uint32_t oldval = ctrlr->int_coal_threshold;
130 	int error = sysctl_handle_int(oidp, &ctrlr->int_coal_threshold, 0,
131 	    req);
132 
133 	if (error)
134 		return (error);
135 
136 	if (oldval != ctrlr->int_coal_threshold)
137 		nvme_ctrlr_cmd_set_interrupt_coalescing(ctrlr,
138 		    ctrlr->int_coal_time, ctrlr->int_coal_threshold, NULL,
139 		    NULL);
140 
141 	return (0);
142 }
143 
144 static int
145 nvme_sysctl_timeout_period(SYSCTL_HANDLER_ARGS)
146 {
147 	struct nvme_controller *ctrlr = arg1;
148 	uint32_t oldval = ctrlr->timeout_period;
149 	int error = sysctl_handle_int(oidp, &ctrlr->timeout_period, 0, req);
150 
151 	if (error)
152 		return (error);
153 
154 	if (ctrlr->timeout_period > NVME_MAX_TIMEOUT_PERIOD ||
155 	    ctrlr->timeout_period < NVME_MIN_TIMEOUT_PERIOD) {
156 		ctrlr->timeout_period = oldval;
157 		return (EINVAL);
158 	}
159 
160 	return (0);
161 }
162 
163 static void
164 nvme_qpair_reset_stats(struct nvme_qpair *qpair)
165 {
166 
167 	qpair->num_cmds = 0;
168 	qpair->num_intr_handler_calls = 0;
169 	qpair->num_retries = 0;
170 	qpair->num_failures = 0;
171 }
172 
173 static int
174 nvme_sysctl_num_cmds(SYSCTL_HANDLER_ARGS)
175 {
176 	struct nvme_controller 	*ctrlr = arg1;
177 	int64_t			num_cmds = 0;
178 	int			i;
179 
180 	num_cmds = ctrlr->adminq.num_cmds;
181 
182 	for (i = 0; i < ctrlr->num_io_queues; i++)
183 		num_cmds += ctrlr->ioq[i].num_cmds;
184 
185 	return (sysctl_handle_64(oidp, &num_cmds, 0, req));
186 }
187 
188 static int
189 nvme_sysctl_num_intr_handler_calls(SYSCTL_HANDLER_ARGS)
190 {
191 	struct nvme_controller 	*ctrlr = arg1;
192 	int64_t			num_intr_handler_calls = 0;
193 	int			i;
194 
195 	num_intr_handler_calls = ctrlr->adminq.num_intr_handler_calls;
196 
197 	for (i = 0; i < ctrlr->num_io_queues; i++)
198 		num_intr_handler_calls += ctrlr->ioq[i].num_intr_handler_calls;
199 
200 	return (sysctl_handle_64(oidp, &num_intr_handler_calls, 0, req));
201 }
202 
203 static int
204 nvme_sysctl_num_retries(SYSCTL_HANDLER_ARGS)
205 {
206 	struct nvme_controller 	*ctrlr = arg1;
207 	int64_t			num_retries = 0;
208 	int			i;
209 
210 	num_retries = ctrlr->adminq.num_retries;
211 
212 	for (i = 0; i < ctrlr->num_io_queues; i++)
213 		num_retries += ctrlr->ioq[i].num_retries;
214 
215 	return (sysctl_handle_64(oidp, &num_retries, 0, req));
216 }
217 
218 static int
219 nvme_sysctl_num_failures(SYSCTL_HANDLER_ARGS)
220 {
221 	struct nvme_controller 	*ctrlr = arg1;
222 	int64_t			num_failures = 0;
223 	int			i;
224 
225 	num_failures = ctrlr->adminq.num_failures;
226 
227 	for (i = 0; i < ctrlr->num_io_queues; i++)
228 		num_failures += ctrlr->ioq[i].num_failures;
229 
230 	return (sysctl_handle_64(oidp, &num_failures, 0, req));
231 }
232 
233 static int
234 nvme_sysctl_reset_stats(SYSCTL_HANDLER_ARGS)
235 {
236 	struct nvme_controller 	*ctrlr = arg1;
237 	uint32_t		i, val = 0;
238 
239 	int error = sysctl_handle_int(oidp, &val, 0, req);
240 
241 	if (error)
242 		return (error);
243 
244 	if (val != 0) {
245 		nvme_qpair_reset_stats(&ctrlr->adminq);
246 
247 		for (i = 0; i < ctrlr->num_io_queues; i++)
248 			nvme_qpair_reset_stats(&ctrlr->ioq[i]);
249 	}
250 
251 	return (0);
252 }
253 
254 
255 static void
256 nvme_sysctl_initialize_queue(struct nvme_qpair *qpair,
257     struct sysctl_ctx_list *ctrlr_ctx, struct sysctl_oid *que_tree)
258 {
259 	struct sysctl_oid_list	*que_list = SYSCTL_CHILDREN(que_tree);
260 
261 	SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "num_entries",
262 	    CTLFLAG_RD, &qpair->num_entries, 0,
263 	    "Number of entries in hardware queue");
264 	SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "num_trackers",
265 	    CTLFLAG_RD, &qpair->num_trackers, 0,
266 	    "Number of trackers pre-allocated for this queue pair");
267 	SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "sq_head",
268 	    CTLFLAG_RD, &qpair->sq_head, 0,
269 	    "Current head of submission queue (as observed by driver)");
270 	SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "sq_tail",
271 	    CTLFLAG_RD, &qpair->sq_tail, 0,
272 	    "Current tail of submission queue (as observed by driver)");
273 	SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "cq_head",
274 	    CTLFLAG_RD, &qpair->cq_head, 0,
275 	    "Current head of completion queue (as observed by driver)");
276 
277 	SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_cmds",
278 	    CTLFLAG_RD, &qpair->num_cmds, "Number of commands submitted");
279 	SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_intr_handler_calls",
280 	    CTLFLAG_RD, &qpair->num_intr_handler_calls,
281 	    "Number of times interrupt handler was invoked (will typically be "
282 	    "less than number of actual interrupts generated due to "
283 	    "coalescing)");
284 	SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_retries",
285 	    CTLFLAG_RD, &qpair->num_retries, "Number of commands retried");
286 	SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_failures",
287 	    CTLFLAG_RD, &qpair->num_failures,
288 	    "Number of commands ending in failure after all retries");
289 
290 	SYSCTL_ADD_PROC(ctrlr_ctx, que_list, OID_AUTO,
291 	    "dump_debug", CTLTYPE_UINT | CTLFLAG_RW, qpair, 0,
292 	    nvme_sysctl_dump_debug, "IU", "Dump debug data");
293 }
294 
295 void
296 nvme_sysctl_initialize_ctrlr(struct nvme_controller *ctrlr)
297 {
298 	struct sysctl_ctx_list	*ctrlr_ctx;
299 	struct sysctl_oid	*ctrlr_tree, *que_tree;
300 	struct sysctl_oid_list	*ctrlr_list;
301 #define QUEUE_NAME_LENGTH	16
302 	char			queue_name[QUEUE_NAME_LENGTH];
303 	int			i;
304 
305 	ctrlr_ctx = device_get_sysctl_ctx(ctrlr->dev);
306 	ctrlr_tree = device_get_sysctl_tree(ctrlr->dev);
307 	ctrlr_list = SYSCTL_CHILDREN(ctrlr_tree);
308 
309 	SYSCTL_ADD_UINT(ctrlr_ctx, ctrlr_list, OID_AUTO, "num_cpus_per_ioq",
310 	    CTLFLAG_RD, &ctrlr->num_cpus_per_ioq, 0,
311 	    "Number of CPUs assigned per I/O queue pair");
312 
313 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
314 	    "int_coal_time", CTLTYPE_UINT | CTLFLAG_RW, ctrlr, 0,
315 	    nvme_sysctl_int_coal_time, "IU",
316 	    "Interrupt coalescing timeout (in microseconds)");
317 
318 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
319 	    "int_coal_threshold", CTLTYPE_UINT | CTLFLAG_RW, ctrlr, 0,
320 	    nvme_sysctl_int_coal_threshold, "IU",
321 	    "Interrupt coalescing threshold");
322 
323 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
324 	    "timeout_period", CTLTYPE_UINT | CTLFLAG_RW, ctrlr, 0,
325 	    nvme_sysctl_timeout_period, "IU",
326 	    "Timeout period (in seconds)");
327 
328 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
329 	    "num_cmds", CTLTYPE_S64 | CTLFLAG_RD,
330 	    ctrlr, 0, nvme_sysctl_num_cmds, "IU",
331 	    "Number of commands submitted");
332 
333 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
334 	    "num_intr_handler_calls", CTLTYPE_S64 | CTLFLAG_RD,
335 	    ctrlr, 0, nvme_sysctl_num_intr_handler_calls, "IU",
336 	    "Number of times interrupt handler was invoked (will "
337 	    "typically be less than number of actual interrupts "
338 	    "generated due to coalescing)");
339 
340 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
341 	    "num_retries", CTLTYPE_S64 | CTLFLAG_RD,
342 	    ctrlr, 0, nvme_sysctl_num_retries, "IU",
343 	    "Number of commands retried");
344 
345 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
346 	    "num_failures", CTLTYPE_S64 | CTLFLAG_RD,
347 	    ctrlr, 0, nvme_sysctl_num_failures, "IU",
348 	    "Number of commands ending in failure after all retries");
349 
350 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
351 	    "reset_stats", CTLTYPE_UINT | CTLFLAG_RW, ctrlr, 0,
352 	    nvme_sysctl_reset_stats, "IU", "Reset statistics to zero");
353 
354 	que_tree = SYSCTL_ADD_NODE(ctrlr_ctx, ctrlr_list, OID_AUTO, "adminq",
355 	    CTLFLAG_RD, NULL, "Admin Queue");
356 
357 	nvme_sysctl_initialize_queue(&ctrlr->adminq, ctrlr_ctx, que_tree);
358 
359 	for (i = 0; i < ctrlr->num_io_queues; i++) {
360 		snprintf(queue_name, QUEUE_NAME_LENGTH, "ioq%d", i);
361 		que_tree = SYSCTL_ADD_NODE(ctrlr_ctx, ctrlr_list, OID_AUTO,
362 		    queue_name, CTLFLAG_RD, NULL, "IO Queue");
363 		nvme_sysctl_initialize_queue(&ctrlr->ioq[i], ctrlr_ctx,
364 		    que_tree);
365 	}
366 }
367