xref: /freebsd/sys/dev/nvme/nvme_sysctl.c (revision c4fde87a3236b500246d4fce76dbca2a3d64f73a)
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 = NVME_SQE(qpair, 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(SYSCTL_HANDLER_ARGS)
97 {
98 	struct nvme_controller *ctrlr = arg1;
99 	uint32_t *valp = (uint32_t *)((char *)ctrlr + (size_t)arg2);
100 	uint32_t oldval = *valp;
101 	int error = sysctl_handle_int(oidp, valp, 0, req);
102 
103 	if (error)
104 		return (error);
105 
106 	if (oldval != *valp)
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_timeout_period(SYSCTL_HANDLER_ARGS)
116 {
117 	uint32_t *ptr = arg1;
118 	uint32_t newval = *ptr;
119 	int error = sysctl_handle_int(oidp, &newval, 0, req);
120 
121 	if (error || (req->newptr == NULL))
122 		return (error);
123 
124 	if (newval > NVME_MAX_TIMEOUT_PERIOD ||
125 	    newval < NVME_MIN_TIMEOUT_PERIOD) {
126 		return (EINVAL);
127 	} else {
128 		*ptr = newval;
129 	}
130 
131 	return (0);
132 }
133 
134 static void
135 nvme_qpair_reset_stats(struct nvme_qpair *qpair)
136 {
137 	/*
138 	 * Reset the values. Due to sanity checks in
139 	 * nvme_qpair_process_completions, we reset the number of interrupt
140 	 * calls to 1.
141 	 */
142 	qpair->num_cmds = 0;
143 	qpair->num_intr_handler_calls = 1;
144 	qpair->num_retries = 0;
145 	qpair->num_failures = 0;
146 	qpair->num_ignored = 0;
147 	qpair->num_recovery_nolock = 0;
148 }
149 
150 static int
151 nvme_sysctl_qpair_stat(SYSCTL_HANDLER_ARGS)
152 {
153 	struct nvme_controller	*ctrlr = arg1;
154 	size_t			off = arg2;
155 	int64_t			sum;
156 	int			i;
157 
158 	sum = *(int64_t *)((char *)&ctrlr->adminq + off);
159 
160 	if (ctrlr->ioq != NULL) {
161 		for (i = 0; i < ctrlr->num_io_queues; i++)
162 			sum += *(int64_t *)((char *)&ctrlr->ioq[i] + off);
163 	}
164 
165 	return (sysctl_handle_64(oidp, &sum, 0, req));
166 }
167 
168 static int
169 nvme_sysctl_reset_stats(SYSCTL_HANDLER_ARGS)
170 {
171 	struct nvme_controller 	*ctrlr = arg1;
172 	uint32_t		i, val = 0;
173 
174 	int error = sysctl_handle_int(oidp, &val, 0, req);
175 
176 	if (error)
177 		return (error);
178 
179 	if (val != 0) {
180 		nvme_qpair_reset_stats(&ctrlr->adminq);
181 
182 		if (ctrlr->ioq != NULL) {
183 			for (i = 0; i < ctrlr->num_io_queues; i++)
184 				nvme_qpair_reset_stats(&ctrlr->ioq[i]);
185 		}
186 	}
187 
188 	return (0);
189 }
190 
191 static void
192 nvme_sysctl_initialize_queue(struct nvme_qpair *qpair,
193     struct sysctl_ctx_list *ctrlr_ctx, struct sysctl_oid *que_tree)
194 {
195 	struct sysctl_oid_list	*que_list = SYSCTL_CHILDREN(que_tree);
196 
197 	SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "num_entries",
198 	    CTLFLAG_RD, &qpair->num_entries, 0,
199 	    "Number of entries in hardware queue");
200 	SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "num_trackers",
201 	    CTLFLAG_RD, &qpair->num_trackers, 0,
202 	    "Number of trackers pre-allocated for this queue pair");
203 	SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "sq_head",
204 	    CTLFLAG_RD, &qpair->sq_head, 0,
205 	    "Current head of submission queue (as observed by driver)");
206 	SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "sq_tail",
207 	    CTLFLAG_RD, &qpair->sq_tail, 0,
208 	    "Current tail of submission queue (as observed by driver)");
209 	SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "cq_head",
210 	    CTLFLAG_RD, &qpair->cq_head, 0,
211 	    "Current head of completion queue (as observed by driver)");
212 
213 	SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_cmds",
214 	    CTLFLAG_RD, &qpair->num_cmds, "Number of commands submitted");
215 	SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_intr_handler_calls",
216 	    CTLFLAG_RD, &qpair->num_intr_handler_calls,
217 	    "Number of times interrupt handler was invoked (will typically be "
218 	    "less than number of actual interrupts generated due to "
219 	    "coalescing)");
220 	SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_retries",
221 	    CTLFLAG_RD, &qpair->num_retries, "Number of commands retried");
222 	SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_failures",
223 	    CTLFLAG_RD, &qpair->num_failures,
224 	    "Number of commands ending in failure after all retries");
225 	SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_ignored",
226 	    CTLFLAG_RD, &qpair->num_ignored,
227 	    "Number of interrupts posted, but were administratively ignored");
228 	SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_recovery_nolock",
229 	    CTLFLAG_RD, &qpair->num_recovery_nolock,
230 	    "Number of times that we failed to lock recovery in the ISR");
231 
232 	SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "recovery",
233 	    CTLFLAG_RW, &qpair->recovery_state, 0,
234 	    "Current recovery state of the queue");
235 
236 	SYSCTL_ADD_PROC(ctrlr_ctx, que_list, OID_AUTO,
237 	    "dump_debug", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
238 	    qpair, 0, nvme_sysctl_dump_debug, "IU", "Dump debug data");
239 }
240 
241 void
242 nvme_sysctl_initialize_ctrlr(struct nvme_controller *ctrlr)
243 {
244 	struct sysctl_ctx_list	*ctrlr_ctx;
245 	struct sysctl_oid	*ctrlr_tree, *que_tree, *ioq_tree;
246 	struct sysctl_oid_list	*ctrlr_list, *ioq_list;
247 #define QUEUE_NAME_LENGTH	16
248 	char			queue_name[QUEUE_NAME_LENGTH];
249 	int			i;
250 
251 	ctrlr_ctx = device_get_sysctl_ctx(ctrlr->dev);
252 	ctrlr_tree = device_get_sysctl_tree(ctrlr->dev);
253 	ctrlr_list = SYSCTL_CHILDREN(ctrlr_tree);
254 
255 	SYSCTL_ADD_UINT(ctrlr_ctx, ctrlr_list, OID_AUTO, "num_io_queues",
256 	    CTLFLAG_RD, &ctrlr->num_io_queues, 0,
257 	    "Number of I/O queue pairs");
258 
259 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
260 	    "int_coal_time", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
261 	    ctrlr, offsetof(struct nvme_controller, int_coal_time),
262 	    nvme_sysctl_int_coal, "IU",
263 	    "Interrupt coalescing timeout (in microseconds)");
264 
265 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
266 	    "int_coal_threshold",
267 	    CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, ctrlr,
268 	    offsetof(struct nvme_controller, int_coal_threshold),
269 	    nvme_sysctl_int_coal, "IU",
270 	    "Interrupt coalescing threshold");
271 
272 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
273 	    "admin_timeout_period", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
274 	    &ctrlr->admin_timeout_period, 0, nvme_sysctl_timeout_period, "IU",
275 	    "Timeout period for Admin queue (in seconds)");
276 
277 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
278 	    "timeout_period", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
279 	    &ctrlr->timeout_period, 0, nvme_sysctl_timeout_period, "IU",
280 	    "Timeout period for I/O queues (in seconds)");
281 
282 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
283 	    "num_cmds", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
284 	    ctrlr, offsetof(struct nvme_qpair, num_cmds),
285 	    nvme_sysctl_qpair_stat, "IU",
286 	    "Number of commands submitted");
287 
288 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
289 	    "num_intr_handler_calls",
290 	    CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_MPSAFE, ctrlr,
291 	    offsetof(struct nvme_qpair, num_intr_handler_calls),
292 	    nvme_sysctl_qpair_stat, "IU",
293 	    "Number of times interrupt handler was invoked (will "
294 	    "typically be less than number of actual interrupts "
295 	    "generated due to coalescing)");
296 
297 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
298 	    "num_retries", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
299 	    ctrlr, offsetof(struct nvme_qpair, num_retries),
300 	    nvme_sysctl_qpair_stat, "IU",
301 	    "Number of commands retried");
302 
303 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
304 	    "num_failures", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
305 	    ctrlr, offsetof(struct nvme_qpair, num_failures),
306 	    nvme_sysctl_qpair_stat, "IU",
307 	    "Number of commands ending in failure after all retries");
308 
309 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
310 	    "num_ignored", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
311 	    ctrlr, offsetof(struct nvme_qpair, num_ignored),
312 	    nvme_sysctl_qpair_stat, "IU",
313 	    "Number of interrupts ignored administratively");
314 
315 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
316 	    "num_recovery_nolock", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
317 	    ctrlr, offsetof(struct nvme_qpair, num_recovery_nolock),
318 	    nvme_sysctl_qpair_stat, "IU",
319 	    "Number of times that we failed to lock recovery in the ISR");
320 
321 	SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
322 	    "reset_stats", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, ctrlr,
323 	    0, nvme_sysctl_reset_stats, "IU", "Reset statistics to zero");
324 
325 	SYSCTL_ADD_UINT(ctrlr_ctx, ctrlr_list, OID_AUTO, "cap_lo",
326 	    CTLFLAG_RD, &ctrlr->cap_lo, 0,
327 	    "Low 32-bits of capacities for the drive");
328 
329 	SYSCTL_ADD_UINT(ctrlr_ctx, ctrlr_list, OID_AUTO, "cap_hi",
330 	    CTLFLAG_RD, &ctrlr->cap_hi, 0,
331 	    "Hi 32-bits of capacities for the drive");
332 
333 	SYSCTL_ADD_BOOL(ctrlr_ctx, ctrlr_list, OID_AUTO, "fail_on_reset",
334 	    CTLFLAG_RD, &ctrlr->fail_on_reset, 0,
335 	    "Pretend the next reset fails and fail the controller");
336 
337 	que_tree = SYSCTL_ADD_NODE(ctrlr_ctx, ctrlr_list, OID_AUTO, "adminq",
338 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Admin Queue");
339 
340 	nvme_sysctl_initialize_queue(&ctrlr->adminq, ctrlr_ctx, que_tree);
341 
342 	/*
343 	 * Make sure that we've constructed the I/O queues before setting up the
344 	 * sysctls. Failed controllers won't allocate it, but we want the rest
345 	 * of the sysctls to diagnose things.
346 	 */
347 	if (ctrlr->ioq != NULL) {
348 		ioq_tree = SYSCTL_ADD_NODE(ctrlr_ctx, ctrlr_list, OID_AUTO,
349 		    "ioq", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "I/O Queues");
350 		ioq_list = SYSCTL_CHILDREN(ioq_tree);
351 
352 		for (i = 0; i < ctrlr->num_io_queues; i++) {
353 			snprintf(queue_name, QUEUE_NAME_LENGTH, "%d", i);
354 			que_tree = SYSCTL_ADD_NODE(ctrlr_ctx, ioq_list, OID_AUTO,
355 			    queue_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "IO Queue");
356 			nvme_sysctl_initialize_queue(&ctrlr->ioq[i], ctrlr_ctx,
357 			    que_tree);
358 		}
359 	}
360 
361 	SYSCTL_ADD_COUNTER_U64(ctrlr_ctx, ctrlr_list, OID_AUTO, "alignment_splits",
362 	    CTLFLAG_RD, &ctrlr->alignment_splits,
363 	    "Number of times we split the I/O alignment for drives with preferred alignment");
364 }
365