1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 27c9e7a6fSAndy Grover /* 37c9e7a6fSAndy Grover * Copyright (C) 2013 Shaohua Li <shli@kernel.org> 47c9e7a6fSAndy Grover * Copyright (C) 2014 Red Hat, Inc. 5f97ec7dbSIlias Tsitsimpis * Copyright (C) 2015 Arrikto, Inc. 6141685a3SXiubo Li * Copyright (C) 2017 Chinamobile, Inc. 77c9e7a6fSAndy Grover */ 87c9e7a6fSAndy Grover 97c9e7a6fSAndy Grover #include <linux/spinlock.h> 107c9e7a6fSAndy Grover #include <linux/module.h> 11ba929992SBart Van Assche #include <linux/kernel.h> 127c9e7a6fSAndy Grover #include <linux/timer.h> 137c9e7a6fSAndy Grover #include <linux/parser.h> 145538d294SDavid S. Miller #include <linux/vmalloc.h> 157c9e7a6fSAndy Grover #include <linux/uio_driver.h> 16d3cbb743SBodo Stroesser #include <linux/xarray.h> 17ac64a2ceSDavid Disseldorp #include <linux/stringify.h> 1826418649SSheng Yang #include <linux/bitops.h> 19f5045724SBart Van Assche #include <linux/highmem.h> 207d7a7435SNicholas Bellinger #include <linux/configfs.h> 21b6df4b79SXiubo Li #include <linux/mutex.h> 229972cebbSMike Christie #include <linux/workqueue.h> 23*bb9b9eb0SXiaoguang Wang #include <linux/pagemap.h> 247c9e7a6fSAndy Grover #include <net/genetlink.h> 25ba929992SBart Van Assche #include <scsi/scsi_common.h> 26ba929992SBart Van Assche #include <scsi/scsi_proto.h> 277c9e7a6fSAndy Grover #include <target/target_core_base.h> 287c9e7a6fSAndy Grover #include <target/target_core_fabric.h> 297c9e7a6fSAndy Grover #include <target/target_core_backend.h> 30e9f720d6SNicholas Bellinger 317c9e7a6fSAndy Grover #include <linux/target_core_user.h> 327c9e7a6fSAndy Grover 33572ccdabSRandy Dunlap /** 34572ccdabSRandy Dunlap * DOC: Userspace I/O 35572ccdabSRandy Dunlap * Userspace I/O 36572ccdabSRandy Dunlap * ------------- 37572ccdabSRandy Dunlap * 387c9e7a6fSAndy Grover * Define a shared-memory interface for LIO to pass SCSI commands and 397c9e7a6fSAndy Grover * data to userspace for processing. This is to allow backends that 407c9e7a6fSAndy Grover * are too complex for in-kernel support to be possible. 417c9e7a6fSAndy Grover * 427c9e7a6fSAndy Grover * It uses the UIO framework to do a lot of the device-creation and 437c9e7a6fSAndy Grover * introspection work for us. 447c9e7a6fSAndy Grover * 457c9e7a6fSAndy Grover * See the .h file for how the ring is laid out. Note that while the 467c9e7a6fSAndy Grover * command ring is defined, the particulars of the data area are 477c9e7a6fSAndy Grover * not. Offset values in the command entry point to other locations 48572ccdabSRandy Dunlap * internal to the mmap-ed area. There is separate space outside the 497c9e7a6fSAndy Grover * command ring for data buffers. This leaves maximum flexibility for 507c9e7a6fSAndy Grover * moving buffer allocations, or even page flipping or other 517c9e7a6fSAndy Grover * allocation techniques, without altering the command ring layout. 527c9e7a6fSAndy Grover * 537c9e7a6fSAndy Grover * SECURITY: 547c9e7a6fSAndy Grover * The user process must be assumed to be malicious. There's no way to 557c9e7a6fSAndy Grover * prevent it breaking the command ring protocol if it wants, but in 567c9e7a6fSAndy Grover * order to prevent other issues we must only ever read *data* from 577c9e7a6fSAndy Grover * the shared memory area, not offsets or sizes. This applies to 587c9e7a6fSAndy Grover * command ring entries as well as the mailbox. Extra code needed for 597c9e7a6fSAndy Grover * this may have a 'UAM' comment. 607c9e7a6fSAndy Grover */ 617c9e7a6fSAndy Grover 627c9e7a6fSAndy Grover #define TCMU_TIME_OUT (30 * MSEC_PER_SEC) 637c9e7a6fSAndy Grover 64ecddbb7eSBodo Stroesser /* For mailbox plus cmd ring, the size is fixed 8MB */ 65c7ede4f0SGuixin Liu #define MB_CMDR_SIZE_DEF (8 * 1024 * 1024) 66ecddbb7eSBodo Stroesser /* Offset of cmd ring is size of mailbox */ 67c7ede4f0SGuixin Liu #define CMDR_OFF ((__u32)sizeof(struct tcmu_mailbox)) 68c7ede4f0SGuixin Liu #define CMDR_SIZE_DEF (MB_CMDR_SIZE_DEF - CMDR_OFF) 6926418649SSheng Yang 70b6df4b79SXiubo Li /* 71f5ce815fSBodo Stroesser * For data area, the default block size is PAGE_SIZE and 72f5ce815fSBodo Stroesser * the default total size is 256K * PAGE_SIZE. 73b6df4b79SXiubo Li */ 74e719afdcSBodo Stroesser #define DATA_PAGES_PER_BLK_DEF 1 75f5ce815fSBodo Stroesser #define DATA_AREA_PAGES_DEF (256 * 1024) 767c9e7a6fSAndy Grover 77f5ce815fSBodo Stroesser #define TCMU_MBS_TO_PAGES(_mbs) ((size_t)_mbs << (20 - PAGE_SHIFT)) 788b084d9dSBodo Stroesser #define TCMU_PAGES_TO_MBS(_pages) (_pages >> (20 - PAGE_SHIFT)) 7980eb8761SMike Christie 80af1dd7ffSMike Christie /* 81af1dd7ffSMike Christie * Default number of global data blocks(512K * PAGE_SIZE) 82af1dd7ffSMike Christie * when the unmap thread will be started. 83af1dd7ffSMike Christie */ 848b084d9dSBodo Stroesser #define TCMU_GLOBAL_MAX_PAGES_DEF (512 * 1024) 85b6df4b79SXiubo Li 86b3af66e2SMike Christie static u8 tcmu_kern_cmd_reply_supported; 87bdaeedc1SMike Christie static u8 tcmu_netlink_blocked; 88b3af66e2SMike Christie 897c9e7a6fSAndy Grover static struct device *tcmu_root_device; 907c9e7a6fSAndy Grover 917c9e7a6fSAndy Grover struct tcmu_hba { 927c9e7a6fSAndy Grover u32 host_id; 937c9e7a6fSAndy Grover }; 947c9e7a6fSAndy Grover 957c9e7a6fSAndy Grover #define TCMU_CONFIG_LEN 256 967c9e7a6fSAndy Grover 973228691fSMike Christie static DEFINE_MUTEX(tcmu_nl_cmd_mutex); 983228691fSMike Christie static LIST_HEAD(tcmu_nl_cmd_list); 993228691fSMike Christie 1003228691fSMike Christie struct tcmu_dev; 1013228691fSMike Christie 102b3af66e2SMike Christie struct tcmu_nl_cmd { 103b3af66e2SMike Christie /* wake up thread waiting for reply */ 104b3af66e2SMike Christie struct completion complete; 1053228691fSMike Christie struct list_head nl_list; 1063228691fSMike Christie struct tcmu_dev *udev; 107b3af66e2SMike Christie int cmd; 108b3af66e2SMike Christie int status; 109b3af66e2SMike Christie }; 110b3af66e2SMike Christie 1117c9e7a6fSAndy Grover struct tcmu_dev { 112b6df4b79SXiubo Li struct list_head node; 113f3cdbe39SMike Christie struct kref kref; 114af1dd7ffSMike Christie 1157c9e7a6fSAndy Grover struct se_device se_dev; 1166888da81SMike Christie struct se_dev_plug se_plug; 1177c9e7a6fSAndy Grover 1187c9e7a6fSAndy Grover char *name; 1197c9e7a6fSAndy Grover struct se_hba *hba; 1207c9e7a6fSAndy Grover 1217c9e7a6fSAndy Grover #define TCMU_DEV_BIT_OPEN 0 1227c9e7a6fSAndy Grover #define TCMU_DEV_BIT_BROKEN 1 123892782caSMike Christie #define TCMU_DEV_BIT_BLOCKED 2 12459526d7aSBodo Stroesser #define TCMU_DEV_BIT_TMR_NOTIFY 3 1253ac0fcb4SBodo Stroesser #define TCMU_DEV_BIT_PLUGGED 4 1267c9e7a6fSAndy Grover unsigned long flags; 1277c9e7a6fSAndy Grover 1287c9e7a6fSAndy Grover struct uio_info uio_info; 1297c9e7a6fSAndy Grover 130b6df4b79SXiubo Li struct inode *inode; 131b6df4b79SXiubo Li 1320e0d7526SMike Christie uint64_t dev_size; 133ecddbb7eSBodo Stroesser 134ecddbb7eSBodo Stroesser struct tcmu_mailbox *mb_addr; 135ecddbb7eSBodo Stroesser void *cmdr; 1367c9e7a6fSAndy Grover u32 cmdr_size; 1377c9e7a6fSAndy Grover u32 cmdr_last_cleaned; 1383d9b9555SAndy Grover /* Offset of data area from start of mb */ 13926418649SSheng Yang /* Must add data_off and mb_addr to get the address */ 1407c9e7a6fSAndy Grover size_t data_off; 141f5ce815fSBodo Stroesser int data_area_mb; 14280eb8761SMike Christie uint32_t max_blocks; 143ecddbb7eSBodo Stroesser size_t mmap_pages; 14426418649SSheng Yang 145b6df4b79SXiubo Li struct mutex cmdr_lock; 146a94a2572SXiubo Li struct list_head qfull_queue; 147bc2d214aSBodo Stroesser struct list_head tmr_queue; 1487c9e7a6fSAndy Grover 149141685a3SXiubo Li uint32_t dbi_max; 150b6df4b79SXiubo Li uint32_t dbi_thresh; 15180eb8761SMike Christie unsigned long *data_bitmap; 1528b084d9dSBodo Stroesser struct xarray data_pages; 153e719afdcSBodo Stroesser uint32_t data_pages_per_blk; 154e719afdcSBodo Stroesser uint32_t data_blk_size; 155141685a3SXiubo Li 156d3cbb743SBodo Stroesser struct xarray commands; 1577c9e7a6fSAndy Grover 1589103575aSMike Christie struct timer_list cmd_timer; 159af980e46SMike Christie unsigned int cmd_time_out; 160a94a2572SXiubo Li struct list_head inflight_queue; 1619103575aSMike Christie 1629103575aSMike Christie struct timer_list qfull_timer; 1639103575aSMike Christie int qfull_time_out; 1649103575aSMike Christie 165488ebe4cSMike Christie struct list_head timedout_entry; 1667c9e7a6fSAndy Grover 167b3af66e2SMike Christie struct tcmu_nl_cmd curr_nl_cmd; 168b3af66e2SMike Christie 1697c9e7a6fSAndy Grover char dev_config[TCMU_CONFIG_LEN]; 170b849b456SKenjiro Nakayama 171b849b456SKenjiro Nakayama int nl_reply_supported; 1727c9e7a6fSAndy Grover }; 1737c9e7a6fSAndy Grover 1747c9e7a6fSAndy Grover #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev) 1757c9e7a6fSAndy Grover 1767c9e7a6fSAndy Grover struct tcmu_cmd { 1777c9e7a6fSAndy Grover struct se_cmd *se_cmd; 1787c9e7a6fSAndy Grover struct tcmu_dev *tcmu_dev; 179a94a2572SXiubo Li struct list_head queue_entry; 1807c9e7a6fSAndy Grover 1817c9e7a6fSAndy Grover uint16_t cmd_id; 1827c9e7a6fSAndy Grover 18326418649SSheng Yang /* Can't use se_cmd when cleaning up expired cmds, because if 1847c9e7a6fSAndy Grover cmd has been completed then accessing se_cmd is off limits */ 185141685a3SXiubo Li uint32_t dbi_cnt; 18652ef2743SBodo Stroesser uint32_t dbi_bidi_cnt; 187141685a3SXiubo Li uint32_t dbi_cur; 188141685a3SXiubo Li uint32_t *dbi; 1897c9e7a6fSAndy Grover 1903c9a7c58SBodo Stroesser uint32_t data_len_bidi; 1913c9a7c58SBodo Stroesser 1927c9e7a6fSAndy Grover unsigned long deadline; 1937c9e7a6fSAndy Grover 1947c9e7a6fSAndy Grover #define TCMU_CMD_BIT_EXPIRED 0 195018c1491SBodo Stroesser #define TCMU_CMD_BIT_KEEP_BUF 1 1967c9e7a6fSAndy Grover unsigned long flags; 1977c9e7a6fSAndy Grover }; 198bc2d214aSBodo Stroesser 199bc2d214aSBodo Stroesser struct tcmu_tmr { 200bc2d214aSBodo Stroesser struct list_head queue_entry; 201bc2d214aSBodo Stroesser 202bc2d214aSBodo Stroesser uint8_t tmr_type; 203bc2d214aSBodo Stroesser uint32_t tmr_cmd_cnt; 2048fdaabe1SGustavo A. R. Silva int16_t tmr_cmd_ids[]; 205bc2d214aSBodo Stroesser }; 206bc2d214aSBodo Stroesser 207af1dd7ffSMike Christie /* 208af1dd7ffSMike Christie * To avoid dead lock the mutex lock order should always be: 209af1dd7ffSMike Christie * 210af1dd7ffSMike Christie * mutex_lock(&root_udev_mutex); 211af1dd7ffSMike Christie * ... 212af1dd7ffSMike Christie * mutex_lock(&tcmu_dev->cmdr_lock); 213af1dd7ffSMike Christie * mutex_unlock(&tcmu_dev->cmdr_lock); 214af1dd7ffSMike Christie * ... 215af1dd7ffSMike Christie * mutex_unlock(&root_udev_mutex); 216af1dd7ffSMike Christie */ 217b6df4b79SXiubo Li static DEFINE_MUTEX(root_udev_mutex); 218b6df4b79SXiubo Li static LIST_HEAD(root_udev); 219b6df4b79SXiubo Li 220488ebe4cSMike Christie static DEFINE_SPINLOCK(timed_out_udevs_lock); 221488ebe4cSMike Christie static LIST_HEAD(timed_out_udevs); 222488ebe4cSMike Christie 22380eb8761SMike Christie static struct kmem_cache *tcmu_cmd_cache; 22480eb8761SMike Christie 2258b084d9dSBodo Stroesser static atomic_t global_page_count = ATOMIC_INIT(0); 226af1dd7ffSMike Christie static struct delayed_work tcmu_unmap_work; 2278b084d9dSBodo Stroesser static int tcmu_global_max_pages = TCMU_GLOBAL_MAX_PAGES_DEF; 228b6df4b79SXiubo Li 22980eb8761SMike Christie static int tcmu_set_global_max_data_area(const char *str, 23080eb8761SMike Christie const struct kernel_param *kp) 23180eb8761SMike Christie { 23280eb8761SMike Christie int ret, max_area_mb; 23380eb8761SMike Christie 23480eb8761SMike Christie ret = kstrtoint(str, 10, &max_area_mb); 23580eb8761SMike Christie if (ret) 23680eb8761SMike Christie return -EINVAL; 23780eb8761SMike Christie 23880eb8761SMike Christie if (max_area_mb <= 0) { 23980eb8761SMike Christie pr_err("global_max_data_area must be larger than 0.\n"); 24080eb8761SMike Christie return -EINVAL; 24180eb8761SMike Christie } 24280eb8761SMike Christie 2438b084d9dSBodo Stroesser tcmu_global_max_pages = TCMU_MBS_TO_PAGES(max_area_mb); 2448b084d9dSBodo Stroesser if (atomic_read(&global_page_count) > tcmu_global_max_pages) 24580eb8761SMike Christie schedule_delayed_work(&tcmu_unmap_work, 0); 24680eb8761SMike Christie else 24780eb8761SMike Christie cancel_delayed_work_sync(&tcmu_unmap_work); 24880eb8761SMike Christie 24980eb8761SMike Christie return 0; 25080eb8761SMike Christie } 25180eb8761SMike Christie 25280eb8761SMike Christie static int tcmu_get_global_max_data_area(char *buffer, 25380eb8761SMike Christie const struct kernel_param *kp) 25480eb8761SMike Christie { 2558b084d9dSBodo Stroesser return sprintf(buffer, "%d\n", TCMU_PAGES_TO_MBS(tcmu_global_max_pages)); 25680eb8761SMike Christie } 25780eb8761SMike Christie 25880eb8761SMike Christie static const struct kernel_param_ops tcmu_global_max_data_area_op = { 25980eb8761SMike Christie .set = tcmu_set_global_max_data_area, 26080eb8761SMike Christie .get = tcmu_get_global_max_data_area, 26180eb8761SMike Christie }; 26280eb8761SMike Christie 26380eb8761SMike Christie module_param_cb(global_max_data_area_mb, &tcmu_global_max_data_area_op, NULL, 26480eb8761SMike Christie S_IWUSR | S_IRUGO); 26580eb8761SMike Christie MODULE_PARM_DESC(global_max_data_area_mb, 26680eb8761SMike Christie "Max MBs allowed to be allocated to all the tcmu device's " 26780eb8761SMike Christie "data areas."); 2687c9e7a6fSAndy Grover 269bdaeedc1SMike Christie static int tcmu_get_block_netlink(char *buffer, 270bdaeedc1SMike Christie const struct kernel_param *kp) 271bdaeedc1SMike Christie { 272bdaeedc1SMike Christie return sprintf(buffer, "%s\n", tcmu_netlink_blocked ? 273bdaeedc1SMike Christie "blocked" : "unblocked"); 274bdaeedc1SMike Christie } 275bdaeedc1SMike Christie 276bdaeedc1SMike Christie static int tcmu_set_block_netlink(const char *str, 277bdaeedc1SMike Christie const struct kernel_param *kp) 278bdaeedc1SMike Christie { 279bdaeedc1SMike Christie int ret; 280bdaeedc1SMike Christie u8 val; 281bdaeedc1SMike Christie 282bdaeedc1SMike Christie ret = kstrtou8(str, 0, &val); 283bdaeedc1SMike Christie if (ret < 0) 284bdaeedc1SMike Christie return ret; 285bdaeedc1SMike Christie 286bdaeedc1SMike Christie if (val > 1) { 287bdaeedc1SMike Christie pr_err("Invalid block netlink value %u\n", val); 288bdaeedc1SMike Christie return -EINVAL; 289bdaeedc1SMike Christie } 290bdaeedc1SMike Christie 291bdaeedc1SMike Christie tcmu_netlink_blocked = val; 292bdaeedc1SMike Christie return 0; 293bdaeedc1SMike Christie } 294bdaeedc1SMike Christie 295bdaeedc1SMike Christie static const struct kernel_param_ops tcmu_block_netlink_op = { 296bdaeedc1SMike Christie .set = tcmu_set_block_netlink, 297bdaeedc1SMike Christie .get = tcmu_get_block_netlink, 298bdaeedc1SMike Christie }; 299bdaeedc1SMike Christie 300bdaeedc1SMike Christie module_param_cb(block_netlink, &tcmu_block_netlink_op, NULL, S_IWUSR | S_IRUGO); 301bdaeedc1SMike Christie MODULE_PARM_DESC(block_netlink, "Block new netlink commands."); 302bdaeedc1SMike Christie 303bdaeedc1SMike Christie static int tcmu_fail_netlink_cmd(struct tcmu_nl_cmd *nl_cmd) 304bdaeedc1SMike Christie { 305bdaeedc1SMike Christie struct tcmu_dev *udev = nl_cmd->udev; 306bdaeedc1SMike Christie 307bdaeedc1SMike Christie if (!tcmu_netlink_blocked) { 308bdaeedc1SMike Christie pr_err("Could not reset device's netlink interface. Netlink is not blocked.\n"); 309bdaeedc1SMike Christie return -EBUSY; 310bdaeedc1SMike Christie } 311bdaeedc1SMike Christie 312bdaeedc1SMike Christie if (nl_cmd->cmd != TCMU_CMD_UNSPEC) { 313bdaeedc1SMike Christie pr_debug("Aborting nl cmd %d on %s\n", nl_cmd->cmd, udev->name); 314bdaeedc1SMike Christie nl_cmd->status = -EINTR; 315bdaeedc1SMike Christie list_del(&nl_cmd->nl_list); 316bdaeedc1SMike Christie complete(&nl_cmd->complete); 317bdaeedc1SMike Christie } 318bdaeedc1SMike Christie return 0; 319bdaeedc1SMike Christie } 320bdaeedc1SMike Christie 321bdaeedc1SMike Christie static int tcmu_set_reset_netlink(const char *str, 322bdaeedc1SMike Christie const struct kernel_param *kp) 323bdaeedc1SMike Christie { 324bdaeedc1SMike Christie struct tcmu_nl_cmd *nl_cmd, *tmp_cmd; 325bdaeedc1SMike Christie int ret; 326bdaeedc1SMike Christie u8 val; 327bdaeedc1SMike Christie 328bdaeedc1SMike Christie ret = kstrtou8(str, 0, &val); 329bdaeedc1SMike Christie if (ret < 0) 330bdaeedc1SMike Christie return ret; 331bdaeedc1SMike Christie 332bdaeedc1SMike Christie if (val != 1) { 333bdaeedc1SMike Christie pr_err("Invalid reset netlink value %u\n", val); 334bdaeedc1SMike Christie return -EINVAL; 335bdaeedc1SMike Christie } 336bdaeedc1SMike Christie 337bdaeedc1SMike Christie mutex_lock(&tcmu_nl_cmd_mutex); 338bdaeedc1SMike Christie list_for_each_entry_safe(nl_cmd, tmp_cmd, &tcmu_nl_cmd_list, nl_list) { 339bdaeedc1SMike Christie ret = tcmu_fail_netlink_cmd(nl_cmd); 340bdaeedc1SMike Christie if (ret) 341bdaeedc1SMike Christie break; 342bdaeedc1SMike Christie } 343bdaeedc1SMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 344bdaeedc1SMike Christie 345bdaeedc1SMike Christie return ret; 346bdaeedc1SMike Christie } 347bdaeedc1SMike Christie 348bdaeedc1SMike Christie static const struct kernel_param_ops tcmu_reset_netlink_op = { 349bdaeedc1SMike Christie .set = tcmu_set_reset_netlink, 350bdaeedc1SMike Christie }; 351bdaeedc1SMike Christie 352bdaeedc1SMike Christie module_param_cb(reset_netlink, &tcmu_reset_netlink_op, NULL, S_IWUSR); 353bdaeedc1SMike Christie MODULE_PARM_DESC(reset_netlink, "Reset netlink commands."); 354bdaeedc1SMike Christie 3557c9e7a6fSAndy Grover /* multicast group */ 3567c9e7a6fSAndy Grover enum tcmu_multicast_groups { 3577c9e7a6fSAndy Grover TCMU_MCGRP_CONFIG, 3587c9e7a6fSAndy Grover }; 3597c9e7a6fSAndy Grover 3607c9e7a6fSAndy Grover static const struct genl_multicast_group tcmu_mcgrps[] = { 3617c9e7a6fSAndy Grover [TCMU_MCGRP_CONFIG] = { .name = "config", }, 3627c9e7a6fSAndy Grover }; 3637c9e7a6fSAndy Grover 364b3af66e2SMike Christie static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = { 365b3af66e2SMike Christie [TCMU_ATTR_DEVICE] = { .type = NLA_STRING }, 366b3af66e2SMike Christie [TCMU_ATTR_MINOR] = { .type = NLA_U32 }, 367b3af66e2SMike Christie [TCMU_ATTR_CMD_STATUS] = { .type = NLA_S32 }, 368b3af66e2SMike Christie [TCMU_ATTR_DEVICE_ID] = { .type = NLA_U32 }, 369b3af66e2SMike Christie [TCMU_ATTR_SUPP_KERN_CMD_REPLY] = { .type = NLA_U8 }, 370b3af66e2SMike Christie }; 371b3af66e2SMike Christie 372b3af66e2SMike Christie static int tcmu_genl_cmd_done(struct genl_info *info, int completed_cmd) 373b3af66e2SMike Christie { 3743228691fSMike Christie struct tcmu_dev *udev = NULL; 375b3af66e2SMike Christie struct tcmu_nl_cmd *nl_cmd; 376b3af66e2SMike Christie int dev_id, rc, ret = 0; 377b3af66e2SMike Christie 378b3af66e2SMike Christie if (!info->attrs[TCMU_ATTR_CMD_STATUS] || 379b3af66e2SMike Christie !info->attrs[TCMU_ATTR_DEVICE_ID]) { 380b3af66e2SMike Christie printk(KERN_ERR "TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n"); 381b3af66e2SMike Christie return -EINVAL; 382b3af66e2SMike Christie } 383b3af66e2SMike Christie 384b3af66e2SMike Christie dev_id = nla_get_u32(info->attrs[TCMU_ATTR_DEVICE_ID]); 385b3af66e2SMike Christie rc = nla_get_s32(info->attrs[TCMU_ATTR_CMD_STATUS]); 386b3af66e2SMike Christie 3873228691fSMike Christie mutex_lock(&tcmu_nl_cmd_mutex); 3883228691fSMike Christie list_for_each_entry(nl_cmd, &tcmu_nl_cmd_list, nl_list) { 3893228691fSMike Christie if (nl_cmd->udev->se_dev.dev_index == dev_id) { 3903228691fSMike Christie udev = nl_cmd->udev; 3913228691fSMike Christie break; 392b3af66e2SMike Christie } 3933228691fSMike Christie } 394b3af66e2SMike Christie 3953228691fSMike Christie if (!udev) { 3960c218e16SMike Christie pr_err("tcmu nl cmd %u/%d completion could not find device with dev id %u.\n", 3973228691fSMike Christie completed_cmd, rc, dev_id); 3983228691fSMike Christie ret = -ENODEV; 3993228691fSMike Christie goto unlock; 4003228691fSMike Christie } 4013228691fSMike Christie list_del(&nl_cmd->nl_list); 402b3af66e2SMike Christie 403bdaeedc1SMike Christie pr_debug("%s genl cmd done got id %d curr %d done %d rc %d stat %d\n", 404bdaeedc1SMike Christie udev->name, dev_id, nl_cmd->cmd, completed_cmd, rc, 405bdaeedc1SMike Christie nl_cmd->status); 406b3af66e2SMike Christie 407b3af66e2SMike Christie if (nl_cmd->cmd != completed_cmd) { 4083228691fSMike Christie pr_err("Mismatched commands on %s (Expecting reply for %d. Current %d).\n", 4093228691fSMike Christie udev->name, completed_cmd, nl_cmd->cmd); 410b3af66e2SMike Christie ret = -EINVAL; 4113228691fSMike Christie goto unlock; 412b3af66e2SMike Christie } 413b3af66e2SMike Christie 4143228691fSMike Christie nl_cmd->status = rc; 415b3af66e2SMike Christie complete(&nl_cmd->complete); 4163228691fSMike Christie unlock: 4173228691fSMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 418b3af66e2SMike Christie return ret; 419b3af66e2SMike Christie } 420b3af66e2SMike Christie 421b3af66e2SMike Christie static int tcmu_genl_rm_dev_done(struct sk_buff *skb, struct genl_info *info) 422b3af66e2SMike Christie { 423b3af66e2SMike Christie return tcmu_genl_cmd_done(info, TCMU_CMD_REMOVED_DEVICE); 424b3af66e2SMike Christie } 425b3af66e2SMike Christie 426b3af66e2SMike Christie static int tcmu_genl_add_dev_done(struct sk_buff *skb, struct genl_info *info) 427b3af66e2SMike Christie { 428b3af66e2SMike Christie return tcmu_genl_cmd_done(info, TCMU_CMD_ADDED_DEVICE); 429b3af66e2SMike Christie } 430b3af66e2SMike Christie 431b3af66e2SMike Christie static int tcmu_genl_reconfig_dev_done(struct sk_buff *skb, 432b3af66e2SMike Christie struct genl_info *info) 433b3af66e2SMike Christie { 434b3af66e2SMike Christie return tcmu_genl_cmd_done(info, TCMU_CMD_RECONFIG_DEVICE); 435b3af66e2SMike Christie } 436b3af66e2SMike Christie 437b3af66e2SMike Christie static int tcmu_genl_set_features(struct sk_buff *skb, struct genl_info *info) 438b3af66e2SMike Christie { 439b3af66e2SMike Christie if (info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]) { 440b3af66e2SMike Christie tcmu_kern_cmd_reply_supported = 441b3af66e2SMike Christie nla_get_u8(info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]); 442b3af66e2SMike Christie printk(KERN_INFO "tcmu daemon: command reply support %u.\n", 443b3af66e2SMike Christie tcmu_kern_cmd_reply_supported); 444b3af66e2SMike Christie } 445b3af66e2SMike Christie 446b3af66e2SMike Christie return 0; 447b3af66e2SMike Christie } 448b3af66e2SMike Christie 44966a9b928SJakub Kicinski static const struct genl_small_ops tcmu_genl_ops[] = { 450b3af66e2SMike Christie { 451b3af66e2SMike Christie .cmd = TCMU_CMD_SET_FEATURES, 452ef6243acSJohannes Berg .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 453b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 454b3af66e2SMike Christie .doit = tcmu_genl_set_features, 455b3af66e2SMike Christie }, 456b3af66e2SMike Christie { 457b3af66e2SMike Christie .cmd = TCMU_CMD_ADDED_DEVICE_DONE, 458ef6243acSJohannes Berg .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 459b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 460b3af66e2SMike Christie .doit = tcmu_genl_add_dev_done, 461b3af66e2SMike Christie }, 462b3af66e2SMike Christie { 463b3af66e2SMike Christie .cmd = TCMU_CMD_REMOVED_DEVICE_DONE, 464ef6243acSJohannes Berg .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 465b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 466b3af66e2SMike Christie .doit = tcmu_genl_rm_dev_done, 467b3af66e2SMike Christie }, 468b3af66e2SMike Christie { 469b3af66e2SMike Christie .cmd = TCMU_CMD_RECONFIG_DEVICE_DONE, 470ef6243acSJohannes Berg .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 471b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 472b3af66e2SMike Christie .doit = tcmu_genl_reconfig_dev_done, 473b3af66e2SMike Christie }, 474b3af66e2SMike Christie }; 475b3af66e2SMike Christie 4767c9e7a6fSAndy Grover /* Our generic netlink family */ 47756989f6dSJohannes Berg static struct genl_family tcmu_genl_family __ro_after_init = { 478489111e5SJohannes Berg .module = THIS_MODULE, 4797c9e7a6fSAndy Grover .hdrsize = 0, 4807c9e7a6fSAndy Grover .name = "TCM-USER", 481b3af66e2SMike Christie .version = 2, 4827c9e7a6fSAndy Grover .maxattr = TCMU_ATTR_MAX, 4833b0f31f2SJohannes Berg .policy = tcmu_attr_policy, 4847c9e7a6fSAndy Grover .mcgrps = tcmu_mcgrps, 4857c9e7a6fSAndy Grover .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps), 48620c08b36SSheng Yang .netnsok = true, 48766a9b928SJakub Kicinski .small_ops = tcmu_genl_ops, 48866a9b928SJakub Kicinski .n_small_ops = ARRAY_SIZE(tcmu_genl_ops), 4897c9e7a6fSAndy Grover }; 4907c9e7a6fSAndy Grover 491141685a3SXiubo Li #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index)) 492141685a3SXiubo Li #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0) 493141685a3SXiubo Li #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index)) 494141685a3SXiubo Li #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++]) 495141685a3SXiubo Li 496b6df4b79SXiubo Li static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len) 497141685a3SXiubo Li { 498141685a3SXiubo Li struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 499141685a3SXiubo Li uint32_t i; 500141685a3SXiubo Li 501b6df4b79SXiubo Li for (i = 0; i < len; i++) 502141685a3SXiubo Li clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap); 503141685a3SXiubo Li } 504141685a3SXiubo Li 5057e98905eSBodo Stroesser static inline int tcmu_get_empty_block(struct tcmu_dev *udev, 5067e98905eSBodo Stroesser struct tcmu_cmd *tcmu_cmd, 507f5ce815fSBodo Stroesser int prev_dbi, int length, int *iov_cnt) 508141685a3SXiubo Li { 509f5ce815fSBodo Stroesser XA_STATE(xas, &udev->data_pages, 0); 510b6df4b79SXiubo Li struct page *page; 511e719afdcSBodo Stroesser int i, cnt, dbi, dpi; 512f5ce815fSBodo Stroesser int page_cnt = DIV_ROUND_UP(length, PAGE_SIZE); 513141685a3SXiubo Li 514b6df4b79SXiubo Li dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh); 515b6df4b79SXiubo Li if (dbi == udev->dbi_thresh) 5167e98905eSBodo Stroesser return -1; 517b6df4b79SXiubo Li 518e719afdcSBodo Stroesser dpi = dbi * udev->data_pages_per_blk; 519f5ce815fSBodo Stroesser /* Count the number of already allocated pages */ 520e719afdcSBodo Stroesser xas_set(&xas, dpi); 521b4150b68SBodo Stroesser rcu_read_lock(); 522f5ce815fSBodo Stroesser for (cnt = 0; xas_next(&xas) && cnt < page_cnt;) 523f5ce815fSBodo Stroesser cnt++; 524b4150b68SBodo Stroesser rcu_read_unlock(); 525b6df4b79SXiubo Li 526f5ce815fSBodo Stroesser for (i = cnt; i < page_cnt; i++) { 5271d2ac7b6SBodo Stroesser /* try to get new zeroed page from the mm */ 5281d2ac7b6SBodo Stroesser page = alloc_page(GFP_NOIO | __GFP_ZERO); 529b6df4b79SXiubo Li if (!page) 530f5ce815fSBodo Stroesser break; 531b6df4b79SXiubo Li 532e719afdcSBodo Stroesser if (xa_store(&udev->data_pages, dpi + i, page, GFP_NOIO)) { 533f5ce815fSBodo Stroesser __free_page(page); 534f5ce815fSBodo Stroesser break; 535b6df4b79SXiubo Li } 536f5ce815fSBodo Stroesser } 537f5ce815fSBodo Stroesser if (atomic_add_return(i - cnt, &global_page_count) > 538f5ce815fSBodo Stroesser tcmu_global_max_pages) 539f5ce815fSBodo Stroesser schedule_delayed_work(&tcmu_unmap_work, 0); 540b6df4b79SXiubo Li 541f5ce815fSBodo Stroesser if (i && dbi > udev->dbi_max) 542141685a3SXiubo Li udev->dbi_max = dbi; 543141685a3SXiubo Li 544141685a3SXiubo Li set_bit(dbi, udev->data_bitmap); 545b6df4b79SXiubo Li tcmu_cmd_set_dbi(tcmu_cmd, dbi); 546141685a3SXiubo Li 5477e98905eSBodo Stroesser if (dbi != prev_dbi + 1) 5487e98905eSBodo Stroesser *iov_cnt += 1; 5497e98905eSBodo Stroesser 550f5ce815fSBodo Stroesser return i == page_cnt ? dbi : -1; 551141685a3SXiubo Li } 552141685a3SXiubo Li 5537e98905eSBodo Stroesser static int tcmu_get_empty_blocks(struct tcmu_dev *udev, 554f5ce815fSBodo Stroesser struct tcmu_cmd *tcmu_cmd, int length) 555b6df4b79SXiubo Li { 5567e98905eSBodo Stroesser /* start value of dbi + 1 must not be a valid dbi */ 5577e98905eSBodo Stroesser int dbi = -2; 558e719afdcSBodo Stroesser int blk_data_len, iov_cnt = 0; 559e719afdcSBodo Stroesser uint32_t blk_size = udev->data_blk_size; 560b6df4b79SXiubo Li 561e719afdcSBodo Stroesser for (; length > 0; length -= blk_size) { 562e719afdcSBodo Stroesser blk_data_len = min_t(uint32_t, length, blk_size); 563e719afdcSBodo Stroesser dbi = tcmu_get_empty_block(udev, tcmu_cmd, dbi, blk_data_len, 564e719afdcSBodo Stroesser &iov_cnt); 5657e98905eSBodo Stroesser if (dbi < 0) 5667e98905eSBodo Stroesser return -1; 567141685a3SXiubo Li } 5687e98905eSBodo Stroesser return iov_cnt; 569141685a3SXiubo Li } 570141685a3SXiubo Li 571141685a3SXiubo Li static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd) 572141685a3SXiubo Li { 573141685a3SXiubo Li kfree(tcmu_cmd->dbi); 574141685a3SXiubo Li kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); 575141685a3SXiubo Li } 576141685a3SXiubo Li 57752ef2743SBodo Stroesser static inline void tcmu_cmd_set_block_cnts(struct tcmu_cmd *cmd) 578141685a3SXiubo Li { 57952ef2743SBodo Stroesser int i, len; 58052ef2743SBodo Stroesser struct se_cmd *se_cmd = cmd->se_cmd; 581e719afdcSBodo Stroesser uint32_t blk_size = cmd->tcmu_dev->data_blk_size; 58252ef2743SBodo Stroesser 583e719afdcSBodo Stroesser cmd->dbi_cnt = DIV_ROUND_UP(se_cmd->data_length, blk_size); 584141685a3SXiubo Li 585141685a3SXiubo Li if (se_cmd->se_cmd_flags & SCF_BIDI) { 586141685a3SXiubo Li BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents)); 58752ef2743SBodo Stroesser for (i = 0, len = 0; i < se_cmd->t_bidi_data_nents; i++) 58852ef2743SBodo Stroesser len += se_cmd->t_bidi_data_sg[i].length; 589e719afdcSBodo Stroesser cmd->dbi_bidi_cnt = DIV_ROUND_UP(len, blk_size); 59052ef2743SBodo Stroesser cmd->dbi_cnt += cmd->dbi_bidi_cnt; 5913c9a7c58SBodo Stroesser cmd->data_len_bidi = len; 592141685a3SXiubo Li } 593141685a3SXiubo Li } 594141685a3SXiubo Li 5953c9a7c58SBodo Stroesser static int new_block_to_iov(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 596c8ed1ff8SBodo Stroesser struct iovec **iov, int prev_dbi, int len) 5973c9a7c58SBodo Stroesser { 5983c9a7c58SBodo Stroesser /* Get the next dbi */ 5993c9a7c58SBodo Stroesser int dbi = tcmu_cmd_get_dbi(cmd); 6003c9a7c58SBodo Stroesser 601e719afdcSBodo Stroesser /* Do not add more than udev->data_blk_size to iov */ 602e719afdcSBodo Stroesser len = min_t(int, len, udev->data_blk_size); 603c8ed1ff8SBodo Stroesser 6043c9a7c58SBodo Stroesser /* 6053c9a7c58SBodo Stroesser * The following code will gather and map the blocks to the same iovec 6063c9a7c58SBodo Stroesser * when the blocks are all next to each other. 6073c9a7c58SBodo Stroesser */ 6083c9a7c58SBodo Stroesser if (dbi != prev_dbi + 1) { 6093c9a7c58SBodo Stroesser /* dbi is not next to previous dbi, so start new iov */ 6103c9a7c58SBodo Stroesser if (prev_dbi >= 0) 6113c9a7c58SBodo Stroesser (*iov)++; 6123c9a7c58SBodo Stroesser /* write offset relative to mb_addr */ 6133c9a7c58SBodo Stroesser (*iov)->iov_base = (void __user *) 614e719afdcSBodo Stroesser (udev->data_off + dbi * udev->data_blk_size); 6153c9a7c58SBodo Stroesser } 6163c9a7c58SBodo Stroesser (*iov)->iov_len += len; 6173c9a7c58SBodo Stroesser 6183c9a7c58SBodo Stroesser return dbi; 6193c9a7c58SBodo Stroesser } 6203c9a7c58SBodo Stroesser 6213c9a7c58SBodo Stroesser static void tcmu_setup_iovs(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 6223c9a7c58SBodo Stroesser struct iovec **iov, int data_length) 6233c9a7c58SBodo Stroesser { 6243c9a7c58SBodo Stroesser /* start value of dbi + 1 must not be a valid dbi */ 6253c9a7c58SBodo Stroesser int dbi = -2; 6263c9a7c58SBodo Stroesser 6273c9a7c58SBodo Stroesser /* We prepare the IOVs for DMA_FROM_DEVICE transfer direction */ 628e719afdcSBodo Stroesser for (; data_length > 0; data_length -= udev->data_blk_size) 629c8ed1ff8SBodo Stroesser dbi = new_block_to_iov(udev, cmd, iov, dbi, data_length); 6303c9a7c58SBodo Stroesser } 6313c9a7c58SBodo Stroesser 6327c9e7a6fSAndy Grover static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd) 6337c9e7a6fSAndy Grover { 6347c9e7a6fSAndy Grover struct se_device *se_dev = se_cmd->se_dev; 6357c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(se_dev); 6367c9e7a6fSAndy Grover struct tcmu_cmd *tcmu_cmd; 6377c9e7a6fSAndy Grover 6380eccce86SDamien Le Moal tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_NOIO); 6397c9e7a6fSAndy Grover if (!tcmu_cmd) 6407c9e7a6fSAndy Grover return NULL; 6417c9e7a6fSAndy Grover 642a94a2572SXiubo Li INIT_LIST_HEAD(&tcmu_cmd->queue_entry); 6437c9e7a6fSAndy Grover tcmu_cmd->se_cmd = se_cmd; 6447c9e7a6fSAndy Grover tcmu_cmd->tcmu_dev = udev; 6457c9e7a6fSAndy Grover 64652ef2743SBodo Stroesser tcmu_cmd_set_block_cnts(tcmu_cmd); 647141685a3SXiubo Li tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t), 6480eccce86SDamien Le Moal GFP_NOIO); 649141685a3SXiubo Li if (!tcmu_cmd->dbi) { 650141685a3SXiubo Li kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); 651141685a3SXiubo Li return NULL; 652141685a3SXiubo Li } 653141685a3SXiubo Li 6547c9e7a6fSAndy Grover return tcmu_cmd; 6557c9e7a6fSAndy Grover } 6567c9e7a6fSAndy Grover 6577c9e7a6fSAndy Grover static inline void tcmu_flush_dcache_range(void *vaddr, size_t size) 6587c9e7a6fSAndy Grover { 659b75d8063SGeliang Tang unsigned long offset = offset_in_page(vaddr); 66026d2b310Stangwenji void *start = vaddr - offset; 6617c9e7a6fSAndy Grover 6627c9e7a6fSAndy Grover size = round_up(size+offset, PAGE_SIZE); 6637c9e7a6fSAndy Grover 6647c9e7a6fSAndy Grover while (size) { 6653145550aSBodo Stroesser flush_dcache_page(vmalloc_to_page(start)); 66626d2b310Stangwenji start += PAGE_SIZE; 6677c9e7a6fSAndy Grover size -= PAGE_SIZE; 6687c9e7a6fSAndy Grover } 6697c9e7a6fSAndy Grover } 6707c9e7a6fSAndy Grover 6717c9e7a6fSAndy Grover /* 6727c9e7a6fSAndy Grover * Some ring helper functions. We don't assume size is a power of 2 so 6737c9e7a6fSAndy Grover * we can't use circ_buf.h. 6747c9e7a6fSAndy Grover */ 6757c9e7a6fSAndy Grover static inline size_t spc_used(size_t head, size_t tail, size_t size) 6767c9e7a6fSAndy Grover { 6777c9e7a6fSAndy Grover int diff = head - tail; 6787c9e7a6fSAndy Grover 6797c9e7a6fSAndy Grover if (diff >= 0) 6807c9e7a6fSAndy Grover return diff; 6817c9e7a6fSAndy Grover else 6827c9e7a6fSAndy Grover return size + diff; 6837c9e7a6fSAndy Grover } 6847c9e7a6fSAndy Grover 6857c9e7a6fSAndy Grover static inline size_t spc_free(size_t head, size_t tail, size_t size) 6867c9e7a6fSAndy Grover { 6877c9e7a6fSAndy Grover /* Keep 1 byte unused or we can't tell full from empty */ 6887c9e7a6fSAndy Grover return (size - spc_used(head, tail, size) - 1); 6897c9e7a6fSAndy Grover } 6907c9e7a6fSAndy Grover 6917c9e7a6fSAndy Grover static inline size_t head_to_end(size_t head, size_t size) 6927c9e7a6fSAndy Grover { 6937c9e7a6fSAndy Grover return size - head; 6947c9e7a6fSAndy Grover } 6957c9e7a6fSAndy Grover 6967c9e7a6fSAndy Grover #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size) 6977c9e7a6fSAndy Grover 698c8ed1ff8SBodo Stroesser #define TCMU_SG_TO_DATA_AREA 1 699c8ed1ff8SBodo Stroesser #define TCMU_DATA_AREA_TO_SG 2 700c8ed1ff8SBodo Stroesser 701c8ed1ff8SBodo Stroesser static inline void tcmu_copy_data(struct tcmu_dev *udev, 702c8ed1ff8SBodo Stroesser struct tcmu_cmd *tcmu_cmd, uint32_t direction, 703c8ed1ff8SBodo Stroesser struct scatterlist *sg, unsigned int sg_nents, 704c8ed1ff8SBodo Stroesser struct iovec **iov, size_t data_len) 705c8ed1ff8SBodo Stroesser { 706c8ed1ff8SBodo Stroesser /* start value of dbi + 1 must not be a valid dbi */ 707c8ed1ff8SBodo Stroesser int dbi = -2; 708f5ce815fSBodo Stroesser size_t page_remaining, cp_len; 709b4150b68SBodo Stroesser int page_cnt, page_inx, dpi; 710c8ed1ff8SBodo Stroesser struct sg_mapping_iter sg_iter; 711c8ed1ff8SBodo Stroesser unsigned int sg_flags; 712c8ed1ff8SBodo Stroesser struct page *page; 713c8ed1ff8SBodo Stroesser void *data_page_start, *data_addr; 714c8ed1ff8SBodo Stroesser 715c8ed1ff8SBodo Stroesser if (direction == TCMU_SG_TO_DATA_AREA) 716c8ed1ff8SBodo Stroesser sg_flags = SG_MITER_ATOMIC | SG_MITER_FROM_SG; 717c8ed1ff8SBodo Stroesser else 718c8ed1ff8SBodo Stroesser sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG; 719c8ed1ff8SBodo Stroesser sg_miter_start(&sg_iter, sg, sg_nents, sg_flags); 720c8ed1ff8SBodo Stroesser 721c8ed1ff8SBodo Stroesser while (data_len) { 722c8ed1ff8SBodo Stroesser if (direction == TCMU_SG_TO_DATA_AREA) 723c8ed1ff8SBodo Stroesser dbi = new_block_to_iov(udev, tcmu_cmd, iov, dbi, 724c8ed1ff8SBodo Stroesser data_len); 725c8ed1ff8SBodo Stroesser else 726c8ed1ff8SBodo Stroesser dbi = tcmu_cmd_get_dbi(tcmu_cmd); 727f5ce815fSBodo Stroesser 728f5ce815fSBodo Stroesser page_cnt = DIV_ROUND_UP(data_len, PAGE_SIZE); 729e719afdcSBodo Stroesser if (page_cnt > udev->data_pages_per_blk) 730e719afdcSBodo Stroesser page_cnt = udev->data_pages_per_blk; 731f5ce815fSBodo Stroesser 732b4150b68SBodo Stroesser dpi = dbi * udev->data_pages_per_blk; 733b4150b68SBodo Stroesser for (page_inx = 0; page_inx < page_cnt && data_len; 734b4150b68SBodo Stroesser page_inx++, dpi++) { 735b4150b68SBodo Stroesser page = xa_load(&udev->data_pages, dpi); 736f5ce815fSBodo Stroesser 737c8ed1ff8SBodo Stroesser if (direction == TCMU_DATA_AREA_TO_SG) 738c8ed1ff8SBodo Stroesser flush_dcache_page(page); 739c8ed1ff8SBodo Stroesser data_page_start = kmap_atomic(page); 740f5ce815fSBodo Stroesser page_remaining = PAGE_SIZE; 741c8ed1ff8SBodo Stroesser 742f5ce815fSBodo Stroesser while (page_remaining && data_len) { 743c8ed1ff8SBodo Stroesser if (!sg_miter_next(&sg_iter)) { 744c8ed1ff8SBodo Stroesser /* set length to 0 to abort outer loop */ 745c8ed1ff8SBodo Stroesser data_len = 0; 746f5ce815fSBodo Stroesser pr_debug("%s: aborting data copy due to exhausted sg_list\n", 747f5ce815fSBodo Stroesser __func__); 748c8ed1ff8SBodo Stroesser break; 749c8ed1ff8SBodo Stroesser } 750f5ce815fSBodo Stroesser cp_len = min3(sg_iter.length, page_remaining, 751f5ce815fSBodo Stroesser data_len); 752c8ed1ff8SBodo Stroesser 753c8ed1ff8SBodo Stroesser data_addr = data_page_start + 754f5ce815fSBodo Stroesser PAGE_SIZE - page_remaining; 755c8ed1ff8SBodo Stroesser if (direction == TCMU_SG_TO_DATA_AREA) 756c8ed1ff8SBodo Stroesser memcpy(data_addr, sg_iter.addr, cp_len); 757c8ed1ff8SBodo Stroesser else 758c8ed1ff8SBodo Stroesser memcpy(sg_iter.addr, data_addr, cp_len); 759c8ed1ff8SBodo Stroesser 760c8ed1ff8SBodo Stroesser data_len -= cp_len; 761f5ce815fSBodo Stroesser page_remaining -= cp_len; 762c8ed1ff8SBodo Stroesser sg_iter.consumed = cp_len; 763c8ed1ff8SBodo Stroesser } 764c8ed1ff8SBodo Stroesser sg_miter_stop(&sg_iter); 765c8ed1ff8SBodo Stroesser 766c8ed1ff8SBodo Stroesser kunmap_atomic(data_page_start); 767c8ed1ff8SBodo Stroesser if (direction == TCMU_SG_TO_DATA_AREA) 768c8ed1ff8SBodo Stroesser flush_dcache_page(page); 769c8ed1ff8SBodo Stroesser } 770c8ed1ff8SBodo Stroesser } 771f5ce815fSBodo Stroesser } 772c8ed1ff8SBodo Stroesser 7733c9a7c58SBodo Stroesser static void scatter_data_area(struct tcmu_dev *udev, struct tcmu_cmd *tcmu_cmd, 7743c9a7c58SBodo Stroesser struct iovec **iov) 77526418649SSheng Yang { 7763c9a7c58SBodo Stroesser struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 777f97ec7dbSIlias Tsitsimpis 778c8ed1ff8SBodo Stroesser tcmu_copy_data(udev, tcmu_cmd, TCMU_SG_TO_DATA_AREA, se_cmd->t_data_sg, 779c8ed1ff8SBodo Stroesser se_cmd->t_data_nents, iov, se_cmd->data_length); 7803c58f737SBodo Stroesser } 781b6df4b79SXiubo Li 782c8ed1ff8SBodo Stroesser static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *tcmu_cmd, 7836c3796d1Sbstroesser@ts.fujitsu.com bool bidi, uint32_t read_len) 784f97ec7dbSIlias Tsitsimpis { 785c8ed1ff8SBodo Stroesser struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 786c8ed1ff8SBodo Stroesser struct scatterlist *data_sg; 787a5d68ba8SXiubo Li unsigned int data_nents; 788a5d68ba8SXiubo Li 789a5d68ba8SXiubo Li if (!bidi) { 790a5d68ba8SXiubo Li data_sg = se_cmd->t_data_sg; 791a5d68ba8SXiubo Li data_nents = se_cmd->t_data_nents; 792a5d68ba8SXiubo Li } else { 793a5d68ba8SXiubo Li /* 794a5d68ba8SXiubo Li * For bidi case, the first count blocks are for Data-Out 795a5d68ba8SXiubo Li * buffer blocks, and before gathering the Data-In buffer 79652ef2743SBodo Stroesser * the Data-Out buffer blocks should be skipped. 797a5d68ba8SXiubo Li */ 798c8ed1ff8SBodo Stroesser tcmu_cmd_set_dbi_cur(tcmu_cmd, 799c8ed1ff8SBodo Stroesser tcmu_cmd->dbi_cnt - tcmu_cmd->dbi_bidi_cnt); 800a5d68ba8SXiubo Li 801a5d68ba8SXiubo Li data_sg = se_cmd->t_bidi_data_sg; 802a5d68ba8SXiubo Li data_nents = se_cmd->t_bidi_data_nents; 803a5d68ba8SXiubo Li } 804f97ec7dbSIlias Tsitsimpis 805c8ed1ff8SBodo Stroesser tcmu_copy_data(udev, tcmu_cmd, TCMU_DATA_AREA_TO_SG, data_sg, 806c8ed1ff8SBodo Stroesser data_nents, NULL, read_len); 807f97ec7dbSIlias Tsitsimpis } 808f97ec7dbSIlias Tsitsimpis 809b6df4b79SXiubo Li static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh) 81026418649SSheng Yang { 8113c0f26ffSMike Christie return thresh - bitmap_weight(bitmap, thresh); 81226418649SSheng Yang } 81326418649SSheng Yang 8147c9e7a6fSAndy Grover /* 8157e98905eSBodo Stroesser * We can't queue a command until we have space available on the cmd ring. 8167c9e7a6fSAndy Grover * 8177c9e7a6fSAndy Grover * Called with ring lock held. 8187c9e7a6fSAndy Grover */ 8197e98905eSBodo Stroesser static bool is_ring_space_avail(struct tcmu_dev *udev, size_t cmd_size) 8207c9e7a6fSAndy Grover { 8217c9e7a6fSAndy Grover struct tcmu_mailbox *mb = udev->mb_addr; 8220241fd39SNicholas Bellinger size_t space, cmd_needed; 8237c9e7a6fSAndy Grover u32 cmd_head; 8247c9e7a6fSAndy Grover 8257c9e7a6fSAndy Grover tcmu_flush_dcache_range(mb, sizeof(*mb)); 8267c9e7a6fSAndy Grover 8277c9e7a6fSAndy Grover cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 8287c9e7a6fSAndy Grover 829f56574a2SAndy Grover /* 830f56574a2SAndy Grover * If cmd end-of-ring space is too small then we need space for a NOP plus 831f56574a2SAndy Grover * original cmd - cmds are internally contiguous. 832f56574a2SAndy Grover */ 833f56574a2SAndy Grover if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size) 834f56574a2SAndy Grover cmd_needed = cmd_size; 835f56574a2SAndy Grover else 836f56574a2SAndy Grover cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size); 837f56574a2SAndy Grover 8387c9e7a6fSAndy Grover space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size); 8397c9e7a6fSAndy Grover if (space < cmd_needed) { 8407c9e7a6fSAndy Grover pr_debug("no cmd space: %u %u %u\n", cmd_head, 8417c9e7a6fSAndy Grover udev->cmdr_last_cleaned, udev->cmdr_size); 8427c9e7a6fSAndy Grover return false; 8437c9e7a6fSAndy Grover } 844bc2d214aSBodo Stroesser return true; 8457e98905eSBodo Stroesser } 8467e98905eSBodo Stroesser 8477e98905eSBodo Stroesser /* 8487e98905eSBodo Stroesser * We have to allocate data buffers before we can queue a command. 8497e98905eSBodo Stroesser * Returns -1 on error (not enough space) or number of needed iovs on success 8507e98905eSBodo Stroesser * 8517e98905eSBodo Stroesser * Called with ring lock held. 8527e98905eSBodo Stroesser */ 8537e98905eSBodo Stroesser static int tcmu_alloc_data_space(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 8547e98905eSBodo Stroesser int *iov_bidi_cnt) 8557e98905eSBodo Stroesser { 8567e98905eSBodo Stroesser int space, iov_cnt = 0, ret = 0; 8577e98905eSBodo Stroesser 8587e98905eSBodo Stroesser if (!cmd->dbi_cnt) 8597e98905eSBodo Stroesser goto wr_iov_cnts; 860bc2d214aSBodo Stroesser 861b6df4b79SXiubo Li /* try to check and get the data blocks as needed */ 862b6df4b79SXiubo Li space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh); 86352ef2743SBodo Stroesser if (space < cmd->dbi_cnt) { 86480eb8761SMike Christie unsigned long blocks_left = 86580eb8761SMike Christie (udev->max_blocks - udev->dbi_thresh) + space; 866b6df4b79SXiubo Li 86752ef2743SBodo Stroesser if (blocks_left < cmd->dbi_cnt) { 868e719afdcSBodo Stroesser pr_debug("no data space: only %lu available, but ask for %u\n", 869e719afdcSBodo Stroesser blocks_left * udev->data_blk_size, 870e719afdcSBodo Stroesser cmd->dbi_cnt * udev->data_blk_size); 8717e98905eSBodo Stroesser return -1; 8727c9e7a6fSAndy Grover } 8737c9e7a6fSAndy Grover 87452ef2743SBodo Stroesser udev->dbi_thresh += cmd->dbi_cnt; 87580eb8761SMike Christie if (udev->dbi_thresh > udev->max_blocks) 87680eb8761SMike Christie udev->dbi_thresh = udev->max_blocks; 877b6df4b79SXiubo Li } 878b6df4b79SXiubo Li 879f5ce815fSBodo Stroesser iov_cnt = tcmu_get_empty_blocks(udev, cmd, cmd->se_cmd->data_length); 8807e98905eSBodo Stroesser if (iov_cnt < 0) 8817e98905eSBodo Stroesser return -1; 8827e98905eSBodo Stroesser 8837e98905eSBodo Stroesser if (cmd->dbi_bidi_cnt) { 884f5ce815fSBodo Stroesser ret = tcmu_get_empty_blocks(udev, cmd, cmd->data_len_bidi); 8857e98905eSBodo Stroesser if (ret < 0) 8867e98905eSBodo Stroesser return -1; 8877e98905eSBodo Stroesser } 8887e98905eSBodo Stroesser wr_iov_cnts: 8897e98905eSBodo Stroesser *iov_bidi_cnt = ret; 8907e98905eSBodo Stroesser return iov_cnt + ret; 8917c9e7a6fSAndy Grover } 8927c9e7a6fSAndy Grover 893fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt) 894fe25cc34SXiubo Li { 895fe25cc34SXiubo Li return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]), 896fe25cc34SXiubo Li sizeof(struct tcmu_cmd_entry)); 897fe25cc34SXiubo Li } 898fe25cc34SXiubo Li 899fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd, 900fe25cc34SXiubo Li size_t base_command_size) 901fe25cc34SXiubo Li { 902fe25cc34SXiubo Li struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 903fe25cc34SXiubo Li size_t command_size; 904fe25cc34SXiubo Li 905fe25cc34SXiubo Li command_size = base_command_size + 906fe25cc34SXiubo Li round_up(scsi_command_size(se_cmd->t_task_cdb), 907fe25cc34SXiubo Li TCMU_OP_ALIGN_SIZE); 908fe25cc34SXiubo Li 909fe25cc34SXiubo Li WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1)); 910fe25cc34SXiubo Li 911fe25cc34SXiubo Li return command_size; 912fe25cc34SXiubo Li } 913fe25cc34SXiubo Li 91461fb2482SBodo Stroesser static void tcmu_setup_cmd_timer(struct tcmu_cmd *tcmu_cmd, unsigned int tmo, 9159103575aSMike Christie struct timer_list *timer) 9160d44374cSMike Christie { 9179103575aSMike Christie if (!tmo) 91861fb2482SBodo Stroesser return; 9199103575aSMike Christie 9200d44374cSMike Christie tcmu_cmd->deadline = round_jiffies_up(jiffies + msecs_to_jiffies(tmo)); 921a94a2572SXiubo Li if (!timer_pending(timer)) 9229103575aSMike Christie mod_timer(timer, tcmu_cmd->deadline); 923a94a2572SXiubo Li 92461fb2482SBodo Stroesser pr_debug("Timeout set up for cmd %p, dev = %s, tmo = %lu\n", tcmu_cmd, 92561fb2482SBodo Stroesser tcmu_cmd->tcmu_dev->name, tmo / MSEC_PER_SEC); 9260d44374cSMike Christie } 9270d44374cSMike Christie 928a94a2572SXiubo Li static int add_to_qfull_queue(struct tcmu_cmd *tcmu_cmd) 929af1dd7ffSMike Christie { 930af1dd7ffSMike Christie struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 9319103575aSMike Christie unsigned int tmo; 932af1dd7ffSMike Christie 9339103575aSMike Christie /* 9349103575aSMike Christie * For backwards compat if qfull_time_out is not set use 9359103575aSMike Christie * cmd_time_out and if that's not set use the default time out. 9369103575aSMike Christie */ 9379103575aSMike Christie if (!udev->qfull_time_out) 9389103575aSMike Christie return -ETIMEDOUT; 9399103575aSMike Christie else if (udev->qfull_time_out > 0) 9409103575aSMike Christie tmo = udev->qfull_time_out; 9419103575aSMike Christie else if (udev->cmd_time_out) 9429103575aSMike Christie tmo = udev->cmd_time_out; 9439103575aSMike Christie else 9449103575aSMike Christie tmo = TCMU_TIME_OUT; 9459103575aSMike Christie 94661fb2482SBodo Stroesser tcmu_setup_cmd_timer(tcmu_cmd, tmo, &udev->qfull_timer); 947af1dd7ffSMike Christie 948a94a2572SXiubo Li list_add_tail(&tcmu_cmd->queue_entry, &udev->qfull_queue); 94961fb2482SBodo Stroesser pr_debug("adding cmd %p on dev %s to ring space wait queue\n", 95061fb2482SBodo Stroesser tcmu_cmd, udev->name); 951af1dd7ffSMike Christie return 0; 952af1dd7ffSMike Christie } 953af1dd7ffSMike Christie 9543d3f9d56SBodo Stroesser static uint32_t ring_insert_padding(struct tcmu_dev *udev, size_t cmd_size) 9553d3f9d56SBodo Stroesser { 9563d3f9d56SBodo Stroesser struct tcmu_cmd_entry_hdr *hdr; 9573d3f9d56SBodo Stroesser struct tcmu_mailbox *mb = udev->mb_addr; 9583d3f9d56SBodo Stroesser uint32_t cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 9593d3f9d56SBodo Stroesser 9603d3f9d56SBodo Stroesser /* Insert a PAD if end-of-ring space is too small */ 9613d3f9d56SBodo Stroesser if (head_to_end(cmd_head, udev->cmdr_size) < cmd_size) { 9623d3f9d56SBodo Stroesser size_t pad_size = head_to_end(cmd_head, udev->cmdr_size); 9633d3f9d56SBodo Stroesser 964ecddbb7eSBodo Stroesser hdr = udev->cmdr + cmd_head; 9653d3f9d56SBodo Stroesser tcmu_hdr_set_op(&hdr->len_op, TCMU_OP_PAD); 9663d3f9d56SBodo Stroesser tcmu_hdr_set_len(&hdr->len_op, pad_size); 9673d3f9d56SBodo Stroesser hdr->cmd_id = 0; /* not used for PAD */ 9683d3f9d56SBodo Stroesser hdr->kflags = 0; 9693d3f9d56SBodo Stroesser hdr->uflags = 0; 9703d3f9d56SBodo Stroesser tcmu_flush_dcache_range(hdr, sizeof(*hdr)); 9713d3f9d56SBodo Stroesser 9723d3f9d56SBodo Stroesser UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size); 9733d3f9d56SBodo Stroesser tcmu_flush_dcache_range(mb, sizeof(*mb)); 9743d3f9d56SBodo Stroesser 9753d3f9d56SBodo Stroesser cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 9763d3f9d56SBodo Stroesser WARN_ON(cmd_head != 0); 9773d3f9d56SBodo Stroesser } 9783d3f9d56SBodo Stroesser 9793d3f9d56SBodo Stroesser return cmd_head; 9803d3f9d56SBodo Stroesser } 9813d3f9d56SBodo Stroesser 9826888da81SMike Christie static void tcmu_unplug_device(struct se_dev_plug *se_plug) 9836888da81SMike Christie { 9846888da81SMike Christie struct se_device *se_dev = se_plug->se_dev; 9856888da81SMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 9866888da81SMike Christie 9873ac0fcb4SBodo Stroesser clear_bit(TCMU_DEV_BIT_PLUGGED, &udev->flags); 9886888da81SMike Christie uio_event_notify(&udev->uio_info); 9896888da81SMike Christie } 9906888da81SMike Christie 9916888da81SMike Christie static struct se_dev_plug *tcmu_plug_device(struct se_device *se_dev) 9926888da81SMike Christie { 9936888da81SMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 9946888da81SMike Christie 9953ac0fcb4SBodo Stroesser if (!test_and_set_bit(TCMU_DEV_BIT_PLUGGED, &udev->flags)) 9966888da81SMike Christie return &udev->se_plug; 9976888da81SMike Christie 9986888da81SMike Christie return NULL; 9996888da81SMike Christie } 10006888da81SMike Christie 10016fd0ce79SMike Christie /** 10026fd0ce79SMike Christie * queue_cmd_ring - queue cmd to ring or internally 10036fd0ce79SMike Christie * @tcmu_cmd: cmd to queue 10046fd0ce79SMike Christie * @scsi_err: TCM error code if failure (-1) returned. 10056fd0ce79SMike Christie * 10066fd0ce79SMike Christie * Returns: 10076fd0ce79SMike Christie * -1 we cannot queue internally or to the ring. 10086fd0ce79SMike Christie * 0 success 1009af1dd7ffSMike Christie * 1 internally queued to wait for ring memory to free. 10106fd0ce79SMike Christie */ 1011e7f41104SBart Van Assche static int queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, sense_reason_t *scsi_err) 10127c9e7a6fSAndy Grover { 10137c9e7a6fSAndy Grover struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 10147c9e7a6fSAndy Grover struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 10157c9e7a6fSAndy Grover size_t base_command_size, command_size; 10163d3f9d56SBodo Stroesser struct tcmu_mailbox *mb = udev->mb_addr; 10177c9e7a6fSAndy Grover struct tcmu_cmd_entry *entry; 10187c9e7a6fSAndy Grover struct iovec *iov; 1019d3cbb743SBodo Stroesser int iov_cnt, iov_bidi_cnt; 1020d3cbb743SBodo Stroesser uint32_t cmd_id, cmd_head; 10217c9e7a6fSAndy Grover uint64_t cdb_off; 1022e719afdcSBodo Stroesser uint32_t blk_size = udev->data_blk_size; 102352ef2743SBodo Stroesser /* size of data buffer needed */ 1024e719afdcSBodo Stroesser size_t data_length = (size_t)tcmu_cmd->dbi_cnt * blk_size; 10257c9e7a6fSAndy Grover 10266fd0ce79SMike Christie *scsi_err = TCM_NO_SENSE; 10276fd0ce79SMike Christie 1028892782caSMike Christie if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) { 1029892782caSMike Christie *scsi_err = TCM_LUN_BUSY; 1030892782caSMike Christie return -1; 1031892782caSMike Christie } 1032892782caSMike Christie 10336fd0ce79SMike Christie if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) { 10346fd0ce79SMike Christie *scsi_err = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 10356fd0ce79SMike Christie return -1; 10366fd0ce79SMike Christie } 10377c9e7a6fSAndy Grover 1038a94a2572SXiubo Li if (!list_empty(&udev->qfull_queue)) 1039af1dd7ffSMike Christie goto queue; 10407c9e7a6fSAndy Grover 1041e719afdcSBodo Stroesser if (data_length > (size_t)udev->max_blocks * blk_size) { 10427e98905eSBodo Stroesser pr_warn("TCMU: Request of size %zu is too big for %zu data area\n", 1043e719afdcSBodo Stroesser data_length, (size_t)udev->max_blocks * blk_size); 10446fd0ce79SMike Christie *scsi_err = TCM_INVALID_CDB_FIELD; 10456fd0ce79SMike Christie return -1; 1046554617b2SAndy Grover } 10477c9e7a6fSAndy Grover 10487e98905eSBodo Stroesser iov_cnt = tcmu_alloc_data_space(udev, tcmu_cmd, &iov_bidi_cnt); 10497e98905eSBodo Stroesser if (iov_cnt < 0) 10507e98905eSBodo Stroesser goto free_and_queue; 10517e98905eSBodo Stroesser 10527e98905eSBodo Stroesser /* 10537e98905eSBodo Stroesser * Must be a certain minimum size for response sense info, but 10547e98905eSBodo Stroesser * also may be larger if the iov array is large. 10557e98905eSBodo Stroesser */ 10567e98905eSBodo Stroesser base_command_size = tcmu_cmd_get_base_cmd_size(iov_cnt); 10577e98905eSBodo Stroesser command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size); 10587e98905eSBodo Stroesser 10597e98905eSBodo Stroesser if (command_size > (udev->cmdr_size / 2)) { 10607e98905eSBodo Stroesser pr_warn("TCMU: Request of size %zu is too big for %u cmd ring\n", 10617e98905eSBodo Stroesser command_size, udev->cmdr_size); 10627e98905eSBodo Stroesser tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur); 10637e98905eSBodo Stroesser *scsi_err = TCM_INVALID_CDB_FIELD; 10647e98905eSBodo Stroesser return -1; 10657e98905eSBodo Stroesser } 10667e98905eSBodo Stroesser 10677e98905eSBodo Stroesser if (!is_ring_space_avail(udev, command_size)) 1068810b8153SMike Christie /* 1069810b8153SMike Christie * Don't leave commands partially setup because the unmap 1070810b8153SMike Christie * thread might need the blocks to make forward progress. 1071810b8153SMike Christie */ 10727e98905eSBodo Stroesser goto free_and_queue; 107326418649SSheng Yang 1074d3cbb743SBodo Stroesser if (xa_alloc(&udev->commands, &cmd_id, tcmu_cmd, XA_LIMIT(1, 0xffff), 1075d3cbb743SBodo Stroesser GFP_NOWAIT) < 0) { 107661fb2482SBodo Stroesser pr_err("tcmu: Could not allocate cmd id.\n"); 10776fd0ce79SMike Christie 107861fb2482SBodo Stroesser tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt); 10796fd0ce79SMike Christie *scsi_err = TCM_OUT_OF_RESOURCES; 10806fd0ce79SMike Christie return -1; 10810d44374cSMike Christie } 108261fb2482SBodo Stroesser tcmu_cmd->cmd_id = cmd_id; 108361fb2482SBodo Stroesser 108461fb2482SBodo Stroesser pr_debug("allocated cmd id %u for cmd %p dev %s\n", tcmu_cmd->cmd_id, 108561fb2482SBodo Stroesser tcmu_cmd, udev->name); 108661fb2482SBodo Stroesser 10877e98905eSBodo Stroesser cmd_head = ring_insert_padding(udev, command_size); 10887e98905eSBodo Stroesser 1089ecddbb7eSBodo Stroesser entry = udev->cmdr + cmd_head; 10907e98905eSBodo Stroesser memset(entry, 0, command_size); 10917e98905eSBodo Stroesser tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD); 10927e98905eSBodo Stroesser 10937e98905eSBodo Stroesser /* prepare iov list and copy data to data area if necessary */ 10947e98905eSBodo Stroesser tcmu_cmd_reset_dbi_cur(tcmu_cmd); 10957e98905eSBodo Stroesser iov = &entry->req.iov[0]; 10963c9a7c58SBodo Stroesser 10973c9a7c58SBodo Stroesser if (se_cmd->data_direction == DMA_TO_DEVICE || 10983c9a7c58SBodo Stroesser se_cmd->se_cmd_flags & SCF_BIDI) 10993c9a7c58SBodo Stroesser scatter_data_area(udev, tcmu_cmd, &iov); 11003c9a7c58SBodo Stroesser else 11013c9a7c58SBodo Stroesser tcmu_setup_iovs(udev, tcmu_cmd, &iov, se_cmd->data_length); 11023c9a7c58SBodo Stroesser 11037e98905eSBodo Stroesser entry->req.iov_cnt = iov_cnt - iov_bidi_cnt; 11047e98905eSBodo Stroesser 11057e98905eSBodo Stroesser /* Handle BIDI commands */ 11067e98905eSBodo Stroesser if (se_cmd->se_cmd_flags & SCF_BIDI) { 11077e98905eSBodo Stroesser iov++; 11083c9a7c58SBodo Stroesser tcmu_setup_iovs(udev, tcmu_cmd, &iov, tcmu_cmd->data_len_bidi); 11097e98905eSBodo Stroesser entry->req.iov_bidi_cnt = iov_bidi_cnt; 11107e98905eSBodo Stroesser } 11117e98905eSBodo Stroesser 111261fb2482SBodo Stroesser tcmu_setup_cmd_timer(tcmu_cmd, udev->cmd_time_out, &udev->cmd_timer); 111361fb2482SBodo Stroesser 11140d44374cSMike Christie entry->hdr.cmd_id = tcmu_cmd->cmd_id; 11150d44374cSMike Christie 1116fe25cc34SXiubo Li tcmu_hdr_set_len(&entry->hdr.len_op, command_size); 1117fe25cc34SXiubo Li 11187c9e7a6fSAndy Grover /* All offsets relative to mb_addr, not start of entry! */ 11197c9e7a6fSAndy Grover cdb_off = CMDR_OFF + cmd_head + base_command_size; 11207c9e7a6fSAndy Grover memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb)); 11217c9e7a6fSAndy Grover entry->req.cdb_off = cdb_off; 11228c4e0f21SBodo Stroesser tcmu_flush_dcache_range(entry, command_size); 11237c9e7a6fSAndy Grover 11247c9e7a6fSAndy Grover UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size); 11257c9e7a6fSAndy Grover tcmu_flush_dcache_range(mb, sizeof(*mb)); 11267c9e7a6fSAndy Grover 1127a94a2572SXiubo Li list_add_tail(&tcmu_cmd->queue_entry, &udev->inflight_queue); 1128a94a2572SXiubo Li 11293ac0fcb4SBodo Stroesser if (!test_bit(TCMU_DEV_BIT_PLUGGED, &udev->flags)) 11307c9e7a6fSAndy Grover uio_event_notify(&udev->uio_info); 11317c9e7a6fSAndy Grover 11326fd0ce79SMike Christie return 0; 1133af1dd7ffSMike Christie 11347e98905eSBodo Stroesser free_and_queue: 11357e98905eSBodo Stroesser tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur); 11367e98905eSBodo Stroesser tcmu_cmd_reset_dbi_cur(tcmu_cmd); 11377e98905eSBodo Stroesser 1138af1dd7ffSMike Christie queue: 1139a94a2572SXiubo Li if (add_to_qfull_queue(tcmu_cmd)) { 1140af1dd7ffSMike Christie *scsi_err = TCM_OUT_OF_RESOURCES; 1141af1dd7ffSMike Christie return -1; 1142af1dd7ffSMike Christie } 1143af1dd7ffSMike Christie 1144af1dd7ffSMike Christie return 1; 11457c9e7a6fSAndy Grover } 11467c9e7a6fSAndy Grover 1147bc2d214aSBodo Stroesser /** 1148bc2d214aSBodo Stroesser * queue_tmr_ring - queue tmr info to ring or internally 1149bc2d214aSBodo Stroesser * @udev: related tcmu_dev 1150bc2d214aSBodo Stroesser * @tmr: tcmu_tmr containing tmr info to queue 1151bc2d214aSBodo Stroesser * 1152bc2d214aSBodo Stroesser * Returns: 1153bc2d214aSBodo Stroesser * 0 success 1154bc2d214aSBodo Stroesser * 1 internally queued to wait for ring memory to free. 1155bc2d214aSBodo Stroesser */ 1156bc2d214aSBodo Stroesser static int 1157bc2d214aSBodo Stroesser queue_tmr_ring(struct tcmu_dev *udev, struct tcmu_tmr *tmr) 1158bc2d214aSBodo Stroesser { 1159bc2d214aSBodo Stroesser struct tcmu_tmr_entry *entry; 1160bc2d214aSBodo Stroesser int cmd_size; 1161bc2d214aSBodo Stroesser int id_list_sz; 1162bc2d214aSBodo Stroesser struct tcmu_mailbox *mb = udev->mb_addr; 1163bc2d214aSBodo Stroesser uint32_t cmd_head; 1164bc2d214aSBodo Stroesser 1165bc2d214aSBodo Stroesser if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) 1166bc2d214aSBodo Stroesser goto out_free; 1167bc2d214aSBodo Stroesser 1168bc2d214aSBodo Stroesser id_list_sz = sizeof(tmr->tmr_cmd_ids[0]) * tmr->tmr_cmd_cnt; 1169bc2d214aSBodo Stroesser cmd_size = round_up(sizeof(*entry) + id_list_sz, TCMU_OP_ALIGN_SIZE); 1170bc2d214aSBodo Stroesser 1171bc2d214aSBodo Stroesser if (!list_empty(&udev->tmr_queue) || 11727e98905eSBodo Stroesser !is_ring_space_avail(udev, cmd_size)) { 1173bc2d214aSBodo Stroesser list_add_tail(&tmr->queue_entry, &udev->tmr_queue); 1174bc2d214aSBodo Stroesser pr_debug("adding tmr %p on dev %s to TMR ring space wait queue\n", 1175bc2d214aSBodo Stroesser tmr, udev->name); 1176bc2d214aSBodo Stroesser return 1; 1177bc2d214aSBodo Stroesser } 1178bc2d214aSBodo Stroesser 1179bc2d214aSBodo Stroesser cmd_head = ring_insert_padding(udev, cmd_size); 1180bc2d214aSBodo Stroesser 1181ecddbb7eSBodo Stroesser entry = udev->cmdr + cmd_head; 1182bc2d214aSBodo Stroesser memset(entry, 0, cmd_size); 1183bc2d214aSBodo Stroesser tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_TMR); 1184bc2d214aSBodo Stroesser tcmu_hdr_set_len(&entry->hdr.len_op, cmd_size); 1185bc2d214aSBodo Stroesser entry->tmr_type = tmr->tmr_type; 1186bc2d214aSBodo Stroesser entry->cmd_cnt = tmr->tmr_cmd_cnt; 1187bc2d214aSBodo Stroesser memcpy(&entry->cmd_ids[0], &tmr->tmr_cmd_ids[0], id_list_sz); 1188bc2d214aSBodo Stroesser tcmu_flush_dcache_range(entry, cmd_size); 1189bc2d214aSBodo Stroesser 1190bc2d214aSBodo Stroesser UPDATE_HEAD(mb->cmd_head, cmd_size, udev->cmdr_size); 1191bc2d214aSBodo Stroesser tcmu_flush_dcache_range(mb, sizeof(*mb)); 1192bc2d214aSBodo Stroesser 1193bc2d214aSBodo Stroesser uio_event_notify(&udev->uio_info); 1194bc2d214aSBodo Stroesser 1195bc2d214aSBodo Stroesser out_free: 1196bc2d214aSBodo Stroesser kfree(tmr); 1197bc2d214aSBodo Stroesser 1198bc2d214aSBodo Stroesser return 0; 1199bc2d214aSBodo Stroesser } 1200bc2d214aSBodo Stroesser 120102eb924fSAndy Grover static sense_reason_t 120202eb924fSAndy Grover tcmu_queue_cmd(struct se_cmd *se_cmd) 12037c9e7a6fSAndy Grover { 1204af1dd7ffSMike Christie struct se_device *se_dev = se_cmd->se_dev; 1205af1dd7ffSMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 12067c9e7a6fSAndy Grover struct tcmu_cmd *tcmu_cmd; 1207c9684927SBodo Stroesser sense_reason_t scsi_ret = TCM_CHECK_CONDITION_ABORT_CMD; 1208c9684927SBodo Stroesser int ret = -1; 12097c9e7a6fSAndy Grover 12107c9e7a6fSAndy Grover tcmu_cmd = tcmu_alloc_cmd(se_cmd); 12117c9e7a6fSAndy Grover if (!tcmu_cmd) 121202eb924fSAndy Grover return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 12137c9e7a6fSAndy Grover 1214af1dd7ffSMike Christie mutex_lock(&udev->cmdr_lock); 1215c9684927SBodo Stroesser if (!(se_cmd->transport_state & CMD_T_ABORTED)) 1216af1dd7ffSMike Christie ret = queue_cmd_ring(tcmu_cmd, &scsi_ret); 1217af1dd7ffSMike Christie if (ret < 0) 1218141685a3SXiubo Li tcmu_free_cmd(tcmu_cmd); 1219780e1384SShin'ichiro Kawasaki else 1220780e1384SShin'ichiro Kawasaki se_cmd->priv = tcmu_cmd; 1221a3512902SBodo Stroesser mutex_unlock(&udev->cmdr_lock); 12226fd0ce79SMike Christie return scsi_ret; 12237c9e7a6fSAndy Grover } 12247c9e7a6fSAndy Grover 1225ed212ca8SBodo Stroesser static void tcmu_set_next_deadline(struct list_head *queue, 1226ed212ca8SBodo Stroesser struct timer_list *timer) 1227ed212ca8SBodo Stroesser { 1228ed212ca8SBodo Stroesser struct tcmu_cmd *cmd; 1229ed212ca8SBodo Stroesser 1230ed212ca8SBodo Stroesser if (!list_empty(queue)) { 1231ed212ca8SBodo Stroesser cmd = list_first_entry(queue, struct tcmu_cmd, queue_entry); 1232ed212ca8SBodo Stroesser mod_timer(timer, cmd->deadline); 1233ed212ca8SBodo Stroesser } else 1234ed212ca8SBodo Stroesser del_timer(timer); 1235ed212ca8SBodo Stroesser } 1236ed212ca8SBodo Stroesser 1237bc2d214aSBodo Stroesser static int 1238bc2d214aSBodo Stroesser tcmu_tmr_type(enum tcm_tmreq_table tmf) 1239bc2d214aSBodo Stroesser { 1240bc2d214aSBodo Stroesser switch (tmf) { 1241bc2d214aSBodo Stroesser case TMR_ABORT_TASK: return TCMU_TMR_ABORT_TASK; 1242bc2d214aSBodo Stroesser case TMR_ABORT_TASK_SET: return TCMU_TMR_ABORT_TASK_SET; 1243bc2d214aSBodo Stroesser case TMR_CLEAR_ACA: return TCMU_TMR_CLEAR_ACA; 1244bc2d214aSBodo Stroesser case TMR_CLEAR_TASK_SET: return TCMU_TMR_CLEAR_TASK_SET; 1245bc2d214aSBodo Stroesser case TMR_LUN_RESET: return TCMU_TMR_LUN_RESET; 1246bc2d214aSBodo Stroesser case TMR_TARGET_WARM_RESET: return TCMU_TMR_TARGET_WARM_RESET; 1247bc2d214aSBodo Stroesser case TMR_TARGET_COLD_RESET: return TCMU_TMR_TARGET_COLD_RESET; 1248bc2d214aSBodo Stroesser case TMR_LUN_RESET_PRO: return TCMU_TMR_LUN_RESET_PRO; 1249bc2d214aSBodo Stroesser default: return TCMU_TMR_UNKNOWN; 1250bc2d214aSBodo Stroesser } 1251bc2d214aSBodo Stroesser } 1252bc2d214aSBodo Stroesser 1253bc2d214aSBodo Stroesser static void 1254bc2d214aSBodo Stroesser tcmu_tmr_notify(struct se_device *se_dev, enum tcm_tmreq_table tmf, 1255bc2d214aSBodo Stroesser struct list_head *cmd_list) 1256bc2d214aSBodo Stroesser { 1257bc2d214aSBodo Stroesser int i = 0, cmd_cnt = 0; 1258bc2d214aSBodo Stroesser bool unqueued = false; 1259bc2d214aSBodo Stroesser struct tcmu_cmd *cmd; 1260bc2d214aSBodo Stroesser struct se_cmd *se_cmd; 1261bc2d214aSBodo Stroesser struct tcmu_tmr *tmr; 1262bc2d214aSBodo Stroesser struct tcmu_dev *udev = TCMU_DEV(se_dev); 1263bc2d214aSBodo Stroesser 1264bc2d214aSBodo Stroesser mutex_lock(&udev->cmdr_lock); 1265bc2d214aSBodo Stroesser 1266bc2d214aSBodo Stroesser /* First we check for aborted commands in qfull_queue */ 1267bc2d214aSBodo Stroesser list_for_each_entry(se_cmd, cmd_list, state_list) { 1268bc2d214aSBodo Stroesser i++; 1269bc2d214aSBodo Stroesser if (!se_cmd->priv) 1270bc2d214aSBodo Stroesser continue; 1271bc2d214aSBodo Stroesser cmd = se_cmd->priv; 1272bc2d214aSBodo Stroesser /* Commands on qfull queue have no id yet */ 1273bc2d214aSBodo Stroesser if (cmd->cmd_id) { 1274bc2d214aSBodo Stroesser cmd_cnt++; 1275bc2d214aSBodo Stroesser continue; 1276bc2d214aSBodo Stroesser } 1277bc2d214aSBodo Stroesser pr_debug("Removing aborted command %p from queue on dev %s.\n", 1278bc2d214aSBodo Stroesser cmd, udev->name); 1279bc2d214aSBodo Stroesser 1280bc2d214aSBodo Stroesser list_del_init(&cmd->queue_entry); 1281bc2d214aSBodo Stroesser tcmu_free_cmd(cmd); 1282780e1384SShin'ichiro Kawasaki se_cmd->priv = NULL; 1283bc2d214aSBodo Stroesser target_complete_cmd(se_cmd, SAM_STAT_TASK_ABORTED); 1284bc2d214aSBodo Stroesser unqueued = true; 1285bc2d214aSBodo Stroesser } 1286bc2d214aSBodo Stroesser if (unqueued) 1287bc2d214aSBodo Stroesser tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer); 1288bc2d214aSBodo Stroesser 128959526d7aSBodo Stroesser if (!test_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags)) 129059526d7aSBodo Stroesser goto unlock; 129159526d7aSBodo Stroesser 1292bc2d214aSBodo Stroesser pr_debug("TMR event %d on dev %s, aborted cmds %d, afflicted cmd_ids %d\n", 1293bc2d214aSBodo Stroesser tcmu_tmr_type(tmf), udev->name, i, cmd_cnt); 1294bc2d214aSBodo Stroesser 1295c20bda34SGustavo A. R. Silva tmr = kmalloc(struct_size(tmr, tmr_cmd_ids, cmd_cnt), GFP_NOIO); 1296bc2d214aSBodo Stroesser if (!tmr) 1297bc2d214aSBodo Stroesser goto unlock; 1298bc2d214aSBodo Stroesser 1299bc2d214aSBodo Stroesser tmr->tmr_type = tcmu_tmr_type(tmf); 1300bc2d214aSBodo Stroesser tmr->tmr_cmd_cnt = cmd_cnt; 1301bc2d214aSBodo Stroesser 1302bc2d214aSBodo Stroesser if (cmd_cnt != 0) { 1303bc2d214aSBodo Stroesser cmd_cnt = 0; 1304bc2d214aSBodo Stroesser list_for_each_entry(se_cmd, cmd_list, state_list) { 1305bc2d214aSBodo Stroesser if (!se_cmd->priv) 1306bc2d214aSBodo Stroesser continue; 1307bc2d214aSBodo Stroesser cmd = se_cmd->priv; 1308bc2d214aSBodo Stroesser if (cmd->cmd_id) 1309bc2d214aSBodo Stroesser tmr->tmr_cmd_ids[cmd_cnt++] = cmd->cmd_id; 1310bc2d214aSBodo Stroesser } 1311bc2d214aSBodo Stroesser } 1312bc2d214aSBodo Stroesser 1313bc2d214aSBodo Stroesser queue_tmr_ring(udev, tmr); 1314bc2d214aSBodo Stroesser 1315bc2d214aSBodo Stroesser unlock: 1316bc2d214aSBodo Stroesser mutex_unlock(&udev->cmdr_lock); 1317bc2d214aSBodo Stroesser } 1318bc2d214aSBodo Stroesser 1319018c1491SBodo Stroesser static bool tcmu_handle_completion(struct tcmu_cmd *cmd, 1320018c1491SBodo Stroesser struct tcmu_cmd_entry *entry, bool keep_buf) 13217c9e7a6fSAndy Grover { 13227c9e7a6fSAndy Grover struct se_cmd *se_cmd = cmd->se_cmd; 13237c9e7a6fSAndy Grover struct tcmu_dev *udev = cmd->tcmu_dev; 13246c3796d1Sbstroesser@ts.fujitsu.com bool read_len_valid = false; 1325018c1491SBodo Stroesser bool ret = true; 1326a86a7586SDmitry Fomichev uint32_t read_len; 13277c9e7a6fSAndy Grover 1328b25c7863SSheng Yang /* 1329b25c7863SSheng Yang * cmd has been completed already from timeout, just reclaim 13303d9b9555SAndy Grover * data area space and free cmd 1331b25c7863SSheng Yang */ 1332a86a7586SDmitry Fomichev if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { 1333a86a7586SDmitry Fomichev WARN_ON_ONCE(se_cmd); 1334141685a3SXiubo Li goto out; 1335a86a7586SDmitry Fomichev } 1336018c1491SBodo Stroesser if (test_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags)) { 1337018c1491SBodo Stroesser pr_err("cmd_id %u already completed with KEEP_BUF, ring is broken\n", 1338018c1491SBodo Stroesser entry->hdr.cmd_id); 1339018c1491SBodo Stroesser set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags); 1340018c1491SBodo Stroesser ret = false; 1341018c1491SBodo Stroesser goto out; 1342018c1491SBodo Stroesser } 1343b25c7863SSheng Yang 1344a94a2572SXiubo Li list_del_init(&cmd->queue_entry); 1345a94a2572SXiubo Li 1346141685a3SXiubo Li tcmu_cmd_reset_dbi_cur(cmd); 13477c9e7a6fSAndy Grover 13480ad46af8SAndy Grover if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) { 13490ad46af8SAndy Grover pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n", 13500ad46af8SAndy Grover cmd->se_cmd); 1351ed97d0cdSAndy Grover entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION; 13526c3796d1Sbstroesser@ts.fujitsu.com goto done; 13536c3796d1Sbstroesser@ts.fujitsu.com } 13546c3796d1Sbstroesser@ts.fujitsu.com 1355a86a7586SDmitry Fomichev read_len = se_cmd->data_length; 13566c3796d1Sbstroesser@ts.fujitsu.com if (se_cmd->data_direction == DMA_FROM_DEVICE && 13576c3796d1Sbstroesser@ts.fujitsu.com (entry->hdr.uflags & TCMU_UFLAG_READ_LEN) && entry->rsp.read_len) { 13586c3796d1Sbstroesser@ts.fujitsu.com read_len_valid = true; 13596c3796d1Sbstroesser@ts.fujitsu.com if (entry->rsp.read_len < read_len) 13606c3796d1Sbstroesser@ts.fujitsu.com read_len = entry->rsp.read_len; 13616c3796d1Sbstroesser@ts.fujitsu.com } 13626c3796d1Sbstroesser@ts.fujitsu.com 13636c3796d1Sbstroesser@ts.fujitsu.com if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) { 1364406f74c2SMike Christie transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer); 13656c3796d1Sbstroesser@ts.fujitsu.com if (!read_len_valid ) 13666c3796d1Sbstroesser@ts.fujitsu.com goto done; 13676c3796d1Sbstroesser@ts.fujitsu.com else 13686c3796d1Sbstroesser@ts.fujitsu.com se_cmd->se_cmd_flags |= SCF_TREAT_READ_AS_NORMAL; 13696c3796d1Sbstroesser@ts.fujitsu.com } 13706c3796d1Sbstroesser@ts.fujitsu.com if (se_cmd->se_cmd_flags & SCF_BIDI) { 137126418649SSheng Yang /* Get Data-In buffer before clean up */ 13726c3796d1Sbstroesser@ts.fujitsu.com gather_data_area(udev, cmd, true, read_len); 1373e4648b01SIlias Tsitsimpis } else if (se_cmd->data_direction == DMA_FROM_DEVICE) { 13746c3796d1Sbstroesser@ts.fujitsu.com gather_data_area(udev, cmd, false, read_len); 13757c9e7a6fSAndy Grover } else if (se_cmd->data_direction == DMA_TO_DEVICE) { 1376141685a3SXiubo Li /* TODO: */ 13772bc396a2SIlias Tsitsimpis } else if (se_cmd->data_direction != DMA_NONE) { 13782bc396a2SIlias Tsitsimpis pr_warn("TCMU: data direction was %d!\n", 13792bc396a2SIlias Tsitsimpis se_cmd->data_direction); 13807c9e7a6fSAndy Grover } 13817c9e7a6fSAndy Grover 13826c3796d1Sbstroesser@ts.fujitsu.com done: 1383780e1384SShin'ichiro Kawasaki se_cmd->priv = NULL; 13846c3796d1Sbstroesser@ts.fujitsu.com if (read_len_valid) { 13856c3796d1Sbstroesser@ts.fujitsu.com pr_debug("read_len = %d\n", read_len); 13866c3796d1Sbstroesser@ts.fujitsu.com target_complete_cmd_with_length(cmd->se_cmd, 13876c3796d1Sbstroesser@ts.fujitsu.com entry->rsp.scsi_status, read_len); 13886c3796d1Sbstroesser@ts.fujitsu.com } else 13897c9e7a6fSAndy Grover target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status); 13907c9e7a6fSAndy Grover 1391141685a3SXiubo Li out: 1392018c1491SBodo Stroesser if (!keep_buf) { 1393b6df4b79SXiubo Li tcmu_cmd_free_data(cmd, cmd->dbi_cnt); 1394141685a3SXiubo Li tcmu_free_cmd(cmd); 1395018c1491SBodo Stroesser } else { 1396018c1491SBodo Stroesser /* 1397018c1491SBodo Stroesser * Keep this command after completion, since userspace still 1398018c1491SBodo Stroesser * needs the data buffer. Mark it with TCMU_CMD_BIT_KEEP_BUF 1399018c1491SBodo Stroesser * and reset potential TCMU_CMD_BIT_EXPIRED, so we don't accept 1400018c1491SBodo Stroesser * a second completion later. 1401018c1491SBodo Stroesser * Userspace can free the buffer later by writing the cmd_id 1402018c1491SBodo Stroesser * to new action attribute free_kept_buf. 1403018c1491SBodo Stroesser */ 1404018c1491SBodo Stroesser clear_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags); 1405018c1491SBodo Stroesser set_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags); 1406018c1491SBodo Stroesser } 1407018c1491SBodo Stroesser return ret; 14087c9e7a6fSAndy Grover } 14097c9e7a6fSAndy Grover 1410bc2d214aSBodo Stroesser static int tcmu_run_tmr_queue(struct tcmu_dev *udev) 1411bc2d214aSBodo Stroesser { 1412bc2d214aSBodo Stroesser struct tcmu_tmr *tmr, *tmp; 1413bc2d214aSBodo Stroesser LIST_HEAD(tmrs); 1414bc2d214aSBodo Stroesser 1415bc2d214aSBodo Stroesser if (list_empty(&udev->tmr_queue)) 1416bc2d214aSBodo Stroesser return 1; 1417bc2d214aSBodo Stroesser 1418bc2d214aSBodo Stroesser pr_debug("running %s's tmr queue\n", udev->name); 1419bc2d214aSBodo Stroesser 1420bc2d214aSBodo Stroesser list_splice_init(&udev->tmr_queue, &tmrs); 1421bc2d214aSBodo Stroesser 1422bc2d214aSBodo Stroesser list_for_each_entry_safe(tmr, tmp, &tmrs, queue_entry) { 1423bc2d214aSBodo Stroesser list_del_init(&tmr->queue_entry); 1424bc2d214aSBodo Stroesser 1425bc2d214aSBodo Stroesser pr_debug("removing tmr %p on dev %s from queue\n", 1426bc2d214aSBodo Stroesser tmr, udev->name); 1427bc2d214aSBodo Stroesser 1428bc2d214aSBodo Stroesser if (queue_tmr_ring(udev, tmr)) { 1429bc2d214aSBodo Stroesser pr_debug("ran out of space during tmr queue run\n"); 1430bc2d214aSBodo Stroesser /* 1431bc2d214aSBodo Stroesser * tmr was requeued, so just put all tmrs back in 1432bc2d214aSBodo Stroesser * the queue 1433bc2d214aSBodo Stroesser */ 1434bc2d214aSBodo Stroesser list_splice_tail(&tmrs, &udev->tmr_queue); 1435bc2d214aSBodo Stroesser return 0; 1436bc2d214aSBodo Stroesser } 1437bc2d214aSBodo Stroesser } 1438bc2d214aSBodo Stroesser 1439bc2d214aSBodo Stroesser return 1; 1440bc2d214aSBodo Stroesser } 1441bc2d214aSBodo Stroesser 14429814b55cSBodo Stroesser static bool tcmu_handle_completions(struct tcmu_dev *udev) 14437c9e7a6fSAndy Grover { 14447c9e7a6fSAndy Grover struct tcmu_mailbox *mb; 1445a94a2572SXiubo Li struct tcmu_cmd *cmd; 1446bc2d214aSBodo Stroesser bool free_space = false; 14477c9e7a6fSAndy Grover 14487c9e7a6fSAndy Grover if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) { 14497c9e7a6fSAndy Grover pr_err("ring broken, not handling completions\n"); 145082473125Skernel test robot return false; 14517c9e7a6fSAndy Grover } 14527c9e7a6fSAndy Grover 14537c9e7a6fSAndy Grover mb = udev->mb_addr; 14547c9e7a6fSAndy Grover tcmu_flush_dcache_range(mb, sizeof(*mb)); 14557c9e7a6fSAndy Grover 14566aa7de05SMark Rutland while (udev->cmdr_last_cleaned != READ_ONCE(mb->cmd_tail)) { 14577c9e7a6fSAndy Grover 1458ecddbb7eSBodo Stroesser struct tcmu_cmd_entry *entry = udev->cmdr + udev->cmdr_last_cleaned; 1459018c1491SBodo Stroesser bool keep_buf; 14607c9e7a6fSAndy Grover 14615a0c256dSBodo Stroesser /* 14625a0c256dSBodo Stroesser * Flush max. up to end of cmd ring since current entry might 14635a0c256dSBodo Stroesser * be a padding that is shorter than sizeof(*entry) 14645a0c256dSBodo Stroesser */ 14655a0c256dSBodo Stroesser size_t ring_left = head_to_end(udev->cmdr_last_cleaned, 14665a0c256dSBodo Stroesser udev->cmdr_size); 14675a0c256dSBodo Stroesser tcmu_flush_dcache_range(entry, ring_left < sizeof(*entry) ? 14685a0c256dSBodo Stroesser ring_left : sizeof(*entry)); 14697c9e7a6fSAndy Grover 1470bc2d214aSBodo Stroesser free_space = true; 1471bc2d214aSBodo Stroesser 1472bc2d214aSBodo Stroesser if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD || 1473bc2d214aSBodo Stroesser tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_TMR) { 14740ad46af8SAndy Grover UPDATE_HEAD(udev->cmdr_last_cleaned, 14750ad46af8SAndy Grover tcmu_hdr_get_len(entry->hdr.len_op), 14760ad46af8SAndy Grover udev->cmdr_size); 14777c9e7a6fSAndy Grover continue; 14787c9e7a6fSAndy Grover } 14790ad46af8SAndy Grover WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD); 14807c9e7a6fSAndy Grover 1481018c1491SBodo Stroesser keep_buf = !!(entry->hdr.uflags & TCMU_UFLAG_KEEP_BUF); 1482018c1491SBodo Stroesser if (keep_buf) 1483018c1491SBodo Stroesser cmd = xa_load(&udev->commands, entry->hdr.cmd_id); 1484018c1491SBodo Stroesser else 1485d3cbb743SBodo Stroesser cmd = xa_erase(&udev->commands, entry->hdr.cmd_id); 14867c9e7a6fSAndy Grover if (!cmd) { 148788cf1073SMike Christie pr_err("cmd_id %u not found, ring is broken\n", 148888cf1073SMike Christie entry->hdr.cmd_id); 14897c9e7a6fSAndy Grover set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags); 14909814b55cSBodo Stroesser return false; 14917c9e7a6fSAndy Grover } 14927c9e7a6fSAndy Grover 1493018c1491SBodo Stroesser if (!tcmu_handle_completion(cmd, entry, keep_buf)) 1494018c1491SBodo Stroesser break; 14957c9e7a6fSAndy Grover 14960ad46af8SAndy Grover UPDATE_HEAD(udev->cmdr_last_cleaned, 14970ad46af8SAndy Grover tcmu_hdr_get_len(entry->hdr.len_op), 14980ad46af8SAndy Grover udev->cmdr_size); 14997c9e7a6fSAndy Grover } 1500bc2d214aSBodo Stroesser if (free_space) 1501bc2d214aSBodo Stroesser free_space = tcmu_run_tmr_queue(udev); 15027c9e7a6fSAndy Grover 15038b084d9dSBodo Stroesser if (atomic_read(&global_page_count) > tcmu_global_max_pages && 1504d3cbb743SBodo Stroesser xa_empty(&udev->commands) && list_empty(&udev->qfull_queue)) { 1505af1dd7ffSMike Christie /* 1506ed212ca8SBodo Stroesser * Allocated blocks exceeded global block limit, currently no 1507ed212ca8SBodo Stroesser * more pending or waiting commands so try to reclaim blocks. 1508af1dd7ffSMike Christie */ 1509af1dd7ffSMike Christie schedule_delayed_work(&tcmu_unmap_work, 0); 1510af1dd7ffSMike Christie } 1511ed212ca8SBodo Stroesser if (udev->cmd_time_out) 1512a94a2572SXiubo Li tcmu_set_next_deadline(&udev->inflight_queue, &udev->cmd_timer); 15137c9e7a6fSAndy Grover 1514bc2d214aSBodo Stroesser return free_space; 15157c9e7a6fSAndy Grover } 15167c9e7a6fSAndy Grover 151761fb2482SBodo Stroesser static void tcmu_check_expired_ring_cmd(struct tcmu_cmd *cmd) 15187c9e7a6fSAndy Grover { 1519af1dd7ffSMike Christie struct se_cmd *se_cmd; 15207c9e7a6fSAndy Grover 1521ed212ca8SBodo Stroesser if (!time_after_eq(jiffies, cmd->deadline)) 152261fb2482SBodo Stroesser return; 15239103575aSMike Christie 1524af1dd7ffSMike Christie set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags); 152540d883b0SXiubo Li list_del_init(&cmd->queue_entry); 152661fb2482SBodo Stroesser se_cmd = cmd->se_cmd; 1527a3512902SBodo Stroesser se_cmd->priv = NULL; 1528a86a7586SDmitry Fomichev cmd->se_cmd = NULL; 152961fb2482SBodo Stroesser 153061fb2482SBodo Stroesser pr_debug("Timing out inflight cmd %u on dev %s.\n", 153161fb2482SBodo Stroesser cmd->cmd_id, cmd->tcmu_dev->name); 153261fb2482SBodo Stroesser 153361fb2482SBodo Stroesser target_complete_cmd(se_cmd, SAM_STAT_CHECK_CONDITION); 1534af1dd7ffSMike Christie } 15359103575aSMike Christie 153661fb2482SBodo Stroesser static void tcmu_check_expired_queue_cmd(struct tcmu_cmd *cmd) 153761fb2482SBodo Stroesser { 153861fb2482SBodo Stroesser struct se_cmd *se_cmd; 15399103575aSMike Christie 1540ed212ca8SBodo Stroesser if (!time_after_eq(jiffies, cmd->deadline)) 154161fb2482SBodo Stroesser return; 154261fb2482SBodo Stroesser 15439d7464b1SDan Carpenter pr_debug("Timing out queued cmd %p on dev %s.\n", 15449d7464b1SDan Carpenter cmd, cmd->tcmu_dev->name); 15459d7464b1SDan Carpenter 154661fb2482SBodo Stroesser list_del_init(&cmd->queue_entry); 154761fb2482SBodo Stroesser se_cmd = cmd->se_cmd; 154861fb2482SBodo Stroesser tcmu_free_cmd(cmd); 154961fb2482SBodo Stroesser 1550780e1384SShin'ichiro Kawasaki se_cmd->priv = NULL; 155161fb2482SBodo Stroesser target_complete_cmd(se_cmd, SAM_STAT_TASK_SET_FULL); 15527c9e7a6fSAndy Grover } 15537c9e7a6fSAndy Grover 15549103575aSMike Christie static void tcmu_device_timedout(struct tcmu_dev *udev) 15557c9e7a6fSAndy Grover { 1556488ebe4cSMike Christie spin_lock(&timed_out_udevs_lock); 1557488ebe4cSMike Christie if (list_empty(&udev->timedout_entry)) 1558488ebe4cSMike Christie list_add_tail(&udev->timedout_entry, &timed_out_udevs); 1559488ebe4cSMike Christie spin_unlock(&timed_out_udevs_lock); 15607c9e7a6fSAndy Grover 1561af1dd7ffSMike Christie schedule_delayed_work(&tcmu_unmap_work, 0); 15627c9e7a6fSAndy Grover } 15637c9e7a6fSAndy Grover 15649103575aSMike Christie static void tcmu_cmd_timedout(struct timer_list *t) 15659103575aSMike Christie { 15669103575aSMike Christie struct tcmu_dev *udev = from_timer(udev, t, cmd_timer); 15679103575aSMike Christie 15689103575aSMike Christie pr_debug("%s cmd timeout has expired\n", udev->name); 15699103575aSMike Christie tcmu_device_timedout(udev); 15709103575aSMike Christie } 15719103575aSMike Christie 15729103575aSMike Christie static void tcmu_qfull_timedout(struct timer_list *t) 15739103575aSMike Christie { 15749103575aSMike Christie struct tcmu_dev *udev = from_timer(udev, t, qfull_timer); 15759103575aSMike Christie 15769103575aSMike Christie pr_debug("%s qfull timeout has expired\n", udev->name); 15779103575aSMike Christie tcmu_device_timedout(udev); 15789103575aSMike Christie } 15799103575aSMike Christie 15807c9e7a6fSAndy Grover static int tcmu_attach_hba(struct se_hba *hba, u32 host_id) 15817c9e7a6fSAndy Grover { 15827c9e7a6fSAndy Grover struct tcmu_hba *tcmu_hba; 15837c9e7a6fSAndy Grover 15847c9e7a6fSAndy Grover tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL); 15857c9e7a6fSAndy Grover if (!tcmu_hba) 15867c9e7a6fSAndy Grover return -ENOMEM; 15877c9e7a6fSAndy Grover 15887c9e7a6fSAndy Grover tcmu_hba->host_id = host_id; 15897c9e7a6fSAndy Grover hba->hba_ptr = tcmu_hba; 15907c9e7a6fSAndy Grover 15917c9e7a6fSAndy Grover return 0; 15927c9e7a6fSAndy Grover } 15937c9e7a6fSAndy Grover 15947c9e7a6fSAndy Grover static void tcmu_detach_hba(struct se_hba *hba) 15957c9e7a6fSAndy Grover { 15967c9e7a6fSAndy Grover kfree(hba->hba_ptr); 15977c9e7a6fSAndy Grover hba->hba_ptr = NULL; 15987c9e7a6fSAndy Grover } 15997c9e7a6fSAndy Grover 16007c9e7a6fSAndy Grover static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name) 16017c9e7a6fSAndy Grover { 16027c9e7a6fSAndy Grover struct tcmu_dev *udev; 16037c9e7a6fSAndy Grover 16047c9e7a6fSAndy Grover udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL); 16057c9e7a6fSAndy Grover if (!udev) 16067c9e7a6fSAndy Grover return NULL; 1607f3cdbe39SMike Christie kref_init(&udev->kref); 16087c9e7a6fSAndy Grover 16097c9e7a6fSAndy Grover udev->name = kstrdup(name, GFP_KERNEL); 16107c9e7a6fSAndy Grover if (!udev->name) { 16117c9e7a6fSAndy Grover kfree(udev); 16127c9e7a6fSAndy Grover return NULL; 16137c9e7a6fSAndy Grover } 16147c9e7a6fSAndy Grover 16157c9e7a6fSAndy Grover udev->hba = hba; 1616af980e46SMike Christie udev->cmd_time_out = TCMU_TIME_OUT; 16179103575aSMike Christie udev->qfull_time_out = -1; 16187c9e7a6fSAndy Grover 1619e719afdcSBodo Stroesser udev->data_pages_per_blk = DATA_PAGES_PER_BLK_DEF; 1620e719afdcSBodo Stroesser udev->max_blocks = DATA_AREA_PAGES_DEF / udev->data_pages_per_blk; 1621c7ede4f0SGuixin Liu udev->cmdr_size = CMDR_SIZE_DEF; 1622f5ce815fSBodo Stroesser udev->data_area_mb = TCMU_PAGES_TO_MBS(DATA_AREA_PAGES_DEF); 1623e719afdcSBodo Stroesser 1624b6df4b79SXiubo Li mutex_init(&udev->cmdr_lock); 16257c9e7a6fSAndy Grover 1626ff07e4a4SMike Christie INIT_LIST_HEAD(&udev->node); 1627488ebe4cSMike Christie INIT_LIST_HEAD(&udev->timedout_entry); 1628a94a2572SXiubo Li INIT_LIST_HEAD(&udev->qfull_queue); 1629bc2d214aSBodo Stroesser INIT_LIST_HEAD(&udev->tmr_queue); 1630a94a2572SXiubo Li INIT_LIST_HEAD(&udev->inflight_queue); 1631d3cbb743SBodo Stroesser xa_init_flags(&udev->commands, XA_FLAGS_ALLOC1); 16327c9e7a6fSAndy Grover 16339103575aSMike Christie timer_setup(&udev->qfull_timer, tcmu_qfull_timedout, 0); 16349103575aSMike Christie timer_setup(&udev->cmd_timer, tcmu_cmd_timedout, 0); 16357c9e7a6fSAndy Grover 16368b084d9dSBodo Stroesser xa_init(&udev->data_pages); 1637c22adc0bSXiubo Li 16387c9e7a6fSAndy Grover return &udev->se_dev; 16397c9e7a6fSAndy Grover } 16407c9e7a6fSAndy Grover 164143bf922cSBodo Stroesser static void tcmu_dev_call_rcu(struct rcu_head *p) 164243bf922cSBodo Stroesser { 164343bf922cSBodo Stroesser struct se_device *dev = container_of(p, struct se_device, rcu_head); 164443bf922cSBodo Stroesser struct tcmu_dev *udev = TCMU_DEV(dev); 164543bf922cSBodo Stroesser 164643bf922cSBodo Stroesser kfree(udev->uio_info.name); 164743bf922cSBodo Stroesser kfree(udev->name); 164843bf922cSBodo Stroesser kfree(udev); 164943bf922cSBodo Stroesser } 165043bf922cSBodo Stroesser 165143bf922cSBodo Stroesser static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd) 165243bf922cSBodo Stroesser { 1653018c1491SBodo Stroesser if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags) || 1654018c1491SBodo Stroesser test_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags)) { 165543bf922cSBodo Stroesser kmem_cache_free(tcmu_cmd_cache, cmd); 165643bf922cSBodo Stroesser return 0; 165743bf922cSBodo Stroesser } 165843bf922cSBodo Stroesser return -EINVAL; 165943bf922cSBodo Stroesser } 166043bf922cSBodo Stroesser 1661e719afdcSBodo Stroesser static u32 tcmu_blocks_release(struct tcmu_dev *udev, unsigned long first, 1662f7c89771SBodo Stroesser unsigned long last) 166343bf922cSBodo Stroesser { 1664e719afdcSBodo Stroesser XA_STATE(xas, &udev->data_pages, first * udev->data_pages_per_blk); 166543bf922cSBodo Stroesser struct page *page; 1666f5ce815fSBodo Stroesser u32 pages_freed = 0; 166743bf922cSBodo Stroesser 1668f7c89771SBodo Stroesser xas_lock(&xas); 1669e719afdcSBodo Stroesser xas_for_each(&xas, page, (last + 1) * udev->data_pages_per_blk - 1) { 1670f7c89771SBodo Stroesser xas_store(&xas, NULL); 1671*bb9b9eb0SXiaoguang Wang /* 1672*bb9b9eb0SXiaoguang Wang * While reaching here there may be page faults occurring on 1673*bb9b9eb0SXiaoguang Wang * the to-be-released pages. A race condition may occur if 1674*bb9b9eb0SXiaoguang Wang * unmap_mapping_range() is called before page faults on these 1675*bb9b9eb0SXiaoguang Wang * pages have completed; a valid but stale map is created. 1676*bb9b9eb0SXiaoguang Wang * 1677*bb9b9eb0SXiaoguang Wang * If another command subsequently runs and needs to extend 1678*bb9b9eb0SXiaoguang Wang * dbi_thresh, it may reuse the slot corresponding to the 1679*bb9b9eb0SXiaoguang Wang * previous page in data_bitmap. Though we will allocate a new 1680*bb9b9eb0SXiaoguang Wang * page for the slot in data_area, no page fault will happen 1681*bb9b9eb0SXiaoguang Wang * because we have a valid map. Therefore the command's data 1682*bb9b9eb0SXiaoguang Wang * will be lost. 1683*bb9b9eb0SXiaoguang Wang * 1684*bb9b9eb0SXiaoguang Wang * We lock and unlock pages that are to be released to ensure 1685*bb9b9eb0SXiaoguang Wang * all page faults have completed. This way 1686*bb9b9eb0SXiaoguang Wang * unmap_mapping_range() can ensure stale maps are cleanly 1687*bb9b9eb0SXiaoguang Wang * removed. 1688*bb9b9eb0SXiaoguang Wang */ 1689*bb9b9eb0SXiaoguang Wang lock_page(page); 1690*bb9b9eb0SXiaoguang Wang unlock_page(page); 169143bf922cSBodo Stroesser __free_page(page); 1692f5ce815fSBodo Stroesser pages_freed++; 169343bf922cSBodo Stroesser } 1694f7c89771SBodo Stroesser xas_unlock(&xas); 1695f5ce815fSBodo Stroesser 1696f5ce815fSBodo Stroesser atomic_sub(pages_freed, &global_page_count); 1697f5ce815fSBodo Stroesser 1698f5ce815fSBodo Stroesser return pages_freed; 169943bf922cSBodo Stroesser } 170043bf922cSBodo Stroesser 170143bf922cSBodo Stroesser static void tcmu_remove_all_queued_tmr(struct tcmu_dev *udev) 170243bf922cSBodo Stroesser { 170343bf922cSBodo Stroesser struct tcmu_tmr *tmr, *tmp; 170443bf922cSBodo Stroesser 170543bf922cSBodo Stroesser list_for_each_entry_safe(tmr, tmp, &udev->tmr_queue, queue_entry) { 170643bf922cSBodo Stroesser list_del_init(&tmr->queue_entry); 170743bf922cSBodo Stroesser kfree(tmr); 170843bf922cSBodo Stroesser } 170943bf922cSBodo Stroesser } 171043bf922cSBodo Stroesser 171143bf922cSBodo Stroesser static void tcmu_dev_kref_release(struct kref *kref) 171243bf922cSBodo Stroesser { 171343bf922cSBodo Stroesser struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref); 171443bf922cSBodo Stroesser struct se_device *dev = &udev->se_dev; 171543bf922cSBodo Stroesser struct tcmu_cmd *cmd; 171643bf922cSBodo Stroesser bool all_expired = true; 1717d3cbb743SBodo Stroesser unsigned long i; 171843bf922cSBodo Stroesser 171943bf922cSBodo Stroesser vfree(udev->mb_addr); 172043bf922cSBodo Stroesser udev->mb_addr = NULL; 172143bf922cSBodo Stroesser 172243bf922cSBodo Stroesser spin_lock_bh(&timed_out_udevs_lock); 172343bf922cSBodo Stroesser if (!list_empty(&udev->timedout_entry)) 172443bf922cSBodo Stroesser list_del(&udev->timedout_entry); 172543bf922cSBodo Stroesser spin_unlock_bh(&timed_out_udevs_lock); 172643bf922cSBodo Stroesser 172743bf922cSBodo Stroesser /* Upper layer should drain all requests before calling this */ 172843bf922cSBodo Stroesser mutex_lock(&udev->cmdr_lock); 1729d3cbb743SBodo Stroesser xa_for_each(&udev->commands, i, cmd) { 173043bf922cSBodo Stroesser if (tcmu_check_and_free_pending_cmd(cmd) != 0) 173143bf922cSBodo Stroesser all_expired = false; 173243bf922cSBodo Stroesser } 173343bf922cSBodo Stroesser /* There can be left over TMR cmds. Remove them. */ 173443bf922cSBodo Stroesser tcmu_remove_all_queued_tmr(udev); 173543bf922cSBodo Stroesser if (!list_empty(&udev->qfull_queue)) 173643bf922cSBodo Stroesser all_expired = false; 1737d3cbb743SBodo Stroesser xa_destroy(&udev->commands); 173843bf922cSBodo Stroesser WARN_ON(!all_expired); 173943bf922cSBodo Stroesser 1740e719afdcSBodo Stroesser tcmu_blocks_release(udev, 0, udev->dbi_max); 174143bf922cSBodo Stroesser bitmap_free(udev->data_bitmap); 174243bf922cSBodo Stroesser mutex_unlock(&udev->cmdr_lock); 174343bf922cSBodo Stroesser 17448f33bb24SBodo Stroesser pr_debug("dev_kref_release\n"); 17458f33bb24SBodo Stroesser 174643bf922cSBodo Stroesser call_rcu(&dev->rcu_head, tcmu_dev_call_rcu); 174743bf922cSBodo Stroesser } 174843bf922cSBodo Stroesser 174961fb2482SBodo Stroesser static void run_qfull_queue(struct tcmu_dev *udev, bool fail) 1750af1dd7ffSMike Christie { 1751af1dd7ffSMike Christie struct tcmu_cmd *tcmu_cmd, *tmp_cmd; 1752af1dd7ffSMike Christie LIST_HEAD(cmds); 1753af1dd7ffSMike Christie sense_reason_t scsi_ret; 1754af1dd7ffSMike Christie int ret; 1755af1dd7ffSMike Christie 1756a94a2572SXiubo Li if (list_empty(&udev->qfull_queue)) 175761fb2482SBodo Stroesser return; 1758af1dd7ffSMike Christie 1759892782caSMike Christie pr_debug("running %s's cmdr queue forcefail %d\n", udev->name, fail); 1760af1dd7ffSMike Christie 1761a94a2572SXiubo Li list_splice_init(&udev->qfull_queue, &cmds); 1762af1dd7ffSMike Christie 1763a94a2572SXiubo Li list_for_each_entry_safe(tcmu_cmd, tmp_cmd, &cmds, queue_entry) { 1764a94a2572SXiubo Li list_del_init(&tcmu_cmd->queue_entry); 1765af1dd7ffSMike Christie 176661fb2482SBodo Stroesser pr_debug("removing cmd %p on dev %s from queue\n", 176761fb2482SBodo Stroesser tcmu_cmd, udev->name); 1768af1dd7ffSMike Christie 1769892782caSMike Christie if (fail) { 1770892782caSMike Christie /* 1771892782caSMike Christie * We were not able to even start the command, so 1772892782caSMike Christie * fail with busy to allow a retry in case runner 1773892782caSMike Christie * was only temporarily down. If the device is being 1774892782caSMike Christie * removed then LIO core will do the right thing and 1775892782caSMike Christie * fail the retry. 1776892782caSMike Christie */ 1777780e1384SShin'ichiro Kawasaki tcmu_cmd->se_cmd->priv = NULL; 1778892782caSMike Christie target_complete_cmd(tcmu_cmd->se_cmd, SAM_STAT_BUSY); 1779892782caSMike Christie tcmu_free_cmd(tcmu_cmd); 1780892782caSMike Christie continue; 1781892782caSMike Christie } 1782892782caSMike Christie 1783af1dd7ffSMike Christie ret = queue_cmd_ring(tcmu_cmd, &scsi_ret); 1784af1dd7ffSMike Christie if (ret < 0) { 178561fb2482SBodo Stroesser pr_debug("cmd %p on dev %s failed with %u\n", 178661fb2482SBodo Stroesser tcmu_cmd, udev->name, scsi_ret); 1787af1dd7ffSMike Christie /* 1788af1dd7ffSMike Christie * Ignore scsi_ret for now. target_complete_cmd 1789af1dd7ffSMike Christie * drops it. 1790af1dd7ffSMike Christie */ 1791780e1384SShin'ichiro Kawasaki tcmu_cmd->se_cmd->priv = NULL; 1792af1dd7ffSMike Christie target_complete_cmd(tcmu_cmd->se_cmd, 1793af1dd7ffSMike Christie SAM_STAT_CHECK_CONDITION); 1794af1dd7ffSMike Christie tcmu_free_cmd(tcmu_cmd); 1795af1dd7ffSMike Christie } else if (ret > 0) { 1796af1dd7ffSMike Christie pr_debug("ran out of space during cmdr queue run\n"); 1797af1dd7ffSMike Christie /* 1798af1dd7ffSMike Christie * cmd was requeued, so just put all cmds back in 1799af1dd7ffSMike Christie * the queue 1800af1dd7ffSMike Christie */ 1801a94a2572SXiubo Li list_splice_tail(&cmds, &udev->qfull_queue); 1802a94a2572SXiubo Li break; 1803af1dd7ffSMike Christie } 1804af1dd7ffSMike Christie } 1805a94a2572SXiubo Li 1806a94a2572SXiubo Li tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer); 1807af1dd7ffSMike Christie } 1808af1dd7ffSMike Christie 18097c9e7a6fSAndy Grover static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on) 18107c9e7a6fSAndy Grover { 1811af1dd7ffSMike Christie struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 18127c9e7a6fSAndy Grover 1813af1dd7ffSMike Christie mutex_lock(&udev->cmdr_lock); 1814bc2d214aSBodo Stroesser if (tcmu_handle_completions(udev)) 1815a94a2572SXiubo Li run_qfull_queue(udev, false); 1816af1dd7ffSMike Christie mutex_unlock(&udev->cmdr_lock); 18177c9e7a6fSAndy Grover 18187c9e7a6fSAndy Grover return 0; 18197c9e7a6fSAndy Grover } 18207c9e7a6fSAndy Grover 18217c9e7a6fSAndy Grover /* 18227c9e7a6fSAndy Grover * mmap code from uio.c. Copied here because we want to hook mmap() 18237c9e7a6fSAndy Grover * and this stuff must come along. 18247c9e7a6fSAndy Grover */ 18257c9e7a6fSAndy Grover static int tcmu_find_mem_index(struct vm_area_struct *vma) 18267c9e7a6fSAndy Grover { 18277c9e7a6fSAndy Grover struct tcmu_dev *udev = vma->vm_private_data; 18287c9e7a6fSAndy Grover struct uio_info *info = &udev->uio_info; 18297c9e7a6fSAndy Grover 18307c9e7a6fSAndy Grover if (vma->vm_pgoff < MAX_UIO_MAPS) { 18317c9e7a6fSAndy Grover if (info->mem[vma->vm_pgoff].size == 0) 18327c9e7a6fSAndy Grover return -1; 18337c9e7a6fSAndy Grover return (int)vma->vm_pgoff; 18347c9e7a6fSAndy Grover } 18357c9e7a6fSAndy Grover return -1; 18367c9e7a6fSAndy Grover } 18377c9e7a6fSAndy Grover 18388b084d9dSBodo Stroesser static struct page *tcmu_try_get_data_page(struct tcmu_dev *udev, uint32_t dpi) 1839b6df4b79SXiubo Li { 1840b6df4b79SXiubo Li struct page *page; 1841b6df4b79SXiubo Li 1842b6df4b79SXiubo Li mutex_lock(&udev->cmdr_lock); 18433722e36cSBodo Stroesser page = xa_load(&udev->data_pages, dpi); 1844b6df4b79SXiubo Li if (likely(page)) { 1845a6968f7aSXiaoguang Wang get_page(page); 1846*bb9b9eb0SXiaoguang Wang lock_page(page); 1847b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1848b6df4b79SXiubo Li return page; 1849b6df4b79SXiubo Li } 1850b6df4b79SXiubo Li 1851b6df4b79SXiubo Li /* 1852c1c390baSMike Christie * Userspace messed up and passed in a address not in the 1853c1c390baSMike Christie * data iov passed to it. 1854b6df4b79SXiubo Li */ 18558b084d9dSBodo Stroesser pr_err("Invalid addr to data page mapping (dpi %u) on device %s\n", 18568b084d9dSBodo Stroesser dpi, udev->name); 1857b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1858b6df4b79SXiubo Li 18598b084d9dSBodo Stroesser return NULL; 1860b6df4b79SXiubo Li } 1861b6df4b79SXiubo Li 18628f33bb24SBodo Stroesser static void tcmu_vma_open(struct vm_area_struct *vma) 18638f33bb24SBodo Stroesser { 18648f33bb24SBodo Stroesser struct tcmu_dev *udev = vma->vm_private_data; 18658f33bb24SBodo Stroesser 18668f33bb24SBodo Stroesser pr_debug("vma_open\n"); 18678f33bb24SBodo Stroesser 18688f33bb24SBodo Stroesser kref_get(&udev->kref); 18698f33bb24SBodo Stroesser } 18708f33bb24SBodo Stroesser 18718f33bb24SBodo Stroesser static void tcmu_vma_close(struct vm_area_struct *vma) 18728f33bb24SBodo Stroesser { 18738f33bb24SBodo Stroesser struct tcmu_dev *udev = vma->vm_private_data; 18748f33bb24SBodo Stroesser 18758f33bb24SBodo Stroesser pr_debug("vma_close\n"); 18768f33bb24SBodo Stroesser 18778f33bb24SBodo Stroesser /* release ref from tcmu_vma_open */ 18788f33bb24SBodo Stroesser kref_put(&udev->kref, tcmu_dev_kref_release); 18798f33bb24SBodo Stroesser } 18808f33bb24SBodo Stroesser 188169589c9bSSouptick Joarder static vm_fault_t tcmu_vma_fault(struct vm_fault *vmf) 18827c9e7a6fSAndy Grover { 188311bac800SDave Jiang struct tcmu_dev *udev = vmf->vma->vm_private_data; 18847c9e7a6fSAndy Grover struct uio_info *info = &udev->uio_info; 18857c9e7a6fSAndy Grover struct page *page; 18867c9e7a6fSAndy Grover unsigned long offset; 18877c9e7a6fSAndy Grover void *addr; 1888*bb9b9eb0SXiaoguang Wang vm_fault_t ret = 0; 18897c9e7a6fSAndy Grover 189011bac800SDave Jiang int mi = tcmu_find_mem_index(vmf->vma); 18917c9e7a6fSAndy Grover if (mi < 0) 18927c9e7a6fSAndy Grover return VM_FAULT_SIGBUS; 18937c9e7a6fSAndy Grover 18947c9e7a6fSAndy Grover /* 18957c9e7a6fSAndy Grover * We need to subtract mi because userspace uses offset = N*PAGE_SIZE 18967c9e7a6fSAndy Grover * to use mem[N]. 18977c9e7a6fSAndy Grover */ 18987c9e7a6fSAndy Grover offset = (vmf->pgoff - mi) << PAGE_SHIFT; 18997c9e7a6fSAndy Grover 1900141685a3SXiubo Li if (offset < udev->data_off) { 1901141685a3SXiubo Li /* For the vmalloc()ed cmd area pages */ 19027c9e7a6fSAndy Grover addr = (void *)(unsigned long)info->mem[mi].addr + offset; 19037c9e7a6fSAndy Grover page = vmalloc_to_page(addr); 1904a6968f7aSXiaoguang Wang get_page(page); 1905141685a3SXiubo Li } else { 19068b084d9dSBodo Stroesser uint32_t dpi; 1907141685a3SXiubo Li 1908b6df4b79SXiubo Li /* For the dynamically growing data area pages */ 19098b084d9dSBodo Stroesser dpi = (offset - udev->data_off) / PAGE_SIZE; 19108b084d9dSBodo Stroesser page = tcmu_try_get_data_page(udev, dpi); 1911b6df4b79SXiubo Li if (!page) 1912c1c390baSMike Christie return VM_FAULT_SIGBUS; 1913*bb9b9eb0SXiaoguang Wang ret = VM_FAULT_LOCKED; 1914141685a3SXiubo Li } 1915141685a3SXiubo Li 19167c9e7a6fSAndy Grover vmf->page = page; 1917*bb9b9eb0SXiaoguang Wang return ret; 19187c9e7a6fSAndy Grover } 19197c9e7a6fSAndy Grover 19207c9e7a6fSAndy Grover static const struct vm_operations_struct tcmu_vm_ops = { 19218f33bb24SBodo Stroesser .open = tcmu_vma_open, 19228f33bb24SBodo Stroesser .close = tcmu_vma_close, 19237c9e7a6fSAndy Grover .fault = tcmu_vma_fault, 19247c9e7a6fSAndy Grover }; 19257c9e7a6fSAndy Grover 19267c9e7a6fSAndy Grover static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma) 19277c9e7a6fSAndy Grover { 19287c9e7a6fSAndy Grover struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 19297c9e7a6fSAndy Grover 19307c9e7a6fSAndy Grover vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 19317c9e7a6fSAndy Grover vma->vm_ops = &tcmu_vm_ops; 19327c9e7a6fSAndy Grover 19337c9e7a6fSAndy Grover vma->vm_private_data = udev; 19347c9e7a6fSAndy Grover 19357c9e7a6fSAndy Grover /* Ensure the mmap is exactly the right size */ 1936ecddbb7eSBodo Stroesser if (vma_pages(vma) != udev->mmap_pages) 19377c9e7a6fSAndy Grover return -EINVAL; 19387c9e7a6fSAndy Grover 19398f33bb24SBodo Stroesser tcmu_vma_open(vma); 19408f33bb24SBodo Stroesser 19417c9e7a6fSAndy Grover return 0; 19427c9e7a6fSAndy Grover } 19437c9e7a6fSAndy Grover 19447c9e7a6fSAndy Grover static int tcmu_open(struct uio_info *info, struct inode *inode) 19457c9e7a6fSAndy Grover { 19467c9e7a6fSAndy Grover struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 19477c9e7a6fSAndy Grover 19487c9e7a6fSAndy Grover /* O_EXCL not supported for char devs, so fake it? */ 19497c9e7a6fSAndy Grover if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags)) 19507c9e7a6fSAndy Grover return -EBUSY; 19517c9e7a6fSAndy Grover 1952b6df4b79SXiubo Li udev->inode = inode; 1953b6df4b79SXiubo Li 19547c9e7a6fSAndy Grover pr_debug("open\n"); 19557c9e7a6fSAndy Grover 19567c9e7a6fSAndy Grover return 0; 19577c9e7a6fSAndy Grover } 19587c9e7a6fSAndy Grover 19597c9e7a6fSAndy Grover static int tcmu_release(struct uio_info *info, struct inode *inode) 19607c9e7a6fSAndy Grover { 19617c9e7a6fSAndy Grover struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 1962018c1491SBodo Stroesser struct tcmu_cmd *cmd; 1963018c1491SBodo Stroesser unsigned long i; 1964018c1491SBodo Stroesser bool freed = false; 1965018c1491SBodo Stroesser 1966018c1491SBodo Stroesser mutex_lock(&udev->cmdr_lock); 1967018c1491SBodo Stroesser 1968018c1491SBodo Stroesser xa_for_each(&udev->commands, i, cmd) { 1969018c1491SBodo Stroesser /* Cmds with KEEP_BUF set are no longer on the ring, but 1970018c1491SBodo Stroesser * userspace still holds the data buffer. If userspace closes 1971018c1491SBodo Stroesser * we implicitly free these cmds and buffers, since after new 1972018c1491SBodo Stroesser * open the (new ?) userspace cannot find the cmd in the ring 1973018c1491SBodo Stroesser * and thus never will release the buffer by writing cmd_id to 1974018c1491SBodo Stroesser * free_kept_buf action attribute. 1975018c1491SBodo Stroesser */ 1976018c1491SBodo Stroesser if (!test_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags)) 1977018c1491SBodo Stroesser continue; 1978018c1491SBodo Stroesser pr_debug("removing KEEP_BUF cmd %u on dev %s from ring\n", 1979018c1491SBodo Stroesser cmd->cmd_id, udev->name); 1980018c1491SBodo Stroesser freed = true; 1981018c1491SBodo Stroesser 1982018c1491SBodo Stroesser xa_erase(&udev->commands, i); 1983018c1491SBodo Stroesser tcmu_cmd_free_data(cmd, cmd->dbi_cnt); 1984018c1491SBodo Stroesser tcmu_free_cmd(cmd); 1985018c1491SBodo Stroesser } 1986018c1491SBodo Stroesser /* 1987018c1491SBodo Stroesser * We only freed data space, not ring space. Therefore we dont call 1988018c1491SBodo Stroesser * run_tmr_queue, but call run_qfull_queue if tmr_list is empty. 1989018c1491SBodo Stroesser */ 1990018c1491SBodo Stroesser if (freed && list_empty(&udev->tmr_queue)) 1991018c1491SBodo Stroesser run_qfull_queue(udev, false); 1992018c1491SBodo Stroesser 1993018c1491SBodo Stroesser mutex_unlock(&udev->cmdr_lock); 19947c9e7a6fSAndy Grover 19957c9e7a6fSAndy Grover clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags); 19967c9e7a6fSAndy Grover 19977c9e7a6fSAndy Grover pr_debug("close\n"); 19988f33bb24SBodo Stroesser 19997c9e7a6fSAndy Grover return 0; 20007c9e7a6fSAndy Grover } 20017c9e7a6fSAndy Grover 20029de3a1efSMike Christie static int tcmu_init_genl_cmd_reply(struct tcmu_dev *udev, int cmd) 2003b3af66e2SMike Christie { 2004b3af66e2SMike Christie struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; 2005b3af66e2SMike Christie 2006b3af66e2SMike Christie if (!tcmu_kern_cmd_reply_supported) 20079de3a1efSMike Christie return 0; 2008b849b456SKenjiro Nakayama 2009b849b456SKenjiro Nakayama if (udev->nl_reply_supported <= 0) 20109de3a1efSMike Christie return 0; 2011b849b456SKenjiro Nakayama 20123228691fSMike Christie mutex_lock(&tcmu_nl_cmd_mutex); 2013b3af66e2SMike Christie 2014bdaeedc1SMike Christie if (tcmu_netlink_blocked) { 2015bdaeedc1SMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 2016bdaeedc1SMike Christie pr_warn("Failing nl cmd %d on %s. Interface is blocked.\n", cmd, 2017bdaeedc1SMike Christie udev->name); 2018bdaeedc1SMike Christie return -EAGAIN; 2019bdaeedc1SMike Christie } 2020b3af66e2SMike Christie 2021b3af66e2SMike Christie if (nl_cmd->cmd != TCMU_CMD_UNSPEC) { 20223228691fSMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 20239de3a1efSMike Christie pr_warn("netlink cmd %d already executing on %s\n", 20249de3a1efSMike Christie nl_cmd->cmd, udev->name); 20259de3a1efSMike Christie return -EBUSY; 2026b3af66e2SMike Christie } 2027b3af66e2SMike Christie 2028b3af66e2SMike Christie memset(nl_cmd, 0, sizeof(*nl_cmd)); 2029b3af66e2SMike Christie nl_cmd->cmd = cmd; 20303228691fSMike Christie nl_cmd->udev = udev; 2031b3af66e2SMike Christie init_completion(&nl_cmd->complete); 20323228691fSMike Christie INIT_LIST_HEAD(&nl_cmd->nl_list); 2033b3af66e2SMike Christie 20343228691fSMike Christie list_add_tail(&nl_cmd->nl_list, &tcmu_nl_cmd_list); 20353228691fSMike Christie 20363228691fSMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 20379de3a1efSMike Christie return 0; 2038b3af66e2SMike Christie } 2039b3af66e2SMike Christie 20407d894862SLi Zhong static void tcmu_destroy_genl_cmd_reply(struct tcmu_dev *udev) 20417d894862SLi Zhong { 20427d894862SLi Zhong struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; 20437d894862SLi Zhong 20447d894862SLi Zhong if (!tcmu_kern_cmd_reply_supported) 20457d894862SLi Zhong return; 20467d894862SLi Zhong 20477d894862SLi Zhong if (udev->nl_reply_supported <= 0) 20487d894862SLi Zhong return; 20497d894862SLi Zhong 20507d894862SLi Zhong mutex_lock(&tcmu_nl_cmd_mutex); 20517d894862SLi Zhong 20527d894862SLi Zhong list_del(&nl_cmd->nl_list); 20537d894862SLi Zhong memset(nl_cmd, 0, sizeof(*nl_cmd)); 20547d894862SLi Zhong 20557d894862SLi Zhong mutex_unlock(&tcmu_nl_cmd_mutex); 20567d894862SLi Zhong } 20577d894862SLi Zhong 2058b3af66e2SMike Christie static int tcmu_wait_genl_cmd_reply(struct tcmu_dev *udev) 2059b3af66e2SMike Christie { 2060b3af66e2SMike Christie struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; 2061b3af66e2SMike Christie int ret; 2062b3af66e2SMike Christie 2063b3af66e2SMike Christie if (!tcmu_kern_cmd_reply_supported) 2064b3af66e2SMike Christie return 0; 2065b3af66e2SMike Christie 2066b849b456SKenjiro Nakayama if (udev->nl_reply_supported <= 0) 2067b849b456SKenjiro Nakayama return 0; 2068b849b456SKenjiro Nakayama 2069b3af66e2SMike Christie pr_debug("sleeping for nl reply\n"); 2070b3af66e2SMike Christie wait_for_completion(&nl_cmd->complete); 2071b3af66e2SMike Christie 20723228691fSMike Christie mutex_lock(&tcmu_nl_cmd_mutex); 2073b3af66e2SMike Christie nl_cmd->cmd = TCMU_CMD_UNSPEC; 2074b3af66e2SMike Christie ret = nl_cmd->status; 20753228691fSMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 2076b3af66e2SMike Christie 207785fae482SLuis de Bethencourt return ret; 2078b3af66e2SMike Christie } 2079b3af66e2SMike Christie 20800e5aee39SZhu Lingshan static int tcmu_netlink_event_init(struct tcmu_dev *udev, 20810e5aee39SZhu Lingshan enum tcmu_genl_cmd cmd, 20820e5aee39SZhu Lingshan struct sk_buff **buf, void **hdr) 20837c9e7a6fSAndy Grover { 20847c9e7a6fSAndy Grover struct sk_buff *skb; 20857c9e7a6fSAndy Grover void *msg_header; 20866e14eab9SNicholas Bellinger int ret = -ENOMEM; 20877c9e7a6fSAndy Grover 20887c9e7a6fSAndy Grover skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 20897c9e7a6fSAndy Grover if (!skb) 20906e14eab9SNicholas Bellinger return ret; 20917c9e7a6fSAndy Grover 20927c9e7a6fSAndy Grover msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd); 20936e14eab9SNicholas Bellinger if (!msg_header) 20946e14eab9SNicholas Bellinger goto free_skb; 20957c9e7a6fSAndy Grover 2096b3af66e2SMike Christie ret = nla_put_string(skb, TCMU_ATTR_DEVICE, udev->uio_info.name); 20976e14eab9SNicholas Bellinger if (ret < 0) 20986e14eab9SNicholas Bellinger goto free_skb; 20997c9e7a6fSAndy Grover 2100b3af66e2SMike Christie ret = nla_put_u32(skb, TCMU_ATTR_MINOR, udev->uio_info.uio_dev->minor); 2101b3af66e2SMike Christie if (ret < 0) 2102b3af66e2SMike Christie goto free_skb; 2103b3af66e2SMike Christie 2104b3af66e2SMike Christie ret = nla_put_u32(skb, TCMU_ATTR_DEVICE_ID, udev->se_dev.dev_index); 21056e14eab9SNicholas Bellinger if (ret < 0) 21066e14eab9SNicholas Bellinger goto free_skb; 21077c9e7a6fSAndy Grover 21080e5aee39SZhu Lingshan *buf = skb; 21090e5aee39SZhu Lingshan *hdr = msg_header; 21100e5aee39SZhu Lingshan return ret; 21110e5aee39SZhu Lingshan 21120e5aee39SZhu Lingshan free_skb: 21130e5aee39SZhu Lingshan nlmsg_free(skb); 21140e5aee39SZhu Lingshan return ret; 21152d76443eSMike Christie } 21162d76443eSMike Christie 21170e5aee39SZhu Lingshan static int tcmu_netlink_event_send(struct tcmu_dev *udev, 21180e5aee39SZhu Lingshan enum tcmu_genl_cmd cmd, 211906add777SMike Christie struct sk_buff *skb, void *msg_header) 21200e5aee39SZhu Lingshan { 212106add777SMike Christie int ret; 21228a45885cSBryant G. Ly 2123053c095aSJohannes Berg genlmsg_end(skb, msg_header); 21247c9e7a6fSAndy Grover 21259de3a1efSMike Christie ret = tcmu_init_genl_cmd_reply(udev, cmd); 21269de3a1efSMike Christie if (ret) { 21279de3a1efSMike Christie nlmsg_free(skb); 21289de3a1efSMike Christie return ret; 21299de3a1efSMike Christie } 2130b3af66e2SMike Christie 213120c08b36SSheng Yang ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0, 21327c9e7a6fSAndy Grover TCMU_MCGRP_CONFIG, GFP_KERNEL); 21332ff717cdSCathy Avery 21342ff717cdSCathy Avery /* Wait during an add as the listener may not be up yet */ 21352ff717cdSCathy Avery if (ret == 0 || 21362ff717cdSCathy Avery (ret == -ESRCH && cmd == TCMU_CMD_ADDED_DEVICE)) 21372ff717cdSCathy Avery return tcmu_wait_genl_cmd_reply(udev); 21387d894862SLi Zhong else 21397d894862SLi Zhong tcmu_destroy_genl_cmd_reply(udev); 21402ff717cdSCathy Avery 21410e5aee39SZhu Lingshan return ret; 21420e5aee39SZhu Lingshan } 21437c9e7a6fSAndy Grover 2144e0c240acSZhu Lingshan static int tcmu_send_dev_add_event(struct tcmu_dev *udev) 2145e0c240acSZhu Lingshan { 2146e0c240acSZhu Lingshan struct sk_buff *skb = NULL; 2147e0c240acSZhu Lingshan void *msg_header = NULL; 2148e0c240acSZhu Lingshan int ret = 0; 2149e0c240acSZhu Lingshan 2150e0c240acSZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_ADDED_DEVICE, &skb, 2151e0c240acSZhu Lingshan &msg_header); 2152e0c240acSZhu Lingshan if (ret < 0) 21537c9e7a6fSAndy Grover return ret; 215406add777SMike Christie return tcmu_netlink_event_send(udev, TCMU_CMD_ADDED_DEVICE, skb, 215506add777SMike Christie msg_header); 2156e0c240acSZhu Lingshan } 2157e0c240acSZhu Lingshan 2158f892bd8eSZhu Lingshan static int tcmu_send_dev_remove_event(struct tcmu_dev *udev) 2159f892bd8eSZhu Lingshan { 2160f892bd8eSZhu Lingshan struct sk_buff *skb = NULL; 2161f892bd8eSZhu Lingshan void *msg_header = NULL; 2162f892bd8eSZhu Lingshan int ret = 0; 2163f892bd8eSZhu Lingshan 2164f892bd8eSZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_REMOVED_DEVICE, 2165f892bd8eSZhu Lingshan &skb, &msg_header); 2166f892bd8eSZhu Lingshan if (ret < 0) 21676e14eab9SNicholas Bellinger return ret; 2168f892bd8eSZhu Lingshan return tcmu_netlink_event_send(udev, TCMU_CMD_REMOVED_DEVICE, 216906add777SMike Christie skb, msg_header); 21707c9e7a6fSAndy Grover } 21717c9e7a6fSAndy Grover 2172de8c5221SBryant G. Ly static int tcmu_update_uio_info(struct tcmu_dev *udev) 21737c9e7a6fSAndy Grover { 21747c9e7a6fSAndy Grover struct tcmu_hba *hba = udev->hba->hba_ptr; 21757c9e7a6fSAndy Grover struct uio_info *info; 21767c9e7a6fSAndy Grover char *str; 21777c9e7a6fSAndy Grover 21787c9e7a6fSAndy Grover info = &udev->uio_info; 217922c2f35fSChristophe JAILLET 218022c2f35fSChristophe JAILLET if (udev->dev_config[0]) 218122c2f35fSChristophe JAILLET str = kasprintf(GFP_KERNEL, "tcm-user/%u/%s/%s", hba->host_id, 218222c2f35fSChristophe JAILLET udev->name, udev->dev_config); 218322c2f35fSChristophe JAILLET else 218422c2f35fSChristophe JAILLET str = kasprintf(GFP_KERNEL, "tcm-user/%u/%s", hba->host_id, 218522c2f35fSChristophe JAILLET udev->name); 21867c9e7a6fSAndy Grover if (!str) 21877c9e7a6fSAndy Grover return -ENOMEM; 21887c9e7a6fSAndy Grover 2189ededd039SBryant G. Ly /* If the old string exists, free it */ 2190ededd039SBryant G. Ly kfree(info->name); 21917c9e7a6fSAndy Grover info->name = str; 21927c9e7a6fSAndy Grover 2193de8c5221SBryant G. Ly return 0; 2194de8c5221SBryant G. Ly } 2195de8c5221SBryant G. Ly 2196de8c5221SBryant G. Ly static int tcmu_configure_device(struct se_device *dev) 2197de8c5221SBryant G. Ly { 2198de8c5221SBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(dev); 2199de8c5221SBryant G. Ly struct uio_info *info; 2200de8c5221SBryant G. Ly struct tcmu_mailbox *mb; 2201f5ce815fSBodo Stroesser size_t data_size; 2202de8c5221SBryant G. Ly int ret = 0; 2203de8c5221SBryant G. Ly 2204de8c5221SBryant G. Ly ret = tcmu_update_uio_info(udev); 2205de8c5221SBryant G. Ly if (ret) 2206de8c5221SBryant G. Ly return ret; 2207de8c5221SBryant G. Ly 2208de8c5221SBryant G. Ly info = &udev->uio_info; 2209de8c5221SBryant G. Ly 2210c97840c8SMike Christie mutex_lock(&udev->cmdr_lock); 221198effe47SAndy Shevchenko udev->data_bitmap = bitmap_zalloc(udev->max_blocks, GFP_KERNEL); 2212c97840c8SMike Christie mutex_unlock(&udev->cmdr_lock); 2213a24e7917SWei Yongjun if (!udev->data_bitmap) { 2214a24e7917SWei Yongjun ret = -ENOMEM; 221580eb8761SMike Christie goto err_bitmap_alloc; 2216a24e7917SWei Yongjun } 221780eb8761SMike Christie 2218c7ede4f0SGuixin Liu mb = vzalloc(udev->cmdr_size + CMDR_OFF); 2219ecddbb7eSBodo Stroesser if (!mb) { 22207c9e7a6fSAndy Grover ret = -ENOMEM; 22217c9e7a6fSAndy Grover goto err_vzalloc; 22227c9e7a6fSAndy Grover } 22237c9e7a6fSAndy Grover 22247c9e7a6fSAndy Grover /* mailbox fits in first part of CMDR space */ 2225ecddbb7eSBodo Stroesser udev->mb_addr = mb; 2226ecddbb7eSBodo Stroesser udev->cmdr = (void *)mb + CMDR_OFF; 2227c7ede4f0SGuixin Liu udev->data_off = udev->cmdr_size + CMDR_OFF; 2228f5ce815fSBodo Stroesser data_size = TCMU_MBS_TO_PAGES(udev->data_area_mb) << PAGE_SHIFT; 2229c7ede4f0SGuixin Liu udev->mmap_pages = (data_size + udev->cmdr_size + CMDR_OFF) >> PAGE_SHIFT; 2230e719afdcSBodo Stroesser udev->data_blk_size = udev->data_pages_per_blk * PAGE_SIZE; 2231b6df4b79SXiubo Li udev->dbi_thresh = 0; /* Default in Idle state */ 22327c9e7a6fSAndy Grover 2233141685a3SXiubo Li /* Initialise the mailbox of the ring buffer */ 22340ad46af8SAndy Grover mb->version = TCMU_MAILBOX_VERSION; 2235bc2d214aSBodo Stroesser mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC | 2236bc2d214aSBodo Stroesser TCMU_MAILBOX_FLAG_CAP_READ_LEN | 2237018c1491SBodo Stroesser TCMU_MAILBOX_FLAG_CAP_TMR | 2238018c1491SBodo Stroesser TCMU_MAILBOX_FLAG_CAP_KEEP_BUF; 22397c9e7a6fSAndy Grover mb->cmdr_off = CMDR_OFF; 22407c9e7a6fSAndy Grover mb->cmdr_size = udev->cmdr_size; 22417c9e7a6fSAndy Grover 22427c9e7a6fSAndy Grover WARN_ON(!PAGE_ALIGNED(udev->data_off)); 2243f5ce815fSBodo Stroesser WARN_ON(data_size % PAGE_SIZE); 22447c9e7a6fSAndy Grover 2245ac64a2ceSDavid Disseldorp info->version = __stringify(TCMU_MAILBOX_VERSION); 22467c9e7a6fSAndy Grover 22477c9e7a6fSAndy Grover info->mem[0].name = "tcm-user command & data buffer"; 22480633e123SArnd Bergmann info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr; 2249c7ede4f0SGuixin Liu info->mem[0].size = data_size + udev->cmdr_size + CMDR_OFF; 2250141685a3SXiubo Li info->mem[0].memtype = UIO_MEM_NONE; 22517c9e7a6fSAndy Grover 22527c9e7a6fSAndy Grover info->irqcontrol = tcmu_irqcontrol; 22537c9e7a6fSAndy Grover info->irq = UIO_IRQ_CUSTOM; 22547c9e7a6fSAndy Grover 22557c9e7a6fSAndy Grover info->mmap = tcmu_mmap; 22567c9e7a6fSAndy Grover info->open = tcmu_open; 22577c9e7a6fSAndy Grover info->release = tcmu_release; 22587c9e7a6fSAndy Grover 22597c9e7a6fSAndy Grover ret = uio_register_device(tcmu_root_device, info); 22607c9e7a6fSAndy Grover if (ret) 22617c9e7a6fSAndy Grover goto err_register; 22627c9e7a6fSAndy Grover 226381ee28deSSheng Yang /* User can set hw_block_size before enable the device */ 226481ee28deSSheng Yang if (dev->dev_attrib.hw_block_size == 0) 22657c9e7a6fSAndy Grover dev->dev_attrib.hw_block_size = 512; 226681ee28deSSheng Yang /* Other attributes can be configured in userspace */ 22673abaa2bfSMike Christie if (!dev->dev_attrib.hw_max_sectors) 22687c9e7a6fSAndy Grover dev->dev_attrib.hw_max_sectors = 128; 22699a8bb606SBryant G. Ly if (!dev->dev_attrib.emulate_write_cache) 22709a8bb606SBryant G. Ly dev->dev_attrib.emulate_write_cache = 0; 22717c9e7a6fSAndy Grover dev->dev_attrib.hw_queue_depth = 128; 22727c9e7a6fSAndy Grover 2273b849b456SKenjiro Nakayama /* If user didn't explicitly disable netlink reply support, use 2274b849b456SKenjiro Nakayama * module scope setting. 2275b849b456SKenjiro Nakayama */ 2276b849b456SKenjiro Nakayama if (udev->nl_reply_supported >= 0) 2277b849b456SKenjiro Nakayama udev->nl_reply_supported = tcmu_kern_cmd_reply_supported; 2278b849b456SKenjiro Nakayama 2279f3cdbe39SMike Christie /* 2280f3cdbe39SMike Christie * Get a ref incase userspace does a close on the uio device before 2281f3cdbe39SMike Christie * LIO has initiated tcmu_free_device. 2282f3cdbe39SMike Christie */ 2283f3cdbe39SMike Christie kref_get(&udev->kref); 2284f3cdbe39SMike Christie 2285e0c240acSZhu Lingshan ret = tcmu_send_dev_add_event(udev); 22867c9e7a6fSAndy Grover if (ret) 22877c9e7a6fSAndy Grover goto err_netlink; 22887c9e7a6fSAndy Grover 2289b6df4b79SXiubo Li mutex_lock(&root_udev_mutex); 2290b6df4b79SXiubo Li list_add(&udev->node, &root_udev); 2291b6df4b79SXiubo Li mutex_unlock(&root_udev_mutex); 2292b6df4b79SXiubo Li 22937c9e7a6fSAndy Grover return 0; 22947c9e7a6fSAndy Grover 22957c9e7a6fSAndy Grover err_netlink: 2296f3cdbe39SMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 22977c9e7a6fSAndy Grover uio_unregister_device(&udev->uio_info); 22987c9e7a6fSAndy Grover err_register: 22997c9e7a6fSAndy Grover vfree(udev->mb_addr); 2300c22adc0bSXiubo Li udev->mb_addr = NULL; 23017c9e7a6fSAndy Grover err_vzalloc: 230298effe47SAndy Shevchenko bitmap_free(udev->data_bitmap); 230380eb8761SMike Christie udev->data_bitmap = NULL; 230480eb8761SMike Christie err_bitmap_alloc: 23057c9e7a6fSAndy Grover kfree(info->name); 2306f3cdbe39SMike Christie info->name = NULL; 23077c9e7a6fSAndy Grover 23087c9e7a6fSAndy Grover return ret; 23097c9e7a6fSAndy Grover } 23107c9e7a6fSAndy Grover 23117c9e7a6fSAndy Grover static void tcmu_free_device(struct se_device *dev) 23127c9e7a6fSAndy Grover { 23137c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 231492634706SMike Christie 231592634706SMike Christie /* release ref from init */ 231692634706SMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 231792634706SMike Christie } 231892634706SMike Christie 231992634706SMike Christie static void tcmu_destroy_device(struct se_device *dev) 232092634706SMike Christie { 232192634706SMike Christie struct tcmu_dev *udev = TCMU_DEV(dev); 23227c9e7a6fSAndy Grover 23239103575aSMike Christie del_timer_sync(&udev->cmd_timer); 23249103575aSMike Christie del_timer_sync(&udev->qfull_timer); 23257c9e7a6fSAndy Grover 2326b6df4b79SXiubo Li mutex_lock(&root_udev_mutex); 2327b6df4b79SXiubo Li list_del(&udev->node); 2328b6df4b79SXiubo Li mutex_unlock(&root_udev_mutex); 2329b6df4b79SXiubo Li 2330f892bd8eSZhu Lingshan tcmu_send_dev_remove_event(udev); 23317c9e7a6fSAndy Grover 23327c9e7a6fSAndy Grover uio_unregister_device(&udev->uio_info); 23339260695dSMike Christie 23349260695dSMike Christie /* release ref from configure */ 23359260695dSMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 23367c9e7a6fSAndy Grover } 23377c9e7a6fSAndy Grover 2338892782caSMike Christie static void tcmu_unblock_dev(struct tcmu_dev *udev) 2339892782caSMike Christie { 2340892782caSMike Christie mutex_lock(&udev->cmdr_lock); 2341892782caSMike Christie clear_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags); 2342892782caSMike Christie mutex_unlock(&udev->cmdr_lock); 2343892782caSMike Christie } 2344892782caSMike Christie 2345892782caSMike Christie static void tcmu_block_dev(struct tcmu_dev *udev) 2346892782caSMike Christie { 2347892782caSMike Christie mutex_lock(&udev->cmdr_lock); 2348892782caSMike Christie 2349892782caSMike Christie if (test_and_set_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) 2350892782caSMike Christie goto unlock; 2351892782caSMike Christie 2352892782caSMike Christie /* complete IO that has executed successfully */ 2353892782caSMike Christie tcmu_handle_completions(udev); 2354892782caSMike Christie /* fail IO waiting to be queued */ 2355a94a2572SXiubo Li run_qfull_queue(udev, true); 2356892782caSMike Christie 2357892782caSMike Christie unlock: 2358892782caSMike Christie mutex_unlock(&udev->cmdr_lock); 2359892782caSMike Christie } 2360892782caSMike Christie 2361892782caSMike Christie static void tcmu_reset_ring(struct tcmu_dev *udev, u8 err_level) 2362892782caSMike Christie { 2363892782caSMike Christie struct tcmu_mailbox *mb; 2364892782caSMike Christie struct tcmu_cmd *cmd; 2365d3cbb743SBodo Stroesser unsigned long i; 2366892782caSMike Christie 2367892782caSMike Christie mutex_lock(&udev->cmdr_lock); 2368892782caSMike Christie 2369d3cbb743SBodo Stroesser xa_for_each(&udev->commands, i, cmd) { 2370018c1491SBodo Stroesser pr_debug("removing cmd %u on dev %s from ring %s\n", 2371892782caSMike Christie cmd->cmd_id, udev->name, 2372018c1491SBodo Stroesser test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags) ? 2373018c1491SBodo Stroesser "(is expired)" : 2374018c1491SBodo Stroesser (test_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags) ? 2375018c1491SBodo Stroesser "(is keep buffer)" : "")); 2376892782caSMike Christie 2377d3cbb743SBodo Stroesser xa_erase(&udev->commands, i); 2378018c1491SBodo Stroesser if (!test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags) && 2379018c1491SBodo Stroesser !test_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags)) { 2380a86a7586SDmitry Fomichev WARN_ON(!cmd->se_cmd); 2381a94a2572SXiubo Li list_del_init(&cmd->queue_entry); 2382780e1384SShin'ichiro Kawasaki cmd->se_cmd->priv = NULL; 2383892782caSMike Christie if (err_level == 1) { 2384892782caSMike Christie /* 2385892782caSMike Christie * Userspace was not able to start the 2386892782caSMike Christie * command or it is retryable. 2387892782caSMike Christie */ 2388892782caSMike Christie target_complete_cmd(cmd->se_cmd, SAM_STAT_BUSY); 2389892782caSMike Christie } else { 2390892782caSMike Christie /* hard failure */ 2391892782caSMike Christie target_complete_cmd(cmd->se_cmd, 2392892782caSMike Christie SAM_STAT_CHECK_CONDITION); 2393892782caSMike Christie } 2394892782caSMike Christie } 2395892782caSMike Christie tcmu_cmd_free_data(cmd, cmd->dbi_cnt); 2396892782caSMike Christie tcmu_free_cmd(cmd); 2397892782caSMike Christie } 2398892782caSMike Christie 2399892782caSMike Christie mb = udev->mb_addr; 2400892782caSMike Christie tcmu_flush_dcache_range(mb, sizeof(*mb)); 2401892782caSMike Christie pr_debug("mb last %u head %u tail %u\n", udev->cmdr_last_cleaned, 2402892782caSMike Christie mb->cmd_tail, mb->cmd_head); 2403892782caSMike Christie 2404892782caSMike Christie udev->cmdr_last_cleaned = 0; 2405892782caSMike Christie mb->cmd_tail = 0; 2406892782caSMike Christie mb->cmd_head = 0; 2407892782caSMike Christie tcmu_flush_dcache_range(mb, sizeof(*mb)); 2408066f79a5SBodo Stroesser clear_bit(TCMU_DEV_BIT_BROKEN, &udev->flags); 2409892782caSMike Christie 2410892782caSMike Christie del_timer(&udev->cmd_timer); 2411892782caSMike Christie 2412bc2d214aSBodo Stroesser /* 2413bc2d214aSBodo Stroesser * ring is empty and qfull queue never contains aborted commands. 2414bc2d214aSBodo Stroesser * So TMRs in tmr queue do not contain relevant cmd_ids. 2415bc2d214aSBodo Stroesser * After a ring reset userspace should do a fresh start, so 2416bc2d214aSBodo Stroesser * even LUN RESET message is no longer relevant. 2417bc2d214aSBodo Stroesser * Therefore remove all TMRs from qfull queue 2418bc2d214aSBodo Stroesser */ 2419bc2d214aSBodo Stroesser tcmu_remove_all_queued_tmr(udev); 2420bc2d214aSBodo Stroesser 242161fb2482SBodo Stroesser run_qfull_queue(udev, false); 242261fb2482SBodo Stroesser 2423892782caSMike Christie mutex_unlock(&udev->cmdr_lock); 2424892782caSMike Christie } 2425892782caSMike Christie 24267c9e7a6fSAndy Grover enum { 24273abaa2bfSMike Christie Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors, 242808976cb5SBodo Stroesser Opt_nl_reply_supported, Opt_max_data_area_mb, Opt_data_pages_per_blk, 2429c7ede4f0SGuixin Liu Opt_cmd_ring_size_mb, Opt_err, 24307c9e7a6fSAndy Grover }; 24317c9e7a6fSAndy Grover 24327c9e7a6fSAndy Grover static match_table_t tokens = { 24337c9e7a6fSAndy Grover {Opt_dev_config, "dev_config=%s"}, 24340e0d7526SMike Christie {Opt_dev_size, "dev_size=%s"}, 2435b60cb1f8SMike Christie {Opt_hw_block_size, "hw_block_size=%d"}, 2436b60cb1f8SMike Christie {Opt_hw_max_sectors, "hw_max_sectors=%d"}, 2437b849b456SKenjiro Nakayama {Opt_nl_reply_supported, "nl_reply_supported=%d"}, 2438c97840c8SMike Christie {Opt_max_data_area_mb, "max_data_area_mb=%d"}, 243908976cb5SBodo Stroesser {Opt_data_pages_per_blk, "data_pages_per_blk=%d"}, 2440c7ede4f0SGuixin Liu {Opt_cmd_ring_size_mb, "cmd_ring_size_mb=%d"}, 24417c9e7a6fSAndy Grover {Opt_err, NULL} 24427c9e7a6fSAndy Grover }; 24437c9e7a6fSAndy Grover 24443abaa2bfSMike Christie static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib) 24453abaa2bfSMike Christie { 2446b60cb1f8SMike Christie int val, ret; 24473abaa2bfSMike Christie 2448b60cb1f8SMike Christie ret = match_int(arg, &val); 24493abaa2bfSMike Christie if (ret < 0) { 2450b60cb1f8SMike Christie pr_err("match_int() failed for dev attrib. Error %d.\n", 2451b60cb1f8SMike Christie ret); 24523abaa2bfSMike Christie return ret; 24533abaa2bfSMike Christie } 2454b60cb1f8SMike Christie 2455b60cb1f8SMike Christie if (val <= 0) { 2456b60cb1f8SMike Christie pr_err("Invalid dev attrib value %d. Must be greater than zero.\n", 2457b60cb1f8SMike Christie val); 24583abaa2bfSMike Christie return -EINVAL; 24593abaa2bfSMike Christie } 2460b60cb1f8SMike Christie *dev_attrib = val; 24613abaa2bfSMike Christie return 0; 24623abaa2bfSMike Christie } 24633abaa2bfSMike Christie 2464c97840c8SMike Christie static int tcmu_set_max_blocks_param(struct tcmu_dev *udev, substring_t *arg) 2465c97840c8SMike Christie { 2466f5ce815fSBodo Stroesser int val, ret; 2467e719afdcSBodo Stroesser uint32_t pages_per_blk = udev->data_pages_per_blk; 2468c97840c8SMike Christie 2469c97840c8SMike Christie ret = match_int(arg, &val); 2470c97840c8SMike Christie if (ret < 0) { 2471c97840c8SMike Christie pr_err("match_int() failed for max_data_area_mb=. Error %d.\n", 2472c97840c8SMike Christie ret); 2473c97840c8SMike Christie return ret; 2474c97840c8SMike Christie } 2475f5ce815fSBodo Stroesser if (val <= 0) { 2476c97840c8SMike Christie pr_err("Invalid max_data_area %d.\n", val); 2477c97840c8SMike Christie return -EINVAL; 2478c97840c8SMike Christie } 2479f5ce815fSBodo Stroesser if (val > TCMU_PAGES_TO_MBS(tcmu_global_max_pages)) { 2480f5ce815fSBodo Stroesser pr_err("%d is too large. Adjusting max_data_area_mb to global limit of %u\n", 2481f5ce815fSBodo Stroesser val, TCMU_PAGES_TO_MBS(tcmu_global_max_pages)); 2482f5ce815fSBodo Stroesser val = TCMU_PAGES_TO_MBS(tcmu_global_max_pages); 2483f5ce815fSBodo Stroesser } 2484e719afdcSBodo Stroesser if (TCMU_MBS_TO_PAGES(val) < pages_per_blk) { 2485e719afdcSBodo Stroesser pr_err("Invalid max_data_area %d (%zu pages): smaller than data_pages_per_blk (%u pages).\n", 2486e719afdcSBodo Stroesser val, TCMU_MBS_TO_PAGES(val), pages_per_blk); 2487f5ce815fSBodo Stroesser return -EINVAL; 2488f5ce815fSBodo Stroesser } 2489c97840c8SMike Christie 2490c97840c8SMike Christie mutex_lock(&udev->cmdr_lock); 2491c97840c8SMike Christie if (udev->data_bitmap) { 2492c97840c8SMike Christie pr_err("Cannot set max_data_area_mb after it has been enabled.\n"); 2493c97840c8SMike Christie ret = -EINVAL; 2494c97840c8SMike Christie goto unlock; 2495c97840c8SMike Christie } 2496c97840c8SMike Christie 2497f5ce815fSBodo Stroesser udev->data_area_mb = val; 2498e719afdcSBodo Stroesser udev->max_blocks = TCMU_MBS_TO_PAGES(val) / pages_per_blk; 2499c97840c8SMike Christie 2500c97840c8SMike Christie unlock: 2501c97840c8SMike Christie mutex_unlock(&udev->cmdr_lock); 2502c97840c8SMike Christie return ret; 2503c97840c8SMike Christie } 2504c97840c8SMike Christie 250508976cb5SBodo Stroesser static int tcmu_set_data_pages_per_blk(struct tcmu_dev *udev, substring_t *arg) 250608976cb5SBodo Stroesser { 250708976cb5SBodo Stroesser int val, ret; 250808976cb5SBodo Stroesser 250908976cb5SBodo Stroesser ret = match_int(arg, &val); 251008976cb5SBodo Stroesser if (ret < 0) { 251108976cb5SBodo Stroesser pr_err("match_int() failed for data_pages_per_blk=. Error %d.\n", 251208976cb5SBodo Stroesser ret); 251308976cb5SBodo Stroesser return ret; 251408976cb5SBodo Stroesser } 251508976cb5SBodo Stroesser 251608976cb5SBodo Stroesser if (val > TCMU_MBS_TO_PAGES(udev->data_area_mb)) { 251708976cb5SBodo Stroesser pr_err("Invalid data_pages_per_blk %d: greater than max_data_area_mb %d -> %zd pages).\n", 251808976cb5SBodo Stroesser val, udev->data_area_mb, 251908976cb5SBodo Stroesser TCMU_MBS_TO_PAGES(udev->data_area_mb)); 252008976cb5SBodo Stroesser return -EINVAL; 252108976cb5SBodo Stroesser } 252208976cb5SBodo Stroesser 252308976cb5SBodo Stroesser mutex_lock(&udev->cmdr_lock); 252408976cb5SBodo Stroesser if (udev->data_bitmap) { 252508976cb5SBodo Stroesser pr_err("Cannot set data_pages_per_blk after it has been enabled.\n"); 252608976cb5SBodo Stroesser ret = -EINVAL; 252708976cb5SBodo Stroesser goto unlock; 252808976cb5SBodo Stroesser } 252908976cb5SBodo Stroesser 253008976cb5SBodo Stroesser udev->data_pages_per_blk = val; 253108976cb5SBodo Stroesser udev->max_blocks = TCMU_MBS_TO_PAGES(udev->data_area_mb) / val; 253208976cb5SBodo Stroesser 253308976cb5SBodo Stroesser unlock: 253408976cb5SBodo Stroesser mutex_unlock(&udev->cmdr_lock); 253508976cb5SBodo Stroesser return ret; 253608976cb5SBodo Stroesser } 253708976cb5SBodo Stroesser 2538c7ede4f0SGuixin Liu static int tcmu_set_cmd_ring_size(struct tcmu_dev *udev, substring_t *arg) 2539c7ede4f0SGuixin Liu { 2540c7ede4f0SGuixin Liu int val, ret; 2541c7ede4f0SGuixin Liu 2542c7ede4f0SGuixin Liu ret = match_int(arg, &val); 2543c7ede4f0SGuixin Liu if (ret < 0) { 2544c7ede4f0SGuixin Liu pr_err("match_int() failed for cmd_ring_size_mb=. Error %d.\n", 2545c7ede4f0SGuixin Liu ret); 2546c7ede4f0SGuixin Liu return ret; 2547c7ede4f0SGuixin Liu } 2548c7ede4f0SGuixin Liu 2549c7ede4f0SGuixin Liu if (val <= 0) { 2550c7ede4f0SGuixin Liu pr_err("Invalid cmd_ring_size_mb %d.\n", val); 2551c7ede4f0SGuixin Liu return -EINVAL; 2552c7ede4f0SGuixin Liu } 2553c7ede4f0SGuixin Liu 2554c7ede4f0SGuixin Liu mutex_lock(&udev->cmdr_lock); 2555c7ede4f0SGuixin Liu if (udev->data_bitmap) { 2556c7ede4f0SGuixin Liu pr_err("Cannot set cmd_ring_size_mb after it has been enabled.\n"); 2557c7ede4f0SGuixin Liu ret = -EINVAL; 2558c7ede4f0SGuixin Liu goto unlock; 2559c7ede4f0SGuixin Liu } 2560c7ede4f0SGuixin Liu 2561c7ede4f0SGuixin Liu udev->cmdr_size = (val << 20) - CMDR_OFF; 2562c7ede4f0SGuixin Liu if (val > (MB_CMDR_SIZE_DEF >> 20)) { 2563c7ede4f0SGuixin Liu pr_err("%d is too large. Adjusting cmd_ring_size_mb to global limit of %u\n", 2564c7ede4f0SGuixin Liu val, (MB_CMDR_SIZE_DEF >> 20)); 2565c7ede4f0SGuixin Liu udev->cmdr_size = CMDR_SIZE_DEF; 2566c7ede4f0SGuixin Liu } 2567c7ede4f0SGuixin Liu 2568c7ede4f0SGuixin Liu unlock: 2569c7ede4f0SGuixin Liu mutex_unlock(&udev->cmdr_lock); 2570c7ede4f0SGuixin Liu return ret; 2571c7ede4f0SGuixin Liu } 2572c7ede4f0SGuixin Liu 25737c9e7a6fSAndy Grover static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev, 25747c9e7a6fSAndy Grover const char *page, ssize_t count) 25757c9e7a6fSAndy Grover { 25767c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 25770e0d7526SMike Christie char *orig, *ptr, *opts; 25787c9e7a6fSAndy Grover substring_t args[MAX_OPT_ARGS]; 2579c97840c8SMike Christie int ret = 0, token; 25807c9e7a6fSAndy Grover 25817c9e7a6fSAndy Grover opts = kstrdup(page, GFP_KERNEL); 25827c9e7a6fSAndy Grover if (!opts) 25837c9e7a6fSAndy Grover return -ENOMEM; 25847c9e7a6fSAndy Grover 25857c9e7a6fSAndy Grover orig = opts; 25867c9e7a6fSAndy Grover 25877c9e7a6fSAndy Grover while ((ptr = strsep(&opts, ",\n")) != NULL) { 25887c9e7a6fSAndy Grover if (!*ptr) 25897c9e7a6fSAndy Grover continue; 25907c9e7a6fSAndy Grover 25917c9e7a6fSAndy Grover token = match_token(ptr, tokens, args); 25927c9e7a6fSAndy Grover switch (token) { 25937c9e7a6fSAndy Grover case Opt_dev_config: 25947c9e7a6fSAndy Grover if (match_strlcpy(udev->dev_config, &args[0], 25957c9e7a6fSAndy Grover TCMU_CONFIG_LEN) == 0) { 25967c9e7a6fSAndy Grover ret = -EINVAL; 25977c9e7a6fSAndy Grover break; 25987c9e7a6fSAndy Grover } 25997c9e7a6fSAndy Grover pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config); 26007c9e7a6fSAndy Grover break; 26017c9e7a6fSAndy Grover case Opt_dev_size: 26020e0d7526SMike Christie ret = match_u64(&args[0], &udev->dev_size); 26037c9e7a6fSAndy Grover if (ret < 0) 26040e0d7526SMike Christie pr_err("match_u64() failed for dev_size=. Error %d.\n", 26050e0d7526SMike Christie ret); 26067c9e7a6fSAndy Grover break; 26079c1cd1b6SAndy Grover case Opt_hw_block_size: 26083abaa2bfSMike Christie ret = tcmu_set_dev_attrib(&args[0], 26093abaa2bfSMike Christie &(dev->dev_attrib.hw_block_size)); 26109c1cd1b6SAndy Grover break; 26113abaa2bfSMike Christie case Opt_hw_max_sectors: 26123abaa2bfSMike Christie ret = tcmu_set_dev_attrib(&args[0], 26133abaa2bfSMike Christie &(dev->dev_attrib.hw_max_sectors)); 26149c1cd1b6SAndy Grover break; 2615b849b456SKenjiro Nakayama case Opt_nl_reply_supported: 2616b60cb1f8SMike Christie ret = match_int(&args[0], &udev->nl_reply_supported); 2617b849b456SKenjiro Nakayama if (ret < 0) 2618b60cb1f8SMike Christie pr_err("match_int() failed for nl_reply_supported=. Error %d.\n", 2619b60cb1f8SMike Christie ret); 2620b849b456SKenjiro Nakayama break; 262180eb8761SMike Christie case Opt_max_data_area_mb: 2622c97840c8SMike Christie ret = tcmu_set_max_blocks_param(udev, &args[0]); 262380eb8761SMike Christie break; 262408976cb5SBodo Stroesser case Opt_data_pages_per_blk: 262508976cb5SBodo Stroesser ret = tcmu_set_data_pages_per_blk(udev, &args[0]); 262608976cb5SBodo Stroesser break; 2627c7ede4f0SGuixin Liu case Opt_cmd_ring_size_mb: 2628c7ede4f0SGuixin Liu ret = tcmu_set_cmd_ring_size(udev, &args[0]); 2629c7ede4f0SGuixin Liu break; 26307c9e7a6fSAndy Grover default: 26317c9e7a6fSAndy Grover break; 26327c9e7a6fSAndy Grover } 26332579325cSMike Christie 26342579325cSMike Christie if (ret) 26352579325cSMike Christie break; 26367c9e7a6fSAndy Grover } 26377c9e7a6fSAndy Grover 26387c9e7a6fSAndy Grover kfree(orig); 26397c9e7a6fSAndy Grover return (!ret) ? count : ret; 26407c9e7a6fSAndy Grover } 26417c9e7a6fSAndy Grover 26427c9e7a6fSAndy Grover static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b) 26437c9e7a6fSAndy Grover { 26447c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 26457c9e7a6fSAndy Grover ssize_t bl = 0; 26467c9e7a6fSAndy Grover 26477c9e7a6fSAndy Grover bl = sprintf(b + bl, "Config: %s ", 26487c9e7a6fSAndy Grover udev->dev_config[0] ? udev->dev_config : "NULL"); 26490e0d7526SMike Christie bl += sprintf(b + bl, "Size: %llu ", udev->dev_size); 265008976cb5SBodo Stroesser bl += sprintf(b + bl, "MaxDataAreaMB: %u ", udev->data_area_mb); 2651c7ede4f0SGuixin Liu bl += sprintf(b + bl, "DataPagesPerBlk: %u ", udev->data_pages_per_blk); 2652c7ede4f0SGuixin Liu bl += sprintf(b + bl, "CmdRingSizeMB: %u\n", 2653c7ede4f0SGuixin Liu (udev->cmdr_size + CMDR_OFF) >> 20); 26547c9e7a6fSAndy Grover 26557c9e7a6fSAndy Grover return bl; 26567c9e7a6fSAndy Grover } 26577c9e7a6fSAndy Grover 26587c9e7a6fSAndy Grover static sector_t tcmu_get_blocks(struct se_device *dev) 26597c9e7a6fSAndy Grover { 26607c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 26617c9e7a6fSAndy Grover 26627c9e7a6fSAndy Grover return div_u64(udev->dev_size - dev->dev_attrib.block_size, 26637c9e7a6fSAndy Grover dev->dev_attrib.block_size); 26647c9e7a6fSAndy Grover } 26657c9e7a6fSAndy Grover 26667c9e7a6fSAndy Grover static sense_reason_t 26677c9e7a6fSAndy Grover tcmu_parse_cdb(struct se_cmd *cmd) 26687c9e7a6fSAndy Grover { 266902eb924fSAndy Grover return passthrough_parse_cdb(cmd, tcmu_queue_cmd); 26709c1cd1b6SAndy Grover } 26719c1cd1b6SAndy Grover 26727d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page) 26737d7a7435SNicholas Bellinger { 26747d7a7435SNicholas Bellinger struct se_dev_attrib *da = container_of(to_config_group(item), 26757d7a7435SNicholas Bellinger struct se_dev_attrib, da_group); 2676b5ab697cSKenjiro Nakayama struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 26777d7a7435SNicholas Bellinger 26787d7a7435SNicholas Bellinger return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC); 26797d7a7435SNicholas Bellinger } 26807d7a7435SNicholas Bellinger 26817d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page, 26827d7a7435SNicholas Bellinger size_t count) 26837d7a7435SNicholas Bellinger { 26847d7a7435SNicholas Bellinger struct se_dev_attrib *da = container_of(to_config_group(item), 26857d7a7435SNicholas Bellinger struct se_dev_attrib, da_group); 26867d7a7435SNicholas Bellinger struct tcmu_dev *udev = container_of(da->da_dev, 26877d7a7435SNicholas Bellinger struct tcmu_dev, se_dev); 26887d7a7435SNicholas Bellinger u32 val; 26897d7a7435SNicholas Bellinger int ret; 26907d7a7435SNicholas Bellinger 26917d7a7435SNicholas Bellinger if (da->da_dev->export_count) { 26927d7a7435SNicholas Bellinger pr_err("Unable to set tcmu cmd_time_out while exports exist\n"); 26937d7a7435SNicholas Bellinger return -EINVAL; 26947d7a7435SNicholas Bellinger } 26957d7a7435SNicholas Bellinger 26967d7a7435SNicholas Bellinger ret = kstrtou32(page, 0, &val); 26977d7a7435SNicholas Bellinger if (ret < 0) 26987d7a7435SNicholas Bellinger return ret; 26997d7a7435SNicholas Bellinger 27007d7a7435SNicholas Bellinger udev->cmd_time_out = val * MSEC_PER_SEC; 27017d7a7435SNicholas Bellinger return count; 27027d7a7435SNicholas Bellinger } 27037d7a7435SNicholas Bellinger CONFIGFS_ATTR(tcmu_, cmd_time_out); 27047d7a7435SNicholas Bellinger 27059103575aSMike Christie static ssize_t tcmu_qfull_time_out_show(struct config_item *item, char *page) 27069103575aSMike Christie { 27079103575aSMike Christie struct se_dev_attrib *da = container_of(to_config_group(item), 27089103575aSMike Christie struct se_dev_attrib, da_group); 27099103575aSMike Christie struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 27109103575aSMike Christie 27119103575aSMike Christie return snprintf(page, PAGE_SIZE, "%ld\n", udev->qfull_time_out <= 0 ? 27129103575aSMike Christie udev->qfull_time_out : 27139103575aSMike Christie udev->qfull_time_out / MSEC_PER_SEC); 27149103575aSMike Christie } 27159103575aSMike Christie 27169103575aSMike Christie static ssize_t tcmu_qfull_time_out_store(struct config_item *item, 27179103575aSMike Christie const char *page, size_t count) 27189103575aSMike Christie { 27199103575aSMike Christie struct se_dev_attrib *da = container_of(to_config_group(item), 27209103575aSMike Christie struct se_dev_attrib, da_group); 27219103575aSMike Christie struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 27229103575aSMike Christie s32 val; 27239103575aSMike Christie int ret; 27249103575aSMike Christie 27259103575aSMike Christie ret = kstrtos32(page, 0, &val); 27269103575aSMike Christie if (ret < 0) 27279103575aSMike Christie return ret; 27289103575aSMike Christie 27299103575aSMike Christie if (val >= 0) { 27309103575aSMike Christie udev->qfull_time_out = val * MSEC_PER_SEC; 2731125966dbSPrasanna Kumar Kalever } else if (val == -1) { 2732125966dbSPrasanna Kumar Kalever udev->qfull_time_out = val; 27339103575aSMike Christie } else { 27349103575aSMike Christie printk(KERN_ERR "Invalid qfull timeout value %d\n", val); 27359103575aSMike Christie return -EINVAL; 27369103575aSMike Christie } 27379103575aSMike Christie return count; 27389103575aSMike Christie } 27399103575aSMike Christie CONFIGFS_ATTR(tcmu_, qfull_time_out); 27409103575aSMike Christie 274180eb8761SMike Christie static ssize_t tcmu_max_data_area_mb_show(struct config_item *item, char *page) 274280eb8761SMike Christie { 274380eb8761SMike Christie struct se_dev_attrib *da = container_of(to_config_group(item), 274480eb8761SMike Christie struct se_dev_attrib, da_group); 274580eb8761SMike Christie struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 274680eb8761SMike Christie 2747f5ce815fSBodo Stroesser return snprintf(page, PAGE_SIZE, "%u\n", udev->data_area_mb); 274880eb8761SMike Christie } 274980eb8761SMike Christie CONFIGFS_ATTR_RO(tcmu_, max_data_area_mb); 275080eb8761SMike Christie 275108976cb5SBodo Stroesser static ssize_t tcmu_data_pages_per_blk_show(struct config_item *item, 275208976cb5SBodo Stroesser char *page) 275308976cb5SBodo Stroesser { 275408976cb5SBodo Stroesser struct se_dev_attrib *da = container_of(to_config_group(item), 275508976cb5SBodo Stroesser struct se_dev_attrib, da_group); 275608976cb5SBodo Stroesser struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 275708976cb5SBodo Stroesser 275808976cb5SBodo Stroesser return snprintf(page, PAGE_SIZE, "%u\n", udev->data_pages_per_blk); 275908976cb5SBodo Stroesser } 276008976cb5SBodo Stroesser CONFIGFS_ATTR_RO(tcmu_, data_pages_per_blk); 276108976cb5SBodo Stroesser 2762c7ede4f0SGuixin Liu static ssize_t tcmu_cmd_ring_size_mb_show(struct config_item *item, char *page) 2763c7ede4f0SGuixin Liu { 2764c7ede4f0SGuixin Liu struct se_dev_attrib *da = container_of(to_config_group(item), 2765c7ede4f0SGuixin Liu struct se_dev_attrib, da_group); 2766c7ede4f0SGuixin Liu struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2767c7ede4f0SGuixin Liu 2768c7ede4f0SGuixin Liu return snprintf(page, PAGE_SIZE, "%u\n", 2769c7ede4f0SGuixin Liu (udev->cmdr_size + CMDR_OFF) >> 20); 2770c7ede4f0SGuixin Liu } 2771c7ede4f0SGuixin Liu CONFIGFS_ATTR_RO(tcmu_, cmd_ring_size_mb); 2772c7ede4f0SGuixin Liu 27732d76443eSMike Christie static ssize_t tcmu_dev_config_show(struct config_item *item, char *page) 2774ee018252SBryant G. Ly { 2775ee018252SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 2776ee018252SBryant G. Ly struct se_dev_attrib, da_group); 2777ee018252SBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2778ee018252SBryant G. Ly 2779ee018252SBryant G. Ly return snprintf(page, PAGE_SIZE, "%s\n", udev->dev_config); 2780ee018252SBryant G. Ly } 2781ee018252SBryant G. Ly 278202ccfb54SZhu Lingshan static int tcmu_send_dev_config_event(struct tcmu_dev *udev, 278302ccfb54SZhu Lingshan const char *reconfig_data) 278402ccfb54SZhu Lingshan { 278502ccfb54SZhu Lingshan struct sk_buff *skb = NULL; 278602ccfb54SZhu Lingshan void *msg_header = NULL; 278702ccfb54SZhu Lingshan int ret = 0; 278802ccfb54SZhu Lingshan 278902ccfb54SZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE, 279002ccfb54SZhu Lingshan &skb, &msg_header); 279102ccfb54SZhu Lingshan if (ret < 0) 279202ccfb54SZhu Lingshan return ret; 279302ccfb54SZhu Lingshan ret = nla_put_string(skb, TCMU_ATTR_DEV_CFG, reconfig_data); 279402ccfb54SZhu Lingshan if (ret < 0) { 279502ccfb54SZhu Lingshan nlmsg_free(skb); 279602ccfb54SZhu Lingshan return ret; 279702ccfb54SZhu Lingshan } 279802ccfb54SZhu Lingshan return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE, 279906add777SMike Christie skb, msg_header); 280002ccfb54SZhu Lingshan } 280102ccfb54SZhu Lingshan 280202ccfb54SZhu Lingshan 28032d76443eSMike Christie static ssize_t tcmu_dev_config_store(struct config_item *item, const char *page, 2804ee018252SBryant G. Ly size_t count) 2805ee018252SBryant G. Ly { 2806ee018252SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 2807ee018252SBryant G. Ly struct se_dev_attrib, da_group); 2808ee018252SBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 28092d76443eSMike Christie int ret, len; 2810ee018252SBryant G. Ly 28112d76443eSMike Christie len = strlen(page); 28122d76443eSMike Christie if (!len || len > TCMU_CONFIG_LEN - 1) 2813ee018252SBryant G. Ly return -EINVAL; 2814ee018252SBryant G. Ly 2815ee018252SBryant G. Ly /* Check if device has been configured before */ 281663d5be0fSMike Christie if (target_dev_configured(&udev->se_dev)) { 281702ccfb54SZhu Lingshan ret = tcmu_send_dev_config_event(udev, page); 2818ee018252SBryant G. Ly if (ret) { 2819ee018252SBryant G. Ly pr_err("Unable to reconfigure device\n"); 2820ee018252SBryant G. Ly return ret; 2821ee018252SBryant G. Ly } 2822de8c5221SBryant G. Ly strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN); 2823de8c5221SBryant G. Ly 2824de8c5221SBryant G. Ly ret = tcmu_update_uio_info(udev); 2825de8c5221SBryant G. Ly if (ret) 2826de8c5221SBryant G. Ly return ret; 2827de8c5221SBryant G. Ly return count; 2828ee018252SBryant G. Ly } 28292d76443eSMike Christie strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN); 2830ee018252SBryant G. Ly 2831ee018252SBryant G. Ly return count; 2832ee018252SBryant G. Ly } 28332d76443eSMike Christie CONFIGFS_ATTR(tcmu_, dev_config); 2834ee018252SBryant G. Ly 2835801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_show(struct config_item *item, char *page) 2836801fc54dSBryant G. Ly { 2837801fc54dSBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 2838801fc54dSBryant G. Ly struct se_dev_attrib, da_group); 2839801fc54dSBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2840801fc54dSBryant G. Ly 28410e0d7526SMike Christie return snprintf(page, PAGE_SIZE, "%llu\n", udev->dev_size); 2842801fc54dSBryant G. Ly } 2843801fc54dSBryant G. Ly 284484e28506SZhu Lingshan static int tcmu_send_dev_size_event(struct tcmu_dev *udev, u64 size) 284584e28506SZhu Lingshan { 284684e28506SZhu Lingshan struct sk_buff *skb = NULL; 284784e28506SZhu Lingshan void *msg_header = NULL; 284884e28506SZhu Lingshan int ret = 0; 284984e28506SZhu Lingshan 285084e28506SZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE, 285184e28506SZhu Lingshan &skb, &msg_header); 285284e28506SZhu Lingshan if (ret < 0) 285384e28506SZhu Lingshan return ret; 285484e28506SZhu Lingshan ret = nla_put_u64_64bit(skb, TCMU_ATTR_DEV_SIZE, 285584e28506SZhu Lingshan size, TCMU_ATTR_PAD); 285684e28506SZhu Lingshan if (ret < 0) { 285784e28506SZhu Lingshan nlmsg_free(skb); 285884e28506SZhu Lingshan return ret; 285984e28506SZhu Lingshan } 286084e28506SZhu Lingshan return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE, 286106add777SMike Christie skb, msg_header); 286284e28506SZhu Lingshan } 286384e28506SZhu Lingshan 2864801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page, 2865801fc54dSBryant G. Ly size_t count) 2866801fc54dSBryant G. Ly { 2867801fc54dSBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 2868801fc54dSBryant G. Ly struct se_dev_attrib, da_group); 2869801fc54dSBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 28702d76443eSMike Christie u64 val; 2871801fc54dSBryant G. Ly int ret; 2872801fc54dSBryant G. Ly 28732d76443eSMike Christie ret = kstrtou64(page, 0, &val); 2874801fc54dSBryant G. Ly if (ret < 0) 2875801fc54dSBryant G. Ly return ret; 2876801fc54dSBryant G. Ly 2877801fc54dSBryant G. Ly /* Check if device has been configured before */ 287863d5be0fSMike Christie if (target_dev_configured(&udev->se_dev)) { 287984e28506SZhu Lingshan ret = tcmu_send_dev_size_event(udev, val); 2880801fc54dSBryant G. Ly if (ret) { 2881801fc54dSBryant G. Ly pr_err("Unable to reconfigure device\n"); 2882801fc54dSBryant G. Ly return ret; 2883801fc54dSBryant G. Ly } 2884801fc54dSBryant G. Ly } 28852d76443eSMike Christie udev->dev_size = val; 2886801fc54dSBryant G. Ly return count; 2887801fc54dSBryant G. Ly } 2888801fc54dSBryant G. Ly CONFIGFS_ATTR(tcmu_, dev_size); 2889801fc54dSBryant G. Ly 2890b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_show(struct config_item *item, 2891b849b456SKenjiro Nakayama char *page) 2892b849b456SKenjiro Nakayama { 2893b849b456SKenjiro Nakayama struct se_dev_attrib *da = container_of(to_config_group(item), 2894b849b456SKenjiro Nakayama struct se_dev_attrib, da_group); 2895b849b456SKenjiro Nakayama struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2896b849b456SKenjiro Nakayama 2897b849b456SKenjiro Nakayama return snprintf(page, PAGE_SIZE, "%d\n", udev->nl_reply_supported); 2898b849b456SKenjiro Nakayama } 2899b849b456SKenjiro Nakayama 2900b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_store(struct config_item *item, 2901b849b456SKenjiro Nakayama const char *page, size_t count) 2902b849b456SKenjiro Nakayama { 2903b849b456SKenjiro Nakayama struct se_dev_attrib *da = container_of(to_config_group(item), 2904b849b456SKenjiro Nakayama struct se_dev_attrib, da_group); 2905b849b456SKenjiro Nakayama struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2906b849b456SKenjiro Nakayama s8 val; 2907b849b456SKenjiro Nakayama int ret; 2908b849b456SKenjiro Nakayama 2909b849b456SKenjiro Nakayama ret = kstrtos8(page, 0, &val); 2910b849b456SKenjiro Nakayama if (ret < 0) 2911b849b456SKenjiro Nakayama return ret; 2912b849b456SKenjiro Nakayama 2913b849b456SKenjiro Nakayama udev->nl_reply_supported = val; 2914b849b456SKenjiro Nakayama return count; 2915b849b456SKenjiro Nakayama } 2916b849b456SKenjiro Nakayama CONFIGFS_ATTR(tcmu_, nl_reply_supported); 2917b849b456SKenjiro Nakayama 29189a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_show(struct config_item *item, 29199a8bb606SBryant G. Ly char *page) 29209a8bb606SBryant G. Ly { 29219a8bb606SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 29229a8bb606SBryant G. Ly struct se_dev_attrib, da_group); 29239a8bb606SBryant G. Ly 29249a8bb606SBryant G. Ly return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache); 29259a8bb606SBryant G. Ly } 29269a8bb606SBryant G. Ly 292733d065ccSZhu Lingshan static int tcmu_send_emulate_write_cache(struct tcmu_dev *udev, u8 val) 292833d065ccSZhu Lingshan { 292933d065ccSZhu Lingshan struct sk_buff *skb = NULL; 293033d065ccSZhu Lingshan void *msg_header = NULL; 293133d065ccSZhu Lingshan int ret = 0; 293233d065ccSZhu Lingshan 293333d065ccSZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE, 293433d065ccSZhu Lingshan &skb, &msg_header); 293533d065ccSZhu Lingshan if (ret < 0) 293633d065ccSZhu Lingshan return ret; 293733d065ccSZhu Lingshan ret = nla_put_u8(skb, TCMU_ATTR_WRITECACHE, val); 293833d065ccSZhu Lingshan if (ret < 0) { 293933d065ccSZhu Lingshan nlmsg_free(skb); 294033d065ccSZhu Lingshan return ret; 294133d065ccSZhu Lingshan } 294233d065ccSZhu Lingshan return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE, 294306add777SMike Christie skb, msg_header); 294433d065ccSZhu Lingshan } 294533d065ccSZhu Lingshan 29469a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_store(struct config_item *item, 29479a8bb606SBryant G. Ly const char *page, size_t count) 29489a8bb606SBryant G. Ly { 29499a8bb606SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 29509a8bb606SBryant G. Ly struct se_dev_attrib, da_group); 29511068be7bSBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 29522d76443eSMike Christie u8 val; 29539a8bb606SBryant G. Ly int ret; 29549a8bb606SBryant G. Ly 29552d76443eSMike Christie ret = kstrtou8(page, 0, &val); 29569a8bb606SBryant G. Ly if (ret < 0) 29579a8bb606SBryant G. Ly return ret; 29589a8bb606SBryant G. Ly 29591068be7bSBryant G. Ly /* Check if device has been configured before */ 296063d5be0fSMike Christie if (target_dev_configured(&udev->se_dev)) { 296133d065ccSZhu Lingshan ret = tcmu_send_emulate_write_cache(udev, val); 29621068be7bSBryant G. Ly if (ret) { 29631068be7bSBryant G. Ly pr_err("Unable to reconfigure device\n"); 29641068be7bSBryant G. Ly return ret; 29651068be7bSBryant G. Ly } 29661068be7bSBryant G. Ly } 29672d76443eSMike Christie 29682d76443eSMike Christie da->emulate_write_cache = val; 29699a8bb606SBryant G. Ly return count; 29709a8bb606SBryant G. Ly } 29719a8bb606SBryant G. Ly CONFIGFS_ATTR(tcmu_, emulate_write_cache); 29729a8bb606SBryant G. Ly 297359526d7aSBodo Stroesser static ssize_t tcmu_tmr_notification_show(struct config_item *item, char *page) 297459526d7aSBodo Stroesser { 297559526d7aSBodo Stroesser struct se_dev_attrib *da = container_of(to_config_group(item), 297659526d7aSBodo Stroesser struct se_dev_attrib, da_group); 297759526d7aSBodo Stroesser struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 297859526d7aSBodo Stroesser 297959526d7aSBodo Stroesser return snprintf(page, PAGE_SIZE, "%i\n", 298059526d7aSBodo Stroesser test_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags)); 298159526d7aSBodo Stroesser } 298259526d7aSBodo Stroesser 298359526d7aSBodo Stroesser static ssize_t tcmu_tmr_notification_store(struct config_item *item, 298459526d7aSBodo Stroesser const char *page, size_t count) 298559526d7aSBodo Stroesser { 298659526d7aSBodo Stroesser struct se_dev_attrib *da = container_of(to_config_group(item), 298759526d7aSBodo Stroesser struct se_dev_attrib, da_group); 298859526d7aSBodo Stroesser struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 298959526d7aSBodo Stroesser u8 val; 299059526d7aSBodo Stroesser int ret; 299159526d7aSBodo Stroesser 299259526d7aSBodo Stroesser ret = kstrtou8(page, 0, &val); 299359526d7aSBodo Stroesser if (ret < 0) 299459526d7aSBodo Stroesser return ret; 299559526d7aSBodo Stroesser if (val > 1) 299659526d7aSBodo Stroesser return -EINVAL; 299759526d7aSBodo Stroesser 299859526d7aSBodo Stroesser if (val) 299959526d7aSBodo Stroesser set_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags); 300059526d7aSBodo Stroesser else 300159526d7aSBodo Stroesser clear_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags); 300259526d7aSBodo Stroesser return count; 300359526d7aSBodo Stroesser } 300459526d7aSBodo Stroesser CONFIGFS_ATTR(tcmu_, tmr_notification); 300559526d7aSBodo Stroesser 3006892782caSMike Christie static ssize_t tcmu_block_dev_show(struct config_item *item, char *page) 3007892782caSMike Christie { 3008892782caSMike Christie struct se_device *se_dev = container_of(to_config_group(item), 3009892782caSMike Christie struct se_device, 3010892782caSMike Christie dev_action_group); 3011892782caSMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 3012892782caSMike Christie 3013892782caSMike Christie if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) 3014892782caSMike Christie return snprintf(page, PAGE_SIZE, "%s\n", "blocked"); 3015892782caSMike Christie else 3016892782caSMike Christie return snprintf(page, PAGE_SIZE, "%s\n", "unblocked"); 3017892782caSMike Christie } 3018892782caSMike Christie 3019892782caSMike Christie static ssize_t tcmu_block_dev_store(struct config_item *item, const char *page, 3020892782caSMike Christie size_t count) 3021892782caSMike Christie { 3022892782caSMike Christie struct se_device *se_dev = container_of(to_config_group(item), 3023892782caSMike Christie struct se_device, 3024892782caSMike Christie dev_action_group); 3025892782caSMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 3026892782caSMike Christie u8 val; 3027892782caSMike Christie int ret; 3028892782caSMike Christie 3029a30b0473SMike Christie if (!target_dev_configured(&udev->se_dev)) { 3030a30b0473SMike Christie pr_err("Device is not configured.\n"); 3031a30b0473SMike Christie return -EINVAL; 3032a30b0473SMike Christie } 3033a30b0473SMike Christie 3034892782caSMike Christie ret = kstrtou8(page, 0, &val); 3035892782caSMike Christie if (ret < 0) 3036892782caSMike Christie return ret; 3037892782caSMike Christie 3038892782caSMike Christie if (val > 1) { 3039892782caSMike Christie pr_err("Invalid block value %d\n", val); 3040892782caSMike Christie return -EINVAL; 3041892782caSMike Christie } 3042892782caSMike Christie 3043892782caSMike Christie if (!val) 3044892782caSMike Christie tcmu_unblock_dev(udev); 3045892782caSMike Christie else 3046892782caSMike Christie tcmu_block_dev(udev); 3047892782caSMike Christie return count; 3048892782caSMike Christie } 3049892782caSMike Christie CONFIGFS_ATTR(tcmu_, block_dev); 3050892782caSMike Christie 3051892782caSMike Christie static ssize_t tcmu_reset_ring_store(struct config_item *item, const char *page, 3052892782caSMike Christie size_t count) 3053892782caSMike Christie { 3054892782caSMike Christie struct se_device *se_dev = container_of(to_config_group(item), 3055892782caSMike Christie struct se_device, 3056892782caSMike Christie dev_action_group); 3057892782caSMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 3058892782caSMike Christie u8 val; 3059892782caSMike Christie int ret; 3060892782caSMike Christie 3061a30b0473SMike Christie if (!target_dev_configured(&udev->se_dev)) { 3062a30b0473SMike Christie pr_err("Device is not configured.\n"); 3063a30b0473SMike Christie return -EINVAL; 3064a30b0473SMike Christie } 3065a30b0473SMike Christie 3066892782caSMike Christie ret = kstrtou8(page, 0, &val); 3067892782caSMike Christie if (ret < 0) 3068892782caSMike Christie return ret; 3069892782caSMike Christie 3070892782caSMike Christie if (val != 1 && val != 2) { 3071892782caSMike Christie pr_err("Invalid reset ring value %d\n", val); 3072892782caSMike Christie return -EINVAL; 3073892782caSMike Christie } 3074892782caSMike Christie 3075892782caSMike Christie tcmu_reset_ring(udev, val); 3076892782caSMike Christie return count; 3077892782caSMike Christie } 3078892782caSMike Christie CONFIGFS_ATTR_WO(tcmu_, reset_ring); 3079892782caSMike Christie 3080018c1491SBodo Stroesser static ssize_t tcmu_free_kept_buf_store(struct config_item *item, const char *page, 3081018c1491SBodo Stroesser size_t count) 3082018c1491SBodo Stroesser { 3083018c1491SBodo Stroesser struct se_device *se_dev = container_of(to_config_group(item), 3084018c1491SBodo Stroesser struct se_device, 3085018c1491SBodo Stroesser dev_action_group); 3086018c1491SBodo Stroesser struct tcmu_dev *udev = TCMU_DEV(se_dev); 3087018c1491SBodo Stroesser struct tcmu_cmd *cmd; 3088018c1491SBodo Stroesser u16 cmd_id; 3089018c1491SBodo Stroesser int ret; 3090018c1491SBodo Stroesser 3091018c1491SBodo Stroesser if (!target_dev_configured(&udev->se_dev)) { 3092018c1491SBodo Stroesser pr_err("Device is not configured.\n"); 3093018c1491SBodo Stroesser return -EINVAL; 3094018c1491SBodo Stroesser } 3095018c1491SBodo Stroesser 3096018c1491SBodo Stroesser ret = kstrtou16(page, 0, &cmd_id); 3097018c1491SBodo Stroesser if (ret < 0) 3098018c1491SBodo Stroesser return ret; 3099018c1491SBodo Stroesser 3100018c1491SBodo Stroesser mutex_lock(&udev->cmdr_lock); 3101018c1491SBodo Stroesser 3102018c1491SBodo Stroesser { 3103018c1491SBodo Stroesser XA_STATE(xas, &udev->commands, cmd_id); 3104018c1491SBodo Stroesser 3105018c1491SBodo Stroesser xas_lock(&xas); 3106018c1491SBodo Stroesser cmd = xas_load(&xas); 3107018c1491SBodo Stroesser if (!cmd) { 3108018c1491SBodo Stroesser pr_err("free_kept_buf: cmd_id %d not found\n", cmd_id); 3109018c1491SBodo Stroesser count = -EINVAL; 3110018c1491SBodo Stroesser xas_unlock(&xas); 3111018c1491SBodo Stroesser goto out_unlock; 3112018c1491SBodo Stroesser } 3113018c1491SBodo Stroesser if (!test_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags)) { 3114018c1491SBodo Stroesser pr_err("free_kept_buf: cmd_id %d was not completed with KEEP_BUF\n", 3115018c1491SBodo Stroesser cmd_id); 3116018c1491SBodo Stroesser count = -EINVAL; 3117018c1491SBodo Stroesser xas_unlock(&xas); 3118018c1491SBodo Stroesser goto out_unlock; 3119018c1491SBodo Stroesser } 3120018c1491SBodo Stroesser xas_store(&xas, NULL); 3121018c1491SBodo Stroesser xas_unlock(&xas); 3122018c1491SBodo Stroesser } 3123018c1491SBodo Stroesser 3124018c1491SBodo Stroesser tcmu_cmd_free_data(cmd, cmd->dbi_cnt); 3125018c1491SBodo Stroesser tcmu_free_cmd(cmd); 3126018c1491SBodo Stroesser /* 3127018c1491SBodo Stroesser * We only freed data space, not ring space. Therefore we dont call 3128018c1491SBodo Stroesser * run_tmr_queue, but call run_qfull_queue if tmr_list is empty. 3129018c1491SBodo Stroesser */ 3130018c1491SBodo Stroesser if (list_empty(&udev->tmr_queue)) 3131018c1491SBodo Stroesser run_qfull_queue(udev, false); 3132018c1491SBodo Stroesser 3133018c1491SBodo Stroesser out_unlock: 3134018c1491SBodo Stroesser mutex_unlock(&udev->cmdr_lock); 3135018c1491SBodo Stroesser return count; 3136018c1491SBodo Stroesser } 3137018c1491SBodo Stroesser CONFIGFS_ATTR_WO(tcmu_, free_kept_buf); 3138018c1491SBodo Stroesser 31395821783bSColin Ian King static struct configfs_attribute *tcmu_attrib_attrs[] = { 3140801fc54dSBryant G. Ly &tcmu_attr_cmd_time_out, 31419103575aSMike Christie &tcmu_attr_qfull_time_out, 314280eb8761SMike Christie &tcmu_attr_max_data_area_mb, 314308976cb5SBodo Stroesser &tcmu_attr_data_pages_per_blk, 3144c7ede4f0SGuixin Liu &tcmu_attr_cmd_ring_size_mb, 31452d76443eSMike Christie &tcmu_attr_dev_config, 3146801fc54dSBryant G. Ly &tcmu_attr_dev_size, 3147801fc54dSBryant G. Ly &tcmu_attr_emulate_write_cache, 314859526d7aSBodo Stroesser &tcmu_attr_tmr_notification, 3149b849b456SKenjiro Nakayama &tcmu_attr_nl_reply_supported, 3150801fc54dSBryant G. Ly NULL, 3151801fc54dSBryant G. Ly }; 3152801fc54dSBryant G. Ly 31537d7a7435SNicholas Bellinger static struct configfs_attribute **tcmu_attrs; 31547d7a7435SNicholas Bellinger 3155892782caSMike Christie static struct configfs_attribute *tcmu_action_attrs[] = { 3156892782caSMike Christie &tcmu_attr_block_dev, 3157892782caSMike Christie &tcmu_attr_reset_ring, 3158018c1491SBodo Stroesser &tcmu_attr_free_kept_buf, 3159892782caSMike Christie NULL, 3160892782caSMike Christie }; 3161892782caSMike Christie 31627d7a7435SNicholas Bellinger static struct target_backend_ops tcmu_ops = { 31637c9e7a6fSAndy Grover .name = "user", 31647c9e7a6fSAndy Grover .owner = THIS_MODULE, 316569088a04SBodo Stroesser .transport_flags_default = TRANSPORT_FLAG_PASSTHROUGH, 3166356ba2a8SBodo Stroesser .transport_flags_changeable = TRANSPORT_FLAG_PASSTHROUGH_PGR | 3167356ba2a8SBodo Stroesser TRANSPORT_FLAG_PASSTHROUGH_ALUA, 31687c9e7a6fSAndy Grover .attach_hba = tcmu_attach_hba, 31697c9e7a6fSAndy Grover .detach_hba = tcmu_detach_hba, 31707c9e7a6fSAndy Grover .alloc_device = tcmu_alloc_device, 31717c9e7a6fSAndy Grover .configure_device = tcmu_configure_device, 317292634706SMike Christie .destroy_device = tcmu_destroy_device, 31737c9e7a6fSAndy Grover .free_device = tcmu_free_device, 31746888da81SMike Christie .unplug_device = tcmu_unplug_device, 31756888da81SMike Christie .plug_device = tcmu_plug_device, 31767c9e7a6fSAndy Grover .parse_cdb = tcmu_parse_cdb, 3177bc2d214aSBodo Stroesser .tmr_notify = tcmu_tmr_notify, 31787c9e7a6fSAndy Grover .set_configfs_dev_params = tcmu_set_configfs_dev_params, 31797c9e7a6fSAndy Grover .show_configfs_dev_params = tcmu_show_configfs_dev_params, 31807c9e7a6fSAndy Grover .get_device_type = sbc_get_device_type, 31817c9e7a6fSAndy Grover .get_blocks = tcmu_get_blocks, 3182892782caSMike Christie .tb_dev_action_attrs = tcmu_action_attrs, 31837c9e7a6fSAndy Grover }; 31847c9e7a6fSAndy Grover 318589ec9cfdSMike Christie static void find_free_blocks(void) 3186b6df4b79SXiubo Li { 3187b6df4b79SXiubo Li struct tcmu_dev *udev; 3188b6df4b79SXiubo Li loff_t off; 3189f5ce815fSBodo Stroesser u32 pages_freed, total_pages_freed = 0; 3190f5ce815fSBodo Stroesser u32 start, end, block, total_blocks_freed = 0; 3191af1dd7ffSMike Christie 31928b084d9dSBodo Stroesser if (atomic_read(&global_page_count) <= tcmu_global_max_pages) 3193af1dd7ffSMike Christie return; 3194b6df4b79SXiubo Li 3195b6df4b79SXiubo Li mutex_lock(&root_udev_mutex); 3196b6df4b79SXiubo Li list_for_each_entry(udev, &root_udev, node) { 3197b6df4b79SXiubo Li mutex_lock(&udev->cmdr_lock); 3198b6df4b79SXiubo Li 3199dc335a99SMike Christie if (!target_dev_configured(&udev->se_dev)) { 3200dc335a99SMike Christie mutex_unlock(&udev->cmdr_lock); 3201dc335a99SMike Christie continue; 3202dc335a99SMike Christie } 3203dc335a99SMike Christie 3204b6df4b79SXiubo Li /* Try to complete the finished commands first */ 3205bc2d214aSBodo Stroesser if (tcmu_handle_completions(udev)) 3206bc2d214aSBodo Stroesser run_qfull_queue(udev, false); 3207b6df4b79SXiubo Li 3208af1dd7ffSMike Christie /* Skip the udevs in idle */ 3209af1dd7ffSMike Christie if (!udev->dbi_thresh) { 3210b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 3211b6df4b79SXiubo Li continue; 3212b6df4b79SXiubo Li } 3213b6df4b79SXiubo Li 3214b6df4b79SXiubo Li end = udev->dbi_max + 1; 3215b6df4b79SXiubo Li block = find_last_bit(udev->data_bitmap, end); 3216b6df4b79SXiubo Li if (block == udev->dbi_max) { 3217b6df4b79SXiubo Li /* 3218af1dd7ffSMike Christie * The last bit is dbi_max, so it is not possible 3219af1dd7ffSMike Christie * reclaim any blocks. 3220b6df4b79SXiubo Li */ 3221b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 3222b6df4b79SXiubo Li continue; 3223b6df4b79SXiubo Li } else if (block == end) { 3224b6df4b79SXiubo Li /* The current udev will goto idle state */ 3225b6df4b79SXiubo Li udev->dbi_thresh = start = 0; 3226b6df4b79SXiubo Li udev->dbi_max = 0; 3227b6df4b79SXiubo Li } else { 3228b6df4b79SXiubo Li udev->dbi_thresh = start = block + 1; 3229b6df4b79SXiubo Li udev->dbi_max = block; 3230b6df4b79SXiubo Li } 3231b6df4b79SXiubo Li 3232*bb9b9eb0SXiaoguang Wang /* 3233*bb9b9eb0SXiaoguang Wang * Release the block pages. 3234*bb9b9eb0SXiaoguang Wang * 3235*bb9b9eb0SXiaoguang Wang * Also note that since tcmu_vma_fault() gets an extra page 3236*bb9b9eb0SXiaoguang Wang * refcount, tcmu_blocks_release() won't free pages if pages 3237*bb9b9eb0SXiaoguang Wang * are mapped. This means it is safe to call 3238*bb9b9eb0SXiaoguang Wang * tcmu_blocks_release() before unmap_mapping_range() which 3239*bb9b9eb0SXiaoguang Wang * drops the refcount of any pages it unmaps and thus releases 3240*bb9b9eb0SXiaoguang Wang * them. 3241*bb9b9eb0SXiaoguang Wang */ 3242*bb9b9eb0SXiaoguang Wang pages_freed = tcmu_blocks_release(udev, start, end - 1); 3243*bb9b9eb0SXiaoguang Wang 3244b6df4b79SXiubo Li /* Here will truncate the data area from off */ 3245e719afdcSBodo Stroesser off = udev->data_off + (loff_t)start * udev->data_blk_size; 3246b6df4b79SXiubo Li unmap_mapping_range(udev->inode->i_mapping, off, 0, 1); 3247b6df4b79SXiubo Li 3248b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 3249af1dd7ffSMike Christie 3250f5ce815fSBodo Stroesser total_pages_freed += pages_freed; 3251f5ce815fSBodo Stroesser total_blocks_freed += end - start; 3252f5ce815fSBodo Stroesser pr_debug("Freed %u pages (total %u) from %u blocks (total %u) from %s.\n", 3253f5ce815fSBodo Stroesser pages_freed, total_pages_freed, end - start, 3254f5ce815fSBodo Stroesser total_blocks_freed, udev->name); 3255b6df4b79SXiubo Li } 325689ec9cfdSMike Christie mutex_unlock(&root_udev_mutex); 325789ec9cfdSMike Christie 32588b084d9dSBodo Stroesser if (atomic_read(&global_page_count) > tcmu_global_max_pages) 3259af1dd7ffSMike Christie schedule_delayed_work(&tcmu_unmap_work, msecs_to_jiffies(5000)); 3260b6df4b79SXiubo Li } 3261b6df4b79SXiubo Li 3262488ebe4cSMike Christie static void check_timedout_devices(void) 3263488ebe4cSMike Christie { 3264488ebe4cSMike Christie struct tcmu_dev *udev, *tmp_dev; 326561fb2482SBodo Stroesser struct tcmu_cmd *cmd, *tmp_cmd; 3266488ebe4cSMike Christie LIST_HEAD(devs); 3267488ebe4cSMike Christie 3268488ebe4cSMike Christie spin_lock_bh(&timed_out_udevs_lock); 3269488ebe4cSMike Christie list_splice_init(&timed_out_udevs, &devs); 3270488ebe4cSMike Christie 3271488ebe4cSMike Christie list_for_each_entry_safe(udev, tmp_dev, &devs, timedout_entry) { 3272488ebe4cSMike Christie list_del_init(&udev->timedout_entry); 3273488ebe4cSMike Christie spin_unlock_bh(&timed_out_udevs_lock); 3274488ebe4cSMike Christie 32756fddcb77SMike Christie mutex_lock(&udev->cmdr_lock); 3276a94a2572SXiubo Li 327761fb2482SBodo Stroesser /* 327861fb2482SBodo Stroesser * If cmd_time_out is disabled but qfull is set deadline 327961fb2482SBodo Stroesser * will only reflect the qfull timeout. Ignore it. 328061fb2482SBodo Stroesser */ 328161fb2482SBodo Stroesser if (udev->cmd_time_out) { 328261fb2482SBodo Stroesser list_for_each_entry_safe(cmd, tmp_cmd, 328361fb2482SBodo Stroesser &udev->inflight_queue, 328461fb2482SBodo Stroesser queue_entry) { 328561fb2482SBodo Stroesser tcmu_check_expired_ring_cmd(cmd); 328661fb2482SBodo Stroesser } 328761fb2482SBodo Stroesser tcmu_set_next_deadline(&udev->inflight_queue, 328861fb2482SBodo Stroesser &udev->cmd_timer); 328961fb2482SBodo Stroesser } 329061fb2482SBodo Stroesser list_for_each_entry_safe(cmd, tmp_cmd, &udev->qfull_queue, 329161fb2482SBodo Stroesser queue_entry) { 329261fb2482SBodo Stroesser tcmu_check_expired_queue_cmd(cmd); 329361fb2482SBodo Stroesser } 3294a94a2572SXiubo Li tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer); 3295a94a2572SXiubo Li 32966fddcb77SMike Christie mutex_unlock(&udev->cmdr_lock); 3297488ebe4cSMike Christie 3298488ebe4cSMike Christie spin_lock_bh(&timed_out_udevs_lock); 3299488ebe4cSMike Christie } 3300488ebe4cSMike Christie 3301488ebe4cSMike Christie spin_unlock_bh(&timed_out_udevs_lock); 3302488ebe4cSMike Christie } 3303488ebe4cSMike Christie 33049972cebbSMike Christie static void tcmu_unmap_work_fn(struct work_struct *work) 330589ec9cfdSMike Christie { 3306488ebe4cSMike Christie check_timedout_devices(); 330789ec9cfdSMike Christie find_free_blocks(); 330889ec9cfdSMike Christie } 330989ec9cfdSMike Christie 33107c9e7a6fSAndy Grover static int __init tcmu_module_init(void) 33117c9e7a6fSAndy Grover { 3312801fc54dSBryant G. Ly int ret, i, k, len = 0; 33137c9e7a6fSAndy Grover 33147c9e7a6fSAndy Grover BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0); 33157c9e7a6fSAndy Grover 3316af1dd7ffSMike Christie INIT_DELAYED_WORK(&tcmu_unmap_work, tcmu_unmap_work_fn); 33179972cebbSMike Christie 33187c9e7a6fSAndy Grover tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache", 33197c9e7a6fSAndy Grover sizeof(struct tcmu_cmd), 33207c9e7a6fSAndy Grover __alignof__(struct tcmu_cmd), 33217c9e7a6fSAndy Grover 0, NULL); 33227c9e7a6fSAndy Grover if (!tcmu_cmd_cache) 33237c9e7a6fSAndy Grover return -ENOMEM; 33247c9e7a6fSAndy Grover 33257c9e7a6fSAndy Grover tcmu_root_device = root_device_register("tcm_user"); 33267c9e7a6fSAndy Grover if (IS_ERR(tcmu_root_device)) { 33277c9e7a6fSAndy Grover ret = PTR_ERR(tcmu_root_device); 33287c9e7a6fSAndy Grover goto out_free_cache; 33297c9e7a6fSAndy Grover } 33307c9e7a6fSAndy Grover 33317c9e7a6fSAndy Grover ret = genl_register_family(&tcmu_genl_family); 33327c9e7a6fSAndy Grover if (ret < 0) { 33337c9e7a6fSAndy Grover goto out_unreg_device; 33347c9e7a6fSAndy Grover } 33357c9e7a6fSAndy Grover 33364703b625SBodo Stroesser for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) 33377d7a7435SNicholas Bellinger len += sizeof(struct configfs_attribute *); 33384703b625SBodo Stroesser for (i = 0; passthrough_pr_attrib_attrs[i] != NULL; i++) 3339801fc54dSBryant G. Ly len += sizeof(struct configfs_attribute *); 33404703b625SBodo Stroesser for (i = 0; tcmu_attrib_attrs[i] != NULL; i++) 33414703b625SBodo Stroesser len += sizeof(struct configfs_attribute *); 3342801fc54dSBryant G. Ly len += sizeof(struct configfs_attribute *); 33437d7a7435SNicholas Bellinger 33447d7a7435SNicholas Bellinger tcmu_attrs = kzalloc(len, GFP_KERNEL); 33457d7a7435SNicholas Bellinger if (!tcmu_attrs) { 33467d7a7435SNicholas Bellinger ret = -ENOMEM; 33477d7a7435SNicholas Bellinger goto out_unreg_genl; 33487d7a7435SNicholas Bellinger } 33497d7a7435SNicholas Bellinger 33504703b625SBodo Stroesser for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) 33517d7a7435SNicholas Bellinger tcmu_attrs[i] = passthrough_attrib_attrs[i]; 33524703b625SBodo Stroesser for (k = 0; passthrough_pr_attrib_attrs[k] != NULL; k++) 33534703b625SBodo Stroesser tcmu_attrs[i++] = passthrough_pr_attrib_attrs[k]; 33544703b625SBodo Stroesser for (k = 0; tcmu_attrib_attrs[k] != NULL; k++) 33554703b625SBodo Stroesser tcmu_attrs[i++] = tcmu_attrib_attrs[k]; 33567d7a7435SNicholas Bellinger tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs; 33577d7a7435SNicholas Bellinger 33580a06d430SChristoph Hellwig ret = transport_backend_register(&tcmu_ops); 33597c9e7a6fSAndy Grover if (ret) 33607d7a7435SNicholas Bellinger goto out_attrs; 33617c9e7a6fSAndy Grover 33627c9e7a6fSAndy Grover return 0; 33637c9e7a6fSAndy Grover 33647d7a7435SNicholas Bellinger out_attrs: 33657d7a7435SNicholas Bellinger kfree(tcmu_attrs); 33667c9e7a6fSAndy Grover out_unreg_genl: 33677c9e7a6fSAndy Grover genl_unregister_family(&tcmu_genl_family); 33687c9e7a6fSAndy Grover out_unreg_device: 33697c9e7a6fSAndy Grover root_device_unregister(tcmu_root_device); 33707c9e7a6fSAndy Grover out_free_cache: 33717c9e7a6fSAndy Grover kmem_cache_destroy(tcmu_cmd_cache); 33727c9e7a6fSAndy Grover 33737c9e7a6fSAndy Grover return ret; 33747c9e7a6fSAndy Grover } 33757c9e7a6fSAndy Grover 33767c9e7a6fSAndy Grover static void __exit tcmu_module_exit(void) 33777c9e7a6fSAndy Grover { 3378af1dd7ffSMike Christie cancel_delayed_work_sync(&tcmu_unmap_work); 33790a06d430SChristoph Hellwig target_backend_unregister(&tcmu_ops); 33807d7a7435SNicholas Bellinger kfree(tcmu_attrs); 33817c9e7a6fSAndy Grover genl_unregister_family(&tcmu_genl_family); 33827c9e7a6fSAndy Grover root_device_unregister(tcmu_root_device); 33837c9e7a6fSAndy Grover kmem_cache_destroy(tcmu_cmd_cache); 33847c9e7a6fSAndy Grover } 33857c9e7a6fSAndy Grover 33867c9e7a6fSAndy Grover MODULE_DESCRIPTION("TCM USER subsystem plugin"); 33877c9e7a6fSAndy Grover MODULE_AUTHOR("Shaohua Li <shli@kernel.org>"); 33887c9e7a6fSAndy Grover MODULE_AUTHOR("Andy Grover <agrover@redhat.com>"); 33897c9e7a6fSAndy Grover MODULE_LICENSE("GPL"); 33907c9e7a6fSAndy Grover 33917c9e7a6fSAndy Grover module_init(tcmu_module_init); 33927c9e7a6fSAndy Grover module_exit(tcmu_module_exit); 3393