2 * Copyright (c) 2003, 2008 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org>
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
35 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_ramdisk.c#3 $
38 * CAM Target Layer backend for a "fake" ramdisk.
40 * Author: Ken Merry <ken@FreeBSD.org>
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/condvar.h>
50 #include <sys/types.h>
52 #include <sys/mutex.h>
53 #include <sys/malloc.h>
54 #include <sys/taskqueue.h>
56 #include <sys/queue.h>
58 #include <sys/ioccom.h>
59 #include <sys/module.h>
60 #include <sys/sysctl.h>
62 #include <cam/scsi/scsi_all.h>
63 #include <cam/scsi/scsi_da.h>
64 #include <cam/ctl/ctl_io.h>
65 #include <cam/ctl/ctl.h>
66 #include <cam/ctl/ctl_util.h>
67 #include <cam/ctl/ctl_backend.h>
68 #include <cam/ctl/ctl_debug.h>
69 #include <cam/ctl/ctl_ioctl.h>
70 #include <cam/ctl/ctl_ha.h>
71 #include <cam/ctl/ctl_private.h>
72 #include <cam/ctl/ctl_error.h>
75 CTL_BE_RAMDISK_LUN_UNCONFIGURED = 0x01,
76 CTL_BE_RAMDISK_LUN_CONFIG_ERR = 0x02,
77 CTL_BE_RAMDISK_LUN_WAITING = 0x04
78 } ctl_be_ramdisk_lun_flags;
80 struct ctl_be_ramdisk_lun {
81 struct ctl_lun_create_params params;
85 struct ctl_be_ramdisk_softc *softc;
86 ctl_be_ramdisk_lun_flags flags;
87 STAILQ_ENTRY(ctl_be_ramdisk_lun) links;
88 struct ctl_be_lun cbe_lun;
89 struct taskqueue *io_taskqueue;
91 STAILQ_HEAD(, ctl_io_hdr) cont_queue;
92 struct mtx_padalign queue_lock;
95 struct ctl_be_ramdisk_softc {
98 #ifdef CTL_RAMDISK_PAGES
99 uint8_t **ramdisk_pages;
102 uint8_t *ramdisk_buffer;
105 STAILQ_HEAD(, ctl_be_ramdisk_lun) lun_list;
108 static struct ctl_be_ramdisk_softc rd_softc;
109 extern struct ctl_softc *control_softc;
111 int ctl_backend_ramdisk_init(void);
112 void ctl_backend_ramdisk_shutdown(void);
113 static int ctl_backend_ramdisk_move_done(union ctl_io *io);
114 static int ctl_backend_ramdisk_submit(union ctl_io *io);
115 static void ctl_backend_ramdisk_continue(union ctl_io *io);
116 static int ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd,
117 caddr_t addr, int flag, struct thread *td);
118 static int ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
119 struct ctl_lun_req *req);
120 static int ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
121 struct ctl_lun_req *req);
122 static int ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
123 struct ctl_lun_req *req);
124 static void ctl_backend_ramdisk_worker(void *context, int pending);
125 static void ctl_backend_ramdisk_lun_shutdown(void *be_lun);
126 static void ctl_backend_ramdisk_lun_config_status(void *be_lun,
127 ctl_lun_config_status status);
128 static int ctl_backend_ramdisk_config_write(union ctl_io *io);
129 static int ctl_backend_ramdisk_config_read(union ctl_io *io);
131 static struct ctl_backend_driver ctl_be_ramdisk_driver =
134 .flags = CTL_BE_FLAG_HAS_CONFIG,
135 .init = ctl_backend_ramdisk_init,
136 .data_submit = ctl_backend_ramdisk_submit,
137 .data_move_done = ctl_backend_ramdisk_move_done,
138 .config_read = ctl_backend_ramdisk_config_read,
139 .config_write = ctl_backend_ramdisk_config_write,
140 .ioctl = ctl_backend_ramdisk_ioctl
143 MALLOC_DEFINE(M_RAMDISK, "ramdisk", "Memory used for CTL RAMdisk");
144 CTL_BACKEND_DECLARE(cbr, ctl_be_ramdisk_driver);
147 ctl_backend_ramdisk_init(void)
149 struct ctl_be_ramdisk_softc *softc = &rd_softc;
150 #ifdef CTL_RAMDISK_PAGES
154 memset(softc, 0, sizeof(*softc));
155 mtx_init(&softc->lock, "ctlramdisk", NULL, MTX_DEF);
156 STAILQ_INIT(&softc->lun_list);
157 softc->rd_size = 1024 * 1024;
158 #ifdef CTL_RAMDISK_PAGES
159 softc->num_pages = softc->rd_size / PAGE_SIZE;
160 softc->ramdisk_pages = (uint8_t **)malloc(sizeof(uint8_t *) *
161 softc->num_pages, M_RAMDISK,
163 for (i = 0; i < softc->num_pages; i++)
164 softc->ramdisk_pages[i] = malloc(PAGE_SIZE, M_RAMDISK,M_WAITOK);
166 softc->ramdisk_buffer = (uint8_t *)malloc(softc->rd_size, M_RAMDISK,
174 ctl_backend_ramdisk_shutdown(void)
176 struct ctl_be_ramdisk_softc *softc = &rd_softc;
177 struct ctl_be_ramdisk_lun *lun, *next_lun;
178 #ifdef CTL_RAMDISK_PAGES
182 mtx_lock(&softc->lock);
183 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
185 * Grab the next LUN. The current LUN may get removed by
186 * ctl_invalidate_lun(), which will call our LUN shutdown
187 * routine, if there is no outstanding I/O for this LUN.
189 next_lun = STAILQ_NEXT(lun, links);
192 * Drop our lock here. Since ctl_invalidate_lun() can call
193 * back into us, this could potentially lead to a recursive
194 * lock of the same mutex, which would cause a hang.
196 mtx_unlock(&softc->lock);
197 ctl_disable_lun(&lun->cbe_lun);
198 ctl_invalidate_lun(&lun->cbe_lun);
199 mtx_lock(&softc->lock);
201 mtx_unlock(&softc->lock);
203 #ifdef CTL_RAMDISK_PAGES
204 for (i = 0; i < softc->num_pages; i++)
205 free(softc->ramdisk_pages[i], M_RAMDISK);
207 free(softc->ramdisk_pages, M_RAMDISK);
209 free(softc->ramdisk_buffer, M_RAMDISK);
212 if (ctl_backend_deregister(&ctl_be_ramdisk_driver) != 0) {
213 printf("ctl_backend_ramdisk_shutdown: "
214 "ctl_backend_deregister() failed!\n");
219 ctl_backend_ramdisk_move_done(union ctl_io *io)
221 struct ctl_be_lun *cbe_lun;
222 struct ctl_be_ramdisk_lun *be_lun;
224 struct bintime cur_bt;
227 CTL_DEBUG_PRINT(("ctl_backend_ramdisk_move_done\n"));
228 cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
229 CTL_PRIV_BACKEND_LUN].ptr;
230 be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun->be_lun;
232 getbinuptime(&cur_bt);
233 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
234 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
236 io->io_hdr.num_dmas++;
237 if (io->scsiio.kern_sg_entries > 0)
238 free(io->scsiio.kern_data_ptr, M_RAMDISK);
239 io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
240 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
242 } else if ((io->io_hdr.port_status == 0) &&
243 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
244 if (io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer > 0) {
245 mtx_lock(&be_lun->queue_lock);
246 STAILQ_INSERT_TAIL(&be_lun->cont_queue,
248 mtx_unlock(&be_lun->queue_lock);
249 taskqueue_enqueue(be_lun->io_taskqueue,
253 ctl_set_success(&io->scsiio);
254 } else if ((io->io_hdr.port_status != 0) &&
255 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
256 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
258 * For hardware error sense keys, the sense key
259 * specific value is defined to be a retry count,
260 * but we use it to pass back an internal FETD
261 * error code. XXX KDM Hopefully the FETD is only
262 * using 16 bits for an error code, since that's
263 * all the space we have in the sks field.
265 ctl_set_internal_failure(&io->scsiio,
268 io->io_hdr.port_status);
270 ctl_data_submit_done(io);
275 ctl_backend_ramdisk_submit(union ctl_io *io)
277 struct ctl_be_lun *cbe_lun;
278 struct ctl_lba_len_flags *lbalen;
280 cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
281 CTL_PRIV_BACKEND_LUN].ptr;
282 lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
283 if (lbalen->flags & CTL_LLF_VERIFY) {
284 ctl_set_success(&io->scsiio);
285 ctl_data_submit_done(io);
286 return (CTL_RETVAL_COMPLETE);
288 io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer =
289 lbalen->len * cbe_lun->blocksize;
290 ctl_backend_ramdisk_continue(io);
291 return (CTL_RETVAL_COMPLETE);
295 ctl_backend_ramdisk_continue(union ctl_io *io)
297 struct ctl_be_ramdisk_softc *softc;
298 int len, len_filled, sg_filled;
299 #ifdef CTL_RAMDISK_PAGES
300 struct ctl_sg_entry *sg_entries;
305 len = io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer;
306 #ifdef CTL_RAMDISK_PAGES
307 sg_filled = min(btoc(len), softc->num_pages);
309 io->scsiio.kern_data_ptr = malloc(sizeof(struct ctl_sg_entry) *
310 sg_filled, M_RAMDISK,
312 sg_entries = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
313 for (i = 0, len_filled = 0; i < sg_filled; i++) {
314 sg_entries[i].addr = softc->ramdisk_pages[i];
315 sg_entries[i].len = MIN(PAGE_SIZE, len - len_filled);
316 len_filled += sg_entries[i].len;
321 io->scsiio.kern_data_ptr = softc->ramdisk_pages[0];
325 len_filled = min(len, softc->rd_size);
326 io->scsiio.kern_data_ptr = softc->ramdisk_buffer;
327 #endif /* CTL_RAMDISK_PAGES */
329 io->scsiio.be_move_done = ctl_backend_ramdisk_move_done;
330 io->scsiio.kern_data_resid = 0;
331 io->scsiio.kern_data_len = len_filled;
332 io->scsiio.kern_sg_entries = sg_filled;
333 io->io_hdr.flags |= CTL_FLAG_ALLOCATED;
334 io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer -= len_filled;
336 getbinuptime(&io->io_hdr.dma_start_bt);
342 ctl_backend_ramdisk_worker(void *context, int pending)
344 struct ctl_be_ramdisk_lun *be_lun;
347 be_lun = (struct ctl_be_ramdisk_lun *)context;
349 mtx_lock(&be_lun->queue_lock);
351 io = (union ctl_io *)STAILQ_FIRST(&be_lun->cont_queue);
353 STAILQ_REMOVE(&be_lun->cont_queue, &io->io_hdr,
355 mtx_unlock(&be_lun->queue_lock);
356 ctl_backend_ramdisk_continue(io);
357 mtx_lock(&be_lun->queue_lock);
362 * If we get here, there is no work left in the queues, so
363 * just break out and let the task queue go to sleep.
367 mtx_unlock(&be_lun->queue_lock);
371 ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
372 int flag, struct thread *td)
374 struct ctl_be_ramdisk_softc *softc = &rd_softc;
375 struct ctl_lun_req *lun_req;
381 lun_req = (struct ctl_lun_req *)addr;
382 switch (lun_req->reqtype) {
383 case CTL_LUNREQ_CREATE:
384 retval = ctl_backend_ramdisk_create(softc, lun_req);
387 retval = ctl_backend_ramdisk_rm(softc, lun_req);
389 case CTL_LUNREQ_MODIFY:
390 retval = ctl_backend_ramdisk_modify(softc, lun_req);
393 lun_req->status = CTL_LUN_ERROR;
394 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
395 "%s: invalid LUN request type %d", __func__,
409 ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
410 struct ctl_lun_req *req)
412 struct ctl_be_ramdisk_lun *be_lun;
413 struct ctl_lun_rm_params *params;
416 params = &req->reqdata.rm;
417 mtx_lock(&softc->lock);
418 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
419 if (be_lun->cbe_lun.lun_id == params->lun_id)
422 mtx_unlock(&softc->lock);
423 if (be_lun == NULL) {
424 snprintf(req->error_str, sizeof(req->error_str),
425 "%s: LUN %u is not managed by the ramdisk backend",
426 __func__, params->lun_id);
430 retval = ctl_disable_lun(&be_lun->cbe_lun);
432 snprintf(req->error_str, sizeof(req->error_str),
433 "%s: error %d returned from ctl_disable_lun() for "
434 "LUN %d", __func__, retval, params->lun_id);
439 * Set the waiting flag before we invalidate the LUN. Our shutdown
440 * routine can be called any time after we invalidate the LUN,
441 * and can be called from our context.
443 * This tells the shutdown routine that we're waiting, or we're
444 * going to wait for the shutdown to happen.
446 mtx_lock(&softc->lock);
447 be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
448 mtx_unlock(&softc->lock);
450 retval = ctl_invalidate_lun(&be_lun->cbe_lun);
452 snprintf(req->error_str, sizeof(req->error_str),
453 "%s: error %d returned from ctl_invalidate_lun() for "
454 "LUN %d", __func__, retval, params->lun_id);
455 mtx_lock(&softc->lock);
456 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
457 mtx_unlock(&softc->lock);
461 mtx_lock(&softc->lock);
462 while ((be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) == 0) {
463 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
467 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
470 * We only remove this LUN from the list and free it (below) if
471 * retval == 0. If the user interrupted the wait, we just bail out
472 * without actually freeing the LUN. We let the shutdown routine
473 * free the LUN if that happens.
476 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
481 mtx_unlock(&softc->lock);
484 taskqueue_drain_all(be_lun->io_taskqueue);
485 taskqueue_free(be_lun->io_taskqueue);
486 ctl_free_opts(&be_lun->cbe_lun.options);
487 mtx_destroy(&be_lun->queue_lock);
488 free(be_lun, M_RAMDISK);
491 req->status = CTL_LUN_OK;
495 req->status = CTL_LUN_ERROR;
500 ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
501 struct ctl_lun_req *req)
503 struct ctl_be_ramdisk_lun *be_lun;
504 struct ctl_be_lun *cbe_lun;
505 struct ctl_lun_create_params *params;
511 params = &req->reqdata.create;
513 be_lun = malloc(sizeof(*be_lun), M_RAMDISK, M_ZERO | M_WAITOK);
514 cbe_lun = &be_lun->cbe_lun;
515 cbe_lun->be_lun = be_lun;
516 be_lun->params = req->reqdata.create;
517 be_lun->softc = softc;
518 sprintf(be_lun->lunname, "cram%d", softc->num_luns);
519 ctl_init_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args);
521 if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
522 cbe_lun->lun_type = params->device_type;
524 cbe_lun->lun_type = T_DIRECT;
525 be_lun->flags = CTL_BE_RAMDISK_LUN_UNCONFIGURED;
527 value = ctl_get_opt(&cbe_lun->options, "ha_role");
529 if (strcmp(value, "primary") == 0)
530 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
531 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
532 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
534 if (cbe_lun->lun_type == T_DIRECT ||
535 cbe_lun->lun_type == T_CDROM) {
536 if (params->blocksize_bytes != 0)
537 cbe_lun->blocksize = params->blocksize_bytes;
538 else if (cbe_lun->lun_type == T_CDROM)
539 cbe_lun->blocksize = 2048;
541 cbe_lun->blocksize = 512;
542 if (params->lun_size_bytes < cbe_lun->blocksize) {
543 snprintf(req->error_str, sizeof(req->error_str),
544 "%s: LUN size %ju < blocksize %u", __func__,
545 params->lun_size_bytes, cbe_lun->blocksize);
548 be_lun->size_blocks = params->lun_size_bytes / cbe_lun->blocksize;
549 be_lun->size_bytes = be_lun->size_blocks * cbe_lun->blocksize;
550 cbe_lun->maxlba = be_lun->size_blocks - 1;
551 cbe_lun->atomicblock = UINT32_MAX;
552 cbe_lun->opttxferlen = softc->rd_size / cbe_lun->blocksize;
555 /* Tell the user the blocksize we ended up using */
556 params->blocksize_bytes = cbe_lun->blocksize;
557 params->lun_size_bytes = be_lun->size_bytes;
559 value = ctl_get_opt(&cbe_lun->options, "unmap");
560 if (value != NULL && strcmp(value, "on") == 0)
561 cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
562 value = ctl_get_opt(&cbe_lun->options, "readonly");
564 if (strcmp(value, "on") == 0)
565 cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
566 } else if (cbe_lun->lun_type != T_DIRECT)
567 cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
568 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
569 value = ctl_get_opt(&cbe_lun->options, "serseq");
570 if (value != NULL && strcmp(value, "on") == 0)
571 cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
572 else if (value != NULL && strcmp(value, "read") == 0)
573 cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
574 else if (value != NULL && strcmp(value, "off") == 0)
575 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
577 if (params->flags & CTL_LUN_FLAG_ID_REQ) {
578 cbe_lun->req_lun_id = params->req_lun_id;
579 cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
581 cbe_lun->req_lun_id = 0;
583 cbe_lun->lun_shutdown = ctl_backend_ramdisk_lun_shutdown;
584 cbe_lun->lun_config_status = ctl_backend_ramdisk_lun_config_status;
585 cbe_lun->be = &ctl_be_ramdisk_driver;
586 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
587 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
589 strncpy((char *)cbe_lun->serial_num, tmpstr,
590 MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
592 /* Tell the user what we used for a serial number */
593 strncpy((char *)params->serial_num, tmpstr,
594 MIN(sizeof(params->serial_num), sizeof(tmpstr)));
596 strncpy((char *)cbe_lun->serial_num, params->serial_num,
597 MIN(sizeof(cbe_lun->serial_num),
598 sizeof(params->serial_num)));
600 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
601 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
602 strncpy((char *)cbe_lun->device_id, tmpstr,
603 MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
605 /* Tell the user what we used for a device ID */
606 strncpy((char *)params->device_id, tmpstr,
607 MIN(sizeof(params->device_id), sizeof(tmpstr)));
609 strncpy((char *)cbe_lun->device_id, params->device_id,
610 MIN(sizeof(cbe_lun->device_id),
611 sizeof(params->device_id)));
614 STAILQ_INIT(&be_lun->cont_queue);
615 mtx_init(&be_lun->queue_lock, "cram queue lock", NULL, MTX_DEF);
616 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_backend_ramdisk_worker,
619 be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
620 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
621 if (be_lun->io_taskqueue == NULL) {
622 snprintf(req->error_str, sizeof(req->error_str),
623 "%s: Unable to create taskqueue", __func__);
627 retval = taskqueue_start_threads(&be_lun->io_taskqueue,
631 "%s taskq", be_lun->lunname);
635 mtx_lock(&softc->lock);
637 STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
638 mtx_unlock(&softc->lock);
640 retval = ctl_add_lun(&be_lun->cbe_lun);
642 mtx_lock(&softc->lock);
643 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
646 mtx_unlock(&softc->lock);
647 snprintf(req->error_str, sizeof(req->error_str),
648 "%s: ctl_add_lun() returned error %d, see dmesg for "
649 "details", __func__, retval);
654 mtx_lock(&softc->lock);
657 * Tell the config_status routine that we're waiting so it won't
658 * clean up the LUN in the event of an error.
660 be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
662 while (be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) {
663 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
667 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
669 if (be_lun->flags & CTL_BE_RAMDISK_LUN_CONFIG_ERR) {
670 snprintf(req->error_str, sizeof(req->error_str),
671 "%s: LUN configuration error, see dmesg for details",
673 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
676 mtx_unlock(&softc->lock);
679 params->req_lun_id = cbe_lun->lun_id;
681 mtx_unlock(&softc->lock);
683 req->status = CTL_LUN_OK;
687 req->status = CTL_LUN_ERROR;
688 if (be_lun != NULL) {
689 if (be_lun->io_taskqueue != NULL) {
690 taskqueue_free(be_lun->io_taskqueue);
692 ctl_free_opts(&cbe_lun->options);
693 mtx_destroy(&be_lun->queue_lock);
694 free(be_lun, M_RAMDISK);
700 ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
701 struct ctl_lun_req *req)
703 struct ctl_be_ramdisk_lun *be_lun;
704 struct ctl_be_lun *cbe_lun;
705 struct ctl_lun_modify_params *params;
710 params = &req->reqdata.modify;
712 mtx_lock(&softc->lock);
713 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
714 if (be_lun->cbe_lun.lun_id == params->lun_id)
717 mtx_unlock(&softc->lock);
718 if (be_lun == NULL) {
719 snprintf(req->error_str, sizeof(req->error_str),
720 "%s: LUN %u is not managed by the ramdisk backend",
721 __func__, params->lun_id);
724 cbe_lun = &be_lun->cbe_lun;
726 if (params->lun_size_bytes != 0)
727 be_lun->params.lun_size_bytes = params->lun_size_bytes;
728 ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args);
730 wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
731 value = ctl_get_opt(&cbe_lun->options, "ha_role");
733 if (strcmp(value, "primary") == 0)
734 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
736 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
737 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
738 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
740 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
741 if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
742 if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
743 ctl_lun_primary(cbe_lun);
745 ctl_lun_secondary(cbe_lun);
748 blocksize = be_lun->cbe_lun.blocksize;
749 if (be_lun->params.lun_size_bytes < blocksize) {
750 snprintf(req->error_str, sizeof(req->error_str),
751 "%s: LUN size %ju < blocksize %u", __func__,
752 be_lun->params.lun_size_bytes, blocksize);
755 be_lun->size_blocks = be_lun->params.lun_size_bytes / blocksize;
756 be_lun->size_bytes = be_lun->size_blocks * blocksize;
757 be_lun->cbe_lun.maxlba = be_lun->size_blocks - 1;
758 ctl_lun_capacity_changed(&be_lun->cbe_lun);
760 /* Tell the user the exact size we ended up using */
761 params->lun_size_bytes = be_lun->size_bytes;
763 req->status = CTL_LUN_OK;
767 req->status = CTL_LUN_ERROR;
772 ctl_backend_ramdisk_lun_shutdown(void *be_lun)
774 struct ctl_be_ramdisk_lun *lun;
775 struct ctl_be_ramdisk_softc *softc;
778 lun = (struct ctl_be_ramdisk_lun *)be_lun;
782 mtx_lock(&softc->lock);
783 lun->flags |= CTL_BE_RAMDISK_LUN_UNCONFIGURED;
784 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
787 STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
792 mtx_unlock(&softc->lock);
795 free(be_lun, M_RAMDISK);
799 ctl_backend_ramdisk_lun_config_status(void *be_lun,
800 ctl_lun_config_status status)
802 struct ctl_be_ramdisk_lun *lun;
803 struct ctl_be_ramdisk_softc *softc;
805 lun = (struct ctl_be_ramdisk_lun *)be_lun;
808 if (status == CTL_LUN_CONFIG_OK) {
809 mtx_lock(&softc->lock);
810 lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
811 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING)
813 mtx_unlock(&softc->lock);
816 * We successfully added the LUN, attempt to enable it.
818 if (ctl_enable_lun(&lun->cbe_lun) != 0) {
819 printf("%s: ctl_enable_lun() failed!\n", __func__);
820 if (ctl_invalidate_lun(&lun->cbe_lun) != 0) {
821 printf("%s: ctl_invalidate_lun() failed!\n",
830 mtx_lock(&softc->lock);
831 lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
834 * If we have a user waiting, let him handle the cleanup. If not,
835 * clean things up here.
837 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
838 lun->flags |= CTL_BE_RAMDISK_LUN_CONFIG_ERR;
841 STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
844 free(lun, M_RAMDISK);
846 mtx_unlock(&softc->lock);
850 ctl_backend_ramdisk_config_write(union ctl_io *io)
852 struct ctl_be_lun *cbe_lun;
855 cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
856 CTL_PRIV_BACKEND_LUN].ptr;
858 switch (io->scsiio.cdb[0]) {
859 case SYNCHRONIZE_CACHE:
860 case SYNCHRONIZE_CACHE_16:
862 * The upper level CTL code will filter out any CDBs with
863 * the immediate bit set and return the proper error. It
864 * will also not allow a sync cache command to go to a LUN
865 * that is powered down.
867 * We don't really need to worry about what LBA range the
868 * user asked to be synced out. When they issue a sync
869 * cache command, we'll sync out the whole thing.
871 * This is obviously just a stubbed out implementation.
872 * The real implementation will be in the RAIDCore/CTL
873 * interface, and can only really happen when RAIDCore
874 * implements a per-array cache sync.
876 ctl_set_success(&io->scsiio);
877 ctl_config_write_done(io);
879 case START_STOP_UNIT: {
880 struct scsi_start_stop_unit *cdb;
882 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
883 if (cdb->how & SSS_START) {
884 if (cdb->how & SSS_LOEJ)
885 ctl_lun_has_media(cbe_lun);
886 ctl_start_lun(cbe_lun);
888 ctl_stop_lun(cbe_lun);
889 if (cdb->how & SSS_LOEJ)
890 ctl_lun_ejected(cbe_lun);
892 ctl_set_success(&io->scsiio);
893 ctl_config_write_done(io);
900 ctl_set_success(&io->scsiio);
901 ctl_config_write_done(io);
904 ctl_set_invalid_opcode(&io->scsiio);
905 ctl_config_write_done(io);
906 retval = CTL_RETVAL_COMPLETE;
914 ctl_backend_ramdisk_config_read(union ctl_io *io)
918 switch (io->scsiio.cdb[0]) {
919 case SERVICE_ACTION_IN:
920 if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
921 /* We have nothing to tell, leave default data. */
922 ctl_config_read_done(io);
923 retval = CTL_RETVAL_COMPLETE;
926 ctl_set_invalid_field(&io->scsiio,
932 ctl_config_read_done(io);
933 retval = CTL_RETVAL_COMPLETE;
936 ctl_set_invalid_opcode(&io->scsiio);
937 ctl_config_read_done(io);
938 retval = CTL_RETVAL_COMPLETE;