ctl_backend_ramdisk.c revision 275474
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 275474 2014-12-04 11:34:19Z 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_frontend_internal.h>
66229997Sken#include <cam/ctl/ctl_debug.h>
67229997Sken#include <cam/ctl/ctl_ioctl.h>
68229997Sken#include <cam/ctl/ctl_error.h>
69229997Sken
70229997Skentypedef enum {
71229997Sken	CTL_BE_RAMDISK_LUN_UNCONFIGURED	= 0x01,
72229997Sken	CTL_BE_RAMDISK_LUN_CONFIG_ERR	= 0x02,
73229997Sken	CTL_BE_RAMDISK_LUN_WAITING	= 0x04
74229997Sken} ctl_be_ramdisk_lun_flags;
75229997Sken
76229997Skenstruct ctl_be_ramdisk_lun {
77264886Smav	char lunname[32];
78229997Sken	uint64_t size_bytes;
79229997Sken	uint64_t size_blocks;
80229997Sken	struct ctl_be_ramdisk_softc *softc;
81229997Sken	ctl_be_ramdisk_lun_flags flags;
82229997Sken	STAILQ_ENTRY(ctl_be_ramdisk_lun) links;
83229997Sken	struct ctl_be_lun ctl_be_lun;
84264886Smav	struct taskqueue *io_taskqueue;
85264886Smav	struct task io_task;
86264886Smav	STAILQ_HEAD(, ctl_io_hdr) cont_queue;
87267877Smav	struct mtx_padalign queue_lock;
88229997Sken};
89229997Sken
90229997Skenstruct ctl_be_ramdisk_softc {
91229997Sken	struct mtx lock;
92229997Sken	int rd_size;
93229997Sken#ifdef CTL_RAMDISK_PAGES
94229997Sken	uint8_t **ramdisk_pages;
95229997Sken	int num_pages;
96229997Sken#else
97229997Sken	uint8_t *ramdisk_buffer;
98229997Sken#endif
99229997Sken	int num_luns;
100229997Sken	STAILQ_HEAD(, ctl_be_ramdisk_lun) lun_list;
101229997Sken};
102229997Sken
103229997Skenstatic struct ctl_be_ramdisk_softc rd_softc;
104229997Sken
105229997Skenint ctl_backend_ramdisk_init(void);
106229997Skenvoid ctl_backend_ramdisk_shutdown(void);
107229997Skenstatic int ctl_backend_ramdisk_move_done(union ctl_io *io);
108229997Skenstatic int ctl_backend_ramdisk_submit(union ctl_io *io);
109264886Smavstatic void ctl_backend_ramdisk_continue(union ctl_io *io);
110229997Skenstatic int ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd,
111229997Sken				     caddr_t addr, int flag, struct thread *td);
112229997Skenstatic int ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
113229997Sken				  struct ctl_lun_req *req);
114229997Skenstatic int ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
115229997Sken				      struct ctl_lun_req *req, int do_wait);
116232604Straszstatic int ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
117232604Strasz				  struct ctl_lun_req *req);
118264886Smavstatic void ctl_backend_ramdisk_worker(void *context, int pending);
119229997Skenstatic void ctl_backend_ramdisk_lun_shutdown(void *be_lun);
120229997Skenstatic void ctl_backend_ramdisk_lun_config_status(void *be_lun,
121229997Sken						  ctl_lun_config_status status);
122229997Skenstatic int ctl_backend_ramdisk_config_write(union ctl_io *io);
123229997Skenstatic int ctl_backend_ramdisk_config_read(union ctl_io *io);
124229997Sken
125229997Skenstatic struct ctl_backend_driver ctl_be_ramdisk_driver =
126229997Sken{
127230334Sken	.name = "ramdisk",
128230334Sken	.flags = CTL_BE_FLAG_HAS_CONFIG,
129230334Sken	.init = ctl_backend_ramdisk_init,
130230334Sken	.data_submit = ctl_backend_ramdisk_submit,
131230334Sken	.data_move_done = ctl_backend_ramdisk_move_done,
132230334Sken	.config_read = ctl_backend_ramdisk_config_read,
133230334Sken	.config_write = ctl_backend_ramdisk_config_write,
134230334Sken	.ioctl = ctl_backend_ramdisk_ioctl
135229997Sken};
136229997Sken
137229997SkenMALLOC_DEFINE(M_RAMDISK, "ramdisk", "Memory used for CTL RAMdisk");
138229997SkenCTL_BACKEND_DECLARE(cbr, ctl_be_ramdisk_driver);
139229997Sken
140229997Skenint
141229997Skenctl_backend_ramdisk_init(void)
142229997Sken{
143229997Sken	struct ctl_be_ramdisk_softc *softc;
144229997Sken#ifdef CTL_RAMDISK_PAGES
145240993Strasz	int i;
146229997Sken#endif
147229997Sken
148229997Sken
149229997Sken	softc = &rd_softc;
150229997Sken
151229997Sken	memset(softc, 0, sizeof(*softc));
152229997Sken
153267877Smav	mtx_init(&softc->lock, "ctlramdisk", NULL, MTX_DEF);
154229997Sken
155229997Sken	STAILQ_INIT(&softc->lun_list);
156264886Smav	softc->rd_size = 1024 * 1024;
157229997Sken#ifdef CTL_RAMDISK_PAGES
158229997Sken	softc->num_pages = softc->rd_size / PAGE_SIZE;
159229997Sken	softc->ramdisk_pages = (uint8_t **)malloc(sizeof(uint8_t *) *
160229997Sken						  softc->num_pages, M_RAMDISK,
161229997Sken						  M_WAITOK);
162240993Strasz	for (i = 0; i < softc->num_pages; i++)
163229997Sken		softc->ramdisk_pages[i] = malloc(PAGE_SIZE, M_RAMDISK,M_WAITOK);
164229997Sken#else
165229997Sken	softc->ramdisk_buffer = (uint8_t *)malloc(softc->rd_size, M_RAMDISK,
166229997Sken						  M_WAITOK);
167229997Sken#endif
168229997Sken
169229997Sken	return (0);
170229997Sken}
171229997Sken
172229997Skenvoid
173229997Skenctl_backend_ramdisk_shutdown(void)
174229997Sken{
175229997Sken	struct ctl_be_ramdisk_softc *softc;
176229997Sken	struct ctl_be_ramdisk_lun *lun, *next_lun;
177229997Sken#ifdef CTL_RAMDISK_PAGES
178229997Sken	int i;
179229997Sken#endif
180229997Sken
181229997Sken	softc = &rd_softc;
182229997Sken
183229997Sken	mtx_lock(&softc->lock);
184229997Sken	for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
185229997Sken		/*
186229997Sken		 * Grab the next LUN.  The current LUN may get removed by
187229997Sken		 * ctl_invalidate_lun(), which will call our LUN shutdown
188229997Sken		 * routine, if there is no outstanding I/O for this LUN.
189229997Sken		 */
190229997Sken		next_lun = STAILQ_NEXT(lun, links);
191229997Sken
192229997Sken		/*
193229997Sken		 * Drop our lock here.  Since ctl_invalidate_lun() can call
194229997Sken		 * back into us, this could potentially lead to a recursive
195229997Sken		 * lock of the same mutex, which would cause a hang.
196229997Sken		 */
197229997Sken		mtx_unlock(&softc->lock);
198229997Sken		ctl_disable_lun(&lun->ctl_be_lun);
199229997Sken		ctl_invalidate_lun(&lun->ctl_be_lun);
200229997Sken		mtx_lock(&softc->lock);
201229997Sken	}
202229997Sken	mtx_unlock(&softc->lock);
203229997Sken
204229997Sken#ifdef CTL_RAMDISK_PAGES
205229997Sken	for (i = 0; i < softc->num_pages; i++)
206229997Sken		free(softc->ramdisk_pages[i], M_RAMDISK);
207229997Sken
208229997Sken	free(softc->ramdisk_pages, M_RAMDISK);
209229997Sken#else
210229997Sken	free(softc->ramdisk_buffer, M_RAMDISK);
211229997Sken#endif
212229997Sken
213229997Sken	if (ctl_backend_deregister(&ctl_be_ramdisk_driver) != 0) {
214229997Sken		printf("ctl_backend_ramdisk_shutdown: "
215229997Sken		       "ctl_backend_deregister() failed!\n");
216229997Sken	}
217229997Sken}
218229997Sken
219229997Skenstatic int
220229997Skenctl_backend_ramdisk_move_done(union ctl_io *io)
221229997Sken{
222264886Smav	struct ctl_be_lun *ctl_be_lun;
223264886Smav	struct ctl_be_ramdisk_lun *be_lun;
224229997Sken#ifdef CTL_TIME_IO
225229997Sken	struct bintime cur_bt;
226229997Sken#endif
227229997Sken
228229997Sken	CTL_DEBUG_PRINT(("ctl_backend_ramdisk_move_done\n"));
229264886Smav	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
230264886Smav		CTL_PRIV_BACKEND_LUN].ptr;
231264886Smav	be_lun = (struct ctl_be_ramdisk_lun *)ctl_be_lun->be_lun;
232264886Smav#ifdef CTL_TIME_IO
233264886Smav	getbintime(&cur_bt);
234264886Smav	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
235264886Smav	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
236264886Smav	io->io_hdr.num_dmas++;
237264886Smav#endif
238264886Smav	if (io->scsiio.kern_sg_entries > 0)
239264886Smav		free(io->scsiio.kern_data_ptr, M_RAMDISK);
240264886Smav	io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
241275058Smav	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
242275058Smav		;
243275058Smav	} else if ((io->io_hdr.port_status == 0) &&
244275058Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
245267519Smav		if (io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer > 0) {
246267877Smav			mtx_lock(&be_lun->queue_lock);
247264886Smav			STAILQ_INSERT_TAIL(&be_lun->cont_queue,
248264886Smav			    &io->io_hdr, links);
249267877Smav			mtx_unlock(&be_lun->queue_lock);
250264886Smav			taskqueue_enqueue(be_lun->io_taskqueue,
251264886Smav			    &be_lun->io_task);
252264886Smav			return (0);
253264886Smav		}
254275009Smav		ctl_set_success(&io->scsiio);
255275058Smav	} else if ((io->io_hdr.port_status != 0) &&
256275058Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
257275058Smav	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
258229997Sken		/*
259229997Sken		 * For hardware error sense keys, the sense key
260229997Sken		 * specific value is defined to be a retry count,
261229997Sken		 * but we use it to pass back an internal FETD
262229997Sken		 * error code.  XXX KDM  Hopefully the FETD is only
263229997Sken		 * using 16 bits for an error code, since that's
264229997Sken		 * all the space we have in the sks field.
265229997Sken		 */
266229997Sken		ctl_set_internal_failure(&io->scsiio,
267229997Sken					 /*sks_valid*/ 1,
268229997Sken					 /*retry_count*/
269229997Sken					 io->io_hdr.port_status);
270229997Sken	}
271267537Smav	ctl_data_submit_done(io);
272229997Sken	return(0);
273229997Sken}
274229997Sken
275229997Skenstatic int
276229997Skenctl_backend_ramdisk_submit(union ctl_io *io)
277229997Sken{
278267519Smav	struct ctl_be_lun *ctl_be_lun;
279267537Smav	struct ctl_lba_len_flags *lbalen;
280229997Sken
281267519Smav	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
282267519Smav		CTL_PRIV_BACKEND_LUN].ptr;
283267537Smav	lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
284267537Smav	if (lbalen->flags & CTL_LLF_VERIFY) {
285267537Smav		ctl_set_success(&io->scsiio);
286267537Smav		ctl_data_submit_done(io);
287267537Smav		return (CTL_RETVAL_COMPLETE);
288267537Smav	}
289267519Smav	io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer =
290267519Smav	    lbalen->len * ctl_be_lun->blocksize;
291264886Smav	ctl_backend_ramdisk_continue(io);
292264886Smav	return (CTL_RETVAL_COMPLETE);
293264886Smav}
294229997Sken
295264886Smavstatic void
296264886Smavctl_backend_ramdisk_continue(union ctl_io *io)
297264886Smav{
298264886Smav	struct ctl_be_ramdisk_softc *softc;
299264886Smav	int len, len_filled, sg_filled;
300264886Smav#ifdef CTL_RAMDISK_PAGES
301264886Smav	struct ctl_sg_entry *sg_entries;
302264886Smav	int i;
303264886Smav#endif
304229997Sken
305264886Smav	softc = &rd_softc;
306267519Smav	len = io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer;
307229997Sken#ifdef CTL_RAMDISK_PAGES
308264886Smav	sg_filled = min(btoc(len), softc->num_pages);
309264886Smav	if (sg_filled > 1) {
310229997Sken		io->scsiio.kern_data_ptr = malloc(sizeof(struct ctl_sg_entry) *
311264886Smav						  sg_filled, M_RAMDISK,
312229997Sken						  M_WAITOK);
313229997Sken		sg_entries = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
314264886Smav		for (i = 0, len_filled = 0; i < sg_filled; i++) {
315229997Sken			sg_entries[i].addr = softc->ramdisk_pages[i];
316229997Sken			sg_entries[i].len = ctl_min(PAGE_SIZE,
317229997Sken						    len - len_filled);
318264886Smav			len_filled += sg_entries[i].len;
319229997Sken		}
320264886Smav		io->io_hdr.flags |= CTL_FLAG_KDPTR_SGLIST;
321229997Sken	} else {
322264886Smav		sg_filled = 0;
323264886Smav		len_filled = len;
324229997Sken		io->scsiio.kern_data_ptr = softc->ramdisk_pages[0];
325264886Smav	}
326229997Sken#else
327264886Smav	sg_filled = 0;
328264886Smav	len_filled = min(len, softc->rd_size);
329264886Smav	io->scsiio.kern_data_ptr = softc->ramdisk_buffer;
330264886Smav#endif /* CTL_RAMDISK_PAGES */
331229997Sken
332267514Smav	io->scsiio.be_move_done = ctl_backend_ramdisk_move_done;
333267514Smav	io->scsiio.kern_data_resid = 0;
334264886Smav	io->scsiio.kern_data_len = len_filled;
335264886Smav	io->scsiio.kern_sg_entries = sg_filled;
336264886Smav	io->io_hdr.flags |= CTL_FLAG_ALLOCATED;
337267519Smav	io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer -= len_filled;
338229997Sken#ifdef CTL_TIME_IO
339229997Sken	getbintime(&io->io_hdr.dma_start_bt);
340229997Sken#endif
341229997Sken	ctl_datamove(io);
342264886Smav}
343229997Sken
344264886Smavstatic void
345264886Smavctl_backend_ramdisk_worker(void *context, int pending)
346264886Smav{
347264886Smav	struct ctl_be_ramdisk_softc *softc;
348264886Smav	struct ctl_be_ramdisk_lun *be_lun;
349264886Smav	union ctl_io *io;
350264886Smav
351264886Smav	be_lun = (struct ctl_be_ramdisk_lun *)context;
352264886Smav	softc = be_lun->softc;
353264886Smav
354267877Smav	mtx_lock(&be_lun->queue_lock);
355264886Smav	for (;;) {
356264886Smav		io = (union ctl_io *)STAILQ_FIRST(&be_lun->cont_queue);
357264886Smav		if (io != NULL) {
358264886Smav			STAILQ_REMOVE(&be_lun->cont_queue, &io->io_hdr,
359264886Smav				      ctl_io_hdr, links);
360264886Smav
361267877Smav			mtx_unlock(&be_lun->queue_lock);
362264886Smav
363264886Smav			ctl_backend_ramdisk_continue(io);
364264886Smav
365267877Smav			mtx_lock(&be_lun->queue_lock);
366264886Smav			continue;
367264886Smav		}
368264886Smav
369264886Smav		/*
370264886Smav		 * If we get here, there is no work left in the queues, so
371264886Smav		 * just break out and let the task queue go to sleep.
372264886Smav		 */
373264886Smav		break;
374264886Smav	}
375267877Smav	mtx_unlock(&be_lun->queue_lock);
376229997Sken}
377229997Sken
378229997Skenstatic int
379229997Skenctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
380229997Sken			  int flag, struct thread *td)
381229997Sken{
382229997Sken	struct ctl_be_ramdisk_softc *softc;
383229997Sken	int retval;
384229997Sken
385229997Sken	retval = 0;
386229997Sken	softc = &rd_softc;
387229997Sken
388229997Sken	switch (cmd) {
389229997Sken	case CTL_LUN_REQ: {
390229997Sken		struct ctl_lun_req *lun_req;
391229997Sken
392229997Sken		lun_req = (struct ctl_lun_req *)addr;
393229997Sken
394229997Sken		switch (lun_req->reqtype) {
395229997Sken		case CTL_LUNREQ_CREATE:
396229997Sken			retval = ctl_backend_ramdisk_create(softc, lun_req,
397229997Sken							    /*do_wait*/ 1);
398229997Sken			break;
399229997Sken		case CTL_LUNREQ_RM:
400229997Sken			retval = ctl_backend_ramdisk_rm(softc, lun_req);
401229997Sken			break;
402232604Strasz		case CTL_LUNREQ_MODIFY:
403232604Strasz			retval = ctl_backend_ramdisk_modify(softc, lun_req);
404232604Strasz			break;
405229997Sken		default:
406229997Sken			lun_req->status = CTL_LUN_ERROR;
407229997Sken			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
408229997Sken				 "%s: invalid LUN request type %d", __func__,
409229997Sken				 lun_req->reqtype);
410229997Sken			break;
411229997Sken		}
412229997Sken		break;
413229997Sken	}
414229997Sken	default:
415229997Sken		retval = ENOTTY;
416229997Sken		break;
417229997Sken	}
418229997Sken
419229997Sken	return (retval);
420229997Sken}
421229997Sken
422229997Skenstatic int
423229997Skenctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
424229997Sken		       struct ctl_lun_req *req)
425229997Sken{
426229997Sken	struct ctl_be_ramdisk_lun *be_lun;
427229997Sken	struct ctl_lun_rm_params *params;
428229997Sken	int retval;
429229997Sken
430229997Sken
431229997Sken	retval = 0;
432229997Sken	params = &req->reqdata.rm;
433229997Sken
434229997Sken	be_lun = NULL;
435229997Sken
436229997Sken	mtx_lock(&softc->lock);
437229997Sken
438229997Sken	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
439229997Sken		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
440229997Sken			break;
441229997Sken	}
442229997Sken	mtx_unlock(&softc->lock);
443229997Sken
444229997Sken	if (be_lun == NULL) {
445229997Sken		snprintf(req->error_str, sizeof(req->error_str),
446229997Sken			 "%s: LUN %u is not managed by the ramdisk backend",
447229997Sken			 __func__, params->lun_id);
448229997Sken		goto bailout_error;
449229997Sken	}
450229997Sken
451229997Sken	retval = ctl_disable_lun(&be_lun->ctl_be_lun);
452229997Sken
453229997Sken	if (retval != 0) {
454229997Sken		snprintf(req->error_str, sizeof(req->error_str),
455229997Sken			 "%s: error %d returned from ctl_disable_lun() for "
456229997Sken			 "LUN %d", __func__, retval, params->lun_id);
457229997Sken		goto bailout_error;
458229997Sken	}
459229997Sken
460229997Sken	/*
461229997Sken	 * Set the waiting flag before we invalidate the LUN.  Our shutdown
462229997Sken	 * routine can be called any time after we invalidate the LUN,
463229997Sken	 * and can be called from our context.
464229997Sken	 *
465229997Sken	 * This tells the shutdown routine that we're waiting, or we're
466229997Sken	 * going to wait for the shutdown to happen.
467229997Sken	 */
468229997Sken	mtx_lock(&softc->lock);
469229997Sken	be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
470229997Sken	mtx_unlock(&softc->lock);
471229997Sken
472229997Sken	retval = ctl_invalidate_lun(&be_lun->ctl_be_lun);
473229997Sken	if (retval != 0) {
474229997Sken		snprintf(req->error_str, sizeof(req->error_str),
475229997Sken			 "%s: error %d returned from ctl_invalidate_lun() for "
476229997Sken			 "LUN %d", __func__, retval, params->lun_id);
477252569Smav		mtx_lock(&softc->lock);
478252569Smav		be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
479252569Smav		mtx_unlock(&softc->lock);
480229997Sken		goto bailout_error;
481229997Sken	}
482229997Sken
483229997Sken	mtx_lock(&softc->lock);
484229997Sken
485229997Sken	while ((be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) == 0) {
486229997Sken		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
487229997Sken 		if (retval == EINTR)
488229997Sken			break;
489229997Sken	}
490229997Sken	be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
491229997Sken
492229997Sken	/*
493229997Sken	 * We only remove this LUN from the list and free it (below) if
494229997Sken	 * retval == 0.  If the user interrupted the wait, we just bail out
495229997Sken	 * without actually freeing the LUN.  We let the shutdown routine
496229997Sken	 * free the LUN if that happens.
497229997Sken	 */
498229997Sken	if (retval == 0) {
499229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
500229997Sken			      links);
501229997Sken		softc->num_luns--;
502229997Sken	}
503229997Sken
504229997Sken	mtx_unlock(&softc->lock);
505229997Sken
506264886Smav	if (retval == 0) {
507264886Smav		taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task);
508264886Smav		taskqueue_free(be_lun->io_taskqueue);
509268280Smav		ctl_free_opts(&be_lun->ctl_be_lun.options);
510267877Smav		mtx_destroy(&be_lun->queue_lock);
511229997Sken		free(be_lun, M_RAMDISK);
512264886Smav	}
513229997Sken
514229997Sken	req->status = CTL_LUN_OK;
515229997Sken
516229997Sken	return (retval);
517229997Sken
518229997Skenbailout_error:
519229997Sken	req->status = CTL_LUN_ERROR;
520229997Sken
521229997Sken	return (0);
522229997Sken}
523229997Sken
524229997Skenstatic int
525229997Skenctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
526229997Sken			   struct ctl_lun_req *req, int do_wait)
527229997Sken{
528229997Sken	struct ctl_be_ramdisk_lun *be_lun;
529229997Sken	struct ctl_lun_create_params *params;
530229997Sken	uint32_t blocksize;
531267481Smav	char *value;
532229997Sken	char tmpstr[32];
533267481Smav	int retval, unmap;
534229997Sken
535229997Sken	retval = 0;
536229997Sken	params = &req->reqdata.create;
537229997Sken	if (params->blocksize_bytes != 0)
538229997Sken		blocksize = params->blocksize_bytes;
539229997Sken	else
540229997Sken		blocksize = 512;
541229997Sken
542229997Sken	be_lun = malloc(sizeof(*be_lun), M_RAMDISK, M_ZERO | (do_wait ?
543229997Sken			M_WAITOK : M_NOWAIT));
544229997Sken
545229997Sken	if (be_lun == NULL) {
546229997Sken		snprintf(req->error_str, sizeof(req->error_str),
547229997Sken			 "%s: error allocating %zd bytes", __func__,
548229997Sken			 sizeof(*be_lun));
549229997Sken		goto bailout_error;
550229997Sken	}
551264886Smav	sprintf(be_lun->lunname, "cram%d", softc->num_luns);
552268280Smav	ctl_init_opts(&be_lun->ctl_be_lun.options,
553268280Smav	    req->num_be_args, req->kern_be_args);
554229997Sken
555229997Sken	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
556229997Sken		be_lun->ctl_be_lun.lun_type = params->device_type;
557229997Sken	else
558229997Sken		be_lun->ctl_be_lun.lun_type = T_DIRECT;
559229997Sken
560229997Sken	if (be_lun->ctl_be_lun.lun_type == T_DIRECT) {
561229997Sken
562229997Sken		if (params->lun_size_bytes < blocksize) {
563229997Sken			snprintf(req->error_str, sizeof(req->error_str),
564229997Sken				 "%s: LUN size %ju < blocksize %u", __func__,
565229997Sken				 params->lun_size_bytes, blocksize);
566229997Sken			goto bailout_error;
567229997Sken		}
568229997Sken
569229997Sken		be_lun->size_blocks = params->lun_size_bytes / blocksize;
570229997Sken		be_lun->size_bytes = be_lun->size_blocks * blocksize;
571229997Sken
572229997Sken		be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1;
573229997Sken	} else {
574229997Sken		be_lun->ctl_be_lun.maxlba = 0;
575229997Sken		blocksize = 0;
576229997Sken		be_lun->size_bytes = 0;
577229997Sken		be_lun->size_blocks = 0;
578229997Sken	}
579229997Sken
580229997Sken	be_lun->ctl_be_lun.blocksize = blocksize;
581229997Sken
582229997Sken	/* Tell the user the blocksize we ended up using */
583229997Sken	params->blocksize_bytes = blocksize;
584229997Sken
585229997Sken	/* Tell the user the exact size we ended up using */
586229997Sken	params->lun_size_bytes = be_lun->size_bytes;
587229997Sken
588229997Sken	be_lun->softc = softc;
589229997Sken
590264279Smav	unmap = 0;
591268280Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap");
592267481Smav	if (value != NULL && strcmp(value, "on") == 0)
593267481Smav		unmap = 1;
594254759Strasz
595229997Sken	be_lun->flags = CTL_BE_RAMDISK_LUN_UNCONFIGURED;
596229997Sken	be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY;
597264274Smav	if (unmap)
598264279Smav		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP;
599272734Smav	be_lun->ctl_be_lun.atomicblock = UINT32_MAX;
600229997Sken	be_lun->ctl_be_lun.be_lun = be_lun;
601229997Sken
602229997Sken	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
603229997Sken		be_lun->ctl_be_lun.req_lun_id = params->req_lun_id;
604229997Sken		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ;
605229997Sken	} else
606229997Sken		be_lun->ctl_be_lun.req_lun_id = 0;
607229997Sken
608229997Sken	be_lun->ctl_be_lun.lun_shutdown = ctl_backend_ramdisk_lun_shutdown;
609229997Sken	be_lun->ctl_be_lun.lun_config_status =
610229997Sken		ctl_backend_ramdisk_lun_config_status;
611229997Sken	be_lun->ctl_be_lun.be = &ctl_be_ramdisk_driver;
612229997Sken	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
613229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
614229997Sken			 softc->num_luns);
615229997Sken		strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr,
616229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
617229997Sken			sizeof(tmpstr)));
618229997Sken
619229997Sken		/* Tell the user what we used for a serial number */
620229997Sken		strncpy((char *)params->serial_num, tmpstr,
621229997Sken			ctl_min(sizeof(params->serial_num), sizeof(tmpstr)));
622229997Sken	} else {
623229997Sken		strncpy((char *)be_lun->ctl_be_lun.serial_num,
624229997Sken			params->serial_num,
625229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
626229997Sken			sizeof(params->serial_num)));
627229997Sken	}
628229997Sken	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
629229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
630229997Sken		strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr,
631229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
632229997Sken			sizeof(tmpstr)));
633229997Sken
634229997Sken		/* Tell the user what we used for a device ID */
635229997Sken		strncpy((char *)params->device_id, tmpstr,
636229997Sken			ctl_min(sizeof(params->device_id), sizeof(tmpstr)));
637229997Sken	} else {
638229997Sken		strncpy((char *)be_lun->ctl_be_lun.device_id,
639229997Sken			params->device_id,
640229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
641229997Sken				sizeof(params->device_id)));
642229997Sken	}
643229997Sken
644264886Smav	STAILQ_INIT(&be_lun->cont_queue);
645267877Smav	mtx_init(&be_lun->queue_lock, "cram queue lock", NULL, MTX_DEF);
646264886Smav	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_backend_ramdisk_worker,
647264886Smav	    be_lun);
648264886Smav
649264886Smav	be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
650264886Smav	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
651264886Smav	if (be_lun->io_taskqueue == NULL) {
652264886Smav		snprintf(req->error_str, sizeof(req->error_str),
653264886Smav			 "%s: Unable to create taskqueue", __func__);
654264886Smav		goto bailout_error;
655264886Smav	}
656264886Smav
657264886Smav	retval = taskqueue_start_threads(&be_lun->io_taskqueue,
658264886Smav					 /*num threads*/1,
659264886Smav					 /*priority*/PWAIT,
660264886Smav					 /*thread name*/
661264886Smav					 "%s taskq", be_lun->lunname);
662264886Smav	if (retval != 0)
663264886Smav		goto bailout_error;
664264886Smav
665229997Sken	mtx_lock(&softc->lock);
666229997Sken	softc->num_luns++;
667229997Sken	STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
668229997Sken
669229997Sken	mtx_unlock(&softc->lock);
670229997Sken
671229997Sken	retval = ctl_add_lun(&be_lun->ctl_be_lun);
672229997Sken	if (retval != 0) {
673229997Sken		mtx_lock(&softc->lock);
674229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
675229997Sken			      links);
676229997Sken		softc->num_luns--;
677229997Sken		mtx_unlock(&softc->lock);
678229997Sken		snprintf(req->error_str, sizeof(req->error_str),
679229997Sken			 "%s: ctl_add_lun() returned error %d, see dmesg for "
680229997Sken			"details", __func__, retval);
681229997Sken		retval = 0;
682229997Sken		goto bailout_error;
683229997Sken	}
684229997Sken
685229997Sken	if (do_wait == 0)
686229997Sken		return (retval);
687229997Sken
688229997Sken	mtx_lock(&softc->lock);
689229997Sken
690229997Sken	/*
691229997Sken	 * Tell the config_status routine that we're waiting so it won't
692229997Sken	 * clean up the LUN in the event of an error.
693229997Sken	 */
694229997Sken	be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
695229997Sken
696229997Sken	while (be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) {
697229997Sken		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
698229997Sken		if (retval == EINTR)
699229997Sken			break;
700229997Sken	}
701229997Sken	be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
702229997Sken
703229997Sken	if (be_lun->flags & CTL_BE_RAMDISK_LUN_CONFIG_ERR) {
704229997Sken		snprintf(req->error_str, sizeof(req->error_str),
705229997Sken			 "%s: LUN configuration error, see dmesg for details",
706229997Sken			 __func__);
707229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
708229997Sken			      links);
709229997Sken		softc->num_luns--;
710229997Sken		mtx_unlock(&softc->lock);
711229997Sken		goto bailout_error;
712229997Sken	} else {
713229997Sken		params->req_lun_id = be_lun->ctl_be_lun.lun_id;
714229997Sken	}
715229997Sken	mtx_unlock(&softc->lock);
716229997Sken
717229997Sken	req->status = CTL_LUN_OK;
718229997Sken
719229997Sken	return (retval);
720229997Sken
721229997Skenbailout_error:
722229997Sken	req->status = CTL_LUN_ERROR;
723264886Smav	if (be_lun != NULL) {
724264886Smav		if (be_lun->io_taskqueue != NULL) {
725264886Smav			taskqueue_free(be_lun->io_taskqueue);
726264886Smav		}
727268280Smav		ctl_free_opts(&be_lun->ctl_be_lun.options);
728267877Smav		mtx_destroy(&be_lun->queue_lock);
729264886Smav		free(be_lun, M_RAMDISK);
730264886Smav	}
731229997Sken
732229997Sken	return (retval);
733229997Sken}
734229997Sken
735232604Straszstatic int
736232604Straszctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
737232604Strasz		       struct ctl_lun_req *req)
738232604Strasz{
739232604Strasz	struct ctl_be_ramdisk_lun *be_lun;
740232604Strasz	struct ctl_lun_modify_params *params;
741232604Strasz	uint32_t blocksize;
742232604Strasz
743232604Strasz	params = &req->reqdata.modify;
744232604Strasz
745232604Strasz	be_lun = NULL;
746232604Strasz
747232604Strasz	mtx_lock(&softc->lock);
748232604Strasz	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
749232604Strasz		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
750232604Strasz			break;
751232604Strasz	}
752232604Strasz	mtx_unlock(&softc->lock);
753232604Strasz
754232604Strasz	if (be_lun == NULL) {
755232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
756232604Strasz			 "%s: LUN %u is not managed by the ramdisk backend",
757232604Strasz			 __func__, params->lun_id);
758232604Strasz		goto bailout_error;
759232604Strasz	}
760232604Strasz
761232604Strasz	if (params->lun_size_bytes == 0) {
762232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
763232604Strasz			"%s: LUN size \"auto\" not supported "
764232604Strasz			"by the ramdisk backend", __func__);
765232604Strasz		goto bailout_error;
766232604Strasz	}
767232604Strasz
768232604Strasz	blocksize = be_lun->ctl_be_lun.blocksize;
769232604Strasz
770232604Strasz	if (params->lun_size_bytes < blocksize) {
771232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
772232604Strasz			"%s: LUN size %ju < blocksize %u", __func__,
773232604Strasz			params->lun_size_bytes, blocksize);
774232604Strasz		goto bailout_error;
775232604Strasz	}
776232604Strasz
777232604Strasz	be_lun->size_blocks = params->lun_size_bytes / blocksize;
778232604Strasz	be_lun->size_bytes = be_lun->size_blocks * blocksize;
779232604Strasz
780232604Strasz	/*
781232604Strasz	 * The maximum LBA is the size - 1.
782232604Strasz	 *
783232604Strasz	 * XXX: Note that this field is being updated without locking,
784232604Strasz	 * 	which might cause problems on 32-bit architectures.
785232604Strasz	 */
786232604Strasz	be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1;
787232604Strasz	ctl_lun_capacity_changed(&be_lun->ctl_be_lun);
788232604Strasz
789232604Strasz	/* Tell the user the exact size we ended up using */
790232604Strasz	params->lun_size_bytes = be_lun->size_bytes;
791232604Strasz
792232604Strasz	req->status = CTL_LUN_OK;
793232604Strasz
794232604Strasz	return (0);
795232604Strasz
796232604Straszbailout_error:
797232604Strasz	req->status = CTL_LUN_ERROR;
798232604Strasz
799232604Strasz	return (0);
800232604Strasz}
801232604Strasz
802229997Skenstatic void
803229997Skenctl_backend_ramdisk_lun_shutdown(void *be_lun)
804229997Sken{
805229997Sken	struct ctl_be_ramdisk_lun *lun;
806229997Sken	struct ctl_be_ramdisk_softc *softc;
807229997Sken	int do_free;
808229997Sken
809229997Sken	lun = (struct ctl_be_ramdisk_lun *)be_lun;
810229997Sken	softc = lun->softc;
811229997Sken	do_free = 0;
812229997Sken
813229997Sken	mtx_lock(&softc->lock);
814229997Sken
815229997Sken	lun->flags |= CTL_BE_RAMDISK_LUN_UNCONFIGURED;
816229997Sken
817229997Sken	if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
818229997Sken		wakeup(lun);
819229997Sken	} else {
820269058Smav		STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
821229997Sken			      links);
822229997Sken		softc->num_luns--;
823229997Sken		do_free = 1;
824229997Sken	}
825229997Sken
826229997Sken	mtx_unlock(&softc->lock);
827229997Sken
828229997Sken	if (do_free != 0)
829229997Sken		free(be_lun, M_RAMDISK);
830229997Sken}
831229997Sken
832229997Skenstatic void
833229997Skenctl_backend_ramdisk_lun_config_status(void *be_lun,
834229997Sken				      ctl_lun_config_status status)
835229997Sken{
836229997Sken	struct ctl_be_ramdisk_lun *lun;
837229997Sken	struct ctl_be_ramdisk_softc *softc;
838229997Sken
839229997Sken	lun = (struct ctl_be_ramdisk_lun *)be_lun;
840229997Sken	softc = lun->softc;
841229997Sken
842229997Sken	if (status == CTL_LUN_CONFIG_OK) {
843229997Sken		mtx_lock(&softc->lock);
844229997Sken		lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
845229997Sken		if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING)
846229997Sken			wakeup(lun);
847229997Sken		mtx_unlock(&softc->lock);
848229997Sken
849229997Sken		/*
850229997Sken		 * We successfully added the LUN, attempt to enable it.
851229997Sken		 */
852229997Sken		if (ctl_enable_lun(&lun->ctl_be_lun) != 0) {
853229997Sken			printf("%s: ctl_enable_lun() failed!\n", __func__);
854229997Sken			if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) {
855229997Sken				printf("%s: ctl_invalidate_lun() failed!\n",
856229997Sken				       __func__);
857229997Sken			}
858229997Sken		}
859229997Sken
860229997Sken		return;
861229997Sken	}
862229997Sken
863229997Sken
864229997Sken	mtx_lock(&softc->lock);
865229997Sken	lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
866229997Sken
867229997Sken	/*
868229997Sken	 * If we have a user waiting, let him handle the cleanup.  If not,
869229997Sken	 * clean things up here.
870229997Sken	 */
871229997Sken	if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
872229997Sken		lun->flags |= CTL_BE_RAMDISK_LUN_CONFIG_ERR;
873229997Sken		wakeup(lun);
874229997Sken	} else {
875229997Sken		STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
876229997Sken			      links);
877229997Sken		softc->num_luns--;
878229997Sken		free(lun, M_RAMDISK);
879229997Sken	}
880229997Sken	mtx_unlock(&softc->lock);
881229997Sken}
882229997Sken
883229997Skenstatic int
884229997Skenctl_backend_ramdisk_config_write(union ctl_io *io)
885229997Sken{
886229997Sken	struct ctl_be_ramdisk_softc *softc;
887229997Sken	int retval;
888229997Sken
889229997Sken	retval = 0;
890229997Sken	softc = &rd_softc;
891229997Sken
892229997Sken	switch (io->scsiio.cdb[0]) {
893229997Sken	case SYNCHRONIZE_CACHE:
894229997Sken	case SYNCHRONIZE_CACHE_16:
895229997Sken		/*
896229997Sken		 * The upper level CTL code will filter out any CDBs with
897229997Sken		 * the immediate bit set and return the proper error.  It
898229997Sken		 * will also not allow a sync cache command to go to a LUN
899229997Sken		 * that is powered down.
900229997Sken		 *
901229997Sken		 * We don't really need to worry about what LBA range the
902229997Sken		 * user asked to be synced out.  When they issue a sync
903229997Sken		 * cache command, we'll sync out the whole thing.
904229997Sken		 *
905229997Sken		 * This is obviously just a stubbed out implementation.
906229997Sken		 * The real implementation will be in the RAIDCore/CTL
907229997Sken		 * interface, and can only really happen when RAIDCore
908229997Sken		 * implements a per-array cache sync.
909229997Sken		 */
910229997Sken		ctl_set_success(&io->scsiio);
911229997Sken		ctl_config_write_done(io);
912229997Sken		break;
913229997Sken	case START_STOP_UNIT: {
914229997Sken		struct scsi_start_stop_unit *cdb;
915229997Sken		struct ctl_be_lun *ctl_be_lun;
916229997Sken		struct ctl_be_ramdisk_lun *be_lun;
917229997Sken
918229997Sken		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
919229997Sken
920229997Sken		ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
921229997Sken			CTL_PRIV_BACKEND_LUN].ptr;
922229997Sken		be_lun = (struct ctl_be_ramdisk_lun *)ctl_be_lun->be_lun;
923229997Sken
924229997Sken		if (cdb->how & SSS_START)
925229997Sken			retval = ctl_start_lun(ctl_be_lun);
926229997Sken		else {
927229997Sken			retval = ctl_stop_lun(ctl_be_lun);
928229997Sken#ifdef NEEDTOPORT
929229997Sken			if ((retval == 0)
930229997Sken			 && (cdb->byte2 & SSS_ONOFFLINE))
931229997Sken				retval = ctl_lun_offline(ctl_be_lun);
932229997Sken#endif
933229997Sken		}
934229997Sken
935229997Sken		/*
936229997Sken		 * In general, the above routines should not fail.  They
937229997Sken		 * just set state for the LUN.  So we've got something
938229997Sken		 * pretty wrong here if we can't start or stop the LUN.
939229997Sken		 */
940229997Sken		if (retval != 0) {
941229997Sken			ctl_set_internal_failure(&io->scsiio,
942229997Sken						 /*sks_valid*/ 1,
943229997Sken						 /*retry_count*/ 0xf051);
944229997Sken			retval = CTL_RETVAL_COMPLETE;
945229997Sken		} else {
946229997Sken			ctl_set_success(&io->scsiio);
947229997Sken		}
948229997Sken		ctl_config_write_done(io);
949229997Sken		break;
950229997Sken	}
951264274Smav	case WRITE_SAME_10:
952264274Smav	case WRITE_SAME_16:
953264274Smav	case UNMAP:
954264274Smav		ctl_set_success(&io->scsiio);
955264274Smav		ctl_config_write_done(io);
956264274Smav		break;
957229997Sken	default:
958229997Sken		ctl_set_invalid_opcode(&io->scsiio);
959229997Sken		ctl_config_write_done(io);
960229997Sken		retval = CTL_RETVAL_COMPLETE;
961229997Sken		break;
962229997Sken	}
963229997Sken
964229997Sken	return (retval);
965229997Sken}
966229997Sken
967229997Skenstatic int
968229997Skenctl_backend_ramdisk_config_read(union ctl_io *io)
969229997Sken{
970275474Smav	int retval = 0;
971275474Smav
972275474Smav	switch (io->scsiio.cdb[0]) {
973275474Smav	case SERVICE_ACTION_IN:
974275474Smav		if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
975275474Smav			/* We have nothing to tell, leave default data. */
976275474Smav			ctl_config_read_done(io);
977275474Smav			retval = CTL_RETVAL_COMPLETE;
978275474Smav			break;
979275474Smav		}
980275474Smav		ctl_set_invalid_field(&io->scsiio,
981275474Smav				      /*sks_valid*/ 1,
982275474Smav				      /*command*/ 1,
983275474Smav				      /*field*/ 1,
984275474Smav				      /*bit_valid*/ 1,
985275474Smav				      /*bit*/ 4);
986275474Smav		ctl_config_read_done(io);
987275474Smav		retval = CTL_RETVAL_COMPLETE;
988275474Smav		break;
989275474Smav	default:
990275474Smav		ctl_set_invalid_opcode(&io->scsiio);
991275474Smav		ctl_config_read_done(io);
992275474Smav		retval = CTL_RETVAL_COMPLETE;
993275474Smav		break;
994275474Smav	}
995275474Smav
996275474Smav	return (retval);
997229997Sken}
998