1 /* 2 * Copyright (C) 2013 Shaohua Li <shli@kernel.org> 3 * Copyright (C) 2014 Red Hat, Inc. 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms and conditions of the GNU General Public License, 7 * version 2, as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 * 14 * You should have received a copy of the GNU General Public License along with 15 * this program; if not, write to the Free Software Foundation, Inc., 16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 17 */ 18 19 #include <linux/spinlock.h> 20 #include <linux/module.h> 21 #include <linux/idr.h> 22 #include <linux/kernel.h> 23 #include <linux/timer.h> 24 #include <linux/parser.h> 25 #include <linux/vmalloc.h> 26 #include <linux/uio_driver.h> 27 #include <net/genetlink.h> 28 #include <scsi/scsi_common.h> 29 #include <scsi/scsi_proto.h> 30 #include <target/target_core_base.h> 31 #include <target/target_core_fabric.h> 32 #include <target/target_core_backend.h> 33 #include <target/target_core_backend_configfs.h> 34 35 #include <linux/target_core_user.h> 36 37 /* 38 * Define a shared-memory interface for LIO to pass SCSI commands and 39 * data to userspace for processing. This is to allow backends that 40 * are too complex for in-kernel support to be possible. 41 * 42 * It uses the UIO framework to do a lot of the device-creation and 43 * introspection work for us. 44 * 45 * See the .h file for how the ring is laid out. Note that while the 46 * command ring is defined, the particulars of the data area are 47 * not. Offset values in the command entry point to other locations 48 * internal to the mmap()ed area. There is separate space outside the 49 * command ring for data buffers. This leaves maximum flexibility for 50 * moving buffer allocations, or even page flipping or other 51 * allocation techniques, without altering the command ring layout. 52 * 53 * SECURITY: 54 * The user process must be assumed to be malicious. There's no way to 55 * prevent it breaking the command ring protocol if it wants, but in 56 * order to prevent other issues we must only ever read *data* from 57 * the shared memory area, not offsets or sizes. This applies to 58 * command ring entries as well as the mailbox. Extra code needed for 59 * this may have a 'UAM' comment. 60 */ 61 62 63 #define TCMU_TIME_OUT (30 * MSEC_PER_SEC) 64 65 #define CMDR_SIZE (16 * 4096) 66 #define DATA_SIZE (257 * 4096) 67 68 #define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE) 69 70 static struct device *tcmu_root_device; 71 72 struct tcmu_hba { 73 u32 host_id; 74 }; 75 76 #define TCMU_CONFIG_LEN 256 77 78 struct tcmu_dev { 79 struct se_device se_dev; 80 81 char *name; 82 struct se_hba *hba; 83 84 #define TCMU_DEV_BIT_OPEN 0 85 #define TCMU_DEV_BIT_BROKEN 1 86 unsigned long flags; 87 88 struct uio_info uio_info; 89 90 struct tcmu_mailbox *mb_addr; 91 size_t dev_size; 92 u32 cmdr_size; 93 u32 cmdr_last_cleaned; 94 /* Offset of data ring from start of mb */ 95 size_t data_off; 96 size_t data_size; 97 /* Ring head + tail values. */ 98 /* Must add data_off and mb_addr to get the address */ 99 size_t data_head; 100 size_t data_tail; 101 102 wait_queue_head_t wait_cmdr; 103 /* TODO should this be a mutex? */ 104 spinlock_t cmdr_lock; 105 106 struct idr commands; 107 spinlock_t commands_lock; 108 109 struct timer_list timeout; 110 111 char dev_config[TCMU_CONFIG_LEN]; 112 }; 113 114 #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev) 115 116 #define CMDR_OFF sizeof(struct tcmu_mailbox) 117 118 struct tcmu_cmd { 119 struct se_cmd *se_cmd; 120 struct tcmu_dev *tcmu_dev; 121 122 uint16_t cmd_id; 123 124 /* Can't use se_cmd->data_length when cleaning up expired cmds, because if 125 cmd has been completed then accessing se_cmd is off limits */ 126 size_t data_length; 127 128 unsigned long deadline; 129 130 #define TCMU_CMD_BIT_EXPIRED 0 131 unsigned long flags; 132 }; 133 134 static struct kmem_cache *tcmu_cmd_cache; 135 136 /* multicast group */ 137 enum tcmu_multicast_groups { 138 TCMU_MCGRP_CONFIG, 139 }; 140 141 static const struct genl_multicast_group tcmu_mcgrps[] = { 142 [TCMU_MCGRP_CONFIG] = { .name = "config", }, 143 }; 144 145 /* Our generic netlink family */ 146 static struct genl_family tcmu_genl_family = { 147 .id = GENL_ID_GENERATE, 148 .hdrsize = 0, 149 .name = "TCM-USER", 150 .version = 1, 151 .maxattr = TCMU_ATTR_MAX, 152 .mcgrps = tcmu_mcgrps, 153 .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps), 154 }; 155 156 static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd) 157 { 158 struct se_device *se_dev = se_cmd->se_dev; 159 struct tcmu_dev *udev = TCMU_DEV(se_dev); 160 struct tcmu_cmd *tcmu_cmd; 161 int cmd_id; 162 163 tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_KERNEL); 164 if (!tcmu_cmd) 165 return NULL; 166 167 tcmu_cmd->se_cmd = se_cmd; 168 tcmu_cmd->tcmu_dev = udev; 169 tcmu_cmd->data_length = se_cmd->data_length; 170 171 tcmu_cmd->deadline = jiffies + msecs_to_jiffies(TCMU_TIME_OUT); 172 173 idr_preload(GFP_KERNEL); 174 spin_lock_irq(&udev->commands_lock); 175 cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 0, 176 USHRT_MAX, GFP_NOWAIT); 177 spin_unlock_irq(&udev->commands_lock); 178 idr_preload_end(); 179 180 if (cmd_id < 0) { 181 kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); 182 return NULL; 183 } 184 tcmu_cmd->cmd_id = cmd_id; 185 186 return tcmu_cmd; 187 } 188 189 static inline void tcmu_flush_dcache_range(void *vaddr, size_t size) 190 { 191 unsigned long offset = (unsigned long) vaddr & ~PAGE_MASK; 192 193 size = round_up(size+offset, PAGE_SIZE); 194 vaddr -= offset; 195 196 while (size) { 197 flush_dcache_page(virt_to_page(vaddr)); 198 size -= PAGE_SIZE; 199 } 200 } 201 202 /* 203 * Some ring helper functions. We don't assume size is a power of 2 so 204 * we can't use circ_buf.h. 205 */ 206 static inline size_t spc_used(size_t head, size_t tail, size_t size) 207 { 208 int diff = head - tail; 209 210 if (diff >= 0) 211 return diff; 212 else 213 return size + diff; 214 } 215 216 static inline size_t spc_free(size_t head, size_t tail, size_t size) 217 { 218 /* Keep 1 byte unused or we can't tell full from empty */ 219 return (size - spc_used(head, tail, size) - 1); 220 } 221 222 static inline size_t head_to_end(size_t head, size_t size) 223 { 224 return size - head; 225 } 226 227 #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size) 228 229 /* 230 * We can't queue a command until we have space available on the cmd ring *and* space 231 * space avail on the data ring. 232 * 233 * Called with ring lock held. 234 */ 235 static bool is_ring_space_avail(struct tcmu_dev *udev, size_t cmd_size, size_t data_needed) 236 { 237 struct tcmu_mailbox *mb = udev->mb_addr; 238 size_t space; 239 u32 cmd_head; 240 size_t cmd_needed; 241 242 tcmu_flush_dcache_range(mb, sizeof(*mb)); 243 244 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 245 246 /* 247 * If cmd end-of-ring space is too small then we need space for a NOP plus 248 * original cmd - cmds are internally contiguous. 249 */ 250 if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size) 251 cmd_needed = cmd_size; 252 else 253 cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size); 254 255 space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size); 256 if (space < cmd_needed) { 257 pr_debug("no cmd space: %u %u %u\n", cmd_head, 258 udev->cmdr_last_cleaned, udev->cmdr_size); 259 return false; 260 } 261 262 space = spc_free(udev->data_head, udev->data_tail, udev->data_size); 263 if (space < data_needed) { 264 pr_debug("no data space: %zu %zu %zu\n", udev->data_head, 265 udev->data_tail, udev->data_size); 266 return false; 267 } 268 269 return true; 270 } 271 272 static int tcmu_queue_cmd_ring(struct tcmu_cmd *tcmu_cmd) 273 { 274 struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 275 struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 276 size_t base_command_size, command_size; 277 struct tcmu_mailbox *mb; 278 struct tcmu_cmd_entry *entry; 279 int i; 280 struct scatterlist *sg; 281 struct iovec *iov; 282 int iov_cnt = 0; 283 uint32_t cmd_head; 284 uint64_t cdb_off; 285 286 if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) 287 return -EINVAL; 288 289 /* 290 * Must be a certain minimum size for response sense info, but 291 * also may be larger if the iov array is large. 292 * 293 * iovs = sgl_nents+1, for end-of-ring case, plus another 1 294 * b/c size == offsetof one-past-element. 295 */ 296 base_command_size = max(offsetof(struct tcmu_cmd_entry, 297 req.iov[se_cmd->t_data_nents + 2]), 298 sizeof(struct tcmu_cmd_entry)); 299 command_size = base_command_size 300 + round_up(scsi_command_size(se_cmd->t_task_cdb), TCMU_OP_ALIGN_SIZE); 301 302 WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1)); 303 304 spin_lock_irq(&udev->cmdr_lock); 305 306 mb = udev->mb_addr; 307 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 308 if ((command_size > (udev->cmdr_size / 2)) 309 || tcmu_cmd->data_length > (udev->data_size - 1)) 310 pr_warn("TCMU: Request of size %zu/%zu may be too big for %u/%zu " 311 "cmd/data ring buffers\n", command_size, tcmu_cmd->data_length, 312 udev->cmdr_size, udev->data_size); 313 314 while (!is_ring_space_avail(udev, command_size, tcmu_cmd->data_length)) { 315 int ret; 316 DEFINE_WAIT(__wait); 317 318 prepare_to_wait(&udev->wait_cmdr, &__wait, TASK_INTERRUPTIBLE); 319 320 pr_debug("sleeping for ring space\n"); 321 spin_unlock_irq(&udev->cmdr_lock); 322 ret = schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT)); 323 finish_wait(&udev->wait_cmdr, &__wait); 324 if (!ret) { 325 pr_warn("tcmu: command timed out\n"); 326 return -ETIMEDOUT; 327 } 328 329 spin_lock_irq(&udev->cmdr_lock); 330 331 /* We dropped cmdr_lock, cmd_head is stale */ 332 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 333 } 334 335 /* Insert a PAD if end-of-ring space is too small */ 336 if (head_to_end(cmd_head, udev->cmdr_size) < command_size) { 337 size_t pad_size = head_to_end(cmd_head, udev->cmdr_size); 338 339 entry = (void *) mb + CMDR_OFF + cmd_head; 340 tcmu_flush_dcache_range(entry, sizeof(*entry)); 341 tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD); 342 tcmu_hdr_set_len(&entry->hdr.len_op, pad_size); 343 entry->hdr.cmd_id = 0; /* not used for PAD */ 344 entry->hdr.kflags = 0; 345 entry->hdr.uflags = 0; 346 347 UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size); 348 349 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 350 WARN_ON(cmd_head != 0); 351 } 352 353 entry = (void *) mb + CMDR_OFF + cmd_head; 354 tcmu_flush_dcache_range(entry, sizeof(*entry)); 355 tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD); 356 tcmu_hdr_set_len(&entry->hdr.len_op, command_size); 357 entry->hdr.cmd_id = tcmu_cmd->cmd_id; 358 entry->hdr.kflags = 0; 359 entry->hdr.uflags = 0; 360 361 /* 362 * Fix up iovecs, and handle if allocation in data ring wrapped. 363 */ 364 iov = &entry->req.iov[0]; 365 for_each_sg(se_cmd->t_data_sg, sg, se_cmd->t_data_nents, i) { 366 size_t copy_bytes = min((size_t)sg->length, 367 head_to_end(udev->data_head, udev->data_size)); 368 void *from = kmap_atomic(sg_page(sg)) + sg->offset; 369 void *to = (void *) mb + udev->data_off + udev->data_head; 370 371 if (tcmu_cmd->se_cmd->data_direction == DMA_TO_DEVICE) { 372 memcpy(to, from, copy_bytes); 373 tcmu_flush_dcache_range(to, copy_bytes); 374 } 375 376 /* Even iov_base is relative to mb_addr */ 377 iov->iov_len = copy_bytes; 378 iov->iov_base = (void __user *) udev->data_off + 379 udev->data_head; 380 iov_cnt++; 381 iov++; 382 383 UPDATE_HEAD(udev->data_head, copy_bytes, udev->data_size); 384 385 /* Uh oh, we wrapped the buffer. Must split sg across 2 iovs. */ 386 if (sg->length != copy_bytes) { 387 from += copy_bytes; 388 copy_bytes = sg->length - copy_bytes; 389 390 iov->iov_len = copy_bytes; 391 iov->iov_base = (void __user *) udev->data_off + 392 udev->data_head; 393 394 if (se_cmd->data_direction == DMA_TO_DEVICE) { 395 to = (void *) mb + udev->data_off + udev->data_head; 396 memcpy(to, from, copy_bytes); 397 tcmu_flush_dcache_range(to, copy_bytes); 398 } 399 400 iov_cnt++; 401 iov++; 402 403 UPDATE_HEAD(udev->data_head, copy_bytes, udev->data_size); 404 } 405 406 kunmap_atomic(from); 407 } 408 entry->req.iov_cnt = iov_cnt; 409 entry->req.iov_bidi_cnt = 0; 410 entry->req.iov_dif_cnt = 0; 411 412 /* All offsets relative to mb_addr, not start of entry! */ 413 cdb_off = CMDR_OFF + cmd_head + base_command_size; 414 memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb)); 415 entry->req.cdb_off = cdb_off; 416 tcmu_flush_dcache_range(entry, sizeof(*entry)); 417 418 UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size); 419 tcmu_flush_dcache_range(mb, sizeof(*mb)); 420 421 spin_unlock_irq(&udev->cmdr_lock); 422 423 /* TODO: only if FLUSH and FUA? */ 424 uio_event_notify(&udev->uio_info); 425 426 mod_timer(&udev->timeout, 427 round_jiffies_up(jiffies + msecs_to_jiffies(TCMU_TIME_OUT))); 428 429 return 0; 430 } 431 432 static int tcmu_queue_cmd(struct se_cmd *se_cmd) 433 { 434 struct se_device *se_dev = se_cmd->se_dev; 435 struct tcmu_dev *udev = TCMU_DEV(se_dev); 436 struct tcmu_cmd *tcmu_cmd; 437 int ret; 438 439 tcmu_cmd = tcmu_alloc_cmd(se_cmd); 440 if (!tcmu_cmd) 441 return -ENOMEM; 442 443 ret = tcmu_queue_cmd_ring(tcmu_cmd); 444 if (ret < 0) { 445 pr_err("TCMU: Could not queue command\n"); 446 spin_lock_irq(&udev->commands_lock); 447 idr_remove(&udev->commands, tcmu_cmd->cmd_id); 448 spin_unlock_irq(&udev->commands_lock); 449 450 kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); 451 } 452 453 return ret; 454 } 455 456 static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry) 457 { 458 struct se_cmd *se_cmd = cmd->se_cmd; 459 struct tcmu_dev *udev = cmd->tcmu_dev; 460 461 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { 462 /* cmd has been completed already from timeout, just reclaim data 463 ring space */ 464 UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size); 465 return; 466 } 467 468 if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) { 469 UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size); 470 pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n", 471 cmd->se_cmd); 472 transport_generic_request_failure(cmd->se_cmd, 473 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE); 474 cmd->se_cmd = NULL; 475 kmem_cache_free(tcmu_cmd_cache, cmd); 476 return; 477 } 478 479 if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) { 480 memcpy(se_cmd->sense_buffer, entry->rsp.sense_buffer, 481 se_cmd->scsi_sense_length); 482 483 UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size); 484 } 485 else if (se_cmd->data_direction == DMA_FROM_DEVICE) { 486 struct scatterlist *sg; 487 int i; 488 489 /* It'd be easier to look at entry's iovec again, but UAM */ 490 for_each_sg(se_cmd->t_data_sg, sg, se_cmd->t_data_nents, i) { 491 size_t copy_bytes; 492 void *to; 493 void *from; 494 495 copy_bytes = min((size_t)sg->length, 496 head_to_end(udev->data_tail, udev->data_size)); 497 498 to = kmap_atomic(sg_page(sg)) + sg->offset; 499 WARN_ON(sg->length + sg->offset > PAGE_SIZE); 500 from = (void *) udev->mb_addr + udev->data_off + udev->data_tail; 501 tcmu_flush_dcache_range(from, copy_bytes); 502 memcpy(to, from, copy_bytes); 503 504 UPDATE_HEAD(udev->data_tail, copy_bytes, udev->data_size); 505 506 /* Uh oh, wrapped the data buffer for this sg's data */ 507 if (sg->length != copy_bytes) { 508 from = (void *) udev->mb_addr + udev->data_off + udev->data_tail; 509 WARN_ON(udev->data_tail); 510 to += copy_bytes; 511 copy_bytes = sg->length - copy_bytes; 512 tcmu_flush_dcache_range(from, copy_bytes); 513 memcpy(to, from, copy_bytes); 514 515 UPDATE_HEAD(udev->data_tail, copy_bytes, udev->data_size); 516 } 517 518 kunmap_atomic(to); 519 } 520 521 } else if (se_cmd->data_direction == DMA_TO_DEVICE) { 522 UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size); 523 } else { 524 pr_warn("TCMU: data direction was %d!\n", se_cmd->data_direction); 525 } 526 527 target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status); 528 cmd->se_cmd = NULL; 529 530 kmem_cache_free(tcmu_cmd_cache, cmd); 531 } 532 533 static unsigned int tcmu_handle_completions(struct tcmu_dev *udev) 534 { 535 struct tcmu_mailbox *mb; 536 LIST_HEAD(cpl_cmds); 537 unsigned long flags; 538 int handled = 0; 539 540 if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) { 541 pr_err("ring broken, not handling completions\n"); 542 return 0; 543 } 544 545 spin_lock_irqsave(&udev->cmdr_lock, flags); 546 547 mb = udev->mb_addr; 548 tcmu_flush_dcache_range(mb, sizeof(*mb)); 549 550 while (udev->cmdr_last_cleaned != ACCESS_ONCE(mb->cmd_tail)) { 551 552 struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned; 553 struct tcmu_cmd *cmd; 554 555 tcmu_flush_dcache_range(entry, sizeof(*entry)); 556 557 if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) { 558 UPDATE_HEAD(udev->cmdr_last_cleaned, 559 tcmu_hdr_get_len(entry->hdr.len_op), 560 udev->cmdr_size); 561 continue; 562 } 563 WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD); 564 565 spin_lock(&udev->commands_lock); 566 cmd = idr_find(&udev->commands, entry->hdr.cmd_id); 567 if (cmd) 568 idr_remove(&udev->commands, cmd->cmd_id); 569 spin_unlock(&udev->commands_lock); 570 571 if (!cmd) { 572 pr_err("cmd_id not found, ring is broken\n"); 573 set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags); 574 break; 575 } 576 577 tcmu_handle_completion(cmd, entry); 578 579 UPDATE_HEAD(udev->cmdr_last_cleaned, 580 tcmu_hdr_get_len(entry->hdr.len_op), 581 udev->cmdr_size); 582 583 handled++; 584 } 585 586 if (mb->cmd_tail == mb->cmd_head) 587 del_timer(&udev->timeout); /* no more pending cmds */ 588 589 spin_unlock_irqrestore(&udev->cmdr_lock, flags); 590 591 wake_up(&udev->wait_cmdr); 592 593 return handled; 594 } 595 596 static int tcmu_check_expired_cmd(int id, void *p, void *data) 597 { 598 struct tcmu_cmd *cmd = p; 599 600 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) 601 return 0; 602 603 if (!time_after(cmd->deadline, jiffies)) 604 return 0; 605 606 set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags); 607 target_complete_cmd(cmd->se_cmd, SAM_STAT_CHECK_CONDITION); 608 cmd->se_cmd = NULL; 609 610 kmem_cache_free(tcmu_cmd_cache, cmd); 611 612 return 0; 613 } 614 615 static void tcmu_device_timedout(unsigned long data) 616 { 617 struct tcmu_dev *udev = (struct tcmu_dev *)data; 618 unsigned long flags; 619 int handled; 620 621 handled = tcmu_handle_completions(udev); 622 623 pr_warn("%d completions handled from timeout\n", handled); 624 625 spin_lock_irqsave(&udev->commands_lock, flags); 626 idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL); 627 spin_unlock_irqrestore(&udev->commands_lock, flags); 628 629 /* 630 * We don't need to wakeup threads on wait_cmdr since they have their 631 * own timeout. 632 */ 633 } 634 635 static int tcmu_attach_hba(struct se_hba *hba, u32 host_id) 636 { 637 struct tcmu_hba *tcmu_hba; 638 639 tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL); 640 if (!tcmu_hba) 641 return -ENOMEM; 642 643 tcmu_hba->host_id = host_id; 644 hba->hba_ptr = tcmu_hba; 645 646 return 0; 647 } 648 649 static void tcmu_detach_hba(struct se_hba *hba) 650 { 651 kfree(hba->hba_ptr); 652 hba->hba_ptr = NULL; 653 } 654 655 static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name) 656 { 657 struct tcmu_dev *udev; 658 659 udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL); 660 if (!udev) 661 return NULL; 662 663 udev->name = kstrdup(name, GFP_KERNEL); 664 if (!udev->name) { 665 kfree(udev); 666 return NULL; 667 } 668 669 udev->hba = hba; 670 671 init_waitqueue_head(&udev->wait_cmdr); 672 spin_lock_init(&udev->cmdr_lock); 673 674 idr_init(&udev->commands); 675 spin_lock_init(&udev->commands_lock); 676 677 setup_timer(&udev->timeout, tcmu_device_timedout, 678 (unsigned long)udev); 679 680 return &udev->se_dev; 681 } 682 683 static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on) 684 { 685 struct tcmu_dev *tcmu_dev = container_of(info, struct tcmu_dev, uio_info); 686 687 tcmu_handle_completions(tcmu_dev); 688 689 return 0; 690 } 691 692 /* 693 * mmap code from uio.c. Copied here because we want to hook mmap() 694 * and this stuff must come along. 695 */ 696 static int tcmu_find_mem_index(struct vm_area_struct *vma) 697 { 698 struct tcmu_dev *udev = vma->vm_private_data; 699 struct uio_info *info = &udev->uio_info; 700 701 if (vma->vm_pgoff < MAX_UIO_MAPS) { 702 if (info->mem[vma->vm_pgoff].size == 0) 703 return -1; 704 return (int)vma->vm_pgoff; 705 } 706 return -1; 707 } 708 709 static int tcmu_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 710 { 711 struct tcmu_dev *udev = vma->vm_private_data; 712 struct uio_info *info = &udev->uio_info; 713 struct page *page; 714 unsigned long offset; 715 void *addr; 716 717 int mi = tcmu_find_mem_index(vma); 718 if (mi < 0) 719 return VM_FAULT_SIGBUS; 720 721 /* 722 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE 723 * to use mem[N]. 724 */ 725 offset = (vmf->pgoff - mi) << PAGE_SHIFT; 726 727 addr = (void *)(unsigned long)info->mem[mi].addr + offset; 728 if (info->mem[mi].memtype == UIO_MEM_LOGICAL) 729 page = virt_to_page(addr); 730 else 731 page = vmalloc_to_page(addr); 732 get_page(page); 733 vmf->page = page; 734 return 0; 735 } 736 737 static const struct vm_operations_struct tcmu_vm_ops = { 738 .fault = tcmu_vma_fault, 739 }; 740 741 static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma) 742 { 743 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 744 745 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 746 vma->vm_ops = &tcmu_vm_ops; 747 748 vma->vm_private_data = udev; 749 750 /* Ensure the mmap is exactly the right size */ 751 if (vma_pages(vma) != (TCMU_RING_SIZE >> PAGE_SHIFT)) 752 return -EINVAL; 753 754 return 0; 755 } 756 757 static int tcmu_open(struct uio_info *info, struct inode *inode) 758 { 759 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 760 761 /* O_EXCL not supported for char devs, so fake it? */ 762 if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags)) 763 return -EBUSY; 764 765 pr_debug("open\n"); 766 767 return 0; 768 } 769 770 static int tcmu_release(struct uio_info *info, struct inode *inode) 771 { 772 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 773 774 clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags); 775 776 pr_debug("close\n"); 777 778 return 0; 779 } 780 781 static int tcmu_netlink_event(enum tcmu_genl_cmd cmd, const char *name, int minor) 782 { 783 struct sk_buff *skb; 784 void *msg_header; 785 int ret = -ENOMEM; 786 787 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 788 if (!skb) 789 return ret; 790 791 msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd); 792 if (!msg_header) 793 goto free_skb; 794 795 ret = nla_put_string(skb, TCMU_ATTR_DEVICE, name); 796 if (ret < 0) 797 goto free_skb; 798 799 ret = nla_put_u32(skb, TCMU_ATTR_MINOR, minor); 800 if (ret < 0) 801 goto free_skb; 802 803 genlmsg_end(skb, msg_header); 804 805 ret = genlmsg_multicast(&tcmu_genl_family, skb, 0, 806 TCMU_MCGRP_CONFIG, GFP_KERNEL); 807 808 /* We don't care if no one is listening */ 809 if (ret == -ESRCH) 810 ret = 0; 811 812 return ret; 813 free_skb: 814 nlmsg_free(skb); 815 return ret; 816 } 817 818 static int tcmu_configure_device(struct se_device *dev) 819 { 820 struct tcmu_dev *udev = TCMU_DEV(dev); 821 struct tcmu_hba *hba = udev->hba->hba_ptr; 822 struct uio_info *info; 823 struct tcmu_mailbox *mb; 824 size_t size; 825 size_t used; 826 int ret = 0; 827 char *str; 828 829 info = &udev->uio_info; 830 831 size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name, 832 udev->dev_config); 833 size += 1; /* for \0 */ 834 str = kmalloc(size, GFP_KERNEL); 835 if (!str) 836 return -ENOMEM; 837 838 used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name); 839 840 if (udev->dev_config[0]) 841 snprintf(str + used, size - used, "/%s", udev->dev_config); 842 843 info->name = str; 844 845 udev->mb_addr = vzalloc(TCMU_RING_SIZE); 846 if (!udev->mb_addr) { 847 ret = -ENOMEM; 848 goto err_vzalloc; 849 } 850 851 /* mailbox fits in first part of CMDR space */ 852 udev->cmdr_size = CMDR_SIZE - CMDR_OFF; 853 udev->data_off = CMDR_SIZE; 854 udev->data_size = TCMU_RING_SIZE - CMDR_SIZE; 855 856 mb = udev->mb_addr; 857 mb->version = TCMU_MAILBOX_VERSION; 858 mb->cmdr_off = CMDR_OFF; 859 mb->cmdr_size = udev->cmdr_size; 860 861 WARN_ON(!PAGE_ALIGNED(udev->data_off)); 862 WARN_ON(udev->data_size % PAGE_SIZE); 863 864 info->version = xstr(TCMU_MAILBOX_VERSION); 865 866 info->mem[0].name = "tcm-user command & data buffer"; 867 info->mem[0].addr = (phys_addr_t) udev->mb_addr; 868 info->mem[0].size = TCMU_RING_SIZE; 869 info->mem[0].memtype = UIO_MEM_VIRTUAL; 870 871 info->irqcontrol = tcmu_irqcontrol; 872 info->irq = UIO_IRQ_CUSTOM; 873 874 info->mmap = tcmu_mmap; 875 info->open = tcmu_open; 876 info->release = tcmu_release; 877 878 ret = uio_register_device(tcmu_root_device, info); 879 if (ret) 880 goto err_register; 881 882 /* Other attributes can be configured in userspace */ 883 dev->dev_attrib.hw_block_size = 512; 884 dev->dev_attrib.hw_max_sectors = 128; 885 dev->dev_attrib.hw_queue_depth = 128; 886 887 ret = tcmu_netlink_event(TCMU_CMD_ADDED_DEVICE, udev->uio_info.name, 888 udev->uio_info.uio_dev->minor); 889 if (ret) 890 goto err_netlink; 891 892 return 0; 893 894 err_netlink: 895 uio_unregister_device(&udev->uio_info); 896 err_register: 897 vfree(udev->mb_addr); 898 err_vzalloc: 899 kfree(info->name); 900 901 return ret; 902 } 903 904 static int tcmu_check_pending_cmd(int id, void *p, void *data) 905 { 906 struct tcmu_cmd *cmd = p; 907 908 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) 909 return 0; 910 return -EINVAL; 911 } 912 913 static void tcmu_free_device(struct se_device *dev) 914 { 915 struct tcmu_dev *udev = TCMU_DEV(dev); 916 int i; 917 918 del_timer_sync(&udev->timeout); 919 920 vfree(udev->mb_addr); 921 922 /* Upper layer should drain all requests before calling this */ 923 spin_lock_irq(&udev->commands_lock); 924 i = idr_for_each(&udev->commands, tcmu_check_pending_cmd, NULL); 925 idr_destroy(&udev->commands); 926 spin_unlock_irq(&udev->commands_lock); 927 WARN_ON(i); 928 929 /* Device was configured */ 930 if (udev->uio_info.uio_dev) { 931 tcmu_netlink_event(TCMU_CMD_REMOVED_DEVICE, udev->uio_info.name, 932 udev->uio_info.uio_dev->minor); 933 934 uio_unregister_device(&udev->uio_info); 935 kfree(udev->uio_info.name); 936 kfree(udev->name); 937 } 938 939 kfree(udev); 940 } 941 942 enum { 943 Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_err, 944 }; 945 946 static match_table_t tokens = { 947 {Opt_dev_config, "dev_config=%s"}, 948 {Opt_dev_size, "dev_size=%u"}, 949 {Opt_hw_block_size, "hw_block_size=%u"}, 950 {Opt_err, NULL} 951 }; 952 953 static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev, 954 const char *page, ssize_t count) 955 { 956 struct tcmu_dev *udev = TCMU_DEV(dev); 957 char *orig, *ptr, *opts, *arg_p; 958 substring_t args[MAX_OPT_ARGS]; 959 int ret = 0, token; 960 unsigned long tmp_ul; 961 962 opts = kstrdup(page, GFP_KERNEL); 963 if (!opts) 964 return -ENOMEM; 965 966 orig = opts; 967 968 while ((ptr = strsep(&opts, ",\n")) != NULL) { 969 if (!*ptr) 970 continue; 971 972 token = match_token(ptr, tokens, args); 973 switch (token) { 974 case Opt_dev_config: 975 if (match_strlcpy(udev->dev_config, &args[0], 976 TCMU_CONFIG_LEN) == 0) { 977 ret = -EINVAL; 978 break; 979 } 980 pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config); 981 break; 982 case Opt_dev_size: 983 arg_p = match_strdup(&args[0]); 984 if (!arg_p) { 985 ret = -ENOMEM; 986 break; 987 } 988 ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size); 989 kfree(arg_p); 990 if (ret < 0) 991 pr_err("kstrtoul() failed for dev_size=\n"); 992 break; 993 case Opt_hw_block_size: 994 arg_p = match_strdup(&args[0]); 995 if (!arg_p) { 996 ret = -ENOMEM; 997 break; 998 } 999 ret = kstrtoul(arg_p, 0, &tmp_ul); 1000 kfree(arg_p); 1001 if (ret < 0) { 1002 pr_err("kstrtoul() failed for hw_block_size=\n"); 1003 break; 1004 } 1005 if (!tmp_ul) { 1006 pr_err("hw_block_size must be nonzero\n"); 1007 break; 1008 } 1009 dev->dev_attrib.hw_block_size = tmp_ul; 1010 break; 1011 default: 1012 break; 1013 } 1014 } 1015 1016 kfree(orig); 1017 return (!ret) ? count : ret; 1018 } 1019 1020 static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b) 1021 { 1022 struct tcmu_dev *udev = TCMU_DEV(dev); 1023 ssize_t bl = 0; 1024 1025 bl = sprintf(b + bl, "Config: %s ", 1026 udev->dev_config[0] ? udev->dev_config : "NULL"); 1027 bl += sprintf(b + bl, "Size: %zu\n", udev->dev_size); 1028 1029 return bl; 1030 } 1031 1032 static sector_t tcmu_get_blocks(struct se_device *dev) 1033 { 1034 struct tcmu_dev *udev = TCMU_DEV(dev); 1035 1036 return div_u64(udev->dev_size - dev->dev_attrib.block_size, 1037 dev->dev_attrib.block_size); 1038 } 1039 1040 static sense_reason_t 1041 tcmu_pass_op(struct se_cmd *se_cmd) 1042 { 1043 int ret = tcmu_queue_cmd(se_cmd); 1044 1045 if (ret != 0) 1046 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1047 else 1048 return TCM_NO_SENSE; 1049 } 1050 1051 static sense_reason_t 1052 tcmu_parse_cdb(struct se_cmd *cmd) 1053 { 1054 return passthrough_parse_cdb(cmd, tcmu_pass_op); 1055 } 1056 1057 DEF_TB_DEV_ATTRIB_RO(tcmu, hw_pi_prot_type); 1058 TB_DEV_ATTR_RO(tcmu, hw_pi_prot_type); 1059 1060 DEF_TB_DEV_ATTRIB_RO(tcmu, hw_block_size); 1061 TB_DEV_ATTR_RO(tcmu, hw_block_size); 1062 1063 DEF_TB_DEV_ATTRIB_RO(tcmu, hw_max_sectors); 1064 TB_DEV_ATTR_RO(tcmu, hw_max_sectors); 1065 1066 DEF_TB_DEV_ATTRIB_RO(tcmu, hw_queue_depth); 1067 TB_DEV_ATTR_RO(tcmu, hw_queue_depth); 1068 1069 static struct configfs_attribute *tcmu_backend_dev_attrs[] = { 1070 &tcmu_dev_attrib_hw_pi_prot_type.attr, 1071 &tcmu_dev_attrib_hw_block_size.attr, 1072 &tcmu_dev_attrib_hw_max_sectors.attr, 1073 &tcmu_dev_attrib_hw_queue_depth.attr, 1074 NULL, 1075 }; 1076 1077 static struct se_subsystem_api tcmu_template = { 1078 .name = "user", 1079 .inquiry_prod = "USER", 1080 .inquiry_rev = TCMU_VERSION, 1081 .owner = THIS_MODULE, 1082 .transport_flags = TRANSPORT_FLAG_PASSTHROUGH, 1083 .attach_hba = tcmu_attach_hba, 1084 .detach_hba = tcmu_detach_hba, 1085 .alloc_device = tcmu_alloc_device, 1086 .configure_device = tcmu_configure_device, 1087 .free_device = tcmu_free_device, 1088 .parse_cdb = tcmu_parse_cdb, 1089 .set_configfs_dev_params = tcmu_set_configfs_dev_params, 1090 .show_configfs_dev_params = tcmu_show_configfs_dev_params, 1091 .get_device_type = sbc_get_device_type, 1092 .get_blocks = tcmu_get_blocks, 1093 }; 1094 1095 static int __init tcmu_module_init(void) 1096 { 1097 struct target_backend_cits *tbc = &tcmu_template.tb_cits; 1098 int ret; 1099 1100 BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0); 1101 1102 tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache", 1103 sizeof(struct tcmu_cmd), 1104 __alignof__(struct tcmu_cmd), 1105 0, NULL); 1106 if (!tcmu_cmd_cache) 1107 return -ENOMEM; 1108 1109 tcmu_root_device = root_device_register("tcm_user"); 1110 if (IS_ERR(tcmu_root_device)) { 1111 ret = PTR_ERR(tcmu_root_device); 1112 goto out_free_cache; 1113 } 1114 1115 ret = genl_register_family(&tcmu_genl_family); 1116 if (ret < 0) { 1117 goto out_unreg_device; 1118 } 1119 1120 target_core_setup_sub_cits(&tcmu_template); 1121 tbc->tb_dev_attrib_cit.ct_attrs = tcmu_backend_dev_attrs; 1122 1123 ret = transport_subsystem_register(&tcmu_template); 1124 if (ret) 1125 goto out_unreg_genl; 1126 1127 return 0; 1128 1129 out_unreg_genl: 1130 genl_unregister_family(&tcmu_genl_family); 1131 out_unreg_device: 1132 root_device_unregister(tcmu_root_device); 1133 out_free_cache: 1134 kmem_cache_destroy(tcmu_cmd_cache); 1135 1136 return ret; 1137 } 1138 1139 static void __exit tcmu_module_exit(void) 1140 { 1141 transport_subsystem_release(&tcmu_template); 1142 genl_unregister_family(&tcmu_genl_family); 1143 root_device_unregister(tcmu_root_device); 1144 kmem_cache_destroy(tcmu_cmd_cache); 1145 } 1146 1147 MODULE_DESCRIPTION("TCM USER subsystem plugin"); 1148 MODULE_AUTHOR("Shaohua Li <shli@kernel.org>"); 1149 MODULE_AUTHOR("Andy Grover <agrover@redhat.com>"); 1150 MODULE_LICENSE("GPL"); 1151 1152 module_init(tcmu_module_init); 1153 module_exit(tcmu_module_exit); 1154