xref: /freebsd/sys/dev/nvd/nvd.c (revision ce6a89e27cd190313be39bb479880aeda4778436)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2012-2016 Intel Corporation
5  * All rights reserved.
6  * Copyright (C) 2018 Alexander Motin <mav@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/bio.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/queue.h>
39 #include <sys/sysctl.h>
40 #include <sys/systm.h>
41 #include <sys/taskqueue.h>
42 #include <machine/atomic.h>
43 
44 #include <geom/geom.h>
45 #include <geom/geom_disk.h>
46 
47 #include <dev/nvme/nvme.h>
48 
49 #define NVD_STR		"nvd"
50 
51 struct nvd_disk;
52 struct nvd_controller;
53 
54 static disk_ioctl_t nvd_ioctl;
55 static disk_strategy_t nvd_strategy;
56 static dumper_t nvd_dump;
57 static disk_getattr_t nvd_getattr;
58 
59 static void nvd_done(void *arg, const struct nvme_completion *cpl);
60 static void nvd_gone(struct nvd_disk *ndisk);
61 
62 static void *nvd_new_disk(struct nvme_namespace *ns, void *ctrlr);
63 
64 static void *nvd_new_controller(struct nvme_controller *ctrlr);
65 static void nvd_controller_fail(void *ctrlr);
66 
67 static int nvd_load(void);
68 static void nvd_unload(void);
69 
70 MALLOC_DEFINE(M_NVD, "nvd", "nvd(4) allocations");
71 
72 struct nvme_consumer *consumer_handle;
73 
74 struct nvd_disk {
75 	struct nvd_controller	*ctrlr;
76 
77 	struct bio_queue_head	bioq;
78 	struct task		bioqtask;
79 	struct mtx		bioqlock;
80 
81 	struct disk		*disk;
82 	struct taskqueue	*tq;
83 	struct nvme_namespace	*ns;
84 
85 	uint32_t		cur_depth;
86 #define	NVD_ODEPTH	(1 << 30)
87 	uint32_t		ordered_in_flight;
88 	u_int			unit;
89 
90 	TAILQ_ENTRY(nvd_disk)	global_tailq;
91 	TAILQ_ENTRY(nvd_disk)	ctrlr_tailq;
92 };
93 
94 struct nvd_controller {
95 
96 	TAILQ_ENTRY(nvd_controller)	tailq;
97 	TAILQ_HEAD(, nvd_disk)		disk_head;
98 };
99 
100 static struct mtx			nvd_lock;
101 static TAILQ_HEAD(, nvd_controller)	ctrlr_head;
102 static TAILQ_HEAD(disk_list, nvd_disk)	disk_head;
103 
104 static SYSCTL_NODE(_hw, OID_AUTO, nvd, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
105     "nvd driver parameters");
106 /*
107  * The NVMe specification does not define a maximum or optimal delete size, so
108  *  technically max delete size is min(full size of the namespace, 2^32 - 1
109  *  LBAs).  A single delete for a multi-TB NVMe namespace though may take much
110  *  longer to complete than the nvme(4) I/O timeout period.  So choose a sensible
111  *  default here that is still suitably large to minimize the number of overall
112  *  delete operations.
113  */
114 static uint64_t nvd_delete_max = (1024 * 1024 * 1024);  /* 1GB */
115 SYSCTL_UQUAD(_hw_nvd, OID_AUTO, delete_max, CTLFLAG_RDTUN, &nvd_delete_max, 0,
116 	     "nvd maximum BIO_DELETE size in bytes");
117 
118 static int nvd_modevent(module_t mod, int type, void *arg)
119 {
120 	int error = 0;
121 
122 	switch (type) {
123 	case MOD_LOAD:
124 		error = nvd_load();
125 		break;
126 	case MOD_UNLOAD:
127 		nvd_unload();
128 		break;
129 	default:
130 		break;
131 	}
132 
133 	return (error);
134 }
135 
136 moduledata_t nvd_mod = {
137 	NVD_STR,
138 	(modeventhand_t)nvd_modevent,
139 	0
140 };
141 
142 DECLARE_MODULE(nvd, nvd_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
143 MODULE_VERSION(nvd, 1);
144 MODULE_DEPEND(nvd, nvme, 1, 1, 1);
145 
146 static int
147 nvd_load()
148 {
149 	if (!nvme_use_nvd)
150 		return 0;
151 
152 	mtx_init(&nvd_lock, "nvd_lock", NULL, MTX_DEF);
153 	TAILQ_INIT(&ctrlr_head);
154 	TAILQ_INIT(&disk_head);
155 
156 	consumer_handle = nvme_register_consumer(nvd_new_disk,
157 	    nvd_new_controller, NULL, nvd_controller_fail);
158 
159 	return (consumer_handle != NULL ? 0 : -1);
160 }
161 
162 static void
163 nvd_unload()
164 {
165 	struct nvd_controller	*ctrlr;
166 	struct nvd_disk		*ndisk;
167 
168 	if (!nvme_use_nvd)
169 		return;
170 
171 	mtx_lock(&nvd_lock);
172 	while ((ctrlr = TAILQ_FIRST(&ctrlr_head)) != NULL) {
173 		TAILQ_REMOVE(&ctrlr_head, ctrlr, tailq);
174 		TAILQ_FOREACH(ndisk, &ctrlr->disk_head, ctrlr_tailq)
175 			nvd_gone(ndisk);
176 		while (!TAILQ_EMPTY(&ctrlr->disk_head))
177 			msleep(&ctrlr->disk_head, &nvd_lock, 0, "nvd_unload",0);
178 		free(ctrlr, M_NVD);
179 	}
180 	mtx_unlock(&nvd_lock);
181 
182 	nvme_unregister_consumer(consumer_handle);
183 
184 	mtx_destroy(&nvd_lock);
185 }
186 
187 static void
188 nvd_bio_submit(struct nvd_disk *ndisk, struct bio *bp)
189 {
190 	int err;
191 
192 	bp->bio_driver1 = NULL;
193 	if (__predict_false(bp->bio_flags & BIO_ORDERED))
194 		atomic_add_int(&ndisk->cur_depth, NVD_ODEPTH);
195 	else
196 		atomic_add_int(&ndisk->cur_depth, 1);
197 	err = nvme_ns_bio_process(ndisk->ns, bp, nvd_done);
198 	if (err) {
199 		if (__predict_false(bp->bio_flags & BIO_ORDERED)) {
200 			atomic_add_int(&ndisk->cur_depth, -NVD_ODEPTH);
201 			atomic_add_int(&ndisk->ordered_in_flight, -1);
202 			wakeup(&ndisk->cur_depth);
203 		} else {
204 			if (atomic_fetchadd_int(&ndisk->cur_depth, -1) == 1 &&
205 			    __predict_false(ndisk->ordered_in_flight != 0))
206 				wakeup(&ndisk->cur_depth);
207 		}
208 		bp->bio_error = err;
209 		bp->bio_flags |= BIO_ERROR;
210 		bp->bio_resid = bp->bio_bcount;
211 		biodone(bp);
212 	}
213 }
214 
215 static void
216 nvd_strategy(struct bio *bp)
217 {
218 	struct nvd_disk *ndisk = (struct nvd_disk *)bp->bio_disk->d_drv1;
219 
220 	/*
221 	 * bio with BIO_ORDERED flag must be executed after all previous
222 	 * bios in the queue, and before any successive bios.
223 	 */
224 	if (__predict_false(bp->bio_flags & BIO_ORDERED)) {
225 		if (atomic_fetchadd_int(&ndisk->ordered_in_flight, 1) == 0 &&
226 		    ndisk->cur_depth == 0 && bioq_first(&ndisk->bioq) == NULL) {
227 			nvd_bio_submit(ndisk, bp);
228 			return;
229 		}
230 	} else if (__predict_true(ndisk->ordered_in_flight == 0)) {
231 		nvd_bio_submit(ndisk, bp);
232 		return;
233 	}
234 
235 	/*
236 	 * There are ordered bios in flight, so we need to submit
237 	 *  bios through the task queue to enforce ordering.
238 	 */
239 	mtx_lock(&ndisk->bioqlock);
240 	bioq_insert_tail(&ndisk->bioq, bp);
241 	mtx_unlock(&ndisk->bioqlock);
242 	taskqueue_enqueue(ndisk->tq, &ndisk->bioqtask);
243 }
244 
245 static void
246 nvd_gone(struct nvd_disk *ndisk)
247 {
248 	struct bio	*bp;
249 
250 	printf(NVD_STR"%u: detached\n", ndisk->unit);
251 	mtx_lock(&ndisk->bioqlock);
252 	disk_gone(ndisk->disk);
253 	while ((bp = bioq_takefirst(&ndisk->bioq)) != NULL) {
254 		if (__predict_false(bp->bio_flags & BIO_ORDERED))
255 			atomic_add_int(&ndisk->ordered_in_flight, -1);
256 		bp->bio_error = ENXIO;
257 		bp->bio_flags |= BIO_ERROR;
258 		bp->bio_resid = bp->bio_bcount;
259 		biodone(bp);
260 	}
261 	mtx_unlock(&ndisk->bioqlock);
262 }
263 
264 static void
265 nvd_gonecb(struct disk *dp)
266 {
267 	struct nvd_disk *ndisk = (struct nvd_disk *)dp->d_drv1;
268 
269 	disk_destroy(ndisk->disk);
270 	mtx_lock(&nvd_lock);
271 	TAILQ_REMOVE(&disk_head, ndisk, global_tailq);
272 	TAILQ_REMOVE(&ndisk->ctrlr->disk_head, ndisk, ctrlr_tailq);
273 	if (TAILQ_EMPTY(&ndisk->ctrlr->disk_head))
274 		wakeup(&ndisk->ctrlr->disk_head);
275 	mtx_unlock(&nvd_lock);
276 	taskqueue_free(ndisk->tq);
277 	mtx_destroy(&ndisk->bioqlock);
278 	free(ndisk, M_NVD);
279 }
280 
281 static int
282 nvd_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
283     struct thread *td)
284 {
285 	struct nvd_disk		*ndisk = dp->d_drv1;
286 
287 	return (nvme_ns_ioctl_process(ndisk->ns, cmd, data, fflag, td));
288 }
289 
290 static int
291 nvd_dump(void *arg, void *virt, vm_offset_t phys, off_t offset, size_t len)
292 {
293 	struct disk *dp = arg;
294 	struct nvd_disk *ndisk = dp->d_drv1;
295 
296 	return (nvme_ns_dump(ndisk->ns, virt, offset, len));
297 }
298 
299 static int
300 nvd_getattr(struct bio *bp)
301 {
302 	struct nvd_disk *ndisk = (struct nvd_disk *)bp->bio_disk->d_drv1;
303 	const struct nvme_namespace_data *nsdata;
304 	u_int i;
305 
306 	if (!strcmp("GEOM::lunid", bp->bio_attribute)) {
307 		nsdata = nvme_ns_get_data(ndisk->ns);
308 
309 		/* Try to return NGUID as lunid. */
310 		for (i = 0; i < sizeof(nsdata->nguid); i++) {
311 			if (nsdata->nguid[i] != 0)
312 				break;
313 		}
314 		if (i < sizeof(nsdata->nguid)) {
315 			if (bp->bio_length < sizeof(nsdata->nguid) * 2 + 1)
316 				return (EFAULT);
317 			for (i = 0; i < sizeof(nsdata->nguid); i++) {
318 				sprintf(&bp->bio_data[i * 2], "%02x",
319 				    nsdata->nguid[i]);
320 			}
321 			bp->bio_completed = bp->bio_length;
322 			return (0);
323 		}
324 
325 		/* Try to return EUI64 as lunid. */
326 		for (i = 0; i < sizeof(nsdata->eui64); i++) {
327 			if (nsdata->eui64[i] != 0)
328 				break;
329 		}
330 		if (i < sizeof(nsdata->eui64)) {
331 			if (bp->bio_length < sizeof(nsdata->eui64) * 2 + 1)
332 				return (EFAULT);
333 			for (i = 0; i < sizeof(nsdata->eui64); i++) {
334 				sprintf(&bp->bio_data[i * 2], "%02x",
335 				    nsdata->eui64[i]);
336 			}
337 			bp->bio_completed = bp->bio_length;
338 			return (0);
339 		}
340 	}
341 	return (-1);
342 }
343 
344 static void
345 nvd_done(void *arg, const struct nvme_completion *cpl)
346 {
347 	struct bio *bp = (struct bio *)arg;
348 	struct nvd_disk *ndisk = bp->bio_disk->d_drv1;
349 
350 	if (__predict_false(bp->bio_flags & BIO_ORDERED)) {
351 		atomic_add_int(&ndisk->cur_depth, -NVD_ODEPTH);
352 		atomic_add_int(&ndisk->ordered_in_flight, -1);
353 		wakeup(&ndisk->cur_depth);
354 	} else {
355 		if (atomic_fetchadd_int(&ndisk->cur_depth, -1) == 1 &&
356 		    __predict_false(ndisk->ordered_in_flight != 0))
357 			wakeup(&ndisk->cur_depth);
358 	}
359 
360 	biodone(bp);
361 }
362 
363 static void
364 nvd_bioq_process(void *arg, int pending)
365 {
366 	struct nvd_disk *ndisk = arg;
367 	struct bio *bp;
368 
369 	for (;;) {
370 		mtx_lock(&ndisk->bioqlock);
371 		bp = bioq_takefirst(&ndisk->bioq);
372 		mtx_unlock(&ndisk->bioqlock);
373 		if (bp == NULL)
374 			break;
375 
376 		if (__predict_false(bp->bio_flags & BIO_ORDERED)) {
377 			/*
378 			 * bio with BIO_ORDERED flag set must be executed
379 			 * after all previous bios.
380 			 */
381 			while (ndisk->cur_depth > 0)
382 				tsleep(&ndisk->cur_depth, 0, "nvdorb", 1);
383 		} else {
384 			/*
385 			 * bio with BIO_ORDERED flag set must be completed
386 			 * before proceeding with additional bios.
387 			 */
388 			while (ndisk->cur_depth >= NVD_ODEPTH)
389 				tsleep(&ndisk->cur_depth, 0, "nvdora", 1);
390 		}
391 
392 		nvd_bio_submit(ndisk, bp);
393 	}
394 }
395 
396 static void *
397 nvd_new_controller(struct nvme_controller *ctrlr)
398 {
399 	struct nvd_controller	*nvd_ctrlr;
400 
401 	nvd_ctrlr = malloc(sizeof(struct nvd_controller), M_NVD,
402 	    M_ZERO | M_WAITOK);
403 
404 	TAILQ_INIT(&nvd_ctrlr->disk_head);
405 	mtx_lock(&nvd_lock);
406 	TAILQ_INSERT_TAIL(&ctrlr_head, nvd_ctrlr, tailq);
407 	mtx_unlock(&nvd_lock);
408 
409 	return (nvd_ctrlr);
410 }
411 
412 static void *
413 nvd_new_disk(struct nvme_namespace *ns, void *ctrlr_arg)
414 {
415 	uint8_t			descr[NVME_MODEL_NUMBER_LENGTH+1];
416 	struct nvd_disk		*ndisk, *tnd;
417 	struct disk		*disk;
418 	struct nvd_controller	*ctrlr = ctrlr_arg;
419 	int unit;
420 
421 	ndisk = malloc(sizeof(struct nvd_disk), M_NVD, M_ZERO | M_WAITOK);
422 	ndisk->ctrlr = ctrlr;
423 	ndisk->ns = ns;
424 	ndisk->cur_depth = 0;
425 	ndisk->ordered_in_flight = 0;
426 	mtx_init(&ndisk->bioqlock, "nvd bioq lock", NULL, MTX_DEF);
427 	bioq_init(&ndisk->bioq);
428 	TASK_INIT(&ndisk->bioqtask, 0, nvd_bioq_process, ndisk);
429 
430 	mtx_lock(&nvd_lock);
431 	unit = 0;
432 	TAILQ_FOREACH(tnd, &disk_head, global_tailq) {
433 		if (tnd->unit > unit)
434 			break;
435 		unit = tnd->unit + 1;
436 	}
437 	ndisk->unit = unit;
438 	if (tnd != NULL)
439 		TAILQ_INSERT_BEFORE(tnd, ndisk, global_tailq);
440 	else
441 		TAILQ_INSERT_TAIL(&disk_head, ndisk, global_tailq);
442 	TAILQ_INSERT_TAIL(&ctrlr->disk_head, ndisk, ctrlr_tailq);
443 	mtx_unlock(&nvd_lock);
444 
445 	ndisk->tq = taskqueue_create("nvd_taskq", M_WAITOK,
446 	    taskqueue_thread_enqueue, &ndisk->tq);
447 	taskqueue_start_threads(&ndisk->tq, 1, PI_DISK, "nvd taskq");
448 
449 	disk = ndisk->disk = disk_alloc();
450 	disk->d_strategy = nvd_strategy;
451 	disk->d_ioctl = nvd_ioctl;
452 	disk->d_dump = nvd_dump;
453 	disk->d_getattr = nvd_getattr;
454 	disk->d_gone = nvd_gonecb;
455 	disk->d_name = NVD_STR;
456 	disk->d_unit = ndisk->unit;
457 	disk->d_drv1 = ndisk;
458 
459 	disk->d_sectorsize = nvme_ns_get_sector_size(ns);
460 	disk->d_mediasize = (off_t)nvme_ns_get_size(ns);
461 	disk->d_maxsize = nvme_ns_get_max_io_xfer_size(ns);
462 	disk->d_delmaxsize = (off_t)nvme_ns_get_size(ns);
463 	if (disk->d_delmaxsize > nvd_delete_max)
464 		disk->d_delmaxsize = nvd_delete_max;
465 	disk->d_stripesize = nvme_ns_get_stripesize(ns);
466 	disk->d_flags = DISKFLAG_UNMAPPED_BIO | DISKFLAG_DIRECT_COMPLETION;
467 	if (nvme_ns_get_flags(ns) & NVME_NS_DEALLOCATE_SUPPORTED)
468 		disk->d_flags |= DISKFLAG_CANDELETE;
469 	if (nvme_ns_get_flags(ns) & NVME_NS_FLUSH_SUPPORTED)
470 		disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
471 
472 	/*
473 	 * d_ident and d_descr are both far bigger than the length of either
474 	 *  the serial or model number strings.
475 	 */
476 	nvme_strvis(disk->d_ident, nvme_ns_get_serial_number(ns),
477 	    sizeof(disk->d_ident), NVME_SERIAL_NUMBER_LENGTH);
478 	nvme_strvis(descr, nvme_ns_get_model_number(ns), sizeof(descr),
479 	    NVME_MODEL_NUMBER_LENGTH);
480 	strlcpy(disk->d_descr, descr, sizeof(descr));
481 
482 	disk->d_rotation_rate = DISK_RR_NON_ROTATING;
483 
484 	disk_create(disk, DISK_VERSION);
485 
486 	printf(NVD_STR"%u: <%s> NVMe namespace\n", disk->d_unit, descr);
487 	printf(NVD_STR"%u: %juMB (%ju %u byte sectors)\n", disk->d_unit,
488 		(uintmax_t)disk->d_mediasize / (1024*1024),
489 		(uintmax_t)disk->d_mediasize / disk->d_sectorsize,
490 		disk->d_sectorsize);
491 
492 	return (ndisk);
493 }
494 
495 static void
496 nvd_controller_fail(void *ctrlr_arg)
497 {
498 	struct nvd_controller	*ctrlr = ctrlr_arg;
499 	struct nvd_disk		*ndisk;
500 
501 	mtx_lock(&nvd_lock);
502 	TAILQ_REMOVE(&ctrlr_head, ctrlr, tailq);
503 	TAILQ_FOREACH(ndisk, &ctrlr->disk_head, ctrlr_tailq)
504 		nvd_gone(ndisk);
505 	while (!TAILQ_EMPTY(&ctrlr->disk_head))
506 		msleep(&ctrlr->disk_head, &nvd_lock, 0, "nvd_fail", 0);
507 	mtx_unlock(&nvd_lock);
508 	free(ctrlr, M_NVD);
509 }
510 
511