xref: /freebsd/sys/dev/nvme/nvme_sysctl.c (revision 90ec6a30353aa7caaf995ea50e2e23aa5a099600)
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 | CTLFLAG_MPSAFE, 0,
48     "NVMe sysctl tunables");
49 SYSCTL_INT(_hw_nvme, OID_AUTO, use_nvd, CTLFLAG_RDTUN,
50     &nvme_use_nvd, 1, "1 = Create NVD devices, 0 = Create NDA devices");
51 SYSCTL_BOOL(_hw_nvme, OID_AUTO, verbose_cmd_dump, CTLFLAG_RWTUN,
52     &nvme_verbose_cmd_dump, 0,
53     "enable verbose command printting when a command fails");
54 
55 /*
56  * CTLTYPE_S64 and sysctl_handle_64 were added in r217616.  Define these
57  *  explicitly here for older kernels that don't include the r217616
58  *  changeset.
59  */
60 #ifndef CTLTYPE_S64
61 #define CTLTYPE_S64		CTLTYPE_QUAD
62 #define sysctl_handle_64	sysctl_handle_quad
63 #endif
64 
65 static void
66 nvme_dump_queue(struct nvme_qpair *qpair)
67 {
68 	struct nvme_completion *cpl;
69 	struct nvme_command *cmd;
70 	int i;
71 
72 	printf("id:%04Xh phase:%d\n", qpair->id, qpair->phase);
73 
74 	printf("Completion queue:\n");
75 	for (i = 0; i < qpair->num_entries; i++) {
76 		cpl = &qpair->cpl[i];
77 		printf("%05d: ", i);
78 		nvme_dump_completion(cpl);
79 	}
80 
81 	printf("Submission queue:\n");
82 	for (i = 0; i < qpair->num_entries; i++) {
83 		cmd = &qpair->cmd[i];
84 		printf("%05d: ", i);
85 		nvme_dump_command(cmd);
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 newval = ctrlr->timeout_period;
149 	int error = sysctl_handle_int(oidp, &newval, 0, req);
150 
151 	if (error || (req->newptr == NULL))
152 		return (error);
153 
154 	if (newval > NVME_MAX_TIMEOUT_PERIOD ||
155 	    newval < NVME_MIN_TIMEOUT_PERIOD) {
156 		return (EINVAL);
157 	} else {
158 		ctrlr->timeout_period = newval;
159 	}
160 
161 	return (0);
162 }
163 
164 static void
165 nvme_qpair_reset_stats(struct nvme_qpair *qpair)
166 {
167 
168 	qpair->num_cmds = 0;
169 	qpair->num_intr_handler_calls = 0;
170 	qpair->num_retries = 0;
171 	qpair->num_failures = 0;
172 }
173 
174 static int
175 nvme_sysctl_num_cmds(SYSCTL_HANDLER_ARGS)
176 {
177 	struct nvme_controller 	*ctrlr = arg1;
178 	int64_t			num_cmds = 0;
179 	int			i;
180 
181 	num_cmds = ctrlr->adminq.num_cmds;
182 
183 	for (i = 0; i < ctrlr->num_io_queues; i++)
184 		num_cmds += ctrlr->ioq[i].num_cmds;
185 
186 	return (sysctl_handle_64(oidp, &num_cmds, 0, req));
187 }
188 
189 static int
190 nvme_sysctl_num_intr_handler_calls(SYSCTL_HANDLER_ARGS)
191 {
192 	struct nvme_controller 	*ctrlr = arg1;
193 	int64_t			num_intr_handler_calls = 0;
194 	int			i;
195 
196 	num_intr_handler_calls = ctrlr->adminq.num_intr_handler_calls;
197 
198 	for (i = 0; i < ctrlr->num_io_queues; i++)
199 		num_intr_handler_calls += ctrlr->ioq[i].num_intr_handler_calls;
200 
201 	return (sysctl_handle_64(oidp, &num_intr_handler_calls, 0, req));
202 }
203 
204 static int
205 nvme_sysctl_num_retries(SYSCTL_HANDLER_ARGS)
206 {
207 	struct nvme_controller 	*ctrlr = arg1;
208 	int64_t			num_retries = 0;
209 	int			i;
210 
211 	num_retries = ctrlr->adminq.num_retries;
212 
213 	for (i = 0; i < ctrlr->num_io_queues; i++)
214 		num_retries += ctrlr->ioq[i].num_retries;
215 
216 	return (sysctl_handle_64(oidp, &num_retries, 0, req));
217 }
218 
219 static int
220 nvme_sysctl_num_failures(SYSCTL_HANDLER_ARGS)
221 {
222 	struct nvme_controller 	*ctrlr = arg1;
223 	int64_t			num_failures = 0;
224 	int			i;
225 
226 	num_failures = ctrlr->adminq.num_failures;
227 
228 	for (i = 0; i < ctrlr->num_io_queues; i++)
229 		num_failures += ctrlr->ioq[i].num_failures;
230 
231 	return (sysctl_handle_64(oidp, &num_failures, 0, req));
232 }
233 
234 static int
235 nvme_sysctl_reset_stats(SYSCTL_HANDLER_ARGS)
236 {
237 	struct nvme_controller 	*ctrlr = arg1;
238 	uint32_t		i, val = 0;
239 
240 	int error = sysctl_handle_int(oidp, &val, 0, req);
241 
242 	if (error)
243 		return (error);
244 
245 	if (val != 0) {
246 		nvme_qpair_reset_stats(&ctrlr->adminq);
247 
248 		for (i = 0; i < ctrlr->num_io_queues; i++)
249 			nvme_qpair_reset_stats(&ctrlr->ioq[i]);
250 	}
251 
252 	return (0);
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 | CTLFLAG_NEEDGIANT,
292 	    qpair, 0, 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_io_queues",
310 	    CTLFLAG_RD, &ctrlr->num_io_queues, 0,
311 	    "Number of I/O queue pairs");
312 
313 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
314 	    "int_coal_time", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
315 	    ctrlr, 0, 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",
320 	    CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, ctrlr, 0,
321 	    nvme_sysctl_int_coal_threshold, "IU",
322 	    "Interrupt coalescing threshold");
323 
324 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
325 	    "timeout_period", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
326 	    ctrlr, 0, nvme_sysctl_timeout_period, "IU",
327 	    "Timeout period (in seconds)");
328 
329 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
330 	    "num_cmds", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
331 	    ctrlr, 0, nvme_sysctl_num_cmds, "IU",
332 	    "Number of commands submitted");
333 
334 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
335 	    "num_intr_handler_calls",
336 	    CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_NEEDGIANT, ctrlr, 0,
337 	    nvme_sysctl_num_intr_handler_calls, "IU",
338 	    "Number of times interrupt handler was invoked (will "
339 	    "typically be less than number of actual interrupts "
340 	    "generated due to coalescing)");
341 
342 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
343 	    "num_retries", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
344 	    ctrlr, 0, nvme_sysctl_num_retries, "IU",
345 	    "Number of commands retried");
346 
347 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
348 	    "num_failures", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
349 	    ctrlr, 0, nvme_sysctl_num_failures, "IU",
350 	    "Number of commands ending in failure after all retries");
351 
352 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
353 	    "reset_stats", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, ctrlr,
354 	    0, nvme_sysctl_reset_stats, "IU", "Reset statistics to zero");
355 
356 	que_tree = SYSCTL_ADD_NODE(ctrlr_ctx, ctrlr_list, OID_AUTO, "adminq",
357 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Admin Queue");
358 
359 	nvme_sysctl_initialize_queue(&ctrlr->adminq, ctrlr_ctx, que_tree);
360 
361 	for (i = 0; i < ctrlr->num_io_queues; i++) {
362 		snprintf(queue_name, QUEUE_NAME_LENGTH, "ioq%d", i);
363 		que_tree = SYSCTL_ADD_NODE(ctrlr_ctx, ctrlr_list, OID_AUTO,
364 		    queue_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "IO Queue");
365 		nvme_sysctl_initialize_queue(&ctrlr->ioq[i], ctrlr_ctx,
366 		    que_tree);
367 	}
368 }
369