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> 359972cebbSMike Christie #include <linux/workqueue.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 45572ccdabSRandy Dunlap /** 46572ccdabSRandy Dunlap * DOC: Userspace I/O 47572ccdabSRandy Dunlap * Userspace I/O 48572ccdabSRandy Dunlap * ------------- 49572ccdabSRandy Dunlap * 507c9e7a6fSAndy Grover * Define a shared-memory interface for LIO to pass SCSI commands and 517c9e7a6fSAndy Grover * data to userspace for processing. This is to allow backends that 527c9e7a6fSAndy Grover * are too complex for in-kernel support to be possible. 537c9e7a6fSAndy Grover * 547c9e7a6fSAndy Grover * It uses the UIO framework to do a lot of the device-creation and 557c9e7a6fSAndy Grover * introspection work for us. 567c9e7a6fSAndy Grover * 577c9e7a6fSAndy Grover * See the .h file for how the ring is laid out. Note that while the 587c9e7a6fSAndy Grover * command ring is defined, the particulars of the data area are 597c9e7a6fSAndy Grover * not. Offset values in the command entry point to other locations 60572ccdabSRandy Dunlap * internal to the mmap-ed area. There is separate space outside the 617c9e7a6fSAndy Grover * command ring for data buffers. This leaves maximum flexibility for 627c9e7a6fSAndy Grover * moving buffer allocations, or even page flipping or other 637c9e7a6fSAndy Grover * allocation techniques, without altering the command ring layout. 647c9e7a6fSAndy Grover * 657c9e7a6fSAndy Grover * SECURITY: 667c9e7a6fSAndy Grover * The user process must be assumed to be malicious. There's no way to 677c9e7a6fSAndy Grover * prevent it breaking the command ring protocol if it wants, but in 687c9e7a6fSAndy Grover * order to prevent other issues we must only ever read *data* from 697c9e7a6fSAndy Grover * the shared memory area, not offsets or sizes. This applies to 707c9e7a6fSAndy Grover * command ring entries as well as the mailbox. Extra code needed for 717c9e7a6fSAndy Grover * this may have a 'UAM' comment. 727c9e7a6fSAndy Grover */ 737c9e7a6fSAndy Grover 747c9e7a6fSAndy Grover #define TCMU_TIME_OUT (30 * MSEC_PER_SEC) 757c9e7a6fSAndy Grover 76b6df4b79SXiubo Li /* For cmd area, the size is fixed 8MB */ 77b6df4b79SXiubo Li #define CMDR_SIZE (8 * 1024 * 1024) 7826418649SSheng Yang 79b6df4b79SXiubo Li /* 80b6df4b79SXiubo Li * For data area, the block size is PAGE_SIZE and 81b6df4b79SXiubo Li * the total size is 256K * PAGE_SIZE. 82b6df4b79SXiubo Li */ 83b6df4b79SXiubo Li #define DATA_BLOCK_SIZE PAGE_SIZE 8480eb8761SMike Christie #define DATA_BLOCK_SHIFT PAGE_SHIFT 8580eb8761SMike Christie #define DATA_BLOCK_BITS_DEF (256 * 1024) 867c9e7a6fSAndy Grover 8780eb8761SMike Christie #define TCMU_MBS_TO_BLOCKS(_mbs) (_mbs << (20 - DATA_BLOCK_SHIFT)) 8880eb8761SMike Christie #define TCMU_BLOCKS_TO_MBS(_blocks) (_blocks >> (20 - DATA_BLOCK_SHIFT)) 8980eb8761SMike Christie 90af1dd7ffSMike Christie /* 91af1dd7ffSMike Christie * Default number of global data blocks(512K * PAGE_SIZE) 92af1dd7ffSMike Christie * when the unmap thread will be started. 93af1dd7ffSMike Christie */ 9480eb8761SMike Christie #define TCMU_GLOBAL_MAX_BLOCKS_DEF (512 * 1024) 95b6df4b79SXiubo Li 96b3af66e2SMike Christie static u8 tcmu_kern_cmd_reply_supported; 97bdaeedc1SMike Christie static u8 tcmu_netlink_blocked; 98b3af66e2SMike Christie 997c9e7a6fSAndy Grover static struct device *tcmu_root_device; 1007c9e7a6fSAndy Grover 1017c9e7a6fSAndy Grover struct tcmu_hba { 1027c9e7a6fSAndy Grover u32 host_id; 1037c9e7a6fSAndy Grover }; 1047c9e7a6fSAndy Grover 1057c9e7a6fSAndy Grover #define TCMU_CONFIG_LEN 256 1067c9e7a6fSAndy Grover 1073228691fSMike Christie static DEFINE_MUTEX(tcmu_nl_cmd_mutex); 1083228691fSMike Christie static LIST_HEAD(tcmu_nl_cmd_list); 1093228691fSMike Christie 1103228691fSMike Christie struct tcmu_dev; 1113228691fSMike Christie 112b3af66e2SMike Christie struct tcmu_nl_cmd { 113b3af66e2SMike Christie /* wake up thread waiting for reply */ 114b3af66e2SMike Christie struct completion complete; 1153228691fSMike Christie struct list_head nl_list; 1163228691fSMike Christie struct tcmu_dev *udev; 117b3af66e2SMike Christie int cmd; 118b3af66e2SMike Christie int status; 119b3af66e2SMike Christie }; 120b3af66e2SMike Christie 1217c9e7a6fSAndy Grover struct tcmu_dev { 122b6df4b79SXiubo Li struct list_head node; 123f3cdbe39SMike Christie struct kref kref; 124af1dd7ffSMike Christie 1257c9e7a6fSAndy Grover struct se_device se_dev; 1267c9e7a6fSAndy Grover 1277c9e7a6fSAndy Grover char *name; 1287c9e7a6fSAndy Grover struct se_hba *hba; 1297c9e7a6fSAndy Grover 1307c9e7a6fSAndy Grover #define TCMU_DEV_BIT_OPEN 0 1317c9e7a6fSAndy Grover #define TCMU_DEV_BIT_BROKEN 1 132892782caSMike Christie #define TCMU_DEV_BIT_BLOCKED 2 1337c9e7a6fSAndy Grover unsigned long flags; 1347c9e7a6fSAndy Grover 1357c9e7a6fSAndy Grover struct uio_info uio_info; 1367c9e7a6fSAndy Grover 137b6df4b79SXiubo Li struct inode *inode; 138b6df4b79SXiubo Li 1397c9e7a6fSAndy Grover struct tcmu_mailbox *mb_addr; 1407c9e7a6fSAndy Grover size_t dev_size; 1417c9e7a6fSAndy Grover u32 cmdr_size; 1427c9e7a6fSAndy Grover u32 cmdr_last_cleaned; 1433d9b9555SAndy Grover /* Offset of data area from start of mb */ 14426418649SSheng Yang /* Must add data_off and mb_addr to get the address */ 1457c9e7a6fSAndy Grover size_t data_off; 1467c9e7a6fSAndy Grover size_t data_size; 14780eb8761SMike Christie uint32_t max_blocks; 14880eb8761SMike Christie size_t ring_size; 14926418649SSheng Yang 150b6df4b79SXiubo Li struct mutex cmdr_lock; 151af1dd7ffSMike Christie struct list_head cmdr_queue; 1527c9e7a6fSAndy Grover 153141685a3SXiubo Li uint32_t dbi_max; 154b6df4b79SXiubo Li uint32_t dbi_thresh; 15580eb8761SMike Christie unsigned long *data_bitmap; 156141685a3SXiubo Li struct radix_tree_root data_blocks; 157141685a3SXiubo Li 1587c9e7a6fSAndy Grover struct idr commands; 1597c9e7a6fSAndy Grover 1609103575aSMike Christie struct timer_list cmd_timer; 161af980e46SMike Christie unsigned int cmd_time_out; 1629103575aSMike Christie 1639103575aSMike Christie struct timer_list qfull_timer; 1649103575aSMike Christie int qfull_time_out; 1659103575aSMike Christie 166488ebe4cSMike Christie struct list_head timedout_entry; 1677c9e7a6fSAndy Grover 168b3af66e2SMike Christie struct tcmu_nl_cmd curr_nl_cmd; 169b3af66e2SMike Christie 1707c9e7a6fSAndy Grover char dev_config[TCMU_CONFIG_LEN]; 171b849b456SKenjiro Nakayama 172b849b456SKenjiro Nakayama int nl_reply_supported; 1737c9e7a6fSAndy Grover }; 1747c9e7a6fSAndy Grover 1757c9e7a6fSAndy Grover #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev) 1767c9e7a6fSAndy Grover 1777c9e7a6fSAndy Grover #define CMDR_OFF sizeof(struct tcmu_mailbox) 1787c9e7a6fSAndy Grover 1797c9e7a6fSAndy Grover struct tcmu_cmd { 1807c9e7a6fSAndy Grover struct se_cmd *se_cmd; 1817c9e7a6fSAndy Grover struct tcmu_dev *tcmu_dev; 182af1dd7ffSMike Christie struct list_head cmdr_queue_entry; 1837c9e7a6fSAndy Grover 1847c9e7a6fSAndy Grover uint16_t cmd_id; 1857c9e7a6fSAndy Grover 18626418649SSheng Yang /* Can't use se_cmd when cleaning up expired cmds, because if 1877c9e7a6fSAndy Grover cmd has been completed then accessing se_cmd is off limits */ 188141685a3SXiubo Li uint32_t dbi_cnt; 189141685a3SXiubo Li uint32_t dbi_cur; 190141685a3SXiubo Li uint32_t *dbi; 1917c9e7a6fSAndy Grover 1927c9e7a6fSAndy Grover unsigned long deadline; 1937c9e7a6fSAndy Grover 1947c9e7a6fSAndy Grover #define TCMU_CMD_BIT_EXPIRED 0 1957c9e7a6fSAndy Grover unsigned long flags; 1967c9e7a6fSAndy Grover }; 197af1dd7ffSMike Christie /* 198af1dd7ffSMike Christie * To avoid dead lock the mutex lock order should always be: 199af1dd7ffSMike Christie * 200af1dd7ffSMike Christie * mutex_lock(&root_udev_mutex); 201af1dd7ffSMike Christie * ... 202af1dd7ffSMike Christie * mutex_lock(&tcmu_dev->cmdr_lock); 203af1dd7ffSMike Christie * mutex_unlock(&tcmu_dev->cmdr_lock); 204af1dd7ffSMike Christie * ... 205af1dd7ffSMike Christie * mutex_unlock(&root_udev_mutex); 206af1dd7ffSMike Christie */ 207b6df4b79SXiubo Li static DEFINE_MUTEX(root_udev_mutex); 208b6df4b79SXiubo Li static LIST_HEAD(root_udev); 209b6df4b79SXiubo Li 210488ebe4cSMike Christie static DEFINE_SPINLOCK(timed_out_udevs_lock); 211488ebe4cSMike Christie static LIST_HEAD(timed_out_udevs); 212488ebe4cSMike Christie 21380eb8761SMike Christie static struct kmem_cache *tcmu_cmd_cache; 21480eb8761SMike Christie 215b6df4b79SXiubo Li static atomic_t global_db_count = ATOMIC_INIT(0); 216af1dd7ffSMike Christie static struct delayed_work tcmu_unmap_work; 21780eb8761SMike Christie static int tcmu_global_max_blocks = TCMU_GLOBAL_MAX_BLOCKS_DEF; 218b6df4b79SXiubo Li 21980eb8761SMike Christie static int tcmu_set_global_max_data_area(const char *str, 22080eb8761SMike Christie const struct kernel_param *kp) 22180eb8761SMike Christie { 22280eb8761SMike Christie int ret, max_area_mb; 22380eb8761SMike Christie 22480eb8761SMike Christie ret = kstrtoint(str, 10, &max_area_mb); 22580eb8761SMike Christie if (ret) 22680eb8761SMike Christie return -EINVAL; 22780eb8761SMike Christie 22880eb8761SMike Christie if (max_area_mb <= 0) { 22980eb8761SMike Christie pr_err("global_max_data_area must be larger than 0.\n"); 23080eb8761SMike Christie return -EINVAL; 23180eb8761SMike Christie } 23280eb8761SMike Christie 23380eb8761SMike Christie tcmu_global_max_blocks = TCMU_MBS_TO_BLOCKS(max_area_mb); 23480eb8761SMike Christie if (atomic_read(&global_db_count) > tcmu_global_max_blocks) 23580eb8761SMike Christie schedule_delayed_work(&tcmu_unmap_work, 0); 23680eb8761SMike Christie else 23780eb8761SMike Christie cancel_delayed_work_sync(&tcmu_unmap_work); 23880eb8761SMike Christie 23980eb8761SMike Christie return 0; 24080eb8761SMike Christie } 24180eb8761SMike Christie 24280eb8761SMike Christie static int tcmu_get_global_max_data_area(char *buffer, 24380eb8761SMike Christie const struct kernel_param *kp) 24480eb8761SMike Christie { 24580eb8761SMike Christie return sprintf(buffer, "%d", TCMU_BLOCKS_TO_MBS(tcmu_global_max_blocks)); 24680eb8761SMike Christie } 24780eb8761SMike Christie 24880eb8761SMike Christie static const struct kernel_param_ops tcmu_global_max_data_area_op = { 24980eb8761SMike Christie .set = tcmu_set_global_max_data_area, 25080eb8761SMike Christie .get = tcmu_get_global_max_data_area, 25180eb8761SMike Christie }; 25280eb8761SMike Christie 25380eb8761SMike Christie module_param_cb(global_max_data_area_mb, &tcmu_global_max_data_area_op, NULL, 25480eb8761SMike Christie S_IWUSR | S_IRUGO); 25580eb8761SMike Christie MODULE_PARM_DESC(global_max_data_area_mb, 25680eb8761SMike Christie "Max MBs allowed to be allocated to all the tcmu device's " 25780eb8761SMike Christie "data areas."); 2587c9e7a6fSAndy Grover 259bdaeedc1SMike Christie static int tcmu_get_block_netlink(char *buffer, 260bdaeedc1SMike Christie const struct kernel_param *kp) 261bdaeedc1SMike Christie { 262bdaeedc1SMike Christie return sprintf(buffer, "%s\n", tcmu_netlink_blocked ? 263bdaeedc1SMike Christie "blocked" : "unblocked"); 264bdaeedc1SMike Christie } 265bdaeedc1SMike Christie 266bdaeedc1SMike Christie static int tcmu_set_block_netlink(const char *str, 267bdaeedc1SMike Christie const struct kernel_param *kp) 268bdaeedc1SMike Christie { 269bdaeedc1SMike Christie int ret; 270bdaeedc1SMike Christie u8 val; 271bdaeedc1SMike Christie 272bdaeedc1SMike Christie ret = kstrtou8(str, 0, &val); 273bdaeedc1SMike Christie if (ret < 0) 274bdaeedc1SMike Christie return ret; 275bdaeedc1SMike Christie 276bdaeedc1SMike Christie if (val > 1) { 277bdaeedc1SMike Christie pr_err("Invalid block netlink value %u\n", val); 278bdaeedc1SMike Christie return -EINVAL; 279bdaeedc1SMike Christie } 280bdaeedc1SMike Christie 281bdaeedc1SMike Christie tcmu_netlink_blocked = val; 282bdaeedc1SMike Christie return 0; 283bdaeedc1SMike Christie } 284bdaeedc1SMike Christie 285bdaeedc1SMike Christie static const struct kernel_param_ops tcmu_block_netlink_op = { 286bdaeedc1SMike Christie .set = tcmu_set_block_netlink, 287bdaeedc1SMike Christie .get = tcmu_get_block_netlink, 288bdaeedc1SMike Christie }; 289bdaeedc1SMike Christie 290bdaeedc1SMike Christie module_param_cb(block_netlink, &tcmu_block_netlink_op, NULL, S_IWUSR | S_IRUGO); 291bdaeedc1SMike Christie MODULE_PARM_DESC(block_netlink, "Block new netlink commands."); 292bdaeedc1SMike Christie 293bdaeedc1SMike Christie static int tcmu_fail_netlink_cmd(struct tcmu_nl_cmd *nl_cmd) 294bdaeedc1SMike Christie { 295bdaeedc1SMike Christie struct tcmu_dev *udev = nl_cmd->udev; 296bdaeedc1SMike Christie 297bdaeedc1SMike Christie if (!tcmu_netlink_blocked) { 298bdaeedc1SMike Christie pr_err("Could not reset device's netlink interface. Netlink is not blocked.\n"); 299bdaeedc1SMike Christie return -EBUSY; 300bdaeedc1SMike Christie } 301bdaeedc1SMike Christie 302bdaeedc1SMike Christie if (nl_cmd->cmd != TCMU_CMD_UNSPEC) { 303bdaeedc1SMike Christie pr_debug("Aborting nl cmd %d on %s\n", nl_cmd->cmd, udev->name); 304bdaeedc1SMike Christie nl_cmd->status = -EINTR; 305bdaeedc1SMike Christie list_del(&nl_cmd->nl_list); 306bdaeedc1SMike Christie complete(&nl_cmd->complete); 307bdaeedc1SMike Christie } 308bdaeedc1SMike Christie return 0; 309bdaeedc1SMike Christie } 310bdaeedc1SMike Christie 311bdaeedc1SMike Christie static int tcmu_set_reset_netlink(const char *str, 312bdaeedc1SMike Christie const struct kernel_param *kp) 313bdaeedc1SMike Christie { 314bdaeedc1SMike Christie struct tcmu_nl_cmd *nl_cmd, *tmp_cmd; 315bdaeedc1SMike Christie int ret; 316bdaeedc1SMike Christie u8 val; 317bdaeedc1SMike Christie 318bdaeedc1SMike Christie ret = kstrtou8(str, 0, &val); 319bdaeedc1SMike Christie if (ret < 0) 320bdaeedc1SMike Christie return ret; 321bdaeedc1SMike Christie 322bdaeedc1SMike Christie if (val != 1) { 323bdaeedc1SMike Christie pr_err("Invalid reset netlink value %u\n", val); 324bdaeedc1SMike Christie return -EINVAL; 325bdaeedc1SMike Christie } 326bdaeedc1SMike Christie 327bdaeedc1SMike Christie mutex_lock(&tcmu_nl_cmd_mutex); 328bdaeedc1SMike Christie list_for_each_entry_safe(nl_cmd, tmp_cmd, &tcmu_nl_cmd_list, nl_list) { 329bdaeedc1SMike Christie ret = tcmu_fail_netlink_cmd(nl_cmd); 330bdaeedc1SMike Christie if (ret) 331bdaeedc1SMike Christie break; 332bdaeedc1SMike Christie } 333bdaeedc1SMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 334bdaeedc1SMike Christie 335bdaeedc1SMike Christie return ret; 336bdaeedc1SMike Christie } 337bdaeedc1SMike Christie 338bdaeedc1SMike Christie static const struct kernel_param_ops tcmu_reset_netlink_op = { 339bdaeedc1SMike Christie .set = tcmu_set_reset_netlink, 340bdaeedc1SMike Christie }; 341bdaeedc1SMike Christie 342bdaeedc1SMike Christie module_param_cb(reset_netlink, &tcmu_reset_netlink_op, NULL, S_IWUSR); 343bdaeedc1SMike Christie MODULE_PARM_DESC(reset_netlink, "Reset netlink commands."); 344bdaeedc1SMike Christie 3457c9e7a6fSAndy Grover /* multicast group */ 3467c9e7a6fSAndy Grover enum tcmu_multicast_groups { 3477c9e7a6fSAndy Grover TCMU_MCGRP_CONFIG, 3487c9e7a6fSAndy Grover }; 3497c9e7a6fSAndy Grover 3507c9e7a6fSAndy Grover static const struct genl_multicast_group tcmu_mcgrps[] = { 3517c9e7a6fSAndy Grover [TCMU_MCGRP_CONFIG] = { .name = "config", }, 3527c9e7a6fSAndy Grover }; 3537c9e7a6fSAndy Grover 354b3af66e2SMike Christie static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = { 355b3af66e2SMike Christie [TCMU_ATTR_DEVICE] = { .type = NLA_STRING }, 356b3af66e2SMike Christie [TCMU_ATTR_MINOR] = { .type = NLA_U32 }, 357b3af66e2SMike Christie [TCMU_ATTR_CMD_STATUS] = { .type = NLA_S32 }, 358b3af66e2SMike Christie [TCMU_ATTR_DEVICE_ID] = { .type = NLA_U32 }, 359b3af66e2SMike Christie [TCMU_ATTR_SUPP_KERN_CMD_REPLY] = { .type = NLA_U8 }, 360b3af66e2SMike Christie }; 361b3af66e2SMike Christie 362b3af66e2SMike Christie static int tcmu_genl_cmd_done(struct genl_info *info, int completed_cmd) 363b3af66e2SMike Christie { 3643228691fSMike Christie struct tcmu_dev *udev = NULL; 365b3af66e2SMike Christie struct tcmu_nl_cmd *nl_cmd; 366b3af66e2SMike Christie int dev_id, rc, ret = 0; 367b3af66e2SMike Christie 368b3af66e2SMike Christie if (!info->attrs[TCMU_ATTR_CMD_STATUS] || 369b3af66e2SMike Christie !info->attrs[TCMU_ATTR_DEVICE_ID]) { 370b3af66e2SMike Christie printk(KERN_ERR "TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n"); 371b3af66e2SMike Christie return -EINVAL; 372b3af66e2SMike Christie } 373b3af66e2SMike Christie 374b3af66e2SMike Christie dev_id = nla_get_u32(info->attrs[TCMU_ATTR_DEVICE_ID]); 375b3af66e2SMike Christie rc = nla_get_s32(info->attrs[TCMU_ATTR_CMD_STATUS]); 376b3af66e2SMike Christie 3773228691fSMike Christie mutex_lock(&tcmu_nl_cmd_mutex); 3783228691fSMike Christie list_for_each_entry(nl_cmd, &tcmu_nl_cmd_list, nl_list) { 3793228691fSMike Christie if (nl_cmd->udev->se_dev.dev_index == dev_id) { 3803228691fSMike Christie udev = nl_cmd->udev; 3813228691fSMike Christie break; 382b3af66e2SMike Christie } 3833228691fSMike Christie } 384b3af66e2SMike Christie 3853228691fSMike Christie if (!udev) { 3860c218e16SMike Christie pr_err("tcmu nl cmd %u/%d completion could not find device with dev id %u.\n", 3873228691fSMike Christie completed_cmd, rc, dev_id); 3883228691fSMike Christie ret = -ENODEV; 3893228691fSMike Christie goto unlock; 3903228691fSMike Christie } 3913228691fSMike Christie list_del(&nl_cmd->nl_list); 392b3af66e2SMike Christie 393bdaeedc1SMike Christie pr_debug("%s genl cmd done got id %d curr %d done %d rc %d stat %d\n", 394bdaeedc1SMike Christie udev->name, dev_id, nl_cmd->cmd, completed_cmd, rc, 395bdaeedc1SMike Christie nl_cmd->status); 396b3af66e2SMike Christie 397b3af66e2SMike Christie if (nl_cmd->cmd != completed_cmd) { 3983228691fSMike Christie pr_err("Mismatched commands on %s (Expecting reply for %d. Current %d).\n", 3993228691fSMike Christie udev->name, completed_cmd, nl_cmd->cmd); 400b3af66e2SMike Christie ret = -EINVAL; 4013228691fSMike Christie goto unlock; 402b3af66e2SMike Christie } 403b3af66e2SMike Christie 4043228691fSMike Christie nl_cmd->status = rc; 405b3af66e2SMike Christie complete(&nl_cmd->complete); 4063228691fSMike Christie unlock: 4073228691fSMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 408b3af66e2SMike Christie return ret; 409b3af66e2SMike Christie } 410b3af66e2SMike Christie 411b3af66e2SMike Christie static int tcmu_genl_rm_dev_done(struct sk_buff *skb, struct genl_info *info) 412b3af66e2SMike Christie { 413b3af66e2SMike Christie return tcmu_genl_cmd_done(info, TCMU_CMD_REMOVED_DEVICE); 414b3af66e2SMike Christie } 415b3af66e2SMike Christie 416b3af66e2SMike Christie static int tcmu_genl_add_dev_done(struct sk_buff *skb, struct genl_info *info) 417b3af66e2SMike Christie { 418b3af66e2SMike Christie return tcmu_genl_cmd_done(info, TCMU_CMD_ADDED_DEVICE); 419b3af66e2SMike Christie } 420b3af66e2SMike Christie 421b3af66e2SMike Christie static int tcmu_genl_reconfig_dev_done(struct sk_buff *skb, 422b3af66e2SMike Christie struct genl_info *info) 423b3af66e2SMike Christie { 424b3af66e2SMike Christie return tcmu_genl_cmd_done(info, TCMU_CMD_RECONFIG_DEVICE); 425b3af66e2SMike Christie } 426b3af66e2SMike Christie 427b3af66e2SMike Christie static int tcmu_genl_set_features(struct sk_buff *skb, struct genl_info *info) 428b3af66e2SMike Christie { 429b3af66e2SMike Christie if (info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]) { 430b3af66e2SMike Christie tcmu_kern_cmd_reply_supported = 431b3af66e2SMike Christie nla_get_u8(info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]); 432b3af66e2SMike Christie printk(KERN_INFO "tcmu daemon: command reply support %u.\n", 433b3af66e2SMike Christie tcmu_kern_cmd_reply_supported); 434b3af66e2SMike Christie } 435b3af66e2SMike Christie 436b3af66e2SMike Christie return 0; 437b3af66e2SMike Christie } 438b3af66e2SMike Christie 439b3af66e2SMike Christie static const struct genl_ops tcmu_genl_ops[] = { 440b3af66e2SMike Christie { 441b3af66e2SMike Christie .cmd = TCMU_CMD_SET_FEATURES, 442b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 443b3af66e2SMike Christie .policy = tcmu_attr_policy, 444b3af66e2SMike Christie .doit = tcmu_genl_set_features, 445b3af66e2SMike Christie }, 446b3af66e2SMike Christie { 447b3af66e2SMike Christie .cmd = TCMU_CMD_ADDED_DEVICE_DONE, 448b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 449b3af66e2SMike Christie .policy = tcmu_attr_policy, 450b3af66e2SMike Christie .doit = tcmu_genl_add_dev_done, 451b3af66e2SMike Christie }, 452b3af66e2SMike Christie { 453b3af66e2SMike Christie .cmd = TCMU_CMD_REMOVED_DEVICE_DONE, 454b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 455b3af66e2SMike Christie .policy = tcmu_attr_policy, 456b3af66e2SMike Christie .doit = tcmu_genl_rm_dev_done, 457b3af66e2SMike Christie }, 458b3af66e2SMike Christie { 459b3af66e2SMike Christie .cmd = TCMU_CMD_RECONFIG_DEVICE_DONE, 460b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 461b3af66e2SMike Christie .policy = tcmu_attr_policy, 462b3af66e2SMike Christie .doit = tcmu_genl_reconfig_dev_done, 463b3af66e2SMike Christie }, 464b3af66e2SMike Christie }; 465b3af66e2SMike Christie 4667c9e7a6fSAndy Grover /* Our generic netlink family */ 46756989f6dSJohannes Berg static struct genl_family tcmu_genl_family __ro_after_init = { 468489111e5SJohannes Berg .module = THIS_MODULE, 4697c9e7a6fSAndy Grover .hdrsize = 0, 4707c9e7a6fSAndy Grover .name = "TCM-USER", 471b3af66e2SMike Christie .version = 2, 4727c9e7a6fSAndy Grover .maxattr = TCMU_ATTR_MAX, 4737c9e7a6fSAndy Grover .mcgrps = tcmu_mcgrps, 4747c9e7a6fSAndy Grover .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps), 47520c08b36SSheng Yang .netnsok = true, 476b3af66e2SMike Christie .ops = tcmu_genl_ops, 477b3af66e2SMike Christie .n_ops = ARRAY_SIZE(tcmu_genl_ops), 4787c9e7a6fSAndy Grover }; 4797c9e7a6fSAndy Grover 480141685a3SXiubo Li #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index)) 481141685a3SXiubo Li #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0) 482141685a3SXiubo Li #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index)) 483141685a3SXiubo Li #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++]) 484141685a3SXiubo Li 485b6df4b79SXiubo Li static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len) 486141685a3SXiubo Li { 487141685a3SXiubo Li struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 488141685a3SXiubo Li uint32_t i; 489141685a3SXiubo Li 490b6df4b79SXiubo Li for (i = 0; i < len; i++) 491141685a3SXiubo Li clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap); 492141685a3SXiubo Li } 493141685a3SXiubo Li 494b6df4b79SXiubo Li static inline bool tcmu_get_empty_block(struct tcmu_dev *udev, 495b6df4b79SXiubo Li struct tcmu_cmd *tcmu_cmd) 496141685a3SXiubo Li { 497b6df4b79SXiubo Li struct page *page; 498b6df4b79SXiubo Li int ret, dbi; 499141685a3SXiubo Li 500b6df4b79SXiubo Li dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh); 501b6df4b79SXiubo Li if (dbi == udev->dbi_thresh) 502b6df4b79SXiubo Li return false; 503b6df4b79SXiubo Li 504b6df4b79SXiubo Li page = radix_tree_lookup(&udev->data_blocks, dbi); 505b6df4b79SXiubo Li if (!page) { 506b6df4b79SXiubo Li if (atomic_add_return(1, &global_db_count) > 50780eb8761SMike Christie tcmu_global_max_blocks) 508af1dd7ffSMike Christie schedule_delayed_work(&tcmu_unmap_work, 0); 509b6df4b79SXiubo Li 510b6df4b79SXiubo Li /* try to get new page from the mm */ 511b6df4b79SXiubo Li page = alloc_page(GFP_KERNEL); 512b6df4b79SXiubo Li if (!page) 513daf78c30SXiubo Li goto err_alloc; 514b6df4b79SXiubo Li 515b6df4b79SXiubo Li ret = radix_tree_insert(&udev->data_blocks, dbi, page); 516daf78c30SXiubo Li if (ret) 517daf78c30SXiubo Li goto err_insert; 518b6df4b79SXiubo Li } 519b6df4b79SXiubo Li 520141685a3SXiubo Li if (dbi > udev->dbi_max) 521141685a3SXiubo Li udev->dbi_max = dbi; 522141685a3SXiubo Li 523141685a3SXiubo Li set_bit(dbi, udev->data_bitmap); 524b6df4b79SXiubo Li tcmu_cmd_set_dbi(tcmu_cmd, dbi); 525141685a3SXiubo Li 526b6df4b79SXiubo Li return true; 527daf78c30SXiubo Li err_insert: 528daf78c30SXiubo Li __free_page(page); 529daf78c30SXiubo Li err_alloc: 530daf78c30SXiubo Li atomic_dec(&global_db_count); 531daf78c30SXiubo Li return false; 532141685a3SXiubo Li } 533141685a3SXiubo Li 534b6df4b79SXiubo Li static bool tcmu_get_empty_blocks(struct tcmu_dev *udev, 535b6df4b79SXiubo Li struct tcmu_cmd *tcmu_cmd) 536b6df4b79SXiubo Li { 537b6df4b79SXiubo Li int i; 538b6df4b79SXiubo Li 539b6df4b79SXiubo Li for (i = tcmu_cmd->dbi_cur; i < tcmu_cmd->dbi_cnt; i++) { 540b6df4b79SXiubo Li if (!tcmu_get_empty_block(udev, tcmu_cmd)) 541af1dd7ffSMike Christie return false; 542141685a3SXiubo Li } 543b6df4b79SXiubo Li return true; 544141685a3SXiubo Li } 545141685a3SXiubo Li 546b6df4b79SXiubo Li static inline struct page * 547b6df4b79SXiubo Li tcmu_get_block_page(struct tcmu_dev *udev, uint32_t dbi) 548141685a3SXiubo Li { 549141685a3SXiubo Li return radix_tree_lookup(&udev->data_blocks, dbi); 550141685a3SXiubo Li } 551141685a3SXiubo Li 552141685a3SXiubo Li static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd) 553141685a3SXiubo Li { 554141685a3SXiubo Li kfree(tcmu_cmd->dbi); 555141685a3SXiubo Li kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); 556141685a3SXiubo Li } 557141685a3SXiubo Li 558141685a3SXiubo Li static inline size_t tcmu_cmd_get_data_length(struct tcmu_cmd *tcmu_cmd) 559141685a3SXiubo Li { 560141685a3SXiubo Li struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 561141685a3SXiubo Li size_t data_length = round_up(se_cmd->data_length, DATA_BLOCK_SIZE); 562141685a3SXiubo Li 563141685a3SXiubo Li if (se_cmd->se_cmd_flags & SCF_BIDI) { 564141685a3SXiubo Li BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents)); 565141685a3SXiubo Li data_length += round_up(se_cmd->t_bidi_data_sg->length, 566141685a3SXiubo Li DATA_BLOCK_SIZE); 567141685a3SXiubo Li } 568141685a3SXiubo Li 569141685a3SXiubo Li return data_length; 570141685a3SXiubo Li } 571141685a3SXiubo Li 572141685a3SXiubo Li static inline uint32_t tcmu_cmd_get_block_cnt(struct tcmu_cmd *tcmu_cmd) 573141685a3SXiubo Li { 574141685a3SXiubo Li size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd); 575141685a3SXiubo Li 576141685a3SXiubo Li return data_length / DATA_BLOCK_SIZE; 577141685a3SXiubo Li } 578141685a3SXiubo Li 5797c9e7a6fSAndy Grover static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd) 5807c9e7a6fSAndy Grover { 5817c9e7a6fSAndy Grover struct se_device *se_dev = se_cmd->se_dev; 5827c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(se_dev); 5837c9e7a6fSAndy Grover struct tcmu_cmd *tcmu_cmd; 5847c9e7a6fSAndy Grover 5857c9e7a6fSAndy Grover tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_KERNEL); 5867c9e7a6fSAndy Grover if (!tcmu_cmd) 5877c9e7a6fSAndy Grover return NULL; 5887c9e7a6fSAndy Grover 589af1dd7ffSMike Christie INIT_LIST_HEAD(&tcmu_cmd->cmdr_queue_entry); 5907c9e7a6fSAndy Grover tcmu_cmd->se_cmd = se_cmd; 5917c9e7a6fSAndy Grover tcmu_cmd->tcmu_dev = udev; 5927c9e7a6fSAndy Grover 593141685a3SXiubo Li tcmu_cmd_reset_dbi_cur(tcmu_cmd); 594141685a3SXiubo Li tcmu_cmd->dbi_cnt = tcmu_cmd_get_block_cnt(tcmu_cmd); 595141685a3SXiubo Li tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t), 596141685a3SXiubo Li GFP_KERNEL); 597141685a3SXiubo Li if (!tcmu_cmd->dbi) { 598141685a3SXiubo Li kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); 599141685a3SXiubo Li return NULL; 600141685a3SXiubo Li } 601141685a3SXiubo Li 6027c9e7a6fSAndy Grover return tcmu_cmd; 6037c9e7a6fSAndy Grover } 6047c9e7a6fSAndy Grover 6057c9e7a6fSAndy Grover static inline void tcmu_flush_dcache_range(void *vaddr, size_t size) 6067c9e7a6fSAndy Grover { 607b75d8063SGeliang Tang unsigned long offset = offset_in_page(vaddr); 60826d2b310Stangwenji void *start = vaddr - offset; 6097c9e7a6fSAndy Grover 6107c9e7a6fSAndy Grover size = round_up(size+offset, PAGE_SIZE); 6117c9e7a6fSAndy Grover 6127c9e7a6fSAndy Grover while (size) { 61326d2b310Stangwenji flush_dcache_page(virt_to_page(start)); 61426d2b310Stangwenji start += PAGE_SIZE; 6157c9e7a6fSAndy Grover size -= PAGE_SIZE; 6167c9e7a6fSAndy Grover } 6177c9e7a6fSAndy Grover } 6187c9e7a6fSAndy Grover 6197c9e7a6fSAndy Grover /* 6207c9e7a6fSAndy Grover * Some ring helper functions. We don't assume size is a power of 2 so 6217c9e7a6fSAndy Grover * we can't use circ_buf.h. 6227c9e7a6fSAndy Grover */ 6237c9e7a6fSAndy Grover static inline size_t spc_used(size_t head, size_t tail, size_t size) 6247c9e7a6fSAndy Grover { 6257c9e7a6fSAndy Grover int diff = head - tail; 6267c9e7a6fSAndy Grover 6277c9e7a6fSAndy Grover if (diff >= 0) 6287c9e7a6fSAndy Grover return diff; 6297c9e7a6fSAndy Grover else 6307c9e7a6fSAndy Grover return size + diff; 6317c9e7a6fSAndy Grover } 6327c9e7a6fSAndy Grover 6337c9e7a6fSAndy Grover static inline size_t spc_free(size_t head, size_t tail, size_t size) 6347c9e7a6fSAndy Grover { 6357c9e7a6fSAndy Grover /* Keep 1 byte unused or we can't tell full from empty */ 6367c9e7a6fSAndy Grover return (size - spc_used(head, tail, size) - 1); 6377c9e7a6fSAndy Grover } 6387c9e7a6fSAndy Grover 6397c9e7a6fSAndy Grover static inline size_t head_to_end(size_t head, size_t size) 6407c9e7a6fSAndy Grover { 6417c9e7a6fSAndy Grover return size - head; 6427c9e7a6fSAndy Grover } 6437c9e7a6fSAndy Grover 6443e609135SXiubo Li static inline void new_iov(struct iovec **iov, int *iov_cnt) 645f1dbd087SSheng Yang { 646f1dbd087SSheng Yang struct iovec *iovec; 647f1dbd087SSheng Yang 648f1dbd087SSheng Yang if (*iov_cnt != 0) 649f1dbd087SSheng Yang (*iov)++; 650f1dbd087SSheng Yang (*iov_cnt)++; 651f1dbd087SSheng Yang 652f1dbd087SSheng Yang iovec = *iov; 653f1dbd087SSheng Yang memset(iovec, 0, sizeof(struct iovec)); 654f1dbd087SSheng Yang } 655f1dbd087SSheng Yang 6567c9e7a6fSAndy Grover #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size) 6577c9e7a6fSAndy Grover 65826418649SSheng Yang /* offset is relative to mb_addr */ 659141685a3SXiubo Li static inline size_t get_block_offset_user(struct tcmu_dev *dev, 660141685a3SXiubo Li int dbi, int remaining) 66126418649SSheng Yang { 662141685a3SXiubo Li return dev->data_off + dbi * DATA_BLOCK_SIZE + 66326418649SSheng Yang DATA_BLOCK_SIZE - remaining; 66426418649SSheng Yang } 66526418649SSheng Yang 666daf78c30SXiubo Li static inline size_t iov_tail(struct iovec *iov) 66726418649SSheng Yang { 66826418649SSheng Yang return (size_t)iov->iov_base + iov->iov_len; 66926418649SSheng Yang } 67026418649SSheng Yang 6711a1fc0b8SMike Christie static void scatter_data_area(struct tcmu_dev *udev, 672141685a3SXiubo Li struct tcmu_cmd *tcmu_cmd, struct scatterlist *data_sg, 673141685a3SXiubo Li unsigned int data_nents, struct iovec **iov, 674141685a3SXiubo Li int *iov_cnt, bool copy_data) 675f97ec7dbSIlias Tsitsimpis { 676141685a3SXiubo Li int i, dbi; 67726418649SSheng Yang int block_remaining = 0; 678141685a3SXiubo Li void *from, *to = NULL; 679141685a3SXiubo Li size_t copy_bytes, to_offset, offset; 680f97ec7dbSIlias Tsitsimpis struct scatterlist *sg; 681b6df4b79SXiubo Li struct page *page; 682f97ec7dbSIlias Tsitsimpis 683f97ec7dbSIlias Tsitsimpis for_each_sg(data_sg, sg, data_nents, i) { 68426418649SSheng Yang int sg_remaining = sg->length; 685f97ec7dbSIlias Tsitsimpis from = kmap_atomic(sg_page(sg)) + sg->offset; 68626418649SSheng Yang while (sg_remaining > 0) { 68726418649SSheng Yang if (block_remaining == 0) { 688b6df4b79SXiubo Li if (to) 689b6df4b79SXiubo Li kunmap_atomic(to); 690b6df4b79SXiubo Li 69126418649SSheng Yang block_remaining = DATA_BLOCK_SIZE; 692b6df4b79SXiubo Li dbi = tcmu_cmd_get_dbi(tcmu_cmd); 693b6df4b79SXiubo Li page = tcmu_get_block_page(udev, dbi); 694b6df4b79SXiubo Li to = kmap_atomic(page); 695141685a3SXiubo Li } 696141685a3SXiubo Li 6973e609135SXiubo Li /* 6983e609135SXiubo Li * Covert to virtual offset of the ring data area. 6993e609135SXiubo Li */ 700141685a3SXiubo Li to_offset = get_block_offset_user(udev, dbi, 70126418649SSheng Yang block_remaining); 702141685a3SXiubo Li 7033e609135SXiubo Li /* 7043e609135SXiubo Li * The following code will gather and map the blocks 7053e609135SXiubo Li * to the same iovec when the blocks are all next to 7063e609135SXiubo Li * each other. 7073e609135SXiubo Li */ 7083e609135SXiubo Li copy_bytes = min_t(size_t, sg_remaining, 7093e609135SXiubo Li block_remaining); 71026418649SSheng Yang if (*iov_cnt != 0 && 711daf78c30SXiubo Li to_offset == iov_tail(*iov)) { 7123e609135SXiubo Li /* 7133e609135SXiubo Li * Will append to the current iovec, because 7143e609135SXiubo Li * the current block page is next to the 7153e609135SXiubo Li * previous one. 7163e609135SXiubo Li */ 717f1dbd087SSheng Yang (*iov)->iov_len += copy_bytes; 71826418649SSheng Yang } else { 7193e609135SXiubo Li /* 7203e609135SXiubo Li * Will allocate a new iovec because we are 7213e609135SXiubo Li * first time here or the current block page 7223e609135SXiubo Li * is not next to the previous one. 7233e609135SXiubo Li */ 7243e609135SXiubo Li new_iov(iov, iov_cnt); 72526418649SSheng Yang (*iov)->iov_base = (void __user *)to_offset; 726f97ec7dbSIlias Tsitsimpis (*iov)->iov_len = copy_bytes; 72726418649SSheng Yang } 7283e609135SXiubo Li 729f97ec7dbSIlias Tsitsimpis if (copy_data) { 730c542942cSXiubo Li offset = DATA_BLOCK_SIZE - block_remaining; 731c542942cSXiubo Li memcpy(to + offset, 732c542942cSXiubo Li from + sg->length - sg_remaining, 73326418649SSheng Yang copy_bytes); 734f97ec7dbSIlias Tsitsimpis tcmu_flush_dcache_range(to, copy_bytes); 735f97ec7dbSIlias Tsitsimpis } 7363e609135SXiubo Li 73726418649SSheng Yang sg_remaining -= copy_bytes; 73826418649SSheng Yang block_remaining -= copy_bytes; 739f97ec7dbSIlias Tsitsimpis } 740e2e21bd8SSagi Grimberg kunmap_atomic(from - sg->offset); 741f97ec7dbSIlias Tsitsimpis } 7423e609135SXiubo Li 743b6df4b79SXiubo Li if (to) 744b6df4b79SXiubo Li kunmap_atomic(to); 7450c28481fSSheng Yang } 7460c28481fSSheng Yang 747a5d68ba8SXiubo Li static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 748a5d68ba8SXiubo Li bool bidi) 749f97ec7dbSIlias Tsitsimpis { 750a5d68ba8SXiubo Li struct se_cmd *se_cmd = cmd->se_cmd; 751141685a3SXiubo Li int i, dbi; 75226418649SSheng Yang int block_remaining = 0; 753b6df4b79SXiubo Li void *from = NULL, *to; 754141685a3SXiubo Li size_t copy_bytes, offset; 755a5d68ba8SXiubo Li struct scatterlist *sg, *data_sg; 756b6df4b79SXiubo Li struct page *page; 757a5d68ba8SXiubo Li unsigned int data_nents; 758141685a3SXiubo Li uint32_t count = 0; 759a5d68ba8SXiubo Li 760a5d68ba8SXiubo Li if (!bidi) { 761a5d68ba8SXiubo Li data_sg = se_cmd->t_data_sg; 762a5d68ba8SXiubo Li data_nents = se_cmd->t_data_nents; 763a5d68ba8SXiubo Li } else { 764a5d68ba8SXiubo Li 765a5d68ba8SXiubo Li /* 766a5d68ba8SXiubo Li * For bidi case, the first count blocks are for Data-Out 767a5d68ba8SXiubo Li * buffer blocks, and before gathering the Data-In buffer 768a5d68ba8SXiubo Li * the Data-Out buffer blocks should be discarded. 769a5d68ba8SXiubo Li */ 770a5d68ba8SXiubo Li count = DIV_ROUND_UP(se_cmd->data_length, DATA_BLOCK_SIZE); 771a5d68ba8SXiubo Li 772a5d68ba8SXiubo Li data_sg = se_cmd->t_bidi_data_sg; 773a5d68ba8SXiubo Li data_nents = se_cmd->t_bidi_data_nents; 774a5d68ba8SXiubo Li } 775f97ec7dbSIlias Tsitsimpis 776141685a3SXiubo Li tcmu_cmd_set_dbi_cur(cmd, count); 777141685a3SXiubo Li 778f97ec7dbSIlias Tsitsimpis for_each_sg(data_sg, sg, data_nents, i) { 77926418649SSheng Yang int sg_remaining = sg->length; 780f97ec7dbSIlias Tsitsimpis to = kmap_atomic(sg_page(sg)) + sg->offset; 78126418649SSheng Yang while (sg_remaining > 0) { 78226418649SSheng Yang if (block_remaining == 0) { 783b6df4b79SXiubo Li if (from) 784b6df4b79SXiubo Li kunmap_atomic(from); 785b6df4b79SXiubo Li 78626418649SSheng Yang block_remaining = DATA_BLOCK_SIZE; 787141685a3SXiubo Li dbi = tcmu_cmd_get_dbi(cmd); 788b6df4b79SXiubo Li page = tcmu_get_block_page(udev, dbi); 789b6df4b79SXiubo Li from = kmap_atomic(page); 79026418649SSheng Yang } 79126418649SSheng Yang copy_bytes = min_t(size_t, sg_remaining, 79226418649SSheng Yang block_remaining); 793141685a3SXiubo Li offset = DATA_BLOCK_SIZE - block_remaining; 794f97ec7dbSIlias Tsitsimpis tcmu_flush_dcache_range(from, copy_bytes); 795c542942cSXiubo Li memcpy(to + sg->length - sg_remaining, from + offset, 79626418649SSheng Yang copy_bytes); 797f97ec7dbSIlias Tsitsimpis 79826418649SSheng Yang sg_remaining -= copy_bytes; 79926418649SSheng Yang block_remaining -= copy_bytes; 800f97ec7dbSIlias Tsitsimpis } 801e2e21bd8SSagi Grimberg kunmap_atomic(to - sg->offset); 802f97ec7dbSIlias Tsitsimpis } 803b6df4b79SXiubo Li if (from) 804b6df4b79SXiubo Li kunmap_atomic(from); 805f97ec7dbSIlias Tsitsimpis } 806f97ec7dbSIlias Tsitsimpis 807b6df4b79SXiubo Li static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh) 80826418649SSheng Yang { 8093c0f26ffSMike Christie return thresh - bitmap_weight(bitmap, thresh); 81026418649SSheng Yang } 81126418649SSheng Yang 8127c9e7a6fSAndy Grover /* 813f97ec7dbSIlias Tsitsimpis * We can't queue a command until we have space available on the cmd ring *and* 8143d9b9555SAndy Grover * space available on the data area. 8157c9e7a6fSAndy Grover * 8167c9e7a6fSAndy Grover * Called with ring lock held. 8177c9e7a6fSAndy Grover */ 818b6df4b79SXiubo Li static bool is_ring_space_avail(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 819b6df4b79SXiubo Li size_t cmd_size, size_t data_needed) 8207c9e7a6fSAndy Grover { 8217c9e7a6fSAndy Grover struct tcmu_mailbox *mb = udev->mb_addr; 822b6df4b79SXiubo Li uint32_t blocks_needed = (data_needed + DATA_BLOCK_SIZE - 1) 823b6df4b79SXiubo Li / DATA_BLOCK_SIZE; 8240241fd39SNicholas Bellinger size_t space, cmd_needed; 8257c9e7a6fSAndy Grover u32 cmd_head; 8267c9e7a6fSAndy Grover 8277c9e7a6fSAndy Grover tcmu_flush_dcache_range(mb, sizeof(*mb)); 8287c9e7a6fSAndy Grover 8297c9e7a6fSAndy Grover cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 8307c9e7a6fSAndy Grover 831f56574a2SAndy Grover /* 832f56574a2SAndy Grover * If cmd end-of-ring space is too small then we need space for a NOP plus 833f56574a2SAndy Grover * original cmd - cmds are internally contiguous. 834f56574a2SAndy Grover */ 835f56574a2SAndy Grover if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size) 836f56574a2SAndy Grover cmd_needed = cmd_size; 837f56574a2SAndy Grover else 838f56574a2SAndy Grover cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size); 839f56574a2SAndy Grover 8407c9e7a6fSAndy Grover space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size); 8417c9e7a6fSAndy Grover if (space < cmd_needed) { 8427c9e7a6fSAndy Grover pr_debug("no cmd space: %u %u %u\n", cmd_head, 8437c9e7a6fSAndy Grover udev->cmdr_last_cleaned, udev->cmdr_size); 8447c9e7a6fSAndy Grover return false; 8457c9e7a6fSAndy Grover } 8467c9e7a6fSAndy Grover 847b6df4b79SXiubo Li /* try to check and get the data blocks as needed */ 848b6df4b79SXiubo Li space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh); 8493c0f26ffSMike Christie if ((space * DATA_BLOCK_SIZE) < data_needed) { 85080eb8761SMike Christie unsigned long blocks_left = 85180eb8761SMike Christie (udev->max_blocks - udev->dbi_thresh) + space; 852b6df4b79SXiubo Li 853b6df4b79SXiubo Li if (blocks_left < blocks_needed) { 854b6df4b79SXiubo Li pr_debug("no data space: only %lu available, but ask for %zu\n", 855b6df4b79SXiubo Li blocks_left * DATA_BLOCK_SIZE, 856b6df4b79SXiubo Li data_needed); 8577c9e7a6fSAndy Grover return false; 8587c9e7a6fSAndy Grover } 8597c9e7a6fSAndy Grover 860f890f579SMike Christie udev->dbi_thresh += blocks_needed; 86180eb8761SMike Christie if (udev->dbi_thresh > udev->max_blocks) 86280eb8761SMike Christie udev->dbi_thresh = udev->max_blocks; 863b6df4b79SXiubo Li } 864b6df4b79SXiubo Li 865daf78c30SXiubo Li return tcmu_get_empty_blocks(udev, cmd); 8667c9e7a6fSAndy Grover } 8677c9e7a6fSAndy Grover 868fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt) 869fe25cc34SXiubo Li { 870fe25cc34SXiubo Li return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]), 871fe25cc34SXiubo Li sizeof(struct tcmu_cmd_entry)); 872fe25cc34SXiubo Li } 873fe25cc34SXiubo Li 874fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd, 875fe25cc34SXiubo Li size_t base_command_size) 876fe25cc34SXiubo Li { 877fe25cc34SXiubo Li struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 878fe25cc34SXiubo Li size_t command_size; 879fe25cc34SXiubo Li 880fe25cc34SXiubo Li command_size = base_command_size + 881fe25cc34SXiubo Li round_up(scsi_command_size(se_cmd->t_task_cdb), 882fe25cc34SXiubo Li TCMU_OP_ALIGN_SIZE); 883fe25cc34SXiubo Li 884fe25cc34SXiubo Li WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1)); 885fe25cc34SXiubo Li 886fe25cc34SXiubo Li return command_size; 887fe25cc34SXiubo Li } 888fe25cc34SXiubo Li 8899103575aSMike Christie static int tcmu_setup_cmd_timer(struct tcmu_cmd *tcmu_cmd, unsigned int tmo, 8909103575aSMike Christie struct timer_list *timer) 8910d44374cSMike Christie { 8920d44374cSMike Christie struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 8930d44374cSMike Christie int cmd_id; 8940d44374cSMike Christie 8950d44374cSMike Christie if (tcmu_cmd->cmd_id) 8969103575aSMike Christie goto setup_timer; 8970d44374cSMike Christie 8980d44374cSMike Christie cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 1, USHRT_MAX, GFP_NOWAIT); 8990d44374cSMike Christie if (cmd_id < 0) { 9000d44374cSMike Christie pr_err("tcmu: Could not allocate cmd id.\n"); 9010d44374cSMike Christie return cmd_id; 9020d44374cSMike Christie } 9030d44374cSMike Christie tcmu_cmd->cmd_id = cmd_id; 9040d44374cSMike Christie 905af1dd7ffSMike Christie pr_debug("allocated cmd %u for dev %s tmo %lu\n", tcmu_cmd->cmd_id, 906af1dd7ffSMike Christie udev->name, tmo / MSEC_PER_SEC); 9070d44374cSMike Christie 9089103575aSMike Christie setup_timer: 9099103575aSMike Christie if (!tmo) 9109103575aSMike Christie return 0; 9119103575aSMike Christie 9120d44374cSMike Christie tcmu_cmd->deadline = round_jiffies_up(jiffies + msecs_to_jiffies(tmo)); 9139103575aSMike Christie mod_timer(timer, tcmu_cmd->deadline); 9140d44374cSMike Christie return 0; 9150d44374cSMike Christie } 9160d44374cSMike Christie 917af1dd7ffSMike Christie static int add_to_cmdr_queue(struct tcmu_cmd *tcmu_cmd) 918af1dd7ffSMike Christie { 919af1dd7ffSMike Christie struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 9209103575aSMike Christie unsigned int tmo; 921af1dd7ffSMike Christie int ret; 922af1dd7ffSMike Christie 9239103575aSMike Christie /* 9249103575aSMike Christie * For backwards compat if qfull_time_out is not set use 9259103575aSMike Christie * cmd_time_out and if that's not set use the default time out. 9269103575aSMike Christie */ 9279103575aSMike Christie if (!udev->qfull_time_out) 9289103575aSMike Christie return -ETIMEDOUT; 9299103575aSMike Christie else if (udev->qfull_time_out > 0) 9309103575aSMike Christie tmo = udev->qfull_time_out; 9319103575aSMike Christie else if (udev->cmd_time_out) 9329103575aSMike Christie tmo = udev->cmd_time_out; 9339103575aSMike Christie else 9349103575aSMike Christie tmo = TCMU_TIME_OUT; 9359103575aSMike Christie 9369103575aSMike Christie ret = tcmu_setup_cmd_timer(tcmu_cmd, tmo, &udev->qfull_timer); 937af1dd7ffSMike Christie if (ret) 938af1dd7ffSMike Christie return ret; 939af1dd7ffSMike Christie 940af1dd7ffSMike Christie list_add_tail(&tcmu_cmd->cmdr_queue_entry, &udev->cmdr_queue); 941af1dd7ffSMike Christie pr_debug("adding cmd %u on dev %s to ring space wait queue\n", 942af1dd7ffSMike Christie tcmu_cmd->cmd_id, udev->name); 943af1dd7ffSMike Christie return 0; 944af1dd7ffSMike Christie } 945af1dd7ffSMike Christie 9466fd0ce79SMike Christie /** 9476fd0ce79SMike Christie * queue_cmd_ring - queue cmd to ring or internally 9486fd0ce79SMike Christie * @tcmu_cmd: cmd to queue 9496fd0ce79SMike Christie * @scsi_err: TCM error code if failure (-1) returned. 9506fd0ce79SMike Christie * 9516fd0ce79SMike Christie * Returns: 9526fd0ce79SMike Christie * -1 we cannot queue internally or to the ring. 9536fd0ce79SMike Christie * 0 success 954af1dd7ffSMike Christie * 1 internally queued to wait for ring memory to free. 9556fd0ce79SMike Christie */ 9566fd0ce79SMike Christie static sense_reason_t queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, int *scsi_err) 9577c9e7a6fSAndy Grover { 9587c9e7a6fSAndy Grover struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 9597c9e7a6fSAndy Grover struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 9607c9e7a6fSAndy Grover size_t base_command_size, command_size; 9617c9e7a6fSAndy Grover struct tcmu_mailbox *mb; 9627c9e7a6fSAndy Grover struct tcmu_cmd_entry *entry; 9637c9e7a6fSAndy Grover struct iovec *iov; 964141685a3SXiubo Li int iov_cnt, ret; 9657c9e7a6fSAndy Grover uint32_t cmd_head; 9667c9e7a6fSAndy Grover uint64_t cdb_off; 967f97ec7dbSIlias Tsitsimpis bool copy_to_data_area; 968ab22d260SXiubo Li size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd); 9697c9e7a6fSAndy Grover 9706fd0ce79SMike Christie *scsi_err = TCM_NO_SENSE; 9716fd0ce79SMike Christie 972892782caSMike Christie if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) { 973892782caSMike Christie *scsi_err = TCM_LUN_BUSY; 974892782caSMike Christie return -1; 975892782caSMike Christie } 976892782caSMike Christie 9776fd0ce79SMike Christie if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) { 9786fd0ce79SMike Christie *scsi_err = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 9796fd0ce79SMike Christie return -1; 9806fd0ce79SMike Christie } 9817c9e7a6fSAndy Grover 9827c9e7a6fSAndy Grover /* 9837c9e7a6fSAndy Grover * Must be a certain minimum size for response sense info, but 9847c9e7a6fSAndy Grover * also may be larger if the iov array is large. 9857c9e7a6fSAndy Grover * 986fe25cc34SXiubo Li * We prepare as many iovs as possbile for potential uses here, 987fe25cc34SXiubo Li * because it's expensive to tell how many regions are freed in 988fe25cc34SXiubo Li * the bitmap & global data pool, as the size calculated here 989fe25cc34SXiubo Li * will only be used to do the checks. 990fe25cc34SXiubo Li * 991fe25cc34SXiubo Li * The size will be recalculated later as actually needed to save 992fe25cc34SXiubo Li * cmd area memories. 9937c9e7a6fSAndy Grover */ 994fe25cc34SXiubo Li base_command_size = tcmu_cmd_get_base_cmd_size(tcmu_cmd->dbi_cnt); 995fe25cc34SXiubo Li command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size); 9967c9e7a6fSAndy Grover 997af1dd7ffSMike Christie if (!list_empty(&udev->cmdr_queue)) 998af1dd7ffSMike Christie goto queue; 9997c9e7a6fSAndy Grover 10007c9e7a6fSAndy Grover mb = udev->mb_addr; 10017c9e7a6fSAndy Grover cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 1002554617b2SAndy Grover if ((command_size > (udev->cmdr_size / 2)) || 1003554617b2SAndy Grover data_length > udev->data_size) { 1004554617b2SAndy Grover pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu " 10053d9b9555SAndy Grover "cmd ring/data area\n", command_size, data_length, 10067c9e7a6fSAndy Grover udev->cmdr_size, udev->data_size); 10076fd0ce79SMike Christie *scsi_err = TCM_INVALID_CDB_FIELD; 10086fd0ce79SMike Christie return -1; 1009554617b2SAndy Grover } 10107c9e7a6fSAndy Grover 1011af1dd7ffSMike Christie if (!is_ring_space_avail(udev, tcmu_cmd, command_size, data_length)) { 1012810b8153SMike Christie /* 1013810b8153SMike Christie * Don't leave commands partially setup because the unmap 1014810b8153SMike Christie * thread might need the blocks to make forward progress. 1015810b8153SMike Christie */ 1016810b8153SMike Christie tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur); 1017810b8153SMike Christie tcmu_cmd_reset_dbi_cur(tcmu_cmd); 1018af1dd7ffSMike Christie goto queue; 10197c9e7a6fSAndy Grover } 10207c9e7a6fSAndy Grover 1021f56574a2SAndy Grover /* Insert a PAD if end-of-ring space is too small */ 1022f56574a2SAndy Grover if (head_to_end(cmd_head, udev->cmdr_size) < command_size) { 1023f56574a2SAndy Grover size_t pad_size = head_to_end(cmd_head, udev->cmdr_size); 1024f56574a2SAndy Grover 10257c9e7a6fSAndy Grover entry = (void *) mb + CMDR_OFF + cmd_head; 10260ad46af8SAndy Grover tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD); 10270ad46af8SAndy Grover tcmu_hdr_set_len(&entry->hdr.len_op, pad_size); 10280ad46af8SAndy Grover entry->hdr.cmd_id = 0; /* not used for PAD */ 10290ad46af8SAndy Grover entry->hdr.kflags = 0; 10300ad46af8SAndy Grover entry->hdr.uflags = 0; 10319d62bc0eSXiubo Li tcmu_flush_dcache_range(entry, sizeof(*entry)); 10327c9e7a6fSAndy Grover 10337c9e7a6fSAndy Grover UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size); 10349d62bc0eSXiubo Li tcmu_flush_dcache_range(mb, sizeof(*mb)); 10357c9e7a6fSAndy Grover 10367c9e7a6fSAndy Grover cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 10377c9e7a6fSAndy Grover WARN_ON(cmd_head != 0); 10387c9e7a6fSAndy Grover } 10397c9e7a6fSAndy Grover 10407c9e7a6fSAndy Grover entry = (void *) mb + CMDR_OFF + cmd_head; 1041b3743c71SXiubo Li memset(entry, 0, command_size); 10420ad46af8SAndy Grover tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD); 10437c9e7a6fSAndy Grover 10443d9b9555SAndy Grover /* Handle allocating space from the data area */ 1045b6df4b79SXiubo Li tcmu_cmd_reset_dbi_cur(tcmu_cmd); 10467c9e7a6fSAndy Grover iov = &entry->req.iov[0]; 1047f97ec7dbSIlias Tsitsimpis iov_cnt = 0; 1048e4648b01SIlias Tsitsimpis copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE 1049e4648b01SIlias Tsitsimpis || se_cmd->se_cmd_flags & SCF_BIDI); 10501a1fc0b8SMike Christie scatter_data_area(udev, tcmu_cmd, se_cmd->t_data_sg, 1051b6df4b79SXiubo Li se_cmd->t_data_nents, &iov, &iov_cnt, 1052b6df4b79SXiubo Li copy_to_data_area); 10537c9e7a6fSAndy Grover entry->req.iov_cnt = iov_cnt; 10547c9e7a6fSAndy Grover 1055e4648b01SIlias Tsitsimpis /* Handle BIDI commands */ 1056e4648b01SIlias Tsitsimpis iov_cnt = 0; 1057b3743c71SXiubo Li if (se_cmd->se_cmd_flags & SCF_BIDI) { 1058ab22d260SXiubo Li iov++; 10591a1fc0b8SMike Christie scatter_data_area(udev, tcmu_cmd, se_cmd->t_bidi_data_sg, 10601a1fc0b8SMike Christie se_cmd->t_bidi_data_nents, &iov, &iov_cnt, 10611a1fc0b8SMike Christie false); 1062ab22d260SXiubo Li } 1063b3743c71SXiubo Li entry->req.iov_bidi_cnt = iov_cnt; 106426418649SSheng Yang 10659103575aSMike Christie ret = tcmu_setup_cmd_timer(tcmu_cmd, udev->cmd_time_out, 10669103575aSMike Christie &udev->cmd_timer); 10670d44374cSMike Christie if (ret) { 10680d44374cSMike Christie tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt); 10696fd0ce79SMike Christie 10706fd0ce79SMike Christie *scsi_err = TCM_OUT_OF_RESOURCES; 10716fd0ce79SMike Christie return -1; 10720d44374cSMike Christie } 10730d44374cSMike Christie entry->hdr.cmd_id = tcmu_cmd->cmd_id; 10740d44374cSMike Christie 1075fe25cc34SXiubo Li /* 1076fe25cc34SXiubo Li * Recalaulate the command's base size and size according 1077fe25cc34SXiubo Li * to the actual needs 1078fe25cc34SXiubo Li */ 1079fe25cc34SXiubo Li base_command_size = tcmu_cmd_get_base_cmd_size(entry->req.iov_cnt + 1080fe25cc34SXiubo Li entry->req.iov_bidi_cnt); 1081fe25cc34SXiubo Li command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size); 1082fe25cc34SXiubo Li 1083fe25cc34SXiubo Li tcmu_hdr_set_len(&entry->hdr.len_op, command_size); 1084fe25cc34SXiubo Li 10857c9e7a6fSAndy Grover /* All offsets relative to mb_addr, not start of entry! */ 10867c9e7a6fSAndy Grover cdb_off = CMDR_OFF + cmd_head + base_command_size; 10877c9e7a6fSAndy Grover memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb)); 10887c9e7a6fSAndy Grover entry->req.cdb_off = cdb_off; 10897c9e7a6fSAndy Grover tcmu_flush_dcache_range(entry, sizeof(*entry)); 10907c9e7a6fSAndy Grover 10917c9e7a6fSAndy Grover UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size); 10927c9e7a6fSAndy Grover tcmu_flush_dcache_range(mb, sizeof(*mb)); 10937c9e7a6fSAndy Grover 10947c9e7a6fSAndy Grover /* TODO: only if FLUSH and FUA? */ 10957c9e7a6fSAndy Grover uio_event_notify(&udev->uio_info); 10967c9e7a6fSAndy Grover 10976fd0ce79SMike Christie return 0; 1098af1dd7ffSMike Christie 1099af1dd7ffSMike Christie queue: 1100af1dd7ffSMike Christie if (add_to_cmdr_queue(tcmu_cmd)) { 1101af1dd7ffSMike Christie *scsi_err = TCM_OUT_OF_RESOURCES; 1102af1dd7ffSMike Christie return -1; 1103af1dd7ffSMike Christie } 1104af1dd7ffSMike Christie 1105af1dd7ffSMike Christie return 1; 11067c9e7a6fSAndy Grover } 11077c9e7a6fSAndy Grover 110802eb924fSAndy Grover static sense_reason_t 110902eb924fSAndy Grover tcmu_queue_cmd(struct se_cmd *se_cmd) 11107c9e7a6fSAndy Grover { 1111af1dd7ffSMike Christie struct se_device *se_dev = se_cmd->se_dev; 1112af1dd7ffSMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 11137c9e7a6fSAndy Grover struct tcmu_cmd *tcmu_cmd; 11146fd0ce79SMike Christie sense_reason_t scsi_ret; 1115af1dd7ffSMike Christie int ret; 11167c9e7a6fSAndy Grover 11177c9e7a6fSAndy Grover tcmu_cmd = tcmu_alloc_cmd(se_cmd); 11187c9e7a6fSAndy Grover if (!tcmu_cmd) 111902eb924fSAndy Grover return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 11207c9e7a6fSAndy Grover 1121af1dd7ffSMike Christie mutex_lock(&udev->cmdr_lock); 1122af1dd7ffSMike Christie ret = queue_cmd_ring(tcmu_cmd, &scsi_ret); 1123af1dd7ffSMike Christie mutex_unlock(&udev->cmdr_lock); 1124af1dd7ffSMike Christie if (ret < 0) 1125141685a3SXiubo Li tcmu_free_cmd(tcmu_cmd); 11266fd0ce79SMike Christie return scsi_ret; 11277c9e7a6fSAndy Grover } 11287c9e7a6fSAndy Grover 11297c9e7a6fSAndy Grover static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry) 11307c9e7a6fSAndy Grover { 11317c9e7a6fSAndy Grover struct se_cmd *se_cmd = cmd->se_cmd; 11327c9e7a6fSAndy Grover struct tcmu_dev *udev = cmd->tcmu_dev; 11337c9e7a6fSAndy Grover 1134b25c7863SSheng Yang /* 1135b25c7863SSheng Yang * cmd has been completed already from timeout, just reclaim 11363d9b9555SAndy Grover * data area space and free cmd 1137b25c7863SSheng Yang */ 1138141685a3SXiubo Li if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) 1139141685a3SXiubo Li goto out; 1140b25c7863SSheng Yang 1141141685a3SXiubo Li tcmu_cmd_reset_dbi_cur(cmd); 11427c9e7a6fSAndy Grover 11430ad46af8SAndy Grover if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) { 11440ad46af8SAndy Grover pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n", 11450ad46af8SAndy Grover cmd->se_cmd); 1146ed97d0cdSAndy Grover entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION; 1147ed97d0cdSAndy Grover } else if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) { 1148406f74c2SMike Christie transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer); 1149e4648b01SIlias Tsitsimpis } else if (se_cmd->se_cmd_flags & SCF_BIDI) { 115026418649SSheng Yang /* Get Data-In buffer before clean up */ 1151a5d68ba8SXiubo Li gather_data_area(udev, cmd, true); 1152e4648b01SIlias Tsitsimpis } else if (se_cmd->data_direction == DMA_FROM_DEVICE) { 1153a5d68ba8SXiubo Li gather_data_area(udev, cmd, false); 11547c9e7a6fSAndy Grover } else if (se_cmd->data_direction == DMA_TO_DEVICE) { 1155141685a3SXiubo Li /* TODO: */ 11562bc396a2SIlias Tsitsimpis } else if (se_cmd->data_direction != DMA_NONE) { 11572bc396a2SIlias Tsitsimpis pr_warn("TCMU: data direction was %d!\n", 11582bc396a2SIlias Tsitsimpis se_cmd->data_direction); 11597c9e7a6fSAndy Grover } 11607c9e7a6fSAndy Grover 11617c9e7a6fSAndy Grover target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status); 11627c9e7a6fSAndy Grover 1163141685a3SXiubo Li out: 1164141685a3SXiubo Li cmd->se_cmd = NULL; 1165b6df4b79SXiubo Li tcmu_cmd_free_data(cmd, cmd->dbi_cnt); 1166141685a3SXiubo Li tcmu_free_cmd(cmd); 11677c9e7a6fSAndy Grover } 11687c9e7a6fSAndy Grover 11697c9e7a6fSAndy Grover static unsigned int tcmu_handle_completions(struct tcmu_dev *udev) 11707c9e7a6fSAndy Grover { 11717c9e7a6fSAndy Grover struct tcmu_mailbox *mb; 11727c9e7a6fSAndy Grover int handled = 0; 11737c9e7a6fSAndy Grover 11747c9e7a6fSAndy Grover if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) { 11757c9e7a6fSAndy Grover pr_err("ring broken, not handling completions\n"); 11767c9e7a6fSAndy Grover return 0; 11777c9e7a6fSAndy Grover } 11787c9e7a6fSAndy Grover 11797c9e7a6fSAndy Grover mb = udev->mb_addr; 11807c9e7a6fSAndy Grover tcmu_flush_dcache_range(mb, sizeof(*mb)); 11817c9e7a6fSAndy Grover 11826aa7de05SMark Rutland while (udev->cmdr_last_cleaned != READ_ONCE(mb->cmd_tail)) { 11837c9e7a6fSAndy Grover 11847c9e7a6fSAndy Grover struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned; 11857c9e7a6fSAndy Grover struct tcmu_cmd *cmd; 11867c9e7a6fSAndy Grover 11877c9e7a6fSAndy Grover tcmu_flush_dcache_range(entry, sizeof(*entry)); 11887c9e7a6fSAndy Grover 11890ad46af8SAndy Grover if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) { 11900ad46af8SAndy Grover UPDATE_HEAD(udev->cmdr_last_cleaned, 11910ad46af8SAndy Grover tcmu_hdr_get_len(entry->hdr.len_op), 11920ad46af8SAndy Grover udev->cmdr_size); 11937c9e7a6fSAndy Grover continue; 11947c9e7a6fSAndy Grover } 11950ad46af8SAndy Grover WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD); 11967c9e7a6fSAndy Grover 1197d3e709e6SMatthew Wilcox cmd = idr_remove(&udev->commands, entry->hdr.cmd_id); 11987c9e7a6fSAndy Grover if (!cmd) { 119988cf1073SMike Christie pr_err("cmd_id %u not found, ring is broken\n", 120088cf1073SMike Christie entry->hdr.cmd_id); 12017c9e7a6fSAndy Grover set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags); 12027c9e7a6fSAndy Grover break; 12037c9e7a6fSAndy Grover } 12047c9e7a6fSAndy Grover 12057c9e7a6fSAndy Grover tcmu_handle_completion(cmd, entry); 12067c9e7a6fSAndy Grover 12070ad46af8SAndy Grover UPDATE_HEAD(udev->cmdr_last_cleaned, 12080ad46af8SAndy Grover tcmu_hdr_get_len(entry->hdr.len_op), 12090ad46af8SAndy Grover udev->cmdr_size); 12107c9e7a6fSAndy Grover 12117c9e7a6fSAndy Grover handled++; 12127c9e7a6fSAndy Grover } 12137c9e7a6fSAndy Grover 12149103575aSMike Christie if (mb->cmd_tail == mb->cmd_head) { 12159103575aSMike Christie /* no more pending commands */ 12169103575aSMike Christie del_timer(&udev->cmd_timer); 12179103575aSMike Christie 12189103575aSMike Christie if (list_empty(&udev->cmdr_queue)) { 1219af1dd7ffSMike Christie /* 12209103575aSMike Christie * no more pending or waiting commands so try to 12219103575aSMike Christie * reclaim blocks if needed. 1222af1dd7ffSMike Christie */ 12239103575aSMike Christie if (atomic_read(&global_db_count) > 122480eb8761SMike Christie tcmu_global_max_blocks) 1225af1dd7ffSMike Christie schedule_delayed_work(&tcmu_unmap_work, 0); 1226af1dd7ffSMike Christie } 12279103575aSMike Christie } 12287c9e7a6fSAndy Grover 12297c9e7a6fSAndy Grover return handled; 12307c9e7a6fSAndy Grover } 12317c9e7a6fSAndy Grover 12327c9e7a6fSAndy Grover static int tcmu_check_expired_cmd(int id, void *p, void *data) 12337c9e7a6fSAndy Grover { 12347c9e7a6fSAndy Grover struct tcmu_cmd *cmd = p; 1235af1dd7ffSMike Christie struct tcmu_dev *udev = cmd->tcmu_dev; 1236af1dd7ffSMike Christie u8 scsi_status; 1237af1dd7ffSMike Christie struct se_cmd *se_cmd; 1238af1dd7ffSMike Christie bool is_running; 12397c9e7a6fSAndy Grover 12407c9e7a6fSAndy Grover if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) 12417c9e7a6fSAndy Grover return 0; 12427c9e7a6fSAndy Grover 1243611e2267SAndy Grover if (!time_after(jiffies, cmd->deadline)) 12447c9e7a6fSAndy Grover return 0; 12457c9e7a6fSAndy Grover 1246af1dd7ffSMike Christie is_running = list_empty(&cmd->cmdr_queue_entry); 124745dc488cSMike Christie se_cmd = cmd->se_cmd; 12487c9e7a6fSAndy Grover 1249af1dd7ffSMike Christie if (is_running) { 12509103575aSMike Christie /* 12519103575aSMike Christie * If cmd_time_out is disabled but qfull is set deadline 12529103575aSMike Christie * will only reflect the qfull timeout. Ignore it. 12539103575aSMike Christie */ 12549103575aSMike Christie if (!udev->cmd_time_out) 12559103575aSMike Christie return 0; 12569103575aSMike Christie 1257af1dd7ffSMike Christie set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags); 1258af1dd7ffSMike Christie /* 1259af1dd7ffSMike Christie * target_complete_cmd will translate this to LUN COMM FAILURE 1260af1dd7ffSMike Christie */ 1261af1dd7ffSMike Christie scsi_status = SAM_STAT_CHECK_CONDITION; 1262af1dd7ffSMike Christie } else { 1263af1dd7ffSMike Christie list_del_init(&cmd->cmdr_queue_entry); 1264af1dd7ffSMike Christie 1265af1dd7ffSMike Christie idr_remove(&udev->commands, id); 1266af1dd7ffSMike Christie tcmu_free_cmd(cmd); 1267af1dd7ffSMike Christie scsi_status = SAM_STAT_TASK_SET_FULL; 1268af1dd7ffSMike Christie } 12699103575aSMike Christie 12709103575aSMike Christie pr_debug("Timing out cmd %u on dev %s that is %s.\n", 12719103575aSMike Christie id, udev->name, is_running ? "inflight" : "queued"); 12729103575aSMike Christie 1273af1dd7ffSMike Christie target_complete_cmd(se_cmd, scsi_status); 12747c9e7a6fSAndy Grover return 0; 12757c9e7a6fSAndy Grover } 12767c9e7a6fSAndy Grover 12779103575aSMike Christie static void tcmu_device_timedout(struct tcmu_dev *udev) 12787c9e7a6fSAndy Grover { 1279488ebe4cSMike Christie spin_lock(&timed_out_udevs_lock); 1280488ebe4cSMike Christie if (list_empty(&udev->timedout_entry)) 1281488ebe4cSMike Christie list_add_tail(&udev->timedout_entry, &timed_out_udevs); 1282488ebe4cSMike Christie spin_unlock(&timed_out_udevs_lock); 12837c9e7a6fSAndy Grover 1284af1dd7ffSMike Christie schedule_delayed_work(&tcmu_unmap_work, 0); 12857c9e7a6fSAndy Grover } 12867c9e7a6fSAndy Grover 12879103575aSMike Christie static void tcmu_cmd_timedout(struct timer_list *t) 12889103575aSMike Christie { 12899103575aSMike Christie struct tcmu_dev *udev = from_timer(udev, t, cmd_timer); 12909103575aSMike Christie 12919103575aSMike Christie pr_debug("%s cmd timeout has expired\n", udev->name); 12929103575aSMike Christie tcmu_device_timedout(udev); 12939103575aSMike Christie } 12949103575aSMike Christie 12959103575aSMike Christie static void tcmu_qfull_timedout(struct timer_list *t) 12969103575aSMike Christie { 12979103575aSMike Christie struct tcmu_dev *udev = from_timer(udev, t, qfull_timer); 12989103575aSMike Christie 12999103575aSMike Christie pr_debug("%s qfull timeout has expired\n", udev->name); 13009103575aSMike Christie tcmu_device_timedout(udev); 13019103575aSMike Christie } 13029103575aSMike Christie 13037c9e7a6fSAndy Grover static int tcmu_attach_hba(struct se_hba *hba, u32 host_id) 13047c9e7a6fSAndy Grover { 13057c9e7a6fSAndy Grover struct tcmu_hba *tcmu_hba; 13067c9e7a6fSAndy Grover 13077c9e7a6fSAndy Grover tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL); 13087c9e7a6fSAndy Grover if (!tcmu_hba) 13097c9e7a6fSAndy Grover return -ENOMEM; 13107c9e7a6fSAndy Grover 13117c9e7a6fSAndy Grover tcmu_hba->host_id = host_id; 13127c9e7a6fSAndy Grover hba->hba_ptr = tcmu_hba; 13137c9e7a6fSAndy Grover 13147c9e7a6fSAndy Grover return 0; 13157c9e7a6fSAndy Grover } 13167c9e7a6fSAndy Grover 13177c9e7a6fSAndy Grover static void tcmu_detach_hba(struct se_hba *hba) 13187c9e7a6fSAndy Grover { 13197c9e7a6fSAndy Grover kfree(hba->hba_ptr); 13207c9e7a6fSAndy Grover hba->hba_ptr = NULL; 13217c9e7a6fSAndy Grover } 13227c9e7a6fSAndy Grover 13237c9e7a6fSAndy Grover static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name) 13247c9e7a6fSAndy Grover { 13257c9e7a6fSAndy Grover struct tcmu_dev *udev; 13267c9e7a6fSAndy Grover 13277c9e7a6fSAndy Grover udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL); 13287c9e7a6fSAndy Grover if (!udev) 13297c9e7a6fSAndy Grover return NULL; 1330f3cdbe39SMike Christie kref_init(&udev->kref); 13317c9e7a6fSAndy Grover 13327c9e7a6fSAndy Grover udev->name = kstrdup(name, GFP_KERNEL); 13337c9e7a6fSAndy Grover if (!udev->name) { 13347c9e7a6fSAndy Grover kfree(udev); 13357c9e7a6fSAndy Grover return NULL; 13367c9e7a6fSAndy Grover } 13377c9e7a6fSAndy Grover 13387c9e7a6fSAndy Grover udev->hba = hba; 1339af980e46SMike Christie udev->cmd_time_out = TCMU_TIME_OUT; 13409103575aSMike Christie udev->qfull_time_out = -1; 13417c9e7a6fSAndy Grover 134280eb8761SMike Christie udev->max_blocks = DATA_BLOCK_BITS_DEF; 1343b6df4b79SXiubo Li mutex_init(&udev->cmdr_lock); 13447c9e7a6fSAndy Grover 1345ff07e4a4SMike Christie INIT_LIST_HEAD(&udev->node); 1346488ebe4cSMike Christie INIT_LIST_HEAD(&udev->timedout_entry); 1347af1dd7ffSMike Christie INIT_LIST_HEAD(&udev->cmdr_queue); 13487c9e7a6fSAndy Grover idr_init(&udev->commands); 13497c9e7a6fSAndy Grover 13509103575aSMike Christie timer_setup(&udev->qfull_timer, tcmu_qfull_timedout, 0); 13519103575aSMike Christie timer_setup(&udev->cmd_timer, tcmu_cmd_timedout, 0); 13527c9e7a6fSAndy Grover 1353c22adc0bSXiubo Li INIT_RADIX_TREE(&udev->data_blocks, GFP_KERNEL); 1354c22adc0bSXiubo Li 13557c9e7a6fSAndy Grover return &udev->se_dev; 13567c9e7a6fSAndy Grover } 13577c9e7a6fSAndy Grover 1358892782caSMike Christie static bool run_cmdr_queue(struct tcmu_dev *udev, bool fail) 1359af1dd7ffSMike Christie { 1360af1dd7ffSMike Christie struct tcmu_cmd *tcmu_cmd, *tmp_cmd; 1361af1dd7ffSMike Christie LIST_HEAD(cmds); 1362af1dd7ffSMike Christie bool drained = true; 1363af1dd7ffSMike Christie sense_reason_t scsi_ret; 1364af1dd7ffSMike Christie int ret; 1365af1dd7ffSMike Christie 1366af1dd7ffSMike Christie if (list_empty(&udev->cmdr_queue)) 1367af1dd7ffSMike Christie return true; 1368af1dd7ffSMike Christie 1369892782caSMike Christie pr_debug("running %s's cmdr queue forcefail %d\n", udev->name, fail); 1370af1dd7ffSMike Christie 1371af1dd7ffSMike Christie list_splice_init(&udev->cmdr_queue, &cmds); 1372af1dd7ffSMike Christie 1373af1dd7ffSMike Christie list_for_each_entry_safe(tcmu_cmd, tmp_cmd, &cmds, cmdr_queue_entry) { 1374af1dd7ffSMike Christie list_del_init(&tcmu_cmd->cmdr_queue_entry); 1375af1dd7ffSMike Christie 1376af1dd7ffSMike Christie pr_debug("removing cmd %u on dev %s from queue\n", 1377af1dd7ffSMike Christie tcmu_cmd->cmd_id, udev->name); 1378af1dd7ffSMike Christie 1379892782caSMike Christie if (fail) { 1380892782caSMike Christie idr_remove(&udev->commands, tcmu_cmd->cmd_id); 1381892782caSMike Christie /* 1382892782caSMike Christie * We were not able to even start the command, so 1383892782caSMike Christie * fail with busy to allow a retry in case runner 1384892782caSMike Christie * was only temporarily down. If the device is being 1385892782caSMike Christie * removed then LIO core will do the right thing and 1386892782caSMike Christie * fail the retry. 1387892782caSMike Christie */ 1388892782caSMike Christie target_complete_cmd(tcmu_cmd->se_cmd, SAM_STAT_BUSY); 1389892782caSMike Christie tcmu_free_cmd(tcmu_cmd); 1390892782caSMike Christie continue; 1391892782caSMike Christie } 1392892782caSMike Christie 1393af1dd7ffSMike Christie ret = queue_cmd_ring(tcmu_cmd, &scsi_ret); 1394af1dd7ffSMike Christie if (ret < 0) { 1395af1dd7ffSMike Christie pr_debug("cmd %u on dev %s failed with %u\n", 1396af1dd7ffSMike Christie tcmu_cmd->cmd_id, udev->name, scsi_ret); 1397af1dd7ffSMike Christie 1398af1dd7ffSMike Christie idr_remove(&udev->commands, tcmu_cmd->cmd_id); 1399af1dd7ffSMike Christie /* 1400af1dd7ffSMike Christie * Ignore scsi_ret for now. target_complete_cmd 1401af1dd7ffSMike Christie * drops it. 1402af1dd7ffSMike Christie */ 1403af1dd7ffSMike Christie target_complete_cmd(tcmu_cmd->se_cmd, 1404af1dd7ffSMike Christie SAM_STAT_CHECK_CONDITION); 1405af1dd7ffSMike Christie tcmu_free_cmd(tcmu_cmd); 1406af1dd7ffSMike Christie } else if (ret > 0) { 1407af1dd7ffSMike Christie pr_debug("ran out of space during cmdr queue run\n"); 1408af1dd7ffSMike Christie /* 1409af1dd7ffSMike Christie * cmd was requeued, so just put all cmds back in 1410af1dd7ffSMike Christie * the queue 1411af1dd7ffSMike Christie */ 1412af1dd7ffSMike Christie list_splice_tail(&cmds, &udev->cmdr_queue); 1413af1dd7ffSMike Christie drained = false; 1414af1dd7ffSMike Christie goto done; 1415af1dd7ffSMike Christie } 1416af1dd7ffSMike Christie } 14179103575aSMike Christie if (list_empty(&udev->cmdr_queue)) 14189103575aSMike Christie del_timer(&udev->qfull_timer); 1419af1dd7ffSMike Christie done: 1420af1dd7ffSMike Christie return drained; 1421af1dd7ffSMike Christie } 1422af1dd7ffSMike Christie 14237c9e7a6fSAndy Grover static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on) 14247c9e7a6fSAndy Grover { 1425af1dd7ffSMike Christie struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 14267c9e7a6fSAndy Grover 1427af1dd7ffSMike Christie mutex_lock(&udev->cmdr_lock); 1428af1dd7ffSMike Christie tcmu_handle_completions(udev); 1429892782caSMike Christie run_cmdr_queue(udev, false); 1430af1dd7ffSMike Christie mutex_unlock(&udev->cmdr_lock); 14317c9e7a6fSAndy Grover 14327c9e7a6fSAndy Grover return 0; 14337c9e7a6fSAndy Grover } 14347c9e7a6fSAndy Grover 14357c9e7a6fSAndy Grover /* 14367c9e7a6fSAndy Grover * mmap code from uio.c. Copied here because we want to hook mmap() 14377c9e7a6fSAndy Grover * and this stuff must come along. 14387c9e7a6fSAndy Grover */ 14397c9e7a6fSAndy Grover static int tcmu_find_mem_index(struct vm_area_struct *vma) 14407c9e7a6fSAndy Grover { 14417c9e7a6fSAndy Grover struct tcmu_dev *udev = vma->vm_private_data; 14427c9e7a6fSAndy Grover struct uio_info *info = &udev->uio_info; 14437c9e7a6fSAndy Grover 14447c9e7a6fSAndy Grover if (vma->vm_pgoff < MAX_UIO_MAPS) { 14457c9e7a6fSAndy Grover if (info->mem[vma->vm_pgoff].size == 0) 14467c9e7a6fSAndy Grover return -1; 14477c9e7a6fSAndy Grover return (int)vma->vm_pgoff; 14487c9e7a6fSAndy Grover } 14497c9e7a6fSAndy Grover return -1; 14507c9e7a6fSAndy Grover } 14517c9e7a6fSAndy Grover 1452b6df4b79SXiubo Li static struct page *tcmu_try_get_block_page(struct tcmu_dev *udev, uint32_t dbi) 1453b6df4b79SXiubo Li { 1454b6df4b79SXiubo Li struct page *page; 1455b6df4b79SXiubo Li 1456b6df4b79SXiubo Li mutex_lock(&udev->cmdr_lock); 1457b6df4b79SXiubo Li page = tcmu_get_block_page(udev, dbi); 1458b6df4b79SXiubo Li if (likely(page)) { 1459b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1460b6df4b79SXiubo Li return page; 1461b6df4b79SXiubo Li } 1462b6df4b79SXiubo Li 1463b6df4b79SXiubo Li /* 1464c1c390baSMike Christie * Userspace messed up and passed in a address not in the 1465c1c390baSMike Christie * data iov passed to it. 1466b6df4b79SXiubo Li */ 1467c1c390baSMike Christie pr_err("Invalid addr to data block mapping (dbi %u) on device %s\n", 1468c1c390baSMike Christie dbi, udev->name); 1469c1c390baSMike Christie page = NULL; 1470b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1471b6df4b79SXiubo Li 1472b6df4b79SXiubo Li return page; 1473b6df4b79SXiubo Li } 1474b6df4b79SXiubo Li 147569589c9bSSouptick Joarder static vm_fault_t tcmu_vma_fault(struct vm_fault *vmf) 14767c9e7a6fSAndy Grover { 147711bac800SDave Jiang struct tcmu_dev *udev = vmf->vma->vm_private_data; 14787c9e7a6fSAndy Grover struct uio_info *info = &udev->uio_info; 14797c9e7a6fSAndy Grover struct page *page; 14807c9e7a6fSAndy Grover unsigned long offset; 14817c9e7a6fSAndy Grover void *addr; 14827c9e7a6fSAndy Grover 148311bac800SDave Jiang int mi = tcmu_find_mem_index(vmf->vma); 14847c9e7a6fSAndy Grover if (mi < 0) 14857c9e7a6fSAndy Grover return VM_FAULT_SIGBUS; 14867c9e7a6fSAndy Grover 14877c9e7a6fSAndy Grover /* 14887c9e7a6fSAndy Grover * We need to subtract mi because userspace uses offset = N*PAGE_SIZE 14897c9e7a6fSAndy Grover * to use mem[N]. 14907c9e7a6fSAndy Grover */ 14917c9e7a6fSAndy Grover offset = (vmf->pgoff - mi) << PAGE_SHIFT; 14927c9e7a6fSAndy Grover 1493141685a3SXiubo Li if (offset < udev->data_off) { 1494141685a3SXiubo Li /* For the vmalloc()ed cmd area pages */ 14957c9e7a6fSAndy Grover addr = (void *)(unsigned long)info->mem[mi].addr + offset; 14967c9e7a6fSAndy Grover page = vmalloc_to_page(addr); 1497141685a3SXiubo Li } else { 1498141685a3SXiubo Li uint32_t dbi; 1499141685a3SXiubo Li 1500b6df4b79SXiubo Li /* For the dynamically growing data area pages */ 1501141685a3SXiubo Li dbi = (offset - udev->data_off) / DATA_BLOCK_SIZE; 1502b6df4b79SXiubo Li page = tcmu_try_get_block_page(udev, dbi); 1503b6df4b79SXiubo Li if (!page) 1504c1c390baSMike Christie return VM_FAULT_SIGBUS; 1505141685a3SXiubo Li } 1506141685a3SXiubo Li 15077c9e7a6fSAndy Grover get_page(page); 15087c9e7a6fSAndy Grover vmf->page = page; 15097c9e7a6fSAndy Grover return 0; 15107c9e7a6fSAndy Grover } 15117c9e7a6fSAndy Grover 15127c9e7a6fSAndy Grover static const struct vm_operations_struct tcmu_vm_ops = { 15137c9e7a6fSAndy Grover .fault = tcmu_vma_fault, 15147c9e7a6fSAndy Grover }; 15157c9e7a6fSAndy Grover 15167c9e7a6fSAndy Grover static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma) 15177c9e7a6fSAndy Grover { 15187c9e7a6fSAndy Grover struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 15197c9e7a6fSAndy Grover 15207c9e7a6fSAndy Grover vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 15217c9e7a6fSAndy Grover vma->vm_ops = &tcmu_vm_ops; 15227c9e7a6fSAndy Grover 15237c9e7a6fSAndy Grover vma->vm_private_data = udev; 15247c9e7a6fSAndy Grover 15257c9e7a6fSAndy Grover /* Ensure the mmap is exactly the right size */ 152680eb8761SMike Christie if (vma_pages(vma) != (udev->ring_size >> PAGE_SHIFT)) 15277c9e7a6fSAndy Grover return -EINVAL; 15287c9e7a6fSAndy Grover 15297c9e7a6fSAndy Grover return 0; 15307c9e7a6fSAndy Grover } 15317c9e7a6fSAndy Grover 15327c9e7a6fSAndy Grover static int tcmu_open(struct uio_info *info, struct inode *inode) 15337c9e7a6fSAndy Grover { 15347c9e7a6fSAndy Grover struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 15357c9e7a6fSAndy Grover 15367c9e7a6fSAndy Grover /* O_EXCL not supported for char devs, so fake it? */ 15377c9e7a6fSAndy Grover if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags)) 15387c9e7a6fSAndy Grover return -EBUSY; 15397c9e7a6fSAndy Grover 1540b6df4b79SXiubo Li udev->inode = inode; 15419260695dSMike Christie kref_get(&udev->kref); 1542b6df4b79SXiubo Li 15437c9e7a6fSAndy Grover pr_debug("open\n"); 15447c9e7a6fSAndy Grover 15457c9e7a6fSAndy Grover return 0; 15467c9e7a6fSAndy Grover } 15477c9e7a6fSAndy Grover 1548f3cdbe39SMike Christie static void tcmu_dev_call_rcu(struct rcu_head *p) 1549f3cdbe39SMike Christie { 1550f3cdbe39SMike Christie struct se_device *dev = container_of(p, struct se_device, rcu_head); 1551f3cdbe39SMike Christie struct tcmu_dev *udev = TCMU_DEV(dev); 1552f3cdbe39SMike Christie 1553f3cdbe39SMike Christie kfree(udev->uio_info.name); 1554f3cdbe39SMike Christie kfree(udev->name); 1555f3cdbe39SMike Christie kfree(udev); 1556f3cdbe39SMike Christie } 1557f3cdbe39SMike Christie 1558c22adc0bSXiubo Li static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd) 1559c22adc0bSXiubo Li { 1560c22adc0bSXiubo Li if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { 1561c22adc0bSXiubo Li kmem_cache_free(tcmu_cmd_cache, cmd); 1562c22adc0bSXiubo Li return 0; 1563c22adc0bSXiubo Li } 1564c22adc0bSXiubo Li return -EINVAL; 1565c22adc0bSXiubo Li } 1566c22adc0bSXiubo Li 1567bf99ec13SMike Christie static void tcmu_blocks_release(struct radix_tree_root *blocks, 1568bf99ec13SMike Christie int start, int end) 1569c22adc0bSXiubo Li { 1570c22adc0bSXiubo Li int i; 1571c22adc0bSXiubo Li struct page *page; 1572c22adc0bSXiubo Li 1573bf99ec13SMike Christie for (i = start; i < end; i++) { 1574bf99ec13SMike Christie page = radix_tree_delete(blocks, i); 1575c22adc0bSXiubo Li if (page) { 1576c22adc0bSXiubo Li __free_page(page); 1577c22adc0bSXiubo Li atomic_dec(&global_db_count); 1578c22adc0bSXiubo Li } 1579c22adc0bSXiubo Li } 1580c22adc0bSXiubo Li } 1581c22adc0bSXiubo Li 1582f3cdbe39SMike Christie static void tcmu_dev_kref_release(struct kref *kref) 1583f3cdbe39SMike Christie { 1584f3cdbe39SMike Christie struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref); 1585f3cdbe39SMike Christie struct se_device *dev = &udev->se_dev; 1586c22adc0bSXiubo Li struct tcmu_cmd *cmd; 1587c22adc0bSXiubo Li bool all_expired = true; 1588c22adc0bSXiubo Li int i; 1589c22adc0bSXiubo Li 1590c22adc0bSXiubo Li vfree(udev->mb_addr); 1591c22adc0bSXiubo Li udev->mb_addr = NULL; 1592c22adc0bSXiubo Li 1593488ebe4cSMike Christie spin_lock_bh(&timed_out_udevs_lock); 1594488ebe4cSMike Christie if (!list_empty(&udev->timedout_entry)) 1595488ebe4cSMike Christie list_del(&udev->timedout_entry); 1596488ebe4cSMike Christie spin_unlock_bh(&timed_out_udevs_lock); 1597488ebe4cSMike Christie 1598c22adc0bSXiubo Li /* Upper layer should drain all requests before calling this */ 15996fddcb77SMike Christie mutex_lock(&udev->cmdr_lock); 1600c22adc0bSXiubo Li idr_for_each_entry(&udev->commands, cmd, i) { 1601c22adc0bSXiubo Li if (tcmu_check_and_free_pending_cmd(cmd) != 0) 1602c22adc0bSXiubo Li all_expired = false; 1603c22adc0bSXiubo Li } 1604c22adc0bSXiubo Li idr_destroy(&udev->commands); 1605c22adc0bSXiubo Li WARN_ON(!all_expired); 1606c22adc0bSXiubo Li 1607bf99ec13SMike Christie tcmu_blocks_release(&udev->data_blocks, 0, udev->dbi_max + 1); 160880eb8761SMike Christie kfree(udev->data_bitmap); 1609bf99ec13SMike Christie mutex_unlock(&udev->cmdr_lock); 1610f3cdbe39SMike Christie 1611f3cdbe39SMike Christie call_rcu(&dev->rcu_head, tcmu_dev_call_rcu); 1612f3cdbe39SMike Christie } 1613f3cdbe39SMike Christie 16147c9e7a6fSAndy Grover static int tcmu_release(struct uio_info *info, struct inode *inode) 16157c9e7a6fSAndy Grover { 16167c9e7a6fSAndy Grover struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 16177c9e7a6fSAndy Grover 16187c9e7a6fSAndy Grover clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags); 16197c9e7a6fSAndy Grover 16207c9e7a6fSAndy Grover pr_debug("close\n"); 16219260695dSMike Christie /* release ref from open */ 1622f3cdbe39SMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 16237c9e7a6fSAndy Grover return 0; 16247c9e7a6fSAndy Grover } 16257c9e7a6fSAndy Grover 16269de3a1efSMike Christie static int tcmu_init_genl_cmd_reply(struct tcmu_dev *udev, int cmd) 1627b3af66e2SMike Christie { 1628b3af66e2SMike Christie struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; 1629b3af66e2SMike Christie 1630b3af66e2SMike Christie if (!tcmu_kern_cmd_reply_supported) 16319de3a1efSMike Christie return 0; 1632b849b456SKenjiro Nakayama 1633b849b456SKenjiro Nakayama if (udev->nl_reply_supported <= 0) 16349de3a1efSMike Christie return 0; 1635b849b456SKenjiro Nakayama 16363228691fSMike Christie mutex_lock(&tcmu_nl_cmd_mutex); 1637b3af66e2SMike Christie 1638bdaeedc1SMike Christie if (tcmu_netlink_blocked) { 1639bdaeedc1SMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 1640bdaeedc1SMike Christie pr_warn("Failing nl cmd %d on %s. Interface is blocked.\n", cmd, 1641bdaeedc1SMike Christie udev->name); 1642bdaeedc1SMike Christie return -EAGAIN; 1643bdaeedc1SMike Christie } 1644bdaeedc1SMike Christie 1645b3af66e2SMike Christie if (nl_cmd->cmd != TCMU_CMD_UNSPEC) { 16463228691fSMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 16479de3a1efSMike Christie pr_warn("netlink cmd %d already executing on %s\n", 16489de3a1efSMike Christie nl_cmd->cmd, udev->name); 16499de3a1efSMike Christie return -EBUSY; 1650b3af66e2SMike Christie } 1651b3af66e2SMike Christie 1652b3af66e2SMike Christie memset(nl_cmd, 0, sizeof(*nl_cmd)); 1653b3af66e2SMike Christie nl_cmd->cmd = cmd; 16543228691fSMike Christie nl_cmd->udev = udev; 1655b3af66e2SMike Christie init_completion(&nl_cmd->complete); 16563228691fSMike Christie INIT_LIST_HEAD(&nl_cmd->nl_list); 1657b3af66e2SMike Christie 16583228691fSMike Christie list_add_tail(&nl_cmd->nl_list, &tcmu_nl_cmd_list); 16593228691fSMike Christie 16603228691fSMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 16619de3a1efSMike Christie return 0; 1662b3af66e2SMike Christie } 1663b3af66e2SMike Christie 1664b3af66e2SMike Christie static int tcmu_wait_genl_cmd_reply(struct tcmu_dev *udev) 1665b3af66e2SMike Christie { 1666b3af66e2SMike Christie struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; 1667b3af66e2SMike Christie int ret; 1668b3af66e2SMike Christie 1669b3af66e2SMike Christie if (!tcmu_kern_cmd_reply_supported) 1670b3af66e2SMike Christie return 0; 1671b3af66e2SMike Christie 1672b849b456SKenjiro Nakayama if (udev->nl_reply_supported <= 0) 1673b849b456SKenjiro Nakayama return 0; 1674b849b456SKenjiro Nakayama 1675b3af66e2SMike Christie pr_debug("sleeping for nl reply\n"); 1676b3af66e2SMike Christie wait_for_completion(&nl_cmd->complete); 1677b3af66e2SMike Christie 16783228691fSMike Christie mutex_lock(&tcmu_nl_cmd_mutex); 1679b3af66e2SMike Christie nl_cmd->cmd = TCMU_CMD_UNSPEC; 1680b3af66e2SMike Christie ret = nl_cmd->status; 16813228691fSMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 1682b3af66e2SMike Christie 168385fae482SLuis de Bethencourt return ret; 1684b3af66e2SMike Christie } 1685b3af66e2SMike Christie 16860e5aee39SZhu Lingshan static int tcmu_netlink_event_init(struct tcmu_dev *udev, 16870e5aee39SZhu Lingshan enum tcmu_genl_cmd cmd, 16880e5aee39SZhu Lingshan struct sk_buff **buf, void **hdr) 16897c9e7a6fSAndy Grover { 16907c9e7a6fSAndy Grover struct sk_buff *skb; 16917c9e7a6fSAndy Grover void *msg_header; 16926e14eab9SNicholas Bellinger int ret = -ENOMEM; 16937c9e7a6fSAndy Grover 16947c9e7a6fSAndy Grover skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 16957c9e7a6fSAndy Grover if (!skb) 16966e14eab9SNicholas Bellinger return ret; 16977c9e7a6fSAndy Grover 16987c9e7a6fSAndy Grover msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd); 16996e14eab9SNicholas Bellinger if (!msg_header) 17006e14eab9SNicholas Bellinger goto free_skb; 17017c9e7a6fSAndy Grover 1702b3af66e2SMike Christie ret = nla_put_string(skb, TCMU_ATTR_DEVICE, udev->uio_info.name); 17036e14eab9SNicholas Bellinger if (ret < 0) 17046e14eab9SNicholas Bellinger goto free_skb; 17057c9e7a6fSAndy Grover 1706b3af66e2SMike Christie ret = nla_put_u32(skb, TCMU_ATTR_MINOR, udev->uio_info.uio_dev->minor); 1707b3af66e2SMike Christie if (ret < 0) 1708b3af66e2SMike Christie goto free_skb; 1709b3af66e2SMike Christie 1710b3af66e2SMike Christie ret = nla_put_u32(skb, TCMU_ATTR_DEVICE_ID, udev->se_dev.dev_index); 17116e14eab9SNicholas Bellinger if (ret < 0) 17126e14eab9SNicholas Bellinger goto free_skb; 17137c9e7a6fSAndy Grover 17140e5aee39SZhu Lingshan *buf = skb; 17150e5aee39SZhu Lingshan *hdr = msg_header; 17160e5aee39SZhu Lingshan return ret; 17170e5aee39SZhu Lingshan 17180e5aee39SZhu Lingshan free_skb: 17190e5aee39SZhu Lingshan nlmsg_free(skb); 17200e5aee39SZhu Lingshan return ret; 17212d76443eSMike Christie } 17222d76443eSMike Christie 17230e5aee39SZhu Lingshan static int tcmu_netlink_event_send(struct tcmu_dev *udev, 17240e5aee39SZhu Lingshan enum tcmu_genl_cmd cmd, 172506add777SMike Christie struct sk_buff *skb, void *msg_header) 17260e5aee39SZhu Lingshan { 172706add777SMike Christie int ret; 17288a45885cSBryant G. Ly 1729053c095aSJohannes Berg genlmsg_end(skb, msg_header); 17307c9e7a6fSAndy Grover 17319de3a1efSMike Christie ret = tcmu_init_genl_cmd_reply(udev, cmd); 17329de3a1efSMike Christie if (ret) { 17339de3a1efSMike Christie nlmsg_free(skb); 17349de3a1efSMike Christie return ret; 17359de3a1efSMike Christie } 1736b3af66e2SMike Christie 173720c08b36SSheng Yang ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0, 17387c9e7a6fSAndy Grover TCMU_MCGRP_CONFIG, GFP_KERNEL); 17397c9e7a6fSAndy Grover /* We don't care if no one is listening */ 17407c9e7a6fSAndy Grover if (ret == -ESRCH) 17417c9e7a6fSAndy Grover ret = 0; 1742b3af66e2SMike Christie if (!ret) 1743b3af66e2SMike Christie ret = tcmu_wait_genl_cmd_reply(udev); 17440e5aee39SZhu Lingshan return ret; 17450e5aee39SZhu Lingshan } 17467c9e7a6fSAndy Grover 1747e0c240acSZhu Lingshan static int tcmu_send_dev_add_event(struct tcmu_dev *udev) 1748e0c240acSZhu Lingshan { 1749e0c240acSZhu Lingshan struct sk_buff *skb = NULL; 1750e0c240acSZhu Lingshan void *msg_header = NULL; 1751e0c240acSZhu Lingshan int ret = 0; 1752e0c240acSZhu Lingshan 1753e0c240acSZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_ADDED_DEVICE, &skb, 1754e0c240acSZhu Lingshan &msg_header); 1755e0c240acSZhu Lingshan if (ret < 0) 17567c9e7a6fSAndy Grover return ret; 175706add777SMike Christie return tcmu_netlink_event_send(udev, TCMU_CMD_ADDED_DEVICE, skb, 175806add777SMike Christie msg_header); 1759e0c240acSZhu Lingshan } 1760e0c240acSZhu Lingshan 1761f892bd8eSZhu Lingshan static int tcmu_send_dev_remove_event(struct tcmu_dev *udev) 1762f892bd8eSZhu Lingshan { 1763f892bd8eSZhu Lingshan struct sk_buff *skb = NULL; 1764f892bd8eSZhu Lingshan void *msg_header = NULL; 1765f892bd8eSZhu Lingshan int ret = 0; 1766f892bd8eSZhu Lingshan 1767f892bd8eSZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_REMOVED_DEVICE, 1768f892bd8eSZhu Lingshan &skb, &msg_header); 1769f892bd8eSZhu Lingshan if (ret < 0) 17706e14eab9SNicholas Bellinger return ret; 1771f892bd8eSZhu Lingshan return tcmu_netlink_event_send(udev, TCMU_CMD_REMOVED_DEVICE, 177206add777SMike Christie skb, msg_header); 17737c9e7a6fSAndy Grover } 17747c9e7a6fSAndy Grover 1775de8c5221SBryant G. Ly static int tcmu_update_uio_info(struct tcmu_dev *udev) 17767c9e7a6fSAndy Grover { 17777c9e7a6fSAndy Grover struct tcmu_hba *hba = udev->hba->hba_ptr; 17787c9e7a6fSAndy Grover struct uio_info *info; 1779de8c5221SBryant G. Ly size_t size, used; 17807c9e7a6fSAndy Grover char *str; 17817c9e7a6fSAndy Grover 17827c9e7a6fSAndy Grover info = &udev->uio_info; 17837c9e7a6fSAndy Grover size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name, 17847c9e7a6fSAndy Grover udev->dev_config); 17857c9e7a6fSAndy Grover size += 1; /* for \0 */ 17867c9e7a6fSAndy Grover str = kmalloc(size, GFP_KERNEL); 17877c9e7a6fSAndy Grover if (!str) 17887c9e7a6fSAndy Grover return -ENOMEM; 17897c9e7a6fSAndy Grover 17907c9e7a6fSAndy Grover used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name); 17917c9e7a6fSAndy Grover if (udev->dev_config[0]) 17927c9e7a6fSAndy Grover snprintf(str + used, size - used, "/%s", udev->dev_config); 17937c9e7a6fSAndy Grover 1794ededd039SBryant G. Ly /* If the old string exists, free it */ 1795ededd039SBryant G. Ly kfree(info->name); 17967c9e7a6fSAndy Grover info->name = str; 17977c9e7a6fSAndy Grover 1798de8c5221SBryant G. Ly return 0; 1799de8c5221SBryant G. Ly } 1800de8c5221SBryant G. Ly 1801de8c5221SBryant G. Ly static int tcmu_configure_device(struct se_device *dev) 1802de8c5221SBryant G. Ly { 1803de8c5221SBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(dev); 1804de8c5221SBryant G. Ly struct uio_info *info; 1805de8c5221SBryant G. Ly struct tcmu_mailbox *mb; 1806de8c5221SBryant G. Ly int ret = 0; 1807de8c5221SBryant G. Ly 1808de8c5221SBryant G. Ly ret = tcmu_update_uio_info(udev); 1809de8c5221SBryant G. Ly if (ret) 1810de8c5221SBryant G. Ly return ret; 1811de8c5221SBryant G. Ly 1812de8c5221SBryant G. Ly info = &udev->uio_info; 1813de8c5221SBryant G. Ly 18146396bb22SKees Cook udev->data_bitmap = kcalloc(BITS_TO_LONGS(udev->max_blocks), 18156396bb22SKees Cook sizeof(unsigned long), 18166396bb22SKees Cook GFP_KERNEL); 1817a24e7917SWei Yongjun if (!udev->data_bitmap) { 1818a24e7917SWei Yongjun ret = -ENOMEM; 181980eb8761SMike Christie goto err_bitmap_alloc; 1820a24e7917SWei Yongjun } 182180eb8761SMike Christie 1822141685a3SXiubo Li udev->mb_addr = vzalloc(CMDR_SIZE); 18237c9e7a6fSAndy Grover if (!udev->mb_addr) { 18247c9e7a6fSAndy Grover ret = -ENOMEM; 18257c9e7a6fSAndy Grover goto err_vzalloc; 18267c9e7a6fSAndy Grover } 18277c9e7a6fSAndy Grover 18287c9e7a6fSAndy Grover /* mailbox fits in first part of CMDR space */ 18297c9e7a6fSAndy Grover udev->cmdr_size = CMDR_SIZE - CMDR_OFF; 18307c9e7a6fSAndy Grover udev->data_off = CMDR_SIZE; 183180eb8761SMike Christie udev->data_size = udev->max_blocks * DATA_BLOCK_SIZE; 1832b6df4b79SXiubo Li udev->dbi_thresh = 0; /* Default in Idle state */ 18337c9e7a6fSAndy Grover 1834141685a3SXiubo Li /* Initialise the mailbox of the ring buffer */ 18357c9e7a6fSAndy Grover mb = udev->mb_addr; 18360ad46af8SAndy Grover mb->version = TCMU_MAILBOX_VERSION; 183732c76de3SSheng Yang mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC; 18387c9e7a6fSAndy Grover mb->cmdr_off = CMDR_OFF; 18397c9e7a6fSAndy Grover mb->cmdr_size = udev->cmdr_size; 18407c9e7a6fSAndy Grover 18417c9e7a6fSAndy Grover WARN_ON(!PAGE_ALIGNED(udev->data_off)); 18427c9e7a6fSAndy Grover WARN_ON(udev->data_size % PAGE_SIZE); 184326418649SSheng Yang WARN_ON(udev->data_size % DATA_BLOCK_SIZE); 18447c9e7a6fSAndy Grover 1845ac64a2ceSDavid Disseldorp info->version = __stringify(TCMU_MAILBOX_VERSION); 18467c9e7a6fSAndy Grover 18477c9e7a6fSAndy Grover info->mem[0].name = "tcm-user command & data buffer"; 18480633e123SArnd Bergmann info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr; 184980eb8761SMike Christie info->mem[0].size = udev->ring_size = udev->data_size + CMDR_SIZE; 1850141685a3SXiubo Li info->mem[0].memtype = UIO_MEM_NONE; 18517c9e7a6fSAndy Grover 18527c9e7a6fSAndy Grover info->irqcontrol = tcmu_irqcontrol; 18537c9e7a6fSAndy Grover info->irq = UIO_IRQ_CUSTOM; 18547c9e7a6fSAndy Grover 18557c9e7a6fSAndy Grover info->mmap = tcmu_mmap; 18567c9e7a6fSAndy Grover info->open = tcmu_open; 18577c9e7a6fSAndy Grover info->release = tcmu_release; 18587c9e7a6fSAndy Grover 18597c9e7a6fSAndy Grover ret = uio_register_device(tcmu_root_device, info); 18607c9e7a6fSAndy Grover if (ret) 18617c9e7a6fSAndy Grover goto err_register; 18627c9e7a6fSAndy Grover 186381ee28deSSheng Yang /* User can set hw_block_size before enable the device */ 186481ee28deSSheng Yang if (dev->dev_attrib.hw_block_size == 0) 18657c9e7a6fSAndy Grover dev->dev_attrib.hw_block_size = 512; 186681ee28deSSheng Yang /* Other attributes can be configured in userspace */ 18673abaa2bfSMike Christie if (!dev->dev_attrib.hw_max_sectors) 18687c9e7a6fSAndy Grover dev->dev_attrib.hw_max_sectors = 128; 18699a8bb606SBryant G. Ly if (!dev->dev_attrib.emulate_write_cache) 18709a8bb606SBryant G. Ly dev->dev_attrib.emulate_write_cache = 0; 18717c9e7a6fSAndy Grover dev->dev_attrib.hw_queue_depth = 128; 18727c9e7a6fSAndy Grover 1873b849b456SKenjiro Nakayama /* If user didn't explicitly disable netlink reply support, use 1874b849b456SKenjiro Nakayama * module scope setting. 1875b849b456SKenjiro Nakayama */ 1876b849b456SKenjiro Nakayama if (udev->nl_reply_supported >= 0) 1877b849b456SKenjiro Nakayama udev->nl_reply_supported = tcmu_kern_cmd_reply_supported; 1878b849b456SKenjiro Nakayama 1879f3cdbe39SMike Christie /* 1880f3cdbe39SMike Christie * Get a ref incase userspace does a close on the uio device before 1881f3cdbe39SMike Christie * LIO has initiated tcmu_free_device. 1882f3cdbe39SMike Christie */ 1883f3cdbe39SMike Christie kref_get(&udev->kref); 1884f3cdbe39SMike Christie 1885e0c240acSZhu Lingshan ret = tcmu_send_dev_add_event(udev); 18867c9e7a6fSAndy Grover if (ret) 18877c9e7a6fSAndy Grover goto err_netlink; 18887c9e7a6fSAndy Grover 1889b6df4b79SXiubo Li mutex_lock(&root_udev_mutex); 1890b6df4b79SXiubo Li list_add(&udev->node, &root_udev); 1891b6df4b79SXiubo Li mutex_unlock(&root_udev_mutex); 1892b6df4b79SXiubo Li 18937c9e7a6fSAndy Grover return 0; 18947c9e7a6fSAndy Grover 18957c9e7a6fSAndy Grover err_netlink: 1896f3cdbe39SMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 18977c9e7a6fSAndy Grover uio_unregister_device(&udev->uio_info); 18987c9e7a6fSAndy Grover err_register: 18997c9e7a6fSAndy Grover vfree(udev->mb_addr); 1900c22adc0bSXiubo Li udev->mb_addr = NULL; 19017c9e7a6fSAndy Grover err_vzalloc: 190280eb8761SMike Christie kfree(udev->data_bitmap); 190380eb8761SMike Christie udev->data_bitmap = NULL; 190480eb8761SMike Christie err_bitmap_alloc: 19057c9e7a6fSAndy Grover kfree(info->name); 1906f3cdbe39SMike Christie info->name = NULL; 19077c9e7a6fSAndy Grover 19087c9e7a6fSAndy Grover return ret; 19097c9e7a6fSAndy Grover } 19107c9e7a6fSAndy Grover 19117c9e7a6fSAndy Grover static void tcmu_free_device(struct se_device *dev) 19127c9e7a6fSAndy Grover { 19137c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 191492634706SMike Christie 191592634706SMike Christie /* release ref from init */ 191692634706SMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 191792634706SMike Christie } 191892634706SMike Christie 191992634706SMike Christie static void tcmu_destroy_device(struct se_device *dev) 192092634706SMike Christie { 192192634706SMike Christie struct tcmu_dev *udev = TCMU_DEV(dev); 19227c9e7a6fSAndy Grover 19239103575aSMike Christie del_timer_sync(&udev->cmd_timer); 19249103575aSMike Christie del_timer_sync(&udev->qfull_timer); 19257c9e7a6fSAndy Grover 1926b6df4b79SXiubo Li mutex_lock(&root_udev_mutex); 1927b6df4b79SXiubo Li list_del(&udev->node); 1928b6df4b79SXiubo Li mutex_unlock(&root_udev_mutex); 1929b6df4b79SXiubo Li 1930f892bd8eSZhu Lingshan tcmu_send_dev_remove_event(udev); 19317c9e7a6fSAndy Grover 19327c9e7a6fSAndy Grover uio_unregister_device(&udev->uio_info); 19339260695dSMike Christie 19349260695dSMike Christie /* release ref from configure */ 19359260695dSMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 19367c9e7a6fSAndy Grover } 19377c9e7a6fSAndy Grover 1938892782caSMike Christie static void tcmu_unblock_dev(struct tcmu_dev *udev) 1939892782caSMike Christie { 1940892782caSMike Christie mutex_lock(&udev->cmdr_lock); 1941892782caSMike Christie clear_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags); 1942892782caSMike Christie mutex_unlock(&udev->cmdr_lock); 1943892782caSMike Christie } 1944892782caSMike Christie 1945892782caSMike Christie static void tcmu_block_dev(struct tcmu_dev *udev) 1946892782caSMike Christie { 1947892782caSMike Christie mutex_lock(&udev->cmdr_lock); 1948892782caSMike Christie 1949892782caSMike Christie if (test_and_set_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) 1950892782caSMike Christie goto unlock; 1951892782caSMike Christie 1952892782caSMike Christie /* complete IO that has executed successfully */ 1953892782caSMike Christie tcmu_handle_completions(udev); 1954892782caSMike Christie /* fail IO waiting to be queued */ 1955892782caSMike Christie run_cmdr_queue(udev, true); 1956892782caSMike Christie 1957892782caSMike Christie unlock: 1958892782caSMike Christie mutex_unlock(&udev->cmdr_lock); 1959892782caSMike Christie } 1960892782caSMike Christie 1961892782caSMike Christie static void tcmu_reset_ring(struct tcmu_dev *udev, u8 err_level) 1962892782caSMike Christie { 1963892782caSMike Christie struct tcmu_mailbox *mb; 1964892782caSMike Christie struct tcmu_cmd *cmd; 1965892782caSMike Christie int i; 1966892782caSMike Christie 1967892782caSMike Christie mutex_lock(&udev->cmdr_lock); 1968892782caSMike Christie 1969892782caSMike Christie idr_for_each_entry(&udev->commands, cmd, i) { 1970892782caSMike Christie if (!list_empty(&cmd->cmdr_queue_entry)) 1971892782caSMike Christie continue; 1972892782caSMike Christie 1973892782caSMike Christie pr_debug("removing cmd %u on dev %s from ring (is expired %d)\n", 1974892782caSMike Christie cmd->cmd_id, udev->name, 1975892782caSMike Christie test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)); 1976892782caSMike Christie 1977892782caSMike Christie idr_remove(&udev->commands, i); 1978892782caSMike Christie if (!test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { 1979892782caSMike Christie if (err_level == 1) { 1980892782caSMike Christie /* 1981892782caSMike Christie * Userspace was not able to start the 1982892782caSMike Christie * command or it is retryable. 1983892782caSMike Christie */ 1984892782caSMike Christie target_complete_cmd(cmd->se_cmd, SAM_STAT_BUSY); 1985892782caSMike Christie } else { 1986892782caSMike Christie /* hard failure */ 1987892782caSMike Christie target_complete_cmd(cmd->se_cmd, 1988892782caSMike Christie SAM_STAT_CHECK_CONDITION); 1989892782caSMike Christie } 1990892782caSMike Christie } 1991892782caSMike Christie tcmu_cmd_free_data(cmd, cmd->dbi_cnt); 1992892782caSMike Christie tcmu_free_cmd(cmd); 1993892782caSMike Christie } 1994892782caSMike Christie 1995892782caSMike Christie mb = udev->mb_addr; 1996892782caSMike Christie tcmu_flush_dcache_range(mb, sizeof(*mb)); 1997892782caSMike Christie pr_debug("mb last %u head %u tail %u\n", udev->cmdr_last_cleaned, 1998892782caSMike Christie mb->cmd_tail, mb->cmd_head); 1999892782caSMike Christie 2000892782caSMike Christie udev->cmdr_last_cleaned = 0; 2001892782caSMike Christie mb->cmd_tail = 0; 2002892782caSMike Christie mb->cmd_head = 0; 2003892782caSMike Christie tcmu_flush_dcache_range(mb, sizeof(*mb)); 2004892782caSMike Christie 2005892782caSMike Christie del_timer(&udev->cmd_timer); 2006892782caSMike Christie 2007892782caSMike Christie mutex_unlock(&udev->cmdr_lock); 2008892782caSMike Christie } 2009892782caSMike Christie 20107c9e7a6fSAndy Grover enum { 20113abaa2bfSMike Christie Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors, 201280eb8761SMike Christie Opt_nl_reply_supported, Opt_max_data_area_mb, Opt_err, 20137c9e7a6fSAndy Grover }; 20147c9e7a6fSAndy Grover 20157c9e7a6fSAndy Grover static match_table_t tokens = { 20167c9e7a6fSAndy Grover {Opt_dev_config, "dev_config=%s"}, 20177c9e7a6fSAndy Grover {Opt_dev_size, "dev_size=%u"}, 20189c1cd1b6SAndy Grover {Opt_hw_block_size, "hw_block_size=%u"}, 20193abaa2bfSMike Christie {Opt_hw_max_sectors, "hw_max_sectors=%u"}, 2020b849b456SKenjiro Nakayama {Opt_nl_reply_supported, "nl_reply_supported=%d"}, 202180eb8761SMike Christie {Opt_max_data_area_mb, "max_data_area_mb=%u"}, 20227c9e7a6fSAndy Grover {Opt_err, NULL} 20237c9e7a6fSAndy Grover }; 20247c9e7a6fSAndy Grover 20253abaa2bfSMike Christie static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib) 20263abaa2bfSMike Christie { 20273abaa2bfSMike Christie unsigned long tmp_ul; 20283abaa2bfSMike Christie char *arg_p; 20293abaa2bfSMike Christie int ret; 20303abaa2bfSMike Christie 20313abaa2bfSMike Christie arg_p = match_strdup(arg); 20323abaa2bfSMike Christie if (!arg_p) 20333abaa2bfSMike Christie return -ENOMEM; 20343abaa2bfSMike Christie 20353abaa2bfSMike Christie ret = kstrtoul(arg_p, 0, &tmp_ul); 20363abaa2bfSMike Christie kfree(arg_p); 20373abaa2bfSMike Christie if (ret < 0) { 20383abaa2bfSMike Christie pr_err("kstrtoul() failed for dev attrib\n"); 20393abaa2bfSMike Christie return ret; 20403abaa2bfSMike Christie } 20413abaa2bfSMike Christie if (!tmp_ul) { 20423abaa2bfSMike Christie pr_err("dev attrib must be nonzero\n"); 20433abaa2bfSMike Christie return -EINVAL; 20443abaa2bfSMike Christie } 20453abaa2bfSMike Christie *dev_attrib = tmp_ul; 20463abaa2bfSMike Christie return 0; 20473abaa2bfSMike Christie } 20483abaa2bfSMike Christie 20497c9e7a6fSAndy Grover static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev, 20507c9e7a6fSAndy Grover const char *page, ssize_t count) 20517c9e7a6fSAndy Grover { 20527c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 20537c9e7a6fSAndy Grover char *orig, *ptr, *opts, *arg_p; 20547c9e7a6fSAndy Grover substring_t args[MAX_OPT_ARGS]; 205580eb8761SMike Christie int ret = 0, token, tmpval; 20567c9e7a6fSAndy Grover 20577c9e7a6fSAndy Grover opts = kstrdup(page, GFP_KERNEL); 20587c9e7a6fSAndy Grover if (!opts) 20597c9e7a6fSAndy Grover return -ENOMEM; 20607c9e7a6fSAndy Grover 20617c9e7a6fSAndy Grover orig = opts; 20627c9e7a6fSAndy Grover 20637c9e7a6fSAndy Grover while ((ptr = strsep(&opts, ",\n")) != NULL) { 20647c9e7a6fSAndy Grover if (!*ptr) 20657c9e7a6fSAndy Grover continue; 20667c9e7a6fSAndy Grover 20677c9e7a6fSAndy Grover token = match_token(ptr, tokens, args); 20687c9e7a6fSAndy Grover switch (token) { 20697c9e7a6fSAndy Grover case Opt_dev_config: 20707c9e7a6fSAndy Grover if (match_strlcpy(udev->dev_config, &args[0], 20717c9e7a6fSAndy Grover TCMU_CONFIG_LEN) == 0) { 20727c9e7a6fSAndy Grover ret = -EINVAL; 20737c9e7a6fSAndy Grover break; 20747c9e7a6fSAndy Grover } 20757c9e7a6fSAndy Grover pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config); 20767c9e7a6fSAndy Grover break; 20777c9e7a6fSAndy Grover case Opt_dev_size: 20787c9e7a6fSAndy Grover arg_p = match_strdup(&args[0]); 20797c9e7a6fSAndy Grover if (!arg_p) { 20807c9e7a6fSAndy Grover ret = -ENOMEM; 20817c9e7a6fSAndy Grover break; 20827c9e7a6fSAndy Grover } 20837c9e7a6fSAndy Grover ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size); 20847c9e7a6fSAndy Grover kfree(arg_p); 20857c9e7a6fSAndy Grover if (ret < 0) 20867c9e7a6fSAndy Grover pr_err("kstrtoul() failed for dev_size=\n"); 20877c9e7a6fSAndy Grover break; 20889c1cd1b6SAndy Grover case Opt_hw_block_size: 20893abaa2bfSMike Christie ret = tcmu_set_dev_attrib(&args[0], 20903abaa2bfSMike Christie &(dev->dev_attrib.hw_block_size)); 20919c1cd1b6SAndy Grover break; 20923abaa2bfSMike Christie case Opt_hw_max_sectors: 20933abaa2bfSMike Christie ret = tcmu_set_dev_attrib(&args[0], 20943abaa2bfSMike Christie &(dev->dev_attrib.hw_max_sectors)); 20959c1cd1b6SAndy Grover break; 2096b849b456SKenjiro Nakayama case Opt_nl_reply_supported: 2097b849b456SKenjiro Nakayama arg_p = match_strdup(&args[0]); 2098b849b456SKenjiro Nakayama if (!arg_p) { 2099b849b456SKenjiro Nakayama ret = -ENOMEM; 2100b849b456SKenjiro Nakayama break; 2101b849b456SKenjiro Nakayama } 210216b93277SDan Carpenter ret = kstrtoint(arg_p, 0, &udev->nl_reply_supported); 2103b849b456SKenjiro Nakayama kfree(arg_p); 2104b849b456SKenjiro Nakayama if (ret < 0) 210516b93277SDan Carpenter pr_err("kstrtoint() failed for nl_reply_supported=\n"); 2106b849b456SKenjiro Nakayama break; 210780eb8761SMike Christie case Opt_max_data_area_mb: 210880eb8761SMike Christie if (dev->export_count) { 210980eb8761SMike Christie pr_err("Unable to set max_data_area_mb while exports exist\n"); 211080eb8761SMike Christie ret = -EINVAL; 211180eb8761SMike Christie break; 211280eb8761SMike Christie } 211380eb8761SMike Christie 211480eb8761SMike Christie arg_p = match_strdup(&args[0]); 211580eb8761SMike Christie if (!arg_p) { 211680eb8761SMike Christie ret = -ENOMEM; 211780eb8761SMike Christie break; 211880eb8761SMike Christie } 211980eb8761SMike Christie ret = kstrtoint(arg_p, 0, &tmpval); 212080eb8761SMike Christie kfree(arg_p); 212180eb8761SMike Christie if (ret < 0) { 212280eb8761SMike Christie pr_err("kstrtoint() failed for max_data_area_mb=\n"); 212380eb8761SMike Christie break; 212480eb8761SMike Christie } 212580eb8761SMike Christie 212680eb8761SMike Christie if (tmpval <= 0) { 212780eb8761SMike Christie pr_err("Invalid max_data_area %d\n", tmpval); 212880eb8761SMike Christie ret = -EINVAL; 212980eb8761SMike Christie break; 213080eb8761SMike Christie } 213180eb8761SMike Christie 213280eb8761SMike Christie udev->max_blocks = TCMU_MBS_TO_BLOCKS(tmpval); 213380eb8761SMike Christie if (udev->max_blocks > tcmu_global_max_blocks) { 213480eb8761SMike Christie pr_err("%d is too large. Adjusting max_data_area_mb to global limit of %u\n", 213580eb8761SMike Christie tmpval, 213680eb8761SMike Christie TCMU_BLOCKS_TO_MBS(tcmu_global_max_blocks)); 213780eb8761SMike Christie udev->max_blocks = tcmu_global_max_blocks; 213880eb8761SMike Christie } 213980eb8761SMike Christie break; 21407c9e7a6fSAndy Grover default: 21417c9e7a6fSAndy Grover break; 21427c9e7a6fSAndy Grover } 21432579325cSMike Christie 21442579325cSMike Christie if (ret) 21452579325cSMike Christie break; 21467c9e7a6fSAndy Grover } 21477c9e7a6fSAndy Grover 21487c9e7a6fSAndy Grover kfree(orig); 21497c9e7a6fSAndy Grover return (!ret) ? count : ret; 21507c9e7a6fSAndy Grover } 21517c9e7a6fSAndy Grover 21527c9e7a6fSAndy Grover static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b) 21537c9e7a6fSAndy Grover { 21547c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 21557c9e7a6fSAndy Grover ssize_t bl = 0; 21567c9e7a6fSAndy Grover 21577c9e7a6fSAndy Grover bl = sprintf(b + bl, "Config: %s ", 21587c9e7a6fSAndy Grover udev->dev_config[0] ? udev->dev_config : "NULL"); 215980eb8761SMike Christie bl += sprintf(b + bl, "Size: %zu ", udev->dev_size); 216080eb8761SMike Christie bl += sprintf(b + bl, "MaxDataAreaMB: %u\n", 216180eb8761SMike Christie TCMU_BLOCKS_TO_MBS(udev->max_blocks)); 21627c9e7a6fSAndy Grover 21637c9e7a6fSAndy Grover return bl; 21647c9e7a6fSAndy Grover } 21657c9e7a6fSAndy Grover 21667c9e7a6fSAndy Grover static sector_t tcmu_get_blocks(struct se_device *dev) 21677c9e7a6fSAndy Grover { 21687c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 21697c9e7a6fSAndy Grover 21707c9e7a6fSAndy Grover return div_u64(udev->dev_size - dev->dev_attrib.block_size, 21717c9e7a6fSAndy Grover dev->dev_attrib.block_size); 21727c9e7a6fSAndy Grover } 21737c9e7a6fSAndy Grover 21747c9e7a6fSAndy Grover static sense_reason_t 21757c9e7a6fSAndy Grover tcmu_parse_cdb(struct se_cmd *cmd) 21767c9e7a6fSAndy Grover { 217702eb924fSAndy Grover return passthrough_parse_cdb(cmd, tcmu_queue_cmd); 21789c1cd1b6SAndy Grover } 21799c1cd1b6SAndy Grover 21807d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page) 21817d7a7435SNicholas Bellinger { 21827d7a7435SNicholas Bellinger struct se_dev_attrib *da = container_of(to_config_group(item), 21837d7a7435SNicholas Bellinger struct se_dev_attrib, da_group); 2184b5ab697cSKenjiro Nakayama struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 21857d7a7435SNicholas Bellinger 21867d7a7435SNicholas Bellinger return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC); 21877d7a7435SNicholas Bellinger } 21887d7a7435SNicholas Bellinger 21897d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page, 21907d7a7435SNicholas Bellinger size_t count) 21917d7a7435SNicholas Bellinger { 21927d7a7435SNicholas Bellinger struct se_dev_attrib *da = container_of(to_config_group(item), 21937d7a7435SNicholas Bellinger struct se_dev_attrib, da_group); 21947d7a7435SNicholas Bellinger struct tcmu_dev *udev = container_of(da->da_dev, 21957d7a7435SNicholas Bellinger struct tcmu_dev, se_dev); 21967d7a7435SNicholas Bellinger u32 val; 21977d7a7435SNicholas Bellinger int ret; 21987d7a7435SNicholas Bellinger 21997d7a7435SNicholas Bellinger if (da->da_dev->export_count) { 22007d7a7435SNicholas Bellinger pr_err("Unable to set tcmu cmd_time_out while exports exist\n"); 22017d7a7435SNicholas Bellinger return -EINVAL; 22027d7a7435SNicholas Bellinger } 22037d7a7435SNicholas Bellinger 22047d7a7435SNicholas Bellinger ret = kstrtou32(page, 0, &val); 22057d7a7435SNicholas Bellinger if (ret < 0) 22067d7a7435SNicholas Bellinger return ret; 22077d7a7435SNicholas Bellinger 22087d7a7435SNicholas Bellinger udev->cmd_time_out = val * MSEC_PER_SEC; 22097d7a7435SNicholas Bellinger return count; 22107d7a7435SNicholas Bellinger } 22117d7a7435SNicholas Bellinger CONFIGFS_ATTR(tcmu_, cmd_time_out); 22127d7a7435SNicholas Bellinger 22139103575aSMike Christie static ssize_t tcmu_qfull_time_out_show(struct config_item *item, char *page) 22149103575aSMike Christie { 22159103575aSMike Christie struct se_dev_attrib *da = container_of(to_config_group(item), 22169103575aSMike Christie struct se_dev_attrib, da_group); 22179103575aSMike Christie struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 22189103575aSMike Christie 22199103575aSMike Christie return snprintf(page, PAGE_SIZE, "%ld\n", udev->qfull_time_out <= 0 ? 22209103575aSMike Christie udev->qfull_time_out : 22219103575aSMike Christie udev->qfull_time_out / MSEC_PER_SEC); 22229103575aSMike Christie } 22239103575aSMike Christie 22249103575aSMike Christie static ssize_t tcmu_qfull_time_out_store(struct config_item *item, 22259103575aSMike Christie const char *page, size_t count) 22269103575aSMike Christie { 22279103575aSMike Christie struct se_dev_attrib *da = container_of(to_config_group(item), 22289103575aSMike Christie struct se_dev_attrib, da_group); 22299103575aSMike Christie struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 22309103575aSMike Christie s32 val; 22319103575aSMike Christie int ret; 22329103575aSMike Christie 22339103575aSMike Christie ret = kstrtos32(page, 0, &val); 22349103575aSMike Christie if (ret < 0) 22359103575aSMike Christie return ret; 22369103575aSMike Christie 22379103575aSMike Christie if (val >= 0) { 22389103575aSMike Christie udev->qfull_time_out = val * MSEC_PER_SEC; 2239125966dbSPrasanna Kumar Kalever } else if (val == -1) { 2240125966dbSPrasanna Kumar Kalever udev->qfull_time_out = val; 22419103575aSMike Christie } else { 22429103575aSMike Christie printk(KERN_ERR "Invalid qfull timeout value %d\n", val); 22439103575aSMike Christie return -EINVAL; 22449103575aSMike Christie } 22459103575aSMike Christie return count; 22469103575aSMike Christie } 22479103575aSMike Christie CONFIGFS_ATTR(tcmu_, qfull_time_out); 22489103575aSMike Christie 224980eb8761SMike Christie static ssize_t tcmu_max_data_area_mb_show(struct config_item *item, char *page) 225080eb8761SMike Christie { 225180eb8761SMike Christie struct se_dev_attrib *da = container_of(to_config_group(item), 225280eb8761SMike Christie struct se_dev_attrib, da_group); 225380eb8761SMike Christie struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 225480eb8761SMike Christie 225580eb8761SMike Christie return snprintf(page, PAGE_SIZE, "%u\n", 225680eb8761SMike Christie TCMU_BLOCKS_TO_MBS(udev->max_blocks)); 225780eb8761SMike Christie } 225880eb8761SMike Christie CONFIGFS_ATTR_RO(tcmu_, max_data_area_mb); 225980eb8761SMike Christie 22602d76443eSMike Christie static ssize_t tcmu_dev_config_show(struct config_item *item, char *page) 2261ee018252SBryant G. Ly { 2262ee018252SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 2263ee018252SBryant G. Ly struct se_dev_attrib, da_group); 2264ee018252SBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2265ee018252SBryant G. Ly 2266ee018252SBryant G. Ly return snprintf(page, PAGE_SIZE, "%s\n", udev->dev_config); 2267ee018252SBryant G. Ly } 2268ee018252SBryant G. Ly 226902ccfb54SZhu Lingshan static int tcmu_send_dev_config_event(struct tcmu_dev *udev, 227002ccfb54SZhu Lingshan const char *reconfig_data) 227102ccfb54SZhu Lingshan { 227202ccfb54SZhu Lingshan struct sk_buff *skb = NULL; 227302ccfb54SZhu Lingshan void *msg_header = NULL; 227402ccfb54SZhu Lingshan int ret = 0; 227502ccfb54SZhu Lingshan 227602ccfb54SZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE, 227702ccfb54SZhu Lingshan &skb, &msg_header); 227802ccfb54SZhu Lingshan if (ret < 0) 227902ccfb54SZhu Lingshan return ret; 228002ccfb54SZhu Lingshan ret = nla_put_string(skb, TCMU_ATTR_DEV_CFG, reconfig_data); 228102ccfb54SZhu Lingshan if (ret < 0) { 228202ccfb54SZhu Lingshan nlmsg_free(skb); 228302ccfb54SZhu Lingshan return ret; 228402ccfb54SZhu Lingshan } 228502ccfb54SZhu Lingshan return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE, 228606add777SMike Christie skb, msg_header); 228702ccfb54SZhu Lingshan } 228802ccfb54SZhu Lingshan 228902ccfb54SZhu Lingshan 22902d76443eSMike Christie static ssize_t tcmu_dev_config_store(struct config_item *item, const char *page, 2291ee018252SBryant G. Ly size_t count) 2292ee018252SBryant G. Ly { 2293ee018252SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 2294ee018252SBryant G. Ly struct se_dev_attrib, da_group); 2295ee018252SBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 22962d76443eSMike Christie int ret, len; 2297ee018252SBryant G. Ly 22982d76443eSMike Christie len = strlen(page); 22992d76443eSMike Christie if (!len || len > TCMU_CONFIG_LEN - 1) 2300ee018252SBryant G. Ly return -EINVAL; 2301ee018252SBryant G. Ly 2302ee018252SBryant G. Ly /* Check if device has been configured before */ 230363d5be0fSMike Christie if (target_dev_configured(&udev->se_dev)) { 230402ccfb54SZhu Lingshan ret = tcmu_send_dev_config_event(udev, page); 2305ee018252SBryant G. Ly if (ret) { 2306ee018252SBryant G. Ly pr_err("Unable to reconfigure device\n"); 2307ee018252SBryant G. Ly return ret; 2308ee018252SBryant G. Ly } 2309de8c5221SBryant G. Ly strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN); 2310de8c5221SBryant G. Ly 2311de8c5221SBryant G. Ly ret = tcmu_update_uio_info(udev); 2312de8c5221SBryant G. Ly if (ret) 2313de8c5221SBryant G. Ly return ret; 2314de8c5221SBryant G. Ly return count; 2315ee018252SBryant G. Ly } 23162d76443eSMike Christie strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN); 2317ee018252SBryant G. Ly 2318ee018252SBryant G. Ly return count; 2319ee018252SBryant G. Ly } 23202d76443eSMike Christie CONFIGFS_ATTR(tcmu_, dev_config); 2321ee018252SBryant G. Ly 2322801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_show(struct config_item *item, char *page) 2323801fc54dSBryant G. Ly { 2324801fc54dSBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 2325801fc54dSBryant G. Ly struct se_dev_attrib, da_group); 2326801fc54dSBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2327801fc54dSBryant G. Ly 2328801fc54dSBryant G. Ly return snprintf(page, PAGE_SIZE, "%zu\n", udev->dev_size); 2329801fc54dSBryant G. Ly } 2330801fc54dSBryant G. Ly 233184e28506SZhu Lingshan static int tcmu_send_dev_size_event(struct tcmu_dev *udev, u64 size) 233284e28506SZhu Lingshan { 233384e28506SZhu Lingshan struct sk_buff *skb = NULL; 233484e28506SZhu Lingshan void *msg_header = NULL; 233584e28506SZhu Lingshan int ret = 0; 233684e28506SZhu Lingshan 233784e28506SZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE, 233884e28506SZhu Lingshan &skb, &msg_header); 233984e28506SZhu Lingshan if (ret < 0) 234084e28506SZhu Lingshan return ret; 234184e28506SZhu Lingshan ret = nla_put_u64_64bit(skb, TCMU_ATTR_DEV_SIZE, 234284e28506SZhu Lingshan size, TCMU_ATTR_PAD); 234384e28506SZhu Lingshan if (ret < 0) { 234484e28506SZhu Lingshan nlmsg_free(skb); 234584e28506SZhu Lingshan return ret; 234684e28506SZhu Lingshan } 234784e28506SZhu Lingshan return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE, 234806add777SMike Christie skb, msg_header); 234984e28506SZhu Lingshan } 235084e28506SZhu Lingshan 2351801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page, 2352801fc54dSBryant G. Ly size_t count) 2353801fc54dSBryant G. Ly { 2354801fc54dSBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 2355801fc54dSBryant G. Ly struct se_dev_attrib, da_group); 2356801fc54dSBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 23572d76443eSMike Christie u64 val; 2358801fc54dSBryant G. Ly int ret; 2359801fc54dSBryant G. Ly 23602d76443eSMike Christie ret = kstrtou64(page, 0, &val); 2361801fc54dSBryant G. Ly if (ret < 0) 2362801fc54dSBryant G. Ly return ret; 2363801fc54dSBryant G. Ly 2364801fc54dSBryant G. Ly /* Check if device has been configured before */ 236563d5be0fSMike Christie if (target_dev_configured(&udev->se_dev)) { 236684e28506SZhu Lingshan ret = tcmu_send_dev_size_event(udev, val); 2367801fc54dSBryant G. Ly if (ret) { 2368801fc54dSBryant G. Ly pr_err("Unable to reconfigure device\n"); 2369801fc54dSBryant G. Ly return ret; 2370801fc54dSBryant G. Ly } 2371801fc54dSBryant G. Ly } 23722d76443eSMike Christie udev->dev_size = val; 2373801fc54dSBryant G. Ly return count; 2374801fc54dSBryant G. Ly } 2375801fc54dSBryant G. Ly CONFIGFS_ATTR(tcmu_, dev_size); 2376801fc54dSBryant G. Ly 2377b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_show(struct config_item *item, 2378b849b456SKenjiro Nakayama char *page) 2379b849b456SKenjiro Nakayama { 2380b849b456SKenjiro Nakayama struct se_dev_attrib *da = container_of(to_config_group(item), 2381b849b456SKenjiro Nakayama struct se_dev_attrib, da_group); 2382b849b456SKenjiro Nakayama struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2383b849b456SKenjiro Nakayama 2384b849b456SKenjiro Nakayama return snprintf(page, PAGE_SIZE, "%d\n", udev->nl_reply_supported); 2385b849b456SKenjiro Nakayama } 2386b849b456SKenjiro Nakayama 2387b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_store(struct config_item *item, 2388b849b456SKenjiro Nakayama const char *page, size_t count) 2389b849b456SKenjiro Nakayama { 2390b849b456SKenjiro Nakayama struct se_dev_attrib *da = container_of(to_config_group(item), 2391b849b456SKenjiro Nakayama struct se_dev_attrib, da_group); 2392b849b456SKenjiro Nakayama struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2393b849b456SKenjiro Nakayama s8 val; 2394b849b456SKenjiro Nakayama int ret; 2395b849b456SKenjiro Nakayama 2396b849b456SKenjiro Nakayama ret = kstrtos8(page, 0, &val); 2397b849b456SKenjiro Nakayama if (ret < 0) 2398b849b456SKenjiro Nakayama return ret; 2399b849b456SKenjiro Nakayama 2400b849b456SKenjiro Nakayama udev->nl_reply_supported = val; 2401b849b456SKenjiro Nakayama return count; 2402b849b456SKenjiro Nakayama } 2403b849b456SKenjiro Nakayama CONFIGFS_ATTR(tcmu_, nl_reply_supported); 2404b849b456SKenjiro Nakayama 24059a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_show(struct config_item *item, 24069a8bb606SBryant G. Ly char *page) 24079a8bb606SBryant G. Ly { 24089a8bb606SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 24099a8bb606SBryant G. Ly struct se_dev_attrib, da_group); 24109a8bb606SBryant G. Ly 24119a8bb606SBryant G. Ly return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache); 24129a8bb606SBryant G. Ly } 24139a8bb606SBryant G. Ly 241433d065ccSZhu Lingshan static int tcmu_send_emulate_write_cache(struct tcmu_dev *udev, u8 val) 241533d065ccSZhu Lingshan { 241633d065ccSZhu Lingshan struct sk_buff *skb = NULL; 241733d065ccSZhu Lingshan void *msg_header = NULL; 241833d065ccSZhu Lingshan int ret = 0; 241933d065ccSZhu Lingshan 242033d065ccSZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE, 242133d065ccSZhu Lingshan &skb, &msg_header); 242233d065ccSZhu Lingshan if (ret < 0) 242333d065ccSZhu Lingshan return ret; 242433d065ccSZhu Lingshan ret = nla_put_u8(skb, TCMU_ATTR_WRITECACHE, val); 242533d065ccSZhu Lingshan if (ret < 0) { 242633d065ccSZhu Lingshan nlmsg_free(skb); 242733d065ccSZhu Lingshan return ret; 242833d065ccSZhu Lingshan } 242933d065ccSZhu Lingshan return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE, 243006add777SMike Christie skb, msg_header); 243133d065ccSZhu Lingshan } 243233d065ccSZhu Lingshan 24339a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_store(struct config_item *item, 24349a8bb606SBryant G. Ly const char *page, size_t count) 24359a8bb606SBryant G. Ly { 24369a8bb606SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 24379a8bb606SBryant G. Ly struct se_dev_attrib, da_group); 24381068be7bSBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 24392d76443eSMike Christie u8 val; 24409a8bb606SBryant G. Ly int ret; 24419a8bb606SBryant G. Ly 24422d76443eSMike Christie ret = kstrtou8(page, 0, &val); 24439a8bb606SBryant G. Ly if (ret < 0) 24449a8bb606SBryant G. Ly return ret; 24459a8bb606SBryant G. Ly 24461068be7bSBryant G. Ly /* Check if device has been configured before */ 244763d5be0fSMike Christie if (target_dev_configured(&udev->se_dev)) { 244833d065ccSZhu Lingshan ret = tcmu_send_emulate_write_cache(udev, val); 24491068be7bSBryant G. Ly if (ret) { 24501068be7bSBryant G. Ly pr_err("Unable to reconfigure device\n"); 24511068be7bSBryant G. Ly return ret; 24521068be7bSBryant G. Ly } 24531068be7bSBryant G. Ly } 24542d76443eSMike Christie 24552d76443eSMike Christie da->emulate_write_cache = val; 24569a8bb606SBryant G. Ly return count; 24579a8bb606SBryant G. Ly } 24589a8bb606SBryant G. Ly CONFIGFS_ATTR(tcmu_, emulate_write_cache); 24599a8bb606SBryant G. Ly 2460892782caSMike Christie static ssize_t tcmu_block_dev_show(struct config_item *item, char *page) 2461892782caSMike Christie { 2462892782caSMike Christie struct se_device *se_dev = container_of(to_config_group(item), 2463892782caSMike Christie struct se_device, 2464892782caSMike Christie dev_action_group); 2465892782caSMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 2466892782caSMike Christie 2467892782caSMike Christie if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) 2468892782caSMike Christie return snprintf(page, PAGE_SIZE, "%s\n", "blocked"); 2469892782caSMike Christie else 2470892782caSMike Christie return snprintf(page, PAGE_SIZE, "%s\n", "unblocked"); 2471892782caSMike Christie } 2472892782caSMike Christie 2473892782caSMike Christie static ssize_t tcmu_block_dev_store(struct config_item *item, const char *page, 2474892782caSMike Christie size_t count) 2475892782caSMike Christie { 2476892782caSMike Christie struct se_device *se_dev = container_of(to_config_group(item), 2477892782caSMike Christie struct se_device, 2478892782caSMike Christie dev_action_group); 2479892782caSMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 2480892782caSMike Christie u8 val; 2481892782caSMike Christie int ret; 2482892782caSMike Christie 2483892782caSMike Christie ret = kstrtou8(page, 0, &val); 2484892782caSMike Christie if (ret < 0) 2485892782caSMike Christie return ret; 2486892782caSMike Christie 2487892782caSMike Christie if (val > 1) { 2488892782caSMike Christie pr_err("Invalid block value %d\n", val); 2489892782caSMike Christie return -EINVAL; 2490892782caSMike Christie } 2491892782caSMike Christie 2492892782caSMike Christie if (!val) 2493892782caSMike Christie tcmu_unblock_dev(udev); 2494892782caSMike Christie else 2495892782caSMike Christie tcmu_block_dev(udev); 2496892782caSMike Christie return count; 2497892782caSMike Christie } 2498892782caSMike Christie CONFIGFS_ATTR(tcmu_, block_dev); 2499892782caSMike Christie 2500892782caSMike Christie static ssize_t tcmu_reset_ring_store(struct config_item *item, const char *page, 2501892782caSMike Christie size_t count) 2502892782caSMike Christie { 2503892782caSMike Christie struct se_device *se_dev = container_of(to_config_group(item), 2504892782caSMike Christie struct se_device, 2505892782caSMike Christie dev_action_group); 2506892782caSMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 2507892782caSMike Christie u8 val; 2508892782caSMike Christie int ret; 2509892782caSMike Christie 2510892782caSMike Christie ret = kstrtou8(page, 0, &val); 2511892782caSMike Christie if (ret < 0) 2512892782caSMike Christie return ret; 2513892782caSMike Christie 2514892782caSMike Christie if (val != 1 && val != 2) { 2515892782caSMike Christie pr_err("Invalid reset ring value %d\n", val); 2516892782caSMike Christie return -EINVAL; 2517892782caSMike Christie } 2518892782caSMike Christie 2519892782caSMike Christie tcmu_reset_ring(udev, val); 2520892782caSMike Christie return count; 2521892782caSMike Christie } 2522892782caSMike Christie CONFIGFS_ATTR_WO(tcmu_, reset_ring); 2523892782caSMike Christie 25245821783bSColin Ian King static struct configfs_attribute *tcmu_attrib_attrs[] = { 2525801fc54dSBryant G. Ly &tcmu_attr_cmd_time_out, 25269103575aSMike Christie &tcmu_attr_qfull_time_out, 252780eb8761SMike Christie &tcmu_attr_max_data_area_mb, 25282d76443eSMike Christie &tcmu_attr_dev_config, 2529801fc54dSBryant G. Ly &tcmu_attr_dev_size, 2530801fc54dSBryant G. Ly &tcmu_attr_emulate_write_cache, 2531b849b456SKenjiro Nakayama &tcmu_attr_nl_reply_supported, 2532801fc54dSBryant G. Ly NULL, 2533801fc54dSBryant G. Ly }; 2534801fc54dSBryant G. Ly 25357d7a7435SNicholas Bellinger static struct configfs_attribute **tcmu_attrs; 25367d7a7435SNicholas Bellinger 2537892782caSMike Christie static struct configfs_attribute *tcmu_action_attrs[] = { 2538892782caSMike Christie &tcmu_attr_block_dev, 2539892782caSMike Christie &tcmu_attr_reset_ring, 2540892782caSMike Christie NULL, 2541892782caSMike Christie }; 2542892782caSMike Christie 25437d7a7435SNicholas Bellinger static struct target_backend_ops tcmu_ops = { 25447c9e7a6fSAndy Grover .name = "user", 25457c9e7a6fSAndy Grover .owner = THIS_MODULE, 2546a3541703SAndy Grover .transport_flags = TRANSPORT_FLAG_PASSTHROUGH, 25477c9e7a6fSAndy Grover .attach_hba = tcmu_attach_hba, 25487c9e7a6fSAndy Grover .detach_hba = tcmu_detach_hba, 25497c9e7a6fSAndy Grover .alloc_device = tcmu_alloc_device, 25507c9e7a6fSAndy Grover .configure_device = tcmu_configure_device, 255192634706SMike Christie .destroy_device = tcmu_destroy_device, 25527c9e7a6fSAndy Grover .free_device = tcmu_free_device, 25537c9e7a6fSAndy Grover .parse_cdb = tcmu_parse_cdb, 25547c9e7a6fSAndy Grover .set_configfs_dev_params = tcmu_set_configfs_dev_params, 25557c9e7a6fSAndy Grover .show_configfs_dev_params = tcmu_show_configfs_dev_params, 25567c9e7a6fSAndy Grover .get_device_type = sbc_get_device_type, 25577c9e7a6fSAndy Grover .get_blocks = tcmu_get_blocks, 2558892782caSMike Christie .tb_dev_action_attrs = tcmu_action_attrs, 25597c9e7a6fSAndy Grover }; 25607c9e7a6fSAndy Grover 256189ec9cfdSMike Christie static void find_free_blocks(void) 2562b6df4b79SXiubo Li { 2563b6df4b79SXiubo Li struct tcmu_dev *udev; 2564b6df4b79SXiubo Li loff_t off; 2565af1dd7ffSMike Christie u32 start, end, block, total_freed = 0; 2566af1dd7ffSMike Christie 256780eb8761SMike Christie if (atomic_read(&global_db_count) <= tcmu_global_max_blocks) 2568af1dd7ffSMike Christie return; 2569b6df4b79SXiubo Li 2570b6df4b79SXiubo Li mutex_lock(&root_udev_mutex); 2571b6df4b79SXiubo Li list_for_each_entry(udev, &root_udev, node) { 2572b6df4b79SXiubo Li mutex_lock(&udev->cmdr_lock); 2573b6df4b79SXiubo Li 2574b6df4b79SXiubo Li /* Try to complete the finished commands first */ 2575b6df4b79SXiubo Li tcmu_handle_completions(udev); 2576b6df4b79SXiubo Li 2577af1dd7ffSMike Christie /* Skip the udevs in idle */ 2578af1dd7ffSMike Christie if (!udev->dbi_thresh) { 2579b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 2580b6df4b79SXiubo Li continue; 2581b6df4b79SXiubo Li } 2582b6df4b79SXiubo Li 2583b6df4b79SXiubo Li end = udev->dbi_max + 1; 2584b6df4b79SXiubo Li block = find_last_bit(udev->data_bitmap, end); 2585b6df4b79SXiubo Li if (block == udev->dbi_max) { 2586b6df4b79SXiubo Li /* 2587af1dd7ffSMike Christie * The last bit is dbi_max, so it is not possible 2588af1dd7ffSMike Christie * reclaim any blocks. 2589b6df4b79SXiubo Li */ 2590b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 2591b6df4b79SXiubo Li continue; 2592b6df4b79SXiubo Li } else if (block == end) { 2593b6df4b79SXiubo Li /* The current udev will goto idle state */ 2594b6df4b79SXiubo Li udev->dbi_thresh = start = 0; 2595b6df4b79SXiubo Li udev->dbi_max = 0; 2596b6df4b79SXiubo Li } else { 2597b6df4b79SXiubo Li udev->dbi_thresh = start = block + 1; 2598b6df4b79SXiubo Li udev->dbi_max = block; 2599b6df4b79SXiubo Li } 2600b6df4b79SXiubo Li 2601b6df4b79SXiubo Li /* Here will truncate the data area from off */ 2602b6df4b79SXiubo Li off = udev->data_off + start * DATA_BLOCK_SIZE; 2603b6df4b79SXiubo Li unmap_mapping_range(udev->inode->i_mapping, off, 0, 1); 2604b6df4b79SXiubo Li 2605b6df4b79SXiubo Li /* Release the block pages */ 2606bf99ec13SMike Christie tcmu_blocks_release(&udev->data_blocks, start, end); 2607b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 2608af1dd7ffSMike Christie 2609af1dd7ffSMike Christie total_freed += end - start; 2610af1dd7ffSMike Christie pr_debug("Freed %u blocks (total %u) from %s.\n", end - start, 2611af1dd7ffSMike Christie total_freed, udev->name); 2612b6df4b79SXiubo Li } 261389ec9cfdSMike Christie mutex_unlock(&root_udev_mutex); 261489ec9cfdSMike Christie 261580eb8761SMike Christie if (atomic_read(&global_db_count) > tcmu_global_max_blocks) 2616af1dd7ffSMike Christie schedule_delayed_work(&tcmu_unmap_work, msecs_to_jiffies(5000)); 2617b6df4b79SXiubo Li } 2618b6df4b79SXiubo Li 2619488ebe4cSMike Christie static void check_timedout_devices(void) 2620488ebe4cSMike Christie { 2621488ebe4cSMike Christie struct tcmu_dev *udev, *tmp_dev; 2622488ebe4cSMike Christie LIST_HEAD(devs); 2623488ebe4cSMike Christie 2624488ebe4cSMike Christie spin_lock_bh(&timed_out_udevs_lock); 2625488ebe4cSMike Christie list_splice_init(&timed_out_udevs, &devs); 2626488ebe4cSMike Christie 2627488ebe4cSMike Christie list_for_each_entry_safe(udev, tmp_dev, &devs, timedout_entry) { 2628488ebe4cSMike Christie list_del_init(&udev->timedout_entry); 2629488ebe4cSMike Christie spin_unlock_bh(&timed_out_udevs_lock); 2630488ebe4cSMike Christie 26316fddcb77SMike Christie mutex_lock(&udev->cmdr_lock); 2632488ebe4cSMike Christie idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL); 26336fddcb77SMike Christie mutex_unlock(&udev->cmdr_lock); 2634488ebe4cSMike Christie 2635488ebe4cSMike Christie spin_lock_bh(&timed_out_udevs_lock); 2636488ebe4cSMike Christie } 2637488ebe4cSMike Christie 2638488ebe4cSMike Christie spin_unlock_bh(&timed_out_udevs_lock); 2639488ebe4cSMike Christie } 2640488ebe4cSMike Christie 26419972cebbSMike Christie static void tcmu_unmap_work_fn(struct work_struct *work) 264289ec9cfdSMike Christie { 2643488ebe4cSMike Christie check_timedout_devices(); 264489ec9cfdSMike Christie find_free_blocks(); 264589ec9cfdSMike Christie } 264689ec9cfdSMike Christie 26477c9e7a6fSAndy Grover static int __init tcmu_module_init(void) 26487c9e7a6fSAndy Grover { 2649801fc54dSBryant G. Ly int ret, i, k, len = 0; 26507c9e7a6fSAndy Grover 26517c9e7a6fSAndy Grover BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0); 26527c9e7a6fSAndy Grover 2653af1dd7ffSMike Christie INIT_DELAYED_WORK(&tcmu_unmap_work, tcmu_unmap_work_fn); 26549972cebbSMike Christie 26557c9e7a6fSAndy Grover tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache", 26567c9e7a6fSAndy Grover sizeof(struct tcmu_cmd), 26577c9e7a6fSAndy Grover __alignof__(struct tcmu_cmd), 26587c9e7a6fSAndy Grover 0, NULL); 26597c9e7a6fSAndy Grover if (!tcmu_cmd_cache) 26607c9e7a6fSAndy Grover return -ENOMEM; 26617c9e7a6fSAndy Grover 26627c9e7a6fSAndy Grover tcmu_root_device = root_device_register("tcm_user"); 26637c9e7a6fSAndy Grover if (IS_ERR(tcmu_root_device)) { 26647c9e7a6fSAndy Grover ret = PTR_ERR(tcmu_root_device); 26657c9e7a6fSAndy Grover goto out_free_cache; 26667c9e7a6fSAndy Grover } 26677c9e7a6fSAndy Grover 26687c9e7a6fSAndy Grover ret = genl_register_family(&tcmu_genl_family); 26697c9e7a6fSAndy Grover if (ret < 0) { 26707c9e7a6fSAndy Grover goto out_unreg_device; 26717c9e7a6fSAndy Grover } 26727c9e7a6fSAndy Grover 26737d7a7435SNicholas Bellinger for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) { 26747d7a7435SNicholas Bellinger len += sizeof(struct configfs_attribute *); 26757d7a7435SNicholas Bellinger } 2676801fc54dSBryant G. Ly for (i = 0; tcmu_attrib_attrs[i] != NULL; i++) { 2677801fc54dSBryant G. Ly len += sizeof(struct configfs_attribute *); 2678801fc54dSBryant G. Ly } 2679801fc54dSBryant G. Ly len += sizeof(struct configfs_attribute *); 26807d7a7435SNicholas Bellinger 26817d7a7435SNicholas Bellinger tcmu_attrs = kzalloc(len, GFP_KERNEL); 26827d7a7435SNicholas Bellinger if (!tcmu_attrs) { 26837d7a7435SNicholas Bellinger ret = -ENOMEM; 26847d7a7435SNicholas Bellinger goto out_unreg_genl; 26857d7a7435SNicholas Bellinger } 26867d7a7435SNicholas Bellinger 26877d7a7435SNicholas Bellinger for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) { 26887d7a7435SNicholas Bellinger tcmu_attrs[i] = passthrough_attrib_attrs[i]; 26897d7a7435SNicholas Bellinger } 2690801fc54dSBryant G. Ly for (k = 0; tcmu_attrib_attrs[k] != NULL; k++) { 2691801fc54dSBryant G. Ly tcmu_attrs[i] = tcmu_attrib_attrs[k]; 26929a8bb606SBryant G. Ly i++; 2693801fc54dSBryant G. Ly } 26947d7a7435SNicholas Bellinger tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs; 26957d7a7435SNicholas Bellinger 26960a06d430SChristoph Hellwig ret = transport_backend_register(&tcmu_ops); 26977c9e7a6fSAndy Grover if (ret) 26987d7a7435SNicholas Bellinger goto out_attrs; 26997c9e7a6fSAndy Grover 27007c9e7a6fSAndy Grover return 0; 27017c9e7a6fSAndy Grover 27027d7a7435SNicholas Bellinger out_attrs: 27037d7a7435SNicholas Bellinger kfree(tcmu_attrs); 27047c9e7a6fSAndy Grover out_unreg_genl: 27057c9e7a6fSAndy Grover genl_unregister_family(&tcmu_genl_family); 27067c9e7a6fSAndy Grover out_unreg_device: 27077c9e7a6fSAndy Grover root_device_unregister(tcmu_root_device); 27087c9e7a6fSAndy Grover out_free_cache: 27097c9e7a6fSAndy Grover kmem_cache_destroy(tcmu_cmd_cache); 27107c9e7a6fSAndy Grover 27117c9e7a6fSAndy Grover return ret; 27127c9e7a6fSAndy Grover } 27137c9e7a6fSAndy Grover 27147c9e7a6fSAndy Grover static void __exit tcmu_module_exit(void) 27157c9e7a6fSAndy Grover { 2716af1dd7ffSMike Christie cancel_delayed_work_sync(&tcmu_unmap_work); 27170a06d430SChristoph Hellwig target_backend_unregister(&tcmu_ops); 27187d7a7435SNicholas Bellinger kfree(tcmu_attrs); 27197c9e7a6fSAndy Grover genl_unregister_family(&tcmu_genl_family); 27207c9e7a6fSAndy Grover root_device_unregister(tcmu_root_device); 27217c9e7a6fSAndy Grover kmem_cache_destroy(tcmu_cmd_cache); 27227c9e7a6fSAndy Grover } 27237c9e7a6fSAndy Grover 27247c9e7a6fSAndy Grover MODULE_DESCRIPTION("TCM USER subsystem plugin"); 27257c9e7a6fSAndy Grover MODULE_AUTHOR("Shaohua Li <shli@kernel.org>"); 27267c9e7a6fSAndy Grover MODULE_AUTHOR("Andy Grover <agrover@redhat.com>"); 27277c9e7a6fSAndy Grover MODULE_LICENSE("GPL"); 27287c9e7a6fSAndy Grover 27297c9e7a6fSAndy Grover module_init(tcmu_module_init); 27307c9e7a6fSAndy Grover module_exit(tcmu_module_exit); 2731