2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2012-2013 Intel Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
35 #include <sys/fcntl.h>
36 #include <sys/kthread.h>
37 #include <sys/module.h>
39 #include <sys/syscallsubr.h>
40 #include <sys/sysctl.h>
41 #include <sys/sysproto.h>
42 #include <sys/systm.h>
43 #include <sys/unistd.h>
45 #include <geom/geom.h>
47 #include "nvme_private.h"
49 struct nvme_io_test_thread {
52 struct nvme_namespace *ns;
53 enum nvme_nvm_opcode opc;
58 uint64_t io_completed;
61 struct nvme_io_test_internal {
63 struct nvme_namespace *ns;
64 enum nvme_nvm_opcode opc;
71 uint64_t io_completed[NVME_TEST_MAX_THREADS];
75 nvme_ns_bio_test_cb(struct bio *bio)
79 mtx = mtx_pool_find(mtxpool_sleep, bio);
86 nvme_ns_bio_test(void *arg)
88 struct nvme_io_test_internal *io_test = arg;
95 uint64_t io_completed = 0, offset;
97 #if __FreeBSD_version >= 900017
101 buf = malloc(io_test->size, M_NVME, M_WAITOK);
102 idx = atomic_fetchadd_int(&io_test->td_idx, 1);
103 dev = io_test->ns->cdev;
105 offset = idx * 2048 * nvme_ns_get_sector_size(io_test->ns);
111 memset(bio, 0, sizeof(*bio));
112 bio->bio_cmd = (io_test->opc == NVME_OPC_READ) ?
113 BIO_READ : BIO_WRITE;
114 bio->bio_done = nvme_ns_bio_test_cb;
116 bio->bio_offset = offset;
118 bio->bio_bcount = io_test->size;
120 if (io_test->flags & NVME_TEST_FLAG_REFTHREAD) {
121 #if __FreeBSD_version >= 900017
122 csw = dev_refthread(dev, &ref);
124 csw = dev_refthread(dev);
129 mtx = mtx_pool_find(mtxpool_sleep, bio);
131 (*csw->d_strategy)(bio);
132 msleep(bio, mtx, PRIBIO, "biotestwait", 0);
135 if (io_test->flags & NVME_TEST_FLAG_REFTHREAD) {
136 #if __FreeBSD_version >= 900017
137 dev_relthread(dev, ref);
143 if ((bio->bio_flags & BIO_ERROR) || (bio->bio_resid > 0))
151 timevalsub(&t, &io_test->start);
153 if (t.tv_sec >= io_test->time)
156 offset += io_test->size;
157 if ((offset + io_test->size) > nvme_ns_get_size(io_test->ns))
161 io_test->io_completed[idx] = io_completed;
166 atomic_subtract_int(&io_test->td_active, 1);
169 #if __FreeBSD_version >= 800000
177 nvme_ns_io_test_cb(void *arg, const struct nvme_completion *cpl)
179 struct nvme_io_test_thread *tth = arg;
184 if (nvme_completion_is_error(cpl)) {
185 printf("%s: error occurred\n", __func__);
191 timevalsub(&t, &tth->start);
193 if (t.tv_sec >= tth->time) {
200 nvme_ns_cmd_write(tth->ns, tth->buf, tth->idx * 2048,
201 tth->size/nvme_ns_get_sector_size(tth->ns),
202 nvme_ns_io_test_cb, tth);
205 nvme_ns_cmd_read(tth->ns, tth->buf, tth->idx * 2048,
206 tth->size/nvme_ns_get_sector_size(tth->ns),
207 nvme_ns_io_test_cb, tth);
215 nvme_ns_io_test(void *arg)
217 struct nvme_io_test_internal *io_test = arg;
218 struct nvme_io_test_thread *tth;
219 struct nvme_completion cpl;
222 tth = malloc(sizeof(*tth), M_NVME, M_WAITOK | M_ZERO);
223 tth->ns = io_test->ns;
224 tth->opc = io_test->opc;
225 memcpy(&tth->start, &io_test->start, sizeof(tth->start));
226 tth->buf = malloc(io_test->size, M_NVME, M_WAITOK);
227 tth->size = io_test->size;
228 tth->time = io_test->time;
229 tth->idx = atomic_fetchadd_int(&io_test->td_idx, 1);
231 memset(&cpl, 0, sizeof(cpl));
233 nvme_ns_io_test_cb(tth, &cpl);
235 error = tsleep(tth, 0, "test_wait", tth->time*hz*2);
238 printf("%s: error = %d\n", __func__, error);
240 io_test->io_completed[tth->idx] = tth->io_completed;
243 free(tth->buf, M_NVME);
246 atomic_subtract_int(&io_test->td_active, 1);
249 #if __FreeBSD_version >= 800004
257 nvme_ns_test(struct nvme_namespace *ns, u_long cmd, caddr_t arg)
259 struct nvme_io_test *io_test;
260 struct nvme_io_test_internal *io_test_internal;
264 io_test = (struct nvme_io_test *)arg;
266 if ((io_test->opc != NVME_OPC_READ) &&
267 (io_test->opc != NVME_OPC_WRITE))
270 if (io_test->size % nvme_ns_get_sector_size(ns))
273 io_test_internal = malloc(sizeof(*io_test_internal), M_NVME,
275 io_test_internal->opc = io_test->opc;
276 io_test_internal->ns = ns;
277 io_test_internal->td_active = io_test->num_threads;
278 io_test_internal->time = io_test->time;
279 io_test_internal->size = io_test->size;
280 io_test_internal->flags = io_test->flags;
282 if (cmd == NVME_IO_TEST)
283 fn = nvme_ns_io_test;
285 fn = nvme_ns_bio_test;
287 getmicrouptime(&io_test_internal->start);
289 for (i = 0; i < io_test->num_threads; i++)
290 #if __FreeBSD_version >= 800004
291 kthread_add(fn, io_test_internal,
292 NULL, NULL, 0, 0, "nvme_io_test[%d]", i);
294 kthread_create(fn, io_test_internal,
295 NULL, 0, 0, "nvme_io_test[%d]", i);
298 tsleep(io_test_internal, 0, "nvme_test", io_test->time * 2 * hz);
300 while (io_test_internal->td_active > 0)
303 memcpy(io_test->io_completed, io_test_internal->io_completed,
304 sizeof(io_test->io_completed));
306 free(io_test_internal, M_NVME);