ctl_backend_ramdisk.c revision 240993
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 240993 2012-09-27 10:51:38Z trasz $");
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>
53229997Sken#include <sys/time.h>
54229997Sken#include <sys/queue.h>
55229997Sken#include <sys/conf.h>
56229997Sken#include <sys/ioccom.h>
57229997Sken#include <sys/module.h>
58229997Sken
59229997Sken#include <cam/scsi/scsi_all.h>
60229997Sken#include <cam/ctl/ctl_io.h>
61229997Sken#include <cam/ctl/ctl.h>
62229997Sken#include <cam/ctl/ctl_util.h>
63229997Sken#include <cam/ctl/ctl_backend.h>
64229997Sken#include <cam/ctl/ctl_frontend_internal.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 {
76229997Sken	uint64_t size_bytes;
77229997Sken	uint64_t size_blocks;
78229997Sken	struct ctl_be_ramdisk_softc *softc;
79229997Sken	ctl_be_ramdisk_lun_flags flags;
80229997Sken	STAILQ_ENTRY(ctl_be_ramdisk_lun) links;
81229997Sken	struct ctl_be_lun ctl_be_lun;
82229997Sken};
83229997Sken
84229997Skenstruct ctl_be_ramdisk_softc {
85229997Sken	struct mtx lock;
86229997Sken	int rd_size;
87229997Sken#ifdef CTL_RAMDISK_PAGES
88229997Sken	uint8_t **ramdisk_pages;
89229997Sken	int num_pages;
90229997Sken#else
91229997Sken	uint8_t *ramdisk_buffer;
92229997Sken#endif
93229997Sken	int num_luns;
94229997Sken	STAILQ_HEAD(, ctl_be_ramdisk_lun) lun_list;
95229997Sken};
96229997Sken
97229997Skenstatic struct ctl_be_ramdisk_softc rd_softc;
98229997Sken
99229997Skenint ctl_backend_ramdisk_init(void);
100229997Skenvoid ctl_backend_ramdisk_shutdown(void);
101229997Skenstatic int ctl_backend_ramdisk_move_done(union ctl_io *io);
102229997Skenstatic int ctl_backend_ramdisk_submit(union ctl_io *io);
103229997Skenstatic int ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd,
104229997Sken				     caddr_t addr, int flag, struct thread *td);
105229997Skenstatic int ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
106229997Sken				  struct ctl_lun_req *req);
107229997Skenstatic int ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
108229997Sken				      struct ctl_lun_req *req, int do_wait);
109232604Straszstatic int ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
110232604Strasz				  struct ctl_lun_req *req);
111229997Skenstatic void ctl_backend_ramdisk_lun_shutdown(void *be_lun);
112229997Skenstatic void ctl_backend_ramdisk_lun_config_status(void *be_lun,
113229997Sken						  ctl_lun_config_status status);
114229997Skenstatic int ctl_backend_ramdisk_config_write(union ctl_io *io);
115229997Skenstatic int ctl_backend_ramdisk_config_read(union ctl_io *io);
116229997Sken
117229997Skenstatic struct ctl_backend_driver ctl_be_ramdisk_driver =
118229997Sken{
119230334Sken	.name = "ramdisk",
120230334Sken	.flags = CTL_BE_FLAG_HAS_CONFIG,
121230334Sken	.init = ctl_backend_ramdisk_init,
122230334Sken	.data_submit = ctl_backend_ramdisk_submit,
123230334Sken	.data_move_done = ctl_backend_ramdisk_move_done,
124230334Sken	.config_read = ctl_backend_ramdisk_config_read,
125230334Sken	.config_write = ctl_backend_ramdisk_config_write,
126230334Sken	.ioctl = ctl_backend_ramdisk_ioctl
127229997Sken};
128229997Sken
129229997SkenMALLOC_DEFINE(M_RAMDISK, "ramdisk", "Memory used for CTL RAMdisk");
130229997SkenCTL_BACKEND_DECLARE(cbr, ctl_be_ramdisk_driver);
131229997Sken
132229997Skenint
133229997Skenctl_backend_ramdisk_init(void)
134229997Sken{
135229997Sken	struct ctl_be_ramdisk_softc *softc;
136229997Sken#ifdef CTL_RAMDISK_PAGES
137240993Strasz	int i;
138229997Sken#endif
139229997Sken
140229997Sken
141229997Sken	softc = &rd_softc;
142229997Sken
143229997Sken	memset(softc, 0, sizeof(*softc));
144229997Sken
145229997Sken	mtx_init(&softc->lock, "ramdisk", NULL, MTX_DEF);
146229997Sken
147229997Sken	STAILQ_INIT(&softc->lun_list);
148229997Sken	softc->rd_size = 4 * 1024 * 1024;
149229997Sken#ifdef CTL_RAMDISK_PAGES
150229997Sken	softc->num_pages = softc->rd_size / PAGE_SIZE;
151229997Sken	softc->ramdisk_pages = (uint8_t **)malloc(sizeof(uint8_t *) *
152229997Sken						  softc->num_pages, M_RAMDISK,
153229997Sken						  M_WAITOK);
154240993Strasz	for (i = 0; i < softc->num_pages; i++)
155229997Sken		softc->ramdisk_pages[i] = malloc(PAGE_SIZE, M_RAMDISK,M_WAITOK);
156229997Sken#else
157229997Sken	softc->ramdisk_buffer = (uint8_t *)malloc(softc->rd_size, M_RAMDISK,
158229997Sken						  M_WAITOK);
159229997Sken#endif
160229997Sken
161229997Sken	return (0);
162229997Sken}
163229997Sken
164229997Skenvoid
165229997Skenctl_backend_ramdisk_shutdown(void)
166229997Sken{
167229997Sken	struct ctl_be_ramdisk_softc *softc;
168229997Sken	struct ctl_be_ramdisk_lun *lun, *next_lun;
169229997Sken#ifdef CTL_RAMDISK_PAGES
170229997Sken	int i;
171229997Sken#endif
172229997Sken
173229997Sken	softc = &rd_softc;
174229997Sken
175229997Sken	mtx_lock(&softc->lock);
176229997Sken	for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
177229997Sken		/*
178229997Sken		 * Grab the next LUN.  The current LUN may get removed by
179229997Sken		 * ctl_invalidate_lun(), which will call our LUN shutdown
180229997Sken		 * routine, if there is no outstanding I/O for this LUN.
181229997Sken		 */
182229997Sken		next_lun = STAILQ_NEXT(lun, links);
183229997Sken
184229997Sken		/*
185229997Sken		 * Drop our lock here.  Since ctl_invalidate_lun() can call
186229997Sken		 * back into us, this could potentially lead to a recursive
187229997Sken		 * lock of the same mutex, which would cause a hang.
188229997Sken		 */
189229997Sken		mtx_unlock(&softc->lock);
190229997Sken		ctl_disable_lun(&lun->ctl_be_lun);
191229997Sken		ctl_invalidate_lun(&lun->ctl_be_lun);
192229997Sken		mtx_lock(&softc->lock);
193229997Sken	}
194229997Sken	mtx_unlock(&softc->lock);
195229997Sken
196229997Sken#ifdef CTL_RAMDISK_PAGES
197229997Sken	for (i = 0; i < softc->num_pages; i++)
198229997Sken		free(softc->ramdisk_pages[i], M_RAMDISK);
199229997Sken
200229997Sken	free(softc->ramdisk_pages, M_RAMDISK);
201229997Sken#else
202229997Sken	free(softc->ramdisk_buffer, M_RAMDISK);
203229997Sken#endif
204229997Sken
205229997Sken	if (ctl_backend_deregister(&ctl_be_ramdisk_driver) != 0) {
206229997Sken		printf("ctl_backend_ramdisk_shutdown: "
207229997Sken		       "ctl_backend_deregister() failed!\n");
208229997Sken	}
209229997Sken}
210229997Sken
211229997Skenstatic int
212229997Skenctl_backend_ramdisk_move_done(union ctl_io *io)
213229997Sken{
214229997Sken#ifdef CTL_TIME_IO
215229997Sken	struct bintime cur_bt;
216229997Sken#endif
217229997Sken
218229997Sken	CTL_DEBUG_PRINT(("ctl_backend_ramdisk_move_done\n"));
219229997Sken	if ((io->io_hdr.port_status == 0)
220229997Sken	 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
221229997Sken	 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
222229997Sken		io->io_hdr.status = CTL_SUCCESS;
223229997Sken	else if ((io->io_hdr.port_status != 0)
224229997Sken	      && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
225229997Sken	      && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
226229997Sken		/*
227229997Sken		 * For hardware error sense keys, the sense key
228229997Sken		 * specific value is defined to be a retry count,
229229997Sken		 * but we use it to pass back an internal FETD
230229997Sken		 * error code.  XXX KDM  Hopefully the FETD is only
231229997Sken		 * using 16 bits for an error code, since that's
232229997Sken		 * all the space we have in the sks field.
233229997Sken		 */
234229997Sken		ctl_set_internal_failure(&io->scsiio,
235229997Sken					 /*sks_valid*/ 1,
236229997Sken					 /*retry_count*/
237229997Sken					 io->io_hdr.port_status);
238229997Sken	}
239229997Sken#ifdef CTL_TIME_IO
240229997Sken	getbintime(&cur_bt);
241229997Sken	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
242229997Sken	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
243229997Sken	io->io_hdr.num_dmas++;
244229997Sken#endif
245229997Sken
246229997Sken	if (io->scsiio.kern_sg_entries > 0)
247229997Sken		free(io->scsiio.kern_data_ptr, M_RAMDISK);
248229997Sken	ctl_done(io);
249229997Sken	return(0);
250229997Sken}
251229997Sken
252229997Skenstatic int
253229997Skenctl_backend_ramdisk_submit(union ctl_io *io)
254229997Sken{
255229997Sken	struct ctl_lba_len lbalen;
256229997Sken#ifdef CTL_RAMDISK_PAGES
257229997Sken	struct ctl_sg_entry *sg_entries;
258229997Sken	int len_filled;
259229997Sken	int i;
260229997Sken#endif
261229997Sken	int num_sg_entries, len;
262229997Sken	struct ctl_be_ramdisk_softc *softc;
263229997Sken	struct ctl_be_lun *ctl_be_lun;
264229997Sken	struct ctl_be_ramdisk_lun *be_lun;
265229997Sken
266229997Sken	softc = &rd_softc;
267229997Sken
268229997Sken	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
269229997Sken		CTL_PRIV_BACKEND_LUN].ptr;
270229997Sken	be_lun = (struct ctl_be_ramdisk_lun *)ctl_be_lun->be_lun;
271229997Sken
272229997Sken	memcpy(&lbalen, io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
273229997Sken	       sizeof(lbalen));
274229997Sken
275229997Sken	len = lbalen.len * ctl_be_lun->blocksize;
276229997Sken
277229997Sken	/*
278229997Sken	 * Kick out the request if it's bigger than we can handle.
279229997Sken	 */
280229997Sken	if (len > softc->rd_size) {
281229997Sken		ctl_set_internal_failure(&io->scsiio,
282229997Sken					 /*sks_valid*/ 0,
283229997Sken					 /*retry_count*/ 0);
284229997Sken		ctl_done(io);
285229997Sken		return (CTL_RETVAL_COMPLETE);
286229997Sken	}
287229997Sken
288229997Sken	/*
289229997Sken	 * Kick out the request if it's larger than the device size that
290229997Sken	 * the user requested.
291229997Sken	 */
292229997Sken	if (((lbalen.lba * ctl_be_lun->blocksize) + len) > be_lun->size_bytes) {
293229997Sken		ctl_set_lba_out_of_range(&io->scsiio);
294229997Sken		ctl_done(io);
295229997Sken		return (CTL_RETVAL_COMPLETE);
296229997Sken	}
297229997Sken
298229997Sken#ifdef CTL_RAMDISK_PAGES
299229997Sken	num_sg_entries = len >> PAGE_SHIFT;
300229997Sken	if ((len & (PAGE_SIZE - 1)) != 0)
301229997Sken		num_sg_entries++;
302229997Sken
303229997Sken	if (num_sg_entries > 1) {
304229997Sken		io->scsiio.kern_data_ptr = malloc(sizeof(struct ctl_sg_entry) *
305229997Sken						  num_sg_entries, M_RAMDISK,
306229997Sken						  M_WAITOK);
307229997Sken		sg_entries = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
308229997Sken		for (i = 0, len_filled = 0; i < num_sg_entries;
309229997Sken		     i++, len_filled += PAGE_SIZE) {
310229997Sken			sg_entries[i].addr = softc->ramdisk_pages[i];
311229997Sken			sg_entries[i].len = ctl_min(PAGE_SIZE,
312229997Sken						    len - len_filled);
313229997Sken		}
314229997Sken	} else {
315229997Sken#endif /* CTL_RAMDISK_PAGES */
316229997Sken		/*
317229997Sken		 * If this is less than 1 page, don't bother allocating a
318229997Sken		 * scatter/gather list for it.  This saves time/overhead.
319229997Sken		 */
320229997Sken		num_sg_entries = 0;
321229997Sken#ifdef CTL_RAMDISK_PAGES
322229997Sken		io->scsiio.kern_data_ptr = softc->ramdisk_pages[0];
323229997Sken#else
324229997Sken		io->scsiio.kern_data_ptr = softc->ramdisk_buffer;
325229997Sken#endif
326229997Sken#ifdef CTL_RAMDISK_PAGES
327229997Sken	}
328229997Sken#endif
329229997Sken
330229997Sken	io->scsiio.be_move_done = ctl_backend_ramdisk_move_done;
331229997Sken	io->scsiio.kern_data_len = len;
332229997Sken	io->scsiio.kern_total_len = len;
333229997Sken	io->scsiio.kern_rel_offset = 0;
334229997Sken	io->scsiio.kern_data_resid = 0;
335229997Sken	io->scsiio.kern_sg_entries = num_sg_entries;
336229997Sken	io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
337229997Sken#ifdef CTL_TIME_IO
338229997Sken	getbintime(&io->io_hdr.dma_start_bt);
339229997Sken#endif
340229997Sken	ctl_datamove(io);
341229997Sken
342229997Sken	return (CTL_RETVAL_COMPLETE);
343229997Sken}
344229997Sken
345229997Skenstatic int
346229997Skenctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
347229997Sken			  int flag, struct thread *td)
348229997Sken{
349229997Sken	struct ctl_be_ramdisk_softc *softc;
350229997Sken	int retval;
351229997Sken
352229997Sken	retval = 0;
353229997Sken	softc = &rd_softc;
354229997Sken
355229997Sken	switch (cmd) {
356229997Sken	case CTL_LUN_REQ: {
357229997Sken		struct ctl_lun_req *lun_req;
358229997Sken
359229997Sken		lun_req = (struct ctl_lun_req *)addr;
360229997Sken
361229997Sken		switch (lun_req->reqtype) {
362229997Sken		case CTL_LUNREQ_CREATE:
363229997Sken			retval = ctl_backend_ramdisk_create(softc, lun_req,
364229997Sken							    /*do_wait*/ 1);
365229997Sken			break;
366229997Sken		case CTL_LUNREQ_RM:
367229997Sken			retval = ctl_backend_ramdisk_rm(softc, lun_req);
368229997Sken			break;
369232604Strasz		case CTL_LUNREQ_MODIFY:
370232604Strasz			retval = ctl_backend_ramdisk_modify(softc, lun_req);
371232604Strasz			break;
372229997Sken		default:
373229997Sken			lun_req->status = CTL_LUN_ERROR;
374229997Sken			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
375229997Sken				 "%s: invalid LUN request type %d", __func__,
376229997Sken				 lun_req->reqtype);
377229997Sken			break;
378229997Sken		}
379229997Sken		break;
380229997Sken	}
381229997Sken	default:
382229997Sken		retval = ENOTTY;
383229997Sken		break;
384229997Sken	}
385229997Sken
386229997Sken	return (retval);
387229997Sken}
388229997Sken
389229997Skenstatic int
390229997Skenctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
391229997Sken		       struct ctl_lun_req *req)
392229997Sken{
393229997Sken	struct ctl_be_ramdisk_lun *be_lun;
394229997Sken	struct ctl_lun_rm_params *params;
395229997Sken	int retval;
396229997Sken
397229997Sken
398229997Sken	retval = 0;
399229997Sken	params = &req->reqdata.rm;
400229997Sken
401229997Sken	be_lun = NULL;
402229997Sken
403229997Sken	mtx_lock(&softc->lock);
404229997Sken
405229997Sken	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
406229997Sken		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
407229997Sken			break;
408229997Sken	}
409229997Sken	mtx_unlock(&softc->lock);
410229997Sken
411229997Sken	if (be_lun == NULL) {
412229997Sken		snprintf(req->error_str, sizeof(req->error_str),
413229997Sken			 "%s: LUN %u is not managed by the ramdisk backend",
414229997Sken			 __func__, params->lun_id);
415229997Sken		goto bailout_error;
416229997Sken	}
417229997Sken
418229997Sken	retval = ctl_disable_lun(&be_lun->ctl_be_lun);
419229997Sken
420229997Sken	if (retval != 0) {
421229997Sken		snprintf(req->error_str, sizeof(req->error_str),
422229997Sken			 "%s: error %d returned from ctl_disable_lun() for "
423229997Sken			 "LUN %d", __func__, retval, params->lun_id);
424229997Sken		goto bailout_error;
425229997Sken	}
426229997Sken
427229997Sken	/*
428229997Sken	 * Set the waiting flag before we invalidate the LUN.  Our shutdown
429229997Sken	 * routine can be called any time after we invalidate the LUN,
430229997Sken	 * and can be called from our context.
431229997Sken	 *
432229997Sken	 * This tells the shutdown routine that we're waiting, or we're
433229997Sken	 * going to wait for the shutdown to happen.
434229997Sken	 */
435229997Sken	mtx_lock(&softc->lock);
436229997Sken	be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
437229997Sken	mtx_unlock(&softc->lock);
438229997Sken
439229997Sken	retval = ctl_invalidate_lun(&be_lun->ctl_be_lun);
440229997Sken	if (retval != 0) {
441229997Sken		snprintf(req->error_str, sizeof(req->error_str),
442229997Sken			 "%s: error %d returned from ctl_invalidate_lun() for "
443229997Sken			 "LUN %d", __func__, retval, params->lun_id);
444229997Sken		goto bailout_error;
445229997Sken	}
446229997Sken
447229997Sken	mtx_lock(&softc->lock);
448229997Sken
449229997Sken	while ((be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) == 0) {
450229997Sken		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
451229997Sken 		if (retval == EINTR)
452229997Sken			break;
453229997Sken	}
454229997Sken	be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
455229997Sken
456229997Sken	/*
457229997Sken	 * We only remove this LUN from the list and free it (below) if
458229997Sken	 * retval == 0.  If the user interrupted the wait, we just bail out
459229997Sken	 * without actually freeing the LUN.  We let the shutdown routine
460229997Sken	 * free the LUN if that happens.
461229997Sken	 */
462229997Sken	if (retval == 0) {
463229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
464229997Sken			      links);
465229997Sken		softc->num_luns--;
466229997Sken	}
467229997Sken
468229997Sken	mtx_unlock(&softc->lock);
469229997Sken
470229997Sken	if (retval == 0)
471229997Sken		free(be_lun, M_RAMDISK);
472229997Sken
473229997Sken	req->status = CTL_LUN_OK;
474229997Sken
475229997Sken	return (retval);
476229997Sken
477229997Skenbailout_error:
478229997Sken
479229997Sken	/*
480229997Sken	 * Don't leave the waiting flag set.
481229997Sken	 */
482229997Sken	mtx_lock(&softc->lock);
483229997Sken	be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
484229997Sken	mtx_unlock(&softc->lock);
485229997Sken
486229997Sken	req->status = CTL_LUN_ERROR;
487229997Sken
488229997Sken	return (0);
489229997Sken}
490229997Sken
491229997Skenstatic int
492229997Skenctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
493229997Sken			   struct ctl_lun_req *req, int do_wait)
494229997Sken{
495229997Sken	struct ctl_be_ramdisk_lun *be_lun;
496229997Sken	struct ctl_lun_create_params *params;
497229997Sken	uint32_t blocksize;
498229997Sken	char tmpstr[32];
499229997Sken	int retval;
500229997Sken
501229997Sken	retval = 0;
502229997Sken	params = &req->reqdata.create;
503229997Sken	if (params->blocksize_bytes != 0)
504229997Sken		blocksize = params->blocksize_bytes;
505229997Sken	else
506229997Sken		blocksize = 512;
507229997Sken
508229997Sken	be_lun = malloc(sizeof(*be_lun), M_RAMDISK, M_ZERO | (do_wait ?
509229997Sken			M_WAITOK : M_NOWAIT));
510229997Sken
511229997Sken	if (be_lun == NULL) {
512229997Sken		snprintf(req->error_str, sizeof(req->error_str),
513229997Sken			 "%s: error allocating %zd bytes", __func__,
514229997Sken			 sizeof(*be_lun));
515229997Sken		goto bailout_error;
516229997Sken	}
517229997Sken
518229997Sken	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
519229997Sken		be_lun->ctl_be_lun.lun_type = params->device_type;
520229997Sken	else
521229997Sken		be_lun->ctl_be_lun.lun_type = T_DIRECT;
522229997Sken
523229997Sken	if (be_lun->ctl_be_lun.lun_type == T_DIRECT) {
524229997Sken
525229997Sken		if (params->lun_size_bytes < blocksize) {
526229997Sken			snprintf(req->error_str, sizeof(req->error_str),
527229997Sken				 "%s: LUN size %ju < blocksize %u", __func__,
528229997Sken				 params->lun_size_bytes, blocksize);
529229997Sken			goto bailout_error;
530229997Sken		}
531229997Sken
532229997Sken		be_lun->size_blocks = params->lun_size_bytes / blocksize;
533229997Sken		be_lun->size_bytes = be_lun->size_blocks * blocksize;
534229997Sken
535229997Sken		be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1;
536229997Sken	} else {
537229997Sken		be_lun->ctl_be_lun.maxlba = 0;
538229997Sken		blocksize = 0;
539229997Sken		be_lun->size_bytes = 0;
540229997Sken		be_lun->size_blocks = 0;
541229997Sken	}
542229997Sken
543229997Sken	be_lun->ctl_be_lun.blocksize = blocksize;
544229997Sken
545229997Sken	/* Tell the user the blocksize we ended up using */
546229997Sken	params->blocksize_bytes = blocksize;
547229997Sken
548229997Sken	/* Tell the user the exact size we ended up using */
549229997Sken	params->lun_size_bytes = be_lun->size_bytes;
550229997Sken
551229997Sken	be_lun->softc = softc;
552229997Sken
553229997Sken	be_lun->flags = CTL_BE_RAMDISK_LUN_UNCONFIGURED;
554229997Sken	be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY;
555229997Sken	be_lun->ctl_be_lun.be_lun = be_lun;
556229997Sken
557229997Sken	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
558229997Sken		be_lun->ctl_be_lun.req_lun_id = params->req_lun_id;
559229997Sken		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ;
560229997Sken	} else
561229997Sken		be_lun->ctl_be_lun.req_lun_id = 0;
562229997Sken
563229997Sken	be_lun->ctl_be_lun.lun_shutdown = ctl_backend_ramdisk_lun_shutdown;
564229997Sken	be_lun->ctl_be_lun.lun_config_status =
565229997Sken		ctl_backend_ramdisk_lun_config_status;
566229997Sken	be_lun->ctl_be_lun.be = &ctl_be_ramdisk_driver;
567229997Sken	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
568229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
569229997Sken			 softc->num_luns);
570229997Sken		strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr,
571229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
572229997Sken			sizeof(tmpstr)));
573229997Sken
574229997Sken		/* Tell the user what we used for a serial number */
575229997Sken		strncpy((char *)params->serial_num, tmpstr,
576229997Sken			ctl_min(sizeof(params->serial_num), sizeof(tmpstr)));
577229997Sken	} else {
578229997Sken		strncpy((char *)be_lun->ctl_be_lun.serial_num,
579229997Sken			params->serial_num,
580229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
581229997Sken			sizeof(params->serial_num)));
582229997Sken	}
583229997Sken	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
584229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
585229997Sken		strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr,
586229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
587229997Sken			sizeof(tmpstr)));
588229997Sken
589229997Sken		/* Tell the user what we used for a device ID */
590229997Sken		strncpy((char *)params->device_id, tmpstr,
591229997Sken			ctl_min(sizeof(params->device_id), sizeof(tmpstr)));
592229997Sken	} else {
593229997Sken		strncpy((char *)be_lun->ctl_be_lun.device_id,
594229997Sken			params->device_id,
595229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
596229997Sken				sizeof(params->device_id)));
597229997Sken	}
598229997Sken
599229997Sken	mtx_lock(&softc->lock);
600229997Sken	softc->num_luns++;
601229997Sken	STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
602229997Sken
603229997Sken	mtx_unlock(&softc->lock);
604229997Sken
605229997Sken	retval = ctl_add_lun(&be_lun->ctl_be_lun);
606229997Sken	if (retval != 0) {
607229997Sken		mtx_lock(&softc->lock);
608229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
609229997Sken			      links);
610229997Sken		softc->num_luns--;
611229997Sken		mtx_unlock(&softc->lock);
612229997Sken		snprintf(req->error_str, sizeof(req->error_str),
613229997Sken			 "%s: ctl_add_lun() returned error %d, see dmesg for "
614229997Sken			"details", __func__, retval);
615229997Sken		retval = 0;
616229997Sken		goto bailout_error;
617229997Sken	}
618229997Sken
619229997Sken	if (do_wait == 0)
620229997Sken		return (retval);
621229997Sken
622229997Sken	mtx_lock(&softc->lock);
623229997Sken
624229997Sken	/*
625229997Sken	 * Tell the config_status routine that we're waiting so it won't
626229997Sken	 * clean up the LUN in the event of an error.
627229997Sken	 */
628229997Sken	be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
629229997Sken
630229997Sken	while (be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) {
631229997Sken		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
632229997Sken		if (retval == EINTR)
633229997Sken			break;
634229997Sken	}
635229997Sken	be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
636229997Sken
637229997Sken	if (be_lun->flags & CTL_BE_RAMDISK_LUN_CONFIG_ERR) {
638229997Sken		snprintf(req->error_str, sizeof(req->error_str),
639229997Sken			 "%s: LUN configuration error, see dmesg for details",
640229997Sken			 __func__);
641229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
642229997Sken			      links);
643229997Sken		softc->num_luns--;
644229997Sken		mtx_unlock(&softc->lock);
645229997Sken		goto bailout_error;
646229997Sken	} else {
647229997Sken		params->req_lun_id = be_lun->ctl_be_lun.lun_id;
648229997Sken	}
649229997Sken	mtx_unlock(&softc->lock);
650229997Sken
651229997Sken	req->status = CTL_LUN_OK;
652229997Sken
653229997Sken	return (retval);
654229997Sken
655229997Skenbailout_error:
656229997Sken	req->status = CTL_LUN_ERROR;
657229997Sken	free(be_lun, M_RAMDISK);
658229997Sken
659229997Sken	return (retval);
660229997Sken}
661229997Sken
662232604Straszstatic int
663232604Straszctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
664232604Strasz		       struct ctl_lun_req *req)
665232604Strasz{
666232604Strasz	struct ctl_be_ramdisk_lun *be_lun;
667232604Strasz	struct ctl_lun_modify_params *params;
668232604Strasz	uint32_t blocksize;
669232604Strasz
670232604Strasz	params = &req->reqdata.modify;
671232604Strasz
672232604Strasz	be_lun = NULL;
673232604Strasz
674232604Strasz	mtx_lock(&softc->lock);
675232604Strasz	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
676232604Strasz		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
677232604Strasz			break;
678232604Strasz	}
679232604Strasz	mtx_unlock(&softc->lock);
680232604Strasz
681232604Strasz	if (be_lun == NULL) {
682232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
683232604Strasz			 "%s: LUN %u is not managed by the ramdisk backend",
684232604Strasz			 __func__, params->lun_id);
685232604Strasz		goto bailout_error;
686232604Strasz	}
687232604Strasz
688232604Strasz	if (params->lun_size_bytes == 0) {
689232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
690232604Strasz			"%s: LUN size \"auto\" not supported "
691232604Strasz			"by the ramdisk backend", __func__);
692232604Strasz		goto bailout_error;
693232604Strasz	}
694232604Strasz
695232604Strasz	blocksize = be_lun->ctl_be_lun.blocksize;
696232604Strasz
697232604Strasz	if (params->lun_size_bytes < blocksize) {
698232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
699232604Strasz			"%s: LUN size %ju < blocksize %u", __func__,
700232604Strasz			params->lun_size_bytes, blocksize);
701232604Strasz		goto bailout_error;
702232604Strasz	}
703232604Strasz
704232604Strasz	be_lun->size_blocks = params->lun_size_bytes / blocksize;
705232604Strasz	be_lun->size_bytes = be_lun->size_blocks * blocksize;
706232604Strasz
707232604Strasz	/*
708232604Strasz	 * The maximum LBA is the size - 1.
709232604Strasz	 *
710232604Strasz	 * XXX: Note that this field is being updated without locking,
711232604Strasz	 * 	which might cause problems on 32-bit architectures.
712232604Strasz	 */
713232604Strasz	be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1;
714232604Strasz	ctl_lun_capacity_changed(&be_lun->ctl_be_lun);
715232604Strasz
716232604Strasz	/* Tell the user the exact size we ended up using */
717232604Strasz	params->lun_size_bytes = be_lun->size_bytes;
718232604Strasz
719232604Strasz	req->status = CTL_LUN_OK;
720232604Strasz
721232604Strasz	return (0);
722232604Strasz
723232604Straszbailout_error:
724232604Strasz	req->status = CTL_LUN_ERROR;
725232604Strasz
726232604Strasz	return (0);
727232604Strasz}
728232604Strasz
729229997Skenstatic void
730229997Skenctl_backend_ramdisk_lun_shutdown(void *be_lun)
731229997Sken{
732229997Sken	struct ctl_be_ramdisk_lun *lun;
733229997Sken	struct ctl_be_ramdisk_softc *softc;
734229997Sken	int do_free;
735229997Sken
736229997Sken	lun = (struct ctl_be_ramdisk_lun *)be_lun;
737229997Sken	softc = lun->softc;
738229997Sken	do_free = 0;
739229997Sken
740229997Sken	mtx_lock(&softc->lock);
741229997Sken
742229997Sken	lun->flags |= CTL_BE_RAMDISK_LUN_UNCONFIGURED;
743229997Sken
744229997Sken	if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
745229997Sken		wakeup(lun);
746229997Sken	} else {
747229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
748229997Sken			      links);
749229997Sken		softc->num_luns--;
750229997Sken		do_free = 1;
751229997Sken	}
752229997Sken
753229997Sken	mtx_unlock(&softc->lock);
754229997Sken
755229997Sken	if (do_free != 0)
756229997Sken		free(be_lun, M_RAMDISK);
757229997Sken}
758229997Sken
759229997Skenstatic void
760229997Skenctl_backend_ramdisk_lun_config_status(void *be_lun,
761229997Sken				      ctl_lun_config_status status)
762229997Sken{
763229997Sken	struct ctl_be_ramdisk_lun *lun;
764229997Sken	struct ctl_be_ramdisk_softc *softc;
765229997Sken
766229997Sken	lun = (struct ctl_be_ramdisk_lun *)be_lun;
767229997Sken	softc = lun->softc;
768229997Sken
769229997Sken	if (status == CTL_LUN_CONFIG_OK) {
770229997Sken		mtx_lock(&softc->lock);
771229997Sken		lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
772229997Sken		if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING)
773229997Sken			wakeup(lun);
774229997Sken		mtx_unlock(&softc->lock);
775229997Sken
776229997Sken		/*
777229997Sken		 * We successfully added the LUN, attempt to enable it.
778229997Sken		 */
779229997Sken		if (ctl_enable_lun(&lun->ctl_be_lun) != 0) {
780229997Sken			printf("%s: ctl_enable_lun() failed!\n", __func__);
781229997Sken			if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) {
782229997Sken				printf("%s: ctl_invalidate_lun() failed!\n",
783229997Sken				       __func__);
784229997Sken			}
785229997Sken		}
786229997Sken
787229997Sken		return;
788229997Sken	}
789229997Sken
790229997Sken
791229997Sken	mtx_lock(&softc->lock);
792229997Sken	lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
793229997Sken
794229997Sken	/*
795229997Sken	 * If we have a user waiting, let him handle the cleanup.  If not,
796229997Sken	 * clean things up here.
797229997Sken	 */
798229997Sken	if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
799229997Sken		lun->flags |= CTL_BE_RAMDISK_LUN_CONFIG_ERR;
800229997Sken		wakeup(lun);
801229997Sken	} else {
802229997Sken		STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
803229997Sken			      links);
804229997Sken		softc->num_luns--;
805229997Sken		free(lun, M_RAMDISK);
806229997Sken	}
807229997Sken	mtx_unlock(&softc->lock);
808229997Sken}
809229997Sken
810229997Skenstatic int
811229997Skenctl_backend_ramdisk_config_write(union ctl_io *io)
812229997Sken{
813229997Sken	struct ctl_be_ramdisk_softc *softc;
814229997Sken	int retval;
815229997Sken
816229997Sken	retval = 0;
817229997Sken	softc = &rd_softc;
818229997Sken
819229997Sken	switch (io->scsiio.cdb[0]) {
820229997Sken	case SYNCHRONIZE_CACHE:
821229997Sken	case SYNCHRONIZE_CACHE_16:
822229997Sken		/*
823229997Sken		 * The upper level CTL code will filter out any CDBs with
824229997Sken		 * the immediate bit set and return the proper error.  It
825229997Sken		 * will also not allow a sync cache command to go to a LUN
826229997Sken		 * that is powered down.
827229997Sken		 *
828229997Sken		 * We don't really need to worry about what LBA range the
829229997Sken		 * user asked to be synced out.  When they issue a sync
830229997Sken		 * cache command, we'll sync out the whole thing.
831229997Sken		 *
832229997Sken		 * This is obviously just a stubbed out implementation.
833229997Sken		 * The real implementation will be in the RAIDCore/CTL
834229997Sken		 * interface, and can only really happen when RAIDCore
835229997Sken		 * implements a per-array cache sync.
836229997Sken		 */
837229997Sken		ctl_set_success(&io->scsiio);
838229997Sken		ctl_config_write_done(io);
839229997Sken		break;
840229997Sken	case START_STOP_UNIT: {
841229997Sken		struct scsi_start_stop_unit *cdb;
842229997Sken		struct ctl_be_lun *ctl_be_lun;
843229997Sken		struct ctl_be_ramdisk_lun *be_lun;
844229997Sken
845229997Sken		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
846229997Sken
847229997Sken		ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
848229997Sken			CTL_PRIV_BACKEND_LUN].ptr;
849229997Sken		be_lun = (struct ctl_be_ramdisk_lun *)ctl_be_lun->be_lun;
850229997Sken
851229997Sken		if (cdb->how & SSS_START)
852229997Sken			retval = ctl_start_lun(ctl_be_lun);
853229997Sken		else {
854229997Sken			retval = ctl_stop_lun(ctl_be_lun);
855229997Sken#ifdef NEEDTOPORT
856229997Sken			if ((retval == 0)
857229997Sken			 && (cdb->byte2 & SSS_ONOFFLINE))
858229997Sken				retval = ctl_lun_offline(ctl_be_lun);
859229997Sken#endif
860229997Sken		}
861229997Sken
862229997Sken		/*
863229997Sken		 * In general, the above routines should not fail.  They
864229997Sken		 * just set state for the LUN.  So we've got something
865229997Sken		 * pretty wrong here if we can't start or stop the LUN.
866229997Sken		 */
867229997Sken		if (retval != 0) {
868229997Sken			ctl_set_internal_failure(&io->scsiio,
869229997Sken						 /*sks_valid*/ 1,
870229997Sken						 /*retry_count*/ 0xf051);
871229997Sken			retval = CTL_RETVAL_COMPLETE;
872229997Sken		} else {
873229997Sken			ctl_set_success(&io->scsiio);
874229997Sken		}
875229997Sken		ctl_config_write_done(io);
876229997Sken		break;
877229997Sken	}
878229997Sken	default:
879229997Sken		ctl_set_invalid_opcode(&io->scsiio);
880229997Sken		ctl_config_write_done(io);
881229997Sken		retval = CTL_RETVAL_COMPLETE;
882229997Sken		break;
883229997Sken	}
884229997Sken
885229997Sken	return (retval);
886229997Sken}
887229997Sken
888229997Skenstatic int
889229997Skenctl_backend_ramdisk_config_read(union ctl_io *io)
890229997Sken{
891229997Sken	/*
892229997Sken	 * XXX KDM need to implement!!
893229997Sken	 */
894229997Sken	return (0);
895229997Sken}
896