2 * Copyright (c) 2003, 2008 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
6 * Portions of this software were developed by Edward Tomasz Napierala
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_ramdisk.c#3 $
37 * CAM Target Layer backend for a "fake" ramdisk.
39 * Author: Ken Merry <ken@FreeBSD.org>
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
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>
51 #include <sys/mutex.h>
52 #include <sys/malloc.h>
54 #include <sys/queue.h>
56 #include <sys/ioccom.h>
57 #include <sys/module.h>
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/ctl/ctl_io.h>
61 #include <cam/ctl/ctl.h>
62 #include <cam/ctl/ctl_util.h>
63 #include <cam/ctl/ctl_backend.h>
64 #include <cam/ctl/ctl_frontend_internal.h>
65 #include <cam/ctl/ctl_debug.h>
66 #include <cam/ctl/ctl_ioctl.h>
67 #include <cam/ctl/ctl_error.h>
70 CTL_BE_RAMDISK_LUN_UNCONFIGURED = 0x01,
71 CTL_BE_RAMDISK_LUN_CONFIG_ERR = 0x02,
72 CTL_BE_RAMDISK_LUN_WAITING = 0x04
73 } ctl_be_ramdisk_lun_flags;
75 struct ctl_be_ramdisk_lun {
78 struct ctl_be_ramdisk_softc *softc;
79 ctl_be_ramdisk_lun_flags flags;
80 STAILQ_ENTRY(ctl_be_ramdisk_lun) links;
81 struct ctl_be_lun ctl_be_lun;
84 struct ctl_be_ramdisk_softc {
87 #ifdef CTL_RAMDISK_PAGES
88 uint8_t **ramdisk_pages;
91 uint8_t *ramdisk_buffer;
94 STAILQ_HEAD(, ctl_be_ramdisk_lun) lun_list;
97 static struct ctl_be_ramdisk_softc rd_softc;
99 int ctl_backend_ramdisk_init(void);
100 void ctl_backend_ramdisk_shutdown(void);
101 static int ctl_backend_ramdisk_move_done(union ctl_io *io);
102 static int ctl_backend_ramdisk_submit(union ctl_io *io);
103 static int ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd,
104 caddr_t addr, int flag, struct thread *td);
105 static int ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
106 struct ctl_lun_req *req);
107 static int ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
108 struct ctl_lun_req *req, int do_wait);
109 static int ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
110 struct ctl_lun_req *req);
111 static void ctl_backend_ramdisk_lun_shutdown(void *be_lun);
112 static void ctl_backend_ramdisk_lun_config_status(void *be_lun,
113 ctl_lun_config_status status);
114 static int ctl_backend_ramdisk_config_write(union ctl_io *io);
115 static int ctl_backend_ramdisk_config_read(union ctl_io *io);
117 static struct ctl_backend_driver ctl_be_ramdisk_driver =
120 .flags = CTL_BE_FLAG_HAS_CONFIG,
121 .init = ctl_backend_ramdisk_init,
122 .data_submit = ctl_backend_ramdisk_submit,
123 .data_move_done = ctl_backend_ramdisk_move_done,
124 .config_read = ctl_backend_ramdisk_config_read,
125 .config_write = ctl_backend_ramdisk_config_write,
126 .ioctl = ctl_backend_ramdisk_ioctl
129 MALLOC_DEFINE(M_RAMDISK, "ramdisk", "Memory used for CTL RAMdisk");
130 CTL_BACKEND_DECLARE(cbr, ctl_be_ramdisk_driver);
133 ctl_backend_ramdisk_init(void)
135 struct ctl_be_ramdisk_softc *softc;
136 #ifdef CTL_RAMDISK_PAGES
143 memset(softc, 0, sizeof(*softc));
145 mtx_init(&softc->lock, "ramdisk", NULL, MTX_DEF);
147 STAILQ_INIT(&softc->lun_list);
148 softc->rd_size = 4 * 1024 * 1024;
149 #ifdef CTL_RAMDISK_PAGES
150 softc->num_pages = softc->rd_size / PAGE_SIZE;
151 softc->ramdisk_pages = (uint8_t **)malloc(sizeof(uint8_t *) *
152 softc->num_pages, M_RAMDISK,
154 for (i = 0; i < softc->num_pages; i++)
155 softc->ramdisk_pages[i] = malloc(PAGE_SIZE, M_RAMDISK,M_WAITOK);
157 softc->ramdisk_buffer = (uint8_t *)malloc(softc->rd_size, M_RAMDISK,
165 ctl_backend_ramdisk_shutdown(void)
167 struct ctl_be_ramdisk_softc *softc;
168 struct ctl_be_ramdisk_lun *lun, *next_lun;
169 #ifdef CTL_RAMDISK_PAGES
175 mtx_lock(&softc->lock);
176 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
178 * Grab the next LUN. The current LUN may get removed by
179 * ctl_invalidate_lun(), which will call our LUN shutdown
180 * routine, if there is no outstanding I/O for this LUN.
182 next_lun = STAILQ_NEXT(lun, links);
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->ctl_be_lun);
191 ctl_invalidate_lun(&lun->ctl_be_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)
215 struct bintime cur_bt;
218 CTL_DEBUG_PRINT(("ctl_backend_ramdisk_move_done\n"));
219 if ((io->io_hdr.port_status == 0)
220 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
221 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
222 io->io_hdr.status = CTL_SUCCESS;
223 else if ((io->io_hdr.port_status != 0)
224 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
225 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
227 * For hardware error sense keys, the sense key
228 * specific value is defined to be a retry count,
229 * but we use it to pass back an internal FETD
230 * error code. XXX KDM Hopefully the FETD is only
231 * using 16 bits for an error code, since that's
232 * all the space we have in the sks field.
234 ctl_set_internal_failure(&io->scsiio,
237 io->io_hdr.port_status);
241 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
242 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
243 io->io_hdr.num_dmas++;
246 if (io->scsiio.kern_sg_entries > 0)
247 free(io->scsiio.kern_data_ptr, M_RAMDISK);
253 ctl_backend_ramdisk_submit(union ctl_io *io)
255 struct ctl_lba_len lbalen;
256 #ifdef CTL_RAMDISK_PAGES
257 struct ctl_sg_entry *sg_entries;
261 int num_sg_entries, len;
262 struct ctl_be_ramdisk_softc *softc;
263 struct ctl_be_lun *ctl_be_lun;
264 struct ctl_be_ramdisk_lun *be_lun;
268 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
269 CTL_PRIV_BACKEND_LUN].ptr;
270 be_lun = (struct ctl_be_ramdisk_lun *)ctl_be_lun->be_lun;
272 memcpy(&lbalen, io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
275 len = lbalen.len * ctl_be_lun->blocksize;
278 * Kick out the request if it's bigger than we can handle.
280 if (len > softc->rd_size) {
281 ctl_set_internal_failure(&io->scsiio,
285 return (CTL_RETVAL_COMPLETE);
289 * Kick out the request if it's larger than the device size that
290 * the user requested.
292 if (((lbalen.lba * ctl_be_lun->blocksize) + len) > be_lun->size_bytes) {
293 ctl_set_lba_out_of_range(&io->scsiio);
295 return (CTL_RETVAL_COMPLETE);
298 #ifdef CTL_RAMDISK_PAGES
299 num_sg_entries = len >> PAGE_SHIFT;
300 if ((len & (PAGE_SIZE - 1)) != 0)
303 if (num_sg_entries > 1) {
304 io->scsiio.kern_data_ptr = malloc(sizeof(struct ctl_sg_entry) *
305 num_sg_entries, M_RAMDISK,
307 sg_entries = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
308 for (i = 0, len_filled = 0; i < num_sg_entries;
309 i++, len_filled += PAGE_SIZE) {
310 sg_entries[i].addr = softc->ramdisk_pages[i];
311 sg_entries[i].len = ctl_min(PAGE_SIZE,
315 #endif /* CTL_RAMDISK_PAGES */
317 * If this is less than 1 page, don't bother allocating a
318 * scatter/gather list for it. This saves time/overhead.
321 #ifdef CTL_RAMDISK_PAGES
322 io->scsiio.kern_data_ptr = softc->ramdisk_pages[0];
324 io->scsiio.kern_data_ptr = softc->ramdisk_buffer;
326 #ifdef CTL_RAMDISK_PAGES
330 io->scsiio.be_move_done = ctl_backend_ramdisk_move_done;
331 io->scsiio.kern_data_len = len;
332 io->scsiio.kern_total_len = len;
333 io->scsiio.kern_rel_offset = 0;
334 io->scsiio.kern_data_resid = 0;
335 io->scsiio.kern_sg_entries = num_sg_entries;
336 io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
338 getbintime(&io->io_hdr.dma_start_bt);
342 return (CTL_RETVAL_COMPLETE);
346 ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
347 int flag, struct thread *td)
349 struct ctl_be_ramdisk_softc *softc;
357 struct ctl_lun_req *lun_req;
359 lun_req = (struct ctl_lun_req *)addr;
361 switch (lun_req->reqtype) {
362 case CTL_LUNREQ_CREATE:
363 retval = ctl_backend_ramdisk_create(softc, lun_req,
367 retval = ctl_backend_ramdisk_rm(softc, lun_req);
369 case CTL_LUNREQ_MODIFY:
370 retval = ctl_backend_ramdisk_modify(softc, lun_req);
373 lun_req->status = CTL_LUN_ERROR;
374 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
375 "%s: invalid LUN request type %d", __func__,
390 ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
391 struct ctl_lun_req *req)
393 struct ctl_be_ramdisk_lun *be_lun;
394 struct ctl_lun_rm_params *params;
399 params = &req->reqdata.rm;
403 mtx_lock(&softc->lock);
405 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
406 if (be_lun->ctl_be_lun.lun_id == params->lun_id)
409 mtx_unlock(&softc->lock);
411 if (be_lun == NULL) {
412 snprintf(req->error_str, sizeof(req->error_str),
413 "%s: LUN %u is not managed by the ramdisk backend",
414 __func__, params->lun_id);
418 retval = ctl_disable_lun(&be_lun->ctl_be_lun);
421 snprintf(req->error_str, sizeof(req->error_str),
422 "%s: error %d returned from ctl_disable_lun() for "
423 "LUN %d", __func__, retval, params->lun_id);
428 * Set the waiting flag before we invalidate the LUN. Our shutdown
429 * routine can be called any time after we invalidate the LUN,
430 * and can be called from our context.
432 * This tells the shutdown routine that we're waiting, or we're
433 * going to wait for the shutdown to happen.
435 mtx_lock(&softc->lock);
436 be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
437 mtx_unlock(&softc->lock);
439 retval = ctl_invalidate_lun(&be_lun->ctl_be_lun);
441 snprintf(req->error_str, sizeof(req->error_str),
442 "%s: error %d returned from ctl_invalidate_lun() for "
443 "LUN %d", __func__, retval, params->lun_id);
444 mtx_lock(&softc->lock);
445 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
446 mtx_unlock(&softc->lock);
450 mtx_lock(&softc->lock);
452 while ((be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) == 0) {
453 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
457 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
460 * We only remove this LUN from the list and free it (below) if
461 * retval == 0. If the user interrupted the wait, we just bail out
462 * without actually freeing the LUN. We let the shutdown routine
463 * free the LUN if that happens.
466 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
471 mtx_unlock(&softc->lock);
474 free(be_lun, M_RAMDISK);
476 req->status = CTL_LUN_OK;
481 req->status = CTL_LUN_ERROR;
487 ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
488 struct ctl_lun_req *req, int do_wait)
490 struct ctl_be_ramdisk_lun *be_lun;
491 struct ctl_lun_create_params *params;
497 params = &req->reqdata.create;
498 if (params->blocksize_bytes != 0)
499 blocksize = params->blocksize_bytes;
503 be_lun = malloc(sizeof(*be_lun), M_RAMDISK, M_ZERO | (do_wait ?
504 M_WAITOK : M_NOWAIT));
506 if (be_lun == NULL) {
507 snprintf(req->error_str, sizeof(req->error_str),
508 "%s: error allocating %zd bytes", __func__,
513 if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
514 be_lun->ctl_be_lun.lun_type = params->device_type;
516 be_lun->ctl_be_lun.lun_type = T_DIRECT;
518 if (be_lun->ctl_be_lun.lun_type == T_DIRECT) {
520 if (params->lun_size_bytes < blocksize) {
521 snprintf(req->error_str, sizeof(req->error_str),
522 "%s: LUN size %ju < blocksize %u", __func__,
523 params->lun_size_bytes, blocksize);
527 be_lun->size_blocks = params->lun_size_bytes / blocksize;
528 be_lun->size_bytes = be_lun->size_blocks * blocksize;
530 be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1;
532 be_lun->ctl_be_lun.maxlba = 0;
534 be_lun->size_bytes = 0;
535 be_lun->size_blocks = 0;
538 be_lun->ctl_be_lun.blocksize = blocksize;
540 /* Tell the user the blocksize we ended up using */
541 params->blocksize_bytes = blocksize;
543 /* Tell the user the exact size we ended up using */
544 params->lun_size_bytes = be_lun->size_bytes;
546 be_lun->softc = softc;
548 be_lun->flags = CTL_BE_RAMDISK_LUN_UNCONFIGURED;
549 be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY;
550 be_lun->ctl_be_lun.be_lun = be_lun;
552 if (params->flags & CTL_LUN_FLAG_ID_REQ) {
553 be_lun->ctl_be_lun.req_lun_id = params->req_lun_id;
554 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ;
556 be_lun->ctl_be_lun.req_lun_id = 0;
558 be_lun->ctl_be_lun.lun_shutdown = ctl_backend_ramdisk_lun_shutdown;
559 be_lun->ctl_be_lun.lun_config_status =
560 ctl_backend_ramdisk_lun_config_status;
561 be_lun->ctl_be_lun.be = &ctl_be_ramdisk_driver;
562 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
563 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
565 strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr,
566 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
569 /* Tell the user what we used for a serial number */
570 strncpy((char *)params->serial_num, tmpstr,
571 ctl_min(sizeof(params->serial_num), sizeof(tmpstr)));
573 strncpy((char *)be_lun->ctl_be_lun.serial_num,
575 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
576 sizeof(params->serial_num)));
578 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
579 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
580 strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr,
581 ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
584 /* Tell the user what we used for a device ID */
585 strncpy((char *)params->device_id, tmpstr,
586 ctl_min(sizeof(params->device_id), sizeof(tmpstr)));
588 strncpy((char *)be_lun->ctl_be_lun.device_id,
590 ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
591 sizeof(params->device_id)));
594 mtx_lock(&softc->lock);
596 STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
598 mtx_unlock(&softc->lock);
600 retval = ctl_add_lun(&be_lun->ctl_be_lun);
602 mtx_lock(&softc->lock);
603 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
606 mtx_unlock(&softc->lock);
607 snprintf(req->error_str, sizeof(req->error_str),
608 "%s: ctl_add_lun() returned error %d, see dmesg for "
609 "details", __func__, retval);
617 mtx_lock(&softc->lock);
620 * Tell the config_status routine that we're waiting so it won't
621 * clean up the LUN in the event of an error.
623 be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
625 while (be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) {
626 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
630 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
632 if (be_lun->flags & CTL_BE_RAMDISK_LUN_CONFIG_ERR) {
633 snprintf(req->error_str, sizeof(req->error_str),
634 "%s: LUN configuration error, see dmesg for details",
636 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
639 mtx_unlock(&softc->lock);
642 params->req_lun_id = be_lun->ctl_be_lun.lun_id;
644 mtx_unlock(&softc->lock);
646 req->status = CTL_LUN_OK;
651 req->status = CTL_LUN_ERROR;
652 free(be_lun, M_RAMDISK);
658 ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
659 struct ctl_lun_req *req)
661 struct ctl_be_ramdisk_lun *be_lun;
662 struct ctl_lun_modify_params *params;
665 params = &req->reqdata.modify;
669 mtx_lock(&softc->lock);
670 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
671 if (be_lun->ctl_be_lun.lun_id == params->lun_id)
674 mtx_unlock(&softc->lock);
676 if (be_lun == NULL) {
677 snprintf(req->error_str, sizeof(req->error_str),
678 "%s: LUN %u is not managed by the ramdisk backend",
679 __func__, params->lun_id);
683 if (params->lun_size_bytes == 0) {
684 snprintf(req->error_str, sizeof(req->error_str),
685 "%s: LUN size \"auto\" not supported "
686 "by the ramdisk backend", __func__);
690 blocksize = be_lun->ctl_be_lun.blocksize;
692 if (params->lun_size_bytes < blocksize) {
693 snprintf(req->error_str, sizeof(req->error_str),
694 "%s: LUN size %ju < blocksize %u", __func__,
695 params->lun_size_bytes, blocksize);
699 be_lun->size_blocks = params->lun_size_bytes / blocksize;
700 be_lun->size_bytes = be_lun->size_blocks * blocksize;
703 * The maximum LBA is the size - 1.
705 * XXX: Note that this field is being updated without locking,
706 * which might cause problems on 32-bit architectures.
708 be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1;
709 ctl_lun_capacity_changed(&be_lun->ctl_be_lun);
711 /* Tell the user the exact size we ended up using */
712 params->lun_size_bytes = be_lun->size_bytes;
714 req->status = CTL_LUN_OK;
719 req->status = CTL_LUN_ERROR;
725 ctl_backend_ramdisk_lun_shutdown(void *be_lun)
727 struct ctl_be_ramdisk_lun *lun;
728 struct ctl_be_ramdisk_softc *softc;
731 lun = (struct ctl_be_ramdisk_lun *)be_lun;
735 mtx_lock(&softc->lock);
737 lun->flags |= CTL_BE_RAMDISK_LUN_UNCONFIGURED;
739 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
742 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
748 mtx_unlock(&softc->lock);
751 free(be_lun, M_RAMDISK);
755 ctl_backend_ramdisk_lun_config_status(void *be_lun,
756 ctl_lun_config_status status)
758 struct ctl_be_ramdisk_lun *lun;
759 struct ctl_be_ramdisk_softc *softc;
761 lun = (struct ctl_be_ramdisk_lun *)be_lun;
764 if (status == CTL_LUN_CONFIG_OK) {
765 mtx_lock(&softc->lock);
766 lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
767 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING)
769 mtx_unlock(&softc->lock);
772 * We successfully added the LUN, attempt to enable it.
774 if (ctl_enable_lun(&lun->ctl_be_lun) != 0) {
775 printf("%s: ctl_enable_lun() failed!\n", __func__);
776 if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) {
777 printf("%s: ctl_invalidate_lun() failed!\n",
786 mtx_lock(&softc->lock);
787 lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
790 * If we have a user waiting, let him handle the cleanup. If not,
791 * clean things up here.
793 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
794 lun->flags |= CTL_BE_RAMDISK_LUN_CONFIG_ERR;
797 STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
800 free(lun, M_RAMDISK);
802 mtx_unlock(&softc->lock);
806 ctl_backend_ramdisk_config_write(union ctl_io *io)
808 struct ctl_be_ramdisk_softc *softc;
814 switch (io->scsiio.cdb[0]) {
815 case SYNCHRONIZE_CACHE:
816 case SYNCHRONIZE_CACHE_16:
818 * The upper level CTL code will filter out any CDBs with
819 * the immediate bit set and return the proper error. It
820 * will also not allow a sync cache command to go to a LUN
821 * that is powered down.
823 * We don't really need to worry about what LBA range the
824 * user asked to be synced out. When they issue a sync
825 * cache command, we'll sync out the whole thing.
827 * This is obviously just a stubbed out implementation.
828 * The real implementation will be in the RAIDCore/CTL
829 * interface, and can only really happen when RAIDCore
830 * implements a per-array cache sync.
832 ctl_set_success(&io->scsiio);
833 ctl_config_write_done(io);
835 case START_STOP_UNIT: {
836 struct scsi_start_stop_unit *cdb;
837 struct ctl_be_lun *ctl_be_lun;
838 struct ctl_be_ramdisk_lun *be_lun;
840 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
842 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
843 CTL_PRIV_BACKEND_LUN].ptr;
844 be_lun = (struct ctl_be_ramdisk_lun *)ctl_be_lun->be_lun;
846 if (cdb->how & SSS_START)
847 retval = ctl_start_lun(ctl_be_lun);
849 retval = ctl_stop_lun(ctl_be_lun);
852 && (cdb->byte2 & SSS_ONOFFLINE))
853 retval = ctl_lun_offline(ctl_be_lun);
858 * In general, the above routines should not fail. They
859 * just set state for the LUN. So we've got something
860 * pretty wrong here if we can't start or stop the LUN.
863 ctl_set_internal_failure(&io->scsiio,
865 /*retry_count*/ 0xf051);
866 retval = CTL_RETVAL_COMPLETE;
868 ctl_set_success(&io->scsiio);
870 ctl_config_write_done(io);
874 ctl_set_invalid_opcode(&io->scsiio);
875 ctl_config_write_done(io);
876 retval = CTL_RETVAL_COMPLETE;
884 ctl_backend_ramdisk_config_read(union ctl_io *io)
887 * XXX KDM need to implement!!