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