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 */ 64ecddbb7eSBodo Stroesser #define MB_CMDR_SIZE (8 * 1024 * 1024) 65ecddbb7eSBodo Stroesser /* Offset of cmd ring is size of mailbox */ 66ecddbb7eSBodo Stroesser #define CMDR_OFF sizeof(struct tcmu_mailbox) 67ecddbb7eSBodo Stroesser #define CMDR_SIZE (MB_CMDR_SIZE - 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 1246888da81SMike Christie #define TCM_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 1947c9e7a6fSAndy Grover unsigned long flags; 1957c9e7a6fSAndy Grover }; 196bc2d214aSBodo Stroesser 197bc2d214aSBodo Stroesser struct tcmu_tmr { 198bc2d214aSBodo Stroesser struct list_head queue_entry; 199bc2d214aSBodo Stroesser 200bc2d214aSBodo Stroesser uint8_t tmr_type; 201bc2d214aSBodo Stroesser uint32_t tmr_cmd_cnt; 2028fdaabe1SGustavo A. R. Silva int16_t tmr_cmd_ids[]; 203bc2d214aSBodo Stroesser }; 204bc2d214aSBodo Stroesser 205af1dd7ffSMike Christie /* 206af1dd7ffSMike Christie * To avoid dead lock the mutex lock order should always be: 207af1dd7ffSMike Christie * 208af1dd7ffSMike Christie * mutex_lock(&root_udev_mutex); 209af1dd7ffSMike Christie * ... 210af1dd7ffSMike Christie * mutex_lock(&tcmu_dev->cmdr_lock); 211af1dd7ffSMike Christie * mutex_unlock(&tcmu_dev->cmdr_lock); 212af1dd7ffSMike Christie * ... 213af1dd7ffSMike Christie * mutex_unlock(&root_udev_mutex); 214af1dd7ffSMike Christie */ 215b6df4b79SXiubo Li static DEFINE_MUTEX(root_udev_mutex); 216b6df4b79SXiubo Li static LIST_HEAD(root_udev); 217b6df4b79SXiubo Li 218488ebe4cSMike Christie static DEFINE_SPINLOCK(timed_out_udevs_lock); 219488ebe4cSMike Christie static LIST_HEAD(timed_out_udevs); 220488ebe4cSMike Christie 22180eb8761SMike Christie static struct kmem_cache *tcmu_cmd_cache; 22280eb8761SMike Christie 2238b084d9dSBodo Stroesser static atomic_t global_page_count = ATOMIC_INIT(0); 224af1dd7ffSMike Christie static struct delayed_work tcmu_unmap_work; 2258b084d9dSBodo Stroesser static int tcmu_global_max_pages = TCMU_GLOBAL_MAX_PAGES_DEF; 226b6df4b79SXiubo Li 22780eb8761SMike Christie static int tcmu_set_global_max_data_area(const char *str, 22880eb8761SMike Christie const struct kernel_param *kp) 22980eb8761SMike Christie { 23080eb8761SMike Christie int ret, max_area_mb; 23180eb8761SMike Christie 23280eb8761SMike Christie ret = kstrtoint(str, 10, &max_area_mb); 23380eb8761SMike Christie if (ret) 23480eb8761SMike Christie return -EINVAL; 23580eb8761SMike Christie 23680eb8761SMike Christie if (max_area_mb <= 0) { 23780eb8761SMike Christie pr_err("global_max_data_area must be larger than 0.\n"); 23880eb8761SMike Christie return -EINVAL; 23980eb8761SMike Christie } 24080eb8761SMike Christie 2418b084d9dSBodo Stroesser tcmu_global_max_pages = TCMU_MBS_TO_PAGES(max_area_mb); 2428b084d9dSBodo Stroesser if (atomic_read(&global_page_count) > tcmu_global_max_pages) 24380eb8761SMike Christie schedule_delayed_work(&tcmu_unmap_work, 0); 24480eb8761SMike Christie else 24580eb8761SMike Christie cancel_delayed_work_sync(&tcmu_unmap_work); 24680eb8761SMike Christie 24780eb8761SMike Christie return 0; 24880eb8761SMike Christie } 24980eb8761SMike Christie 25080eb8761SMike Christie static int tcmu_get_global_max_data_area(char *buffer, 25180eb8761SMike Christie const struct kernel_param *kp) 25280eb8761SMike Christie { 2538b084d9dSBodo Stroesser return sprintf(buffer, "%d\n", TCMU_PAGES_TO_MBS(tcmu_global_max_pages)); 25480eb8761SMike Christie } 25580eb8761SMike Christie 25680eb8761SMike Christie static const struct kernel_param_ops tcmu_global_max_data_area_op = { 25780eb8761SMike Christie .set = tcmu_set_global_max_data_area, 25880eb8761SMike Christie .get = tcmu_get_global_max_data_area, 25980eb8761SMike Christie }; 26080eb8761SMike Christie 26180eb8761SMike Christie module_param_cb(global_max_data_area_mb, &tcmu_global_max_data_area_op, NULL, 26280eb8761SMike Christie S_IWUSR | S_IRUGO); 26380eb8761SMike Christie MODULE_PARM_DESC(global_max_data_area_mb, 26480eb8761SMike Christie "Max MBs allowed to be allocated to all the tcmu device's " 26580eb8761SMike Christie "data areas."); 2667c9e7a6fSAndy Grover 267bdaeedc1SMike Christie static int tcmu_get_block_netlink(char *buffer, 268bdaeedc1SMike Christie const struct kernel_param *kp) 269bdaeedc1SMike Christie { 270bdaeedc1SMike Christie return sprintf(buffer, "%s\n", tcmu_netlink_blocked ? 271bdaeedc1SMike Christie "blocked" : "unblocked"); 272bdaeedc1SMike Christie } 273bdaeedc1SMike Christie 274bdaeedc1SMike Christie static int tcmu_set_block_netlink(const char *str, 275bdaeedc1SMike Christie const struct kernel_param *kp) 276bdaeedc1SMike Christie { 277bdaeedc1SMike Christie int ret; 278bdaeedc1SMike Christie u8 val; 279bdaeedc1SMike Christie 280bdaeedc1SMike Christie ret = kstrtou8(str, 0, &val); 281bdaeedc1SMike Christie if (ret < 0) 282bdaeedc1SMike Christie return ret; 283bdaeedc1SMike Christie 284bdaeedc1SMike Christie if (val > 1) { 285bdaeedc1SMike Christie pr_err("Invalid block netlink value %u\n", val); 286bdaeedc1SMike Christie return -EINVAL; 287bdaeedc1SMike Christie } 288bdaeedc1SMike Christie 289bdaeedc1SMike Christie tcmu_netlink_blocked = val; 290bdaeedc1SMike Christie return 0; 291bdaeedc1SMike Christie } 292bdaeedc1SMike Christie 293bdaeedc1SMike Christie static const struct kernel_param_ops tcmu_block_netlink_op = { 294bdaeedc1SMike Christie .set = tcmu_set_block_netlink, 295bdaeedc1SMike Christie .get = tcmu_get_block_netlink, 296bdaeedc1SMike Christie }; 297bdaeedc1SMike Christie 298bdaeedc1SMike Christie module_param_cb(block_netlink, &tcmu_block_netlink_op, NULL, S_IWUSR | S_IRUGO); 299bdaeedc1SMike Christie MODULE_PARM_DESC(block_netlink, "Block new netlink commands."); 300bdaeedc1SMike Christie 301bdaeedc1SMike Christie static int tcmu_fail_netlink_cmd(struct tcmu_nl_cmd *nl_cmd) 302bdaeedc1SMike Christie { 303bdaeedc1SMike Christie struct tcmu_dev *udev = nl_cmd->udev; 304bdaeedc1SMike Christie 305bdaeedc1SMike Christie if (!tcmu_netlink_blocked) { 306bdaeedc1SMike Christie pr_err("Could not reset device's netlink interface. Netlink is not blocked.\n"); 307bdaeedc1SMike Christie return -EBUSY; 308bdaeedc1SMike Christie } 309bdaeedc1SMike Christie 310bdaeedc1SMike Christie if (nl_cmd->cmd != TCMU_CMD_UNSPEC) { 311bdaeedc1SMike Christie pr_debug("Aborting nl cmd %d on %s\n", nl_cmd->cmd, udev->name); 312bdaeedc1SMike Christie nl_cmd->status = -EINTR; 313bdaeedc1SMike Christie list_del(&nl_cmd->nl_list); 314bdaeedc1SMike Christie complete(&nl_cmd->complete); 315bdaeedc1SMike Christie } 316bdaeedc1SMike Christie return 0; 317bdaeedc1SMike Christie } 318bdaeedc1SMike Christie 319bdaeedc1SMike Christie static int tcmu_set_reset_netlink(const char *str, 320bdaeedc1SMike Christie const struct kernel_param *kp) 321bdaeedc1SMike Christie { 322bdaeedc1SMike Christie struct tcmu_nl_cmd *nl_cmd, *tmp_cmd; 323bdaeedc1SMike Christie int ret; 324bdaeedc1SMike Christie u8 val; 325bdaeedc1SMike Christie 326bdaeedc1SMike Christie ret = kstrtou8(str, 0, &val); 327bdaeedc1SMike Christie if (ret < 0) 328bdaeedc1SMike Christie return ret; 329bdaeedc1SMike Christie 330bdaeedc1SMike Christie if (val != 1) { 331bdaeedc1SMike Christie pr_err("Invalid reset netlink value %u\n", val); 332bdaeedc1SMike Christie return -EINVAL; 333bdaeedc1SMike Christie } 334bdaeedc1SMike Christie 335bdaeedc1SMike Christie mutex_lock(&tcmu_nl_cmd_mutex); 336bdaeedc1SMike Christie list_for_each_entry_safe(nl_cmd, tmp_cmd, &tcmu_nl_cmd_list, nl_list) { 337bdaeedc1SMike Christie ret = tcmu_fail_netlink_cmd(nl_cmd); 338bdaeedc1SMike Christie if (ret) 339bdaeedc1SMike Christie break; 340bdaeedc1SMike Christie } 341bdaeedc1SMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 342bdaeedc1SMike Christie 343bdaeedc1SMike Christie return ret; 344bdaeedc1SMike Christie } 345bdaeedc1SMike Christie 346bdaeedc1SMike Christie static const struct kernel_param_ops tcmu_reset_netlink_op = { 347bdaeedc1SMike Christie .set = tcmu_set_reset_netlink, 348bdaeedc1SMike Christie }; 349bdaeedc1SMike Christie 350bdaeedc1SMike Christie module_param_cb(reset_netlink, &tcmu_reset_netlink_op, NULL, S_IWUSR); 351bdaeedc1SMike Christie MODULE_PARM_DESC(reset_netlink, "Reset netlink commands."); 352bdaeedc1SMike Christie 3537c9e7a6fSAndy Grover /* multicast group */ 3547c9e7a6fSAndy Grover enum tcmu_multicast_groups { 3557c9e7a6fSAndy Grover TCMU_MCGRP_CONFIG, 3567c9e7a6fSAndy Grover }; 3577c9e7a6fSAndy Grover 3587c9e7a6fSAndy Grover static const struct genl_multicast_group tcmu_mcgrps[] = { 3597c9e7a6fSAndy Grover [TCMU_MCGRP_CONFIG] = { .name = "config", }, 3607c9e7a6fSAndy Grover }; 3617c9e7a6fSAndy Grover 362b3af66e2SMike Christie static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = { 363b3af66e2SMike Christie [TCMU_ATTR_DEVICE] = { .type = NLA_STRING }, 364b3af66e2SMike Christie [TCMU_ATTR_MINOR] = { .type = NLA_U32 }, 365b3af66e2SMike Christie [TCMU_ATTR_CMD_STATUS] = { .type = NLA_S32 }, 366b3af66e2SMike Christie [TCMU_ATTR_DEVICE_ID] = { .type = NLA_U32 }, 367b3af66e2SMike Christie [TCMU_ATTR_SUPP_KERN_CMD_REPLY] = { .type = NLA_U8 }, 368b3af66e2SMike Christie }; 369b3af66e2SMike Christie 370b3af66e2SMike Christie static int tcmu_genl_cmd_done(struct genl_info *info, int completed_cmd) 371b3af66e2SMike Christie { 3723228691fSMike Christie struct tcmu_dev *udev = NULL; 373b3af66e2SMike Christie struct tcmu_nl_cmd *nl_cmd; 374b3af66e2SMike Christie int dev_id, rc, ret = 0; 375b3af66e2SMike Christie 376b3af66e2SMike Christie if (!info->attrs[TCMU_ATTR_CMD_STATUS] || 377b3af66e2SMike Christie !info->attrs[TCMU_ATTR_DEVICE_ID]) { 378b3af66e2SMike Christie printk(KERN_ERR "TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n"); 379b3af66e2SMike Christie return -EINVAL; 380b3af66e2SMike Christie } 381b3af66e2SMike Christie 382b3af66e2SMike Christie dev_id = nla_get_u32(info->attrs[TCMU_ATTR_DEVICE_ID]); 383b3af66e2SMike Christie rc = nla_get_s32(info->attrs[TCMU_ATTR_CMD_STATUS]); 384b3af66e2SMike Christie 3853228691fSMike Christie mutex_lock(&tcmu_nl_cmd_mutex); 3863228691fSMike Christie list_for_each_entry(nl_cmd, &tcmu_nl_cmd_list, nl_list) { 3873228691fSMike Christie if (nl_cmd->udev->se_dev.dev_index == dev_id) { 3883228691fSMike Christie udev = nl_cmd->udev; 3893228691fSMike Christie break; 390b3af66e2SMike Christie } 3913228691fSMike Christie } 392b3af66e2SMike Christie 3933228691fSMike Christie if (!udev) { 3940c218e16SMike Christie pr_err("tcmu nl cmd %u/%d completion could not find device with dev id %u.\n", 3953228691fSMike Christie completed_cmd, rc, dev_id); 3963228691fSMike Christie ret = -ENODEV; 3973228691fSMike Christie goto unlock; 3983228691fSMike Christie } 3993228691fSMike Christie list_del(&nl_cmd->nl_list); 400b3af66e2SMike Christie 401bdaeedc1SMike Christie pr_debug("%s genl cmd done got id %d curr %d done %d rc %d stat %d\n", 402bdaeedc1SMike Christie udev->name, dev_id, nl_cmd->cmd, completed_cmd, rc, 403bdaeedc1SMike Christie nl_cmd->status); 404b3af66e2SMike Christie 405b3af66e2SMike Christie if (nl_cmd->cmd != completed_cmd) { 4063228691fSMike Christie pr_err("Mismatched commands on %s (Expecting reply for %d. Current %d).\n", 4073228691fSMike Christie udev->name, completed_cmd, nl_cmd->cmd); 408b3af66e2SMike Christie ret = -EINVAL; 4093228691fSMike Christie goto unlock; 410b3af66e2SMike Christie } 411b3af66e2SMike Christie 4123228691fSMike Christie nl_cmd->status = rc; 413b3af66e2SMike Christie complete(&nl_cmd->complete); 4143228691fSMike Christie unlock: 4153228691fSMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 416b3af66e2SMike Christie return ret; 417b3af66e2SMike Christie } 418b3af66e2SMike Christie 419b3af66e2SMike Christie static int tcmu_genl_rm_dev_done(struct sk_buff *skb, struct genl_info *info) 420b3af66e2SMike Christie { 421b3af66e2SMike Christie return tcmu_genl_cmd_done(info, TCMU_CMD_REMOVED_DEVICE); 422b3af66e2SMike Christie } 423b3af66e2SMike Christie 424b3af66e2SMike Christie static int tcmu_genl_add_dev_done(struct sk_buff *skb, struct genl_info *info) 425b3af66e2SMike Christie { 426b3af66e2SMike Christie return tcmu_genl_cmd_done(info, TCMU_CMD_ADDED_DEVICE); 427b3af66e2SMike Christie } 428b3af66e2SMike Christie 429b3af66e2SMike Christie static int tcmu_genl_reconfig_dev_done(struct sk_buff *skb, 430b3af66e2SMike Christie struct genl_info *info) 431b3af66e2SMike Christie { 432b3af66e2SMike Christie return tcmu_genl_cmd_done(info, TCMU_CMD_RECONFIG_DEVICE); 433b3af66e2SMike Christie } 434b3af66e2SMike Christie 435b3af66e2SMike Christie static int tcmu_genl_set_features(struct sk_buff *skb, struct genl_info *info) 436b3af66e2SMike Christie { 437b3af66e2SMike Christie if (info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]) { 438b3af66e2SMike Christie tcmu_kern_cmd_reply_supported = 439b3af66e2SMike Christie nla_get_u8(info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]); 440b3af66e2SMike Christie printk(KERN_INFO "tcmu daemon: command reply support %u.\n", 441b3af66e2SMike Christie tcmu_kern_cmd_reply_supported); 442b3af66e2SMike Christie } 443b3af66e2SMike Christie 444b3af66e2SMike Christie return 0; 445b3af66e2SMike Christie } 446b3af66e2SMike Christie 44766a9b928SJakub Kicinski static const struct genl_small_ops tcmu_genl_ops[] = { 448b3af66e2SMike Christie { 449b3af66e2SMike Christie .cmd = TCMU_CMD_SET_FEATURES, 450ef6243acSJohannes Berg .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 451b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 452b3af66e2SMike Christie .doit = tcmu_genl_set_features, 453b3af66e2SMike Christie }, 454b3af66e2SMike Christie { 455b3af66e2SMike Christie .cmd = TCMU_CMD_ADDED_DEVICE_DONE, 456ef6243acSJohannes Berg .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 457b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 458b3af66e2SMike Christie .doit = tcmu_genl_add_dev_done, 459b3af66e2SMike Christie }, 460b3af66e2SMike Christie { 461b3af66e2SMike Christie .cmd = TCMU_CMD_REMOVED_DEVICE_DONE, 462ef6243acSJohannes Berg .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 463b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 464b3af66e2SMike Christie .doit = tcmu_genl_rm_dev_done, 465b3af66e2SMike Christie }, 466b3af66e2SMike Christie { 467b3af66e2SMike Christie .cmd = TCMU_CMD_RECONFIG_DEVICE_DONE, 468ef6243acSJohannes Berg .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 469b3af66e2SMike Christie .flags = GENL_ADMIN_PERM, 470b3af66e2SMike Christie .doit = tcmu_genl_reconfig_dev_done, 471b3af66e2SMike Christie }, 472b3af66e2SMike Christie }; 473b3af66e2SMike Christie 4747c9e7a6fSAndy Grover /* Our generic netlink family */ 47556989f6dSJohannes Berg static struct genl_family tcmu_genl_family __ro_after_init = { 476489111e5SJohannes Berg .module = THIS_MODULE, 4777c9e7a6fSAndy Grover .hdrsize = 0, 4787c9e7a6fSAndy Grover .name = "TCM-USER", 479b3af66e2SMike Christie .version = 2, 4807c9e7a6fSAndy Grover .maxattr = TCMU_ATTR_MAX, 4813b0f31f2SJohannes Berg .policy = tcmu_attr_policy, 4827c9e7a6fSAndy Grover .mcgrps = tcmu_mcgrps, 4837c9e7a6fSAndy Grover .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps), 48420c08b36SSheng Yang .netnsok = true, 48566a9b928SJakub Kicinski .small_ops = tcmu_genl_ops, 48666a9b928SJakub Kicinski .n_small_ops = ARRAY_SIZE(tcmu_genl_ops), 4877c9e7a6fSAndy Grover }; 4887c9e7a6fSAndy Grover 489141685a3SXiubo Li #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index)) 490141685a3SXiubo Li #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0) 491141685a3SXiubo Li #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index)) 492141685a3SXiubo Li #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++]) 493141685a3SXiubo Li 494b6df4b79SXiubo Li static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len) 495141685a3SXiubo Li { 496141685a3SXiubo Li struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 497141685a3SXiubo Li uint32_t i; 498141685a3SXiubo Li 499b6df4b79SXiubo Li for (i = 0; i < len; i++) 500141685a3SXiubo Li clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap); 501141685a3SXiubo Li } 502141685a3SXiubo Li 5037e98905eSBodo Stroesser static inline int tcmu_get_empty_block(struct tcmu_dev *udev, 5047e98905eSBodo Stroesser struct tcmu_cmd *tcmu_cmd, 505f5ce815fSBodo Stroesser int prev_dbi, int length, int *iov_cnt) 506141685a3SXiubo Li { 507f5ce815fSBodo Stroesser XA_STATE(xas, &udev->data_pages, 0); 508b6df4b79SXiubo Li struct page *page; 509e719afdcSBodo Stroesser int i, cnt, dbi, dpi; 510f5ce815fSBodo Stroesser int page_cnt = DIV_ROUND_UP(length, PAGE_SIZE); 511141685a3SXiubo Li 512b6df4b79SXiubo Li dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh); 513b6df4b79SXiubo Li if (dbi == udev->dbi_thresh) 5147e98905eSBodo Stroesser return -1; 515b6df4b79SXiubo Li 516e719afdcSBodo Stroesser dpi = dbi * udev->data_pages_per_blk; 517f5ce815fSBodo Stroesser /* Count the number of already allocated pages */ 518e719afdcSBodo Stroesser xas_set(&xas, dpi); 519*b4150b68SBodo Stroesser rcu_read_lock(); 520f5ce815fSBodo Stroesser for (cnt = 0; xas_next(&xas) && cnt < page_cnt;) 521f5ce815fSBodo Stroesser cnt++; 522*b4150b68SBodo Stroesser rcu_read_unlock(); 523b6df4b79SXiubo Li 524f5ce815fSBodo Stroesser for (i = cnt; i < page_cnt; i++) { 525b6df4b79SXiubo Li /* try to get new page from the mm */ 5260eccce86SDamien Le Moal page = alloc_page(GFP_NOIO); 527b6df4b79SXiubo Li if (!page) 528f5ce815fSBodo Stroesser break; 529b6df4b79SXiubo Li 530e719afdcSBodo Stroesser if (xa_store(&udev->data_pages, dpi + i, page, GFP_NOIO)) { 531f5ce815fSBodo Stroesser __free_page(page); 532f5ce815fSBodo Stroesser break; 533b6df4b79SXiubo Li } 534f5ce815fSBodo Stroesser } 535f5ce815fSBodo Stroesser if (atomic_add_return(i - cnt, &global_page_count) > 536f5ce815fSBodo Stroesser tcmu_global_max_pages) 537f5ce815fSBodo Stroesser schedule_delayed_work(&tcmu_unmap_work, 0); 538b6df4b79SXiubo Li 539f5ce815fSBodo Stroesser if (i && dbi > udev->dbi_max) 540141685a3SXiubo Li udev->dbi_max = dbi; 541141685a3SXiubo Li 542141685a3SXiubo Li set_bit(dbi, udev->data_bitmap); 543b6df4b79SXiubo Li tcmu_cmd_set_dbi(tcmu_cmd, dbi); 544141685a3SXiubo Li 5457e98905eSBodo Stroesser if (dbi != prev_dbi + 1) 5467e98905eSBodo Stroesser *iov_cnt += 1; 5477e98905eSBodo Stroesser 548f5ce815fSBodo Stroesser return i == page_cnt ? dbi : -1; 549141685a3SXiubo Li } 550141685a3SXiubo Li 5517e98905eSBodo Stroesser static int tcmu_get_empty_blocks(struct tcmu_dev *udev, 552f5ce815fSBodo Stroesser struct tcmu_cmd *tcmu_cmd, int length) 553b6df4b79SXiubo Li { 5547e98905eSBodo Stroesser /* start value of dbi + 1 must not be a valid dbi */ 5557e98905eSBodo Stroesser int dbi = -2; 556e719afdcSBodo Stroesser int blk_data_len, iov_cnt = 0; 557e719afdcSBodo Stroesser uint32_t blk_size = udev->data_blk_size; 558b6df4b79SXiubo Li 559e719afdcSBodo Stroesser for (; length > 0; length -= blk_size) { 560e719afdcSBodo Stroesser blk_data_len = min_t(uint32_t, length, blk_size); 561e719afdcSBodo Stroesser dbi = tcmu_get_empty_block(udev, tcmu_cmd, dbi, blk_data_len, 562e719afdcSBodo Stroesser &iov_cnt); 5637e98905eSBodo Stroesser if (dbi < 0) 5647e98905eSBodo Stroesser return -1; 565141685a3SXiubo Li } 5667e98905eSBodo Stroesser return iov_cnt; 567141685a3SXiubo Li } 568141685a3SXiubo Li 569141685a3SXiubo Li static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd) 570141685a3SXiubo Li { 571141685a3SXiubo Li kfree(tcmu_cmd->dbi); 572141685a3SXiubo Li kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); 573141685a3SXiubo Li } 574141685a3SXiubo Li 57552ef2743SBodo Stroesser static inline void tcmu_cmd_set_block_cnts(struct tcmu_cmd *cmd) 576141685a3SXiubo Li { 57752ef2743SBodo Stroesser int i, len; 57852ef2743SBodo Stroesser struct se_cmd *se_cmd = cmd->se_cmd; 579e719afdcSBodo Stroesser uint32_t blk_size = cmd->tcmu_dev->data_blk_size; 58052ef2743SBodo Stroesser 581e719afdcSBodo Stroesser cmd->dbi_cnt = DIV_ROUND_UP(se_cmd->data_length, blk_size); 582141685a3SXiubo Li 583141685a3SXiubo Li if (se_cmd->se_cmd_flags & SCF_BIDI) { 584141685a3SXiubo Li BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents)); 58552ef2743SBodo Stroesser for (i = 0, len = 0; i < se_cmd->t_bidi_data_nents; i++) 58652ef2743SBodo Stroesser len += se_cmd->t_bidi_data_sg[i].length; 587e719afdcSBodo Stroesser cmd->dbi_bidi_cnt = DIV_ROUND_UP(len, blk_size); 58852ef2743SBodo Stroesser cmd->dbi_cnt += cmd->dbi_bidi_cnt; 5893c9a7c58SBodo Stroesser cmd->data_len_bidi = len; 590141685a3SXiubo Li } 591141685a3SXiubo Li } 592141685a3SXiubo Li 5933c9a7c58SBodo Stroesser static int new_block_to_iov(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 594c8ed1ff8SBodo Stroesser struct iovec **iov, int prev_dbi, int len) 5953c9a7c58SBodo Stroesser { 5963c9a7c58SBodo Stroesser /* Get the next dbi */ 5973c9a7c58SBodo Stroesser int dbi = tcmu_cmd_get_dbi(cmd); 5983c9a7c58SBodo Stroesser 599e719afdcSBodo Stroesser /* Do not add more than udev->data_blk_size to iov */ 600e719afdcSBodo Stroesser len = min_t(int, len, udev->data_blk_size); 601c8ed1ff8SBodo Stroesser 6023c9a7c58SBodo Stroesser /* 6033c9a7c58SBodo Stroesser * The following code will gather and map the blocks to the same iovec 6043c9a7c58SBodo Stroesser * when the blocks are all next to each other. 6053c9a7c58SBodo Stroesser */ 6063c9a7c58SBodo Stroesser if (dbi != prev_dbi + 1) { 6073c9a7c58SBodo Stroesser /* dbi is not next to previous dbi, so start new iov */ 6083c9a7c58SBodo Stroesser if (prev_dbi >= 0) 6093c9a7c58SBodo Stroesser (*iov)++; 6103c9a7c58SBodo Stroesser /* write offset relative to mb_addr */ 6113c9a7c58SBodo Stroesser (*iov)->iov_base = (void __user *) 612e719afdcSBodo Stroesser (udev->data_off + dbi * udev->data_blk_size); 6133c9a7c58SBodo Stroesser } 6143c9a7c58SBodo Stroesser (*iov)->iov_len += len; 6153c9a7c58SBodo Stroesser 6163c9a7c58SBodo Stroesser return dbi; 6173c9a7c58SBodo Stroesser } 6183c9a7c58SBodo Stroesser 6193c9a7c58SBodo Stroesser static void tcmu_setup_iovs(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 6203c9a7c58SBodo Stroesser struct iovec **iov, int data_length) 6213c9a7c58SBodo Stroesser { 6223c9a7c58SBodo Stroesser /* start value of dbi + 1 must not be a valid dbi */ 6233c9a7c58SBodo Stroesser int dbi = -2; 6243c9a7c58SBodo Stroesser 6253c9a7c58SBodo Stroesser /* We prepare the IOVs for DMA_FROM_DEVICE transfer direction */ 626e719afdcSBodo Stroesser for (; data_length > 0; data_length -= udev->data_blk_size) 627c8ed1ff8SBodo Stroesser dbi = new_block_to_iov(udev, cmd, iov, dbi, data_length); 6283c9a7c58SBodo Stroesser } 6293c9a7c58SBodo Stroesser 6307c9e7a6fSAndy Grover static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd) 6317c9e7a6fSAndy Grover { 6327c9e7a6fSAndy Grover struct se_device *se_dev = se_cmd->se_dev; 6337c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(se_dev); 6347c9e7a6fSAndy Grover struct tcmu_cmd *tcmu_cmd; 6357c9e7a6fSAndy Grover 6360eccce86SDamien Le Moal tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_NOIO); 6377c9e7a6fSAndy Grover if (!tcmu_cmd) 6387c9e7a6fSAndy Grover return NULL; 6397c9e7a6fSAndy Grover 640a94a2572SXiubo Li INIT_LIST_HEAD(&tcmu_cmd->queue_entry); 6417c9e7a6fSAndy Grover tcmu_cmd->se_cmd = se_cmd; 6427c9e7a6fSAndy Grover tcmu_cmd->tcmu_dev = udev; 6437c9e7a6fSAndy Grover 64452ef2743SBodo Stroesser tcmu_cmd_set_block_cnts(tcmu_cmd); 645141685a3SXiubo Li tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t), 6460eccce86SDamien Le Moal GFP_NOIO); 647141685a3SXiubo Li if (!tcmu_cmd->dbi) { 648141685a3SXiubo Li kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); 649141685a3SXiubo Li return NULL; 650141685a3SXiubo Li } 651141685a3SXiubo Li 6527c9e7a6fSAndy Grover return tcmu_cmd; 6537c9e7a6fSAndy Grover } 6547c9e7a6fSAndy Grover 6557c9e7a6fSAndy Grover static inline void tcmu_flush_dcache_range(void *vaddr, size_t size) 6567c9e7a6fSAndy Grover { 657b75d8063SGeliang Tang unsigned long offset = offset_in_page(vaddr); 65826d2b310Stangwenji void *start = vaddr - offset; 6597c9e7a6fSAndy Grover 6607c9e7a6fSAndy Grover size = round_up(size+offset, PAGE_SIZE); 6617c9e7a6fSAndy Grover 6627c9e7a6fSAndy Grover while (size) { 6633145550aSBodo Stroesser flush_dcache_page(vmalloc_to_page(start)); 66426d2b310Stangwenji start += PAGE_SIZE; 6657c9e7a6fSAndy Grover size -= PAGE_SIZE; 6667c9e7a6fSAndy Grover } 6677c9e7a6fSAndy Grover } 6687c9e7a6fSAndy Grover 6697c9e7a6fSAndy Grover /* 6707c9e7a6fSAndy Grover * Some ring helper functions. We don't assume size is a power of 2 so 6717c9e7a6fSAndy Grover * we can't use circ_buf.h. 6727c9e7a6fSAndy Grover */ 6737c9e7a6fSAndy Grover static inline size_t spc_used(size_t head, size_t tail, size_t size) 6747c9e7a6fSAndy Grover { 6757c9e7a6fSAndy Grover int diff = head - tail; 6767c9e7a6fSAndy Grover 6777c9e7a6fSAndy Grover if (diff >= 0) 6787c9e7a6fSAndy Grover return diff; 6797c9e7a6fSAndy Grover else 6807c9e7a6fSAndy Grover return size + diff; 6817c9e7a6fSAndy Grover } 6827c9e7a6fSAndy Grover 6837c9e7a6fSAndy Grover static inline size_t spc_free(size_t head, size_t tail, size_t size) 6847c9e7a6fSAndy Grover { 6857c9e7a6fSAndy Grover /* Keep 1 byte unused or we can't tell full from empty */ 6867c9e7a6fSAndy Grover return (size - spc_used(head, tail, size) - 1); 6877c9e7a6fSAndy Grover } 6887c9e7a6fSAndy Grover 6897c9e7a6fSAndy Grover static inline size_t head_to_end(size_t head, size_t size) 6907c9e7a6fSAndy Grover { 6917c9e7a6fSAndy Grover return size - head; 6927c9e7a6fSAndy Grover } 6937c9e7a6fSAndy Grover 6947c9e7a6fSAndy Grover #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size) 6957c9e7a6fSAndy Grover 696c8ed1ff8SBodo Stroesser #define TCMU_SG_TO_DATA_AREA 1 697c8ed1ff8SBodo Stroesser #define TCMU_DATA_AREA_TO_SG 2 698c8ed1ff8SBodo Stroesser 699c8ed1ff8SBodo Stroesser static inline void tcmu_copy_data(struct tcmu_dev *udev, 700c8ed1ff8SBodo Stroesser struct tcmu_cmd *tcmu_cmd, uint32_t direction, 701c8ed1ff8SBodo Stroesser struct scatterlist *sg, unsigned int sg_nents, 702c8ed1ff8SBodo Stroesser struct iovec **iov, size_t data_len) 703c8ed1ff8SBodo Stroesser { 704c8ed1ff8SBodo Stroesser /* start value of dbi + 1 must not be a valid dbi */ 705c8ed1ff8SBodo Stroesser int dbi = -2; 706f5ce815fSBodo Stroesser size_t page_remaining, cp_len; 707*b4150b68SBodo Stroesser int page_cnt, page_inx, dpi; 708c8ed1ff8SBodo Stroesser struct sg_mapping_iter sg_iter; 709c8ed1ff8SBodo Stroesser unsigned int sg_flags; 710c8ed1ff8SBodo Stroesser struct page *page; 711c8ed1ff8SBodo Stroesser void *data_page_start, *data_addr; 712c8ed1ff8SBodo Stroesser 713c8ed1ff8SBodo Stroesser if (direction == TCMU_SG_TO_DATA_AREA) 714c8ed1ff8SBodo Stroesser sg_flags = SG_MITER_ATOMIC | SG_MITER_FROM_SG; 715c8ed1ff8SBodo Stroesser else 716c8ed1ff8SBodo Stroesser sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG; 717c8ed1ff8SBodo Stroesser sg_miter_start(&sg_iter, sg, sg_nents, sg_flags); 718c8ed1ff8SBodo Stroesser 719c8ed1ff8SBodo Stroesser while (data_len) { 720c8ed1ff8SBodo Stroesser if (direction == TCMU_SG_TO_DATA_AREA) 721c8ed1ff8SBodo Stroesser dbi = new_block_to_iov(udev, tcmu_cmd, iov, dbi, 722c8ed1ff8SBodo Stroesser data_len); 723c8ed1ff8SBodo Stroesser else 724c8ed1ff8SBodo Stroesser dbi = tcmu_cmd_get_dbi(tcmu_cmd); 725f5ce815fSBodo Stroesser 726f5ce815fSBodo Stroesser page_cnt = DIV_ROUND_UP(data_len, PAGE_SIZE); 727e719afdcSBodo Stroesser if (page_cnt > udev->data_pages_per_blk) 728e719afdcSBodo Stroesser page_cnt = udev->data_pages_per_blk; 729f5ce815fSBodo Stroesser 730*b4150b68SBodo Stroesser dpi = dbi * udev->data_pages_per_blk; 731*b4150b68SBodo Stroesser for (page_inx = 0; page_inx < page_cnt && data_len; 732*b4150b68SBodo Stroesser page_inx++, dpi++) { 733*b4150b68SBodo Stroesser page = xa_load(&udev->data_pages, dpi); 734f5ce815fSBodo Stroesser 735c8ed1ff8SBodo Stroesser if (direction == TCMU_DATA_AREA_TO_SG) 736c8ed1ff8SBodo Stroesser flush_dcache_page(page); 737c8ed1ff8SBodo Stroesser data_page_start = kmap_atomic(page); 738f5ce815fSBodo Stroesser page_remaining = PAGE_SIZE; 739c8ed1ff8SBodo Stroesser 740f5ce815fSBodo Stroesser while (page_remaining && data_len) { 741c8ed1ff8SBodo Stroesser if (!sg_miter_next(&sg_iter)) { 742c8ed1ff8SBodo Stroesser /* set length to 0 to abort outer loop */ 743c8ed1ff8SBodo Stroesser data_len = 0; 744f5ce815fSBodo Stroesser pr_debug("%s: aborting data copy due to exhausted sg_list\n", 745f5ce815fSBodo Stroesser __func__); 746c8ed1ff8SBodo Stroesser break; 747c8ed1ff8SBodo Stroesser } 748f5ce815fSBodo Stroesser cp_len = min3(sg_iter.length, page_remaining, 749f5ce815fSBodo Stroesser data_len); 750c8ed1ff8SBodo Stroesser 751c8ed1ff8SBodo Stroesser data_addr = data_page_start + 752f5ce815fSBodo Stroesser PAGE_SIZE - page_remaining; 753c8ed1ff8SBodo Stroesser if (direction == TCMU_SG_TO_DATA_AREA) 754c8ed1ff8SBodo Stroesser memcpy(data_addr, sg_iter.addr, cp_len); 755c8ed1ff8SBodo Stroesser else 756c8ed1ff8SBodo Stroesser memcpy(sg_iter.addr, data_addr, cp_len); 757c8ed1ff8SBodo Stroesser 758c8ed1ff8SBodo Stroesser data_len -= cp_len; 759f5ce815fSBodo Stroesser page_remaining -= cp_len; 760c8ed1ff8SBodo Stroesser sg_iter.consumed = cp_len; 761c8ed1ff8SBodo Stroesser } 762c8ed1ff8SBodo Stroesser sg_miter_stop(&sg_iter); 763c8ed1ff8SBodo Stroesser 764c8ed1ff8SBodo Stroesser kunmap_atomic(data_page_start); 765c8ed1ff8SBodo Stroesser if (direction == TCMU_SG_TO_DATA_AREA) 766c8ed1ff8SBodo Stroesser flush_dcache_page(page); 767c8ed1ff8SBodo Stroesser } 768c8ed1ff8SBodo Stroesser } 769f5ce815fSBodo Stroesser } 770c8ed1ff8SBodo Stroesser 7713c9a7c58SBodo Stroesser static void scatter_data_area(struct tcmu_dev *udev, struct tcmu_cmd *tcmu_cmd, 7723c9a7c58SBodo Stroesser struct iovec **iov) 77326418649SSheng Yang { 7743c9a7c58SBodo Stroesser struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 775f97ec7dbSIlias Tsitsimpis 776c8ed1ff8SBodo Stroesser tcmu_copy_data(udev, tcmu_cmd, TCMU_SG_TO_DATA_AREA, se_cmd->t_data_sg, 777c8ed1ff8SBodo Stroesser se_cmd->t_data_nents, iov, se_cmd->data_length); 7783c58f737SBodo Stroesser } 779b6df4b79SXiubo Li 780c8ed1ff8SBodo Stroesser static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *tcmu_cmd, 7816c3796d1Sbstroesser@ts.fujitsu.com bool bidi, uint32_t read_len) 782f97ec7dbSIlias Tsitsimpis { 783c8ed1ff8SBodo Stroesser struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 784c8ed1ff8SBodo Stroesser struct scatterlist *data_sg; 785a5d68ba8SXiubo Li unsigned int data_nents; 786a5d68ba8SXiubo Li 787a5d68ba8SXiubo Li if (!bidi) { 788a5d68ba8SXiubo Li data_sg = se_cmd->t_data_sg; 789a5d68ba8SXiubo Li data_nents = se_cmd->t_data_nents; 790a5d68ba8SXiubo Li } else { 791a5d68ba8SXiubo Li /* 792a5d68ba8SXiubo Li * For bidi case, the first count blocks are for Data-Out 793a5d68ba8SXiubo Li * buffer blocks, and before gathering the Data-In buffer 79452ef2743SBodo Stroesser * the Data-Out buffer blocks should be skipped. 795a5d68ba8SXiubo Li */ 796c8ed1ff8SBodo Stroesser tcmu_cmd_set_dbi_cur(tcmu_cmd, 797c8ed1ff8SBodo Stroesser tcmu_cmd->dbi_cnt - tcmu_cmd->dbi_bidi_cnt); 798a5d68ba8SXiubo Li 799a5d68ba8SXiubo Li data_sg = se_cmd->t_bidi_data_sg; 800a5d68ba8SXiubo Li data_nents = se_cmd->t_bidi_data_nents; 801a5d68ba8SXiubo Li } 802f97ec7dbSIlias Tsitsimpis 803c8ed1ff8SBodo Stroesser tcmu_copy_data(udev, tcmu_cmd, TCMU_DATA_AREA_TO_SG, data_sg, 804c8ed1ff8SBodo Stroesser data_nents, NULL, read_len); 805f97ec7dbSIlias Tsitsimpis } 806f97ec7dbSIlias Tsitsimpis 807b6df4b79SXiubo Li static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh) 80826418649SSheng Yang { 8093c0f26ffSMike Christie return thresh - bitmap_weight(bitmap, thresh); 81026418649SSheng Yang } 81126418649SSheng Yang 8127c9e7a6fSAndy Grover /* 8137e98905eSBodo Stroesser * We can't queue a command until we have space available on the cmd ring. 8147c9e7a6fSAndy Grover * 8157c9e7a6fSAndy Grover * Called with ring lock held. 8167c9e7a6fSAndy Grover */ 8177e98905eSBodo Stroesser static bool is_ring_space_avail(struct tcmu_dev *udev, size_t cmd_size) 8187c9e7a6fSAndy Grover { 8197c9e7a6fSAndy Grover struct tcmu_mailbox *mb = udev->mb_addr; 8200241fd39SNicholas Bellinger size_t space, cmd_needed; 8217c9e7a6fSAndy Grover u32 cmd_head; 8227c9e7a6fSAndy Grover 8237c9e7a6fSAndy Grover tcmu_flush_dcache_range(mb, sizeof(*mb)); 8247c9e7a6fSAndy Grover 8257c9e7a6fSAndy Grover cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 8267c9e7a6fSAndy Grover 827f56574a2SAndy Grover /* 828f56574a2SAndy Grover * If cmd end-of-ring space is too small then we need space for a NOP plus 829f56574a2SAndy Grover * original cmd - cmds are internally contiguous. 830f56574a2SAndy Grover */ 831f56574a2SAndy Grover if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size) 832f56574a2SAndy Grover cmd_needed = cmd_size; 833f56574a2SAndy Grover else 834f56574a2SAndy Grover cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size); 835f56574a2SAndy Grover 8367c9e7a6fSAndy Grover space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size); 8377c9e7a6fSAndy Grover if (space < cmd_needed) { 8387c9e7a6fSAndy Grover pr_debug("no cmd space: %u %u %u\n", cmd_head, 8397c9e7a6fSAndy Grover udev->cmdr_last_cleaned, udev->cmdr_size); 8407c9e7a6fSAndy Grover return false; 8417c9e7a6fSAndy Grover } 842bc2d214aSBodo Stroesser return true; 8437e98905eSBodo Stroesser } 8447e98905eSBodo Stroesser 8457e98905eSBodo Stroesser /* 8467e98905eSBodo Stroesser * We have to allocate data buffers before we can queue a command. 8477e98905eSBodo Stroesser * Returns -1 on error (not enough space) or number of needed iovs on success 8487e98905eSBodo Stroesser * 8497e98905eSBodo Stroesser * Called with ring lock held. 8507e98905eSBodo Stroesser */ 8517e98905eSBodo Stroesser static int tcmu_alloc_data_space(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 8527e98905eSBodo Stroesser int *iov_bidi_cnt) 8537e98905eSBodo Stroesser { 8547e98905eSBodo Stroesser int space, iov_cnt = 0, ret = 0; 8557e98905eSBodo Stroesser 8567e98905eSBodo Stroesser if (!cmd->dbi_cnt) 8577e98905eSBodo Stroesser goto wr_iov_cnts; 858bc2d214aSBodo Stroesser 859b6df4b79SXiubo Li /* try to check and get the data blocks as needed */ 860b6df4b79SXiubo Li space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh); 86152ef2743SBodo Stroesser if (space < cmd->dbi_cnt) { 86280eb8761SMike Christie unsigned long blocks_left = 86380eb8761SMike Christie (udev->max_blocks - udev->dbi_thresh) + space; 864b6df4b79SXiubo Li 86552ef2743SBodo Stroesser if (blocks_left < cmd->dbi_cnt) { 866e719afdcSBodo Stroesser pr_debug("no data space: only %lu available, but ask for %u\n", 867e719afdcSBodo Stroesser blocks_left * udev->data_blk_size, 868e719afdcSBodo Stroesser cmd->dbi_cnt * udev->data_blk_size); 8697e98905eSBodo Stroesser return -1; 8707c9e7a6fSAndy Grover } 8717c9e7a6fSAndy Grover 87252ef2743SBodo Stroesser udev->dbi_thresh += cmd->dbi_cnt; 87380eb8761SMike Christie if (udev->dbi_thresh > udev->max_blocks) 87480eb8761SMike Christie udev->dbi_thresh = udev->max_blocks; 875b6df4b79SXiubo Li } 876b6df4b79SXiubo Li 877f5ce815fSBodo Stroesser iov_cnt = tcmu_get_empty_blocks(udev, cmd, cmd->se_cmd->data_length); 8787e98905eSBodo Stroesser if (iov_cnt < 0) 8797e98905eSBodo Stroesser return -1; 8807e98905eSBodo Stroesser 8817e98905eSBodo Stroesser if (cmd->dbi_bidi_cnt) { 882f5ce815fSBodo Stroesser ret = tcmu_get_empty_blocks(udev, cmd, cmd->data_len_bidi); 8837e98905eSBodo Stroesser if (ret < 0) 8847e98905eSBodo Stroesser return -1; 8857e98905eSBodo Stroesser } 8867e98905eSBodo Stroesser wr_iov_cnts: 8877e98905eSBodo Stroesser *iov_bidi_cnt = ret; 8887e98905eSBodo Stroesser return iov_cnt + ret; 8897c9e7a6fSAndy Grover } 8907c9e7a6fSAndy Grover 891fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt) 892fe25cc34SXiubo Li { 893fe25cc34SXiubo Li return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]), 894fe25cc34SXiubo Li sizeof(struct tcmu_cmd_entry)); 895fe25cc34SXiubo Li } 896fe25cc34SXiubo Li 897fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd, 898fe25cc34SXiubo Li size_t base_command_size) 899fe25cc34SXiubo Li { 900fe25cc34SXiubo Li struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 901fe25cc34SXiubo Li size_t command_size; 902fe25cc34SXiubo Li 903fe25cc34SXiubo Li command_size = base_command_size + 904fe25cc34SXiubo Li round_up(scsi_command_size(se_cmd->t_task_cdb), 905fe25cc34SXiubo Li TCMU_OP_ALIGN_SIZE); 906fe25cc34SXiubo Li 907fe25cc34SXiubo Li WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1)); 908fe25cc34SXiubo Li 909fe25cc34SXiubo Li return command_size; 910fe25cc34SXiubo Li } 911fe25cc34SXiubo Li 91261fb2482SBodo Stroesser static void tcmu_setup_cmd_timer(struct tcmu_cmd *tcmu_cmd, unsigned int tmo, 9139103575aSMike Christie struct timer_list *timer) 9140d44374cSMike Christie { 9159103575aSMike Christie if (!tmo) 91661fb2482SBodo Stroesser return; 9179103575aSMike Christie 9180d44374cSMike Christie tcmu_cmd->deadline = round_jiffies_up(jiffies + msecs_to_jiffies(tmo)); 919a94a2572SXiubo Li if (!timer_pending(timer)) 9209103575aSMike Christie mod_timer(timer, tcmu_cmd->deadline); 921a94a2572SXiubo Li 92261fb2482SBodo Stroesser pr_debug("Timeout set up for cmd %p, dev = %s, tmo = %lu\n", tcmu_cmd, 92361fb2482SBodo Stroesser tcmu_cmd->tcmu_dev->name, tmo / MSEC_PER_SEC); 9240d44374cSMike Christie } 9250d44374cSMike Christie 926a94a2572SXiubo Li static int add_to_qfull_queue(struct tcmu_cmd *tcmu_cmd) 927af1dd7ffSMike Christie { 928af1dd7ffSMike Christie struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 9299103575aSMike Christie unsigned int tmo; 930af1dd7ffSMike Christie 9319103575aSMike Christie /* 9329103575aSMike Christie * For backwards compat if qfull_time_out is not set use 9339103575aSMike Christie * cmd_time_out and if that's not set use the default time out. 9349103575aSMike Christie */ 9359103575aSMike Christie if (!udev->qfull_time_out) 9369103575aSMike Christie return -ETIMEDOUT; 9379103575aSMike Christie else if (udev->qfull_time_out > 0) 9389103575aSMike Christie tmo = udev->qfull_time_out; 9399103575aSMike Christie else if (udev->cmd_time_out) 9409103575aSMike Christie tmo = udev->cmd_time_out; 9419103575aSMike Christie else 9429103575aSMike Christie tmo = TCMU_TIME_OUT; 9439103575aSMike Christie 94461fb2482SBodo Stroesser tcmu_setup_cmd_timer(tcmu_cmd, tmo, &udev->qfull_timer); 945af1dd7ffSMike Christie 946a94a2572SXiubo Li list_add_tail(&tcmu_cmd->queue_entry, &udev->qfull_queue); 94761fb2482SBodo Stroesser pr_debug("adding cmd %p on dev %s to ring space wait queue\n", 94861fb2482SBodo Stroesser tcmu_cmd, udev->name); 949af1dd7ffSMike Christie return 0; 950af1dd7ffSMike Christie } 951af1dd7ffSMike Christie 9523d3f9d56SBodo Stroesser static uint32_t ring_insert_padding(struct tcmu_dev *udev, size_t cmd_size) 9533d3f9d56SBodo Stroesser { 9543d3f9d56SBodo Stroesser struct tcmu_cmd_entry_hdr *hdr; 9553d3f9d56SBodo Stroesser struct tcmu_mailbox *mb = udev->mb_addr; 9563d3f9d56SBodo Stroesser uint32_t cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 9573d3f9d56SBodo Stroesser 9583d3f9d56SBodo Stroesser /* Insert a PAD if end-of-ring space is too small */ 9593d3f9d56SBodo Stroesser if (head_to_end(cmd_head, udev->cmdr_size) < cmd_size) { 9603d3f9d56SBodo Stroesser size_t pad_size = head_to_end(cmd_head, udev->cmdr_size); 9613d3f9d56SBodo Stroesser 962ecddbb7eSBodo Stroesser hdr = udev->cmdr + cmd_head; 9633d3f9d56SBodo Stroesser tcmu_hdr_set_op(&hdr->len_op, TCMU_OP_PAD); 9643d3f9d56SBodo Stroesser tcmu_hdr_set_len(&hdr->len_op, pad_size); 9653d3f9d56SBodo Stroesser hdr->cmd_id = 0; /* not used for PAD */ 9663d3f9d56SBodo Stroesser hdr->kflags = 0; 9673d3f9d56SBodo Stroesser hdr->uflags = 0; 9683d3f9d56SBodo Stroesser tcmu_flush_dcache_range(hdr, sizeof(*hdr)); 9693d3f9d56SBodo Stroesser 9703d3f9d56SBodo Stroesser UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size); 9713d3f9d56SBodo Stroesser tcmu_flush_dcache_range(mb, sizeof(*mb)); 9723d3f9d56SBodo Stroesser 9733d3f9d56SBodo Stroesser cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ 9743d3f9d56SBodo Stroesser WARN_ON(cmd_head != 0); 9753d3f9d56SBodo Stroesser } 9763d3f9d56SBodo Stroesser 9773d3f9d56SBodo Stroesser return cmd_head; 9783d3f9d56SBodo Stroesser } 9793d3f9d56SBodo Stroesser 9806888da81SMike Christie static void tcmu_unplug_device(struct se_dev_plug *se_plug) 9816888da81SMike Christie { 9826888da81SMike Christie struct se_device *se_dev = se_plug->se_dev; 9836888da81SMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 9846888da81SMike Christie 9856888da81SMike Christie clear_bit(TCM_DEV_BIT_PLUGGED, &udev->flags); 9866888da81SMike Christie uio_event_notify(&udev->uio_info); 9876888da81SMike Christie } 9886888da81SMike Christie 9896888da81SMike Christie static struct se_dev_plug *tcmu_plug_device(struct se_device *se_dev) 9906888da81SMike Christie { 9916888da81SMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 9926888da81SMike Christie 9936888da81SMike Christie if (!test_and_set_bit(TCM_DEV_BIT_PLUGGED, &udev->flags)) 9946888da81SMike Christie return &udev->se_plug; 9956888da81SMike Christie 9966888da81SMike Christie return NULL; 9976888da81SMike Christie } 9986888da81SMike Christie 9996fd0ce79SMike Christie /** 10006fd0ce79SMike Christie * queue_cmd_ring - queue cmd to ring or internally 10016fd0ce79SMike Christie * @tcmu_cmd: cmd to queue 10026fd0ce79SMike Christie * @scsi_err: TCM error code if failure (-1) returned. 10036fd0ce79SMike Christie * 10046fd0ce79SMike Christie * Returns: 10056fd0ce79SMike Christie * -1 we cannot queue internally or to the ring. 10066fd0ce79SMike Christie * 0 success 1007af1dd7ffSMike Christie * 1 internally queued to wait for ring memory to free. 10086fd0ce79SMike Christie */ 1009e7f41104SBart Van Assche static int queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, sense_reason_t *scsi_err) 10107c9e7a6fSAndy Grover { 10117c9e7a6fSAndy Grover struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; 10127c9e7a6fSAndy Grover struct se_cmd *se_cmd = tcmu_cmd->se_cmd; 10137c9e7a6fSAndy Grover size_t base_command_size, command_size; 10143d3f9d56SBodo Stroesser struct tcmu_mailbox *mb = udev->mb_addr; 10157c9e7a6fSAndy Grover struct tcmu_cmd_entry *entry; 10167c9e7a6fSAndy Grover struct iovec *iov; 1017d3cbb743SBodo Stroesser int iov_cnt, iov_bidi_cnt; 1018d3cbb743SBodo Stroesser uint32_t cmd_id, cmd_head; 10197c9e7a6fSAndy Grover uint64_t cdb_off; 1020e719afdcSBodo Stroesser uint32_t blk_size = udev->data_blk_size; 102152ef2743SBodo Stroesser /* size of data buffer needed */ 1022e719afdcSBodo Stroesser size_t data_length = (size_t)tcmu_cmd->dbi_cnt * blk_size; 10237c9e7a6fSAndy Grover 10246fd0ce79SMike Christie *scsi_err = TCM_NO_SENSE; 10256fd0ce79SMike Christie 1026892782caSMike Christie if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) { 1027892782caSMike Christie *scsi_err = TCM_LUN_BUSY; 1028892782caSMike Christie return -1; 1029892782caSMike Christie } 1030892782caSMike Christie 10316fd0ce79SMike Christie if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) { 10326fd0ce79SMike Christie *scsi_err = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 10336fd0ce79SMike Christie return -1; 10346fd0ce79SMike Christie } 10357c9e7a6fSAndy Grover 1036a94a2572SXiubo Li if (!list_empty(&udev->qfull_queue)) 1037af1dd7ffSMike Christie goto queue; 10387c9e7a6fSAndy Grover 1039e719afdcSBodo Stroesser if (data_length > (size_t)udev->max_blocks * blk_size) { 10407e98905eSBodo Stroesser pr_warn("TCMU: Request of size %zu is too big for %zu data area\n", 1041e719afdcSBodo Stroesser data_length, (size_t)udev->max_blocks * blk_size); 10426fd0ce79SMike Christie *scsi_err = TCM_INVALID_CDB_FIELD; 10436fd0ce79SMike Christie return -1; 1044554617b2SAndy Grover } 10457c9e7a6fSAndy Grover 10467e98905eSBodo Stroesser iov_cnt = tcmu_alloc_data_space(udev, tcmu_cmd, &iov_bidi_cnt); 10477e98905eSBodo Stroesser if (iov_cnt < 0) 10487e98905eSBodo Stroesser goto free_and_queue; 10497e98905eSBodo Stroesser 10507e98905eSBodo Stroesser /* 10517e98905eSBodo Stroesser * Must be a certain minimum size for response sense info, but 10527e98905eSBodo Stroesser * also may be larger if the iov array is large. 10537e98905eSBodo Stroesser */ 10547e98905eSBodo Stroesser base_command_size = tcmu_cmd_get_base_cmd_size(iov_cnt); 10557e98905eSBodo Stroesser command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size); 10567e98905eSBodo Stroesser 10577e98905eSBodo Stroesser if (command_size > (udev->cmdr_size / 2)) { 10587e98905eSBodo Stroesser pr_warn("TCMU: Request of size %zu is too big for %u cmd ring\n", 10597e98905eSBodo Stroesser command_size, udev->cmdr_size); 10607e98905eSBodo Stroesser tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur); 10617e98905eSBodo Stroesser *scsi_err = TCM_INVALID_CDB_FIELD; 10627e98905eSBodo Stroesser return -1; 10637e98905eSBodo Stroesser } 10647e98905eSBodo Stroesser 10657e98905eSBodo Stroesser if (!is_ring_space_avail(udev, command_size)) 1066810b8153SMike Christie /* 1067810b8153SMike Christie * Don't leave commands partially setup because the unmap 1068810b8153SMike Christie * thread might need the blocks to make forward progress. 1069810b8153SMike Christie */ 10707e98905eSBodo Stroesser goto free_and_queue; 107126418649SSheng Yang 1072d3cbb743SBodo Stroesser if (xa_alloc(&udev->commands, &cmd_id, tcmu_cmd, XA_LIMIT(1, 0xffff), 1073d3cbb743SBodo Stroesser GFP_NOWAIT) < 0) { 107461fb2482SBodo Stroesser pr_err("tcmu: Could not allocate cmd id.\n"); 10756fd0ce79SMike Christie 107661fb2482SBodo Stroesser tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt); 10776fd0ce79SMike Christie *scsi_err = TCM_OUT_OF_RESOURCES; 10786fd0ce79SMike Christie return -1; 10790d44374cSMike Christie } 108061fb2482SBodo Stroesser tcmu_cmd->cmd_id = cmd_id; 108161fb2482SBodo Stroesser 108261fb2482SBodo Stroesser pr_debug("allocated cmd id %u for cmd %p dev %s\n", tcmu_cmd->cmd_id, 108361fb2482SBodo Stroesser tcmu_cmd, udev->name); 108461fb2482SBodo Stroesser 10857e98905eSBodo Stroesser cmd_head = ring_insert_padding(udev, command_size); 10867e98905eSBodo Stroesser 1087ecddbb7eSBodo Stroesser entry = udev->cmdr + cmd_head; 10887e98905eSBodo Stroesser memset(entry, 0, command_size); 10897e98905eSBodo Stroesser tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD); 10907e98905eSBodo Stroesser 10917e98905eSBodo Stroesser /* prepare iov list and copy data to data area if necessary */ 10927e98905eSBodo Stroesser tcmu_cmd_reset_dbi_cur(tcmu_cmd); 10937e98905eSBodo Stroesser iov = &entry->req.iov[0]; 10943c9a7c58SBodo Stroesser 10953c9a7c58SBodo Stroesser if (se_cmd->data_direction == DMA_TO_DEVICE || 10963c9a7c58SBodo Stroesser se_cmd->se_cmd_flags & SCF_BIDI) 10973c9a7c58SBodo Stroesser scatter_data_area(udev, tcmu_cmd, &iov); 10983c9a7c58SBodo Stroesser else 10993c9a7c58SBodo Stroesser tcmu_setup_iovs(udev, tcmu_cmd, &iov, se_cmd->data_length); 11003c9a7c58SBodo Stroesser 11017e98905eSBodo Stroesser entry->req.iov_cnt = iov_cnt - iov_bidi_cnt; 11027e98905eSBodo Stroesser 11037e98905eSBodo Stroesser /* Handle BIDI commands */ 11047e98905eSBodo Stroesser if (se_cmd->se_cmd_flags & SCF_BIDI) { 11057e98905eSBodo Stroesser iov++; 11063c9a7c58SBodo Stroesser tcmu_setup_iovs(udev, tcmu_cmd, &iov, tcmu_cmd->data_len_bidi); 11077e98905eSBodo Stroesser entry->req.iov_bidi_cnt = iov_bidi_cnt; 11087e98905eSBodo Stroesser } 11097e98905eSBodo Stroesser 111061fb2482SBodo Stroesser tcmu_setup_cmd_timer(tcmu_cmd, udev->cmd_time_out, &udev->cmd_timer); 111161fb2482SBodo Stroesser 11120d44374cSMike Christie entry->hdr.cmd_id = tcmu_cmd->cmd_id; 11130d44374cSMike Christie 1114fe25cc34SXiubo Li tcmu_hdr_set_len(&entry->hdr.len_op, command_size); 1115fe25cc34SXiubo Li 11167c9e7a6fSAndy Grover /* All offsets relative to mb_addr, not start of entry! */ 11177c9e7a6fSAndy Grover cdb_off = CMDR_OFF + cmd_head + base_command_size; 11187c9e7a6fSAndy Grover memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb)); 11197c9e7a6fSAndy Grover entry->req.cdb_off = cdb_off; 11208c4e0f21SBodo Stroesser tcmu_flush_dcache_range(entry, command_size); 11217c9e7a6fSAndy Grover 11227c9e7a6fSAndy Grover UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size); 11237c9e7a6fSAndy Grover tcmu_flush_dcache_range(mb, sizeof(*mb)); 11247c9e7a6fSAndy Grover 1125a94a2572SXiubo Li list_add_tail(&tcmu_cmd->queue_entry, &udev->inflight_queue); 1126a94a2572SXiubo Li 11276888da81SMike Christie if (!test_bit(TCM_DEV_BIT_PLUGGED, &udev->flags)) 11287c9e7a6fSAndy Grover uio_event_notify(&udev->uio_info); 11297c9e7a6fSAndy Grover 11306fd0ce79SMike Christie return 0; 1131af1dd7ffSMike Christie 11327e98905eSBodo Stroesser free_and_queue: 11337e98905eSBodo Stroesser tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur); 11347e98905eSBodo Stroesser tcmu_cmd_reset_dbi_cur(tcmu_cmd); 11357e98905eSBodo Stroesser 1136af1dd7ffSMike Christie queue: 1137a94a2572SXiubo Li if (add_to_qfull_queue(tcmu_cmd)) { 1138af1dd7ffSMike Christie *scsi_err = TCM_OUT_OF_RESOURCES; 1139af1dd7ffSMike Christie return -1; 1140af1dd7ffSMike Christie } 1141af1dd7ffSMike Christie 1142af1dd7ffSMike Christie return 1; 11437c9e7a6fSAndy Grover } 11447c9e7a6fSAndy Grover 1145bc2d214aSBodo Stroesser /** 1146bc2d214aSBodo Stroesser * queue_tmr_ring - queue tmr info to ring or internally 1147bc2d214aSBodo Stroesser * @udev: related tcmu_dev 1148bc2d214aSBodo Stroesser * @tmr: tcmu_tmr containing tmr info to queue 1149bc2d214aSBodo Stroesser * 1150bc2d214aSBodo Stroesser * Returns: 1151bc2d214aSBodo Stroesser * 0 success 1152bc2d214aSBodo Stroesser * 1 internally queued to wait for ring memory to free. 1153bc2d214aSBodo Stroesser */ 1154bc2d214aSBodo Stroesser static int 1155bc2d214aSBodo Stroesser queue_tmr_ring(struct tcmu_dev *udev, struct tcmu_tmr *tmr) 1156bc2d214aSBodo Stroesser { 1157bc2d214aSBodo Stroesser struct tcmu_tmr_entry *entry; 1158bc2d214aSBodo Stroesser int cmd_size; 1159bc2d214aSBodo Stroesser int id_list_sz; 1160bc2d214aSBodo Stroesser struct tcmu_mailbox *mb = udev->mb_addr; 1161bc2d214aSBodo Stroesser uint32_t cmd_head; 1162bc2d214aSBodo Stroesser 1163bc2d214aSBodo Stroesser if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) 1164bc2d214aSBodo Stroesser goto out_free; 1165bc2d214aSBodo Stroesser 1166bc2d214aSBodo Stroesser id_list_sz = sizeof(tmr->tmr_cmd_ids[0]) * tmr->tmr_cmd_cnt; 1167bc2d214aSBodo Stroesser cmd_size = round_up(sizeof(*entry) + id_list_sz, TCMU_OP_ALIGN_SIZE); 1168bc2d214aSBodo Stroesser 1169bc2d214aSBodo Stroesser if (!list_empty(&udev->tmr_queue) || 11707e98905eSBodo Stroesser !is_ring_space_avail(udev, cmd_size)) { 1171bc2d214aSBodo Stroesser list_add_tail(&tmr->queue_entry, &udev->tmr_queue); 1172bc2d214aSBodo Stroesser pr_debug("adding tmr %p on dev %s to TMR ring space wait queue\n", 1173bc2d214aSBodo Stroesser tmr, udev->name); 1174bc2d214aSBodo Stroesser return 1; 1175bc2d214aSBodo Stroesser } 1176bc2d214aSBodo Stroesser 1177bc2d214aSBodo Stroesser cmd_head = ring_insert_padding(udev, cmd_size); 1178bc2d214aSBodo Stroesser 1179ecddbb7eSBodo Stroesser entry = udev->cmdr + cmd_head; 1180bc2d214aSBodo Stroesser memset(entry, 0, cmd_size); 1181bc2d214aSBodo Stroesser tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_TMR); 1182bc2d214aSBodo Stroesser tcmu_hdr_set_len(&entry->hdr.len_op, cmd_size); 1183bc2d214aSBodo Stroesser entry->tmr_type = tmr->tmr_type; 1184bc2d214aSBodo Stroesser entry->cmd_cnt = tmr->tmr_cmd_cnt; 1185bc2d214aSBodo Stroesser memcpy(&entry->cmd_ids[0], &tmr->tmr_cmd_ids[0], id_list_sz); 1186bc2d214aSBodo Stroesser tcmu_flush_dcache_range(entry, cmd_size); 1187bc2d214aSBodo Stroesser 1188bc2d214aSBodo Stroesser UPDATE_HEAD(mb->cmd_head, cmd_size, udev->cmdr_size); 1189bc2d214aSBodo Stroesser tcmu_flush_dcache_range(mb, sizeof(*mb)); 1190bc2d214aSBodo Stroesser 1191bc2d214aSBodo Stroesser uio_event_notify(&udev->uio_info); 1192bc2d214aSBodo Stroesser 1193bc2d214aSBodo Stroesser out_free: 1194bc2d214aSBodo Stroesser kfree(tmr); 1195bc2d214aSBodo Stroesser 1196bc2d214aSBodo Stroesser return 0; 1197bc2d214aSBodo Stroesser } 1198bc2d214aSBodo Stroesser 119902eb924fSAndy Grover static sense_reason_t 120002eb924fSAndy Grover tcmu_queue_cmd(struct se_cmd *se_cmd) 12017c9e7a6fSAndy Grover { 1202af1dd7ffSMike Christie struct se_device *se_dev = se_cmd->se_dev; 1203af1dd7ffSMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 12047c9e7a6fSAndy Grover struct tcmu_cmd *tcmu_cmd; 1205c9684927SBodo Stroesser sense_reason_t scsi_ret = TCM_CHECK_CONDITION_ABORT_CMD; 1206c9684927SBodo Stroesser int ret = -1; 12077c9e7a6fSAndy Grover 12087c9e7a6fSAndy Grover tcmu_cmd = tcmu_alloc_cmd(se_cmd); 12097c9e7a6fSAndy Grover if (!tcmu_cmd) 121002eb924fSAndy Grover return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 12117c9e7a6fSAndy Grover 1212af1dd7ffSMike Christie mutex_lock(&udev->cmdr_lock); 1213c9684927SBodo Stroesser if (!(se_cmd->transport_state & CMD_T_ABORTED)) 1214af1dd7ffSMike Christie ret = queue_cmd_ring(tcmu_cmd, &scsi_ret); 1215af1dd7ffSMike Christie if (ret < 0) 1216141685a3SXiubo Li tcmu_free_cmd(tcmu_cmd); 1217780e1384SShin'ichiro Kawasaki else 1218780e1384SShin'ichiro Kawasaki se_cmd->priv = tcmu_cmd; 1219a3512902SBodo Stroesser mutex_unlock(&udev->cmdr_lock); 12206fd0ce79SMike Christie return scsi_ret; 12217c9e7a6fSAndy Grover } 12227c9e7a6fSAndy Grover 1223ed212ca8SBodo Stroesser static void tcmu_set_next_deadline(struct list_head *queue, 1224ed212ca8SBodo Stroesser struct timer_list *timer) 1225ed212ca8SBodo Stroesser { 1226ed212ca8SBodo Stroesser struct tcmu_cmd *cmd; 1227ed212ca8SBodo Stroesser 1228ed212ca8SBodo Stroesser if (!list_empty(queue)) { 1229ed212ca8SBodo Stroesser cmd = list_first_entry(queue, struct tcmu_cmd, queue_entry); 1230ed212ca8SBodo Stroesser mod_timer(timer, cmd->deadline); 1231ed212ca8SBodo Stroesser } else 1232ed212ca8SBodo Stroesser del_timer(timer); 1233ed212ca8SBodo Stroesser } 1234ed212ca8SBodo Stroesser 1235bc2d214aSBodo Stroesser static int 1236bc2d214aSBodo Stroesser tcmu_tmr_type(enum tcm_tmreq_table tmf) 1237bc2d214aSBodo Stroesser { 1238bc2d214aSBodo Stroesser switch (tmf) { 1239bc2d214aSBodo Stroesser case TMR_ABORT_TASK: return TCMU_TMR_ABORT_TASK; 1240bc2d214aSBodo Stroesser case TMR_ABORT_TASK_SET: return TCMU_TMR_ABORT_TASK_SET; 1241bc2d214aSBodo Stroesser case TMR_CLEAR_ACA: return TCMU_TMR_CLEAR_ACA; 1242bc2d214aSBodo Stroesser case TMR_CLEAR_TASK_SET: return TCMU_TMR_CLEAR_TASK_SET; 1243bc2d214aSBodo Stroesser case TMR_LUN_RESET: return TCMU_TMR_LUN_RESET; 1244bc2d214aSBodo Stroesser case TMR_TARGET_WARM_RESET: return TCMU_TMR_TARGET_WARM_RESET; 1245bc2d214aSBodo Stroesser case TMR_TARGET_COLD_RESET: return TCMU_TMR_TARGET_COLD_RESET; 1246bc2d214aSBodo Stroesser case TMR_LUN_RESET_PRO: return TCMU_TMR_LUN_RESET_PRO; 1247bc2d214aSBodo Stroesser default: return TCMU_TMR_UNKNOWN; 1248bc2d214aSBodo Stroesser } 1249bc2d214aSBodo Stroesser } 1250bc2d214aSBodo Stroesser 1251bc2d214aSBodo Stroesser static void 1252bc2d214aSBodo Stroesser tcmu_tmr_notify(struct se_device *se_dev, enum tcm_tmreq_table tmf, 1253bc2d214aSBodo Stroesser struct list_head *cmd_list) 1254bc2d214aSBodo Stroesser { 1255bc2d214aSBodo Stroesser int i = 0, cmd_cnt = 0; 1256bc2d214aSBodo Stroesser bool unqueued = false; 1257bc2d214aSBodo Stroesser uint16_t *cmd_ids = NULL; 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 12941080782fSBodo Stroesser tmr = kmalloc(sizeof(*tmr) + cmd_cnt * sizeof(*cmd_ids), 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 13187c9e7a6fSAndy Grover static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry) 13197c9e7a6fSAndy Grover { 13207c9e7a6fSAndy Grover struct se_cmd *se_cmd = cmd->se_cmd; 13217c9e7a6fSAndy Grover struct tcmu_dev *udev = cmd->tcmu_dev; 13226c3796d1Sbstroesser@ts.fujitsu.com bool read_len_valid = false; 1323a86a7586SDmitry Fomichev uint32_t read_len; 13247c9e7a6fSAndy Grover 1325b25c7863SSheng Yang /* 1326b25c7863SSheng Yang * cmd has been completed already from timeout, just reclaim 13273d9b9555SAndy Grover * data area space and free cmd 1328b25c7863SSheng Yang */ 1329a86a7586SDmitry Fomichev if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { 1330a86a7586SDmitry Fomichev WARN_ON_ONCE(se_cmd); 1331141685a3SXiubo Li goto out; 1332a86a7586SDmitry Fomichev } 1333b25c7863SSheng Yang 1334a94a2572SXiubo Li list_del_init(&cmd->queue_entry); 1335a94a2572SXiubo Li 1336141685a3SXiubo Li tcmu_cmd_reset_dbi_cur(cmd); 13377c9e7a6fSAndy Grover 13380ad46af8SAndy Grover if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) { 13390ad46af8SAndy Grover pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n", 13400ad46af8SAndy Grover cmd->se_cmd); 1341ed97d0cdSAndy Grover entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION; 13426c3796d1Sbstroesser@ts.fujitsu.com goto done; 13436c3796d1Sbstroesser@ts.fujitsu.com } 13446c3796d1Sbstroesser@ts.fujitsu.com 1345a86a7586SDmitry Fomichev read_len = se_cmd->data_length; 13466c3796d1Sbstroesser@ts.fujitsu.com if (se_cmd->data_direction == DMA_FROM_DEVICE && 13476c3796d1Sbstroesser@ts.fujitsu.com (entry->hdr.uflags & TCMU_UFLAG_READ_LEN) && entry->rsp.read_len) { 13486c3796d1Sbstroesser@ts.fujitsu.com read_len_valid = true; 13496c3796d1Sbstroesser@ts.fujitsu.com if (entry->rsp.read_len < read_len) 13506c3796d1Sbstroesser@ts.fujitsu.com read_len = entry->rsp.read_len; 13516c3796d1Sbstroesser@ts.fujitsu.com } 13526c3796d1Sbstroesser@ts.fujitsu.com 13536c3796d1Sbstroesser@ts.fujitsu.com if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) { 1354406f74c2SMike Christie transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer); 13556c3796d1Sbstroesser@ts.fujitsu.com if (!read_len_valid ) 13566c3796d1Sbstroesser@ts.fujitsu.com goto done; 13576c3796d1Sbstroesser@ts.fujitsu.com else 13586c3796d1Sbstroesser@ts.fujitsu.com se_cmd->se_cmd_flags |= SCF_TREAT_READ_AS_NORMAL; 13596c3796d1Sbstroesser@ts.fujitsu.com } 13606c3796d1Sbstroesser@ts.fujitsu.com if (se_cmd->se_cmd_flags & SCF_BIDI) { 136126418649SSheng Yang /* Get Data-In buffer before clean up */ 13626c3796d1Sbstroesser@ts.fujitsu.com gather_data_area(udev, cmd, true, read_len); 1363e4648b01SIlias Tsitsimpis } else if (se_cmd->data_direction == DMA_FROM_DEVICE) { 13646c3796d1Sbstroesser@ts.fujitsu.com gather_data_area(udev, cmd, false, read_len); 13657c9e7a6fSAndy Grover } else if (se_cmd->data_direction == DMA_TO_DEVICE) { 1366141685a3SXiubo Li /* TODO: */ 13672bc396a2SIlias Tsitsimpis } else if (se_cmd->data_direction != DMA_NONE) { 13682bc396a2SIlias Tsitsimpis pr_warn("TCMU: data direction was %d!\n", 13692bc396a2SIlias Tsitsimpis se_cmd->data_direction); 13707c9e7a6fSAndy Grover } 13717c9e7a6fSAndy Grover 13726c3796d1Sbstroesser@ts.fujitsu.com done: 1373780e1384SShin'ichiro Kawasaki se_cmd->priv = NULL; 13746c3796d1Sbstroesser@ts.fujitsu.com if (read_len_valid) { 13756c3796d1Sbstroesser@ts.fujitsu.com pr_debug("read_len = %d\n", read_len); 13766c3796d1Sbstroesser@ts.fujitsu.com target_complete_cmd_with_length(cmd->se_cmd, 13776c3796d1Sbstroesser@ts.fujitsu.com entry->rsp.scsi_status, read_len); 13786c3796d1Sbstroesser@ts.fujitsu.com } else 13797c9e7a6fSAndy Grover target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status); 13807c9e7a6fSAndy Grover 1381141685a3SXiubo Li out: 1382b6df4b79SXiubo Li tcmu_cmd_free_data(cmd, cmd->dbi_cnt); 1383141685a3SXiubo Li tcmu_free_cmd(cmd); 13847c9e7a6fSAndy Grover } 13857c9e7a6fSAndy Grover 1386bc2d214aSBodo Stroesser static int tcmu_run_tmr_queue(struct tcmu_dev *udev) 1387bc2d214aSBodo Stroesser { 1388bc2d214aSBodo Stroesser struct tcmu_tmr *tmr, *tmp; 1389bc2d214aSBodo Stroesser LIST_HEAD(tmrs); 1390bc2d214aSBodo Stroesser 1391bc2d214aSBodo Stroesser if (list_empty(&udev->tmr_queue)) 1392bc2d214aSBodo Stroesser return 1; 1393bc2d214aSBodo Stroesser 1394bc2d214aSBodo Stroesser pr_debug("running %s's tmr queue\n", udev->name); 1395bc2d214aSBodo Stroesser 1396bc2d214aSBodo Stroesser list_splice_init(&udev->tmr_queue, &tmrs); 1397bc2d214aSBodo Stroesser 1398bc2d214aSBodo Stroesser list_for_each_entry_safe(tmr, tmp, &tmrs, queue_entry) { 1399bc2d214aSBodo Stroesser list_del_init(&tmr->queue_entry); 1400bc2d214aSBodo Stroesser 1401bc2d214aSBodo Stroesser pr_debug("removing tmr %p on dev %s from queue\n", 1402bc2d214aSBodo Stroesser tmr, udev->name); 1403bc2d214aSBodo Stroesser 1404bc2d214aSBodo Stroesser if (queue_tmr_ring(udev, tmr)) { 1405bc2d214aSBodo Stroesser pr_debug("ran out of space during tmr queue run\n"); 1406bc2d214aSBodo Stroesser /* 1407bc2d214aSBodo Stroesser * tmr was requeued, so just put all tmrs back in 1408bc2d214aSBodo Stroesser * the queue 1409bc2d214aSBodo Stroesser */ 1410bc2d214aSBodo Stroesser list_splice_tail(&tmrs, &udev->tmr_queue); 1411bc2d214aSBodo Stroesser return 0; 1412bc2d214aSBodo Stroesser } 1413bc2d214aSBodo Stroesser } 1414bc2d214aSBodo Stroesser 1415bc2d214aSBodo Stroesser return 1; 1416bc2d214aSBodo Stroesser } 1417bc2d214aSBodo Stroesser 14189814b55cSBodo Stroesser static bool tcmu_handle_completions(struct tcmu_dev *udev) 14197c9e7a6fSAndy Grover { 14207c9e7a6fSAndy Grover struct tcmu_mailbox *mb; 1421a94a2572SXiubo Li struct tcmu_cmd *cmd; 1422bc2d214aSBodo Stroesser bool free_space = false; 14237c9e7a6fSAndy Grover 14247c9e7a6fSAndy Grover if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) { 14257c9e7a6fSAndy Grover pr_err("ring broken, not handling completions\n"); 14267c9e7a6fSAndy Grover return 0; 14277c9e7a6fSAndy Grover } 14287c9e7a6fSAndy Grover 14297c9e7a6fSAndy Grover mb = udev->mb_addr; 14307c9e7a6fSAndy Grover tcmu_flush_dcache_range(mb, sizeof(*mb)); 14317c9e7a6fSAndy Grover 14326aa7de05SMark Rutland while (udev->cmdr_last_cleaned != READ_ONCE(mb->cmd_tail)) { 14337c9e7a6fSAndy Grover 1434ecddbb7eSBodo Stroesser struct tcmu_cmd_entry *entry = udev->cmdr + udev->cmdr_last_cleaned; 14357c9e7a6fSAndy Grover 14365a0c256dSBodo Stroesser /* 14375a0c256dSBodo Stroesser * Flush max. up to end of cmd ring since current entry might 14385a0c256dSBodo Stroesser * be a padding that is shorter than sizeof(*entry) 14395a0c256dSBodo Stroesser */ 14405a0c256dSBodo Stroesser size_t ring_left = head_to_end(udev->cmdr_last_cleaned, 14415a0c256dSBodo Stroesser udev->cmdr_size); 14425a0c256dSBodo Stroesser tcmu_flush_dcache_range(entry, ring_left < sizeof(*entry) ? 14435a0c256dSBodo Stroesser ring_left : sizeof(*entry)); 14447c9e7a6fSAndy Grover 1445bc2d214aSBodo Stroesser free_space = true; 1446bc2d214aSBodo Stroesser 1447bc2d214aSBodo Stroesser if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD || 1448bc2d214aSBodo Stroesser tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_TMR) { 14490ad46af8SAndy Grover UPDATE_HEAD(udev->cmdr_last_cleaned, 14500ad46af8SAndy Grover tcmu_hdr_get_len(entry->hdr.len_op), 14510ad46af8SAndy Grover udev->cmdr_size); 14527c9e7a6fSAndy Grover continue; 14537c9e7a6fSAndy Grover } 14540ad46af8SAndy Grover WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD); 14557c9e7a6fSAndy Grover 1456d3cbb743SBodo Stroesser cmd = xa_erase(&udev->commands, entry->hdr.cmd_id); 14577c9e7a6fSAndy Grover if (!cmd) { 145888cf1073SMike Christie pr_err("cmd_id %u not found, ring is broken\n", 145988cf1073SMike Christie entry->hdr.cmd_id); 14607c9e7a6fSAndy Grover set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags); 14619814b55cSBodo Stroesser return false; 14627c9e7a6fSAndy Grover } 14637c9e7a6fSAndy Grover 14647c9e7a6fSAndy Grover tcmu_handle_completion(cmd, entry); 14657c9e7a6fSAndy Grover 14660ad46af8SAndy Grover UPDATE_HEAD(udev->cmdr_last_cleaned, 14670ad46af8SAndy Grover tcmu_hdr_get_len(entry->hdr.len_op), 14680ad46af8SAndy Grover udev->cmdr_size); 14697c9e7a6fSAndy Grover } 1470bc2d214aSBodo Stroesser if (free_space) 1471bc2d214aSBodo Stroesser free_space = tcmu_run_tmr_queue(udev); 14727c9e7a6fSAndy Grover 14738b084d9dSBodo Stroesser if (atomic_read(&global_page_count) > tcmu_global_max_pages && 1474d3cbb743SBodo Stroesser xa_empty(&udev->commands) && list_empty(&udev->qfull_queue)) { 1475af1dd7ffSMike Christie /* 1476ed212ca8SBodo Stroesser * Allocated blocks exceeded global block limit, currently no 1477ed212ca8SBodo Stroesser * more pending or waiting commands so try to reclaim blocks. 1478af1dd7ffSMike Christie */ 1479af1dd7ffSMike Christie schedule_delayed_work(&tcmu_unmap_work, 0); 1480af1dd7ffSMike Christie } 1481ed212ca8SBodo Stroesser if (udev->cmd_time_out) 1482a94a2572SXiubo Li tcmu_set_next_deadline(&udev->inflight_queue, &udev->cmd_timer); 14837c9e7a6fSAndy Grover 1484bc2d214aSBodo Stroesser return free_space; 14857c9e7a6fSAndy Grover } 14867c9e7a6fSAndy Grover 148761fb2482SBodo Stroesser static void tcmu_check_expired_ring_cmd(struct tcmu_cmd *cmd) 14887c9e7a6fSAndy Grover { 1489af1dd7ffSMike Christie struct se_cmd *se_cmd; 14907c9e7a6fSAndy Grover 1491ed212ca8SBodo Stroesser if (!time_after_eq(jiffies, cmd->deadline)) 149261fb2482SBodo Stroesser return; 14939103575aSMike Christie 1494af1dd7ffSMike Christie set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags); 149540d883b0SXiubo Li list_del_init(&cmd->queue_entry); 149661fb2482SBodo Stroesser se_cmd = cmd->se_cmd; 1497a3512902SBodo Stroesser se_cmd->priv = NULL; 1498a86a7586SDmitry Fomichev cmd->se_cmd = NULL; 149961fb2482SBodo Stroesser 150061fb2482SBodo Stroesser pr_debug("Timing out inflight cmd %u on dev %s.\n", 150161fb2482SBodo Stroesser cmd->cmd_id, cmd->tcmu_dev->name); 150261fb2482SBodo Stroesser 150361fb2482SBodo Stroesser target_complete_cmd(se_cmd, SAM_STAT_CHECK_CONDITION); 1504af1dd7ffSMike Christie } 15059103575aSMike Christie 150661fb2482SBodo Stroesser static void tcmu_check_expired_queue_cmd(struct tcmu_cmd *cmd) 150761fb2482SBodo Stroesser { 150861fb2482SBodo Stroesser struct se_cmd *se_cmd; 15099103575aSMike Christie 1510ed212ca8SBodo Stroesser if (!time_after_eq(jiffies, cmd->deadline)) 151161fb2482SBodo Stroesser return; 151261fb2482SBodo Stroesser 15139d7464b1SDan Carpenter pr_debug("Timing out queued cmd %p on dev %s.\n", 15149d7464b1SDan Carpenter cmd, cmd->tcmu_dev->name); 15159d7464b1SDan Carpenter 151661fb2482SBodo Stroesser list_del_init(&cmd->queue_entry); 151761fb2482SBodo Stroesser se_cmd = cmd->se_cmd; 151861fb2482SBodo Stroesser tcmu_free_cmd(cmd); 151961fb2482SBodo Stroesser 1520780e1384SShin'ichiro Kawasaki se_cmd->priv = NULL; 152161fb2482SBodo Stroesser target_complete_cmd(se_cmd, SAM_STAT_TASK_SET_FULL); 15227c9e7a6fSAndy Grover } 15237c9e7a6fSAndy Grover 15249103575aSMike Christie static void tcmu_device_timedout(struct tcmu_dev *udev) 15257c9e7a6fSAndy Grover { 1526488ebe4cSMike Christie spin_lock(&timed_out_udevs_lock); 1527488ebe4cSMike Christie if (list_empty(&udev->timedout_entry)) 1528488ebe4cSMike Christie list_add_tail(&udev->timedout_entry, &timed_out_udevs); 1529488ebe4cSMike Christie spin_unlock(&timed_out_udevs_lock); 15307c9e7a6fSAndy Grover 1531af1dd7ffSMike Christie schedule_delayed_work(&tcmu_unmap_work, 0); 15327c9e7a6fSAndy Grover } 15337c9e7a6fSAndy Grover 15349103575aSMike Christie static void tcmu_cmd_timedout(struct timer_list *t) 15359103575aSMike Christie { 15369103575aSMike Christie struct tcmu_dev *udev = from_timer(udev, t, cmd_timer); 15379103575aSMike Christie 15389103575aSMike Christie pr_debug("%s cmd timeout has expired\n", udev->name); 15399103575aSMike Christie tcmu_device_timedout(udev); 15409103575aSMike Christie } 15419103575aSMike Christie 15429103575aSMike Christie static void tcmu_qfull_timedout(struct timer_list *t) 15439103575aSMike Christie { 15449103575aSMike Christie struct tcmu_dev *udev = from_timer(udev, t, qfull_timer); 15459103575aSMike Christie 15469103575aSMike Christie pr_debug("%s qfull timeout has expired\n", udev->name); 15479103575aSMike Christie tcmu_device_timedout(udev); 15489103575aSMike Christie } 15499103575aSMike Christie 15507c9e7a6fSAndy Grover static int tcmu_attach_hba(struct se_hba *hba, u32 host_id) 15517c9e7a6fSAndy Grover { 15527c9e7a6fSAndy Grover struct tcmu_hba *tcmu_hba; 15537c9e7a6fSAndy Grover 15547c9e7a6fSAndy Grover tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL); 15557c9e7a6fSAndy Grover if (!tcmu_hba) 15567c9e7a6fSAndy Grover return -ENOMEM; 15577c9e7a6fSAndy Grover 15587c9e7a6fSAndy Grover tcmu_hba->host_id = host_id; 15597c9e7a6fSAndy Grover hba->hba_ptr = tcmu_hba; 15607c9e7a6fSAndy Grover 15617c9e7a6fSAndy Grover return 0; 15627c9e7a6fSAndy Grover } 15637c9e7a6fSAndy Grover 15647c9e7a6fSAndy Grover static void tcmu_detach_hba(struct se_hba *hba) 15657c9e7a6fSAndy Grover { 15667c9e7a6fSAndy Grover kfree(hba->hba_ptr); 15677c9e7a6fSAndy Grover hba->hba_ptr = NULL; 15687c9e7a6fSAndy Grover } 15697c9e7a6fSAndy Grover 15707c9e7a6fSAndy Grover static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name) 15717c9e7a6fSAndy Grover { 15727c9e7a6fSAndy Grover struct tcmu_dev *udev; 15737c9e7a6fSAndy Grover 15747c9e7a6fSAndy Grover udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL); 15757c9e7a6fSAndy Grover if (!udev) 15767c9e7a6fSAndy Grover return NULL; 1577f3cdbe39SMike Christie kref_init(&udev->kref); 15787c9e7a6fSAndy Grover 15797c9e7a6fSAndy Grover udev->name = kstrdup(name, GFP_KERNEL); 15807c9e7a6fSAndy Grover if (!udev->name) { 15817c9e7a6fSAndy Grover kfree(udev); 15827c9e7a6fSAndy Grover return NULL; 15837c9e7a6fSAndy Grover } 15847c9e7a6fSAndy Grover 15857c9e7a6fSAndy Grover udev->hba = hba; 1586af980e46SMike Christie udev->cmd_time_out = TCMU_TIME_OUT; 15879103575aSMike Christie udev->qfull_time_out = -1; 15887c9e7a6fSAndy Grover 1589e719afdcSBodo Stroesser udev->data_pages_per_blk = DATA_PAGES_PER_BLK_DEF; 1590e719afdcSBodo Stroesser udev->max_blocks = DATA_AREA_PAGES_DEF / udev->data_pages_per_blk; 1591f5ce815fSBodo Stroesser udev->data_area_mb = TCMU_PAGES_TO_MBS(DATA_AREA_PAGES_DEF); 1592e719afdcSBodo Stroesser 1593b6df4b79SXiubo Li mutex_init(&udev->cmdr_lock); 15947c9e7a6fSAndy Grover 1595ff07e4a4SMike Christie INIT_LIST_HEAD(&udev->node); 1596488ebe4cSMike Christie INIT_LIST_HEAD(&udev->timedout_entry); 1597a94a2572SXiubo Li INIT_LIST_HEAD(&udev->qfull_queue); 1598bc2d214aSBodo Stroesser INIT_LIST_HEAD(&udev->tmr_queue); 1599a94a2572SXiubo Li INIT_LIST_HEAD(&udev->inflight_queue); 1600d3cbb743SBodo Stroesser xa_init_flags(&udev->commands, XA_FLAGS_ALLOC1); 16017c9e7a6fSAndy Grover 16029103575aSMike Christie timer_setup(&udev->qfull_timer, tcmu_qfull_timedout, 0); 16039103575aSMike Christie timer_setup(&udev->cmd_timer, tcmu_cmd_timedout, 0); 16047c9e7a6fSAndy Grover 16058b084d9dSBodo Stroesser xa_init(&udev->data_pages); 1606c22adc0bSXiubo Li 16077c9e7a6fSAndy Grover return &udev->se_dev; 16087c9e7a6fSAndy Grover } 16097c9e7a6fSAndy Grover 161043bf922cSBodo Stroesser static void tcmu_dev_call_rcu(struct rcu_head *p) 161143bf922cSBodo Stroesser { 161243bf922cSBodo Stroesser struct se_device *dev = container_of(p, struct se_device, rcu_head); 161343bf922cSBodo Stroesser struct tcmu_dev *udev = TCMU_DEV(dev); 161443bf922cSBodo Stroesser 161543bf922cSBodo Stroesser kfree(udev->uio_info.name); 161643bf922cSBodo Stroesser kfree(udev->name); 161743bf922cSBodo Stroesser kfree(udev); 161843bf922cSBodo Stroesser } 161943bf922cSBodo Stroesser 162043bf922cSBodo Stroesser static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd) 162143bf922cSBodo Stroesser { 162243bf922cSBodo Stroesser if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { 162343bf922cSBodo Stroesser kmem_cache_free(tcmu_cmd_cache, cmd); 162443bf922cSBodo Stroesser return 0; 162543bf922cSBodo Stroesser } 162643bf922cSBodo Stroesser return -EINVAL; 162743bf922cSBodo Stroesser } 162843bf922cSBodo Stroesser 1629e719afdcSBodo Stroesser static u32 tcmu_blocks_release(struct tcmu_dev *udev, unsigned long first, 1630f7c89771SBodo Stroesser unsigned long last) 163143bf922cSBodo Stroesser { 1632e719afdcSBodo Stroesser XA_STATE(xas, &udev->data_pages, first * udev->data_pages_per_blk); 163343bf922cSBodo Stroesser struct page *page; 1634f5ce815fSBodo Stroesser u32 pages_freed = 0; 163543bf922cSBodo Stroesser 1636f7c89771SBodo Stroesser xas_lock(&xas); 1637e719afdcSBodo Stroesser xas_for_each(&xas, page, (last + 1) * udev->data_pages_per_blk - 1) { 1638f7c89771SBodo Stroesser xas_store(&xas, NULL); 163943bf922cSBodo Stroesser __free_page(page); 1640f5ce815fSBodo Stroesser pages_freed++; 164143bf922cSBodo Stroesser } 1642f7c89771SBodo Stroesser xas_unlock(&xas); 1643f5ce815fSBodo Stroesser 1644f5ce815fSBodo Stroesser atomic_sub(pages_freed, &global_page_count); 1645f5ce815fSBodo Stroesser 1646f5ce815fSBodo Stroesser return pages_freed; 164743bf922cSBodo Stroesser } 164843bf922cSBodo Stroesser 164943bf922cSBodo Stroesser static void tcmu_remove_all_queued_tmr(struct tcmu_dev *udev) 165043bf922cSBodo Stroesser { 165143bf922cSBodo Stroesser struct tcmu_tmr *tmr, *tmp; 165243bf922cSBodo Stroesser 165343bf922cSBodo Stroesser list_for_each_entry_safe(tmr, tmp, &udev->tmr_queue, queue_entry) { 165443bf922cSBodo Stroesser list_del_init(&tmr->queue_entry); 165543bf922cSBodo Stroesser kfree(tmr); 165643bf922cSBodo Stroesser } 165743bf922cSBodo Stroesser } 165843bf922cSBodo Stroesser 165943bf922cSBodo Stroesser static void tcmu_dev_kref_release(struct kref *kref) 166043bf922cSBodo Stroesser { 166143bf922cSBodo Stroesser struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref); 166243bf922cSBodo Stroesser struct se_device *dev = &udev->se_dev; 166343bf922cSBodo Stroesser struct tcmu_cmd *cmd; 166443bf922cSBodo Stroesser bool all_expired = true; 1665d3cbb743SBodo Stroesser unsigned long i; 166643bf922cSBodo Stroesser 166743bf922cSBodo Stroesser vfree(udev->mb_addr); 166843bf922cSBodo Stroesser udev->mb_addr = NULL; 166943bf922cSBodo Stroesser 167043bf922cSBodo Stroesser spin_lock_bh(&timed_out_udevs_lock); 167143bf922cSBodo Stroesser if (!list_empty(&udev->timedout_entry)) 167243bf922cSBodo Stroesser list_del(&udev->timedout_entry); 167343bf922cSBodo Stroesser spin_unlock_bh(&timed_out_udevs_lock); 167443bf922cSBodo Stroesser 167543bf922cSBodo Stroesser /* Upper layer should drain all requests before calling this */ 167643bf922cSBodo Stroesser mutex_lock(&udev->cmdr_lock); 1677d3cbb743SBodo Stroesser xa_for_each(&udev->commands, i, cmd) { 167843bf922cSBodo Stroesser if (tcmu_check_and_free_pending_cmd(cmd) != 0) 167943bf922cSBodo Stroesser all_expired = false; 168043bf922cSBodo Stroesser } 168143bf922cSBodo Stroesser /* There can be left over TMR cmds. Remove them. */ 168243bf922cSBodo Stroesser tcmu_remove_all_queued_tmr(udev); 168343bf922cSBodo Stroesser if (!list_empty(&udev->qfull_queue)) 168443bf922cSBodo Stroesser all_expired = false; 1685d3cbb743SBodo Stroesser xa_destroy(&udev->commands); 168643bf922cSBodo Stroesser WARN_ON(!all_expired); 168743bf922cSBodo Stroesser 1688e719afdcSBodo Stroesser tcmu_blocks_release(udev, 0, udev->dbi_max); 168943bf922cSBodo Stroesser bitmap_free(udev->data_bitmap); 169043bf922cSBodo Stroesser mutex_unlock(&udev->cmdr_lock); 169143bf922cSBodo Stroesser 16928f33bb24SBodo Stroesser pr_debug("dev_kref_release\n"); 16938f33bb24SBodo Stroesser 169443bf922cSBodo Stroesser call_rcu(&dev->rcu_head, tcmu_dev_call_rcu); 169543bf922cSBodo Stroesser } 169643bf922cSBodo Stroesser 169761fb2482SBodo Stroesser static void run_qfull_queue(struct tcmu_dev *udev, bool fail) 1698af1dd7ffSMike Christie { 1699af1dd7ffSMike Christie struct tcmu_cmd *tcmu_cmd, *tmp_cmd; 1700af1dd7ffSMike Christie LIST_HEAD(cmds); 1701af1dd7ffSMike Christie sense_reason_t scsi_ret; 1702af1dd7ffSMike Christie int ret; 1703af1dd7ffSMike Christie 1704a94a2572SXiubo Li if (list_empty(&udev->qfull_queue)) 170561fb2482SBodo Stroesser return; 1706af1dd7ffSMike Christie 1707892782caSMike Christie pr_debug("running %s's cmdr queue forcefail %d\n", udev->name, fail); 1708af1dd7ffSMike Christie 1709a94a2572SXiubo Li list_splice_init(&udev->qfull_queue, &cmds); 1710af1dd7ffSMike Christie 1711a94a2572SXiubo Li list_for_each_entry_safe(tcmu_cmd, tmp_cmd, &cmds, queue_entry) { 1712a94a2572SXiubo Li list_del_init(&tcmu_cmd->queue_entry); 1713af1dd7ffSMike Christie 171461fb2482SBodo Stroesser pr_debug("removing cmd %p on dev %s from queue\n", 171561fb2482SBodo Stroesser tcmu_cmd, udev->name); 1716af1dd7ffSMike Christie 1717892782caSMike Christie if (fail) { 1718892782caSMike Christie /* 1719892782caSMike Christie * We were not able to even start the command, so 1720892782caSMike Christie * fail with busy to allow a retry in case runner 1721892782caSMike Christie * was only temporarily down. If the device is being 1722892782caSMike Christie * removed then LIO core will do the right thing and 1723892782caSMike Christie * fail the retry. 1724892782caSMike Christie */ 1725780e1384SShin'ichiro Kawasaki tcmu_cmd->se_cmd->priv = NULL; 1726892782caSMike Christie target_complete_cmd(tcmu_cmd->se_cmd, SAM_STAT_BUSY); 1727892782caSMike Christie tcmu_free_cmd(tcmu_cmd); 1728892782caSMike Christie continue; 1729892782caSMike Christie } 1730892782caSMike Christie 1731af1dd7ffSMike Christie ret = queue_cmd_ring(tcmu_cmd, &scsi_ret); 1732af1dd7ffSMike Christie if (ret < 0) { 173361fb2482SBodo Stroesser pr_debug("cmd %p on dev %s failed with %u\n", 173461fb2482SBodo Stroesser tcmu_cmd, udev->name, scsi_ret); 1735af1dd7ffSMike Christie /* 1736af1dd7ffSMike Christie * Ignore scsi_ret for now. target_complete_cmd 1737af1dd7ffSMike Christie * drops it. 1738af1dd7ffSMike Christie */ 1739780e1384SShin'ichiro Kawasaki tcmu_cmd->se_cmd->priv = NULL; 1740af1dd7ffSMike Christie target_complete_cmd(tcmu_cmd->se_cmd, 1741af1dd7ffSMike Christie SAM_STAT_CHECK_CONDITION); 1742af1dd7ffSMike Christie tcmu_free_cmd(tcmu_cmd); 1743af1dd7ffSMike Christie } else if (ret > 0) { 1744af1dd7ffSMike Christie pr_debug("ran out of space during cmdr queue run\n"); 1745af1dd7ffSMike Christie /* 1746af1dd7ffSMike Christie * cmd was requeued, so just put all cmds back in 1747af1dd7ffSMike Christie * the queue 1748af1dd7ffSMike Christie */ 1749a94a2572SXiubo Li list_splice_tail(&cmds, &udev->qfull_queue); 1750a94a2572SXiubo Li break; 1751af1dd7ffSMike Christie } 1752af1dd7ffSMike Christie } 1753a94a2572SXiubo Li 1754a94a2572SXiubo Li tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer); 1755af1dd7ffSMike Christie } 1756af1dd7ffSMike Christie 17577c9e7a6fSAndy Grover static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on) 17587c9e7a6fSAndy Grover { 1759af1dd7ffSMike Christie struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 17607c9e7a6fSAndy Grover 1761af1dd7ffSMike Christie mutex_lock(&udev->cmdr_lock); 1762bc2d214aSBodo Stroesser if (tcmu_handle_completions(udev)) 1763a94a2572SXiubo Li run_qfull_queue(udev, false); 1764af1dd7ffSMike Christie mutex_unlock(&udev->cmdr_lock); 17657c9e7a6fSAndy Grover 17667c9e7a6fSAndy Grover return 0; 17677c9e7a6fSAndy Grover } 17687c9e7a6fSAndy Grover 17697c9e7a6fSAndy Grover /* 17707c9e7a6fSAndy Grover * mmap code from uio.c. Copied here because we want to hook mmap() 17717c9e7a6fSAndy Grover * and this stuff must come along. 17727c9e7a6fSAndy Grover */ 17737c9e7a6fSAndy Grover static int tcmu_find_mem_index(struct vm_area_struct *vma) 17747c9e7a6fSAndy Grover { 17757c9e7a6fSAndy Grover struct tcmu_dev *udev = vma->vm_private_data; 17767c9e7a6fSAndy Grover struct uio_info *info = &udev->uio_info; 17777c9e7a6fSAndy Grover 17787c9e7a6fSAndy Grover if (vma->vm_pgoff < MAX_UIO_MAPS) { 17797c9e7a6fSAndy Grover if (info->mem[vma->vm_pgoff].size == 0) 17807c9e7a6fSAndy Grover return -1; 17817c9e7a6fSAndy Grover return (int)vma->vm_pgoff; 17827c9e7a6fSAndy Grover } 17837c9e7a6fSAndy Grover return -1; 17847c9e7a6fSAndy Grover } 17857c9e7a6fSAndy Grover 17868b084d9dSBodo Stroesser static struct page *tcmu_try_get_data_page(struct tcmu_dev *udev, uint32_t dpi) 1787b6df4b79SXiubo Li { 1788b6df4b79SXiubo Li struct page *page; 1789b6df4b79SXiubo Li 1790b6df4b79SXiubo Li mutex_lock(&udev->cmdr_lock); 17913722e36cSBodo Stroesser page = xa_load(&udev->data_pages, dpi); 1792b6df4b79SXiubo Li if (likely(page)) { 1793b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1794b6df4b79SXiubo Li return page; 1795b6df4b79SXiubo Li } 1796b6df4b79SXiubo Li 1797b6df4b79SXiubo Li /* 1798c1c390baSMike Christie * Userspace messed up and passed in a address not in the 1799c1c390baSMike Christie * data iov passed to it. 1800b6df4b79SXiubo Li */ 18018b084d9dSBodo Stroesser pr_err("Invalid addr to data page mapping (dpi %u) on device %s\n", 18028b084d9dSBodo Stroesser dpi, udev->name); 1803b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 1804b6df4b79SXiubo Li 18058b084d9dSBodo Stroesser return NULL; 1806b6df4b79SXiubo Li } 1807b6df4b79SXiubo Li 18088f33bb24SBodo Stroesser static void tcmu_vma_open(struct vm_area_struct *vma) 18098f33bb24SBodo Stroesser { 18108f33bb24SBodo Stroesser struct tcmu_dev *udev = vma->vm_private_data; 18118f33bb24SBodo Stroesser 18128f33bb24SBodo Stroesser pr_debug("vma_open\n"); 18138f33bb24SBodo Stroesser 18148f33bb24SBodo Stroesser kref_get(&udev->kref); 18158f33bb24SBodo Stroesser } 18168f33bb24SBodo Stroesser 18178f33bb24SBodo Stroesser static void tcmu_vma_close(struct vm_area_struct *vma) 18188f33bb24SBodo Stroesser { 18198f33bb24SBodo Stroesser struct tcmu_dev *udev = vma->vm_private_data; 18208f33bb24SBodo Stroesser 18218f33bb24SBodo Stroesser pr_debug("vma_close\n"); 18228f33bb24SBodo Stroesser 18238f33bb24SBodo Stroesser /* release ref from tcmu_vma_open */ 18248f33bb24SBodo Stroesser kref_put(&udev->kref, tcmu_dev_kref_release); 18258f33bb24SBodo Stroesser } 18268f33bb24SBodo Stroesser 182769589c9bSSouptick Joarder static vm_fault_t tcmu_vma_fault(struct vm_fault *vmf) 18287c9e7a6fSAndy Grover { 182911bac800SDave Jiang struct tcmu_dev *udev = vmf->vma->vm_private_data; 18307c9e7a6fSAndy Grover struct uio_info *info = &udev->uio_info; 18317c9e7a6fSAndy Grover struct page *page; 18327c9e7a6fSAndy Grover unsigned long offset; 18337c9e7a6fSAndy Grover void *addr; 18347c9e7a6fSAndy Grover 183511bac800SDave Jiang int mi = tcmu_find_mem_index(vmf->vma); 18367c9e7a6fSAndy Grover if (mi < 0) 18377c9e7a6fSAndy Grover return VM_FAULT_SIGBUS; 18387c9e7a6fSAndy Grover 18397c9e7a6fSAndy Grover /* 18407c9e7a6fSAndy Grover * We need to subtract mi because userspace uses offset = N*PAGE_SIZE 18417c9e7a6fSAndy Grover * to use mem[N]. 18427c9e7a6fSAndy Grover */ 18437c9e7a6fSAndy Grover offset = (vmf->pgoff - mi) << PAGE_SHIFT; 18447c9e7a6fSAndy Grover 1845141685a3SXiubo Li if (offset < udev->data_off) { 1846141685a3SXiubo Li /* For the vmalloc()ed cmd area pages */ 18477c9e7a6fSAndy Grover addr = (void *)(unsigned long)info->mem[mi].addr + offset; 18487c9e7a6fSAndy Grover page = vmalloc_to_page(addr); 1849141685a3SXiubo Li } else { 18508b084d9dSBodo Stroesser uint32_t dpi; 1851141685a3SXiubo Li 1852b6df4b79SXiubo Li /* For the dynamically growing data area pages */ 18538b084d9dSBodo Stroesser dpi = (offset - udev->data_off) / PAGE_SIZE; 18548b084d9dSBodo Stroesser page = tcmu_try_get_data_page(udev, dpi); 1855b6df4b79SXiubo Li if (!page) 1856c1c390baSMike Christie return VM_FAULT_SIGBUS; 1857141685a3SXiubo Li } 1858141685a3SXiubo Li 18597c9e7a6fSAndy Grover get_page(page); 18607c9e7a6fSAndy Grover vmf->page = page; 18617c9e7a6fSAndy Grover return 0; 18627c9e7a6fSAndy Grover } 18637c9e7a6fSAndy Grover 18647c9e7a6fSAndy Grover static const struct vm_operations_struct tcmu_vm_ops = { 18658f33bb24SBodo Stroesser .open = tcmu_vma_open, 18668f33bb24SBodo Stroesser .close = tcmu_vma_close, 18677c9e7a6fSAndy Grover .fault = tcmu_vma_fault, 18687c9e7a6fSAndy Grover }; 18697c9e7a6fSAndy Grover 18707c9e7a6fSAndy Grover static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma) 18717c9e7a6fSAndy Grover { 18727c9e7a6fSAndy Grover struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 18737c9e7a6fSAndy Grover 18747c9e7a6fSAndy Grover vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 18757c9e7a6fSAndy Grover vma->vm_ops = &tcmu_vm_ops; 18767c9e7a6fSAndy Grover 18777c9e7a6fSAndy Grover vma->vm_private_data = udev; 18787c9e7a6fSAndy Grover 18797c9e7a6fSAndy Grover /* Ensure the mmap is exactly the right size */ 1880ecddbb7eSBodo Stroesser if (vma_pages(vma) != udev->mmap_pages) 18817c9e7a6fSAndy Grover return -EINVAL; 18827c9e7a6fSAndy Grover 18838f33bb24SBodo Stroesser tcmu_vma_open(vma); 18848f33bb24SBodo Stroesser 18857c9e7a6fSAndy Grover return 0; 18867c9e7a6fSAndy Grover } 18877c9e7a6fSAndy Grover 18887c9e7a6fSAndy Grover static int tcmu_open(struct uio_info *info, struct inode *inode) 18897c9e7a6fSAndy Grover { 18907c9e7a6fSAndy Grover struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 18917c9e7a6fSAndy Grover 18927c9e7a6fSAndy Grover /* O_EXCL not supported for char devs, so fake it? */ 18937c9e7a6fSAndy Grover if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags)) 18947c9e7a6fSAndy Grover return -EBUSY; 18957c9e7a6fSAndy Grover 1896b6df4b79SXiubo Li udev->inode = inode; 1897b6df4b79SXiubo Li 18987c9e7a6fSAndy Grover pr_debug("open\n"); 18997c9e7a6fSAndy Grover 19007c9e7a6fSAndy Grover return 0; 19017c9e7a6fSAndy Grover } 19027c9e7a6fSAndy Grover 19037c9e7a6fSAndy Grover static int tcmu_release(struct uio_info *info, struct inode *inode) 19047c9e7a6fSAndy Grover { 19057c9e7a6fSAndy Grover struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); 19067c9e7a6fSAndy Grover 19077c9e7a6fSAndy Grover clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags); 19087c9e7a6fSAndy Grover 19097c9e7a6fSAndy Grover pr_debug("close\n"); 19108f33bb24SBodo Stroesser 19117c9e7a6fSAndy Grover return 0; 19127c9e7a6fSAndy Grover } 19137c9e7a6fSAndy Grover 19149de3a1efSMike Christie static int tcmu_init_genl_cmd_reply(struct tcmu_dev *udev, int cmd) 1915b3af66e2SMike Christie { 1916b3af66e2SMike Christie struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; 1917b3af66e2SMike Christie 1918b3af66e2SMike Christie if (!tcmu_kern_cmd_reply_supported) 19199de3a1efSMike Christie return 0; 1920b849b456SKenjiro Nakayama 1921b849b456SKenjiro Nakayama if (udev->nl_reply_supported <= 0) 19229de3a1efSMike Christie return 0; 1923b849b456SKenjiro Nakayama 19243228691fSMike Christie mutex_lock(&tcmu_nl_cmd_mutex); 1925b3af66e2SMike Christie 1926bdaeedc1SMike Christie if (tcmu_netlink_blocked) { 1927bdaeedc1SMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 1928bdaeedc1SMike Christie pr_warn("Failing nl cmd %d on %s. Interface is blocked.\n", cmd, 1929bdaeedc1SMike Christie udev->name); 1930bdaeedc1SMike Christie return -EAGAIN; 1931bdaeedc1SMike Christie } 1932b3af66e2SMike Christie 1933b3af66e2SMike Christie if (nl_cmd->cmd != TCMU_CMD_UNSPEC) { 19343228691fSMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 19359de3a1efSMike Christie pr_warn("netlink cmd %d already executing on %s\n", 19369de3a1efSMike Christie nl_cmd->cmd, udev->name); 19379de3a1efSMike Christie return -EBUSY; 1938b3af66e2SMike Christie } 1939b3af66e2SMike Christie 1940b3af66e2SMike Christie memset(nl_cmd, 0, sizeof(*nl_cmd)); 1941b3af66e2SMike Christie nl_cmd->cmd = cmd; 19423228691fSMike Christie nl_cmd->udev = udev; 1943b3af66e2SMike Christie init_completion(&nl_cmd->complete); 19443228691fSMike Christie INIT_LIST_HEAD(&nl_cmd->nl_list); 1945b3af66e2SMike Christie 19463228691fSMike Christie list_add_tail(&nl_cmd->nl_list, &tcmu_nl_cmd_list); 19473228691fSMike Christie 19483228691fSMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 19499de3a1efSMike Christie return 0; 1950b3af66e2SMike Christie } 1951b3af66e2SMike Christie 19527d894862SLi Zhong static void tcmu_destroy_genl_cmd_reply(struct tcmu_dev *udev) 19537d894862SLi Zhong { 19547d894862SLi Zhong struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; 19557d894862SLi Zhong 19567d894862SLi Zhong if (!tcmu_kern_cmd_reply_supported) 19577d894862SLi Zhong return; 19587d894862SLi Zhong 19597d894862SLi Zhong if (udev->nl_reply_supported <= 0) 19607d894862SLi Zhong return; 19617d894862SLi Zhong 19627d894862SLi Zhong mutex_lock(&tcmu_nl_cmd_mutex); 19637d894862SLi Zhong 19647d894862SLi Zhong list_del(&nl_cmd->nl_list); 19657d894862SLi Zhong memset(nl_cmd, 0, sizeof(*nl_cmd)); 19667d894862SLi Zhong 19677d894862SLi Zhong mutex_unlock(&tcmu_nl_cmd_mutex); 19687d894862SLi Zhong } 19697d894862SLi Zhong 1970b3af66e2SMike Christie static int tcmu_wait_genl_cmd_reply(struct tcmu_dev *udev) 1971b3af66e2SMike Christie { 1972b3af66e2SMike Christie struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; 1973b3af66e2SMike Christie int ret; 1974b3af66e2SMike Christie 1975b3af66e2SMike Christie if (!tcmu_kern_cmd_reply_supported) 1976b3af66e2SMike Christie return 0; 1977b3af66e2SMike Christie 1978b849b456SKenjiro Nakayama if (udev->nl_reply_supported <= 0) 1979b849b456SKenjiro Nakayama return 0; 1980b849b456SKenjiro Nakayama 1981b3af66e2SMike Christie pr_debug("sleeping for nl reply\n"); 1982b3af66e2SMike Christie wait_for_completion(&nl_cmd->complete); 1983b3af66e2SMike Christie 19843228691fSMike Christie mutex_lock(&tcmu_nl_cmd_mutex); 1985b3af66e2SMike Christie nl_cmd->cmd = TCMU_CMD_UNSPEC; 1986b3af66e2SMike Christie ret = nl_cmd->status; 19873228691fSMike Christie mutex_unlock(&tcmu_nl_cmd_mutex); 1988b3af66e2SMike Christie 198985fae482SLuis de Bethencourt return ret; 1990b3af66e2SMike Christie } 1991b3af66e2SMike Christie 19920e5aee39SZhu Lingshan static int tcmu_netlink_event_init(struct tcmu_dev *udev, 19930e5aee39SZhu Lingshan enum tcmu_genl_cmd cmd, 19940e5aee39SZhu Lingshan struct sk_buff **buf, void **hdr) 19957c9e7a6fSAndy Grover { 19967c9e7a6fSAndy Grover struct sk_buff *skb; 19977c9e7a6fSAndy Grover void *msg_header; 19986e14eab9SNicholas Bellinger int ret = -ENOMEM; 19997c9e7a6fSAndy Grover 20007c9e7a6fSAndy Grover skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 20017c9e7a6fSAndy Grover if (!skb) 20026e14eab9SNicholas Bellinger return ret; 20037c9e7a6fSAndy Grover 20047c9e7a6fSAndy Grover msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd); 20056e14eab9SNicholas Bellinger if (!msg_header) 20066e14eab9SNicholas Bellinger goto free_skb; 20077c9e7a6fSAndy Grover 2008b3af66e2SMike Christie ret = nla_put_string(skb, TCMU_ATTR_DEVICE, udev->uio_info.name); 20096e14eab9SNicholas Bellinger if (ret < 0) 20106e14eab9SNicholas Bellinger goto free_skb; 20117c9e7a6fSAndy Grover 2012b3af66e2SMike Christie ret = nla_put_u32(skb, TCMU_ATTR_MINOR, udev->uio_info.uio_dev->minor); 2013b3af66e2SMike Christie if (ret < 0) 2014b3af66e2SMike Christie goto free_skb; 2015b3af66e2SMike Christie 2016b3af66e2SMike Christie ret = nla_put_u32(skb, TCMU_ATTR_DEVICE_ID, udev->se_dev.dev_index); 20176e14eab9SNicholas Bellinger if (ret < 0) 20186e14eab9SNicholas Bellinger goto free_skb; 20197c9e7a6fSAndy Grover 20200e5aee39SZhu Lingshan *buf = skb; 20210e5aee39SZhu Lingshan *hdr = msg_header; 20220e5aee39SZhu Lingshan return ret; 20230e5aee39SZhu Lingshan 20240e5aee39SZhu Lingshan free_skb: 20250e5aee39SZhu Lingshan nlmsg_free(skb); 20260e5aee39SZhu Lingshan return ret; 20272d76443eSMike Christie } 20282d76443eSMike Christie 20290e5aee39SZhu Lingshan static int tcmu_netlink_event_send(struct tcmu_dev *udev, 20300e5aee39SZhu Lingshan enum tcmu_genl_cmd cmd, 203106add777SMike Christie struct sk_buff *skb, void *msg_header) 20320e5aee39SZhu Lingshan { 203306add777SMike Christie int ret; 20348a45885cSBryant G. Ly 2035053c095aSJohannes Berg genlmsg_end(skb, msg_header); 20367c9e7a6fSAndy Grover 20379de3a1efSMike Christie ret = tcmu_init_genl_cmd_reply(udev, cmd); 20389de3a1efSMike Christie if (ret) { 20399de3a1efSMike Christie nlmsg_free(skb); 20409de3a1efSMike Christie return ret; 20419de3a1efSMike Christie } 2042b3af66e2SMike Christie 204320c08b36SSheng Yang ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0, 20447c9e7a6fSAndy Grover TCMU_MCGRP_CONFIG, GFP_KERNEL); 20452ff717cdSCathy Avery 20462ff717cdSCathy Avery /* Wait during an add as the listener may not be up yet */ 20472ff717cdSCathy Avery if (ret == 0 || 20482ff717cdSCathy Avery (ret == -ESRCH && cmd == TCMU_CMD_ADDED_DEVICE)) 20492ff717cdSCathy Avery return tcmu_wait_genl_cmd_reply(udev); 20507d894862SLi Zhong else 20517d894862SLi Zhong tcmu_destroy_genl_cmd_reply(udev); 20522ff717cdSCathy Avery 20530e5aee39SZhu Lingshan return ret; 20540e5aee39SZhu Lingshan } 20557c9e7a6fSAndy Grover 2056e0c240acSZhu Lingshan static int tcmu_send_dev_add_event(struct tcmu_dev *udev) 2057e0c240acSZhu Lingshan { 2058e0c240acSZhu Lingshan struct sk_buff *skb = NULL; 2059e0c240acSZhu Lingshan void *msg_header = NULL; 2060e0c240acSZhu Lingshan int ret = 0; 2061e0c240acSZhu Lingshan 2062e0c240acSZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_ADDED_DEVICE, &skb, 2063e0c240acSZhu Lingshan &msg_header); 2064e0c240acSZhu Lingshan if (ret < 0) 20657c9e7a6fSAndy Grover return ret; 206606add777SMike Christie return tcmu_netlink_event_send(udev, TCMU_CMD_ADDED_DEVICE, skb, 206706add777SMike Christie msg_header); 2068e0c240acSZhu Lingshan } 2069e0c240acSZhu Lingshan 2070f892bd8eSZhu Lingshan static int tcmu_send_dev_remove_event(struct tcmu_dev *udev) 2071f892bd8eSZhu Lingshan { 2072f892bd8eSZhu Lingshan struct sk_buff *skb = NULL; 2073f892bd8eSZhu Lingshan void *msg_header = NULL; 2074f892bd8eSZhu Lingshan int ret = 0; 2075f892bd8eSZhu Lingshan 2076f892bd8eSZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_REMOVED_DEVICE, 2077f892bd8eSZhu Lingshan &skb, &msg_header); 2078f892bd8eSZhu Lingshan if (ret < 0) 20796e14eab9SNicholas Bellinger return ret; 2080f892bd8eSZhu Lingshan return tcmu_netlink_event_send(udev, TCMU_CMD_REMOVED_DEVICE, 208106add777SMike Christie skb, msg_header); 20827c9e7a6fSAndy Grover } 20837c9e7a6fSAndy Grover 2084de8c5221SBryant G. Ly static int tcmu_update_uio_info(struct tcmu_dev *udev) 20857c9e7a6fSAndy Grover { 20867c9e7a6fSAndy Grover struct tcmu_hba *hba = udev->hba->hba_ptr; 20877c9e7a6fSAndy Grover struct uio_info *info; 20887c9e7a6fSAndy Grover char *str; 20897c9e7a6fSAndy Grover 20907c9e7a6fSAndy Grover info = &udev->uio_info; 209122c2f35fSChristophe JAILLET 209222c2f35fSChristophe JAILLET if (udev->dev_config[0]) 209322c2f35fSChristophe JAILLET str = kasprintf(GFP_KERNEL, "tcm-user/%u/%s/%s", hba->host_id, 209422c2f35fSChristophe JAILLET udev->name, udev->dev_config); 209522c2f35fSChristophe JAILLET else 209622c2f35fSChristophe JAILLET str = kasprintf(GFP_KERNEL, "tcm-user/%u/%s", hba->host_id, 209722c2f35fSChristophe JAILLET udev->name); 20987c9e7a6fSAndy Grover if (!str) 20997c9e7a6fSAndy Grover return -ENOMEM; 21007c9e7a6fSAndy Grover 2101ededd039SBryant G. Ly /* If the old string exists, free it */ 2102ededd039SBryant G. Ly kfree(info->name); 21037c9e7a6fSAndy Grover info->name = str; 21047c9e7a6fSAndy Grover 2105de8c5221SBryant G. Ly return 0; 2106de8c5221SBryant G. Ly } 2107de8c5221SBryant G. Ly 2108de8c5221SBryant G. Ly static int tcmu_configure_device(struct se_device *dev) 2109de8c5221SBryant G. Ly { 2110de8c5221SBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(dev); 2111de8c5221SBryant G. Ly struct uio_info *info; 2112de8c5221SBryant G. Ly struct tcmu_mailbox *mb; 2113f5ce815fSBodo Stroesser size_t data_size; 2114de8c5221SBryant G. Ly int ret = 0; 2115de8c5221SBryant G. Ly 2116de8c5221SBryant G. Ly ret = tcmu_update_uio_info(udev); 2117de8c5221SBryant G. Ly if (ret) 2118de8c5221SBryant G. Ly return ret; 2119de8c5221SBryant G. Ly 2120de8c5221SBryant G. Ly info = &udev->uio_info; 2121de8c5221SBryant G. Ly 2122c97840c8SMike Christie mutex_lock(&udev->cmdr_lock); 212398effe47SAndy Shevchenko udev->data_bitmap = bitmap_zalloc(udev->max_blocks, GFP_KERNEL); 2124c97840c8SMike Christie mutex_unlock(&udev->cmdr_lock); 2125a24e7917SWei Yongjun if (!udev->data_bitmap) { 2126a24e7917SWei Yongjun ret = -ENOMEM; 212780eb8761SMike Christie goto err_bitmap_alloc; 2128a24e7917SWei Yongjun } 212980eb8761SMike Christie 2130ecddbb7eSBodo Stroesser mb = vzalloc(MB_CMDR_SIZE); 2131ecddbb7eSBodo Stroesser if (!mb) { 21327c9e7a6fSAndy Grover ret = -ENOMEM; 21337c9e7a6fSAndy Grover goto err_vzalloc; 21347c9e7a6fSAndy Grover } 21357c9e7a6fSAndy Grover 21367c9e7a6fSAndy Grover /* mailbox fits in first part of CMDR space */ 2137ecddbb7eSBodo Stroesser udev->mb_addr = mb; 2138ecddbb7eSBodo Stroesser udev->cmdr = (void *)mb + CMDR_OFF; 2139ecddbb7eSBodo Stroesser udev->cmdr_size = CMDR_SIZE; 2140ecddbb7eSBodo Stroesser udev->data_off = MB_CMDR_SIZE; 2141f5ce815fSBodo Stroesser data_size = TCMU_MBS_TO_PAGES(udev->data_area_mb) << PAGE_SHIFT; 2142f5ce815fSBodo Stroesser udev->mmap_pages = (data_size + MB_CMDR_SIZE) >> PAGE_SHIFT; 2143e719afdcSBodo Stroesser udev->data_blk_size = udev->data_pages_per_blk * PAGE_SIZE; 2144b6df4b79SXiubo Li udev->dbi_thresh = 0; /* Default in Idle state */ 21457c9e7a6fSAndy Grover 2146141685a3SXiubo Li /* Initialise the mailbox of the ring buffer */ 21470ad46af8SAndy Grover mb->version = TCMU_MAILBOX_VERSION; 2148bc2d214aSBodo Stroesser mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC | 2149bc2d214aSBodo Stroesser TCMU_MAILBOX_FLAG_CAP_READ_LEN | 2150bc2d214aSBodo Stroesser TCMU_MAILBOX_FLAG_CAP_TMR; 21517c9e7a6fSAndy Grover mb->cmdr_off = CMDR_OFF; 21527c9e7a6fSAndy Grover mb->cmdr_size = udev->cmdr_size; 21537c9e7a6fSAndy Grover 21547c9e7a6fSAndy Grover WARN_ON(!PAGE_ALIGNED(udev->data_off)); 2155f5ce815fSBodo Stroesser WARN_ON(data_size % PAGE_SIZE); 21567c9e7a6fSAndy Grover 2157ac64a2ceSDavid Disseldorp info->version = __stringify(TCMU_MAILBOX_VERSION); 21587c9e7a6fSAndy Grover 21597c9e7a6fSAndy Grover info->mem[0].name = "tcm-user command & data buffer"; 21600633e123SArnd Bergmann info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr; 2161f5ce815fSBodo Stroesser info->mem[0].size = data_size + MB_CMDR_SIZE; 2162141685a3SXiubo Li info->mem[0].memtype = UIO_MEM_NONE; 21637c9e7a6fSAndy Grover 21647c9e7a6fSAndy Grover info->irqcontrol = tcmu_irqcontrol; 21657c9e7a6fSAndy Grover info->irq = UIO_IRQ_CUSTOM; 21667c9e7a6fSAndy Grover 21677c9e7a6fSAndy Grover info->mmap = tcmu_mmap; 21687c9e7a6fSAndy Grover info->open = tcmu_open; 21697c9e7a6fSAndy Grover info->release = tcmu_release; 21707c9e7a6fSAndy Grover 21717c9e7a6fSAndy Grover ret = uio_register_device(tcmu_root_device, info); 21727c9e7a6fSAndy Grover if (ret) 21737c9e7a6fSAndy Grover goto err_register; 21747c9e7a6fSAndy Grover 217581ee28deSSheng Yang /* User can set hw_block_size before enable the device */ 217681ee28deSSheng Yang if (dev->dev_attrib.hw_block_size == 0) 21777c9e7a6fSAndy Grover dev->dev_attrib.hw_block_size = 512; 217881ee28deSSheng Yang /* Other attributes can be configured in userspace */ 21793abaa2bfSMike Christie if (!dev->dev_attrib.hw_max_sectors) 21807c9e7a6fSAndy Grover dev->dev_attrib.hw_max_sectors = 128; 21819a8bb606SBryant G. Ly if (!dev->dev_attrib.emulate_write_cache) 21829a8bb606SBryant G. Ly dev->dev_attrib.emulate_write_cache = 0; 21837c9e7a6fSAndy Grover dev->dev_attrib.hw_queue_depth = 128; 21847c9e7a6fSAndy Grover 2185b849b456SKenjiro Nakayama /* If user didn't explicitly disable netlink reply support, use 2186b849b456SKenjiro Nakayama * module scope setting. 2187b849b456SKenjiro Nakayama */ 2188b849b456SKenjiro Nakayama if (udev->nl_reply_supported >= 0) 2189b849b456SKenjiro Nakayama udev->nl_reply_supported = tcmu_kern_cmd_reply_supported; 2190b849b456SKenjiro Nakayama 2191f3cdbe39SMike Christie /* 2192f3cdbe39SMike Christie * Get a ref incase userspace does a close on the uio device before 2193f3cdbe39SMike Christie * LIO has initiated tcmu_free_device. 2194f3cdbe39SMike Christie */ 2195f3cdbe39SMike Christie kref_get(&udev->kref); 2196f3cdbe39SMike Christie 2197e0c240acSZhu Lingshan ret = tcmu_send_dev_add_event(udev); 21987c9e7a6fSAndy Grover if (ret) 21997c9e7a6fSAndy Grover goto err_netlink; 22007c9e7a6fSAndy Grover 2201b6df4b79SXiubo Li mutex_lock(&root_udev_mutex); 2202b6df4b79SXiubo Li list_add(&udev->node, &root_udev); 2203b6df4b79SXiubo Li mutex_unlock(&root_udev_mutex); 2204b6df4b79SXiubo Li 22057c9e7a6fSAndy Grover return 0; 22067c9e7a6fSAndy Grover 22077c9e7a6fSAndy Grover err_netlink: 2208f3cdbe39SMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 22097c9e7a6fSAndy Grover uio_unregister_device(&udev->uio_info); 22107c9e7a6fSAndy Grover err_register: 22117c9e7a6fSAndy Grover vfree(udev->mb_addr); 2212c22adc0bSXiubo Li udev->mb_addr = NULL; 22137c9e7a6fSAndy Grover err_vzalloc: 221498effe47SAndy Shevchenko bitmap_free(udev->data_bitmap); 221580eb8761SMike Christie udev->data_bitmap = NULL; 221680eb8761SMike Christie err_bitmap_alloc: 22177c9e7a6fSAndy Grover kfree(info->name); 2218f3cdbe39SMike Christie info->name = NULL; 22197c9e7a6fSAndy Grover 22207c9e7a6fSAndy Grover return ret; 22217c9e7a6fSAndy Grover } 22227c9e7a6fSAndy Grover 22237c9e7a6fSAndy Grover static void tcmu_free_device(struct se_device *dev) 22247c9e7a6fSAndy Grover { 22257c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 222692634706SMike Christie 222792634706SMike Christie /* release ref from init */ 222892634706SMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 222992634706SMike Christie } 223092634706SMike Christie 223192634706SMike Christie static void tcmu_destroy_device(struct se_device *dev) 223292634706SMike Christie { 223392634706SMike Christie struct tcmu_dev *udev = TCMU_DEV(dev); 22347c9e7a6fSAndy Grover 22359103575aSMike Christie del_timer_sync(&udev->cmd_timer); 22369103575aSMike Christie del_timer_sync(&udev->qfull_timer); 22377c9e7a6fSAndy Grover 2238b6df4b79SXiubo Li mutex_lock(&root_udev_mutex); 2239b6df4b79SXiubo Li list_del(&udev->node); 2240b6df4b79SXiubo Li mutex_unlock(&root_udev_mutex); 2241b6df4b79SXiubo Li 2242f892bd8eSZhu Lingshan tcmu_send_dev_remove_event(udev); 22437c9e7a6fSAndy Grover 22447c9e7a6fSAndy Grover uio_unregister_device(&udev->uio_info); 22459260695dSMike Christie 22469260695dSMike Christie /* release ref from configure */ 22479260695dSMike Christie kref_put(&udev->kref, tcmu_dev_kref_release); 22487c9e7a6fSAndy Grover } 22497c9e7a6fSAndy Grover 2250892782caSMike Christie static void tcmu_unblock_dev(struct tcmu_dev *udev) 2251892782caSMike Christie { 2252892782caSMike Christie mutex_lock(&udev->cmdr_lock); 2253892782caSMike Christie clear_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags); 2254892782caSMike Christie mutex_unlock(&udev->cmdr_lock); 2255892782caSMike Christie } 2256892782caSMike Christie 2257892782caSMike Christie static void tcmu_block_dev(struct tcmu_dev *udev) 2258892782caSMike Christie { 2259892782caSMike Christie mutex_lock(&udev->cmdr_lock); 2260892782caSMike Christie 2261892782caSMike Christie if (test_and_set_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) 2262892782caSMike Christie goto unlock; 2263892782caSMike Christie 2264892782caSMike Christie /* complete IO that has executed successfully */ 2265892782caSMike Christie tcmu_handle_completions(udev); 2266892782caSMike Christie /* fail IO waiting to be queued */ 2267a94a2572SXiubo Li run_qfull_queue(udev, true); 2268892782caSMike Christie 2269892782caSMike Christie unlock: 2270892782caSMike Christie mutex_unlock(&udev->cmdr_lock); 2271892782caSMike Christie } 2272892782caSMike Christie 2273892782caSMike Christie static void tcmu_reset_ring(struct tcmu_dev *udev, u8 err_level) 2274892782caSMike Christie { 2275892782caSMike Christie struct tcmu_mailbox *mb; 2276892782caSMike Christie struct tcmu_cmd *cmd; 2277d3cbb743SBodo Stroesser unsigned long i; 2278892782caSMike Christie 2279892782caSMike Christie mutex_lock(&udev->cmdr_lock); 2280892782caSMike Christie 2281d3cbb743SBodo Stroesser xa_for_each(&udev->commands, i, cmd) { 2282892782caSMike Christie pr_debug("removing cmd %u on dev %s from ring (is expired %d)\n", 2283892782caSMike Christie cmd->cmd_id, udev->name, 2284892782caSMike Christie test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)); 2285892782caSMike Christie 2286d3cbb743SBodo Stroesser xa_erase(&udev->commands, i); 2287892782caSMike Christie if (!test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { 2288a86a7586SDmitry Fomichev WARN_ON(!cmd->se_cmd); 2289a94a2572SXiubo Li list_del_init(&cmd->queue_entry); 2290780e1384SShin'ichiro Kawasaki cmd->se_cmd->priv = NULL; 2291892782caSMike Christie if (err_level == 1) { 2292892782caSMike Christie /* 2293892782caSMike Christie * Userspace was not able to start the 2294892782caSMike Christie * command or it is retryable. 2295892782caSMike Christie */ 2296892782caSMike Christie target_complete_cmd(cmd->se_cmd, SAM_STAT_BUSY); 2297892782caSMike Christie } else { 2298892782caSMike Christie /* hard failure */ 2299892782caSMike Christie target_complete_cmd(cmd->se_cmd, 2300892782caSMike Christie SAM_STAT_CHECK_CONDITION); 2301892782caSMike Christie } 2302892782caSMike Christie } 2303892782caSMike Christie tcmu_cmd_free_data(cmd, cmd->dbi_cnt); 2304892782caSMike Christie tcmu_free_cmd(cmd); 2305892782caSMike Christie } 2306892782caSMike Christie 2307892782caSMike Christie mb = udev->mb_addr; 2308892782caSMike Christie tcmu_flush_dcache_range(mb, sizeof(*mb)); 2309892782caSMike Christie pr_debug("mb last %u head %u tail %u\n", udev->cmdr_last_cleaned, 2310892782caSMike Christie mb->cmd_tail, mb->cmd_head); 2311892782caSMike Christie 2312892782caSMike Christie udev->cmdr_last_cleaned = 0; 2313892782caSMike Christie mb->cmd_tail = 0; 2314892782caSMike Christie mb->cmd_head = 0; 2315892782caSMike Christie tcmu_flush_dcache_range(mb, sizeof(*mb)); 2316066f79a5SBodo Stroesser clear_bit(TCMU_DEV_BIT_BROKEN, &udev->flags); 2317892782caSMike Christie 2318892782caSMike Christie del_timer(&udev->cmd_timer); 2319892782caSMike Christie 2320bc2d214aSBodo Stroesser /* 2321bc2d214aSBodo Stroesser * ring is empty and qfull queue never contains aborted commands. 2322bc2d214aSBodo Stroesser * So TMRs in tmr queue do not contain relevant cmd_ids. 2323bc2d214aSBodo Stroesser * After a ring reset userspace should do a fresh start, so 2324bc2d214aSBodo Stroesser * even LUN RESET message is no longer relevant. 2325bc2d214aSBodo Stroesser * Therefore remove all TMRs from qfull queue 2326bc2d214aSBodo Stroesser */ 2327bc2d214aSBodo Stroesser tcmu_remove_all_queued_tmr(udev); 2328bc2d214aSBodo Stroesser 232961fb2482SBodo Stroesser run_qfull_queue(udev, false); 233061fb2482SBodo Stroesser 2331892782caSMike Christie mutex_unlock(&udev->cmdr_lock); 2332892782caSMike Christie } 2333892782caSMike Christie 23347c9e7a6fSAndy Grover enum { 23353abaa2bfSMike Christie Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors, 233608976cb5SBodo Stroesser Opt_nl_reply_supported, Opt_max_data_area_mb, Opt_data_pages_per_blk, 233708976cb5SBodo Stroesser Opt_err, 23387c9e7a6fSAndy Grover }; 23397c9e7a6fSAndy Grover 23407c9e7a6fSAndy Grover static match_table_t tokens = { 23417c9e7a6fSAndy Grover {Opt_dev_config, "dev_config=%s"}, 23420e0d7526SMike Christie {Opt_dev_size, "dev_size=%s"}, 2343b60cb1f8SMike Christie {Opt_hw_block_size, "hw_block_size=%d"}, 2344b60cb1f8SMike Christie {Opt_hw_max_sectors, "hw_max_sectors=%d"}, 2345b849b456SKenjiro Nakayama {Opt_nl_reply_supported, "nl_reply_supported=%d"}, 2346c97840c8SMike Christie {Opt_max_data_area_mb, "max_data_area_mb=%d"}, 234708976cb5SBodo Stroesser {Opt_data_pages_per_blk, "data_pages_per_blk=%d"}, 23487c9e7a6fSAndy Grover {Opt_err, NULL} 23497c9e7a6fSAndy Grover }; 23507c9e7a6fSAndy Grover 23513abaa2bfSMike Christie static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib) 23523abaa2bfSMike Christie { 2353b60cb1f8SMike Christie int val, ret; 23543abaa2bfSMike Christie 2355b60cb1f8SMike Christie ret = match_int(arg, &val); 23563abaa2bfSMike Christie if (ret < 0) { 2357b60cb1f8SMike Christie pr_err("match_int() failed for dev attrib. Error %d.\n", 2358b60cb1f8SMike Christie ret); 23593abaa2bfSMike Christie return ret; 23603abaa2bfSMike Christie } 2361b60cb1f8SMike Christie 2362b60cb1f8SMike Christie if (val <= 0) { 2363b60cb1f8SMike Christie pr_err("Invalid dev attrib value %d. Must be greater than zero.\n", 2364b60cb1f8SMike Christie val); 23653abaa2bfSMike Christie return -EINVAL; 23663abaa2bfSMike Christie } 2367b60cb1f8SMike Christie *dev_attrib = val; 23683abaa2bfSMike Christie return 0; 23693abaa2bfSMike Christie } 23703abaa2bfSMike Christie 2371c97840c8SMike Christie static int tcmu_set_max_blocks_param(struct tcmu_dev *udev, substring_t *arg) 2372c97840c8SMike Christie { 2373f5ce815fSBodo Stroesser int val, ret; 2374e719afdcSBodo Stroesser uint32_t pages_per_blk = udev->data_pages_per_blk; 2375c97840c8SMike Christie 2376c97840c8SMike Christie ret = match_int(arg, &val); 2377c97840c8SMike Christie if (ret < 0) { 2378c97840c8SMike Christie pr_err("match_int() failed for max_data_area_mb=. Error %d.\n", 2379c97840c8SMike Christie ret); 2380c97840c8SMike Christie return ret; 2381c97840c8SMike Christie } 2382f5ce815fSBodo Stroesser if (val <= 0) { 2383c97840c8SMike Christie pr_err("Invalid max_data_area %d.\n", val); 2384c97840c8SMike Christie return -EINVAL; 2385c97840c8SMike Christie } 2386f5ce815fSBodo Stroesser if (val > TCMU_PAGES_TO_MBS(tcmu_global_max_pages)) { 2387f5ce815fSBodo Stroesser pr_err("%d is too large. Adjusting max_data_area_mb to global limit of %u\n", 2388f5ce815fSBodo Stroesser val, TCMU_PAGES_TO_MBS(tcmu_global_max_pages)); 2389f5ce815fSBodo Stroesser val = TCMU_PAGES_TO_MBS(tcmu_global_max_pages); 2390f5ce815fSBodo Stroesser } 2391e719afdcSBodo Stroesser if (TCMU_MBS_TO_PAGES(val) < pages_per_blk) { 2392e719afdcSBodo Stroesser pr_err("Invalid max_data_area %d (%zu pages): smaller than data_pages_per_blk (%u pages).\n", 2393e719afdcSBodo Stroesser val, TCMU_MBS_TO_PAGES(val), pages_per_blk); 2394f5ce815fSBodo Stroesser return -EINVAL; 2395f5ce815fSBodo Stroesser } 2396c97840c8SMike Christie 2397c97840c8SMike Christie mutex_lock(&udev->cmdr_lock); 2398c97840c8SMike Christie if (udev->data_bitmap) { 2399c97840c8SMike Christie pr_err("Cannot set max_data_area_mb after it has been enabled.\n"); 2400c97840c8SMike Christie ret = -EINVAL; 2401c97840c8SMike Christie goto unlock; 2402c97840c8SMike Christie } 2403c97840c8SMike Christie 2404f5ce815fSBodo Stroesser udev->data_area_mb = val; 2405e719afdcSBodo Stroesser udev->max_blocks = TCMU_MBS_TO_PAGES(val) / pages_per_blk; 2406c97840c8SMike Christie 2407c97840c8SMike Christie unlock: 2408c97840c8SMike Christie mutex_unlock(&udev->cmdr_lock); 2409c97840c8SMike Christie return ret; 2410c97840c8SMike Christie } 2411c97840c8SMike Christie 241208976cb5SBodo Stroesser static int tcmu_set_data_pages_per_blk(struct tcmu_dev *udev, substring_t *arg) 241308976cb5SBodo Stroesser { 241408976cb5SBodo Stroesser int val, ret; 241508976cb5SBodo Stroesser 241608976cb5SBodo Stroesser ret = match_int(arg, &val); 241708976cb5SBodo Stroesser if (ret < 0) { 241808976cb5SBodo Stroesser pr_err("match_int() failed for data_pages_per_blk=. Error %d.\n", 241908976cb5SBodo Stroesser ret); 242008976cb5SBodo Stroesser return ret; 242108976cb5SBodo Stroesser } 242208976cb5SBodo Stroesser 242308976cb5SBodo Stroesser if (val > TCMU_MBS_TO_PAGES(udev->data_area_mb)) { 242408976cb5SBodo Stroesser pr_err("Invalid data_pages_per_blk %d: greater than max_data_area_mb %d -> %zd pages).\n", 242508976cb5SBodo Stroesser val, udev->data_area_mb, 242608976cb5SBodo Stroesser TCMU_MBS_TO_PAGES(udev->data_area_mb)); 242708976cb5SBodo Stroesser return -EINVAL; 242808976cb5SBodo Stroesser } 242908976cb5SBodo Stroesser 243008976cb5SBodo Stroesser mutex_lock(&udev->cmdr_lock); 243108976cb5SBodo Stroesser if (udev->data_bitmap) { 243208976cb5SBodo Stroesser pr_err("Cannot set data_pages_per_blk after it has been enabled.\n"); 243308976cb5SBodo Stroesser ret = -EINVAL; 243408976cb5SBodo Stroesser goto unlock; 243508976cb5SBodo Stroesser } 243608976cb5SBodo Stroesser 243708976cb5SBodo Stroesser udev->data_pages_per_blk = val; 243808976cb5SBodo Stroesser udev->max_blocks = TCMU_MBS_TO_PAGES(udev->data_area_mb) / val; 243908976cb5SBodo Stroesser 244008976cb5SBodo Stroesser unlock: 244108976cb5SBodo Stroesser mutex_unlock(&udev->cmdr_lock); 244208976cb5SBodo Stroesser return ret; 244308976cb5SBodo Stroesser } 244408976cb5SBodo Stroesser 24457c9e7a6fSAndy Grover static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev, 24467c9e7a6fSAndy Grover const char *page, ssize_t count) 24477c9e7a6fSAndy Grover { 24487c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 24490e0d7526SMike Christie char *orig, *ptr, *opts; 24507c9e7a6fSAndy Grover substring_t args[MAX_OPT_ARGS]; 2451c97840c8SMike Christie int ret = 0, token; 24527c9e7a6fSAndy Grover 24537c9e7a6fSAndy Grover opts = kstrdup(page, GFP_KERNEL); 24547c9e7a6fSAndy Grover if (!opts) 24557c9e7a6fSAndy Grover return -ENOMEM; 24567c9e7a6fSAndy Grover 24577c9e7a6fSAndy Grover orig = opts; 24587c9e7a6fSAndy Grover 24597c9e7a6fSAndy Grover while ((ptr = strsep(&opts, ",\n")) != NULL) { 24607c9e7a6fSAndy Grover if (!*ptr) 24617c9e7a6fSAndy Grover continue; 24627c9e7a6fSAndy Grover 24637c9e7a6fSAndy Grover token = match_token(ptr, tokens, args); 24647c9e7a6fSAndy Grover switch (token) { 24657c9e7a6fSAndy Grover case Opt_dev_config: 24667c9e7a6fSAndy Grover if (match_strlcpy(udev->dev_config, &args[0], 24677c9e7a6fSAndy Grover TCMU_CONFIG_LEN) == 0) { 24687c9e7a6fSAndy Grover ret = -EINVAL; 24697c9e7a6fSAndy Grover break; 24707c9e7a6fSAndy Grover } 24717c9e7a6fSAndy Grover pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config); 24727c9e7a6fSAndy Grover break; 24737c9e7a6fSAndy Grover case Opt_dev_size: 24740e0d7526SMike Christie ret = match_u64(&args[0], &udev->dev_size); 24757c9e7a6fSAndy Grover if (ret < 0) 24760e0d7526SMike Christie pr_err("match_u64() failed for dev_size=. Error %d.\n", 24770e0d7526SMike Christie ret); 24787c9e7a6fSAndy Grover break; 24799c1cd1b6SAndy Grover case Opt_hw_block_size: 24803abaa2bfSMike Christie ret = tcmu_set_dev_attrib(&args[0], 24813abaa2bfSMike Christie &(dev->dev_attrib.hw_block_size)); 24829c1cd1b6SAndy Grover break; 24833abaa2bfSMike Christie case Opt_hw_max_sectors: 24843abaa2bfSMike Christie ret = tcmu_set_dev_attrib(&args[0], 24853abaa2bfSMike Christie &(dev->dev_attrib.hw_max_sectors)); 24869c1cd1b6SAndy Grover break; 2487b849b456SKenjiro Nakayama case Opt_nl_reply_supported: 2488b60cb1f8SMike Christie ret = match_int(&args[0], &udev->nl_reply_supported); 2489b849b456SKenjiro Nakayama if (ret < 0) 2490b60cb1f8SMike Christie pr_err("match_int() failed for nl_reply_supported=. Error %d.\n", 2491b60cb1f8SMike Christie ret); 2492b849b456SKenjiro Nakayama break; 249380eb8761SMike Christie case Opt_max_data_area_mb: 2494c97840c8SMike Christie ret = tcmu_set_max_blocks_param(udev, &args[0]); 249580eb8761SMike Christie break; 249608976cb5SBodo Stroesser case Opt_data_pages_per_blk: 249708976cb5SBodo Stroesser ret = tcmu_set_data_pages_per_blk(udev, &args[0]); 249808976cb5SBodo Stroesser break; 24997c9e7a6fSAndy Grover default: 25007c9e7a6fSAndy Grover break; 25017c9e7a6fSAndy Grover } 25022579325cSMike Christie 25032579325cSMike Christie if (ret) 25042579325cSMike Christie break; 25057c9e7a6fSAndy Grover } 25067c9e7a6fSAndy Grover 25077c9e7a6fSAndy Grover kfree(orig); 25087c9e7a6fSAndy Grover return (!ret) ? count : ret; 25097c9e7a6fSAndy Grover } 25107c9e7a6fSAndy Grover 25117c9e7a6fSAndy Grover static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b) 25127c9e7a6fSAndy Grover { 25137c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 25147c9e7a6fSAndy Grover ssize_t bl = 0; 25157c9e7a6fSAndy Grover 25167c9e7a6fSAndy Grover bl = sprintf(b + bl, "Config: %s ", 25177c9e7a6fSAndy Grover udev->dev_config[0] ? udev->dev_config : "NULL"); 25180e0d7526SMike Christie bl += sprintf(b + bl, "Size: %llu ", udev->dev_size); 251908976cb5SBodo Stroesser bl += sprintf(b + bl, "MaxDataAreaMB: %u ", udev->data_area_mb); 252008976cb5SBodo Stroesser bl += sprintf(b + bl, "DataPagesPerBlk: %u\n", udev->data_pages_per_blk); 25217c9e7a6fSAndy Grover 25227c9e7a6fSAndy Grover return bl; 25237c9e7a6fSAndy Grover } 25247c9e7a6fSAndy Grover 25257c9e7a6fSAndy Grover static sector_t tcmu_get_blocks(struct se_device *dev) 25267c9e7a6fSAndy Grover { 25277c9e7a6fSAndy Grover struct tcmu_dev *udev = TCMU_DEV(dev); 25287c9e7a6fSAndy Grover 25297c9e7a6fSAndy Grover return div_u64(udev->dev_size - dev->dev_attrib.block_size, 25307c9e7a6fSAndy Grover dev->dev_attrib.block_size); 25317c9e7a6fSAndy Grover } 25327c9e7a6fSAndy Grover 25337c9e7a6fSAndy Grover static sense_reason_t 25347c9e7a6fSAndy Grover tcmu_parse_cdb(struct se_cmd *cmd) 25357c9e7a6fSAndy Grover { 253602eb924fSAndy Grover return passthrough_parse_cdb(cmd, tcmu_queue_cmd); 25379c1cd1b6SAndy Grover } 25389c1cd1b6SAndy Grover 25397d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page) 25407d7a7435SNicholas Bellinger { 25417d7a7435SNicholas Bellinger struct se_dev_attrib *da = container_of(to_config_group(item), 25427d7a7435SNicholas Bellinger struct se_dev_attrib, da_group); 2543b5ab697cSKenjiro Nakayama struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 25447d7a7435SNicholas Bellinger 25457d7a7435SNicholas Bellinger return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC); 25467d7a7435SNicholas Bellinger } 25477d7a7435SNicholas Bellinger 25487d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page, 25497d7a7435SNicholas Bellinger size_t count) 25507d7a7435SNicholas Bellinger { 25517d7a7435SNicholas Bellinger struct se_dev_attrib *da = container_of(to_config_group(item), 25527d7a7435SNicholas Bellinger struct se_dev_attrib, da_group); 25537d7a7435SNicholas Bellinger struct tcmu_dev *udev = container_of(da->da_dev, 25547d7a7435SNicholas Bellinger struct tcmu_dev, se_dev); 25557d7a7435SNicholas Bellinger u32 val; 25567d7a7435SNicholas Bellinger int ret; 25577d7a7435SNicholas Bellinger 25587d7a7435SNicholas Bellinger if (da->da_dev->export_count) { 25597d7a7435SNicholas Bellinger pr_err("Unable to set tcmu cmd_time_out while exports exist\n"); 25607d7a7435SNicholas Bellinger return -EINVAL; 25617d7a7435SNicholas Bellinger } 25627d7a7435SNicholas Bellinger 25637d7a7435SNicholas Bellinger ret = kstrtou32(page, 0, &val); 25647d7a7435SNicholas Bellinger if (ret < 0) 25657d7a7435SNicholas Bellinger return ret; 25667d7a7435SNicholas Bellinger 25677d7a7435SNicholas Bellinger udev->cmd_time_out = val * MSEC_PER_SEC; 25687d7a7435SNicholas Bellinger return count; 25697d7a7435SNicholas Bellinger } 25707d7a7435SNicholas Bellinger CONFIGFS_ATTR(tcmu_, cmd_time_out); 25717d7a7435SNicholas Bellinger 25729103575aSMike Christie static ssize_t tcmu_qfull_time_out_show(struct config_item *item, char *page) 25739103575aSMike Christie { 25749103575aSMike Christie struct se_dev_attrib *da = container_of(to_config_group(item), 25759103575aSMike Christie struct se_dev_attrib, da_group); 25769103575aSMike Christie struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 25779103575aSMike Christie 25789103575aSMike Christie return snprintf(page, PAGE_SIZE, "%ld\n", udev->qfull_time_out <= 0 ? 25799103575aSMike Christie udev->qfull_time_out : 25809103575aSMike Christie udev->qfull_time_out / MSEC_PER_SEC); 25819103575aSMike Christie } 25829103575aSMike Christie 25839103575aSMike Christie static ssize_t tcmu_qfull_time_out_store(struct config_item *item, 25849103575aSMike Christie const char *page, size_t count) 25859103575aSMike Christie { 25869103575aSMike Christie struct se_dev_attrib *da = container_of(to_config_group(item), 25879103575aSMike Christie struct se_dev_attrib, da_group); 25889103575aSMike Christie struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 25899103575aSMike Christie s32 val; 25909103575aSMike Christie int ret; 25919103575aSMike Christie 25929103575aSMike Christie ret = kstrtos32(page, 0, &val); 25939103575aSMike Christie if (ret < 0) 25949103575aSMike Christie return ret; 25959103575aSMike Christie 25969103575aSMike Christie if (val >= 0) { 25979103575aSMike Christie udev->qfull_time_out = val * MSEC_PER_SEC; 2598125966dbSPrasanna Kumar Kalever } else if (val == -1) { 2599125966dbSPrasanna Kumar Kalever udev->qfull_time_out = val; 26009103575aSMike Christie } else { 26019103575aSMike Christie printk(KERN_ERR "Invalid qfull timeout value %d\n", val); 26029103575aSMike Christie return -EINVAL; 26039103575aSMike Christie } 26049103575aSMike Christie return count; 26059103575aSMike Christie } 26069103575aSMike Christie CONFIGFS_ATTR(tcmu_, qfull_time_out); 26079103575aSMike Christie 260880eb8761SMike Christie static ssize_t tcmu_max_data_area_mb_show(struct config_item *item, char *page) 260980eb8761SMike Christie { 261080eb8761SMike Christie struct se_dev_attrib *da = container_of(to_config_group(item), 261180eb8761SMike Christie struct se_dev_attrib, da_group); 261280eb8761SMike Christie struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 261380eb8761SMike Christie 2614f5ce815fSBodo Stroesser return snprintf(page, PAGE_SIZE, "%u\n", udev->data_area_mb); 261580eb8761SMike Christie } 261680eb8761SMike Christie CONFIGFS_ATTR_RO(tcmu_, max_data_area_mb); 261780eb8761SMike Christie 261808976cb5SBodo Stroesser static ssize_t tcmu_data_pages_per_blk_show(struct config_item *item, 261908976cb5SBodo Stroesser char *page) 262008976cb5SBodo Stroesser { 262108976cb5SBodo Stroesser struct se_dev_attrib *da = container_of(to_config_group(item), 262208976cb5SBodo Stroesser struct se_dev_attrib, da_group); 262308976cb5SBodo Stroesser struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 262408976cb5SBodo Stroesser 262508976cb5SBodo Stroesser return snprintf(page, PAGE_SIZE, "%u\n", udev->data_pages_per_blk); 262608976cb5SBodo Stroesser } 262708976cb5SBodo Stroesser CONFIGFS_ATTR_RO(tcmu_, data_pages_per_blk); 262808976cb5SBodo Stroesser 26292d76443eSMike Christie static ssize_t tcmu_dev_config_show(struct config_item *item, char *page) 2630ee018252SBryant G. Ly { 2631ee018252SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 2632ee018252SBryant G. Ly struct se_dev_attrib, da_group); 2633ee018252SBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2634ee018252SBryant G. Ly 2635ee018252SBryant G. Ly return snprintf(page, PAGE_SIZE, "%s\n", udev->dev_config); 2636ee018252SBryant G. Ly } 2637ee018252SBryant G. Ly 263802ccfb54SZhu Lingshan static int tcmu_send_dev_config_event(struct tcmu_dev *udev, 263902ccfb54SZhu Lingshan const char *reconfig_data) 264002ccfb54SZhu Lingshan { 264102ccfb54SZhu Lingshan struct sk_buff *skb = NULL; 264202ccfb54SZhu Lingshan void *msg_header = NULL; 264302ccfb54SZhu Lingshan int ret = 0; 264402ccfb54SZhu Lingshan 264502ccfb54SZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE, 264602ccfb54SZhu Lingshan &skb, &msg_header); 264702ccfb54SZhu Lingshan if (ret < 0) 264802ccfb54SZhu Lingshan return ret; 264902ccfb54SZhu Lingshan ret = nla_put_string(skb, TCMU_ATTR_DEV_CFG, reconfig_data); 265002ccfb54SZhu Lingshan if (ret < 0) { 265102ccfb54SZhu Lingshan nlmsg_free(skb); 265202ccfb54SZhu Lingshan return ret; 265302ccfb54SZhu Lingshan } 265402ccfb54SZhu Lingshan return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE, 265506add777SMike Christie skb, msg_header); 265602ccfb54SZhu Lingshan } 265702ccfb54SZhu Lingshan 265802ccfb54SZhu Lingshan 26592d76443eSMike Christie static ssize_t tcmu_dev_config_store(struct config_item *item, const char *page, 2660ee018252SBryant G. Ly size_t count) 2661ee018252SBryant G. Ly { 2662ee018252SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 2663ee018252SBryant G. Ly struct se_dev_attrib, da_group); 2664ee018252SBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 26652d76443eSMike Christie int ret, len; 2666ee018252SBryant G. Ly 26672d76443eSMike Christie len = strlen(page); 26682d76443eSMike Christie if (!len || len > TCMU_CONFIG_LEN - 1) 2669ee018252SBryant G. Ly return -EINVAL; 2670ee018252SBryant G. Ly 2671ee018252SBryant G. Ly /* Check if device has been configured before */ 267263d5be0fSMike Christie if (target_dev_configured(&udev->se_dev)) { 267302ccfb54SZhu Lingshan ret = tcmu_send_dev_config_event(udev, page); 2674ee018252SBryant G. Ly if (ret) { 2675ee018252SBryant G. Ly pr_err("Unable to reconfigure device\n"); 2676ee018252SBryant G. Ly return ret; 2677ee018252SBryant G. Ly } 2678de8c5221SBryant G. Ly strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN); 2679de8c5221SBryant G. Ly 2680de8c5221SBryant G. Ly ret = tcmu_update_uio_info(udev); 2681de8c5221SBryant G. Ly if (ret) 2682de8c5221SBryant G. Ly return ret; 2683de8c5221SBryant G. Ly return count; 2684ee018252SBryant G. Ly } 26852d76443eSMike Christie strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN); 2686ee018252SBryant G. Ly 2687ee018252SBryant G. Ly return count; 2688ee018252SBryant G. Ly } 26892d76443eSMike Christie CONFIGFS_ATTR(tcmu_, dev_config); 2690ee018252SBryant G. Ly 2691801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_show(struct config_item *item, char *page) 2692801fc54dSBryant G. Ly { 2693801fc54dSBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 2694801fc54dSBryant G. Ly struct se_dev_attrib, da_group); 2695801fc54dSBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2696801fc54dSBryant G. Ly 26970e0d7526SMike Christie return snprintf(page, PAGE_SIZE, "%llu\n", udev->dev_size); 2698801fc54dSBryant G. Ly } 2699801fc54dSBryant G. Ly 270084e28506SZhu Lingshan static int tcmu_send_dev_size_event(struct tcmu_dev *udev, u64 size) 270184e28506SZhu Lingshan { 270284e28506SZhu Lingshan struct sk_buff *skb = NULL; 270384e28506SZhu Lingshan void *msg_header = NULL; 270484e28506SZhu Lingshan int ret = 0; 270584e28506SZhu Lingshan 270684e28506SZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE, 270784e28506SZhu Lingshan &skb, &msg_header); 270884e28506SZhu Lingshan if (ret < 0) 270984e28506SZhu Lingshan return ret; 271084e28506SZhu Lingshan ret = nla_put_u64_64bit(skb, TCMU_ATTR_DEV_SIZE, 271184e28506SZhu Lingshan size, TCMU_ATTR_PAD); 271284e28506SZhu Lingshan if (ret < 0) { 271384e28506SZhu Lingshan nlmsg_free(skb); 271484e28506SZhu Lingshan return ret; 271584e28506SZhu Lingshan } 271684e28506SZhu Lingshan return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE, 271706add777SMike Christie skb, msg_header); 271884e28506SZhu Lingshan } 271984e28506SZhu Lingshan 2720801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page, 2721801fc54dSBryant G. Ly size_t count) 2722801fc54dSBryant G. Ly { 2723801fc54dSBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 2724801fc54dSBryant G. Ly struct se_dev_attrib, da_group); 2725801fc54dSBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 27262d76443eSMike Christie u64 val; 2727801fc54dSBryant G. Ly int ret; 2728801fc54dSBryant G. Ly 27292d76443eSMike Christie ret = kstrtou64(page, 0, &val); 2730801fc54dSBryant G. Ly if (ret < 0) 2731801fc54dSBryant G. Ly return ret; 2732801fc54dSBryant G. Ly 2733801fc54dSBryant G. Ly /* Check if device has been configured before */ 273463d5be0fSMike Christie if (target_dev_configured(&udev->se_dev)) { 273584e28506SZhu Lingshan ret = tcmu_send_dev_size_event(udev, val); 2736801fc54dSBryant G. Ly if (ret) { 2737801fc54dSBryant G. Ly pr_err("Unable to reconfigure device\n"); 2738801fc54dSBryant G. Ly return ret; 2739801fc54dSBryant G. Ly } 2740801fc54dSBryant G. Ly } 27412d76443eSMike Christie udev->dev_size = val; 2742801fc54dSBryant G. Ly return count; 2743801fc54dSBryant G. Ly } 2744801fc54dSBryant G. Ly CONFIGFS_ATTR(tcmu_, dev_size); 2745801fc54dSBryant G. Ly 2746b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_show(struct config_item *item, 2747b849b456SKenjiro Nakayama char *page) 2748b849b456SKenjiro Nakayama { 2749b849b456SKenjiro Nakayama struct se_dev_attrib *da = container_of(to_config_group(item), 2750b849b456SKenjiro Nakayama struct se_dev_attrib, da_group); 2751b849b456SKenjiro Nakayama struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2752b849b456SKenjiro Nakayama 2753b849b456SKenjiro Nakayama return snprintf(page, PAGE_SIZE, "%d\n", udev->nl_reply_supported); 2754b849b456SKenjiro Nakayama } 2755b849b456SKenjiro Nakayama 2756b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_store(struct config_item *item, 2757b849b456SKenjiro Nakayama const char *page, size_t count) 2758b849b456SKenjiro Nakayama { 2759b849b456SKenjiro Nakayama struct se_dev_attrib *da = container_of(to_config_group(item), 2760b849b456SKenjiro Nakayama struct se_dev_attrib, da_group); 2761b849b456SKenjiro Nakayama struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 2762b849b456SKenjiro Nakayama s8 val; 2763b849b456SKenjiro Nakayama int ret; 2764b849b456SKenjiro Nakayama 2765b849b456SKenjiro Nakayama ret = kstrtos8(page, 0, &val); 2766b849b456SKenjiro Nakayama if (ret < 0) 2767b849b456SKenjiro Nakayama return ret; 2768b849b456SKenjiro Nakayama 2769b849b456SKenjiro Nakayama udev->nl_reply_supported = val; 2770b849b456SKenjiro Nakayama return count; 2771b849b456SKenjiro Nakayama } 2772b849b456SKenjiro Nakayama CONFIGFS_ATTR(tcmu_, nl_reply_supported); 2773b849b456SKenjiro Nakayama 27749a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_show(struct config_item *item, 27759a8bb606SBryant G. Ly char *page) 27769a8bb606SBryant G. Ly { 27779a8bb606SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 27789a8bb606SBryant G. Ly struct se_dev_attrib, da_group); 27799a8bb606SBryant G. Ly 27809a8bb606SBryant G. Ly return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache); 27819a8bb606SBryant G. Ly } 27829a8bb606SBryant G. Ly 278333d065ccSZhu Lingshan static int tcmu_send_emulate_write_cache(struct tcmu_dev *udev, u8 val) 278433d065ccSZhu Lingshan { 278533d065ccSZhu Lingshan struct sk_buff *skb = NULL; 278633d065ccSZhu Lingshan void *msg_header = NULL; 278733d065ccSZhu Lingshan int ret = 0; 278833d065ccSZhu Lingshan 278933d065ccSZhu Lingshan ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE, 279033d065ccSZhu Lingshan &skb, &msg_header); 279133d065ccSZhu Lingshan if (ret < 0) 279233d065ccSZhu Lingshan return ret; 279333d065ccSZhu Lingshan ret = nla_put_u8(skb, TCMU_ATTR_WRITECACHE, val); 279433d065ccSZhu Lingshan if (ret < 0) { 279533d065ccSZhu Lingshan nlmsg_free(skb); 279633d065ccSZhu Lingshan return ret; 279733d065ccSZhu Lingshan } 279833d065ccSZhu Lingshan return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE, 279906add777SMike Christie skb, msg_header); 280033d065ccSZhu Lingshan } 280133d065ccSZhu Lingshan 28029a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_store(struct config_item *item, 28039a8bb606SBryant G. Ly const char *page, size_t count) 28049a8bb606SBryant G. Ly { 28059a8bb606SBryant G. Ly struct se_dev_attrib *da = container_of(to_config_group(item), 28069a8bb606SBryant G. Ly struct se_dev_attrib, da_group); 28071068be7bSBryant G. Ly struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 28082d76443eSMike Christie u8 val; 28099a8bb606SBryant G. Ly int ret; 28109a8bb606SBryant G. Ly 28112d76443eSMike Christie ret = kstrtou8(page, 0, &val); 28129a8bb606SBryant G. Ly if (ret < 0) 28139a8bb606SBryant G. Ly return ret; 28149a8bb606SBryant G. Ly 28151068be7bSBryant G. Ly /* Check if device has been configured before */ 281663d5be0fSMike Christie if (target_dev_configured(&udev->se_dev)) { 281733d065ccSZhu Lingshan ret = tcmu_send_emulate_write_cache(udev, val); 28181068be7bSBryant G. Ly if (ret) { 28191068be7bSBryant G. Ly pr_err("Unable to reconfigure device\n"); 28201068be7bSBryant G. Ly return ret; 28211068be7bSBryant G. Ly } 28221068be7bSBryant G. Ly } 28232d76443eSMike Christie 28242d76443eSMike Christie da->emulate_write_cache = val; 28259a8bb606SBryant G. Ly return count; 28269a8bb606SBryant G. Ly } 28279a8bb606SBryant G. Ly CONFIGFS_ATTR(tcmu_, emulate_write_cache); 28289a8bb606SBryant G. Ly 282959526d7aSBodo Stroesser static ssize_t tcmu_tmr_notification_show(struct config_item *item, char *page) 283059526d7aSBodo Stroesser { 283159526d7aSBodo Stroesser struct se_dev_attrib *da = container_of(to_config_group(item), 283259526d7aSBodo Stroesser struct se_dev_attrib, da_group); 283359526d7aSBodo Stroesser struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 283459526d7aSBodo Stroesser 283559526d7aSBodo Stroesser return snprintf(page, PAGE_SIZE, "%i\n", 283659526d7aSBodo Stroesser test_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags)); 283759526d7aSBodo Stroesser } 283859526d7aSBodo Stroesser 283959526d7aSBodo Stroesser static ssize_t tcmu_tmr_notification_store(struct config_item *item, 284059526d7aSBodo Stroesser const char *page, size_t count) 284159526d7aSBodo Stroesser { 284259526d7aSBodo Stroesser struct se_dev_attrib *da = container_of(to_config_group(item), 284359526d7aSBodo Stroesser struct se_dev_attrib, da_group); 284459526d7aSBodo Stroesser struct tcmu_dev *udev = TCMU_DEV(da->da_dev); 284559526d7aSBodo Stroesser u8 val; 284659526d7aSBodo Stroesser int ret; 284759526d7aSBodo Stroesser 284859526d7aSBodo Stroesser ret = kstrtou8(page, 0, &val); 284959526d7aSBodo Stroesser if (ret < 0) 285059526d7aSBodo Stroesser return ret; 285159526d7aSBodo Stroesser if (val > 1) 285259526d7aSBodo Stroesser return -EINVAL; 285359526d7aSBodo Stroesser 285459526d7aSBodo Stroesser if (val) 285559526d7aSBodo Stroesser set_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags); 285659526d7aSBodo Stroesser else 285759526d7aSBodo Stroesser clear_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags); 285859526d7aSBodo Stroesser return count; 285959526d7aSBodo Stroesser } 286059526d7aSBodo Stroesser CONFIGFS_ATTR(tcmu_, tmr_notification); 286159526d7aSBodo Stroesser 2862892782caSMike Christie static ssize_t tcmu_block_dev_show(struct config_item *item, char *page) 2863892782caSMike Christie { 2864892782caSMike Christie struct se_device *se_dev = container_of(to_config_group(item), 2865892782caSMike Christie struct se_device, 2866892782caSMike Christie dev_action_group); 2867892782caSMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 2868892782caSMike Christie 2869892782caSMike Christie if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) 2870892782caSMike Christie return snprintf(page, PAGE_SIZE, "%s\n", "blocked"); 2871892782caSMike Christie else 2872892782caSMike Christie return snprintf(page, PAGE_SIZE, "%s\n", "unblocked"); 2873892782caSMike Christie } 2874892782caSMike Christie 2875892782caSMike Christie static ssize_t tcmu_block_dev_store(struct config_item *item, const char *page, 2876892782caSMike Christie size_t count) 2877892782caSMike Christie { 2878892782caSMike Christie struct se_device *se_dev = container_of(to_config_group(item), 2879892782caSMike Christie struct se_device, 2880892782caSMike Christie dev_action_group); 2881892782caSMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 2882892782caSMike Christie u8 val; 2883892782caSMike Christie int ret; 2884892782caSMike Christie 2885a30b0473SMike Christie if (!target_dev_configured(&udev->se_dev)) { 2886a30b0473SMike Christie pr_err("Device is not configured.\n"); 2887a30b0473SMike Christie return -EINVAL; 2888a30b0473SMike Christie } 2889a30b0473SMike Christie 2890892782caSMike Christie ret = kstrtou8(page, 0, &val); 2891892782caSMike Christie if (ret < 0) 2892892782caSMike Christie return ret; 2893892782caSMike Christie 2894892782caSMike Christie if (val > 1) { 2895892782caSMike Christie pr_err("Invalid block value %d\n", val); 2896892782caSMike Christie return -EINVAL; 2897892782caSMike Christie } 2898892782caSMike Christie 2899892782caSMike Christie if (!val) 2900892782caSMike Christie tcmu_unblock_dev(udev); 2901892782caSMike Christie else 2902892782caSMike Christie tcmu_block_dev(udev); 2903892782caSMike Christie return count; 2904892782caSMike Christie } 2905892782caSMike Christie CONFIGFS_ATTR(tcmu_, block_dev); 2906892782caSMike Christie 2907892782caSMike Christie static ssize_t tcmu_reset_ring_store(struct config_item *item, const char *page, 2908892782caSMike Christie size_t count) 2909892782caSMike Christie { 2910892782caSMike Christie struct se_device *se_dev = container_of(to_config_group(item), 2911892782caSMike Christie struct se_device, 2912892782caSMike Christie dev_action_group); 2913892782caSMike Christie struct tcmu_dev *udev = TCMU_DEV(se_dev); 2914892782caSMike Christie u8 val; 2915892782caSMike Christie int ret; 2916892782caSMike Christie 2917a30b0473SMike Christie if (!target_dev_configured(&udev->se_dev)) { 2918a30b0473SMike Christie pr_err("Device is not configured.\n"); 2919a30b0473SMike Christie return -EINVAL; 2920a30b0473SMike Christie } 2921a30b0473SMike Christie 2922892782caSMike Christie ret = kstrtou8(page, 0, &val); 2923892782caSMike Christie if (ret < 0) 2924892782caSMike Christie return ret; 2925892782caSMike Christie 2926892782caSMike Christie if (val != 1 && val != 2) { 2927892782caSMike Christie pr_err("Invalid reset ring value %d\n", val); 2928892782caSMike Christie return -EINVAL; 2929892782caSMike Christie } 2930892782caSMike Christie 2931892782caSMike Christie tcmu_reset_ring(udev, val); 2932892782caSMike Christie return count; 2933892782caSMike Christie } 2934892782caSMike Christie CONFIGFS_ATTR_WO(tcmu_, reset_ring); 2935892782caSMike Christie 29365821783bSColin Ian King static struct configfs_attribute *tcmu_attrib_attrs[] = { 2937801fc54dSBryant G. Ly &tcmu_attr_cmd_time_out, 29389103575aSMike Christie &tcmu_attr_qfull_time_out, 293980eb8761SMike Christie &tcmu_attr_max_data_area_mb, 294008976cb5SBodo Stroesser &tcmu_attr_data_pages_per_blk, 29412d76443eSMike Christie &tcmu_attr_dev_config, 2942801fc54dSBryant G. Ly &tcmu_attr_dev_size, 2943801fc54dSBryant G. Ly &tcmu_attr_emulate_write_cache, 294459526d7aSBodo Stroesser &tcmu_attr_tmr_notification, 2945b849b456SKenjiro Nakayama &tcmu_attr_nl_reply_supported, 2946801fc54dSBryant G. Ly NULL, 2947801fc54dSBryant G. Ly }; 2948801fc54dSBryant G. Ly 29497d7a7435SNicholas Bellinger static struct configfs_attribute **tcmu_attrs; 29507d7a7435SNicholas Bellinger 2951892782caSMike Christie static struct configfs_attribute *tcmu_action_attrs[] = { 2952892782caSMike Christie &tcmu_attr_block_dev, 2953892782caSMike Christie &tcmu_attr_reset_ring, 2954892782caSMike Christie NULL, 2955892782caSMike Christie }; 2956892782caSMike Christie 29577d7a7435SNicholas Bellinger static struct target_backend_ops tcmu_ops = { 29587c9e7a6fSAndy Grover .name = "user", 29597c9e7a6fSAndy Grover .owner = THIS_MODULE, 296069088a04SBodo Stroesser .transport_flags_default = TRANSPORT_FLAG_PASSTHROUGH, 2961356ba2a8SBodo Stroesser .transport_flags_changeable = TRANSPORT_FLAG_PASSTHROUGH_PGR | 2962356ba2a8SBodo Stroesser TRANSPORT_FLAG_PASSTHROUGH_ALUA, 29637c9e7a6fSAndy Grover .attach_hba = tcmu_attach_hba, 29647c9e7a6fSAndy Grover .detach_hba = tcmu_detach_hba, 29657c9e7a6fSAndy Grover .alloc_device = tcmu_alloc_device, 29667c9e7a6fSAndy Grover .configure_device = tcmu_configure_device, 296792634706SMike Christie .destroy_device = tcmu_destroy_device, 29687c9e7a6fSAndy Grover .free_device = tcmu_free_device, 29696888da81SMike Christie .unplug_device = tcmu_unplug_device, 29706888da81SMike Christie .plug_device = tcmu_plug_device, 29717c9e7a6fSAndy Grover .parse_cdb = tcmu_parse_cdb, 2972bc2d214aSBodo Stroesser .tmr_notify = tcmu_tmr_notify, 29737c9e7a6fSAndy Grover .set_configfs_dev_params = tcmu_set_configfs_dev_params, 29747c9e7a6fSAndy Grover .show_configfs_dev_params = tcmu_show_configfs_dev_params, 29757c9e7a6fSAndy Grover .get_device_type = sbc_get_device_type, 29767c9e7a6fSAndy Grover .get_blocks = tcmu_get_blocks, 2977892782caSMike Christie .tb_dev_action_attrs = tcmu_action_attrs, 29787c9e7a6fSAndy Grover }; 29797c9e7a6fSAndy Grover 298089ec9cfdSMike Christie static void find_free_blocks(void) 2981b6df4b79SXiubo Li { 2982b6df4b79SXiubo Li struct tcmu_dev *udev; 2983b6df4b79SXiubo Li loff_t off; 2984f5ce815fSBodo Stroesser u32 pages_freed, total_pages_freed = 0; 2985f5ce815fSBodo Stroesser u32 start, end, block, total_blocks_freed = 0; 2986af1dd7ffSMike Christie 29878b084d9dSBodo Stroesser if (atomic_read(&global_page_count) <= tcmu_global_max_pages) 2988af1dd7ffSMike Christie return; 2989b6df4b79SXiubo Li 2990b6df4b79SXiubo Li mutex_lock(&root_udev_mutex); 2991b6df4b79SXiubo Li list_for_each_entry(udev, &root_udev, node) { 2992b6df4b79SXiubo Li mutex_lock(&udev->cmdr_lock); 2993b6df4b79SXiubo Li 2994dc335a99SMike Christie if (!target_dev_configured(&udev->se_dev)) { 2995dc335a99SMike Christie mutex_unlock(&udev->cmdr_lock); 2996dc335a99SMike Christie continue; 2997dc335a99SMike Christie } 2998dc335a99SMike Christie 2999b6df4b79SXiubo Li /* Try to complete the finished commands first */ 3000bc2d214aSBodo Stroesser if (tcmu_handle_completions(udev)) 3001bc2d214aSBodo Stroesser run_qfull_queue(udev, false); 3002b6df4b79SXiubo Li 3003af1dd7ffSMike Christie /* Skip the udevs in idle */ 3004af1dd7ffSMike Christie if (!udev->dbi_thresh) { 3005b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 3006b6df4b79SXiubo Li continue; 3007b6df4b79SXiubo Li } 3008b6df4b79SXiubo Li 3009b6df4b79SXiubo Li end = udev->dbi_max + 1; 3010b6df4b79SXiubo Li block = find_last_bit(udev->data_bitmap, end); 3011b6df4b79SXiubo Li if (block == udev->dbi_max) { 3012b6df4b79SXiubo Li /* 3013af1dd7ffSMike Christie * The last bit is dbi_max, so it is not possible 3014af1dd7ffSMike Christie * reclaim any blocks. 3015b6df4b79SXiubo Li */ 3016b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 3017b6df4b79SXiubo Li continue; 3018b6df4b79SXiubo Li } else if (block == end) { 3019b6df4b79SXiubo Li /* The current udev will goto idle state */ 3020b6df4b79SXiubo Li udev->dbi_thresh = start = 0; 3021b6df4b79SXiubo Li udev->dbi_max = 0; 3022b6df4b79SXiubo Li } else { 3023b6df4b79SXiubo Li udev->dbi_thresh = start = block + 1; 3024b6df4b79SXiubo Li udev->dbi_max = block; 3025b6df4b79SXiubo Li } 3026b6df4b79SXiubo Li 3027b6df4b79SXiubo Li /* Here will truncate the data area from off */ 3028e719afdcSBodo Stroesser off = udev->data_off + (loff_t)start * udev->data_blk_size; 3029b6df4b79SXiubo Li unmap_mapping_range(udev->inode->i_mapping, off, 0, 1); 3030b6df4b79SXiubo Li 3031b6df4b79SXiubo Li /* Release the block pages */ 3032e719afdcSBodo Stroesser pages_freed = tcmu_blocks_release(udev, start, end - 1); 3033b6df4b79SXiubo Li mutex_unlock(&udev->cmdr_lock); 3034af1dd7ffSMike Christie 3035f5ce815fSBodo Stroesser total_pages_freed += pages_freed; 3036f5ce815fSBodo Stroesser total_blocks_freed += end - start; 3037f5ce815fSBodo Stroesser pr_debug("Freed %u pages (total %u) from %u blocks (total %u) from %s.\n", 3038f5ce815fSBodo Stroesser pages_freed, total_pages_freed, end - start, 3039f5ce815fSBodo Stroesser total_blocks_freed, udev->name); 3040b6df4b79SXiubo Li } 304189ec9cfdSMike Christie mutex_unlock(&root_udev_mutex); 304289ec9cfdSMike Christie 30438b084d9dSBodo Stroesser if (atomic_read(&global_page_count) > tcmu_global_max_pages) 3044af1dd7ffSMike Christie schedule_delayed_work(&tcmu_unmap_work, msecs_to_jiffies(5000)); 3045b6df4b79SXiubo Li } 3046b6df4b79SXiubo Li 3047488ebe4cSMike Christie static void check_timedout_devices(void) 3048488ebe4cSMike Christie { 3049488ebe4cSMike Christie struct tcmu_dev *udev, *tmp_dev; 305061fb2482SBodo Stroesser struct tcmu_cmd *cmd, *tmp_cmd; 3051488ebe4cSMike Christie LIST_HEAD(devs); 3052488ebe4cSMike Christie 3053488ebe4cSMike Christie spin_lock_bh(&timed_out_udevs_lock); 3054488ebe4cSMike Christie list_splice_init(&timed_out_udevs, &devs); 3055488ebe4cSMike Christie 3056488ebe4cSMike Christie list_for_each_entry_safe(udev, tmp_dev, &devs, timedout_entry) { 3057488ebe4cSMike Christie list_del_init(&udev->timedout_entry); 3058488ebe4cSMike Christie spin_unlock_bh(&timed_out_udevs_lock); 3059488ebe4cSMike Christie 30606fddcb77SMike Christie mutex_lock(&udev->cmdr_lock); 3061a94a2572SXiubo Li 306261fb2482SBodo Stroesser /* 306361fb2482SBodo Stroesser * If cmd_time_out is disabled but qfull is set deadline 306461fb2482SBodo Stroesser * will only reflect the qfull timeout. Ignore it. 306561fb2482SBodo Stroesser */ 306661fb2482SBodo Stroesser if (udev->cmd_time_out) { 306761fb2482SBodo Stroesser list_for_each_entry_safe(cmd, tmp_cmd, 306861fb2482SBodo Stroesser &udev->inflight_queue, 306961fb2482SBodo Stroesser queue_entry) { 307061fb2482SBodo Stroesser tcmu_check_expired_ring_cmd(cmd); 307161fb2482SBodo Stroesser } 307261fb2482SBodo Stroesser tcmu_set_next_deadline(&udev->inflight_queue, 307361fb2482SBodo Stroesser &udev->cmd_timer); 307461fb2482SBodo Stroesser } 307561fb2482SBodo Stroesser list_for_each_entry_safe(cmd, tmp_cmd, &udev->qfull_queue, 307661fb2482SBodo Stroesser queue_entry) { 307761fb2482SBodo Stroesser tcmu_check_expired_queue_cmd(cmd); 307861fb2482SBodo Stroesser } 3079a94a2572SXiubo Li tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer); 3080a94a2572SXiubo Li 30816fddcb77SMike Christie mutex_unlock(&udev->cmdr_lock); 3082488ebe4cSMike Christie 3083488ebe4cSMike Christie spin_lock_bh(&timed_out_udevs_lock); 3084488ebe4cSMike Christie } 3085488ebe4cSMike Christie 3086488ebe4cSMike Christie spin_unlock_bh(&timed_out_udevs_lock); 3087488ebe4cSMike Christie } 3088488ebe4cSMike Christie 30899972cebbSMike Christie static void tcmu_unmap_work_fn(struct work_struct *work) 309089ec9cfdSMike Christie { 3091488ebe4cSMike Christie check_timedout_devices(); 309289ec9cfdSMike Christie find_free_blocks(); 309389ec9cfdSMike Christie } 309489ec9cfdSMike Christie 30957c9e7a6fSAndy Grover static int __init tcmu_module_init(void) 30967c9e7a6fSAndy Grover { 3097801fc54dSBryant G. Ly int ret, i, k, len = 0; 30987c9e7a6fSAndy Grover 30997c9e7a6fSAndy Grover BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0); 31007c9e7a6fSAndy Grover 3101af1dd7ffSMike Christie INIT_DELAYED_WORK(&tcmu_unmap_work, tcmu_unmap_work_fn); 31029972cebbSMike Christie 31037c9e7a6fSAndy Grover tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache", 31047c9e7a6fSAndy Grover sizeof(struct tcmu_cmd), 31057c9e7a6fSAndy Grover __alignof__(struct tcmu_cmd), 31067c9e7a6fSAndy Grover 0, NULL); 31077c9e7a6fSAndy Grover if (!tcmu_cmd_cache) 31087c9e7a6fSAndy Grover return -ENOMEM; 31097c9e7a6fSAndy Grover 31107c9e7a6fSAndy Grover tcmu_root_device = root_device_register("tcm_user"); 31117c9e7a6fSAndy Grover if (IS_ERR(tcmu_root_device)) { 31127c9e7a6fSAndy Grover ret = PTR_ERR(tcmu_root_device); 31137c9e7a6fSAndy Grover goto out_free_cache; 31147c9e7a6fSAndy Grover } 31157c9e7a6fSAndy Grover 31167c9e7a6fSAndy Grover ret = genl_register_family(&tcmu_genl_family); 31177c9e7a6fSAndy Grover if (ret < 0) { 31187c9e7a6fSAndy Grover goto out_unreg_device; 31197c9e7a6fSAndy Grover } 31207c9e7a6fSAndy Grover 31214703b625SBodo Stroesser for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) 31227d7a7435SNicholas Bellinger len += sizeof(struct configfs_attribute *); 31234703b625SBodo Stroesser for (i = 0; passthrough_pr_attrib_attrs[i] != NULL; i++) 3124801fc54dSBryant G. Ly len += sizeof(struct configfs_attribute *); 31254703b625SBodo Stroesser for (i = 0; tcmu_attrib_attrs[i] != NULL; i++) 31264703b625SBodo Stroesser len += sizeof(struct configfs_attribute *); 3127801fc54dSBryant G. Ly len += sizeof(struct configfs_attribute *); 31287d7a7435SNicholas Bellinger 31297d7a7435SNicholas Bellinger tcmu_attrs = kzalloc(len, GFP_KERNEL); 31307d7a7435SNicholas Bellinger if (!tcmu_attrs) { 31317d7a7435SNicholas Bellinger ret = -ENOMEM; 31327d7a7435SNicholas Bellinger goto out_unreg_genl; 31337d7a7435SNicholas Bellinger } 31347d7a7435SNicholas Bellinger 31354703b625SBodo Stroesser for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) 31367d7a7435SNicholas Bellinger tcmu_attrs[i] = passthrough_attrib_attrs[i]; 31374703b625SBodo Stroesser for (k = 0; passthrough_pr_attrib_attrs[k] != NULL; k++) 31384703b625SBodo Stroesser tcmu_attrs[i++] = passthrough_pr_attrib_attrs[k]; 31394703b625SBodo Stroesser for (k = 0; tcmu_attrib_attrs[k] != NULL; k++) 31404703b625SBodo Stroesser tcmu_attrs[i++] = tcmu_attrib_attrs[k]; 31417d7a7435SNicholas Bellinger tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs; 31427d7a7435SNicholas Bellinger 31430a06d430SChristoph Hellwig ret = transport_backend_register(&tcmu_ops); 31447c9e7a6fSAndy Grover if (ret) 31457d7a7435SNicholas Bellinger goto out_attrs; 31467c9e7a6fSAndy Grover 31477c9e7a6fSAndy Grover return 0; 31487c9e7a6fSAndy Grover 31497d7a7435SNicholas Bellinger out_attrs: 31507d7a7435SNicholas Bellinger kfree(tcmu_attrs); 31517c9e7a6fSAndy Grover out_unreg_genl: 31527c9e7a6fSAndy Grover genl_unregister_family(&tcmu_genl_family); 31537c9e7a6fSAndy Grover out_unreg_device: 31547c9e7a6fSAndy Grover root_device_unregister(tcmu_root_device); 31557c9e7a6fSAndy Grover out_free_cache: 31567c9e7a6fSAndy Grover kmem_cache_destroy(tcmu_cmd_cache); 31577c9e7a6fSAndy Grover 31587c9e7a6fSAndy Grover return ret; 31597c9e7a6fSAndy Grover } 31607c9e7a6fSAndy Grover 31617c9e7a6fSAndy Grover static void __exit tcmu_module_exit(void) 31627c9e7a6fSAndy Grover { 3163af1dd7ffSMike Christie cancel_delayed_work_sync(&tcmu_unmap_work); 31640a06d430SChristoph Hellwig target_backend_unregister(&tcmu_ops); 31657d7a7435SNicholas Bellinger kfree(tcmu_attrs); 31667c9e7a6fSAndy Grover genl_unregister_family(&tcmu_genl_family); 31677c9e7a6fSAndy Grover root_device_unregister(tcmu_root_device); 31687c9e7a6fSAndy Grover kmem_cache_destroy(tcmu_cmd_cache); 31697c9e7a6fSAndy Grover } 31707c9e7a6fSAndy Grover 31717c9e7a6fSAndy Grover MODULE_DESCRIPTION("TCM USER subsystem plugin"); 31727c9e7a6fSAndy Grover MODULE_AUTHOR("Shaohua Li <shli@kernel.org>"); 31737c9e7a6fSAndy Grover MODULE_AUTHOR("Andy Grover <agrover@redhat.com>"); 31747c9e7a6fSAndy Grover MODULE_LICENSE("GPL"); 31757c9e7a6fSAndy Grover 31767c9e7a6fSAndy Grover module_init(tcmu_module_init); 31777c9e7a6fSAndy Grover module_exit(tcmu_module_exit); 3178