2 * Copyright (C) 2012-2013 Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/sysctl.h>
34 #include "nvme_private.h"
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
42 #define CTLTYPE_S64 CTLTYPE_QUAD
43 #define sysctl_handle_64 sysctl_handle_quad
47 nvme_dump_queue(struct nvme_qpair *qpair)
49 struct nvme_completion *cpl;
50 struct nvme_command *cmd;
53 printf("id:%04Xh phase:%d\n", qpair->id, qpair->phase);
55 printf("Completion queue:\n");
56 for (i = 0; i < qpair->num_entries; i++) {
59 nvme_dump_completion(cpl);
62 printf("Submission queue:\n");
63 for (i = 0; i < qpair->num_entries; i++) {
66 nvme_dump_command(cmd);
72 nvme_sysctl_dump_debug(SYSCTL_HANDLER_ARGS)
74 struct nvme_qpair *qpair = arg1;
77 int error = sysctl_handle_int(oidp, &val, 0, req);
83 nvme_dump_queue(qpair);
89 nvme_sysctl_int_coal_time(SYSCTL_HANDLER_ARGS)
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,
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,
108 nvme_sysctl_int_coal_threshold(SYSCTL_HANDLER_ARGS)
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,
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,
127 nvme_sysctl_timeout_period(SYSCTL_HANDLER_ARGS)
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);
136 if (ctrlr->timeout_period > NVME_MAX_TIMEOUT_PERIOD ||
137 ctrlr->timeout_period < NVME_MIN_TIMEOUT_PERIOD) {
138 ctrlr->timeout_period = oldval;
146 nvme_qpair_reset_stats(struct nvme_qpair *qpair)
150 qpair->num_intr_handler_calls = 0;
154 nvme_sysctl_num_cmds(SYSCTL_HANDLER_ARGS)
156 struct nvme_controller *ctrlr = arg1;
157 int64_t num_cmds = 0;
160 num_cmds = ctrlr->adminq.num_cmds;
162 for (i = 0; i < ctrlr->num_io_queues; i++)
163 num_cmds += ctrlr->ioq[i].num_cmds;
165 return (sysctl_handle_64(oidp, &num_cmds, 0, req));
169 nvme_sysctl_num_intr_handler_calls(SYSCTL_HANDLER_ARGS)
171 struct nvme_controller *ctrlr = arg1;
172 int64_t num_intr_handler_calls = 0;
175 num_intr_handler_calls = ctrlr->adminq.num_intr_handler_calls;
177 for (i = 0; i < ctrlr->num_io_queues; i++)
178 num_intr_handler_calls += ctrlr->ioq[i].num_intr_handler_calls;
180 return (sysctl_handle_64(oidp, &num_intr_handler_calls, 0, req));
184 nvme_sysctl_reset_stats(SYSCTL_HANDLER_ARGS)
186 struct nvme_controller *ctrlr = arg1;
189 int error = sysctl_handle_int(oidp, &val, 0, req);
195 nvme_qpair_reset_stats(&ctrlr->adminq);
197 for (i = 0; i < ctrlr->num_io_queues; i++)
198 nvme_qpair_reset_stats(&ctrlr->ioq[i]);
206 nvme_sysctl_initialize_queue(struct nvme_qpair *qpair,
207 struct sysctl_ctx_list *ctrlr_ctx, struct sysctl_oid *que_tree)
209 struct sysctl_oid_list *que_list = SYSCTL_CHILDREN(que_tree);
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)");
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 "
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");
241 nvme_sysctl_initialize_ctrlr(struct nvme_controller *ctrlr)
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];
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);
254 SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
255 "int_coal_time", CTLTYPE_UINT | CTLFLAG_RW, ctrlr, 0,
256 nvme_sysctl_int_coal_time, "IU",
257 "Interrupt coalescing timeout (in microseconds)");
259 SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
260 "int_coal_threshold", CTLTYPE_UINT | CTLFLAG_RW, ctrlr, 0,
261 nvme_sysctl_int_coal_threshold, "IU",
262 "Interrupt coalescing threshold");
264 SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
265 "timeout_period", CTLTYPE_UINT | CTLFLAG_RW, ctrlr, 0,
266 nvme_sysctl_timeout_period, "IU",
267 "Timeout period (in seconds)");
269 SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
270 "num_cmds", CTLTYPE_S64 | CTLFLAG_RD,
271 ctrlr, 0, nvme_sysctl_num_cmds, "IU",
272 "Number of commands submitted");
274 SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
275 "num_intr_handler_calls", CTLTYPE_S64 | CTLFLAG_RD,
276 ctrlr, 0, nvme_sysctl_num_intr_handler_calls, "IU",
277 "Number of times interrupt handler was invoked (will "
278 "typically be less than number of actual interrupts "
279 "generated due to coalescing)");
281 SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
282 "reset_stats", CTLTYPE_UINT | CTLFLAG_RW, ctrlr, 0,
283 nvme_sysctl_reset_stats, "IU", "Reset statistics to zero");
285 que_tree = SYSCTL_ADD_NODE(ctrlr_ctx, ctrlr_list, OID_AUTO, "adminq",
286 CTLFLAG_RD, NULL, "Admin Queue");
288 nvme_sysctl_initialize_queue(&ctrlr->adminq, ctrlr_ctx, que_tree);
290 for (i = 0; i < ctrlr->num_io_queues; i++) {
291 snprintf(queue_name, QUEUE_NAME_LENGTH, "ioq%d", i);
292 que_tree = SYSCTL_ADD_NODE(ctrlr_ctx, ctrlr_list, OID_AUTO,
293 queue_name, CTLFLAG_RD, NULL, "IO Queue");
294 nvme_sysctl_initialize_queue(&ctrlr->ioq[i], ctrlr_ctx,