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