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>
33 #include <sys/fcntl.h>
34 #include <sys/kthread.h>
35 #include <sys/module.h>
37 #include <sys/syscallsubr.h>
38 #include <sys/sysctl.h>
39 #include <sys/sysproto.h>
40 #include <sys/systm.h>
41 #include <sys/unistd.h>
43 #include <geom/geom.h>
45 #include "nvme_private.h"
47 struct nvme_io_test_thread {
50 struct nvme_namespace *ns;
51 enum nvme_nvm_opcode opc;
56 uint64_t io_completed;
59 struct nvme_io_test_internal {
61 struct nvme_namespace *ns;
62 enum nvme_nvm_opcode opc;
69 uint64_t io_completed[NVME_TEST_MAX_THREADS];
73 nvme_ns_bio_test_cb(struct bio *bio)
77 mtx = mtx_pool_find(mtxpool_sleep, bio);
84 nvme_ns_bio_test(void *arg)
86 struct nvme_io_test_internal *io_test = arg;
93 uint64_t io_completed = 0, offset;
95 #if __FreeBSD_version >= 900017
99 buf = malloc(io_test->size, M_NVME, M_WAITOK);
100 idx = atomic_fetchadd_int(&io_test->td_idx, 1);
101 dev = io_test->ns->cdev;
103 offset = idx * 2048 * nvme_ns_get_sector_size(io_test->ns);
109 memset(bio, 0, sizeof(*bio));
110 bio->bio_cmd = (io_test->opc == NVME_OPC_READ) ?
111 BIO_READ : BIO_WRITE;
112 bio->bio_done = nvme_ns_bio_test_cb;
114 bio->bio_offset = offset;
116 bio->bio_bcount = io_test->size;
118 if (io_test->flags & NVME_TEST_FLAG_REFTHREAD) {
119 #if __FreeBSD_version >= 900017
120 csw = dev_refthread(dev, &ref);
122 csw = dev_refthread(dev);
127 mtx = mtx_pool_find(mtxpool_sleep, bio);
129 (*csw->d_strategy)(bio);
130 msleep(bio, mtx, PRIBIO, "biotestwait", 0);
133 if (io_test->flags & NVME_TEST_FLAG_REFTHREAD) {
134 #if __FreeBSD_version >= 900017
135 dev_relthread(dev, ref);
141 if ((bio->bio_flags & BIO_ERROR) || (bio->bio_resid > 0))
149 timevalsub(&t, &io_test->start);
151 if (t.tv_sec >= io_test->time)
154 offset += io_test->size;
155 if ((offset + io_test->size) > nvme_ns_get_size(io_test->ns))
159 io_test->io_completed[idx] = io_completed;
164 atomic_subtract_int(&io_test->td_active, 1);
167 #if __FreeBSD_version >= 800000
175 nvme_ns_io_test_cb(void *arg, const struct nvme_completion *cpl)
177 struct nvme_io_test_thread *tth = arg;
182 if (nvme_completion_is_error(cpl)) {
183 printf("%s: error occurred\n", __func__);
189 timevalsub(&t, &tth->start);
191 if (t.tv_sec >= tth->time) {
198 nvme_ns_cmd_write(tth->ns, tth->buf, tth->idx * 2048,
199 tth->size/nvme_ns_get_sector_size(tth->ns),
200 nvme_ns_io_test_cb, tth);
203 nvme_ns_cmd_read(tth->ns, tth->buf, tth->idx * 2048,
204 tth->size/nvme_ns_get_sector_size(tth->ns),
205 nvme_ns_io_test_cb, tth);
213 nvme_ns_io_test(void *arg)
215 struct nvme_io_test_internal *io_test = arg;
216 struct nvme_io_test_thread *tth;
217 struct nvme_completion cpl;
220 tth = malloc(sizeof(*tth), M_NVME, M_WAITOK | M_ZERO);
221 tth->ns = io_test->ns;
222 tth->opc = io_test->opc;
223 memcpy(&tth->start, &io_test->start, sizeof(tth->start));
224 tth->buf = malloc(io_test->size, M_NVME, M_WAITOK);
225 tth->size = io_test->size;
226 tth->time = io_test->time;
227 tth->idx = atomic_fetchadd_int(&io_test->td_idx, 1);
229 memset(&cpl, 0, sizeof(cpl));
231 nvme_ns_io_test_cb(tth, &cpl);
233 error = tsleep(tth, 0, "test_wait", tth->time*hz*2);
236 printf("%s: error = %d\n", __func__, error);
238 io_test->io_completed[tth->idx] = tth->io_completed;
241 free(tth->buf, M_NVME);
244 atomic_subtract_int(&io_test->td_active, 1);
247 #if __FreeBSD_version >= 800004
255 nvme_ns_test(struct nvme_namespace *ns, u_long cmd, caddr_t arg)
257 struct nvme_io_test *io_test;
258 struct nvme_io_test_internal *io_test_internal;
262 io_test = (struct nvme_io_test *)arg;
264 if ((io_test->opc != NVME_OPC_READ) &&
265 (io_test->opc != NVME_OPC_WRITE))
268 if (io_test->size % nvme_ns_get_sector_size(ns))
271 io_test_internal = malloc(sizeof(*io_test_internal), M_NVME,
273 io_test_internal->opc = io_test->opc;
274 io_test_internal->ns = ns;
275 io_test_internal->td_active = io_test->num_threads;
276 io_test_internal->time = io_test->time;
277 io_test_internal->size = io_test->size;
278 io_test_internal->flags = io_test->flags;
280 if (cmd == NVME_IO_TEST)
281 fn = nvme_ns_io_test;
283 fn = nvme_ns_bio_test;
285 getmicrouptime(&io_test_internal->start);
287 for (i = 0; i < io_test->num_threads; i++)
288 #if __FreeBSD_version >= 800004
289 kthread_add(fn, io_test_internal,
290 NULL, NULL, 0, 0, "nvme_io_test[%d]", i);
292 kthread_create(fn, io_test_internal,
293 NULL, 0, 0, "nvme_io_test[%d]", i);
296 tsleep(io_test_internal, 0, "nvme_test", io_test->time * 2 * hz);
298 while (io_test_internal->td_active > 0)
301 memcpy(io_test->io_completed, io_test_internal->io_completed,
302 sizeof(io_test->io_completed));
304 free(io_test_internal, M_NVME);