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> 237c9e7a6fSAndy Grover #include <net/genetlink.h> 24ba929992SBart Van Assche #include <scsi/scsi_common.h> 25ba929992SBart Van Assche #include <scsi/scsi_proto.h> 267c9e7a6fSAndy Grover #include <target/target_core_base.h> 277c9e7a6fSAndy Grover #include <target/target_core_fabric.h> 287c9e7a6fSAndy Grover #include <target/target_core_backend.h> 29e9f720d6SNicholas Bellinger 307c9e7a6fSAndy Grover #include <linux/target_core_user.h> 317c9e7a6fSAndy Grover 32572ccdabSRandy Dunlap /** 33572ccdabSRandy Dunlap * DOC: Userspace I/O 34572ccdabSRandy Dunlap * Userspace I/O 35572ccdabSRandy Dunlap * ------------- 36572ccdabSRandy Dunlap * 377c9e7a6fSAndy Grover * Define a shared-memory interface for LIO to pass SCSI commands and 387c9e7a6fSAndy Grover * data to userspace for processing. This is to allow backends that 397c9e7a6fSAndy Grover * are too complex for in-kernel support to be possible. 407c9e7a6fSAndy Grover * 417c9e7a6fSAndy Grover * It uses the UIO framework to do a lot of the device-creation and 427c9e7a6fSAndy Grover * introspection work for us. 437c9e7a6fSAndy Grover * 447c9e7a6fSAndy Grover * See the .h file for how the ring is laid out. Note that while the 457c9e7a6fSAndy Grover * command ring is defined, the particulars of the data area are 467c9e7a6fSAndy Grover * not. Offset values in the command entry point to other locations 47572ccdabSRandy Dunlap * internal to the mmap-ed area. There is separate space outside the 487c9e7a6fSAndy Grover * command ring for data buffers. This leaves maximum flexibility for 497c9e7a6fSAndy Grover * moving buffer allocations, or even page flipping or other 507c9e7a6fSAndy Grover * allocation techniques, without altering the command ring layout. 517c9e7a6fSAndy Grover * 527c9e7a6fSAndy Grover * SECURITY: 537c9e7a6fSAndy Grover * The user process must be assumed to be malicious. There's no way to 547c9e7a6fSAndy Grover * prevent it breaking the command ring protocol if it wants, but in 557c9e7a6fSAndy Grover * order to prevent other issues we must only ever read *data* from 567c9e7a6fSAndy Grover * the shared memory area, not offsets or sizes. This applies to 577c9e7a6fSAndy Grover * command ring entries as well as the mailbox. Extra code needed for 587c9e7a6fSAndy Grover * this may have a 'UAM' comment. 597c9e7a6fSAndy Grover */ 607c9e7a6fSAndy Grover 617c9e7a6fSAndy Grover #define TCMU_TIME_OUT (30 * MSEC_PER_SEC) 627c9e7a6fSAndy Grover 63ecddbb7eSBodo Stroesser /* For mailbox plus cmd ring, the size is fixed 8MB */ 64c7ede4f0SGuixin Liu #define MB_CMDR_SIZE_DEF (8 * 1024 * 1024) 65ecddbb7eSBodo Stroesser /* Offset of cmd ring is size of mailbox */ 66c7ede4f0SGuixin Liu #define CMDR_OFF ((__u32)sizeof(struct tcmu_mailbox)) 67c7ede4f0SGuixin Liu #define CMDR_SIZE_DEF (MB_CMDR_SIZE_DEF - CMDR_OFF) 6826418649SSheng Yang 69b6df4b79SXiubo Li /* 70f5ce815fSBodo Stroesser * For data area, the default block size is PAGE_SIZE and 71f5ce815fSBodo Stroesser * the default total size is 256K * PAGE_SIZE. 72b6df4b79SXiubo Li */ 73e719afdcSBodo Stroesser #define DATA_PAGES_PER_BLK_DEF 1 74f5ce815fSBodo Stroesser #define DATA_AREA_PAGES_DEF (256 * 1024) 757c9e7a6fSAndy Grover 76f5ce815fSBodo Stroesser #define TCMU_MBS_TO_PAGES(_mbs) ((size_t)_mbs << (20 - PAGE_SHIFT)) 778b084d9dSBodo Stroesser #define TCMU_PAGES_TO_MBS(_pages) (_pages >> (20 - PAGE_SHIFT)) 7880eb8761SMike Christie 79af1dd7ffSMike Christie /* 80af1dd7ffSMike Christie * Default number of global data blocks(512K * PAGE_SIZE) 81af1dd7ffSMike Christie * when the unmap thread will be started. 82af1dd7ffSMike Christie */ 838b084d9dSBodo Stroesser #define TCMU_GLOBAL_MAX_PAGES_DEF (512 * 1024) 84b6df4b79SXiubo Li 85b3af66e2SMike Christie static u8 tcmu_kern_cmd_reply_supported; 86bdaeedc1SMike Christie static u8 tcmu_netlink_blocked; 87b3af66e2SMike Christie 887c9e7a6fSAndy Grover static struct device *tcmu_root_device; 897c9e7a6fSAndy Grover 907c9e7a6fSAndy Grover struct tcmu_hba { 917c9e7a6fSAndy Grover u32 host_id; 927c9e7a6fSAndy Grover }; 937c9e7a6fSAndy Grover 947c9e7a6fSAndy Grover #define TCMU_CONFIG_LEN 256 957c9e7a6fSAndy Grover 963228691fSMike Christie static DEFINE_MUTEX(tcmu_nl_cmd_mutex); 973228691fSMike Christie static LIST_HEAD(tcmu_nl_cmd_list); 983228691fSMike Christie 993228691fSMike Christie struct tcmu_dev; 1003228691fSMike Christie 101b3af66e2SMike Christie struct tcmu_nl_cmd { 102b3af66e2SMike Christie /* wake up thread waiting for reply */ 103b3af66e2SMike Christie struct completion complete; 1043228691fSMike Christie struct list_head nl_list; 1053228691fSMike Christie struct tcmu_dev *udev; 106b3af66e2SMike Christie int cmd; 107b3af66e2SMike Christie int status; 108b3af66e2SMike Christie }; 109b3af66e2SMike Christie 1107c9e7a6fSAndy Grover struct tcmu_dev { 111b6df4b79SXiubo Li struct list_head node; 112f3cdbe39SMike Christie struct kref kref; 113af1dd7ffSMike Christie 1147c9e7a6fSAndy Grover struct se_device se_dev; 1156888da81SMike Christie struct se_dev_plug se_plug; 1167c9e7a6fSAndy Grover 1177c9e7a6fSAndy Grover char *name; 1187c9e7a6fSAndy Grover struct se_hba *hba; 1197c9e7a6fSAndy Grover 1207c9e7a6fSAndy Grover #define TCMU_DEV_BIT_OPEN 0 1217c9e7a6fSAndy Grover #define TCMU_DEV_BIT_BROKEN 1 122892782caSMike Christie #define TCMU_DEV_BIT_BLOCKED 2 12359526d7aSBodo Stroesser #define TCMU_DEV_BIT_TMR_NOTIFY 3 1243ac0fcb4SBodo Stroesser #define TCMU_DEV_BIT_PLUGGED 4 1257c9e7a6fSAndy Grover unsigned long flags; 1267c9e7a6fSAndy Grover 1277c9e7a6fSAndy Grover struct uio_info uio_info; 1287c9e7a6fSAndy Grover 129b6df4b79SXiubo Li struct inode *inode; 130b6df4b79SXiubo Li 1310e0d7526SMike Christie uint64_t dev_size; 132ecddbb7eSBodo Stroesser 133ecddbb7eSBodo Stroesser struct tcmu_mailbox *mb_addr; 134ecddbb7eSBodo Stroesser void *cmdr; 1357c9e7a6fSAndy Grover u32 cmdr_size; 1367c9e7a6fSAndy Grover u32 cmdr_last_cleaned; 1373d9b9555SAndy Grover /* Offset of data area from start of mb */ 13826418649SSheng Yang /* Must add data_off and mb_addr to get the address */ 1397c9e7a6fSAndy Grover size_t data_off; 140f5ce815fSBodo Stroesser int data_area_mb; 14180eb8761SMike Christie uint32_t max_blocks; 142ecddbb7eSBodo Stroesser size_t mmap_pages; 14326418649SSheng Yang 144b6df4b79SXiubo Li struct mutex cmdr_lock; 145a94a2572SXiubo Li struct list_head qfull_queue; 146bc2d214aSBodo Stroesser struct list_head tmr_queue; 1477c9e7a6fSAndy Grover 148141685a3SXiubo Li uint32_t dbi_max; 149b6df4b79SXiubo Li uint32_t dbi_thresh; 15080eb8761SMike Christie unsigned long *data_bitmap; 1518b084d9dSBodo Stroesser struct xarray data_pages; 152e719afdcSBodo Stroesser uint32_t data_pages_per_blk; 153e719afdcSBodo Stroesser uint32_t data_blk_size; 154141685a3SXiubo Li 155d3cbb743SBodo Stroesser struct xarray commands; 1567c9e7a6fSAndy Grover 1579103575aSMike Christie struct timer_list cmd_timer; 158af980e46SMike Christie unsigned int cmd_time_out; 159a94a2572SXiubo Li struct list_head inflight_queue; 1609103575aSMike Christie 1619103575aSMike Christie struct timer_list qfull_timer; 1629103575aSMike Christie int qfull_time_out; 1639103575aSMike Christie 164488ebe4cSMike Christie struct list_head timedout_entry; 1657c9e7a6fSAndy Grover 166b3af66e2SMike Christie struct tcmu_nl_cmd curr_nl_cmd; 167b3af66e2SMike Christie 1687c9e7a6fSAndy Grover char dev_config[TCMU_CONFIG_LEN]; 169b849b456SKenjiro Nakayama 170b849b456SKenjiro Nakayama int nl_reply_supported; 1717c9e7a6fSAndy Grover }; 1727c9e7a6fSAndy Grover 1737c9e7a6fSAndy Grover #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev) 1747c9e7a6fSAndy Grover 1757c9e7a6fSAndy Grover struct tcmu_cmd { 1767c9e7a6fSAndy Grover struct se_cmd *se_cmd; 1777c9e7a6fSAndy Grover struct tcmu_dev *tcmu_dev; 178a94a2572SXiubo Li struct list_head queue_entry; 1797c9e7a6fSAndy Grover 1807c9e7a6fSAndy Grover uint16_t cmd_id; 1817c9e7a6fSAndy Grover 18226418649SSheng Yang /* Can't use se_cmd when cleaning up expired cmds, because if 1837c9e7a6fSAndy Grover cmd has been completed then accessing se_cmd is off limits */ 184141685a3SXiubo Li uint32_t dbi_cnt; 18552ef2743SBodo Stroesser uint32_t dbi_bidi_cnt; 186141685a3SXiubo Li uint32_t dbi_cur; 187141685a3SXiubo Li uint32_t *dbi; 1887c9e7a6fSAndy Grover 1893c9a7c58SBodo Stroesser uint32_t data_len_bidi; 1903c9a7c58SBodo Stroesser 1917c9e7a6fSAndy Grover unsigned long deadline; 1927c9e7a6fSAndy Grover 1937c9e7a6fSAndy Grover #define TCMU_CMD_BIT_EXPIRED 0 194018c1491SBodo Stroesser #define TCMU_CMD_BIT_KEEP_BUF 1 1957c9e7a6fSAndy Grover unsigned long flags; 1967c9e7a6fSAndy Grover }; 197bc2d214aSBodo Stroesser 198bc2d214aSBodo Stroesser struct tcmu_tmr { 199bc2d214aSBodo Stroesser struct list_head queue_entry; 200bc2d214aSBodo Stroesser 201bc2d214aSBodo Stroesser uint8_t tmr_type; 202bc2d214aSBodo Stroesser uint32_t tmr_cmd_cnt; 2038fdaabe1SGustavo A. R. Silva int16_t tmr_cmd_ids[]; 204bc2d214aSBodo Stroesser }; 205bc2d214aSBodo Stroesser 206af1dd7ffSMike Christie /* 207af1dd7ffSMike Christie * To avoid dead lock the mutex lock order should always be: 208af1dd7ffSMike Christie * 209af1dd7ffSMike Christie * mutex_lock(&root_udev_mutex); 210af1dd7ffSMike Christie * ... 211af1dd7ffSMike Christie * mutex_lock(&tcmu_dev->cmdr_lock); 212af1dd7ffSMike Christie * mutex_unlock(&tcmu_dev->cmdr_lock); 213af1dd7ffSMike Christie * ... 214af1dd7ffSMike Christie * mutex_unlock(&root_udev_mutex); 215af1dd7ffSMike Christie */ 216b6df4b79SXiubo Li static DEFINE_MUTEX(root_udev_mutex); 217b6df4b79SXiubo Li static LIST_HEAD(root_udev); 218b6df4b79SXiubo Li 219488ebe4cSMike Christie static DEFINE_SPINLOCK(timed_out_udevs_lock); 220488ebe4cSMike Christie static LIST_HEAD(timed_out_udevs); 221488ebe4cSMike Christie 22280eb8761SMike Christie static struct kmem_cache *tcmu_cmd_cache; 22380eb8761SMike Christie 2248b084d9dSBodo Stroesser static atomic_t global_page_count = ATOMIC_INIT(0); 225af1dd7ffSMike Christie static struct delayed_work tcmu_unmap_work; 2268b084d9dSBodo Stroesser static int tcmu_global_max_pages = TCMU_GLOBAL_MAX_PAGES_DEF; 227b6df4b79SXiubo Li 22880eb8761SMike Christie static int tcmu_set_global_max_data_area(const char *str, 22980eb8761SMike Christie const struct kernel_param *kp) 23080eb8761SMike Christie { 23180eb8761SMike Christie int ret, max_area_mb; 23280eb8761SMike Christie 23380eb8761SMike Christie ret = kstrtoint(str, 10, &max_area_mb); 23480eb8761SMike Christie if (ret) 23580eb8761SMike Christie return -EINVAL; 23680eb8761SMike Christie 23780eb8761SMike Christie if (max_area_mb <= 0) { 23880eb8761SMike Christie pr_err("global_max_data_area must be larger than 0.\n"); 23980eb8761SMike Christie return -EINVAL; 24080eb8761SMike Christie } 24180eb8761SMike Christie 2428b084d9dSBodo Stroesser tcmu_global_max_pages = TCMU_MBS_TO_PAGES(max_area_mb); 2438b084d9dSBodo Stroesser if (atomic_read(&global_page_count) > tcmu_global_max_pages) 24480eb8761SMike Christie schedule_delayed_work(&tcmu_unmap_work, 0); 24580eb8761SMike Christie else 24680eb8761SMike Christie cancel_delayed_work_sync(&tcmu_unmap_work); 24780eb8761SMike Christie 24880eb8761SMike Christie return 0; 24980eb8761SMike Christie } 25080eb8761SMike Christie 25180eb8761SMike Christie static int tcmu_get_global_max_data_area(char *buffer, 25280eb8761SMike Christie const struct kernel_param *kp) 25380eb8761SMike Christie { 2548b084d9dSBodo Stroesser return sprintf(buffer, "%d\n", TCMU_PAGES_TO_MBS(tcmu_global_max_pages)); 25580eb8761SMike Christie } 25680eb8761SMike Christie 25780eb8761SMike Christie static const struct kernel_param_ops tcmu_global_max_data_area_op = { 25880eb8761SMike Christie .set = tcmu_set_global_max_data_area, 25980eb8761SMike Christie .get = tcmu_get_global_max_data_area, 26080eb8761SMike Christie }; 26180eb8761SMike Christie 26280eb8761SMike Christie module_param_cb(global_max_data_area_mb, &tcmu_global_max_data_area_op, NULL, 26380eb8761SMike Christie S_IWUSR | S_IRUGO); 26480eb8761SMike Christie MODULE_PARM_DESC(global_max_data_area_mb, 26580eb8761SMike Christie "Max MBs allowed to be allocated to all the tcmu device's " 26680eb8761SMike Christie "data areas."); 2677c9e7a6fSAndy Grover 268bdaeedc1SMike Christie static int tcmu_get_block_netlink(char *buffer, 269bdaeedc1SMike Christie const struct kernel_param *kp) 270bdaeedc1SMike Christie { 271bdaeedc1SMike Christie return sprintf(buffer, "%s\n", tcmu_netlink_blocked ? 272bdaeedc1SMike Christie "blocked" : "unblocked"); 273bdaeedc1SMike Christie } 274bdaeedc1SMike Christie 275bdaeedc1SMike Christie static int tcmu_set_block_netlink(const char *str, 276bdaeedc1SMike Christie const struct kernel_param *kp) 277bdaeedc1SMike Christie { 278bdaeedc1SMike Christie int ret; 279bdaeedc1SMike Christie u8 val; 280bdaeedc1SMike Christie 281bdaeedc1SMike Christie ret = kstrtou8(str, 0, &val); 282bdaeedc1SMike Christie if (ret < 0) 283bdaeedc1SMike Christie return ret; 284bdaeedc1SMike Christie 285bdaeedc1SMike Christie if (val > 1) { 286bdaeedc1SMike Christie pr_err("Invalid block netlink value %u\n", val); 287bdaeedc1SMike Christie return -EINVAL; 288bdaeedc1SMike Christie } 289bdaeedc1SMike Christie 290bdaeedc1SMike Christie tcmu_netlink_blocked = val; 291bdaeedc1SMike Christie return 0; 292bdaeedc1SMike Christie } 293bdaeedc1SMike Christie 294bdaeedc1SMike Christie static const struct kernel_param_ops tcmu_block_netlink_op = { 295bdaeedc1SMike Christie .set = tcmu_set_block_netlink, 296bdaeedc1SMike Christie .get = tcmu_get_block_netlink, 297bdaeedc1SMike Christie }; 298bdaeedc1SMike Christie 299bdaeedc1SMike Christie module_param_cb(block_netlink, &tcmu_block_netlink_op, NULL, S_IWUSR | S_IRUGO); 300bdaeedc1SMike Christie MODULE_PARM_DESC(block_netlink, "Block new netlink commands."); 301bdaeedc1SMike Christie 302bdaeedc1SMike Christie static int tcmu_fail_netlink_cmd(struct tcmu_nl_cmd *nl_cmd) 303bdaeedc1SMike Christie { 304bdaeedc1SMike Christie struct tcmu_dev *udev = nl_cmd->udev; 305bdaeedc1SMike Christie 306bdaeedc1SMike Christie if (!tcmu_netlink_blocked) { 307bdaeedc1SMike Christie pr_err("Could not reset device's netlink interface. Netlink is not blocked.\n"); 308bdaeedc1SMike Christie return -EBUSY; 309bdaeedc1SMike Christie } 310bdaeedc1SMike Christie 311bdaeedc1SMike Christie if (nl_cmd->cmd != TCMU_CMD_UNSPEC) { 312bdaeedc1SMike Christie pr_debug("Aborting nl cmd %d on %s\n", nl_cmd->cmd, udev->name); 313bdaeedc1SMike Christie nl_cmd->status = -EINTR; 314bdaeedc1SMike Christie list_del(&nl_cmd->nl_list); 315bdaeedc1SMike Christie complete(&nl_cmd->complete); 316bdaeedc1SMike Christie } 317bdaeedc1SMike Christie return 0; 318bdaeedc1SMike Christie } 319bdaeedc1SMike Christie 320bdaeedc1SMike Christie static int tcmu_set_reset_netlink(const char *str, 321bdaeedc1SMike Christie const struct kernel_param *kp) 322bdaeedc1SMike Christie { 323bdaeedc1SMike Christie struct tcmu_nl_cmd *nl_cmd, *tmp_cmd; 324bdaeedc1SMike Christie int ret; 325bdaeedc1SMike Christie u8 val; 326bdaeedc1SMike Christie 327bdaeedc1SMike Christie ret = kstrtou8(str, 0, &val); 328bdaeedc1SMike Christie if (ret < 0) 329bdaeedc1SMike Christie return ret; 330bdaeedc1SMike Christie 331bdaeedc1SMike Christie if (val != 1) { 332bdaeedc1SMike Christie pr_err("Invalid reset netlink value %u\n", val); 333bdaeedc1SMike Christie return -EINVAL; 334bdaeedc1SMike Christie } 335bdaeedc1SMike Christie 336bdaeedc1SMike Christie mutex_lock(&tcmu_nl_cmd_mutex); 337bdaeedc1SMike Christie list_for_each_entry_safe(nl_cmd, tmp_cmd, &tcmu_nl_cmd_list, nl_list) { 338bdaeedc1SMike Christie ret = tcmu_fail_netlink_cmd(nl_cmd); 339bdaeedc1SMike Christie if (ret) 340bdaeedc1SMike Christie break; 341bdaeedc1SMike Christie } 342bdaeedc1SMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 343bdaeedc1SMike Christie 344bdaeedc1SMike Christie return ret; 345bdaeedc1SMike Christie } 346bdaeedc1SMike Christie 347bdaeedc1SMike Christie static const struct kernel_param_ops tcmu_reset_netlink_op = { 348bdaeedc1SMike Christie .set = tcmu_set_reset_netlink, 349bdaeedc1SMike Christie }; 350bdaeedc1SMike Christie 351bdaeedc1SMike Christie module_param_cb(reset_netlink, &tcmu_reset_netlink_op, NULL, S_IWUSR); 352bdaeedc1SMike Christie MODULE_PARM_DESC(reset_netlink, "Reset netlink commands."); 353bdaeedc1SMike Christie 3547c9e7a6fSAndy Grover /* multicast group */ 3557c9e7a6fSAndy Grover enum tcmu_multicast_groups { 3567c9e7a6fSAndy Grover TCMU_MCGRP_CONFIG, 3577c9e7a6fSAndy Grover }; 3587c9e7a6fSAndy Grover 3597c9e7a6fSAndy Grover static const struct genl_multicast_group tcmu_mcgrps[] = { 3607c9e7a6fSAndy Grover [TCMU_MCGRP_CONFIG] = { .name = "config", }, 3617c9e7a6fSAndy Grover }; 3627c9e7a6fSAndy Grover 363b3af66e2SMike Christie static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = { 364b3af66e2SMike Christie [TCMU_ATTR_DEVICE] = { .type = NLA_STRING }, 365b3af66e2SMike Christie [TCMU_ATTR_MINOR] = { .type = NLA_U32 }, 366b3af66e2SMike Christie [TCMU_ATTR_CMD_STATUS] = { .type = NLA_S32 }, 367b3af66e2SMike Christie [TCMU_ATTR_DEVICE_ID] = { .type = NLA_U32 }, 368b3af66e2SMike Christie [TCMU_ATTR_SUPP_KERN_CMD_REPLY] = { .type = NLA_U8 }, 369b3af66e2SMike Christie }; 370b3af66e2SMike Christie 371b3af66e2SMike Christie static int tcmu_genl_cmd_done(struct genl_info *info, int completed_cmd) 372b3af66e2SMike Christie { 3733228691fSMike Christie struct tcmu_dev *udev = NULL; 374b3af66e2SMike Christie struct tcmu_nl_cmd *nl_cmd; 375b3af66e2SMike Christie int dev_id, rc, ret = 0; 376b3af66e2SMike Christie 377b3af66e2SMike Christie if (!info->attrs[TCMU_ATTR_CMD_STATUS] || 378b3af66e2SMike Christie !info->attrs[TCMU_ATTR_DEVICE_ID]) { 379b3af66e2SMike Christie printk(KERN_ERR "TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n"); 380b3af66e2SMike Christie return -EINVAL; 381b3af66e2SMike Christie } 382b3af66e2SMike Christie 383b3af66e2SMike Christie dev_id = nla_get_u32(info->attrs[TCMU_ATTR_DEVICE_ID]); 384b3af66e2SMike Christie rc = nla_get_s32(info->attrs[TCMU_ATTR_CMD_STATUS]); 385b3af66e2SMike Christie 3863228691fSMike Christie mutex_lock(&tcmu_nl_cmd_mutex); 3873228691fSMike Christie list_for_each_entry(nl_cmd, &tcmu_nl_cmd_list, nl_list) { 3883228691fSMike Christie if (nl_cmd->udev->se_dev.dev_index == dev_id) { 3893228691fSMike Christie udev = nl_cmd->udev; 3903228691fSMike Christie break; 391b3af66e2SMike Christie } 3923228691fSMike Christie } 393b3af66e2SMike Christie 3943228691fSMike Christie if (!udev) { 3950c218e16SMike Christie pr_err("tcmu nl cmd %u/%d completion could not find device with dev id %u.\n", 3963228691fSMike Christie completed_cmd, rc, dev_id); 3973228691fSMike Christie ret = -ENODEV; 3983228691fSMike Christie goto unlock; 3993228691fSMike Christie } 4003228691fSMike Christie list_del(&nl_cmd->nl_list); 401b3af66e2SMike Christie 402bdaeedc1SMike Christie pr_debug("%s genl cmd done got id %d curr %d done %d rc %d stat %d\n", 403bdaeedc1SMike Christie udev->name, dev_id, nl_cmd->cmd, completed_cmd, rc, 404bdaeedc1SMike Christie nl_cmd->status); 405b3af66e2SMike Christie 406b3af66e2SMike Christie if (nl_cmd->cmd != completed_cmd) { 4073228691fSMike Christie pr_err("Mismatched commands on %s (Expecting reply for %d. Current %d).\n", 4083228691fSMike Christie udev->name, completed_cmd, nl_cmd->cmd); 409b3af66e2SMike Christie ret = -EINVAL; 4103228691fSMike Christie goto unlock; 411b3af66e2SMike Christie } 412b3af66e2SMike Christie 4133228691fSMike Christie nl_cmd->status = rc; 414b3af66e2SMike Christie complete(&nl_cmd->complete); 4153228691fSMike Christie unlock: 4163228691fSMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 417b3af66e2SMike Christie return ret; 418b3af66e2SMike Christie } 419b3af66e2SMike Christie 420b3af66e2SMike Christie static int tcmu_genl_rm_dev_done(struct sk_buff *skb, struct genl_info *info) 421b3af66e2SMike Christie { 422b3af66e2SMike Christie return tcmu_genl_cmd_done(info, TCMU_CMD_REMOVED_DEVICE); 423b3af66e2SMike Christie } 424b3af66e2SMike Christie 425b3af66e2SMike Christie static int tcmu_genl_add_dev_done(struct sk_buff *skb, struct genl_info *info) 426b3af66e2SMike Christie { 427b3af66e2SMike Christie return tcmu_genl_cmd_done(info, TCMU_CMD_ADDED_DEVICE); 428b3af66e2SMike Christie } 429b3af66e2SMike Christie 430b3af66e2SMike Christie static int tcmu_genl_reconfig_dev_done(struct sk_buff *skb, 431b3af66e2SMike Christie struct genl_info *info) 432b3af66e2SMike Christie { 433b3af66e2SMike Christie return tcmu_genl_cmd_done(info, TCMU_CMD_RECONFIG_DEVICE); 434b3af66e2SMike Christie } 435b3af66e2SMike Christie 436b3af66e2SMike Christie static int tcmu_genl_set_features(struct sk_buff *skb, struct genl_info *info) 437b3af66e2SMike Christie { 438b3af66e2SMike Christie if (info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]) { 439b3af66e2SMike Christie tcmu_kern_cmd_reply_supported = 440b3af66e2SMike Christie nla_get_u8(info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]); 441b3af66e2SMike Christie printk(KERN_INFO "tcmu daemon: command reply support %u.\n", 442b3af66e2SMike Christie tcmu_kern_cmd_reply_supported); 443b3af66e2SMike Christie } 444b3af66e2SMike Christie 445b3af66e2SMike Christie return 0; 446b3af66e2SMike Christie } 447b3af66e2SMike Christie 44866a9b928SJakub Kicinski static const struct genl_small_ops tcmu_genl_ops[] = { 449b3af66e2SMike Christie { 450b3af66e2SMike Christie .cmd = TCMU_CMD_SET_FEATURES, 451ef6243acSJohannes Berg .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 452b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 453b3af66e2SMike Christie .doit = tcmu_genl_set_features, 454b3af66e2SMike Christie }, 455b3af66e2SMike Christie { 456b3af66e2SMike Christie .cmd = TCMU_CMD_ADDED_DEVICE_DONE, 457ef6243acSJohannes Berg .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 458b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 459b3af66e2SMike Christie .doit = tcmu_genl_add_dev_done, 460b3af66e2SMike Christie }, 461b3af66e2SMike Christie { 462b3af66e2SMike Christie .cmd = TCMU_CMD_REMOVED_DEVICE_DONE, 463ef6243acSJohannes Berg .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 464b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 465b3af66e2SMike Christie .doit = tcmu_genl_rm_dev_done, 466b3af66e2SMike Christie }, 467b3af66e2SMike Christie { 468b3af66e2SMike Christie .cmd = TCMU_CMD_RECONFIG_DEVICE_DONE, 469ef6243acSJohannes Berg .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 470b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 471b3af66e2SMike Christie .doit = tcmu_genl_reconfig_dev_done, 472b3af66e2SMike Christie }, 473b3af66e2SMike Christie }; 474b3af66e2SMike Christie 4757c9e7a6fSAndy Grover /* Our generic netlink family */ 47656989f6dSJohannes Berg static struct genl_family tcmu_genl_family __ro_after_init = { 477489111e5SJohannes Berg .module = THIS_MODULE, 4787c9e7a6fSAndy Grover .hdrsize = 0, 4797c9e7a6fSAndy Grover .name = "TCM-USER", 480b3af66e2SMike Christie .version = 2, 4817c9e7a6fSAndy Grover .maxattr = TCMU_ATTR_MAX, 4823b0f31f2SJohannes Berg .policy = tcmu_attr_policy, 4837c9e7a6fSAndy Grover .mcgrps = tcmu_mcgrps, 4847c9e7a6fSAndy Grover .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps), 48520c08b36SSheng Yang .netnsok = true, 48666a9b928SJakub Kicinski .small_ops = tcmu_genl_ops, 48766a9b928SJakub Kicinski .n_small_ops = ARRAY_SIZE(tcmu_genl_ops), 4887c9e7a6fSAndy Grover }; 4897c9e7a6fSAndy Grover 490141685a3SXiubo Li #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index)) 491141685a3SXiubo Li #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0) 492141685a3SXiubo Li #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index)) 493141685a3SXiubo Li #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++]) 494141685a3SXiubo Li 495b6df4b79SXiubo Li static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len) 496141685a3SXiubo Li { 497141685a3SXiubo Li struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 498141685a3SXiubo Li uint32_t i; 499141685a3SXiubo Li 500b6df4b79SXiubo Li for (i = 0; i < len; i++) 501141685a3SXiubo Li clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap); 502141685a3SXiubo Li } 503141685a3SXiubo Li 5047e98905eSBodo Stroesser static inline int tcmu_get_empty_block(struct tcmu_dev *udev, 5057e98905eSBodo Stroesser struct tcmu_cmd *tcmu_cmd, 506f5ce815fSBodo Stroesser int prev_dbi, int length, int *iov_cnt) 507141685a3SXiubo Li { 508f5ce815fSBodo Stroesser XA_STATE(xas, &udev->data_pages, 0); 509b6df4b79SXiubo Li struct page *page; 510e719afdcSBodo Stroesser int i, cnt, dbi, dpi; 511f5ce815fSBodo Stroesser int page_cnt = DIV_ROUND_UP(length, PAGE_SIZE); 512141685a3SXiubo Li 513b6df4b79SXiubo Li dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh); 514b6df4b79SXiubo Li if (dbi == udev->dbi_thresh) 5157e98905eSBodo Stroesser return -1; 516b6df4b79SXiubo Li 517e719afdcSBodo Stroesser dpi = dbi * udev->data_pages_per_blk; 518f5ce815fSBodo Stroesser /* Count the number of already allocated pages */ 519e719afdcSBodo Stroesser xas_set(&xas, dpi); 520b4150b68SBodo Stroesser rcu_read_lock(); 521f5ce815fSBodo Stroesser for (cnt = 0; xas_next(&xas) && cnt < page_cnt;) 522f5ce815fSBodo Stroesser cnt++; 523b4150b68SBodo Stroesser rcu_read_unlock(); 524b6df4b79SXiubo Li 525f5ce815fSBodo Stroesser for (i = cnt; i < page_cnt; i++) { 5261d2ac7b6SBodo Stroesser /* try to get new zeroed page from the mm */ 5271d2ac7b6SBodo Stroesser page = alloc_page(GFP_NOIO | __GFP_ZERO); 528b6df4b79SXiubo Li if (!page) 529f5ce815fSBodo Stroesser break; 530b6df4b79SXiubo Li 531e719afdcSBodo Stroesser if (xa_store(&udev->data_pages, dpi + i, page, GFP_NOIO)) { 532f5ce815fSBodo Stroesser __free_page(page); 533f5ce815fSBodo Stroesser break; 534b6df4b79SXiubo Li } 535f5ce815fSBodo Stroesser } 536f5ce815fSBodo Stroesser if (atomic_add_return(i - cnt, &global_page_count) > 537f5ce815fSBodo Stroesser tcmu_global_max_pages) 538f5ce815fSBodo Stroesser schedule_delayed_work(&tcmu_unmap_work, 0); 539b6df4b79SXiubo Li 540f5ce815fSBodo Stroesser if (i && dbi > udev->dbi_max) 541141685a3SXiubo Li udev->dbi_max = dbi; 542141685a3SXiubo Li 543141685a3SXiubo Li set_bit(dbi, udev->data_bitmap); 544b6df4b79SXiubo Li tcmu_cmd_set_dbi(tcmu_cmd, dbi); 545141685a3SXiubo Li 5467e98905eSBodo Stroesser if (dbi != prev_dbi + 1) 5477e98905eSBodo Stroesser *iov_cnt += 1; 5487e98905eSBodo Stroesser 549f5ce815fSBodo Stroesser return i == page_cnt ? dbi : -1; 550141685a3SXiubo Li } 551141685a3SXiubo Li 5527e98905eSBodo Stroesser static int tcmu_get_empty_blocks(struct tcmu_dev *udev, 553f5ce815fSBodo Stroesser struct tcmu_cmd *tcmu_cmd, int length) 554b6df4b79SXiubo Li { 5557e98905eSBodo Stroesser /* start value of dbi + 1 must not be a valid dbi */ 5567e98905eSBodo Stroesser int dbi = -2; 557e719afdcSBodo Stroesser int blk_data_len, iov_cnt = 0; 558e719afdcSBodo Stroesser uint32_t blk_size = udev->data_blk_size; 559b6df4b79SXiubo Li 560e719afdcSBodo Stroesser for (; length > 0; length -= blk_size) { 561e719afdcSBodo Stroesser blk_data_len = min_t(uint32_t, length, blk_size); 562e719afdcSBodo Stroesser dbi = tcmu_get_empty_block(udev, tcmu_cmd, dbi, blk_data_len, 563e719afdcSBodo Stroesser &iov_cnt); 5647e98905eSBodo Stroesser if (dbi < 0) 5657e98905eSBodo Stroesser return -1; 566141685a3SXiubo Li } 5677e98905eSBodo Stroesser return iov_cnt; 568141685a3SXiubo Li } 569141685a3SXiubo Li 570141685a3SXiubo Li static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd) 571141685a3SXiubo Li { 572141685a3SXiubo Li kfree(tcmu_cmd->dbi); 573141685a3SXiubo Li kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); 574141685a3SXiubo Li } 575141685a3SXiubo Li 57652ef2743SBodo Stroesser static inline void tcmu_cmd_set_block_cnts(struct tcmu_cmd *cmd) 577141685a3SXiubo Li { 57852ef2743SBodo Stroesser int i, len; 57952ef2743SBodo Stroesser struct se_cmd *se_cmd = cmd->se_cmd; 580e719afdcSBodo Stroesser uint32_t blk_size = cmd->tcmu_dev->data_blk_size; 58152ef2743SBodo Stroesser 582e719afdcSBodo Stroesser cmd->dbi_cnt = DIV_ROUND_UP(se_cmd->data_length, blk_size); 583141685a3SXiubo Li 584141685a3SXiubo Li if (se_cmd->se_cmd_flags & SCF_BIDI) { 585141685a3SXiubo Li BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents)); 58652ef2743SBodo Stroesser for (i = 0, len = 0; i < se_cmd->t_bidi_data_nents; i++) 58752ef2743SBodo Stroesser len += se_cmd->t_bidi_data_sg[i].length; 588e719afdcSBodo Stroesser cmd->dbi_bidi_cnt = DIV_ROUND_UP(len, blk_size); 58952ef2743SBodo Stroesser cmd->dbi_cnt += cmd->dbi_bidi_cnt; 5903c9a7c58SBodo Stroesser cmd->data_len_bidi = len; 591141685a3SXiubo Li } 592141685a3SXiubo Li } 593141685a3SXiubo Li 5943c9a7c58SBodo Stroesser static int new_block_to_iov(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 595c8ed1ff8SBodo Stroesser struct iovec **iov, int prev_dbi, int len) 5963c9a7c58SBodo Stroesser { 5973c9a7c58SBodo Stroesser /* Get the next dbi */ 5983c9a7c58SBodo Stroesser int dbi = tcmu_cmd_get_dbi(cmd); 5993c9a7c58SBodo Stroesser 600e719afdcSBodo Stroesser /* Do not add more than udev->data_blk_size to iov */ 601e719afdcSBodo Stroesser len = min_t(int, len, udev->data_blk_size); 602c8ed1ff8SBodo Stroesser 6033c9a7c58SBodo Stroesser /* 6043c9a7c58SBodo Stroesser * The following code will gather and map the blocks to the same iovec 6053c9a7c58SBodo Stroesser * when the blocks are all next to each other. 6063c9a7c58SBodo Stroesser */ 6073c9a7c58SBodo Stroesser if (dbi != prev_dbi + 1) { 6083c9a7c58SBodo Stroesser /* dbi is not next to previous dbi, so start new iov */ 6093c9a7c58SBodo Stroesser if (prev_dbi >= 0) 6103c9a7c58SBodo Stroesser (*iov)++; 6113c9a7c58SBodo Stroesser /* write offset relative to mb_addr */ 6123c9a7c58SBodo Stroesser (*iov)->iov_base = (void __user *) 613e719afdcSBodo Stroesser (udev->data_off + dbi * udev->data_blk_size); 6143c9a7c58SBodo Stroesser } 6153c9a7c58SBodo Stroesser (*iov)->iov_len += len; 6163c9a7c58SBodo Stroesser 6173c9a7c58SBodo Stroesser return dbi; 6183c9a7c58SBodo Stroesser } 6193c9a7c58SBodo Stroesser 6203c9a7c58SBodo Stroesser static void tcmu_setup_iovs(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 6213c9a7c58SBodo Stroesser struct iovec **iov, int data_length) 6223c9a7c58SBodo Stroesser { 6233c9a7c58SBodo Stroesser /* start value of dbi + 1 must not be a valid dbi */ 6243c9a7c58SBodo Stroesser int dbi = -2; 6253c9a7c58SBodo Stroesser 6263c9a7c58SBodo Stroesser /* We prepare the IOVs for DMA_FROM_DEVICE transfer direction */ 627e719afdcSBodo Stroesser for (; data_length > 0; data_length -= udev->data_blk_size) 628c8ed1ff8SBodo Stroesser dbi = new_block_to_iov(udev, cmd, iov, dbi, data_length); 6293c9a7c58SBodo Stroesser } 6303c9a7c58SBodo Stroesser 6317c9e7a6fSAndy Grover static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd) 6327c9e7a6fSAndy Grover { 6337c9e7a6fSAndy Grover struct se_device *se_dev = se_cmd->se_dev; 6347c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(se_dev); 6357c9e7a6fSAndy Grover struct tcmu_cmd *tcmu_cmd; 6367c9e7a6fSAndy Grover 6370eccce86SDamien Le Moal tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_NOIO); 6387c9e7a6fSAndy Grover if (!tcmu_cmd) 6397c9e7a6fSAndy Grover return NULL; 6407c9e7a6fSAndy Grover 641a94a2572SXiubo Li INIT_LIST_HEAD(&tcmu_cmd->queue_entry); 6427c9e7a6fSAndy Grover tcmu_cmd->se_cmd = se_cmd; 6437c9e7a6fSAndy Grover tcmu_cmd->tcmu_dev = udev; 6447c9e7a6fSAndy Grover 64552ef2743SBodo Stroesser tcmu_cmd_set_block_cnts(tcmu_cmd); 646141685a3SXiubo Li tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t), 6470eccce86SDamien Le Moal GFP_NOIO); 648141685a3SXiubo Li if (!tcmu_cmd->dbi) { 649141685a3SXiubo Li kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); 650141685a3SXiubo Li return NULL; 651141685a3SXiubo Li } 652141685a3SXiubo Li 6537c9e7a6fSAndy Grover return tcmu_cmd; 6547c9e7a6fSAndy Grover } 6557c9e7a6fSAndy Grover 6567c9e7a6fSAndy Grover static inline void tcmu_flush_dcache_range(void *vaddr, size_t size) 6577c9e7a6fSAndy Grover { 658b75d8063SGeliang Tang unsigned long offset = offset_in_page(vaddr); 65926d2b310Stangwenji void *start = vaddr - offset; 6607c9e7a6fSAndy Grover 6617c9e7a6fSAndy Grover size = round_up(size+offset, PAGE_SIZE); 6627c9e7a6fSAndy Grover 6637c9e7a6fSAndy Grover while (size) { 6643145550aSBodo Stroesser flush_dcache_page(vmalloc_to_page(start)); 66526d2b310Stangwenji start += PAGE_SIZE; 6667c9e7a6fSAndy Grover size -= PAGE_SIZE; 6677c9e7a6fSAndy Grover } 6687c9e7a6fSAndy Grover } 6697c9e7a6fSAndy Grover 6707c9e7a6fSAndy Grover /* 6717c9e7a6fSAndy Grover * Some ring helper functions. We don't assume size is a power of 2 so 6727c9e7a6fSAndy Grover * we can't use circ_buf.h. 6737c9e7a6fSAndy Grover */ 6747c9e7a6fSAndy Grover static inline size_t spc_used(size_t head, size_t tail, size_t size) 6757c9e7a6fSAndy Grover { 6767c9e7a6fSAndy Grover int diff = head - tail; 6777c9e7a6fSAndy Grover 6787c9e7a6fSAndy Grover if (diff >= 0) 6797c9e7a6fSAndy Grover return diff; 6807c9e7a6fSAndy Grover else 6817c9e7a6fSAndy Grover return size + diff; 6827c9e7a6fSAndy Grover } 6837c9e7a6fSAndy Grover 6847c9e7a6fSAndy Grover static inline size_t spc_free(size_t head, size_t tail, size_t size) 6857c9e7a6fSAndy Grover { 6867c9e7a6fSAndy Grover /* Keep 1 byte unused or we can't tell full from empty */ 6877c9e7a6fSAndy Grover return (size - spc_used(head, tail, size) - 1); 6887c9e7a6fSAndy Grover } 6897c9e7a6fSAndy Grover 6907c9e7a6fSAndy Grover static inline size_t head_to_end(size_t head, size_t size) 6917c9e7a6fSAndy Grover { 6927c9e7a6fSAndy Grover return size - head; 6937c9e7a6fSAndy Grover } 6947c9e7a6fSAndy Grover 6957c9e7a6fSAndy Grover #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size) 6967c9e7a6fSAndy Grover 697c8ed1ff8SBodo Stroesser #define TCMU_SG_TO_DATA_AREA 1 698c8ed1ff8SBodo Stroesser #define TCMU_DATA_AREA_TO_SG 2 699c8ed1ff8SBodo Stroesser 700c8ed1ff8SBodo Stroesser static inline void tcmu_copy_data(struct tcmu_dev *udev, 701c8ed1ff8SBodo Stroesser struct tcmu_cmd *tcmu_cmd, uint32_t direction, 702c8ed1ff8SBodo Stroesser struct scatterlist *sg, unsigned int sg_nents, 703c8ed1ff8SBodo Stroesser struct iovec **iov, size_t data_len) 704c8ed1ff8SBodo Stroesser { 705c8ed1ff8SBodo Stroesser /* start value of dbi + 1 must not be a valid dbi */ 706c8ed1ff8SBodo Stroesser int dbi = -2; 707f5ce815fSBodo Stroesser size_t page_remaining, cp_len; 708b4150b68SBodo Stroesser int page_cnt, page_inx, dpi; 709c8ed1ff8SBodo Stroesser struct sg_mapping_iter sg_iter; 710c8ed1ff8SBodo Stroesser unsigned int sg_flags; 711c8ed1ff8SBodo Stroesser struct page *page; 712c8ed1ff8SBodo Stroesser void *data_page_start, *data_addr; 713c8ed1ff8SBodo Stroesser 714c8ed1ff8SBodo Stroesser if (direction == TCMU_SG_TO_DATA_AREA) 715c8ed1ff8SBodo Stroesser sg_flags = SG_MITER_ATOMIC | SG_MITER_FROM_SG; 716c8ed1ff8SBodo Stroesser else 717c8ed1ff8SBodo Stroesser sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG; 718c8ed1ff8SBodo Stroesser sg_miter_start(&sg_iter, sg, sg_nents, sg_flags); 719c8ed1ff8SBodo Stroesser 720c8ed1ff8SBodo Stroesser while (data_len) { 721c8ed1ff8SBodo Stroesser if (direction == TCMU_SG_TO_DATA_AREA) 722c8ed1ff8SBodo Stroesser dbi = new_block_to_iov(udev, tcmu_cmd, iov, dbi, 723c8ed1ff8SBodo Stroesser data_len); 724c8ed1ff8SBodo Stroesser else 725c8ed1ff8SBodo Stroesser dbi = tcmu_cmd_get_dbi(tcmu_cmd); 726f5ce815fSBodo Stroesser 727f5ce815fSBodo Stroesser page_cnt = DIV_ROUND_UP(data_len, PAGE_SIZE); 728e719afdcSBodo Stroesser if (page_cnt > udev->data_pages_per_blk) 729e719afdcSBodo Stroesser page_cnt = udev->data_pages_per_blk; 730f5ce815fSBodo Stroesser 731b4150b68SBodo Stroesser dpi = dbi * udev->data_pages_per_blk; 732b4150b68SBodo Stroesser for (page_inx = 0; page_inx < page_cnt && data_len; 733b4150b68SBodo Stroesser page_inx++, dpi++) { 734b4150b68SBodo Stroesser page = xa_load(&udev->data_pages, dpi); 735f5ce815fSBodo Stroesser 736c8ed1ff8SBodo Stroesser if (direction == TCMU_DATA_AREA_TO_SG) 737c8ed1ff8SBodo Stroesser flush_dcache_page(page); 738c8ed1ff8SBodo Stroesser data_page_start = kmap_atomic(page); 739f5ce815fSBodo Stroesser page_remaining = PAGE_SIZE; 740c8ed1ff8SBodo Stroesser 741f5ce815fSBodo Stroesser while (page_remaining && data_len) { 742c8ed1ff8SBodo Stroesser if (!sg_miter_next(&sg_iter)) { 743c8ed1ff8SBodo Stroesser /* set length to 0 to abort outer loop */ 744c8ed1ff8SBodo Stroesser data_len = 0; 745f5ce815fSBodo Stroesser pr_debug("%s: aborting data copy due to exhausted sg_list\n", 746f5ce815fSBodo Stroesser __func__); 747c8ed1ff8SBodo Stroesser break; 748c8ed1ff8SBodo Stroesser } 749f5ce815fSBodo Stroesser cp_len = min3(sg_iter.length, page_remaining, 750f5ce815fSBodo Stroesser data_len); 751c8ed1ff8SBodo Stroesser 752c8ed1ff8SBodo Stroesser data_addr = data_page_start + 753f5ce815fSBodo Stroesser PAGE_SIZE - page_remaining; 754c8ed1ff8SBodo Stroesser if (direction == TCMU_SG_TO_DATA_AREA) 755c8ed1ff8SBodo Stroesser memcpy(data_addr, sg_iter.addr, cp_len); 756c8ed1ff8SBodo Stroesser else 757c8ed1ff8SBodo Stroesser memcpy(sg_iter.addr, data_addr, cp_len); 758c8ed1ff8SBodo Stroesser 759c8ed1ff8SBodo Stroesser data_len -= cp_len; 760f5ce815fSBodo Stroesser page_remaining -= cp_len; 761c8ed1ff8SBodo Stroesser sg_iter.consumed = cp_len; 762c8ed1ff8SBodo Stroesser } 763c8ed1ff8SBodo Stroesser sg_miter_stop(&sg_iter); 764c8ed1ff8SBodo Stroesser 765c8ed1ff8SBodo Stroesser kunmap_atomic(data_page_start); 766c8ed1ff8SBodo Stroesser if (direction == TCMU_SG_TO_DATA_AREA) 767c8ed1ff8SBodo Stroesser flush_dcache_page(page); 768c8ed1ff8SBodo Stroesser } 769c8ed1ff8SBodo Stroesser } 770f5ce815fSBodo Stroesser } 771c8ed1ff8SBodo Stroesser 7723c9a7c58SBodo Stroesser static void scatter_data_area(struct tcmu_dev *udev, struct tcmu_cmd *tcmu_cmd, 7733c9a7c58SBodo Stroesser struct iovec **iov) 77426418649SSheng Yang { 7753c9a7c58SBodo Stroesser struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 776f97ec7dbSIlias Tsitsimpis 777c8ed1ff8SBodo Stroesser tcmu_copy_data(udev, tcmu_cmd, TCMU_SG_TO_DATA_AREA, se_cmd->t_data_sg, 778c8ed1ff8SBodo Stroesser se_cmd->t_data_nents, iov, se_cmd->data_length); 7793c58f737SBodo Stroesser } 780b6df4b79SXiubo Li 781c8ed1ff8SBodo Stroesser static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *tcmu_cmd, 7826c3796d1Sbstroesser@ts.fujitsu.com bool bidi, uint32_t read_len) 783f97ec7dbSIlias Tsitsimpis { 784c8ed1ff8SBodo Stroesser struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 785c8ed1ff8SBodo Stroesser struct scatterlist *data_sg; 786a5d68ba8SXiubo Li unsigned int data_nents; 787a5d68ba8SXiubo Li 788a5d68ba8SXiubo Li if (!bidi) { 789a5d68ba8SXiubo Li data_sg = se_cmd->t_data_sg; 790a5d68ba8SXiubo Li data_nents = se_cmd->t_data_nents; 791a5d68ba8SXiubo Li } else { 792a5d68ba8SXiubo Li /* 793a5d68ba8SXiubo Li * For bidi case, the first count blocks are for Data-Out 794a5d68ba8SXiubo Li * buffer blocks, and before gathering the Data-In buffer 79552ef2743SBodo Stroesser * the Data-Out buffer blocks should be skipped. 796a5d68ba8SXiubo Li */ 797c8ed1ff8SBodo Stroesser tcmu_cmd_set_dbi_cur(tcmu_cmd, 798c8ed1ff8SBodo Stroesser tcmu_cmd->dbi_cnt - tcmu_cmd->dbi_bidi_cnt); 799a5d68ba8SXiubo Li 800a5d68ba8SXiubo Li data_sg = se_cmd->t_bidi_data_sg; 801a5d68ba8SXiubo Li data_nents = se_cmd->t_bidi_data_nents; 802a5d68ba8SXiubo Li } 803f97ec7dbSIlias Tsitsimpis 804c8ed1ff8SBodo Stroesser tcmu_copy_data(udev, tcmu_cmd, TCMU_DATA_AREA_TO_SG, data_sg, 805c8ed1ff8SBodo Stroesser data_nents, NULL, read_len); 806f97ec7dbSIlias Tsitsimpis } 807f97ec7dbSIlias Tsitsimpis 808b6df4b79SXiubo Li static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh) 80926418649SSheng Yang { 8103c0f26ffSMike Christie return thresh - bitmap_weight(bitmap, thresh); 81126418649SSheng Yang } 81226418649SSheng Yang 8137c9e7a6fSAndy Grover /* 8147e98905eSBodo Stroesser * We can't queue a command until we have space available on the cmd ring. 8157c9e7a6fSAndy Grover * 8167c9e7a6fSAndy Grover * Called with ring lock held. 8177c9e7a6fSAndy Grover */ 8187e98905eSBodo Stroesser static bool is_ring_space_avail(struct tcmu_dev *udev, size_t cmd_size) 8197c9e7a6fSAndy Grover { 8207c9e7a6fSAndy Grover struct tcmu_mailbox *mb = udev->mb_addr; 8210241fd39SNicholas Bellinger size_t space, cmd_needed; 8227c9e7a6fSAndy Grover u32 cmd_head; 8237c9e7a6fSAndy Grover 8247c9e7a6fSAndy Grover tcmu_flush_dcache_range(mb, sizeof(*mb)); 8257c9e7a6fSAndy Grover 8267c9e7a6fSAndy Grover cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 8277c9e7a6fSAndy Grover 828f56574a2SAndy Grover /* 829f56574a2SAndy Grover * If cmd end-of-ring space is too small then we need space for a NOP plus 830f56574a2SAndy Grover * original cmd - cmds are internally contiguous. 831f56574a2SAndy Grover */ 832f56574a2SAndy Grover if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size) 833f56574a2SAndy Grover cmd_needed = cmd_size; 834f56574a2SAndy Grover else 835f56574a2SAndy Grover cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size); 836f56574a2SAndy Grover 8377c9e7a6fSAndy Grover space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size); 8387c9e7a6fSAndy Grover if (space < cmd_needed) { 8397c9e7a6fSAndy Grover pr_debug("no cmd space: %u %u %u\n", cmd_head, 8407c9e7a6fSAndy Grover udev->cmdr_last_cleaned, udev->cmdr_size); 8417c9e7a6fSAndy Grover return false; 8427c9e7a6fSAndy Grover } 843bc2d214aSBodo Stroesser return true; 8447e98905eSBodo Stroesser } 8457e98905eSBodo Stroesser 8467e98905eSBodo Stroesser /* 8477e98905eSBodo Stroesser * We have to allocate data buffers before we can queue a command. 8487e98905eSBodo Stroesser * Returns -1 on error (not enough space) or number of needed iovs on success 8497e98905eSBodo Stroesser * 8507e98905eSBodo Stroesser * Called with ring lock held. 8517e98905eSBodo Stroesser */ 8527e98905eSBodo Stroesser static int tcmu_alloc_data_space(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 8537e98905eSBodo Stroesser int *iov_bidi_cnt) 8547e98905eSBodo Stroesser { 8557e98905eSBodo Stroesser int space, iov_cnt = 0, ret = 0; 8567e98905eSBodo Stroesser 8577e98905eSBodo Stroesser if (!cmd->dbi_cnt) 8587e98905eSBodo Stroesser goto wr_iov_cnts; 859bc2d214aSBodo Stroesser 860b6df4b79SXiubo Li /* try to check and get the data blocks as needed */ 861b6df4b79SXiubo Li space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh); 86252ef2743SBodo Stroesser if (space < cmd->dbi_cnt) { 86380eb8761SMike Christie unsigned long blocks_left = 86480eb8761SMike Christie (udev->max_blocks - udev->dbi_thresh) + space; 865b6df4b79SXiubo Li 86652ef2743SBodo Stroesser if (blocks_left < cmd->dbi_cnt) { 867e719afdcSBodo Stroesser pr_debug("no data space: only %lu available, but ask for %u\n", 868e719afdcSBodo Stroesser blocks_left * udev->data_blk_size, 869e719afdcSBodo Stroesser cmd->dbi_cnt * udev->data_blk_size); 8707e98905eSBodo Stroesser return -1; 8717c9e7a6fSAndy Grover } 8727c9e7a6fSAndy Grover 87352ef2743SBodo Stroesser udev->dbi_thresh += cmd->dbi_cnt; 87480eb8761SMike Christie if (udev->dbi_thresh > udev->max_blocks) 87580eb8761SMike Christie udev->dbi_thresh = udev->max_blocks; 876b6df4b79SXiubo Li } 877b6df4b79SXiubo Li 878f5ce815fSBodo Stroesser iov_cnt = tcmu_get_empty_blocks(udev, cmd, cmd->se_cmd->data_length); 8797e98905eSBodo Stroesser if (iov_cnt < 0) 8807e98905eSBodo Stroesser return -1; 8817e98905eSBodo Stroesser 8827e98905eSBodo Stroesser if (cmd->dbi_bidi_cnt) { 883f5ce815fSBodo Stroesser ret = tcmu_get_empty_blocks(udev, cmd, cmd->data_len_bidi); 8847e98905eSBodo Stroesser if (ret < 0) 8857e98905eSBodo Stroesser return -1; 8867e98905eSBodo Stroesser } 8877e98905eSBodo Stroesser wr_iov_cnts: 8887e98905eSBodo Stroesser *iov_bidi_cnt = ret; 8897e98905eSBodo Stroesser return iov_cnt + ret; 8907c9e7a6fSAndy Grover } 8917c9e7a6fSAndy Grover 892fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt) 893fe25cc34SXiubo Li { 894fe25cc34SXiubo Li return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]), 895fe25cc34SXiubo Li sizeof(struct tcmu_cmd_entry)); 896fe25cc34SXiubo Li } 897fe25cc34SXiubo Li 898fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd, 899fe25cc34SXiubo Li size_t base_command_size) 900fe25cc34SXiubo Li { 901fe25cc34SXiubo Li struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 902fe25cc34SXiubo Li size_t command_size; 903fe25cc34SXiubo Li 904fe25cc34SXiubo Li command_size = base_command_size + 905fe25cc34SXiubo Li round_up(scsi_command_size(se_cmd->t_task_cdb), 906fe25cc34SXiubo Li TCMU_OP_ALIGN_SIZE); 907fe25cc34SXiubo Li 908fe25cc34SXiubo Li WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1)); 909fe25cc34SXiubo Li 910fe25cc34SXiubo Li return command_size; 911fe25cc34SXiubo Li } 912fe25cc34SXiubo Li 91361fb2482SBodo Stroesser static void tcmu_setup_cmd_timer(struct tcmu_cmd *tcmu_cmd, unsigned int tmo, 9149103575aSMike Christie struct timer_list *timer) 9150d44374cSMike Christie { 9169103575aSMike Christie if (!tmo) 91761fb2482SBodo Stroesser return; 9189103575aSMike Christie 9190d44374cSMike Christie tcmu_cmd->deadline = round_jiffies_up(jiffies + msecs_to_jiffies(tmo)); 920a94a2572SXiubo Li if (!timer_pending(timer)) 9219103575aSMike Christie mod_timer(timer, tcmu_cmd->deadline); 922a94a2572SXiubo Li 92361fb2482SBodo Stroesser pr_debug("Timeout set up for cmd %p, dev = %s, tmo = %lu\n", tcmu_cmd, 92461fb2482SBodo Stroesser tcmu_cmd->tcmu_dev->name, tmo / MSEC_PER_SEC); 9250d44374cSMike Christie } 9260d44374cSMike Christie 927a94a2572SXiubo Li static int add_to_qfull_queue(struct tcmu_cmd *tcmu_cmd) 928af1dd7ffSMike Christie { 929af1dd7ffSMike Christie struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 9309103575aSMike Christie unsigned int tmo; 931af1dd7ffSMike Christie 9329103575aSMike Christie /* 9339103575aSMike Christie * For backwards compat if qfull_time_out is not set use 9349103575aSMike Christie * cmd_time_out and if that's not set use the default time out. 9359103575aSMike Christie */ 9369103575aSMike Christie if (!udev->qfull_time_out) 9379103575aSMike Christie return -ETIMEDOUT; 9389103575aSMike Christie else if (udev->qfull_time_out > 0) 9399103575aSMike Christie tmo = udev->qfull_time_out; 9409103575aSMike Christie else if (udev->cmd_time_out) 9419103575aSMike Christie tmo = udev->cmd_time_out; 9429103575aSMike Christie else 9439103575aSMike Christie tmo = TCMU_TIME_OUT; 9449103575aSMike Christie 94561fb2482SBodo Stroesser tcmu_setup_cmd_timer(tcmu_cmd, tmo, &udev->qfull_timer); 946af1dd7ffSMike Christie 947a94a2572SXiubo Li list_add_tail(&tcmu_cmd->queue_entry, &udev->qfull_queue); 94861fb2482SBodo Stroesser pr_debug("adding cmd %p on dev %s to ring space wait queue\n", 94961fb2482SBodo Stroesser tcmu_cmd, udev->name); 950af1dd7ffSMike Christie return 0; 951af1dd7ffSMike Christie } 952af1dd7ffSMike Christie 9533d3f9d56SBodo Stroesser static uint32_t ring_insert_padding(struct tcmu_dev *udev, size_t cmd_size) 9543d3f9d56SBodo Stroesser { 9553d3f9d56SBodo Stroesser struct tcmu_cmd_entry_hdr *hdr; 9563d3f9d56SBodo Stroesser struct tcmu_mailbox *mb = udev->mb_addr; 9573d3f9d56SBodo Stroesser uint32_t cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 9583d3f9d56SBodo Stroesser 9593d3f9d56SBodo Stroesser /* Insert a PAD if end-of-ring space is too small */ 9603d3f9d56SBodo Stroesser if (head_to_end(cmd_head, udev->cmdr_size) < cmd_size) { 9613d3f9d56SBodo Stroesser size_t pad_size = head_to_end(cmd_head, udev->cmdr_size); 9623d3f9d56SBodo Stroesser 963ecddbb7eSBodo Stroesser hdr = udev->cmdr + cmd_head; 9643d3f9d56SBodo Stroesser tcmu_hdr_set_op(&hdr->len_op, TCMU_OP_PAD); 9653d3f9d56SBodo Stroesser tcmu_hdr_set_len(&hdr->len_op, pad_size); 9663d3f9d56SBodo Stroesser hdr->cmd_id = 0; /* not used for PAD */ 9673d3f9d56SBodo Stroesser hdr->kflags = 0; 9683d3f9d56SBodo Stroesser hdr->uflags = 0; 9693d3f9d56SBodo Stroesser tcmu_flush_dcache_range(hdr, sizeof(*hdr)); 9703d3f9d56SBodo Stroesser 9713d3f9d56SBodo Stroesser UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size); 9723d3f9d56SBodo Stroesser tcmu_flush_dcache_range(mb, sizeof(*mb)); 9733d3f9d56SBodo Stroesser 9743d3f9d56SBodo Stroesser cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 9753d3f9d56SBodo Stroesser WARN_ON(cmd_head != 0); 9763d3f9d56SBodo Stroesser } 9773d3f9d56SBodo Stroesser 9783d3f9d56SBodo Stroesser return cmd_head; 9793d3f9d56SBodo Stroesser } 9803d3f9d56SBodo Stroesser 9816888da81SMike Christie static void tcmu_unplug_device(struct se_dev_plug *se_plug) 9826888da81SMike Christie { 9836888da81SMike Christie struct se_device *se_dev = se_plug->se_dev; 9846888da81SMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 9856888da81SMike Christie 9863ac0fcb4SBodo Stroesser clear_bit(TCMU_DEV_BIT_PLUGGED, &udev->flags); 9876888da81SMike Christie uio_event_notify(&udev->uio_info); 9886888da81SMike Christie } 9896888da81SMike Christie 9906888da81SMike Christie static struct se_dev_plug *tcmu_plug_device(struct se_device *se_dev) 9916888da81SMike Christie { 9926888da81SMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 9936888da81SMike Christie 9943ac0fcb4SBodo Stroesser if (!test_and_set_bit(TCMU_DEV_BIT_PLUGGED, &udev->flags)) 9956888da81SMike Christie return &udev->se_plug; 9966888da81SMike Christie 9976888da81SMike Christie return NULL; 9986888da81SMike Christie } 9996888da81SMike Christie 10006fd0ce79SMike Christie /** 10016fd0ce79SMike Christie * queue_cmd_ring - queue cmd to ring or internally 10026fd0ce79SMike Christie * @tcmu_cmd: cmd to queue 10036fd0ce79SMike Christie * @scsi_err: TCM error code if failure (-1) returned. 10046fd0ce79SMike Christie * 10056fd0ce79SMike Christie * Returns: 10066fd0ce79SMike Christie * -1 we cannot queue internally or to the ring. 10076fd0ce79SMike Christie * 0 success 1008af1dd7ffSMike Christie * 1 internally queued to wait for ring memory to free. 10096fd0ce79SMike Christie */ 1010e7f41104SBart Van Assche static int queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, sense_reason_t *scsi_err) 10117c9e7a6fSAndy Grover { 10127c9e7a6fSAndy Grover struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 10137c9e7a6fSAndy Grover struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 10147c9e7a6fSAndy Grover size_t base_command_size, command_size; 10153d3f9d56SBodo Stroesser struct tcmu_mailbox *mb = udev->mb_addr; 10167c9e7a6fSAndy Grover struct tcmu_cmd_entry *entry; 10177c9e7a6fSAndy Grover struct iovec *iov; 1018d3cbb743SBodo Stroesser int iov_cnt, iov_bidi_cnt; 1019d3cbb743SBodo Stroesser uint32_t cmd_id, cmd_head; 10207c9e7a6fSAndy Grover uint64_t cdb_off; 1021e719afdcSBodo Stroesser uint32_t blk_size = udev->data_blk_size; 102252ef2743SBodo Stroesser /* size of data buffer needed */ 1023e719afdcSBodo Stroesser size_t data_length = (size_t)tcmu_cmd->dbi_cnt * blk_size; 10247c9e7a6fSAndy Grover 10256fd0ce79SMike Christie *scsi_err = TCM_NO_SENSE; 10266fd0ce79SMike Christie 1027892782caSMike Christie if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) { 1028892782caSMike Christie *scsi_err = TCM_LUN_BUSY; 1029892782caSMike Christie return -1; 1030892782caSMike Christie } 1031892782caSMike Christie 10326fd0ce79SMike Christie if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) { 10336fd0ce79SMike Christie *scsi_err = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 10346fd0ce79SMike Christie return -1; 10356fd0ce79SMike Christie } 10367c9e7a6fSAndy Grover 1037a94a2572SXiubo Li if (!list_empty(&udev->qfull_queue)) 1038af1dd7ffSMike Christie goto queue; 10397c9e7a6fSAndy Grover 1040e719afdcSBodo Stroesser if (data_length > (size_t)udev->max_blocks * blk_size) { 10417e98905eSBodo Stroesser pr_warn("TCMU: Request of size %zu is too big for %zu data area\n", 1042e719afdcSBodo Stroesser data_length, (size_t)udev->max_blocks * blk_size); 10436fd0ce79SMike Christie *scsi_err = TCM_INVALID_CDB_FIELD; 10446fd0ce79SMike Christie return -1; 1045554617b2SAndy Grover } 10467c9e7a6fSAndy Grover 10477e98905eSBodo Stroesser iov_cnt = tcmu_alloc_data_space(udev, tcmu_cmd, &iov_bidi_cnt); 10487e98905eSBodo Stroesser if (iov_cnt < 0) 10497e98905eSBodo Stroesser goto free_and_queue; 10507e98905eSBodo Stroesser 10517e98905eSBodo Stroesser /* 10527e98905eSBodo Stroesser * Must be a certain minimum size for response sense info, but 10537e98905eSBodo Stroesser * also may be larger if the iov array is large. 10547e98905eSBodo Stroesser */ 10557e98905eSBodo Stroesser base_command_size = tcmu_cmd_get_base_cmd_size(iov_cnt); 10567e98905eSBodo Stroesser command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size); 10577e98905eSBodo Stroesser 10587e98905eSBodo Stroesser if (command_size > (udev->cmdr_size / 2)) { 10597e98905eSBodo Stroesser pr_warn("TCMU: Request of size %zu is too big for %u cmd ring\n", 10607e98905eSBodo Stroesser command_size, udev->cmdr_size); 10617e98905eSBodo Stroesser tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur); 10627e98905eSBodo Stroesser *scsi_err = TCM_INVALID_CDB_FIELD; 10637e98905eSBodo Stroesser return -1; 10647e98905eSBodo Stroesser } 10657e98905eSBodo Stroesser 10667e98905eSBodo Stroesser if (!is_ring_space_avail(udev, command_size)) 1067810b8153SMike Christie /* 1068810b8153SMike Christie * Don't leave commands partially setup because the unmap 1069810b8153SMike Christie * thread might need the blocks to make forward progress. 1070810b8153SMike Christie */ 10717e98905eSBodo Stroesser goto free_and_queue; 107226418649SSheng Yang 1073d3cbb743SBodo Stroesser if (xa_alloc(&udev->commands, &cmd_id, tcmu_cmd, XA_LIMIT(1, 0xffff), 1074d3cbb743SBodo Stroesser GFP_NOWAIT) < 0) { 107561fb2482SBodo Stroesser pr_err("tcmu: Could not allocate cmd id.\n"); 10766fd0ce79SMike Christie 107761fb2482SBodo Stroesser tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt); 10786fd0ce79SMike Christie *scsi_err = TCM_OUT_OF_RESOURCES; 10796fd0ce79SMike Christie return -1; 10800d44374cSMike Christie } 108161fb2482SBodo Stroesser tcmu_cmd->cmd_id = cmd_id; 108261fb2482SBodo Stroesser 108361fb2482SBodo Stroesser pr_debug("allocated cmd id %u for cmd %p dev %s\n", tcmu_cmd->cmd_id, 108461fb2482SBodo Stroesser tcmu_cmd, udev->name); 108561fb2482SBodo Stroesser 10867e98905eSBodo Stroesser cmd_head = ring_insert_padding(udev, command_size); 10877e98905eSBodo Stroesser 1088ecddbb7eSBodo Stroesser entry = udev->cmdr + cmd_head; 10897e98905eSBodo Stroesser memset(entry, 0, command_size); 10907e98905eSBodo Stroesser tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD); 10917e98905eSBodo Stroesser 10927e98905eSBodo Stroesser /* prepare iov list and copy data to data area if necessary */ 10937e98905eSBodo Stroesser tcmu_cmd_reset_dbi_cur(tcmu_cmd); 10947e98905eSBodo Stroesser iov = &entry->req.iov[0]; 10953c9a7c58SBodo Stroesser 10963c9a7c58SBodo Stroesser if (se_cmd->data_direction == DMA_TO_DEVICE || 10973c9a7c58SBodo Stroesser se_cmd->se_cmd_flags & SCF_BIDI) 10983c9a7c58SBodo Stroesser scatter_data_area(udev, tcmu_cmd, &iov); 10993c9a7c58SBodo Stroesser else 11003c9a7c58SBodo Stroesser tcmu_setup_iovs(udev, tcmu_cmd, &iov, se_cmd->data_length); 11013c9a7c58SBodo Stroesser 11027e98905eSBodo Stroesser entry->req.iov_cnt = iov_cnt - iov_bidi_cnt; 11037e98905eSBodo Stroesser 11047e98905eSBodo Stroesser /* Handle BIDI commands */ 11057e98905eSBodo Stroesser if (se_cmd->se_cmd_flags & SCF_BIDI) { 11067e98905eSBodo Stroesser iov++; 11073c9a7c58SBodo Stroesser tcmu_setup_iovs(udev, tcmu_cmd, &iov, tcmu_cmd->data_len_bidi); 11087e98905eSBodo Stroesser entry->req.iov_bidi_cnt = iov_bidi_cnt; 11097e98905eSBodo Stroesser } 11107e98905eSBodo Stroesser 111161fb2482SBodo Stroesser tcmu_setup_cmd_timer(tcmu_cmd, udev->cmd_time_out, &udev->cmd_timer); 111261fb2482SBodo Stroesser 11130d44374cSMike Christie entry->hdr.cmd_id = tcmu_cmd->cmd_id; 11140d44374cSMike Christie 1115fe25cc34SXiubo Li tcmu_hdr_set_len(&entry->hdr.len_op, command_size); 1116fe25cc34SXiubo Li 11177c9e7a6fSAndy Grover /* All offsets relative to mb_addr, not start of entry! */ 11187c9e7a6fSAndy Grover cdb_off = CMDR_OFF + cmd_head + base_command_size; 11197c9e7a6fSAndy Grover memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb)); 11207c9e7a6fSAndy Grover entry->req.cdb_off = cdb_off; 11218c4e0f21SBodo Stroesser tcmu_flush_dcache_range(entry, command_size); 11227c9e7a6fSAndy Grover 11237c9e7a6fSAndy Grover UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size); 11247c9e7a6fSAndy Grover tcmu_flush_dcache_range(mb, sizeof(*mb)); 11257c9e7a6fSAndy Grover 1126a94a2572SXiubo Li list_add_tail(&tcmu_cmd->queue_entry, &udev->inflight_queue); 1127a94a2572SXiubo Li 11283ac0fcb4SBodo Stroesser if (!test_bit(TCMU_DEV_BIT_PLUGGED, &udev->flags)) 11297c9e7a6fSAndy Grover uio_event_notify(&udev->uio_info); 11307c9e7a6fSAndy Grover 11316fd0ce79SMike Christie return 0; 1132af1dd7ffSMike Christie 11337e98905eSBodo Stroesser free_and_queue: 11347e98905eSBodo Stroesser tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur); 11357e98905eSBodo Stroesser tcmu_cmd_reset_dbi_cur(tcmu_cmd); 11367e98905eSBodo Stroesser 1137af1dd7ffSMike Christie queue: 1138a94a2572SXiubo Li if (add_to_qfull_queue(tcmu_cmd)) { 1139af1dd7ffSMike Christie *scsi_err = TCM_OUT_OF_RESOURCES; 1140af1dd7ffSMike Christie return -1; 1141af1dd7ffSMike Christie } 1142af1dd7ffSMike Christie 1143af1dd7ffSMike Christie return 1; 11447c9e7a6fSAndy Grover } 11457c9e7a6fSAndy Grover 1146bc2d214aSBodo Stroesser /** 1147bc2d214aSBodo Stroesser * queue_tmr_ring - queue tmr info to ring or internally 1148bc2d214aSBodo Stroesser * @udev: related tcmu_dev 1149bc2d214aSBodo Stroesser * @tmr: tcmu_tmr containing tmr info to queue 1150bc2d214aSBodo Stroesser * 1151bc2d214aSBodo Stroesser * Returns: 1152bc2d214aSBodo Stroesser * 0 success 1153bc2d214aSBodo Stroesser * 1 internally queued to wait for ring memory to free. 1154bc2d214aSBodo Stroesser */ 1155bc2d214aSBodo Stroesser static int 1156bc2d214aSBodo Stroesser queue_tmr_ring(struct tcmu_dev *udev, struct tcmu_tmr *tmr) 1157bc2d214aSBodo Stroesser { 1158bc2d214aSBodo Stroesser struct tcmu_tmr_entry *entry; 1159bc2d214aSBodo Stroesser int cmd_size; 1160bc2d214aSBodo Stroesser int id_list_sz; 1161bc2d214aSBodo Stroesser struct tcmu_mailbox *mb = udev->mb_addr; 1162bc2d214aSBodo Stroesser uint32_t cmd_head; 1163bc2d214aSBodo Stroesser 1164bc2d214aSBodo Stroesser if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) 1165bc2d214aSBodo Stroesser goto out_free; 1166bc2d214aSBodo Stroesser 1167bc2d214aSBodo Stroesser id_list_sz = sizeof(tmr->tmr_cmd_ids[0]) * tmr->tmr_cmd_cnt; 1168bc2d214aSBodo Stroesser cmd_size = round_up(sizeof(*entry) + id_list_sz, TCMU_OP_ALIGN_SIZE); 1169bc2d214aSBodo Stroesser 1170bc2d214aSBodo Stroesser if (!list_empty(&udev->tmr_queue) || 11717e98905eSBodo Stroesser !is_ring_space_avail(udev, cmd_size)) { 1172bc2d214aSBodo Stroesser list_add_tail(&tmr->queue_entry, &udev->tmr_queue); 1173bc2d214aSBodo Stroesser pr_debug("adding tmr %p on dev %s to TMR ring space wait queue\n", 1174bc2d214aSBodo Stroesser tmr, udev->name); 1175bc2d214aSBodo Stroesser return 1; 1176bc2d214aSBodo Stroesser } 1177bc2d214aSBodo Stroesser 1178bc2d214aSBodo Stroesser cmd_head = ring_insert_padding(udev, cmd_size); 1179bc2d214aSBodo Stroesser 1180ecddbb7eSBodo Stroesser entry = udev->cmdr + cmd_head; 1181bc2d214aSBodo Stroesser memset(entry, 0, cmd_size); 1182bc2d214aSBodo Stroesser tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_TMR); 1183bc2d214aSBodo Stroesser tcmu_hdr_set_len(&entry->hdr.len_op, cmd_size); 1184bc2d214aSBodo Stroesser entry->tmr_type = tmr->tmr_type; 1185bc2d214aSBodo Stroesser entry->cmd_cnt = tmr->tmr_cmd_cnt; 1186bc2d214aSBodo Stroesser memcpy(&entry->cmd_ids[0], &tmr->tmr_cmd_ids[0], id_list_sz); 1187bc2d214aSBodo Stroesser tcmu_flush_dcache_range(entry, cmd_size); 1188bc2d214aSBodo Stroesser 1189bc2d214aSBodo Stroesser UPDATE_HEAD(mb->cmd_head, cmd_size, udev->cmdr_size); 1190bc2d214aSBodo Stroesser tcmu_flush_dcache_range(mb, sizeof(*mb)); 1191bc2d214aSBodo Stroesser 1192bc2d214aSBodo Stroesser uio_event_notify(&udev->uio_info); 1193bc2d214aSBodo Stroesser 1194bc2d214aSBodo Stroesser out_free: 1195bc2d214aSBodo Stroesser kfree(tmr); 1196bc2d214aSBodo Stroesser 1197bc2d214aSBodo Stroesser return 0; 1198bc2d214aSBodo Stroesser } 1199bc2d214aSBodo Stroesser 120002eb924fSAndy Grover static sense_reason_t 120102eb924fSAndy Grover tcmu_queue_cmd(struct se_cmd *se_cmd) 12027c9e7a6fSAndy Grover { 1203af1dd7ffSMike Christie struct se_device *se_dev = se_cmd->se_dev; 1204af1dd7ffSMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 12057c9e7a6fSAndy Grover struct tcmu_cmd *tcmu_cmd; 1206c9684927SBodo Stroesser sense_reason_t scsi_ret = TCM_CHECK_CONDITION_ABORT_CMD; 1207c9684927SBodo Stroesser int ret = -1; 12087c9e7a6fSAndy Grover 12097c9e7a6fSAndy Grover tcmu_cmd = tcmu_alloc_cmd(se_cmd); 12107c9e7a6fSAndy Grover if (!tcmu_cmd) 121102eb924fSAndy Grover return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 12127c9e7a6fSAndy Grover 1213af1dd7ffSMike Christie mutex_lock(&udev->cmdr_lock); 1214c9684927SBodo Stroesser if (!(se_cmd->transport_state & CMD_T_ABORTED)) 1215af1dd7ffSMike Christie ret = queue_cmd_ring(tcmu_cmd, &scsi_ret); 1216af1dd7ffSMike Christie if (ret < 0) 1217141685a3SXiubo Li tcmu_free_cmd(tcmu_cmd); 1218780e1384SShin'ichiro Kawasaki else 1219780e1384SShin'ichiro Kawasaki se_cmd->priv = tcmu_cmd; 1220a3512902SBodo Stroesser mutex_unlock(&udev->cmdr_lock); 12216fd0ce79SMike Christie return scsi_ret; 12227c9e7a6fSAndy Grover } 12237c9e7a6fSAndy Grover 1224ed212ca8SBodo Stroesser static void tcmu_set_next_deadline(struct list_head *queue, 1225ed212ca8SBodo Stroesser struct timer_list *timer) 1226ed212ca8SBodo Stroesser { 1227ed212ca8SBodo Stroesser struct tcmu_cmd *cmd; 1228ed212ca8SBodo Stroesser 1229ed212ca8SBodo Stroesser if (!list_empty(queue)) { 1230ed212ca8SBodo Stroesser cmd = list_first_entry(queue, struct tcmu_cmd, queue_entry); 1231ed212ca8SBodo Stroesser mod_timer(timer, cmd->deadline); 1232ed212ca8SBodo Stroesser } else 1233ed212ca8SBodo Stroesser del_timer(timer); 1234ed212ca8SBodo Stroesser } 1235ed212ca8SBodo Stroesser 1236bc2d214aSBodo Stroesser static int 1237bc2d214aSBodo Stroesser tcmu_tmr_type(enum tcm_tmreq_table tmf) 1238bc2d214aSBodo Stroesser { 1239bc2d214aSBodo Stroesser switch (tmf) { 1240bc2d214aSBodo Stroesser case TMR_ABORT_TASK: return TCMU_TMR_ABORT_TASK; 1241bc2d214aSBodo Stroesser case TMR_ABORT_TASK_SET: return TCMU_TMR_ABORT_TASK_SET; 1242bc2d214aSBodo Stroesser case TMR_CLEAR_ACA: return TCMU_TMR_CLEAR_ACA; 1243bc2d214aSBodo Stroesser case TMR_CLEAR_TASK_SET: return TCMU_TMR_CLEAR_TASK_SET; 1244bc2d214aSBodo Stroesser case TMR_LUN_RESET: return TCMU_TMR_LUN_RESET; 1245bc2d214aSBodo Stroesser case TMR_TARGET_WARM_RESET: return TCMU_TMR_TARGET_WARM_RESET; 1246bc2d214aSBodo Stroesser case TMR_TARGET_COLD_RESET: return TCMU_TMR_TARGET_COLD_RESET; 1247bc2d214aSBodo Stroesser case TMR_LUN_RESET_PRO: return TCMU_TMR_LUN_RESET_PRO; 1248bc2d214aSBodo Stroesser default: return TCMU_TMR_UNKNOWN; 1249bc2d214aSBodo Stroesser } 1250bc2d214aSBodo Stroesser } 1251bc2d214aSBodo Stroesser 1252bc2d214aSBodo Stroesser static void 1253bc2d214aSBodo Stroesser tcmu_tmr_notify(struct se_device *se_dev, enum tcm_tmreq_table tmf, 1254bc2d214aSBodo Stroesser struct list_head *cmd_list) 1255bc2d214aSBodo Stroesser { 1256bc2d214aSBodo Stroesser int i = 0, cmd_cnt = 0; 1257bc2d214aSBodo Stroesser bool unqueued = false; 1258bc2d214aSBodo Stroesser struct tcmu_cmd *cmd; 1259bc2d214aSBodo Stroesser struct se_cmd *se_cmd; 1260bc2d214aSBodo Stroesser struct tcmu_tmr *tmr; 1261bc2d214aSBodo Stroesser struct tcmu_dev *udev = TCMU_DEV(se_dev); 1262bc2d214aSBodo Stroesser 1263bc2d214aSBodo Stroesser mutex_lock(&udev->cmdr_lock); 1264bc2d214aSBodo Stroesser 1265bc2d214aSBodo Stroesser /* First we check for aborted commands in qfull_queue */ 1266bc2d214aSBodo Stroesser list_for_each_entry(se_cmd, cmd_list, state_list) { 1267bc2d214aSBodo Stroesser i++; 1268bc2d214aSBodo Stroesser if (!se_cmd->priv) 1269bc2d214aSBodo Stroesser continue; 1270bc2d214aSBodo Stroesser cmd = se_cmd->priv; 1271bc2d214aSBodo Stroesser /* Commands on qfull queue have no id yet */ 1272bc2d214aSBodo Stroesser if (cmd->cmd_id) { 1273bc2d214aSBodo Stroesser cmd_cnt++; 1274bc2d214aSBodo Stroesser continue; 1275bc2d214aSBodo Stroesser } 1276bc2d214aSBodo Stroesser pr_debug("Removing aborted command %p from queue on dev %s.\n", 1277bc2d214aSBodo Stroesser cmd, udev->name); 1278bc2d214aSBodo Stroesser 1279bc2d214aSBodo Stroesser list_del_init(&cmd->queue_entry); 1280bc2d214aSBodo Stroesser tcmu_free_cmd(cmd); 1281780e1384SShin'ichiro Kawasaki se_cmd->priv = NULL; 1282bc2d214aSBodo Stroesser target_complete_cmd(se_cmd, SAM_STAT_TASK_ABORTED); 1283bc2d214aSBodo Stroesser unqueued = true; 1284bc2d214aSBodo Stroesser } 1285bc2d214aSBodo Stroesser if (unqueued) 1286bc2d214aSBodo Stroesser tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer); 1287bc2d214aSBodo Stroesser 128859526d7aSBodo Stroesser if (!test_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags)) 128959526d7aSBodo Stroesser goto unlock; 129059526d7aSBodo Stroesser 1291bc2d214aSBodo Stroesser pr_debug("TMR event %d on dev %s, aborted cmds %d, afflicted cmd_ids %d\n", 1292bc2d214aSBodo Stroesser tcmu_tmr_type(tmf), udev->name, i, cmd_cnt); 1293bc2d214aSBodo Stroesser 1294c20bda34SGustavo A. R. Silva tmr = kmalloc(struct_size(tmr, tmr_cmd_ids, cmd_cnt), GFP_NOIO); 1295bc2d214aSBodo Stroesser if (!tmr) 1296bc2d214aSBodo Stroesser goto unlock; 1297bc2d214aSBodo Stroesser 1298bc2d214aSBodo Stroesser tmr->tmr_type = tcmu_tmr_type(tmf); 1299bc2d214aSBodo Stroesser tmr->tmr_cmd_cnt = cmd_cnt; 1300bc2d214aSBodo Stroesser 1301bc2d214aSBodo Stroesser if (cmd_cnt != 0) { 1302bc2d214aSBodo Stroesser cmd_cnt = 0; 1303bc2d214aSBodo Stroesser list_for_each_entry(se_cmd, cmd_list, state_list) { 1304bc2d214aSBodo Stroesser if (!se_cmd->priv) 1305bc2d214aSBodo Stroesser continue; 1306bc2d214aSBodo Stroesser cmd = se_cmd->priv; 1307bc2d214aSBodo Stroesser if (cmd->cmd_id) 1308bc2d214aSBodo Stroesser tmr->tmr_cmd_ids[cmd_cnt++] = cmd->cmd_id; 1309bc2d214aSBodo Stroesser } 1310bc2d214aSBodo Stroesser } 1311bc2d214aSBodo Stroesser 1312bc2d214aSBodo Stroesser queue_tmr_ring(udev, tmr); 1313bc2d214aSBodo Stroesser 1314bc2d214aSBodo Stroesser unlock: 1315bc2d214aSBodo Stroesser mutex_unlock(&udev->cmdr_lock); 1316bc2d214aSBodo Stroesser } 1317bc2d214aSBodo Stroesser 1318018c1491SBodo Stroesser static bool tcmu_handle_completion(struct tcmu_cmd *cmd, 1319018c1491SBodo Stroesser struct tcmu_cmd_entry *entry, bool keep_buf) 13207c9e7a6fSAndy Grover { 13217c9e7a6fSAndy Grover struct se_cmd *se_cmd = cmd->se_cmd; 13227c9e7a6fSAndy Grover struct tcmu_dev *udev = cmd->tcmu_dev; 13236c3796d1Sbstroesser@ts.fujitsu.com bool read_len_valid = false; 1324018c1491SBodo Stroesser bool ret = true; 1325a86a7586SDmitry Fomichev uint32_t read_len; 13267c9e7a6fSAndy Grover 1327b25c7863SSheng Yang /* 1328b25c7863SSheng Yang * cmd has been completed already from timeout, just reclaim 13293d9b9555SAndy Grover * data area space and free cmd 1330b25c7863SSheng Yang */ 1331a86a7586SDmitry Fomichev if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { 1332a86a7586SDmitry Fomichev WARN_ON_ONCE(se_cmd); 1333141685a3SXiubo Li goto out; 1334a86a7586SDmitry Fomichev } 1335018c1491SBodo Stroesser if (test_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags)) { 1336018c1491SBodo Stroesser pr_err("cmd_id %u already completed with KEEP_BUF, ring is broken\n", 1337018c1491SBodo Stroesser entry->hdr.cmd_id); 1338018c1491SBodo Stroesser set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags); 1339018c1491SBodo Stroesser ret = false; 1340018c1491SBodo Stroesser goto out; 1341018c1491SBodo Stroesser } 1342b25c7863SSheng Yang 1343a94a2572SXiubo Li list_del_init(&cmd->queue_entry); 1344a94a2572SXiubo Li 1345141685a3SXiubo Li tcmu_cmd_reset_dbi_cur(cmd); 13467c9e7a6fSAndy Grover 13470ad46af8SAndy Grover if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) { 13480ad46af8SAndy Grover pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n", 13490ad46af8SAndy Grover cmd->se_cmd); 1350ed97d0cdSAndy Grover entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION; 13516c3796d1Sbstroesser@ts.fujitsu.com goto done; 13526c3796d1Sbstroesser@ts.fujitsu.com } 13536c3796d1Sbstroesser@ts.fujitsu.com 1354a86a7586SDmitry Fomichev read_len = se_cmd->data_length; 13556c3796d1Sbstroesser@ts.fujitsu.com if (se_cmd->data_direction == DMA_FROM_DEVICE && 13566c3796d1Sbstroesser@ts.fujitsu.com (entry->hdr.uflags & TCMU_UFLAG_READ_LEN) && entry->rsp.read_len) { 13576c3796d1Sbstroesser@ts.fujitsu.com read_len_valid = true; 13586c3796d1Sbstroesser@ts.fujitsu.com if (entry->rsp.read_len < read_len) 13596c3796d1Sbstroesser@ts.fujitsu.com read_len = entry->rsp.read_len; 13606c3796d1Sbstroesser@ts.fujitsu.com } 13616c3796d1Sbstroesser@ts.fujitsu.com 13626c3796d1Sbstroesser@ts.fujitsu.com if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) { 1363406f74c2SMike Christie transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer); 13646c3796d1Sbstroesser@ts.fujitsu.com if (!read_len_valid ) 13656c3796d1Sbstroesser@ts.fujitsu.com goto done; 13666c3796d1Sbstroesser@ts.fujitsu.com else 13676c3796d1Sbstroesser@ts.fujitsu.com se_cmd->se_cmd_flags |= SCF_TREAT_READ_AS_NORMAL; 13686c3796d1Sbstroesser@ts.fujitsu.com } 13696c3796d1Sbstroesser@ts.fujitsu.com if (se_cmd->se_cmd_flags & SCF_BIDI) { 137026418649SSheng Yang /* Get Data-In buffer before clean up */ 13716c3796d1Sbstroesser@ts.fujitsu.com gather_data_area(udev, cmd, true, read_len); 1372e4648b01SIlias Tsitsimpis } else if (se_cmd->data_direction == DMA_FROM_DEVICE) { 13736c3796d1Sbstroesser@ts.fujitsu.com gather_data_area(udev, cmd, false, read_len); 13747c9e7a6fSAndy Grover } else if (se_cmd->data_direction == DMA_TO_DEVICE) { 1375141685a3SXiubo Li /* TODO: */ 13762bc396a2SIlias Tsitsimpis } else if (se_cmd->data_direction != DMA_NONE) { 13772bc396a2SIlias Tsitsimpis pr_warn("TCMU: data direction was %d!\n", 13782bc396a2SIlias Tsitsimpis se_cmd->data_direction); 13797c9e7a6fSAndy Grover } 13807c9e7a6fSAndy Grover 13816c3796d1Sbstroesser@ts.fujitsu.com done: 1382780e1384SShin'ichiro Kawasaki se_cmd->priv = NULL; 13836c3796d1Sbstroesser@ts.fujitsu.com if (read_len_valid) { 13846c3796d1Sbstroesser@ts.fujitsu.com pr_debug("read_len = %d\n", read_len); 13856c3796d1Sbstroesser@ts.fujitsu.com target_complete_cmd_with_length(cmd->se_cmd, 13866c3796d1Sbstroesser@ts.fujitsu.com entry->rsp.scsi_status, read_len); 13876c3796d1Sbstroesser@ts.fujitsu.com } else 13887c9e7a6fSAndy Grover target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status); 13897c9e7a6fSAndy Grover 1390141685a3SXiubo Li out: 1391018c1491SBodo Stroesser if (!keep_buf) { 1392b6df4b79SXiubo Li tcmu_cmd_free_data(cmd, cmd->dbi_cnt); 1393141685a3SXiubo Li tcmu_free_cmd(cmd); 1394018c1491SBodo Stroesser } else { 1395018c1491SBodo Stroesser /* 1396018c1491SBodo Stroesser * Keep this command after completion, since userspace still 1397018c1491SBodo Stroesser * needs the data buffer. Mark it with TCMU_CMD_BIT_KEEP_BUF 1398018c1491SBodo Stroesser * and reset potential TCMU_CMD_BIT_EXPIRED, so we don't accept 1399018c1491SBodo Stroesser * a second completion later. 1400018c1491SBodo Stroesser * Userspace can free the buffer later by writing the cmd_id 1401018c1491SBodo Stroesser * to new action attribute free_kept_buf. 1402018c1491SBodo Stroesser */ 1403018c1491SBodo Stroesser clear_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags); 1404018c1491SBodo Stroesser set_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags); 1405018c1491SBodo Stroesser } 1406018c1491SBodo Stroesser return ret; 14077c9e7a6fSAndy Grover } 14087c9e7a6fSAndy Grover 1409bc2d214aSBodo Stroesser static int tcmu_run_tmr_queue(struct tcmu_dev *udev) 1410bc2d214aSBodo Stroesser { 1411bc2d214aSBodo Stroesser struct tcmu_tmr *tmr, *tmp; 1412bc2d214aSBodo Stroesser LIST_HEAD(tmrs); 1413bc2d214aSBodo Stroesser 1414bc2d214aSBodo Stroesser if (list_empty(&udev->tmr_queue)) 1415bc2d214aSBodo Stroesser return 1; 1416bc2d214aSBodo Stroesser 1417bc2d214aSBodo Stroesser pr_debug("running %s's tmr queue\n", udev->name); 1418bc2d214aSBodo Stroesser 1419bc2d214aSBodo Stroesser list_splice_init(&udev->tmr_queue, &tmrs); 1420bc2d214aSBodo Stroesser 1421bc2d214aSBodo Stroesser list_for_each_entry_safe(tmr, tmp, &tmrs, queue_entry) { 1422bc2d214aSBodo Stroesser list_del_init(&tmr->queue_entry); 1423bc2d214aSBodo Stroesser 1424bc2d214aSBodo Stroesser pr_debug("removing tmr %p on dev %s from queue\n", 1425bc2d214aSBodo Stroesser tmr, udev->name); 1426bc2d214aSBodo Stroesser 1427bc2d214aSBodo Stroesser if (queue_tmr_ring(udev, tmr)) { 1428bc2d214aSBodo Stroesser pr_debug("ran out of space during tmr queue run\n"); 1429bc2d214aSBodo Stroesser /* 1430bc2d214aSBodo Stroesser * tmr was requeued, so just put all tmrs back in 1431bc2d214aSBodo Stroesser * the queue 1432bc2d214aSBodo Stroesser */ 1433bc2d214aSBodo Stroesser list_splice_tail(&tmrs, &udev->tmr_queue); 1434bc2d214aSBodo Stroesser return 0; 1435bc2d214aSBodo Stroesser } 1436bc2d214aSBodo Stroesser } 1437bc2d214aSBodo Stroesser 1438bc2d214aSBodo Stroesser return 1; 1439bc2d214aSBodo Stroesser } 1440bc2d214aSBodo Stroesser 14419814b55cSBodo Stroesser static bool tcmu_handle_completions(struct tcmu_dev *udev) 14427c9e7a6fSAndy Grover { 14437c9e7a6fSAndy Grover struct tcmu_mailbox *mb; 1444a94a2572SXiubo Li struct tcmu_cmd *cmd; 1445bc2d214aSBodo Stroesser bool free_space = false; 14467c9e7a6fSAndy Grover 14477c9e7a6fSAndy Grover if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) { 14487c9e7a6fSAndy Grover pr_err("ring broken, not handling completions\n"); 144982473125Skernel test robot return false; 14507c9e7a6fSAndy Grover } 14517c9e7a6fSAndy Grover 14527c9e7a6fSAndy Grover mb = udev->mb_addr; 14537c9e7a6fSAndy Grover tcmu_flush_dcache_range(mb, sizeof(*mb)); 14547c9e7a6fSAndy Grover 14556aa7de05SMark Rutland while (udev->cmdr_last_cleaned != READ_ONCE(mb->cmd_tail)) { 14567c9e7a6fSAndy Grover 1457ecddbb7eSBodo Stroesser struct tcmu_cmd_entry *entry = udev->cmdr + udev->cmdr_last_cleaned; 1458018c1491SBodo Stroesser bool keep_buf; 14597c9e7a6fSAndy Grover 14605a0c256dSBodo Stroesser /* 14615a0c256dSBodo Stroesser * Flush max. up to end of cmd ring since current entry might 14625a0c256dSBodo Stroesser * be a padding that is shorter than sizeof(*entry) 14635a0c256dSBodo Stroesser */ 14645a0c256dSBodo Stroesser size_t ring_left = head_to_end(udev->cmdr_last_cleaned, 14655a0c256dSBodo Stroesser udev->cmdr_size); 14665a0c256dSBodo Stroesser tcmu_flush_dcache_range(entry, ring_left < sizeof(*entry) ? 14675a0c256dSBodo Stroesser ring_left : sizeof(*entry)); 14687c9e7a6fSAndy Grover 1469bc2d214aSBodo Stroesser free_space = true; 1470bc2d214aSBodo Stroesser 1471bc2d214aSBodo Stroesser if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD || 1472bc2d214aSBodo Stroesser tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_TMR) { 14730ad46af8SAndy Grover UPDATE_HEAD(udev->cmdr_last_cleaned, 14740ad46af8SAndy Grover tcmu_hdr_get_len(entry->hdr.len_op), 14750ad46af8SAndy Grover udev->cmdr_size); 14767c9e7a6fSAndy Grover continue; 14777c9e7a6fSAndy Grover } 14780ad46af8SAndy Grover WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD); 14797c9e7a6fSAndy Grover 1480018c1491SBodo Stroesser keep_buf = !!(entry->hdr.uflags & TCMU_UFLAG_KEEP_BUF); 1481018c1491SBodo Stroesser if (keep_buf) 1482018c1491SBodo Stroesser cmd = xa_load(&udev->commands, entry->hdr.cmd_id); 1483018c1491SBodo Stroesser else 1484d3cbb743SBodo Stroesser cmd = xa_erase(&udev->commands, entry->hdr.cmd_id); 14857c9e7a6fSAndy Grover if (!cmd) { 148688cf1073SMike Christie pr_err("cmd_id %u not found, ring is broken\n", 148788cf1073SMike Christie entry->hdr.cmd_id); 14887c9e7a6fSAndy Grover set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags); 14899814b55cSBodo Stroesser return false; 14907c9e7a6fSAndy Grover } 14917c9e7a6fSAndy Grover 1492018c1491SBodo Stroesser if (!tcmu_handle_completion(cmd, entry, keep_buf)) 1493018c1491SBodo Stroesser break; 14947c9e7a6fSAndy Grover 14950ad46af8SAndy Grover UPDATE_HEAD(udev->cmdr_last_cleaned, 14960ad46af8SAndy Grover tcmu_hdr_get_len(entry->hdr.len_op), 14970ad46af8SAndy Grover udev->cmdr_size); 14987c9e7a6fSAndy Grover } 1499bc2d214aSBodo Stroesser if (free_space) 1500bc2d214aSBodo Stroesser free_space = tcmu_run_tmr_queue(udev); 15017c9e7a6fSAndy Grover 15028b084d9dSBodo Stroesser if (atomic_read(&global_page_count) > tcmu_global_max_pages && 1503d3cbb743SBodo Stroesser xa_empty(&udev->commands) && list_empty(&udev->qfull_queue)) { 1504af1dd7ffSMike Christie /* 1505ed212ca8SBodo Stroesser * Allocated blocks exceeded global block limit, currently no 1506ed212ca8SBodo Stroesser * more pending or waiting commands so try to reclaim blocks. 1507af1dd7ffSMike Christie */ 1508af1dd7ffSMike Christie schedule_delayed_work(&tcmu_unmap_work, 0); 1509af1dd7ffSMike Christie } 1510ed212ca8SBodo Stroesser if (udev->cmd_time_out) 1511a94a2572SXiubo Li tcmu_set_next_deadline(&udev->inflight_queue, &udev->cmd_timer); 15127c9e7a6fSAndy Grover 1513bc2d214aSBodo Stroesser return free_space; 15147c9e7a6fSAndy Grover } 15157c9e7a6fSAndy Grover 151661fb2482SBodo Stroesser static void tcmu_check_expired_ring_cmd(struct tcmu_cmd *cmd) 15177c9e7a6fSAndy Grover { 1518af1dd7ffSMike Christie struct se_cmd *se_cmd; 15197c9e7a6fSAndy Grover 1520ed212ca8SBodo Stroesser if (!time_after_eq(jiffies, cmd->deadline)) 152161fb2482SBodo Stroesser return; 15229103575aSMike Christie 1523af1dd7ffSMike Christie set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags); 152440d883b0SXiubo Li list_del_init(&cmd->queue_entry); 152561fb2482SBodo Stroesser se_cmd = cmd->se_cmd; 1526a3512902SBodo Stroesser se_cmd->priv = NULL; 1527a86a7586SDmitry Fomichev cmd->se_cmd = NULL; 152861fb2482SBodo Stroesser 152961fb2482SBodo Stroesser pr_debug("Timing out inflight cmd %u on dev %s.\n", 153061fb2482SBodo Stroesser cmd->cmd_id, cmd->tcmu_dev->name); 153161fb2482SBodo Stroesser 153261fb2482SBodo Stroesser target_complete_cmd(se_cmd, SAM_STAT_CHECK_CONDITION); 1533af1dd7ffSMike Christie } 15349103575aSMike Christie 153561fb2482SBodo Stroesser static void tcmu_check_expired_queue_cmd(struct tcmu_cmd *cmd) 153661fb2482SBodo Stroesser { 153761fb2482SBodo Stroesser struct se_cmd *se_cmd; 15389103575aSMike Christie 1539ed212ca8SBodo Stroesser if (!time_after_eq(jiffies, cmd->deadline)) 154061fb2482SBodo Stroesser return; 154161fb2482SBodo Stroesser 15429d7464b1SDan Carpenter pr_debug("Timing out queued cmd %p on dev %s.\n", 15439d7464b1SDan Carpenter cmd, cmd->tcmu_dev->name); 15449d7464b1SDan Carpenter 154561fb2482SBodo Stroesser list_del_init(&cmd->queue_entry); 154661fb2482SBodo Stroesser se_cmd = cmd->se_cmd; 154761fb2482SBodo Stroesser tcmu_free_cmd(cmd); 154861fb2482SBodo Stroesser 1549780e1384SShin'ichiro Kawasaki se_cmd->priv = NULL; 155061fb2482SBodo Stroesser target_complete_cmd(se_cmd, SAM_STAT_TASK_SET_FULL); 15517c9e7a6fSAndy Grover } 15527c9e7a6fSAndy Grover 15539103575aSMike Christie static void tcmu_device_timedout(struct tcmu_dev *udev) 15547c9e7a6fSAndy Grover { 1555488ebe4cSMike Christie spin_lock(&timed_out_udevs_lock); 1556488ebe4cSMike Christie if (list_empty(&udev->timedout_entry)) 1557488ebe4cSMike Christie list_add_tail(&udev->timedout_entry, &timed_out_udevs); 1558488ebe4cSMike Christie spin_unlock(&timed_out_udevs_lock); 15597c9e7a6fSAndy Grover 1560af1dd7ffSMike Christie schedule_delayed_work(&tcmu_unmap_work, 0); 15617c9e7a6fSAndy Grover } 15627c9e7a6fSAndy Grover 15639103575aSMike Christie static void tcmu_cmd_timedout(struct timer_list *t) 15649103575aSMike Christie { 15659103575aSMike Christie struct tcmu_dev *udev = from_timer(udev, t, cmd_timer); 15669103575aSMike Christie 15679103575aSMike Christie pr_debug("%s cmd timeout has expired\n", udev->name); 15689103575aSMike Christie tcmu_device_timedout(udev); 15699103575aSMike Christie } 15709103575aSMike Christie 15719103575aSMike Christie static void tcmu_qfull_timedout(struct timer_list *t) 15729103575aSMike Christie { 15739103575aSMike Christie struct tcmu_dev *udev = from_timer(udev, t, qfull_timer); 15749103575aSMike Christie 15759103575aSMike Christie pr_debug("%s qfull timeout has expired\n", udev->name); 15769103575aSMike Christie tcmu_device_timedout(udev); 15779103575aSMike Christie } 15789103575aSMike Christie 15797c9e7a6fSAndy Grover static int tcmu_attach_hba(struct se_hba *hba, u32 host_id) 15807c9e7a6fSAndy Grover { 15817c9e7a6fSAndy Grover struct tcmu_hba *tcmu_hba; 15827c9e7a6fSAndy Grover 15837c9e7a6fSAndy Grover tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL); 15847c9e7a6fSAndy Grover if (!tcmu_hba) 15857c9e7a6fSAndy Grover return -ENOMEM; 15867c9e7a6fSAndy Grover 15877c9e7a6fSAndy Grover tcmu_hba->host_id = host_id; 15887c9e7a6fSAndy Grover hba->hba_ptr = tcmu_hba; 15897c9e7a6fSAndy Grover 15907c9e7a6fSAndy Grover return 0; 15917c9e7a6fSAndy Grover } 15927c9e7a6fSAndy Grover 15937c9e7a6fSAndy Grover static void tcmu_detach_hba(struct se_hba *hba) 15947c9e7a6fSAndy Grover { 15957c9e7a6fSAndy Grover kfree(hba->hba_ptr); 15967c9e7a6fSAndy Grover hba->hba_ptr = NULL; 15977c9e7a6fSAndy Grover } 15987c9e7a6fSAndy Grover 15997c9e7a6fSAndy Grover static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name) 16007c9e7a6fSAndy Grover { 16017c9e7a6fSAndy Grover struct tcmu_dev *udev; 16027c9e7a6fSAndy Grover 16037c9e7a6fSAndy Grover udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL); 16047c9e7a6fSAndy Grover if (!udev) 16057c9e7a6fSAndy Grover return NULL; 1606f3cdbe39SMike Christie kref_init(&udev->kref); 16077c9e7a6fSAndy Grover 16087c9e7a6fSAndy Grover udev->name = kstrdup(name, GFP_KERNEL); 16097c9e7a6fSAndy Grover if (!udev->name) { 16107c9e7a6fSAndy Grover kfree(udev); 16117c9e7a6fSAndy Grover return NULL; 16127c9e7a6fSAndy Grover } 16137c9e7a6fSAndy Grover 16147c9e7a6fSAndy Grover udev->hba = hba; 1615af980e46SMike Christie udev->cmd_time_out = TCMU_TIME_OUT; 16169103575aSMike Christie udev->qfull_time_out = -1; 16177c9e7a6fSAndy Grover 1618e719afdcSBodo Stroesser udev->data_pages_per_blk = DATA_PAGES_PER_BLK_DEF; 1619e719afdcSBodo Stroesser udev->max_blocks = DATA_AREA_PAGES_DEF / udev->data_pages_per_blk; 1620c7ede4f0SGuixin Liu udev->cmdr_size = CMDR_SIZE_DEF; 1621f5ce815fSBodo Stroesser udev->data_area_mb = TCMU_PAGES_TO_MBS(DATA_AREA_PAGES_DEF); 1622e719afdcSBodo Stroesser 1623b6df4b79SXiubo Li mutex_init(&udev->cmdr_lock); 16247c9e7a6fSAndy Grover 1625ff07e4a4SMike Christie INIT_LIST_HEAD(&udev->node); 1626488ebe4cSMike Christie INIT_LIST_HEAD(&udev->timedout_entry); 1627a94a2572SXiubo Li INIT_LIST_HEAD(&udev->qfull_queue); 1628bc2d214aSBodo Stroesser INIT_LIST_HEAD(&udev->tmr_queue); 1629a94a2572SXiubo Li INIT_LIST_HEAD(&udev->inflight_queue); 1630d3cbb743SBodo Stroesser xa_init_flags(&udev->commands, XA_FLAGS_ALLOC1); 16317c9e7a6fSAndy Grover 16329103575aSMike Christie timer_setup(&udev->qfull_timer, tcmu_qfull_timedout, 0); 16339103575aSMike Christie timer_setup(&udev->cmd_timer, tcmu_cmd_timedout, 0); 16347c9e7a6fSAndy Grover 16358b084d9dSBodo Stroesser xa_init(&udev->data_pages); 1636c22adc0bSXiubo Li 16377c9e7a6fSAndy Grover return &udev->se_dev; 16387c9e7a6fSAndy Grover } 16397c9e7a6fSAndy Grover 164043bf922cSBodo Stroesser static void tcmu_dev_call_rcu(struct rcu_head *p) 164143bf922cSBodo Stroesser { 164243bf922cSBodo Stroesser struct se_device *dev = container_of(p, struct se_device, rcu_head); 164343bf922cSBodo Stroesser struct tcmu_dev *udev = TCMU_DEV(dev); 164443bf922cSBodo Stroesser 164543bf922cSBodo Stroesser kfree(udev->uio_info.name); 164643bf922cSBodo Stroesser kfree(udev->name); 164743bf922cSBodo Stroesser kfree(udev); 164843bf922cSBodo Stroesser } 164943bf922cSBodo Stroesser 165043bf922cSBodo Stroesser static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd) 165143bf922cSBodo Stroesser { 1652018c1491SBodo Stroesser if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags) || 1653018c1491SBodo Stroesser test_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags)) { 165443bf922cSBodo Stroesser kmem_cache_free(tcmu_cmd_cache, cmd); 165543bf922cSBodo Stroesser return 0; 165643bf922cSBodo Stroesser } 165743bf922cSBodo Stroesser return -EINVAL; 165843bf922cSBodo Stroesser } 165943bf922cSBodo Stroesser 1660e719afdcSBodo Stroesser static u32 tcmu_blocks_release(struct tcmu_dev *udev, unsigned long first, 1661f7c89771SBodo Stroesser unsigned long last) 166243bf922cSBodo Stroesser { 1663e719afdcSBodo Stroesser XA_STATE(xas, &udev->data_pages, first * udev->data_pages_per_blk); 166443bf922cSBodo Stroesser struct page *page; 1665f5ce815fSBodo Stroesser u32 pages_freed = 0; 166643bf922cSBodo Stroesser 1667f7c89771SBodo Stroesser xas_lock(&xas); 1668e719afdcSBodo Stroesser xas_for_each(&xas, page, (last + 1) * udev->data_pages_per_blk - 1) { 1669f7c89771SBodo Stroesser xas_store(&xas, NULL); 167043bf922cSBodo Stroesser __free_page(page); 1671f5ce815fSBodo Stroesser pages_freed++; 167243bf922cSBodo Stroesser } 1673f7c89771SBodo Stroesser xas_unlock(&xas); 1674f5ce815fSBodo Stroesser 1675f5ce815fSBodo Stroesser atomic_sub(pages_freed, &global_page_count); 1676f5ce815fSBodo Stroesser 1677f5ce815fSBodo Stroesser return pages_freed; 167843bf922cSBodo Stroesser } 167943bf922cSBodo Stroesser 168043bf922cSBodo Stroesser static void tcmu_remove_all_queued_tmr(struct tcmu_dev *udev) 168143bf922cSBodo Stroesser { 168243bf922cSBodo Stroesser struct tcmu_tmr *tmr, *tmp; 168343bf922cSBodo Stroesser 168443bf922cSBodo Stroesser list_for_each_entry_safe(tmr, tmp, &udev->tmr_queue, queue_entry) { 168543bf922cSBodo Stroesser list_del_init(&tmr->queue_entry); 168643bf922cSBodo Stroesser kfree(tmr); 168743bf922cSBodo Stroesser } 168843bf922cSBodo Stroesser } 168943bf922cSBodo Stroesser 169043bf922cSBodo Stroesser static void tcmu_dev_kref_release(struct kref *kref) 169143bf922cSBodo Stroesser { 169243bf922cSBodo Stroesser struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref); 169343bf922cSBodo Stroesser struct se_device *dev = &udev->se_dev; 169443bf922cSBodo Stroesser struct tcmu_cmd *cmd; 169543bf922cSBodo Stroesser bool all_expired = true; 1696d3cbb743SBodo Stroesser unsigned long i; 169743bf922cSBodo Stroesser 169843bf922cSBodo Stroesser vfree(udev->mb_addr); 169943bf922cSBodo Stroesser udev->mb_addr = NULL; 170043bf922cSBodo Stroesser 170143bf922cSBodo Stroesser spin_lock_bh(&timed_out_udevs_lock); 170243bf922cSBodo Stroesser if (!list_empty(&udev->timedout_entry)) 170343bf922cSBodo Stroesser list_del(&udev->timedout_entry); 170443bf922cSBodo Stroesser spin_unlock_bh(&timed_out_udevs_lock); 170543bf922cSBodo Stroesser 170643bf922cSBodo Stroesser /* Upper layer should drain all requests before calling this */ 170743bf922cSBodo Stroesser mutex_lock(&udev->cmdr_lock); 1708d3cbb743SBodo Stroesser xa_for_each(&udev->commands, i, cmd) { 170943bf922cSBodo Stroesser if (tcmu_check_and_free_pending_cmd(cmd) != 0) 171043bf922cSBodo Stroesser all_expired = false; 171143bf922cSBodo Stroesser } 171243bf922cSBodo Stroesser /* There can be left over TMR cmds. Remove them. */ 171343bf922cSBodo Stroesser tcmu_remove_all_queued_tmr(udev); 171443bf922cSBodo Stroesser if (!list_empty(&udev->qfull_queue)) 171543bf922cSBodo Stroesser all_expired = false; 1716d3cbb743SBodo Stroesser xa_destroy(&udev->commands); 171743bf922cSBodo Stroesser WARN_ON(!all_expired); 171843bf922cSBodo Stroesser 1719e719afdcSBodo Stroesser tcmu_blocks_release(udev, 0, udev->dbi_max); 172043bf922cSBodo Stroesser bitmap_free(udev->data_bitmap); 172143bf922cSBodo Stroesser mutex_unlock(&udev->cmdr_lock); 172243bf922cSBodo Stroesser 17238f33bb24SBodo Stroesser pr_debug("dev_kref_release\n"); 17248f33bb24SBodo Stroesser 172543bf922cSBodo Stroesser call_rcu(&dev->rcu_head, tcmu_dev_call_rcu); 172643bf922cSBodo Stroesser } 172743bf922cSBodo Stroesser 172861fb2482SBodo Stroesser static void run_qfull_queue(struct tcmu_dev *udev, bool fail) 1729af1dd7ffSMike Christie { 1730af1dd7ffSMike Christie struct tcmu_cmd *tcmu_cmd, *tmp_cmd; 1731af1dd7ffSMike Christie LIST_HEAD(cmds); 1732af1dd7ffSMike Christie sense_reason_t scsi_ret; 1733af1dd7ffSMike Christie int ret; 1734af1dd7ffSMike Christie 1735a94a2572SXiubo Li if (list_empty(&udev->qfull_queue)) 173661fb2482SBodo Stroesser return; 1737af1dd7ffSMike Christie 1738892782caSMike Christie pr_debug("running %s's cmdr queue forcefail %d\n", udev->name, fail); 1739af1dd7ffSMike Christie 1740a94a2572SXiubo Li list_splice_init(&udev->qfull_queue, &cmds); 1741af1dd7ffSMike Christie 1742a94a2572SXiubo Li list_for_each_entry_safe(tcmu_cmd, tmp_cmd, &cmds, queue_entry) { 1743a94a2572SXiubo Li list_del_init(&tcmu_cmd->queue_entry); 1744af1dd7ffSMike Christie 174561fb2482SBodo Stroesser pr_debug("removing cmd %p on dev %s from queue\n", 174661fb2482SBodo Stroesser tcmu_cmd, udev->name); 1747af1dd7ffSMike Christie 1748892782caSMike Christie if (fail) { 1749892782caSMike Christie /* 1750892782caSMike Christie * We were not able to even start the command, so 1751892782caSMike Christie * fail with busy to allow a retry in case runner 1752892782caSMike Christie * was only temporarily down. If the device is being 1753892782caSMike Christie * removed then LIO core will do the right thing and 1754892782caSMike Christie * fail the retry. 1755892782caSMike Christie */ 1756780e1384SShin'ichiro Kawasaki tcmu_cmd->se_cmd->priv = NULL; 1757892782caSMike Christie target_complete_cmd(tcmu_cmd->se_cmd, SAM_STAT_BUSY); 1758892782caSMike Christie tcmu_free_cmd(tcmu_cmd); 1759892782caSMike Christie continue; 1760892782caSMike Christie } 1761892782caSMike Christie 1762af1dd7ffSMike Christie ret = queue_cmd_ring(tcmu_cmd, &scsi_ret); 1763af1dd7ffSMike Christie if (ret < 0) { 176461fb2482SBodo Stroesser pr_debug("cmd %p on dev %s failed with %u\n", 176561fb2482SBodo Stroesser tcmu_cmd, udev->name, scsi_ret); 1766af1dd7ffSMike Christie /* 1767af1dd7ffSMike Christie * Ignore scsi_ret for now. target_complete_cmd 1768af1dd7ffSMike Christie * drops it. 1769af1dd7ffSMike Christie */ 1770780e1384SShin'ichiro Kawasaki tcmu_cmd->se_cmd->priv = NULL; 1771af1dd7ffSMike Christie target_complete_cmd(tcmu_cmd->se_cmd, 1772af1dd7ffSMike Christie SAM_STAT_CHECK_CONDITION); 1773af1dd7ffSMike Christie tcmu_free_cmd(tcmu_cmd); 1774af1dd7ffSMike Christie } else if (ret > 0) { 1775af1dd7ffSMike Christie pr_debug("ran out of space during cmdr queue run\n"); 1776af1dd7ffSMike Christie /* 1777af1dd7ffSMike Christie * cmd was requeued, so just put all cmds back in 1778af1dd7ffSMike Christie * the queue 1779af1dd7ffSMike Christie */ 1780a94a2572SXiubo Li list_splice_tail(&cmds, &udev->qfull_queue); 1781a94a2572SXiubo Li break; 1782af1dd7ffSMike Christie } 1783af1dd7ffSMike Christie } 1784a94a2572SXiubo Li 1785a94a2572SXiubo Li tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer); 1786af1dd7ffSMike Christie } 1787af1dd7ffSMike Christie 17887c9e7a6fSAndy Grover static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on) 17897c9e7a6fSAndy Grover { 1790af1dd7ffSMike Christie struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 17917c9e7a6fSAndy Grover 1792af1dd7ffSMike Christie mutex_lock(&udev->cmdr_lock); 1793bc2d214aSBodo Stroesser if (tcmu_handle_completions(udev)) 1794a94a2572SXiubo Li run_qfull_queue(udev, false); 1795af1dd7ffSMike Christie mutex_unlock(&udev->cmdr_lock); 17967c9e7a6fSAndy Grover 17977c9e7a6fSAndy Grover return 0; 17987c9e7a6fSAndy Grover } 17997c9e7a6fSAndy Grover 18007c9e7a6fSAndy Grover /* 18017c9e7a6fSAndy Grover * mmap code from uio.c. Copied here because we want to hook mmap() 18027c9e7a6fSAndy Grover * and this stuff must come along. 18037c9e7a6fSAndy Grover */ 18047c9e7a6fSAndy Grover static int tcmu_find_mem_index(struct vm_area_struct *vma) 18057c9e7a6fSAndy Grover { 18067c9e7a6fSAndy Grover struct tcmu_dev *udev = vma->vm_private_data; 18077c9e7a6fSAndy Grover struct uio_info *info = &udev->uio_info; 18087c9e7a6fSAndy Grover 18097c9e7a6fSAndy Grover if (vma->vm_pgoff < MAX_UIO_MAPS) { 18107c9e7a6fSAndy Grover if (info->mem[vma->vm_pgoff].size == 0) 18117c9e7a6fSAndy Grover return -1; 18127c9e7a6fSAndy Grover return (int)vma->vm_pgoff; 18137c9e7a6fSAndy Grover } 18147c9e7a6fSAndy Grover return -1; 18157c9e7a6fSAndy Grover } 18167c9e7a6fSAndy Grover 18178b084d9dSBodo Stroesser static struct page *tcmu_try_get_data_page(struct tcmu_dev *udev, uint32_t dpi) 1818b6df4b79SXiubo Li { 1819b6df4b79SXiubo Li struct page *page; 1820b6df4b79SXiubo Li 1821b6df4b79SXiubo Li mutex_lock(&udev->cmdr_lock); 18223722e36cSBodo Stroesser page = xa_load(&udev->data_pages, dpi); 1823b6df4b79SXiubo Li if (likely(page)) { 1824*a6968f7aSXiaoguang Wang get_page(page); 1825b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1826b6df4b79SXiubo Li return page; 1827b6df4b79SXiubo Li } 1828b6df4b79SXiubo Li 1829b6df4b79SXiubo Li /* 1830c1c390baSMike Christie * Userspace messed up and passed in a address not in the 1831c1c390baSMike Christie * data iov passed to it. 1832b6df4b79SXiubo Li */ 18338b084d9dSBodo Stroesser pr_err("Invalid addr to data page mapping (dpi %u) on device %s\n", 18348b084d9dSBodo Stroesser dpi, udev->name); 1835b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1836b6df4b79SXiubo Li 18378b084d9dSBodo Stroesser return NULL; 1838b6df4b79SXiubo Li } 1839b6df4b79SXiubo Li 18408f33bb24SBodo Stroesser static void tcmu_vma_open(struct vm_area_struct *vma) 18418f33bb24SBodo Stroesser { 18428f33bb24SBodo Stroesser struct tcmu_dev *udev = vma->vm_private_data; 18438f33bb24SBodo Stroesser 18448f33bb24SBodo Stroesser pr_debug("vma_open\n"); 18458f33bb24SBodo Stroesser 18468f33bb24SBodo Stroesser kref_get(&udev->kref); 18478f33bb24SBodo Stroesser } 18488f33bb24SBodo Stroesser 18498f33bb24SBodo Stroesser static void tcmu_vma_close(struct vm_area_struct *vma) 18508f33bb24SBodo Stroesser { 18518f33bb24SBodo Stroesser struct tcmu_dev *udev = vma->vm_private_data; 18528f33bb24SBodo Stroesser 18538f33bb24SBodo Stroesser pr_debug("vma_close\n"); 18548f33bb24SBodo Stroesser 18558f33bb24SBodo Stroesser /* release ref from tcmu_vma_open */ 18568f33bb24SBodo Stroesser kref_put(&udev->kref, tcmu_dev_kref_release); 18578f33bb24SBodo Stroesser } 18588f33bb24SBodo Stroesser 185969589c9bSSouptick Joarder static vm_fault_t tcmu_vma_fault(struct vm_fault *vmf) 18607c9e7a6fSAndy Grover { 186111bac800SDave Jiang struct tcmu_dev *udev = vmf->vma->vm_private_data; 18627c9e7a6fSAndy Grover struct uio_info *info = &udev->uio_info; 18637c9e7a6fSAndy Grover struct page *page; 18647c9e7a6fSAndy Grover unsigned long offset; 18657c9e7a6fSAndy Grover void *addr; 18667c9e7a6fSAndy Grover 186711bac800SDave Jiang int mi = tcmu_find_mem_index(vmf->vma); 18687c9e7a6fSAndy Grover if (mi < 0) 18697c9e7a6fSAndy Grover return VM_FAULT_SIGBUS; 18707c9e7a6fSAndy Grover 18717c9e7a6fSAndy Grover /* 18727c9e7a6fSAndy Grover * We need to subtract mi because userspace uses offset = N*PAGE_SIZE 18737c9e7a6fSAndy Grover * to use mem[N]. 18747c9e7a6fSAndy Grover */ 18757c9e7a6fSAndy Grover offset = (vmf->pgoff - mi) << PAGE_SHIFT; 18767c9e7a6fSAndy Grover 1877141685a3SXiubo Li if (offset < udev->data_off) { 1878141685a3SXiubo Li /* For the vmalloc()ed cmd area pages */ 18797c9e7a6fSAndy Grover addr = (void *)(unsigned long)info->mem[mi].addr + offset; 18807c9e7a6fSAndy Grover page = vmalloc_to_page(addr); 1881*a6968f7aSXiaoguang Wang get_page(page); 1882141685a3SXiubo Li } else { 18838b084d9dSBodo Stroesser uint32_t dpi; 1884141685a3SXiubo Li 1885b6df4b79SXiubo Li /* For the dynamically growing data area pages */ 18868b084d9dSBodo Stroesser dpi = (offset - udev->data_off) / PAGE_SIZE; 18878b084d9dSBodo Stroesser page = tcmu_try_get_data_page(udev, dpi); 1888b6df4b79SXiubo Li if (!page) 1889c1c390baSMike Christie return VM_FAULT_SIGBUS; 1890141685a3SXiubo Li } 1891141685a3SXiubo Li 18927c9e7a6fSAndy Grover vmf->page = page; 18937c9e7a6fSAndy Grover return 0; 18947c9e7a6fSAndy Grover } 18957c9e7a6fSAndy Grover 18967c9e7a6fSAndy Grover static const struct vm_operations_struct tcmu_vm_ops = { 18978f33bb24SBodo Stroesser .open = tcmu_vma_open, 18988f33bb24SBodo Stroesser .close = tcmu_vma_close, 18997c9e7a6fSAndy Grover .fault = tcmu_vma_fault, 19007c9e7a6fSAndy Grover }; 19017c9e7a6fSAndy Grover 19027c9e7a6fSAndy Grover static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma) 19037c9e7a6fSAndy Grover { 19047c9e7a6fSAndy Grover struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 19057c9e7a6fSAndy Grover 19067c9e7a6fSAndy Grover vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 19077c9e7a6fSAndy Grover vma->vm_ops = &tcmu_vm_ops; 19087c9e7a6fSAndy Grover 19097c9e7a6fSAndy Grover vma->vm_private_data = udev; 19107c9e7a6fSAndy Grover 19117c9e7a6fSAndy Grover /* Ensure the mmap is exactly the right size */ 1912ecddbb7eSBodo Stroesser if (vma_pages(vma) != udev->mmap_pages) 19137c9e7a6fSAndy Grover return -EINVAL; 19147c9e7a6fSAndy Grover 19158f33bb24SBodo Stroesser tcmu_vma_open(vma); 19168f33bb24SBodo Stroesser 19177c9e7a6fSAndy Grover return 0; 19187c9e7a6fSAndy Grover } 19197c9e7a6fSAndy Grover 19207c9e7a6fSAndy Grover static int tcmu_open(struct uio_info *info, struct inode *inode) 19217c9e7a6fSAndy Grover { 19227c9e7a6fSAndy Grover struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 19237c9e7a6fSAndy Grover 19247c9e7a6fSAndy Grover /* O_EXCL not supported for char devs, so fake it? */ 19257c9e7a6fSAndy Grover if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags)) 19267c9e7a6fSAndy Grover return -EBUSY; 19277c9e7a6fSAndy Grover 1928b6df4b79SXiubo Li udev->inode = inode; 1929b6df4b79SXiubo Li 19307c9e7a6fSAndy Grover pr_debug("open\n"); 19317c9e7a6fSAndy Grover 19327c9e7a6fSAndy Grover return 0; 19337c9e7a6fSAndy Grover } 19347c9e7a6fSAndy Grover 19357c9e7a6fSAndy Grover static int tcmu_release(struct uio_info *info, struct inode *inode) 19367c9e7a6fSAndy Grover { 19377c9e7a6fSAndy Grover struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 1938018c1491SBodo Stroesser struct tcmu_cmd *cmd; 1939018c1491SBodo Stroesser unsigned long i; 1940018c1491SBodo Stroesser bool freed = false; 1941018c1491SBodo Stroesser 1942018c1491SBodo Stroesser mutex_lock(&udev->cmdr_lock); 1943018c1491SBodo Stroesser 1944018c1491SBodo Stroesser xa_for_each(&udev->commands, i, cmd) { 1945018c1491SBodo Stroesser /* Cmds with KEEP_BUF set are no longer on the ring, but 1946018c1491SBodo Stroesser * userspace still holds the data buffer. If userspace closes 1947018c1491SBodo Stroesser * we implicitly free these cmds and buffers, since after new 1948018c1491SBodo Stroesser * open the (new ?) userspace cannot find the cmd in the ring 1949018c1491SBodo Stroesser * and thus never will release the buffer by writing cmd_id to 1950018c1491SBodo Stroesser * free_kept_buf action attribute. 1951018c1491SBodo Stroesser */ 1952018c1491SBodo Stroesser if (!test_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags)) 1953018c1491SBodo Stroesser continue; 1954018c1491SBodo Stroesser pr_debug("removing KEEP_BUF cmd %u on dev %s from ring\n", 1955018c1491SBodo Stroesser cmd->cmd_id, udev->name); 1956018c1491SBodo Stroesser freed = true; 1957018c1491SBodo Stroesser 1958018c1491SBodo Stroesser xa_erase(&udev->commands, i); 1959018c1491SBodo Stroesser tcmu_cmd_free_data(cmd, cmd->dbi_cnt); 1960018c1491SBodo Stroesser tcmu_free_cmd(cmd); 1961018c1491SBodo Stroesser } 1962018c1491SBodo Stroesser /* 1963018c1491SBodo Stroesser * We only freed data space, not ring space. Therefore we dont call 1964018c1491SBodo Stroesser * run_tmr_queue, but call run_qfull_queue if tmr_list is empty. 1965018c1491SBodo Stroesser */ 1966018c1491SBodo Stroesser if (freed && list_empty(&udev->tmr_queue)) 1967018c1491SBodo Stroesser run_qfull_queue(udev, false); 1968018c1491SBodo Stroesser 1969018c1491SBodo Stroesser mutex_unlock(&udev->cmdr_lock); 19707c9e7a6fSAndy Grover 19717c9e7a6fSAndy Grover clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags); 19727c9e7a6fSAndy Grover 19737c9e7a6fSAndy Grover pr_debug("close\n"); 19748f33bb24SBodo Stroesser 19757c9e7a6fSAndy Grover return 0; 19767c9e7a6fSAndy Grover } 19777c9e7a6fSAndy Grover 19789de3a1efSMike Christie static int tcmu_init_genl_cmd_reply(struct tcmu_dev *udev, int cmd) 1979b3af66e2SMike Christie { 1980b3af66e2SMike Christie struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; 1981b3af66e2SMike Christie 1982b3af66e2SMike Christie if (!tcmu_kern_cmd_reply_supported) 19839de3a1efSMike Christie return 0; 1984b849b456SKenjiro Nakayama 1985b849b456SKenjiro Nakayama if (udev->nl_reply_supported <= 0) 19869de3a1efSMike Christie return 0; 1987b849b456SKenjiro Nakayama 19883228691fSMike Christie mutex_lock(&tcmu_nl_cmd_mutex); 1989b3af66e2SMike Christie 1990bdaeedc1SMike Christie if (tcmu_netlink_blocked) { 1991bdaeedc1SMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 1992bdaeedc1SMike Christie pr_warn("Failing nl cmd %d on %s. Interface is blocked.\n", cmd, 1993bdaeedc1SMike Christie udev->name); 1994bdaeedc1SMike Christie return -EAGAIN; 1995bdaeedc1SMike Christie } 1996b3af66e2SMike Christie 1997b3af66e2SMike Christie if (nl_cmd->cmd != TCMU_CMD_UNSPEC) { 19983228691fSMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 19999de3a1efSMike Christie pr_warn("netlink cmd %d already executing on %s\n", 20009de3a1efSMike Christie nl_cmd->cmd, udev->name); 20019de3a1efSMike Christie return -EBUSY; 2002b3af66e2SMike Christie } 2003b3af66e2SMike Christie 2004b3af66e2SMike Christie memset(nl_cmd, 0, sizeof(*nl_cmd)); 2005b3af66e2SMike Christie nl_cmd->cmd = cmd; 20063228691fSMike Christie nl_cmd->udev = udev; 2007b3af66e2SMike Christie init_completion(&nl_cmd->complete); 20083228691fSMike Christie INIT_LIST_HEAD(&nl_cmd->nl_list); 2009b3af66e2SMike Christie 20103228691fSMike Christie list_add_tail(&nl_cmd->nl_list, &tcmu_nl_cmd_list); 20113228691fSMike Christie 20123228691fSMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 20139de3a1efSMike Christie return 0; 2014b3af66e2SMike Christie } 2015b3af66e2SMike Christie 20167d894862SLi Zhong static void tcmu_destroy_genl_cmd_reply(struct tcmu_dev *udev) 20177d894862SLi Zhong { 20187d894862SLi Zhong struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; 20197d894862SLi Zhong 20207d894862SLi Zhong if (!tcmu_kern_cmd_reply_supported) 20217d894862SLi Zhong return; 20227d894862SLi Zhong 20237d894862SLi Zhong if (udev->nl_reply_supported <= 0) 20247d894862SLi Zhong return; 20257d894862SLi Zhong 20267d894862SLi Zhong mutex_lock(&tcmu_nl_cmd_mutex); 20277d894862SLi Zhong 20287d894862SLi Zhong list_del(&nl_cmd->nl_list); 20297d894862SLi Zhong memset(nl_cmd, 0, sizeof(*nl_cmd)); 20307d894862SLi Zhong 20317d894862SLi Zhong mutex_unlock(&tcmu_nl_cmd_mutex); 20327d894862SLi Zhong } 20337d894862SLi Zhong 2034b3af66e2SMike Christie static int tcmu_wait_genl_cmd_reply(struct tcmu_dev *udev) 2035b3af66e2SMike Christie { 2036b3af66e2SMike Christie struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; 2037b3af66e2SMike Christie int ret; 2038b3af66e2SMike Christie 2039b3af66e2SMike Christie if (!tcmu_kern_cmd_reply_supported) 2040b3af66e2SMike Christie return 0; 2041b3af66e2SMike Christie 2042b849b456SKenjiro Nakayama if (udev->nl_reply_supported <= 0) 2043b849b456SKenjiro Nakayama return 0; 2044b849b456SKenjiro Nakayama 2045b3af66e2SMike Christie pr_debug("sleeping for nl reply\n"); 2046b3af66e2SMike Christie wait_for_completion(&nl_cmd->complete); 2047b3af66e2SMike Christie 20483228691fSMike Christie mutex_lock(&tcmu_nl_cmd_mutex); 2049b3af66e2SMike Christie nl_cmd->cmd = TCMU_CMD_UNSPEC; 2050b3af66e2SMike Christie ret = nl_cmd->status; 20513228691fSMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 2052b3af66e2SMike Christie 205385fae482SLuis de Bethencourt return ret; 2054b3af66e2SMike Christie } 2055b3af66e2SMike Christie 20560e5aee39SZhu Lingshan static int tcmu_netlink_event_init(struct tcmu_dev *udev, 20570e5aee39SZhu Lingshan enum tcmu_genl_cmd cmd, 20580e5aee39SZhu Lingshan struct sk_buff **buf, void **hdr) 20597c9e7a6fSAndy Grover { 20607c9e7a6fSAndy Grover struct sk_buff *skb; 20617c9e7a6fSAndy Grover void *msg_header; 20626e14eab9SNicholas Bellinger int ret = -ENOMEM; 20637c9e7a6fSAndy Grover 20647c9e7a6fSAndy Grover skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 20657c9e7a6fSAndy Grover if (!skb) 20666e14eab9SNicholas Bellinger return ret; 20677c9e7a6fSAndy Grover 20687c9e7a6fSAndy Grover msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd); 20696e14eab9SNicholas Bellinger if (!msg_header) 20706e14eab9SNicholas Bellinger goto free_skb; 20717c9e7a6fSAndy Grover 2072b3af66e2SMike Christie ret = nla_put_string(skb, TCMU_ATTR_DEVICE, udev->uio_info.name); 20736e14eab9SNicholas Bellinger if (ret < 0) 20746e14eab9SNicholas Bellinger goto free_skb; 20757c9e7a6fSAndy Grover 2076b3af66e2SMike Christie ret = nla_put_u32(skb, TCMU_ATTR_MINOR, udev->uio_info.uio_dev->minor); 2077b3af66e2SMike Christie if (ret < 0) 2078b3af66e2SMike Christie goto free_skb; 2079b3af66e2SMike Christie 2080b3af66e2SMike Christie ret = nla_put_u32(skb, TCMU_ATTR_DEVICE_ID, udev->se_dev.dev_index); 20816e14eab9SNicholas Bellinger if (ret < 0) 20826e14eab9SNicholas Bellinger goto free_skb; 20837c9e7a6fSAndy Grover 20840e5aee39SZhu Lingshan *buf = skb; 20850e5aee39SZhu Lingshan *hdr = msg_header; 20860e5aee39SZhu Lingshan return ret; 20870e5aee39SZhu Lingshan 20880e5aee39SZhu Lingshan free_skb: 20890e5aee39SZhu Lingshan nlmsg_free(skb); 20900e5aee39SZhu Lingshan return ret; 20912d76443eSMike Christie } 20922d76443eSMike Christie 20930e5aee39SZhu Lingshan static int tcmu_netlink_event_send(struct tcmu_dev *udev, 20940e5aee39SZhu Lingshan enum tcmu_genl_cmd cmd, 209506add777SMike Christie struct sk_buff *skb, void *msg_header) 20960e5aee39SZhu Lingshan { 209706add777SMike Christie int ret; 20988a45885cSBryant G. Ly 2099053c095aSJohannes Berg genlmsg_end(skb, msg_header); 21007c9e7a6fSAndy Grover 21019de3a1efSMike Christie ret = tcmu_init_genl_cmd_reply(udev, cmd); 21029de3a1efSMike Christie if (ret) { 21039de3a1efSMike Christie nlmsg_free(skb); 21049de3a1efSMike Christie return ret; 21059de3a1efSMike Christie } 2106b3af66e2SMike Christie 210720c08b36SSheng Yang ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0, 21087c9e7a6fSAndy Grover TCMU_MCGRP_CONFIG, GFP_KERNEL); 21092ff717cdSCathy Avery 21102ff717cdSCathy Avery /* Wait during an add as the listener may not be up yet */ 21112ff717cdSCathy Avery if (ret == 0 || 21122ff717cdSCathy Avery (ret == -ESRCH && cmd == TCMU_CMD_ADDED_DEVICE)) 21132ff717cdSCathy Avery return tcmu_wait_genl_cmd_reply(udev); 21147d894862SLi Zhong else 21157d894862SLi Zhong tcmu_destroy_genl_cmd_reply(udev); 21162ff717cdSCathy Avery 21170e5aee39SZhu Lingshan return ret; 21180e5aee39SZhu Lingshan } 21197c9e7a6fSAndy Grover 2120e0c240acSZhu Lingshan static int tcmu_send_dev_add_event(struct tcmu_dev *udev) 2121e0c240acSZhu Lingshan { 2122e0c240acSZhu Lingshan struct sk_buff *skb = NULL; 2123e0c240acSZhu Lingshan void *msg_header = NULL; 2124e0c240acSZhu Lingshan int ret = 0; 2125e0c240acSZhu Lingshan 2126e0c240acSZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_ADDED_DEVICE, &skb, 2127e0c240acSZhu Lingshan &msg_header); 2128e0c240acSZhu Lingshan if (ret < 0) 21297c9e7a6fSAndy Grover return ret; 213006add777SMike Christie return tcmu_netlink_event_send(udev, TCMU_CMD_ADDED_DEVICE, skb, 213106add777SMike Christie msg_header); 2132e0c240acSZhu Lingshan } 2133e0c240acSZhu Lingshan 2134f892bd8eSZhu Lingshan static int tcmu_send_dev_remove_event(struct tcmu_dev *udev) 2135f892bd8eSZhu Lingshan { 2136f892bd8eSZhu Lingshan struct sk_buff *skb = NULL; 2137f892bd8eSZhu Lingshan void *msg_header = NULL; 2138f892bd8eSZhu Lingshan int ret = 0; 2139f892bd8eSZhu Lingshan 2140f892bd8eSZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_REMOVED_DEVICE, 2141f892bd8eSZhu Lingshan &skb, &msg_header); 2142f892bd8eSZhu Lingshan if (ret < 0) 21436e14eab9SNicholas Bellinger return ret; 2144f892bd8eSZhu Lingshan return tcmu_netlink_event_send(udev, TCMU_CMD_REMOVED_DEVICE, 214506add777SMike Christie skb, msg_header); 21467c9e7a6fSAndy Grover } 21477c9e7a6fSAndy Grover 2148de8c5221SBryant G. Ly static int tcmu_update_uio_info(struct tcmu_dev *udev) 21497c9e7a6fSAndy Grover { 21507c9e7a6fSAndy Grover struct tcmu_hba *hba = udev->hba->hba_ptr; 21517c9e7a6fSAndy Grover struct uio_info *info; 21527c9e7a6fSAndy Grover char *str; 21537c9e7a6fSAndy Grover 21547c9e7a6fSAndy Grover info = &udev->uio_info; 215522c2f35fSChristophe JAILLET 215622c2f35fSChristophe JAILLET if (udev->dev_config[0]) 215722c2f35fSChristophe JAILLET str = kasprintf(GFP_KERNEL, "tcm-user/%u/%s/%s", hba->host_id, 215822c2f35fSChristophe JAILLET udev->name, udev->dev_config); 215922c2f35fSChristophe JAILLET else 216022c2f35fSChristophe JAILLET str = kasprintf(GFP_KERNEL, "tcm-user/%u/%s", hba->host_id, 216122c2f35fSChristophe JAILLET udev->name); 21627c9e7a6fSAndy Grover if (!str) 21637c9e7a6fSAndy Grover return -ENOMEM; 21647c9e7a6fSAndy Grover 2165ededd039SBryant G. Ly /* If the old string exists, free it */ 2166ededd039SBryant G. Ly kfree(info->name); 21677c9e7a6fSAndy Grover info->name = str; 21687c9e7a6fSAndy Grover 2169de8c5221SBryant G. Ly return 0; 2170de8c5221SBryant G. Ly } 2171de8c5221SBryant G. Ly 2172de8c5221SBryant G. Ly static int tcmu_configure_device(struct se_device *dev) 2173de8c5221SBryant G. Ly { 2174de8c5221SBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(dev); 2175de8c5221SBryant G. Ly struct uio_info *info; 2176de8c5221SBryant G. Ly struct tcmu_mailbox *mb; 2177f5ce815fSBodo Stroesser size_t data_size; 2178de8c5221SBryant G. Ly int ret = 0; 2179de8c5221SBryant G. Ly 2180de8c5221SBryant G. Ly ret = tcmu_update_uio_info(udev); 2181de8c5221SBryant G. Ly if (ret) 2182de8c5221SBryant G. Ly return ret; 2183de8c5221SBryant G. Ly 2184de8c5221SBryant G. Ly info = &udev->uio_info; 2185de8c5221SBryant G. Ly 2186c97840c8SMike Christie mutex_lock(&udev->cmdr_lock); 218798effe47SAndy Shevchenko udev->data_bitmap = bitmap_zalloc(udev->max_blocks, GFP_KERNEL); 2188c97840c8SMike Christie mutex_unlock(&udev->cmdr_lock); 2189a24e7917SWei Yongjun if (!udev->data_bitmap) { 2190a24e7917SWei Yongjun ret = -ENOMEM; 219180eb8761SMike Christie goto err_bitmap_alloc; 2192a24e7917SWei Yongjun } 219380eb8761SMike Christie 2194c7ede4f0SGuixin Liu mb = vzalloc(udev->cmdr_size + CMDR_OFF); 2195ecddbb7eSBodo Stroesser if (!mb) { 21967c9e7a6fSAndy Grover ret = -ENOMEM; 21977c9e7a6fSAndy Grover goto err_vzalloc; 21987c9e7a6fSAndy Grover } 21997c9e7a6fSAndy Grover 22007c9e7a6fSAndy Grover /* mailbox fits in first part of CMDR space */ 2201ecddbb7eSBodo Stroesser udev->mb_addr = mb; 2202ecddbb7eSBodo Stroesser udev->cmdr = (void *)mb + CMDR_OFF; 2203c7ede4f0SGuixin Liu udev->data_off = udev->cmdr_size + CMDR_OFF; 2204f5ce815fSBodo Stroesser data_size = TCMU_MBS_TO_PAGES(udev->data_area_mb) << PAGE_SHIFT; 2205c7ede4f0SGuixin Liu udev->mmap_pages = (data_size + udev->cmdr_size + CMDR_OFF) >> PAGE_SHIFT; 2206e719afdcSBodo Stroesser udev->data_blk_size = udev->data_pages_per_blk * PAGE_SIZE; 2207b6df4b79SXiubo Li udev->dbi_thresh = 0; /* Default in Idle state */ 22087c9e7a6fSAndy Grover 2209141685a3SXiubo Li /* Initialise the mailbox of the ring buffer */ 22100ad46af8SAndy Grover mb->version = TCMU_MAILBOX_VERSION; 2211bc2d214aSBodo Stroesser mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC | 2212bc2d214aSBodo Stroesser TCMU_MAILBOX_FLAG_CAP_READ_LEN | 2213018c1491SBodo Stroesser TCMU_MAILBOX_FLAG_CAP_TMR | 2214018c1491SBodo Stroesser TCMU_MAILBOX_FLAG_CAP_KEEP_BUF; 22157c9e7a6fSAndy Grover mb->cmdr_off = CMDR_OFF; 22167c9e7a6fSAndy Grover mb->cmdr_size = udev->cmdr_size; 22177c9e7a6fSAndy Grover 22187c9e7a6fSAndy Grover WARN_ON(!PAGE_ALIGNED(udev->data_off)); 2219f5ce815fSBodo Stroesser WARN_ON(data_size % PAGE_SIZE); 22207c9e7a6fSAndy Grover 2221ac64a2ceSDavid Disseldorp info->version = __stringify(TCMU_MAILBOX_VERSION); 22227c9e7a6fSAndy Grover 22237c9e7a6fSAndy Grover info->mem[0].name = "tcm-user command & data buffer"; 22240633e123SArnd Bergmann info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr; 2225c7ede4f0SGuixin Liu info->mem[0].size = data_size + udev->cmdr_size + CMDR_OFF; 2226141685a3SXiubo Li info->mem[0].memtype = UIO_MEM_NONE; 22277c9e7a6fSAndy Grover 22287c9e7a6fSAndy Grover info->irqcontrol = tcmu_irqcontrol; 22297c9e7a6fSAndy Grover info->irq = UIO_IRQ_CUSTOM; 22307c9e7a6fSAndy Grover 22317c9e7a6fSAndy Grover info->mmap = tcmu_mmap; 22327c9e7a6fSAndy Grover info->open = tcmu_open; 22337c9e7a6fSAndy Grover info->release = tcmu_release; 22347c9e7a6fSAndy Grover 22357c9e7a6fSAndy Grover ret = uio_register_device(tcmu_root_device, info); 22367c9e7a6fSAndy Grover if (ret) 22377c9e7a6fSAndy Grover goto err_register; 22387c9e7a6fSAndy Grover 223981ee28deSSheng Yang /* User can set hw_block_size before enable the device */ 224081ee28deSSheng Yang if (dev->dev_attrib.hw_block_size == 0) 22417c9e7a6fSAndy Grover dev->dev_attrib.hw_block_size = 512; 224281ee28deSSheng Yang /* Other attributes can be configured in userspace */ 22433abaa2bfSMike Christie if (!dev->dev_attrib.hw_max_sectors) 22447c9e7a6fSAndy Grover dev->dev_attrib.hw_max_sectors = 128; 22459a8bb606SBryant G. Ly if (!dev->dev_attrib.emulate_write_cache) 22469a8bb606SBryant G. Ly dev->dev_attrib.emulate_write_cache = 0; 22477c9e7a6fSAndy Grover dev->dev_attrib.hw_queue_depth = 128; 22487c9e7a6fSAndy Grover 2249b849b456SKenjiro Nakayama /* If user didn't explicitly disable netlink reply support, use 2250b849b456SKenjiro Nakayama * module scope setting. 2251b849b456SKenjiro Nakayama */ 2252b849b456SKenjiro Nakayama if (udev->nl_reply_supported >= 0) 2253b849b456SKenjiro Nakayama udev->nl_reply_supported = tcmu_kern_cmd_reply_supported; 2254b849b456SKenjiro Nakayama 2255f3cdbe39SMike Christie /* 2256f3cdbe39SMike Christie * Get a ref incase userspace does a close on the uio device before 2257f3cdbe39SMike Christie * LIO has initiated tcmu_free_device. 2258f3cdbe39SMike Christie */ 2259f3cdbe39SMike Christie kref_get(&udev->kref); 2260f3cdbe39SMike Christie 2261e0c240acSZhu Lingshan ret = tcmu_send_dev_add_event(udev); 22627c9e7a6fSAndy Grover if (ret) 22637c9e7a6fSAndy Grover goto err_netlink; 22647c9e7a6fSAndy Grover 2265b6df4b79SXiubo Li mutex_lock(&root_udev_mutex); 2266b6df4b79SXiubo Li list_add(&udev->node, &root_udev); 2267b6df4b79SXiubo Li mutex_unlock(&root_udev_mutex); 2268b6df4b79SXiubo Li 22697c9e7a6fSAndy Grover return 0; 22707c9e7a6fSAndy Grover 22717c9e7a6fSAndy Grover err_netlink: 2272f3cdbe39SMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 22737c9e7a6fSAndy Grover uio_unregister_device(&udev->uio_info); 22747c9e7a6fSAndy Grover err_register: 22757c9e7a6fSAndy Grover vfree(udev->mb_addr); 2276c22adc0bSXiubo Li udev->mb_addr = NULL; 22777c9e7a6fSAndy Grover err_vzalloc: 227898effe47SAndy Shevchenko bitmap_free(udev->data_bitmap); 227980eb8761SMike Christie udev->data_bitmap = NULL; 228080eb8761SMike Christie err_bitmap_alloc: 22817c9e7a6fSAndy Grover kfree(info->name); 2282f3cdbe39SMike Christie info->name = NULL; 22837c9e7a6fSAndy Grover 22847c9e7a6fSAndy Grover return ret; 22857c9e7a6fSAndy Grover } 22867c9e7a6fSAndy Grover 22877c9e7a6fSAndy Grover static void tcmu_free_device(struct se_device *dev) 22887c9e7a6fSAndy Grover { 22897c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 229092634706SMike Christie 229192634706SMike Christie /* release ref from init */ 229292634706SMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 229392634706SMike Christie } 229492634706SMike Christie 229592634706SMike Christie static void tcmu_destroy_device(struct se_device *dev) 229692634706SMike Christie { 229792634706SMike Christie struct tcmu_dev *udev = TCMU_DEV(dev); 22987c9e7a6fSAndy Grover 22999103575aSMike Christie del_timer_sync(&udev->cmd_timer); 23009103575aSMike Christie del_timer_sync(&udev->qfull_timer); 23017c9e7a6fSAndy Grover 2302b6df4b79SXiubo Li mutex_lock(&root_udev_mutex); 2303b6df4b79SXiubo Li list_del(&udev->node); 2304b6df4b79SXiubo Li mutex_unlock(&root_udev_mutex); 2305b6df4b79SXiubo Li 2306f892bd8eSZhu Lingshan tcmu_send_dev_remove_event(udev); 23077c9e7a6fSAndy Grover 23087c9e7a6fSAndy Grover uio_unregister_device(&udev->uio_info); 23099260695dSMike Christie 23109260695dSMike Christie /* release ref from configure */ 23119260695dSMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 23127c9e7a6fSAndy Grover } 23137c9e7a6fSAndy Grover 2314892782caSMike Christie static void tcmu_unblock_dev(struct tcmu_dev *udev) 2315892782caSMike Christie { 2316892782caSMike Christie mutex_lock(&udev->cmdr_lock); 2317892782caSMike Christie clear_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags); 2318892782caSMike Christie mutex_unlock(&udev->cmdr_lock); 2319892782caSMike Christie } 2320892782caSMike Christie 2321892782caSMike Christie static void tcmu_block_dev(struct tcmu_dev *udev) 2322892782caSMike Christie { 2323892782caSMike Christie mutex_lock(&udev->cmdr_lock); 2324892782caSMike Christie 2325892782caSMike Christie if (test_and_set_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) 2326892782caSMike Christie goto unlock; 2327892782caSMike Christie 2328892782caSMike Christie /* complete IO that has executed successfully */ 2329892782caSMike Christie tcmu_handle_completions(udev); 2330892782caSMike Christie /* fail IO waiting to be queued */ 2331a94a2572SXiubo Li run_qfull_queue(udev, true); 2332892782caSMike Christie 2333892782caSMike Christie unlock: 2334892782caSMike Christie mutex_unlock(&udev->cmdr_lock); 2335892782caSMike Christie } 2336892782caSMike Christie 2337892782caSMike Christie static void tcmu_reset_ring(struct tcmu_dev *udev, u8 err_level) 2338892782caSMike Christie { 2339892782caSMike Christie struct tcmu_mailbox *mb; 2340892782caSMike Christie struct tcmu_cmd *cmd; 2341d3cbb743SBodo Stroesser unsigned long i; 2342892782caSMike Christie 2343892782caSMike Christie mutex_lock(&udev->cmdr_lock); 2344892782caSMike Christie 2345d3cbb743SBodo Stroesser xa_for_each(&udev->commands, i, cmd) { 2346018c1491SBodo Stroesser pr_debug("removing cmd %u on dev %s from ring %s\n", 2347892782caSMike Christie cmd->cmd_id, udev->name, 2348018c1491SBodo Stroesser test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags) ? 2349018c1491SBodo Stroesser "(is expired)" : 2350018c1491SBodo Stroesser (test_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags) ? 2351018c1491SBodo Stroesser "(is keep buffer)" : "")); 2352892782caSMike Christie 2353d3cbb743SBodo Stroesser xa_erase(&udev->commands, i); 2354018c1491SBodo Stroesser if (!test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags) && 2355018c1491SBodo Stroesser !test_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags)) { 2356a86a7586SDmitry Fomichev WARN_ON(!cmd->se_cmd); 2357a94a2572SXiubo Li list_del_init(&cmd->queue_entry); 2358780e1384SShin'ichiro Kawasaki cmd->se_cmd->priv = NULL; 2359892782caSMike Christie if (err_level == 1) { 2360892782caSMike Christie /* 2361892782caSMike Christie * Userspace was not able to start the 2362892782caSMike Christie * command or it is retryable. 2363892782caSMike Christie */ 2364892782caSMike Christie target_complete_cmd(cmd->se_cmd, SAM_STAT_BUSY); 2365892782caSMike Christie } else { 2366892782caSMike Christie /* hard failure */ 2367892782caSMike Christie target_complete_cmd(cmd->se_cmd, 2368892782caSMike Christie SAM_STAT_CHECK_CONDITION); 2369892782caSMike Christie } 2370892782caSMike Christie } 2371892782caSMike Christie tcmu_cmd_free_data(cmd, cmd->dbi_cnt); 2372892782caSMike Christie tcmu_free_cmd(cmd); 2373892782caSMike Christie } 2374892782caSMike Christie 2375892782caSMike Christie mb = udev->mb_addr; 2376892782caSMike Christie tcmu_flush_dcache_range(mb, sizeof(*mb)); 2377892782caSMike Christie pr_debug("mb last %u head %u tail %u\n", udev->cmdr_last_cleaned, 2378892782caSMike Christie mb->cmd_tail, mb->cmd_head); 2379892782caSMike Christie 2380892782caSMike Christie udev->cmdr_last_cleaned = 0; 2381892782caSMike Christie mb->cmd_tail = 0; 2382892782caSMike Christie mb->cmd_head = 0; 2383892782caSMike Christie tcmu_flush_dcache_range(mb, sizeof(*mb)); 2384066f79a5SBodo Stroesser clear_bit(TCMU_DEV_BIT_BROKEN, &udev->flags); 2385892782caSMike Christie 2386892782caSMike Christie del_timer(&udev->cmd_timer); 2387892782caSMike Christie 2388bc2d214aSBodo Stroesser /* 2389bc2d214aSBodo Stroesser * ring is empty and qfull queue never contains aborted commands. 2390bc2d214aSBodo Stroesser * So TMRs in tmr queue do not contain relevant cmd_ids. 2391bc2d214aSBodo Stroesser * After a ring reset userspace should do a fresh start, so 2392bc2d214aSBodo Stroesser * even LUN RESET message is no longer relevant. 2393bc2d214aSBodo Stroesser * Therefore remove all TMRs from qfull queue 2394bc2d214aSBodo Stroesser */ 2395bc2d214aSBodo Stroesser tcmu_remove_all_queued_tmr(udev); 2396bc2d214aSBodo Stroesser 239761fb2482SBodo Stroesser run_qfull_queue(udev, false); 239861fb2482SBodo Stroesser 2399892782caSMike Christie mutex_unlock(&udev->cmdr_lock); 2400892782caSMike Christie } 2401892782caSMike Christie 24027c9e7a6fSAndy Grover enum { 24033abaa2bfSMike Christie Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors, 240408976cb5SBodo Stroesser Opt_nl_reply_supported, Opt_max_data_area_mb, Opt_data_pages_per_blk, 2405c7ede4f0SGuixin Liu Opt_cmd_ring_size_mb, Opt_err, 24067c9e7a6fSAndy Grover }; 24077c9e7a6fSAndy Grover 24087c9e7a6fSAndy Grover static match_table_t tokens = { 24097c9e7a6fSAndy Grover {Opt_dev_config, "dev_config=%s"}, 24100e0d7526SMike Christie {Opt_dev_size, "dev_size=%s"}, 2411b60cb1f8SMike Christie {Opt_hw_block_size, "hw_block_size=%d"}, 2412b60cb1f8SMike Christie {Opt_hw_max_sectors, "hw_max_sectors=%d"}, 2413b849b456SKenjiro Nakayama {Opt_nl_reply_supported, "nl_reply_supported=%d"}, 2414c97840c8SMike Christie {Opt_max_data_area_mb, "max_data_area_mb=%d"}, 241508976cb5SBodo Stroesser {Opt_data_pages_per_blk, "data_pages_per_blk=%d"}, 2416c7ede4f0SGuixin Liu {Opt_cmd_ring_size_mb, "cmd_ring_size_mb=%d"}, 24177c9e7a6fSAndy Grover {Opt_err, NULL} 24187c9e7a6fSAndy Grover }; 24197c9e7a6fSAndy Grover 24203abaa2bfSMike Christie static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib) 24213abaa2bfSMike Christie { 2422b60cb1f8SMike Christie int val, ret; 24233abaa2bfSMike Christie 2424b60cb1f8SMike Christie ret = match_int(arg, &val); 24253abaa2bfSMike Christie if (ret < 0) { 2426b60cb1f8SMike Christie pr_err("match_int() failed for dev attrib. Error %d.\n", 2427b60cb1f8SMike Christie ret); 24283abaa2bfSMike Christie return ret; 24293abaa2bfSMike Christie } 2430b60cb1f8SMike Christie 2431b60cb1f8SMike Christie if (val <= 0) { 2432b60cb1f8SMike Christie pr_err("Invalid dev attrib value %d. Must be greater than zero.\n", 2433b60cb1f8SMike Christie val); 24343abaa2bfSMike Christie return -EINVAL; 24353abaa2bfSMike Christie } 2436b60cb1f8SMike Christie *dev_attrib = val; 24373abaa2bfSMike Christie return 0; 24383abaa2bfSMike Christie } 24393abaa2bfSMike Christie 2440c97840c8SMike Christie static int tcmu_set_max_blocks_param(struct tcmu_dev *udev, substring_t *arg) 2441c97840c8SMike Christie { 2442f5ce815fSBodo Stroesser int val, ret; 2443e719afdcSBodo Stroesser uint32_t pages_per_blk = udev->data_pages_per_blk; 2444c97840c8SMike Christie 2445c97840c8SMike Christie ret = match_int(arg, &val); 2446c97840c8SMike Christie if (ret < 0) { 2447c97840c8SMike Christie pr_err("match_int() failed for max_data_area_mb=. Error %d.\n", 2448c97840c8SMike Christie ret); 2449c97840c8SMike Christie return ret; 2450c97840c8SMike Christie } 2451f5ce815fSBodo Stroesser if (val <= 0) { 2452c97840c8SMike Christie pr_err("Invalid max_data_area %d.\n", val); 2453c97840c8SMike Christie return -EINVAL; 2454c97840c8SMike Christie } 2455f5ce815fSBodo Stroesser if (val > TCMU_PAGES_TO_MBS(tcmu_global_max_pages)) { 2456f5ce815fSBodo Stroesser pr_err("%d is too large. Adjusting max_data_area_mb to global limit of %u\n", 2457f5ce815fSBodo Stroesser val, TCMU_PAGES_TO_MBS(tcmu_global_max_pages)); 2458f5ce815fSBodo Stroesser val = TCMU_PAGES_TO_MBS(tcmu_global_max_pages); 2459f5ce815fSBodo Stroesser } 2460e719afdcSBodo Stroesser if (TCMU_MBS_TO_PAGES(val) < pages_per_blk) { 2461e719afdcSBodo Stroesser pr_err("Invalid max_data_area %d (%zu pages): smaller than data_pages_per_blk (%u pages).\n", 2462e719afdcSBodo Stroesser val, TCMU_MBS_TO_PAGES(val), pages_per_blk); 2463f5ce815fSBodo Stroesser return -EINVAL; 2464f5ce815fSBodo Stroesser } 2465c97840c8SMike Christie 2466c97840c8SMike Christie mutex_lock(&udev->cmdr_lock); 2467c97840c8SMike Christie if (udev->data_bitmap) { 2468c97840c8SMike Christie pr_err("Cannot set max_data_area_mb after it has been enabled.\n"); 2469c97840c8SMike Christie ret = -EINVAL; 2470c97840c8SMike Christie goto unlock; 2471c97840c8SMike Christie } 2472c97840c8SMike Christie 2473f5ce815fSBodo Stroesser udev->data_area_mb = val; 2474e719afdcSBodo Stroesser udev->max_blocks = TCMU_MBS_TO_PAGES(val) / pages_per_blk; 2475c97840c8SMike Christie 2476c97840c8SMike Christie unlock: 2477c97840c8SMike Christie mutex_unlock(&udev->cmdr_lock); 2478c97840c8SMike Christie return ret; 2479c97840c8SMike Christie } 2480c97840c8SMike Christie 248108976cb5SBodo Stroesser static int tcmu_set_data_pages_per_blk(struct tcmu_dev *udev, substring_t *arg) 248208976cb5SBodo Stroesser { 248308976cb5SBodo Stroesser int val, ret; 248408976cb5SBodo Stroesser 248508976cb5SBodo Stroesser ret = match_int(arg, &val); 248608976cb5SBodo Stroesser if (ret < 0) { 248708976cb5SBodo Stroesser pr_err("match_int() failed for data_pages_per_blk=. Error %d.\n", 248808976cb5SBodo Stroesser ret); 248908976cb5SBodo Stroesser return ret; 249008976cb5SBodo Stroesser } 249108976cb5SBodo Stroesser 249208976cb5SBodo Stroesser if (val > TCMU_MBS_TO_PAGES(udev->data_area_mb)) { 249308976cb5SBodo Stroesser pr_err("Invalid data_pages_per_blk %d: greater than max_data_area_mb %d -> %zd pages).\n", 249408976cb5SBodo Stroesser val, udev->data_area_mb, 249508976cb5SBodo Stroesser TCMU_MBS_TO_PAGES(udev->data_area_mb)); 249608976cb5SBodo Stroesser return -EINVAL; 249708976cb5SBodo Stroesser } 249808976cb5SBodo Stroesser 249908976cb5SBodo Stroesser mutex_lock(&udev->cmdr_lock); 250008976cb5SBodo Stroesser if (udev->data_bitmap) { 250108976cb5SBodo Stroesser pr_err("Cannot set data_pages_per_blk after it has been enabled.\n"); 250208976cb5SBodo Stroesser ret = -EINVAL; 250308976cb5SBodo Stroesser goto unlock; 250408976cb5SBodo Stroesser } 250508976cb5SBodo Stroesser 250608976cb5SBodo Stroesser udev->data_pages_per_blk = val; 250708976cb5SBodo Stroesser udev->max_blocks = TCMU_MBS_TO_PAGES(udev->data_area_mb) / val; 250808976cb5SBodo Stroesser 250908976cb5SBodo Stroesser unlock: 251008976cb5SBodo Stroesser mutex_unlock(&udev->cmdr_lock); 251108976cb5SBodo Stroesser return ret; 251208976cb5SBodo Stroesser } 251308976cb5SBodo Stroesser 2514c7ede4f0SGuixin Liu static int tcmu_set_cmd_ring_size(struct tcmu_dev *udev, substring_t *arg) 2515c7ede4f0SGuixin Liu { 2516c7ede4f0SGuixin Liu int val, ret; 2517c7ede4f0SGuixin Liu 2518c7ede4f0SGuixin Liu ret = match_int(arg, &val); 2519c7ede4f0SGuixin Liu if (ret < 0) { 2520c7ede4f0SGuixin Liu pr_err("match_int() failed for cmd_ring_size_mb=. Error %d.\n", 2521c7ede4f0SGuixin Liu ret); 2522c7ede4f0SGuixin Liu return ret; 2523c7ede4f0SGuixin Liu } 2524c7ede4f0SGuixin Liu 2525c7ede4f0SGuixin Liu if (val <= 0) { 2526c7ede4f0SGuixin Liu pr_err("Invalid cmd_ring_size_mb %d.\n", val); 2527c7ede4f0SGuixin Liu return -EINVAL; 2528c7ede4f0SGuixin Liu } 2529c7ede4f0SGuixin Liu 2530c7ede4f0SGuixin Liu mutex_lock(&udev->cmdr_lock); 2531c7ede4f0SGuixin Liu if (udev->data_bitmap) { 2532c7ede4f0SGuixin Liu pr_err("Cannot set cmd_ring_size_mb after it has been enabled.\n"); 2533c7ede4f0SGuixin Liu ret = -EINVAL; 2534c7ede4f0SGuixin Liu goto unlock; 2535c7ede4f0SGuixin Liu } 2536c7ede4f0SGuixin Liu 2537c7ede4f0SGuixin Liu udev->cmdr_size = (val << 20) - CMDR_OFF; 2538c7ede4f0SGuixin Liu if (val > (MB_CMDR_SIZE_DEF >> 20)) { 2539c7ede4f0SGuixin Liu pr_err("%d is too large. Adjusting cmd_ring_size_mb to global limit of %u\n", 2540c7ede4f0SGuixin Liu val, (MB_CMDR_SIZE_DEF >> 20)); 2541c7ede4f0SGuixin Liu udev->cmdr_size = CMDR_SIZE_DEF; 2542c7ede4f0SGuixin Liu } 2543c7ede4f0SGuixin Liu 2544c7ede4f0SGuixin Liu unlock: 2545c7ede4f0SGuixin Liu mutex_unlock(&udev->cmdr_lock); 2546c7ede4f0SGuixin Liu return ret; 2547c7ede4f0SGuixin Liu } 2548c7ede4f0SGuixin Liu 25497c9e7a6fSAndy Grover static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev, 25507c9e7a6fSAndy Grover const char *page, ssize_t count) 25517c9e7a6fSAndy Grover { 25527c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 25530e0d7526SMike Christie char *orig, *ptr, *opts; 25547c9e7a6fSAndy Grover substring_t args[MAX_OPT_ARGS]; 2555c97840c8SMike Christie int ret = 0, token; 25567c9e7a6fSAndy Grover 25577c9e7a6fSAndy Grover opts = kstrdup(page, GFP_KERNEL); 25587c9e7a6fSAndy Grover if (!opts) 25597c9e7a6fSAndy Grover return -ENOMEM; 25607c9e7a6fSAndy Grover 25617c9e7a6fSAndy Grover orig = opts; 25627c9e7a6fSAndy Grover 25637c9e7a6fSAndy Grover while ((ptr = strsep(&opts, ",\n")) != NULL) { 25647c9e7a6fSAndy Grover if (!*ptr) 25657c9e7a6fSAndy Grover continue; 25667c9e7a6fSAndy Grover 25677c9e7a6fSAndy Grover token = match_token(ptr, tokens, args); 25687c9e7a6fSAndy Grover switch (token) { 25697c9e7a6fSAndy Grover case Opt_dev_config: 25707c9e7a6fSAndy Grover if (match_strlcpy(udev->dev_config, &args[0], 25717c9e7a6fSAndy Grover TCMU_CONFIG_LEN) == 0) { 25727c9e7a6fSAndy Grover ret = -EINVAL; 25737c9e7a6fSAndy Grover break; 25747c9e7a6fSAndy Grover } 25757c9e7a6fSAndy Grover pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config); 25767c9e7a6fSAndy Grover break; 25777c9e7a6fSAndy Grover case Opt_dev_size: 25780e0d7526SMike Christie ret = match_u64(&args[0], &udev->dev_size); 25797c9e7a6fSAndy Grover if (ret < 0) 25800e0d7526SMike Christie pr_err("match_u64() failed for dev_size=. Error %d.\n", 25810e0d7526SMike Christie ret); 25827c9e7a6fSAndy Grover break; 25839c1cd1b6SAndy Grover case Opt_hw_block_size: 25843abaa2bfSMike Christie ret = tcmu_set_dev_attrib(&args[0], 25853abaa2bfSMike Christie &(dev->dev_attrib.hw_block_size)); 25869c1cd1b6SAndy Grover break; 25873abaa2bfSMike Christie case Opt_hw_max_sectors: 25883abaa2bfSMike Christie ret = tcmu_set_dev_attrib(&args[0], 25893abaa2bfSMike Christie &(dev->dev_attrib.hw_max_sectors)); 25909c1cd1b6SAndy Grover break; 2591b849b456SKenjiro Nakayama case Opt_nl_reply_supported: 2592b60cb1f8SMike Christie ret = match_int(&args[0], &udev->nl_reply_supported); 2593b849b456SKenjiro Nakayama if (ret < 0) 2594b60cb1f8SMike Christie pr_err("match_int() failed for nl_reply_supported=. Error %d.\n", 2595b60cb1f8SMike Christie ret); 2596b849b456SKenjiro Nakayama break; 259780eb8761SMike Christie case Opt_max_data_area_mb: 2598c97840c8SMike Christie ret = tcmu_set_max_blocks_param(udev, &args[0]); 259980eb8761SMike Christie break; 260008976cb5SBodo Stroesser case Opt_data_pages_per_blk: 260108976cb5SBodo Stroesser ret = tcmu_set_data_pages_per_blk(udev, &args[0]); 260208976cb5SBodo Stroesser break; 2603c7ede4f0SGuixin Liu case Opt_cmd_ring_size_mb: 2604c7ede4f0SGuixin Liu ret = tcmu_set_cmd_ring_size(udev, &args[0]); 2605c7ede4f0SGuixin Liu break; 26067c9e7a6fSAndy Grover default: 26077c9e7a6fSAndy Grover break; 26087c9e7a6fSAndy Grover } 26092579325cSMike Christie 26102579325cSMike Christie if (ret) 26112579325cSMike Christie break; 26127c9e7a6fSAndy Grover } 26137c9e7a6fSAndy Grover 26147c9e7a6fSAndy Grover kfree(orig); 26157c9e7a6fSAndy Grover return (!ret) ? count : ret; 26167c9e7a6fSAndy Grover } 26177c9e7a6fSAndy Grover 26187c9e7a6fSAndy Grover static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b) 26197c9e7a6fSAndy Grover { 26207c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 26217c9e7a6fSAndy Grover ssize_t bl = 0; 26227c9e7a6fSAndy Grover 26237c9e7a6fSAndy Grover bl = sprintf(b + bl, "Config: %s ", 26247c9e7a6fSAndy Grover udev->dev_config[0] ? udev->dev_config : "NULL"); 26250e0d7526SMike Christie bl += sprintf(b + bl, "Size: %llu ", udev->dev_size); 262608976cb5SBodo Stroesser bl += sprintf(b + bl, "MaxDataAreaMB: %u ", udev->data_area_mb); 2627c7ede4f0SGuixin Liu bl += sprintf(b + bl, "DataPagesPerBlk: %u ", udev->data_pages_per_blk); 2628c7ede4f0SGuixin Liu bl += sprintf(b + bl, "CmdRingSizeMB: %u\n", 2629c7ede4f0SGuixin Liu (udev->cmdr_size + CMDR_OFF) >> 20); 26307c9e7a6fSAndy Grover 26317c9e7a6fSAndy Grover return bl; 26327c9e7a6fSAndy Grover } 26337c9e7a6fSAndy Grover 26347c9e7a6fSAndy Grover static sector_t tcmu_get_blocks(struct se_device *dev) 26357c9e7a6fSAndy Grover { 26367c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 26377c9e7a6fSAndy Grover 26387c9e7a6fSAndy Grover return div_u64(udev->dev_size - dev->dev_attrib.block_size, 26397c9e7a6fSAndy Grover dev->dev_attrib.block_size); 26407c9e7a6fSAndy Grover } 26417c9e7a6fSAndy Grover 26427c9e7a6fSAndy Grover static sense_reason_t 26437c9e7a6fSAndy Grover tcmu_parse_cdb(struct se_cmd *cmd) 26447c9e7a6fSAndy Grover { 264502eb924fSAndy Grover return passthrough_parse_cdb(cmd, tcmu_queue_cmd); 26469c1cd1b6SAndy Grover } 26479c1cd1b6SAndy Grover 26487d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page) 26497d7a7435SNicholas Bellinger { 26507d7a7435SNicholas Bellinger struct se_dev_attrib *da = container_of(to_config_group(item), 26517d7a7435SNicholas Bellinger struct se_dev_attrib, da_group); 2652b5ab697cSKenjiro Nakayama struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 26537d7a7435SNicholas Bellinger 26547d7a7435SNicholas Bellinger return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC); 26557d7a7435SNicholas Bellinger } 26567d7a7435SNicholas Bellinger 26577d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page, 26587d7a7435SNicholas Bellinger size_t count) 26597d7a7435SNicholas Bellinger { 26607d7a7435SNicholas Bellinger struct se_dev_attrib *da = container_of(to_config_group(item), 26617d7a7435SNicholas Bellinger struct se_dev_attrib, da_group); 26627d7a7435SNicholas Bellinger struct tcmu_dev *udev = container_of(da->da_dev, 26637d7a7435SNicholas Bellinger struct tcmu_dev, se_dev); 26647d7a7435SNicholas Bellinger u32 val; 26657d7a7435SNicholas Bellinger int ret; 26667d7a7435SNicholas Bellinger 26677d7a7435SNicholas Bellinger if (da->da_dev->export_count) { 26687d7a7435SNicholas Bellinger pr_err("Unable to set tcmu cmd_time_out while exports exist\n"); 26697d7a7435SNicholas Bellinger return -EINVAL; 26707d7a7435SNicholas Bellinger } 26717d7a7435SNicholas Bellinger 26727d7a7435SNicholas Bellinger ret = kstrtou32(page, 0, &val); 26737d7a7435SNicholas Bellinger if (ret < 0) 26747d7a7435SNicholas Bellinger return ret; 26757d7a7435SNicholas Bellinger 26767d7a7435SNicholas Bellinger udev->cmd_time_out = val * MSEC_PER_SEC; 26777d7a7435SNicholas Bellinger return count; 26787d7a7435SNicholas Bellinger } 26797d7a7435SNicholas Bellinger CONFIGFS_ATTR(tcmu_, cmd_time_out); 26807d7a7435SNicholas Bellinger 26819103575aSMike Christie static ssize_t tcmu_qfull_time_out_show(struct config_item *item, char *page) 26829103575aSMike Christie { 26839103575aSMike Christie struct se_dev_attrib *da = container_of(to_config_group(item), 26849103575aSMike Christie struct se_dev_attrib, da_group); 26859103575aSMike Christie struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 26869103575aSMike Christie 26879103575aSMike Christie return snprintf(page, PAGE_SIZE, "%ld\n", udev->qfull_time_out <= 0 ? 26889103575aSMike Christie udev->qfull_time_out : 26899103575aSMike Christie udev->qfull_time_out / MSEC_PER_SEC); 26909103575aSMike Christie } 26919103575aSMike Christie 26929103575aSMike Christie static ssize_t tcmu_qfull_time_out_store(struct config_item *item, 26939103575aSMike Christie const char *page, size_t count) 26949103575aSMike Christie { 26959103575aSMike Christie struct se_dev_attrib *da = container_of(to_config_group(item), 26969103575aSMike Christie struct se_dev_attrib, da_group); 26979103575aSMike Christie struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 26989103575aSMike Christie s32 val; 26999103575aSMike Christie int ret; 27009103575aSMike Christie 27019103575aSMike Christie ret = kstrtos32(page, 0, &val); 27029103575aSMike Christie if (ret < 0) 27039103575aSMike Christie return ret; 27049103575aSMike Christie 27059103575aSMike Christie if (val >= 0) { 27069103575aSMike Christie udev->qfull_time_out = val * MSEC_PER_SEC; 2707125966dbSPrasanna Kumar Kalever } else if (val == -1) { 2708125966dbSPrasanna Kumar Kalever udev->qfull_time_out = val; 27099103575aSMike Christie } else { 27109103575aSMike Christie printk(KERN_ERR "Invalid qfull timeout value %d\n", val); 27119103575aSMike Christie return -EINVAL; 27129103575aSMike Christie } 27139103575aSMike Christie return count; 27149103575aSMike Christie } 27159103575aSMike Christie CONFIGFS_ATTR(tcmu_, qfull_time_out); 27169103575aSMike Christie 271780eb8761SMike Christie static ssize_t tcmu_max_data_area_mb_show(struct config_item *item, char *page) 271880eb8761SMike Christie { 271980eb8761SMike Christie struct se_dev_attrib *da = container_of(to_config_group(item), 272080eb8761SMike Christie struct se_dev_attrib, da_group); 272180eb8761SMike Christie struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 272280eb8761SMike Christie 2723f5ce815fSBodo Stroesser return snprintf(page, PAGE_SIZE, "%u\n", udev->data_area_mb); 272480eb8761SMike Christie } 272580eb8761SMike Christie CONFIGFS_ATTR_RO(tcmu_, max_data_area_mb); 272680eb8761SMike Christie 272708976cb5SBodo Stroesser static ssize_t tcmu_data_pages_per_blk_show(struct config_item *item, 272808976cb5SBodo Stroesser char *page) 272908976cb5SBodo Stroesser { 273008976cb5SBodo Stroesser struct se_dev_attrib *da = container_of(to_config_group(item), 273108976cb5SBodo Stroesser struct se_dev_attrib, da_group); 273208976cb5SBodo Stroesser struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 273308976cb5SBodo Stroesser 273408976cb5SBodo Stroesser return snprintf(page, PAGE_SIZE, "%u\n", udev->data_pages_per_blk); 273508976cb5SBodo Stroesser } 273608976cb5SBodo Stroesser CONFIGFS_ATTR_RO(tcmu_, data_pages_per_blk); 273708976cb5SBodo Stroesser 2738c7ede4f0SGuixin Liu static ssize_t tcmu_cmd_ring_size_mb_show(struct config_item *item, char *page) 2739c7ede4f0SGuixin Liu { 2740c7ede4f0SGuixin Liu struct se_dev_attrib *da = container_of(to_config_group(item), 2741c7ede4f0SGuixin Liu struct se_dev_attrib, da_group); 2742c7ede4f0SGuixin Liu struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2743c7ede4f0SGuixin Liu 2744c7ede4f0SGuixin Liu return snprintf(page, PAGE_SIZE, "%u\n", 2745c7ede4f0SGuixin Liu (udev->cmdr_size + CMDR_OFF) >> 20); 2746c7ede4f0SGuixin Liu } 2747c7ede4f0SGuixin Liu CONFIGFS_ATTR_RO(tcmu_, cmd_ring_size_mb); 2748c7ede4f0SGuixin Liu 27492d76443eSMike Christie static ssize_t tcmu_dev_config_show(struct config_item *item, char *page) 2750ee018252SBryant G. Ly { 2751ee018252SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 2752ee018252SBryant G. Ly struct se_dev_attrib, da_group); 2753ee018252SBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2754ee018252SBryant G. Ly 2755ee018252SBryant G. Ly return snprintf(page, PAGE_SIZE, "%s\n", udev->dev_config); 2756ee018252SBryant G. Ly } 2757ee018252SBryant G. Ly 275802ccfb54SZhu Lingshan static int tcmu_send_dev_config_event(struct tcmu_dev *udev, 275902ccfb54SZhu Lingshan const char *reconfig_data) 276002ccfb54SZhu Lingshan { 276102ccfb54SZhu Lingshan struct sk_buff *skb = NULL; 276202ccfb54SZhu Lingshan void *msg_header = NULL; 276302ccfb54SZhu Lingshan int ret = 0; 276402ccfb54SZhu Lingshan 276502ccfb54SZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE, 276602ccfb54SZhu Lingshan &skb, &msg_header); 276702ccfb54SZhu Lingshan if (ret < 0) 276802ccfb54SZhu Lingshan return ret; 276902ccfb54SZhu Lingshan ret = nla_put_string(skb, TCMU_ATTR_DEV_CFG, reconfig_data); 277002ccfb54SZhu Lingshan if (ret < 0) { 277102ccfb54SZhu Lingshan nlmsg_free(skb); 277202ccfb54SZhu Lingshan return ret; 277302ccfb54SZhu Lingshan } 277402ccfb54SZhu Lingshan return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE, 277506add777SMike Christie skb, msg_header); 277602ccfb54SZhu Lingshan } 277702ccfb54SZhu Lingshan 277802ccfb54SZhu Lingshan 27792d76443eSMike Christie static ssize_t tcmu_dev_config_store(struct config_item *item, const char *page, 2780ee018252SBryant G. Ly size_t count) 2781ee018252SBryant G. Ly { 2782ee018252SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 2783ee018252SBryant G. Ly struct se_dev_attrib, da_group); 2784ee018252SBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 27852d76443eSMike Christie int ret, len; 2786ee018252SBryant G. Ly 27872d76443eSMike Christie len = strlen(page); 27882d76443eSMike Christie if (!len || len > TCMU_CONFIG_LEN - 1) 2789ee018252SBryant G. Ly return -EINVAL; 2790ee018252SBryant G. Ly 2791ee018252SBryant G. Ly /* Check if device has been configured before */ 279263d5be0fSMike Christie if (target_dev_configured(&udev->se_dev)) { 279302ccfb54SZhu Lingshan ret = tcmu_send_dev_config_event(udev, page); 2794ee018252SBryant G. Ly if (ret) { 2795ee018252SBryant G. Ly pr_err("Unable to reconfigure device\n"); 2796ee018252SBryant G. Ly return ret; 2797ee018252SBryant G. Ly } 2798de8c5221SBryant G. Ly strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN); 2799de8c5221SBryant G. Ly 2800de8c5221SBryant G. Ly ret = tcmu_update_uio_info(udev); 2801de8c5221SBryant G. Ly if (ret) 2802de8c5221SBryant G. Ly return ret; 2803de8c5221SBryant G. Ly return count; 2804ee018252SBryant G. Ly } 28052d76443eSMike Christie strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN); 2806ee018252SBryant G. Ly 2807ee018252SBryant G. Ly return count; 2808ee018252SBryant G. Ly } 28092d76443eSMike Christie CONFIGFS_ATTR(tcmu_, dev_config); 2810ee018252SBryant G. Ly 2811801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_show(struct config_item *item, char *page) 2812801fc54dSBryant G. Ly { 2813801fc54dSBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 2814801fc54dSBryant G. Ly struct se_dev_attrib, da_group); 2815801fc54dSBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2816801fc54dSBryant G. Ly 28170e0d7526SMike Christie return snprintf(page, PAGE_SIZE, "%llu\n", udev->dev_size); 2818801fc54dSBryant G. Ly } 2819801fc54dSBryant G. Ly 282084e28506SZhu Lingshan static int tcmu_send_dev_size_event(struct tcmu_dev *udev, u64 size) 282184e28506SZhu Lingshan { 282284e28506SZhu Lingshan struct sk_buff *skb = NULL; 282384e28506SZhu Lingshan void *msg_header = NULL; 282484e28506SZhu Lingshan int ret = 0; 282584e28506SZhu Lingshan 282684e28506SZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE, 282784e28506SZhu Lingshan &skb, &msg_header); 282884e28506SZhu Lingshan if (ret < 0) 282984e28506SZhu Lingshan return ret; 283084e28506SZhu Lingshan ret = nla_put_u64_64bit(skb, TCMU_ATTR_DEV_SIZE, 283184e28506SZhu Lingshan size, TCMU_ATTR_PAD); 283284e28506SZhu Lingshan if (ret < 0) { 283384e28506SZhu Lingshan nlmsg_free(skb); 283484e28506SZhu Lingshan return ret; 283584e28506SZhu Lingshan } 283684e28506SZhu Lingshan return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE, 283706add777SMike Christie skb, msg_header); 283884e28506SZhu Lingshan } 283984e28506SZhu Lingshan 2840801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page, 2841801fc54dSBryant G. Ly size_t count) 2842801fc54dSBryant G. Ly { 2843801fc54dSBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 2844801fc54dSBryant G. Ly struct se_dev_attrib, da_group); 2845801fc54dSBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 28462d76443eSMike Christie u64 val; 2847801fc54dSBryant G. Ly int ret; 2848801fc54dSBryant G. Ly 28492d76443eSMike Christie ret = kstrtou64(page, 0, &val); 2850801fc54dSBryant G. Ly if (ret < 0) 2851801fc54dSBryant G. Ly return ret; 2852801fc54dSBryant G. Ly 2853801fc54dSBryant G. Ly /* Check if device has been configured before */ 285463d5be0fSMike Christie if (target_dev_configured(&udev->se_dev)) { 285584e28506SZhu Lingshan ret = tcmu_send_dev_size_event(udev, val); 2856801fc54dSBryant G. Ly if (ret) { 2857801fc54dSBryant G. Ly pr_err("Unable to reconfigure device\n"); 2858801fc54dSBryant G. Ly return ret; 2859801fc54dSBryant G. Ly } 2860801fc54dSBryant G. Ly } 28612d76443eSMike Christie udev->dev_size = val; 2862801fc54dSBryant G. Ly return count; 2863801fc54dSBryant G. Ly } 2864801fc54dSBryant G. Ly CONFIGFS_ATTR(tcmu_, dev_size); 2865801fc54dSBryant G. Ly 2866b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_show(struct config_item *item, 2867b849b456SKenjiro Nakayama char *page) 2868b849b456SKenjiro Nakayama { 2869b849b456SKenjiro Nakayama struct se_dev_attrib *da = container_of(to_config_group(item), 2870b849b456SKenjiro Nakayama struct se_dev_attrib, da_group); 2871b849b456SKenjiro Nakayama struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2872b849b456SKenjiro Nakayama 2873b849b456SKenjiro Nakayama return snprintf(page, PAGE_SIZE, "%d\n", udev->nl_reply_supported); 2874b849b456SKenjiro Nakayama } 2875b849b456SKenjiro Nakayama 2876b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_store(struct config_item *item, 2877b849b456SKenjiro Nakayama const char *page, size_t count) 2878b849b456SKenjiro Nakayama { 2879b849b456SKenjiro Nakayama struct se_dev_attrib *da = container_of(to_config_group(item), 2880b849b456SKenjiro Nakayama struct se_dev_attrib, da_group); 2881b849b456SKenjiro Nakayama struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2882b849b456SKenjiro Nakayama s8 val; 2883b849b456SKenjiro Nakayama int ret; 2884b849b456SKenjiro Nakayama 2885b849b456SKenjiro Nakayama ret = kstrtos8(page, 0, &val); 2886b849b456SKenjiro Nakayama if (ret < 0) 2887b849b456SKenjiro Nakayama return ret; 2888b849b456SKenjiro Nakayama 2889b849b456SKenjiro Nakayama udev->nl_reply_supported = val; 2890b849b456SKenjiro Nakayama return count; 2891b849b456SKenjiro Nakayama } 2892b849b456SKenjiro Nakayama CONFIGFS_ATTR(tcmu_, nl_reply_supported); 2893b849b456SKenjiro Nakayama 28949a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_show(struct config_item *item, 28959a8bb606SBryant G. Ly char *page) 28969a8bb606SBryant G. Ly { 28979a8bb606SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 28989a8bb606SBryant G. Ly struct se_dev_attrib, da_group); 28999a8bb606SBryant G. Ly 29009a8bb606SBryant G. Ly return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache); 29019a8bb606SBryant G. Ly } 29029a8bb606SBryant G. Ly 290333d065ccSZhu Lingshan static int tcmu_send_emulate_write_cache(struct tcmu_dev *udev, u8 val) 290433d065ccSZhu Lingshan { 290533d065ccSZhu Lingshan struct sk_buff *skb = NULL; 290633d065ccSZhu Lingshan void *msg_header = NULL; 290733d065ccSZhu Lingshan int ret = 0; 290833d065ccSZhu Lingshan 290933d065ccSZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE, 291033d065ccSZhu Lingshan &skb, &msg_header); 291133d065ccSZhu Lingshan if (ret < 0) 291233d065ccSZhu Lingshan return ret; 291333d065ccSZhu Lingshan ret = nla_put_u8(skb, TCMU_ATTR_WRITECACHE, val); 291433d065ccSZhu Lingshan if (ret < 0) { 291533d065ccSZhu Lingshan nlmsg_free(skb); 291633d065ccSZhu Lingshan return ret; 291733d065ccSZhu Lingshan } 291833d065ccSZhu Lingshan return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE, 291906add777SMike Christie skb, msg_header); 292033d065ccSZhu Lingshan } 292133d065ccSZhu Lingshan 29229a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_store(struct config_item *item, 29239a8bb606SBryant G. Ly const char *page, size_t count) 29249a8bb606SBryant G. Ly { 29259a8bb606SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 29269a8bb606SBryant G. Ly struct se_dev_attrib, da_group); 29271068be7bSBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 29282d76443eSMike Christie u8 val; 29299a8bb606SBryant G. Ly int ret; 29309a8bb606SBryant G. Ly 29312d76443eSMike Christie ret = kstrtou8(page, 0, &val); 29329a8bb606SBryant G. Ly if (ret < 0) 29339a8bb606SBryant G. Ly return ret; 29349a8bb606SBryant G. Ly 29351068be7bSBryant G. Ly /* Check if device has been configured before */ 293663d5be0fSMike Christie if (target_dev_configured(&udev->se_dev)) { 293733d065ccSZhu Lingshan ret = tcmu_send_emulate_write_cache(udev, val); 29381068be7bSBryant G. Ly if (ret) { 29391068be7bSBryant G. Ly pr_err("Unable to reconfigure device\n"); 29401068be7bSBryant G. Ly return ret; 29411068be7bSBryant G. Ly } 29421068be7bSBryant G. Ly } 29432d76443eSMike Christie 29442d76443eSMike Christie da->emulate_write_cache = val; 29459a8bb606SBryant G. Ly return count; 29469a8bb606SBryant G. Ly } 29479a8bb606SBryant G. Ly CONFIGFS_ATTR(tcmu_, emulate_write_cache); 29489a8bb606SBryant G. Ly 294959526d7aSBodo Stroesser static ssize_t tcmu_tmr_notification_show(struct config_item *item, char *page) 295059526d7aSBodo Stroesser { 295159526d7aSBodo Stroesser struct se_dev_attrib *da = container_of(to_config_group(item), 295259526d7aSBodo Stroesser struct se_dev_attrib, da_group); 295359526d7aSBodo Stroesser struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 295459526d7aSBodo Stroesser 295559526d7aSBodo Stroesser return snprintf(page, PAGE_SIZE, "%i\n", 295659526d7aSBodo Stroesser test_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags)); 295759526d7aSBodo Stroesser } 295859526d7aSBodo Stroesser 295959526d7aSBodo Stroesser static ssize_t tcmu_tmr_notification_store(struct config_item *item, 296059526d7aSBodo Stroesser const char *page, size_t count) 296159526d7aSBodo Stroesser { 296259526d7aSBodo Stroesser struct se_dev_attrib *da = container_of(to_config_group(item), 296359526d7aSBodo Stroesser struct se_dev_attrib, da_group); 296459526d7aSBodo Stroesser struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 296559526d7aSBodo Stroesser u8 val; 296659526d7aSBodo Stroesser int ret; 296759526d7aSBodo Stroesser 296859526d7aSBodo Stroesser ret = kstrtou8(page, 0, &val); 296959526d7aSBodo Stroesser if (ret < 0) 297059526d7aSBodo Stroesser return ret; 297159526d7aSBodo Stroesser if (val > 1) 297259526d7aSBodo Stroesser return -EINVAL; 297359526d7aSBodo Stroesser 297459526d7aSBodo Stroesser if (val) 297559526d7aSBodo Stroesser set_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags); 297659526d7aSBodo Stroesser else 297759526d7aSBodo Stroesser clear_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags); 297859526d7aSBodo Stroesser return count; 297959526d7aSBodo Stroesser } 298059526d7aSBodo Stroesser CONFIGFS_ATTR(tcmu_, tmr_notification); 298159526d7aSBodo Stroesser 2982892782caSMike Christie static ssize_t tcmu_block_dev_show(struct config_item *item, char *page) 2983892782caSMike Christie { 2984892782caSMike Christie struct se_device *se_dev = container_of(to_config_group(item), 2985892782caSMike Christie struct se_device, 2986892782caSMike Christie dev_action_group); 2987892782caSMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 2988892782caSMike Christie 2989892782caSMike Christie if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) 2990892782caSMike Christie return snprintf(page, PAGE_SIZE, "%s\n", "blocked"); 2991892782caSMike Christie else 2992892782caSMike Christie return snprintf(page, PAGE_SIZE, "%s\n", "unblocked"); 2993892782caSMike Christie } 2994892782caSMike Christie 2995892782caSMike Christie static ssize_t tcmu_block_dev_store(struct config_item *item, const char *page, 2996892782caSMike Christie size_t count) 2997892782caSMike Christie { 2998892782caSMike Christie struct se_device *se_dev = container_of(to_config_group(item), 2999892782caSMike Christie struct se_device, 3000892782caSMike Christie dev_action_group); 3001892782caSMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 3002892782caSMike Christie u8 val; 3003892782caSMike Christie int ret; 3004892782caSMike Christie 3005a30b0473SMike Christie if (!target_dev_configured(&udev->se_dev)) { 3006a30b0473SMike Christie pr_err("Device is not configured.\n"); 3007a30b0473SMike Christie return -EINVAL; 3008a30b0473SMike Christie } 3009a30b0473SMike Christie 3010892782caSMike Christie ret = kstrtou8(page, 0, &val); 3011892782caSMike Christie if (ret < 0) 3012892782caSMike Christie return ret; 3013892782caSMike Christie 3014892782caSMike Christie if (val > 1) { 3015892782caSMike Christie pr_err("Invalid block value %d\n", val); 3016892782caSMike Christie return -EINVAL; 3017892782caSMike Christie } 3018892782caSMike Christie 3019892782caSMike Christie if (!val) 3020892782caSMike Christie tcmu_unblock_dev(udev); 3021892782caSMike Christie else 3022892782caSMike Christie tcmu_block_dev(udev); 3023892782caSMike Christie return count; 3024892782caSMike Christie } 3025892782caSMike Christie CONFIGFS_ATTR(tcmu_, block_dev); 3026892782caSMike Christie 3027892782caSMike Christie static ssize_t tcmu_reset_ring_store(struct config_item *item, const char *page, 3028892782caSMike Christie size_t count) 3029892782caSMike Christie { 3030892782caSMike Christie struct se_device *se_dev = container_of(to_config_group(item), 3031892782caSMike Christie struct se_device, 3032892782caSMike Christie dev_action_group); 3033892782caSMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 3034892782caSMike Christie u8 val; 3035892782caSMike Christie int ret; 3036892782caSMike Christie 3037a30b0473SMike Christie if (!target_dev_configured(&udev->se_dev)) { 3038a30b0473SMike Christie pr_err("Device is not configured.\n"); 3039a30b0473SMike Christie return -EINVAL; 3040a30b0473SMike Christie } 3041a30b0473SMike Christie 3042892782caSMike Christie ret = kstrtou8(page, 0, &val); 3043892782caSMike Christie if (ret < 0) 3044892782caSMike Christie return ret; 3045892782caSMike Christie 3046892782caSMike Christie if (val != 1 && val != 2) { 3047892782caSMike Christie pr_err("Invalid reset ring value %d\n", val); 3048892782caSMike Christie return -EINVAL; 3049892782caSMike Christie } 3050892782caSMike Christie 3051892782caSMike Christie tcmu_reset_ring(udev, val); 3052892782caSMike Christie return count; 3053892782caSMike Christie } 3054892782caSMike Christie CONFIGFS_ATTR_WO(tcmu_, reset_ring); 3055892782caSMike Christie 3056018c1491SBodo Stroesser static ssize_t tcmu_free_kept_buf_store(struct config_item *item, const char *page, 3057018c1491SBodo Stroesser size_t count) 3058018c1491SBodo Stroesser { 3059018c1491SBodo Stroesser struct se_device *se_dev = container_of(to_config_group(item), 3060018c1491SBodo Stroesser struct se_device, 3061018c1491SBodo Stroesser dev_action_group); 3062018c1491SBodo Stroesser struct tcmu_dev *udev = TCMU_DEV(se_dev); 3063018c1491SBodo Stroesser struct tcmu_cmd *cmd; 3064018c1491SBodo Stroesser u16 cmd_id; 3065018c1491SBodo Stroesser int ret; 3066018c1491SBodo Stroesser 3067018c1491SBodo Stroesser if (!target_dev_configured(&udev->se_dev)) { 3068018c1491SBodo Stroesser pr_err("Device is not configured.\n"); 3069018c1491SBodo Stroesser return -EINVAL; 3070018c1491SBodo Stroesser } 3071018c1491SBodo Stroesser 3072018c1491SBodo Stroesser ret = kstrtou16(page, 0, &cmd_id); 3073018c1491SBodo Stroesser if (ret < 0) 3074018c1491SBodo Stroesser return ret; 3075018c1491SBodo Stroesser 3076018c1491SBodo Stroesser mutex_lock(&udev->cmdr_lock); 3077018c1491SBodo Stroesser 3078018c1491SBodo Stroesser { 3079018c1491SBodo Stroesser XA_STATE(xas, &udev->commands, cmd_id); 3080018c1491SBodo Stroesser 3081018c1491SBodo Stroesser xas_lock(&xas); 3082018c1491SBodo Stroesser cmd = xas_load(&xas); 3083018c1491SBodo Stroesser if (!cmd) { 3084018c1491SBodo Stroesser pr_err("free_kept_buf: cmd_id %d not found\n", cmd_id); 3085018c1491SBodo Stroesser count = -EINVAL; 3086018c1491SBodo Stroesser xas_unlock(&xas); 3087018c1491SBodo Stroesser goto out_unlock; 3088018c1491SBodo Stroesser } 3089018c1491SBodo Stroesser if (!test_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags)) { 3090018c1491SBodo Stroesser pr_err("free_kept_buf: cmd_id %d was not completed with KEEP_BUF\n", 3091018c1491SBodo Stroesser cmd_id); 3092018c1491SBodo Stroesser count = -EINVAL; 3093018c1491SBodo Stroesser xas_unlock(&xas); 3094018c1491SBodo Stroesser goto out_unlock; 3095018c1491SBodo Stroesser } 3096018c1491SBodo Stroesser xas_store(&xas, NULL); 3097018c1491SBodo Stroesser xas_unlock(&xas); 3098018c1491SBodo Stroesser } 3099018c1491SBodo Stroesser 3100018c1491SBodo Stroesser tcmu_cmd_free_data(cmd, cmd->dbi_cnt); 3101018c1491SBodo Stroesser tcmu_free_cmd(cmd); 3102018c1491SBodo Stroesser /* 3103018c1491SBodo Stroesser * We only freed data space, not ring space. Therefore we dont call 3104018c1491SBodo Stroesser * run_tmr_queue, but call run_qfull_queue if tmr_list is empty. 3105018c1491SBodo Stroesser */ 3106018c1491SBodo Stroesser if (list_empty(&udev->tmr_queue)) 3107018c1491SBodo Stroesser run_qfull_queue(udev, false); 3108018c1491SBodo Stroesser 3109018c1491SBodo Stroesser out_unlock: 3110018c1491SBodo Stroesser mutex_unlock(&udev->cmdr_lock); 3111018c1491SBodo Stroesser return count; 3112018c1491SBodo Stroesser } 3113018c1491SBodo Stroesser CONFIGFS_ATTR_WO(tcmu_, free_kept_buf); 3114018c1491SBodo Stroesser 31155821783bSColin Ian King static struct configfs_attribute *tcmu_attrib_attrs[] = { 3116801fc54dSBryant G. Ly &tcmu_attr_cmd_time_out, 31179103575aSMike Christie &tcmu_attr_qfull_time_out, 311880eb8761SMike Christie &tcmu_attr_max_data_area_mb, 311908976cb5SBodo Stroesser &tcmu_attr_data_pages_per_blk, 3120c7ede4f0SGuixin Liu &tcmu_attr_cmd_ring_size_mb, 31212d76443eSMike Christie &tcmu_attr_dev_config, 3122801fc54dSBryant G. Ly &tcmu_attr_dev_size, 3123801fc54dSBryant G. Ly &tcmu_attr_emulate_write_cache, 312459526d7aSBodo Stroesser &tcmu_attr_tmr_notification, 3125b849b456SKenjiro Nakayama &tcmu_attr_nl_reply_supported, 3126801fc54dSBryant G. Ly NULL, 3127801fc54dSBryant G. Ly }; 3128801fc54dSBryant G. Ly 31297d7a7435SNicholas Bellinger static struct configfs_attribute **tcmu_attrs; 31307d7a7435SNicholas Bellinger 3131892782caSMike Christie static struct configfs_attribute *tcmu_action_attrs[] = { 3132892782caSMike Christie &tcmu_attr_block_dev, 3133892782caSMike Christie &tcmu_attr_reset_ring, 3134018c1491SBodo Stroesser &tcmu_attr_free_kept_buf, 3135892782caSMike Christie NULL, 3136892782caSMike Christie }; 3137892782caSMike Christie 31387d7a7435SNicholas Bellinger static struct target_backend_ops tcmu_ops = { 31397c9e7a6fSAndy Grover .name = "user", 31407c9e7a6fSAndy Grover .owner = THIS_MODULE, 314169088a04SBodo Stroesser .transport_flags_default = TRANSPORT_FLAG_PASSTHROUGH, 3142356ba2a8SBodo Stroesser .transport_flags_changeable = TRANSPORT_FLAG_PASSTHROUGH_PGR | 3143356ba2a8SBodo Stroesser TRANSPORT_FLAG_PASSTHROUGH_ALUA, 31447c9e7a6fSAndy Grover .attach_hba = tcmu_attach_hba, 31457c9e7a6fSAndy Grover .detach_hba = tcmu_detach_hba, 31467c9e7a6fSAndy Grover .alloc_device = tcmu_alloc_device, 31477c9e7a6fSAndy Grover .configure_device = tcmu_configure_device, 314892634706SMike Christie .destroy_device = tcmu_destroy_device, 31497c9e7a6fSAndy Grover .free_device = tcmu_free_device, 31506888da81SMike Christie .unplug_device = tcmu_unplug_device, 31516888da81SMike Christie .plug_device = tcmu_plug_device, 31527c9e7a6fSAndy Grover .parse_cdb = tcmu_parse_cdb, 3153bc2d214aSBodo Stroesser .tmr_notify = tcmu_tmr_notify, 31547c9e7a6fSAndy Grover .set_configfs_dev_params = tcmu_set_configfs_dev_params, 31557c9e7a6fSAndy Grover .show_configfs_dev_params = tcmu_show_configfs_dev_params, 31567c9e7a6fSAndy Grover .get_device_type = sbc_get_device_type, 31577c9e7a6fSAndy Grover .get_blocks = tcmu_get_blocks, 3158892782caSMike Christie .tb_dev_action_attrs = tcmu_action_attrs, 31597c9e7a6fSAndy Grover }; 31607c9e7a6fSAndy Grover 316189ec9cfdSMike Christie static void find_free_blocks(void) 3162b6df4b79SXiubo Li { 3163b6df4b79SXiubo Li struct tcmu_dev *udev; 3164b6df4b79SXiubo Li loff_t off; 3165f5ce815fSBodo Stroesser u32 pages_freed, total_pages_freed = 0; 3166f5ce815fSBodo Stroesser u32 start, end, block, total_blocks_freed = 0; 3167af1dd7ffSMike Christie 31688b084d9dSBodo Stroesser if (atomic_read(&global_page_count) <= tcmu_global_max_pages) 3169af1dd7ffSMike Christie return; 3170b6df4b79SXiubo Li 3171b6df4b79SXiubo Li mutex_lock(&root_udev_mutex); 3172b6df4b79SXiubo Li list_for_each_entry(udev, &root_udev, node) { 3173b6df4b79SXiubo Li mutex_lock(&udev->cmdr_lock); 3174b6df4b79SXiubo Li 3175dc335a99SMike Christie if (!target_dev_configured(&udev->se_dev)) { 3176dc335a99SMike Christie mutex_unlock(&udev->cmdr_lock); 3177dc335a99SMike Christie continue; 3178dc335a99SMike Christie } 3179dc335a99SMike Christie 3180b6df4b79SXiubo Li /* Try to complete the finished commands first */ 3181bc2d214aSBodo Stroesser if (tcmu_handle_completions(udev)) 3182bc2d214aSBodo Stroesser run_qfull_queue(udev, false); 3183b6df4b79SXiubo Li 3184af1dd7ffSMike Christie /* Skip the udevs in idle */ 3185af1dd7ffSMike Christie if (!udev->dbi_thresh) { 3186b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 3187b6df4b79SXiubo Li continue; 3188b6df4b79SXiubo Li } 3189b6df4b79SXiubo Li 3190b6df4b79SXiubo Li end = udev->dbi_max + 1; 3191b6df4b79SXiubo Li block = find_last_bit(udev->data_bitmap, end); 3192b6df4b79SXiubo Li if (block == udev->dbi_max) { 3193b6df4b79SXiubo Li /* 3194af1dd7ffSMike Christie * The last bit is dbi_max, so it is not possible 3195af1dd7ffSMike Christie * reclaim any blocks. 3196b6df4b79SXiubo Li */ 3197b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 3198b6df4b79SXiubo Li continue; 3199b6df4b79SXiubo Li } else if (block == end) { 3200b6df4b79SXiubo Li /* The current udev will goto idle state */ 3201b6df4b79SXiubo Li udev->dbi_thresh = start = 0; 3202b6df4b79SXiubo Li udev->dbi_max = 0; 3203b6df4b79SXiubo Li } else { 3204b6df4b79SXiubo Li udev->dbi_thresh = start = block + 1; 3205b6df4b79SXiubo Li udev->dbi_max = block; 3206b6df4b79SXiubo Li } 3207b6df4b79SXiubo Li 3208b6df4b79SXiubo Li /* Here will truncate the data area from off */ 3209e719afdcSBodo Stroesser off = udev->data_off + (loff_t)start * udev->data_blk_size; 3210b6df4b79SXiubo Li unmap_mapping_range(udev->inode->i_mapping, off, 0, 1); 3211b6df4b79SXiubo Li 3212b6df4b79SXiubo Li /* Release the block pages */ 3213e719afdcSBodo Stroesser pages_freed = tcmu_blocks_release(udev, start, end - 1); 3214b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 3215af1dd7ffSMike Christie 3216f5ce815fSBodo Stroesser total_pages_freed += pages_freed; 3217f5ce815fSBodo Stroesser total_blocks_freed += end - start; 3218f5ce815fSBodo Stroesser pr_debug("Freed %u pages (total %u) from %u blocks (total %u) from %s.\n", 3219f5ce815fSBodo Stroesser pages_freed, total_pages_freed, end - start, 3220f5ce815fSBodo Stroesser total_blocks_freed, udev->name); 3221b6df4b79SXiubo Li } 322289ec9cfdSMike Christie mutex_unlock(&root_udev_mutex); 322389ec9cfdSMike Christie 32248b084d9dSBodo Stroesser if (atomic_read(&global_page_count) > tcmu_global_max_pages) 3225af1dd7ffSMike Christie schedule_delayed_work(&tcmu_unmap_work, msecs_to_jiffies(5000)); 3226b6df4b79SXiubo Li } 3227b6df4b79SXiubo Li 3228488ebe4cSMike Christie static void check_timedout_devices(void) 3229488ebe4cSMike Christie { 3230488ebe4cSMike Christie struct tcmu_dev *udev, *tmp_dev; 323161fb2482SBodo Stroesser struct tcmu_cmd *cmd, *tmp_cmd; 3232488ebe4cSMike Christie LIST_HEAD(devs); 3233488ebe4cSMike Christie 3234488ebe4cSMike Christie spin_lock_bh(&timed_out_udevs_lock); 3235488ebe4cSMike Christie list_splice_init(&timed_out_udevs, &devs); 3236488ebe4cSMike Christie 3237488ebe4cSMike Christie list_for_each_entry_safe(udev, tmp_dev, &devs, timedout_entry) { 3238488ebe4cSMike Christie list_del_init(&udev->timedout_entry); 3239488ebe4cSMike Christie spin_unlock_bh(&timed_out_udevs_lock); 3240488ebe4cSMike Christie 32416fddcb77SMike Christie mutex_lock(&udev->cmdr_lock); 3242a94a2572SXiubo Li 324361fb2482SBodo Stroesser /* 324461fb2482SBodo Stroesser * If cmd_time_out is disabled but qfull is set deadline 324561fb2482SBodo Stroesser * will only reflect the qfull timeout. Ignore it. 324661fb2482SBodo Stroesser */ 324761fb2482SBodo Stroesser if (udev->cmd_time_out) { 324861fb2482SBodo Stroesser list_for_each_entry_safe(cmd, tmp_cmd, 324961fb2482SBodo Stroesser &udev->inflight_queue, 325061fb2482SBodo Stroesser queue_entry) { 325161fb2482SBodo Stroesser tcmu_check_expired_ring_cmd(cmd); 325261fb2482SBodo Stroesser } 325361fb2482SBodo Stroesser tcmu_set_next_deadline(&udev->inflight_queue, 325461fb2482SBodo Stroesser &udev->cmd_timer); 325561fb2482SBodo Stroesser } 325661fb2482SBodo Stroesser list_for_each_entry_safe(cmd, tmp_cmd, &udev->qfull_queue, 325761fb2482SBodo Stroesser queue_entry) { 325861fb2482SBodo Stroesser tcmu_check_expired_queue_cmd(cmd); 325961fb2482SBodo Stroesser } 3260a94a2572SXiubo Li tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer); 3261a94a2572SXiubo Li 32626fddcb77SMike Christie mutex_unlock(&udev->cmdr_lock); 3263488ebe4cSMike Christie 3264488ebe4cSMike Christie spin_lock_bh(&timed_out_udevs_lock); 3265488ebe4cSMike Christie } 3266488ebe4cSMike Christie 3267488ebe4cSMike Christie spin_unlock_bh(&timed_out_udevs_lock); 3268488ebe4cSMike Christie } 3269488ebe4cSMike Christie 32709972cebbSMike Christie static void tcmu_unmap_work_fn(struct work_struct *work) 327189ec9cfdSMike Christie { 3272488ebe4cSMike Christie check_timedout_devices(); 327389ec9cfdSMike Christie find_free_blocks(); 327489ec9cfdSMike Christie } 327589ec9cfdSMike Christie 32767c9e7a6fSAndy Grover static int __init tcmu_module_init(void) 32777c9e7a6fSAndy Grover { 3278801fc54dSBryant G. Ly int ret, i, k, len = 0; 32797c9e7a6fSAndy Grover 32807c9e7a6fSAndy Grover BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0); 32817c9e7a6fSAndy Grover 3282af1dd7ffSMike Christie INIT_DELAYED_WORK(&tcmu_unmap_work, tcmu_unmap_work_fn); 32839972cebbSMike Christie 32847c9e7a6fSAndy Grover tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache", 32857c9e7a6fSAndy Grover sizeof(struct tcmu_cmd), 32867c9e7a6fSAndy Grover __alignof__(struct tcmu_cmd), 32877c9e7a6fSAndy Grover 0, NULL); 32887c9e7a6fSAndy Grover if (!tcmu_cmd_cache) 32897c9e7a6fSAndy Grover return -ENOMEM; 32907c9e7a6fSAndy Grover 32917c9e7a6fSAndy Grover tcmu_root_device = root_device_register("tcm_user"); 32927c9e7a6fSAndy Grover if (IS_ERR(tcmu_root_device)) { 32937c9e7a6fSAndy Grover ret = PTR_ERR(tcmu_root_device); 32947c9e7a6fSAndy Grover goto out_free_cache; 32957c9e7a6fSAndy Grover } 32967c9e7a6fSAndy Grover 32977c9e7a6fSAndy Grover ret = genl_register_family(&tcmu_genl_family); 32987c9e7a6fSAndy Grover if (ret < 0) { 32997c9e7a6fSAndy Grover goto out_unreg_device; 33007c9e7a6fSAndy Grover } 33017c9e7a6fSAndy Grover 33024703b625SBodo Stroesser for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) 33037d7a7435SNicholas Bellinger len += sizeof(struct configfs_attribute *); 33044703b625SBodo Stroesser for (i = 0; passthrough_pr_attrib_attrs[i] != NULL; i++) 3305801fc54dSBryant G. Ly len += sizeof(struct configfs_attribute *); 33064703b625SBodo Stroesser for (i = 0; tcmu_attrib_attrs[i] != NULL; i++) 33074703b625SBodo Stroesser len += sizeof(struct configfs_attribute *); 3308801fc54dSBryant G. Ly len += sizeof(struct configfs_attribute *); 33097d7a7435SNicholas Bellinger 33107d7a7435SNicholas Bellinger tcmu_attrs = kzalloc(len, GFP_KERNEL); 33117d7a7435SNicholas Bellinger if (!tcmu_attrs) { 33127d7a7435SNicholas Bellinger ret = -ENOMEM; 33137d7a7435SNicholas Bellinger goto out_unreg_genl; 33147d7a7435SNicholas Bellinger } 33157d7a7435SNicholas Bellinger 33164703b625SBodo Stroesser for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) 33177d7a7435SNicholas Bellinger tcmu_attrs[i] = passthrough_attrib_attrs[i]; 33184703b625SBodo Stroesser for (k = 0; passthrough_pr_attrib_attrs[k] != NULL; k++) 33194703b625SBodo Stroesser tcmu_attrs[i++] = passthrough_pr_attrib_attrs[k]; 33204703b625SBodo Stroesser for (k = 0; tcmu_attrib_attrs[k] != NULL; k++) 33214703b625SBodo Stroesser tcmu_attrs[i++] = tcmu_attrib_attrs[k]; 33227d7a7435SNicholas Bellinger tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs; 33237d7a7435SNicholas Bellinger 33240a06d430SChristoph Hellwig ret = transport_backend_register(&tcmu_ops); 33257c9e7a6fSAndy Grover if (ret) 33267d7a7435SNicholas Bellinger goto out_attrs; 33277c9e7a6fSAndy Grover 33287c9e7a6fSAndy Grover return 0; 33297c9e7a6fSAndy Grover 33307d7a7435SNicholas Bellinger out_attrs: 33317d7a7435SNicholas Bellinger kfree(tcmu_attrs); 33327c9e7a6fSAndy Grover out_unreg_genl: 33337c9e7a6fSAndy Grover genl_unregister_family(&tcmu_genl_family); 33347c9e7a6fSAndy Grover out_unreg_device: 33357c9e7a6fSAndy Grover root_device_unregister(tcmu_root_device); 33367c9e7a6fSAndy Grover out_free_cache: 33377c9e7a6fSAndy Grover kmem_cache_destroy(tcmu_cmd_cache); 33387c9e7a6fSAndy Grover 33397c9e7a6fSAndy Grover return ret; 33407c9e7a6fSAndy Grover } 33417c9e7a6fSAndy Grover 33427c9e7a6fSAndy Grover static void __exit tcmu_module_exit(void) 33437c9e7a6fSAndy Grover { 3344af1dd7ffSMike Christie cancel_delayed_work_sync(&tcmu_unmap_work); 33450a06d430SChristoph Hellwig target_backend_unregister(&tcmu_ops); 33467d7a7435SNicholas Bellinger kfree(tcmu_attrs); 33477c9e7a6fSAndy Grover genl_unregister_family(&tcmu_genl_family); 33487c9e7a6fSAndy Grover root_device_unregister(tcmu_root_device); 33497c9e7a6fSAndy Grover kmem_cache_destroy(tcmu_cmd_cache); 33507c9e7a6fSAndy Grover } 33517c9e7a6fSAndy Grover 33527c9e7a6fSAndy Grover MODULE_DESCRIPTION("TCM USER subsystem plugin"); 33537c9e7a6fSAndy Grover MODULE_AUTHOR("Shaohua Li <shli@kernel.org>"); 33547c9e7a6fSAndy Grover MODULE_AUTHOR("Andy Grover <agrover@redhat.com>"); 33557c9e7a6fSAndy Grover MODULE_LICENSE("GPL"); 33567c9e7a6fSAndy Grover 33577c9e7a6fSAndy Grover module_init(tcmu_module_init); 33587c9e7a6fSAndy Grover module_exit(tcmu_module_exit); 3359