]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/cam/ctl/ctl_backend_ramdisk.c
MFC r287500: Allow LUN options modification via CTL_LUNREQ_MODIFY.
[FreeBSD/stable/10.git] / sys / cam / ctl / ctl_backend_ramdisk.c
1 /*-
2  * Copyright (c) 2003, 2008 Silicon Graphics International Corp.
3  * Copyright (c) 2012 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Portions of this software were developed by Edward Tomasz Napierala
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions, and the following disclaimer,
14  *    without modification.
15  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16  *    substantially similar to the "NO WARRANTY" disclaimer below
17  *    ("Disclaimer") and any redistribution must be conditioned upon
18  *    including a substantially similar Disclaimer requirement for further
19  *    binary redistribution.
20  *
21  * NO WARRANTY
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGES.
33  *
34  * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_ramdisk.c#3 $
35  */
36 /*
37  * CAM Target Layer backend for a "fake" ramdisk.
38  *
39  * Author: Ken Merry <ken@FreeBSD.org>
40  */
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/condvar.h>
49 #include <sys/types.h>
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/malloc.h>
53 #include <sys/taskqueue.h>
54 #include <sys/time.h>
55 #include <sys/queue.h>
56 #include <sys/conf.h>
57 #include <sys/ioccom.h>
58 #include <sys/module.h>
59
60 #include <cam/scsi/scsi_all.h>
61 #include <cam/ctl/ctl_io.h>
62 #include <cam/ctl/ctl.h>
63 #include <cam/ctl/ctl_util.h>
64 #include <cam/ctl/ctl_backend.h>
65 #include <cam/ctl/ctl_debug.h>
66 #include <cam/ctl/ctl_ioctl.h>
67 #include <cam/ctl/ctl_error.h>
68
69 typedef enum {
70         CTL_BE_RAMDISK_LUN_UNCONFIGURED = 0x01,
71         CTL_BE_RAMDISK_LUN_CONFIG_ERR   = 0x02,
72         CTL_BE_RAMDISK_LUN_WAITING      = 0x04
73 } ctl_be_ramdisk_lun_flags;
74
75 struct ctl_be_ramdisk_lun {
76         struct ctl_lun_create_params params;
77         char lunname[32];
78         uint64_t size_bytes;
79         uint64_t size_blocks;
80         struct ctl_be_ramdisk_softc *softc;
81         ctl_be_ramdisk_lun_flags flags;
82         STAILQ_ENTRY(ctl_be_ramdisk_lun) links;
83         struct ctl_be_lun cbe_lun;
84         struct taskqueue *io_taskqueue;
85         struct task io_task;
86         STAILQ_HEAD(, ctl_io_hdr) cont_queue;
87         struct mtx_padalign queue_lock;
88 };
89
90 struct ctl_be_ramdisk_softc {
91         struct mtx lock;
92         int rd_size;
93 #ifdef CTL_RAMDISK_PAGES
94         uint8_t **ramdisk_pages;
95         int num_pages;
96 #else
97         uint8_t *ramdisk_buffer;
98 #endif
99         int num_luns;
100         STAILQ_HEAD(, ctl_be_ramdisk_lun) lun_list;
101 };
102
103 static struct ctl_be_ramdisk_softc rd_softc;
104
105 int ctl_backend_ramdisk_init(void);
106 void ctl_backend_ramdisk_shutdown(void);
107 static int ctl_backend_ramdisk_move_done(union ctl_io *io);
108 static int ctl_backend_ramdisk_submit(union ctl_io *io);
109 static void ctl_backend_ramdisk_continue(union ctl_io *io);
110 static int ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd,
111                                      caddr_t addr, int flag, struct thread *td);
112 static int ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
113                                   struct ctl_lun_req *req);
114 static int ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
115                                       struct ctl_lun_req *req);
116 static int ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
117                                   struct ctl_lun_req *req);
118 static void ctl_backend_ramdisk_worker(void *context, int pending);
119 static void ctl_backend_ramdisk_lun_shutdown(void *be_lun);
120 static void ctl_backend_ramdisk_lun_config_status(void *be_lun,
121                                                   ctl_lun_config_status status);
122 static int ctl_backend_ramdisk_config_write(union ctl_io *io);
123 static int ctl_backend_ramdisk_config_read(union ctl_io *io);
124
125 static struct ctl_backend_driver ctl_be_ramdisk_driver = 
126 {
127         .name = "ramdisk",
128         .flags = CTL_BE_FLAG_HAS_CONFIG,
129         .init = ctl_backend_ramdisk_init,
130         .data_submit = ctl_backend_ramdisk_submit,
131         .data_move_done = ctl_backend_ramdisk_move_done,
132         .config_read = ctl_backend_ramdisk_config_read,
133         .config_write = ctl_backend_ramdisk_config_write,
134         .ioctl = ctl_backend_ramdisk_ioctl
135 };
136
137 MALLOC_DEFINE(M_RAMDISK, "ramdisk", "Memory used for CTL RAMdisk");
138 CTL_BACKEND_DECLARE(cbr, ctl_be_ramdisk_driver);
139
140 int
141 ctl_backend_ramdisk_init(void)
142 {
143         struct ctl_be_ramdisk_softc *softc;
144 #ifdef CTL_RAMDISK_PAGES
145         int i;
146 #endif
147
148
149         softc = &rd_softc;
150
151         memset(softc, 0, sizeof(*softc));
152
153         mtx_init(&softc->lock, "ctlramdisk", NULL, MTX_DEF);
154
155         STAILQ_INIT(&softc->lun_list);
156         softc->rd_size = 1024 * 1024;
157 #ifdef CTL_RAMDISK_PAGES
158         softc->num_pages = softc->rd_size / PAGE_SIZE;
159         softc->ramdisk_pages = (uint8_t **)malloc(sizeof(uint8_t *) *
160                                                   softc->num_pages, M_RAMDISK,
161                                                   M_WAITOK);
162         for (i = 0; i < softc->num_pages; i++)
163                 softc->ramdisk_pages[i] = malloc(PAGE_SIZE, M_RAMDISK,M_WAITOK);
164 #else
165         softc->ramdisk_buffer = (uint8_t *)malloc(softc->rd_size, M_RAMDISK,
166                                                   M_WAITOK);
167 #endif
168
169         return (0);
170 }
171
172 void
173 ctl_backend_ramdisk_shutdown(void)
174 {
175         struct ctl_be_ramdisk_softc *softc;
176         struct ctl_be_ramdisk_lun *lun, *next_lun;
177 #ifdef CTL_RAMDISK_PAGES
178         int i;
179 #endif
180
181         softc = &rd_softc;
182
183         mtx_lock(&softc->lock);
184         for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
185                 /*
186                  * Grab the next LUN.  The current LUN may get removed by
187                  * ctl_invalidate_lun(), which will call our LUN shutdown
188                  * routine, if there is no outstanding I/O for this LUN.
189                  */
190                 next_lun = STAILQ_NEXT(lun, links);
191
192                 /*
193                  * Drop our lock here.  Since ctl_invalidate_lun() can call
194                  * back into us, this could potentially lead to a recursive
195                  * lock of the same mutex, which would cause a hang.
196                  */
197                 mtx_unlock(&softc->lock);
198                 ctl_disable_lun(&lun->cbe_lun);
199                 ctl_invalidate_lun(&lun->cbe_lun);
200                 mtx_lock(&softc->lock);
201         }
202         mtx_unlock(&softc->lock);
203         
204 #ifdef CTL_RAMDISK_PAGES
205         for (i = 0; i < softc->num_pages; i++)
206                 free(softc->ramdisk_pages[i], M_RAMDISK);
207
208         free(softc->ramdisk_pages, M_RAMDISK);
209 #else
210         free(softc->ramdisk_buffer, M_RAMDISK);
211 #endif
212
213         if (ctl_backend_deregister(&ctl_be_ramdisk_driver) != 0) {
214                 printf("ctl_backend_ramdisk_shutdown: "
215                        "ctl_backend_deregister() failed!\n");
216         }
217 }
218
219 static int
220 ctl_backend_ramdisk_move_done(union ctl_io *io)
221 {
222         struct ctl_be_lun *cbe_lun;
223         struct ctl_be_ramdisk_lun *be_lun;
224 #ifdef CTL_TIME_IO
225         struct bintime cur_bt;
226 #endif
227
228         CTL_DEBUG_PRINT(("ctl_backend_ramdisk_move_done\n"));
229         cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
230                 CTL_PRIV_BACKEND_LUN].ptr;
231         be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun->be_lun;
232 #ifdef CTL_TIME_IO
233         getbintime(&cur_bt);
234         bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
235         bintime_add(&io->io_hdr.dma_bt, &cur_bt);
236         io->io_hdr.num_dmas++;
237 #endif
238         if (io->scsiio.kern_sg_entries > 0)
239                 free(io->scsiio.kern_data_ptr, M_RAMDISK);
240         io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
241         if (io->io_hdr.flags & CTL_FLAG_ABORT) {
242                 ;
243         } else if ((io->io_hdr.port_status == 0) &&
244             ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
245                 if (io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer > 0) {
246                         mtx_lock(&be_lun->queue_lock);
247                         STAILQ_INSERT_TAIL(&be_lun->cont_queue,
248                             &io->io_hdr, links);
249                         mtx_unlock(&be_lun->queue_lock);
250                         taskqueue_enqueue(be_lun->io_taskqueue,
251                             &be_lun->io_task);
252                         return (0);
253                 }
254                 ctl_set_success(&io->scsiio);
255         } else if ((io->io_hdr.port_status != 0) &&
256             ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
257              (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
258                 /*
259                  * For hardware error sense keys, the sense key
260                  * specific value is defined to be a retry count,
261                  * but we use it to pass back an internal FETD
262                  * error code.  XXX KDM  Hopefully the FETD is only
263                  * using 16 bits for an error code, since that's
264                  * all the space we have in the sks field.
265                  */
266                 ctl_set_internal_failure(&io->scsiio,
267                                          /*sks_valid*/ 1,
268                                          /*retry_count*/
269                                          io->io_hdr.port_status);
270         }
271         ctl_data_submit_done(io);
272         return(0);
273 }
274
275 static int
276 ctl_backend_ramdisk_submit(union ctl_io *io)
277 {
278         struct ctl_be_lun *cbe_lun;
279         struct ctl_lba_len_flags *lbalen;
280
281         cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
282                 CTL_PRIV_BACKEND_LUN].ptr;
283         lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
284         if (lbalen->flags & CTL_LLF_VERIFY) {
285                 ctl_set_success(&io->scsiio);
286                 ctl_data_submit_done(io);
287                 return (CTL_RETVAL_COMPLETE);
288         }
289         io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer =
290             lbalen->len * cbe_lun->blocksize;
291         ctl_backend_ramdisk_continue(io);
292         return (CTL_RETVAL_COMPLETE);
293 }
294
295 static void
296 ctl_backend_ramdisk_continue(union ctl_io *io)
297 {
298         struct ctl_be_ramdisk_softc *softc;
299         int len, len_filled, sg_filled;
300 #ifdef CTL_RAMDISK_PAGES
301         struct ctl_sg_entry *sg_entries;
302         int i;
303 #endif
304
305         softc = &rd_softc;
306         len = io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer;
307 #ifdef CTL_RAMDISK_PAGES
308         sg_filled = min(btoc(len), softc->num_pages);
309         if (sg_filled > 1) {
310                 io->scsiio.kern_data_ptr = malloc(sizeof(struct ctl_sg_entry) *
311                                                   sg_filled, M_RAMDISK,
312                                                   M_WAITOK);
313                 sg_entries = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
314                 for (i = 0, len_filled = 0; i < sg_filled; i++) {
315                         sg_entries[i].addr = softc->ramdisk_pages[i];
316                         sg_entries[i].len = MIN(PAGE_SIZE, len - len_filled);
317                         len_filled += sg_entries[i].len;
318                 }
319                 io->io_hdr.flags |= CTL_FLAG_KDPTR_SGLIST;
320         } else {
321                 sg_filled = 0;
322                 len_filled = len;
323                 io->scsiio.kern_data_ptr = softc->ramdisk_pages[0];
324         }
325 #else
326         sg_filled = 0;
327         len_filled = min(len, softc->rd_size);
328         io->scsiio.kern_data_ptr = softc->ramdisk_buffer;
329 #endif /* CTL_RAMDISK_PAGES */
330
331         io->scsiio.be_move_done = ctl_backend_ramdisk_move_done;
332         io->scsiio.kern_data_resid = 0;
333         io->scsiio.kern_data_len = len_filled;
334         io->scsiio.kern_sg_entries = sg_filled;
335         io->io_hdr.flags |= CTL_FLAG_ALLOCATED;
336         io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer -= len_filled;
337 #ifdef CTL_TIME_IO
338         getbintime(&io->io_hdr.dma_start_bt);
339 #endif
340         ctl_datamove(io);
341 }
342
343 static void
344 ctl_backend_ramdisk_worker(void *context, int pending)
345 {
346         struct ctl_be_ramdisk_softc *softc;
347         struct ctl_be_ramdisk_lun *be_lun;
348         union ctl_io *io;
349
350         be_lun = (struct ctl_be_ramdisk_lun *)context;
351         softc = be_lun->softc;
352
353         mtx_lock(&be_lun->queue_lock);
354         for (;;) {
355                 io = (union ctl_io *)STAILQ_FIRST(&be_lun->cont_queue);
356                 if (io != NULL) {
357                         STAILQ_REMOVE(&be_lun->cont_queue, &io->io_hdr,
358                                       ctl_io_hdr, links);
359
360                         mtx_unlock(&be_lun->queue_lock);
361
362                         ctl_backend_ramdisk_continue(io);
363
364                         mtx_lock(&be_lun->queue_lock);
365                         continue;
366                 }
367
368                 /*
369                  * If we get here, there is no work left in the queues, so
370                  * just break out and let the task queue go to sleep.
371                  */
372                 break;
373         }
374         mtx_unlock(&be_lun->queue_lock);
375 }
376
377 static int
378 ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
379                           int flag, struct thread *td)
380 {
381         struct ctl_be_ramdisk_softc *softc;
382         int retval;
383
384         retval = 0;
385         softc = &rd_softc;
386
387         switch (cmd) {
388         case CTL_LUN_REQ: {
389                 struct ctl_lun_req *lun_req;
390
391                 lun_req = (struct ctl_lun_req *)addr;
392
393                 switch (lun_req->reqtype) {
394                 case CTL_LUNREQ_CREATE:
395                         retval = ctl_backend_ramdisk_create(softc, lun_req);
396                         break;
397                 case CTL_LUNREQ_RM:
398                         retval = ctl_backend_ramdisk_rm(softc, lun_req);
399                         break;
400                 case CTL_LUNREQ_MODIFY:
401                         retval = ctl_backend_ramdisk_modify(softc, lun_req);
402                         break;
403                 default:
404                         lun_req->status = CTL_LUN_ERROR;
405                         snprintf(lun_req->error_str, sizeof(lun_req->error_str),
406                                  "%s: invalid LUN request type %d", __func__,
407                                  lun_req->reqtype);
408                         break;
409                 }
410                 break;
411         }
412         default:
413                 retval = ENOTTY;
414                 break;
415         }
416
417         return (retval);
418 }
419
420 static int
421 ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
422                        struct ctl_lun_req *req)
423 {
424         struct ctl_be_ramdisk_lun *be_lun;
425         struct ctl_lun_rm_params *params;
426         int retval;
427
428
429         retval = 0;
430         params = &req->reqdata.rm;
431
432         be_lun = NULL;
433
434         mtx_lock(&softc->lock);
435
436         STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
437                 if (be_lun->cbe_lun.lun_id == params->lun_id)
438                         break;
439         }
440         mtx_unlock(&softc->lock);
441
442         if (be_lun == NULL) {
443                 snprintf(req->error_str, sizeof(req->error_str),
444                          "%s: LUN %u is not managed by the ramdisk backend",
445                          __func__, params->lun_id);
446                 goto bailout_error;
447         }
448
449         retval = ctl_disable_lun(&be_lun->cbe_lun);
450
451         if (retval != 0) {
452                 snprintf(req->error_str, sizeof(req->error_str),
453                          "%s: error %d returned from ctl_disable_lun() for "
454                          "LUN %d", __func__, retval, params->lun_id);
455                 goto bailout_error;
456         }
457
458         /*
459          * Set the waiting flag before we invalidate the LUN.  Our shutdown
460          * routine can be called any time after we invalidate the LUN,
461          * and can be called from our context.
462          *
463          * This tells the shutdown routine that we're waiting, or we're
464          * going to wait for the shutdown to happen.
465          */
466         mtx_lock(&softc->lock);
467         be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
468         mtx_unlock(&softc->lock);
469
470         retval = ctl_invalidate_lun(&be_lun->cbe_lun);
471         if (retval != 0) {
472                 snprintf(req->error_str, sizeof(req->error_str),
473                          "%s: error %d returned from ctl_invalidate_lun() for "
474                          "LUN %d", __func__, retval, params->lun_id);
475                 mtx_lock(&softc->lock);
476                 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
477                 mtx_unlock(&softc->lock);
478                 goto bailout_error;
479         }
480
481         mtx_lock(&softc->lock);
482
483         while ((be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) == 0) {
484                 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
485                 if (retval == EINTR)   
486                         break;
487         }
488         be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
489
490         /*
491          * We only remove this LUN from the list and free it (below) if
492          * retval == 0.  If the user interrupted the wait, we just bail out
493          * without actually freeing the LUN.  We let the shutdown routine
494          * free the LUN if that happens.
495          */
496         if (retval == 0) {
497                 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
498                               links);
499                 softc->num_luns--;
500         }
501
502         mtx_unlock(&softc->lock);
503
504         if (retval == 0) {
505                 taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task);
506                 taskqueue_free(be_lun->io_taskqueue);
507                 ctl_free_opts(&be_lun->cbe_lun.options);
508                 mtx_destroy(&be_lun->queue_lock);
509                 free(be_lun, M_RAMDISK);
510         }
511
512         req->status = CTL_LUN_OK;
513
514         return (retval);
515
516 bailout_error:
517         req->status = CTL_LUN_ERROR;
518
519         return (0);
520 }
521
522 static int
523 ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
524                            struct ctl_lun_req *req)
525 {
526         struct ctl_be_ramdisk_lun *be_lun;
527         struct ctl_be_lun *cbe_lun;
528         struct ctl_lun_create_params *params;
529         char *value;
530         char tmpstr[32];
531         int retval;
532
533         retval = 0;
534         params = &req->reqdata.create;
535
536         be_lun = malloc(sizeof(*be_lun), M_RAMDISK, M_ZERO | M_WAITOK);
537         cbe_lun = &be_lun->cbe_lun;
538         cbe_lun->be_lun = be_lun;
539         be_lun->params = req->reqdata.create;
540         be_lun->softc = softc;
541         sprintf(be_lun->lunname, "cram%d", softc->num_luns);
542         ctl_init_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args);
543
544         if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
545                 cbe_lun->lun_type = params->device_type;
546         else
547                 cbe_lun->lun_type = T_DIRECT;
548         be_lun->flags = CTL_BE_RAMDISK_LUN_UNCONFIGURED;
549         cbe_lun->flags = CTL_LUN_FLAG_PRIMARY;
550
551         if (cbe_lun->lun_type == T_DIRECT) {
552                 if (params->blocksize_bytes != 0)
553                         cbe_lun->blocksize = params->blocksize_bytes;
554                 else
555                         cbe_lun->blocksize = 512;
556                 if (params->lun_size_bytes < cbe_lun->blocksize) {
557                         snprintf(req->error_str, sizeof(req->error_str),
558                                  "%s: LUN size %ju < blocksize %u", __func__,
559                                  params->lun_size_bytes, cbe_lun->blocksize);
560                         goto bailout_error;
561                 }
562                 be_lun->size_blocks = params->lun_size_bytes / cbe_lun->blocksize;
563                 be_lun->size_bytes = be_lun->size_blocks * cbe_lun->blocksize;
564                 cbe_lun->maxlba = be_lun->size_blocks - 1;
565                 cbe_lun->atomicblock = UINT32_MAX;
566                 cbe_lun->opttxferlen = softc->rd_size / cbe_lun->blocksize;
567         }
568
569         /* Tell the user the blocksize we ended up using */
570         params->blocksize_bytes = cbe_lun->blocksize;
571         params->lun_size_bytes = be_lun->size_bytes;
572
573         value = ctl_get_opt(&cbe_lun->options, "unmap");
574         if (value != NULL && strcmp(value, "on") == 0)
575                 cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
576         value = ctl_get_opt(&cbe_lun->options, "readonly");
577         if (value != NULL && strcmp(value, "on") == 0)
578                 cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
579         cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
580         value = ctl_get_opt(&cbe_lun->options, "serseq");
581         if (value != NULL && strcmp(value, "on") == 0)
582                 cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
583         else if (value != NULL && strcmp(value, "read") == 0)
584                 cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
585         else if (value != NULL && strcmp(value, "off") == 0)
586                 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
587
588         if (params->flags & CTL_LUN_FLAG_ID_REQ) {
589                 cbe_lun->req_lun_id = params->req_lun_id;
590                 cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
591         } else
592                 cbe_lun->req_lun_id = 0;
593
594         cbe_lun->lun_shutdown = ctl_backend_ramdisk_lun_shutdown;
595         cbe_lun->lun_config_status = ctl_backend_ramdisk_lun_config_status;
596         cbe_lun->be = &ctl_be_ramdisk_driver;
597         if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
598                 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
599                          softc->num_luns);
600                 strncpy((char *)cbe_lun->serial_num, tmpstr,
601                         MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
602
603                 /* Tell the user what we used for a serial number */
604                 strncpy((char *)params->serial_num, tmpstr,
605                         MIN(sizeof(params->serial_num), sizeof(tmpstr)));
606         } else { 
607                 strncpy((char *)cbe_lun->serial_num, params->serial_num,
608                         MIN(sizeof(cbe_lun->serial_num),
609                             sizeof(params->serial_num)));
610         }
611         if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
612                 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
613                 strncpy((char *)cbe_lun->device_id, tmpstr,
614                         MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
615
616                 /* Tell the user what we used for a device ID */
617                 strncpy((char *)params->device_id, tmpstr,
618                         MIN(sizeof(params->device_id), sizeof(tmpstr)));
619         } else {
620                 strncpy((char *)cbe_lun->device_id, params->device_id,
621                         MIN(sizeof(cbe_lun->device_id),
622                             sizeof(params->device_id)));
623         }
624
625         STAILQ_INIT(&be_lun->cont_queue);
626         mtx_init(&be_lun->queue_lock, "cram queue lock", NULL, MTX_DEF);
627         TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_backend_ramdisk_worker,
628             be_lun);
629
630         be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
631             taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
632         if (be_lun->io_taskqueue == NULL) {
633                 snprintf(req->error_str, sizeof(req->error_str),
634                          "%s: Unable to create taskqueue", __func__);
635                 goto bailout_error;
636         }
637
638         retval = taskqueue_start_threads(&be_lun->io_taskqueue,
639                                          /*num threads*/1,
640                                          /*priority*/PWAIT,
641                                          /*thread name*/
642                                          "%s taskq", be_lun->lunname);
643         if (retval != 0)
644                 goto bailout_error;
645
646         mtx_lock(&softc->lock);
647         softc->num_luns++;
648         STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
649
650         mtx_unlock(&softc->lock);
651
652         retval = ctl_add_lun(&be_lun->cbe_lun);
653         if (retval != 0) {
654                 mtx_lock(&softc->lock);
655                 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
656                               links);
657                 softc->num_luns--;
658                 mtx_unlock(&softc->lock);
659                 snprintf(req->error_str, sizeof(req->error_str),
660                          "%s: ctl_add_lun() returned error %d, see dmesg for "
661                         "details", __func__, retval);
662                 retval = 0;
663                 goto bailout_error;
664         }
665
666         mtx_lock(&softc->lock);
667
668         /*
669          * Tell the config_status routine that we're waiting so it won't
670          * clean up the LUN in the event of an error.
671          */
672         be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
673
674         while (be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) {
675                 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
676                 if (retval == EINTR)
677                         break;
678         }
679         be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
680
681         if (be_lun->flags & CTL_BE_RAMDISK_LUN_CONFIG_ERR) {
682                 snprintf(req->error_str, sizeof(req->error_str),
683                          "%s: LUN configuration error, see dmesg for details",
684                          __func__);
685                 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
686                               links);
687                 softc->num_luns--;
688                 mtx_unlock(&softc->lock);
689                 goto bailout_error;
690         } else {
691                 params->req_lun_id = cbe_lun->lun_id;
692         }
693         mtx_unlock(&softc->lock);
694
695         req->status = CTL_LUN_OK;
696
697         return (retval);
698
699 bailout_error:
700         req->status = CTL_LUN_ERROR;
701         if (be_lun != NULL) {
702                 if (be_lun->io_taskqueue != NULL) {
703                         taskqueue_free(be_lun->io_taskqueue);
704                 }
705                 ctl_free_opts(&cbe_lun->options);
706                 mtx_destroy(&be_lun->queue_lock);
707                 free(be_lun, M_RAMDISK);
708         }
709
710         return (retval);
711 }
712
713 static int
714 ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
715                        struct ctl_lun_req *req)
716 {
717         struct ctl_be_ramdisk_lun *be_lun;
718         struct ctl_be_lun *cbe_lun;
719         struct ctl_lun_modify_params *params;
720         uint32_t blocksize;
721
722         params = &req->reqdata.modify;
723
724         mtx_lock(&softc->lock);
725         STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
726                 if (be_lun->cbe_lun.lun_id == params->lun_id)
727                         break;
728         }
729         mtx_unlock(&softc->lock);
730
731         if (be_lun == NULL) {
732                 snprintf(req->error_str, sizeof(req->error_str),
733                          "%s: LUN %u is not managed by the ramdisk backend",
734                          __func__, params->lun_id);
735                 goto bailout_error;
736         }
737         cbe_lun = &be_lun->cbe_lun;
738
739         if (params->lun_size_bytes != 0)
740                 be_lun->params.lun_size_bytes = params->lun_size_bytes;
741         ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args);
742         blocksize = be_lun->cbe_lun.blocksize;
743
744         if (be_lun->params.lun_size_bytes < blocksize) {
745                 snprintf(req->error_str, sizeof(req->error_str),
746                         "%s: LUN size %ju < blocksize %u", __func__,
747                         be_lun->params.lun_size_bytes, blocksize);
748                 goto bailout_error;
749         }
750
751         be_lun->size_blocks = be_lun->params.lun_size_bytes / blocksize;
752         be_lun->size_bytes = be_lun->size_blocks * blocksize;
753         be_lun->cbe_lun.maxlba = be_lun->size_blocks - 1;
754         ctl_lun_capacity_changed(&be_lun->cbe_lun);
755
756         /* Tell the user the exact size we ended up using */
757         params->lun_size_bytes = be_lun->size_bytes;
758
759         req->status = CTL_LUN_OK;
760
761         return (0);
762
763 bailout_error:
764         req->status = CTL_LUN_ERROR;
765
766         return (0);
767 }
768
769 static void
770 ctl_backend_ramdisk_lun_shutdown(void *be_lun)
771 {
772         struct ctl_be_ramdisk_lun *lun;
773         struct ctl_be_ramdisk_softc *softc;
774         int do_free;
775
776         lun = (struct ctl_be_ramdisk_lun *)be_lun;
777         softc = lun->softc;
778         do_free = 0;
779
780         mtx_lock(&softc->lock);
781
782         lun->flags |= CTL_BE_RAMDISK_LUN_UNCONFIGURED;
783
784         if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
785                 wakeup(lun);
786         } else {
787                 STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
788                               links);
789                 softc->num_luns--;
790                 do_free = 1;
791         }
792
793         mtx_unlock(&softc->lock);
794
795         if (do_free != 0)
796                 free(be_lun, M_RAMDISK);
797 }
798
799 static void
800 ctl_backend_ramdisk_lun_config_status(void *be_lun,
801                                       ctl_lun_config_status status)
802 {
803         struct ctl_be_ramdisk_lun *lun;
804         struct ctl_be_ramdisk_softc *softc;
805
806         lun = (struct ctl_be_ramdisk_lun *)be_lun;
807         softc = lun->softc;
808
809         if (status == CTL_LUN_CONFIG_OK) {
810                 mtx_lock(&softc->lock);
811                 lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
812                 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING)
813                         wakeup(lun);
814                 mtx_unlock(&softc->lock);
815
816                 /*
817                  * We successfully added the LUN, attempt to enable it.
818                  */
819                 if (ctl_enable_lun(&lun->cbe_lun) != 0) {
820                         printf("%s: ctl_enable_lun() failed!\n", __func__);
821                         if (ctl_invalidate_lun(&lun->cbe_lun) != 0) {
822                                 printf("%s: ctl_invalidate_lun() failed!\n",
823                                        __func__);
824                         }
825                 }
826
827                 return;
828         }
829
830
831         mtx_lock(&softc->lock);
832         lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
833
834         /*
835          * If we have a user waiting, let him handle the cleanup.  If not,
836          * clean things up here.
837          */
838         if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
839                 lun->flags |= CTL_BE_RAMDISK_LUN_CONFIG_ERR;
840                 wakeup(lun);
841         } else {
842                 STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
843                               links);
844                 softc->num_luns--;
845                 free(lun, M_RAMDISK);
846         }
847         mtx_unlock(&softc->lock);
848 }
849
850 static int
851 ctl_backend_ramdisk_config_write(union ctl_io *io)
852 {
853         struct ctl_be_ramdisk_softc *softc;
854         int retval;
855
856         retval = 0;
857         softc = &rd_softc;
858
859         switch (io->scsiio.cdb[0]) {
860         case SYNCHRONIZE_CACHE:
861         case SYNCHRONIZE_CACHE_16:
862                 /*
863                  * The upper level CTL code will filter out any CDBs with
864                  * the immediate bit set and return the proper error.  It
865                  * will also not allow a sync cache command to go to a LUN
866                  * that is powered down.
867                  *
868                  * We don't really need to worry about what LBA range the
869                  * user asked to be synced out.  When they issue a sync
870                  * cache command, we'll sync out the whole thing.
871                  *
872                  * This is obviously just a stubbed out implementation.
873                  * The real implementation will be in the RAIDCore/CTL
874                  * interface, and can only really happen when RAIDCore
875                  * implements a per-array cache sync.
876                  */
877                 ctl_set_success(&io->scsiio);
878                 ctl_config_write_done(io);
879                 break;
880         case START_STOP_UNIT: {
881                 struct scsi_start_stop_unit *cdb;
882                 struct ctl_be_lun *cbe_lun;
883                 struct ctl_be_ramdisk_lun *be_lun;
884
885                 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
886
887                 cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
888                         CTL_PRIV_BACKEND_LUN].ptr;
889                 be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun->be_lun;
890
891                 if (cdb->how & SSS_START)
892                         retval = ctl_start_lun(cbe_lun);
893                 else {
894                         retval = ctl_stop_lun(cbe_lun);
895 #ifdef NEEDTOPORT
896                         if ((retval == 0)
897                          && (cdb->byte2 & SSS_ONOFFLINE))
898                                 retval = ctl_lun_offline(cbe_lun);
899 #endif
900                 }
901
902                 /*
903                  * In general, the above routines should not fail.  They
904                  * just set state for the LUN.  So we've got something
905                  * pretty wrong here if we can't start or stop the LUN.
906                  */
907                 if (retval != 0) {
908                         ctl_set_internal_failure(&io->scsiio,
909                                                  /*sks_valid*/ 1,
910                                                  /*retry_count*/ 0xf051);
911                         retval = CTL_RETVAL_COMPLETE;
912                 } else {
913                         ctl_set_success(&io->scsiio);
914                 }
915                 ctl_config_write_done(io);
916                 break;
917         }
918         case WRITE_SAME_10:
919         case WRITE_SAME_16:
920         case UNMAP:
921                 ctl_set_success(&io->scsiio);
922                 ctl_config_write_done(io);
923                 break;
924         default:
925                 ctl_set_invalid_opcode(&io->scsiio);
926                 ctl_config_write_done(io);
927                 retval = CTL_RETVAL_COMPLETE;
928                 break;
929         }
930
931         return (retval);
932 }
933
934 static int
935 ctl_backend_ramdisk_config_read(union ctl_io *io)
936 {
937         int retval = 0;
938
939         switch (io->scsiio.cdb[0]) {
940         case SERVICE_ACTION_IN:
941                 if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
942                         /* We have nothing to tell, leave default data. */
943                         ctl_config_read_done(io);
944                         retval = CTL_RETVAL_COMPLETE;
945                         break;
946                 }
947                 ctl_set_invalid_field(&io->scsiio,
948                                       /*sks_valid*/ 1,
949                                       /*command*/ 1,
950                                       /*field*/ 1,
951                                       /*bit_valid*/ 1,
952                                       /*bit*/ 4);
953                 ctl_config_read_done(io);
954                 retval = CTL_RETVAL_COMPLETE;
955                 break;
956         default:
957                 ctl_set_invalid_opcode(&io->scsiio);
958                 ctl_config_read_done(io);
959                 retval = CTL_RETVAL_COMPLETE;
960                 break;
961         }
962
963         return (retval);
964 }