xref: /freebsd/sys/cam/ctl/ctl_backend_ramdisk.c (revision c9dbb1cc52b063bbd9ab078a7afc89a8696da659)
1 /*-
2  * Copyright (c) 2003, 2008 Silicon Graphics International Corp.
3  * Copyright (c) 2012 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Portions of this software were developed by Edward Tomasz Napierala
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions, and the following disclaimer,
14  *    without modification.
15  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16  *    substantially similar to the "NO WARRANTY" disclaimer below
17  *    ("Disclaimer") and any redistribution must be conditioned upon
18  *    including a substantially similar Disclaimer requirement for further
19  *    binary redistribution.
20  *
21  * NO WARRANTY
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGES.
33  *
34  * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_ramdisk.c#3 $
35  */
36 /*
37  * CAM Target Layer backend for a "fake" ramdisk.
38  *
39  * Author: Ken Merry <ken@FreeBSD.org>
40  */
41 
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/condvar.h>
49 #include <sys/types.h>
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/malloc.h>
53 #include <sys/taskqueue.h>
54 #include <sys/time.h>
55 #include <sys/queue.h>
56 #include <sys/conf.h>
57 #include <sys/ioccom.h>
58 #include <sys/module.h>
59 #include <sys/sysctl.h>
60 
61 #include <cam/scsi/scsi_all.h>
62 #include <cam/scsi/scsi_da.h>
63 #include <cam/ctl/ctl_io.h>
64 #include <cam/ctl/ctl.h>
65 #include <cam/ctl/ctl_util.h>
66 #include <cam/ctl/ctl_backend.h>
67 #include <cam/ctl/ctl_debug.h>
68 #include <cam/ctl/ctl_ioctl.h>
69 #include <cam/ctl/ctl_ha.h>
70 #include <cam/ctl/ctl_private.h>
71 #include <cam/ctl/ctl_error.h>
72 
73 typedef enum {
74 	CTL_BE_RAMDISK_LUN_UNCONFIGURED	= 0x01,
75 	CTL_BE_RAMDISK_LUN_CONFIG_ERR	= 0x02,
76 	CTL_BE_RAMDISK_LUN_WAITING	= 0x04
77 } ctl_be_ramdisk_lun_flags;
78 
79 struct ctl_be_ramdisk_lun {
80 	struct ctl_lun_create_params params;
81 	char lunname[32];
82 	uint64_t size_bytes;
83 	uint64_t size_blocks;
84 	struct ctl_be_ramdisk_softc *softc;
85 	ctl_be_ramdisk_lun_flags flags;
86 	STAILQ_ENTRY(ctl_be_ramdisk_lun) links;
87 	struct ctl_be_lun cbe_lun;
88 	struct taskqueue *io_taskqueue;
89 	struct task io_task;
90 	STAILQ_HEAD(, ctl_io_hdr) cont_queue;
91 	struct mtx_padalign queue_lock;
92 };
93 
94 struct ctl_be_ramdisk_softc {
95 	struct mtx lock;
96 	int rd_size;
97 #ifdef CTL_RAMDISK_PAGES
98 	uint8_t **ramdisk_pages;
99 	int num_pages;
100 #else
101 	uint8_t *ramdisk_buffer;
102 #endif
103 	int num_luns;
104 	STAILQ_HEAD(, ctl_be_ramdisk_lun) lun_list;
105 };
106 
107 static struct ctl_be_ramdisk_softc rd_softc;
108 extern struct ctl_softc *control_softc;
109 
110 int ctl_backend_ramdisk_init(void);
111 void ctl_backend_ramdisk_shutdown(void);
112 static int ctl_backend_ramdisk_move_done(union ctl_io *io);
113 static int ctl_backend_ramdisk_submit(union ctl_io *io);
114 static void ctl_backend_ramdisk_continue(union ctl_io *io);
115 static int ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd,
116 				     caddr_t addr, int flag, struct thread *td);
117 static int ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
118 				  struct ctl_lun_req *req);
119 static int ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
120 				      struct ctl_lun_req *req);
121 static int ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
122 				  struct ctl_lun_req *req);
123 static void ctl_backend_ramdisk_worker(void *context, int pending);
124 static void ctl_backend_ramdisk_lun_shutdown(void *be_lun);
125 static void ctl_backend_ramdisk_lun_config_status(void *be_lun,
126 						  ctl_lun_config_status status);
127 static int ctl_backend_ramdisk_config_write(union ctl_io *io);
128 static int ctl_backend_ramdisk_config_read(union ctl_io *io);
129 
130 static struct ctl_backend_driver ctl_be_ramdisk_driver =
131 {
132 	.name = "ramdisk",
133 	.flags = CTL_BE_FLAG_HAS_CONFIG,
134 	.init = ctl_backend_ramdisk_init,
135 	.data_submit = ctl_backend_ramdisk_submit,
136 	.data_move_done = ctl_backend_ramdisk_move_done,
137 	.config_read = ctl_backend_ramdisk_config_read,
138 	.config_write = ctl_backend_ramdisk_config_write,
139 	.ioctl = ctl_backend_ramdisk_ioctl
140 };
141 
142 MALLOC_DEFINE(M_RAMDISK, "ramdisk", "Memory used for CTL RAMdisk");
143 CTL_BACKEND_DECLARE(cbr, ctl_be_ramdisk_driver);
144 
145 int
146 ctl_backend_ramdisk_init(void)
147 {
148 	struct ctl_be_ramdisk_softc *softc;
149 #ifdef CTL_RAMDISK_PAGES
150 	int i;
151 #endif
152 
153 
154 	softc = &rd_softc;
155 
156 	memset(softc, 0, sizeof(*softc));
157 
158 	mtx_init(&softc->lock, "ctlramdisk", NULL, MTX_DEF);
159 
160 	STAILQ_INIT(&softc->lun_list);
161 	softc->rd_size = 1024 * 1024;
162 #ifdef CTL_RAMDISK_PAGES
163 	softc->num_pages = softc->rd_size / PAGE_SIZE;
164 	softc->ramdisk_pages = (uint8_t **)malloc(sizeof(uint8_t *) *
165 						  softc->num_pages, M_RAMDISK,
166 						  M_WAITOK);
167 	for (i = 0; i < softc->num_pages; i++)
168 		softc->ramdisk_pages[i] = malloc(PAGE_SIZE, M_RAMDISK,M_WAITOK);
169 #else
170 	softc->ramdisk_buffer = (uint8_t *)malloc(softc->rd_size, M_RAMDISK,
171 						  M_WAITOK);
172 #endif
173 
174 	return (0);
175 }
176 
177 void
178 ctl_backend_ramdisk_shutdown(void)
179 {
180 	struct ctl_be_ramdisk_softc *softc;
181 	struct ctl_be_ramdisk_lun *lun, *next_lun;
182 #ifdef CTL_RAMDISK_PAGES
183 	int i;
184 #endif
185 
186 	softc = &rd_softc;
187 
188 	mtx_lock(&softc->lock);
189 	for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
190 		/*
191 		 * Grab the next LUN.  The current LUN may get removed by
192 		 * ctl_invalidate_lun(), which will call our LUN shutdown
193 		 * routine, if there is no outstanding I/O for this LUN.
194 		 */
195 		next_lun = STAILQ_NEXT(lun, links);
196 
197 		/*
198 		 * Drop our lock here.  Since ctl_invalidate_lun() can call
199 		 * back into us, this could potentially lead to a recursive
200 		 * lock of the same mutex, which would cause a hang.
201 		 */
202 		mtx_unlock(&softc->lock);
203 		ctl_disable_lun(&lun->cbe_lun);
204 		ctl_invalidate_lun(&lun->cbe_lun);
205 		mtx_lock(&softc->lock);
206 	}
207 	mtx_unlock(&softc->lock);
208 
209 #ifdef CTL_RAMDISK_PAGES
210 	for (i = 0; i < softc->num_pages; i++)
211 		free(softc->ramdisk_pages[i], M_RAMDISK);
212 
213 	free(softc->ramdisk_pages, M_RAMDISK);
214 #else
215 	free(softc->ramdisk_buffer, M_RAMDISK);
216 #endif
217 
218 	if (ctl_backend_deregister(&ctl_be_ramdisk_driver) != 0) {
219 		printf("ctl_backend_ramdisk_shutdown: "
220 		       "ctl_backend_deregister() failed!\n");
221 	}
222 }
223 
224 static int
225 ctl_backend_ramdisk_move_done(union ctl_io *io)
226 {
227 	struct ctl_be_lun *cbe_lun;
228 	struct ctl_be_ramdisk_lun *be_lun;
229 #ifdef CTL_TIME_IO
230 	struct bintime cur_bt;
231 #endif
232 
233 	CTL_DEBUG_PRINT(("ctl_backend_ramdisk_move_done\n"));
234 	cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
235 		CTL_PRIV_BACKEND_LUN].ptr;
236 	be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun->be_lun;
237 #ifdef CTL_TIME_IO
238 	getbintime(&cur_bt);
239 	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
240 	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
241 	io->io_hdr.num_dmas++;
242 #endif
243 	if (io->scsiio.kern_sg_entries > 0)
244 		free(io->scsiio.kern_data_ptr, M_RAMDISK);
245 	io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
246 	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
247 		;
248 	} else if ((io->io_hdr.port_status == 0) &&
249 	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
250 		if (io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer > 0) {
251 			mtx_lock(&be_lun->queue_lock);
252 			STAILQ_INSERT_TAIL(&be_lun->cont_queue,
253 			    &io->io_hdr, links);
254 			mtx_unlock(&be_lun->queue_lock);
255 			taskqueue_enqueue(be_lun->io_taskqueue,
256 			    &be_lun->io_task);
257 			return (0);
258 		}
259 		ctl_set_success(&io->scsiio);
260 	} else if ((io->io_hdr.port_status != 0) &&
261 	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
262 	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
263 		/*
264 		 * For hardware error sense keys, the sense key
265 		 * specific value is defined to be a retry count,
266 		 * but we use it to pass back an internal FETD
267 		 * error code.  XXX KDM  Hopefully the FETD is only
268 		 * using 16 bits for an error code, since that's
269 		 * all the space we have in the sks field.
270 		 */
271 		ctl_set_internal_failure(&io->scsiio,
272 					 /*sks_valid*/ 1,
273 					 /*retry_count*/
274 					 io->io_hdr.port_status);
275 	}
276 	ctl_data_submit_done(io);
277 	return(0);
278 }
279 
280 static int
281 ctl_backend_ramdisk_submit(union ctl_io *io)
282 {
283 	struct ctl_be_lun *cbe_lun;
284 	struct ctl_lba_len_flags *lbalen;
285 
286 	cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
287 		CTL_PRIV_BACKEND_LUN].ptr;
288 	lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
289 	if (lbalen->flags & CTL_LLF_VERIFY) {
290 		ctl_set_success(&io->scsiio);
291 		ctl_data_submit_done(io);
292 		return (CTL_RETVAL_COMPLETE);
293 	}
294 	io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer =
295 	    lbalen->len * cbe_lun->blocksize;
296 	ctl_backend_ramdisk_continue(io);
297 	return (CTL_RETVAL_COMPLETE);
298 }
299 
300 static void
301 ctl_backend_ramdisk_continue(union ctl_io *io)
302 {
303 	struct ctl_be_ramdisk_softc *softc;
304 	int len, len_filled, sg_filled;
305 #ifdef CTL_RAMDISK_PAGES
306 	struct ctl_sg_entry *sg_entries;
307 	int i;
308 #endif
309 
310 	softc = &rd_softc;
311 	len = io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer;
312 #ifdef CTL_RAMDISK_PAGES
313 	sg_filled = min(btoc(len), softc->num_pages);
314 	if (sg_filled > 1) {
315 		io->scsiio.kern_data_ptr = malloc(sizeof(struct ctl_sg_entry) *
316 						  sg_filled, M_RAMDISK,
317 						  M_WAITOK);
318 		sg_entries = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
319 		for (i = 0, len_filled = 0; i < sg_filled; i++) {
320 			sg_entries[i].addr = softc->ramdisk_pages[i];
321 			sg_entries[i].len = MIN(PAGE_SIZE, len - len_filled);
322 			len_filled += sg_entries[i].len;
323 		}
324 	} else {
325 		sg_filled = 0;
326 		len_filled = len;
327 		io->scsiio.kern_data_ptr = softc->ramdisk_pages[0];
328 	}
329 #else
330 	sg_filled = 0;
331 	len_filled = min(len, softc->rd_size);
332 	io->scsiio.kern_data_ptr = softc->ramdisk_buffer;
333 #endif /* CTL_RAMDISK_PAGES */
334 
335 	io->scsiio.be_move_done = ctl_backend_ramdisk_move_done;
336 	io->scsiio.kern_data_resid = 0;
337 	io->scsiio.kern_data_len = len_filled;
338 	io->scsiio.kern_sg_entries = sg_filled;
339 	io->io_hdr.flags |= CTL_FLAG_ALLOCATED;
340 	io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer -= len_filled;
341 #ifdef CTL_TIME_IO
342 	getbintime(&io->io_hdr.dma_start_bt);
343 #endif
344 	ctl_datamove(io);
345 }
346 
347 static void
348 ctl_backend_ramdisk_worker(void *context, int pending)
349 {
350 	struct ctl_be_ramdisk_softc *softc;
351 	struct ctl_be_ramdisk_lun *be_lun;
352 	union ctl_io *io;
353 
354 	be_lun = (struct ctl_be_ramdisk_lun *)context;
355 	softc = be_lun->softc;
356 
357 	mtx_lock(&be_lun->queue_lock);
358 	for (;;) {
359 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->cont_queue);
360 		if (io != NULL) {
361 			STAILQ_REMOVE(&be_lun->cont_queue, &io->io_hdr,
362 				      ctl_io_hdr, links);
363 
364 			mtx_unlock(&be_lun->queue_lock);
365 
366 			ctl_backend_ramdisk_continue(io);
367 
368 			mtx_lock(&be_lun->queue_lock);
369 			continue;
370 		}
371 
372 		/*
373 		 * If we get here, there is no work left in the queues, so
374 		 * just break out and let the task queue go to sleep.
375 		 */
376 		break;
377 	}
378 	mtx_unlock(&be_lun->queue_lock);
379 }
380 
381 static int
382 ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
383 			  int flag, struct thread *td)
384 {
385 	struct ctl_be_ramdisk_softc *softc;
386 	int retval;
387 
388 	retval = 0;
389 	softc = &rd_softc;
390 
391 	switch (cmd) {
392 	case CTL_LUN_REQ: {
393 		struct ctl_lun_req *lun_req;
394 
395 		lun_req = (struct ctl_lun_req *)addr;
396 
397 		switch (lun_req->reqtype) {
398 		case CTL_LUNREQ_CREATE:
399 			retval = ctl_backend_ramdisk_create(softc, lun_req);
400 			break;
401 		case CTL_LUNREQ_RM:
402 			retval = ctl_backend_ramdisk_rm(softc, lun_req);
403 			break;
404 		case CTL_LUNREQ_MODIFY:
405 			retval = ctl_backend_ramdisk_modify(softc, lun_req);
406 			break;
407 		default:
408 			lun_req->status = CTL_LUN_ERROR;
409 			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
410 				 "%s: invalid LUN request type %d", __func__,
411 				 lun_req->reqtype);
412 			break;
413 		}
414 		break;
415 	}
416 	default:
417 		retval = ENOTTY;
418 		break;
419 	}
420 
421 	return (retval);
422 }
423 
424 static int
425 ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
426 		       struct ctl_lun_req *req)
427 {
428 	struct ctl_be_ramdisk_lun *be_lun;
429 	struct ctl_lun_rm_params *params;
430 	int retval;
431 
432 
433 	retval = 0;
434 	params = &req->reqdata.rm;
435 
436 	be_lun = NULL;
437 
438 	mtx_lock(&softc->lock);
439 
440 	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
441 		if (be_lun->cbe_lun.lun_id == params->lun_id)
442 			break;
443 	}
444 	mtx_unlock(&softc->lock);
445 
446 	if (be_lun == NULL) {
447 		snprintf(req->error_str, sizeof(req->error_str),
448 			 "%s: LUN %u is not managed by the ramdisk backend",
449 			 __func__, params->lun_id);
450 		goto bailout_error;
451 	}
452 
453 	retval = ctl_disable_lun(&be_lun->cbe_lun);
454 
455 	if (retval != 0) {
456 		snprintf(req->error_str, sizeof(req->error_str),
457 			 "%s: error %d returned from ctl_disable_lun() for "
458 			 "LUN %d", __func__, retval, params->lun_id);
459 		goto bailout_error;
460 	}
461 
462 	/*
463 	 * Set the waiting flag before we invalidate the LUN.  Our shutdown
464 	 * routine can be called any time after we invalidate the LUN,
465 	 * and can be called from our context.
466 	 *
467 	 * This tells the shutdown routine that we're waiting, or we're
468 	 * going to wait for the shutdown to happen.
469 	 */
470 	mtx_lock(&softc->lock);
471 	be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
472 	mtx_unlock(&softc->lock);
473 
474 	retval = ctl_invalidate_lun(&be_lun->cbe_lun);
475 	if (retval != 0) {
476 		snprintf(req->error_str, sizeof(req->error_str),
477 			 "%s: error %d returned from ctl_invalidate_lun() for "
478 			 "LUN %d", __func__, retval, params->lun_id);
479 		mtx_lock(&softc->lock);
480 		be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
481 		mtx_unlock(&softc->lock);
482 		goto bailout_error;
483 	}
484 
485 	mtx_lock(&softc->lock);
486 
487 	while ((be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) == 0) {
488 		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
489  		if (retval == EINTR)
490 			break;
491 	}
492 	be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
493 
494 	/*
495 	 * We only remove this LUN from the list and free it (below) if
496 	 * retval == 0.  If the user interrupted the wait, we just bail out
497 	 * without actually freeing the LUN.  We let the shutdown routine
498 	 * free the LUN if that happens.
499 	 */
500 	if (retval == 0) {
501 		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
502 			      links);
503 		softc->num_luns--;
504 	}
505 
506 	mtx_unlock(&softc->lock);
507 
508 	if (retval == 0) {
509 		taskqueue_drain_all(be_lun->io_taskqueue);
510 		taskqueue_free(be_lun->io_taskqueue);
511 		ctl_free_opts(&be_lun->cbe_lun.options);
512 		mtx_destroy(&be_lun->queue_lock);
513 		free(be_lun, M_RAMDISK);
514 	}
515 
516 	req->status = CTL_LUN_OK;
517 
518 	return (retval);
519 
520 bailout_error:
521 	req->status = CTL_LUN_ERROR;
522 
523 	return (0);
524 }
525 
526 static int
527 ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
528 			   struct ctl_lun_req *req)
529 {
530 	struct ctl_be_ramdisk_lun *be_lun;
531 	struct ctl_be_lun *cbe_lun;
532 	struct ctl_lun_create_params *params;
533 	char *value;
534 	char tmpstr[32];
535 	int retval;
536 
537 	retval = 0;
538 	params = &req->reqdata.create;
539 
540 	be_lun = malloc(sizeof(*be_lun), M_RAMDISK, M_ZERO | M_WAITOK);
541 	cbe_lun = &be_lun->cbe_lun;
542 	cbe_lun->be_lun = be_lun;
543 	be_lun->params = req->reqdata.create;
544 	be_lun->softc = softc;
545 	sprintf(be_lun->lunname, "cram%d", softc->num_luns);
546 	ctl_init_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args);
547 
548 	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
549 		cbe_lun->lun_type = params->device_type;
550 	else
551 		cbe_lun->lun_type = T_DIRECT;
552 	be_lun->flags = CTL_BE_RAMDISK_LUN_UNCONFIGURED;
553 	cbe_lun->flags = 0;
554 	value = ctl_get_opt(&cbe_lun->options, "ha_role");
555 	if (value != NULL) {
556 		if (strcmp(value, "primary") == 0)
557 			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
558 	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
559 		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
560 
561 	if (cbe_lun->lun_type == T_DIRECT) {
562 		if (params->blocksize_bytes != 0)
563 			cbe_lun->blocksize = params->blocksize_bytes;
564 		else
565 			cbe_lun->blocksize = 512;
566 		if (params->lun_size_bytes < cbe_lun->blocksize) {
567 			snprintf(req->error_str, sizeof(req->error_str),
568 				 "%s: LUN size %ju < blocksize %u", __func__,
569 				 params->lun_size_bytes, cbe_lun->blocksize);
570 			goto bailout_error;
571 		}
572 		be_lun->size_blocks = params->lun_size_bytes / cbe_lun->blocksize;
573 		be_lun->size_bytes = be_lun->size_blocks * cbe_lun->blocksize;
574 		cbe_lun->maxlba = be_lun->size_blocks - 1;
575 		cbe_lun->atomicblock = UINT32_MAX;
576 		cbe_lun->opttxferlen = softc->rd_size / cbe_lun->blocksize;
577 	}
578 
579 	/* Tell the user the blocksize we ended up using */
580 	params->blocksize_bytes = cbe_lun->blocksize;
581 	params->lun_size_bytes = be_lun->size_bytes;
582 
583 	value = ctl_get_opt(&cbe_lun->options, "unmap");
584 	if (value != NULL && strcmp(value, "on") == 0)
585 		cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
586 	value = ctl_get_opt(&cbe_lun->options, "readonly");
587 	if (value != NULL && strcmp(value, "on") == 0)
588 		cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
589 	cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
590 	value = ctl_get_opt(&cbe_lun->options, "serseq");
591 	if (value != NULL && strcmp(value, "on") == 0)
592 		cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
593 	else if (value != NULL && strcmp(value, "read") == 0)
594 		cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
595 	else if (value != NULL && strcmp(value, "off") == 0)
596 		cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
597 
598 	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
599 		cbe_lun->req_lun_id = params->req_lun_id;
600 		cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
601 	} else
602 		cbe_lun->req_lun_id = 0;
603 
604 	cbe_lun->lun_shutdown = ctl_backend_ramdisk_lun_shutdown;
605 	cbe_lun->lun_config_status = ctl_backend_ramdisk_lun_config_status;
606 	cbe_lun->be = &ctl_be_ramdisk_driver;
607 	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
608 		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
609 			 softc->num_luns);
610 		strncpy((char *)cbe_lun->serial_num, tmpstr,
611 			MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
612 
613 		/* Tell the user what we used for a serial number */
614 		strncpy((char *)params->serial_num, tmpstr,
615 			MIN(sizeof(params->serial_num), sizeof(tmpstr)));
616 	} else {
617 		strncpy((char *)cbe_lun->serial_num, params->serial_num,
618 			MIN(sizeof(cbe_lun->serial_num),
619 			    sizeof(params->serial_num)));
620 	}
621 	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
622 		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
623 		strncpy((char *)cbe_lun->device_id, tmpstr,
624 			MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
625 
626 		/* Tell the user what we used for a device ID */
627 		strncpy((char *)params->device_id, tmpstr,
628 			MIN(sizeof(params->device_id), sizeof(tmpstr)));
629 	} else {
630 		strncpy((char *)cbe_lun->device_id, params->device_id,
631 			MIN(sizeof(cbe_lun->device_id),
632 			    sizeof(params->device_id)));
633 	}
634 
635 	STAILQ_INIT(&be_lun->cont_queue);
636 	mtx_init(&be_lun->queue_lock, "cram queue lock", NULL, MTX_DEF);
637 	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_backend_ramdisk_worker,
638 	    be_lun);
639 
640 	be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
641 	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
642 	if (be_lun->io_taskqueue == NULL) {
643 		snprintf(req->error_str, sizeof(req->error_str),
644 			 "%s: Unable to create taskqueue", __func__);
645 		goto bailout_error;
646 	}
647 
648 	retval = taskqueue_start_threads(&be_lun->io_taskqueue,
649 					 /*num threads*/1,
650 					 /*priority*/PWAIT,
651 					 /*thread name*/
652 					 "%s taskq", be_lun->lunname);
653 	if (retval != 0)
654 		goto bailout_error;
655 
656 	mtx_lock(&softc->lock);
657 	softc->num_luns++;
658 	STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
659 
660 	mtx_unlock(&softc->lock);
661 
662 	retval = ctl_add_lun(&be_lun->cbe_lun);
663 	if (retval != 0) {
664 		mtx_lock(&softc->lock);
665 		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
666 			      links);
667 		softc->num_luns--;
668 		mtx_unlock(&softc->lock);
669 		snprintf(req->error_str, sizeof(req->error_str),
670 			 "%s: ctl_add_lun() returned error %d, see dmesg for "
671 			"details", __func__, retval);
672 		retval = 0;
673 		goto bailout_error;
674 	}
675 
676 	mtx_lock(&softc->lock);
677 
678 	/*
679 	 * Tell the config_status routine that we're waiting so it won't
680 	 * clean up the LUN in the event of an error.
681 	 */
682 	be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
683 
684 	while (be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) {
685 		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
686 		if (retval == EINTR)
687 			break;
688 	}
689 	be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
690 
691 	if (be_lun->flags & CTL_BE_RAMDISK_LUN_CONFIG_ERR) {
692 		snprintf(req->error_str, sizeof(req->error_str),
693 			 "%s: LUN configuration error, see dmesg for details",
694 			 __func__);
695 		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
696 			      links);
697 		softc->num_luns--;
698 		mtx_unlock(&softc->lock);
699 		goto bailout_error;
700 	} else {
701 		params->req_lun_id = cbe_lun->lun_id;
702 	}
703 	mtx_unlock(&softc->lock);
704 
705 	req->status = CTL_LUN_OK;
706 
707 	return (retval);
708 
709 bailout_error:
710 	req->status = CTL_LUN_ERROR;
711 	if (be_lun != NULL) {
712 		if (be_lun->io_taskqueue != NULL) {
713 			taskqueue_free(be_lun->io_taskqueue);
714 		}
715 		ctl_free_opts(&cbe_lun->options);
716 		mtx_destroy(&be_lun->queue_lock);
717 		free(be_lun, M_RAMDISK);
718 	}
719 
720 	return (retval);
721 }
722 
723 static int
724 ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
725 		       struct ctl_lun_req *req)
726 {
727 	struct ctl_be_ramdisk_lun *be_lun;
728 	struct ctl_be_lun *cbe_lun;
729 	struct ctl_lun_modify_params *params;
730 	char *value;
731 	uint32_t blocksize;
732 	int wasprim;
733 
734 	params = &req->reqdata.modify;
735 
736 	mtx_lock(&softc->lock);
737 	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
738 		if (be_lun->cbe_lun.lun_id == params->lun_id)
739 			break;
740 	}
741 	mtx_unlock(&softc->lock);
742 
743 	if (be_lun == NULL) {
744 		snprintf(req->error_str, sizeof(req->error_str),
745 			 "%s: LUN %u is not managed by the ramdisk backend",
746 			 __func__, params->lun_id);
747 		goto bailout_error;
748 	}
749 	cbe_lun = &be_lun->cbe_lun;
750 
751 	if (params->lun_size_bytes != 0)
752 		be_lun->params.lun_size_bytes = params->lun_size_bytes;
753 	ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args);
754 
755 	wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
756 	value = ctl_get_opt(&cbe_lun->options, "ha_role");
757 	if (value != NULL) {
758 		if (strcmp(value, "primary") == 0)
759 			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
760 		else
761 			cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
762 	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
763 		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
764 	else
765 		cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
766 	if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
767 		if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
768 			ctl_lun_primary(cbe_lun);
769 		else
770 			ctl_lun_secondary(cbe_lun);
771 	}
772 
773 	blocksize = be_lun->cbe_lun.blocksize;
774 	if (be_lun->params.lun_size_bytes < blocksize) {
775 		snprintf(req->error_str, sizeof(req->error_str),
776 			"%s: LUN size %ju < blocksize %u", __func__,
777 			be_lun->params.lun_size_bytes, blocksize);
778 		goto bailout_error;
779 	}
780 	be_lun->size_blocks = be_lun->params.lun_size_bytes / blocksize;
781 	be_lun->size_bytes = be_lun->size_blocks * blocksize;
782 	be_lun->cbe_lun.maxlba = be_lun->size_blocks - 1;
783 	ctl_lun_capacity_changed(&be_lun->cbe_lun);
784 
785 	/* Tell the user the exact size we ended up using */
786 	params->lun_size_bytes = be_lun->size_bytes;
787 
788 	req->status = CTL_LUN_OK;
789 
790 	return (0);
791 
792 bailout_error:
793 	req->status = CTL_LUN_ERROR;
794 
795 	return (0);
796 }
797 
798 static void
799 ctl_backend_ramdisk_lun_shutdown(void *be_lun)
800 {
801 	struct ctl_be_ramdisk_lun *lun;
802 	struct ctl_be_ramdisk_softc *softc;
803 	int do_free;
804 
805 	lun = (struct ctl_be_ramdisk_lun *)be_lun;
806 	softc = lun->softc;
807 	do_free = 0;
808 
809 	mtx_lock(&softc->lock);
810 
811 	lun->flags |= CTL_BE_RAMDISK_LUN_UNCONFIGURED;
812 
813 	if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
814 		wakeup(lun);
815 	} else {
816 		STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
817 			      links);
818 		softc->num_luns--;
819 		do_free = 1;
820 	}
821 
822 	mtx_unlock(&softc->lock);
823 
824 	if (do_free != 0)
825 		free(be_lun, M_RAMDISK);
826 }
827 
828 static void
829 ctl_backend_ramdisk_lun_config_status(void *be_lun,
830 				      ctl_lun_config_status status)
831 {
832 	struct ctl_be_ramdisk_lun *lun;
833 	struct ctl_be_ramdisk_softc *softc;
834 
835 	lun = (struct ctl_be_ramdisk_lun *)be_lun;
836 	softc = lun->softc;
837 
838 	if (status == CTL_LUN_CONFIG_OK) {
839 		mtx_lock(&softc->lock);
840 		lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
841 		if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING)
842 			wakeup(lun);
843 		mtx_unlock(&softc->lock);
844 
845 		/*
846 		 * We successfully added the LUN, attempt to enable it.
847 		 */
848 		if (ctl_enable_lun(&lun->cbe_lun) != 0) {
849 			printf("%s: ctl_enable_lun() failed!\n", __func__);
850 			if (ctl_invalidate_lun(&lun->cbe_lun) != 0) {
851 				printf("%s: ctl_invalidate_lun() failed!\n",
852 				       __func__);
853 			}
854 		}
855 
856 		return;
857 	}
858 
859 
860 	mtx_lock(&softc->lock);
861 	lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
862 
863 	/*
864 	 * If we have a user waiting, let him handle the cleanup.  If not,
865 	 * clean things up here.
866 	 */
867 	if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
868 		lun->flags |= CTL_BE_RAMDISK_LUN_CONFIG_ERR;
869 		wakeup(lun);
870 	} else {
871 		STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
872 			      links);
873 		softc->num_luns--;
874 		free(lun, M_RAMDISK);
875 	}
876 	mtx_unlock(&softc->lock);
877 }
878 
879 static int
880 ctl_backend_ramdisk_config_write(union ctl_io *io)
881 {
882 	struct ctl_be_ramdisk_softc *softc;
883 	int retval;
884 
885 	retval = 0;
886 	softc = &rd_softc;
887 
888 	switch (io->scsiio.cdb[0]) {
889 	case SYNCHRONIZE_CACHE:
890 	case SYNCHRONIZE_CACHE_16:
891 		/*
892 		 * The upper level CTL code will filter out any CDBs with
893 		 * the immediate bit set and return the proper error.  It
894 		 * will also not allow a sync cache command to go to a LUN
895 		 * that is powered down.
896 		 *
897 		 * We don't really need to worry about what LBA range the
898 		 * user asked to be synced out.  When they issue a sync
899 		 * cache command, we'll sync out the whole thing.
900 		 *
901 		 * This is obviously just a stubbed out implementation.
902 		 * The real implementation will be in the RAIDCore/CTL
903 		 * interface, and can only really happen when RAIDCore
904 		 * implements a per-array cache sync.
905 		 */
906 		ctl_set_success(&io->scsiio);
907 		ctl_config_write_done(io);
908 		break;
909 	case START_STOP_UNIT: {
910 		struct scsi_start_stop_unit *cdb;
911 		struct ctl_be_lun *cbe_lun;
912 		struct ctl_be_ramdisk_lun *be_lun;
913 
914 		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
915 
916 		cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
917 			CTL_PRIV_BACKEND_LUN].ptr;
918 		be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun->be_lun;
919 
920 		if (cdb->how & SSS_START)
921 			retval = ctl_start_lun(cbe_lun);
922 		else {
923 			retval = ctl_stop_lun(cbe_lun);
924 #ifdef NEEDTOPORT
925 			if ((retval == 0)
926 			 && (cdb->byte2 & SSS_ONOFFLINE))
927 				retval = ctl_lun_offline(cbe_lun);
928 #endif
929 		}
930 
931 		/*
932 		 * In general, the above routines should not fail.  They
933 		 * just set state for the LUN.  So we've got something
934 		 * pretty wrong here if we can't start or stop the LUN.
935 		 */
936 		if (retval != 0) {
937 			ctl_set_internal_failure(&io->scsiio,
938 						 /*sks_valid*/ 1,
939 						 /*retry_count*/ 0xf051);
940 			retval = CTL_RETVAL_COMPLETE;
941 		} else {
942 			ctl_set_success(&io->scsiio);
943 		}
944 		ctl_config_write_done(io);
945 		break;
946 	}
947 	case WRITE_SAME_10:
948 	case WRITE_SAME_16:
949 	case UNMAP:
950 		ctl_set_success(&io->scsiio);
951 		ctl_config_write_done(io);
952 		break;
953 	default:
954 		ctl_set_invalid_opcode(&io->scsiio);
955 		ctl_config_write_done(io);
956 		retval = CTL_RETVAL_COMPLETE;
957 		break;
958 	}
959 
960 	return (retval);
961 }
962 
963 static int
964 ctl_backend_ramdisk_config_read(union ctl_io *io)
965 {
966 	int retval = 0;
967 
968 	switch (io->scsiio.cdb[0]) {
969 	case SERVICE_ACTION_IN:
970 		if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
971 			/* We have nothing to tell, leave default data. */
972 			ctl_config_read_done(io);
973 			retval = CTL_RETVAL_COMPLETE;
974 			break;
975 		}
976 		ctl_set_invalid_field(&io->scsiio,
977 				      /*sks_valid*/ 1,
978 				      /*command*/ 1,
979 				      /*field*/ 1,
980 				      /*bit_valid*/ 1,
981 				      /*bit*/ 4);
982 		ctl_config_read_done(io);
983 		retval = CTL_RETVAL_COMPLETE;
984 		break;
985 	default:
986 		ctl_set_invalid_opcode(&io->scsiio);
987 		ctl_config_read_done(io);
988 		retval = CTL_RETVAL_COMPLETE;
989 		break;
990 	}
991 
992 	return (retval);
993 }
994