]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/nvme/nvme_sysctl.c
Correctly implement support for remote wakeup for USB 3.0 device.
[FreeBSD/FreeBSD.git] / sys / dev / nvme / nvme_sysctl.c
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
90 static int
91 nvme_sysctl_dump_debug(SYSCTL_HANDLER_ARGS)
92 {
93         struct nvme_qpair       *qpair = arg1;
94         uint32_t                val = 0;
95
96         int error = sysctl_handle_int(oidp, &val, 0, req);
97
98         if (error)
99                 return (error);
100
101         if (val != 0)
102                 nvme_dump_queue(qpair);
103
104         return (0);
105 }
106
107 static int
108 nvme_sysctl_int_coal_time(SYSCTL_HANDLER_ARGS)
109 {
110         struct nvme_controller *ctrlr = arg1;
111         uint32_t oldval = ctrlr->int_coal_time;
112         int error = sysctl_handle_int(oidp, &ctrlr->int_coal_time, 0,
113             req);
114
115         if (error)
116                 return (error);
117
118         if (oldval != ctrlr->int_coal_time)
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_int_coal_threshold(SYSCTL_HANDLER_ARGS)
128 {
129         struct nvme_controller *ctrlr = arg1;
130         uint32_t oldval = ctrlr->int_coal_threshold;
131         int error = sysctl_handle_int(oidp, &ctrlr->int_coal_threshold, 0,
132             req);
133
134         if (error)
135                 return (error);
136
137         if (oldval != ctrlr->int_coal_threshold)
138                 nvme_ctrlr_cmd_set_interrupt_coalescing(ctrlr,
139                     ctrlr->int_coal_time, ctrlr->int_coal_threshold, NULL,
140                     NULL);
141
142         return (0);
143 }
144
145 static int
146 nvme_sysctl_timeout_period(SYSCTL_HANDLER_ARGS)
147 {
148         struct nvme_controller *ctrlr = arg1;
149         uint32_t oldval = ctrlr->timeout_period;
150         int error = sysctl_handle_int(oidp, &ctrlr->timeout_period, 0, req);
151
152         if (error)
153                 return (error);
154
155         if (ctrlr->timeout_period > NVME_MAX_TIMEOUT_PERIOD ||
156             ctrlr->timeout_period < NVME_MIN_TIMEOUT_PERIOD) {
157                 ctrlr->timeout_period = oldval;
158                 return (EINVAL);
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
256 static void
257 nvme_sysctl_initialize_queue(struct nvme_qpair *qpair,
258     struct sysctl_ctx_list *ctrlr_ctx, struct sysctl_oid *que_tree)
259 {
260         struct sysctl_oid_list  *que_list = SYSCTL_CHILDREN(que_tree);
261
262         SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "num_entries",
263             CTLFLAG_RD, &qpair->num_entries, 0,
264             "Number of entries in hardware queue");
265         SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "num_trackers",
266             CTLFLAG_RD, &qpair->num_trackers, 0,
267             "Number of trackers pre-allocated for this queue pair");
268         SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "sq_head",
269             CTLFLAG_RD, &qpair->sq_head, 0,
270             "Current head of submission queue (as observed by driver)");
271         SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "sq_tail",
272             CTLFLAG_RD, &qpair->sq_tail, 0,
273             "Current tail of submission queue (as observed by driver)");
274         SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "cq_head",
275             CTLFLAG_RD, &qpair->cq_head, 0,
276             "Current head of completion queue (as observed by driver)");
277
278         SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_cmds",
279             CTLFLAG_RD, &qpair->num_cmds, "Number of commands submitted");
280         SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_intr_handler_calls",
281             CTLFLAG_RD, &qpair->num_intr_handler_calls,
282             "Number of times interrupt handler was invoked (will typically be "
283             "less than number of actual interrupts generated due to "
284             "coalescing)");
285         SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_retries",
286             CTLFLAG_RD, &qpair->num_retries, "Number of commands retried");
287         SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_failures",
288             CTLFLAG_RD, &qpair->num_failures,
289             "Number of commands ending in failure after all retries");
290
291         SYSCTL_ADD_PROC(ctrlr_ctx, que_list, OID_AUTO,
292             "dump_debug", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
293             qpair, 0, nvme_sysctl_dump_debug, "IU", "Dump debug data");
294 }
295
296 void
297 nvme_sysctl_initialize_ctrlr(struct nvme_controller *ctrlr)
298 {
299         struct sysctl_ctx_list  *ctrlr_ctx;
300         struct sysctl_oid       *ctrlr_tree, *que_tree;
301         struct sysctl_oid_list  *ctrlr_list;
302 #define QUEUE_NAME_LENGTH       16
303         char                    queue_name[QUEUE_NAME_LENGTH];
304         int                     i;
305
306         ctrlr_ctx = device_get_sysctl_ctx(ctrlr->dev);
307         ctrlr_tree = device_get_sysctl_tree(ctrlr->dev);
308         ctrlr_list = SYSCTL_CHILDREN(ctrlr_tree);
309
310         SYSCTL_ADD_UINT(ctrlr_ctx, ctrlr_list, OID_AUTO, "num_io_queues",
311             CTLFLAG_RD, &ctrlr->num_io_queues, 0,
312             "Number of I/O queue pairs");
313
314         SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
315             "int_coal_time", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
316             ctrlr, 0, nvme_sysctl_int_coal_time, "IU",
317             "Interrupt coalescing timeout (in microseconds)");
318
319         SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
320             "int_coal_threshold",
321             CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, ctrlr, 0,
322             nvme_sysctl_int_coal_threshold, "IU",
323             "Interrupt coalescing threshold");
324
325         SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
326             "timeout_period", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
327             ctrlr, 0, nvme_sysctl_timeout_period, "IU",
328             "Timeout period (in seconds)");
329
330         SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
331             "num_cmds", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
332             ctrlr, 0, nvme_sysctl_num_cmds, "IU",
333             "Number of commands submitted");
334
335         SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
336             "num_intr_handler_calls",
337             CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_NEEDGIANT, ctrlr, 0,
338             nvme_sysctl_num_intr_handler_calls, "IU",
339             "Number of times interrupt handler was invoked (will "
340             "typically be less than number of actual interrupts "
341             "generated due to coalescing)");
342
343         SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
344             "num_retries", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
345             ctrlr, 0, nvme_sysctl_num_retries, "IU",
346             "Number of commands retried");
347
348         SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
349             "num_failures", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
350             ctrlr, 0, nvme_sysctl_num_failures, "IU",
351             "Number of commands ending in failure after all retries");
352
353         SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO,
354             "reset_stats", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, ctrlr,
355             0, nvme_sysctl_reset_stats, "IU", "Reset statistics to zero");
356
357         que_tree = SYSCTL_ADD_NODE(ctrlr_ctx, ctrlr_list, OID_AUTO, "adminq",
358             CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Admin Queue");
359
360         nvme_sysctl_initialize_queue(&ctrlr->adminq, ctrlr_ctx, que_tree);
361
362         for (i = 0; i < ctrlr->num_io_queues; i++) {
363                 snprintf(queue_name, QUEUE_NAME_LENGTH, "ioq%d", i);
364                 que_tree = SYSCTL_ADD_NODE(ctrlr_ctx, ctrlr_list, OID_AUTO,
365                     queue_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "IO Queue");
366                 nvme_sysctl_initialize_queue(&ctrlr->ioq[i], ctrlr_ctx,
367                     que_tree);
368         }
369 }