xref: /freebsd/sys/dev/nvme/nvme_sysctl.c (revision 397e83df75e0fcd0d3fcb95ae4d794cb7600fc89)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #include "opt_nvme.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/sysctl.h>
36 
37 #include "nvme_private.h"
38 
39 #ifndef NVME_USE_NVD
40 #define NVME_USE_NVD 0
41 #endif
42 
43 int nvme_use_nvd = NVME_USE_NVD;
44 bool nvme_verbose_cmd_dump = false;
45 
46 SYSCTL_NODE(_hw, OID_AUTO, nvme, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
47     "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 printing when a command fails");
53 
54 static void
55 nvme_dump_queue(struct nvme_qpair *qpair)
56 {
57 	struct nvme_completion *cpl;
58 	struct nvme_command *cmd;
59 	int i;
60 
61 	printf("id:%04Xh phase:%d\n", qpair->id, qpair->phase);
62 
63 	printf("Completion queue:\n");
64 	for (i = 0; i < qpair->num_entries; i++) {
65 		cpl = &qpair->cpl[i];
66 		printf("%05d: ", i);
67 		nvme_qpair_print_completion(qpair, cpl);
68 	}
69 
70 	printf("Submission queue:\n");
71 	for (i = 0; i < qpair->num_entries; i++) {
72 		cmd = &qpair->cmd[i];
73 		printf("%05d: ", i);
74 		nvme_qpair_print_command(qpair, cmd);
75 	}
76 }
77 
78 static int
79 nvme_sysctl_dump_debug(SYSCTL_HANDLER_ARGS)
80 {
81 	struct nvme_qpair 	*qpair = arg1;
82 	uint32_t		val = 0;
83 
84 	int error = sysctl_handle_int(oidp, &val, 0, req);
85 
86 	if (error)
87 		return (error);
88 
89 	if (val != 0)
90 		nvme_dump_queue(qpair);
91 
92 	return (0);
93 }
94 
95 static int
96 nvme_sysctl_int_coal_time(SYSCTL_HANDLER_ARGS)
97 {
98 	struct nvme_controller *ctrlr = arg1;
99 	uint32_t oldval = ctrlr->int_coal_time;
100 	int error = sysctl_handle_int(oidp, &ctrlr->int_coal_time, 0,
101 	    req);
102 
103 	if (error)
104 		return (error);
105 
106 	if (oldval != ctrlr->int_coal_time)
107 		nvme_ctrlr_cmd_set_interrupt_coalescing(ctrlr,
108 		    ctrlr->int_coal_time, ctrlr->int_coal_threshold, NULL,
109 		    NULL);
110 
111 	return (0);
112 }
113 
114 static int
115 nvme_sysctl_int_coal_threshold(SYSCTL_HANDLER_ARGS)
116 {
117 	struct nvme_controller *ctrlr = arg1;
118 	uint32_t oldval = ctrlr->int_coal_threshold;
119 	int error = sysctl_handle_int(oidp, &ctrlr->int_coal_threshold, 0,
120 	    req);
121 
122 	if (error)
123 		return (error);
124 
125 	if (oldval != ctrlr->int_coal_threshold)
126 		nvme_ctrlr_cmd_set_interrupt_coalescing(ctrlr,
127 		    ctrlr->int_coal_time, ctrlr->int_coal_threshold, NULL,
128 		    NULL);
129 
130 	return (0);
131 }
132 
133 static int
134 nvme_sysctl_timeout_period(SYSCTL_HANDLER_ARGS)
135 {
136 	uint32_t *ptr = arg1;
137 	uint32_t newval = *ptr;
138 	int error = sysctl_handle_int(oidp, &newval, 0, req);
139 
140 	if (error || (req->newptr == NULL))
141 		return (error);
142 
143 	if (newval > NVME_MAX_TIMEOUT_PERIOD ||
144 	    newval < NVME_MIN_TIMEOUT_PERIOD) {
145 		return (EINVAL);
146 	} else {
147 		*ptr = newval;
148 	}
149 
150 	return (0);
151 }
152 
153 static void
154 nvme_qpair_reset_stats(struct nvme_qpair *qpair)
155 {
156 
157 	/*
158 	 * Reset the values. Due to sanity checks in
159 	 * nvme_qpair_process_completions, we reset the number of interrupt
160 	 * calls to 1.
161 	 */
162 	qpair->num_cmds = 0;
163 	qpair->num_intr_handler_calls = 1;
164 	qpair->num_retries = 0;
165 	qpair->num_failures = 0;
166 	qpair->num_ignored = 0;
167 	qpair->num_recovery_nolock = 0;
168 }
169 
170 static int
171 nvme_sysctl_num_cmds(SYSCTL_HANDLER_ARGS)
172 {
173 	struct nvme_controller 	*ctrlr = arg1;
174 	int64_t			num_cmds = 0;
175 	int			i;
176 
177 	num_cmds = ctrlr->adminq.num_cmds;
178 
179 	for (i = 0; i < ctrlr->num_io_queues; i++)
180 		num_cmds += ctrlr->ioq[i].num_cmds;
181 
182 	return (sysctl_handle_64(oidp, &num_cmds, 0, req));
183 }
184 
185 static int
186 nvme_sysctl_num_intr_handler_calls(SYSCTL_HANDLER_ARGS)
187 {
188 	struct nvme_controller 	*ctrlr = arg1;
189 	int64_t			num_intr_handler_calls = 0;
190 	int			i;
191 
192 	num_intr_handler_calls = ctrlr->adminq.num_intr_handler_calls;
193 
194 	for (i = 0; i < ctrlr->num_io_queues; i++)
195 		num_intr_handler_calls += ctrlr->ioq[i].num_intr_handler_calls;
196 
197 	return (sysctl_handle_64(oidp, &num_intr_handler_calls, 0, req));
198 }
199 
200 static int
201 nvme_sysctl_num_retries(SYSCTL_HANDLER_ARGS)
202 {
203 	struct nvme_controller 	*ctrlr = arg1;
204 	int64_t			num_retries = 0;
205 	int			i;
206 
207 	num_retries = ctrlr->adminq.num_retries;
208 
209 	for (i = 0; i < ctrlr->num_io_queues; i++)
210 		num_retries += ctrlr->ioq[i].num_retries;
211 
212 	return (sysctl_handle_64(oidp, &num_retries, 0, req));
213 }
214 
215 static int
216 nvme_sysctl_num_failures(SYSCTL_HANDLER_ARGS)
217 {
218 	struct nvme_controller 	*ctrlr = arg1;
219 	int64_t			num_failures = 0;
220 	int			i;
221 
222 	num_failures = ctrlr->adminq.num_failures;
223 
224 	for (i = 0; i < ctrlr->num_io_queues; i++)
225 		num_failures += ctrlr->ioq[i].num_failures;
226 
227 	return (sysctl_handle_64(oidp, &num_failures, 0, req));
228 }
229 
230 static int
231 nvme_sysctl_num_ignored(SYSCTL_HANDLER_ARGS)
232 {
233 	struct nvme_controller 	*ctrlr = arg1;
234 	int64_t			num_ignored = 0;
235 	int			i;
236 
237 	num_ignored = ctrlr->adminq.num_ignored;
238 
239 	for (i = 0; i < ctrlr->num_io_queues; i++)
240 		num_ignored += ctrlr->ioq[i].num_ignored;
241 
242 	return (sysctl_handle_64(oidp, &num_ignored, 0, req));
243 }
244 
245 static int
246 nvme_sysctl_num_recovery_nolock(SYSCTL_HANDLER_ARGS)
247 {
248 	struct nvme_controller 	*ctrlr = arg1;
249 	int64_t			num;
250 	int			i;
251 
252 	num = ctrlr->adminq.num_recovery_nolock;
253 
254 	for (i = 0; i < ctrlr->num_io_queues; i++)
255 		num += ctrlr->ioq[i].num_recovery_nolock;
256 
257 	return (sysctl_handle_64(oidp, &num, 0, req));
258 }
259 
260 static int
261 nvme_sysctl_reset_stats(SYSCTL_HANDLER_ARGS)
262 {
263 	struct nvme_controller 	*ctrlr = arg1;
264 	uint32_t		i, val = 0;
265 
266 	int error = sysctl_handle_int(oidp, &val, 0, req);
267 
268 	if (error)
269 		return (error);
270 
271 	if (val != 0) {
272 		nvme_qpair_reset_stats(&ctrlr->adminq);
273 
274 		for (i = 0; i < ctrlr->num_io_queues; i++)
275 			nvme_qpair_reset_stats(&ctrlr->ioq[i]);
276 	}
277 
278 	return (0);
279 }
280 
281 static void
282 nvme_sysctl_initialize_queue(struct nvme_qpair *qpair,
283     struct sysctl_ctx_list *ctrlr_ctx, struct sysctl_oid *que_tree)
284 {
285 	struct sysctl_oid_list	*que_list = SYSCTL_CHILDREN(que_tree);
286 
287 	SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "num_entries",
288 	    CTLFLAG_RD, &qpair->num_entries, 0,
289 	    "Number of entries in hardware queue");
290 	SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "num_trackers",
291 	    CTLFLAG_RD, &qpair->num_trackers, 0,
292 	    "Number of trackers pre-allocated for this queue pair");
293 	SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "sq_head",
294 	    CTLFLAG_RD, &qpair->sq_head, 0,
295 	    "Current head of submission queue (as observed by driver)");
296 	SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "sq_tail",
297 	    CTLFLAG_RD, &qpair->sq_tail, 0,
298 	    "Current tail of submission queue (as observed by driver)");
299 	SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "cq_head",
300 	    CTLFLAG_RD, &qpair->cq_head, 0,
301 	    "Current head of completion queue (as observed by driver)");
302 
303 	SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_cmds",
304 	    CTLFLAG_RD, &qpair->num_cmds, "Number of commands submitted");
305 	SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_intr_handler_calls",
306 	    CTLFLAG_RD, &qpair->num_intr_handler_calls,
307 	    "Number of times interrupt handler was invoked (will typically be "
308 	    "less than number of actual interrupts generated due to "
309 	    "coalescing)");
310 	SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_retries",
311 	    CTLFLAG_RD, &qpair->num_retries, "Number of commands retried");
312 	SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_failures",
313 	    CTLFLAG_RD, &qpair->num_failures,
314 	    "Number of commands ending in failure after all retries");
315 	SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_ignored",
316 	    CTLFLAG_RD, &qpair->num_ignored,
317 	    "Number of interrupts posted, but were administratively ignored");
318 	SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_recovery_nolock",
319 	    CTLFLAG_RD, &qpair->num_recovery_nolock,
320 	    "Number of times that we failed to lock recovery in the ISR");
321 
322 	SYSCTL_ADD_PROC(ctrlr_ctx, que_list, OID_AUTO,
323 	    "dump_debug", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
324 	    qpair, 0, nvme_sysctl_dump_debug, "IU", "Dump debug data");
325 }
326 
327 void
328 nvme_sysctl_initialize_ctrlr(struct nvme_controller *ctrlr)
329 {
330 	struct sysctl_ctx_list	*ctrlr_ctx;
331 	struct sysctl_oid	*ctrlr_tree, *que_tree;
332 	struct sysctl_oid_list	*ctrlr_list;
333 #define QUEUE_NAME_LENGTH	16
334 	char			queue_name[QUEUE_NAME_LENGTH];
335 	int			i;
336 
337 	ctrlr_ctx = device_get_sysctl_ctx(ctrlr->dev);
338 	ctrlr_tree = device_get_sysctl_tree(ctrlr->dev);
339 	ctrlr_list = SYSCTL_CHILDREN(ctrlr_tree);
340 
341 	SYSCTL_ADD_UINT(ctrlr_ctx, ctrlr_list, OID_AUTO, "num_io_queues",
342 	    CTLFLAG_RD, &ctrlr->num_io_queues, 0,
343 	    "Number of I/O queue pairs");
344 
345 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
346 	    "int_coal_time", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
347 	    ctrlr, 0, nvme_sysctl_int_coal_time, "IU",
348 	    "Interrupt coalescing timeout (in microseconds)");
349 
350 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
351 	    "int_coal_threshold",
352 	    CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, ctrlr, 0,
353 	    nvme_sysctl_int_coal_threshold, "IU",
354 	    "Interrupt coalescing threshold");
355 
356 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
357 	    "admin_timeout_period", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
358 	    &ctrlr->admin_timeout_period, 0, nvme_sysctl_timeout_period, "IU",
359 	    "Timeout period for Admin queue (in seconds)");
360 
361 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
362 	    "timeout_period", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
363 	    &ctrlr->timeout_period, 0, nvme_sysctl_timeout_period, "IU",
364 	    "Timeout period for I/O queues (in seconds)");
365 
366 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
367 	    "num_cmds", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
368 	    ctrlr, 0, nvme_sysctl_num_cmds, "IU",
369 	    "Number of commands submitted");
370 
371 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
372 	    "num_intr_handler_calls",
373 	    CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_MPSAFE, ctrlr, 0,
374 	    nvme_sysctl_num_intr_handler_calls, "IU",
375 	    "Number of times interrupt handler was invoked (will "
376 	    "typically be less than number of actual interrupts "
377 	    "generated due to coalescing)");
378 
379 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
380 	    "num_retries", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
381 	    ctrlr, 0, nvme_sysctl_num_retries, "IU",
382 	    "Number of commands retried");
383 
384 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
385 	    "num_failures", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
386 	    ctrlr, 0, nvme_sysctl_num_failures, "IU",
387 	    "Number of commands ending in failure after all retries");
388 
389 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
390 	    "num_ignored", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
391 	    ctrlr, 0, nvme_sysctl_num_ignored, "IU",
392 	    "Number of interrupts ignored administratively");
393 
394 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
395 	    "num_recovery_nolock", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
396 	    ctrlr, 0, nvme_sysctl_num_recovery_nolock, "IU",
397 	    "Number of times that we failed to lock recovery in the ISR");
398 
399 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
400 	    "reset_stats", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, ctrlr,
401 	    0, nvme_sysctl_reset_stats, "IU", "Reset statistics to zero");
402 
403 	SYSCTL_ADD_UINT(ctrlr_ctx, ctrlr_list, OID_AUTO, "cap_lo",
404 	    CTLFLAG_RD, &ctrlr->cap_lo, 0,
405 	    "Low 32-bits of capacities for the drive");
406 
407 	SYSCTL_ADD_UINT(ctrlr_ctx, ctrlr_list, OID_AUTO, "cap_hi",
408 	    CTLFLAG_RD, &ctrlr->cap_hi, 0,
409 	    "Hi 32-bits of capacities for the drive");
410 
411 	que_tree = SYSCTL_ADD_NODE(ctrlr_ctx, ctrlr_list, OID_AUTO, "adminq",
412 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Admin Queue");
413 
414 	nvme_sysctl_initialize_queue(&ctrlr->adminq, ctrlr_ctx, que_tree);
415 
416 	for (i = 0; i < ctrlr->num_io_queues; i++) {
417 		snprintf(queue_name, QUEUE_NAME_LENGTH, "ioq%d", i);
418 		que_tree = SYSCTL_ADD_NODE(ctrlr_ctx, ctrlr_list, OID_AUTO,
419 		    queue_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "IO Queue");
420 		nvme_sysctl_initialize_queue(&ctrlr->ioq[i], ctrlr_ctx,
421 		    que_tree);
422 	}
423 
424 	SYSCTL_ADD_COUNTER_U64(ctrlr_ctx, ctrlr_list, OID_AUTO, "alignment_splits",
425 	    CTLFLAG_RD, &ctrlr->alignment_splits,
426 	    "Number of times we split the I/O alignment for drives with preferred alignment");
427 }
428