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]; 153b849b456SKenjiro Nakayama 154b849b456SKenjiro Nakayama int nl_reply_supported; 1557c9e7a6fSAndy Grover }; 1567c9e7a6fSAndy Grover 1577c9e7a6fSAndy Grover #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev) 1587c9e7a6fSAndy Grover 1597c9e7a6fSAndy Grover #define CMDR_OFF sizeof(struct tcmu_mailbox) 1607c9e7a6fSAndy Grover 1617c9e7a6fSAndy Grover struct tcmu_cmd { 1627c9e7a6fSAndy Grover struct se_cmd *se_cmd; 1637c9e7a6fSAndy Grover struct tcmu_dev *tcmu_dev; 1647c9e7a6fSAndy Grover 1657c9e7a6fSAndy Grover uint16_t cmd_id; 1667c9e7a6fSAndy Grover 16726418649SSheng Yang /* Can't use se_cmd when cleaning up expired cmds, because if 1687c9e7a6fSAndy Grover cmd has been completed then accessing se_cmd is off limits */ 169141685a3SXiubo Li uint32_t dbi_cnt; 170141685a3SXiubo Li uint32_t dbi_cur; 171141685a3SXiubo Li uint32_t *dbi; 1727c9e7a6fSAndy Grover 1737c9e7a6fSAndy Grover unsigned long deadline; 1747c9e7a6fSAndy Grover 1757c9e7a6fSAndy Grover #define TCMU_CMD_BIT_EXPIRED 0 1767c9e7a6fSAndy Grover unsigned long flags; 1777c9e7a6fSAndy Grover }; 1787c9e7a6fSAndy Grover 179b6df4b79SXiubo Li static struct task_struct *unmap_thread; 180b6df4b79SXiubo Li static wait_queue_head_t unmap_wait; 181b6df4b79SXiubo Li static DEFINE_MUTEX(root_udev_mutex); 182b6df4b79SXiubo Li static LIST_HEAD(root_udev); 183b6df4b79SXiubo Li 184b6df4b79SXiubo Li static atomic_t global_db_count = ATOMIC_INIT(0); 185b6df4b79SXiubo Li 1867c9e7a6fSAndy Grover static struct kmem_cache *tcmu_cmd_cache; 1877c9e7a6fSAndy Grover 1887c9e7a6fSAndy Grover /* multicast group */ 1897c9e7a6fSAndy Grover enum tcmu_multicast_groups { 1907c9e7a6fSAndy Grover TCMU_MCGRP_CONFIG, 1917c9e7a6fSAndy Grover }; 1927c9e7a6fSAndy Grover 1937c9e7a6fSAndy Grover static const struct genl_multicast_group tcmu_mcgrps[] = { 1947c9e7a6fSAndy Grover [TCMU_MCGRP_CONFIG] = { .name = "config", }, 1957c9e7a6fSAndy Grover }; 1967c9e7a6fSAndy Grover 197b3af66e2SMike Christie static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = { 198b3af66e2SMike Christie [TCMU_ATTR_DEVICE] = { .type = NLA_STRING }, 199b3af66e2SMike Christie [TCMU_ATTR_MINOR] = { .type = NLA_U32 }, 200b3af66e2SMike Christie [TCMU_ATTR_CMD_STATUS] = { .type = NLA_S32 }, 201b3af66e2SMike Christie [TCMU_ATTR_DEVICE_ID] = { .type = NLA_U32 }, 202b3af66e2SMike Christie [TCMU_ATTR_SUPP_KERN_CMD_REPLY] = { .type = NLA_U8 }, 203b3af66e2SMike Christie }; 204b3af66e2SMike Christie 205b3af66e2SMike Christie static int tcmu_genl_cmd_done(struct genl_info *info, int completed_cmd) 206b3af66e2SMike Christie { 207b3af66e2SMike Christie struct se_device *dev; 208b3af66e2SMike Christie struct tcmu_dev *udev; 209b3af66e2SMike Christie struct tcmu_nl_cmd *nl_cmd; 210b3af66e2SMike Christie int dev_id, rc, ret = 0; 211b3af66e2SMike Christie bool is_removed = (completed_cmd == TCMU_CMD_REMOVED_DEVICE); 212b3af66e2SMike Christie 213b3af66e2SMike Christie if (!info->attrs[TCMU_ATTR_CMD_STATUS] || 214b3af66e2SMike Christie !info->attrs[TCMU_ATTR_DEVICE_ID]) { 215b3af66e2SMike Christie printk(KERN_ERR "TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n"); 216b3af66e2SMike Christie return -EINVAL; 217b3af66e2SMike Christie } 218b3af66e2SMike Christie 219b3af66e2SMike Christie dev_id = nla_get_u32(info->attrs[TCMU_ATTR_DEVICE_ID]); 220b3af66e2SMike Christie rc = nla_get_s32(info->attrs[TCMU_ATTR_CMD_STATUS]); 221b3af66e2SMike Christie 222b3af66e2SMike Christie dev = target_find_device(dev_id, !is_removed); 223b3af66e2SMike Christie if (!dev) { 224b3af66e2SMike Christie printk(KERN_ERR "tcmu nl cmd %u/%u completion could not find device with dev id %u.\n", 225b3af66e2SMike Christie completed_cmd, rc, dev_id); 226b3af66e2SMike Christie return -ENODEV; 227b3af66e2SMike Christie } 228b3af66e2SMike Christie udev = TCMU_DEV(dev); 229b3af66e2SMike Christie 230b3af66e2SMike Christie spin_lock(&udev->nl_cmd_lock); 231b3af66e2SMike Christie nl_cmd = &udev->curr_nl_cmd; 232b3af66e2SMike Christie 233b3af66e2SMike Christie pr_debug("genl cmd done got id %d curr %d done %d rc %d\n", dev_id, 234b3af66e2SMike Christie nl_cmd->cmd, completed_cmd, rc); 235b3af66e2SMike Christie 236b3af66e2SMike Christie if (nl_cmd->cmd != completed_cmd) { 237b3af66e2SMike Christie printk(KERN_ERR "Mismatched commands (Expecting reply for %d. Current %d).\n", 238b3af66e2SMike Christie completed_cmd, nl_cmd->cmd); 239b3af66e2SMike Christie ret = -EINVAL; 240b3af66e2SMike Christie } else { 241b3af66e2SMike Christie nl_cmd->status = rc; 242b3af66e2SMike Christie } 243b3af66e2SMike Christie 244b3af66e2SMike Christie spin_unlock(&udev->nl_cmd_lock); 245b3af66e2SMike Christie if (!is_removed) 246b3af66e2SMike Christie target_undepend_item(&dev->dev_group.cg_item); 247b3af66e2SMike Christie if (!ret) 248b3af66e2SMike Christie complete(&nl_cmd->complete); 249b3af66e2SMike Christie return ret; 250b3af66e2SMike Christie } 251b3af66e2SMike Christie 252b3af66e2SMike Christie static int tcmu_genl_rm_dev_done(struct sk_buff *skb, struct genl_info *info) 253b3af66e2SMike Christie { 254b3af66e2SMike Christie return tcmu_genl_cmd_done(info, TCMU_CMD_REMOVED_DEVICE); 255b3af66e2SMike Christie } 256b3af66e2SMike Christie 257b3af66e2SMike Christie static int tcmu_genl_add_dev_done(struct sk_buff *skb, struct genl_info *info) 258b3af66e2SMike Christie { 259b3af66e2SMike Christie return tcmu_genl_cmd_done(info, TCMU_CMD_ADDED_DEVICE); 260b3af66e2SMike Christie } 261b3af66e2SMike Christie 262b3af66e2SMike Christie static int tcmu_genl_reconfig_dev_done(struct sk_buff *skb, 263b3af66e2SMike Christie struct genl_info *info) 264b3af66e2SMike Christie { 265b3af66e2SMike Christie return tcmu_genl_cmd_done(info, TCMU_CMD_RECONFIG_DEVICE); 266b3af66e2SMike Christie } 267b3af66e2SMike Christie 268b3af66e2SMike Christie static int tcmu_genl_set_features(struct sk_buff *skb, struct genl_info *info) 269b3af66e2SMike Christie { 270b3af66e2SMike Christie if (info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]) { 271b3af66e2SMike Christie tcmu_kern_cmd_reply_supported = 272b3af66e2SMike Christie nla_get_u8(info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]); 273b3af66e2SMike Christie printk(KERN_INFO "tcmu daemon: command reply support %u.\n", 274b3af66e2SMike Christie tcmu_kern_cmd_reply_supported); 275b3af66e2SMike Christie } 276b3af66e2SMike Christie 277b3af66e2SMike Christie return 0; 278b3af66e2SMike Christie } 279b3af66e2SMike Christie 280b3af66e2SMike Christie static const struct genl_ops tcmu_genl_ops[] = { 281b3af66e2SMike Christie { 282b3af66e2SMike Christie .cmd = TCMU_CMD_SET_FEATURES, 283b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 284b3af66e2SMike Christie .policy = tcmu_attr_policy, 285b3af66e2SMike Christie .doit = tcmu_genl_set_features, 286b3af66e2SMike Christie }, 287b3af66e2SMike Christie { 288b3af66e2SMike Christie .cmd = TCMU_CMD_ADDED_DEVICE_DONE, 289b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 290b3af66e2SMike Christie .policy = tcmu_attr_policy, 291b3af66e2SMike Christie .doit = tcmu_genl_add_dev_done, 292b3af66e2SMike Christie }, 293b3af66e2SMike Christie { 294b3af66e2SMike Christie .cmd = TCMU_CMD_REMOVED_DEVICE_DONE, 295b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 296b3af66e2SMike Christie .policy = tcmu_attr_policy, 297b3af66e2SMike Christie .doit = tcmu_genl_rm_dev_done, 298b3af66e2SMike Christie }, 299b3af66e2SMike Christie { 300b3af66e2SMike Christie .cmd = TCMU_CMD_RECONFIG_DEVICE_DONE, 301b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 302b3af66e2SMike Christie .policy = tcmu_attr_policy, 303b3af66e2SMike Christie .doit = tcmu_genl_reconfig_dev_done, 304b3af66e2SMike Christie }, 305b3af66e2SMike Christie }; 306b3af66e2SMike Christie 3077c9e7a6fSAndy Grover /* Our generic netlink family */ 30856989f6dSJohannes Berg static struct genl_family tcmu_genl_family __ro_after_init = { 309489111e5SJohannes Berg .module = THIS_MODULE, 3107c9e7a6fSAndy Grover .hdrsize = 0, 3117c9e7a6fSAndy Grover .name = "TCM-USER", 312b3af66e2SMike Christie .version = 2, 3137c9e7a6fSAndy Grover .maxattr = TCMU_ATTR_MAX, 3147c9e7a6fSAndy Grover .mcgrps = tcmu_mcgrps, 3157c9e7a6fSAndy Grover .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps), 31620c08b36SSheng Yang .netnsok = true, 317b3af66e2SMike Christie .ops = tcmu_genl_ops, 318b3af66e2SMike Christie .n_ops = ARRAY_SIZE(tcmu_genl_ops), 3197c9e7a6fSAndy Grover }; 3207c9e7a6fSAndy Grover 321141685a3SXiubo Li #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index)) 322141685a3SXiubo Li #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0) 323141685a3SXiubo Li #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index)) 324141685a3SXiubo Li #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++]) 325141685a3SXiubo Li 326b6df4b79SXiubo Li static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len) 327141685a3SXiubo Li { 328141685a3SXiubo Li struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 329141685a3SXiubo Li uint32_t i; 330141685a3SXiubo Li 331b6df4b79SXiubo Li for (i = 0; i < len; i++) 332141685a3SXiubo Li clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap); 333141685a3SXiubo Li } 334141685a3SXiubo Li 335b6df4b79SXiubo Li static inline bool tcmu_get_empty_block(struct tcmu_dev *udev, 336b6df4b79SXiubo Li struct tcmu_cmd *tcmu_cmd) 337141685a3SXiubo Li { 338b6df4b79SXiubo Li struct page *page; 339b6df4b79SXiubo Li int ret, dbi; 340141685a3SXiubo Li 341b6df4b79SXiubo Li dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh); 342b6df4b79SXiubo Li if (dbi == udev->dbi_thresh) 343b6df4b79SXiubo Li return false; 344b6df4b79SXiubo Li 345b6df4b79SXiubo Li page = radix_tree_lookup(&udev->data_blocks, dbi); 346b6df4b79SXiubo Li if (!page) { 347b6df4b79SXiubo Li if (atomic_add_return(1, &global_db_count) > 348b6df4b79SXiubo Li TCMU_GLOBAL_MAX_BLOCKS) { 349b6df4b79SXiubo Li atomic_dec(&global_db_count); 350b6df4b79SXiubo Li return false; 351b6df4b79SXiubo Li } 352b6df4b79SXiubo Li 353b6df4b79SXiubo Li /* try to get new page from the mm */ 354b6df4b79SXiubo Li page = alloc_page(GFP_KERNEL); 355b6df4b79SXiubo Li if (!page) 356daf78c30SXiubo Li goto err_alloc; 357b6df4b79SXiubo Li 358b6df4b79SXiubo Li ret = radix_tree_insert(&udev->data_blocks, dbi, page); 359daf78c30SXiubo Li if (ret) 360daf78c30SXiubo Li goto err_insert; 361b6df4b79SXiubo Li } 362b6df4b79SXiubo Li 363141685a3SXiubo Li if (dbi > udev->dbi_max) 364141685a3SXiubo Li udev->dbi_max = dbi; 365141685a3SXiubo Li 366141685a3SXiubo Li set_bit(dbi, udev->data_bitmap); 367b6df4b79SXiubo Li tcmu_cmd_set_dbi(tcmu_cmd, dbi); 368141685a3SXiubo Li 369b6df4b79SXiubo Li return true; 370daf78c30SXiubo Li err_insert: 371daf78c30SXiubo Li __free_page(page); 372daf78c30SXiubo Li err_alloc: 373daf78c30SXiubo Li atomic_dec(&global_db_count); 374daf78c30SXiubo Li return false; 375141685a3SXiubo Li } 376141685a3SXiubo Li 377b6df4b79SXiubo Li static bool tcmu_get_empty_blocks(struct tcmu_dev *udev, 378b6df4b79SXiubo Li struct tcmu_cmd *tcmu_cmd) 379b6df4b79SXiubo Li { 380b6df4b79SXiubo Li int i; 381b6df4b79SXiubo Li 382b6df4b79SXiubo Li udev->waiting_global = false; 383b6df4b79SXiubo Li 384b6df4b79SXiubo Li for (i = tcmu_cmd->dbi_cur; i < tcmu_cmd->dbi_cnt; i++) { 385b6df4b79SXiubo Li if (!tcmu_get_empty_block(udev, tcmu_cmd)) 386b6df4b79SXiubo Li goto err; 387141685a3SXiubo Li } 388b6df4b79SXiubo Li return true; 389b6df4b79SXiubo Li 390b6df4b79SXiubo Li err: 391b6df4b79SXiubo Li udev->waiting_global = true; 392b6df4b79SXiubo Li /* Try to wake up the unmap thread */ 393b6df4b79SXiubo Li wake_up(&unmap_wait); 394b6df4b79SXiubo Li return false; 395141685a3SXiubo Li } 396141685a3SXiubo Li 397b6df4b79SXiubo Li static inline struct page * 398b6df4b79SXiubo Li tcmu_get_block_page(struct tcmu_dev *udev, uint32_t dbi) 399141685a3SXiubo Li { 400141685a3SXiubo Li return radix_tree_lookup(&udev->data_blocks, dbi); 401141685a3SXiubo Li } 402141685a3SXiubo Li 403141685a3SXiubo Li static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd) 404141685a3SXiubo Li { 405141685a3SXiubo Li kfree(tcmu_cmd->dbi); 406141685a3SXiubo Li kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); 407141685a3SXiubo Li } 408141685a3SXiubo Li 409141685a3SXiubo Li static inline size_t tcmu_cmd_get_data_length(struct tcmu_cmd *tcmu_cmd) 410141685a3SXiubo Li { 411141685a3SXiubo Li struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 412141685a3SXiubo Li size_t data_length = round_up(se_cmd->data_length, DATA_BLOCK_SIZE); 413141685a3SXiubo Li 414141685a3SXiubo Li if (se_cmd->se_cmd_flags & SCF_BIDI) { 415141685a3SXiubo Li BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents)); 416141685a3SXiubo Li data_length += round_up(se_cmd->t_bidi_data_sg->length, 417141685a3SXiubo Li DATA_BLOCK_SIZE); 418141685a3SXiubo Li } 419141685a3SXiubo Li 420141685a3SXiubo Li return data_length; 421141685a3SXiubo Li } 422141685a3SXiubo Li 423141685a3SXiubo Li static inline uint32_t tcmu_cmd_get_block_cnt(struct tcmu_cmd *tcmu_cmd) 424141685a3SXiubo Li { 425141685a3SXiubo Li size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd); 426141685a3SXiubo Li 427141685a3SXiubo Li return data_length / DATA_BLOCK_SIZE; 428141685a3SXiubo Li } 429141685a3SXiubo Li 4307c9e7a6fSAndy Grover static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd) 4317c9e7a6fSAndy Grover { 4327c9e7a6fSAndy Grover struct se_device *se_dev = se_cmd->se_dev; 4337c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(se_dev); 4347c9e7a6fSAndy Grover struct tcmu_cmd *tcmu_cmd; 4357c9e7a6fSAndy Grover 4367c9e7a6fSAndy Grover tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_KERNEL); 4377c9e7a6fSAndy Grover if (!tcmu_cmd) 4387c9e7a6fSAndy Grover return NULL; 4397c9e7a6fSAndy Grover 4407c9e7a6fSAndy Grover tcmu_cmd->se_cmd = se_cmd; 4417c9e7a6fSAndy Grover tcmu_cmd->tcmu_dev = udev; 4427c9e7a6fSAndy Grover 443141685a3SXiubo Li tcmu_cmd_reset_dbi_cur(tcmu_cmd); 444141685a3SXiubo Li tcmu_cmd->dbi_cnt = tcmu_cmd_get_block_cnt(tcmu_cmd); 445141685a3SXiubo Li tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t), 446141685a3SXiubo Li GFP_KERNEL); 447141685a3SXiubo Li if (!tcmu_cmd->dbi) { 448141685a3SXiubo Li kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); 449141685a3SXiubo Li return NULL; 450141685a3SXiubo Li } 451141685a3SXiubo Li 4527c9e7a6fSAndy Grover return tcmu_cmd; 4537c9e7a6fSAndy Grover } 4547c9e7a6fSAndy Grover 4557c9e7a6fSAndy Grover static inline void tcmu_flush_dcache_range(void *vaddr, size_t size) 4567c9e7a6fSAndy Grover { 457b75d8063SGeliang Tang unsigned long offset = offset_in_page(vaddr); 45826d2b310Stangwenji void *start = vaddr - offset; 4597c9e7a6fSAndy Grover 4607c9e7a6fSAndy Grover size = round_up(size+offset, PAGE_SIZE); 4617c9e7a6fSAndy Grover 4627c9e7a6fSAndy Grover while (size) { 46326d2b310Stangwenji flush_dcache_page(virt_to_page(start)); 46426d2b310Stangwenji start += PAGE_SIZE; 4657c9e7a6fSAndy Grover size -= PAGE_SIZE; 4667c9e7a6fSAndy Grover } 4677c9e7a6fSAndy Grover } 4687c9e7a6fSAndy Grover 4697c9e7a6fSAndy Grover /* 4707c9e7a6fSAndy Grover * Some ring helper functions. We don't assume size is a power of 2 so 4717c9e7a6fSAndy Grover * we can't use circ_buf.h. 4727c9e7a6fSAndy Grover */ 4737c9e7a6fSAndy Grover static inline size_t spc_used(size_t head, size_t tail, size_t size) 4747c9e7a6fSAndy Grover { 4757c9e7a6fSAndy Grover int diff = head - tail; 4767c9e7a6fSAndy Grover 4777c9e7a6fSAndy Grover if (diff >= 0) 4787c9e7a6fSAndy Grover return diff; 4797c9e7a6fSAndy Grover else 4807c9e7a6fSAndy Grover return size + diff; 4817c9e7a6fSAndy Grover } 4827c9e7a6fSAndy Grover 4837c9e7a6fSAndy Grover static inline size_t spc_free(size_t head, size_t tail, size_t size) 4847c9e7a6fSAndy Grover { 4857c9e7a6fSAndy Grover /* Keep 1 byte unused or we can't tell full from empty */ 4867c9e7a6fSAndy Grover return (size - spc_used(head, tail, size) - 1); 4877c9e7a6fSAndy Grover } 4887c9e7a6fSAndy Grover 4897c9e7a6fSAndy Grover static inline size_t head_to_end(size_t head, size_t size) 4907c9e7a6fSAndy Grover { 4917c9e7a6fSAndy Grover return size - head; 4927c9e7a6fSAndy Grover } 4937c9e7a6fSAndy Grover 494f1dbd087SSheng Yang static inline void new_iov(struct iovec **iov, int *iov_cnt, 495f1dbd087SSheng Yang struct tcmu_dev *udev) 496f1dbd087SSheng Yang { 497f1dbd087SSheng Yang struct iovec *iovec; 498f1dbd087SSheng Yang 499f1dbd087SSheng Yang if (*iov_cnt != 0) 500f1dbd087SSheng Yang (*iov)++; 501f1dbd087SSheng Yang (*iov_cnt)++; 502f1dbd087SSheng Yang 503f1dbd087SSheng Yang iovec = *iov; 504f1dbd087SSheng Yang memset(iovec, 0, sizeof(struct iovec)); 505f1dbd087SSheng Yang } 506f1dbd087SSheng Yang 5077c9e7a6fSAndy Grover #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size) 5087c9e7a6fSAndy Grover 50926418649SSheng Yang /* offset is relative to mb_addr */ 510141685a3SXiubo Li static inline size_t get_block_offset_user(struct tcmu_dev *dev, 511141685a3SXiubo Li int dbi, int remaining) 51226418649SSheng Yang { 513141685a3SXiubo Li return dev->data_off + dbi * DATA_BLOCK_SIZE + 51426418649SSheng Yang DATA_BLOCK_SIZE - remaining; 51526418649SSheng Yang } 51626418649SSheng Yang 517daf78c30SXiubo Li static inline size_t iov_tail(struct iovec *iov) 51826418649SSheng Yang { 51926418649SSheng Yang return (size_t)iov->iov_base + iov->iov_len; 52026418649SSheng Yang } 52126418649SSheng Yang 522b6df4b79SXiubo Li static int scatter_data_area(struct tcmu_dev *udev, 523141685a3SXiubo Li struct tcmu_cmd *tcmu_cmd, struct scatterlist *data_sg, 524141685a3SXiubo Li unsigned int data_nents, struct iovec **iov, 525141685a3SXiubo Li int *iov_cnt, bool copy_data) 526f97ec7dbSIlias Tsitsimpis { 527141685a3SXiubo Li int i, dbi; 52826418649SSheng Yang int block_remaining = 0; 529141685a3SXiubo Li void *from, *to = NULL; 530141685a3SXiubo Li size_t copy_bytes, to_offset, offset; 531f97ec7dbSIlias Tsitsimpis struct scatterlist *sg; 532b6df4b79SXiubo Li struct page *page; 533f97ec7dbSIlias Tsitsimpis 534f97ec7dbSIlias Tsitsimpis for_each_sg(data_sg, sg, data_nents, i) { 53526418649SSheng Yang int sg_remaining = sg->length; 536f97ec7dbSIlias Tsitsimpis from = kmap_atomic(sg_page(sg)) + sg->offset; 53726418649SSheng Yang while (sg_remaining > 0) { 53826418649SSheng Yang if (block_remaining == 0) { 539b6df4b79SXiubo Li if (to) 540b6df4b79SXiubo Li kunmap_atomic(to); 541b6df4b79SXiubo Li 54226418649SSheng Yang block_remaining = DATA_BLOCK_SIZE; 543b6df4b79SXiubo Li dbi = tcmu_cmd_get_dbi(tcmu_cmd); 544b6df4b79SXiubo Li page = tcmu_get_block_page(udev, dbi); 545b6df4b79SXiubo Li to = kmap_atomic(page); 546141685a3SXiubo Li } 547141685a3SXiubo Li 54826418649SSheng Yang copy_bytes = min_t(size_t, sg_remaining, 54926418649SSheng Yang block_remaining); 550141685a3SXiubo Li to_offset = get_block_offset_user(udev, dbi, 55126418649SSheng Yang block_remaining); 552141685a3SXiubo Li 55326418649SSheng Yang if (*iov_cnt != 0 && 554daf78c30SXiubo Li to_offset == iov_tail(*iov)) { 555f1dbd087SSheng Yang (*iov)->iov_len += copy_bytes; 55626418649SSheng Yang } else { 557f1dbd087SSheng Yang new_iov(iov, iov_cnt, udev); 55826418649SSheng Yang (*iov)->iov_base = (void __user *)to_offset; 559f97ec7dbSIlias Tsitsimpis (*iov)->iov_len = copy_bytes; 56026418649SSheng Yang } 561f97ec7dbSIlias Tsitsimpis if (copy_data) { 562c542942cSXiubo Li offset = DATA_BLOCK_SIZE - block_remaining; 563c542942cSXiubo Li memcpy(to + offset, 564c542942cSXiubo Li from + sg->length - sg_remaining, 56526418649SSheng Yang copy_bytes); 566f97ec7dbSIlias Tsitsimpis tcmu_flush_dcache_range(to, copy_bytes); 567f97ec7dbSIlias Tsitsimpis } 56826418649SSheng Yang sg_remaining -= copy_bytes; 56926418649SSheng Yang block_remaining -= copy_bytes; 570f97ec7dbSIlias Tsitsimpis } 571e2e21bd8SSagi Grimberg kunmap_atomic(from - sg->offset); 572f97ec7dbSIlias Tsitsimpis } 573b6df4b79SXiubo Li if (to) 574b6df4b79SXiubo Li kunmap_atomic(to); 575f97ec7dbSIlias Tsitsimpis 576141685a3SXiubo Li return 0; 5770c28481fSSheng Yang } 5780c28481fSSheng Yang 579a5d68ba8SXiubo Li static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 580a5d68ba8SXiubo Li bool bidi) 581f97ec7dbSIlias Tsitsimpis { 582a5d68ba8SXiubo Li struct se_cmd *se_cmd = cmd->se_cmd; 583141685a3SXiubo Li int i, dbi; 58426418649SSheng Yang int block_remaining = 0; 585b6df4b79SXiubo Li void *from = NULL, *to; 586141685a3SXiubo Li size_t copy_bytes, offset; 587a5d68ba8SXiubo Li struct scatterlist *sg, *data_sg; 588b6df4b79SXiubo Li struct page *page; 589a5d68ba8SXiubo Li unsigned int data_nents; 590141685a3SXiubo Li uint32_t count = 0; 591a5d68ba8SXiubo Li 592a5d68ba8SXiubo Li if (!bidi) { 593a5d68ba8SXiubo Li data_sg = se_cmd->t_data_sg; 594a5d68ba8SXiubo Li data_nents = se_cmd->t_data_nents; 595a5d68ba8SXiubo Li } else { 596a5d68ba8SXiubo Li 597a5d68ba8SXiubo Li /* 598a5d68ba8SXiubo Li * For bidi case, the first count blocks are for Data-Out 599a5d68ba8SXiubo Li * buffer blocks, and before gathering the Data-In buffer 600a5d68ba8SXiubo Li * the Data-Out buffer blocks should be discarded. 601a5d68ba8SXiubo Li */ 602a5d68ba8SXiubo Li count = DIV_ROUND_UP(se_cmd->data_length, DATA_BLOCK_SIZE); 603a5d68ba8SXiubo Li 604a5d68ba8SXiubo Li data_sg = se_cmd->t_bidi_data_sg; 605a5d68ba8SXiubo Li data_nents = se_cmd->t_bidi_data_nents; 606a5d68ba8SXiubo Li } 607f97ec7dbSIlias Tsitsimpis 608141685a3SXiubo Li tcmu_cmd_set_dbi_cur(cmd, count); 609141685a3SXiubo Li 610f97ec7dbSIlias Tsitsimpis for_each_sg(data_sg, sg, data_nents, i) { 61126418649SSheng Yang int sg_remaining = sg->length; 612f97ec7dbSIlias Tsitsimpis to = kmap_atomic(sg_page(sg)) + sg->offset; 61326418649SSheng Yang while (sg_remaining > 0) { 61426418649SSheng Yang if (block_remaining == 0) { 615b6df4b79SXiubo Li if (from) 616b6df4b79SXiubo Li kunmap_atomic(from); 617b6df4b79SXiubo Li 61826418649SSheng Yang block_remaining = DATA_BLOCK_SIZE; 619141685a3SXiubo Li dbi = tcmu_cmd_get_dbi(cmd); 620b6df4b79SXiubo Li page = tcmu_get_block_page(udev, dbi); 621b6df4b79SXiubo Li from = kmap_atomic(page); 62226418649SSheng Yang } 62326418649SSheng Yang copy_bytes = min_t(size_t, sg_remaining, 62426418649SSheng Yang block_remaining); 625141685a3SXiubo Li offset = DATA_BLOCK_SIZE - block_remaining; 626f97ec7dbSIlias Tsitsimpis tcmu_flush_dcache_range(from, copy_bytes); 627c542942cSXiubo Li memcpy(to + sg->length - sg_remaining, from + offset, 62826418649SSheng Yang copy_bytes); 629f97ec7dbSIlias Tsitsimpis 63026418649SSheng Yang sg_remaining -= copy_bytes; 63126418649SSheng Yang block_remaining -= copy_bytes; 632f97ec7dbSIlias Tsitsimpis } 633e2e21bd8SSagi Grimberg kunmap_atomic(to - sg->offset); 634f97ec7dbSIlias Tsitsimpis } 635b6df4b79SXiubo Li if (from) 636b6df4b79SXiubo Li kunmap_atomic(from); 637f97ec7dbSIlias Tsitsimpis } 638f97ec7dbSIlias Tsitsimpis 639b6df4b79SXiubo Li static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh) 64026418649SSheng Yang { 641b6df4b79SXiubo Li return DATA_BLOCK_SIZE * (thresh - bitmap_weight(bitmap, thresh)); 64226418649SSheng Yang } 64326418649SSheng Yang 6447c9e7a6fSAndy Grover /* 645f97ec7dbSIlias Tsitsimpis * We can't queue a command until we have space available on the cmd ring *and* 6463d9b9555SAndy Grover * space available on the data area. 6477c9e7a6fSAndy Grover * 6487c9e7a6fSAndy Grover * Called with ring lock held. 6497c9e7a6fSAndy Grover */ 650b6df4b79SXiubo Li static bool is_ring_space_avail(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 651b6df4b79SXiubo Li size_t cmd_size, size_t data_needed) 6527c9e7a6fSAndy Grover { 6537c9e7a6fSAndy Grover struct tcmu_mailbox *mb = udev->mb_addr; 654b6df4b79SXiubo Li uint32_t blocks_needed = (data_needed + DATA_BLOCK_SIZE - 1) 655b6df4b79SXiubo Li / DATA_BLOCK_SIZE; 6560241fd39SNicholas Bellinger size_t space, cmd_needed; 6577c9e7a6fSAndy Grover u32 cmd_head; 6587c9e7a6fSAndy Grover 6597c9e7a6fSAndy Grover tcmu_flush_dcache_range(mb, sizeof(*mb)); 6607c9e7a6fSAndy Grover 6617c9e7a6fSAndy Grover cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 6627c9e7a6fSAndy Grover 663f56574a2SAndy Grover /* 664f56574a2SAndy Grover * If cmd end-of-ring space is too small then we need space for a NOP plus 665f56574a2SAndy Grover * original cmd - cmds are internally contiguous. 666f56574a2SAndy Grover */ 667f56574a2SAndy Grover if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size) 668f56574a2SAndy Grover cmd_needed = cmd_size; 669f56574a2SAndy Grover else 670f56574a2SAndy Grover cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size); 671f56574a2SAndy Grover 6727c9e7a6fSAndy Grover space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size); 6737c9e7a6fSAndy Grover if (space < cmd_needed) { 6747c9e7a6fSAndy Grover pr_debug("no cmd space: %u %u %u\n", cmd_head, 6757c9e7a6fSAndy Grover udev->cmdr_last_cleaned, udev->cmdr_size); 6767c9e7a6fSAndy Grover return false; 6777c9e7a6fSAndy Grover } 6787c9e7a6fSAndy Grover 679b6df4b79SXiubo Li /* try to check and get the data blocks as needed */ 680b6df4b79SXiubo Li space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh); 6817c9e7a6fSAndy Grover if (space < data_needed) { 682b6df4b79SXiubo Li unsigned long blocks_left = DATA_BLOCK_BITS - udev->dbi_thresh; 683b6df4b79SXiubo Li unsigned long grow; 684b6df4b79SXiubo Li 685b6df4b79SXiubo Li if (blocks_left < blocks_needed) { 686b6df4b79SXiubo Li pr_debug("no data space: only %lu available, but ask for %zu\n", 687b6df4b79SXiubo Li blocks_left * DATA_BLOCK_SIZE, 688b6df4b79SXiubo Li data_needed); 6897c9e7a6fSAndy Grover return false; 6907c9e7a6fSAndy Grover } 6917c9e7a6fSAndy Grover 692b6df4b79SXiubo Li /* Try to expand the thresh */ 693b6df4b79SXiubo Li if (!udev->dbi_thresh) { 694b6df4b79SXiubo Li /* From idle state */ 695b6df4b79SXiubo Li uint32_t init_thresh = DATA_BLOCK_INIT_BITS; 696b6df4b79SXiubo Li 697b6df4b79SXiubo Li udev->dbi_thresh = max(blocks_needed, init_thresh); 698b6df4b79SXiubo Li } else { 699b6df4b79SXiubo Li /* 700b6df4b79SXiubo Li * Grow the data area by max(blocks needed, 701b6df4b79SXiubo Li * dbi_thresh / 2), but limited to the max 702b6df4b79SXiubo Li * DATA_BLOCK_BITS size. 703b6df4b79SXiubo Li */ 704b6df4b79SXiubo Li grow = max(blocks_needed, udev->dbi_thresh / 2); 705b6df4b79SXiubo Li udev->dbi_thresh += grow; 706b6df4b79SXiubo Li if (udev->dbi_thresh > DATA_BLOCK_BITS) 707b6df4b79SXiubo Li udev->dbi_thresh = DATA_BLOCK_BITS; 708b6df4b79SXiubo Li } 709b6df4b79SXiubo Li } 710b6df4b79SXiubo Li 711daf78c30SXiubo Li return tcmu_get_empty_blocks(udev, cmd); 7127c9e7a6fSAndy Grover } 7137c9e7a6fSAndy Grover 714fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt) 715fe25cc34SXiubo Li { 716fe25cc34SXiubo Li return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]), 717fe25cc34SXiubo Li sizeof(struct tcmu_cmd_entry)); 718fe25cc34SXiubo Li } 719fe25cc34SXiubo Li 720fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd, 721fe25cc34SXiubo Li size_t base_command_size) 722fe25cc34SXiubo Li { 723fe25cc34SXiubo Li struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 724fe25cc34SXiubo Li size_t command_size; 725fe25cc34SXiubo Li 726fe25cc34SXiubo Li command_size = base_command_size + 727fe25cc34SXiubo Li round_up(scsi_command_size(se_cmd->t_task_cdb), 728fe25cc34SXiubo Li TCMU_OP_ALIGN_SIZE); 729fe25cc34SXiubo Li 730fe25cc34SXiubo Li WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1)); 731fe25cc34SXiubo Li 732fe25cc34SXiubo Li return command_size; 733fe25cc34SXiubo Li } 734fe25cc34SXiubo Li 7350d44374cSMike Christie static int tcmu_setup_cmd_timer(struct tcmu_cmd *tcmu_cmd) 7360d44374cSMike Christie { 7370d44374cSMike Christie struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 7380d44374cSMike Christie unsigned long tmo = udev->cmd_time_out; 7390d44374cSMike Christie int cmd_id; 7400d44374cSMike Christie 7410d44374cSMike Christie if (tcmu_cmd->cmd_id) 7420d44374cSMike Christie return 0; 7430d44374cSMike Christie 7440d44374cSMike Christie cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 1, USHRT_MAX, GFP_NOWAIT); 7450d44374cSMike Christie if (cmd_id < 0) { 7460d44374cSMike Christie pr_err("tcmu: Could not allocate cmd id.\n"); 7470d44374cSMike Christie return cmd_id; 7480d44374cSMike Christie } 7490d44374cSMike Christie tcmu_cmd->cmd_id = cmd_id; 7500d44374cSMike Christie 7510d44374cSMike Christie if (!tmo) 7520d44374cSMike Christie return 0; 7530d44374cSMike Christie 7540d44374cSMike Christie tcmu_cmd->deadline = round_jiffies_up(jiffies + msecs_to_jiffies(tmo)); 7550d44374cSMike Christie mod_timer(&udev->timeout, tcmu_cmd->deadline); 7560d44374cSMike Christie return 0; 7570d44374cSMike Christie } 7580d44374cSMike Christie 75902eb924fSAndy Grover static sense_reason_t 76002eb924fSAndy Grover tcmu_queue_cmd_ring(struct tcmu_cmd *tcmu_cmd) 7617c9e7a6fSAndy Grover { 7627c9e7a6fSAndy Grover struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 7637c9e7a6fSAndy Grover struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 7647c9e7a6fSAndy Grover size_t base_command_size, command_size; 7657c9e7a6fSAndy Grover struct tcmu_mailbox *mb; 7667c9e7a6fSAndy Grover struct tcmu_cmd_entry *entry; 7677c9e7a6fSAndy Grover struct iovec *iov; 768141685a3SXiubo Li int iov_cnt, ret; 7697c9e7a6fSAndy Grover uint32_t cmd_head; 7707c9e7a6fSAndy Grover uint64_t cdb_off; 771f97ec7dbSIlias Tsitsimpis bool copy_to_data_area; 772ab22d260SXiubo Li size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd); 7737c9e7a6fSAndy Grover 7747c9e7a6fSAndy Grover if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) 77502eb924fSAndy Grover return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 7767c9e7a6fSAndy Grover 7777c9e7a6fSAndy Grover /* 7787c9e7a6fSAndy Grover * Must be a certain minimum size for response sense info, but 7797c9e7a6fSAndy Grover * also may be larger if the iov array is large. 7807c9e7a6fSAndy Grover * 781fe25cc34SXiubo Li * We prepare as many iovs as possbile for potential uses here, 782fe25cc34SXiubo Li * because it's expensive to tell how many regions are freed in 783fe25cc34SXiubo Li * the bitmap & global data pool, as the size calculated here 784fe25cc34SXiubo Li * will only be used to do the checks. 785fe25cc34SXiubo Li * 786fe25cc34SXiubo Li * The size will be recalculated later as actually needed to save 787fe25cc34SXiubo Li * cmd area memories. 7887c9e7a6fSAndy Grover */ 789fe25cc34SXiubo Li base_command_size = tcmu_cmd_get_base_cmd_size(tcmu_cmd->dbi_cnt); 790fe25cc34SXiubo Li command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size); 7917c9e7a6fSAndy Grover 792b6df4b79SXiubo Li mutex_lock(&udev->cmdr_lock); 7937c9e7a6fSAndy Grover 7947c9e7a6fSAndy Grover mb = udev->mb_addr; 7957c9e7a6fSAndy Grover cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 796554617b2SAndy Grover if ((command_size > (udev->cmdr_size / 2)) || 797554617b2SAndy Grover data_length > udev->data_size) { 798554617b2SAndy Grover pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu " 7993d9b9555SAndy Grover "cmd ring/data area\n", command_size, data_length, 8007c9e7a6fSAndy Grover udev->cmdr_size, udev->data_size); 801b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 802554617b2SAndy Grover return TCM_INVALID_CDB_FIELD; 803554617b2SAndy Grover } 8047c9e7a6fSAndy Grover 805b6df4b79SXiubo Li while (!is_ring_space_avail(udev, tcmu_cmd, command_size, data_length)) { 8067c9e7a6fSAndy Grover int ret; 8077c9e7a6fSAndy Grover DEFINE_WAIT(__wait); 8087c9e7a6fSAndy Grover 8097c9e7a6fSAndy Grover prepare_to_wait(&udev->wait_cmdr, &__wait, TASK_INTERRUPTIBLE); 8107c9e7a6fSAndy Grover 8117c9e7a6fSAndy Grover pr_debug("sleeping for ring space\n"); 812b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 813af980e46SMike Christie if (udev->cmd_time_out) 814af980e46SMike Christie ret = schedule_timeout( 815af980e46SMike Christie msecs_to_jiffies(udev->cmd_time_out)); 816af980e46SMike Christie else 8177c9e7a6fSAndy Grover ret = schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT)); 8187c9e7a6fSAndy Grover finish_wait(&udev->wait_cmdr, &__wait); 8197c9e7a6fSAndy Grover if (!ret) { 8207c9e7a6fSAndy Grover pr_warn("tcmu: command timed out\n"); 82102eb924fSAndy Grover return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 8227c9e7a6fSAndy Grover } 8237c9e7a6fSAndy Grover 824b6df4b79SXiubo Li mutex_lock(&udev->cmdr_lock); 8257c9e7a6fSAndy Grover 8267c9e7a6fSAndy Grover /* We dropped cmdr_lock, cmd_head is stale */ 8277c9e7a6fSAndy Grover cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 8287c9e7a6fSAndy Grover } 8297c9e7a6fSAndy Grover 830f56574a2SAndy Grover /* Insert a PAD if end-of-ring space is too small */ 831f56574a2SAndy Grover if (head_to_end(cmd_head, udev->cmdr_size) < command_size) { 832f56574a2SAndy Grover size_t pad_size = head_to_end(cmd_head, udev->cmdr_size); 833f56574a2SAndy Grover 8347c9e7a6fSAndy Grover entry = (void *) mb + CMDR_OFF + cmd_head; 8350ad46af8SAndy Grover tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD); 8360ad46af8SAndy Grover tcmu_hdr_set_len(&entry->hdr.len_op, pad_size); 8370ad46af8SAndy Grover entry->hdr.cmd_id = 0; /* not used for PAD */ 8380ad46af8SAndy Grover entry->hdr.kflags = 0; 8390ad46af8SAndy Grover entry->hdr.uflags = 0; 8409d62bc0eSXiubo Li tcmu_flush_dcache_range(entry, sizeof(*entry)); 8417c9e7a6fSAndy Grover 8427c9e7a6fSAndy Grover UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size); 8439d62bc0eSXiubo Li tcmu_flush_dcache_range(mb, sizeof(*mb)); 8447c9e7a6fSAndy Grover 8457c9e7a6fSAndy Grover cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 8467c9e7a6fSAndy Grover WARN_ON(cmd_head != 0); 8477c9e7a6fSAndy Grover } 8487c9e7a6fSAndy Grover 8497c9e7a6fSAndy Grover entry = (void *) mb + CMDR_OFF + cmd_head; 850b3743c71SXiubo Li memset(entry, 0, command_size); 8510ad46af8SAndy Grover tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD); 8527c9e7a6fSAndy Grover 8533d9b9555SAndy Grover /* Handle allocating space from the data area */ 854b6df4b79SXiubo Li tcmu_cmd_reset_dbi_cur(tcmu_cmd); 8557c9e7a6fSAndy Grover iov = &entry->req.iov[0]; 856f97ec7dbSIlias Tsitsimpis iov_cnt = 0; 857e4648b01SIlias Tsitsimpis copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE 858e4648b01SIlias Tsitsimpis || se_cmd->se_cmd_flags & SCF_BIDI); 859b6df4b79SXiubo Li ret = scatter_data_area(udev, tcmu_cmd, se_cmd->t_data_sg, 860b6df4b79SXiubo Li se_cmd->t_data_nents, &iov, &iov_cnt, 861b6df4b79SXiubo Li copy_to_data_area); 862141685a3SXiubo Li if (ret) { 863b6df4b79SXiubo Li tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt); 864b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 865b6df4b79SXiubo Li 866141685a3SXiubo Li pr_err("tcmu: alloc and scatter data failed\n"); 867141685a3SXiubo Li return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 868141685a3SXiubo Li } 8697c9e7a6fSAndy Grover entry->req.iov_cnt = iov_cnt; 8707c9e7a6fSAndy Grover 871e4648b01SIlias Tsitsimpis /* Handle BIDI commands */ 872e4648b01SIlias Tsitsimpis iov_cnt = 0; 873b3743c71SXiubo Li if (se_cmd->se_cmd_flags & SCF_BIDI) { 874ab22d260SXiubo Li iov++; 875b6df4b79SXiubo Li ret = scatter_data_area(udev, tcmu_cmd, 876141685a3SXiubo Li se_cmd->t_bidi_data_sg, 877141685a3SXiubo Li se_cmd->t_bidi_data_nents, 878141685a3SXiubo Li &iov, &iov_cnt, false); 879141685a3SXiubo Li if (ret) { 880b6df4b79SXiubo Li tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt); 881b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 882b6df4b79SXiubo Li 883141685a3SXiubo Li pr_err("tcmu: alloc and scatter bidi data failed\n"); 884141685a3SXiubo Li return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 885141685a3SXiubo Li } 886ab22d260SXiubo Li } 887b3743c71SXiubo Li entry->req.iov_bidi_cnt = iov_cnt; 88826418649SSheng Yang 8890d44374cSMike Christie ret = tcmu_setup_cmd_timer(tcmu_cmd); 8900d44374cSMike Christie if (ret) { 8910d44374cSMike Christie tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt); 89297488c73SDan Carpenter mutex_unlock(&udev->cmdr_lock); 8930d44374cSMike Christie return TCM_OUT_OF_RESOURCES; 8940d44374cSMike Christie } 8950d44374cSMike Christie entry->hdr.cmd_id = tcmu_cmd->cmd_id; 8960d44374cSMike Christie 897fe25cc34SXiubo Li /* 898fe25cc34SXiubo Li * Recalaulate the command's base size and size according 899fe25cc34SXiubo Li * to the actual needs 900fe25cc34SXiubo Li */ 901fe25cc34SXiubo Li base_command_size = tcmu_cmd_get_base_cmd_size(entry->req.iov_cnt + 902fe25cc34SXiubo Li entry->req.iov_bidi_cnt); 903fe25cc34SXiubo Li command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size); 904fe25cc34SXiubo Li 905fe25cc34SXiubo Li tcmu_hdr_set_len(&entry->hdr.len_op, command_size); 906fe25cc34SXiubo Li 9077c9e7a6fSAndy Grover /* All offsets relative to mb_addr, not start of entry! */ 9087c9e7a6fSAndy Grover cdb_off = CMDR_OFF + cmd_head + base_command_size; 9097c9e7a6fSAndy Grover memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb)); 9107c9e7a6fSAndy Grover entry->req.cdb_off = cdb_off; 9117c9e7a6fSAndy Grover tcmu_flush_dcache_range(entry, sizeof(*entry)); 9127c9e7a6fSAndy Grover 9137c9e7a6fSAndy Grover UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size); 9147c9e7a6fSAndy Grover tcmu_flush_dcache_range(mb, sizeof(*mb)); 915b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 9167c9e7a6fSAndy Grover 9177c9e7a6fSAndy Grover /* TODO: only if FLUSH and FUA? */ 9187c9e7a6fSAndy Grover uio_event_notify(&udev->uio_info); 9197c9e7a6fSAndy Grover 920af980e46SMike Christie if (udev->cmd_time_out) 921af980e46SMike Christie mod_timer(&udev->timeout, round_jiffies_up(jiffies + 922af980e46SMike Christie msecs_to_jiffies(udev->cmd_time_out))); 9237c9e7a6fSAndy Grover 92402eb924fSAndy Grover return TCM_NO_SENSE; 9257c9e7a6fSAndy Grover } 9267c9e7a6fSAndy Grover 92702eb924fSAndy Grover static sense_reason_t 92802eb924fSAndy Grover tcmu_queue_cmd(struct se_cmd *se_cmd) 9297c9e7a6fSAndy Grover { 9307c9e7a6fSAndy Grover struct tcmu_cmd *tcmu_cmd; 931ecaf597bSBart Van Assche sense_reason_t ret; 9327c9e7a6fSAndy Grover 9337c9e7a6fSAndy Grover tcmu_cmd = tcmu_alloc_cmd(se_cmd); 9347c9e7a6fSAndy Grover if (!tcmu_cmd) 93502eb924fSAndy Grover return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 9367c9e7a6fSAndy Grover 9377c9e7a6fSAndy Grover ret = tcmu_queue_cmd_ring(tcmu_cmd); 93802eb924fSAndy Grover if (ret != TCM_NO_SENSE) { 9397c9e7a6fSAndy Grover pr_err("TCMU: Could not queue command\n"); 9407c9e7a6fSAndy Grover 941141685a3SXiubo Li tcmu_free_cmd(tcmu_cmd); 9427c9e7a6fSAndy Grover } 9437c9e7a6fSAndy Grover 9447c9e7a6fSAndy Grover return ret; 9457c9e7a6fSAndy Grover } 9467c9e7a6fSAndy Grover 9477c9e7a6fSAndy Grover static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry) 9487c9e7a6fSAndy Grover { 9497c9e7a6fSAndy Grover struct se_cmd *se_cmd = cmd->se_cmd; 9507c9e7a6fSAndy Grover struct tcmu_dev *udev = cmd->tcmu_dev; 9517c9e7a6fSAndy Grover 952b25c7863SSheng Yang /* 953b25c7863SSheng Yang * cmd has been completed already from timeout, just reclaim 9543d9b9555SAndy Grover * data area space and free cmd 955b25c7863SSheng Yang */ 956141685a3SXiubo Li if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) 957141685a3SXiubo Li goto out; 958b25c7863SSheng Yang 959141685a3SXiubo Li tcmu_cmd_reset_dbi_cur(cmd); 9607c9e7a6fSAndy Grover 9610ad46af8SAndy Grover if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) { 9620ad46af8SAndy Grover pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n", 9630ad46af8SAndy Grover cmd->se_cmd); 964ed97d0cdSAndy Grover entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION; 965ed97d0cdSAndy Grover } else if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) { 966406f74c2SMike Christie transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer); 967e4648b01SIlias Tsitsimpis } else if (se_cmd->se_cmd_flags & SCF_BIDI) { 96826418649SSheng Yang /* Get Data-In buffer before clean up */ 969a5d68ba8SXiubo Li gather_data_area(udev, cmd, true); 970e4648b01SIlias Tsitsimpis } else if (se_cmd->data_direction == DMA_FROM_DEVICE) { 971a5d68ba8SXiubo Li gather_data_area(udev, cmd, false); 9727c9e7a6fSAndy Grover } else if (se_cmd->data_direction == DMA_TO_DEVICE) { 973141685a3SXiubo Li /* TODO: */ 9742bc396a2SIlias Tsitsimpis } else if (se_cmd->data_direction != DMA_NONE) { 9752bc396a2SIlias Tsitsimpis pr_warn("TCMU: data direction was %d!\n", 9762bc396a2SIlias Tsitsimpis se_cmd->data_direction); 9777c9e7a6fSAndy Grover } 9787c9e7a6fSAndy Grover 9797c9e7a6fSAndy Grover target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status); 9807c9e7a6fSAndy Grover 981141685a3SXiubo Li out: 982141685a3SXiubo Li cmd->se_cmd = NULL; 983b6df4b79SXiubo Li tcmu_cmd_free_data(cmd, cmd->dbi_cnt); 984141685a3SXiubo Li tcmu_free_cmd(cmd); 9857c9e7a6fSAndy Grover } 9867c9e7a6fSAndy Grover 9877c9e7a6fSAndy Grover static unsigned int tcmu_handle_completions(struct tcmu_dev *udev) 9887c9e7a6fSAndy Grover { 9897c9e7a6fSAndy Grover struct tcmu_mailbox *mb; 9907c9e7a6fSAndy Grover int handled = 0; 9917c9e7a6fSAndy Grover 9927c9e7a6fSAndy Grover if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) { 9937c9e7a6fSAndy Grover pr_err("ring broken, not handling completions\n"); 9947c9e7a6fSAndy Grover return 0; 9957c9e7a6fSAndy Grover } 9967c9e7a6fSAndy Grover 9977c9e7a6fSAndy Grover mb = udev->mb_addr; 9987c9e7a6fSAndy Grover tcmu_flush_dcache_range(mb, sizeof(*mb)); 9997c9e7a6fSAndy Grover 10006aa7de05SMark Rutland while (udev->cmdr_last_cleaned != READ_ONCE(mb->cmd_tail)) { 10017c9e7a6fSAndy Grover 10027c9e7a6fSAndy Grover struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned; 10037c9e7a6fSAndy Grover struct tcmu_cmd *cmd; 10047c9e7a6fSAndy Grover 10057c9e7a6fSAndy Grover tcmu_flush_dcache_range(entry, sizeof(*entry)); 10067c9e7a6fSAndy Grover 10070ad46af8SAndy Grover if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) { 10080ad46af8SAndy Grover UPDATE_HEAD(udev->cmdr_last_cleaned, 10090ad46af8SAndy Grover tcmu_hdr_get_len(entry->hdr.len_op), 10100ad46af8SAndy Grover udev->cmdr_size); 10117c9e7a6fSAndy Grover continue; 10127c9e7a6fSAndy Grover } 10130ad46af8SAndy Grover WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD); 10147c9e7a6fSAndy Grover 10157c9e7a6fSAndy Grover spin_lock(&udev->commands_lock); 1016d3e709e6SMatthew Wilcox cmd = idr_remove(&udev->commands, entry->hdr.cmd_id); 10177c9e7a6fSAndy Grover spin_unlock(&udev->commands_lock); 10187c9e7a6fSAndy Grover 10197c9e7a6fSAndy Grover if (!cmd) { 10207c9e7a6fSAndy Grover pr_err("cmd_id not found, ring is broken\n"); 10217c9e7a6fSAndy Grover set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags); 10227c9e7a6fSAndy Grover break; 10237c9e7a6fSAndy Grover } 10247c9e7a6fSAndy Grover 10257c9e7a6fSAndy Grover tcmu_handle_completion(cmd, entry); 10267c9e7a6fSAndy Grover 10270ad46af8SAndy Grover UPDATE_HEAD(udev->cmdr_last_cleaned, 10280ad46af8SAndy Grover tcmu_hdr_get_len(entry->hdr.len_op), 10290ad46af8SAndy Grover udev->cmdr_size); 10307c9e7a6fSAndy Grover 10317c9e7a6fSAndy Grover handled++; 10327c9e7a6fSAndy Grover } 10337c9e7a6fSAndy Grover 10347c9e7a6fSAndy Grover if (mb->cmd_tail == mb->cmd_head) 10357c9e7a6fSAndy Grover del_timer(&udev->timeout); /* no more pending cmds */ 10367c9e7a6fSAndy Grover 10377c9e7a6fSAndy Grover wake_up(&udev->wait_cmdr); 10387c9e7a6fSAndy Grover 10397c9e7a6fSAndy Grover return handled; 10407c9e7a6fSAndy Grover } 10417c9e7a6fSAndy Grover 10427c9e7a6fSAndy Grover static int tcmu_check_expired_cmd(int id, void *p, void *data) 10437c9e7a6fSAndy Grover { 10447c9e7a6fSAndy Grover struct tcmu_cmd *cmd = p; 10457c9e7a6fSAndy Grover 10467c9e7a6fSAndy Grover if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) 10477c9e7a6fSAndy Grover return 0; 10487c9e7a6fSAndy Grover 1049611e2267SAndy Grover if (!time_after(jiffies, cmd->deadline)) 10507c9e7a6fSAndy Grover return 0; 10517c9e7a6fSAndy Grover 10527c9e7a6fSAndy Grover set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags); 10537c9e7a6fSAndy Grover target_complete_cmd(cmd->se_cmd, SAM_STAT_CHECK_CONDITION); 10547c9e7a6fSAndy Grover cmd->se_cmd = NULL; 10557c9e7a6fSAndy Grover 10567c9e7a6fSAndy Grover return 0; 10577c9e7a6fSAndy Grover } 10587c9e7a6fSAndy Grover 1059e99e88a9SKees Cook static void tcmu_device_timedout(struct timer_list *t) 10607c9e7a6fSAndy Grover { 1061e99e88a9SKees Cook struct tcmu_dev *udev = from_timer(udev, t, timeout); 10627c9e7a6fSAndy Grover unsigned long flags; 10637c9e7a6fSAndy Grover 10647c9e7a6fSAndy Grover spin_lock_irqsave(&udev->commands_lock, flags); 10657c9e7a6fSAndy Grover idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL); 10667c9e7a6fSAndy Grover spin_unlock_irqrestore(&udev->commands_lock, flags); 10677c9e7a6fSAndy Grover 1068b6df4b79SXiubo Li /* Try to wake up the ummap thread */ 1069b6df4b79SXiubo Li wake_up(&unmap_wait); 1070b6df4b79SXiubo Li 10717c9e7a6fSAndy Grover /* 10727c9e7a6fSAndy Grover * We don't need to wakeup threads on wait_cmdr since they have their 10737c9e7a6fSAndy Grover * own timeout. 10747c9e7a6fSAndy Grover */ 10757c9e7a6fSAndy Grover } 10767c9e7a6fSAndy Grover 10777c9e7a6fSAndy Grover static int tcmu_attach_hba(struct se_hba *hba, u32 host_id) 10787c9e7a6fSAndy Grover { 10797c9e7a6fSAndy Grover struct tcmu_hba *tcmu_hba; 10807c9e7a6fSAndy Grover 10817c9e7a6fSAndy Grover tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL); 10827c9e7a6fSAndy Grover if (!tcmu_hba) 10837c9e7a6fSAndy Grover return -ENOMEM; 10847c9e7a6fSAndy Grover 10857c9e7a6fSAndy Grover tcmu_hba->host_id = host_id; 10867c9e7a6fSAndy Grover hba->hba_ptr = tcmu_hba; 10877c9e7a6fSAndy Grover 10887c9e7a6fSAndy Grover return 0; 10897c9e7a6fSAndy Grover } 10907c9e7a6fSAndy Grover 10917c9e7a6fSAndy Grover static void tcmu_detach_hba(struct se_hba *hba) 10927c9e7a6fSAndy Grover { 10937c9e7a6fSAndy Grover kfree(hba->hba_ptr); 10947c9e7a6fSAndy Grover hba->hba_ptr = NULL; 10957c9e7a6fSAndy Grover } 10967c9e7a6fSAndy Grover 10977c9e7a6fSAndy Grover static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name) 10987c9e7a6fSAndy Grover { 10997c9e7a6fSAndy Grover struct tcmu_dev *udev; 11007c9e7a6fSAndy Grover 11017c9e7a6fSAndy Grover udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL); 11027c9e7a6fSAndy Grover if (!udev) 11037c9e7a6fSAndy Grover return NULL; 1104f3cdbe39SMike Christie kref_init(&udev->kref); 11057c9e7a6fSAndy Grover 11067c9e7a6fSAndy Grover udev->name = kstrdup(name, GFP_KERNEL); 11077c9e7a6fSAndy Grover if (!udev->name) { 11087c9e7a6fSAndy Grover kfree(udev); 11097c9e7a6fSAndy Grover return NULL; 11107c9e7a6fSAndy Grover } 11117c9e7a6fSAndy Grover 11127c9e7a6fSAndy Grover udev->hba = hba; 1113af980e46SMike Christie udev->cmd_time_out = TCMU_TIME_OUT; 11147c9e7a6fSAndy Grover 11157c9e7a6fSAndy Grover init_waitqueue_head(&udev->wait_cmdr); 1116b6df4b79SXiubo Li mutex_init(&udev->cmdr_lock); 11177c9e7a6fSAndy Grover 11187c9e7a6fSAndy Grover idr_init(&udev->commands); 11197c9e7a6fSAndy Grover spin_lock_init(&udev->commands_lock); 11207c9e7a6fSAndy Grover 1121e99e88a9SKees Cook timer_setup(&udev->timeout, tcmu_device_timedout, 0); 11227c9e7a6fSAndy Grover 1123b3af66e2SMike Christie init_waitqueue_head(&udev->nl_cmd_wq); 1124b3af66e2SMike Christie spin_lock_init(&udev->nl_cmd_lock); 1125b3af66e2SMike Christie 1126c22adc0bSXiubo Li INIT_RADIX_TREE(&udev->data_blocks, GFP_KERNEL); 1127c22adc0bSXiubo Li 11287c9e7a6fSAndy Grover return &udev->se_dev; 11297c9e7a6fSAndy Grover } 11307c9e7a6fSAndy Grover 11317c9e7a6fSAndy Grover static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on) 11327c9e7a6fSAndy Grover { 11337c9e7a6fSAndy Grover struct tcmu_dev *tcmu_dev = container_of(info, struct tcmu_dev, uio_info); 11347c9e7a6fSAndy Grover 1135b6df4b79SXiubo Li mutex_lock(&tcmu_dev->cmdr_lock); 11367c9e7a6fSAndy Grover tcmu_handle_completions(tcmu_dev); 1137b6df4b79SXiubo Li mutex_unlock(&tcmu_dev->cmdr_lock); 11387c9e7a6fSAndy Grover 11397c9e7a6fSAndy Grover return 0; 11407c9e7a6fSAndy Grover } 11417c9e7a6fSAndy Grover 11427c9e7a6fSAndy Grover /* 11437c9e7a6fSAndy Grover * mmap code from uio.c. Copied here because we want to hook mmap() 11447c9e7a6fSAndy Grover * and this stuff must come along. 11457c9e7a6fSAndy Grover */ 11467c9e7a6fSAndy Grover static int tcmu_find_mem_index(struct vm_area_struct *vma) 11477c9e7a6fSAndy Grover { 11487c9e7a6fSAndy Grover struct tcmu_dev *udev = vma->vm_private_data; 11497c9e7a6fSAndy Grover struct uio_info *info = &udev->uio_info; 11507c9e7a6fSAndy Grover 11517c9e7a6fSAndy Grover if (vma->vm_pgoff < MAX_UIO_MAPS) { 11527c9e7a6fSAndy Grover if (info->mem[vma->vm_pgoff].size == 0) 11537c9e7a6fSAndy Grover return -1; 11547c9e7a6fSAndy Grover return (int)vma->vm_pgoff; 11557c9e7a6fSAndy Grover } 11567c9e7a6fSAndy Grover return -1; 11577c9e7a6fSAndy Grover } 11587c9e7a6fSAndy Grover 1159b6df4b79SXiubo Li static struct page *tcmu_try_get_block_page(struct tcmu_dev *udev, uint32_t dbi) 1160b6df4b79SXiubo Li { 1161b6df4b79SXiubo Li struct page *page; 1162b6df4b79SXiubo Li int ret; 1163b6df4b79SXiubo Li 1164b6df4b79SXiubo Li mutex_lock(&udev->cmdr_lock); 1165b6df4b79SXiubo Li page = tcmu_get_block_page(udev, dbi); 1166b6df4b79SXiubo Li if (likely(page)) { 1167b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1168b6df4b79SXiubo Li return page; 1169b6df4b79SXiubo Li } 1170b6df4b79SXiubo Li 1171b6df4b79SXiubo Li /* 1172b6df4b79SXiubo Li * Normally it shouldn't be here: 1173b6df4b79SXiubo Li * Only when the userspace has touched the blocks which 1174b6df4b79SXiubo Li * are out of the tcmu_cmd's data iov[], and will return 1175b6df4b79SXiubo Li * one zeroed page. 1176b6df4b79SXiubo Li */ 1177b6df4b79SXiubo Li pr_warn("Block(%u) out of cmd's iov[] has been touched!\n", dbi); 1178b6df4b79SXiubo Li pr_warn("Mostly it will be a bug of userspace, please have a check!\n"); 1179b6df4b79SXiubo Li 1180b6df4b79SXiubo Li if (dbi >= udev->dbi_thresh) { 1181b6df4b79SXiubo Li /* Extern the udev->dbi_thresh to dbi + 1 */ 1182b6df4b79SXiubo Li udev->dbi_thresh = dbi + 1; 1183b6df4b79SXiubo Li udev->dbi_max = dbi; 1184b6df4b79SXiubo Li } 1185b6df4b79SXiubo Li 1186b6df4b79SXiubo Li page = radix_tree_lookup(&udev->data_blocks, dbi); 1187b6df4b79SXiubo Li if (!page) { 1188b6df4b79SXiubo Li page = alloc_page(GFP_KERNEL | __GFP_ZERO); 1189b6df4b79SXiubo Li if (!page) { 1190b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1191b6df4b79SXiubo Li return NULL; 1192b6df4b79SXiubo Li } 1193b6df4b79SXiubo Li 1194b6df4b79SXiubo Li ret = radix_tree_insert(&udev->data_blocks, dbi, page); 1195b6df4b79SXiubo Li if (ret) { 1196b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1197b6df4b79SXiubo Li __free_page(page); 1198b6df4b79SXiubo Li return NULL; 1199b6df4b79SXiubo Li } 1200b6df4b79SXiubo Li 1201b6df4b79SXiubo Li /* 1202b6df4b79SXiubo Li * Since this case is rare in page fault routine, here we 1203b6df4b79SXiubo Li * will allow the global_db_count >= TCMU_GLOBAL_MAX_BLOCKS 1204b6df4b79SXiubo Li * to reduce possible page fault call trace. 1205b6df4b79SXiubo Li */ 1206b6df4b79SXiubo Li atomic_inc(&global_db_count); 1207b6df4b79SXiubo Li } 1208b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1209b6df4b79SXiubo Li 1210b6df4b79SXiubo Li return page; 1211b6df4b79SXiubo Li } 1212b6df4b79SXiubo Li 121311bac800SDave Jiang static int tcmu_vma_fault(struct vm_fault *vmf) 12147c9e7a6fSAndy Grover { 121511bac800SDave Jiang struct tcmu_dev *udev = vmf->vma->vm_private_data; 12167c9e7a6fSAndy Grover struct uio_info *info = &udev->uio_info; 12177c9e7a6fSAndy Grover struct page *page; 12187c9e7a6fSAndy Grover unsigned long offset; 12197c9e7a6fSAndy Grover void *addr; 12207c9e7a6fSAndy Grover 122111bac800SDave Jiang int mi = tcmu_find_mem_index(vmf->vma); 12227c9e7a6fSAndy Grover if (mi < 0) 12237c9e7a6fSAndy Grover return VM_FAULT_SIGBUS; 12247c9e7a6fSAndy Grover 12257c9e7a6fSAndy Grover /* 12267c9e7a6fSAndy Grover * We need to subtract mi because userspace uses offset = N*PAGE_SIZE 12277c9e7a6fSAndy Grover * to use mem[N]. 12287c9e7a6fSAndy Grover */ 12297c9e7a6fSAndy Grover offset = (vmf->pgoff - mi) << PAGE_SHIFT; 12307c9e7a6fSAndy Grover 1231141685a3SXiubo Li if (offset < udev->data_off) { 1232141685a3SXiubo Li /* For the vmalloc()ed cmd area pages */ 12337c9e7a6fSAndy Grover addr = (void *)(unsigned long)info->mem[mi].addr + offset; 12347c9e7a6fSAndy Grover page = vmalloc_to_page(addr); 1235141685a3SXiubo Li } else { 1236141685a3SXiubo Li uint32_t dbi; 1237141685a3SXiubo Li 1238b6df4b79SXiubo Li /* For the dynamically growing data area pages */ 1239141685a3SXiubo Li dbi = (offset - udev->data_off) / DATA_BLOCK_SIZE; 1240b6df4b79SXiubo Li page = tcmu_try_get_block_page(udev, dbi); 1241b6df4b79SXiubo Li if (!page) 1242141685a3SXiubo Li return VM_FAULT_NOPAGE; 1243141685a3SXiubo Li } 1244141685a3SXiubo Li 12457c9e7a6fSAndy Grover get_page(page); 12467c9e7a6fSAndy Grover vmf->page = page; 12477c9e7a6fSAndy Grover return 0; 12487c9e7a6fSAndy Grover } 12497c9e7a6fSAndy Grover 12507c9e7a6fSAndy Grover static const struct vm_operations_struct tcmu_vm_ops = { 12517c9e7a6fSAndy Grover .fault = tcmu_vma_fault, 12527c9e7a6fSAndy Grover }; 12537c9e7a6fSAndy Grover 12547c9e7a6fSAndy Grover static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma) 12557c9e7a6fSAndy Grover { 12567c9e7a6fSAndy Grover struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 12577c9e7a6fSAndy Grover 12587c9e7a6fSAndy Grover vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 12597c9e7a6fSAndy Grover vma->vm_ops = &tcmu_vm_ops; 12607c9e7a6fSAndy Grover 12617c9e7a6fSAndy Grover vma->vm_private_data = udev; 12627c9e7a6fSAndy Grover 12637c9e7a6fSAndy Grover /* Ensure the mmap is exactly the right size */ 12647c9e7a6fSAndy Grover if (vma_pages(vma) != (TCMU_RING_SIZE >> PAGE_SHIFT)) 12657c9e7a6fSAndy Grover return -EINVAL; 12667c9e7a6fSAndy Grover 12677c9e7a6fSAndy Grover return 0; 12687c9e7a6fSAndy Grover } 12697c9e7a6fSAndy Grover 12707c9e7a6fSAndy Grover static int tcmu_open(struct uio_info *info, struct inode *inode) 12717c9e7a6fSAndy Grover { 12727c9e7a6fSAndy Grover struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 12737c9e7a6fSAndy Grover 12747c9e7a6fSAndy Grover /* O_EXCL not supported for char devs, so fake it? */ 12757c9e7a6fSAndy Grover if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags)) 12767c9e7a6fSAndy Grover return -EBUSY; 12777c9e7a6fSAndy Grover 1278b6df4b79SXiubo Li udev->inode = inode; 12799260695dSMike Christie kref_get(&udev->kref); 1280b6df4b79SXiubo Li 12817c9e7a6fSAndy Grover pr_debug("open\n"); 12827c9e7a6fSAndy Grover 12837c9e7a6fSAndy Grover return 0; 12847c9e7a6fSAndy Grover } 12857c9e7a6fSAndy Grover 1286f3cdbe39SMike Christie static void tcmu_dev_call_rcu(struct rcu_head *p) 1287f3cdbe39SMike Christie { 1288f3cdbe39SMike Christie struct se_device *dev = container_of(p, struct se_device, rcu_head); 1289f3cdbe39SMike Christie struct tcmu_dev *udev = TCMU_DEV(dev); 1290f3cdbe39SMike Christie 1291f3cdbe39SMike Christie kfree(udev->uio_info.name); 1292f3cdbe39SMike Christie kfree(udev->name); 1293f3cdbe39SMike Christie kfree(udev); 1294f3cdbe39SMike Christie } 1295f3cdbe39SMike Christie 1296c22adc0bSXiubo Li static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd) 1297c22adc0bSXiubo Li { 1298c22adc0bSXiubo Li if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { 1299c22adc0bSXiubo Li kmem_cache_free(tcmu_cmd_cache, cmd); 1300c22adc0bSXiubo Li return 0; 1301c22adc0bSXiubo Li } 1302c22adc0bSXiubo Li return -EINVAL; 1303c22adc0bSXiubo Li } 1304c22adc0bSXiubo Li 1305bf99ec13SMike Christie static void tcmu_blocks_release(struct radix_tree_root *blocks, 1306bf99ec13SMike Christie int start, int end) 1307c22adc0bSXiubo Li { 1308c22adc0bSXiubo Li int i; 1309c22adc0bSXiubo Li struct page *page; 1310c22adc0bSXiubo Li 1311bf99ec13SMike Christie for (i = start; i < end; i++) { 1312bf99ec13SMike Christie page = radix_tree_delete(blocks, i); 1313c22adc0bSXiubo Li if (page) { 1314c22adc0bSXiubo Li __free_page(page); 1315c22adc0bSXiubo Li atomic_dec(&global_db_count); 1316c22adc0bSXiubo Li } 1317c22adc0bSXiubo Li } 1318c22adc0bSXiubo Li } 1319c22adc0bSXiubo Li 1320f3cdbe39SMike Christie static void tcmu_dev_kref_release(struct kref *kref) 1321f3cdbe39SMike Christie { 1322f3cdbe39SMike Christie struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref); 1323f3cdbe39SMike Christie struct se_device *dev = &udev->se_dev; 1324c22adc0bSXiubo Li struct tcmu_cmd *cmd; 1325c22adc0bSXiubo Li bool all_expired = true; 1326c22adc0bSXiubo Li int i; 1327c22adc0bSXiubo Li 1328c22adc0bSXiubo Li vfree(udev->mb_addr); 1329c22adc0bSXiubo Li udev->mb_addr = NULL; 1330c22adc0bSXiubo Li 1331c22adc0bSXiubo Li /* Upper layer should drain all requests before calling this */ 1332c22adc0bSXiubo Li spin_lock_irq(&udev->commands_lock); 1333c22adc0bSXiubo Li idr_for_each_entry(&udev->commands, cmd, i) { 1334c22adc0bSXiubo Li if (tcmu_check_and_free_pending_cmd(cmd) != 0) 1335c22adc0bSXiubo Li all_expired = false; 1336c22adc0bSXiubo Li } 1337c22adc0bSXiubo Li idr_destroy(&udev->commands); 1338c22adc0bSXiubo Li spin_unlock_irq(&udev->commands_lock); 1339c22adc0bSXiubo Li WARN_ON(!all_expired); 1340c22adc0bSXiubo Li 1341bf99ec13SMike Christie mutex_lock(&udev->cmdr_lock); 1342bf99ec13SMike Christie tcmu_blocks_release(&udev->data_blocks, 0, udev->dbi_max + 1); 1343bf99ec13SMike Christie mutex_unlock(&udev->cmdr_lock); 1344f3cdbe39SMike Christie 1345f3cdbe39SMike Christie call_rcu(&dev->rcu_head, tcmu_dev_call_rcu); 1346f3cdbe39SMike Christie } 1347f3cdbe39SMike Christie 13487c9e7a6fSAndy Grover static int tcmu_release(struct uio_info *info, struct inode *inode) 13497c9e7a6fSAndy Grover { 13507c9e7a6fSAndy Grover struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 13517c9e7a6fSAndy Grover 13527c9e7a6fSAndy Grover clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags); 13537c9e7a6fSAndy Grover 13547c9e7a6fSAndy Grover pr_debug("close\n"); 13559260695dSMike Christie /* release ref from open */ 1356f3cdbe39SMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 13577c9e7a6fSAndy Grover return 0; 13587c9e7a6fSAndy Grover } 13597c9e7a6fSAndy Grover 1360b3af66e2SMike Christie static void tcmu_init_genl_cmd_reply(struct tcmu_dev *udev, int cmd) 1361b3af66e2SMike Christie { 1362b3af66e2SMike Christie struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; 1363b3af66e2SMike Christie 1364b3af66e2SMike Christie if (!tcmu_kern_cmd_reply_supported) 1365b3af66e2SMike Christie return; 1366b849b456SKenjiro Nakayama 1367b849b456SKenjiro Nakayama if (udev->nl_reply_supported <= 0) 1368b849b456SKenjiro Nakayama return; 1369b849b456SKenjiro Nakayama 1370b3af66e2SMike Christie relock: 1371b3af66e2SMike Christie spin_lock(&udev->nl_cmd_lock); 1372b3af66e2SMike Christie 1373b3af66e2SMike Christie if (nl_cmd->cmd != TCMU_CMD_UNSPEC) { 1374b3af66e2SMike Christie spin_unlock(&udev->nl_cmd_lock); 1375b3af66e2SMike Christie pr_debug("sleeping for open nl cmd\n"); 1376b3af66e2SMike Christie wait_event(udev->nl_cmd_wq, (nl_cmd->cmd == TCMU_CMD_UNSPEC)); 1377b3af66e2SMike Christie goto relock; 1378b3af66e2SMike Christie } 1379b3af66e2SMike Christie 1380b3af66e2SMike Christie memset(nl_cmd, 0, sizeof(*nl_cmd)); 1381b3af66e2SMike Christie nl_cmd->cmd = cmd; 1382b3af66e2SMike Christie init_completion(&nl_cmd->complete); 1383b3af66e2SMike Christie 1384b3af66e2SMike Christie spin_unlock(&udev->nl_cmd_lock); 1385b3af66e2SMike Christie } 1386b3af66e2SMike Christie 1387b3af66e2SMike Christie static int tcmu_wait_genl_cmd_reply(struct tcmu_dev *udev) 1388b3af66e2SMike Christie { 1389b3af66e2SMike Christie struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; 1390b3af66e2SMike Christie int ret; 1391b3af66e2SMike Christie DEFINE_WAIT(__wait); 1392b3af66e2SMike Christie 1393b3af66e2SMike Christie if (!tcmu_kern_cmd_reply_supported) 1394b3af66e2SMike Christie return 0; 1395b3af66e2SMike Christie 1396b849b456SKenjiro Nakayama if (udev->nl_reply_supported <= 0) 1397b849b456SKenjiro Nakayama return 0; 1398b849b456SKenjiro Nakayama 1399b3af66e2SMike Christie pr_debug("sleeping for nl reply\n"); 1400b3af66e2SMike Christie wait_for_completion(&nl_cmd->complete); 1401b3af66e2SMike Christie 1402b3af66e2SMike Christie spin_lock(&udev->nl_cmd_lock); 1403b3af66e2SMike Christie nl_cmd->cmd = TCMU_CMD_UNSPEC; 1404b3af66e2SMike Christie ret = nl_cmd->status; 1405b3af66e2SMike Christie nl_cmd->status = 0; 1406b3af66e2SMike Christie spin_unlock(&udev->nl_cmd_lock); 1407b3af66e2SMike Christie 1408b3af66e2SMike Christie wake_up_all(&udev->nl_cmd_wq); 1409b3af66e2SMike Christie 1410b3af66e2SMike Christie return ret;; 1411b3af66e2SMike Christie } 1412b3af66e2SMike Christie 1413b3af66e2SMike Christie static int tcmu_netlink_event(struct tcmu_dev *udev, enum tcmu_genl_cmd cmd, 1414b3af66e2SMike Christie int reconfig_attr, const void *reconfig_data) 14157c9e7a6fSAndy Grover { 14167c9e7a6fSAndy Grover struct sk_buff *skb; 14177c9e7a6fSAndy Grover void *msg_header; 14186e14eab9SNicholas Bellinger int ret = -ENOMEM; 14197c9e7a6fSAndy Grover 14207c9e7a6fSAndy Grover skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 14217c9e7a6fSAndy Grover if (!skb) 14226e14eab9SNicholas Bellinger return ret; 14237c9e7a6fSAndy Grover 14247c9e7a6fSAndy Grover msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd); 14256e14eab9SNicholas Bellinger if (!msg_header) 14266e14eab9SNicholas Bellinger goto free_skb; 14277c9e7a6fSAndy Grover 1428b3af66e2SMike Christie ret = nla_put_string(skb, TCMU_ATTR_DEVICE, udev->uio_info.name); 14296e14eab9SNicholas Bellinger if (ret < 0) 14306e14eab9SNicholas Bellinger goto free_skb; 14317c9e7a6fSAndy Grover 1432b3af66e2SMike Christie ret = nla_put_u32(skb, TCMU_ATTR_MINOR, udev->uio_info.uio_dev->minor); 1433b3af66e2SMike Christie if (ret < 0) 1434b3af66e2SMike Christie goto free_skb; 1435b3af66e2SMike Christie 1436b3af66e2SMike Christie ret = nla_put_u32(skb, TCMU_ATTR_DEVICE_ID, udev->se_dev.dev_index); 14376e14eab9SNicholas Bellinger if (ret < 0) 14386e14eab9SNicholas Bellinger goto free_skb; 14397c9e7a6fSAndy Grover 14402d76443eSMike Christie if (cmd == TCMU_CMD_RECONFIG_DEVICE) { 14412d76443eSMike Christie switch (reconfig_attr) { 14422d76443eSMike Christie case TCMU_ATTR_DEV_CFG: 14432d76443eSMike Christie ret = nla_put_string(skb, reconfig_attr, reconfig_data); 14442d76443eSMike Christie break; 14452d76443eSMike Christie case TCMU_ATTR_DEV_SIZE: 14462d76443eSMike Christie ret = nla_put_u64_64bit(skb, reconfig_attr, 14472d76443eSMike Christie *((u64 *)reconfig_data), 14482d76443eSMike Christie TCMU_ATTR_PAD); 14492d76443eSMike Christie break; 14502d76443eSMike Christie case TCMU_ATTR_WRITECACHE: 14512d76443eSMike Christie ret = nla_put_u8(skb, reconfig_attr, 14522d76443eSMike Christie *((u8 *)reconfig_data)); 14532d76443eSMike Christie break; 14542d76443eSMike Christie default: 14552d76443eSMike Christie BUG(); 14562d76443eSMike Christie } 14572d76443eSMike Christie 14588a45885cSBryant G. Ly if (ret < 0) 14598a45885cSBryant G. Ly goto free_skb; 14602d76443eSMike Christie } 14618a45885cSBryant G. Ly 1462053c095aSJohannes Berg genlmsg_end(skb, msg_header); 14637c9e7a6fSAndy Grover 1464b3af66e2SMike Christie tcmu_init_genl_cmd_reply(udev, cmd); 1465b3af66e2SMike Christie 146620c08b36SSheng Yang ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0, 14677c9e7a6fSAndy Grover TCMU_MCGRP_CONFIG, GFP_KERNEL); 14687c9e7a6fSAndy Grover /* We don't care if no one is listening */ 14697c9e7a6fSAndy Grover if (ret == -ESRCH) 14707c9e7a6fSAndy Grover ret = 0; 1471b3af66e2SMike Christie if (!ret) 1472b3af66e2SMike Christie ret = tcmu_wait_genl_cmd_reply(udev); 14737c9e7a6fSAndy Grover 14747c9e7a6fSAndy Grover return ret; 14756e14eab9SNicholas Bellinger free_skb: 14766e14eab9SNicholas Bellinger nlmsg_free(skb); 14776e14eab9SNicholas Bellinger return ret; 14787c9e7a6fSAndy Grover } 14797c9e7a6fSAndy Grover 1480de8c5221SBryant G. Ly static int tcmu_update_uio_info(struct tcmu_dev *udev) 14817c9e7a6fSAndy Grover { 14827c9e7a6fSAndy Grover struct tcmu_hba *hba = udev->hba->hba_ptr; 14837c9e7a6fSAndy Grover struct uio_info *info; 1484de8c5221SBryant G. Ly size_t size, used; 14857c9e7a6fSAndy Grover char *str; 14867c9e7a6fSAndy Grover 14877c9e7a6fSAndy Grover info = &udev->uio_info; 14887c9e7a6fSAndy Grover size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name, 14897c9e7a6fSAndy Grover udev->dev_config); 14907c9e7a6fSAndy Grover size += 1; /* for \0 */ 14917c9e7a6fSAndy Grover str = kmalloc(size, GFP_KERNEL); 14927c9e7a6fSAndy Grover if (!str) 14937c9e7a6fSAndy Grover return -ENOMEM; 14947c9e7a6fSAndy Grover 14957c9e7a6fSAndy Grover used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name); 14967c9e7a6fSAndy Grover if (udev->dev_config[0]) 14977c9e7a6fSAndy Grover snprintf(str + used, size - used, "/%s", udev->dev_config); 14987c9e7a6fSAndy Grover 1499ededd039SBryant G. Ly /* If the old string exists, free it */ 1500ededd039SBryant G. Ly kfree(info->name); 15017c9e7a6fSAndy Grover info->name = str; 15027c9e7a6fSAndy Grover 1503de8c5221SBryant G. Ly return 0; 1504de8c5221SBryant G. Ly } 1505de8c5221SBryant G. Ly 1506de8c5221SBryant G. Ly static int tcmu_configure_device(struct se_device *dev) 1507de8c5221SBryant G. Ly { 1508de8c5221SBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(dev); 1509de8c5221SBryant G. Ly struct uio_info *info; 1510de8c5221SBryant G. Ly struct tcmu_mailbox *mb; 1511de8c5221SBryant G. Ly int ret = 0; 1512de8c5221SBryant G. Ly 1513de8c5221SBryant G. Ly ret = tcmu_update_uio_info(udev); 1514de8c5221SBryant G. Ly if (ret) 1515de8c5221SBryant G. Ly return ret; 1516de8c5221SBryant G. Ly 1517de8c5221SBryant G. Ly info = &udev->uio_info; 1518de8c5221SBryant G. Ly 1519141685a3SXiubo Li udev->mb_addr = vzalloc(CMDR_SIZE); 15207c9e7a6fSAndy Grover if (!udev->mb_addr) { 15217c9e7a6fSAndy Grover ret = -ENOMEM; 15227c9e7a6fSAndy Grover goto err_vzalloc; 15237c9e7a6fSAndy Grover } 15247c9e7a6fSAndy Grover 15257c9e7a6fSAndy Grover /* mailbox fits in first part of CMDR space */ 15267c9e7a6fSAndy Grover udev->cmdr_size = CMDR_SIZE - CMDR_OFF; 15277c9e7a6fSAndy Grover udev->data_off = CMDR_SIZE; 1528141685a3SXiubo Li udev->data_size = DATA_SIZE; 1529b6df4b79SXiubo Li udev->dbi_thresh = 0; /* Default in Idle state */ 1530b6df4b79SXiubo Li udev->waiting_global = false; 15317c9e7a6fSAndy Grover 1532141685a3SXiubo Li /* Initialise the mailbox of the ring buffer */ 15337c9e7a6fSAndy Grover mb = udev->mb_addr; 15340ad46af8SAndy Grover mb->version = TCMU_MAILBOX_VERSION; 153532c76de3SSheng Yang mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC; 15367c9e7a6fSAndy Grover mb->cmdr_off = CMDR_OFF; 15377c9e7a6fSAndy Grover mb->cmdr_size = udev->cmdr_size; 15387c9e7a6fSAndy Grover 15397c9e7a6fSAndy Grover WARN_ON(!PAGE_ALIGNED(udev->data_off)); 15407c9e7a6fSAndy Grover WARN_ON(udev->data_size % PAGE_SIZE); 154126418649SSheng Yang WARN_ON(udev->data_size % DATA_BLOCK_SIZE); 15427c9e7a6fSAndy Grover 1543ac64a2ceSDavid Disseldorp info->version = __stringify(TCMU_MAILBOX_VERSION); 15447c9e7a6fSAndy Grover 15457c9e7a6fSAndy Grover info->mem[0].name = "tcm-user command & data buffer"; 15460633e123SArnd Bergmann info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr; 15477c9e7a6fSAndy Grover info->mem[0].size = TCMU_RING_SIZE; 1548141685a3SXiubo Li info->mem[0].memtype = UIO_MEM_NONE; 15497c9e7a6fSAndy Grover 15507c9e7a6fSAndy Grover info->irqcontrol = tcmu_irqcontrol; 15517c9e7a6fSAndy Grover info->irq = UIO_IRQ_CUSTOM; 15527c9e7a6fSAndy Grover 15537c9e7a6fSAndy Grover info->mmap = tcmu_mmap; 15547c9e7a6fSAndy Grover info->open = tcmu_open; 15557c9e7a6fSAndy Grover info->release = tcmu_release; 15567c9e7a6fSAndy Grover 15577c9e7a6fSAndy Grover ret = uio_register_device(tcmu_root_device, info); 15587c9e7a6fSAndy Grover if (ret) 15597c9e7a6fSAndy Grover goto err_register; 15607c9e7a6fSAndy Grover 156181ee28deSSheng Yang /* User can set hw_block_size before enable the device */ 156281ee28deSSheng Yang if (dev->dev_attrib.hw_block_size == 0) 15637c9e7a6fSAndy Grover dev->dev_attrib.hw_block_size = 512; 156481ee28deSSheng Yang /* Other attributes can be configured in userspace */ 15653abaa2bfSMike Christie if (!dev->dev_attrib.hw_max_sectors) 15667c9e7a6fSAndy Grover dev->dev_attrib.hw_max_sectors = 128; 15679a8bb606SBryant G. Ly if (!dev->dev_attrib.emulate_write_cache) 15689a8bb606SBryant G. Ly dev->dev_attrib.emulate_write_cache = 0; 15697c9e7a6fSAndy Grover dev->dev_attrib.hw_queue_depth = 128; 15707c9e7a6fSAndy Grover 1571b849b456SKenjiro Nakayama /* If user didn't explicitly disable netlink reply support, use 1572b849b456SKenjiro Nakayama * module scope setting. 1573b849b456SKenjiro Nakayama */ 1574b849b456SKenjiro Nakayama if (udev->nl_reply_supported >= 0) 1575b849b456SKenjiro Nakayama udev->nl_reply_supported = tcmu_kern_cmd_reply_supported; 1576b849b456SKenjiro Nakayama 1577f3cdbe39SMike Christie /* 1578f3cdbe39SMike Christie * Get a ref incase userspace does a close on the uio device before 1579f3cdbe39SMike Christie * LIO has initiated tcmu_free_device. 1580f3cdbe39SMike Christie */ 1581f3cdbe39SMike Christie kref_get(&udev->kref); 1582f3cdbe39SMike Christie 1583b3af66e2SMike Christie ret = tcmu_netlink_event(udev, TCMU_CMD_ADDED_DEVICE, 0, NULL); 15847c9e7a6fSAndy Grover if (ret) 15857c9e7a6fSAndy Grover goto err_netlink; 15867c9e7a6fSAndy Grover 1587b6df4b79SXiubo Li mutex_lock(&root_udev_mutex); 1588b6df4b79SXiubo Li list_add(&udev->node, &root_udev); 1589b6df4b79SXiubo Li mutex_unlock(&root_udev_mutex); 1590b6df4b79SXiubo Li 15917c9e7a6fSAndy Grover return 0; 15927c9e7a6fSAndy Grover 15937c9e7a6fSAndy Grover err_netlink: 1594f3cdbe39SMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 15957c9e7a6fSAndy Grover uio_unregister_device(&udev->uio_info); 15967c9e7a6fSAndy Grover err_register: 15977c9e7a6fSAndy Grover vfree(udev->mb_addr); 1598c22adc0bSXiubo Li udev->mb_addr = NULL; 15997c9e7a6fSAndy Grover err_vzalloc: 16007c9e7a6fSAndy Grover kfree(info->name); 1601f3cdbe39SMike Christie info->name = NULL; 16027c9e7a6fSAndy Grover 16037c9e7a6fSAndy Grover return ret; 16047c9e7a6fSAndy Grover } 16057c9e7a6fSAndy Grover 1606972c7f16SMike Christie static bool tcmu_dev_configured(struct tcmu_dev *udev) 1607972c7f16SMike Christie { 1608972c7f16SMike Christie return udev->uio_info.uio_dev ? true : false; 1609972c7f16SMike Christie } 1610972c7f16SMike Christie 16117c9e7a6fSAndy Grover static void tcmu_free_device(struct se_device *dev) 16127c9e7a6fSAndy Grover { 16137c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 161492634706SMike Christie 161592634706SMike Christie /* release ref from init */ 161692634706SMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 161792634706SMike Christie } 161892634706SMike Christie 161992634706SMike Christie static void tcmu_destroy_device(struct se_device *dev) 162092634706SMike Christie { 162192634706SMike Christie struct tcmu_dev *udev = TCMU_DEV(dev); 16227c9e7a6fSAndy Grover 16237c9e7a6fSAndy Grover del_timer_sync(&udev->timeout); 16247c9e7a6fSAndy Grover 1625b6df4b79SXiubo Li mutex_lock(&root_udev_mutex); 1626b6df4b79SXiubo Li list_del(&udev->node); 1627b6df4b79SXiubo Li mutex_unlock(&root_udev_mutex); 1628b6df4b79SXiubo Li 1629b3af66e2SMike Christie tcmu_netlink_event(udev, TCMU_CMD_REMOVED_DEVICE, 0, NULL); 16307c9e7a6fSAndy Grover 16317c9e7a6fSAndy Grover uio_unregister_device(&udev->uio_info); 16329260695dSMike Christie 16339260695dSMike Christie /* release ref from configure */ 16349260695dSMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 16357c9e7a6fSAndy Grover } 16367c9e7a6fSAndy Grover 16377c9e7a6fSAndy Grover enum { 16383abaa2bfSMike Christie Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors, 1639b849b456SKenjiro Nakayama Opt_nl_reply_supported, Opt_err, 16407c9e7a6fSAndy Grover }; 16417c9e7a6fSAndy Grover 16427c9e7a6fSAndy Grover static match_table_t tokens = { 16437c9e7a6fSAndy Grover {Opt_dev_config, "dev_config=%s"}, 16447c9e7a6fSAndy Grover {Opt_dev_size, "dev_size=%u"}, 16459c1cd1b6SAndy Grover {Opt_hw_block_size, "hw_block_size=%u"}, 16463abaa2bfSMike Christie {Opt_hw_max_sectors, "hw_max_sectors=%u"}, 1647b849b456SKenjiro Nakayama {Opt_nl_reply_supported, "nl_reply_supported=%d"}, 16487c9e7a6fSAndy Grover {Opt_err, NULL} 16497c9e7a6fSAndy Grover }; 16507c9e7a6fSAndy Grover 16513abaa2bfSMike Christie static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib) 16523abaa2bfSMike Christie { 16533abaa2bfSMike Christie unsigned long tmp_ul; 16543abaa2bfSMike Christie char *arg_p; 16553abaa2bfSMike Christie int ret; 16563abaa2bfSMike Christie 16573abaa2bfSMike Christie arg_p = match_strdup(arg); 16583abaa2bfSMike Christie if (!arg_p) 16593abaa2bfSMike Christie return -ENOMEM; 16603abaa2bfSMike Christie 16613abaa2bfSMike Christie ret = kstrtoul(arg_p, 0, &tmp_ul); 16623abaa2bfSMike Christie kfree(arg_p); 16633abaa2bfSMike Christie if (ret < 0) { 16643abaa2bfSMike Christie pr_err("kstrtoul() failed for dev attrib\n"); 16653abaa2bfSMike Christie return ret; 16663abaa2bfSMike Christie } 16673abaa2bfSMike Christie if (!tmp_ul) { 16683abaa2bfSMike Christie pr_err("dev attrib must be nonzero\n"); 16693abaa2bfSMike Christie return -EINVAL; 16703abaa2bfSMike Christie } 16713abaa2bfSMike Christie *dev_attrib = tmp_ul; 16723abaa2bfSMike Christie return 0; 16733abaa2bfSMike Christie } 16743abaa2bfSMike Christie 16757c9e7a6fSAndy Grover static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev, 16767c9e7a6fSAndy Grover const char *page, ssize_t count) 16777c9e7a6fSAndy Grover { 16787c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 16797c9e7a6fSAndy Grover char *orig, *ptr, *opts, *arg_p; 16807c9e7a6fSAndy Grover substring_t args[MAX_OPT_ARGS]; 16817c9e7a6fSAndy Grover int ret = 0, token; 16827c9e7a6fSAndy Grover 16837c9e7a6fSAndy Grover opts = kstrdup(page, GFP_KERNEL); 16847c9e7a6fSAndy Grover if (!opts) 16857c9e7a6fSAndy Grover return -ENOMEM; 16867c9e7a6fSAndy Grover 16877c9e7a6fSAndy Grover orig = opts; 16887c9e7a6fSAndy Grover 16897c9e7a6fSAndy Grover while ((ptr = strsep(&opts, ",\n")) != NULL) { 16907c9e7a6fSAndy Grover if (!*ptr) 16917c9e7a6fSAndy Grover continue; 16927c9e7a6fSAndy Grover 16937c9e7a6fSAndy Grover token = match_token(ptr, tokens, args); 16947c9e7a6fSAndy Grover switch (token) { 16957c9e7a6fSAndy Grover case Opt_dev_config: 16967c9e7a6fSAndy Grover if (match_strlcpy(udev->dev_config, &args[0], 16977c9e7a6fSAndy Grover TCMU_CONFIG_LEN) == 0) { 16987c9e7a6fSAndy Grover ret = -EINVAL; 16997c9e7a6fSAndy Grover break; 17007c9e7a6fSAndy Grover } 17017c9e7a6fSAndy Grover pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config); 17027c9e7a6fSAndy Grover break; 17037c9e7a6fSAndy Grover case Opt_dev_size: 17047c9e7a6fSAndy Grover arg_p = match_strdup(&args[0]); 17057c9e7a6fSAndy Grover if (!arg_p) { 17067c9e7a6fSAndy Grover ret = -ENOMEM; 17077c9e7a6fSAndy Grover break; 17087c9e7a6fSAndy Grover } 17097c9e7a6fSAndy Grover ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size); 17107c9e7a6fSAndy Grover kfree(arg_p); 17117c9e7a6fSAndy Grover if (ret < 0) 17127c9e7a6fSAndy Grover pr_err("kstrtoul() failed for dev_size=\n"); 17137c9e7a6fSAndy Grover break; 17149c1cd1b6SAndy Grover case Opt_hw_block_size: 17153abaa2bfSMike Christie ret = tcmu_set_dev_attrib(&args[0], 17163abaa2bfSMike Christie &(dev->dev_attrib.hw_block_size)); 17179c1cd1b6SAndy Grover break; 17183abaa2bfSMike Christie case Opt_hw_max_sectors: 17193abaa2bfSMike Christie ret = tcmu_set_dev_attrib(&args[0], 17203abaa2bfSMike Christie &(dev->dev_attrib.hw_max_sectors)); 17219c1cd1b6SAndy Grover break; 1722b849b456SKenjiro Nakayama case Opt_nl_reply_supported: 1723b849b456SKenjiro Nakayama arg_p = match_strdup(&args[0]); 1724b849b456SKenjiro Nakayama if (!arg_p) { 1725b849b456SKenjiro Nakayama ret = -ENOMEM; 1726b849b456SKenjiro Nakayama break; 1727b849b456SKenjiro Nakayama } 172816b93277SDan Carpenter ret = kstrtoint(arg_p, 0, &udev->nl_reply_supported); 1729b849b456SKenjiro Nakayama kfree(arg_p); 1730b849b456SKenjiro Nakayama if (ret < 0) 173116b93277SDan Carpenter pr_err("kstrtoint() failed for nl_reply_supported=\n"); 1732b849b456SKenjiro Nakayama break; 17337c9e7a6fSAndy Grover default: 17347c9e7a6fSAndy Grover break; 17357c9e7a6fSAndy Grover } 17362579325cSMike Christie 17372579325cSMike Christie if (ret) 17382579325cSMike Christie break; 17397c9e7a6fSAndy Grover } 17407c9e7a6fSAndy Grover 17417c9e7a6fSAndy Grover kfree(orig); 17427c9e7a6fSAndy Grover return (!ret) ? count : ret; 17437c9e7a6fSAndy Grover } 17447c9e7a6fSAndy Grover 17457c9e7a6fSAndy Grover static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b) 17467c9e7a6fSAndy Grover { 17477c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 17487c9e7a6fSAndy Grover ssize_t bl = 0; 17497c9e7a6fSAndy Grover 17507c9e7a6fSAndy Grover bl = sprintf(b + bl, "Config: %s ", 17517c9e7a6fSAndy Grover udev->dev_config[0] ? udev->dev_config : "NULL"); 17527d7a7435SNicholas Bellinger bl += sprintf(b + bl, "Size: %zu\n", udev->dev_size); 17537c9e7a6fSAndy Grover 17547c9e7a6fSAndy Grover return bl; 17557c9e7a6fSAndy Grover } 17567c9e7a6fSAndy Grover 17577c9e7a6fSAndy Grover static sector_t tcmu_get_blocks(struct se_device *dev) 17587c9e7a6fSAndy Grover { 17597c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 17607c9e7a6fSAndy Grover 17617c9e7a6fSAndy Grover return div_u64(udev->dev_size - dev->dev_attrib.block_size, 17627c9e7a6fSAndy Grover dev->dev_attrib.block_size); 17637c9e7a6fSAndy Grover } 17647c9e7a6fSAndy Grover 17657c9e7a6fSAndy Grover static sense_reason_t 17667c9e7a6fSAndy Grover tcmu_parse_cdb(struct se_cmd *cmd) 17677c9e7a6fSAndy Grover { 176802eb924fSAndy Grover return passthrough_parse_cdb(cmd, tcmu_queue_cmd); 17699c1cd1b6SAndy Grover } 17709c1cd1b6SAndy Grover 17717d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page) 17727d7a7435SNicholas Bellinger { 17737d7a7435SNicholas Bellinger struct se_dev_attrib *da = container_of(to_config_group(item), 17747d7a7435SNicholas Bellinger struct se_dev_attrib, da_group); 1775b5ab697cSKenjiro Nakayama struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 17767d7a7435SNicholas Bellinger 17777d7a7435SNicholas Bellinger return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC); 17787d7a7435SNicholas Bellinger } 17797d7a7435SNicholas Bellinger 17807d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page, 17817d7a7435SNicholas Bellinger size_t count) 17827d7a7435SNicholas Bellinger { 17837d7a7435SNicholas Bellinger struct se_dev_attrib *da = container_of(to_config_group(item), 17847d7a7435SNicholas Bellinger struct se_dev_attrib, da_group); 17857d7a7435SNicholas Bellinger struct tcmu_dev *udev = container_of(da->da_dev, 17867d7a7435SNicholas Bellinger struct tcmu_dev, se_dev); 17877d7a7435SNicholas Bellinger u32 val; 17887d7a7435SNicholas Bellinger int ret; 17897d7a7435SNicholas Bellinger 17907d7a7435SNicholas Bellinger if (da->da_dev->export_count) { 17917d7a7435SNicholas Bellinger pr_err("Unable to set tcmu cmd_time_out while exports exist\n"); 17927d7a7435SNicholas Bellinger return -EINVAL; 17937d7a7435SNicholas Bellinger } 17947d7a7435SNicholas Bellinger 17957d7a7435SNicholas Bellinger ret = kstrtou32(page, 0, &val); 17967d7a7435SNicholas Bellinger if (ret < 0) 17977d7a7435SNicholas Bellinger return ret; 17987d7a7435SNicholas Bellinger 17997d7a7435SNicholas Bellinger udev->cmd_time_out = val * MSEC_PER_SEC; 18007d7a7435SNicholas Bellinger return count; 18017d7a7435SNicholas Bellinger } 18027d7a7435SNicholas Bellinger CONFIGFS_ATTR(tcmu_, cmd_time_out); 18037d7a7435SNicholas Bellinger 18042d76443eSMike Christie static ssize_t tcmu_dev_config_show(struct config_item *item, char *page) 1805ee018252SBryant G. Ly { 1806ee018252SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 1807ee018252SBryant G. Ly struct se_dev_attrib, da_group); 1808ee018252SBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 1809ee018252SBryant G. Ly 1810ee018252SBryant G. Ly return snprintf(page, PAGE_SIZE, "%s\n", udev->dev_config); 1811ee018252SBryant G. Ly } 1812ee018252SBryant G. Ly 18132d76443eSMike Christie static ssize_t tcmu_dev_config_store(struct config_item *item, const char *page, 1814ee018252SBryant G. Ly size_t count) 1815ee018252SBryant G. Ly { 1816ee018252SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 1817ee018252SBryant G. Ly struct se_dev_attrib, da_group); 1818ee018252SBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 18192d76443eSMike Christie int ret, len; 1820ee018252SBryant G. Ly 18212d76443eSMike Christie len = strlen(page); 18222d76443eSMike Christie if (!len || len > TCMU_CONFIG_LEN - 1) 1823ee018252SBryant G. Ly return -EINVAL; 1824ee018252SBryant G. Ly 1825ee018252SBryant G. Ly /* Check if device has been configured before */ 1826ee018252SBryant G. Ly if (tcmu_dev_configured(udev)) { 1827b3af66e2SMike Christie ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE, 18282d76443eSMike Christie TCMU_ATTR_DEV_CFG, page); 1829ee018252SBryant G. Ly if (ret) { 1830ee018252SBryant G. Ly pr_err("Unable to reconfigure device\n"); 1831ee018252SBryant G. Ly return ret; 1832ee018252SBryant G. Ly } 1833de8c5221SBryant G. Ly strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN); 1834de8c5221SBryant G. Ly 1835de8c5221SBryant G. Ly ret = tcmu_update_uio_info(udev); 1836de8c5221SBryant G. Ly if (ret) 1837de8c5221SBryant G. Ly return ret; 1838de8c5221SBryant G. Ly return count; 1839ee018252SBryant G. Ly } 18402d76443eSMike Christie strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN); 1841ee018252SBryant G. Ly 1842ee018252SBryant G. Ly return count; 1843ee018252SBryant G. Ly } 18442d76443eSMike Christie CONFIGFS_ATTR(tcmu_, dev_config); 1845ee018252SBryant G. Ly 1846801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_show(struct config_item *item, char *page) 1847801fc54dSBryant G. Ly { 1848801fc54dSBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 1849801fc54dSBryant G. Ly struct se_dev_attrib, da_group); 1850801fc54dSBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 1851801fc54dSBryant G. Ly 1852801fc54dSBryant G. Ly return snprintf(page, PAGE_SIZE, "%zu\n", udev->dev_size); 1853801fc54dSBryant G. Ly } 1854801fc54dSBryant G. Ly 1855801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page, 1856801fc54dSBryant G. Ly size_t count) 1857801fc54dSBryant G. Ly { 1858801fc54dSBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 1859801fc54dSBryant G. Ly struct se_dev_attrib, da_group); 1860801fc54dSBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 18612d76443eSMike Christie u64 val; 1862801fc54dSBryant G. Ly int ret; 1863801fc54dSBryant G. Ly 18642d76443eSMike Christie ret = kstrtou64(page, 0, &val); 1865801fc54dSBryant G. Ly if (ret < 0) 1866801fc54dSBryant G. Ly return ret; 1867801fc54dSBryant G. Ly 1868801fc54dSBryant G. Ly /* Check if device has been configured before */ 1869801fc54dSBryant G. Ly if (tcmu_dev_configured(udev)) { 1870b3af66e2SMike Christie ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE, 18712d76443eSMike Christie TCMU_ATTR_DEV_SIZE, &val); 1872801fc54dSBryant G. Ly if (ret) { 1873801fc54dSBryant G. Ly pr_err("Unable to reconfigure device\n"); 1874801fc54dSBryant G. Ly return ret; 1875801fc54dSBryant G. Ly } 1876801fc54dSBryant G. Ly } 18772d76443eSMike Christie udev->dev_size = val; 1878801fc54dSBryant G. Ly return count; 1879801fc54dSBryant G. Ly } 1880801fc54dSBryant G. Ly CONFIGFS_ATTR(tcmu_, dev_size); 1881801fc54dSBryant G. Ly 1882b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_show(struct config_item *item, 1883b849b456SKenjiro Nakayama char *page) 1884b849b456SKenjiro Nakayama { 1885b849b456SKenjiro Nakayama struct se_dev_attrib *da = container_of(to_config_group(item), 1886b849b456SKenjiro Nakayama struct se_dev_attrib, da_group); 1887b849b456SKenjiro Nakayama struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 1888b849b456SKenjiro Nakayama 1889b849b456SKenjiro Nakayama return snprintf(page, PAGE_SIZE, "%d\n", udev->nl_reply_supported); 1890b849b456SKenjiro Nakayama } 1891b849b456SKenjiro Nakayama 1892b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_store(struct config_item *item, 1893b849b456SKenjiro Nakayama const char *page, size_t count) 1894b849b456SKenjiro Nakayama { 1895b849b456SKenjiro Nakayama struct se_dev_attrib *da = container_of(to_config_group(item), 1896b849b456SKenjiro Nakayama struct se_dev_attrib, da_group); 1897b849b456SKenjiro Nakayama struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 1898b849b456SKenjiro Nakayama s8 val; 1899b849b456SKenjiro Nakayama int ret; 1900b849b456SKenjiro Nakayama 1901b849b456SKenjiro Nakayama ret = kstrtos8(page, 0, &val); 1902b849b456SKenjiro Nakayama if (ret < 0) 1903b849b456SKenjiro Nakayama return ret; 1904b849b456SKenjiro Nakayama 1905b849b456SKenjiro Nakayama udev->nl_reply_supported = val; 1906b849b456SKenjiro Nakayama return count; 1907b849b456SKenjiro Nakayama } 1908b849b456SKenjiro Nakayama CONFIGFS_ATTR(tcmu_, nl_reply_supported); 1909b849b456SKenjiro Nakayama 19109a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_show(struct config_item *item, 19119a8bb606SBryant G. Ly char *page) 19129a8bb606SBryant G. Ly { 19139a8bb606SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 19149a8bb606SBryant G. Ly struct se_dev_attrib, da_group); 19159a8bb606SBryant G. Ly 19169a8bb606SBryant G. Ly return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache); 19179a8bb606SBryant G. Ly } 19189a8bb606SBryant G. Ly 19199a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_store(struct config_item *item, 19209a8bb606SBryant G. Ly const char *page, size_t count) 19219a8bb606SBryant G. Ly { 19229a8bb606SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 19239a8bb606SBryant G. Ly struct se_dev_attrib, da_group); 19241068be7bSBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 19252d76443eSMike Christie u8 val; 19269a8bb606SBryant G. Ly int ret; 19279a8bb606SBryant G. Ly 19282d76443eSMike Christie ret = kstrtou8(page, 0, &val); 19299a8bb606SBryant G. Ly if (ret < 0) 19309a8bb606SBryant G. Ly return ret; 19319a8bb606SBryant G. Ly 19321068be7bSBryant G. Ly /* Check if device has been configured before */ 19331068be7bSBryant G. Ly if (tcmu_dev_configured(udev)) { 1934b3af66e2SMike Christie ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE, 19352d76443eSMike Christie TCMU_ATTR_WRITECACHE, &val); 19361068be7bSBryant G. Ly if (ret) { 19371068be7bSBryant G. Ly pr_err("Unable to reconfigure device\n"); 19381068be7bSBryant G. Ly return ret; 19391068be7bSBryant G. Ly } 19401068be7bSBryant G. Ly } 19412d76443eSMike Christie 19422d76443eSMike Christie da->emulate_write_cache = val; 19439a8bb606SBryant G. Ly return count; 19449a8bb606SBryant G. Ly } 19459a8bb606SBryant G. Ly CONFIGFS_ATTR(tcmu_, emulate_write_cache); 19469a8bb606SBryant G. Ly 19475821783bSColin Ian King static struct configfs_attribute *tcmu_attrib_attrs[] = { 1948801fc54dSBryant G. Ly &tcmu_attr_cmd_time_out, 19492d76443eSMike Christie &tcmu_attr_dev_config, 1950801fc54dSBryant G. Ly &tcmu_attr_dev_size, 1951801fc54dSBryant G. Ly &tcmu_attr_emulate_write_cache, 1952b849b456SKenjiro Nakayama &tcmu_attr_nl_reply_supported, 1953801fc54dSBryant G. Ly NULL, 1954801fc54dSBryant G. Ly }; 1955801fc54dSBryant G. Ly 19567d7a7435SNicholas Bellinger static struct configfs_attribute **tcmu_attrs; 19577d7a7435SNicholas Bellinger 19587d7a7435SNicholas Bellinger static struct target_backend_ops tcmu_ops = { 19597c9e7a6fSAndy Grover .name = "user", 19607c9e7a6fSAndy Grover .owner = THIS_MODULE, 1961a3541703SAndy Grover .transport_flags = TRANSPORT_FLAG_PASSTHROUGH, 19627c9e7a6fSAndy Grover .attach_hba = tcmu_attach_hba, 19637c9e7a6fSAndy Grover .detach_hba = tcmu_detach_hba, 19647c9e7a6fSAndy Grover .alloc_device = tcmu_alloc_device, 19657c9e7a6fSAndy Grover .configure_device = tcmu_configure_device, 196692634706SMike Christie .destroy_device = tcmu_destroy_device, 19677c9e7a6fSAndy Grover .free_device = tcmu_free_device, 19687c9e7a6fSAndy Grover .parse_cdb = tcmu_parse_cdb, 19697c9e7a6fSAndy Grover .set_configfs_dev_params = tcmu_set_configfs_dev_params, 19707c9e7a6fSAndy Grover .show_configfs_dev_params = tcmu_show_configfs_dev_params, 19717c9e7a6fSAndy Grover .get_device_type = sbc_get_device_type, 19727c9e7a6fSAndy Grover .get_blocks = tcmu_get_blocks, 19737d7a7435SNicholas Bellinger .tb_dev_attrib_attrs = NULL, 19747c9e7a6fSAndy Grover }; 19757c9e7a6fSAndy Grover 1976b6df4b79SXiubo Li static int unmap_thread_fn(void *data) 1977b6df4b79SXiubo Li { 1978b6df4b79SXiubo Li struct tcmu_dev *udev; 1979b6df4b79SXiubo Li loff_t off; 1980b6df4b79SXiubo Li uint32_t start, end, block; 1981b6df4b79SXiubo Li 198207932a02SXiubo Li while (!kthread_should_stop()) { 1983b6df4b79SXiubo Li DEFINE_WAIT(__wait); 1984b6df4b79SXiubo Li 1985b6df4b79SXiubo Li prepare_to_wait(&unmap_wait, &__wait, TASK_INTERRUPTIBLE); 1986b6df4b79SXiubo Li schedule(); 1987b6df4b79SXiubo Li finish_wait(&unmap_wait, &__wait); 1988b6df4b79SXiubo Li 1989d906d8afSMike Christie if (kthread_should_stop()) 1990d906d8afSMike Christie break; 1991d906d8afSMike Christie 1992b6df4b79SXiubo Li mutex_lock(&root_udev_mutex); 1993b6df4b79SXiubo Li list_for_each_entry(udev, &root_udev, node) { 1994b6df4b79SXiubo Li mutex_lock(&udev->cmdr_lock); 1995b6df4b79SXiubo Li 1996b6df4b79SXiubo Li /* Try to complete the finished commands first */ 1997b6df4b79SXiubo Li tcmu_handle_completions(udev); 1998b6df4b79SXiubo Li 1999b6df4b79SXiubo Li /* Skip the udevs waiting the global pool or in idle */ 2000b6df4b79SXiubo Li if (udev->waiting_global || !udev->dbi_thresh) { 2001b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 2002b6df4b79SXiubo Li continue; 2003b6df4b79SXiubo Li } 2004b6df4b79SXiubo Li 2005b6df4b79SXiubo Li end = udev->dbi_max + 1; 2006b6df4b79SXiubo Li block = find_last_bit(udev->data_bitmap, end); 2007b6df4b79SXiubo Li if (block == udev->dbi_max) { 2008b6df4b79SXiubo Li /* 2009b6df4b79SXiubo Li * The last bit is dbi_max, so there is 2010b6df4b79SXiubo Li * no need to shrink any blocks. 2011b6df4b79SXiubo Li */ 2012b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 2013b6df4b79SXiubo Li continue; 2014b6df4b79SXiubo Li } else if (block == end) { 2015b6df4b79SXiubo Li /* The current udev will goto idle state */ 2016b6df4b79SXiubo Li udev->dbi_thresh = start = 0; 2017b6df4b79SXiubo Li udev->dbi_max = 0; 2018b6df4b79SXiubo Li } else { 2019b6df4b79SXiubo Li udev->dbi_thresh = start = block + 1; 2020b6df4b79SXiubo Li udev->dbi_max = block; 2021b6df4b79SXiubo Li } 2022b6df4b79SXiubo Li 2023b6df4b79SXiubo Li /* Here will truncate the data area from off */ 2024b6df4b79SXiubo Li off = udev->data_off + start * DATA_BLOCK_SIZE; 2025b6df4b79SXiubo Li unmap_mapping_range(udev->inode->i_mapping, off, 0, 1); 2026b6df4b79SXiubo Li 2027b6df4b79SXiubo Li /* Release the block pages */ 2028bf99ec13SMike Christie tcmu_blocks_release(&udev->data_blocks, start, end); 2029b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 2030b6df4b79SXiubo Li } 2031b6df4b79SXiubo Li 2032b6df4b79SXiubo Li /* 2033b6df4b79SXiubo Li * Try to wake up the udevs who are waiting 2034b6df4b79SXiubo Li * for the global data pool. 2035b6df4b79SXiubo Li */ 2036b6df4b79SXiubo Li list_for_each_entry(udev, &root_udev, node) { 2037b6df4b79SXiubo Li if (udev->waiting_global) 2038b6df4b79SXiubo Li wake_up(&udev->wait_cmdr); 2039b6df4b79SXiubo Li } 2040b6df4b79SXiubo Li mutex_unlock(&root_udev_mutex); 2041b6df4b79SXiubo Li } 2042b6df4b79SXiubo Li 2043b6df4b79SXiubo Li return 0; 2044b6df4b79SXiubo Li } 2045b6df4b79SXiubo Li 20467c9e7a6fSAndy Grover static int __init tcmu_module_init(void) 20477c9e7a6fSAndy Grover { 2048801fc54dSBryant G. Ly int ret, i, k, len = 0; 20497c9e7a6fSAndy Grover 20507c9e7a6fSAndy Grover BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0); 20517c9e7a6fSAndy Grover 20527c9e7a6fSAndy Grover tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache", 20537c9e7a6fSAndy Grover sizeof(struct tcmu_cmd), 20547c9e7a6fSAndy Grover __alignof__(struct tcmu_cmd), 20557c9e7a6fSAndy Grover 0, NULL); 20567c9e7a6fSAndy Grover if (!tcmu_cmd_cache) 20577c9e7a6fSAndy Grover return -ENOMEM; 20587c9e7a6fSAndy Grover 20597c9e7a6fSAndy Grover tcmu_root_device = root_device_register("tcm_user"); 20607c9e7a6fSAndy Grover if (IS_ERR(tcmu_root_device)) { 20617c9e7a6fSAndy Grover ret = PTR_ERR(tcmu_root_device); 20627c9e7a6fSAndy Grover goto out_free_cache; 20637c9e7a6fSAndy Grover } 20647c9e7a6fSAndy Grover 20657c9e7a6fSAndy Grover ret = genl_register_family(&tcmu_genl_family); 20667c9e7a6fSAndy Grover if (ret < 0) { 20677c9e7a6fSAndy Grover goto out_unreg_device; 20687c9e7a6fSAndy Grover } 20697c9e7a6fSAndy Grover 20707d7a7435SNicholas Bellinger for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) { 20717d7a7435SNicholas Bellinger len += sizeof(struct configfs_attribute *); 20727d7a7435SNicholas Bellinger } 2073801fc54dSBryant G. Ly for (i = 0; tcmu_attrib_attrs[i] != NULL; i++) { 2074801fc54dSBryant G. Ly len += sizeof(struct configfs_attribute *); 2075801fc54dSBryant G. Ly } 2076801fc54dSBryant G. Ly len += sizeof(struct configfs_attribute *); 20777d7a7435SNicholas Bellinger 20787d7a7435SNicholas Bellinger tcmu_attrs = kzalloc(len, GFP_KERNEL); 20797d7a7435SNicholas Bellinger if (!tcmu_attrs) { 20807d7a7435SNicholas Bellinger ret = -ENOMEM; 20817d7a7435SNicholas Bellinger goto out_unreg_genl; 20827d7a7435SNicholas Bellinger } 20837d7a7435SNicholas Bellinger 20847d7a7435SNicholas Bellinger for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) { 20857d7a7435SNicholas Bellinger tcmu_attrs[i] = passthrough_attrib_attrs[i]; 20867d7a7435SNicholas Bellinger } 2087801fc54dSBryant G. Ly for (k = 0; tcmu_attrib_attrs[k] != NULL; k++) { 2088801fc54dSBryant G. Ly tcmu_attrs[i] = tcmu_attrib_attrs[k]; 20899a8bb606SBryant G. Ly i++; 2090801fc54dSBryant G. Ly } 20917d7a7435SNicholas Bellinger tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs; 20927d7a7435SNicholas Bellinger 20930a06d430SChristoph Hellwig ret = transport_backend_register(&tcmu_ops); 20947c9e7a6fSAndy Grover if (ret) 20957d7a7435SNicholas Bellinger goto out_attrs; 20967c9e7a6fSAndy Grover 2097b6df4b79SXiubo Li init_waitqueue_head(&unmap_wait); 2098b6df4b79SXiubo Li unmap_thread = kthread_run(unmap_thread_fn, NULL, "tcmu_unmap"); 2099b6df4b79SXiubo Li if (IS_ERR(unmap_thread)) { 2100b6df4b79SXiubo Li ret = PTR_ERR(unmap_thread); 2101b6df4b79SXiubo Li goto out_unreg_transport; 2102b6df4b79SXiubo Li } 2103b6df4b79SXiubo Li 21047c9e7a6fSAndy Grover return 0; 21057c9e7a6fSAndy Grover 2106b6df4b79SXiubo Li out_unreg_transport: 2107b6df4b79SXiubo Li target_backend_unregister(&tcmu_ops); 21087d7a7435SNicholas Bellinger out_attrs: 21097d7a7435SNicholas Bellinger kfree(tcmu_attrs); 21107c9e7a6fSAndy Grover out_unreg_genl: 21117c9e7a6fSAndy Grover genl_unregister_family(&tcmu_genl_family); 21127c9e7a6fSAndy Grover out_unreg_device: 21137c9e7a6fSAndy Grover root_device_unregister(tcmu_root_device); 21147c9e7a6fSAndy Grover out_free_cache: 21157c9e7a6fSAndy Grover kmem_cache_destroy(tcmu_cmd_cache); 21167c9e7a6fSAndy Grover 21177c9e7a6fSAndy Grover return ret; 21187c9e7a6fSAndy Grover } 21197c9e7a6fSAndy Grover 21207c9e7a6fSAndy Grover static void __exit tcmu_module_exit(void) 21217c9e7a6fSAndy Grover { 2122b6df4b79SXiubo Li kthread_stop(unmap_thread); 21230a06d430SChristoph Hellwig target_backend_unregister(&tcmu_ops); 21247d7a7435SNicholas Bellinger kfree(tcmu_attrs); 21257c9e7a6fSAndy Grover genl_unregister_family(&tcmu_genl_family); 21267c9e7a6fSAndy Grover root_device_unregister(tcmu_root_device); 21277c9e7a6fSAndy Grover kmem_cache_destroy(tcmu_cmd_cache); 21287c9e7a6fSAndy Grover } 21297c9e7a6fSAndy Grover 21307c9e7a6fSAndy Grover MODULE_DESCRIPTION("TCM USER subsystem plugin"); 21317c9e7a6fSAndy Grover MODULE_AUTHOR("Shaohua Li <shli@kernel.org>"); 21327c9e7a6fSAndy Grover MODULE_AUTHOR("Andy Grover <agrover@redhat.com>"); 21337c9e7a6fSAndy Grover MODULE_LICENSE("GPL"); 21347c9e7a6fSAndy Grover 21357c9e7a6fSAndy Grover module_init(tcmu_module_init); 21367c9e7a6fSAndy Grover module_exit(tcmu_module_exit); 2137