ctl_backend_ramdisk.c revision 254759
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 254759 2013-08-24 01:50:31Z 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); 444252569Smav mtx_lock(&softc->lock); 445252569Smav be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING; 446252569Smav mtx_unlock(&softc->lock); 447229997Sken goto bailout_error; 448229997Sken } 449229997Sken 450229997Sken mtx_lock(&softc->lock); 451229997Sken 452229997Sken while ((be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) == 0) { 453229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0); 454229997Sken if (retval == EINTR) 455229997Sken break; 456229997Sken } 457229997Sken be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING; 458229997Sken 459229997Sken /* 460229997Sken * We only remove this LUN from the list and free it (below) if 461229997Sken * retval == 0. If the user interrupted the wait, we just bail out 462229997Sken * without actually freeing the LUN. We let the shutdown routine 463229997Sken * free the LUN if that happens. 464229997Sken */ 465229997Sken if (retval == 0) { 466229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun, 467229997Sken links); 468229997Sken softc->num_luns--; 469229997Sken } 470229997Sken 471229997Sken mtx_unlock(&softc->lock); 472229997Sken 473229997Sken if (retval == 0) 474229997Sken free(be_lun, M_RAMDISK); 475229997Sken 476229997Sken req->status = CTL_LUN_OK; 477229997Sken 478229997Sken return (retval); 479229997Sken 480229997Skenbailout_error: 481229997Sken req->status = CTL_LUN_ERROR; 482229997Sken 483229997Sken return (0); 484229997Sken} 485229997Sken 486229997Skenstatic int 487229997Skenctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc, 488229997Sken struct ctl_lun_req *req, int do_wait) 489229997Sken{ 490229997Sken struct ctl_be_ramdisk_lun *be_lun; 491229997Sken struct ctl_lun_create_params *params; 492229997Sken uint32_t blocksize; 493229997Sken char tmpstr[32]; 494254759Strasz int i, retval; 495229997Sken 496229997Sken retval = 0; 497229997Sken params = &req->reqdata.create; 498229997Sken if (params->blocksize_bytes != 0) 499229997Sken blocksize = params->blocksize_bytes; 500229997Sken else 501229997Sken blocksize = 512; 502229997Sken 503229997Sken be_lun = malloc(sizeof(*be_lun), M_RAMDISK, M_ZERO | (do_wait ? 504229997Sken M_WAITOK : M_NOWAIT)); 505229997Sken 506229997Sken if (be_lun == NULL) { 507229997Sken snprintf(req->error_str, sizeof(req->error_str), 508229997Sken "%s: error allocating %zd bytes", __func__, 509229997Sken sizeof(*be_lun)); 510229997Sken goto bailout_error; 511229997Sken } 512254759Strasz STAILQ_INIT(&be_lun->ctl_be_lun.options); 513229997Sken 514229997Sken if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 515229997Sken be_lun->ctl_be_lun.lun_type = params->device_type; 516229997Sken else 517229997Sken be_lun->ctl_be_lun.lun_type = T_DIRECT; 518229997Sken 519229997Sken if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { 520229997Sken 521229997Sken if (params->lun_size_bytes < blocksize) { 522229997Sken snprintf(req->error_str, sizeof(req->error_str), 523229997Sken "%s: LUN size %ju < blocksize %u", __func__, 524229997Sken params->lun_size_bytes, blocksize); 525229997Sken goto bailout_error; 526229997Sken } 527229997Sken 528229997Sken be_lun->size_blocks = params->lun_size_bytes / blocksize; 529229997Sken be_lun->size_bytes = be_lun->size_blocks * blocksize; 530229997Sken 531229997Sken be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1; 532229997Sken } else { 533229997Sken be_lun->ctl_be_lun.maxlba = 0; 534229997Sken blocksize = 0; 535229997Sken be_lun->size_bytes = 0; 536229997Sken be_lun->size_blocks = 0; 537229997Sken } 538229997Sken 539229997Sken be_lun->ctl_be_lun.blocksize = blocksize; 540229997Sken 541229997Sken /* Tell the user the blocksize we ended up using */ 542229997Sken params->blocksize_bytes = blocksize; 543229997Sken 544229997Sken /* Tell the user the exact size we ended up using */ 545229997Sken params->lun_size_bytes = be_lun->size_bytes; 546229997Sken 547229997Sken be_lun->softc = softc; 548229997Sken 549254759Strasz for (i = 0; i < req->num_be_args; i++) { 550254759Strasz struct ctl_be_lun_option *opt; 551254759Strasz 552254759Strasz opt = malloc(sizeof(*opt), M_RAMDISK, M_WAITOK); 553254759Strasz opt->name = malloc(strlen(req->kern_be_args[i].kname) + 1, M_RAMDISK, M_WAITOK); 554254759Strasz strcpy(opt->name, req->kern_be_args[i].kname); 555254759Strasz opt->value = malloc(strlen(req->kern_be_args[i].kvalue) + 1, M_RAMDISK, M_WAITOK); 556254759Strasz strcpy(opt->value, req->kern_be_args[i].kvalue); 557254759Strasz STAILQ_INSERT_TAIL(&be_lun->ctl_be_lun.options, opt, links); 558254759Strasz } 559254759Strasz 560229997Sken be_lun->flags = CTL_BE_RAMDISK_LUN_UNCONFIGURED; 561229997Sken be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; 562229997Sken be_lun->ctl_be_lun.be_lun = be_lun; 563229997Sken 564229997Sken if (params->flags & CTL_LUN_FLAG_ID_REQ) { 565229997Sken be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; 566229997Sken be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; 567229997Sken } else 568229997Sken be_lun->ctl_be_lun.req_lun_id = 0; 569229997Sken 570229997Sken be_lun->ctl_be_lun.lun_shutdown = ctl_backend_ramdisk_lun_shutdown; 571229997Sken be_lun->ctl_be_lun.lun_config_status = 572229997Sken ctl_backend_ramdisk_lun_config_status; 573229997Sken be_lun->ctl_be_lun.be = &ctl_be_ramdisk_driver; 574229997Sken if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 575229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 576229997Sken softc->num_luns); 577229997Sken strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, 578229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 579229997Sken sizeof(tmpstr))); 580229997Sken 581229997Sken /* Tell the user what we used for a serial number */ 582229997Sken strncpy((char *)params->serial_num, tmpstr, 583229997Sken ctl_min(sizeof(params->serial_num), sizeof(tmpstr))); 584229997Sken } else { 585229997Sken strncpy((char *)be_lun->ctl_be_lun.serial_num, 586229997Sken params->serial_num, 587229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 588229997Sken sizeof(params->serial_num))); 589229997Sken } 590229997Sken if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 591229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 592229997Sken strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, 593229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 594229997Sken sizeof(tmpstr))); 595229997Sken 596229997Sken /* Tell the user what we used for a device ID */ 597229997Sken strncpy((char *)params->device_id, tmpstr, 598229997Sken ctl_min(sizeof(params->device_id), sizeof(tmpstr))); 599229997Sken } else { 600229997Sken strncpy((char *)be_lun->ctl_be_lun.device_id, 601229997Sken params->device_id, 602229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 603229997Sken sizeof(params->device_id))); 604229997Sken } 605229997Sken 606229997Sken mtx_lock(&softc->lock); 607229997Sken softc->num_luns++; 608229997Sken STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 609229997Sken 610229997Sken mtx_unlock(&softc->lock); 611229997Sken 612229997Sken retval = ctl_add_lun(&be_lun->ctl_be_lun); 613229997Sken if (retval != 0) { 614229997Sken mtx_lock(&softc->lock); 615229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun, 616229997Sken links); 617229997Sken softc->num_luns--; 618229997Sken mtx_unlock(&softc->lock); 619229997Sken snprintf(req->error_str, sizeof(req->error_str), 620229997Sken "%s: ctl_add_lun() returned error %d, see dmesg for " 621229997Sken "details", __func__, retval); 622229997Sken retval = 0; 623229997Sken goto bailout_error; 624229997Sken } 625229997Sken 626229997Sken if (do_wait == 0) 627229997Sken return (retval); 628229997Sken 629229997Sken mtx_lock(&softc->lock); 630229997Sken 631229997Sken /* 632229997Sken * Tell the config_status routine that we're waiting so it won't 633229997Sken * clean up the LUN in the event of an error. 634229997Sken */ 635229997Sken be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING; 636229997Sken 637229997Sken while (be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) { 638229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0); 639229997Sken if (retval == EINTR) 640229997Sken break; 641229997Sken } 642229997Sken be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING; 643229997Sken 644229997Sken if (be_lun->flags & CTL_BE_RAMDISK_LUN_CONFIG_ERR) { 645229997Sken snprintf(req->error_str, sizeof(req->error_str), 646229997Sken "%s: LUN configuration error, see dmesg for details", 647229997Sken __func__); 648229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun, 649229997Sken links); 650229997Sken softc->num_luns--; 651229997Sken mtx_unlock(&softc->lock); 652229997Sken goto bailout_error; 653229997Sken } else { 654229997Sken params->req_lun_id = be_lun->ctl_be_lun.lun_id; 655229997Sken } 656229997Sken mtx_unlock(&softc->lock); 657229997Sken 658229997Sken req->status = CTL_LUN_OK; 659229997Sken 660229997Sken return (retval); 661229997Sken 662229997Skenbailout_error: 663229997Sken req->status = CTL_LUN_ERROR; 664229997Sken free(be_lun, M_RAMDISK); 665229997Sken 666229997Sken return (retval); 667229997Sken} 668229997Sken 669232604Straszstatic int 670232604Straszctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc, 671232604Strasz struct ctl_lun_req *req) 672232604Strasz{ 673232604Strasz struct ctl_be_ramdisk_lun *be_lun; 674232604Strasz struct ctl_lun_modify_params *params; 675232604Strasz uint32_t blocksize; 676232604Strasz 677232604Strasz params = &req->reqdata.modify; 678232604Strasz 679232604Strasz be_lun = NULL; 680232604Strasz 681232604Strasz mtx_lock(&softc->lock); 682232604Strasz STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 683232604Strasz if (be_lun->ctl_be_lun.lun_id == params->lun_id) 684232604Strasz break; 685232604Strasz } 686232604Strasz mtx_unlock(&softc->lock); 687232604Strasz 688232604Strasz if (be_lun == NULL) { 689232604Strasz snprintf(req->error_str, sizeof(req->error_str), 690232604Strasz "%s: LUN %u is not managed by the ramdisk backend", 691232604Strasz __func__, params->lun_id); 692232604Strasz goto bailout_error; 693232604Strasz } 694232604Strasz 695232604Strasz if (params->lun_size_bytes == 0) { 696232604Strasz snprintf(req->error_str, sizeof(req->error_str), 697232604Strasz "%s: LUN size \"auto\" not supported " 698232604Strasz "by the ramdisk backend", __func__); 699232604Strasz goto bailout_error; 700232604Strasz } 701232604Strasz 702232604Strasz blocksize = be_lun->ctl_be_lun.blocksize; 703232604Strasz 704232604Strasz if (params->lun_size_bytes < blocksize) { 705232604Strasz snprintf(req->error_str, sizeof(req->error_str), 706232604Strasz "%s: LUN size %ju < blocksize %u", __func__, 707232604Strasz params->lun_size_bytes, blocksize); 708232604Strasz goto bailout_error; 709232604Strasz } 710232604Strasz 711232604Strasz be_lun->size_blocks = params->lun_size_bytes / blocksize; 712232604Strasz be_lun->size_bytes = be_lun->size_blocks * blocksize; 713232604Strasz 714232604Strasz /* 715232604Strasz * The maximum LBA is the size - 1. 716232604Strasz * 717232604Strasz * XXX: Note that this field is being updated without locking, 718232604Strasz * which might cause problems on 32-bit architectures. 719232604Strasz */ 720232604Strasz be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1; 721232604Strasz ctl_lun_capacity_changed(&be_lun->ctl_be_lun); 722232604Strasz 723232604Strasz /* Tell the user the exact size we ended up using */ 724232604Strasz params->lun_size_bytes = be_lun->size_bytes; 725232604Strasz 726232604Strasz req->status = CTL_LUN_OK; 727232604Strasz 728232604Strasz return (0); 729232604Strasz 730232604Straszbailout_error: 731232604Strasz req->status = CTL_LUN_ERROR; 732232604Strasz 733232604Strasz return (0); 734232604Strasz} 735232604Strasz 736229997Skenstatic void 737229997Skenctl_backend_ramdisk_lun_shutdown(void *be_lun) 738229997Sken{ 739229997Sken struct ctl_be_ramdisk_lun *lun; 740229997Sken struct ctl_be_ramdisk_softc *softc; 741229997Sken int do_free; 742229997Sken 743229997Sken lun = (struct ctl_be_ramdisk_lun *)be_lun; 744229997Sken softc = lun->softc; 745229997Sken do_free = 0; 746229997Sken 747229997Sken mtx_lock(&softc->lock); 748229997Sken 749229997Sken lun->flags |= CTL_BE_RAMDISK_LUN_UNCONFIGURED; 750229997Sken 751229997Sken if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) { 752229997Sken wakeup(lun); 753229997Sken } else { 754229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun, 755229997Sken links); 756229997Sken softc->num_luns--; 757229997Sken do_free = 1; 758229997Sken } 759229997Sken 760229997Sken mtx_unlock(&softc->lock); 761229997Sken 762229997Sken if (do_free != 0) 763229997Sken free(be_lun, M_RAMDISK); 764229997Sken} 765229997Sken 766229997Skenstatic void 767229997Skenctl_backend_ramdisk_lun_config_status(void *be_lun, 768229997Sken ctl_lun_config_status status) 769229997Sken{ 770229997Sken struct ctl_be_ramdisk_lun *lun; 771229997Sken struct ctl_be_ramdisk_softc *softc; 772229997Sken 773229997Sken lun = (struct ctl_be_ramdisk_lun *)be_lun; 774229997Sken softc = lun->softc; 775229997Sken 776229997Sken if (status == CTL_LUN_CONFIG_OK) { 777229997Sken mtx_lock(&softc->lock); 778229997Sken lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED; 779229997Sken if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) 780229997Sken wakeup(lun); 781229997Sken mtx_unlock(&softc->lock); 782229997Sken 783229997Sken /* 784229997Sken * We successfully added the LUN, attempt to enable it. 785229997Sken */ 786229997Sken if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { 787229997Sken printf("%s: ctl_enable_lun() failed!\n", __func__); 788229997Sken if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { 789229997Sken printf("%s: ctl_invalidate_lun() failed!\n", 790229997Sken __func__); 791229997Sken } 792229997Sken } 793229997Sken 794229997Sken return; 795229997Sken } 796229997Sken 797229997Sken 798229997Sken mtx_lock(&softc->lock); 799229997Sken lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED; 800229997Sken 801229997Sken /* 802229997Sken * If we have a user waiting, let him handle the cleanup. If not, 803229997Sken * clean things up here. 804229997Sken */ 805229997Sken if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) { 806229997Sken lun->flags |= CTL_BE_RAMDISK_LUN_CONFIG_ERR; 807229997Sken wakeup(lun); 808229997Sken } else { 809229997Sken STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun, 810229997Sken links); 811229997Sken softc->num_luns--; 812229997Sken free(lun, M_RAMDISK); 813229997Sken } 814229997Sken mtx_unlock(&softc->lock); 815229997Sken} 816229997Sken 817229997Skenstatic int 818229997Skenctl_backend_ramdisk_config_write(union ctl_io *io) 819229997Sken{ 820229997Sken struct ctl_be_ramdisk_softc *softc; 821229997Sken int retval; 822229997Sken 823229997Sken retval = 0; 824229997Sken softc = &rd_softc; 825229997Sken 826229997Sken switch (io->scsiio.cdb[0]) { 827229997Sken case SYNCHRONIZE_CACHE: 828229997Sken case SYNCHRONIZE_CACHE_16: 829229997Sken /* 830229997Sken * The upper level CTL code will filter out any CDBs with 831229997Sken * the immediate bit set and return the proper error. It 832229997Sken * will also not allow a sync cache command to go to a LUN 833229997Sken * that is powered down. 834229997Sken * 835229997Sken * We don't really need to worry about what LBA range the 836229997Sken * user asked to be synced out. When they issue a sync 837229997Sken * cache command, we'll sync out the whole thing. 838229997Sken * 839229997Sken * This is obviously just a stubbed out implementation. 840229997Sken * The real implementation will be in the RAIDCore/CTL 841229997Sken * interface, and can only really happen when RAIDCore 842229997Sken * implements a per-array cache sync. 843229997Sken */ 844229997Sken ctl_set_success(&io->scsiio); 845229997Sken ctl_config_write_done(io); 846229997Sken break; 847229997Sken case START_STOP_UNIT: { 848229997Sken struct scsi_start_stop_unit *cdb; 849229997Sken struct ctl_be_lun *ctl_be_lun; 850229997Sken struct ctl_be_ramdisk_lun *be_lun; 851229997Sken 852229997Sken cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 853229997Sken 854229997Sken ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 855229997Sken CTL_PRIV_BACKEND_LUN].ptr; 856229997Sken be_lun = (struct ctl_be_ramdisk_lun *)ctl_be_lun->be_lun; 857229997Sken 858229997Sken if (cdb->how & SSS_START) 859229997Sken retval = ctl_start_lun(ctl_be_lun); 860229997Sken else { 861229997Sken retval = ctl_stop_lun(ctl_be_lun); 862229997Sken#ifdef NEEDTOPORT 863229997Sken if ((retval == 0) 864229997Sken && (cdb->byte2 & SSS_ONOFFLINE)) 865229997Sken retval = ctl_lun_offline(ctl_be_lun); 866229997Sken#endif 867229997Sken } 868229997Sken 869229997Sken /* 870229997Sken * In general, the above routines should not fail. They 871229997Sken * just set state for the LUN. So we've got something 872229997Sken * pretty wrong here if we can't start or stop the LUN. 873229997Sken */ 874229997Sken if (retval != 0) { 875229997Sken ctl_set_internal_failure(&io->scsiio, 876229997Sken /*sks_valid*/ 1, 877229997Sken /*retry_count*/ 0xf051); 878229997Sken retval = CTL_RETVAL_COMPLETE; 879229997Sken } else { 880229997Sken ctl_set_success(&io->scsiio); 881229997Sken } 882229997Sken ctl_config_write_done(io); 883229997Sken break; 884229997Sken } 885229997Sken default: 886229997Sken ctl_set_invalid_opcode(&io->scsiio); 887229997Sken ctl_config_write_done(io); 888229997Sken retval = CTL_RETVAL_COMPLETE; 889229997Sken break; 890229997Sken } 891229997Sken 892229997Sken return (retval); 893229997Sken} 894229997Sken 895229997Skenstatic int 896229997Skenctl_backend_ramdisk_config_read(union ctl_io *io) 897229997Sken{ 898229997Sken /* 899229997Sken * XXX KDM need to implement!! 900229997Sken */ 901229997Sken return (0); 902229997Sken} 903