17c9e7a6fSAndy Grover /*
27c9e7a6fSAndy Grover  * Copyright (C) 2013 Shaohua Li <shli@kernel.org>
37c9e7a6fSAndy Grover  * Copyright (C) 2014 Red Hat, Inc.
4f97ec7dbSIlias Tsitsimpis  * Copyright (C) 2015 Arrikto, Inc.
5141685a3SXiubo Li  * Copyright (C) 2017 Chinamobile, Inc.
67c9e7a6fSAndy Grover  *
77c9e7a6fSAndy Grover  * This program is free software; you can redistribute it and/or modify it
87c9e7a6fSAndy Grover  * under the terms and conditions of the GNU General Public License,
97c9e7a6fSAndy Grover  * version 2, as published by the Free Software Foundation.
107c9e7a6fSAndy Grover  *
117c9e7a6fSAndy Grover  * This program is distributed in the hope it will be useful, but WITHOUT
127c9e7a6fSAndy Grover  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
137c9e7a6fSAndy Grover  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
147c9e7a6fSAndy Grover  * more details.
157c9e7a6fSAndy Grover  *
167c9e7a6fSAndy Grover  * You should have received a copy of the GNU General Public License along with
177c9e7a6fSAndy Grover  * this program; if not, write to the Free Software Foundation, Inc.,
187c9e7a6fSAndy Grover  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
197c9e7a6fSAndy Grover  */
207c9e7a6fSAndy Grover 
217c9e7a6fSAndy Grover #include <linux/spinlock.h>
227c9e7a6fSAndy Grover #include <linux/module.h>
237c9e7a6fSAndy Grover #include <linux/idr.h>
24ba929992SBart Van Assche #include <linux/kernel.h>
257c9e7a6fSAndy Grover #include <linux/timer.h>
267c9e7a6fSAndy Grover #include <linux/parser.h>
275538d294SDavid S. Miller #include <linux/vmalloc.h>
287c9e7a6fSAndy Grover #include <linux/uio_driver.h>
29141685a3SXiubo Li #include <linux/radix-tree.h>
30ac64a2ceSDavid Disseldorp #include <linux/stringify.h>
3126418649SSheng Yang #include <linux/bitops.h>
32f5045724SBart Van Assche #include <linux/highmem.h>
337d7a7435SNicholas Bellinger #include <linux/configfs.h>
34b6df4b79SXiubo Li #include <linux/mutex.h>
35b6df4b79SXiubo Li #include <linux/kthread.h>
367c9e7a6fSAndy Grover #include <net/genetlink.h>
37ba929992SBart Van Assche #include <scsi/scsi_common.h>
38ba929992SBart Van Assche #include <scsi/scsi_proto.h>
397c9e7a6fSAndy Grover #include <target/target_core_base.h>
407c9e7a6fSAndy Grover #include <target/target_core_fabric.h>
417c9e7a6fSAndy Grover #include <target/target_core_backend.h>
42e9f720d6SNicholas Bellinger 
437c9e7a6fSAndy Grover #include <linux/target_core_user.h>
447c9e7a6fSAndy Grover 
457c9e7a6fSAndy Grover /*
467c9e7a6fSAndy Grover  * Define a shared-memory interface for LIO to pass SCSI commands and
477c9e7a6fSAndy Grover  * data to userspace for processing. This is to allow backends that
487c9e7a6fSAndy Grover  * are too complex for in-kernel support to be possible.
497c9e7a6fSAndy Grover  *
507c9e7a6fSAndy Grover  * It uses the UIO framework to do a lot of the device-creation and
517c9e7a6fSAndy Grover  * introspection work for us.
527c9e7a6fSAndy Grover  *
537c9e7a6fSAndy Grover  * See the .h file for how the ring is laid out. Note that while the
547c9e7a6fSAndy Grover  * command ring is defined, the particulars of the data area are
557c9e7a6fSAndy Grover  * not. Offset values in the command entry point to other locations
567c9e7a6fSAndy Grover  * internal to the mmap()ed area. There is separate space outside the
577c9e7a6fSAndy Grover  * command ring for data buffers. This leaves maximum flexibility for
587c9e7a6fSAndy Grover  * moving buffer allocations, or even page flipping or other
597c9e7a6fSAndy Grover  * allocation techniques, without altering the command ring layout.
607c9e7a6fSAndy Grover  *
617c9e7a6fSAndy Grover  * SECURITY:
627c9e7a6fSAndy Grover  * The user process must be assumed to be malicious. There's no way to
637c9e7a6fSAndy Grover  * prevent it breaking the command ring protocol if it wants, but in
647c9e7a6fSAndy Grover  * order to prevent other issues we must only ever read *data* from
657c9e7a6fSAndy Grover  * the shared memory area, not offsets or sizes. This applies to
667c9e7a6fSAndy Grover  * command ring entries as well as the mailbox. Extra code needed for
677c9e7a6fSAndy Grover  * this may have a 'UAM' comment.
687c9e7a6fSAndy Grover  */
697c9e7a6fSAndy Grover 
707c9e7a6fSAndy Grover #define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
717c9e7a6fSAndy Grover 
72b6df4b79SXiubo Li /* For cmd area, the size is fixed 8MB */
73b6df4b79SXiubo Li #define CMDR_SIZE (8 * 1024 * 1024)
7426418649SSheng Yang 
75b6df4b79SXiubo Li /*
76b6df4b79SXiubo Li  * For data area, the block size is PAGE_SIZE and
77b6df4b79SXiubo Li  * the total size is 256K * PAGE_SIZE.
78b6df4b79SXiubo Li  */
79b6df4b79SXiubo Li #define DATA_BLOCK_SIZE PAGE_SIZE
80b6df4b79SXiubo Li #define DATA_BLOCK_BITS (256 * 1024)
8126418649SSheng Yang #define DATA_SIZE (DATA_BLOCK_BITS * DATA_BLOCK_SIZE)
82b6df4b79SXiubo Li #define DATA_BLOCK_INIT_BITS 128
837c9e7a6fSAndy Grover 
84b6df4b79SXiubo Li /* The total size of the ring is 8M + 256K * PAGE_SIZE */
857c9e7a6fSAndy Grover #define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE)
867c9e7a6fSAndy Grover 
87b6df4b79SXiubo Li /* Default maximum of the global data blocks(512K * PAGE_SIZE) */
88b6df4b79SXiubo Li #define TCMU_GLOBAL_MAX_BLOCKS (512 * 1024)
89b6df4b79SXiubo Li 
90b3af66e2SMike Christie static u8 tcmu_kern_cmd_reply_supported;
91b3af66e2SMike Christie 
927c9e7a6fSAndy Grover static struct device *tcmu_root_device;
937c9e7a6fSAndy Grover 
947c9e7a6fSAndy Grover struct tcmu_hba {
957c9e7a6fSAndy Grover 	u32 host_id;
967c9e7a6fSAndy Grover };
977c9e7a6fSAndy Grover 
987c9e7a6fSAndy Grover #define TCMU_CONFIG_LEN 256
997c9e7a6fSAndy Grover 
100b3af66e2SMike Christie struct tcmu_nl_cmd {
101b3af66e2SMike Christie 	/* wake up thread waiting for reply */
102b3af66e2SMike Christie 	struct completion complete;
103b3af66e2SMike Christie 	int cmd;
104b3af66e2SMike Christie 	int status;
105b3af66e2SMike Christie };
106b3af66e2SMike Christie 
1077c9e7a6fSAndy Grover struct tcmu_dev {
108b6df4b79SXiubo Li 	struct list_head node;
109f3cdbe39SMike Christie 	struct kref kref;
1107c9e7a6fSAndy Grover 	struct se_device se_dev;
1117c9e7a6fSAndy Grover 
1127c9e7a6fSAndy Grover 	char *name;
1137c9e7a6fSAndy Grover 	struct se_hba *hba;
1147c9e7a6fSAndy Grover 
1157c9e7a6fSAndy Grover #define TCMU_DEV_BIT_OPEN 0
1167c9e7a6fSAndy Grover #define TCMU_DEV_BIT_BROKEN 1
1177c9e7a6fSAndy Grover 	unsigned long flags;
1187c9e7a6fSAndy Grover 
1197c9e7a6fSAndy Grover 	struct uio_info uio_info;
1207c9e7a6fSAndy Grover 
121b6df4b79SXiubo Li 	struct inode *inode;
122b6df4b79SXiubo Li 
1237c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb_addr;
1247c9e7a6fSAndy Grover 	size_t dev_size;
1257c9e7a6fSAndy Grover 	u32 cmdr_size;
1267c9e7a6fSAndy Grover 	u32 cmdr_last_cleaned;
1273d9b9555SAndy Grover 	/* Offset of data area from start of mb */
12826418649SSheng Yang 	/* Must add data_off and mb_addr to get the address */
1297c9e7a6fSAndy Grover 	size_t data_off;
1307c9e7a6fSAndy Grover 	size_t data_size;
13126418649SSheng Yang 
1327c9e7a6fSAndy Grover 	wait_queue_head_t wait_cmdr;
133b6df4b79SXiubo Li 	struct mutex cmdr_lock;
1347c9e7a6fSAndy Grover 
135b6df4b79SXiubo Li 	bool waiting_global;
136141685a3SXiubo Li 	uint32_t dbi_max;
137b6df4b79SXiubo Li 	uint32_t dbi_thresh;
138141685a3SXiubo Li 	DECLARE_BITMAP(data_bitmap, DATA_BLOCK_BITS);
139141685a3SXiubo Li 	struct radix_tree_root data_blocks;
140141685a3SXiubo Li 
1417c9e7a6fSAndy Grover 	struct idr commands;
1427c9e7a6fSAndy Grover 	spinlock_t commands_lock;
1437c9e7a6fSAndy Grover 
1447c9e7a6fSAndy Grover 	struct timer_list timeout;
145af980e46SMike Christie 	unsigned int cmd_time_out;
1467c9e7a6fSAndy Grover 
147b3af66e2SMike Christie 	spinlock_t nl_cmd_lock;
148b3af66e2SMike Christie 	struct tcmu_nl_cmd curr_nl_cmd;
149b3af66e2SMike Christie 	/* wake up threads waiting on curr_nl_cmd */
150b3af66e2SMike Christie 	wait_queue_head_t nl_cmd_wq;
151b3af66e2SMike Christie 
1527c9e7a6fSAndy Grover 	char dev_config[TCMU_CONFIG_LEN];
1537c9e7a6fSAndy Grover };
1547c9e7a6fSAndy Grover 
1557c9e7a6fSAndy Grover #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
1567c9e7a6fSAndy Grover 
1577c9e7a6fSAndy Grover #define CMDR_OFF sizeof(struct tcmu_mailbox)
1587c9e7a6fSAndy Grover 
1597c9e7a6fSAndy Grover struct tcmu_cmd {
1607c9e7a6fSAndy Grover 	struct se_cmd *se_cmd;
1617c9e7a6fSAndy Grover 	struct tcmu_dev *tcmu_dev;
1627c9e7a6fSAndy Grover 
1637c9e7a6fSAndy Grover 	uint16_t cmd_id;
1647c9e7a6fSAndy Grover 
16526418649SSheng Yang 	/* Can't use se_cmd when cleaning up expired cmds, because if
1667c9e7a6fSAndy Grover 	   cmd has been completed then accessing se_cmd is off limits */
167141685a3SXiubo Li 	uint32_t dbi_cnt;
168141685a3SXiubo Li 	uint32_t dbi_cur;
169141685a3SXiubo Li 	uint32_t *dbi;
1707c9e7a6fSAndy Grover 
1717c9e7a6fSAndy Grover 	unsigned long deadline;
1727c9e7a6fSAndy Grover 
1737c9e7a6fSAndy Grover #define TCMU_CMD_BIT_EXPIRED 0
1747c9e7a6fSAndy Grover 	unsigned long flags;
1757c9e7a6fSAndy Grover };
1767c9e7a6fSAndy Grover 
177b6df4b79SXiubo Li static struct task_struct *unmap_thread;
178b6df4b79SXiubo Li static wait_queue_head_t unmap_wait;
179b6df4b79SXiubo Li static DEFINE_MUTEX(root_udev_mutex);
180b6df4b79SXiubo Li static LIST_HEAD(root_udev);
181b6df4b79SXiubo Li 
182b6df4b79SXiubo Li static atomic_t global_db_count = ATOMIC_INIT(0);
183b6df4b79SXiubo Li 
1847c9e7a6fSAndy Grover static struct kmem_cache *tcmu_cmd_cache;
1857c9e7a6fSAndy Grover 
1867c9e7a6fSAndy Grover /* multicast group */
1877c9e7a6fSAndy Grover enum tcmu_multicast_groups {
1887c9e7a6fSAndy Grover 	TCMU_MCGRP_CONFIG,
1897c9e7a6fSAndy Grover };
1907c9e7a6fSAndy Grover 
1917c9e7a6fSAndy Grover static const struct genl_multicast_group tcmu_mcgrps[] = {
1927c9e7a6fSAndy Grover 	[TCMU_MCGRP_CONFIG] = { .name = "config", },
1937c9e7a6fSAndy Grover };
1947c9e7a6fSAndy Grover 
195b3af66e2SMike Christie static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = {
196b3af66e2SMike Christie 	[TCMU_ATTR_DEVICE]	= { .type = NLA_STRING },
197b3af66e2SMike Christie 	[TCMU_ATTR_MINOR]	= { .type = NLA_U32 },
198b3af66e2SMike Christie 	[TCMU_ATTR_CMD_STATUS]	= { .type = NLA_S32 },
199b3af66e2SMike Christie 	[TCMU_ATTR_DEVICE_ID]	= { .type = NLA_U32 },
200b3af66e2SMike Christie 	[TCMU_ATTR_SUPP_KERN_CMD_REPLY] = { .type = NLA_U8 },
201b3af66e2SMike Christie };
202b3af66e2SMike Christie 
203b3af66e2SMike Christie static int tcmu_genl_cmd_done(struct genl_info *info, int completed_cmd)
204b3af66e2SMike Christie {
205b3af66e2SMike Christie 	struct se_device *dev;
206b3af66e2SMike Christie 	struct tcmu_dev *udev;
207b3af66e2SMike Christie 	struct tcmu_nl_cmd *nl_cmd;
208b3af66e2SMike Christie 	int dev_id, rc, ret = 0;
209b3af66e2SMike Christie 	bool is_removed = (completed_cmd == TCMU_CMD_REMOVED_DEVICE);
210b3af66e2SMike Christie 
211b3af66e2SMike Christie 	if (!info->attrs[TCMU_ATTR_CMD_STATUS] ||
212b3af66e2SMike Christie 	    !info->attrs[TCMU_ATTR_DEVICE_ID]) {
213b3af66e2SMike Christie 		printk(KERN_ERR "TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n");
214b3af66e2SMike Christie                 return -EINVAL;
215b3af66e2SMike Christie         }
216b3af66e2SMike Christie 
217b3af66e2SMike Christie 	dev_id = nla_get_u32(info->attrs[TCMU_ATTR_DEVICE_ID]);
218b3af66e2SMike Christie 	rc = nla_get_s32(info->attrs[TCMU_ATTR_CMD_STATUS]);
219b3af66e2SMike Christie 
220b3af66e2SMike Christie 	dev = target_find_device(dev_id, !is_removed);
221b3af66e2SMike Christie 	if (!dev) {
222b3af66e2SMike Christie 		printk(KERN_ERR "tcmu nl cmd %u/%u completion could not find device with dev id %u.\n",
223b3af66e2SMike Christie 		       completed_cmd, rc, dev_id);
224b3af66e2SMike Christie 		return -ENODEV;
225b3af66e2SMike Christie 	}
226b3af66e2SMike Christie 	udev = TCMU_DEV(dev);
227b3af66e2SMike Christie 
228b3af66e2SMike Christie 	spin_lock(&udev->nl_cmd_lock);
229b3af66e2SMike Christie 	nl_cmd = &udev->curr_nl_cmd;
230b3af66e2SMike Christie 
231b3af66e2SMike Christie 	pr_debug("genl cmd done got id %d curr %d done %d rc %d\n", dev_id,
232b3af66e2SMike Christie 		 nl_cmd->cmd, completed_cmd, rc);
233b3af66e2SMike Christie 
234b3af66e2SMike Christie 	if (nl_cmd->cmd != completed_cmd) {
235b3af66e2SMike Christie 		printk(KERN_ERR "Mismatched commands (Expecting reply for %d. Current %d).\n",
236b3af66e2SMike Christie 		       completed_cmd, nl_cmd->cmd);
237b3af66e2SMike Christie 		ret = -EINVAL;
238b3af66e2SMike Christie 	} else {
239b3af66e2SMike Christie 		nl_cmd->status = rc;
240b3af66e2SMike Christie 	}
241b3af66e2SMike Christie 
242b3af66e2SMike Christie 	spin_unlock(&udev->nl_cmd_lock);
243b3af66e2SMike Christie 	if (!is_removed)
244b3af66e2SMike Christie 		 target_undepend_item(&dev->dev_group.cg_item);
245b3af66e2SMike Christie 	if (!ret)
246b3af66e2SMike Christie 		complete(&nl_cmd->complete);
247b3af66e2SMike Christie 	return ret;
248b3af66e2SMike Christie }
249b3af66e2SMike Christie 
250b3af66e2SMike Christie static int tcmu_genl_rm_dev_done(struct sk_buff *skb, struct genl_info *info)
251b3af66e2SMike Christie {
252b3af66e2SMike Christie 	return tcmu_genl_cmd_done(info, TCMU_CMD_REMOVED_DEVICE);
253b3af66e2SMike Christie }
254b3af66e2SMike Christie 
255b3af66e2SMike Christie static int tcmu_genl_add_dev_done(struct sk_buff *skb, struct genl_info *info)
256b3af66e2SMike Christie {
257b3af66e2SMike Christie 	return tcmu_genl_cmd_done(info, TCMU_CMD_ADDED_DEVICE);
258b3af66e2SMike Christie }
259b3af66e2SMike Christie 
260b3af66e2SMike Christie static int tcmu_genl_reconfig_dev_done(struct sk_buff *skb,
261b3af66e2SMike Christie 				       struct genl_info *info)
262b3af66e2SMike Christie {
263b3af66e2SMike Christie 	return tcmu_genl_cmd_done(info, TCMU_CMD_RECONFIG_DEVICE);
264b3af66e2SMike Christie }
265b3af66e2SMike Christie 
266b3af66e2SMike Christie static int tcmu_genl_set_features(struct sk_buff *skb, struct genl_info *info)
267b3af66e2SMike Christie {
268b3af66e2SMike Christie 	if (info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]) {
269b3af66e2SMike Christie 		tcmu_kern_cmd_reply_supported  =
270b3af66e2SMike Christie 			nla_get_u8(info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]);
271b3af66e2SMike Christie 		printk(KERN_INFO "tcmu daemon: command reply support %u.\n",
272b3af66e2SMike Christie 		       tcmu_kern_cmd_reply_supported);
273b3af66e2SMike Christie 	}
274b3af66e2SMike Christie 
275b3af66e2SMike Christie 	return 0;
276b3af66e2SMike Christie }
277b3af66e2SMike Christie 
278b3af66e2SMike Christie static const struct genl_ops tcmu_genl_ops[] = {
279b3af66e2SMike Christie 	{
280b3af66e2SMike Christie 		.cmd	= TCMU_CMD_SET_FEATURES,
281b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
282b3af66e2SMike Christie 		.policy	= tcmu_attr_policy,
283b3af66e2SMike Christie 		.doit	= tcmu_genl_set_features,
284b3af66e2SMike Christie 	},
285b3af66e2SMike Christie 	{
286b3af66e2SMike Christie 		.cmd	= TCMU_CMD_ADDED_DEVICE_DONE,
287b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
288b3af66e2SMike Christie 		.policy	= tcmu_attr_policy,
289b3af66e2SMike Christie 		.doit	= tcmu_genl_add_dev_done,
290b3af66e2SMike Christie 	},
291b3af66e2SMike Christie 	{
292b3af66e2SMike Christie 		.cmd	= TCMU_CMD_REMOVED_DEVICE_DONE,
293b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
294b3af66e2SMike Christie 		.policy	= tcmu_attr_policy,
295b3af66e2SMike Christie 		.doit	= tcmu_genl_rm_dev_done,
296b3af66e2SMike Christie 	},
297b3af66e2SMike Christie 	{
298b3af66e2SMike Christie 		.cmd	= TCMU_CMD_RECONFIG_DEVICE_DONE,
299b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
300b3af66e2SMike Christie 		.policy	= tcmu_attr_policy,
301b3af66e2SMike Christie 		.doit	= tcmu_genl_reconfig_dev_done,
302b3af66e2SMike Christie 	},
303b3af66e2SMike Christie };
304b3af66e2SMike Christie 
3057c9e7a6fSAndy Grover /* Our generic netlink family */
30656989f6dSJohannes Berg static struct genl_family tcmu_genl_family __ro_after_init = {
307489111e5SJohannes Berg 	.module = THIS_MODULE,
3087c9e7a6fSAndy Grover 	.hdrsize = 0,
3097c9e7a6fSAndy Grover 	.name = "TCM-USER",
310b3af66e2SMike Christie 	.version = 2,
3117c9e7a6fSAndy Grover 	.maxattr = TCMU_ATTR_MAX,
3127c9e7a6fSAndy Grover 	.mcgrps = tcmu_mcgrps,
3137c9e7a6fSAndy Grover 	.n_mcgrps = ARRAY_SIZE(tcmu_mcgrps),
31420c08b36SSheng Yang 	.netnsok = true,
315b3af66e2SMike Christie 	.ops = tcmu_genl_ops,
316b3af66e2SMike Christie 	.n_ops = ARRAY_SIZE(tcmu_genl_ops),
3177c9e7a6fSAndy Grover };
3187c9e7a6fSAndy Grover 
319141685a3SXiubo Li #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index))
320141685a3SXiubo Li #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0)
321141685a3SXiubo Li #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index))
322141685a3SXiubo Li #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++])
323141685a3SXiubo Li 
324b6df4b79SXiubo Li static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len)
325141685a3SXiubo Li {
326141685a3SXiubo Li 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
327141685a3SXiubo Li 	uint32_t i;
328141685a3SXiubo Li 
329b6df4b79SXiubo Li 	for (i = 0; i < len; i++)
330141685a3SXiubo Li 		clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap);
331141685a3SXiubo Li }
332141685a3SXiubo Li 
333b6df4b79SXiubo Li static inline bool tcmu_get_empty_block(struct tcmu_dev *udev,
334b6df4b79SXiubo Li 					struct tcmu_cmd *tcmu_cmd)
335141685a3SXiubo Li {
336b6df4b79SXiubo Li 	struct page *page;
337b6df4b79SXiubo Li 	int ret, dbi;
338141685a3SXiubo Li 
339b6df4b79SXiubo Li 	dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh);
340b6df4b79SXiubo Li 	if (dbi == udev->dbi_thresh)
341b6df4b79SXiubo Li 		return false;
342b6df4b79SXiubo Li 
343b6df4b79SXiubo Li 	page = radix_tree_lookup(&udev->data_blocks, dbi);
344b6df4b79SXiubo Li 	if (!page) {
345b6df4b79SXiubo Li 		if (atomic_add_return(1, &global_db_count) >
346b6df4b79SXiubo Li 					TCMU_GLOBAL_MAX_BLOCKS) {
347b6df4b79SXiubo Li 			atomic_dec(&global_db_count);
348b6df4b79SXiubo Li 			return false;
349b6df4b79SXiubo Li 		}
350b6df4b79SXiubo Li 
351b6df4b79SXiubo Li 		/* try to get new page from the mm */
352b6df4b79SXiubo Li 		page = alloc_page(GFP_KERNEL);
353b6df4b79SXiubo Li 		if (!page)
354daf78c30SXiubo Li 			goto err_alloc;
355b6df4b79SXiubo Li 
356b6df4b79SXiubo Li 		ret = radix_tree_insert(&udev->data_blocks, dbi, page);
357daf78c30SXiubo Li 		if (ret)
358daf78c30SXiubo Li 			goto err_insert;
359b6df4b79SXiubo Li 	}
360b6df4b79SXiubo Li 
361141685a3SXiubo Li 	if (dbi > udev->dbi_max)
362141685a3SXiubo Li 		udev->dbi_max = dbi;
363141685a3SXiubo Li 
364141685a3SXiubo Li 	set_bit(dbi, udev->data_bitmap);
365b6df4b79SXiubo Li 	tcmu_cmd_set_dbi(tcmu_cmd, dbi);
366141685a3SXiubo Li 
367b6df4b79SXiubo Li 	return true;
368daf78c30SXiubo Li err_insert:
369daf78c30SXiubo Li 	__free_page(page);
370daf78c30SXiubo Li err_alloc:
371daf78c30SXiubo Li 	atomic_dec(&global_db_count);
372daf78c30SXiubo Li 	return false;
373141685a3SXiubo Li }
374141685a3SXiubo Li 
375b6df4b79SXiubo Li static bool tcmu_get_empty_blocks(struct tcmu_dev *udev,
376b6df4b79SXiubo Li 				  struct tcmu_cmd *tcmu_cmd)
377b6df4b79SXiubo Li {
378b6df4b79SXiubo Li 	int i;
379b6df4b79SXiubo Li 
380b6df4b79SXiubo Li 	udev->waiting_global = false;
381b6df4b79SXiubo Li 
382b6df4b79SXiubo Li 	for (i = tcmu_cmd->dbi_cur; i < tcmu_cmd->dbi_cnt; i++) {
383b6df4b79SXiubo Li 		if (!tcmu_get_empty_block(udev, tcmu_cmd))
384b6df4b79SXiubo Li 			goto err;
385141685a3SXiubo Li 	}
386b6df4b79SXiubo Li 	return true;
387b6df4b79SXiubo Li 
388b6df4b79SXiubo Li err:
389b6df4b79SXiubo Li 	udev->waiting_global = true;
390b6df4b79SXiubo Li 	/* Try to wake up the unmap thread */
391b6df4b79SXiubo Li 	wake_up(&unmap_wait);
392b6df4b79SXiubo Li 	return false;
393141685a3SXiubo Li }
394141685a3SXiubo Li 
395b6df4b79SXiubo Li static inline struct page *
396b6df4b79SXiubo Li tcmu_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
397141685a3SXiubo Li {
398141685a3SXiubo Li 	return radix_tree_lookup(&udev->data_blocks, dbi);
399141685a3SXiubo Li }
400141685a3SXiubo Li 
401141685a3SXiubo Li static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd)
402141685a3SXiubo Li {
403141685a3SXiubo Li 	kfree(tcmu_cmd->dbi);
404141685a3SXiubo Li 	kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
405141685a3SXiubo Li }
406141685a3SXiubo Li 
407141685a3SXiubo Li static inline size_t tcmu_cmd_get_data_length(struct tcmu_cmd *tcmu_cmd)
408141685a3SXiubo Li {
409141685a3SXiubo Li 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
410141685a3SXiubo Li 	size_t data_length = round_up(se_cmd->data_length, DATA_BLOCK_SIZE);
411141685a3SXiubo Li 
412141685a3SXiubo Li 	if (se_cmd->se_cmd_flags & SCF_BIDI) {
413141685a3SXiubo Li 		BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents));
414141685a3SXiubo Li 		data_length += round_up(se_cmd->t_bidi_data_sg->length,
415141685a3SXiubo Li 				DATA_BLOCK_SIZE);
416141685a3SXiubo Li 	}
417141685a3SXiubo Li 
418141685a3SXiubo Li 	return data_length;
419141685a3SXiubo Li }
420141685a3SXiubo Li 
421141685a3SXiubo Li static inline uint32_t tcmu_cmd_get_block_cnt(struct tcmu_cmd *tcmu_cmd)
422141685a3SXiubo Li {
423141685a3SXiubo Li 	size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
424141685a3SXiubo Li 
425141685a3SXiubo Li 	return data_length / DATA_BLOCK_SIZE;
426141685a3SXiubo Li }
427141685a3SXiubo Li 
4287c9e7a6fSAndy Grover static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd)
4297c9e7a6fSAndy Grover {
4307c9e7a6fSAndy Grover 	struct se_device *se_dev = se_cmd->se_dev;
4317c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
4327c9e7a6fSAndy Grover 	struct tcmu_cmd *tcmu_cmd;
4337c9e7a6fSAndy Grover 	int cmd_id;
4347c9e7a6fSAndy Grover 
4357c9e7a6fSAndy Grover 	tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_KERNEL);
4367c9e7a6fSAndy Grover 	if (!tcmu_cmd)
4377c9e7a6fSAndy Grover 		return NULL;
4387c9e7a6fSAndy Grover 
4397c9e7a6fSAndy Grover 	tcmu_cmd->se_cmd = se_cmd;
4407c9e7a6fSAndy Grover 	tcmu_cmd->tcmu_dev = udev;
441af980e46SMike Christie 	if (udev->cmd_time_out)
442af980e46SMike Christie 		tcmu_cmd->deadline = jiffies +
443af980e46SMike Christie 					msecs_to_jiffies(udev->cmd_time_out);
4447c9e7a6fSAndy Grover 
445141685a3SXiubo Li 	tcmu_cmd_reset_dbi_cur(tcmu_cmd);
446141685a3SXiubo Li 	tcmu_cmd->dbi_cnt = tcmu_cmd_get_block_cnt(tcmu_cmd);
447141685a3SXiubo Li 	tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t),
448141685a3SXiubo Li 				GFP_KERNEL);
449141685a3SXiubo Li 	if (!tcmu_cmd->dbi) {
450141685a3SXiubo Li 		kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
451141685a3SXiubo Li 		return NULL;
452141685a3SXiubo Li 	}
453141685a3SXiubo Li 
4547c9e7a6fSAndy Grover 	idr_preload(GFP_KERNEL);
4557c9e7a6fSAndy Grover 	spin_lock_irq(&udev->commands_lock);
4567c9e7a6fSAndy Grover 	cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 0,
4577c9e7a6fSAndy Grover 		USHRT_MAX, GFP_NOWAIT);
4587c9e7a6fSAndy Grover 	spin_unlock_irq(&udev->commands_lock);
4597c9e7a6fSAndy Grover 	idr_preload_end();
4607c9e7a6fSAndy Grover 
4617c9e7a6fSAndy Grover 	if (cmd_id < 0) {
462141685a3SXiubo Li 		tcmu_free_cmd(tcmu_cmd);
4637c9e7a6fSAndy Grover 		return NULL;
4647c9e7a6fSAndy Grover 	}
4657c9e7a6fSAndy Grover 	tcmu_cmd->cmd_id = cmd_id;
4667c9e7a6fSAndy Grover 
4677c9e7a6fSAndy Grover 	return tcmu_cmd;
4687c9e7a6fSAndy Grover }
4697c9e7a6fSAndy Grover 
4707c9e7a6fSAndy Grover static inline void tcmu_flush_dcache_range(void *vaddr, size_t size)
4717c9e7a6fSAndy Grover {
472b75d8063SGeliang Tang 	unsigned long offset = offset_in_page(vaddr);
4737c9e7a6fSAndy Grover 
4747c9e7a6fSAndy Grover 	size = round_up(size+offset, PAGE_SIZE);
4757c9e7a6fSAndy Grover 	vaddr -= offset;
4767c9e7a6fSAndy Grover 
4777c9e7a6fSAndy Grover 	while (size) {
4787c9e7a6fSAndy Grover 		flush_dcache_page(virt_to_page(vaddr));
4797c9e7a6fSAndy Grover 		size -= PAGE_SIZE;
4807c9e7a6fSAndy Grover 	}
4817c9e7a6fSAndy Grover }
4827c9e7a6fSAndy Grover 
4837c9e7a6fSAndy Grover /*
4847c9e7a6fSAndy Grover  * Some ring helper functions. We don't assume size is a power of 2 so
4857c9e7a6fSAndy Grover  * we can't use circ_buf.h.
4867c9e7a6fSAndy Grover  */
4877c9e7a6fSAndy Grover static inline size_t spc_used(size_t head, size_t tail, size_t size)
4887c9e7a6fSAndy Grover {
4897c9e7a6fSAndy Grover 	int diff = head - tail;
4907c9e7a6fSAndy Grover 
4917c9e7a6fSAndy Grover 	if (diff >= 0)
4927c9e7a6fSAndy Grover 		return diff;
4937c9e7a6fSAndy Grover 	else
4947c9e7a6fSAndy Grover 		return size + diff;
4957c9e7a6fSAndy Grover }
4967c9e7a6fSAndy Grover 
4977c9e7a6fSAndy Grover static inline size_t spc_free(size_t head, size_t tail, size_t size)
4987c9e7a6fSAndy Grover {
4997c9e7a6fSAndy Grover 	/* Keep 1 byte unused or we can't tell full from empty */
5007c9e7a6fSAndy Grover 	return (size - spc_used(head, tail, size) - 1);
5017c9e7a6fSAndy Grover }
5027c9e7a6fSAndy Grover 
5037c9e7a6fSAndy Grover static inline size_t head_to_end(size_t head, size_t size)
5047c9e7a6fSAndy Grover {
5057c9e7a6fSAndy Grover 	return size - head;
5067c9e7a6fSAndy Grover }
5077c9e7a6fSAndy Grover 
508f1dbd087SSheng Yang static inline void new_iov(struct iovec **iov, int *iov_cnt,
509f1dbd087SSheng Yang 			   struct tcmu_dev *udev)
510f1dbd087SSheng Yang {
511f1dbd087SSheng Yang 	struct iovec *iovec;
512f1dbd087SSheng Yang 
513f1dbd087SSheng Yang 	if (*iov_cnt != 0)
514f1dbd087SSheng Yang 		(*iov)++;
515f1dbd087SSheng Yang 	(*iov_cnt)++;
516f1dbd087SSheng Yang 
517f1dbd087SSheng Yang 	iovec = *iov;
518f1dbd087SSheng Yang 	memset(iovec, 0, sizeof(struct iovec));
519f1dbd087SSheng Yang }
520f1dbd087SSheng Yang 
5217c9e7a6fSAndy Grover #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
5227c9e7a6fSAndy Grover 
52326418649SSheng Yang /* offset is relative to mb_addr */
524141685a3SXiubo Li static inline size_t get_block_offset_user(struct tcmu_dev *dev,
525141685a3SXiubo Li 		int dbi, int remaining)
52626418649SSheng Yang {
527141685a3SXiubo Li 	return dev->data_off + dbi * DATA_BLOCK_SIZE +
52826418649SSheng Yang 		DATA_BLOCK_SIZE - remaining;
52926418649SSheng Yang }
53026418649SSheng Yang 
531daf78c30SXiubo Li static inline size_t iov_tail(struct iovec *iov)
53226418649SSheng Yang {
53326418649SSheng Yang 	return (size_t)iov->iov_base + iov->iov_len;
53426418649SSheng Yang }
53526418649SSheng Yang 
536b6df4b79SXiubo Li static int scatter_data_area(struct tcmu_dev *udev,
537141685a3SXiubo Li 	struct tcmu_cmd *tcmu_cmd, struct scatterlist *data_sg,
538141685a3SXiubo Li 	unsigned int data_nents, struct iovec **iov,
539141685a3SXiubo Li 	int *iov_cnt, bool copy_data)
540f97ec7dbSIlias Tsitsimpis {
541141685a3SXiubo Li 	int i, dbi;
54226418649SSheng Yang 	int block_remaining = 0;
543141685a3SXiubo Li 	void *from, *to = NULL;
544141685a3SXiubo Li 	size_t copy_bytes, to_offset, offset;
545f97ec7dbSIlias Tsitsimpis 	struct scatterlist *sg;
546b6df4b79SXiubo Li 	struct page *page;
547f97ec7dbSIlias Tsitsimpis 
548f97ec7dbSIlias Tsitsimpis 	for_each_sg(data_sg, sg, data_nents, i) {
54926418649SSheng Yang 		int sg_remaining = sg->length;
550f97ec7dbSIlias Tsitsimpis 		from = kmap_atomic(sg_page(sg)) + sg->offset;
55126418649SSheng Yang 		while (sg_remaining > 0) {
55226418649SSheng Yang 			if (block_remaining == 0) {
553b6df4b79SXiubo Li 				if (to)
554b6df4b79SXiubo Li 					kunmap_atomic(to);
555b6df4b79SXiubo Li 
55626418649SSheng Yang 				block_remaining = DATA_BLOCK_SIZE;
557b6df4b79SXiubo Li 				dbi = tcmu_cmd_get_dbi(tcmu_cmd);
558b6df4b79SXiubo Li 				page = tcmu_get_block_page(udev, dbi);
559b6df4b79SXiubo Li 				to = kmap_atomic(page);
560141685a3SXiubo Li 			}
561141685a3SXiubo Li 
56226418649SSheng Yang 			copy_bytes = min_t(size_t, sg_remaining,
56326418649SSheng Yang 					block_remaining);
564141685a3SXiubo Li 			to_offset = get_block_offset_user(udev, dbi,
56526418649SSheng Yang 					block_remaining);
566141685a3SXiubo Li 
56726418649SSheng Yang 			if (*iov_cnt != 0 &&
568daf78c30SXiubo Li 			    to_offset == iov_tail(*iov)) {
569f1dbd087SSheng Yang 				(*iov)->iov_len += copy_bytes;
57026418649SSheng Yang 			} else {
571f1dbd087SSheng Yang 				new_iov(iov, iov_cnt, udev);
57226418649SSheng Yang 				(*iov)->iov_base = (void __user *)to_offset;
573f97ec7dbSIlias Tsitsimpis 				(*iov)->iov_len = copy_bytes;
57426418649SSheng Yang 			}
575f97ec7dbSIlias Tsitsimpis 			if (copy_data) {
576c542942cSXiubo Li 				offset = DATA_BLOCK_SIZE - block_remaining;
577c542942cSXiubo Li 				memcpy(to + offset,
578c542942cSXiubo Li 				       from + sg->length - sg_remaining,
57926418649SSheng Yang 				       copy_bytes);
580f97ec7dbSIlias Tsitsimpis 				tcmu_flush_dcache_range(to, copy_bytes);
581f97ec7dbSIlias Tsitsimpis 			}
58226418649SSheng Yang 			sg_remaining -= copy_bytes;
58326418649SSheng Yang 			block_remaining -= copy_bytes;
584f97ec7dbSIlias Tsitsimpis 		}
585e2e21bd8SSagi Grimberg 		kunmap_atomic(from - sg->offset);
586f97ec7dbSIlias Tsitsimpis 	}
587b6df4b79SXiubo Li 	if (to)
588b6df4b79SXiubo Li 		kunmap_atomic(to);
589f97ec7dbSIlias Tsitsimpis 
590141685a3SXiubo Li 	return 0;
5910c28481fSSheng Yang }
5920c28481fSSheng Yang 
593a5d68ba8SXiubo Li static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
594a5d68ba8SXiubo Li 			     bool bidi)
595f97ec7dbSIlias Tsitsimpis {
596a5d68ba8SXiubo Li 	struct se_cmd *se_cmd = cmd->se_cmd;
597141685a3SXiubo Li 	int i, dbi;
59826418649SSheng Yang 	int block_remaining = 0;
599b6df4b79SXiubo Li 	void *from = NULL, *to;
600141685a3SXiubo Li 	size_t copy_bytes, offset;
601a5d68ba8SXiubo Li 	struct scatterlist *sg, *data_sg;
602b6df4b79SXiubo Li 	struct page *page;
603a5d68ba8SXiubo Li 	unsigned int data_nents;
604141685a3SXiubo Li 	uint32_t count = 0;
605a5d68ba8SXiubo Li 
606a5d68ba8SXiubo Li 	if (!bidi) {
607a5d68ba8SXiubo Li 		data_sg = se_cmd->t_data_sg;
608a5d68ba8SXiubo Li 		data_nents = se_cmd->t_data_nents;
609a5d68ba8SXiubo Li 	} else {
610a5d68ba8SXiubo Li 
611a5d68ba8SXiubo Li 		/*
612a5d68ba8SXiubo Li 		 * For bidi case, the first count blocks are for Data-Out
613a5d68ba8SXiubo Li 		 * buffer blocks, and before gathering the Data-In buffer
614a5d68ba8SXiubo Li 		 * the Data-Out buffer blocks should be discarded.
615a5d68ba8SXiubo Li 		 */
616a5d68ba8SXiubo Li 		count = DIV_ROUND_UP(se_cmd->data_length, DATA_BLOCK_SIZE);
617a5d68ba8SXiubo Li 
618a5d68ba8SXiubo Li 		data_sg = se_cmd->t_bidi_data_sg;
619a5d68ba8SXiubo Li 		data_nents = se_cmd->t_bidi_data_nents;
620a5d68ba8SXiubo Li 	}
621f97ec7dbSIlias Tsitsimpis 
622141685a3SXiubo Li 	tcmu_cmd_set_dbi_cur(cmd, count);
623141685a3SXiubo Li 
624f97ec7dbSIlias Tsitsimpis 	for_each_sg(data_sg, sg, data_nents, i) {
62526418649SSheng Yang 		int sg_remaining = sg->length;
626f97ec7dbSIlias Tsitsimpis 		to = kmap_atomic(sg_page(sg)) + sg->offset;
62726418649SSheng Yang 		while (sg_remaining > 0) {
62826418649SSheng Yang 			if (block_remaining == 0) {
629b6df4b79SXiubo Li 				if (from)
630b6df4b79SXiubo Li 					kunmap_atomic(from);
631b6df4b79SXiubo Li 
63226418649SSheng Yang 				block_remaining = DATA_BLOCK_SIZE;
633141685a3SXiubo Li 				dbi = tcmu_cmd_get_dbi(cmd);
634b6df4b79SXiubo Li 				page = tcmu_get_block_page(udev, dbi);
635b6df4b79SXiubo Li 				from = kmap_atomic(page);
63626418649SSheng Yang 			}
63726418649SSheng Yang 			copy_bytes = min_t(size_t, sg_remaining,
63826418649SSheng Yang 					block_remaining);
639141685a3SXiubo Li 			offset = DATA_BLOCK_SIZE - block_remaining;
640f97ec7dbSIlias Tsitsimpis 			tcmu_flush_dcache_range(from, copy_bytes);
641c542942cSXiubo Li 			memcpy(to + sg->length - sg_remaining, from + offset,
64226418649SSheng Yang 					copy_bytes);
643f97ec7dbSIlias Tsitsimpis 
64426418649SSheng Yang 			sg_remaining -= copy_bytes;
64526418649SSheng Yang 			block_remaining -= copy_bytes;
646f97ec7dbSIlias Tsitsimpis 		}
647e2e21bd8SSagi Grimberg 		kunmap_atomic(to - sg->offset);
648f97ec7dbSIlias Tsitsimpis 	}
649b6df4b79SXiubo Li 	if (from)
650b6df4b79SXiubo Li 		kunmap_atomic(from);
651f97ec7dbSIlias Tsitsimpis }
652f97ec7dbSIlias Tsitsimpis 
653b6df4b79SXiubo Li static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh)
65426418649SSheng Yang {
655b6df4b79SXiubo Li 	return DATA_BLOCK_SIZE * (thresh - bitmap_weight(bitmap, thresh));
65626418649SSheng Yang }
65726418649SSheng Yang 
6587c9e7a6fSAndy Grover /*
659f97ec7dbSIlias Tsitsimpis  * We can't queue a command until we have space available on the cmd ring *and*
6603d9b9555SAndy Grover  * space available on the data area.
6617c9e7a6fSAndy Grover  *
6627c9e7a6fSAndy Grover  * Called with ring lock held.
6637c9e7a6fSAndy Grover  */
664b6df4b79SXiubo Li static bool is_ring_space_avail(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
665b6df4b79SXiubo Li 		size_t cmd_size, size_t data_needed)
6667c9e7a6fSAndy Grover {
6677c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb = udev->mb_addr;
668b6df4b79SXiubo Li 	uint32_t blocks_needed = (data_needed + DATA_BLOCK_SIZE - 1)
669b6df4b79SXiubo Li 				/ DATA_BLOCK_SIZE;
6700241fd39SNicholas Bellinger 	size_t space, cmd_needed;
6717c9e7a6fSAndy Grover 	u32 cmd_head;
6727c9e7a6fSAndy Grover 
6737c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
6747c9e7a6fSAndy Grover 
6757c9e7a6fSAndy Grover 	cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
6767c9e7a6fSAndy Grover 
677f56574a2SAndy Grover 	/*
678f56574a2SAndy Grover 	 * If cmd end-of-ring space is too small then we need space for a NOP plus
679f56574a2SAndy Grover 	 * original cmd - cmds are internally contiguous.
680f56574a2SAndy Grover 	 */
681f56574a2SAndy Grover 	if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size)
682f56574a2SAndy Grover 		cmd_needed = cmd_size;
683f56574a2SAndy Grover 	else
684f56574a2SAndy Grover 		cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size);
685f56574a2SAndy Grover 
6867c9e7a6fSAndy Grover 	space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size);
6877c9e7a6fSAndy Grover 	if (space < cmd_needed) {
6887c9e7a6fSAndy Grover 		pr_debug("no cmd space: %u %u %u\n", cmd_head,
6897c9e7a6fSAndy Grover 		       udev->cmdr_last_cleaned, udev->cmdr_size);
6907c9e7a6fSAndy Grover 		return false;
6917c9e7a6fSAndy Grover 	}
6927c9e7a6fSAndy Grover 
693b6df4b79SXiubo Li 	/* try to check and get the data blocks as needed */
694b6df4b79SXiubo Li 	space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh);
6957c9e7a6fSAndy Grover 	if (space < data_needed) {
696b6df4b79SXiubo Li 		unsigned long blocks_left = DATA_BLOCK_BITS - udev->dbi_thresh;
697b6df4b79SXiubo Li 		unsigned long grow;
698b6df4b79SXiubo Li 
699b6df4b79SXiubo Li 		if (blocks_left < blocks_needed) {
700b6df4b79SXiubo Li 			pr_debug("no data space: only %lu available, but ask for %zu\n",
701b6df4b79SXiubo Li 					blocks_left * DATA_BLOCK_SIZE,
702b6df4b79SXiubo Li 					data_needed);
7037c9e7a6fSAndy Grover 			return false;
7047c9e7a6fSAndy Grover 		}
7057c9e7a6fSAndy Grover 
706b6df4b79SXiubo Li 		/* Try to expand the thresh */
707b6df4b79SXiubo Li 		if (!udev->dbi_thresh) {
708b6df4b79SXiubo Li 			/* From idle state */
709b6df4b79SXiubo Li 			uint32_t init_thresh = DATA_BLOCK_INIT_BITS;
710b6df4b79SXiubo Li 
711b6df4b79SXiubo Li 			udev->dbi_thresh = max(blocks_needed, init_thresh);
712b6df4b79SXiubo Li 		} else {
713b6df4b79SXiubo Li 			/*
714b6df4b79SXiubo Li 			 * Grow the data area by max(blocks needed,
715b6df4b79SXiubo Li 			 * dbi_thresh / 2), but limited to the max
716b6df4b79SXiubo Li 			 * DATA_BLOCK_BITS size.
717b6df4b79SXiubo Li 			 */
718b6df4b79SXiubo Li 			grow = max(blocks_needed, udev->dbi_thresh / 2);
719b6df4b79SXiubo Li 			udev->dbi_thresh += grow;
720b6df4b79SXiubo Li 			if (udev->dbi_thresh > DATA_BLOCK_BITS)
721b6df4b79SXiubo Li 				udev->dbi_thresh = DATA_BLOCK_BITS;
722b6df4b79SXiubo Li 		}
723b6df4b79SXiubo Li 	}
724b6df4b79SXiubo Li 
725daf78c30SXiubo Li 	return tcmu_get_empty_blocks(udev, cmd);
7267c9e7a6fSAndy Grover }
7277c9e7a6fSAndy Grover 
728fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt)
729fe25cc34SXiubo Li {
730fe25cc34SXiubo Li 	return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]),
731fe25cc34SXiubo Li 			sizeof(struct tcmu_cmd_entry));
732fe25cc34SXiubo Li }
733fe25cc34SXiubo Li 
734fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd,
735fe25cc34SXiubo Li 					   size_t base_command_size)
736fe25cc34SXiubo Li {
737fe25cc34SXiubo Li 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
738fe25cc34SXiubo Li 	size_t command_size;
739fe25cc34SXiubo Li 
740fe25cc34SXiubo Li 	command_size = base_command_size +
741fe25cc34SXiubo Li 		round_up(scsi_command_size(se_cmd->t_task_cdb),
742fe25cc34SXiubo Li 				TCMU_OP_ALIGN_SIZE);
743fe25cc34SXiubo Li 
744fe25cc34SXiubo Li 	WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1));
745fe25cc34SXiubo Li 
746fe25cc34SXiubo Li 	return command_size;
747fe25cc34SXiubo Li }
748fe25cc34SXiubo Li 
74902eb924fSAndy Grover static sense_reason_t
75002eb924fSAndy Grover tcmu_queue_cmd_ring(struct tcmu_cmd *tcmu_cmd)
7517c9e7a6fSAndy Grover {
7527c9e7a6fSAndy Grover 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
7537c9e7a6fSAndy Grover 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
7547c9e7a6fSAndy Grover 	size_t base_command_size, command_size;
7557c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb;
7567c9e7a6fSAndy Grover 	struct tcmu_cmd_entry *entry;
7577c9e7a6fSAndy Grover 	struct iovec *iov;
758141685a3SXiubo Li 	int iov_cnt, ret;
7597c9e7a6fSAndy Grover 	uint32_t cmd_head;
7607c9e7a6fSAndy Grover 	uint64_t cdb_off;
761f97ec7dbSIlias Tsitsimpis 	bool copy_to_data_area;
762ab22d260SXiubo Li 	size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
7637c9e7a6fSAndy Grover 
7647c9e7a6fSAndy Grover 	if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags))
76502eb924fSAndy Grover 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
7667c9e7a6fSAndy Grover 
7677c9e7a6fSAndy Grover 	/*
7687c9e7a6fSAndy Grover 	 * Must be a certain minimum size for response sense info, but
7697c9e7a6fSAndy Grover 	 * also may be larger if the iov array is large.
7707c9e7a6fSAndy Grover 	 *
771fe25cc34SXiubo Li 	 * We prepare as many iovs as possbile for potential uses here,
772fe25cc34SXiubo Li 	 * because it's expensive to tell how many regions are freed in
773fe25cc34SXiubo Li 	 * the bitmap & global data pool, as the size calculated here
774fe25cc34SXiubo Li 	 * will only be used to do the checks.
775fe25cc34SXiubo Li 	 *
776fe25cc34SXiubo Li 	 * The size will be recalculated later as actually needed to save
777fe25cc34SXiubo Li 	 * cmd area memories.
7787c9e7a6fSAndy Grover 	 */
779fe25cc34SXiubo Li 	base_command_size = tcmu_cmd_get_base_cmd_size(tcmu_cmd->dbi_cnt);
780fe25cc34SXiubo Li 	command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
7817c9e7a6fSAndy Grover 
782b6df4b79SXiubo Li 	mutex_lock(&udev->cmdr_lock);
7837c9e7a6fSAndy Grover 
7847c9e7a6fSAndy Grover 	mb = udev->mb_addr;
7857c9e7a6fSAndy Grover 	cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
786554617b2SAndy Grover 	if ((command_size > (udev->cmdr_size / 2)) ||
787554617b2SAndy Grover 	    data_length > udev->data_size) {
788554617b2SAndy Grover 		pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu "
7893d9b9555SAndy Grover 			"cmd ring/data area\n", command_size, data_length,
7907c9e7a6fSAndy Grover 			udev->cmdr_size, udev->data_size);
791b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
792554617b2SAndy Grover 		return TCM_INVALID_CDB_FIELD;
793554617b2SAndy Grover 	}
7947c9e7a6fSAndy Grover 
795b6df4b79SXiubo Li 	while (!is_ring_space_avail(udev, tcmu_cmd, command_size, data_length)) {
7967c9e7a6fSAndy Grover 		int ret;
7977c9e7a6fSAndy Grover 		DEFINE_WAIT(__wait);
7987c9e7a6fSAndy Grover 
7997c9e7a6fSAndy Grover 		prepare_to_wait(&udev->wait_cmdr, &__wait, TASK_INTERRUPTIBLE);
8007c9e7a6fSAndy Grover 
8017c9e7a6fSAndy Grover 		pr_debug("sleeping for ring space\n");
802b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
803af980e46SMike Christie 		if (udev->cmd_time_out)
804af980e46SMike Christie 			ret = schedule_timeout(
805af980e46SMike Christie 					msecs_to_jiffies(udev->cmd_time_out));
806af980e46SMike Christie 		else
8077c9e7a6fSAndy Grover 			ret = schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT));
8087c9e7a6fSAndy Grover 		finish_wait(&udev->wait_cmdr, &__wait);
8097c9e7a6fSAndy Grover 		if (!ret) {
8107c9e7a6fSAndy Grover 			pr_warn("tcmu: command timed out\n");
81102eb924fSAndy Grover 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
8127c9e7a6fSAndy Grover 		}
8137c9e7a6fSAndy Grover 
814b6df4b79SXiubo Li 		mutex_lock(&udev->cmdr_lock);
8157c9e7a6fSAndy Grover 
8167c9e7a6fSAndy Grover 		/* We dropped cmdr_lock, cmd_head is stale */
8177c9e7a6fSAndy Grover 		cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
8187c9e7a6fSAndy Grover 	}
8197c9e7a6fSAndy Grover 
820f56574a2SAndy Grover 	/* Insert a PAD if end-of-ring space is too small */
821f56574a2SAndy Grover 	if (head_to_end(cmd_head, udev->cmdr_size) < command_size) {
822f56574a2SAndy Grover 		size_t pad_size = head_to_end(cmd_head, udev->cmdr_size);
823f56574a2SAndy Grover 
8247c9e7a6fSAndy Grover 		entry = (void *) mb + CMDR_OFF + cmd_head;
8250ad46af8SAndy Grover 		tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD);
8260ad46af8SAndy Grover 		tcmu_hdr_set_len(&entry->hdr.len_op, pad_size);
8270ad46af8SAndy Grover 		entry->hdr.cmd_id = 0; /* not used for PAD */
8280ad46af8SAndy Grover 		entry->hdr.kflags = 0;
8290ad46af8SAndy Grover 		entry->hdr.uflags = 0;
8309d62bc0eSXiubo Li 		tcmu_flush_dcache_range(entry, sizeof(*entry));
8317c9e7a6fSAndy Grover 
8327c9e7a6fSAndy Grover 		UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size);
8339d62bc0eSXiubo Li 		tcmu_flush_dcache_range(mb, sizeof(*mb));
8347c9e7a6fSAndy Grover 
8357c9e7a6fSAndy Grover 		cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
8367c9e7a6fSAndy Grover 		WARN_ON(cmd_head != 0);
8377c9e7a6fSAndy Grover 	}
8387c9e7a6fSAndy Grover 
8397c9e7a6fSAndy Grover 	entry = (void *) mb + CMDR_OFF + cmd_head;
840b3743c71SXiubo Li 	memset(entry, 0, command_size);
8410ad46af8SAndy Grover 	tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD);
8420ad46af8SAndy Grover 	entry->hdr.cmd_id = tcmu_cmd->cmd_id;
8437c9e7a6fSAndy Grover 
8443d9b9555SAndy Grover 	/* Handle allocating space from the data area */
845b6df4b79SXiubo Li 	tcmu_cmd_reset_dbi_cur(tcmu_cmd);
8467c9e7a6fSAndy Grover 	iov = &entry->req.iov[0];
847f97ec7dbSIlias Tsitsimpis 	iov_cnt = 0;
848e4648b01SIlias Tsitsimpis 	copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE
849e4648b01SIlias Tsitsimpis 		|| se_cmd->se_cmd_flags & SCF_BIDI);
850b6df4b79SXiubo Li 	ret = scatter_data_area(udev, tcmu_cmd, se_cmd->t_data_sg,
851b6df4b79SXiubo Li 				se_cmd->t_data_nents, &iov, &iov_cnt,
852b6df4b79SXiubo Li 				copy_to_data_area);
853141685a3SXiubo Li 	if (ret) {
854b6df4b79SXiubo Li 		tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt);
855b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
856b6df4b79SXiubo Li 
857141685a3SXiubo Li 		pr_err("tcmu: alloc and scatter data failed\n");
858141685a3SXiubo Li 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
859141685a3SXiubo Li 	}
8607c9e7a6fSAndy Grover 	entry->req.iov_cnt = iov_cnt;
8617c9e7a6fSAndy Grover 
862e4648b01SIlias Tsitsimpis 	/* Handle BIDI commands */
863e4648b01SIlias Tsitsimpis 	iov_cnt = 0;
864b3743c71SXiubo Li 	if (se_cmd->se_cmd_flags & SCF_BIDI) {
865ab22d260SXiubo Li 		iov++;
866b6df4b79SXiubo Li 		ret = scatter_data_area(udev, tcmu_cmd,
867141685a3SXiubo Li 					se_cmd->t_bidi_data_sg,
868141685a3SXiubo Li 					se_cmd->t_bidi_data_nents,
869141685a3SXiubo Li 					&iov, &iov_cnt, false);
870141685a3SXiubo Li 		if (ret) {
871b6df4b79SXiubo Li 			tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt);
872b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
873b6df4b79SXiubo Li 
874141685a3SXiubo Li 			pr_err("tcmu: alloc and scatter bidi data failed\n");
875141685a3SXiubo Li 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
876141685a3SXiubo Li 		}
877ab22d260SXiubo Li 	}
878b3743c71SXiubo Li 	entry->req.iov_bidi_cnt = iov_cnt;
87926418649SSheng Yang 
880fe25cc34SXiubo Li 	/*
881fe25cc34SXiubo Li 	 * Recalaulate the command's base size and size according
882fe25cc34SXiubo Li 	 * to the actual needs
883fe25cc34SXiubo Li 	 */
884fe25cc34SXiubo Li 	base_command_size = tcmu_cmd_get_base_cmd_size(entry->req.iov_cnt +
885fe25cc34SXiubo Li 						       entry->req.iov_bidi_cnt);
886fe25cc34SXiubo Li 	command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
887fe25cc34SXiubo Li 
888fe25cc34SXiubo Li 	tcmu_hdr_set_len(&entry->hdr.len_op, command_size);
889fe25cc34SXiubo Li 
8907c9e7a6fSAndy Grover 	/* All offsets relative to mb_addr, not start of entry! */
8917c9e7a6fSAndy Grover 	cdb_off = CMDR_OFF + cmd_head + base_command_size;
8927c9e7a6fSAndy Grover 	memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb));
8937c9e7a6fSAndy Grover 	entry->req.cdb_off = cdb_off;
8947c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(entry, sizeof(*entry));
8957c9e7a6fSAndy Grover 
8967c9e7a6fSAndy Grover 	UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size);
8977c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
898b6df4b79SXiubo Li 	mutex_unlock(&udev->cmdr_lock);
8997c9e7a6fSAndy Grover 
9007c9e7a6fSAndy Grover 	/* TODO: only if FLUSH and FUA? */
9017c9e7a6fSAndy Grover 	uio_event_notify(&udev->uio_info);
9027c9e7a6fSAndy Grover 
903af980e46SMike Christie 	if (udev->cmd_time_out)
904af980e46SMike Christie 		mod_timer(&udev->timeout, round_jiffies_up(jiffies +
905af980e46SMike Christie 			  msecs_to_jiffies(udev->cmd_time_out)));
9067c9e7a6fSAndy Grover 
90702eb924fSAndy Grover 	return TCM_NO_SENSE;
9087c9e7a6fSAndy Grover }
9097c9e7a6fSAndy Grover 
91002eb924fSAndy Grover static sense_reason_t
91102eb924fSAndy Grover tcmu_queue_cmd(struct se_cmd *se_cmd)
9127c9e7a6fSAndy Grover {
9137c9e7a6fSAndy Grover 	struct se_device *se_dev = se_cmd->se_dev;
9147c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
9157c9e7a6fSAndy Grover 	struct tcmu_cmd *tcmu_cmd;
916ecaf597bSBart Van Assche 	sense_reason_t ret;
9177c9e7a6fSAndy Grover 
9187c9e7a6fSAndy Grover 	tcmu_cmd = tcmu_alloc_cmd(se_cmd);
9197c9e7a6fSAndy Grover 	if (!tcmu_cmd)
92002eb924fSAndy Grover 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
9217c9e7a6fSAndy Grover 
9227c9e7a6fSAndy Grover 	ret = tcmu_queue_cmd_ring(tcmu_cmd);
92302eb924fSAndy Grover 	if (ret != TCM_NO_SENSE) {
9247c9e7a6fSAndy Grover 		pr_err("TCMU: Could not queue command\n");
9257c9e7a6fSAndy Grover 		spin_lock_irq(&udev->commands_lock);
9267c9e7a6fSAndy Grover 		idr_remove(&udev->commands, tcmu_cmd->cmd_id);
9277c9e7a6fSAndy Grover 		spin_unlock_irq(&udev->commands_lock);
9287c9e7a6fSAndy Grover 
929141685a3SXiubo Li 		tcmu_free_cmd(tcmu_cmd);
9307c9e7a6fSAndy Grover 	}
9317c9e7a6fSAndy Grover 
9327c9e7a6fSAndy Grover 	return ret;
9337c9e7a6fSAndy Grover }
9347c9e7a6fSAndy Grover 
9357c9e7a6fSAndy Grover static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry)
9367c9e7a6fSAndy Grover {
9377c9e7a6fSAndy Grover 	struct se_cmd *se_cmd = cmd->se_cmd;
9387c9e7a6fSAndy Grover 	struct tcmu_dev *udev = cmd->tcmu_dev;
9397c9e7a6fSAndy Grover 
940b25c7863SSheng Yang 	/*
941b25c7863SSheng Yang 	 * cmd has been completed already from timeout, just reclaim
9423d9b9555SAndy Grover 	 * data area space and free cmd
943b25c7863SSheng Yang 	 */
944141685a3SXiubo Li 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
945141685a3SXiubo Li 		goto out;
946b25c7863SSheng Yang 
947141685a3SXiubo Li 	tcmu_cmd_reset_dbi_cur(cmd);
9487c9e7a6fSAndy Grover 
9490ad46af8SAndy Grover 	if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) {
9500ad46af8SAndy Grover 		pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
9510ad46af8SAndy Grover 			cmd->se_cmd);
952ed97d0cdSAndy Grover 		entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION;
953ed97d0cdSAndy Grover 	} else if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) {
954406f74c2SMike Christie 		transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer);
955e4648b01SIlias Tsitsimpis 	} else if (se_cmd->se_cmd_flags & SCF_BIDI) {
95626418649SSheng Yang 		/* Get Data-In buffer before clean up */
957a5d68ba8SXiubo Li 		gather_data_area(udev, cmd, true);
958e4648b01SIlias Tsitsimpis 	} else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
959a5d68ba8SXiubo Li 		gather_data_area(udev, cmd, false);
9607c9e7a6fSAndy Grover 	} else if (se_cmd->data_direction == DMA_TO_DEVICE) {
961141685a3SXiubo Li 		/* TODO: */
9622bc396a2SIlias Tsitsimpis 	} else if (se_cmd->data_direction != DMA_NONE) {
9632bc396a2SIlias Tsitsimpis 		pr_warn("TCMU: data direction was %d!\n",
9642bc396a2SIlias Tsitsimpis 			se_cmd->data_direction);
9657c9e7a6fSAndy Grover 	}
9667c9e7a6fSAndy Grover 
9677c9e7a6fSAndy Grover 	target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
9687c9e7a6fSAndy Grover 
969141685a3SXiubo Li out:
970141685a3SXiubo Li 	cmd->se_cmd = NULL;
971b6df4b79SXiubo Li 	tcmu_cmd_free_data(cmd, cmd->dbi_cnt);
972141685a3SXiubo Li 	tcmu_free_cmd(cmd);
9737c9e7a6fSAndy Grover }
9747c9e7a6fSAndy Grover 
9757c9e7a6fSAndy Grover static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
9767c9e7a6fSAndy Grover {
9777c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb;
9787c9e7a6fSAndy Grover 	int handled = 0;
9797c9e7a6fSAndy Grover 
9807c9e7a6fSAndy Grover 	if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
9817c9e7a6fSAndy Grover 		pr_err("ring broken, not handling completions\n");
9827c9e7a6fSAndy Grover 		return 0;
9837c9e7a6fSAndy Grover 	}
9847c9e7a6fSAndy Grover 
9857c9e7a6fSAndy Grover 	mb = udev->mb_addr;
9867c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
9877c9e7a6fSAndy Grover 
9887c9e7a6fSAndy Grover 	while (udev->cmdr_last_cleaned != ACCESS_ONCE(mb->cmd_tail)) {
9897c9e7a6fSAndy Grover 
9907c9e7a6fSAndy Grover 		struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned;
9917c9e7a6fSAndy Grover 		struct tcmu_cmd *cmd;
9927c9e7a6fSAndy Grover 
9937c9e7a6fSAndy Grover 		tcmu_flush_dcache_range(entry, sizeof(*entry));
9947c9e7a6fSAndy Grover 
9950ad46af8SAndy Grover 		if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) {
9960ad46af8SAndy Grover 			UPDATE_HEAD(udev->cmdr_last_cleaned,
9970ad46af8SAndy Grover 				    tcmu_hdr_get_len(entry->hdr.len_op),
9980ad46af8SAndy Grover 				    udev->cmdr_size);
9997c9e7a6fSAndy Grover 			continue;
10007c9e7a6fSAndy Grover 		}
10010ad46af8SAndy Grover 		WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
10027c9e7a6fSAndy Grover 
10037c9e7a6fSAndy Grover 		spin_lock(&udev->commands_lock);
1004d3e709e6SMatthew Wilcox 		cmd = idr_remove(&udev->commands, entry->hdr.cmd_id);
10057c9e7a6fSAndy Grover 		spin_unlock(&udev->commands_lock);
10067c9e7a6fSAndy Grover 
10077c9e7a6fSAndy Grover 		if (!cmd) {
10087c9e7a6fSAndy Grover 			pr_err("cmd_id not found, ring is broken\n");
10097c9e7a6fSAndy Grover 			set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
10107c9e7a6fSAndy Grover 			break;
10117c9e7a6fSAndy Grover 		}
10127c9e7a6fSAndy Grover 
10137c9e7a6fSAndy Grover 		tcmu_handle_completion(cmd, entry);
10147c9e7a6fSAndy Grover 
10150ad46af8SAndy Grover 		UPDATE_HEAD(udev->cmdr_last_cleaned,
10160ad46af8SAndy Grover 			    tcmu_hdr_get_len(entry->hdr.len_op),
10170ad46af8SAndy Grover 			    udev->cmdr_size);
10187c9e7a6fSAndy Grover 
10197c9e7a6fSAndy Grover 		handled++;
10207c9e7a6fSAndy Grover 	}
10217c9e7a6fSAndy Grover 
10227c9e7a6fSAndy Grover 	if (mb->cmd_tail == mb->cmd_head)
10237c9e7a6fSAndy Grover 		del_timer(&udev->timeout); /* no more pending cmds */
10247c9e7a6fSAndy Grover 
10257c9e7a6fSAndy Grover 	wake_up(&udev->wait_cmdr);
10267c9e7a6fSAndy Grover 
10277c9e7a6fSAndy Grover 	return handled;
10287c9e7a6fSAndy Grover }
10297c9e7a6fSAndy Grover 
10307c9e7a6fSAndy Grover static int tcmu_check_expired_cmd(int id, void *p, void *data)
10317c9e7a6fSAndy Grover {
10327c9e7a6fSAndy Grover 	struct tcmu_cmd *cmd = p;
10337c9e7a6fSAndy Grover 
10347c9e7a6fSAndy Grover 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
10357c9e7a6fSAndy Grover 		return 0;
10367c9e7a6fSAndy Grover 
1037611e2267SAndy Grover 	if (!time_after(jiffies, cmd->deadline))
10387c9e7a6fSAndy Grover 		return 0;
10397c9e7a6fSAndy Grover 
10407c9e7a6fSAndy Grover 	set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
10417c9e7a6fSAndy Grover 	target_complete_cmd(cmd->se_cmd, SAM_STAT_CHECK_CONDITION);
10427c9e7a6fSAndy Grover 	cmd->se_cmd = NULL;
10437c9e7a6fSAndy Grover 
10447c9e7a6fSAndy Grover 	return 0;
10457c9e7a6fSAndy Grover }
10467c9e7a6fSAndy Grover 
10477c9e7a6fSAndy Grover static void tcmu_device_timedout(unsigned long data)
10487c9e7a6fSAndy Grover {
10497c9e7a6fSAndy Grover 	struct tcmu_dev *udev = (struct tcmu_dev *)data;
10507c9e7a6fSAndy Grover 	unsigned long flags;
10517c9e7a6fSAndy Grover 
10527c9e7a6fSAndy Grover 	spin_lock_irqsave(&udev->commands_lock, flags);
10537c9e7a6fSAndy Grover 	idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL);
10547c9e7a6fSAndy Grover 	spin_unlock_irqrestore(&udev->commands_lock, flags);
10557c9e7a6fSAndy Grover 
1056b6df4b79SXiubo Li 	/* Try to wake up the ummap thread */
1057b6df4b79SXiubo Li 	wake_up(&unmap_wait);
1058b6df4b79SXiubo Li 
10597c9e7a6fSAndy Grover 	/*
10607c9e7a6fSAndy Grover 	 * We don't need to wakeup threads on wait_cmdr since they have their
10617c9e7a6fSAndy Grover 	 * own timeout.
10627c9e7a6fSAndy Grover 	 */
10637c9e7a6fSAndy Grover }
10647c9e7a6fSAndy Grover 
10657c9e7a6fSAndy Grover static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
10667c9e7a6fSAndy Grover {
10677c9e7a6fSAndy Grover 	struct tcmu_hba *tcmu_hba;
10687c9e7a6fSAndy Grover 
10697c9e7a6fSAndy Grover 	tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL);
10707c9e7a6fSAndy Grover 	if (!tcmu_hba)
10717c9e7a6fSAndy Grover 		return -ENOMEM;
10727c9e7a6fSAndy Grover 
10737c9e7a6fSAndy Grover 	tcmu_hba->host_id = host_id;
10747c9e7a6fSAndy Grover 	hba->hba_ptr = tcmu_hba;
10757c9e7a6fSAndy Grover 
10767c9e7a6fSAndy Grover 	return 0;
10777c9e7a6fSAndy Grover }
10787c9e7a6fSAndy Grover 
10797c9e7a6fSAndy Grover static void tcmu_detach_hba(struct se_hba *hba)
10807c9e7a6fSAndy Grover {
10817c9e7a6fSAndy Grover 	kfree(hba->hba_ptr);
10827c9e7a6fSAndy Grover 	hba->hba_ptr = NULL;
10837c9e7a6fSAndy Grover }
10847c9e7a6fSAndy Grover 
10857c9e7a6fSAndy Grover static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
10867c9e7a6fSAndy Grover {
10877c9e7a6fSAndy Grover 	struct tcmu_dev *udev;
10887c9e7a6fSAndy Grover 
10897c9e7a6fSAndy Grover 	udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
10907c9e7a6fSAndy Grover 	if (!udev)
10917c9e7a6fSAndy Grover 		return NULL;
1092f3cdbe39SMike Christie 	kref_init(&udev->kref);
10937c9e7a6fSAndy Grover 
10947c9e7a6fSAndy Grover 	udev->name = kstrdup(name, GFP_KERNEL);
10957c9e7a6fSAndy Grover 	if (!udev->name) {
10967c9e7a6fSAndy Grover 		kfree(udev);
10977c9e7a6fSAndy Grover 		return NULL;
10987c9e7a6fSAndy Grover 	}
10997c9e7a6fSAndy Grover 
11007c9e7a6fSAndy Grover 	udev->hba = hba;
1101af980e46SMike Christie 	udev->cmd_time_out = TCMU_TIME_OUT;
11027c9e7a6fSAndy Grover 
11037c9e7a6fSAndy Grover 	init_waitqueue_head(&udev->wait_cmdr);
1104b6df4b79SXiubo Li 	mutex_init(&udev->cmdr_lock);
11057c9e7a6fSAndy Grover 
11067c9e7a6fSAndy Grover 	idr_init(&udev->commands);
11077c9e7a6fSAndy Grover 	spin_lock_init(&udev->commands_lock);
11087c9e7a6fSAndy Grover 
11097c9e7a6fSAndy Grover 	setup_timer(&udev->timeout, tcmu_device_timedout,
11107c9e7a6fSAndy Grover 		(unsigned long)udev);
11117c9e7a6fSAndy Grover 
1112b3af66e2SMike Christie 	init_waitqueue_head(&udev->nl_cmd_wq);
1113b3af66e2SMike Christie 	spin_lock_init(&udev->nl_cmd_lock);
1114b3af66e2SMike Christie 
1115c22adc0bSXiubo Li 	INIT_RADIX_TREE(&udev->data_blocks, GFP_KERNEL);
1116c22adc0bSXiubo Li 
11177c9e7a6fSAndy Grover 	return &udev->se_dev;
11187c9e7a6fSAndy Grover }
11197c9e7a6fSAndy Grover 
11207c9e7a6fSAndy Grover static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
11217c9e7a6fSAndy Grover {
11227c9e7a6fSAndy Grover 	struct tcmu_dev *tcmu_dev = container_of(info, struct tcmu_dev, uio_info);
11237c9e7a6fSAndy Grover 
1124b6df4b79SXiubo Li 	mutex_lock(&tcmu_dev->cmdr_lock);
11257c9e7a6fSAndy Grover 	tcmu_handle_completions(tcmu_dev);
1126b6df4b79SXiubo Li 	mutex_unlock(&tcmu_dev->cmdr_lock);
11277c9e7a6fSAndy Grover 
11287c9e7a6fSAndy Grover 	return 0;
11297c9e7a6fSAndy Grover }
11307c9e7a6fSAndy Grover 
11317c9e7a6fSAndy Grover /*
11327c9e7a6fSAndy Grover  * mmap code from uio.c. Copied here because we want to hook mmap()
11337c9e7a6fSAndy Grover  * and this stuff must come along.
11347c9e7a6fSAndy Grover  */
11357c9e7a6fSAndy Grover static int tcmu_find_mem_index(struct vm_area_struct *vma)
11367c9e7a6fSAndy Grover {
11377c9e7a6fSAndy Grover 	struct tcmu_dev *udev = vma->vm_private_data;
11387c9e7a6fSAndy Grover 	struct uio_info *info = &udev->uio_info;
11397c9e7a6fSAndy Grover 
11407c9e7a6fSAndy Grover 	if (vma->vm_pgoff < MAX_UIO_MAPS) {
11417c9e7a6fSAndy Grover 		if (info->mem[vma->vm_pgoff].size == 0)
11427c9e7a6fSAndy Grover 			return -1;
11437c9e7a6fSAndy Grover 		return (int)vma->vm_pgoff;
11447c9e7a6fSAndy Grover 	}
11457c9e7a6fSAndy Grover 	return -1;
11467c9e7a6fSAndy Grover }
11477c9e7a6fSAndy Grover 
1148b6df4b79SXiubo Li static struct page *tcmu_try_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
1149b6df4b79SXiubo Li {
1150b6df4b79SXiubo Li 	struct page *page;
1151b6df4b79SXiubo Li 	int ret;
1152b6df4b79SXiubo Li 
1153b6df4b79SXiubo Li 	mutex_lock(&udev->cmdr_lock);
1154b6df4b79SXiubo Li 	page = tcmu_get_block_page(udev, dbi);
1155b6df4b79SXiubo Li 	if (likely(page)) {
1156b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
1157b6df4b79SXiubo Li 		return page;
1158b6df4b79SXiubo Li 	}
1159b6df4b79SXiubo Li 
1160b6df4b79SXiubo Li 	/*
1161b6df4b79SXiubo Li 	 * Normally it shouldn't be here:
1162b6df4b79SXiubo Li 	 * Only when the userspace has touched the blocks which
1163b6df4b79SXiubo Li 	 * are out of the tcmu_cmd's data iov[], and will return
1164b6df4b79SXiubo Li 	 * one zeroed page.
1165b6df4b79SXiubo Li 	 */
1166b6df4b79SXiubo Li 	pr_warn("Block(%u) out of cmd's iov[] has been touched!\n", dbi);
1167b6df4b79SXiubo Li 	pr_warn("Mostly it will be a bug of userspace, please have a check!\n");
1168b6df4b79SXiubo Li 
1169b6df4b79SXiubo Li 	if (dbi >= udev->dbi_thresh) {
1170b6df4b79SXiubo Li 		/* Extern the udev->dbi_thresh to dbi + 1 */
1171b6df4b79SXiubo Li 		udev->dbi_thresh = dbi + 1;
1172b6df4b79SXiubo Li 		udev->dbi_max = dbi;
1173b6df4b79SXiubo Li 	}
1174b6df4b79SXiubo Li 
1175b6df4b79SXiubo Li 	page = radix_tree_lookup(&udev->data_blocks, dbi);
1176b6df4b79SXiubo Li 	if (!page) {
1177b6df4b79SXiubo Li 		page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1178b6df4b79SXiubo Li 		if (!page) {
1179b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
1180b6df4b79SXiubo Li 			return NULL;
1181b6df4b79SXiubo Li 		}
1182b6df4b79SXiubo Li 
1183b6df4b79SXiubo Li 		ret = radix_tree_insert(&udev->data_blocks, dbi, page);
1184b6df4b79SXiubo Li 		if (ret) {
1185b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
1186b6df4b79SXiubo Li 			__free_page(page);
1187b6df4b79SXiubo Li 			return NULL;
1188b6df4b79SXiubo Li 		}
1189b6df4b79SXiubo Li 
1190b6df4b79SXiubo Li 		/*
1191b6df4b79SXiubo Li 		 * Since this case is rare in page fault routine, here we
1192b6df4b79SXiubo Li 		 * will allow the global_db_count >= TCMU_GLOBAL_MAX_BLOCKS
1193b6df4b79SXiubo Li 		 * to reduce possible page fault call trace.
1194b6df4b79SXiubo Li 		 */
1195b6df4b79SXiubo Li 		atomic_inc(&global_db_count);
1196b6df4b79SXiubo Li 	}
1197b6df4b79SXiubo Li 	mutex_unlock(&udev->cmdr_lock);
1198b6df4b79SXiubo Li 
1199b6df4b79SXiubo Li 	return page;
1200b6df4b79SXiubo Li }
1201b6df4b79SXiubo Li 
120211bac800SDave Jiang static int tcmu_vma_fault(struct vm_fault *vmf)
12037c9e7a6fSAndy Grover {
120411bac800SDave Jiang 	struct tcmu_dev *udev = vmf->vma->vm_private_data;
12057c9e7a6fSAndy Grover 	struct uio_info *info = &udev->uio_info;
12067c9e7a6fSAndy Grover 	struct page *page;
12077c9e7a6fSAndy Grover 	unsigned long offset;
12087c9e7a6fSAndy Grover 	void *addr;
12097c9e7a6fSAndy Grover 
121011bac800SDave Jiang 	int mi = tcmu_find_mem_index(vmf->vma);
12117c9e7a6fSAndy Grover 	if (mi < 0)
12127c9e7a6fSAndy Grover 		return VM_FAULT_SIGBUS;
12137c9e7a6fSAndy Grover 
12147c9e7a6fSAndy Grover 	/*
12157c9e7a6fSAndy Grover 	 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
12167c9e7a6fSAndy Grover 	 * to use mem[N].
12177c9e7a6fSAndy Grover 	 */
12187c9e7a6fSAndy Grover 	offset = (vmf->pgoff - mi) << PAGE_SHIFT;
12197c9e7a6fSAndy Grover 
1220141685a3SXiubo Li 	if (offset < udev->data_off) {
1221141685a3SXiubo Li 		/* For the vmalloc()ed cmd area pages */
12227c9e7a6fSAndy Grover 		addr = (void *)(unsigned long)info->mem[mi].addr + offset;
12237c9e7a6fSAndy Grover 		page = vmalloc_to_page(addr);
1224141685a3SXiubo Li 	} else {
1225141685a3SXiubo Li 		uint32_t dbi;
1226141685a3SXiubo Li 
1227b6df4b79SXiubo Li 		/* For the dynamically growing data area pages */
1228141685a3SXiubo Li 		dbi = (offset - udev->data_off) / DATA_BLOCK_SIZE;
1229b6df4b79SXiubo Li 		page = tcmu_try_get_block_page(udev, dbi);
1230b6df4b79SXiubo Li 		if (!page)
1231141685a3SXiubo Li 			return VM_FAULT_NOPAGE;
1232141685a3SXiubo Li 	}
1233141685a3SXiubo Li 
12347c9e7a6fSAndy Grover 	get_page(page);
12357c9e7a6fSAndy Grover 	vmf->page = page;
12367c9e7a6fSAndy Grover 	return 0;
12377c9e7a6fSAndy Grover }
12387c9e7a6fSAndy Grover 
12397c9e7a6fSAndy Grover static const struct vm_operations_struct tcmu_vm_ops = {
12407c9e7a6fSAndy Grover 	.fault = tcmu_vma_fault,
12417c9e7a6fSAndy Grover };
12427c9e7a6fSAndy Grover 
12437c9e7a6fSAndy Grover static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma)
12447c9e7a6fSAndy Grover {
12457c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
12467c9e7a6fSAndy Grover 
12477c9e7a6fSAndy Grover 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
12487c9e7a6fSAndy Grover 	vma->vm_ops = &tcmu_vm_ops;
12497c9e7a6fSAndy Grover 
12507c9e7a6fSAndy Grover 	vma->vm_private_data = udev;
12517c9e7a6fSAndy Grover 
12527c9e7a6fSAndy Grover 	/* Ensure the mmap is exactly the right size */
12537c9e7a6fSAndy Grover 	if (vma_pages(vma) != (TCMU_RING_SIZE >> PAGE_SHIFT))
12547c9e7a6fSAndy Grover 		return -EINVAL;
12557c9e7a6fSAndy Grover 
12567c9e7a6fSAndy Grover 	return 0;
12577c9e7a6fSAndy Grover }
12587c9e7a6fSAndy Grover 
12597c9e7a6fSAndy Grover static int tcmu_open(struct uio_info *info, struct inode *inode)
12607c9e7a6fSAndy Grover {
12617c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
12627c9e7a6fSAndy Grover 
12637c9e7a6fSAndy Grover 	/* O_EXCL not supported for char devs, so fake it? */
12647c9e7a6fSAndy Grover 	if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
12657c9e7a6fSAndy Grover 		return -EBUSY;
12667c9e7a6fSAndy Grover 
1267b6df4b79SXiubo Li 	udev->inode = inode;
12689260695dSMike Christie 	kref_get(&udev->kref);
1269b6df4b79SXiubo Li 
12707c9e7a6fSAndy Grover 	pr_debug("open\n");
12717c9e7a6fSAndy Grover 
12727c9e7a6fSAndy Grover 	return 0;
12737c9e7a6fSAndy Grover }
12747c9e7a6fSAndy Grover 
1275f3cdbe39SMike Christie static void tcmu_dev_call_rcu(struct rcu_head *p)
1276f3cdbe39SMike Christie {
1277f3cdbe39SMike Christie 	struct se_device *dev = container_of(p, struct se_device, rcu_head);
1278f3cdbe39SMike Christie 	struct tcmu_dev *udev = TCMU_DEV(dev);
1279f3cdbe39SMike Christie 
1280f3cdbe39SMike Christie 	kfree(udev->uio_info.name);
1281f3cdbe39SMike Christie 	kfree(udev->name);
1282f3cdbe39SMike Christie 	kfree(udev);
1283f3cdbe39SMike Christie }
1284f3cdbe39SMike Christie 
1285c22adc0bSXiubo Li static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
1286c22adc0bSXiubo Li {
1287c22adc0bSXiubo Li 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
1288c22adc0bSXiubo Li 		kmem_cache_free(tcmu_cmd_cache, cmd);
1289c22adc0bSXiubo Li 		return 0;
1290c22adc0bSXiubo Li 	}
1291c22adc0bSXiubo Li 	return -EINVAL;
1292c22adc0bSXiubo Li }
1293c22adc0bSXiubo Li 
1294c22adc0bSXiubo Li static void tcmu_blocks_release(struct tcmu_dev *udev)
1295c22adc0bSXiubo Li {
1296c22adc0bSXiubo Li 	int i;
1297c22adc0bSXiubo Li 	struct page *page;
1298c22adc0bSXiubo Li 
1299c22adc0bSXiubo Li 	/* Try to release all block pages */
1300c22adc0bSXiubo Li 	mutex_lock(&udev->cmdr_lock);
1301c22adc0bSXiubo Li 	for (i = 0; i <= udev->dbi_max; i++) {
1302c22adc0bSXiubo Li 		page = radix_tree_delete(&udev->data_blocks, i);
1303c22adc0bSXiubo Li 		if (page) {
1304c22adc0bSXiubo Li 			__free_page(page);
1305c22adc0bSXiubo Li 			atomic_dec(&global_db_count);
1306c22adc0bSXiubo Li 		}
1307c22adc0bSXiubo Li 	}
1308c22adc0bSXiubo Li 	mutex_unlock(&udev->cmdr_lock);
1309c22adc0bSXiubo Li }
1310c22adc0bSXiubo Li 
1311f3cdbe39SMike Christie static void tcmu_dev_kref_release(struct kref *kref)
1312f3cdbe39SMike Christie {
1313f3cdbe39SMike Christie 	struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref);
1314f3cdbe39SMike Christie 	struct se_device *dev = &udev->se_dev;
1315c22adc0bSXiubo Li 	struct tcmu_cmd *cmd;
1316c22adc0bSXiubo Li 	bool all_expired = true;
1317c22adc0bSXiubo Li 	int i;
1318c22adc0bSXiubo Li 
1319c22adc0bSXiubo Li 	vfree(udev->mb_addr);
1320c22adc0bSXiubo Li 	udev->mb_addr = NULL;
1321c22adc0bSXiubo Li 
1322c22adc0bSXiubo Li 	/* Upper layer should drain all requests before calling this */
1323c22adc0bSXiubo Li 	spin_lock_irq(&udev->commands_lock);
1324c22adc0bSXiubo Li 	idr_for_each_entry(&udev->commands, cmd, i) {
1325c22adc0bSXiubo Li 		if (tcmu_check_and_free_pending_cmd(cmd) != 0)
1326c22adc0bSXiubo Li 			all_expired = false;
1327c22adc0bSXiubo Li 	}
1328c22adc0bSXiubo Li 	idr_destroy(&udev->commands);
1329c22adc0bSXiubo Li 	spin_unlock_irq(&udev->commands_lock);
1330c22adc0bSXiubo Li 	WARN_ON(!all_expired);
1331c22adc0bSXiubo Li 
1332c22adc0bSXiubo Li 	tcmu_blocks_release(udev);
1333f3cdbe39SMike Christie 
1334f3cdbe39SMike Christie 	call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
1335f3cdbe39SMike Christie }
1336f3cdbe39SMike Christie 
13377c9e7a6fSAndy Grover static int tcmu_release(struct uio_info *info, struct inode *inode)
13387c9e7a6fSAndy Grover {
13397c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
13407c9e7a6fSAndy Grover 
13417c9e7a6fSAndy Grover 	clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
13427c9e7a6fSAndy Grover 
13437c9e7a6fSAndy Grover 	pr_debug("close\n");
13449260695dSMike Christie 	/* release ref from open */
1345f3cdbe39SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
13467c9e7a6fSAndy Grover 	return 0;
13477c9e7a6fSAndy Grover }
13487c9e7a6fSAndy Grover 
1349b3af66e2SMike Christie static void tcmu_init_genl_cmd_reply(struct tcmu_dev *udev, int cmd)
1350b3af66e2SMike Christie {
1351b3af66e2SMike Christie 	struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
1352b3af66e2SMike Christie 
1353b3af66e2SMike Christie 	if (!tcmu_kern_cmd_reply_supported)
1354b3af66e2SMike Christie 		return;
1355b3af66e2SMike Christie relock:
1356b3af66e2SMike Christie 	spin_lock(&udev->nl_cmd_lock);
1357b3af66e2SMike Christie 
1358b3af66e2SMike Christie 	if (nl_cmd->cmd != TCMU_CMD_UNSPEC) {
1359b3af66e2SMike Christie 		spin_unlock(&udev->nl_cmd_lock);
1360b3af66e2SMike Christie 		pr_debug("sleeping for open nl cmd\n");
1361b3af66e2SMike Christie 		wait_event(udev->nl_cmd_wq, (nl_cmd->cmd == TCMU_CMD_UNSPEC));
1362b3af66e2SMike Christie 		goto relock;
1363b3af66e2SMike Christie 	}
1364b3af66e2SMike Christie 
1365b3af66e2SMike Christie 	memset(nl_cmd, 0, sizeof(*nl_cmd));
1366b3af66e2SMike Christie 	nl_cmd->cmd = cmd;
1367b3af66e2SMike Christie 	init_completion(&nl_cmd->complete);
1368b3af66e2SMike Christie 
1369b3af66e2SMike Christie 	spin_unlock(&udev->nl_cmd_lock);
1370b3af66e2SMike Christie }
1371b3af66e2SMike Christie 
1372b3af66e2SMike Christie static int tcmu_wait_genl_cmd_reply(struct tcmu_dev *udev)
1373b3af66e2SMike Christie {
1374b3af66e2SMike Christie 	struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
1375b3af66e2SMike Christie 	int ret;
1376b3af66e2SMike Christie 	DEFINE_WAIT(__wait);
1377b3af66e2SMike Christie 
1378b3af66e2SMike Christie 	if (!tcmu_kern_cmd_reply_supported)
1379b3af66e2SMike Christie 		return 0;
1380b3af66e2SMike Christie 
1381b3af66e2SMike Christie 	pr_debug("sleeping for nl reply\n");
1382b3af66e2SMike Christie 	wait_for_completion(&nl_cmd->complete);
1383b3af66e2SMike Christie 
1384b3af66e2SMike Christie 	spin_lock(&udev->nl_cmd_lock);
1385b3af66e2SMike Christie 	nl_cmd->cmd = TCMU_CMD_UNSPEC;
1386b3af66e2SMike Christie 	ret = nl_cmd->status;
1387b3af66e2SMike Christie 	nl_cmd->status = 0;
1388b3af66e2SMike Christie 	spin_unlock(&udev->nl_cmd_lock);
1389b3af66e2SMike Christie 
1390b3af66e2SMike Christie 	wake_up_all(&udev->nl_cmd_wq);
1391b3af66e2SMike Christie 
1392b3af66e2SMike Christie 	return ret;;
1393b3af66e2SMike Christie }
1394b3af66e2SMike Christie 
1395b3af66e2SMike Christie static int tcmu_netlink_event(struct tcmu_dev *udev, enum tcmu_genl_cmd cmd,
1396b3af66e2SMike Christie 			      int reconfig_attr, const void *reconfig_data)
13977c9e7a6fSAndy Grover {
13987c9e7a6fSAndy Grover 	struct sk_buff *skb;
13997c9e7a6fSAndy Grover 	void *msg_header;
14006e14eab9SNicholas Bellinger 	int ret = -ENOMEM;
14017c9e7a6fSAndy Grover 
14027c9e7a6fSAndy Grover 	skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
14037c9e7a6fSAndy Grover 	if (!skb)
14046e14eab9SNicholas Bellinger 		return ret;
14057c9e7a6fSAndy Grover 
14067c9e7a6fSAndy Grover 	msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd);
14076e14eab9SNicholas Bellinger 	if (!msg_header)
14086e14eab9SNicholas Bellinger 		goto free_skb;
14097c9e7a6fSAndy Grover 
1410b3af66e2SMike Christie 	ret = nla_put_string(skb, TCMU_ATTR_DEVICE, udev->uio_info.name);
14116e14eab9SNicholas Bellinger 	if (ret < 0)
14126e14eab9SNicholas Bellinger 		goto free_skb;
14137c9e7a6fSAndy Grover 
1414b3af66e2SMike Christie 	ret = nla_put_u32(skb, TCMU_ATTR_MINOR, udev->uio_info.uio_dev->minor);
1415b3af66e2SMike Christie 	if (ret < 0)
1416b3af66e2SMike Christie 		goto free_skb;
1417b3af66e2SMike Christie 
1418b3af66e2SMike Christie 	ret = nla_put_u32(skb, TCMU_ATTR_DEVICE_ID, udev->se_dev.dev_index);
14196e14eab9SNicholas Bellinger 	if (ret < 0)
14206e14eab9SNicholas Bellinger 		goto free_skb;
14217c9e7a6fSAndy Grover 
14222d76443eSMike Christie 	if (cmd == TCMU_CMD_RECONFIG_DEVICE) {
14232d76443eSMike Christie 		switch (reconfig_attr) {
14242d76443eSMike Christie 		case TCMU_ATTR_DEV_CFG:
14252d76443eSMike Christie 			ret = nla_put_string(skb, reconfig_attr, reconfig_data);
14262d76443eSMike Christie 			break;
14272d76443eSMike Christie 		case TCMU_ATTR_DEV_SIZE:
14282d76443eSMike Christie 			ret = nla_put_u64_64bit(skb, reconfig_attr,
14292d76443eSMike Christie 						*((u64 *)reconfig_data),
14302d76443eSMike Christie 						TCMU_ATTR_PAD);
14312d76443eSMike Christie 			break;
14322d76443eSMike Christie 		case TCMU_ATTR_WRITECACHE:
14332d76443eSMike Christie 			ret = nla_put_u8(skb, reconfig_attr,
14342d76443eSMike Christie 					  *((u8 *)reconfig_data));
14352d76443eSMike Christie 			break;
14362d76443eSMike Christie 		default:
14372d76443eSMike Christie 			BUG();
14382d76443eSMike Christie 		}
14392d76443eSMike Christie 
14408a45885cSBryant G. Ly 		if (ret < 0)
14418a45885cSBryant G. Ly 			goto free_skb;
14422d76443eSMike Christie 	}
14438a45885cSBryant G. Ly 
1444053c095aSJohannes Berg 	genlmsg_end(skb, msg_header);
14457c9e7a6fSAndy Grover 
1446b3af66e2SMike Christie 	tcmu_init_genl_cmd_reply(udev, cmd);
1447b3af66e2SMike Christie 
144820c08b36SSheng Yang 	ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0,
14497c9e7a6fSAndy Grover 				TCMU_MCGRP_CONFIG, GFP_KERNEL);
14507c9e7a6fSAndy Grover 	/* We don't care if no one is listening */
14517c9e7a6fSAndy Grover 	if (ret == -ESRCH)
14527c9e7a6fSAndy Grover 		ret = 0;
1453b3af66e2SMike Christie 	if (!ret)
1454b3af66e2SMike Christie 		ret = tcmu_wait_genl_cmd_reply(udev);
14557c9e7a6fSAndy Grover 
14567c9e7a6fSAndy Grover 	return ret;
14576e14eab9SNicholas Bellinger free_skb:
14586e14eab9SNicholas Bellinger 	nlmsg_free(skb);
14596e14eab9SNicholas Bellinger 	return ret;
14607c9e7a6fSAndy Grover }
14617c9e7a6fSAndy Grover 
1462de8c5221SBryant G. Ly static int tcmu_update_uio_info(struct tcmu_dev *udev)
14637c9e7a6fSAndy Grover {
14647c9e7a6fSAndy Grover 	struct tcmu_hba *hba = udev->hba->hba_ptr;
14657c9e7a6fSAndy Grover 	struct uio_info *info;
1466de8c5221SBryant G. Ly 	size_t size, used;
14677c9e7a6fSAndy Grover 	char *str;
14687c9e7a6fSAndy Grover 
14697c9e7a6fSAndy Grover 	info = &udev->uio_info;
14707c9e7a6fSAndy Grover 	size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name,
14717c9e7a6fSAndy Grover 			udev->dev_config);
14727c9e7a6fSAndy Grover 	size += 1; /* for \0 */
14737c9e7a6fSAndy Grover 	str = kmalloc(size, GFP_KERNEL);
14747c9e7a6fSAndy Grover 	if (!str)
14757c9e7a6fSAndy Grover 		return -ENOMEM;
14767c9e7a6fSAndy Grover 
14777c9e7a6fSAndy Grover 	used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name);
14787c9e7a6fSAndy Grover 	if (udev->dev_config[0])
14797c9e7a6fSAndy Grover 		snprintf(str + used, size - used, "/%s", udev->dev_config);
14807c9e7a6fSAndy Grover 
1481ededd039SBryant G. Ly 	/* If the old string exists, free it */
1482ededd039SBryant G. Ly 	kfree(info->name);
14837c9e7a6fSAndy Grover 	info->name = str;
14847c9e7a6fSAndy Grover 
1485de8c5221SBryant G. Ly 	return 0;
1486de8c5221SBryant G. Ly }
1487de8c5221SBryant G. Ly 
1488de8c5221SBryant G. Ly static int tcmu_configure_device(struct se_device *dev)
1489de8c5221SBryant G. Ly {
1490de8c5221SBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(dev);
1491de8c5221SBryant G. Ly 	struct uio_info *info;
1492de8c5221SBryant G. Ly 	struct tcmu_mailbox *mb;
1493de8c5221SBryant G. Ly 	int ret = 0;
1494de8c5221SBryant G. Ly 
1495de8c5221SBryant G. Ly 	ret = tcmu_update_uio_info(udev);
1496de8c5221SBryant G. Ly 	if (ret)
1497de8c5221SBryant G. Ly 		return ret;
1498de8c5221SBryant G. Ly 
1499de8c5221SBryant G. Ly 	info = &udev->uio_info;
1500de8c5221SBryant G. Ly 
1501141685a3SXiubo Li 	udev->mb_addr = vzalloc(CMDR_SIZE);
15027c9e7a6fSAndy Grover 	if (!udev->mb_addr) {
15037c9e7a6fSAndy Grover 		ret = -ENOMEM;
15047c9e7a6fSAndy Grover 		goto err_vzalloc;
15057c9e7a6fSAndy Grover 	}
15067c9e7a6fSAndy Grover 
15077c9e7a6fSAndy Grover 	/* mailbox fits in first part of CMDR space */
15087c9e7a6fSAndy Grover 	udev->cmdr_size = CMDR_SIZE - CMDR_OFF;
15097c9e7a6fSAndy Grover 	udev->data_off = CMDR_SIZE;
1510141685a3SXiubo Li 	udev->data_size = DATA_SIZE;
1511b6df4b79SXiubo Li 	udev->dbi_thresh = 0; /* Default in Idle state */
1512b6df4b79SXiubo Li 	udev->waiting_global = false;
15137c9e7a6fSAndy Grover 
1514141685a3SXiubo Li 	/* Initialise the mailbox of the ring buffer */
15157c9e7a6fSAndy Grover 	mb = udev->mb_addr;
15160ad46af8SAndy Grover 	mb->version = TCMU_MAILBOX_VERSION;
151732c76de3SSheng Yang 	mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC;
15187c9e7a6fSAndy Grover 	mb->cmdr_off = CMDR_OFF;
15197c9e7a6fSAndy Grover 	mb->cmdr_size = udev->cmdr_size;
15207c9e7a6fSAndy Grover 
15217c9e7a6fSAndy Grover 	WARN_ON(!PAGE_ALIGNED(udev->data_off));
15227c9e7a6fSAndy Grover 	WARN_ON(udev->data_size % PAGE_SIZE);
152326418649SSheng Yang 	WARN_ON(udev->data_size % DATA_BLOCK_SIZE);
15247c9e7a6fSAndy Grover 
1525ac64a2ceSDavid Disseldorp 	info->version = __stringify(TCMU_MAILBOX_VERSION);
15267c9e7a6fSAndy Grover 
15277c9e7a6fSAndy Grover 	info->mem[0].name = "tcm-user command & data buffer";
15280633e123SArnd Bergmann 	info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr;
15297c9e7a6fSAndy Grover 	info->mem[0].size = TCMU_RING_SIZE;
1530141685a3SXiubo Li 	info->mem[0].memtype = UIO_MEM_NONE;
15317c9e7a6fSAndy Grover 
15327c9e7a6fSAndy Grover 	info->irqcontrol = tcmu_irqcontrol;
15337c9e7a6fSAndy Grover 	info->irq = UIO_IRQ_CUSTOM;
15347c9e7a6fSAndy Grover 
15357c9e7a6fSAndy Grover 	info->mmap = tcmu_mmap;
15367c9e7a6fSAndy Grover 	info->open = tcmu_open;
15377c9e7a6fSAndy Grover 	info->release = tcmu_release;
15387c9e7a6fSAndy Grover 
15397c9e7a6fSAndy Grover 	ret = uio_register_device(tcmu_root_device, info);
15407c9e7a6fSAndy Grover 	if (ret)
15417c9e7a6fSAndy Grover 		goto err_register;
15427c9e7a6fSAndy Grover 
154381ee28deSSheng Yang 	/* User can set hw_block_size before enable the device */
154481ee28deSSheng Yang 	if (dev->dev_attrib.hw_block_size == 0)
15457c9e7a6fSAndy Grover 		dev->dev_attrib.hw_block_size = 512;
154681ee28deSSheng Yang 	/* Other attributes can be configured in userspace */
15473abaa2bfSMike Christie 	if (!dev->dev_attrib.hw_max_sectors)
15487c9e7a6fSAndy Grover 		dev->dev_attrib.hw_max_sectors = 128;
15499a8bb606SBryant G. Ly 	if (!dev->dev_attrib.emulate_write_cache)
15509a8bb606SBryant G. Ly 		dev->dev_attrib.emulate_write_cache = 0;
15517c9e7a6fSAndy Grover 	dev->dev_attrib.hw_queue_depth = 128;
15527c9e7a6fSAndy Grover 
1553f3cdbe39SMike Christie 	/*
1554f3cdbe39SMike Christie 	 * Get a ref incase userspace does a close on the uio device before
1555f3cdbe39SMike Christie 	 * LIO has initiated tcmu_free_device.
1556f3cdbe39SMike Christie 	 */
1557f3cdbe39SMike Christie 	kref_get(&udev->kref);
1558f3cdbe39SMike Christie 
1559b3af66e2SMike Christie 	ret = tcmu_netlink_event(udev, TCMU_CMD_ADDED_DEVICE, 0, NULL);
15607c9e7a6fSAndy Grover 	if (ret)
15617c9e7a6fSAndy Grover 		goto err_netlink;
15627c9e7a6fSAndy Grover 
1563b6df4b79SXiubo Li 	mutex_lock(&root_udev_mutex);
1564b6df4b79SXiubo Li 	list_add(&udev->node, &root_udev);
1565b6df4b79SXiubo Li 	mutex_unlock(&root_udev_mutex);
1566b6df4b79SXiubo Li 
15677c9e7a6fSAndy Grover 	return 0;
15687c9e7a6fSAndy Grover 
15697c9e7a6fSAndy Grover err_netlink:
1570f3cdbe39SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
15717c9e7a6fSAndy Grover 	uio_unregister_device(&udev->uio_info);
15727c9e7a6fSAndy Grover err_register:
15737c9e7a6fSAndy Grover 	vfree(udev->mb_addr);
1574c22adc0bSXiubo Li 	udev->mb_addr = NULL;
15757c9e7a6fSAndy Grover err_vzalloc:
15767c9e7a6fSAndy Grover 	kfree(info->name);
1577f3cdbe39SMike Christie 	info->name = NULL;
15787c9e7a6fSAndy Grover 
15797c9e7a6fSAndy Grover 	return ret;
15807c9e7a6fSAndy Grover }
15817c9e7a6fSAndy Grover 
1582972c7f16SMike Christie static bool tcmu_dev_configured(struct tcmu_dev *udev)
1583972c7f16SMike Christie {
1584972c7f16SMike Christie 	return udev->uio_info.uio_dev ? true : false;
1585972c7f16SMike Christie }
1586972c7f16SMike Christie 
15877c9e7a6fSAndy Grover static void tcmu_free_device(struct se_device *dev)
15887c9e7a6fSAndy Grover {
15897c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
159092634706SMike Christie 
159192634706SMike Christie 	/* release ref from init */
159292634706SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
159392634706SMike Christie }
159492634706SMike Christie 
159592634706SMike Christie static void tcmu_destroy_device(struct se_device *dev)
159692634706SMike Christie {
159792634706SMike Christie 	struct tcmu_dev *udev = TCMU_DEV(dev);
15987c9e7a6fSAndy Grover 
15997c9e7a6fSAndy Grover 	del_timer_sync(&udev->timeout);
16007c9e7a6fSAndy Grover 
1601b6df4b79SXiubo Li 	mutex_lock(&root_udev_mutex);
1602b6df4b79SXiubo Li 	list_del(&udev->node);
1603b6df4b79SXiubo Li 	mutex_unlock(&root_udev_mutex);
1604b6df4b79SXiubo Li 
1605b3af66e2SMike Christie 	tcmu_netlink_event(udev, TCMU_CMD_REMOVED_DEVICE, 0, NULL);
16067c9e7a6fSAndy Grover 
16077c9e7a6fSAndy Grover 	uio_unregister_device(&udev->uio_info);
16089260695dSMike Christie 
16099260695dSMike Christie 	/* release ref from configure */
16109260695dSMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
16117c9e7a6fSAndy Grover }
16127c9e7a6fSAndy Grover 
16137c9e7a6fSAndy Grover enum {
16143abaa2bfSMike Christie 	Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors,
16157d7a7435SNicholas Bellinger 	Opt_err,
16167c9e7a6fSAndy Grover };
16177c9e7a6fSAndy Grover 
16187c9e7a6fSAndy Grover static match_table_t tokens = {
16197c9e7a6fSAndy Grover 	{Opt_dev_config, "dev_config=%s"},
16207c9e7a6fSAndy Grover 	{Opt_dev_size, "dev_size=%u"},
16219c1cd1b6SAndy Grover 	{Opt_hw_block_size, "hw_block_size=%u"},
16223abaa2bfSMike Christie 	{Opt_hw_max_sectors, "hw_max_sectors=%u"},
16237c9e7a6fSAndy Grover 	{Opt_err, NULL}
16247c9e7a6fSAndy Grover };
16257c9e7a6fSAndy Grover 
16263abaa2bfSMike Christie static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib)
16273abaa2bfSMike Christie {
16283abaa2bfSMike Christie 	unsigned long tmp_ul;
16293abaa2bfSMike Christie 	char *arg_p;
16303abaa2bfSMike Christie 	int ret;
16313abaa2bfSMike Christie 
16323abaa2bfSMike Christie 	arg_p = match_strdup(arg);
16333abaa2bfSMike Christie 	if (!arg_p)
16343abaa2bfSMike Christie 		return -ENOMEM;
16353abaa2bfSMike Christie 
16363abaa2bfSMike Christie 	ret = kstrtoul(arg_p, 0, &tmp_ul);
16373abaa2bfSMike Christie 	kfree(arg_p);
16383abaa2bfSMike Christie 	if (ret < 0) {
16393abaa2bfSMike Christie 		pr_err("kstrtoul() failed for dev attrib\n");
16403abaa2bfSMike Christie 		return ret;
16413abaa2bfSMike Christie 	}
16423abaa2bfSMike Christie 	if (!tmp_ul) {
16433abaa2bfSMike Christie 		pr_err("dev attrib must be nonzero\n");
16443abaa2bfSMike Christie 		return -EINVAL;
16453abaa2bfSMike Christie 	}
16463abaa2bfSMike Christie 	*dev_attrib = tmp_ul;
16473abaa2bfSMike Christie 	return 0;
16483abaa2bfSMike Christie }
16493abaa2bfSMike Christie 
16507c9e7a6fSAndy Grover static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev,
16517c9e7a6fSAndy Grover 		const char *page, ssize_t count)
16527c9e7a6fSAndy Grover {
16537c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
16547c9e7a6fSAndy Grover 	char *orig, *ptr, *opts, *arg_p;
16557c9e7a6fSAndy Grover 	substring_t args[MAX_OPT_ARGS];
16567c9e7a6fSAndy Grover 	int ret = 0, token;
16577c9e7a6fSAndy Grover 
16587c9e7a6fSAndy Grover 	opts = kstrdup(page, GFP_KERNEL);
16597c9e7a6fSAndy Grover 	if (!opts)
16607c9e7a6fSAndy Grover 		return -ENOMEM;
16617c9e7a6fSAndy Grover 
16627c9e7a6fSAndy Grover 	orig = opts;
16637c9e7a6fSAndy Grover 
16647c9e7a6fSAndy Grover 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
16657c9e7a6fSAndy Grover 		if (!*ptr)
16667c9e7a6fSAndy Grover 			continue;
16677c9e7a6fSAndy Grover 
16687c9e7a6fSAndy Grover 		token = match_token(ptr, tokens, args);
16697c9e7a6fSAndy Grover 		switch (token) {
16707c9e7a6fSAndy Grover 		case Opt_dev_config:
16717c9e7a6fSAndy Grover 			if (match_strlcpy(udev->dev_config, &args[0],
16727c9e7a6fSAndy Grover 					  TCMU_CONFIG_LEN) == 0) {
16737c9e7a6fSAndy Grover 				ret = -EINVAL;
16747c9e7a6fSAndy Grover 				break;
16757c9e7a6fSAndy Grover 			}
16767c9e7a6fSAndy Grover 			pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config);
16777c9e7a6fSAndy Grover 			break;
16787c9e7a6fSAndy Grover 		case Opt_dev_size:
16797c9e7a6fSAndy Grover 			arg_p = match_strdup(&args[0]);
16807c9e7a6fSAndy Grover 			if (!arg_p) {
16817c9e7a6fSAndy Grover 				ret = -ENOMEM;
16827c9e7a6fSAndy Grover 				break;
16837c9e7a6fSAndy Grover 			}
16847c9e7a6fSAndy Grover 			ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size);
16857c9e7a6fSAndy Grover 			kfree(arg_p);
16867c9e7a6fSAndy Grover 			if (ret < 0)
16877c9e7a6fSAndy Grover 				pr_err("kstrtoul() failed for dev_size=\n");
16887c9e7a6fSAndy Grover 			break;
16899c1cd1b6SAndy Grover 		case Opt_hw_block_size:
16903abaa2bfSMike Christie 			ret = tcmu_set_dev_attrib(&args[0],
16913abaa2bfSMike Christie 					&(dev->dev_attrib.hw_block_size));
16929c1cd1b6SAndy Grover 			break;
16933abaa2bfSMike Christie 		case Opt_hw_max_sectors:
16943abaa2bfSMike Christie 			ret = tcmu_set_dev_attrib(&args[0],
16953abaa2bfSMike Christie 					&(dev->dev_attrib.hw_max_sectors));
16969c1cd1b6SAndy Grover 			break;
16977c9e7a6fSAndy Grover 		default:
16987c9e7a6fSAndy Grover 			break;
16997c9e7a6fSAndy Grover 		}
17002579325cSMike Christie 
17012579325cSMike Christie 		if (ret)
17022579325cSMike Christie 			break;
17037c9e7a6fSAndy Grover 	}
17047c9e7a6fSAndy Grover 
17057c9e7a6fSAndy Grover 	kfree(orig);
17067c9e7a6fSAndy Grover 	return (!ret) ? count : ret;
17077c9e7a6fSAndy Grover }
17087c9e7a6fSAndy Grover 
17097c9e7a6fSAndy Grover static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b)
17107c9e7a6fSAndy Grover {
17117c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
17127c9e7a6fSAndy Grover 	ssize_t bl = 0;
17137c9e7a6fSAndy Grover 
17147c9e7a6fSAndy Grover 	bl = sprintf(b + bl, "Config: %s ",
17157c9e7a6fSAndy Grover 		     udev->dev_config[0] ? udev->dev_config : "NULL");
17167d7a7435SNicholas Bellinger 	bl += sprintf(b + bl, "Size: %zu\n", udev->dev_size);
17177c9e7a6fSAndy Grover 
17187c9e7a6fSAndy Grover 	return bl;
17197c9e7a6fSAndy Grover }
17207c9e7a6fSAndy Grover 
17217c9e7a6fSAndy Grover static sector_t tcmu_get_blocks(struct se_device *dev)
17227c9e7a6fSAndy Grover {
17237c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
17247c9e7a6fSAndy Grover 
17257c9e7a6fSAndy Grover 	return div_u64(udev->dev_size - dev->dev_attrib.block_size,
17267c9e7a6fSAndy Grover 		       dev->dev_attrib.block_size);
17277c9e7a6fSAndy Grover }
17287c9e7a6fSAndy Grover 
17297c9e7a6fSAndy Grover static sense_reason_t
17307c9e7a6fSAndy Grover tcmu_parse_cdb(struct se_cmd *cmd)
17317c9e7a6fSAndy Grover {
173202eb924fSAndy Grover 	return passthrough_parse_cdb(cmd, tcmu_queue_cmd);
17339c1cd1b6SAndy Grover }
17349c1cd1b6SAndy Grover 
17357d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page)
17367d7a7435SNicholas Bellinger {
17377d7a7435SNicholas Bellinger 	struct se_dev_attrib *da = container_of(to_config_group(item),
17387d7a7435SNicholas Bellinger 					struct se_dev_attrib, da_group);
1739b5ab697cSKenjiro Nakayama 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
17407d7a7435SNicholas Bellinger 
17417d7a7435SNicholas Bellinger 	return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC);
17427d7a7435SNicholas Bellinger }
17437d7a7435SNicholas Bellinger 
17447d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page,
17457d7a7435SNicholas Bellinger 				       size_t count)
17467d7a7435SNicholas Bellinger {
17477d7a7435SNicholas Bellinger 	struct se_dev_attrib *da = container_of(to_config_group(item),
17487d7a7435SNicholas Bellinger 					struct se_dev_attrib, da_group);
17497d7a7435SNicholas Bellinger 	struct tcmu_dev *udev = container_of(da->da_dev,
17507d7a7435SNicholas Bellinger 					struct tcmu_dev, se_dev);
17517d7a7435SNicholas Bellinger 	u32 val;
17527d7a7435SNicholas Bellinger 	int ret;
17537d7a7435SNicholas Bellinger 
17547d7a7435SNicholas Bellinger 	if (da->da_dev->export_count) {
17557d7a7435SNicholas Bellinger 		pr_err("Unable to set tcmu cmd_time_out while exports exist\n");
17567d7a7435SNicholas Bellinger 		return -EINVAL;
17577d7a7435SNicholas Bellinger 	}
17587d7a7435SNicholas Bellinger 
17597d7a7435SNicholas Bellinger 	ret = kstrtou32(page, 0, &val);
17607d7a7435SNicholas Bellinger 	if (ret < 0)
17617d7a7435SNicholas Bellinger 		return ret;
17627d7a7435SNicholas Bellinger 
17637d7a7435SNicholas Bellinger 	udev->cmd_time_out = val * MSEC_PER_SEC;
17647d7a7435SNicholas Bellinger 	return count;
17657d7a7435SNicholas Bellinger }
17667d7a7435SNicholas Bellinger CONFIGFS_ATTR(tcmu_, cmd_time_out);
17677d7a7435SNicholas Bellinger 
17682d76443eSMike Christie static ssize_t tcmu_dev_config_show(struct config_item *item, char *page)
1769ee018252SBryant G. Ly {
1770ee018252SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
1771ee018252SBryant G. Ly 						struct se_dev_attrib, da_group);
1772ee018252SBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
1773ee018252SBryant G. Ly 
1774ee018252SBryant G. Ly 	return snprintf(page, PAGE_SIZE, "%s\n", udev->dev_config);
1775ee018252SBryant G. Ly }
1776ee018252SBryant G. Ly 
17772d76443eSMike Christie static ssize_t tcmu_dev_config_store(struct config_item *item, const char *page,
1778ee018252SBryant G. Ly 				     size_t count)
1779ee018252SBryant G. Ly {
1780ee018252SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
1781ee018252SBryant G. Ly 						struct se_dev_attrib, da_group);
1782ee018252SBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
17832d76443eSMike Christie 	int ret, len;
1784ee018252SBryant G. Ly 
17852d76443eSMike Christie 	len = strlen(page);
17862d76443eSMike Christie 	if (!len || len > TCMU_CONFIG_LEN - 1)
1787ee018252SBryant G. Ly 		return -EINVAL;
1788ee018252SBryant G. Ly 
1789ee018252SBryant G. Ly 	/* Check if device has been configured before */
1790ee018252SBryant G. Ly 	if (tcmu_dev_configured(udev)) {
1791b3af66e2SMike Christie 		ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE,
17922d76443eSMike Christie 					 TCMU_ATTR_DEV_CFG, page);
1793ee018252SBryant G. Ly 		if (ret) {
1794ee018252SBryant G. Ly 			pr_err("Unable to reconfigure device\n");
1795ee018252SBryant G. Ly 			return ret;
1796ee018252SBryant G. Ly 		}
1797de8c5221SBryant G. Ly 		strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN);
1798de8c5221SBryant G. Ly 
1799de8c5221SBryant G. Ly 		ret = tcmu_update_uio_info(udev);
1800de8c5221SBryant G. Ly 		if (ret)
1801de8c5221SBryant G. Ly 			return ret;
1802de8c5221SBryant G. Ly 		return count;
1803ee018252SBryant G. Ly 	}
18042d76443eSMike Christie 	strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN);
1805ee018252SBryant G. Ly 
1806ee018252SBryant G. Ly 	return count;
1807ee018252SBryant G. Ly }
18082d76443eSMike Christie CONFIGFS_ATTR(tcmu_, dev_config);
1809ee018252SBryant G. Ly 
1810801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_show(struct config_item *item, char *page)
1811801fc54dSBryant G. Ly {
1812801fc54dSBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
1813801fc54dSBryant G. Ly 						struct se_dev_attrib, da_group);
1814801fc54dSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
1815801fc54dSBryant G. Ly 
1816801fc54dSBryant G. Ly 	return snprintf(page, PAGE_SIZE, "%zu\n", udev->dev_size);
1817801fc54dSBryant G. Ly }
1818801fc54dSBryant G. Ly 
1819801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page,
1820801fc54dSBryant G. Ly 				   size_t count)
1821801fc54dSBryant G. Ly {
1822801fc54dSBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
1823801fc54dSBryant G. Ly 						struct se_dev_attrib, da_group);
1824801fc54dSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
18252d76443eSMike Christie 	u64 val;
1826801fc54dSBryant G. Ly 	int ret;
1827801fc54dSBryant G. Ly 
18282d76443eSMike Christie 	ret = kstrtou64(page, 0, &val);
1829801fc54dSBryant G. Ly 	if (ret < 0)
1830801fc54dSBryant G. Ly 		return ret;
1831801fc54dSBryant G. Ly 
1832801fc54dSBryant G. Ly 	/* Check if device has been configured before */
1833801fc54dSBryant G. Ly 	if (tcmu_dev_configured(udev)) {
1834b3af66e2SMike Christie 		ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE,
18352d76443eSMike Christie 					 TCMU_ATTR_DEV_SIZE, &val);
1836801fc54dSBryant G. Ly 		if (ret) {
1837801fc54dSBryant G. Ly 			pr_err("Unable to reconfigure device\n");
1838801fc54dSBryant G. Ly 			return ret;
1839801fc54dSBryant G. Ly 		}
1840801fc54dSBryant G. Ly 	}
18412d76443eSMike Christie 	udev->dev_size = val;
1842801fc54dSBryant G. Ly 	return count;
1843801fc54dSBryant G. Ly }
1844801fc54dSBryant G. Ly CONFIGFS_ATTR(tcmu_, dev_size);
1845801fc54dSBryant G. Ly 
18469a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_show(struct config_item *item,
18479a8bb606SBryant G. Ly 					     char *page)
18489a8bb606SBryant G. Ly {
18499a8bb606SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
18509a8bb606SBryant G. Ly 					struct se_dev_attrib, da_group);
18519a8bb606SBryant G. Ly 
18529a8bb606SBryant G. Ly 	return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache);
18539a8bb606SBryant G. Ly }
18549a8bb606SBryant G. Ly 
18559a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_store(struct config_item *item,
18569a8bb606SBryant G. Ly 					      const char *page, size_t count)
18579a8bb606SBryant G. Ly {
18589a8bb606SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
18599a8bb606SBryant G. Ly 					struct se_dev_attrib, da_group);
18601068be7bSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
18612d76443eSMike Christie 	u8 val;
18629a8bb606SBryant G. Ly 	int ret;
18639a8bb606SBryant G. Ly 
18642d76443eSMike Christie 	ret = kstrtou8(page, 0, &val);
18659a8bb606SBryant G. Ly 	if (ret < 0)
18669a8bb606SBryant G. Ly 		return ret;
18679a8bb606SBryant G. Ly 
18681068be7bSBryant G. Ly 	/* Check if device has been configured before */
18691068be7bSBryant G. Ly 	if (tcmu_dev_configured(udev)) {
1870b3af66e2SMike Christie 		ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE,
18712d76443eSMike Christie 					 TCMU_ATTR_WRITECACHE, &val);
18721068be7bSBryant G. Ly 		if (ret) {
18731068be7bSBryant G. Ly 			pr_err("Unable to reconfigure device\n");
18741068be7bSBryant G. Ly 			return ret;
18751068be7bSBryant G. Ly 		}
18761068be7bSBryant G. Ly 	}
18772d76443eSMike Christie 
18782d76443eSMike Christie 	da->emulate_write_cache = val;
18799a8bb606SBryant G. Ly 	return count;
18809a8bb606SBryant G. Ly }
18819a8bb606SBryant G. Ly CONFIGFS_ATTR(tcmu_, emulate_write_cache);
18829a8bb606SBryant G. Ly 
18835821783bSColin Ian King static struct configfs_attribute *tcmu_attrib_attrs[] = {
1884801fc54dSBryant G. Ly 	&tcmu_attr_cmd_time_out,
18852d76443eSMike Christie 	&tcmu_attr_dev_config,
1886801fc54dSBryant G. Ly 	&tcmu_attr_dev_size,
1887801fc54dSBryant G. Ly 	&tcmu_attr_emulate_write_cache,
1888801fc54dSBryant G. Ly 	NULL,
1889801fc54dSBryant G. Ly };
1890801fc54dSBryant G. Ly 
18917d7a7435SNicholas Bellinger static struct configfs_attribute **tcmu_attrs;
18927d7a7435SNicholas Bellinger 
18937d7a7435SNicholas Bellinger static struct target_backend_ops tcmu_ops = {
18947c9e7a6fSAndy Grover 	.name			= "user",
18957c9e7a6fSAndy Grover 	.owner			= THIS_MODULE,
1896a3541703SAndy Grover 	.transport_flags	= TRANSPORT_FLAG_PASSTHROUGH,
18977c9e7a6fSAndy Grover 	.attach_hba		= tcmu_attach_hba,
18987c9e7a6fSAndy Grover 	.detach_hba		= tcmu_detach_hba,
18997c9e7a6fSAndy Grover 	.alloc_device		= tcmu_alloc_device,
19007c9e7a6fSAndy Grover 	.configure_device	= tcmu_configure_device,
190192634706SMike Christie 	.destroy_device		= tcmu_destroy_device,
19027c9e7a6fSAndy Grover 	.free_device		= tcmu_free_device,
19037c9e7a6fSAndy Grover 	.parse_cdb		= tcmu_parse_cdb,
19047c9e7a6fSAndy Grover 	.set_configfs_dev_params = tcmu_set_configfs_dev_params,
19057c9e7a6fSAndy Grover 	.show_configfs_dev_params = tcmu_show_configfs_dev_params,
19067c9e7a6fSAndy Grover 	.get_device_type	= sbc_get_device_type,
19077c9e7a6fSAndy Grover 	.get_blocks		= tcmu_get_blocks,
19087d7a7435SNicholas Bellinger 	.tb_dev_attrib_attrs	= NULL,
19097c9e7a6fSAndy Grover };
19107c9e7a6fSAndy Grover 
1911b6df4b79SXiubo Li static int unmap_thread_fn(void *data)
1912b6df4b79SXiubo Li {
1913b6df4b79SXiubo Li 	struct tcmu_dev *udev;
1914b6df4b79SXiubo Li 	loff_t off;
1915b6df4b79SXiubo Li 	uint32_t start, end, block;
1916b6df4b79SXiubo Li 	struct page *page;
1917b6df4b79SXiubo Li 	int i;
1918b6df4b79SXiubo Li 
191907932a02SXiubo Li 	while (!kthread_should_stop()) {
1920b6df4b79SXiubo Li 		DEFINE_WAIT(__wait);
1921b6df4b79SXiubo Li 
1922b6df4b79SXiubo Li 		prepare_to_wait(&unmap_wait, &__wait, TASK_INTERRUPTIBLE);
1923b6df4b79SXiubo Li 		schedule();
1924b6df4b79SXiubo Li 		finish_wait(&unmap_wait, &__wait);
1925b6df4b79SXiubo Li 
1926d906d8afSMike Christie 		if (kthread_should_stop())
1927d906d8afSMike Christie 			break;
1928d906d8afSMike Christie 
1929b6df4b79SXiubo Li 		mutex_lock(&root_udev_mutex);
1930b6df4b79SXiubo Li 		list_for_each_entry(udev, &root_udev, node) {
1931b6df4b79SXiubo Li 			mutex_lock(&udev->cmdr_lock);
1932b6df4b79SXiubo Li 
1933b6df4b79SXiubo Li 			/* Try to complete the finished commands first */
1934b6df4b79SXiubo Li 			tcmu_handle_completions(udev);
1935b6df4b79SXiubo Li 
1936b6df4b79SXiubo Li 			/* Skip the udevs waiting the global pool or in idle */
1937b6df4b79SXiubo Li 			if (udev->waiting_global || !udev->dbi_thresh) {
1938b6df4b79SXiubo Li 				mutex_unlock(&udev->cmdr_lock);
1939b6df4b79SXiubo Li 				continue;
1940b6df4b79SXiubo Li 			}
1941b6df4b79SXiubo Li 
1942b6df4b79SXiubo Li 			end = udev->dbi_max + 1;
1943b6df4b79SXiubo Li 			block = find_last_bit(udev->data_bitmap, end);
1944b6df4b79SXiubo Li 			if (block == udev->dbi_max) {
1945b6df4b79SXiubo Li 				/*
1946b6df4b79SXiubo Li 				 * The last bit is dbi_max, so there is
1947b6df4b79SXiubo Li 				 * no need to shrink any blocks.
1948b6df4b79SXiubo Li 				 */
1949b6df4b79SXiubo Li 				mutex_unlock(&udev->cmdr_lock);
1950b6df4b79SXiubo Li 				continue;
1951b6df4b79SXiubo Li 			} else if (block == end) {
1952b6df4b79SXiubo Li 				/* The current udev will goto idle state */
1953b6df4b79SXiubo Li 				udev->dbi_thresh = start = 0;
1954b6df4b79SXiubo Li 				udev->dbi_max = 0;
1955b6df4b79SXiubo Li 			} else {
1956b6df4b79SXiubo Li 				udev->dbi_thresh = start = block + 1;
1957b6df4b79SXiubo Li 				udev->dbi_max = block;
1958b6df4b79SXiubo Li 			}
1959b6df4b79SXiubo Li 
1960b6df4b79SXiubo Li 			/* Here will truncate the data area from off */
1961b6df4b79SXiubo Li 			off = udev->data_off + start * DATA_BLOCK_SIZE;
1962b6df4b79SXiubo Li 			unmap_mapping_range(udev->inode->i_mapping, off, 0, 1);
1963b6df4b79SXiubo Li 
1964b6df4b79SXiubo Li 			/* Release the block pages */
1965b6df4b79SXiubo Li 			for (i = start; i < end; i++) {
1966b6df4b79SXiubo Li 				page = radix_tree_delete(&udev->data_blocks, i);
1967b6df4b79SXiubo Li 				if (page) {
1968b6df4b79SXiubo Li 					__free_page(page);
1969b6df4b79SXiubo Li 					atomic_dec(&global_db_count);
1970b6df4b79SXiubo Li 				}
1971b6df4b79SXiubo Li 			}
1972b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
1973b6df4b79SXiubo Li 		}
1974b6df4b79SXiubo Li 
1975b6df4b79SXiubo Li 		/*
1976b6df4b79SXiubo Li 		 * Try to wake up the udevs who are waiting
1977b6df4b79SXiubo Li 		 * for the global data pool.
1978b6df4b79SXiubo Li 		 */
1979b6df4b79SXiubo Li 		list_for_each_entry(udev, &root_udev, node) {
1980b6df4b79SXiubo Li 			if (udev->waiting_global)
1981b6df4b79SXiubo Li 				wake_up(&udev->wait_cmdr);
1982b6df4b79SXiubo Li 		}
1983b6df4b79SXiubo Li 		mutex_unlock(&root_udev_mutex);
1984b6df4b79SXiubo Li 	}
1985b6df4b79SXiubo Li 
1986b6df4b79SXiubo Li 	return 0;
1987b6df4b79SXiubo Li }
1988b6df4b79SXiubo Li 
19897c9e7a6fSAndy Grover static int __init tcmu_module_init(void)
19907c9e7a6fSAndy Grover {
1991801fc54dSBryant G. Ly 	int ret, i, k, len = 0;
19927c9e7a6fSAndy Grover 
19937c9e7a6fSAndy Grover 	BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
19947c9e7a6fSAndy Grover 
19957c9e7a6fSAndy Grover 	tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
19967c9e7a6fSAndy Grover 				sizeof(struct tcmu_cmd),
19977c9e7a6fSAndy Grover 				__alignof__(struct tcmu_cmd),
19987c9e7a6fSAndy Grover 				0, NULL);
19997c9e7a6fSAndy Grover 	if (!tcmu_cmd_cache)
20007c9e7a6fSAndy Grover 		return -ENOMEM;
20017c9e7a6fSAndy Grover 
20027c9e7a6fSAndy Grover 	tcmu_root_device = root_device_register("tcm_user");
20037c9e7a6fSAndy Grover 	if (IS_ERR(tcmu_root_device)) {
20047c9e7a6fSAndy Grover 		ret = PTR_ERR(tcmu_root_device);
20057c9e7a6fSAndy Grover 		goto out_free_cache;
20067c9e7a6fSAndy Grover 	}
20077c9e7a6fSAndy Grover 
20087c9e7a6fSAndy Grover 	ret = genl_register_family(&tcmu_genl_family);
20097c9e7a6fSAndy Grover 	if (ret < 0) {
20107c9e7a6fSAndy Grover 		goto out_unreg_device;
20117c9e7a6fSAndy Grover 	}
20127c9e7a6fSAndy Grover 
20137d7a7435SNicholas Bellinger 	for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) {
20147d7a7435SNicholas Bellinger 		len += sizeof(struct configfs_attribute *);
20157d7a7435SNicholas Bellinger 	}
2016801fc54dSBryant G. Ly 	for (i = 0; tcmu_attrib_attrs[i] != NULL; i++) {
2017801fc54dSBryant G. Ly 		len += sizeof(struct configfs_attribute *);
2018801fc54dSBryant G. Ly 	}
2019801fc54dSBryant G. Ly 	len += sizeof(struct configfs_attribute *);
20207d7a7435SNicholas Bellinger 
20217d7a7435SNicholas Bellinger 	tcmu_attrs = kzalloc(len, GFP_KERNEL);
20227d7a7435SNicholas Bellinger 	if (!tcmu_attrs) {
20237d7a7435SNicholas Bellinger 		ret = -ENOMEM;
20247d7a7435SNicholas Bellinger 		goto out_unreg_genl;
20257d7a7435SNicholas Bellinger 	}
20267d7a7435SNicholas Bellinger 
20277d7a7435SNicholas Bellinger 	for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) {
20287d7a7435SNicholas Bellinger 		tcmu_attrs[i] = passthrough_attrib_attrs[i];
20297d7a7435SNicholas Bellinger 	}
2030801fc54dSBryant G. Ly 	for (k = 0; tcmu_attrib_attrs[k] != NULL; k++) {
2031801fc54dSBryant G. Ly 		tcmu_attrs[i] = tcmu_attrib_attrs[k];
20329a8bb606SBryant G. Ly 		i++;
2033801fc54dSBryant G. Ly 	}
20347d7a7435SNicholas Bellinger 	tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs;
20357d7a7435SNicholas Bellinger 
20360a06d430SChristoph Hellwig 	ret = transport_backend_register(&tcmu_ops);
20377c9e7a6fSAndy Grover 	if (ret)
20387d7a7435SNicholas Bellinger 		goto out_attrs;
20397c9e7a6fSAndy Grover 
2040b6df4b79SXiubo Li 	init_waitqueue_head(&unmap_wait);
2041b6df4b79SXiubo Li 	unmap_thread = kthread_run(unmap_thread_fn, NULL, "tcmu_unmap");
2042b6df4b79SXiubo Li 	if (IS_ERR(unmap_thread)) {
2043b6df4b79SXiubo Li 		ret = PTR_ERR(unmap_thread);
2044b6df4b79SXiubo Li 		goto out_unreg_transport;
2045b6df4b79SXiubo Li 	}
2046b6df4b79SXiubo Li 
20477c9e7a6fSAndy Grover 	return 0;
20487c9e7a6fSAndy Grover 
2049b6df4b79SXiubo Li out_unreg_transport:
2050b6df4b79SXiubo Li 	target_backend_unregister(&tcmu_ops);
20517d7a7435SNicholas Bellinger out_attrs:
20527d7a7435SNicholas Bellinger 	kfree(tcmu_attrs);
20537c9e7a6fSAndy Grover out_unreg_genl:
20547c9e7a6fSAndy Grover 	genl_unregister_family(&tcmu_genl_family);
20557c9e7a6fSAndy Grover out_unreg_device:
20567c9e7a6fSAndy Grover 	root_device_unregister(tcmu_root_device);
20577c9e7a6fSAndy Grover out_free_cache:
20587c9e7a6fSAndy Grover 	kmem_cache_destroy(tcmu_cmd_cache);
20597c9e7a6fSAndy Grover 
20607c9e7a6fSAndy Grover 	return ret;
20617c9e7a6fSAndy Grover }
20627c9e7a6fSAndy Grover 
20637c9e7a6fSAndy Grover static void __exit tcmu_module_exit(void)
20647c9e7a6fSAndy Grover {
2065b6df4b79SXiubo Li 	kthread_stop(unmap_thread);
20660a06d430SChristoph Hellwig 	target_backend_unregister(&tcmu_ops);
20677d7a7435SNicholas Bellinger 	kfree(tcmu_attrs);
20687c9e7a6fSAndy Grover 	genl_unregister_family(&tcmu_genl_family);
20697c9e7a6fSAndy Grover 	root_device_unregister(tcmu_root_device);
20707c9e7a6fSAndy Grover 	kmem_cache_destroy(tcmu_cmd_cache);
20717c9e7a6fSAndy Grover }
20727c9e7a6fSAndy Grover 
20737c9e7a6fSAndy Grover MODULE_DESCRIPTION("TCM USER subsystem plugin");
20747c9e7a6fSAndy Grover MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
20757c9e7a6fSAndy Grover MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
20767c9e7a6fSAndy Grover MODULE_LICENSE("GPL");
20777c9e7a6fSAndy Grover 
20787c9e7a6fSAndy Grover module_init(tcmu_module_init);
20797c9e7a6fSAndy Grover module_exit(tcmu_module_exit);
2080