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 newval = ctrlr->timeout_period; 150 int error = sysctl_handle_int(oidp, &newval, 0, req); 151 152 if (error || (req->newptr == NULL)) 153 return (error); 154 155 if (newval > NVME_MAX_TIMEOUT_PERIOD || 156 newval < NVME_MIN_TIMEOUT_PERIOD) { 157 return (EINVAL); 158 } else { 159 ctrlr->timeout_period = newval; 160 } 161 162 return (0); 163 } 164 165 static void 166 nvme_qpair_reset_stats(struct nvme_qpair *qpair) 167 { 168 169 qpair->num_cmds = 0; 170 qpair->num_intr_handler_calls = 0; 171 qpair->num_retries = 0; 172 qpair->num_failures = 0; 173 } 174 175 static int 176 nvme_sysctl_num_cmds(SYSCTL_HANDLER_ARGS) 177 { 178 struct nvme_controller *ctrlr = arg1; 179 int64_t num_cmds = 0; 180 int i; 181 182 num_cmds = ctrlr->adminq.num_cmds; 183 184 for (i = 0; i < ctrlr->num_io_queues; i++) 185 num_cmds += ctrlr->ioq[i].num_cmds; 186 187 return (sysctl_handle_64(oidp, &num_cmds, 0, req)); 188 } 189 190 static int 191 nvme_sysctl_num_intr_handler_calls(SYSCTL_HANDLER_ARGS) 192 { 193 struct nvme_controller *ctrlr = arg1; 194 int64_t num_intr_handler_calls = 0; 195 int i; 196 197 num_intr_handler_calls = ctrlr->adminq.num_intr_handler_calls; 198 199 for (i = 0; i < ctrlr->num_io_queues; i++) 200 num_intr_handler_calls += ctrlr->ioq[i].num_intr_handler_calls; 201 202 return (sysctl_handle_64(oidp, &num_intr_handler_calls, 0, req)); 203 } 204 205 static int 206 nvme_sysctl_num_retries(SYSCTL_HANDLER_ARGS) 207 { 208 struct nvme_controller *ctrlr = arg1; 209 int64_t num_retries = 0; 210 int i; 211 212 num_retries = ctrlr->adminq.num_retries; 213 214 for (i = 0; i < ctrlr->num_io_queues; i++) 215 num_retries += ctrlr->ioq[i].num_retries; 216 217 return (sysctl_handle_64(oidp, &num_retries, 0, req)); 218 } 219 220 static int 221 nvme_sysctl_num_failures(SYSCTL_HANDLER_ARGS) 222 { 223 struct nvme_controller *ctrlr = arg1; 224 int64_t num_failures = 0; 225 int i; 226 227 num_failures = ctrlr->adminq.num_failures; 228 229 for (i = 0; i < ctrlr->num_io_queues; i++) 230 num_failures += ctrlr->ioq[i].num_failures; 231 232 return (sysctl_handle_64(oidp, &num_failures, 0, req)); 233 } 234 235 static int 236 nvme_sysctl_reset_stats(SYSCTL_HANDLER_ARGS) 237 { 238 struct nvme_controller *ctrlr = arg1; 239 uint32_t i, val = 0; 240 241 int error = sysctl_handle_int(oidp, &val, 0, req); 242 243 if (error) 244 return (error); 245 246 if (val != 0) { 247 nvme_qpair_reset_stats(&ctrlr->adminq); 248 249 for (i = 0; i < ctrlr->num_io_queues; i++) 250 nvme_qpair_reset_stats(&ctrlr->ioq[i]); 251 } 252 253 return (0); 254 } 255 256 257 static void 258 nvme_sysctl_initialize_queue(struct nvme_qpair *qpair, 259 struct sysctl_ctx_list *ctrlr_ctx, struct sysctl_oid *que_tree) 260 { 261 struct sysctl_oid_list *que_list = SYSCTL_CHILDREN(que_tree); 262 263 SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "num_entries", 264 CTLFLAG_RD, &qpair->num_entries, 0, 265 "Number of entries in hardware queue"); 266 SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "num_trackers", 267 CTLFLAG_RD, &qpair->num_trackers, 0, 268 "Number of trackers pre-allocated for this queue pair"); 269 SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "sq_head", 270 CTLFLAG_RD, &qpair->sq_head, 0, 271 "Current head of submission queue (as observed by driver)"); 272 SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "sq_tail", 273 CTLFLAG_RD, &qpair->sq_tail, 0, 274 "Current tail of submission queue (as observed by driver)"); 275 SYSCTL_ADD_UINT(ctrlr_ctx, que_list, OID_AUTO, "cq_head", 276 CTLFLAG_RD, &qpair->cq_head, 0, 277 "Current head of completion queue (as observed by driver)"); 278 279 SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_cmds", 280 CTLFLAG_RD, &qpair->num_cmds, "Number of commands submitted"); 281 SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_intr_handler_calls", 282 CTLFLAG_RD, &qpair->num_intr_handler_calls, 283 "Number of times interrupt handler was invoked (will typically be " 284 "less than number of actual interrupts generated due to " 285 "coalescing)"); 286 SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_retries", 287 CTLFLAG_RD, &qpair->num_retries, "Number of commands retried"); 288 SYSCTL_ADD_QUAD(ctrlr_ctx, que_list, OID_AUTO, "num_failures", 289 CTLFLAG_RD, &qpair->num_failures, 290 "Number of commands ending in failure after all retries"); 291 292 SYSCTL_ADD_PROC(ctrlr_ctx, que_list, OID_AUTO, 293 "dump_debug", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 294 qpair, 0, nvme_sysctl_dump_debug, "IU", "Dump debug data"); 295 } 296 297 void 298 nvme_sysctl_initialize_ctrlr(struct nvme_controller *ctrlr) 299 { 300 struct sysctl_ctx_list *ctrlr_ctx; 301 struct sysctl_oid *ctrlr_tree, *que_tree; 302 struct sysctl_oid_list *ctrlr_list; 303 #define QUEUE_NAME_LENGTH 16 304 char queue_name[QUEUE_NAME_LENGTH]; 305 int i; 306 307 ctrlr_ctx = device_get_sysctl_ctx(ctrlr->dev); 308 ctrlr_tree = device_get_sysctl_tree(ctrlr->dev); 309 ctrlr_list = SYSCTL_CHILDREN(ctrlr_tree); 310 311 SYSCTL_ADD_UINT(ctrlr_ctx, ctrlr_list, OID_AUTO, "num_io_queues", 312 CTLFLAG_RD, &ctrlr->num_io_queues, 0, 313 "Number of I/O queue pairs"); 314 315 SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO, 316 "int_coal_time", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 317 ctrlr, 0, nvme_sysctl_int_coal_time, "IU", 318 "Interrupt coalescing timeout (in microseconds)"); 319 320 SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO, 321 "int_coal_threshold", 322 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, ctrlr, 0, 323 nvme_sysctl_int_coal_threshold, "IU", 324 "Interrupt coalescing threshold"); 325 326 SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO, 327 "timeout_period", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 328 ctrlr, 0, nvme_sysctl_timeout_period, "IU", 329 "Timeout period (in seconds)"); 330 331 SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO, 332 "num_cmds", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 333 ctrlr, 0, nvme_sysctl_num_cmds, "IU", 334 "Number of commands submitted"); 335 336 SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO, 337 "num_intr_handler_calls", 338 CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_NEEDGIANT, ctrlr, 0, 339 nvme_sysctl_num_intr_handler_calls, "IU", 340 "Number of times interrupt handler was invoked (will " 341 "typically be less than number of actual interrupts " 342 "generated due to coalescing)"); 343 344 SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO, 345 "num_retries", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 346 ctrlr, 0, nvme_sysctl_num_retries, "IU", 347 "Number of commands retried"); 348 349 SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO, 350 "num_failures", CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 351 ctrlr, 0, nvme_sysctl_num_failures, "IU", 352 "Number of commands ending in failure after all retries"); 353 354 SYSCTL_ADD_PROC(ctrlr_ctx, ctrlr_list, OID_AUTO, 355 "reset_stats", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, ctrlr, 356 0, nvme_sysctl_reset_stats, "IU", "Reset statistics to zero"); 357 358 que_tree = SYSCTL_ADD_NODE(ctrlr_ctx, ctrlr_list, OID_AUTO, "adminq", 359 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Admin Queue"); 360 361 nvme_sysctl_initialize_queue(&ctrlr->adminq, ctrlr_ctx, que_tree); 362 363 for (i = 0; i < ctrlr->num_io_queues; i++) { 364 snprintf(queue_name, QUEUE_NAME_LENGTH, "ioq%d", i); 365 que_tree = SYSCTL_ADD_NODE(ctrlr_ctx, ctrlr_list, OID_AUTO, 366 queue_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "IO Queue"); 367 nvme_sysctl_initialize_queue(&ctrlr->ioq[i], ctrlr_ctx, 368 que_tree); 369 } 370 } 371