ctl_backend_ramdisk.c revision 287670
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 287670 2015-09-11 14:33:05Z 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>
59287621Smav#include <sys/sysctl.h>
60229997Sken
61229997Sken#include <cam/scsi/scsi_all.h>
62287621Smav#include <cam/scsi/scsi_da.h>
63229997Sken#include <cam/ctl/ctl_io.h>
64229997Sken#include <cam/ctl/ctl.h>
65229997Sken#include <cam/ctl/ctl_util.h>
66229997Sken#include <cam/ctl/ctl_backend.h>
67229997Sken#include <cam/ctl/ctl_debug.h>
68229997Sken#include <cam/ctl/ctl_ioctl.h>
69287621Smav#include <cam/ctl/ctl_ha.h>
70287621Smav#include <cam/ctl/ctl_private.h>
71229997Sken#include <cam/ctl/ctl_error.h>
72229997Sken
73229997Skentypedef enum {
74229997Sken	CTL_BE_RAMDISK_LUN_UNCONFIGURED	= 0x01,
75229997Sken	CTL_BE_RAMDISK_LUN_CONFIG_ERR	= 0x02,
76229997Sken	CTL_BE_RAMDISK_LUN_WAITING	= 0x04
77229997Sken} ctl_be_ramdisk_lun_flags;
78229997Sken
79229997Skenstruct ctl_be_ramdisk_lun {
80287500Smav	struct ctl_lun_create_params params;
81264886Smav	char lunname[32];
82229997Sken	uint64_t size_bytes;
83229997Sken	uint64_t size_blocks;
84229997Sken	struct ctl_be_ramdisk_softc *softc;
85229997Sken	ctl_be_ramdisk_lun_flags flags;
86229997Sken	STAILQ_ENTRY(ctl_be_ramdisk_lun) links;
87287499Smav	struct ctl_be_lun cbe_lun;
88264886Smav	struct taskqueue *io_taskqueue;
89264886Smav	struct task io_task;
90264886Smav	STAILQ_HEAD(, ctl_io_hdr) cont_queue;
91267877Smav	struct mtx_padalign queue_lock;
92229997Sken};
93229997Sken
94229997Skenstruct ctl_be_ramdisk_softc {
95229997Sken	struct mtx lock;
96229997Sken	int rd_size;
97229997Sken#ifdef CTL_RAMDISK_PAGES
98229997Sken	uint8_t **ramdisk_pages;
99229997Sken	int num_pages;
100229997Sken#else
101229997Sken	uint8_t *ramdisk_buffer;
102229997Sken#endif
103229997Sken	int num_luns;
104229997Sken	STAILQ_HEAD(, ctl_be_ramdisk_lun) lun_list;
105229997Sken};
106229997Sken
107229997Skenstatic struct ctl_be_ramdisk_softc rd_softc;
108287621Smavextern struct ctl_softc *control_softc;
109229997Sken
110229997Skenint ctl_backend_ramdisk_init(void);
111229997Skenvoid ctl_backend_ramdisk_shutdown(void);
112229997Skenstatic int ctl_backend_ramdisk_move_done(union ctl_io *io);
113229997Skenstatic int ctl_backend_ramdisk_submit(union ctl_io *io);
114264886Smavstatic void ctl_backend_ramdisk_continue(union ctl_io *io);
115229997Skenstatic int ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd,
116229997Sken				     caddr_t addr, int flag, struct thread *td);
117229997Skenstatic int ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
118229997Sken				  struct ctl_lun_req *req);
119229997Skenstatic int ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
120287499Smav				      struct ctl_lun_req *req);
121232604Straszstatic int ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
122232604Strasz				  struct ctl_lun_req *req);
123264886Smavstatic void ctl_backend_ramdisk_worker(void *context, int pending);
124229997Skenstatic void ctl_backend_ramdisk_lun_shutdown(void *be_lun);
125229997Skenstatic void ctl_backend_ramdisk_lun_config_status(void *be_lun,
126229997Sken						  ctl_lun_config_status status);
127229997Skenstatic int ctl_backend_ramdisk_config_write(union ctl_io *io);
128229997Skenstatic int ctl_backend_ramdisk_config_read(union ctl_io *io);
129229997Sken
130229997Skenstatic struct ctl_backend_driver ctl_be_ramdisk_driver =
131229997Sken{
132230334Sken	.name = "ramdisk",
133230334Sken	.flags = CTL_BE_FLAG_HAS_CONFIG,
134230334Sken	.init = ctl_backend_ramdisk_init,
135230334Sken	.data_submit = ctl_backend_ramdisk_submit,
136230334Sken	.data_move_done = ctl_backend_ramdisk_move_done,
137230334Sken	.config_read = ctl_backend_ramdisk_config_read,
138230334Sken	.config_write = ctl_backend_ramdisk_config_write,
139230334Sken	.ioctl = ctl_backend_ramdisk_ioctl
140229997Sken};
141229997Sken
142229997SkenMALLOC_DEFINE(M_RAMDISK, "ramdisk", "Memory used for CTL RAMdisk");
143229997SkenCTL_BACKEND_DECLARE(cbr, ctl_be_ramdisk_driver);
144229997Sken
145229997Skenint
146229997Skenctl_backend_ramdisk_init(void)
147229997Sken{
148229997Sken	struct ctl_be_ramdisk_softc *softc;
149229997Sken#ifdef CTL_RAMDISK_PAGES
150240993Strasz	int i;
151229997Sken#endif
152229997Sken
153229997Sken
154229997Sken	softc = &rd_softc;
155229997Sken
156229997Sken	memset(softc, 0, sizeof(*softc));
157229997Sken
158267877Smav	mtx_init(&softc->lock, "ctlramdisk", NULL, MTX_DEF);
159229997Sken
160229997Sken	STAILQ_INIT(&softc->lun_list);
161264886Smav	softc->rd_size = 1024 * 1024;
162229997Sken#ifdef CTL_RAMDISK_PAGES
163229997Sken	softc->num_pages = softc->rd_size / PAGE_SIZE;
164229997Sken	softc->ramdisk_pages = (uint8_t **)malloc(sizeof(uint8_t *) *
165229997Sken						  softc->num_pages, M_RAMDISK,
166229997Sken						  M_WAITOK);
167240993Strasz	for (i = 0; i < softc->num_pages; i++)
168229997Sken		softc->ramdisk_pages[i] = malloc(PAGE_SIZE, M_RAMDISK,M_WAITOK);
169229997Sken#else
170229997Sken	softc->ramdisk_buffer = (uint8_t *)malloc(softc->rd_size, M_RAMDISK,
171229997Sken						  M_WAITOK);
172229997Sken#endif
173229997Sken
174229997Sken	return (0);
175229997Sken}
176229997Sken
177229997Skenvoid
178229997Skenctl_backend_ramdisk_shutdown(void)
179229997Sken{
180229997Sken	struct ctl_be_ramdisk_softc *softc;
181229997Sken	struct ctl_be_ramdisk_lun *lun, *next_lun;
182229997Sken#ifdef CTL_RAMDISK_PAGES
183229997Sken	int i;
184229997Sken#endif
185229997Sken
186229997Sken	softc = &rd_softc;
187229997Sken
188229997Sken	mtx_lock(&softc->lock);
189229997Sken	for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
190229997Sken		/*
191229997Sken		 * Grab the next LUN.  The current LUN may get removed by
192229997Sken		 * ctl_invalidate_lun(), which will call our LUN shutdown
193229997Sken		 * routine, if there is no outstanding I/O for this LUN.
194229997Sken		 */
195229997Sken		next_lun = STAILQ_NEXT(lun, links);
196229997Sken
197229997Sken		/*
198229997Sken		 * Drop our lock here.  Since ctl_invalidate_lun() can call
199229997Sken		 * back into us, this could potentially lead to a recursive
200229997Sken		 * lock of the same mutex, which would cause a hang.
201229997Sken		 */
202229997Sken		mtx_unlock(&softc->lock);
203287499Smav		ctl_disable_lun(&lun->cbe_lun);
204287499Smav		ctl_invalidate_lun(&lun->cbe_lun);
205229997Sken		mtx_lock(&softc->lock);
206229997Sken	}
207229997Sken	mtx_unlock(&softc->lock);
208229997Sken
209229997Sken#ifdef CTL_RAMDISK_PAGES
210229997Sken	for (i = 0; i < softc->num_pages; i++)
211229997Sken		free(softc->ramdisk_pages[i], M_RAMDISK);
212229997Sken
213229997Sken	free(softc->ramdisk_pages, M_RAMDISK);
214229997Sken#else
215229997Sken	free(softc->ramdisk_buffer, M_RAMDISK);
216229997Sken#endif
217229997Sken
218229997Sken	if (ctl_backend_deregister(&ctl_be_ramdisk_driver) != 0) {
219229997Sken		printf("ctl_backend_ramdisk_shutdown: "
220229997Sken		       "ctl_backend_deregister() failed!\n");
221229997Sken	}
222229997Sken}
223229997Sken
224229997Skenstatic int
225229997Skenctl_backend_ramdisk_move_done(union ctl_io *io)
226229997Sken{
227287499Smav	struct ctl_be_lun *cbe_lun;
228264886Smav	struct ctl_be_ramdisk_lun *be_lun;
229229997Sken#ifdef CTL_TIME_IO
230229997Sken	struct bintime cur_bt;
231229997Sken#endif
232229997Sken
233229997Sken	CTL_DEBUG_PRINT(("ctl_backend_ramdisk_move_done\n"));
234287499Smav	cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
235264886Smav		CTL_PRIV_BACKEND_LUN].ptr;
236287499Smav	be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun->be_lun;
237264886Smav#ifdef CTL_TIME_IO
238264886Smav	getbintime(&cur_bt);
239264886Smav	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
240264886Smav	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
241264886Smav	io->io_hdr.num_dmas++;
242264886Smav#endif
243264886Smav	if (io->scsiio.kern_sg_entries > 0)
244264886Smav		free(io->scsiio.kern_data_ptr, M_RAMDISK);
245264886Smav	io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
246275058Smav	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
247275058Smav		;
248275058Smav	} else if ((io->io_hdr.port_status == 0) &&
249275058Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
250267519Smav		if (io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer > 0) {
251267877Smav			mtx_lock(&be_lun->queue_lock);
252264886Smav			STAILQ_INSERT_TAIL(&be_lun->cont_queue,
253264886Smav			    &io->io_hdr, links);
254267877Smav			mtx_unlock(&be_lun->queue_lock);
255264886Smav			taskqueue_enqueue(be_lun->io_taskqueue,
256264886Smav			    &be_lun->io_task);
257264886Smav			return (0);
258264886Smav		}
259275009Smav		ctl_set_success(&io->scsiio);
260275058Smav	} else if ((io->io_hdr.port_status != 0) &&
261275058Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
262275058Smav	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
263229997Sken		/*
264229997Sken		 * For hardware error sense keys, the sense key
265229997Sken		 * specific value is defined to be a retry count,
266229997Sken		 * but we use it to pass back an internal FETD
267229997Sken		 * error code.  XXX KDM  Hopefully the FETD is only
268229997Sken		 * using 16 bits for an error code, since that's
269229997Sken		 * all the space we have in the sks field.
270229997Sken		 */
271229997Sken		ctl_set_internal_failure(&io->scsiio,
272229997Sken					 /*sks_valid*/ 1,
273229997Sken					 /*retry_count*/
274229997Sken					 io->io_hdr.port_status);
275229997Sken	}
276267537Smav	ctl_data_submit_done(io);
277229997Sken	return(0);
278229997Sken}
279229997Sken
280229997Skenstatic int
281229997Skenctl_backend_ramdisk_submit(union ctl_io *io)
282229997Sken{
283287499Smav	struct ctl_be_lun *cbe_lun;
284267537Smav	struct ctl_lba_len_flags *lbalen;
285229997Sken
286287499Smav	cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
287267519Smav		CTL_PRIV_BACKEND_LUN].ptr;
288267537Smav	lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
289267537Smav	if (lbalen->flags & CTL_LLF_VERIFY) {
290267537Smav		ctl_set_success(&io->scsiio);
291267537Smav		ctl_data_submit_done(io);
292267537Smav		return (CTL_RETVAL_COMPLETE);
293267537Smav	}
294267519Smav	io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer =
295287499Smav	    lbalen->len * cbe_lun->blocksize;
296264886Smav	ctl_backend_ramdisk_continue(io);
297264886Smav	return (CTL_RETVAL_COMPLETE);
298264886Smav}
299229997Sken
300264886Smavstatic void
301264886Smavctl_backend_ramdisk_continue(union ctl_io *io)
302264886Smav{
303264886Smav	struct ctl_be_ramdisk_softc *softc;
304264886Smav	int len, len_filled, sg_filled;
305264886Smav#ifdef CTL_RAMDISK_PAGES
306264886Smav	struct ctl_sg_entry *sg_entries;
307264886Smav	int i;
308264886Smav#endif
309229997Sken
310264886Smav	softc = &rd_softc;
311267519Smav	len = io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer;
312229997Sken#ifdef CTL_RAMDISK_PAGES
313264886Smav	sg_filled = min(btoc(len), softc->num_pages);
314264886Smav	if (sg_filled > 1) {
315229997Sken		io->scsiio.kern_data_ptr = malloc(sizeof(struct ctl_sg_entry) *
316264886Smav						  sg_filled, M_RAMDISK,
317229997Sken						  M_WAITOK);
318229997Sken		sg_entries = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
319264886Smav		for (i = 0, len_filled = 0; i < sg_filled; i++) {
320229997Sken			sg_entries[i].addr = softc->ramdisk_pages[i];
321275953Smav			sg_entries[i].len = MIN(PAGE_SIZE, len - len_filled);
322264886Smav			len_filled += sg_entries[i].len;
323229997Sken		}
324264886Smav		io->io_hdr.flags |= CTL_FLAG_KDPTR_SGLIST;
325229997Sken	} else {
326264886Smav		sg_filled = 0;
327264886Smav		len_filled = len;
328229997Sken		io->scsiio.kern_data_ptr = softc->ramdisk_pages[0];
329264886Smav	}
330229997Sken#else
331264886Smav	sg_filled = 0;
332264886Smav	len_filled = min(len, softc->rd_size);
333264886Smav	io->scsiio.kern_data_ptr = softc->ramdisk_buffer;
334264886Smav#endif /* CTL_RAMDISK_PAGES */
335229997Sken
336267514Smav	io->scsiio.be_move_done = ctl_backend_ramdisk_move_done;
337267514Smav	io->scsiio.kern_data_resid = 0;
338264886Smav	io->scsiio.kern_data_len = len_filled;
339264886Smav	io->scsiio.kern_sg_entries = sg_filled;
340264886Smav	io->io_hdr.flags |= CTL_FLAG_ALLOCATED;
341267519Smav	io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer -= len_filled;
342229997Sken#ifdef CTL_TIME_IO
343229997Sken	getbintime(&io->io_hdr.dma_start_bt);
344229997Sken#endif
345229997Sken	ctl_datamove(io);
346264886Smav}
347229997Sken
348264886Smavstatic void
349264886Smavctl_backend_ramdisk_worker(void *context, int pending)
350264886Smav{
351264886Smav	struct ctl_be_ramdisk_softc *softc;
352264886Smav	struct ctl_be_ramdisk_lun *be_lun;
353264886Smav	union ctl_io *io;
354264886Smav
355264886Smav	be_lun = (struct ctl_be_ramdisk_lun *)context;
356264886Smav	softc = be_lun->softc;
357264886Smav
358267877Smav	mtx_lock(&be_lun->queue_lock);
359264886Smav	for (;;) {
360264886Smav		io = (union ctl_io *)STAILQ_FIRST(&be_lun->cont_queue);
361264886Smav		if (io != NULL) {
362264886Smav			STAILQ_REMOVE(&be_lun->cont_queue, &io->io_hdr,
363264886Smav				      ctl_io_hdr, links);
364264886Smav
365267877Smav			mtx_unlock(&be_lun->queue_lock);
366264886Smav
367264886Smav			ctl_backend_ramdisk_continue(io);
368264886Smav
369267877Smav			mtx_lock(&be_lun->queue_lock);
370264886Smav			continue;
371264886Smav		}
372264886Smav
373264886Smav		/*
374264886Smav		 * If we get here, there is no work left in the queues, so
375264886Smav		 * just break out and let the task queue go to sleep.
376264886Smav		 */
377264886Smav		break;
378264886Smav	}
379267877Smav	mtx_unlock(&be_lun->queue_lock);
380229997Sken}
381229997Sken
382229997Skenstatic int
383229997Skenctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
384229997Sken			  int flag, struct thread *td)
385229997Sken{
386229997Sken	struct ctl_be_ramdisk_softc *softc;
387229997Sken	int retval;
388229997Sken
389229997Sken	retval = 0;
390229997Sken	softc = &rd_softc;
391229997Sken
392229997Sken	switch (cmd) {
393229997Sken	case CTL_LUN_REQ: {
394229997Sken		struct ctl_lun_req *lun_req;
395229997Sken
396229997Sken		lun_req = (struct ctl_lun_req *)addr;
397229997Sken
398229997Sken		switch (lun_req->reqtype) {
399229997Sken		case CTL_LUNREQ_CREATE:
400287499Smav			retval = ctl_backend_ramdisk_create(softc, lun_req);
401229997Sken			break;
402229997Sken		case CTL_LUNREQ_RM:
403229997Sken			retval = ctl_backend_ramdisk_rm(softc, lun_req);
404229997Sken			break;
405232604Strasz		case CTL_LUNREQ_MODIFY:
406232604Strasz			retval = ctl_backend_ramdisk_modify(softc, lun_req);
407232604Strasz			break;
408229997Sken		default:
409229997Sken			lun_req->status = CTL_LUN_ERROR;
410229997Sken			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
411229997Sken				 "%s: invalid LUN request type %d", __func__,
412229997Sken				 lun_req->reqtype);
413229997Sken			break;
414229997Sken		}
415229997Sken		break;
416229997Sken	}
417229997Sken	default:
418229997Sken		retval = ENOTTY;
419229997Sken		break;
420229997Sken	}
421229997Sken
422229997Sken	return (retval);
423229997Sken}
424229997Sken
425229997Skenstatic int
426229997Skenctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
427229997Sken		       struct ctl_lun_req *req)
428229997Sken{
429229997Sken	struct ctl_be_ramdisk_lun *be_lun;
430229997Sken	struct ctl_lun_rm_params *params;
431229997Sken	int retval;
432229997Sken
433229997Sken
434229997Sken	retval = 0;
435229997Sken	params = &req->reqdata.rm;
436229997Sken
437229997Sken	be_lun = NULL;
438229997Sken
439229997Sken	mtx_lock(&softc->lock);
440229997Sken
441229997Sken	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
442287499Smav		if (be_lun->cbe_lun.lun_id == params->lun_id)
443229997Sken			break;
444229997Sken	}
445229997Sken	mtx_unlock(&softc->lock);
446229997Sken
447229997Sken	if (be_lun == NULL) {
448229997Sken		snprintf(req->error_str, sizeof(req->error_str),
449229997Sken			 "%s: LUN %u is not managed by the ramdisk backend",
450229997Sken			 __func__, params->lun_id);
451229997Sken		goto bailout_error;
452229997Sken	}
453229997Sken
454287499Smav	retval = ctl_disable_lun(&be_lun->cbe_lun);
455229997Sken
456229997Sken	if (retval != 0) {
457229997Sken		snprintf(req->error_str, sizeof(req->error_str),
458229997Sken			 "%s: error %d returned from ctl_disable_lun() for "
459229997Sken			 "LUN %d", __func__, retval, params->lun_id);
460229997Sken		goto bailout_error;
461229997Sken	}
462229997Sken
463229997Sken	/*
464229997Sken	 * Set the waiting flag before we invalidate the LUN.  Our shutdown
465229997Sken	 * routine can be called any time after we invalidate the LUN,
466229997Sken	 * and can be called from our context.
467229997Sken	 *
468229997Sken	 * This tells the shutdown routine that we're waiting, or we're
469229997Sken	 * going to wait for the shutdown to happen.
470229997Sken	 */
471229997Sken	mtx_lock(&softc->lock);
472229997Sken	be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
473229997Sken	mtx_unlock(&softc->lock);
474229997Sken
475287499Smav	retval = ctl_invalidate_lun(&be_lun->cbe_lun);
476229997Sken	if (retval != 0) {
477229997Sken		snprintf(req->error_str, sizeof(req->error_str),
478229997Sken			 "%s: error %d returned from ctl_invalidate_lun() for "
479229997Sken			 "LUN %d", __func__, retval, params->lun_id);
480252569Smav		mtx_lock(&softc->lock);
481252569Smav		be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
482252569Smav		mtx_unlock(&softc->lock);
483229997Sken		goto bailout_error;
484229997Sken	}
485229997Sken
486229997Sken	mtx_lock(&softc->lock);
487229997Sken
488229997Sken	while ((be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) == 0) {
489229997Sken		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
490229997Sken 		if (retval == EINTR)
491229997Sken			break;
492229997Sken	}
493229997Sken	be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
494229997Sken
495229997Sken	/*
496229997Sken	 * We only remove this LUN from the list and free it (below) if
497229997Sken	 * retval == 0.  If the user interrupted the wait, we just bail out
498229997Sken	 * without actually freeing the LUN.  We let the shutdown routine
499229997Sken	 * free the LUN if that happens.
500229997Sken	 */
501229997Sken	if (retval == 0) {
502229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
503229997Sken			      links);
504229997Sken		softc->num_luns--;
505229997Sken	}
506229997Sken
507229997Sken	mtx_unlock(&softc->lock);
508229997Sken
509264886Smav	if (retval == 0) {
510287670Smav		taskqueue_drain_all(be_lun->io_taskqueue);
511264886Smav		taskqueue_free(be_lun->io_taskqueue);
512287499Smav		ctl_free_opts(&be_lun->cbe_lun.options);
513267877Smav		mtx_destroy(&be_lun->queue_lock);
514229997Sken		free(be_lun, M_RAMDISK);
515264886Smav	}
516229997Sken
517229997Sken	req->status = CTL_LUN_OK;
518229997Sken
519229997Sken	return (retval);
520229997Sken
521229997Skenbailout_error:
522229997Sken	req->status = CTL_LUN_ERROR;
523229997Sken
524229997Sken	return (0);
525229997Sken}
526229997Sken
527229997Skenstatic int
528229997Skenctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
529287499Smav			   struct ctl_lun_req *req)
530229997Sken{
531229997Sken	struct ctl_be_ramdisk_lun *be_lun;
532287499Smav	struct ctl_be_lun *cbe_lun;
533229997Sken	struct ctl_lun_create_params *params;
534267481Smav	char *value;
535229997Sken	char tmpstr[32];
536287499Smav	int retval;
537229997Sken
538229997Sken	retval = 0;
539229997Sken	params = &req->reqdata.create;
540229997Sken
541287499Smav	be_lun = malloc(sizeof(*be_lun), M_RAMDISK, M_ZERO | M_WAITOK);
542287499Smav	cbe_lun = &be_lun->cbe_lun;
543287499Smav	cbe_lun->be_lun = be_lun;
544287500Smav	be_lun->params = req->reqdata.create;
545287499Smav	be_lun->softc = softc;
546264886Smav	sprintf(be_lun->lunname, "cram%d", softc->num_luns);
547287499Smav	ctl_init_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args);
548229997Sken
549229997Sken	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
550287499Smav		cbe_lun->lun_type = params->device_type;
551229997Sken	else
552287499Smav		cbe_lun->lun_type = T_DIRECT;
553287499Smav	be_lun->flags = CTL_BE_RAMDISK_LUN_UNCONFIGURED;
554287621Smav	cbe_lun->flags = 0;
555287621Smav	value = ctl_get_opt(&cbe_lun->options, "ha_role");
556287621Smav	if (value != NULL) {
557287621Smav		if (strcmp(value, "primary") == 0)
558287621Smav			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
559287621Smav	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
560287621Smav		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
561229997Sken
562287499Smav	if (cbe_lun->lun_type == T_DIRECT) {
563287499Smav		if (params->blocksize_bytes != 0)
564287499Smav			cbe_lun->blocksize = params->blocksize_bytes;
565287499Smav		else
566287499Smav			cbe_lun->blocksize = 512;
567287499Smav		if (params->lun_size_bytes < cbe_lun->blocksize) {
568229997Sken			snprintf(req->error_str, sizeof(req->error_str),
569229997Sken				 "%s: LUN size %ju < blocksize %u", __func__,
570287499Smav				 params->lun_size_bytes, cbe_lun->blocksize);
571229997Sken			goto bailout_error;
572229997Sken		}
573287499Smav		be_lun->size_blocks = params->lun_size_bytes / cbe_lun->blocksize;
574287499Smav		be_lun->size_bytes = be_lun->size_blocks * cbe_lun->blocksize;
575287499Smav		cbe_lun->maxlba = be_lun->size_blocks - 1;
576287499Smav		cbe_lun->atomicblock = UINT32_MAX;
577287499Smav		cbe_lun->opttxferlen = softc->rd_size / cbe_lun->blocksize;
578229997Sken	}
579229997Sken
580229997Sken	/* Tell the user the blocksize we ended up using */
581287499Smav	params->blocksize_bytes = cbe_lun->blocksize;
582229997Sken	params->lun_size_bytes = be_lun->size_bytes;
583229997Sken
584287499Smav	value = ctl_get_opt(&cbe_lun->options, "unmap");
585267481Smav	if (value != NULL && strcmp(value, "on") == 0)
586287499Smav		cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
587287499Smav	value = ctl_get_opt(&cbe_lun->options, "readonly");
588287499Smav	if (value != NULL && strcmp(value, "on") == 0)
589287499Smav		cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
590287499Smav	cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
591287499Smav	value = ctl_get_opt(&cbe_lun->options, "serseq");
592287499Smav	if (value != NULL && strcmp(value, "on") == 0)
593287499Smav		cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
594287499Smav	else if (value != NULL && strcmp(value, "read") == 0)
595287499Smav		cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
596287499Smav	else if (value != NULL && strcmp(value, "off") == 0)
597287499Smav		cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
598254759Strasz
599229997Sken	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
600287499Smav		cbe_lun->req_lun_id = params->req_lun_id;
601287499Smav		cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
602229997Sken	} else
603287499Smav		cbe_lun->req_lun_id = 0;
604229997Sken
605287499Smav	cbe_lun->lun_shutdown = ctl_backend_ramdisk_lun_shutdown;
606287499Smav	cbe_lun->lun_config_status = ctl_backend_ramdisk_lun_config_status;
607287499Smav	cbe_lun->be = &ctl_be_ramdisk_driver;
608229997Sken	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
609229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
610229997Sken			 softc->num_luns);
611287499Smav		strncpy((char *)cbe_lun->serial_num, tmpstr,
612287499Smav			MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
613229997Sken
614229997Sken		/* Tell the user what we used for a serial number */
615229997Sken		strncpy((char *)params->serial_num, tmpstr,
616275953Smav			MIN(sizeof(params->serial_num), sizeof(tmpstr)));
617229997Sken	} else {
618287499Smav		strncpy((char *)cbe_lun->serial_num, params->serial_num,
619287499Smav			MIN(sizeof(cbe_lun->serial_num),
620275953Smav			    sizeof(params->serial_num)));
621229997Sken	}
622229997Sken	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
623229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
624287499Smav		strncpy((char *)cbe_lun->device_id, tmpstr,
625287499Smav			MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
626229997Sken
627229997Sken		/* Tell the user what we used for a device ID */
628229997Sken		strncpy((char *)params->device_id, tmpstr,
629275953Smav			MIN(sizeof(params->device_id), sizeof(tmpstr)));
630229997Sken	} else {
631287499Smav		strncpy((char *)cbe_lun->device_id, params->device_id,
632287499Smav			MIN(sizeof(cbe_lun->device_id),
633275953Smav			    sizeof(params->device_id)));
634229997Sken	}
635229997Sken
636264886Smav	STAILQ_INIT(&be_lun->cont_queue);
637267877Smav	mtx_init(&be_lun->queue_lock, "cram queue lock", NULL, MTX_DEF);
638264886Smav	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_backend_ramdisk_worker,
639264886Smav	    be_lun);
640264886Smav
641264886Smav	be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
642264886Smav	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
643264886Smav	if (be_lun->io_taskqueue == NULL) {
644264886Smav		snprintf(req->error_str, sizeof(req->error_str),
645264886Smav			 "%s: Unable to create taskqueue", __func__);
646264886Smav		goto bailout_error;
647264886Smav	}
648264886Smav
649264886Smav	retval = taskqueue_start_threads(&be_lun->io_taskqueue,
650264886Smav					 /*num threads*/1,
651264886Smav					 /*priority*/PWAIT,
652264886Smav					 /*thread name*/
653264886Smav					 "%s taskq", be_lun->lunname);
654264886Smav	if (retval != 0)
655264886Smav		goto bailout_error;
656264886Smav
657229997Sken	mtx_lock(&softc->lock);
658229997Sken	softc->num_luns++;
659229997Sken	STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
660229997Sken
661229997Sken	mtx_unlock(&softc->lock);
662229997Sken
663287499Smav	retval = ctl_add_lun(&be_lun->cbe_lun);
664229997Sken	if (retval != 0) {
665229997Sken		mtx_lock(&softc->lock);
666229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
667229997Sken			      links);
668229997Sken		softc->num_luns--;
669229997Sken		mtx_unlock(&softc->lock);
670229997Sken		snprintf(req->error_str, sizeof(req->error_str),
671229997Sken			 "%s: ctl_add_lun() returned error %d, see dmesg for "
672229997Sken			"details", __func__, retval);
673229997Sken		retval = 0;
674229997Sken		goto bailout_error;
675229997Sken	}
676229997Sken
677229997Sken	mtx_lock(&softc->lock);
678229997Sken
679229997Sken	/*
680229997Sken	 * Tell the config_status routine that we're waiting so it won't
681229997Sken	 * clean up the LUN in the event of an error.
682229997Sken	 */
683229997Sken	be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
684229997Sken
685229997Sken	while (be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) {
686229997Sken		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
687229997Sken		if (retval == EINTR)
688229997Sken			break;
689229997Sken	}
690229997Sken	be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
691229997Sken
692229997Sken	if (be_lun->flags & CTL_BE_RAMDISK_LUN_CONFIG_ERR) {
693229997Sken		snprintf(req->error_str, sizeof(req->error_str),
694229997Sken			 "%s: LUN configuration error, see dmesg for details",
695229997Sken			 __func__);
696229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
697229997Sken			      links);
698229997Sken		softc->num_luns--;
699229997Sken		mtx_unlock(&softc->lock);
700229997Sken		goto bailout_error;
701229997Sken	} else {
702287499Smav		params->req_lun_id = cbe_lun->lun_id;
703229997Sken	}
704229997Sken	mtx_unlock(&softc->lock);
705229997Sken
706229997Sken	req->status = CTL_LUN_OK;
707229997Sken
708229997Sken	return (retval);
709229997Sken
710229997Skenbailout_error:
711229997Sken	req->status = CTL_LUN_ERROR;
712264886Smav	if (be_lun != NULL) {
713264886Smav		if (be_lun->io_taskqueue != NULL) {
714264886Smav			taskqueue_free(be_lun->io_taskqueue);
715264886Smav		}
716287499Smav		ctl_free_opts(&cbe_lun->options);
717267877Smav		mtx_destroy(&be_lun->queue_lock);
718264886Smav		free(be_lun, M_RAMDISK);
719264886Smav	}
720229997Sken
721229997Sken	return (retval);
722229997Sken}
723229997Sken
724232604Straszstatic int
725232604Straszctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
726232604Strasz		       struct ctl_lun_req *req)
727232604Strasz{
728232604Strasz	struct ctl_be_ramdisk_lun *be_lun;
729287500Smav	struct ctl_be_lun *cbe_lun;
730232604Strasz	struct ctl_lun_modify_params *params;
731287621Smav	char *value;
732232604Strasz	uint32_t blocksize;
733287621Smav	int wasprim;
734232604Strasz
735232604Strasz	params = &req->reqdata.modify;
736232604Strasz
737232604Strasz	mtx_lock(&softc->lock);
738232604Strasz	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
739287499Smav		if (be_lun->cbe_lun.lun_id == params->lun_id)
740232604Strasz			break;
741232604Strasz	}
742232604Strasz	mtx_unlock(&softc->lock);
743232604Strasz
744232604Strasz	if (be_lun == NULL) {
745232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
746232604Strasz			 "%s: LUN %u is not managed by the ramdisk backend",
747232604Strasz			 __func__, params->lun_id);
748232604Strasz		goto bailout_error;
749232604Strasz	}
750287500Smav	cbe_lun = &be_lun->cbe_lun;
751232604Strasz
752287500Smav	if (params->lun_size_bytes != 0)
753287500Smav		be_lun->params.lun_size_bytes = params->lun_size_bytes;
754287500Smav	ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args);
755287621Smav
756287621Smav	wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
757287621Smav	value = ctl_get_opt(&cbe_lun->options, "ha_role");
758287621Smav	if (value != NULL) {
759287621Smav		if (strcmp(value, "primary") == 0)
760287621Smav			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
761287621Smav		else
762287621Smav			cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
763287621Smav	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
764287621Smav		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
765287621Smav	else
766287621Smav		cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
767287621Smav	if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
768287621Smav		if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
769287621Smav			ctl_lun_primary(cbe_lun);
770287621Smav		else
771287621Smav			ctl_lun_secondary(cbe_lun);
772287621Smav	}
773287621Smav
774287499Smav	blocksize = be_lun->cbe_lun.blocksize;
775287500Smav	if (be_lun->params.lun_size_bytes < blocksize) {
776232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
777232604Strasz			"%s: LUN size %ju < blocksize %u", __func__,
778287500Smav			be_lun->params.lun_size_bytes, blocksize);
779232604Strasz		goto bailout_error;
780232604Strasz	}
781287500Smav	be_lun->size_blocks = be_lun->params.lun_size_bytes / blocksize;
782232604Strasz	be_lun->size_bytes = be_lun->size_blocks * blocksize;
783287499Smav	be_lun->cbe_lun.maxlba = be_lun->size_blocks - 1;
784287499Smav	ctl_lun_capacity_changed(&be_lun->cbe_lun);
785232604Strasz
786232604Strasz	/* Tell the user the exact size we ended up using */
787232604Strasz	params->lun_size_bytes = be_lun->size_bytes;
788232604Strasz
789232604Strasz	req->status = CTL_LUN_OK;
790232604Strasz
791232604Strasz	return (0);
792232604Strasz
793232604Straszbailout_error:
794232604Strasz	req->status = CTL_LUN_ERROR;
795232604Strasz
796232604Strasz	return (0);
797232604Strasz}
798232604Strasz
799229997Skenstatic void
800229997Skenctl_backend_ramdisk_lun_shutdown(void *be_lun)
801229997Sken{
802229997Sken	struct ctl_be_ramdisk_lun *lun;
803229997Sken	struct ctl_be_ramdisk_softc *softc;
804229997Sken	int do_free;
805229997Sken
806229997Sken	lun = (struct ctl_be_ramdisk_lun *)be_lun;
807229997Sken	softc = lun->softc;
808229997Sken	do_free = 0;
809229997Sken
810229997Sken	mtx_lock(&softc->lock);
811229997Sken
812229997Sken	lun->flags |= CTL_BE_RAMDISK_LUN_UNCONFIGURED;
813229997Sken
814229997Sken	if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
815229997Sken		wakeup(lun);
816229997Sken	} else {
817269058Smav		STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
818229997Sken			      links);
819229997Sken		softc->num_luns--;
820229997Sken		do_free = 1;
821229997Sken	}
822229997Sken
823229997Sken	mtx_unlock(&softc->lock);
824229997Sken
825229997Sken	if (do_free != 0)
826229997Sken		free(be_lun, M_RAMDISK);
827229997Sken}
828229997Sken
829229997Skenstatic void
830229997Skenctl_backend_ramdisk_lun_config_status(void *be_lun,
831229997Sken				      ctl_lun_config_status status)
832229997Sken{
833229997Sken	struct ctl_be_ramdisk_lun *lun;
834229997Sken	struct ctl_be_ramdisk_softc *softc;
835229997Sken
836229997Sken	lun = (struct ctl_be_ramdisk_lun *)be_lun;
837229997Sken	softc = lun->softc;
838229997Sken
839229997Sken	if (status == CTL_LUN_CONFIG_OK) {
840229997Sken		mtx_lock(&softc->lock);
841229997Sken		lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
842229997Sken		if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING)
843229997Sken			wakeup(lun);
844229997Sken		mtx_unlock(&softc->lock);
845229997Sken
846229997Sken		/*
847229997Sken		 * We successfully added the LUN, attempt to enable it.
848229997Sken		 */
849287499Smav		if (ctl_enable_lun(&lun->cbe_lun) != 0) {
850229997Sken			printf("%s: ctl_enable_lun() failed!\n", __func__);
851287499Smav			if (ctl_invalidate_lun(&lun->cbe_lun) != 0) {
852229997Sken				printf("%s: ctl_invalidate_lun() failed!\n",
853229997Sken				       __func__);
854229997Sken			}
855229997Sken		}
856229997Sken
857229997Sken		return;
858229997Sken	}
859229997Sken
860229997Sken
861229997Sken	mtx_lock(&softc->lock);
862229997Sken	lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
863229997Sken
864229997Sken	/*
865229997Sken	 * If we have a user waiting, let him handle the cleanup.  If not,
866229997Sken	 * clean things up here.
867229997Sken	 */
868229997Sken	if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
869229997Sken		lun->flags |= CTL_BE_RAMDISK_LUN_CONFIG_ERR;
870229997Sken		wakeup(lun);
871229997Sken	} else {
872229997Sken		STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
873229997Sken			      links);
874229997Sken		softc->num_luns--;
875229997Sken		free(lun, M_RAMDISK);
876229997Sken	}
877229997Sken	mtx_unlock(&softc->lock);
878229997Sken}
879229997Sken
880229997Skenstatic int
881229997Skenctl_backend_ramdisk_config_write(union ctl_io *io)
882229997Sken{
883229997Sken	struct ctl_be_ramdisk_softc *softc;
884229997Sken	int retval;
885229997Sken
886229997Sken	retval = 0;
887229997Sken	softc = &rd_softc;
888229997Sken
889229997Sken	switch (io->scsiio.cdb[0]) {
890229997Sken	case SYNCHRONIZE_CACHE:
891229997Sken	case SYNCHRONIZE_CACHE_16:
892229997Sken		/*
893229997Sken		 * The upper level CTL code will filter out any CDBs with
894229997Sken		 * the immediate bit set and return the proper error.  It
895229997Sken		 * will also not allow a sync cache command to go to a LUN
896229997Sken		 * that is powered down.
897229997Sken		 *
898229997Sken		 * We don't really need to worry about what LBA range the
899229997Sken		 * user asked to be synced out.  When they issue a sync
900229997Sken		 * cache command, we'll sync out the whole thing.
901229997Sken		 *
902229997Sken		 * This is obviously just a stubbed out implementation.
903229997Sken		 * The real implementation will be in the RAIDCore/CTL
904229997Sken		 * interface, and can only really happen when RAIDCore
905229997Sken		 * implements a per-array cache sync.
906229997Sken		 */
907229997Sken		ctl_set_success(&io->scsiio);
908229997Sken		ctl_config_write_done(io);
909229997Sken		break;
910229997Sken	case START_STOP_UNIT: {
911229997Sken		struct scsi_start_stop_unit *cdb;
912287499Smav		struct ctl_be_lun *cbe_lun;
913229997Sken		struct ctl_be_ramdisk_lun *be_lun;
914229997Sken
915229997Sken		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
916229997Sken
917287499Smav		cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
918229997Sken			CTL_PRIV_BACKEND_LUN].ptr;
919287499Smav		be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun->be_lun;
920229997Sken
921229997Sken		if (cdb->how & SSS_START)
922287499Smav			retval = ctl_start_lun(cbe_lun);
923229997Sken		else {
924287499Smav			retval = ctl_stop_lun(cbe_lun);
925229997Sken#ifdef NEEDTOPORT
926229997Sken			if ((retval == 0)
927229997Sken			 && (cdb->byte2 & SSS_ONOFFLINE))
928287499Smav				retval = ctl_lun_offline(cbe_lun);
929229997Sken#endif
930229997Sken		}
931229997Sken
932229997Sken		/*
933229997Sken		 * In general, the above routines should not fail.  They
934229997Sken		 * just set state for the LUN.  So we've got something
935229997Sken		 * pretty wrong here if we can't start or stop the LUN.
936229997Sken		 */
937229997Sken		if (retval != 0) {
938229997Sken			ctl_set_internal_failure(&io->scsiio,
939229997Sken						 /*sks_valid*/ 1,
940229997Sken						 /*retry_count*/ 0xf051);
941229997Sken			retval = CTL_RETVAL_COMPLETE;
942229997Sken		} else {
943229997Sken			ctl_set_success(&io->scsiio);
944229997Sken		}
945229997Sken		ctl_config_write_done(io);
946229997Sken		break;
947229997Sken	}
948264274Smav	case WRITE_SAME_10:
949264274Smav	case WRITE_SAME_16:
950264274Smav	case UNMAP:
951264274Smav		ctl_set_success(&io->scsiio);
952264274Smav		ctl_config_write_done(io);
953264274Smav		break;
954229997Sken	default:
955229997Sken		ctl_set_invalid_opcode(&io->scsiio);
956229997Sken		ctl_config_write_done(io);
957229997Sken		retval = CTL_RETVAL_COMPLETE;
958229997Sken		break;
959229997Sken	}
960229997Sken
961229997Sken	return (retval);
962229997Sken}
963229997Sken
964229997Skenstatic int
965229997Skenctl_backend_ramdisk_config_read(union ctl_io *io)
966229997Sken{
967275474Smav	int retval = 0;
968275474Smav
969275474Smav	switch (io->scsiio.cdb[0]) {
970275474Smav	case SERVICE_ACTION_IN:
971275474Smav		if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
972275474Smav			/* We have nothing to tell, leave default data. */
973275474Smav			ctl_config_read_done(io);
974275474Smav			retval = CTL_RETVAL_COMPLETE;
975275474Smav			break;
976275474Smav		}
977275474Smav		ctl_set_invalid_field(&io->scsiio,
978275474Smav				      /*sks_valid*/ 1,
979275474Smav				      /*command*/ 1,
980275474Smav				      /*field*/ 1,
981275474Smav				      /*bit_valid*/ 1,
982275474Smav				      /*bit*/ 4);
983275474Smav		ctl_config_read_done(io);
984275474Smav		retval = CTL_RETVAL_COMPLETE;
985275474Smav		break;
986275474Smav	default:
987275474Smav		ctl_set_invalid_opcode(&io->scsiio);
988275474Smav		ctl_config_read_done(io);
989275474Smav		retval = CTL_RETVAL_COMPLETE;
990275474Smav		break;
991275474Smav	}
992275474Smav
993275474Smav	return (retval);
994229997Sken}
995