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