ctl_backend_ramdisk.c revision 287499
1229997Sken/*-
2229997Sken * Copyright (c) 2003, 2008 Silicon Graphics International Corp.
3232604Strasz * Copyright (c) 2012 The FreeBSD Foundation
4229997Sken * All rights reserved.
5229997Sken *
6232604Strasz * Portions of this software were developed by Edward Tomasz Napierala
7232604Strasz * under sponsorship from the FreeBSD Foundation.
8232604Strasz *
9229997Sken * Redistribution and use in source and binary forms, with or without
10229997Sken * modification, are permitted provided that the following conditions
11229997Sken * are met:
12229997Sken * 1. Redistributions of source code must retain the above copyright
13229997Sken *    notice, this list of conditions, and the following disclaimer,
14229997Sken *    without modification.
15229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16229997Sken *    substantially similar to the "NO WARRANTY" disclaimer below
17229997Sken *    ("Disclaimer") and any redistribution must be conditioned upon
18229997Sken *    including a substantially similar Disclaimer requirement for further
19229997Sken *    binary redistribution.
20229997Sken *
21229997Sken * NO WARRANTY
22229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
25229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32229997Sken * POSSIBILITY OF SUCH DAMAGES.
33229997Sken *
34229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_ramdisk.c#3 $
35229997Sken */
36229997Sken/*
37229997Sken * CAM Target Layer backend for a "fake" ramdisk.
38229997Sken *
39229997Sken * Author: Ken Merry <ken@FreeBSD.org>
40229997Sken */
41229997Sken
42229997Sken#include <sys/cdefs.h>
43229997Sken__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_backend_ramdisk.c 287499 2015-09-06 09:54:56Z mav $");
44229997Sken
45229997Sken#include <sys/param.h>
46229997Sken#include <sys/systm.h>
47229997Sken#include <sys/kernel.h>
48229997Sken#include <sys/condvar.h>
49229997Sken#include <sys/types.h>
50229997Sken#include <sys/lock.h>
51229997Sken#include <sys/mutex.h>
52229997Sken#include <sys/malloc.h>
53264886Smav#include <sys/taskqueue.h>
54229997Sken#include <sys/time.h>
55229997Sken#include <sys/queue.h>
56229997Sken#include <sys/conf.h>
57229997Sken#include <sys/ioccom.h>
58229997Sken#include <sys/module.h>
59229997Sken
60229997Sken#include <cam/scsi/scsi_all.h>
61229997Sken#include <cam/ctl/ctl_io.h>
62229997Sken#include <cam/ctl/ctl.h>
63229997Sken#include <cam/ctl/ctl_util.h>
64229997Sken#include <cam/ctl/ctl_backend.h>
65229997Sken#include <cam/ctl/ctl_debug.h>
66229997Sken#include <cam/ctl/ctl_ioctl.h>
67229997Sken#include <cam/ctl/ctl_error.h>
68229997Sken
69229997Skentypedef enum {
70229997Sken	CTL_BE_RAMDISK_LUN_UNCONFIGURED	= 0x01,
71229997Sken	CTL_BE_RAMDISK_LUN_CONFIG_ERR	= 0x02,
72229997Sken	CTL_BE_RAMDISK_LUN_WAITING	= 0x04
73229997Sken} ctl_be_ramdisk_lun_flags;
74229997Sken
75229997Skenstruct ctl_be_ramdisk_lun {
76264886Smav	char lunname[32];
77229997Sken	uint64_t size_bytes;
78229997Sken	uint64_t size_blocks;
79229997Sken	struct ctl_be_ramdisk_softc *softc;
80229997Sken	ctl_be_ramdisk_lun_flags flags;
81229997Sken	STAILQ_ENTRY(ctl_be_ramdisk_lun) links;
82287499Smav	struct ctl_be_lun cbe_lun;
83264886Smav	struct taskqueue *io_taskqueue;
84264886Smav	struct task io_task;
85264886Smav	STAILQ_HEAD(, ctl_io_hdr) cont_queue;
86267877Smav	struct mtx_padalign queue_lock;
87229997Sken};
88229997Sken
89229997Skenstruct ctl_be_ramdisk_softc {
90229997Sken	struct mtx lock;
91229997Sken	int rd_size;
92229997Sken#ifdef CTL_RAMDISK_PAGES
93229997Sken	uint8_t **ramdisk_pages;
94229997Sken	int num_pages;
95229997Sken#else
96229997Sken	uint8_t *ramdisk_buffer;
97229997Sken#endif
98229997Sken	int num_luns;
99229997Sken	STAILQ_HEAD(, ctl_be_ramdisk_lun) lun_list;
100229997Sken};
101229997Sken
102229997Skenstatic struct ctl_be_ramdisk_softc rd_softc;
103229997Sken
104229997Skenint ctl_backend_ramdisk_init(void);
105229997Skenvoid ctl_backend_ramdisk_shutdown(void);
106229997Skenstatic int ctl_backend_ramdisk_move_done(union ctl_io *io);
107229997Skenstatic int ctl_backend_ramdisk_submit(union ctl_io *io);
108264886Smavstatic void ctl_backend_ramdisk_continue(union ctl_io *io);
109229997Skenstatic int ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd,
110229997Sken				     caddr_t addr, int flag, struct thread *td);
111229997Skenstatic int ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
112229997Sken				  struct ctl_lun_req *req);
113229997Skenstatic int ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
114287499Smav				      struct ctl_lun_req *req);
115232604Straszstatic int ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
116232604Strasz				  struct ctl_lun_req *req);
117264886Smavstatic void ctl_backend_ramdisk_worker(void *context, int pending);
118229997Skenstatic void ctl_backend_ramdisk_lun_shutdown(void *be_lun);
119229997Skenstatic void ctl_backend_ramdisk_lun_config_status(void *be_lun,
120229997Sken						  ctl_lun_config_status status);
121229997Skenstatic int ctl_backend_ramdisk_config_write(union ctl_io *io);
122229997Skenstatic int ctl_backend_ramdisk_config_read(union ctl_io *io);
123229997Sken
124229997Skenstatic struct ctl_backend_driver ctl_be_ramdisk_driver =
125229997Sken{
126230334Sken	.name = "ramdisk",
127230334Sken	.flags = CTL_BE_FLAG_HAS_CONFIG,
128230334Sken	.init = ctl_backend_ramdisk_init,
129230334Sken	.data_submit = ctl_backend_ramdisk_submit,
130230334Sken	.data_move_done = ctl_backend_ramdisk_move_done,
131230334Sken	.config_read = ctl_backend_ramdisk_config_read,
132230334Sken	.config_write = ctl_backend_ramdisk_config_write,
133230334Sken	.ioctl = ctl_backend_ramdisk_ioctl
134229997Sken};
135229997Sken
136229997SkenMALLOC_DEFINE(M_RAMDISK, "ramdisk", "Memory used for CTL RAMdisk");
137229997SkenCTL_BACKEND_DECLARE(cbr, ctl_be_ramdisk_driver);
138229997Sken
139229997Skenint
140229997Skenctl_backend_ramdisk_init(void)
141229997Sken{
142229997Sken	struct ctl_be_ramdisk_softc *softc;
143229997Sken#ifdef CTL_RAMDISK_PAGES
144240993Strasz	int i;
145229997Sken#endif
146229997Sken
147229997Sken
148229997Sken	softc = &rd_softc;
149229997Sken
150229997Sken	memset(softc, 0, sizeof(*softc));
151229997Sken
152267877Smav	mtx_init(&softc->lock, "ctlramdisk", NULL, MTX_DEF);
153229997Sken
154229997Sken	STAILQ_INIT(&softc->lun_list);
155264886Smav	softc->rd_size = 1024 * 1024;
156229997Sken#ifdef CTL_RAMDISK_PAGES
157229997Sken	softc->num_pages = softc->rd_size / PAGE_SIZE;
158229997Sken	softc->ramdisk_pages = (uint8_t **)malloc(sizeof(uint8_t *) *
159229997Sken						  softc->num_pages, M_RAMDISK,
160229997Sken						  M_WAITOK);
161240993Strasz	for (i = 0; i < softc->num_pages; i++)
162229997Sken		softc->ramdisk_pages[i] = malloc(PAGE_SIZE, M_RAMDISK,M_WAITOK);
163229997Sken#else
164229997Sken	softc->ramdisk_buffer = (uint8_t *)malloc(softc->rd_size, M_RAMDISK,
165229997Sken						  M_WAITOK);
166229997Sken#endif
167229997Sken
168229997Sken	return (0);
169229997Sken}
170229997Sken
171229997Skenvoid
172229997Skenctl_backend_ramdisk_shutdown(void)
173229997Sken{
174229997Sken	struct ctl_be_ramdisk_softc *softc;
175229997Sken	struct ctl_be_ramdisk_lun *lun, *next_lun;
176229997Sken#ifdef CTL_RAMDISK_PAGES
177229997Sken	int i;
178229997Sken#endif
179229997Sken
180229997Sken	softc = &rd_softc;
181229997Sken
182229997Sken	mtx_lock(&softc->lock);
183229997Sken	for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
184229997Sken		/*
185229997Sken		 * Grab the next LUN.  The current LUN may get removed by
186229997Sken		 * ctl_invalidate_lun(), which will call our LUN shutdown
187229997Sken		 * routine, if there is no outstanding I/O for this LUN.
188229997Sken		 */
189229997Sken		next_lun = STAILQ_NEXT(lun, links);
190229997Sken
191229997Sken		/*
192229997Sken		 * Drop our lock here.  Since ctl_invalidate_lun() can call
193229997Sken		 * back into us, this could potentially lead to a recursive
194229997Sken		 * lock of the same mutex, which would cause a hang.
195229997Sken		 */
196229997Sken		mtx_unlock(&softc->lock);
197287499Smav		ctl_disable_lun(&lun->cbe_lun);
198287499Smav		ctl_invalidate_lun(&lun->cbe_lun);
199229997Sken		mtx_lock(&softc->lock);
200229997Sken	}
201229997Sken	mtx_unlock(&softc->lock);
202229997Sken
203229997Sken#ifdef CTL_RAMDISK_PAGES
204229997Sken	for (i = 0; i < softc->num_pages; i++)
205229997Sken		free(softc->ramdisk_pages[i], M_RAMDISK);
206229997Sken
207229997Sken	free(softc->ramdisk_pages, M_RAMDISK);
208229997Sken#else
209229997Sken	free(softc->ramdisk_buffer, M_RAMDISK);
210229997Sken#endif
211229997Sken
212229997Sken	if (ctl_backend_deregister(&ctl_be_ramdisk_driver) != 0) {
213229997Sken		printf("ctl_backend_ramdisk_shutdown: "
214229997Sken		       "ctl_backend_deregister() failed!\n");
215229997Sken	}
216229997Sken}
217229997Sken
218229997Skenstatic int
219229997Skenctl_backend_ramdisk_move_done(union ctl_io *io)
220229997Sken{
221287499Smav	struct ctl_be_lun *cbe_lun;
222264886Smav	struct ctl_be_ramdisk_lun *be_lun;
223229997Sken#ifdef CTL_TIME_IO
224229997Sken	struct bintime cur_bt;
225229997Sken#endif
226229997Sken
227229997Sken	CTL_DEBUG_PRINT(("ctl_backend_ramdisk_move_done\n"));
228287499Smav	cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
229264886Smav		CTL_PRIV_BACKEND_LUN].ptr;
230287499Smav	be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun->be_lun;
231264886Smav#ifdef CTL_TIME_IO
232264886Smav	getbintime(&cur_bt);
233264886Smav	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
234264886Smav	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
235264886Smav	io->io_hdr.num_dmas++;
236264886Smav#endif
237264886Smav	if (io->scsiio.kern_sg_entries > 0)
238264886Smav		free(io->scsiio.kern_data_ptr, M_RAMDISK);
239264886Smav	io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
240275058Smav	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
241275058Smav		;
242275058Smav	} else if ((io->io_hdr.port_status == 0) &&
243275058Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
244267519Smav		if (io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer > 0) {
245267877Smav			mtx_lock(&be_lun->queue_lock);
246264886Smav			STAILQ_INSERT_TAIL(&be_lun->cont_queue,
247264886Smav			    &io->io_hdr, links);
248267877Smav			mtx_unlock(&be_lun->queue_lock);
249264886Smav			taskqueue_enqueue(be_lun->io_taskqueue,
250264886Smav			    &be_lun->io_task);
251264886Smav			return (0);
252264886Smav		}
253275009Smav		ctl_set_success(&io->scsiio);
254275058Smav	} else if ((io->io_hdr.port_status != 0) &&
255275058Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
256275058Smav	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
257229997Sken		/*
258229997Sken		 * For hardware error sense keys, the sense key
259229997Sken		 * specific value is defined to be a retry count,
260229997Sken		 * but we use it to pass back an internal FETD
261229997Sken		 * error code.  XXX KDM  Hopefully the FETD is only
262229997Sken		 * using 16 bits for an error code, since that's
263229997Sken		 * all the space we have in the sks field.
264229997Sken		 */
265229997Sken		ctl_set_internal_failure(&io->scsiio,
266229997Sken					 /*sks_valid*/ 1,
267229997Sken					 /*retry_count*/
268229997Sken					 io->io_hdr.port_status);
269229997Sken	}
270267537Smav	ctl_data_submit_done(io);
271229997Sken	return(0);
272229997Sken}
273229997Sken
274229997Skenstatic int
275229997Skenctl_backend_ramdisk_submit(union ctl_io *io)
276229997Sken{
277287499Smav	struct ctl_be_lun *cbe_lun;
278267537Smav	struct ctl_lba_len_flags *lbalen;
279229997Sken
280287499Smav	cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
281267519Smav		CTL_PRIV_BACKEND_LUN].ptr;
282267537Smav	lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
283267537Smav	if (lbalen->flags & CTL_LLF_VERIFY) {
284267537Smav		ctl_set_success(&io->scsiio);
285267537Smav		ctl_data_submit_done(io);
286267537Smav		return (CTL_RETVAL_COMPLETE);
287267537Smav	}
288267519Smav	io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer =
289287499Smav	    lbalen->len * cbe_lun->blocksize;
290264886Smav	ctl_backend_ramdisk_continue(io);
291264886Smav	return (CTL_RETVAL_COMPLETE);
292264886Smav}
293229997Sken
294264886Smavstatic void
295264886Smavctl_backend_ramdisk_continue(union ctl_io *io)
296264886Smav{
297264886Smav	struct ctl_be_ramdisk_softc *softc;
298264886Smav	int len, len_filled, sg_filled;
299264886Smav#ifdef CTL_RAMDISK_PAGES
300264886Smav	struct ctl_sg_entry *sg_entries;
301264886Smav	int i;
302264886Smav#endif
303229997Sken
304264886Smav	softc = &rd_softc;
305267519Smav	len = io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer;
306229997Sken#ifdef CTL_RAMDISK_PAGES
307264886Smav	sg_filled = min(btoc(len), softc->num_pages);
308264886Smav	if (sg_filled > 1) {
309229997Sken		io->scsiio.kern_data_ptr = malloc(sizeof(struct ctl_sg_entry) *
310264886Smav						  sg_filled, M_RAMDISK,
311229997Sken						  M_WAITOK);
312229997Sken		sg_entries = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
313264886Smav		for (i = 0, len_filled = 0; i < sg_filled; i++) {
314229997Sken			sg_entries[i].addr = softc->ramdisk_pages[i];
315275953Smav			sg_entries[i].len = MIN(PAGE_SIZE, len - len_filled);
316264886Smav			len_filled += sg_entries[i].len;
317229997Sken		}
318264886Smav		io->io_hdr.flags |= CTL_FLAG_KDPTR_SGLIST;
319229997Sken	} else {
320264886Smav		sg_filled = 0;
321264886Smav		len_filled = len;
322229997Sken		io->scsiio.kern_data_ptr = softc->ramdisk_pages[0];
323264886Smav	}
324229997Sken#else
325264886Smav	sg_filled = 0;
326264886Smav	len_filled = min(len, softc->rd_size);
327264886Smav	io->scsiio.kern_data_ptr = softc->ramdisk_buffer;
328264886Smav#endif /* CTL_RAMDISK_PAGES */
329229997Sken
330267514Smav	io->scsiio.be_move_done = ctl_backend_ramdisk_move_done;
331267514Smav	io->scsiio.kern_data_resid = 0;
332264886Smav	io->scsiio.kern_data_len = len_filled;
333264886Smav	io->scsiio.kern_sg_entries = sg_filled;
334264886Smav	io->io_hdr.flags |= CTL_FLAG_ALLOCATED;
335267519Smav	io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer -= len_filled;
336229997Sken#ifdef CTL_TIME_IO
337229997Sken	getbintime(&io->io_hdr.dma_start_bt);
338229997Sken#endif
339229997Sken	ctl_datamove(io);
340264886Smav}
341229997Sken
342264886Smavstatic void
343264886Smavctl_backend_ramdisk_worker(void *context, int pending)
344264886Smav{
345264886Smav	struct ctl_be_ramdisk_softc *softc;
346264886Smav	struct ctl_be_ramdisk_lun *be_lun;
347264886Smav	union ctl_io *io;
348264886Smav
349264886Smav	be_lun = (struct ctl_be_ramdisk_lun *)context;
350264886Smav	softc = be_lun->softc;
351264886Smav
352267877Smav	mtx_lock(&be_lun->queue_lock);
353264886Smav	for (;;) {
354264886Smav		io = (union ctl_io *)STAILQ_FIRST(&be_lun->cont_queue);
355264886Smav		if (io != NULL) {
356264886Smav			STAILQ_REMOVE(&be_lun->cont_queue, &io->io_hdr,
357264886Smav				      ctl_io_hdr, links);
358264886Smav
359267877Smav			mtx_unlock(&be_lun->queue_lock);
360264886Smav
361264886Smav			ctl_backend_ramdisk_continue(io);
362264886Smav
363267877Smav			mtx_lock(&be_lun->queue_lock);
364264886Smav			continue;
365264886Smav		}
366264886Smav
367264886Smav		/*
368264886Smav		 * If we get here, there is no work left in the queues, so
369264886Smav		 * just break out and let the task queue go to sleep.
370264886Smav		 */
371264886Smav		break;
372264886Smav	}
373267877Smav	mtx_unlock(&be_lun->queue_lock);
374229997Sken}
375229997Sken
376229997Skenstatic int
377229997Skenctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
378229997Sken			  int flag, struct thread *td)
379229997Sken{
380229997Sken	struct ctl_be_ramdisk_softc *softc;
381229997Sken	int retval;
382229997Sken
383229997Sken	retval = 0;
384229997Sken	softc = &rd_softc;
385229997Sken
386229997Sken	switch (cmd) {
387229997Sken	case CTL_LUN_REQ: {
388229997Sken		struct ctl_lun_req *lun_req;
389229997Sken
390229997Sken		lun_req = (struct ctl_lun_req *)addr;
391229997Sken
392229997Sken		switch (lun_req->reqtype) {
393229997Sken		case CTL_LUNREQ_CREATE:
394287499Smav			retval = ctl_backend_ramdisk_create(softc, lun_req);
395229997Sken			break;
396229997Sken		case CTL_LUNREQ_RM:
397229997Sken			retval = ctl_backend_ramdisk_rm(softc, lun_req);
398229997Sken			break;
399232604Strasz		case CTL_LUNREQ_MODIFY:
400232604Strasz			retval = ctl_backend_ramdisk_modify(softc, lun_req);
401232604Strasz			break;
402229997Sken		default:
403229997Sken			lun_req->status = CTL_LUN_ERROR;
404229997Sken			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
405229997Sken				 "%s: invalid LUN request type %d", __func__,
406229997Sken				 lun_req->reqtype);
407229997Sken			break;
408229997Sken		}
409229997Sken		break;
410229997Sken	}
411229997Sken	default:
412229997Sken		retval = ENOTTY;
413229997Sken		break;
414229997Sken	}
415229997Sken
416229997Sken	return (retval);
417229997Sken}
418229997Sken
419229997Skenstatic int
420229997Skenctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
421229997Sken		       struct ctl_lun_req *req)
422229997Sken{
423229997Sken	struct ctl_be_ramdisk_lun *be_lun;
424229997Sken	struct ctl_lun_rm_params *params;
425229997Sken	int retval;
426229997Sken
427229997Sken
428229997Sken	retval = 0;
429229997Sken	params = &req->reqdata.rm;
430229997Sken
431229997Sken	be_lun = NULL;
432229997Sken
433229997Sken	mtx_lock(&softc->lock);
434229997Sken
435229997Sken	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
436287499Smav		if (be_lun->cbe_lun.lun_id == params->lun_id)
437229997Sken			break;
438229997Sken	}
439229997Sken	mtx_unlock(&softc->lock);
440229997Sken
441229997Sken	if (be_lun == NULL) {
442229997Sken		snprintf(req->error_str, sizeof(req->error_str),
443229997Sken			 "%s: LUN %u is not managed by the ramdisk backend",
444229997Sken			 __func__, params->lun_id);
445229997Sken		goto bailout_error;
446229997Sken	}
447229997Sken
448287499Smav	retval = ctl_disable_lun(&be_lun->cbe_lun);
449229997Sken
450229997Sken	if (retval != 0) {
451229997Sken		snprintf(req->error_str, sizeof(req->error_str),
452229997Sken			 "%s: error %d returned from ctl_disable_lun() for "
453229997Sken			 "LUN %d", __func__, retval, params->lun_id);
454229997Sken		goto bailout_error;
455229997Sken	}
456229997Sken
457229997Sken	/*
458229997Sken	 * Set the waiting flag before we invalidate the LUN.  Our shutdown
459229997Sken	 * routine can be called any time after we invalidate the LUN,
460229997Sken	 * and can be called from our context.
461229997Sken	 *
462229997Sken	 * This tells the shutdown routine that we're waiting, or we're
463229997Sken	 * going to wait for the shutdown to happen.
464229997Sken	 */
465229997Sken	mtx_lock(&softc->lock);
466229997Sken	be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
467229997Sken	mtx_unlock(&softc->lock);
468229997Sken
469287499Smav	retval = ctl_invalidate_lun(&be_lun->cbe_lun);
470229997Sken	if (retval != 0) {
471229997Sken		snprintf(req->error_str, sizeof(req->error_str),
472229997Sken			 "%s: error %d returned from ctl_invalidate_lun() for "
473229997Sken			 "LUN %d", __func__, retval, params->lun_id);
474252569Smav		mtx_lock(&softc->lock);
475252569Smav		be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
476252569Smav		mtx_unlock(&softc->lock);
477229997Sken		goto bailout_error;
478229997Sken	}
479229997Sken
480229997Sken	mtx_lock(&softc->lock);
481229997Sken
482229997Sken	while ((be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) == 0) {
483229997Sken		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
484229997Sken 		if (retval == EINTR)
485229997Sken			break;
486229997Sken	}
487229997Sken	be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
488229997Sken
489229997Sken	/*
490229997Sken	 * We only remove this LUN from the list and free it (below) if
491229997Sken	 * retval == 0.  If the user interrupted the wait, we just bail out
492229997Sken	 * without actually freeing the LUN.  We let the shutdown routine
493229997Sken	 * free the LUN if that happens.
494229997Sken	 */
495229997Sken	if (retval == 0) {
496229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
497229997Sken			      links);
498229997Sken		softc->num_luns--;
499229997Sken	}
500229997Sken
501229997Sken	mtx_unlock(&softc->lock);
502229997Sken
503264886Smav	if (retval == 0) {
504264886Smav		taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task);
505264886Smav		taskqueue_free(be_lun->io_taskqueue);
506287499Smav		ctl_free_opts(&be_lun->cbe_lun.options);
507267877Smav		mtx_destroy(&be_lun->queue_lock);
508229997Sken		free(be_lun, M_RAMDISK);
509264886Smav	}
510229997Sken
511229997Sken	req->status = CTL_LUN_OK;
512229997Sken
513229997Sken	return (retval);
514229997Sken
515229997Skenbailout_error:
516229997Sken	req->status = CTL_LUN_ERROR;
517229997Sken
518229997Sken	return (0);
519229997Sken}
520229997Sken
521229997Skenstatic int
522229997Skenctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
523287499Smav			   struct ctl_lun_req *req)
524229997Sken{
525229997Sken	struct ctl_be_ramdisk_lun *be_lun;
526287499Smav	struct ctl_be_lun *cbe_lun;
527229997Sken	struct ctl_lun_create_params *params;
528267481Smav	char *value;
529229997Sken	char tmpstr[32];
530287499Smav	int retval;
531229997Sken
532229997Sken	retval = 0;
533229997Sken	params = &req->reqdata.create;
534229997Sken
535287499Smav	be_lun = malloc(sizeof(*be_lun), M_RAMDISK, M_ZERO | M_WAITOK);
536287499Smav	cbe_lun = &be_lun->cbe_lun;
537287499Smav	cbe_lun->be_lun = be_lun;
538287499Smav	be_lun->softc = softc;
539264886Smav	sprintf(be_lun->lunname, "cram%d", softc->num_luns);
540287499Smav	ctl_init_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args);
541229997Sken
542229997Sken	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
543287499Smav		cbe_lun->lun_type = params->device_type;
544229997Sken	else
545287499Smav		cbe_lun->lun_type = T_DIRECT;
546287499Smav	be_lun->flags = CTL_BE_RAMDISK_LUN_UNCONFIGURED;
547287499Smav	cbe_lun->flags = CTL_LUN_FLAG_PRIMARY;
548229997Sken
549287499Smav	if (cbe_lun->lun_type == T_DIRECT) {
550287499Smav		if (params->blocksize_bytes != 0)
551287499Smav			cbe_lun->blocksize = params->blocksize_bytes;
552287499Smav		else
553287499Smav			cbe_lun->blocksize = 512;
554287499Smav		if (params->lun_size_bytes < cbe_lun->blocksize) {
555229997Sken			snprintf(req->error_str, sizeof(req->error_str),
556229997Sken				 "%s: LUN size %ju < blocksize %u", __func__,
557287499Smav				 params->lun_size_bytes, cbe_lun->blocksize);
558229997Sken			goto bailout_error;
559229997Sken		}
560287499Smav		be_lun->size_blocks = params->lun_size_bytes / cbe_lun->blocksize;
561287499Smav		be_lun->size_bytes = be_lun->size_blocks * cbe_lun->blocksize;
562287499Smav		cbe_lun->maxlba = be_lun->size_blocks - 1;
563287499Smav		cbe_lun->atomicblock = UINT32_MAX;
564287499Smav		cbe_lun->opttxferlen = softc->rd_size / cbe_lun->blocksize;
565229997Sken	}
566229997Sken
567229997Sken	/* Tell the user the blocksize we ended up using */
568287499Smav	params->blocksize_bytes = cbe_lun->blocksize;
569229997Sken	params->lun_size_bytes = be_lun->size_bytes;
570229997Sken
571287499Smav	value = ctl_get_opt(&cbe_lun->options, "unmap");
572267481Smav	if (value != NULL && strcmp(value, "on") == 0)
573287499Smav		cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
574287499Smav	value = ctl_get_opt(&cbe_lun->options, "readonly");
575287499Smav	if (value != NULL && strcmp(value, "on") == 0)
576287499Smav		cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
577287499Smav	cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
578287499Smav	value = ctl_get_opt(&cbe_lun->options, "serseq");
579287499Smav	if (value != NULL && strcmp(value, "on") == 0)
580287499Smav		cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
581287499Smav	else if (value != NULL && strcmp(value, "read") == 0)
582287499Smav		cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
583287499Smav	else if (value != NULL && strcmp(value, "off") == 0)
584287499Smav		cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
585254759Strasz
586229997Sken	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
587287499Smav		cbe_lun->req_lun_id = params->req_lun_id;
588287499Smav		cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
589229997Sken	} else
590287499Smav		cbe_lun->req_lun_id = 0;
591229997Sken
592287499Smav	cbe_lun->lun_shutdown = ctl_backend_ramdisk_lun_shutdown;
593287499Smav	cbe_lun->lun_config_status = ctl_backend_ramdisk_lun_config_status;
594287499Smav	cbe_lun->be = &ctl_be_ramdisk_driver;
595229997Sken	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
596229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
597229997Sken			 softc->num_luns);
598287499Smav		strncpy((char *)cbe_lun->serial_num, tmpstr,
599287499Smav			MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
600229997Sken
601229997Sken		/* Tell the user what we used for a serial number */
602229997Sken		strncpy((char *)params->serial_num, tmpstr,
603275953Smav			MIN(sizeof(params->serial_num), sizeof(tmpstr)));
604229997Sken	} else {
605287499Smav		strncpy((char *)cbe_lun->serial_num, params->serial_num,
606287499Smav			MIN(sizeof(cbe_lun->serial_num),
607275953Smav			    sizeof(params->serial_num)));
608229997Sken	}
609229997Sken	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
610229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
611287499Smav		strncpy((char *)cbe_lun->device_id, tmpstr,
612287499Smav			MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
613229997Sken
614229997Sken		/* Tell the user what we used for a device ID */
615229997Sken		strncpy((char *)params->device_id, tmpstr,
616275953Smav			MIN(sizeof(params->device_id), sizeof(tmpstr)));
617229997Sken	} else {
618287499Smav		strncpy((char *)cbe_lun->device_id, params->device_id,
619287499Smav			MIN(sizeof(cbe_lun->device_id),
620275953Smav			    sizeof(params->device_id)));
621229997Sken	}
622229997Sken
623264886Smav	STAILQ_INIT(&be_lun->cont_queue);
624267877Smav	mtx_init(&be_lun->queue_lock, "cram queue lock", NULL, MTX_DEF);
625264886Smav	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_backend_ramdisk_worker,
626264886Smav	    be_lun);
627264886Smav
628264886Smav	be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
629264886Smav	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
630264886Smav	if (be_lun->io_taskqueue == NULL) {
631264886Smav		snprintf(req->error_str, sizeof(req->error_str),
632264886Smav			 "%s: Unable to create taskqueue", __func__);
633264886Smav		goto bailout_error;
634264886Smav	}
635264886Smav
636264886Smav	retval = taskqueue_start_threads(&be_lun->io_taskqueue,
637264886Smav					 /*num threads*/1,
638264886Smav					 /*priority*/PWAIT,
639264886Smav					 /*thread name*/
640264886Smav					 "%s taskq", be_lun->lunname);
641264886Smav	if (retval != 0)
642264886Smav		goto bailout_error;
643264886Smav
644229997Sken	mtx_lock(&softc->lock);
645229997Sken	softc->num_luns++;
646229997Sken	STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
647229997Sken
648229997Sken	mtx_unlock(&softc->lock);
649229997Sken
650287499Smav	retval = ctl_add_lun(&be_lun->cbe_lun);
651229997Sken	if (retval != 0) {
652229997Sken		mtx_lock(&softc->lock);
653229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
654229997Sken			      links);
655229997Sken		softc->num_luns--;
656229997Sken		mtx_unlock(&softc->lock);
657229997Sken		snprintf(req->error_str, sizeof(req->error_str),
658229997Sken			 "%s: ctl_add_lun() returned error %d, see dmesg for "
659229997Sken			"details", __func__, retval);
660229997Sken		retval = 0;
661229997Sken		goto bailout_error;
662229997Sken	}
663229997Sken
664229997Sken	mtx_lock(&softc->lock);
665229997Sken
666229997Sken	/*
667229997Sken	 * Tell the config_status routine that we're waiting so it won't
668229997Sken	 * clean up the LUN in the event of an error.
669229997Sken	 */
670229997Sken	be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
671229997Sken
672229997Sken	while (be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) {
673229997Sken		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
674229997Sken		if (retval == EINTR)
675229997Sken			break;
676229997Sken	}
677229997Sken	be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
678229997Sken
679229997Sken	if (be_lun->flags & CTL_BE_RAMDISK_LUN_CONFIG_ERR) {
680229997Sken		snprintf(req->error_str, sizeof(req->error_str),
681229997Sken			 "%s: LUN configuration error, see dmesg for details",
682229997Sken			 __func__);
683229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
684229997Sken			      links);
685229997Sken		softc->num_luns--;
686229997Sken		mtx_unlock(&softc->lock);
687229997Sken		goto bailout_error;
688229997Sken	} else {
689287499Smav		params->req_lun_id = cbe_lun->lun_id;
690229997Sken	}
691229997Sken	mtx_unlock(&softc->lock);
692229997Sken
693229997Sken	req->status = CTL_LUN_OK;
694229997Sken
695229997Sken	return (retval);
696229997Sken
697229997Skenbailout_error:
698229997Sken	req->status = CTL_LUN_ERROR;
699264886Smav	if (be_lun != NULL) {
700264886Smav		if (be_lun->io_taskqueue != NULL) {
701264886Smav			taskqueue_free(be_lun->io_taskqueue);
702264886Smav		}
703287499Smav		ctl_free_opts(&cbe_lun->options);
704267877Smav		mtx_destroy(&be_lun->queue_lock);
705264886Smav		free(be_lun, M_RAMDISK);
706264886Smav	}
707229997Sken
708229997Sken	return (retval);
709229997Sken}
710229997Sken
711232604Straszstatic int
712232604Straszctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
713232604Strasz		       struct ctl_lun_req *req)
714232604Strasz{
715232604Strasz	struct ctl_be_ramdisk_lun *be_lun;
716232604Strasz	struct ctl_lun_modify_params *params;
717232604Strasz	uint32_t blocksize;
718232604Strasz
719232604Strasz	params = &req->reqdata.modify;
720232604Strasz
721232604Strasz	be_lun = NULL;
722232604Strasz
723232604Strasz	mtx_lock(&softc->lock);
724232604Strasz	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
725287499Smav		if (be_lun->cbe_lun.lun_id == params->lun_id)
726232604Strasz			break;
727232604Strasz	}
728232604Strasz	mtx_unlock(&softc->lock);
729232604Strasz
730232604Strasz	if (be_lun == NULL) {
731232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
732232604Strasz			 "%s: LUN %u is not managed by the ramdisk backend",
733232604Strasz			 __func__, params->lun_id);
734232604Strasz		goto bailout_error;
735232604Strasz	}
736232604Strasz
737232604Strasz	if (params->lun_size_bytes == 0) {
738232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
739232604Strasz			"%s: LUN size \"auto\" not supported "
740232604Strasz			"by the ramdisk backend", __func__);
741232604Strasz		goto bailout_error;
742232604Strasz	}
743232604Strasz
744287499Smav	blocksize = be_lun->cbe_lun.blocksize;
745232604Strasz
746232604Strasz	if (params->lun_size_bytes < blocksize) {
747232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
748232604Strasz			"%s: LUN size %ju < blocksize %u", __func__,
749232604Strasz			params->lun_size_bytes, blocksize);
750232604Strasz		goto bailout_error;
751232604Strasz	}
752232604Strasz
753232604Strasz	be_lun->size_blocks = params->lun_size_bytes / blocksize;
754232604Strasz	be_lun->size_bytes = be_lun->size_blocks * blocksize;
755232604Strasz
756232604Strasz	/*
757232604Strasz	 * The maximum LBA is the size - 1.
758232604Strasz	 *
759232604Strasz	 * XXX: Note that this field is being updated without locking,
760232604Strasz	 * 	which might cause problems on 32-bit architectures.
761232604Strasz	 */
762287499Smav	be_lun->cbe_lun.maxlba = be_lun->size_blocks - 1;
763287499Smav	ctl_lun_capacity_changed(&be_lun->cbe_lun);
764232604Strasz
765232604Strasz	/* Tell the user the exact size we ended up using */
766232604Strasz	params->lun_size_bytes = be_lun->size_bytes;
767232604Strasz
768232604Strasz	req->status = CTL_LUN_OK;
769232604Strasz
770232604Strasz	return (0);
771232604Strasz
772232604Straszbailout_error:
773232604Strasz	req->status = CTL_LUN_ERROR;
774232604Strasz
775232604Strasz	return (0);
776232604Strasz}
777232604Strasz
778229997Skenstatic void
779229997Skenctl_backend_ramdisk_lun_shutdown(void *be_lun)
780229997Sken{
781229997Sken	struct ctl_be_ramdisk_lun *lun;
782229997Sken	struct ctl_be_ramdisk_softc *softc;
783229997Sken	int do_free;
784229997Sken
785229997Sken	lun = (struct ctl_be_ramdisk_lun *)be_lun;
786229997Sken	softc = lun->softc;
787229997Sken	do_free = 0;
788229997Sken
789229997Sken	mtx_lock(&softc->lock);
790229997Sken
791229997Sken	lun->flags |= CTL_BE_RAMDISK_LUN_UNCONFIGURED;
792229997Sken
793229997Sken	if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
794229997Sken		wakeup(lun);
795229997Sken	} else {
796269058Smav		STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
797229997Sken			      links);
798229997Sken		softc->num_luns--;
799229997Sken		do_free = 1;
800229997Sken	}
801229997Sken
802229997Sken	mtx_unlock(&softc->lock);
803229997Sken
804229997Sken	if (do_free != 0)
805229997Sken		free(be_lun, M_RAMDISK);
806229997Sken}
807229997Sken
808229997Skenstatic void
809229997Skenctl_backend_ramdisk_lun_config_status(void *be_lun,
810229997Sken				      ctl_lun_config_status status)
811229997Sken{
812229997Sken	struct ctl_be_ramdisk_lun *lun;
813229997Sken	struct ctl_be_ramdisk_softc *softc;
814229997Sken
815229997Sken	lun = (struct ctl_be_ramdisk_lun *)be_lun;
816229997Sken	softc = lun->softc;
817229997Sken
818229997Sken	if (status == CTL_LUN_CONFIG_OK) {
819229997Sken		mtx_lock(&softc->lock);
820229997Sken		lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
821229997Sken		if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING)
822229997Sken			wakeup(lun);
823229997Sken		mtx_unlock(&softc->lock);
824229997Sken
825229997Sken		/*
826229997Sken		 * We successfully added the LUN, attempt to enable it.
827229997Sken		 */
828287499Smav		if (ctl_enable_lun(&lun->cbe_lun) != 0) {
829229997Sken			printf("%s: ctl_enable_lun() failed!\n", __func__);
830287499Smav			if (ctl_invalidate_lun(&lun->cbe_lun) != 0) {
831229997Sken				printf("%s: ctl_invalidate_lun() failed!\n",
832229997Sken				       __func__);
833229997Sken			}
834229997Sken		}
835229997Sken
836229997Sken		return;
837229997Sken	}
838229997Sken
839229997Sken
840229997Sken	mtx_lock(&softc->lock);
841229997Sken	lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
842229997Sken
843229997Sken	/*
844229997Sken	 * If we have a user waiting, let him handle the cleanup.  If not,
845229997Sken	 * clean things up here.
846229997Sken	 */
847229997Sken	if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
848229997Sken		lun->flags |= CTL_BE_RAMDISK_LUN_CONFIG_ERR;
849229997Sken		wakeup(lun);
850229997Sken	} else {
851229997Sken		STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
852229997Sken			      links);
853229997Sken		softc->num_luns--;
854229997Sken		free(lun, M_RAMDISK);
855229997Sken	}
856229997Sken	mtx_unlock(&softc->lock);
857229997Sken}
858229997Sken
859229997Skenstatic int
860229997Skenctl_backend_ramdisk_config_write(union ctl_io *io)
861229997Sken{
862229997Sken	struct ctl_be_ramdisk_softc *softc;
863229997Sken	int retval;
864229997Sken
865229997Sken	retval = 0;
866229997Sken	softc = &rd_softc;
867229997Sken
868229997Sken	switch (io->scsiio.cdb[0]) {
869229997Sken	case SYNCHRONIZE_CACHE:
870229997Sken	case SYNCHRONIZE_CACHE_16:
871229997Sken		/*
872229997Sken		 * The upper level CTL code will filter out any CDBs with
873229997Sken		 * the immediate bit set and return the proper error.  It
874229997Sken		 * will also not allow a sync cache command to go to a LUN
875229997Sken		 * that is powered down.
876229997Sken		 *
877229997Sken		 * We don't really need to worry about what LBA range the
878229997Sken		 * user asked to be synced out.  When they issue a sync
879229997Sken		 * cache command, we'll sync out the whole thing.
880229997Sken		 *
881229997Sken		 * This is obviously just a stubbed out implementation.
882229997Sken		 * The real implementation will be in the RAIDCore/CTL
883229997Sken		 * interface, and can only really happen when RAIDCore
884229997Sken		 * implements a per-array cache sync.
885229997Sken		 */
886229997Sken		ctl_set_success(&io->scsiio);
887229997Sken		ctl_config_write_done(io);
888229997Sken		break;
889229997Sken	case START_STOP_UNIT: {
890229997Sken		struct scsi_start_stop_unit *cdb;
891287499Smav		struct ctl_be_lun *cbe_lun;
892229997Sken		struct ctl_be_ramdisk_lun *be_lun;
893229997Sken
894229997Sken		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
895229997Sken
896287499Smav		cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
897229997Sken			CTL_PRIV_BACKEND_LUN].ptr;
898287499Smav		be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun->be_lun;
899229997Sken
900229997Sken		if (cdb->how & SSS_START)
901287499Smav			retval = ctl_start_lun(cbe_lun);
902229997Sken		else {
903287499Smav			retval = ctl_stop_lun(cbe_lun);
904229997Sken#ifdef NEEDTOPORT
905229997Sken			if ((retval == 0)
906229997Sken			 && (cdb->byte2 & SSS_ONOFFLINE))
907287499Smav				retval = ctl_lun_offline(cbe_lun);
908229997Sken#endif
909229997Sken		}
910229997Sken
911229997Sken		/*
912229997Sken		 * In general, the above routines should not fail.  They
913229997Sken		 * just set state for the LUN.  So we've got something
914229997Sken		 * pretty wrong here if we can't start or stop the LUN.
915229997Sken		 */
916229997Sken		if (retval != 0) {
917229997Sken			ctl_set_internal_failure(&io->scsiio,
918229997Sken						 /*sks_valid*/ 1,
919229997Sken						 /*retry_count*/ 0xf051);
920229997Sken			retval = CTL_RETVAL_COMPLETE;
921229997Sken		} else {
922229997Sken			ctl_set_success(&io->scsiio);
923229997Sken		}
924229997Sken		ctl_config_write_done(io);
925229997Sken		break;
926229997Sken	}
927264274Smav	case WRITE_SAME_10:
928264274Smav	case WRITE_SAME_16:
929264274Smav	case UNMAP:
930264274Smav		ctl_set_success(&io->scsiio);
931264274Smav		ctl_config_write_done(io);
932264274Smav		break;
933229997Sken	default:
934229997Sken		ctl_set_invalid_opcode(&io->scsiio);
935229997Sken		ctl_config_write_done(io);
936229997Sken		retval = CTL_RETVAL_COMPLETE;
937229997Sken		break;
938229997Sken	}
939229997Sken
940229997Sken	return (retval);
941229997Sken}
942229997Sken
943229997Skenstatic int
944229997Skenctl_backend_ramdisk_config_read(union ctl_io *io)
945229997Sken{
946275474Smav	int retval = 0;
947275474Smav
948275474Smav	switch (io->scsiio.cdb[0]) {
949275474Smav	case SERVICE_ACTION_IN:
950275474Smav		if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
951275474Smav			/* We have nothing to tell, leave default data. */
952275474Smav			ctl_config_read_done(io);
953275474Smav			retval = CTL_RETVAL_COMPLETE;
954275474Smav			break;
955275474Smav		}
956275474Smav		ctl_set_invalid_field(&io->scsiio,
957275474Smav				      /*sks_valid*/ 1,
958275474Smav				      /*command*/ 1,
959275474Smav				      /*field*/ 1,
960275474Smav				      /*bit_valid*/ 1,
961275474Smav				      /*bit*/ 4);
962275474Smav		ctl_config_read_done(io);
963275474Smav		retval = CTL_RETVAL_COMPLETE;
964275474Smav		break;
965275474Smav	default:
966275474Smav		ctl_set_invalid_opcode(&io->scsiio);
967275474Smav		ctl_config_read_done(io);
968275474Smav		retval = CTL_RETVAL_COMPLETE;
969275474Smav		break;
970275474Smav	}
971275474Smav
972275474Smav	return (retval);
973229997Sken}
974