1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2013 Shaohua Li <shli@kernel.org> 4 * Copyright (C) 2014 Red Hat, Inc. 5 * Copyright (C) 2015 Arrikto, Inc. 6 * Copyright (C) 2017 Chinamobile, Inc. 7 */ 8 9 #include <linux/spinlock.h> 10 #include <linux/module.h> 11 #include <linux/idr.h> 12 #include <linux/kernel.h> 13 #include <linux/timer.h> 14 #include <linux/parser.h> 15 #include <linux/vmalloc.h> 16 #include <linux/uio_driver.h> 17 #include <linux/radix-tree.h> 18 #include <linux/stringify.h> 19 #include <linux/bitops.h> 20 #include <linux/highmem.h> 21 #include <linux/configfs.h> 22 #include <linux/mutex.h> 23 #include <linux/workqueue.h> 24 #include <net/genetlink.h> 25 #include <scsi/scsi_common.h> 26 #include <scsi/scsi_proto.h> 27 #include <target/target_core_base.h> 28 #include <target/target_core_fabric.h> 29 #include <target/target_core_backend.h> 30 31 #include <linux/target_core_user.h> 32 33 /** 34 * DOC: Userspace I/O 35 * Userspace I/O 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 #define TCMU_TIME_OUT (30 * MSEC_PER_SEC) 63 64 /* For cmd area, the size is fixed 8MB */ 65 #define CMDR_SIZE (8 * 1024 * 1024) 66 67 /* 68 * For data area, the block size is PAGE_SIZE and 69 * the total size is 256K * PAGE_SIZE. 70 */ 71 #define DATA_BLOCK_SIZE PAGE_SIZE 72 #define DATA_BLOCK_SHIFT PAGE_SHIFT 73 #define DATA_BLOCK_BITS_DEF (256 * 1024) 74 75 #define TCMU_MBS_TO_BLOCKS(_mbs) (_mbs << (20 - DATA_BLOCK_SHIFT)) 76 #define TCMU_BLOCKS_TO_MBS(_blocks) (_blocks >> (20 - DATA_BLOCK_SHIFT)) 77 78 /* 79 * Default number of global data blocks(512K * PAGE_SIZE) 80 * when the unmap thread will be started. 81 */ 82 #define TCMU_GLOBAL_MAX_BLOCKS_DEF (512 * 1024) 83 84 static u8 tcmu_kern_cmd_reply_supported; 85 static u8 tcmu_netlink_blocked; 86 87 static struct device *tcmu_root_device; 88 89 struct tcmu_hba { 90 u32 host_id; 91 }; 92 93 #define TCMU_CONFIG_LEN 256 94 95 static DEFINE_MUTEX(tcmu_nl_cmd_mutex); 96 static LIST_HEAD(tcmu_nl_cmd_list); 97 98 struct tcmu_dev; 99 100 struct tcmu_nl_cmd { 101 /* wake up thread waiting for reply */ 102 struct completion complete; 103 struct list_head nl_list; 104 struct tcmu_dev *udev; 105 int cmd; 106 int status; 107 }; 108 109 struct tcmu_dev { 110 struct list_head node; 111 struct kref kref; 112 113 struct se_device se_dev; 114 115 char *name; 116 struct se_hba *hba; 117 118 #define TCMU_DEV_BIT_OPEN 0 119 #define TCMU_DEV_BIT_BROKEN 1 120 #define TCMU_DEV_BIT_BLOCKED 2 121 #define TCMU_DEV_BIT_TMR_NOTIFY 3 122 unsigned long flags; 123 124 struct uio_info uio_info; 125 126 struct inode *inode; 127 128 struct tcmu_mailbox *mb_addr; 129 uint64_t dev_size; 130 u32 cmdr_size; 131 u32 cmdr_last_cleaned; 132 /* Offset of data area from start of mb */ 133 /* Must add data_off and mb_addr to get the address */ 134 size_t data_off; 135 size_t data_size; 136 uint32_t max_blocks; 137 size_t ring_size; 138 139 struct mutex cmdr_lock; 140 struct list_head qfull_queue; 141 struct list_head tmr_queue; 142 143 uint32_t dbi_max; 144 uint32_t dbi_thresh; 145 unsigned long *data_bitmap; 146 struct radix_tree_root data_blocks; 147 148 struct idr commands; 149 150 struct timer_list cmd_timer; 151 unsigned int cmd_time_out; 152 struct list_head inflight_queue; 153 154 struct timer_list qfull_timer; 155 int qfull_time_out; 156 157 struct list_head timedout_entry; 158 159 struct tcmu_nl_cmd curr_nl_cmd; 160 161 char dev_config[TCMU_CONFIG_LEN]; 162 163 int nl_reply_supported; 164 }; 165 166 #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev) 167 168 #define CMDR_OFF sizeof(struct tcmu_mailbox) 169 170 struct tcmu_cmd { 171 struct se_cmd *se_cmd; 172 struct tcmu_dev *tcmu_dev; 173 struct list_head queue_entry; 174 175 uint16_t cmd_id; 176 177 /* Can't use se_cmd when cleaning up expired cmds, because if 178 cmd has been completed then accessing se_cmd is off limits */ 179 uint32_t dbi_cnt; 180 uint32_t dbi_bidi_cnt; 181 uint32_t dbi_cur; 182 uint32_t *dbi; 183 184 uint32_t data_len_bidi; 185 186 unsigned long deadline; 187 188 #define TCMU_CMD_BIT_EXPIRED 0 189 unsigned long flags; 190 }; 191 192 struct tcmu_tmr { 193 struct list_head queue_entry; 194 195 uint8_t tmr_type; 196 uint32_t tmr_cmd_cnt; 197 int16_t tmr_cmd_ids[]; 198 }; 199 200 /* 201 * To avoid dead lock the mutex lock order should always be: 202 * 203 * mutex_lock(&root_udev_mutex); 204 * ... 205 * mutex_lock(&tcmu_dev->cmdr_lock); 206 * mutex_unlock(&tcmu_dev->cmdr_lock); 207 * ... 208 * mutex_unlock(&root_udev_mutex); 209 */ 210 static DEFINE_MUTEX(root_udev_mutex); 211 static LIST_HEAD(root_udev); 212 213 static DEFINE_SPINLOCK(timed_out_udevs_lock); 214 static LIST_HEAD(timed_out_udevs); 215 216 static struct kmem_cache *tcmu_cmd_cache; 217 218 static atomic_t global_db_count = ATOMIC_INIT(0); 219 static struct delayed_work tcmu_unmap_work; 220 static int tcmu_global_max_blocks = TCMU_GLOBAL_MAX_BLOCKS_DEF; 221 222 static int tcmu_set_global_max_data_area(const char *str, 223 const struct kernel_param *kp) 224 { 225 int ret, max_area_mb; 226 227 ret = kstrtoint(str, 10, &max_area_mb); 228 if (ret) 229 return -EINVAL; 230 231 if (max_area_mb <= 0) { 232 pr_err("global_max_data_area must be larger than 0.\n"); 233 return -EINVAL; 234 } 235 236 tcmu_global_max_blocks = TCMU_MBS_TO_BLOCKS(max_area_mb); 237 if (atomic_read(&global_db_count) > tcmu_global_max_blocks) 238 schedule_delayed_work(&tcmu_unmap_work, 0); 239 else 240 cancel_delayed_work_sync(&tcmu_unmap_work); 241 242 return 0; 243 } 244 245 static int tcmu_get_global_max_data_area(char *buffer, 246 const struct kernel_param *kp) 247 { 248 return sprintf(buffer, "%d\n", TCMU_BLOCKS_TO_MBS(tcmu_global_max_blocks)); 249 } 250 251 static const struct kernel_param_ops tcmu_global_max_data_area_op = { 252 .set = tcmu_set_global_max_data_area, 253 .get = tcmu_get_global_max_data_area, 254 }; 255 256 module_param_cb(global_max_data_area_mb, &tcmu_global_max_data_area_op, NULL, 257 S_IWUSR | S_IRUGO); 258 MODULE_PARM_DESC(global_max_data_area_mb, 259 "Max MBs allowed to be allocated to all the tcmu device's " 260 "data areas."); 261 262 static int tcmu_get_block_netlink(char *buffer, 263 const struct kernel_param *kp) 264 { 265 return sprintf(buffer, "%s\n", tcmu_netlink_blocked ? 266 "blocked" : "unblocked"); 267 } 268 269 static int tcmu_set_block_netlink(const char *str, 270 const struct kernel_param *kp) 271 { 272 int ret; 273 u8 val; 274 275 ret = kstrtou8(str, 0, &val); 276 if (ret < 0) 277 return ret; 278 279 if (val > 1) { 280 pr_err("Invalid block netlink value %u\n", val); 281 return -EINVAL; 282 } 283 284 tcmu_netlink_blocked = val; 285 return 0; 286 } 287 288 static const struct kernel_param_ops tcmu_block_netlink_op = { 289 .set = tcmu_set_block_netlink, 290 .get = tcmu_get_block_netlink, 291 }; 292 293 module_param_cb(block_netlink, &tcmu_block_netlink_op, NULL, S_IWUSR | S_IRUGO); 294 MODULE_PARM_DESC(block_netlink, "Block new netlink commands."); 295 296 static int tcmu_fail_netlink_cmd(struct tcmu_nl_cmd *nl_cmd) 297 { 298 struct tcmu_dev *udev = nl_cmd->udev; 299 300 if (!tcmu_netlink_blocked) { 301 pr_err("Could not reset device's netlink interface. Netlink is not blocked.\n"); 302 return -EBUSY; 303 } 304 305 if (nl_cmd->cmd != TCMU_CMD_UNSPEC) { 306 pr_debug("Aborting nl cmd %d on %s\n", nl_cmd->cmd, udev->name); 307 nl_cmd->status = -EINTR; 308 list_del(&nl_cmd->nl_list); 309 complete(&nl_cmd->complete); 310 } 311 return 0; 312 } 313 314 static int tcmu_set_reset_netlink(const char *str, 315 const struct kernel_param *kp) 316 { 317 struct tcmu_nl_cmd *nl_cmd, *tmp_cmd; 318 int ret; 319 u8 val; 320 321 ret = kstrtou8(str, 0, &val); 322 if (ret < 0) 323 return ret; 324 325 if (val != 1) { 326 pr_err("Invalid reset netlink value %u\n", val); 327 return -EINVAL; 328 } 329 330 mutex_lock(&tcmu_nl_cmd_mutex); 331 list_for_each_entry_safe(nl_cmd, tmp_cmd, &tcmu_nl_cmd_list, nl_list) { 332 ret = tcmu_fail_netlink_cmd(nl_cmd); 333 if (ret) 334 break; 335 } 336 mutex_unlock(&tcmu_nl_cmd_mutex); 337 338 return ret; 339 } 340 341 static const struct kernel_param_ops tcmu_reset_netlink_op = { 342 .set = tcmu_set_reset_netlink, 343 }; 344 345 module_param_cb(reset_netlink, &tcmu_reset_netlink_op, NULL, S_IWUSR); 346 MODULE_PARM_DESC(reset_netlink, "Reset netlink commands."); 347 348 /* multicast group */ 349 enum tcmu_multicast_groups { 350 TCMU_MCGRP_CONFIG, 351 }; 352 353 static const struct genl_multicast_group tcmu_mcgrps[] = { 354 [TCMU_MCGRP_CONFIG] = { .name = "config", }, 355 }; 356 357 static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = { 358 [TCMU_ATTR_DEVICE] = { .type = NLA_STRING }, 359 [TCMU_ATTR_MINOR] = { .type = NLA_U32 }, 360 [TCMU_ATTR_CMD_STATUS] = { .type = NLA_S32 }, 361 [TCMU_ATTR_DEVICE_ID] = { .type = NLA_U32 }, 362 [TCMU_ATTR_SUPP_KERN_CMD_REPLY] = { .type = NLA_U8 }, 363 }; 364 365 static int tcmu_genl_cmd_done(struct genl_info *info, int completed_cmd) 366 { 367 struct tcmu_dev *udev = NULL; 368 struct tcmu_nl_cmd *nl_cmd; 369 int dev_id, rc, ret = 0; 370 371 if (!info->attrs[TCMU_ATTR_CMD_STATUS] || 372 !info->attrs[TCMU_ATTR_DEVICE_ID]) { 373 printk(KERN_ERR "TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n"); 374 return -EINVAL; 375 } 376 377 dev_id = nla_get_u32(info->attrs[TCMU_ATTR_DEVICE_ID]); 378 rc = nla_get_s32(info->attrs[TCMU_ATTR_CMD_STATUS]); 379 380 mutex_lock(&tcmu_nl_cmd_mutex); 381 list_for_each_entry(nl_cmd, &tcmu_nl_cmd_list, nl_list) { 382 if (nl_cmd->udev->se_dev.dev_index == dev_id) { 383 udev = nl_cmd->udev; 384 break; 385 } 386 } 387 388 if (!udev) { 389 pr_err("tcmu nl cmd %u/%d completion could not find device with dev id %u.\n", 390 completed_cmd, rc, dev_id); 391 ret = -ENODEV; 392 goto unlock; 393 } 394 list_del(&nl_cmd->nl_list); 395 396 pr_debug("%s genl cmd done got id %d curr %d done %d rc %d stat %d\n", 397 udev->name, dev_id, nl_cmd->cmd, completed_cmd, rc, 398 nl_cmd->status); 399 400 if (nl_cmd->cmd != completed_cmd) { 401 pr_err("Mismatched commands on %s (Expecting reply for %d. Current %d).\n", 402 udev->name, completed_cmd, nl_cmd->cmd); 403 ret = -EINVAL; 404 goto unlock; 405 } 406 407 nl_cmd->status = rc; 408 complete(&nl_cmd->complete); 409 unlock: 410 mutex_unlock(&tcmu_nl_cmd_mutex); 411 return ret; 412 } 413 414 static int tcmu_genl_rm_dev_done(struct sk_buff *skb, struct genl_info *info) 415 { 416 return tcmu_genl_cmd_done(info, TCMU_CMD_REMOVED_DEVICE); 417 } 418 419 static int tcmu_genl_add_dev_done(struct sk_buff *skb, struct genl_info *info) 420 { 421 return tcmu_genl_cmd_done(info, TCMU_CMD_ADDED_DEVICE); 422 } 423 424 static int tcmu_genl_reconfig_dev_done(struct sk_buff *skb, 425 struct genl_info *info) 426 { 427 return tcmu_genl_cmd_done(info, TCMU_CMD_RECONFIG_DEVICE); 428 } 429 430 static int tcmu_genl_set_features(struct sk_buff *skb, struct genl_info *info) 431 { 432 if (info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]) { 433 tcmu_kern_cmd_reply_supported = 434 nla_get_u8(info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]); 435 printk(KERN_INFO "tcmu daemon: command reply support %u.\n", 436 tcmu_kern_cmd_reply_supported); 437 } 438 439 return 0; 440 } 441 442 static const struct genl_small_ops tcmu_genl_ops[] = { 443 { 444 .cmd = TCMU_CMD_SET_FEATURES, 445 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 446 .flags = GENL_ADMIN_PERM, 447 .doit = tcmu_genl_set_features, 448 }, 449 { 450 .cmd = TCMU_CMD_ADDED_DEVICE_DONE, 451 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 452 .flags = GENL_ADMIN_PERM, 453 .doit = tcmu_genl_add_dev_done, 454 }, 455 { 456 .cmd = TCMU_CMD_REMOVED_DEVICE_DONE, 457 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 458 .flags = GENL_ADMIN_PERM, 459 .doit = tcmu_genl_rm_dev_done, 460 }, 461 { 462 .cmd = TCMU_CMD_RECONFIG_DEVICE_DONE, 463 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 464 .flags = GENL_ADMIN_PERM, 465 .doit = tcmu_genl_reconfig_dev_done, 466 }, 467 }; 468 469 /* Our generic netlink family */ 470 static struct genl_family tcmu_genl_family __ro_after_init = { 471 .module = THIS_MODULE, 472 .hdrsize = 0, 473 .name = "TCM-USER", 474 .version = 2, 475 .maxattr = TCMU_ATTR_MAX, 476 .policy = tcmu_attr_policy, 477 .mcgrps = tcmu_mcgrps, 478 .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps), 479 .netnsok = true, 480 .small_ops = tcmu_genl_ops, 481 .n_small_ops = ARRAY_SIZE(tcmu_genl_ops), 482 }; 483 484 #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index)) 485 #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0) 486 #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index)) 487 #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++]) 488 489 static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len) 490 { 491 struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 492 uint32_t i; 493 494 for (i = 0; i < len; i++) 495 clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap); 496 } 497 498 static inline int tcmu_get_empty_block(struct tcmu_dev *udev, 499 struct tcmu_cmd *tcmu_cmd, 500 int prev_dbi, int *iov_cnt) 501 { 502 struct page *page; 503 int ret, dbi; 504 505 dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh); 506 if (dbi == udev->dbi_thresh) 507 return -1; 508 509 page = radix_tree_lookup(&udev->data_blocks, dbi); 510 if (!page) { 511 if (atomic_add_return(1, &global_db_count) > 512 tcmu_global_max_blocks) 513 schedule_delayed_work(&tcmu_unmap_work, 0); 514 515 /* try to get new page from the mm */ 516 page = alloc_page(GFP_NOIO); 517 if (!page) 518 goto err_alloc; 519 520 ret = radix_tree_insert(&udev->data_blocks, dbi, page); 521 if (ret) 522 goto err_insert; 523 } 524 525 if (dbi > udev->dbi_max) 526 udev->dbi_max = dbi; 527 528 set_bit(dbi, udev->data_bitmap); 529 tcmu_cmd_set_dbi(tcmu_cmd, dbi); 530 531 if (dbi != prev_dbi + 1) 532 *iov_cnt += 1; 533 534 return dbi; 535 err_insert: 536 __free_page(page); 537 err_alloc: 538 atomic_dec(&global_db_count); 539 return -1; 540 } 541 542 static int tcmu_get_empty_blocks(struct tcmu_dev *udev, 543 struct tcmu_cmd *tcmu_cmd, int dbi_cnt) 544 { 545 /* start value of dbi + 1 must not be a valid dbi */ 546 int dbi = -2; 547 int i, iov_cnt = 0; 548 549 for (i = 0; i < dbi_cnt; i++) { 550 dbi = tcmu_get_empty_block(udev, tcmu_cmd, dbi, &iov_cnt); 551 if (dbi < 0) 552 return -1; 553 } 554 return iov_cnt; 555 } 556 557 static inline struct page * 558 tcmu_get_block_page(struct tcmu_dev *udev, uint32_t dbi) 559 { 560 return radix_tree_lookup(&udev->data_blocks, dbi); 561 } 562 563 static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd) 564 { 565 kfree(tcmu_cmd->dbi); 566 kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); 567 } 568 569 static inline void tcmu_cmd_set_block_cnts(struct tcmu_cmd *cmd) 570 { 571 int i, len; 572 struct se_cmd *se_cmd = cmd->se_cmd; 573 574 cmd->dbi_cnt = DIV_ROUND_UP(se_cmd->data_length, DATA_BLOCK_SIZE); 575 576 if (se_cmd->se_cmd_flags & SCF_BIDI) { 577 BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents)); 578 for (i = 0, len = 0; i < se_cmd->t_bidi_data_nents; i++) 579 len += se_cmd->t_bidi_data_sg[i].length; 580 cmd->dbi_bidi_cnt = DIV_ROUND_UP(len, DATA_BLOCK_SIZE); 581 cmd->dbi_cnt += cmd->dbi_bidi_cnt; 582 cmd->data_len_bidi = len; 583 } 584 } 585 586 static int new_block_to_iov(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 587 struct iovec **iov, int prev_dbi, int len) 588 { 589 /* Get the next dbi */ 590 int dbi = tcmu_cmd_get_dbi(cmd); 591 592 /* Do not add more than DATA_BLOCK_SIZE to iov */ 593 if (len > DATA_BLOCK_SIZE) 594 len = DATA_BLOCK_SIZE; 595 596 /* 597 * The following code will gather and map the blocks to the same iovec 598 * when the blocks are all next to each other. 599 */ 600 if (dbi != prev_dbi + 1) { 601 /* dbi is not next to previous dbi, so start new iov */ 602 if (prev_dbi >= 0) 603 (*iov)++; 604 /* write offset relative to mb_addr */ 605 (*iov)->iov_base = (void __user *) 606 (udev->data_off + dbi * DATA_BLOCK_SIZE); 607 } 608 (*iov)->iov_len += len; 609 610 return dbi; 611 } 612 613 static void tcmu_setup_iovs(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 614 struct iovec **iov, int data_length) 615 { 616 /* start value of dbi + 1 must not be a valid dbi */ 617 int dbi = -2; 618 619 /* We prepare the IOVs for DMA_FROM_DEVICE transfer direction */ 620 for (; data_length > 0; data_length -= DATA_BLOCK_SIZE) 621 dbi = new_block_to_iov(udev, cmd, iov, dbi, data_length); 622 } 623 624 static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd) 625 { 626 struct se_device *se_dev = se_cmd->se_dev; 627 struct tcmu_dev *udev = TCMU_DEV(se_dev); 628 struct tcmu_cmd *tcmu_cmd; 629 630 tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_NOIO); 631 if (!tcmu_cmd) 632 return NULL; 633 634 INIT_LIST_HEAD(&tcmu_cmd->queue_entry); 635 tcmu_cmd->se_cmd = se_cmd; 636 tcmu_cmd->tcmu_dev = udev; 637 638 tcmu_cmd_set_block_cnts(tcmu_cmd); 639 tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t), 640 GFP_NOIO); 641 if (!tcmu_cmd->dbi) { 642 kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); 643 return NULL; 644 } 645 646 return tcmu_cmd; 647 } 648 649 static inline void tcmu_flush_dcache_range(void *vaddr, size_t size) 650 { 651 unsigned long offset = offset_in_page(vaddr); 652 void *start = vaddr - offset; 653 654 size = round_up(size+offset, PAGE_SIZE); 655 656 while (size) { 657 flush_dcache_page(vmalloc_to_page(start)); 658 start += PAGE_SIZE; 659 size -= PAGE_SIZE; 660 } 661 } 662 663 /* 664 * Some ring helper functions. We don't assume size is a power of 2 so 665 * we can't use circ_buf.h. 666 */ 667 static inline size_t spc_used(size_t head, size_t tail, size_t size) 668 { 669 int diff = head - tail; 670 671 if (diff >= 0) 672 return diff; 673 else 674 return size + diff; 675 } 676 677 static inline size_t spc_free(size_t head, size_t tail, size_t size) 678 { 679 /* Keep 1 byte unused or we can't tell full from empty */ 680 return (size - spc_used(head, tail, size) - 1); 681 } 682 683 static inline size_t head_to_end(size_t head, size_t size) 684 { 685 return size - head; 686 } 687 688 #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size) 689 690 #define TCMU_SG_TO_DATA_AREA 1 691 #define TCMU_DATA_AREA_TO_SG 2 692 693 static inline void tcmu_copy_data(struct tcmu_dev *udev, 694 struct tcmu_cmd *tcmu_cmd, uint32_t direction, 695 struct scatterlist *sg, unsigned int sg_nents, 696 struct iovec **iov, size_t data_len) 697 { 698 /* start value of dbi + 1 must not be a valid dbi */ 699 int dbi = -2; 700 size_t block_remaining, cp_len; 701 struct sg_mapping_iter sg_iter; 702 unsigned int sg_flags; 703 struct page *page; 704 void *data_page_start, *data_addr; 705 706 if (direction == TCMU_SG_TO_DATA_AREA) 707 sg_flags = SG_MITER_ATOMIC | SG_MITER_FROM_SG; 708 else 709 sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG; 710 sg_miter_start(&sg_iter, sg, sg_nents, sg_flags); 711 712 while (data_len) { 713 if (direction == TCMU_SG_TO_DATA_AREA) 714 dbi = new_block_to_iov(udev, tcmu_cmd, iov, dbi, 715 data_len); 716 else 717 dbi = tcmu_cmd_get_dbi(tcmu_cmd); 718 page = tcmu_get_block_page(udev, dbi); 719 if (direction == TCMU_DATA_AREA_TO_SG) 720 flush_dcache_page(page); 721 data_page_start = kmap_atomic(page); 722 block_remaining = DATA_BLOCK_SIZE; 723 724 while (block_remaining && data_len) { 725 if (!sg_miter_next(&sg_iter)) { 726 /* set length to 0 to abort outer loop */ 727 data_len = 0; 728 pr_debug("tcmu_move_data: aborting data copy due to exhausted sg_list\n"); 729 break; 730 } 731 cp_len = min3(sg_iter.length, block_remaining, data_len); 732 733 data_addr = data_page_start + 734 DATA_BLOCK_SIZE - block_remaining; 735 if (direction == TCMU_SG_TO_DATA_AREA) 736 memcpy(data_addr, sg_iter.addr, cp_len); 737 else 738 memcpy(sg_iter.addr, data_addr, cp_len); 739 740 data_len -= cp_len; 741 block_remaining -= cp_len; 742 sg_iter.consumed = cp_len; 743 } 744 sg_miter_stop(&sg_iter); 745 746 kunmap_atomic(data_page_start); 747 if (direction == TCMU_SG_TO_DATA_AREA) 748 flush_dcache_page(page); 749 } 750 } 751 752 static void scatter_data_area(struct tcmu_dev *udev, struct tcmu_cmd *tcmu_cmd, 753 struct iovec **iov) 754 { 755 struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 756 757 tcmu_copy_data(udev, tcmu_cmd, TCMU_SG_TO_DATA_AREA, se_cmd->t_data_sg, 758 se_cmd->t_data_nents, iov, se_cmd->data_length); 759 } 760 761 static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *tcmu_cmd, 762 bool bidi, uint32_t read_len) 763 { 764 struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 765 struct scatterlist *data_sg; 766 unsigned int data_nents; 767 768 if (!bidi) { 769 data_sg = se_cmd->t_data_sg; 770 data_nents = se_cmd->t_data_nents; 771 } else { 772 /* 773 * For bidi case, the first count blocks are for Data-Out 774 * buffer blocks, and before gathering the Data-In buffer 775 * the Data-Out buffer blocks should be skipped. 776 */ 777 tcmu_cmd_set_dbi_cur(tcmu_cmd, 778 tcmu_cmd->dbi_cnt - tcmu_cmd->dbi_bidi_cnt); 779 780 data_sg = se_cmd->t_bidi_data_sg; 781 data_nents = se_cmd->t_bidi_data_nents; 782 } 783 784 tcmu_copy_data(udev, tcmu_cmd, TCMU_DATA_AREA_TO_SG, data_sg, 785 data_nents, NULL, read_len); 786 } 787 788 static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh) 789 { 790 return thresh - bitmap_weight(bitmap, thresh); 791 } 792 793 /* 794 * We can't queue a command until we have space available on the cmd ring. 795 * 796 * Called with ring lock held. 797 */ 798 static bool is_ring_space_avail(struct tcmu_dev *udev, size_t cmd_size) 799 { 800 struct tcmu_mailbox *mb = udev->mb_addr; 801 size_t space, cmd_needed; 802 u32 cmd_head; 803 804 tcmu_flush_dcache_range(mb, sizeof(*mb)); 805 806 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 807 808 /* 809 * If cmd end-of-ring space is too small then we need space for a NOP plus 810 * original cmd - cmds are internally contiguous. 811 */ 812 if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size) 813 cmd_needed = cmd_size; 814 else 815 cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size); 816 817 space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size); 818 if (space < cmd_needed) { 819 pr_debug("no cmd space: %u %u %u\n", cmd_head, 820 udev->cmdr_last_cleaned, udev->cmdr_size); 821 return false; 822 } 823 return true; 824 } 825 826 /* 827 * We have to allocate data buffers before we can queue a command. 828 * Returns -1 on error (not enough space) or number of needed iovs on success 829 * 830 * Called with ring lock held. 831 */ 832 static int tcmu_alloc_data_space(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 833 int *iov_bidi_cnt) 834 { 835 int space, iov_cnt = 0, ret = 0; 836 837 if (!cmd->dbi_cnt) 838 goto wr_iov_cnts; 839 840 /* try to check and get the data blocks as needed */ 841 space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh); 842 if (space < cmd->dbi_cnt) { 843 unsigned long blocks_left = 844 (udev->max_blocks - udev->dbi_thresh) + space; 845 846 if (blocks_left < cmd->dbi_cnt) { 847 pr_debug("no data space: only %lu available, but ask for %lu\n", 848 blocks_left * DATA_BLOCK_SIZE, 849 cmd->dbi_cnt * DATA_BLOCK_SIZE); 850 return -1; 851 } 852 853 udev->dbi_thresh += cmd->dbi_cnt; 854 if (udev->dbi_thresh > udev->max_blocks) 855 udev->dbi_thresh = udev->max_blocks; 856 } 857 858 iov_cnt = tcmu_get_empty_blocks(udev, cmd, 859 cmd->dbi_cnt - cmd->dbi_bidi_cnt); 860 if (iov_cnt < 0) 861 return -1; 862 863 if (cmd->dbi_bidi_cnt) { 864 ret = tcmu_get_empty_blocks(udev, cmd, cmd->dbi_bidi_cnt); 865 if (ret < 0) 866 return -1; 867 } 868 wr_iov_cnts: 869 *iov_bidi_cnt = ret; 870 return iov_cnt + ret; 871 } 872 873 static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt) 874 { 875 return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]), 876 sizeof(struct tcmu_cmd_entry)); 877 } 878 879 static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd, 880 size_t base_command_size) 881 { 882 struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 883 size_t command_size; 884 885 command_size = base_command_size + 886 round_up(scsi_command_size(se_cmd->t_task_cdb), 887 TCMU_OP_ALIGN_SIZE); 888 889 WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1)); 890 891 return command_size; 892 } 893 894 static void tcmu_setup_cmd_timer(struct tcmu_cmd *tcmu_cmd, unsigned int tmo, 895 struct timer_list *timer) 896 { 897 if (!tmo) 898 return; 899 900 tcmu_cmd->deadline = round_jiffies_up(jiffies + msecs_to_jiffies(tmo)); 901 if (!timer_pending(timer)) 902 mod_timer(timer, tcmu_cmd->deadline); 903 904 pr_debug("Timeout set up for cmd %p, dev = %s, tmo = %lu\n", tcmu_cmd, 905 tcmu_cmd->tcmu_dev->name, tmo / MSEC_PER_SEC); 906 } 907 908 static int add_to_qfull_queue(struct tcmu_cmd *tcmu_cmd) 909 { 910 struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 911 unsigned int tmo; 912 913 /* 914 * For backwards compat if qfull_time_out is not set use 915 * cmd_time_out and if that's not set use the default time out. 916 */ 917 if (!udev->qfull_time_out) 918 return -ETIMEDOUT; 919 else if (udev->qfull_time_out > 0) 920 tmo = udev->qfull_time_out; 921 else if (udev->cmd_time_out) 922 tmo = udev->cmd_time_out; 923 else 924 tmo = TCMU_TIME_OUT; 925 926 tcmu_setup_cmd_timer(tcmu_cmd, tmo, &udev->qfull_timer); 927 928 list_add_tail(&tcmu_cmd->queue_entry, &udev->qfull_queue); 929 pr_debug("adding cmd %p on dev %s to ring space wait queue\n", 930 tcmu_cmd, udev->name); 931 return 0; 932 } 933 934 static uint32_t ring_insert_padding(struct tcmu_dev *udev, size_t cmd_size) 935 { 936 struct tcmu_cmd_entry_hdr *hdr; 937 struct tcmu_mailbox *mb = udev->mb_addr; 938 uint32_t cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 939 940 /* Insert a PAD if end-of-ring space is too small */ 941 if (head_to_end(cmd_head, udev->cmdr_size) < cmd_size) { 942 size_t pad_size = head_to_end(cmd_head, udev->cmdr_size); 943 944 hdr = (void *) mb + CMDR_OFF + cmd_head; 945 tcmu_hdr_set_op(&hdr->len_op, TCMU_OP_PAD); 946 tcmu_hdr_set_len(&hdr->len_op, pad_size); 947 hdr->cmd_id = 0; /* not used for PAD */ 948 hdr->kflags = 0; 949 hdr->uflags = 0; 950 tcmu_flush_dcache_range(hdr, sizeof(*hdr)); 951 952 UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size); 953 tcmu_flush_dcache_range(mb, sizeof(*mb)); 954 955 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 956 WARN_ON(cmd_head != 0); 957 } 958 959 return cmd_head; 960 } 961 962 /** 963 * queue_cmd_ring - queue cmd to ring or internally 964 * @tcmu_cmd: cmd to queue 965 * @scsi_err: TCM error code if failure (-1) returned. 966 * 967 * Returns: 968 * -1 we cannot queue internally or to the ring. 969 * 0 success 970 * 1 internally queued to wait for ring memory to free. 971 */ 972 static int queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, sense_reason_t *scsi_err) 973 { 974 struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 975 struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 976 size_t base_command_size, command_size; 977 struct tcmu_mailbox *mb = udev->mb_addr; 978 struct tcmu_cmd_entry *entry; 979 struct iovec *iov; 980 int iov_cnt, iov_bidi_cnt, cmd_id; 981 uint32_t cmd_head; 982 uint64_t cdb_off; 983 /* size of data buffer needed */ 984 size_t data_length = (size_t)tcmu_cmd->dbi_cnt * DATA_BLOCK_SIZE; 985 986 *scsi_err = TCM_NO_SENSE; 987 988 if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) { 989 *scsi_err = TCM_LUN_BUSY; 990 return -1; 991 } 992 993 if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) { 994 *scsi_err = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 995 return -1; 996 } 997 998 if (!list_empty(&udev->qfull_queue)) 999 goto queue; 1000 1001 if (data_length > udev->data_size) { 1002 pr_warn("TCMU: Request of size %zu is too big for %zu data area\n", 1003 data_length, udev->data_size); 1004 *scsi_err = TCM_INVALID_CDB_FIELD; 1005 return -1; 1006 } 1007 1008 iov_cnt = tcmu_alloc_data_space(udev, tcmu_cmd, &iov_bidi_cnt); 1009 if (iov_cnt < 0) 1010 goto free_and_queue; 1011 1012 /* 1013 * Must be a certain minimum size for response sense info, but 1014 * also may be larger if the iov array is large. 1015 */ 1016 base_command_size = tcmu_cmd_get_base_cmd_size(iov_cnt); 1017 command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size); 1018 1019 if (command_size > (udev->cmdr_size / 2)) { 1020 pr_warn("TCMU: Request of size %zu is too big for %u cmd ring\n", 1021 command_size, udev->cmdr_size); 1022 tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur); 1023 *scsi_err = TCM_INVALID_CDB_FIELD; 1024 return -1; 1025 } 1026 1027 if (!is_ring_space_avail(udev, command_size)) 1028 /* 1029 * Don't leave commands partially setup because the unmap 1030 * thread might need the blocks to make forward progress. 1031 */ 1032 goto free_and_queue; 1033 1034 cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 1, USHRT_MAX, GFP_NOWAIT); 1035 if (cmd_id < 0) { 1036 pr_err("tcmu: Could not allocate cmd id.\n"); 1037 1038 tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt); 1039 *scsi_err = TCM_OUT_OF_RESOURCES; 1040 return -1; 1041 } 1042 tcmu_cmd->cmd_id = cmd_id; 1043 1044 pr_debug("allocated cmd id %u for cmd %p dev %s\n", tcmu_cmd->cmd_id, 1045 tcmu_cmd, udev->name); 1046 1047 cmd_head = ring_insert_padding(udev, command_size); 1048 1049 entry = (void *) mb + CMDR_OFF + cmd_head; 1050 memset(entry, 0, command_size); 1051 tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD); 1052 1053 /* prepare iov list and copy data to data area if necessary */ 1054 tcmu_cmd_reset_dbi_cur(tcmu_cmd); 1055 iov = &entry->req.iov[0]; 1056 1057 if (se_cmd->data_direction == DMA_TO_DEVICE || 1058 se_cmd->se_cmd_flags & SCF_BIDI) 1059 scatter_data_area(udev, tcmu_cmd, &iov); 1060 else 1061 tcmu_setup_iovs(udev, tcmu_cmd, &iov, se_cmd->data_length); 1062 1063 entry->req.iov_cnt = iov_cnt - iov_bidi_cnt; 1064 1065 /* Handle BIDI commands */ 1066 if (se_cmd->se_cmd_flags & SCF_BIDI) { 1067 iov++; 1068 tcmu_setup_iovs(udev, tcmu_cmd, &iov, tcmu_cmd->data_len_bidi); 1069 entry->req.iov_bidi_cnt = iov_bidi_cnt; 1070 } 1071 1072 tcmu_setup_cmd_timer(tcmu_cmd, udev->cmd_time_out, &udev->cmd_timer); 1073 1074 entry->hdr.cmd_id = tcmu_cmd->cmd_id; 1075 1076 tcmu_hdr_set_len(&entry->hdr.len_op, command_size); 1077 1078 /* All offsets relative to mb_addr, not start of entry! */ 1079 cdb_off = CMDR_OFF + cmd_head + base_command_size; 1080 memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb)); 1081 entry->req.cdb_off = cdb_off; 1082 tcmu_flush_dcache_range(entry, command_size); 1083 1084 UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size); 1085 tcmu_flush_dcache_range(mb, sizeof(*mb)); 1086 1087 list_add_tail(&tcmu_cmd->queue_entry, &udev->inflight_queue); 1088 1089 /* TODO: only if FLUSH and FUA? */ 1090 uio_event_notify(&udev->uio_info); 1091 1092 return 0; 1093 1094 free_and_queue: 1095 tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur); 1096 tcmu_cmd_reset_dbi_cur(tcmu_cmd); 1097 1098 queue: 1099 if (add_to_qfull_queue(tcmu_cmd)) { 1100 *scsi_err = TCM_OUT_OF_RESOURCES; 1101 return -1; 1102 } 1103 1104 return 1; 1105 } 1106 1107 /** 1108 * queue_tmr_ring - queue tmr info to ring or internally 1109 * @udev: related tcmu_dev 1110 * @tmr: tcmu_tmr containing tmr info to queue 1111 * 1112 * Returns: 1113 * 0 success 1114 * 1 internally queued to wait for ring memory to free. 1115 */ 1116 static int 1117 queue_tmr_ring(struct tcmu_dev *udev, struct tcmu_tmr *tmr) 1118 { 1119 struct tcmu_tmr_entry *entry; 1120 int cmd_size; 1121 int id_list_sz; 1122 struct tcmu_mailbox *mb = udev->mb_addr; 1123 uint32_t cmd_head; 1124 1125 if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) 1126 goto out_free; 1127 1128 id_list_sz = sizeof(tmr->tmr_cmd_ids[0]) * tmr->tmr_cmd_cnt; 1129 cmd_size = round_up(sizeof(*entry) + id_list_sz, TCMU_OP_ALIGN_SIZE); 1130 1131 if (!list_empty(&udev->tmr_queue) || 1132 !is_ring_space_avail(udev, cmd_size)) { 1133 list_add_tail(&tmr->queue_entry, &udev->tmr_queue); 1134 pr_debug("adding tmr %p on dev %s to TMR ring space wait queue\n", 1135 tmr, udev->name); 1136 return 1; 1137 } 1138 1139 cmd_head = ring_insert_padding(udev, cmd_size); 1140 1141 entry = (void *)mb + CMDR_OFF + cmd_head; 1142 memset(entry, 0, cmd_size); 1143 tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_TMR); 1144 tcmu_hdr_set_len(&entry->hdr.len_op, cmd_size); 1145 entry->tmr_type = tmr->tmr_type; 1146 entry->cmd_cnt = tmr->tmr_cmd_cnt; 1147 memcpy(&entry->cmd_ids[0], &tmr->tmr_cmd_ids[0], id_list_sz); 1148 tcmu_flush_dcache_range(entry, cmd_size); 1149 1150 UPDATE_HEAD(mb->cmd_head, cmd_size, udev->cmdr_size); 1151 tcmu_flush_dcache_range(mb, sizeof(*mb)); 1152 1153 uio_event_notify(&udev->uio_info); 1154 1155 out_free: 1156 kfree(tmr); 1157 1158 return 0; 1159 } 1160 1161 static sense_reason_t 1162 tcmu_queue_cmd(struct se_cmd *se_cmd) 1163 { 1164 struct se_device *se_dev = se_cmd->se_dev; 1165 struct tcmu_dev *udev = TCMU_DEV(se_dev); 1166 struct tcmu_cmd *tcmu_cmd; 1167 sense_reason_t scsi_ret = TCM_CHECK_CONDITION_ABORT_CMD; 1168 int ret = -1; 1169 1170 tcmu_cmd = tcmu_alloc_cmd(se_cmd); 1171 if (!tcmu_cmd) 1172 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1173 1174 mutex_lock(&udev->cmdr_lock); 1175 if (!(se_cmd->transport_state & CMD_T_ABORTED)) 1176 ret = queue_cmd_ring(tcmu_cmd, &scsi_ret); 1177 if (ret < 0) 1178 tcmu_free_cmd(tcmu_cmd); 1179 else 1180 se_cmd->priv = tcmu_cmd; 1181 mutex_unlock(&udev->cmdr_lock); 1182 return scsi_ret; 1183 } 1184 1185 static void tcmu_set_next_deadline(struct list_head *queue, 1186 struct timer_list *timer) 1187 { 1188 struct tcmu_cmd *cmd; 1189 1190 if (!list_empty(queue)) { 1191 cmd = list_first_entry(queue, struct tcmu_cmd, queue_entry); 1192 mod_timer(timer, cmd->deadline); 1193 } else 1194 del_timer(timer); 1195 } 1196 1197 static int 1198 tcmu_tmr_type(enum tcm_tmreq_table tmf) 1199 { 1200 switch (tmf) { 1201 case TMR_ABORT_TASK: return TCMU_TMR_ABORT_TASK; 1202 case TMR_ABORT_TASK_SET: return TCMU_TMR_ABORT_TASK_SET; 1203 case TMR_CLEAR_ACA: return TCMU_TMR_CLEAR_ACA; 1204 case TMR_CLEAR_TASK_SET: return TCMU_TMR_CLEAR_TASK_SET; 1205 case TMR_LUN_RESET: return TCMU_TMR_LUN_RESET; 1206 case TMR_TARGET_WARM_RESET: return TCMU_TMR_TARGET_WARM_RESET; 1207 case TMR_TARGET_COLD_RESET: return TCMU_TMR_TARGET_COLD_RESET; 1208 case TMR_LUN_RESET_PRO: return TCMU_TMR_LUN_RESET_PRO; 1209 default: return TCMU_TMR_UNKNOWN; 1210 } 1211 } 1212 1213 static void 1214 tcmu_tmr_notify(struct se_device *se_dev, enum tcm_tmreq_table tmf, 1215 struct list_head *cmd_list) 1216 { 1217 int i = 0, cmd_cnt = 0; 1218 bool unqueued = false; 1219 uint16_t *cmd_ids = NULL; 1220 struct tcmu_cmd *cmd; 1221 struct se_cmd *se_cmd; 1222 struct tcmu_tmr *tmr; 1223 struct tcmu_dev *udev = TCMU_DEV(se_dev); 1224 1225 mutex_lock(&udev->cmdr_lock); 1226 1227 /* First we check for aborted commands in qfull_queue */ 1228 list_for_each_entry(se_cmd, cmd_list, state_list) { 1229 i++; 1230 if (!se_cmd->priv) 1231 continue; 1232 cmd = se_cmd->priv; 1233 /* Commands on qfull queue have no id yet */ 1234 if (cmd->cmd_id) { 1235 cmd_cnt++; 1236 continue; 1237 } 1238 pr_debug("Removing aborted command %p from queue on dev %s.\n", 1239 cmd, udev->name); 1240 1241 list_del_init(&cmd->queue_entry); 1242 tcmu_free_cmd(cmd); 1243 se_cmd->priv = NULL; 1244 target_complete_cmd(se_cmd, SAM_STAT_TASK_ABORTED); 1245 unqueued = true; 1246 } 1247 if (unqueued) 1248 tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer); 1249 1250 if (!test_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags)) 1251 goto unlock; 1252 1253 pr_debug("TMR event %d on dev %s, aborted cmds %d, afflicted cmd_ids %d\n", 1254 tcmu_tmr_type(tmf), udev->name, i, cmd_cnt); 1255 1256 tmr = kmalloc(sizeof(*tmr) + cmd_cnt * sizeof(*cmd_ids), GFP_KERNEL); 1257 if (!tmr) 1258 goto unlock; 1259 1260 tmr->tmr_type = tcmu_tmr_type(tmf); 1261 tmr->tmr_cmd_cnt = cmd_cnt; 1262 1263 if (cmd_cnt != 0) { 1264 cmd_cnt = 0; 1265 list_for_each_entry(se_cmd, cmd_list, state_list) { 1266 if (!se_cmd->priv) 1267 continue; 1268 cmd = se_cmd->priv; 1269 if (cmd->cmd_id) 1270 tmr->tmr_cmd_ids[cmd_cnt++] = cmd->cmd_id; 1271 } 1272 } 1273 1274 queue_tmr_ring(udev, tmr); 1275 1276 unlock: 1277 mutex_unlock(&udev->cmdr_lock); 1278 } 1279 1280 static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry) 1281 { 1282 struct se_cmd *se_cmd = cmd->se_cmd; 1283 struct tcmu_dev *udev = cmd->tcmu_dev; 1284 bool read_len_valid = false; 1285 uint32_t read_len; 1286 1287 /* 1288 * cmd has been completed already from timeout, just reclaim 1289 * data area space and free cmd 1290 */ 1291 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { 1292 WARN_ON_ONCE(se_cmd); 1293 goto out; 1294 } 1295 1296 list_del_init(&cmd->queue_entry); 1297 1298 tcmu_cmd_reset_dbi_cur(cmd); 1299 1300 if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) { 1301 pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n", 1302 cmd->se_cmd); 1303 entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION; 1304 goto done; 1305 } 1306 1307 read_len = se_cmd->data_length; 1308 if (se_cmd->data_direction == DMA_FROM_DEVICE && 1309 (entry->hdr.uflags & TCMU_UFLAG_READ_LEN) && entry->rsp.read_len) { 1310 read_len_valid = true; 1311 if (entry->rsp.read_len < read_len) 1312 read_len = entry->rsp.read_len; 1313 } 1314 1315 if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) { 1316 transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer); 1317 if (!read_len_valid ) 1318 goto done; 1319 else 1320 se_cmd->se_cmd_flags |= SCF_TREAT_READ_AS_NORMAL; 1321 } 1322 if (se_cmd->se_cmd_flags & SCF_BIDI) { 1323 /* Get Data-In buffer before clean up */ 1324 gather_data_area(udev, cmd, true, read_len); 1325 } else if (se_cmd->data_direction == DMA_FROM_DEVICE) { 1326 gather_data_area(udev, cmd, false, read_len); 1327 } else if (se_cmd->data_direction == DMA_TO_DEVICE) { 1328 /* TODO: */ 1329 } else if (se_cmd->data_direction != DMA_NONE) { 1330 pr_warn("TCMU: data direction was %d!\n", 1331 se_cmd->data_direction); 1332 } 1333 1334 done: 1335 se_cmd->priv = NULL; 1336 if (read_len_valid) { 1337 pr_debug("read_len = %d\n", read_len); 1338 target_complete_cmd_with_length(cmd->se_cmd, 1339 entry->rsp.scsi_status, read_len); 1340 } else 1341 target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status); 1342 1343 out: 1344 tcmu_cmd_free_data(cmd, cmd->dbi_cnt); 1345 tcmu_free_cmd(cmd); 1346 } 1347 1348 static int tcmu_run_tmr_queue(struct tcmu_dev *udev) 1349 { 1350 struct tcmu_tmr *tmr, *tmp; 1351 LIST_HEAD(tmrs); 1352 1353 if (list_empty(&udev->tmr_queue)) 1354 return 1; 1355 1356 pr_debug("running %s's tmr queue\n", udev->name); 1357 1358 list_splice_init(&udev->tmr_queue, &tmrs); 1359 1360 list_for_each_entry_safe(tmr, tmp, &tmrs, queue_entry) { 1361 list_del_init(&tmr->queue_entry); 1362 1363 pr_debug("removing tmr %p on dev %s from queue\n", 1364 tmr, udev->name); 1365 1366 if (queue_tmr_ring(udev, tmr)) { 1367 pr_debug("ran out of space during tmr queue run\n"); 1368 /* 1369 * tmr was requeued, so just put all tmrs back in 1370 * the queue 1371 */ 1372 list_splice_tail(&tmrs, &udev->tmr_queue); 1373 return 0; 1374 } 1375 } 1376 1377 return 1; 1378 } 1379 1380 static unsigned int tcmu_handle_completions(struct tcmu_dev *udev) 1381 { 1382 struct tcmu_mailbox *mb; 1383 struct tcmu_cmd *cmd; 1384 bool free_space = false; 1385 1386 if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) { 1387 pr_err("ring broken, not handling completions\n"); 1388 return 0; 1389 } 1390 1391 mb = udev->mb_addr; 1392 tcmu_flush_dcache_range(mb, sizeof(*mb)); 1393 1394 while (udev->cmdr_last_cleaned != READ_ONCE(mb->cmd_tail)) { 1395 1396 struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned; 1397 1398 /* 1399 * Flush max. up to end of cmd ring since current entry might 1400 * be a padding that is shorter than sizeof(*entry) 1401 */ 1402 size_t ring_left = head_to_end(udev->cmdr_last_cleaned, 1403 udev->cmdr_size); 1404 tcmu_flush_dcache_range(entry, ring_left < sizeof(*entry) ? 1405 ring_left : sizeof(*entry)); 1406 1407 free_space = true; 1408 1409 if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD || 1410 tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_TMR) { 1411 UPDATE_HEAD(udev->cmdr_last_cleaned, 1412 tcmu_hdr_get_len(entry->hdr.len_op), 1413 udev->cmdr_size); 1414 continue; 1415 } 1416 WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD); 1417 1418 cmd = idr_remove(&udev->commands, entry->hdr.cmd_id); 1419 if (!cmd) { 1420 pr_err("cmd_id %u not found, ring is broken\n", 1421 entry->hdr.cmd_id); 1422 set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags); 1423 break; 1424 } 1425 1426 tcmu_handle_completion(cmd, entry); 1427 1428 UPDATE_HEAD(udev->cmdr_last_cleaned, 1429 tcmu_hdr_get_len(entry->hdr.len_op), 1430 udev->cmdr_size); 1431 } 1432 if (free_space) 1433 free_space = tcmu_run_tmr_queue(udev); 1434 1435 if (atomic_read(&global_db_count) > tcmu_global_max_blocks && 1436 idr_is_empty(&udev->commands) && list_empty(&udev->qfull_queue)) { 1437 /* 1438 * Allocated blocks exceeded global block limit, currently no 1439 * more pending or waiting commands so try to reclaim blocks. 1440 */ 1441 schedule_delayed_work(&tcmu_unmap_work, 0); 1442 } 1443 if (udev->cmd_time_out) 1444 tcmu_set_next_deadline(&udev->inflight_queue, &udev->cmd_timer); 1445 1446 return free_space; 1447 } 1448 1449 static void tcmu_check_expired_ring_cmd(struct tcmu_cmd *cmd) 1450 { 1451 struct se_cmd *se_cmd; 1452 1453 if (!time_after_eq(jiffies, cmd->deadline)) 1454 return; 1455 1456 set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags); 1457 list_del_init(&cmd->queue_entry); 1458 se_cmd = cmd->se_cmd; 1459 se_cmd->priv = NULL; 1460 cmd->se_cmd = NULL; 1461 1462 pr_debug("Timing out inflight cmd %u on dev %s.\n", 1463 cmd->cmd_id, cmd->tcmu_dev->name); 1464 1465 target_complete_cmd(se_cmd, SAM_STAT_CHECK_CONDITION); 1466 } 1467 1468 static void tcmu_check_expired_queue_cmd(struct tcmu_cmd *cmd) 1469 { 1470 struct se_cmd *se_cmd; 1471 1472 if (!time_after_eq(jiffies, cmd->deadline)) 1473 return; 1474 1475 pr_debug("Timing out queued cmd %p on dev %s.\n", 1476 cmd, cmd->tcmu_dev->name); 1477 1478 list_del_init(&cmd->queue_entry); 1479 se_cmd = cmd->se_cmd; 1480 tcmu_free_cmd(cmd); 1481 1482 se_cmd->priv = NULL; 1483 target_complete_cmd(se_cmd, SAM_STAT_TASK_SET_FULL); 1484 } 1485 1486 static void tcmu_device_timedout(struct tcmu_dev *udev) 1487 { 1488 spin_lock(&timed_out_udevs_lock); 1489 if (list_empty(&udev->timedout_entry)) 1490 list_add_tail(&udev->timedout_entry, &timed_out_udevs); 1491 spin_unlock(&timed_out_udevs_lock); 1492 1493 schedule_delayed_work(&tcmu_unmap_work, 0); 1494 } 1495 1496 static void tcmu_cmd_timedout(struct timer_list *t) 1497 { 1498 struct tcmu_dev *udev = from_timer(udev, t, cmd_timer); 1499 1500 pr_debug("%s cmd timeout has expired\n", udev->name); 1501 tcmu_device_timedout(udev); 1502 } 1503 1504 static void tcmu_qfull_timedout(struct timer_list *t) 1505 { 1506 struct tcmu_dev *udev = from_timer(udev, t, qfull_timer); 1507 1508 pr_debug("%s qfull timeout has expired\n", udev->name); 1509 tcmu_device_timedout(udev); 1510 } 1511 1512 static int tcmu_attach_hba(struct se_hba *hba, u32 host_id) 1513 { 1514 struct tcmu_hba *tcmu_hba; 1515 1516 tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL); 1517 if (!tcmu_hba) 1518 return -ENOMEM; 1519 1520 tcmu_hba->host_id = host_id; 1521 hba->hba_ptr = tcmu_hba; 1522 1523 return 0; 1524 } 1525 1526 static void tcmu_detach_hba(struct se_hba *hba) 1527 { 1528 kfree(hba->hba_ptr); 1529 hba->hba_ptr = NULL; 1530 } 1531 1532 static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name) 1533 { 1534 struct tcmu_dev *udev; 1535 1536 udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL); 1537 if (!udev) 1538 return NULL; 1539 kref_init(&udev->kref); 1540 1541 udev->name = kstrdup(name, GFP_KERNEL); 1542 if (!udev->name) { 1543 kfree(udev); 1544 return NULL; 1545 } 1546 1547 udev->hba = hba; 1548 udev->cmd_time_out = TCMU_TIME_OUT; 1549 udev->qfull_time_out = -1; 1550 1551 udev->max_blocks = DATA_BLOCK_BITS_DEF; 1552 mutex_init(&udev->cmdr_lock); 1553 1554 INIT_LIST_HEAD(&udev->node); 1555 INIT_LIST_HEAD(&udev->timedout_entry); 1556 INIT_LIST_HEAD(&udev->qfull_queue); 1557 INIT_LIST_HEAD(&udev->tmr_queue); 1558 INIT_LIST_HEAD(&udev->inflight_queue); 1559 idr_init(&udev->commands); 1560 1561 timer_setup(&udev->qfull_timer, tcmu_qfull_timedout, 0); 1562 timer_setup(&udev->cmd_timer, tcmu_cmd_timedout, 0); 1563 1564 INIT_RADIX_TREE(&udev->data_blocks, GFP_KERNEL); 1565 1566 return &udev->se_dev; 1567 } 1568 1569 static void tcmu_dev_call_rcu(struct rcu_head *p) 1570 { 1571 struct se_device *dev = container_of(p, struct se_device, rcu_head); 1572 struct tcmu_dev *udev = TCMU_DEV(dev); 1573 1574 kfree(udev->uio_info.name); 1575 kfree(udev->name); 1576 kfree(udev); 1577 } 1578 1579 static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd) 1580 { 1581 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { 1582 kmem_cache_free(tcmu_cmd_cache, cmd); 1583 return 0; 1584 } 1585 return -EINVAL; 1586 } 1587 1588 static void tcmu_blocks_release(struct radix_tree_root *blocks, 1589 int start, int end) 1590 { 1591 int i; 1592 struct page *page; 1593 1594 for (i = start; i < end; i++) { 1595 page = radix_tree_delete(blocks, i); 1596 if (page) { 1597 __free_page(page); 1598 atomic_dec(&global_db_count); 1599 } 1600 } 1601 } 1602 1603 static void tcmu_remove_all_queued_tmr(struct tcmu_dev *udev) 1604 { 1605 struct tcmu_tmr *tmr, *tmp; 1606 1607 list_for_each_entry_safe(tmr, tmp, &udev->tmr_queue, queue_entry) { 1608 list_del_init(&tmr->queue_entry); 1609 kfree(tmr); 1610 } 1611 } 1612 1613 static void tcmu_dev_kref_release(struct kref *kref) 1614 { 1615 struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref); 1616 struct se_device *dev = &udev->se_dev; 1617 struct tcmu_cmd *cmd; 1618 bool all_expired = true; 1619 int i; 1620 1621 vfree(udev->mb_addr); 1622 udev->mb_addr = NULL; 1623 1624 spin_lock_bh(&timed_out_udevs_lock); 1625 if (!list_empty(&udev->timedout_entry)) 1626 list_del(&udev->timedout_entry); 1627 spin_unlock_bh(&timed_out_udevs_lock); 1628 1629 /* Upper layer should drain all requests before calling this */ 1630 mutex_lock(&udev->cmdr_lock); 1631 idr_for_each_entry(&udev->commands, cmd, i) { 1632 if (tcmu_check_and_free_pending_cmd(cmd) != 0) 1633 all_expired = false; 1634 } 1635 /* There can be left over TMR cmds. Remove them. */ 1636 tcmu_remove_all_queued_tmr(udev); 1637 if (!list_empty(&udev->qfull_queue)) 1638 all_expired = false; 1639 idr_destroy(&udev->commands); 1640 WARN_ON(!all_expired); 1641 1642 tcmu_blocks_release(&udev->data_blocks, 0, udev->dbi_max + 1); 1643 bitmap_free(udev->data_bitmap); 1644 mutex_unlock(&udev->cmdr_lock); 1645 1646 pr_debug("dev_kref_release\n"); 1647 1648 call_rcu(&dev->rcu_head, tcmu_dev_call_rcu); 1649 } 1650 1651 static void run_qfull_queue(struct tcmu_dev *udev, bool fail) 1652 { 1653 struct tcmu_cmd *tcmu_cmd, *tmp_cmd; 1654 LIST_HEAD(cmds); 1655 sense_reason_t scsi_ret; 1656 int ret; 1657 1658 if (list_empty(&udev->qfull_queue)) 1659 return; 1660 1661 pr_debug("running %s's cmdr queue forcefail %d\n", udev->name, fail); 1662 1663 list_splice_init(&udev->qfull_queue, &cmds); 1664 1665 list_for_each_entry_safe(tcmu_cmd, tmp_cmd, &cmds, queue_entry) { 1666 list_del_init(&tcmu_cmd->queue_entry); 1667 1668 pr_debug("removing cmd %p on dev %s from queue\n", 1669 tcmu_cmd, udev->name); 1670 1671 if (fail) { 1672 /* 1673 * We were not able to even start the command, so 1674 * fail with busy to allow a retry in case runner 1675 * was only temporarily down. If the device is being 1676 * removed then LIO core will do the right thing and 1677 * fail the retry. 1678 */ 1679 tcmu_cmd->se_cmd->priv = NULL; 1680 target_complete_cmd(tcmu_cmd->se_cmd, SAM_STAT_BUSY); 1681 tcmu_free_cmd(tcmu_cmd); 1682 continue; 1683 } 1684 1685 ret = queue_cmd_ring(tcmu_cmd, &scsi_ret); 1686 if (ret < 0) { 1687 pr_debug("cmd %p on dev %s failed with %u\n", 1688 tcmu_cmd, udev->name, scsi_ret); 1689 /* 1690 * Ignore scsi_ret for now. target_complete_cmd 1691 * drops it. 1692 */ 1693 tcmu_cmd->se_cmd->priv = NULL; 1694 target_complete_cmd(tcmu_cmd->se_cmd, 1695 SAM_STAT_CHECK_CONDITION); 1696 tcmu_free_cmd(tcmu_cmd); 1697 } else if (ret > 0) { 1698 pr_debug("ran out of space during cmdr queue run\n"); 1699 /* 1700 * cmd was requeued, so just put all cmds back in 1701 * the queue 1702 */ 1703 list_splice_tail(&cmds, &udev->qfull_queue); 1704 break; 1705 } 1706 } 1707 1708 tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer); 1709 } 1710 1711 static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on) 1712 { 1713 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 1714 1715 mutex_lock(&udev->cmdr_lock); 1716 if (tcmu_handle_completions(udev)) 1717 run_qfull_queue(udev, false); 1718 mutex_unlock(&udev->cmdr_lock); 1719 1720 return 0; 1721 } 1722 1723 /* 1724 * mmap code from uio.c. Copied here because we want to hook mmap() 1725 * and this stuff must come along. 1726 */ 1727 static int tcmu_find_mem_index(struct vm_area_struct *vma) 1728 { 1729 struct tcmu_dev *udev = vma->vm_private_data; 1730 struct uio_info *info = &udev->uio_info; 1731 1732 if (vma->vm_pgoff < MAX_UIO_MAPS) { 1733 if (info->mem[vma->vm_pgoff].size == 0) 1734 return -1; 1735 return (int)vma->vm_pgoff; 1736 } 1737 return -1; 1738 } 1739 1740 static struct page *tcmu_try_get_block_page(struct tcmu_dev *udev, uint32_t dbi) 1741 { 1742 struct page *page; 1743 1744 mutex_lock(&udev->cmdr_lock); 1745 page = tcmu_get_block_page(udev, dbi); 1746 if (likely(page)) { 1747 mutex_unlock(&udev->cmdr_lock); 1748 return page; 1749 } 1750 1751 /* 1752 * Userspace messed up and passed in a address not in the 1753 * data iov passed to it. 1754 */ 1755 pr_err("Invalid addr to data block mapping (dbi %u) on device %s\n", 1756 dbi, udev->name); 1757 page = NULL; 1758 mutex_unlock(&udev->cmdr_lock); 1759 1760 return page; 1761 } 1762 1763 static void tcmu_vma_open(struct vm_area_struct *vma) 1764 { 1765 struct tcmu_dev *udev = vma->vm_private_data; 1766 1767 pr_debug("vma_open\n"); 1768 1769 kref_get(&udev->kref); 1770 } 1771 1772 static void tcmu_vma_close(struct vm_area_struct *vma) 1773 { 1774 struct tcmu_dev *udev = vma->vm_private_data; 1775 1776 pr_debug("vma_close\n"); 1777 1778 /* release ref from tcmu_vma_open */ 1779 kref_put(&udev->kref, tcmu_dev_kref_release); 1780 } 1781 1782 static vm_fault_t tcmu_vma_fault(struct vm_fault *vmf) 1783 { 1784 struct tcmu_dev *udev = vmf->vma->vm_private_data; 1785 struct uio_info *info = &udev->uio_info; 1786 struct page *page; 1787 unsigned long offset; 1788 void *addr; 1789 1790 int mi = tcmu_find_mem_index(vmf->vma); 1791 if (mi < 0) 1792 return VM_FAULT_SIGBUS; 1793 1794 /* 1795 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE 1796 * to use mem[N]. 1797 */ 1798 offset = (vmf->pgoff - mi) << PAGE_SHIFT; 1799 1800 if (offset < udev->data_off) { 1801 /* For the vmalloc()ed cmd area pages */ 1802 addr = (void *)(unsigned long)info->mem[mi].addr + offset; 1803 page = vmalloc_to_page(addr); 1804 } else { 1805 uint32_t dbi; 1806 1807 /* For the dynamically growing data area pages */ 1808 dbi = (offset - udev->data_off) / DATA_BLOCK_SIZE; 1809 page = tcmu_try_get_block_page(udev, dbi); 1810 if (!page) 1811 return VM_FAULT_SIGBUS; 1812 } 1813 1814 get_page(page); 1815 vmf->page = page; 1816 return 0; 1817 } 1818 1819 static const struct vm_operations_struct tcmu_vm_ops = { 1820 .open = tcmu_vma_open, 1821 .close = tcmu_vma_close, 1822 .fault = tcmu_vma_fault, 1823 }; 1824 1825 static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma) 1826 { 1827 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 1828 1829 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 1830 vma->vm_ops = &tcmu_vm_ops; 1831 1832 vma->vm_private_data = udev; 1833 1834 /* Ensure the mmap is exactly the right size */ 1835 if (vma_pages(vma) != (udev->ring_size >> PAGE_SHIFT)) 1836 return -EINVAL; 1837 1838 tcmu_vma_open(vma); 1839 1840 return 0; 1841 } 1842 1843 static int tcmu_open(struct uio_info *info, struct inode *inode) 1844 { 1845 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 1846 1847 /* O_EXCL not supported for char devs, so fake it? */ 1848 if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags)) 1849 return -EBUSY; 1850 1851 udev->inode = inode; 1852 1853 pr_debug("open\n"); 1854 1855 return 0; 1856 } 1857 1858 static int tcmu_release(struct uio_info *info, struct inode *inode) 1859 { 1860 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 1861 1862 clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags); 1863 1864 pr_debug("close\n"); 1865 1866 return 0; 1867 } 1868 1869 static int tcmu_init_genl_cmd_reply(struct tcmu_dev *udev, int cmd) 1870 { 1871 struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; 1872 1873 if (!tcmu_kern_cmd_reply_supported) 1874 return 0; 1875 1876 if (udev->nl_reply_supported <= 0) 1877 return 0; 1878 1879 mutex_lock(&tcmu_nl_cmd_mutex); 1880 1881 if (tcmu_netlink_blocked) { 1882 mutex_unlock(&tcmu_nl_cmd_mutex); 1883 pr_warn("Failing nl cmd %d on %s. Interface is blocked.\n", cmd, 1884 udev->name); 1885 return -EAGAIN; 1886 } 1887 1888 if (nl_cmd->cmd != TCMU_CMD_UNSPEC) { 1889 mutex_unlock(&tcmu_nl_cmd_mutex); 1890 pr_warn("netlink cmd %d already executing on %s\n", 1891 nl_cmd->cmd, udev->name); 1892 return -EBUSY; 1893 } 1894 1895 memset(nl_cmd, 0, sizeof(*nl_cmd)); 1896 nl_cmd->cmd = cmd; 1897 nl_cmd->udev = udev; 1898 init_completion(&nl_cmd->complete); 1899 INIT_LIST_HEAD(&nl_cmd->nl_list); 1900 1901 list_add_tail(&nl_cmd->nl_list, &tcmu_nl_cmd_list); 1902 1903 mutex_unlock(&tcmu_nl_cmd_mutex); 1904 return 0; 1905 } 1906 1907 static void tcmu_destroy_genl_cmd_reply(struct tcmu_dev *udev) 1908 { 1909 struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; 1910 1911 if (!tcmu_kern_cmd_reply_supported) 1912 return; 1913 1914 if (udev->nl_reply_supported <= 0) 1915 return; 1916 1917 mutex_lock(&tcmu_nl_cmd_mutex); 1918 1919 list_del(&nl_cmd->nl_list); 1920 memset(nl_cmd, 0, sizeof(*nl_cmd)); 1921 1922 mutex_unlock(&tcmu_nl_cmd_mutex); 1923 } 1924 1925 static int tcmu_wait_genl_cmd_reply(struct tcmu_dev *udev) 1926 { 1927 struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; 1928 int ret; 1929 1930 if (!tcmu_kern_cmd_reply_supported) 1931 return 0; 1932 1933 if (udev->nl_reply_supported <= 0) 1934 return 0; 1935 1936 pr_debug("sleeping for nl reply\n"); 1937 wait_for_completion(&nl_cmd->complete); 1938 1939 mutex_lock(&tcmu_nl_cmd_mutex); 1940 nl_cmd->cmd = TCMU_CMD_UNSPEC; 1941 ret = nl_cmd->status; 1942 mutex_unlock(&tcmu_nl_cmd_mutex); 1943 1944 return ret; 1945 } 1946 1947 static int tcmu_netlink_event_init(struct tcmu_dev *udev, 1948 enum tcmu_genl_cmd cmd, 1949 struct sk_buff **buf, void **hdr) 1950 { 1951 struct sk_buff *skb; 1952 void *msg_header; 1953 int ret = -ENOMEM; 1954 1955 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 1956 if (!skb) 1957 return ret; 1958 1959 msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd); 1960 if (!msg_header) 1961 goto free_skb; 1962 1963 ret = nla_put_string(skb, TCMU_ATTR_DEVICE, udev->uio_info.name); 1964 if (ret < 0) 1965 goto free_skb; 1966 1967 ret = nla_put_u32(skb, TCMU_ATTR_MINOR, udev->uio_info.uio_dev->minor); 1968 if (ret < 0) 1969 goto free_skb; 1970 1971 ret = nla_put_u32(skb, TCMU_ATTR_DEVICE_ID, udev->se_dev.dev_index); 1972 if (ret < 0) 1973 goto free_skb; 1974 1975 *buf = skb; 1976 *hdr = msg_header; 1977 return ret; 1978 1979 free_skb: 1980 nlmsg_free(skb); 1981 return ret; 1982 } 1983 1984 static int tcmu_netlink_event_send(struct tcmu_dev *udev, 1985 enum tcmu_genl_cmd cmd, 1986 struct sk_buff *skb, void *msg_header) 1987 { 1988 int ret; 1989 1990 genlmsg_end(skb, msg_header); 1991 1992 ret = tcmu_init_genl_cmd_reply(udev, cmd); 1993 if (ret) { 1994 nlmsg_free(skb); 1995 return ret; 1996 } 1997 1998 ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0, 1999 TCMU_MCGRP_CONFIG, GFP_KERNEL); 2000 2001 /* Wait during an add as the listener may not be up yet */ 2002 if (ret == 0 || 2003 (ret == -ESRCH && cmd == TCMU_CMD_ADDED_DEVICE)) 2004 return tcmu_wait_genl_cmd_reply(udev); 2005 else 2006 tcmu_destroy_genl_cmd_reply(udev); 2007 2008 return ret; 2009 } 2010 2011 static int tcmu_send_dev_add_event(struct tcmu_dev *udev) 2012 { 2013 struct sk_buff *skb = NULL; 2014 void *msg_header = NULL; 2015 int ret = 0; 2016 2017 ret = tcmu_netlink_event_init(udev, TCMU_CMD_ADDED_DEVICE, &skb, 2018 &msg_header); 2019 if (ret < 0) 2020 return ret; 2021 return tcmu_netlink_event_send(udev, TCMU_CMD_ADDED_DEVICE, skb, 2022 msg_header); 2023 } 2024 2025 static int tcmu_send_dev_remove_event(struct tcmu_dev *udev) 2026 { 2027 struct sk_buff *skb = NULL; 2028 void *msg_header = NULL; 2029 int ret = 0; 2030 2031 ret = tcmu_netlink_event_init(udev, TCMU_CMD_REMOVED_DEVICE, 2032 &skb, &msg_header); 2033 if (ret < 0) 2034 return ret; 2035 return tcmu_netlink_event_send(udev, TCMU_CMD_REMOVED_DEVICE, 2036 skb, msg_header); 2037 } 2038 2039 static int tcmu_update_uio_info(struct tcmu_dev *udev) 2040 { 2041 struct tcmu_hba *hba = udev->hba->hba_ptr; 2042 struct uio_info *info; 2043 char *str; 2044 2045 info = &udev->uio_info; 2046 2047 if (udev->dev_config[0]) 2048 str = kasprintf(GFP_KERNEL, "tcm-user/%u/%s/%s", hba->host_id, 2049 udev->name, udev->dev_config); 2050 else 2051 str = kasprintf(GFP_KERNEL, "tcm-user/%u/%s", hba->host_id, 2052 udev->name); 2053 if (!str) 2054 return -ENOMEM; 2055 2056 /* If the old string exists, free it */ 2057 kfree(info->name); 2058 info->name = str; 2059 2060 return 0; 2061 } 2062 2063 static int tcmu_configure_device(struct se_device *dev) 2064 { 2065 struct tcmu_dev *udev = TCMU_DEV(dev); 2066 struct uio_info *info; 2067 struct tcmu_mailbox *mb; 2068 int ret = 0; 2069 2070 ret = tcmu_update_uio_info(udev); 2071 if (ret) 2072 return ret; 2073 2074 info = &udev->uio_info; 2075 2076 mutex_lock(&udev->cmdr_lock); 2077 udev->data_bitmap = bitmap_zalloc(udev->max_blocks, GFP_KERNEL); 2078 mutex_unlock(&udev->cmdr_lock); 2079 if (!udev->data_bitmap) { 2080 ret = -ENOMEM; 2081 goto err_bitmap_alloc; 2082 } 2083 2084 udev->mb_addr = vzalloc(CMDR_SIZE); 2085 if (!udev->mb_addr) { 2086 ret = -ENOMEM; 2087 goto err_vzalloc; 2088 } 2089 2090 /* mailbox fits in first part of CMDR space */ 2091 udev->cmdr_size = CMDR_SIZE - CMDR_OFF; 2092 udev->data_off = CMDR_SIZE; 2093 udev->data_size = udev->max_blocks * DATA_BLOCK_SIZE; 2094 udev->dbi_thresh = 0; /* Default in Idle state */ 2095 2096 /* Initialise the mailbox of the ring buffer */ 2097 mb = udev->mb_addr; 2098 mb->version = TCMU_MAILBOX_VERSION; 2099 mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC | 2100 TCMU_MAILBOX_FLAG_CAP_READ_LEN | 2101 TCMU_MAILBOX_FLAG_CAP_TMR; 2102 mb->cmdr_off = CMDR_OFF; 2103 mb->cmdr_size = udev->cmdr_size; 2104 2105 WARN_ON(!PAGE_ALIGNED(udev->data_off)); 2106 WARN_ON(udev->data_size % PAGE_SIZE); 2107 WARN_ON(udev->data_size % DATA_BLOCK_SIZE); 2108 2109 info->version = __stringify(TCMU_MAILBOX_VERSION); 2110 2111 info->mem[0].name = "tcm-user command & data buffer"; 2112 info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr; 2113 info->mem[0].size = udev->ring_size = udev->data_size + CMDR_SIZE; 2114 info->mem[0].memtype = UIO_MEM_NONE; 2115 2116 info->irqcontrol = tcmu_irqcontrol; 2117 info->irq = UIO_IRQ_CUSTOM; 2118 2119 info->mmap = tcmu_mmap; 2120 info->open = tcmu_open; 2121 info->release = tcmu_release; 2122 2123 ret = uio_register_device(tcmu_root_device, info); 2124 if (ret) 2125 goto err_register; 2126 2127 /* User can set hw_block_size before enable the device */ 2128 if (dev->dev_attrib.hw_block_size == 0) 2129 dev->dev_attrib.hw_block_size = 512; 2130 /* Other attributes can be configured in userspace */ 2131 if (!dev->dev_attrib.hw_max_sectors) 2132 dev->dev_attrib.hw_max_sectors = 128; 2133 if (!dev->dev_attrib.emulate_write_cache) 2134 dev->dev_attrib.emulate_write_cache = 0; 2135 dev->dev_attrib.hw_queue_depth = 128; 2136 2137 /* If user didn't explicitly disable netlink reply support, use 2138 * module scope setting. 2139 */ 2140 if (udev->nl_reply_supported >= 0) 2141 udev->nl_reply_supported = tcmu_kern_cmd_reply_supported; 2142 2143 /* 2144 * Get a ref incase userspace does a close on the uio device before 2145 * LIO has initiated tcmu_free_device. 2146 */ 2147 kref_get(&udev->kref); 2148 2149 ret = tcmu_send_dev_add_event(udev); 2150 if (ret) 2151 goto err_netlink; 2152 2153 mutex_lock(&root_udev_mutex); 2154 list_add(&udev->node, &root_udev); 2155 mutex_unlock(&root_udev_mutex); 2156 2157 return 0; 2158 2159 err_netlink: 2160 kref_put(&udev->kref, tcmu_dev_kref_release); 2161 uio_unregister_device(&udev->uio_info); 2162 err_register: 2163 vfree(udev->mb_addr); 2164 udev->mb_addr = NULL; 2165 err_vzalloc: 2166 bitmap_free(udev->data_bitmap); 2167 udev->data_bitmap = NULL; 2168 err_bitmap_alloc: 2169 kfree(info->name); 2170 info->name = NULL; 2171 2172 return ret; 2173 } 2174 2175 static void tcmu_free_device(struct se_device *dev) 2176 { 2177 struct tcmu_dev *udev = TCMU_DEV(dev); 2178 2179 /* release ref from init */ 2180 kref_put(&udev->kref, tcmu_dev_kref_release); 2181 } 2182 2183 static void tcmu_destroy_device(struct se_device *dev) 2184 { 2185 struct tcmu_dev *udev = TCMU_DEV(dev); 2186 2187 del_timer_sync(&udev->cmd_timer); 2188 del_timer_sync(&udev->qfull_timer); 2189 2190 mutex_lock(&root_udev_mutex); 2191 list_del(&udev->node); 2192 mutex_unlock(&root_udev_mutex); 2193 2194 tcmu_send_dev_remove_event(udev); 2195 2196 uio_unregister_device(&udev->uio_info); 2197 2198 /* release ref from configure */ 2199 kref_put(&udev->kref, tcmu_dev_kref_release); 2200 } 2201 2202 static void tcmu_unblock_dev(struct tcmu_dev *udev) 2203 { 2204 mutex_lock(&udev->cmdr_lock); 2205 clear_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags); 2206 mutex_unlock(&udev->cmdr_lock); 2207 } 2208 2209 static void tcmu_block_dev(struct tcmu_dev *udev) 2210 { 2211 mutex_lock(&udev->cmdr_lock); 2212 2213 if (test_and_set_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) 2214 goto unlock; 2215 2216 /* complete IO that has executed successfully */ 2217 tcmu_handle_completions(udev); 2218 /* fail IO waiting to be queued */ 2219 run_qfull_queue(udev, true); 2220 2221 unlock: 2222 mutex_unlock(&udev->cmdr_lock); 2223 } 2224 2225 static void tcmu_reset_ring(struct tcmu_dev *udev, u8 err_level) 2226 { 2227 struct tcmu_mailbox *mb; 2228 struct tcmu_cmd *cmd; 2229 int i; 2230 2231 mutex_lock(&udev->cmdr_lock); 2232 2233 idr_for_each_entry(&udev->commands, cmd, i) { 2234 pr_debug("removing cmd %u on dev %s from ring (is expired %d)\n", 2235 cmd->cmd_id, udev->name, 2236 test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)); 2237 2238 idr_remove(&udev->commands, i); 2239 if (!test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { 2240 WARN_ON(!cmd->se_cmd); 2241 list_del_init(&cmd->queue_entry); 2242 cmd->se_cmd->priv = NULL; 2243 if (err_level == 1) { 2244 /* 2245 * Userspace was not able to start the 2246 * command or it is retryable. 2247 */ 2248 target_complete_cmd(cmd->se_cmd, SAM_STAT_BUSY); 2249 } else { 2250 /* hard failure */ 2251 target_complete_cmd(cmd->se_cmd, 2252 SAM_STAT_CHECK_CONDITION); 2253 } 2254 } 2255 tcmu_cmd_free_data(cmd, cmd->dbi_cnt); 2256 tcmu_free_cmd(cmd); 2257 } 2258 2259 mb = udev->mb_addr; 2260 tcmu_flush_dcache_range(mb, sizeof(*mb)); 2261 pr_debug("mb last %u head %u tail %u\n", udev->cmdr_last_cleaned, 2262 mb->cmd_tail, mb->cmd_head); 2263 2264 udev->cmdr_last_cleaned = 0; 2265 mb->cmd_tail = 0; 2266 mb->cmd_head = 0; 2267 tcmu_flush_dcache_range(mb, sizeof(*mb)); 2268 clear_bit(TCMU_DEV_BIT_BROKEN, &udev->flags); 2269 2270 del_timer(&udev->cmd_timer); 2271 2272 /* 2273 * ring is empty and qfull queue never contains aborted commands. 2274 * So TMRs in tmr queue do not contain relevant cmd_ids. 2275 * After a ring reset userspace should do a fresh start, so 2276 * even LUN RESET message is no longer relevant. 2277 * Therefore remove all TMRs from qfull queue 2278 */ 2279 tcmu_remove_all_queued_tmr(udev); 2280 2281 run_qfull_queue(udev, false); 2282 2283 mutex_unlock(&udev->cmdr_lock); 2284 } 2285 2286 enum { 2287 Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors, 2288 Opt_nl_reply_supported, Opt_max_data_area_mb, Opt_err, 2289 }; 2290 2291 static match_table_t tokens = { 2292 {Opt_dev_config, "dev_config=%s"}, 2293 {Opt_dev_size, "dev_size=%s"}, 2294 {Opt_hw_block_size, "hw_block_size=%d"}, 2295 {Opt_hw_max_sectors, "hw_max_sectors=%d"}, 2296 {Opt_nl_reply_supported, "nl_reply_supported=%d"}, 2297 {Opt_max_data_area_mb, "max_data_area_mb=%d"}, 2298 {Opt_err, NULL} 2299 }; 2300 2301 static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib) 2302 { 2303 int val, ret; 2304 2305 ret = match_int(arg, &val); 2306 if (ret < 0) { 2307 pr_err("match_int() failed for dev attrib. Error %d.\n", 2308 ret); 2309 return ret; 2310 } 2311 2312 if (val <= 0) { 2313 pr_err("Invalid dev attrib value %d. Must be greater than zero.\n", 2314 val); 2315 return -EINVAL; 2316 } 2317 *dev_attrib = val; 2318 return 0; 2319 } 2320 2321 static int tcmu_set_max_blocks_param(struct tcmu_dev *udev, substring_t *arg) 2322 { 2323 int val, ret; 2324 2325 ret = match_int(arg, &val); 2326 if (ret < 0) { 2327 pr_err("match_int() failed for max_data_area_mb=. Error %d.\n", 2328 ret); 2329 return ret; 2330 } 2331 2332 if (val <= 0) { 2333 pr_err("Invalid max_data_area %d.\n", val); 2334 return -EINVAL; 2335 } 2336 2337 mutex_lock(&udev->cmdr_lock); 2338 if (udev->data_bitmap) { 2339 pr_err("Cannot set max_data_area_mb after it has been enabled.\n"); 2340 ret = -EINVAL; 2341 goto unlock; 2342 } 2343 2344 udev->max_blocks = TCMU_MBS_TO_BLOCKS(val); 2345 if (udev->max_blocks > tcmu_global_max_blocks) { 2346 pr_err("%d is too large. Adjusting max_data_area_mb to global limit of %u\n", 2347 val, TCMU_BLOCKS_TO_MBS(tcmu_global_max_blocks)); 2348 udev->max_blocks = tcmu_global_max_blocks; 2349 } 2350 2351 unlock: 2352 mutex_unlock(&udev->cmdr_lock); 2353 return ret; 2354 } 2355 2356 static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev, 2357 const char *page, ssize_t count) 2358 { 2359 struct tcmu_dev *udev = TCMU_DEV(dev); 2360 char *orig, *ptr, *opts; 2361 substring_t args[MAX_OPT_ARGS]; 2362 int ret = 0, token; 2363 2364 opts = kstrdup(page, GFP_KERNEL); 2365 if (!opts) 2366 return -ENOMEM; 2367 2368 orig = opts; 2369 2370 while ((ptr = strsep(&opts, ",\n")) != NULL) { 2371 if (!*ptr) 2372 continue; 2373 2374 token = match_token(ptr, tokens, args); 2375 switch (token) { 2376 case Opt_dev_config: 2377 if (match_strlcpy(udev->dev_config, &args[0], 2378 TCMU_CONFIG_LEN) == 0) { 2379 ret = -EINVAL; 2380 break; 2381 } 2382 pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config); 2383 break; 2384 case Opt_dev_size: 2385 ret = match_u64(&args[0], &udev->dev_size); 2386 if (ret < 0) 2387 pr_err("match_u64() failed for dev_size=. Error %d.\n", 2388 ret); 2389 break; 2390 case Opt_hw_block_size: 2391 ret = tcmu_set_dev_attrib(&args[0], 2392 &(dev->dev_attrib.hw_block_size)); 2393 break; 2394 case Opt_hw_max_sectors: 2395 ret = tcmu_set_dev_attrib(&args[0], 2396 &(dev->dev_attrib.hw_max_sectors)); 2397 break; 2398 case Opt_nl_reply_supported: 2399 ret = match_int(&args[0], &udev->nl_reply_supported); 2400 if (ret < 0) 2401 pr_err("match_int() failed for nl_reply_supported=. Error %d.\n", 2402 ret); 2403 break; 2404 case Opt_max_data_area_mb: 2405 ret = tcmu_set_max_blocks_param(udev, &args[0]); 2406 break; 2407 default: 2408 break; 2409 } 2410 2411 if (ret) 2412 break; 2413 } 2414 2415 kfree(orig); 2416 return (!ret) ? count : ret; 2417 } 2418 2419 static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b) 2420 { 2421 struct tcmu_dev *udev = TCMU_DEV(dev); 2422 ssize_t bl = 0; 2423 2424 bl = sprintf(b + bl, "Config: %s ", 2425 udev->dev_config[0] ? udev->dev_config : "NULL"); 2426 bl += sprintf(b + bl, "Size: %llu ", udev->dev_size); 2427 bl += sprintf(b + bl, "MaxDataAreaMB: %u\n", 2428 TCMU_BLOCKS_TO_MBS(udev->max_blocks)); 2429 2430 return bl; 2431 } 2432 2433 static sector_t tcmu_get_blocks(struct se_device *dev) 2434 { 2435 struct tcmu_dev *udev = TCMU_DEV(dev); 2436 2437 return div_u64(udev->dev_size - dev->dev_attrib.block_size, 2438 dev->dev_attrib.block_size); 2439 } 2440 2441 static sense_reason_t 2442 tcmu_parse_cdb(struct se_cmd *cmd) 2443 { 2444 return passthrough_parse_cdb(cmd, tcmu_queue_cmd); 2445 } 2446 2447 static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page) 2448 { 2449 struct se_dev_attrib *da = container_of(to_config_group(item), 2450 struct se_dev_attrib, da_group); 2451 struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2452 2453 return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC); 2454 } 2455 2456 static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page, 2457 size_t count) 2458 { 2459 struct se_dev_attrib *da = container_of(to_config_group(item), 2460 struct se_dev_attrib, da_group); 2461 struct tcmu_dev *udev = container_of(da->da_dev, 2462 struct tcmu_dev, se_dev); 2463 u32 val; 2464 int ret; 2465 2466 if (da->da_dev->export_count) { 2467 pr_err("Unable to set tcmu cmd_time_out while exports exist\n"); 2468 return -EINVAL; 2469 } 2470 2471 ret = kstrtou32(page, 0, &val); 2472 if (ret < 0) 2473 return ret; 2474 2475 udev->cmd_time_out = val * MSEC_PER_SEC; 2476 return count; 2477 } 2478 CONFIGFS_ATTR(tcmu_, cmd_time_out); 2479 2480 static ssize_t tcmu_qfull_time_out_show(struct config_item *item, char *page) 2481 { 2482 struct se_dev_attrib *da = container_of(to_config_group(item), 2483 struct se_dev_attrib, da_group); 2484 struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2485 2486 return snprintf(page, PAGE_SIZE, "%ld\n", udev->qfull_time_out <= 0 ? 2487 udev->qfull_time_out : 2488 udev->qfull_time_out / MSEC_PER_SEC); 2489 } 2490 2491 static ssize_t tcmu_qfull_time_out_store(struct config_item *item, 2492 const char *page, size_t count) 2493 { 2494 struct se_dev_attrib *da = container_of(to_config_group(item), 2495 struct se_dev_attrib, da_group); 2496 struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2497 s32 val; 2498 int ret; 2499 2500 ret = kstrtos32(page, 0, &val); 2501 if (ret < 0) 2502 return ret; 2503 2504 if (val >= 0) { 2505 udev->qfull_time_out = val * MSEC_PER_SEC; 2506 } else if (val == -1) { 2507 udev->qfull_time_out = val; 2508 } else { 2509 printk(KERN_ERR "Invalid qfull timeout value %d\n", val); 2510 return -EINVAL; 2511 } 2512 return count; 2513 } 2514 CONFIGFS_ATTR(tcmu_, qfull_time_out); 2515 2516 static ssize_t tcmu_max_data_area_mb_show(struct config_item *item, char *page) 2517 { 2518 struct se_dev_attrib *da = container_of(to_config_group(item), 2519 struct se_dev_attrib, da_group); 2520 struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2521 2522 return snprintf(page, PAGE_SIZE, "%u\n", 2523 TCMU_BLOCKS_TO_MBS(udev->max_blocks)); 2524 } 2525 CONFIGFS_ATTR_RO(tcmu_, max_data_area_mb); 2526 2527 static ssize_t tcmu_dev_config_show(struct config_item *item, char *page) 2528 { 2529 struct se_dev_attrib *da = container_of(to_config_group(item), 2530 struct se_dev_attrib, da_group); 2531 struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2532 2533 return snprintf(page, PAGE_SIZE, "%s\n", udev->dev_config); 2534 } 2535 2536 static int tcmu_send_dev_config_event(struct tcmu_dev *udev, 2537 const char *reconfig_data) 2538 { 2539 struct sk_buff *skb = NULL; 2540 void *msg_header = NULL; 2541 int ret = 0; 2542 2543 ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE, 2544 &skb, &msg_header); 2545 if (ret < 0) 2546 return ret; 2547 ret = nla_put_string(skb, TCMU_ATTR_DEV_CFG, reconfig_data); 2548 if (ret < 0) { 2549 nlmsg_free(skb); 2550 return ret; 2551 } 2552 return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE, 2553 skb, msg_header); 2554 } 2555 2556 2557 static ssize_t tcmu_dev_config_store(struct config_item *item, const char *page, 2558 size_t count) 2559 { 2560 struct se_dev_attrib *da = container_of(to_config_group(item), 2561 struct se_dev_attrib, da_group); 2562 struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2563 int ret, len; 2564 2565 len = strlen(page); 2566 if (!len || len > TCMU_CONFIG_LEN - 1) 2567 return -EINVAL; 2568 2569 /* Check if device has been configured before */ 2570 if (target_dev_configured(&udev->se_dev)) { 2571 ret = tcmu_send_dev_config_event(udev, page); 2572 if (ret) { 2573 pr_err("Unable to reconfigure device\n"); 2574 return ret; 2575 } 2576 strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN); 2577 2578 ret = tcmu_update_uio_info(udev); 2579 if (ret) 2580 return ret; 2581 return count; 2582 } 2583 strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN); 2584 2585 return count; 2586 } 2587 CONFIGFS_ATTR(tcmu_, dev_config); 2588 2589 static ssize_t tcmu_dev_size_show(struct config_item *item, char *page) 2590 { 2591 struct se_dev_attrib *da = container_of(to_config_group(item), 2592 struct se_dev_attrib, da_group); 2593 struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2594 2595 return snprintf(page, PAGE_SIZE, "%llu\n", udev->dev_size); 2596 } 2597 2598 static int tcmu_send_dev_size_event(struct tcmu_dev *udev, u64 size) 2599 { 2600 struct sk_buff *skb = NULL; 2601 void *msg_header = NULL; 2602 int ret = 0; 2603 2604 ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE, 2605 &skb, &msg_header); 2606 if (ret < 0) 2607 return ret; 2608 ret = nla_put_u64_64bit(skb, TCMU_ATTR_DEV_SIZE, 2609 size, TCMU_ATTR_PAD); 2610 if (ret < 0) { 2611 nlmsg_free(skb); 2612 return ret; 2613 } 2614 return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE, 2615 skb, msg_header); 2616 } 2617 2618 static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page, 2619 size_t count) 2620 { 2621 struct se_dev_attrib *da = container_of(to_config_group(item), 2622 struct se_dev_attrib, da_group); 2623 struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2624 u64 val; 2625 int ret; 2626 2627 ret = kstrtou64(page, 0, &val); 2628 if (ret < 0) 2629 return ret; 2630 2631 /* Check if device has been configured before */ 2632 if (target_dev_configured(&udev->se_dev)) { 2633 ret = tcmu_send_dev_size_event(udev, val); 2634 if (ret) { 2635 pr_err("Unable to reconfigure device\n"); 2636 return ret; 2637 } 2638 } 2639 udev->dev_size = val; 2640 return count; 2641 } 2642 CONFIGFS_ATTR(tcmu_, dev_size); 2643 2644 static ssize_t tcmu_nl_reply_supported_show(struct config_item *item, 2645 char *page) 2646 { 2647 struct se_dev_attrib *da = container_of(to_config_group(item), 2648 struct se_dev_attrib, da_group); 2649 struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2650 2651 return snprintf(page, PAGE_SIZE, "%d\n", udev->nl_reply_supported); 2652 } 2653 2654 static ssize_t tcmu_nl_reply_supported_store(struct config_item *item, 2655 const char *page, size_t count) 2656 { 2657 struct se_dev_attrib *da = container_of(to_config_group(item), 2658 struct se_dev_attrib, da_group); 2659 struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2660 s8 val; 2661 int ret; 2662 2663 ret = kstrtos8(page, 0, &val); 2664 if (ret < 0) 2665 return ret; 2666 2667 udev->nl_reply_supported = val; 2668 return count; 2669 } 2670 CONFIGFS_ATTR(tcmu_, nl_reply_supported); 2671 2672 static ssize_t tcmu_emulate_write_cache_show(struct config_item *item, 2673 char *page) 2674 { 2675 struct se_dev_attrib *da = container_of(to_config_group(item), 2676 struct se_dev_attrib, da_group); 2677 2678 return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache); 2679 } 2680 2681 static int tcmu_send_emulate_write_cache(struct tcmu_dev *udev, u8 val) 2682 { 2683 struct sk_buff *skb = NULL; 2684 void *msg_header = NULL; 2685 int ret = 0; 2686 2687 ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE, 2688 &skb, &msg_header); 2689 if (ret < 0) 2690 return ret; 2691 ret = nla_put_u8(skb, TCMU_ATTR_WRITECACHE, val); 2692 if (ret < 0) { 2693 nlmsg_free(skb); 2694 return ret; 2695 } 2696 return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE, 2697 skb, msg_header); 2698 } 2699 2700 static ssize_t tcmu_emulate_write_cache_store(struct config_item *item, 2701 const char *page, size_t count) 2702 { 2703 struct se_dev_attrib *da = container_of(to_config_group(item), 2704 struct se_dev_attrib, da_group); 2705 struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2706 u8 val; 2707 int ret; 2708 2709 ret = kstrtou8(page, 0, &val); 2710 if (ret < 0) 2711 return ret; 2712 2713 /* Check if device has been configured before */ 2714 if (target_dev_configured(&udev->se_dev)) { 2715 ret = tcmu_send_emulate_write_cache(udev, val); 2716 if (ret) { 2717 pr_err("Unable to reconfigure device\n"); 2718 return ret; 2719 } 2720 } 2721 2722 da->emulate_write_cache = val; 2723 return count; 2724 } 2725 CONFIGFS_ATTR(tcmu_, emulate_write_cache); 2726 2727 static ssize_t tcmu_tmr_notification_show(struct config_item *item, char *page) 2728 { 2729 struct se_dev_attrib *da = container_of(to_config_group(item), 2730 struct se_dev_attrib, da_group); 2731 struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2732 2733 return snprintf(page, PAGE_SIZE, "%i\n", 2734 test_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags)); 2735 } 2736 2737 static ssize_t tcmu_tmr_notification_store(struct config_item *item, 2738 const char *page, size_t count) 2739 { 2740 struct se_dev_attrib *da = container_of(to_config_group(item), 2741 struct se_dev_attrib, da_group); 2742 struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2743 u8 val; 2744 int ret; 2745 2746 ret = kstrtou8(page, 0, &val); 2747 if (ret < 0) 2748 return ret; 2749 if (val > 1) 2750 return -EINVAL; 2751 2752 if (val) 2753 set_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags); 2754 else 2755 clear_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags); 2756 return count; 2757 } 2758 CONFIGFS_ATTR(tcmu_, tmr_notification); 2759 2760 static ssize_t tcmu_block_dev_show(struct config_item *item, char *page) 2761 { 2762 struct se_device *se_dev = container_of(to_config_group(item), 2763 struct se_device, 2764 dev_action_group); 2765 struct tcmu_dev *udev = TCMU_DEV(se_dev); 2766 2767 if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) 2768 return snprintf(page, PAGE_SIZE, "%s\n", "blocked"); 2769 else 2770 return snprintf(page, PAGE_SIZE, "%s\n", "unblocked"); 2771 } 2772 2773 static ssize_t tcmu_block_dev_store(struct config_item *item, const char *page, 2774 size_t count) 2775 { 2776 struct se_device *se_dev = container_of(to_config_group(item), 2777 struct se_device, 2778 dev_action_group); 2779 struct tcmu_dev *udev = TCMU_DEV(se_dev); 2780 u8 val; 2781 int ret; 2782 2783 if (!target_dev_configured(&udev->se_dev)) { 2784 pr_err("Device is not configured.\n"); 2785 return -EINVAL; 2786 } 2787 2788 ret = kstrtou8(page, 0, &val); 2789 if (ret < 0) 2790 return ret; 2791 2792 if (val > 1) { 2793 pr_err("Invalid block value %d\n", val); 2794 return -EINVAL; 2795 } 2796 2797 if (!val) 2798 tcmu_unblock_dev(udev); 2799 else 2800 tcmu_block_dev(udev); 2801 return count; 2802 } 2803 CONFIGFS_ATTR(tcmu_, block_dev); 2804 2805 static ssize_t tcmu_reset_ring_store(struct config_item *item, const char *page, 2806 size_t count) 2807 { 2808 struct se_device *se_dev = container_of(to_config_group(item), 2809 struct se_device, 2810 dev_action_group); 2811 struct tcmu_dev *udev = TCMU_DEV(se_dev); 2812 u8 val; 2813 int ret; 2814 2815 if (!target_dev_configured(&udev->se_dev)) { 2816 pr_err("Device is not configured.\n"); 2817 return -EINVAL; 2818 } 2819 2820 ret = kstrtou8(page, 0, &val); 2821 if (ret < 0) 2822 return ret; 2823 2824 if (val != 1 && val != 2) { 2825 pr_err("Invalid reset ring value %d\n", val); 2826 return -EINVAL; 2827 } 2828 2829 tcmu_reset_ring(udev, val); 2830 return count; 2831 } 2832 CONFIGFS_ATTR_WO(tcmu_, reset_ring); 2833 2834 static struct configfs_attribute *tcmu_attrib_attrs[] = { 2835 &tcmu_attr_cmd_time_out, 2836 &tcmu_attr_qfull_time_out, 2837 &tcmu_attr_max_data_area_mb, 2838 &tcmu_attr_dev_config, 2839 &tcmu_attr_dev_size, 2840 &tcmu_attr_emulate_write_cache, 2841 &tcmu_attr_tmr_notification, 2842 &tcmu_attr_nl_reply_supported, 2843 NULL, 2844 }; 2845 2846 static struct configfs_attribute **tcmu_attrs; 2847 2848 static struct configfs_attribute *tcmu_action_attrs[] = { 2849 &tcmu_attr_block_dev, 2850 &tcmu_attr_reset_ring, 2851 NULL, 2852 }; 2853 2854 static struct target_backend_ops tcmu_ops = { 2855 .name = "user", 2856 .owner = THIS_MODULE, 2857 .transport_flags_default = TRANSPORT_FLAG_PASSTHROUGH, 2858 .transport_flags_changeable = TRANSPORT_FLAG_PASSTHROUGH_PGR | 2859 TRANSPORT_FLAG_PASSTHROUGH_ALUA, 2860 .attach_hba = tcmu_attach_hba, 2861 .detach_hba = tcmu_detach_hba, 2862 .alloc_device = tcmu_alloc_device, 2863 .configure_device = tcmu_configure_device, 2864 .destroy_device = tcmu_destroy_device, 2865 .free_device = tcmu_free_device, 2866 .parse_cdb = tcmu_parse_cdb, 2867 .tmr_notify = tcmu_tmr_notify, 2868 .set_configfs_dev_params = tcmu_set_configfs_dev_params, 2869 .show_configfs_dev_params = tcmu_show_configfs_dev_params, 2870 .get_device_type = sbc_get_device_type, 2871 .get_blocks = tcmu_get_blocks, 2872 .tb_dev_action_attrs = tcmu_action_attrs, 2873 }; 2874 2875 static void find_free_blocks(void) 2876 { 2877 struct tcmu_dev *udev; 2878 loff_t off; 2879 u32 start, end, block, total_freed = 0; 2880 2881 if (atomic_read(&global_db_count) <= tcmu_global_max_blocks) 2882 return; 2883 2884 mutex_lock(&root_udev_mutex); 2885 list_for_each_entry(udev, &root_udev, node) { 2886 mutex_lock(&udev->cmdr_lock); 2887 2888 if (!target_dev_configured(&udev->se_dev)) { 2889 mutex_unlock(&udev->cmdr_lock); 2890 continue; 2891 } 2892 2893 /* Try to complete the finished commands first */ 2894 if (tcmu_handle_completions(udev)) 2895 run_qfull_queue(udev, false); 2896 2897 /* Skip the udevs in idle */ 2898 if (!udev->dbi_thresh) { 2899 mutex_unlock(&udev->cmdr_lock); 2900 continue; 2901 } 2902 2903 end = udev->dbi_max + 1; 2904 block = find_last_bit(udev->data_bitmap, end); 2905 if (block == udev->dbi_max) { 2906 /* 2907 * The last bit is dbi_max, so it is not possible 2908 * reclaim any blocks. 2909 */ 2910 mutex_unlock(&udev->cmdr_lock); 2911 continue; 2912 } else if (block == end) { 2913 /* The current udev will goto idle state */ 2914 udev->dbi_thresh = start = 0; 2915 udev->dbi_max = 0; 2916 } else { 2917 udev->dbi_thresh = start = block + 1; 2918 udev->dbi_max = block; 2919 } 2920 2921 /* Here will truncate the data area from off */ 2922 off = udev->data_off + start * DATA_BLOCK_SIZE; 2923 unmap_mapping_range(udev->inode->i_mapping, off, 0, 1); 2924 2925 /* Release the block pages */ 2926 tcmu_blocks_release(&udev->data_blocks, start, end); 2927 mutex_unlock(&udev->cmdr_lock); 2928 2929 total_freed += end - start; 2930 pr_debug("Freed %u blocks (total %u) from %s.\n", end - start, 2931 total_freed, udev->name); 2932 } 2933 mutex_unlock(&root_udev_mutex); 2934 2935 if (atomic_read(&global_db_count) > tcmu_global_max_blocks) 2936 schedule_delayed_work(&tcmu_unmap_work, msecs_to_jiffies(5000)); 2937 } 2938 2939 static void check_timedout_devices(void) 2940 { 2941 struct tcmu_dev *udev, *tmp_dev; 2942 struct tcmu_cmd *cmd, *tmp_cmd; 2943 LIST_HEAD(devs); 2944 2945 spin_lock_bh(&timed_out_udevs_lock); 2946 list_splice_init(&timed_out_udevs, &devs); 2947 2948 list_for_each_entry_safe(udev, tmp_dev, &devs, timedout_entry) { 2949 list_del_init(&udev->timedout_entry); 2950 spin_unlock_bh(&timed_out_udevs_lock); 2951 2952 mutex_lock(&udev->cmdr_lock); 2953 2954 /* 2955 * If cmd_time_out is disabled but qfull is set deadline 2956 * will only reflect the qfull timeout. Ignore it. 2957 */ 2958 if (udev->cmd_time_out) { 2959 list_for_each_entry_safe(cmd, tmp_cmd, 2960 &udev->inflight_queue, 2961 queue_entry) { 2962 tcmu_check_expired_ring_cmd(cmd); 2963 } 2964 tcmu_set_next_deadline(&udev->inflight_queue, 2965 &udev->cmd_timer); 2966 } 2967 list_for_each_entry_safe(cmd, tmp_cmd, &udev->qfull_queue, 2968 queue_entry) { 2969 tcmu_check_expired_queue_cmd(cmd); 2970 } 2971 tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer); 2972 2973 mutex_unlock(&udev->cmdr_lock); 2974 2975 spin_lock_bh(&timed_out_udevs_lock); 2976 } 2977 2978 spin_unlock_bh(&timed_out_udevs_lock); 2979 } 2980 2981 static void tcmu_unmap_work_fn(struct work_struct *work) 2982 { 2983 check_timedout_devices(); 2984 find_free_blocks(); 2985 } 2986 2987 static int __init tcmu_module_init(void) 2988 { 2989 int ret, i, k, len = 0; 2990 2991 BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0); 2992 2993 INIT_DELAYED_WORK(&tcmu_unmap_work, tcmu_unmap_work_fn); 2994 2995 tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache", 2996 sizeof(struct tcmu_cmd), 2997 __alignof__(struct tcmu_cmd), 2998 0, NULL); 2999 if (!tcmu_cmd_cache) 3000 return -ENOMEM; 3001 3002 tcmu_root_device = root_device_register("tcm_user"); 3003 if (IS_ERR(tcmu_root_device)) { 3004 ret = PTR_ERR(tcmu_root_device); 3005 goto out_free_cache; 3006 } 3007 3008 ret = genl_register_family(&tcmu_genl_family); 3009 if (ret < 0) { 3010 goto out_unreg_device; 3011 } 3012 3013 for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) 3014 len += sizeof(struct configfs_attribute *); 3015 for (i = 0; passthrough_pr_attrib_attrs[i] != NULL; i++) 3016 len += sizeof(struct configfs_attribute *); 3017 for (i = 0; tcmu_attrib_attrs[i] != NULL; i++) 3018 len += sizeof(struct configfs_attribute *); 3019 len += sizeof(struct configfs_attribute *); 3020 3021 tcmu_attrs = kzalloc(len, GFP_KERNEL); 3022 if (!tcmu_attrs) { 3023 ret = -ENOMEM; 3024 goto out_unreg_genl; 3025 } 3026 3027 for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) 3028 tcmu_attrs[i] = passthrough_attrib_attrs[i]; 3029 for (k = 0; passthrough_pr_attrib_attrs[k] != NULL; k++) 3030 tcmu_attrs[i++] = passthrough_pr_attrib_attrs[k]; 3031 for (k = 0; tcmu_attrib_attrs[k] != NULL; k++) 3032 tcmu_attrs[i++] = tcmu_attrib_attrs[k]; 3033 tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs; 3034 3035 ret = transport_backend_register(&tcmu_ops); 3036 if (ret) 3037 goto out_attrs; 3038 3039 return 0; 3040 3041 out_attrs: 3042 kfree(tcmu_attrs); 3043 out_unreg_genl: 3044 genl_unregister_family(&tcmu_genl_family); 3045 out_unreg_device: 3046 root_device_unregister(tcmu_root_device); 3047 out_free_cache: 3048 kmem_cache_destroy(tcmu_cmd_cache); 3049 3050 return ret; 3051 } 3052 3053 static void __exit tcmu_module_exit(void) 3054 { 3055 cancel_delayed_work_sync(&tcmu_unmap_work); 3056 target_backend_unregister(&tcmu_ops); 3057 kfree(tcmu_attrs); 3058 genl_unregister_family(&tcmu_genl_family); 3059 root_device_unregister(tcmu_root_device); 3060 kmem_cache_destroy(tcmu_cmd_cache); 3061 } 3062 3063 MODULE_DESCRIPTION("TCM USER subsystem plugin"); 3064 MODULE_AUTHOR("Shaohua Li <shli@kernel.org>"); 3065 MODULE_AUTHOR("Andy Grover <agrover@redhat.com>"); 3066 MODULE_LICENSE("GPL"); 3067 3068 module_init(tcmu_module_init); 3069 module_exit(tcmu_module_exit); 3070