xref: /freebsd/sys/dev/nvd/nvd.c (revision 47ef4244)
1 /*-
2  * Copyright (C) 2012-2013 Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/bio.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
35 #include <sys/systm.h>
36 #include <sys/taskqueue.h>
37 
38 #include <geom/geom.h>
39 #include <geom/geom_disk.h>
40 
41 #include <dev/nvme/nvme.h>
42 
43 #define NVD_STR		"nvd"
44 
45 struct nvd_disk;
46 
47 static disk_ioctl_t nvd_ioctl;
48 static disk_strategy_t nvd_strategy;
49 
50 static void nvd_done(void *arg, const struct nvme_completion *cpl);
51 
52 static void *nvd_new_disk(struct nvme_namespace *ns, void *ctrlr);
53 static void destroy_geom_disk(struct nvd_disk *ndisk);
54 
55 static void *nvd_new_controller(struct nvme_controller *ctrlr);
56 static void nvd_controller_fail(void *ctrlr);
57 
58 static int nvd_load(void);
59 static void nvd_unload(void);
60 
61 MALLOC_DEFINE(M_NVD, "nvd", "nvd(4) allocations");
62 
63 struct nvme_consumer *consumer_handle;
64 
65 struct nvd_disk {
66 
67 	struct bio_queue_head	bioq;
68 	struct task		bioqtask;
69 	struct mtx		bioqlock;
70 
71 	struct disk		*disk;
72 	struct taskqueue	*tq;
73 	struct nvme_namespace	*ns;
74 
75 	uint32_t		cur_depth;
76 
77 	TAILQ_ENTRY(nvd_disk)	global_tailq;
78 	TAILQ_ENTRY(nvd_disk)	ctrlr_tailq;
79 };
80 
81 struct nvd_controller {
82 
83 	TAILQ_ENTRY(nvd_controller)	tailq;
84 	TAILQ_HEAD(, nvd_disk)		disk_head;
85 };
86 
87 static TAILQ_HEAD(, nvd_controller)	ctrlr_head;
88 static TAILQ_HEAD(disk_list, nvd_disk)	disk_head;
89 
90 static int nvd_modevent(module_t mod, int type, void *arg)
91 {
92 	int error = 0;
93 
94 	switch (type) {
95 	case MOD_LOAD:
96 		error = nvd_load();
97 		break;
98 	case MOD_UNLOAD:
99 		nvd_unload();
100 		break;
101 	default:
102 		break;
103 	}
104 
105 	return (error);
106 }
107 
108 moduledata_t nvd_mod = {
109 	NVD_STR,
110 	(modeventhand_t)nvd_modevent,
111 	0
112 };
113 
114 DECLARE_MODULE(nvd, nvd_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
115 MODULE_VERSION(nvd, 1);
116 MODULE_DEPEND(nvd, nvme, 1, 1, 1);
117 
118 static int
119 nvd_load()
120 {
121 
122 	TAILQ_INIT(&ctrlr_head);
123 	TAILQ_INIT(&disk_head);
124 
125 	consumer_handle = nvme_register_consumer(nvd_new_disk,
126 	    nvd_new_controller, NULL, nvd_controller_fail);
127 
128 	return (consumer_handle != NULL ? 0 : -1);
129 }
130 
131 static void
132 nvd_unload()
133 {
134 	struct nvd_controller	*ctrlr;
135 	struct nvd_disk		*disk;
136 
137 	while (!TAILQ_EMPTY(&ctrlr_head)) {
138 		ctrlr = TAILQ_FIRST(&ctrlr_head);
139 		TAILQ_REMOVE(&ctrlr_head, ctrlr, tailq);
140 		free(ctrlr, M_NVD);
141 	}
142 
143 	while (!TAILQ_EMPTY(&disk_head)) {
144 		disk = TAILQ_FIRST(&disk_head);
145 		TAILQ_REMOVE(&disk_head, disk, global_tailq);
146 		destroy_geom_disk(disk);
147 		free(disk, M_NVD);
148 	}
149 
150 	nvme_unregister_consumer(consumer_handle);
151 }
152 
153 static int
154 nvd_bio_submit(struct nvd_disk *ndisk, struct bio *bp)
155 {
156 	int err;
157 
158 	bp->bio_driver1 = NULL;
159 	atomic_add_int(&ndisk->cur_depth, 1);
160 	err = nvme_ns_bio_process(ndisk->ns, bp, nvd_done);
161 	if (err) {
162 		atomic_add_int(&ndisk->cur_depth, -1);
163 		bp->bio_error = err;
164 		bp->bio_flags |= BIO_ERROR;
165 		bp->bio_resid = bp->bio_bcount;
166 		biodone(bp);
167 		return (-1);
168 	}
169 
170 	return (0);
171 }
172 
173 static void
174 nvd_strategy(struct bio *bp)
175 {
176 	struct nvd_disk *ndisk;
177 
178 	ndisk = (struct nvd_disk *)bp->bio_disk->d_drv1;
179 
180 	mtx_lock(&ndisk->bioqlock);
181 	bioq_insert_tail(&ndisk->bioq, bp);
182 	mtx_unlock(&ndisk->bioqlock);
183 	taskqueue_enqueue(ndisk->tq, &ndisk->bioqtask);
184 }
185 
186 static int
187 nvd_ioctl(struct disk *ndisk, u_long cmd, void *data, int fflag,
188     struct thread *td)
189 {
190 	int ret = 0;
191 
192 	switch (cmd) {
193 	default:
194 		ret = EIO;
195 	}
196 
197 	return (ret);
198 }
199 
200 static void
201 nvd_done(void *arg, const struct nvme_completion *cpl)
202 {
203 	struct bio *bp;
204 	struct nvd_disk *ndisk;
205 
206 	bp = (struct bio *)arg;
207 
208 	ndisk = bp->bio_disk->d_drv1;
209 
210 	atomic_add_int(&ndisk->cur_depth, -1);
211 
212 	biodone(bp);
213 }
214 
215 static void
216 nvd_bioq_process(void *arg, int pending)
217 {
218 	struct nvd_disk *ndisk = arg;
219 	struct bio *bp;
220 
221 	for (;;) {
222 		mtx_lock(&ndisk->bioqlock);
223 		bp = bioq_takefirst(&ndisk->bioq);
224 		mtx_unlock(&ndisk->bioqlock);
225 		if (bp == NULL)
226 			break;
227 
228 		if (nvd_bio_submit(ndisk, bp) != 0) {
229 			continue;
230 		}
231 
232 #ifdef BIO_ORDERED
233 		/*
234 		 * BIO_ORDERED flag dictates that the bio with BIO_ORDERED
235 		 *  flag set must be completed before proceeding with
236 		 *  additional bios.
237 		 */
238 		if (bp->bio_flags & BIO_ORDERED) {
239 			while (ndisk->cur_depth > 0) {
240 				pause("nvd flush", 1);
241 			}
242 		}
243 #endif
244 	}
245 }
246 
247 static void *
248 nvd_new_controller(struct nvme_controller *ctrlr)
249 {
250 	struct nvd_controller	*nvd_ctrlr;
251 
252 	nvd_ctrlr = malloc(sizeof(struct nvd_controller), M_NVD,
253 	    M_ZERO | M_WAITOK);
254 
255 	TAILQ_INIT(&nvd_ctrlr->disk_head);
256 	TAILQ_INSERT_TAIL(&ctrlr_head, nvd_ctrlr, tailq);
257 
258 	return (nvd_ctrlr);
259 }
260 
261 static void *
262 nvd_new_disk(struct nvme_namespace *ns, void *ctrlr_arg)
263 {
264 	uint8_t			descr[NVME_MODEL_NUMBER_LENGTH+1];
265 	struct nvd_disk		*ndisk;
266 	struct disk		*disk;
267 	struct nvd_controller	*ctrlr = ctrlr_arg;
268 
269 	ndisk = malloc(sizeof(struct nvd_disk), M_NVD, M_ZERO | M_WAITOK);
270 
271 	disk = disk_alloc();
272 	disk->d_strategy = nvd_strategy;
273 	disk->d_ioctl = nvd_ioctl;
274 	disk->d_name = NVD_STR;
275 	disk->d_drv1 = ndisk;
276 
277 	disk->d_maxsize = nvme_ns_get_max_io_xfer_size(ns);
278 	disk->d_sectorsize = nvme_ns_get_sector_size(ns);
279 	disk->d_mediasize = (off_t)nvme_ns_get_size(ns);
280 	disk->d_delmaxsize = (off_t)nvme_ns_get_size(ns);
281 	disk->d_stripesize = nvme_ns_get_optimal_sector_size(ns);
282 
283 	if (TAILQ_EMPTY(&disk_head))
284 		disk->d_unit = 0;
285 	else
286 		disk->d_unit =
287 		    TAILQ_LAST(&disk_head, disk_list)->disk->d_unit + 1;
288 
289 	disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
290 
291 	if (nvme_ns_get_flags(ns) & NVME_NS_DEALLOCATE_SUPPORTED)
292 		disk->d_flags |= DISKFLAG_CANDELETE;
293 
294 	if (nvme_ns_get_flags(ns) & NVME_NS_FLUSH_SUPPORTED)
295 		disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
296 
297 /* ifdef used here to ease porting to stable branches at a later point. */
298 #ifdef DISKFLAG_UNMAPPED_BIO
299 	disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
300 #endif
301 
302 	/*
303 	 * d_ident and d_descr are both far bigger than the length of either
304 	 *  the serial or model number strings.
305 	 */
306 	nvme_strvis(disk->d_ident, nvme_ns_get_serial_number(ns),
307 	    sizeof(disk->d_ident), NVME_SERIAL_NUMBER_LENGTH);
308 
309 	nvme_strvis(descr, nvme_ns_get_model_number(ns), sizeof(descr),
310 	    NVME_MODEL_NUMBER_LENGTH);
311 
312 #if __FreeBSD_version >= 900034
313 	strlcpy(disk->d_descr, descr, sizeof(descr));
314 #endif
315 
316 	ndisk->ns = ns;
317 	ndisk->disk = disk;
318 	ndisk->cur_depth = 0;
319 
320 	mtx_init(&ndisk->bioqlock, "NVD bioq lock", NULL, MTX_DEF);
321 	bioq_init(&ndisk->bioq);
322 
323 	TASK_INIT(&ndisk->bioqtask, 0, nvd_bioq_process, ndisk);
324 	ndisk->tq = taskqueue_create("nvd_taskq", M_WAITOK,
325 	    taskqueue_thread_enqueue, &ndisk->tq);
326 	taskqueue_start_threads(&ndisk->tq, 1, PI_DISK, "nvd taskq");
327 
328 	TAILQ_INSERT_TAIL(&disk_head, ndisk, global_tailq);
329 	TAILQ_INSERT_TAIL(&ctrlr->disk_head, ndisk, ctrlr_tailq);
330 
331 	disk_create(disk, DISK_VERSION);
332 
333 	printf(NVD_STR"%u: <%s> NVMe namespace\n", disk->d_unit, descr);
334 	printf(NVD_STR"%u: %juMB (%ju %u byte sectors)\n", disk->d_unit,
335 		(uintmax_t)disk->d_mediasize / (1024*1024),
336 		(uintmax_t)disk->d_mediasize / disk->d_sectorsize,
337 		disk->d_sectorsize);
338 
339 	return (NULL);
340 }
341 
342 static void
343 destroy_geom_disk(struct nvd_disk *ndisk)
344 {
345 	struct bio	*bp;
346 	struct disk	*disk;
347 	uint32_t	unit;
348 	int		cnt = 0;
349 
350 	disk = ndisk->disk;
351 	unit = disk->d_unit;
352 	taskqueue_free(ndisk->tq);
353 
354 	disk_destroy(ndisk->disk);
355 
356 	mtx_lock(&ndisk->bioqlock);
357 	for (;;) {
358 		bp = bioq_takefirst(&ndisk->bioq);
359 		if (bp == NULL)
360 			break;
361 		bp->bio_error = EIO;
362 		bp->bio_flags |= BIO_ERROR;
363 		bp->bio_resid = bp->bio_bcount;
364 		cnt++;
365 		biodone(bp);
366 	}
367 
368 	printf(NVD_STR"%u: lost device - %d outstanding\n", unit, cnt);
369 	printf(NVD_STR"%u: removing device entry\n", unit);
370 
371 	mtx_unlock(&ndisk->bioqlock);
372 
373 	mtx_destroy(&ndisk->bioqlock);
374 }
375 
376 static void
377 nvd_controller_fail(void *ctrlr_arg)
378 {
379 	struct nvd_controller	*ctrlr = ctrlr_arg;
380 	struct nvd_disk		*disk;
381 
382 	while (!TAILQ_EMPTY(&ctrlr->disk_head)) {
383 		disk = TAILQ_FIRST(&ctrlr->disk_head);
384 		TAILQ_REMOVE(&disk_head, disk, global_tailq);
385 		TAILQ_REMOVE(&ctrlr->disk_head, disk, ctrlr_tailq);
386 		destroy_geom_disk(disk);
387 		free(disk, M_NVD);
388 	}
389 
390 	TAILQ_REMOVE(&ctrlr_head, ctrlr, tailq);
391 	free(ctrlr, M_NVD);
392 }
393 
394