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>
117c9e7a6fSAndy Grover #include <linux/idr.h>
12ba929992SBart Van Assche #include <linux/kernel.h>
137c9e7a6fSAndy Grover #include <linux/timer.h>
147c9e7a6fSAndy Grover #include <linux/parser.h>
155538d294SDavid S. Miller #include <linux/vmalloc.h>
167c9e7a6fSAndy Grover #include <linux/uio_driver.h>
17141685a3SXiubo Li #include <linux/radix-tree.h>
18ac64a2ceSDavid Disseldorp #include <linux/stringify.h>
1926418649SSheng Yang #include <linux/bitops.h>
20f5045724SBart Van Assche #include <linux/highmem.h>
217d7a7435SNicholas Bellinger #include <linux/configfs.h>
22b6df4b79SXiubo Li #include <linux/mutex.h>
239972cebbSMike Christie #include <linux/workqueue.h>
247c9e7a6fSAndy Grover #include <net/genetlink.h>
25ba929992SBart Van Assche #include <scsi/scsi_common.h>
26ba929992SBart Van Assche #include <scsi/scsi_proto.h>
277c9e7a6fSAndy Grover #include <target/target_core_base.h>
287c9e7a6fSAndy Grover #include <target/target_core_fabric.h>
297c9e7a6fSAndy Grover #include <target/target_core_backend.h>
30e9f720d6SNicholas Bellinger 
317c9e7a6fSAndy Grover #include <linux/target_core_user.h>
327c9e7a6fSAndy Grover 
33572ccdabSRandy Dunlap /**
34572ccdabSRandy Dunlap  * DOC: Userspace I/O
35572ccdabSRandy Dunlap  * Userspace I/O
36572ccdabSRandy Dunlap  * -------------
37572ccdabSRandy Dunlap  *
387c9e7a6fSAndy Grover  * Define a shared-memory interface for LIO to pass SCSI commands and
397c9e7a6fSAndy Grover  * data to userspace for processing. This is to allow backends that
407c9e7a6fSAndy Grover  * are too complex for in-kernel support to be possible.
417c9e7a6fSAndy Grover  *
427c9e7a6fSAndy Grover  * It uses the UIO framework to do a lot of the device-creation and
437c9e7a6fSAndy Grover  * introspection work for us.
447c9e7a6fSAndy Grover  *
457c9e7a6fSAndy Grover  * See the .h file for how the ring is laid out. Note that while the
467c9e7a6fSAndy Grover  * command ring is defined, the particulars of the data area are
477c9e7a6fSAndy Grover  * not. Offset values in the command entry point to other locations
48572ccdabSRandy Dunlap  * internal to the mmap-ed area. There is separate space outside the
497c9e7a6fSAndy Grover  * command ring for data buffers. This leaves maximum flexibility for
507c9e7a6fSAndy Grover  * moving buffer allocations, or even page flipping or other
517c9e7a6fSAndy Grover  * allocation techniques, without altering the command ring layout.
527c9e7a6fSAndy Grover  *
537c9e7a6fSAndy Grover  * SECURITY:
547c9e7a6fSAndy Grover  * The user process must be assumed to be malicious. There's no way to
557c9e7a6fSAndy Grover  * prevent it breaking the command ring protocol if it wants, but in
567c9e7a6fSAndy Grover  * order to prevent other issues we must only ever read *data* from
577c9e7a6fSAndy Grover  * the shared memory area, not offsets or sizes. This applies to
587c9e7a6fSAndy Grover  * command ring entries as well as the mailbox. Extra code needed for
597c9e7a6fSAndy Grover  * this may have a 'UAM' comment.
607c9e7a6fSAndy Grover  */
617c9e7a6fSAndy Grover 
627c9e7a6fSAndy Grover #define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
637c9e7a6fSAndy Grover 
64b6df4b79SXiubo Li /* For cmd area, the size is fixed 8MB */
65b6df4b79SXiubo Li #define CMDR_SIZE (8 * 1024 * 1024)
6626418649SSheng Yang 
67b6df4b79SXiubo Li /*
68b6df4b79SXiubo Li  * For data area, the block size is PAGE_SIZE and
69b6df4b79SXiubo Li  * the total size is 256K * PAGE_SIZE.
70b6df4b79SXiubo Li  */
71b6df4b79SXiubo Li #define DATA_BLOCK_SIZE PAGE_SIZE
7280eb8761SMike Christie #define DATA_BLOCK_SHIFT PAGE_SHIFT
7380eb8761SMike Christie #define DATA_BLOCK_BITS_DEF (256 * 1024)
747c9e7a6fSAndy Grover 
7580eb8761SMike Christie #define TCMU_MBS_TO_BLOCKS(_mbs) (_mbs << (20 - DATA_BLOCK_SHIFT))
7680eb8761SMike Christie #define TCMU_BLOCKS_TO_MBS(_blocks) (_blocks >> (20 - DATA_BLOCK_SHIFT))
7780eb8761SMike Christie 
78af1dd7ffSMike Christie /*
79af1dd7ffSMike Christie  * Default number of global data blocks(512K * PAGE_SIZE)
80af1dd7ffSMike Christie  * when the unmap thread will be started.
81af1dd7ffSMike Christie  */
8280eb8761SMike Christie #define TCMU_GLOBAL_MAX_BLOCKS_DEF (512 * 1024)
83b6df4b79SXiubo Li 
84b3af66e2SMike Christie static u8 tcmu_kern_cmd_reply_supported;
85bdaeedc1SMike Christie static u8 tcmu_netlink_blocked;
86b3af66e2SMike Christie 
877c9e7a6fSAndy Grover static struct device *tcmu_root_device;
887c9e7a6fSAndy Grover 
897c9e7a6fSAndy Grover struct tcmu_hba {
907c9e7a6fSAndy Grover 	u32 host_id;
917c9e7a6fSAndy Grover };
927c9e7a6fSAndy Grover 
937c9e7a6fSAndy Grover #define TCMU_CONFIG_LEN 256
947c9e7a6fSAndy Grover 
953228691fSMike Christie static DEFINE_MUTEX(tcmu_nl_cmd_mutex);
963228691fSMike Christie static LIST_HEAD(tcmu_nl_cmd_list);
973228691fSMike Christie 
983228691fSMike Christie struct tcmu_dev;
993228691fSMike Christie 
100b3af66e2SMike Christie struct tcmu_nl_cmd {
101b3af66e2SMike Christie 	/* wake up thread waiting for reply */
102b3af66e2SMike Christie 	struct completion complete;
1033228691fSMike Christie 	struct list_head nl_list;
1043228691fSMike Christie 	struct tcmu_dev *udev;
105b3af66e2SMike Christie 	int cmd;
106b3af66e2SMike Christie 	int status;
107b3af66e2SMike Christie };
108b3af66e2SMike Christie 
1097c9e7a6fSAndy Grover struct tcmu_dev {
110b6df4b79SXiubo Li 	struct list_head node;
111f3cdbe39SMike Christie 	struct kref kref;
112af1dd7ffSMike Christie 
1137c9e7a6fSAndy Grover 	struct se_device se_dev;
1147c9e7a6fSAndy Grover 
1157c9e7a6fSAndy Grover 	char *name;
1167c9e7a6fSAndy Grover 	struct se_hba *hba;
1177c9e7a6fSAndy Grover 
1187c9e7a6fSAndy Grover #define TCMU_DEV_BIT_OPEN 0
1197c9e7a6fSAndy Grover #define TCMU_DEV_BIT_BROKEN 1
120892782caSMike Christie #define TCMU_DEV_BIT_BLOCKED 2
1217c9e7a6fSAndy Grover 	unsigned long flags;
1227c9e7a6fSAndy Grover 
1237c9e7a6fSAndy Grover 	struct uio_info uio_info;
1247c9e7a6fSAndy Grover 
125b6df4b79SXiubo Li 	struct inode *inode;
126b6df4b79SXiubo Li 
1277c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb_addr;
1280e0d7526SMike Christie 	uint64_t dev_size;
1297c9e7a6fSAndy Grover 	u32 cmdr_size;
1307c9e7a6fSAndy Grover 	u32 cmdr_last_cleaned;
1313d9b9555SAndy Grover 	/* Offset of data area from start of mb */
13226418649SSheng Yang 	/* Must add data_off and mb_addr to get the address */
1337c9e7a6fSAndy Grover 	size_t data_off;
1347c9e7a6fSAndy Grover 	size_t data_size;
13580eb8761SMike Christie 	uint32_t max_blocks;
13680eb8761SMike Christie 	size_t ring_size;
13726418649SSheng Yang 
138b6df4b79SXiubo Li 	struct mutex cmdr_lock;
139a94a2572SXiubo Li 	struct list_head qfull_queue;
1407c9e7a6fSAndy Grover 
141141685a3SXiubo Li 	uint32_t dbi_max;
142b6df4b79SXiubo Li 	uint32_t dbi_thresh;
14380eb8761SMike Christie 	unsigned long *data_bitmap;
144141685a3SXiubo Li 	struct radix_tree_root data_blocks;
145141685a3SXiubo Li 
1467c9e7a6fSAndy Grover 	struct idr commands;
1477c9e7a6fSAndy Grover 
1489103575aSMike Christie 	struct timer_list cmd_timer;
149af980e46SMike Christie 	unsigned int cmd_time_out;
150a94a2572SXiubo Li 	struct list_head inflight_queue;
1519103575aSMike Christie 
1529103575aSMike Christie 	struct timer_list qfull_timer;
1539103575aSMike Christie 	int qfull_time_out;
1549103575aSMike Christie 
155488ebe4cSMike Christie 	struct list_head timedout_entry;
1567c9e7a6fSAndy Grover 
157b3af66e2SMike Christie 	struct tcmu_nl_cmd curr_nl_cmd;
158b3af66e2SMike Christie 
1597c9e7a6fSAndy Grover 	char dev_config[TCMU_CONFIG_LEN];
160b849b456SKenjiro Nakayama 
161b849b456SKenjiro Nakayama 	int nl_reply_supported;
1627c9e7a6fSAndy Grover };
1637c9e7a6fSAndy Grover 
1647c9e7a6fSAndy Grover #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
1657c9e7a6fSAndy Grover 
1667c9e7a6fSAndy Grover #define CMDR_OFF sizeof(struct tcmu_mailbox)
1677c9e7a6fSAndy Grover 
1687c9e7a6fSAndy Grover struct tcmu_cmd {
1697c9e7a6fSAndy Grover 	struct se_cmd *se_cmd;
1707c9e7a6fSAndy Grover 	struct tcmu_dev *tcmu_dev;
171a94a2572SXiubo Li 	struct list_head queue_entry;
1727c9e7a6fSAndy Grover 
1737c9e7a6fSAndy Grover 	uint16_t cmd_id;
1747c9e7a6fSAndy Grover 
17526418649SSheng Yang 	/* Can't use se_cmd when cleaning up expired cmds, because if
1767c9e7a6fSAndy Grover 	   cmd has been completed then accessing se_cmd is off limits */
177141685a3SXiubo Li 	uint32_t dbi_cnt;
178141685a3SXiubo Li 	uint32_t dbi_cur;
179141685a3SXiubo Li 	uint32_t *dbi;
1807c9e7a6fSAndy Grover 
1817c9e7a6fSAndy Grover 	unsigned long deadline;
1827c9e7a6fSAndy Grover 
1837c9e7a6fSAndy Grover #define TCMU_CMD_BIT_EXPIRED 0
1847c9e7a6fSAndy Grover 	unsigned long flags;
1857c9e7a6fSAndy Grover };
186af1dd7ffSMike Christie /*
187af1dd7ffSMike Christie  * To avoid dead lock the mutex lock order should always be:
188af1dd7ffSMike Christie  *
189af1dd7ffSMike Christie  * mutex_lock(&root_udev_mutex);
190af1dd7ffSMike Christie  * ...
191af1dd7ffSMike Christie  * mutex_lock(&tcmu_dev->cmdr_lock);
192af1dd7ffSMike Christie  * mutex_unlock(&tcmu_dev->cmdr_lock);
193af1dd7ffSMike Christie  * ...
194af1dd7ffSMike Christie  * mutex_unlock(&root_udev_mutex);
195af1dd7ffSMike Christie  */
196b6df4b79SXiubo Li static DEFINE_MUTEX(root_udev_mutex);
197b6df4b79SXiubo Li static LIST_HEAD(root_udev);
198b6df4b79SXiubo Li 
199488ebe4cSMike Christie static DEFINE_SPINLOCK(timed_out_udevs_lock);
200488ebe4cSMike Christie static LIST_HEAD(timed_out_udevs);
201488ebe4cSMike Christie 
20280eb8761SMike Christie static struct kmem_cache *tcmu_cmd_cache;
20380eb8761SMike Christie 
204b6df4b79SXiubo Li static atomic_t global_db_count = ATOMIC_INIT(0);
205af1dd7ffSMike Christie static struct delayed_work tcmu_unmap_work;
20680eb8761SMike Christie static int tcmu_global_max_blocks = TCMU_GLOBAL_MAX_BLOCKS_DEF;
207b6df4b79SXiubo Li 
20880eb8761SMike Christie static int tcmu_set_global_max_data_area(const char *str,
20980eb8761SMike Christie 					 const struct kernel_param *kp)
21080eb8761SMike Christie {
21180eb8761SMike Christie 	int ret, max_area_mb;
21280eb8761SMike Christie 
21380eb8761SMike Christie 	ret = kstrtoint(str, 10, &max_area_mb);
21480eb8761SMike Christie 	if (ret)
21580eb8761SMike Christie 		return -EINVAL;
21680eb8761SMike Christie 
21780eb8761SMike Christie 	if (max_area_mb <= 0) {
21880eb8761SMike Christie 		pr_err("global_max_data_area must be larger than 0.\n");
21980eb8761SMike Christie 		return -EINVAL;
22080eb8761SMike Christie 	}
22180eb8761SMike Christie 
22280eb8761SMike Christie 	tcmu_global_max_blocks = TCMU_MBS_TO_BLOCKS(max_area_mb);
22380eb8761SMike Christie 	if (atomic_read(&global_db_count) > tcmu_global_max_blocks)
22480eb8761SMike Christie 		schedule_delayed_work(&tcmu_unmap_work, 0);
22580eb8761SMike Christie 	else
22680eb8761SMike Christie 		cancel_delayed_work_sync(&tcmu_unmap_work);
22780eb8761SMike Christie 
22880eb8761SMike Christie 	return 0;
22980eb8761SMike Christie }
23080eb8761SMike Christie 
23180eb8761SMike Christie static int tcmu_get_global_max_data_area(char *buffer,
23280eb8761SMike Christie 					 const struct kernel_param *kp)
23380eb8761SMike Christie {
23480eb8761SMike Christie 	return sprintf(buffer, "%d", TCMU_BLOCKS_TO_MBS(tcmu_global_max_blocks));
23580eb8761SMike Christie }
23680eb8761SMike Christie 
23780eb8761SMike Christie static const struct kernel_param_ops tcmu_global_max_data_area_op = {
23880eb8761SMike Christie 	.set = tcmu_set_global_max_data_area,
23980eb8761SMike Christie 	.get = tcmu_get_global_max_data_area,
24080eb8761SMike Christie };
24180eb8761SMike Christie 
24280eb8761SMike Christie module_param_cb(global_max_data_area_mb, &tcmu_global_max_data_area_op, NULL,
24380eb8761SMike Christie 		S_IWUSR | S_IRUGO);
24480eb8761SMike Christie MODULE_PARM_DESC(global_max_data_area_mb,
24580eb8761SMike Christie 		 "Max MBs allowed to be allocated to all the tcmu device's "
24680eb8761SMike Christie 		 "data areas.");
2477c9e7a6fSAndy Grover 
248bdaeedc1SMike Christie static int tcmu_get_block_netlink(char *buffer,
249bdaeedc1SMike Christie 				  const struct kernel_param *kp)
250bdaeedc1SMike Christie {
251bdaeedc1SMike Christie 	return sprintf(buffer, "%s\n", tcmu_netlink_blocked ?
252bdaeedc1SMike Christie 		       "blocked" : "unblocked");
253bdaeedc1SMike Christie }
254bdaeedc1SMike Christie 
255bdaeedc1SMike Christie static int tcmu_set_block_netlink(const char *str,
256bdaeedc1SMike Christie 				  const struct kernel_param *kp)
257bdaeedc1SMike Christie {
258bdaeedc1SMike Christie 	int ret;
259bdaeedc1SMike Christie 	u8 val;
260bdaeedc1SMike Christie 
261bdaeedc1SMike Christie 	ret = kstrtou8(str, 0, &val);
262bdaeedc1SMike Christie 	if (ret < 0)
263bdaeedc1SMike Christie 		return ret;
264bdaeedc1SMike Christie 
265bdaeedc1SMike Christie 	if (val > 1) {
266bdaeedc1SMike Christie 		pr_err("Invalid block netlink value %u\n", val);
267bdaeedc1SMike Christie 		return -EINVAL;
268bdaeedc1SMike Christie 	}
269bdaeedc1SMike Christie 
270bdaeedc1SMike Christie 	tcmu_netlink_blocked = val;
271bdaeedc1SMike Christie 	return 0;
272bdaeedc1SMike Christie }
273bdaeedc1SMike Christie 
274bdaeedc1SMike Christie static const struct kernel_param_ops tcmu_block_netlink_op = {
275bdaeedc1SMike Christie 	.set = tcmu_set_block_netlink,
276bdaeedc1SMike Christie 	.get = tcmu_get_block_netlink,
277bdaeedc1SMike Christie };
278bdaeedc1SMike Christie 
279bdaeedc1SMike Christie module_param_cb(block_netlink, &tcmu_block_netlink_op, NULL, S_IWUSR | S_IRUGO);
280bdaeedc1SMike Christie MODULE_PARM_DESC(block_netlink, "Block new netlink commands.");
281bdaeedc1SMike Christie 
282bdaeedc1SMike Christie static int tcmu_fail_netlink_cmd(struct tcmu_nl_cmd *nl_cmd)
283bdaeedc1SMike Christie {
284bdaeedc1SMike Christie 	struct tcmu_dev *udev = nl_cmd->udev;
285bdaeedc1SMike Christie 
286bdaeedc1SMike Christie 	if (!tcmu_netlink_blocked) {
287bdaeedc1SMike Christie 		pr_err("Could not reset device's netlink interface. Netlink is not blocked.\n");
288bdaeedc1SMike Christie 		return -EBUSY;
289bdaeedc1SMike Christie 	}
290bdaeedc1SMike Christie 
291bdaeedc1SMike Christie 	if (nl_cmd->cmd != TCMU_CMD_UNSPEC) {
292bdaeedc1SMike Christie 		pr_debug("Aborting nl cmd %d on %s\n", nl_cmd->cmd, udev->name);
293bdaeedc1SMike Christie 		nl_cmd->status = -EINTR;
294bdaeedc1SMike Christie 		list_del(&nl_cmd->nl_list);
295bdaeedc1SMike Christie 		complete(&nl_cmd->complete);
296bdaeedc1SMike Christie 	}
297bdaeedc1SMike Christie 	return 0;
298bdaeedc1SMike Christie }
299bdaeedc1SMike Christie 
300bdaeedc1SMike Christie static int tcmu_set_reset_netlink(const char *str,
301bdaeedc1SMike Christie 				  const struct kernel_param *kp)
302bdaeedc1SMike Christie {
303bdaeedc1SMike Christie 	struct tcmu_nl_cmd *nl_cmd, *tmp_cmd;
304bdaeedc1SMike Christie 	int ret;
305bdaeedc1SMike Christie 	u8 val;
306bdaeedc1SMike Christie 
307bdaeedc1SMike Christie 	ret = kstrtou8(str, 0, &val);
308bdaeedc1SMike Christie 	if (ret < 0)
309bdaeedc1SMike Christie 		return ret;
310bdaeedc1SMike Christie 
311bdaeedc1SMike Christie 	if (val != 1) {
312bdaeedc1SMike Christie 		pr_err("Invalid reset netlink value %u\n", val);
313bdaeedc1SMike Christie 		return -EINVAL;
314bdaeedc1SMike Christie 	}
315bdaeedc1SMike Christie 
316bdaeedc1SMike Christie 	mutex_lock(&tcmu_nl_cmd_mutex);
317bdaeedc1SMike Christie 	list_for_each_entry_safe(nl_cmd, tmp_cmd, &tcmu_nl_cmd_list, nl_list) {
318bdaeedc1SMike Christie 		ret = tcmu_fail_netlink_cmd(nl_cmd);
319bdaeedc1SMike Christie 		if (ret)
320bdaeedc1SMike Christie 			break;
321bdaeedc1SMike Christie 	}
322bdaeedc1SMike Christie 	mutex_unlock(&tcmu_nl_cmd_mutex);
323bdaeedc1SMike Christie 
324bdaeedc1SMike Christie 	return ret;
325bdaeedc1SMike Christie }
326bdaeedc1SMike Christie 
327bdaeedc1SMike Christie static const struct kernel_param_ops tcmu_reset_netlink_op = {
328bdaeedc1SMike Christie 	.set = tcmu_set_reset_netlink,
329bdaeedc1SMike Christie };
330bdaeedc1SMike Christie 
331bdaeedc1SMike Christie module_param_cb(reset_netlink, &tcmu_reset_netlink_op, NULL, S_IWUSR);
332bdaeedc1SMike Christie MODULE_PARM_DESC(reset_netlink, "Reset netlink commands.");
333bdaeedc1SMike Christie 
3347c9e7a6fSAndy Grover /* multicast group */
3357c9e7a6fSAndy Grover enum tcmu_multicast_groups {
3367c9e7a6fSAndy Grover 	TCMU_MCGRP_CONFIG,
3377c9e7a6fSAndy Grover };
3387c9e7a6fSAndy Grover 
3397c9e7a6fSAndy Grover static const struct genl_multicast_group tcmu_mcgrps[] = {
3407c9e7a6fSAndy Grover 	[TCMU_MCGRP_CONFIG] = { .name = "config", },
3417c9e7a6fSAndy Grover };
3427c9e7a6fSAndy Grover 
343b3af66e2SMike Christie static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = {
344b3af66e2SMike Christie 	[TCMU_ATTR_DEVICE]	= { .type = NLA_STRING },
345b3af66e2SMike Christie 	[TCMU_ATTR_MINOR]	= { .type = NLA_U32 },
346b3af66e2SMike Christie 	[TCMU_ATTR_CMD_STATUS]	= { .type = NLA_S32 },
347b3af66e2SMike Christie 	[TCMU_ATTR_DEVICE_ID]	= { .type = NLA_U32 },
348b3af66e2SMike Christie 	[TCMU_ATTR_SUPP_KERN_CMD_REPLY] = { .type = NLA_U8 },
349b3af66e2SMike Christie };
350b3af66e2SMike Christie 
351b3af66e2SMike Christie static int tcmu_genl_cmd_done(struct genl_info *info, int completed_cmd)
352b3af66e2SMike Christie {
3533228691fSMike Christie 	struct tcmu_dev *udev = NULL;
354b3af66e2SMike Christie 	struct tcmu_nl_cmd *nl_cmd;
355b3af66e2SMike Christie 	int dev_id, rc, ret = 0;
356b3af66e2SMike Christie 
357b3af66e2SMike Christie 	if (!info->attrs[TCMU_ATTR_CMD_STATUS] ||
358b3af66e2SMike Christie 	    !info->attrs[TCMU_ATTR_DEVICE_ID]) {
359b3af66e2SMike Christie 		printk(KERN_ERR "TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n");
360b3af66e2SMike Christie 		return -EINVAL;
361b3af66e2SMike Christie         }
362b3af66e2SMike Christie 
363b3af66e2SMike Christie 	dev_id = nla_get_u32(info->attrs[TCMU_ATTR_DEVICE_ID]);
364b3af66e2SMike Christie 	rc = nla_get_s32(info->attrs[TCMU_ATTR_CMD_STATUS]);
365b3af66e2SMike Christie 
3663228691fSMike Christie 	mutex_lock(&tcmu_nl_cmd_mutex);
3673228691fSMike Christie 	list_for_each_entry(nl_cmd, &tcmu_nl_cmd_list, nl_list) {
3683228691fSMike Christie 		if (nl_cmd->udev->se_dev.dev_index == dev_id) {
3693228691fSMike Christie 			udev = nl_cmd->udev;
3703228691fSMike Christie 			break;
371b3af66e2SMike Christie 		}
3723228691fSMike Christie 	}
373b3af66e2SMike Christie 
3743228691fSMike Christie 	if (!udev) {
3750c218e16SMike Christie 		pr_err("tcmu nl cmd %u/%d completion could not find device with dev id %u.\n",
3763228691fSMike Christie 		       completed_cmd, rc, dev_id);
3773228691fSMike Christie 		ret = -ENODEV;
3783228691fSMike Christie 		goto unlock;
3793228691fSMike Christie 	}
3803228691fSMike Christie 	list_del(&nl_cmd->nl_list);
381b3af66e2SMike Christie 
382bdaeedc1SMike Christie 	pr_debug("%s genl cmd done got id %d curr %d done %d rc %d stat %d\n",
383bdaeedc1SMike Christie 		 udev->name, dev_id, nl_cmd->cmd, completed_cmd, rc,
384bdaeedc1SMike Christie 		 nl_cmd->status);
385b3af66e2SMike Christie 
386b3af66e2SMike Christie 	if (nl_cmd->cmd != completed_cmd) {
3873228691fSMike Christie 		pr_err("Mismatched commands on %s (Expecting reply for %d. Current %d).\n",
3883228691fSMike Christie 		       udev->name, completed_cmd, nl_cmd->cmd);
389b3af66e2SMike Christie 		ret = -EINVAL;
3903228691fSMike Christie 		goto unlock;
391b3af66e2SMike Christie 	}
392b3af66e2SMike Christie 
3933228691fSMike Christie 	nl_cmd->status = rc;
394b3af66e2SMike Christie 	complete(&nl_cmd->complete);
3953228691fSMike Christie unlock:
3963228691fSMike Christie 	mutex_unlock(&tcmu_nl_cmd_mutex);
397b3af66e2SMike Christie 	return ret;
398b3af66e2SMike Christie }
399b3af66e2SMike Christie 
400b3af66e2SMike Christie static int tcmu_genl_rm_dev_done(struct sk_buff *skb, struct genl_info *info)
401b3af66e2SMike Christie {
402b3af66e2SMike Christie 	return tcmu_genl_cmd_done(info, TCMU_CMD_REMOVED_DEVICE);
403b3af66e2SMike Christie }
404b3af66e2SMike Christie 
405b3af66e2SMike Christie static int tcmu_genl_add_dev_done(struct sk_buff *skb, struct genl_info *info)
406b3af66e2SMike Christie {
407b3af66e2SMike Christie 	return tcmu_genl_cmd_done(info, TCMU_CMD_ADDED_DEVICE);
408b3af66e2SMike Christie }
409b3af66e2SMike Christie 
410b3af66e2SMike Christie static int tcmu_genl_reconfig_dev_done(struct sk_buff *skb,
411b3af66e2SMike Christie 				       struct genl_info *info)
412b3af66e2SMike Christie {
413b3af66e2SMike Christie 	return tcmu_genl_cmd_done(info, TCMU_CMD_RECONFIG_DEVICE);
414b3af66e2SMike Christie }
415b3af66e2SMike Christie 
416b3af66e2SMike Christie static int tcmu_genl_set_features(struct sk_buff *skb, struct genl_info *info)
417b3af66e2SMike Christie {
418b3af66e2SMike Christie 	if (info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]) {
419b3af66e2SMike Christie 		tcmu_kern_cmd_reply_supported  =
420b3af66e2SMike Christie 			nla_get_u8(info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]);
421b3af66e2SMike Christie 		printk(KERN_INFO "tcmu daemon: command reply support %u.\n",
422b3af66e2SMike Christie 		       tcmu_kern_cmd_reply_supported);
423b3af66e2SMike Christie 	}
424b3af66e2SMike Christie 
425b3af66e2SMike Christie 	return 0;
426b3af66e2SMike Christie }
427b3af66e2SMike Christie 
428b3af66e2SMike Christie static const struct genl_ops tcmu_genl_ops[] = {
429b3af66e2SMike Christie 	{
430b3af66e2SMike Christie 		.cmd	= TCMU_CMD_SET_FEATURES,
431ef6243acSJohannes Berg 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
432b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
433b3af66e2SMike Christie 		.doit	= tcmu_genl_set_features,
434b3af66e2SMike Christie 	},
435b3af66e2SMike Christie 	{
436b3af66e2SMike Christie 		.cmd	= TCMU_CMD_ADDED_DEVICE_DONE,
437ef6243acSJohannes Berg 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
438b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
439b3af66e2SMike Christie 		.doit	= tcmu_genl_add_dev_done,
440b3af66e2SMike Christie 	},
441b3af66e2SMike Christie 	{
442b3af66e2SMike Christie 		.cmd	= TCMU_CMD_REMOVED_DEVICE_DONE,
443ef6243acSJohannes Berg 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
444b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
445b3af66e2SMike Christie 		.doit	= tcmu_genl_rm_dev_done,
446b3af66e2SMike Christie 	},
447b3af66e2SMike Christie 	{
448b3af66e2SMike Christie 		.cmd	= TCMU_CMD_RECONFIG_DEVICE_DONE,
449ef6243acSJohannes Berg 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
450b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
451b3af66e2SMike Christie 		.doit	= tcmu_genl_reconfig_dev_done,
452b3af66e2SMike Christie 	},
453b3af66e2SMike Christie };
454b3af66e2SMike Christie 
4557c9e7a6fSAndy Grover /* Our generic netlink family */
45656989f6dSJohannes Berg static struct genl_family tcmu_genl_family __ro_after_init = {
457489111e5SJohannes Berg 	.module = THIS_MODULE,
4587c9e7a6fSAndy Grover 	.hdrsize = 0,
4597c9e7a6fSAndy Grover 	.name = "TCM-USER",
460b3af66e2SMike Christie 	.version = 2,
4617c9e7a6fSAndy Grover 	.maxattr = TCMU_ATTR_MAX,
4623b0f31f2SJohannes Berg 	.policy = tcmu_attr_policy,
4637c9e7a6fSAndy Grover 	.mcgrps = tcmu_mcgrps,
4647c9e7a6fSAndy Grover 	.n_mcgrps = ARRAY_SIZE(tcmu_mcgrps),
46520c08b36SSheng Yang 	.netnsok = true,
466b3af66e2SMike Christie 	.ops = tcmu_genl_ops,
467b3af66e2SMike Christie 	.n_ops = ARRAY_SIZE(tcmu_genl_ops),
4687c9e7a6fSAndy Grover };
4697c9e7a6fSAndy Grover 
470141685a3SXiubo Li #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index))
471141685a3SXiubo Li #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0)
472141685a3SXiubo Li #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index))
473141685a3SXiubo Li #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++])
474141685a3SXiubo Li 
475b6df4b79SXiubo Li static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len)
476141685a3SXiubo Li {
477141685a3SXiubo Li 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
478141685a3SXiubo Li 	uint32_t i;
479141685a3SXiubo Li 
480b6df4b79SXiubo Li 	for (i = 0; i < len; i++)
481141685a3SXiubo Li 		clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap);
482141685a3SXiubo Li }
483141685a3SXiubo Li 
484b6df4b79SXiubo Li static inline bool tcmu_get_empty_block(struct tcmu_dev *udev,
485b6df4b79SXiubo Li 					struct tcmu_cmd *tcmu_cmd)
486141685a3SXiubo Li {
487b6df4b79SXiubo Li 	struct page *page;
488b6df4b79SXiubo Li 	int ret, dbi;
489141685a3SXiubo Li 
490b6df4b79SXiubo Li 	dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh);
491b6df4b79SXiubo Li 	if (dbi == udev->dbi_thresh)
492b6df4b79SXiubo Li 		return false;
493b6df4b79SXiubo Li 
494b6df4b79SXiubo Li 	page = radix_tree_lookup(&udev->data_blocks, dbi);
495b6df4b79SXiubo Li 	if (!page) {
496b6df4b79SXiubo Li 		if (atomic_add_return(1, &global_db_count) >
49780eb8761SMike Christie 				      tcmu_global_max_blocks)
498af1dd7ffSMike Christie 			schedule_delayed_work(&tcmu_unmap_work, 0);
499b6df4b79SXiubo Li 
500b6df4b79SXiubo Li 		/* try to get new page from the mm */
5010eccce86SDamien Le Moal 		page = alloc_page(GFP_NOIO);
502b6df4b79SXiubo Li 		if (!page)
503daf78c30SXiubo Li 			goto err_alloc;
504b6df4b79SXiubo Li 
505b6df4b79SXiubo Li 		ret = radix_tree_insert(&udev->data_blocks, dbi, page);
506daf78c30SXiubo Li 		if (ret)
507daf78c30SXiubo Li 			goto err_insert;
508b6df4b79SXiubo Li 	}
509b6df4b79SXiubo Li 
510141685a3SXiubo Li 	if (dbi > udev->dbi_max)
511141685a3SXiubo Li 		udev->dbi_max = dbi;
512141685a3SXiubo Li 
513141685a3SXiubo Li 	set_bit(dbi, udev->data_bitmap);
514b6df4b79SXiubo Li 	tcmu_cmd_set_dbi(tcmu_cmd, dbi);
515141685a3SXiubo Li 
516b6df4b79SXiubo Li 	return true;
517daf78c30SXiubo Li err_insert:
518daf78c30SXiubo Li 	__free_page(page);
519daf78c30SXiubo Li err_alloc:
520daf78c30SXiubo Li 	atomic_dec(&global_db_count);
521daf78c30SXiubo Li 	return false;
522141685a3SXiubo Li }
523141685a3SXiubo Li 
524b6df4b79SXiubo Li static bool tcmu_get_empty_blocks(struct tcmu_dev *udev,
525b6df4b79SXiubo Li 				  struct tcmu_cmd *tcmu_cmd)
526b6df4b79SXiubo Li {
527b6df4b79SXiubo Li 	int i;
528b6df4b79SXiubo Li 
529b6df4b79SXiubo Li 	for (i = tcmu_cmd->dbi_cur; i < tcmu_cmd->dbi_cnt; i++) {
530b6df4b79SXiubo Li 		if (!tcmu_get_empty_block(udev, tcmu_cmd))
531af1dd7ffSMike Christie 			return false;
532141685a3SXiubo Li 	}
533b6df4b79SXiubo Li 	return true;
534141685a3SXiubo Li }
535141685a3SXiubo Li 
536b6df4b79SXiubo Li static inline struct page *
537b6df4b79SXiubo Li tcmu_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
538141685a3SXiubo Li {
539141685a3SXiubo Li 	return radix_tree_lookup(&udev->data_blocks, dbi);
540141685a3SXiubo Li }
541141685a3SXiubo Li 
542141685a3SXiubo Li static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd)
543141685a3SXiubo Li {
544a3512902SBodo Stroesser 	if (tcmu_cmd->se_cmd)
545a3512902SBodo Stroesser 		tcmu_cmd->se_cmd->priv = NULL;
546141685a3SXiubo Li 	kfree(tcmu_cmd->dbi);
547141685a3SXiubo Li 	kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
548141685a3SXiubo Li }
549141685a3SXiubo Li 
550141685a3SXiubo Li static inline size_t tcmu_cmd_get_data_length(struct tcmu_cmd *tcmu_cmd)
551141685a3SXiubo Li {
552141685a3SXiubo Li 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
553141685a3SXiubo Li 	size_t data_length = round_up(se_cmd->data_length, DATA_BLOCK_SIZE);
554141685a3SXiubo Li 
555141685a3SXiubo Li 	if (se_cmd->se_cmd_flags & SCF_BIDI) {
556141685a3SXiubo Li 		BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents));
557141685a3SXiubo Li 		data_length += round_up(se_cmd->t_bidi_data_sg->length,
558141685a3SXiubo Li 				DATA_BLOCK_SIZE);
559141685a3SXiubo Li 	}
560141685a3SXiubo Li 
561141685a3SXiubo Li 	return data_length;
562141685a3SXiubo Li }
563141685a3SXiubo Li 
564141685a3SXiubo Li static inline uint32_t tcmu_cmd_get_block_cnt(struct tcmu_cmd *tcmu_cmd)
565141685a3SXiubo Li {
566141685a3SXiubo Li 	size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
567141685a3SXiubo Li 
568141685a3SXiubo Li 	return data_length / DATA_BLOCK_SIZE;
569141685a3SXiubo Li }
570141685a3SXiubo Li 
5717c9e7a6fSAndy Grover static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd)
5727c9e7a6fSAndy Grover {
5737c9e7a6fSAndy Grover 	struct se_device *se_dev = se_cmd->se_dev;
5747c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
5757c9e7a6fSAndy Grover 	struct tcmu_cmd *tcmu_cmd;
5767c9e7a6fSAndy Grover 
5770eccce86SDamien Le Moal 	tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_NOIO);
5787c9e7a6fSAndy Grover 	if (!tcmu_cmd)
5797c9e7a6fSAndy Grover 		return NULL;
5807c9e7a6fSAndy Grover 
581a94a2572SXiubo Li 	INIT_LIST_HEAD(&tcmu_cmd->queue_entry);
5827c9e7a6fSAndy Grover 	tcmu_cmd->se_cmd = se_cmd;
5837c9e7a6fSAndy Grover 	tcmu_cmd->tcmu_dev = udev;
5847c9e7a6fSAndy Grover 
585141685a3SXiubo Li 	tcmu_cmd_reset_dbi_cur(tcmu_cmd);
586141685a3SXiubo Li 	tcmu_cmd->dbi_cnt = tcmu_cmd_get_block_cnt(tcmu_cmd);
587141685a3SXiubo Li 	tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t),
5880eccce86SDamien Le Moal 				GFP_NOIO);
589141685a3SXiubo Li 	if (!tcmu_cmd->dbi) {
590141685a3SXiubo Li 		kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
591141685a3SXiubo Li 		return NULL;
592141685a3SXiubo Li 	}
593141685a3SXiubo Li 
5947c9e7a6fSAndy Grover 	return tcmu_cmd;
5957c9e7a6fSAndy Grover }
5967c9e7a6fSAndy Grover 
5977c9e7a6fSAndy Grover static inline void tcmu_flush_dcache_range(void *vaddr, size_t size)
5987c9e7a6fSAndy Grover {
599b75d8063SGeliang Tang 	unsigned long offset = offset_in_page(vaddr);
60026d2b310Stangwenji 	void *start = vaddr - offset;
6017c9e7a6fSAndy Grover 
6027c9e7a6fSAndy Grover 	size = round_up(size+offset, PAGE_SIZE);
6037c9e7a6fSAndy Grover 
6047c9e7a6fSAndy Grover 	while (size) {
6053145550aSBodo Stroesser 		flush_dcache_page(vmalloc_to_page(start));
60626d2b310Stangwenji 		start += PAGE_SIZE;
6077c9e7a6fSAndy Grover 		size -= PAGE_SIZE;
6087c9e7a6fSAndy Grover 	}
6097c9e7a6fSAndy Grover }
6107c9e7a6fSAndy Grover 
6117c9e7a6fSAndy Grover /*
6127c9e7a6fSAndy Grover  * Some ring helper functions. We don't assume size is a power of 2 so
6137c9e7a6fSAndy Grover  * we can't use circ_buf.h.
6147c9e7a6fSAndy Grover  */
6157c9e7a6fSAndy Grover static inline size_t spc_used(size_t head, size_t tail, size_t size)
6167c9e7a6fSAndy Grover {
6177c9e7a6fSAndy Grover 	int diff = head - tail;
6187c9e7a6fSAndy Grover 
6197c9e7a6fSAndy Grover 	if (diff >= 0)
6207c9e7a6fSAndy Grover 		return diff;
6217c9e7a6fSAndy Grover 	else
6227c9e7a6fSAndy Grover 		return size + diff;
6237c9e7a6fSAndy Grover }
6247c9e7a6fSAndy Grover 
6257c9e7a6fSAndy Grover static inline size_t spc_free(size_t head, size_t tail, size_t size)
6267c9e7a6fSAndy Grover {
6277c9e7a6fSAndy Grover 	/* Keep 1 byte unused or we can't tell full from empty */
6287c9e7a6fSAndy Grover 	return (size - spc_used(head, tail, size) - 1);
6297c9e7a6fSAndy Grover }
6307c9e7a6fSAndy Grover 
6317c9e7a6fSAndy Grover static inline size_t head_to_end(size_t head, size_t size)
6327c9e7a6fSAndy Grover {
6337c9e7a6fSAndy Grover 	return size - head;
6347c9e7a6fSAndy Grover }
6357c9e7a6fSAndy Grover 
6363e609135SXiubo Li static inline void new_iov(struct iovec **iov, int *iov_cnt)
637f1dbd087SSheng Yang {
638f1dbd087SSheng Yang 	struct iovec *iovec;
639f1dbd087SSheng Yang 
640f1dbd087SSheng Yang 	if (*iov_cnt != 0)
641f1dbd087SSheng Yang 		(*iov)++;
642f1dbd087SSheng Yang 	(*iov_cnt)++;
643f1dbd087SSheng Yang 
644f1dbd087SSheng Yang 	iovec = *iov;
645f1dbd087SSheng Yang 	memset(iovec, 0, sizeof(struct iovec));
646f1dbd087SSheng Yang }
647f1dbd087SSheng Yang 
6487c9e7a6fSAndy Grover #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
6497c9e7a6fSAndy Grover 
65026418649SSheng Yang /* offset is relative to mb_addr */
651141685a3SXiubo Li static inline size_t get_block_offset_user(struct tcmu_dev *dev,
652141685a3SXiubo Li 		int dbi, int remaining)
65326418649SSheng Yang {
654141685a3SXiubo Li 	return dev->data_off + dbi * DATA_BLOCK_SIZE +
65526418649SSheng Yang 		DATA_BLOCK_SIZE - remaining;
65626418649SSheng Yang }
65726418649SSheng Yang 
658daf78c30SXiubo Li static inline size_t iov_tail(struct iovec *iov)
65926418649SSheng Yang {
66026418649SSheng Yang 	return (size_t)iov->iov_base + iov->iov_len;
66126418649SSheng Yang }
66226418649SSheng Yang 
6631a1fc0b8SMike Christie static void scatter_data_area(struct tcmu_dev *udev,
664141685a3SXiubo Li 	struct tcmu_cmd *tcmu_cmd, struct scatterlist *data_sg,
665141685a3SXiubo Li 	unsigned int data_nents, struct iovec **iov,
666141685a3SXiubo Li 	int *iov_cnt, bool copy_data)
667f97ec7dbSIlias Tsitsimpis {
668141685a3SXiubo Li 	int i, dbi;
66926418649SSheng Yang 	int block_remaining = 0;
670141685a3SXiubo Li 	void *from, *to = NULL;
671141685a3SXiubo Li 	size_t copy_bytes, to_offset, offset;
672f97ec7dbSIlias Tsitsimpis 	struct scatterlist *sg;
673b6df4b79SXiubo Li 	struct page *page;
674f97ec7dbSIlias Tsitsimpis 
675f97ec7dbSIlias Tsitsimpis 	for_each_sg(data_sg, sg, data_nents, i) {
67626418649SSheng Yang 		int sg_remaining = sg->length;
677f97ec7dbSIlias Tsitsimpis 		from = kmap_atomic(sg_page(sg)) + sg->offset;
67826418649SSheng Yang 		while (sg_remaining > 0) {
67926418649SSheng Yang 			if (block_remaining == 0) {
6803c58f737SBodo Stroesser 				if (to) {
6813c58f737SBodo Stroesser 					flush_dcache_page(page);
682b6df4b79SXiubo Li 					kunmap_atomic(to);
6833c58f737SBodo Stroesser 				}
684b6df4b79SXiubo Li 
68526418649SSheng Yang 				block_remaining = DATA_BLOCK_SIZE;
686b6df4b79SXiubo Li 				dbi = tcmu_cmd_get_dbi(tcmu_cmd);
687b6df4b79SXiubo Li 				page = tcmu_get_block_page(udev, dbi);
688b6df4b79SXiubo Li 				to = kmap_atomic(page);
689141685a3SXiubo Li 			}
690141685a3SXiubo Li 
6913e609135SXiubo Li 			/*
6923e609135SXiubo Li 			 * Covert to virtual offset of the ring data area.
6933e609135SXiubo Li 			 */
694141685a3SXiubo Li 			to_offset = get_block_offset_user(udev, dbi,
69526418649SSheng Yang 					block_remaining);
696141685a3SXiubo Li 
6973e609135SXiubo Li 			/*
6983e609135SXiubo Li 			 * The following code will gather and map the blocks
6993e609135SXiubo Li 			 * to the same iovec when the blocks are all next to
7003e609135SXiubo Li 			 * each other.
7013e609135SXiubo Li 			 */
7023e609135SXiubo Li 			copy_bytes = min_t(size_t, sg_remaining,
7033e609135SXiubo Li 					block_remaining);
70426418649SSheng Yang 			if (*iov_cnt != 0 &&
705daf78c30SXiubo Li 			    to_offset == iov_tail(*iov)) {
7063e609135SXiubo Li 				/*
7073e609135SXiubo Li 				 * Will append to the current iovec, because
7083e609135SXiubo Li 				 * the current block page is next to the
7093e609135SXiubo Li 				 * previous one.
7103e609135SXiubo Li 				 */
711f1dbd087SSheng Yang 				(*iov)->iov_len += copy_bytes;
71226418649SSheng Yang 			} else {
7133e609135SXiubo Li 				/*
7143e609135SXiubo Li 				 * Will allocate a new iovec because we are
7153e609135SXiubo Li 				 * first time here or the current block page
7163e609135SXiubo Li 				 * is not next to the previous one.
7173e609135SXiubo Li 				 */
7183e609135SXiubo Li 				new_iov(iov, iov_cnt);
71926418649SSheng Yang 				(*iov)->iov_base = (void __user *)to_offset;
720f97ec7dbSIlias Tsitsimpis 				(*iov)->iov_len = copy_bytes;
72126418649SSheng Yang 			}
7223e609135SXiubo Li 
723f97ec7dbSIlias Tsitsimpis 			if (copy_data) {
724c542942cSXiubo Li 				offset = DATA_BLOCK_SIZE - block_remaining;
725c542942cSXiubo Li 				memcpy(to + offset,
726c542942cSXiubo Li 				       from + sg->length - sg_remaining,
72726418649SSheng Yang 				       copy_bytes);
728f97ec7dbSIlias Tsitsimpis 			}
7293e609135SXiubo Li 
73026418649SSheng Yang 			sg_remaining -= copy_bytes;
73126418649SSheng Yang 			block_remaining -= copy_bytes;
732f97ec7dbSIlias Tsitsimpis 		}
733e2e21bd8SSagi Grimberg 		kunmap_atomic(from - sg->offset);
734f97ec7dbSIlias Tsitsimpis 	}
7353e609135SXiubo Li 
7363c58f737SBodo Stroesser 	if (to) {
7373c58f737SBodo Stroesser 		flush_dcache_page(page);
738b6df4b79SXiubo Li 		kunmap_atomic(to);
7390c28481fSSheng Yang 	}
7403c58f737SBodo Stroesser }
7410c28481fSSheng Yang 
742a5d68ba8SXiubo Li static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
7436c3796d1Sbstroesser@ts.fujitsu.com 			     bool bidi, uint32_t read_len)
744f97ec7dbSIlias Tsitsimpis {
745a5d68ba8SXiubo Li 	struct se_cmd *se_cmd = cmd->se_cmd;
746141685a3SXiubo Li 	int i, dbi;
74726418649SSheng Yang 	int block_remaining = 0;
748b6df4b79SXiubo Li 	void *from = NULL, *to;
749141685a3SXiubo Li 	size_t copy_bytes, offset;
750a5d68ba8SXiubo Li 	struct scatterlist *sg, *data_sg;
751b6df4b79SXiubo Li 	struct page *page;
752a5d68ba8SXiubo Li 	unsigned int data_nents;
753141685a3SXiubo Li 	uint32_t count = 0;
754a5d68ba8SXiubo Li 
755a5d68ba8SXiubo Li 	if (!bidi) {
756a5d68ba8SXiubo Li 		data_sg = se_cmd->t_data_sg;
757a5d68ba8SXiubo Li 		data_nents = se_cmd->t_data_nents;
758a5d68ba8SXiubo Li 	} else {
759a5d68ba8SXiubo Li 
760a5d68ba8SXiubo Li 		/*
761a5d68ba8SXiubo Li 		 * For bidi case, the first count blocks are for Data-Out
762a5d68ba8SXiubo Li 		 * buffer blocks, and before gathering the Data-In buffer
763a5d68ba8SXiubo Li 		 * the Data-Out buffer blocks should be discarded.
764a5d68ba8SXiubo Li 		 */
765a5d68ba8SXiubo Li 		count = DIV_ROUND_UP(se_cmd->data_length, DATA_BLOCK_SIZE);
766a5d68ba8SXiubo Li 
767a5d68ba8SXiubo Li 		data_sg = se_cmd->t_bidi_data_sg;
768a5d68ba8SXiubo Li 		data_nents = se_cmd->t_bidi_data_nents;
769a5d68ba8SXiubo Li 	}
770f97ec7dbSIlias Tsitsimpis 
771141685a3SXiubo Li 	tcmu_cmd_set_dbi_cur(cmd, count);
772141685a3SXiubo Li 
773f97ec7dbSIlias Tsitsimpis 	for_each_sg(data_sg, sg, data_nents, i) {
77426418649SSheng Yang 		int sg_remaining = sg->length;
775f97ec7dbSIlias Tsitsimpis 		to = kmap_atomic(sg_page(sg)) + sg->offset;
7766c3796d1Sbstroesser@ts.fujitsu.com 		while (sg_remaining > 0 && read_len > 0) {
77726418649SSheng Yang 			if (block_remaining == 0) {
778b6df4b79SXiubo Li 				if (from)
779b6df4b79SXiubo Li 					kunmap_atomic(from);
780b6df4b79SXiubo Li 
78126418649SSheng Yang 				block_remaining = DATA_BLOCK_SIZE;
782141685a3SXiubo Li 				dbi = tcmu_cmd_get_dbi(cmd);
783b6df4b79SXiubo Li 				page = tcmu_get_block_page(udev, dbi);
784b6df4b79SXiubo Li 				from = kmap_atomic(page);
7853c58f737SBodo Stroesser 				flush_dcache_page(page);
78626418649SSheng Yang 			}
78726418649SSheng Yang 			copy_bytes = min_t(size_t, sg_remaining,
78826418649SSheng Yang 					block_remaining);
7896c3796d1Sbstroesser@ts.fujitsu.com 			if (read_len < copy_bytes)
7906c3796d1Sbstroesser@ts.fujitsu.com 				copy_bytes = read_len;
791141685a3SXiubo Li 			offset = DATA_BLOCK_SIZE - block_remaining;
792c542942cSXiubo Li 			memcpy(to + sg->length - sg_remaining, from + offset,
79326418649SSheng Yang 					copy_bytes);
794f97ec7dbSIlias Tsitsimpis 
79526418649SSheng Yang 			sg_remaining -= copy_bytes;
79626418649SSheng Yang 			block_remaining -= copy_bytes;
7976c3796d1Sbstroesser@ts.fujitsu.com 			read_len -= copy_bytes;
798f97ec7dbSIlias Tsitsimpis 		}
799e2e21bd8SSagi Grimberg 		kunmap_atomic(to - sg->offset);
8006c3796d1Sbstroesser@ts.fujitsu.com 		if (read_len == 0)
8016c3796d1Sbstroesser@ts.fujitsu.com 			break;
802f97ec7dbSIlias Tsitsimpis 	}
803b6df4b79SXiubo Li 	if (from)
804b6df4b79SXiubo Li 		kunmap_atomic(from);
805f97ec7dbSIlias Tsitsimpis }
806f97ec7dbSIlias Tsitsimpis 
807b6df4b79SXiubo Li static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh)
80826418649SSheng Yang {
8093c0f26ffSMike Christie 	return thresh - bitmap_weight(bitmap, thresh);
81026418649SSheng Yang }
81126418649SSheng Yang 
8127c9e7a6fSAndy Grover /*
813f97ec7dbSIlias Tsitsimpis  * We can't queue a command until we have space available on the cmd ring *and*
8143d9b9555SAndy Grover  * space available on the data area.
8157c9e7a6fSAndy Grover  *
8167c9e7a6fSAndy Grover  * Called with ring lock held.
8177c9e7a6fSAndy Grover  */
818b6df4b79SXiubo Li static bool is_ring_space_avail(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
819b6df4b79SXiubo Li 		size_t cmd_size, size_t data_needed)
8207c9e7a6fSAndy Grover {
8217c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb = udev->mb_addr;
822b6df4b79SXiubo Li 	uint32_t blocks_needed = (data_needed + DATA_BLOCK_SIZE - 1)
823b6df4b79SXiubo Li 				/ DATA_BLOCK_SIZE;
8240241fd39SNicholas Bellinger 	size_t space, cmd_needed;
8257c9e7a6fSAndy Grover 	u32 cmd_head;
8267c9e7a6fSAndy Grover 
8277c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
8287c9e7a6fSAndy Grover 
8297c9e7a6fSAndy Grover 	cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
8307c9e7a6fSAndy Grover 
831f56574a2SAndy Grover 	/*
832f56574a2SAndy Grover 	 * If cmd end-of-ring space is too small then we need space for a NOP plus
833f56574a2SAndy Grover 	 * original cmd - cmds are internally contiguous.
834f56574a2SAndy Grover 	 */
835f56574a2SAndy Grover 	if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size)
836f56574a2SAndy Grover 		cmd_needed = cmd_size;
837f56574a2SAndy Grover 	else
838f56574a2SAndy Grover 		cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size);
839f56574a2SAndy Grover 
8407c9e7a6fSAndy Grover 	space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size);
8417c9e7a6fSAndy Grover 	if (space < cmd_needed) {
8427c9e7a6fSAndy Grover 		pr_debug("no cmd space: %u %u %u\n", cmd_head,
8437c9e7a6fSAndy Grover 		       udev->cmdr_last_cleaned, udev->cmdr_size);
8447c9e7a6fSAndy Grover 		return false;
8457c9e7a6fSAndy Grover 	}
8467c9e7a6fSAndy Grover 
847b6df4b79SXiubo Li 	/* try to check and get the data blocks as needed */
848b6df4b79SXiubo Li 	space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh);
8493c0f26ffSMike Christie 	if ((space * DATA_BLOCK_SIZE) < data_needed) {
85080eb8761SMike Christie 		unsigned long blocks_left =
85180eb8761SMike Christie 				(udev->max_blocks - udev->dbi_thresh) + space;
852b6df4b79SXiubo Li 
853b6df4b79SXiubo Li 		if (blocks_left < blocks_needed) {
854b6df4b79SXiubo Li 			pr_debug("no data space: only %lu available, but ask for %zu\n",
855b6df4b79SXiubo Li 					blocks_left * DATA_BLOCK_SIZE,
856b6df4b79SXiubo Li 					data_needed);
8577c9e7a6fSAndy Grover 			return false;
8587c9e7a6fSAndy Grover 		}
8597c9e7a6fSAndy Grover 
860f890f579SMike Christie 		udev->dbi_thresh += blocks_needed;
86180eb8761SMike Christie 		if (udev->dbi_thresh > udev->max_blocks)
86280eb8761SMike Christie 			udev->dbi_thresh = udev->max_blocks;
863b6df4b79SXiubo Li 	}
864b6df4b79SXiubo Li 
865daf78c30SXiubo Li 	return tcmu_get_empty_blocks(udev, cmd);
8667c9e7a6fSAndy Grover }
8677c9e7a6fSAndy Grover 
868fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt)
869fe25cc34SXiubo Li {
870fe25cc34SXiubo Li 	return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]),
871fe25cc34SXiubo Li 			sizeof(struct tcmu_cmd_entry));
872fe25cc34SXiubo Li }
873fe25cc34SXiubo Li 
874fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd,
875fe25cc34SXiubo Li 					   size_t base_command_size)
876fe25cc34SXiubo Li {
877fe25cc34SXiubo Li 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
878fe25cc34SXiubo Li 	size_t command_size;
879fe25cc34SXiubo Li 
880fe25cc34SXiubo Li 	command_size = base_command_size +
881fe25cc34SXiubo Li 		round_up(scsi_command_size(se_cmd->t_task_cdb),
882fe25cc34SXiubo Li 				TCMU_OP_ALIGN_SIZE);
883fe25cc34SXiubo Li 
884fe25cc34SXiubo Li 	WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1));
885fe25cc34SXiubo Li 
886fe25cc34SXiubo Li 	return command_size;
887fe25cc34SXiubo Li }
888fe25cc34SXiubo Li 
88961fb2482SBodo Stroesser static void tcmu_setup_cmd_timer(struct tcmu_cmd *tcmu_cmd, unsigned int tmo,
8909103575aSMike Christie 				 struct timer_list *timer)
8910d44374cSMike Christie {
8929103575aSMike Christie 	if (!tmo)
89361fb2482SBodo Stroesser 		return;
8949103575aSMike Christie 
8950d44374cSMike Christie 	tcmu_cmd->deadline = round_jiffies_up(jiffies + msecs_to_jiffies(tmo));
896a94a2572SXiubo Li 	if (!timer_pending(timer))
8979103575aSMike Christie 		mod_timer(timer, tcmu_cmd->deadline);
898a94a2572SXiubo Li 
89961fb2482SBodo Stroesser 	pr_debug("Timeout set up for cmd %p, dev = %s, tmo = %lu\n", tcmu_cmd,
90061fb2482SBodo Stroesser 		 tcmu_cmd->tcmu_dev->name, tmo / MSEC_PER_SEC);
9010d44374cSMike Christie }
9020d44374cSMike Christie 
903a94a2572SXiubo Li static int add_to_qfull_queue(struct tcmu_cmd *tcmu_cmd)
904af1dd7ffSMike Christie {
905af1dd7ffSMike Christie 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
9069103575aSMike Christie 	unsigned int tmo;
907af1dd7ffSMike Christie 
9089103575aSMike Christie 	/*
9099103575aSMike Christie 	 * For backwards compat if qfull_time_out is not set use
9109103575aSMike Christie 	 * cmd_time_out and if that's not set use the default time out.
9119103575aSMike Christie 	 */
9129103575aSMike Christie 	if (!udev->qfull_time_out)
9139103575aSMike Christie 		return -ETIMEDOUT;
9149103575aSMike Christie 	else if (udev->qfull_time_out > 0)
9159103575aSMike Christie 		tmo = udev->qfull_time_out;
9169103575aSMike Christie 	else if (udev->cmd_time_out)
9179103575aSMike Christie 		tmo = udev->cmd_time_out;
9189103575aSMike Christie 	else
9199103575aSMike Christie 		tmo = TCMU_TIME_OUT;
9209103575aSMike Christie 
92161fb2482SBodo Stroesser 	tcmu_setup_cmd_timer(tcmu_cmd, tmo, &udev->qfull_timer);
922af1dd7ffSMike Christie 
923a94a2572SXiubo Li 	list_add_tail(&tcmu_cmd->queue_entry, &udev->qfull_queue);
92461fb2482SBodo Stroesser 	pr_debug("adding cmd %p on dev %s to ring space wait queue\n",
92561fb2482SBodo Stroesser 		 tcmu_cmd, udev->name);
926af1dd7ffSMike Christie 	return 0;
927af1dd7ffSMike Christie }
928af1dd7ffSMike Christie 
9293d3f9d56SBodo Stroesser static uint32_t ring_insert_padding(struct tcmu_dev *udev, size_t cmd_size)
9303d3f9d56SBodo Stroesser {
9313d3f9d56SBodo Stroesser 	struct tcmu_cmd_entry_hdr *hdr;
9323d3f9d56SBodo Stroesser 	struct tcmu_mailbox *mb = udev->mb_addr;
9333d3f9d56SBodo Stroesser 	uint32_t cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
9343d3f9d56SBodo Stroesser 
9353d3f9d56SBodo Stroesser 	/* Insert a PAD if end-of-ring space is too small */
9363d3f9d56SBodo Stroesser 	if (head_to_end(cmd_head, udev->cmdr_size) < cmd_size) {
9373d3f9d56SBodo Stroesser 		size_t pad_size = head_to_end(cmd_head, udev->cmdr_size);
9383d3f9d56SBodo Stroesser 
9393d3f9d56SBodo Stroesser 		hdr = (void *) mb + CMDR_OFF + cmd_head;
9403d3f9d56SBodo Stroesser 		tcmu_hdr_set_op(&hdr->len_op, TCMU_OP_PAD);
9413d3f9d56SBodo Stroesser 		tcmu_hdr_set_len(&hdr->len_op, pad_size);
9423d3f9d56SBodo Stroesser 		hdr->cmd_id = 0; /* not used for PAD */
9433d3f9d56SBodo Stroesser 		hdr->kflags = 0;
9443d3f9d56SBodo Stroesser 		hdr->uflags = 0;
9453d3f9d56SBodo Stroesser 		tcmu_flush_dcache_range(hdr, sizeof(*hdr));
9463d3f9d56SBodo Stroesser 
9473d3f9d56SBodo Stroesser 		UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size);
9483d3f9d56SBodo Stroesser 		tcmu_flush_dcache_range(mb, sizeof(*mb));
9493d3f9d56SBodo Stroesser 
9503d3f9d56SBodo Stroesser 		cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
9513d3f9d56SBodo Stroesser 		WARN_ON(cmd_head != 0);
9523d3f9d56SBodo Stroesser 	}
9533d3f9d56SBodo Stroesser 
9543d3f9d56SBodo Stroesser 	return cmd_head;
9553d3f9d56SBodo Stroesser }
9563d3f9d56SBodo Stroesser 
9576fd0ce79SMike Christie /**
9586fd0ce79SMike Christie  * queue_cmd_ring - queue cmd to ring or internally
9596fd0ce79SMike Christie  * @tcmu_cmd: cmd to queue
9606fd0ce79SMike Christie  * @scsi_err: TCM error code if failure (-1) returned.
9616fd0ce79SMike Christie  *
9626fd0ce79SMike Christie  * Returns:
9636fd0ce79SMike Christie  * -1 we cannot queue internally or to the ring.
9646fd0ce79SMike Christie  *  0 success
965af1dd7ffSMike Christie  *  1 internally queued to wait for ring memory to free.
9666fd0ce79SMike Christie  */
967e7f41104SBart Van Assche static int queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, sense_reason_t *scsi_err)
9687c9e7a6fSAndy Grover {
9697c9e7a6fSAndy Grover 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
9707c9e7a6fSAndy Grover 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
9717c9e7a6fSAndy Grover 	size_t base_command_size, command_size;
9723d3f9d56SBodo Stroesser 	struct tcmu_mailbox *mb = udev->mb_addr;
9737c9e7a6fSAndy Grover 	struct tcmu_cmd_entry *entry;
9747c9e7a6fSAndy Grover 	struct iovec *iov;
97561fb2482SBodo Stroesser 	int iov_cnt, cmd_id;
9767c9e7a6fSAndy Grover 	uint32_t cmd_head;
9777c9e7a6fSAndy Grover 	uint64_t cdb_off;
978f97ec7dbSIlias Tsitsimpis 	bool copy_to_data_area;
979ab22d260SXiubo Li 	size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
9807c9e7a6fSAndy Grover 
9816fd0ce79SMike Christie 	*scsi_err = TCM_NO_SENSE;
9826fd0ce79SMike Christie 
983892782caSMike Christie 	if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) {
984892782caSMike Christie 		*scsi_err = TCM_LUN_BUSY;
985892782caSMike Christie 		return -1;
986892782caSMike Christie 	}
987892782caSMike Christie 
9886fd0ce79SMike Christie 	if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
9896fd0ce79SMike Christie 		*scsi_err = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
9906fd0ce79SMike Christie 		return -1;
9916fd0ce79SMike Christie 	}
9927c9e7a6fSAndy Grover 
9937c9e7a6fSAndy Grover 	/*
9947c9e7a6fSAndy Grover 	 * Must be a certain minimum size for response sense info, but
9957c9e7a6fSAndy Grover 	 * also may be larger if the iov array is large.
9967c9e7a6fSAndy Grover 	 *
997fe25cc34SXiubo Li 	 * We prepare as many iovs as possbile for potential uses here,
998fe25cc34SXiubo Li 	 * because it's expensive to tell how many regions are freed in
999fe25cc34SXiubo Li 	 * the bitmap & global data pool, as the size calculated here
1000fe25cc34SXiubo Li 	 * will only be used to do the checks.
1001fe25cc34SXiubo Li 	 *
1002fe25cc34SXiubo Li 	 * The size will be recalculated later as actually needed to save
1003fe25cc34SXiubo Li 	 * cmd area memories.
10047c9e7a6fSAndy Grover 	 */
1005fe25cc34SXiubo Li 	base_command_size = tcmu_cmd_get_base_cmd_size(tcmu_cmd->dbi_cnt);
1006fe25cc34SXiubo Li 	command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
10077c9e7a6fSAndy Grover 
1008a94a2572SXiubo Li 	if (!list_empty(&udev->qfull_queue))
1009af1dd7ffSMike Christie 		goto queue;
10107c9e7a6fSAndy Grover 
1011554617b2SAndy Grover 	if ((command_size > (udev->cmdr_size / 2)) ||
1012554617b2SAndy Grover 	    data_length > udev->data_size) {
1013554617b2SAndy Grover 		pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu "
10143d9b9555SAndy Grover 			"cmd ring/data area\n", command_size, data_length,
10157c9e7a6fSAndy Grover 			udev->cmdr_size, udev->data_size);
10166fd0ce79SMike Christie 		*scsi_err = TCM_INVALID_CDB_FIELD;
10176fd0ce79SMike Christie 		return -1;
1018554617b2SAndy Grover 	}
10197c9e7a6fSAndy Grover 
1020af1dd7ffSMike Christie 	if (!is_ring_space_avail(udev, tcmu_cmd, command_size, data_length)) {
1021810b8153SMike Christie 		/*
1022810b8153SMike Christie 		 * Don't leave commands partially setup because the unmap
1023810b8153SMike Christie 		 * thread might need the blocks to make forward progress.
1024810b8153SMike Christie 		 */
1025810b8153SMike Christie 		tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur);
1026810b8153SMike Christie 		tcmu_cmd_reset_dbi_cur(tcmu_cmd);
1027af1dd7ffSMike Christie 		goto queue;
10287c9e7a6fSAndy Grover 	}
10297c9e7a6fSAndy Grover 
10303d3f9d56SBodo Stroesser 	cmd_head = ring_insert_padding(udev, command_size);
10317c9e7a6fSAndy Grover 
10327c9e7a6fSAndy Grover 	entry = (void *) mb + CMDR_OFF + cmd_head;
1033b3743c71SXiubo Li 	memset(entry, 0, command_size);
10340ad46af8SAndy Grover 	tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD);
10357c9e7a6fSAndy Grover 
10363d9b9555SAndy Grover 	/* Handle allocating space from the data area */
1037b6df4b79SXiubo Li 	tcmu_cmd_reset_dbi_cur(tcmu_cmd);
10387c9e7a6fSAndy Grover 	iov = &entry->req.iov[0];
1039f97ec7dbSIlias Tsitsimpis 	iov_cnt = 0;
1040e4648b01SIlias Tsitsimpis 	copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE
1041e4648b01SIlias Tsitsimpis 		|| se_cmd->se_cmd_flags & SCF_BIDI);
10421a1fc0b8SMike Christie 	scatter_data_area(udev, tcmu_cmd, se_cmd->t_data_sg,
1043b6df4b79SXiubo Li 			  se_cmd->t_data_nents, &iov, &iov_cnt,
1044b6df4b79SXiubo Li 			  copy_to_data_area);
10457c9e7a6fSAndy Grover 	entry->req.iov_cnt = iov_cnt;
10467c9e7a6fSAndy Grover 
1047e4648b01SIlias Tsitsimpis 	/* Handle BIDI commands */
1048e4648b01SIlias Tsitsimpis 	iov_cnt = 0;
1049b3743c71SXiubo Li 	if (se_cmd->se_cmd_flags & SCF_BIDI) {
1050ab22d260SXiubo Li 		iov++;
10511a1fc0b8SMike Christie 		scatter_data_area(udev, tcmu_cmd, se_cmd->t_bidi_data_sg,
10521a1fc0b8SMike Christie 				  se_cmd->t_bidi_data_nents, &iov, &iov_cnt,
10531a1fc0b8SMike Christie 				  false);
1054ab22d260SXiubo Li 	}
1055b3743c71SXiubo Li 	entry->req.iov_bidi_cnt = iov_cnt;
105626418649SSheng Yang 
105761fb2482SBodo Stroesser 	cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 1, USHRT_MAX, GFP_NOWAIT);
105861fb2482SBodo Stroesser 	if (cmd_id < 0) {
105961fb2482SBodo Stroesser 		pr_err("tcmu: Could not allocate cmd id.\n");
10606fd0ce79SMike Christie 
106161fb2482SBodo Stroesser 		tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt);
10626fd0ce79SMike Christie 		*scsi_err = TCM_OUT_OF_RESOURCES;
10636fd0ce79SMike Christie 		return -1;
10640d44374cSMike Christie 	}
106561fb2482SBodo Stroesser 	tcmu_cmd->cmd_id = cmd_id;
106661fb2482SBodo Stroesser 
106761fb2482SBodo Stroesser 	pr_debug("allocated cmd id %u for cmd %p dev %s\n", tcmu_cmd->cmd_id,
106861fb2482SBodo Stroesser 		 tcmu_cmd, udev->name);
106961fb2482SBodo Stroesser 
107061fb2482SBodo Stroesser 	tcmu_setup_cmd_timer(tcmu_cmd, udev->cmd_time_out, &udev->cmd_timer);
107161fb2482SBodo Stroesser 
10720d44374cSMike Christie 	entry->hdr.cmd_id = tcmu_cmd->cmd_id;
10730d44374cSMike Christie 
1074fe25cc34SXiubo Li 	/*
1075fe25cc34SXiubo Li 	 * Recalaulate the command's base size and size according
1076fe25cc34SXiubo Li 	 * to the actual needs
1077fe25cc34SXiubo Li 	 */
1078fe25cc34SXiubo Li 	base_command_size = tcmu_cmd_get_base_cmd_size(entry->req.iov_cnt +
1079fe25cc34SXiubo Li 						       entry->req.iov_bidi_cnt);
1080fe25cc34SXiubo Li 	command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
1081fe25cc34SXiubo Li 
1082fe25cc34SXiubo Li 	tcmu_hdr_set_len(&entry->hdr.len_op, command_size);
1083fe25cc34SXiubo Li 
10847c9e7a6fSAndy Grover 	/* All offsets relative to mb_addr, not start of entry! */
10857c9e7a6fSAndy Grover 	cdb_off = CMDR_OFF + cmd_head + base_command_size;
10867c9e7a6fSAndy Grover 	memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb));
10877c9e7a6fSAndy Grover 	entry->req.cdb_off = cdb_off;
10888c4e0f21SBodo Stroesser 	tcmu_flush_dcache_range(entry, command_size);
10897c9e7a6fSAndy Grover 
10907c9e7a6fSAndy Grover 	UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size);
10917c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
10927c9e7a6fSAndy Grover 
1093a94a2572SXiubo Li 	list_add_tail(&tcmu_cmd->queue_entry, &udev->inflight_queue);
1094a94a2572SXiubo Li 
10957c9e7a6fSAndy Grover 	/* TODO: only if FLUSH and FUA? */
10967c9e7a6fSAndy Grover 	uio_event_notify(&udev->uio_info);
10977c9e7a6fSAndy Grover 
10986fd0ce79SMike Christie 	return 0;
1099af1dd7ffSMike Christie 
1100af1dd7ffSMike Christie queue:
1101a94a2572SXiubo Li 	if (add_to_qfull_queue(tcmu_cmd)) {
1102af1dd7ffSMike Christie 		*scsi_err = TCM_OUT_OF_RESOURCES;
1103af1dd7ffSMike Christie 		return -1;
1104af1dd7ffSMike Christie 	}
1105af1dd7ffSMike Christie 
1106af1dd7ffSMike Christie 	return 1;
11077c9e7a6fSAndy Grover }
11087c9e7a6fSAndy Grover 
110902eb924fSAndy Grover static sense_reason_t
111002eb924fSAndy Grover tcmu_queue_cmd(struct se_cmd *se_cmd)
11117c9e7a6fSAndy Grover {
1112af1dd7ffSMike Christie 	struct se_device *se_dev = se_cmd->se_dev;
1113af1dd7ffSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
11147c9e7a6fSAndy Grover 	struct tcmu_cmd *tcmu_cmd;
1115c9684927SBodo Stroesser 	sense_reason_t scsi_ret = TCM_CHECK_CONDITION_ABORT_CMD;
1116c9684927SBodo Stroesser 	int ret = -1;
11177c9e7a6fSAndy Grover 
11187c9e7a6fSAndy Grover 	tcmu_cmd = tcmu_alloc_cmd(se_cmd);
11197c9e7a6fSAndy Grover 	if (!tcmu_cmd)
112002eb924fSAndy Grover 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
11217c9e7a6fSAndy Grover 
1122af1dd7ffSMike Christie 	mutex_lock(&udev->cmdr_lock);
1123a3512902SBodo Stroesser 	se_cmd->priv = tcmu_cmd;
1124c9684927SBodo Stroesser 	if (!(se_cmd->transport_state & CMD_T_ABORTED))
1125af1dd7ffSMike Christie 		ret = queue_cmd_ring(tcmu_cmd, &scsi_ret);
1126af1dd7ffSMike Christie 	if (ret < 0)
1127141685a3SXiubo Li 		tcmu_free_cmd(tcmu_cmd);
1128a3512902SBodo Stroesser 	mutex_unlock(&udev->cmdr_lock);
11296fd0ce79SMike Christie 	return scsi_ret;
11307c9e7a6fSAndy Grover }
11317c9e7a6fSAndy Grover 
11327c9e7a6fSAndy Grover static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry)
11337c9e7a6fSAndy Grover {
11347c9e7a6fSAndy Grover 	struct se_cmd *se_cmd = cmd->se_cmd;
11357c9e7a6fSAndy Grover 	struct tcmu_dev *udev = cmd->tcmu_dev;
11366c3796d1Sbstroesser@ts.fujitsu.com 	bool read_len_valid = false;
1137a86a7586SDmitry Fomichev 	uint32_t read_len;
11387c9e7a6fSAndy Grover 
1139b25c7863SSheng Yang 	/*
1140b25c7863SSheng Yang 	 * cmd has been completed already from timeout, just reclaim
11413d9b9555SAndy Grover 	 * data area space and free cmd
1142b25c7863SSheng Yang 	 */
1143a86a7586SDmitry Fomichev 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
1144a86a7586SDmitry Fomichev 		WARN_ON_ONCE(se_cmd);
1145141685a3SXiubo Li 		goto out;
1146a86a7586SDmitry Fomichev 	}
1147b25c7863SSheng Yang 
1148a94a2572SXiubo Li 	list_del_init(&cmd->queue_entry);
1149a94a2572SXiubo Li 
1150141685a3SXiubo Li 	tcmu_cmd_reset_dbi_cur(cmd);
11517c9e7a6fSAndy Grover 
11520ad46af8SAndy Grover 	if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) {
11530ad46af8SAndy Grover 		pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
11540ad46af8SAndy Grover 			cmd->se_cmd);
1155ed97d0cdSAndy Grover 		entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION;
11566c3796d1Sbstroesser@ts.fujitsu.com 		goto done;
11576c3796d1Sbstroesser@ts.fujitsu.com 	}
11586c3796d1Sbstroesser@ts.fujitsu.com 
1159a86a7586SDmitry Fomichev 	read_len = se_cmd->data_length;
11606c3796d1Sbstroesser@ts.fujitsu.com 	if (se_cmd->data_direction == DMA_FROM_DEVICE &&
11616c3796d1Sbstroesser@ts.fujitsu.com 	    (entry->hdr.uflags & TCMU_UFLAG_READ_LEN) && entry->rsp.read_len) {
11626c3796d1Sbstroesser@ts.fujitsu.com 		read_len_valid = true;
11636c3796d1Sbstroesser@ts.fujitsu.com 		if (entry->rsp.read_len < read_len)
11646c3796d1Sbstroesser@ts.fujitsu.com 			read_len = entry->rsp.read_len;
11656c3796d1Sbstroesser@ts.fujitsu.com 	}
11666c3796d1Sbstroesser@ts.fujitsu.com 
11676c3796d1Sbstroesser@ts.fujitsu.com 	if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) {
1168406f74c2SMike Christie 		transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer);
11696c3796d1Sbstroesser@ts.fujitsu.com 		if (!read_len_valid )
11706c3796d1Sbstroesser@ts.fujitsu.com 			goto done;
11716c3796d1Sbstroesser@ts.fujitsu.com 		else
11726c3796d1Sbstroesser@ts.fujitsu.com 			se_cmd->se_cmd_flags |= SCF_TREAT_READ_AS_NORMAL;
11736c3796d1Sbstroesser@ts.fujitsu.com 	}
11746c3796d1Sbstroesser@ts.fujitsu.com 	if (se_cmd->se_cmd_flags & SCF_BIDI) {
117526418649SSheng Yang 		/* Get Data-In buffer before clean up */
11766c3796d1Sbstroesser@ts.fujitsu.com 		gather_data_area(udev, cmd, true, read_len);
1177e4648b01SIlias Tsitsimpis 	} else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
11786c3796d1Sbstroesser@ts.fujitsu.com 		gather_data_area(udev, cmd, false, read_len);
11797c9e7a6fSAndy Grover 	} else if (se_cmd->data_direction == DMA_TO_DEVICE) {
1180141685a3SXiubo Li 		/* TODO: */
11812bc396a2SIlias Tsitsimpis 	} else if (se_cmd->data_direction != DMA_NONE) {
11822bc396a2SIlias Tsitsimpis 		pr_warn("TCMU: data direction was %d!\n",
11832bc396a2SIlias Tsitsimpis 			se_cmd->data_direction);
11847c9e7a6fSAndy Grover 	}
11857c9e7a6fSAndy Grover 
11866c3796d1Sbstroesser@ts.fujitsu.com done:
11876c3796d1Sbstroesser@ts.fujitsu.com 	if (read_len_valid) {
11886c3796d1Sbstroesser@ts.fujitsu.com 		pr_debug("read_len = %d\n", read_len);
11896c3796d1Sbstroesser@ts.fujitsu.com 		target_complete_cmd_with_length(cmd->se_cmd,
11906c3796d1Sbstroesser@ts.fujitsu.com 					entry->rsp.scsi_status, read_len);
11916c3796d1Sbstroesser@ts.fujitsu.com 	} else
11927c9e7a6fSAndy Grover 		target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
11937c9e7a6fSAndy Grover 
1194141685a3SXiubo Li out:
1195b6df4b79SXiubo Li 	tcmu_cmd_free_data(cmd, cmd->dbi_cnt);
1196141685a3SXiubo Li 	tcmu_free_cmd(cmd);
11977c9e7a6fSAndy Grover }
11987c9e7a6fSAndy Grover 
1199a94a2572SXiubo Li static void tcmu_set_next_deadline(struct list_head *queue,
1200a94a2572SXiubo Li 				   struct timer_list *timer)
1201a94a2572SXiubo Li {
1202a94a2572SXiubo Li 	struct tcmu_cmd *tcmu_cmd, *tmp_cmd;
1203a94a2572SXiubo Li 	unsigned long deadline = 0;
1204a94a2572SXiubo Li 
1205a94a2572SXiubo Li 	list_for_each_entry_safe(tcmu_cmd, tmp_cmd, queue, queue_entry) {
1206a94a2572SXiubo Li 		if (!time_after(jiffies, tcmu_cmd->deadline)) {
1207a94a2572SXiubo Li 			deadline = tcmu_cmd->deadline;
1208a94a2572SXiubo Li 			break;
1209a94a2572SXiubo Li 		}
1210a94a2572SXiubo Li 	}
1211a94a2572SXiubo Li 
1212a94a2572SXiubo Li 	if (deadline)
1213a94a2572SXiubo Li 		mod_timer(timer, deadline);
1214a94a2572SXiubo Li 	else
1215a94a2572SXiubo Li 		del_timer(timer);
1216a94a2572SXiubo Li }
1217a94a2572SXiubo Li 
12187c9e7a6fSAndy Grover static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
12197c9e7a6fSAndy Grover {
12207c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb;
1221a94a2572SXiubo Li 	struct tcmu_cmd *cmd;
12227c9e7a6fSAndy Grover 	int handled = 0;
12237c9e7a6fSAndy Grover 
12247c9e7a6fSAndy Grover 	if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
12257c9e7a6fSAndy Grover 		pr_err("ring broken, not handling completions\n");
12267c9e7a6fSAndy Grover 		return 0;
12277c9e7a6fSAndy Grover 	}
12287c9e7a6fSAndy Grover 
12297c9e7a6fSAndy Grover 	mb = udev->mb_addr;
12307c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
12317c9e7a6fSAndy Grover 
12326aa7de05SMark Rutland 	while (udev->cmdr_last_cleaned != READ_ONCE(mb->cmd_tail)) {
12337c9e7a6fSAndy Grover 
12347c9e7a6fSAndy Grover 		struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned;
12357c9e7a6fSAndy Grover 
12365a0c256dSBodo Stroesser 		/*
12375a0c256dSBodo Stroesser 		 * Flush max. up to end of cmd ring since current entry might
12385a0c256dSBodo Stroesser 		 * be a padding that is shorter than sizeof(*entry)
12395a0c256dSBodo Stroesser 		 */
12405a0c256dSBodo Stroesser 		size_t ring_left = head_to_end(udev->cmdr_last_cleaned,
12415a0c256dSBodo Stroesser 					       udev->cmdr_size);
12425a0c256dSBodo Stroesser 		tcmu_flush_dcache_range(entry, ring_left < sizeof(*entry) ?
12435a0c256dSBodo Stroesser 					ring_left : sizeof(*entry));
12447c9e7a6fSAndy Grover 
12450ad46af8SAndy Grover 		if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) {
12460ad46af8SAndy Grover 			UPDATE_HEAD(udev->cmdr_last_cleaned,
12470ad46af8SAndy Grover 				    tcmu_hdr_get_len(entry->hdr.len_op),
12480ad46af8SAndy Grover 				    udev->cmdr_size);
12497c9e7a6fSAndy Grover 			continue;
12507c9e7a6fSAndy Grover 		}
12510ad46af8SAndy Grover 		WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
12527c9e7a6fSAndy Grover 
1253d3e709e6SMatthew Wilcox 		cmd = idr_remove(&udev->commands, entry->hdr.cmd_id);
12547c9e7a6fSAndy Grover 		if (!cmd) {
125588cf1073SMike Christie 			pr_err("cmd_id %u not found, ring is broken\n",
125688cf1073SMike Christie 			       entry->hdr.cmd_id);
12577c9e7a6fSAndy Grover 			set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
12587c9e7a6fSAndy Grover 			break;
12597c9e7a6fSAndy Grover 		}
12607c9e7a6fSAndy Grover 
12617c9e7a6fSAndy Grover 		tcmu_handle_completion(cmd, entry);
12627c9e7a6fSAndy Grover 
12630ad46af8SAndy Grover 		UPDATE_HEAD(udev->cmdr_last_cleaned,
12640ad46af8SAndy Grover 			    tcmu_hdr_get_len(entry->hdr.len_op),
12650ad46af8SAndy Grover 			    udev->cmdr_size);
12667c9e7a6fSAndy Grover 
12677c9e7a6fSAndy Grover 		handled++;
12687c9e7a6fSAndy Grover 	}
12697c9e7a6fSAndy Grover 
12709103575aSMike Christie 	if (mb->cmd_tail == mb->cmd_head) {
12719103575aSMike Christie 		/* no more pending commands */
12729103575aSMike Christie 		del_timer(&udev->cmd_timer);
12739103575aSMike Christie 
1274a94a2572SXiubo Li 		if (list_empty(&udev->qfull_queue)) {
1275af1dd7ffSMike Christie 			/*
12769103575aSMike Christie 			 * no more pending or waiting commands so try to
12779103575aSMike Christie 			 * reclaim blocks if needed.
1278af1dd7ffSMike Christie 			 */
12799103575aSMike Christie 			if (atomic_read(&global_db_count) >
128080eb8761SMike Christie 			    tcmu_global_max_blocks)
1281af1dd7ffSMike Christie 				schedule_delayed_work(&tcmu_unmap_work, 0);
1282af1dd7ffSMike Christie 		}
1283a94a2572SXiubo Li 	} else if (udev->cmd_time_out) {
1284a94a2572SXiubo Li 		tcmu_set_next_deadline(&udev->inflight_queue, &udev->cmd_timer);
12859103575aSMike Christie 	}
12867c9e7a6fSAndy Grover 
12877c9e7a6fSAndy Grover 	return handled;
12887c9e7a6fSAndy Grover }
12897c9e7a6fSAndy Grover 
129061fb2482SBodo Stroesser static void tcmu_check_expired_ring_cmd(struct tcmu_cmd *cmd)
12917c9e7a6fSAndy Grover {
1292af1dd7ffSMike Christie 	struct se_cmd *se_cmd;
12937c9e7a6fSAndy Grover 
1294611e2267SAndy Grover 	if (!time_after(jiffies, cmd->deadline))
129561fb2482SBodo Stroesser 		return;
12969103575aSMike Christie 
1297af1dd7ffSMike Christie 	set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
129840d883b0SXiubo Li 	list_del_init(&cmd->queue_entry);
129961fb2482SBodo Stroesser 	se_cmd = cmd->se_cmd;
1300a3512902SBodo Stroesser 	se_cmd->priv = NULL;
1301a86a7586SDmitry Fomichev 	cmd->se_cmd = NULL;
130261fb2482SBodo Stroesser 
130361fb2482SBodo Stroesser 	pr_debug("Timing out inflight cmd %u on dev %s.\n",
130461fb2482SBodo Stroesser 		 cmd->cmd_id, cmd->tcmu_dev->name);
130561fb2482SBodo Stroesser 
130661fb2482SBodo Stroesser 	target_complete_cmd(se_cmd, SAM_STAT_CHECK_CONDITION);
1307af1dd7ffSMike Christie }
13089103575aSMike Christie 
130961fb2482SBodo Stroesser static void tcmu_check_expired_queue_cmd(struct tcmu_cmd *cmd)
131061fb2482SBodo Stroesser {
131161fb2482SBodo Stroesser 	struct se_cmd *se_cmd;
13129103575aSMike Christie 
131361fb2482SBodo Stroesser 	if (!time_after(jiffies, cmd->deadline))
131461fb2482SBodo Stroesser 		return;
131561fb2482SBodo Stroesser 
13169d7464b1SDan Carpenter 	pr_debug("Timing out queued cmd %p on dev %s.\n",
13179d7464b1SDan Carpenter 		  cmd, cmd->tcmu_dev->name);
13189d7464b1SDan Carpenter 
131961fb2482SBodo Stroesser 	list_del_init(&cmd->queue_entry);
132061fb2482SBodo Stroesser 	se_cmd = cmd->se_cmd;
132161fb2482SBodo Stroesser 	tcmu_free_cmd(cmd);
132261fb2482SBodo Stroesser 
132361fb2482SBodo Stroesser 	target_complete_cmd(se_cmd, SAM_STAT_TASK_SET_FULL);
13247c9e7a6fSAndy Grover }
13257c9e7a6fSAndy Grover 
13269103575aSMike Christie static void tcmu_device_timedout(struct tcmu_dev *udev)
13277c9e7a6fSAndy Grover {
1328488ebe4cSMike Christie 	spin_lock(&timed_out_udevs_lock);
1329488ebe4cSMike Christie 	if (list_empty(&udev->timedout_entry))
1330488ebe4cSMike Christie 		list_add_tail(&udev->timedout_entry, &timed_out_udevs);
1331488ebe4cSMike Christie 	spin_unlock(&timed_out_udevs_lock);
13327c9e7a6fSAndy Grover 
1333af1dd7ffSMike Christie 	schedule_delayed_work(&tcmu_unmap_work, 0);
13347c9e7a6fSAndy Grover }
13357c9e7a6fSAndy Grover 
13369103575aSMike Christie static void tcmu_cmd_timedout(struct timer_list *t)
13379103575aSMike Christie {
13389103575aSMike Christie 	struct tcmu_dev *udev = from_timer(udev, t, cmd_timer);
13399103575aSMike Christie 
13409103575aSMike Christie 	pr_debug("%s cmd timeout has expired\n", udev->name);
13419103575aSMike Christie 	tcmu_device_timedout(udev);
13429103575aSMike Christie }
13439103575aSMike Christie 
13449103575aSMike Christie static void tcmu_qfull_timedout(struct timer_list *t)
13459103575aSMike Christie {
13469103575aSMike Christie 	struct tcmu_dev *udev = from_timer(udev, t, qfull_timer);
13479103575aSMike Christie 
13489103575aSMike Christie 	pr_debug("%s qfull timeout has expired\n", udev->name);
13499103575aSMike Christie 	tcmu_device_timedout(udev);
13509103575aSMike Christie }
13519103575aSMike Christie 
13527c9e7a6fSAndy Grover static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
13537c9e7a6fSAndy Grover {
13547c9e7a6fSAndy Grover 	struct tcmu_hba *tcmu_hba;
13557c9e7a6fSAndy Grover 
13567c9e7a6fSAndy Grover 	tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL);
13577c9e7a6fSAndy Grover 	if (!tcmu_hba)
13587c9e7a6fSAndy Grover 		return -ENOMEM;
13597c9e7a6fSAndy Grover 
13607c9e7a6fSAndy Grover 	tcmu_hba->host_id = host_id;
13617c9e7a6fSAndy Grover 	hba->hba_ptr = tcmu_hba;
13627c9e7a6fSAndy Grover 
13637c9e7a6fSAndy Grover 	return 0;
13647c9e7a6fSAndy Grover }
13657c9e7a6fSAndy Grover 
13667c9e7a6fSAndy Grover static void tcmu_detach_hba(struct se_hba *hba)
13677c9e7a6fSAndy Grover {
13687c9e7a6fSAndy Grover 	kfree(hba->hba_ptr);
13697c9e7a6fSAndy Grover 	hba->hba_ptr = NULL;
13707c9e7a6fSAndy Grover }
13717c9e7a6fSAndy Grover 
13727c9e7a6fSAndy Grover static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
13737c9e7a6fSAndy Grover {
13747c9e7a6fSAndy Grover 	struct tcmu_dev *udev;
13757c9e7a6fSAndy Grover 
13767c9e7a6fSAndy Grover 	udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
13777c9e7a6fSAndy Grover 	if (!udev)
13787c9e7a6fSAndy Grover 		return NULL;
1379f3cdbe39SMike Christie 	kref_init(&udev->kref);
13807c9e7a6fSAndy Grover 
13817c9e7a6fSAndy Grover 	udev->name = kstrdup(name, GFP_KERNEL);
13827c9e7a6fSAndy Grover 	if (!udev->name) {
13837c9e7a6fSAndy Grover 		kfree(udev);
13847c9e7a6fSAndy Grover 		return NULL;
13857c9e7a6fSAndy Grover 	}
13867c9e7a6fSAndy Grover 
13877c9e7a6fSAndy Grover 	udev->hba = hba;
1388af980e46SMike Christie 	udev->cmd_time_out = TCMU_TIME_OUT;
13899103575aSMike Christie 	udev->qfull_time_out = -1;
13907c9e7a6fSAndy Grover 
139180eb8761SMike Christie 	udev->max_blocks = DATA_BLOCK_BITS_DEF;
1392b6df4b79SXiubo Li 	mutex_init(&udev->cmdr_lock);
13937c9e7a6fSAndy Grover 
1394ff07e4a4SMike Christie 	INIT_LIST_HEAD(&udev->node);
1395488ebe4cSMike Christie 	INIT_LIST_HEAD(&udev->timedout_entry);
1396a94a2572SXiubo Li 	INIT_LIST_HEAD(&udev->qfull_queue);
1397a94a2572SXiubo Li 	INIT_LIST_HEAD(&udev->inflight_queue);
13987c9e7a6fSAndy Grover 	idr_init(&udev->commands);
13997c9e7a6fSAndy Grover 
14009103575aSMike Christie 	timer_setup(&udev->qfull_timer, tcmu_qfull_timedout, 0);
14019103575aSMike Christie 	timer_setup(&udev->cmd_timer, tcmu_cmd_timedout, 0);
14027c9e7a6fSAndy Grover 
1403c22adc0bSXiubo Li 	INIT_RADIX_TREE(&udev->data_blocks, GFP_KERNEL);
1404c22adc0bSXiubo Li 
14057c9e7a6fSAndy Grover 	return &udev->se_dev;
14067c9e7a6fSAndy Grover }
14077c9e7a6fSAndy Grover 
140861fb2482SBodo Stroesser static void run_qfull_queue(struct tcmu_dev *udev, bool fail)
1409af1dd7ffSMike Christie {
1410af1dd7ffSMike Christie 	struct tcmu_cmd *tcmu_cmd, *tmp_cmd;
1411af1dd7ffSMike Christie 	LIST_HEAD(cmds);
1412af1dd7ffSMike Christie 	sense_reason_t scsi_ret;
1413af1dd7ffSMike Christie 	int ret;
1414af1dd7ffSMike Christie 
1415a94a2572SXiubo Li 	if (list_empty(&udev->qfull_queue))
141661fb2482SBodo Stroesser 		return;
1417af1dd7ffSMike Christie 
1418892782caSMike Christie 	pr_debug("running %s's cmdr queue forcefail %d\n", udev->name, fail);
1419af1dd7ffSMike Christie 
1420a94a2572SXiubo Li 	list_splice_init(&udev->qfull_queue, &cmds);
1421af1dd7ffSMike Christie 
1422a94a2572SXiubo Li 	list_for_each_entry_safe(tcmu_cmd, tmp_cmd, &cmds, queue_entry) {
1423a94a2572SXiubo Li 		list_del_init(&tcmu_cmd->queue_entry);
1424af1dd7ffSMike Christie 
142561fb2482SBodo Stroesser 		pr_debug("removing cmd %p on dev %s from queue\n",
142661fb2482SBodo Stroesser 			 tcmu_cmd, udev->name);
1427af1dd7ffSMike Christie 
1428892782caSMike Christie 		if (fail) {
1429892782caSMike Christie 			/*
1430892782caSMike Christie 			 * We were not able to even start the command, so
1431892782caSMike Christie 			 * fail with busy to allow a retry in case runner
1432892782caSMike Christie 			 * was only temporarily down. If the device is being
1433892782caSMike Christie 			 * removed then LIO core will do the right thing and
1434892782caSMike Christie 			 * fail the retry.
1435892782caSMike Christie 			 */
1436892782caSMike Christie 			target_complete_cmd(tcmu_cmd->se_cmd, SAM_STAT_BUSY);
1437892782caSMike Christie 			tcmu_free_cmd(tcmu_cmd);
1438892782caSMike Christie 			continue;
1439892782caSMike Christie 		}
1440892782caSMike Christie 
1441af1dd7ffSMike Christie 		ret = queue_cmd_ring(tcmu_cmd, &scsi_ret);
1442af1dd7ffSMike Christie 		if (ret < 0) {
144361fb2482SBodo Stroesser 			pr_debug("cmd %p on dev %s failed with %u\n",
144461fb2482SBodo Stroesser 				 tcmu_cmd, udev->name, scsi_ret);
1445af1dd7ffSMike Christie 			/*
1446af1dd7ffSMike Christie 			 * Ignore scsi_ret for now. target_complete_cmd
1447af1dd7ffSMike Christie 			 * drops it.
1448af1dd7ffSMike Christie 			 */
1449af1dd7ffSMike Christie 			target_complete_cmd(tcmu_cmd->se_cmd,
1450af1dd7ffSMike Christie 					    SAM_STAT_CHECK_CONDITION);
1451af1dd7ffSMike Christie 			tcmu_free_cmd(tcmu_cmd);
1452af1dd7ffSMike Christie 		} else if (ret > 0) {
1453af1dd7ffSMike Christie 			pr_debug("ran out of space during cmdr queue run\n");
1454af1dd7ffSMike Christie 			/*
1455af1dd7ffSMike Christie 			 * cmd was requeued, so just put all cmds back in
1456af1dd7ffSMike Christie 			 * the queue
1457af1dd7ffSMike Christie 			 */
1458a94a2572SXiubo Li 			list_splice_tail(&cmds, &udev->qfull_queue);
1459a94a2572SXiubo Li 			break;
1460af1dd7ffSMike Christie 		}
1461af1dd7ffSMike Christie 	}
1462a94a2572SXiubo Li 
1463a94a2572SXiubo Li 	tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer);
1464af1dd7ffSMike Christie }
1465af1dd7ffSMike Christie 
14667c9e7a6fSAndy Grover static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
14677c9e7a6fSAndy Grover {
1468af1dd7ffSMike Christie 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
14697c9e7a6fSAndy Grover 
1470af1dd7ffSMike Christie 	mutex_lock(&udev->cmdr_lock);
1471af1dd7ffSMike Christie 	tcmu_handle_completions(udev);
1472a94a2572SXiubo Li 	run_qfull_queue(udev, false);
1473af1dd7ffSMike Christie 	mutex_unlock(&udev->cmdr_lock);
14747c9e7a6fSAndy Grover 
14757c9e7a6fSAndy Grover 	return 0;
14767c9e7a6fSAndy Grover }
14777c9e7a6fSAndy Grover 
14787c9e7a6fSAndy Grover /*
14797c9e7a6fSAndy Grover  * mmap code from uio.c. Copied here because we want to hook mmap()
14807c9e7a6fSAndy Grover  * and this stuff must come along.
14817c9e7a6fSAndy Grover  */
14827c9e7a6fSAndy Grover static int tcmu_find_mem_index(struct vm_area_struct *vma)
14837c9e7a6fSAndy Grover {
14847c9e7a6fSAndy Grover 	struct tcmu_dev *udev = vma->vm_private_data;
14857c9e7a6fSAndy Grover 	struct uio_info *info = &udev->uio_info;
14867c9e7a6fSAndy Grover 
14877c9e7a6fSAndy Grover 	if (vma->vm_pgoff < MAX_UIO_MAPS) {
14887c9e7a6fSAndy Grover 		if (info->mem[vma->vm_pgoff].size == 0)
14897c9e7a6fSAndy Grover 			return -1;
14907c9e7a6fSAndy Grover 		return (int)vma->vm_pgoff;
14917c9e7a6fSAndy Grover 	}
14927c9e7a6fSAndy Grover 	return -1;
14937c9e7a6fSAndy Grover }
14947c9e7a6fSAndy Grover 
1495b6df4b79SXiubo Li static struct page *tcmu_try_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
1496b6df4b79SXiubo Li {
1497b6df4b79SXiubo Li 	struct page *page;
1498b6df4b79SXiubo Li 
1499b6df4b79SXiubo Li 	mutex_lock(&udev->cmdr_lock);
1500b6df4b79SXiubo Li 	page = tcmu_get_block_page(udev, dbi);
1501b6df4b79SXiubo Li 	if (likely(page)) {
1502b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
1503b6df4b79SXiubo Li 		return page;
1504b6df4b79SXiubo Li 	}
1505b6df4b79SXiubo Li 
1506b6df4b79SXiubo Li 	/*
1507c1c390baSMike Christie 	 * Userspace messed up and passed in a address not in the
1508c1c390baSMike Christie 	 * data iov passed to it.
1509b6df4b79SXiubo Li 	 */
1510c1c390baSMike Christie 	pr_err("Invalid addr to data block mapping  (dbi %u) on device %s\n",
1511c1c390baSMike Christie 	       dbi, udev->name);
1512c1c390baSMike Christie 	page = NULL;
1513b6df4b79SXiubo Li 	mutex_unlock(&udev->cmdr_lock);
1514b6df4b79SXiubo Li 
1515b6df4b79SXiubo Li 	return page;
1516b6df4b79SXiubo Li }
1517b6df4b79SXiubo Li 
151869589c9bSSouptick Joarder static vm_fault_t tcmu_vma_fault(struct vm_fault *vmf)
15197c9e7a6fSAndy Grover {
152011bac800SDave Jiang 	struct tcmu_dev *udev = vmf->vma->vm_private_data;
15217c9e7a6fSAndy Grover 	struct uio_info *info = &udev->uio_info;
15227c9e7a6fSAndy Grover 	struct page *page;
15237c9e7a6fSAndy Grover 	unsigned long offset;
15247c9e7a6fSAndy Grover 	void *addr;
15257c9e7a6fSAndy Grover 
152611bac800SDave Jiang 	int mi = tcmu_find_mem_index(vmf->vma);
15277c9e7a6fSAndy Grover 	if (mi < 0)
15287c9e7a6fSAndy Grover 		return VM_FAULT_SIGBUS;
15297c9e7a6fSAndy Grover 
15307c9e7a6fSAndy Grover 	/*
15317c9e7a6fSAndy Grover 	 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
15327c9e7a6fSAndy Grover 	 * to use mem[N].
15337c9e7a6fSAndy Grover 	 */
15347c9e7a6fSAndy Grover 	offset = (vmf->pgoff - mi) << PAGE_SHIFT;
15357c9e7a6fSAndy Grover 
1536141685a3SXiubo Li 	if (offset < udev->data_off) {
1537141685a3SXiubo Li 		/* For the vmalloc()ed cmd area pages */
15387c9e7a6fSAndy Grover 		addr = (void *)(unsigned long)info->mem[mi].addr + offset;
15397c9e7a6fSAndy Grover 		page = vmalloc_to_page(addr);
1540141685a3SXiubo Li 	} else {
1541141685a3SXiubo Li 		uint32_t dbi;
1542141685a3SXiubo Li 
1543b6df4b79SXiubo Li 		/* For the dynamically growing data area pages */
1544141685a3SXiubo Li 		dbi = (offset - udev->data_off) / DATA_BLOCK_SIZE;
1545b6df4b79SXiubo Li 		page = tcmu_try_get_block_page(udev, dbi);
1546b6df4b79SXiubo Li 		if (!page)
1547c1c390baSMike Christie 			return VM_FAULT_SIGBUS;
1548141685a3SXiubo Li 	}
1549141685a3SXiubo Li 
15507c9e7a6fSAndy Grover 	get_page(page);
15517c9e7a6fSAndy Grover 	vmf->page = page;
15527c9e7a6fSAndy Grover 	return 0;
15537c9e7a6fSAndy Grover }
15547c9e7a6fSAndy Grover 
15557c9e7a6fSAndy Grover static const struct vm_operations_struct tcmu_vm_ops = {
15567c9e7a6fSAndy Grover 	.fault = tcmu_vma_fault,
15577c9e7a6fSAndy Grover };
15587c9e7a6fSAndy Grover 
15597c9e7a6fSAndy Grover static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma)
15607c9e7a6fSAndy Grover {
15617c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
15627c9e7a6fSAndy Grover 
15637c9e7a6fSAndy Grover 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
15647c9e7a6fSAndy Grover 	vma->vm_ops = &tcmu_vm_ops;
15657c9e7a6fSAndy Grover 
15667c9e7a6fSAndy Grover 	vma->vm_private_data = udev;
15677c9e7a6fSAndy Grover 
15687c9e7a6fSAndy Grover 	/* Ensure the mmap is exactly the right size */
156980eb8761SMike Christie 	if (vma_pages(vma) != (udev->ring_size >> PAGE_SHIFT))
15707c9e7a6fSAndy Grover 		return -EINVAL;
15717c9e7a6fSAndy Grover 
15727c9e7a6fSAndy Grover 	return 0;
15737c9e7a6fSAndy Grover }
15747c9e7a6fSAndy Grover 
15757c9e7a6fSAndy Grover static int tcmu_open(struct uio_info *info, struct inode *inode)
15767c9e7a6fSAndy Grover {
15777c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
15787c9e7a6fSAndy Grover 
15797c9e7a6fSAndy Grover 	/* O_EXCL not supported for char devs, so fake it? */
15807c9e7a6fSAndy Grover 	if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
15817c9e7a6fSAndy Grover 		return -EBUSY;
15827c9e7a6fSAndy Grover 
1583b6df4b79SXiubo Li 	udev->inode = inode;
15849260695dSMike Christie 	kref_get(&udev->kref);
1585b6df4b79SXiubo Li 
15867c9e7a6fSAndy Grover 	pr_debug("open\n");
15877c9e7a6fSAndy Grover 
15887c9e7a6fSAndy Grover 	return 0;
15897c9e7a6fSAndy Grover }
15907c9e7a6fSAndy Grover 
1591f3cdbe39SMike Christie static void tcmu_dev_call_rcu(struct rcu_head *p)
1592f3cdbe39SMike Christie {
1593f3cdbe39SMike Christie 	struct se_device *dev = container_of(p, struct se_device, rcu_head);
1594f3cdbe39SMike Christie 	struct tcmu_dev *udev = TCMU_DEV(dev);
1595f3cdbe39SMike Christie 
1596f3cdbe39SMike Christie 	kfree(udev->uio_info.name);
1597f3cdbe39SMike Christie 	kfree(udev->name);
1598f3cdbe39SMike Christie 	kfree(udev);
1599f3cdbe39SMike Christie }
1600f3cdbe39SMike Christie 
1601c22adc0bSXiubo Li static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
1602c22adc0bSXiubo Li {
1603c22adc0bSXiubo Li 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
1604c22adc0bSXiubo Li 		kmem_cache_free(tcmu_cmd_cache, cmd);
1605c22adc0bSXiubo Li 		return 0;
1606c22adc0bSXiubo Li 	}
1607c22adc0bSXiubo Li 	return -EINVAL;
1608c22adc0bSXiubo Li }
1609c22adc0bSXiubo Li 
1610bf99ec13SMike Christie static void tcmu_blocks_release(struct radix_tree_root *blocks,
1611bf99ec13SMike Christie 				int start, int end)
1612c22adc0bSXiubo Li {
1613c22adc0bSXiubo Li 	int i;
1614c22adc0bSXiubo Li 	struct page *page;
1615c22adc0bSXiubo Li 
1616bf99ec13SMike Christie 	for (i = start; i < end; i++) {
1617bf99ec13SMike Christie 		page = radix_tree_delete(blocks, i);
1618c22adc0bSXiubo Li 		if (page) {
1619c22adc0bSXiubo Li 			__free_page(page);
1620c22adc0bSXiubo Li 			atomic_dec(&global_db_count);
1621c22adc0bSXiubo Li 		}
1622c22adc0bSXiubo Li 	}
1623c22adc0bSXiubo Li }
1624c22adc0bSXiubo Li 
1625f3cdbe39SMike Christie static void tcmu_dev_kref_release(struct kref *kref)
1626f3cdbe39SMike Christie {
1627f3cdbe39SMike Christie 	struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref);
1628f3cdbe39SMike Christie 	struct se_device *dev = &udev->se_dev;
1629c22adc0bSXiubo Li 	struct tcmu_cmd *cmd;
1630c22adc0bSXiubo Li 	bool all_expired = true;
1631c22adc0bSXiubo Li 	int i;
1632c22adc0bSXiubo Li 
1633c22adc0bSXiubo Li 	vfree(udev->mb_addr);
1634c22adc0bSXiubo Li 	udev->mb_addr = NULL;
1635c22adc0bSXiubo Li 
1636488ebe4cSMike Christie 	spin_lock_bh(&timed_out_udevs_lock);
1637488ebe4cSMike Christie 	if (!list_empty(&udev->timedout_entry))
1638488ebe4cSMike Christie 		list_del(&udev->timedout_entry);
1639488ebe4cSMike Christie 	spin_unlock_bh(&timed_out_udevs_lock);
1640488ebe4cSMike Christie 
1641c22adc0bSXiubo Li 	/* Upper layer should drain all requests before calling this */
16426fddcb77SMike Christie 	mutex_lock(&udev->cmdr_lock);
1643c22adc0bSXiubo Li 	idr_for_each_entry(&udev->commands, cmd, i) {
1644c22adc0bSXiubo Li 		if (tcmu_check_and_free_pending_cmd(cmd) != 0)
1645c22adc0bSXiubo Li 			all_expired = false;
1646c22adc0bSXiubo Li 	}
164761fb2482SBodo Stroesser 	if (!list_empty(&udev->qfull_queue))
164861fb2482SBodo Stroesser 		all_expired = false;
1649c22adc0bSXiubo Li 	idr_destroy(&udev->commands);
1650c22adc0bSXiubo Li 	WARN_ON(!all_expired);
1651c22adc0bSXiubo Li 
1652bf99ec13SMike Christie 	tcmu_blocks_release(&udev->data_blocks, 0, udev->dbi_max + 1);
165398effe47SAndy Shevchenko 	bitmap_free(udev->data_bitmap);
1654bf99ec13SMike Christie 	mutex_unlock(&udev->cmdr_lock);
1655f3cdbe39SMike Christie 
1656f3cdbe39SMike Christie 	call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
1657f3cdbe39SMike Christie }
1658f3cdbe39SMike Christie 
16597c9e7a6fSAndy Grover static int tcmu_release(struct uio_info *info, struct inode *inode)
16607c9e7a6fSAndy Grover {
16617c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
16627c9e7a6fSAndy Grover 
16637c9e7a6fSAndy Grover 	clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
16647c9e7a6fSAndy Grover 
16657c9e7a6fSAndy Grover 	pr_debug("close\n");
16669260695dSMike Christie 	/* release ref from open */
1667f3cdbe39SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
16687c9e7a6fSAndy Grover 	return 0;
16697c9e7a6fSAndy Grover }
16707c9e7a6fSAndy Grover 
16719de3a1efSMike Christie static int tcmu_init_genl_cmd_reply(struct tcmu_dev *udev, int cmd)
1672b3af66e2SMike Christie {
1673b3af66e2SMike Christie 	struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
1674b3af66e2SMike Christie 
1675b3af66e2SMike Christie 	if (!tcmu_kern_cmd_reply_supported)
16769de3a1efSMike Christie 		return 0;
1677b849b456SKenjiro Nakayama 
1678b849b456SKenjiro Nakayama 	if (udev->nl_reply_supported <= 0)
16799de3a1efSMike Christie 		return 0;
1680b849b456SKenjiro Nakayama 
16813228691fSMike Christie 	mutex_lock(&tcmu_nl_cmd_mutex);
1682b3af66e2SMike Christie 
1683bdaeedc1SMike Christie 	if (tcmu_netlink_blocked) {
1684bdaeedc1SMike Christie 		mutex_unlock(&tcmu_nl_cmd_mutex);
1685bdaeedc1SMike Christie 		pr_warn("Failing nl cmd %d on %s. Interface is blocked.\n", cmd,
1686bdaeedc1SMike Christie 			udev->name);
1687bdaeedc1SMike Christie 		return -EAGAIN;
1688bdaeedc1SMike Christie 	}
1689b3af66e2SMike Christie 
1690b3af66e2SMike Christie 	if (nl_cmd->cmd != TCMU_CMD_UNSPEC) {
16913228691fSMike Christie 		mutex_unlock(&tcmu_nl_cmd_mutex);
16929de3a1efSMike Christie 		pr_warn("netlink cmd %d already executing on %s\n",
16939de3a1efSMike Christie 			 nl_cmd->cmd, udev->name);
16949de3a1efSMike Christie 		return -EBUSY;
1695b3af66e2SMike Christie 	}
1696b3af66e2SMike Christie 
1697b3af66e2SMike Christie 	memset(nl_cmd, 0, sizeof(*nl_cmd));
1698b3af66e2SMike Christie 	nl_cmd->cmd = cmd;
16993228691fSMike Christie 	nl_cmd->udev = udev;
1700b3af66e2SMike Christie 	init_completion(&nl_cmd->complete);
17013228691fSMike Christie 	INIT_LIST_HEAD(&nl_cmd->nl_list);
1702b3af66e2SMike Christie 
17033228691fSMike Christie 	list_add_tail(&nl_cmd->nl_list, &tcmu_nl_cmd_list);
17043228691fSMike Christie 
17053228691fSMike Christie 	mutex_unlock(&tcmu_nl_cmd_mutex);
17069de3a1efSMike Christie 	return 0;
1707b3af66e2SMike Christie }
1708b3af66e2SMike Christie 
17097d894862SLi Zhong static void tcmu_destroy_genl_cmd_reply(struct tcmu_dev *udev)
17107d894862SLi Zhong {
17117d894862SLi Zhong 	struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
17127d894862SLi Zhong 
17137d894862SLi Zhong 	if (!tcmu_kern_cmd_reply_supported)
17147d894862SLi Zhong 		return;
17157d894862SLi Zhong 
17167d894862SLi Zhong 	if (udev->nl_reply_supported <= 0)
17177d894862SLi Zhong 		return;
17187d894862SLi Zhong 
17197d894862SLi Zhong 	mutex_lock(&tcmu_nl_cmd_mutex);
17207d894862SLi Zhong 
17217d894862SLi Zhong 	list_del(&nl_cmd->nl_list);
17227d894862SLi Zhong 	memset(nl_cmd, 0, sizeof(*nl_cmd));
17237d894862SLi Zhong 
17247d894862SLi Zhong 	mutex_unlock(&tcmu_nl_cmd_mutex);
17257d894862SLi Zhong }
17267d894862SLi Zhong 
1727b3af66e2SMike Christie static int tcmu_wait_genl_cmd_reply(struct tcmu_dev *udev)
1728b3af66e2SMike Christie {
1729b3af66e2SMike Christie 	struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
1730b3af66e2SMike Christie 	int ret;
1731b3af66e2SMike Christie 
1732b3af66e2SMike Christie 	if (!tcmu_kern_cmd_reply_supported)
1733b3af66e2SMike Christie 		return 0;
1734b3af66e2SMike Christie 
1735b849b456SKenjiro Nakayama 	if (udev->nl_reply_supported <= 0)
1736b849b456SKenjiro Nakayama 		return 0;
1737b849b456SKenjiro Nakayama 
1738b3af66e2SMike Christie 	pr_debug("sleeping for nl reply\n");
1739b3af66e2SMike Christie 	wait_for_completion(&nl_cmd->complete);
1740b3af66e2SMike Christie 
17413228691fSMike Christie 	mutex_lock(&tcmu_nl_cmd_mutex);
1742b3af66e2SMike Christie 	nl_cmd->cmd = TCMU_CMD_UNSPEC;
1743b3af66e2SMike Christie 	ret = nl_cmd->status;
17443228691fSMike Christie 	mutex_unlock(&tcmu_nl_cmd_mutex);
1745b3af66e2SMike Christie 
174685fae482SLuis de Bethencourt 	return ret;
1747b3af66e2SMike Christie }
1748b3af66e2SMike Christie 
17490e5aee39SZhu Lingshan static int tcmu_netlink_event_init(struct tcmu_dev *udev,
17500e5aee39SZhu Lingshan 				   enum tcmu_genl_cmd cmd,
17510e5aee39SZhu Lingshan 				   struct sk_buff **buf, void **hdr)
17527c9e7a6fSAndy Grover {
17537c9e7a6fSAndy Grover 	struct sk_buff *skb;
17547c9e7a6fSAndy Grover 	void *msg_header;
17556e14eab9SNicholas Bellinger 	int ret = -ENOMEM;
17567c9e7a6fSAndy Grover 
17577c9e7a6fSAndy Grover 	skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
17587c9e7a6fSAndy Grover 	if (!skb)
17596e14eab9SNicholas Bellinger 		return ret;
17607c9e7a6fSAndy Grover 
17617c9e7a6fSAndy Grover 	msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd);
17626e14eab9SNicholas Bellinger 	if (!msg_header)
17636e14eab9SNicholas Bellinger 		goto free_skb;
17647c9e7a6fSAndy Grover 
1765b3af66e2SMike Christie 	ret = nla_put_string(skb, TCMU_ATTR_DEVICE, udev->uio_info.name);
17666e14eab9SNicholas Bellinger 	if (ret < 0)
17676e14eab9SNicholas Bellinger 		goto free_skb;
17687c9e7a6fSAndy Grover 
1769b3af66e2SMike Christie 	ret = nla_put_u32(skb, TCMU_ATTR_MINOR, udev->uio_info.uio_dev->minor);
1770b3af66e2SMike Christie 	if (ret < 0)
1771b3af66e2SMike Christie 		goto free_skb;
1772b3af66e2SMike Christie 
1773b3af66e2SMike Christie 	ret = nla_put_u32(skb, TCMU_ATTR_DEVICE_ID, udev->se_dev.dev_index);
17746e14eab9SNicholas Bellinger 	if (ret < 0)
17756e14eab9SNicholas Bellinger 		goto free_skb;
17767c9e7a6fSAndy Grover 
17770e5aee39SZhu Lingshan 	*buf = skb;
17780e5aee39SZhu Lingshan 	*hdr = msg_header;
17790e5aee39SZhu Lingshan 	return ret;
17800e5aee39SZhu Lingshan 
17810e5aee39SZhu Lingshan free_skb:
17820e5aee39SZhu Lingshan 	nlmsg_free(skb);
17830e5aee39SZhu Lingshan 	return ret;
17842d76443eSMike Christie }
17852d76443eSMike Christie 
17860e5aee39SZhu Lingshan static int tcmu_netlink_event_send(struct tcmu_dev *udev,
17870e5aee39SZhu Lingshan 				   enum tcmu_genl_cmd cmd,
178806add777SMike Christie 				   struct sk_buff *skb, void *msg_header)
17890e5aee39SZhu Lingshan {
179006add777SMike Christie 	int ret;
17918a45885cSBryant G. Ly 
1792053c095aSJohannes Berg 	genlmsg_end(skb, msg_header);
17937c9e7a6fSAndy Grover 
17949de3a1efSMike Christie 	ret = tcmu_init_genl_cmd_reply(udev, cmd);
17959de3a1efSMike Christie 	if (ret) {
17969de3a1efSMike Christie 		nlmsg_free(skb);
17979de3a1efSMike Christie 		return ret;
17989de3a1efSMike Christie 	}
1799b3af66e2SMike Christie 
180020c08b36SSheng Yang 	ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0,
18017c9e7a6fSAndy Grover 				      TCMU_MCGRP_CONFIG, GFP_KERNEL);
18022ff717cdSCathy Avery 
18032ff717cdSCathy Avery 	/* Wait during an add as the listener may not be up yet */
18042ff717cdSCathy Avery 	if (ret == 0 ||
18052ff717cdSCathy Avery 	   (ret == -ESRCH && cmd == TCMU_CMD_ADDED_DEVICE))
18062ff717cdSCathy Avery 		return tcmu_wait_genl_cmd_reply(udev);
18077d894862SLi Zhong 	else
18087d894862SLi Zhong 		tcmu_destroy_genl_cmd_reply(udev);
18092ff717cdSCathy Avery 
18100e5aee39SZhu Lingshan 	return ret;
18110e5aee39SZhu Lingshan }
18127c9e7a6fSAndy Grover 
1813e0c240acSZhu Lingshan static int tcmu_send_dev_add_event(struct tcmu_dev *udev)
1814e0c240acSZhu Lingshan {
1815e0c240acSZhu Lingshan 	struct sk_buff *skb = NULL;
1816e0c240acSZhu Lingshan 	void *msg_header = NULL;
1817e0c240acSZhu Lingshan 	int ret = 0;
1818e0c240acSZhu Lingshan 
1819e0c240acSZhu Lingshan 	ret = tcmu_netlink_event_init(udev, TCMU_CMD_ADDED_DEVICE, &skb,
1820e0c240acSZhu Lingshan 				      &msg_header);
1821e0c240acSZhu Lingshan 	if (ret < 0)
18227c9e7a6fSAndy Grover 		return ret;
182306add777SMike Christie 	return tcmu_netlink_event_send(udev, TCMU_CMD_ADDED_DEVICE, skb,
182406add777SMike Christie 				       msg_header);
1825e0c240acSZhu Lingshan }
1826e0c240acSZhu Lingshan 
1827f892bd8eSZhu Lingshan static int tcmu_send_dev_remove_event(struct tcmu_dev *udev)
1828f892bd8eSZhu Lingshan {
1829f892bd8eSZhu Lingshan 	struct sk_buff *skb = NULL;
1830f892bd8eSZhu Lingshan 	void *msg_header = NULL;
1831f892bd8eSZhu Lingshan 	int ret = 0;
1832f892bd8eSZhu Lingshan 
1833f892bd8eSZhu Lingshan 	ret = tcmu_netlink_event_init(udev, TCMU_CMD_REMOVED_DEVICE,
1834f892bd8eSZhu Lingshan 				      &skb, &msg_header);
1835f892bd8eSZhu Lingshan 	if (ret < 0)
18366e14eab9SNicholas Bellinger 		return ret;
1837f892bd8eSZhu Lingshan 	return tcmu_netlink_event_send(udev, TCMU_CMD_REMOVED_DEVICE,
183806add777SMike Christie 				       skb, msg_header);
18397c9e7a6fSAndy Grover }
18407c9e7a6fSAndy Grover 
1841de8c5221SBryant G. Ly static int tcmu_update_uio_info(struct tcmu_dev *udev)
18427c9e7a6fSAndy Grover {
18437c9e7a6fSAndy Grover 	struct tcmu_hba *hba = udev->hba->hba_ptr;
18447c9e7a6fSAndy Grover 	struct uio_info *info;
18457c9e7a6fSAndy Grover 	char *str;
18467c9e7a6fSAndy Grover 
18477c9e7a6fSAndy Grover 	info = &udev->uio_info;
184822c2f35fSChristophe JAILLET 
184922c2f35fSChristophe JAILLET 	if (udev->dev_config[0])
185022c2f35fSChristophe JAILLET 		str = kasprintf(GFP_KERNEL, "tcm-user/%u/%s/%s", hba->host_id,
185122c2f35fSChristophe JAILLET 				udev->name, udev->dev_config);
185222c2f35fSChristophe JAILLET 	else
185322c2f35fSChristophe JAILLET 		str = kasprintf(GFP_KERNEL, "tcm-user/%u/%s", hba->host_id,
185422c2f35fSChristophe JAILLET 				udev->name);
18557c9e7a6fSAndy Grover 	if (!str)
18567c9e7a6fSAndy Grover 		return -ENOMEM;
18577c9e7a6fSAndy Grover 
1858ededd039SBryant G. Ly 	/* If the old string exists, free it */
1859ededd039SBryant G. Ly 	kfree(info->name);
18607c9e7a6fSAndy Grover 	info->name = str;
18617c9e7a6fSAndy Grover 
1862de8c5221SBryant G. Ly 	return 0;
1863de8c5221SBryant G. Ly }
1864de8c5221SBryant G. Ly 
1865de8c5221SBryant G. Ly static int tcmu_configure_device(struct se_device *dev)
1866de8c5221SBryant G. Ly {
1867de8c5221SBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(dev);
1868de8c5221SBryant G. Ly 	struct uio_info *info;
1869de8c5221SBryant G. Ly 	struct tcmu_mailbox *mb;
1870de8c5221SBryant G. Ly 	int ret = 0;
1871de8c5221SBryant G. Ly 
1872de8c5221SBryant G. Ly 	ret = tcmu_update_uio_info(udev);
1873de8c5221SBryant G. Ly 	if (ret)
1874de8c5221SBryant G. Ly 		return ret;
1875de8c5221SBryant G. Ly 
1876de8c5221SBryant G. Ly 	info = &udev->uio_info;
1877de8c5221SBryant G. Ly 
1878c97840c8SMike Christie 	mutex_lock(&udev->cmdr_lock);
187998effe47SAndy Shevchenko 	udev->data_bitmap = bitmap_zalloc(udev->max_blocks, GFP_KERNEL);
1880c97840c8SMike Christie 	mutex_unlock(&udev->cmdr_lock);
1881a24e7917SWei Yongjun 	if (!udev->data_bitmap) {
1882a24e7917SWei Yongjun 		ret = -ENOMEM;
188380eb8761SMike Christie 		goto err_bitmap_alloc;
1884a24e7917SWei Yongjun 	}
188580eb8761SMike Christie 
1886141685a3SXiubo Li 	udev->mb_addr = vzalloc(CMDR_SIZE);
18877c9e7a6fSAndy Grover 	if (!udev->mb_addr) {
18887c9e7a6fSAndy Grover 		ret = -ENOMEM;
18897c9e7a6fSAndy Grover 		goto err_vzalloc;
18907c9e7a6fSAndy Grover 	}
18917c9e7a6fSAndy Grover 
18927c9e7a6fSAndy Grover 	/* mailbox fits in first part of CMDR space */
18937c9e7a6fSAndy Grover 	udev->cmdr_size = CMDR_SIZE - CMDR_OFF;
18947c9e7a6fSAndy Grover 	udev->data_off = CMDR_SIZE;
189580eb8761SMike Christie 	udev->data_size = udev->max_blocks * DATA_BLOCK_SIZE;
1896b6df4b79SXiubo Li 	udev->dbi_thresh = 0; /* Default in Idle state */
18977c9e7a6fSAndy Grover 
1898141685a3SXiubo Li 	/* Initialise the mailbox of the ring buffer */
18997c9e7a6fSAndy Grover 	mb = udev->mb_addr;
19000ad46af8SAndy Grover 	mb->version = TCMU_MAILBOX_VERSION;
19016c3796d1Sbstroesser@ts.fujitsu.com 	mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC | TCMU_MAILBOX_FLAG_CAP_READ_LEN;
19027c9e7a6fSAndy Grover 	mb->cmdr_off = CMDR_OFF;
19037c9e7a6fSAndy Grover 	mb->cmdr_size = udev->cmdr_size;
19047c9e7a6fSAndy Grover 
19057c9e7a6fSAndy Grover 	WARN_ON(!PAGE_ALIGNED(udev->data_off));
19067c9e7a6fSAndy Grover 	WARN_ON(udev->data_size % PAGE_SIZE);
190726418649SSheng Yang 	WARN_ON(udev->data_size % DATA_BLOCK_SIZE);
19087c9e7a6fSAndy Grover 
1909ac64a2ceSDavid Disseldorp 	info->version = __stringify(TCMU_MAILBOX_VERSION);
19107c9e7a6fSAndy Grover 
19117c9e7a6fSAndy Grover 	info->mem[0].name = "tcm-user command & data buffer";
19120633e123SArnd Bergmann 	info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr;
191380eb8761SMike Christie 	info->mem[0].size = udev->ring_size = udev->data_size + CMDR_SIZE;
1914141685a3SXiubo Li 	info->mem[0].memtype = UIO_MEM_NONE;
19157c9e7a6fSAndy Grover 
19167c9e7a6fSAndy Grover 	info->irqcontrol = tcmu_irqcontrol;
19177c9e7a6fSAndy Grover 	info->irq = UIO_IRQ_CUSTOM;
19187c9e7a6fSAndy Grover 
19197c9e7a6fSAndy Grover 	info->mmap = tcmu_mmap;
19207c9e7a6fSAndy Grover 	info->open = tcmu_open;
19217c9e7a6fSAndy Grover 	info->release = tcmu_release;
19227c9e7a6fSAndy Grover 
19237c9e7a6fSAndy Grover 	ret = uio_register_device(tcmu_root_device, info);
19247c9e7a6fSAndy Grover 	if (ret)
19257c9e7a6fSAndy Grover 		goto err_register;
19267c9e7a6fSAndy Grover 
192781ee28deSSheng Yang 	/* User can set hw_block_size before enable the device */
192881ee28deSSheng Yang 	if (dev->dev_attrib.hw_block_size == 0)
19297c9e7a6fSAndy Grover 		dev->dev_attrib.hw_block_size = 512;
193081ee28deSSheng Yang 	/* Other attributes can be configured in userspace */
19313abaa2bfSMike Christie 	if (!dev->dev_attrib.hw_max_sectors)
19327c9e7a6fSAndy Grover 		dev->dev_attrib.hw_max_sectors = 128;
19339a8bb606SBryant G. Ly 	if (!dev->dev_attrib.emulate_write_cache)
19349a8bb606SBryant G. Ly 		dev->dev_attrib.emulate_write_cache = 0;
19357c9e7a6fSAndy Grover 	dev->dev_attrib.hw_queue_depth = 128;
19367c9e7a6fSAndy Grover 
1937b849b456SKenjiro Nakayama 	/* If user didn't explicitly disable netlink reply support, use
1938b849b456SKenjiro Nakayama 	 * module scope setting.
1939b849b456SKenjiro Nakayama 	 */
1940b849b456SKenjiro Nakayama 	if (udev->nl_reply_supported >= 0)
1941b849b456SKenjiro Nakayama 		udev->nl_reply_supported = tcmu_kern_cmd_reply_supported;
1942b849b456SKenjiro Nakayama 
1943f3cdbe39SMike Christie 	/*
1944f3cdbe39SMike Christie 	 * Get a ref incase userspace does a close on the uio device before
1945f3cdbe39SMike Christie 	 * LIO has initiated tcmu_free_device.
1946f3cdbe39SMike Christie 	 */
1947f3cdbe39SMike Christie 	kref_get(&udev->kref);
1948f3cdbe39SMike Christie 
1949e0c240acSZhu Lingshan 	ret = tcmu_send_dev_add_event(udev);
19507c9e7a6fSAndy Grover 	if (ret)
19517c9e7a6fSAndy Grover 		goto err_netlink;
19527c9e7a6fSAndy Grover 
1953b6df4b79SXiubo Li 	mutex_lock(&root_udev_mutex);
1954b6df4b79SXiubo Li 	list_add(&udev->node, &root_udev);
1955b6df4b79SXiubo Li 	mutex_unlock(&root_udev_mutex);
1956b6df4b79SXiubo Li 
19577c9e7a6fSAndy Grover 	return 0;
19587c9e7a6fSAndy Grover 
19597c9e7a6fSAndy Grover err_netlink:
1960f3cdbe39SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
19617c9e7a6fSAndy Grover 	uio_unregister_device(&udev->uio_info);
19627c9e7a6fSAndy Grover err_register:
19637c9e7a6fSAndy Grover 	vfree(udev->mb_addr);
1964c22adc0bSXiubo Li 	udev->mb_addr = NULL;
19657c9e7a6fSAndy Grover err_vzalloc:
196698effe47SAndy Shevchenko 	bitmap_free(udev->data_bitmap);
196780eb8761SMike Christie 	udev->data_bitmap = NULL;
196880eb8761SMike Christie err_bitmap_alloc:
19697c9e7a6fSAndy Grover 	kfree(info->name);
1970f3cdbe39SMike Christie 	info->name = NULL;
19717c9e7a6fSAndy Grover 
19727c9e7a6fSAndy Grover 	return ret;
19737c9e7a6fSAndy Grover }
19747c9e7a6fSAndy Grover 
19757c9e7a6fSAndy Grover static void tcmu_free_device(struct se_device *dev)
19767c9e7a6fSAndy Grover {
19777c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
197892634706SMike Christie 
197992634706SMike Christie 	/* release ref from init */
198092634706SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
198192634706SMike Christie }
198292634706SMike Christie 
198392634706SMike Christie static void tcmu_destroy_device(struct se_device *dev)
198492634706SMike Christie {
198592634706SMike Christie 	struct tcmu_dev *udev = TCMU_DEV(dev);
19867c9e7a6fSAndy Grover 
19879103575aSMike Christie 	del_timer_sync(&udev->cmd_timer);
19889103575aSMike Christie 	del_timer_sync(&udev->qfull_timer);
19897c9e7a6fSAndy Grover 
1990b6df4b79SXiubo Li 	mutex_lock(&root_udev_mutex);
1991b6df4b79SXiubo Li 	list_del(&udev->node);
1992b6df4b79SXiubo Li 	mutex_unlock(&root_udev_mutex);
1993b6df4b79SXiubo Li 
1994f892bd8eSZhu Lingshan 	tcmu_send_dev_remove_event(udev);
19957c9e7a6fSAndy Grover 
19967c9e7a6fSAndy Grover 	uio_unregister_device(&udev->uio_info);
19979260695dSMike Christie 
19989260695dSMike Christie 	/* release ref from configure */
19999260695dSMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
20007c9e7a6fSAndy Grover }
20017c9e7a6fSAndy Grover 
2002892782caSMike Christie static void tcmu_unblock_dev(struct tcmu_dev *udev)
2003892782caSMike Christie {
2004892782caSMike Christie 	mutex_lock(&udev->cmdr_lock);
2005892782caSMike Christie 	clear_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags);
2006892782caSMike Christie 	mutex_unlock(&udev->cmdr_lock);
2007892782caSMike Christie }
2008892782caSMike Christie 
2009892782caSMike Christie static void tcmu_block_dev(struct tcmu_dev *udev)
2010892782caSMike Christie {
2011892782caSMike Christie 	mutex_lock(&udev->cmdr_lock);
2012892782caSMike Christie 
2013892782caSMike Christie 	if (test_and_set_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags))
2014892782caSMike Christie 		goto unlock;
2015892782caSMike Christie 
2016892782caSMike Christie 	/* complete IO that has executed successfully */
2017892782caSMike Christie 	tcmu_handle_completions(udev);
2018892782caSMike Christie 	/* fail IO waiting to be queued */
2019a94a2572SXiubo Li 	run_qfull_queue(udev, true);
2020892782caSMike Christie 
2021892782caSMike Christie unlock:
2022892782caSMike Christie 	mutex_unlock(&udev->cmdr_lock);
2023892782caSMike Christie }
2024892782caSMike Christie 
2025892782caSMike Christie static void tcmu_reset_ring(struct tcmu_dev *udev, u8 err_level)
2026892782caSMike Christie {
2027892782caSMike Christie 	struct tcmu_mailbox *mb;
2028892782caSMike Christie 	struct tcmu_cmd *cmd;
2029892782caSMike Christie 	int i;
2030892782caSMike Christie 
2031892782caSMike Christie 	mutex_lock(&udev->cmdr_lock);
2032892782caSMike Christie 
2033892782caSMike Christie 	idr_for_each_entry(&udev->commands, cmd, i) {
2034892782caSMike Christie 		pr_debug("removing cmd %u on dev %s from ring (is expired %d)\n",
2035892782caSMike Christie 			  cmd->cmd_id, udev->name,
2036892782caSMike Christie 			  test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags));
2037892782caSMike Christie 
2038892782caSMike Christie 		idr_remove(&udev->commands, i);
2039892782caSMike Christie 		if (!test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
2040a86a7586SDmitry Fomichev 			WARN_ON(!cmd->se_cmd);
2041a94a2572SXiubo Li 			list_del_init(&cmd->queue_entry);
2042892782caSMike Christie 			if (err_level == 1) {
2043892782caSMike Christie 				/*
2044892782caSMike Christie 				 * Userspace was not able to start the
2045892782caSMike Christie 				 * command or it is retryable.
2046892782caSMike Christie 				 */
2047892782caSMike Christie 				target_complete_cmd(cmd->se_cmd, SAM_STAT_BUSY);
2048892782caSMike Christie 			} else {
2049892782caSMike Christie 				/* hard failure */
2050892782caSMike Christie 				target_complete_cmd(cmd->se_cmd,
2051892782caSMike Christie 						    SAM_STAT_CHECK_CONDITION);
2052892782caSMike Christie 			}
2053892782caSMike Christie 		}
2054892782caSMike Christie 		tcmu_cmd_free_data(cmd, cmd->dbi_cnt);
2055892782caSMike Christie 		tcmu_free_cmd(cmd);
2056892782caSMike Christie 	}
2057892782caSMike Christie 
2058892782caSMike Christie 	mb = udev->mb_addr;
2059892782caSMike Christie 	tcmu_flush_dcache_range(mb, sizeof(*mb));
2060892782caSMike Christie 	pr_debug("mb last %u head %u tail %u\n", udev->cmdr_last_cleaned,
2061892782caSMike Christie 		 mb->cmd_tail, mb->cmd_head);
2062892782caSMike Christie 
2063892782caSMike Christie 	udev->cmdr_last_cleaned = 0;
2064892782caSMike Christie 	mb->cmd_tail = 0;
2065892782caSMike Christie 	mb->cmd_head = 0;
2066892782caSMike Christie 	tcmu_flush_dcache_range(mb, sizeof(*mb));
2067066f79a5SBodo Stroesser 	clear_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
2068892782caSMike Christie 
2069892782caSMike Christie 	del_timer(&udev->cmd_timer);
2070892782caSMike Christie 
207161fb2482SBodo Stroesser 	run_qfull_queue(udev, false);
207261fb2482SBodo Stroesser 
2073892782caSMike Christie 	mutex_unlock(&udev->cmdr_lock);
2074892782caSMike Christie }
2075892782caSMike Christie 
20767c9e7a6fSAndy Grover enum {
20773abaa2bfSMike Christie 	Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors,
207880eb8761SMike Christie 	Opt_nl_reply_supported, Opt_max_data_area_mb, Opt_err,
20797c9e7a6fSAndy Grover };
20807c9e7a6fSAndy Grover 
20817c9e7a6fSAndy Grover static match_table_t tokens = {
20827c9e7a6fSAndy Grover 	{Opt_dev_config, "dev_config=%s"},
20830e0d7526SMike Christie 	{Opt_dev_size, "dev_size=%s"},
2084b60cb1f8SMike Christie 	{Opt_hw_block_size, "hw_block_size=%d"},
2085b60cb1f8SMike Christie 	{Opt_hw_max_sectors, "hw_max_sectors=%d"},
2086b849b456SKenjiro Nakayama 	{Opt_nl_reply_supported, "nl_reply_supported=%d"},
2087c97840c8SMike Christie 	{Opt_max_data_area_mb, "max_data_area_mb=%d"},
20887c9e7a6fSAndy Grover 	{Opt_err, NULL}
20897c9e7a6fSAndy Grover };
20907c9e7a6fSAndy Grover 
20913abaa2bfSMike Christie static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib)
20923abaa2bfSMike Christie {
2093b60cb1f8SMike Christie 	int val, ret;
20943abaa2bfSMike Christie 
2095b60cb1f8SMike Christie 	ret = match_int(arg, &val);
20963abaa2bfSMike Christie 	if (ret < 0) {
2097b60cb1f8SMike Christie 		pr_err("match_int() failed for dev attrib. Error %d.\n",
2098b60cb1f8SMike Christie 		       ret);
20993abaa2bfSMike Christie 		return ret;
21003abaa2bfSMike Christie 	}
2101b60cb1f8SMike Christie 
2102b60cb1f8SMike Christie 	if (val <= 0) {
2103b60cb1f8SMike Christie 		pr_err("Invalid dev attrib value %d. Must be greater than zero.\n",
2104b60cb1f8SMike Christie 		       val);
21053abaa2bfSMike Christie 		return -EINVAL;
21063abaa2bfSMike Christie 	}
2107b60cb1f8SMike Christie 	*dev_attrib = val;
21083abaa2bfSMike Christie 	return 0;
21093abaa2bfSMike Christie }
21103abaa2bfSMike Christie 
2111c97840c8SMike Christie static int tcmu_set_max_blocks_param(struct tcmu_dev *udev, substring_t *arg)
2112c97840c8SMike Christie {
2113c97840c8SMike Christie 	int val, ret;
2114c97840c8SMike Christie 
2115c97840c8SMike Christie 	ret = match_int(arg, &val);
2116c97840c8SMike Christie 	if (ret < 0) {
2117c97840c8SMike Christie 		pr_err("match_int() failed for max_data_area_mb=. Error %d.\n",
2118c97840c8SMike Christie 		       ret);
2119c97840c8SMike Christie 		return ret;
2120c97840c8SMike Christie 	}
2121c97840c8SMike Christie 
2122c97840c8SMike Christie 	if (val <= 0) {
2123c97840c8SMike Christie 		pr_err("Invalid max_data_area %d.\n", val);
2124c97840c8SMike Christie 		return -EINVAL;
2125c97840c8SMike Christie 	}
2126c97840c8SMike Christie 
2127c97840c8SMike Christie 	mutex_lock(&udev->cmdr_lock);
2128c97840c8SMike Christie 	if (udev->data_bitmap) {
2129c97840c8SMike Christie 		pr_err("Cannot set max_data_area_mb after it has been enabled.\n");
2130c97840c8SMike Christie 		ret = -EINVAL;
2131c97840c8SMike Christie 		goto unlock;
2132c97840c8SMike Christie 	}
2133c97840c8SMike Christie 
2134c97840c8SMike Christie 	udev->max_blocks = TCMU_MBS_TO_BLOCKS(val);
2135c97840c8SMike Christie 	if (udev->max_blocks > tcmu_global_max_blocks) {
2136c97840c8SMike Christie 		pr_err("%d is too large. Adjusting max_data_area_mb to global limit of %u\n",
2137c97840c8SMike Christie 		       val, TCMU_BLOCKS_TO_MBS(tcmu_global_max_blocks));
2138c97840c8SMike Christie 		udev->max_blocks = tcmu_global_max_blocks;
2139c97840c8SMike Christie 	}
2140c97840c8SMike Christie 
2141c97840c8SMike Christie unlock:
2142c97840c8SMike Christie 	mutex_unlock(&udev->cmdr_lock);
2143c97840c8SMike Christie 	return ret;
2144c97840c8SMike Christie }
2145c97840c8SMike Christie 
21467c9e7a6fSAndy Grover static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev,
21477c9e7a6fSAndy Grover 		const char *page, ssize_t count)
21487c9e7a6fSAndy Grover {
21497c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
21500e0d7526SMike Christie 	char *orig, *ptr, *opts;
21517c9e7a6fSAndy Grover 	substring_t args[MAX_OPT_ARGS];
2152c97840c8SMike Christie 	int ret = 0, token;
21537c9e7a6fSAndy Grover 
21547c9e7a6fSAndy Grover 	opts = kstrdup(page, GFP_KERNEL);
21557c9e7a6fSAndy Grover 	if (!opts)
21567c9e7a6fSAndy Grover 		return -ENOMEM;
21577c9e7a6fSAndy Grover 
21587c9e7a6fSAndy Grover 	orig = opts;
21597c9e7a6fSAndy Grover 
21607c9e7a6fSAndy Grover 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
21617c9e7a6fSAndy Grover 		if (!*ptr)
21627c9e7a6fSAndy Grover 			continue;
21637c9e7a6fSAndy Grover 
21647c9e7a6fSAndy Grover 		token = match_token(ptr, tokens, args);
21657c9e7a6fSAndy Grover 		switch (token) {
21667c9e7a6fSAndy Grover 		case Opt_dev_config:
21677c9e7a6fSAndy Grover 			if (match_strlcpy(udev->dev_config, &args[0],
21687c9e7a6fSAndy Grover 					  TCMU_CONFIG_LEN) == 0) {
21697c9e7a6fSAndy Grover 				ret = -EINVAL;
21707c9e7a6fSAndy Grover 				break;
21717c9e7a6fSAndy Grover 			}
21727c9e7a6fSAndy Grover 			pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config);
21737c9e7a6fSAndy Grover 			break;
21747c9e7a6fSAndy Grover 		case Opt_dev_size:
21750e0d7526SMike Christie 			ret = match_u64(&args[0], &udev->dev_size);
21767c9e7a6fSAndy Grover 			if (ret < 0)
21770e0d7526SMike Christie 				pr_err("match_u64() failed for dev_size=. Error %d.\n",
21780e0d7526SMike Christie 				       ret);
21797c9e7a6fSAndy Grover 			break;
21809c1cd1b6SAndy Grover 		case Opt_hw_block_size:
21813abaa2bfSMike Christie 			ret = tcmu_set_dev_attrib(&args[0],
21823abaa2bfSMike Christie 					&(dev->dev_attrib.hw_block_size));
21839c1cd1b6SAndy Grover 			break;
21843abaa2bfSMike Christie 		case Opt_hw_max_sectors:
21853abaa2bfSMike Christie 			ret = tcmu_set_dev_attrib(&args[0],
21863abaa2bfSMike Christie 					&(dev->dev_attrib.hw_max_sectors));
21879c1cd1b6SAndy Grover 			break;
2188b849b456SKenjiro Nakayama 		case Opt_nl_reply_supported:
2189b60cb1f8SMike Christie 			ret = match_int(&args[0], &udev->nl_reply_supported);
2190b849b456SKenjiro Nakayama 			if (ret < 0)
2191b60cb1f8SMike Christie 				pr_err("match_int() failed for nl_reply_supported=. Error %d.\n",
2192b60cb1f8SMike Christie 				       ret);
2193b849b456SKenjiro Nakayama 			break;
219480eb8761SMike Christie 		case Opt_max_data_area_mb:
2195c97840c8SMike Christie 			ret = tcmu_set_max_blocks_param(udev, &args[0]);
219680eb8761SMike Christie 			break;
21977c9e7a6fSAndy Grover 		default:
21987c9e7a6fSAndy Grover 			break;
21997c9e7a6fSAndy Grover 		}
22002579325cSMike Christie 
22012579325cSMike Christie 		if (ret)
22022579325cSMike Christie 			break;
22037c9e7a6fSAndy Grover 	}
22047c9e7a6fSAndy Grover 
22057c9e7a6fSAndy Grover 	kfree(orig);
22067c9e7a6fSAndy Grover 	return (!ret) ? count : ret;
22077c9e7a6fSAndy Grover }
22087c9e7a6fSAndy Grover 
22097c9e7a6fSAndy Grover static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b)
22107c9e7a6fSAndy Grover {
22117c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
22127c9e7a6fSAndy Grover 	ssize_t bl = 0;
22137c9e7a6fSAndy Grover 
22147c9e7a6fSAndy Grover 	bl = sprintf(b + bl, "Config: %s ",
22157c9e7a6fSAndy Grover 		     udev->dev_config[0] ? udev->dev_config : "NULL");
22160e0d7526SMike Christie 	bl += sprintf(b + bl, "Size: %llu ", udev->dev_size);
221780eb8761SMike Christie 	bl += sprintf(b + bl, "MaxDataAreaMB: %u\n",
221880eb8761SMike Christie 		      TCMU_BLOCKS_TO_MBS(udev->max_blocks));
22197c9e7a6fSAndy Grover 
22207c9e7a6fSAndy Grover 	return bl;
22217c9e7a6fSAndy Grover }
22227c9e7a6fSAndy Grover 
22237c9e7a6fSAndy Grover static sector_t tcmu_get_blocks(struct se_device *dev)
22247c9e7a6fSAndy Grover {
22257c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
22267c9e7a6fSAndy Grover 
22277c9e7a6fSAndy Grover 	return div_u64(udev->dev_size - dev->dev_attrib.block_size,
22287c9e7a6fSAndy Grover 		       dev->dev_attrib.block_size);
22297c9e7a6fSAndy Grover }
22307c9e7a6fSAndy Grover 
22317c9e7a6fSAndy Grover static sense_reason_t
22327c9e7a6fSAndy Grover tcmu_parse_cdb(struct se_cmd *cmd)
22337c9e7a6fSAndy Grover {
223402eb924fSAndy Grover 	return passthrough_parse_cdb(cmd, tcmu_queue_cmd);
22359c1cd1b6SAndy Grover }
22369c1cd1b6SAndy Grover 
22377d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page)
22387d7a7435SNicholas Bellinger {
22397d7a7435SNicholas Bellinger 	struct se_dev_attrib *da = container_of(to_config_group(item),
22407d7a7435SNicholas Bellinger 					struct se_dev_attrib, da_group);
2241b5ab697cSKenjiro Nakayama 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
22427d7a7435SNicholas Bellinger 
22437d7a7435SNicholas Bellinger 	return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC);
22447d7a7435SNicholas Bellinger }
22457d7a7435SNicholas Bellinger 
22467d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page,
22477d7a7435SNicholas Bellinger 				       size_t count)
22487d7a7435SNicholas Bellinger {
22497d7a7435SNicholas Bellinger 	struct se_dev_attrib *da = container_of(to_config_group(item),
22507d7a7435SNicholas Bellinger 					struct se_dev_attrib, da_group);
22517d7a7435SNicholas Bellinger 	struct tcmu_dev *udev = container_of(da->da_dev,
22527d7a7435SNicholas Bellinger 					struct tcmu_dev, se_dev);
22537d7a7435SNicholas Bellinger 	u32 val;
22547d7a7435SNicholas Bellinger 	int ret;
22557d7a7435SNicholas Bellinger 
22567d7a7435SNicholas Bellinger 	if (da->da_dev->export_count) {
22577d7a7435SNicholas Bellinger 		pr_err("Unable to set tcmu cmd_time_out while exports exist\n");
22587d7a7435SNicholas Bellinger 		return -EINVAL;
22597d7a7435SNicholas Bellinger 	}
22607d7a7435SNicholas Bellinger 
22617d7a7435SNicholas Bellinger 	ret = kstrtou32(page, 0, &val);
22627d7a7435SNicholas Bellinger 	if (ret < 0)
22637d7a7435SNicholas Bellinger 		return ret;
22647d7a7435SNicholas Bellinger 
22657d7a7435SNicholas Bellinger 	udev->cmd_time_out = val * MSEC_PER_SEC;
22667d7a7435SNicholas Bellinger 	return count;
22677d7a7435SNicholas Bellinger }
22687d7a7435SNicholas Bellinger CONFIGFS_ATTR(tcmu_, cmd_time_out);
22697d7a7435SNicholas Bellinger 
22709103575aSMike Christie static ssize_t tcmu_qfull_time_out_show(struct config_item *item, char *page)
22719103575aSMike Christie {
22729103575aSMike Christie 	struct se_dev_attrib *da = container_of(to_config_group(item),
22739103575aSMike Christie 						struct se_dev_attrib, da_group);
22749103575aSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
22759103575aSMike Christie 
22769103575aSMike Christie 	return snprintf(page, PAGE_SIZE, "%ld\n", udev->qfull_time_out <= 0 ?
22779103575aSMike Christie 			udev->qfull_time_out :
22789103575aSMike Christie 			udev->qfull_time_out / MSEC_PER_SEC);
22799103575aSMike Christie }
22809103575aSMike Christie 
22819103575aSMike Christie static ssize_t tcmu_qfull_time_out_store(struct config_item *item,
22829103575aSMike Christie 					 const char *page, size_t count)
22839103575aSMike Christie {
22849103575aSMike Christie 	struct se_dev_attrib *da = container_of(to_config_group(item),
22859103575aSMike Christie 					struct se_dev_attrib, da_group);
22869103575aSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
22879103575aSMike Christie 	s32 val;
22889103575aSMike Christie 	int ret;
22899103575aSMike Christie 
22909103575aSMike Christie 	ret = kstrtos32(page, 0, &val);
22919103575aSMike Christie 	if (ret < 0)
22929103575aSMike Christie 		return ret;
22939103575aSMike Christie 
22949103575aSMike Christie 	if (val >= 0) {
22959103575aSMike Christie 		udev->qfull_time_out = val * MSEC_PER_SEC;
2296125966dbSPrasanna Kumar Kalever 	} else if (val == -1) {
2297125966dbSPrasanna Kumar Kalever 		udev->qfull_time_out = val;
22989103575aSMike Christie 	} else {
22999103575aSMike Christie 		printk(KERN_ERR "Invalid qfull timeout value %d\n", val);
23009103575aSMike Christie 		return -EINVAL;
23019103575aSMike Christie 	}
23029103575aSMike Christie 	return count;
23039103575aSMike Christie }
23049103575aSMike Christie CONFIGFS_ATTR(tcmu_, qfull_time_out);
23059103575aSMike Christie 
230680eb8761SMike Christie static ssize_t tcmu_max_data_area_mb_show(struct config_item *item, char *page)
230780eb8761SMike Christie {
230880eb8761SMike Christie 	struct se_dev_attrib *da = container_of(to_config_group(item),
230980eb8761SMike Christie 						struct se_dev_attrib, da_group);
231080eb8761SMike Christie 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
231180eb8761SMike Christie 
231280eb8761SMike Christie 	return snprintf(page, PAGE_SIZE, "%u\n",
231380eb8761SMike Christie 			TCMU_BLOCKS_TO_MBS(udev->max_blocks));
231480eb8761SMike Christie }
231580eb8761SMike Christie CONFIGFS_ATTR_RO(tcmu_, max_data_area_mb);
231680eb8761SMike Christie 
23172d76443eSMike Christie static ssize_t tcmu_dev_config_show(struct config_item *item, char *page)
2318ee018252SBryant G. Ly {
2319ee018252SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
2320ee018252SBryant G. Ly 						struct se_dev_attrib, da_group);
2321ee018252SBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2322ee018252SBryant G. Ly 
2323ee018252SBryant G. Ly 	return snprintf(page, PAGE_SIZE, "%s\n", udev->dev_config);
2324ee018252SBryant G. Ly }
2325ee018252SBryant G. Ly 
232602ccfb54SZhu Lingshan static int tcmu_send_dev_config_event(struct tcmu_dev *udev,
232702ccfb54SZhu Lingshan 				      const char *reconfig_data)
232802ccfb54SZhu Lingshan {
232902ccfb54SZhu Lingshan 	struct sk_buff *skb = NULL;
233002ccfb54SZhu Lingshan 	void *msg_header = NULL;
233102ccfb54SZhu Lingshan 	int ret = 0;
233202ccfb54SZhu Lingshan 
233302ccfb54SZhu Lingshan 	ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE,
233402ccfb54SZhu Lingshan 				      &skb, &msg_header);
233502ccfb54SZhu Lingshan 	if (ret < 0)
233602ccfb54SZhu Lingshan 		return ret;
233702ccfb54SZhu Lingshan 	ret = nla_put_string(skb, TCMU_ATTR_DEV_CFG, reconfig_data);
233802ccfb54SZhu Lingshan 	if (ret < 0) {
233902ccfb54SZhu Lingshan 		nlmsg_free(skb);
234002ccfb54SZhu Lingshan 		return ret;
234102ccfb54SZhu Lingshan 	}
234202ccfb54SZhu Lingshan 	return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE,
234306add777SMike Christie 				       skb, msg_header);
234402ccfb54SZhu Lingshan }
234502ccfb54SZhu Lingshan 
234602ccfb54SZhu Lingshan 
23472d76443eSMike Christie static ssize_t tcmu_dev_config_store(struct config_item *item, const char *page,
2348ee018252SBryant G. Ly 				     size_t count)
2349ee018252SBryant G. Ly {
2350ee018252SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
2351ee018252SBryant G. Ly 						struct se_dev_attrib, da_group);
2352ee018252SBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
23532d76443eSMike Christie 	int ret, len;
2354ee018252SBryant G. Ly 
23552d76443eSMike Christie 	len = strlen(page);
23562d76443eSMike Christie 	if (!len || len > TCMU_CONFIG_LEN - 1)
2357ee018252SBryant G. Ly 		return -EINVAL;
2358ee018252SBryant G. Ly 
2359ee018252SBryant G. Ly 	/* Check if device has been configured before */
236063d5be0fSMike Christie 	if (target_dev_configured(&udev->se_dev)) {
236102ccfb54SZhu Lingshan 		ret = tcmu_send_dev_config_event(udev, page);
2362ee018252SBryant G. Ly 		if (ret) {
2363ee018252SBryant G. Ly 			pr_err("Unable to reconfigure device\n");
2364ee018252SBryant G. Ly 			return ret;
2365ee018252SBryant G. Ly 		}
2366de8c5221SBryant G. Ly 		strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN);
2367de8c5221SBryant G. Ly 
2368de8c5221SBryant G. Ly 		ret = tcmu_update_uio_info(udev);
2369de8c5221SBryant G. Ly 		if (ret)
2370de8c5221SBryant G. Ly 			return ret;
2371de8c5221SBryant G. Ly 		return count;
2372ee018252SBryant G. Ly 	}
23732d76443eSMike Christie 	strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN);
2374ee018252SBryant G. Ly 
2375ee018252SBryant G. Ly 	return count;
2376ee018252SBryant G. Ly }
23772d76443eSMike Christie CONFIGFS_ATTR(tcmu_, dev_config);
2378ee018252SBryant G. Ly 
2379801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_show(struct config_item *item, char *page)
2380801fc54dSBryant G. Ly {
2381801fc54dSBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
2382801fc54dSBryant G. Ly 						struct se_dev_attrib, da_group);
2383801fc54dSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2384801fc54dSBryant G. Ly 
23850e0d7526SMike Christie 	return snprintf(page, PAGE_SIZE, "%llu\n", udev->dev_size);
2386801fc54dSBryant G. Ly }
2387801fc54dSBryant G. Ly 
238884e28506SZhu Lingshan static int tcmu_send_dev_size_event(struct tcmu_dev *udev, u64 size)
238984e28506SZhu Lingshan {
239084e28506SZhu Lingshan 	struct sk_buff *skb = NULL;
239184e28506SZhu Lingshan 	void *msg_header = NULL;
239284e28506SZhu Lingshan 	int ret = 0;
239384e28506SZhu Lingshan 
239484e28506SZhu Lingshan 	ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE,
239584e28506SZhu Lingshan 				      &skb, &msg_header);
239684e28506SZhu Lingshan 	if (ret < 0)
239784e28506SZhu Lingshan 		return ret;
239884e28506SZhu Lingshan 	ret = nla_put_u64_64bit(skb, TCMU_ATTR_DEV_SIZE,
239984e28506SZhu Lingshan 				size, TCMU_ATTR_PAD);
240084e28506SZhu Lingshan 	if (ret < 0) {
240184e28506SZhu Lingshan 		nlmsg_free(skb);
240284e28506SZhu Lingshan 		return ret;
240384e28506SZhu Lingshan 	}
240484e28506SZhu Lingshan 	return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE,
240506add777SMike Christie 				       skb, msg_header);
240684e28506SZhu Lingshan }
240784e28506SZhu Lingshan 
2408801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page,
2409801fc54dSBryant G. Ly 				   size_t count)
2410801fc54dSBryant G. Ly {
2411801fc54dSBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
2412801fc54dSBryant G. Ly 						struct se_dev_attrib, da_group);
2413801fc54dSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
24142d76443eSMike Christie 	u64 val;
2415801fc54dSBryant G. Ly 	int ret;
2416801fc54dSBryant G. Ly 
24172d76443eSMike Christie 	ret = kstrtou64(page, 0, &val);
2418801fc54dSBryant G. Ly 	if (ret < 0)
2419801fc54dSBryant G. Ly 		return ret;
2420801fc54dSBryant G. Ly 
2421801fc54dSBryant G. Ly 	/* Check if device has been configured before */
242263d5be0fSMike Christie 	if (target_dev_configured(&udev->se_dev)) {
242384e28506SZhu Lingshan 		ret = tcmu_send_dev_size_event(udev, val);
2424801fc54dSBryant G. Ly 		if (ret) {
2425801fc54dSBryant G. Ly 			pr_err("Unable to reconfigure device\n");
2426801fc54dSBryant G. Ly 			return ret;
2427801fc54dSBryant G. Ly 		}
2428801fc54dSBryant G. Ly 	}
24292d76443eSMike Christie 	udev->dev_size = val;
2430801fc54dSBryant G. Ly 	return count;
2431801fc54dSBryant G. Ly }
2432801fc54dSBryant G. Ly CONFIGFS_ATTR(tcmu_, dev_size);
2433801fc54dSBryant G. Ly 
2434b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_show(struct config_item *item,
2435b849b456SKenjiro Nakayama 		char *page)
2436b849b456SKenjiro Nakayama {
2437b849b456SKenjiro Nakayama 	struct se_dev_attrib *da = container_of(to_config_group(item),
2438b849b456SKenjiro Nakayama 						struct se_dev_attrib, da_group);
2439b849b456SKenjiro Nakayama 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2440b849b456SKenjiro Nakayama 
2441b849b456SKenjiro Nakayama 	return snprintf(page, PAGE_SIZE, "%d\n", udev->nl_reply_supported);
2442b849b456SKenjiro Nakayama }
2443b849b456SKenjiro Nakayama 
2444b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_store(struct config_item *item,
2445b849b456SKenjiro Nakayama 		const char *page, size_t count)
2446b849b456SKenjiro Nakayama {
2447b849b456SKenjiro Nakayama 	struct se_dev_attrib *da = container_of(to_config_group(item),
2448b849b456SKenjiro Nakayama 						struct se_dev_attrib, da_group);
2449b849b456SKenjiro Nakayama 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2450b849b456SKenjiro Nakayama 	s8 val;
2451b849b456SKenjiro Nakayama 	int ret;
2452b849b456SKenjiro Nakayama 
2453b849b456SKenjiro Nakayama 	ret = kstrtos8(page, 0, &val);
2454b849b456SKenjiro Nakayama 	if (ret < 0)
2455b849b456SKenjiro Nakayama 		return ret;
2456b849b456SKenjiro Nakayama 
2457b849b456SKenjiro Nakayama 	udev->nl_reply_supported = val;
2458b849b456SKenjiro Nakayama 	return count;
2459b849b456SKenjiro Nakayama }
2460b849b456SKenjiro Nakayama CONFIGFS_ATTR(tcmu_, nl_reply_supported);
2461b849b456SKenjiro Nakayama 
24629a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_show(struct config_item *item,
24639a8bb606SBryant G. Ly 					     char *page)
24649a8bb606SBryant G. Ly {
24659a8bb606SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
24669a8bb606SBryant G. Ly 					struct se_dev_attrib, da_group);
24679a8bb606SBryant G. Ly 
24689a8bb606SBryant G. Ly 	return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache);
24699a8bb606SBryant G. Ly }
24709a8bb606SBryant G. Ly 
247133d065ccSZhu Lingshan static int tcmu_send_emulate_write_cache(struct tcmu_dev *udev, u8 val)
247233d065ccSZhu Lingshan {
247333d065ccSZhu Lingshan 	struct sk_buff *skb = NULL;
247433d065ccSZhu Lingshan 	void *msg_header = NULL;
247533d065ccSZhu Lingshan 	int ret = 0;
247633d065ccSZhu Lingshan 
247733d065ccSZhu Lingshan 	ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE,
247833d065ccSZhu Lingshan 				      &skb, &msg_header);
247933d065ccSZhu Lingshan 	if (ret < 0)
248033d065ccSZhu Lingshan 		return ret;
248133d065ccSZhu Lingshan 	ret = nla_put_u8(skb, TCMU_ATTR_WRITECACHE, val);
248233d065ccSZhu Lingshan 	if (ret < 0) {
248333d065ccSZhu Lingshan 		nlmsg_free(skb);
248433d065ccSZhu Lingshan 		return ret;
248533d065ccSZhu Lingshan 	}
248633d065ccSZhu Lingshan 	return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE,
248706add777SMike Christie 				       skb, msg_header);
248833d065ccSZhu Lingshan }
248933d065ccSZhu Lingshan 
24909a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_store(struct config_item *item,
24919a8bb606SBryant G. Ly 					      const char *page, size_t count)
24929a8bb606SBryant G. Ly {
24939a8bb606SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
24949a8bb606SBryant G. Ly 					struct se_dev_attrib, da_group);
24951068be7bSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
24962d76443eSMike Christie 	u8 val;
24979a8bb606SBryant G. Ly 	int ret;
24989a8bb606SBryant G. Ly 
24992d76443eSMike Christie 	ret = kstrtou8(page, 0, &val);
25009a8bb606SBryant G. Ly 	if (ret < 0)
25019a8bb606SBryant G. Ly 		return ret;
25029a8bb606SBryant G. Ly 
25031068be7bSBryant G. Ly 	/* Check if device has been configured before */
250463d5be0fSMike Christie 	if (target_dev_configured(&udev->se_dev)) {
250533d065ccSZhu Lingshan 		ret = tcmu_send_emulate_write_cache(udev, val);
25061068be7bSBryant G. Ly 		if (ret) {
25071068be7bSBryant G. Ly 			pr_err("Unable to reconfigure device\n");
25081068be7bSBryant G. Ly 			return ret;
25091068be7bSBryant G. Ly 		}
25101068be7bSBryant G. Ly 	}
25112d76443eSMike Christie 
25122d76443eSMike Christie 	da->emulate_write_cache = val;
25139a8bb606SBryant G. Ly 	return count;
25149a8bb606SBryant G. Ly }
25159a8bb606SBryant G. Ly CONFIGFS_ATTR(tcmu_, emulate_write_cache);
25169a8bb606SBryant G. Ly 
2517892782caSMike Christie static ssize_t tcmu_block_dev_show(struct config_item *item, char *page)
2518892782caSMike Christie {
2519892782caSMike Christie 	struct se_device *se_dev = container_of(to_config_group(item),
2520892782caSMike Christie 						struct se_device,
2521892782caSMike Christie 						dev_action_group);
2522892782caSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
2523892782caSMike Christie 
2524892782caSMike Christie 	if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags))
2525892782caSMike Christie 		return snprintf(page, PAGE_SIZE, "%s\n", "blocked");
2526892782caSMike Christie 	else
2527892782caSMike Christie 		return snprintf(page, PAGE_SIZE, "%s\n", "unblocked");
2528892782caSMike Christie }
2529892782caSMike Christie 
2530892782caSMike Christie static ssize_t tcmu_block_dev_store(struct config_item *item, const char *page,
2531892782caSMike Christie 				    size_t count)
2532892782caSMike Christie {
2533892782caSMike Christie 	struct se_device *se_dev = container_of(to_config_group(item),
2534892782caSMike Christie 						struct se_device,
2535892782caSMike Christie 						dev_action_group);
2536892782caSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
2537892782caSMike Christie 	u8 val;
2538892782caSMike Christie 	int ret;
2539892782caSMike Christie 
2540a30b0473SMike Christie 	if (!target_dev_configured(&udev->se_dev)) {
2541a30b0473SMike Christie 		pr_err("Device is not configured.\n");
2542a30b0473SMike Christie 		return -EINVAL;
2543a30b0473SMike Christie 	}
2544a30b0473SMike Christie 
2545892782caSMike Christie 	ret = kstrtou8(page, 0, &val);
2546892782caSMike Christie 	if (ret < 0)
2547892782caSMike Christie 		return ret;
2548892782caSMike Christie 
2549892782caSMike Christie 	if (val > 1) {
2550892782caSMike Christie 		pr_err("Invalid block value %d\n", val);
2551892782caSMike Christie 		return -EINVAL;
2552892782caSMike Christie 	}
2553892782caSMike Christie 
2554892782caSMike Christie 	if (!val)
2555892782caSMike Christie 		tcmu_unblock_dev(udev);
2556892782caSMike Christie 	else
2557892782caSMike Christie 		tcmu_block_dev(udev);
2558892782caSMike Christie 	return count;
2559892782caSMike Christie }
2560892782caSMike Christie CONFIGFS_ATTR(tcmu_, block_dev);
2561892782caSMike Christie 
2562892782caSMike Christie static ssize_t tcmu_reset_ring_store(struct config_item *item, const char *page,
2563892782caSMike Christie 				     size_t count)
2564892782caSMike Christie {
2565892782caSMike Christie 	struct se_device *se_dev = container_of(to_config_group(item),
2566892782caSMike Christie 						struct se_device,
2567892782caSMike Christie 						dev_action_group);
2568892782caSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
2569892782caSMike Christie 	u8 val;
2570892782caSMike Christie 	int ret;
2571892782caSMike Christie 
2572a30b0473SMike Christie 	if (!target_dev_configured(&udev->se_dev)) {
2573a30b0473SMike Christie 		pr_err("Device is not configured.\n");
2574a30b0473SMike Christie 		return -EINVAL;
2575a30b0473SMike Christie 	}
2576a30b0473SMike Christie 
2577892782caSMike Christie 	ret = kstrtou8(page, 0, &val);
2578892782caSMike Christie 	if (ret < 0)
2579892782caSMike Christie 		return ret;
2580892782caSMike Christie 
2581892782caSMike Christie 	if (val != 1 && val != 2) {
2582892782caSMike Christie 		pr_err("Invalid reset ring value %d\n", val);
2583892782caSMike Christie 		return -EINVAL;
2584892782caSMike Christie 	}
2585892782caSMike Christie 
2586892782caSMike Christie 	tcmu_reset_ring(udev, val);
2587892782caSMike Christie 	return count;
2588892782caSMike Christie }
2589892782caSMike Christie CONFIGFS_ATTR_WO(tcmu_, reset_ring);
2590892782caSMike Christie 
25915821783bSColin Ian King static struct configfs_attribute *tcmu_attrib_attrs[] = {
2592801fc54dSBryant G. Ly 	&tcmu_attr_cmd_time_out,
25939103575aSMike Christie 	&tcmu_attr_qfull_time_out,
259480eb8761SMike Christie 	&tcmu_attr_max_data_area_mb,
25952d76443eSMike Christie 	&tcmu_attr_dev_config,
2596801fc54dSBryant G. Ly 	&tcmu_attr_dev_size,
2597801fc54dSBryant G. Ly 	&tcmu_attr_emulate_write_cache,
2598b849b456SKenjiro Nakayama 	&tcmu_attr_nl_reply_supported,
2599801fc54dSBryant G. Ly 	NULL,
2600801fc54dSBryant G. Ly };
2601801fc54dSBryant G. Ly 
26027d7a7435SNicholas Bellinger static struct configfs_attribute **tcmu_attrs;
26037d7a7435SNicholas Bellinger 
2604892782caSMike Christie static struct configfs_attribute *tcmu_action_attrs[] = {
2605892782caSMike Christie 	&tcmu_attr_block_dev,
2606892782caSMike Christie 	&tcmu_attr_reset_ring,
2607892782caSMike Christie 	NULL,
2608892782caSMike Christie };
2609892782caSMike Christie 
26107d7a7435SNicholas Bellinger static struct target_backend_ops tcmu_ops = {
26117c9e7a6fSAndy Grover 	.name			= "user",
26127c9e7a6fSAndy Grover 	.owner			= THIS_MODULE,
261369088a04SBodo Stroesser 	.transport_flags_default = TRANSPORT_FLAG_PASSTHROUGH,
2614356ba2a8SBodo Stroesser 	.transport_flags_changeable = TRANSPORT_FLAG_PASSTHROUGH_PGR |
2615356ba2a8SBodo Stroesser 				      TRANSPORT_FLAG_PASSTHROUGH_ALUA,
26167c9e7a6fSAndy Grover 	.attach_hba		= tcmu_attach_hba,
26177c9e7a6fSAndy Grover 	.detach_hba		= tcmu_detach_hba,
26187c9e7a6fSAndy Grover 	.alloc_device		= tcmu_alloc_device,
26197c9e7a6fSAndy Grover 	.configure_device	= tcmu_configure_device,
262092634706SMike Christie 	.destroy_device		= tcmu_destroy_device,
26217c9e7a6fSAndy Grover 	.free_device		= tcmu_free_device,
26227c9e7a6fSAndy Grover 	.parse_cdb		= tcmu_parse_cdb,
26237c9e7a6fSAndy Grover 	.set_configfs_dev_params = tcmu_set_configfs_dev_params,
26247c9e7a6fSAndy Grover 	.show_configfs_dev_params = tcmu_show_configfs_dev_params,
26257c9e7a6fSAndy Grover 	.get_device_type	= sbc_get_device_type,
26267c9e7a6fSAndy Grover 	.get_blocks		= tcmu_get_blocks,
2627892782caSMike Christie 	.tb_dev_action_attrs	= tcmu_action_attrs,
26287c9e7a6fSAndy Grover };
26297c9e7a6fSAndy Grover 
263089ec9cfdSMike Christie static void find_free_blocks(void)
2631b6df4b79SXiubo Li {
2632b6df4b79SXiubo Li 	struct tcmu_dev *udev;
2633b6df4b79SXiubo Li 	loff_t off;
2634af1dd7ffSMike Christie 	u32 start, end, block, total_freed = 0;
2635af1dd7ffSMike Christie 
263680eb8761SMike Christie 	if (atomic_read(&global_db_count) <= tcmu_global_max_blocks)
2637af1dd7ffSMike Christie 		return;
2638b6df4b79SXiubo Li 
2639b6df4b79SXiubo Li 	mutex_lock(&root_udev_mutex);
2640b6df4b79SXiubo Li 	list_for_each_entry(udev, &root_udev, node) {
2641b6df4b79SXiubo Li 		mutex_lock(&udev->cmdr_lock);
2642b6df4b79SXiubo Li 
2643dc335a99SMike Christie 		if (!target_dev_configured(&udev->se_dev)) {
2644dc335a99SMike Christie 			mutex_unlock(&udev->cmdr_lock);
2645dc335a99SMike Christie 			continue;
2646dc335a99SMike Christie 		}
2647dc335a99SMike Christie 
2648b6df4b79SXiubo Li 		/* Try to complete the finished commands first */
2649b6df4b79SXiubo Li 		tcmu_handle_completions(udev);
2650b6df4b79SXiubo Li 
2651af1dd7ffSMike Christie 		/* Skip the udevs in idle */
2652af1dd7ffSMike Christie 		if (!udev->dbi_thresh) {
2653b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
2654b6df4b79SXiubo Li 			continue;
2655b6df4b79SXiubo Li 		}
2656b6df4b79SXiubo Li 
2657b6df4b79SXiubo Li 		end = udev->dbi_max + 1;
2658b6df4b79SXiubo Li 		block = find_last_bit(udev->data_bitmap, end);
2659b6df4b79SXiubo Li 		if (block == udev->dbi_max) {
2660b6df4b79SXiubo Li 			/*
2661af1dd7ffSMike Christie 			 * The last bit is dbi_max, so it is not possible
2662af1dd7ffSMike Christie 			 * reclaim any blocks.
2663b6df4b79SXiubo Li 			 */
2664b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
2665b6df4b79SXiubo Li 			continue;
2666b6df4b79SXiubo Li 		} else if (block == end) {
2667b6df4b79SXiubo Li 			/* The current udev will goto idle state */
2668b6df4b79SXiubo Li 			udev->dbi_thresh = start = 0;
2669b6df4b79SXiubo Li 			udev->dbi_max = 0;
2670b6df4b79SXiubo Li 		} else {
2671b6df4b79SXiubo Li 			udev->dbi_thresh = start = block + 1;
2672b6df4b79SXiubo Li 			udev->dbi_max = block;
2673b6df4b79SXiubo Li 		}
2674b6df4b79SXiubo Li 
2675b6df4b79SXiubo Li 		/* Here will truncate the data area from off */
2676b6df4b79SXiubo Li 		off = udev->data_off + start * DATA_BLOCK_SIZE;
2677b6df4b79SXiubo Li 		unmap_mapping_range(udev->inode->i_mapping, off, 0, 1);
2678b6df4b79SXiubo Li 
2679b6df4b79SXiubo Li 		/* Release the block pages */
2680bf99ec13SMike Christie 		tcmu_blocks_release(&udev->data_blocks, start, end);
2681b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
2682af1dd7ffSMike Christie 
2683af1dd7ffSMike Christie 		total_freed += end - start;
2684af1dd7ffSMike Christie 		pr_debug("Freed %u blocks (total %u) from %s.\n", end - start,
2685af1dd7ffSMike Christie 			 total_freed, udev->name);
2686b6df4b79SXiubo Li 	}
268789ec9cfdSMike Christie 	mutex_unlock(&root_udev_mutex);
268889ec9cfdSMike Christie 
268980eb8761SMike Christie 	if (atomic_read(&global_db_count) > tcmu_global_max_blocks)
2690af1dd7ffSMike Christie 		schedule_delayed_work(&tcmu_unmap_work, msecs_to_jiffies(5000));
2691b6df4b79SXiubo Li }
2692b6df4b79SXiubo Li 
2693488ebe4cSMike Christie static void check_timedout_devices(void)
2694488ebe4cSMike Christie {
2695488ebe4cSMike Christie 	struct tcmu_dev *udev, *tmp_dev;
269661fb2482SBodo Stroesser 	struct tcmu_cmd *cmd, *tmp_cmd;
2697488ebe4cSMike Christie 	LIST_HEAD(devs);
2698488ebe4cSMike Christie 
2699488ebe4cSMike Christie 	spin_lock_bh(&timed_out_udevs_lock);
2700488ebe4cSMike Christie 	list_splice_init(&timed_out_udevs, &devs);
2701488ebe4cSMike Christie 
2702488ebe4cSMike Christie 	list_for_each_entry_safe(udev, tmp_dev, &devs, timedout_entry) {
2703488ebe4cSMike Christie 		list_del_init(&udev->timedout_entry);
2704488ebe4cSMike Christie 		spin_unlock_bh(&timed_out_udevs_lock);
2705488ebe4cSMike Christie 
27066fddcb77SMike Christie 		mutex_lock(&udev->cmdr_lock);
2707a94a2572SXiubo Li 
270861fb2482SBodo Stroesser 		/*
270961fb2482SBodo Stroesser 		 * If cmd_time_out is disabled but qfull is set deadline
271061fb2482SBodo Stroesser 		 * will only reflect the qfull timeout. Ignore it.
271161fb2482SBodo Stroesser 		 */
271261fb2482SBodo Stroesser 		if (udev->cmd_time_out) {
271361fb2482SBodo Stroesser 			list_for_each_entry_safe(cmd, tmp_cmd,
271461fb2482SBodo Stroesser 						 &udev->inflight_queue,
271561fb2482SBodo Stroesser 						 queue_entry) {
271661fb2482SBodo Stroesser 				tcmu_check_expired_ring_cmd(cmd);
271761fb2482SBodo Stroesser 			}
271861fb2482SBodo Stroesser 			tcmu_set_next_deadline(&udev->inflight_queue,
271961fb2482SBodo Stroesser 					       &udev->cmd_timer);
272061fb2482SBodo Stroesser 		}
272161fb2482SBodo Stroesser 		list_for_each_entry_safe(cmd, tmp_cmd, &udev->qfull_queue,
272261fb2482SBodo Stroesser 					 queue_entry) {
272361fb2482SBodo Stroesser 			tcmu_check_expired_queue_cmd(cmd);
272461fb2482SBodo Stroesser 		}
2725a94a2572SXiubo Li 		tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer);
2726a94a2572SXiubo Li 
27276fddcb77SMike Christie 		mutex_unlock(&udev->cmdr_lock);
2728488ebe4cSMike Christie 
2729488ebe4cSMike Christie 		spin_lock_bh(&timed_out_udevs_lock);
2730488ebe4cSMike Christie 	}
2731488ebe4cSMike Christie 
2732488ebe4cSMike Christie 	spin_unlock_bh(&timed_out_udevs_lock);
2733488ebe4cSMike Christie }
2734488ebe4cSMike Christie 
27359972cebbSMike Christie static void tcmu_unmap_work_fn(struct work_struct *work)
273689ec9cfdSMike Christie {
2737488ebe4cSMike Christie 	check_timedout_devices();
273889ec9cfdSMike Christie 	find_free_blocks();
273989ec9cfdSMike Christie }
274089ec9cfdSMike Christie 
27417c9e7a6fSAndy Grover static int __init tcmu_module_init(void)
27427c9e7a6fSAndy Grover {
2743801fc54dSBryant G. Ly 	int ret, i, k, len = 0;
27447c9e7a6fSAndy Grover 
27457c9e7a6fSAndy Grover 	BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
27467c9e7a6fSAndy Grover 
2747af1dd7ffSMike Christie 	INIT_DELAYED_WORK(&tcmu_unmap_work, tcmu_unmap_work_fn);
27489972cebbSMike Christie 
27497c9e7a6fSAndy Grover 	tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
27507c9e7a6fSAndy Grover 				sizeof(struct tcmu_cmd),
27517c9e7a6fSAndy Grover 				__alignof__(struct tcmu_cmd),
27527c9e7a6fSAndy Grover 				0, NULL);
27537c9e7a6fSAndy Grover 	if (!tcmu_cmd_cache)
27547c9e7a6fSAndy Grover 		return -ENOMEM;
27557c9e7a6fSAndy Grover 
27567c9e7a6fSAndy Grover 	tcmu_root_device = root_device_register("tcm_user");
27577c9e7a6fSAndy Grover 	if (IS_ERR(tcmu_root_device)) {
27587c9e7a6fSAndy Grover 		ret = PTR_ERR(tcmu_root_device);
27597c9e7a6fSAndy Grover 		goto out_free_cache;
27607c9e7a6fSAndy Grover 	}
27617c9e7a6fSAndy Grover 
27627c9e7a6fSAndy Grover 	ret = genl_register_family(&tcmu_genl_family);
27637c9e7a6fSAndy Grover 	if (ret < 0) {
27647c9e7a6fSAndy Grover 		goto out_unreg_device;
27657c9e7a6fSAndy Grover 	}
27667c9e7a6fSAndy Grover 
27674703b625SBodo Stroesser 	for (i = 0; passthrough_attrib_attrs[i] != NULL; i++)
27687d7a7435SNicholas Bellinger 		len += sizeof(struct configfs_attribute *);
27694703b625SBodo Stroesser 	for (i = 0; passthrough_pr_attrib_attrs[i] != NULL; i++)
2770801fc54dSBryant G. Ly 		len += sizeof(struct configfs_attribute *);
27714703b625SBodo Stroesser 	for (i = 0; tcmu_attrib_attrs[i] != NULL; i++)
27724703b625SBodo Stroesser 		len += sizeof(struct configfs_attribute *);
2773801fc54dSBryant G. Ly 	len += sizeof(struct configfs_attribute *);
27747d7a7435SNicholas Bellinger 
27757d7a7435SNicholas Bellinger 	tcmu_attrs = kzalloc(len, GFP_KERNEL);
27767d7a7435SNicholas Bellinger 	if (!tcmu_attrs) {
27777d7a7435SNicholas Bellinger 		ret = -ENOMEM;
27787d7a7435SNicholas Bellinger 		goto out_unreg_genl;
27797d7a7435SNicholas Bellinger 	}
27807d7a7435SNicholas Bellinger 
27814703b625SBodo Stroesser 	for (i = 0; passthrough_attrib_attrs[i] != NULL; i++)
27827d7a7435SNicholas Bellinger 		tcmu_attrs[i] = passthrough_attrib_attrs[i];
27834703b625SBodo Stroesser 	for (k = 0; passthrough_pr_attrib_attrs[k] != NULL; k++)
27844703b625SBodo Stroesser 		tcmu_attrs[i++] = passthrough_pr_attrib_attrs[k];
27854703b625SBodo Stroesser 	for (k = 0; tcmu_attrib_attrs[k] != NULL; k++)
27864703b625SBodo Stroesser 		tcmu_attrs[i++] = tcmu_attrib_attrs[k];
27877d7a7435SNicholas Bellinger 	tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs;
27887d7a7435SNicholas Bellinger 
27890a06d430SChristoph Hellwig 	ret = transport_backend_register(&tcmu_ops);
27907c9e7a6fSAndy Grover 	if (ret)
27917d7a7435SNicholas Bellinger 		goto out_attrs;
27927c9e7a6fSAndy Grover 
27937c9e7a6fSAndy Grover 	return 0;
27947c9e7a6fSAndy Grover 
27957d7a7435SNicholas Bellinger out_attrs:
27967d7a7435SNicholas Bellinger 	kfree(tcmu_attrs);
27977c9e7a6fSAndy Grover out_unreg_genl:
27987c9e7a6fSAndy Grover 	genl_unregister_family(&tcmu_genl_family);
27997c9e7a6fSAndy Grover out_unreg_device:
28007c9e7a6fSAndy Grover 	root_device_unregister(tcmu_root_device);
28017c9e7a6fSAndy Grover out_free_cache:
28027c9e7a6fSAndy Grover 	kmem_cache_destroy(tcmu_cmd_cache);
28037c9e7a6fSAndy Grover 
28047c9e7a6fSAndy Grover 	return ret;
28057c9e7a6fSAndy Grover }
28067c9e7a6fSAndy Grover 
28077c9e7a6fSAndy Grover static void __exit tcmu_module_exit(void)
28087c9e7a6fSAndy Grover {
2809af1dd7ffSMike Christie 	cancel_delayed_work_sync(&tcmu_unmap_work);
28100a06d430SChristoph Hellwig 	target_backend_unregister(&tcmu_ops);
28117d7a7435SNicholas Bellinger 	kfree(tcmu_attrs);
28127c9e7a6fSAndy Grover 	genl_unregister_family(&tcmu_genl_family);
28137c9e7a6fSAndy Grover 	root_device_unregister(tcmu_root_device);
28147c9e7a6fSAndy Grover 	kmem_cache_destroy(tcmu_cmd_cache);
28157c9e7a6fSAndy Grover }
28167c9e7a6fSAndy Grover 
28177c9e7a6fSAndy Grover MODULE_DESCRIPTION("TCM USER subsystem plugin");
28187c9e7a6fSAndy Grover MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
28197c9e7a6fSAndy Grover MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
28207c9e7a6fSAndy Grover MODULE_LICENSE("GPL");
28217c9e7a6fSAndy Grover 
28227c9e7a6fSAndy Grover module_init(tcmu_module_init);
28237c9e7a6fSAndy Grover module_exit(tcmu_module_exit);
2824