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 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun) {
185 * Drop our lock here. Since ctl_invalidate_lun() can call
186 * back into us, this could potentially lead to a recursive
187 * lock of the same mutex, which would cause a hang.
189 mtx_unlock(&softc->lock);
190 ctl_disable_lun(&lun->cbe_lun);
191 ctl_invalidate_lun(&lun->cbe_lun);
192 mtx_lock(&softc->lock);
194 mtx_unlock(&softc->lock);
196 #ifdef CTL_RAMDISK_PAGES
197 for (i = 0; i < softc->num_pages; i++)
198 free(softc->ramdisk_pages[i], M_RAMDISK);
200 free(softc->ramdisk_pages, M_RAMDISK);
202 free(softc->ramdisk_buffer, M_RAMDISK);
205 if (ctl_backend_deregister(&ctl_be_ramdisk_driver) != 0) {
206 printf("ctl_backend_ramdisk_shutdown: "
207 "ctl_backend_deregister() failed!\n");
212 ctl_backend_ramdisk_move_done(union ctl_io *io)
214 struct ctl_be_lun *cbe_lun;
215 struct ctl_be_ramdisk_lun *be_lun;
217 struct bintime cur_bt;
220 CTL_DEBUG_PRINT(("ctl_backend_ramdisk_move_done\n"));
221 cbe_lun = CTL_BACKEND_LUN(io);
222 be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun->be_lun;
224 getbinuptime(&cur_bt);
225 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
226 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
228 io->io_hdr.num_dmas++;
229 if (io->scsiio.kern_sg_entries > 0)
230 free(io->scsiio.kern_data_ptr, M_RAMDISK);
231 io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
232 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
234 } else if (io->io_hdr.port_status != 0 &&
235 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
236 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
237 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
238 /*retry_count*/ io->io_hdr.port_status);
239 } else if (io->scsiio.kern_data_resid != 0 &&
240 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
241 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
242 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
243 ctl_set_invalid_field_ciu(&io->scsiio);
244 } else if ((io->io_hdr.port_status == 0) &&
245 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
246 if (io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer > 0) {
247 mtx_lock(&be_lun->queue_lock);
248 STAILQ_INSERT_TAIL(&be_lun->cont_queue,
250 mtx_unlock(&be_lun->queue_lock);
251 taskqueue_enqueue(be_lun->io_taskqueue,
255 ctl_set_success(&io->scsiio);
257 ctl_data_submit_done(io);
262 ctl_backend_ramdisk_submit(union ctl_io *io)
264 struct ctl_be_lun *cbe_lun;
265 struct ctl_lba_len_flags *lbalen;
267 cbe_lun = CTL_BACKEND_LUN(io);
268 lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
269 if (lbalen->flags & CTL_LLF_VERIFY) {
270 ctl_set_success(&io->scsiio);
271 ctl_data_submit_done(io);
272 return (CTL_RETVAL_COMPLETE);
274 io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer =
275 lbalen->len * cbe_lun->blocksize;
276 ctl_backend_ramdisk_continue(io);
277 return (CTL_RETVAL_COMPLETE);
281 ctl_backend_ramdisk_continue(union ctl_io *io)
283 struct ctl_be_ramdisk_softc *softc;
284 int len, len_filled, sg_filled;
285 #ifdef CTL_RAMDISK_PAGES
286 struct ctl_sg_entry *sg_entries;
291 len = io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer;
292 #ifdef CTL_RAMDISK_PAGES
293 sg_filled = min(btoc(len), softc->num_pages);
295 io->scsiio.kern_data_ptr = malloc(sizeof(struct ctl_sg_entry) *
296 sg_filled, M_RAMDISK,
298 sg_entries = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
299 for (i = 0, len_filled = 0; i < sg_filled; i++) {
300 sg_entries[i].addr = softc->ramdisk_pages[i];
301 sg_entries[i].len = MIN(PAGE_SIZE, len - len_filled);
302 len_filled += sg_entries[i].len;
307 io->scsiio.kern_data_ptr = softc->ramdisk_pages[0];
311 len_filled = min(len, softc->rd_size);
312 io->scsiio.kern_data_ptr = softc->ramdisk_buffer;
313 #endif /* CTL_RAMDISK_PAGES */
315 io->scsiio.be_move_done = ctl_backend_ramdisk_move_done;
316 io->scsiio.kern_data_len = len_filled;
317 io->scsiio.kern_sg_entries = sg_filled;
318 io->io_hdr.flags |= CTL_FLAG_ALLOCATED;
319 io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer -= len_filled;
321 getbinuptime(&io->io_hdr.dma_start_bt);
327 ctl_backend_ramdisk_worker(void *context, int pending)
329 struct ctl_be_ramdisk_lun *be_lun;
332 be_lun = (struct ctl_be_ramdisk_lun *)context;
334 mtx_lock(&be_lun->queue_lock);
336 io = (union ctl_io *)STAILQ_FIRST(&be_lun->cont_queue);
338 STAILQ_REMOVE(&be_lun->cont_queue, &io->io_hdr,
340 mtx_unlock(&be_lun->queue_lock);
341 ctl_backend_ramdisk_continue(io);
342 mtx_lock(&be_lun->queue_lock);
347 * If we get here, there is no work left in the queues, so
348 * just break out and let the task queue go to sleep.
352 mtx_unlock(&be_lun->queue_lock);
356 ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
357 int flag, struct thread *td)
359 struct ctl_be_ramdisk_softc *softc = &rd_softc;
360 struct ctl_lun_req *lun_req;
366 lun_req = (struct ctl_lun_req *)addr;
367 switch (lun_req->reqtype) {
368 case CTL_LUNREQ_CREATE:
369 retval = ctl_backend_ramdisk_create(softc, lun_req);
372 retval = ctl_backend_ramdisk_rm(softc, lun_req);
374 case CTL_LUNREQ_MODIFY:
375 retval = ctl_backend_ramdisk_modify(softc, lun_req);
378 lun_req->status = CTL_LUN_ERROR;
379 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
380 "%s: invalid LUN request type %d", __func__,
394 ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
395 struct ctl_lun_req *req)
397 struct ctl_be_ramdisk_lun *be_lun;
398 struct ctl_lun_rm_params *params;
401 params = &req->reqdata.rm;
402 mtx_lock(&softc->lock);
403 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
404 if (be_lun->cbe_lun.lun_id == params->lun_id)
407 mtx_unlock(&softc->lock);
408 if (be_lun == NULL) {
409 snprintf(req->error_str, sizeof(req->error_str),
410 "%s: LUN %u is not managed by the ramdisk backend",
411 __func__, params->lun_id);
415 retval = ctl_disable_lun(&be_lun->cbe_lun);
417 snprintf(req->error_str, sizeof(req->error_str),
418 "%s: error %d returned from ctl_disable_lun() for "
419 "LUN %d", __func__, retval, params->lun_id);
424 * Set the waiting flag before we invalidate the LUN. Our shutdown
425 * routine can be called any time after we invalidate the LUN,
426 * and can be called from our context.
428 * This tells the shutdown routine that we're waiting, or we're
429 * going to wait for the shutdown to happen.
431 mtx_lock(&softc->lock);
432 be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
433 mtx_unlock(&softc->lock);
435 retval = ctl_invalidate_lun(&be_lun->cbe_lun);
437 snprintf(req->error_str, sizeof(req->error_str),
438 "%s: error %d returned from ctl_invalidate_lun() for "
439 "LUN %d", __func__, retval, params->lun_id);
440 mtx_lock(&softc->lock);
441 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
442 mtx_unlock(&softc->lock);
446 mtx_lock(&softc->lock);
447 while ((be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) == 0) {
448 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
452 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
455 * We only remove this LUN from the list and free it (below) if
456 * retval == 0. If the user interrupted the wait, we just bail out
457 * without actually freeing the LUN. We let the shutdown routine
458 * free the LUN if that happens.
461 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
466 mtx_unlock(&softc->lock);
469 taskqueue_drain_all(be_lun->io_taskqueue);
470 taskqueue_free(be_lun->io_taskqueue);
471 ctl_free_opts(&be_lun->cbe_lun.options);
472 mtx_destroy(&be_lun->queue_lock);
473 free(be_lun, M_RAMDISK);
476 req->status = CTL_LUN_OK;
480 req->status = CTL_LUN_ERROR;
485 ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
486 struct ctl_lun_req *req)
488 struct ctl_be_ramdisk_lun *be_lun;
489 struct ctl_be_lun *cbe_lun;
490 struct ctl_lun_create_params *params;
496 params = &req->reqdata.create;
498 be_lun = malloc(sizeof(*be_lun), M_RAMDISK, M_ZERO | M_WAITOK);
499 cbe_lun = &be_lun->cbe_lun;
500 cbe_lun->be_lun = be_lun;
501 be_lun->params = req->reqdata.create;
502 be_lun->softc = softc;
503 sprintf(be_lun->lunname, "cram%d", softc->num_luns);
504 ctl_init_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args);
506 if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
507 cbe_lun->lun_type = params->device_type;
509 cbe_lun->lun_type = T_DIRECT;
510 be_lun->flags = CTL_BE_RAMDISK_LUN_UNCONFIGURED;
512 value = ctl_get_opt(&cbe_lun->options, "ha_role");
514 if (strcmp(value, "primary") == 0)
515 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
516 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
517 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
519 if (cbe_lun->lun_type == T_DIRECT ||
520 cbe_lun->lun_type == T_CDROM) {
521 if (params->blocksize_bytes != 0)
522 cbe_lun->blocksize = params->blocksize_bytes;
523 else if (cbe_lun->lun_type == T_CDROM)
524 cbe_lun->blocksize = 2048;
526 cbe_lun->blocksize = 512;
527 if (params->lun_size_bytes < cbe_lun->blocksize) {
528 snprintf(req->error_str, sizeof(req->error_str),
529 "%s: LUN size %ju < blocksize %u", __func__,
530 params->lun_size_bytes, cbe_lun->blocksize);
533 be_lun->size_blocks = params->lun_size_bytes / cbe_lun->blocksize;
534 be_lun->size_bytes = be_lun->size_blocks * cbe_lun->blocksize;
535 cbe_lun->maxlba = be_lun->size_blocks - 1;
536 cbe_lun->atomicblock = UINT32_MAX;
537 cbe_lun->opttxferlen = softc->rd_size / cbe_lun->blocksize;
540 /* Tell the user the blocksize we ended up using */
541 params->blocksize_bytes = cbe_lun->blocksize;
542 params->lun_size_bytes = be_lun->size_bytes;
544 value = ctl_get_opt(&cbe_lun->options, "unmap");
545 if (value != NULL && strcmp(value, "on") == 0)
546 cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
547 value = ctl_get_opt(&cbe_lun->options, "readonly");
549 if (strcmp(value, "on") == 0)
550 cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
551 } else if (cbe_lun->lun_type != T_DIRECT)
552 cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
553 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
554 value = ctl_get_opt(&cbe_lun->options, "serseq");
555 if (value != NULL && strcmp(value, "on") == 0)
556 cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
557 else if (value != NULL && strcmp(value, "read") == 0)
558 cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
559 else if (value != NULL && strcmp(value, "off") == 0)
560 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
562 if (params->flags & CTL_LUN_FLAG_ID_REQ) {
563 cbe_lun->req_lun_id = params->req_lun_id;
564 cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
566 cbe_lun->req_lun_id = 0;
568 cbe_lun->lun_shutdown = ctl_backend_ramdisk_lun_shutdown;
569 cbe_lun->lun_config_status = ctl_backend_ramdisk_lun_config_status;
570 cbe_lun->be = &ctl_be_ramdisk_driver;
571 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
572 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
574 strncpy((char *)cbe_lun->serial_num, tmpstr,
575 MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
577 /* Tell the user what we used for a serial number */
578 strncpy((char *)params->serial_num, tmpstr,
579 MIN(sizeof(params->serial_num), sizeof(tmpstr)));
581 strncpy((char *)cbe_lun->serial_num, params->serial_num,
582 MIN(sizeof(cbe_lun->serial_num),
583 sizeof(params->serial_num)));
585 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
586 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
587 strncpy((char *)cbe_lun->device_id, tmpstr,
588 MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
590 /* Tell the user what we used for a device ID */
591 strncpy((char *)params->device_id, tmpstr,
592 MIN(sizeof(params->device_id), sizeof(tmpstr)));
594 strncpy((char *)cbe_lun->device_id, params->device_id,
595 MIN(sizeof(cbe_lun->device_id),
596 sizeof(params->device_id)));
599 STAILQ_INIT(&be_lun->cont_queue);
600 mtx_init(&be_lun->queue_lock, "cram queue lock", NULL, MTX_DEF);
601 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_backend_ramdisk_worker,
604 be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
605 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
606 if (be_lun->io_taskqueue == NULL) {
607 snprintf(req->error_str, sizeof(req->error_str),
608 "%s: Unable to create taskqueue", __func__);
612 retval = taskqueue_start_threads(&be_lun->io_taskqueue,
616 "%s taskq", be_lun->lunname);
620 mtx_lock(&softc->lock);
622 STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
623 mtx_unlock(&softc->lock);
625 retval = ctl_add_lun(&be_lun->cbe_lun);
627 mtx_lock(&softc->lock);
628 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
631 mtx_unlock(&softc->lock);
632 snprintf(req->error_str, sizeof(req->error_str),
633 "%s: ctl_add_lun() returned error %d, see dmesg for "
634 "details", __func__, retval);
639 mtx_lock(&softc->lock);
642 * Tell the config_status routine that we're waiting so it won't
643 * clean up the LUN in the event of an error.
645 be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
647 while (be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) {
648 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
652 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
654 if (be_lun->flags & CTL_BE_RAMDISK_LUN_CONFIG_ERR) {
655 snprintf(req->error_str, sizeof(req->error_str),
656 "%s: LUN configuration error, see dmesg for details",
658 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
661 mtx_unlock(&softc->lock);
664 params->req_lun_id = cbe_lun->lun_id;
666 mtx_unlock(&softc->lock);
668 req->status = CTL_LUN_OK;
672 req->status = CTL_LUN_ERROR;
673 if (be_lun != NULL) {
674 if (be_lun->io_taskqueue != NULL) {
675 taskqueue_free(be_lun->io_taskqueue);
677 ctl_free_opts(&cbe_lun->options);
678 mtx_destroy(&be_lun->queue_lock);
679 free(be_lun, M_RAMDISK);
685 ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
686 struct ctl_lun_req *req)
688 struct ctl_be_ramdisk_lun *be_lun;
689 struct ctl_be_lun *cbe_lun;
690 struct ctl_lun_modify_params *params;
695 params = &req->reqdata.modify;
697 mtx_lock(&softc->lock);
698 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
699 if (be_lun->cbe_lun.lun_id == params->lun_id)
702 mtx_unlock(&softc->lock);
703 if (be_lun == NULL) {
704 snprintf(req->error_str, sizeof(req->error_str),
705 "%s: LUN %u is not managed by the ramdisk backend",
706 __func__, params->lun_id);
709 cbe_lun = &be_lun->cbe_lun;
711 if (params->lun_size_bytes != 0)
712 be_lun->params.lun_size_bytes = params->lun_size_bytes;
713 ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args);
715 wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
716 value = ctl_get_opt(&cbe_lun->options, "ha_role");
718 if (strcmp(value, "primary") == 0)
719 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
721 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
722 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
723 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
725 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
726 if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
727 if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
728 ctl_lun_primary(cbe_lun);
730 ctl_lun_secondary(cbe_lun);
733 blocksize = be_lun->cbe_lun.blocksize;
734 if (be_lun->params.lun_size_bytes < blocksize) {
735 snprintf(req->error_str, sizeof(req->error_str),
736 "%s: LUN size %ju < blocksize %u", __func__,
737 be_lun->params.lun_size_bytes, blocksize);
740 be_lun->size_blocks = be_lun->params.lun_size_bytes / blocksize;
741 be_lun->size_bytes = be_lun->size_blocks * blocksize;
742 be_lun->cbe_lun.maxlba = be_lun->size_blocks - 1;
743 ctl_lun_capacity_changed(&be_lun->cbe_lun);
745 /* Tell the user the exact size we ended up using */
746 params->lun_size_bytes = be_lun->size_bytes;
748 req->status = CTL_LUN_OK;
752 req->status = CTL_LUN_ERROR;
757 ctl_backend_ramdisk_lun_shutdown(void *be_lun)
759 struct ctl_be_ramdisk_lun *lun;
760 struct ctl_be_ramdisk_softc *softc;
763 lun = (struct ctl_be_ramdisk_lun *)be_lun;
767 mtx_lock(&softc->lock);
768 lun->flags |= CTL_BE_RAMDISK_LUN_UNCONFIGURED;
769 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
772 STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
777 mtx_unlock(&softc->lock);
780 free(be_lun, M_RAMDISK);
784 ctl_backend_ramdisk_lun_config_status(void *be_lun,
785 ctl_lun_config_status status)
787 struct ctl_be_ramdisk_lun *lun;
788 struct ctl_be_ramdisk_softc *softc;
790 lun = (struct ctl_be_ramdisk_lun *)be_lun;
793 if (status == CTL_LUN_CONFIG_OK) {
794 mtx_lock(&softc->lock);
795 lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
796 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING)
798 mtx_unlock(&softc->lock);
801 * We successfully added the LUN, attempt to enable it.
803 if (ctl_enable_lun(&lun->cbe_lun) != 0) {
804 printf("%s: ctl_enable_lun() failed!\n", __func__);
805 if (ctl_invalidate_lun(&lun->cbe_lun) != 0) {
806 printf("%s: ctl_invalidate_lun() failed!\n",
815 mtx_lock(&softc->lock);
816 lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
819 * If we have a user waiting, let him handle the cleanup. If not,
820 * clean things up here.
822 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
823 lun->flags |= CTL_BE_RAMDISK_LUN_CONFIG_ERR;
826 STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
829 free(lun, M_RAMDISK);
831 mtx_unlock(&softc->lock);
835 ctl_backend_ramdisk_config_write(union ctl_io *io)
837 struct ctl_be_lun *cbe_lun;
840 cbe_lun = CTL_BACKEND_LUN(io);
842 switch (io->scsiio.cdb[0]) {
843 case SYNCHRONIZE_CACHE:
844 case SYNCHRONIZE_CACHE_16:
846 * The upper level CTL code will filter out any CDBs with
847 * the immediate bit set and return the proper error. It
848 * will also not allow a sync cache command to go to a LUN
849 * that is powered down.
851 * We don't really need to worry about what LBA range the
852 * user asked to be synced out. When they issue a sync
853 * cache command, we'll sync out the whole thing.
855 * This is obviously just a stubbed out implementation.
856 * The real implementation will be in the RAIDCore/CTL
857 * interface, and can only really happen when RAIDCore
858 * implements a per-array cache sync.
860 ctl_set_success(&io->scsiio);
861 ctl_config_write_done(io);
863 case START_STOP_UNIT: {
864 struct scsi_start_stop_unit *cdb;
866 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
867 if ((cdb->how & SSS_PC_MASK) != 0) {
868 ctl_set_success(&io->scsiio);
869 ctl_config_write_done(io);
872 if (cdb->how & SSS_START) {
873 if (cdb->how & SSS_LOEJ)
874 ctl_lun_has_media(cbe_lun);
875 ctl_start_lun(cbe_lun);
877 ctl_stop_lun(cbe_lun);
878 if (cdb->how & SSS_LOEJ)
879 ctl_lun_ejected(cbe_lun);
881 ctl_set_success(&io->scsiio);
882 ctl_config_write_done(io);
889 ctl_set_success(&io->scsiio);
890 ctl_config_write_done(io);
893 ctl_set_invalid_opcode(&io->scsiio);
894 ctl_config_write_done(io);
895 retval = CTL_RETVAL_COMPLETE;
903 ctl_backend_ramdisk_config_read(union ctl_io *io)
907 switch (io->scsiio.cdb[0]) {
908 case SERVICE_ACTION_IN:
909 if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
910 /* We have nothing to tell, leave default data. */
911 ctl_config_read_done(io);
912 retval = CTL_RETVAL_COMPLETE;
915 ctl_set_invalid_field(&io->scsiio,
921 ctl_config_read_done(io);
922 retval = CTL_RETVAL_COMPLETE;
925 ctl_set_invalid_opcode(&io->scsiio);
926 ctl_config_read_done(io);
927 retval = CTL_RETVAL_COMPLETE;