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