17c9e7a6fSAndy Grover /* 27c9e7a6fSAndy Grover * Copyright (C) 2013 Shaohua Li <shli@kernel.org> 37c9e7a6fSAndy Grover * Copyright (C) 2014 Red Hat, Inc. 4f97ec7dbSIlias Tsitsimpis * Copyright (C) 2015 Arrikto, Inc. 5141685a3SXiubo Li * Copyright (C) 2017 Chinamobile, Inc. 67c9e7a6fSAndy Grover * 77c9e7a6fSAndy Grover * This program is free software; you can redistribute it and/or modify it 87c9e7a6fSAndy Grover * under the terms and conditions of the GNU General Public License, 97c9e7a6fSAndy Grover * version 2, as published by the Free Software Foundation. 107c9e7a6fSAndy Grover * 117c9e7a6fSAndy Grover * This program is distributed in the hope it will be useful, but WITHOUT 127c9e7a6fSAndy Grover * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 137c9e7a6fSAndy Grover * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 147c9e7a6fSAndy Grover * more details. 157c9e7a6fSAndy Grover * 167c9e7a6fSAndy Grover * You should have received a copy of the GNU General Public License along with 177c9e7a6fSAndy Grover * this program; if not, write to the Free Software Foundation, Inc., 187c9e7a6fSAndy Grover * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 197c9e7a6fSAndy Grover */ 207c9e7a6fSAndy Grover 217c9e7a6fSAndy Grover #include <linux/spinlock.h> 227c9e7a6fSAndy Grover #include <linux/module.h> 237c9e7a6fSAndy Grover #include <linux/idr.h> 24ba929992SBart Van Assche #include <linux/kernel.h> 257c9e7a6fSAndy Grover #include <linux/timer.h> 267c9e7a6fSAndy Grover #include <linux/parser.h> 275538d294SDavid S. Miller #include <linux/vmalloc.h> 287c9e7a6fSAndy Grover #include <linux/uio_driver.h> 29141685a3SXiubo Li #include <linux/radix-tree.h> 30ac64a2ceSDavid Disseldorp #include <linux/stringify.h> 3126418649SSheng Yang #include <linux/bitops.h> 32f5045724SBart Van Assche #include <linux/highmem.h> 337d7a7435SNicholas Bellinger #include <linux/configfs.h> 34b6df4b79SXiubo Li #include <linux/mutex.h> 35b6df4b79SXiubo Li #include <linux/kthread.h> 367c9e7a6fSAndy Grover #include <net/genetlink.h> 37ba929992SBart Van Assche #include <scsi/scsi_common.h> 38ba929992SBart Van Assche #include <scsi/scsi_proto.h> 397c9e7a6fSAndy Grover #include <target/target_core_base.h> 407c9e7a6fSAndy Grover #include <target/target_core_fabric.h> 417c9e7a6fSAndy Grover #include <target/target_core_backend.h> 42e9f720d6SNicholas Bellinger 437c9e7a6fSAndy Grover #include <linux/target_core_user.h> 447c9e7a6fSAndy Grover 457c9e7a6fSAndy Grover /* 467c9e7a6fSAndy Grover * Define a shared-memory interface for LIO to pass SCSI commands and 477c9e7a6fSAndy Grover * data to userspace for processing. This is to allow backends that 487c9e7a6fSAndy Grover * are too complex for in-kernel support to be possible. 497c9e7a6fSAndy Grover * 507c9e7a6fSAndy Grover * It uses the UIO framework to do a lot of the device-creation and 517c9e7a6fSAndy Grover * introspection work for us. 527c9e7a6fSAndy Grover * 537c9e7a6fSAndy Grover * See the .h file for how the ring is laid out. Note that while the 547c9e7a6fSAndy Grover * command ring is defined, the particulars of the data area are 557c9e7a6fSAndy Grover * not. Offset values in the command entry point to other locations 567c9e7a6fSAndy Grover * internal to the mmap()ed area. There is separate space outside the 577c9e7a6fSAndy Grover * command ring for data buffers. This leaves maximum flexibility for 587c9e7a6fSAndy Grover * moving buffer allocations, or even page flipping or other 597c9e7a6fSAndy Grover * allocation techniques, without altering the command ring layout. 607c9e7a6fSAndy Grover * 617c9e7a6fSAndy Grover * SECURITY: 627c9e7a6fSAndy Grover * The user process must be assumed to be malicious. There's no way to 637c9e7a6fSAndy Grover * prevent it breaking the command ring protocol if it wants, but in 647c9e7a6fSAndy Grover * order to prevent other issues we must only ever read *data* from 657c9e7a6fSAndy Grover * the shared memory area, not offsets or sizes. This applies to 667c9e7a6fSAndy Grover * command ring entries as well as the mailbox. Extra code needed for 677c9e7a6fSAndy Grover * this may have a 'UAM' comment. 687c9e7a6fSAndy Grover */ 697c9e7a6fSAndy Grover 707c9e7a6fSAndy Grover #define TCMU_TIME_OUT (30 * MSEC_PER_SEC) 717c9e7a6fSAndy Grover 72b6df4b79SXiubo Li /* For cmd area, the size is fixed 8MB */ 73b6df4b79SXiubo Li #define CMDR_SIZE (8 * 1024 * 1024) 7426418649SSheng Yang 75b6df4b79SXiubo Li /* 76b6df4b79SXiubo Li * For data area, the block size is PAGE_SIZE and 77b6df4b79SXiubo Li * the total size is 256K * PAGE_SIZE. 78b6df4b79SXiubo Li */ 79b6df4b79SXiubo Li #define DATA_BLOCK_SIZE PAGE_SIZE 80b6df4b79SXiubo Li #define DATA_BLOCK_BITS (256 * 1024) 8126418649SSheng Yang #define DATA_SIZE (DATA_BLOCK_BITS * DATA_BLOCK_SIZE) 82b6df4b79SXiubo Li #define DATA_BLOCK_INIT_BITS 128 837c9e7a6fSAndy Grover 84b6df4b79SXiubo Li /* The total size of the ring is 8M + 256K * PAGE_SIZE */ 857c9e7a6fSAndy Grover #define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE) 867c9e7a6fSAndy Grover 87b6df4b79SXiubo Li /* Default maximum of the global data blocks(512K * PAGE_SIZE) */ 88b6df4b79SXiubo Li #define TCMU_GLOBAL_MAX_BLOCKS (512 * 1024) 89b6df4b79SXiubo Li 90b3af66e2SMike Christie static u8 tcmu_kern_cmd_reply_supported; 91b3af66e2SMike Christie 927c9e7a6fSAndy Grover static struct device *tcmu_root_device; 937c9e7a6fSAndy Grover 947c9e7a6fSAndy Grover struct tcmu_hba { 957c9e7a6fSAndy Grover u32 host_id; 967c9e7a6fSAndy Grover }; 977c9e7a6fSAndy Grover 987c9e7a6fSAndy Grover #define TCMU_CONFIG_LEN 256 997c9e7a6fSAndy Grover 100b3af66e2SMike Christie struct tcmu_nl_cmd { 101b3af66e2SMike Christie /* wake up thread waiting for reply */ 102b3af66e2SMike Christie struct completion complete; 103b3af66e2SMike Christie int cmd; 104b3af66e2SMike Christie int status; 105b3af66e2SMike Christie }; 106b3af66e2SMike Christie 1077c9e7a6fSAndy Grover struct tcmu_dev { 108b6df4b79SXiubo Li struct list_head node; 109f3cdbe39SMike Christie struct kref kref; 1107c9e7a6fSAndy Grover struct se_device se_dev; 1117c9e7a6fSAndy Grover 1127c9e7a6fSAndy Grover char *name; 1137c9e7a6fSAndy Grover struct se_hba *hba; 1147c9e7a6fSAndy Grover 1157c9e7a6fSAndy Grover #define TCMU_DEV_BIT_OPEN 0 1167c9e7a6fSAndy Grover #define TCMU_DEV_BIT_BROKEN 1 1177c9e7a6fSAndy Grover unsigned long flags; 1187c9e7a6fSAndy Grover 1197c9e7a6fSAndy Grover struct uio_info uio_info; 1207c9e7a6fSAndy Grover 121b6df4b79SXiubo Li struct inode *inode; 122b6df4b79SXiubo Li 1237c9e7a6fSAndy Grover struct tcmu_mailbox *mb_addr; 1247c9e7a6fSAndy Grover size_t dev_size; 1257c9e7a6fSAndy Grover u32 cmdr_size; 1267c9e7a6fSAndy Grover u32 cmdr_last_cleaned; 1273d9b9555SAndy Grover /* Offset of data area from start of mb */ 12826418649SSheng Yang /* Must add data_off and mb_addr to get the address */ 1297c9e7a6fSAndy Grover size_t data_off; 1307c9e7a6fSAndy Grover size_t data_size; 13126418649SSheng Yang 1327c9e7a6fSAndy Grover wait_queue_head_t wait_cmdr; 133b6df4b79SXiubo Li struct mutex cmdr_lock; 1347c9e7a6fSAndy Grover 135b6df4b79SXiubo Li bool waiting_global; 136141685a3SXiubo Li uint32_t dbi_max; 137b6df4b79SXiubo Li uint32_t dbi_thresh; 138141685a3SXiubo Li DECLARE_BITMAP(data_bitmap, DATA_BLOCK_BITS); 139141685a3SXiubo Li struct radix_tree_root data_blocks; 140141685a3SXiubo Li 1417c9e7a6fSAndy Grover struct idr commands; 1427c9e7a6fSAndy Grover spinlock_t commands_lock; 1437c9e7a6fSAndy Grover 1447c9e7a6fSAndy Grover struct timer_list timeout; 145af980e46SMike Christie unsigned int cmd_time_out; 1467c9e7a6fSAndy Grover 147b3af66e2SMike Christie spinlock_t nl_cmd_lock; 148b3af66e2SMike Christie struct tcmu_nl_cmd curr_nl_cmd; 149b3af66e2SMike Christie /* wake up threads waiting on curr_nl_cmd */ 150b3af66e2SMike Christie wait_queue_head_t nl_cmd_wq; 151b3af66e2SMike Christie 1527c9e7a6fSAndy Grover char dev_config[TCMU_CONFIG_LEN]; 1537c9e7a6fSAndy Grover }; 1547c9e7a6fSAndy Grover 1557c9e7a6fSAndy Grover #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev) 1567c9e7a6fSAndy Grover 1577c9e7a6fSAndy Grover #define CMDR_OFF sizeof(struct tcmu_mailbox) 1587c9e7a6fSAndy Grover 1597c9e7a6fSAndy Grover struct tcmu_cmd { 1607c9e7a6fSAndy Grover struct se_cmd *se_cmd; 1617c9e7a6fSAndy Grover struct tcmu_dev *tcmu_dev; 1627c9e7a6fSAndy Grover 1637c9e7a6fSAndy Grover uint16_t cmd_id; 1647c9e7a6fSAndy Grover 16526418649SSheng Yang /* Can't use se_cmd when cleaning up expired cmds, because if 1667c9e7a6fSAndy Grover cmd has been completed then accessing se_cmd is off limits */ 167141685a3SXiubo Li uint32_t dbi_cnt; 168141685a3SXiubo Li uint32_t dbi_cur; 169141685a3SXiubo Li uint32_t *dbi; 1707c9e7a6fSAndy Grover 1717c9e7a6fSAndy Grover unsigned long deadline; 1727c9e7a6fSAndy Grover 1737c9e7a6fSAndy Grover #define TCMU_CMD_BIT_EXPIRED 0 1747c9e7a6fSAndy Grover unsigned long flags; 1757c9e7a6fSAndy Grover }; 1767c9e7a6fSAndy Grover 177b6df4b79SXiubo Li static struct task_struct *unmap_thread; 178b6df4b79SXiubo Li static wait_queue_head_t unmap_wait; 179b6df4b79SXiubo Li static DEFINE_MUTEX(root_udev_mutex); 180b6df4b79SXiubo Li static LIST_HEAD(root_udev); 181b6df4b79SXiubo Li 182b6df4b79SXiubo Li static atomic_t global_db_count = ATOMIC_INIT(0); 183b6df4b79SXiubo Li 1847c9e7a6fSAndy Grover static struct kmem_cache *tcmu_cmd_cache; 1857c9e7a6fSAndy Grover 1867c9e7a6fSAndy Grover /* multicast group */ 1877c9e7a6fSAndy Grover enum tcmu_multicast_groups { 1887c9e7a6fSAndy Grover TCMU_MCGRP_CONFIG, 1897c9e7a6fSAndy Grover }; 1907c9e7a6fSAndy Grover 1917c9e7a6fSAndy Grover static const struct genl_multicast_group tcmu_mcgrps[] = { 1927c9e7a6fSAndy Grover [TCMU_MCGRP_CONFIG] = { .name = "config", }, 1937c9e7a6fSAndy Grover }; 1947c9e7a6fSAndy Grover 195b3af66e2SMike Christie static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = { 196b3af66e2SMike Christie [TCMU_ATTR_DEVICE] = { .type = NLA_STRING }, 197b3af66e2SMike Christie [TCMU_ATTR_MINOR] = { .type = NLA_U32 }, 198b3af66e2SMike Christie [TCMU_ATTR_CMD_STATUS] = { .type = NLA_S32 }, 199b3af66e2SMike Christie [TCMU_ATTR_DEVICE_ID] = { .type = NLA_U32 }, 200b3af66e2SMike Christie [TCMU_ATTR_SUPP_KERN_CMD_REPLY] = { .type = NLA_U8 }, 201b3af66e2SMike Christie }; 202b3af66e2SMike Christie 203b3af66e2SMike Christie static int tcmu_genl_cmd_done(struct genl_info *info, int completed_cmd) 204b3af66e2SMike Christie { 205b3af66e2SMike Christie struct se_device *dev; 206b3af66e2SMike Christie struct tcmu_dev *udev; 207b3af66e2SMike Christie struct tcmu_nl_cmd *nl_cmd; 208b3af66e2SMike Christie int dev_id, rc, ret = 0; 209b3af66e2SMike Christie bool is_removed = (completed_cmd == TCMU_CMD_REMOVED_DEVICE); 210b3af66e2SMike Christie 211b3af66e2SMike Christie if (!info->attrs[TCMU_ATTR_CMD_STATUS] || 212b3af66e2SMike Christie !info->attrs[TCMU_ATTR_DEVICE_ID]) { 213b3af66e2SMike Christie printk(KERN_ERR "TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n"); 214b3af66e2SMike Christie return -EINVAL; 215b3af66e2SMike Christie } 216b3af66e2SMike Christie 217b3af66e2SMike Christie dev_id = nla_get_u32(info->attrs[TCMU_ATTR_DEVICE_ID]); 218b3af66e2SMike Christie rc = nla_get_s32(info->attrs[TCMU_ATTR_CMD_STATUS]); 219b3af66e2SMike Christie 220b3af66e2SMike Christie dev = target_find_device(dev_id, !is_removed); 221b3af66e2SMike Christie if (!dev) { 222b3af66e2SMike Christie printk(KERN_ERR "tcmu nl cmd %u/%u completion could not find device with dev id %u.\n", 223b3af66e2SMike Christie completed_cmd, rc, dev_id); 224b3af66e2SMike Christie return -ENODEV; 225b3af66e2SMike Christie } 226b3af66e2SMike Christie udev = TCMU_DEV(dev); 227b3af66e2SMike Christie 228b3af66e2SMike Christie spin_lock(&udev->nl_cmd_lock); 229b3af66e2SMike Christie nl_cmd = &udev->curr_nl_cmd; 230b3af66e2SMike Christie 231b3af66e2SMike Christie pr_debug("genl cmd done got id %d curr %d done %d rc %d\n", dev_id, 232b3af66e2SMike Christie nl_cmd->cmd, completed_cmd, rc); 233b3af66e2SMike Christie 234b3af66e2SMike Christie if (nl_cmd->cmd != completed_cmd) { 235b3af66e2SMike Christie printk(KERN_ERR "Mismatched commands (Expecting reply for %d. Current %d).\n", 236b3af66e2SMike Christie completed_cmd, nl_cmd->cmd); 237b3af66e2SMike Christie ret = -EINVAL; 238b3af66e2SMike Christie } else { 239b3af66e2SMike Christie nl_cmd->status = rc; 240b3af66e2SMike Christie } 241b3af66e2SMike Christie 242b3af66e2SMike Christie spin_unlock(&udev->nl_cmd_lock); 243b3af66e2SMike Christie if (!is_removed) 244b3af66e2SMike Christie target_undepend_item(&dev->dev_group.cg_item); 245b3af66e2SMike Christie if (!ret) 246b3af66e2SMike Christie complete(&nl_cmd->complete); 247b3af66e2SMike Christie return ret; 248b3af66e2SMike Christie } 249b3af66e2SMike Christie 250b3af66e2SMike Christie static int tcmu_genl_rm_dev_done(struct sk_buff *skb, struct genl_info *info) 251b3af66e2SMike Christie { 252b3af66e2SMike Christie return tcmu_genl_cmd_done(info, TCMU_CMD_REMOVED_DEVICE); 253b3af66e2SMike Christie } 254b3af66e2SMike Christie 255b3af66e2SMike Christie static int tcmu_genl_add_dev_done(struct sk_buff *skb, struct genl_info *info) 256b3af66e2SMike Christie { 257b3af66e2SMike Christie return tcmu_genl_cmd_done(info, TCMU_CMD_ADDED_DEVICE); 258b3af66e2SMike Christie } 259b3af66e2SMike Christie 260b3af66e2SMike Christie static int tcmu_genl_reconfig_dev_done(struct sk_buff *skb, 261b3af66e2SMike Christie struct genl_info *info) 262b3af66e2SMike Christie { 263b3af66e2SMike Christie return tcmu_genl_cmd_done(info, TCMU_CMD_RECONFIG_DEVICE); 264b3af66e2SMike Christie } 265b3af66e2SMike Christie 266b3af66e2SMike Christie static int tcmu_genl_set_features(struct sk_buff *skb, struct genl_info *info) 267b3af66e2SMike Christie { 268b3af66e2SMike Christie if (info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]) { 269b3af66e2SMike Christie tcmu_kern_cmd_reply_supported = 270b3af66e2SMike Christie nla_get_u8(info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]); 271b3af66e2SMike Christie printk(KERN_INFO "tcmu daemon: command reply support %u.\n", 272b3af66e2SMike Christie tcmu_kern_cmd_reply_supported); 273b3af66e2SMike Christie } 274b3af66e2SMike Christie 275b3af66e2SMike Christie return 0; 276b3af66e2SMike Christie } 277b3af66e2SMike Christie 278b3af66e2SMike Christie static const struct genl_ops tcmu_genl_ops[] = { 279b3af66e2SMike Christie { 280b3af66e2SMike Christie .cmd = TCMU_CMD_SET_FEATURES, 281b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 282b3af66e2SMike Christie .policy = tcmu_attr_policy, 283b3af66e2SMike Christie .doit = tcmu_genl_set_features, 284b3af66e2SMike Christie }, 285b3af66e2SMike Christie { 286b3af66e2SMike Christie .cmd = TCMU_CMD_ADDED_DEVICE_DONE, 287b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 288b3af66e2SMike Christie .policy = tcmu_attr_policy, 289b3af66e2SMike Christie .doit = tcmu_genl_add_dev_done, 290b3af66e2SMike Christie }, 291b3af66e2SMike Christie { 292b3af66e2SMike Christie .cmd = TCMU_CMD_REMOVED_DEVICE_DONE, 293b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 294b3af66e2SMike Christie .policy = tcmu_attr_policy, 295b3af66e2SMike Christie .doit = tcmu_genl_rm_dev_done, 296b3af66e2SMike Christie }, 297b3af66e2SMike Christie { 298b3af66e2SMike Christie .cmd = TCMU_CMD_RECONFIG_DEVICE_DONE, 299b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 300b3af66e2SMike Christie .policy = tcmu_attr_policy, 301b3af66e2SMike Christie .doit = tcmu_genl_reconfig_dev_done, 302b3af66e2SMike Christie }, 303b3af66e2SMike Christie }; 304b3af66e2SMike Christie 3057c9e7a6fSAndy Grover /* Our generic netlink family */ 30656989f6dSJohannes Berg static struct genl_family tcmu_genl_family __ro_after_init = { 307489111e5SJohannes Berg .module = THIS_MODULE, 3087c9e7a6fSAndy Grover .hdrsize = 0, 3097c9e7a6fSAndy Grover .name = "TCM-USER", 310b3af66e2SMike Christie .version = 2, 3117c9e7a6fSAndy Grover .maxattr = TCMU_ATTR_MAX, 3127c9e7a6fSAndy Grover .mcgrps = tcmu_mcgrps, 3137c9e7a6fSAndy Grover .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps), 31420c08b36SSheng Yang .netnsok = true, 315b3af66e2SMike Christie .ops = tcmu_genl_ops, 316b3af66e2SMike Christie .n_ops = ARRAY_SIZE(tcmu_genl_ops), 3177c9e7a6fSAndy Grover }; 3187c9e7a6fSAndy Grover 319141685a3SXiubo Li #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index)) 320141685a3SXiubo Li #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0) 321141685a3SXiubo Li #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index)) 322141685a3SXiubo Li #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++]) 323141685a3SXiubo Li 324b6df4b79SXiubo Li static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len) 325141685a3SXiubo Li { 326141685a3SXiubo Li struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 327141685a3SXiubo Li uint32_t i; 328141685a3SXiubo Li 329b6df4b79SXiubo Li for (i = 0; i < len; i++) 330141685a3SXiubo Li clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap); 331141685a3SXiubo Li } 332141685a3SXiubo Li 333b6df4b79SXiubo Li static inline bool tcmu_get_empty_block(struct tcmu_dev *udev, 334b6df4b79SXiubo Li struct tcmu_cmd *tcmu_cmd) 335141685a3SXiubo Li { 336b6df4b79SXiubo Li struct page *page; 337b6df4b79SXiubo Li int ret, dbi; 338141685a3SXiubo Li 339b6df4b79SXiubo Li dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh); 340b6df4b79SXiubo Li if (dbi == udev->dbi_thresh) 341b6df4b79SXiubo Li return false; 342b6df4b79SXiubo Li 343b6df4b79SXiubo Li page = radix_tree_lookup(&udev->data_blocks, dbi); 344b6df4b79SXiubo Li if (!page) { 345b6df4b79SXiubo Li 346b6df4b79SXiubo Li if (atomic_add_return(1, &global_db_count) > 347b6df4b79SXiubo Li TCMU_GLOBAL_MAX_BLOCKS) { 348b6df4b79SXiubo Li atomic_dec(&global_db_count); 349b6df4b79SXiubo Li return false; 350b6df4b79SXiubo Li } 351b6df4b79SXiubo Li 352b6df4b79SXiubo Li /* try to get new page from the mm */ 353b6df4b79SXiubo Li page = alloc_page(GFP_KERNEL); 354b6df4b79SXiubo Li if (!page) 355b6df4b79SXiubo Li return false; 356b6df4b79SXiubo Li 357b6df4b79SXiubo Li ret = radix_tree_insert(&udev->data_blocks, dbi, page); 358b6df4b79SXiubo Li if (ret) { 359b6df4b79SXiubo Li __free_page(page); 360b6df4b79SXiubo Li return false; 361b6df4b79SXiubo Li } 362b6df4b79SXiubo Li 363b6df4b79SXiubo Li } 364b6df4b79SXiubo Li 365141685a3SXiubo Li if (dbi > udev->dbi_max) 366141685a3SXiubo Li udev->dbi_max = dbi; 367141685a3SXiubo Li 368141685a3SXiubo Li set_bit(dbi, udev->data_bitmap); 369b6df4b79SXiubo Li tcmu_cmd_set_dbi(tcmu_cmd, dbi); 370141685a3SXiubo Li 371b6df4b79SXiubo Li return true; 372141685a3SXiubo Li } 373141685a3SXiubo Li 374b6df4b79SXiubo Li static bool tcmu_get_empty_blocks(struct tcmu_dev *udev, 375b6df4b79SXiubo Li struct tcmu_cmd *tcmu_cmd) 376b6df4b79SXiubo Li { 377b6df4b79SXiubo Li int i; 378b6df4b79SXiubo Li 379b6df4b79SXiubo Li udev->waiting_global = false; 380b6df4b79SXiubo Li 381b6df4b79SXiubo Li for (i = tcmu_cmd->dbi_cur; i < tcmu_cmd->dbi_cnt; i++) { 382b6df4b79SXiubo Li if (!tcmu_get_empty_block(udev, tcmu_cmd)) 383b6df4b79SXiubo Li goto err; 384141685a3SXiubo Li } 385b6df4b79SXiubo Li return true; 386b6df4b79SXiubo Li 387b6df4b79SXiubo Li err: 388b6df4b79SXiubo Li udev->waiting_global = true; 389b6df4b79SXiubo Li /* Try to wake up the unmap thread */ 390b6df4b79SXiubo Li wake_up(&unmap_wait); 391b6df4b79SXiubo Li return false; 392141685a3SXiubo Li } 393141685a3SXiubo Li 394b6df4b79SXiubo Li static inline struct page * 395b6df4b79SXiubo Li tcmu_get_block_page(struct tcmu_dev *udev, uint32_t dbi) 396141685a3SXiubo Li { 397141685a3SXiubo Li return radix_tree_lookup(&udev->data_blocks, dbi); 398141685a3SXiubo Li } 399141685a3SXiubo Li 400141685a3SXiubo Li static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd) 401141685a3SXiubo Li { 402141685a3SXiubo Li kfree(tcmu_cmd->dbi); 403141685a3SXiubo Li kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); 404141685a3SXiubo Li } 405141685a3SXiubo Li 406141685a3SXiubo Li static inline size_t tcmu_cmd_get_data_length(struct tcmu_cmd *tcmu_cmd) 407141685a3SXiubo Li { 408141685a3SXiubo Li struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 409141685a3SXiubo Li size_t data_length = round_up(se_cmd->data_length, DATA_BLOCK_SIZE); 410141685a3SXiubo Li 411141685a3SXiubo Li if (se_cmd->se_cmd_flags & SCF_BIDI) { 412141685a3SXiubo Li BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents)); 413141685a3SXiubo Li data_length += round_up(se_cmd->t_bidi_data_sg->length, 414141685a3SXiubo Li DATA_BLOCK_SIZE); 415141685a3SXiubo Li } 416141685a3SXiubo Li 417141685a3SXiubo Li return data_length; 418141685a3SXiubo Li } 419141685a3SXiubo Li 420141685a3SXiubo Li static inline uint32_t tcmu_cmd_get_block_cnt(struct tcmu_cmd *tcmu_cmd) 421141685a3SXiubo Li { 422141685a3SXiubo Li size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd); 423141685a3SXiubo Li 424141685a3SXiubo Li return data_length / DATA_BLOCK_SIZE; 425141685a3SXiubo Li } 426141685a3SXiubo Li 4277c9e7a6fSAndy Grover static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd) 4287c9e7a6fSAndy Grover { 4297c9e7a6fSAndy Grover struct se_device *se_dev = se_cmd->se_dev; 4307c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(se_dev); 4317c9e7a6fSAndy Grover struct tcmu_cmd *tcmu_cmd; 4327c9e7a6fSAndy Grover int cmd_id; 4337c9e7a6fSAndy Grover 4347c9e7a6fSAndy Grover tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_KERNEL); 4357c9e7a6fSAndy Grover if (!tcmu_cmd) 4367c9e7a6fSAndy Grover return NULL; 4377c9e7a6fSAndy Grover 4387c9e7a6fSAndy Grover tcmu_cmd->se_cmd = se_cmd; 4397c9e7a6fSAndy Grover tcmu_cmd->tcmu_dev = udev; 440af980e46SMike Christie if (udev->cmd_time_out) 441af980e46SMike Christie tcmu_cmd->deadline = jiffies + 442af980e46SMike Christie msecs_to_jiffies(udev->cmd_time_out); 4437c9e7a6fSAndy Grover 444141685a3SXiubo Li tcmu_cmd_reset_dbi_cur(tcmu_cmd); 445141685a3SXiubo Li tcmu_cmd->dbi_cnt = tcmu_cmd_get_block_cnt(tcmu_cmd); 446141685a3SXiubo Li tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t), 447141685a3SXiubo Li GFP_KERNEL); 448141685a3SXiubo Li if (!tcmu_cmd->dbi) { 449141685a3SXiubo Li kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); 450141685a3SXiubo Li return NULL; 451141685a3SXiubo Li } 452141685a3SXiubo Li 4537c9e7a6fSAndy Grover idr_preload(GFP_KERNEL); 4547c9e7a6fSAndy Grover spin_lock_irq(&udev->commands_lock); 4557c9e7a6fSAndy Grover cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 0, 4567c9e7a6fSAndy Grover USHRT_MAX, GFP_NOWAIT); 4577c9e7a6fSAndy Grover spin_unlock_irq(&udev->commands_lock); 4587c9e7a6fSAndy Grover idr_preload_end(); 4597c9e7a6fSAndy Grover 4607c9e7a6fSAndy Grover if (cmd_id < 0) { 461141685a3SXiubo Li tcmu_free_cmd(tcmu_cmd); 4627c9e7a6fSAndy Grover return NULL; 4637c9e7a6fSAndy Grover } 4647c9e7a6fSAndy Grover tcmu_cmd->cmd_id = cmd_id; 4657c9e7a6fSAndy Grover 4667c9e7a6fSAndy Grover return tcmu_cmd; 4677c9e7a6fSAndy Grover } 4687c9e7a6fSAndy Grover 4697c9e7a6fSAndy Grover static inline void tcmu_flush_dcache_range(void *vaddr, size_t size) 4707c9e7a6fSAndy Grover { 471b75d8063SGeliang Tang unsigned long offset = offset_in_page(vaddr); 4727c9e7a6fSAndy Grover 4737c9e7a6fSAndy Grover size = round_up(size+offset, PAGE_SIZE); 4747c9e7a6fSAndy Grover vaddr -= offset; 4757c9e7a6fSAndy Grover 4767c9e7a6fSAndy Grover while (size) { 4777c9e7a6fSAndy Grover flush_dcache_page(virt_to_page(vaddr)); 4787c9e7a6fSAndy Grover size -= PAGE_SIZE; 4797c9e7a6fSAndy Grover } 4807c9e7a6fSAndy Grover } 4817c9e7a6fSAndy Grover 4827c9e7a6fSAndy Grover /* 4837c9e7a6fSAndy Grover * Some ring helper functions. We don't assume size is a power of 2 so 4847c9e7a6fSAndy Grover * we can't use circ_buf.h. 4857c9e7a6fSAndy Grover */ 4867c9e7a6fSAndy Grover static inline size_t spc_used(size_t head, size_t tail, size_t size) 4877c9e7a6fSAndy Grover { 4887c9e7a6fSAndy Grover int diff = head - tail; 4897c9e7a6fSAndy Grover 4907c9e7a6fSAndy Grover if (diff >= 0) 4917c9e7a6fSAndy Grover return diff; 4927c9e7a6fSAndy Grover else 4937c9e7a6fSAndy Grover return size + diff; 4947c9e7a6fSAndy Grover } 4957c9e7a6fSAndy Grover 4967c9e7a6fSAndy Grover static inline size_t spc_free(size_t head, size_t tail, size_t size) 4977c9e7a6fSAndy Grover { 4987c9e7a6fSAndy Grover /* Keep 1 byte unused or we can't tell full from empty */ 4997c9e7a6fSAndy Grover return (size - spc_used(head, tail, size) - 1); 5007c9e7a6fSAndy Grover } 5017c9e7a6fSAndy Grover 5027c9e7a6fSAndy Grover static inline size_t head_to_end(size_t head, size_t size) 5037c9e7a6fSAndy Grover { 5047c9e7a6fSAndy Grover return size - head; 5057c9e7a6fSAndy Grover } 5067c9e7a6fSAndy Grover 507f1dbd087SSheng Yang static inline void new_iov(struct iovec **iov, int *iov_cnt, 508f1dbd087SSheng Yang struct tcmu_dev *udev) 509f1dbd087SSheng Yang { 510f1dbd087SSheng Yang struct iovec *iovec; 511f1dbd087SSheng Yang 512f1dbd087SSheng Yang if (*iov_cnt != 0) 513f1dbd087SSheng Yang (*iov)++; 514f1dbd087SSheng Yang (*iov_cnt)++; 515f1dbd087SSheng Yang 516f1dbd087SSheng Yang iovec = *iov; 517f1dbd087SSheng Yang memset(iovec, 0, sizeof(struct iovec)); 518f1dbd087SSheng Yang } 519f1dbd087SSheng Yang 5207c9e7a6fSAndy Grover #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size) 5217c9e7a6fSAndy Grover 52226418649SSheng Yang /* offset is relative to mb_addr */ 523141685a3SXiubo Li static inline size_t get_block_offset_user(struct tcmu_dev *dev, 524141685a3SXiubo Li int dbi, int remaining) 52526418649SSheng Yang { 526141685a3SXiubo Li return dev->data_off + dbi * DATA_BLOCK_SIZE + 52726418649SSheng Yang DATA_BLOCK_SIZE - remaining; 52826418649SSheng Yang } 52926418649SSheng Yang 53026418649SSheng Yang static inline size_t iov_tail(struct tcmu_dev *udev, struct iovec *iov) 53126418649SSheng Yang { 53226418649SSheng Yang return (size_t)iov->iov_base + iov->iov_len; 53326418649SSheng Yang } 53426418649SSheng Yang 535b6df4b79SXiubo Li static int scatter_data_area(struct tcmu_dev *udev, 536141685a3SXiubo Li struct tcmu_cmd *tcmu_cmd, struct scatterlist *data_sg, 537141685a3SXiubo Li unsigned int data_nents, struct iovec **iov, 538141685a3SXiubo Li int *iov_cnt, bool copy_data) 539f97ec7dbSIlias Tsitsimpis { 540141685a3SXiubo Li int i, dbi; 54126418649SSheng Yang int block_remaining = 0; 542141685a3SXiubo Li void *from, *to = NULL; 543141685a3SXiubo Li size_t copy_bytes, to_offset, offset; 544f97ec7dbSIlias Tsitsimpis struct scatterlist *sg; 545b6df4b79SXiubo Li struct page *page; 546f97ec7dbSIlias Tsitsimpis 547f97ec7dbSIlias Tsitsimpis for_each_sg(data_sg, sg, data_nents, i) { 54826418649SSheng Yang int sg_remaining = sg->length; 549f97ec7dbSIlias Tsitsimpis from = kmap_atomic(sg_page(sg)) + sg->offset; 55026418649SSheng Yang while (sg_remaining > 0) { 55126418649SSheng Yang if (block_remaining == 0) { 552b6df4b79SXiubo Li if (to) 553b6df4b79SXiubo Li kunmap_atomic(to); 554b6df4b79SXiubo Li 55526418649SSheng Yang block_remaining = DATA_BLOCK_SIZE; 556b6df4b79SXiubo Li dbi = tcmu_cmd_get_dbi(tcmu_cmd); 557b6df4b79SXiubo Li page = tcmu_get_block_page(udev, dbi); 558b6df4b79SXiubo Li to = kmap_atomic(page); 559141685a3SXiubo Li } 560141685a3SXiubo Li 56126418649SSheng Yang copy_bytes = min_t(size_t, sg_remaining, 56226418649SSheng Yang block_remaining); 563141685a3SXiubo Li to_offset = get_block_offset_user(udev, dbi, 56426418649SSheng Yang block_remaining); 565141685a3SXiubo Li offset = DATA_BLOCK_SIZE - block_remaining; 566b3743c71SXiubo Li to += offset; 567141685a3SXiubo Li 56826418649SSheng Yang if (*iov_cnt != 0 && 56926418649SSheng Yang to_offset == iov_tail(udev, *iov)) { 570f1dbd087SSheng Yang (*iov)->iov_len += copy_bytes; 57126418649SSheng Yang } else { 572f1dbd087SSheng Yang new_iov(iov, iov_cnt, udev); 57326418649SSheng Yang (*iov)->iov_base = (void __user *)to_offset; 574f97ec7dbSIlias Tsitsimpis (*iov)->iov_len = copy_bytes; 57526418649SSheng Yang } 576f97ec7dbSIlias Tsitsimpis if (copy_data) { 57726418649SSheng Yang memcpy(to, from + sg->length - sg_remaining, 57826418649SSheng Yang copy_bytes); 579f97ec7dbSIlias Tsitsimpis tcmu_flush_dcache_range(to, copy_bytes); 580f97ec7dbSIlias Tsitsimpis } 58126418649SSheng Yang sg_remaining -= copy_bytes; 58226418649SSheng Yang block_remaining -= copy_bytes; 583f97ec7dbSIlias Tsitsimpis } 584e2e21bd8SSagi Grimberg kunmap_atomic(from - sg->offset); 585f97ec7dbSIlias Tsitsimpis } 586b6df4b79SXiubo Li if (to) 587b6df4b79SXiubo Li kunmap_atomic(to); 588f97ec7dbSIlias Tsitsimpis 589141685a3SXiubo Li return 0; 5900c28481fSSheng Yang } 5910c28481fSSheng Yang 592a5d68ba8SXiubo Li static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 593a5d68ba8SXiubo Li bool bidi) 594f97ec7dbSIlias Tsitsimpis { 595a5d68ba8SXiubo Li struct se_cmd *se_cmd = cmd->se_cmd; 596141685a3SXiubo Li int i, dbi; 59726418649SSheng Yang int block_remaining = 0; 598b6df4b79SXiubo Li void *from = NULL, *to; 599141685a3SXiubo Li size_t copy_bytes, offset; 600a5d68ba8SXiubo Li struct scatterlist *sg, *data_sg; 601b6df4b79SXiubo Li struct page *page; 602a5d68ba8SXiubo Li unsigned int data_nents; 603141685a3SXiubo Li uint32_t count = 0; 604a5d68ba8SXiubo Li 605a5d68ba8SXiubo Li if (!bidi) { 606a5d68ba8SXiubo Li data_sg = se_cmd->t_data_sg; 607a5d68ba8SXiubo Li data_nents = se_cmd->t_data_nents; 608a5d68ba8SXiubo Li } else { 609a5d68ba8SXiubo Li 610a5d68ba8SXiubo Li /* 611a5d68ba8SXiubo Li * For bidi case, the first count blocks are for Data-Out 612a5d68ba8SXiubo Li * buffer blocks, and before gathering the Data-In buffer 613a5d68ba8SXiubo Li * the Data-Out buffer blocks should be discarded. 614a5d68ba8SXiubo Li */ 615a5d68ba8SXiubo Li count = DIV_ROUND_UP(se_cmd->data_length, DATA_BLOCK_SIZE); 616a5d68ba8SXiubo Li 617a5d68ba8SXiubo Li data_sg = se_cmd->t_bidi_data_sg; 618a5d68ba8SXiubo Li data_nents = se_cmd->t_bidi_data_nents; 619a5d68ba8SXiubo Li } 620f97ec7dbSIlias Tsitsimpis 621141685a3SXiubo Li tcmu_cmd_set_dbi_cur(cmd, count); 622141685a3SXiubo Li 623f97ec7dbSIlias Tsitsimpis for_each_sg(data_sg, sg, data_nents, i) { 62426418649SSheng Yang int sg_remaining = sg->length; 625f97ec7dbSIlias Tsitsimpis to = kmap_atomic(sg_page(sg)) + sg->offset; 62626418649SSheng Yang while (sg_remaining > 0) { 62726418649SSheng Yang if (block_remaining == 0) { 628b6df4b79SXiubo Li if (from) 629b6df4b79SXiubo Li kunmap_atomic(from); 630b6df4b79SXiubo Li 63126418649SSheng Yang block_remaining = DATA_BLOCK_SIZE; 632141685a3SXiubo Li dbi = tcmu_cmd_get_dbi(cmd); 633b6df4b79SXiubo Li page = tcmu_get_block_page(udev, dbi); 634b6df4b79SXiubo Li from = kmap_atomic(page); 63526418649SSheng Yang } 63626418649SSheng Yang copy_bytes = min_t(size_t, sg_remaining, 63726418649SSheng Yang block_remaining); 638141685a3SXiubo Li offset = DATA_BLOCK_SIZE - block_remaining; 639b3743c71SXiubo Li from += offset; 640f97ec7dbSIlias Tsitsimpis tcmu_flush_dcache_range(from, copy_bytes); 64126418649SSheng Yang memcpy(to + sg->length - sg_remaining, from, 64226418649SSheng Yang copy_bytes); 643f97ec7dbSIlias Tsitsimpis 64426418649SSheng Yang sg_remaining -= copy_bytes; 64526418649SSheng Yang block_remaining -= copy_bytes; 646f97ec7dbSIlias Tsitsimpis } 647e2e21bd8SSagi Grimberg kunmap_atomic(to - sg->offset); 648f97ec7dbSIlias Tsitsimpis } 649b6df4b79SXiubo Li if (from) 650b6df4b79SXiubo Li kunmap_atomic(from); 651f97ec7dbSIlias Tsitsimpis } 652f97ec7dbSIlias Tsitsimpis 653b6df4b79SXiubo Li static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh) 65426418649SSheng Yang { 655b6df4b79SXiubo Li return DATA_BLOCK_SIZE * (thresh - bitmap_weight(bitmap, thresh)); 65626418649SSheng Yang } 65726418649SSheng Yang 6587c9e7a6fSAndy Grover /* 659f97ec7dbSIlias Tsitsimpis * We can't queue a command until we have space available on the cmd ring *and* 6603d9b9555SAndy Grover * space available on the data area. 6617c9e7a6fSAndy Grover * 6627c9e7a6fSAndy Grover * Called with ring lock held. 6637c9e7a6fSAndy Grover */ 664b6df4b79SXiubo Li static bool is_ring_space_avail(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 665b6df4b79SXiubo Li size_t cmd_size, size_t data_needed) 6667c9e7a6fSAndy Grover { 6677c9e7a6fSAndy Grover struct tcmu_mailbox *mb = udev->mb_addr; 668b6df4b79SXiubo Li uint32_t blocks_needed = (data_needed + DATA_BLOCK_SIZE - 1) 669b6df4b79SXiubo Li / DATA_BLOCK_SIZE; 6700241fd39SNicholas Bellinger size_t space, cmd_needed; 6717c9e7a6fSAndy Grover u32 cmd_head; 6727c9e7a6fSAndy Grover 6737c9e7a6fSAndy Grover tcmu_flush_dcache_range(mb, sizeof(*mb)); 6747c9e7a6fSAndy Grover 6757c9e7a6fSAndy Grover cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 6767c9e7a6fSAndy Grover 677f56574a2SAndy Grover /* 678f56574a2SAndy Grover * If cmd end-of-ring space is too small then we need space for a NOP plus 679f56574a2SAndy Grover * original cmd - cmds are internally contiguous. 680f56574a2SAndy Grover */ 681f56574a2SAndy Grover if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size) 682f56574a2SAndy Grover cmd_needed = cmd_size; 683f56574a2SAndy Grover else 684f56574a2SAndy Grover cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size); 685f56574a2SAndy Grover 6867c9e7a6fSAndy Grover space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size); 6877c9e7a6fSAndy Grover if (space < cmd_needed) { 6887c9e7a6fSAndy Grover pr_debug("no cmd space: %u %u %u\n", cmd_head, 6897c9e7a6fSAndy Grover udev->cmdr_last_cleaned, udev->cmdr_size); 6907c9e7a6fSAndy Grover return false; 6917c9e7a6fSAndy Grover } 6927c9e7a6fSAndy Grover 693b6df4b79SXiubo Li /* try to check and get the data blocks as needed */ 694b6df4b79SXiubo Li space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh); 6957c9e7a6fSAndy Grover if (space < data_needed) { 696b6df4b79SXiubo Li unsigned long blocks_left = DATA_BLOCK_BITS - udev->dbi_thresh; 697b6df4b79SXiubo Li unsigned long grow; 698b6df4b79SXiubo Li 699b6df4b79SXiubo Li if (blocks_left < blocks_needed) { 700b6df4b79SXiubo Li pr_debug("no data space: only %lu available, but ask for %zu\n", 701b6df4b79SXiubo Li blocks_left * DATA_BLOCK_SIZE, 702b6df4b79SXiubo Li data_needed); 7037c9e7a6fSAndy Grover return false; 7047c9e7a6fSAndy Grover } 7057c9e7a6fSAndy Grover 706b6df4b79SXiubo Li /* Try to expand the thresh */ 707b6df4b79SXiubo Li if (!udev->dbi_thresh) { 708b6df4b79SXiubo Li /* From idle state */ 709b6df4b79SXiubo Li uint32_t init_thresh = DATA_BLOCK_INIT_BITS; 710b6df4b79SXiubo Li 711b6df4b79SXiubo Li udev->dbi_thresh = max(blocks_needed, init_thresh); 712b6df4b79SXiubo Li } else { 713b6df4b79SXiubo Li /* 714b6df4b79SXiubo Li * Grow the data area by max(blocks needed, 715b6df4b79SXiubo Li * dbi_thresh / 2), but limited to the max 716b6df4b79SXiubo Li * DATA_BLOCK_BITS size. 717b6df4b79SXiubo Li */ 718b6df4b79SXiubo Li grow = max(blocks_needed, udev->dbi_thresh / 2); 719b6df4b79SXiubo Li udev->dbi_thresh += grow; 720b6df4b79SXiubo Li if (udev->dbi_thresh > DATA_BLOCK_BITS) 721b6df4b79SXiubo Li udev->dbi_thresh = DATA_BLOCK_BITS; 722b6df4b79SXiubo Li } 723b6df4b79SXiubo Li } 724b6df4b79SXiubo Li 725b6df4b79SXiubo Li if (!tcmu_get_empty_blocks(udev, cmd)) 726b6df4b79SXiubo Li return false; 727b6df4b79SXiubo Li 7287c9e7a6fSAndy Grover return true; 7297c9e7a6fSAndy Grover } 7307c9e7a6fSAndy Grover 731fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt) 732fe25cc34SXiubo Li { 733fe25cc34SXiubo Li return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]), 734fe25cc34SXiubo Li sizeof(struct tcmu_cmd_entry)); 735fe25cc34SXiubo Li } 736fe25cc34SXiubo Li 737fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd, 738fe25cc34SXiubo Li size_t base_command_size) 739fe25cc34SXiubo Li { 740fe25cc34SXiubo Li struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 741fe25cc34SXiubo Li size_t command_size; 742fe25cc34SXiubo Li 743fe25cc34SXiubo Li command_size = base_command_size + 744fe25cc34SXiubo Li round_up(scsi_command_size(se_cmd->t_task_cdb), 745fe25cc34SXiubo Li TCMU_OP_ALIGN_SIZE); 746fe25cc34SXiubo Li 747fe25cc34SXiubo Li WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1)); 748fe25cc34SXiubo Li 749fe25cc34SXiubo Li return command_size; 750fe25cc34SXiubo Li } 751fe25cc34SXiubo Li 75202eb924fSAndy Grover static sense_reason_t 75302eb924fSAndy Grover tcmu_queue_cmd_ring(struct tcmu_cmd *tcmu_cmd) 7547c9e7a6fSAndy Grover { 7557c9e7a6fSAndy Grover struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 7567c9e7a6fSAndy Grover struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 7577c9e7a6fSAndy Grover size_t base_command_size, command_size; 7587c9e7a6fSAndy Grover struct tcmu_mailbox *mb; 7597c9e7a6fSAndy Grover struct tcmu_cmd_entry *entry; 7607c9e7a6fSAndy Grover struct iovec *iov; 761141685a3SXiubo Li int iov_cnt, ret; 7627c9e7a6fSAndy Grover uint32_t cmd_head; 7637c9e7a6fSAndy Grover uint64_t cdb_off; 764f97ec7dbSIlias Tsitsimpis bool copy_to_data_area; 765ab22d260SXiubo Li size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd); 7667c9e7a6fSAndy Grover 7677c9e7a6fSAndy Grover if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) 76802eb924fSAndy Grover return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 7697c9e7a6fSAndy Grover 7707c9e7a6fSAndy Grover /* 7717c9e7a6fSAndy Grover * Must be a certain minimum size for response sense info, but 7727c9e7a6fSAndy Grover * also may be larger if the iov array is large. 7737c9e7a6fSAndy Grover * 774fe25cc34SXiubo Li * We prepare as many iovs as possbile for potential uses here, 775fe25cc34SXiubo Li * because it's expensive to tell how many regions are freed in 776fe25cc34SXiubo Li * the bitmap & global data pool, as the size calculated here 777fe25cc34SXiubo Li * will only be used to do the checks. 778fe25cc34SXiubo Li * 779fe25cc34SXiubo Li * The size will be recalculated later as actually needed to save 780fe25cc34SXiubo Li * cmd area memories. 7817c9e7a6fSAndy Grover */ 782fe25cc34SXiubo Li base_command_size = tcmu_cmd_get_base_cmd_size(tcmu_cmd->dbi_cnt); 783fe25cc34SXiubo Li command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size); 7847c9e7a6fSAndy Grover 785b6df4b79SXiubo Li mutex_lock(&udev->cmdr_lock); 7867c9e7a6fSAndy Grover 7877c9e7a6fSAndy Grover mb = udev->mb_addr; 7887c9e7a6fSAndy Grover cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 789554617b2SAndy Grover if ((command_size > (udev->cmdr_size / 2)) || 790554617b2SAndy Grover data_length > udev->data_size) { 791554617b2SAndy Grover pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu " 7923d9b9555SAndy Grover "cmd ring/data area\n", command_size, data_length, 7937c9e7a6fSAndy Grover udev->cmdr_size, udev->data_size); 794b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 795554617b2SAndy Grover return TCM_INVALID_CDB_FIELD; 796554617b2SAndy Grover } 7977c9e7a6fSAndy Grover 798b6df4b79SXiubo Li while (!is_ring_space_avail(udev, tcmu_cmd, command_size, data_length)) { 7997c9e7a6fSAndy Grover int ret; 8007c9e7a6fSAndy Grover DEFINE_WAIT(__wait); 8017c9e7a6fSAndy Grover 8027c9e7a6fSAndy Grover prepare_to_wait(&udev->wait_cmdr, &__wait, TASK_INTERRUPTIBLE); 8037c9e7a6fSAndy Grover 8047c9e7a6fSAndy Grover pr_debug("sleeping for ring space\n"); 805b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 806af980e46SMike Christie if (udev->cmd_time_out) 807af980e46SMike Christie ret = schedule_timeout( 808af980e46SMike Christie msecs_to_jiffies(udev->cmd_time_out)); 809af980e46SMike Christie else 8107c9e7a6fSAndy Grover ret = schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT)); 8117c9e7a6fSAndy Grover finish_wait(&udev->wait_cmdr, &__wait); 8127c9e7a6fSAndy Grover if (!ret) { 8137c9e7a6fSAndy Grover pr_warn("tcmu: command timed out\n"); 81402eb924fSAndy Grover return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 8157c9e7a6fSAndy Grover } 8167c9e7a6fSAndy Grover 817b6df4b79SXiubo Li mutex_lock(&udev->cmdr_lock); 8187c9e7a6fSAndy Grover 8197c9e7a6fSAndy Grover /* We dropped cmdr_lock, cmd_head is stale */ 8207c9e7a6fSAndy Grover cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 8217c9e7a6fSAndy Grover } 8227c9e7a6fSAndy Grover 823f56574a2SAndy Grover /* Insert a PAD if end-of-ring space is too small */ 824f56574a2SAndy Grover if (head_to_end(cmd_head, udev->cmdr_size) < command_size) { 825f56574a2SAndy Grover size_t pad_size = head_to_end(cmd_head, udev->cmdr_size); 826f56574a2SAndy Grover 8277c9e7a6fSAndy Grover entry = (void *) mb + CMDR_OFF + cmd_head; 8280ad46af8SAndy Grover tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD); 8290ad46af8SAndy Grover tcmu_hdr_set_len(&entry->hdr.len_op, pad_size); 8300ad46af8SAndy Grover entry->hdr.cmd_id = 0; /* not used for PAD */ 8310ad46af8SAndy Grover entry->hdr.kflags = 0; 8320ad46af8SAndy Grover entry->hdr.uflags = 0; 8339d62bc0eSXiubo Li tcmu_flush_dcache_range(entry, sizeof(*entry)); 8347c9e7a6fSAndy Grover 8357c9e7a6fSAndy Grover UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size); 8369d62bc0eSXiubo Li tcmu_flush_dcache_range(mb, sizeof(*mb)); 8377c9e7a6fSAndy Grover 8387c9e7a6fSAndy Grover cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 8397c9e7a6fSAndy Grover WARN_ON(cmd_head != 0); 8407c9e7a6fSAndy Grover } 8417c9e7a6fSAndy Grover 8427c9e7a6fSAndy Grover entry = (void *) mb + CMDR_OFF + cmd_head; 843b3743c71SXiubo Li memset(entry, 0, command_size); 8440ad46af8SAndy Grover tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD); 8450ad46af8SAndy Grover entry->hdr.cmd_id = tcmu_cmd->cmd_id; 8467c9e7a6fSAndy Grover 8473d9b9555SAndy Grover /* Handle allocating space from the data area */ 848b6df4b79SXiubo Li tcmu_cmd_reset_dbi_cur(tcmu_cmd); 8497c9e7a6fSAndy Grover iov = &entry->req.iov[0]; 850f97ec7dbSIlias Tsitsimpis iov_cnt = 0; 851e4648b01SIlias Tsitsimpis copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE 852e4648b01SIlias Tsitsimpis || se_cmd->se_cmd_flags & SCF_BIDI); 853b6df4b79SXiubo Li ret = scatter_data_area(udev, tcmu_cmd, se_cmd->t_data_sg, 854b6df4b79SXiubo Li se_cmd->t_data_nents, &iov, &iov_cnt, 855b6df4b79SXiubo Li copy_to_data_area); 856141685a3SXiubo Li if (ret) { 857b6df4b79SXiubo Li tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt); 858b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 859b6df4b79SXiubo Li 860141685a3SXiubo Li pr_err("tcmu: alloc and scatter data failed\n"); 861141685a3SXiubo Li return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 862141685a3SXiubo Li } 8637c9e7a6fSAndy Grover entry->req.iov_cnt = iov_cnt; 8647c9e7a6fSAndy Grover 865e4648b01SIlias Tsitsimpis /* Handle BIDI commands */ 866e4648b01SIlias Tsitsimpis iov_cnt = 0; 867b3743c71SXiubo Li if (se_cmd->se_cmd_flags & SCF_BIDI) { 868ab22d260SXiubo Li iov++; 869b6df4b79SXiubo Li ret = scatter_data_area(udev, tcmu_cmd, 870141685a3SXiubo Li se_cmd->t_bidi_data_sg, 871141685a3SXiubo Li se_cmd->t_bidi_data_nents, 872141685a3SXiubo Li &iov, &iov_cnt, false); 873141685a3SXiubo Li if (ret) { 874b6df4b79SXiubo Li tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt); 875b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 876b6df4b79SXiubo Li 877141685a3SXiubo Li pr_err("tcmu: alloc and scatter bidi data failed\n"); 878141685a3SXiubo Li return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 879141685a3SXiubo Li } 880ab22d260SXiubo Li } 881b3743c71SXiubo Li entry->req.iov_bidi_cnt = iov_cnt; 88226418649SSheng Yang 883fe25cc34SXiubo Li /* 884fe25cc34SXiubo Li * Recalaulate the command's base size and size according 885fe25cc34SXiubo Li * to the actual needs 886fe25cc34SXiubo Li */ 887fe25cc34SXiubo Li base_command_size = tcmu_cmd_get_base_cmd_size(entry->req.iov_cnt + 888fe25cc34SXiubo Li entry->req.iov_bidi_cnt); 889fe25cc34SXiubo Li command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size); 890fe25cc34SXiubo Li 891fe25cc34SXiubo Li tcmu_hdr_set_len(&entry->hdr.len_op, command_size); 892fe25cc34SXiubo Li 8937c9e7a6fSAndy Grover /* All offsets relative to mb_addr, not start of entry! */ 8947c9e7a6fSAndy Grover cdb_off = CMDR_OFF + cmd_head + base_command_size; 8957c9e7a6fSAndy Grover memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb)); 8967c9e7a6fSAndy Grover entry->req.cdb_off = cdb_off; 8977c9e7a6fSAndy Grover tcmu_flush_dcache_range(entry, sizeof(*entry)); 8987c9e7a6fSAndy Grover 8997c9e7a6fSAndy Grover UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size); 9007c9e7a6fSAndy Grover tcmu_flush_dcache_range(mb, sizeof(*mb)); 901b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 9027c9e7a6fSAndy Grover 9037c9e7a6fSAndy Grover /* TODO: only if FLUSH and FUA? */ 9047c9e7a6fSAndy Grover uio_event_notify(&udev->uio_info); 9057c9e7a6fSAndy Grover 906af980e46SMike Christie if (udev->cmd_time_out) 907af980e46SMike Christie mod_timer(&udev->timeout, round_jiffies_up(jiffies + 908af980e46SMike Christie msecs_to_jiffies(udev->cmd_time_out))); 9097c9e7a6fSAndy Grover 91002eb924fSAndy Grover return TCM_NO_SENSE; 9117c9e7a6fSAndy Grover } 9127c9e7a6fSAndy Grover 91302eb924fSAndy Grover static sense_reason_t 91402eb924fSAndy Grover tcmu_queue_cmd(struct se_cmd *se_cmd) 9157c9e7a6fSAndy Grover { 9167c9e7a6fSAndy Grover struct se_device *se_dev = se_cmd->se_dev; 9177c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(se_dev); 9187c9e7a6fSAndy Grover struct tcmu_cmd *tcmu_cmd; 919ecaf597bSBart Van Assche sense_reason_t ret; 9207c9e7a6fSAndy Grover 9217c9e7a6fSAndy Grover tcmu_cmd = tcmu_alloc_cmd(se_cmd); 9227c9e7a6fSAndy Grover if (!tcmu_cmd) 92302eb924fSAndy Grover return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 9247c9e7a6fSAndy Grover 9257c9e7a6fSAndy Grover ret = tcmu_queue_cmd_ring(tcmu_cmd); 92602eb924fSAndy Grover if (ret != TCM_NO_SENSE) { 9277c9e7a6fSAndy Grover pr_err("TCMU: Could not queue command\n"); 9287c9e7a6fSAndy Grover spin_lock_irq(&udev->commands_lock); 9297c9e7a6fSAndy Grover idr_remove(&udev->commands, tcmu_cmd->cmd_id); 9307c9e7a6fSAndy Grover spin_unlock_irq(&udev->commands_lock); 9317c9e7a6fSAndy Grover 932141685a3SXiubo Li tcmu_free_cmd(tcmu_cmd); 9337c9e7a6fSAndy Grover } 9347c9e7a6fSAndy Grover 9357c9e7a6fSAndy Grover return ret; 9367c9e7a6fSAndy Grover } 9377c9e7a6fSAndy Grover 9387c9e7a6fSAndy Grover static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry) 9397c9e7a6fSAndy Grover { 9407c9e7a6fSAndy Grover struct se_cmd *se_cmd = cmd->se_cmd; 9417c9e7a6fSAndy Grover struct tcmu_dev *udev = cmd->tcmu_dev; 9427c9e7a6fSAndy Grover 943b25c7863SSheng Yang /* 944b25c7863SSheng Yang * cmd has been completed already from timeout, just reclaim 9453d9b9555SAndy Grover * data area space and free cmd 946b25c7863SSheng Yang */ 947141685a3SXiubo Li if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) 948141685a3SXiubo Li goto out; 949b25c7863SSheng Yang 950141685a3SXiubo Li tcmu_cmd_reset_dbi_cur(cmd); 9517c9e7a6fSAndy Grover 9520ad46af8SAndy Grover if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) { 9530ad46af8SAndy Grover pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n", 9540ad46af8SAndy Grover cmd->se_cmd); 955ed97d0cdSAndy Grover entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION; 956ed97d0cdSAndy Grover } else if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) { 957406f74c2SMike Christie transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer); 958e4648b01SIlias Tsitsimpis } else if (se_cmd->se_cmd_flags & SCF_BIDI) { 95926418649SSheng Yang /* Get Data-In buffer before clean up */ 960a5d68ba8SXiubo Li gather_data_area(udev, cmd, true); 961e4648b01SIlias Tsitsimpis } else if (se_cmd->data_direction == DMA_FROM_DEVICE) { 962a5d68ba8SXiubo Li gather_data_area(udev, cmd, false); 9637c9e7a6fSAndy Grover } else if (se_cmd->data_direction == DMA_TO_DEVICE) { 964141685a3SXiubo Li /* TODO: */ 9652bc396a2SIlias Tsitsimpis } else if (se_cmd->data_direction != DMA_NONE) { 9662bc396a2SIlias Tsitsimpis pr_warn("TCMU: data direction was %d!\n", 9672bc396a2SIlias Tsitsimpis se_cmd->data_direction); 9687c9e7a6fSAndy Grover } 9697c9e7a6fSAndy Grover 9707c9e7a6fSAndy Grover target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status); 9717c9e7a6fSAndy Grover 972141685a3SXiubo Li out: 973141685a3SXiubo Li cmd->se_cmd = NULL; 974b6df4b79SXiubo Li tcmu_cmd_free_data(cmd, cmd->dbi_cnt); 975141685a3SXiubo Li tcmu_free_cmd(cmd); 9767c9e7a6fSAndy Grover } 9777c9e7a6fSAndy Grover 9787c9e7a6fSAndy Grover static unsigned int tcmu_handle_completions(struct tcmu_dev *udev) 9797c9e7a6fSAndy Grover { 9807c9e7a6fSAndy Grover struct tcmu_mailbox *mb; 9817c9e7a6fSAndy Grover int handled = 0; 9827c9e7a6fSAndy Grover 9837c9e7a6fSAndy Grover if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) { 9847c9e7a6fSAndy Grover pr_err("ring broken, not handling completions\n"); 9857c9e7a6fSAndy Grover return 0; 9867c9e7a6fSAndy Grover } 9877c9e7a6fSAndy Grover 9887c9e7a6fSAndy Grover mb = udev->mb_addr; 9897c9e7a6fSAndy Grover tcmu_flush_dcache_range(mb, sizeof(*mb)); 9907c9e7a6fSAndy Grover 9917c9e7a6fSAndy Grover while (udev->cmdr_last_cleaned != ACCESS_ONCE(mb->cmd_tail)) { 9927c9e7a6fSAndy Grover 9937c9e7a6fSAndy Grover struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned; 9947c9e7a6fSAndy Grover struct tcmu_cmd *cmd; 9957c9e7a6fSAndy Grover 9967c9e7a6fSAndy Grover tcmu_flush_dcache_range(entry, sizeof(*entry)); 9977c9e7a6fSAndy Grover 9980ad46af8SAndy Grover if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) { 9990ad46af8SAndy Grover UPDATE_HEAD(udev->cmdr_last_cleaned, 10000ad46af8SAndy Grover tcmu_hdr_get_len(entry->hdr.len_op), 10010ad46af8SAndy Grover udev->cmdr_size); 10027c9e7a6fSAndy Grover continue; 10037c9e7a6fSAndy Grover } 10040ad46af8SAndy Grover WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD); 10057c9e7a6fSAndy Grover 10067c9e7a6fSAndy Grover spin_lock(&udev->commands_lock); 1007d3e709e6SMatthew Wilcox cmd = idr_remove(&udev->commands, entry->hdr.cmd_id); 10087c9e7a6fSAndy Grover spin_unlock(&udev->commands_lock); 10097c9e7a6fSAndy Grover 10107c9e7a6fSAndy Grover if (!cmd) { 10117c9e7a6fSAndy Grover pr_err("cmd_id not found, ring is broken\n"); 10127c9e7a6fSAndy Grover set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags); 10137c9e7a6fSAndy Grover break; 10147c9e7a6fSAndy Grover } 10157c9e7a6fSAndy Grover 10167c9e7a6fSAndy Grover tcmu_handle_completion(cmd, entry); 10177c9e7a6fSAndy Grover 10180ad46af8SAndy Grover UPDATE_HEAD(udev->cmdr_last_cleaned, 10190ad46af8SAndy Grover tcmu_hdr_get_len(entry->hdr.len_op), 10200ad46af8SAndy Grover udev->cmdr_size); 10217c9e7a6fSAndy Grover 10227c9e7a6fSAndy Grover handled++; 10237c9e7a6fSAndy Grover } 10247c9e7a6fSAndy Grover 10257c9e7a6fSAndy Grover if (mb->cmd_tail == mb->cmd_head) 10267c9e7a6fSAndy Grover del_timer(&udev->timeout); /* no more pending cmds */ 10277c9e7a6fSAndy Grover 10287c9e7a6fSAndy Grover wake_up(&udev->wait_cmdr); 10297c9e7a6fSAndy Grover 10307c9e7a6fSAndy Grover return handled; 10317c9e7a6fSAndy Grover } 10327c9e7a6fSAndy Grover 10337c9e7a6fSAndy Grover static int tcmu_check_expired_cmd(int id, void *p, void *data) 10347c9e7a6fSAndy Grover { 10357c9e7a6fSAndy Grover struct tcmu_cmd *cmd = p; 10367c9e7a6fSAndy Grover 10377c9e7a6fSAndy Grover if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) 10387c9e7a6fSAndy Grover return 0; 10397c9e7a6fSAndy Grover 1040611e2267SAndy Grover if (!time_after(jiffies, cmd->deadline)) 10417c9e7a6fSAndy Grover return 0; 10427c9e7a6fSAndy Grover 10437c9e7a6fSAndy Grover set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags); 10447c9e7a6fSAndy Grover target_complete_cmd(cmd->se_cmd, SAM_STAT_CHECK_CONDITION); 10457c9e7a6fSAndy Grover cmd->se_cmd = NULL; 10467c9e7a6fSAndy Grover 10477c9e7a6fSAndy Grover return 0; 10487c9e7a6fSAndy Grover } 10497c9e7a6fSAndy Grover 10507c9e7a6fSAndy Grover static void tcmu_device_timedout(unsigned long data) 10517c9e7a6fSAndy Grover { 10527c9e7a6fSAndy Grover struct tcmu_dev *udev = (struct tcmu_dev *)data; 10537c9e7a6fSAndy Grover unsigned long flags; 10547c9e7a6fSAndy Grover 10557c9e7a6fSAndy Grover spin_lock_irqsave(&udev->commands_lock, flags); 10567c9e7a6fSAndy Grover idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL); 10577c9e7a6fSAndy Grover spin_unlock_irqrestore(&udev->commands_lock, flags); 10587c9e7a6fSAndy Grover 1059b6df4b79SXiubo Li /* Try to wake up the ummap thread */ 1060b6df4b79SXiubo Li wake_up(&unmap_wait); 1061b6df4b79SXiubo Li 10627c9e7a6fSAndy Grover /* 10637c9e7a6fSAndy Grover * We don't need to wakeup threads on wait_cmdr since they have their 10647c9e7a6fSAndy Grover * own timeout. 10657c9e7a6fSAndy Grover */ 10667c9e7a6fSAndy Grover } 10677c9e7a6fSAndy Grover 10687c9e7a6fSAndy Grover static int tcmu_attach_hba(struct se_hba *hba, u32 host_id) 10697c9e7a6fSAndy Grover { 10707c9e7a6fSAndy Grover struct tcmu_hba *tcmu_hba; 10717c9e7a6fSAndy Grover 10727c9e7a6fSAndy Grover tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL); 10737c9e7a6fSAndy Grover if (!tcmu_hba) 10747c9e7a6fSAndy Grover return -ENOMEM; 10757c9e7a6fSAndy Grover 10767c9e7a6fSAndy Grover tcmu_hba->host_id = host_id; 10777c9e7a6fSAndy Grover hba->hba_ptr = tcmu_hba; 10787c9e7a6fSAndy Grover 10797c9e7a6fSAndy Grover return 0; 10807c9e7a6fSAndy Grover } 10817c9e7a6fSAndy Grover 10827c9e7a6fSAndy Grover static void tcmu_detach_hba(struct se_hba *hba) 10837c9e7a6fSAndy Grover { 10847c9e7a6fSAndy Grover kfree(hba->hba_ptr); 10857c9e7a6fSAndy Grover hba->hba_ptr = NULL; 10867c9e7a6fSAndy Grover } 10877c9e7a6fSAndy Grover 10887c9e7a6fSAndy Grover static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name) 10897c9e7a6fSAndy Grover { 10907c9e7a6fSAndy Grover struct tcmu_dev *udev; 10917c9e7a6fSAndy Grover 10927c9e7a6fSAndy Grover udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL); 10937c9e7a6fSAndy Grover if (!udev) 10947c9e7a6fSAndy Grover return NULL; 1095f3cdbe39SMike Christie kref_init(&udev->kref); 10967c9e7a6fSAndy Grover 10977c9e7a6fSAndy Grover udev->name = kstrdup(name, GFP_KERNEL); 10987c9e7a6fSAndy Grover if (!udev->name) { 10997c9e7a6fSAndy Grover kfree(udev); 11007c9e7a6fSAndy Grover return NULL; 11017c9e7a6fSAndy Grover } 11027c9e7a6fSAndy Grover 11037c9e7a6fSAndy Grover udev->hba = hba; 1104af980e46SMike Christie udev->cmd_time_out = TCMU_TIME_OUT; 11057c9e7a6fSAndy Grover 11067c9e7a6fSAndy Grover init_waitqueue_head(&udev->wait_cmdr); 1107b6df4b79SXiubo Li mutex_init(&udev->cmdr_lock); 11087c9e7a6fSAndy Grover 11097c9e7a6fSAndy Grover idr_init(&udev->commands); 11107c9e7a6fSAndy Grover spin_lock_init(&udev->commands_lock); 11117c9e7a6fSAndy Grover 11127c9e7a6fSAndy Grover setup_timer(&udev->timeout, tcmu_device_timedout, 11137c9e7a6fSAndy Grover (unsigned long)udev); 11147c9e7a6fSAndy Grover 1115b3af66e2SMike Christie init_waitqueue_head(&udev->nl_cmd_wq); 1116b3af66e2SMike Christie spin_lock_init(&udev->nl_cmd_lock); 1117b3af66e2SMike Christie 11187c9e7a6fSAndy Grover return &udev->se_dev; 11197c9e7a6fSAndy Grover } 11207c9e7a6fSAndy Grover 11217c9e7a6fSAndy Grover static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on) 11227c9e7a6fSAndy Grover { 11237c9e7a6fSAndy Grover struct tcmu_dev *tcmu_dev = container_of(info, struct tcmu_dev, uio_info); 11247c9e7a6fSAndy Grover 1125b6df4b79SXiubo Li mutex_lock(&tcmu_dev->cmdr_lock); 11267c9e7a6fSAndy Grover tcmu_handle_completions(tcmu_dev); 1127b6df4b79SXiubo Li mutex_unlock(&tcmu_dev->cmdr_lock); 11287c9e7a6fSAndy Grover 11297c9e7a6fSAndy Grover return 0; 11307c9e7a6fSAndy Grover } 11317c9e7a6fSAndy Grover 11327c9e7a6fSAndy Grover /* 11337c9e7a6fSAndy Grover * mmap code from uio.c. Copied here because we want to hook mmap() 11347c9e7a6fSAndy Grover * and this stuff must come along. 11357c9e7a6fSAndy Grover */ 11367c9e7a6fSAndy Grover static int tcmu_find_mem_index(struct vm_area_struct *vma) 11377c9e7a6fSAndy Grover { 11387c9e7a6fSAndy Grover struct tcmu_dev *udev = vma->vm_private_data; 11397c9e7a6fSAndy Grover struct uio_info *info = &udev->uio_info; 11407c9e7a6fSAndy Grover 11417c9e7a6fSAndy Grover if (vma->vm_pgoff < MAX_UIO_MAPS) { 11427c9e7a6fSAndy Grover if (info->mem[vma->vm_pgoff].size == 0) 11437c9e7a6fSAndy Grover return -1; 11447c9e7a6fSAndy Grover return (int)vma->vm_pgoff; 11457c9e7a6fSAndy Grover } 11467c9e7a6fSAndy Grover return -1; 11477c9e7a6fSAndy Grover } 11487c9e7a6fSAndy Grover 1149b6df4b79SXiubo Li static struct page *tcmu_try_get_block_page(struct tcmu_dev *udev, uint32_t dbi) 1150b6df4b79SXiubo Li { 1151b6df4b79SXiubo Li struct page *page; 1152b6df4b79SXiubo Li int ret; 1153b6df4b79SXiubo Li 1154b6df4b79SXiubo Li mutex_lock(&udev->cmdr_lock); 1155b6df4b79SXiubo Li page = tcmu_get_block_page(udev, dbi); 1156b6df4b79SXiubo Li if (likely(page)) { 1157b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1158b6df4b79SXiubo Li return page; 1159b6df4b79SXiubo Li } 1160b6df4b79SXiubo Li 1161b6df4b79SXiubo Li /* 1162b6df4b79SXiubo Li * Normally it shouldn't be here: 1163b6df4b79SXiubo Li * Only when the userspace has touched the blocks which 1164b6df4b79SXiubo Li * are out of the tcmu_cmd's data iov[], and will return 1165b6df4b79SXiubo Li * one zeroed page. 1166b6df4b79SXiubo Li */ 1167b6df4b79SXiubo Li pr_warn("Block(%u) out of cmd's iov[] has been touched!\n", dbi); 1168b6df4b79SXiubo Li pr_warn("Mostly it will be a bug of userspace, please have a check!\n"); 1169b6df4b79SXiubo Li 1170b6df4b79SXiubo Li if (dbi >= udev->dbi_thresh) { 1171b6df4b79SXiubo Li /* Extern the udev->dbi_thresh to dbi + 1 */ 1172b6df4b79SXiubo Li udev->dbi_thresh = dbi + 1; 1173b6df4b79SXiubo Li udev->dbi_max = dbi; 1174b6df4b79SXiubo Li } 1175b6df4b79SXiubo Li 1176b6df4b79SXiubo Li page = radix_tree_lookup(&udev->data_blocks, dbi); 1177b6df4b79SXiubo Li if (!page) { 1178b6df4b79SXiubo Li page = alloc_page(GFP_KERNEL | __GFP_ZERO); 1179b6df4b79SXiubo Li if (!page) { 1180b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1181b6df4b79SXiubo Li return NULL; 1182b6df4b79SXiubo Li } 1183b6df4b79SXiubo Li 1184b6df4b79SXiubo Li ret = radix_tree_insert(&udev->data_blocks, dbi, page); 1185b6df4b79SXiubo Li if (ret) { 1186b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1187b6df4b79SXiubo Li __free_page(page); 1188b6df4b79SXiubo Li return NULL; 1189b6df4b79SXiubo Li } 1190b6df4b79SXiubo Li 1191b6df4b79SXiubo Li /* 1192b6df4b79SXiubo Li * Since this case is rare in page fault routine, here we 1193b6df4b79SXiubo Li * will allow the global_db_count >= TCMU_GLOBAL_MAX_BLOCKS 1194b6df4b79SXiubo Li * to reduce possible page fault call trace. 1195b6df4b79SXiubo Li */ 1196b6df4b79SXiubo Li atomic_inc(&global_db_count); 1197b6df4b79SXiubo Li } 1198b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1199b6df4b79SXiubo Li 1200b6df4b79SXiubo Li return page; 1201b6df4b79SXiubo Li } 1202b6df4b79SXiubo Li 120311bac800SDave Jiang static int tcmu_vma_fault(struct vm_fault *vmf) 12047c9e7a6fSAndy Grover { 120511bac800SDave Jiang struct tcmu_dev *udev = vmf->vma->vm_private_data; 12067c9e7a6fSAndy Grover struct uio_info *info = &udev->uio_info; 12077c9e7a6fSAndy Grover struct page *page; 12087c9e7a6fSAndy Grover unsigned long offset; 12097c9e7a6fSAndy Grover void *addr; 12107c9e7a6fSAndy Grover 121111bac800SDave Jiang int mi = tcmu_find_mem_index(vmf->vma); 12127c9e7a6fSAndy Grover if (mi < 0) 12137c9e7a6fSAndy Grover return VM_FAULT_SIGBUS; 12147c9e7a6fSAndy Grover 12157c9e7a6fSAndy Grover /* 12167c9e7a6fSAndy Grover * We need to subtract mi because userspace uses offset = N*PAGE_SIZE 12177c9e7a6fSAndy Grover * to use mem[N]. 12187c9e7a6fSAndy Grover */ 12197c9e7a6fSAndy Grover offset = (vmf->pgoff - mi) << PAGE_SHIFT; 12207c9e7a6fSAndy Grover 1221141685a3SXiubo Li if (offset < udev->data_off) { 1222141685a3SXiubo Li /* For the vmalloc()ed cmd area pages */ 12237c9e7a6fSAndy Grover addr = (void *)(unsigned long)info->mem[mi].addr + offset; 12247c9e7a6fSAndy Grover page = vmalloc_to_page(addr); 1225141685a3SXiubo Li } else { 1226141685a3SXiubo Li uint32_t dbi; 1227141685a3SXiubo Li 1228b6df4b79SXiubo Li /* For the dynamically growing data area pages */ 1229141685a3SXiubo Li dbi = (offset - udev->data_off) / DATA_BLOCK_SIZE; 1230b6df4b79SXiubo Li page = tcmu_try_get_block_page(udev, dbi); 1231b6df4b79SXiubo Li if (!page) 1232141685a3SXiubo Li return VM_FAULT_NOPAGE; 1233141685a3SXiubo Li } 1234141685a3SXiubo Li 12357c9e7a6fSAndy Grover get_page(page); 12367c9e7a6fSAndy Grover vmf->page = page; 12377c9e7a6fSAndy Grover return 0; 12387c9e7a6fSAndy Grover } 12397c9e7a6fSAndy Grover 12407c9e7a6fSAndy Grover static const struct vm_operations_struct tcmu_vm_ops = { 12417c9e7a6fSAndy Grover .fault = tcmu_vma_fault, 12427c9e7a6fSAndy Grover }; 12437c9e7a6fSAndy Grover 12447c9e7a6fSAndy Grover static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma) 12457c9e7a6fSAndy Grover { 12467c9e7a6fSAndy Grover struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 12477c9e7a6fSAndy Grover 12487c9e7a6fSAndy Grover vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 12497c9e7a6fSAndy Grover vma->vm_ops = &tcmu_vm_ops; 12507c9e7a6fSAndy Grover 12517c9e7a6fSAndy Grover vma->vm_private_data = udev; 12527c9e7a6fSAndy Grover 12537c9e7a6fSAndy Grover /* Ensure the mmap is exactly the right size */ 12547c9e7a6fSAndy Grover if (vma_pages(vma) != (TCMU_RING_SIZE >> PAGE_SHIFT)) 12557c9e7a6fSAndy Grover return -EINVAL; 12567c9e7a6fSAndy Grover 12577c9e7a6fSAndy Grover return 0; 12587c9e7a6fSAndy Grover } 12597c9e7a6fSAndy Grover 12607c9e7a6fSAndy Grover static int tcmu_open(struct uio_info *info, struct inode *inode) 12617c9e7a6fSAndy Grover { 12627c9e7a6fSAndy Grover struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 12637c9e7a6fSAndy Grover 12647c9e7a6fSAndy Grover /* O_EXCL not supported for char devs, so fake it? */ 12657c9e7a6fSAndy Grover if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags)) 12667c9e7a6fSAndy Grover return -EBUSY; 12677c9e7a6fSAndy Grover 1268b6df4b79SXiubo Li udev->inode = inode; 12699260695dSMike Christie kref_get(&udev->kref); 1270b6df4b79SXiubo Li 12717c9e7a6fSAndy Grover pr_debug("open\n"); 12727c9e7a6fSAndy Grover 12737c9e7a6fSAndy Grover return 0; 12747c9e7a6fSAndy Grover } 12757c9e7a6fSAndy Grover 1276f3cdbe39SMike Christie static void tcmu_dev_call_rcu(struct rcu_head *p) 1277f3cdbe39SMike Christie { 1278f3cdbe39SMike Christie struct se_device *dev = container_of(p, struct se_device, rcu_head); 1279f3cdbe39SMike Christie struct tcmu_dev *udev = TCMU_DEV(dev); 1280f3cdbe39SMike Christie 1281f3cdbe39SMike Christie kfree(udev->uio_info.name); 1282f3cdbe39SMike Christie kfree(udev->name); 1283f3cdbe39SMike Christie kfree(udev); 1284f3cdbe39SMike Christie } 1285f3cdbe39SMike Christie 1286f3cdbe39SMike Christie static void tcmu_dev_kref_release(struct kref *kref) 1287f3cdbe39SMike Christie { 1288f3cdbe39SMike Christie struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref); 1289f3cdbe39SMike Christie struct se_device *dev = &udev->se_dev; 1290f3cdbe39SMike Christie 1291f3cdbe39SMike Christie call_rcu(&dev->rcu_head, tcmu_dev_call_rcu); 1292f3cdbe39SMike Christie } 1293f3cdbe39SMike Christie 12947c9e7a6fSAndy Grover static int tcmu_release(struct uio_info *info, struct inode *inode) 12957c9e7a6fSAndy Grover { 12967c9e7a6fSAndy Grover struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 12977c9e7a6fSAndy Grover 12987c9e7a6fSAndy Grover clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags); 12997c9e7a6fSAndy Grover 13007c9e7a6fSAndy Grover pr_debug("close\n"); 13019260695dSMike Christie /* release ref from open */ 1302f3cdbe39SMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 13037c9e7a6fSAndy Grover return 0; 13047c9e7a6fSAndy Grover } 13057c9e7a6fSAndy Grover 1306b3af66e2SMike Christie static void tcmu_init_genl_cmd_reply(struct tcmu_dev *udev, int cmd) 1307b3af66e2SMike Christie { 1308b3af66e2SMike Christie struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; 1309b3af66e2SMike Christie 1310b3af66e2SMike Christie if (!tcmu_kern_cmd_reply_supported) 1311b3af66e2SMike Christie return; 1312b3af66e2SMike Christie relock: 1313b3af66e2SMike Christie spin_lock(&udev->nl_cmd_lock); 1314b3af66e2SMike Christie 1315b3af66e2SMike Christie if (nl_cmd->cmd != TCMU_CMD_UNSPEC) { 1316b3af66e2SMike Christie spin_unlock(&udev->nl_cmd_lock); 1317b3af66e2SMike Christie pr_debug("sleeping for open nl cmd\n"); 1318b3af66e2SMike Christie wait_event(udev->nl_cmd_wq, (nl_cmd->cmd == TCMU_CMD_UNSPEC)); 1319b3af66e2SMike Christie goto relock; 1320b3af66e2SMike Christie } 1321b3af66e2SMike Christie 1322b3af66e2SMike Christie memset(nl_cmd, 0, sizeof(*nl_cmd)); 1323b3af66e2SMike Christie nl_cmd->cmd = cmd; 1324b3af66e2SMike Christie init_completion(&nl_cmd->complete); 1325b3af66e2SMike Christie 1326b3af66e2SMike Christie spin_unlock(&udev->nl_cmd_lock); 1327b3af66e2SMike Christie } 1328b3af66e2SMike Christie 1329b3af66e2SMike Christie static int tcmu_wait_genl_cmd_reply(struct tcmu_dev *udev) 1330b3af66e2SMike Christie { 1331b3af66e2SMike Christie struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; 1332b3af66e2SMike Christie int ret; 1333b3af66e2SMike Christie DEFINE_WAIT(__wait); 1334b3af66e2SMike Christie 1335b3af66e2SMike Christie if (!tcmu_kern_cmd_reply_supported) 1336b3af66e2SMike Christie return 0; 1337b3af66e2SMike Christie 1338b3af66e2SMike Christie pr_debug("sleeping for nl reply\n"); 1339b3af66e2SMike Christie wait_for_completion(&nl_cmd->complete); 1340b3af66e2SMike Christie 1341b3af66e2SMike Christie spin_lock(&udev->nl_cmd_lock); 1342b3af66e2SMike Christie nl_cmd->cmd = TCMU_CMD_UNSPEC; 1343b3af66e2SMike Christie ret = nl_cmd->status; 1344b3af66e2SMike Christie nl_cmd->status = 0; 1345b3af66e2SMike Christie spin_unlock(&udev->nl_cmd_lock); 1346b3af66e2SMike Christie 1347b3af66e2SMike Christie wake_up_all(&udev->nl_cmd_wq); 1348b3af66e2SMike Christie 1349b3af66e2SMike Christie return ret;; 1350b3af66e2SMike Christie } 1351b3af66e2SMike Christie 1352b3af66e2SMike Christie static int tcmu_netlink_event(struct tcmu_dev *udev, enum tcmu_genl_cmd cmd, 1353b3af66e2SMike Christie int reconfig_attr, const void *reconfig_data) 13547c9e7a6fSAndy Grover { 13557c9e7a6fSAndy Grover struct sk_buff *skb; 13567c9e7a6fSAndy Grover void *msg_header; 13576e14eab9SNicholas Bellinger int ret = -ENOMEM; 13587c9e7a6fSAndy Grover 13597c9e7a6fSAndy Grover skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 13607c9e7a6fSAndy Grover if (!skb) 13616e14eab9SNicholas Bellinger return ret; 13627c9e7a6fSAndy Grover 13637c9e7a6fSAndy Grover msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd); 13646e14eab9SNicholas Bellinger if (!msg_header) 13656e14eab9SNicholas Bellinger goto free_skb; 13667c9e7a6fSAndy Grover 1367b3af66e2SMike Christie ret = nla_put_string(skb, TCMU_ATTR_DEVICE, udev->uio_info.name); 13686e14eab9SNicholas Bellinger if (ret < 0) 13696e14eab9SNicholas Bellinger goto free_skb; 13707c9e7a6fSAndy Grover 1371b3af66e2SMike Christie ret = nla_put_u32(skb, TCMU_ATTR_MINOR, udev->uio_info.uio_dev->minor); 1372b3af66e2SMike Christie if (ret < 0) 1373b3af66e2SMike Christie goto free_skb; 1374b3af66e2SMike Christie 1375b3af66e2SMike Christie ret = nla_put_u32(skb, TCMU_ATTR_DEVICE_ID, udev->se_dev.dev_index); 13766e14eab9SNicholas Bellinger if (ret < 0) 13776e14eab9SNicholas Bellinger goto free_skb; 13787c9e7a6fSAndy Grover 13792d76443eSMike Christie if (cmd == TCMU_CMD_RECONFIG_DEVICE) { 13802d76443eSMike Christie switch (reconfig_attr) { 13812d76443eSMike Christie case TCMU_ATTR_DEV_CFG: 13822d76443eSMike Christie ret = nla_put_string(skb, reconfig_attr, reconfig_data); 13832d76443eSMike Christie break; 13842d76443eSMike Christie case TCMU_ATTR_DEV_SIZE: 13852d76443eSMike Christie ret = nla_put_u64_64bit(skb, reconfig_attr, 13862d76443eSMike Christie *((u64 *)reconfig_data), 13872d76443eSMike Christie TCMU_ATTR_PAD); 13882d76443eSMike Christie break; 13892d76443eSMike Christie case TCMU_ATTR_WRITECACHE: 13902d76443eSMike Christie ret = nla_put_u8(skb, reconfig_attr, 13912d76443eSMike Christie *((u8 *)reconfig_data)); 13922d76443eSMike Christie break; 13932d76443eSMike Christie default: 13942d76443eSMike Christie BUG(); 13952d76443eSMike Christie } 13962d76443eSMike Christie 13978a45885cSBryant G. Ly if (ret < 0) 13988a45885cSBryant G. Ly goto free_skb; 13992d76443eSMike Christie } 14008a45885cSBryant G. Ly 1401053c095aSJohannes Berg genlmsg_end(skb, msg_header); 14027c9e7a6fSAndy Grover 1403b3af66e2SMike Christie tcmu_init_genl_cmd_reply(udev, cmd); 1404b3af66e2SMike Christie 140520c08b36SSheng Yang ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0, 14067c9e7a6fSAndy Grover TCMU_MCGRP_CONFIG, GFP_KERNEL); 14077c9e7a6fSAndy Grover /* We don't care if no one is listening */ 14087c9e7a6fSAndy Grover if (ret == -ESRCH) 14097c9e7a6fSAndy Grover ret = 0; 1410b3af66e2SMike Christie if (!ret) 1411b3af66e2SMike Christie ret = tcmu_wait_genl_cmd_reply(udev); 14127c9e7a6fSAndy Grover 14137c9e7a6fSAndy Grover return ret; 14146e14eab9SNicholas Bellinger free_skb: 14156e14eab9SNicholas Bellinger nlmsg_free(skb); 14166e14eab9SNicholas Bellinger return ret; 14177c9e7a6fSAndy Grover } 14187c9e7a6fSAndy Grover 1419de8c5221SBryant G. Ly static int tcmu_update_uio_info(struct tcmu_dev *udev) 14207c9e7a6fSAndy Grover { 14217c9e7a6fSAndy Grover struct tcmu_hba *hba = udev->hba->hba_ptr; 14227c9e7a6fSAndy Grover struct uio_info *info; 1423de8c5221SBryant G. Ly size_t size, used; 14247c9e7a6fSAndy Grover char *str; 14257c9e7a6fSAndy Grover 14267c9e7a6fSAndy Grover info = &udev->uio_info; 14277c9e7a6fSAndy Grover size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name, 14287c9e7a6fSAndy Grover udev->dev_config); 14297c9e7a6fSAndy Grover size += 1; /* for \0 */ 14307c9e7a6fSAndy Grover str = kmalloc(size, GFP_KERNEL); 14317c9e7a6fSAndy Grover if (!str) 14327c9e7a6fSAndy Grover return -ENOMEM; 14337c9e7a6fSAndy Grover 14347c9e7a6fSAndy Grover used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name); 14357c9e7a6fSAndy Grover if (udev->dev_config[0]) 14367c9e7a6fSAndy Grover snprintf(str + used, size - used, "/%s", udev->dev_config); 14377c9e7a6fSAndy Grover 14387c9e7a6fSAndy Grover info->name = str; 14397c9e7a6fSAndy Grover 1440de8c5221SBryant G. Ly return 0; 1441de8c5221SBryant G. Ly } 1442de8c5221SBryant G. Ly 1443de8c5221SBryant G. Ly static int tcmu_configure_device(struct se_device *dev) 1444de8c5221SBryant G. Ly { 1445de8c5221SBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(dev); 1446de8c5221SBryant G. Ly struct uio_info *info; 1447de8c5221SBryant G. Ly struct tcmu_mailbox *mb; 1448de8c5221SBryant G. Ly int ret = 0; 1449de8c5221SBryant G. Ly 1450de8c5221SBryant G. Ly ret = tcmu_update_uio_info(udev); 1451de8c5221SBryant G. Ly if (ret) 1452de8c5221SBryant G. Ly return ret; 1453de8c5221SBryant G. Ly 1454de8c5221SBryant G. Ly info = &udev->uio_info; 1455de8c5221SBryant G. Ly 1456141685a3SXiubo Li udev->mb_addr = vzalloc(CMDR_SIZE); 14577c9e7a6fSAndy Grover if (!udev->mb_addr) { 14587c9e7a6fSAndy Grover ret = -ENOMEM; 14597c9e7a6fSAndy Grover goto err_vzalloc; 14607c9e7a6fSAndy Grover } 14617c9e7a6fSAndy Grover 14627c9e7a6fSAndy Grover /* mailbox fits in first part of CMDR space */ 14637c9e7a6fSAndy Grover udev->cmdr_size = CMDR_SIZE - CMDR_OFF; 14647c9e7a6fSAndy Grover udev->data_off = CMDR_SIZE; 1465141685a3SXiubo Li udev->data_size = DATA_SIZE; 1466b6df4b79SXiubo Li udev->dbi_thresh = 0; /* Default in Idle state */ 1467b6df4b79SXiubo Li udev->waiting_global = false; 14687c9e7a6fSAndy Grover 1469141685a3SXiubo Li /* Initialise the mailbox of the ring buffer */ 14707c9e7a6fSAndy Grover mb = udev->mb_addr; 14710ad46af8SAndy Grover mb->version = TCMU_MAILBOX_VERSION; 147232c76de3SSheng Yang mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC; 14737c9e7a6fSAndy Grover mb->cmdr_off = CMDR_OFF; 14747c9e7a6fSAndy Grover mb->cmdr_size = udev->cmdr_size; 14757c9e7a6fSAndy Grover 14767c9e7a6fSAndy Grover WARN_ON(!PAGE_ALIGNED(udev->data_off)); 14777c9e7a6fSAndy Grover WARN_ON(udev->data_size % PAGE_SIZE); 147826418649SSheng Yang WARN_ON(udev->data_size % DATA_BLOCK_SIZE); 14797c9e7a6fSAndy Grover 1480b6df4b79SXiubo Li INIT_RADIX_TREE(&udev->data_blocks, GFP_KERNEL); 1481141685a3SXiubo Li 1482ac64a2ceSDavid Disseldorp info->version = __stringify(TCMU_MAILBOX_VERSION); 14837c9e7a6fSAndy Grover 14847c9e7a6fSAndy Grover info->mem[0].name = "tcm-user command & data buffer"; 14850633e123SArnd Bergmann info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr; 14867c9e7a6fSAndy Grover info->mem[0].size = TCMU_RING_SIZE; 1487141685a3SXiubo Li info->mem[0].memtype = UIO_MEM_NONE; 14887c9e7a6fSAndy Grover 14897c9e7a6fSAndy Grover info->irqcontrol = tcmu_irqcontrol; 14907c9e7a6fSAndy Grover info->irq = UIO_IRQ_CUSTOM; 14917c9e7a6fSAndy Grover 14927c9e7a6fSAndy Grover info->mmap = tcmu_mmap; 14937c9e7a6fSAndy Grover info->open = tcmu_open; 14947c9e7a6fSAndy Grover info->release = tcmu_release; 14957c9e7a6fSAndy Grover 14967c9e7a6fSAndy Grover ret = uio_register_device(tcmu_root_device, info); 14977c9e7a6fSAndy Grover if (ret) 14987c9e7a6fSAndy Grover goto err_register; 14997c9e7a6fSAndy Grover 150081ee28deSSheng Yang /* User can set hw_block_size before enable the device */ 150181ee28deSSheng Yang if (dev->dev_attrib.hw_block_size == 0) 15027c9e7a6fSAndy Grover dev->dev_attrib.hw_block_size = 512; 150381ee28deSSheng Yang /* Other attributes can be configured in userspace */ 15043abaa2bfSMike Christie if (!dev->dev_attrib.hw_max_sectors) 15057c9e7a6fSAndy Grover dev->dev_attrib.hw_max_sectors = 128; 15069a8bb606SBryant G. Ly if (!dev->dev_attrib.emulate_write_cache) 15079a8bb606SBryant G. Ly dev->dev_attrib.emulate_write_cache = 0; 15087c9e7a6fSAndy Grover dev->dev_attrib.hw_queue_depth = 128; 15097c9e7a6fSAndy Grover 1510f3cdbe39SMike Christie /* 1511f3cdbe39SMike Christie * Get a ref incase userspace does a close on the uio device before 1512f3cdbe39SMike Christie * LIO has initiated tcmu_free_device. 1513f3cdbe39SMike Christie */ 1514f3cdbe39SMike Christie kref_get(&udev->kref); 1515f3cdbe39SMike Christie 1516b3af66e2SMike Christie ret = tcmu_netlink_event(udev, TCMU_CMD_ADDED_DEVICE, 0, NULL); 15177c9e7a6fSAndy Grover if (ret) 15187c9e7a6fSAndy Grover goto err_netlink; 15197c9e7a6fSAndy Grover 1520b6df4b79SXiubo Li mutex_lock(&root_udev_mutex); 1521b6df4b79SXiubo Li list_add(&udev->node, &root_udev); 1522b6df4b79SXiubo Li mutex_unlock(&root_udev_mutex); 1523b6df4b79SXiubo Li 15247c9e7a6fSAndy Grover return 0; 15257c9e7a6fSAndy Grover 15267c9e7a6fSAndy Grover err_netlink: 1527f3cdbe39SMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 15287c9e7a6fSAndy Grover uio_unregister_device(&udev->uio_info); 15297c9e7a6fSAndy Grover err_register: 15307c9e7a6fSAndy Grover vfree(udev->mb_addr); 15317c9e7a6fSAndy Grover err_vzalloc: 15327c9e7a6fSAndy Grover kfree(info->name); 1533f3cdbe39SMike Christie info->name = NULL; 15347c9e7a6fSAndy Grover 15357c9e7a6fSAndy Grover return ret; 15367c9e7a6fSAndy Grover } 15377c9e7a6fSAndy Grover 1538b25c7863SSheng Yang static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd) 15397c9e7a6fSAndy Grover { 1540b25c7863SSheng Yang if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { 1541b25c7863SSheng Yang kmem_cache_free(tcmu_cmd_cache, cmd); 15427c9e7a6fSAndy Grover return 0; 1543b25c7863SSheng Yang } 15447c9e7a6fSAndy Grover return -EINVAL; 15457c9e7a6fSAndy Grover } 15467c9e7a6fSAndy Grover 1547972c7f16SMike Christie static bool tcmu_dev_configured(struct tcmu_dev *udev) 1548972c7f16SMike Christie { 1549972c7f16SMike Christie return udev->uio_info.uio_dev ? true : false; 1550972c7f16SMike Christie } 1551972c7f16SMike Christie 1552b6df4b79SXiubo Li static void tcmu_blocks_release(struct tcmu_dev *udev) 1553b6df4b79SXiubo Li { 1554b6df4b79SXiubo Li int i; 1555b6df4b79SXiubo Li struct page *page; 1556b6df4b79SXiubo Li 1557b6df4b79SXiubo Li /* Try to release all block pages */ 1558b6df4b79SXiubo Li mutex_lock(&udev->cmdr_lock); 1559b6df4b79SXiubo Li for (i = 0; i <= udev->dbi_max; i++) { 1560b6df4b79SXiubo Li page = radix_tree_delete(&udev->data_blocks, i); 1561b6df4b79SXiubo Li if (page) { 1562b6df4b79SXiubo Li __free_page(page); 1563b6df4b79SXiubo Li atomic_dec(&global_db_count); 1564b6df4b79SXiubo Li } 1565b6df4b79SXiubo Li } 1566b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1567b6df4b79SXiubo Li } 1568b6df4b79SXiubo Li 15697c9e7a6fSAndy Grover static void tcmu_free_device(struct se_device *dev) 15707c9e7a6fSAndy Grover { 15717c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 157292634706SMike Christie 157392634706SMike Christie /* release ref from init */ 157492634706SMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 157592634706SMike Christie } 157692634706SMike Christie 157792634706SMike Christie static void tcmu_destroy_device(struct se_device *dev) 157892634706SMike Christie { 157992634706SMike Christie struct tcmu_dev *udev = TCMU_DEV(dev); 1580b25c7863SSheng Yang struct tcmu_cmd *cmd; 1581b25c7863SSheng Yang bool all_expired = true; 15827c9e7a6fSAndy Grover int i; 15837c9e7a6fSAndy Grover 15847c9e7a6fSAndy Grover del_timer_sync(&udev->timeout); 15857c9e7a6fSAndy Grover 1586b6df4b79SXiubo Li mutex_lock(&root_udev_mutex); 1587b6df4b79SXiubo Li list_del(&udev->node); 1588b6df4b79SXiubo Li mutex_unlock(&root_udev_mutex); 1589b6df4b79SXiubo Li 15907c9e7a6fSAndy Grover vfree(udev->mb_addr); 15917c9e7a6fSAndy Grover 15927c9e7a6fSAndy Grover /* Upper layer should drain all requests before calling this */ 15937c9e7a6fSAndy Grover spin_lock_irq(&udev->commands_lock); 1594b25c7863SSheng Yang idr_for_each_entry(&udev->commands, cmd, i) { 1595b25c7863SSheng Yang if (tcmu_check_and_free_pending_cmd(cmd) != 0) 1596b25c7863SSheng Yang all_expired = false; 1597b25c7863SSheng Yang } 15987c9e7a6fSAndy Grover idr_destroy(&udev->commands); 15997c9e7a6fSAndy Grover spin_unlock_irq(&udev->commands_lock); 1600b25c7863SSheng Yang WARN_ON(!all_expired); 16017c9e7a6fSAndy Grover 1602b6df4b79SXiubo Li tcmu_blocks_release(udev); 1603141685a3SXiubo Li 1604b3af66e2SMike Christie tcmu_netlink_event(udev, TCMU_CMD_REMOVED_DEVICE, 0, NULL); 16057c9e7a6fSAndy Grover 16067c9e7a6fSAndy Grover uio_unregister_device(&udev->uio_info); 16079260695dSMike Christie 16089260695dSMike Christie /* release ref from configure */ 16099260695dSMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 16107c9e7a6fSAndy Grover } 16117c9e7a6fSAndy Grover 16127c9e7a6fSAndy Grover enum { 16133abaa2bfSMike Christie Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors, 16147d7a7435SNicholas Bellinger Opt_err, 16157c9e7a6fSAndy Grover }; 16167c9e7a6fSAndy Grover 16177c9e7a6fSAndy Grover static match_table_t tokens = { 16187c9e7a6fSAndy Grover {Opt_dev_config, "dev_config=%s"}, 16197c9e7a6fSAndy Grover {Opt_dev_size, "dev_size=%u"}, 16209c1cd1b6SAndy Grover {Opt_hw_block_size, "hw_block_size=%u"}, 16213abaa2bfSMike Christie {Opt_hw_max_sectors, "hw_max_sectors=%u"}, 16227c9e7a6fSAndy Grover {Opt_err, NULL} 16237c9e7a6fSAndy Grover }; 16247c9e7a6fSAndy Grover 16253abaa2bfSMike Christie static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib) 16263abaa2bfSMike Christie { 16273abaa2bfSMike Christie unsigned long tmp_ul; 16283abaa2bfSMike Christie char *arg_p; 16293abaa2bfSMike Christie int ret; 16303abaa2bfSMike Christie 16313abaa2bfSMike Christie arg_p = match_strdup(arg); 16323abaa2bfSMike Christie if (!arg_p) 16333abaa2bfSMike Christie return -ENOMEM; 16343abaa2bfSMike Christie 16353abaa2bfSMike Christie ret = kstrtoul(arg_p, 0, &tmp_ul); 16363abaa2bfSMike Christie kfree(arg_p); 16373abaa2bfSMike Christie if (ret < 0) { 16383abaa2bfSMike Christie pr_err("kstrtoul() failed for dev attrib\n"); 16393abaa2bfSMike Christie return ret; 16403abaa2bfSMike Christie } 16413abaa2bfSMike Christie if (!tmp_ul) { 16423abaa2bfSMike Christie pr_err("dev attrib must be nonzero\n"); 16433abaa2bfSMike Christie return -EINVAL; 16443abaa2bfSMike Christie } 16453abaa2bfSMike Christie *dev_attrib = tmp_ul; 16463abaa2bfSMike Christie return 0; 16473abaa2bfSMike Christie } 16483abaa2bfSMike Christie 16497c9e7a6fSAndy Grover static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev, 16507c9e7a6fSAndy Grover const char *page, ssize_t count) 16517c9e7a6fSAndy Grover { 16527c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 16537c9e7a6fSAndy Grover char *orig, *ptr, *opts, *arg_p; 16547c9e7a6fSAndy Grover substring_t args[MAX_OPT_ARGS]; 16557c9e7a6fSAndy Grover int ret = 0, token; 16567c9e7a6fSAndy Grover 16577c9e7a6fSAndy Grover opts = kstrdup(page, GFP_KERNEL); 16587c9e7a6fSAndy Grover if (!opts) 16597c9e7a6fSAndy Grover return -ENOMEM; 16607c9e7a6fSAndy Grover 16617c9e7a6fSAndy Grover orig = opts; 16627c9e7a6fSAndy Grover 16637c9e7a6fSAndy Grover while ((ptr = strsep(&opts, ",\n")) != NULL) { 16647c9e7a6fSAndy Grover if (!*ptr) 16657c9e7a6fSAndy Grover continue; 16667c9e7a6fSAndy Grover 16677c9e7a6fSAndy Grover token = match_token(ptr, tokens, args); 16687c9e7a6fSAndy Grover switch (token) { 16697c9e7a6fSAndy Grover case Opt_dev_config: 16707c9e7a6fSAndy Grover if (match_strlcpy(udev->dev_config, &args[0], 16717c9e7a6fSAndy Grover TCMU_CONFIG_LEN) == 0) { 16727c9e7a6fSAndy Grover ret = -EINVAL; 16737c9e7a6fSAndy Grover break; 16747c9e7a6fSAndy Grover } 16757c9e7a6fSAndy Grover pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config); 16767c9e7a6fSAndy Grover break; 16777c9e7a6fSAndy Grover case Opt_dev_size: 16787c9e7a6fSAndy Grover arg_p = match_strdup(&args[0]); 16797c9e7a6fSAndy Grover if (!arg_p) { 16807c9e7a6fSAndy Grover ret = -ENOMEM; 16817c9e7a6fSAndy Grover break; 16827c9e7a6fSAndy Grover } 16837c9e7a6fSAndy Grover ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size); 16847c9e7a6fSAndy Grover kfree(arg_p); 16857c9e7a6fSAndy Grover if (ret < 0) 16867c9e7a6fSAndy Grover pr_err("kstrtoul() failed for dev_size=\n"); 16877c9e7a6fSAndy Grover break; 16889c1cd1b6SAndy Grover case Opt_hw_block_size: 16893abaa2bfSMike Christie ret = tcmu_set_dev_attrib(&args[0], 16903abaa2bfSMike Christie &(dev->dev_attrib.hw_block_size)); 16919c1cd1b6SAndy Grover break; 16923abaa2bfSMike Christie case Opt_hw_max_sectors: 16933abaa2bfSMike Christie ret = tcmu_set_dev_attrib(&args[0], 16943abaa2bfSMike Christie &(dev->dev_attrib.hw_max_sectors)); 16959c1cd1b6SAndy Grover break; 16967c9e7a6fSAndy Grover default: 16977c9e7a6fSAndy Grover break; 16987c9e7a6fSAndy Grover } 16992579325cSMike Christie 17002579325cSMike Christie if (ret) 17012579325cSMike Christie break; 17027c9e7a6fSAndy Grover } 17037c9e7a6fSAndy Grover 17047c9e7a6fSAndy Grover kfree(orig); 17057c9e7a6fSAndy Grover return (!ret) ? count : ret; 17067c9e7a6fSAndy Grover } 17077c9e7a6fSAndy Grover 17087c9e7a6fSAndy Grover static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b) 17097c9e7a6fSAndy Grover { 17107c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 17117c9e7a6fSAndy Grover ssize_t bl = 0; 17127c9e7a6fSAndy Grover 17137c9e7a6fSAndy Grover bl = sprintf(b + bl, "Config: %s ", 17147c9e7a6fSAndy Grover udev->dev_config[0] ? udev->dev_config : "NULL"); 17157d7a7435SNicholas Bellinger bl += sprintf(b + bl, "Size: %zu\n", udev->dev_size); 17167c9e7a6fSAndy Grover 17177c9e7a6fSAndy Grover return bl; 17187c9e7a6fSAndy Grover } 17197c9e7a6fSAndy Grover 17207c9e7a6fSAndy Grover static sector_t tcmu_get_blocks(struct se_device *dev) 17217c9e7a6fSAndy Grover { 17227c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 17237c9e7a6fSAndy Grover 17247c9e7a6fSAndy Grover return div_u64(udev->dev_size - dev->dev_attrib.block_size, 17257c9e7a6fSAndy Grover dev->dev_attrib.block_size); 17267c9e7a6fSAndy Grover } 17277c9e7a6fSAndy Grover 17287c9e7a6fSAndy Grover static sense_reason_t 17297c9e7a6fSAndy Grover tcmu_parse_cdb(struct se_cmd *cmd) 17307c9e7a6fSAndy Grover { 173102eb924fSAndy Grover return passthrough_parse_cdb(cmd, tcmu_queue_cmd); 17329c1cd1b6SAndy Grover } 17339c1cd1b6SAndy Grover 17347d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page) 17357d7a7435SNicholas Bellinger { 17367d7a7435SNicholas Bellinger struct se_dev_attrib *da = container_of(to_config_group(item), 17377d7a7435SNicholas Bellinger struct se_dev_attrib, da_group); 17387d7a7435SNicholas Bellinger struct tcmu_dev *udev = container_of(da->da_dev, 17397d7a7435SNicholas Bellinger struct tcmu_dev, se_dev); 17407d7a7435SNicholas Bellinger 17417d7a7435SNicholas Bellinger return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC); 17427d7a7435SNicholas Bellinger } 17437d7a7435SNicholas Bellinger 17447d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page, 17457d7a7435SNicholas Bellinger size_t count) 17467d7a7435SNicholas Bellinger { 17477d7a7435SNicholas Bellinger struct se_dev_attrib *da = container_of(to_config_group(item), 17487d7a7435SNicholas Bellinger struct se_dev_attrib, da_group); 17497d7a7435SNicholas Bellinger struct tcmu_dev *udev = container_of(da->da_dev, 17507d7a7435SNicholas Bellinger struct tcmu_dev, se_dev); 17517d7a7435SNicholas Bellinger u32 val; 17527d7a7435SNicholas Bellinger int ret; 17537d7a7435SNicholas Bellinger 17547d7a7435SNicholas Bellinger if (da->da_dev->export_count) { 17557d7a7435SNicholas Bellinger pr_err("Unable to set tcmu cmd_time_out while exports exist\n"); 17567d7a7435SNicholas Bellinger return -EINVAL; 17577d7a7435SNicholas Bellinger } 17587d7a7435SNicholas Bellinger 17597d7a7435SNicholas Bellinger ret = kstrtou32(page, 0, &val); 17607d7a7435SNicholas Bellinger if (ret < 0) 17617d7a7435SNicholas Bellinger return ret; 17627d7a7435SNicholas Bellinger 17637d7a7435SNicholas Bellinger udev->cmd_time_out = val * MSEC_PER_SEC; 17647d7a7435SNicholas Bellinger return count; 17657d7a7435SNicholas Bellinger } 17667d7a7435SNicholas Bellinger CONFIGFS_ATTR(tcmu_, cmd_time_out); 17677d7a7435SNicholas Bellinger 17682d76443eSMike Christie static ssize_t tcmu_dev_config_show(struct config_item *item, char *page) 1769ee018252SBryant G. Ly { 1770ee018252SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 1771ee018252SBryant G. Ly struct se_dev_attrib, da_group); 1772ee018252SBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 1773ee018252SBryant G. Ly 1774ee018252SBryant G. Ly return snprintf(page, PAGE_SIZE, "%s\n", udev->dev_config); 1775ee018252SBryant G. Ly } 1776ee018252SBryant G. Ly 17772d76443eSMike Christie static ssize_t tcmu_dev_config_store(struct config_item *item, const char *page, 1778ee018252SBryant G. Ly size_t count) 1779ee018252SBryant G. Ly { 1780ee018252SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 1781ee018252SBryant G. Ly struct se_dev_attrib, da_group); 1782ee018252SBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 17832d76443eSMike Christie int ret, len; 1784ee018252SBryant G. Ly 17852d76443eSMike Christie len = strlen(page); 17862d76443eSMike Christie if (!len || len > TCMU_CONFIG_LEN - 1) 1787ee018252SBryant G. Ly return -EINVAL; 1788ee018252SBryant G. Ly 1789ee018252SBryant G. Ly /* Check if device has been configured before */ 1790ee018252SBryant G. Ly if (tcmu_dev_configured(udev)) { 1791b3af66e2SMike Christie ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE, 17922d76443eSMike Christie TCMU_ATTR_DEV_CFG, page); 1793ee018252SBryant G. Ly if (ret) { 1794ee018252SBryant G. Ly pr_err("Unable to reconfigure device\n"); 1795ee018252SBryant G. Ly return ret; 1796ee018252SBryant G. Ly } 1797de8c5221SBryant G. Ly strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN); 1798de8c5221SBryant G. Ly 1799de8c5221SBryant G. Ly ret = tcmu_update_uio_info(udev); 1800de8c5221SBryant G. Ly if (ret) 1801de8c5221SBryant G. Ly return ret; 1802de8c5221SBryant G. Ly return count; 1803ee018252SBryant G. Ly } 18042d76443eSMike Christie strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN); 1805ee018252SBryant G. Ly 1806ee018252SBryant G. Ly return count; 1807ee018252SBryant G. Ly } 18082d76443eSMike Christie CONFIGFS_ATTR(tcmu_, dev_config); 1809ee018252SBryant G. Ly 1810801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_show(struct config_item *item, char *page) 1811801fc54dSBryant G. Ly { 1812801fc54dSBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 1813801fc54dSBryant G. Ly struct se_dev_attrib, da_group); 1814801fc54dSBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 1815801fc54dSBryant G. Ly 1816801fc54dSBryant G. Ly return snprintf(page, PAGE_SIZE, "%zu\n", udev->dev_size); 1817801fc54dSBryant G. Ly } 1818801fc54dSBryant G. Ly 1819801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page, 1820801fc54dSBryant G. Ly size_t count) 1821801fc54dSBryant G. Ly { 1822801fc54dSBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 1823801fc54dSBryant G. Ly struct se_dev_attrib, da_group); 1824801fc54dSBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 18252d76443eSMike Christie u64 val; 1826801fc54dSBryant G. Ly int ret; 1827801fc54dSBryant G. Ly 18282d76443eSMike Christie ret = kstrtou64(page, 0, &val); 1829801fc54dSBryant G. Ly if (ret < 0) 1830801fc54dSBryant G. Ly return ret; 1831801fc54dSBryant G. Ly 1832801fc54dSBryant G. Ly /* Check if device has been configured before */ 1833801fc54dSBryant G. Ly if (tcmu_dev_configured(udev)) { 1834b3af66e2SMike Christie ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE, 18352d76443eSMike Christie TCMU_ATTR_DEV_SIZE, &val); 1836801fc54dSBryant G. Ly if (ret) { 1837801fc54dSBryant G. Ly pr_err("Unable to reconfigure device\n"); 1838801fc54dSBryant G. Ly return ret; 1839801fc54dSBryant G. Ly } 1840801fc54dSBryant G. Ly } 18412d76443eSMike Christie udev->dev_size = val; 1842801fc54dSBryant G. Ly return count; 1843801fc54dSBryant G. Ly } 1844801fc54dSBryant G. Ly CONFIGFS_ATTR(tcmu_, dev_size); 1845801fc54dSBryant G. Ly 18469a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_show(struct config_item *item, 18479a8bb606SBryant G. Ly char *page) 18489a8bb606SBryant G. Ly { 18499a8bb606SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 18509a8bb606SBryant G. Ly struct se_dev_attrib, da_group); 18519a8bb606SBryant G. Ly 18529a8bb606SBryant G. Ly return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache); 18539a8bb606SBryant G. Ly } 18549a8bb606SBryant G. Ly 18559a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_store(struct config_item *item, 18569a8bb606SBryant G. Ly const char *page, size_t count) 18579a8bb606SBryant G. Ly { 18589a8bb606SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 18599a8bb606SBryant G. Ly struct se_dev_attrib, da_group); 18601068be7bSBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 18612d76443eSMike Christie u8 val; 18629a8bb606SBryant G. Ly int ret; 18639a8bb606SBryant G. Ly 18642d76443eSMike Christie ret = kstrtou8(page, 0, &val); 18659a8bb606SBryant G. Ly if (ret < 0) 18669a8bb606SBryant G. Ly return ret; 18679a8bb606SBryant G. Ly 18681068be7bSBryant G. Ly /* Check if device has been configured before */ 18691068be7bSBryant G. Ly if (tcmu_dev_configured(udev)) { 1870b3af66e2SMike Christie ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE, 18712d76443eSMike Christie TCMU_ATTR_WRITECACHE, &val); 18721068be7bSBryant G. Ly if (ret) { 18731068be7bSBryant G. Ly pr_err("Unable to reconfigure device\n"); 18741068be7bSBryant G. Ly return ret; 18751068be7bSBryant G. Ly } 18761068be7bSBryant G. Ly } 18772d76443eSMike Christie 18782d76443eSMike Christie da->emulate_write_cache = val; 18799a8bb606SBryant G. Ly return count; 18809a8bb606SBryant G. Ly } 18819a8bb606SBryant G. Ly CONFIGFS_ATTR(tcmu_, emulate_write_cache); 18829a8bb606SBryant G. Ly 18835821783bSColin Ian King static struct configfs_attribute *tcmu_attrib_attrs[] = { 1884801fc54dSBryant G. Ly &tcmu_attr_cmd_time_out, 18852d76443eSMike Christie &tcmu_attr_dev_config, 1886801fc54dSBryant G. Ly &tcmu_attr_dev_size, 1887801fc54dSBryant G. Ly &tcmu_attr_emulate_write_cache, 1888801fc54dSBryant G. Ly NULL, 1889801fc54dSBryant G. Ly }; 1890801fc54dSBryant G. Ly 18917d7a7435SNicholas Bellinger static struct configfs_attribute **tcmu_attrs; 18927d7a7435SNicholas Bellinger 18937d7a7435SNicholas Bellinger static struct target_backend_ops tcmu_ops = { 18947c9e7a6fSAndy Grover .name = "user", 18957c9e7a6fSAndy Grover .owner = THIS_MODULE, 1896a3541703SAndy Grover .transport_flags = TRANSPORT_FLAG_PASSTHROUGH, 18977c9e7a6fSAndy Grover .attach_hba = tcmu_attach_hba, 18987c9e7a6fSAndy Grover .detach_hba = tcmu_detach_hba, 18997c9e7a6fSAndy Grover .alloc_device = tcmu_alloc_device, 19007c9e7a6fSAndy Grover .configure_device = tcmu_configure_device, 190192634706SMike Christie .destroy_device = tcmu_destroy_device, 19027c9e7a6fSAndy Grover .free_device = tcmu_free_device, 19037c9e7a6fSAndy Grover .parse_cdb = tcmu_parse_cdb, 19047c9e7a6fSAndy Grover .set_configfs_dev_params = tcmu_set_configfs_dev_params, 19057c9e7a6fSAndy Grover .show_configfs_dev_params = tcmu_show_configfs_dev_params, 19067c9e7a6fSAndy Grover .get_device_type = sbc_get_device_type, 19077c9e7a6fSAndy Grover .get_blocks = tcmu_get_blocks, 19087d7a7435SNicholas Bellinger .tb_dev_attrib_attrs = NULL, 19097c9e7a6fSAndy Grover }; 19107c9e7a6fSAndy Grover 1911b6df4b79SXiubo Li static int unmap_thread_fn(void *data) 1912b6df4b79SXiubo Li { 1913b6df4b79SXiubo Li struct tcmu_dev *udev; 1914b6df4b79SXiubo Li loff_t off; 1915b6df4b79SXiubo Li uint32_t start, end, block; 1916b6df4b79SXiubo Li struct page *page; 1917b6df4b79SXiubo Li int i; 1918b6df4b79SXiubo Li 191907932a02SXiubo Li while (!kthread_should_stop()) { 1920b6df4b79SXiubo Li DEFINE_WAIT(__wait); 1921b6df4b79SXiubo Li 1922b6df4b79SXiubo Li prepare_to_wait(&unmap_wait, &__wait, TASK_INTERRUPTIBLE); 1923b6df4b79SXiubo Li schedule(); 1924b6df4b79SXiubo Li finish_wait(&unmap_wait, &__wait); 1925b6df4b79SXiubo Li 1926d906d8afSMike Christie if (kthread_should_stop()) 1927d906d8afSMike Christie break; 1928d906d8afSMike Christie 1929b6df4b79SXiubo Li mutex_lock(&root_udev_mutex); 1930b6df4b79SXiubo Li list_for_each_entry(udev, &root_udev, node) { 1931b6df4b79SXiubo Li mutex_lock(&udev->cmdr_lock); 1932b6df4b79SXiubo Li 1933b6df4b79SXiubo Li /* Try to complete the finished commands first */ 1934b6df4b79SXiubo Li tcmu_handle_completions(udev); 1935b6df4b79SXiubo Li 1936b6df4b79SXiubo Li /* Skip the udevs waiting the global pool or in idle */ 1937b6df4b79SXiubo Li if (udev->waiting_global || !udev->dbi_thresh) { 1938b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1939b6df4b79SXiubo Li continue; 1940b6df4b79SXiubo Li } 1941b6df4b79SXiubo Li 1942b6df4b79SXiubo Li end = udev->dbi_max + 1; 1943b6df4b79SXiubo Li block = find_last_bit(udev->data_bitmap, end); 1944b6df4b79SXiubo Li if (block == udev->dbi_max) { 1945b6df4b79SXiubo Li /* 1946b6df4b79SXiubo Li * The last bit is dbi_max, so there is 1947b6df4b79SXiubo Li * no need to shrink any blocks. 1948b6df4b79SXiubo Li */ 1949b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1950b6df4b79SXiubo Li continue; 1951b6df4b79SXiubo Li } else if (block == end) { 1952b6df4b79SXiubo Li /* The current udev will goto idle state */ 1953b6df4b79SXiubo Li udev->dbi_thresh = start = 0; 1954b6df4b79SXiubo Li udev->dbi_max = 0; 1955b6df4b79SXiubo Li } else { 1956b6df4b79SXiubo Li udev->dbi_thresh = start = block + 1; 1957b6df4b79SXiubo Li udev->dbi_max = block; 1958b6df4b79SXiubo Li } 1959b6df4b79SXiubo Li 1960b6df4b79SXiubo Li /* Here will truncate the data area from off */ 1961b6df4b79SXiubo Li off = udev->data_off + start * DATA_BLOCK_SIZE; 1962b6df4b79SXiubo Li unmap_mapping_range(udev->inode->i_mapping, off, 0, 1); 1963b6df4b79SXiubo Li 1964b6df4b79SXiubo Li /* Release the block pages */ 1965b6df4b79SXiubo Li for (i = start; i < end; i++) { 1966b6df4b79SXiubo Li page = radix_tree_delete(&udev->data_blocks, i); 1967b6df4b79SXiubo Li if (page) { 1968b6df4b79SXiubo Li __free_page(page); 1969b6df4b79SXiubo Li atomic_dec(&global_db_count); 1970b6df4b79SXiubo Li } 1971b6df4b79SXiubo Li } 1972b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1973b6df4b79SXiubo Li } 1974b6df4b79SXiubo Li 1975b6df4b79SXiubo Li /* 1976b6df4b79SXiubo Li * Try to wake up the udevs who are waiting 1977b6df4b79SXiubo Li * for the global data pool. 1978b6df4b79SXiubo Li */ 1979b6df4b79SXiubo Li list_for_each_entry(udev, &root_udev, node) { 1980b6df4b79SXiubo Li if (udev->waiting_global) 1981b6df4b79SXiubo Li wake_up(&udev->wait_cmdr); 1982b6df4b79SXiubo Li } 1983b6df4b79SXiubo Li mutex_unlock(&root_udev_mutex); 1984b6df4b79SXiubo Li } 1985b6df4b79SXiubo Li 1986b6df4b79SXiubo Li return 0; 1987b6df4b79SXiubo Li } 1988b6df4b79SXiubo Li 19897c9e7a6fSAndy Grover static int __init tcmu_module_init(void) 19907c9e7a6fSAndy Grover { 1991801fc54dSBryant G. Ly int ret, i, k, len = 0; 19927c9e7a6fSAndy Grover 19937c9e7a6fSAndy Grover BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0); 19947c9e7a6fSAndy Grover 19957c9e7a6fSAndy Grover tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache", 19967c9e7a6fSAndy Grover sizeof(struct tcmu_cmd), 19977c9e7a6fSAndy Grover __alignof__(struct tcmu_cmd), 19987c9e7a6fSAndy Grover 0, NULL); 19997c9e7a6fSAndy Grover if (!tcmu_cmd_cache) 20007c9e7a6fSAndy Grover return -ENOMEM; 20017c9e7a6fSAndy Grover 20027c9e7a6fSAndy Grover tcmu_root_device = root_device_register("tcm_user"); 20037c9e7a6fSAndy Grover if (IS_ERR(tcmu_root_device)) { 20047c9e7a6fSAndy Grover ret = PTR_ERR(tcmu_root_device); 20057c9e7a6fSAndy Grover goto out_free_cache; 20067c9e7a6fSAndy Grover } 20077c9e7a6fSAndy Grover 20087c9e7a6fSAndy Grover ret = genl_register_family(&tcmu_genl_family); 20097c9e7a6fSAndy Grover if (ret < 0) { 20107c9e7a6fSAndy Grover goto out_unreg_device; 20117c9e7a6fSAndy Grover } 20127c9e7a6fSAndy Grover 20137d7a7435SNicholas Bellinger for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) { 20147d7a7435SNicholas Bellinger len += sizeof(struct configfs_attribute *); 20157d7a7435SNicholas Bellinger } 2016801fc54dSBryant G. Ly for (i = 0; tcmu_attrib_attrs[i] != NULL; i++) { 2017801fc54dSBryant G. Ly len += sizeof(struct configfs_attribute *); 2018801fc54dSBryant G. Ly } 2019801fc54dSBryant G. Ly len += sizeof(struct configfs_attribute *); 20207d7a7435SNicholas Bellinger 20217d7a7435SNicholas Bellinger tcmu_attrs = kzalloc(len, GFP_KERNEL); 20227d7a7435SNicholas Bellinger if (!tcmu_attrs) { 20237d7a7435SNicholas Bellinger ret = -ENOMEM; 20247d7a7435SNicholas Bellinger goto out_unreg_genl; 20257d7a7435SNicholas Bellinger } 20267d7a7435SNicholas Bellinger 20277d7a7435SNicholas Bellinger for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) { 20287d7a7435SNicholas Bellinger tcmu_attrs[i] = passthrough_attrib_attrs[i]; 20297d7a7435SNicholas Bellinger } 2030801fc54dSBryant G. Ly for (k = 0; tcmu_attrib_attrs[k] != NULL; k++) { 2031801fc54dSBryant G. Ly tcmu_attrs[i] = tcmu_attrib_attrs[k]; 20329a8bb606SBryant G. Ly i++; 2033801fc54dSBryant G. Ly } 20347d7a7435SNicholas Bellinger tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs; 20357d7a7435SNicholas Bellinger 20360a06d430SChristoph Hellwig ret = transport_backend_register(&tcmu_ops); 20377c9e7a6fSAndy Grover if (ret) 20387d7a7435SNicholas Bellinger goto out_attrs; 20397c9e7a6fSAndy Grover 2040b6df4b79SXiubo Li init_waitqueue_head(&unmap_wait); 2041b6df4b79SXiubo Li unmap_thread = kthread_run(unmap_thread_fn, NULL, "tcmu_unmap"); 2042b6df4b79SXiubo Li if (IS_ERR(unmap_thread)) { 2043b6df4b79SXiubo Li ret = PTR_ERR(unmap_thread); 2044b6df4b79SXiubo Li goto out_unreg_transport; 2045b6df4b79SXiubo Li } 2046b6df4b79SXiubo Li 20477c9e7a6fSAndy Grover return 0; 20487c9e7a6fSAndy Grover 2049b6df4b79SXiubo Li out_unreg_transport: 2050b6df4b79SXiubo Li target_backend_unregister(&tcmu_ops); 20517d7a7435SNicholas Bellinger out_attrs: 20527d7a7435SNicholas Bellinger kfree(tcmu_attrs); 20537c9e7a6fSAndy Grover out_unreg_genl: 20547c9e7a6fSAndy Grover genl_unregister_family(&tcmu_genl_family); 20557c9e7a6fSAndy Grover out_unreg_device: 20567c9e7a6fSAndy Grover root_device_unregister(tcmu_root_device); 20577c9e7a6fSAndy Grover out_free_cache: 20587c9e7a6fSAndy Grover kmem_cache_destroy(tcmu_cmd_cache); 20597c9e7a6fSAndy Grover 20607c9e7a6fSAndy Grover return ret; 20617c9e7a6fSAndy Grover } 20627c9e7a6fSAndy Grover 20637c9e7a6fSAndy Grover static void __exit tcmu_module_exit(void) 20647c9e7a6fSAndy Grover { 2065b6df4b79SXiubo Li kthread_stop(unmap_thread); 20660a06d430SChristoph Hellwig target_backend_unregister(&tcmu_ops); 20677d7a7435SNicholas Bellinger kfree(tcmu_attrs); 20687c9e7a6fSAndy Grover genl_unregister_family(&tcmu_genl_family); 20697c9e7a6fSAndy Grover root_device_unregister(tcmu_root_device); 20707c9e7a6fSAndy Grover kmem_cache_destroy(tcmu_cmd_cache); 20717c9e7a6fSAndy Grover } 20727c9e7a6fSAndy Grover 20737c9e7a6fSAndy Grover MODULE_DESCRIPTION("TCM USER subsystem plugin"); 20747c9e7a6fSAndy Grover MODULE_AUTHOR("Shaohua Li <shli@kernel.org>"); 20757c9e7a6fSAndy Grover MODULE_AUTHOR("Andy Grover <agrover@redhat.com>"); 20767c9e7a6fSAndy Grover MODULE_LICENSE("GPL"); 20777c9e7a6fSAndy Grover 20787c9e7a6fSAndy Grover module_init(tcmu_module_init); 20797c9e7a6fSAndy Grover module_exit(tcmu_module_exit); 2080