1229997Sken/*-
2229997Sken * Copyright (c) 2003, 2008 Silicon Graphics International Corp.
3232604Strasz * Copyright (c) 2012 The FreeBSD Foundation
4288348Smav * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org>
5229997Sken * All rights reserved.
6229997Sken *
7232604Strasz * Portions of this software were developed by Edward Tomasz Napierala
8232604Strasz * under sponsorship from the FreeBSD Foundation.
9232604Strasz *
10229997Sken * Redistribution and use in source and binary forms, with or without
11229997Sken * modification, are permitted provided that the following conditions
12229997Sken * are met:
13229997Sken * 1. Redistributions of source code must retain the above copyright
14229997Sken *    notice, this list of conditions, and the following disclaimer,
15229997Sken *    without modification.
16229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17229997Sken *    substantially similar to the "NO WARRANTY" disclaimer below
18229997Sken *    ("Disclaimer") and any redistribution must be conditioned upon
19229997Sken *    including a substantially similar Disclaimer requirement for further
20229997Sken *    binary redistribution.
21229997Sken *
22229997Sken * NO WARRANTY
23229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33229997Sken * POSSIBILITY OF SUCH DAMAGES.
34229997Sken *
35229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_ramdisk.c#3 $
36229997Sken */
37229997Sken/*
38229997Sken * CAM Target Layer backend for a "fake" ramdisk.
39229997Sken *
40229997Sken * Author: Ken Merry <ken@FreeBSD.org>
41229997Sken */
42229997Sken
43229997Sken#include <sys/cdefs.h>
44229997Sken__FBSDID("$FreeBSD: releng/11.0/sys/cam/ctl/ctl_backend_ramdisk.c 288427 2015-09-30 20:38:35Z mav $");
45229997Sken
46229997Sken#include <sys/param.h>
47229997Sken#include <sys/systm.h>
48229997Sken#include <sys/kernel.h>
49229997Sken#include <sys/condvar.h>
50229997Sken#include <sys/types.h>
51229997Sken#include <sys/lock.h>
52229997Sken#include <sys/mutex.h>
53229997Sken#include <sys/malloc.h>
54264886Smav#include <sys/taskqueue.h>
55229997Sken#include <sys/time.h>
56229997Sken#include <sys/queue.h>
57229997Sken#include <sys/conf.h>
58229997Sken#include <sys/ioccom.h>
59229997Sken#include <sys/module.h>
60287621Smav#include <sys/sysctl.h>
61229997Sken
62229997Sken#include <cam/scsi/scsi_all.h>
63287621Smav#include <cam/scsi/scsi_da.h>
64229997Sken#include <cam/ctl/ctl_io.h>
65229997Sken#include <cam/ctl/ctl.h>
66229997Sken#include <cam/ctl/ctl_util.h>
67229997Sken#include <cam/ctl/ctl_backend.h>
68229997Sken#include <cam/ctl/ctl_debug.h>
69229997Sken#include <cam/ctl/ctl_ioctl.h>
70287621Smav#include <cam/ctl/ctl_ha.h>
71287621Smav#include <cam/ctl/ctl_private.h>
72229997Sken#include <cam/ctl/ctl_error.h>
73229997Sken
74229997Skentypedef enum {
75229997Sken	CTL_BE_RAMDISK_LUN_UNCONFIGURED	= 0x01,
76229997Sken	CTL_BE_RAMDISK_LUN_CONFIG_ERR	= 0x02,
77229997Sken	CTL_BE_RAMDISK_LUN_WAITING	= 0x04
78229997Sken} ctl_be_ramdisk_lun_flags;
79229997Sken
80229997Skenstruct ctl_be_ramdisk_lun {
81287500Smav	struct ctl_lun_create_params params;
82264886Smav	char lunname[32];
83229997Sken	uint64_t size_bytes;
84229997Sken	uint64_t size_blocks;
85229997Sken	struct ctl_be_ramdisk_softc *softc;
86229997Sken	ctl_be_ramdisk_lun_flags flags;
87229997Sken	STAILQ_ENTRY(ctl_be_ramdisk_lun) links;
88287499Smav	struct ctl_be_lun cbe_lun;
89264886Smav	struct taskqueue *io_taskqueue;
90264886Smav	struct task io_task;
91264886Smav	STAILQ_HEAD(, ctl_io_hdr) cont_queue;
92267877Smav	struct mtx_padalign queue_lock;
93229997Sken};
94229997Sken
95229997Skenstruct ctl_be_ramdisk_softc {
96229997Sken	struct mtx lock;
97229997Sken	int rd_size;
98229997Sken#ifdef CTL_RAMDISK_PAGES
99229997Sken	uint8_t **ramdisk_pages;
100229997Sken	int num_pages;
101229997Sken#else
102229997Sken	uint8_t *ramdisk_buffer;
103229997Sken#endif
104229997Sken	int num_luns;
105229997Sken	STAILQ_HEAD(, ctl_be_ramdisk_lun) lun_list;
106229997Sken};
107229997Sken
108229997Skenstatic struct ctl_be_ramdisk_softc rd_softc;
109287621Smavextern struct ctl_softc *control_softc;
110229997Sken
111229997Skenint ctl_backend_ramdisk_init(void);
112229997Skenvoid ctl_backend_ramdisk_shutdown(void);
113229997Skenstatic int ctl_backend_ramdisk_move_done(union ctl_io *io);
114229997Skenstatic int ctl_backend_ramdisk_submit(union ctl_io *io);
115264886Smavstatic void ctl_backend_ramdisk_continue(union ctl_io *io);
116229997Skenstatic int ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd,
117229997Sken				     caddr_t addr, int flag, struct thread *td);
118229997Skenstatic int ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
119229997Sken				  struct ctl_lun_req *req);
120229997Skenstatic int ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
121287499Smav				      struct ctl_lun_req *req);
122232604Straszstatic int ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
123232604Strasz				  struct ctl_lun_req *req);
124264886Smavstatic void ctl_backend_ramdisk_worker(void *context, int pending);
125229997Skenstatic void ctl_backend_ramdisk_lun_shutdown(void *be_lun);
126229997Skenstatic void ctl_backend_ramdisk_lun_config_status(void *be_lun,
127229997Sken						  ctl_lun_config_status status);
128229997Skenstatic int ctl_backend_ramdisk_config_write(union ctl_io *io);
129229997Skenstatic int ctl_backend_ramdisk_config_read(union ctl_io *io);
130229997Sken
131229997Skenstatic struct ctl_backend_driver ctl_be_ramdisk_driver =
132229997Sken{
133230334Sken	.name = "ramdisk",
134230334Sken	.flags = CTL_BE_FLAG_HAS_CONFIG,
135230334Sken	.init = ctl_backend_ramdisk_init,
136230334Sken	.data_submit = ctl_backend_ramdisk_submit,
137230334Sken	.data_move_done = ctl_backend_ramdisk_move_done,
138230334Sken	.config_read = ctl_backend_ramdisk_config_read,
139230334Sken	.config_write = ctl_backend_ramdisk_config_write,
140230334Sken	.ioctl = ctl_backend_ramdisk_ioctl
141229997Sken};
142229997Sken
143229997SkenMALLOC_DEFINE(M_RAMDISK, "ramdisk", "Memory used for CTL RAMdisk");
144229997SkenCTL_BACKEND_DECLARE(cbr, ctl_be_ramdisk_driver);
145229997Sken
146229997Skenint
147229997Skenctl_backend_ramdisk_init(void)
148229997Sken{
149288220Smav	struct ctl_be_ramdisk_softc *softc = &rd_softc;
150229997Sken#ifdef CTL_RAMDISK_PAGES
151240993Strasz	int i;
152229997Sken#endif
153229997Sken
154229997Sken	memset(softc, 0, sizeof(*softc));
155267877Smav	mtx_init(&softc->lock, "ctlramdisk", NULL, MTX_DEF);
156229997Sken	STAILQ_INIT(&softc->lun_list);
157264886Smav	softc->rd_size = 1024 * 1024;
158229997Sken#ifdef CTL_RAMDISK_PAGES
159229997Sken	softc->num_pages = softc->rd_size / PAGE_SIZE;
160229997Sken	softc->ramdisk_pages = (uint8_t **)malloc(sizeof(uint8_t *) *
161229997Sken						  softc->num_pages, M_RAMDISK,
162229997Sken						  M_WAITOK);
163240993Strasz	for (i = 0; i < softc->num_pages; i++)
164229997Sken		softc->ramdisk_pages[i] = malloc(PAGE_SIZE, M_RAMDISK,M_WAITOK);
165229997Sken#else
166229997Sken	softc->ramdisk_buffer = (uint8_t *)malloc(softc->rd_size, M_RAMDISK,
167229997Sken						  M_WAITOK);
168229997Sken#endif
169229997Sken
170229997Sken	return (0);
171229997Sken}
172229997Sken
173229997Skenvoid
174229997Skenctl_backend_ramdisk_shutdown(void)
175229997Sken{
176288220Smav	struct ctl_be_ramdisk_softc *softc = &rd_softc;
177229997Sken	struct ctl_be_ramdisk_lun *lun, *next_lun;
178229997Sken#ifdef CTL_RAMDISK_PAGES
179229997Sken	int i;
180229997Sken#endif
181229997Sken
182229997Sken	mtx_lock(&softc->lock);
183288427Smav	STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun) {
184229997Sken		/*
185229997Sken		 * Drop our lock here.  Since ctl_invalidate_lun() can call
186229997Sken		 * back into us, this could potentially lead to a recursive
187229997Sken		 * lock of the same mutex, which would cause a hang.
188229997Sken		 */
189229997Sken		mtx_unlock(&softc->lock);
190287499Smav		ctl_disable_lun(&lun->cbe_lun);
191287499Smav		ctl_invalidate_lun(&lun->cbe_lun);
192229997Sken		mtx_lock(&softc->lock);
193229997Sken	}
194229997Sken	mtx_unlock(&softc->lock);
195229997Sken
196229997Sken#ifdef CTL_RAMDISK_PAGES
197229997Sken	for (i = 0; i < softc->num_pages; i++)
198229997Sken		free(softc->ramdisk_pages[i], M_RAMDISK);
199229997Sken
200229997Sken	free(softc->ramdisk_pages, M_RAMDISK);
201229997Sken#else
202229997Sken	free(softc->ramdisk_buffer, M_RAMDISK);
203229997Sken#endif
204229997Sken
205229997Sken	if (ctl_backend_deregister(&ctl_be_ramdisk_driver) != 0) {
206229997Sken		printf("ctl_backend_ramdisk_shutdown: "
207229997Sken		       "ctl_backend_deregister() failed!\n");
208229997Sken	}
209229997Sken}
210229997Sken
211229997Skenstatic int
212229997Skenctl_backend_ramdisk_move_done(union ctl_io *io)
213229997Sken{
214287499Smav	struct ctl_be_lun *cbe_lun;
215264886Smav	struct ctl_be_ramdisk_lun *be_lun;
216229997Sken#ifdef CTL_TIME_IO
217229997Sken	struct bintime cur_bt;
218229997Sken#endif
219229997Sken
220229997Sken	CTL_DEBUG_PRINT(("ctl_backend_ramdisk_move_done\n"));
221287499Smav	cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
222264886Smav		CTL_PRIV_BACKEND_LUN].ptr;
223287499Smav	be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun->be_lun;
224264886Smav#ifdef CTL_TIME_IO
225288215Smav	getbinuptime(&cur_bt);
226264886Smav	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
227264886Smav	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
228288215Smav#endif
229264886Smav	io->io_hdr.num_dmas++;
230264886Smav	if (io->scsiio.kern_sg_entries > 0)
231264886Smav		free(io->scsiio.kern_data_ptr, M_RAMDISK);
232264886Smav	io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
233275058Smav	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
234275058Smav		;
235275058Smav	} else if ((io->io_hdr.port_status == 0) &&
236275058Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
237267519Smav		if (io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer > 0) {
238267877Smav			mtx_lock(&be_lun->queue_lock);
239264886Smav			STAILQ_INSERT_TAIL(&be_lun->cont_queue,
240264886Smav			    &io->io_hdr, links);
241267877Smav			mtx_unlock(&be_lun->queue_lock);
242264886Smav			taskqueue_enqueue(be_lun->io_taskqueue,
243264886Smav			    &be_lun->io_task);
244264886Smav			return (0);
245264886Smav		}
246275009Smav		ctl_set_success(&io->scsiio);
247275058Smav	} else if ((io->io_hdr.port_status != 0) &&
248275058Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
249275058Smav	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
250229997Sken		/*
251229997Sken		 * For hardware error sense keys, the sense key
252229997Sken		 * specific value is defined to be a retry count,
253229997Sken		 * but we use it to pass back an internal FETD
254229997Sken		 * error code.  XXX KDM  Hopefully the FETD is only
255229997Sken		 * using 16 bits for an error code, since that's
256229997Sken		 * all the space we have in the sks field.
257229997Sken		 */
258229997Sken		ctl_set_internal_failure(&io->scsiio,
259229997Sken					 /*sks_valid*/ 1,
260229997Sken					 /*retry_count*/
261229997Sken					 io->io_hdr.port_status);
262229997Sken	}
263267537Smav	ctl_data_submit_done(io);
264229997Sken	return(0);
265229997Sken}
266229997Sken
267229997Skenstatic int
268229997Skenctl_backend_ramdisk_submit(union ctl_io *io)
269229997Sken{
270287499Smav	struct ctl_be_lun *cbe_lun;
271267537Smav	struct ctl_lba_len_flags *lbalen;
272229997Sken
273287499Smav	cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
274267519Smav		CTL_PRIV_BACKEND_LUN].ptr;
275267537Smav	lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
276267537Smav	if (lbalen->flags & CTL_LLF_VERIFY) {
277267537Smav		ctl_set_success(&io->scsiio);
278267537Smav		ctl_data_submit_done(io);
279267537Smav		return (CTL_RETVAL_COMPLETE);
280267537Smav	}
281267519Smav	io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer =
282287499Smav	    lbalen->len * cbe_lun->blocksize;
283264886Smav	ctl_backend_ramdisk_continue(io);
284264886Smav	return (CTL_RETVAL_COMPLETE);
285264886Smav}
286229997Sken
287264886Smavstatic void
288264886Smavctl_backend_ramdisk_continue(union ctl_io *io)
289264886Smav{
290264886Smav	struct ctl_be_ramdisk_softc *softc;
291264886Smav	int len, len_filled, sg_filled;
292264886Smav#ifdef CTL_RAMDISK_PAGES
293264886Smav	struct ctl_sg_entry *sg_entries;
294264886Smav	int i;
295264886Smav#endif
296229997Sken
297264886Smav	softc = &rd_softc;
298267519Smav	len = io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer;
299229997Sken#ifdef CTL_RAMDISK_PAGES
300264886Smav	sg_filled = min(btoc(len), softc->num_pages);
301264886Smav	if (sg_filled > 1) {
302229997Sken		io->scsiio.kern_data_ptr = malloc(sizeof(struct ctl_sg_entry) *
303264886Smav						  sg_filled, M_RAMDISK,
304229997Sken						  M_WAITOK);
305229997Sken		sg_entries = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
306264886Smav		for (i = 0, len_filled = 0; i < sg_filled; i++) {
307229997Sken			sg_entries[i].addr = softc->ramdisk_pages[i];
308275953Smav			sg_entries[i].len = MIN(PAGE_SIZE, len - len_filled);
309264886Smav			len_filled += sg_entries[i].len;
310229997Sken		}
311229997Sken	} else {
312264886Smav		sg_filled = 0;
313264886Smav		len_filled = len;
314229997Sken		io->scsiio.kern_data_ptr = softc->ramdisk_pages[0];
315264886Smav	}
316229997Sken#else
317264886Smav	sg_filled = 0;
318264886Smav	len_filled = min(len, softc->rd_size);
319264886Smav	io->scsiio.kern_data_ptr = softc->ramdisk_buffer;
320264886Smav#endif /* CTL_RAMDISK_PAGES */
321229997Sken
322267514Smav	io->scsiio.be_move_done = ctl_backend_ramdisk_move_done;
323267514Smav	io->scsiio.kern_data_resid = 0;
324264886Smav	io->scsiio.kern_data_len = len_filled;
325264886Smav	io->scsiio.kern_sg_entries = sg_filled;
326264886Smav	io->io_hdr.flags |= CTL_FLAG_ALLOCATED;
327267519Smav	io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer -= len_filled;
328229997Sken#ifdef CTL_TIME_IO
329288215Smav	getbinuptime(&io->io_hdr.dma_start_bt);
330229997Sken#endif
331229997Sken	ctl_datamove(io);
332264886Smav}
333229997Sken
334264886Smavstatic void
335264886Smavctl_backend_ramdisk_worker(void *context, int pending)
336264886Smav{
337264886Smav	struct ctl_be_ramdisk_lun *be_lun;
338264886Smav	union ctl_io *io;
339264886Smav
340264886Smav	be_lun = (struct ctl_be_ramdisk_lun *)context;
341264886Smav
342267877Smav	mtx_lock(&be_lun->queue_lock);
343264886Smav	for (;;) {
344264886Smav		io = (union ctl_io *)STAILQ_FIRST(&be_lun->cont_queue);
345264886Smav		if (io != NULL) {
346264886Smav			STAILQ_REMOVE(&be_lun->cont_queue, &io->io_hdr,
347264886Smav				      ctl_io_hdr, links);
348267877Smav			mtx_unlock(&be_lun->queue_lock);
349264886Smav			ctl_backend_ramdisk_continue(io);
350267877Smav			mtx_lock(&be_lun->queue_lock);
351264886Smav			continue;
352264886Smav		}
353264886Smav
354264886Smav		/*
355264886Smav		 * If we get here, there is no work left in the queues, so
356264886Smav		 * just break out and let the task queue go to sleep.
357264886Smav		 */
358264886Smav		break;
359264886Smav	}
360267877Smav	mtx_unlock(&be_lun->queue_lock);
361229997Sken}
362229997Sken
363229997Skenstatic int
364229997Skenctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
365229997Sken			  int flag, struct thread *td)
366229997Sken{
367288220Smav	struct ctl_be_ramdisk_softc *softc = &rd_softc;
368288220Smav	struct ctl_lun_req *lun_req;
369229997Sken	int retval;
370229997Sken
371229997Sken	retval = 0;
372229997Sken	switch (cmd) {
373288220Smav	case CTL_LUN_REQ:
374229997Sken		lun_req = (struct ctl_lun_req *)addr;
375229997Sken		switch (lun_req->reqtype) {
376229997Sken		case CTL_LUNREQ_CREATE:
377287499Smav			retval = ctl_backend_ramdisk_create(softc, lun_req);
378229997Sken			break;
379229997Sken		case CTL_LUNREQ_RM:
380229997Sken			retval = ctl_backend_ramdisk_rm(softc, lun_req);
381229997Sken			break;
382232604Strasz		case CTL_LUNREQ_MODIFY:
383232604Strasz			retval = ctl_backend_ramdisk_modify(softc, lun_req);
384232604Strasz			break;
385229997Sken		default:
386229997Sken			lun_req->status = CTL_LUN_ERROR;
387229997Sken			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
388229997Sken				 "%s: invalid LUN request type %d", __func__,
389229997Sken				 lun_req->reqtype);
390229997Sken			break;
391229997Sken		}
392229997Sken		break;
393229997Sken	default:
394229997Sken		retval = ENOTTY;
395229997Sken		break;
396229997Sken	}
397229997Sken
398229997Sken	return (retval);
399229997Sken}
400229997Sken
401229997Skenstatic int
402229997Skenctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
403229997Sken		       struct ctl_lun_req *req)
404229997Sken{
405229997Sken	struct ctl_be_ramdisk_lun *be_lun;
406229997Sken	struct ctl_lun_rm_params *params;
407229997Sken	int retval;
408229997Sken
409229997Sken	params = &req->reqdata.rm;
410229997Sken	mtx_lock(&softc->lock);
411229997Sken	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
412287499Smav		if (be_lun->cbe_lun.lun_id == params->lun_id)
413229997Sken			break;
414229997Sken	}
415229997Sken	mtx_unlock(&softc->lock);
416229997Sken	if (be_lun == NULL) {
417229997Sken		snprintf(req->error_str, sizeof(req->error_str),
418229997Sken			 "%s: LUN %u is not managed by the ramdisk backend",
419229997Sken			 __func__, params->lun_id);
420229997Sken		goto bailout_error;
421229997Sken	}
422229997Sken
423287499Smav	retval = ctl_disable_lun(&be_lun->cbe_lun);
424229997Sken	if (retval != 0) {
425229997Sken		snprintf(req->error_str, sizeof(req->error_str),
426229997Sken			 "%s: error %d returned from ctl_disable_lun() for "
427229997Sken			 "LUN %d", __func__, retval, params->lun_id);
428229997Sken		goto bailout_error;
429229997Sken	}
430229997Sken
431229997Sken	/*
432229997Sken	 * Set the waiting flag before we invalidate the LUN.  Our shutdown
433229997Sken	 * routine can be called any time after we invalidate the LUN,
434229997Sken	 * and can be called from our context.
435229997Sken	 *
436229997Sken	 * This tells the shutdown routine that we're waiting, or we're
437229997Sken	 * going to wait for the shutdown to happen.
438229997Sken	 */
439229997Sken	mtx_lock(&softc->lock);
440229997Sken	be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
441229997Sken	mtx_unlock(&softc->lock);
442229997Sken
443287499Smav	retval = ctl_invalidate_lun(&be_lun->cbe_lun);
444229997Sken	if (retval != 0) {
445229997Sken		snprintf(req->error_str, sizeof(req->error_str),
446229997Sken			 "%s: error %d returned from ctl_invalidate_lun() for "
447229997Sken			 "LUN %d", __func__, retval, params->lun_id);
448252569Smav		mtx_lock(&softc->lock);
449252569Smav		be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
450252569Smav		mtx_unlock(&softc->lock);
451229997Sken		goto bailout_error;
452229997Sken	}
453229997Sken
454229997Sken	mtx_lock(&softc->lock);
455229997Sken	while ((be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) == 0) {
456229997Sken		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
457288220Smav		if (retval == EINTR)
458229997Sken			break;
459229997Sken	}
460229997Sken	be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
461229997Sken
462229997Sken	/*
463229997Sken	 * We only remove this LUN from the list and free it (below) if
464229997Sken	 * retval == 0.  If the user interrupted the wait, we just bail out
465229997Sken	 * without actually freeing the LUN.  We let the shutdown routine
466229997Sken	 * free the LUN if that happens.
467229997Sken	 */
468229997Sken	if (retval == 0) {
469229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
470229997Sken			      links);
471229997Sken		softc->num_luns--;
472229997Sken	}
473229997Sken
474229997Sken	mtx_unlock(&softc->lock);
475229997Sken
476264886Smav	if (retval == 0) {
477287670Smav		taskqueue_drain_all(be_lun->io_taskqueue);
478264886Smav		taskqueue_free(be_lun->io_taskqueue);
479287499Smav		ctl_free_opts(&be_lun->cbe_lun.options);
480267877Smav		mtx_destroy(&be_lun->queue_lock);
481229997Sken		free(be_lun, M_RAMDISK);
482264886Smav	}
483229997Sken
484229997Sken	req->status = CTL_LUN_OK;
485229997Sken	return (retval);
486229997Sken
487229997Skenbailout_error:
488229997Sken	req->status = CTL_LUN_ERROR;
489229997Sken	return (0);
490229997Sken}
491229997Sken
492229997Skenstatic int
493229997Skenctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
494287499Smav			   struct ctl_lun_req *req)
495229997Sken{
496229997Sken	struct ctl_be_ramdisk_lun *be_lun;
497287499Smav	struct ctl_be_lun *cbe_lun;
498229997Sken	struct ctl_lun_create_params *params;
499267481Smav	char *value;
500229997Sken	char tmpstr[32];
501287499Smav	int retval;
502229997Sken
503229997Sken	retval = 0;
504229997Sken	params = &req->reqdata.create;
505229997Sken
506287499Smav	be_lun = malloc(sizeof(*be_lun), M_RAMDISK, M_ZERO | M_WAITOK);
507287499Smav	cbe_lun = &be_lun->cbe_lun;
508287499Smav	cbe_lun->be_lun = be_lun;
509287500Smav	be_lun->params = req->reqdata.create;
510287499Smav	be_lun->softc = softc;
511264886Smav	sprintf(be_lun->lunname, "cram%d", softc->num_luns);
512287499Smav	ctl_init_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args);
513229997Sken
514229997Sken	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
515287499Smav		cbe_lun->lun_type = params->device_type;
516229997Sken	else
517287499Smav		cbe_lun->lun_type = T_DIRECT;
518287499Smav	be_lun->flags = CTL_BE_RAMDISK_LUN_UNCONFIGURED;
519287621Smav	cbe_lun->flags = 0;
520287621Smav	value = ctl_get_opt(&cbe_lun->options, "ha_role");
521287621Smav	if (value != NULL) {
522287621Smav		if (strcmp(value, "primary") == 0)
523287621Smav			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
524287621Smav	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
525287621Smav		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
526229997Sken
527288310Smav	if (cbe_lun->lun_type == T_DIRECT ||
528288310Smav	    cbe_lun->lun_type == T_CDROM) {
529287499Smav		if (params->blocksize_bytes != 0)
530287499Smav			cbe_lun->blocksize = params->blocksize_bytes;
531288310Smav		else if (cbe_lun->lun_type == T_CDROM)
532288310Smav			cbe_lun->blocksize = 2048;
533287499Smav		else
534287499Smav			cbe_lun->blocksize = 512;
535287499Smav		if (params->lun_size_bytes < cbe_lun->blocksize) {
536229997Sken			snprintf(req->error_str, sizeof(req->error_str),
537229997Sken				 "%s: LUN size %ju < blocksize %u", __func__,
538287499Smav				 params->lun_size_bytes, cbe_lun->blocksize);
539229997Sken			goto bailout_error;
540229997Sken		}
541287499Smav		be_lun->size_blocks = params->lun_size_bytes / cbe_lun->blocksize;
542287499Smav		be_lun->size_bytes = be_lun->size_blocks * cbe_lun->blocksize;
543287499Smav		cbe_lun->maxlba = be_lun->size_blocks - 1;
544287499Smav		cbe_lun->atomicblock = UINT32_MAX;
545287499Smav		cbe_lun->opttxferlen = softc->rd_size / cbe_lun->blocksize;
546229997Sken	}
547229997Sken
548229997Sken	/* Tell the user the blocksize we ended up using */
549287499Smav	params->blocksize_bytes = cbe_lun->blocksize;
550229997Sken	params->lun_size_bytes = be_lun->size_bytes;
551229997Sken
552287499Smav	value = ctl_get_opt(&cbe_lun->options, "unmap");
553267481Smav	if (value != NULL && strcmp(value, "on") == 0)
554287499Smav		cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
555287499Smav	value = ctl_get_opt(&cbe_lun->options, "readonly");
556288310Smav	if (value != NULL) {
557288310Smav		if (strcmp(value, "on") == 0)
558288310Smav			cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
559288310Smav	} else if (cbe_lun->lun_type != T_DIRECT)
560287499Smav		cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
561287499Smav	cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
562287499Smav	value = ctl_get_opt(&cbe_lun->options, "serseq");
563287499Smav	if (value != NULL && strcmp(value, "on") == 0)
564287499Smav		cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
565287499Smav	else if (value != NULL && strcmp(value, "read") == 0)
566287499Smav		cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
567287499Smav	else if (value != NULL && strcmp(value, "off") == 0)
568287499Smav		cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
569254759Strasz
570229997Sken	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
571287499Smav		cbe_lun->req_lun_id = params->req_lun_id;
572287499Smav		cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
573229997Sken	} else
574287499Smav		cbe_lun->req_lun_id = 0;
575229997Sken
576287499Smav	cbe_lun->lun_shutdown = ctl_backend_ramdisk_lun_shutdown;
577287499Smav	cbe_lun->lun_config_status = ctl_backend_ramdisk_lun_config_status;
578287499Smav	cbe_lun->be = &ctl_be_ramdisk_driver;
579229997Sken	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
580229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
581229997Sken			 softc->num_luns);
582287499Smav		strncpy((char *)cbe_lun->serial_num, tmpstr,
583287499Smav			MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
584229997Sken
585229997Sken		/* Tell the user what we used for a serial number */
586229997Sken		strncpy((char *)params->serial_num, tmpstr,
587275953Smav			MIN(sizeof(params->serial_num), sizeof(tmpstr)));
588229997Sken	} else {
589287499Smav		strncpy((char *)cbe_lun->serial_num, params->serial_num,
590287499Smav			MIN(sizeof(cbe_lun->serial_num),
591275953Smav			    sizeof(params->serial_num)));
592229997Sken	}
593229997Sken	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
594229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
595287499Smav		strncpy((char *)cbe_lun->device_id, tmpstr,
596287499Smav			MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
597229997Sken
598229997Sken		/* Tell the user what we used for a device ID */
599229997Sken		strncpy((char *)params->device_id, tmpstr,
600275953Smav			MIN(sizeof(params->device_id), sizeof(tmpstr)));
601229997Sken	} else {
602287499Smav		strncpy((char *)cbe_lun->device_id, params->device_id,
603287499Smav			MIN(sizeof(cbe_lun->device_id),
604275953Smav			    sizeof(params->device_id)));
605229997Sken	}
606229997Sken
607264886Smav	STAILQ_INIT(&be_lun->cont_queue);
608267877Smav	mtx_init(&be_lun->queue_lock, "cram queue lock", NULL, MTX_DEF);
609264886Smav	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_backend_ramdisk_worker,
610264886Smav	    be_lun);
611264886Smav
612264886Smav	be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
613264886Smav	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
614264886Smav	if (be_lun->io_taskqueue == NULL) {
615264886Smav		snprintf(req->error_str, sizeof(req->error_str),
616264886Smav			 "%s: Unable to create taskqueue", __func__);
617264886Smav		goto bailout_error;
618264886Smav	}
619264886Smav
620264886Smav	retval = taskqueue_start_threads(&be_lun->io_taskqueue,
621264886Smav					 /*num threads*/1,
622264886Smav					 /*priority*/PWAIT,
623264886Smav					 /*thread name*/
624264886Smav					 "%s taskq", be_lun->lunname);
625264886Smav	if (retval != 0)
626264886Smav		goto bailout_error;
627264886Smav
628229997Sken	mtx_lock(&softc->lock);
629229997Sken	softc->num_luns++;
630229997Sken	STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
631229997Sken	mtx_unlock(&softc->lock);
632229997Sken
633287499Smav	retval = ctl_add_lun(&be_lun->cbe_lun);
634229997Sken	if (retval != 0) {
635229997Sken		mtx_lock(&softc->lock);
636229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
637229997Sken			      links);
638229997Sken		softc->num_luns--;
639229997Sken		mtx_unlock(&softc->lock);
640229997Sken		snprintf(req->error_str, sizeof(req->error_str),
641229997Sken			 "%s: ctl_add_lun() returned error %d, see dmesg for "
642229997Sken			"details", __func__, retval);
643229997Sken		retval = 0;
644229997Sken		goto bailout_error;
645229997Sken	}
646229997Sken
647229997Sken	mtx_lock(&softc->lock);
648229997Sken
649229997Sken	/*
650229997Sken	 * Tell the config_status routine that we're waiting so it won't
651229997Sken	 * clean up the LUN in the event of an error.
652229997Sken	 */
653229997Sken	be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
654229997Sken
655229997Sken	while (be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) {
656229997Sken		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
657229997Sken		if (retval == EINTR)
658229997Sken			break;
659229997Sken	}
660229997Sken	be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
661229997Sken
662229997Sken	if (be_lun->flags & CTL_BE_RAMDISK_LUN_CONFIG_ERR) {
663229997Sken		snprintf(req->error_str, sizeof(req->error_str),
664229997Sken			 "%s: LUN configuration error, see dmesg for details",
665229997Sken			 __func__);
666229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
667229997Sken			      links);
668229997Sken		softc->num_luns--;
669229997Sken		mtx_unlock(&softc->lock);
670229997Sken		goto bailout_error;
671229997Sken	} else {
672287499Smav		params->req_lun_id = cbe_lun->lun_id;
673229997Sken	}
674229997Sken	mtx_unlock(&softc->lock);
675229997Sken
676229997Sken	req->status = CTL_LUN_OK;
677229997Sken	return (retval);
678229997Sken
679229997Skenbailout_error:
680229997Sken	req->status = CTL_LUN_ERROR;
681264886Smav	if (be_lun != NULL) {
682264886Smav		if (be_lun->io_taskqueue != NULL) {
683264886Smav			taskqueue_free(be_lun->io_taskqueue);
684264886Smav		}
685287499Smav		ctl_free_opts(&cbe_lun->options);
686267877Smav		mtx_destroy(&be_lun->queue_lock);
687264886Smav		free(be_lun, M_RAMDISK);
688264886Smav	}
689229997Sken	return (retval);
690229997Sken}
691229997Sken
692232604Straszstatic int
693232604Straszctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
694232604Strasz		       struct ctl_lun_req *req)
695232604Strasz{
696232604Strasz	struct ctl_be_ramdisk_lun *be_lun;
697287500Smav	struct ctl_be_lun *cbe_lun;
698232604Strasz	struct ctl_lun_modify_params *params;
699287621Smav	char *value;
700232604Strasz	uint32_t blocksize;
701287621Smav	int wasprim;
702232604Strasz
703232604Strasz	params = &req->reqdata.modify;
704232604Strasz
705232604Strasz	mtx_lock(&softc->lock);
706232604Strasz	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
707287499Smav		if (be_lun->cbe_lun.lun_id == params->lun_id)
708232604Strasz			break;
709232604Strasz	}
710232604Strasz	mtx_unlock(&softc->lock);
711232604Strasz	if (be_lun == NULL) {
712232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
713232604Strasz			 "%s: LUN %u is not managed by the ramdisk backend",
714232604Strasz			 __func__, params->lun_id);
715232604Strasz		goto bailout_error;
716232604Strasz	}
717287500Smav	cbe_lun = &be_lun->cbe_lun;
718232604Strasz
719287500Smav	if (params->lun_size_bytes != 0)
720287500Smav		be_lun->params.lun_size_bytes = params->lun_size_bytes;
721287500Smav	ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args);
722287621Smav
723287621Smav	wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
724287621Smav	value = ctl_get_opt(&cbe_lun->options, "ha_role");
725287621Smav	if (value != NULL) {
726287621Smav		if (strcmp(value, "primary") == 0)
727287621Smav			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
728287621Smav		else
729287621Smav			cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
730287621Smav	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
731287621Smav		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
732287621Smav	else
733287621Smav		cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
734287621Smav	if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
735287621Smav		if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
736287621Smav			ctl_lun_primary(cbe_lun);
737287621Smav		else
738287621Smav			ctl_lun_secondary(cbe_lun);
739287621Smav	}
740287621Smav
741287499Smav	blocksize = be_lun->cbe_lun.blocksize;
742287500Smav	if (be_lun->params.lun_size_bytes < blocksize) {
743232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
744232604Strasz			"%s: LUN size %ju < blocksize %u", __func__,
745287500Smav			be_lun->params.lun_size_bytes, blocksize);
746232604Strasz		goto bailout_error;
747232604Strasz	}
748287500Smav	be_lun->size_blocks = be_lun->params.lun_size_bytes / blocksize;
749232604Strasz	be_lun->size_bytes = be_lun->size_blocks * blocksize;
750287499Smav	be_lun->cbe_lun.maxlba = be_lun->size_blocks - 1;
751287499Smav	ctl_lun_capacity_changed(&be_lun->cbe_lun);
752232604Strasz
753232604Strasz	/* Tell the user the exact size we ended up using */
754232604Strasz	params->lun_size_bytes = be_lun->size_bytes;
755232604Strasz
756232604Strasz	req->status = CTL_LUN_OK;
757232604Strasz	return (0);
758232604Strasz
759232604Straszbailout_error:
760232604Strasz	req->status = CTL_LUN_ERROR;
761232604Strasz	return (0);
762232604Strasz}
763232604Strasz
764229997Skenstatic void
765229997Skenctl_backend_ramdisk_lun_shutdown(void *be_lun)
766229997Sken{
767229997Sken	struct ctl_be_ramdisk_lun *lun;
768229997Sken	struct ctl_be_ramdisk_softc *softc;
769229997Sken	int do_free;
770229997Sken
771229997Sken	lun = (struct ctl_be_ramdisk_lun *)be_lun;
772229997Sken	softc = lun->softc;
773229997Sken	do_free = 0;
774229997Sken
775229997Sken	mtx_lock(&softc->lock);
776229997Sken	lun->flags |= CTL_BE_RAMDISK_LUN_UNCONFIGURED;
777229997Sken	if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
778229997Sken		wakeup(lun);
779229997Sken	} else {
780269058Smav		STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
781229997Sken			      links);
782229997Sken		softc->num_luns--;
783229997Sken		do_free = 1;
784229997Sken	}
785229997Sken	mtx_unlock(&softc->lock);
786229997Sken
787229997Sken	if (do_free != 0)
788229997Sken		free(be_lun, M_RAMDISK);
789229997Sken}
790229997Sken
791229997Skenstatic void
792229997Skenctl_backend_ramdisk_lun_config_status(void *be_lun,
793229997Sken				      ctl_lun_config_status status)
794229997Sken{
795229997Sken	struct ctl_be_ramdisk_lun *lun;
796229997Sken	struct ctl_be_ramdisk_softc *softc;
797229997Sken
798229997Sken	lun = (struct ctl_be_ramdisk_lun *)be_lun;
799229997Sken	softc = lun->softc;
800229997Sken
801229997Sken	if (status == CTL_LUN_CONFIG_OK) {
802229997Sken		mtx_lock(&softc->lock);
803229997Sken		lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
804229997Sken		if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING)
805229997Sken			wakeup(lun);
806229997Sken		mtx_unlock(&softc->lock);
807229997Sken
808229997Sken		/*
809229997Sken		 * We successfully added the LUN, attempt to enable it.
810229997Sken		 */
811287499Smav		if (ctl_enable_lun(&lun->cbe_lun) != 0) {
812229997Sken			printf("%s: ctl_enable_lun() failed!\n", __func__);
813287499Smav			if (ctl_invalidate_lun(&lun->cbe_lun) != 0) {
814229997Sken				printf("%s: ctl_invalidate_lun() failed!\n",
815229997Sken				       __func__);
816229997Sken			}
817229997Sken		}
818229997Sken
819229997Sken		return;
820229997Sken	}
821229997Sken
822229997Sken
823229997Sken	mtx_lock(&softc->lock);
824229997Sken	lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
825229997Sken
826229997Sken	/*
827229997Sken	 * If we have a user waiting, let him handle the cleanup.  If not,
828229997Sken	 * clean things up here.
829229997Sken	 */
830229997Sken	if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
831229997Sken		lun->flags |= CTL_BE_RAMDISK_LUN_CONFIG_ERR;
832229997Sken		wakeup(lun);
833229997Sken	} else {
834229997Sken		STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
835229997Sken			      links);
836229997Sken		softc->num_luns--;
837229997Sken		free(lun, M_RAMDISK);
838229997Sken	}
839229997Sken	mtx_unlock(&softc->lock);
840229997Sken}
841229997Sken
842229997Skenstatic int
843229997Skenctl_backend_ramdisk_config_write(union ctl_io *io)
844229997Sken{
845288348Smav	struct ctl_be_lun *cbe_lun;
846229997Sken	int retval;
847229997Sken
848288348Smav	cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
849288348Smav	    CTL_PRIV_BACKEND_LUN].ptr;
850229997Sken	retval = 0;
851229997Sken	switch (io->scsiio.cdb[0]) {
852229997Sken	case SYNCHRONIZE_CACHE:
853229997Sken	case SYNCHRONIZE_CACHE_16:
854229997Sken		/*
855229997Sken		 * The upper level CTL code will filter out any CDBs with
856229997Sken		 * the immediate bit set and return the proper error.  It
857229997Sken		 * will also not allow a sync cache command to go to a LUN
858229997Sken		 * that is powered down.
859229997Sken		 *
860229997Sken		 * We don't really need to worry about what LBA range the
861229997Sken		 * user asked to be synced out.  When they issue a sync
862229997Sken		 * cache command, we'll sync out the whole thing.
863229997Sken		 *
864229997Sken		 * This is obviously just a stubbed out implementation.
865229997Sken		 * The real implementation will be in the RAIDCore/CTL
866229997Sken		 * interface, and can only really happen when RAIDCore
867229997Sken		 * implements a per-array cache sync.
868229997Sken		 */
869229997Sken		ctl_set_success(&io->scsiio);
870229997Sken		ctl_config_write_done(io);
871229997Sken		break;
872229997Sken	case START_STOP_UNIT: {
873229997Sken		struct scsi_start_stop_unit *cdb;
874229997Sken
875229997Sken		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
876288369Smav		if ((cdb->how & SSS_PC_MASK) != 0) {
877288369Smav			ctl_set_success(&io->scsiio);
878288369Smav			ctl_config_write_done(io);
879288369Smav			break;
880288369Smav		}
881288348Smav		if (cdb->how & SSS_START) {
882288348Smav			if (cdb->how & SSS_LOEJ)
883288348Smav				ctl_lun_has_media(cbe_lun);
884288348Smav			ctl_start_lun(cbe_lun);
885229997Sken		} else {
886288348Smav			ctl_stop_lun(cbe_lun);
887288348Smav			if (cdb->how & SSS_LOEJ)
888288348Smav				ctl_lun_ejected(cbe_lun);
889229997Sken		}
890288348Smav		ctl_set_success(&io->scsiio);
891229997Sken		ctl_config_write_done(io);
892229997Sken		break;
893229997Sken	}
894288310Smav	case PREVENT_ALLOW:
895264274Smav	case WRITE_SAME_10:
896264274Smav	case WRITE_SAME_16:
897264274Smav	case UNMAP:
898264274Smav		ctl_set_success(&io->scsiio);
899264274Smav		ctl_config_write_done(io);
900264274Smav		break;
901229997Sken	default:
902229997Sken		ctl_set_invalid_opcode(&io->scsiio);
903229997Sken		ctl_config_write_done(io);
904229997Sken		retval = CTL_RETVAL_COMPLETE;
905229997Sken		break;
906229997Sken	}
907229997Sken
908229997Sken	return (retval);
909229997Sken}
910229997Sken
911229997Skenstatic int
912229997Skenctl_backend_ramdisk_config_read(union ctl_io *io)
913229997Sken{
914275474Smav	int retval = 0;
915275474Smav
916275474Smav	switch (io->scsiio.cdb[0]) {
917275474Smav	case SERVICE_ACTION_IN:
918275474Smav		if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
919275474Smav			/* We have nothing to tell, leave default data. */
920275474Smav			ctl_config_read_done(io);
921275474Smav			retval = CTL_RETVAL_COMPLETE;
922275474Smav			break;
923275474Smav		}
924275474Smav		ctl_set_invalid_field(&io->scsiio,
925275474Smav				      /*sks_valid*/ 1,
926275474Smav				      /*command*/ 1,
927275474Smav				      /*field*/ 1,
928275474Smav				      /*bit_valid*/ 1,
929275474Smav				      /*bit*/ 4);
930275474Smav		ctl_config_read_done(io);
931275474Smav		retval = CTL_RETVAL_COMPLETE;
932275474Smav		break;
933275474Smav	default:
934275474Smav		ctl_set_invalid_opcode(&io->scsiio);
935275474Smav		ctl_config_read_done(io);
936275474Smav		retval = CTL_RETVAL_COMPLETE;
937275474Smav		break;
938275474Smav	}
939275474Smav
940275474Smav	return (retval);
941229997Sken}
942