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_frontend_internal.h> 66 #include <cam/ctl/ctl_debug.h> 67 #include <cam/ctl/ctl_ioctl.h> 68 #include <cam/ctl/ctl_error.h> 69 70 typedef enum { 71 CTL_BE_RAMDISK_LUN_UNCONFIGURED = 0x01, 72 CTL_BE_RAMDISK_LUN_CONFIG_ERR = 0x02, 73 CTL_BE_RAMDISK_LUN_WAITING = 0x04 74 } ctl_be_ramdisk_lun_flags; 75 76 struct ctl_be_ramdisk_lun { 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 ctl_be_lun; 84 struct taskqueue *io_taskqueue; 85 struct task io_task; 86 STAILQ_HEAD(, ctl_io_hdr) cont_queue; 87 struct mtx 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, int do_wait); 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, "ramdisk", 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->ctl_be_lun); 199 ctl_invalidate_lun(&lun->ctl_be_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 *ctl_be_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 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 230 CTL_PRIV_BACKEND_LUN].ptr; 231 be_lun = (struct ctl_be_ramdisk_lun *)ctl_be_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.port_status == 0) 242 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 243 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 244 if (io->scsiio.kern_rel_offset < io->scsiio.kern_total_len) { 245 mtx_lock(&be_lun->lock); 246 STAILQ_INSERT_TAIL(&be_lun->cont_queue, 247 &io->io_hdr, links); 248 mtx_unlock(&be_lun->lock); 249 taskqueue_enqueue(be_lun->io_taskqueue, 250 &be_lun->io_task); 251 return (0); 252 } 253 io->io_hdr.status = CTL_SUCCESS; 254 } else if ((io->io_hdr.port_status != 0) 255 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 256 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 257 /* 258 * For hardware error sense keys, the sense key 259 * specific value is defined to be a retry count, 260 * but we use it to pass back an internal FETD 261 * error code. XXX KDM Hopefully the FETD is only 262 * using 16 bits for an error code, since that's 263 * all the space we have in the sks field. 264 */ 265 ctl_set_internal_failure(&io->scsiio, 266 /*sks_valid*/ 1, 267 /*retry_count*/ 268 io->io_hdr.port_status); 269 } 270 ctl_done(io); 271 return(0); 272 } 273 274 static int 275 ctl_backend_ramdisk_submit(union ctl_io *io) 276 { 277 struct ctl_lba_len lbalen; 278 struct ctl_be_lun *ctl_be_lun; 279 280 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 281 CTL_PRIV_BACKEND_LUN].ptr; 282 283 memcpy(&lbalen, io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 284 sizeof(lbalen)); 285 io->scsiio.be_move_done = ctl_backend_ramdisk_move_done; 286 io->scsiio.kern_total_len = lbalen.len * ctl_be_lun->blocksize; 287 io->scsiio.kern_rel_offset = 0; 288 io->scsiio.kern_data_resid = 0; 289 ctl_backend_ramdisk_continue(io); 290 291 return (CTL_RETVAL_COMPLETE); 292 } 293 294 static void 295 ctl_backend_ramdisk_continue(union ctl_io *io) 296 { 297 struct ctl_be_ramdisk_softc *softc; 298 int len, len_filled, sg_filled; 299 #ifdef CTL_RAMDISK_PAGES 300 struct ctl_sg_entry *sg_entries; 301 int i; 302 #endif 303 304 softc = &rd_softc; 305 len = io->scsiio.kern_total_len - io->scsiio.kern_rel_offset; 306 #ifdef CTL_RAMDISK_PAGES 307 sg_filled = min(btoc(len), softc->num_pages); 308 if (sg_filled > 1) { 309 io->scsiio.kern_data_ptr = malloc(sizeof(struct ctl_sg_entry) * 310 sg_filled, M_RAMDISK, 311 M_WAITOK); 312 sg_entries = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 313 for (i = 0, len_filled = 0; i < sg_filled; i++) { 314 sg_entries[i].addr = softc->ramdisk_pages[i]; 315 sg_entries[i].len = ctl_min(PAGE_SIZE, 316 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.kern_data_len = len_filled; 332 io->scsiio.kern_sg_entries = sg_filled; 333 io->io_hdr.flags |= CTL_FLAG_ALLOCATED; 334 #ifdef CTL_TIME_IO 335 getbintime(&io->io_hdr.dma_start_bt); 336 #endif 337 ctl_datamove(io); 338 } 339 340 static void 341 ctl_backend_ramdisk_worker(void *context, int pending) 342 { 343 struct ctl_be_ramdisk_softc *softc; 344 struct ctl_be_ramdisk_lun *be_lun; 345 union ctl_io *io; 346 347 be_lun = (struct ctl_be_ramdisk_lun *)context; 348 softc = be_lun->softc; 349 350 mtx_lock(&be_lun->lock); 351 for (;;) { 352 io = (union ctl_io *)STAILQ_FIRST(&be_lun->cont_queue); 353 if (io != NULL) { 354 STAILQ_REMOVE(&be_lun->cont_queue, &io->io_hdr, 355 ctl_io_hdr, links); 356 357 mtx_unlock(&be_lun->lock); 358 359 ctl_backend_ramdisk_continue(io); 360 361 mtx_lock(&be_lun->lock); 362 continue; 363 } 364 365 /* 366 * If we get here, there is no work left in the queues, so 367 * just break out and let the task queue go to sleep. 368 */ 369 break; 370 } 371 mtx_unlock(&be_lun->lock); 372 } 373 374 static int 375 ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 376 int flag, struct thread *td) 377 { 378 struct ctl_be_ramdisk_softc *softc; 379 int retval; 380 381 retval = 0; 382 softc = &rd_softc; 383 384 switch (cmd) { 385 case CTL_LUN_REQ: { 386 struct ctl_lun_req *lun_req; 387 388 lun_req = (struct ctl_lun_req *)addr; 389 390 switch (lun_req->reqtype) { 391 case CTL_LUNREQ_CREATE: 392 retval = ctl_backend_ramdisk_create(softc, lun_req, 393 /*do_wait*/ 1); 394 break; 395 case CTL_LUNREQ_RM: 396 retval = ctl_backend_ramdisk_rm(softc, lun_req); 397 break; 398 case CTL_LUNREQ_MODIFY: 399 retval = ctl_backend_ramdisk_modify(softc, lun_req); 400 break; 401 default: 402 lun_req->status = CTL_LUN_ERROR; 403 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 404 "%s: invalid LUN request type %d", __func__, 405 lun_req->reqtype); 406 break; 407 } 408 break; 409 } 410 default: 411 retval = ENOTTY; 412 break; 413 } 414 415 return (retval); 416 } 417 418 static int 419 ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc, 420 struct ctl_lun_req *req) 421 { 422 struct ctl_be_ramdisk_lun *be_lun; 423 struct ctl_lun_rm_params *params; 424 int retval; 425 426 427 retval = 0; 428 params = &req->reqdata.rm; 429 430 be_lun = NULL; 431 432 mtx_lock(&softc->lock); 433 434 STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 435 if (be_lun->ctl_be_lun.lun_id == params->lun_id) 436 break; 437 } 438 mtx_unlock(&softc->lock); 439 440 if (be_lun == NULL) { 441 snprintf(req->error_str, sizeof(req->error_str), 442 "%s: LUN %u is not managed by the ramdisk backend", 443 __func__, params->lun_id); 444 goto bailout_error; 445 } 446 447 retval = ctl_disable_lun(&be_lun->ctl_be_lun); 448 449 if (retval != 0) { 450 snprintf(req->error_str, sizeof(req->error_str), 451 "%s: error %d returned from ctl_disable_lun() for " 452 "LUN %d", __func__, retval, params->lun_id); 453 goto bailout_error; 454 } 455 456 /* 457 * Set the waiting flag before we invalidate the LUN. Our shutdown 458 * routine can be called any time after we invalidate the LUN, 459 * and can be called from our context. 460 * 461 * This tells the shutdown routine that we're waiting, or we're 462 * going to wait for the shutdown to happen. 463 */ 464 mtx_lock(&softc->lock); 465 be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING; 466 mtx_unlock(&softc->lock); 467 468 retval = ctl_invalidate_lun(&be_lun->ctl_be_lun); 469 if (retval != 0) { 470 snprintf(req->error_str, sizeof(req->error_str), 471 "%s: error %d returned from ctl_invalidate_lun() for " 472 "LUN %d", __func__, retval, params->lun_id); 473 mtx_lock(&softc->lock); 474 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING; 475 mtx_unlock(&softc->lock); 476 goto bailout_error; 477 } 478 479 mtx_lock(&softc->lock); 480 481 while ((be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) == 0) { 482 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0); 483 if (retval == EINTR) 484 break; 485 } 486 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING; 487 488 /* 489 * We only remove this LUN from the list and free it (below) if 490 * retval == 0. If the user interrupted the wait, we just bail out 491 * without actually freeing the LUN. We let the shutdown routine 492 * free the LUN if that happens. 493 */ 494 if (retval == 0) { 495 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun, 496 links); 497 softc->num_luns--; 498 } 499 500 mtx_unlock(&softc->lock); 501 502 if (retval == 0) { 503 taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 504 taskqueue_free(be_lun->io_taskqueue); 505 mtx_destroy(&be_lun->lock); 506 free(be_lun, M_RAMDISK); 507 } 508 509 req->status = CTL_LUN_OK; 510 511 return (retval); 512 513 bailout_error: 514 req->status = CTL_LUN_ERROR; 515 516 return (0); 517 } 518 519 static int 520 ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc, 521 struct ctl_lun_req *req, int do_wait) 522 { 523 struct ctl_be_ramdisk_lun *be_lun; 524 struct ctl_lun_create_params *params; 525 uint32_t blocksize; 526 char tmpstr[32]; 527 int i, retval, unmap; 528 529 retval = 0; 530 params = &req->reqdata.create; 531 if (params->blocksize_bytes != 0) 532 blocksize = params->blocksize_bytes; 533 else 534 blocksize = 512; 535 536 be_lun = malloc(sizeof(*be_lun), M_RAMDISK, M_ZERO | (do_wait ? 537 M_WAITOK : M_NOWAIT)); 538 539 if (be_lun == NULL) { 540 snprintf(req->error_str, sizeof(req->error_str), 541 "%s: error allocating %zd bytes", __func__, 542 sizeof(*be_lun)); 543 goto bailout_error; 544 } 545 sprintf(be_lun->lunname, "cram%d", softc->num_luns); 546 STAILQ_INIT(&be_lun->ctl_be_lun.options); 547 548 if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 549 be_lun->ctl_be_lun.lun_type = params->device_type; 550 else 551 be_lun->ctl_be_lun.lun_type = T_DIRECT; 552 553 if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { 554 555 if (params->lun_size_bytes < blocksize) { 556 snprintf(req->error_str, sizeof(req->error_str), 557 "%s: LUN size %ju < blocksize %u", __func__, 558 params->lun_size_bytes, blocksize); 559 goto bailout_error; 560 } 561 562 be_lun->size_blocks = params->lun_size_bytes / blocksize; 563 be_lun->size_bytes = be_lun->size_blocks * blocksize; 564 565 be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1; 566 } else { 567 be_lun->ctl_be_lun.maxlba = 0; 568 blocksize = 0; 569 be_lun->size_bytes = 0; 570 be_lun->size_blocks = 0; 571 } 572 573 be_lun->ctl_be_lun.blocksize = blocksize; 574 575 /* Tell the user the blocksize we ended up using */ 576 params->blocksize_bytes = blocksize; 577 578 /* Tell the user the exact size we ended up using */ 579 params->lun_size_bytes = be_lun->size_bytes; 580 581 be_lun->softc = softc; 582 583 unmap = 0; 584 for (i = 0; i < req->num_be_args; i++) { 585 if (strcmp(req->kern_be_args[i].kname, "unmap") == 0 && 586 strcmp(req->kern_be_args[i].kvalue, "on") == 0) { 587 unmap = 1; 588 } else { 589 struct ctl_be_lun_option *opt; 590 591 opt = malloc(sizeof(*opt), M_RAMDISK, M_WAITOK); 592 opt->name = malloc(strlen(req->kern_be_args[i].kname) + 1, M_RAMDISK, M_WAITOK); 593 strcpy(opt->name, req->kern_be_args[i].kname); 594 opt->value = malloc(strlen(req->kern_be_args[i].kvalue) + 1, M_RAMDISK, M_WAITOK); 595 strcpy(opt->value, req->kern_be_args[i].kvalue); 596 STAILQ_INSERT_TAIL(&be_lun->ctl_be_lun.options, opt, links); 597 } 598 } 599 600 be_lun->flags = CTL_BE_RAMDISK_LUN_UNCONFIGURED; 601 be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; 602 if (unmap) 603 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; 604 be_lun->ctl_be_lun.be_lun = be_lun; 605 606 if (params->flags & CTL_LUN_FLAG_ID_REQ) { 607 be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; 608 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; 609 } else 610 be_lun->ctl_be_lun.req_lun_id = 0; 611 612 be_lun->ctl_be_lun.lun_shutdown = ctl_backend_ramdisk_lun_shutdown; 613 be_lun->ctl_be_lun.lun_config_status = 614 ctl_backend_ramdisk_lun_config_status; 615 be_lun->ctl_be_lun.be = &ctl_be_ramdisk_driver; 616 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 617 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 618 softc->num_luns); 619 strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, 620 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 621 sizeof(tmpstr))); 622 623 /* Tell the user what we used for a serial number */ 624 strncpy((char *)params->serial_num, tmpstr, 625 ctl_min(sizeof(params->serial_num), sizeof(tmpstr))); 626 } else { 627 strncpy((char *)be_lun->ctl_be_lun.serial_num, 628 params->serial_num, 629 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 630 sizeof(params->serial_num))); 631 } 632 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 633 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 634 strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, 635 ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 636 sizeof(tmpstr))); 637 638 /* Tell the user what we used for a device ID */ 639 strncpy((char *)params->device_id, tmpstr, 640 ctl_min(sizeof(params->device_id), sizeof(tmpstr))); 641 } else { 642 strncpy((char *)be_lun->ctl_be_lun.device_id, 643 params->device_id, 644 ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 645 sizeof(params->device_id))); 646 } 647 648 STAILQ_INIT(&be_lun->cont_queue); 649 mtx_init(&be_lun->lock, "CTL ramdisk", NULL, MTX_DEF); 650 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_backend_ramdisk_worker, 651 be_lun); 652 653 be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 654 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 655 if (be_lun->io_taskqueue == NULL) { 656 snprintf(req->error_str, sizeof(req->error_str), 657 "%s: Unable to create taskqueue", __func__); 658 goto bailout_error; 659 } 660 661 retval = taskqueue_start_threads(&be_lun->io_taskqueue, 662 /*num threads*/1, 663 /*priority*/PWAIT, 664 /*thread name*/ 665 "%s taskq", be_lun->lunname); 666 if (retval != 0) 667 goto bailout_error; 668 669 mtx_lock(&softc->lock); 670 softc->num_luns++; 671 STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 672 673 mtx_unlock(&softc->lock); 674 675 retval = ctl_add_lun(&be_lun->ctl_be_lun); 676 if (retval != 0) { 677 mtx_lock(&softc->lock); 678 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun, 679 links); 680 softc->num_luns--; 681 mtx_unlock(&softc->lock); 682 snprintf(req->error_str, sizeof(req->error_str), 683 "%s: ctl_add_lun() returned error %d, see dmesg for " 684 "details", __func__, retval); 685 retval = 0; 686 goto bailout_error; 687 } 688 689 if (do_wait == 0) 690 return (retval); 691 692 mtx_lock(&softc->lock); 693 694 /* 695 * Tell the config_status routine that we're waiting so it won't 696 * clean up the LUN in the event of an error. 697 */ 698 be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING; 699 700 while (be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) { 701 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0); 702 if (retval == EINTR) 703 break; 704 } 705 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING; 706 707 if (be_lun->flags & CTL_BE_RAMDISK_LUN_CONFIG_ERR) { 708 snprintf(req->error_str, sizeof(req->error_str), 709 "%s: LUN configuration error, see dmesg for details", 710 __func__); 711 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun, 712 links); 713 softc->num_luns--; 714 mtx_unlock(&softc->lock); 715 goto bailout_error; 716 } else { 717 params->req_lun_id = be_lun->ctl_be_lun.lun_id; 718 } 719 mtx_unlock(&softc->lock); 720 721 req->status = CTL_LUN_OK; 722 723 return (retval); 724 725 bailout_error: 726 req->status = CTL_LUN_ERROR; 727 if (be_lun != NULL) { 728 if (be_lun->io_taskqueue != NULL) { 729 taskqueue_free(be_lun->io_taskqueue); 730 } 731 mtx_destroy(&be_lun->lock); 732 free(be_lun, M_RAMDISK); 733 } 734 735 return (retval); 736 } 737 738 static int 739 ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc, 740 struct ctl_lun_req *req) 741 { 742 struct ctl_be_ramdisk_lun *be_lun; 743 struct ctl_lun_modify_params *params; 744 uint32_t blocksize; 745 746 params = &req->reqdata.modify; 747 748 be_lun = NULL; 749 750 mtx_lock(&softc->lock); 751 STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 752 if (be_lun->ctl_be_lun.lun_id == params->lun_id) 753 break; 754 } 755 mtx_unlock(&softc->lock); 756 757 if (be_lun == NULL) { 758 snprintf(req->error_str, sizeof(req->error_str), 759 "%s: LUN %u is not managed by the ramdisk backend", 760 __func__, params->lun_id); 761 goto bailout_error; 762 } 763 764 if (params->lun_size_bytes == 0) { 765 snprintf(req->error_str, sizeof(req->error_str), 766 "%s: LUN size \"auto\" not supported " 767 "by the ramdisk backend", __func__); 768 goto bailout_error; 769 } 770 771 blocksize = be_lun->ctl_be_lun.blocksize; 772 773 if (params->lun_size_bytes < blocksize) { 774 snprintf(req->error_str, sizeof(req->error_str), 775 "%s: LUN size %ju < blocksize %u", __func__, 776 params->lun_size_bytes, blocksize); 777 goto bailout_error; 778 } 779 780 be_lun->size_blocks = params->lun_size_bytes / blocksize; 781 be_lun->size_bytes = be_lun->size_blocks * blocksize; 782 783 /* 784 * The maximum LBA is the size - 1. 785 * 786 * XXX: Note that this field is being updated without locking, 787 * which might cause problems on 32-bit architectures. 788 */ 789 be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1; 790 ctl_lun_capacity_changed(&be_lun->ctl_be_lun); 791 792 /* Tell the user the exact size we ended up using */ 793 params->lun_size_bytes = be_lun->size_bytes; 794 795 req->status = CTL_LUN_OK; 796 797 return (0); 798 799 bailout_error: 800 req->status = CTL_LUN_ERROR; 801 802 return (0); 803 } 804 805 static void 806 ctl_backend_ramdisk_lun_shutdown(void *be_lun) 807 { 808 struct ctl_be_ramdisk_lun *lun; 809 struct ctl_be_ramdisk_softc *softc; 810 int do_free; 811 812 lun = (struct ctl_be_ramdisk_lun *)be_lun; 813 softc = lun->softc; 814 do_free = 0; 815 816 mtx_lock(&softc->lock); 817 818 lun->flags |= CTL_BE_RAMDISK_LUN_UNCONFIGURED; 819 820 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) { 821 wakeup(lun); 822 } else { 823 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun, 824 links); 825 softc->num_luns--; 826 do_free = 1; 827 } 828 829 mtx_unlock(&softc->lock); 830 831 if (do_free != 0) 832 free(be_lun, M_RAMDISK); 833 } 834 835 static void 836 ctl_backend_ramdisk_lun_config_status(void *be_lun, 837 ctl_lun_config_status status) 838 { 839 struct ctl_be_ramdisk_lun *lun; 840 struct ctl_be_ramdisk_softc *softc; 841 842 lun = (struct ctl_be_ramdisk_lun *)be_lun; 843 softc = lun->softc; 844 845 if (status == CTL_LUN_CONFIG_OK) { 846 mtx_lock(&softc->lock); 847 lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED; 848 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) 849 wakeup(lun); 850 mtx_unlock(&softc->lock); 851 852 /* 853 * We successfully added the LUN, attempt to enable it. 854 */ 855 if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { 856 printf("%s: ctl_enable_lun() failed!\n", __func__); 857 if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { 858 printf("%s: ctl_invalidate_lun() failed!\n", 859 __func__); 860 } 861 } 862 863 return; 864 } 865 866 867 mtx_lock(&softc->lock); 868 lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED; 869 870 /* 871 * If we have a user waiting, let him handle the cleanup. If not, 872 * clean things up here. 873 */ 874 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) { 875 lun->flags |= CTL_BE_RAMDISK_LUN_CONFIG_ERR; 876 wakeup(lun); 877 } else { 878 STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun, 879 links); 880 softc->num_luns--; 881 free(lun, M_RAMDISK); 882 } 883 mtx_unlock(&softc->lock); 884 } 885 886 static int 887 ctl_backend_ramdisk_config_write(union ctl_io *io) 888 { 889 struct ctl_be_ramdisk_softc *softc; 890 int retval; 891 892 retval = 0; 893 softc = &rd_softc; 894 895 switch (io->scsiio.cdb[0]) { 896 case SYNCHRONIZE_CACHE: 897 case SYNCHRONIZE_CACHE_16: 898 /* 899 * The upper level CTL code will filter out any CDBs with 900 * the immediate bit set and return the proper error. It 901 * will also not allow a sync cache command to go to a LUN 902 * that is powered down. 903 * 904 * We don't really need to worry about what LBA range the 905 * user asked to be synced out. When they issue a sync 906 * cache command, we'll sync out the whole thing. 907 * 908 * This is obviously just a stubbed out implementation. 909 * The real implementation will be in the RAIDCore/CTL 910 * interface, and can only really happen when RAIDCore 911 * implements a per-array cache sync. 912 */ 913 ctl_set_success(&io->scsiio); 914 ctl_config_write_done(io); 915 break; 916 case START_STOP_UNIT: { 917 struct scsi_start_stop_unit *cdb; 918 struct ctl_be_lun *ctl_be_lun; 919 struct ctl_be_ramdisk_lun *be_lun; 920 921 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 922 923 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 924 CTL_PRIV_BACKEND_LUN].ptr; 925 be_lun = (struct ctl_be_ramdisk_lun *)ctl_be_lun->be_lun; 926 927 if (cdb->how & SSS_START) 928 retval = ctl_start_lun(ctl_be_lun); 929 else { 930 retval = ctl_stop_lun(ctl_be_lun); 931 #ifdef NEEDTOPORT 932 if ((retval == 0) 933 && (cdb->byte2 & SSS_ONOFFLINE)) 934 retval = ctl_lun_offline(ctl_be_lun); 935 #endif 936 } 937 938 /* 939 * In general, the above routines should not fail. They 940 * just set state for the LUN. So we've got something 941 * pretty wrong here if we can't start or stop the LUN. 942 */ 943 if (retval != 0) { 944 ctl_set_internal_failure(&io->scsiio, 945 /*sks_valid*/ 1, 946 /*retry_count*/ 0xf051); 947 retval = CTL_RETVAL_COMPLETE; 948 } else { 949 ctl_set_success(&io->scsiio); 950 } 951 ctl_config_write_done(io); 952 break; 953 } 954 case WRITE_SAME_10: 955 case WRITE_SAME_16: 956 case UNMAP: 957 ctl_set_success(&io->scsiio); 958 ctl_config_write_done(io); 959 break; 960 default: 961 ctl_set_invalid_opcode(&io->scsiio); 962 ctl_config_write_done(io); 963 retval = CTL_RETVAL_COMPLETE; 964 break; 965 } 966 967 return (retval); 968 } 969 970 static int 971 ctl_backend_ramdisk_config_read(union ctl_io *io) 972 { 973 /* 974 * XXX KDM need to implement!! 975 */ 976 return (0); 977 } 978